Keep reserved bits in CPSR on write
[deliverable/binutils-gdb.git] / gdb / dwarf2read.c
CommitLineData
c906108c 1/* DWARF 2 debugging format support for GDB.
917c78fc 2
618f726f 3 Copyright (C) 1994-2016 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>
325fac50 76#include <algorithm>
d8151005 77
34eaf542
TT
78typedef struct symbol *symbolp;
79DEF_VEC_P (symbolp);
80
73be47f5
DE
81/* When == 1, print basic high level tracing messages.
82 When > 1, be more verbose.
b4f54984
DE
83 This is in contrast to the low level DIE reading of dwarf_die_debug. */
84static unsigned int dwarf_read_debug = 0;
45cfd468 85
d97bc12b 86/* When non-zero, dump DIEs after they are read in. */
b4f54984 87static unsigned int dwarf_die_debug = 0;
d97bc12b 88
27e0867f
DE
89/* When non-zero, dump line number entries as they are read in. */
90static unsigned int dwarf_line_debug = 0;
91
900e11f9
JK
92/* When non-zero, cross-check physname against demangler. */
93static int check_physname = 0;
94
481860b3 95/* When non-zero, do not reject deprecated .gdb_index sections. */
e615022a 96static int use_deprecated_index_sections = 0;
481860b3 97
6502dd73
DJ
98static const struct objfile_data *dwarf2_objfile_data_key;
99
f1e6e072
TT
100/* The "aclass" indices for various kinds of computed DWARF symbols. */
101
102static int dwarf2_locexpr_index;
103static int dwarf2_loclist_index;
104static int dwarf2_locexpr_block_index;
105static int dwarf2_loclist_block_index;
106
73869dc2
DE
107/* A descriptor for dwarf sections.
108
109 S.ASECTION, SIZE are typically initialized when the objfile is first
110 scanned. BUFFER, READIN are filled in later when the section is read.
111 If the section contained compressed data then SIZE is updated to record
112 the uncompressed size of the section.
113
114 DWP file format V2 introduces a wrinkle that is easiest to handle by
115 creating the concept of virtual sections contained within a real section.
116 In DWP V2 the sections of the input DWO files are concatenated together
117 into one section, but section offsets are kept relative to the original
118 input section.
119 If this is a virtual dwp-v2 section, S.CONTAINING_SECTION is a backlink to
120 the real section this "virtual" section is contained in, and BUFFER,SIZE
121 describe the virtual section. */
122
dce234bc
PP
123struct dwarf2_section_info
124{
73869dc2
DE
125 union
126 {
e5aa3347 127 /* If this is a real section, the bfd section. */
049412e3 128 asection *section;
73869dc2 129 /* If this is a virtual section, pointer to the containing ("real")
e5aa3347 130 section. */
73869dc2
DE
131 struct dwarf2_section_info *containing_section;
132 } s;
19ac8c2e 133 /* Pointer to section data, only valid if readin. */
d521ce57 134 const gdb_byte *buffer;
73869dc2 135 /* The size of the section, real or virtual. */
dce234bc 136 bfd_size_type size;
73869dc2
DE
137 /* If this is a virtual section, the offset in the real section.
138 Only valid if is_virtual. */
139 bfd_size_type virtual_offset;
be391dca 140 /* True if we have tried to read this section. */
73869dc2
DE
141 char readin;
142 /* True if this is a virtual section, False otherwise.
049412e3 143 This specifies which of s.section and s.containing_section to use. */
73869dc2 144 char is_virtual;
dce234bc
PP
145};
146
8b70b953
TT
147typedef struct dwarf2_section_info dwarf2_section_info_def;
148DEF_VEC_O (dwarf2_section_info_def);
149
9291a0cd
TT
150/* All offsets in the index are of this type. It must be
151 architecture-independent. */
152typedef uint32_t offset_type;
153
154DEF_VEC_I (offset_type);
155
156942c7
DE
156/* Ensure only legit values are used. */
157#define DW2_GDB_INDEX_SYMBOL_STATIC_SET_VALUE(cu_index, value) \
158 do { \
159 gdb_assert ((unsigned int) (value) <= 1); \
160 GDB_INDEX_SYMBOL_STATIC_SET_VALUE((cu_index), (value)); \
161 } while (0)
162
163/* Ensure only legit values are used. */
164#define DW2_GDB_INDEX_SYMBOL_KIND_SET_VALUE(cu_index, value) \
165 do { \
166 gdb_assert ((value) >= GDB_INDEX_SYMBOL_KIND_TYPE \
167 && (value) <= GDB_INDEX_SYMBOL_KIND_OTHER); \
168 GDB_INDEX_SYMBOL_KIND_SET_VALUE((cu_index), (value)); \
169 } while (0)
170
171/* Ensure we don't use more than the alloted nuber of bits for the CU. */
172#define DW2_GDB_INDEX_CU_SET_VALUE(cu_index, value) \
173 do { \
174 gdb_assert (((value) & ~GDB_INDEX_CU_MASK) == 0); \
175 GDB_INDEX_CU_SET_VALUE((cu_index), (value)); \
176 } while (0)
177
9291a0cd
TT
178/* A description of the mapped index. The file format is described in
179 a comment by the code that writes the index. */
180struct mapped_index
181{
559a7a62
JK
182 /* Index data format version. */
183 int version;
184
9291a0cd
TT
185 /* The total length of the buffer. */
186 off_t total_size;
b11b1f88 187
9291a0cd
TT
188 /* A pointer to the address table data. */
189 const gdb_byte *address_table;
b11b1f88 190
9291a0cd
TT
191 /* Size of the address table data in bytes. */
192 offset_type address_table_size;
b11b1f88 193
3876f04e
DE
194 /* The symbol table, implemented as a hash table. */
195 const offset_type *symbol_table;
b11b1f88 196
9291a0cd 197 /* Size in slots, each slot is 2 offset_types. */
3876f04e 198 offset_type symbol_table_slots;
b11b1f88 199
9291a0cd
TT
200 /* A pointer to the constant pool. */
201 const char *constant_pool;
202};
203
95554aad
TT
204typedef struct dwarf2_per_cu_data *dwarf2_per_cu_ptr;
205DEF_VEC_P (dwarf2_per_cu_ptr);
206
52059ffd
TT
207struct tu_stats
208{
209 int nr_uniq_abbrev_tables;
210 int nr_symtabs;
211 int nr_symtab_sharers;
212 int nr_stmt_less_type_units;
213 int nr_all_type_units_reallocs;
214};
215
9cdd5dbd
DE
216/* Collection of data recorded per objfile.
217 This hangs off of dwarf2_objfile_data_key. */
218
6502dd73
DJ
219struct dwarf2_per_objfile
220{
dce234bc
PP
221 struct dwarf2_section_info info;
222 struct dwarf2_section_info abbrev;
223 struct dwarf2_section_info line;
dce234bc
PP
224 struct dwarf2_section_info loc;
225 struct dwarf2_section_info macinfo;
cf2c3c16 226 struct dwarf2_section_info macro;
dce234bc
PP
227 struct dwarf2_section_info str;
228 struct dwarf2_section_info ranges;
3019eac3 229 struct dwarf2_section_info addr;
dce234bc
PP
230 struct dwarf2_section_info frame;
231 struct dwarf2_section_info eh_frame;
9291a0cd 232 struct dwarf2_section_info gdb_index;
ae038cb0 233
8b70b953
TT
234 VEC (dwarf2_section_info_def) *types;
235
be391dca
TT
236 /* Back link. */
237 struct objfile *objfile;
238
d467dd73 239 /* Table of all the compilation units. This is used to locate
10b3939b 240 the target compilation unit of a particular reference. */
ae038cb0
DJ
241 struct dwarf2_per_cu_data **all_comp_units;
242
243 /* The number of compilation units in ALL_COMP_UNITS. */
244 int n_comp_units;
245
1fd400ff 246 /* The number of .debug_types-related CUs. */
d467dd73 247 int n_type_units;
1fd400ff 248
6aa5f3a6
DE
249 /* The number of elements allocated in all_type_units.
250 If there are skeleton-less TUs, we add them to all_type_units lazily. */
251 int n_allocated_type_units;
252
a2ce51a0
DE
253 /* The .debug_types-related CUs (TUs).
254 This is stored in malloc space because we may realloc it. */
b4dd5633 255 struct signatured_type **all_type_units;
1fd400ff 256
f4dc4d17
DE
257 /* Table of struct type_unit_group objects.
258 The hash key is the DW_AT_stmt_list value. */
259 htab_t type_unit_groups;
72dca2f5 260
348e048f
DE
261 /* A table mapping .debug_types signatures to its signatured_type entry.
262 This is NULL if the .debug_types section hasn't been read in yet. */
263 htab_t signatured_types;
264
f4dc4d17
DE
265 /* Type unit statistics, to see how well the scaling improvements
266 are doing. */
52059ffd 267 struct tu_stats tu_stats;
f4dc4d17
DE
268
269 /* A chain of compilation units that are currently read in, so that
270 they can be freed later. */
271 struct dwarf2_per_cu_data *read_in_chain;
272
3019eac3
DE
273 /* A table mapping DW_AT_dwo_name values to struct dwo_file objects.
274 This is NULL if the table hasn't been allocated yet. */
275 htab_t dwo_files;
276
80626a55
DE
277 /* Non-zero if we've check for whether there is a DWP file. */
278 int dwp_checked;
279
280 /* The DWP file if there is one, or NULL. */
281 struct dwp_file *dwp_file;
282
36586728
TT
283 /* The shared '.dwz' file, if one exists. This is used when the
284 original data was compressed using 'dwz -m'. */
285 struct dwz_file *dwz_file;
286
72dca2f5
FR
287 /* A flag indicating wether this objfile has a section loaded at a
288 VMA of 0. */
289 int has_section_at_zero;
9291a0cd 290
ae2de4f8
DE
291 /* True if we are using the mapped index,
292 or we are faking it for OBJF_READNOW's sake. */
9291a0cd
TT
293 unsigned char using_index;
294
ae2de4f8 295 /* The mapped index, or NULL if .gdb_index is missing or not being used. */
9291a0cd 296 struct mapped_index *index_table;
98bfdba5 297
7b9f3c50 298 /* When using index_table, this keeps track of all quick_file_names entries.
56e64610
DE
299 TUs typically share line table entries with a CU, so we maintain a
300 separate table of all line table entries to support the sharing.
301 Note that while there can be way more TUs than CUs, we've already
302 sorted all the TUs into "type unit groups", grouped by their
303 DW_AT_stmt_list value. Therefore the only sharing done here is with a
304 CU and its associated TU group if there is one. */
7b9f3c50
DE
305 htab_t quick_file_names_table;
306
98bfdba5
PA
307 /* Set during partial symbol reading, to prevent queueing of full
308 symbols. */
309 int reading_partial_symbols;
673bfd45 310
dee91e82 311 /* Table mapping type DIEs to their struct type *.
673bfd45 312 This is NULL if not allocated yet.
02142a6c 313 The mapping is done via (CU/TU + DIE offset) -> type. */
dee91e82 314 htab_t die_type_hash;
95554aad
TT
315
316 /* The CUs we recently read. */
317 VEC (dwarf2_per_cu_ptr) *just_read_cus;
527f3840
JK
318
319 /* Table containing line_header indexed by offset and offset_in_dwz. */
320 htab_t line_header_hash;
6502dd73
DJ
321};
322
323static struct dwarf2_per_objfile *dwarf2_per_objfile;
c906108c 324
251d32d9 325/* Default names of the debugging sections. */
c906108c 326
233a11ab
CS
327/* Note that if the debugging section has been compressed, it might
328 have a name like .zdebug_info. */
329
9cdd5dbd
DE
330static const struct dwarf2_debug_sections dwarf2_elf_names =
331{
251d32d9
TG
332 { ".debug_info", ".zdebug_info" },
333 { ".debug_abbrev", ".zdebug_abbrev" },
334 { ".debug_line", ".zdebug_line" },
335 { ".debug_loc", ".zdebug_loc" },
336 { ".debug_macinfo", ".zdebug_macinfo" },
cf2c3c16 337 { ".debug_macro", ".zdebug_macro" },
251d32d9
TG
338 { ".debug_str", ".zdebug_str" },
339 { ".debug_ranges", ".zdebug_ranges" },
340 { ".debug_types", ".zdebug_types" },
3019eac3 341 { ".debug_addr", ".zdebug_addr" },
251d32d9
TG
342 { ".debug_frame", ".zdebug_frame" },
343 { ".eh_frame", NULL },
24d3216f
TT
344 { ".gdb_index", ".zgdb_index" },
345 23
251d32d9 346};
c906108c 347
80626a55 348/* List of DWO/DWP sections. */
3019eac3 349
80626a55 350static const struct dwop_section_names
3019eac3
DE
351{
352 struct dwarf2_section_names abbrev_dwo;
353 struct dwarf2_section_names info_dwo;
354 struct dwarf2_section_names line_dwo;
355 struct dwarf2_section_names loc_dwo;
09262596
DE
356 struct dwarf2_section_names macinfo_dwo;
357 struct dwarf2_section_names macro_dwo;
3019eac3
DE
358 struct dwarf2_section_names str_dwo;
359 struct dwarf2_section_names str_offsets_dwo;
360 struct dwarf2_section_names types_dwo;
80626a55
DE
361 struct dwarf2_section_names cu_index;
362 struct dwarf2_section_names tu_index;
3019eac3 363}
80626a55 364dwop_section_names =
3019eac3
DE
365{
366 { ".debug_abbrev.dwo", ".zdebug_abbrev.dwo" },
367 { ".debug_info.dwo", ".zdebug_info.dwo" },
368 { ".debug_line.dwo", ".zdebug_line.dwo" },
369 { ".debug_loc.dwo", ".zdebug_loc.dwo" },
09262596
DE
370 { ".debug_macinfo.dwo", ".zdebug_macinfo.dwo" },
371 { ".debug_macro.dwo", ".zdebug_macro.dwo" },
3019eac3
DE
372 { ".debug_str.dwo", ".zdebug_str.dwo" },
373 { ".debug_str_offsets.dwo", ".zdebug_str_offsets.dwo" },
374 { ".debug_types.dwo", ".zdebug_types.dwo" },
80626a55
DE
375 { ".debug_cu_index", ".zdebug_cu_index" },
376 { ".debug_tu_index", ".zdebug_tu_index" },
3019eac3
DE
377};
378
c906108c
SS
379/* local data types */
380
107d2387
AC
381/* The data in a compilation unit header, after target2host
382 translation, looks like this. */
c906108c 383struct comp_unit_head
a738430d 384{
c764a876 385 unsigned int length;
a738430d 386 short version;
a738430d
MK
387 unsigned char addr_size;
388 unsigned char signed_addr_p;
b64f50a1 389 sect_offset abbrev_offset;
57349743 390
a738430d
MK
391 /* Size of file offsets; either 4 or 8. */
392 unsigned int offset_size;
57349743 393
a738430d
MK
394 /* Size of the length field; either 4 or 12. */
395 unsigned int initial_length_size;
57349743 396
a738430d
MK
397 /* Offset to the first byte of this compilation unit header in the
398 .debug_info section, for resolving relative reference dies. */
b64f50a1 399 sect_offset offset;
57349743 400
d00adf39
DE
401 /* Offset to first die in this cu from the start of the cu.
402 This will be the first byte following the compilation unit header. */
b64f50a1 403 cu_offset first_die_offset;
a738430d 404};
c906108c 405
3da10d80
KS
406/* Type used for delaying computation of method physnames.
407 See comments for compute_delayed_physnames. */
408struct delayed_method_info
409{
410 /* The type to which the method is attached, i.e., its parent class. */
411 struct type *type;
412
413 /* The index of the method in the type's function fieldlists. */
414 int fnfield_index;
415
416 /* The index of the method in the fieldlist. */
417 int index;
418
419 /* The name of the DIE. */
420 const char *name;
421
422 /* The DIE associated with this method. */
423 struct die_info *die;
424};
425
426typedef struct delayed_method_info delayed_method_info;
427DEF_VEC_O (delayed_method_info);
428
e7c27a73
DJ
429/* Internal state when decoding a particular compilation unit. */
430struct dwarf2_cu
431{
432 /* The objfile containing this compilation unit. */
433 struct objfile *objfile;
434
d00adf39 435 /* The header of the compilation unit. */
e7c27a73 436 struct comp_unit_head header;
e142c38c 437
d00adf39
DE
438 /* Base address of this compilation unit. */
439 CORE_ADDR base_address;
440
441 /* Non-zero if base_address has been set. */
442 int base_known;
443
e142c38c
DJ
444 /* The language we are debugging. */
445 enum language language;
446 const struct language_defn *language_defn;
447
b0f35d58
DL
448 const char *producer;
449
e142c38c
DJ
450 /* The generic symbol table building routines have separate lists for
451 file scope symbols and all all other scopes (local scopes). So
452 we need to select the right one to pass to add_symbol_to_list().
453 We do it by keeping a pointer to the correct list in list_in_scope.
454
455 FIXME: The original dwarf code just treated the file scope as the
456 first local scope, and all other local scopes as nested local
457 scopes, and worked fine. Check to see if we really need to
458 distinguish these in buildsym.c. */
459 struct pending **list_in_scope;
460
433df2d4
DE
461 /* The abbrev table for this CU.
462 Normally this points to the abbrev table in the objfile.
463 But if DWO_UNIT is non-NULL this is the abbrev table in the DWO file. */
464 struct abbrev_table *abbrev_table;
72bf9492 465
b64f50a1
JK
466 /* Hash table holding all the loaded partial DIEs
467 with partial_die->offset.SECT_OFF as hash. */
72bf9492
DJ
468 htab_t partial_dies;
469
470 /* Storage for things with the same lifetime as this read-in compilation
471 unit, including partial DIEs. */
472 struct obstack comp_unit_obstack;
473
ae038cb0
DJ
474 /* When multiple dwarf2_cu structures are living in memory, this field
475 chains them all together, so that they can be released efficiently.
476 We will probably also want a generation counter so that most-recently-used
477 compilation units are cached... */
478 struct dwarf2_per_cu_data *read_in_chain;
479
69d751e3 480 /* Backlink to our per_cu entry. */
ae038cb0
DJ
481 struct dwarf2_per_cu_data *per_cu;
482
483 /* How many compilation units ago was this CU last referenced? */
484 int last_used;
485
b64f50a1
JK
486 /* A hash table of DIE cu_offset for following references with
487 die_info->offset.sect_off as hash. */
51545339 488 htab_t die_hash;
10b3939b
DJ
489
490 /* Full DIEs if read in. */
491 struct die_info *dies;
492
493 /* A set of pointers to dwarf2_per_cu_data objects for compilation
494 units referenced by this one. Only set during full symbol processing;
495 partial symbol tables do not have dependencies. */
496 htab_t dependencies;
497
cb1df416
DJ
498 /* Header data from the line table, during full symbol processing. */
499 struct line_header *line_header;
500
3da10d80
KS
501 /* A list of methods which need to have physnames computed
502 after all type information has been read. */
503 VEC (delayed_method_info) *method_list;
504
96408a79
SA
505 /* To be copied to symtab->call_site_htab. */
506 htab_t call_site_htab;
507
034e5797
DE
508 /* Non-NULL if this CU came from a DWO file.
509 There is an invariant here that is important to remember:
510 Except for attributes copied from the top level DIE in the "main"
511 (or "stub") file in preparation for reading the DWO file
512 (e.g., DW_AT_GNU_addr_base), we KISS: there is only *one* CU.
513 Either there isn't a DWO file (in which case this is NULL and the point
514 is moot), or there is and either we're not going to read it (in which
515 case this is NULL) or there is and we are reading it (in which case this
516 is non-NULL). */
3019eac3
DE
517 struct dwo_unit *dwo_unit;
518
519 /* The DW_AT_addr_base attribute if present, zero otherwise
520 (zero is a valid value though).
1dbab08b 521 Note this value comes from the Fission stub CU/TU's DIE. */
3019eac3
DE
522 ULONGEST addr_base;
523
2e3cf129
DE
524 /* The DW_AT_ranges_base attribute if present, zero otherwise
525 (zero is a valid value though).
1dbab08b 526 Note this value comes from the Fission stub CU/TU's DIE.
2e3cf129 527 Also note that the value is zero in the non-DWO case so this value can
ab435259
DE
528 be used without needing to know whether DWO files are in use or not.
529 N.B. This does not apply to DW_AT_ranges appearing in
530 DW_TAG_compile_unit dies. This is a bit of a wart, consider if ever
531 DW_AT_ranges appeared in the DW_TAG_compile_unit of DWO DIEs: then
532 DW_AT_ranges_base *would* have to be applied, and we'd have to care
533 whether the DW_AT_ranges attribute came from the skeleton or DWO. */
2e3cf129
DE
534 ULONGEST ranges_base;
535
ae038cb0
DJ
536 /* Mark used when releasing cached dies. */
537 unsigned int mark : 1;
538
8be455d7
JK
539 /* This CU references .debug_loc. See the symtab->locations_valid field.
540 This test is imperfect as there may exist optimized debug code not using
541 any location list and still facing inlining issues if handled as
542 unoptimized code. For a future better test see GCC PR other/32998. */
8be455d7 543 unsigned int has_loclist : 1;
ba919b58 544
1b80a9fa
JK
545 /* These cache the results for producer_is_* fields. CHECKED_PRODUCER is set
546 if all the producer_is_* fields are valid. This information is cached
547 because profiling CU expansion showed excessive time spent in
548 producer_is_gxx_lt_4_6. */
ba919b58
TT
549 unsigned int checked_producer : 1;
550 unsigned int producer_is_gxx_lt_4_6 : 1;
1b80a9fa 551 unsigned int producer_is_gcc_lt_4_3 : 1;
685b1105 552 unsigned int producer_is_icc : 1;
4d4ec4e5
TT
553
554 /* When set, the file that we're processing is known to have
555 debugging info for C++ namespaces. GCC 3.3.x did not produce
556 this information, but later versions do. */
557
558 unsigned int processing_has_namespace_info : 1;
e7c27a73
DJ
559};
560
10b3939b
DJ
561/* Persistent data held for a compilation unit, even when not
562 processing it. We put a pointer to this structure in the
28dee7f5 563 read_symtab_private field of the psymtab. */
10b3939b 564
ae038cb0
DJ
565struct dwarf2_per_cu_data
566{
36586728 567 /* The start offset and length of this compilation unit.
45452591 568 NOTE: Unlike comp_unit_head.length, this length includes
3019eac3
DE
569 initial_length_size.
570 If the DIE refers to a DWO file, this is always of the original die,
571 not the DWO file. */
b64f50a1 572 sect_offset offset;
36586728 573 unsigned int length;
ae038cb0
DJ
574
575 /* Flag indicating this compilation unit will be read in before
576 any of the current compilation units are processed. */
c764a876 577 unsigned int queued : 1;
ae038cb0 578
0d99eb77
DE
579 /* This flag will be set when reading partial DIEs if we need to load
580 absolutely all DIEs for this compilation unit, instead of just the ones
581 we think are interesting. It gets set if we look for a DIE in the
5afb4e99
DJ
582 hash table and don't find it. */
583 unsigned int load_all_dies : 1;
584
0186c6a7
DE
585 /* Non-zero if this CU is from .debug_types.
586 Struct dwarf2_per_cu_data is contained in struct signatured_type iff
587 this is non-zero. */
3019eac3
DE
588 unsigned int is_debug_types : 1;
589
36586728
TT
590 /* Non-zero if this CU is from the .dwz file. */
591 unsigned int is_dwz : 1;
592
a2ce51a0
DE
593 /* Non-zero if reading a TU directly from a DWO file, bypassing the stub.
594 This flag is only valid if is_debug_types is true.
595 We can't read a CU directly from a DWO file: There are required
596 attributes in the stub. */
597 unsigned int reading_dwo_directly : 1;
598
7ee85ab1
DE
599 /* Non-zero if the TU has been read.
600 This is used to assist the "Stay in DWO Optimization" for Fission:
601 When reading a DWO, it's faster to read TUs from the DWO instead of
602 fetching them from random other DWOs (due to comdat folding).
603 If the TU has already been read, the optimization is unnecessary
604 (and unwise - we don't want to change where gdb thinks the TU lives
605 "midflight").
606 This flag is only valid if is_debug_types is true. */
607 unsigned int tu_read : 1;
608
3019eac3
DE
609 /* The section this CU/TU lives in.
610 If the DIE refers to a DWO file, this is always the original die,
611 not the DWO file. */
8a0459fd 612 struct dwarf2_section_info *section;
348e048f 613
17ea53c3 614 /* Set to non-NULL iff this CU is currently loaded. When it gets freed out
cc12ce38
DE
615 of the CU cache it gets reset to NULL again. This is left as NULL for
616 dummy CUs (a CU header, but nothing else). */
ae038cb0 617 struct dwarf2_cu *cu;
1c379e20 618
9cdd5dbd
DE
619 /* The corresponding objfile.
620 Normally we can get the objfile from dwarf2_per_objfile.
621 However we can enter this file with just a "per_cu" handle. */
9291a0cd
TT
622 struct objfile *objfile;
623
fffbe6a8
YQ
624 /* When dwarf2_per_objfile->using_index is true, the 'quick' field
625 is active. Otherwise, the 'psymtab' field is active. */
9291a0cd
TT
626 union
627 {
628 /* The partial symbol table associated with this compilation unit,
95554aad 629 or NULL for unread partial units. */
9291a0cd
TT
630 struct partial_symtab *psymtab;
631
632 /* Data needed by the "quick" functions. */
633 struct dwarf2_per_cu_quick_data *quick;
634 } v;
95554aad 635
796a7ff8
DE
636 /* The CUs we import using DW_TAG_imported_unit. This is filled in
637 while reading psymtabs, used to compute the psymtab dependencies,
638 and then cleared. Then it is filled in again while reading full
639 symbols, and only deleted when the objfile is destroyed.
640
641 This is also used to work around a difference between the way gold
642 generates .gdb_index version <=7 and the way gdb does. Arguably this
643 is a gold bug. For symbols coming from TUs, gold records in the index
644 the CU that includes the TU instead of the TU itself. This breaks
645 dw2_lookup_symbol: It assumes that if the index says symbol X lives
646 in CU/TU Y, then one need only expand Y and a subsequent lookup in Y
647 will find X. Alas TUs live in their own symtab, so after expanding CU Y
648 we need to look in TU Z to find X. Fortunately, this is akin to
649 DW_TAG_imported_unit, so we just use the same mechanism: For
650 .gdb_index version <=7 this also records the TUs that the CU referred
651 to. Concurrently with this change gdb was modified to emit version 8
69d751e3
DE
652 indices so we only pay a price for gold generated indices.
653 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
796a7ff8 654 VEC (dwarf2_per_cu_ptr) *imported_symtabs;
ae038cb0
DJ
655};
656
348e048f
DE
657/* Entry in the signatured_types hash table. */
658
659struct signatured_type
660{
42e7ad6c 661 /* The "per_cu" object of this type.
ac9ec31b 662 This struct is used iff per_cu.is_debug_types.
42e7ad6c
DE
663 N.B.: This is the first member so that it's easy to convert pointers
664 between them. */
665 struct dwarf2_per_cu_data per_cu;
666
3019eac3 667 /* The type's signature. */
348e048f
DE
668 ULONGEST signature;
669
3019eac3 670 /* Offset in the TU of the type's DIE, as read from the TU header.
c88ee1f0
DE
671 If this TU is a DWO stub and the definition lives in a DWO file
672 (specified by DW_AT_GNU_dwo_name), this value is unusable. */
3019eac3
DE
673 cu_offset type_offset_in_tu;
674
675 /* Offset in the section of the type's DIE.
676 If the definition lives in a DWO file, this is the offset in the
677 .debug_types.dwo section.
678 The value is zero until the actual value is known.
679 Zero is otherwise not a valid section offset. */
680 sect_offset type_offset_in_section;
0186c6a7
DE
681
682 /* Type units are grouped by their DW_AT_stmt_list entry so that they
683 can share them. This points to the containing symtab. */
684 struct type_unit_group *type_unit_group;
ac9ec31b
DE
685
686 /* The type.
687 The first time we encounter this type we fully read it in and install it
688 in the symbol tables. Subsequent times we only need the type. */
689 struct type *type;
a2ce51a0
DE
690
691 /* Containing DWO unit.
692 This field is valid iff per_cu.reading_dwo_directly. */
693 struct dwo_unit *dwo_unit;
348e048f
DE
694};
695
0186c6a7
DE
696typedef struct signatured_type *sig_type_ptr;
697DEF_VEC_P (sig_type_ptr);
698
094b34ac
DE
699/* A struct that can be used as a hash key for tables based on DW_AT_stmt_list.
700 This includes type_unit_group and quick_file_names. */
701
702struct stmt_list_hash
703{
704 /* The DWO unit this table is from or NULL if there is none. */
705 struct dwo_unit *dwo_unit;
706
707 /* Offset in .debug_line or .debug_line.dwo. */
708 sect_offset line_offset;
709};
710
f4dc4d17
DE
711/* Each element of dwarf2_per_objfile->type_unit_groups is a pointer to
712 an object of this type. */
713
714struct type_unit_group
715{
0186c6a7 716 /* dwarf2read.c's main "handle" on a TU symtab.
f4dc4d17
DE
717 To simplify things we create an artificial CU that "includes" all the
718 type units using this stmt_list so that the rest of the code still has
719 a "per_cu" handle on the symtab.
720 This PER_CU is recognized by having no section. */
8a0459fd 721#define IS_TYPE_UNIT_GROUP(per_cu) ((per_cu)->section == NULL)
094b34ac
DE
722 struct dwarf2_per_cu_data per_cu;
723
0186c6a7
DE
724 /* The TUs that share this DW_AT_stmt_list entry.
725 This is added to while parsing type units to build partial symtabs,
726 and is deleted afterwards and not used again. */
727 VEC (sig_type_ptr) *tus;
f4dc4d17 728
43f3e411 729 /* The compunit symtab.
094b34ac 730 Type units in a group needn't all be defined in the same source file,
43f3e411
DE
731 so we create an essentially anonymous symtab as the compunit symtab. */
732 struct compunit_symtab *compunit_symtab;
f4dc4d17 733
094b34ac
DE
734 /* The data used to construct the hash key. */
735 struct stmt_list_hash hash;
f4dc4d17
DE
736
737 /* The number of symtabs from the line header.
738 The value here must match line_header.num_file_names. */
739 unsigned int num_symtabs;
740
741 /* The symbol tables for this TU (obtained from the files listed in
742 DW_AT_stmt_list).
743 WARNING: The order of entries here must match the order of entries
744 in the line header. After the first TU using this type_unit_group, the
745 line header for the subsequent TUs is recreated from this. This is done
746 because we need to use the same symtabs for each TU using the same
747 DW_AT_stmt_list value. Also note that symtabs may be repeated here,
748 there's no guarantee the line header doesn't have duplicate entries. */
749 struct symtab **symtabs;
750};
751
73869dc2 752/* These sections are what may appear in a (real or virtual) DWO file. */
3019eac3
DE
753
754struct dwo_sections
755{
756 struct dwarf2_section_info abbrev;
3019eac3
DE
757 struct dwarf2_section_info line;
758 struct dwarf2_section_info loc;
09262596
DE
759 struct dwarf2_section_info macinfo;
760 struct dwarf2_section_info macro;
3019eac3
DE
761 struct dwarf2_section_info str;
762 struct dwarf2_section_info str_offsets;
80626a55
DE
763 /* In the case of a virtual DWO file, these two are unused. */
764 struct dwarf2_section_info info;
3019eac3
DE
765 VEC (dwarf2_section_info_def) *types;
766};
767
c88ee1f0 768/* CUs/TUs in DWP/DWO files. */
3019eac3
DE
769
770struct dwo_unit
771{
772 /* Backlink to the containing struct dwo_file. */
773 struct dwo_file *dwo_file;
774
775 /* The "id" that distinguishes this CU/TU.
776 .debug_info calls this "dwo_id", .debug_types calls this "signature".
777 Since signatures came first, we stick with it for consistency. */
778 ULONGEST signature;
779
780 /* The section this CU/TU lives in, in the DWO file. */
8a0459fd 781 struct dwarf2_section_info *section;
3019eac3 782
19ac8c2e 783 /* Same as dwarf2_per_cu_data:{offset,length} but in the DWO section. */
3019eac3
DE
784 sect_offset offset;
785 unsigned int length;
786
787 /* For types, offset in the type's DIE of the type defined by this TU. */
788 cu_offset type_offset_in_tu;
789};
790
73869dc2
DE
791/* include/dwarf2.h defines the DWP section codes.
792 It defines a max value but it doesn't define a min value, which we
793 use for error checking, so provide one. */
794
795enum dwp_v2_section_ids
796{
797 DW_SECT_MIN = 1
798};
799
80626a55 800/* Data for one DWO file.
57d63ce2
DE
801
802 This includes virtual DWO files (a virtual DWO file is a DWO file as it
803 appears in a DWP file). DWP files don't really have DWO files per se -
804 comdat folding of types "loses" the DWO file they came from, and from
805 a high level view DWP files appear to contain a mass of random types.
806 However, to maintain consistency with the non-DWP case we pretend DWP
807 files contain virtual DWO files, and we assign each TU with one virtual
808 DWO file (generally based on the line and abbrev section offsets -
809 a heuristic that seems to work in practice). */
3019eac3
DE
810
811struct dwo_file
812{
0ac5b59e 813 /* The DW_AT_GNU_dwo_name attribute.
80626a55
DE
814 For virtual DWO files the name is constructed from the section offsets
815 of abbrev,line,loc,str_offsets so that we combine virtual DWO files
816 from related CU+TUs. */
0ac5b59e
DE
817 const char *dwo_name;
818
819 /* The DW_AT_comp_dir attribute. */
820 const char *comp_dir;
3019eac3 821
80626a55
DE
822 /* The bfd, when the file is open. Otherwise this is NULL.
823 This is unused(NULL) for virtual DWO files where we use dwp_file.dbfd. */
824 bfd *dbfd;
3019eac3 825
73869dc2
DE
826 /* The sections that make up this DWO file.
827 Remember that for virtual DWO files in DWP V2, these are virtual
828 sections (for lack of a better name). */
3019eac3
DE
829 struct dwo_sections sections;
830
19c3d4c9
DE
831 /* The CU in the file.
832 We only support one because having more than one requires hacking the
833 dwo_name of each to match, which is highly unlikely to happen.
834 Doing this means all TUs can share comp_dir: We also assume that
835 DW_AT_comp_dir across all TUs in a DWO file will be identical. */
836 struct dwo_unit *cu;
3019eac3
DE
837
838 /* Table of TUs in the file.
839 Each element is a struct dwo_unit. */
840 htab_t tus;
841};
842
80626a55
DE
843/* These sections are what may appear in a DWP file. */
844
845struct dwp_sections
846{
73869dc2 847 /* These are used by both DWP version 1 and 2. */
80626a55
DE
848 struct dwarf2_section_info str;
849 struct dwarf2_section_info cu_index;
850 struct dwarf2_section_info tu_index;
73869dc2
DE
851
852 /* These are only used by DWP version 2 files.
853 In DWP version 1 the .debug_info.dwo, .debug_types.dwo, and other
854 sections are referenced by section number, and are not recorded here.
855 In DWP version 2 there is at most one copy of all these sections, each
856 section being (effectively) comprised of the concatenation of all of the
857 individual sections that exist in the version 1 format.
858 To keep the code simple we treat each of these concatenated pieces as a
859 section itself (a virtual section?). */
860 struct dwarf2_section_info abbrev;
861 struct dwarf2_section_info info;
862 struct dwarf2_section_info line;
863 struct dwarf2_section_info loc;
864 struct dwarf2_section_info macinfo;
865 struct dwarf2_section_info macro;
866 struct dwarf2_section_info str_offsets;
867 struct dwarf2_section_info types;
80626a55
DE
868};
869
73869dc2
DE
870/* These sections are what may appear in a virtual DWO file in DWP version 1.
871 A virtual DWO file is a DWO file as it appears in a DWP file. */
80626a55 872
73869dc2 873struct virtual_v1_dwo_sections
80626a55
DE
874{
875 struct dwarf2_section_info abbrev;
876 struct dwarf2_section_info line;
877 struct dwarf2_section_info loc;
878 struct dwarf2_section_info macinfo;
879 struct dwarf2_section_info macro;
880 struct dwarf2_section_info str_offsets;
881 /* Each DWP hash table entry records one CU or one TU.
8a0459fd 882 That is recorded here, and copied to dwo_unit.section. */
80626a55
DE
883 struct dwarf2_section_info info_or_types;
884};
885
73869dc2
DE
886/* Similar to virtual_v1_dwo_sections, but for DWP version 2.
887 In version 2, the sections of the DWO files are concatenated together
888 and stored in one section of that name. Thus each ELF section contains
889 several "virtual" sections. */
890
891struct virtual_v2_dwo_sections
892{
893 bfd_size_type abbrev_offset;
894 bfd_size_type abbrev_size;
895
896 bfd_size_type line_offset;
897 bfd_size_type line_size;
898
899 bfd_size_type loc_offset;
900 bfd_size_type loc_size;
901
902 bfd_size_type macinfo_offset;
903 bfd_size_type macinfo_size;
904
905 bfd_size_type macro_offset;
906 bfd_size_type macro_size;
907
908 bfd_size_type str_offsets_offset;
909 bfd_size_type str_offsets_size;
910
911 /* Each DWP hash table entry records one CU or one TU.
912 That is recorded here, and copied to dwo_unit.section. */
913 bfd_size_type info_or_types_offset;
914 bfd_size_type info_or_types_size;
915};
916
80626a55
DE
917/* Contents of DWP hash tables. */
918
919struct dwp_hash_table
920{
73869dc2 921 uint32_t version, nr_columns;
80626a55 922 uint32_t nr_units, nr_slots;
73869dc2
DE
923 const gdb_byte *hash_table, *unit_table;
924 union
925 {
926 struct
927 {
928 const gdb_byte *indices;
929 } v1;
930 struct
931 {
932 /* This is indexed by column number and gives the id of the section
933 in that column. */
934#define MAX_NR_V2_DWO_SECTIONS \
935 (1 /* .debug_info or .debug_types */ \
936 + 1 /* .debug_abbrev */ \
937 + 1 /* .debug_line */ \
938 + 1 /* .debug_loc */ \
939 + 1 /* .debug_str_offsets */ \
940 + 1 /* .debug_macro or .debug_macinfo */)
941 int section_ids[MAX_NR_V2_DWO_SECTIONS];
942 const gdb_byte *offsets;
943 const gdb_byte *sizes;
944 } v2;
945 } section_pool;
80626a55
DE
946};
947
948/* Data for one DWP file. */
949
950struct dwp_file
951{
952 /* Name of the file. */
953 const char *name;
954
73869dc2
DE
955 /* File format version. */
956 int version;
957
93417882 958 /* The bfd. */
80626a55
DE
959 bfd *dbfd;
960
961 /* Section info for this file. */
962 struct dwp_sections sections;
963
57d63ce2 964 /* Table of CUs in the file. */
80626a55
DE
965 const struct dwp_hash_table *cus;
966
967 /* Table of TUs in the file. */
968 const struct dwp_hash_table *tus;
969
19ac8c2e
DE
970 /* Tables of loaded CUs/TUs. Each entry is a struct dwo_unit *. */
971 htab_t loaded_cus;
972 htab_t loaded_tus;
80626a55 973
73869dc2
DE
974 /* Table to map ELF section numbers to their sections.
975 This is only needed for the DWP V1 file format. */
80626a55
DE
976 unsigned int num_sections;
977 asection **elf_sections;
978};
979
36586728
TT
980/* This represents a '.dwz' file. */
981
982struct dwz_file
983{
984 /* A dwz file can only contain a few sections. */
985 struct dwarf2_section_info abbrev;
986 struct dwarf2_section_info info;
987 struct dwarf2_section_info str;
988 struct dwarf2_section_info line;
989 struct dwarf2_section_info macro;
2ec9a5e0 990 struct dwarf2_section_info gdb_index;
36586728
TT
991
992 /* The dwz's BFD. */
993 bfd *dwz_bfd;
994};
995
0963b4bd
MS
996/* Struct used to pass misc. parameters to read_die_and_children, et
997 al. which are used for both .debug_info and .debug_types dies.
998 All parameters here are unchanging for the life of the call. This
dee91e82 999 struct exists to abstract away the constant parameters of die reading. */
93311388
DE
1000
1001struct die_reader_specs
1002{
a32a8923 1003 /* The bfd of die_section. */
93311388
DE
1004 bfd* abfd;
1005
1006 /* The CU of the DIE we are parsing. */
1007 struct dwarf2_cu *cu;
1008
80626a55 1009 /* Non-NULL if reading a DWO file (including one packaged into a DWP). */
3019eac3
DE
1010 struct dwo_file *dwo_file;
1011
dee91e82 1012 /* The section the die comes from.
3019eac3 1013 This is either .debug_info or .debug_types, or the .dwo variants. */
dee91e82
DE
1014 struct dwarf2_section_info *die_section;
1015
1016 /* die_section->buffer. */
d521ce57 1017 const gdb_byte *buffer;
f664829e
DE
1018
1019 /* The end of the buffer. */
1020 const gdb_byte *buffer_end;
a2ce51a0
DE
1021
1022 /* The value of the DW_AT_comp_dir attribute. */
1023 const char *comp_dir;
93311388
DE
1024};
1025
fd820528 1026/* Type of function passed to init_cutu_and_read_dies, et.al. */
dee91e82 1027typedef void (die_reader_func_ftype) (const struct die_reader_specs *reader,
d521ce57 1028 const gdb_byte *info_ptr,
dee91e82
DE
1029 struct die_info *comp_unit_die,
1030 int has_children,
1031 void *data);
1032
52059ffd
TT
1033struct file_entry
1034{
1035 const char *name;
1036 unsigned int dir_index;
1037 unsigned int mod_time;
1038 unsigned int length;
83769d0b
DE
1039 /* Non-zero if referenced by the Line Number Program. */
1040 int included_p;
1041 /* The associated symbol table, if any. */
1042 struct symtab *symtab;
52059ffd
TT
1043};
1044
debd256d
JB
1045/* The line number information for a compilation unit (found in the
1046 .debug_line section) begins with a "statement program header",
1047 which contains the following information. */
1048struct line_header
1049{
527f3840
JK
1050 /* Offset of line number information in .debug_line section. */
1051 sect_offset offset;
1052
1053 /* OFFSET is for struct dwz_file associated with dwarf2_per_objfile. */
1054 unsigned offset_in_dwz : 1;
1055
debd256d
JB
1056 unsigned int total_length;
1057 unsigned short version;
1058 unsigned int header_length;
1059 unsigned char minimum_instruction_length;
2dc7f7b3 1060 unsigned char maximum_ops_per_instruction;
debd256d
JB
1061 unsigned char default_is_stmt;
1062 int line_base;
1063 unsigned char line_range;
1064 unsigned char opcode_base;
1065
1066 /* standard_opcode_lengths[i] is the number of operands for the
1067 standard opcode whose value is i. This means that
1068 standard_opcode_lengths[0] is unused, and the last meaningful
1069 element is standard_opcode_lengths[opcode_base - 1]. */
1070 unsigned char *standard_opcode_lengths;
1071
1072 /* The include_directories table. NOTE! These strings are not
1073 allocated with xmalloc; instead, they are pointers into
1074 debug_line_buffer. If you try to free them, `free' will get
1075 indigestion. */
1076 unsigned int num_include_dirs, include_dirs_size;
d521ce57 1077 const char **include_dirs;
debd256d
JB
1078
1079 /* The file_names table. NOTE! These strings are not allocated
1080 with xmalloc; instead, they are pointers into debug_line_buffer.
1081 Don't try to free them directly. */
1082 unsigned int num_file_names, file_names_size;
52059ffd 1083 struct file_entry *file_names;
debd256d
JB
1084
1085 /* The start and end of the statement program following this
6502dd73 1086 header. These point into dwarf2_per_objfile->line_buffer. */
d521ce57 1087 const gdb_byte *statement_program_start, *statement_program_end;
debd256d 1088};
c906108c
SS
1089
1090/* When we construct a partial symbol table entry we only
0963b4bd 1091 need this much information. */
c906108c
SS
1092struct partial_die_info
1093 {
72bf9492 1094 /* Offset of this DIE. */
b64f50a1 1095 sect_offset offset;
72bf9492
DJ
1096
1097 /* DWARF-2 tag for this DIE. */
1098 ENUM_BITFIELD(dwarf_tag) tag : 16;
1099
72bf9492
DJ
1100 /* Assorted flags describing the data found in this DIE. */
1101 unsigned int has_children : 1;
1102 unsigned int is_external : 1;
1103 unsigned int is_declaration : 1;
1104 unsigned int has_type : 1;
1105 unsigned int has_specification : 1;
1106 unsigned int has_pc_info : 1;
481860b3 1107 unsigned int may_be_inlined : 1;
72bf9492
DJ
1108
1109 /* Flag set if the SCOPE field of this structure has been
1110 computed. */
1111 unsigned int scope_set : 1;
1112
fa4028e9
JB
1113 /* Flag set if the DIE has a byte_size attribute. */
1114 unsigned int has_byte_size : 1;
1115
ff908ebf
AW
1116 /* Flag set if the DIE has a DW_AT_const_value attribute. */
1117 unsigned int has_const_value : 1;
1118
98bfdba5
PA
1119 /* Flag set if any of the DIE's children are template arguments. */
1120 unsigned int has_template_arguments : 1;
1121
abc72ce4
DE
1122 /* Flag set if fixup_partial_die has been called on this die. */
1123 unsigned int fixup_called : 1;
1124
36586728
TT
1125 /* Flag set if DW_TAG_imported_unit uses DW_FORM_GNU_ref_alt. */
1126 unsigned int is_dwz : 1;
1127
1128 /* Flag set if spec_offset uses DW_FORM_GNU_ref_alt. */
1129 unsigned int spec_is_dwz : 1;
1130
72bf9492 1131 /* The name of this DIE. Normally the value of DW_AT_name, but
94af9270 1132 sometimes a default name for unnamed DIEs. */
15d034d0 1133 const char *name;
72bf9492 1134
abc72ce4
DE
1135 /* The linkage name, if present. */
1136 const char *linkage_name;
1137
72bf9492
DJ
1138 /* The scope to prepend to our children. This is generally
1139 allocated on the comp_unit_obstack, so will disappear
1140 when this compilation unit leaves the cache. */
15d034d0 1141 const char *scope;
72bf9492 1142
95554aad
TT
1143 /* Some data associated with the partial DIE. The tag determines
1144 which field is live. */
1145 union
1146 {
1147 /* The location description associated with this DIE, if any. */
1148 struct dwarf_block *locdesc;
1149 /* The offset of an import, for DW_TAG_imported_unit. */
1150 sect_offset offset;
1151 } d;
72bf9492
DJ
1152
1153 /* If HAS_PC_INFO, the PC range associated with this DIE. */
c906108c
SS
1154 CORE_ADDR lowpc;
1155 CORE_ADDR highpc;
72bf9492 1156
93311388 1157 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
72bf9492 1158 DW_AT_sibling, if any. */
abc72ce4
DE
1159 /* NOTE: This member isn't strictly necessary, read_partial_die could
1160 return DW_AT_sibling values to its caller load_partial_dies. */
d521ce57 1161 const gdb_byte *sibling;
72bf9492
DJ
1162
1163 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
1164 DW_AT_specification (or DW_AT_abstract_origin or
1165 DW_AT_extension). */
b64f50a1 1166 sect_offset spec_offset;
72bf9492
DJ
1167
1168 /* Pointers to this DIE's parent, first child, and next sibling,
1169 if any. */
1170 struct partial_die_info *die_parent, *die_child, *die_sibling;
c906108c
SS
1171 };
1172
0963b4bd 1173/* This data structure holds the information of an abbrev. */
c906108c
SS
1174struct abbrev_info
1175 {
1176 unsigned int number; /* number identifying abbrev */
1177 enum dwarf_tag tag; /* dwarf tag */
f3dd6933
DJ
1178 unsigned short has_children; /* boolean */
1179 unsigned short num_attrs; /* number of attributes */
c906108c
SS
1180 struct attr_abbrev *attrs; /* an array of attribute descriptions */
1181 struct abbrev_info *next; /* next in chain */
1182 };
1183
1184struct attr_abbrev
1185 {
9d25dd43
DE
1186 ENUM_BITFIELD(dwarf_attribute) name : 16;
1187 ENUM_BITFIELD(dwarf_form) form : 16;
c906108c
SS
1188 };
1189
433df2d4
DE
1190/* Size of abbrev_table.abbrev_hash_table. */
1191#define ABBREV_HASH_SIZE 121
1192
1193/* Top level data structure to contain an abbreviation table. */
1194
1195struct abbrev_table
1196{
f4dc4d17
DE
1197 /* Where the abbrev table came from.
1198 This is used as a sanity check when the table is used. */
433df2d4
DE
1199 sect_offset offset;
1200
1201 /* Storage for the abbrev table. */
1202 struct obstack abbrev_obstack;
1203
1204 /* Hash table of abbrevs.
1205 This is an array of size ABBREV_HASH_SIZE allocated in abbrev_obstack.
1206 It could be statically allocated, but the previous code didn't so we
1207 don't either. */
1208 struct abbrev_info **abbrevs;
1209};
1210
0963b4bd 1211/* Attributes have a name and a value. */
b60c80d6
DJ
1212struct attribute
1213 {
9d25dd43 1214 ENUM_BITFIELD(dwarf_attribute) name : 16;
8285870a
JK
1215 ENUM_BITFIELD(dwarf_form) form : 15;
1216
1217 /* Has DW_STRING already been updated by dwarf2_canonicalize_name? This
1218 field should be in u.str (existing only for DW_STRING) but it is kept
1219 here for better struct attribute alignment. */
1220 unsigned int string_is_canonical : 1;
1221
b60c80d6
DJ
1222 union
1223 {
15d034d0 1224 const char *str;
b60c80d6 1225 struct dwarf_block *blk;
43bbcdc2
PH
1226 ULONGEST unsnd;
1227 LONGEST snd;
b60c80d6 1228 CORE_ADDR addr;
ac9ec31b 1229 ULONGEST signature;
b60c80d6
DJ
1230 }
1231 u;
1232 };
1233
0963b4bd 1234/* This data structure holds a complete die structure. */
c906108c
SS
1235struct die_info
1236 {
76815b17
DE
1237 /* DWARF-2 tag for this DIE. */
1238 ENUM_BITFIELD(dwarf_tag) tag : 16;
1239
1240 /* Number of attributes */
98bfdba5
PA
1241 unsigned char num_attrs;
1242
1243 /* True if we're presently building the full type name for the
1244 type derived from this DIE. */
1245 unsigned char building_fullname : 1;
76815b17 1246
adde2bff
DE
1247 /* True if this die is in process. PR 16581. */
1248 unsigned char in_process : 1;
1249
76815b17
DE
1250 /* Abbrev number */
1251 unsigned int abbrev;
1252
93311388 1253 /* Offset in .debug_info or .debug_types section. */
b64f50a1 1254 sect_offset offset;
78ba4af6
JB
1255
1256 /* The dies in a compilation unit form an n-ary tree. PARENT
1257 points to this die's parent; CHILD points to the first child of
1258 this node; and all the children of a given node are chained
4950bc1c 1259 together via their SIBLING fields. */
639d11d3
DC
1260 struct die_info *child; /* Its first child, if any. */
1261 struct die_info *sibling; /* Its next sibling, if any. */
1262 struct die_info *parent; /* Its parent, if any. */
c906108c 1263
b60c80d6
DJ
1264 /* An array of attributes, with NUM_ATTRS elements. There may be
1265 zero, but it's not common and zero-sized arrays are not
1266 sufficiently portable C. */
1267 struct attribute attrs[1];
c906108c
SS
1268 };
1269
0963b4bd 1270/* Get at parts of an attribute structure. */
c906108c
SS
1271
1272#define DW_STRING(attr) ((attr)->u.str)
8285870a 1273#define DW_STRING_IS_CANONICAL(attr) ((attr)->string_is_canonical)
c906108c
SS
1274#define DW_UNSND(attr) ((attr)->u.unsnd)
1275#define DW_BLOCK(attr) ((attr)->u.blk)
1276#define DW_SND(attr) ((attr)->u.snd)
1277#define DW_ADDR(attr) ((attr)->u.addr)
ac9ec31b 1278#define DW_SIGNATURE(attr) ((attr)->u.signature)
c906108c 1279
0963b4bd 1280/* Blocks are a bunch of untyped bytes. */
c906108c
SS
1281struct dwarf_block
1282 {
56eb65bd 1283 size_t size;
1d6edc3c
JK
1284
1285 /* Valid only if SIZE is not zero. */
d521ce57 1286 const gdb_byte *data;
c906108c
SS
1287 };
1288
c906108c
SS
1289#ifndef ATTR_ALLOC_CHUNK
1290#define ATTR_ALLOC_CHUNK 4
1291#endif
1292
c906108c
SS
1293/* Allocate fields for structs, unions and enums in this size. */
1294#ifndef DW_FIELD_ALLOC_CHUNK
1295#define DW_FIELD_ALLOC_CHUNK 4
1296#endif
1297
c906108c
SS
1298/* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
1299 but this would require a corresponding change in unpack_field_as_long
1300 and friends. */
1301static int bits_per_byte = 8;
1302
52059ffd
TT
1303struct nextfield
1304{
1305 struct nextfield *next;
1306 int accessibility;
1307 int virtuality;
1308 struct field field;
1309};
1310
1311struct nextfnfield
1312{
1313 struct nextfnfield *next;
1314 struct fn_field fnfield;
1315};
1316
1317struct fnfieldlist
1318{
1319 const char *name;
1320 int length;
1321 struct nextfnfield *head;
1322};
1323
1324struct typedef_field_list
1325{
1326 struct typedef_field field;
1327 struct typedef_field_list *next;
1328};
1329
c906108c
SS
1330/* The routines that read and process dies for a C struct or C++ class
1331 pass lists of data member fields and lists of member function fields
1332 in an instance of a field_info structure, as defined below. */
1333struct field_info
c5aa993b 1334 {
0963b4bd 1335 /* List of data member and baseclasses fields. */
52059ffd 1336 struct nextfield *fields, *baseclasses;
c906108c 1337
7d0ccb61 1338 /* Number of fields (including baseclasses). */
c5aa993b 1339 int nfields;
c906108c 1340
c5aa993b
JM
1341 /* Number of baseclasses. */
1342 int nbaseclasses;
c906108c 1343
c5aa993b
JM
1344 /* Set if the accesibility of one of the fields is not public. */
1345 int non_public_fields;
c906108c 1346
c5aa993b
JM
1347 /* Member function fields array, entries are allocated in the order they
1348 are encountered in the object file. */
52059ffd 1349 struct nextfnfield *fnfields;
c906108c 1350
c5aa993b
JM
1351 /* Member function fieldlist array, contains name of possibly overloaded
1352 member function, number of overloaded member functions and a pointer
1353 to the head of the member function field chain. */
52059ffd 1354 struct fnfieldlist *fnfieldlists;
c906108c 1355
c5aa993b
JM
1356 /* Number of entries in the fnfieldlists array. */
1357 int nfnfields;
98751a41
JK
1358
1359 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
1360 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
52059ffd 1361 struct typedef_field_list *typedef_field_list;
98751a41 1362 unsigned typedef_field_list_count;
c5aa993b 1363 };
c906108c 1364
10b3939b
DJ
1365/* One item on the queue of compilation units to read in full symbols
1366 for. */
1367struct dwarf2_queue_item
1368{
1369 struct dwarf2_per_cu_data *per_cu;
95554aad 1370 enum language pretend_language;
10b3939b
DJ
1371 struct dwarf2_queue_item *next;
1372};
1373
1374/* The current queue. */
1375static struct dwarf2_queue_item *dwarf2_queue, *dwarf2_queue_tail;
1376
ae038cb0
DJ
1377/* Loaded secondary compilation units are kept in memory until they
1378 have not been referenced for the processing of this many
1379 compilation units. Set this to zero to disable caching. Cache
1380 sizes of up to at least twenty will improve startup time for
1381 typical inter-CU-reference binaries, at an obvious memory cost. */
b4f54984 1382static int dwarf_max_cache_age = 5;
920d2a44 1383static void
b4f54984
DE
1384show_dwarf_max_cache_age (struct ui_file *file, int from_tty,
1385 struct cmd_list_element *c, const char *value)
920d2a44 1386{
3e43a32a 1387 fprintf_filtered (file, _("The upper bound on the age of cached "
b4f54984 1388 "DWARF compilation units is %s.\n"),
920d2a44
AC
1389 value);
1390}
4390d890 1391\f
c906108c
SS
1392/* local function prototypes */
1393
a32a8923
DE
1394static const char *get_section_name (const struct dwarf2_section_info *);
1395
1396static const char *get_section_file_name (const struct dwarf2_section_info *);
1397
4efb68b1 1398static void dwarf2_locate_sections (bfd *, asection *, void *);
c906108c 1399
918dd910
JK
1400static void dwarf2_find_base_address (struct die_info *die,
1401 struct dwarf2_cu *cu);
1402
0018ea6f
DE
1403static struct partial_symtab *create_partial_symtab
1404 (struct dwarf2_per_cu_data *per_cu, const char *name);
1405
c67a9c90 1406static void dwarf2_build_psymtabs_hard (struct objfile *);
c906108c 1407
72bf9492
DJ
1408static void scan_partial_symbols (struct partial_die_info *,
1409 CORE_ADDR *, CORE_ADDR *,
5734ee8b 1410 int, struct dwarf2_cu *);
c906108c 1411
72bf9492
DJ
1412static void add_partial_symbol (struct partial_die_info *,
1413 struct dwarf2_cu *);
63d06c5c 1414
72bf9492
DJ
1415static void add_partial_namespace (struct partial_die_info *pdi,
1416 CORE_ADDR *lowpc, CORE_ADDR *highpc,
cdc07690 1417 int set_addrmap, struct dwarf2_cu *cu);
63d06c5c 1418
5d7cb8df 1419static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
cdc07690 1420 CORE_ADDR *highpc, int set_addrmap,
5d7cb8df
JK
1421 struct dwarf2_cu *cu);
1422
72bf9492
DJ
1423static void add_partial_enumeration (struct partial_die_info *enum_pdi,
1424 struct dwarf2_cu *cu);
91c24f0a 1425
bc30ff58
JB
1426static void add_partial_subprogram (struct partial_die_info *pdi,
1427 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 1428 int need_pc, struct dwarf2_cu *cu);
bc30ff58 1429
257e7a09
YQ
1430static void dwarf2_read_symtab (struct partial_symtab *,
1431 struct objfile *);
c906108c 1432
a14ed312 1433static void psymtab_to_symtab_1 (struct partial_symtab *);
c906108c 1434
433df2d4
DE
1435static struct abbrev_info *abbrev_table_lookup_abbrev
1436 (const struct abbrev_table *, unsigned int);
1437
1438static struct abbrev_table *abbrev_table_read_table
1439 (struct dwarf2_section_info *, sect_offset);
1440
1441static void abbrev_table_free (struct abbrev_table *);
1442
f4dc4d17
DE
1443static void abbrev_table_free_cleanup (void *);
1444
dee91e82
DE
1445static void dwarf2_read_abbrevs (struct dwarf2_cu *,
1446 struct dwarf2_section_info *);
c906108c 1447
f3dd6933 1448static void dwarf2_free_abbrev_table (void *);
c906108c 1449
d521ce57 1450static unsigned int peek_abbrev_code (bfd *, const gdb_byte *);
6caca83c 1451
dee91e82 1452static struct partial_die_info *load_partial_dies
d521ce57 1453 (const struct die_reader_specs *, const gdb_byte *, int);
72bf9492 1454
d521ce57
TT
1455static const gdb_byte *read_partial_die (const struct die_reader_specs *,
1456 struct partial_die_info *,
1457 struct abbrev_info *,
1458 unsigned int,
1459 const gdb_byte *);
c906108c 1460
36586728 1461static struct partial_die_info *find_partial_die (sect_offset, int,
10b3939b 1462 struct dwarf2_cu *);
72bf9492
DJ
1463
1464static void fixup_partial_die (struct partial_die_info *,
1465 struct dwarf2_cu *);
1466
d521ce57
TT
1467static const gdb_byte *read_attribute (const struct die_reader_specs *,
1468 struct attribute *, struct attr_abbrev *,
1469 const gdb_byte *);
a8329558 1470
a1855c1d 1471static unsigned int read_1_byte (bfd *, const gdb_byte *);
c906108c 1472
a1855c1d 1473static int read_1_signed_byte (bfd *, const gdb_byte *);
c906108c 1474
a1855c1d 1475static unsigned int read_2_bytes (bfd *, const gdb_byte *);
c906108c 1476
a1855c1d 1477static unsigned int read_4_bytes (bfd *, const gdb_byte *);
c906108c 1478
a1855c1d 1479static ULONGEST read_8_bytes (bfd *, const gdb_byte *);
c906108c 1480
d521ce57 1481static CORE_ADDR read_address (bfd *, const gdb_byte *ptr, struct dwarf2_cu *,
891d2f0b 1482 unsigned int *);
c906108c 1483
d521ce57 1484static LONGEST read_initial_length (bfd *, const gdb_byte *, unsigned int *);
c764a876
DE
1485
1486static LONGEST read_checked_initial_length_and_offset
d521ce57 1487 (bfd *, const gdb_byte *, const struct comp_unit_head *,
c764a876 1488 unsigned int *, unsigned int *);
613e1657 1489
d521ce57
TT
1490static LONGEST read_offset (bfd *, const gdb_byte *,
1491 const struct comp_unit_head *,
c764a876
DE
1492 unsigned int *);
1493
d521ce57 1494static LONGEST read_offset_1 (bfd *, const gdb_byte *, unsigned int);
613e1657 1495
f4dc4d17
DE
1496static sect_offset read_abbrev_offset (struct dwarf2_section_info *,
1497 sect_offset);
1498
d521ce57 1499static const gdb_byte *read_n_bytes (bfd *, const gdb_byte *, unsigned int);
c906108c 1500
d521ce57 1501static const char *read_direct_string (bfd *, const gdb_byte *, unsigned int *);
c906108c 1502
d521ce57
TT
1503static const char *read_indirect_string (bfd *, const gdb_byte *,
1504 const struct comp_unit_head *,
1505 unsigned int *);
4bdf3d34 1506
d521ce57 1507static const char *read_indirect_string_from_dwz (struct dwz_file *, LONGEST);
36586728 1508
d521ce57 1509static ULONGEST read_unsigned_leb128 (bfd *, const gdb_byte *, unsigned int *);
c906108c 1510
d521ce57 1511static LONGEST read_signed_leb128 (bfd *, const gdb_byte *, unsigned int *);
c906108c 1512
d521ce57
TT
1513static CORE_ADDR read_addr_index_from_leb128 (struct dwarf2_cu *,
1514 const gdb_byte *,
3019eac3
DE
1515 unsigned int *);
1516
d521ce57 1517static const char *read_str_index (const struct die_reader_specs *reader,
342587c4 1518 ULONGEST str_index);
3019eac3 1519
e142c38c 1520static void set_cu_language (unsigned int, struct dwarf2_cu *);
c906108c 1521
e142c38c
DJ
1522static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
1523 struct dwarf2_cu *);
c906108c 1524
348e048f 1525static struct attribute *dwarf2_attr_no_follow (struct die_info *,
45e58e77 1526 unsigned int);
348e048f 1527
7d45c7c3
KB
1528static const char *dwarf2_string_attr (struct die_info *die, unsigned int name,
1529 struct dwarf2_cu *cu);
1530
05cf31d1
JB
1531static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
1532 struct dwarf2_cu *cu);
1533
e142c38c 1534static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
3ca72b44 1535
e142c38c 1536static struct die_info *die_specification (struct die_info *die,
f2f0e013 1537 struct dwarf2_cu **);
63d06c5c 1538
debd256d
JB
1539static void free_line_header (struct line_header *lh);
1540
3019eac3
DE
1541static struct line_header *dwarf_decode_line_header (unsigned int offset,
1542 struct dwarf2_cu *cu);
debd256d 1543
f3f5162e 1544static void dwarf_decode_lines (struct line_header *, const char *,
c3b7b696 1545 struct dwarf2_cu *, struct partial_symtab *,
527f3840 1546 CORE_ADDR, int decode_mapping);
c906108c 1547
4d663531 1548static void dwarf2_start_subfile (const char *, const char *);
c906108c 1549
43f3e411
DE
1550static struct compunit_symtab *dwarf2_start_symtab (struct dwarf2_cu *,
1551 const char *, const char *,
1552 CORE_ADDR);
f4dc4d17 1553
a14ed312 1554static struct symbol *new_symbol (struct die_info *, struct type *,
e7c27a73 1555 struct dwarf2_cu *);
c906108c 1556
34eaf542
TT
1557static struct symbol *new_symbol_full (struct die_info *, struct type *,
1558 struct dwarf2_cu *, struct symbol *);
1559
ff39bb5e 1560static void dwarf2_const_value (const struct attribute *, struct symbol *,
e7c27a73 1561 struct dwarf2_cu *);
c906108c 1562
ff39bb5e 1563static void dwarf2_const_value_attr (const struct attribute *attr,
98bfdba5
PA
1564 struct type *type,
1565 const char *name,
1566 struct obstack *obstack,
12df843f 1567 struct dwarf2_cu *cu, LONGEST *value,
d521ce57 1568 const gdb_byte **bytes,
98bfdba5 1569 struct dwarf2_locexpr_baton **baton);
2df3850c 1570
e7c27a73 1571static struct type *die_type (struct die_info *, struct dwarf2_cu *);
c906108c 1572
b4ba55a1
JB
1573static int need_gnat_info (struct dwarf2_cu *);
1574
3e43a32a
MS
1575static struct type *die_descriptive_type (struct die_info *,
1576 struct dwarf2_cu *);
b4ba55a1
JB
1577
1578static void set_descriptive_type (struct type *, struct die_info *,
1579 struct dwarf2_cu *);
1580
e7c27a73
DJ
1581static struct type *die_containing_type (struct die_info *,
1582 struct dwarf2_cu *);
c906108c 1583
ff39bb5e 1584static struct type *lookup_die_type (struct die_info *, const struct attribute *,
673bfd45 1585 struct dwarf2_cu *);
c906108c 1586
f792889a 1587static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
c906108c 1588
673bfd45
DE
1589static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1590
0d5cff50 1591static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
63d06c5c 1592
6e70227d 1593static char *typename_concat (struct obstack *obs, const char *prefix,
f55ee35c
JK
1594 const char *suffix, int physname,
1595 struct dwarf2_cu *cu);
63d06c5c 1596
e7c27a73 1597static void read_file_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1598
348e048f
DE
1599static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1600
e7c27a73 1601static void read_func_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1602
e7c27a73 1603static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1604
96408a79
SA
1605static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1606
ff013f42
JK
1607static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
1608 struct dwarf2_cu *, struct partial_symtab *);
1609
3a2b436a 1610/* How dwarf2_get_pc_bounds constructed its *LOWPC and *HIGHPC return
e385593e 1611 values. Keep the items ordered with increasing constraints compliance. */
3a2b436a
JK
1612enum pc_bounds_kind
1613{
e385593e 1614 /* No attribute DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges was found. */
3a2b436a
JK
1615 PC_BOUNDS_NOT_PRESENT,
1616
e385593e
JK
1617 /* Some of the attributes DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges
1618 were present but they do not form a valid range of PC addresses. */
1619 PC_BOUNDS_INVALID,
1620
3a2b436a
JK
1621 /* Discontiguous range was found - that is DW_AT_ranges was found. */
1622 PC_BOUNDS_RANGES,
1623
1624 /* Contiguous range was found - DW_AT_low_pc and DW_AT_high_pc were found. */
1625 PC_BOUNDS_HIGH_LOW,
1626};
1627
1628static enum pc_bounds_kind dwarf2_get_pc_bounds (struct die_info *,
1629 CORE_ADDR *, CORE_ADDR *,
1630 struct dwarf2_cu *,
1631 struct partial_symtab *);
c906108c 1632
fae299cd
DC
1633static void get_scope_pc_bounds (struct die_info *,
1634 CORE_ADDR *, CORE_ADDR *,
1635 struct dwarf2_cu *);
1636
801e3a5b
JB
1637static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1638 CORE_ADDR, struct dwarf2_cu *);
1639
a14ed312 1640static void dwarf2_add_field (struct field_info *, struct die_info *,
e7c27a73 1641 struct dwarf2_cu *);
c906108c 1642
a14ed312 1643static void dwarf2_attach_fields_to_type (struct field_info *,
e7c27a73 1644 struct type *, struct dwarf2_cu *);
c906108c 1645
a14ed312 1646static void dwarf2_add_member_fn (struct field_info *,
e26fb1d7 1647 struct die_info *, struct type *,
e7c27a73 1648 struct dwarf2_cu *);
c906108c 1649
a14ed312 1650static void dwarf2_attach_fn_fields_to_type (struct field_info *,
3e43a32a
MS
1651 struct type *,
1652 struct dwarf2_cu *);
c906108c 1653
134d01f1 1654static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1655
e7c27a73 1656static void read_common_block (struct die_info *, struct dwarf2_cu *);
c906108c 1657
e7c27a73 1658static void read_namespace (struct die_info *die, struct dwarf2_cu *);
d9fa45fe 1659
5d7cb8df
JK
1660static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1661
22cee43f
PMR
1662static struct using_direct **using_directives (enum language);
1663
27aa8d6a
SW
1664static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1665
74921315
KS
1666static int read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu);
1667
f55ee35c
JK
1668static struct type *read_module_type (struct die_info *die,
1669 struct dwarf2_cu *cu);
1670
38d518c9 1671static const char *namespace_name (struct die_info *die,
e142c38c 1672 int *is_anonymous, struct dwarf2_cu *);
38d518c9 1673
134d01f1 1674static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1675
e7c27a73 1676static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *);
c906108c 1677
6e70227d 1678static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
7ca2d3a3
DL
1679 struct dwarf2_cu *);
1680
bf6af496 1681static struct die_info *read_die_and_siblings_1
d521ce57 1682 (const struct die_reader_specs *, const gdb_byte *, const gdb_byte **,
bf6af496 1683 struct die_info *);
639d11d3 1684
dee91e82 1685static struct die_info *read_die_and_siblings (const struct die_reader_specs *,
d521ce57
TT
1686 const gdb_byte *info_ptr,
1687 const gdb_byte **new_info_ptr,
639d11d3
DC
1688 struct die_info *parent);
1689
d521ce57
TT
1690static const gdb_byte *read_full_die_1 (const struct die_reader_specs *,
1691 struct die_info **, const gdb_byte *,
1692 int *, int);
3019eac3 1693
d521ce57
TT
1694static const gdb_byte *read_full_die (const struct die_reader_specs *,
1695 struct die_info **, const gdb_byte *,
1696 int *);
93311388 1697
e7c27a73 1698static void process_die (struct die_info *, struct dwarf2_cu *);
c906108c 1699
15d034d0
TT
1700static const char *dwarf2_canonicalize_name (const char *, struct dwarf2_cu *,
1701 struct obstack *);
71c25dea 1702
15d034d0 1703static const char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
9219021c 1704
15d034d0 1705static const char *dwarf2_full_name (const char *name,
98bfdba5
PA
1706 struct die_info *die,
1707 struct dwarf2_cu *cu);
1708
ca69b9e6
DE
1709static const char *dwarf2_physname (const char *name, struct die_info *die,
1710 struct dwarf2_cu *cu);
1711
e142c38c 1712static struct die_info *dwarf2_extension (struct die_info *die,
f2f0e013 1713 struct dwarf2_cu **);
9219021c 1714
f39c6ffd 1715static const char *dwarf_tag_name (unsigned int);
c906108c 1716
f39c6ffd 1717static const char *dwarf_attr_name (unsigned int);
c906108c 1718
f39c6ffd 1719static const char *dwarf_form_name (unsigned int);
c906108c 1720
a14ed312 1721static char *dwarf_bool_name (unsigned int);
c906108c 1722
f39c6ffd 1723static const char *dwarf_type_encoding_name (unsigned int);
c906108c 1724
f9aca02d 1725static struct die_info *sibling_die (struct die_info *);
c906108c 1726
d97bc12b
DE
1727static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1728
1729static void dump_die_for_error (struct die_info *);
1730
1731static void dump_die_1 (struct ui_file *, int level, int max_level,
1732 struct die_info *);
c906108c 1733
d97bc12b 1734/*static*/ void dump_die (struct die_info *, int max_level);
c906108c 1735
51545339 1736static void store_in_ref_table (struct die_info *,
10b3939b 1737 struct dwarf2_cu *);
c906108c 1738
ff39bb5e 1739static sect_offset dwarf2_get_ref_die_offset (const struct attribute *);
c906108c 1740
ff39bb5e 1741static LONGEST dwarf2_get_attr_constant_value (const struct attribute *, int);
a02abb62 1742
348e048f 1743static struct die_info *follow_die_ref_or_sig (struct die_info *,
ff39bb5e 1744 const struct attribute *,
348e048f
DE
1745 struct dwarf2_cu **);
1746
10b3939b 1747static struct die_info *follow_die_ref (struct die_info *,
ff39bb5e 1748 const struct attribute *,
f2f0e013 1749 struct dwarf2_cu **);
c906108c 1750
348e048f 1751static struct die_info *follow_die_sig (struct die_info *,
ff39bb5e 1752 const struct attribute *,
348e048f
DE
1753 struct dwarf2_cu **);
1754
ac9ec31b
DE
1755static struct type *get_signatured_type (struct die_info *, ULONGEST,
1756 struct dwarf2_cu *);
1757
1758static struct type *get_DW_AT_signature_type (struct die_info *,
ff39bb5e 1759 const struct attribute *,
ac9ec31b
DE
1760 struct dwarf2_cu *);
1761
e5fe5e75 1762static void load_full_type_unit (struct dwarf2_per_cu_data *per_cu);
348e048f 1763
52dc124a 1764static void read_signatured_type (struct signatured_type *);
348e048f 1765
63e43d3a
PMR
1766static int attr_to_dynamic_prop (const struct attribute *attr,
1767 struct die_info *die, struct dwarf2_cu *cu,
1768 struct dynamic_prop *prop);
1769
c906108c
SS
1770/* memory allocation interface */
1771
7b5a2f43 1772static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
c906108c 1773
b60c80d6 1774static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
c906108c 1775
43f3e411 1776static void dwarf_decode_macros (struct dwarf2_cu *, unsigned int, int);
2e276125 1777
6e5a29e1 1778static int attr_form_is_block (const struct attribute *);
8e19ed76 1779
6e5a29e1 1780static int attr_form_is_section_offset (const struct attribute *);
3690dd37 1781
6e5a29e1 1782static int attr_form_is_constant (const struct attribute *);
3690dd37 1783
6e5a29e1 1784static int attr_form_is_ref (const struct attribute *);
7771576e 1785
8cf6f0b1
TT
1786static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1787 struct dwarf2_loclist_baton *baton,
ff39bb5e 1788 const struct attribute *attr);
8cf6f0b1 1789
ff39bb5e 1790static void dwarf2_symbol_mark_computed (const struct attribute *attr,
93e7bd98 1791 struct symbol *sym,
f1e6e072
TT
1792 struct dwarf2_cu *cu,
1793 int is_block);
4c2df51b 1794
d521ce57
TT
1795static const gdb_byte *skip_one_die (const struct die_reader_specs *reader,
1796 const gdb_byte *info_ptr,
1797 struct abbrev_info *abbrev);
4bb7a0a7 1798
72bf9492
DJ
1799static void free_stack_comp_unit (void *);
1800
72bf9492
DJ
1801static hashval_t partial_die_hash (const void *item);
1802
1803static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1804
ae038cb0 1805static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
36586728 1806 (sect_offset offset, unsigned int offset_in_dwz, struct objfile *objfile);
ae038cb0 1807
9816fde3 1808static void init_one_comp_unit (struct dwarf2_cu *cu,
23745b47 1809 struct dwarf2_per_cu_data *per_cu);
9816fde3
JK
1810
1811static void prepare_one_comp_unit (struct dwarf2_cu *cu,
95554aad
TT
1812 struct die_info *comp_unit_die,
1813 enum language pretend_language);
93311388 1814
68dc6402 1815static void free_heap_comp_unit (void *);
ae038cb0
DJ
1816
1817static void free_cached_comp_units (void *);
1818
1819static void age_cached_comp_units (void);
1820
dee91e82 1821static void free_one_cached_comp_unit (struct dwarf2_per_cu_data *);
ae038cb0 1822
f792889a
DJ
1823static struct type *set_die_type (struct die_info *, struct type *,
1824 struct dwarf2_cu *);
1c379e20 1825
ae038cb0
DJ
1826static void create_all_comp_units (struct objfile *);
1827
0e50663e 1828static int create_all_type_units (struct objfile *);
1fd400ff 1829
95554aad
TT
1830static void load_full_comp_unit (struct dwarf2_per_cu_data *,
1831 enum language);
10b3939b 1832
95554aad
TT
1833static void process_full_comp_unit (struct dwarf2_per_cu_data *,
1834 enum language);
10b3939b 1835
f4dc4d17
DE
1836static void process_full_type_unit (struct dwarf2_per_cu_data *,
1837 enum language);
1838
10b3939b
DJ
1839static void dwarf2_add_dependence (struct dwarf2_cu *,
1840 struct dwarf2_per_cu_data *);
1841
ae038cb0
DJ
1842static void dwarf2_mark (struct dwarf2_cu *);
1843
1844static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
1845
b64f50a1 1846static struct type *get_die_type_at_offset (sect_offset,
ac9ec31b 1847 struct dwarf2_per_cu_data *);
673bfd45 1848
f792889a 1849static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
72019c9c 1850
9291a0cd
TT
1851static void dwarf2_release_queue (void *dummy);
1852
95554aad
TT
1853static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
1854 enum language pretend_language);
1855
a0f42c21 1856static void process_queue (void);
9291a0cd
TT
1857
1858static void find_file_and_directory (struct die_info *die,
1859 struct dwarf2_cu *cu,
15d034d0 1860 const char **name, const char **comp_dir);
9291a0cd
TT
1861
1862static char *file_full_name (int file, struct line_header *lh,
1863 const char *comp_dir);
1864
d521ce57 1865static const gdb_byte *read_and_check_comp_unit_head
36586728
TT
1866 (struct comp_unit_head *header,
1867 struct dwarf2_section_info *section,
d521ce57 1868 struct dwarf2_section_info *abbrev_section, const gdb_byte *info_ptr,
36586728
TT
1869 int is_debug_types_section);
1870
fd820528 1871static void init_cutu_and_read_dies
f4dc4d17
DE
1872 (struct dwarf2_per_cu_data *this_cu, struct abbrev_table *abbrev_table,
1873 int use_existing_cu, int keep,
3019eac3
DE
1874 die_reader_func_ftype *die_reader_func, void *data);
1875
dee91e82
DE
1876static void init_cutu_and_read_dies_simple
1877 (struct dwarf2_per_cu_data *this_cu,
1878 die_reader_func_ftype *die_reader_func, void *data);
9291a0cd 1879
673bfd45 1880static htab_t allocate_signatured_type_table (struct objfile *objfile);
1fd400ff 1881
3019eac3
DE
1882static htab_t allocate_dwo_unit_table (struct objfile *objfile);
1883
57d63ce2
DE
1884static struct dwo_unit *lookup_dwo_unit_in_dwp
1885 (struct dwp_file *dwp_file, const char *comp_dir,
1886 ULONGEST signature, int is_debug_types);
a2ce51a0
DE
1887
1888static struct dwp_file *get_dwp_file (void);
1889
3019eac3 1890static struct dwo_unit *lookup_dwo_comp_unit
a1855c1d 1891 (struct dwarf2_per_cu_data *, const char *, const char *, ULONGEST);
3019eac3
DE
1892
1893static struct dwo_unit *lookup_dwo_type_unit
a1855c1d 1894 (struct signatured_type *, const char *, const char *);
3019eac3 1895
89e63ee4
DE
1896static void queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *);
1897
3019eac3
DE
1898static void free_dwo_file_cleanup (void *);
1899
95554aad
TT
1900static void process_cu_includes (void);
1901
1b80a9fa 1902static void check_producer (struct dwarf2_cu *cu);
527f3840
JK
1903
1904static void free_line_header_voidp (void *arg);
4390d890
DE
1905\f
1906/* Various complaints about symbol reading that don't abort the process. */
1907
1908static void
1909dwarf2_statement_list_fits_in_line_number_section_complaint (void)
1910{
1911 complaint (&symfile_complaints,
1912 _("statement list doesn't fit in .debug_line section"));
1913}
1914
1915static void
1916dwarf2_debug_line_missing_file_complaint (void)
1917{
1918 complaint (&symfile_complaints,
1919 _(".debug_line section has line data without a file"));
1920}
1921
1922static void
1923dwarf2_debug_line_missing_end_sequence_complaint (void)
1924{
1925 complaint (&symfile_complaints,
1926 _(".debug_line section has line "
1927 "program sequence without an end"));
1928}
1929
1930static void
1931dwarf2_complex_location_expr_complaint (void)
1932{
1933 complaint (&symfile_complaints, _("location expression too complex"));
1934}
1935
1936static void
1937dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
1938 int arg3)
1939{
1940 complaint (&symfile_complaints,
1941 _("const value length mismatch for '%s', got %d, expected %d"),
1942 arg1, arg2, arg3);
1943}
1944
1945static void
1946dwarf2_section_buffer_overflow_complaint (struct dwarf2_section_info *section)
1947{
1948 complaint (&symfile_complaints,
1949 _("debug info runs off end of %s section"
1950 " [in module %s]"),
a32a8923
DE
1951 get_section_name (section),
1952 get_section_file_name (section));
4390d890 1953}
1b80a9fa 1954
4390d890
DE
1955static void
1956dwarf2_macro_malformed_definition_complaint (const char *arg1)
1957{
1958 complaint (&symfile_complaints,
1959 _("macro debug info contains a "
1960 "malformed macro definition:\n`%s'"),
1961 arg1);
1962}
1963
1964static void
1965dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
1966{
1967 complaint (&symfile_complaints,
1968 _("invalid attribute class or form for '%s' in '%s'"),
1969 arg1, arg2);
1970}
527f3840
JK
1971
1972/* Hash function for line_header_hash. */
1973
1974static hashval_t
1975line_header_hash (const struct line_header *ofs)
1976{
1977 return ofs->offset.sect_off ^ ofs->offset_in_dwz;
1978}
1979
1980/* Hash function for htab_create_alloc_ex for line_header_hash. */
1981
1982static hashval_t
1983line_header_hash_voidp (const void *item)
1984{
9a3c8263 1985 const struct line_header *ofs = (const struct line_header *) item;
527f3840
JK
1986
1987 return line_header_hash (ofs);
1988}
1989
1990/* Equality function for line_header_hash. */
1991
1992static int
1993line_header_eq_voidp (const void *item_lhs, const void *item_rhs)
1994{
9a3c8263
SM
1995 const struct line_header *ofs_lhs = (const struct line_header *) item_lhs;
1996 const struct line_header *ofs_rhs = (const struct line_header *) item_rhs;
527f3840
JK
1997
1998 return (ofs_lhs->offset.sect_off == ofs_rhs->offset.sect_off
1999 && ofs_lhs->offset_in_dwz == ofs_rhs->offset_in_dwz);
2000}
2001
4390d890 2002\f
9291a0cd
TT
2003#if WORDS_BIGENDIAN
2004
2005/* Convert VALUE between big- and little-endian. */
2006static offset_type
2007byte_swap (offset_type value)
2008{
2009 offset_type result;
2010
2011 result = (value & 0xff) << 24;
2012 result |= (value & 0xff00) << 8;
2013 result |= (value & 0xff0000) >> 8;
2014 result |= (value & 0xff000000) >> 24;
2015 return result;
2016}
2017
2018#define MAYBE_SWAP(V) byte_swap (V)
2019
2020#else
2021#define MAYBE_SWAP(V) (V)
2022#endif /* WORDS_BIGENDIAN */
2023
31aa7e4e
JB
2024/* Read the given attribute value as an address, taking the attribute's
2025 form into account. */
2026
2027static CORE_ADDR
2028attr_value_as_address (struct attribute *attr)
2029{
2030 CORE_ADDR addr;
2031
2032 if (attr->form != DW_FORM_addr && attr->form != DW_FORM_GNU_addr_index)
2033 {
2034 /* Aside from a few clearly defined exceptions, attributes that
2035 contain an address must always be in DW_FORM_addr form.
2036 Unfortunately, some compilers happen to be violating this
2037 requirement by encoding addresses using other forms, such
2038 as DW_FORM_data4 for example. For those broken compilers,
2039 we try to do our best, without any guarantee of success,
2040 to interpret the address correctly. It would also be nice
2041 to generate a complaint, but that would require us to maintain
2042 a list of legitimate cases where a non-address form is allowed,
2043 as well as update callers to pass in at least the CU's DWARF
2044 version. This is more overhead than what we're willing to
2045 expand for a pretty rare case. */
2046 addr = DW_UNSND (attr);
2047 }
2048 else
2049 addr = DW_ADDR (attr);
2050
2051 return addr;
2052}
2053
9291a0cd
TT
2054/* The suffix for an index file. */
2055#define INDEX_SUFFIX ".gdb-index"
2056
c906108c 2057/* Try to locate the sections we need for DWARF 2 debugging
251d32d9
TG
2058 information and return true if we have enough to do something.
2059 NAMES points to the dwarf2 section names, or is NULL if the standard
2060 ELF names are used. */
c906108c
SS
2061
2062int
251d32d9
TG
2063dwarf2_has_info (struct objfile *objfile,
2064 const struct dwarf2_debug_sections *names)
c906108c 2065{
9a3c8263
SM
2066 dwarf2_per_objfile = ((struct dwarf2_per_objfile *)
2067 objfile_data (objfile, dwarf2_objfile_data_key));
be391dca
TT
2068 if (!dwarf2_per_objfile)
2069 {
2070 /* Initialize per-objfile state. */
2071 struct dwarf2_per_objfile *data
8d749320 2072 = XOBNEW (&objfile->objfile_obstack, struct dwarf2_per_objfile);
9a619af0 2073
be391dca
TT
2074 memset (data, 0, sizeof (*data));
2075 set_objfile_data (objfile, dwarf2_objfile_data_key, data);
2076 dwarf2_per_objfile = data;
6502dd73 2077
251d32d9
TG
2078 bfd_map_over_sections (objfile->obfd, dwarf2_locate_sections,
2079 (void *) names);
be391dca
TT
2080 dwarf2_per_objfile->objfile = objfile;
2081 }
73869dc2 2082 return (!dwarf2_per_objfile->info.is_virtual
049412e3 2083 && dwarf2_per_objfile->info.s.section != NULL
73869dc2 2084 && !dwarf2_per_objfile->abbrev.is_virtual
049412e3 2085 && dwarf2_per_objfile->abbrev.s.section != NULL);
73869dc2
DE
2086}
2087
2088/* Return the containing section of virtual section SECTION. */
2089
2090static struct dwarf2_section_info *
2091get_containing_section (const struct dwarf2_section_info *section)
2092{
2093 gdb_assert (section->is_virtual);
2094 return section->s.containing_section;
c906108c
SS
2095}
2096
a32a8923
DE
2097/* Return the bfd owner of SECTION. */
2098
2099static struct bfd *
2100get_section_bfd_owner (const struct dwarf2_section_info *section)
2101{
73869dc2
DE
2102 if (section->is_virtual)
2103 {
2104 section = get_containing_section (section);
2105 gdb_assert (!section->is_virtual);
2106 }
049412e3 2107 return section->s.section->owner;
a32a8923
DE
2108}
2109
2110/* Return the bfd section of SECTION.
2111 Returns NULL if the section is not present. */
2112
2113static asection *
2114get_section_bfd_section (const struct dwarf2_section_info *section)
2115{
73869dc2
DE
2116 if (section->is_virtual)
2117 {
2118 section = get_containing_section (section);
2119 gdb_assert (!section->is_virtual);
2120 }
049412e3 2121 return section->s.section;
a32a8923
DE
2122}
2123
2124/* Return the name of SECTION. */
2125
2126static const char *
2127get_section_name (const struct dwarf2_section_info *section)
2128{
2129 asection *sectp = get_section_bfd_section (section);
2130
2131 gdb_assert (sectp != NULL);
2132 return bfd_section_name (get_section_bfd_owner (section), sectp);
2133}
2134
2135/* Return the name of the file SECTION is in. */
2136
2137static const char *
2138get_section_file_name (const struct dwarf2_section_info *section)
2139{
2140 bfd *abfd = get_section_bfd_owner (section);
2141
2142 return bfd_get_filename (abfd);
2143}
2144
2145/* Return the id of SECTION.
2146 Returns 0 if SECTION doesn't exist. */
2147
2148static int
2149get_section_id (const struct dwarf2_section_info *section)
2150{
2151 asection *sectp = get_section_bfd_section (section);
2152
2153 if (sectp == NULL)
2154 return 0;
2155 return sectp->id;
2156}
2157
2158/* Return the flags of SECTION.
73869dc2 2159 SECTION (or containing section if this is a virtual section) must exist. */
a32a8923
DE
2160
2161static int
2162get_section_flags (const struct dwarf2_section_info *section)
2163{
2164 asection *sectp = get_section_bfd_section (section);
2165
2166 gdb_assert (sectp != NULL);
2167 return bfd_get_section_flags (sectp->owner, sectp);
2168}
2169
251d32d9
TG
2170/* When loading sections, we look either for uncompressed section or for
2171 compressed section names. */
233a11ab
CS
2172
2173static int
251d32d9
TG
2174section_is_p (const char *section_name,
2175 const struct dwarf2_section_names *names)
233a11ab 2176{
251d32d9
TG
2177 if (names->normal != NULL
2178 && strcmp (section_name, names->normal) == 0)
2179 return 1;
2180 if (names->compressed != NULL
2181 && strcmp (section_name, names->compressed) == 0)
2182 return 1;
2183 return 0;
233a11ab
CS
2184}
2185
c906108c
SS
2186/* This function is mapped across the sections and remembers the
2187 offset and size of each of the debugging sections we are interested
2188 in. */
2189
2190static void
251d32d9 2191dwarf2_locate_sections (bfd *abfd, asection *sectp, void *vnames)
c906108c 2192{
251d32d9 2193 const struct dwarf2_debug_sections *names;
dc7650b8 2194 flagword aflag = bfd_get_section_flags (abfd, sectp);
251d32d9
TG
2195
2196 if (vnames == NULL)
2197 names = &dwarf2_elf_names;
2198 else
2199 names = (const struct dwarf2_debug_sections *) vnames;
2200
dc7650b8
JK
2201 if ((aflag & SEC_HAS_CONTENTS) == 0)
2202 {
2203 }
2204 else if (section_is_p (sectp->name, &names->info))
c906108c 2205 {
049412e3 2206 dwarf2_per_objfile->info.s.section = sectp;
dce234bc 2207 dwarf2_per_objfile->info.size = bfd_get_section_size (sectp);
c906108c 2208 }
251d32d9 2209 else if (section_is_p (sectp->name, &names->abbrev))
c906108c 2210 {
049412e3 2211 dwarf2_per_objfile->abbrev.s.section = sectp;
dce234bc 2212 dwarf2_per_objfile->abbrev.size = bfd_get_section_size (sectp);
c906108c 2213 }
251d32d9 2214 else if (section_is_p (sectp->name, &names->line))
c906108c 2215 {
049412e3 2216 dwarf2_per_objfile->line.s.section = sectp;
dce234bc 2217 dwarf2_per_objfile->line.size = bfd_get_section_size (sectp);
c906108c 2218 }
251d32d9 2219 else if (section_is_p (sectp->name, &names->loc))
c906108c 2220 {
049412e3 2221 dwarf2_per_objfile->loc.s.section = sectp;
dce234bc 2222 dwarf2_per_objfile->loc.size = bfd_get_section_size (sectp);
c906108c 2223 }
251d32d9 2224 else if (section_is_p (sectp->name, &names->macinfo))
c906108c 2225 {
049412e3 2226 dwarf2_per_objfile->macinfo.s.section = sectp;
dce234bc 2227 dwarf2_per_objfile->macinfo.size = bfd_get_section_size (sectp);
c906108c 2228 }
cf2c3c16
TT
2229 else if (section_is_p (sectp->name, &names->macro))
2230 {
049412e3 2231 dwarf2_per_objfile->macro.s.section = sectp;
cf2c3c16
TT
2232 dwarf2_per_objfile->macro.size = bfd_get_section_size (sectp);
2233 }
251d32d9 2234 else if (section_is_p (sectp->name, &names->str))
c906108c 2235 {
049412e3 2236 dwarf2_per_objfile->str.s.section = sectp;
dce234bc 2237 dwarf2_per_objfile->str.size = bfd_get_section_size (sectp);
c906108c 2238 }
3019eac3
DE
2239 else if (section_is_p (sectp->name, &names->addr))
2240 {
049412e3 2241 dwarf2_per_objfile->addr.s.section = sectp;
3019eac3
DE
2242 dwarf2_per_objfile->addr.size = bfd_get_section_size (sectp);
2243 }
251d32d9 2244 else if (section_is_p (sectp->name, &names->frame))
b6af0555 2245 {
049412e3 2246 dwarf2_per_objfile->frame.s.section = sectp;
dce234bc 2247 dwarf2_per_objfile->frame.size = bfd_get_section_size (sectp);
b6af0555 2248 }
251d32d9 2249 else if (section_is_p (sectp->name, &names->eh_frame))
b6af0555 2250 {
049412e3 2251 dwarf2_per_objfile->eh_frame.s.section = sectp;
dc7650b8 2252 dwarf2_per_objfile->eh_frame.size = bfd_get_section_size (sectp);
b6af0555 2253 }
251d32d9 2254 else if (section_is_p (sectp->name, &names->ranges))
af34e669 2255 {
049412e3 2256 dwarf2_per_objfile->ranges.s.section = sectp;
dce234bc 2257 dwarf2_per_objfile->ranges.size = bfd_get_section_size (sectp);
af34e669 2258 }
251d32d9 2259 else if (section_is_p (sectp->name, &names->types))
348e048f 2260 {
8b70b953
TT
2261 struct dwarf2_section_info type_section;
2262
2263 memset (&type_section, 0, sizeof (type_section));
049412e3 2264 type_section.s.section = sectp;
8b70b953
TT
2265 type_section.size = bfd_get_section_size (sectp);
2266
2267 VEC_safe_push (dwarf2_section_info_def, dwarf2_per_objfile->types,
2268 &type_section);
348e048f 2269 }
251d32d9 2270 else if (section_is_p (sectp->name, &names->gdb_index))
9291a0cd 2271 {
049412e3 2272 dwarf2_per_objfile->gdb_index.s.section = sectp;
9291a0cd
TT
2273 dwarf2_per_objfile->gdb_index.size = bfd_get_section_size (sectp);
2274 }
dce234bc 2275
b4e1fd61 2276 if ((bfd_get_section_flags (abfd, sectp) & (SEC_LOAD | SEC_ALLOC))
72dca2f5
FR
2277 && bfd_section_vma (abfd, sectp) == 0)
2278 dwarf2_per_objfile->has_section_at_zero = 1;
c906108c
SS
2279}
2280
fceca515
DE
2281/* A helper function that decides whether a section is empty,
2282 or not present. */
9e0ac564
TT
2283
2284static int
19ac8c2e 2285dwarf2_section_empty_p (const struct dwarf2_section_info *section)
9e0ac564 2286{
73869dc2
DE
2287 if (section->is_virtual)
2288 return section->size == 0;
049412e3 2289 return section->s.section == NULL || section->size == 0;
9e0ac564
TT
2290}
2291
3019eac3
DE
2292/* Read the contents of the section INFO.
2293 OBJFILE is the main object file, but not necessarily the file where
a32a8923
DE
2294 the section comes from. E.g., for DWO files the bfd of INFO is the bfd
2295 of the DWO file.
dce234bc 2296 If the section is compressed, uncompress it before returning. */
c906108c 2297
dce234bc
PP
2298static void
2299dwarf2_read_section (struct objfile *objfile, struct dwarf2_section_info *info)
c906108c 2300{
a32a8923 2301 asection *sectp;
3019eac3 2302 bfd *abfd;
dce234bc 2303 gdb_byte *buf, *retbuf;
c906108c 2304
be391dca
TT
2305 if (info->readin)
2306 return;
dce234bc 2307 info->buffer = NULL;
be391dca 2308 info->readin = 1;
188dd5d6 2309
9e0ac564 2310 if (dwarf2_section_empty_p (info))
dce234bc 2311 return;
c906108c 2312
a32a8923 2313 sectp = get_section_bfd_section (info);
3019eac3 2314
73869dc2
DE
2315 /* If this is a virtual section we need to read in the real one first. */
2316 if (info->is_virtual)
2317 {
2318 struct dwarf2_section_info *containing_section =
2319 get_containing_section (info);
2320
2321 gdb_assert (sectp != NULL);
2322 if ((sectp->flags & SEC_RELOC) != 0)
2323 {
2324 error (_("Dwarf Error: DWP format V2 with relocations is not"
2325 " supported in section %s [in module %s]"),
2326 get_section_name (info), get_section_file_name (info));
2327 }
2328 dwarf2_read_section (objfile, containing_section);
2329 /* Other code should have already caught virtual sections that don't
2330 fit. */
2331 gdb_assert (info->virtual_offset + info->size
2332 <= containing_section->size);
2333 /* If the real section is empty or there was a problem reading the
2334 section we shouldn't get here. */
2335 gdb_assert (containing_section->buffer != NULL);
2336 info->buffer = containing_section->buffer + info->virtual_offset;
2337 return;
2338 }
2339
4bf44c1c
TT
2340 /* If the section has relocations, we must read it ourselves.
2341 Otherwise we attach it to the BFD. */
2342 if ((sectp->flags & SEC_RELOC) == 0)
dce234bc 2343 {
d521ce57 2344 info->buffer = gdb_bfd_map_section (sectp, &info->size);
4bf44c1c 2345 return;
dce234bc 2346 }
dce234bc 2347
224c3ddb 2348 buf = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, info->size);
4bf44c1c 2349 info->buffer = buf;
dce234bc
PP
2350
2351 /* When debugging .o files, we may need to apply relocations; see
2352 http://sourceware.org/ml/gdb-patches/2002-04/msg00136.html .
2353 We never compress sections in .o files, so we only need to
2354 try this when the section is not compressed. */
ac8035ab 2355 retbuf = symfile_relocate_debug_section (objfile, sectp, buf);
dce234bc
PP
2356 if (retbuf != NULL)
2357 {
2358 info->buffer = retbuf;
2359 return;
2360 }
2361
a32a8923
DE
2362 abfd = get_section_bfd_owner (info);
2363 gdb_assert (abfd != NULL);
2364
dce234bc
PP
2365 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
2366 || bfd_bread (buf, info->size, abfd) != info->size)
19ac8c2e
DE
2367 {
2368 error (_("Dwarf Error: Can't read DWARF data"
2369 " in section %s [in module %s]"),
2370 bfd_section_name (abfd, sectp), bfd_get_filename (abfd));
2371 }
dce234bc
PP
2372}
2373
9e0ac564
TT
2374/* A helper function that returns the size of a section in a safe way.
2375 If you are positive that the section has been read before using the
2376 size, then it is safe to refer to the dwarf2_section_info object's
2377 "size" field directly. In other cases, you must call this
2378 function, because for compressed sections the size field is not set
2379 correctly until the section has been read. */
2380
2381static bfd_size_type
2382dwarf2_section_size (struct objfile *objfile,
2383 struct dwarf2_section_info *info)
2384{
2385 if (!info->readin)
2386 dwarf2_read_section (objfile, info);
2387 return info->size;
2388}
2389
dce234bc 2390/* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
0963b4bd 2391 SECTION_NAME. */
af34e669 2392
dce234bc 2393void
3017a003
TG
2394dwarf2_get_section_info (struct objfile *objfile,
2395 enum dwarf2_section_enum sect,
d521ce57 2396 asection **sectp, const gdb_byte **bufp,
dce234bc
PP
2397 bfd_size_type *sizep)
2398{
2399 struct dwarf2_per_objfile *data
9a3c8263
SM
2400 = (struct dwarf2_per_objfile *) objfile_data (objfile,
2401 dwarf2_objfile_data_key);
dce234bc 2402 struct dwarf2_section_info *info;
a3b2a86b
TT
2403
2404 /* We may see an objfile without any DWARF, in which case we just
2405 return nothing. */
2406 if (data == NULL)
2407 {
2408 *sectp = NULL;
2409 *bufp = NULL;
2410 *sizep = 0;
2411 return;
2412 }
3017a003
TG
2413 switch (sect)
2414 {
2415 case DWARF2_DEBUG_FRAME:
2416 info = &data->frame;
2417 break;
2418 case DWARF2_EH_FRAME:
2419 info = &data->eh_frame;
2420 break;
2421 default:
2422 gdb_assert_not_reached ("unexpected section");
2423 }
dce234bc 2424
9e0ac564 2425 dwarf2_read_section (objfile, info);
dce234bc 2426
a32a8923 2427 *sectp = get_section_bfd_section (info);
dce234bc
PP
2428 *bufp = info->buffer;
2429 *sizep = info->size;
2430}
2431
36586728
TT
2432/* A helper function to find the sections for a .dwz file. */
2433
2434static void
2435locate_dwz_sections (bfd *abfd, asection *sectp, void *arg)
2436{
9a3c8263 2437 struct dwz_file *dwz_file = (struct dwz_file *) arg;
36586728
TT
2438
2439 /* Note that we only support the standard ELF names, because .dwz
2440 is ELF-only (at the time of writing). */
2441 if (section_is_p (sectp->name, &dwarf2_elf_names.abbrev))
2442 {
049412e3 2443 dwz_file->abbrev.s.section = sectp;
36586728
TT
2444 dwz_file->abbrev.size = bfd_get_section_size (sectp);
2445 }
2446 else if (section_is_p (sectp->name, &dwarf2_elf_names.info))
2447 {
049412e3 2448 dwz_file->info.s.section = sectp;
36586728
TT
2449 dwz_file->info.size = bfd_get_section_size (sectp);
2450 }
2451 else if (section_is_p (sectp->name, &dwarf2_elf_names.str))
2452 {
049412e3 2453 dwz_file->str.s.section = sectp;
36586728
TT
2454 dwz_file->str.size = bfd_get_section_size (sectp);
2455 }
2456 else if (section_is_p (sectp->name, &dwarf2_elf_names.line))
2457 {
049412e3 2458 dwz_file->line.s.section = sectp;
36586728
TT
2459 dwz_file->line.size = bfd_get_section_size (sectp);
2460 }
2461 else if (section_is_p (sectp->name, &dwarf2_elf_names.macro))
2462 {
049412e3 2463 dwz_file->macro.s.section = sectp;
36586728
TT
2464 dwz_file->macro.size = bfd_get_section_size (sectp);
2465 }
2ec9a5e0
TT
2466 else if (section_is_p (sectp->name, &dwarf2_elf_names.gdb_index))
2467 {
049412e3 2468 dwz_file->gdb_index.s.section = sectp;
2ec9a5e0
TT
2469 dwz_file->gdb_index.size = bfd_get_section_size (sectp);
2470 }
36586728
TT
2471}
2472
4db1a1dc
TT
2473/* Open the separate '.dwz' debug file, if needed. Return NULL if
2474 there is no .gnu_debugaltlink section in the file. Error if there
2475 is such a section but the file cannot be found. */
36586728
TT
2476
2477static struct dwz_file *
2478dwarf2_get_dwz_file (void)
2479{
4db1a1dc
TT
2480 bfd *dwz_bfd;
2481 char *data;
36586728
TT
2482 struct cleanup *cleanup;
2483 const char *filename;
2484 struct dwz_file *result;
acd13123 2485 bfd_size_type buildid_len_arg;
dc294be5
TT
2486 size_t buildid_len;
2487 bfd_byte *buildid;
36586728
TT
2488
2489 if (dwarf2_per_objfile->dwz_file != NULL)
2490 return dwarf2_per_objfile->dwz_file;
2491
4db1a1dc
TT
2492 bfd_set_error (bfd_error_no_error);
2493 data = bfd_get_alt_debug_link_info (dwarf2_per_objfile->objfile->obfd,
acd13123 2494 &buildid_len_arg, &buildid);
4db1a1dc
TT
2495 if (data == NULL)
2496 {
2497 if (bfd_get_error () == bfd_error_no_error)
2498 return NULL;
2499 error (_("could not read '.gnu_debugaltlink' section: %s"),
2500 bfd_errmsg (bfd_get_error ()));
2501 }
36586728 2502 cleanup = make_cleanup (xfree, data);
dc294be5 2503 make_cleanup (xfree, buildid);
36586728 2504
acd13123
TT
2505 buildid_len = (size_t) buildid_len_arg;
2506
f9d83a0b 2507 filename = (const char *) data;
36586728
TT
2508 if (!IS_ABSOLUTE_PATH (filename))
2509 {
4262abfb 2510 char *abs = gdb_realpath (objfile_name (dwarf2_per_objfile->objfile));
36586728
TT
2511 char *rel;
2512
2513 make_cleanup (xfree, abs);
2514 abs = ldirname (abs);
2515 make_cleanup (xfree, abs);
2516
2517 rel = concat (abs, SLASH_STRING, filename, (char *) NULL);
2518 make_cleanup (xfree, rel);
2519 filename = rel;
2520 }
2521
dc294be5
TT
2522 /* First try the file name given in the section. If that doesn't
2523 work, try to use the build-id instead. */
36586728 2524 dwz_bfd = gdb_bfd_open (filename, gnutarget, -1);
dc294be5 2525 if (dwz_bfd != NULL)
36586728 2526 {
dc294be5
TT
2527 if (!build_id_verify (dwz_bfd, buildid_len, buildid))
2528 {
2529 gdb_bfd_unref (dwz_bfd);
2530 dwz_bfd = NULL;
2531 }
36586728
TT
2532 }
2533
dc294be5
TT
2534 if (dwz_bfd == NULL)
2535 dwz_bfd = build_id_to_debug_bfd (buildid_len, buildid);
2536
2537 if (dwz_bfd == NULL)
2538 error (_("could not find '.gnu_debugaltlink' file for %s"),
2539 objfile_name (dwarf2_per_objfile->objfile));
2540
36586728
TT
2541 result = OBSTACK_ZALLOC (&dwarf2_per_objfile->objfile->objfile_obstack,
2542 struct dwz_file);
2543 result->dwz_bfd = dwz_bfd;
2544
2545 bfd_map_over_sections (dwz_bfd, locate_dwz_sections, result);
2546
2547 do_cleanups (cleanup);
2548
13aaf454 2549 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd, dwz_bfd);
8d2cc612 2550 dwarf2_per_objfile->dwz_file = result;
36586728
TT
2551 return result;
2552}
9291a0cd 2553\f
7b9f3c50
DE
2554/* DWARF quick_symbols_functions support. */
2555
2556/* TUs can share .debug_line entries, and there can be a lot more TUs than
2557 unique line tables, so we maintain a separate table of all .debug_line
2558 derived entries to support the sharing.
2559 All the quick functions need is the list of file names. We discard the
2560 line_header when we're done and don't need to record it here. */
2561struct quick_file_names
2562{
094b34ac
DE
2563 /* The data used to construct the hash key. */
2564 struct stmt_list_hash hash;
7b9f3c50
DE
2565
2566 /* The number of entries in file_names, real_names. */
2567 unsigned int num_file_names;
2568
2569 /* The file names from the line table, after being run through
2570 file_full_name. */
2571 const char **file_names;
2572
2573 /* The file names from the line table after being run through
2574 gdb_realpath. These are computed lazily. */
2575 const char **real_names;
2576};
2577
2578/* When using the index (and thus not using psymtabs), each CU has an
2579 object of this type. This is used to hold information needed by
2580 the various "quick" methods. */
2581struct dwarf2_per_cu_quick_data
2582{
2583 /* The file table. This can be NULL if there was no file table
2584 or it's currently not read in.
2585 NOTE: This points into dwarf2_per_objfile->quick_file_names_table. */
2586 struct quick_file_names *file_names;
2587
2588 /* The corresponding symbol table. This is NULL if symbols for this
2589 CU have not yet been read. */
43f3e411 2590 struct compunit_symtab *compunit_symtab;
7b9f3c50
DE
2591
2592 /* A temporary mark bit used when iterating over all CUs in
2593 expand_symtabs_matching. */
2594 unsigned int mark : 1;
2595
2596 /* True if we've tried to read the file table and found there isn't one.
2597 There will be no point in trying to read it again next time. */
2598 unsigned int no_file_data : 1;
2599};
2600
094b34ac
DE
2601/* Utility hash function for a stmt_list_hash. */
2602
2603static hashval_t
2604hash_stmt_list_entry (const struct stmt_list_hash *stmt_list_hash)
2605{
2606 hashval_t v = 0;
2607
2608 if (stmt_list_hash->dwo_unit != NULL)
2609 v += (uintptr_t) stmt_list_hash->dwo_unit->dwo_file;
2610 v += stmt_list_hash->line_offset.sect_off;
2611 return v;
2612}
2613
2614/* Utility equality function for a stmt_list_hash. */
2615
2616static int
2617eq_stmt_list_entry (const struct stmt_list_hash *lhs,
2618 const struct stmt_list_hash *rhs)
2619{
2620 if ((lhs->dwo_unit != NULL) != (rhs->dwo_unit != NULL))
2621 return 0;
2622 if (lhs->dwo_unit != NULL
2623 && lhs->dwo_unit->dwo_file != rhs->dwo_unit->dwo_file)
2624 return 0;
2625
2626 return lhs->line_offset.sect_off == rhs->line_offset.sect_off;
2627}
2628
7b9f3c50
DE
2629/* Hash function for a quick_file_names. */
2630
2631static hashval_t
2632hash_file_name_entry (const void *e)
2633{
9a3c8263
SM
2634 const struct quick_file_names *file_data
2635 = (const struct quick_file_names *) e;
7b9f3c50 2636
094b34ac 2637 return hash_stmt_list_entry (&file_data->hash);
7b9f3c50
DE
2638}
2639
2640/* Equality function for a quick_file_names. */
2641
2642static int
2643eq_file_name_entry (const void *a, const void *b)
2644{
9a3c8263
SM
2645 const struct quick_file_names *ea = (const struct quick_file_names *) a;
2646 const struct quick_file_names *eb = (const struct quick_file_names *) b;
7b9f3c50 2647
094b34ac 2648 return eq_stmt_list_entry (&ea->hash, &eb->hash);
7b9f3c50
DE
2649}
2650
2651/* Delete function for a quick_file_names. */
2652
2653static void
2654delete_file_name_entry (void *e)
2655{
9a3c8263 2656 struct quick_file_names *file_data = (struct quick_file_names *) e;
7b9f3c50
DE
2657 int i;
2658
2659 for (i = 0; i < file_data->num_file_names; ++i)
2660 {
2661 xfree ((void*) file_data->file_names[i]);
2662 if (file_data->real_names)
2663 xfree ((void*) file_data->real_names[i]);
2664 }
2665
2666 /* The space for the struct itself lives on objfile_obstack,
2667 so we don't free it here. */
2668}
2669
2670/* Create a quick_file_names hash table. */
2671
2672static htab_t
2673create_quick_file_names_table (unsigned int nr_initial_entries)
2674{
2675 return htab_create_alloc (nr_initial_entries,
2676 hash_file_name_entry, eq_file_name_entry,
2677 delete_file_name_entry, xcalloc, xfree);
2678}
9291a0cd 2679
918dd910
JK
2680/* Read in PER_CU->CU. This function is unrelated to symtabs, symtab would
2681 have to be created afterwards. You should call age_cached_comp_units after
2682 processing PER_CU->CU. dw2_setup must have been already called. */
2683
2684static void
2685load_cu (struct dwarf2_per_cu_data *per_cu)
2686{
3019eac3 2687 if (per_cu->is_debug_types)
e5fe5e75 2688 load_full_type_unit (per_cu);
918dd910 2689 else
95554aad 2690 load_full_comp_unit (per_cu, language_minimal);
918dd910 2691
cc12ce38
DE
2692 if (per_cu->cu == NULL)
2693 return; /* Dummy CU. */
2dc860c0
DE
2694
2695 dwarf2_find_base_address (per_cu->cu->dies, per_cu->cu);
918dd910
JK
2696}
2697
a0f42c21 2698/* Read in the symbols for PER_CU. */
2fdf6df6 2699
9291a0cd 2700static void
a0f42c21 2701dw2_do_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
9291a0cd
TT
2702{
2703 struct cleanup *back_to;
2704
f4dc4d17
DE
2705 /* Skip type_unit_groups, reading the type units they contain
2706 is handled elsewhere. */
2707 if (IS_TYPE_UNIT_GROUP (per_cu))
2708 return;
2709
9291a0cd
TT
2710 back_to = make_cleanup (dwarf2_release_queue, NULL);
2711
95554aad 2712 if (dwarf2_per_objfile->using_index
43f3e411 2713 ? per_cu->v.quick->compunit_symtab == NULL
95554aad
TT
2714 : (per_cu->v.psymtab == NULL || !per_cu->v.psymtab->readin))
2715 {
2716 queue_comp_unit (per_cu, language_minimal);
2717 load_cu (per_cu);
89e63ee4
DE
2718
2719 /* If we just loaded a CU from a DWO, and we're working with an index
2720 that may badly handle TUs, load all the TUs in that DWO as well.
2721 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
2722 if (!per_cu->is_debug_types
cc12ce38 2723 && per_cu->cu != NULL
89e63ee4
DE
2724 && per_cu->cu->dwo_unit != NULL
2725 && dwarf2_per_objfile->index_table != NULL
2726 && dwarf2_per_objfile->index_table->version <= 7
2727 /* DWP files aren't supported yet. */
2728 && get_dwp_file () == NULL)
2729 queue_and_load_all_dwo_tus (per_cu);
95554aad 2730 }
9291a0cd 2731
a0f42c21 2732 process_queue ();
9291a0cd
TT
2733
2734 /* Age the cache, releasing compilation units that have not
2735 been used recently. */
2736 age_cached_comp_units ();
2737
2738 do_cleanups (back_to);
2739}
2740
2741/* Ensure that the symbols for PER_CU have been read in. OBJFILE is
2742 the objfile from which this CU came. Returns the resulting symbol
2743 table. */
2fdf6df6 2744
43f3e411 2745static struct compunit_symtab *
a0f42c21 2746dw2_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
9291a0cd 2747{
95554aad 2748 gdb_assert (dwarf2_per_objfile->using_index);
43f3e411 2749 if (!per_cu->v.quick->compunit_symtab)
9291a0cd
TT
2750 {
2751 struct cleanup *back_to = make_cleanup (free_cached_comp_units, NULL);
2752 increment_reading_symtab ();
a0f42c21 2753 dw2_do_instantiate_symtab (per_cu);
95554aad 2754 process_cu_includes ();
9291a0cd
TT
2755 do_cleanups (back_to);
2756 }
f194fefb 2757
43f3e411 2758 return per_cu->v.quick->compunit_symtab;
9291a0cd
TT
2759}
2760
8832e7e3 2761/* Return the CU/TU given its index.
f4dc4d17
DE
2762
2763 This is intended for loops like:
2764
2765 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2766 + dwarf2_per_objfile->n_type_units); ++i)
2767 {
8832e7e3 2768 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
f4dc4d17
DE
2769
2770 ...;
2771 }
2772*/
2fdf6df6 2773
1fd400ff 2774static struct dwarf2_per_cu_data *
8832e7e3 2775dw2_get_cutu (int index)
1fd400ff
TT
2776{
2777 if (index >= dwarf2_per_objfile->n_comp_units)
2778 {
f4dc4d17 2779 index -= dwarf2_per_objfile->n_comp_units;
094b34ac
DE
2780 gdb_assert (index < dwarf2_per_objfile->n_type_units);
2781 return &dwarf2_per_objfile->all_type_units[index]->per_cu;
f4dc4d17
DE
2782 }
2783
2784 return dwarf2_per_objfile->all_comp_units[index];
2785}
2786
8832e7e3
DE
2787/* Return the CU given its index.
2788 This differs from dw2_get_cutu in that it's for when you know INDEX
2789 refers to a CU. */
f4dc4d17
DE
2790
2791static struct dwarf2_per_cu_data *
8832e7e3 2792dw2_get_cu (int index)
f4dc4d17 2793{
8832e7e3 2794 gdb_assert (index >= 0 && index < dwarf2_per_objfile->n_comp_units);
f4dc4d17 2795
1fd400ff
TT
2796 return dwarf2_per_objfile->all_comp_units[index];
2797}
2798
2ec9a5e0
TT
2799/* A helper for create_cus_from_index that handles a given list of
2800 CUs. */
2fdf6df6 2801
74a0d9f6 2802static void
2ec9a5e0
TT
2803create_cus_from_index_list (struct objfile *objfile,
2804 const gdb_byte *cu_list, offset_type n_elements,
2805 struct dwarf2_section_info *section,
2806 int is_dwz,
2807 int base_offset)
9291a0cd
TT
2808{
2809 offset_type i;
9291a0cd 2810
2ec9a5e0 2811 for (i = 0; i < n_elements; i += 2)
9291a0cd
TT
2812 {
2813 struct dwarf2_per_cu_data *the_cu;
2814 ULONGEST offset, length;
2815
74a0d9f6
JK
2816 gdb_static_assert (sizeof (ULONGEST) >= 8);
2817 offset = extract_unsigned_integer (cu_list, 8, BFD_ENDIAN_LITTLE);
2818 length = extract_unsigned_integer (cu_list + 8, 8, BFD_ENDIAN_LITTLE);
9291a0cd
TT
2819 cu_list += 2 * 8;
2820
2821 the_cu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2822 struct dwarf2_per_cu_data);
b64f50a1 2823 the_cu->offset.sect_off = offset;
9291a0cd
TT
2824 the_cu->length = length;
2825 the_cu->objfile = objfile;
8a0459fd 2826 the_cu->section = section;
9291a0cd
TT
2827 the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2828 struct dwarf2_per_cu_quick_data);
2ec9a5e0
TT
2829 the_cu->is_dwz = is_dwz;
2830 dwarf2_per_objfile->all_comp_units[base_offset + i / 2] = the_cu;
9291a0cd 2831 }
9291a0cd
TT
2832}
2833
2ec9a5e0 2834/* Read the CU list from the mapped index, and use it to create all
74a0d9f6 2835 the CU objects for this objfile. */
2ec9a5e0 2836
74a0d9f6 2837static void
2ec9a5e0
TT
2838create_cus_from_index (struct objfile *objfile,
2839 const gdb_byte *cu_list, offset_type cu_list_elements,
2840 const gdb_byte *dwz_list, offset_type dwz_elements)
2841{
2842 struct dwz_file *dwz;
2843
2844 dwarf2_per_objfile->n_comp_units = (cu_list_elements + dwz_elements) / 2;
8d749320
SM
2845 dwarf2_per_objfile->all_comp_units =
2846 XOBNEWVEC (&objfile->objfile_obstack, struct dwarf2_per_cu_data *,
2847 dwarf2_per_objfile->n_comp_units);
2ec9a5e0 2848
74a0d9f6
JK
2849 create_cus_from_index_list (objfile, cu_list, cu_list_elements,
2850 &dwarf2_per_objfile->info, 0, 0);
2ec9a5e0
TT
2851
2852 if (dwz_elements == 0)
74a0d9f6 2853 return;
2ec9a5e0
TT
2854
2855 dwz = dwarf2_get_dwz_file ();
74a0d9f6
JK
2856 create_cus_from_index_list (objfile, dwz_list, dwz_elements, &dwz->info, 1,
2857 cu_list_elements / 2);
2ec9a5e0
TT
2858}
2859
1fd400ff 2860/* Create the signatured type hash table from the index. */
673bfd45 2861
74a0d9f6 2862static void
673bfd45 2863create_signatured_type_table_from_index (struct objfile *objfile,
8b70b953 2864 struct dwarf2_section_info *section,
673bfd45
DE
2865 const gdb_byte *bytes,
2866 offset_type elements)
1fd400ff
TT
2867{
2868 offset_type i;
673bfd45 2869 htab_t sig_types_hash;
1fd400ff 2870
6aa5f3a6
DE
2871 dwarf2_per_objfile->n_type_units
2872 = dwarf2_per_objfile->n_allocated_type_units
2873 = elements / 3;
8d749320
SM
2874 dwarf2_per_objfile->all_type_units =
2875 XNEWVEC (struct signatured_type *, dwarf2_per_objfile->n_type_units);
1fd400ff 2876
673bfd45 2877 sig_types_hash = allocate_signatured_type_table (objfile);
1fd400ff
TT
2878
2879 for (i = 0; i < elements; i += 3)
2880 {
52dc124a
DE
2881 struct signatured_type *sig_type;
2882 ULONGEST offset, type_offset_in_tu, signature;
1fd400ff
TT
2883 void **slot;
2884
74a0d9f6
JK
2885 gdb_static_assert (sizeof (ULONGEST) >= 8);
2886 offset = extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
2887 type_offset_in_tu = extract_unsigned_integer (bytes + 8, 8,
2888 BFD_ENDIAN_LITTLE);
1fd400ff
TT
2889 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
2890 bytes += 3 * 8;
2891
52dc124a 2892 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1fd400ff 2893 struct signatured_type);
52dc124a 2894 sig_type->signature = signature;
3019eac3
DE
2895 sig_type->type_offset_in_tu.cu_off = type_offset_in_tu;
2896 sig_type->per_cu.is_debug_types = 1;
8a0459fd 2897 sig_type->per_cu.section = section;
52dc124a
DE
2898 sig_type->per_cu.offset.sect_off = offset;
2899 sig_type->per_cu.objfile = objfile;
2900 sig_type->per_cu.v.quick
1fd400ff
TT
2901 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2902 struct dwarf2_per_cu_quick_data);
2903
52dc124a
DE
2904 slot = htab_find_slot (sig_types_hash, sig_type, INSERT);
2905 *slot = sig_type;
1fd400ff 2906
b4dd5633 2907 dwarf2_per_objfile->all_type_units[i / 3] = sig_type;
1fd400ff
TT
2908 }
2909
673bfd45 2910 dwarf2_per_objfile->signatured_types = sig_types_hash;
1fd400ff
TT
2911}
2912
9291a0cd
TT
2913/* Read the address map data from the mapped index, and use it to
2914 populate the objfile's psymtabs_addrmap. */
2fdf6df6 2915
9291a0cd
TT
2916static void
2917create_addrmap_from_index (struct objfile *objfile, struct mapped_index *index)
2918{
3e29f34a 2919 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9291a0cd
TT
2920 const gdb_byte *iter, *end;
2921 struct obstack temp_obstack;
2922 struct addrmap *mutable_map;
2923 struct cleanup *cleanup;
2924 CORE_ADDR baseaddr;
2925
2926 obstack_init (&temp_obstack);
2927 cleanup = make_cleanup_obstack_free (&temp_obstack);
2928 mutable_map = addrmap_create_mutable (&temp_obstack);
2929
2930 iter = index->address_table;
2931 end = iter + index->address_table_size;
2932
2933 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
2934
2935 while (iter < end)
2936 {
2937 ULONGEST hi, lo, cu_index;
2938 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2939 iter += 8;
2940 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2941 iter += 8;
2942 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
2943 iter += 4;
f652bce2 2944
24a55014 2945 if (lo > hi)
f652bce2 2946 {
24a55014
DE
2947 complaint (&symfile_complaints,
2948 _(".gdb_index address table has invalid range (%s - %s)"),
c0cd8254 2949 hex_string (lo), hex_string (hi));
24a55014 2950 continue;
f652bce2 2951 }
24a55014
DE
2952
2953 if (cu_index >= dwarf2_per_objfile->n_comp_units)
f652bce2
DE
2954 {
2955 complaint (&symfile_complaints,
2956 _(".gdb_index address table has invalid CU number %u"),
2957 (unsigned) cu_index);
24a55014 2958 continue;
f652bce2 2959 }
24a55014 2960
3e29f34a
MR
2961 lo = gdbarch_adjust_dwarf2_addr (gdbarch, lo + baseaddr);
2962 hi = gdbarch_adjust_dwarf2_addr (gdbarch, hi + baseaddr);
2963 addrmap_set_empty (mutable_map, lo, hi - 1, dw2_get_cutu (cu_index));
9291a0cd
TT
2964 }
2965
2966 objfile->psymtabs_addrmap = addrmap_create_fixed (mutable_map,
2967 &objfile->objfile_obstack);
2968 do_cleanups (cleanup);
2969}
2970
59d7bcaf
JK
2971/* The hash function for strings in the mapped index. This is the same as
2972 SYMBOL_HASH_NEXT, but we keep a separate copy to maintain control over the
2973 implementation. This is necessary because the hash function is tied to the
2974 format of the mapped index file. The hash values do not have to match with
559a7a62
JK
2975 SYMBOL_HASH_NEXT.
2976
2977 Use INT_MAX for INDEX_VERSION if you generate the current index format. */
2fdf6df6 2978
9291a0cd 2979static hashval_t
559a7a62 2980mapped_index_string_hash (int index_version, const void *p)
9291a0cd
TT
2981{
2982 const unsigned char *str = (const unsigned char *) p;
2983 hashval_t r = 0;
2984 unsigned char c;
2985
2986 while ((c = *str++) != 0)
559a7a62
JK
2987 {
2988 if (index_version >= 5)
2989 c = tolower (c);
2990 r = r * 67 + c - 113;
2991 }
9291a0cd
TT
2992
2993 return r;
2994}
2995
2996/* Find a slot in the mapped index INDEX for the object named NAME.
2997 If NAME is found, set *VEC_OUT to point to the CU vector in the
2998 constant pool and return 1. If NAME cannot be found, return 0. */
2fdf6df6 2999
9291a0cd
TT
3000static int
3001find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
3002 offset_type **vec_out)
3003{
0cf03b49
JK
3004 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
3005 offset_type hash;
9291a0cd 3006 offset_type slot, step;
559a7a62 3007 int (*cmp) (const char *, const char *);
9291a0cd 3008
0cf03b49
JK
3009 if (current_language->la_language == language_cplus
3010 || current_language->la_language == language_java
45280282
IB
3011 || current_language->la_language == language_fortran
3012 || current_language->la_language == language_d)
0cf03b49
JK
3013 {
3014 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
3015 not contain any. */
a8719064 3016
72998fb3 3017 if (strchr (name, '(') != NULL)
0cf03b49 3018 {
72998fb3 3019 char *without_params = cp_remove_params (name);
0cf03b49 3020
72998fb3
DE
3021 if (without_params != NULL)
3022 {
3023 make_cleanup (xfree, without_params);
3024 name = without_params;
3025 }
0cf03b49
JK
3026 }
3027 }
3028
559a7a62 3029 /* Index version 4 did not support case insensitive searches. But the
feea76c2 3030 indices for case insensitive languages are built in lowercase, therefore
559a7a62
JK
3031 simulate our NAME being searched is also lowercased. */
3032 hash = mapped_index_string_hash ((index->version == 4
3033 && case_sensitivity == case_sensitive_off
3034 ? 5 : index->version),
3035 name);
3036
3876f04e
DE
3037 slot = hash & (index->symbol_table_slots - 1);
3038 step = ((hash * 17) & (index->symbol_table_slots - 1)) | 1;
559a7a62 3039 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
9291a0cd
TT
3040
3041 for (;;)
3042 {
3043 /* Convert a slot number to an offset into the table. */
3044 offset_type i = 2 * slot;
3045 const char *str;
3876f04e 3046 if (index->symbol_table[i] == 0 && index->symbol_table[i + 1] == 0)
0cf03b49
JK
3047 {
3048 do_cleanups (back_to);
3049 return 0;
3050 }
9291a0cd 3051
3876f04e 3052 str = index->constant_pool + MAYBE_SWAP (index->symbol_table[i]);
559a7a62 3053 if (!cmp (name, str))
9291a0cd
TT
3054 {
3055 *vec_out = (offset_type *) (index->constant_pool
3876f04e 3056 + MAYBE_SWAP (index->symbol_table[i + 1]));
0cf03b49 3057 do_cleanups (back_to);
9291a0cd
TT
3058 return 1;
3059 }
3060
3876f04e 3061 slot = (slot + step) & (index->symbol_table_slots - 1);
9291a0cd
TT
3062 }
3063}
3064
2ec9a5e0
TT
3065/* A helper function that reads the .gdb_index from SECTION and fills
3066 in MAP. FILENAME is the name of the file containing the section;
3067 it is used for error reporting. DEPRECATED_OK is nonzero if it is
3068 ok to use deprecated sections.
3069
3070 CU_LIST, CU_LIST_ELEMENTS, TYPES_LIST, and TYPES_LIST_ELEMENTS are
3071 out parameters that are filled in with information about the CU and
3072 TU lists in the section.
3073
3074 Returns 1 if all went well, 0 otherwise. */
2fdf6df6 3075
9291a0cd 3076static int
2ec9a5e0
TT
3077read_index_from_section (struct objfile *objfile,
3078 const char *filename,
3079 int deprecated_ok,
3080 struct dwarf2_section_info *section,
3081 struct mapped_index *map,
3082 const gdb_byte **cu_list,
3083 offset_type *cu_list_elements,
3084 const gdb_byte **types_list,
3085 offset_type *types_list_elements)
9291a0cd 3086{
948f8e3d 3087 const gdb_byte *addr;
2ec9a5e0 3088 offset_type version;
b3b272e1 3089 offset_type *metadata;
1fd400ff 3090 int i;
9291a0cd 3091
2ec9a5e0 3092 if (dwarf2_section_empty_p (section))
9291a0cd 3093 return 0;
82430852
JK
3094
3095 /* Older elfutils strip versions could keep the section in the main
3096 executable while splitting it for the separate debug info file. */
a32a8923 3097 if ((get_section_flags (section) & SEC_HAS_CONTENTS) == 0)
82430852
JK
3098 return 0;
3099
2ec9a5e0 3100 dwarf2_read_section (objfile, section);
9291a0cd 3101
2ec9a5e0 3102 addr = section->buffer;
9291a0cd 3103 /* Version check. */
1fd400ff 3104 version = MAYBE_SWAP (*(offset_type *) addr);
987d643c 3105 /* Versions earlier than 3 emitted every copy of a psymbol. This
a6e293d1 3106 causes the index to behave very poorly for certain requests. Version 3
831adc1f 3107 contained incomplete addrmap. So, it seems better to just ignore such
481860b3 3108 indices. */
831adc1f 3109 if (version < 4)
481860b3
GB
3110 {
3111 static int warning_printed = 0;
3112 if (!warning_printed)
3113 {
3114 warning (_("Skipping obsolete .gdb_index section in %s."),
2ec9a5e0 3115 filename);
481860b3
GB
3116 warning_printed = 1;
3117 }
3118 return 0;
3119 }
3120 /* Index version 4 uses a different hash function than index version
3121 5 and later.
3122
3123 Versions earlier than 6 did not emit psymbols for inlined
3124 functions. Using these files will cause GDB not to be able to
3125 set breakpoints on inlined functions by name, so we ignore these
e615022a
DE
3126 indices unless the user has done
3127 "set use-deprecated-index-sections on". */
2ec9a5e0 3128 if (version < 6 && !deprecated_ok)
481860b3
GB
3129 {
3130 static int warning_printed = 0;
3131 if (!warning_printed)
3132 {
e615022a
DE
3133 warning (_("\
3134Skipping deprecated .gdb_index section in %s.\n\
3135Do \"set use-deprecated-index-sections on\" before the file is read\n\
3136to use the section anyway."),
2ec9a5e0 3137 filename);
481860b3
GB
3138 warning_printed = 1;
3139 }
3140 return 0;
3141 }
796a7ff8 3142 /* Version 7 indices generated by gold refer to the CU for a symbol instead
8943b874
DE
3143 of the TU (for symbols coming from TUs),
3144 http://sourceware.org/bugzilla/show_bug.cgi?id=15021.
3145 Plus gold-generated indices can have duplicate entries for global symbols,
3146 http://sourceware.org/bugzilla/show_bug.cgi?id=15646.
3147 These are just performance bugs, and we can't distinguish gdb-generated
3148 indices from gold-generated ones, so issue no warning here. */
796a7ff8 3149
481860b3 3150 /* Indexes with higher version than the one supported by GDB may be no
594e8718 3151 longer backward compatible. */
796a7ff8 3152 if (version > 8)
594e8718 3153 return 0;
9291a0cd 3154
559a7a62 3155 map->version = version;
2ec9a5e0 3156 map->total_size = section->size;
9291a0cd
TT
3157
3158 metadata = (offset_type *) (addr + sizeof (offset_type));
1fd400ff
TT
3159
3160 i = 0;
2ec9a5e0
TT
3161 *cu_list = addr + MAYBE_SWAP (metadata[i]);
3162 *cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
3163 / 8);
1fd400ff
TT
3164 ++i;
3165
2ec9a5e0
TT
3166 *types_list = addr + MAYBE_SWAP (metadata[i]);
3167 *types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
3168 - MAYBE_SWAP (metadata[i]))
3169 / 8);
987d643c 3170 ++i;
1fd400ff
TT
3171
3172 map->address_table = addr + MAYBE_SWAP (metadata[i]);
3173 map->address_table_size = (MAYBE_SWAP (metadata[i + 1])
3174 - MAYBE_SWAP (metadata[i]));
3175 ++i;
3176
3876f04e
DE
3177 map->symbol_table = (offset_type *) (addr + MAYBE_SWAP (metadata[i]));
3178 map->symbol_table_slots = ((MAYBE_SWAP (metadata[i + 1])
3179 - MAYBE_SWAP (metadata[i]))
3180 / (2 * sizeof (offset_type)));
1fd400ff 3181 ++i;
9291a0cd 3182
f9d83a0b 3183 map->constant_pool = (char *) (addr + MAYBE_SWAP (metadata[i]));
1fd400ff 3184
2ec9a5e0
TT
3185 return 1;
3186}
3187
3188
3189/* Read the index file. If everything went ok, initialize the "quick"
3190 elements of all the CUs and return 1. Otherwise, return 0. */
3191
3192static int
3193dwarf2_read_index (struct objfile *objfile)
3194{
3195 struct mapped_index local_map, *map;
3196 const gdb_byte *cu_list, *types_list, *dwz_list = NULL;
3197 offset_type cu_list_elements, types_list_elements, dwz_list_elements = 0;
4db1a1dc 3198 struct dwz_file *dwz;
2ec9a5e0 3199
4262abfb 3200 if (!read_index_from_section (objfile, objfile_name (objfile),
2ec9a5e0
TT
3201 use_deprecated_index_sections,
3202 &dwarf2_per_objfile->gdb_index, &local_map,
3203 &cu_list, &cu_list_elements,
3204 &types_list, &types_list_elements))
3205 return 0;
3206
0fefef59 3207 /* Don't use the index if it's empty. */
2ec9a5e0 3208 if (local_map.symbol_table_slots == 0)
0fefef59
DE
3209 return 0;
3210
2ec9a5e0
TT
3211 /* If there is a .dwz file, read it so we can get its CU list as
3212 well. */
4db1a1dc
TT
3213 dwz = dwarf2_get_dwz_file ();
3214 if (dwz != NULL)
2ec9a5e0 3215 {
2ec9a5e0
TT
3216 struct mapped_index dwz_map;
3217 const gdb_byte *dwz_types_ignore;
3218 offset_type dwz_types_elements_ignore;
3219
3220 if (!read_index_from_section (objfile, bfd_get_filename (dwz->dwz_bfd),
3221 1,
3222 &dwz->gdb_index, &dwz_map,
3223 &dwz_list, &dwz_list_elements,
3224 &dwz_types_ignore,
3225 &dwz_types_elements_ignore))
3226 {
3227 warning (_("could not read '.gdb_index' section from %s; skipping"),
3228 bfd_get_filename (dwz->dwz_bfd));
3229 return 0;
3230 }
3231 }
3232
74a0d9f6
JK
3233 create_cus_from_index (objfile, cu_list, cu_list_elements, dwz_list,
3234 dwz_list_elements);
1fd400ff 3235
8b70b953
TT
3236 if (types_list_elements)
3237 {
3238 struct dwarf2_section_info *section;
3239
3240 /* We can only handle a single .debug_types when we have an
3241 index. */
3242 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) != 1)
3243 return 0;
3244
3245 section = VEC_index (dwarf2_section_info_def,
3246 dwarf2_per_objfile->types, 0);
3247
74a0d9f6
JK
3248 create_signatured_type_table_from_index (objfile, section, types_list,
3249 types_list_elements);
8b70b953 3250 }
9291a0cd 3251
2ec9a5e0
TT
3252 create_addrmap_from_index (objfile, &local_map);
3253
8d749320 3254 map = XOBNEW (&objfile->objfile_obstack, struct mapped_index);
2ec9a5e0 3255 *map = local_map;
9291a0cd
TT
3256
3257 dwarf2_per_objfile->index_table = map;
3258 dwarf2_per_objfile->using_index = 1;
7b9f3c50
DE
3259 dwarf2_per_objfile->quick_file_names_table =
3260 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
9291a0cd
TT
3261
3262 return 1;
3263}
3264
3265/* A helper for the "quick" functions which sets the global
3266 dwarf2_per_objfile according to OBJFILE. */
2fdf6df6 3267
9291a0cd
TT
3268static void
3269dw2_setup (struct objfile *objfile)
3270{
9a3c8263
SM
3271 dwarf2_per_objfile = ((struct dwarf2_per_objfile *)
3272 objfile_data (objfile, dwarf2_objfile_data_key));
9291a0cd
TT
3273 gdb_assert (dwarf2_per_objfile);
3274}
3275
dee91e82 3276/* die_reader_func for dw2_get_file_names. */
2fdf6df6 3277
dee91e82
DE
3278static void
3279dw2_get_file_names_reader (const struct die_reader_specs *reader,
d521ce57 3280 const gdb_byte *info_ptr,
dee91e82
DE
3281 struct die_info *comp_unit_die,
3282 int has_children,
3283 void *data)
9291a0cd 3284{
dee91e82
DE
3285 struct dwarf2_cu *cu = reader->cu;
3286 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
3287 struct objfile *objfile = dwarf2_per_objfile->objfile;
094b34ac 3288 struct dwarf2_per_cu_data *lh_cu;
7b9f3c50 3289 struct line_header *lh;
9291a0cd 3290 struct attribute *attr;
dee91e82 3291 int i;
15d034d0 3292 const char *name, *comp_dir;
7b9f3c50
DE
3293 void **slot;
3294 struct quick_file_names *qfn;
3295 unsigned int line_offset;
9291a0cd 3296
0186c6a7
DE
3297 gdb_assert (! this_cu->is_debug_types);
3298
07261596
TT
3299 /* Our callers never want to match partial units -- instead they
3300 will match the enclosing full CU. */
3301 if (comp_unit_die->tag == DW_TAG_partial_unit)
3302 {
3303 this_cu->v.quick->no_file_data = 1;
3304 return;
3305 }
3306
0186c6a7 3307 lh_cu = this_cu;
7b9f3c50
DE
3308 lh = NULL;
3309 slot = NULL;
3310 line_offset = 0;
dee91e82
DE
3311
3312 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
9291a0cd
TT
3313 if (attr)
3314 {
7b9f3c50
DE
3315 struct quick_file_names find_entry;
3316
3317 line_offset = DW_UNSND (attr);
3318
3319 /* We may have already read in this line header (TU line header sharing).
3320 If we have we're done. */
094b34ac
DE
3321 find_entry.hash.dwo_unit = cu->dwo_unit;
3322 find_entry.hash.line_offset.sect_off = line_offset;
7b9f3c50
DE
3323 slot = htab_find_slot (dwarf2_per_objfile->quick_file_names_table,
3324 &find_entry, INSERT);
3325 if (*slot != NULL)
3326 {
9a3c8263 3327 lh_cu->v.quick->file_names = (struct quick_file_names *) *slot;
dee91e82 3328 return;
7b9f3c50
DE
3329 }
3330
3019eac3 3331 lh = dwarf_decode_line_header (line_offset, cu);
9291a0cd
TT
3332 }
3333 if (lh == NULL)
3334 {
094b34ac 3335 lh_cu->v.quick->no_file_data = 1;
dee91e82 3336 return;
9291a0cd
TT
3337 }
3338
8d749320 3339 qfn = XOBNEW (&objfile->objfile_obstack, struct quick_file_names);
094b34ac
DE
3340 qfn->hash.dwo_unit = cu->dwo_unit;
3341 qfn->hash.line_offset.sect_off = line_offset;
7b9f3c50
DE
3342 gdb_assert (slot != NULL);
3343 *slot = qfn;
9291a0cd 3344
dee91e82 3345 find_file_and_directory (comp_unit_die, cu, &name, &comp_dir);
9291a0cd 3346
7b9f3c50 3347 qfn->num_file_names = lh->num_file_names;
8d749320
SM
3348 qfn->file_names =
3349 XOBNEWVEC (&objfile->objfile_obstack, const char *, lh->num_file_names);
9291a0cd 3350 for (i = 0; i < lh->num_file_names; ++i)
7b9f3c50
DE
3351 qfn->file_names[i] = file_full_name (i + 1, lh, comp_dir);
3352 qfn->real_names = NULL;
9291a0cd 3353
7b9f3c50 3354 free_line_header (lh);
7b9f3c50 3355
094b34ac 3356 lh_cu->v.quick->file_names = qfn;
dee91e82
DE
3357}
3358
3359/* A helper for the "quick" functions which attempts to read the line
3360 table for THIS_CU. */
3361
3362static struct quick_file_names *
e4a48d9d 3363dw2_get_file_names (struct dwarf2_per_cu_data *this_cu)
dee91e82 3364{
0186c6a7
DE
3365 /* This should never be called for TUs. */
3366 gdb_assert (! this_cu->is_debug_types);
3367 /* Nor type unit groups. */
3368 gdb_assert (! IS_TYPE_UNIT_GROUP (this_cu));
f4dc4d17 3369
dee91e82
DE
3370 if (this_cu->v.quick->file_names != NULL)
3371 return this_cu->v.quick->file_names;
3372 /* If we know there is no line data, no point in looking again. */
3373 if (this_cu->v.quick->no_file_data)
3374 return NULL;
3375
0186c6a7 3376 init_cutu_and_read_dies_simple (this_cu, dw2_get_file_names_reader, NULL);
dee91e82
DE
3377
3378 if (this_cu->v.quick->no_file_data)
3379 return NULL;
3380 return this_cu->v.quick->file_names;
9291a0cd
TT
3381}
3382
3383/* A helper for the "quick" functions which computes and caches the
7b9f3c50 3384 real path for a given file name from the line table. */
2fdf6df6 3385
9291a0cd 3386static const char *
7b9f3c50
DE
3387dw2_get_real_path (struct objfile *objfile,
3388 struct quick_file_names *qfn, int index)
9291a0cd 3389{
7b9f3c50
DE
3390 if (qfn->real_names == NULL)
3391 qfn->real_names = OBSTACK_CALLOC (&objfile->objfile_obstack,
26f2dc30 3392 qfn->num_file_names, const char *);
9291a0cd 3393
7b9f3c50
DE
3394 if (qfn->real_names[index] == NULL)
3395 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]);
9291a0cd 3396
7b9f3c50 3397 return qfn->real_names[index];
9291a0cd
TT
3398}
3399
3400static struct symtab *
3401dw2_find_last_source_symtab (struct objfile *objfile)
3402{
43f3e411 3403 struct compunit_symtab *cust;
9291a0cd 3404 int index;
ae2de4f8 3405
9291a0cd
TT
3406 dw2_setup (objfile);
3407 index = dwarf2_per_objfile->n_comp_units - 1;
43f3e411
DE
3408 cust = dw2_instantiate_symtab (dw2_get_cutu (index));
3409 if (cust == NULL)
3410 return NULL;
3411 return compunit_primary_filetab (cust);
9291a0cd
TT
3412}
3413
7b9f3c50
DE
3414/* Traversal function for dw2_forget_cached_source_info. */
3415
3416static int
3417dw2_free_cached_file_names (void **slot, void *info)
9291a0cd 3418{
7b9f3c50 3419 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
9291a0cd 3420
7b9f3c50 3421 if (file_data->real_names)
9291a0cd 3422 {
7b9f3c50 3423 int i;
9291a0cd 3424
7b9f3c50 3425 for (i = 0; i < file_data->num_file_names; ++i)
9291a0cd 3426 {
7b9f3c50
DE
3427 xfree ((void*) file_data->real_names[i]);
3428 file_data->real_names[i] = NULL;
9291a0cd
TT
3429 }
3430 }
7b9f3c50
DE
3431
3432 return 1;
3433}
3434
3435static void
3436dw2_forget_cached_source_info (struct objfile *objfile)
3437{
3438 dw2_setup (objfile);
3439
3440 htab_traverse_noresize (dwarf2_per_objfile->quick_file_names_table,
3441 dw2_free_cached_file_names, NULL);
9291a0cd
TT
3442}
3443
f8eba3c6
TT
3444/* Helper function for dw2_map_symtabs_matching_filename that expands
3445 the symtabs and calls the iterator. */
3446
3447static int
3448dw2_map_expand_apply (struct objfile *objfile,
3449 struct dwarf2_per_cu_data *per_cu,
f5b95b50 3450 const char *name, const char *real_path,
f8eba3c6
TT
3451 int (*callback) (struct symtab *, void *),
3452 void *data)
3453{
43f3e411 3454 struct compunit_symtab *last_made = objfile->compunit_symtabs;
f8eba3c6
TT
3455
3456 /* Don't visit already-expanded CUs. */
43f3e411 3457 if (per_cu->v.quick->compunit_symtab)
f8eba3c6
TT
3458 return 0;
3459
3460 /* This may expand more than one symtab, and we want to iterate over
3461 all of them. */
a0f42c21 3462 dw2_instantiate_symtab (per_cu);
f8eba3c6 3463
f5b95b50 3464 return iterate_over_some_symtabs (name, real_path, callback, data,
43f3e411 3465 objfile->compunit_symtabs, last_made);
f8eba3c6
TT
3466}
3467
3468/* Implementation of the map_symtabs_matching_filename method. */
3469
9291a0cd 3470static int
f8eba3c6 3471dw2_map_symtabs_matching_filename (struct objfile *objfile, const char *name,
f5b95b50 3472 const char *real_path,
f8eba3c6
TT
3473 int (*callback) (struct symtab *, void *),
3474 void *data)
9291a0cd
TT
3475{
3476 int i;
c011a4f4 3477 const char *name_basename = lbasename (name);
9291a0cd
TT
3478
3479 dw2_setup (objfile);
ae2de4f8 3480
848e3e78
DE
3481 /* The rule is CUs specify all the files, including those used by
3482 any TU, so there's no need to scan TUs here. */
f4dc4d17 3483
848e3e78 3484 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
9291a0cd
TT
3485 {
3486 int j;
8832e7e3 3487 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
7b9f3c50 3488 struct quick_file_names *file_data;
9291a0cd 3489
3d7bb9d9 3490 /* We only need to look at symtabs not already expanded. */
43f3e411 3491 if (per_cu->v.quick->compunit_symtab)
9291a0cd
TT
3492 continue;
3493
e4a48d9d 3494 file_data = dw2_get_file_names (per_cu);
7b9f3c50 3495 if (file_data == NULL)
9291a0cd
TT
3496 continue;
3497
7b9f3c50 3498 for (j = 0; j < file_data->num_file_names; ++j)
9291a0cd 3499 {
7b9f3c50 3500 const char *this_name = file_data->file_names[j];
da235a7c 3501 const char *this_real_name;
9291a0cd 3502
af529f8f 3503 if (compare_filenames_for_search (this_name, name))
9291a0cd 3504 {
f5b95b50 3505 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
f8eba3c6
TT
3506 callback, data))
3507 return 1;
288e77a7 3508 continue;
4aac40c8 3509 }
9291a0cd 3510
c011a4f4
DE
3511 /* Before we invoke realpath, which can get expensive when many
3512 files are involved, do a quick comparison of the basenames. */
3513 if (! basenames_may_differ
3514 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
3515 continue;
3516
da235a7c
JK
3517 this_real_name = dw2_get_real_path (objfile, file_data, j);
3518 if (compare_filenames_for_search (this_real_name, name))
9291a0cd 3519 {
da235a7c
JK
3520 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3521 callback, data))
3522 return 1;
288e77a7 3523 continue;
da235a7c 3524 }
9291a0cd 3525
da235a7c
JK
3526 if (real_path != NULL)
3527 {
af529f8f
JK
3528 gdb_assert (IS_ABSOLUTE_PATH (real_path));
3529 gdb_assert (IS_ABSOLUTE_PATH (name));
7b9f3c50 3530 if (this_real_name != NULL
af529f8f 3531 && FILENAME_CMP (real_path, this_real_name) == 0)
9291a0cd 3532 {
f5b95b50 3533 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
f8eba3c6
TT
3534 callback, data))
3535 return 1;
288e77a7 3536 continue;
9291a0cd
TT
3537 }
3538 }
3539 }
3540 }
3541
9291a0cd
TT
3542 return 0;
3543}
3544
da51c347
DE
3545/* Struct used to manage iterating over all CUs looking for a symbol. */
3546
3547struct dw2_symtab_iterator
9291a0cd 3548{
da51c347
DE
3549 /* The internalized form of .gdb_index. */
3550 struct mapped_index *index;
3551 /* If non-zero, only look for symbols that match BLOCK_INDEX. */
3552 int want_specific_block;
3553 /* One of GLOBAL_BLOCK or STATIC_BLOCK.
3554 Unused if !WANT_SPECIFIC_BLOCK. */
3555 int block_index;
3556 /* The kind of symbol we're looking for. */
3557 domain_enum domain;
3558 /* The list of CUs from the index entry of the symbol,
3559 or NULL if not found. */
3560 offset_type *vec;
3561 /* The next element in VEC to look at. */
3562 int next;
3563 /* The number of elements in VEC, or zero if there is no match. */
3564 int length;
8943b874
DE
3565 /* Have we seen a global version of the symbol?
3566 If so we can ignore all further global instances.
3567 This is to work around gold/15646, inefficient gold-generated
3568 indices. */
3569 int global_seen;
da51c347 3570};
9291a0cd 3571
da51c347
DE
3572/* Initialize the index symtab iterator ITER.
3573 If WANT_SPECIFIC_BLOCK is non-zero, only look for symbols
3574 in block BLOCK_INDEX. Otherwise BLOCK_INDEX is ignored. */
2fdf6df6 3575
9291a0cd 3576static void
da51c347
DE
3577dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
3578 struct mapped_index *index,
3579 int want_specific_block,
3580 int block_index,
3581 domain_enum domain,
3582 const char *name)
3583{
3584 iter->index = index;
3585 iter->want_specific_block = want_specific_block;
3586 iter->block_index = block_index;
3587 iter->domain = domain;
3588 iter->next = 0;
8943b874 3589 iter->global_seen = 0;
da51c347
DE
3590
3591 if (find_slot_in_mapped_hash (index, name, &iter->vec))
3592 iter->length = MAYBE_SWAP (*iter->vec);
3593 else
3594 {
3595 iter->vec = NULL;
3596 iter->length = 0;
3597 }
3598}
3599
3600/* Return the next matching CU or NULL if there are no more. */
3601
3602static struct dwarf2_per_cu_data *
3603dw2_symtab_iter_next (struct dw2_symtab_iterator *iter)
3604{
3605 for ( ; iter->next < iter->length; ++iter->next)
3606 {
3607 offset_type cu_index_and_attrs =
3608 MAYBE_SWAP (iter->vec[iter->next + 1]);
3609 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
3190f0c6 3610 struct dwarf2_per_cu_data *per_cu;
da51c347
DE
3611 int want_static = iter->block_index != GLOBAL_BLOCK;
3612 /* This value is only valid for index versions >= 7. */
3613 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
3614 gdb_index_symbol_kind symbol_kind =
3615 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3616 /* Only check the symbol attributes if they're present.
3617 Indices prior to version 7 don't record them,
3618 and indices >= 7 may elide them for certain symbols
3619 (gold does this). */
3620 int attrs_valid =
3621 (iter->index->version >= 7
3622 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
3623
3190f0c6
DE
3624 /* Don't crash on bad data. */
3625 if (cu_index >= (dwarf2_per_objfile->n_comp_units
3626 + dwarf2_per_objfile->n_type_units))
3627 {
3628 complaint (&symfile_complaints,
3629 _(".gdb_index entry has bad CU index"
4262abfb
JK
3630 " [in module %s]"),
3631 objfile_name (dwarf2_per_objfile->objfile));
3190f0c6
DE
3632 continue;
3633 }
3634
8832e7e3 3635 per_cu = dw2_get_cutu (cu_index);
3190f0c6 3636
da51c347 3637 /* Skip if already read in. */
43f3e411 3638 if (per_cu->v.quick->compunit_symtab)
da51c347
DE
3639 continue;
3640
8943b874
DE
3641 /* Check static vs global. */
3642 if (attrs_valid)
3643 {
3644 if (iter->want_specific_block
3645 && want_static != is_static)
3646 continue;
3647 /* Work around gold/15646. */
3648 if (!is_static && iter->global_seen)
3649 continue;
3650 if (!is_static)
3651 iter->global_seen = 1;
3652 }
da51c347
DE
3653
3654 /* Only check the symbol's kind if it has one. */
3655 if (attrs_valid)
3656 {
3657 switch (iter->domain)
3658 {
3659 case VAR_DOMAIN:
3660 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
3661 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
3662 /* Some types are also in VAR_DOMAIN. */
3663 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3664 continue;
3665 break;
3666 case STRUCT_DOMAIN:
3667 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3668 continue;
3669 break;
3670 case LABEL_DOMAIN:
3671 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3672 continue;
3673 break;
3674 default:
3675 break;
3676 }
3677 }
3678
3679 ++iter->next;
3680 return per_cu;
3681 }
3682
3683 return NULL;
3684}
3685
43f3e411 3686static struct compunit_symtab *
da51c347
DE
3687dw2_lookup_symbol (struct objfile *objfile, int block_index,
3688 const char *name, domain_enum domain)
9291a0cd 3689{
43f3e411 3690 struct compunit_symtab *stab_best = NULL;
156942c7
DE
3691 struct mapped_index *index;
3692
9291a0cd
TT
3693 dw2_setup (objfile);
3694
156942c7
DE
3695 index = dwarf2_per_objfile->index_table;
3696
da51c347 3697 /* index is NULL if OBJF_READNOW. */
156942c7 3698 if (index)
9291a0cd 3699 {
da51c347
DE
3700 struct dw2_symtab_iterator iter;
3701 struct dwarf2_per_cu_data *per_cu;
3702
3703 dw2_symtab_iter_init (&iter, index, 1, block_index, domain, name);
9291a0cd 3704
da51c347 3705 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
9291a0cd 3706 {
b2e2f908 3707 struct symbol *sym, *with_opaque = NULL;
43f3e411
DE
3708 struct compunit_symtab *stab = dw2_instantiate_symtab (per_cu);
3709 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
f194fefb 3710 struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
da51c347 3711
b2e2f908
DE
3712 sym = block_find_symbol (block, name, domain,
3713 block_find_non_opaque_type_preferred,
3714 &with_opaque);
3715
da51c347
DE
3716 /* Some caution must be observed with overloaded functions
3717 and methods, since the index will not contain any overload
3718 information (but NAME might contain it). */
da51c347 3719
b2e2f908
DE
3720 if (sym != NULL
3721 && strcmp_iw (SYMBOL_SEARCH_NAME (sym), name) == 0)
3722 return stab;
3723 if (with_opaque != NULL
3724 && strcmp_iw (SYMBOL_SEARCH_NAME (with_opaque), name) == 0)
3725 stab_best = stab;
da51c347
DE
3726
3727 /* Keep looking through other CUs. */
9291a0cd
TT
3728 }
3729 }
9291a0cd 3730
da51c347 3731 return stab_best;
9291a0cd
TT
3732}
3733
3734static void
3735dw2_print_stats (struct objfile *objfile)
3736{
e4a48d9d 3737 int i, total, count;
9291a0cd
TT
3738
3739 dw2_setup (objfile);
e4a48d9d 3740 total = dwarf2_per_objfile->n_comp_units + dwarf2_per_objfile->n_type_units;
9291a0cd 3741 count = 0;
e4a48d9d 3742 for (i = 0; i < total; ++i)
9291a0cd 3743 {
8832e7e3 3744 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
9291a0cd 3745
43f3e411 3746 if (!per_cu->v.quick->compunit_symtab)
9291a0cd
TT
3747 ++count;
3748 }
e4a48d9d 3749 printf_filtered (_(" Number of read CUs: %d\n"), total - count);
9291a0cd
TT
3750 printf_filtered (_(" Number of unread CUs: %d\n"), count);
3751}
3752
779bd270
DE
3753/* This dumps minimal information about the index.
3754 It is called via "mt print objfiles".
3755 One use is to verify .gdb_index has been loaded by the
3756 gdb.dwarf2/gdb-index.exp testcase. */
3757
9291a0cd
TT
3758static void
3759dw2_dump (struct objfile *objfile)
3760{
779bd270
DE
3761 dw2_setup (objfile);
3762 gdb_assert (dwarf2_per_objfile->using_index);
3763 printf_filtered (".gdb_index:");
3764 if (dwarf2_per_objfile->index_table != NULL)
3765 {
3766 printf_filtered (" version %d\n",
3767 dwarf2_per_objfile->index_table->version);
3768 }
3769 else
3770 printf_filtered (" faked for \"readnow\"\n");
3771 printf_filtered ("\n");
9291a0cd
TT
3772}
3773
3774static void
3189cb12
DE
3775dw2_relocate (struct objfile *objfile,
3776 const struct section_offsets *new_offsets,
3777 const struct section_offsets *delta)
9291a0cd
TT
3778{
3779 /* There's nothing to relocate here. */
3780}
3781
3782static void
3783dw2_expand_symtabs_for_function (struct objfile *objfile,
3784 const char *func_name)
3785{
da51c347
DE
3786 struct mapped_index *index;
3787
3788 dw2_setup (objfile);
3789
3790 index = dwarf2_per_objfile->index_table;
3791
3792 /* index is NULL if OBJF_READNOW. */
3793 if (index)
3794 {
3795 struct dw2_symtab_iterator iter;
3796 struct dwarf2_per_cu_data *per_cu;
3797
3798 /* Note: It doesn't matter what we pass for block_index here. */
3799 dw2_symtab_iter_init (&iter, index, 0, GLOBAL_BLOCK, VAR_DOMAIN,
3800 func_name);
3801
3802 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3803 dw2_instantiate_symtab (per_cu);
3804 }
9291a0cd
TT
3805}
3806
3807static void
3808dw2_expand_all_symtabs (struct objfile *objfile)
3809{
3810 int i;
3811
3812 dw2_setup (objfile);
1fd400ff
TT
3813
3814 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
d467dd73 3815 + dwarf2_per_objfile->n_type_units); ++i)
9291a0cd 3816 {
8832e7e3 3817 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
9291a0cd 3818
a0f42c21 3819 dw2_instantiate_symtab (per_cu);
9291a0cd
TT
3820 }
3821}
3822
3823static void
652a8996
JK
3824dw2_expand_symtabs_with_fullname (struct objfile *objfile,
3825 const char *fullname)
9291a0cd
TT
3826{
3827 int i;
3828
3829 dw2_setup (objfile);
d4637a04
DE
3830
3831 /* We don't need to consider type units here.
3832 This is only called for examining code, e.g. expand_line_sal.
3833 There can be an order of magnitude (or more) more type units
3834 than comp units, and we avoid them if we can. */
3835
3836 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
9291a0cd
TT
3837 {
3838 int j;
8832e7e3 3839 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
7b9f3c50 3840 struct quick_file_names *file_data;
9291a0cd 3841
3d7bb9d9 3842 /* We only need to look at symtabs not already expanded. */
43f3e411 3843 if (per_cu->v.quick->compunit_symtab)
9291a0cd
TT
3844 continue;
3845
e4a48d9d 3846 file_data = dw2_get_file_names (per_cu);
7b9f3c50 3847 if (file_data == NULL)
9291a0cd
TT
3848 continue;
3849
7b9f3c50 3850 for (j = 0; j < file_data->num_file_names; ++j)
9291a0cd 3851 {
652a8996
JK
3852 const char *this_fullname = file_data->file_names[j];
3853
3854 if (filename_cmp (this_fullname, fullname) == 0)
9291a0cd 3855 {
a0f42c21 3856 dw2_instantiate_symtab (per_cu);
9291a0cd
TT
3857 break;
3858 }
3859 }
3860 }
3861}
3862
9291a0cd 3863static void
ade7ed9e 3864dw2_map_matching_symbols (struct objfile *objfile,
fe978cb0 3865 const char * name, domain_enum domain,
ade7ed9e 3866 int global,
40658b94
PH
3867 int (*callback) (struct block *,
3868 struct symbol *, void *),
2edb89d3
JK
3869 void *data, symbol_compare_ftype *match,
3870 symbol_compare_ftype *ordered_compare)
9291a0cd 3871{
40658b94 3872 /* Currently unimplemented; used for Ada. The function can be called if the
a9e6a4bb
JK
3873 current language is Ada for a non-Ada objfile using GNU index. As Ada
3874 does not look for non-Ada symbols this function should just return. */
9291a0cd
TT
3875}
3876
3877static void
f8eba3c6
TT
3878dw2_expand_symtabs_matching
3879 (struct objfile *objfile,
206f2a57
DE
3880 expand_symtabs_file_matcher_ftype *file_matcher,
3881 expand_symtabs_symbol_matcher_ftype *symbol_matcher,
276d885b 3882 expand_symtabs_exp_notify_ftype *expansion_notify,
f8eba3c6
TT
3883 enum search_domain kind,
3884 void *data)
9291a0cd
TT
3885{
3886 int i;
3887 offset_type iter;
4b5246aa 3888 struct mapped_index *index;
9291a0cd
TT
3889
3890 dw2_setup (objfile);
ae2de4f8
DE
3891
3892 /* index_table is NULL if OBJF_READNOW. */
9291a0cd
TT
3893 if (!dwarf2_per_objfile->index_table)
3894 return;
4b5246aa 3895 index = dwarf2_per_objfile->index_table;
9291a0cd 3896
7b08b9eb 3897 if (file_matcher != NULL)
24c79950
TT
3898 {
3899 struct cleanup *cleanup;
3900 htab_t visited_found, visited_not_found;
3901
3902 visited_found = htab_create_alloc (10,
3903 htab_hash_pointer, htab_eq_pointer,
3904 NULL, xcalloc, xfree);
3905 cleanup = make_cleanup_htab_delete (visited_found);
3906 visited_not_found = htab_create_alloc (10,
3907 htab_hash_pointer, htab_eq_pointer,
3908 NULL, xcalloc, xfree);
3909 make_cleanup_htab_delete (visited_not_found);
3910
848e3e78
DE
3911 /* The rule is CUs specify all the files, including those used by
3912 any TU, so there's no need to scan TUs here. */
3913
3914 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
24c79950
TT
3915 {
3916 int j;
8832e7e3 3917 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
24c79950
TT
3918 struct quick_file_names *file_data;
3919 void **slot;
7b08b9eb 3920
61d96d7e
DE
3921 QUIT;
3922
24c79950 3923 per_cu->v.quick->mark = 0;
3d7bb9d9 3924
24c79950 3925 /* We only need to look at symtabs not already expanded. */
43f3e411 3926 if (per_cu->v.quick->compunit_symtab)
24c79950 3927 continue;
7b08b9eb 3928
e4a48d9d 3929 file_data = dw2_get_file_names (per_cu);
24c79950
TT
3930 if (file_data == NULL)
3931 continue;
7b08b9eb 3932
24c79950
TT
3933 if (htab_find (visited_not_found, file_data) != NULL)
3934 continue;
3935 else if (htab_find (visited_found, file_data) != NULL)
3936 {
3937 per_cu->v.quick->mark = 1;
3938 continue;
3939 }
3940
3941 for (j = 0; j < file_data->num_file_names; ++j)
3942 {
da235a7c
JK
3943 const char *this_real_name;
3944
fbd9ab74 3945 if (file_matcher (file_data->file_names[j], data, 0))
24c79950
TT
3946 {
3947 per_cu->v.quick->mark = 1;
3948 break;
3949 }
da235a7c
JK
3950
3951 /* Before we invoke realpath, which can get expensive when many
3952 files are involved, do a quick comparison of the basenames. */
3953 if (!basenames_may_differ
3954 && !file_matcher (lbasename (file_data->file_names[j]),
3955 data, 1))
3956 continue;
3957
3958 this_real_name = dw2_get_real_path (objfile, file_data, j);
3959 if (file_matcher (this_real_name, data, 0))
3960 {
3961 per_cu->v.quick->mark = 1;
3962 break;
3963 }
24c79950
TT
3964 }
3965
3966 slot = htab_find_slot (per_cu->v.quick->mark
3967 ? visited_found
3968 : visited_not_found,
3969 file_data, INSERT);
3970 *slot = file_data;
3971 }
3972
3973 do_cleanups (cleanup);
3974 }
9291a0cd 3975
3876f04e 3976 for (iter = 0; iter < index->symbol_table_slots; ++iter)
9291a0cd
TT
3977 {
3978 offset_type idx = 2 * iter;
3979 const char *name;
3980 offset_type *vec, vec_len, vec_idx;
8943b874 3981 int global_seen = 0;
9291a0cd 3982
61d96d7e
DE
3983 QUIT;
3984
3876f04e 3985 if (index->symbol_table[idx] == 0 && index->symbol_table[idx + 1] == 0)
9291a0cd
TT
3986 continue;
3987
3876f04e 3988 name = index->constant_pool + MAYBE_SWAP (index->symbol_table[idx]);
9291a0cd 3989
206f2a57 3990 if (! (*symbol_matcher) (name, data))
9291a0cd
TT
3991 continue;
3992
3993 /* The name was matched, now expand corresponding CUs that were
3994 marked. */
4b5246aa 3995 vec = (offset_type *) (index->constant_pool
3876f04e 3996 + MAYBE_SWAP (index->symbol_table[idx + 1]));
9291a0cd
TT
3997 vec_len = MAYBE_SWAP (vec[0]);
3998 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
3999 {
e254ef6a 4000 struct dwarf2_per_cu_data *per_cu;
156942c7 4001 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[vec_idx + 1]);
8943b874
DE
4002 /* This value is only valid for index versions >= 7. */
4003 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
156942c7
DE
4004 gdb_index_symbol_kind symbol_kind =
4005 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
4006 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
3190f0c6
DE
4007 /* Only check the symbol attributes if they're present.
4008 Indices prior to version 7 don't record them,
4009 and indices >= 7 may elide them for certain symbols
4010 (gold does this). */
4011 int attrs_valid =
4012 (index->version >= 7
4013 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
4014
8943b874
DE
4015 /* Work around gold/15646. */
4016 if (attrs_valid)
4017 {
4018 if (!is_static && global_seen)
4019 continue;
4020 if (!is_static)
4021 global_seen = 1;
4022 }
4023
3190f0c6
DE
4024 /* Only check the symbol's kind if it has one. */
4025 if (attrs_valid)
156942c7
DE
4026 {
4027 switch (kind)
4028 {
4029 case VARIABLES_DOMAIN:
4030 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
4031 continue;
4032 break;
4033 case FUNCTIONS_DOMAIN:
4034 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
4035 continue;
4036 break;
4037 case TYPES_DOMAIN:
4038 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
4039 continue;
4040 break;
4041 default:
4042 break;
4043 }
4044 }
4045
3190f0c6
DE
4046 /* Don't crash on bad data. */
4047 if (cu_index >= (dwarf2_per_objfile->n_comp_units
4048 + dwarf2_per_objfile->n_type_units))
4049 {
4050 complaint (&symfile_complaints,
4051 _(".gdb_index entry has bad CU index"
4262abfb 4052 " [in module %s]"), objfile_name (objfile));
3190f0c6
DE
4053 continue;
4054 }
4055
8832e7e3 4056 per_cu = dw2_get_cutu (cu_index);
7b08b9eb 4057 if (file_matcher == NULL || per_cu->v.quick->mark)
276d885b
GB
4058 {
4059 int symtab_was_null =
4060 (per_cu->v.quick->compunit_symtab == NULL);
4061
4062 dw2_instantiate_symtab (per_cu);
4063
4064 if (expansion_notify != NULL
4065 && symtab_was_null
4066 && per_cu->v.quick->compunit_symtab != NULL)
4067 {
4068 expansion_notify (per_cu->v.quick->compunit_symtab,
4069 data);
4070 }
4071 }
9291a0cd
TT
4072 }
4073 }
4074}
4075
43f3e411 4076/* A helper for dw2_find_pc_sect_compunit_symtab which finds the most specific
9703b513
TT
4077 symtab. */
4078
43f3e411
DE
4079static struct compunit_symtab *
4080recursively_find_pc_sect_compunit_symtab (struct compunit_symtab *cust,
4081 CORE_ADDR pc)
9703b513
TT
4082{
4083 int i;
4084
43f3e411
DE
4085 if (COMPUNIT_BLOCKVECTOR (cust) != NULL
4086 && blockvector_contains_pc (COMPUNIT_BLOCKVECTOR (cust), pc))
4087 return cust;
9703b513 4088
43f3e411 4089 if (cust->includes == NULL)
a3ec0bb1
DE
4090 return NULL;
4091
43f3e411 4092 for (i = 0; cust->includes[i]; ++i)
9703b513 4093 {
43f3e411 4094 struct compunit_symtab *s = cust->includes[i];
9703b513 4095
43f3e411 4096 s = recursively_find_pc_sect_compunit_symtab (s, pc);
9703b513
TT
4097 if (s != NULL)
4098 return s;
4099 }
4100
4101 return NULL;
4102}
4103
43f3e411
DE
4104static struct compunit_symtab *
4105dw2_find_pc_sect_compunit_symtab (struct objfile *objfile,
4106 struct bound_minimal_symbol msymbol,
4107 CORE_ADDR pc,
4108 struct obj_section *section,
4109 int warn_if_readin)
9291a0cd
TT
4110{
4111 struct dwarf2_per_cu_data *data;
43f3e411 4112 struct compunit_symtab *result;
9291a0cd
TT
4113
4114 dw2_setup (objfile);
4115
4116 if (!objfile->psymtabs_addrmap)
4117 return NULL;
4118
9a3c8263
SM
4119 data = (struct dwarf2_per_cu_data *) addrmap_find (objfile->psymtabs_addrmap,
4120 pc);
9291a0cd
TT
4121 if (!data)
4122 return NULL;
4123
43f3e411 4124 if (warn_if_readin && data->v.quick->compunit_symtab)
abebb8b0 4125 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
9291a0cd
TT
4126 paddress (get_objfile_arch (objfile), pc));
4127
43f3e411
DE
4128 result
4129 = recursively_find_pc_sect_compunit_symtab (dw2_instantiate_symtab (data),
4130 pc);
9703b513
TT
4131 gdb_assert (result != NULL);
4132 return result;
9291a0cd
TT
4133}
4134
9291a0cd 4135static void
44b13c5a 4136dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
74e2f255 4137 void *data, int need_fullname)
9291a0cd
TT
4138{
4139 int i;
24c79950
TT
4140 struct cleanup *cleanup;
4141 htab_t visited = htab_create_alloc (10, htab_hash_pointer, htab_eq_pointer,
4142 NULL, xcalloc, xfree);
9291a0cd 4143
24c79950 4144 cleanup = make_cleanup_htab_delete (visited);
9291a0cd 4145 dw2_setup (objfile);
ae2de4f8 4146
848e3e78
DE
4147 /* The rule is CUs specify all the files, including those used by
4148 any TU, so there's no need to scan TUs here.
4149 We can ignore file names coming from already-expanded CUs. */
f4dc4d17 4150
848e3e78 4151 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
24c79950 4152 {
8832e7e3 4153 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
24c79950 4154
43f3e411 4155 if (per_cu->v.quick->compunit_symtab)
24c79950
TT
4156 {
4157 void **slot = htab_find_slot (visited, per_cu->v.quick->file_names,
4158 INSERT);
4159
4160 *slot = per_cu->v.quick->file_names;
4161 }
4162 }
4163
848e3e78 4164 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
9291a0cd
TT
4165 {
4166 int j;
8832e7e3 4167 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
7b9f3c50 4168 struct quick_file_names *file_data;
24c79950 4169 void **slot;
9291a0cd 4170
3d7bb9d9 4171 /* We only need to look at symtabs not already expanded. */
43f3e411 4172 if (per_cu->v.quick->compunit_symtab)
9291a0cd
TT
4173 continue;
4174
e4a48d9d 4175 file_data = dw2_get_file_names (per_cu);
7b9f3c50 4176 if (file_data == NULL)
9291a0cd
TT
4177 continue;
4178
24c79950
TT
4179 slot = htab_find_slot (visited, file_data, INSERT);
4180 if (*slot)
4181 {
4182 /* Already visited. */
4183 continue;
4184 }
4185 *slot = file_data;
4186
7b9f3c50 4187 for (j = 0; j < file_data->num_file_names; ++j)
9291a0cd 4188 {
74e2f255
DE
4189 const char *this_real_name;
4190
4191 if (need_fullname)
4192 this_real_name = dw2_get_real_path (objfile, file_data, j);
4193 else
4194 this_real_name = NULL;
7b9f3c50 4195 (*fun) (file_data->file_names[j], this_real_name, data);
9291a0cd
TT
4196 }
4197 }
24c79950
TT
4198
4199 do_cleanups (cleanup);
9291a0cd
TT
4200}
4201
4202static int
4203dw2_has_symbols (struct objfile *objfile)
4204{
4205 return 1;
4206}
4207
4208const struct quick_symbol_functions dwarf2_gdb_index_functions =
4209{
4210 dw2_has_symbols,
4211 dw2_find_last_source_symtab,
4212 dw2_forget_cached_source_info,
f8eba3c6 4213 dw2_map_symtabs_matching_filename,
9291a0cd 4214 dw2_lookup_symbol,
9291a0cd
TT
4215 dw2_print_stats,
4216 dw2_dump,
4217 dw2_relocate,
4218 dw2_expand_symtabs_for_function,
4219 dw2_expand_all_symtabs,
652a8996 4220 dw2_expand_symtabs_with_fullname,
40658b94 4221 dw2_map_matching_symbols,
9291a0cd 4222 dw2_expand_symtabs_matching,
43f3e411 4223 dw2_find_pc_sect_compunit_symtab,
9291a0cd
TT
4224 dw2_map_symbol_filenames
4225};
4226
4227/* Initialize for reading DWARF for this objfile. Return 0 if this
4228 file will use psymtabs, or 1 if using the GNU index. */
4229
4230int
4231dwarf2_initialize_objfile (struct objfile *objfile)
4232{
4233 /* If we're about to read full symbols, don't bother with the
4234 indices. In this case we also don't care if some other debug
4235 format is making psymtabs, because they are all about to be
4236 expanded anyway. */
4237 if ((objfile->flags & OBJF_READNOW))
4238 {
4239 int i;
4240
4241 dwarf2_per_objfile->using_index = 1;
4242 create_all_comp_units (objfile);
0e50663e 4243 create_all_type_units (objfile);
7b9f3c50
DE
4244 dwarf2_per_objfile->quick_file_names_table =
4245 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
9291a0cd 4246
1fd400ff 4247 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
d467dd73 4248 + dwarf2_per_objfile->n_type_units); ++i)
9291a0cd 4249 {
8832e7e3 4250 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
9291a0cd 4251
e254ef6a
DE
4252 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
4253 struct dwarf2_per_cu_quick_data);
9291a0cd
TT
4254 }
4255
4256 /* Return 1 so that gdb sees the "quick" functions. However,
4257 these functions will be no-ops because we will have expanded
4258 all symtabs. */
4259 return 1;
4260 }
4261
4262 if (dwarf2_read_index (objfile))
4263 return 1;
4264
9291a0cd
TT
4265 return 0;
4266}
4267
4268\f
4269
dce234bc
PP
4270/* Build a partial symbol table. */
4271
4272void
f29dff0a 4273dwarf2_build_psymtabs (struct objfile *objfile)
dce234bc 4274{
c9bf0622 4275
f29dff0a 4276 if (objfile->global_psymbols.size == 0 && objfile->static_psymbols.size == 0)
c906108c
SS
4277 {
4278 init_psymbol_list (objfile, 1024);
4279 }
4280
492d29ea 4281 TRY
c9bf0622
TT
4282 {
4283 /* This isn't really ideal: all the data we allocate on the
4284 objfile's obstack is still uselessly kept around. However,
4285 freeing it seems unsafe. */
4286 struct cleanup *cleanups = make_cleanup_discard_psymtabs (objfile);
4287
4288 dwarf2_build_psymtabs_hard (objfile);
4289 discard_cleanups (cleanups);
4290 }
492d29ea
PA
4291 CATCH (except, RETURN_MASK_ERROR)
4292 {
4293 exception_print (gdb_stderr, except);
4294 }
4295 END_CATCH
c906108c 4296}
c906108c 4297
1ce1cefd
DE
4298/* Return the total length of the CU described by HEADER. */
4299
4300static unsigned int
4301get_cu_length (const struct comp_unit_head *header)
4302{
4303 return header->initial_length_size + header->length;
4304}
4305
45452591
DE
4306/* Return TRUE if OFFSET is within CU_HEADER. */
4307
4308static inline int
b64f50a1 4309offset_in_cu_p (const struct comp_unit_head *cu_header, sect_offset offset)
45452591 4310{
b64f50a1 4311 sect_offset bottom = { cu_header->offset.sect_off };
1ce1cefd 4312 sect_offset top = { cu_header->offset.sect_off + get_cu_length (cu_header) };
9a619af0 4313
b64f50a1 4314 return (offset.sect_off >= bottom.sect_off && offset.sect_off < top.sect_off);
45452591
DE
4315}
4316
3b80fe9b
DE
4317/* Find the base address of the compilation unit for range lists and
4318 location lists. It will normally be specified by DW_AT_low_pc.
4319 In DWARF-3 draft 4, the base address could be overridden by
4320 DW_AT_entry_pc. It's been removed, but GCC still uses this for
4321 compilation units with discontinuous ranges. */
4322
4323static void
4324dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
4325{
4326 struct attribute *attr;
4327
4328 cu->base_known = 0;
4329 cu->base_address = 0;
4330
4331 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
4332 if (attr)
4333 {
31aa7e4e 4334 cu->base_address = attr_value_as_address (attr);
3b80fe9b
DE
4335 cu->base_known = 1;
4336 }
4337 else
4338 {
4339 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
4340 if (attr)
4341 {
31aa7e4e 4342 cu->base_address = attr_value_as_address (attr);
3b80fe9b
DE
4343 cu->base_known = 1;
4344 }
4345 }
4346}
4347
93311388
DE
4348/* Read in the comp unit header information from the debug_info at info_ptr.
4349 NOTE: This leaves members offset, first_die_offset to be filled in
4350 by the caller. */
107d2387 4351
d521ce57 4352static const gdb_byte *
107d2387 4353read_comp_unit_head (struct comp_unit_head *cu_header,
d521ce57 4354 const gdb_byte *info_ptr, bfd *abfd)
107d2387
AC
4355{
4356 int signed_addr;
891d2f0b 4357 unsigned int bytes_read;
c764a876
DE
4358
4359 cu_header->length = read_initial_length (abfd, info_ptr, &bytes_read);
4360 cu_header->initial_length_size = bytes_read;
4361 cu_header->offset_size = (bytes_read == 4) ? 4 : 8;
613e1657 4362 info_ptr += bytes_read;
107d2387
AC
4363 cu_header->version = read_2_bytes (abfd, info_ptr);
4364 info_ptr += 2;
b64f50a1
JK
4365 cu_header->abbrev_offset.sect_off = read_offset (abfd, info_ptr, cu_header,
4366 &bytes_read);
613e1657 4367 info_ptr += bytes_read;
107d2387
AC
4368 cu_header->addr_size = read_1_byte (abfd, info_ptr);
4369 info_ptr += 1;
4370 signed_addr = bfd_get_sign_extend_vma (abfd);
4371 if (signed_addr < 0)
8e65ff28 4372 internal_error (__FILE__, __LINE__,
e2e0b3e5 4373 _("read_comp_unit_head: dwarf from non elf file"));
107d2387 4374 cu_header->signed_addr_p = signed_addr;
c764a876 4375
107d2387
AC
4376 return info_ptr;
4377}
4378
36586728
TT
4379/* Helper function that returns the proper abbrev section for
4380 THIS_CU. */
4381
4382static struct dwarf2_section_info *
4383get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
4384{
4385 struct dwarf2_section_info *abbrev;
4386
4387 if (this_cu->is_dwz)
4388 abbrev = &dwarf2_get_dwz_file ()->abbrev;
4389 else
4390 abbrev = &dwarf2_per_objfile->abbrev;
4391
4392 return abbrev;
4393}
4394
9ff913ba
DE
4395/* Subroutine of read_and_check_comp_unit_head and
4396 read_and_check_type_unit_head to simplify them.
4397 Perform various error checking on the header. */
4398
4399static void
4400error_check_comp_unit_head (struct comp_unit_head *header,
4bdcc0c1
DE
4401 struct dwarf2_section_info *section,
4402 struct dwarf2_section_info *abbrev_section)
9ff913ba 4403{
a32a8923 4404 const char *filename = get_section_file_name (section);
9ff913ba
DE
4405
4406 if (header->version != 2 && header->version != 3 && header->version != 4)
4407 error (_("Dwarf Error: wrong version in compilation unit header "
4408 "(is %d, should be 2, 3, or 4) [in module %s]"), header->version,
4409 filename);
4410
b64f50a1 4411 if (header->abbrev_offset.sect_off
36586728 4412 >= dwarf2_section_size (dwarf2_per_objfile->objfile, abbrev_section))
9ff913ba
DE
4413 error (_("Dwarf Error: bad offset (0x%lx) in compilation unit header "
4414 "(offset 0x%lx + 6) [in module %s]"),
b64f50a1 4415 (long) header->abbrev_offset.sect_off, (long) header->offset.sect_off,
9ff913ba
DE
4416 filename);
4417
4418 /* Cast to unsigned long to use 64-bit arithmetic when possible to
4419 avoid potential 32-bit overflow. */
1ce1cefd 4420 if (((unsigned long) header->offset.sect_off + get_cu_length (header))
9ff913ba
DE
4421 > section->size)
4422 error (_("Dwarf Error: bad length (0x%lx) in compilation unit header "
4423 "(offset 0x%lx + 0) [in module %s]"),
b64f50a1 4424 (long) header->length, (long) header->offset.sect_off,
9ff913ba
DE
4425 filename);
4426}
4427
4428/* Read in a CU/TU header and perform some basic error checking.
4429 The contents of the header are stored in HEADER.
4430 The result is a pointer to the start of the first DIE. */
adabb602 4431
d521ce57 4432static const gdb_byte *
9ff913ba
DE
4433read_and_check_comp_unit_head (struct comp_unit_head *header,
4434 struct dwarf2_section_info *section,
4bdcc0c1 4435 struct dwarf2_section_info *abbrev_section,
d521ce57 4436 const gdb_byte *info_ptr,
9ff913ba 4437 int is_debug_types_section)
72bf9492 4438{
d521ce57 4439 const gdb_byte *beg_of_comp_unit = info_ptr;
a32a8923 4440 bfd *abfd = get_section_bfd_owner (section);
72bf9492 4441
b64f50a1 4442 header->offset.sect_off = beg_of_comp_unit - section->buffer;
adabb602 4443
72bf9492
DJ
4444 info_ptr = read_comp_unit_head (header, info_ptr, abfd);
4445
460c1c54
CC
4446 /* If we're reading a type unit, skip over the signature and
4447 type_offset fields. */
b0df02fd 4448 if (is_debug_types_section)
460c1c54
CC
4449 info_ptr += 8 /*signature*/ + header->offset_size;
4450
b64f50a1 4451 header->first_die_offset.cu_off = info_ptr - beg_of_comp_unit;
adabb602 4452
4bdcc0c1 4453 error_check_comp_unit_head (header, section, abbrev_section);
72bf9492
DJ
4454
4455 return info_ptr;
4456}
4457
348e048f
DE
4458/* Read in the types comp unit header information from .debug_types entry at
4459 types_ptr. The result is a pointer to one past the end of the header. */
4460
d521ce57 4461static const gdb_byte *
9ff913ba
DE
4462read_and_check_type_unit_head (struct comp_unit_head *header,
4463 struct dwarf2_section_info *section,
4bdcc0c1 4464 struct dwarf2_section_info *abbrev_section,
d521ce57 4465 const gdb_byte *info_ptr,
dee91e82
DE
4466 ULONGEST *signature,
4467 cu_offset *type_offset_in_tu)
348e048f 4468{
d521ce57 4469 const gdb_byte *beg_of_comp_unit = info_ptr;
a32a8923 4470 bfd *abfd = get_section_bfd_owner (section);
348e048f 4471
b64f50a1 4472 header->offset.sect_off = beg_of_comp_unit - section->buffer;
348e048f 4473
9ff913ba 4474 info_ptr = read_comp_unit_head (header, info_ptr, abfd);
348e048f 4475
9ff913ba
DE
4476 /* If we're reading a type unit, skip over the signature and
4477 type_offset fields. */
4478 if (signature != NULL)
4479 *signature = read_8_bytes (abfd, info_ptr);
4480 info_ptr += 8;
dee91e82
DE
4481 if (type_offset_in_tu != NULL)
4482 type_offset_in_tu->cu_off = read_offset_1 (abfd, info_ptr,
4483 header->offset_size);
9ff913ba
DE
4484 info_ptr += header->offset_size;
4485
b64f50a1 4486 header->first_die_offset.cu_off = info_ptr - beg_of_comp_unit;
348e048f 4487
4bdcc0c1 4488 error_check_comp_unit_head (header, section, abbrev_section);
9ff913ba
DE
4489
4490 return info_ptr;
348e048f
DE
4491}
4492
f4dc4d17
DE
4493/* Fetch the abbreviation table offset from a comp or type unit header. */
4494
4495static sect_offset
4496read_abbrev_offset (struct dwarf2_section_info *section,
4497 sect_offset offset)
4498{
a32a8923 4499 bfd *abfd = get_section_bfd_owner (section);
d521ce57 4500 const gdb_byte *info_ptr;
ac298888 4501 unsigned int initial_length_size, offset_size;
f4dc4d17
DE
4502 sect_offset abbrev_offset;
4503
4504 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
4505 info_ptr = section->buffer + offset.sect_off;
ac298888 4506 read_initial_length (abfd, info_ptr, &initial_length_size);
f4dc4d17
DE
4507 offset_size = initial_length_size == 4 ? 4 : 8;
4508 info_ptr += initial_length_size + 2 /*version*/;
4509 abbrev_offset.sect_off = read_offset_1 (abfd, info_ptr, offset_size);
4510 return abbrev_offset;
4511}
4512
aaa75496
JB
4513/* Allocate a new partial symtab for file named NAME and mark this new
4514 partial symtab as being an include of PST. */
4515
4516static void
d521ce57 4517dwarf2_create_include_psymtab (const char *name, struct partial_symtab *pst,
aaa75496
JB
4518 struct objfile *objfile)
4519{
4520 struct partial_symtab *subpst = allocate_psymtab (name, objfile);
4521
fbd9ab74
JK
4522 if (!IS_ABSOLUTE_PATH (subpst->filename))
4523 {
4524 /* It shares objfile->objfile_obstack. */
4525 subpst->dirname = pst->dirname;
4526 }
4527
aaa75496
JB
4528 subpst->textlow = 0;
4529 subpst->texthigh = 0;
4530
8d749320
SM
4531 subpst->dependencies
4532 = XOBNEW (&objfile->objfile_obstack, struct partial_symtab *);
aaa75496
JB
4533 subpst->dependencies[0] = pst;
4534 subpst->number_of_dependencies = 1;
4535
4536 subpst->globals_offset = 0;
4537 subpst->n_global_syms = 0;
4538 subpst->statics_offset = 0;
4539 subpst->n_static_syms = 0;
43f3e411 4540 subpst->compunit_symtab = NULL;
aaa75496
JB
4541 subpst->read_symtab = pst->read_symtab;
4542 subpst->readin = 0;
4543
4544 /* No private part is necessary for include psymtabs. This property
4545 can be used to differentiate between such include psymtabs and
10b3939b 4546 the regular ones. */
58a9656e 4547 subpst->read_symtab_private = NULL;
aaa75496
JB
4548}
4549
4550/* Read the Line Number Program data and extract the list of files
4551 included by the source file represented by PST. Build an include
d85a05f0 4552 partial symtab for each of these included files. */
aaa75496
JB
4553
4554static void
4555dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
dee91e82
DE
4556 struct die_info *die,
4557 struct partial_symtab *pst)
aaa75496 4558{
d85a05f0
DJ
4559 struct line_header *lh = NULL;
4560 struct attribute *attr;
aaa75496 4561
d85a05f0
DJ
4562 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
4563 if (attr)
3019eac3 4564 lh = dwarf_decode_line_header (DW_UNSND (attr), cu);
aaa75496
JB
4565 if (lh == NULL)
4566 return; /* No linetable, so no includes. */
4567
c6da4cef 4568 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). */
527f3840 4569 dwarf_decode_lines (lh, pst->dirname, cu, pst, pst->textlow, 1);
aaa75496
JB
4570
4571 free_line_header (lh);
4572}
4573
348e048f 4574static hashval_t
52dc124a 4575hash_signatured_type (const void *item)
348e048f 4576{
9a3c8263
SM
4577 const struct signatured_type *sig_type
4578 = (const struct signatured_type *) item;
9a619af0 4579
348e048f 4580 /* This drops the top 32 bits of the signature, but is ok for a hash. */
52dc124a 4581 return sig_type->signature;
348e048f
DE
4582}
4583
4584static int
52dc124a 4585eq_signatured_type (const void *item_lhs, const void *item_rhs)
348e048f 4586{
9a3c8263
SM
4587 const struct signatured_type *lhs = (const struct signatured_type *) item_lhs;
4588 const struct signatured_type *rhs = (const struct signatured_type *) item_rhs;
9a619af0 4589
348e048f
DE
4590 return lhs->signature == rhs->signature;
4591}
4592
1fd400ff
TT
4593/* Allocate a hash table for signatured types. */
4594
4595static htab_t
673bfd45 4596allocate_signatured_type_table (struct objfile *objfile)
1fd400ff
TT
4597{
4598 return htab_create_alloc_ex (41,
52dc124a
DE
4599 hash_signatured_type,
4600 eq_signatured_type,
1fd400ff
TT
4601 NULL,
4602 &objfile->objfile_obstack,
4603 hashtab_obstack_allocate,
4604 dummy_obstack_deallocate);
4605}
4606
d467dd73 4607/* A helper function to add a signatured type CU to a table. */
1fd400ff
TT
4608
4609static int
d467dd73 4610add_signatured_type_cu_to_table (void **slot, void *datum)
1fd400ff 4611{
9a3c8263
SM
4612 struct signatured_type *sigt = (struct signatured_type *) *slot;
4613 struct signatured_type ***datap = (struct signatured_type ***) datum;
1fd400ff 4614
b4dd5633 4615 **datap = sigt;
1fd400ff
TT
4616 ++*datap;
4617
4618 return 1;
4619}
4620
c88ee1f0
DE
4621/* Create the hash table of all entries in the .debug_types
4622 (or .debug_types.dwo) section(s).
4623 If reading a DWO file, then DWO_FILE is a pointer to the DWO file object,
4624 otherwise it is NULL.
4625
4626 The result is a pointer to the hash table or NULL if there are no types.
4627
4628 Note: This function processes DWO files only, not DWP files. */
348e048f 4629
3019eac3
DE
4630static htab_t
4631create_debug_types_hash_table (struct dwo_file *dwo_file,
4632 VEC (dwarf2_section_info_def) *types)
348e048f 4633{
3019eac3 4634 struct objfile *objfile = dwarf2_per_objfile->objfile;
8b70b953 4635 htab_t types_htab = NULL;
8b70b953
TT
4636 int ix;
4637 struct dwarf2_section_info *section;
4bdcc0c1 4638 struct dwarf2_section_info *abbrev_section;
348e048f 4639
3019eac3
DE
4640 if (VEC_empty (dwarf2_section_info_def, types))
4641 return NULL;
348e048f 4642
4bdcc0c1
DE
4643 abbrev_section = (dwo_file != NULL
4644 ? &dwo_file->sections.abbrev
4645 : &dwarf2_per_objfile->abbrev);
4646
b4f54984 4647 if (dwarf_read_debug)
09406207
DE
4648 fprintf_unfiltered (gdb_stdlog, "Reading .debug_types%s for %s:\n",
4649 dwo_file ? ".dwo" : "",
a32a8923 4650 get_section_file_name (abbrev_section));
09406207 4651
8b70b953 4652 for (ix = 0;
3019eac3 4653 VEC_iterate (dwarf2_section_info_def, types, ix, section);
8b70b953
TT
4654 ++ix)
4655 {
3019eac3 4656 bfd *abfd;
d521ce57 4657 const gdb_byte *info_ptr, *end_ptr;
348e048f 4658
8b70b953
TT
4659 dwarf2_read_section (objfile, section);
4660 info_ptr = section->buffer;
348e048f 4661
8b70b953
TT
4662 if (info_ptr == NULL)
4663 continue;
348e048f 4664
3019eac3 4665 /* We can't set abfd until now because the section may be empty or
a32a8923
DE
4666 not present, in which case the bfd is unknown. */
4667 abfd = get_section_bfd_owner (section);
3019eac3 4668
dee91e82
DE
4669 /* We don't use init_cutu_and_read_dies_simple, or some such, here
4670 because we don't need to read any dies: the signature is in the
4671 header. */
8b70b953
TT
4672
4673 end_ptr = info_ptr + section->size;
4674 while (info_ptr < end_ptr)
4675 {
b64f50a1 4676 sect_offset offset;
3019eac3 4677 cu_offset type_offset_in_tu;
8b70b953 4678 ULONGEST signature;
52dc124a 4679 struct signatured_type *sig_type;
3019eac3 4680 struct dwo_unit *dwo_tu;
8b70b953 4681 void **slot;
d521ce57 4682 const gdb_byte *ptr = info_ptr;
9ff913ba 4683 struct comp_unit_head header;
dee91e82 4684 unsigned int length;
348e048f 4685
b64f50a1 4686 offset.sect_off = ptr - section->buffer;
348e048f 4687
8b70b953 4688 /* We need to read the type's signature in order to build the hash
9ff913ba 4689 table, but we don't need anything else just yet. */
348e048f 4690
4bdcc0c1
DE
4691 ptr = read_and_check_type_unit_head (&header, section,
4692 abbrev_section, ptr,
3019eac3 4693 &signature, &type_offset_in_tu);
6caca83c 4694
1ce1cefd 4695 length = get_cu_length (&header);
dee91e82 4696
6caca83c 4697 /* Skip dummy type units. */
dee91e82
DE
4698 if (ptr >= info_ptr + length
4699 || peek_abbrev_code (abfd, ptr) == 0)
6caca83c 4700 {
1ce1cefd 4701 info_ptr += length;
6caca83c
CC
4702 continue;
4703 }
8b70b953 4704
0349ea22
DE
4705 if (types_htab == NULL)
4706 {
4707 if (dwo_file)
4708 types_htab = allocate_dwo_unit_table (objfile);
4709 else
4710 types_htab = allocate_signatured_type_table (objfile);
4711 }
4712
3019eac3
DE
4713 if (dwo_file)
4714 {
4715 sig_type = NULL;
4716 dwo_tu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
4717 struct dwo_unit);
4718 dwo_tu->dwo_file = dwo_file;
4719 dwo_tu->signature = signature;
4720 dwo_tu->type_offset_in_tu = type_offset_in_tu;
8a0459fd 4721 dwo_tu->section = section;
3019eac3
DE
4722 dwo_tu->offset = offset;
4723 dwo_tu->length = length;
4724 }
4725 else
4726 {
4727 /* N.B.: type_offset is not usable if this type uses a DWO file.
4728 The real type_offset is in the DWO file. */
4729 dwo_tu = NULL;
4730 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
4731 struct signatured_type);
4732 sig_type->signature = signature;
4733 sig_type->type_offset_in_tu = type_offset_in_tu;
4734 sig_type->per_cu.objfile = objfile;
4735 sig_type->per_cu.is_debug_types = 1;
8a0459fd 4736 sig_type->per_cu.section = section;
3019eac3
DE
4737 sig_type->per_cu.offset = offset;
4738 sig_type->per_cu.length = length;
4739 }
8b70b953 4740
3019eac3
DE
4741 slot = htab_find_slot (types_htab,
4742 dwo_file ? (void*) dwo_tu : (void *) sig_type,
4743 INSERT);
8b70b953
TT
4744 gdb_assert (slot != NULL);
4745 if (*slot != NULL)
4746 {
3019eac3
DE
4747 sect_offset dup_offset;
4748
4749 if (dwo_file)
4750 {
9a3c8263
SM
4751 const struct dwo_unit *dup_tu
4752 = (const struct dwo_unit *) *slot;
3019eac3
DE
4753
4754 dup_offset = dup_tu->offset;
4755 }
4756 else
4757 {
9a3c8263
SM
4758 const struct signatured_type *dup_tu
4759 = (const struct signatured_type *) *slot;
3019eac3
DE
4760
4761 dup_offset = dup_tu->per_cu.offset;
4762 }
b3c8eb43 4763
8b70b953 4764 complaint (&symfile_complaints,
c88ee1f0 4765 _("debug type entry at offset 0x%x is duplicate to"
4031ecc5 4766 " the entry at offset 0x%x, signature %s"),
3019eac3 4767 offset.sect_off, dup_offset.sect_off,
4031ecc5 4768 hex_string (signature));
8b70b953 4769 }
3019eac3 4770 *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
348e048f 4771
b4f54984 4772 if (dwarf_read_debug > 1)
4031ecc5 4773 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, signature %s\n",
b64f50a1 4774 offset.sect_off,
4031ecc5 4775 hex_string (signature));
348e048f 4776
dee91e82 4777 info_ptr += length;
8b70b953 4778 }
348e048f
DE
4779 }
4780
3019eac3
DE
4781 return types_htab;
4782}
4783
4784/* Create the hash table of all entries in the .debug_types section,
4785 and initialize all_type_units.
4786 The result is zero if there is an error (e.g. missing .debug_types section),
4787 otherwise non-zero. */
4788
4789static int
4790create_all_type_units (struct objfile *objfile)
4791{
4792 htab_t types_htab;
b4dd5633 4793 struct signatured_type **iter;
3019eac3
DE
4794
4795 types_htab = create_debug_types_hash_table (NULL, dwarf2_per_objfile->types);
4796 if (types_htab == NULL)
4797 {
4798 dwarf2_per_objfile->signatured_types = NULL;
4799 return 0;
4800 }
4801
348e048f
DE
4802 dwarf2_per_objfile->signatured_types = types_htab;
4803
6aa5f3a6
DE
4804 dwarf2_per_objfile->n_type_units
4805 = dwarf2_per_objfile->n_allocated_type_units
4806 = htab_elements (types_htab);
8d749320
SM
4807 dwarf2_per_objfile->all_type_units =
4808 XNEWVEC (struct signatured_type *, dwarf2_per_objfile->n_type_units);
d467dd73
DE
4809 iter = &dwarf2_per_objfile->all_type_units[0];
4810 htab_traverse_noresize (types_htab, add_signatured_type_cu_to_table, &iter);
4811 gdb_assert (iter - &dwarf2_per_objfile->all_type_units[0]
4812 == dwarf2_per_objfile->n_type_units);
1fd400ff 4813
348e048f
DE
4814 return 1;
4815}
4816
6aa5f3a6
DE
4817/* Add an entry for signature SIG to dwarf2_per_objfile->signatured_types.
4818 If SLOT is non-NULL, it is the entry to use in the hash table.
4819 Otherwise we find one. */
4820
4821static struct signatured_type *
4822add_type_unit (ULONGEST sig, void **slot)
4823{
4824 struct objfile *objfile = dwarf2_per_objfile->objfile;
4825 int n_type_units = dwarf2_per_objfile->n_type_units;
4826 struct signatured_type *sig_type;
4827
4828 gdb_assert (n_type_units <= dwarf2_per_objfile->n_allocated_type_units);
4829 ++n_type_units;
4830 if (n_type_units > dwarf2_per_objfile->n_allocated_type_units)
4831 {
4832 if (dwarf2_per_objfile->n_allocated_type_units == 0)
4833 dwarf2_per_objfile->n_allocated_type_units = 1;
4834 dwarf2_per_objfile->n_allocated_type_units *= 2;
4835 dwarf2_per_objfile->all_type_units
224c3ddb
SM
4836 = XRESIZEVEC (struct signatured_type *,
4837 dwarf2_per_objfile->all_type_units,
4838 dwarf2_per_objfile->n_allocated_type_units);
6aa5f3a6
DE
4839 ++dwarf2_per_objfile->tu_stats.nr_all_type_units_reallocs;
4840 }
4841 dwarf2_per_objfile->n_type_units = n_type_units;
4842
4843 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
4844 struct signatured_type);
4845 dwarf2_per_objfile->all_type_units[n_type_units - 1] = sig_type;
4846 sig_type->signature = sig;
4847 sig_type->per_cu.is_debug_types = 1;
4848 if (dwarf2_per_objfile->using_index)
4849 {
4850 sig_type->per_cu.v.quick =
4851 OBSTACK_ZALLOC (&objfile->objfile_obstack,
4852 struct dwarf2_per_cu_quick_data);
4853 }
4854
4855 if (slot == NULL)
4856 {
4857 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
4858 sig_type, INSERT);
4859 }
4860 gdb_assert (*slot == NULL);
4861 *slot = sig_type;
4862 /* The rest of sig_type must be filled in by the caller. */
4863 return sig_type;
4864}
4865
a2ce51a0
DE
4866/* Subroutine of lookup_dwo_signatured_type and lookup_dwp_signatured_type.
4867 Fill in SIG_ENTRY with DWO_ENTRY. */
4868
4869static void
4870fill_in_sig_entry_from_dwo_entry (struct objfile *objfile,
4871 struct signatured_type *sig_entry,
4872 struct dwo_unit *dwo_entry)
4873{
7ee85ab1 4874 /* Make sure we're not clobbering something we don't expect to. */
a2ce51a0
DE
4875 gdb_assert (! sig_entry->per_cu.queued);
4876 gdb_assert (sig_entry->per_cu.cu == NULL);
6aa5f3a6
DE
4877 if (dwarf2_per_objfile->using_index)
4878 {
4879 gdb_assert (sig_entry->per_cu.v.quick != NULL);
43f3e411 4880 gdb_assert (sig_entry->per_cu.v.quick->compunit_symtab == NULL);
6aa5f3a6
DE
4881 }
4882 else
4883 gdb_assert (sig_entry->per_cu.v.psymtab == NULL);
a2ce51a0
DE
4884 gdb_assert (sig_entry->signature == dwo_entry->signature);
4885 gdb_assert (sig_entry->type_offset_in_section.sect_off == 0);
4886 gdb_assert (sig_entry->type_unit_group == NULL);
7ee85ab1
DE
4887 gdb_assert (sig_entry->dwo_unit == NULL);
4888
4889 sig_entry->per_cu.section = dwo_entry->section;
4890 sig_entry->per_cu.offset = dwo_entry->offset;
4891 sig_entry->per_cu.length = dwo_entry->length;
4892 sig_entry->per_cu.reading_dwo_directly = 1;
4893 sig_entry->per_cu.objfile = objfile;
a2ce51a0
DE
4894 sig_entry->type_offset_in_tu = dwo_entry->type_offset_in_tu;
4895 sig_entry->dwo_unit = dwo_entry;
4896}
4897
4898/* Subroutine of lookup_signatured_type.
7ee85ab1
DE
4899 If we haven't read the TU yet, create the signatured_type data structure
4900 for a TU to be read in directly from a DWO file, bypassing the stub.
4901 This is the "Stay in DWO Optimization": When there is no DWP file and we're
4902 using .gdb_index, then when reading a CU we want to stay in the DWO file
4903 containing that CU. Otherwise we could end up reading several other DWO
4904 files (due to comdat folding) to process the transitive closure of all the
4905 mentioned TUs, and that can be slow. The current DWO file will have every
4906 type signature that it needs.
a2ce51a0
DE
4907 We only do this for .gdb_index because in the psymtab case we already have
4908 to read all the DWOs to build the type unit groups. */
4909
4910static struct signatured_type *
4911lookup_dwo_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
4912{
4913 struct objfile *objfile = dwarf2_per_objfile->objfile;
4914 struct dwo_file *dwo_file;
4915 struct dwo_unit find_dwo_entry, *dwo_entry;
4916 struct signatured_type find_sig_entry, *sig_entry;
6aa5f3a6 4917 void **slot;
a2ce51a0
DE
4918
4919 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
4920
6aa5f3a6
DE
4921 /* If TU skeletons have been removed then we may not have read in any
4922 TUs yet. */
4923 if (dwarf2_per_objfile->signatured_types == NULL)
4924 {
4925 dwarf2_per_objfile->signatured_types
4926 = allocate_signatured_type_table (objfile);
4927 }
a2ce51a0
DE
4928
4929 /* We only ever need to read in one copy of a signatured type.
6aa5f3a6
DE
4930 Use the global signatured_types array to do our own comdat-folding
4931 of types. If this is the first time we're reading this TU, and
4932 the TU has an entry in .gdb_index, replace the recorded data from
4933 .gdb_index with this TU. */
a2ce51a0 4934
a2ce51a0 4935 find_sig_entry.signature = sig;
6aa5f3a6
DE
4936 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
4937 &find_sig_entry, INSERT);
9a3c8263 4938 sig_entry = (struct signatured_type *) *slot;
7ee85ab1
DE
4939
4940 /* We can get here with the TU already read, *or* in the process of being
6aa5f3a6
DE
4941 read. Don't reassign the global entry to point to this DWO if that's
4942 the case. Also note that if the TU is already being read, it may not
4943 have come from a DWO, the program may be a mix of Fission-compiled
4944 code and non-Fission-compiled code. */
4945
4946 /* Have we already tried to read this TU?
4947 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
4948 needn't exist in the global table yet). */
4949 if (sig_entry != NULL && sig_entry->per_cu.tu_read)
a2ce51a0
DE
4950 return sig_entry;
4951
6aa5f3a6
DE
4952 /* Note: cu->dwo_unit is the dwo_unit that references this TU, not the
4953 dwo_unit of the TU itself. */
4954 dwo_file = cu->dwo_unit->dwo_file;
4955
a2ce51a0
DE
4956 /* Ok, this is the first time we're reading this TU. */
4957 if (dwo_file->tus == NULL)
4958 return NULL;
4959 find_dwo_entry.signature = sig;
9a3c8263 4960 dwo_entry = (struct dwo_unit *) htab_find (dwo_file->tus, &find_dwo_entry);
a2ce51a0
DE
4961 if (dwo_entry == NULL)
4962 return NULL;
4963
6aa5f3a6
DE
4964 /* If the global table doesn't have an entry for this TU, add one. */
4965 if (sig_entry == NULL)
4966 sig_entry = add_type_unit (sig, slot);
4967
a2ce51a0 4968 fill_in_sig_entry_from_dwo_entry (objfile, sig_entry, dwo_entry);
89e63ee4 4969 sig_entry->per_cu.tu_read = 1;
a2ce51a0
DE
4970 return sig_entry;
4971}
4972
a2ce51a0
DE
4973/* Subroutine of lookup_signatured_type.
4974 Look up the type for signature SIG, and if we can't find SIG in .gdb_index
6aa5f3a6
DE
4975 then try the DWP file. If the TU stub (skeleton) has been removed then
4976 it won't be in .gdb_index. */
a2ce51a0
DE
4977
4978static struct signatured_type *
4979lookup_dwp_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
4980{
4981 struct objfile *objfile = dwarf2_per_objfile->objfile;
4982 struct dwp_file *dwp_file = get_dwp_file ();
4983 struct dwo_unit *dwo_entry;
4984 struct signatured_type find_sig_entry, *sig_entry;
6aa5f3a6 4985 void **slot;
a2ce51a0
DE
4986
4987 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
4988 gdb_assert (dwp_file != NULL);
4989
6aa5f3a6
DE
4990 /* If TU skeletons have been removed then we may not have read in any
4991 TUs yet. */
4992 if (dwarf2_per_objfile->signatured_types == NULL)
a2ce51a0 4993 {
6aa5f3a6
DE
4994 dwarf2_per_objfile->signatured_types
4995 = allocate_signatured_type_table (objfile);
a2ce51a0
DE
4996 }
4997
6aa5f3a6
DE
4998 find_sig_entry.signature = sig;
4999 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
5000 &find_sig_entry, INSERT);
9a3c8263 5001 sig_entry = (struct signatured_type *) *slot;
6aa5f3a6
DE
5002
5003 /* Have we already tried to read this TU?
5004 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
5005 needn't exist in the global table yet). */
5006 if (sig_entry != NULL)
5007 return sig_entry;
5008
a2ce51a0
DE
5009 if (dwp_file->tus == NULL)
5010 return NULL;
57d63ce2
DE
5011 dwo_entry = lookup_dwo_unit_in_dwp (dwp_file, NULL,
5012 sig, 1 /* is_debug_types */);
a2ce51a0
DE
5013 if (dwo_entry == NULL)
5014 return NULL;
5015
6aa5f3a6 5016 sig_entry = add_type_unit (sig, slot);
a2ce51a0
DE
5017 fill_in_sig_entry_from_dwo_entry (objfile, sig_entry, dwo_entry);
5018
a2ce51a0
DE
5019 return sig_entry;
5020}
5021
380bca97 5022/* Lookup a signature based type for DW_FORM_ref_sig8.
5a8b3f62
DE
5023 Returns NULL if signature SIG is not present in the table.
5024 It is up to the caller to complain about this. */
348e048f
DE
5025
5026static struct signatured_type *
a2ce51a0 5027lookup_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
348e048f 5028{
a2ce51a0
DE
5029 if (cu->dwo_unit
5030 && dwarf2_per_objfile->using_index)
5031 {
5032 /* We're in a DWO/DWP file, and we're using .gdb_index.
5033 These cases require special processing. */
5034 if (get_dwp_file () == NULL)
5035 return lookup_dwo_signatured_type (cu, sig);
5036 else
5037 return lookup_dwp_signatured_type (cu, sig);
5038 }
5039 else
5040 {
5041 struct signatured_type find_entry, *entry;
348e048f 5042
a2ce51a0
DE
5043 if (dwarf2_per_objfile->signatured_types == NULL)
5044 return NULL;
5045 find_entry.signature = sig;
9a3c8263
SM
5046 entry = ((struct signatured_type *)
5047 htab_find (dwarf2_per_objfile->signatured_types, &find_entry));
a2ce51a0
DE
5048 return entry;
5049 }
348e048f 5050}
42e7ad6c
DE
5051\f
5052/* Low level DIE reading support. */
348e048f 5053
d85a05f0
DJ
5054/* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
5055
5056static void
5057init_cu_die_reader (struct die_reader_specs *reader,
dee91e82 5058 struct dwarf2_cu *cu,
3019eac3
DE
5059 struct dwarf2_section_info *section,
5060 struct dwo_file *dwo_file)
d85a05f0 5061{
fceca515 5062 gdb_assert (section->readin && section->buffer != NULL);
a32a8923 5063 reader->abfd = get_section_bfd_owner (section);
d85a05f0 5064 reader->cu = cu;
3019eac3 5065 reader->dwo_file = dwo_file;
dee91e82
DE
5066 reader->die_section = section;
5067 reader->buffer = section->buffer;
f664829e 5068 reader->buffer_end = section->buffer + section->size;
a2ce51a0 5069 reader->comp_dir = NULL;
d85a05f0
DJ
5070}
5071
b0c7bfa9
DE
5072/* Subroutine of init_cutu_and_read_dies to simplify it.
5073 Read in the rest of a CU/TU top level DIE from DWO_UNIT.
5074 There's just a lot of work to do, and init_cutu_and_read_dies is big enough
5075 already.
5076
5077 STUB_COMP_UNIT_DIE is for the stub DIE, we copy over certain attributes
5078 from it to the DIE in the DWO. If NULL we are skipping the stub.
a2ce51a0
DE
5079 STUB_COMP_DIR is similar to STUB_COMP_UNIT_DIE: When reading a TU directly
5080 from the DWO file, bypassing the stub, it contains the DW_AT_comp_dir
c54a1dd8
DE
5081 attribute of the referencing CU. At most one of STUB_COMP_UNIT_DIE and
5082 STUB_COMP_DIR may be non-NULL.
b0c7bfa9
DE
5083 *RESULT_READER,*RESULT_INFO_PTR,*RESULT_COMP_UNIT_DIE,*RESULT_HAS_CHILDREN
5084 are filled in with the info of the DIE from the DWO file.
5085 ABBREV_TABLE_PROVIDED is non-zero if the caller of init_cutu_and_read_dies
5086 provided an abbrev table to use.
5087 The result is non-zero if a valid (non-dummy) DIE was found. */
5088
5089static int
5090read_cutu_die_from_dwo (struct dwarf2_per_cu_data *this_cu,
5091 struct dwo_unit *dwo_unit,
5092 int abbrev_table_provided,
5093 struct die_info *stub_comp_unit_die,
a2ce51a0 5094 const char *stub_comp_dir,
b0c7bfa9 5095 struct die_reader_specs *result_reader,
d521ce57 5096 const gdb_byte **result_info_ptr,
b0c7bfa9
DE
5097 struct die_info **result_comp_unit_die,
5098 int *result_has_children)
5099{
5100 struct objfile *objfile = dwarf2_per_objfile->objfile;
5101 struct dwarf2_cu *cu = this_cu->cu;
5102 struct dwarf2_section_info *section;
5103 bfd *abfd;
d521ce57 5104 const gdb_byte *begin_info_ptr, *info_ptr;
b0c7bfa9
DE
5105 ULONGEST signature; /* Or dwo_id. */
5106 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
5107 int i,num_extra_attrs;
5108 struct dwarf2_section_info *dwo_abbrev_section;
5109 struct attribute *attr;
5110 struct die_info *comp_unit_die;
5111
b0aeadb3
DE
5112 /* At most one of these may be provided. */
5113 gdb_assert ((stub_comp_unit_die != NULL) + (stub_comp_dir != NULL) <= 1);
a2ce51a0 5114
b0c7bfa9
DE
5115 /* These attributes aren't processed until later:
5116 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
0d60c288
DE
5117 DW_AT_comp_dir is used now, to find the DWO file, but it is also
5118 referenced later. However, these attributes are found in the stub
5119 which we won't have later. In order to not impose this complication
5120 on the rest of the code, we read them here and copy them to the
5121 DWO CU/TU die. */
b0c7bfa9
DE
5122
5123 stmt_list = NULL;
5124 low_pc = NULL;
5125 high_pc = NULL;
5126 ranges = NULL;
5127 comp_dir = NULL;
5128
5129 if (stub_comp_unit_die != NULL)
5130 {
5131 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
5132 DWO file. */
5133 if (! this_cu->is_debug_types)
5134 stmt_list = dwarf2_attr (stub_comp_unit_die, DW_AT_stmt_list, cu);
5135 low_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_low_pc, cu);
5136 high_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_high_pc, cu);
5137 ranges = dwarf2_attr (stub_comp_unit_die, DW_AT_ranges, cu);
5138 comp_dir = dwarf2_attr (stub_comp_unit_die, DW_AT_comp_dir, cu);
5139
5140 /* There should be a DW_AT_addr_base attribute here (if needed).
5141 We need the value before we can process DW_FORM_GNU_addr_index. */
5142 cu->addr_base = 0;
5143 attr = dwarf2_attr (stub_comp_unit_die, DW_AT_GNU_addr_base, cu);
5144 if (attr)
5145 cu->addr_base = DW_UNSND (attr);
5146
5147 /* There should be a DW_AT_ranges_base attribute here (if needed).
5148 We need the value before we can process DW_AT_ranges. */
5149 cu->ranges_base = 0;
5150 attr = dwarf2_attr (stub_comp_unit_die, DW_AT_GNU_ranges_base, cu);
5151 if (attr)
5152 cu->ranges_base = DW_UNSND (attr);
5153 }
a2ce51a0
DE
5154 else if (stub_comp_dir != NULL)
5155 {
5156 /* Reconstruct the comp_dir attribute to simplify the code below. */
8d749320 5157 comp_dir = XOBNEW (&cu->comp_unit_obstack, struct attribute);
a2ce51a0
DE
5158 comp_dir->name = DW_AT_comp_dir;
5159 comp_dir->form = DW_FORM_string;
5160 DW_STRING_IS_CANONICAL (comp_dir) = 0;
5161 DW_STRING (comp_dir) = stub_comp_dir;
5162 }
b0c7bfa9
DE
5163
5164 /* Set up for reading the DWO CU/TU. */
5165 cu->dwo_unit = dwo_unit;
5166 section = dwo_unit->section;
5167 dwarf2_read_section (objfile, section);
a32a8923 5168 abfd = get_section_bfd_owner (section);
b0c7bfa9
DE
5169 begin_info_ptr = info_ptr = section->buffer + dwo_unit->offset.sect_off;
5170 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
5171 init_cu_die_reader (result_reader, cu, section, dwo_unit->dwo_file);
5172
5173 if (this_cu->is_debug_types)
5174 {
5175 ULONGEST header_signature;
5176 cu_offset type_offset_in_tu;
5177 struct signatured_type *sig_type = (struct signatured_type *) this_cu;
5178
5179 info_ptr = read_and_check_type_unit_head (&cu->header, section,
5180 dwo_abbrev_section,
5181 info_ptr,
5182 &header_signature,
5183 &type_offset_in_tu);
a2ce51a0
DE
5184 /* This is not an assert because it can be caused by bad debug info. */
5185 if (sig_type->signature != header_signature)
5186 {
5187 error (_("Dwarf Error: signature mismatch %s vs %s while reading"
5188 " TU at offset 0x%x [in module %s]"),
5189 hex_string (sig_type->signature),
5190 hex_string (header_signature),
5191 dwo_unit->offset.sect_off,
5192 bfd_get_filename (abfd));
5193 }
b0c7bfa9
DE
5194 gdb_assert (dwo_unit->offset.sect_off == cu->header.offset.sect_off);
5195 /* For DWOs coming from DWP files, we don't know the CU length
5196 nor the type's offset in the TU until now. */
5197 dwo_unit->length = get_cu_length (&cu->header);
5198 dwo_unit->type_offset_in_tu = type_offset_in_tu;
5199
5200 /* Establish the type offset that can be used to lookup the type.
5201 For DWO files, we don't know it until now. */
5202 sig_type->type_offset_in_section.sect_off =
5203 dwo_unit->offset.sect_off + dwo_unit->type_offset_in_tu.cu_off;
5204 }
5205 else
5206 {
5207 info_ptr = read_and_check_comp_unit_head (&cu->header, section,
5208 dwo_abbrev_section,
5209 info_ptr, 0);
5210 gdb_assert (dwo_unit->offset.sect_off == cu->header.offset.sect_off);
5211 /* For DWOs coming from DWP files, we don't know the CU length
5212 until now. */
5213 dwo_unit->length = get_cu_length (&cu->header);
5214 }
5215
02142a6c
DE
5216 /* Replace the CU's original abbrev table with the DWO's.
5217 Reminder: We can't read the abbrev table until we've read the header. */
b0c7bfa9
DE
5218 if (abbrev_table_provided)
5219 {
5220 /* Don't free the provided abbrev table, the caller of
5221 init_cutu_and_read_dies owns it. */
5222 dwarf2_read_abbrevs (cu, dwo_abbrev_section);
02142a6c 5223 /* Ensure the DWO abbrev table gets freed. */
b0c7bfa9
DE
5224 make_cleanup (dwarf2_free_abbrev_table, cu);
5225 }
5226 else
5227 {
5228 dwarf2_free_abbrev_table (cu);
5229 dwarf2_read_abbrevs (cu, dwo_abbrev_section);
02142a6c 5230 /* Leave any existing abbrev table cleanup as is. */
b0c7bfa9
DE
5231 }
5232
5233 /* Read in the die, but leave space to copy over the attributes
5234 from the stub. This has the benefit of simplifying the rest of
5235 the code - all the work to maintain the illusion of a single
5236 DW_TAG_{compile,type}_unit DIE is done here. */
5237 num_extra_attrs = ((stmt_list != NULL)
5238 + (low_pc != NULL)
5239 + (high_pc != NULL)
5240 + (ranges != NULL)
5241 + (comp_dir != NULL));
5242 info_ptr = read_full_die_1 (result_reader, result_comp_unit_die, info_ptr,
5243 result_has_children, num_extra_attrs);
5244
5245 /* Copy over the attributes from the stub to the DIE we just read in. */
5246 comp_unit_die = *result_comp_unit_die;
5247 i = comp_unit_die->num_attrs;
5248 if (stmt_list != NULL)
5249 comp_unit_die->attrs[i++] = *stmt_list;
5250 if (low_pc != NULL)
5251 comp_unit_die->attrs[i++] = *low_pc;
5252 if (high_pc != NULL)
5253 comp_unit_die->attrs[i++] = *high_pc;
5254 if (ranges != NULL)
5255 comp_unit_die->attrs[i++] = *ranges;
5256 if (comp_dir != NULL)
5257 comp_unit_die->attrs[i++] = *comp_dir;
5258 comp_unit_die->num_attrs += num_extra_attrs;
5259
b4f54984 5260 if (dwarf_die_debug)
bf6af496
DE
5261 {
5262 fprintf_unfiltered (gdb_stdlog,
5263 "Read die from %s@0x%x of %s:\n",
a32a8923 5264 get_section_name (section),
bf6af496
DE
5265 (unsigned) (begin_info_ptr - section->buffer),
5266 bfd_get_filename (abfd));
b4f54984 5267 dump_die (comp_unit_die, dwarf_die_debug);
bf6af496
DE
5268 }
5269
a2ce51a0
DE
5270 /* Save the comp_dir attribute. If there is no DWP file then we'll read
5271 TUs by skipping the stub and going directly to the entry in the DWO file.
5272 However, skipping the stub means we won't get DW_AT_comp_dir, so we have
5273 to get it via circuitous means. Blech. */
5274 if (comp_dir != NULL)
5275 result_reader->comp_dir = DW_STRING (comp_dir);
5276
b0c7bfa9
DE
5277 /* Skip dummy compilation units. */
5278 if (info_ptr >= begin_info_ptr + dwo_unit->length
5279 || peek_abbrev_code (abfd, info_ptr) == 0)
5280 return 0;
5281
5282 *result_info_ptr = info_ptr;
5283 return 1;
5284}
5285
5286/* Subroutine of init_cutu_and_read_dies to simplify it.
5287 Look up the DWO unit specified by COMP_UNIT_DIE of THIS_CU.
6a506a2d 5288 Returns NULL if the specified DWO unit cannot be found. */
b0c7bfa9
DE
5289
5290static struct dwo_unit *
5291lookup_dwo_unit (struct dwarf2_per_cu_data *this_cu,
5292 struct die_info *comp_unit_die)
5293{
5294 struct dwarf2_cu *cu = this_cu->cu;
5295 struct attribute *attr;
5296 ULONGEST signature;
5297 struct dwo_unit *dwo_unit;
5298 const char *comp_dir, *dwo_name;
5299
a2ce51a0
DE
5300 gdb_assert (cu != NULL);
5301
b0c7bfa9 5302 /* Yeah, we look dwo_name up again, but it simplifies the code. */
7d45c7c3
KB
5303 dwo_name = dwarf2_string_attr (comp_unit_die, DW_AT_GNU_dwo_name, cu);
5304 comp_dir = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
b0c7bfa9
DE
5305
5306 if (this_cu->is_debug_types)
5307 {
5308 struct signatured_type *sig_type;
5309
5310 /* Since this_cu is the first member of struct signatured_type,
5311 we can go from a pointer to one to a pointer to the other. */
5312 sig_type = (struct signatured_type *) this_cu;
5313 signature = sig_type->signature;
5314 dwo_unit = lookup_dwo_type_unit (sig_type, dwo_name, comp_dir);
5315 }
5316 else
5317 {
5318 struct attribute *attr;
5319
5320 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
5321 if (! attr)
5322 error (_("Dwarf Error: missing dwo_id for dwo_name %s"
5323 " [in module %s]"),
4262abfb 5324 dwo_name, objfile_name (this_cu->objfile));
b0c7bfa9
DE
5325 signature = DW_UNSND (attr);
5326 dwo_unit = lookup_dwo_comp_unit (this_cu, dwo_name, comp_dir,
5327 signature);
5328 }
5329
b0c7bfa9
DE
5330 return dwo_unit;
5331}
5332
a2ce51a0 5333/* Subroutine of init_cutu_and_read_dies to simplify it.
6aa5f3a6
DE
5334 See it for a description of the parameters.
5335 Read a TU directly from a DWO file, bypassing the stub.
5336
5337 Note: This function could be a little bit simpler if we shared cleanups
5338 with our caller, init_cutu_and_read_dies. That's generally a fragile thing
5339 to do, so we keep this function self-contained. Or we could move this
5340 into our caller, but it's complex enough already. */
a2ce51a0
DE
5341
5342static void
6aa5f3a6
DE
5343init_tu_and_read_dwo_dies (struct dwarf2_per_cu_data *this_cu,
5344 int use_existing_cu, int keep,
a2ce51a0
DE
5345 die_reader_func_ftype *die_reader_func,
5346 void *data)
5347{
5348 struct dwarf2_cu *cu;
5349 struct signatured_type *sig_type;
6aa5f3a6 5350 struct cleanup *cleanups, *free_cu_cleanup = NULL;
a2ce51a0
DE
5351 struct die_reader_specs reader;
5352 const gdb_byte *info_ptr;
5353 struct die_info *comp_unit_die;
5354 int has_children;
5355
5356 /* Verify we can do the following downcast, and that we have the
5357 data we need. */
5358 gdb_assert (this_cu->is_debug_types && this_cu->reading_dwo_directly);
5359 sig_type = (struct signatured_type *) this_cu;
5360 gdb_assert (sig_type->dwo_unit != NULL);
5361
5362 cleanups = make_cleanup (null_cleanup, NULL);
5363
6aa5f3a6
DE
5364 if (use_existing_cu && this_cu->cu != NULL)
5365 {
5366 gdb_assert (this_cu->cu->dwo_unit == sig_type->dwo_unit);
5367 cu = this_cu->cu;
5368 /* There's no need to do the rereading_dwo_cu handling that
5369 init_cutu_and_read_dies does since we don't read the stub. */
5370 }
5371 else
5372 {
5373 /* If !use_existing_cu, this_cu->cu must be NULL. */
5374 gdb_assert (this_cu->cu == NULL);
8d749320 5375 cu = XNEW (struct dwarf2_cu);
6aa5f3a6
DE
5376 init_one_comp_unit (cu, this_cu);
5377 /* If an error occurs while loading, release our storage. */
5378 free_cu_cleanup = make_cleanup (free_heap_comp_unit, cu);
5379 }
5380
5381 /* A future optimization, if needed, would be to use an existing
5382 abbrev table. When reading DWOs with skeletonless TUs, all the TUs
5383 could share abbrev tables. */
a2ce51a0
DE
5384
5385 if (read_cutu_die_from_dwo (this_cu, sig_type->dwo_unit,
5386 0 /* abbrev_table_provided */,
5387 NULL /* stub_comp_unit_die */,
5388 sig_type->dwo_unit->dwo_file->comp_dir,
5389 &reader, &info_ptr,
5390 &comp_unit_die, &has_children) == 0)
5391 {
5392 /* Dummy die. */
5393 do_cleanups (cleanups);
5394 return;
5395 }
5396
5397 /* All the "real" work is done here. */
5398 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
5399
6aa5f3a6 5400 /* This duplicates the code in init_cutu_and_read_dies,
a2ce51a0
DE
5401 but the alternative is making the latter more complex.
5402 This function is only for the special case of using DWO files directly:
5403 no point in overly complicating the general case just to handle this. */
6aa5f3a6 5404 if (free_cu_cleanup != NULL)
a2ce51a0 5405 {
6aa5f3a6
DE
5406 if (keep)
5407 {
5408 /* We've successfully allocated this compilation unit. Let our
5409 caller clean it up when finished with it. */
5410 discard_cleanups (free_cu_cleanup);
a2ce51a0 5411
6aa5f3a6
DE
5412 /* We can only discard free_cu_cleanup and all subsequent cleanups.
5413 So we have to manually free the abbrev table. */
5414 dwarf2_free_abbrev_table (cu);
a2ce51a0 5415
6aa5f3a6
DE
5416 /* Link this CU into read_in_chain. */
5417 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
5418 dwarf2_per_objfile->read_in_chain = this_cu;
5419 }
5420 else
5421 do_cleanups (free_cu_cleanup);
a2ce51a0 5422 }
a2ce51a0
DE
5423
5424 do_cleanups (cleanups);
5425}
5426
fd820528 5427/* Initialize a CU (or TU) and read its DIEs.
3019eac3 5428 If the CU defers to a DWO file, read the DWO file as well.
dee91e82 5429
f4dc4d17
DE
5430 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
5431 Otherwise the table specified in the comp unit header is read in and used.
5432 This is an optimization for when we already have the abbrev table.
5433
dee91e82
DE
5434 If USE_EXISTING_CU is non-zero, and THIS_CU->cu is non-NULL, then use it.
5435 Otherwise, a new CU is allocated with xmalloc.
5436
5437 If KEEP is non-zero, then if we allocated a dwarf2_cu we add it to
5438 read_in_chain. Otherwise the dwarf2_cu data is freed at the end.
5439
5440 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
fd820528 5441 linker) then DIE_READER_FUNC will not get called. */
aaa75496 5442
70221824 5443static void
fd820528 5444init_cutu_and_read_dies (struct dwarf2_per_cu_data *this_cu,
f4dc4d17 5445 struct abbrev_table *abbrev_table,
fd820528
DE
5446 int use_existing_cu, int keep,
5447 die_reader_func_ftype *die_reader_func,
5448 void *data)
c906108c 5449{
dee91e82 5450 struct objfile *objfile = dwarf2_per_objfile->objfile;
8a0459fd 5451 struct dwarf2_section_info *section = this_cu->section;
a32a8923 5452 bfd *abfd = get_section_bfd_owner (section);
dee91e82 5453 struct dwarf2_cu *cu;
d521ce57 5454 const gdb_byte *begin_info_ptr, *info_ptr;
dee91e82 5455 struct die_reader_specs reader;
d85a05f0 5456 struct die_info *comp_unit_die;
dee91e82 5457 int has_children;
d85a05f0 5458 struct attribute *attr;
365156ad 5459 struct cleanup *cleanups, *free_cu_cleanup = NULL;
dee91e82 5460 struct signatured_type *sig_type = NULL;
4bdcc0c1 5461 struct dwarf2_section_info *abbrev_section;
42e7ad6c
DE
5462 /* Non-zero if CU currently points to a DWO file and we need to
5463 reread it. When this happens we need to reread the skeleton die
a2ce51a0 5464 before we can reread the DWO file (this only applies to CUs, not TUs). */
42e7ad6c 5465 int rereading_dwo_cu = 0;
c906108c 5466
b4f54984 5467 if (dwarf_die_debug)
09406207
DE
5468 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset 0x%x\n",
5469 this_cu->is_debug_types ? "type" : "comp",
5470 this_cu->offset.sect_off);
5471
dee91e82
DE
5472 if (use_existing_cu)
5473 gdb_assert (keep);
23745b47 5474
a2ce51a0
DE
5475 /* If we're reading a TU directly from a DWO file, including a virtual DWO
5476 file (instead of going through the stub), short-circuit all of this. */
5477 if (this_cu->reading_dwo_directly)
5478 {
5479 /* Narrow down the scope of possibilities to have to understand. */
5480 gdb_assert (this_cu->is_debug_types);
5481 gdb_assert (abbrev_table == NULL);
6aa5f3a6
DE
5482 init_tu_and_read_dwo_dies (this_cu, use_existing_cu, keep,
5483 die_reader_func, data);
a2ce51a0
DE
5484 return;
5485 }
5486
dee91e82
DE
5487 cleanups = make_cleanup (null_cleanup, NULL);
5488
5489 /* This is cheap if the section is already read in. */
5490 dwarf2_read_section (objfile, section);
5491
5492 begin_info_ptr = info_ptr = section->buffer + this_cu->offset.sect_off;
36586728
TT
5493
5494 abbrev_section = get_abbrev_section_for_cu (this_cu);
dee91e82
DE
5495
5496 if (use_existing_cu && this_cu->cu != NULL)
5497 {
5498 cu = this_cu->cu;
42e7ad6c
DE
5499 /* If this CU is from a DWO file we need to start over, we need to
5500 refetch the attributes from the skeleton CU.
5501 This could be optimized by retrieving those attributes from when we
5502 were here the first time: the previous comp_unit_die was stored in
5503 comp_unit_obstack. But there's no data yet that we need this
5504 optimization. */
5505 if (cu->dwo_unit != NULL)
5506 rereading_dwo_cu = 1;
dee91e82
DE
5507 }
5508 else
5509 {
5510 /* If !use_existing_cu, this_cu->cu must be NULL. */
5511 gdb_assert (this_cu->cu == NULL);
8d749320 5512 cu = XNEW (struct dwarf2_cu);
dee91e82 5513 init_one_comp_unit (cu, this_cu);
dee91e82 5514 /* If an error occurs while loading, release our storage. */
365156ad 5515 free_cu_cleanup = make_cleanup (free_heap_comp_unit, cu);
42e7ad6c 5516 }
dee91e82 5517
b0c7bfa9 5518 /* Get the header. */
42e7ad6c
DE
5519 if (cu->header.first_die_offset.cu_off != 0 && ! rereading_dwo_cu)
5520 {
5521 /* We already have the header, there's no need to read it in again. */
5522 info_ptr += cu->header.first_die_offset.cu_off;
5523 }
5524 else
5525 {
3019eac3 5526 if (this_cu->is_debug_types)
dee91e82
DE
5527 {
5528 ULONGEST signature;
42e7ad6c 5529 cu_offset type_offset_in_tu;
dee91e82 5530
4bdcc0c1
DE
5531 info_ptr = read_and_check_type_unit_head (&cu->header, section,
5532 abbrev_section, info_ptr,
42e7ad6c
DE
5533 &signature,
5534 &type_offset_in_tu);
dee91e82 5535
42e7ad6c
DE
5536 /* Since per_cu is the first member of struct signatured_type,
5537 we can go from a pointer to one to a pointer to the other. */
5538 sig_type = (struct signatured_type *) this_cu;
5539 gdb_assert (sig_type->signature == signature);
5540 gdb_assert (sig_type->type_offset_in_tu.cu_off
5541 == type_offset_in_tu.cu_off);
dee91e82
DE
5542 gdb_assert (this_cu->offset.sect_off == cu->header.offset.sect_off);
5543
42e7ad6c
DE
5544 /* LENGTH has not been set yet for type units if we're
5545 using .gdb_index. */
1ce1cefd 5546 this_cu->length = get_cu_length (&cu->header);
3019eac3
DE
5547
5548 /* Establish the type offset that can be used to lookup the type. */
5549 sig_type->type_offset_in_section.sect_off =
5550 this_cu->offset.sect_off + sig_type->type_offset_in_tu.cu_off;
dee91e82
DE
5551 }
5552 else
5553 {
4bdcc0c1
DE
5554 info_ptr = read_and_check_comp_unit_head (&cu->header, section,
5555 abbrev_section,
5556 info_ptr, 0);
dee91e82
DE
5557
5558 gdb_assert (this_cu->offset.sect_off == cu->header.offset.sect_off);
1ce1cefd 5559 gdb_assert (this_cu->length == get_cu_length (&cu->header));
dee91e82
DE
5560 }
5561 }
10b3939b 5562
6caca83c 5563 /* Skip dummy compilation units. */
dee91e82 5564 if (info_ptr >= begin_info_ptr + this_cu->length
6caca83c
CC
5565 || peek_abbrev_code (abfd, info_ptr) == 0)
5566 {
dee91e82 5567 do_cleanups (cleanups);
21b2bd31 5568 return;
6caca83c
CC
5569 }
5570
433df2d4
DE
5571 /* If we don't have them yet, read the abbrevs for this compilation unit.
5572 And if we need to read them now, make sure they're freed when we're
42e7ad6c
DE
5573 done. Note that it's important that if the CU had an abbrev table
5574 on entry we don't free it when we're done: Somewhere up the call stack
5575 it may be in use. */
f4dc4d17
DE
5576 if (abbrev_table != NULL)
5577 {
5578 gdb_assert (cu->abbrev_table == NULL);
5579 gdb_assert (cu->header.abbrev_offset.sect_off
5580 == abbrev_table->offset.sect_off);
5581 cu->abbrev_table = abbrev_table;
5582 }
5583 else if (cu->abbrev_table == NULL)
dee91e82 5584 {
4bdcc0c1 5585 dwarf2_read_abbrevs (cu, abbrev_section);
dee91e82
DE
5586 make_cleanup (dwarf2_free_abbrev_table, cu);
5587 }
42e7ad6c
DE
5588 else if (rereading_dwo_cu)
5589 {
5590 dwarf2_free_abbrev_table (cu);
5591 dwarf2_read_abbrevs (cu, abbrev_section);
5592 }
af703f96 5593
dee91e82 5594 /* Read the top level CU/TU die. */
3019eac3 5595 init_cu_die_reader (&reader, cu, section, NULL);
dee91e82 5596 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
93311388 5597
b0c7bfa9
DE
5598 /* If we are in a DWO stub, process it and then read in the "real" CU/TU
5599 from the DWO file.
5600 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains a
5601 DWO CU, that this test will fail (the attribute will not be present). */
3019eac3
DE
5602 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_name, cu);
5603 if (attr)
5604 {
3019eac3 5605 struct dwo_unit *dwo_unit;
b0c7bfa9 5606 struct die_info *dwo_comp_unit_die;
3019eac3
DE
5607
5608 if (has_children)
6a506a2d
DE
5609 {
5610 complaint (&symfile_complaints,
5611 _("compilation unit with DW_AT_GNU_dwo_name"
5612 " has children (offset 0x%x) [in module %s]"),
5613 this_cu->offset.sect_off, bfd_get_filename (abfd));
5614 }
b0c7bfa9 5615 dwo_unit = lookup_dwo_unit (this_cu, comp_unit_die);
6a506a2d 5616 if (dwo_unit != NULL)
3019eac3 5617 {
6a506a2d
DE
5618 if (read_cutu_die_from_dwo (this_cu, dwo_unit,
5619 abbrev_table != NULL,
a2ce51a0 5620 comp_unit_die, NULL,
6a506a2d
DE
5621 &reader, &info_ptr,
5622 &dwo_comp_unit_die, &has_children) == 0)
5623 {
5624 /* Dummy die. */
5625 do_cleanups (cleanups);
5626 return;
5627 }
5628 comp_unit_die = dwo_comp_unit_die;
5629 }
5630 else
5631 {
5632 /* Yikes, we couldn't find the rest of the DIE, we only have
5633 the stub. A complaint has already been logged. There's
5634 not much more we can do except pass on the stub DIE to
5635 die_reader_func. We don't want to throw an error on bad
5636 debug info. */
3019eac3
DE
5637 }
5638 }
5639
b0c7bfa9 5640 /* All of the above is setup for this call. Yikes. */
dee91e82
DE
5641 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
5642
b0c7bfa9 5643 /* Done, clean up. */
365156ad 5644 if (free_cu_cleanup != NULL)
348e048f 5645 {
365156ad
TT
5646 if (keep)
5647 {
5648 /* We've successfully allocated this compilation unit. Let our
5649 caller clean it up when finished with it. */
5650 discard_cleanups (free_cu_cleanup);
dee91e82 5651
365156ad
TT
5652 /* We can only discard free_cu_cleanup and all subsequent cleanups.
5653 So we have to manually free the abbrev table. */
5654 dwarf2_free_abbrev_table (cu);
dee91e82 5655
365156ad
TT
5656 /* Link this CU into read_in_chain. */
5657 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
5658 dwarf2_per_objfile->read_in_chain = this_cu;
5659 }
5660 else
5661 do_cleanups (free_cu_cleanup);
348e048f 5662 }
365156ad
TT
5663
5664 do_cleanups (cleanups);
dee91e82
DE
5665}
5666
33e80786
DE
5667/* Read CU/TU THIS_CU but do not follow DW_AT_GNU_dwo_name if present.
5668 DWO_FILE, if non-NULL, is the DWO file to read (the caller is assumed
5669 to have already done the lookup to find the DWO file).
dee91e82
DE
5670
5671 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
3019eac3 5672 THIS_CU->is_debug_types, but nothing else.
dee91e82
DE
5673
5674 We fill in THIS_CU->length.
5675
5676 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
5677 linker) then DIE_READER_FUNC will not get called.
5678
5679 THIS_CU->cu is always freed when done.
3019eac3
DE
5680 This is done in order to not leave THIS_CU->cu in a state where we have
5681 to care whether it refers to the "main" CU or the DWO CU. */
dee91e82
DE
5682
5683static void
5684init_cutu_and_read_dies_no_follow (struct dwarf2_per_cu_data *this_cu,
3019eac3 5685 struct dwo_file *dwo_file,
dee91e82
DE
5686 die_reader_func_ftype *die_reader_func,
5687 void *data)
5688{
5689 struct objfile *objfile = dwarf2_per_objfile->objfile;
8a0459fd 5690 struct dwarf2_section_info *section = this_cu->section;
a32a8923 5691 bfd *abfd = get_section_bfd_owner (section);
33e80786 5692 struct dwarf2_section_info *abbrev_section;
dee91e82 5693 struct dwarf2_cu cu;
d521ce57 5694 const gdb_byte *begin_info_ptr, *info_ptr;
dee91e82
DE
5695 struct die_reader_specs reader;
5696 struct cleanup *cleanups;
5697 struct die_info *comp_unit_die;
5698 int has_children;
5699
b4f54984 5700 if (dwarf_die_debug)
09406207
DE
5701 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset 0x%x\n",
5702 this_cu->is_debug_types ? "type" : "comp",
5703 this_cu->offset.sect_off);
5704
dee91e82
DE
5705 gdb_assert (this_cu->cu == NULL);
5706
33e80786
DE
5707 abbrev_section = (dwo_file != NULL
5708 ? &dwo_file->sections.abbrev
5709 : get_abbrev_section_for_cu (this_cu));
5710
dee91e82
DE
5711 /* This is cheap if the section is already read in. */
5712 dwarf2_read_section (objfile, section);
5713
5714 init_one_comp_unit (&cu, this_cu);
5715
5716 cleanups = make_cleanup (free_stack_comp_unit, &cu);
5717
5718 begin_info_ptr = info_ptr = section->buffer + this_cu->offset.sect_off;
4bdcc0c1
DE
5719 info_ptr = read_and_check_comp_unit_head (&cu.header, section,
5720 abbrev_section, info_ptr,
3019eac3 5721 this_cu->is_debug_types);
dee91e82 5722
1ce1cefd 5723 this_cu->length = get_cu_length (&cu.header);
dee91e82
DE
5724
5725 /* Skip dummy compilation units. */
5726 if (info_ptr >= begin_info_ptr + this_cu->length
5727 || peek_abbrev_code (abfd, info_ptr) == 0)
c906108c 5728 {
dee91e82 5729 do_cleanups (cleanups);
21b2bd31 5730 return;
93311388 5731 }
72bf9492 5732
dee91e82
DE
5733 dwarf2_read_abbrevs (&cu, abbrev_section);
5734 make_cleanup (dwarf2_free_abbrev_table, &cu);
5735
3019eac3 5736 init_cu_die_reader (&reader, &cu, section, dwo_file);
dee91e82
DE
5737 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
5738
5739 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
5740
5741 do_cleanups (cleanups);
5742}
5743
3019eac3
DE
5744/* Read a CU/TU, except that this does not look for DW_AT_GNU_dwo_name and
5745 does not lookup the specified DWO file.
5746 This cannot be used to read DWO files.
dee91e82
DE
5747
5748 THIS_CU->cu is always freed when done.
3019eac3
DE
5749 This is done in order to not leave THIS_CU->cu in a state where we have
5750 to care whether it refers to the "main" CU or the DWO CU.
5751 We can revisit this if the data shows there's a performance issue. */
dee91e82
DE
5752
5753static void
5754init_cutu_and_read_dies_simple (struct dwarf2_per_cu_data *this_cu,
5755 die_reader_func_ftype *die_reader_func,
5756 void *data)
5757{
33e80786 5758 init_cutu_and_read_dies_no_follow (this_cu, NULL, die_reader_func, data);
dee91e82 5759}
0018ea6f
DE
5760\f
5761/* Type Unit Groups.
dee91e82 5762
0018ea6f
DE
5763 Type Unit Groups are a way to collapse the set of all TUs (type units) into
5764 a more manageable set. The grouping is done by DW_AT_stmt_list entry
5765 so that all types coming from the same compilation (.o file) are grouped
5766 together. A future step could be to put the types in the same symtab as
5767 the CU the types ultimately came from. */
ff013f42 5768
f4dc4d17
DE
5769static hashval_t
5770hash_type_unit_group (const void *item)
5771{
9a3c8263
SM
5772 const struct type_unit_group *tu_group
5773 = (const struct type_unit_group *) item;
f4dc4d17 5774
094b34ac 5775 return hash_stmt_list_entry (&tu_group->hash);
f4dc4d17 5776}
348e048f
DE
5777
5778static int
f4dc4d17 5779eq_type_unit_group (const void *item_lhs, const void *item_rhs)
348e048f 5780{
9a3c8263
SM
5781 const struct type_unit_group *lhs = (const struct type_unit_group *) item_lhs;
5782 const struct type_unit_group *rhs = (const struct type_unit_group *) item_rhs;
348e048f 5783
094b34ac 5784 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
f4dc4d17 5785}
348e048f 5786
f4dc4d17
DE
5787/* Allocate a hash table for type unit groups. */
5788
5789static htab_t
5790allocate_type_unit_groups_table (void)
5791{
5792 return htab_create_alloc_ex (3,
5793 hash_type_unit_group,
5794 eq_type_unit_group,
5795 NULL,
5796 &dwarf2_per_objfile->objfile->objfile_obstack,
5797 hashtab_obstack_allocate,
5798 dummy_obstack_deallocate);
5799}
dee91e82 5800
f4dc4d17
DE
5801/* Type units that don't have DW_AT_stmt_list are grouped into their own
5802 partial symtabs. We combine several TUs per psymtab to not let the size
5803 of any one psymtab grow too big. */
5804#define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
5805#define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
dee91e82 5806
094b34ac 5807/* Helper routine for get_type_unit_group.
f4dc4d17
DE
5808 Create the type_unit_group object used to hold one or more TUs. */
5809
5810static struct type_unit_group *
094b34ac 5811create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
f4dc4d17
DE
5812{
5813 struct objfile *objfile = dwarf2_per_objfile->objfile;
094b34ac 5814 struct dwarf2_per_cu_data *per_cu;
f4dc4d17 5815 struct type_unit_group *tu_group;
f4dc4d17
DE
5816
5817 tu_group = OBSTACK_ZALLOC (&objfile->objfile_obstack,
5818 struct type_unit_group);
094b34ac 5819 per_cu = &tu_group->per_cu;
f4dc4d17 5820 per_cu->objfile = objfile;
f4dc4d17 5821
094b34ac
DE
5822 if (dwarf2_per_objfile->using_index)
5823 {
5824 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
5825 struct dwarf2_per_cu_quick_data);
094b34ac
DE
5826 }
5827 else
5828 {
5829 unsigned int line_offset = line_offset_struct.sect_off;
5830 struct partial_symtab *pst;
5831 char *name;
5832
5833 /* Give the symtab a useful name for debug purposes. */
5834 if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
5835 name = xstrprintf ("<type_units_%d>",
5836 (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
5837 else
5838 name = xstrprintf ("<type_units_at_0x%x>", line_offset);
5839
5840 pst = create_partial_symtab (per_cu, name);
5841 pst->anonymous = 1;
f4dc4d17 5842
094b34ac
DE
5843 xfree (name);
5844 }
f4dc4d17 5845
094b34ac
DE
5846 tu_group->hash.dwo_unit = cu->dwo_unit;
5847 tu_group->hash.line_offset = line_offset_struct;
f4dc4d17
DE
5848
5849 return tu_group;
5850}
5851
094b34ac
DE
5852/* Look up the type_unit_group for type unit CU, and create it if necessary.
5853 STMT_LIST is a DW_AT_stmt_list attribute. */
f4dc4d17
DE
5854
5855static struct type_unit_group *
ff39bb5e 5856get_type_unit_group (struct dwarf2_cu *cu, const struct attribute *stmt_list)
f4dc4d17
DE
5857{
5858 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
5859 struct type_unit_group *tu_group;
5860 void **slot;
5861 unsigned int line_offset;
5862 struct type_unit_group type_unit_group_for_lookup;
5863
5864 if (dwarf2_per_objfile->type_unit_groups == NULL)
5865 {
5866 dwarf2_per_objfile->type_unit_groups =
5867 allocate_type_unit_groups_table ();
5868 }
5869
5870 /* Do we need to create a new group, or can we use an existing one? */
5871
5872 if (stmt_list)
5873 {
5874 line_offset = DW_UNSND (stmt_list);
5875 ++tu_stats->nr_symtab_sharers;
5876 }
5877 else
5878 {
5879 /* Ugh, no stmt_list. Rare, but we have to handle it.
5880 We can do various things here like create one group per TU or
5881 spread them over multiple groups to split up the expansion work.
5882 To avoid worst case scenarios (too many groups or too large groups)
5883 we, umm, group them in bunches. */
5884 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
5885 | (tu_stats->nr_stmt_less_type_units
5886 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
5887 ++tu_stats->nr_stmt_less_type_units;
5888 }
5889
094b34ac
DE
5890 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
5891 type_unit_group_for_lookup.hash.line_offset.sect_off = line_offset;
f4dc4d17
DE
5892 slot = htab_find_slot (dwarf2_per_objfile->type_unit_groups,
5893 &type_unit_group_for_lookup, INSERT);
5894 if (*slot != NULL)
5895 {
9a3c8263 5896 tu_group = (struct type_unit_group *) *slot;
f4dc4d17
DE
5897 gdb_assert (tu_group != NULL);
5898 }
5899 else
5900 {
5901 sect_offset line_offset_struct;
5902
5903 line_offset_struct.sect_off = line_offset;
094b34ac 5904 tu_group = create_type_unit_group (cu, line_offset_struct);
f4dc4d17
DE
5905 *slot = tu_group;
5906 ++tu_stats->nr_symtabs;
5907 }
5908
5909 return tu_group;
5910}
0018ea6f
DE
5911\f
5912/* Partial symbol tables. */
5913
5914/* Create a psymtab named NAME and assign it to PER_CU.
5915
5916 The caller must fill in the following details:
5917 dirname, textlow, texthigh. */
5918
5919static struct partial_symtab *
5920create_partial_symtab (struct dwarf2_per_cu_data *per_cu, const char *name)
5921{
5922 struct objfile *objfile = per_cu->objfile;
5923 struct partial_symtab *pst;
5924
18a94d75 5925 pst = start_psymtab_common (objfile, name, 0,
0018ea6f
DE
5926 objfile->global_psymbols.next,
5927 objfile->static_psymbols.next);
5928
5929 pst->psymtabs_addrmap_supported = 1;
5930
5931 /* This is the glue that links PST into GDB's symbol API. */
5932 pst->read_symtab_private = per_cu;
5933 pst->read_symtab = dwarf2_read_symtab;
5934 per_cu->v.psymtab = pst;
5935
5936 return pst;
5937}
5938
b93601f3
TT
5939/* The DATA object passed to process_psymtab_comp_unit_reader has this
5940 type. */
5941
5942struct process_psymtab_comp_unit_data
5943{
5944 /* True if we are reading a DW_TAG_partial_unit. */
5945
5946 int want_partial_unit;
5947
5948 /* The "pretend" language that is used if the CU doesn't declare a
5949 language. */
5950
5951 enum language pretend_language;
5952};
5953
0018ea6f
DE
5954/* die_reader_func for process_psymtab_comp_unit. */
5955
5956static void
5957process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
d521ce57 5958 const gdb_byte *info_ptr,
0018ea6f
DE
5959 struct die_info *comp_unit_die,
5960 int has_children,
5961 void *data)
5962{
5963 struct dwarf2_cu *cu = reader->cu;
5964 struct objfile *objfile = cu->objfile;
3e29f34a 5965 struct gdbarch *gdbarch = get_objfile_arch (objfile);
0018ea6f 5966 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
0018ea6f
DE
5967 CORE_ADDR baseaddr;
5968 CORE_ADDR best_lowpc = 0, best_highpc = 0;
5969 struct partial_symtab *pst;
3a2b436a 5970 enum pc_bounds_kind cu_bounds_kind;
0018ea6f 5971 const char *filename;
9a3c8263
SM
5972 struct process_psymtab_comp_unit_data *info
5973 = (struct process_psymtab_comp_unit_data *) data;
0018ea6f 5974
b93601f3 5975 if (comp_unit_die->tag == DW_TAG_partial_unit && !info->want_partial_unit)
0018ea6f
DE
5976 return;
5977
5978 gdb_assert (! per_cu->is_debug_types);
5979
b93601f3 5980 prepare_one_comp_unit (cu, comp_unit_die, info->pretend_language);
0018ea6f
DE
5981
5982 cu->list_in_scope = &file_symbols;
5983
5984 /* Allocate a new partial symbol table structure. */
7d45c7c3
KB
5985 filename = dwarf2_string_attr (comp_unit_die, DW_AT_name, cu);
5986 if (filename == NULL)
0018ea6f 5987 filename = "";
0018ea6f
DE
5988
5989 pst = create_partial_symtab (per_cu, filename);
5990
5991 /* This must be done before calling dwarf2_build_include_psymtabs. */
7d45c7c3 5992 pst->dirname = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
0018ea6f
DE
5993
5994 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5995
5996 dwarf2_find_base_address (comp_unit_die, cu);
5997
5998 /* Possibly set the default values of LOWPC and HIGHPC from
5999 `DW_AT_ranges'. */
3a2b436a
JK
6000 cu_bounds_kind = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
6001 &best_highpc, cu, pst);
6002 if (cu_bounds_kind == PC_BOUNDS_HIGH_LOW && best_lowpc < best_highpc)
0018ea6f
DE
6003 /* Store the contiguous range if it is not empty; it can be empty for
6004 CUs with no code. */
6005 addrmap_set_empty (objfile->psymtabs_addrmap,
3e29f34a
MR
6006 gdbarch_adjust_dwarf2_addr (gdbarch,
6007 best_lowpc + baseaddr),
6008 gdbarch_adjust_dwarf2_addr (gdbarch,
6009 best_highpc + baseaddr) - 1,
6010 pst);
0018ea6f
DE
6011
6012 /* Check if comp unit has_children.
6013 If so, read the rest of the partial symbols from this comp unit.
6014 If not, there's no more debug_info for this comp unit. */
6015 if (has_children)
6016 {
6017 struct partial_die_info *first_die;
6018 CORE_ADDR lowpc, highpc;
6019
6020 lowpc = ((CORE_ADDR) -1);
6021 highpc = ((CORE_ADDR) 0);
6022
6023 first_die = load_partial_dies (reader, info_ptr, 1);
6024
6025 scan_partial_symbols (first_die, &lowpc, &highpc,
e385593e 6026 cu_bounds_kind <= PC_BOUNDS_INVALID, cu);
0018ea6f
DE
6027
6028 /* If we didn't find a lowpc, set it to highpc to avoid
6029 complaints from `maint check'. */
6030 if (lowpc == ((CORE_ADDR) -1))
6031 lowpc = highpc;
6032
6033 /* If the compilation unit didn't have an explicit address range,
6034 then use the information extracted from its child dies. */
e385593e 6035 if (cu_bounds_kind <= PC_BOUNDS_INVALID)
0018ea6f
DE
6036 {
6037 best_lowpc = lowpc;
6038 best_highpc = highpc;
6039 }
6040 }
3e29f34a
MR
6041 pst->textlow = gdbarch_adjust_dwarf2_addr (gdbarch, best_lowpc + baseaddr);
6042 pst->texthigh = gdbarch_adjust_dwarf2_addr (gdbarch, best_highpc + baseaddr);
0018ea6f 6043
8763cede 6044 end_psymtab_common (objfile, pst);
0018ea6f
DE
6045
6046 if (!VEC_empty (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs))
6047 {
6048 int i;
6049 int len = VEC_length (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs);
6050 struct dwarf2_per_cu_data *iter;
6051
6052 /* Fill in 'dependencies' here; we fill in 'users' in a
6053 post-pass. */
6054 pst->number_of_dependencies = len;
8d749320
SM
6055 pst->dependencies =
6056 XOBNEWVEC (&objfile->objfile_obstack, struct partial_symtab *, len);
0018ea6f
DE
6057 for (i = 0;
6058 VEC_iterate (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs,
6059 i, iter);
6060 ++i)
6061 pst->dependencies[i] = iter->v.psymtab;
6062
6063 VEC_free (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs);
6064 }
6065
6066 /* Get the list of files included in the current compilation unit,
6067 and build a psymtab for each of them. */
6068 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
6069
b4f54984 6070 if (dwarf_read_debug)
0018ea6f
DE
6071 {
6072 struct gdbarch *gdbarch = get_objfile_arch (objfile);
6073
6074 fprintf_unfiltered (gdb_stdlog,
6075 "Psymtab for %s unit @0x%x: %s - %s"
6076 ", %d global, %d static syms\n",
6077 per_cu->is_debug_types ? "type" : "comp",
6078 per_cu->offset.sect_off,
6079 paddress (gdbarch, pst->textlow),
6080 paddress (gdbarch, pst->texthigh),
6081 pst->n_global_syms, pst->n_static_syms);
6082 }
6083}
6084
6085/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
6086 Process compilation unit THIS_CU for a psymtab. */
6087
6088static void
6089process_psymtab_comp_unit (struct dwarf2_per_cu_data *this_cu,
b93601f3
TT
6090 int want_partial_unit,
6091 enum language pretend_language)
0018ea6f 6092{
b93601f3
TT
6093 struct process_psymtab_comp_unit_data info;
6094
0018ea6f
DE
6095 /* If this compilation unit was already read in, free the
6096 cached copy in order to read it in again. This is
6097 necessary because we skipped some symbols when we first
6098 read in the compilation unit (see load_partial_dies).
6099 This problem could be avoided, but the benefit is unclear. */
6100 if (this_cu->cu != NULL)
6101 free_one_cached_comp_unit (this_cu);
6102
6103 gdb_assert (! this_cu->is_debug_types);
b93601f3
TT
6104 info.want_partial_unit = want_partial_unit;
6105 info.pretend_language = pretend_language;
0018ea6f
DE
6106 init_cutu_and_read_dies (this_cu, NULL, 0, 0,
6107 process_psymtab_comp_unit_reader,
b93601f3 6108 &info);
0018ea6f
DE
6109
6110 /* Age out any secondary CUs. */
6111 age_cached_comp_units ();
6112}
f4dc4d17
DE
6113
6114/* Reader function for build_type_psymtabs. */
6115
6116static void
6117build_type_psymtabs_reader (const struct die_reader_specs *reader,
d521ce57 6118 const gdb_byte *info_ptr,
f4dc4d17
DE
6119 struct die_info *type_unit_die,
6120 int has_children,
6121 void *data)
6122{
6123 struct objfile *objfile = dwarf2_per_objfile->objfile;
6124 struct dwarf2_cu *cu = reader->cu;
6125 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
0186c6a7 6126 struct signatured_type *sig_type;
f4dc4d17
DE
6127 struct type_unit_group *tu_group;
6128 struct attribute *attr;
6129 struct partial_die_info *first_die;
6130 CORE_ADDR lowpc, highpc;
6131 struct partial_symtab *pst;
6132
6133 gdb_assert (data == NULL);
0186c6a7
DE
6134 gdb_assert (per_cu->is_debug_types);
6135 sig_type = (struct signatured_type *) per_cu;
f4dc4d17
DE
6136
6137 if (! has_children)
6138 return;
6139
6140 attr = dwarf2_attr_no_follow (type_unit_die, DW_AT_stmt_list);
094b34ac 6141 tu_group = get_type_unit_group (cu, attr);
f4dc4d17 6142
0186c6a7 6143 VEC_safe_push (sig_type_ptr, tu_group->tus, sig_type);
f4dc4d17
DE
6144
6145 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
6146 cu->list_in_scope = &file_symbols;
6147 pst = create_partial_symtab (per_cu, "");
6148 pst->anonymous = 1;
6149
6150 first_die = load_partial_dies (reader, info_ptr, 1);
6151
6152 lowpc = (CORE_ADDR) -1;
6153 highpc = (CORE_ADDR) 0;
6154 scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
6155
8763cede 6156 end_psymtab_common (objfile, pst);
f4dc4d17
DE
6157}
6158
73051182
DE
6159/* Struct used to sort TUs by their abbreviation table offset. */
6160
6161struct tu_abbrev_offset
6162{
6163 struct signatured_type *sig_type;
6164 sect_offset abbrev_offset;
6165};
6166
6167/* Helper routine for build_type_psymtabs_1, passed to qsort. */
6168
6169static int
6170sort_tu_by_abbrev_offset (const void *ap, const void *bp)
6171{
9a3c8263
SM
6172 const struct tu_abbrev_offset * const *a
6173 = (const struct tu_abbrev_offset * const*) ap;
6174 const struct tu_abbrev_offset * const *b
6175 = (const struct tu_abbrev_offset * const*) bp;
73051182
DE
6176 unsigned int aoff = (*a)->abbrev_offset.sect_off;
6177 unsigned int boff = (*b)->abbrev_offset.sect_off;
6178
6179 return (aoff > boff) - (aoff < boff);
6180}
6181
6182/* Efficiently read all the type units.
6183 This does the bulk of the work for build_type_psymtabs.
6184
6185 The efficiency is because we sort TUs by the abbrev table they use and
6186 only read each abbrev table once. In one program there are 200K TUs
6187 sharing 8K abbrev tables.
6188
6189 The main purpose of this function is to support building the
6190 dwarf2_per_objfile->type_unit_groups table.
6191 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
6192 can collapse the search space by grouping them by stmt_list.
6193 The savings can be significant, in the same program from above the 200K TUs
6194 share 8K stmt_list tables.
6195
6196 FUNC is expected to call get_type_unit_group, which will create the
6197 struct type_unit_group if necessary and add it to
6198 dwarf2_per_objfile->type_unit_groups. */
6199
6200static void
6201build_type_psymtabs_1 (void)
6202{
73051182
DE
6203 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
6204 struct cleanup *cleanups;
6205 struct abbrev_table *abbrev_table;
6206 sect_offset abbrev_offset;
6207 struct tu_abbrev_offset *sorted_by_abbrev;
73051182
DE
6208 int i;
6209
6210 /* It's up to the caller to not call us multiple times. */
6211 gdb_assert (dwarf2_per_objfile->type_unit_groups == NULL);
6212
6213 if (dwarf2_per_objfile->n_type_units == 0)
6214 return;
6215
6216 /* TUs typically share abbrev tables, and there can be way more TUs than
6217 abbrev tables. Sort by abbrev table to reduce the number of times we
6218 read each abbrev table in.
6219 Alternatives are to punt or to maintain a cache of abbrev tables.
6220 This is simpler and efficient enough for now.
6221
6222 Later we group TUs by their DW_AT_stmt_list value (as this defines the
6223 symtab to use). Typically TUs with the same abbrev offset have the same
6224 stmt_list value too so in practice this should work well.
6225
6226 The basic algorithm here is:
6227
6228 sort TUs by abbrev table
6229 for each TU with same abbrev table:
6230 read abbrev table if first user
6231 read TU top level DIE
6232 [IWBN if DWO skeletons had DW_AT_stmt_list]
6233 call FUNC */
6234
b4f54984 6235 if (dwarf_read_debug)
73051182
DE
6236 fprintf_unfiltered (gdb_stdlog, "Building type unit groups ...\n");
6237
6238 /* Sort in a separate table to maintain the order of all_type_units
6239 for .gdb_index: TU indices directly index all_type_units. */
6240 sorted_by_abbrev = XNEWVEC (struct tu_abbrev_offset,
6241 dwarf2_per_objfile->n_type_units);
6242 for (i = 0; i < dwarf2_per_objfile->n_type_units; ++i)
6243 {
6244 struct signatured_type *sig_type = dwarf2_per_objfile->all_type_units[i];
6245
6246 sorted_by_abbrev[i].sig_type = sig_type;
6247 sorted_by_abbrev[i].abbrev_offset =
6248 read_abbrev_offset (sig_type->per_cu.section,
6249 sig_type->per_cu.offset);
6250 }
6251 cleanups = make_cleanup (xfree, sorted_by_abbrev);
6252 qsort (sorted_by_abbrev, dwarf2_per_objfile->n_type_units,
6253 sizeof (struct tu_abbrev_offset), sort_tu_by_abbrev_offset);
6254
6255 abbrev_offset.sect_off = ~(unsigned) 0;
6256 abbrev_table = NULL;
6257 make_cleanup (abbrev_table_free_cleanup, &abbrev_table);
6258
6259 for (i = 0; i < dwarf2_per_objfile->n_type_units; ++i)
6260 {
6261 const struct tu_abbrev_offset *tu = &sorted_by_abbrev[i];
6262
6263 /* Switch to the next abbrev table if necessary. */
6264 if (abbrev_table == NULL
6265 || tu->abbrev_offset.sect_off != abbrev_offset.sect_off)
6266 {
6267 if (abbrev_table != NULL)
6268 {
6269 abbrev_table_free (abbrev_table);
6270 /* Reset to NULL in case abbrev_table_read_table throws
6271 an error: abbrev_table_free_cleanup will get called. */
6272 abbrev_table = NULL;
6273 }
6274 abbrev_offset = tu->abbrev_offset;
6275 abbrev_table =
6276 abbrev_table_read_table (&dwarf2_per_objfile->abbrev,
6277 abbrev_offset);
6278 ++tu_stats->nr_uniq_abbrev_tables;
6279 }
6280
6281 init_cutu_and_read_dies (&tu->sig_type->per_cu, abbrev_table, 0, 0,
6282 build_type_psymtabs_reader, NULL);
6283 }
6284
73051182 6285 do_cleanups (cleanups);
6aa5f3a6 6286}
73051182 6287
6aa5f3a6
DE
6288/* Print collected type unit statistics. */
6289
6290static void
6291print_tu_stats (void)
6292{
6293 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
6294
6295 fprintf_unfiltered (gdb_stdlog, "Type unit statistics:\n");
6296 fprintf_unfiltered (gdb_stdlog, " %d TUs\n",
6297 dwarf2_per_objfile->n_type_units);
6298 fprintf_unfiltered (gdb_stdlog, " %d uniq abbrev tables\n",
6299 tu_stats->nr_uniq_abbrev_tables);
6300 fprintf_unfiltered (gdb_stdlog, " %d symtabs from stmt_list entries\n",
6301 tu_stats->nr_symtabs);
6302 fprintf_unfiltered (gdb_stdlog, " %d symtab sharers\n",
6303 tu_stats->nr_symtab_sharers);
6304 fprintf_unfiltered (gdb_stdlog, " %d type units without a stmt_list\n",
6305 tu_stats->nr_stmt_less_type_units);
6306 fprintf_unfiltered (gdb_stdlog, " %d all_type_units reallocs\n",
6307 tu_stats->nr_all_type_units_reallocs);
73051182
DE
6308}
6309
f4dc4d17
DE
6310/* Traversal function for build_type_psymtabs. */
6311
6312static int
6313build_type_psymtab_dependencies (void **slot, void *info)
6314{
6315 struct objfile *objfile = dwarf2_per_objfile->objfile;
6316 struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
094b34ac 6317 struct dwarf2_per_cu_data *per_cu = &tu_group->per_cu;
f4dc4d17 6318 struct partial_symtab *pst = per_cu->v.psymtab;
0186c6a7
DE
6319 int len = VEC_length (sig_type_ptr, tu_group->tus);
6320 struct signatured_type *iter;
f4dc4d17
DE
6321 int i;
6322
6323 gdb_assert (len > 0);
0186c6a7 6324 gdb_assert (IS_TYPE_UNIT_GROUP (per_cu));
f4dc4d17
DE
6325
6326 pst->number_of_dependencies = len;
8d749320
SM
6327 pst->dependencies =
6328 XOBNEWVEC (&objfile->objfile_obstack, struct partial_symtab *, len);
f4dc4d17 6329 for (i = 0;
0186c6a7 6330 VEC_iterate (sig_type_ptr, tu_group->tus, i, iter);
f4dc4d17
DE
6331 ++i)
6332 {
0186c6a7
DE
6333 gdb_assert (iter->per_cu.is_debug_types);
6334 pst->dependencies[i] = iter->per_cu.v.psymtab;
796a7ff8 6335 iter->type_unit_group = tu_group;
f4dc4d17
DE
6336 }
6337
0186c6a7 6338 VEC_free (sig_type_ptr, tu_group->tus);
348e048f
DE
6339
6340 return 1;
6341}
6342
6343/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
6344 Build partial symbol tables for the .debug_types comp-units. */
6345
6346static void
6347build_type_psymtabs (struct objfile *objfile)
6348{
0e50663e 6349 if (! create_all_type_units (objfile))
348e048f
DE
6350 return;
6351
73051182 6352 build_type_psymtabs_1 ();
6aa5f3a6 6353}
f4dc4d17 6354
6aa5f3a6
DE
6355/* Traversal function for process_skeletonless_type_unit.
6356 Read a TU in a DWO file and build partial symbols for it. */
6357
6358static int
6359process_skeletonless_type_unit (void **slot, void *info)
6360{
6361 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
9a3c8263 6362 struct objfile *objfile = (struct objfile *) info;
6aa5f3a6
DE
6363 struct signatured_type find_entry, *entry;
6364
6365 /* If this TU doesn't exist in the global table, add it and read it in. */
6366
6367 if (dwarf2_per_objfile->signatured_types == NULL)
6368 {
6369 dwarf2_per_objfile->signatured_types
6370 = allocate_signatured_type_table (objfile);
6371 }
6372
6373 find_entry.signature = dwo_unit->signature;
6374 slot = htab_find_slot (dwarf2_per_objfile->signatured_types, &find_entry,
6375 INSERT);
6376 /* If we've already seen this type there's nothing to do. What's happening
6377 is we're doing our own version of comdat-folding here. */
6378 if (*slot != NULL)
6379 return 1;
6380
6381 /* This does the job that create_all_type_units would have done for
6382 this TU. */
6383 entry = add_type_unit (dwo_unit->signature, slot);
6384 fill_in_sig_entry_from_dwo_entry (objfile, entry, dwo_unit);
6385 *slot = entry;
6386
6387 /* This does the job that build_type_psymtabs_1 would have done. */
6388 init_cutu_and_read_dies (&entry->per_cu, NULL, 0, 0,
6389 build_type_psymtabs_reader, NULL);
6390
6391 return 1;
6392}
6393
6394/* Traversal function for process_skeletonless_type_units. */
6395
6396static int
6397process_dwo_file_for_skeletonless_type_units (void **slot, void *info)
6398{
6399 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
6400
6401 if (dwo_file->tus != NULL)
6402 {
6403 htab_traverse_noresize (dwo_file->tus,
6404 process_skeletonless_type_unit, info);
6405 }
6406
6407 return 1;
6408}
6409
6410/* Scan all TUs of DWO files, verifying we've processed them.
6411 This is needed in case a TU was emitted without its skeleton.
6412 Note: This can't be done until we know what all the DWO files are. */
6413
6414static void
6415process_skeletonless_type_units (struct objfile *objfile)
6416{
6417 /* Skeletonless TUs in DWP files without .gdb_index is not supported yet. */
6418 if (get_dwp_file () == NULL
6419 && dwarf2_per_objfile->dwo_files != NULL)
6420 {
6421 htab_traverse_noresize (dwarf2_per_objfile->dwo_files,
6422 process_dwo_file_for_skeletonless_type_units,
6423 objfile);
6424 }
348e048f
DE
6425}
6426
60606b2c
TT
6427/* A cleanup function that clears objfile's psymtabs_addrmap field. */
6428
6429static void
6430psymtabs_addrmap_cleanup (void *o)
6431{
9a3c8263 6432 struct objfile *objfile = (struct objfile *) o;
ec61707d 6433
60606b2c
TT
6434 objfile->psymtabs_addrmap = NULL;
6435}
6436
95554aad
TT
6437/* Compute the 'user' field for each psymtab in OBJFILE. */
6438
6439static void
6440set_partial_user (struct objfile *objfile)
6441{
6442 int i;
6443
6444 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
6445 {
8832e7e3 6446 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
95554aad
TT
6447 struct partial_symtab *pst = per_cu->v.psymtab;
6448 int j;
6449
36586728
TT
6450 if (pst == NULL)
6451 continue;
6452
95554aad
TT
6453 for (j = 0; j < pst->number_of_dependencies; ++j)
6454 {
6455 /* Set the 'user' field only if it is not already set. */
6456 if (pst->dependencies[j]->user == NULL)
6457 pst->dependencies[j]->user = pst;
6458 }
6459 }
6460}
6461
93311388
DE
6462/* Build the partial symbol table by doing a quick pass through the
6463 .debug_info and .debug_abbrev sections. */
72bf9492 6464
93311388 6465static void
c67a9c90 6466dwarf2_build_psymtabs_hard (struct objfile *objfile)
93311388 6467{
60606b2c
TT
6468 struct cleanup *back_to, *addrmap_cleanup;
6469 struct obstack temp_obstack;
21b2bd31 6470 int i;
93311388 6471
b4f54984 6472 if (dwarf_read_debug)
45cfd468
DE
6473 {
6474 fprintf_unfiltered (gdb_stdlog, "Building psymtabs of objfile %s ...\n",
4262abfb 6475 objfile_name (objfile));
45cfd468
DE
6476 }
6477
98bfdba5
PA
6478 dwarf2_per_objfile->reading_partial_symbols = 1;
6479
be391dca 6480 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
91c24f0a 6481
93311388
DE
6482 /* Any cached compilation units will be linked by the per-objfile
6483 read_in_chain. Make sure to free them when we're done. */
6484 back_to = make_cleanup (free_cached_comp_units, NULL);
72bf9492 6485
348e048f
DE
6486 build_type_psymtabs (objfile);
6487
93311388 6488 create_all_comp_units (objfile);
c906108c 6489
60606b2c
TT
6490 /* Create a temporary address map on a temporary obstack. We later
6491 copy this to the final obstack. */
6492 obstack_init (&temp_obstack);
6493 make_cleanup_obstack_free (&temp_obstack);
6494 objfile->psymtabs_addrmap = addrmap_create_mutable (&temp_obstack);
6495 addrmap_cleanup = make_cleanup (psymtabs_addrmap_cleanup, objfile);
72bf9492 6496
21b2bd31 6497 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
93311388 6498 {
8832e7e3 6499 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
aaa75496 6500
b93601f3 6501 process_psymtab_comp_unit (per_cu, 0, language_minimal);
c906108c 6502 }
ff013f42 6503
6aa5f3a6
DE
6504 /* This has to wait until we read the CUs, we need the list of DWOs. */
6505 process_skeletonless_type_units (objfile);
6506
6507 /* Now that all TUs have been processed we can fill in the dependencies. */
6508 if (dwarf2_per_objfile->type_unit_groups != NULL)
6509 {
6510 htab_traverse_noresize (dwarf2_per_objfile->type_unit_groups,
6511 build_type_psymtab_dependencies, NULL);
6512 }
6513
b4f54984 6514 if (dwarf_read_debug)
6aa5f3a6
DE
6515 print_tu_stats ();
6516
95554aad
TT
6517 set_partial_user (objfile);
6518
ff013f42
JK
6519 objfile->psymtabs_addrmap = addrmap_create_fixed (objfile->psymtabs_addrmap,
6520 &objfile->objfile_obstack);
60606b2c 6521 discard_cleanups (addrmap_cleanup);
ff013f42 6522
ae038cb0 6523 do_cleanups (back_to);
45cfd468 6524
b4f54984 6525 if (dwarf_read_debug)
45cfd468 6526 fprintf_unfiltered (gdb_stdlog, "Done building psymtabs of %s\n",
4262abfb 6527 objfile_name (objfile));
ae038cb0
DJ
6528}
6529
3019eac3 6530/* die_reader_func for load_partial_comp_unit. */
ae038cb0
DJ
6531
6532static void
dee91e82 6533load_partial_comp_unit_reader (const struct die_reader_specs *reader,
d521ce57 6534 const gdb_byte *info_ptr,
dee91e82
DE
6535 struct die_info *comp_unit_die,
6536 int has_children,
6537 void *data)
ae038cb0 6538{
dee91e82 6539 struct dwarf2_cu *cu = reader->cu;
ae038cb0 6540
95554aad 6541 prepare_one_comp_unit (cu, comp_unit_die, language_minimal);
ae038cb0 6542
ae038cb0
DJ
6543 /* Check if comp unit has_children.
6544 If so, read the rest of the partial symbols from this comp unit.
0963b4bd 6545 If not, there's no more debug_info for this comp unit. */
d85a05f0 6546 if (has_children)
dee91e82
DE
6547 load_partial_dies (reader, info_ptr, 0);
6548}
98bfdba5 6549
dee91e82
DE
6550/* Load the partial DIEs for a secondary CU into memory.
6551 This is also used when rereading a primary CU with load_all_dies. */
c5b7e1cb 6552
dee91e82
DE
6553static void
6554load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu)
6555{
f4dc4d17
DE
6556 init_cutu_and_read_dies (this_cu, NULL, 1, 1,
6557 load_partial_comp_unit_reader, NULL);
ae038cb0
DJ
6558}
6559
ae038cb0 6560static void
36586728
TT
6561read_comp_units_from_section (struct objfile *objfile,
6562 struct dwarf2_section_info *section,
6563 unsigned int is_dwz,
6564 int *n_allocated,
6565 int *n_comp_units,
6566 struct dwarf2_per_cu_data ***all_comp_units)
ae038cb0 6567{
d521ce57 6568 const gdb_byte *info_ptr;
a32a8923 6569 bfd *abfd = get_section_bfd_owner (section);
be391dca 6570
b4f54984 6571 if (dwarf_read_debug)
bf6af496 6572 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s\n",
a32a8923
DE
6573 get_section_name (section),
6574 get_section_file_name (section));
bf6af496 6575
36586728 6576 dwarf2_read_section (objfile, section);
ae038cb0 6577
36586728 6578 info_ptr = section->buffer;
6e70227d 6579
36586728 6580 while (info_ptr < section->buffer + section->size)
ae038cb0 6581 {
c764a876 6582 unsigned int length, initial_length_size;
ae038cb0 6583 struct dwarf2_per_cu_data *this_cu;
b64f50a1 6584 sect_offset offset;
ae038cb0 6585
36586728 6586 offset.sect_off = info_ptr - section->buffer;
ae038cb0
DJ
6587
6588 /* Read just enough information to find out where the next
6589 compilation unit is. */
36586728 6590 length = read_initial_length (abfd, info_ptr, &initial_length_size);
ae038cb0
DJ
6591
6592 /* Save the compilation unit for later lookup. */
8d749320 6593 this_cu = XOBNEW (&objfile->objfile_obstack, struct dwarf2_per_cu_data);
ae038cb0
DJ
6594 memset (this_cu, 0, sizeof (*this_cu));
6595 this_cu->offset = offset;
c764a876 6596 this_cu->length = length + initial_length_size;
36586728 6597 this_cu->is_dwz = is_dwz;
9291a0cd 6598 this_cu->objfile = objfile;
8a0459fd 6599 this_cu->section = section;
ae038cb0 6600
36586728 6601 if (*n_comp_units == *n_allocated)
ae038cb0 6602 {
36586728 6603 *n_allocated *= 2;
224c3ddb
SM
6604 *all_comp_units = XRESIZEVEC (struct dwarf2_per_cu_data *,
6605 *all_comp_units, *n_allocated);
ae038cb0 6606 }
36586728
TT
6607 (*all_comp_units)[*n_comp_units] = this_cu;
6608 ++*n_comp_units;
ae038cb0
DJ
6609
6610 info_ptr = info_ptr + this_cu->length;
6611 }
36586728
TT
6612}
6613
6614/* Create a list of all compilation units in OBJFILE.
6615 This is only done for -readnow and building partial symtabs. */
6616
6617static void
6618create_all_comp_units (struct objfile *objfile)
6619{
6620 int n_allocated;
6621 int n_comp_units;
6622 struct dwarf2_per_cu_data **all_comp_units;
4db1a1dc 6623 struct dwz_file *dwz;
36586728
TT
6624
6625 n_comp_units = 0;
6626 n_allocated = 10;
8d749320 6627 all_comp_units = XNEWVEC (struct dwarf2_per_cu_data *, n_allocated);
36586728
TT
6628
6629 read_comp_units_from_section (objfile, &dwarf2_per_objfile->info, 0,
6630 &n_allocated, &n_comp_units, &all_comp_units);
6631
4db1a1dc
TT
6632 dwz = dwarf2_get_dwz_file ();
6633 if (dwz != NULL)
6634 read_comp_units_from_section (objfile, &dwz->info, 1,
6635 &n_allocated, &n_comp_units,
6636 &all_comp_units);
ae038cb0 6637
8d749320
SM
6638 dwarf2_per_objfile->all_comp_units = XOBNEWVEC (&objfile->objfile_obstack,
6639 struct dwarf2_per_cu_data *,
6640 n_comp_units);
ae038cb0
DJ
6641 memcpy (dwarf2_per_objfile->all_comp_units, all_comp_units,
6642 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
6643 xfree (all_comp_units);
6644 dwarf2_per_objfile->n_comp_units = n_comp_units;
c906108c
SS
6645}
6646
5734ee8b 6647/* Process all loaded DIEs for compilation unit CU, starting at
cdc07690 6648 FIRST_DIE. The caller should pass SET_ADDRMAP == 1 if the compilation
5734ee8b 6649 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
cdc07690
YQ
6650 DW_AT_ranges). See the comments of add_partial_subprogram on how
6651 SET_ADDRMAP is used and how *LOWPC and *HIGHPC are updated. */
c906108c 6652
72bf9492
DJ
6653static void
6654scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
cdc07690
YQ
6655 CORE_ADDR *highpc, int set_addrmap,
6656 struct dwarf2_cu *cu)
c906108c 6657{
72bf9492 6658 struct partial_die_info *pdi;
c906108c 6659
91c24f0a
DC
6660 /* Now, march along the PDI's, descending into ones which have
6661 interesting children but skipping the children of the other ones,
6662 until we reach the end of the compilation unit. */
c906108c 6663
72bf9492 6664 pdi = first_die;
91c24f0a 6665
72bf9492
DJ
6666 while (pdi != NULL)
6667 {
6668 fixup_partial_die (pdi, cu);
c906108c 6669
f55ee35c 6670 /* Anonymous namespaces or modules have no name but have interesting
91c24f0a
DC
6671 children, so we need to look at them. Ditto for anonymous
6672 enums. */
933c6fe4 6673
72bf9492 6674 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
95554aad
TT
6675 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
6676 || pdi->tag == DW_TAG_imported_unit)
c906108c 6677 {
72bf9492 6678 switch (pdi->tag)
c906108c
SS
6679 {
6680 case DW_TAG_subprogram:
cdc07690 6681 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
c906108c 6682 break;
72929c62 6683 case DW_TAG_constant:
c906108c
SS
6684 case DW_TAG_variable:
6685 case DW_TAG_typedef:
91c24f0a 6686 case DW_TAG_union_type:
72bf9492 6687 if (!pdi->is_declaration)
63d06c5c 6688 {
72bf9492 6689 add_partial_symbol (pdi, cu);
63d06c5c
DC
6690 }
6691 break;
c906108c 6692 case DW_TAG_class_type:
680b30c7 6693 case DW_TAG_interface_type:
c906108c 6694 case DW_TAG_structure_type:
72bf9492 6695 if (!pdi->is_declaration)
c906108c 6696 {
72bf9492 6697 add_partial_symbol (pdi, cu);
c906108c 6698 }
e98c9e7c
TT
6699 if (cu->language == language_rust && pdi->has_children)
6700 scan_partial_symbols (pdi->die_child, lowpc, highpc,
6701 set_addrmap, cu);
c906108c 6702 break;
91c24f0a 6703 case DW_TAG_enumeration_type:
72bf9492
DJ
6704 if (!pdi->is_declaration)
6705 add_partial_enumeration (pdi, cu);
c906108c
SS
6706 break;
6707 case DW_TAG_base_type:
a02abb62 6708 case DW_TAG_subrange_type:
c906108c 6709 /* File scope base type definitions are added to the partial
c5aa993b 6710 symbol table. */
72bf9492 6711 add_partial_symbol (pdi, cu);
c906108c 6712 break;
d9fa45fe 6713 case DW_TAG_namespace:
cdc07690 6714 add_partial_namespace (pdi, lowpc, highpc, set_addrmap, cu);
91c24f0a 6715 break;
5d7cb8df 6716 case DW_TAG_module:
cdc07690 6717 add_partial_module (pdi, lowpc, highpc, set_addrmap, cu);
5d7cb8df 6718 break;
95554aad
TT
6719 case DW_TAG_imported_unit:
6720 {
6721 struct dwarf2_per_cu_data *per_cu;
6722
f4dc4d17
DE
6723 /* For now we don't handle imported units in type units. */
6724 if (cu->per_cu->is_debug_types)
6725 {
6726 error (_("Dwarf Error: DW_TAG_imported_unit is not"
6727 " supported in type units [in module %s]"),
4262abfb 6728 objfile_name (cu->objfile));
f4dc4d17
DE
6729 }
6730
95554aad 6731 per_cu = dwarf2_find_containing_comp_unit (pdi->d.offset,
36586728 6732 pdi->is_dwz,
95554aad
TT
6733 cu->objfile);
6734
6735 /* Go read the partial unit, if needed. */
6736 if (per_cu->v.psymtab == NULL)
b93601f3 6737 process_psymtab_comp_unit (per_cu, 1, cu->language);
95554aad 6738
f4dc4d17 6739 VEC_safe_push (dwarf2_per_cu_ptr,
796a7ff8 6740 cu->per_cu->imported_symtabs, per_cu);
95554aad
TT
6741 }
6742 break;
74921315
KS
6743 case DW_TAG_imported_declaration:
6744 add_partial_symbol (pdi, cu);
6745 break;
c906108c
SS
6746 default:
6747 break;
6748 }
6749 }
6750
72bf9492
DJ
6751 /* If the die has a sibling, skip to the sibling. */
6752
6753 pdi = pdi->die_sibling;
6754 }
6755}
6756
6757/* Functions used to compute the fully scoped name of a partial DIE.
91c24f0a 6758
72bf9492 6759 Normally, this is simple. For C++, the parent DIE's fully scoped
987504bb
JJ
6760 name is concatenated with "::" and the partial DIE's name. For
6761 Java, the same thing occurs except that "." is used instead of "::".
72bf9492
DJ
6762 Enumerators are an exception; they use the scope of their parent
6763 enumeration type, i.e. the name of the enumeration type is not
6764 prepended to the enumerator.
91c24f0a 6765
72bf9492
DJ
6766 There are two complexities. One is DW_AT_specification; in this
6767 case "parent" means the parent of the target of the specification,
6768 instead of the direct parent of the DIE. The other is compilers
6769 which do not emit DW_TAG_namespace; in this case we try to guess
6770 the fully qualified name of structure types from their members'
6771 linkage names. This must be done using the DIE's children rather
6772 than the children of any DW_AT_specification target. We only need
6773 to do this for structures at the top level, i.e. if the target of
6774 any DW_AT_specification (if any; otherwise the DIE itself) does not
6775 have a parent. */
6776
6777/* Compute the scope prefix associated with PDI's parent, in
6778 compilation unit CU. The result will be allocated on CU's
6779 comp_unit_obstack, or a copy of the already allocated PDI->NAME
6780 field. NULL is returned if no prefix is necessary. */
15d034d0 6781static const char *
72bf9492
DJ
6782partial_die_parent_scope (struct partial_die_info *pdi,
6783 struct dwarf2_cu *cu)
6784{
15d034d0 6785 const char *grandparent_scope;
72bf9492 6786 struct partial_die_info *parent, *real_pdi;
91c24f0a 6787
72bf9492
DJ
6788 /* We need to look at our parent DIE; if we have a DW_AT_specification,
6789 then this means the parent of the specification DIE. */
6790
6791 real_pdi = pdi;
72bf9492 6792 while (real_pdi->has_specification)
36586728
TT
6793 real_pdi = find_partial_die (real_pdi->spec_offset,
6794 real_pdi->spec_is_dwz, cu);
72bf9492
DJ
6795
6796 parent = real_pdi->die_parent;
6797 if (parent == NULL)
6798 return NULL;
6799
6800 if (parent->scope_set)
6801 return parent->scope;
6802
6803 fixup_partial_die (parent, cu);
6804
10b3939b 6805 grandparent_scope = partial_die_parent_scope (parent, cu);
72bf9492 6806
acebe513
UW
6807 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
6808 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
6809 Work around this problem here. */
6810 if (cu->language == language_cplus
6e70227d 6811 && parent->tag == DW_TAG_namespace
acebe513
UW
6812 && strcmp (parent->name, "::") == 0
6813 && grandparent_scope == NULL)
6814 {
6815 parent->scope = NULL;
6816 parent->scope_set = 1;
6817 return NULL;
6818 }
6819
9c6c53f7
SA
6820 if (pdi->tag == DW_TAG_enumerator)
6821 /* Enumerators should not get the name of the enumeration as a prefix. */
6822 parent->scope = grandparent_scope;
6823 else if (parent->tag == DW_TAG_namespace
f55ee35c 6824 || parent->tag == DW_TAG_module
72bf9492
DJ
6825 || parent->tag == DW_TAG_structure_type
6826 || parent->tag == DW_TAG_class_type
680b30c7 6827 || parent->tag == DW_TAG_interface_type
ceeb3d5a
TT
6828 || parent->tag == DW_TAG_union_type
6829 || parent->tag == DW_TAG_enumeration_type)
72bf9492
DJ
6830 {
6831 if (grandparent_scope == NULL)
6832 parent->scope = parent->name;
6833 else
3e43a32a
MS
6834 parent->scope = typename_concat (&cu->comp_unit_obstack,
6835 grandparent_scope,
f55ee35c 6836 parent->name, 0, cu);
72bf9492 6837 }
72bf9492
DJ
6838 else
6839 {
6840 /* FIXME drow/2004-04-01: What should we be doing with
6841 function-local names? For partial symbols, we should probably be
6842 ignoring them. */
6843 complaint (&symfile_complaints,
e2e0b3e5 6844 _("unhandled containing DIE tag %d for DIE at %d"),
b64f50a1 6845 parent->tag, pdi->offset.sect_off);
72bf9492 6846 parent->scope = grandparent_scope;
c906108c
SS
6847 }
6848
72bf9492
DJ
6849 parent->scope_set = 1;
6850 return parent->scope;
6851}
6852
6853/* Return the fully scoped name associated with PDI, from compilation unit
6854 CU. The result will be allocated with malloc. */
4568ecf9 6855
72bf9492
DJ
6856static char *
6857partial_die_full_name (struct partial_die_info *pdi,
6858 struct dwarf2_cu *cu)
6859{
15d034d0 6860 const char *parent_scope;
72bf9492 6861
98bfdba5
PA
6862 /* If this is a template instantiation, we can not work out the
6863 template arguments from partial DIEs. So, unfortunately, we have
6864 to go through the full DIEs. At least any work we do building
6865 types here will be reused if full symbols are loaded later. */
6866 if (pdi->has_template_arguments)
6867 {
6868 fixup_partial_die (pdi, cu);
6869
6870 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
6871 {
6872 struct die_info *die;
6873 struct attribute attr;
6874 struct dwarf2_cu *ref_cu = cu;
6875
b64f50a1 6876 /* DW_FORM_ref_addr is using section offset. */
b4069958 6877 attr.name = (enum dwarf_attribute) 0;
98bfdba5 6878 attr.form = DW_FORM_ref_addr;
4568ecf9 6879 attr.u.unsnd = pdi->offset.sect_off;
98bfdba5
PA
6880 die = follow_die_ref (NULL, &attr, &ref_cu);
6881
6882 return xstrdup (dwarf2_full_name (NULL, die, ref_cu));
6883 }
6884 }
6885
72bf9492
DJ
6886 parent_scope = partial_die_parent_scope (pdi, cu);
6887 if (parent_scope == NULL)
6888 return NULL;
6889 else
f55ee35c 6890 return typename_concat (NULL, parent_scope, pdi->name, 0, cu);
c906108c
SS
6891}
6892
6893static void
72bf9492 6894add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
c906108c 6895{
e7c27a73 6896 struct objfile *objfile = cu->objfile;
3e29f34a 6897 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c 6898 CORE_ADDR addr = 0;
15d034d0 6899 const char *actual_name = NULL;
e142c38c 6900 CORE_ADDR baseaddr;
15d034d0 6901 char *built_actual_name;
e142c38c
DJ
6902
6903 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 6904
15d034d0
TT
6905 built_actual_name = partial_die_full_name (pdi, cu);
6906 if (built_actual_name != NULL)
6907 actual_name = built_actual_name;
63d06c5c 6908
72bf9492
DJ
6909 if (actual_name == NULL)
6910 actual_name = pdi->name;
6911
c906108c
SS
6912 switch (pdi->tag)
6913 {
6914 case DW_TAG_subprogram:
3e29f34a 6915 addr = gdbarch_adjust_dwarf2_addr (gdbarch, pdi->lowpc + baseaddr);
2cfa0c8d 6916 if (pdi->is_external || cu->language == language_ada)
c906108c 6917 {
2cfa0c8d
JB
6918 /* brobecker/2007-12-26: Normally, only "external" DIEs are part
6919 of the global scope. But in Ada, we want to be able to access
6920 nested procedures globally. So all Ada subprograms are stored
6921 in the global scope. */
f47fb265 6922 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 6923 built_actual_name != NULL,
f47fb265
MS
6924 VAR_DOMAIN, LOC_BLOCK,
6925 &objfile->global_psymbols,
1762568f 6926 addr, cu->language, objfile);
c906108c
SS
6927 }
6928 else
6929 {
f47fb265 6930 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 6931 built_actual_name != NULL,
f47fb265
MS
6932 VAR_DOMAIN, LOC_BLOCK,
6933 &objfile->static_psymbols,
1762568f 6934 addr, cu->language, objfile);
c906108c
SS
6935 }
6936 break;
72929c62
JB
6937 case DW_TAG_constant:
6938 {
6939 struct psymbol_allocation_list *list;
6940
6941 if (pdi->is_external)
6942 list = &objfile->global_psymbols;
6943 else
6944 list = &objfile->static_psymbols;
f47fb265 6945 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 6946 built_actual_name != NULL, VAR_DOMAIN, LOC_STATIC,
1762568f 6947 list, 0, cu->language, objfile);
72929c62
JB
6948 }
6949 break;
c906108c 6950 case DW_TAG_variable:
95554aad
TT
6951 if (pdi->d.locdesc)
6952 addr = decode_locdesc (pdi->d.locdesc, cu);
caac4577 6953
95554aad 6954 if (pdi->d.locdesc
caac4577
JG
6955 && addr == 0
6956 && !dwarf2_per_objfile->has_section_at_zero)
6957 {
6958 /* A global or static variable may also have been stripped
6959 out by the linker if unused, in which case its address
6960 will be nullified; do not add such variables into partial
6961 symbol table then. */
6962 }
6963 else if (pdi->is_external)
c906108c
SS
6964 {
6965 /* Global Variable.
6966 Don't enter into the minimal symbol tables as there is
6967 a minimal symbol table entry from the ELF symbols already.
6968 Enter into partial symbol table if it has a location
6969 descriptor or a type.
6970 If the location descriptor is missing, new_symbol will create
6971 a LOC_UNRESOLVED symbol, the address of the variable will then
6972 be determined from the minimal symbol table whenever the variable
6973 is referenced.
6974 The address for the partial symbol table entry is not
6975 used by GDB, but it comes in handy for debugging partial symbol
6976 table building. */
6977
95554aad 6978 if (pdi->d.locdesc || pdi->has_type)
f47fb265 6979 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 6980 built_actual_name != NULL,
f47fb265
MS
6981 VAR_DOMAIN, LOC_STATIC,
6982 &objfile->global_psymbols,
1762568f 6983 addr + baseaddr,
f47fb265 6984 cu->language, objfile);
c906108c
SS
6985 }
6986 else
6987 {
ff908ebf
AW
6988 int has_loc = pdi->d.locdesc != NULL;
6989
6990 /* Static Variable. Skip symbols whose value we cannot know (those
6991 without location descriptors or constant values). */
6992 if (!has_loc && !pdi->has_const_value)
decbce07 6993 {
15d034d0 6994 xfree (built_actual_name);
decbce07
MS
6995 return;
6996 }
ff908ebf 6997
f47fb265 6998 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 6999 built_actual_name != NULL,
f47fb265
MS
7000 VAR_DOMAIN, LOC_STATIC,
7001 &objfile->static_psymbols,
ff908ebf 7002 has_loc ? addr + baseaddr : (CORE_ADDR) 0,
f47fb265 7003 cu->language, objfile);
c906108c
SS
7004 }
7005 break;
7006 case DW_TAG_typedef:
7007 case DW_TAG_base_type:
a02abb62 7008 case DW_TAG_subrange_type:
38d518c9 7009 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 7010 built_actual_name != NULL,
176620f1 7011 VAR_DOMAIN, LOC_TYPEDEF,
c906108c 7012 &objfile->static_psymbols,
1762568f 7013 0, cu->language, objfile);
c906108c 7014 break;
74921315 7015 case DW_TAG_imported_declaration:
72bf9492
DJ
7016 case DW_TAG_namespace:
7017 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 7018 built_actual_name != NULL,
72bf9492
DJ
7019 VAR_DOMAIN, LOC_TYPEDEF,
7020 &objfile->global_psymbols,
1762568f 7021 0, cu->language, objfile);
72bf9492 7022 break;
530e8392
KB
7023 case DW_TAG_module:
7024 add_psymbol_to_list (actual_name, strlen (actual_name),
7025 built_actual_name != NULL,
7026 MODULE_DOMAIN, LOC_TYPEDEF,
7027 &objfile->global_psymbols,
1762568f 7028 0, cu->language, objfile);
530e8392 7029 break;
c906108c 7030 case DW_TAG_class_type:
680b30c7 7031 case DW_TAG_interface_type:
c906108c
SS
7032 case DW_TAG_structure_type:
7033 case DW_TAG_union_type:
7034 case DW_TAG_enumeration_type:
fa4028e9
JB
7035 /* Skip external references. The DWARF standard says in the section
7036 about "Structure, Union, and Class Type Entries": "An incomplete
7037 structure, union or class type is represented by a structure,
7038 union or class entry that does not have a byte size attribute
7039 and that has a DW_AT_declaration attribute." */
7040 if (!pdi->has_byte_size && pdi->is_declaration)
decbce07 7041 {
15d034d0 7042 xfree (built_actual_name);
decbce07
MS
7043 return;
7044 }
fa4028e9 7045
63d06c5c
DC
7046 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
7047 static vs. global. */
38d518c9 7048 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 7049 built_actual_name != NULL,
176620f1 7050 STRUCT_DOMAIN, LOC_TYPEDEF,
987504bb
JJ
7051 (cu->language == language_cplus
7052 || cu->language == language_java)
63d06c5c
DC
7053 ? &objfile->global_psymbols
7054 : &objfile->static_psymbols,
1762568f 7055 0, cu->language, objfile);
c906108c 7056
c906108c
SS
7057 break;
7058 case DW_TAG_enumerator:
38d518c9 7059 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 7060 built_actual_name != NULL,
176620f1 7061 VAR_DOMAIN, LOC_CONST,
987504bb
JJ
7062 (cu->language == language_cplus
7063 || cu->language == language_java)
f6fe98ef
DJ
7064 ? &objfile->global_psymbols
7065 : &objfile->static_psymbols,
1762568f 7066 0, cu->language, objfile);
c906108c
SS
7067 break;
7068 default:
7069 break;
7070 }
5c4e30ca 7071
15d034d0 7072 xfree (built_actual_name);
c906108c
SS
7073}
7074
5c4e30ca
DC
7075/* Read a partial die corresponding to a namespace; also, add a symbol
7076 corresponding to that namespace to the symbol table. NAMESPACE is
7077 the name of the enclosing namespace. */
91c24f0a 7078
72bf9492
DJ
7079static void
7080add_partial_namespace (struct partial_die_info *pdi,
91c24f0a 7081 CORE_ADDR *lowpc, CORE_ADDR *highpc,
cdc07690 7082 int set_addrmap, struct dwarf2_cu *cu)
91c24f0a 7083{
72bf9492 7084 /* Add a symbol for the namespace. */
e7c27a73 7085
72bf9492 7086 add_partial_symbol (pdi, cu);
5c4e30ca
DC
7087
7088 /* Now scan partial symbols in that namespace. */
7089
91c24f0a 7090 if (pdi->has_children)
cdc07690 7091 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
91c24f0a
DC
7092}
7093
5d7cb8df
JK
7094/* Read a partial die corresponding to a Fortran module. */
7095
7096static void
7097add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
cdc07690 7098 CORE_ADDR *highpc, int set_addrmap, struct dwarf2_cu *cu)
5d7cb8df 7099{
530e8392
KB
7100 /* Add a symbol for the namespace. */
7101
7102 add_partial_symbol (pdi, cu);
7103
f55ee35c 7104 /* Now scan partial symbols in that module. */
5d7cb8df
JK
7105
7106 if (pdi->has_children)
cdc07690 7107 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
5d7cb8df
JK
7108}
7109
bc30ff58
JB
7110/* Read a partial die corresponding to a subprogram and create a partial
7111 symbol for that subprogram. When the CU language allows it, this
7112 routine also defines a partial symbol for each nested subprogram
cdc07690 7113 that this subprogram contains. If SET_ADDRMAP is true, record the
428fc5fc
YQ
7114 covered ranges in the addrmap. Set *LOWPC and *HIGHPC to the lowest
7115 and highest PC values found in PDI.
6e70227d 7116
cdc07690
YQ
7117 PDI may also be a lexical block, in which case we simply search
7118 recursively for subprograms defined inside that lexical block.
bc30ff58
JB
7119 Again, this is only performed when the CU language allows this
7120 type of definitions. */
7121
7122static void
7123add_partial_subprogram (struct partial_die_info *pdi,
7124 CORE_ADDR *lowpc, CORE_ADDR *highpc,
cdc07690 7125 int set_addrmap, struct dwarf2_cu *cu)
bc30ff58
JB
7126{
7127 if (pdi->tag == DW_TAG_subprogram)
7128 {
7129 if (pdi->has_pc_info)
7130 {
7131 if (pdi->lowpc < *lowpc)
7132 *lowpc = pdi->lowpc;
7133 if (pdi->highpc > *highpc)
7134 *highpc = pdi->highpc;
cdc07690 7135 if (set_addrmap)
5734ee8b 7136 {
5734ee8b 7137 struct objfile *objfile = cu->objfile;
3e29f34a
MR
7138 struct gdbarch *gdbarch = get_objfile_arch (objfile);
7139 CORE_ADDR baseaddr;
7140 CORE_ADDR highpc;
7141 CORE_ADDR lowpc;
5734ee8b
DJ
7142
7143 baseaddr = ANOFFSET (objfile->section_offsets,
7144 SECT_OFF_TEXT (objfile));
3e29f34a
MR
7145 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch,
7146 pdi->lowpc + baseaddr);
7147 highpc = gdbarch_adjust_dwarf2_addr (gdbarch,
7148 pdi->highpc + baseaddr);
7149 addrmap_set_empty (objfile->psymtabs_addrmap, lowpc, highpc - 1,
9291a0cd 7150 cu->per_cu->v.psymtab);
5734ee8b 7151 }
481860b3
GB
7152 }
7153
7154 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
7155 {
bc30ff58 7156 if (!pdi->is_declaration)
e8d05480
JB
7157 /* Ignore subprogram DIEs that do not have a name, they are
7158 illegal. Do not emit a complaint at this point, we will
7159 do so when we convert this psymtab into a symtab. */
7160 if (pdi->name)
7161 add_partial_symbol (pdi, cu);
bc30ff58
JB
7162 }
7163 }
6e70227d 7164
bc30ff58
JB
7165 if (! pdi->has_children)
7166 return;
7167
7168 if (cu->language == language_ada)
7169 {
7170 pdi = pdi->die_child;
7171 while (pdi != NULL)
7172 {
7173 fixup_partial_die (pdi, cu);
7174 if (pdi->tag == DW_TAG_subprogram
7175 || pdi->tag == DW_TAG_lexical_block)
cdc07690 7176 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
bc30ff58
JB
7177 pdi = pdi->die_sibling;
7178 }
7179 }
7180}
7181
91c24f0a
DC
7182/* Read a partial die corresponding to an enumeration type. */
7183
72bf9492
DJ
7184static void
7185add_partial_enumeration (struct partial_die_info *enum_pdi,
7186 struct dwarf2_cu *cu)
91c24f0a 7187{
72bf9492 7188 struct partial_die_info *pdi;
91c24f0a
DC
7189
7190 if (enum_pdi->name != NULL)
72bf9492
DJ
7191 add_partial_symbol (enum_pdi, cu);
7192
7193 pdi = enum_pdi->die_child;
7194 while (pdi)
91c24f0a 7195 {
72bf9492 7196 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
e2e0b3e5 7197 complaint (&symfile_complaints, _("malformed enumerator DIE ignored"));
91c24f0a 7198 else
72bf9492
DJ
7199 add_partial_symbol (pdi, cu);
7200 pdi = pdi->die_sibling;
91c24f0a 7201 }
91c24f0a
DC
7202}
7203
6caca83c
CC
7204/* Return the initial uleb128 in the die at INFO_PTR. */
7205
7206static unsigned int
d521ce57 7207peek_abbrev_code (bfd *abfd, const gdb_byte *info_ptr)
6caca83c
CC
7208{
7209 unsigned int bytes_read;
7210
7211 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
7212}
7213
4bb7a0a7
DJ
7214/* Read the initial uleb128 in the die at INFO_PTR in compilation unit CU.
7215 Return the corresponding abbrev, or NULL if the number is zero (indicating
7216 an empty DIE). In either case *BYTES_READ will be set to the length of
7217 the initial number. */
7218
7219static struct abbrev_info *
d521ce57 7220peek_die_abbrev (const gdb_byte *info_ptr, unsigned int *bytes_read,
891d2f0b 7221 struct dwarf2_cu *cu)
4bb7a0a7
DJ
7222{
7223 bfd *abfd = cu->objfile->obfd;
7224 unsigned int abbrev_number;
7225 struct abbrev_info *abbrev;
7226
7227 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
7228
7229 if (abbrev_number == 0)
7230 return NULL;
7231
433df2d4 7232 abbrev = abbrev_table_lookup_abbrev (cu->abbrev_table, abbrev_number);
4bb7a0a7
DJ
7233 if (!abbrev)
7234 {
422b9917
DE
7235 error (_("Dwarf Error: Could not find abbrev number %d in %s"
7236 " at offset 0x%x [in module %s]"),
7237 abbrev_number, cu->per_cu->is_debug_types ? "TU" : "CU",
7238 cu->header.offset.sect_off, bfd_get_filename (abfd));
4bb7a0a7
DJ
7239 }
7240
7241 return abbrev;
7242}
7243
93311388
DE
7244/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
7245 Returns a pointer to the end of a series of DIEs, terminated by an empty
4bb7a0a7
DJ
7246 DIE. Any children of the skipped DIEs will also be skipped. */
7247
d521ce57
TT
7248static const gdb_byte *
7249skip_children (const struct die_reader_specs *reader, const gdb_byte *info_ptr)
4bb7a0a7 7250{
dee91e82 7251 struct dwarf2_cu *cu = reader->cu;
4bb7a0a7
DJ
7252 struct abbrev_info *abbrev;
7253 unsigned int bytes_read;
7254
7255 while (1)
7256 {
7257 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
7258 if (abbrev == NULL)
7259 return info_ptr + bytes_read;
7260 else
dee91e82 7261 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
4bb7a0a7
DJ
7262 }
7263}
7264
93311388
DE
7265/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
7266 INFO_PTR should point just after the initial uleb128 of a DIE, and the
4bb7a0a7
DJ
7267 abbrev corresponding to that skipped uleb128 should be passed in
7268 ABBREV. Returns a pointer to this DIE's sibling, skipping any
7269 children. */
7270
d521ce57
TT
7271static const gdb_byte *
7272skip_one_die (const struct die_reader_specs *reader, const gdb_byte *info_ptr,
dee91e82 7273 struct abbrev_info *abbrev)
4bb7a0a7
DJ
7274{
7275 unsigned int bytes_read;
7276 struct attribute attr;
dee91e82
DE
7277 bfd *abfd = reader->abfd;
7278 struct dwarf2_cu *cu = reader->cu;
d521ce57 7279 const gdb_byte *buffer = reader->buffer;
f664829e 7280 const gdb_byte *buffer_end = reader->buffer_end;
4bb7a0a7
DJ
7281 unsigned int form, i;
7282
7283 for (i = 0; i < abbrev->num_attrs; i++)
7284 {
7285 /* The only abbrev we care about is DW_AT_sibling. */
7286 if (abbrev->attrs[i].name == DW_AT_sibling)
7287 {
dee91e82 7288 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
4bb7a0a7 7289 if (attr.form == DW_FORM_ref_addr)
3e43a32a
MS
7290 complaint (&symfile_complaints,
7291 _("ignoring absolute DW_AT_sibling"));
4bb7a0a7 7292 else
b9502d3f
WN
7293 {
7294 unsigned int off = dwarf2_get_ref_die_offset (&attr).sect_off;
7295 const gdb_byte *sibling_ptr = buffer + off;
7296
7297 if (sibling_ptr < info_ptr)
7298 complaint (&symfile_complaints,
7299 _("DW_AT_sibling points backwards"));
22869d73
KS
7300 else if (sibling_ptr > reader->buffer_end)
7301 dwarf2_section_buffer_overflow_complaint (reader->die_section);
b9502d3f
WN
7302 else
7303 return sibling_ptr;
7304 }
4bb7a0a7
DJ
7305 }
7306
7307 /* If it isn't DW_AT_sibling, skip this attribute. */
7308 form = abbrev->attrs[i].form;
7309 skip_attribute:
7310 switch (form)
7311 {
4bb7a0a7 7312 case DW_FORM_ref_addr:
ae411497
TT
7313 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
7314 and later it is offset sized. */
7315 if (cu->header.version == 2)
7316 info_ptr += cu->header.addr_size;
7317 else
7318 info_ptr += cu->header.offset_size;
7319 break;
36586728
TT
7320 case DW_FORM_GNU_ref_alt:
7321 info_ptr += cu->header.offset_size;
7322 break;
ae411497 7323 case DW_FORM_addr:
4bb7a0a7
DJ
7324 info_ptr += cu->header.addr_size;
7325 break;
7326 case DW_FORM_data1:
7327 case DW_FORM_ref1:
7328 case DW_FORM_flag:
7329 info_ptr += 1;
7330 break;
2dc7f7b3
TT
7331 case DW_FORM_flag_present:
7332 break;
4bb7a0a7
DJ
7333 case DW_FORM_data2:
7334 case DW_FORM_ref2:
7335 info_ptr += 2;
7336 break;
7337 case DW_FORM_data4:
7338 case DW_FORM_ref4:
7339 info_ptr += 4;
7340 break;
7341 case DW_FORM_data8:
7342 case DW_FORM_ref8:
55f1336d 7343 case DW_FORM_ref_sig8:
4bb7a0a7
DJ
7344 info_ptr += 8;
7345 break;
7346 case DW_FORM_string:
9b1c24c8 7347 read_direct_string (abfd, info_ptr, &bytes_read);
4bb7a0a7
DJ
7348 info_ptr += bytes_read;
7349 break;
2dc7f7b3 7350 case DW_FORM_sec_offset:
4bb7a0a7 7351 case DW_FORM_strp:
36586728 7352 case DW_FORM_GNU_strp_alt:
4bb7a0a7
DJ
7353 info_ptr += cu->header.offset_size;
7354 break;
2dc7f7b3 7355 case DW_FORM_exprloc:
4bb7a0a7
DJ
7356 case DW_FORM_block:
7357 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
7358 info_ptr += bytes_read;
7359 break;
7360 case DW_FORM_block1:
7361 info_ptr += 1 + read_1_byte (abfd, info_ptr);
7362 break;
7363 case DW_FORM_block2:
7364 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
7365 break;
7366 case DW_FORM_block4:
7367 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
7368 break;
7369 case DW_FORM_sdata:
7370 case DW_FORM_udata:
7371 case DW_FORM_ref_udata:
3019eac3
DE
7372 case DW_FORM_GNU_addr_index:
7373 case DW_FORM_GNU_str_index:
d521ce57 7374 info_ptr = safe_skip_leb128 (info_ptr, buffer_end);
4bb7a0a7
DJ
7375 break;
7376 case DW_FORM_indirect:
7377 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
7378 info_ptr += bytes_read;
7379 /* We need to continue parsing from here, so just go back to
7380 the top. */
7381 goto skip_attribute;
7382
7383 default:
3e43a32a
MS
7384 error (_("Dwarf Error: Cannot handle %s "
7385 "in DWARF reader [in module %s]"),
4bb7a0a7
DJ
7386 dwarf_form_name (form),
7387 bfd_get_filename (abfd));
7388 }
7389 }
7390
7391 if (abbrev->has_children)
dee91e82 7392 return skip_children (reader, info_ptr);
4bb7a0a7
DJ
7393 else
7394 return info_ptr;
7395}
7396
93311388 7397/* Locate ORIG_PDI's sibling.
dee91e82 7398 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
91c24f0a 7399
d521ce57 7400static const gdb_byte *
dee91e82
DE
7401locate_pdi_sibling (const struct die_reader_specs *reader,
7402 struct partial_die_info *orig_pdi,
d521ce57 7403 const gdb_byte *info_ptr)
91c24f0a
DC
7404{
7405 /* Do we know the sibling already? */
72bf9492 7406
91c24f0a
DC
7407 if (orig_pdi->sibling)
7408 return orig_pdi->sibling;
7409
7410 /* Are there any children to deal with? */
7411
7412 if (!orig_pdi->has_children)
7413 return info_ptr;
7414
4bb7a0a7 7415 /* Skip the children the long way. */
91c24f0a 7416
dee91e82 7417 return skip_children (reader, info_ptr);
91c24f0a
DC
7418}
7419
257e7a09 7420/* Expand this partial symbol table into a full symbol table. SELF is
442e4d9c 7421 not NULL. */
c906108c
SS
7422
7423static void
257e7a09
YQ
7424dwarf2_read_symtab (struct partial_symtab *self,
7425 struct objfile *objfile)
c906108c 7426{
257e7a09 7427 if (self->readin)
c906108c 7428 {
442e4d9c 7429 warning (_("bug: psymtab for %s is already read in."),
257e7a09 7430 self->filename);
442e4d9c
YQ
7431 }
7432 else
7433 {
7434 if (info_verbose)
c906108c 7435 {
442e4d9c 7436 printf_filtered (_("Reading in symbols for %s..."),
257e7a09 7437 self->filename);
442e4d9c 7438 gdb_flush (gdb_stdout);
c906108c 7439 }
c906108c 7440
442e4d9c 7441 /* Restore our global data. */
9a3c8263
SM
7442 dwarf2_per_objfile
7443 = (struct dwarf2_per_objfile *) objfile_data (objfile,
7444 dwarf2_objfile_data_key);
10b3939b 7445
442e4d9c
YQ
7446 /* If this psymtab is constructed from a debug-only objfile, the
7447 has_section_at_zero flag will not necessarily be correct. We
7448 can get the correct value for this flag by looking at the data
7449 associated with the (presumably stripped) associated objfile. */
7450 if (objfile->separate_debug_objfile_backlink)
7451 {
7452 struct dwarf2_per_objfile *dpo_backlink
9a3c8263
SM
7453 = ((struct dwarf2_per_objfile *)
7454 objfile_data (objfile->separate_debug_objfile_backlink,
7455 dwarf2_objfile_data_key));
9a619af0 7456
442e4d9c
YQ
7457 dwarf2_per_objfile->has_section_at_zero
7458 = dpo_backlink->has_section_at_zero;
7459 }
b2ab525c 7460
442e4d9c 7461 dwarf2_per_objfile->reading_partial_symbols = 0;
98bfdba5 7462
257e7a09 7463 psymtab_to_symtab_1 (self);
c906108c 7464
442e4d9c
YQ
7465 /* Finish up the debug error message. */
7466 if (info_verbose)
7467 printf_filtered (_("done.\n"));
c906108c 7468 }
95554aad
TT
7469
7470 process_cu_includes ();
c906108c 7471}
9cdd5dbd
DE
7472\f
7473/* Reading in full CUs. */
c906108c 7474
10b3939b
DJ
7475/* Add PER_CU to the queue. */
7476
7477static void
95554aad
TT
7478queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
7479 enum language pretend_language)
10b3939b
DJ
7480{
7481 struct dwarf2_queue_item *item;
7482
7483 per_cu->queued = 1;
8d749320 7484 item = XNEW (struct dwarf2_queue_item);
10b3939b 7485 item->per_cu = per_cu;
95554aad 7486 item->pretend_language = pretend_language;
10b3939b
DJ
7487 item->next = NULL;
7488
7489 if (dwarf2_queue == NULL)
7490 dwarf2_queue = item;
7491 else
7492 dwarf2_queue_tail->next = item;
7493
7494 dwarf2_queue_tail = item;
7495}
7496
89e63ee4
DE
7497/* If PER_CU is not yet queued, add it to the queue.
7498 If DEPENDENT_CU is non-NULL, it has a reference to PER_CU so add a
7499 dependency.
0907af0c 7500 The result is non-zero if PER_CU was queued, otherwise the result is zero
69d751e3
DE
7501 meaning either PER_CU is already queued or it is already loaded.
7502
7503 N.B. There is an invariant here that if a CU is queued then it is loaded.
7504 The caller is required to load PER_CU if we return non-zero. */
0907af0c
DE
7505
7506static int
89e63ee4 7507maybe_queue_comp_unit (struct dwarf2_cu *dependent_cu,
0907af0c
DE
7508 struct dwarf2_per_cu_data *per_cu,
7509 enum language pretend_language)
7510{
7511 /* We may arrive here during partial symbol reading, if we need full
7512 DIEs to process an unusual case (e.g. template arguments). Do
7513 not queue PER_CU, just tell our caller to load its DIEs. */
7514 if (dwarf2_per_objfile->reading_partial_symbols)
7515 {
7516 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
7517 return 1;
7518 return 0;
7519 }
7520
7521 /* Mark the dependence relation so that we don't flush PER_CU
7522 too early. */
89e63ee4
DE
7523 if (dependent_cu != NULL)
7524 dwarf2_add_dependence (dependent_cu, per_cu);
0907af0c
DE
7525
7526 /* If it's already on the queue, we have nothing to do. */
7527 if (per_cu->queued)
7528 return 0;
7529
7530 /* If the compilation unit is already loaded, just mark it as
7531 used. */
7532 if (per_cu->cu != NULL)
7533 {
7534 per_cu->cu->last_used = 0;
7535 return 0;
7536 }
7537
7538 /* Add it to the queue. */
7539 queue_comp_unit (per_cu, pretend_language);
7540
7541 return 1;
7542}
7543
10b3939b
DJ
7544/* Process the queue. */
7545
7546static void
a0f42c21 7547process_queue (void)
10b3939b
DJ
7548{
7549 struct dwarf2_queue_item *item, *next_item;
7550
b4f54984 7551 if (dwarf_read_debug)
45cfd468
DE
7552 {
7553 fprintf_unfiltered (gdb_stdlog,
7554 "Expanding one or more symtabs of objfile %s ...\n",
4262abfb 7555 objfile_name (dwarf2_per_objfile->objfile));
45cfd468
DE
7556 }
7557
03dd20cc
DJ
7558 /* The queue starts out with one item, but following a DIE reference
7559 may load a new CU, adding it to the end of the queue. */
10b3939b
DJ
7560 for (item = dwarf2_queue; item != NULL; dwarf2_queue = item = next_item)
7561 {
cc12ce38
DE
7562 if ((dwarf2_per_objfile->using_index
7563 ? !item->per_cu->v.quick->compunit_symtab
7564 : (item->per_cu->v.psymtab && !item->per_cu->v.psymtab->readin))
7565 /* Skip dummy CUs. */
7566 && item->per_cu->cu != NULL)
f4dc4d17
DE
7567 {
7568 struct dwarf2_per_cu_data *per_cu = item->per_cu;
73be47f5 7569 unsigned int debug_print_threshold;
247f5c4f 7570 char buf[100];
f4dc4d17 7571
247f5c4f 7572 if (per_cu->is_debug_types)
f4dc4d17 7573 {
247f5c4f
DE
7574 struct signatured_type *sig_type =
7575 (struct signatured_type *) per_cu;
7576
7577 sprintf (buf, "TU %s at offset 0x%x",
73be47f5
DE
7578 hex_string (sig_type->signature),
7579 per_cu->offset.sect_off);
7580 /* There can be 100s of TUs.
7581 Only print them in verbose mode. */
7582 debug_print_threshold = 2;
f4dc4d17 7583 }
247f5c4f 7584 else
73be47f5
DE
7585 {
7586 sprintf (buf, "CU at offset 0x%x", per_cu->offset.sect_off);
7587 debug_print_threshold = 1;
7588 }
247f5c4f 7589
b4f54984 7590 if (dwarf_read_debug >= debug_print_threshold)
247f5c4f 7591 fprintf_unfiltered (gdb_stdlog, "Expanding symtab of %s\n", buf);
f4dc4d17
DE
7592
7593 if (per_cu->is_debug_types)
7594 process_full_type_unit (per_cu, item->pretend_language);
7595 else
7596 process_full_comp_unit (per_cu, item->pretend_language);
7597
b4f54984 7598 if (dwarf_read_debug >= debug_print_threshold)
247f5c4f 7599 fprintf_unfiltered (gdb_stdlog, "Done expanding %s\n", buf);
f4dc4d17 7600 }
10b3939b
DJ
7601
7602 item->per_cu->queued = 0;
7603 next_item = item->next;
7604 xfree (item);
7605 }
7606
7607 dwarf2_queue_tail = NULL;
45cfd468 7608
b4f54984 7609 if (dwarf_read_debug)
45cfd468
DE
7610 {
7611 fprintf_unfiltered (gdb_stdlog, "Done expanding symtabs of %s.\n",
4262abfb 7612 objfile_name (dwarf2_per_objfile->objfile));
45cfd468 7613 }
10b3939b
DJ
7614}
7615
7616/* Free all allocated queue entries. This function only releases anything if
7617 an error was thrown; if the queue was processed then it would have been
7618 freed as we went along. */
7619
7620static void
7621dwarf2_release_queue (void *dummy)
7622{
7623 struct dwarf2_queue_item *item, *last;
7624
7625 item = dwarf2_queue;
7626 while (item)
7627 {
7628 /* Anything still marked queued is likely to be in an
7629 inconsistent state, so discard it. */
7630 if (item->per_cu->queued)
7631 {
7632 if (item->per_cu->cu != NULL)
dee91e82 7633 free_one_cached_comp_unit (item->per_cu);
10b3939b
DJ
7634 item->per_cu->queued = 0;
7635 }
7636
7637 last = item;
7638 item = item->next;
7639 xfree (last);
7640 }
7641
7642 dwarf2_queue = dwarf2_queue_tail = NULL;
7643}
7644
7645/* Read in full symbols for PST, and anything it depends on. */
7646
c906108c 7647static void
fba45db2 7648psymtab_to_symtab_1 (struct partial_symtab *pst)
c906108c 7649{
10b3939b 7650 struct dwarf2_per_cu_data *per_cu;
aaa75496
JB
7651 int i;
7652
95554aad
TT
7653 if (pst->readin)
7654 return;
7655
aaa75496 7656 for (i = 0; i < pst->number_of_dependencies; i++)
95554aad
TT
7657 if (!pst->dependencies[i]->readin
7658 && pst->dependencies[i]->user == NULL)
aaa75496
JB
7659 {
7660 /* Inform about additional files that need to be read in. */
7661 if (info_verbose)
7662 {
a3f17187 7663 /* FIXME: i18n: Need to make this a single string. */
aaa75496
JB
7664 fputs_filtered (" ", gdb_stdout);
7665 wrap_here ("");
7666 fputs_filtered ("and ", gdb_stdout);
7667 wrap_here ("");
7668 printf_filtered ("%s...", pst->dependencies[i]->filename);
0963b4bd 7669 wrap_here (""); /* Flush output. */
aaa75496
JB
7670 gdb_flush (gdb_stdout);
7671 }
7672 psymtab_to_symtab_1 (pst->dependencies[i]);
7673 }
7674
9a3c8263 7675 per_cu = (struct dwarf2_per_cu_data *) pst->read_symtab_private;
10b3939b
DJ
7676
7677 if (per_cu == NULL)
aaa75496
JB
7678 {
7679 /* It's an include file, no symbols to read for it.
7680 Everything is in the parent symtab. */
7681 pst->readin = 1;
7682 return;
7683 }
c906108c 7684
a0f42c21 7685 dw2_do_instantiate_symtab (per_cu);
10b3939b
DJ
7686}
7687
dee91e82
DE
7688/* Trivial hash function for die_info: the hash value of a DIE
7689 is its offset in .debug_info for this objfile. */
10b3939b 7690
dee91e82
DE
7691static hashval_t
7692die_hash (const void *item)
10b3939b 7693{
9a3c8263 7694 const struct die_info *die = (const struct die_info *) item;
6502dd73 7695
dee91e82
DE
7696 return die->offset.sect_off;
7697}
63d06c5c 7698
dee91e82
DE
7699/* Trivial comparison function for die_info structures: two DIEs
7700 are equal if they have the same offset. */
98bfdba5 7701
dee91e82
DE
7702static int
7703die_eq (const void *item_lhs, const void *item_rhs)
7704{
9a3c8263
SM
7705 const struct die_info *die_lhs = (const struct die_info *) item_lhs;
7706 const struct die_info *die_rhs = (const struct die_info *) item_rhs;
c906108c 7707
dee91e82
DE
7708 return die_lhs->offset.sect_off == die_rhs->offset.sect_off;
7709}
c906108c 7710
dee91e82
DE
7711/* die_reader_func for load_full_comp_unit.
7712 This is identical to read_signatured_type_reader,
7713 but is kept separate for now. */
c906108c 7714
dee91e82
DE
7715static void
7716load_full_comp_unit_reader (const struct die_reader_specs *reader,
d521ce57 7717 const gdb_byte *info_ptr,
dee91e82
DE
7718 struct die_info *comp_unit_die,
7719 int has_children,
7720 void *data)
7721{
7722 struct dwarf2_cu *cu = reader->cu;
9a3c8263 7723 enum language *language_ptr = (enum language *) data;
6caca83c 7724
dee91e82
DE
7725 gdb_assert (cu->die_hash == NULL);
7726 cu->die_hash =
7727 htab_create_alloc_ex (cu->header.length / 12,
7728 die_hash,
7729 die_eq,
7730 NULL,
7731 &cu->comp_unit_obstack,
7732 hashtab_obstack_allocate,
7733 dummy_obstack_deallocate);
e142c38c 7734
dee91e82
DE
7735 if (has_children)
7736 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
7737 &info_ptr, comp_unit_die);
7738 cu->dies = comp_unit_die;
7739 /* comp_unit_die is not stored in die_hash, no need. */
10b3939b
DJ
7740
7741 /* We try not to read any attributes in this function, because not
9cdd5dbd 7742 all CUs needed for references have been loaded yet, and symbol
10b3939b 7743 table processing isn't initialized. But we have to set the CU language,
dee91e82
DE
7744 or we won't be able to build types correctly.
7745 Similarly, if we do not read the producer, we can not apply
7746 producer-specific interpretation. */
95554aad 7747 prepare_one_comp_unit (cu, cu->dies, *language_ptr);
dee91e82 7748}
10b3939b 7749
dee91e82 7750/* Load the DIEs associated with PER_CU into memory. */
a6c727b2 7751
dee91e82 7752static void
95554aad
TT
7753load_full_comp_unit (struct dwarf2_per_cu_data *this_cu,
7754 enum language pretend_language)
dee91e82 7755{
3019eac3 7756 gdb_assert (! this_cu->is_debug_types);
c5b7e1cb 7757
f4dc4d17
DE
7758 init_cutu_and_read_dies (this_cu, NULL, 1, 1,
7759 load_full_comp_unit_reader, &pretend_language);
10b3939b
DJ
7760}
7761
3da10d80
KS
7762/* Add a DIE to the delayed physname list. */
7763
7764static void
7765add_to_method_list (struct type *type, int fnfield_index, int index,
7766 const char *name, struct die_info *die,
7767 struct dwarf2_cu *cu)
7768{
7769 struct delayed_method_info mi;
7770 mi.type = type;
7771 mi.fnfield_index = fnfield_index;
7772 mi.index = index;
7773 mi.name = name;
7774 mi.die = die;
7775 VEC_safe_push (delayed_method_info, cu->method_list, &mi);
7776}
7777
7778/* A cleanup for freeing the delayed method list. */
7779
7780static void
7781free_delayed_list (void *ptr)
7782{
7783 struct dwarf2_cu *cu = (struct dwarf2_cu *) ptr;
7784 if (cu->method_list != NULL)
7785 {
7786 VEC_free (delayed_method_info, cu->method_list);
7787 cu->method_list = NULL;
7788 }
7789}
7790
7791/* Compute the physnames of any methods on the CU's method list.
7792
7793 The computation of method physnames is delayed in order to avoid the
7794 (bad) condition that one of the method's formal parameters is of an as yet
7795 incomplete type. */
7796
7797static void
7798compute_delayed_physnames (struct dwarf2_cu *cu)
7799{
7800 int i;
7801 struct delayed_method_info *mi;
7802 for (i = 0; VEC_iterate (delayed_method_info, cu->method_list, i, mi) ; ++i)
7803 {
1d06ead6 7804 const char *physname;
3da10d80
KS
7805 struct fn_fieldlist *fn_flp
7806 = &TYPE_FN_FIELDLIST (mi->type, mi->fnfield_index);
7d455152 7807 physname = dwarf2_physname (mi->name, mi->die, cu);
005e54bb
DE
7808 TYPE_FN_FIELD_PHYSNAME (fn_flp->fn_fields, mi->index)
7809 = physname ? physname : "";
3da10d80
KS
7810 }
7811}
7812
a766d390
DE
7813/* Go objects should be embedded in a DW_TAG_module DIE,
7814 and it's not clear if/how imported objects will appear.
7815 To keep Go support simple until that's worked out,
7816 go back through what we've read and create something usable.
7817 We could do this while processing each DIE, and feels kinda cleaner,
7818 but that way is more invasive.
7819 This is to, for example, allow the user to type "p var" or "b main"
7820 without having to specify the package name, and allow lookups
7821 of module.object to work in contexts that use the expression
7822 parser. */
7823
7824static void
7825fixup_go_packaging (struct dwarf2_cu *cu)
7826{
7827 char *package_name = NULL;
7828 struct pending *list;
7829 int i;
7830
7831 for (list = global_symbols; list != NULL; list = list->next)
7832 {
7833 for (i = 0; i < list->nsyms; ++i)
7834 {
7835 struct symbol *sym = list->symbol[i];
7836
7837 if (SYMBOL_LANGUAGE (sym) == language_go
7838 && SYMBOL_CLASS (sym) == LOC_BLOCK)
7839 {
7840 char *this_package_name = go_symbol_package_name (sym);
7841
7842 if (this_package_name == NULL)
7843 continue;
7844 if (package_name == NULL)
7845 package_name = this_package_name;
7846 else
7847 {
7848 if (strcmp (package_name, this_package_name) != 0)
7849 complaint (&symfile_complaints,
7850 _("Symtab %s has objects from two different Go packages: %s and %s"),
08be3fe3
DE
7851 (symbol_symtab (sym) != NULL
7852 ? symtab_to_filename_for_display
7853 (symbol_symtab (sym))
4262abfb 7854 : objfile_name (cu->objfile)),
a766d390
DE
7855 this_package_name, package_name);
7856 xfree (this_package_name);
7857 }
7858 }
7859 }
7860 }
7861
7862 if (package_name != NULL)
7863 {
7864 struct objfile *objfile = cu->objfile;
34a68019 7865 const char *saved_package_name
224c3ddb
SM
7866 = (const char *) obstack_copy0 (&objfile->per_bfd->storage_obstack,
7867 package_name,
7868 strlen (package_name));
19f392bc
UW
7869 struct type *type = init_type (objfile, TYPE_CODE_MODULE, 0,
7870 saved_package_name);
a766d390
DE
7871 struct symbol *sym;
7872
7873 TYPE_TAG_NAME (type) = TYPE_NAME (type);
7874
e623cf5d 7875 sym = allocate_symbol (objfile);
f85f34ed 7876 SYMBOL_SET_LANGUAGE (sym, language_go, &objfile->objfile_obstack);
86f62fd7
TT
7877 SYMBOL_SET_NAMES (sym, saved_package_name,
7878 strlen (saved_package_name), 0, objfile);
a766d390
DE
7879 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
7880 e.g., "main" finds the "main" module and not C's main(). */
7881 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
f1e6e072 7882 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
a766d390
DE
7883 SYMBOL_TYPE (sym) = type;
7884
7885 add_symbol_to_list (sym, &global_symbols);
7886
7887 xfree (package_name);
7888 }
7889}
7890
95554aad
TT
7891/* Return the symtab for PER_CU. This works properly regardless of
7892 whether we're using the index or psymtabs. */
7893
43f3e411
DE
7894static struct compunit_symtab *
7895get_compunit_symtab (struct dwarf2_per_cu_data *per_cu)
95554aad
TT
7896{
7897 return (dwarf2_per_objfile->using_index
43f3e411
DE
7898 ? per_cu->v.quick->compunit_symtab
7899 : per_cu->v.psymtab->compunit_symtab);
95554aad
TT
7900}
7901
7902/* A helper function for computing the list of all symbol tables
7903 included by PER_CU. */
7904
7905static void
43f3e411 7906recursively_compute_inclusions (VEC (compunit_symtab_ptr) **result,
ec94af83 7907 htab_t all_children, htab_t all_type_symtabs,
f9125b6c 7908 struct dwarf2_per_cu_data *per_cu,
43f3e411 7909 struct compunit_symtab *immediate_parent)
95554aad
TT
7910{
7911 void **slot;
7912 int ix;
43f3e411 7913 struct compunit_symtab *cust;
95554aad
TT
7914 struct dwarf2_per_cu_data *iter;
7915
7916 slot = htab_find_slot (all_children, per_cu, INSERT);
7917 if (*slot != NULL)
7918 {
7919 /* This inclusion and its children have been processed. */
7920 return;
7921 }
7922
7923 *slot = per_cu;
7924 /* Only add a CU if it has a symbol table. */
43f3e411
DE
7925 cust = get_compunit_symtab (per_cu);
7926 if (cust != NULL)
ec94af83
DE
7927 {
7928 /* If this is a type unit only add its symbol table if we haven't
7929 seen it yet (type unit per_cu's can share symtabs). */
7930 if (per_cu->is_debug_types)
7931 {
43f3e411 7932 slot = htab_find_slot (all_type_symtabs, cust, INSERT);
ec94af83
DE
7933 if (*slot == NULL)
7934 {
43f3e411
DE
7935 *slot = cust;
7936 VEC_safe_push (compunit_symtab_ptr, *result, cust);
7937 if (cust->user == NULL)
7938 cust->user = immediate_parent;
ec94af83
DE
7939 }
7940 }
7941 else
f9125b6c 7942 {
43f3e411
DE
7943 VEC_safe_push (compunit_symtab_ptr, *result, cust);
7944 if (cust->user == NULL)
7945 cust->user = immediate_parent;
f9125b6c 7946 }
ec94af83 7947 }
95554aad
TT
7948
7949 for (ix = 0;
796a7ff8 7950 VEC_iterate (dwarf2_per_cu_ptr, per_cu->imported_symtabs, ix, iter);
95554aad 7951 ++ix)
ec94af83
DE
7952 {
7953 recursively_compute_inclusions (result, all_children,
43f3e411 7954 all_type_symtabs, iter, cust);
ec94af83 7955 }
95554aad
TT
7956}
7957
43f3e411 7958/* Compute the compunit_symtab 'includes' fields for the compunit_symtab of
95554aad
TT
7959 PER_CU. */
7960
7961static void
43f3e411 7962compute_compunit_symtab_includes (struct dwarf2_per_cu_data *per_cu)
95554aad 7963{
f4dc4d17
DE
7964 gdb_assert (! per_cu->is_debug_types);
7965
796a7ff8 7966 if (!VEC_empty (dwarf2_per_cu_ptr, per_cu->imported_symtabs))
95554aad
TT
7967 {
7968 int ix, len;
ec94af83 7969 struct dwarf2_per_cu_data *per_cu_iter;
43f3e411
DE
7970 struct compunit_symtab *compunit_symtab_iter;
7971 VEC (compunit_symtab_ptr) *result_symtabs = NULL;
ec94af83 7972 htab_t all_children, all_type_symtabs;
43f3e411 7973 struct compunit_symtab *cust = get_compunit_symtab (per_cu);
95554aad
TT
7974
7975 /* If we don't have a symtab, we can just skip this case. */
43f3e411 7976 if (cust == NULL)
95554aad
TT
7977 return;
7978
7979 all_children = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
7980 NULL, xcalloc, xfree);
ec94af83
DE
7981 all_type_symtabs = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
7982 NULL, xcalloc, xfree);
95554aad
TT
7983
7984 for (ix = 0;
796a7ff8 7985 VEC_iterate (dwarf2_per_cu_ptr, per_cu->imported_symtabs,
ec94af83 7986 ix, per_cu_iter);
95554aad 7987 ++ix)
ec94af83
DE
7988 {
7989 recursively_compute_inclusions (&result_symtabs, all_children,
f9125b6c 7990 all_type_symtabs, per_cu_iter,
43f3e411 7991 cust);
ec94af83 7992 }
95554aad 7993
ec94af83 7994 /* Now we have a transitive closure of all the included symtabs. */
43f3e411
DE
7995 len = VEC_length (compunit_symtab_ptr, result_symtabs);
7996 cust->includes
8d749320
SM
7997 = XOBNEWVEC (&dwarf2_per_objfile->objfile->objfile_obstack,
7998 struct compunit_symtab *, len + 1);
95554aad 7999 for (ix = 0;
43f3e411
DE
8000 VEC_iterate (compunit_symtab_ptr, result_symtabs, ix,
8001 compunit_symtab_iter);
95554aad 8002 ++ix)
43f3e411
DE
8003 cust->includes[ix] = compunit_symtab_iter;
8004 cust->includes[len] = NULL;
95554aad 8005
43f3e411 8006 VEC_free (compunit_symtab_ptr, result_symtabs);
95554aad 8007 htab_delete (all_children);
ec94af83 8008 htab_delete (all_type_symtabs);
95554aad
TT
8009 }
8010}
8011
8012/* Compute the 'includes' field for the symtabs of all the CUs we just
8013 read. */
8014
8015static void
8016process_cu_includes (void)
8017{
8018 int ix;
8019 struct dwarf2_per_cu_data *iter;
8020
8021 for (ix = 0;
8022 VEC_iterate (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus,
8023 ix, iter);
8024 ++ix)
f4dc4d17
DE
8025 {
8026 if (! iter->is_debug_types)
43f3e411 8027 compute_compunit_symtab_includes (iter);
f4dc4d17 8028 }
95554aad
TT
8029
8030 VEC_free (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus);
8031}
8032
9cdd5dbd 8033/* Generate full symbol information for PER_CU, whose DIEs have
10b3939b
DJ
8034 already been loaded into memory. */
8035
8036static void
95554aad
TT
8037process_full_comp_unit (struct dwarf2_per_cu_data *per_cu,
8038 enum language pretend_language)
10b3939b 8039{
10b3939b 8040 struct dwarf2_cu *cu = per_cu->cu;
9291a0cd 8041 struct objfile *objfile = per_cu->objfile;
3e29f34a 8042 struct gdbarch *gdbarch = get_objfile_arch (objfile);
10b3939b 8043 CORE_ADDR lowpc, highpc;
43f3e411 8044 struct compunit_symtab *cust;
3da10d80 8045 struct cleanup *back_to, *delayed_list_cleanup;
10b3939b 8046 CORE_ADDR baseaddr;
4359dff1 8047 struct block *static_block;
3e29f34a 8048 CORE_ADDR addr;
10b3939b
DJ
8049
8050 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
8051
10b3939b
DJ
8052 buildsym_init ();
8053 back_to = make_cleanup (really_free_pendings, NULL);
3da10d80 8054 delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
10b3939b
DJ
8055
8056 cu->list_in_scope = &file_symbols;
c906108c 8057
95554aad
TT
8058 cu->language = pretend_language;
8059 cu->language_defn = language_def (cu->language);
8060
c906108c 8061 /* Do line number decoding in read_file_scope () */
10b3939b 8062 process_die (cu->dies, cu);
c906108c 8063
a766d390
DE
8064 /* For now fudge the Go package. */
8065 if (cu->language == language_go)
8066 fixup_go_packaging (cu);
8067
3da10d80
KS
8068 /* Now that we have processed all the DIEs in the CU, all the types
8069 should be complete, and it should now be safe to compute all of the
8070 physnames. */
8071 compute_delayed_physnames (cu);
8072 do_cleanups (delayed_list_cleanup);
8073
fae299cd
DC
8074 /* Some compilers don't define a DW_AT_high_pc attribute for the
8075 compilation unit. If the DW_AT_high_pc is missing, synthesize
8076 it, by scanning the DIE's below the compilation unit. */
10b3939b 8077 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
c906108c 8078
3e29f34a
MR
8079 addr = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
8080 static_block = end_symtab_get_static_block (addr, 0, 1);
4359dff1
JK
8081
8082 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
8083 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
8084 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
8085 addrmap to help ensure it has an accurate map of pc values belonging to
8086 this comp unit. */
8087 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
8088
43f3e411
DE
8089 cust = end_symtab_from_static_block (static_block,
8090 SECT_OFF_TEXT (objfile), 0);
c906108c 8091
43f3e411 8092 if (cust != NULL)
c906108c 8093 {
df15bd07 8094 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
4632c0d0 8095
8be455d7
JK
8096 /* Set symtab language to language from DW_AT_language. If the
8097 compilation is from a C file generated by language preprocessors, do
8098 not set the language if it was already deduced by start_subfile. */
43f3e411 8099 if (!(cu->language == language_c
40e3ad0e 8100 && COMPUNIT_FILETABS (cust)->language != language_unknown))
43f3e411 8101 COMPUNIT_FILETABS (cust)->language = cu->language;
8be455d7
JK
8102
8103 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
8104 produce DW_AT_location with location lists but it can be possibly
ab260dad
JK
8105 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
8106 there were bugs in prologue debug info, fixed later in GCC-4.5
8107 by "unwind info for epilogues" patch (which is not directly related).
8be455d7
JK
8108
8109 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
8110 needed, it would be wrong due to missing DW_AT_producer there.
8111
8112 Still one can confuse GDB by using non-standard GCC compilation
8113 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
8114 */
ab260dad 8115 if (cu->has_loclist && gcc_4_minor >= 5)
43f3e411 8116 cust->locations_valid = 1;
e0d00bc7
JK
8117
8118 if (gcc_4_minor >= 5)
43f3e411 8119 cust->epilogue_unwind_valid = 1;
96408a79 8120
43f3e411 8121 cust->call_site_htab = cu->call_site_htab;
c906108c 8122 }
9291a0cd
TT
8123
8124 if (dwarf2_per_objfile->using_index)
43f3e411 8125 per_cu->v.quick->compunit_symtab = cust;
9291a0cd
TT
8126 else
8127 {
8128 struct partial_symtab *pst = per_cu->v.psymtab;
43f3e411 8129 pst->compunit_symtab = cust;
9291a0cd
TT
8130 pst->readin = 1;
8131 }
c906108c 8132
95554aad
TT
8133 /* Push it for inclusion processing later. */
8134 VEC_safe_push (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus, per_cu);
8135
c906108c 8136 do_cleanups (back_to);
f4dc4d17 8137}
45cfd468 8138
f4dc4d17
DE
8139/* Generate full symbol information for type unit PER_CU, whose DIEs have
8140 already been loaded into memory. */
8141
8142static void
8143process_full_type_unit (struct dwarf2_per_cu_data *per_cu,
8144 enum language pretend_language)
8145{
8146 struct dwarf2_cu *cu = per_cu->cu;
8147 struct objfile *objfile = per_cu->objfile;
43f3e411 8148 struct compunit_symtab *cust;
f4dc4d17 8149 struct cleanup *back_to, *delayed_list_cleanup;
0186c6a7
DE
8150 struct signatured_type *sig_type;
8151
8152 gdb_assert (per_cu->is_debug_types);
8153 sig_type = (struct signatured_type *) per_cu;
f4dc4d17
DE
8154
8155 buildsym_init ();
8156 back_to = make_cleanup (really_free_pendings, NULL);
8157 delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
8158
8159 cu->list_in_scope = &file_symbols;
8160
8161 cu->language = pretend_language;
8162 cu->language_defn = language_def (cu->language);
8163
8164 /* The symbol tables are set up in read_type_unit_scope. */
8165 process_die (cu->dies, cu);
8166
8167 /* For now fudge the Go package. */
8168 if (cu->language == language_go)
8169 fixup_go_packaging (cu);
8170
8171 /* Now that we have processed all the DIEs in the CU, all the types
8172 should be complete, and it should now be safe to compute all of the
8173 physnames. */
8174 compute_delayed_physnames (cu);
8175 do_cleanups (delayed_list_cleanup);
8176
8177 /* TUs share symbol tables.
8178 If this is the first TU to use this symtab, complete the construction
094b34ac
DE
8179 of it with end_expandable_symtab. Otherwise, complete the addition of
8180 this TU's symbols to the existing symtab. */
43f3e411 8181 if (sig_type->type_unit_group->compunit_symtab == NULL)
45cfd468 8182 {
43f3e411
DE
8183 cust = end_expandable_symtab (0, SECT_OFF_TEXT (objfile));
8184 sig_type->type_unit_group->compunit_symtab = cust;
f4dc4d17 8185
43f3e411 8186 if (cust != NULL)
f4dc4d17
DE
8187 {
8188 /* Set symtab language to language from DW_AT_language. If the
8189 compilation is from a C file generated by language preprocessors,
8190 do not set the language if it was already deduced by
8191 start_subfile. */
43f3e411
DE
8192 if (!(cu->language == language_c
8193 && COMPUNIT_FILETABS (cust)->language != language_c))
8194 COMPUNIT_FILETABS (cust)->language = cu->language;
f4dc4d17
DE
8195 }
8196 }
8197 else
8198 {
0ab9ce85 8199 augment_type_symtab ();
43f3e411 8200 cust = sig_type->type_unit_group->compunit_symtab;
f4dc4d17
DE
8201 }
8202
8203 if (dwarf2_per_objfile->using_index)
43f3e411 8204 per_cu->v.quick->compunit_symtab = cust;
f4dc4d17
DE
8205 else
8206 {
8207 struct partial_symtab *pst = per_cu->v.psymtab;
43f3e411 8208 pst->compunit_symtab = cust;
f4dc4d17 8209 pst->readin = 1;
45cfd468 8210 }
f4dc4d17
DE
8211
8212 do_cleanups (back_to);
c906108c
SS
8213}
8214
95554aad
TT
8215/* Process an imported unit DIE. */
8216
8217static void
8218process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
8219{
8220 struct attribute *attr;
8221
f4dc4d17
DE
8222 /* For now we don't handle imported units in type units. */
8223 if (cu->per_cu->is_debug_types)
8224 {
8225 error (_("Dwarf Error: DW_TAG_imported_unit is not"
8226 " supported in type units [in module %s]"),
4262abfb 8227 objfile_name (cu->objfile));
f4dc4d17
DE
8228 }
8229
95554aad
TT
8230 attr = dwarf2_attr (die, DW_AT_import, cu);
8231 if (attr != NULL)
8232 {
8233 struct dwarf2_per_cu_data *per_cu;
95554aad 8234 sect_offset offset;
36586728 8235 int is_dwz;
95554aad
TT
8236
8237 offset = dwarf2_get_ref_die_offset (attr);
36586728
TT
8238 is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
8239 per_cu = dwarf2_find_containing_comp_unit (offset, is_dwz, cu->objfile);
95554aad 8240
69d751e3 8241 /* If necessary, add it to the queue and load its DIEs. */
95554aad
TT
8242 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
8243 load_full_comp_unit (per_cu, cu->language);
8244
796a7ff8 8245 VEC_safe_push (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs,
95554aad
TT
8246 per_cu);
8247 }
8248}
8249
adde2bff
DE
8250/* Reset the in_process bit of a die. */
8251
8252static void
8253reset_die_in_process (void *arg)
8254{
9a3c8263 8255 struct die_info *die = (struct die_info *) arg;
8c3cb9fa 8256
adde2bff
DE
8257 die->in_process = 0;
8258}
8259
c906108c
SS
8260/* Process a die and its children. */
8261
8262static void
e7c27a73 8263process_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 8264{
adde2bff
DE
8265 struct cleanup *in_process;
8266
8267 /* We should only be processing those not already in process. */
8268 gdb_assert (!die->in_process);
8269
8270 die->in_process = 1;
8271 in_process = make_cleanup (reset_die_in_process,die);
8272
c906108c
SS
8273 switch (die->tag)
8274 {
8275 case DW_TAG_padding:
8276 break;
8277 case DW_TAG_compile_unit:
95554aad 8278 case DW_TAG_partial_unit:
e7c27a73 8279 read_file_scope (die, cu);
c906108c 8280 break;
348e048f
DE
8281 case DW_TAG_type_unit:
8282 read_type_unit_scope (die, cu);
8283 break;
c906108c 8284 case DW_TAG_subprogram:
c906108c 8285 case DW_TAG_inlined_subroutine:
edb3359d 8286 read_func_scope (die, cu);
c906108c
SS
8287 break;
8288 case DW_TAG_lexical_block:
14898363
L
8289 case DW_TAG_try_block:
8290 case DW_TAG_catch_block:
e7c27a73 8291 read_lexical_block_scope (die, cu);
c906108c 8292 break;
96408a79
SA
8293 case DW_TAG_GNU_call_site:
8294 read_call_site_scope (die, cu);
8295 break;
c906108c 8296 case DW_TAG_class_type:
680b30c7 8297 case DW_TAG_interface_type:
c906108c
SS
8298 case DW_TAG_structure_type:
8299 case DW_TAG_union_type:
134d01f1 8300 process_structure_scope (die, cu);
c906108c
SS
8301 break;
8302 case DW_TAG_enumeration_type:
134d01f1 8303 process_enumeration_scope (die, cu);
c906108c 8304 break;
134d01f1 8305
f792889a
DJ
8306 /* These dies have a type, but processing them does not create
8307 a symbol or recurse to process the children. Therefore we can
8308 read them on-demand through read_type_die. */
c906108c 8309 case DW_TAG_subroutine_type:
72019c9c 8310 case DW_TAG_set_type:
c906108c 8311 case DW_TAG_array_type:
c906108c 8312 case DW_TAG_pointer_type:
c906108c 8313 case DW_TAG_ptr_to_member_type:
c906108c 8314 case DW_TAG_reference_type:
c906108c 8315 case DW_TAG_string_type:
c906108c 8316 break;
134d01f1 8317
c906108c 8318 case DW_TAG_base_type:
a02abb62 8319 case DW_TAG_subrange_type:
cb249c71 8320 case DW_TAG_typedef:
134d01f1
DJ
8321 /* Add a typedef symbol for the type definition, if it has a
8322 DW_AT_name. */
f792889a 8323 new_symbol (die, read_type_die (die, cu), cu);
a02abb62 8324 break;
c906108c 8325 case DW_TAG_common_block:
e7c27a73 8326 read_common_block (die, cu);
c906108c
SS
8327 break;
8328 case DW_TAG_common_inclusion:
8329 break;
d9fa45fe 8330 case DW_TAG_namespace:
4d4ec4e5 8331 cu->processing_has_namespace_info = 1;
e7c27a73 8332 read_namespace (die, cu);
d9fa45fe 8333 break;
5d7cb8df 8334 case DW_TAG_module:
4d4ec4e5 8335 cu->processing_has_namespace_info = 1;
5d7cb8df
JK
8336 read_module (die, cu);
8337 break;
d9fa45fe 8338 case DW_TAG_imported_declaration:
74921315
KS
8339 cu->processing_has_namespace_info = 1;
8340 if (read_namespace_alias (die, cu))
8341 break;
8342 /* The declaration is not a global namespace alias: fall through. */
d9fa45fe 8343 case DW_TAG_imported_module:
4d4ec4e5 8344 cu->processing_has_namespace_info = 1;
27aa8d6a
SW
8345 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
8346 || cu->language != language_fortran))
8347 complaint (&symfile_complaints, _("Tag '%s' has unexpected children"),
8348 dwarf_tag_name (die->tag));
8349 read_import_statement (die, cu);
d9fa45fe 8350 break;
95554aad
TT
8351
8352 case DW_TAG_imported_unit:
8353 process_imported_unit_die (die, cu);
8354 break;
8355
c906108c 8356 default:
e7c27a73 8357 new_symbol (die, NULL, cu);
c906108c
SS
8358 break;
8359 }
adde2bff
DE
8360
8361 do_cleanups (in_process);
c906108c 8362}
ca69b9e6
DE
8363\f
8364/* DWARF name computation. */
c906108c 8365
94af9270
KS
8366/* A helper function for dwarf2_compute_name which determines whether DIE
8367 needs to have the name of the scope prepended to the name listed in the
8368 die. */
8369
8370static int
8371die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
8372{
1c809c68
TT
8373 struct attribute *attr;
8374
94af9270
KS
8375 switch (die->tag)
8376 {
8377 case DW_TAG_namespace:
8378 case DW_TAG_typedef:
8379 case DW_TAG_class_type:
8380 case DW_TAG_interface_type:
8381 case DW_TAG_structure_type:
8382 case DW_TAG_union_type:
8383 case DW_TAG_enumeration_type:
8384 case DW_TAG_enumerator:
8385 case DW_TAG_subprogram:
08a76f8a 8386 case DW_TAG_inlined_subroutine:
94af9270 8387 case DW_TAG_member:
74921315 8388 case DW_TAG_imported_declaration:
94af9270
KS
8389 return 1;
8390
8391 case DW_TAG_variable:
c2b0a229 8392 case DW_TAG_constant:
94af9270
KS
8393 /* We only need to prefix "globally" visible variables. These include
8394 any variable marked with DW_AT_external or any variable that
8395 lives in a namespace. [Variables in anonymous namespaces
8396 require prefixing, but they are not DW_AT_external.] */
8397
8398 if (dwarf2_attr (die, DW_AT_specification, cu))
8399 {
8400 struct dwarf2_cu *spec_cu = cu;
9a619af0 8401
94af9270
KS
8402 return die_needs_namespace (die_specification (die, &spec_cu),
8403 spec_cu);
8404 }
8405
1c809c68 8406 attr = dwarf2_attr (die, DW_AT_external, cu);
f55ee35c
JK
8407 if (attr == NULL && die->parent->tag != DW_TAG_namespace
8408 && die->parent->tag != DW_TAG_module)
1c809c68
TT
8409 return 0;
8410 /* A variable in a lexical block of some kind does not need a
8411 namespace, even though in C++ such variables may be external
8412 and have a mangled name. */
8413 if (die->parent->tag == DW_TAG_lexical_block
8414 || die->parent->tag == DW_TAG_try_block
1054b214
TT
8415 || die->parent->tag == DW_TAG_catch_block
8416 || die->parent->tag == DW_TAG_subprogram)
1c809c68
TT
8417 return 0;
8418 return 1;
94af9270
KS
8419
8420 default:
8421 return 0;
8422 }
8423}
8424
98bfdba5
PA
8425/* Retrieve the last character from a mem_file. */
8426
8427static void
8428do_ui_file_peek_last (void *object, const char *buffer, long length)
8429{
8430 char *last_char_p = (char *) object;
8431
8432 if (length > 0)
8433 *last_char_p = buffer[length - 1];
8434}
8435
94af9270 8436/* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
a766d390
DE
8437 compute the physname for the object, which include a method's:
8438 - formal parameters (C++/Java),
8439 - receiver type (Go),
8440 - return type (Java).
8441
8442 The term "physname" is a bit confusing.
8443 For C++, for example, it is the demangled name.
8444 For Go, for example, it's the mangled name.
94af9270 8445
af6b7be1
JB
8446 For Ada, return the DIE's linkage name rather than the fully qualified
8447 name. PHYSNAME is ignored..
8448
94af9270
KS
8449 The result is allocated on the objfile_obstack and canonicalized. */
8450
8451static const char *
15d034d0
TT
8452dwarf2_compute_name (const char *name,
8453 struct die_info *die, struct dwarf2_cu *cu,
94af9270
KS
8454 int physname)
8455{
bb5ed363
DE
8456 struct objfile *objfile = cu->objfile;
8457
94af9270
KS
8458 if (name == NULL)
8459 name = dwarf2_name (die, cu);
8460
2ee7123e
DE
8461 /* For Fortran GDB prefers DW_AT_*linkage_name for the physname if present
8462 but otherwise compute it by typename_concat inside GDB.
8463 FIXME: Actually this is not really true, or at least not always true.
8464 It's all very confusing. SYMBOL_SET_NAMES doesn't try to demangle
8465 Fortran names because there is no mangling standard. So new_symbol_full
8466 will set the demangled name to the result of dwarf2_full_name, and it is
8467 the demangled name that GDB uses if it exists. */
f55ee35c
JK
8468 if (cu->language == language_ada
8469 || (cu->language == language_fortran && physname))
8470 {
8471 /* For Ada unit, we prefer the linkage name over the name, as
8472 the former contains the exported name, which the user expects
8473 to be able to reference. Ideally, we want the user to be able
8474 to reference this entity using either natural or linkage name,
8475 but we haven't started looking at this enhancement yet. */
2ee7123e 8476 const char *linkage_name;
f55ee35c 8477
2ee7123e
DE
8478 linkage_name = dwarf2_string_attr (die, DW_AT_linkage_name, cu);
8479 if (linkage_name == NULL)
8480 linkage_name = dwarf2_string_attr (die, DW_AT_MIPS_linkage_name, cu);
8481 if (linkage_name != NULL)
8482 return linkage_name;
f55ee35c
JK
8483 }
8484
94af9270
KS
8485 /* These are the only languages we know how to qualify names in. */
8486 if (name != NULL
f55ee35c 8487 && (cu->language == language_cplus || cu->language == language_java
c44af4eb
TT
8488 || cu->language == language_fortran || cu->language == language_d
8489 || cu->language == language_rust))
94af9270
KS
8490 {
8491 if (die_needs_namespace (die, cu))
8492 {
8493 long length;
0d5cff50 8494 const char *prefix;
94af9270 8495 struct ui_file *buf;
34a68019
TT
8496 char *intermediate_name;
8497 const char *canonical_name = NULL;
94af9270
KS
8498
8499 prefix = determine_prefix (die, cu);
8500 buf = mem_fileopen ();
8501 if (*prefix != '\0')
8502 {
f55ee35c
JK
8503 char *prefixed_name = typename_concat (NULL, prefix, name,
8504 physname, cu);
9a619af0 8505
94af9270
KS
8506 fputs_unfiltered (prefixed_name, buf);
8507 xfree (prefixed_name);
8508 }
8509 else
62d5b8da 8510 fputs_unfiltered (name, buf);
94af9270 8511
98bfdba5
PA
8512 /* Template parameters may be specified in the DIE's DW_AT_name, or
8513 as children with DW_TAG_template_type_param or
8514 DW_TAG_value_type_param. If the latter, add them to the name
8515 here. If the name already has template parameters, then
8516 skip this step; some versions of GCC emit both, and
8517 it is more efficient to use the pre-computed name.
8518
8519 Something to keep in mind about this process: it is very
8520 unlikely, or in some cases downright impossible, to produce
8521 something that will match the mangled name of a function.
8522 If the definition of the function has the same debug info,
8523 we should be able to match up with it anyway. But fallbacks
8524 using the minimal symbol, for instance to find a method
8525 implemented in a stripped copy of libstdc++, will not work.
8526 If we do not have debug info for the definition, we will have to
8527 match them up some other way.
8528
8529 When we do name matching there is a related problem with function
8530 templates; two instantiated function templates are allowed to
8531 differ only by their return types, which we do not add here. */
8532
8533 if (cu->language == language_cplus && strchr (name, '<') == NULL)
8534 {
8535 struct attribute *attr;
8536 struct die_info *child;
8537 int first = 1;
8538
8539 die->building_fullname = 1;
8540
8541 for (child = die->child; child != NULL; child = child->sibling)
8542 {
8543 struct type *type;
12df843f 8544 LONGEST value;
d521ce57 8545 const gdb_byte *bytes;
98bfdba5
PA
8546 struct dwarf2_locexpr_baton *baton;
8547 struct value *v;
8548
8549 if (child->tag != DW_TAG_template_type_param
8550 && child->tag != DW_TAG_template_value_param)
8551 continue;
8552
8553 if (first)
8554 {
8555 fputs_unfiltered ("<", buf);
8556 first = 0;
8557 }
8558 else
8559 fputs_unfiltered (", ", buf);
8560
8561 attr = dwarf2_attr (child, DW_AT_type, cu);
8562 if (attr == NULL)
8563 {
8564 complaint (&symfile_complaints,
8565 _("template parameter missing DW_AT_type"));
8566 fputs_unfiltered ("UNKNOWN_TYPE", buf);
8567 continue;
8568 }
8569 type = die_type (child, cu);
8570
8571 if (child->tag == DW_TAG_template_type_param)
8572 {
79d43c61 8573 c_print_type (type, "", buf, -1, 0, &type_print_raw_options);
98bfdba5
PA
8574 continue;
8575 }
8576
8577 attr = dwarf2_attr (child, DW_AT_const_value, cu);
8578 if (attr == NULL)
8579 {
8580 complaint (&symfile_complaints,
3e43a32a
MS
8581 _("template parameter missing "
8582 "DW_AT_const_value"));
98bfdba5
PA
8583 fputs_unfiltered ("UNKNOWN_VALUE", buf);
8584 continue;
8585 }
8586
8587 dwarf2_const_value_attr (attr, type, name,
8588 &cu->comp_unit_obstack, cu,
8589 &value, &bytes, &baton);
8590
8591 if (TYPE_NOSIGN (type))
8592 /* GDB prints characters as NUMBER 'CHAR'. If that's
8593 changed, this can use value_print instead. */
8594 c_printchar (value, type, buf);
8595 else
8596 {
8597 struct value_print_options opts;
8598
8599 if (baton != NULL)
8600 v = dwarf2_evaluate_loc_desc (type, NULL,
8601 baton->data,
8602 baton->size,
8603 baton->per_cu);
8604 else if (bytes != NULL)
8605 {
8606 v = allocate_value (type);
8607 memcpy (value_contents_writeable (v), bytes,
8608 TYPE_LENGTH (type));
8609 }
8610 else
8611 v = value_from_longest (type, value);
8612
3e43a32a
MS
8613 /* Specify decimal so that we do not depend on
8614 the radix. */
98bfdba5
PA
8615 get_formatted_print_options (&opts, 'd');
8616 opts.raw = 1;
8617 value_print (v, buf, &opts);
8618 release_value (v);
8619 value_free (v);
8620 }
8621 }
8622
8623 die->building_fullname = 0;
8624
8625 if (!first)
8626 {
8627 /* Close the argument list, with a space if necessary
8628 (nested templates). */
8629 char last_char = '\0';
8630 ui_file_put (buf, do_ui_file_peek_last, &last_char);
8631 if (last_char == '>')
8632 fputs_unfiltered (" >", buf);
8633 else
8634 fputs_unfiltered (">", buf);
8635 }
8636 }
8637
94af9270
KS
8638 /* For Java and C++ methods, append formal parameter type
8639 information, if PHYSNAME. */
6e70227d 8640
94af9270
KS
8641 if (physname && die->tag == DW_TAG_subprogram
8642 && (cu->language == language_cplus
8643 || cu->language == language_java))
8644 {
8645 struct type *type = read_type_die (die, cu);
8646
79d43c61
TT
8647 c_type_print_args (type, buf, 1, cu->language,
8648 &type_print_raw_options);
94af9270
KS
8649
8650 if (cu->language == language_java)
8651 {
8652 /* For java, we must append the return type to method
0963b4bd 8653 names. */
94af9270
KS
8654 if (die->tag == DW_TAG_subprogram)
8655 java_print_type (TYPE_TARGET_TYPE (type), "", buf,
79d43c61 8656 0, 0, &type_print_raw_options);
94af9270
KS
8657 }
8658 else if (cu->language == language_cplus)
8659 {
60430eff
DJ
8660 /* Assume that an artificial first parameter is
8661 "this", but do not crash if it is not. RealView
8662 marks unnamed (and thus unused) parameters as
8663 artificial; there is no way to differentiate
8664 the two cases. */
94af9270
KS
8665 if (TYPE_NFIELDS (type) > 0
8666 && TYPE_FIELD_ARTIFICIAL (type, 0)
60430eff 8667 && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
3e43a32a
MS
8668 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
8669 0))))
94af9270
KS
8670 fputs_unfiltered (" const", buf);
8671 }
8672 }
8673
34a68019 8674 intermediate_name = ui_file_xstrdup (buf, &length);
94af9270
KS
8675 ui_file_delete (buf);
8676
8677 if (cu->language == language_cplus)
34a68019
TT
8678 canonical_name
8679 = dwarf2_canonicalize_name (intermediate_name, cu,
8680 &objfile->per_bfd->storage_obstack);
8681
8682 /* If we only computed INTERMEDIATE_NAME, or if
8683 INTERMEDIATE_NAME is already canonical, then we need to
8684 copy it to the appropriate obstack. */
8685 if (canonical_name == NULL || canonical_name == intermediate_name)
224c3ddb
SM
8686 name = ((const char *)
8687 obstack_copy0 (&objfile->per_bfd->storage_obstack,
8688 intermediate_name,
8689 strlen (intermediate_name)));
34a68019
TT
8690 else
8691 name = canonical_name;
9a619af0 8692
34a68019 8693 xfree (intermediate_name);
94af9270
KS
8694 }
8695 }
8696
8697 return name;
8698}
8699
0114d602
DJ
8700/* Return the fully qualified name of DIE, based on its DW_AT_name.
8701 If scope qualifiers are appropriate they will be added. The result
34a68019 8702 will be allocated on the storage_obstack, or NULL if the DIE does
94af9270
KS
8703 not have a name. NAME may either be from a previous call to
8704 dwarf2_name or NULL.
8705
0963b4bd 8706 The output string will be canonicalized (if C++/Java). */
0114d602
DJ
8707
8708static const char *
15d034d0 8709dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
0114d602 8710{
94af9270
KS
8711 return dwarf2_compute_name (name, die, cu, 0);
8712}
0114d602 8713
94af9270
KS
8714/* Construct a physname for the given DIE in CU. NAME may either be
8715 from a previous call to dwarf2_name or NULL. The result will be
8716 allocated on the objfile_objstack or NULL if the DIE does not have a
8717 name.
0114d602 8718
94af9270 8719 The output string will be canonicalized (if C++/Java). */
0114d602 8720
94af9270 8721static const char *
15d034d0 8722dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
94af9270 8723{
bb5ed363 8724 struct objfile *objfile = cu->objfile;
900e11f9
JK
8725 const char *retval, *mangled = NULL, *canon = NULL;
8726 struct cleanup *back_to;
8727 int need_copy = 1;
8728
8729 /* In this case dwarf2_compute_name is just a shortcut not building anything
8730 on its own. */
8731 if (!die_needs_namespace (die, cu))
8732 return dwarf2_compute_name (name, die, cu, 1);
8733
8734 back_to = make_cleanup (null_cleanup, NULL);
8735
7d45c7c3
KB
8736 mangled = dwarf2_string_attr (die, DW_AT_linkage_name, cu);
8737 if (mangled == NULL)
8738 mangled = dwarf2_string_attr (die, DW_AT_MIPS_linkage_name, cu);
900e11f9 8739
e98c9e7c
TT
8740 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
8741 See https://github.com/rust-lang/rust/issues/32925. */
8742 if (cu->language == language_rust && mangled != NULL
8743 && strchr (mangled, '{') != NULL)
8744 mangled = NULL;
8745
900e11f9
JK
8746 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
8747 has computed. */
7d45c7c3 8748 if (mangled != NULL)
900e11f9
JK
8749 {
8750 char *demangled;
8751
900e11f9
JK
8752 /* Use DMGL_RET_DROP for C++ template functions to suppress their return
8753 type. It is easier for GDB users to search for such functions as
8754 `name(params)' than `long name(params)'. In such case the minimal
8755 symbol names do not match the full symbol names but for template
8756 functions there is never a need to look up their definition from their
8757 declaration so the only disadvantage remains the minimal symbol
8758 variant `long name(params)' does not have the proper inferior type.
8759 */
8760
a766d390
DE
8761 if (cu->language == language_go)
8762 {
8763 /* This is a lie, but we already lie to the caller new_symbol_full.
8764 new_symbol_full assumes we return the mangled name.
8765 This just undoes that lie until things are cleaned up. */
8766 demangled = NULL;
8767 }
8768 else
8769 {
8de20a37
TT
8770 demangled = gdb_demangle (mangled,
8771 (DMGL_PARAMS | DMGL_ANSI
8772 | (cu->language == language_java
8773 ? DMGL_JAVA | DMGL_RET_POSTFIX
8774 : DMGL_RET_DROP)));
a766d390 8775 }
900e11f9
JK
8776 if (demangled)
8777 {
8778 make_cleanup (xfree, demangled);
8779 canon = demangled;
8780 }
8781 else
8782 {
8783 canon = mangled;
8784 need_copy = 0;
8785 }
8786 }
8787
8788 if (canon == NULL || check_physname)
8789 {
8790 const char *physname = dwarf2_compute_name (name, die, cu, 1);
8791
8792 if (canon != NULL && strcmp (physname, canon) != 0)
8793 {
8794 /* It may not mean a bug in GDB. The compiler could also
8795 compute DW_AT_linkage_name incorrectly. But in such case
8796 GDB would need to be bug-to-bug compatible. */
8797
8798 complaint (&symfile_complaints,
8799 _("Computed physname <%s> does not match demangled <%s> "
8800 "(from linkage <%s>) - DIE at 0x%x [in module %s]"),
4262abfb
JK
8801 physname, canon, mangled, die->offset.sect_off,
8802 objfile_name (objfile));
900e11f9
JK
8803
8804 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
8805 is available here - over computed PHYSNAME. It is safer
8806 against both buggy GDB and buggy compilers. */
8807
8808 retval = canon;
8809 }
8810 else
8811 {
8812 retval = physname;
8813 need_copy = 0;
8814 }
8815 }
8816 else
8817 retval = canon;
8818
8819 if (need_copy)
224c3ddb
SM
8820 retval = ((const char *)
8821 obstack_copy0 (&objfile->per_bfd->storage_obstack,
8822 retval, strlen (retval)));
900e11f9
JK
8823
8824 do_cleanups (back_to);
8825 return retval;
0114d602
DJ
8826}
8827
74921315
KS
8828/* Inspect DIE in CU for a namespace alias. If one exists, record
8829 a new symbol for it.
8830
8831 Returns 1 if a namespace alias was recorded, 0 otherwise. */
8832
8833static int
8834read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu)
8835{
8836 struct attribute *attr;
8837
8838 /* If the die does not have a name, this is not a namespace
8839 alias. */
8840 attr = dwarf2_attr (die, DW_AT_name, cu);
8841 if (attr != NULL)
8842 {
8843 int num;
8844 struct die_info *d = die;
8845 struct dwarf2_cu *imported_cu = cu;
8846
8847 /* If the compiler has nested DW_AT_imported_declaration DIEs,
8848 keep inspecting DIEs until we hit the underlying import. */
8849#define MAX_NESTED_IMPORTED_DECLARATIONS 100
8850 for (num = 0; num < MAX_NESTED_IMPORTED_DECLARATIONS; ++num)
8851 {
8852 attr = dwarf2_attr (d, DW_AT_import, cu);
8853 if (attr == NULL)
8854 break;
8855
8856 d = follow_die_ref (d, attr, &imported_cu);
8857 if (d->tag != DW_TAG_imported_declaration)
8858 break;
8859 }
8860
8861 if (num == MAX_NESTED_IMPORTED_DECLARATIONS)
8862 {
8863 complaint (&symfile_complaints,
8864 _("DIE at 0x%x has too many recursively imported "
8865 "declarations"), d->offset.sect_off);
8866 return 0;
8867 }
8868
8869 if (attr != NULL)
8870 {
8871 struct type *type;
8872 sect_offset offset = dwarf2_get_ref_die_offset (attr);
8873
8874 type = get_die_type_at_offset (offset, cu->per_cu);
8875 if (type != NULL && TYPE_CODE (type) == TYPE_CODE_NAMESPACE)
8876 {
8877 /* This declaration is a global namespace alias. Add
8878 a symbol for it whose type is the aliased namespace. */
8879 new_symbol (die, type, cu);
8880 return 1;
8881 }
8882 }
8883 }
8884
8885 return 0;
8886}
8887
22cee43f
PMR
8888/* Return the using directives repository (global or local?) to use in the
8889 current context for LANGUAGE.
8890
8891 For Ada, imported declarations can materialize renamings, which *may* be
8892 global. However it is impossible (for now?) in DWARF to distinguish
8893 "external" imported declarations and "static" ones. As all imported
8894 declarations seem to be static in all other languages, make them all CU-wide
8895 global only in Ada. */
8896
8897static struct using_direct **
8898using_directives (enum language language)
8899{
8900 if (language == language_ada && context_stack_depth == 0)
8901 return &global_using_directives;
8902 else
8903 return &local_using_directives;
8904}
8905
27aa8d6a
SW
8906/* Read the import statement specified by the given die and record it. */
8907
8908static void
8909read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
8910{
bb5ed363 8911 struct objfile *objfile = cu->objfile;
27aa8d6a 8912 struct attribute *import_attr;
32019081 8913 struct die_info *imported_die, *child_die;
de4affc9 8914 struct dwarf2_cu *imported_cu;
27aa8d6a 8915 const char *imported_name;
794684b6 8916 const char *imported_name_prefix;
13387711
SW
8917 const char *canonical_name;
8918 const char *import_alias;
8919 const char *imported_declaration = NULL;
794684b6 8920 const char *import_prefix;
32019081
JK
8921 VEC (const_char_ptr) *excludes = NULL;
8922 struct cleanup *cleanups;
13387711 8923
27aa8d6a
SW
8924 import_attr = dwarf2_attr (die, DW_AT_import, cu);
8925 if (import_attr == NULL)
8926 {
8927 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
8928 dwarf_tag_name (die->tag));
8929 return;
8930 }
8931
de4affc9
CC
8932 imported_cu = cu;
8933 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
8934 imported_name = dwarf2_name (imported_die, imported_cu);
27aa8d6a
SW
8935 if (imported_name == NULL)
8936 {
8937 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
8938
8939 The import in the following code:
8940 namespace A
8941 {
8942 typedef int B;
8943 }
8944
8945 int main ()
8946 {
8947 using A::B;
8948 B b;
8949 return b;
8950 }
8951
8952 ...
8953 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
8954 <52> DW_AT_decl_file : 1
8955 <53> DW_AT_decl_line : 6
8956 <54> DW_AT_import : <0x75>
8957 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
8958 <59> DW_AT_name : B
8959 <5b> DW_AT_decl_file : 1
8960 <5c> DW_AT_decl_line : 2
8961 <5d> DW_AT_type : <0x6e>
8962 ...
8963 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
8964 <76> DW_AT_byte_size : 4
8965 <77> DW_AT_encoding : 5 (signed)
8966
8967 imports the wrong die ( 0x75 instead of 0x58 ).
8968 This case will be ignored until the gcc bug is fixed. */
8969 return;
8970 }
8971
82856980
SW
8972 /* Figure out the local name after import. */
8973 import_alias = dwarf2_name (die, cu);
27aa8d6a 8974
794684b6
SW
8975 /* Figure out where the statement is being imported to. */
8976 import_prefix = determine_prefix (die, cu);
8977
8978 /* Figure out what the scope of the imported die is and prepend it
8979 to the name of the imported die. */
de4affc9 8980 imported_name_prefix = determine_prefix (imported_die, imported_cu);
794684b6 8981
f55ee35c
JK
8982 if (imported_die->tag != DW_TAG_namespace
8983 && imported_die->tag != DW_TAG_module)
794684b6 8984 {
13387711
SW
8985 imported_declaration = imported_name;
8986 canonical_name = imported_name_prefix;
794684b6 8987 }
13387711 8988 else if (strlen (imported_name_prefix) > 0)
12aaed36 8989 canonical_name = obconcat (&objfile->objfile_obstack,
45280282
IB
8990 imported_name_prefix,
8991 (cu->language == language_d ? "." : "::"),
8992 imported_name, (char *) NULL);
13387711
SW
8993 else
8994 canonical_name = imported_name;
794684b6 8995
32019081
JK
8996 cleanups = make_cleanup (VEC_cleanup (const_char_ptr), &excludes);
8997
8998 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
8999 for (child_die = die->child; child_die && child_die->tag;
9000 child_die = sibling_die (child_die))
9001 {
9002 /* DWARF-4: A Fortran use statement with a “rename list” may be
9003 represented by an imported module entry with an import attribute
9004 referring to the module and owned entries corresponding to those
9005 entities that are renamed as part of being imported. */
9006
9007 if (child_die->tag != DW_TAG_imported_declaration)
9008 {
9009 complaint (&symfile_complaints,
9010 _("child DW_TAG_imported_declaration expected "
9011 "- DIE at 0x%x [in module %s]"),
4262abfb 9012 child_die->offset.sect_off, objfile_name (objfile));
32019081
JK
9013 continue;
9014 }
9015
9016 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
9017 if (import_attr == NULL)
9018 {
9019 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
9020 dwarf_tag_name (child_die->tag));
9021 continue;
9022 }
9023
9024 imported_cu = cu;
9025 imported_die = follow_die_ref_or_sig (child_die, import_attr,
9026 &imported_cu);
9027 imported_name = dwarf2_name (imported_die, imported_cu);
9028 if (imported_name == NULL)
9029 {
9030 complaint (&symfile_complaints,
9031 _("child DW_TAG_imported_declaration has unknown "
9032 "imported name - DIE at 0x%x [in module %s]"),
4262abfb 9033 child_die->offset.sect_off, objfile_name (objfile));
32019081
JK
9034 continue;
9035 }
9036
9037 VEC_safe_push (const_char_ptr, excludes, imported_name);
9038
9039 process_die (child_die, cu);
9040 }
9041
22cee43f
PMR
9042 add_using_directive (using_directives (cu->language),
9043 import_prefix,
9044 canonical_name,
9045 import_alias,
9046 imported_declaration,
9047 excludes,
9048 0,
9049 &objfile->objfile_obstack);
32019081
JK
9050
9051 do_cleanups (cleanups);
27aa8d6a
SW
9052}
9053
f4dc4d17 9054/* Cleanup function for handle_DW_AT_stmt_list. */
ae2de4f8 9055
cb1df416
DJ
9056static void
9057free_cu_line_header (void *arg)
9058{
9a3c8263 9059 struct dwarf2_cu *cu = (struct dwarf2_cu *) arg;
cb1df416
DJ
9060
9061 free_line_header (cu->line_header);
9062 cu->line_header = NULL;
9063}
9064
1b80a9fa
JK
9065/* Check for possibly missing DW_AT_comp_dir with relative .debug_line
9066 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
9067 this, it was first present in GCC release 4.3.0. */
9068
9069static int
9070producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
9071{
9072 if (!cu->checked_producer)
9073 check_producer (cu);
9074
9075 return cu->producer_is_gcc_lt_4_3;
9076}
9077
9291a0cd
TT
9078static void
9079find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu,
15d034d0 9080 const char **name, const char **comp_dir)
9291a0cd 9081{
9291a0cd
TT
9082 /* Find the filename. Do not use dwarf2_name here, since the filename
9083 is not a source language identifier. */
7d45c7c3
KB
9084 *name = dwarf2_string_attr (die, DW_AT_name, cu);
9085 *comp_dir = dwarf2_string_attr (die, DW_AT_comp_dir, cu);
9291a0cd 9086
7d45c7c3
KB
9087 if (*comp_dir == NULL
9088 && producer_is_gcc_lt_4_3 (cu) && *name != NULL
9089 && IS_ABSOLUTE_PATH (*name))
9291a0cd 9090 {
15d034d0
TT
9091 char *d = ldirname (*name);
9092
9093 *comp_dir = d;
9094 if (d != NULL)
9095 make_cleanup (xfree, d);
9291a0cd
TT
9096 }
9097 if (*comp_dir != NULL)
9098 {
9099 /* Irix 6.2 native cc prepends <machine>.: to the compilation
9100 directory, get rid of it. */
e6a959d6 9101 const char *cp = strchr (*comp_dir, ':');
9291a0cd
TT
9102
9103 if (cp && cp != *comp_dir && cp[-1] == '.' && cp[1] == '/')
9104 *comp_dir = cp + 1;
9105 }
9106
9107 if (*name == NULL)
9108 *name = "<unknown>";
9109}
9110
f4dc4d17
DE
9111/* Handle DW_AT_stmt_list for a compilation unit.
9112 DIE is the DW_TAG_compile_unit die for CU.
c3b7b696
YQ
9113 COMP_DIR is the compilation directory. LOWPC is passed to
9114 dwarf_decode_lines. See dwarf_decode_lines comments about it. */
2ab95328
TT
9115
9116static void
9117handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
c3b7b696 9118 const char *comp_dir, CORE_ADDR lowpc) /* ARI: editCase function */
2ab95328 9119{
527f3840 9120 struct objfile *objfile = dwarf2_per_objfile->objfile;
2ab95328 9121 struct attribute *attr;
527f3840
JK
9122 unsigned int line_offset;
9123 struct line_header line_header_local;
9124 hashval_t line_header_local_hash;
9125 unsigned u;
9126 void **slot;
9127 int decode_mapping;
2ab95328 9128
f4dc4d17
DE
9129 gdb_assert (! cu->per_cu->is_debug_types);
9130
2ab95328 9131 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
527f3840
JK
9132 if (attr == NULL)
9133 return;
9134
9135 line_offset = DW_UNSND (attr);
9136
9137 /* The line header hash table is only created if needed (it exists to
9138 prevent redundant reading of the line table for partial_units).
9139 If we're given a partial_unit, we'll need it. If we're given a
9140 compile_unit, then use the line header hash table if it's already
9141 created, but don't create one just yet. */
9142
9143 if (dwarf2_per_objfile->line_header_hash == NULL
9144 && die->tag == DW_TAG_partial_unit)
2ab95328 9145 {
527f3840
JK
9146 dwarf2_per_objfile->line_header_hash
9147 = htab_create_alloc_ex (127, line_header_hash_voidp,
9148 line_header_eq_voidp,
9149 free_line_header_voidp,
9150 &objfile->objfile_obstack,
9151 hashtab_obstack_allocate,
9152 dummy_obstack_deallocate);
9153 }
2ab95328 9154
527f3840
JK
9155 line_header_local.offset.sect_off = line_offset;
9156 line_header_local.offset_in_dwz = cu->per_cu->is_dwz;
9157 line_header_local_hash = line_header_hash (&line_header_local);
9158 if (dwarf2_per_objfile->line_header_hash != NULL)
9159 {
9160 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash,
9161 &line_header_local,
9162 line_header_local_hash, NO_INSERT);
9163
9164 /* For DW_TAG_compile_unit we need info like symtab::linetable which
9165 is not present in *SLOT (since if there is something in *SLOT then
9166 it will be for a partial_unit). */
9167 if (die->tag == DW_TAG_partial_unit && slot != NULL)
dee91e82 9168 {
527f3840 9169 gdb_assert (*slot != NULL);
9a3c8263 9170 cu->line_header = (struct line_header *) *slot;
527f3840 9171 return;
dee91e82 9172 }
2ab95328 9173 }
527f3840
JK
9174
9175 /* dwarf_decode_line_header does not yet provide sufficient information.
9176 We always have to call also dwarf_decode_lines for it. */
9177 cu->line_header = dwarf_decode_line_header (line_offset, cu);
9178 if (cu->line_header == NULL)
9179 return;
9180
9181 if (dwarf2_per_objfile->line_header_hash == NULL)
9182 slot = NULL;
9183 else
9184 {
9185 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash,
9186 &line_header_local,
9187 line_header_local_hash, INSERT);
9188 gdb_assert (slot != NULL);
9189 }
9190 if (slot != NULL && *slot == NULL)
9191 {
9192 /* This newly decoded line number information unit will be owned
9193 by line_header_hash hash table. */
9194 *slot = cu->line_header;
9195 }
9196 else
9197 {
9198 /* We cannot free any current entry in (*slot) as that struct line_header
9199 may be already used by multiple CUs. Create only temporary decoded
9200 line_header for this CU - it may happen at most once for each line
9201 number information unit. And if we're not using line_header_hash
9202 then this is what we want as well. */
9203 gdb_assert (die->tag != DW_TAG_partial_unit);
9204 make_cleanup (free_cu_line_header, cu);
9205 }
9206 decode_mapping = (die->tag != DW_TAG_partial_unit);
9207 dwarf_decode_lines (cu->line_header, comp_dir, cu, NULL, lowpc,
9208 decode_mapping);
2ab95328
TT
9209}
9210
95554aad 9211/* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
ae2de4f8 9212
c906108c 9213static void
e7c27a73 9214read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 9215{
dee91e82 9216 struct objfile *objfile = dwarf2_per_objfile->objfile;
3e29f34a 9217 struct gdbarch *gdbarch = get_objfile_arch (objfile);
debd256d 9218 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
2acceee2 9219 CORE_ADDR lowpc = ((CORE_ADDR) -1);
c906108c
SS
9220 CORE_ADDR highpc = ((CORE_ADDR) 0);
9221 struct attribute *attr;
15d034d0
TT
9222 const char *name = NULL;
9223 const char *comp_dir = NULL;
c906108c 9224 struct die_info *child_die;
e142c38c 9225 CORE_ADDR baseaddr;
6e70227d 9226
e142c38c 9227 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 9228
fae299cd 9229 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
c906108c
SS
9230
9231 /* If we didn't find a lowpc, set it to highpc to avoid complaints
9232 from finish_block. */
2acceee2 9233 if (lowpc == ((CORE_ADDR) -1))
c906108c 9234 lowpc = highpc;
3e29f34a 9235 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
c906108c 9236
9291a0cd 9237 find_file_and_directory (die, cu, &name, &comp_dir);
e1024ff1 9238
95554aad 9239 prepare_one_comp_unit (cu, die, cu->language);
303b6f5d 9240
f4b8a18d
KW
9241 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
9242 standardised yet. As a workaround for the language detection we fall
9243 back to the DW_AT_producer string. */
9244 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
9245 cu->language = language_opencl;
9246
3019eac3
DE
9247 /* Similar hack for Go. */
9248 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
9249 set_cu_language (DW_LANG_Go, cu);
9250
f4dc4d17 9251 dwarf2_start_symtab (cu, name, comp_dir, lowpc);
3019eac3
DE
9252
9253 /* Decode line number information if present. We do this before
9254 processing child DIEs, so that the line header table is available
9255 for DW_AT_decl_file. */
c3b7b696 9256 handle_DW_AT_stmt_list (die, cu, comp_dir, lowpc);
3019eac3
DE
9257
9258 /* Process all dies in compilation unit. */
9259 if (die->child != NULL)
9260 {
9261 child_die = die->child;
9262 while (child_die && child_die->tag)
9263 {
9264 process_die (child_die, cu);
9265 child_die = sibling_die (child_die);
9266 }
9267 }
9268
9269 /* Decode macro information, if present. Dwarf 2 macro information
9270 refers to information in the line number info statement program
9271 header, so we can only read it if we've read the header
9272 successfully. */
9273 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
9274 if (attr && cu->line_header)
9275 {
9276 if (dwarf2_attr (die, DW_AT_macro_info, cu))
9277 complaint (&symfile_complaints,
9278 _("CU refers to both DW_AT_GNU_macros and DW_AT_macro_info"));
9279
43f3e411 9280 dwarf_decode_macros (cu, DW_UNSND (attr), 1);
3019eac3
DE
9281 }
9282 else
9283 {
9284 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
9285 if (attr && cu->line_header)
9286 {
9287 unsigned int macro_offset = DW_UNSND (attr);
9288
43f3e411 9289 dwarf_decode_macros (cu, macro_offset, 0);
3019eac3
DE
9290 }
9291 }
9292
9293 do_cleanups (back_to);
9294}
9295
f4dc4d17
DE
9296/* TU version of handle_DW_AT_stmt_list for read_type_unit_scope.
9297 Create the set of symtabs used by this TU, or if this TU is sharing
9298 symtabs with another TU and the symtabs have already been created
9299 then restore those symtabs in the line header.
9300 We don't need the pc/line-number mapping for type units. */
3019eac3
DE
9301
9302static void
f4dc4d17 9303setup_type_unit_groups (struct die_info *die, struct dwarf2_cu *cu)
3019eac3 9304{
f4dc4d17
DE
9305 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
9306 struct type_unit_group *tu_group;
9307 int first_time;
9308 struct line_header *lh;
3019eac3 9309 struct attribute *attr;
f4dc4d17 9310 unsigned int i, line_offset;
0186c6a7 9311 struct signatured_type *sig_type;
3019eac3 9312
f4dc4d17 9313 gdb_assert (per_cu->is_debug_types);
0186c6a7 9314 sig_type = (struct signatured_type *) per_cu;
3019eac3 9315
f4dc4d17 9316 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
3019eac3 9317
f4dc4d17 9318 /* If we're using .gdb_index (includes -readnow) then
74e04d1c 9319 per_cu->type_unit_group may not have been set up yet. */
0186c6a7
DE
9320 if (sig_type->type_unit_group == NULL)
9321 sig_type->type_unit_group = get_type_unit_group (cu, attr);
9322 tu_group = sig_type->type_unit_group;
f4dc4d17
DE
9323
9324 /* If we've already processed this stmt_list there's no real need to
9325 do it again, we could fake it and just recreate the part we need
9326 (file name,index -> symtab mapping). If data shows this optimization
9327 is useful we can do it then. */
43f3e411 9328 first_time = tu_group->compunit_symtab == NULL;
f4dc4d17
DE
9329
9330 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
9331 debug info. */
9332 lh = NULL;
9333 if (attr != NULL)
3019eac3 9334 {
f4dc4d17
DE
9335 line_offset = DW_UNSND (attr);
9336 lh = dwarf_decode_line_header (line_offset, cu);
9337 }
9338 if (lh == NULL)
9339 {
9340 if (first_time)
9341 dwarf2_start_symtab (cu, "", NULL, 0);
9342 else
9343 {
9344 gdb_assert (tu_group->symtabs == NULL);
0ab9ce85 9345 restart_symtab (tu_group->compunit_symtab, "", 0);
f4dc4d17 9346 }
f4dc4d17 9347 return;
3019eac3
DE
9348 }
9349
f4dc4d17
DE
9350 cu->line_header = lh;
9351 make_cleanup (free_cu_line_header, cu);
3019eac3 9352
f4dc4d17
DE
9353 if (first_time)
9354 {
43f3e411 9355 struct compunit_symtab *cust = dwarf2_start_symtab (cu, "", NULL, 0);
3019eac3 9356
1fd60fc0
DE
9357 /* Note: We don't assign tu_group->compunit_symtab yet because we're
9358 still initializing it, and our caller (a few levels up)
9359 process_full_type_unit still needs to know if this is the first
9360 time. */
9361
f4dc4d17
DE
9362 tu_group->num_symtabs = lh->num_file_names;
9363 tu_group->symtabs = XNEWVEC (struct symtab *, lh->num_file_names);
3019eac3 9364
f4dc4d17
DE
9365 for (i = 0; i < lh->num_file_names; ++i)
9366 {
d521ce57 9367 const char *dir = NULL;
f4dc4d17 9368 struct file_entry *fe = &lh->file_names[i];
3019eac3 9369
afa6c9ab 9370 if (fe->dir_index && lh->include_dirs != NULL)
f4dc4d17 9371 dir = lh->include_dirs[fe->dir_index - 1];
4d663531 9372 dwarf2_start_subfile (fe->name, dir);
3019eac3 9373
f4dc4d17
DE
9374 if (current_subfile->symtab == NULL)
9375 {
9376 /* NOTE: start_subfile will recognize when it's been passed
9377 a file it has already seen. So we can't assume there's a
43f3e411 9378 simple mapping from lh->file_names to subfiles, plus
f4dc4d17 9379 lh->file_names may contain dups. */
43f3e411
DE
9380 current_subfile->symtab
9381 = allocate_symtab (cust, current_subfile->name);
f4dc4d17
DE
9382 }
9383
9384 fe->symtab = current_subfile->symtab;
9385 tu_group->symtabs[i] = fe->symtab;
9386 }
9387 }
9388 else
3019eac3 9389 {
0ab9ce85 9390 restart_symtab (tu_group->compunit_symtab, "", 0);
f4dc4d17
DE
9391
9392 for (i = 0; i < lh->num_file_names; ++i)
9393 {
9394 struct file_entry *fe = &lh->file_names[i];
9395
9396 fe->symtab = tu_group->symtabs[i];
9397 }
3019eac3
DE
9398 }
9399
f4dc4d17
DE
9400 /* The main symtab is allocated last. Type units don't have DW_AT_name
9401 so they don't have a "real" (so to speak) symtab anyway.
9402 There is later code that will assign the main symtab to all symbols
9403 that don't have one. We need to handle the case of a symbol with a
9404 missing symtab (DW_AT_decl_file) anyway. */
9405}
3019eac3 9406
f4dc4d17
DE
9407/* Process DW_TAG_type_unit.
9408 For TUs we want to skip the first top level sibling if it's not the
9409 actual type being defined by this TU. In this case the first top
9410 level sibling is there to provide context only. */
3019eac3 9411
f4dc4d17
DE
9412static void
9413read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
9414{
9415 struct die_info *child_die;
3019eac3 9416
f4dc4d17
DE
9417 prepare_one_comp_unit (cu, die, language_minimal);
9418
9419 /* Initialize (or reinitialize) the machinery for building symtabs.
9420 We do this before processing child DIEs, so that the line header table
9421 is available for DW_AT_decl_file. */
9422 setup_type_unit_groups (die, cu);
9423
9424 if (die->child != NULL)
9425 {
9426 child_die = die->child;
9427 while (child_die && child_die->tag)
9428 {
9429 process_die (child_die, cu);
9430 child_die = sibling_die (child_die);
9431 }
9432 }
3019eac3
DE
9433}
9434\f
80626a55
DE
9435/* DWO/DWP files.
9436
9437 http://gcc.gnu.org/wiki/DebugFission
9438 http://gcc.gnu.org/wiki/DebugFissionDWP
9439
9440 To simplify handling of both DWO files ("object" files with the DWARF info)
9441 and DWP files (a file with the DWOs packaged up into one file), we treat
9442 DWP files as having a collection of virtual DWO files. */
3019eac3
DE
9443
9444static hashval_t
9445hash_dwo_file (const void *item)
9446{
9a3c8263 9447 const struct dwo_file *dwo_file = (const struct dwo_file *) item;
a2ce51a0 9448 hashval_t hash;
3019eac3 9449
a2ce51a0
DE
9450 hash = htab_hash_string (dwo_file->dwo_name);
9451 if (dwo_file->comp_dir != NULL)
9452 hash += htab_hash_string (dwo_file->comp_dir);
9453 return hash;
3019eac3
DE
9454}
9455
9456static int
9457eq_dwo_file (const void *item_lhs, const void *item_rhs)
9458{
9a3c8263
SM
9459 const struct dwo_file *lhs = (const struct dwo_file *) item_lhs;
9460 const struct dwo_file *rhs = (const struct dwo_file *) item_rhs;
3019eac3 9461
a2ce51a0
DE
9462 if (strcmp (lhs->dwo_name, rhs->dwo_name) != 0)
9463 return 0;
9464 if (lhs->comp_dir == NULL || rhs->comp_dir == NULL)
9465 return lhs->comp_dir == rhs->comp_dir;
9466 return strcmp (lhs->comp_dir, rhs->comp_dir) == 0;
3019eac3
DE
9467}
9468
9469/* Allocate a hash table for DWO files. */
9470
9471static htab_t
9472allocate_dwo_file_hash_table (void)
9473{
9474 struct objfile *objfile = dwarf2_per_objfile->objfile;
9475
9476 return htab_create_alloc_ex (41,
9477 hash_dwo_file,
9478 eq_dwo_file,
9479 NULL,
9480 &objfile->objfile_obstack,
9481 hashtab_obstack_allocate,
9482 dummy_obstack_deallocate);
9483}
9484
80626a55
DE
9485/* Lookup DWO file DWO_NAME. */
9486
9487static void **
0ac5b59e 9488lookup_dwo_file_slot (const char *dwo_name, const char *comp_dir)
80626a55
DE
9489{
9490 struct dwo_file find_entry;
9491 void **slot;
9492
9493 if (dwarf2_per_objfile->dwo_files == NULL)
9494 dwarf2_per_objfile->dwo_files = allocate_dwo_file_hash_table ();
9495
9496 memset (&find_entry, 0, sizeof (find_entry));
0ac5b59e
DE
9497 find_entry.dwo_name = dwo_name;
9498 find_entry.comp_dir = comp_dir;
80626a55
DE
9499 slot = htab_find_slot (dwarf2_per_objfile->dwo_files, &find_entry, INSERT);
9500
9501 return slot;
9502}
9503
3019eac3
DE
9504static hashval_t
9505hash_dwo_unit (const void *item)
9506{
9a3c8263 9507 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
3019eac3
DE
9508
9509 /* This drops the top 32 bits of the id, but is ok for a hash. */
9510 return dwo_unit->signature;
9511}
9512
9513static int
9514eq_dwo_unit (const void *item_lhs, const void *item_rhs)
9515{
9a3c8263
SM
9516 const struct dwo_unit *lhs = (const struct dwo_unit *) item_lhs;
9517 const struct dwo_unit *rhs = (const struct dwo_unit *) item_rhs;
3019eac3
DE
9518
9519 /* The signature is assumed to be unique within the DWO file.
9520 So while object file CU dwo_id's always have the value zero,
9521 that's OK, assuming each object file DWO file has only one CU,
9522 and that's the rule for now. */
9523 return lhs->signature == rhs->signature;
9524}
9525
9526/* Allocate a hash table for DWO CUs,TUs.
9527 There is one of these tables for each of CUs,TUs for each DWO file. */
9528
9529static htab_t
9530allocate_dwo_unit_table (struct objfile *objfile)
9531{
9532 /* Start out with a pretty small number.
9533 Generally DWO files contain only one CU and maybe some TUs. */
9534 return htab_create_alloc_ex (3,
9535 hash_dwo_unit,
9536 eq_dwo_unit,
9537 NULL,
9538 &objfile->objfile_obstack,
9539 hashtab_obstack_allocate,
9540 dummy_obstack_deallocate);
9541}
9542
80626a55 9543/* Structure used to pass data to create_dwo_debug_info_hash_table_reader. */
3019eac3 9544
19c3d4c9 9545struct create_dwo_cu_data
3019eac3
DE
9546{
9547 struct dwo_file *dwo_file;
19c3d4c9 9548 struct dwo_unit dwo_unit;
3019eac3
DE
9549};
9550
19c3d4c9 9551/* die_reader_func for create_dwo_cu. */
3019eac3
DE
9552
9553static void
19c3d4c9
DE
9554create_dwo_cu_reader (const struct die_reader_specs *reader,
9555 const gdb_byte *info_ptr,
9556 struct die_info *comp_unit_die,
9557 int has_children,
9558 void *datap)
3019eac3
DE
9559{
9560 struct dwarf2_cu *cu = reader->cu;
3019eac3 9561 sect_offset offset = cu->per_cu->offset;
8a0459fd 9562 struct dwarf2_section_info *section = cu->per_cu->section;
9a3c8263 9563 struct create_dwo_cu_data *data = (struct create_dwo_cu_data *) datap;
3019eac3 9564 struct dwo_file *dwo_file = data->dwo_file;
19c3d4c9 9565 struct dwo_unit *dwo_unit = &data->dwo_unit;
3019eac3 9566 struct attribute *attr;
3019eac3
DE
9567
9568 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
9569 if (attr == NULL)
9570 {
19c3d4c9
DE
9571 complaint (&symfile_complaints,
9572 _("Dwarf Error: debug entry at offset 0x%x is missing"
9573 " its dwo_id [in module %s]"),
9574 offset.sect_off, dwo_file->dwo_name);
3019eac3
DE
9575 return;
9576 }
9577
3019eac3
DE
9578 dwo_unit->dwo_file = dwo_file;
9579 dwo_unit->signature = DW_UNSND (attr);
8a0459fd 9580 dwo_unit->section = section;
3019eac3
DE
9581 dwo_unit->offset = offset;
9582 dwo_unit->length = cu->per_cu->length;
9583
b4f54984 9584 if (dwarf_read_debug)
4031ecc5
DE
9585 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, dwo_id %s\n",
9586 offset.sect_off, hex_string (dwo_unit->signature));
3019eac3
DE
9587}
9588
19c3d4c9
DE
9589/* Create the dwo_unit for the lone CU in DWO_FILE.
9590 Note: This function processes DWO files only, not DWP files. */
3019eac3 9591
19c3d4c9
DE
9592static struct dwo_unit *
9593create_dwo_cu (struct dwo_file *dwo_file)
3019eac3
DE
9594{
9595 struct objfile *objfile = dwarf2_per_objfile->objfile;
9596 struct dwarf2_section_info *section = &dwo_file->sections.info;
d521ce57 9597 const gdb_byte *info_ptr, *end_ptr;
19c3d4c9
DE
9598 struct create_dwo_cu_data create_dwo_cu_data;
9599 struct dwo_unit *dwo_unit;
3019eac3
DE
9600
9601 dwarf2_read_section (objfile, section);
9602 info_ptr = section->buffer;
9603
9604 if (info_ptr == NULL)
9605 return NULL;
9606
b4f54984 9607 if (dwarf_read_debug)
19c3d4c9
DE
9608 {
9609 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
a32a8923
DE
9610 get_section_name (section),
9611 get_section_file_name (section));
19c3d4c9 9612 }
3019eac3 9613
19c3d4c9
DE
9614 create_dwo_cu_data.dwo_file = dwo_file;
9615 dwo_unit = NULL;
3019eac3
DE
9616
9617 end_ptr = info_ptr + section->size;
9618 while (info_ptr < end_ptr)
9619 {
9620 struct dwarf2_per_cu_data per_cu;
9621
19c3d4c9
DE
9622 memset (&create_dwo_cu_data.dwo_unit, 0,
9623 sizeof (create_dwo_cu_data.dwo_unit));
3019eac3
DE
9624 memset (&per_cu, 0, sizeof (per_cu));
9625 per_cu.objfile = objfile;
9626 per_cu.is_debug_types = 0;
9627 per_cu.offset.sect_off = info_ptr - section->buffer;
8a0459fd 9628 per_cu.section = section;
3019eac3 9629
33e80786 9630 init_cutu_and_read_dies_no_follow (&per_cu, dwo_file,
19c3d4c9
DE
9631 create_dwo_cu_reader,
9632 &create_dwo_cu_data);
9633
9634 if (create_dwo_cu_data.dwo_unit.dwo_file != NULL)
9635 {
9636 /* If we've already found one, complain. We only support one
9637 because having more than one requires hacking the dwo_name of
9638 each to match, which is highly unlikely to happen. */
9639 if (dwo_unit != NULL)
9640 {
9641 complaint (&symfile_complaints,
9642 _("Multiple CUs in DWO file %s [in module %s]"),
4262abfb 9643 dwo_file->dwo_name, objfile_name (objfile));
19c3d4c9
DE
9644 break;
9645 }
9646
9647 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
9648 *dwo_unit = create_dwo_cu_data.dwo_unit;
9649 }
3019eac3
DE
9650
9651 info_ptr += per_cu.length;
9652 }
9653
19c3d4c9 9654 return dwo_unit;
3019eac3
DE
9655}
9656
80626a55
DE
9657/* DWP file .debug_{cu,tu}_index section format:
9658 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
9659
d2415c6c
DE
9660 DWP Version 1:
9661
80626a55
DE
9662 Both index sections have the same format, and serve to map a 64-bit
9663 signature to a set of section numbers. Each section begins with a header,
9664 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
9665 indexes, and a pool of 32-bit section numbers. The index sections will be
9666 aligned at 8-byte boundaries in the file.
9667
d2415c6c
DE
9668 The index section header consists of:
9669
9670 V, 32 bit version number
9671 -, 32 bits unused
9672 N, 32 bit number of compilation units or type units in the index
9673 M, 32 bit number of slots in the hash table
80626a55 9674
d2415c6c 9675 Numbers are recorded using the byte order of the application binary.
80626a55 9676
d2415c6c
DE
9677 The hash table begins at offset 16 in the section, and consists of an array
9678 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
9679 order of the application binary). Unused slots in the hash table are 0.
9680 (We rely on the extreme unlikeliness of a signature being exactly 0.)
80626a55 9681
d2415c6c
DE
9682 The parallel table begins immediately after the hash table
9683 (at offset 16 + 8 * M from the beginning of the section), and consists of an
9684 array of 32-bit indexes (using the byte order of the application binary),
9685 corresponding 1-1 with slots in the hash table. Each entry in the parallel
9686 table contains a 32-bit index into the pool of section numbers. For unused
9687 hash table slots, the corresponding entry in the parallel table will be 0.
80626a55 9688
73869dc2
DE
9689 The pool of section numbers begins immediately following the hash table
9690 (at offset 16 + 12 * M from the beginning of the section). The pool of
9691 section numbers consists of an array of 32-bit words (using the byte order
9692 of the application binary). Each item in the array is indexed starting
9693 from 0. The hash table entry provides the index of the first section
9694 number in the set. Additional section numbers in the set follow, and the
9695 set is terminated by a 0 entry (section number 0 is not used in ELF).
9696
9697 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
9698 section must be the first entry in the set, and the .debug_abbrev.dwo must
9699 be the second entry. Other members of the set may follow in any order.
9700
9701 ---
9702
9703 DWP Version 2:
9704
9705 DWP Version 2 combines all the .debug_info, etc. sections into one,
9706 and the entries in the index tables are now offsets into these sections.
9707 CU offsets begin at 0. TU offsets begin at the size of the .debug_info
9708 section.
9709
9710 Index Section Contents:
9711 Header
9712 Hash Table of Signatures dwp_hash_table.hash_table
9713 Parallel Table of Indices dwp_hash_table.unit_table
9714 Table of Section Offsets dwp_hash_table.v2.{section_ids,offsets}
9715 Table of Section Sizes dwp_hash_table.v2.sizes
9716
9717 The index section header consists of:
9718
9719 V, 32 bit version number
9720 L, 32 bit number of columns in the table of section offsets
9721 N, 32 bit number of compilation units or type units in the index
9722 M, 32 bit number of slots in the hash table
9723
9724 Numbers are recorded using the byte order of the application binary.
9725
9726 The hash table has the same format as version 1.
9727 The parallel table of indices has the same format as version 1,
9728 except that the entries are origin-1 indices into the table of sections
9729 offsets and the table of section sizes.
9730
9731 The table of offsets begins immediately following the parallel table
9732 (at offset 16 + 12 * M from the beginning of the section). The table is
9733 a two-dimensional array of 32-bit words (using the byte order of the
9734 application binary), with L columns and N+1 rows, in row-major order.
9735 Each row in the array is indexed starting from 0. The first row provides
9736 a key to the remaining rows: each column in this row provides an identifier
9737 for a debug section, and the offsets in the same column of subsequent rows
9738 refer to that section. The section identifiers are:
9739
9740 DW_SECT_INFO 1 .debug_info.dwo
9741 DW_SECT_TYPES 2 .debug_types.dwo
9742 DW_SECT_ABBREV 3 .debug_abbrev.dwo
9743 DW_SECT_LINE 4 .debug_line.dwo
9744 DW_SECT_LOC 5 .debug_loc.dwo
9745 DW_SECT_STR_OFFSETS 6 .debug_str_offsets.dwo
9746 DW_SECT_MACINFO 7 .debug_macinfo.dwo
9747 DW_SECT_MACRO 8 .debug_macro.dwo
9748
9749 The offsets provided by the CU and TU index sections are the base offsets
9750 for the contributions made by each CU or TU to the corresponding section
9751 in the package file. Each CU and TU header contains an abbrev_offset
9752 field, used to find the abbreviations table for that CU or TU within the
9753 contribution to the .debug_abbrev.dwo section for that CU or TU, and should
9754 be interpreted as relative to the base offset given in the index section.
9755 Likewise, offsets into .debug_line.dwo from DW_AT_stmt_list attributes
9756 should be interpreted as relative to the base offset for .debug_line.dwo,
9757 and offsets into other debug sections obtained from DWARF attributes should
9758 also be interpreted as relative to the corresponding base offset.
9759
9760 The table of sizes begins immediately following the table of offsets.
9761 Like the table of offsets, it is a two-dimensional array of 32-bit words,
9762 with L columns and N rows, in row-major order. Each row in the array is
9763 indexed starting from 1 (row 0 is shared by the two tables).
9764
9765 ---
9766
9767 Hash table lookup is handled the same in version 1 and 2:
9768
9769 We assume that N and M will not exceed 2^32 - 1.
9770 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
9771
d2415c6c
DE
9772 Given a 64-bit compilation unit signature or a type signature S, an entry
9773 in the hash table is located as follows:
80626a55 9774
d2415c6c
DE
9775 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
9776 the low-order k bits all set to 1.
80626a55 9777
d2415c6c 9778 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
80626a55 9779
d2415c6c
DE
9780 3) If the hash table entry at index H matches the signature, use that
9781 entry. If the hash table entry at index H is unused (all zeroes),
9782 terminate the search: the signature is not present in the table.
80626a55 9783
d2415c6c 9784 4) Let H = (H + H') modulo M. Repeat at Step 3.
80626a55 9785
d2415c6c 9786 Because M > N and H' and M are relatively prime, the search is guaranteed
73869dc2 9787 to stop at an unused slot or find the match. */
80626a55
DE
9788
9789/* Create a hash table to map DWO IDs to their CU/TU entry in
9790 .debug_{info,types}.dwo in DWP_FILE.
9791 Returns NULL if there isn't one.
9792 Note: This function processes DWP files only, not DWO files. */
9793
9794static struct dwp_hash_table *
9795create_dwp_hash_table (struct dwp_file *dwp_file, int is_debug_types)
9796{
9797 struct objfile *objfile = dwarf2_per_objfile->objfile;
9798 bfd *dbfd = dwp_file->dbfd;
948f8e3d 9799 const gdb_byte *index_ptr, *index_end;
80626a55 9800 struct dwarf2_section_info *index;
73869dc2 9801 uint32_t version, nr_columns, nr_units, nr_slots;
80626a55
DE
9802 struct dwp_hash_table *htab;
9803
9804 if (is_debug_types)
9805 index = &dwp_file->sections.tu_index;
9806 else
9807 index = &dwp_file->sections.cu_index;
9808
9809 if (dwarf2_section_empty_p (index))
9810 return NULL;
9811 dwarf2_read_section (objfile, index);
9812
9813 index_ptr = index->buffer;
9814 index_end = index_ptr + index->size;
9815
9816 version = read_4_bytes (dbfd, index_ptr);
73869dc2
DE
9817 index_ptr += 4;
9818 if (version == 2)
9819 nr_columns = read_4_bytes (dbfd, index_ptr);
9820 else
9821 nr_columns = 0;
9822 index_ptr += 4;
80626a55
DE
9823 nr_units = read_4_bytes (dbfd, index_ptr);
9824 index_ptr += 4;
9825 nr_slots = read_4_bytes (dbfd, index_ptr);
9826 index_ptr += 4;
9827
73869dc2 9828 if (version != 1 && version != 2)
80626a55 9829 {
21aa081e 9830 error (_("Dwarf Error: unsupported DWP file version (%s)"
80626a55 9831 " [in module %s]"),
21aa081e 9832 pulongest (version), dwp_file->name);
80626a55
DE
9833 }
9834 if (nr_slots != (nr_slots & -nr_slots))
9835 {
21aa081e 9836 error (_("Dwarf Error: number of slots in DWP hash table (%s)"
80626a55 9837 " is not power of 2 [in module %s]"),
21aa081e 9838 pulongest (nr_slots), dwp_file->name);
80626a55
DE
9839 }
9840
9841 htab = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_hash_table);
73869dc2
DE
9842 htab->version = version;
9843 htab->nr_columns = nr_columns;
80626a55
DE
9844 htab->nr_units = nr_units;
9845 htab->nr_slots = nr_slots;
9846 htab->hash_table = index_ptr;
9847 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
73869dc2
DE
9848
9849 /* Exit early if the table is empty. */
9850 if (nr_slots == 0 || nr_units == 0
9851 || (version == 2 && nr_columns == 0))
9852 {
9853 /* All must be zero. */
9854 if (nr_slots != 0 || nr_units != 0
9855 || (version == 2 && nr_columns != 0))
9856 {
9857 complaint (&symfile_complaints,
9858 _("Empty DWP but nr_slots,nr_units,nr_columns not"
9859 " all zero [in modules %s]"),
9860 dwp_file->name);
9861 }
9862 return htab;
9863 }
9864
9865 if (version == 1)
9866 {
9867 htab->section_pool.v1.indices =
9868 htab->unit_table + sizeof (uint32_t) * nr_slots;
9869 /* It's harder to decide whether the section is too small in v1.
9870 V1 is deprecated anyway so we punt. */
9871 }
9872 else
9873 {
9874 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
9875 int *ids = htab->section_pool.v2.section_ids;
9876 /* Reverse map for error checking. */
9877 int ids_seen[DW_SECT_MAX + 1];
9878 int i;
9879
9880 if (nr_columns < 2)
9881 {
9882 error (_("Dwarf Error: bad DWP hash table, too few columns"
9883 " in section table [in module %s]"),
9884 dwp_file->name);
9885 }
9886 if (nr_columns > MAX_NR_V2_DWO_SECTIONS)
9887 {
9888 error (_("Dwarf Error: bad DWP hash table, too many columns"
9889 " in section table [in module %s]"),
9890 dwp_file->name);
9891 }
9892 memset (ids, 255, (DW_SECT_MAX + 1) * sizeof (int32_t));
9893 memset (ids_seen, 255, (DW_SECT_MAX + 1) * sizeof (int32_t));
9894 for (i = 0; i < nr_columns; ++i)
9895 {
9896 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
9897
9898 if (id < DW_SECT_MIN || id > DW_SECT_MAX)
9899 {
9900 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
9901 " in section table [in module %s]"),
9902 id, dwp_file->name);
9903 }
9904 if (ids_seen[id] != -1)
9905 {
9906 error (_("Dwarf Error: bad DWP hash table, duplicate section"
9907 " id %d in section table [in module %s]"),
9908 id, dwp_file->name);
9909 }
9910 ids_seen[id] = i;
9911 ids[i] = id;
9912 }
9913 /* Must have exactly one info or types section. */
9914 if (((ids_seen[DW_SECT_INFO] != -1)
9915 + (ids_seen[DW_SECT_TYPES] != -1))
9916 != 1)
9917 {
9918 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
9919 " DWO info/types section [in module %s]"),
9920 dwp_file->name);
9921 }
9922 /* Must have an abbrev section. */
9923 if (ids_seen[DW_SECT_ABBREV] == -1)
9924 {
9925 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
9926 " section [in module %s]"),
9927 dwp_file->name);
9928 }
9929 htab->section_pool.v2.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
9930 htab->section_pool.v2.sizes =
9931 htab->section_pool.v2.offsets + (sizeof (uint32_t)
9932 * nr_units * nr_columns);
9933 if ((htab->section_pool.v2.sizes + (sizeof (uint32_t)
9934 * nr_units * nr_columns))
9935 > index_end)
9936 {
9937 error (_("Dwarf Error: DWP index section is corrupt (too small)"
9938 " [in module %s]"),
9939 dwp_file->name);
9940 }
9941 }
80626a55
DE
9942
9943 return htab;
9944}
9945
9946/* Update SECTIONS with the data from SECTP.
9947
9948 This function is like the other "locate" section routines that are
9949 passed to bfd_map_over_sections, but in this context the sections to
73869dc2 9950 read comes from the DWP V1 hash table, not the full ELF section table.
80626a55
DE
9951
9952 The result is non-zero for success, or zero if an error was found. */
9953
9954static int
73869dc2
DE
9955locate_v1_virtual_dwo_sections (asection *sectp,
9956 struct virtual_v1_dwo_sections *sections)
80626a55
DE
9957{
9958 const struct dwop_section_names *names = &dwop_section_names;
9959
9960 if (section_is_p (sectp->name, &names->abbrev_dwo))
9961 {
9962 /* There can be only one. */
049412e3 9963 if (sections->abbrev.s.section != NULL)
80626a55 9964 return 0;
049412e3 9965 sections->abbrev.s.section = sectp;
80626a55
DE
9966 sections->abbrev.size = bfd_get_section_size (sectp);
9967 }
9968 else if (section_is_p (sectp->name, &names->info_dwo)
9969 || section_is_p (sectp->name, &names->types_dwo))
9970 {
9971 /* There can be only one. */
049412e3 9972 if (sections->info_or_types.s.section != NULL)
80626a55 9973 return 0;
049412e3 9974 sections->info_or_types.s.section = sectp;
80626a55
DE
9975 sections->info_or_types.size = bfd_get_section_size (sectp);
9976 }
9977 else if (section_is_p (sectp->name, &names->line_dwo))
9978 {
9979 /* There can be only one. */
049412e3 9980 if (sections->line.s.section != NULL)
80626a55 9981 return 0;
049412e3 9982 sections->line.s.section = sectp;
80626a55
DE
9983 sections->line.size = bfd_get_section_size (sectp);
9984 }
9985 else if (section_is_p (sectp->name, &names->loc_dwo))
9986 {
9987 /* There can be only one. */
049412e3 9988 if (sections->loc.s.section != NULL)
80626a55 9989 return 0;
049412e3 9990 sections->loc.s.section = sectp;
80626a55
DE
9991 sections->loc.size = bfd_get_section_size (sectp);
9992 }
9993 else if (section_is_p (sectp->name, &names->macinfo_dwo))
9994 {
9995 /* There can be only one. */
049412e3 9996 if (sections->macinfo.s.section != NULL)
80626a55 9997 return 0;
049412e3 9998 sections->macinfo.s.section = sectp;
80626a55
DE
9999 sections->macinfo.size = bfd_get_section_size (sectp);
10000 }
10001 else if (section_is_p (sectp->name, &names->macro_dwo))
10002 {
10003 /* There can be only one. */
049412e3 10004 if (sections->macro.s.section != NULL)
80626a55 10005 return 0;
049412e3 10006 sections->macro.s.section = sectp;
80626a55
DE
10007 sections->macro.size = bfd_get_section_size (sectp);
10008 }
10009 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
10010 {
10011 /* There can be only one. */
049412e3 10012 if (sections->str_offsets.s.section != NULL)
80626a55 10013 return 0;
049412e3 10014 sections->str_offsets.s.section = sectp;
80626a55
DE
10015 sections->str_offsets.size = bfd_get_section_size (sectp);
10016 }
10017 else
10018 {
10019 /* No other kind of section is valid. */
10020 return 0;
10021 }
10022
10023 return 1;
10024}
10025
73869dc2
DE
10026/* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
10027 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
10028 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
10029 This is for DWP version 1 files. */
80626a55
DE
10030
10031static struct dwo_unit *
73869dc2
DE
10032create_dwo_unit_in_dwp_v1 (struct dwp_file *dwp_file,
10033 uint32_t unit_index,
10034 const char *comp_dir,
10035 ULONGEST signature, int is_debug_types)
80626a55
DE
10036{
10037 struct objfile *objfile = dwarf2_per_objfile->objfile;
73869dc2
DE
10038 const struct dwp_hash_table *dwp_htab =
10039 is_debug_types ? dwp_file->tus : dwp_file->cus;
80626a55
DE
10040 bfd *dbfd = dwp_file->dbfd;
10041 const char *kind = is_debug_types ? "TU" : "CU";
10042 struct dwo_file *dwo_file;
10043 struct dwo_unit *dwo_unit;
73869dc2 10044 struct virtual_v1_dwo_sections sections;
80626a55
DE
10045 void **dwo_file_slot;
10046 char *virtual_dwo_name;
80626a55
DE
10047 struct cleanup *cleanups;
10048 int i;
10049
73869dc2
DE
10050 gdb_assert (dwp_file->version == 1);
10051
b4f54984 10052 if (dwarf_read_debug)
80626a55 10053 {
73869dc2 10054 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V1 file: %s\n",
80626a55 10055 kind,
73869dc2 10056 pulongest (unit_index), hex_string (signature),
80626a55
DE
10057 dwp_file->name);
10058 }
10059
19ac8c2e 10060 /* Fetch the sections of this DWO unit.
80626a55
DE
10061 Put a limit on the number of sections we look for so that bad data
10062 doesn't cause us to loop forever. */
10063
73869dc2 10064#define MAX_NR_V1_DWO_SECTIONS \
80626a55
DE
10065 (1 /* .debug_info or .debug_types */ \
10066 + 1 /* .debug_abbrev */ \
10067 + 1 /* .debug_line */ \
10068 + 1 /* .debug_loc */ \
10069 + 1 /* .debug_str_offsets */ \
19ac8c2e 10070 + 1 /* .debug_macro or .debug_macinfo */ \
80626a55
DE
10071 + 1 /* trailing zero */)
10072
10073 memset (&sections, 0, sizeof (sections));
10074 cleanups = make_cleanup (null_cleanup, 0);
10075
73869dc2 10076 for (i = 0; i < MAX_NR_V1_DWO_SECTIONS; ++i)
80626a55
DE
10077 {
10078 asection *sectp;
10079 uint32_t section_nr =
10080 read_4_bytes (dbfd,
73869dc2
DE
10081 dwp_htab->section_pool.v1.indices
10082 + (unit_index + i) * sizeof (uint32_t));
80626a55
DE
10083
10084 if (section_nr == 0)
10085 break;
10086 if (section_nr >= dwp_file->num_sections)
10087 {
10088 error (_("Dwarf Error: bad DWP hash table, section number too large"
10089 " [in module %s]"),
10090 dwp_file->name);
10091 }
10092
10093 sectp = dwp_file->elf_sections[section_nr];
73869dc2 10094 if (! locate_v1_virtual_dwo_sections (sectp, &sections))
80626a55
DE
10095 {
10096 error (_("Dwarf Error: bad DWP hash table, invalid section found"
10097 " [in module %s]"),
10098 dwp_file->name);
10099 }
10100 }
10101
10102 if (i < 2
a32a8923
DE
10103 || dwarf2_section_empty_p (&sections.info_or_types)
10104 || dwarf2_section_empty_p (&sections.abbrev))
80626a55
DE
10105 {
10106 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
10107 " [in module %s]"),
10108 dwp_file->name);
10109 }
73869dc2 10110 if (i == MAX_NR_V1_DWO_SECTIONS)
80626a55
DE
10111 {
10112 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
10113 " [in module %s]"),
10114 dwp_file->name);
10115 }
10116
10117 /* It's easier for the rest of the code if we fake a struct dwo_file and
10118 have dwo_unit "live" in that. At least for now.
10119
10120 The DWP file can be made up of a random collection of CUs and TUs.
c766f7ec 10121 However, for each CU + set of TUs that came from the same original DWO
57d63ce2
DE
10122 file, we can combine them back into a virtual DWO file to save space
10123 (fewer struct dwo_file objects to allocate). Remember that for really
80626a55
DE
10124 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
10125
2792b94d
PM
10126 virtual_dwo_name =
10127 xstrprintf ("virtual-dwo/%d-%d-%d-%d",
a32a8923
DE
10128 get_section_id (&sections.abbrev),
10129 get_section_id (&sections.line),
10130 get_section_id (&sections.loc),
10131 get_section_id (&sections.str_offsets));
80626a55
DE
10132 make_cleanup (xfree, virtual_dwo_name);
10133 /* Can we use an existing virtual DWO file? */
0ac5b59e 10134 dwo_file_slot = lookup_dwo_file_slot (virtual_dwo_name, comp_dir);
80626a55
DE
10135 /* Create one if necessary. */
10136 if (*dwo_file_slot == NULL)
10137 {
b4f54984 10138 if (dwarf_read_debug)
80626a55
DE
10139 {
10140 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
10141 virtual_dwo_name);
10142 }
10143 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
224c3ddb
SM
10144 dwo_file->dwo_name
10145 = (const char *) obstack_copy0 (&objfile->objfile_obstack,
10146 virtual_dwo_name,
10147 strlen (virtual_dwo_name));
0ac5b59e 10148 dwo_file->comp_dir = comp_dir;
80626a55
DE
10149 dwo_file->sections.abbrev = sections.abbrev;
10150 dwo_file->sections.line = sections.line;
10151 dwo_file->sections.loc = sections.loc;
10152 dwo_file->sections.macinfo = sections.macinfo;
10153 dwo_file->sections.macro = sections.macro;
10154 dwo_file->sections.str_offsets = sections.str_offsets;
10155 /* The "str" section is global to the entire DWP file. */
10156 dwo_file->sections.str = dwp_file->sections.str;
57d63ce2 10157 /* The info or types section is assigned below to dwo_unit,
80626a55
DE
10158 there's no need to record it in dwo_file.
10159 Also, we can't simply record type sections in dwo_file because
10160 we record a pointer into the vector in dwo_unit. As we collect more
10161 types we'll grow the vector and eventually have to reallocate space
57d63ce2
DE
10162 for it, invalidating all copies of pointers into the previous
10163 contents. */
80626a55
DE
10164 *dwo_file_slot = dwo_file;
10165 }
10166 else
10167 {
b4f54984 10168 if (dwarf_read_debug)
80626a55
DE
10169 {
10170 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
10171 virtual_dwo_name);
10172 }
9a3c8263 10173 dwo_file = (struct dwo_file *) *dwo_file_slot;
80626a55
DE
10174 }
10175 do_cleanups (cleanups);
10176
10177 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
10178 dwo_unit->dwo_file = dwo_file;
10179 dwo_unit->signature = signature;
8d749320
SM
10180 dwo_unit->section =
10181 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
8a0459fd 10182 *dwo_unit->section = sections.info_or_types;
57d63ce2 10183 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
80626a55
DE
10184
10185 return dwo_unit;
10186}
10187
73869dc2
DE
10188/* Subroutine of create_dwo_unit_in_dwp_v2 to simplify it.
10189 Given a pointer to the containing section SECTION, and OFFSET,SIZE of the
10190 piece within that section used by a TU/CU, return a virtual section
10191 of just that piece. */
10192
10193static struct dwarf2_section_info
10194create_dwp_v2_section (struct dwarf2_section_info *section,
10195 bfd_size_type offset, bfd_size_type size)
10196{
10197 struct dwarf2_section_info result;
10198 asection *sectp;
10199
10200 gdb_assert (section != NULL);
10201 gdb_assert (!section->is_virtual);
10202
10203 memset (&result, 0, sizeof (result));
10204 result.s.containing_section = section;
10205 result.is_virtual = 1;
10206
10207 if (size == 0)
10208 return result;
10209
10210 sectp = get_section_bfd_section (section);
10211
10212 /* Flag an error if the piece denoted by OFFSET,SIZE is outside the
10213 bounds of the real section. This is a pretty-rare event, so just
10214 flag an error (easier) instead of a warning and trying to cope. */
10215 if (sectp == NULL
10216 || offset + size > bfd_get_section_size (sectp))
10217 {
10218 bfd *abfd = sectp->owner;
10219
10220 error (_("Dwarf Error: Bad DWP V2 section info, doesn't fit"
10221 " in section %s [in module %s]"),
10222 sectp ? bfd_section_name (abfd, sectp) : "<unknown>",
10223 objfile_name (dwarf2_per_objfile->objfile));
10224 }
10225
10226 result.virtual_offset = offset;
10227 result.size = size;
10228 return result;
10229}
10230
10231/* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
10232 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
10233 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
10234 This is for DWP version 2 files. */
10235
10236static struct dwo_unit *
10237create_dwo_unit_in_dwp_v2 (struct dwp_file *dwp_file,
10238 uint32_t unit_index,
10239 const char *comp_dir,
10240 ULONGEST signature, int is_debug_types)
10241{
10242 struct objfile *objfile = dwarf2_per_objfile->objfile;
10243 const struct dwp_hash_table *dwp_htab =
10244 is_debug_types ? dwp_file->tus : dwp_file->cus;
10245 bfd *dbfd = dwp_file->dbfd;
10246 const char *kind = is_debug_types ? "TU" : "CU";
10247 struct dwo_file *dwo_file;
10248 struct dwo_unit *dwo_unit;
10249 struct virtual_v2_dwo_sections sections;
10250 void **dwo_file_slot;
10251 char *virtual_dwo_name;
73869dc2
DE
10252 struct cleanup *cleanups;
10253 int i;
10254
10255 gdb_assert (dwp_file->version == 2);
10256
b4f54984 10257 if (dwarf_read_debug)
73869dc2
DE
10258 {
10259 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V2 file: %s\n",
10260 kind,
10261 pulongest (unit_index), hex_string (signature),
10262 dwp_file->name);
10263 }
10264
10265 /* Fetch the section offsets of this DWO unit. */
10266
10267 memset (&sections, 0, sizeof (sections));
10268 cleanups = make_cleanup (null_cleanup, 0);
10269
10270 for (i = 0; i < dwp_htab->nr_columns; ++i)
10271 {
10272 uint32_t offset = read_4_bytes (dbfd,
10273 dwp_htab->section_pool.v2.offsets
10274 + (((unit_index - 1) * dwp_htab->nr_columns
10275 + i)
10276 * sizeof (uint32_t)));
10277 uint32_t size = read_4_bytes (dbfd,
10278 dwp_htab->section_pool.v2.sizes
10279 + (((unit_index - 1) * dwp_htab->nr_columns
10280 + i)
10281 * sizeof (uint32_t)));
10282
10283 switch (dwp_htab->section_pool.v2.section_ids[i])
10284 {
10285 case DW_SECT_INFO:
10286 case DW_SECT_TYPES:
10287 sections.info_or_types_offset = offset;
10288 sections.info_or_types_size = size;
10289 break;
10290 case DW_SECT_ABBREV:
10291 sections.abbrev_offset = offset;
10292 sections.abbrev_size = size;
10293 break;
10294 case DW_SECT_LINE:
10295 sections.line_offset = offset;
10296 sections.line_size = size;
10297 break;
10298 case DW_SECT_LOC:
10299 sections.loc_offset = offset;
10300 sections.loc_size = size;
10301 break;
10302 case DW_SECT_STR_OFFSETS:
10303 sections.str_offsets_offset = offset;
10304 sections.str_offsets_size = size;
10305 break;
10306 case DW_SECT_MACINFO:
10307 sections.macinfo_offset = offset;
10308 sections.macinfo_size = size;
10309 break;
10310 case DW_SECT_MACRO:
10311 sections.macro_offset = offset;
10312 sections.macro_size = size;
10313 break;
10314 }
10315 }
10316
10317 /* It's easier for the rest of the code if we fake a struct dwo_file and
10318 have dwo_unit "live" in that. At least for now.
10319
10320 The DWP file can be made up of a random collection of CUs and TUs.
10321 However, for each CU + set of TUs that came from the same original DWO
10322 file, we can combine them back into a virtual DWO file to save space
10323 (fewer struct dwo_file objects to allocate). Remember that for really
10324 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
10325
10326 virtual_dwo_name =
10327 xstrprintf ("virtual-dwo/%ld-%ld-%ld-%ld",
10328 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
10329 (long) (sections.line_size ? sections.line_offset : 0),
10330 (long) (sections.loc_size ? sections.loc_offset : 0),
10331 (long) (sections.str_offsets_size
10332 ? sections.str_offsets_offset : 0));
10333 make_cleanup (xfree, virtual_dwo_name);
10334 /* Can we use an existing virtual DWO file? */
10335 dwo_file_slot = lookup_dwo_file_slot (virtual_dwo_name, comp_dir);
10336 /* Create one if necessary. */
10337 if (*dwo_file_slot == NULL)
10338 {
b4f54984 10339 if (dwarf_read_debug)
73869dc2
DE
10340 {
10341 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
10342 virtual_dwo_name);
10343 }
10344 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
224c3ddb
SM
10345 dwo_file->dwo_name
10346 = (const char *) obstack_copy0 (&objfile->objfile_obstack,
10347 virtual_dwo_name,
10348 strlen (virtual_dwo_name));
73869dc2
DE
10349 dwo_file->comp_dir = comp_dir;
10350 dwo_file->sections.abbrev =
10351 create_dwp_v2_section (&dwp_file->sections.abbrev,
10352 sections.abbrev_offset, sections.abbrev_size);
10353 dwo_file->sections.line =
10354 create_dwp_v2_section (&dwp_file->sections.line,
10355 sections.line_offset, sections.line_size);
10356 dwo_file->sections.loc =
10357 create_dwp_v2_section (&dwp_file->sections.loc,
10358 sections.loc_offset, sections.loc_size);
10359 dwo_file->sections.macinfo =
10360 create_dwp_v2_section (&dwp_file->sections.macinfo,
10361 sections.macinfo_offset, sections.macinfo_size);
10362 dwo_file->sections.macro =
10363 create_dwp_v2_section (&dwp_file->sections.macro,
10364 sections.macro_offset, sections.macro_size);
10365 dwo_file->sections.str_offsets =
10366 create_dwp_v2_section (&dwp_file->sections.str_offsets,
10367 sections.str_offsets_offset,
10368 sections.str_offsets_size);
10369 /* The "str" section is global to the entire DWP file. */
10370 dwo_file->sections.str = dwp_file->sections.str;
10371 /* The info or types section is assigned below to dwo_unit,
10372 there's no need to record it in dwo_file.
10373 Also, we can't simply record type sections in dwo_file because
10374 we record a pointer into the vector in dwo_unit. As we collect more
10375 types we'll grow the vector and eventually have to reallocate space
10376 for it, invalidating all copies of pointers into the previous
10377 contents. */
10378 *dwo_file_slot = dwo_file;
10379 }
10380 else
10381 {
b4f54984 10382 if (dwarf_read_debug)
73869dc2
DE
10383 {
10384 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
10385 virtual_dwo_name);
10386 }
9a3c8263 10387 dwo_file = (struct dwo_file *) *dwo_file_slot;
73869dc2
DE
10388 }
10389 do_cleanups (cleanups);
10390
10391 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
10392 dwo_unit->dwo_file = dwo_file;
10393 dwo_unit->signature = signature;
8d749320
SM
10394 dwo_unit->section =
10395 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
73869dc2
DE
10396 *dwo_unit->section = create_dwp_v2_section (is_debug_types
10397 ? &dwp_file->sections.types
10398 : &dwp_file->sections.info,
10399 sections.info_or_types_offset,
10400 sections.info_or_types_size);
10401 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
10402
10403 return dwo_unit;
10404}
10405
57d63ce2
DE
10406/* Lookup the DWO unit with SIGNATURE in DWP_FILE.
10407 Returns NULL if the signature isn't found. */
80626a55
DE
10408
10409static struct dwo_unit *
57d63ce2
DE
10410lookup_dwo_unit_in_dwp (struct dwp_file *dwp_file, const char *comp_dir,
10411 ULONGEST signature, int is_debug_types)
80626a55 10412{
57d63ce2
DE
10413 const struct dwp_hash_table *dwp_htab =
10414 is_debug_types ? dwp_file->tus : dwp_file->cus;
80626a55 10415 bfd *dbfd = dwp_file->dbfd;
57d63ce2 10416 uint32_t mask = dwp_htab->nr_slots - 1;
80626a55
DE
10417 uint32_t hash = signature & mask;
10418 uint32_t hash2 = ((signature >> 32) & mask) | 1;
10419 unsigned int i;
10420 void **slot;
870f88f7 10421 struct dwo_unit find_dwo_cu;
80626a55
DE
10422
10423 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
10424 find_dwo_cu.signature = signature;
19ac8c2e
DE
10425 slot = htab_find_slot (is_debug_types
10426 ? dwp_file->loaded_tus
10427 : dwp_file->loaded_cus,
10428 &find_dwo_cu, INSERT);
80626a55
DE
10429
10430 if (*slot != NULL)
9a3c8263 10431 return (struct dwo_unit *) *slot;
80626a55
DE
10432
10433 /* Use a for loop so that we don't loop forever on bad debug info. */
57d63ce2 10434 for (i = 0; i < dwp_htab->nr_slots; ++i)
80626a55
DE
10435 {
10436 ULONGEST signature_in_table;
10437
10438 signature_in_table =
57d63ce2 10439 read_8_bytes (dbfd, dwp_htab->hash_table + hash * sizeof (uint64_t));
80626a55
DE
10440 if (signature_in_table == signature)
10441 {
57d63ce2
DE
10442 uint32_t unit_index =
10443 read_4_bytes (dbfd,
10444 dwp_htab->unit_table + hash * sizeof (uint32_t));
80626a55 10445
73869dc2
DE
10446 if (dwp_file->version == 1)
10447 {
10448 *slot = create_dwo_unit_in_dwp_v1 (dwp_file, unit_index,
10449 comp_dir, signature,
10450 is_debug_types);
10451 }
10452 else
10453 {
10454 *slot = create_dwo_unit_in_dwp_v2 (dwp_file, unit_index,
10455 comp_dir, signature,
10456 is_debug_types);
10457 }
9a3c8263 10458 return (struct dwo_unit *) *slot;
80626a55
DE
10459 }
10460 if (signature_in_table == 0)
10461 return NULL;
10462 hash = (hash + hash2) & mask;
10463 }
10464
10465 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
10466 " [in module %s]"),
10467 dwp_file->name);
10468}
10469
ab5088bf 10470/* Subroutine of open_dwo_file,open_dwp_file to simplify them.
3019eac3
DE
10471 Open the file specified by FILE_NAME and hand it off to BFD for
10472 preliminary analysis. Return a newly initialized bfd *, which
10473 includes a canonicalized copy of FILE_NAME.
80626a55 10474 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
6ac97d4c
DE
10475 SEARCH_CWD is true if the current directory is to be searched.
10476 It will be searched before debug-file-directory.
13aaf454
DE
10477 If successful, the file is added to the bfd include table of the
10478 objfile's bfd (see gdb_bfd_record_inclusion).
6ac97d4c 10479 If unable to find/open the file, return NULL.
3019eac3
DE
10480 NOTE: This function is derived from symfile_bfd_open. */
10481
10482static bfd *
6ac97d4c 10483try_open_dwop_file (const char *file_name, int is_dwp, int search_cwd)
3019eac3
DE
10484{
10485 bfd *sym_bfd;
80626a55 10486 int desc, flags;
3019eac3 10487 char *absolute_name;
9c02c129
DE
10488 /* Blech. OPF_TRY_CWD_FIRST also disables searching the path list if
10489 FILE_NAME contains a '/'. So we can't use it. Instead prepend "."
10490 to debug_file_directory. */
10491 char *search_path;
10492 static const char dirname_separator_string[] = { DIRNAME_SEPARATOR, '\0' };
10493
6ac97d4c
DE
10494 if (search_cwd)
10495 {
10496 if (*debug_file_directory != '\0')
10497 search_path = concat (".", dirname_separator_string,
b36cec19 10498 debug_file_directory, (char *) NULL);
6ac97d4c
DE
10499 else
10500 search_path = xstrdup (".");
10501 }
9c02c129 10502 else
6ac97d4c 10503 search_path = xstrdup (debug_file_directory);
3019eac3 10504
492c0ab7 10505 flags = OPF_RETURN_REALPATH;
80626a55
DE
10506 if (is_dwp)
10507 flags |= OPF_SEARCH_IN_PATH;
9c02c129 10508 desc = openp (search_path, flags, file_name,
3019eac3 10509 O_RDONLY | O_BINARY, &absolute_name);
9c02c129 10510 xfree (search_path);
3019eac3
DE
10511 if (desc < 0)
10512 return NULL;
10513
bb397797 10514 sym_bfd = gdb_bfd_open (absolute_name, gnutarget, desc);
a4453b7e 10515 xfree (absolute_name);
9c02c129
DE
10516 if (sym_bfd == NULL)
10517 return NULL;
3019eac3
DE
10518 bfd_set_cacheable (sym_bfd, 1);
10519
10520 if (!bfd_check_format (sym_bfd, bfd_object))
10521 {
cbb099e8 10522 gdb_bfd_unref (sym_bfd); /* This also closes desc. */
3019eac3
DE
10523 return NULL;
10524 }
10525
13aaf454
DE
10526 /* Success. Record the bfd as having been included by the objfile's bfd.
10527 This is important because things like demangled_names_hash lives in the
10528 objfile's per_bfd space and may have references to things like symbol
10529 names that live in the DWO/DWP file's per_bfd space. PR 16426. */
10530 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd, sym_bfd);
10531
3019eac3
DE
10532 return sym_bfd;
10533}
10534
ab5088bf 10535/* Try to open DWO file FILE_NAME.
3019eac3
DE
10536 COMP_DIR is the DW_AT_comp_dir attribute.
10537 The result is the bfd handle of the file.
10538 If there is a problem finding or opening the file, return NULL.
10539 Upon success, the canonicalized path of the file is stored in the bfd,
10540 same as symfile_bfd_open. */
10541
10542static bfd *
ab5088bf 10543open_dwo_file (const char *file_name, const char *comp_dir)
3019eac3
DE
10544{
10545 bfd *abfd;
3019eac3 10546
80626a55 10547 if (IS_ABSOLUTE_PATH (file_name))
6ac97d4c 10548 return try_open_dwop_file (file_name, 0 /*is_dwp*/, 0 /*search_cwd*/);
3019eac3
DE
10549
10550 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
10551
10552 if (comp_dir != NULL)
10553 {
b36cec19
PA
10554 char *path_to_try = concat (comp_dir, SLASH_STRING,
10555 file_name, (char *) NULL);
3019eac3
DE
10556
10557 /* NOTE: If comp_dir is a relative path, this will also try the
10558 search path, which seems useful. */
6ac97d4c 10559 abfd = try_open_dwop_file (path_to_try, 0 /*is_dwp*/, 1 /*search_cwd*/);
3019eac3
DE
10560 xfree (path_to_try);
10561 if (abfd != NULL)
10562 return abfd;
10563 }
10564
10565 /* That didn't work, try debug-file-directory, which, despite its name,
10566 is a list of paths. */
10567
10568 if (*debug_file_directory == '\0')
10569 return NULL;
10570
6ac97d4c 10571 return try_open_dwop_file (file_name, 0 /*is_dwp*/, 1 /*search_cwd*/);
3019eac3
DE
10572}
10573
80626a55
DE
10574/* This function is mapped across the sections and remembers the offset and
10575 size of each of the DWO debugging sections we are interested in. */
10576
10577static void
10578dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp, void *dwo_sections_ptr)
10579{
9a3c8263 10580 struct dwo_sections *dwo_sections = (struct dwo_sections *) dwo_sections_ptr;
80626a55
DE
10581 const struct dwop_section_names *names = &dwop_section_names;
10582
10583 if (section_is_p (sectp->name, &names->abbrev_dwo))
10584 {
049412e3 10585 dwo_sections->abbrev.s.section = sectp;
80626a55
DE
10586 dwo_sections->abbrev.size = bfd_get_section_size (sectp);
10587 }
10588 else if (section_is_p (sectp->name, &names->info_dwo))
10589 {
049412e3 10590 dwo_sections->info.s.section = sectp;
80626a55
DE
10591 dwo_sections->info.size = bfd_get_section_size (sectp);
10592 }
10593 else if (section_is_p (sectp->name, &names->line_dwo))
10594 {
049412e3 10595 dwo_sections->line.s.section = sectp;
80626a55
DE
10596 dwo_sections->line.size = bfd_get_section_size (sectp);
10597 }
10598 else if (section_is_p (sectp->name, &names->loc_dwo))
10599 {
049412e3 10600 dwo_sections->loc.s.section = sectp;
80626a55
DE
10601 dwo_sections->loc.size = bfd_get_section_size (sectp);
10602 }
10603 else if (section_is_p (sectp->name, &names->macinfo_dwo))
10604 {
049412e3 10605 dwo_sections->macinfo.s.section = sectp;
80626a55
DE
10606 dwo_sections->macinfo.size = bfd_get_section_size (sectp);
10607 }
10608 else if (section_is_p (sectp->name, &names->macro_dwo))
10609 {
049412e3 10610 dwo_sections->macro.s.section = sectp;
80626a55
DE
10611 dwo_sections->macro.size = bfd_get_section_size (sectp);
10612 }
10613 else if (section_is_p (sectp->name, &names->str_dwo))
10614 {
049412e3 10615 dwo_sections->str.s.section = sectp;
80626a55
DE
10616 dwo_sections->str.size = bfd_get_section_size (sectp);
10617 }
10618 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
10619 {
049412e3 10620 dwo_sections->str_offsets.s.section = sectp;
80626a55
DE
10621 dwo_sections->str_offsets.size = bfd_get_section_size (sectp);
10622 }
10623 else if (section_is_p (sectp->name, &names->types_dwo))
10624 {
10625 struct dwarf2_section_info type_section;
10626
10627 memset (&type_section, 0, sizeof (type_section));
049412e3 10628 type_section.s.section = sectp;
80626a55
DE
10629 type_section.size = bfd_get_section_size (sectp);
10630 VEC_safe_push (dwarf2_section_info_def, dwo_sections->types,
10631 &type_section);
10632 }
10633}
10634
ab5088bf 10635/* Initialize the use of the DWO file specified by DWO_NAME and referenced
19c3d4c9 10636 by PER_CU. This is for the non-DWP case.
80626a55 10637 The result is NULL if DWO_NAME can't be found. */
3019eac3
DE
10638
10639static struct dwo_file *
0ac5b59e
DE
10640open_and_init_dwo_file (struct dwarf2_per_cu_data *per_cu,
10641 const char *dwo_name, const char *comp_dir)
3019eac3
DE
10642{
10643 struct objfile *objfile = dwarf2_per_objfile->objfile;
80626a55
DE
10644 struct dwo_file *dwo_file;
10645 bfd *dbfd;
3019eac3
DE
10646 struct cleanup *cleanups;
10647
ab5088bf 10648 dbfd = open_dwo_file (dwo_name, comp_dir);
80626a55
DE
10649 if (dbfd == NULL)
10650 {
b4f54984 10651 if (dwarf_read_debug)
80626a55
DE
10652 fprintf_unfiltered (gdb_stdlog, "DWO file not found: %s\n", dwo_name);
10653 return NULL;
10654 }
10655 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
0ac5b59e
DE
10656 dwo_file->dwo_name = dwo_name;
10657 dwo_file->comp_dir = comp_dir;
80626a55 10658 dwo_file->dbfd = dbfd;
3019eac3
DE
10659
10660 cleanups = make_cleanup (free_dwo_file_cleanup, dwo_file);
10661
80626a55 10662 bfd_map_over_sections (dbfd, dwarf2_locate_dwo_sections, &dwo_file->sections);
3019eac3 10663
19c3d4c9 10664 dwo_file->cu = create_dwo_cu (dwo_file);
3019eac3
DE
10665
10666 dwo_file->tus = create_debug_types_hash_table (dwo_file,
10667 dwo_file->sections.types);
10668
10669 discard_cleanups (cleanups);
10670
b4f54984 10671 if (dwarf_read_debug)
80626a55
DE
10672 fprintf_unfiltered (gdb_stdlog, "DWO file found: %s\n", dwo_name);
10673
3019eac3
DE
10674 return dwo_file;
10675}
10676
80626a55 10677/* This function is mapped across the sections and remembers the offset and
73869dc2
DE
10678 size of each of the DWP debugging sections common to version 1 and 2 that
10679 we are interested in. */
3019eac3 10680
80626a55 10681static void
73869dc2
DE
10682dwarf2_locate_common_dwp_sections (bfd *abfd, asection *sectp,
10683 void *dwp_file_ptr)
3019eac3 10684{
9a3c8263 10685 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
80626a55
DE
10686 const struct dwop_section_names *names = &dwop_section_names;
10687 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
3019eac3 10688
80626a55 10689 /* Record the ELF section number for later lookup: this is what the
73869dc2 10690 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
80626a55
DE
10691 gdb_assert (elf_section_nr < dwp_file->num_sections);
10692 dwp_file->elf_sections[elf_section_nr] = sectp;
3019eac3 10693
80626a55
DE
10694 /* Look for specific sections that we need. */
10695 if (section_is_p (sectp->name, &names->str_dwo))
10696 {
049412e3 10697 dwp_file->sections.str.s.section = sectp;
80626a55
DE
10698 dwp_file->sections.str.size = bfd_get_section_size (sectp);
10699 }
10700 else if (section_is_p (sectp->name, &names->cu_index))
10701 {
049412e3 10702 dwp_file->sections.cu_index.s.section = sectp;
80626a55
DE
10703 dwp_file->sections.cu_index.size = bfd_get_section_size (sectp);
10704 }
10705 else if (section_is_p (sectp->name, &names->tu_index))
10706 {
049412e3 10707 dwp_file->sections.tu_index.s.section = sectp;
80626a55
DE
10708 dwp_file->sections.tu_index.size = bfd_get_section_size (sectp);
10709 }
10710}
3019eac3 10711
73869dc2
DE
10712/* This function is mapped across the sections and remembers the offset and
10713 size of each of the DWP version 2 debugging sections that we are interested
10714 in. This is split into a separate function because we don't know if we
10715 have version 1 or 2 until we parse the cu_index/tu_index sections. */
10716
10717static void
10718dwarf2_locate_v2_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
10719{
9a3c8263 10720 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
73869dc2
DE
10721 const struct dwop_section_names *names = &dwop_section_names;
10722 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
10723
10724 /* Record the ELF section number for later lookup: this is what the
10725 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
10726 gdb_assert (elf_section_nr < dwp_file->num_sections);
10727 dwp_file->elf_sections[elf_section_nr] = sectp;
10728
10729 /* Look for specific sections that we need. */
10730 if (section_is_p (sectp->name, &names->abbrev_dwo))
10731 {
049412e3 10732 dwp_file->sections.abbrev.s.section = sectp;
73869dc2
DE
10733 dwp_file->sections.abbrev.size = bfd_get_section_size (sectp);
10734 }
10735 else if (section_is_p (sectp->name, &names->info_dwo))
10736 {
049412e3 10737 dwp_file->sections.info.s.section = sectp;
73869dc2
DE
10738 dwp_file->sections.info.size = bfd_get_section_size (sectp);
10739 }
10740 else if (section_is_p (sectp->name, &names->line_dwo))
10741 {
049412e3 10742 dwp_file->sections.line.s.section = sectp;
73869dc2
DE
10743 dwp_file->sections.line.size = bfd_get_section_size (sectp);
10744 }
10745 else if (section_is_p (sectp->name, &names->loc_dwo))
10746 {
049412e3 10747 dwp_file->sections.loc.s.section = sectp;
73869dc2
DE
10748 dwp_file->sections.loc.size = bfd_get_section_size (sectp);
10749 }
10750 else if (section_is_p (sectp->name, &names->macinfo_dwo))
10751 {
049412e3 10752 dwp_file->sections.macinfo.s.section = sectp;
73869dc2
DE
10753 dwp_file->sections.macinfo.size = bfd_get_section_size (sectp);
10754 }
10755 else if (section_is_p (sectp->name, &names->macro_dwo))
10756 {
049412e3 10757 dwp_file->sections.macro.s.section = sectp;
73869dc2
DE
10758 dwp_file->sections.macro.size = bfd_get_section_size (sectp);
10759 }
10760 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
10761 {
049412e3 10762 dwp_file->sections.str_offsets.s.section = sectp;
73869dc2
DE
10763 dwp_file->sections.str_offsets.size = bfd_get_section_size (sectp);
10764 }
10765 else if (section_is_p (sectp->name, &names->types_dwo))
10766 {
049412e3 10767 dwp_file->sections.types.s.section = sectp;
73869dc2
DE
10768 dwp_file->sections.types.size = bfd_get_section_size (sectp);
10769 }
10770}
10771
80626a55 10772/* Hash function for dwp_file loaded CUs/TUs. */
3019eac3 10773
80626a55
DE
10774static hashval_t
10775hash_dwp_loaded_cutus (const void *item)
10776{
9a3c8263 10777 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
3019eac3 10778
80626a55
DE
10779 /* This drops the top 32 bits of the signature, but is ok for a hash. */
10780 return dwo_unit->signature;
3019eac3
DE
10781}
10782
80626a55 10783/* Equality function for dwp_file loaded CUs/TUs. */
3019eac3 10784
80626a55
DE
10785static int
10786eq_dwp_loaded_cutus (const void *a, const void *b)
3019eac3 10787{
9a3c8263
SM
10788 const struct dwo_unit *dua = (const struct dwo_unit *) a;
10789 const struct dwo_unit *dub = (const struct dwo_unit *) b;
3019eac3 10790
80626a55
DE
10791 return dua->signature == dub->signature;
10792}
3019eac3 10793
80626a55 10794/* Allocate a hash table for dwp_file loaded CUs/TUs. */
3019eac3 10795
80626a55
DE
10796static htab_t
10797allocate_dwp_loaded_cutus_table (struct objfile *objfile)
10798{
10799 return htab_create_alloc_ex (3,
10800 hash_dwp_loaded_cutus,
10801 eq_dwp_loaded_cutus,
10802 NULL,
10803 &objfile->objfile_obstack,
10804 hashtab_obstack_allocate,
10805 dummy_obstack_deallocate);
10806}
3019eac3 10807
ab5088bf
DE
10808/* Try to open DWP file FILE_NAME.
10809 The result is the bfd handle of the file.
10810 If there is a problem finding or opening the file, return NULL.
10811 Upon success, the canonicalized path of the file is stored in the bfd,
10812 same as symfile_bfd_open. */
10813
10814static bfd *
10815open_dwp_file (const char *file_name)
10816{
6ac97d4c
DE
10817 bfd *abfd;
10818
10819 abfd = try_open_dwop_file (file_name, 1 /*is_dwp*/, 1 /*search_cwd*/);
10820 if (abfd != NULL)
10821 return abfd;
10822
10823 /* Work around upstream bug 15652.
10824 http://sourceware.org/bugzilla/show_bug.cgi?id=15652
10825 [Whether that's a "bug" is debatable, but it is getting in our way.]
10826 We have no real idea where the dwp file is, because gdb's realpath-ing
10827 of the executable's path may have discarded the needed info.
10828 [IWBN if the dwp file name was recorded in the executable, akin to
10829 .gnu_debuglink, but that doesn't exist yet.]
10830 Strip the directory from FILE_NAME and search again. */
10831 if (*debug_file_directory != '\0')
10832 {
10833 /* Don't implicitly search the current directory here.
10834 If the user wants to search "." to handle this case,
10835 it must be added to debug-file-directory. */
10836 return try_open_dwop_file (lbasename (file_name), 1 /*is_dwp*/,
10837 0 /*search_cwd*/);
10838 }
10839
10840 return NULL;
ab5088bf
DE
10841}
10842
80626a55
DE
10843/* Initialize the use of the DWP file for the current objfile.
10844 By convention the name of the DWP file is ${objfile}.dwp.
10845 The result is NULL if it can't be found. */
a766d390 10846
80626a55 10847static struct dwp_file *
ab5088bf 10848open_and_init_dwp_file (void)
80626a55
DE
10849{
10850 struct objfile *objfile = dwarf2_per_objfile->objfile;
10851 struct dwp_file *dwp_file;
10852 char *dwp_name;
10853 bfd *dbfd;
6c447423 10854 struct cleanup *cleanups = make_cleanup (null_cleanup, 0);
80626a55 10855
82bf32bc
JK
10856 /* Try to find first .dwp for the binary file before any symbolic links
10857 resolving. */
6c447423
DE
10858
10859 /* If the objfile is a debug file, find the name of the real binary
10860 file and get the name of dwp file from there. */
10861 if (objfile->separate_debug_objfile_backlink != NULL)
10862 {
10863 struct objfile *backlink = objfile->separate_debug_objfile_backlink;
10864 const char *backlink_basename = lbasename (backlink->original_name);
10865 char *debug_dirname = ldirname (objfile->original_name);
10866
10867 make_cleanup (xfree, debug_dirname);
10868 dwp_name = xstrprintf ("%s%s%s.dwp", debug_dirname,
10869 SLASH_STRING, backlink_basename);
10870 }
10871 else
10872 dwp_name = xstrprintf ("%s.dwp", objfile->original_name);
10873 make_cleanup (xfree, dwp_name);
80626a55 10874
ab5088bf 10875 dbfd = open_dwp_file (dwp_name);
82bf32bc
JK
10876 if (dbfd == NULL
10877 && strcmp (objfile->original_name, objfile_name (objfile)) != 0)
10878 {
10879 /* Try to find .dwp for the binary file after gdb_realpath resolving. */
10880 dwp_name = xstrprintf ("%s.dwp", objfile_name (objfile));
10881 make_cleanup (xfree, dwp_name);
10882 dbfd = open_dwp_file (dwp_name);
10883 }
10884
80626a55
DE
10885 if (dbfd == NULL)
10886 {
b4f54984 10887 if (dwarf_read_debug)
80626a55
DE
10888 fprintf_unfiltered (gdb_stdlog, "DWP file not found: %s\n", dwp_name);
10889 do_cleanups (cleanups);
10890 return NULL;
3019eac3 10891 }
80626a55 10892 dwp_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_file);
93417882 10893 dwp_file->name = bfd_get_filename (dbfd);
80626a55
DE
10894 dwp_file->dbfd = dbfd;
10895 do_cleanups (cleanups);
c906108c 10896
80626a55
DE
10897 /* +1: section 0 is unused */
10898 dwp_file->num_sections = bfd_count_sections (dbfd) + 1;
10899 dwp_file->elf_sections =
10900 OBSTACK_CALLOC (&objfile->objfile_obstack,
10901 dwp_file->num_sections, asection *);
10902
73869dc2 10903 bfd_map_over_sections (dbfd, dwarf2_locate_common_dwp_sections, dwp_file);
80626a55
DE
10904
10905 dwp_file->cus = create_dwp_hash_table (dwp_file, 0);
10906
10907 dwp_file->tus = create_dwp_hash_table (dwp_file, 1);
10908
73869dc2
DE
10909 /* The DWP file version is stored in the hash table. Oh well. */
10910 if (dwp_file->cus->version != dwp_file->tus->version)
10911 {
10912 /* Technically speaking, we should try to limp along, but this is
fbcbc3fd 10913 pretty bizarre. We use pulongest here because that's the established
4d65956b 10914 portability solution (e.g, we cannot use %u for uint32_t). */
fbcbc3fd
DE
10915 error (_("Dwarf Error: DWP file CU version %s doesn't match"
10916 " TU version %s [in DWP file %s]"),
10917 pulongest (dwp_file->cus->version),
10918 pulongest (dwp_file->tus->version), dwp_name);
73869dc2
DE
10919 }
10920 dwp_file->version = dwp_file->cus->version;
10921
10922 if (dwp_file->version == 2)
10923 bfd_map_over_sections (dbfd, dwarf2_locate_v2_dwp_sections, dwp_file);
10924
19ac8c2e
DE
10925 dwp_file->loaded_cus = allocate_dwp_loaded_cutus_table (objfile);
10926 dwp_file->loaded_tus = allocate_dwp_loaded_cutus_table (objfile);
80626a55 10927
b4f54984 10928 if (dwarf_read_debug)
80626a55
DE
10929 {
10930 fprintf_unfiltered (gdb_stdlog, "DWP file found: %s\n", dwp_file->name);
10931 fprintf_unfiltered (gdb_stdlog,
21aa081e
PA
10932 " %s CUs, %s TUs\n",
10933 pulongest (dwp_file->cus ? dwp_file->cus->nr_units : 0),
10934 pulongest (dwp_file->tus ? dwp_file->tus->nr_units : 0));
80626a55
DE
10935 }
10936
10937 return dwp_file;
3019eac3 10938}
c906108c 10939
ab5088bf
DE
10940/* Wrapper around open_and_init_dwp_file, only open it once. */
10941
10942static struct dwp_file *
10943get_dwp_file (void)
10944{
10945 if (! dwarf2_per_objfile->dwp_checked)
10946 {
10947 dwarf2_per_objfile->dwp_file = open_and_init_dwp_file ();
10948 dwarf2_per_objfile->dwp_checked = 1;
10949 }
10950 return dwarf2_per_objfile->dwp_file;
10951}
10952
80626a55
DE
10953/* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
10954 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
10955 or in the DWP file for the objfile, referenced by THIS_UNIT.
3019eac3 10956 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
80626a55
DE
10957 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
10958
10959 This is called, for example, when wanting to read a variable with a
10960 complex location. Therefore we don't want to do file i/o for every call.
10961 Therefore we don't want to look for a DWO file on every call.
10962 Therefore we first see if we've already seen SIGNATURE in a DWP file,
10963 then we check if we've already seen DWO_NAME, and only THEN do we check
10964 for a DWO file.
10965
1c658ad5 10966 The result is a pointer to the dwo_unit object or NULL if we didn't find it
80626a55 10967 (dwo_id mismatch or couldn't find the DWO/DWP file). */
debd256d 10968
3019eac3 10969static struct dwo_unit *
80626a55
DE
10970lookup_dwo_cutu (struct dwarf2_per_cu_data *this_unit,
10971 const char *dwo_name, const char *comp_dir,
10972 ULONGEST signature, int is_debug_types)
3019eac3
DE
10973{
10974 struct objfile *objfile = dwarf2_per_objfile->objfile;
80626a55
DE
10975 const char *kind = is_debug_types ? "TU" : "CU";
10976 void **dwo_file_slot;
3019eac3 10977 struct dwo_file *dwo_file;
80626a55 10978 struct dwp_file *dwp_file;
cb1df416 10979
6a506a2d
DE
10980 /* First see if there's a DWP file.
10981 If we have a DWP file but didn't find the DWO inside it, don't
10982 look for the original DWO file. It makes gdb behave differently
10983 depending on whether one is debugging in the build tree. */
cf2c3c16 10984
ab5088bf 10985 dwp_file = get_dwp_file ();
80626a55 10986 if (dwp_file != NULL)
cf2c3c16 10987 {
80626a55
DE
10988 const struct dwp_hash_table *dwp_htab =
10989 is_debug_types ? dwp_file->tus : dwp_file->cus;
10990
10991 if (dwp_htab != NULL)
10992 {
10993 struct dwo_unit *dwo_cutu =
57d63ce2
DE
10994 lookup_dwo_unit_in_dwp (dwp_file, comp_dir,
10995 signature, is_debug_types);
80626a55
DE
10996
10997 if (dwo_cutu != NULL)
10998 {
b4f54984 10999 if (dwarf_read_debug)
80626a55
DE
11000 {
11001 fprintf_unfiltered (gdb_stdlog,
11002 "Virtual DWO %s %s found: @%s\n",
11003 kind, hex_string (signature),
11004 host_address_to_string (dwo_cutu));
11005 }
11006 return dwo_cutu;
11007 }
11008 }
11009 }
6a506a2d 11010 else
80626a55 11011 {
6a506a2d 11012 /* No DWP file, look for the DWO file. */
80626a55 11013
6a506a2d
DE
11014 dwo_file_slot = lookup_dwo_file_slot (dwo_name, comp_dir);
11015 if (*dwo_file_slot == NULL)
80626a55 11016 {
6a506a2d
DE
11017 /* Read in the file and build a table of the CUs/TUs it contains. */
11018 *dwo_file_slot = open_and_init_dwo_file (this_unit, dwo_name, comp_dir);
19c3d4c9 11019 }
6a506a2d 11020 /* NOTE: This will be NULL if unable to open the file. */
9a3c8263 11021 dwo_file = (struct dwo_file *) *dwo_file_slot;
3019eac3 11022
6a506a2d 11023 if (dwo_file != NULL)
19c3d4c9 11024 {
6a506a2d
DE
11025 struct dwo_unit *dwo_cutu = NULL;
11026
11027 if (is_debug_types && dwo_file->tus)
11028 {
11029 struct dwo_unit find_dwo_cutu;
11030
11031 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
11032 find_dwo_cutu.signature = signature;
9a3c8263
SM
11033 dwo_cutu
11034 = (struct dwo_unit *) htab_find (dwo_file->tus, &find_dwo_cutu);
6a506a2d
DE
11035 }
11036 else if (!is_debug_types && dwo_file->cu)
80626a55 11037 {
6a506a2d
DE
11038 if (signature == dwo_file->cu->signature)
11039 dwo_cutu = dwo_file->cu;
11040 }
11041
11042 if (dwo_cutu != NULL)
11043 {
b4f54984 11044 if (dwarf_read_debug)
6a506a2d
DE
11045 {
11046 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) found: @%s\n",
11047 kind, dwo_name, hex_string (signature),
11048 host_address_to_string (dwo_cutu));
11049 }
11050 return dwo_cutu;
80626a55
DE
11051 }
11052 }
2e276125 11053 }
9cdd5dbd 11054
80626a55
DE
11055 /* We didn't find it. This could mean a dwo_id mismatch, or
11056 someone deleted the DWO/DWP file, or the search path isn't set up
11057 correctly to find the file. */
11058
b4f54984 11059 if (dwarf_read_debug)
80626a55
DE
11060 {
11061 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) not found\n",
11062 kind, dwo_name, hex_string (signature));
11063 }
3019eac3 11064
6656a72d
DE
11065 /* This is a warning and not a complaint because it can be caused by
11066 pilot error (e.g., user accidentally deleting the DWO). */
43942612
DE
11067 {
11068 /* Print the name of the DWP file if we looked there, helps the user
11069 better diagnose the problem. */
11070 char *dwp_text = NULL;
11071 struct cleanup *cleanups;
11072
11073 if (dwp_file != NULL)
11074 dwp_text = xstrprintf (" [in DWP file %s]", lbasename (dwp_file->name));
11075 cleanups = make_cleanup (xfree, dwp_text);
11076
11077 warning (_("Could not find DWO %s %s(%s)%s referenced by %s at offset 0x%x"
11078 " [in module %s]"),
11079 kind, dwo_name, hex_string (signature),
11080 dwp_text != NULL ? dwp_text : "",
11081 this_unit->is_debug_types ? "TU" : "CU",
11082 this_unit->offset.sect_off, objfile_name (objfile));
11083
11084 do_cleanups (cleanups);
11085 }
3019eac3 11086 return NULL;
5fb290d7
DJ
11087}
11088
80626a55
DE
11089/* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
11090 See lookup_dwo_cutu_unit for details. */
11091
11092static struct dwo_unit *
11093lookup_dwo_comp_unit (struct dwarf2_per_cu_data *this_cu,
11094 const char *dwo_name, const char *comp_dir,
11095 ULONGEST signature)
11096{
11097 return lookup_dwo_cutu (this_cu, dwo_name, comp_dir, signature, 0);
11098}
11099
11100/* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
11101 See lookup_dwo_cutu_unit for details. */
11102
11103static struct dwo_unit *
11104lookup_dwo_type_unit (struct signatured_type *this_tu,
11105 const char *dwo_name, const char *comp_dir)
11106{
11107 return lookup_dwo_cutu (&this_tu->per_cu, dwo_name, comp_dir, this_tu->signature, 1);
11108}
11109
89e63ee4
DE
11110/* Traversal function for queue_and_load_all_dwo_tus. */
11111
11112static int
11113queue_and_load_dwo_tu (void **slot, void *info)
11114{
11115 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
11116 struct dwarf2_per_cu_data *per_cu = (struct dwarf2_per_cu_data *) info;
11117 ULONGEST signature = dwo_unit->signature;
11118 struct signatured_type *sig_type =
11119 lookup_dwo_signatured_type (per_cu->cu, signature);
11120
11121 if (sig_type != NULL)
11122 {
11123 struct dwarf2_per_cu_data *sig_cu = &sig_type->per_cu;
11124
11125 /* We pass NULL for DEPENDENT_CU because we don't yet know if there's
11126 a real dependency of PER_CU on SIG_TYPE. That is detected later
11127 while processing PER_CU. */
11128 if (maybe_queue_comp_unit (NULL, sig_cu, per_cu->cu->language))
11129 load_full_type_unit (sig_cu);
11130 VEC_safe_push (dwarf2_per_cu_ptr, per_cu->imported_symtabs, sig_cu);
11131 }
11132
11133 return 1;
11134}
11135
11136/* Queue all TUs contained in the DWO of PER_CU to be read in.
11137 The DWO may have the only definition of the type, though it may not be
11138 referenced anywhere in PER_CU. Thus we have to load *all* its TUs.
11139 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
11140
11141static void
11142queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *per_cu)
11143{
11144 struct dwo_unit *dwo_unit;
11145 struct dwo_file *dwo_file;
11146
11147 gdb_assert (!per_cu->is_debug_types);
11148 gdb_assert (get_dwp_file () == NULL);
11149 gdb_assert (per_cu->cu != NULL);
11150
11151 dwo_unit = per_cu->cu->dwo_unit;
11152 gdb_assert (dwo_unit != NULL);
11153
11154 dwo_file = dwo_unit->dwo_file;
11155 if (dwo_file->tus != NULL)
11156 htab_traverse_noresize (dwo_file->tus, queue_and_load_dwo_tu, per_cu);
11157}
11158
3019eac3
DE
11159/* Free all resources associated with DWO_FILE.
11160 Close the DWO file and munmap the sections.
11161 All memory should be on the objfile obstack. */
348e048f
DE
11162
11163static void
3019eac3 11164free_dwo_file (struct dwo_file *dwo_file, struct objfile *objfile)
348e048f 11165{
348e048f 11166
5c6fa7ab 11167 /* Note: dbfd is NULL for virtual DWO files. */
80626a55 11168 gdb_bfd_unref (dwo_file->dbfd);
348e048f 11169
3019eac3
DE
11170 VEC_free (dwarf2_section_info_def, dwo_file->sections.types);
11171}
348e048f 11172
3019eac3 11173/* Wrapper for free_dwo_file for use in cleanups. */
348e048f 11174
3019eac3
DE
11175static void
11176free_dwo_file_cleanup (void *arg)
11177{
11178 struct dwo_file *dwo_file = (struct dwo_file *) arg;
11179 struct objfile *objfile = dwarf2_per_objfile->objfile;
348e048f 11180
3019eac3
DE
11181 free_dwo_file (dwo_file, objfile);
11182}
348e048f 11183
3019eac3 11184/* Traversal function for free_dwo_files. */
2ab95328 11185
3019eac3
DE
11186static int
11187free_dwo_file_from_slot (void **slot, void *info)
11188{
11189 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
11190 struct objfile *objfile = (struct objfile *) info;
348e048f 11191
3019eac3 11192 free_dwo_file (dwo_file, objfile);
348e048f 11193
3019eac3
DE
11194 return 1;
11195}
348e048f 11196
3019eac3 11197/* Free all resources associated with DWO_FILES. */
348e048f 11198
3019eac3
DE
11199static void
11200free_dwo_files (htab_t dwo_files, struct objfile *objfile)
11201{
11202 htab_traverse_noresize (dwo_files, free_dwo_file_from_slot, objfile);
348e048f 11203}
3019eac3
DE
11204\f
11205/* Read in various DIEs. */
348e048f 11206
d389af10
JK
11207/* qsort helper for inherit_abstract_dies. */
11208
11209static int
11210unsigned_int_compar (const void *ap, const void *bp)
11211{
11212 unsigned int a = *(unsigned int *) ap;
11213 unsigned int b = *(unsigned int *) bp;
11214
11215 return (a > b) - (b > a);
11216}
11217
11218/* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
3e43a32a
MS
11219 Inherit only the children of the DW_AT_abstract_origin DIE not being
11220 already referenced by DW_AT_abstract_origin from the children of the
11221 current DIE. */
d389af10
JK
11222
11223static void
11224inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
11225{
11226 struct die_info *child_die;
11227 unsigned die_children_count;
11228 /* CU offsets which were referenced by children of the current DIE. */
b64f50a1
JK
11229 sect_offset *offsets;
11230 sect_offset *offsets_end, *offsetp;
d389af10
JK
11231 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
11232 struct die_info *origin_die;
11233 /* Iterator of the ORIGIN_DIE children. */
11234 struct die_info *origin_child_die;
11235 struct cleanup *cleanups;
11236 struct attribute *attr;
cd02d79d
PA
11237 struct dwarf2_cu *origin_cu;
11238 struct pending **origin_previous_list_in_scope;
d389af10
JK
11239
11240 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
11241 if (!attr)
11242 return;
11243
cd02d79d
PA
11244 /* Note that following die references may follow to a die in a
11245 different cu. */
11246
11247 origin_cu = cu;
11248 origin_die = follow_die_ref (die, attr, &origin_cu);
11249
11250 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
11251 symbols in. */
11252 origin_previous_list_in_scope = origin_cu->list_in_scope;
11253 origin_cu->list_in_scope = cu->list_in_scope;
11254
edb3359d
DJ
11255 if (die->tag != origin_die->tag
11256 && !(die->tag == DW_TAG_inlined_subroutine
11257 && origin_die->tag == DW_TAG_subprogram))
d389af10
JK
11258 complaint (&symfile_complaints,
11259 _("DIE 0x%x and its abstract origin 0x%x have different tags"),
b64f50a1 11260 die->offset.sect_off, origin_die->offset.sect_off);
d389af10
JK
11261
11262 child_die = die->child;
11263 die_children_count = 0;
11264 while (child_die && child_die->tag)
11265 {
11266 child_die = sibling_die (child_die);
11267 die_children_count++;
11268 }
8d749320 11269 offsets = XNEWVEC (sect_offset, die_children_count);
d389af10
JK
11270 cleanups = make_cleanup (xfree, offsets);
11271
11272 offsets_end = offsets;
3ea89b92
PMR
11273 for (child_die = die->child;
11274 child_die && child_die->tag;
11275 child_die = sibling_die (child_die))
11276 {
11277 struct die_info *child_origin_die;
11278 struct dwarf2_cu *child_origin_cu;
11279
11280 /* We are trying to process concrete instance entries:
11281 DW_TAG_GNU_call_site DIEs indeed have a DW_AT_abstract_origin tag, but
11282 it's not relevant to our analysis here. i.e. detecting DIEs that are
11283 present in the abstract instance but not referenced in the concrete
11284 one. */
11285 if (child_die->tag == DW_TAG_GNU_call_site)
11286 continue;
11287
c38f313d
DJ
11288 /* For each CHILD_DIE, find the corresponding child of
11289 ORIGIN_DIE. If there is more than one layer of
11290 DW_AT_abstract_origin, follow them all; there shouldn't be,
11291 but GCC versions at least through 4.4 generate this (GCC PR
11292 40573). */
3ea89b92
PMR
11293 child_origin_die = child_die;
11294 child_origin_cu = cu;
c38f313d
DJ
11295 while (1)
11296 {
cd02d79d
PA
11297 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
11298 child_origin_cu);
c38f313d
DJ
11299 if (attr == NULL)
11300 break;
cd02d79d
PA
11301 child_origin_die = follow_die_ref (child_origin_die, attr,
11302 &child_origin_cu);
c38f313d
DJ
11303 }
11304
d389af10
JK
11305 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
11306 counterpart may exist. */
c38f313d 11307 if (child_origin_die != child_die)
d389af10 11308 {
edb3359d
DJ
11309 if (child_die->tag != child_origin_die->tag
11310 && !(child_die->tag == DW_TAG_inlined_subroutine
11311 && child_origin_die->tag == DW_TAG_subprogram))
d389af10
JK
11312 complaint (&symfile_complaints,
11313 _("Child DIE 0x%x and its abstract origin 0x%x have "
b64f50a1
JK
11314 "different tags"), child_die->offset.sect_off,
11315 child_origin_die->offset.sect_off);
c38f313d
DJ
11316 if (child_origin_die->parent != origin_die)
11317 complaint (&symfile_complaints,
11318 _("Child DIE 0x%x and its abstract origin 0x%x have "
b64f50a1
JK
11319 "different parents"), child_die->offset.sect_off,
11320 child_origin_die->offset.sect_off);
c38f313d
DJ
11321 else
11322 *offsets_end++ = child_origin_die->offset;
d389af10 11323 }
d389af10
JK
11324 }
11325 qsort (offsets, offsets_end - offsets, sizeof (*offsets),
11326 unsigned_int_compar);
11327 for (offsetp = offsets + 1; offsetp < offsets_end; offsetp++)
b64f50a1 11328 if (offsetp[-1].sect_off == offsetp->sect_off)
3e43a32a
MS
11329 complaint (&symfile_complaints,
11330 _("Multiple children of DIE 0x%x refer "
11331 "to DIE 0x%x as their abstract origin"),
b64f50a1 11332 die->offset.sect_off, offsetp->sect_off);
d389af10
JK
11333
11334 offsetp = offsets;
11335 origin_child_die = origin_die->child;
11336 while (origin_child_die && origin_child_die->tag)
11337 {
11338 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
b64f50a1
JK
11339 while (offsetp < offsets_end
11340 && offsetp->sect_off < origin_child_die->offset.sect_off)
d389af10 11341 offsetp++;
b64f50a1
JK
11342 if (offsetp >= offsets_end
11343 || offsetp->sect_off > origin_child_die->offset.sect_off)
d389af10 11344 {
adde2bff
DE
11345 /* Found that ORIGIN_CHILD_DIE is really not referenced.
11346 Check whether we're already processing ORIGIN_CHILD_DIE.
11347 This can happen with mutually referenced abstract_origins.
11348 PR 16581. */
11349 if (!origin_child_die->in_process)
11350 process_die (origin_child_die, origin_cu);
d389af10
JK
11351 }
11352 origin_child_die = sibling_die (origin_child_die);
11353 }
cd02d79d 11354 origin_cu->list_in_scope = origin_previous_list_in_scope;
d389af10
JK
11355
11356 do_cleanups (cleanups);
11357}
11358
c906108c 11359static void
e7c27a73 11360read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 11361{
e7c27a73 11362 struct objfile *objfile = cu->objfile;
3e29f34a 11363 struct gdbarch *gdbarch = get_objfile_arch (objfile);
fe978cb0 11364 struct context_stack *newobj;
c906108c
SS
11365 CORE_ADDR lowpc;
11366 CORE_ADDR highpc;
11367 struct die_info *child_die;
edb3359d 11368 struct attribute *attr, *call_line, *call_file;
15d034d0 11369 const char *name;
e142c38c 11370 CORE_ADDR baseaddr;
801e3a5b 11371 struct block *block;
edb3359d 11372 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
34eaf542
TT
11373 VEC (symbolp) *template_args = NULL;
11374 struct template_symbol *templ_func = NULL;
edb3359d
DJ
11375
11376 if (inlined_func)
11377 {
11378 /* If we do not have call site information, we can't show the
11379 caller of this inlined function. That's too confusing, so
11380 only use the scope for local variables. */
11381 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
11382 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
11383 if (call_line == NULL || call_file == NULL)
11384 {
11385 read_lexical_block_scope (die, cu);
11386 return;
11387 }
11388 }
c906108c 11389
e142c38c
DJ
11390 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
11391
94af9270 11392 name = dwarf2_name (die, cu);
c906108c 11393
e8d05480
JB
11394 /* Ignore functions with missing or empty names. These are actually
11395 illegal according to the DWARF standard. */
11396 if (name == NULL)
11397 {
11398 complaint (&symfile_complaints,
b64f50a1
JK
11399 _("missing name for subprogram DIE at %d"),
11400 die->offset.sect_off);
e8d05480
JB
11401 return;
11402 }
11403
11404 /* Ignore functions with missing or invalid low and high pc attributes. */
3a2b436a 11405 if (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL)
e385593e 11406 <= PC_BOUNDS_INVALID)
e8d05480 11407 {
ae4d0c03
PM
11408 attr = dwarf2_attr (die, DW_AT_external, cu);
11409 if (!attr || !DW_UNSND (attr))
11410 complaint (&symfile_complaints,
3e43a32a
MS
11411 _("cannot get low and high bounds "
11412 "for subprogram DIE at %d"),
b64f50a1 11413 die->offset.sect_off);
e8d05480
JB
11414 return;
11415 }
c906108c 11416
3e29f34a
MR
11417 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
11418 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
c906108c 11419
34eaf542
TT
11420 /* If we have any template arguments, then we must allocate a
11421 different sort of symbol. */
11422 for (child_die = die->child; child_die; child_die = sibling_die (child_die))
11423 {
11424 if (child_die->tag == DW_TAG_template_type_param
11425 || child_die->tag == DW_TAG_template_value_param)
11426 {
e623cf5d 11427 templ_func = allocate_template_symbol (objfile);
34eaf542
TT
11428 templ_func->base.is_cplus_template_function = 1;
11429 break;
11430 }
11431 }
11432
fe978cb0
PA
11433 newobj = push_context (0, lowpc);
11434 newobj->name = new_symbol_full (die, read_type_die (die, cu), cu,
34eaf542 11435 (struct symbol *) templ_func);
4c2df51b 11436
4cecd739
DJ
11437 /* If there is a location expression for DW_AT_frame_base, record
11438 it. */
e142c38c 11439 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
4c2df51b 11440 if (attr)
fe978cb0 11441 dwarf2_symbol_mark_computed (attr, newobj->name, cu, 1);
4c2df51b 11442
63e43d3a
PMR
11443 /* If there is a location for the static link, record it. */
11444 newobj->static_link = NULL;
11445 attr = dwarf2_attr (die, DW_AT_static_link, cu);
11446 if (attr)
11447 {
224c3ddb
SM
11448 newobj->static_link
11449 = XOBNEW (&objfile->objfile_obstack, struct dynamic_prop);
63e43d3a
PMR
11450 attr_to_dynamic_prop (attr, die, cu, newobj->static_link);
11451 }
11452
e142c38c 11453 cu->list_in_scope = &local_symbols;
c906108c 11454
639d11d3 11455 if (die->child != NULL)
c906108c 11456 {
639d11d3 11457 child_die = die->child;
c906108c
SS
11458 while (child_die && child_die->tag)
11459 {
34eaf542
TT
11460 if (child_die->tag == DW_TAG_template_type_param
11461 || child_die->tag == DW_TAG_template_value_param)
11462 {
11463 struct symbol *arg = new_symbol (child_die, NULL, cu);
11464
f1078f66
DJ
11465 if (arg != NULL)
11466 VEC_safe_push (symbolp, template_args, arg);
34eaf542
TT
11467 }
11468 else
11469 process_die (child_die, cu);
c906108c
SS
11470 child_die = sibling_die (child_die);
11471 }
11472 }
11473
d389af10
JK
11474 inherit_abstract_dies (die, cu);
11475
4a811a97
UW
11476 /* If we have a DW_AT_specification, we might need to import using
11477 directives from the context of the specification DIE. See the
11478 comment in determine_prefix. */
11479 if (cu->language == language_cplus
11480 && dwarf2_attr (die, DW_AT_specification, cu))
11481 {
11482 struct dwarf2_cu *spec_cu = cu;
11483 struct die_info *spec_die = die_specification (die, &spec_cu);
11484
11485 while (spec_die)
11486 {
11487 child_die = spec_die->child;
11488 while (child_die && child_die->tag)
11489 {
11490 if (child_die->tag == DW_TAG_imported_module)
11491 process_die (child_die, spec_cu);
11492 child_die = sibling_die (child_die);
11493 }
11494
11495 /* In some cases, GCC generates specification DIEs that
11496 themselves contain DW_AT_specification attributes. */
11497 spec_die = die_specification (spec_die, &spec_cu);
11498 }
11499 }
11500
fe978cb0 11501 newobj = pop_context ();
c906108c 11502 /* Make a block for the local symbols within. */
fe978cb0 11503 block = finish_block (newobj->name, &local_symbols, newobj->old_blocks,
63e43d3a 11504 newobj->static_link, lowpc, highpc);
801e3a5b 11505
df8a16a1 11506 /* For C++, set the block's scope. */
45280282
IB
11507 if ((cu->language == language_cplus
11508 || cu->language == language_fortran
c44af4eb
TT
11509 || cu->language == language_d
11510 || cu->language == language_rust)
4d4ec4e5 11511 && cu->processing_has_namespace_info)
195a3f6c
TT
11512 block_set_scope (block, determine_prefix (die, cu),
11513 &objfile->objfile_obstack);
df8a16a1 11514
801e3a5b
JB
11515 /* If we have address ranges, record them. */
11516 dwarf2_record_block_ranges (die, block, baseaddr, cu);
6e70227d 11517
fe978cb0 11518 gdbarch_make_symbol_special (gdbarch, newobj->name, objfile);
3e29f34a 11519
34eaf542
TT
11520 /* Attach template arguments to function. */
11521 if (! VEC_empty (symbolp, template_args))
11522 {
11523 gdb_assert (templ_func != NULL);
11524
11525 templ_func->n_template_arguments = VEC_length (symbolp, template_args);
11526 templ_func->template_arguments
8d749320
SM
11527 = XOBNEWVEC (&objfile->objfile_obstack, struct symbol *,
11528 templ_func->n_template_arguments);
34eaf542
TT
11529 memcpy (templ_func->template_arguments,
11530 VEC_address (symbolp, template_args),
11531 (templ_func->n_template_arguments * sizeof (struct symbol *)));
11532 VEC_free (symbolp, template_args);
11533 }
11534
208d8187
JB
11535 /* In C++, we can have functions nested inside functions (e.g., when
11536 a function declares a class that has methods). This means that
11537 when we finish processing a function scope, we may need to go
11538 back to building a containing block's symbol lists. */
fe978cb0 11539 local_symbols = newobj->locals;
22cee43f 11540 local_using_directives = newobj->local_using_directives;
208d8187 11541
921e78cf
JB
11542 /* If we've finished processing a top-level function, subsequent
11543 symbols go in the file symbol list. */
11544 if (outermost_context_p ())
e142c38c 11545 cu->list_in_scope = &file_symbols;
c906108c
SS
11546}
11547
11548/* Process all the DIES contained within a lexical block scope. Start
11549 a new scope, process the dies, and then close the scope. */
11550
11551static void
e7c27a73 11552read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 11553{
e7c27a73 11554 struct objfile *objfile = cu->objfile;
3e29f34a 11555 struct gdbarch *gdbarch = get_objfile_arch (objfile);
fe978cb0 11556 struct context_stack *newobj;
c906108c
SS
11557 CORE_ADDR lowpc, highpc;
11558 struct die_info *child_die;
e142c38c
DJ
11559 CORE_ADDR baseaddr;
11560
11561 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c
SS
11562
11563 /* Ignore blocks with missing or invalid low and high pc attributes. */
af34e669
DJ
11564 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
11565 as multiple lexical blocks? Handling children in a sane way would
6e70227d 11566 be nasty. Might be easier to properly extend generic blocks to
af34e669 11567 describe ranges. */
e385593e
JK
11568 switch (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
11569 {
11570 case PC_BOUNDS_NOT_PRESENT:
11571 /* DW_TAG_lexical_block has no attributes, process its children as if
11572 there was no wrapping by that DW_TAG_lexical_block.
11573 GCC does no longer produces such DWARF since GCC r224161. */
11574 for (child_die = die->child;
11575 child_die != NULL && child_die->tag;
11576 child_die = sibling_die (child_die))
11577 process_die (child_die, cu);
11578 return;
11579 case PC_BOUNDS_INVALID:
11580 return;
11581 }
3e29f34a
MR
11582 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
11583 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
c906108c
SS
11584
11585 push_context (0, lowpc);
639d11d3 11586 if (die->child != NULL)
c906108c 11587 {
639d11d3 11588 child_die = die->child;
c906108c
SS
11589 while (child_die && child_die->tag)
11590 {
e7c27a73 11591 process_die (child_die, cu);
c906108c
SS
11592 child_die = sibling_die (child_die);
11593 }
11594 }
3ea89b92 11595 inherit_abstract_dies (die, cu);
fe978cb0 11596 newobj = pop_context ();
c906108c 11597
22cee43f 11598 if (local_symbols != NULL || local_using_directives != NULL)
c906108c 11599 {
801e3a5b 11600 struct block *block
63e43d3a 11601 = finish_block (0, &local_symbols, newobj->old_blocks, NULL,
fe978cb0 11602 newobj->start_addr, highpc);
801e3a5b
JB
11603
11604 /* Note that recording ranges after traversing children, as we
11605 do here, means that recording a parent's ranges entails
11606 walking across all its children's ranges as they appear in
11607 the address map, which is quadratic behavior.
11608
11609 It would be nicer to record the parent's ranges before
11610 traversing its children, simply overriding whatever you find
11611 there. But since we don't even decide whether to create a
11612 block until after we've traversed its children, that's hard
11613 to do. */
11614 dwarf2_record_block_ranges (die, block, baseaddr, cu);
c906108c 11615 }
fe978cb0 11616 local_symbols = newobj->locals;
22cee43f 11617 local_using_directives = newobj->local_using_directives;
c906108c
SS
11618}
11619
96408a79
SA
11620/* Read in DW_TAG_GNU_call_site and insert it to CU->call_site_htab. */
11621
11622static void
11623read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
11624{
11625 struct objfile *objfile = cu->objfile;
11626 struct gdbarch *gdbarch = get_objfile_arch (objfile);
11627 CORE_ADDR pc, baseaddr;
11628 struct attribute *attr;
11629 struct call_site *call_site, call_site_local;
11630 void **slot;
11631 int nparams;
11632 struct die_info *child_die;
11633
11634 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
11635
11636 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
11637 if (!attr)
11638 {
11639 complaint (&symfile_complaints,
11640 _("missing DW_AT_low_pc for DW_TAG_GNU_call_site "
11641 "DIE 0x%x [in module %s]"),
4262abfb 11642 die->offset.sect_off, objfile_name (objfile));
96408a79
SA
11643 return;
11644 }
31aa7e4e 11645 pc = attr_value_as_address (attr) + baseaddr;
3e29f34a 11646 pc = gdbarch_adjust_dwarf2_addr (gdbarch, pc);
96408a79
SA
11647
11648 if (cu->call_site_htab == NULL)
11649 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
11650 NULL, &objfile->objfile_obstack,
11651 hashtab_obstack_allocate, NULL);
11652 call_site_local.pc = pc;
11653 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
11654 if (*slot != NULL)
11655 {
11656 complaint (&symfile_complaints,
11657 _("Duplicate PC %s for DW_TAG_GNU_call_site "
11658 "DIE 0x%x [in module %s]"),
4262abfb
JK
11659 paddress (gdbarch, pc), die->offset.sect_off,
11660 objfile_name (objfile));
96408a79
SA
11661 return;
11662 }
11663
11664 /* Count parameters at the caller. */
11665
11666 nparams = 0;
11667 for (child_die = die->child; child_die && child_die->tag;
11668 child_die = sibling_die (child_die))
11669 {
11670 if (child_die->tag != DW_TAG_GNU_call_site_parameter)
11671 {
11672 complaint (&symfile_complaints,
11673 _("Tag %d is not DW_TAG_GNU_call_site_parameter in "
11674 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
4262abfb
JK
11675 child_die->tag, child_die->offset.sect_off,
11676 objfile_name (objfile));
96408a79
SA
11677 continue;
11678 }
11679
11680 nparams++;
11681 }
11682
224c3ddb
SM
11683 call_site
11684 = ((struct call_site *)
11685 obstack_alloc (&objfile->objfile_obstack,
11686 sizeof (*call_site)
11687 + (sizeof (*call_site->parameter) * (nparams - 1))));
96408a79
SA
11688 *slot = call_site;
11689 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
11690 call_site->pc = pc;
11691
11692 if (dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
11693 {
11694 struct die_info *func_die;
11695
11696 /* Skip also over DW_TAG_inlined_subroutine. */
11697 for (func_die = die->parent;
11698 func_die && func_die->tag != DW_TAG_subprogram
11699 && func_die->tag != DW_TAG_subroutine_type;
11700 func_die = func_die->parent);
11701
11702 /* DW_AT_GNU_all_call_sites is a superset
11703 of DW_AT_GNU_all_tail_call_sites. */
11704 if (func_die
11705 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
11706 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
11707 {
11708 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
11709 not complete. But keep CALL_SITE for look ups via call_site_htab,
11710 both the initial caller containing the real return address PC and
11711 the final callee containing the current PC of a chain of tail
11712 calls do not need to have the tail call list complete. But any
11713 function candidate for a virtual tail call frame searched via
11714 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
11715 determined unambiguously. */
11716 }
11717 else
11718 {
11719 struct type *func_type = NULL;
11720
11721 if (func_die)
11722 func_type = get_die_type (func_die, cu);
11723 if (func_type != NULL)
11724 {
11725 gdb_assert (TYPE_CODE (func_type) == TYPE_CODE_FUNC);
11726
11727 /* Enlist this call site to the function. */
11728 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
11729 TYPE_TAIL_CALL_LIST (func_type) = call_site;
11730 }
11731 else
11732 complaint (&symfile_complaints,
11733 _("Cannot find function owning DW_TAG_GNU_call_site "
11734 "DIE 0x%x [in module %s]"),
4262abfb 11735 die->offset.sect_off, objfile_name (objfile));
96408a79
SA
11736 }
11737 }
11738
11739 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
11740 if (attr == NULL)
11741 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
11742 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
11743 if (!attr || (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0))
11744 /* Keep NULL DWARF_BLOCK. */;
11745 else if (attr_form_is_block (attr))
11746 {
11747 struct dwarf2_locexpr_baton *dlbaton;
11748
8d749320 11749 dlbaton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
96408a79
SA
11750 dlbaton->data = DW_BLOCK (attr)->data;
11751 dlbaton->size = DW_BLOCK (attr)->size;
11752 dlbaton->per_cu = cu->per_cu;
11753
11754 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
11755 }
7771576e 11756 else if (attr_form_is_ref (attr))
96408a79 11757 {
96408a79
SA
11758 struct dwarf2_cu *target_cu = cu;
11759 struct die_info *target_die;
11760
ac9ec31b 11761 target_die = follow_die_ref (die, attr, &target_cu);
96408a79
SA
11762 gdb_assert (target_cu->objfile == objfile);
11763 if (die_is_declaration (target_die, target_cu))
11764 {
7d45c7c3 11765 const char *target_physname;
9112db09
JK
11766
11767 /* Prefer the mangled name; otherwise compute the demangled one. */
7d45c7c3
KB
11768 target_physname = dwarf2_string_attr (target_die,
11769 DW_AT_linkage_name,
11770 target_cu);
11771 if (target_physname == NULL)
11772 target_physname = dwarf2_string_attr (target_die,
11773 DW_AT_MIPS_linkage_name,
11774 target_cu);
11775 if (target_physname == NULL)
9112db09 11776 target_physname = dwarf2_physname (NULL, target_die, target_cu);
96408a79
SA
11777 if (target_physname == NULL)
11778 complaint (&symfile_complaints,
11779 _("DW_AT_GNU_call_site_target target DIE has invalid "
11780 "physname, for referencing DIE 0x%x [in module %s]"),
4262abfb 11781 die->offset.sect_off, objfile_name (objfile));
96408a79 11782 else
7d455152 11783 SET_FIELD_PHYSNAME (call_site->target, target_physname);
96408a79
SA
11784 }
11785 else
11786 {
11787 CORE_ADDR lowpc;
11788
11789 /* DW_AT_entry_pc should be preferred. */
3a2b436a 11790 if (dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL)
e385593e 11791 <= PC_BOUNDS_INVALID)
96408a79
SA
11792 complaint (&symfile_complaints,
11793 _("DW_AT_GNU_call_site_target target DIE has invalid "
11794 "low pc, for referencing DIE 0x%x [in module %s]"),
4262abfb 11795 die->offset.sect_off, objfile_name (objfile));
96408a79 11796 else
3e29f34a
MR
11797 {
11798 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
11799 SET_FIELD_PHYSADDR (call_site->target, lowpc);
11800 }
96408a79
SA
11801 }
11802 }
11803 else
11804 complaint (&symfile_complaints,
11805 _("DW_TAG_GNU_call_site DW_AT_GNU_call_site_target is neither "
11806 "block nor reference, for DIE 0x%x [in module %s]"),
4262abfb 11807 die->offset.sect_off, objfile_name (objfile));
96408a79
SA
11808
11809 call_site->per_cu = cu->per_cu;
11810
11811 for (child_die = die->child;
11812 child_die && child_die->tag;
11813 child_die = sibling_die (child_die))
11814 {
96408a79 11815 struct call_site_parameter *parameter;
1788b2d3 11816 struct attribute *loc, *origin;
96408a79
SA
11817
11818 if (child_die->tag != DW_TAG_GNU_call_site_parameter)
11819 {
11820 /* Already printed the complaint above. */
11821 continue;
11822 }
11823
11824 gdb_assert (call_site->parameter_count < nparams);
11825 parameter = &call_site->parameter[call_site->parameter_count];
11826
1788b2d3
JK
11827 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
11828 specifies DW_TAG_formal_parameter. Value of the data assumed for the
11829 register is contained in DW_AT_GNU_call_site_value. */
96408a79 11830
24c5c679 11831 loc = dwarf2_attr (child_die, DW_AT_location, cu);
1788b2d3 11832 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
7771576e 11833 if (loc == NULL && origin != NULL && attr_form_is_ref (origin))
1788b2d3
JK
11834 {
11835 sect_offset offset;
11836
11837 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
11838 offset = dwarf2_get_ref_die_offset (origin);
d76b7dbc
JK
11839 if (!offset_in_cu_p (&cu->header, offset))
11840 {
11841 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
11842 binding can be done only inside one CU. Such referenced DIE
11843 therefore cannot be even moved to DW_TAG_partial_unit. */
11844 complaint (&symfile_complaints,
11845 _("DW_AT_abstract_origin offset is not in CU for "
11846 "DW_TAG_GNU_call_site child DIE 0x%x "
11847 "[in module %s]"),
4262abfb 11848 child_die->offset.sect_off, objfile_name (objfile));
d76b7dbc
JK
11849 continue;
11850 }
1788b2d3
JK
11851 parameter->u.param_offset.cu_off = (offset.sect_off
11852 - cu->header.offset.sect_off);
11853 }
11854 else if (loc == NULL || origin != NULL || !attr_form_is_block (loc))
96408a79
SA
11855 {
11856 complaint (&symfile_complaints,
11857 _("No DW_FORM_block* DW_AT_location for "
11858 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
4262abfb 11859 child_die->offset.sect_off, objfile_name (objfile));
96408a79
SA
11860 continue;
11861 }
24c5c679 11862 else
96408a79 11863 {
24c5c679
JK
11864 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
11865 (DW_BLOCK (loc)->data, &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size]);
11866 if (parameter->u.dwarf_reg != -1)
11867 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
11868 else if (dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (loc)->data,
11869 &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size],
11870 &parameter->u.fb_offset))
11871 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
11872 else
11873 {
11874 complaint (&symfile_complaints,
11875 _("Only single DW_OP_reg or DW_OP_fbreg is supported "
11876 "for DW_FORM_block* DW_AT_location is supported for "
11877 "DW_TAG_GNU_call_site child DIE 0x%x "
11878 "[in module %s]"),
4262abfb 11879 child_die->offset.sect_off, objfile_name (objfile));
24c5c679
JK
11880 continue;
11881 }
96408a79
SA
11882 }
11883
11884 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
11885 if (!attr_form_is_block (attr))
11886 {
11887 complaint (&symfile_complaints,
11888 _("No DW_FORM_block* DW_AT_GNU_call_site_value for "
11889 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
4262abfb 11890 child_die->offset.sect_off, objfile_name (objfile));
96408a79
SA
11891 continue;
11892 }
11893 parameter->value = DW_BLOCK (attr)->data;
11894 parameter->value_size = DW_BLOCK (attr)->size;
11895
11896 /* Parameters are not pre-cleared by memset above. */
11897 parameter->data_value = NULL;
11898 parameter->data_value_size = 0;
11899 call_site->parameter_count++;
11900
11901 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
11902 if (attr)
11903 {
11904 if (!attr_form_is_block (attr))
11905 complaint (&symfile_complaints,
11906 _("No DW_FORM_block* DW_AT_GNU_call_site_data_value for "
11907 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
4262abfb 11908 child_die->offset.sect_off, objfile_name (objfile));
96408a79
SA
11909 else
11910 {
11911 parameter->data_value = DW_BLOCK (attr)->data;
11912 parameter->data_value_size = DW_BLOCK (attr)->size;
11913 }
11914 }
11915 }
11916}
11917
43039443 11918/* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
ff013f42
JK
11919 Return 1 if the attributes are present and valid, otherwise, return 0.
11920 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
43039443
JK
11921
11922static int
11923dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
ff013f42
JK
11924 CORE_ADDR *high_return, struct dwarf2_cu *cu,
11925 struct partial_symtab *ranges_pst)
43039443
JK
11926{
11927 struct objfile *objfile = cu->objfile;
3e29f34a 11928 struct gdbarch *gdbarch = get_objfile_arch (objfile);
43039443
JK
11929 struct comp_unit_head *cu_header = &cu->header;
11930 bfd *obfd = objfile->obfd;
11931 unsigned int addr_size = cu_header->addr_size;
11932 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
11933 /* Base address selection entry. */
11934 CORE_ADDR base;
11935 int found_base;
11936 unsigned int dummy;
d521ce57 11937 const gdb_byte *buffer;
43039443
JK
11938 int low_set;
11939 CORE_ADDR low = 0;
11940 CORE_ADDR high = 0;
ff013f42 11941 CORE_ADDR baseaddr;
43039443 11942
d00adf39
DE
11943 found_base = cu->base_known;
11944 base = cu->base_address;
43039443 11945
be391dca 11946 dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
dce234bc 11947 if (offset >= dwarf2_per_objfile->ranges.size)
43039443
JK
11948 {
11949 complaint (&symfile_complaints,
11950 _("Offset %d out of bounds for DW_AT_ranges attribute"),
11951 offset);
11952 return 0;
11953 }
dce234bc 11954 buffer = dwarf2_per_objfile->ranges.buffer + offset;
43039443 11955
43039443
JK
11956 low_set = 0;
11957
e7030f15 11958 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
ff013f42 11959
43039443
JK
11960 while (1)
11961 {
11962 CORE_ADDR range_beginning, range_end;
11963
11964 range_beginning = read_address (obfd, buffer, cu, &dummy);
11965 buffer += addr_size;
11966 range_end = read_address (obfd, buffer, cu, &dummy);
11967 buffer += addr_size;
11968 offset += 2 * addr_size;
11969
11970 /* An end of list marker is a pair of zero addresses. */
11971 if (range_beginning == 0 && range_end == 0)
11972 /* Found the end of list entry. */
11973 break;
11974
11975 /* Each base address selection entry is a pair of 2 values.
11976 The first is the largest possible address, the second is
11977 the base address. Check for a base address here. */
11978 if ((range_beginning & mask) == mask)
11979 {
28d2bfb9
AB
11980 /* If we found the largest possible address, then we already
11981 have the base address in range_end. */
11982 base = range_end;
43039443
JK
11983 found_base = 1;
11984 continue;
11985 }
11986
11987 if (!found_base)
11988 {
11989 /* We have no valid base address for the ranges
11990 data. */
11991 complaint (&symfile_complaints,
11992 _("Invalid .debug_ranges data (no base address)"));
11993 return 0;
11994 }
11995
9277c30c
UW
11996 if (range_beginning > range_end)
11997 {
11998 /* Inverted range entries are invalid. */
11999 complaint (&symfile_complaints,
12000 _("Invalid .debug_ranges data (inverted range)"));
12001 return 0;
12002 }
12003
12004 /* Empty range entries have no effect. */
12005 if (range_beginning == range_end)
12006 continue;
12007
43039443
JK
12008 range_beginning += base;
12009 range_end += base;
12010
01093045
DE
12011 /* A not-uncommon case of bad debug info.
12012 Don't pollute the addrmap with bad data. */
12013 if (range_beginning + baseaddr == 0
12014 && !dwarf2_per_objfile->has_section_at_zero)
12015 {
12016 complaint (&symfile_complaints,
12017 _(".debug_ranges entry has start address of zero"
4262abfb 12018 " [in module %s]"), objfile_name (objfile));
01093045
DE
12019 continue;
12020 }
12021
9277c30c 12022 if (ranges_pst != NULL)
3e29f34a
MR
12023 {
12024 CORE_ADDR lowpc;
12025 CORE_ADDR highpc;
12026
12027 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch,
12028 range_beginning + baseaddr);
12029 highpc = gdbarch_adjust_dwarf2_addr (gdbarch,
12030 range_end + baseaddr);
12031 addrmap_set_empty (objfile->psymtabs_addrmap, lowpc, highpc - 1,
12032 ranges_pst);
12033 }
ff013f42 12034
43039443
JK
12035 /* FIXME: This is recording everything as a low-high
12036 segment of consecutive addresses. We should have a
12037 data structure for discontiguous block ranges
12038 instead. */
12039 if (! low_set)
12040 {
12041 low = range_beginning;
12042 high = range_end;
12043 low_set = 1;
12044 }
12045 else
12046 {
12047 if (range_beginning < low)
12048 low = range_beginning;
12049 if (range_end > high)
12050 high = range_end;
12051 }
12052 }
12053
12054 if (! low_set)
12055 /* If the first entry is an end-of-list marker, the range
12056 describes an empty scope, i.e. no instructions. */
12057 return 0;
12058
12059 if (low_return)
12060 *low_return = low;
12061 if (high_return)
12062 *high_return = high;
12063 return 1;
12064}
12065
3a2b436a
JK
12066/* Get low and high pc attributes from a die. See enum pc_bounds_kind
12067 definition for the return value. *LOWPC and *HIGHPC are set iff
e385593e 12068 neither PC_BOUNDS_NOT_PRESENT nor PC_BOUNDS_INVALID are returned. */
380bca97 12069
3a2b436a 12070static enum pc_bounds_kind
af34e669 12071dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
d85a05f0
DJ
12072 CORE_ADDR *highpc, struct dwarf2_cu *cu,
12073 struct partial_symtab *pst)
c906108c
SS
12074{
12075 struct attribute *attr;
91da1414 12076 struct attribute *attr_high;
af34e669
DJ
12077 CORE_ADDR low = 0;
12078 CORE_ADDR high = 0;
e385593e 12079 enum pc_bounds_kind ret;
c906108c 12080
91da1414
MW
12081 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
12082 if (attr_high)
af34e669 12083 {
e142c38c 12084 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
af34e669 12085 if (attr)
91da1414 12086 {
31aa7e4e
JB
12087 low = attr_value_as_address (attr);
12088 high = attr_value_as_address (attr_high);
12089 if (cu->header.version >= 4 && attr_form_is_constant (attr_high))
12090 high += low;
91da1414 12091 }
af34e669
DJ
12092 else
12093 /* Found high w/o low attribute. */
e385593e 12094 return PC_BOUNDS_INVALID;
af34e669
DJ
12095
12096 /* Found consecutive range of addresses. */
3a2b436a 12097 ret = PC_BOUNDS_HIGH_LOW;
af34e669 12098 }
c906108c 12099 else
af34e669 12100 {
e142c38c 12101 attr = dwarf2_attr (die, DW_AT_ranges, cu);
af34e669
DJ
12102 if (attr != NULL)
12103 {
ab435259
DE
12104 /* DW_AT_ranges_base does not apply to DIEs from the DWO skeleton.
12105 We take advantage of the fact that DW_AT_ranges does not appear
12106 in DW_TAG_compile_unit of DWO files. */
12107 int need_ranges_base = die->tag != DW_TAG_compile_unit;
12108 unsigned int ranges_offset = (DW_UNSND (attr)
12109 + (need_ranges_base
12110 ? cu->ranges_base
12111 : 0));
2e3cf129 12112
af34e669 12113 /* Value of the DW_AT_ranges attribute is the offset in the
a604369a 12114 .debug_ranges section. */
2e3cf129 12115 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst))
e385593e 12116 return PC_BOUNDS_INVALID;
43039443 12117 /* Found discontinuous range of addresses. */
3a2b436a 12118 ret = PC_BOUNDS_RANGES;
af34e669 12119 }
e385593e
JK
12120 else
12121 return PC_BOUNDS_NOT_PRESENT;
af34e669 12122 }
c906108c 12123
9373cf26
JK
12124 /* read_partial_die has also the strict LOW < HIGH requirement. */
12125 if (high <= low)
e385593e 12126 return PC_BOUNDS_INVALID;
c906108c
SS
12127
12128 /* When using the GNU linker, .gnu.linkonce. sections are used to
12129 eliminate duplicate copies of functions and vtables and such.
12130 The linker will arbitrarily choose one and discard the others.
12131 The AT_*_pc values for such functions refer to local labels in
12132 these sections. If the section from that file was discarded, the
12133 labels are not in the output, so the relocs get a value of 0.
12134 If this is a discarded function, mark the pc bounds as invalid,
12135 so that GDB will ignore it. */
72dca2f5 12136 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
e385593e 12137 return PC_BOUNDS_INVALID;
c906108c
SS
12138
12139 *lowpc = low;
96408a79
SA
12140 if (highpc)
12141 *highpc = high;
af34e669 12142 return ret;
c906108c
SS
12143}
12144
b084d499
JB
12145/* Assuming that DIE represents a subprogram DIE or a lexical block, get
12146 its low and high PC addresses. Do nothing if these addresses could not
12147 be determined. Otherwise, set LOWPC to the low address if it is smaller,
12148 and HIGHPC to the high address if greater than HIGHPC. */
12149
12150static void
12151dwarf2_get_subprogram_pc_bounds (struct die_info *die,
12152 CORE_ADDR *lowpc, CORE_ADDR *highpc,
12153 struct dwarf2_cu *cu)
12154{
12155 CORE_ADDR low, high;
12156 struct die_info *child = die->child;
12157
e385593e 12158 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL) >= PC_BOUNDS_RANGES)
b084d499 12159 {
325fac50
PA
12160 *lowpc = std::min (*lowpc, low);
12161 *highpc = std::max (*highpc, high);
b084d499
JB
12162 }
12163
12164 /* If the language does not allow nested subprograms (either inside
12165 subprograms or lexical blocks), we're done. */
12166 if (cu->language != language_ada)
12167 return;
6e70227d 12168
b084d499
JB
12169 /* Check all the children of the given DIE. If it contains nested
12170 subprograms, then check their pc bounds. Likewise, we need to
12171 check lexical blocks as well, as they may also contain subprogram
12172 definitions. */
12173 while (child && child->tag)
12174 {
12175 if (child->tag == DW_TAG_subprogram
12176 || child->tag == DW_TAG_lexical_block)
12177 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
12178 child = sibling_die (child);
12179 }
12180}
12181
fae299cd
DC
12182/* Get the low and high pc's represented by the scope DIE, and store
12183 them in *LOWPC and *HIGHPC. If the correct values can't be
12184 determined, set *LOWPC to -1 and *HIGHPC to 0. */
12185
12186static void
12187get_scope_pc_bounds (struct die_info *die,
12188 CORE_ADDR *lowpc, CORE_ADDR *highpc,
12189 struct dwarf2_cu *cu)
12190{
12191 CORE_ADDR best_low = (CORE_ADDR) -1;
12192 CORE_ADDR best_high = (CORE_ADDR) 0;
12193 CORE_ADDR current_low, current_high;
12194
3a2b436a 12195 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL)
e385593e 12196 >= PC_BOUNDS_RANGES)
fae299cd
DC
12197 {
12198 best_low = current_low;
12199 best_high = current_high;
12200 }
12201 else
12202 {
12203 struct die_info *child = die->child;
12204
12205 while (child && child->tag)
12206 {
12207 switch (child->tag) {
12208 case DW_TAG_subprogram:
b084d499 12209 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
fae299cd
DC
12210 break;
12211 case DW_TAG_namespace:
f55ee35c 12212 case DW_TAG_module:
fae299cd
DC
12213 /* FIXME: carlton/2004-01-16: Should we do this for
12214 DW_TAG_class_type/DW_TAG_structure_type, too? I think
12215 that current GCC's always emit the DIEs corresponding
12216 to definitions of methods of classes as children of a
12217 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
12218 the DIEs giving the declarations, which could be
12219 anywhere). But I don't see any reason why the
12220 standards says that they have to be there. */
12221 get_scope_pc_bounds (child, &current_low, &current_high, cu);
12222
12223 if (current_low != ((CORE_ADDR) -1))
12224 {
325fac50
PA
12225 best_low = std::min (best_low, current_low);
12226 best_high = std::max (best_high, current_high);
fae299cd
DC
12227 }
12228 break;
12229 default:
0963b4bd 12230 /* Ignore. */
fae299cd
DC
12231 break;
12232 }
12233
12234 child = sibling_die (child);
12235 }
12236 }
12237
12238 *lowpc = best_low;
12239 *highpc = best_high;
12240}
12241
801e3a5b
JB
12242/* Record the address ranges for BLOCK, offset by BASEADDR, as given
12243 in DIE. */
380bca97 12244
801e3a5b
JB
12245static void
12246dwarf2_record_block_ranges (struct die_info *die, struct block *block,
12247 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
12248{
bb5ed363 12249 struct objfile *objfile = cu->objfile;
3e29f34a 12250 struct gdbarch *gdbarch = get_objfile_arch (objfile);
801e3a5b 12251 struct attribute *attr;
91da1414 12252 struct attribute *attr_high;
801e3a5b 12253
91da1414
MW
12254 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
12255 if (attr_high)
801e3a5b 12256 {
801e3a5b
JB
12257 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
12258 if (attr)
12259 {
31aa7e4e
JB
12260 CORE_ADDR low = attr_value_as_address (attr);
12261 CORE_ADDR high = attr_value_as_address (attr_high);
12262
12263 if (cu->header.version >= 4 && attr_form_is_constant (attr_high))
12264 high += low;
9a619af0 12265
3e29f34a
MR
12266 low = gdbarch_adjust_dwarf2_addr (gdbarch, low + baseaddr);
12267 high = gdbarch_adjust_dwarf2_addr (gdbarch, high + baseaddr);
12268 record_block_range (block, low, high - 1);
801e3a5b
JB
12269 }
12270 }
12271
12272 attr = dwarf2_attr (die, DW_AT_ranges, cu);
12273 if (attr)
12274 {
bb5ed363 12275 bfd *obfd = objfile->obfd;
ab435259
DE
12276 /* DW_AT_ranges_base does not apply to DIEs from the DWO skeleton.
12277 We take advantage of the fact that DW_AT_ranges does not appear
12278 in DW_TAG_compile_unit of DWO files. */
12279 int need_ranges_base = die->tag != DW_TAG_compile_unit;
801e3a5b
JB
12280
12281 /* The value of the DW_AT_ranges attribute is the offset of the
12282 address range list in the .debug_ranges section. */
ab435259
DE
12283 unsigned long offset = (DW_UNSND (attr)
12284 + (need_ranges_base ? cu->ranges_base : 0));
d62bfeaf 12285 const gdb_byte *buffer;
801e3a5b
JB
12286
12287 /* For some target architectures, but not others, the
12288 read_address function sign-extends the addresses it returns.
12289 To recognize base address selection entries, we need a
12290 mask. */
12291 unsigned int addr_size = cu->header.addr_size;
12292 CORE_ADDR base_select_mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
12293
12294 /* The base address, to which the next pair is relative. Note
12295 that this 'base' is a DWARF concept: most entries in a range
12296 list are relative, to reduce the number of relocs against the
12297 debugging information. This is separate from this function's
12298 'baseaddr' argument, which GDB uses to relocate debugging
12299 information from a shared library based on the address at
12300 which the library was loaded. */
d00adf39
DE
12301 CORE_ADDR base = cu->base_address;
12302 int base_known = cu->base_known;
801e3a5b 12303
d62bfeaf 12304 dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
dce234bc 12305 if (offset >= dwarf2_per_objfile->ranges.size)
801e3a5b
JB
12306 {
12307 complaint (&symfile_complaints,
12308 _("Offset %lu out of bounds for DW_AT_ranges attribute"),
12309 offset);
12310 return;
12311 }
d62bfeaf 12312 buffer = dwarf2_per_objfile->ranges.buffer + offset;
801e3a5b
JB
12313
12314 for (;;)
12315 {
12316 unsigned int bytes_read;
12317 CORE_ADDR start, end;
12318
12319 start = read_address (obfd, buffer, cu, &bytes_read);
12320 buffer += bytes_read;
12321 end = read_address (obfd, buffer, cu, &bytes_read);
12322 buffer += bytes_read;
12323
12324 /* Did we find the end of the range list? */
12325 if (start == 0 && end == 0)
12326 break;
12327
12328 /* Did we find a base address selection entry? */
12329 else if ((start & base_select_mask) == base_select_mask)
12330 {
12331 base = end;
12332 base_known = 1;
12333 }
12334
12335 /* We found an ordinary address range. */
12336 else
12337 {
12338 if (!base_known)
12339 {
12340 complaint (&symfile_complaints,
3e43a32a
MS
12341 _("Invalid .debug_ranges data "
12342 "(no base address)"));
801e3a5b
JB
12343 return;
12344 }
12345
9277c30c
UW
12346 if (start > end)
12347 {
12348 /* Inverted range entries are invalid. */
12349 complaint (&symfile_complaints,
12350 _("Invalid .debug_ranges data "
12351 "(inverted range)"));
12352 return;
12353 }
12354
12355 /* Empty range entries have no effect. */
12356 if (start == end)
12357 continue;
12358
01093045
DE
12359 start += base + baseaddr;
12360 end += base + baseaddr;
12361
12362 /* A not-uncommon case of bad debug info.
12363 Don't pollute the addrmap with bad data. */
12364 if (start == 0 && !dwarf2_per_objfile->has_section_at_zero)
12365 {
12366 complaint (&symfile_complaints,
12367 _(".debug_ranges entry has start address of zero"
4262abfb 12368 " [in module %s]"), objfile_name (objfile));
01093045
DE
12369 continue;
12370 }
12371
3e29f34a
MR
12372 start = gdbarch_adjust_dwarf2_addr (gdbarch, start);
12373 end = gdbarch_adjust_dwarf2_addr (gdbarch, end);
01093045 12374 record_block_range (block, start, end - 1);
801e3a5b
JB
12375 }
12376 }
12377 }
12378}
12379
685b1105
JK
12380/* Check whether the producer field indicates either of GCC < 4.6, or the
12381 Intel C/C++ compiler, and cache the result in CU. */
60d5a603 12382
685b1105
JK
12383static void
12384check_producer (struct dwarf2_cu *cu)
60d5a603 12385{
38360086 12386 int major, minor;
60d5a603
JK
12387
12388 if (cu->producer == NULL)
12389 {
12390 /* For unknown compilers expect their behavior is DWARF version
12391 compliant.
12392
12393 GCC started to support .debug_types sections by -gdwarf-4 since
12394 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
12395 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
12396 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
12397 interpreted incorrectly by GDB now - GCC PR debug/48229. */
60d5a603 12398 }
b1ffba5a 12399 else if (producer_is_gcc (cu->producer, &major, &minor))
60d5a603 12400 {
38360086
MW
12401 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
12402 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
685b1105 12403 }
61012eef 12404 else if (startswith (cu->producer, "Intel(R) C"))
685b1105
JK
12405 cu->producer_is_icc = 1;
12406 else
12407 {
12408 /* For other non-GCC compilers, expect their behavior is DWARF version
12409 compliant. */
60d5a603
JK
12410 }
12411
ba919b58 12412 cu->checked_producer = 1;
685b1105 12413}
ba919b58 12414
685b1105
JK
12415/* Check for GCC PR debug/45124 fix which is not present in any G++ version up
12416 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
12417 during 4.6.0 experimental. */
12418
12419static int
12420producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
12421{
12422 if (!cu->checked_producer)
12423 check_producer (cu);
12424
12425 return cu->producer_is_gxx_lt_4_6;
60d5a603
JK
12426}
12427
12428/* Return the default accessibility type if it is not overriden by
12429 DW_AT_accessibility. */
12430
12431static enum dwarf_access_attribute
12432dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
12433{
12434 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
12435 {
12436 /* The default DWARF 2 accessibility for members is public, the default
12437 accessibility for inheritance is private. */
12438
12439 if (die->tag != DW_TAG_inheritance)
12440 return DW_ACCESS_public;
12441 else
12442 return DW_ACCESS_private;
12443 }
12444 else
12445 {
12446 /* DWARF 3+ defines the default accessibility a different way. The same
12447 rules apply now for DW_TAG_inheritance as for the members and it only
12448 depends on the container kind. */
12449
12450 if (die->parent->tag == DW_TAG_class_type)
12451 return DW_ACCESS_private;
12452 else
12453 return DW_ACCESS_public;
12454 }
12455}
12456
74ac6d43
TT
12457/* Look for DW_AT_data_member_location. Set *OFFSET to the byte
12458 offset. If the attribute was not found return 0, otherwise return
12459 1. If it was found but could not properly be handled, set *OFFSET
12460 to 0. */
12461
12462static int
12463handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
12464 LONGEST *offset)
12465{
12466 struct attribute *attr;
12467
12468 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
12469 if (attr != NULL)
12470 {
12471 *offset = 0;
12472
12473 /* Note that we do not check for a section offset first here.
12474 This is because DW_AT_data_member_location is new in DWARF 4,
12475 so if we see it, we can assume that a constant form is really
12476 a constant and not a section offset. */
12477 if (attr_form_is_constant (attr))
12478 *offset = dwarf2_get_attr_constant_value (attr, 0);
12479 else if (attr_form_is_section_offset (attr))
12480 dwarf2_complex_location_expr_complaint ();
12481 else if (attr_form_is_block (attr))
12482 *offset = decode_locdesc (DW_BLOCK (attr), cu);
12483 else
12484 dwarf2_complex_location_expr_complaint ();
12485
12486 return 1;
12487 }
12488
12489 return 0;
12490}
12491
c906108c
SS
12492/* Add an aggregate field to the field list. */
12493
12494static void
107d2387 12495dwarf2_add_field (struct field_info *fip, struct die_info *die,
e7c27a73 12496 struct dwarf2_cu *cu)
6e70227d 12497{
e7c27a73 12498 struct objfile *objfile = cu->objfile;
5e2b427d 12499 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
12500 struct nextfield *new_field;
12501 struct attribute *attr;
12502 struct field *fp;
15d034d0 12503 const char *fieldname = "";
c906108c
SS
12504
12505 /* Allocate a new field list entry and link it in. */
8d749320 12506 new_field = XNEW (struct nextfield);
b8c9b27d 12507 make_cleanup (xfree, new_field);
c906108c 12508 memset (new_field, 0, sizeof (struct nextfield));
7d0ccb61
DJ
12509
12510 if (die->tag == DW_TAG_inheritance)
12511 {
12512 new_field->next = fip->baseclasses;
12513 fip->baseclasses = new_field;
12514 }
12515 else
12516 {
12517 new_field->next = fip->fields;
12518 fip->fields = new_field;
12519 }
c906108c
SS
12520 fip->nfields++;
12521
e142c38c 12522 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
c906108c
SS
12523 if (attr)
12524 new_field->accessibility = DW_UNSND (attr);
60d5a603
JK
12525 else
12526 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
c906108c
SS
12527 if (new_field->accessibility != DW_ACCESS_public)
12528 fip->non_public_fields = 1;
60d5a603 12529
e142c38c 12530 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
c906108c
SS
12531 if (attr)
12532 new_field->virtuality = DW_UNSND (attr);
60d5a603
JK
12533 else
12534 new_field->virtuality = DW_VIRTUALITY_none;
c906108c
SS
12535
12536 fp = &new_field->field;
a9a9bd0f 12537
e142c38c 12538 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
c906108c 12539 {
74ac6d43
TT
12540 LONGEST offset;
12541
a9a9bd0f 12542 /* Data member other than a C++ static data member. */
6e70227d 12543
c906108c 12544 /* Get type of field. */
e7c27a73 12545 fp->type = die_type (die, cu);
c906108c 12546
d6a843b5 12547 SET_FIELD_BITPOS (*fp, 0);
01ad7f36 12548
c906108c 12549 /* Get bit size of field (zero if none). */
e142c38c 12550 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
c906108c
SS
12551 if (attr)
12552 {
12553 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
12554 }
12555 else
12556 {
12557 FIELD_BITSIZE (*fp) = 0;
12558 }
12559
12560 /* Get bit offset of field. */
74ac6d43
TT
12561 if (handle_data_member_location (die, cu, &offset))
12562 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
e142c38c 12563 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
c906108c
SS
12564 if (attr)
12565 {
5e2b427d 12566 if (gdbarch_bits_big_endian (gdbarch))
c906108c
SS
12567 {
12568 /* For big endian bits, the DW_AT_bit_offset gives the
c5aa993b
JM
12569 additional bit offset from the MSB of the containing
12570 anonymous object to the MSB of the field. We don't
12571 have to do anything special since we don't need to
12572 know the size of the anonymous object. */
f41f5e61 12573 SET_FIELD_BITPOS (*fp, FIELD_BITPOS (*fp) + DW_UNSND (attr));
c906108c
SS
12574 }
12575 else
12576 {
12577 /* For little endian bits, compute the bit offset to the
c5aa993b
JM
12578 MSB of the anonymous object, subtract off the number of
12579 bits from the MSB of the field to the MSB of the
12580 object, and then subtract off the number of bits of
12581 the field itself. The result is the bit offset of
12582 the LSB of the field. */
c906108c
SS
12583 int anonymous_size;
12584 int bit_offset = DW_UNSND (attr);
12585
e142c38c 12586 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
12587 if (attr)
12588 {
12589 /* The size of the anonymous object containing
12590 the bit field is explicit, so use the
12591 indicated size (in bytes). */
12592 anonymous_size = DW_UNSND (attr);
12593 }
12594 else
12595 {
12596 /* The size of the anonymous object containing
12597 the bit field must be inferred from the type
12598 attribute of the data member containing the
12599 bit field. */
12600 anonymous_size = TYPE_LENGTH (fp->type);
12601 }
f41f5e61
PA
12602 SET_FIELD_BITPOS (*fp,
12603 (FIELD_BITPOS (*fp)
12604 + anonymous_size * bits_per_byte
12605 - bit_offset - FIELD_BITSIZE (*fp)));
c906108c
SS
12606 }
12607 }
12608
12609 /* Get name of field. */
39cbfefa
DJ
12610 fieldname = dwarf2_name (die, cu);
12611 if (fieldname == NULL)
12612 fieldname = "";
d8151005
DJ
12613
12614 /* The name is already allocated along with this objfile, so we don't
12615 need to duplicate it for the type. */
12616 fp->name = fieldname;
c906108c
SS
12617
12618 /* Change accessibility for artificial fields (e.g. virtual table
c5aa993b 12619 pointer or virtual base class pointer) to private. */
e142c38c 12620 if (dwarf2_attr (die, DW_AT_artificial, cu))
c906108c 12621 {
d48cc9dd 12622 FIELD_ARTIFICIAL (*fp) = 1;
c906108c
SS
12623 new_field->accessibility = DW_ACCESS_private;
12624 fip->non_public_fields = 1;
12625 }
12626 }
a9a9bd0f 12627 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
c906108c 12628 {
a9a9bd0f
DC
12629 /* C++ static member. */
12630
12631 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
12632 is a declaration, but all versions of G++ as of this writing
12633 (so through at least 3.2.1) incorrectly generate
12634 DW_TAG_variable tags. */
6e70227d 12635
ff355380 12636 const char *physname;
c906108c 12637
a9a9bd0f 12638 /* Get name of field. */
39cbfefa
DJ
12639 fieldname = dwarf2_name (die, cu);
12640 if (fieldname == NULL)
c906108c
SS
12641 return;
12642
254e6b9e 12643 attr = dwarf2_attr (die, DW_AT_const_value, cu);
3863f96c
DE
12644 if (attr
12645 /* Only create a symbol if this is an external value.
12646 new_symbol checks this and puts the value in the global symbol
12647 table, which we want. If it is not external, new_symbol
12648 will try to put the value in cu->list_in_scope which is wrong. */
12649 && dwarf2_flag_true_p (die, DW_AT_external, cu))
254e6b9e
DE
12650 {
12651 /* A static const member, not much different than an enum as far as
12652 we're concerned, except that we can support more types. */
12653 new_symbol (die, NULL, cu);
12654 }
12655
2df3850c 12656 /* Get physical name. */
ff355380 12657 physname = dwarf2_physname (fieldname, die, cu);
c906108c 12658
d8151005
DJ
12659 /* The name is already allocated along with this objfile, so we don't
12660 need to duplicate it for the type. */
12661 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
e7c27a73 12662 FIELD_TYPE (*fp) = die_type (die, cu);
d8151005 12663 FIELD_NAME (*fp) = fieldname;
c906108c
SS
12664 }
12665 else if (die->tag == DW_TAG_inheritance)
12666 {
74ac6d43 12667 LONGEST offset;
d4b96c9a 12668
74ac6d43
TT
12669 /* C++ base class field. */
12670 if (handle_data_member_location (die, cu, &offset))
12671 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
c906108c 12672 FIELD_BITSIZE (*fp) = 0;
e7c27a73 12673 FIELD_TYPE (*fp) = die_type (die, cu);
c906108c
SS
12674 FIELD_NAME (*fp) = type_name_no_tag (fp->type);
12675 fip->nbaseclasses++;
12676 }
12677}
12678
98751a41
JK
12679/* Add a typedef defined in the scope of the FIP's class. */
12680
12681static void
12682dwarf2_add_typedef (struct field_info *fip, struct die_info *die,
12683 struct dwarf2_cu *cu)
6e70227d 12684{
98751a41 12685 struct typedef_field_list *new_field;
98751a41 12686 struct typedef_field *fp;
98751a41
JK
12687
12688 /* Allocate a new field list entry and link it in. */
8d749320 12689 new_field = XCNEW (struct typedef_field_list);
98751a41
JK
12690 make_cleanup (xfree, new_field);
12691
12692 gdb_assert (die->tag == DW_TAG_typedef);
12693
12694 fp = &new_field->field;
12695
12696 /* Get name of field. */
12697 fp->name = dwarf2_name (die, cu);
12698 if (fp->name == NULL)
12699 return;
12700
12701 fp->type = read_type_die (die, cu);
12702
12703 new_field->next = fip->typedef_field_list;
12704 fip->typedef_field_list = new_field;
12705 fip->typedef_field_list_count++;
12706}
12707
c906108c
SS
12708/* Create the vector of fields, and attach it to the type. */
12709
12710static void
fba45db2 12711dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 12712 struct dwarf2_cu *cu)
c906108c
SS
12713{
12714 int nfields = fip->nfields;
12715
12716 /* Record the field count, allocate space for the array of fields,
12717 and create blank accessibility bitfields if necessary. */
12718 TYPE_NFIELDS (type) = nfields;
12719 TYPE_FIELDS (type) = (struct field *)
12720 TYPE_ALLOC (type, sizeof (struct field) * nfields);
12721 memset (TYPE_FIELDS (type), 0, sizeof (struct field) * nfields);
12722
b4ba55a1 12723 if (fip->non_public_fields && cu->language != language_ada)
c906108c
SS
12724 {
12725 ALLOCATE_CPLUS_STRUCT_TYPE (type);
12726
12727 TYPE_FIELD_PRIVATE_BITS (type) =
12728 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
12729 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
12730
12731 TYPE_FIELD_PROTECTED_BITS (type) =
12732 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
12733 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
12734
774b6a14
TT
12735 TYPE_FIELD_IGNORE_BITS (type) =
12736 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
12737 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
c906108c
SS
12738 }
12739
12740 /* If the type has baseclasses, allocate and clear a bit vector for
12741 TYPE_FIELD_VIRTUAL_BITS. */
b4ba55a1 12742 if (fip->nbaseclasses && cu->language != language_ada)
c906108c
SS
12743 {
12744 int num_bytes = B_BYTES (fip->nbaseclasses);
fe1b8b76 12745 unsigned char *pointer;
c906108c
SS
12746
12747 ALLOCATE_CPLUS_STRUCT_TYPE (type);
224c3ddb 12748 pointer = (unsigned char *) TYPE_ALLOC (type, num_bytes);
fe1b8b76 12749 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
c906108c
SS
12750 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->nbaseclasses);
12751 TYPE_N_BASECLASSES (type) = fip->nbaseclasses;
12752 }
12753
3e43a32a
MS
12754 /* Copy the saved-up fields into the field vector. Start from the head of
12755 the list, adding to the tail of the field array, so that they end up in
12756 the same order in the array in which they were added to the list. */
c906108c
SS
12757 while (nfields-- > 0)
12758 {
7d0ccb61
DJ
12759 struct nextfield *fieldp;
12760
12761 if (fip->fields)
12762 {
12763 fieldp = fip->fields;
12764 fip->fields = fieldp->next;
12765 }
12766 else
12767 {
12768 fieldp = fip->baseclasses;
12769 fip->baseclasses = fieldp->next;
12770 }
12771
12772 TYPE_FIELD (type, nfields) = fieldp->field;
12773 switch (fieldp->accessibility)
c906108c 12774 {
c5aa993b 12775 case DW_ACCESS_private:
b4ba55a1
JB
12776 if (cu->language != language_ada)
12777 SET_TYPE_FIELD_PRIVATE (type, nfields);
c5aa993b 12778 break;
c906108c 12779
c5aa993b 12780 case DW_ACCESS_protected:
b4ba55a1
JB
12781 if (cu->language != language_ada)
12782 SET_TYPE_FIELD_PROTECTED (type, nfields);
c5aa993b 12783 break;
c906108c 12784
c5aa993b
JM
12785 case DW_ACCESS_public:
12786 break;
c906108c 12787
c5aa993b
JM
12788 default:
12789 /* Unknown accessibility. Complain and treat it as public. */
12790 {
e2e0b3e5 12791 complaint (&symfile_complaints, _("unsupported accessibility %d"),
7d0ccb61 12792 fieldp->accessibility);
c5aa993b
JM
12793 }
12794 break;
c906108c
SS
12795 }
12796 if (nfields < fip->nbaseclasses)
12797 {
7d0ccb61 12798 switch (fieldp->virtuality)
c906108c 12799 {
c5aa993b
JM
12800 case DW_VIRTUALITY_virtual:
12801 case DW_VIRTUALITY_pure_virtual:
b4ba55a1 12802 if (cu->language == language_ada)
a73c6dcd 12803 error (_("unexpected virtuality in component of Ada type"));
c5aa993b
JM
12804 SET_TYPE_FIELD_VIRTUAL (type, nfields);
12805 break;
c906108c
SS
12806 }
12807 }
c906108c
SS
12808 }
12809}
12810
7d27a96d
TT
12811/* Return true if this member function is a constructor, false
12812 otherwise. */
12813
12814static int
12815dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
12816{
12817 const char *fieldname;
fe978cb0 12818 const char *type_name;
7d27a96d
TT
12819 int len;
12820
12821 if (die->parent == NULL)
12822 return 0;
12823
12824 if (die->parent->tag != DW_TAG_structure_type
12825 && die->parent->tag != DW_TAG_union_type
12826 && die->parent->tag != DW_TAG_class_type)
12827 return 0;
12828
12829 fieldname = dwarf2_name (die, cu);
fe978cb0
PA
12830 type_name = dwarf2_name (die->parent, cu);
12831 if (fieldname == NULL || type_name == NULL)
7d27a96d
TT
12832 return 0;
12833
12834 len = strlen (fieldname);
fe978cb0
PA
12835 return (strncmp (fieldname, type_name, len) == 0
12836 && (type_name[len] == '\0' || type_name[len] == '<'));
7d27a96d
TT
12837}
12838
c906108c
SS
12839/* Add a member function to the proper fieldlist. */
12840
12841static void
107d2387 12842dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
e7c27a73 12843 struct type *type, struct dwarf2_cu *cu)
c906108c 12844{
e7c27a73 12845 struct objfile *objfile = cu->objfile;
c906108c
SS
12846 struct attribute *attr;
12847 struct fnfieldlist *flp;
12848 int i;
12849 struct fn_field *fnp;
15d034d0 12850 const char *fieldname;
c906108c 12851 struct nextfnfield *new_fnfield;
f792889a 12852 struct type *this_type;
60d5a603 12853 enum dwarf_access_attribute accessibility;
c906108c 12854
b4ba55a1 12855 if (cu->language == language_ada)
a73c6dcd 12856 error (_("unexpected member function in Ada type"));
b4ba55a1 12857
2df3850c 12858 /* Get name of member function. */
39cbfefa
DJ
12859 fieldname = dwarf2_name (die, cu);
12860 if (fieldname == NULL)
2df3850c 12861 return;
c906108c 12862
c906108c
SS
12863 /* Look up member function name in fieldlist. */
12864 for (i = 0; i < fip->nfnfields; i++)
12865 {
27bfe10e 12866 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
c906108c
SS
12867 break;
12868 }
12869
12870 /* Create new list element if necessary. */
12871 if (i < fip->nfnfields)
12872 flp = &fip->fnfieldlists[i];
12873 else
12874 {
12875 if ((fip->nfnfields % DW_FIELD_ALLOC_CHUNK) == 0)
12876 {
12877 fip->fnfieldlists = (struct fnfieldlist *)
12878 xrealloc (fip->fnfieldlists,
12879 (fip->nfnfields + DW_FIELD_ALLOC_CHUNK)
c5aa993b 12880 * sizeof (struct fnfieldlist));
c906108c 12881 if (fip->nfnfields == 0)
c13c43fd 12882 make_cleanup (free_current_contents, &fip->fnfieldlists);
c906108c
SS
12883 }
12884 flp = &fip->fnfieldlists[fip->nfnfields];
12885 flp->name = fieldname;
12886 flp->length = 0;
12887 flp->head = NULL;
3da10d80 12888 i = fip->nfnfields++;
c906108c
SS
12889 }
12890
12891 /* Create a new member function field and chain it to the field list
0963b4bd 12892 entry. */
8d749320 12893 new_fnfield = XNEW (struct nextfnfield);
b8c9b27d 12894 make_cleanup (xfree, new_fnfield);
c906108c
SS
12895 memset (new_fnfield, 0, sizeof (struct nextfnfield));
12896 new_fnfield->next = flp->head;
12897 flp->head = new_fnfield;
12898 flp->length++;
12899
12900 /* Fill in the member function field info. */
12901 fnp = &new_fnfield->fnfield;
3da10d80
KS
12902
12903 /* Delay processing of the physname until later. */
12904 if (cu->language == language_cplus || cu->language == language_java)
12905 {
12906 add_to_method_list (type, i, flp->length - 1, fieldname,
12907 die, cu);
12908 }
12909 else
12910 {
1d06ead6 12911 const char *physname = dwarf2_physname (fieldname, die, cu);
3da10d80
KS
12912 fnp->physname = physname ? physname : "";
12913 }
12914
c906108c 12915 fnp->type = alloc_type (objfile);
f792889a
DJ
12916 this_type = read_type_die (die, cu);
12917 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
c906108c 12918 {
f792889a 12919 int nparams = TYPE_NFIELDS (this_type);
c906108c 12920
f792889a 12921 /* TYPE is the domain of this method, and THIS_TYPE is the type
e26fb1d7
DC
12922 of the method itself (TYPE_CODE_METHOD). */
12923 smash_to_method_type (fnp->type, type,
f792889a
DJ
12924 TYPE_TARGET_TYPE (this_type),
12925 TYPE_FIELDS (this_type),
12926 TYPE_NFIELDS (this_type),
12927 TYPE_VARARGS (this_type));
c906108c
SS
12928
12929 /* Handle static member functions.
c5aa993b 12930 Dwarf2 has no clean way to discern C++ static and non-static
0963b4bd
MS
12931 member functions. G++ helps GDB by marking the first
12932 parameter for non-static member functions (which is the this
12933 pointer) as artificial. We obtain this information from
12934 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
f792889a 12935 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
c906108c
SS
12936 fnp->voffset = VOFFSET_STATIC;
12937 }
12938 else
e2e0b3e5 12939 complaint (&symfile_complaints, _("member function type missing for '%s'"),
3da10d80 12940 dwarf2_full_name (fieldname, die, cu));
c906108c
SS
12941
12942 /* Get fcontext from DW_AT_containing_type if present. */
e142c38c 12943 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
e7c27a73 12944 fnp->fcontext = die_containing_type (die, cu);
c906108c 12945
3e43a32a
MS
12946 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
12947 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
c906108c
SS
12948
12949 /* Get accessibility. */
e142c38c 12950 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
c906108c 12951 if (attr)
aead7601 12952 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
60d5a603
JK
12953 else
12954 accessibility = dwarf2_default_access_attribute (die, cu);
12955 switch (accessibility)
c906108c 12956 {
60d5a603
JK
12957 case DW_ACCESS_private:
12958 fnp->is_private = 1;
12959 break;
12960 case DW_ACCESS_protected:
12961 fnp->is_protected = 1;
12962 break;
c906108c
SS
12963 }
12964
b02dede2 12965 /* Check for artificial methods. */
e142c38c 12966 attr = dwarf2_attr (die, DW_AT_artificial, cu);
b02dede2
DJ
12967 if (attr && DW_UNSND (attr) != 0)
12968 fnp->is_artificial = 1;
12969
7d27a96d
TT
12970 fnp->is_constructor = dwarf2_is_constructor (die, cu);
12971
0d564a31 12972 /* Get index in virtual function table if it is a virtual member
aec5aa8b
TT
12973 function. For older versions of GCC, this is an offset in the
12974 appropriate virtual table, as specified by DW_AT_containing_type.
12975 For everyone else, it is an expression to be evaluated relative
0d564a31
DJ
12976 to the object address. */
12977
e142c38c 12978 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
aec5aa8b 12979 if (attr)
8e19ed76 12980 {
aec5aa8b 12981 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size > 0)
8e19ed76 12982 {
aec5aa8b
TT
12983 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
12984 {
12985 /* Old-style GCC. */
12986 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
12987 }
12988 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
12989 || (DW_BLOCK (attr)->size > 1
12990 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
12991 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
12992 {
aec5aa8b
TT
12993 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
12994 if ((fnp->voffset % cu->header.addr_size) != 0)
12995 dwarf2_complex_location_expr_complaint ();
12996 else
12997 fnp->voffset /= cu->header.addr_size;
12998 fnp->voffset += 2;
12999 }
13000 else
13001 dwarf2_complex_location_expr_complaint ();
13002
13003 if (!fnp->fcontext)
7e993ebf
KS
13004 {
13005 /* If there is no `this' field and no DW_AT_containing_type,
13006 we cannot actually find a base class context for the
13007 vtable! */
13008 if (TYPE_NFIELDS (this_type) == 0
13009 || !TYPE_FIELD_ARTIFICIAL (this_type, 0))
13010 {
13011 complaint (&symfile_complaints,
13012 _("cannot determine context for virtual member "
13013 "function \"%s\" (offset %d)"),
13014 fieldname, die->offset.sect_off);
13015 }
13016 else
13017 {
13018 fnp->fcontext
13019 = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
13020 }
13021 }
aec5aa8b 13022 }
3690dd37 13023 else if (attr_form_is_section_offset (attr))
8e19ed76 13024 {
4d3c2250 13025 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
13026 }
13027 else
13028 {
4d3c2250
KB
13029 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
13030 fieldname);
8e19ed76 13031 }
0d564a31 13032 }
d48cc9dd
DJ
13033 else
13034 {
13035 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
13036 if (attr && DW_UNSND (attr))
13037 {
13038 /* GCC does this, as of 2008-08-25; PR debug/37237. */
13039 complaint (&symfile_complaints,
3e43a32a
MS
13040 _("Member function \"%s\" (offset %d) is virtual "
13041 "but the vtable offset is not specified"),
b64f50a1 13042 fieldname, die->offset.sect_off);
9655fd1a 13043 ALLOCATE_CPLUS_STRUCT_TYPE (type);
d48cc9dd
DJ
13044 TYPE_CPLUS_DYNAMIC (type) = 1;
13045 }
13046 }
c906108c
SS
13047}
13048
13049/* Create the vector of member function fields, and attach it to the type. */
13050
13051static void
fba45db2 13052dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 13053 struct dwarf2_cu *cu)
c906108c
SS
13054{
13055 struct fnfieldlist *flp;
c906108c
SS
13056 int i;
13057
b4ba55a1 13058 if (cu->language == language_ada)
a73c6dcd 13059 error (_("unexpected member functions in Ada type"));
b4ba55a1 13060
c906108c
SS
13061 ALLOCATE_CPLUS_STRUCT_TYPE (type);
13062 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
13063 TYPE_ALLOC (type, sizeof (struct fn_fieldlist) * fip->nfnfields);
13064
13065 for (i = 0, flp = fip->fnfieldlists; i < fip->nfnfields; i++, flp++)
13066 {
13067 struct nextfnfield *nfp = flp->head;
13068 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
13069 int k;
13070
13071 TYPE_FN_FIELDLIST_NAME (type, i) = flp->name;
13072 TYPE_FN_FIELDLIST_LENGTH (type, i) = flp->length;
13073 fn_flp->fn_fields = (struct fn_field *)
13074 TYPE_ALLOC (type, sizeof (struct fn_field) * flp->length);
13075 for (k = flp->length; (k--, nfp); nfp = nfp->next)
c5aa993b 13076 fn_flp->fn_fields[k] = nfp->fnfield;
c906108c
SS
13077 }
13078
13079 TYPE_NFN_FIELDS (type) = fip->nfnfields;
c906108c
SS
13080}
13081
1168df01
JB
13082/* Returns non-zero if NAME is the name of a vtable member in CU's
13083 language, zero otherwise. */
13084static int
13085is_vtable_name (const char *name, struct dwarf2_cu *cu)
13086{
13087 static const char vptr[] = "_vptr";
987504bb 13088 static const char vtable[] = "vtable";
1168df01 13089
987504bb
JJ
13090 /* Look for the C++ and Java forms of the vtable. */
13091 if ((cu->language == language_java
61012eef
GB
13092 && startswith (name, vtable))
13093 || (startswith (name, vptr)
987504bb 13094 && is_cplus_marker (name[sizeof (vptr) - 1])))
1168df01
JB
13095 return 1;
13096
13097 return 0;
13098}
13099
c0dd20ea 13100/* GCC outputs unnamed structures that are really pointers to member
0b92b5bb
TT
13101 functions, with the ABI-specified layout. If TYPE describes
13102 such a structure, smash it into a member function type.
61049d3b
DJ
13103
13104 GCC shouldn't do this; it should just output pointer to member DIEs.
13105 This is GCC PR debug/28767. */
c0dd20ea 13106
0b92b5bb
TT
13107static void
13108quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
c0dd20ea 13109{
09e2d7c7 13110 struct type *pfn_type, *self_type, *new_type;
c0dd20ea
DJ
13111
13112 /* Check for a structure with no name and two children. */
0b92b5bb
TT
13113 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
13114 return;
c0dd20ea
DJ
13115
13116 /* Check for __pfn and __delta members. */
0b92b5bb
TT
13117 if (TYPE_FIELD_NAME (type, 0) == NULL
13118 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
13119 || TYPE_FIELD_NAME (type, 1) == NULL
13120 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
13121 return;
c0dd20ea
DJ
13122
13123 /* Find the type of the method. */
0b92b5bb 13124 pfn_type = TYPE_FIELD_TYPE (type, 0);
c0dd20ea
DJ
13125 if (pfn_type == NULL
13126 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
13127 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
0b92b5bb 13128 return;
c0dd20ea
DJ
13129
13130 /* Look for the "this" argument. */
13131 pfn_type = TYPE_TARGET_TYPE (pfn_type);
13132 if (TYPE_NFIELDS (pfn_type) == 0
0b92b5bb 13133 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
c0dd20ea 13134 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
0b92b5bb 13135 return;
c0dd20ea 13136
09e2d7c7 13137 self_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
0b92b5bb 13138 new_type = alloc_type (objfile);
09e2d7c7 13139 smash_to_method_type (new_type, self_type, TYPE_TARGET_TYPE (pfn_type),
c0dd20ea
DJ
13140 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
13141 TYPE_VARARGS (pfn_type));
0b92b5bb 13142 smash_to_methodptr_type (type, new_type);
c0dd20ea 13143}
1168df01 13144
685b1105
JK
13145/* Return non-zero if the CU's PRODUCER string matches the Intel C/C++ compiler
13146 (icc). */
13147
13148static int
13149producer_is_icc (struct dwarf2_cu *cu)
13150{
13151 if (!cu->checked_producer)
13152 check_producer (cu);
13153
13154 return cu->producer_is_icc;
13155}
13156
c906108c 13157/* Called when we find the DIE that starts a structure or union scope
c767944b
DJ
13158 (definition) to create a type for the structure or union. Fill in
13159 the type's name and general properties; the members will not be
83655187
DE
13160 processed until process_structure_scope. A symbol table entry for
13161 the type will also not be done until process_structure_scope (assuming
13162 the type has a name).
c906108c 13163
c767944b
DJ
13164 NOTE: we need to call these functions regardless of whether or not the
13165 DIE has a DW_AT_name attribute, since it might be an anonymous
c906108c 13166 structure or union. This gets the type entered into our set of
83655187 13167 user defined types. */
c906108c 13168
f792889a 13169static struct type *
134d01f1 13170read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 13171{
e7c27a73 13172 struct objfile *objfile = cu->objfile;
c906108c
SS
13173 struct type *type;
13174 struct attribute *attr;
15d034d0 13175 const char *name;
c906108c 13176
348e048f
DE
13177 /* If the definition of this type lives in .debug_types, read that type.
13178 Don't follow DW_AT_specification though, that will take us back up
13179 the chain and we want to go down. */
45e58e77 13180 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
348e048f
DE
13181 if (attr)
13182 {
ac9ec31b 13183 type = get_DW_AT_signature_type (die, attr, cu);
9dc481d3 13184
ac9ec31b 13185 /* The type's CU may not be the same as CU.
02142a6c 13186 Ensure TYPE is recorded with CU in die_type_hash. */
348e048f
DE
13187 return set_die_type (die, type, cu);
13188 }
13189
c0dd20ea 13190 type = alloc_type (objfile);
c906108c 13191 INIT_CPLUS_SPECIFIC (type);
93311388 13192
39cbfefa
DJ
13193 name = dwarf2_name (die, cu);
13194 if (name != NULL)
c906108c 13195 {
987504bb 13196 if (cu->language == language_cplus
45280282 13197 || cu->language == language_java
c44af4eb
TT
13198 || cu->language == language_d
13199 || cu->language == language_rust)
63d06c5c 13200 {
15d034d0 13201 const char *full_name = dwarf2_full_name (name, die, cu);
3da10d80
KS
13202
13203 /* dwarf2_full_name might have already finished building the DIE's
13204 type. If so, there is no need to continue. */
13205 if (get_die_type (die, cu) != NULL)
13206 return get_die_type (die, cu);
13207
13208 TYPE_TAG_NAME (type) = full_name;
94af9270
KS
13209 if (die->tag == DW_TAG_structure_type
13210 || die->tag == DW_TAG_class_type)
13211 TYPE_NAME (type) = TYPE_TAG_NAME (type);
63d06c5c
DC
13212 }
13213 else
13214 {
d8151005
DJ
13215 /* The name is already allocated along with this objfile, so
13216 we don't need to duplicate it for the type. */
7d455152 13217 TYPE_TAG_NAME (type) = name;
94af9270
KS
13218 if (die->tag == DW_TAG_class_type)
13219 TYPE_NAME (type) = TYPE_TAG_NAME (type);
63d06c5c 13220 }
c906108c
SS
13221 }
13222
13223 if (die->tag == DW_TAG_structure_type)
13224 {
13225 TYPE_CODE (type) = TYPE_CODE_STRUCT;
13226 }
13227 else if (die->tag == DW_TAG_union_type)
13228 {
13229 TYPE_CODE (type) = TYPE_CODE_UNION;
13230 }
13231 else
13232 {
4753d33b 13233 TYPE_CODE (type) = TYPE_CODE_STRUCT;
c906108c
SS
13234 }
13235
0cc2414c
TT
13236 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
13237 TYPE_DECLARED_CLASS (type) = 1;
13238
e142c38c 13239 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
13240 if (attr)
13241 {
155bfbd3
JB
13242 if (attr_form_is_constant (attr))
13243 TYPE_LENGTH (type) = DW_UNSND (attr);
13244 else
13245 {
13246 /* For the moment, dynamic type sizes are not supported
13247 by GDB's struct type. The actual size is determined
13248 on-demand when resolving the type of a given object,
13249 so set the type's length to zero for now. Otherwise,
13250 we record an expression as the length, and that expression
13251 could lead to a very large value, which could eventually
13252 lead to us trying to allocate that much memory when creating
13253 a value of that type. */
13254 TYPE_LENGTH (type) = 0;
13255 }
c906108c
SS
13256 }
13257 else
13258 {
13259 TYPE_LENGTH (type) = 0;
13260 }
13261
422b1cb0 13262 if (producer_is_icc (cu) && (TYPE_LENGTH (type) == 0))
685b1105
JK
13263 {
13264 /* ICC does not output the required DW_AT_declaration
13265 on incomplete types, but gives them a size of zero. */
422b1cb0 13266 TYPE_STUB (type) = 1;
685b1105
JK
13267 }
13268 else
13269 TYPE_STUB_SUPPORTED (type) = 1;
13270
dc718098 13271 if (die_is_declaration (die, cu))
876cecd0 13272 TYPE_STUB (type) = 1;
a6c727b2
DJ
13273 else if (attr == NULL && die->child == NULL
13274 && producer_is_realview (cu->producer))
13275 /* RealView does not output the required DW_AT_declaration
13276 on incomplete types. */
13277 TYPE_STUB (type) = 1;
dc718098 13278
c906108c
SS
13279 /* We need to add the type field to the die immediately so we don't
13280 infinitely recurse when dealing with pointers to the structure
0963b4bd 13281 type within the structure itself. */
1c379e20 13282 set_die_type (die, type, cu);
c906108c 13283
7e314c57
JK
13284 /* set_die_type should be already done. */
13285 set_descriptive_type (type, die, cu);
13286
c767944b
DJ
13287 return type;
13288}
13289
13290/* Finish creating a structure or union type, including filling in
13291 its members and creating a symbol for it. */
13292
13293static void
13294process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
13295{
13296 struct objfile *objfile = cu->objfile;
ca040673 13297 struct die_info *child_die;
c767944b
DJ
13298 struct type *type;
13299
13300 type = get_die_type (die, cu);
13301 if (type == NULL)
13302 type = read_structure_type (die, cu);
13303
e142c38c 13304 if (die->child != NULL && ! die_is_declaration (die, cu))
c906108c
SS
13305 {
13306 struct field_info fi;
34eaf542 13307 VEC (symbolp) *template_args = NULL;
c767944b 13308 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
c906108c
SS
13309
13310 memset (&fi, 0, sizeof (struct field_info));
13311
639d11d3 13312 child_die = die->child;
c906108c
SS
13313
13314 while (child_die && child_die->tag)
13315 {
a9a9bd0f
DC
13316 if (child_die->tag == DW_TAG_member
13317 || child_die->tag == DW_TAG_variable)
c906108c 13318 {
a9a9bd0f
DC
13319 /* NOTE: carlton/2002-11-05: A C++ static data member
13320 should be a DW_TAG_member that is a declaration, but
13321 all versions of G++ as of this writing (so through at
13322 least 3.2.1) incorrectly generate DW_TAG_variable
13323 tags for them instead. */
e7c27a73 13324 dwarf2_add_field (&fi, child_die, cu);
c906108c 13325 }
8713b1b1 13326 else if (child_die->tag == DW_TAG_subprogram)
c906108c 13327 {
e98c9e7c
TT
13328 /* Rust doesn't have member functions in the C++ sense.
13329 However, it does emit ordinary functions as children
13330 of a struct DIE. */
13331 if (cu->language == language_rust)
13332 read_func_scope (child_die, cu);
13333 else
13334 {
13335 /* C++ member function. */
13336 dwarf2_add_member_fn (&fi, child_die, type, cu);
13337 }
c906108c
SS
13338 }
13339 else if (child_die->tag == DW_TAG_inheritance)
13340 {
13341 /* C++ base class field. */
e7c27a73 13342 dwarf2_add_field (&fi, child_die, cu);
c906108c 13343 }
98751a41
JK
13344 else if (child_die->tag == DW_TAG_typedef)
13345 dwarf2_add_typedef (&fi, child_die, cu);
34eaf542
TT
13346 else if (child_die->tag == DW_TAG_template_type_param
13347 || child_die->tag == DW_TAG_template_value_param)
13348 {
13349 struct symbol *arg = new_symbol (child_die, NULL, cu);
13350
f1078f66
DJ
13351 if (arg != NULL)
13352 VEC_safe_push (symbolp, template_args, arg);
34eaf542
TT
13353 }
13354
c906108c
SS
13355 child_die = sibling_die (child_die);
13356 }
13357
34eaf542
TT
13358 /* Attach template arguments to type. */
13359 if (! VEC_empty (symbolp, template_args))
13360 {
13361 ALLOCATE_CPLUS_STRUCT_TYPE (type);
13362 TYPE_N_TEMPLATE_ARGUMENTS (type)
13363 = VEC_length (symbolp, template_args);
13364 TYPE_TEMPLATE_ARGUMENTS (type)
8d749320
SM
13365 = XOBNEWVEC (&objfile->objfile_obstack,
13366 struct symbol *,
13367 TYPE_N_TEMPLATE_ARGUMENTS (type));
34eaf542
TT
13368 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
13369 VEC_address (symbolp, template_args),
13370 (TYPE_N_TEMPLATE_ARGUMENTS (type)
13371 * sizeof (struct symbol *)));
13372 VEC_free (symbolp, template_args);
13373 }
13374
c906108c
SS
13375 /* Attach fields and member functions to the type. */
13376 if (fi.nfields)
e7c27a73 13377 dwarf2_attach_fields_to_type (&fi, type, cu);
c906108c
SS
13378 if (fi.nfnfields)
13379 {
e7c27a73 13380 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
c906108c 13381
c5aa993b 13382 /* Get the type which refers to the base class (possibly this
c906108c 13383 class itself) which contains the vtable pointer for the current
0d564a31
DJ
13384 class from the DW_AT_containing_type attribute. This use of
13385 DW_AT_containing_type is a GNU extension. */
c906108c 13386
e142c38c 13387 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
c906108c 13388 {
e7c27a73 13389 struct type *t = die_containing_type (die, cu);
c906108c 13390
ae6ae975 13391 set_type_vptr_basetype (type, t);
c906108c
SS
13392 if (type == t)
13393 {
c906108c
SS
13394 int i;
13395
13396 /* Our own class provides vtbl ptr. */
13397 for (i = TYPE_NFIELDS (t) - 1;
13398 i >= TYPE_N_BASECLASSES (t);
13399 --i)
13400 {
0d5cff50 13401 const char *fieldname = TYPE_FIELD_NAME (t, i);
c906108c 13402
1168df01 13403 if (is_vtable_name (fieldname, cu))
c906108c 13404 {
ae6ae975 13405 set_type_vptr_fieldno (type, i);
c906108c
SS
13406 break;
13407 }
13408 }
13409
13410 /* Complain if virtual function table field not found. */
13411 if (i < TYPE_N_BASECLASSES (t))
4d3c2250 13412 complaint (&symfile_complaints,
3e43a32a
MS
13413 _("virtual function table pointer "
13414 "not found when defining class '%s'"),
4d3c2250
KB
13415 TYPE_TAG_NAME (type) ? TYPE_TAG_NAME (type) :
13416 "");
c906108c
SS
13417 }
13418 else
13419 {
ae6ae975 13420 set_type_vptr_fieldno (type, TYPE_VPTR_FIELDNO (t));
c906108c
SS
13421 }
13422 }
f6235d4c 13423 else if (cu->producer
61012eef 13424 && startswith (cu->producer, "IBM(R) XL C/C++ Advanced Edition"))
f6235d4c
EZ
13425 {
13426 /* The IBM XLC compiler does not provide direct indication
13427 of the containing type, but the vtable pointer is
13428 always named __vfp. */
13429
13430 int i;
13431
13432 for (i = TYPE_NFIELDS (type) - 1;
13433 i >= TYPE_N_BASECLASSES (type);
13434 --i)
13435 {
13436 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
13437 {
ae6ae975
DE
13438 set_type_vptr_fieldno (type, i);
13439 set_type_vptr_basetype (type, type);
f6235d4c
EZ
13440 break;
13441 }
13442 }
13443 }
c906108c 13444 }
98751a41
JK
13445
13446 /* Copy fi.typedef_field_list linked list elements content into the
13447 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
13448 if (fi.typedef_field_list)
13449 {
13450 int i = fi.typedef_field_list_count;
13451
a0d7a4ff 13452 ALLOCATE_CPLUS_STRUCT_TYPE (type);
98751a41 13453 TYPE_TYPEDEF_FIELD_ARRAY (type)
224c3ddb
SM
13454 = ((struct typedef_field *)
13455 TYPE_ALLOC (type, sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * i));
98751a41
JK
13456 TYPE_TYPEDEF_FIELD_COUNT (type) = i;
13457
13458 /* Reverse the list order to keep the debug info elements order. */
13459 while (--i >= 0)
13460 {
13461 struct typedef_field *dest, *src;
6e70227d 13462
98751a41
JK
13463 dest = &TYPE_TYPEDEF_FIELD (type, i);
13464 src = &fi.typedef_field_list->field;
13465 fi.typedef_field_list = fi.typedef_field_list->next;
13466 *dest = *src;
13467 }
13468 }
c767944b
DJ
13469
13470 do_cleanups (back_to);
eb2a6f42
TT
13471
13472 if (HAVE_CPLUS_STRUCT (type))
13473 TYPE_CPLUS_REALLY_JAVA (type) = cu->language == language_java;
c906108c 13474 }
63d06c5c 13475
bb5ed363 13476 quirk_gcc_member_function_pointer (type, objfile);
0b92b5bb 13477
90aeadfc
DC
13478 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
13479 snapshots) has been known to create a die giving a declaration
13480 for a class that has, as a child, a die giving a definition for a
13481 nested class. So we have to process our children even if the
13482 current die is a declaration. Normally, of course, a declaration
13483 won't have any children at all. */
134d01f1 13484
ca040673
DE
13485 child_die = die->child;
13486
90aeadfc
DC
13487 while (child_die != NULL && child_die->tag)
13488 {
13489 if (child_die->tag == DW_TAG_member
13490 || child_die->tag == DW_TAG_variable
34eaf542
TT
13491 || child_die->tag == DW_TAG_inheritance
13492 || child_die->tag == DW_TAG_template_value_param
13493 || child_die->tag == DW_TAG_template_type_param)
134d01f1 13494 {
90aeadfc 13495 /* Do nothing. */
134d01f1 13496 }
90aeadfc
DC
13497 else
13498 process_die (child_die, cu);
134d01f1 13499
90aeadfc 13500 child_die = sibling_die (child_die);
134d01f1
DJ
13501 }
13502
fa4028e9
JB
13503 /* Do not consider external references. According to the DWARF standard,
13504 these DIEs are identified by the fact that they have no byte_size
13505 attribute, and a declaration attribute. */
13506 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
13507 || !die_is_declaration (die, cu))
c767944b 13508 new_symbol (die, type, cu);
134d01f1
DJ
13509}
13510
55426c9d
JB
13511/* Assuming DIE is an enumeration type, and TYPE is its associated type,
13512 update TYPE using some information only available in DIE's children. */
13513
13514static void
13515update_enumeration_type_from_children (struct die_info *die,
13516 struct type *type,
13517 struct dwarf2_cu *cu)
13518{
13519 struct obstack obstack;
60f7655a 13520 struct die_info *child_die;
55426c9d
JB
13521 int unsigned_enum = 1;
13522 int flag_enum = 1;
13523 ULONGEST mask = 0;
13524 struct cleanup *old_chain;
13525
13526 obstack_init (&obstack);
13527 old_chain = make_cleanup_obstack_free (&obstack);
13528
60f7655a
DE
13529 for (child_die = die->child;
13530 child_die != NULL && child_die->tag;
13531 child_die = sibling_die (child_die))
55426c9d
JB
13532 {
13533 struct attribute *attr;
13534 LONGEST value;
13535 const gdb_byte *bytes;
13536 struct dwarf2_locexpr_baton *baton;
13537 const char *name;
60f7655a 13538
55426c9d
JB
13539 if (child_die->tag != DW_TAG_enumerator)
13540 continue;
13541
13542 attr = dwarf2_attr (child_die, DW_AT_const_value, cu);
13543 if (attr == NULL)
13544 continue;
13545
13546 name = dwarf2_name (child_die, cu);
13547 if (name == NULL)
13548 name = "<anonymous enumerator>";
13549
13550 dwarf2_const_value_attr (attr, type, name, &obstack, cu,
13551 &value, &bytes, &baton);
13552 if (value < 0)
13553 {
13554 unsigned_enum = 0;
13555 flag_enum = 0;
13556 }
13557 else if ((mask & value) != 0)
13558 flag_enum = 0;
13559 else
13560 mask |= value;
13561
13562 /* If we already know that the enum type is neither unsigned, nor
13563 a flag type, no need to look at the rest of the enumerates. */
13564 if (!unsigned_enum && !flag_enum)
13565 break;
55426c9d
JB
13566 }
13567
13568 if (unsigned_enum)
13569 TYPE_UNSIGNED (type) = 1;
13570 if (flag_enum)
13571 TYPE_FLAG_ENUM (type) = 1;
13572
13573 do_cleanups (old_chain);
13574}
13575
134d01f1
DJ
13576/* Given a DW_AT_enumeration_type die, set its type. We do not
13577 complete the type's fields yet, or create any symbols. */
c906108c 13578
f792889a 13579static struct type *
134d01f1 13580read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 13581{
e7c27a73 13582 struct objfile *objfile = cu->objfile;
c906108c 13583 struct type *type;
c906108c 13584 struct attribute *attr;
0114d602 13585 const char *name;
134d01f1 13586
348e048f
DE
13587 /* If the definition of this type lives in .debug_types, read that type.
13588 Don't follow DW_AT_specification though, that will take us back up
13589 the chain and we want to go down. */
45e58e77 13590 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
348e048f
DE
13591 if (attr)
13592 {
ac9ec31b 13593 type = get_DW_AT_signature_type (die, attr, cu);
9dc481d3 13594
ac9ec31b 13595 /* The type's CU may not be the same as CU.
02142a6c 13596 Ensure TYPE is recorded with CU in die_type_hash. */
348e048f
DE
13597 return set_die_type (die, type, cu);
13598 }
13599
c906108c
SS
13600 type = alloc_type (objfile);
13601
13602 TYPE_CODE (type) = TYPE_CODE_ENUM;
94af9270 13603 name = dwarf2_full_name (NULL, die, cu);
39cbfefa 13604 if (name != NULL)
7d455152 13605 TYPE_TAG_NAME (type) = name;
c906108c 13606
0626fc76
TT
13607 attr = dwarf2_attr (die, DW_AT_type, cu);
13608 if (attr != NULL)
13609 {
13610 struct type *underlying_type = die_type (die, cu);
13611
13612 TYPE_TARGET_TYPE (type) = underlying_type;
13613 }
13614
e142c38c 13615 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
13616 if (attr)
13617 {
13618 TYPE_LENGTH (type) = DW_UNSND (attr);
13619 }
13620 else
13621 {
13622 TYPE_LENGTH (type) = 0;
13623 }
13624
137033e9
JB
13625 /* The enumeration DIE can be incomplete. In Ada, any type can be
13626 declared as private in the package spec, and then defined only
13627 inside the package body. Such types are known as Taft Amendment
13628 Types. When another package uses such a type, an incomplete DIE
13629 may be generated by the compiler. */
02eb380e 13630 if (die_is_declaration (die, cu))
876cecd0 13631 TYPE_STUB (type) = 1;
02eb380e 13632
0626fc76
TT
13633 /* Finish the creation of this type by using the enum's children.
13634 We must call this even when the underlying type has been provided
13635 so that we can determine if we're looking at a "flag" enum. */
55426c9d
JB
13636 update_enumeration_type_from_children (die, type, cu);
13637
0626fc76
TT
13638 /* If this type has an underlying type that is not a stub, then we
13639 may use its attributes. We always use the "unsigned" attribute
13640 in this situation, because ordinarily we guess whether the type
13641 is unsigned -- but the guess can be wrong and the underlying type
13642 can tell us the reality. However, we defer to a local size
13643 attribute if one exists, because this lets the compiler override
13644 the underlying type if needed. */
13645 if (TYPE_TARGET_TYPE (type) != NULL && !TYPE_STUB (TYPE_TARGET_TYPE (type)))
13646 {
13647 TYPE_UNSIGNED (type) = TYPE_UNSIGNED (TYPE_TARGET_TYPE (type));
13648 if (TYPE_LENGTH (type) == 0)
13649 TYPE_LENGTH (type) = TYPE_LENGTH (TYPE_TARGET_TYPE (type));
13650 }
13651
3d567982
TT
13652 TYPE_DECLARED_CLASS (type) = dwarf2_flag_true_p (die, DW_AT_enum_class, cu);
13653
f792889a 13654 return set_die_type (die, type, cu);
134d01f1
DJ
13655}
13656
13657/* Given a pointer to a die which begins an enumeration, process all
13658 the dies that define the members of the enumeration, and create the
13659 symbol for the enumeration type.
13660
13661 NOTE: We reverse the order of the element list. */
13662
13663static void
13664process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
13665{
f792889a 13666 struct type *this_type;
134d01f1 13667
f792889a
DJ
13668 this_type = get_die_type (die, cu);
13669 if (this_type == NULL)
13670 this_type = read_enumeration_type (die, cu);
9dc481d3 13671
639d11d3 13672 if (die->child != NULL)
c906108c 13673 {
9dc481d3
DE
13674 struct die_info *child_die;
13675 struct symbol *sym;
13676 struct field *fields = NULL;
13677 int num_fields = 0;
15d034d0 13678 const char *name;
9dc481d3 13679
639d11d3 13680 child_die = die->child;
c906108c
SS
13681 while (child_die && child_die->tag)
13682 {
13683 if (child_die->tag != DW_TAG_enumerator)
13684 {
e7c27a73 13685 process_die (child_die, cu);
c906108c
SS
13686 }
13687 else
13688 {
39cbfefa
DJ
13689 name = dwarf2_name (child_die, cu);
13690 if (name)
c906108c 13691 {
f792889a 13692 sym = new_symbol (child_die, this_type, cu);
c906108c
SS
13693
13694 if ((num_fields % DW_FIELD_ALLOC_CHUNK) == 0)
13695 {
13696 fields = (struct field *)
13697 xrealloc (fields,
13698 (num_fields + DW_FIELD_ALLOC_CHUNK)
c5aa993b 13699 * sizeof (struct field));
c906108c
SS
13700 }
13701
3567439c 13702 FIELD_NAME (fields[num_fields]) = SYMBOL_LINKAGE_NAME (sym);
c906108c 13703 FIELD_TYPE (fields[num_fields]) = NULL;
14e75d8e 13704 SET_FIELD_ENUMVAL (fields[num_fields], SYMBOL_VALUE (sym));
c906108c
SS
13705 FIELD_BITSIZE (fields[num_fields]) = 0;
13706
13707 num_fields++;
13708 }
13709 }
13710
13711 child_die = sibling_die (child_die);
13712 }
13713
13714 if (num_fields)
13715 {
f792889a
DJ
13716 TYPE_NFIELDS (this_type) = num_fields;
13717 TYPE_FIELDS (this_type) = (struct field *)
13718 TYPE_ALLOC (this_type, sizeof (struct field) * num_fields);
13719 memcpy (TYPE_FIELDS (this_type), fields,
c906108c 13720 sizeof (struct field) * num_fields);
b8c9b27d 13721 xfree (fields);
c906108c 13722 }
c906108c 13723 }
134d01f1 13724
6c83ed52
TT
13725 /* If we are reading an enum from a .debug_types unit, and the enum
13726 is a declaration, and the enum is not the signatured type in the
13727 unit, then we do not want to add a symbol for it. Adding a
13728 symbol would in some cases obscure the true definition of the
13729 enum, giving users an incomplete type when the definition is
13730 actually available. Note that we do not want to do this for all
13731 enums which are just declarations, because C++0x allows forward
13732 enum declarations. */
3019eac3 13733 if (cu->per_cu->is_debug_types
6c83ed52
TT
13734 && die_is_declaration (die, cu))
13735 {
52dc124a 13736 struct signatured_type *sig_type;
6c83ed52 13737
c0f78cd4 13738 sig_type = (struct signatured_type *) cu->per_cu;
3019eac3
DE
13739 gdb_assert (sig_type->type_offset_in_section.sect_off != 0);
13740 if (sig_type->type_offset_in_section.sect_off != die->offset.sect_off)
6c83ed52
TT
13741 return;
13742 }
13743
f792889a 13744 new_symbol (die, this_type, cu);
c906108c
SS
13745}
13746
13747/* Extract all information from a DW_TAG_array_type DIE and put it in
13748 the DIE's type field. For now, this only handles one dimensional
13749 arrays. */
13750
f792889a 13751static struct type *
e7c27a73 13752read_array_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 13753{
e7c27a73 13754 struct objfile *objfile = cu->objfile;
c906108c 13755 struct die_info *child_die;
7e314c57 13756 struct type *type;
c906108c
SS
13757 struct type *element_type, *range_type, *index_type;
13758 struct type **range_types = NULL;
13759 struct attribute *attr;
13760 int ndim = 0;
13761 struct cleanup *back_to;
15d034d0 13762 const char *name;
dc53a7ad 13763 unsigned int bit_stride = 0;
c906108c 13764
e7c27a73 13765 element_type = die_type (die, cu);
c906108c 13766
7e314c57
JK
13767 /* The die_type call above may have already set the type for this DIE. */
13768 type = get_die_type (die, cu);
13769 if (type)
13770 return type;
13771
dc53a7ad
JB
13772 attr = dwarf2_attr (die, DW_AT_byte_stride, cu);
13773 if (attr != NULL)
13774 bit_stride = DW_UNSND (attr) * 8;
13775
13776 attr = dwarf2_attr (die, DW_AT_bit_stride, cu);
13777 if (attr != NULL)
13778 bit_stride = DW_UNSND (attr);
13779
c906108c
SS
13780 /* Irix 6.2 native cc creates array types without children for
13781 arrays with unspecified length. */
639d11d3 13782 if (die->child == NULL)
c906108c 13783 {
46bf5051 13784 index_type = objfile_type (objfile)->builtin_int;
0c9c3474 13785 range_type = create_static_range_type (NULL, index_type, 0, -1);
dc53a7ad
JB
13786 type = create_array_type_with_stride (NULL, element_type, range_type,
13787 bit_stride);
f792889a 13788 return set_die_type (die, type, cu);
c906108c
SS
13789 }
13790
13791 back_to = make_cleanup (null_cleanup, NULL);
639d11d3 13792 child_die = die->child;
c906108c
SS
13793 while (child_die && child_die->tag)
13794 {
13795 if (child_die->tag == DW_TAG_subrange_type)
13796 {
f792889a 13797 struct type *child_type = read_type_die (child_die, cu);
9a619af0 13798
f792889a 13799 if (child_type != NULL)
a02abb62 13800 {
0963b4bd
MS
13801 /* The range type was succesfully read. Save it for the
13802 array type creation. */
a02abb62
JB
13803 if ((ndim % DW_FIELD_ALLOC_CHUNK) == 0)
13804 {
13805 range_types = (struct type **)
13806 xrealloc (range_types, (ndim + DW_FIELD_ALLOC_CHUNK)
13807 * sizeof (struct type *));
13808 if (ndim == 0)
13809 make_cleanup (free_current_contents, &range_types);
13810 }
f792889a 13811 range_types[ndim++] = child_type;
a02abb62 13812 }
c906108c
SS
13813 }
13814 child_die = sibling_die (child_die);
13815 }
13816
13817 /* Dwarf2 dimensions are output from left to right, create the
13818 necessary array types in backwards order. */
7ca2d3a3 13819
c906108c 13820 type = element_type;
7ca2d3a3
DL
13821
13822 if (read_array_order (die, cu) == DW_ORD_col_major)
13823 {
13824 int i = 0;
9a619af0 13825
7ca2d3a3 13826 while (i < ndim)
dc53a7ad
JB
13827 type = create_array_type_with_stride (NULL, type, range_types[i++],
13828 bit_stride);
7ca2d3a3
DL
13829 }
13830 else
13831 {
13832 while (ndim-- > 0)
dc53a7ad
JB
13833 type = create_array_type_with_stride (NULL, type, range_types[ndim],
13834 bit_stride);
7ca2d3a3 13835 }
c906108c 13836
f5f8a009
EZ
13837 /* Understand Dwarf2 support for vector types (like they occur on
13838 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
13839 array type. This is not part of the Dwarf2/3 standard yet, but a
13840 custom vendor extension. The main difference between a regular
13841 array and the vector variant is that vectors are passed by value
13842 to functions. */
e142c38c 13843 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
f5f8a009 13844 if (attr)
ea37ba09 13845 make_vector_type (type);
f5f8a009 13846
dbc98a8b
KW
13847 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
13848 implementation may choose to implement triple vectors using this
13849 attribute. */
13850 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
13851 if (attr)
13852 {
13853 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
13854 TYPE_LENGTH (type) = DW_UNSND (attr);
13855 else
3e43a32a
MS
13856 complaint (&symfile_complaints,
13857 _("DW_AT_byte_size for array type smaller "
13858 "than the total size of elements"));
dbc98a8b
KW
13859 }
13860
39cbfefa
DJ
13861 name = dwarf2_name (die, cu);
13862 if (name)
13863 TYPE_NAME (type) = name;
6e70227d 13864
0963b4bd 13865 /* Install the type in the die. */
7e314c57
JK
13866 set_die_type (die, type, cu);
13867
13868 /* set_die_type should be already done. */
b4ba55a1
JB
13869 set_descriptive_type (type, die, cu);
13870
c906108c
SS
13871 do_cleanups (back_to);
13872
7e314c57 13873 return type;
c906108c
SS
13874}
13875
7ca2d3a3 13876static enum dwarf_array_dim_ordering
6e70227d 13877read_array_order (struct die_info *die, struct dwarf2_cu *cu)
7ca2d3a3
DL
13878{
13879 struct attribute *attr;
13880
13881 attr = dwarf2_attr (die, DW_AT_ordering, cu);
13882
aead7601
SM
13883 if (attr)
13884 return (enum dwarf_array_dim_ordering) DW_SND (attr);
7ca2d3a3 13885
0963b4bd
MS
13886 /* GNU F77 is a special case, as at 08/2004 array type info is the
13887 opposite order to the dwarf2 specification, but data is still
13888 laid out as per normal fortran.
7ca2d3a3 13889
0963b4bd
MS
13890 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
13891 version checking. */
7ca2d3a3 13892
905e0470
PM
13893 if (cu->language == language_fortran
13894 && cu->producer && strstr (cu->producer, "GNU F77"))
7ca2d3a3
DL
13895 {
13896 return DW_ORD_row_major;
13897 }
13898
6e70227d 13899 switch (cu->language_defn->la_array_ordering)
7ca2d3a3
DL
13900 {
13901 case array_column_major:
13902 return DW_ORD_col_major;
13903 case array_row_major:
13904 default:
13905 return DW_ORD_row_major;
13906 };
13907}
13908
72019c9c 13909/* Extract all information from a DW_TAG_set_type DIE and put it in
0963b4bd 13910 the DIE's type field. */
72019c9c 13911
f792889a 13912static struct type *
72019c9c
GM
13913read_set_type (struct die_info *die, struct dwarf2_cu *cu)
13914{
7e314c57
JK
13915 struct type *domain_type, *set_type;
13916 struct attribute *attr;
f792889a 13917
7e314c57
JK
13918 domain_type = die_type (die, cu);
13919
13920 /* The die_type call above may have already set the type for this DIE. */
13921 set_type = get_die_type (die, cu);
13922 if (set_type)
13923 return set_type;
13924
13925 set_type = create_set_type (NULL, domain_type);
13926
13927 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
d09039dd
PM
13928 if (attr)
13929 TYPE_LENGTH (set_type) = DW_UNSND (attr);
7e314c57 13930
f792889a 13931 return set_die_type (die, set_type, cu);
72019c9c 13932}
7ca2d3a3 13933
0971de02
TT
13934/* A helper for read_common_block that creates a locexpr baton.
13935 SYM is the symbol which we are marking as computed.
13936 COMMON_DIE is the DIE for the common block.
13937 COMMON_LOC is the location expression attribute for the common
13938 block itself.
13939 MEMBER_LOC is the location expression attribute for the particular
13940 member of the common block that we are processing.
13941 CU is the CU from which the above come. */
13942
13943static void
13944mark_common_block_symbol_computed (struct symbol *sym,
13945 struct die_info *common_die,
13946 struct attribute *common_loc,
13947 struct attribute *member_loc,
13948 struct dwarf2_cu *cu)
13949{
13950 struct objfile *objfile = dwarf2_per_objfile->objfile;
13951 struct dwarf2_locexpr_baton *baton;
13952 gdb_byte *ptr;
13953 unsigned int cu_off;
13954 enum bfd_endian byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
13955 LONGEST offset = 0;
13956
13957 gdb_assert (common_loc && member_loc);
13958 gdb_assert (attr_form_is_block (common_loc));
13959 gdb_assert (attr_form_is_block (member_loc)
13960 || attr_form_is_constant (member_loc));
13961
8d749320 13962 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
0971de02
TT
13963 baton->per_cu = cu->per_cu;
13964 gdb_assert (baton->per_cu);
13965
13966 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
13967
13968 if (attr_form_is_constant (member_loc))
13969 {
13970 offset = dwarf2_get_attr_constant_value (member_loc, 0);
13971 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
13972 }
13973 else
13974 baton->size += DW_BLOCK (member_loc)->size;
13975
224c3ddb 13976 ptr = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, baton->size);
0971de02
TT
13977 baton->data = ptr;
13978
13979 *ptr++ = DW_OP_call4;
13980 cu_off = common_die->offset.sect_off - cu->per_cu->offset.sect_off;
13981 store_unsigned_integer (ptr, 4, byte_order, cu_off);
13982 ptr += 4;
13983
13984 if (attr_form_is_constant (member_loc))
13985 {
13986 *ptr++ = DW_OP_addr;
13987 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
13988 ptr += cu->header.addr_size;
13989 }
13990 else
13991 {
13992 /* We have to copy the data here, because DW_OP_call4 will only
13993 use a DW_AT_location attribute. */
13994 memcpy (ptr, DW_BLOCK (member_loc)->data, DW_BLOCK (member_loc)->size);
13995 ptr += DW_BLOCK (member_loc)->size;
13996 }
13997
13998 *ptr++ = DW_OP_plus;
13999 gdb_assert (ptr - baton->data == baton->size);
14000
0971de02 14001 SYMBOL_LOCATION_BATON (sym) = baton;
f1e6e072 14002 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
0971de02
TT
14003}
14004
4357ac6c
TT
14005/* Create appropriate locally-scoped variables for all the
14006 DW_TAG_common_block entries. Also create a struct common_block
14007 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
14008 is used to sepate the common blocks name namespace from regular
14009 variable names. */
c906108c
SS
14010
14011static void
e7c27a73 14012read_common_block (struct die_info *die, struct dwarf2_cu *cu)
c906108c 14013{
0971de02
TT
14014 struct attribute *attr;
14015
14016 attr = dwarf2_attr (die, DW_AT_location, cu);
14017 if (attr)
14018 {
14019 /* Support the .debug_loc offsets. */
14020 if (attr_form_is_block (attr))
14021 {
14022 /* Ok. */
14023 }
14024 else if (attr_form_is_section_offset (attr))
14025 {
14026 dwarf2_complex_location_expr_complaint ();
14027 attr = NULL;
14028 }
14029 else
14030 {
14031 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
14032 "common block member");
14033 attr = NULL;
14034 }
14035 }
14036
639d11d3 14037 if (die->child != NULL)
c906108c 14038 {
4357ac6c
TT
14039 struct objfile *objfile = cu->objfile;
14040 struct die_info *child_die;
14041 size_t n_entries = 0, size;
14042 struct common_block *common_block;
14043 struct symbol *sym;
74ac6d43 14044
4357ac6c
TT
14045 for (child_die = die->child;
14046 child_die && child_die->tag;
14047 child_die = sibling_die (child_die))
14048 ++n_entries;
14049
14050 size = (sizeof (struct common_block)
14051 + (n_entries - 1) * sizeof (struct symbol *));
224c3ddb
SM
14052 common_block
14053 = (struct common_block *) obstack_alloc (&objfile->objfile_obstack,
14054 size);
4357ac6c
TT
14055 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
14056 common_block->n_entries = 0;
14057
14058 for (child_die = die->child;
14059 child_die && child_die->tag;
14060 child_die = sibling_die (child_die))
14061 {
14062 /* Create the symbol in the DW_TAG_common_block block in the current
14063 symbol scope. */
e7c27a73 14064 sym = new_symbol (child_die, NULL, cu);
0971de02
TT
14065 if (sym != NULL)
14066 {
14067 struct attribute *member_loc;
14068
14069 common_block->contents[common_block->n_entries++] = sym;
14070
14071 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
14072 cu);
14073 if (member_loc)
14074 {
14075 /* GDB has handled this for a long time, but it is
14076 not specified by DWARF. It seems to have been
14077 emitted by gfortran at least as recently as:
14078 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
14079 complaint (&symfile_complaints,
14080 _("Variable in common block has "
14081 "DW_AT_data_member_location "
14082 "- DIE at 0x%x [in module %s]"),
4262abfb
JK
14083 child_die->offset.sect_off,
14084 objfile_name (cu->objfile));
0971de02
TT
14085
14086 if (attr_form_is_section_offset (member_loc))
14087 dwarf2_complex_location_expr_complaint ();
14088 else if (attr_form_is_constant (member_loc)
14089 || attr_form_is_block (member_loc))
14090 {
14091 if (attr)
14092 mark_common_block_symbol_computed (sym, die, attr,
14093 member_loc, cu);
14094 }
14095 else
14096 dwarf2_complex_location_expr_complaint ();
14097 }
14098 }
c906108c 14099 }
4357ac6c
TT
14100
14101 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
14102 SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
c906108c
SS
14103 }
14104}
14105
0114d602 14106/* Create a type for a C++ namespace. */
d9fa45fe 14107
0114d602
DJ
14108static struct type *
14109read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
d9fa45fe 14110{
e7c27a73 14111 struct objfile *objfile = cu->objfile;
0114d602 14112 const char *previous_prefix, *name;
9219021c 14113 int is_anonymous;
0114d602
DJ
14114 struct type *type;
14115
14116 /* For extensions, reuse the type of the original namespace. */
14117 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
14118 {
14119 struct die_info *ext_die;
14120 struct dwarf2_cu *ext_cu = cu;
9a619af0 14121
0114d602
DJ
14122 ext_die = dwarf2_extension (die, &ext_cu);
14123 type = read_type_die (ext_die, ext_cu);
9dc481d3
DE
14124
14125 /* EXT_CU may not be the same as CU.
02142a6c 14126 Ensure TYPE is recorded with CU in die_type_hash. */
0114d602
DJ
14127 return set_die_type (die, type, cu);
14128 }
9219021c 14129
e142c38c 14130 name = namespace_name (die, &is_anonymous, cu);
9219021c
DC
14131
14132 /* Now build the name of the current namespace. */
14133
0114d602
DJ
14134 previous_prefix = determine_prefix (die, cu);
14135 if (previous_prefix[0] != '\0')
14136 name = typename_concat (&objfile->objfile_obstack,
f55ee35c 14137 previous_prefix, name, 0, cu);
0114d602
DJ
14138
14139 /* Create the type. */
19f392bc 14140 type = init_type (objfile, TYPE_CODE_NAMESPACE, 0, name);
0114d602
DJ
14141 TYPE_TAG_NAME (type) = TYPE_NAME (type);
14142
60531b24 14143 return set_die_type (die, type, cu);
0114d602
DJ
14144}
14145
22cee43f 14146/* Read a namespace scope. */
0114d602
DJ
14147
14148static void
14149read_namespace (struct die_info *die, struct dwarf2_cu *cu)
14150{
14151 struct objfile *objfile = cu->objfile;
0114d602 14152 int is_anonymous;
9219021c 14153
5c4e30ca
DC
14154 /* Add a symbol associated to this if we haven't seen the namespace
14155 before. Also, add a using directive if it's an anonymous
14156 namespace. */
9219021c 14157
f2f0e013 14158 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
5c4e30ca
DC
14159 {
14160 struct type *type;
14161
0114d602 14162 type = read_type_die (die, cu);
e7c27a73 14163 new_symbol (die, type, cu);
5c4e30ca 14164
e8e80198 14165 namespace_name (die, &is_anonymous, cu);
5c4e30ca 14166 if (is_anonymous)
0114d602
DJ
14167 {
14168 const char *previous_prefix = determine_prefix (die, cu);
9a619af0 14169
22cee43f
PMR
14170 add_using_directive (using_directives (cu->language),
14171 previous_prefix, TYPE_NAME (type), NULL,
14172 NULL, NULL, 0, &objfile->objfile_obstack);
0114d602 14173 }
5c4e30ca 14174 }
9219021c 14175
639d11d3 14176 if (die->child != NULL)
d9fa45fe 14177 {
639d11d3 14178 struct die_info *child_die = die->child;
6e70227d 14179
d9fa45fe
DC
14180 while (child_die && child_die->tag)
14181 {
e7c27a73 14182 process_die (child_die, cu);
d9fa45fe
DC
14183 child_die = sibling_die (child_die);
14184 }
14185 }
38d518c9
EZ
14186}
14187
f55ee35c
JK
14188/* Read a Fortran module as type. This DIE can be only a declaration used for
14189 imported module. Still we need that type as local Fortran "use ... only"
14190 declaration imports depend on the created type in determine_prefix. */
14191
14192static struct type *
14193read_module_type (struct die_info *die, struct dwarf2_cu *cu)
14194{
14195 struct objfile *objfile = cu->objfile;
15d034d0 14196 const char *module_name;
f55ee35c
JK
14197 struct type *type;
14198
14199 module_name = dwarf2_name (die, cu);
14200 if (!module_name)
3e43a32a
MS
14201 complaint (&symfile_complaints,
14202 _("DW_TAG_module has no name, offset 0x%x"),
b64f50a1 14203 die->offset.sect_off);
19f392bc 14204 type = init_type (objfile, TYPE_CODE_MODULE, 0, module_name);
f55ee35c
JK
14205
14206 /* determine_prefix uses TYPE_TAG_NAME. */
14207 TYPE_TAG_NAME (type) = TYPE_NAME (type);
14208
14209 return set_die_type (die, type, cu);
14210}
14211
5d7cb8df
JK
14212/* Read a Fortran module. */
14213
14214static void
14215read_module (struct die_info *die, struct dwarf2_cu *cu)
14216{
14217 struct die_info *child_die = die->child;
530e8392
KB
14218 struct type *type;
14219
14220 type = read_type_die (die, cu);
14221 new_symbol (die, type, cu);
5d7cb8df 14222
5d7cb8df
JK
14223 while (child_die && child_die->tag)
14224 {
14225 process_die (child_die, cu);
14226 child_die = sibling_die (child_die);
14227 }
14228}
14229
38d518c9
EZ
14230/* Return the name of the namespace represented by DIE. Set
14231 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
14232 namespace. */
14233
14234static const char *
e142c38c 14235namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
38d518c9
EZ
14236{
14237 struct die_info *current_die;
14238 const char *name = NULL;
14239
14240 /* Loop through the extensions until we find a name. */
14241
14242 for (current_die = die;
14243 current_die != NULL;
f2f0e013 14244 current_die = dwarf2_extension (die, &cu))
38d518c9 14245 {
96553a0c
DE
14246 /* We don't use dwarf2_name here so that we can detect the absence
14247 of a name -> anonymous namespace. */
7d45c7c3 14248 name = dwarf2_string_attr (die, DW_AT_name, cu);
96553a0c 14249
38d518c9
EZ
14250 if (name != NULL)
14251 break;
14252 }
14253
14254 /* Is it an anonymous namespace? */
14255
14256 *is_anonymous = (name == NULL);
14257 if (*is_anonymous)
2b1dbab0 14258 name = CP_ANONYMOUS_NAMESPACE_STR;
38d518c9
EZ
14259
14260 return name;
d9fa45fe
DC
14261}
14262
c906108c
SS
14263/* Extract all information from a DW_TAG_pointer_type DIE and add to
14264 the user defined type vector. */
14265
f792889a 14266static struct type *
e7c27a73 14267read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 14268{
5e2b427d 14269 struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
e7c27a73 14270 struct comp_unit_head *cu_header = &cu->header;
c906108c 14271 struct type *type;
8b2dbe47
KB
14272 struct attribute *attr_byte_size;
14273 struct attribute *attr_address_class;
14274 int byte_size, addr_class;
7e314c57
JK
14275 struct type *target_type;
14276
14277 target_type = die_type (die, cu);
c906108c 14278
7e314c57
JK
14279 /* The die_type call above may have already set the type for this DIE. */
14280 type = get_die_type (die, cu);
14281 if (type)
14282 return type;
14283
14284 type = lookup_pointer_type (target_type);
8b2dbe47 14285
e142c38c 14286 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
8b2dbe47
KB
14287 if (attr_byte_size)
14288 byte_size = DW_UNSND (attr_byte_size);
c906108c 14289 else
8b2dbe47
KB
14290 byte_size = cu_header->addr_size;
14291
e142c38c 14292 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
8b2dbe47
KB
14293 if (attr_address_class)
14294 addr_class = DW_UNSND (attr_address_class);
14295 else
14296 addr_class = DW_ADDR_none;
14297
14298 /* If the pointer size or address class is different than the
14299 default, create a type variant marked as such and set the
14300 length accordingly. */
14301 if (TYPE_LENGTH (type) != byte_size || addr_class != DW_ADDR_none)
c906108c 14302 {
5e2b427d 14303 if (gdbarch_address_class_type_flags_p (gdbarch))
8b2dbe47
KB
14304 {
14305 int type_flags;
14306
849957d9 14307 type_flags = gdbarch_address_class_type_flags
5e2b427d 14308 (gdbarch, byte_size, addr_class);
876cecd0
TT
14309 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
14310 == 0);
8b2dbe47
KB
14311 type = make_type_with_address_space (type, type_flags);
14312 }
14313 else if (TYPE_LENGTH (type) != byte_size)
14314 {
3e43a32a
MS
14315 complaint (&symfile_complaints,
14316 _("invalid pointer size %d"), byte_size);
8b2dbe47 14317 }
6e70227d 14318 else
9a619af0
MS
14319 {
14320 /* Should we also complain about unhandled address classes? */
14321 }
c906108c 14322 }
8b2dbe47
KB
14323
14324 TYPE_LENGTH (type) = byte_size;
f792889a 14325 return set_die_type (die, type, cu);
c906108c
SS
14326}
14327
14328/* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
14329 the user defined type vector. */
14330
f792889a 14331static struct type *
e7c27a73 14332read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
14333{
14334 struct type *type;
14335 struct type *to_type;
14336 struct type *domain;
14337
e7c27a73
DJ
14338 to_type = die_type (die, cu);
14339 domain = die_containing_type (die, cu);
0d5de010 14340
7e314c57
JK
14341 /* The calls above may have already set the type for this DIE. */
14342 type = get_die_type (die, cu);
14343 if (type)
14344 return type;
14345
0d5de010
DJ
14346 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
14347 type = lookup_methodptr_type (to_type);
7078baeb
TT
14348 else if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_FUNC)
14349 {
14350 struct type *new_type = alloc_type (cu->objfile);
14351
14352 smash_to_method_type (new_type, domain, TYPE_TARGET_TYPE (to_type),
14353 TYPE_FIELDS (to_type), TYPE_NFIELDS (to_type),
14354 TYPE_VARARGS (to_type));
14355 type = lookup_methodptr_type (new_type);
14356 }
0d5de010
DJ
14357 else
14358 type = lookup_memberptr_type (to_type, domain);
c906108c 14359
f792889a 14360 return set_die_type (die, type, cu);
c906108c
SS
14361}
14362
14363/* Extract all information from a DW_TAG_reference_type DIE and add to
14364 the user defined type vector. */
14365
f792889a 14366static struct type *
e7c27a73 14367read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 14368{
e7c27a73 14369 struct comp_unit_head *cu_header = &cu->header;
7e314c57 14370 struct type *type, *target_type;
c906108c
SS
14371 struct attribute *attr;
14372
7e314c57
JK
14373 target_type = die_type (die, cu);
14374
14375 /* The die_type call above may have already set the type for this DIE. */
14376 type = get_die_type (die, cu);
14377 if (type)
14378 return type;
14379
14380 type = lookup_reference_type (target_type);
e142c38c 14381 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
14382 if (attr)
14383 {
14384 TYPE_LENGTH (type) = DW_UNSND (attr);
14385 }
14386 else
14387 {
107d2387 14388 TYPE_LENGTH (type) = cu_header->addr_size;
c906108c 14389 }
f792889a 14390 return set_die_type (die, type, cu);
c906108c
SS
14391}
14392
cf363f18
MW
14393/* Add the given cv-qualifiers to the element type of the array. GCC
14394 outputs DWARF type qualifiers that apply to an array, not the
14395 element type. But GDB relies on the array element type to carry
14396 the cv-qualifiers. This mimics section 6.7.3 of the C99
14397 specification. */
14398
14399static struct type *
14400add_array_cv_type (struct die_info *die, struct dwarf2_cu *cu,
14401 struct type *base_type, int cnst, int voltl)
14402{
14403 struct type *el_type, *inner_array;
14404
14405 base_type = copy_type (base_type);
14406 inner_array = base_type;
14407
14408 while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
14409 {
14410 TYPE_TARGET_TYPE (inner_array) =
14411 copy_type (TYPE_TARGET_TYPE (inner_array));
14412 inner_array = TYPE_TARGET_TYPE (inner_array);
14413 }
14414
14415 el_type = TYPE_TARGET_TYPE (inner_array);
14416 cnst |= TYPE_CONST (el_type);
14417 voltl |= TYPE_VOLATILE (el_type);
14418 TYPE_TARGET_TYPE (inner_array) = make_cv_type (cnst, voltl, el_type, NULL);
14419
14420 return set_die_type (die, base_type, cu);
14421}
14422
f792889a 14423static struct type *
e7c27a73 14424read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 14425{
f792889a 14426 struct type *base_type, *cv_type;
c906108c 14427
e7c27a73 14428 base_type = die_type (die, cu);
7e314c57
JK
14429
14430 /* The die_type call above may have already set the type for this DIE. */
14431 cv_type = get_die_type (die, cu);
14432 if (cv_type)
14433 return cv_type;
14434
2f608a3a
KW
14435 /* In case the const qualifier is applied to an array type, the element type
14436 is so qualified, not the array type (section 6.7.3 of C99). */
14437 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
cf363f18 14438 return add_array_cv_type (die, cu, base_type, 1, 0);
2f608a3a 14439
f792889a
DJ
14440 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
14441 return set_die_type (die, cv_type, cu);
c906108c
SS
14442}
14443
f792889a 14444static struct type *
e7c27a73 14445read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 14446{
f792889a 14447 struct type *base_type, *cv_type;
c906108c 14448
e7c27a73 14449 base_type = die_type (die, cu);
7e314c57
JK
14450
14451 /* The die_type call above may have already set the type for this DIE. */
14452 cv_type = get_die_type (die, cu);
14453 if (cv_type)
14454 return cv_type;
14455
cf363f18
MW
14456 /* In case the volatile qualifier is applied to an array type, the
14457 element type is so qualified, not the array type (section 6.7.3
14458 of C99). */
14459 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
14460 return add_array_cv_type (die, cu, base_type, 0, 1);
14461
f792889a
DJ
14462 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
14463 return set_die_type (die, cv_type, cu);
c906108c
SS
14464}
14465
06d66ee9
TT
14466/* Handle DW_TAG_restrict_type. */
14467
14468static struct type *
14469read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
14470{
14471 struct type *base_type, *cv_type;
14472
14473 base_type = die_type (die, cu);
14474
14475 /* The die_type call above may have already set the type for this DIE. */
14476 cv_type = get_die_type (die, cu);
14477 if (cv_type)
14478 return cv_type;
14479
14480 cv_type = make_restrict_type (base_type);
14481 return set_die_type (die, cv_type, cu);
14482}
14483
a2c2acaf
MW
14484/* Handle DW_TAG_atomic_type. */
14485
14486static struct type *
14487read_tag_atomic_type (struct die_info *die, struct dwarf2_cu *cu)
14488{
14489 struct type *base_type, *cv_type;
14490
14491 base_type = die_type (die, cu);
14492
14493 /* The die_type call above may have already set the type for this DIE. */
14494 cv_type = get_die_type (die, cu);
14495 if (cv_type)
14496 return cv_type;
14497
14498 cv_type = make_atomic_type (base_type);
14499 return set_die_type (die, cv_type, cu);
14500}
14501
c906108c
SS
14502/* Extract all information from a DW_TAG_string_type DIE and add to
14503 the user defined type vector. It isn't really a user defined type,
14504 but it behaves like one, with other DIE's using an AT_user_def_type
14505 attribute to reference it. */
14506
f792889a 14507static struct type *
e7c27a73 14508read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 14509{
e7c27a73 14510 struct objfile *objfile = cu->objfile;
3b7538c0 14511 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
14512 struct type *type, *range_type, *index_type, *char_type;
14513 struct attribute *attr;
14514 unsigned int length;
14515
e142c38c 14516 attr = dwarf2_attr (die, DW_AT_string_length, cu);
c906108c
SS
14517 if (attr)
14518 {
14519 length = DW_UNSND (attr);
14520 }
14521 else
14522 {
0963b4bd 14523 /* Check for the DW_AT_byte_size attribute. */
e142c38c 14524 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
b21b22e0
PS
14525 if (attr)
14526 {
14527 length = DW_UNSND (attr);
14528 }
14529 else
14530 {
14531 length = 1;
14532 }
c906108c 14533 }
6ccb9162 14534
46bf5051 14535 index_type = objfile_type (objfile)->builtin_int;
0c9c3474 14536 range_type = create_static_range_type (NULL, index_type, 1, length);
3b7538c0
UW
14537 char_type = language_string_char_type (cu->language_defn, gdbarch);
14538 type = create_string_type (NULL, char_type, range_type);
6ccb9162 14539
f792889a 14540 return set_die_type (die, type, cu);
c906108c
SS
14541}
14542
4d804846
JB
14543/* Assuming that DIE corresponds to a function, returns nonzero
14544 if the function is prototyped. */
14545
14546static int
14547prototyped_function_p (struct die_info *die, struct dwarf2_cu *cu)
14548{
14549 struct attribute *attr;
14550
14551 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
14552 if (attr && (DW_UNSND (attr) != 0))
14553 return 1;
14554
14555 /* The DWARF standard implies that the DW_AT_prototyped attribute
14556 is only meaninful for C, but the concept also extends to other
14557 languages that allow unprototyped functions (Eg: Objective C).
14558 For all other languages, assume that functions are always
14559 prototyped. */
14560 if (cu->language != language_c
14561 && cu->language != language_objc
14562 && cu->language != language_opencl)
14563 return 1;
14564
14565 /* RealView does not emit DW_AT_prototyped. We can not distinguish
14566 prototyped and unprototyped functions; default to prototyped,
14567 since that is more common in modern code (and RealView warns
14568 about unprototyped functions). */
14569 if (producer_is_realview (cu->producer))
14570 return 1;
14571
14572 return 0;
14573}
14574
c906108c
SS
14575/* Handle DIES due to C code like:
14576
14577 struct foo
c5aa993b
JM
14578 {
14579 int (*funcp)(int a, long l);
14580 int b;
14581 };
c906108c 14582
0963b4bd 14583 ('funcp' generates a DW_TAG_subroutine_type DIE). */
c906108c 14584
f792889a 14585static struct type *
e7c27a73 14586read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 14587{
bb5ed363 14588 struct objfile *objfile = cu->objfile;
0963b4bd
MS
14589 struct type *type; /* Type that this function returns. */
14590 struct type *ftype; /* Function that returns above type. */
c906108c
SS
14591 struct attribute *attr;
14592
e7c27a73 14593 type = die_type (die, cu);
7e314c57
JK
14594
14595 /* The die_type call above may have already set the type for this DIE. */
14596 ftype = get_die_type (die, cu);
14597 if (ftype)
14598 return ftype;
14599
0c8b41f1 14600 ftype = lookup_function_type (type);
c906108c 14601
4d804846 14602 if (prototyped_function_p (die, cu))
a6c727b2 14603 TYPE_PROTOTYPED (ftype) = 1;
c906108c 14604
c055b101
CV
14605 /* Store the calling convention in the type if it's available in
14606 the subroutine die. Otherwise set the calling convention to
14607 the default value DW_CC_normal. */
14608 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
54fcddd0
UW
14609 if (attr)
14610 TYPE_CALLING_CONVENTION (ftype) = DW_UNSND (attr);
14611 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
14612 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
14613 else
14614 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
76c10ea2 14615
743649fd
MW
14616 /* Record whether the function returns normally to its caller or not
14617 if the DWARF producer set that information. */
14618 attr = dwarf2_attr (die, DW_AT_noreturn, cu);
14619 if (attr && (DW_UNSND (attr) != 0))
14620 TYPE_NO_RETURN (ftype) = 1;
14621
76c10ea2
GM
14622 /* We need to add the subroutine type to the die immediately so
14623 we don't infinitely recurse when dealing with parameters
0963b4bd 14624 declared as the same subroutine type. */
76c10ea2 14625 set_die_type (die, ftype, cu);
6e70227d 14626
639d11d3 14627 if (die->child != NULL)
c906108c 14628 {
bb5ed363 14629 struct type *void_type = objfile_type (objfile)->builtin_void;
c906108c 14630 struct die_info *child_die;
8072405b 14631 int nparams, iparams;
c906108c
SS
14632
14633 /* Count the number of parameters.
14634 FIXME: GDB currently ignores vararg functions, but knows about
14635 vararg member functions. */
8072405b 14636 nparams = 0;
639d11d3 14637 child_die = die->child;
c906108c
SS
14638 while (child_die && child_die->tag)
14639 {
14640 if (child_die->tag == DW_TAG_formal_parameter)
14641 nparams++;
14642 else if (child_die->tag == DW_TAG_unspecified_parameters)
876cecd0 14643 TYPE_VARARGS (ftype) = 1;
c906108c
SS
14644 child_die = sibling_die (child_die);
14645 }
14646
14647 /* Allocate storage for parameters and fill them in. */
14648 TYPE_NFIELDS (ftype) = nparams;
14649 TYPE_FIELDS (ftype) = (struct field *)
ae5a43e0 14650 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
c906108c 14651
8072405b
JK
14652 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
14653 even if we error out during the parameters reading below. */
14654 for (iparams = 0; iparams < nparams; iparams++)
14655 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
14656
14657 iparams = 0;
639d11d3 14658 child_die = die->child;
c906108c
SS
14659 while (child_die && child_die->tag)
14660 {
14661 if (child_die->tag == DW_TAG_formal_parameter)
14662 {
3ce3b1ba
PA
14663 struct type *arg_type;
14664
14665 /* DWARF version 2 has no clean way to discern C++
14666 static and non-static member functions. G++ helps
14667 GDB by marking the first parameter for non-static
14668 member functions (which is the this pointer) as
14669 artificial. We pass this information to
14670 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
14671
14672 DWARF version 3 added DW_AT_object_pointer, which GCC
14673 4.5 does not yet generate. */
e142c38c 14674 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
c906108c
SS
14675 if (attr)
14676 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
14677 else
418835cc
KS
14678 {
14679 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
14680
14681 /* GCC/43521: In java, the formal parameter
14682 "this" is sometimes not marked with DW_AT_artificial. */
14683 if (cu->language == language_java)
14684 {
14685 const char *name = dwarf2_name (child_die, cu);
9a619af0 14686
418835cc
KS
14687 if (name && !strcmp (name, "this"))
14688 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 1;
14689 }
14690 }
3ce3b1ba
PA
14691 arg_type = die_type (child_die, cu);
14692
14693 /* RealView does not mark THIS as const, which the testsuite
14694 expects. GCC marks THIS as const in method definitions,
14695 but not in the class specifications (GCC PR 43053). */
14696 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
14697 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
14698 {
14699 int is_this = 0;
14700 struct dwarf2_cu *arg_cu = cu;
14701 const char *name = dwarf2_name (child_die, cu);
14702
14703 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
14704 if (attr)
14705 {
14706 /* If the compiler emits this, use it. */
14707 if (follow_die_ref (die, attr, &arg_cu) == child_die)
14708 is_this = 1;
14709 }
14710 else if (name && strcmp (name, "this") == 0)
14711 /* Function definitions will have the argument names. */
14712 is_this = 1;
14713 else if (name == NULL && iparams == 0)
14714 /* Declarations may not have the names, so like
14715 elsewhere in GDB, assume an artificial first
14716 argument is "this". */
14717 is_this = 1;
14718
14719 if (is_this)
14720 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
14721 arg_type, 0);
14722 }
14723
14724 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
c906108c
SS
14725 iparams++;
14726 }
14727 child_die = sibling_die (child_die);
14728 }
14729 }
14730
76c10ea2 14731 return ftype;
c906108c
SS
14732}
14733
f792889a 14734static struct type *
e7c27a73 14735read_typedef (struct die_info *die, struct dwarf2_cu *cu)
c906108c 14736{
e7c27a73 14737 struct objfile *objfile = cu->objfile;
0114d602 14738 const char *name = NULL;
3c8e0968 14739 struct type *this_type, *target_type;
c906108c 14740
94af9270 14741 name = dwarf2_full_name (NULL, die, cu);
19f392bc
UW
14742 this_type = init_type (objfile, TYPE_CODE_TYPEDEF, 0, name);
14743 TYPE_TARGET_STUB (this_type) = 1;
f792889a 14744 set_die_type (die, this_type, cu);
3c8e0968
DE
14745 target_type = die_type (die, cu);
14746 if (target_type != this_type)
14747 TYPE_TARGET_TYPE (this_type) = target_type;
14748 else
14749 {
14750 /* Self-referential typedefs are, it seems, not allowed by the DWARF
14751 spec and cause infinite loops in GDB. */
14752 complaint (&symfile_complaints,
14753 _("Self-referential DW_TAG_typedef "
14754 "- DIE at 0x%x [in module %s]"),
4262abfb 14755 die->offset.sect_off, objfile_name (objfile));
3c8e0968
DE
14756 TYPE_TARGET_TYPE (this_type) = NULL;
14757 }
f792889a 14758 return this_type;
c906108c
SS
14759}
14760
9b790ce7
UW
14761/* Allocate a floating-point type of size BITS and name NAME. Pass NAME_HINT
14762 (which may be different from NAME) to the architecture back-end to allow
14763 it to guess the correct format if necessary. */
14764
14765static struct type *
14766dwarf2_init_float_type (struct objfile *objfile, int bits, const char *name,
14767 const char *name_hint)
14768{
14769 struct gdbarch *gdbarch = get_objfile_arch (objfile);
14770 const struct floatformat **format;
14771 struct type *type;
14772
14773 format = gdbarch_floatformat_for_type (gdbarch, name_hint, bits);
14774 if (format)
14775 type = init_float_type (objfile, bits, name, format);
14776 else
14777 type = init_type (objfile, TYPE_CODE_ERROR, bits / TARGET_CHAR_BIT, name);
14778
14779 return type;
14780}
14781
c906108c
SS
14782/* Find a representation of a given base type and install
14783 it in the TYPE field of the die. */
14784
f792889a 14785static struct type *
e7c27a73 14786read_base_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 14787{
e7c27a73 14788 struct objfile *objfile = cu->objfile;
c906108c
SS
14789 struct type *type;
14790 struct attribute *attr;
19f392bc 14791 int encoding = 0, bits = 0;
15d034d0 14792 const char *name;
c906108c 14793
e142c38c 14794 attr = dwarf2_attr (die, DW_AT_encoding, cu);
c906108c
SS
14795 if (attr)
14796 {
14797 encoding = DW_UNSND (attr);
14798 }
e142c38c 14799 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
14800 if (attr)
14801 {
19f392bc 14802 bits = DW_UNSND (attr) * TARGET_CHAR_BIT;
c906108c 14803 }
39cbfefa 14804 name = dwarf2_name (die, cu);
6ccb9162 14805 if (!name)
c906108c 14806 {
6ccb9162
UW
14807 complaint (&symfile_complaints,
14808 _("DW_AT_name missing from DW_TAG_base_type"));
c906108c 14809 }
6ccb9162
UW
14810
14811 switch (encoding)
c906108c 14812 {
6ccb9162
UW
14813 case DW_ATE_address:
14814 /* Turn DW_ATE_address into a void * pointer. */
19f392bc
UW
14815 type = init_type (objfile, TYPE_CODE_VOID, 1, NULL);
14816 type = init_pointer_type (objfile, bits, name, type);
6ccb9162
UW
14817 break;
14818 case DW_ATE_boolean:
19f392bc 14819 type = init_boolean_type (objfile, bits, 1, name);
6ccb9162
UW
14820 break;
14821 case DW_ATE_complex_float:
9b790ce7 14822 type = dwarf2_init_float_type (objfile, bits / 2, NULL, name);
19f392bc 14823 type = init_complex_type (objfile, name, type);
6ccb9162
UW
14824 break;
14825 case DW_ATE_decimal_float:
19f392bc 14826 type = init_decfloat_type (objfile, bits, name);
6ccb9162
UW
14827 break;
14828 case DW_ATE_float:
9b790ce7 14829 type = dwarf2_init_float_type (objfile, bits, name, name);
6ccb9162
UW
14830 break;
14831 case DW_ATE_signed:
19f392bc 14832 type = init_integer_type (objfile, bits, 0, name);
6ccb9162
UW
14833 break;
14834 case DW_ATE_unsigned:
3b2b8fea
TT
14835 if (cu->language == language_fortran
14836 && name
61012eef 14837 && startswith (name, "character("))
19f392bc
UW
14838 type = init_character_type (objfile, bits, 1, name);
14839 else
14840 type = init_integer_type (objfile, bits, 1, name);
6ccb9162
UW
14841 break;
14842 case DW_ATE_signed_char:
6e70227d 14843 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea
TT
14844 || cu->language == language_pascal
14845 || cu->language == language_fortran)
19f392bc
UW
14846 type = init_character_type (objfile, bits, 0, name);
14847 else
14848 type = init_integer_type (objfile, bits, 0, name);
6ccb9162
UW
14849 break;
14850 case DW_ATE_unsigned_char:
868a0084 14851 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea 14852 || cu->language == language_pascal
c44af4eb
TT
14853 || cu->language == language_fortran
14854 || cu->language == language_rust)
19f392bc
UW
14855 type = init_character_type (objfile, bits, 1, name);
14856 else
14857 type = init_integer_type (objfile, bits, 1, name);
6ccb9162 14858 break;
75079b2b
TT
14859 case DW_ATE_UTF:
14860 /* We just treat this as an integer and then recognize the
14861 type by name elsewhere. */
19f392bc 14862 type = init_integer_type (objfile, bits, 0, name);
75079b2b
TT
14863 break;
14864
6ccb9162
UW
14865 default:
14866 complaint (&symfile_complaints, _("unsupported DW_AT_encoding: '%s'"),
14867 dwarf_type_encoding_name (encoding));
19f392bc
UW
14868 type = init_type (objfile, TYPE_CODE_ERROR,
14869 bits / TARGET_CHAR_BIT, name);
6ccb9162 14870 break;
c906108c 14871 }
6ccb9162 14872
0114d602 14873 if (name && strcmp (name, "char") == 0)
876cecd0 14874 TYPE_NOSIGN (type) = 1;
0114d602 14875
f792889a 14876 return set_die_type (die, type, cu);
c906108c
SS
14877}
14878
80180f79
SA
14879/* Parse dwarf attribute if it's a block, reference or constant and put the
14880 resulting value of the attribute into struct bound_prop.
14881 Returns 1 if ATTR could be resolved into PROP, 0 otherwise. */
14882
14883static int
14884attr_to_dynamic_prop (const struct attribute *attr, struct die_info *die,
14885 struct dwarf2_cu *cu, struct dynamic_prop *prop)
14886{
14887 struct dwarf2_property_baton *baton;
14888 struct obstack *obstack = &cu->objfile->objfile_obstack;
14889
14890 if (attr == NULL || prop == NULL)
14891 return 0;
14892
14893 if (attr_form_is_block (attr))
14894 {
8d749320 14895 baton = XOBNEW (obstack, struct dwarf2_property_baton);
80180f79
SA
14896 baton->referenced_type = NULL;
14897 baton->locexpr.per_cu = cu->per_cu;
14898 baton->locexpr.size = DW_BLOCK (attr)->size;
14899 baton->locexpr.data = DW_BLOCK (attr)->data;
14900 prop->data.baton = baton;
14901 prop->kind = PROP_LOCEXPR;
14902 gdb_assert (prop->data.baton != NULL);
14903 }
14904 else if (attr_form_is_ref (attr))
14905 {
14906 struct dwarf2_cu *target_cu = cu;
14907 struct die_info *target_die;
14908 struct attribute *target_attr;
14909
14910 target_die = follow_die_ref (die, attr, &target_cu);
14911 target_attr = dwarf2_attr (target_die, DW_AT_location, target_cu);
df25ebbd
JB
14912 if (target_attr == NULL)
14913 target_attr = dwarf2_attr (target_die, DW_AT_data_member_location,
14914 target_cu);
80180f79
SA
14915 if (target_attr == NULL)
14916 return 0;
14917
df25ebbd 14918 switch (target_attr->name)
80180f79 14919 {
df25ebbd
JB
14920 case DW_AT_location:
14921 if (attr_form_is_section_offset (target_attr))
14922 {
8d749320 14923 baton = XOBNEW (obstack, struct dwarf2_property_baton);
df25ebbd
JB
14924 baton->referenced_type = die_type (target_die, target_cu);
14925 fill_in_loclist_baton (cu, &baton->loclist, target_attr);
14926 prop->data.baton = baton;
14927 prop->kind = PROP_LOCLIST;
14928 gdb_assert (prop->data.baton != NULL);
14929 }
14930 else if (attr_form_is_block (target_attr))
14931 {
8d749320 14932 baton = XOBNEW (obstack, struct dwarf2_property_baton);
df25ebbd
JB
14933 baton->referenced_type = die_type (target_die, target_cu);
14934 baton->locexpr.per_cu = cu->per_cu;
14935 baton->locexpr.size = DW_BLOCK (target_attr)->size;
14936 baton->locexpr.data = DW_BLOCK (target_attr)->data;
14937 prop->data.baton = baton;
14938 prop->kind = PROP_LOCEXPR;
14939 gdb_assert (prop->data.baton != NULL);
14940 }
14941 else
14942 {
14943 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
14944 "dynamic property");
14945 return 0;
14946 }
14947 break;
14948 case DW_AT_data_member_location:
14949 {
14950 LONGEST offset;
14951
14952 if (!handle_data_member_location (target_die, target_cu,
14953 &offset))
14954 return 0;
14955
8d749320 14956 baton = XOBNEW (obstack, struct dwarf2_property_baton);
6ad395a7
JB
14957 baton->referenced_type = read_type_die (target_die->parent,
14958 target_cu);
df25ebbd
JB
14959 baton->offset_info.offset = offset;
14960 baton->offset_info.type = die_type (target_die, target_cu);
14961 prop->data.baton = baton;
14962 prop->kind = PROP_ADDR_OFFSET;
14963 break;
14964 }
80180f79
SA
14965 }
14966 }
14967 else if (attr_form_is_constant (attr))
14968 {
14969 prop->data.const_val = dwarf2_get_attr_constant_value (attr, 0);
14970 prop->kind = PROP_CONST;
14971 }
14972 else
14973 {
14974 dwarf2_invalid_attrib_class_complaint (dwarf_form_name (attr->form),
14975 dwarf2_name (die, cu));
14976 return 0;
14977 }
14978
14979 return 1;
14980}
14981
a02abb62
JB
14982/* Read the given DW_AT_subrange DIE. */
14983
f792889a 14984static struct type *
a02abb62
JB
14985read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
14986{
4c9ad8c2 14987 struct type *base_type, *orig_base_type;
a02abb62
JB
14988 struct type *range_type;
14989 struct attribute *attr;
729efb13 14990 struct dynamic_prop low, high;
4fae6e18 14991 int low_default_is_valid;
c451ebe5 14992 int high_bound_is_count = 0;
15d034d0 14993 const char *name;
43bbcdc2 14994 LONGEST negative_mask;
e77813c8 14995
4c9ad8c2
TT
14996 orig_base_type = die_type (die, cu);
14997 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
14998 whereas the real type might be. So, we use ORIG_BASE_TYPE when
14999 creating the range type, but we use the result of check_typedef
15000 when examining properties of the type. */
15001 base_type = check_typedef (orig_base_type);
a02abb62 15002
7e314c57
JK
15003 /* The die_type call above may have already set the type for this DIE. */
15004 range_type = get_die_type (die, cu);
15005 if (range_type)
15006 return range_type;
15007
729efb13
SA
15008 low.kind = PROP_CONST;
15009 high.kind = PROP_CONST;
15010 high.data.const_val = 0;
15011
4fae6e18
JK
15012 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
15013 omitting DW_AT_lower_bound. */
15014 switch (cu->language)
6e70227d 15015 {
4fae6e18
JK
15016 case language_c:
15017 case language_cplus:
729efb13 15018 low.data.const_val = 0;
4fae6e18
JK
15019 low_default_is_valid = 1;
15020 break;
15021 case language_fortran:
729efb13 15022 low.data.const_val = 1;
4fae6e18
JK
15023 low_default_is_valid = 1;
15024 break;
15025 case language_d:
15026 case language_java:
15027 case language_objc:
c44af4eb 15028 case language_rust:
729efb13 15029 low.data.const_val = 0;
4fae6e18
JK
15030 low_default_is_valid = (cu->header.version >= 4);
15031 break;
15032 case language_ada:
15033 case language_m2:
15034 case language_pascal:
729efb13 15035 low.data.const_val = 1;
4fae6e18
JK
15036 low_default_is_valid = (cu->header.version >= 4);
15037 break;
15038 default:
729efb13 15039 low.data.const_val = 0;
4fae6e18
JK
15040 low_default_is_valid = 0;
15041 break;
a02abb62
JB
15042 }
15043
e142c38c 15044 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
a02abb62 15045 if (attr)
11c1ba78 15046 attr_to_dynamic_prop (attr, die, cu, &low);
4fae6e18
JK
15047 else if (!low_default_is_valid)
15048 complaint (&symfile_complaints, _("Missing DW_AT_lower_bound "
15049 "- DIE at 0x%x [in module %s]"),
4262abfb 15050 die->offset.sect_off, objfile_name (cu->objfile));
a02abb62 15051
e142c38c 15052 attr = dwarf2_attr (die, DW_AT_upper_bound, cu);
80180f79 15053 if (!attr_to_dynamic_prop (attr, die, cu, &high))
e77813c8
PM
15054 {
15055 attr = dwarf2_attr (die, DW_AT_count, cu);
c451ebe5 15056 if (attr_to_dynamic_prop (attr, die, cu, &high))
6b662e19 15057 {
c451ebe5
SA
15058 /* If bounds are constant do the final calculation here. */
15059 if (low.kind == PROP_CONST && high.kind == PROP_CONST)
15060 high.data.const_val = low.data.const_val + high.data.const_val - 1;
15061 else
15062 high_bound_is_count = 1;
c2ff108b 15063 }
e77813c8
PM
15064 }
15065
15066 /* Dwarf-2 specifications explicitly allows to create subrange types
15067 without specifying a base type.
15068 In that case, the base type must be set to the type of
15069 the lower bound, upper bound or count, in that order, if any of these
15070 three attributes references an object that has a type.
15071 If no base type is found, the Dwarf-2 specifications say that
15072 a signed integer type of size equal to the size of an address should
15073 be used.
15074 For the following C code: `extern char gdb_int [];'
15075 GCC produces an empty range DIE.
15076 FIXME: muller/2010-05-28: Possible references to object for low bound,
0963b4bd 15077 high bound or count are not yet handled by this code. */
e77813c8
PM
15078 if (TYPE_CODE (base_type) == TYPE_CODE_VOID)
15079 {
15080 struct objfile *objfile = cu->objfile;
15081 struct gdbarch *gdbarch = get_objfile_arch (objfile);
15082 int addr_size = gdbarch_addr_bit (gdbarch) /8;
15083 struct type *int_type = objfile_type (objfile)->builtin_int;
15084
15085 /* Test "int", "long int", and "long long int" objfile types,
15086 and select the first one having a size above or equal to the
15087 architecture address size. */
15088 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
15089 base_type = int_type;
15090 else
15091 {
15092 int_type = objfile_type (objfile)->builtin_long;
15093 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
15094 base_type = int_type;
15095 else
15096 {
15097 int_type = objfile_type (objfile)->builtin_long_long;
15098 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
15099 base_type = int_type;
15100 }
15101 }
15102 }
a02abb62 15103
dbb9c2b1
JB
15104 /* Normally, the DWARF producers are expected to use a signed
15105 constant form (Eg. DW_FORM_sdata) to express negative bounds.
15106 But this is unfortunately not always the case, as witnessed
15107 with GCC, for instance, where the ambiguous DW_FORM_dataN form
15108 is used instead. To work around that ambiguity, we treat
15109 the bounds as signed, and thus sign-extend their values, when
15110 the base type is signed. */
6e70227d 15111 negative_mask =
66c6502d 15112 -((LONGEST) 1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1));
729efb13
SA
15113 if (low.kind == PROP_CONST
15114 && !TYPE_UNSIGNED (base_type) && (low.data.const_val & negative_mask))
15115 low.data.const_val |= negative_mask;
15116 if (high.kind == PROP_CONST
15117 && !TYPE_UNSIGNED (base_type) && (high.data.const_val & negative_mask))
15118 high.data.const_val |= negative_mask;
43bbcdc2 15119
729efb13 15120 range_type = create_range_type (NULL, orig_base_type, &low, &high);
a02abb62 15121
c451ebe5
SA
15122 if (high_bound_is_count)
15123 TYPE_RANGE_DATA (range_type)->flag_upper_bound_is_count = 1;
15124
c2ff108b
JK
15125 /* Ada expects an empty array on no boundary attributes. */
15126 if (attr == NULL && cu->language != language_ada)
729efb13 15127 TYPE_HIGH_BOUND_KIND (range_type) = PROP_UNDEFINED;
c2ff108b 15128
39cbfefa
DJ
15129 name = dwarf2_name (die, cu);
15130 if (name)
15131 TYPE_NAME (range_type) = name;
6e70227d 15132
e142c38c 15133 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
a02abb62
JB
15134 if (attr)
15135 TYPE_LENGTH (range_type) = DW_UNSND (attr);
15136
7e314c57
JK
15137 set_die_type (die, range_type, cu);
15138
15139 /* set_die_type should be already done. */
b4ba55a1
JB
15140 set_descriptive_type (range_type, die, cu);
15141
7e314c57 15142 return range_type;
a02abb62 15143}
6e70227d 15144
f792889a 15145static struct type *
81a17f79
JB
15146read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
15147{
15148 struct type *type;
81a17f79 15149
81a17f79
JB
15150 /* For now, we only support the C meaning of an unspecified type: void. */
15151
19f392bc 15152 type = init_type (cu->objfile, TYPE_CODE_VOID, 0, NULL);
0114d602 15153 TYPE_NAME (type) = dwarf2_name (die, cu);
81a17f79 15154
f792889a 15155 return set_die_type (die, type, cu);
81a17f79 15156}
a02abb62 15157
639d11d3
DC
15158/* Read a single die and all its descendents. Set the die's sibling
15159 field to NULL; set other fields in the die correctly, and set all
15160 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
15161 location of the info_ptr after reading all of those dies. PARENT
15162 is the parent of the die in question. */
15163
15164static struct die_info *
dee91e82 15165read_die_and_children (const struct die_reader_specs *reader,
d521ce57
TT
15166 const gdb_byte *info_ptr,
15167 const gdb_byte **new_info_ptr,
dee91e82 15168 struct die_info *parent)
639d11d3
DC
15169{
15170 struct die_info *die;
d521ce57 15171 const gdb_byte *cur_ptr;
639d11d3
DC
15172 int has_children;
15173
bf6af496 15174 cur_ptr = read_full_die_1 (reader, &die, info_ptr, &has_children, 0);
1d325ec1
DJ
15175 if (die == NULL)
15176 {
15177 *new_info_ptr = cur_ptr;
15178 return NULL;
15179 }
93311388 15180 store_in_ref_table (die, reader->cu);
639d11d3
DC
15181
15182 if (has_children)
bf6af496 15183 die->child = read_die_and_siblings_1 (reader, cur_ptr, new_info_ptr, die);
639d11d3
DC
15184 else
15185 {
15186 die->child = NULL;
15187 *new_info_ptr = cur_ptr;
15188 }
15189
15190 die->sibling = NULL;
15191 die->parent = parent;
15192 return die;
15193}
15194
15195/* Read a die, all of its descendents, and all of its siblings; set
15196 all of the fields of all of the dies correctly. Arguments are as
15197 in read_die_and_children. */
15198
15199static struct die_info *
bf6af496 15200read_die_and_siblings_1 (const struct die_reader_specs *reader,
d521ce57
TT
15201 const gdb_byte *info_ptr,
15202 const gdb_byte **new_info_ptr,
bf6af496 15203 struct die_info *parent)
639d11d3
DC
15204{
15205 struct die_info *first_die, *last_sibling;
d521ce57 15206 const gdb_byte *cur_ptr;
639d11d3 15207
c906108c 15208 cur_ptr = info_ptr;
639d11d3
DC
15209 first_die = last_sibling = NULL;
15210
15211 while (1)
c906108c 15212 {
639d11d3 15213 struct die_info *die
dee91e82 15214 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
639d11d3 15215
1d325ec1 15216 if (die == NULL)
c906108c 15217 {
639d11d3
DC
15218 *new_info_ptr = cur_ptr;
15219 return first_die;
c906108c 15220 }
1d325ec1
DJ
15221
15222 if (!first_die)
15223 first_die = die;
c906108c 15224 else
1d325ec1
DJ
15225 last_sibling->sibling = die;
15226
15227 last_sibling = die;
c906108c 15228 }
c906108c
SS
15229}
15230
bf6af496
DE
15231/* Read a die, all of its descendents, and all of its siblings; set
15232 all of the fields of all of the dies correctly. Arguments are as
15233 in read_die_and_children.
15234 This the main entry point for reading a DIE and all its children. */
15235
15236static struct die_info *
15237read_die_and_siblings (const struct die_reader_specs *reader,
d521ce57
TT
15238 const gdb_byte *info_ptr,
15239 const gdb_byte **new_info_ptr,
bf6af496
DE
15240 struct die_info *parent)
15241{
15242 struct die_info *die = read_die_and_siblings_1 (reader, info_ptr,
15243 new_info_ptr, parent);
15244
b4f54984 15245 if (dwarf_die_debug)
bf6af496
DE
15246 {
15247 fprintf_unfiltered (gdb_stdlog,
15248 "Read die from %s@0x%x of %s:\n",
a32a8923 15249 get_section_name (reader->die_section),
bf6af496
DE
15250 (unsigned) (info_ptr - reader->die_section->buffer),
15251 bfd_get_filename (reader->abfd));
b4f54984 15252 dump_die (die, dwarf_die_debug);
bf6af496
DE
15253 }
15254
15255 return die;
15256}
15257
3019eac3
DE
15258/* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
15259 attributes.
15260 The caller is responsible for filling in the extra attributes
15261 and updating (*DIEP)->num_attrs.
15262 Set DIEP to point to a newly allocated die with its information,
15263 except for its child, sibling, and parent fields.
15264 Set HAS_CHILDREN to tell whether the die has children or not. */
93311388 15265
d521ce57 15266static const gdb_byte *
3019eac3 15267read_full_die_1 (const struct die_reader_specs *reader,
d521ce57 15268 struct die_info **diep, const gdb_byte *info_ptr,
3019eac3 15269 int *has_children, int num_extra_attrs)
93311388 15270{
b64f50a1
JK
15271 unsigned int abbrev_number, bytes_read, i;
15272 sect_offset offset;
93311388
DE
15273 struct abbrev_info *abbrev;
15274 struct die_info *die;
15275 struct dwarf2_cu *cu = reader->cu;
15276 bfd *abfd = reader->abfd;
15277
b64f50a1 15278 offset.sect_off = info_ptr - reader->buffer;
93311388
DE
15279 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
15280 info_ptr += bytes_read;
15281 if (!abbrev_number)
15282 {
15283 *diep = NULL;
15284 *has_children = 0;
15285 return info_ptr;
15286 }
15287
433df2d4 15288 abbrev = abbrev_table_lookup_abbrev (cu->abbrev_table, abbrev_number);
93311388 15289 if (!abbrev)
348e048f
DE
15290 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
15291 abbrev_number,
15292 bfd_get_filename (abfd));
15293
3019eac3 15294 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
93311388
DE
15295 die->offset = offset;
15296 die->tag = abbrev->tag;
15297 die->abbrev = abbrev_number;
15298
3019eac3
DE
15299 /* Make the result usable.
15300 The caller needs to update num_attrs after adding the extra
15301 attributes. */
93311388
DE
15302 die->num_attrs = abbrev->num_attrs;
15303
15304 for (i = 0; i < abbrev->num_attrs; ++i)
dee91e82
DE
15305 info_ptr = read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
15306 info_ptr);
93311388
DE
15307
15308 *diep = die;
15309 *has_children = abbrev->has_children;
15310 return info_ptr;
15311}
15312
3019eac3
DE
15313/* Read a die and all its attributes.
15314 Set DIEP to point to a newly allocated die with its information,
15315 except for its child, sibling, and parent fields.
15316 Set HAS_CHILDREN to tell whether the die has children or not. */
15317
d521ce57 15318static const gdb_byte *
3019eac3 15319read_full_die (const struct die_reader_specs *reader,
d521ce57 15320 struct die_info **diep, const gdb_byte *info_ptr,
3019eac3
DE
15321 int *has_children)
15322{
d521ce57 15323 const gdb_byte *result;
bf6af496
DE
15324
15325 result = read_full_die_1 (reader, diep, info_ptr, has_children, 0);
15326
b4f54984 15327 if (dwarf_die_debug)
bf6af496
DE
15328 {
15329 fprintf_unfiltered (gdb_stdlog,
15330 "Read die from %s@0x%x of %s:\n",
a32a8923 15331 get_section_name (reader->die_section),
bf6af496
DE
15332 (unsigned) (info_ptr - reader->die_section->buffer),
15333 bfd_get_filename (reader->abfd));
b4f54984 15334 dump_die (*diep, dwarf_die_debug);
bf6af496
DE
15335 }
15336
15337 return result;
3019eac3 15338}
433df2d4
DE
15339\f
15340/* Abbreviation tables.
3019eac3 15341
433df2d4 15342 In DWARF version 2, the description of the debugging information is
c906108c
SS
15343 stored in a separate .debug_abbrev section. Before we read any
15344 dies from a section we read in all abbreviations and install them
433df2d4
DE
15345 in a hash table. */
15346
15347/* Allocate space for a struct abbrev_info object in ABBREV_TABLE. */
15348
15349static struct abbrev_info *
15350abbrev_table_alloc_abbrev (struct abbrev_table *abbrev_table)
15351{
15352 struct abbrev_info *abbrev;
15353
8d749320 15354 abbrev = XOBNEW (&abbrev_table->abbrev_obstack, struct abbrev_info);
433df2d4 15355 memset (abbrev, 0, sizeof (struct abbrev_info));
8d749320 15356
433df2d4
DE
15357 return abbrev;
15358}
15359
15360/* Add an abbreviation to the table. */
c906108c
SS
15361
15362static void
433df2d4
DE
15363abbrev_table_add_abbrev (struct abbrev_table *abbrev_table,
15364 unsigned int abbrev_number,
15365 struct abbrev_info *abbrev)
15366{
15367 unsigned int hash_number;
15368
15369 hash_number = abbrev_number % ABBREV_HASH_SIZE;
15370 abbrev->next = abbrev_table->abbrevs[hash_number];
15371 abbrev_table->abbrevs[hash_number] = abbrev;
15372}
dee91e82 15373
433df2d4
DE
15374/* Look up an abbrev in the table.
15375 Returns NULL if the abbrev is not found. */
15376
15377static struct abbrev_info *
15378abbrev_table_lookup_abbrev (const struct abbrev_table *abbrev_table,
15379 unsigned int abbrev_number)
c906108c 15380{
433df2d4
DE
15381 unsigned int hash_number;
15382 struct abbrev_info *abbrev;
15383
15384 hash_number = abbrev_number % ABBREV_HASH_SIZE;
15385 abbrev = abbrev_table->abbrevs[hash_number];
15386
15387 while (abbrev)
15388 {
15389 if (abbrev->number == abbrev_number)
15390 return abbrev;
15391 abbrev = abbrev->next;
15392 }
15393 return NULL;
15394}
15395
15396/* Read in an abbrev table. */
15397
15398static struct abbrev_table *
15399abbrev_table_read_table (struct dwarf2_section_info *section,
15400 sect_offset offset)
15401{
15402 struct objfile *objfile = dwarf2_per_objfile->objfile;
a32a8923 15403 bfd *abfd = get_section_bfd_owner (section);
433df2d4 15404 struct abbrev_table *abbrev_table;
d521ce57 15405 const gdb_byte *abbrev_ptr;
c906108c
SS
15406 struct abbrev_info *cur_abbrev;
15407 unsigned int abbrev_number, bytes_read, abbrev_name;
433df2d4 15408 unsigned int abbrev_form;
f3dd6933
DJ
15409 struct attr_abbrev *cur_attrs;
15410 unsigned int allocated_attrs;
c906108c 15411
70ba0933 15412 abbrev_table = XNEW (struct abbrev_table);
f4dc4d17 15413 abbrev_table->offset = offset;
433df2d4 15414 obstack_init (&abbrev_table->abbrev_obstack);
8d749320
SM
15415 abbrev_table->abbrevs =
15416 XOBNEWVEC (&abbrev_table->abbrev_obstack, struct abbrev_info *,
15417 ABBREV_HASH_SIZE);
433df2d4
DE
15418 memset (abbrev_table->abbrevs, 0,
15419 ABBREV_HASH_SIZE * sizeof (struct abbrev_info *));
c906108c 15420
433df2d4
DE
15421 dwarf2_read_section (objfile, section);
15422 abbrev_ptr = section->buffer + offset.sect_off;
c906108c
SS
15423 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
15424 abbrev_ptr += bytes_read;
15425
f3dd6933 15426 allocated_attrs = ATTR_ALLOC_CHUNK;
8d749320 15427 cur_attrs = XNEWVEC (struct attr_abbrev, allocated_attrs);
6e70227d 15428
0963b4bd 15429 /* Loop until we reach an abbrev number of 0. */
c906108c
SS
15430 while (abbrev_number)
15431 {
433df2d4 15432 cur_abbrev = abbrev_table_alloc_abbrev (abbrev_table);
c906108c
SS
15433
15434 /* read in abbrev header */
15435 cur_abbrev->number = abbrev_number;
aead7601
SM
15436 cur_abbrev->tag
15437 = (enum dwarf_tag) read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
c906108c
SS
15438 abbrev_ptr += bytes_read;
15439 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
15440 abbrev_ptr += 1;
15441
15442 /* now read in declarations */
15443 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
15444 abbrev_ptr += bytes_read;
15445 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
15446 abbrev_ptr += bytes_read;
15447 while (abbrev_name)
15448 {
f3dd6933 15449 if (cur_abbrev->num_attrs == allocated_attrs)
c906108c 15450 {
f3dd6933
DJ
15451 allocated_attrs += ATTR_ALLOC_CHUNK;
15452 cur_attrs
224c3ddb 15453 = XRESIZEVEC (struct attr_abbrev, cur_attrs, allocated_attrs);
c906108c 15454 }
ae038cb0 15455
aead7601
SM
15456 cur_attrs[cur_abbrev->num_attrs].name
15457 = (enum dwarf_attribute) abbrev_name;
15458 cur_attrs[cur_abbrev->num_attrs++].form
15459 = (enum dwarf_form) abbrev_form;
c906108c
SS
15460 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
15461 abbrev_ptr += bytes_read;
15462 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
15463 abbrev_ptr += bytes_read;
15464 }
15465
8d749320
SM
15466 cur_abbrev->attrs =
15467 XOBNEWVEC (&abbrev_table->abbrev_obstack, struct attr_abbrev,
15468 cur_abbrev->num_attrs);
f3dd6933
DJ
15469 memcpy (cur_abbrev->attrs, cur_attrs,
15470 cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
15471
433df2d4 15472 abbrev_table_add_abbrev (abbrev_table, abbrev_number, cur_abbrev);
c906108c
SS
15473
15474 /* Get next abbreviation.
15475 Under Irix6 the abbreviations for a compilation unit are not
c5aa993b
JM
15476 always properly terminated with an abbrev number of 0.
15477 Exit loop if we encounter an abbreviation which we have
15478 already read (which means we are about to read the abbreviations
15479 for the next compile unit) or if the end of the abbreviation
15480 table is reached. */
433df2d4 15481 if ((unsigned int) (abbrev_ptr - section->buffer) >= section->size)
c906108c
SS
15482 break;
15483 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
15484 abbrev_ptr += bytes_read;
433df2d4 15485 if (abbrev_table_lookup_abbrev (abbrev_table, abbrev_number) != NULL)
c906108c
SS
15486 break;
15487 }
f3dd6933
DJ
15488
15489 xfree (cur_attrs);
433df2d4 15490 return abbrev_table;
c906108c
SS
15491}
15492
433df2d4 15493/* Free the resources held by ABBREV_TABLE. */
c906108c 15494
c906108c 15495static void
433df2d4 15496abbrev_table_free (struct abbrev_table *abbrev_table)
c906108c 15497{
433df2d4
DE
15498 obstack_free (&abbrev_table->abbrev_obstack, NULL);
15499 xfree (abbrev_table);
c906108c
SS
15500}
15501
f4dc4d17
DE
15502/* Same as abbrev_table_free but as a cleanup.
15503 We pass in a pointer to the pointer to the table so that we can
15504 set the pointer to NULL when we're done. It also simplifies
73051182 15505 build_type_psymtabs_1. */
f4dc4d17
DE
15506
15507static void
15508abbrev_table_free_cleanup (void *table_ptr)
15509{
9a3c8263 15510 struct abbrev_table **abbrev_table_ptr = (struct abbrev_table **) table_ptr;
f4dc4d17
DE
15511
15512 if (*abbrev_table_ptr != NULL)
15513 abbrev_table_free (*abbrev_table_ptr);
15514 *abbrev_table_ptr = NULL;
15515}
15516
433df2d4
DE
15517/* Read the abbrev table for CU from ABBREV_SECTION. */
15518
15519static void
15520dwarf2_read_abbrevs (struct dwarf2_cu *cu,
15521 struct dwarf2_section_info *abbrev_section)
c906108c 15522{
433df2d4
DE
15523 cu->abbrev_table =
15524 abbrev_table_read_table (abbrev_section, cu->header.abbrev_offset);
15525}
c906108c 15526
433df2d4 15527/* Release the memory used by the abbrev table for a compilation unit. */
c906108c 15528
433df2d4
DE
15529static void
15530dwarf2_free_abbrev_table (void *ptr_to_cu)
15531{
9a3c8263 15532 struct dwarf2_cu *cu = (struct dwarf2_cu *) ptr_to_cu;
c906108c 15533
a2ce51a0
DE
15534 if (cu->abbrev_table != NULL)
15535 abbrev_table_free (cu->abbrev_table);
433df2d4
DE
15536 /* Set this to NULL so that we SEGV if we try to read it later,
15537 and also because free_comp_unit verifies this is NULL. */
15538 cu->abbrev_table = NULL;
15539}
15540\f
72bf9492
DJ
15541/* Returns nonzero if TAG represents a type that we might generate a partial
15542 symbol for. */
15543
15544static int
15545is_type_tag_for_partial (int tag)
15546{
15547 switch (tag)
15548 {
15549#if 0
15550 /* Some types that would be reasonable to generate partial symbols for,
15551 that we don't at present. */
15552 case DW_TAG_array_type:
15553 case DW_TAG_file_type:
15554 case DW_TAG_ptr_to_member_type:
15555 case DW_TAG_set_type:
15556 case DW_TAG_string_type:
15557 case DW_TAG_subroutine_type:
15558#endif
15559 case DW_TAG_base_type:
15560 case DW_TAG_class_type:
680b30c7 15561 case DW_TAG_interface_type:
72bf9492
DJ
15562 case DW_TAG_enumeration_type:
15563 case DW_TAG_structure_type:
15564 case DW_TAG_subrange_type:
15565 case DW_TAG_typedef:
15566 case DW_TAG_union_type:
15567 return 1;
15568 default:
15569 return 0;
15570 }
15571}
15572
15573/* Load all DIEs that are interesting for partial symbols into memory. */
15574
15575static struct partial_die_info *
dee91e82 15576load_partial_dies (const struct die_reader_specs *reader,
d521ce57 15577 const gdb_byte *info_ptr, int building_psymtab)
72bf9492 15578{
dee91e82 15579 struct dwarf2_cu *cu = reader->cu;
bb5ed363 15580 struct objfile *objfile = cu->objfile;
72bf9492
DJ
15581 struct partial_die_info *part_die;
15582 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
15583 struct abbrev_info *abbrev;
15584 unsigned int bytes_read;
5afb4e99 15585 unsigned int load_all = 0;
72bf9492
DJ
15586 int nesting_level = 1;
15587
15588 parent_die = NULL;
15589 last_die = NULL;
15590
7adf1e79
DE
15591 gdb_assert (cu->per_cu != NULL);
15592 if (cu->per_cu->load_all_dies)
5afb4e99
DJ
15593 load_all = 1;
15594
72bf9492
DJ
15595 cu->partial_dies
15596 = htab_create_alloc_ex (cu->header.length / 12,
15597 partial_die_hash,
15598 partial_die_eq,
15599 NULL,
15600 &cu->comp_unit_obstack,
15601 hashtab_obstack_allocate,
15602 dummy_obstack_deallocate);
15603
8d749320 15604 part_die = XOBNEW (&cu->comp_unit_obstack, struct partial_die_info);
72bf9492
DJ
15605
15606 while (1)
15607 {
15608 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
15609
15610 /* A NULL abbrev means the end of a series of children. */
15611 if (abbrev == NULL)
15612 {
15613 if (--nesting_level == 0)
15614 {
15615 /* PART_DIE was probably the last thing allocated on the
15616 comp_unit_obstack, so we could call obstack_free
15617 here. We don't do that because the waste is small,
15618 and will be cleaned up when we're done with this
15619 compilation unit. This way, we're also more robust
15620 against other users of the comp_unit_obstack. */
15621 return first_die;
15622 }
15623 info_ptr += bytes_read;
15624 last_die = parent_die;
15625 parent_die = parent_die->die_parent;
15626 continue;
15627 }
15628
98bfdba5
PA
15629 /* Check for template arguments. We never save these; if
15630 they're seen, we just mark the parent, and go on our way. */
15631 if (parent_die != NULL
15632 && cu->language == language_cplus
15633 && (abbrev->tag == DW_TAG_template_type_param
15634 || abbrev->tag == DW_TAG_template_value_param))
15635 {
15636 parent_die->has_template_arguments = 1;
15637
15638 if (!load_all)
15639 {
15640 /* We don't need a partial DIE for the template argument. */
dee91e82 15641 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
98bfdba5
PA
15642 continue;
15643 }
15644 }
15645
0d99eb77 15646 /* We only recurse into c++ subprograms looking for template arguments.
98bfdba5
PA
15647 Skip their other children. */
15648 if (!load_all
15649 && cu->language == language_cplus
15650 && parent_die != NULL
15651 && parent_die->tag == DW_TAG_subprogram)
15652 {
dee91e82 15653 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
98bfdba5
PA
15654 continue;
15655 }
15656
5afb4e99
DJ
15657 /* Check whether this DIE is interesting enough to save. Normally
15658 we would not be interested in members here, but there may be
15659 later variables referencing them via DW_AT_specification (for
15660 static members). */
15661 if (!load_all
15662 && !is_type_tag_for_partial (abbrev->tag)
72929c62 15663 && abbrev->tag != DW_TAG_constant
72bf9492
DJ
15664 && abbrev->tag != DW_TAG_enumerator
15665 && abbrev->tag != DW_TAG_subprogram
bc30ff58 15666 && abbrev->tag != DW_TAG_lexical_block
72bf9492 15667 && abbrev->tag != DW_TAG_variable
5afb4e99 15668 && abbrev->tag != DW_TAG_namespace
f55ee35c 15669 && abbrev->tag != DW_TAG_module
95554aad 15670 && abbrev->tag != DW_TAG_member
74921315
KS
15671 && abbrev->tag != DW_TAG_imported_unit
15672 && abbrev->tag != DW_TAG_imported_declaration)
72bf9492
DJ
15673 {
15674 /* Otherwise we skip to the next sibling, if any. */
dee91e82 15675 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
72bf9492
DJ
15676 continue;
15677 }
15678
dee91e82
DE
15679 info_ptr = read_partial_die (reader, part_die, abbrev, bytes_read,
15680 info_ptr);
72bf9492
DJ
15681
15682 /* This two-pass algorithm for processing partial symbols has a
15683 high cost in cache pressure. Thus, handle some simple cases
15684 here which cover the majority of C partial symbols. DIEs
15685 which neither have specification tags in them, nor could have
15686 specification tags elsewhere pointing at them, can simply be
15687 processed and discarded.
15688
15689 This segment is also optional; scan_partial_symbols and
15690 add_partial_symbol will handle these DIEs if we chain
15691 them in normally. When compilers which do not emit large
15692 quantities of duplicate debug information are more common,
15693 this code can probably be removed. */
15694
15695 /* Any complete simple types at the top level (pretty much all
15696 of them, for a language without namespaces), can be processed
15697 directly. */
15698 if (parent_die == NULL
15699 && part_die->has_specification == 0
15700 && part_die->is_declaration == 0
d8228535 15701 && ((part_die->tag == DW_TAG_typedef && !part_die->has_children)
72bf9492
DJ
15702 || part_die->tag == DW_TAG_base_type
15703 || part_die->tag == DW_TAG_subrange_type))
15704 {
15705 if (building_psymtab && part_die->name != NULL)
04a679b8 15706 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
72bf9492 15707 VAR_DOMAIN, LOC_TYPEDEF,
bb5ed363 15708 &objfile->static_psymbols,
1762568f 15709 0, cu->language, objfile);
dee91e82 15710 info_ptr = locate_pdi_sibling (reader, part_die, info_ptr);
72bf9492
DJ
15711 continue;
15712 }
15713
d8228535
JK
15714 /* The exception for DW_TAG_typedef with has_children above is
15715 a workaround of GCC PR debug/47510. In the case of this complaint
15716 type_name_no_tag_or_error will error on such types later.
15717
15718 GDB skipped children of DW_TAG_typedef by the shortcut above and then
15719 it could not find the child DIEs referenced later, this is checked
15720 above. In correct DWARF DW_TAG_typedef should have no children. */
15721
15722 if (part_die->tag == DW_TAG_typedef && part_die->has_children)
15723 complaint (&symfile_complaints,
15724 _("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
15725 "- DIE at 0x%x [in module %s]"),
4262abfb 15726 part_die->offset.sect_off, objfile_name (objfile));
d8228535 15727
72bf9492
DJ
15728 /* If we're at the second level, and we're an enumerator, and
15729 our parent has no specification (meaning possibly lives in a
15730 namespace elsewhere), then we can add the partial symbol now
15731 instead of queueing it. */
15732 if (part_die->tag == DW_TAG_enumerator
15733 && parent_die != NULL
15734 && parent_die->die_parent == NULL
15735 && parent_die->tag == DW_TAG_enumeration_type
15736 && parent_die->has_specification == 0)
15737 {
15738 if (part_die->name == NULL)
3e43a32a
MS
15739 complaint (&symfile_complaints,
15740 _("malformed enumerator DIE ignored"));
72bf9492 15741 else if (building_psymtab)
04a679b8 15742 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
72bf9492 15743 VAR_DOMAIN, LOC_CONST,
987504bb
JJ
15744 (cu->language == language_cplus
15745 || cu->language == language_java)
bb5ed363
DE
15746 ? &objfile->global_psymbols
15747 : &objfile->static_psymbols,
1762568f 15748 0, cu->language, objfile);
72bf9492 15749
dee91e82 15750 info_ptr = locate_pdi_sibling (reader, part_die, info_ptr);
72bf9492
DJ
15751 continue;
15752 }
15753
15754 /* We'll save this DIE so link it in. */
15755 part_die->die_parent = parent_die;
15756 part_die->die_sibling = NULL;
15757 part_die->die_child = NULL;
15758
15759 if (last_die && last_die == parent_die)
15760 last_die->die_child = part_die;
15761 else if (last_die)
15762 last_die->die_sibling = part_die;
15763
15764 last_die = part_die;
15765
15766 if (first_die == NULL)
15767 first_die = part_die;
15768
15769 /* Maybe add the DIE to the hash table. Not all DIEs that we
15770 find interesting need to be in the hash table, because we
15771 also have the parent/sibling/child chains; only those that we
15772 might refer to by offset later during partial symbol reading.
15773
15774 For now this means things that might have be the target of a
15775 DW_AT_specification, DW_AT_abstract_origin, or
15776 DW_AT_extension. DW_AT_extension will refer only to
15777 namespaces; DW_AT_abstract_origin refers to functions (and
15778 many things under the function DIE, but we do not recurse
15779 into function DIEs during partial symbol reading) and
15780 possibly variables as well; DW_AT_specification refers to
15781 declarations. Declarations ought to have the DW_AT_declaration
15782 flag. It happens that GCC forgets to put it in sometimes, but
15783 only for functions, not for types.
15784
15785 Adding more things than necessary to the hash table is harmless
15786 except for the performance cost. Adding too few will result in
5afb4e99
DJ
15787 wasted time in find_partial_die, when we reread the compilation
15788 unit with load_all_dies set. */
72bf9492 15789
5afb4e99 15790 if (load_all
72929c62 15791 || abbrev->tag == DW_TAG_constant
5afb4e99 15792 || abbrev->tag == DW_TAG_subprogram
72bf9492
DJ
15793 || abbrev->tag == DW_TAG_variable
15794 || abbrev->tag == DW_TAG_namespace
15795 || part_die->is_declaration)
15796 {
15797 void **slot;
15798
15799 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
b64f50a1 15800 part_die->offset.sect_off, INSERT);
72bf9492
DJ
15801 *slot = part_die;
15802 }
15803
8d749320 15804 part_die = XOBNEW (&cu->comp_unit_obstack, struct partial_die_info);
72bf9492
DJ
15805
15806 /* For some DIEs we want to follow their children (if any). For C
bc30ff58 15807 we have no reason to follow the children of structures; for other
98bfdba5
PA
15808 languages we have to, so that we can get at method physnames
15809 to infer fully qualified class names, for DW_AT_specification,
15810 and for C++ template arguments. For C++, we also look one level
15811 inside functions to find template arguments (if the name of the
15812 function does not already contain the template arguments).
bc30ff58
JB
15813
15814 For Ada, we need to scan the children of subprograms and lexical
15815 blocks as well because Ada allows the definition of nested
15816 entities that could be interesting for the debugger, such as
15817 nested subprograms for instance. */
72bf9492 15818 if (last_die->has_children
5afb4e99
DJ
15819 && (load_all
15820 || last_die->tag == DW_TAG_namespace
f55ee35c 15821 || last_die->tag == DW_TAG_module
72bf9492 15822 || last_die->tag == DW_TAG_enumeration_type
98bfdba5
PA
15823 || (cu->language == language_cplus
15824 && last_die->tag == DW_TAG_subprogram
15825 && (last_die->name == NULL
15826 || strchr (last_die->name, '<') == NULL))
72bf9492
DJ
15827 || (cu->language != language_c
15828 && (last_die->tag == DW_TAG_class_type
680b30c7 15829 || last_die->tag == DW_TAG_interface_type
72bf9492 15830 || last_die->tag == DW_TAG_structure_type
bc30ff58
JB
15831 || last_die->tag == DW_TAG_union_type))
15832 || (cu->language == language_ada
15833 && (last_die->tag == DW_TAG_subprogram
15834 || last_die->tag == DW_TAG_lexical_block))))
72bf9492
DJ
15835 {
15836 nesting_level++;
15837 parent_die = last_die;
15838 continue;
15839 }
15840
15841 /* Otherwise we skip to the next sibling, if any. */
dee91e82 15842 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
72bf9492
DJ
15843
15844 /* Back to the top, do it again. */
15845 }
15846}
15847
c906108c
SS
15848/* Read a minimal amount of information into the minimal die structure. */
15849
d521ce57 15850static const gdb_byte *
dee91e82
DE
15851read_partial_die (const struct die_reader_specs *reader,
15852 struct partial_die_info *part_die,
15853 struct abbrev_info *abbrev, unsigned int abbrev_len,
d521ce57 15854 const gdb_byte *info_ptr)
c906108c 15855{
dee91e82 15856 struct dwarf2_cu *cu = reader->cu;
bb5ed363 15857 struct objfile *objfile = cu->objfile;
d521ce57 15858 const gdb_byte *buffer = reader->buffer;
fa238c03 15859 unsigned int i;
c906108c 15860 struct attribute attr;
c5aa993b 15861 int has_low_pc_attr = 0;
c906108c 15862 int has_high_pc_attr = 0;
91da1414 15863 int high_pc_relative = 0;
c906108c 15864
72bf9492 15865 memset (part_die, 0, sizeof (struct partial_die_info));
c906108c 15866
b64f50a1 15867 part_die->offset.sect_off = info_ptr - buffer;
72bf9492
DJ
15868
15869 info_ptr += abbrev_len;
15870
15871 if (abbrev == NULL)
15872 return info_ptr;
15873
c906108c
SS
15874 part_die->tag = abbrev->tag;
15875 part_die->has_children = abbrev->has_children;
c906108c
SS
15876
15877 for (i = 0; i < abbrev->num_attrs; ++i)
15878 {
dee91e82 15879 info_ptr = read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
c906108c
SS
15880
15881 /* Store the data if it is of an attribute we want to keep in a
c5aa993b 15882 partial symbol table. */
c906108c
SS
15883 switch (attr.name)
15884 {
15885 case DW_AT_name:
71c25dea
TT
15886 switch (part_die->tag)
15887 {
15888 case DW_TAG_compile_unit:
95554aad 15889 case DW_TAG_partial_unit:
348e048f 15890 case DW_TAG_type_unit:
71c25dea
TT
15891 /* Compilation units have a DW_AT_name that is a filename, not
15892 a source language identifier. */
15893 case DW_TAG_enumeration_type:
15894 case DW_TAG_enumerator:
15895 /* These tags always have simple identifiers already; no need
15896 to canonicalize them. */
15897 part_die->name = DW_STRING (&attr);
15898 break;
15899 default:
15900 part_die->name
15901 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
34a68019 15902 &objfile->per_bfd->storage_obstack);
71c25dea
TT
15903 break;
15904 }
c906108c 15905 break;
31ef98ae 15906 case DW_AT_linkage_name:
c906108c 15907 case DW_AT_MIPS_linkage_name:
31ef98ae
TT
15908 /* Note that both forms of linkage name might appear. We
15909 assume they will be the same, and we only store the last
15910 one we see. */
94af9270
KS
15911 if (cu->language == language_ada)
15912 part_die->name = DW_STRING (&attr);
abc72ce4 15913 part_die->linkage_name = DW_STRING (&attr);
c906108c
SS
15914 break;
15915 case DW_AT_low_pc:
15916 has_low_pc_attr = 1;
31aa7e4e 15917 part_die->lowpc = attr_value_as_address (&attr);
c906108c
SS
15918 break;
15919 case DW_AT_high_pc:
15920 has_high_pc_attr = 1;
31aa7e4e
JB
15921 part_die->highpc = attr_value_as_address (&attr);
15922 if (cu->header.version >= 4 && attr_form_is_constant (&attr))
15923 high_pc_relative = 1;
c906108c
SS
15924 break;
15925 case DW_AT_location:
0963b4bd 15926 /* Support the .debug_loc offsets. */
8e19ed76
PS
15927 if (attr_form_is_block (&attr))
15928 {
95554aad 15929 part_die->d.locdesc = DW_BLOCK (&attr);
8e19ed76 15930 }
3690dd37 15931 else if (attr_form_is_section_offset (&attr))
8e19ed76 15932 {
4d3c2250 15933 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
15934 }
15935 else
15936 {
4d3c2250
KB
15937 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
15938 "partial symbol information");
8e19ed76 15939 }
c906108c 15940 break;
c906108c
SS
15941 case DW_AT_external:
15942 part_die->is_external = DW_UNSND (&attr);
15943 break;
15944 case DW_AT_declaration:
15945 part_die->is_declaration = DW_UNSND (&attr);
15946 break;
15947 case DW_AT_type:
15948 part_die->has_type = 1;
15949 break;
15950 case DW_AT_abstract_origin:
15951 case DW_AT_specification:
72bf9492
DJ
15952 case DW_AT_extension:
15953 part_die->has_specification = 1;
c764a876 15954 part_die->spec_offset = dwarf2_get_ref_die_offset (&attr);
36586728
TT
15955 part_die->spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
15956 || cu->per_cu->is_dwz);
c906108c
SS
15957 break;
15958 case DW_AT_sibling:
15959 /* Ignore absolute siblings, they might point outside of
15960 the current compile unit. */
15961 if (attr.form == DW_FORM_ref_addr)
3e43a32a
MS
15962 complaint (&symfile_complaints,
15963 _("ignoring absolute DW_AT_sibling"));
c906108c 15964 else
b9502d3f
WN
15965 {
15966 unsigned int off = dwarf2_get_ref_die_offset (&attr).sect_off;
15967 const gdb_byte *sibling_ptr = buffer + off;
15968
15969 if (sibling_ptr < info_ptr)
15970 complaint (&symfile_complaints,
15971 _("DW_AT_sibling points backwards"));
22869d73
KS
15972 else if (sibling_ptr > reader->buffer_end)
15973 dwarf2_section_buffer_overflow_complaint (reader->die_section);
b9502d3f
WN
15974 else
15975 part_die->sibling = sibling_ptr;
15976 }
c906108c 15977 break;
fa4028e9
JB
15978 case DW_AT_byte_size:
15979 part_die->has_byte_size = 1;
15980 break;
ff908ebf
AW
15981 case DW_AT_const_value:
15982 part_die->has_const_value = 1;
15983 break;
68511cec
CES
15984 case DW_AT_calling_convention:
15985 /* DWARF doesn't provide a way to identify a program's source-level
15986 entry point. DW_AT_calling_convention attributes are only meant
15987 to describe functions' calling conventions.
15988
15989 However, because it's a necessary piece of information in
15990 Fortran, and because DW_CC_program is the only piece of debugging
15991 information whose definition refers to a 'main program' at all,
15992 several compilers have begun marking Fortran main programs with
15993 DW_CC_program --- even when those functions use the standard
15994 calling conventions.
15995
15996 So until DWARF specifies a way to provide this information and
15997 compilers pick up the new representation, we'll support this
15998 practice. */
15999 if (DW_UNSND (&attr) == DW_CC_program
dc365182
JH
16000 && cu->language == language_fortran
16001 && part_die->name != NULL)
3d548a53 16002 set_objfile_main_name (objfile, part_die->name, language_fortran);
68511cec 16003 break;
481860b3
GB
16004 case DW_AT_inline:
16005 if (DW_UNSND (&attr) == DW_INL_inlined
16006 || DW_UNSND (&attr) == DW_INL_declared_inlined)
16007 part_die->may_be_inlined = 1;
16008 break;
95554aad
TT
16009
16010 case DW_AT_import:
16011 if (part_die->tag == DW_TAG_imported_unit)
36586728
TT
16012 {
16013 part_die->d.offset = dwarf2_get_ref_die_offset (&attr);
16014 part_die->is_dwz = (attr.form == DW_FORM_GNU_ref_alt
16015 || cu->per_cu->is_dwz);
16016 }
95554aad
TT
16017 break;
16018
c906108c
SS
16019 default:
16020 break;
16021 }
16022 }
16023
91da1414
MW
16024 if (high_pc_relative)
16025 part_die->highpc += part_die->lowpc;
16026
9373cf26
JK
16027 if (has_low_pc_attr && has_high_pc_attr)
16028 {
16029 /* When using the GNU linker, .gnu.linkonce. sections are used to
16030 eliminate duplicate copies of functions and vtables and such.
16031 The linker will arbitrarily choose one and discard the others.
16032 The AT_*_pc values for such functions refer to local labels in
16033 these sections. If the section from that file was discarded, the
16034 labels are not in the output, so the relocs get a value of 0.
16035 If this is a discarded function, mark the pc bounds as invalid,
16036 so that GDB will ignore it. */
16037 if (part_die->lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
16038 {
bb5ed363 16039 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9373cf26
JK
16040
16041 complaint (&symfile_complaints,
16042 _("DW_AT_low_pc %s is zero "
16043 "for DIE at 0x%x [in module %s]"),
16044 paddress (gdbarch, part_die->lowpc),
4262abfb 16045 part_die->offset.sect_off, objfile_name (objfile));
9373cf26
JK
16046 }
16047 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
16048 else if (part_die->lowpc >= part_die->highpc)
16049 {
bb5ed363 16050 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9373cf26
JK
16051
16052 complaint (&symfile_complaints,
16053 _("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
16054 "for DIE at 0x%x [in module %s]"),
16055 paddress (gdbarch, part_die->lowpc),
16056 paddress (gdbarch, part_die->highpc),
4262abfb 16057 part_die->offset.sect_off, objfile_name (objfile));
9373cf26
JK
16058 }
16059 else
16060 part_die->has_pc_info = 1;
16061 }
85cbf3d3 16062
c906108c
SS
16063 return info_ptr;
16064}
16065
72bf9492
DJ
16066/* Find a cached partial DIE at OFFSET in CU. */
16067
16068static struct partial_die_info *
b64f50a1 16069find_partial_die_in_comp_unit (sect_offset offset, struct dwarf2_cu *cu)
72bf9492
DJ
16070{
16071 struct partial_die_info *lookup_die = NULL;
16072 struct partial_die_info part_die;
16073
16074 part_die.offset = offset;
9a3c8263
SM
16075 lookup_die = ((struct partial_die_info *)
16076 htab_find_with_hash (cu->partial_dies, &part_die,
16077 offset.sect_off));
72bf9492 16078
72bf9492
DJ
16079 return lookup_die;
16080}
16081
348e048f
DE
16082/* Find a partial DIE at OFFSET, which may or may not be in CU,
16083 except in the case of .debug_types DIEs which do not reference
16084 outside their CU (they do however referencing other types via
55f1336d 16085 DW_FORM_ref_sig8). */
72bf9492
DJ
16086
16087static struct partial_die_info *
36586728 16088find_partial_die (sect_offset offset, int offset_in_dwz, struct dwarf2_cu *cu)
72bf9492 16089{
bb5ed363 16090 struct objfile *objfile = cu->objfile;
5afb4e99
DJ
16091 struct dwarf2_per_cu_data *per_cu = NULL;
16092 struct partial_die_info *pd = NULL;
72bf9492 16093
36586728
TT
16094 if (offset_in_dwz == cu->per_cu->is_dwz
16095 && offset_in_cu_p (&cu->header, offset))
5afb4e99
DJ
16096 {
16097 pd = find_partial_die_in_comp_unit (offset, cu);
16098 if (pd != NULL)
16099 return pd;
0d99eb77
DE
16100 /* We missed recording what we needed.
16101 Load all dies and try again. */
16102 per_cu = cu->per_cu;
5afb4e99 16103 }
0d99eb77
DE
16104 else
16105 {
16106 /* TUs don't reference other CUs/TUs (except via type signatures). */
3019eac3 16107 if (cu->per_cu->is_debug_types)
0d99eb77
DE
16108 {
16109 error (_("Dwarf Error: Type Unit at offset 0x%lx contains"
16110 " external reference to offset 0x%lx [in module %s].\n"),
16111 (long) cu->header.offset.sect_off, (long) offset.sect_off,
16112 bfd_get_filename (objfile->obfd));
16113 }
36586728
TT
16114 per_cu = dwarf2_find_containing_comp_unit (offset, offset_in_dwz,
16115 objfile);
72bf9492 16116
0d99eb77
DE
16117 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
16118 load_partial_comp_unit (per_cu);
ae038cb0 16119
0d99eb77
DE
16120 per_cu->cu->last_used = 0;
16121 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
16122 }
5afb4e99 16123
dee91e82
DE
16124 /* If we didn't find it, and not all dies have been loaded,
16125 load them all and try again. */
16126
5afb4e99
DJ
16127 if (pd == NULL && per_cu->load_all_dies == 0)
16128 {
5afb4e99 16129 per_cu->load_all_dies = 1;
fd820528
DE
16130
16131 /* This is nasty. When we reread the DIEs, somewhere up the call chain
16132 THIS_CU->cu may already be in use. So we can't just free it and
16133 replace its DIEs with the ones we read in. Instead, we leave those
16134 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
16135 and clobber THIS_CU->cu->partial_dies with the hash table for the new
16136 set. */
dee91e82 16137 load_partial_comp_unit (per_cu);
5afb4e99
DJ
16138
16139 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
16140 }
16141
16142 if (pd == NULL)
16143 internal_error (__FILE__, __LINE__,
3e43a32a
MS
16144 _("could not find partial DIE 0x%x "
16145 "in cache [from module %s]\n"),
b64f50a1 16146 offset.sect_off, bfd_get_filename (objfile->obfd));
5afb4e99 16147 return pd;
72bf9492
DJ
16148}
16149
abc72ce4
DE
16150/* See if we can figure out if the class lives in a namespace. We do
16151 this by looking for a member function; its demangled name will
16152 contain namespace info, if there is any. */
16153
16154static void
16155guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
16156 struct dwarf2_cu *cu)
16157{
16158 /* NOTE: carlton/2003-10-07: Getting the info this way changes
16159 what template types look like, because the demangler
16160 frequently doesn't give the same name as the debug info. We
16161 could fix this by only using the demangled name to get the
16162 prefix (but see comment in read_structure_type). */
16163
16164 struct partial_die_info *real_pdi;
16165 struct partial_die_info *child_pdi;
16166
16167 /* If this DIE (this DIE's specification, if any) has a parent, then
16168 we should not do this. We'll prepend the parent's fully qualified
16169 name when we create the partial symbol. */
16170
16171 real_pdi = struct_pdi;
16172 while (real_pdi->has_specification)
36586728
TT
16173 real_pdi = find_partial_die (real_pdi->spec_offset,
16174 real_pdi->spec_is_dwz, cu);
abc72ce4
DE
16175
16176 if (real_pdi->die_parent != NULL)
16177 return;
16178
16179 for (child_pdi = struct_pdi->die_child;
16180 child_pdi != NULL;
16181 child_pdi = child_pdi->die_sibling)
16182 {
16183 if (child_pdi->tag == DW_TAG_subprogram
16184 && child_pdi->linkage_name != NULL)
16185 {
16186 char *actual_class_name
16187 = language_class_name_from_physname (cu->language_defn,
16188 child_pdi->linkage_name);
16189 if (actual_class_name != NULL)
16190 {
16191 struct_pdi->name
224c3ddb
SM
16192 = ((const char *)
16193 obstack_copy0 (&cu->objfile->per_bfd->storage_obstack,
16194 actual_class_name,
16195 strlen (actual_class_name)));
abc72ce4
DE
16196 xfree (actual_class_name);
16197 }
16198 break;
16199 }
16200 }
16201}
16202
72bf9492
DJ
16203/* Adjust PART_DIE before generating a symbol for it. This function
16204 may set the is_external flag or change the DIE's name. */
16205
16206static void
16207fixup_partial_die (struct partial_die_info *part_die,
16208 struct dwarf2_cu *cu)
16209{
abc72ce4
DE
16210 /* Once we've fixed up a die, there's no point in doing so again.
16211 This also avoids a memory leak if we were to call
16212 guess_partial_die_structure_name multiple times. */
16213 if (part_die->fixup_called)
16214 return;
16215
72bf9492
DJ
16216 /* If we found a reference attribute and the DIE has no name, try
16217 to find a name in the referred to DIE. */
16218
16219 if (part_die->name == NULL && part_die->has_specification)
16220 {
16221 struct partial_die_info *spec_die;
72bf9492 16222
36586728
TT
16223 spec_die = find_partial_die (part_die->spec_offset,
16224 part_die->spec_is_dwz, cu);
72bf9492 16225
10b3939b 16226 fixup_partial_die (spec_die, cu);
72bf9492
DJ
16227
16228 if (spec_die->name)
16229 {
16230 part_die->name = spec_die->name;
16231
16232 /* Copy DW_AT_external attribute if it is set. */
16233 if (spec_die->is_external)
16234 part_die->is_external = spec_die->is_external;
16235 }
16236 }
16237
16238 /* Set default names for some unnamed DIEs. */
72bf9492
DJ
16239
16240 if (part_die->name == NULL && part_die->tag == DW_TAG_namespace)
2b1dbab0 16241 part_die->name = CP_ANONYMOUS_NAMESPACE_STR;
72bf9492 16242
abc72ce4
DE
16243 /* If there is no parent die to provide a namespace, and there are
16244 children, see if we can determine the namespace from their linkage
122d1940 16245 name. */
abc72ce4 16246 if (cu->language == language_cplus
8b70b953 16247 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
abc72ce4
DE
16248 && part_die->die_parent == NULL
16249 && part_die->has_children
16250 && (part_die->tag == DW_TAG_class_type
16251 || part_die->tag == DW_TAG_structure_type
16252 || part_die->tag == DW_TAG_union_type))
16253 guess_partial_die_structure_name (part_die, cu);
16254
53832f31
TT
16255 /* GCC might emit a nameless struct or union that has a linkage
16256 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
16257 if (part_die->name == NULL
96408a79
SA
16258 && (part_die->tag == DW_TAG_class_type
16259 || part_die->tag == DW_TAG_interface_type
16260 || part_die->tag == DW_TAG_structure_type
16261 || part_die->tag == DW_TAG_union_type)
53832f31
TT
16262 && part_die->linkage_name != NULL)
16263 {
16264 char *demangled;
16265
8de20a37 16266 demangled = gdb_demangle (part_die->linkage_name, DMGL_TYPES);
53832f31
TT
16267 if (demangled)
16268 {
96408a79
SA
16269 const char *base;
16270
16271 /* Strip any leading namespaces/classes, keep only the base name.
16272 DW_AT_name for named DIEs does not contain the prefixes. */
16273 base = strrchr (demangled, ':');
16274 if (base && base > demangled && base[-1] == ':')
16275 base++;
16276 else
16277 base = demangled;
16278
34a68019 16279 part_die->name
224c3ddb
SM
16280 = ((const char *)
16281 obstack_copy0 (&cu->objfile->per_bfd->storage_obstack,
16282 base, strlen (base)));
53832f31
TT
16283 xfree (demangled);
16284 }
16285 }
16286
abc72ce4 16287 part_die->fixup_called = 1;
72bf9492
DJ
16288}
16289
a8329558 16290/* Read an attribute value described by an attribute form. */
c906108c 16291
d521ce57 16292static const gdb_byte *
dee91e82
DE
16293read_attribute_value (const struct die_reader_specs *reader,
16294 struct attribute *attr, unsigned form,
d521ce57 16295 const gdb_byte *info_ptr)
c906108c 16296{
dee91e82 16297 struct dwarf2_cu *cu = reader->cu;
3e29f34a
MR
16298 struct objfile *objfile = cu->objfile;
16299 struct gdbarch *gdbarch = get_objfile_arch (objfile);
dee91e82 16300 bfd *abfd = reader->abfd;
e7c27a73 16301 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
16302 unsigned int bytes_read;
16303 struct dwarf_block *blk;
16304
aead7601 16305 attr->form = (enum dwarf_form) form;
a8329558 16306 switch (form)
c906108c 16307 {
c906108c 16308 case DW_FORM_ref_addr:
ae411497 16309 if (cu->header.version == 2)
4568ecf9 16310 DW_UNSND (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
ae411497 16311 else
4568ecf9
DE
16312 DW_UNSND (attr) = read_offset (abfd, info_ptr,
16313 &cu->header, &bytes_read);
ae411497
TT
16314 info_ptr += bytes_read;
16315 break;
36586728
TT
16316 case DW_FORM_GNU_ref_alt:
16317 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
16318 info_ptr += bytes_read;
16319 break;
ae411497 16320 case DW_FORM_addr:
e7c27a73 16321 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
3e29f34a 16322 DW_ADDR (attr) = gdbarch_adjust_dwarf2_addr (gdbarch, DW_ADDR (attr));
107d2387 16323 info_ptr += bytes_read;
c906108c
SS
16324 break;
16325 case DW_FORM_block2:
7b5a2f43 16326 blk = dwarf_alloc_block (cu);
c906108c
SS
16327 blk->size = read_2_bytes (abfd, info_ptr);
16328 info_ptr += 2;
16329 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
16330 info_ptr += blk->size;
16331 DW_BLOCK (attr) = blk;
16332 break;
16333 case DW_FORM_block4:
7b5a2f43 16334 blk = dwarf_alloc_block (cu);
c906108c
SS
16335 blk->size = read_4_bytes (abfd, info_ptr);
16336 info_ptr += 4;
16337 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
16338 info_ptr += blk->size;
16339 DW_BLOCK (attr) = blk;
16340 break;
16341 case DW_FORM_data2:
16342 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
16343 info_ptr += 2;
16344 break;
16345 case DW_FORM_data4:
16346 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
16347 info_ptr += 4;
16348 break;
16349 case DW_FORM_data8:
16350 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
16351 info_ptr += 8;
16352 break;
2dc7f7b3
TT
16353 case DW_FORM_sec_offset:
16354 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
16355 info_ptr += bytes_read;
16356 break;
c906108c 16357 case DW_FORM_string:
9b1c24c8 16358 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
8285870a 16359 DW_STRING_IS_CANONICAL (attr) = 0;
c906108c
SS
16360 info_ptr += bytes_read;
16361 break;
4bdf3d34 16362 case DW_FORM_strp:
36586728
TT
16363 if (!cu->per_cu->is_dwz)
16364 {
16365 DW_STRING (attr) = read_indirect_string (abfd, info_ptr, cu_header,
16366 &bytes_read);
16367 DW_STRING_IS_CANONICAL (attr) = 0;
16368 info_ptr += bytes_read;
16369 break;
16370 }
16371 /* FALLTHROUGH */
16372 case DW_FORM_GNU_strp_alt:
16373 {
16374 struct dwz_file *dwz = dwarf2_get_dwz_file ();
16375 LONGEST str_offset = read_offset (abfd, info_ptr, cu_header,
16376 &bytes_read);
16377
16378 DW_STRING (attr) = read_indirect_string_from_dwz (dwz, str_offset);
16379 DW_STRING_IS_CANONICAL (attr) = 0;
16380 info_ptr += bytes_read;
16381 }
4bdf3d34 16382 break;
2dc7f7b3 16383 case DW_FORM_exprloc:
c906108c 16384 case DW_FORM_block:
7b5a2f43 16385 blk = dwarf_alloc_block (cu);
c906108c
SS
16386 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
16387 info_ptr += bytes_read;
16388 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
16389 info_ptr += blk->size;
16390 DW_BLOCK (attr) = blk;
16391 break;
16392 case DW_FORM_block1:
7b5a2f43 16393 blk = dwarf_alloc_block (cu);
c906108c
SS
16394 blk->size = read_1_byte (abfd, info_ptr);
16395 info_ptr += 1;
16396 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
16397 info_ptr += blk->size;
16398 DW_BLOCK (attr) = blk;
16399 break;
16400 case DW_FORM_data1:
16401 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
16402 info_ptr += 1;
16403 break;
16404 case DW_FORM_flag:
16405 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
16406 info_ptr += 1;
16407 break;
2dc7f7b3
TT
16408 case DW_FORM_flag_present:
16409 DW_UNSND (attr) = 1;
16410 break;
c906108c
SS
16411 case DW_FORM_sdata:
16412 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
16413 info_ptr += bytes_read;
16414 break;
16415 case DW_FORM_udata:
16416 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
16417 info_ptr += bytes_read;
16418 break;
16419 case DW_FORM_ref1:
4568ecf9
DE
16420 DW_UNSND (attr) = (cu->header.offset.sect_off
16421 + read_1_byte (abfd, info_ptr));
c906108c
SS
16422 info_ptr += 1;
16423 break;
16424 case DW_FORM_ref2:
4568ecf9
DE
16425 DW_UNSND (attr) = (cu->header.offset.sect_off
16426 + read_2_bytes (abfd, info_ptr));
c906108c
SS
16427 info_ptr += 2;
16428 break;
16429 case DW_FORM_ref4:
4568ecf9
DE
16430 DW_UNSND (attr) = (cu->header.offset.sect_off
16431 + read_4_bytes (abfd, info_ptr));
c906108c
SS
16432 info_ptr += 4;
16433 break;
613e1657 16434 case DW_FORM_ref8:
4568ecf9
DE
16435 DW_UNSND (attr) = (cu->header.offset.sect_off
16436 + read_8_bytes (abfd, info_ptr));
613e1657
KB
16437 info_ptr += 8;
16438 break;
55f1336d 16439 case DW_FORM_ref_sig8:
ac9ec31b 16440 DW_SIGNATURE (attr) = read_8_bytes (abfd, info_ptr);
348e048f
DE
16441 info_ptr += 8;
16442 break;
c906108c 16443 case DW_FORM_ref_udata:
4568ecf9
DE
16444 DW_UNSND (attr) = (cu->header.offset.sect_off
16445 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
c906108c
SS
16446 info_ptr += bytes_read;
16447 break;
c906108c 16448 case DW_FORM_indirect:
a8329558
KW
16449 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
16450 info_ptr += bytes_read;
dee91e82 16451 info_ptr = read_attribute_value (reader, attr, form, info_ptr);
a8329558 16452 break;
3019eac3
DE
16453 case DW_FORM_GNU_addr_index:
16454 if (reader->dwo_file == NULL)
16455 {
16456 /* For now flag a hard error.
16457 Later we can turn this into a complaint. */
16458 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
16459 dwarf_form_name (form),
16460 bfd_get_filename (abfd));
16461 }
16462 DW_ADDR (attr) = read_addr_index_from_leb128 (cu, info_ptr, &bytes_read);
16463 info_ptr += bytes_read;
16464 break;
16465 case DW_FORM_GNU_str_index:
16466 if (reader->dwo_file == NULL)
16467 {
16468 /* For now flag a hard error.
16469 Later we can turn this into a complaint if warranted. */
16470 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
16471 dwarf_form_name (form),
16472 bfd_get_filename (abfd));
16473 }
16474 {
16475 ULONGEST str_index =
16476 read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
16477
342587c4 16478 DW_STRING (attr) = read_str_index (reader, str_index);
3019eac3
DE
16479 DW_STRING_IS_CANONICAL (attr) = 0;
16480 info_ptr += bytes_read;
16481 }
16482 break;
c906108c 16483 default:
8a3fe4f8 16484 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
659b0389
ML
16485 dwarf_form_name (form),
16486 bfd_get_filename (abfd));
c906108c 16487 }
28e94949 16488
36586728 16489 /* Super hack. */
7771576e 16490 if (cu->per_cu->is_dwz && attr_form_is_ref (attr))
36586728
TT
16491 attr->form = DW_FORM_GNU_ref_alt;
16492
28e94949
JB
16493 /* We have seen instances where the compiler tried to emit a byte
16494 size attribute of -1 which ended up being encoded as an unsigned
16495 0xffffffff. Although 0xffffffff is technically a valid size value,
16496 an object of this size seems pretty unlikely so we can relatively
16497 safely treat these cases as if the size attribute was invalid and
16498 treat them as zero by default. */
16499 if (attr->name == DW_AT_byte_size
16500 && form == DW_FORM_data4
16501 && DW_UNSND (attr) >= 0xffffffff)
01c66ae6
JB
16502 {
16503 complaint
16504 (&symfile_complaints,
43bbcdc2
PH
16505 _("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
16506 hex_string (DW_UNSND (attr)));
01c66ae6
JB
16507 DW_UNSND (attr) = 0;
16508 }
28e94949 16509
c906108c
SS
16510 return info_ptr;
16511}
16512
a8329558
KW
16513/* Read an attribute described by an abbreviated attribute. */
16514
d521ce57 16515static const gdb_byte *
dee91e82
DE
16516read_attribute (const struct die_reader_specs *reader,
16517 struct attribute *attr, struct attr_abbrev *abbrev,
d521ce57 16518 const gdb_byte *info_ptr)
a8329558
KW
16519{
16520 attr->name = abbrev->name;
dee91e82 16521 return read_attribute_value (reader, attr, abbrev->form, info_ptr);
a8329558
KW
16522}
16523
0963b4bd 16524/* Read dwarf information from a buffer. */
c906108c
SS
16525
16526static unsigned int
a1855c1d 16527read_1_byte (bfd *abfd, const gdb_byte *buf)
c906108c 16528{
fe1b8b76 16529 return bfd_get_8 (abfd, buf);
c906108c
SS
16530}
16531
16532static int
a1855c1d 16533read_1_signed_byte (bfd *abfd, const gdb_byte *buf)
c906108c 16534{
fe1b8b76 16535 return bfd_get_signed_8 (abfd, buf);
c906108c
SS
16536}
16537
16538static unsigned int
a1855c1d 16539read_2_bytes (bfd *abfd, const gdb_byte *buf)
c906108c 16540{
fe1b8b76 16541 return bfd_get_16 (abfd, buf);
c906108c
SS
16542}
16543
21ae7a4d 16544static int
a1855c1d 16545read_2_signed_bytes (bfd *abfd, const gdb_byte *buf)
21ae7a4d
JK
16546{
16547 return bfd_get_signed_16 (abfd, buf);
16548}
16549
c906108c 16550static unsigned int
a1855c1d 16551read_4_bytes (bfd *abfd, const gdb_byte *buf)
c906108c 16552{
fe1b8b76 16553 return bfd_get_32 (abfd, buf);
c906108c
SS
16554}
16555
21ae7a4d 16556static int
a1855c1d 16557read_4_signed_bytes (bfd *abfd, const gdb_byte *buf)
21ae7a4d
JK
16558{
16559 return bfd_get_signed_32 (abfd, buf);
16560}
16561
93311388 16562static ULONGEST
a1855c1d 16563read_8_bytes (bfd *abfd, const gdb_byte *buf)
c906108c 16564{
fe1b8b76 16565 return bfd_get_64 (abfd, buf);
c906108c
SS
16566}
16567
16568static CORE_ADDR
d521ce57 16569read_address (bfd *abfd, const gdb_byte *buf, struct dwarf2_cu *cu,
891d2f0b 16570 unsigned int *bytes_read)
c906108c 16571{
e7c27a73 16572 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
16573 CORE_ADDR retval = 0;
16574
107d2387 16575 if (cu_header->signed_addr_p)
c906108c 16576 {
107d2387
AC
16577 switch (cu_header->addr_size)
16578 {
16579 case 2:
fe1b8b76 16580 retval = bfd_get_signed_16 (abfd, buf);
107d2387
AC
16581 break;
16582 case 4:
fe1b8b76 16583 retval = bfd_get_signed_32 (abfd, buf);
107d2387
AC
16584 break;
16585 case 8:
fe1b8b76 16586 retval = bfd_get_signed_64 (abfd, buf);
107d2387
AC
16587 break;
16588 default:
8e65ff28 16589 internal_error (__FILE__, __LINE__,
e2e0b3e5 16590 _("read_address: bad switch, signed [in module %s]"),
659b0389 16591 bfd_get_filename (abfd));
107d2387
AC
16592 }
16593 }
16594 else
16595 {
16596 switch (cu_header->addr_size)
16597 {
16598 case 2:
fe1b8b76 16599 retval = bfd_get_16 (abfd, buf);
107d2387
AC
16600 break;
16601 case 4:
fe1b8b76 16602 retval = bfd_get_32 (abfd, buf);
107d2387
AC
16603 break;
16604 case 8:
fe1b8b76 16605 retval = bfd_get_64 (abfd, buf);
107d2387
AC
16606 break;
16607 default:
8e65ff28 16608 internal_error (__FILE__, __LINE__,
a73c6dcd
MS
16609 _("read_address: bad switch, "
16610 "unsigned [in module %s]"),
659b0389 16611 bfd_get_filename (abfd));
107d2387 16612 }
c906108c 16613 }
64367e0a 16614
107d2387
AC
16615 *bytes_read = cu_header->addr_size;
16616 return retval;
c906108c
SS
16617}
16618
f7ef9339 16619/* Read the initial length from a section. The (draft) DWARF 3
613e1657
KB
16620 specification allows the initial length to take up either 4 bytes
16621 or 12 bytes. If the first 4 bytes are 0xffffffff, then the next 8
16622 bytes describe the length and all offsets will be 8 bytes in length
16623 instead of 4.
16624
f7ef9339
KB
16625 An older, non-standard 64-bit format is also handled by this
16626 function. The older format in question stores the initial length
16627 as an 8-byte quantity without an escape value. Lengths greater
16628 than 2^32 aren't very common which means that the initial 4 bytes
16629 is almost always zero. Since a length value of zero doesn't make
16630 sense for the 32-bit format, this initial zero can be considered to
16631 be an escape value which indicates the presence of the older 64-bit
16632 format. As written, the code can't detect (old format) lengths
917c78fc
MK
16633 greater than 4GB. If it becomes necessary to handle lengths
16634 somewhat larger than 4GB, we could allow other small values (such
16635 as the non-sensical values of 1, 2, and 3) to also be used as
16636 escape values indicating the presence of the old format.
f7ef9339 16637
917c78fc
MK
16638 The value returned via bytes_read should be used to increment the
16639 relevant pointer after calling read_initial_length().
c764a876 16640
613e1657
KB
16641 [ Note: read_initial_length() and read_offset() are based on the
16642 document entitled "DWARF Debugging Information Format", revision
f7ef9339 16643 3, draft 8, dated November 19, 2001. This document was obtained
613e1657
KB
16644 from:
16645
f7ef9339 16646 http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
6e70227d 16647
613e1657
KB
16648 This document is only a draft and is subject to change. (So beware.)
16649
f7ef9339 16650 Details regarding the older, non-standard 64-bit format were
917c78fc
MK
16651 determined empirically by examining 64-bit ELF files produced by
16652 the SGI toolchain on an IRIX 6.5 machine.
f7ef9339
KB
16653
16654 - Kevin, July 16, 2002
613e1657
KB
16655 ] */
16656
16657static LONGEST
d521ce57 16658read_initial_length (bfd *abfd, const gdb_byte *buf, unsigned int *bytes_read)
613e1657 16659{
fe1b8b76 16660 LONGEST length = bfd_get_32 (abfd, buf);
613e1657 16661
dd373385 16662 if (length == 0xffffffff)
613e1657 16663 {
fe1b8b76 16664 length = bfd_get_64 (abfd, buf + 4);
613e1657 16665 *bytes_read = 12;
613e1657 16666 }
dd373385 16667 else if (length == 0)
f7ef9339 16668 {
dd373385 16669 /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX. */
fe1b8b76 16670 length = bfd_get_64 (abfd, buf);
f7ef9339 16671 *bytes_read = 8;
f7ef9339 16672 }
613e1657
KB
16673 else
16674 {
16675 *bytes_read = 4;
613e1657
KB
16676 }
16677
c764a876
DE
16678 return length;
16679}
dd373385 16680
c764a876
DE
16681/* Cover function for read_initial_length.
16682 Returns the length of the object at BUF, and stores the size of the
16683 initial length in *BYTES_READ and stores the size that offsets will be in
16684 *OFFSET_SIZE.
16685 If the initial length size is not equivalent to that specified in
16686 CU_HEADER then issue a complaint.
16687 This is useful when reading non-comp-unit headers. */
dd373385 16688
c764a876 16689static LONGEST
d521ce57 16690read_checked_initial_length_and_offset (bfd *abfd, const gdb_byte *buf,
c764a876
DE
16691 const struct comp_unit_head *cu_header,
16692 unsigned int *bytes_read,
16693 unsigned int *offset_size)
16694{
16695 LONGEST length = read_initial_length (abfd, buf, bytes_read);
16696
16697 gdb_assert (cu_header->initial_length_size == 4
16698 || cu_header->initial_length_size == 8
16699 || cu_header->initial_length_size == 12);
16700
16701 if (cu_header->initial_length_size != *bytes_read)
16702 complaint (&symfile_complaints,
16703 _("intermixed 32-bit and 64-bit DWARF sections"));
dd373385 16704
c764a876 16705 *offset_size = (*bytes_read == 4) ? 4 : 8;
dd373385 16706 return length;
613e1657
KB
16707}
16708
16709/* Read an offset from the data stream. The size of the offset is
917c78fc 16710 given by cu_header->offset_size. */
613e1657
KB
16711
16712static LONGEST
d521ce57
TT
16713read_offset (bfd *abfd, const gdb_byte *buf,
16714 const struct comp_unit_head *cu_header,
891d2f0b 16715 unsigned int *bytes_read)
c764a876
DE
16716{
16717 LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
9a619af0 16718
c764a876
DE
16719 *bytes_read = cu_header->offset_size;
16720 return offset;
16721}
16722
16723/* Read an offset from the data stream. */
16724
16725static LONGEST
d521ce57 16726read_offset_1 (bfd *abfd, const gdb_byte *buf, unsigned int offset_size)
613e1657
KB
16727{
16728 LONGEST retval = 0;
16729
c764a876 16730 switch (offset_size)
613e1657
KB
16731 {
16732 case 4:
fe1b8b76 16733 retval = bfd_get_32 (abfd, buf);
613e1657
KB
16734 break;
16735 case 8:
fe1b8b76 16736 retval = bfd_get_64 (abfd, buf);
613e1657
KB
16737 break;
16738 default:
8e65ff28 16739 internal_error (__FILE__, __LINE__,
c764a876 16740 _("read_offset_1: bad switch [in module %s]"),
659b0389 16741 bfd_get_filename (abfd));
613e1657
KB
16742 }
16743
917c78fc 16744 return retval;
613e1657
KB
16745}
16746
d521ce57
TT
16747static const gdb_byte *
16748read_n_bytes (bfd *abfd, const gdb_byte *buf, unsigned int size)
c906108c
SS
16749{
16750 /* If the size of a host char is 8 bits, we can return a pointer
16751 to the buffer, otherwise we have to copy the data to a buffer
16752 allocated on the temporary obstack. */
4bdf3d34 16753 gdb_assert (HOST_CHAR_BIT == 8);
c906108c 16754 return buf;
c906108c
SS
16755}
16756
d521ce57
TT
16757static const char *
16758read_direct_string (bfd *abfd, const gdb_byte *buf,
16759 unsigned int *bytes_read_ptr)
c906108c
SS
16760{
16761 /* If the size of a host char is 8 bits, we can return a pointer
16762 to the string, otherwise we have to copy the string to a buffer
16763 allocated on the temporary obstack. */
4bdf3d34 16764 gdb_assert (HOST_CHAR_BIT == 8);
c906108c
SS
16765 if (*buf == '\0')
16766 {
16767 *bytes_read_ptr = 1;
16768 return NULL;
16769 }
d521ce57
TT
16770 *bytes_read_ptr = strlen ((const char *) buf) + 1;
16771 return (const char *) buf;
4bdf3d34
JJ
16772}
16773
d521ce57 16774static const char *
cf2c3c16 16775read_indirect_string_at_offset (bfd *abfd, LONGEST str_offset)
4bdf3d34 16776{
be391dca 16777 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwarf2_per_objfile->str);
dce234bc 16778 if (dwarf2_per_objfile->str.buffer == NULL)
cf2c3c16
TT
16779 error (_("DW_FORM_strp used without .debug_str section [in module %s]"),
16780 bfd_get_filename (abfd));
dce234bc 16781 if (str_offset >= dwarf2_per_objfile->str.size)
cf2c3c16
TT
16782 error (_("DW_FORM_strp pointing outside of "
16783 ".debug_str section [in module %s]"),
16784 bfd_get_filename (abfd));
4bdf3d34 16785 gdb_assert (HOST_CHAR_BIT == 8);
dce234bc 16786 if (dwarf2_per_objfile->str.buffer[str_offset] == '\0')
4bdf3d34 16787 return NULL;
d521ce57 16788 return (const char *) (dwarf2_per_objfile->str.buffer + str_offset);
c906108c
SS
16789}
16790
36586728
TT
16791/* Read a string at offset STR_OFFSET in the .debug_str section from
16792 the .dwz file DWZ. Throw an error if the offset is too large. If
16793 the string consists of a single NUL byte, return NULL; otherwise
16794 return a pointer to the string. */
16795
d521ce57 16796static const char *
36586728
TT
16797read_indirect_string_from_dwz (struct dwz_file *dwz, LONGEST str_offset)
16798{
16799 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwz->str);
16800
16801 if (dwz->str.buffer == NULL)
16802 error (_("DW_FORM_GNU_strp_alt used without .debug_str "
16803 "section [in module %s]"),
16804 bfd_get_filename (dwz->dwz_bfd));
16805 if (str_offset >= dwz->str.size)
16806 error (_("DW_FORM_GNU_strp_alt pointing outside of "
16807 ".debug_str section [in module %s]"),
16808 bfd_get_filename (dwz->dwz_bfd));
16809 gdb_assert (HOST_CHAR_BIT == 8);
16810 if (dwz->str.buffer[str_offset] == '\0')
16811 return NULL;
d521ce57 16812 return (const char *) (dwz->str.buffer + str_offset);
36586728
TT
16813}
16814
d521ce57
TT
16815static const char *
16816read_indirect_string (bfd *abfd, const gdb_byte *buf,
cf2c3c16
TT
16817 const struct comp_unit_head *cu_header,
16818 unsigned int *bytes_read_ptr)
16819{
16820 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
16821
16822 return read_indirect_string_at_offset (abfd, str_offset);
16823}
16824
12df843f 16825static ULONGEST
d521ce57
TT
16826read_unsigned_leb128 (bfd *abfd, const gdb_byte *buf,
16827 unsigned int *bytes_read_ptr)
c906108c 16828{
12df843f 16829 ULONGEST result;
ce5d95e1 16830 unsigned int num_read;
870f88f7 16831 int shift;
c906108c
SS
16832 unsigned char byte;
16833
16834 result = 0;
16835 shift = 0;
16836 num_read = 0;
c906108c
SS
16837 while (1)
16838 {
fe1b8b76 16839 byte = bfd_get_8 (abfd, buf);
c906108c
SS
16840 buf++;
16841 num_read++;
12df843f 16842 result |= ((ULONGEST) (byte & 127) << shift);
c906108c
SS
16843 if ((byte & 128) == 0)
16844 {
16845 break;
16846 }
16847 shift += 7;
16848 }
16849 *bytes_read_ptr = num_read;
16850 return result;
16851}
16852
12df843f 16853static LONGEST
d521ce57
TT
16854read_signed_leb128 (bfd *abfd, const gdb_byte *buf,
16855 unsigned int *bytes_read_ptr)
c906108c 16856{
12df843f 16857 LONGEST result;
870f88f7 16858 int shift, num_read;
c906108c
SS
16859 unsigned char byte;
16860
16861 result = 0;
16862 shift = 0;
c906108c 16863 num_read = 0;
c906108c
SS
16864 while (1)
16865 {
fe1b8b76 16866 byte = bfd_get_8 (abfd, buf);
c906108c
SS
16867 buf++;
16868 num_read++;
12df843f 16869 result |= ((LONGEST) (byte & 127) << shift);
c906108c
SS
16870 shift += 7;
16871 if ((byte & 128) == 0)
16872 {
16873 break;
16874 }
16875 }
77e0b926 16876 if ((shift < 8 * sizeof (result)) && (byte & 0x40))
12df843f 16877 result |= -(((LONGEST) 1) << shift);
c906108c
SS
16878 *bytes_read_ptr = num_read;
16879 return result;
16880}
16881
3019eac3
DE
16882/* Given index ADDR_INDEX in .debug_addr, fetch the value.
16883 ADDR_BASE is the DW_AT_GNU_addr_base attribute or zero.
16884 ADDR_SIZE is the size of addresses from the CU header. */
16885
16886static CORE_ADDR
16887read_addr_index_1 (unsigned int addr_index, ULONGEST addr_base, int addr_size)
16888{
16889 struct objfile *objfile = dwarf2_per_objfile->objfile;
16890 bfd *abfd = objfile->obfd;
16891 const gdb_byte *info_ptr;
16892
16893 dwarf2_read_section (objfile, &dwarf2_per_objfile->addr);
16894 if (dwarf2_per_objfile->addr.buffer == NULL)
16895 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
4262abfb 16896 objfile_name (objfile));
3019eac3
DE
16897 if (addr_base + addr_index * addr_size >= dwarf2_per_objfile->addr.size)
16898 error (_("DW_FORM_addr_index pointing outside of "
16899 ".debug_addr section [in module %s]"),
4262abfb 16900 objfile_name (objfile));
3019eac3
DE
16901 info_ptr = (dwarf2_per_objfile->addr.buffer
16902 + addr_base + addr_index * addr_size);
16903 if (addr_size == 4)
16904 return bfd_get_32 (abfd, info_ptr);
16905 else
16906 return bfd_get_64 (abfd, info_ptr);
16907}
16908
16909/* Given index ADDR_INDEX in .debug_addr, fetch the value. */
16910
16911static CORE_ADDR
16912read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
16913{
16914 return read_addr_index_1 (addr_index, cu->addr_base, cu->header.addr_size);
16915}
16916
16917/* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
16918
16919static CORE_ADDR
d521ce57 16920read_addr_index_from_leb128 (struct dwarf2_cu *cu, const gdb_byte *info_ptr,
3019eac3
DE
16921 unsigned int *bytes_read)
16922{
16923 bfd *abfd = cu->objfile->obfd;
16924 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
16925
16926 return read_addr_index (cu, addr_index);
16927}
16928
16929/* Data structure to pass results from dwarf2_read_addr_index_reader
16930 back to dwarf2_read_addr_index. */
16931
16932struct dwarf2_read_addr_index_data
16933{
16934 ULONGEST addr_base;
16935 int addr_size;
16936};
16937
16938/* die_reader_func for dwarf2_read_addr_index. */
16939
16940static void
16941dwarf2_read_addr_index_reader (const struct die_reader_specs *reader,
d521ce57 16942 const gdb_byte *info_ptr,
3019eac3
DE
16943 struct die_info *comp_unit_die,
16944 int has_children,
16945 void *data)
16946{
16947 struct dwarf2_cu *cu = reader->cu;
16948 struct dwarf2_read_addr_index_data *aidata =
16949 (struct dwarf2_read_addr_index_data *) data;
16950
16951 aidata->addr_base = cu->addr_base;
16952 aidata->addr_size = cu->header.addr_size;
16953}
16954
16955/* Given an index in .debug_addr, fetch the value.
16956 NOTE: This can be called during dwarf expression evaluation,
16957 long after the debug information has been read, and thus per_cu->cu
16958 may no longer exist. */
16959
16960CORE_ADDR
16961dwarf2_read_addr_index (struct dwarf2_per_cu_data *per_cu,
16962 unsigned int addr_index)
16963{
16964 struct objfile *objfile = per_cu->objfile;
16965 struct dwarf2_cu *cu = per_cu->cu;
16966 ULONGEST addr_base;
16967 int addr_size;
16968
16969 /* This is intended to be called from outside this file. */
16970 dw2_setup (objfile);
16971
16972 /* We need addr_base and addr_size.
16973 If we don't have PER_CU->cu, we have to get it.
16974 Nasty, but the alternative is storing the needed info in PER_CU,
16975 which at this point doesn't seem justified: it's not clear how frequently
16976 it would get used and it would increase the size of every PER_CU.
16977 Entry points like dwarf2_per_cu_addr_size do a similar thing
16978 so we're not in uncharted territory here.
16979 Alas we need to be a bit more complicated as addr_base is contained
16980 in the DIE.
16981
16982 We don't need to read the entire CU(/TU).
16983 We just need the header and top level die.
a1b64ce1 16984
3019eac3 16985 IWBN to use the aging mechanism to let us lazily later discard the CU.
a1b64ce1 16986 For now we skip this optimization. */
3019eac3
DE
16987
16988 if (cu != NULL)
16989 {
16990 addr_base = cu->addr_base;
16991 addr_size = cu->header.addr_size;
16992 }
16993 else
16994 {
16995 struct dwarf2_read_addr_index_data aidata;
16996
a1b64ce1
DE
16997 /* Note: We can't use init_cutu_and_read_dies_simple here,
16998 we need addr_base. */
16999 init_cutu_and_read_dies (per_cu, NULL, 0, 0,
17000 dwarf2_read_addr_index_reader, &aidata);
3019eac3
DE
17001 addr_base = aidata.addr_base;
17002 addr_size = aidata.addr_size;
17003 }
17004
17005 return read_addr_index_1 (addr_index, addr_base, addr_size);
17006}
17007
57d63ce2
DE
17008/* Given a DW_FORM_GNU_str_index, fetch the string.
17009 This is only used by the Fission support. */
3019eac3 17010
d521ce57 17011static const char *
342587c4 17012read_str_index (const struct die_reader_specs *reader, ULONGEST str_index)
3019eac3
DE
17013{
17014 struct objfile *objfile = dwarf2_per_objfile->objfile;
c5164cbc 17015 const char *objf_name = objfile_name (objfile);
3019eac3 17016 bfd *abfd = objfile->obfd;
342587c4 17017 struct dwarf2_cu *cu = reader->cu;
73869dc2
DE
17018 struct dwarf2_section_info *str_section = &reader->dwo_file->sections.str;
17019 struct dwarf2_section_info *str_offsets_section =
17020 &reader->dwo_file->sections.str_offsets;
d521ce57 17021 const gdb_byte *info_ptr;
3019eac3 17022 ULONGEST str_offset;
57d63ce2 17023 static const char form_name[] = "DW_FORM_GNU_str_index";
3019eac3 17024
73869dc2
DE
17025 dwarf2_read_section (objfile, str_section);
17026 dwarf2_read_section (objfile, str_offsets_section);
17027 if (str_section->buffer == NULL)
57d63ce2 17028 error (_("%s used without .debug_str.dwo section"
3019eac3 17029 " in CU at offset 0x%lx [in module %s]"),
c5164cbc 17030 form_name, (long) cu->header.offset.sect_off, objf_name);
73869dc2 17031 if (str_offsets_section->buffer == NULL)
57d63ce2 17032 error (_("%s used without .debug_str_offsets.dwo section"
3019eac3 17033 " in CU at offset 0x%lx [in module %s]"),
c5164cbc 17034 form_name, (long) cu->header.offset.sect_off, objf_name);
73869dc2 17035 if (str_index * cu->header.offset_size >= str_offsets_section->size)
57d63ce2 17036 error (_("%s pointing outside of .debug_str_offsets.dwo"
3019eac3 17037 " section in CU at offset 0x%lx [in module %s]"),
c5164cbc 17038 form_name, (long) cu->header.offset.sect_off, objf_name);
73869dc2 17039 info_ptr = (str_offsets_section->buffer
3019eac3
DE
17040 + str_index * cu->header.offset_size);
17041 if (cu->header.offset_size == 4)
17042 str_offset = bfd_get_32 (abfd, info_ptr);
17043 else
17044 str_offset = bfd_get_64 (abfd, info_ptr);
73869dc2 17045 if (str_offset >= str_section->size)
57d63ce2 17046 error (_("Offset from %s pointing outside of"
3019eac3 17047 " .debug_str.dwo section in CU at offset 0x%lx [in module %s]"),
c5164cbc 17048 form_name, (long) cu->header.offset.sect_off, objf_name);
73869dc2 17049 return (const char *) (str_section->buffer + str_offset);
3019eac3
DE
17050}
17051
3019eac3
DE
17052/* Return the length of an LEB128 number in BUF. */
17053
17054static int
17055leb128_size (const gdb_byte *buf)
17056{
17057 const gdb_byte *begin = buf;
17058 gdb_byte byte;
17059
17060 while (1)
17061 {
17062 byte = *buf++;
17063 if ((byte & 128) == 0)
17064 return buf - begin;
17065 }
17066}
17067
c906108c 17068static void
e142c38c 17069set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
c906108c
SS
17070{
17071 switch (lang)
17072 {
17073 case DW_LANG_C89:
76bee0cc 17074 case DW_LANG_C99:
0cfd832f 17075 case DW_LANG_C11:
c906108c 17076 case DW_LANG_C:
d1be3247 17077 case DW_LANG_UPC:
e142c38c 17078 cu->language = language_c;
c906108c
SS
17079 break;
17080 case DW_LANG_C_plus_plus:
0cfd832f
MW
17081 case DW_LANG_C_plus_plus_11:
17082 case DW_LANG_C_plus_plus_14:
e142c38c 17083 cu->language = language_cplus;
c906108c 17084 break;
6aecb9c2
JB
17085 case DW_LANG_D:
17086 cu->language = language_d;
17087 break;
c906108c
SS
17088 case DW_LANG_Fortran77:
17089 case DW_LANG_Fortran90:
b21b22e0 17090 case DW_LANG_Fortran95:
f7de9aab
MW
17091 case DW_LANG_Fortran03:
17092 case DW_LANG_Fortran08:
e142c38c 17093 cu->language = language_fortran;
c906108c 17094 break;
a766d390
DE
17095 case DW_LANG_Go:
17096 cu->language = language_go;
17097 break;
c906108c 17098 case DW_LANG_Mips_Assembler:
e142c38c 17099 cu->language = language_asm;
c906108c 17100 break;
bebd888e 17101 case DW_LANG_Java:
e142c38c 17102 cu->language = language_java;
bebd888e 17103 break;
c906108c 17104 case DW_LANG_Ada83:
8aaf0b47 17105 case DW_LANG_Ada95:
bc5f45f8
JB
17106 cu->language = language_ada;
17107 break;
72019c9c
GM
17108 case DW_LANG_Modula2:
17109 cu->language = language_m2;
17110 break;
fe8e67fd
PM
17111 case DW_LANG_Pascal83:
17112 cu->language = language_pascal;
17113 break;
22566fbd
DJ
17114 case DW_LANG_ObjC:
17115 cu->language = language_objc;
17116 break;
c44af4eb
TT
17117 case DW_LANG_Rust:
17118 case DW_LANG_Rust_old:
17119 cu->language = language_rust;
17120 break;
c906108c
SS
17121 case DW_LANG_Cobol74:
17122 case DW_LANG_Cobol85:
c906108c 17123 default:
e142c38c 17124 cu->language = language_minimal;
c906108c
SS
17125 break;
17126 }
e142c38c 17127 cu->language_defn = language_def (cu->language);
c906108c
SS
17128}
17129
17130/* Return the named attribute or NULL if not there. */
17131
17132static struct attribute *
e142c38c 17133dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
c906108c 17134{
a48e046c 17135 for (;;)
c906108c 17136 {
a48e046c
TT
17137 unsigned int i;
17138 struct attribute *spec = NULL;
17139
17140 for (i = 0; i < die->num_attrs; ++i)
17141 {
17142 if (die->attrs[i].name == name)
17143 return &die->attrs[i];
17144 if (die->attrs[i].name == DW_AT_specification
17145 || die->attrs[i].name == DW_AT_abstract_origin)
17146 spec = &die->attrs[i];
17147 }
17148
17149 if (!spec)
17150 break;
c906108c 17151
f2f0e013 17152 die = follow_die_ref (die, spec, &cu);
f2f0e013 17153 }
c5aa993b 17154
c906108c
SS
17155 return NULL;
17156}
17157
348e048f
DE
17158/* Return the named attribute or NULL if not there,
17159 but do not follow DW_AT_specification, etc.
17160 This is for use in contexts where we're reading .debug_types dies.
17161 Following DW_AT_specification, DW_AT_abstract_origin will take us
17162 back up the chain, and we want to go down. */
17163
17164static struct attribute *
45e58e77 17165dwarf2_attr_no_follow (struct die_info *die, unsigned int name)
348e048f
DE
17166{
17167 unsigned int i;
17168
17169 for (i = 0; i < die->num_attrs; ++i)
17170 if (die->attrs[i].name == name)
17171 return &die->attrs[i];
17172
17173 return NULL;
17174}
17175
7d45c7c3
KB
17176/* Return the string associated with a string-typed attribute, or NULL if it
17177 is either not found or is of an incorrect type. */
17178
17179static const char *
17180dwarf2_string_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
17181{
17182 struct attribute *attr;
17183 const char *str = NULL;
17184
17185 attr = dwarf2_attr (die, name, cu);
17186
17187 if (attr != NULL)
17188 {
17189 if (attr->form == DW_FORM_strp || attr->form == DW_FORM_string
17190 || attr->form == DW_FORM_GNU_strp_alt)
17191 str = DW_STRING (attr);
17192 else
17193 complaint (&symfile_complaints,
17194 _("string type expected for attribute %s for "
17195 "DIE at 0x%x in module %s"),
17196 dwarf_attr_name (name), die->offset.sect_off,
17197 objfile_name (cu->objfile));
17198 }
17199
17200 return str;
17201}
17202
05cf31d1
JB
17203/* Return non-zero iff the attribute NAME is defined for the given DIE,
17204 and holds a non-zero value. This function should only be used for
2dc7f7b3 17205 DW_FORM_flag or DW_FORM_flag_present attributes. */
05cf31d1
JB
17206
17207static int
17208dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
17209{
17210 struct attribute *attr = dwarf2_attr (die, name, cu);
17211
17212 return (attr && DW_UNSND (attr));
17213}
17214
3ca72b44 17215static int
e142c38c 17216die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
3ca72b44 17217{
05cf31d1
JB
17218 /* A DIE is a declaration if it has a DW_AT_declaration attribute
17219 which value is non-zero. However, we have to be careful with
17220 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
17221 (via dwarf2_flag_true_p) follows this attribute. So we may
17222 end up accidently finding a declaration attribute that belongs
17223 to a different DIE referenced by the specification attribute,
17224 even though the given DIE does not have a declaration attribute. */
17225 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
17226 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
3ca72b44
AC
17227}
17228
63d06c5c 17229/* Return the die giving the specification for DIE, if there is
f2f0e013 17230 one. *SPEC_CU is the CU containing DIE on input, and the CU
edb3359d
DJ
17231 containing the return value on output. If there is no
17232 specification, but there is an abstract origin, that is
17233 returned. */
63d06c5c
DC
17234
17235static struct die_info *
f2f0e013 17236die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
63d06c5c 17237{
f2f0e013
DJ
17238 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
17239 *spec_cu);
63d06c5c 17240
edb3359d
DJ
17241 if (spec_attr == NULL)
17242 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
17243
63d06c5c
DC
17244 if (spec_attr == NULL)
17245 return NULL;
17246 else
f2f0e013 17247 return follow_die_ref (die, spec_attr, spec_cu);
63d06c5c 17248}
c906108c 17249
debd256d 17250/* Free the line_header structure *LH, and any arrays and strings it
ae2de4f8
DE
17251 refers to.
17252 NOTE: This is also used as a "cleanup" function. */
17253
debd256d
JB
17254static void
17255free_line_header (struct line_header *lh)
17256{
17257 if (lh->standard_opcode_lengths)
a8bc7b56 17258 xfree (lh->standard_opcode_lengths);
debd256d
JB
17259
17260 /* Remember that all the lh->file_names[i].name pointers are
17261 pointers into debug_line_buffer, and don't need to be freed. */
17262 if (lh->file_names)
a8bc7b56 17263 xfree (lh->file_names);
debd256d
JB
17264
17265 /* Similarly for the include directory names. */
17266 if (lh->include_dirs)
a8bc7b56 17267 xfree (lh->include_dirs);
debd256d 17268
a8bc7b56 17269 xfree (lh);
debd256d
JB
17270}
17271
527f3840
JK
17272/* Stub for free_line_header to match void * callback types. */
17273
17274static void
17275free_line_header_voidp (void *arg)
17276{
9a3c8263 17277 struct line_header *lh = (struct line_header *) arg;
527f3840
JK
17278
17279 free_line_header (lh);
17280}
17281
debd256d 17282/* Add an entry to LH's include directory table. */
ae2de4f8 17283
debd256d 17284static void
d521ce57 17285add_include_dir (struct line_header *lh, const char *include_dir)
c906108c 17286{
27e0867f
DE
17287 if (dwarf_line_debug >= 2)
17288 fprintf_unfiltered (gdb_stdlog, "Adding dir %u: %s\n",
17289 lh->num_include_dirs + 1, include_dir);
17290
debd256d
JB
17291 /* Grow the array if necessary. */
17292 if (lh->include_dirs_size == 0)
c5aa993b 17293 {
debd256d 17294 lh->include_dirs_size = 1; /* for testing */
8d749320 17295 lh->include_dirs = XNEWVEC (const char *, lh->include_dirs_size);
debd256d
JB
17296 }
17297 else if (lh->num_include_dirs >= lh->include_dirs_size)
17298 {
17299 lh->include_dirs_size *= 2;
8d749320
SM
17300 lh->include_dirs = XRESIZEVEC (const char *, lh->include_dirs,
17301 lh->include_dirs_size);
c5aa993b 17302 }
c906108c 17303
debd256d
JB
17304 lh->include_dirs[lh->num_include_dirs++] = include_dir;
17305}
6e70227d 17306
debd256d 17307/* Add an entry to LH's file name table. */
ae2de4f8 17308
debd256d
JB
17309static void
17310add_file_name (struct line_header *lh,
d521ce57 17311 const char *name,
debd256d
JB
17312 unsigned int dir_index,
17313 unsigned int mod_time,
17314 unsigned int length)
17315{
17316 struct file_entry *fe;
17317
27e0867f
DE
17318 if (dwarf_line_debug >= 2)
17319 fprintf_unfiltered (gdb_stdlog, "Adding file %u: %s\n",
17320 lh->num_file_names + 1, name);
17321
debd256d
JB
17322 /* Grow the array if necessary. */
17323 if (lh->file_names_size == 0)
17324 {
17325 lh->file_names_size = 1; /* for testing */
8d749320 17326 lh->file_names = XNEWVEC (struct file_entry, lh->file_names_size);
debd256d
JB
17327 }
17328 else if (lh->num_file_names >= lh->file_names_size)
17329 {
17330 lh->file_names_size *= 2;
224c3ddb
SM
17331 lh->file_names
17332 = XRESIZEVEC (struct file_entry, lh->file_names, lh->file_names_size);
debd256d
JB
17333 }
17334
17335 fe = &lh->file_names[lh->num_file_names++];
17336 fe->name = name;
17337 fe->dir_index = dir_index;
17338 fe->mod_time = mod_time;
17339 fe->length = length;
aaa75496 17340 fe->included_p = 0;
cb1df416 17341 fe->symtab = NULL;
debd256d 17342}
6e70227d 17343
83769d0b 17344/* A convenience function to find the proper .debug_line section for a CU. */
36586728
TT
17345
17346static struct dwarf2_section_info *
17347get_debug_line_section (struct dwarf2_cu *cu)
17348{
17349 struct dwarf2_section_info *section;
17350
17351 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
17352 DWO file. */
17353 if (cu->dwo_unit && cu->per_cu->is_debug_types)
17354 section = &cu->dwo_unit->dwo_file->sections.line;
17355 else if (cu->per_cu->is_dwz)
17356 {
17357 struct dwz_file *dwz = dwarf2_get_dwz_file ();
17358
17359 section = &dwz->line;
17360 }
17361 else
17362 section = &dwarf2_per_objfile->line;
17363
17364 return section;
17365}
17366
debd256d 17367/* Read the statement program header starting at OFFSET in
3019eac3 17368 .debug_line, or .debug_line.dwo. Return a pointer
6502dd73 17369 to a struct line_header, allocated using xmalloc.
cd366ee8
DE
17370 Returns NULL if there is a problem reading the header, e.g., if it
17371 has a version we don't understand.
debd256d
JB
17372
17373 NOTE: the strings in the include directory and file name tables of
3019eac3
DE
17374 the returned object point into the dwarf line section buffer,
17375 and must not be freed. */
ae2de4f8 17376
debd256d 17377static struct line_header *
3019eac3 17378dwarf_decode_line_header (unsigned int offset, struct dwarf2_cu *cu)
debd256d
JB
17379{
17380 struct cleanup *back_to;
17381 struct line_header *lh;
d521ce57 17382 const gdb_byte *line_ptr;
c764a876 17383 unsigned int bytes_read, offset_size;
debd256d 17384 int i;
d521ce57 17385 const char *cur_dir, *cur_file;
3019eac3
DE
17386 struct dwarf2_section_info *section;
17387 bfd *abfd;
17388
36586728 17389 section = get_debug_line_section (cu);
3019eac3
DE
17390 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
17391 if (section->buffer == NULL)
debd256d 17392 {
3019eac3
DE
17393 if (cu->dwo_unit && cu->per_cu->is_debug_types)
17394 complaint (&symfile_complaints, _("missing .debug_line.dwo section"));
17395 else
17396 complaint (&symfile_complaints, _("missing .debug_line section"));
debd256d
JB
17397 return 0;
17398 }
17399
fceca515
DE
17400 /* We can't do this until we know the section is non-empty.
17401 Only then do we know we have such a section. */
a32a8923 17402 abfd = get_section_bfd_owner (section);
fceca515 17403
a738430d
MK
17404 /* Make sure that at least there's room for the total_length field.
17405 That could be 12 bytes long, but we're just going to fudge that. */
3019eac3 17406 if (offset + 4 >= section->size)
debd256d 17407 {
4d3c2250 17408 dwarf2_statement_list_fits_in_line_number_section_complaint ();
debd256d
JB
17409 return 0;
17410 }
17411
8d749320 17412 lh = XNEW (struct line_header);
debd256d
JB
17413 memset (lh, 0, sizeof (*lh));
17414 back_to = make_cleanup ((make_cleanup_ftype *) free_line_header,
17415 (void *) lh);
17416
527f3840
JK
17417 lh->offset.sect_off = offset;
17418 lh->offset_in_dwz = cu->per_cu->is_dwz;
17419
3019eac3 17420 line_ptr = section->buffer + offset;
debd256d 17421
a738430d 17422 /* Read in the header. */
6e70227d 17423 lh->total_length =
c764a876
DE
17424 read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
17425 &bytes_read, &offset_size);
debd256d 17426 line_ptr += bytes_read;
3019eac3 17427 if (line_ptr + lh->total_length > (section->buffer + section->size))
debd256d 17428 {
4d3c2250 17429 dwarf2_statement_list_fits_in_line_number_section_complaint ();
2f324bf6 17430 do_cleanups (back_to);
debd256d
JB
17431 return 0;
17432 }
17433 lh->statement_program_end = line_ptr + lh->total_length;
17434 lh->version = read_2_bytes (abfd, line_ptr);
17435 line_ptr += 2;
cd366ee8
DE
17436 if (lh->version > 4)
17437 {
17438 /* This is a version we don't understand. The format could have
17439 changed in ways we don't handle properly so just punt. */
17440 complaint (&symfile_complaints,
17441 _("unsupported version in .debug_line section"));
17442 return NULL;
17443 }
c764a876
DE
17444 lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
17445 line_ptr += offset_size;
debd256d
JB
17446 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
17447 line_ptr += 1;
2dc7f7b3
TT
17448 if (lh->version >= 4)
17449 {
17450 lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
17451 line_ptr += 1;
17452 }
17453 else
17454 lh->maximum_ops_per_instruction = 1;
17455
17456 if (lh->maximum_ops_per_instruction == 0)
17457 {
17458 lh->maximum_ops_per_instruction = 1;
17459 complaint (&symfile_complaints,
3e43a32a
MS
17460 _("invalid maximum_ops_per_instruction "
17461 "in `.debug_line' section"));
2dc7f7b3
TT
17462 }
17463
debd256d
JB
17464 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
17465 line_ptr += 1;
17466 lh->line_base = read_1_signed_byte (abfd, line_ptr);
17467 line_ptr += 1;
17468 lh->line_range = read_1_byte (abfd, line_ptr);
17469 line_ptr += 1;
17470 lh->opcode_base = read_1_byte (abfd, line_ptr);
17471 line_ptr += 1;
8d749320 17472 lh->standard_opcode_lengths = XNEWVEC (unsigned char, lh->opcode_base);
debd256d
JB
17473
17474 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
17475 for (i = 1; i < lh->opcode_base; ++i)
17476 {
17477 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
17478 line_ptr += 1;
17479 }
17480
a738430d 17481 /* Read directory table. */
9b1c24c8 17482 while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
debd256d
JB
17483 {
17484 line_ptr += bytes_read;
17485 add_include_dir (lh, cur_dir);
17486 }
17487 line_ptr += bytes_read;
17488
a738430d 17489 /* Read file name table. */
9b1c24c8 17490 while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
debd256d
JB
17491 {
17492 unsigned int dir_index, mod_time, length;
17493
17494 line_ptr += bytes_read;
17495 dir_index = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
17496 line_ptr += bytes_read;
17497 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
17498 line_ptr += bytes_read;
17499 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
17500 line_ptr += bytes_read;
17501
17502 add_file_name (lh, cur_file, dir_index, mod_time, length);
17503 }
17504 line_ptr += bytes_read;
6e70227d 17505 lh->statement_program_start = line_ptr;
debd256d 17506
3019eac3 17507 if (line_ptr > (section->buffer + section->size))
4d3c2250 17508 complaint (&symfile_complaints,
3e43a32a
MS
17509 _("line number info header doesn't "
17510 "fit in `.debug_line' section"));
debd256d
JB
17511
17512 discard_cleanups (back_to);
17513 return lh;
17514}
c906108c 17515
c6da4cef
DE
17516/* Subroutine of dwarf_decode_lines to simplify it.
17517 Return the file name of the psymtab for included file FILE_INDEX
17518 in line header LH of PST.
17519 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
17520 If space for the result is malloc'd, it will be freed by a cleanup.
1ed59174
JK
17521 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename.
17522
17523 The function creates dangling cleanup registration. */
c6da4cef 17524
d521ce57 17525static const char *
c6da4cef
DE
17526psymtab_include_file_name (const struct line_header *lh, int file_index,
17527 const struct partial_symtab *pst,
17528 const char *comp_dir)
17529{
17530 const struct file_entry fe = lh->file_names [file_index];
d521ce57
TT
17531 const char *include_name = fe.name;
17532 const char *include_name_to_compare = include_name;
17533 const char *dir_name = NULL;
72b9f47f
TT
17534 const char *pst_filename;
17535 char *copied_name = NULL;
c6da4cef
DE
17536 int file_is_pst;
17537
afa6c9ab 17538 if (fe.dir_index && lh->include_dirs != NULL)
c6da4cef
DE
17539 dir_name = lh->include_dirs[fe.dir_index - 1];
17540
17541 if (!IS_ABSOLUTE_PATH (include_name)
17542 && (dir_name != NULL || comp_dir != NULL))
17543 {
17544 /* Avoid creating a duplicate psymtab for PST.
17545 We do this by comparing INCLUDE_NAME and PST_FILENAME.
17546 Before we do the comparison, however, we need to account
17547 for DIR_NAME and COMP_DIR.
17548 First prepend dir_name (if non-NULL). If we still don't
17549 have an absolute path prepend comp_dir (if non-NULL).
17550 However, the directory we record in the include-file's
17551 psymtab does not contain COMP_DIR (to match the
17552 corresponding symtab(s)).
17553
17554 Example:
17555
17556 bash$ cd /tmp
17557 bash$ gcc -g ./hello.c
17558 include_name = "hello.c"
17559 dir_name = "."
17560 DW_AT_comp_dir = comp_dir = "/tmp"
5f52445b
YQ
17561 DW_AT_name = "./hello.c"
17562
17563 */
c6da4cef
DE
17564
17565 if (dir_name != NULL)
17566 {
d521ce57
TT
17567 char *tem = concat (dir_name, SLASH_STRING,
17568 include_name, (char *)NULL);
17569
17570 make_cleanup (xfree, tem);
17571 include_name = tem;
c6da4cef 17572 include_name_to_compare = include_name;
c6da4cef
DE
17573 }
17574 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
17575 {
d521ce57
TT
17576 char *tem = concat (comp_dir, SLASH_STRING,
17577 include_name, (char *)NULL);
17578
17579 make_cleanup (xfree, tem);
17580 include_name_to_compare = tem;
c6da4cef
DE
17581 }
17582 }
17583
17584 pst_filename = pst->filename;
17585 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
17586 {
72b9f47f
TT
17587 copied_name = concat (pst->dirname, SLASH_STRING,
17588 pst_filename, (char *)NULL);
17589 pst_filename = copied_name;
c6da4cef
DE
17590 }
17591
1e3fad37 17592 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
c6da4cef 17593
72b9f47f
TT
17594 if (copied_name != NULL)
17595 xfree (copied_name);
c6da4cef
DE
17596
17597 if (file_is_pst)
17598 return NULL;
17599 return include_name;
17600}
17601
d9b3de22
DE
17602/* State machine to track the state of the line number program. */
17603
17604typedef struct
17605{
17606 /* These are part of the standard DWARF line number state machine. */
17607
17608 unsigned char op_index;
17609 unsigned int file;
17610 unsigned int line;
17611 CORE_ADDR address;
17612 int is_stmt;
17613 unsigned int discriminator;
17614
17615 /* Additional bits of state we need to track. */
17616
17617 /* The last file that we called dwarf2_start_subfile for.
17618 This is only used for TLLs. */
17619 unsigned int last_file;
17620 /* The last file a line number was recorded for. */
17621 struct subfile *last_subfile;
17622
17623 /* The function to call to record a line. */
17624 record_line_ftype *record_line;
17625
17626 /* The last line number that was recorded, used to coalesce
17627 consecutive entries for the same line. This can happen, for
17628 example, when discriminators are present. PR 17276. */
17629 unsigned int last_line;
17630 int line_has_non_zero_discriminator;
17631} lnp_state_machine;
17632
17633/* There's a lot of static state to pass to dwarf_record_line.
17634 This keeps it all together. */
17635
17636typedef struct
17637{
17638 /* The gdbarch. */
17639 struct gdbarch *gdbarch;
17640
17641 /* The line number header. */
17642 struct line_header *line_header;
17643
17644 /* Non-zero if we're recording lines.
17645 Otherwise we're building partial symtabs and are just interested in
17646 finding include files mentioned by the line number program. */
17647 int record_lines_p;
17648} lnp_reader_state;
17649
c91513d8
PP
17650/* Ignore this record_line request. */
17651
17652static void
17653noop_record_line (struct subfile *subfile, int line, CORE_ADDR pc)
17654{
17655 return;
17656}
17657
a05a36a5
DE
17658/* Return non-zero if we should add LINE to the line number table.
17659 LINE is the line to add, LAST_LINE is the last line that was added,
17660 LAST_SUBFILE is the subfile for LAST_LINE.
17661 LINE_HAS_NON_ZERO_DISCRIMINATOR is non-zero if LINE has ever
17662 had a non-zero discriminator.
17663
17664 We have to be careful in the presence of discriminators.
17665 E.g., for this line:
17666
17667 for (i = 0; i < 100000; i++);
17668
17669 clang can emit four line number entries for that one line,
17670 each with a different discriminator.
17671 See gdb.dwarf2/dw2-single-line-discriminators.exp for an example.
17672
17673 However, we want gdb to coalesce all four entries into one.
17674 Otherwise the user could stepi into the middle of the line and
17675 gdb would get confused about whether the pc really was in the
17676 middle of the line.
17677
17678 Things are further complicated by the fact that two consecutive
17679 line number entries for the same line is a heuristic used by gcc
17680 to denote the end of the prologue. So we can't just discard duplicate
17681 entries, we have to be selective about it. The heuristic we use is
17682 that we only collapse consecutive entries for the same line if at least
17683 one of those entries has a non-zero discriminator. PR 17276.
17684
17685 Note: Addresses in the line number state machine can never go backwards
17686 within one sequence, thus this coalescing is ok. */
17687
17688static int
17689dwarf_record_line_p (unsigned int line, unsigned int last_line,
17690 int line_has_non_zero_discriminator,
17691 struct subfile *last_subfile)
17692{
17693 if (current_subfile != last_subfile)
17694 return 1;
17695 if (line != last_line)
17696 return 1;
17697 /* Same line for the same file that we've seen already.
17698 As a last check, for pr 17276, only record the line if the line
17699 has never had a non-zero discriminator. */
17700 if (!line_has_non_zero_discriminator)
17701 return 1;
17702 return 0;
17703}
17704
252a6764
DE
17705/* Use P_RECORD_LINE to record line number LINE beginning at address ADDRESS
17706 in the line table of subfile SUBFILE. */
17707
17708static void
d9b3de22
DE
17709dwarf_record_line_1 (struct gdbarch *gdbarch, struct subfile *subfile,
17710 unsigned int line, CORE_ADDR address,
17711 record_line_ftype p_record_line)
252a6764
DE
17712{
17713 CORE_ADDR addr = gdbarch_addr_bits_remove (gdbarch, address);
17714
27e0867f
DE
17715 if (dwarf_line_debug)
17716 {
17717 fprintf_unfiltered (gdb_stdlog,
17718 "Recording line %u, file %s, address %s\n",
17719 line, lbasename (subfile->name),
17720 paddress (gdbarch, address));
17721 }
17722
d5962de5 17723 (*p_record_line) (subfile, line, addr);
252a6764
DE
17724}
17725
17726/* Subroutine of dwarf_decode_lines_1 to simplify it.
17727 Mark the end of a set of line number records.
d9b3de22 17728 The arguments are the same as for dwarf_record_line_1.
252a6764
DE
17729 If SUBFILE is NULL the request is ignored. */
17730
17731static void
17732dwarf_finish_line (struct gdbarch *gdbarch, struct subfile *subfile,
17733 CORE_ADDR address, record_line_ftype p_record_line)
17734{
27e0867f
DE
17735 if (subfile == NULL)
17736 return;
17737
17738 if (dwarf_line_debug)
17739 {
17740 fprintf_unfiltered (gdb_stdlog,
17741 "Finishing current line, file %s, address %s\n",
17742 lbasename (subfile->name),
17743 paddress (gdbarch, address));
17744 }
17745
d9b3de22
DE
17746 dwarf_record_line_1 (gdbarch, subfile, 0, address, p_record_line);
17747}
17748
17749/* Record the line in STATE.
17750 END_SEQUENCE is non-zero if we're processing the end of a sequence. */
17751
17752static void
17753dwarf_record_line (lnp_reader_state *reader, lnp_state_machine *state,
17754 int end_sequence)
17755{
17756 const struct line_header *lh = reader->line_header;
17757 unsigned int file, line, discriminator;
17758 int is_stmt;
17759
17760 file = state->file;
17761 line = state->line;
17762 is_stmt = state->is_stmt;
17763 discriminator = state->discriminator;
17764
17765 if (dwarf_line_debug)
17766 {
17767 fprintf_unfiltered (gdb_stdlog,
17768 "Processing actual line %u: file %u,"
17769 " address %s, is_stmt %u, discrim %u\n",
17770 line, file,
17771 paddress (reader->gdbarch, state->address),
17772 is_stmt, discriminator);
17773 }
17774
17775 if (file == 0 || file - 1 >= lh->num_file_names)
17776 dwarf2_debug_line_missing_file_complaint ();
17777 /* For now we ignore lines not starting on an instruction boundary.
17778 But not when processing end_sequence for compatibility with the
17779 previous version of the code. */
17780 else if (state->op_index == 0 || end_sequence)
17781 {
17782 lh->file_names[file - 1].included_p = 1;
17783 if (reader->record_lines_p && is_stmt)
17784 {
e815d2d2 17785 if (state->last_subfile != current_subfile || end_sequence)
d9b3de22
DE
17786 {
17787 dwarf_finish_line (reader->gdbarch, state->last_subfile,
17788 state->address, state->record_line);
17789 }
17790
17791 if (!end_sequence)
17792 {
17793 if (dwarf_record_line_p (line, state->last_line,
17794 state->line_has_non_zero_discriminator,
17795 state->last_subfile))
17796 {
17797 dwarf_record_line_1 (reader->gdbarch, current_subfile,
17798 line, state->address,
17799 state->record_line);
17800 }
17801 state->last_subfile = current_subfile;
17802 state->last_line = line;
17803 }
17804 }
17805 }
17806}
17807
17808/* Initialize STATE for the start of a line number program. */
17809
17810static void
17811init_lnp_state_machine (lnp_state_machine *state,
17812 const lnp_reader_state *reader)
17813{
17814 memset (state, 0, sizeof (*state));
17815
17816 /* Just starting, there is no "last file". */
17817 state->last_file = 0;
17818 state->last_subfile = NULL;
17819
17820 state->record_line = record_line;
17821
17822 state->last_line = 0;
17823 state->line_has_non_zero_discriminator = 0;
17824
17825 /* Initialize these according to the DWARF spec. */
17826 state->op_index = 0;
17827 state->file = 1;
17828 state->line = 1;
17829 /* Call `gdbarch_adjust_dwarf2_line' on the initial 0 address as if there
17830 was a line entry for it so that the backend has a chance to adjust it
17831 and also record it in case it needs it. This is currently used by MIPS
17832 code, cf. `mips_adjust_dwarf2_line'. */
17833 state->address = gdbarch_adjust_dwarf2_line (reader->gdbarch, 0, 0);
17834 state->is_stmt = reader->line_header->default_is_stmt;
17835 state->discriminator = 0;
252a6764
DE
17836}
17837
924c2928
DE
17838/* Check address and if invalid nop-out the rest of the lines in this
17839 sequence. */
17840
17841static void
d9b3de22 17842check_line_address (struct dwarf2_cu *cu, lnp_state_machine *state,
924c2928
DE
17843 const gdb_byte *line_ptr,
17844 CORE_ADDR lowpc, CORE_ADDR address)
17845{
17846 /* If address < lowpc then it's not a usable value, it's outside the
17847 pc range of the CU. However, we restrict the test to only address
17848 values of zero to preserve GDB's previous behaviour which is to
17849 handle the specific case of a function being GC'd by the linker. */
17850
17851 if (address == 0 && address < lowpc)
17852 {
17853 /* This line table is for a function which has been
17854 GCd by the linker. Ignore it. PR gdb/12528 */
17855
17856 struct objfile *objfile = cu->objfile;
17857 long line_offset = line_ptr - get_debug_line_section (cu)->buffer;
17858
17859 complaint (&symfile_complaints,
17860 _(".debug_line address at offset 0x%lx is 0 [in module %s]"),
17861 line_offset, objfile_name (objfile));
d9b3de22
DE
17862 state->record_line = noop_record_line;
17863 /* Note: sm.record_line is left as noop_record_line
924c2928
DE
17864 until we see DW_LNE_end_sequence. */
17865 }
17866}
17867
f3f5162e 17868/* Subroutine of dwarf_decode_lines to simplify it.
d9b3de22
DE
17869 Process the line number information in LH.
17870 If DECODE_FOR_PST_P is non-zero, all we do is process the line number
17871 program in order to set included_p for every referenced header. */
debd256d 17872
c906108c 17873static void
43f3e411
DE
17874dwarf_decode_lines_1 (struct line_header *lh, struct dwarf2_cu *cu,
17875 const int decode_for_pst_p, CORE_ADDR lowpc)
c906108c 17876{
d521ce57
TT
17877 const gdb_byte *line_ptr, *extended_end;
17878 const gdb_byte *line_end;
a8c50c1f 17879 unsigned int bytes_read, extended_len;
699ca60a 17880 unsigned char op_code, extended_op;
e142c38c
DJ
17881 CORE_ADDR baseaddr;
17882 struct objfile *objfile = cu->objfile;
f3f5162e 17883 bfd *abfd = objfile->obfd;
fbf65064 17884 struct gdbarch *gdbarch = get_objfile_arch (objfile);
d9b3de22
DE
17885 /* Non-zero if we're recording line info (as opposed to building partial
17886 symtabs). */
17887 int record_lines_p = !decode_for_pst_p;
17888 /* A collection of things we need to pass to dwarf_record_line. */
17889 lnp_reader_state reader_state;
e142c38c
DJ
17890
17891 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 17892
debd256d
JB
17893 line_ptr = lh->statement_program_start;
17894 line_end = lh->statement_program_end;
c906108c 17895
d9b3de22
DE
17896 reader_state.gdbarch = gdbarch;
17897 reader_state.line_header = lh;
17898 reader_state.record_lines_p = record_lines_p;
17899
c906108c
SS
17900 /* Read the statement sequences until there's nothing left. */
17901 while (line_ptr < line_end)
17902 {
d9b3de22
DE
17903 /* The DWARF line number program state machine. */
17904 lnp_state_machine state_machine;
c906108c 17905 int end_sequence = 0;
d9b3de22
DE
17906
17907 /* Reset the state machine at the start of each sequence. */
17908 init_lnp_state_machine (&state_machine, &reader_state);
17909
17910 if (record_lines_p && lh->num_file_names >= state_machine.file)
c906108c 17911 {
aaa75496 17912 /* Start a subfile for the current file of the state machine. */
debd256d
JB
17913 /* lh->include_dirs and lh->file_names are 0-based, but the
17914 directory and file name numbers in the statement program
17915 are 1-based. */
d9b3de22 17916 struct file_entry *fe = &lh->file_names[state_machine.file - 1];
d521ce57 17917 const char *dir = NULL;
a738430d 17918
afa6c9ab 17919 if (fe->dir_index && lh->include_dirs != NULL)
debd256d 17920 dir = lh->include_dirs[fe->dir_index - 1];
4f1520fb 17921
4d663531 17922 dwarf2_start_subfile (fe->name, dir);
c906108c
SS
17923 }
17924
a738430d 17925 /* Decode the table. */
d9b3de22 17926 while (line_ptr < line_end && !end_sequence)
c906108c
SS
17927 {
17928 op_code = read_1_byte (abfd, line_ptr);
17929 line_ptr += 1;
9aa1fe7e 17930
debd256d 17931 if (op_code >= lh->opcode_base)
6e70227d 17932 {
8e07a239 17933 /* Special opcode. */
699ca60a 17934 unsigned char adj_opcode;
3e29f34a 17935 CORE_ADDR addr_adj;
a05a36a5 17936 int line_delta;
8e07a239 17937
debd256d 17938 adj_opcode = op_code - lh->opcode_base;
d9b3de22
DE
17939 addr_adj = (((state_machine.op_index
17940 + (adj_opcode / lh->line_range))
2dc7f7b3
TT
17941 / lh->maximum_ops_per_instruction)
17942 * lh->minimum_instruction_length);
d9b3de22
DE
17943 state_machine.address
17944 += gdbarch_adjust_dwarf2_line (gdbarch, addr_adj, 1);
17945 state_machine.op_index = ((state_machine.op_index
17946 + (adj_opcode / lh->line_range))
17947 % lh->maximum_ops_per_instruction);
a05a36a5 17948 line_delta = lh->line_base + (adj_opcode % lh->line_range);
d9b3de22 17949 state_machine.line += line_delta;
a05a36a5 17950 if (line_delta != 0)
d9b3de22
DE
17951 state_machine.line_has_non_zero_discriminator
17952 = state_machine.discriminator != 0;
17953
17954 dwarf_record_line (&reader_state, &state_machine, 0);
17955 state_machine.discriminator = 0;
9aa1fe7e
GK
17956 }
17957 else switch (op_code)
c906108c
SS
17958 {
17959 case DW_LNS_extended_op:
3e43a32a
MS
17960 extended_len = read_unsigned_leb128 (abfd, line_ptr,
17961 &bytes_read);
473b7be6 17962 line_ptr += bytes_read;
a8c50c1f 17963 extended_end = line_ptr + extended_len;
c906108c
SS
17964 extended_op = read_1_byte (abfd, line_ptr);
17965 line_ptr += 1;
17966 switch (extended_op)
17967 {
17968 case DW_LNE_end_sequence:
d9b3de22 17969 state_machine.record_line = record_line;
c906108c 17970 end_sequence = 1;
c906108c
SS
17971 break;
17972 case DW_LNE_set_address:
d9b3de22
DE
17973 {
17974 CORE_ADDR address
17975 = read_address (abfd, line_ptr, cu, &bytes_read);
17976
17977 line_ptr += bytes_read;
17978 check_line_address (cu, &state_machine, line_ptr,
17979 lowpc, address);
17980 state_machine.op_index = 0;
17981 address += baseaddr;
17982 state_machine.address
17983 = gdbarch_adjust_dwarf2_line (gdbarch, address, 0);
17984 }
c906108c
SS
17985 break;
17986 case DW_LNE_define_file:
debd256d 17987 {
d521ce57 17988 const char *cur_file;
debd256d 17989 unsigned int dir_index, mod_time, length;
6e70227d 17990
3e43a32a
MS
17991 cur_file = read_direct_string (abfd, line_ptr,
17992 &bytes_read);
debd256d
JB
17993 line_ptr += bytes_read;
17994 dir_index =
17995 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
17996 line_ptr += bytes_read;
17997 mod_time =
17998 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
17999 line_ptr += bytes_read;
18000 length =
18001 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
18002 line_ptr += bytes_read;
18003 add_file_name (lh, cur_file, dir_index, mod_time, length);
18004 }
c906108c 18005 break;
d0c6ba3d
CC
18006 case DW_LNE_set_discriminator:
18007 /* The discriminator is not interesting to the debugger;
a05a36a5
DE
18008 just ignore it. We still need to check its value though:
18009 if there are consecutive entries for the same
18010 (non-prologue) line we want to coalesce them.
18011 PR 17276. */
d9b3de22
DE
18012 state_machine.discriminator
18013 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
18014 state_machine.line_has_non_zero_discriminator
18015 |= state_machine.discriminator != 0;
a05a36a5 18016 line_ptr += bytes_read;
d0c6ba3d 18017 break;
c906108c 18018 default:
4d3c2250 18019 complaint (&symfile_complaints,
e2e0b3e5 18020 _("mangled .debug_line section"));
debd256d 18021 return;
c906108c 18022 }
a8c50c1f
DJ
18023 /* Make sure that we parsed the extended op correctly. If e.g.
18024 we expected a different address size than the producer used,
18025 we may have read the wrong number of bytes. */
18026 if (line_ptr != extended_end)
18027 {
18028 complaint (&symfile_complaints,
18029 _("mangled .debug_line section"));
18030 return;
18031 }
c906108c
SS
18032 break;
18033 case DW_LNS_copy:
d9b3de22
DE
18034 dwarf_record_line (&reader_state, &state_machine, 0);
18035 state_machine.discriminator = 0;
c906108c
SS
18036 break;
18037 case DW_LNS_advance_pc:
2dc7f7b3
TT
18038 {
18039 CORE_ADDR adjust
18040 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
3e29f34a 18041 CORE_ADDR addr_adj;
2dc7f7b3 18042
d9b3de22 18043 addr_adj = (((state_machine.op_index + adjust)
2dc7f7b3
TT
18044 / lh->maximum_ops_per_instruction)
18045 * lh->minimum_instruction_length);
d9b3de22
DE
18046 state_machine.address
18047 += gdbarch_adjust_dwarf2_line (gdbarch, addr_adj, 1);
18048 state_machine.op_index = ((state_machine.op_index + adjust)
18049 % lh->maximum_ops_per_instruction);
2dc7f7b3
TT
18050 line_ptr += bytes_read;
18051 }
c906108c
SS
18052 break;
18053 case DW_LNS_advance_line:
a05a36a5
DE
18054 {
18055 int line_delta
18056 = read_signed_leb128 (abfd, line_ptr, &bytes_read);
18057
d9b3de22 18058 state_machine.line += line_delta;
a05a36a5 18059 if (line_delta != 0)
d9b3de22
DE
18060 state_machine.line_has_non_zero_discriminator
18061 = state_machine.discriminator != 0;
a05a36a5
DE
18062 line_ptr += bytes_read;
18063 }
c906108c
SS
18064 break;
18065 case DW_LNS_set_file:
d9b3de22
DE
18066 {
18067 /* The arrays lh->include_dirs and lh->file_names are
18068 0-based, but the directory and file name numbers in
18069 the statement program are 1-based. */
18070 struct file_entry *fe;
18071 const char *dir = NULL;
18072
18073 state_machine.file = read_unsigned_leb128 (abfd, line_ptr,
18074 &bytes_read);
18075 line_ptr += bytes_read;
18076 if (state_machine.file == 0
18077 || state_machine.file - 1 >= lh->num_file_names)
18078 dwarf2_debug_line_missing_file_complaint ();
18079 else
18080 {
18081 fe = &lh->file_names[state_machine.file - 1];
18082 if (fe->dir_index && lh->include_dirs != NULL)
18083 dir = lh->include_dirs[fe->dir_index - 1];
18084 if (record_lines_p)
18085 {
18086 state_machine.last_subfile = current_subfile;
18087 state_machine.line_has_non_zero_discriminator
18088 = state_machine.discriminator != 0;
18089 dwarf2_start_subfile (fe->name, dir);
18090 }
18091 }
18092 }
c906108c
SS
18093 break;
18094 case DW_LNS_set_column:
0ad93d4f 18095 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
c906108c
SS
18096 line_ptr += bytes_read;
18097 break;
18098 case DW_LNS_negate_stmt:
d9b3de22 18099 state_machine.is_stmt = (!state_machine.is_stmt);
c906108c
SS
18100 break;
18101 case DW_LNS_set_basic_block:
c906108c 18102 break;
c2c6d25f
JM
18103 /* Add to the address register of the state machine the
18104 address increment value corresponding to special opcode
a738430d
MK
18105 255. I.e., this value is scaled by the minimum
18106 instruction length since special opcode 255 would have
b021a221 18107 scaled the increment. */
c906108c 18108 case DW_LNS_const_add_pc:
2dc7f7b3
TT
18109 {
18110 CORE_ADDR adjust = (255 - lh->opcode_base) / lh->line_range;
3e29f34a 18111 CORE_ADDR addr_adj;
2dc7f7b3 18112
d9b3de22 18113 addr_adj = (((state_machine.op_index + adjust)
2dc7f7b3
TT
18114 / lh->maximum_ops_per_instruction)
18115 * lh->minimum_instruction_length);
d9b3de22
DE
18116 state_machine.address
18117 += gdbarch_adjust_dwarf2_line (gdbarch, addr_adj, 1);
18118 state_machine.op_index = ((state_machine.op_index + adjust)
18119 % lh->maximum_ops_per_instruction);
2dc7f7b3 18120 }
c906108c
SS
18121 break;
18122 case DW_LNS_fixed_advance_pc:
3e29f34a
MR
18123 {
18124 CORE_ADDR addr_adj;
18125
18126 addr_adj = read_2_bytes (abfd, line_ptr);
d9b3de22
DE
18127 state_machine.address
18128 += gdbarch_adjust_dwarf2_line (gdbarch, addr_adj, 1);
18129 state_machine.op_index = 0;
3e29f34a
MR
18130 line_ptr += 2;
18131 }
c906108c 18132 break;
9aa1fe7e 18133 default:
a738430d
MK
18134 {
18135 /* Unknown standard opcode, ignore it. */
9aa1fe7e 18136 int i;
a738430d 18137
debd256d 18138 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
9aa1fe7e
GK
18139 {
18140 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
18141 line_ptr += bytes_read;
18142 }
18143 }
c906108c
SS
18144 }
18145 }
d9b3de22
DE
18146
18147 if (!end_sequence)
18148 dwarf2_debug_line_missing_end_sequence_complaint ();
18149
18150 /* We got a DW_LNE_end_sequence (or we ran off the end of the buffer,
18151 in which case we still finish recording the last line). */
18152 dwarf_record_line (&reader_state, &state_machine, 1);
c906108c 18153 }
f3f5162e
DE
18154}
18155
18156/* Decode the Line Number Program (LNP) for the given line_header
18157 structure and CU. The actual information extracted and the type
18158 of structures created from the LNP depends on the value of PST.
18159
18160 1. If PST is NULL, then this procedure uses the data from the program
18161 to create all necessary symbol tables, and their linetables.
18162
18163 2. If PST is not NULL, this procedure reads the program to determine
18164 the list of files included by the unit represented by PST, and
18165 builds all the associated partial symbol tables.
18166
18167 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
18168 It is used for relative paths in the line table.
18169 NOTE: When processing partial symtabs (pst != NULL),
18170 comp_dir == pst->dirname.
18171
18172 NOTE: It is important that psymtabs have the same file name (via strcmp)
18173 as the corresponding symtab. Since COMP_DIR is not used in the name of the
18174 symtab we don't use it in the name of the psymtabs we create.
18175 E.g. expand_line_sal requires this when finding psymtabs to expand.
c3b7b696
YQ
18176 A good testcase for this is mb-inline.exp.
18177
527f3840
JK
18178 LOWPC is the lowest address in CU (or 0 if not known).
18179
18180 Boolean DECODE_MAPPING specifies we need to fully decode .debug_line
18181 for its PC<->lines mapping information. Otherwise only the filename
18182 table is read in. */
f3f5162e
DE
18183
18184static void
18185dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
c3b7b696 18186 struct dwarf2_cu *cu, struct partial_symtab *pst,
527f3840 18187 CORE_ADDR lowpc, int decode_mapping)
f3f5162e
DE
18188{
18189 struct objfile *objfile = cu->objfile;
18190 const int decode_for_pst_p = (pst != NULL);
f3f5162e 18191
527f3840
JK
18192 if (decode_mapping)
18193 dwarf_decode_lines_1 (lh, cu, decode_for_pst_p, lowpc);
aaa75496
JB
18194
18195 if (decode_for_pst_p)
18196 {
18197 int file_index;
18198
18199 /* Now that we're done scanning the Line Header Program, we can
18200 create the psymtab of each included file. */
18201 for (file_index = 0; file_index < lh->num_file_names; file_index++)
18202 if (lh->file_names[file_index].included_p == 1)
18203 {
d521ce57 18204 const char *include_name =
c6da4cef
DE
18205 psymtab_include_file_name (lh, file_index, pst, comp_dir);
18206 if (include_name != NULL)
aaa75496
JB
18207 dwarf2_create_include_psymtab (include_name, pst, objfile);
18208 }
18209 }
cb1df416
DJ
18210 else
18211 {
18212 /* Make sure a symtab is created for every file, even files
18213 which contain only variables (i.e. no code with associated
18214 line numbers). */
43f3e411 18215 struct compunit_symtab *cust = buildsym_compunit_symtab ();
cb1df416 18216 int i;
cb1df416
DJ
18217
18218 for (i = 0; i < lh->num_file_names; i++)
18219 {
d521ce57 18220 const char *dir = NULL;
f3f5162e 18221 struct file_entry *fe;
9a619af0 18222
cb1df416 18223 fe = &lh->file_names[i];
afa6c9ab 18224 if (fe->dir_index && lh->include_dirs != NULL)
cb1df416 18225 dir = lh->include_dirs[fe->dir_index - 1];
4d663531 18226 dwarf2_start_subfile (fe->name, dir);
cb1df416 18227
cb1df416 18228 if (current_subfile->symtab == NULL)
43f3e411
DE
18229 {
18230 current_subfile->symtab
18231 = allocate_symtab (cust, current_subfile->name);
18232 }
cb1df416
DJ
18233 fe->symtab = current_subfile->symtab;
18234 }
18235 }
c906108c
SS
18236}
18237
18238/* Start a subfile for DWARF. FILENAME is the name of the file and
18239 DIRNAME the name of the source directory which contains FILENAME
4d663531 18240 or NULL if not known.
c906108c
SS
18241 This routine tries to keep line numbers from identical absolute and
18242 relative file names in a common subfile.
18243
18244 Using the `list' example from the GDB testsuite, which resides in
18245 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
18246 of /srcdir/list0.c yields the following debugging information for list0.c:
18247
c5aa993b 18248 DW_AT_name: /srcdir/list0.c
4d663531 18249 DW_AT_comp_dir: /compdir
357e46e7 18250 files.files[0].name: list0.h
c5aa993b 18251 files.files[0].dir: /srcdir
357e46e7 18252 files.files[1].name: list0.c
c5aa993b 18253 files.files[1].dir: /srcdir
c906108c
SS
18254
18255 The line number information for list0.c has to end up in a single
4f1520fb
FR
18256 subfile, so that `break /srcdir/list0.c:1' works as expected.
18257 start_subfile will ensure that this happens provided that we pass the
18258 concatenation of files.files[1].dir and files.files[1].name as the
18259 subfile's name. */
c906108c
SS
18260
18261static void
4d663531 18262dwarf2_start_subfile (const char *filename, const char *dirname)
c906108c 18263{
d521ce57 18264 char *copy = NULL;
4f1520fb 18265
4d663531 18266 /* In order not to lose the line information directory,
4f1520fb
FR
18267 we concatenate it to the filename when it makes sense.
18268 Note that the Dwarf3 standard says (speaking of filenames in line
18269 information): ``The directory index is ignored for file names
18270 that represent full path names''. Thus ignoring dirname in the
18271 `else' branch below isn't an issue. */
c906108c 18272
d5166ae1 18273 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
d521ce57
TT
18274 {
18275 copy = concat (dirname, SLASH_STRING, filename, (char *)NULL);
18276 filename = copy;
18277 }
c906108c 18278
4d663531 18279 start_subfile (filename);
4f1520fb 18280
d521ce57
TT
18281 if (copy != NULL)
18282 xfree (copy);
c906108c
SS
18283}
18284
f4dc4d17
DE
18285/* Start a symtab for DWARF.
18286 NAME, COMP_DIR, LOW_PC are passed to start_symtab. */
18287
43f3e411 18288static struct compunit_symtab *
f4dc4d17 18289dwarf2_start_symtab (struct dwarf2_cu *cu,
15d034d0 18290 const char *name, const char *comp_dir, CORE_ADDR low_pc)
f4dc4d17 18291{
43f3e411
DE
18292 struct compunit_symtab *cust
18293 = start_symtab (cu->objfile, name, comp_dir, low_pc);
18294
f4dc4d17
DE
18295 record_debugformat ("DWARF 2");
18296 record_producer (cu->producer);
18297
18298 /* We assume that we're processing GCC output. */
18299 processing_gcc_compilation = 2;
18300
4d4ec4e5 18301 cu->processing_has_namespace_info = 0;
43f3e411
DE
18302
18303 return cust;
f4dc4d17
DE
18304}
18305
4c2df51b
DJ
18306static void
18307var_decode_location (struct attribute *attr, struct symbol *sym,
e7c27a73 18308 struct dwarf2_cu *cu)
4c2df51b 18309{
e7c27a73
DJ
18310 struct objfile *objfile = cu->objfile;
18311 struct comp_unit_head *cu_header = &cu->header;
18312
4c2df51b
DJ
18313 /* NOTE drow/2003-01-30: There used to be a comment and some special
18314 code here to turn a symbol with DW_AT_external and a
18315 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
18316 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
18317 with some versions of binutils) where shared libraries could have
18318 relocations against symbols in their debug information - the
18319 minimal symbol would have the right address, but the debug info
18320 would not. It's no longer necessary, because we will explicitly
18321 apply relocations when we read in the debug information now. */
18322
18323 /* A DW_AT_location attribute with no contents indicates that a
18324 variable has been optimized away. */
18325 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
18326 {
f1e6e072 18327 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
4c2df51b
DJ
18328 return;
18329 }
18330
18331 /* Handle one degenerate form of location expression specially, to
18332 preserve GDB's previous behavior when section offsets are
3019eac3
DE
18333 specified. If this is just a DW_OP_addr or DW_OP_GNU_addr_index
18334 then mark this symbol as LOC_STATIC. */
4c2df51b
DJ
18335
18336 if (attr_form_is_block (attr)
3019eac3
DE
18337 && ((DW_BLOCK (attr)->data[0] == DW_OP_addr
18338 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size)
18339 || (DW_BLOCK (attr)->data[0] == DW_OP_GNU_addr_index
18340 && (DW_BLOCK (attr)->size
18341 == 1 + leb128_size (&DW_BLOCK (attr)->data[1])))))
4c2df51b 18342 {
891d2f0b 18343 unsigned int dummy;
4c2df51b 18344
3019eac3
DE
18345 if (DW_BLOCK (attr)->data[0] == DW_OP_addr)
18346 SYMBOL_VALUE_ADDRESS (sym) =
18347 read_address (objfile->obfd, DW_BLOCK (attr)->data + 1, cu, &dummy);
18348 else
18349 SYMBOL_VALUE_ADDRESS (sym) =
18350 read_addr_index_from_leb128 (cu, DW_BLOCK (attr)->data + 1, &dummy);
f1e6e072 18351 SYMBOL_ACLASS_INDEX (sym) = LOC_STATIC;
4c2df51b
DJ
18352 fixup_symbol_section (sym, objfile);
18353 SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (objfile->section_offsets,
18354 SYMBOL_SECTION (sym));
4c2df51b
DJ
18355 return;
18356 }
18357
18358 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
18359 expression evaluator, and use LOC_COMPUTED only when necessary
18360 (i.e. when the value of a register or memory location is
18361 referenced, or a thread-local block, etc.). Then again, it might
18362 not be worthwhile. I'm assuming that it isn't unless performance
18363 or memory numbers show me otherwise. */
18364
f1e6e072 18365 dwarf2_symbol_mark_computed (attr, sym, cu, 0);
8be455d7 18366
f1e6e072 18367 if (SYMBOL_COMPUTED_OPS (sym)->location_has_loclist)
8be455d7 18368 cu->has_loclist = 1;
4c2df51b
DJ
18369}
18370
c906108c
SS
18371/* Given a pointer to a DWARF information entry, figure out if we need
18372 to make a symbol table entry for it, and if so, create a new entry
18373 and return a pointer to it.
18374 If TYPE is NULL, determine symbol type from the die, otherwise
34eaf542
TT
18375 used the passed type.
18376 If SPACE is not NULL, use it to hold the new symbol. If it is
18377 NULL, allocate a new symbol on the objfile's obstack. */
c906108c
SS
18378
18379static struct symbol *
34eaf542
TT
18380new_symbol_full (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
18381 struct symbol *space)
c906108c 18382{
e7c27a73 18383 struct objfile *objfile = cu->objfile;
3e29f34a 18384 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c 18385 struct symbol *sym = NULL;
15d034d0 18386 const char *name;
c906108c
SS
18387 struct attribute *attr = NULL;
18388 struct attribute *attr2 = NULL;
e142c38c 18389 CORE_ADDR baseaddr;
e37fd15a
SW
18390 struct pending **list_to_add = NULL;
18391
edb3359d 18392 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
e142c38c
DJ
18393
18394 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 18395
94af9270 18396 name = dwarf2_name (die, cu);
c906108c
SS
18397 if (name)
18398 {
94af9270 18399 const char *linkagename;
34eaf542 18400 int suppress_add = 0;
94af9270 18401
34eaf542
TT
18402 if (space)
18403 sym = space;
18404 else
e623cf5d 18405 sym = allocate_symbol (objfile);
c906108c 18406 OBJSTAT (objfile, n_syms++);
2de7ced7
DJ
18407
18408 /* Cache this symbol's name and the name's demangled form (if any). */
f85f34ed 18409 SYMBOL_SET_LANGUAGE (sym, cu->language, &objfile->objfile_obstack);
94af9270
KS
18410 linkagename = dwarf2_physname (name, die, cu);
18411 SYMBOL_SET_NAMES (sym, linkagename, strlen (linkagename), 0, objfile);
c906108c 18412
f55ee35c
JK
18413 /* Fortran does not have mangling standard and the mangling does differ
18414 between gfortran, iFort etc. */
18415 if (cu->language == language_fortran
b250c185 18416 && symbol_get_demangled_name (&(sym->ginfo)) == NULL)
29df156d 18417 symbol_set_demangled_name (&(sym->ginfo),
cfc594ee 18418 dwarf2_full_name (name, die, cu),
29df156d 18419 NULL);
f55ee35c 18420
c906108c 18421 /* Default assumptions.
c5aa993b 18422 Use the passed type or decode it from the die. */
176620f1 18423 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
f1e6e072 18424 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
c906108c
SS
18425 if (type != NULL)
18426 SYMBOL_TYPE (sym) = type;
18427 else
e7c27a73 18428 SYMBOL_TYPE (sym) = die_type (die, cu);
edb3359d
DJ
18429 attr = dwarf2_attr (die,
18430 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
18431 cu);
c906108c
SS
18432 if (attr)
18433 {
18434 SYMBOL_LINE (sym) = DW_UNSND (attr);
18435 }
cb1df416 18436
edb3359d
DJ
18437 attr = dwarf2_attr (die,
18438 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
18439 cu);
cb1df416
DJ
18440 if (attr)
18441 {
18442 int file_index = DW_UNSND (attr);
9a619af0 18443
cb1df416
DJ
18444 if (cu->line_header == NULL
18445 || file_index > cu->line_header->num_file_names)
18446 complaint (&symfile_complaints,
18447 _("file index out of range"));
1c3d648d 18448 else if (file_index > 0)
cb1df416
DJ
18449 {
18450 struct file_entry *fe;
9a619af0 18451
cb1df416 18452 fe = &cu->line_header->file_names[file_index - 1];
08be3fe3 18453 symbol_set_symtab (sym, fe->symtab);
cb1df416
DJ
18454 }
18455 }
18456
c906108c
SS
18457 switch (die->tag)
18458 {
18459 case DW_TAG_label:
e142c38c 18460 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
c906108c 18461 if (attr)
3e29f34a
MR
18462 {
18463 CORE_ADDR addr;
18464
18465 addr = attr_value_as_address (attr);
18466 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr + baseaddr);
18467 SYMBOL_VALUE_ADDRESS (sym) = addr;
18468 }
0f5238ed
TT
18469 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
18470 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
f1e6e072 18471 SYMBOL_ACLASS_INDEX (sym) = LOC_LABEL;
0f5238ed 18472 add_symbol_to_list (sym, cu->list_in_scope);
c906108c
SS
18473 break;
18474 case DW_TAG_subprogram:
18475 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
18476 finish_block. */
f1e6e072 18477 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
e142c38c 18478 attr2 = dwarf2_attr (die, DW_AT_external, cu);
2cfa0c8d
JB
18479 if ((attr2 && (DW_UNSND (attr2) != 0))
18480 || cu->language == language_ada)
c906108c 18481 {
2cfa0c8d
JB
18482 /* Subprograms marked external are stored as a global symbol.
18483 Ada subprograms, whether marked external or not, are always
18484 stored as a global symbol, because we want to be able to
18485 access them globally. For instance, we want to be able
18486 to break on a nested subprogram without having to
18487 specify the context. */
e37fd15a 18488 list_to_add = &global_symbols;
c906108c
SS
18489 }
18490 else
18491 {
e37fd15a 18492 list_to_add = cu->list_in_scope;
c906108c
SS
18493 }
18494 break;
edb3359d
DJ
18495 case DW_TAG_inlined_subroutine:
18496 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
18497 finish_block. */
f1e6e072 18498 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
edb3359d 18499 SYMBOL_INLINED (sym) = 1;
481860b3 18500 list_to_add = cu->list_in_scope;
edb3359d 18501 break;
34eaf542
TT
18502 case DW_TAG_template_value_param:
18503 suppress_add = 1;
18504 /* Fall through. */
72929c62 18505 case DW_TAG_constant:
c906108c 18506 case DW_TAG_variable:
254e6b9e 18507 case DW_TAG_member:
0963b4bd
MS
18508 /* Compilation with minimal debug info may result in
18509 variables with missing type entries. Change the
18510 misleading `void' type to something sensible. */
c906108c 18511 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
64c50499 18512 SYMBOL_TYPE (sym)
46bf5051 18513 = objfile_type (objfile)->nodebug_data_symbol;
64c50499 18514
e142c38c 18515 attr = dwarf2_attr (die, DW_AT_const_value, cu);
254e6b9e
DE
18516 /* In the case of DW_TAG_member, we should only be called for
18517 static const members. */
18518 if (die->tag == DW_TAG_member)
18519 {
3863f96c
DE
18520 /* dwarf2_add_field uses die_is_declaration,
18521 so we do the same. */
254e6b9e
DE
18522 gdb_assert (die_is_declaration (die, cu));
18523 gdb_assert (attr);
18524 }
c906108c
SS
18525 if (attr)
18526 {
e7c27a73 18527 dwarf2_const_value (attr, sym, cu);
e142c38c 18528 attr2 = dwarf2_attr (die, DW_AT_external, cu);
e37fd15a 18529 if (!suppress_add)
34eaf542
TT
18530 {
18531 if (attr2 && (DW_UNSND (attr2) != 0))
e37fd15a 18532 list_to_add = &global_symbols;
34eaf542 18533 else
e37fd15a 18534 list_to_add = cu->list_in_scope;
34eaf542 18535 }
c906108c
SS
18536 break;
18537 }
e142c38c 18538 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
18539 if (attr)
18540 {
e7c27a73 18541 var_decode_location (attr, sym, cu);
e142c38c 18542 attr2 = dwarf2_attr (die, DW_AT_external, cu);
4357ac6c
TT
18543
18544 /* Fortran explicitly imports any global symbols to the local
18545 scope by DW_TAG_common_block. */
18546 if (cu->language == language_fortran && die->parent
18547 && die->parent->tag == DW_TAG_common_block)
18548 attr2 = NULL;
18549
caac4577
JG
18550 if (SYMBOL_CLASS (sym) == LOC_STATIC
18551 && SYMBOL_VALUE_ADDRESS (sym) == 0
18552 && !dwarf2_per_objfile->has_section_at_zero)
18553 {
18554 /* When a static variable is eliminated by the linker,
18555 the corresponding debug information is not stripped
18556 out, but the variable address is set to null;
18557 do not add such variables into symbol table. */
18558 }
18559 else if (attr2 && (DW_UNSND (attr2) != 0))
1c809c68 18560 {
f55ee35c
JK
18561 /* Workaround gfortran PR debug/40040 - it uses
18562 DW_AT_location for variables in -fPIC libraries which may
18563 get overriden by other libraries/executable and get
18564 a different address. Resolve it by the minimal symbol
18565 which may come from inferior's executable using copy
18566 relocation. Make this workaround only for gfortran as for
18567 other compilers GDB cannot guess the minimal symbol
18568 Fortran mangling kind. */
18569 if (cu->language == language_fortran && die->parent
18570 && die->parent->tag == DW_TAG_module
18571 && cu->producer
28586665 18572 && startswith (cu->producer, "GNU Fortran"))
f1e6e072 18573 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
f55ee35c 18574
1c809c68
TT
18575 /* A variable with DW_AT_external is never static,
18576 but it may be block-scoped. */
18577 list_to_add = (cu->list_in_scope == &file_symbols
18578 ? &global_symbols : cu->list_in_scope);
1c809c68 18579 }
c906108c 18580 else
e37fd15a 18581 list_to_add = cu->list_in_scope;
c906108c
SS
18582 }
18583 else
18584 {
18585 /* We do not know the address of this symbol.
c5aa993b
JM
18586 If it is an external symbol and we have type information
18587 for it, enter the symbol as a LOC_UNRESOLVED symbol.
18588 The address of the variable will then be determined from
18589 the minimal symbol table whenever the variable is
18590 referenced. */
e142c38c 18591 attr2 = dwarf2_attr (die, DW_AT_external, cu);
0971de02
TT
18592
18593 /* Fortran explicitly imports any global symbols to the local
18594 scope by DW_TAG_common_block. */
18595 if (cu->language == language_fortran && die->parent
18596 && die->parent->tag == DW_TAG_common_block)
18597 {
18598 /* SYMBOL_CLASS doesn't matter here because
18599 read_common_block is going to reset it. */
18600 if (!suppress_add)
18601 list_to_add = cu->list_in_scope;
18602 }
18603 else if (attr2 && (DW_UNSND (attr2) != 0)
18604 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
c906108c 18605 {
0fe7935b
DJ
18606 /* A variable with DW_AT_external is never static, but it
18607 may be block-scoped. */
18608 list_to_add = (cu->list_in_scope == &file_symbols
18609 ? &global_symbols : cu->list_in_scope);
18610
f1e6e072 18611 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
c906108c 18612 }
442ddf59
JK
18613 else if (!die_is_declaration (die, cu))
18614 {
18615 /* Use the default LOC_OPTIMIZED_OUT class. */
18616 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
e37fd15a
SW
18617 if (!suppress_add)
18618 list_to_add = cu->list_in_scope;
442ddf59 18619 }
c906108c
SS
18620 }
18621 break;
18622 case DW_TAG_formal_parameter:
edb3359d
DJ
18623 /* If we are inside a function, mark this as an argument. If
18624 not, we might be looking at an argument to an inlined function
18625 when we do not have enough information to show inlined frames;
18626 pretend it's a local variable in that case so that the user can
18627 still see it. */
18628 if (context_stack_depth > 0
18629 && context_stack[context_stack_depth - 1].name != NULL)
18630 SYMBOL_IS_ARGUMENT (sym) = 1;
e142c38c 18631 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
18632 if (attr)
18633 {
e7c27a73 18634 var_decode_location (attr, sym, cu);
c906108c 18635 }
e142c38c 18636 attr = dwarf2_attr (die, DW_AT_const_value, cu);
c906108c
SS
18637 if (attr)
18638 {
e7c27a73 18639 dwarf2_const_value (attr, sym, cu);
c906108c 18640 }
f346a30d 18641
e37fd15a 18642 list_to_add = cu->list_in_scope;
c906108c
SS
18643 break;
18644 case DW_TAG_unspecified_parameters:
18645 /* From varargs functions; gdb doesn't seem to have any
18646 interest in this information, so just ignore it for now.
18647 (FIXME?) */
18648 break;
34eaf542
TT
18649 case DW_TAG_template_type_param:
18650 suppress_add = 1;
18651 /* Fall through. */
c906108c 18652 case DW_TAG_class_type:
680b30c7 18653 case DW_TAG_interface_type:
c906108c
SS
18654 case DW_TAG_structure_type:
18655 case DW_TAG_union_type:
72019c9c 18656 case DW_TAG_set_type:
c906108c 18657 case DW_TAG_enumeration_type:
f1e6e072 18658 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
176620f1 18659 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
c906108c 18660
63d06c5c 18661 {
987504bb 18662 /* NOTE: carlton/2003-11-10: C++ and Java class symbols shouldn't
63d06c5c
DC
18663 really ever be static objects: otherwise, if you try
18664 to, say, break of a class's method and you're in a file
18665 which doesn't mention that class, it won't work unless
18666 the check for all static symbols in lookup_symbol_aux
18667 saves you. See the OtherFileClass tests in
18668 gdb.c++/namespace.exp. */
18669
e37fd15a 18670 if (!suppress_add)
34eaf542 18671 {
34eaf542
TT
18672 list_to_add = (cu->list_in_scope == &file_symbols
18673 && (cu->language == language_cplus
18674 || cu->language == language_java)
18675 ? &global_symbols : cu->list_in_scope);
63d06c5c 18676
64382290
TT
18677 /* The semantics of C++ state that "struct foo {
18678 ... }" also defines a typedef for "foo". A Java
18679 class declaration also defines a typedef for the
18680 class. */
18681 if (cu->language == language_cplus
18682 || cu->language == language_java
45280282 18683 || cu->language == language_ada
c44af4eb
TT
18684 || cu->language == language_d
18685 || cu->language == language_rust)
64382290
TT
18686 {
18687 /* The symbol's name is already allocated along
18688 with this objfile, so we don't need to
18689 duplicate it for the type. */
18690 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
18691 TYPE_NAME (SYMBOL_TYPE (sym)) = SYMBOL_SEARCH_NAME (sym);
18692 }
63d06c5c
DC
18693 }
18694 }
c906108c
SS
18695 break;
18696 case DW_TAG_typedef:
f1e6e072 18697 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
63d06c5c 18698 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 18699 list_to_add = cu->list_in_scope;
63d06c5c 18700 break;
c906108c 18701 case DW_TAG_base_type:
a02abb62 18702 case DW_TAG_subrange_type:
f1e6e072 18703 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
176620f1 18704 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 18705 list_to_add = cu->list_in_scope;
c906108c
SS
18706 break;
18707 case DW_TAG_enumerator:
e142c38c 18708 attr = dwarf2_attr (die, DW_AT_const_value, cu);
c906108c
SS
18709 if (attr)
18710 {
e7c27a73 18711 dwarf2_const_value (attr, sym, cu);
c906108c 18712 }
63d06c5c
DC
18713 {
18714 /* NOTE: carlton/2003-11-10: See comment above in the
18715 DW_TAG_class_type, etc. block. */
18716
e142c38c 18717 list_to_add = (cu->list_in_scope == &file_symbols
987504bb
JJ
18718 && (cu->language == language_cplus
18719 || cu->language == language_java)
e142c38c 18720 ? &global_symbols : cu->list_in_scope);
63d06c5c 18721 }
c906108c 18722 break;
74921315 18723 case DW_TAG_imported_declaration:
5c4e30ca 18724 case DW_TAG_namespace:
f1e6e072 18725 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
e37fd15a 18726 list_to_add = &global_symbols;
5c4e30ca 18727 break;
530e8392
KB
18728 case DW_TAG_module:
18729 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
18730 SYMBOL_DOMAIN (sym) = MODULE_DOMAIN;
18731 list_to_add = &global_symbols;
18732 break;
4357ac6c 18733 case DW_TAG_common_block:
f1e6e072 18734 SYMBOL_ACLASS_INDEX (sym) = LOC_COMMON_BLOCK;
4357ac6c
TT
18735 SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
18736 add_symbol_to_list (sym, cu->list_in_scope);
18737 break;
c906108c
SS
18738 default:
18739 /* Not a tag we recognize. Hopefully we aren't processing
18740 trash data, but since we must specifically ignore things
18741 we don't recognize, there is nothing else we should do at
0963b4bd 18742 this point. */
e2e0b3e5 18743 complaint (&symfile_complaints, _("unsupported tag: '%s'"),
4d3c2250 18744 dwarf_tag_name (die->tag));
c906108c
SS
18745 break;
18746 }
df8a16a1 18747
e37fd15a
SW
18748 if (suppress_add)
18749 {
18750 sym->hash_next = objfile->template_symbols;
18751 objfile->template_symbols = sym;
18752 list_to_add = NULL;
18753 }
18754
18755 if (list_to_add != NULL)
18756 add_symbol_to_list (sym, list_to_add);
18757
df8a16a1
DJ
18758 /* For the benefit of old versions of GCC, check for anonymous
18759 namespaces based on the demangled name. */
4d4ec4e5 18760 if (!cu->processing_has_namespace_info
94af9270 18761 && cu->language == language_cplus)
a10964d1 18762 cp_scan_for_anonymous_namespaces (sym, objfile);
c906108c
SS
18763 }
18764 return (sym);
18765}
18766
34eaf542
TT
18767/* A wrapper for new_symbol_full that always allocates a new symbol. */
18768
18769static struct symbol *
18770new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
18771{
18772 return new_symbol_full (die, type, cu, NULL);
18773}
18774
98bfdba5
PA
18775/* Given an attr with a DW_FORM_dataN value in host byte order,
18776 zero-extend it as appropriate for the symbol's type. The DWARF
18777 standard (v4) is not entirely clear about the meaning of using
18778 DW_FORM_dataN for a constant with a signed type, where the type is
18779 wider than the data. The conclusion of a discussion on the DWARF
18780 list was that this is unspecified. We choose to always zero-extend
18781 because that is the interpretation long in use by GCC. */
c906108c 18782
98bfdba5 18783static gdb_byte *
ff39bb5e 18784dwarf2_const_value_data (const struct attribute *attr, struct obstack *obstack,
12df843f 18785 struct dwarf2_cu *cu, LONGEST *value, int bits)
c906108c 18786{
e7c27a73 18787 struct objfile *objfile = cu->objfile;
e17a4113
UW
18788 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
18789 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
98bfdba5
PA
18790 LONGEST l = DW_UNSND (attr);
18791
18792 if (bits < sizeof (*value) * 8)
18793 {
18794 l &= ((LONGEST) 1 << bits) - 1;
18795 *value = l;
18796 }
18797 else if (bits == sizeof (*value) * 8)
18798 *value = l;
18799 else
18800 {
224c3ddb 18801 gdb_byte *bytes = (gdb_byte *) obstack_alloc (obstack, bits / 8);
98bfdba5
PA
18802 store_unsigned_integer (bytes, bits / 8, byte_order, l);
18803 return bytes;
18804 }
18805
18806 return NULL;
18807}
18808
18809/* Read a constant value from an attribute. Either set *VALUE, or if
18810 the value does not fit in *VALUE, set *BYTES - either already
18811 allocated on the objfile obstack, or newly allocated on OBSTACK,
18812 or, set *BATON, if we translated the constant to a location
18813 expression. */
18814
18815static void
ff39bb5e 18816dwarf2_const_value_attr (const struct attribute *attr, struct type *type,
98bfdba5
PA
18817 const char *name, struct obstack *obstack,
18818 struct dwarf2_cu *cu,
d521ce57 18819 LONGEST *value, const gdb_byte **bytes,
98bfdba5
PA
18820 struct dwarf2_locexpr_baton **baton)
18821{
18822 struct objfile *objfile = cu->objfile;
18823 struct comp_unit_head *cu_header = &cu->header;
c906108c 18824 struct dwarf_block *blk;
98bfdba5
PA
18825 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
18826 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
18827
18828 *value = 0;
18829 *bytes = NULL;
18830 *baton = NULL;
c906108c
SS
18831
18832 switch (attr->form)
18833 {
18834 case DW_FORM_addr:
3019eac3 18835 case DW_FORM_GNU_addr_index:
ac56253d 18836 {
ac56253d
TT
18837 gdb_byte *data;
18838
98bfdba5
PA
18839 if (TYPE_LENGTH (type) != cu_header->addr_size)
18840 dwarf2_const_value_length_mismatch_complaint (name,
ac56253d 18841 cu_header->addr_size,
98bfdba5 18842 TYPE_LENGTH (type));
ac56253d
TT
18843 /* Symbols of this form are reasonably rare, so we just
18844 piggyback on the existing location code rather than writing
18845 a new implementation of symbol_computed_ops. */
8d749320 18846 *baton = XOBNEW (obstack, struct dwarf2_locexpr_baton);
98bfdba5
PA
18847 (*baton)->per_cu = cu->per_cu;
18848 gdb_assert ((*baton)->per_cu);
ac56253d 18849
98bfdba5 18850 (*baton)->size = 2 + cu_header->addr_size;
224c3ddb 18851 data = (gdb_byte *) obstack_alloc (obstack, (*baton)->size);
98bfdba5 18852 (*baton)->data = data;
ac56253d
TT
18853
18854 data[0] = DW_OP_addr;
18855 store_unsigned_integer (&data[1], cu_header->addr_size,
18856 byte_order, DW_ADDR (attr));
18857 data[cu_header->addr_size + 1] = DW_OP_stack_value;
ac56253d 18858 }
c906108c 18859 break;
4ac36638 18860 case DW_FORM_string:
93b5768b 18861 case DW_FORM_strp:
3019eac3 18862 case DW_FORM_GNU_str_index:
36586728 18863 case DW_FORM_GNU_strp_alt:
98bfdba5
PA
18864 /* DW_STRING is already allocated on the objfile obstack, point
18865 directly to it. */
d521ce57 18866 *bytes = (const gdb_byte *) DW_STRING (attr);
93b5768b 18867 break;
c906108c
SS
18868 case DW_FORM_block1:
18869 case DW_FORM_block2:
18870 case DW_FORM_block4:
18871 case DW_FORM_block:
2dc7f7b3 18872 case DW_FORM_exprloc:
c906108c 18873 blk = DW_BLOCK (attr);
98bfdba5
PA
18874 if (TYPE_LENGTH (type) != blk->size)
18875 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
18876 TYPE_LENGTH (type));
18877 *bytes = blk->data;
c906108c 18878 break;
2df3850c
JM
18879
18880 /* The DW_AT_const_value attributes are supposed to carry the
18881 symbol's value "represented as it would be on the target
18882 architecture." By the time we get here, it's already been
18883 converted to host endianness, so we just need to sign- or
18884 zero-extend it as appropriate. */
18885 case DW_FORM_data1:
3aef2284 18886 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 8);
2df3850c 18887 break;
c906108c 18888 case DW_FORM_data2:
3aef2284 18889 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 16);
2df3850c 18890 break;
c906108c 18891 case DW_FORM_data4:
3aef2284 18892 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 32);
2df3850c 18893 break;
c906108c 18894 case DW_FORM_data8:
3aef2284 18895 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 64);
2df3850c
JM
18896 break;
18897
c906108c 18898 case DW_FORM_sdata:
98bfdba5 18899 *value = DW_SND (attr);
2df3850c
JM
18900 break;
18901
c906108c 18902 case DW_FORM_udata:
98bfdba5 18903 *value = DW_UNSND (attr);
c906108c 18904 break;
2df3850c 18905
c906108c 18906 default:
4d3c2250 18907 complaint (&symfile_complaints,
e2e0b3e5 18908 _("unsupported const value attribute form: '%s'"),
4d3c2250 18909 dwarf_form_name (attr->form));
98bfdba5 18910 *value = 0;
c906108c
SS
18911 break;
18912 }
18913}
18914
2df3850c 18915
98bfdba5
PA
18916/* Copy constant value from an attribute to a symbol. */
18917
2df3850c 18918static void
ff39bb5e 18919dwarf2_const_value (const struct attribute *attr, struct symbol *sym,
98bfdba5 18920 struct dwarf2_cu *cu)
2df3850c 18921{
98bfdba5 18922 struct objfile *objfile = cu->objfile;
12df843f 18923 LONGEST value;
d521ce57 18924 const gdb_byte *bytes;
98bfdba5 18925 struct dwarf2_locexpr_baton *baton;
2df3850c 18926
98bfdba5
PA
18927 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
18928 SYMBOL_PRINT_NAME (sym),
18929 &objfile->objfile_obstack, cu,
18930 &value, &bytes, &baton);
2df3850c 18931
98bfdba5
PA
18932 if (baton != NULL)
18933 {
98bfdba5 18934 SYMBOL_LOCATION_BATON (sym) = baton;
f1e6e072 18935 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
98bfdba5
PA
18936 }
18937 else if (bytes != NULL)
18938 {
18939 SYMBOL_VALUE_BYTES (sym) = bytes;
f1e6e072 18940 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST_BYTES;
98bfdba5
PA
18941 }
18942 else
18943 {
18944 SYMBOL_VALUE (sym) = value;
f1e6e072 18945 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST;
98bfdba5 18946 }
2df3850c
JM
18947}
18948
c906108c
SS
18949/* Return the type of the die in question using its DW_AT_type attribute. */
18950
18951static struct type *
e7c27a73 18952die_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 18953{
c906108c 18954 struct attribute *type_attr;
c906108c 18955
e142c38c 18956 type_attr = dwarf2_attr (die, DW_AT_type, cu);
c906108c
SS
18957 if (!type_attr)
18958 {
18959 /* A missing DW_AT_type represents a void type. */
46bf5051 18960 return objfile_type (cu->objfile)->builtin_void;
c906108c 18961 }
348e048f 18962
673bfd45 18963 return lookup_die_type (die, type_attr, cu);
c906108c
SS
18964}
18965
b4ba55a1
JB
18966/* True iff CU's producer generates GNAT Ada auxiliary information
18967 that allows to find parallel types through that information instead
18968 of having to do expensive parallel lookups by type name. */
18969
18970static int
18971need_gnat_info (struct dwarf2_cu *cu)
18972{
18973 /* FIXME: brobecker/2010-10-12: As of now, only the AdaCore version
18974 of GNAT produces this auxiliary information, without any indication
18975 that it is produced. Part of enhancing the FSF version of GNAT
18976 to produce that information will be to put in place an indicator
18977 that we can use in order to determine whether the descriptive type
18978 info is available or not. One suggestion that has been made is
18979 to use a new attribute, attached to the CU die. For now, assume
18980 that the descriptive type info is not available. */
18981 return 0;
18982}
18983
b4ba55a1
JB
18984/* Return the auxiliary type of the die in question using its
18985 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
18986 attribute is not present. */
18987
18988static struct type *
18989die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
18990{
b4ba55a1 18991 struct attribute *type_attr;
b4ba55a1
JB
18992
18993 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
18994 if (!type_attr)
18995 return NULL;
18996
673bfd45 18997 return lookup_die_type (die, type_attr, cu);
b4ba55a1
JB
18998}
18999
19000/* If DIE has a descriptive_type attribute, then set the TYPE's
19001 descriptive type accordingly. */
19002
19003static void
19004set_descriptive_type (struct type *type, struct die_info *die,
19005 struct dwarf2_cu *cu)
19006{
19007 struct type *descriptive_type = die_descriptive_type (die, cu);
19008
19009 if (descriptive_type)
19010 {
19011 ALLOCATE_GNAT_AUX_TYPE (type);
19012 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
19013 }
19014}
19015
c906108c
SS
19016/* Return the containing type of the die in question using its
19017 DW_AT_containing_type attribute. */
19018
19019static struct type *
e7c27a73 19020die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 19021{
c906108c 19022 struct attribute *type_attr;
c906108c 19023
e142c38c 19024 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
33ac96f0
JK
19025 if (!type_attr)
19026 error (_("Dwarf Error: Problem turning containing type into gdb type "
4262abfb 19027 "[in module %s]"), objfile_name (cu->objfile));
33ac96f0 19028
673bfd45 19029 return lookup_die_type (die, type_attr, cu);
c906108c
SS
19030}
19031
ac9ec31b
DE
19032/* Return an error marker type to use for the ill formed type in DIE/CU. */
19033
19034static struct type *
19035build_error_marker_type (struct dwarf2_cu *cu, struct die_info *die)
19036{
19037 struct objfile *objfile = dwarf2_per_objfile->objfile;
19038 char *message, *saved;
19039
19040 message = xstrprintf (_("<unknown type in %s, CU 0x%x, DIE 0x%x>"),
4262abfb 19041 objfile_name (objfile),
ac9ec31b
DE
19042 cu->header.offset.sect_off,
19043 die->offset.sect_off);
224c3ddb
SM
19044 saved = (char *) obstack_copy0 (&objfile->objfile_obstack,
19045 message, strlen (message));
ac9ec31b
DE
19046 xfree (message);
19047
19f392bc 19048 return init_type (objfile, TYPE_CODE_ERROR, 0, saved);
ac9ec31b
DE
19049}
19050
673bfd45 19051/* Look up the type of DIE in CU using its type attribute ATTR.
ac9ec31b
DE
19052 ATTR must be one of: DW_AT_type, DW_AT_GNAT_descriptive_type,
19053 DW_AT_containing_type.
673bfd45
DE
19054 If there is no type substitute an error marker. */
19055
c906108c 19056static struct type *
ff39bb5e 19057lookup_die_type (struct die_info *die, const struct attribute *attr,
673bfd45 19058 struct dwarf2_cu *cu)
c906108c 19059{
bb5ed363 19060 struct objfile *objfile = cu->objfile;
f792889a
DJ
19061 struct type *this_type;
19062
ac9ec31b
DE
19063 gdb_assert (attr->name == DW_AT_type
19064 || attr->name == DW_AT_GNAT_descriptive_type
19065 || attr->name == DW_AT_containing_type);
19066
673bfd45
DE
19067 /* First see if we have it cached. */
19068
36586728
TT
19069 if (attr->form == DW_FORM_GNU_ref_alt)
19070 {
19071 struct dwarf2_per_cu_data *per_cu;
19072 sect_offset offset = dwarf2_get_ref_die_offset (attr);
19073
19074 per_cu = dwarf2_find_containing_comp_unit (offset, 1, cu->objfile);
19075 this_type = get_die_type_at_offset (offset, per_cu);
19076 }
7771576e 19077 else if (attr_form_is_ref (attr))
673bfd45 19078 {
b64f50a1 19079 sect_offset offset = dwarf2_get_ref_die_offset (attr);
673bfd45
DE
19080
19081 this_type = get_die_type_at_offset (offset, cu->per_cu);
19082 }
55f1336d 19083 else if (attr->form == DW_FORM_ref_sig8)
673bfd45 19084 {
ac9ec31b 19085 ULONGEST signature = DW_SIGNATURE (attr);
673bfd45 19086
ac9ec31b 19087 return get_signatured_type (die, signature, cu);
673bfd45
DE
19088 }
19089 else
19090 {
ac9ec31b
DE
19091 complaint (&symfile_complaints,
19092 _("Dwarf Error: Bad type attribute %s in DIE"
19093 " at 0x%x [in module %s]"),
19094 dwarf_attr_name (attr->name), die->offset.sect_off,
4262abfb 19095 objfile_name (objfile));
ac9ec31b 19096 return build_error_marker_type (cu, die);
673bfd45
DE
19097 }
19098
19099 /* If not cached we need to read it in. */
19100
19101 if (this_type == NULL)
19102 {
ac9ec31b 19103 struct die_info *type_die = NULL;
673bfd45
DE
19104 struct dwarf2_cu *type_cu = cu;
19105
7771576e 19106 if (attr_form_is_ref (attr))
ac9ec31b
DE
19107 type_die = follow_die_ref (die, attr, &type_cu);
19108 if (type_die == NULL)
19109 return build_error_marker_type (cu, die);
19110 /* If we find the type now, it's probably because the type came
3019eac3
DE
19111 from an inter-CU reference and the type's CU got expanded before
19112 ours. */
ac9ec31b 19113 this_type = read_type_die (type_die, type_cu);
673bfd45
DE
19114 }
19115
19116 /* If we still don't have a type use an error marker. */
19117
19118 if (this_type == NULL)
ac9ec31b 19119 return build_error_marker_type (cu, die);
673bfd45 19120
f792889a 19121 return this_type;
c906108c
SS
19122}
19123
673bfd45
DE
19124/* Return the type in DIE, CU.
19125 Returns NULL for invalid types.
19126
02142a6c 19127 This first does a lookup in die_type_hash,
673bfd45
DE
19128 and only reads the die in if necessary.
19129
19130 NOTE: This can be called when reading in partial or full symbols. */
19131
f792889a 19132static struct type *
e7c27a73 19133read_type_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 19134{
f792889a
DJ
19135 struct type *this_type;
19136
19137 this_type = get_die_type (die, cu);
19138 if (this_type)
19139 return this_type;
19140
673bfd45
DE
19141 return read_type_die_1 (die, cu);
19142}
19143
19144/* Read the type in DIE, CU.
19145 Returns NULL for invalid types. */
19146
19147static struct type *
19148read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
19149{
19150 struct type *this_type = NULL;
19151
c906108c
SS
19152 switch (die->tag)
19153 {
19154 case DW_TAG_class_type:
680b30c7 19155 case DW_TAG_interface_type:
c906108c
SS
19156 case DW_TAG_structure_type:
19157 case DW_TAG_union_type:
f792889a 19158 this_type = read_structure_type (die, cu);
c906108c
SS
19159 break;
19160 case DW_TAG_enumeration_type:
f792889a 19161 this_type = read_enumeration_type (die, cu);
c906108c
SS
19162 break;
19163 case DW_TAG_subprogram:
19164 case DW_TAG_subroutine_type:
edb3359d 19165 case DW_TAG_inlined_subroutine:
f792889a 19166 this_type = read_subroutine_type (die, cu);
c906108c
SS
19167 break;
19168 case DW_TAG_array_type:
f792889a 19169 this_type = read_array_type (die, cu);
c906108c 19170 break;
72019c9c 19171 case DW_TAG_set_type:
f792889a 19172 this_type = read_set_type (die, cu);
72019c9c 19173 break;
c906108c 19174 case DW_TAG_pointer_type:
f792889a 19175 this_type = read_tag_pointer_type (die, cu);
c906108c
SS
19176 break;
19177 case DW_TAG_ptr_to_member_type:
f792889a 19178 this_type = read_tag_ptr_to_member_type (die, cu);
c906108c
SS
19179 break;
19180 case DW_TAG_reference_type:
f792889a 19181 this_type = read_tag_reference_type (die, cu);
c906108c
SS
19182 break;
19183 case DW_TAG_const_type:
f792889a 19184 this_type = read_tag_const_type (die, cu);
c906108c
SS
19185 break;
19186 case DW_TAG_volatile_type:
f792889a 19187 this_type = read_tag_volatile_type (die, cu);
c906108c 19188 break;
06d66ee9
TT
19189 case DW_TAG_restrict_type:
19190 this_type = read_tag_restrict_type (die, cu);
19191 break;
c906108c 19192 case DW_TAG_string_type:
f792889a 19193 this_type = read_tag_string_type (die, cu);
c906108c
SS
19194 break;
19195 case DW_TAG_typedef:
f792889a 19196 this_type = read_typedef (die, cu);
c906108c 19197 break;
a02abb62 19198 case DW_TAG_subrange_type:
f792889a 19199 this_type = read_subrange_type (die, cu);
a02abb62 19200 break;
c906108c 19201 case DW_TAG_base_type:
f792889a 19202 this_type = read_base_type (die, cu);
c906108c 19203 break;
81a17f79 19204 case DW_TAG_unspecified_type:
f792889a 19205 this_type = read_unspecified_type (die, cu);
81a17f79 19206 break;
0114d602
DJ
19207 case DW_TAG_namespace:
19208 this_type = read_namespace_type (die, cu);
19209 break;
f55ee35c
JK
19210 case DW_TAG_module:
19211 this_type = read_module_type (die, cu);
19212 break;
a2c2acaf
MW
19213 case DW_TAG_atomic_type:
19214 this_type = read_tag_atomic_type (die, cu);
19215 break;
c906108c 19216 default:
3e43a32a
MS
19217 complaint (&symfile_complaints,
19218 _("unexpected tag in read_type_die: '%s'"),
4d3c2250 19219 dwarf_tag_name (die->tag));
c906108c
SS
19220 break;
19221 }
63d06c5c 19222
f792889a 19223 return this_type;
63d06c5c
DC
19224}
19225
abc72ce4
DE
19226/* See if we can figure out if the class lives in a namespace. We do
19227 this by looking for a member function; its demangled name will
19228 contain namespace info, if there is any.
19229 Return the computed name or NULL.
19230 Space for the result is allocated on the objfile's obstack.
19231 This is the full-die version of guess_partial_die_structure_name.
19232 In this case we know DIE has no useful parent. */
19233
19234static char *
19235guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
19236{
19237 struct die_info *spec_die;
19238 struct dwarf2_cu *spec_cu;
19239 struct die_info *child;
19240
19241 spec_cu = cu;
19242 spec_die = die_specification (die, &spec_cu);
19243 if (spec_die != NULL)
19244 {
19245 die = spec_die;
19246 cu = spec_cu;
19247 }
19248
19249 for (child = die->child;
19250 child != NULL;
19251 child = child->sibling)
19252 {
19253 if (child->tag == DW_TAG_subprogram)
19254 {
7d45c7c3 19255 const char *linkage_name;
abc72ce4 19256
7d45c7c3
KB
19257 linkage_name = dwarf2_string_attr (child, DW_AT_linkage_name, cu);
19258 if (linkage_name == NULL)
19259 linkage_name = dwarf2_string_attr (child, DW_AT_MIPS_linkage_name,
19260 cu);
19261 if (linkage_name != NULL)
abc72ce4
DE
19262 {
19263 char *actual_name
19264 = language_class_name_from_physname (cu->language_defn,
7d45c7c3 19265 linkage_name);
abc72ce4
DE
19266 char *name = NULL;
19267
19268 if (actual_name != NULL)
19269 {
15d034d0 19270 const char *die_name = dwarf2_name (die, cu);
abc72ce4
DE
19271
19272 if (die_name != NULL
19273 && strcmp (die_name, actual_name) != 0)
19274 {
19275 /* Strip off the class name from the full name.
19276 We want the prefix. */
19277 int die_name_len = strlen (die_name);
19278 int actual_name_len = strlen (actual_name);
19279
19280 /* Test for '::' as a sanity check. */
19281 if (actual_name_len > die_name_len + 2
3e43a32a
MS
19282 && actual_name[actual_name_len
19283 - die_name_len - 1] == ':')
224c3ddb
SM
19284 name = (char *) obstack_copy0 (
19285 &cu->objfile->per_bfd->storage_obstack,
19286 actual_name, actual_name_len - die_name_len - 2);
abc72ce4
DE
19287 }
19288 }
19289 xfree (actual_name);
19290 return name;
19291 }
19292 }
19293 }
19294
19295 return NULL;
19296}
19297
96408a79
SA
19298/* GCC might emit a nameless typedef that has a linkage name. Determine the
19299 prefix part in such case. See
19300 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
19301
19302static char *
19303anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
19304{
19305 struct attribute *attr;
e6a959d6 19306 const char *base;
96408a79
SA
19307
19308 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
19309 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
19310 return NULL;
19311
7d45c7c3 19312 if (dwarf2_string_attr (die, DW_AT_name, cu) != NULL)
96408a79
SA
19313 return NULL;
19314
19315 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
19316 if (attr == NULL)
19317 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
19318 if (attr == NULL || DW_STRING (attr) == NULL)
19319 return NULL;
19320
19321 /* dwarf2_name had to be already called. */
19322 gdb_assert (DW_STRING_IS_CANONICAL (attr));
19323
19324 /* Strip the base name, keep any leading namespaces/classes. */
19325 base = strrchr (DW_STRING (attr), ':');
19326 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
19327 return "";
19328
224c3ddb
SM
19329 return (char *) obstack_copy0 (&cu->objfile->per_bfd->storage_obstack,
19330 DW_STRING (attr),
19331 &base[-1] - DW_STRING (attr));
96408a79
SA
19332}
19333
fdde2d81 19334/* Return the name of the namespace/class that DIE is defined within,
0114d602 19335 or "" if we can't tell. The caller should not xfree the result.
fdde2d81 19336
0114d602
DJ
19337 For example, if we're within the method foo() in the following
19338 code:
19339
19340 namespace N {
19341 class C {
19342 void foo () {
19343 }
19344 };
19345 }
19346
19347 then determine_prefix on foo's die will return "N::C". */
fdde2d81 19348
0d5cff50 19349static const char *
e142c38c 19350determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
63d06c5c 19351{
0114d602
DJ
19352 struct die_info *parent, *spec_die;
19353 struct dwarf2_cu *spec_cu;
19354 struct type *parent_type;
96408a79 19355 char *retval;
63d06c5c 19356
f55ee35c 19357 if (cu->language != language_cplus && cu->language != language_java
c44af4eb
TT
19358 && cu->language != language_fortran && cu->language != language_d
19359 && cu->language != language_rust)
0114d602
DJ
19360 return "";
19361
96408a79
SA
19362 retval = anonymous_struct_prefix (die, cu);
19363 if (retval)
19364 return retval;
19365
0114d602
DJ
19366 /* We have to be careful in the presence of DW_AT_specification.
19367 For example, with GCC 3.4, given the code
19368
19369 namespace N {
19370 void foo() {
19371 // Definition of N::foo.
19372 }
19373 }
19374
19375 then we'll have a tree of DIEs like this:
19376
19377 1: DW_TAG_compile_unit
19378 2: DW_TAG_namespace // N
19379 3: DW_TAG_subprogram // declaration of N::foo
19380 4: DW_TAG_subprogram // definition of N::foo
19381 DW_AT_specification // refers to die #3
19382
19383 Thus, when processing die #4, we have to pretend that we're in
19384 the context of its DW_AT_specification, namely the contex of die
19385 #3. */
19386 spec_cu = cu;
19387 spec_die = die_specification (die, &spec_cu);
19388 if (spec_die == NULL)
19389 parent = die->parent;
19390 else
63d06c5c 19391 {
0114d602
DJ
19392 parent = spec_die->parent;
19393 cu = spec_cu;
63d06c5c 19394 }
0114d602
DJ
19395
19396 if (parent == NULL)
19397 return "";
98bfdba5
PA
19398 else if (parent->building_fullname)
19399 {
19400 const char *name;
19401 const char *parent_name;
19402
19403 /* It has been seen on RealView 2.2 built binaries,
19404 DW_TAG_template_type_param types actually _defined_ as
19405 children of the parent class:
19406
19407 enum E {};
19408 template class <class Enum> Class{};
19409 Class<enum E> class_e;
19410
19411 1: DW_TAG_class_type (Class)
19412 2: DW_TAG_enumeration_type (E)
19413 3: DW_TAG_enumerator (enum1:0)
19414 3: DW_TAG_enumerator (enum2:1)
19415 ...
19416 2: DW_TAG_template_type_param
19417 DW_AT_type DW_FORM_ref_udata (E)
19418
19419 Besides being broken debug info, it can put GDB into an
19420 infinite loop. Consider:
19421
19422 When we're building the full name for Class<E>, we'll start
19423 at Class, and go look over its template type parameters,
19424 finding E. We'll then try to build the full name of E, and
19425 reach here. We're now trying to build the full name of E,
19426 and look over the parent DIE for containing scope. In the
19427 broken case, if we followed the parent DIE of E, we'd again
19428 find Class, and once again go look at its template type
19429 arguments, etc., etc. Simply don't consider such parent die
19430 as source-level parent of this die (it can't be, the language
19431 doesn't allow it), and break the loop here. */
19432 name = dwarf2_name (die, cu);
19433 parent_name = dwarf2_name (parent, cu);
19434 complaint (&symfile_complaints,
19435 _("template param type '%s' defined within parent '%s'"),
19436 name ? name : "<unknown>",
19437 parent_name ? parent_name : "<unknown>");
19438 return "";
19439 }
63d06c5c 19440 else
0114d602
DJ
19441 switch (parent->tag)
19442 {
63d06c5c 19443 case DW_TAG_namespace:
0114d602 19444 parent_type = read_type_die (parent, cu);
acebe513
UW
19445 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
19446 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
19447 Work around this problem here. */
19448 if (cu->language == language_cplus
19449 && strcmp (TYPE_TAG_NAME (parent_type), "::") == 0)
19450 return "";
0114d602
DJ
19451 /* We give a name to even anonymous namespaces. */
19452 return TYPE_TAG_NAME (parent_type);
63d06c5c 19453 case DW_TAG_class_type:
680b30c7 19454 case DW_TAG_interface_type:
63d06c5c 19455 case DW_TAG_structure_type:
0114d602 19456 case DW_TAG_union_type:
f55ee35c 19457 case DW_TAG_module:
0114d602
DJ
19458 parent_type = read_type_die (parent, cu);
19459 if (TYPE_TAG_NAME (parent_type) != NULL)
19460 return TYPE_TAG_NAME (parent_type);
19461 else
19462 /* An anonymous structure is only allowed non-static data
19463 members; no typedefs, no member functions, et cetera.
19464 So it does not need a prefix. */
19465 return "";
abc72ce4 19466 case DW_TAG_compile_unit:
95554aad 19467 case DW_TAG_partial_unit:
abc72ce4
DE
19468 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
19469 if (cu->language == language_cplus
8b70b953 19470 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
abc72ce4
DE
19471 && die->child != NULL
19472 && (die->tag == DW_TAG_class_type
19473 || die->tag == DW_TAG_structure_type
19474 || die->tag == DW_TAG_union_type))
19475 {
19476 char *name = guess_full_die_structure_name (die, cu);
19477 if (name != NULL)
19478 return name;
19479 }
19480 return "";
3d567982
TT
19481 case DW_TAG_enumeration_type:
19482 parent_type = read_type_die (parent, cu);
19483 if (TYPE_DECLARED_CLASS (parent_type))
19484 {
19485 if (TYPE_TAG_NAME (parent_type) != NULL)
19486 return TYPE_TAG_NAME (parent_type);
19487 return "";
19488 }
19489 /* Fall through. */
63d06c5c 19490 default:
8176b9b8 19491 return determine_prefix (parent, cu);
63d06c5c 19492 }
63d06c5c
DC
19493}
19494
3e43a32a
MS
19495/* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
19496 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
19497 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
19498 an obconcat, otherwise allocate storage for the result. The CU argument is
19499 used to determine the language and hence, the appropriate separator. */
987504bb 19500
f55ee35c 19501#define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
63d06c5c
DC
19502
19503static char *
f55ee35c
JK
19504typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
19505 int physname, struct dwarf2_cu *cu)
63d06c5c 19506{
f55ee35c 19507 const char *lead = "";
5c315b68 19508 const char *sep;
63d06c5c 19509
3e43a32a
MS
19510 if (suffix == NULL || suffix[0] == '\0'
19511 || prefix == NULL || prefix[0] == '\0')
987504bb
JJ
19512 sep = "";
19513 else if (cu->language == language_java)
19514 sep = ".";
45280282
IB
19515 else if (cu->language == language_d)
19516 {
19517 /* For D, the 'main' function could be defined in any module, but it
19518 should never be prefixed. */
19519 if (strcmp (suffix, "D main") == 0)
19520 {
19521 prefix = "";
19522 sep = "";
19523 }
19524 else
19525 sep = ".";
19526 }
f55ee35c
JK
19527 else if (cu->language == language_fortran && physname)
19528 {
19529 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
19530 DW_AT_MIPS_linkage_name is preferred and used instead. */
19531
19532 lead = "__";
19533 sep = "_MOD_";
19534 }
987504bb
JJ
19535 else
19536 sep = "::";
63d06c5c 19537
6dd47d34
DE
19538 if (prefix == NULL)
19539 prefix = "";
19540 if (suffix == NULL)
19541 suffix = "";
19542
987504bb
JJ
19543 if (obs == NULL)
19544 {
3e43a32a 19545 char *retval
224c3ddb
SM
19546 = ((char *)
19547 xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1));
9a619af0 19548
f55ee35c
JK
19549 strcpy (retval, lead);
19550 strcat (retval, prefix);
6dd47d34
DE
19551 strcat (retval, sep);
19552 strcat (retval, suffix);
63d06c5c
DC
19553 return retval;
19554 }
987504bb
JJ
19555 else
19556 {
19557 /* We have an obstack. */
f55ee35c 19558 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
987504bb 19559 }
63d06c5c
DC
19560}
19561
c906108c
SS
19562/* Return sibling of die, NULL if no sibling. */
19563
f9aca02d 19564static struct die_info *
fba45db2 19565sibling_die (struct die_info *die)
c906108c 19566{
639d11d3 19567 return die->sibling;
c906108c
SS
19568}
19569
71c25dea
TT
19570/* Get name of a die, return NULL if not found. */
19571
15d034d0
TT
19572static const char *
19573dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
71c25dea
TT
19574 struct obstack *obstack)
19575{
19576 if (name && cu->language == language_cplus)
19577 {
19578 char *canon_name = cp_canonicalize_string (name);
19579
19580 if (canon_name != NULL)
19581 {
19582 if (strcmp (canon_name, name) != 0)
224c3ddb
SM
19583 name = (const char *) obstack_copy0 (obstack, canon_name,
19584 strlen (canon_name));
71c25dea
TT
19585 xfree (canon_name);
19586 }
19587 }
19588
19589 return name;
c906108c
SS
19590}
19591
96553a0c
DE
19592/* Get name of a die, return NULL if not found.
19593 Anonymous namespaces are converted to their magic string. */
9219021c 19594
15d034d0 19595static const char *
e142c38c 19596dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
9219021c
DC
19597{
19598 struct attribute *attr;
19599
e142c38c 19600 attr = dwarf2_attr (die, DW_AT_name, cu);
53832f31 19601 if ((!attr || !DW_STRING (attr))
96553a0c 19602 && die->tag != DW_TAG_namespace
53832f31
TT
19603 && die->tag != DW_TAG_class_type
19604 && die->tag != DW_TAG_interface_type
19605 && die->tag != DW_TAG_structure_type
19606 && die->tag != DW_TAG_union_type)
71c25dea
TT
19607 return NULL;
19608
19609 switch (die->tag)
19610 {
19611 case DW_TAG_compile_unit:
95554aad 19612 case DW_TAG_partial_unit:
71c25dea
TT
19613 /* Compilation units have a DW_AT_name that is a filename, not
19614 a source language identifier. */
19615 case DW_TAG_enumeration_type:
19616 case DW_TAG_enumerator:
19617 /* These tags always have simple identifiers already; no need
19618 to canonicalize them. */
19619 return DW_STRING (attr);
907af001 19620
96553a0c
DE
19621 case DW_TAG_namespace:
19622 if (attr != NULL && DW_STRING (attr) != NULL)
19623 return DW_STRING (attr);
19624 return CP_ANONYMOUS_NAMESPACE_STR;
19625
418835cc
KS
19626 case DW_TAG_subprogram:
19627 /* Java constructors will all be named "<init>", so return
19628 the class name when we see this special case. */
19629 if (cu->language == language_java
19630 && DW_STRING (attr) != NULL
19631 && strcmp (DW_STRING (attr), "<init>") == 0)
19632 {
19633 struct dwarf2_cu *spec_cu = cu;
19634 struct die_info *spec_die;
19635
19636 /* GCJ will output '<init>' for Java constructor names.
19637 For this special case, return the name of the parent class. */
19638
cdc07690 19639 /* GCJ may output subprogram DIEs with AT_specification set.
418835cc
KS
19640 If so, use the name of the specified DIE. */
19641 spec_die = die_specification (die, &spec_cu);
19642 if (spec_die != NULL)
19643 return dwarf2_name (spec_die, spec_cu);
19644
19645 do
19646 {
19647 die = die->parent;
19648 if (die->tag == DW_TAG_class_type)
19649 return dwarf2_name (die, cu);
19650 }
95554aad
TT
19651 while (die->tag != DW_TAG_compile_unit
19652 && die->tag != DW_TAG_partial_unit);
418835cc 19653 }
907af001
UW
19654 break;
19655
19656 case DW_TAG_class_type:
19657 case DW_TAG_interface_type:
19658 case DW_TAG_structure_type:
19659 case DW_TAG_union_type:
19660 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
19661 structures or unions. These were of the form "._%d" in GCC 4.1,
19662 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
19663 and GCC 4.4. We work around this problem by ignoring these. */
53832f31 19664 if (attr && DW_STRING (attr)
61012eef
GB
19665 && (startswith (DW_STRING (attr), "._")
19666 || startswith (DW_STRING (attr), "<anonymous")))
907af001 19667 return NULL;
53832f31
TT
19668
19669 /* GCC might emit a nameless typedef that has a linkage name. See
19670 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
19671 if (!attr || DW_STRING (attr) == NULL)
19672 {
df5c6c50 19673 char *demangled = NULL;
53832f31
TT
19674
19675 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
19676 if (attr == NULL)
19677 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
19678
19679 if (attr == NULL || DW_STRING (attr) == NULL)
19680 return NULL;
19681
df5c6c50
JK
19682 /* Avoid demangling DW_STRING (attr) the second time on a second
19683 call for the same DIE. */
19684 if (!DW_STRING_IS_CANONICAL (attr))
8de20a37 19685 demangled = gdb_demangle (DW_STRING (attr), DMGL_TYPES);
53832f31
TT
19686
19687 if (demangled)
19688 {
e6a959d6 19689 const char *base;
96408a79 19690
53832f31 19691 /* FIXME: we already did this for the partial symbol... */
34a68019 19692 DW_STRING (attr)
224c3ddb
SM
19693 = ((const char *)
19694 obstack_copy0 (&cu->objfile->per_bfd->storage_obstack,
19695 demangled, strlen (demangled)));
53832f31
TT
19696 DW_STRING_IS_CANONICAL (attr) = 1;
19697 xfree (demangled);
96408a79
SA
19698
19699 /* Strip any leading namespaces/classes, keep only the base name.
19700 DW_AT_name for named DIEs does not contain the prefixes. */
19701 base = strrchr (DW_STRING (attr), ':');
19702 if (base && base > DW_STRING (attr) && base[-1] == ':')
19703 return &base[1];
19704 else
19705 return DW_STRING (attr);
53832f31
TT
19706 }
19707 }
907af001
UW
19708 break;
19709
71c25dea 19710 default:
907af001
UW
19711 break;
19712 }
19713
19714 if (!DW_STRING_IS_CANONICAL (attr))
19715 {
19716 DW_STRING (attr)
19717 = dwarf2_canonicalize_name (DW_STRING (attr), cu,
34a68019 19718 &cu->objfile->per_bfd->storage_obstack);
907af001 19719 DW_STRING_IS_CANONICAL (attr) = 1;
71c25dea 19720 }
907af001 19721 return DW_STRING (attr);
9219021c
DC
19722}
19723
19724/* Return the die that this die in an extension of, or NULL if there
f2f0e013
DJ
19725 is none. *EXT_CU is the CU containing DIE on input, and the CU
19726 containing the return value on output. */
9219021c
DC
19727
19728static struct die_info *
f2f0e013 19729dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
9219021c
DC
19730{
19731 struct attribute *attr;
9219021c 19732
f2f0e013 19733 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
9219021c
DC
19734 if (attr == NULL)
19735 return NULL;
19736
f2f0e013 19737 return follow_die_ref (die, attr, ext_cu);
9219021c
DC
19738}
19739
c906108c
SS
19740/* Convert a DIE tag into its string name. */
19741
f39c6ffd 19742static const char *
aa1ee363 19743dwarf_tag_name (unsigned tag)
c906108c 19744{
f39c6ffd
TT
19745 const char *name = get_DW_TAG_name (tag);
19746
19747 if (name == NULL)
19748 return "DW_TAG_<unknown>";
19749
19750 return name;
c906108c
SS
19751}
19752
19753/* Convert a DWARF attribute code into its string name. */
19754
f39c6ffd 19755static const char *
aa1ee363 19756dwarf_attr_name (unsigned attr)
c906108c 19757{
f39c6ffd
TT
19758 const char *name;
19759
c764a876 19760#ifdef MIPS /* collides with DW_AT_HP_block_index */
f39c6ffd
TT
19761 if (attr == DW_AT_MIPS_fde)
19762 return "DW_AT_MIPS_fde";
19763#else
19764 if (attr == DW_AT_HP_block_index)
19765 return "DW_AT_HP_block_index";
c764a876 19766#endif
f39c6ffd
TT
19767
19768 name = get_DW_AT_name (attr);
19769
19770 if (name == NULL)
19771 return "DW_AT_<unknown>";
19772
19773 return name;
c906108c
SS
19774}
19775
19776/* Convert a DWARF value form code into its string name. */
19777
f39c6ffd 19778static const char *
aa1ee363 19779dwarf_form_name (unsigned form)
c906108c 19780{
f39c6ffd
TT
19781 const char *name = get_DW_FORM_name (form);
19782
19783 if (name == NULL)
19784 return "DW_FORM_<unknown>";
19785
19786 return name;
c906108c
SS
19787}
19788
19789static char *
fba45db2 19790dwarf_bool_name (unsigned mybool)
c906108c
SS
19791{
19792 if (mybool)
19793 return "TRUE";
19794 else
19795 return "FALSE";
19796}
19797
19798/* Convert a DWARF type code into its string name. */
19799
f39c6ffd 19800static const char *
aa1ee363 19801dwarf_type_encoding_name (unsigned enc)
c906108c 19802{
f39c6ffd 19803 const char *name = get_DW_ATE_name (enc);
c906108c 19804
f39c6ffd
TT
19805 if (name == NULL)
19806 return "DW_ATE_<unknown>";
c906108c 19807
f39c6ffd 19808 return name;
c906108c 19809}
c906108c 19810
f9aca02d 19811static void
d97bc12b 19812dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
c906108c
SS
19813{
19814 unsigned int i;
19815
d97bc12b
DE
19816 print_spaces (indent, f);
19817 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset 0x%x)\n",
b64f50a1 19818 dwarf_tag_name (die->tag), die->abbrev, die->offset.sect_off);
d97bc12b
DE
19819
19820 if (die->parent != NULL)
19821 {
19822 print_spaces (indent, f);
19823 fprintf_unfiltered (f, " parent at offset: 0x%x\n",
b64f50a1 19824 die->parent->offset.sect_off);
d97bc12b
DE
19825 }
19826
19827 print_spaces (indent, f);
19828 fprintf_unfiltered (f, " has children: %s\n",
639d11d3 19829 dwarf_bool_name (die->child != NULL));
c906108c 19830
d97bc12b
DE
19831 print_spaces (indent, f);
19832 fprintf_unfiltered (f, " attributes:\n");
19833
c906108c
SS
19834 for (i = 0; i < die->num_attrs; ++i)
19835 {
d97bc12b
DE
19836 print_spaces (indent, f);
19837 fprintf_unfiltered (f, " %s (%s) ",
c906108c
SS
19838 dwarf_attr_name (die->attrs[i].name),
19839 dwarf_form_name (die->attrs[i].form));
d97bc12b 19840
c906108c
SS
19841 switch (die->attrs[i].form)
19842 {
c906108c 19843 case DW_FORM_addr:
3019eac3 19844 case DW_FORM_GNU_addr_index:
d97bc12b 19845 fprintf_unfiltered (f, "address: ");
5af949e3 19846 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
c906108c
SS
19847 break;
19848 case DW_FORM_block2:
19849 case DW_FORM_block4:
19850 case DW_FORM_block:
19851 case DW_FORM_block1:
56eb65bd
SP
19852 fprintf_unfiltered (f, "block: size %s",
19853 pulongest (DW_BLOCK (&die->attrs[i])->size));
c906108c 19854 break;
2dc7f7b3 19855 case DW_FORM_exprloc:
56eb65bd
SP
19856 fprintf_unfiltered (f, "expression: size %s",
19857 pulongest (DW_BLOCK (&die->attrs[i])->size));
2dc7f7b3 19858 break;
4568ecf9
DE
19859 case DW_FORM_ref_addr:
19860 fprintf_unfiltered (f, "ref address: ");
19861 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
19862 break;
36586728
TT
19863 case DW_FORM_GNU_ref_alt:
19864 fprintf_unfiltered (f, "alt ref address: ");
19865 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
19866 break;
10b3939b
DJ
19867 case DW_FORM_ref1:
19868 case DW_FORM_ref2:
19869 case DW_FORM_ref4:
4568ecf9
DE
19870 case DW_FORM_ref8:
19871 case DW_FORM_ref_udata:
d97bc12b 19872 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
4568ecf9 19873 (long) (DW_UNSND (&die->attrs[i])));
10b3939b 19874 break;
c906108c
SS
19875 case DW_FORM_data1:
19876 case DW_FORM_data2:
19877 case DW_FORM_data4:
ce5d95e1 19878 case DW_FORM_data8:
c906108c
SS
19879 case DW_FORM_udata:
19880 case DW_FORM_sdata:
43bbcdc2
PH
19881 fprintf_unfiltered (f, "constant: %s",
19882 pulongest (DW_UNSND (&die->attrs[i])));
c906108c 19883 break;
2dc7f7b3
TT
19884 case DW_FORM_sec_offset:
19885 fprintf_unfiltered (f, "section offset: %s",
19886 pulongest (DW_UNSND (&die->attrs[i])));
19887 break;
55f1336d 19888 case DW_FORM_ref_sig8:
ac9ec31b
DE
19889 fprintf_unfiltered (f, "signature: %s",
19890 hex_string (DW_SIGNATURE (&die->attrs[i])));
348e048f 19891 break;
c906108c 19892 case DW_FORM_string:
4bdf3d34 19893 case DW_FORM_strp:
3019eac3 19894 case DW_FORM_GNU_str_index:
36586728 19895 case DW_FORM_GNU_strp_alt:
8285870a 19896 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
c906108c 19897 DW_STRING (&die->attrs[i])
8285870a
JK
19898 ? DW_STRING (&die->attrs[i]) : "",
19899 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
c906108c
SS
19900 break;
19901 case DW_FORM_flag:
19902 if (DW_UNSND (&die->attrs[i]))
d97bc12b 19903 fprintf_unfiltered (f, "flag: TRUE");
c906108c 19904 else
d97bc12b 19905 fprintf_unfiltered (f, "flag: FALSE");
c906108c 19906 break;
2dc7f7b3
TT
19907 case DW_FORM_flag_present:
19908 fprintf_unfiltered (f, "flag: TRUE");
19909 break;
a8329558 19910 case DW_FORM_indirect:
0963b4bd
MS
19911 /* The reader will have reduced the indirect form to
19912 the "base form" so this form should not occur. */
3e43a32a
MS
19913 fprintf_unfiltered (f,
19914 "unexpected attribute form: DW_FORM_indirect");
a8329558 19915 break;
c906108c 19916 default:
d97bc12b 19917 fprintf_unfiltered (f, "unsupported attribute form: %d.",
c5aa993b 19918 die->attrs[i].form);
d97bc12b 19919 break;
c906108c 19920 }
d97bc12b 19921 fprintf_unfiltered (f, "\n");
c906108c
SS
19922 }
19923}
19924
f9aca02d 19925static void
d97bc12b 19926dump_die_for_error (struct die_info *die)
c906108c 19927{
d97bc12b
DE
19928 dump_die_shallow (gdb_stderr, 0, die);
19929}
19930
19931static void
19932dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
19933{
19934 int indent = level * 4;
19935
19936 gdb_assert (die != NULL);
19937
19938 if (level >= max_level)
19939 return;
19940
19941 dump_die_shallow (f, indent, die);
19942
19943 if (die->child != NULL)
c906108c 19944 {
d97bc12b
DE
19945 print_spaces (indent, f);
19946 fprintf_unfiltered (f, " Children:");
19947 if (level + 1 < max_level)
19948 {
19949 fprintf_unfiltered (f, "\n");
19950 dump_die_1 (f, level + 1, max_level, die->child);
19951 }
19952 else
19953 {
3e43a32a
MS
19954 fprintf_unfiltered (f,
19955 " [not printed, max nesting level reached]\n");
d97bc12b
DE
19956 }
19957 }
19958
19959 if (die->sibling != NULL && level > 0)
19960 {
19961 dump_die_1 (f, level, max_level, die->sibling);
c906108c
SS
19962 }
19963}
19964
d97bc12b
DE
19965/* This is called from the pdie macro in gdbinit.in.
19966 It's not static so gcc will keep a copy callable from gdb. */
19967
19968void
19969dump_die (struct die_info *die, int max_level)
19970{
19971 dump_die_1 (gdb_stdlog, 0, max_level, die);
19972}
19973
f9aca02d 19974static void
51545339 19975store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
c906108c 19976{
51545339 19977 void **slot;
c906108c 19978
b64f50a1
JK
19979 slot = htab_find_slot_with_hash (cu->die_hash, die, die->offset.sect_off,
19980 INSERT);
51545339
DJ
19981
19982 *slot = die;
c906108c
SS
19983}
19984
b64f50a1
JK
19985/* Return DIE offset of ATTR. Return 0 with complaint if ATTR is not of the
19986 required kind. */
19987
19988static sect_offset
ff39bb5e 19989dwarf2_get_ref_die_offset (const struct attribute *attr)
93311388 19990{
4568ecf9 19991 sect_offset retval = { DW_UNSND (attr) };
b64f50a1 19992
7771576e 19993 if (attr_form_is_ref (attr))
b64f50a1 19994 return retval;
93311388 19995
b64f50a1 19996 retval.sect_off = 0;
93311388
DE
19997 complaint (&symfile_complaints,
19998 _("unsupported die ref attribute form: '%s'"),
19999 dwarf_form_name (attr->form));
b64f50a1 20000 return retval;
c906108c
SS
20001}
20002
43bbcdc2
PH
20003/* Return the constant value held by ATTR. Return DEFAULT_VALUE if
20004 * the value held by the attribute is not constant. */
a02abb62 20005
43bbcdc2 20006static LONGEST
ff39bb5e 20007dwarf2_get_attr_constant_value (const struct attribute *attr, int default_value)
a02abb62
JB
20008{
20009 if (attr->form == DW_FORM_sdata)
20010 return DW_SND (attr);
20011 else if (attr->form == DW_FORM_udata
20012 || attr->form == DW_FORM_data1
20013 || attr->form == DW_FORM_data2
20014 || attr->form == DW_FORM_data4
20015 || attr->form == DW_FORM_data8)
20016 return DW_UNSND (attr);
20017 else
20018 {
3e43a32a
MS
20019 complaint (&symfile_complaints,
20020 _("Attribute value is not a constant (%s)"),
a02abb62
JB
20021 dwarf_form_name (attr->form));
20022 return default_value;
20023 }
20024}
20025
348e048f
DE
20026/* Follow reference or signature attribute ATTR of SRC_DIE.
20027 On entry *REF_CU is the CU of SRC_DIE.
20028 On exit *REF_CU is the CU of the result. */
20029
20030static struct die_info *
ff39bb5e 20031follow_die_ref_or_sig (struct die_info *src_die, const struct attribute *attr,
348e048f
DE
20032 struct dwarf2_cu **ref_cu)
20033{
20034 struct die_info *die;
20035
7771576e 20036 if (attr_form_is_ref (attr))
348e048f 20037 die = follow_die_ref (src_die, attr, ref_cu);
55f1336d 20038 else if (attr->form == DW_FORM_ref_sig8)
348e048f
DE
20039 die = follow_die_sig (src_die, attr, ref_cu);
20040 else
20041 {
20042 dump_die_for_error (src_die);
20043 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
4262abfb 20044 objfile_name ((*ref_cu)->objfile));
348e048f
DE
20045 }
20046
20047 return die;
03dd20cc
DJ
20048}
20049
5c631832 20050/* Follow reference OFFSET.
673bfd45
DE
20051 On entry *REF_CU is the CU of the source die referencing OFFSET.
20052 On exit *REF_CU is the CU of the result.
20053 Returns NULL if OFFSET is invalid. */
f504f079 20054
f9aca02d 20055static struct die_info *
36586728
TT
20056follow_die_offset (sect_offset offset, int offset_in_dwz,
20057 struct dwarf2_cu **ref_cu)
c906108c 20058{
10b3939b 20059 struct die_info temp_die;
f2f0e013 20060 struct dwarf2_cu *target_cu, *cu = *ref_cu;
10b3939b 20061
348e048f
DE
20062 gdb_assert (cu->per_cu != NULL);
20063
98bfdba5
PA
20064 target_cu = cu;
20065
3019eac3 20066 if (cu->per_cu->is_debug_types)
348e048f
DE
20067 {
20068 /* .debug_types CUs cannot reference anything outside their CU.
20069 If they need to, they have to reference a signatured type via
55f1336d 20070 DW_FORM_ref_sig8. */
348e048f 20071 if (! offset_in_cu_p (&cu->header, offset))
5c631832 20072 return NULL;
348e048f 20073 }
36586728
TT
20074 else if (offset_in_dwz != cu->per_cu->is_dwz
20075 || ! offset_in_cu_p (&cu->header, offset))
10b3939b
DJ
20076 {
20077 struct dwarf2_per_cu_data *per_cu;
9a619af0 20078
36586728
TT
20079 per_cu = dwarf2_find_containing_comp_unit (offset, offset_in_dwz,
20080 cu->objfile);
03dd20cc
DJ
20081
20082 /* If necessary, add it to the queue and load its DIEs. */
95554aad
TT
20083 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
20084 load_full_comp_unit (per_cu, cu->language);
03dd20cc 20085
10b3939b
DJ
20086 target_cu = per_cu->cu;
20087 }
98bfdba5
PA
20088 else if (cu->dies == NULL)
20089 {
20090 /* We're loading full DIEs during partial symbol reading. */
20091 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
95554aad 20092 load_full_comp_unit (cu->per_cu, language_minimal);
98bfdba5 20093 }
c906108c 20094
f2f0e013 20095 *ref_cu = target_cu;
51545339 20096 temp_die.offset = offset;
9a3c8263
SM
20097 return (struct die_info *) htab_find_with_hash (target_cu->die_hash,
20098 &temp_die, offset.sect_off);
5c631832 20099}
10b3939b 20100
5c631832
JK
20101/* Follow reference attribute ATTR of SRC_DIE.
20102 On entry *REF_CU is the CU of SRC_DIE.
20103 On exit *REF_CU is the CU of the result. */
20104
20105static struct die_info *
ff39bb5e 20106follow_die_ref (struct die_info *src_die, const struct attribute *attr,
5c631832
JK
20107 struct dwarf2_cu **ref_cu)
20108{
b64f50a1 20109 sect_offset offset = dwarf2_get_ref_die_offset (attr);
5c631832
JK
20110 struct dwarf2_cu *cu = *ref_cu;
20111 struct die_info *die;
20112
36586728
TT
20113 die = follow_die_offset (offset,
20114 (attr->form == DW_FORM_GNU_ref_alt
20115 || cu->per_cu->is_dwz),
20116 ref_cu);
5c631832
JK
20117 if (!die)
20118 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced from DIE "
20119 "at 0x%x [in module %s]"),
4262abfb
JK
20120 offset.sect_off, src_die->offset.sect_off,
20121 objfile_name (cu->objfile));
348e048f 20122
5c631832
JK
20123 return die;
20124}
20125
d83e736b
JK
20126/* Return DWARF block referenced by DW_AT_location of DIE at OFFSET at PER_CU.
20127 Returned value is intended for DW_OP_call*. Returned
20128 dwarf2_locexpr_baton->data has lifetime of PER_CU->OBJFILE. */
5c631832
JK
20129
20130struct dwarf2_locexpr_baton
8b9737bf
TT
20131dwarf2_fetch_die_loc_sect_off (sect_offset offset,
20132 struct dwarf2_per_cu_data *per_cu,
20133 CORE_ADDR (*get_frame_pc) (void *baton),
20134 void *baton)
5c631832 20135{
918dd910 20136 struct dwarf2_cu *cu;
5c631832
JK
20137 struct die_info *die;
20138 struct attribute *attr;
20139 struct dwarf2_locexpr_baton retval;
20140
8cf6f0b1
TT
20141 dw2_setup (per_cu->objfile);
20142
918dd910
JK
20143 if (per_cu->cu == NULL)
20144 load_cu (per_cu);
20145 cu = per_cu->cu;
cc12ce38
DE
20146 if (cu == NULL)
20147 {
20148 /* We shouldn't get here for a dummy CU, but don't crash on the user.
20149 Instead just throw an error, not much else we can do. */
20150 error (_("Dwarf Error: Dummy CU at 0x%x referenced in module %s"),
20151 offset.sect_off, objfile_name (per_cu->objfile));
20152 }
918dd910 20153
36586728 20154 die = follow_die_offset (offset, per_cu->is_dwz, &cu);
5c631832
JK
20155 if (!die)
20156 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
4262abfb 20157 offset.sect_off, objfile_name (per_cu->objfile));
5c631832
JK
20158
20159 attr = dwarf2_attr (die, DW_AT_location, cu);
20160 if (!attr)
20161 {
e103e986
JK
20162 /* DWARF: "If there is no such attribute, then there is no effect.".
20163 DATA is ignored if SIZE is 0. */
5c631832 20164
e103e986 20165 retval.data = NULL;
5c631832
JK
20166 retval.size = 0;
20167 }
8cf6f0b1
TT
20168 else if (attr_form_is_section_offset (attr))
20169 {
20170 struct dwarf2_loclist_baton loclist_baton;
20171 CORE_ADDR pc = (*get_frame_pc) (baton);
20172 size_t size;
20173
20174 fill_in_loclist_baton (cu, &loclist_baton, attr);
20175
20176 retval.data = dwarf2_find_location_expression (&loclist_baton,
20177 &size, pc);
20178 retval.size = size;
20179 }
5c631832
JK
20180 else
20181 {
20182 if (!attr_form_is_block (attr))
20183 error (_("Dwarf Error: DIE at 0x%x referenced in module %s "
20184 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
4262abfb 20185 offset.sect_off, objfile_name (per_cu->objfile));
5c631832
JK
20186
20187 retval.data = DW_BLOCK (attr)->data;
20188 retval.size = DW_BLOCK (attr)->size;
20189 }
20190 retval.per_cu = cu->per_cu;
918dd910 20191
918dd910
JK
20192 age_cached_comp_units ();
20193
5c631832 20194 return retval;
348e048f
DE
20195}
20196
8b9737bf
TT
20197/* Like dwarf2_fetch_die_loc_sect_off, but take a CU
20198 offset. */
20199
20200struct dwarf2_locexpr_baton
20201dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
20202 struct dwarf2_per_cu_data *per_cu,
20203 CORE_ADDR (*get_frame_pc) (void *baton),
20204 void *baton)
20205{
20206 sect_offset offset = { per_cu->offset.sect_off + offset_in_cu.cu_off };
20207
20208 return dwarf2_fetch_die_loc_sect_off (offset, per_cu, get_frame_pc, baton);
20209}
20210
b6807d98
TT
20211/* Write a constant of a given type as target-ordered bytes into
20212 OBSTACK. */
20213
20214static const gdb_byte *
20215write_constant_as_bytes (struct obstack *obstack,
20216 enum bfd_endian byte_order,
20217 struct type *type,
20218 ULONGEST value,
20219 LONGEST *len)
20220{
20221 gdb_byte *result;
20222
20223 *len = TYPE_LENGTH (type);
224c3ddb 20224 result = (gdb_byte *) obstack_alloc (obstack, *len);
b6807d98
TT
20225 store_unsigned_integer (result, *len, byte_order, value);
20226
20227 return result;
20228}
20229
20230/* If the DIE at OFFSET in PER_CU has a DW_AT_const_value, return a
20231 pointer to the constant bytes and set LEN to the length of the
20232 data. If memory is needed, allocate it on OBSTACK. If the DIE
20233 does not have a DW_AT_const_value, return NULL. */
20234
20235const gdb_byte *
20236dwarf2_fetch_constant_bytes (sect_offset offset,
20237 struct dwarf2_per_cu_data *per_cu,
20238 struct obstack *obstack,
20239 LONGEST *len)
20240{
20241 struct dwarf2_cu *cu;
20242 struct die_info *die;
20243 struct attribute *attr;
20244 const gdb_byte *result = NULL;
20245 struct type *type;
20246 LONGEST value;
20247 enum bfd_endian byte_order;
20248
20249 dw2_setup (per_cu->objfile);
20250
20251 if (per_cu->cu == NULL)
20252 load_cu (per_cu);
20253 cu = per_cu->cu;
cc12ce38
DE
20254 if (cu == NULL)
20255 {
20256 /* We shouldn't get here for a dummy CU, but don't crash on the user.
20257 Instead just throw an error, not much else we can do. */
20258 error (_("Dwarf Error: Dummy CU at 0x%x referenced in module %s"),
20259 offset.sect_off, objfile_name (per_cu->objfile));
20260 }
b6807d98
TT
20261
20262 die = follow_die_offset (offset, per_cu->is_dwz, &cu);
20263 if (!die)
20264 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
4262abfb 20265 offset.sect_off, objfile_name (per_cu->objfile));
b6807d98
TT
20266
20267
20268 attr = dwarf2_attr (die, DW_AT_const_value, cu);
20269 if (attr == NULL)
20270 return NULL;
20271
20272 byte_order = (bfd_big_endian (per_cu->objfile->obfd)
20273 ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
20274
20275 switch (attr->form)
20276 {
20277 case DW_FORM_addr:
20278 case DW_FORM_GNU_addr_index:
20279 {
20280 gdb_byte *tem;
20281
20282 *len = cu->header.addr_size;
224c3ddb 20283 tem = (gdb_byte *) obstack_alloc (obstack, *len);
b6807d98
TT
20284 store_unsigned_integer (tem, *len, byte_order, DW_ADDR (attr));
20285 result = tem;
20286 }
20287 break;
20288 case DW_FORM_string:
20289 case DW_FORM_strp:
20290 case DW_FORM_GNU_str_index:
20291 case DW_FORM_GNU_strp_alt:
20292 /* DW_STRING is already allocated on the objfile obstack, point
20293 directly to it. */
20294 result = (const gdb_byte *) DW_STRING (attr);
20295 *len = strlen (DW_STRING (attr));
20296 break;
20297 case DW_FORM_block1:
20298 case DW_FORM_block2:
20299 case DW_FORM_block4:
20300 case DW_FORM_block:
20301 case DW_FORM_exprloc:
20302 result = DW_BLOCK (attr)->data;
20303 *len = DW_BLOCK (attr)->size;
20304 break;
20305
20306 /* The DW_AT_const_value attributes are supposed to carry the
20307 symbol's value "represented as it would be on the target
20308 architecture." By the time we get here, it's already been
20309 converted to host endianness, so we just need to sign- or
20310 zero-extend it as appropriate. */
20311 case DW_FORM_data1:
20312 type = die_type (die, cu);
20313 result = dwarf2_const_value_data (attr, obstack, cu, &value, 8);
20314 if (result == NULL)
20315 result = write_constant_as_bytes (obstack, byte_order,
20316 type, value, len);
20317 break;
20318 case DW_FORM_data2:
20319 type = die_type (die, cu);
20320 result = dwarf2_const_value_data (attr, obstack, cu, &value, 16);
20321 if (result == NULL)
20322 result = write_constant_as_bytes (obstack, byte_order,
20323 type, value, len);
20324 break;
20325 case DW_FORM_data4:
20326 type = die_type (die, cu);
20327 result = dwarf2_const_value_data (attr, obstack, cu, &value, 32);
20328 if (result == NULL)
20329 result = write_constant_as_bytes (obstack, byte_order,
20330 type, value, len);
20331 break;
20332 case DW_FORM_data8:
20333 type = die_type (die, cu);
20334 result = dwarf2_const_value_data (attr, obstack, cu, &value, 64);
20335 if (result == NULL)
20336 result = write_constant_as_bytes (obstack, byte_order,
20337 type, value, len);
20338 break;
20339
20340 case DW_FORM_sdata:
20341 type = die_type (die, cu);
20342 result = write_constant_as_bytes (obstack, byte_order,
20343 type, DW_SND (attr), len);
20344 break;
20345
20346 case DW_FORM_udata:
20347 type = die_type (die, cu);
20348 result = write_constant_as_bytes (obstack, byte_order,
20349 type, DW_UNSND (attr), len);
20350 break;
20351
20352 default:
20353 complaint (&symfile_complaints,
20354 _("unsupported const value attribute form: '%s'"),
20355 dwarf_form_name (attr->form));
20356 break;
20357 }
20358
20359 return result;
20360}
20361
8a9b8146
TT
20362/* Return the type of the DIE at DIE_OFFSET in the CU named by
20363 PER_CU. */
20364
20365struct type *
b64f50a1 20366dwarf2_get_die_type (cu_offset die_offset,
8a9b8146
TT
20367 struct dwarf2_per_cu_data *per_cu)
20368{
b64f50a1
JK
20369 sect_offset die_offset_sect;
20370
8a9b8146 20371 dw2_setup (per_cu->objfile);
b64f50a1
JK
20372
20373 die_offset_sect.sect_off = per_cu->offset.sect_off + die_offset.cu_off;
20374 return get_die_type_at_offset (die_offset_sect, per_cu);
8a9b8146
TT
20375}
20376
ac9ec31b 20377/* Follow type unit SIG_TYPE referenced by SRC_DIE.
348e048f 20378 On entry *REF_CU is the CU of SRC_DIE.
ac9ec31b
DE
20379 On exit *REF_CU is the CU of the result.
20380 Returns NULL if the referenced DIE isn't found. */
348e048f
DE
20381
20382static struct die_info *
ac9ec31b
DE
20383follow_die_sig_1 (struct die_info *src_die, struct signatured_type *sig_type,
20384 struct dwarf2_cu **ref_cu)
348e048f 20385{
348e048f 20386 struct die_info temp_die;
348e048f
DE
20387 struct dwarf2_cu *sig_cu;
20388 struct die_info *die;
20389
ac9ec31b
DE
20390 /* While it might be nice to assert sig_type->type == NULL here,
20391 we can get here for DW_AT_imported_declaration where we need
20392 the DIE not the type. */
348e048f
DE
20393
20394 /* If necessary, add it to the queue and load its DIEs. */
20395
95554aad 20396 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, language_minimal))
a0f42c21 20397 read_signatured_type (sig_type);
348e048f 20398
348e048f 20399 sig_cu = sig_type->per_cu.cu;
69d751e3 20400 gdb_assert (sig_cu != NULL);
3019eac3
DE
20401 gdb_assert (sig_type->type_offset_in_section.sect_off != 0);
20402 temp_die.offset = sig_type->type_offset_in_section;
9a3c8263
SM
20403 die = (struct die_info *) htab_find_with_hash (sig_cu->die_hash, &temp_die,
20404 temp_die.offset.sect_off);
348e048f
DE
20405 if (die)
20406 {
796a7ff8
DE
20407 /* For .gdb_index version 7 keep track of included TUs.
20408 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
20409 if (dwarf2_per_objfile->index_table != NULL
20410 && dwarf2_per_objfile->index_table->version <= 7)
20411 {
20412 VEC_safe_push (dwarf2_per_cu_ptr,
20413 (*ref_cu)->per_cu->imported_symtabs,
20414 sig_cu->per_cu);
20415 }
20416
348e048f
DE
20417 *ref_cu = sig_cu;
20418 return die;
20419 }
20420
ac9ec31b
DE
20421 return NULL;
20422}
20423
20424/* Follow signatured type referenced by ATTR in SRC_DIE.
20425 On entry *REF_CU is the CU of SRC_DIE.
20426 On exit *REF_CU is the CU of the result.
20427 The result is the DIE of the type.
20428 If the referenced type cannot be found an error is thrown. */
20429
20430static struct die_info *
ff39bb5e 20431follow_die_sig (struct die_info *src_die, const struct attribute *attr,
ac9ec31b
DE
20432 struct dwarf2_cu **ref_cu)
20433{
20434 ULONGEST signature = DW_SIGNATURE (attr);
20435 struct signatured_type *sig_type;
20436 struct die_info *die;
20437
20438 gdb_assert (attr->form == DW_FORM_ref_sig8);
20439
a2ce51a0 20440 sig_type = lookup_signatured_type (*ref_cu, signature);
ac9ec31b
DE
20441 /* sig_type will be NULL if the signatured type is missing from
20442 the debug info. */
20443 if (sig_type == NULL)
20444 {
20445 error (_("Dwarf Error: Cannot find signatured DIE %s referenced"
20446 " from DIE at 0x%x [in module %s]"),
20447 hex_string (signature), src_die->offset.sect_off,
4262abfb 20448 objfile_name ((*ref_cu)->objfile));
ac9ec31b
DE
20449 }
20450
20451 die = follow_die_sig_1 (src_die, sig_type, ref_cu);
20452 if (die == NULL)
20453 {
20454 dump_die_for_error (src_die);
20455 error (_("Dwarf Error: Problem reading signatured DIE %s referenced"
20456 " from DIE at 0x%x [in module %s]"),
20457 hex_string (signature), src_die->offset.sect_off,
4262abfb 20458 objfile_name ((*ref_cu)->objfile));
ac9ec31b
DE
20459 }
20460
20461 return die;
20462}
20463
20464/* Get the type specified by SIGNATURE referenced in DIE/CU,
20465 reading in and processing the type unit if necessary. */
20466
20467static struct type *
20468get_signatured_type (struct die_info *die, ULONGEST signature,
20469 struct dwarf2_cu *cu)
20470{
20471 struct signatured_type *sig_type;
20472 struct dwarf2_cu *type_cu;
20473 struct die_info *type_die;
20474 struct type *type;
20475
a2ce51a0 20476 sig_type = lookup_signatured_type (cu, signature);
ac9ec31b
DE
20477 /* sig_type will be NULL if the signatured type is missing from
20478 the debug info. */
20479 if (sig_type == NULL)
20480 {
20481 complaint (&symfile_complaints,
20482 _("Dwarf Error: Cannot find signatured DIE %s referenced"
20483 " from DIE at 0x%x [in module %s]"),
20484 hex_string (signature), die->offset.sect_off,
4262abfb 20485 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
20486 return build_error_marker_type (cu, die);
20487 }
20488
20489 /* If we already know the type we're done. */
20490 if (sig_type->type != NULL)
20491 return sig_type->type;
20492
20493 type_cu = cu;
20494 type_die = follow_die_sig_1 (die, sig_type, &type_cu);
20495 if (type_die != NULL)
20496 {
20497 /* N.B. We need to call get_die_type to ensure only one type for this DIE
20498 is created. This is important, for example, because for c++ classes
20499 we need TYPE_NAME set which is only done by new_symbol. Blech. */
20500 type = read_type_die (type_die, type_cu);
20501 if (type == NULL)
20502 {
20503 complaint (&symfile_complaints,
20504 _("Dwarf Error: Cannot build signatured type %s"
20505 " referenced from DIE at 0x%x [in module %s]"),
20506 hex_string (signature), die->offset.sect_off,
4262abfb 20507 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
20508 type = build_error_marker_type (cu, die);
20509 }
20510 }
20511 else
20512 {
20513 complaint (&symfile_complaints,
20514 _("Dwarf Error: Problem reading signatured DIE %s referenced"
20515 " from DIE at 0x%x [in module %s]"),
20516 hex_string (signature), die->offset.sect_off,
4262abfb 20517 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
20518 type = build_error_marker_type (cu, die);
20519 }
20520 sig_type->type = type;
20521
20522 return type;
20523}
20524
20525/* Get the type specified by the DW_AT_signature ATTR in DIE/CU,
20526 reading in and processing the type unit if necessary. */
20527
20528static struct type *
ff39bb5e 20529get_DW_AT_signature_type (struct die_info *die, const struct attribute *attr,
b385a60d 20530 struct dwarf2_cu *cu) /* ARI: editCase function */
ac9ec31b
DE
20531{
20532 /* Yes, DW_AT_signature can use a non-ref_sig8 reference. */
7771576e 20533 if (attr_form_is_ref (attr))
ac9ec31b
DE
20534 {
20535 struct dwarf2_cu *type_cu = cu;
20536 struct die_info *type_die = follow_die_ref (die, attr, &type_cu);
20537
20538 return read_type_die (type_die, type_cu);
20539 }
20540 else if (attr->form == DW_FORM_ref_sig8)
20541 {
20542 return get_signatured_type (die, DW_SIGNATURE (attr), cu);
20543 }
20544 else
20545 {
20546 complaint (&symfile_complaints,
20547 _("Dwarf Error: DW_AT_signature has bad form %s in DIE"
20548 " at 0x%x [in module %s]"),
20549 dwarf_form_name (attr->form), die->offset.sect_off,
4262abfb 20550 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
20551 return build_error_marker_type (cu, die);
20552 }
348e048f
DE
20553}
20554
e5fe5e75 20555/* Load the DIEs associated with type unit PER_CU into memory. */
348e048f
DE
20556
20557static void
e5fe5e75 20558load_full_type_unit (struct dwarf2_per_cu_data *per_cu)
348e048f 20559{
52dc124a 20560 struct signatured_type *sig_type;
348e048f 20561
f4dc4d17
DE
20562 /* Caller is responsible for ensuring type_unit_groups don't get here. */
20563 gdb_assert (! IS_TYPE_UNIT_GROUP (per_cu));
20564
6721b2ec
DE
20565 /* We have the per_cu, but we need the signatured_type.
20566 Fortunately this is an easy translation. */
20567 gdb_assert (per_cu->is_debug_types);
20568 sig_type = (struct signatured_type *) per_cu;
348e048f 20569
6721b2ec 20570 gdb_assert (per_cu->cu == NULL);
348e048f 20571
52dc124a 20572 read_signatured_type (sig_type);
348e048f 20573
6721b2ec 20574 gdb_assert (per_cu->cu != NULL);
348e048f
DE
20575}
20576
dee91e82
DE
20577/* die_reader_func for read_signatured_type.
20578 This is identical to load_full_comp_unit_reader,
20579 but is kept separate for now. */
348e048f
DE
20580
20581static void
dee91e82 20582read_signatured_type_reader (const struct die_reader_specs *reader,
d521ce57 20583 const gdb_byte *info_ptr,
dee91e82
DE
20584 struct die_info *comp_unit_die,
20585 int has_children,
20586 void *data)
348e048f 20587{
dee91e82 20588 struct dwarf2_cu *cu = reader->cu;
348e048f 20589
dee91e82
DE
20590 gdb_assert (cu->die_hash == NULL);
20591 cu->die_hash =
20592 htab_create_alloc_ex (cu->header.length / 12,
20593 die_hash,
20594 die_eq,
20595 NULL,
20596 &cu->comp_unit_obstack,
20597 hashtab_obstack_allocate,
20598 dummy_obstack_deallocate);
348e048f 20599
dee91e82
DE
20600 if (has_children)
20601 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
20602 &info_ptr, comp_unit_die);
20603 cu->dies = comp_unit_die;
20604 /* comp_unit_die is not stored in die_hash, no need. */
348e048f
DE
20605
20606 /* We try not to read any attributes in this function, because not
9cdd5dbd 20607 all CUs needed for references have been loaded yet, and symbol
348e048f 20608 table processing isn't initialized. But we have to set the CU language,
dee91e82
DE
20609 or we won't be able to build types correctly.
20610 Similarly, if we do not read the producer, we can not apply
20611 producer-specific interpretation. */
95554aad 20612 prepare_one_comp_unit (cu, cu->dies, language_minimal);
dee91e82 20613}
348e048f 20614
3019eac3
DE
20615/* Read in a signatured type and build its CU and DIEs.
20616 If the type is a stub for the real type in a DWO file,
20617 read in the real type from the DWO file as well. */
dee91e82
DE
20618
20619static void
20620read_signatured_type (struct signatured_type *sig_type)
20621{
20622 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
348e048f 20623
3019eac3 20624 gdb_assert (per_cu->is_debug_types);
dee91e82 20625 gdb_assert (per_cu->cu == NULL);
348e048f 20626
f4dc4d17
DE
20627 init_cutu_and_read_dies (per_cu, NULL, 0, 1,
20628 read_signatured_type_reader, NULL);
7ee85ab1 20629 sig_type->per_cu.tu_read = 1;
c906108c
SS
20630}
20631
c906108c
SS
20632/* Decode simple location descriptions.
20633 Given a pointer to a dwarf block that defines a location, compute
20634 the location and return the value.
20635
4cecd739
DJ
20636 NOTE drow/2003-11-18: This function is called in two situations
20637 now: for the address of static or global variables (partial symbols
20638 only) and for offsets into structures which are expected to be
20639 (more or less) constant. The partial symbol case should go away,
20640 and only the constant case should remain. That will let this
20641 function complain more accurately. A few special modes are allowed
20642 without complaint for global variables (for instance, global
20643 register values and thread-local values).
c906108c
SS
20644
20645 A location description containing no operations indicates that the
4cecd739 20646 object is optimized out. The return value is 0 for that case.
6b992462
DJ
20647 FIXME drow/2003-11-16: No callers check for this case any more; soon all
20648 callers will only want a very basic result and this can become a
21ae7a4d
JK
20649 complaint.
20650
20651 Note that stack[0] is unused except as a default error return. */
c906108c
SS
20652
20653static CORE_ADDR
e7c27a73 20654decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
c906108c 20655{
e7c27a73 20656 struct objfile *objfile = cu->objfile;
56eb65bd
SP
20657 size_t i;
20658 size_t size = blk->size;
d521ce57 20659 const gdb_byte *data = blk->data;
21ae7a4d
JK
20660 CORE_ADDR stack[64];
20661 int stacki;
20662 unsigned int bytes_read, unsnd;
20663 gdb_byte op;
c906108c 20664
21ae7a4d
JK
20665 i = 0;
20666 stacki = 0;
20667 stack[stacki] = 0;
20668 stack[++stacki] = 0;
20669
20670 while (i < size)
20671 {
20672 op = data[i++];
20673 switch (op)
20674 {
20675 case DW_OP_lit0:
20676 case DW_OP_lit1:
20677 case DW_OP_lit2:
20678 case DW_OP_lit3:
20679 case DW_OP_lit4:
20680 case DW_OP_lit5:
20681 case DW_OP_lit6:
20682 case DW_OP_lit7:
20683 case DW_OP_lit8:
20684 case DW_OP_lit9:
20685 case DW_OP_lit10:
20686 case DW_OP_lit11:
20687 case DW_OP_lit12:
20688 case DW_OP_lit13:
20689 case DW_OP_lit14:
20690 case DW_OP_lit15:
20691 case DW_OP_lit16:
20692 case DW_OP_lit17:
20693 case DW_OP_lit18:
20694 case DW_OP_lit19:
20695 case DW_OP_lit20:
20696 case DW_OP_lit21:
20697 case DW_OP_lit22:
20698 case DW_OP_lit23:
20699 case DW_OP_lit24:
20700 case DW_OP_lit25:
20701 case DW_OP_lit26:
20702 case DW_OP_lit27:
20703 case DW_OP_lit28:
20704 case DW_OP_lit29:
20705 case DW_OP_lit30:
20706 case DW_OP_lit31:
20707 stack[++stacki] = op - DW_OP_lit0;
20708 break;
f1bea926 20709
21ae7a4d
JK
20710 case DW_OP_reg0:
20711 case DW_OP_reg1:
20712 case DW_OP_reg2:
20713 case DW_OP_reg3:
20714 case DW_OP_reg4:
20715 case DW_OP_reg5:
20716 case DW_OP_reg6:
20717 case DW_OP_reg7:
20718 case DW_OP_reg8:
20719 case DW_OP_reg9:
20720 case DW_OP_reg10:
20721 case DW_OP_reg11:
20722 case DW_OP_reg12:
20723 case DW_OP_reg13:
20724 case DW_OP_reg14:
20725 case DW_OP_reg15:
20726 case DW_OP_reg16:
20727 case DW_OP_reg17:
20728 case DW_OP_reg18:
20729 case DW_OP_reg19:
20730 case DW_OP_reg20:
20731 case DW_OP_reg21:
20732 case DW_OP_reg22:
20733 case DW_OP_reg23:
20734 case DW_OP_reg24:
20735 case DW_OP_reg25:
20736 case DW_OP_reg26:
20737 case DW_OP_reg27:
20738 case DW_OP_reg28:
20739 case DW_OP_reg29:
20740 case DW_OP_reg30:
20741 case DW_OP_reg31:
20742 stack[++stacki] = op - DW_OP_reg0;
20743 if (i < size)
20744 dwarf2_complex_location_expr_complaint ();
20745 break;
c906108c 20746
21ae7a4d
JK
20747 case DW_OP_regx:
20748 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
20749 i += bytes_read;
20750 stack[++stacki] = unsnd;
20751 if (i < size)
20752 dwarf2_complex_location_expr_complaint ();
20753 break;
c906108c 20754
21ae7a4d
JK
20755 case DW_OP_addr:
20756 stack[++stacki] = read_address (objfile->obfd, &data[i],
20757 cu, &bytes_read);
20758 i += bytes_read;
20759 break;
d53d4ac5 20760
21ae7a4d
JK
20761 case DW_OP_const1u:
20762 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
20763 i += 1;
20764 break;
20765
20766 case DW_OP_const1s:
20767 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
20768 i += 1;
20769 break;
20770
20771 case DW_OP_const2u:
20772 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
20773 i += 2;
20774 break;
20775
20776 case DW_OP_const2s:
20777 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
20778 i += 2;
20779 break;
d53d4ac5 20780
21ae7a4d
JK
20781 case DW_OP_const4u:
20782 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
20783 i += 4;
20784 break;
20785
20786 case DW_OP_const4s:
20787 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
20788 i += 4;
20789 break;
20790
585861ea
JK
20791 case DW_OP_const8u:
20792 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
20793 i += 8;
20794 break;
20795
21ae7a4d
JK
20796 case DW_OP_constu:
20797 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
20798 &bytes_read);
20799 i += bytes_read;
20800 break;
20801
20802 case DW_OP_consts:
20803 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
20804 i += bytes_read;
20805 break;
20806
20807 case DW_OP_dup:
20808 stack[stacki + 1] = stack[stacki];
20809 stacki++;
20810 break;
20811
20812 case DW_OP_plus:
20813 stack[stacki - 1] += stack[stacki];
20814 stacki--;
20815 break;
20816
20817 case DW_OP_plus_uconst:
20818 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
20819 &bytes_read);
20820 i += bytes_read;
20821 break;
20822
20823 case DW_OP_minus:
20824 stack[stacki - 1] -= stack[stacki];
20825 stacki--;
20826 break;
20827
20828 case DW_OP_deref:
20829 /* If we're not the last op, then we definitely can't encode
20830 this using GDB's address_class enum. This is valid for partial
20831 global symbols, although the variable's address will be bogus
20832 in the psymtab. */
20833 if (i < size)
20834 dwarf2_complex_location_expr_complaint ();
20835 break;
20836
20837 case DW_OP_GNU_push_tls_address:
4aa4e28b 20838 case DW_OP_form_tls_address:
21ae7a4d
JK
20839 /* The top of the stack has the offset from the beginning
20840 of the thread control block at which the variable is located. */
20841 /* Nothing should follow this operator, so the top of stack would
20842 be returned. */
20843 /* This is valid for partial global symbols, but the variable's
585861ea
JK
20844 address will be bogus in the psymtab. Make it always at least
20845 non-zero to not look as a variable garbage collected by linker
20846 which have DW_OP_addr 0. */
21ae7a4d
JK
20847 if (i < size)
20848 dwarf2_complex_location_expr_complaint ();
585861ea 20849 stack[stacki]++;
21ae7a4d
JK
20850 break;
20851
20852 case DW_OP_GNU_uninit:
20853 break;
20854
3019eac3 20855 case DW_OP_GNU_addr_index:
49f6c839 20856 case DW_OP_GNU_const_index:
3019eac3
DE
20857 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
20858 &bytes_read);
20859 i += bytes_read;
20860 break;
20861
21ae7a4d
JK
20862 default:
20863 {
f39c6ffd 20864 const char *name = get_DW_OP_name (op);
21ae7a4d
JK
20865
20866 if (name)
20867 complaint (&symfile_complaints, _("unsupported stack op: '%s'"),
20868 name);
20869 else
20870 complaint (&symfile_complaints, _("unsupported stack op: '%02x'"),
20871 op);
20872 }
20873
20874 return (stack[stacki]);
d53d4ac5 20875 }
3c6e0cb3 20876
21ae7a4d
JK
20877 /* Enforce maximum stack depth of SIZE-1 to avoid writing
20878 outside of the allocated space. Also enforce minimum>0. */
20879 if (stacki >= ARRAY_SIZE (stack) - 1)
20880 {
20881 complaint (&symfile_complaints,
20882 _("location description stack overflow"));
20883 return 0;
20884 }
20885
20886 if (stacki <= 0)
20887 {
20888 complaint (&symfile_complaints,
20889 _("location description stack underflow"));
20890 return 0;
20891 }
20892 }
20893 return (stack[stacki]);
c906108c
SS
20894}
20895
20896/* memory allocation interface */
20897
c906108c 20898static struct dwarf_block *
7b5a2f43 20899dwarf_alloc_block (struct dwarf2_cu *cu)
c906108c 20900{
8d749320 20901 return XOBNEW (&cu->comp_unit_obstack, struct dwarf_block);
c906108c
SS
20902}
20903
c906108c 20904static struct die_info *
b60c80d6 20905dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
c906108c
SS
20906{
20907 struct die_info *die;
b60c80d6
DJ
20908 size_t size = sizeof (struct die_info);
20909
20910 if (num_attrs > 1)
20911 size += (num_attrs - 1) * sizeof (struct attribute);
c906108c 20912
b60c80d6 20913 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
c906108c
SS
20914 memset (die, 0, sizeof (struct die_info));
20915 return (die);
20916}
2e276125
JB
20917
20918\f
20919/* Macro support. */
20920
233d95b5
JK
20921/* Return file name relative to the compilation directory of file number I in
20922 *LH's file name table. The result is allocated using xmalloc; the caller is
2e276125 20923 responsible for freeing it. */
233d95b5 20924
2e276125 20925static char *
233d95b5 20926file_file_name (int file, struct line_header *lh)
2e276125 20927{
6a83a1e6
EZ
20928 /* Is the file number a valid index into the line header's file name
20929 table? Remember that file numbers start with one, not zero. */
20930 if (1 <= file && file <= lh->num_file_names)
20931 {
20932 struct file_entry *fe = &lh->file_names[file - 1];
6e70227d 20933
afa6c9ab
SL
20934 if (IS_ABSOLUTE_PATH (fe->name) || fe->dir_index == 0
20935 || lh->include_dirs == NULL)
6a83a1e6 20936 return xstrdup (fe->name);
233d95b5 20937 return concat (lh->include_dirs[fe->dir_index - 1], SLASH_STRING,
b36cec19 20938 fe->name, (char *) NULL);
6a83a1e6 20939 }
2e276125
JB
20940 else
20941 {
6a83a1e6
EZ
20942 /* The compiler produced a bogus file number. We can at least
20943 record the macro definitions made in the file, even if we
20944 won't be able to find the file by name. */
20945 char fake_name[80];
9a619af0 20946
8c042590
PM
20947 xsnprintf (fake_name, sizeof (fake_name),
20948 "<bad macro file number %d>", file);
2e276125 20949
6e70227d 20950 complaint (&symfile_complaints,
6a83a1e6
EZ
20951 _("bad file number in macro information (%d)"),
20952 file);
2e276125 20953
6a83a1e6 20954 return xstrdup (fake_name);
2e276125
JB
20955 }
20956}
20957
233d95b5
JK
20958/* Return the full name of file number I in *LH's file name table.
20959 Use COMP_DIR as the name of the current directory of the
20960 compilation. The result is allocated using xmalloc; the caller is
20961 responsible for freeing it. */
20962static char *
20963file_full_name (int file, struct line_header *lh, const char *comp_dir)
20964{
20965 /* Is the file number a valid index into the line header's file name
20966 table? Remember that file numbers start with one, not zero. */
20967 if (1 <= file && file <= lh->num_file_names)
20968 {
20969 char *relative = file_file_name (file, lh);
20970
20971 if (IS_ABSOLUTE_PATH (relative) || comp_dir == NULL)
20972 return relative;
b36cec19
PA
20973 return reconcat (relative, comp_dir, SLASH_STRING,
20974 relative, (char *) NULL);
233d95b5
JK
20975 }
20976 else
20977 return file_file_name (file, lh);
20978}
20979
2e276125
JB
20980
20981static struct macro_source_file *
20982macro_start_file (int file, int line,
20983 struct macro_source_file *current_file,
43f3e411 20984 struct line_header *lh)
2e276125 20985{
233d95b5
JK
20986 /* File name relative to the compilation directory of this source file. */
20987 char *file_name = file_file_name (file, lh);
2e276125 20988
2e276125 20989 if (! current_file)
abc9d0dc 20990 {
fc474241
DE
20991 /* Note: We don't create a macro table for this compilation unit
20992 at all until we actually get a filename. */
43f3e411 20993 struct macro_table *macro_table = get_macro_table ();
fc474241 20994
abc9d0dc
TT
20995 /* If we have no current file, then this must be the start_file
20996 directive for the compilation unit's main source file. */
fc474241
DE
20997 current_file = macro_set_main (macro_table, file_name);
20998 macro_define_special (macro_table);
abc9d0dc 20999 }
2e276125 21000 else
233d95b5 21001 current_file = macro_include (current_file, line, file_name);
2e276125 21002
233d95b5 21003 xfree (file_name);
6e70227d 21004
2e276125
JB
21005 return current_file;
21006}
21007
21008
21009/* Copy the LEN characters at BUF to a xmalloc'ed block of memory,
21010 followed by a null byte. */
21011static char *
21012copy_string (const char *buf, int len)
21013{
224c3ddb 21014 char *s = (char *) xmalloc (len + 1);
9a619af0 21015
2e276125
JB
21016 memcpy (s, buf, len);
21017 s[len] = '\0';
2e276125
JB
21018 return s;
21019}
21020
21021
21022static const char *
21023consume_improper_spaces (const char *p, const char *body)
21024{
21025 if (*p == ' ')
21026 {
4d3c2250 21027 complaint (&symfile_complaints,
3e43a32a
MS
21028 _("macro definition contains spaces "
21029 "in formal argument list:\n`%s'"),
4d3c2250 21030 body);
2e276125
JB
21031
21032 while (*p == ' ')
21033 p++;
21034 }
21035
21036 return p;
21037}
21038
21039
21040static void
21041parse_macro_definition (struct macro_source_file *file, int line,
21042 const char *body)
21043{
21044 const char *p;
21045
21046 /* The body string takes one of two forms. For object-like macro
21047 definitions, it should be:
21048
21049 <macro name> " " <definition>
21050
21051 For function-like macro definitions, it should be:
21052
21053 <macro name> "() " <definition>
21054 or
21055 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
21056
21057 Spaces may appear only where explicitly indicated, and in the
21058 <definition>.
21059
21060 The Dwarf 2 spec says that an object-like macro's name is always
21061 followed by a space, but versions of GCC around March 2002 omit
6e70227d 21062 the space when the macro's definition is the empty string.
2e276125
JB
21063
21064 The Dwarf 2 spec says that there should be no spaces between the
21065 formal arguments in a function-like macro's formal argument list,
21066 but versions of GCC around March 2002 include spaces after the
21067 commas. */
21068
21069
21070 /* Find the extent of the macro name. The macro name is terminated
21071 by either a space or null character (for an object-like macro) or
21072 an opening paren (for a function-like macro). */
21073 for (p = body; *p; p++)
21074 if (*p == ' ' || *p == '(')
21075 break;
21076
21077 if (*p == ' ' || *p == '\0')
21078 {
21079 /* It's an object-like macro. */
21080 int name_len = p - body;
21081 char *name = copy_string (body, name_len);
21082 const char *replacement;
21083
21084 if (*p == ' ')
21085 replacement = body + name_len + 1;
21086 else
21087 {
4d3c2250 21088 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
21089 replacement = body + name_len;
21090 }
6e70227d 21091
2e276125
JB
21092 macro_define_object (file, line, name, replacement);
21093
21094 xfree (name);
21095 }
21096 else if (*p == '(')
21097 {
21098 /* It's a function-like macro. */
21099 char *name = copy_string (body, p - body);
21100 int argc = 0;
21101 int argv_size = 1;
8d749320 21102 char **argv = XNEWVEC (char *, argv_size);
2e276125
JB
21103
21104 p++;
21105
21106 p = consume_improper_spaces (p, body);
21107
21108 /* Parse the formal argument list. */
21109 while (*p && *p != ')')
21110 {
21111 /* Find the extent of the current argument name. */
21112 const char *arg_start = p;
21113
21114 while (*p && *p != ',' && *p != ')' && *p != ' ')
21115 p++;
21116
21117 if (! *p || p == arg_start)
4d3c2250 21118 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
21119 else
21120 {
21121 /* Make sure argv has room for the new argument. */
21122 if (argc >= argv_size)
21123 {
21124 argv_size *= 2;
224c3ddb 21125 argv = XRESIZEVEC (char *, argv, argv_size);
2e276125
JB
21126 }
21127
21128 argv[argc++] = copy_string (arg_start, p - arg_start);
21129 }
21130
21131 p = consume_improper_spaces (p, body);
21132
21133 /* Consume the comma, if present. */
21134 if (*p == ',')
21135 {
21136 p++;
21137
21138 p = consume_improper_spaces (p, body);
21139 }
21140 }
21141
21142 if (*p == ')')
21143 {
21144 p++;
21145
21146 if (*p == ' ')
21147 /* Perfectly formed definition, no complaints. */
21148 macro_define_function (file, line, name,
6e70227d 21149 argc, (const char **) argv,
2e276125
JB
21150 p + 1);
21151 else if (*p == '\0')
21152 {
21153 /* Complain, but do define it. */
4d3c2250 21154 dwarf2_macro_malformed_definition_complaint (body);
2e276125 21155 macro_define_function (file, line, name,
6e70227d 21156 argc, (const char **) argv,
2e276125
JB
21157 p);
21158 }
21159 else
21160 /* Just complain. */
4d3c2250 21161 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
21162 }
21163 else
21164 /* Just complain. */
4d3c2250 21165 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
21166
21167 xfree (name);
21168 {
21169 int i;
21170
21171 for (i = 0; i < argc; i++)
21172 xfree (argv[i]);
21173 }
21174 xfree (argv);
21175 }
21176 else
4d3c2250 21177 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
21178}
21179
cf2c3c16
TT
21180/* Skip some bytes from BYTES according to the form given in FORM.
21181 Returns the new pointer. */
2e276125 21182
d521ce57
TT
21183static const gdb_byte *
21184skip_form_bytes (bfd *abfd, const gdb_byte *bytes, const gdb_byte *buffer_end,
cf2c3c16
TT
21185 enum dwarf_form form,
21186 unsigned int offset_size,
21187 struct dwarf2_section_info *section)
2e276125 21188{
cf2c3c16 21189 unsigned int bytes_read;
2e276125 21190
cf2c3c16 21191 switch (form)
2e276125 21192 {
cf2c3c16
TT
21193 case DW_FORM_data1:
21194 case DW_FORM_flag:
21195 ++bytes;
21196 break;
21197
21198 case DW_FORM_data2:
21199 bytes += 2;
21200 break;
21201
21202 case DW_FORM_data4:
21203 bytes += 4;
21204 break;
21205
21206 case DW_FORM_data8:
21207 bytes += 8;
21208 break;
21209
21210 case DW_FORM_string:
21211 read_direct_string (abfd, bytes, &bytes_read);
21212 bytes += bytes_read;
21213 break;
21214
21215 case DW_FORM_sec_offset:
21216 case DW_FORM_strp:
36586728 21217 case DW_FORM_GNU_strp_alt:
cf2c3c16
TT
21218 bytes += offset_size;
21219 break;
21220
21221 case DW_FORM_block:
21222 bytes += read_unsigned_leb128 (abfd, bytes, &bytes_read);
21223 bytes += bytes_read;
21224 break;
21225
21226 case DW_FORM_block1:
21227 bytes += 1 + read_1_byte (abfd, bytes);
21228 break;
21229 case DW_FORM_block2:
21230 bytes += 2 + read_2_bytes (abfd, bytes);
21231 break;
21232 case DW_FORM_block4:
21233 bytes += 4 + read_4_bytes (abfd, bytes);
21234 break;
21235
21236 case DW_FORM_sdata:
21237 case DW_FORM_udata:
3019eac3
DE
21238 case DW_FORM_GNU_addr_index:
21239 case DW_FORM_GNU_str_index:
d521ce57 21240 bytes = gdb_skip_leb128 (bytes, buffer_end);
f664829e
DE
21241 if (bytes == NULL)
21242 {
21243 dwarf2_section_buffer_overflow_complaint (section);
21244 return NULL;
21245 }
cf2c3c16
TT
21246 break;
21247
21248 default:
21249 {
21250 complain:
21251 complaint (&symfile_complaints,
21252 _("invalid form 0x%x in `%s'"),
a32a8923 21253 form, get_section_name (section));
cf2c3c16
TT
21254 return NULL;
21255 }
2e276125
JB
21256 }
21257
cf2c3c16
TT
21258 return bytes;
21259}
757a13d0 21260
cf2c3c16
TT
21261/* A helper for dwarf_decode_macros that handles skipping an unknown
21262 opcode. Returns an updated pointer to the macro data buffer; or,
21263 on error, issues a complaint and returns NULL. */
757a13d0 21264
d521ce57 21265static const gdb_byte *
cf2c3c16 21266skip_unknown_opcode (unsigned int opcode,
d521ce57
TT
21267 const gdb_byte **opcode_definitions,
21268 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
cf2c3c16
TT
21269 bfd *abfd,
21270 unsigned int offset_size,
21271 struct dwarf2_section_info *section)
21272{
21273 unsigned int bytes_read, i;
21274 unsigned long arg;
d521ce57 21275 const gdb_byte *defn;
2e276125 21276
cf2c3c16 21277 if (opcode_definitions[opcode] == NULL)
2e276125 21278 {
cf2c3c16
TT
21279 complaint (&symfile_complaints,
21280 _("unrecognized DW_MACFINO opcode 0x%x"),
21281 opcode);
21282 return NULL;
21283 }
2e276125 21284
cf2c3c16
TT
21285 defn = opcode_definitions[opcode];
21286 arg = read_unsigned_leb128 (abfd, defn, &bytes_read);
21287 defn += bytes_read;
2e276125 21288
cf2c3c16
TT
21289 for (i = 0; i < arg; ++i)
21290 {
aead7601
SM
21291 mac_ptr = skip_form_bytes (abfd, mac_ptr, mac_end,
21292 (enum dwarf_form) defn[i], offset_size,
f664829e 21293 section);
cf2c3c16
TT
21294 if (mac_ptr == NULL)
21295 {
21296 /* skip_form_bytes already issued the complaint. */
21297 return NULL;
21298 }
21299 }
757a13d0 21300
cf2c3c16
TT
21301 return mac_ptr;
21302}
757a13d0 21303
cf2c3c16
TT
21304/* A helper function which parses the header of a macro section.
21305 If the macro section is the extended (for now called "GNU") type,
21306 then this updates *OFFSET_SIZE. Returns a pointer to just after
21307 the header, or issues a complaint and returns NULL on error. */
757a13d0 21308
d521ce57
TT
21309static const gdb_byte *
21310dwarf_parse_macro_header (const gdb_byte **opcode_definitions,
cf2c3c16 21311 bfd *abfd,
d521ce57 21312 const gdb_byte *mac_ptr,
cf2c3c16
TT
21313 unsigned int *offset_size,
21314 int section_is_gnu)
21315{
21316 memset (opcode_definitions, 0, 256 * sizeof (gdb_byte *));
757a13d0 21317
cf2c3c16
TT
21318 if (section_is_gnu)
21319 {
21320 unsigned int version, flags;
757a13d0 21321
cf2c3c16
TT
21322 version = read_2_bytes (abfd, mac_ptr);
21323 if (version != 4)
21324 {
21325 complaint (&symfile_complaints,
21326 _("unrecognized version `%d' in .debug_macro section"),
21327 version);
21328 return NULL;
21329 }
21330 mac_ptr += 2;
757a13d0 21331
cf2c3c16
TT
21332 flags = read_1_byte (abfd, mac_ptr);
21333 ++mac_ptr;
21334 *offset_size = (flags & 1) ? 8 : 4;
757a13d0 21335
cf2c3c16
TT
21336 if ((flags & 2) != 0)
21337 /* We don't need the line table offset. */
21338 mac_ptr += *offset_size;
757a13d0 21339
cf2c3c16
TT
21340 /* Vendor opcode descriptions. */
21341 if ((flags & 4) != 0)
21342 {
21343 unsigned int i, count;
757a13d0 21344
cf2c3c16
TT
21345 count = read_1_byte (abfd, mac_ptr);
21346 ++mac_ptr;
21347 for (i = 0; i < count; ++i)
21348 {
21349 unsigned int opcode, bytes_read;
21350 unsigned long arg;
21351
21352 opcode = read_1_byte (abfd, mac_ptr);
21353 ++mac_ptr;
21354 opcode_definitions[opcode] = mac_ptr;
21355 arg = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
21356 mac_ptr += bytes_read;
21357 mac_ptr += arg;
21358 }
757a13d0 21359 }
cf2c3c16 21360 }
757a13d0 21361
cf2c3c16
TT
21362 return mac_ptr;
21363}
757a13d0 21364
cf2c3c16 21365/* A helper for dwarf_decode_macros that handles the GNU extensions,
8fc3fc34 21366 including DW_MACRO_GNU_transparent_include. */
cf2c3c16
TT
21367
21368static void
d521ce57
TT
21369dwarf_decode_macro_bytes (bfd *abfd,
21370 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
cf2c3c16 21371 struct macro_source_file *current_file,
43f3e411 21372 struct line_header *lh,
cf2c3c16 21373 struct dwarf2_section_info *section,
36586728 21374 int section_is_gnu, int section_is_dwz,
cf2c3c16 21375 unsigned int offset_size,
8fc3fc34 21376 htab_t include_hash)
cf2c3c16 21377{
4d663531 21378 struct objfile *objfile = dwarf2_per_objfile->objfile;
cf2c3c16
TT
21379 enum dwarf_macro_record_type macinfo_type;
21380 int at_commandline;
d521ce57 21381 const gdb_byte *opcode_definitions[256];
757a13d0 21382
cf2c3c16
TT
21383 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
21384 &offset_size, section_is_gnu);
21385 if (mac_ptr == NULL)
21386 {
21387 /* We already issued a complaint. */
21388 return;
21389 }
757a13d0
JK
21390
21391 /* Determines if GDB is still before first DW_MACINFO_start_file. If true
21392 GDB is still reading the definitions from command line. First
21393 DW_MACINFO_start_file will need to be ignored as it was already executed
21394 to create CURRENT_FILE for the main source holding also the command line
21395 definitions. On first met DW_MACINFO_start_file this flag is reset to
21396 normally execute all the remaining DW_MACINFO_start_file macinfos. */
21397
21398 at_commandline = 1;
21399
21400 do
21401 {
21402 /* Do we at least have room for a macinfo type byte? */
21403 if (mac_ptr >= mac_end)
21404 {
f664829e 21405 dwarf2_section_buffer_overflow_complaint (section);
757a13d0
JK
21406 break;
21407 }
21408
aead7601 21409 macinfo_type = (enum dwarf_macro_record_type) read_1_byte (abfd, mac_ptr);
757a13d0
JK
21410 mac_ptr++;
21411
cf2c3c16
TT
21412 /* Note that we rely on the fact that the corresponding GNU and
21413 DWARF constants are the same. */
757a13d0
JK
21414 switch (macinfo_type)
21415 {
21416 /* A zero macinfo type indicates the end of the macro
21417 information. */
21418 case 0:
21419 break;
2e276125 21420
cf2c3c16
TT
21421 case DW_MACRO_GNU_define:
21422 case DW_MACRO_GNU_undef:
21423 case DW_MACRO_GNU_define_indirect:
21424 case DW_MACRO_GNU_undef_indirect:
36586728
TT
21425 case DW_MACRO_GNU_define_indirect_alt:
21426 case DW_MACRO_GNU_undef_indirect_alt:
2e276125 21427 {
891d2f0b 21428 unsigned int bytes_read;
2e276125 21429 int line;
d521ce57 21430 const char *body;
cf2c3c16 21431 int is_define;
2e276125 21432
cf2c3c16
TT
21433 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
21434 mac_ptr += bytes_read;
21435
21436 if (macinfo_type == DW_MACRO_GNU_define
21437 || macinfo_type == DW_MACRO_GNU_undef)
21438 {
21439 body = read_direct_string (abfd, mac_ptr, &bytes_read);
21440 mac_ptr += bytes_read;
21441 }
21442 else
21443 {
21444 LONGEST str_offset;
21445
21446 str_offset = read_offset_1 (abfd, mac_ptr, offset_size);
21447 mac_ptr += offset_size;
2e276125 21448
36586728 21449 if (macinfo_type == DW_MACRO_GNU_define_indirect_alt
f7a35f02
TT
21450 || macinfo_type == DW_MACRO_GNU_undef_indirect_alt
21451 || section_is_dwz)
36586728
TT
21452 {
21453 struct dwz_file *dwz = dwarf2_get_dwz_file ();
21454
21455 body = read_indirect_string_from_dwz (dwz, str_offset);
21456 }
21457 else
21458 body = read_indirect_string_at_offset (abfd, str_offset);
cf2c3c16
TT
21459 }
21460
21461 is_define = (macinfo_type == DW_MACRO_GNU_define
36586728
TT
21462 || macinfo_type == DW_MACRO_GNU_define_indirect
21463 || macinfo_type == DW_MACRO_GNU_define_indirect_alt);
2e276125 21464 if (! current_file)
757a13d0
JK
21465 {
21466 /* DWARF violation as no main source is present. */
21467 complaint (&symfile_complaints,
21468 _("debug info with no main source gives macro %s "
21469 "on line %d: %s"),
cf2c3c16
TT
21470 is_define ? _("definition") : _("undefinition"),
21471 line, body);
757a13d0
JK
21472 break;
21473 }
3e43a32a
MS
21474 if ((line == 0 && !at_commandline)
21475 || (line != 0 && at_commandline))
4d3c2250 21476 complaint (&symfile_complaints,
757a13d0
JK
21477 _("debug info gives %s macro %s with %s line %d: %s"),
21478 at_commandline ? _("command-line") : _("in-file"),
cf2c3c16 21479 is_define ? _("definition") : _("undefinition"),
757a13d0
JK
21480 line == 0 ? _("zero") : _("non-zero"), line, body);
21481
cf2c3c16 21482 if (is_define)
757a13d0 21483 parse_macro_definition (current_file, line, body);
cf2c3c16
TT
21484 else
21485 {
21486 gdb_assert (macinfo_type == DW_MACRO_GNU_undef
36586728
TT
21487 || macinfo_type == DW_MACRO_GNU_undef_indirect
21488 || macinfo_type == DW_MACRO_GNU_undef_indirect_alt);
cf2c3c16
TT
21489 macro_undef (current_file, line, body);
21490 }
2e276125
JB
21491 }
21492 break;
21493
cf2c3c16 21494 case DW_MACRO_GNU_start_file:
2e276125 21495 {
891d2f0b 21496 unsigned int bytes_read;
2e276125
JB
21497 int line, file;
21498
21499 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
21500 mac_ptr += bytes_read;
21501 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
21502 mac_ptr += bytes_read;
21503
3e43a32a
MS
21504 if ((line == 0 && !at_commandline)
21505 || (line != 0 && at_commandline))
757a13d0
JK
21506 complaint (&symfile_complaints,
21507 _("debug info gives source %d included "
21508 "from %s at %s line %d"),
21509 file, at_commandline ? _("command-line") : _("file"),
21510 line == 0 ? _("zero") : _("non-zero"), line);
21511
21512 if (at_commandline)
21513 {
cf2c3c16
TT
21514 /* This DW_MACRO_GNU_start_file was executed in the
21515 pass one. */
757a13d0
JK
21516 at_commandline = 0;
21517 }
21518 else
43f3e411 21519 current_file = macro_start_file (file, line, current_file, lh);
2e276125
JB
21520 }
21521 break;
21522
cf2c3c16 21523 case DW_MACRO_GNU_end_file:
2e276125 21524 if (! current_file)
4d3c2250 21525 complaint (&symfile_complaints,
3e43a32a
MS
21526 _("macro debug info has an unmatched "
21527 "`close_file' directive"));
2e276125
JB
21528 else
21529 {
21530 current_file = current_file->included_by;
21531 if (! current_file)
21532 {
cf2c3c16 21533 enum dwarf_macro_record_type next_type;
2e276125
JB
21534
21535 /* GCC circa March 2002 doesn't produce the zero
21536 type byte marking the end of the compilation
21537 unit. Complain if it's not there, but exit no
21538 matter what. */
21539
21540 /* Do we at least have room for a macinfo type byte? */
21541 if (mac_ptr >= mac_end)
21542 {
f664829e 21543 dwarf2_section_buffer_overflow_complaint (section);
2e276125
JB
21544 return;
21545 }
21546
21547 /* We don't increment mac_ptr here, so this is just
21548 a look-ahead. */
aead7601
SM
21549 next_type
21550 = (enum dwarf_macro_record_type) read_1_byte (abfd,
21551 mac_ptr);
2e276125 21552 if (next_type != 0)
4d3c2250 21553 complaint (&symfile_complaints,
3e43a32a
MS
21554 _("no terminating 0-type entry for "
21555 "macros in `.debug_macinfo' section"));
2e276125
JB
21556
21557 return;
21558 }
21559 }
21560 break;
21561
cf2c3c16 21562 case DW_MACRO_GNU_transparent_include:
36586728 21563 case DW_MACRO_GNU_transparent_include_alt:
cf2c3c16
TT
21564 {
21565 LONGEST offset;
8fc3fc34 21566 void **slot;
a036ba48
TT
21567 bfd *include_bfd = abfd;
21568 struct dwarf2_section_info *include_section = section;
d521ce57 21569 const gdb_byte *include_mac_end = mac_end;
a036ba48 21570 int is_dwz = section_is_dwz;
d521ce57 21571 const gdb_byte *new_mac_ptr;
cf2c3c16
TT
21572
21573 offset = read_offset_1 (abfd, mac_ptr, offset_size);
21574 mac_ptr += offset_size;
21575
a036ba48
TT
21576 if (macinfo_type == DW_MACRO_GNU_transparent_include_alt)
21577 {
21578 struct dwz_file *dwz = dwarf2_get_dwz_file ();
21579
4d663531 21580 dwarf2_read_section (objfile, &dwz->macro);
a036ba48 21581
a036ba48 21582 include_section = &dwz->macro;
a32a8923 21583 include_bfd = get_section_bfd_owner (include_section);
a036ba48
TT
21584 include_mac_end = dwz->macro.buffer + dwz->macro.size;
21585 is_dwz = 1;
21586 }
21587
21588 new_mac_ptr = include_section->buffer + offset;
21589 slot = htab_find_slot (include_hash, new_mac_ptr, INSERT);
21590
8fc3fc34
TT
21591 if (*slot != NULL)
21592 {
21593 /* This has actually happened; see
21594 http://sourceware.org/bugzilla/show_bug.cgi?id=13568. */
21595 complaint (&symfile_complaints,
21596 _("recursive DW_MACRO_GNU_transparent_include in "
21597 ".debug_macro section"));
21598 }
21599 else
21600 {
d521ce57 21601 *slot = (void *) new_mac_ptr;
36586728 21602
a036ba48 21603 dwarf_decode_macro_bytes (include_bfd, new_mac_ptr,
43f3e411 21604 include_mac_end, current_file, lh,
36586728 21605 section, section_is_gnu, is_dwz,
4d663531 21606 offset_size, include_hash);
8fc3fc34 21607
d521ce57 21608 htab_remove_elt (include_hash, (void *) new_mac_ptr);
8fc3fc34 21609 }
cf2c3c16
TT
21610 }
21611 break;
21612
2e276125 21613 case DW_MACINFO_vendor_ext:
cf2c3c16
TT
21614 if (!section_is_gnu)
21615 {
21616 unsigned int bytes_read;
2e276125 21617
ac298888
TT
21618 /* This reads the constant, but since we don't recognize
21619 any vendor extensions, we ignore it. */
21620 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
cf2c3c16
TT
21621 mac_ptr += bytes_read;
21622 read_direct_string (abfd, mac_ptr, &bytes_read);
21623 mac_ptr += bytes_read;
2e276125 21624
cf2c3c16
TT
21625 /* We don't recognize any vendor extensions. */
21626 break;
21627 }
21628 /* FALLTHROUGH */
21629
21630 default:
21631 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
f664829e 21632 mac_ptr, mac_end, abfd, offset_size,
cf2c3c16
TT
21633 section);
21634 if (mac_ptr == NULL)
21635 return;
21636 break;
2e276125 21637 }
757a13d0 21638 } while (macinfo_type != 0);
2e276125 21639}
8e19ed76 21640
cf2c3c16 21641static void
09262596 21642dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
43f3e411 21643 int section_is_gnu)
cf2c3c16 21644{
bb5ed363 21645 struct objfile *objfile = dwarf2_per_objfile->objfile;
09262596
DE
21646 struct line_header *lh = cu->line_header;
21647 bfd *abfd;
d521ce57 21648 const gdb_byte *mac_ptr, *mac_end;
cf2c3c16
TT
21649 struct macro_source_file *current_file = 0;
21650 enum dwarf_macro_record_type macinfo_type;
21651 unsigned int offset_size = cu->header.offset_size;
d521ce57 21652 const gdb_byte *opcode_definitions[256];
8fc3fc34
TT
21653 struct cleanup *cleanup;
21654 htab_t include_hash;
21655 void **slot;
09262596
DE
21656 struct dwarf2_section_info *section;
21657 const char *section_name;
21658
21659 if (cu->dwo_unit != NULL)
21660 {
21661 if (section_is_gnu)
21662 {
21663 section = &cu->dwo_unit->dwo_file->sections.macro;
21664 section_name = ".debug_macro.dwo";
21665 }
21666 else
21667 {
21668 section = &cu->dwo_unit->dwo_file->sections.macinfo;
21669 section_name = ".debug_macinfo.dwo";
21670 }
21671 }
21672 else
21673 {
21674 if (section_is_gnu)
21675 {
21676 section = &dwarf2_per_objfile->macro;
21677 section_name = ".debug_macro";
21678 }
21679 else
21680 {
21681 section = &dwarf2_per_objfile->macinfo;
21682 section_name = ".debug_macinfo";
21683 }
21684 }
cf2c3c16 21685
bb5ed363 21686 dwarf2_read_section (objfile, section);
cf2c3c16
TT
21687 if (section->buffer == NULL)
21688 {
fceca515 21689 complaint (&symfile_complaints, _("missing %s section"), section_name);
cf2c3c16
TT
21690 return;
21691 }
a32a8923 21692 abfd = get_section_bfd_owner (section);
cf2c3c16
TT
21693
21694 /* First pass: Find the name of the base filename.
21695 This filename is needed in order to process all macros whose definition
21696 (or undefinition) comes from the command line. These macros are defined
21697 before the first DW_MACINFO_start_file entry, and yet still need to be
21698 associated to the base file.
21699
21700 To determine the base file name, we scan the macro definitions until we
21701 reach the first DW_MACINFO_start_file entry. We then initialize
21702 CURRENT_FILE accordingly so that any macro definition found before the
21703 first DW_MACINFO_start_file can still be associated to the base file. */
21704
21705 mac_ptr = section->buffer + offset;
21706 mac_end = section->buffer + section->size;
21707
21708 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
21709 &offset_size, section_is_gnu);
21710 if (mac_ptr == NULL)
21711 {
21712 /* We already issued a complaint. */
21713 return;
21714 }
21715
21716 do
21717 {
21718 /* Do we at least have room for a macinfo type byte? */
21719 if (mac_ptr >= mac_end)
21720 {
21721 /* Complaint is printed during the second pass as GDB will probably
21722 stop the first pass earlier upon finding
21723 DW_MACINFO_start_file. */
21724 break;
21725 }
21726
aead7601 21727 macinfo_type = (enum dwarf_macro_record_type) read_1_byte (abfd, mac_ptr);
cf2c3c16
TT
21728 mac_ptr++;
21729
21730 /* Note that we rely on the fact that the corresponding GNU and
21731 DWARF constants are the same. */
21732 switch (macinfo_type)
21733 {
21734 /* A zero macinfo type indicates the end of the macro
21735 information. */
21736 case 0:
21737 break;
21738
21739 case DW_MACRO_GNU_define:
21740 case DW_MACRO_GNU_undef:
21741 /* Only skip the data by MAC_PTR. */
21742 {
21743 unsigned int bytes_read;
21744
21745 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
21746 mac_ptr += bytes_read;
21747 read_direct_string (abfd, mac_ptr, &bytes_read);
21748 mac_ptr += bytes_read;
21749 }
21750 break;
21751
21752 case DW_MACRO_GNU_start_file:
21753 {
21754 unsigned int bytes_read;
21755 int line, file;
21756
21757 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
21758 mac_ptr += bytes_read;
21759 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
21760 mac_ptr += bytes_read;
21761
43f3e411 21762 current_file = macro_start_file (file, line, current_file, lh);
cf2c3c16
TT
21763 }
21764 break;
21765
21766 case DW_MACRO_GNU_end_file:
21767 /* No data to skip by MAC_PTR. */
21768 break;
21769
21770 case DW_MACRO_GNU_define_indirect:
21771 case DW_MACRO_GNU_undef_indirect:
f7a35f02
TT
21772 case DW_MACRO_GNU_define_indirect_alt:
21773 case DW_MACRO_GNU_undef_indirect_alt:
cf2c3c16
TT
21774 {
21775 unsigned int bytes_read;
21776
21777 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
21778 mac_ptr += bytes_read;
21779 mac_ptr += offset_size;
21780 }
21781 break;
21782
21783 case DW_MACRO_GNU_transparent_include:
f7a35f02 21784 case DW_MACRO_GNU_transparent_include_alt:
cf2c3c16
TT
21785 /* Note that, according to the spec, a transparent include
21786 chain cannot call DW_MACRO_GNU_start_file. So, we can just
21787 skip this opcode. */
21788 mac_ptr += offset_size;
21789 break;
21790
21791 case DW_MACINFO_vendor_ext:
21792 /* Only skip the data by MAC_PTR. */
21793 if (!section_is_gnu)
21794 {
21795 unsigned int bytes_read;
21796
21797 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
21798 mac_ptr += bytes_read;
21799 read_direct_string (abfd, mac_ptr, &bytes_read);
21800 mac_ptr += bytes_read;
21801 }
21802 /* FALLTHROUGH */
21803
21804 default:
21805 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
f664829e 21806 mac_ptr, mac_end, abfd, offset_size,
cf2c3c16
TT
21807 section);
21808 if (mac_ptr == NULL)
21809 return;
21810 break;
21811 }
21812 } while (macinfo_type != 0 && current_file == NULL);
21813
21814 /* Second pass: Process all entries.
21815
21816 Use the AT_COMMAND_LINE flag to determine whether we are still processing
21817 command-line macro definitions/undefinitions. This flag is unset when we
21818 reach the first DW_MACINFO_start_file entry. */
21819
8fc3fc34
TT
21820 include_hash = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
21821 NULL, xcalloc, xfree);
21822 cleanup = make_cleanup_htab_delete (include_hash);
21823 mac_ptr = section->buffer + offset;
21824 slot = htab_find_slot (include_hash, mac_ptr, INSERT);
d521ce57 21825 *slot = (void *) mac_ptr;
8fc3fc34 21826 dwarf_decode_macro_bytes (abfd, mac_ptr, mac_end,
43f3e411 21827 current_file, lh, section,
4d663531 21828 section_is_gnu, 0, offset_size, include_hash);
8fc3fc34 21829 do_cleanups (cleanup);
cf2c3c16
TT
21830}
21831
8e19ed76 21832/* Check if the attribute's form is a DW_FORM_block*
0963b4bd 21833 if so return true else false. */
380bca97 21834
8e19ed76 21835static int
6e5a29e1 21836attr_form_is_block (const struct attribute *attr)
8e19ed76
PS
21837{
21838 return (attr == NULL ? 0 :
21839 attr->form == DW_FORM_block1
21840 || attr->form == DW_FORM_block2
21841 || attr->form == DW_FORM_block4
2dc7f7b3
TT
21842 || attr->form == DW_FORM_block
21843 || attr->form == DW_FORM_exprloc);
8e19ed76 21844}
4c2df51b 21845
c6a0999f
JB
21846/* Return non-zero if ATTR's value is a section offset --- classes
21847 lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
21848 You may use DW_UNSND (attr) to retrieve such offsets.
21849
21850 Section 7.5.4, "Attribute Encodings", explains that no attribute
21851 may have a value that belongs to more than one of these classes; it
21852 would be ambiguous if we did, because we use the same forms for all
21853 of them. */
380bca97 21854
3690dd37 21855static int
6e5a29e1 21856attr_form_is_section_offset (const struct attribute *attr)
3690dd37
JB
21857{
21858 return (attr->form == DW_FORM_data4
2dc7f7b3
TT
21859 || attr->form == DW_FORM_data8
21860 || attr->form == DW_FORM_sec_offset);
3690dd37
JB
21861}
21862
3690dd37
JB
21863/* Return non-zero if ATTR's value falls in the 'constant' class, or
21864 zero otherwise. When this function returns true, you can apply
21865 dwarf2_get_attr_constant_value to it.
21866
21867 However, note that for some attributes you must check
21868 attr_form_is_section_offset before using this test. DW_FORM_data4
21869 and DW_FORM_data8 are members of both the constant class, and of
21870 the classes that contain offsets into other debug sections
21871 (lineptr, loclistptr, macptr or rangelistptr). The DWARF spec says
21872 that, if an attribute's can be either a constant or one of the
21873 section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
21874 taken as section offsets, not constants. */
380bca97 21875
3690dd37 21876static int
6e5a29e1 21877attr_form_is_constant (const struct attribute *attr)
3690dd37
JB
21878{
21879 switch (attr->form)
21880 {
21881 case DW_FORM_sdata:
21882 case DW_FORM_udata:
21883 case DW_FORM_data1:
21884 case DW_FORM_data2:
21885 case DW_FORM_data4:
21886 case DW_FORM_data8:
21887 return 1;
21888 default:
21889 return 0;
21890 }
21891}
21892
7771576e
SA
21893
21894/* DW_ADDR is always stored already as sect_offset; despite for the forms
21895 besides DW_FORM_ref_addr it is stored as cu_offset in the DWARF file. */
21896
21897static int
6e5a29e1 21898attr_form_is_ref (const struct attribute *attr)
7771576e
SA
21899{
21900 switch (attr->form)
21901 {
21902 case DW_FORM_ref_addr:
21903 case DW_FORM_ref1:
21904 case DW_FORM_ref2:
21905 case DW_FORM_ref4:
21906 case DW_FORM_ref8:
21907 case DW_FORM_ref_udata:
21908 case DW_FORM_GNU_ref_alt:
21909 return 1;
21910 default:
21911 return 0;
21912 }
21913}
21914
3019eac3
DE
21915/* Return the .debug_loc section to use for CU.
21916 For DWO files use .debug_loc.dwo. */
21917
21918static struct dwarf2_section_info *
21919cu_debug_loc_section (struct dwarf2_cu *cu)
21920{
21921 if (cu->dwo_unit)
21922 return &cu->dwo_unit->dwo_file->sections.loc;
21923 return &dwarf2_per_objfile->loc;
21924}
21925
8cf6f0b1
TT
21926/* A helper function that fills in a dwarf2_loclist_baton. */
21927
21928static void
21929fill_in_loclist_baton (struct dwarf2_cu *cu,
21930 struct dwarf2_loclist_baton *baton,
ff39bb5e 21931 const struct attribute *attr)
8cf6f0b1 21932{
3019eac3
DE
21933 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
21934
21935 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
8cf6f0b1
TT
21936
21937 baton->per_cu = cu->per_cu;
21938 gdb_assert (baton->per_cu);
21939 /* We don't know how long the location list is, but make sure we
21940 don't run off the edge of the section. */
3019eac3
DE
21941 baton->size = section->size - DW_UNSND (attr);
21942 baton->data = section->buffer + DW_UNSND (attr);
8cf6f0b1 21943 baton->base_address = cu->base_address;
f664829e 21944 baton->from_dwo = cu->dwo_unit != NULL;
8cf6f0b1
TT
21945}
21946
4c2df51b 21947static void
ff39bb5e 21948dwarf2_symbol_mark_computed (const struct attribute *attr, struct symbol *sym,
f1e6e072 21949 struct dwarf2_cu *cu, int is_block)
4c2df51b 21950{
bb5ed363 21951 struct objfile *objfile = dwarf2_per_objfile->objfile;
3019eac3 21952 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
bb5ed363 21953
3690dd37 21954 if (attr_form_is_section_offset (attr)
3019eac3 21955 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
99bcc461
DJ
21956 the section. If so, fall through to the complaint in the
21957 other branch. */
3019eac3 21958 && DW_UNSND (attr) < dwarf2_section_size (objfile, section))
4c2df51b 21959 {
0d53c4c4 21960 struct dwarf2_loclist_baton *baton;
4c2df51b 21961
8d749320 21962 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_loclist_baton);
4c2df51b 21963
8cf6f0b1 21964 fill_in_loclist_baton (cu, baton, attr);
be391dca 21965
d00adf39 21966 if (cu->base_known == 0)
0d53c4c4 21967 complaint (&symfile_complaints,
3e43a32a
MS
21968 _("Location list used without "
21969 "specifying the CU base address."));
4c2df51b 21970
f1e6e072
TT
21971 SYMBOL_ACLASS_INDEX (sym) = (is_block
21972 ? dwarf2_loclist_block_index
21973 : dwarf2_loclist_index);
0d53c4c4
DJ
21974 SYMBOL_LOCATION_BATON (sym) = baton;
21975 }
21976 else
21977 {
21978 struct dwarf2_locexpr_baton *baton;
21979
8d749320 21980 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
ae0d2f24
UW
21981 baton->per_cu = cu->per_cu;
21982 gdb_assert (baton->per_cu);
0d53c4c4
DJ
21983
21984 if (attr_form_is_block (attr))
21985 {
21986 /* Note that we're just copying the block's data pointer
21987 here, not the actual data. We're still pointing into the
6502dd73
DJ
21988 info_buffer for SYM's objfile; right now we never release
21989 that buffer, but when we do clean up properly this may
21990 need to change. */
0d53c4c4
DJ
21991 baton->size = DW_BLOCK (attr)->size;
21992 baton->data = DW_BLOCK (attr)->data;
21993 }
21994 else
21995 {
21996 dwarf2_invalid_attrib_class_complaint ("location description",
21997 SYMBOL_NATURAL_NAME (sym));
21998 baton->size = 0;
0d53c4c4 21999 }
6e70227d 22000
f1e6e072
TT
22001 SYMBOL_ACLASS_INDEX (sym) = (is_block
22002 ? dwarf2_locexpr_block_index
22003 : dwarf2_locexpr_index);
0d53c4c4
DJ
22004 SYMBOL_LOCATION_BATON (sym) = baton;
22005 }
4c2df51b 22006}
6502dd73 22007
9aa1f1e3
TT
22008/* Return the OBJFILE associated with the compilation unit CU. If CU
22009 came from a separate debuginfo file, then the master objfile is
22010 returned. */
ae0d2f24
UW
22011
22012struct objfile *
22013dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
22014{
9291a0cd 22015 struct objfile *objfile = per_cu->objfile;
ae0d2f24
UW
22016
22017 /* Return the master objfile, so that we can report and look up the
22018 correct file containing this variable. */
22019 if (objfile->separate_debug_objfile_backlink)
22020 objfile = objfile->separate_debug_objfile_backlink;
22021
22022 return objfile;
22023}
22024
96408a79
SA
22025/* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
22026 (CU_HEADERP is unused in such case) or prepare a temporary copy at
22027 CU_HEADERP first. */
22028
22029static const struct comp_unit_head *
22030per_cu_header_read_in (struct comp_unit_head *cu_headerp,
22031 struct dwarf2_per_cu_data *per_cu)
22032{
d521ce57 22033 const gdb_byte *info_ptr;
96408a79
SA
22034
22035 if (per_cu->cu)
22036 return &per_cu->cu->header;
22037
8a0459fd 22038 info_ptr = per_cu->section->buffer + per_cu->offset.sect_off;
96408a79
SA
22039
22040 memset (cu_headerp, 0, sizeof (*cu_headerp));
0bc3a05c 22041 read_comp_unit_head (cu_headerp, info_ptr, per_cu->objfile->obfd);
96408a79
SA
22042
22043 return cu_headerp;
22044}
22045
ae0d2f24
UW
22046/* Return the address size given in the compilation unit header for CU. */
22047
98714339 22048int
ae0d2f24
UW
22049dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
22050{
96408a79
SA
22051 struct comp_unit_head cu_header_local;
22052 const struct comp_unit_head *cu_headerp;
c471e790 22053
96408a79
SA
22054 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
22055
22056 return cu_headerp->addr_size;
ae0d2f24
UW
22057}
22058
9eae7c52
TT
22059/* Return the offset size given in the compilation unit header for CU. */
22060
22061int
22062dwarf2_per_cu_offset_size (struct dwarf2_per_cu_data *per_cu)
22063{
96408a79
SA
22064 struct comp_unit_head cu_header_local;
22065 const struct comp_unit_head *cu_headerp;
9c6c53f7 22066
96408a79
SA
22067 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
22068
22069 return cu_headerp->offset_size;
22070}
22071
22072/* See its dwarf2loc.h declaration. */
22073
22074int
22075dwarf2_per_cu_ref_addr_size (struct dwarf2_per_cu_data *per_cu)
22076{
22077 struct comp_unit_head cu_header_local;
22078 const struct comp_unit_head *cu_headerp;
22079
22080 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
22081
22082 if (cu_headerp->version == 2)
22083 return cu_headerp->addr_size;
22084 else
22085 return cu_headerp->offset_size;
181cebd4
JK
22086}
22087
9aa1f1e3
TT
22088/* Return the text offset of the CU. The returned offset comes from
22089 this CU's objfile. If this objfile came from a separate debuginfo
22090 file, then the offset may be different from the corresponding
22091 offset in the parent objfile. */
22092
22093CORE_ADDR
22094dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
22095{
bb3fa9d0 22096 struct objfile *objfile = per_cu->objfile;
9aa1f1e3
TT
22097
22098 return ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
22099}
22100
348e048f
DE
22101/* Locate the .debug_info compilation unit from CU's objfile which contains
22102 the DIE at OFFSET. Raises an error on failure. */
ae038cb0
DJ
22103
22104static struct dwarf2_per_cu_data *
b64f50a1 22105dwarf2_find_containing_comp_unit (sect_offset offset,
36586728 22106 unsigned int offset_in_dwz,
ae038cb0
DJ
22107 struct objfile *objfile)
22108{
22109 struct dwarf2_per_cu_data *this_cu;
22110 int low, high;
36586728 22111 const sect_offset *cu_off;
ae038cb0 22112
ae038cb0
DJ
22113 low = 0;
22114 high = dwarf2_per_objfile->n_comp_units - 1;
22115 while (high > low)
22116 {
36586728 22117 struct dwarf2_per_cu_data *mid_cu;
ae038cb0 22118 int mid = low + (high - low) / 2;
9a619af0 22119
36586728
TT
22120 mid_cu = dwarf2_per_objfile->all_comp_units[mid];
22121 cu_off = &mid_cu->offset;
22122 if (mid_cu->is_dwz > offset_in_dwz
22123 || (mid_cu->is_dwz == offset_in_dwz
22124 && cu_off->sect_off >= offset.sect_off))
ae038cb0
DJ
22125 high = mid;
22126 else
22127 low = mid + 1;
22128 }
22129 gdb_assert (low == high);
36586728
TT
22130 this_cu = dwarf2_per_objfile->all_comp_units[low];
22131 cu_off = &this_cu->offset;
22132 if (this_cu->is_dwz != offset_in_dwz || cu_off->sect_off > offset.sect_off)
ae038cb0 22133 {
36586728 22134 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
8a3fe4f8
AC
22135 error (_("Dwarf Error: could not find partial DIE containing "
22136 "offset 0x%lx [in module %s]"),
b64f50a1 22137 (long) offset.sect_off, bfd_get_filename (objfile->obfd));
10b3939b 22138
b64f50a1
JK
22139 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->offset.sect_off
22140 <= offset.sect_off);
ae038cb0
DJ
22141 return dwarf2_per_objfile->all_comp_units[low-1];
22142 }
22143 else
22144 {
22145 this_cu = dwarf2_per_objfile->all_comp_units[low];
22146 if (low == dwarf2_per_objfile->n_comp_units - 1
b64f50a1
JK
22147 && offset.sect_off >= this_cu->offset.sect_off + this_cu->length)
22148 error (_("invalid dwarf2 offset %u"), offset.sect_off);
22149 gdb_assert (offset.sect_off < this_cu->offset.sect_off + this_cu->length);
ae038cb0
DJ
22150 return this_cu;
22151 }
22152}
22153
23745b47 22154/* Initialize dwarf2_cu CU, owned by PER_CU. */
93311388 22155
9816fde3 22156static void
23745b47 22157init_one_comp_unit (struct dwarf2_cu *cu, struct dwarf2_per_cu_data *per_cu)
93311388 22158{
9816fde3 22159 memset (cu, 0, sizeof (*cu));
23745b47
DE
22160 per_cu->cu = cu;
22161 cu->per_cu = per_cu;
22162 cu->objfile = per_cu->objfile;
93311388 22163 obstack_init (&cu->comp_unit_obstack);
9816fde3
JK
22164}
22165
22166/* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
22167
22168static void
95554aad
TT
22169prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
22170 enum language pretend_language)
9816fde3
JK
22171{
22172 struct attribute *attr;
22173
22174 /* Set the language we're debugging. */
22175 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
22176 if (attr)
22177 set_cu_language (DW_UNSND (attr), cu);
22178 else
9cded63f 22179 {
95554aad 22180 cu->language = pretend_language;
9cded63f
TT
22181 cu->language_defn = language_def (cu->language);
22182 }
dee91e82 22183
7d45c7c3 22184 cu->producer = dwarf2_string_attr (comp_unit_die, DW_AT_producer, cu);
93311388
DE
22185}
22186
ae038cb0
DJ
22187/* Release one cached compilation unit, CU. We unlink it from the tree
22188 of compilation units, but we don't remove it from the read_in_chain;
93311388
DE
22189 the caller is responsible for that.
22190 NOTE: DATA is a void * because this function is also used as a
22191 cleanup routine. */
ae038cb0
DJ
22192
22193static void
68dc6402 22194free_heap_comp_unit (void *data)
ae038cb0 22195{
9a3c8263 22196 struct dwarf2_cu *cu = (struct dwarf2_cu *) data;
ae038cb0 22197
23745b47
DE
22198 gdb_assert (cu->per_cu != NULL);
22199 cu->per_cu->cu = NULL;
ae038cb0
DJ
22200 cu->per_cu = NULL;
22201
22202 obstack_free (&cu->comp_unit_obstack, NULL);
22203
22204 xfree (cu);
22205}
22206
72bf9492 22207/* This cleanup function is passed the address of a dwarf2_cu on the stack
ae038cb0 22208 when we're finished with it. We can't free the pointer itself, but be
dee91e82 22209 sure to unlink it from the cache. Also release any associated storage. */
72bf9492
DJ
22210
22211static void
22212free_stack_comp_unit (void *data)
22213{
9a3c8263 22214 struct dwarf2_cu *cu = (struct dwarf2_cu *) data;
72bf9492 22215
23745b47
DE
22216 gdb_assert (cu->per_cu != NULL);
22217 cu->per_cu->cu = NULL;
22218 cu->per_cu = NULL;
22219
72bf9492
DJ
22220 obstack_free (&cu->comp_unit_obstack, NULL);
22221 cu->partial_dies = NULL;
ae038cb0
DJ
22222}
22223
22224/* Free all cached compilation units. */
22225
22226static void
22227free_cached_comp_units (void *data)
22228{
22229 struct dwarf2_per_cu_data *per_cu, **last_chain;
22230
22231 per_cu = dwarf2_per_objfile->read_in_chain;
22232 last_chain = &dwarf2_per_objfile->read_in_chain;
22233 while (per_cu != NULL)
22234 {
22235 struct dwarf2_per_cu_data *next_cu;
22236
22237 next_cu = per_cu->cu->read_in_chain;
22238
68dc6402 22239 free_heap_comp_unit (per_cu->cu);
ae038cb0
DJ
22240 *last_chain = next_cu;
22241
22242 per_cu = next_cu;
22243 }
22244}
22245
22246/* Increase the age counter on each cached compilation unit, and free
22247 any that are too old. */
22248
22249static void
22250age_cached_comp_units (void)
22251{
22252 struct dwarf2_per_cu_data *per_cu, **last_chain;
22253
22254 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
22255 per_cu = dwarf2_per_objfile->read_in_chain;
22256 while (per_cu != NULL)
22257 {
22258 per_cu->cu->last_used ++;
b4f54984 22259 if (per_cu->cu->last_used <= dwarf_max_cache_age)
ae038cb0
DJ
22260 dwarf2_mark (per_cu->cu);
22261 per_cu = per_cu->cu->read_in_chain;
22262 }
22263
22264 per_cu = dwarf2_per_objfile->read_in_chain;
22265 last_chain = &dwarf2_per_objfile->read_in_chain;
22266 while (per_cu != NULL)
22267 {
22268 struct dwarf2_per_cu_data *next_cu;
22269
22270 next_cu = per_cu->cu->read_in_chain;
22271
22272 if (!per_cu->cu->mark)
22273 {
68dc6402 22274 free_heap_comp_unit (per_cu->cu);
ae038cb0
DJ
22275 *last_chain = next_cu;
22276 }
22277 else
22278 last_chain = &per_cu->cu->read_in_chain;
22279
22280 per_cu = next_cu;
22281 }
22282}
22283
22284/* Remove a single compilation unit from the cache. */
22285
22286static void
dee91e82 22287free_one_cached_comp_unit (struct dwarf2_per_cu_data *target_per_cu)
ae038cb0
DJ
22288{
22289 struct dwarf2_per_cu_data *per_cu, **last_chain;
22290
22291 per_cu = dwarf2_per_objfile->read_in_chain;
22292 last_chain = &dwarf2_per_objfile->read_in_chain;
22293 while (per_cu != NULL)
22294 {
22295 struct dwarf2_per_cu_data *next_cu;
22296
22297 next_cu = per_cu->cu->read_in_chain;
22298
dee91e82 22299 if (per_cu == target_per_cu)
ae038cb0 22300 {
68dc6402 22301 free_heap_comp_unit (per_cu->cu);
dee91e82 22302 per_cu->cu = NULL;
ae038cb0
DJ
22303 *last_chain = next_cu;
22304 break;
22305 }
22306 else
22307 last_chain = &per_cu->cu->read_in_chain;
22308
22309 per_cu = next_cu;
22310 }
22311}
22312
fe3e1990
DJ
22313/* Release all extra memory associated with OBJFILE. */
22314
22315void
22316dwarf2_free_objfile (struct objfile *objfile)
22317{
9a3c8263
SM
22318 dwarf2_per_objfile
22319 = (struct dwarf2_per_objfile *) objfile_data (objfile,
22320 dwarf2_objfile_data_key);
fe3e1990
DJ
22321
22322 if (dwarf2_per_objfile == NULL)
22323 return;
22324
22325 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
22326 free_cached_comp_units (NULL);
22327
7b9f3c50
DE
22328 if (dwarf2_per_objfile->quick_file_names_table)
22329 htab_delete (dwarf2_per_objfile->quick_file_names_table);
9291a0cd 22330
527f3840
JK
22331 if (dwarf2_per_objfile->line_header_hash)
22332 htab_delete (dwarf2_per_objfile->line_header_hash);
22333
fe3e1990
DJ
22334 /* Everything else should be on the objfile obstack. */
22335}
22336
dee91e82
DE
22337/* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
22338 We store these in a hash table separate from the DIEs, and preserve them
22339 when the DIEs are flushed out of cache.
22340
22341 The CU "per_cu" pointer is needed because offset alone is not enough to
3019eac3 22342 uniquely identify the type. A file may have multiple .debug_types sections,
c88ee1f0
DE
22343 or the type may come from a DWO file. Furthermore, while it's more logical
22344 to use per_cu->section+offset, with Fission the section with the data is in
22345 the DWO file but we don't know that section at the point we need it.
22346 We have to use something in dwarf2_per_cu_data (or the pointer to it)
22347 because we can enter the lookup routine, get_die_type_at_offset, from
22348 outside this file, and thus won't necessarily have PER_CU->cu.
22349 Fortunately, PER_CU is stable for the life of the objfile. */
1c379e20 22350
dee91e82 22351struct dwarf2_per_cu_offset_and_type
1c379e20 22352{
dee91e82 22353 const struct dwarf2_per_cu_data *per_cu;
b64f50a1 22354 sect_offset offset;
1c379e20
DJ
22355 struct type *type;
22356};
22357
dee91e82 22358/* Hash function for a dwarf2_per_cu_offset_and_type. */
1c379e20
DJ
22359
22360static hashval_t
dee91e82 22361per_cu_offset_and_type_hash (const void *item)
1c379e20 22362{
9a3c8263
SM
22363 const struct dwarf2_per_cu_offset_and_type *ofs
22364 = (const struct dwarf2_per_cu_offset_and_type *) item;
9a619af0 22365
dee91e82 22366 return (uintptr_t) ofs->per_cu + ofs->offset.sect_off;
1c379e20
DJ
22367}
22368
dee91e82 22369/* Equality function for a dwarf2_per_cu_offset_and_type. */
1c379e20
DJ
22370
22371static int
dee91e82 22372per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
1c379e20 22373{
9a3c8263
SM
22374 const struct dwarf2_per_cu_offset_and_type *ofs_lhs
22375 = (const struct dwarf2_per_cu_offset_and_type *) item_lhs;
22376 const struct dwarf2_per_cu_offset_and_type *ofs_rhs
22377 = (const struct dwarf2_per_cu_offset_and_type *) item_rhs;
9a619af0 22378
dee91e82
DE
22379 return (ofs_lhs->per_cu == ofs_rhs->per_cu
22380 && ofs_lhs->offset.sect_off == ofs_rhs->offset.sect_off);
1c379e20
DJ
22381}
22382
22383/* Set the type associated with DIE to TYPE. Save it in CU's hash
7e314c57
JK
22384 table if necessary. For convenience, return TYPE.
22385
22386 The DIEs reading must have careful ordering to:
22387 * Not cause infite loops trying to read in DIEs as a prerequisite for
22388 reading current DIE.
22389 * Not trying to dereference contents of still incompletely read in types
22390 while reading in other DIEs.
22391 * Enable referencing still incompletely read in types just by a pointer to
22392 the type without accessing its fields.
22393
22394 Therefore caller should follow these rules:
22395 * Try to fetch any prerequisite types we may need to build this DIE type
22396 before building the type and calling set_die_type.
e71ec853 22397 * After building type call set_die_type for current DIE as soon as
7e314c57
JK
22398 possible before fetching more types to complete the current type.
22399 * Make the type as complete as possible before fetching more types. */
1c379e20 22400
f792889a 22401static struct type *
1c379e20
DJ
22402set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
22403{
dee91e82 22404 struct dwarf2_per_cu_offset_and_type **slot, ofs;
673bfd45 22405 struct objfile *objfile = cu->objfile;
3cdcd0ce
JB
22406 struct attribute *attr;
22407 struct dynamic_prop prop;
1c379e20 22408
b4ba55a1
JB
22409 /* For Ada types, make sure that the gnat-specific data is always
22410 initialized (if not already set). There are a few types where
22411 we should not be doing so, because the type-specific area is
22412 already used to hold some other piece of info (eg: TYPE_CODE_FLT
22413 where the type-specific area is used to store the floatformat).
22414 But this is not a problem, because the gnat-specific information
22415 is actually not needed for these types. */
22416 if (need_gnat_info (cu)
22417 && TYPE_CODE (type) != TYPE_CODE_FUNC
22418 && TYPE_CODE (type) != TYPE_CODE_FLT
09e2d7c7
DE
22419 && TYPE_CODE (type) != TYPE_CODE_METHODPTR
22420 && TYPE_CODE (type) != TYPE_CODE_MEMBERPTR
22421 && TYPE_CODE (type) != TYPE_CODE_METHOD
b4ba55a1
JB
22422 && !HAVE_GNAT_AUX_INFO (type))
22423 INIT_GNAT_SPECIFIC (type);
22424
3f2f83dd
KB
22425 /* Read DW_AT_allocated and set in type. */
22426 attr = dwarf2_attr (die, DW_AT_allocated, cu);
22427 if (attr_form_is_block (attr))
22428 {
22429 if (attr_to_dynamic_prop (attr, die, cu, &prop))
22430 add_dyn_prop (DYN_PROP_ALLOCATED, prop, type, objfile);
22431 }
22432 else if (attr != NULL)
22433 {
22434 complaint (&symfile_complaints,
22435 _("DW_AT_allocated has the wrong form (%s) at DIE 0x%x"),
22436 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
22437 die->offset.sect_off);
22438 }
22439
22440 /* Read DW_AT_associated and set in type. */
22441 attr = dwarf2_attr (die, DW_AT_associated, cu);
22442 if (attr_form_is_block (attr))
22443 {
22444 if (attr_to_dynamic_prop (attr, die, cu, &prop))
22445 add_dyn_prop (DYN_PROP_ASSOCIATED, prop, type, objfile);
22446 }
22447 else if (attr != NULL)
22448 {
22449 complaint (&symfile_complaints,
22450 _("DW_AT_associated has the wrong form (%s) at DIE 0x%x"),
22451 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
22452 die->offset.sect_off);
22453 }
22454
3cdcd0ce
JB
22455 /* Read DW_AT_data_location and set in type. */
22456 attr = dwarf2_attr (die, DW_AT_data_location, cu);
22457 if (attr_to_dynamic_prop (attr, die, cu, &prop))
93a8e227 22458 add_dyn_prop (DYN_PROP_DATA_LOCATION, prop, type, objfile);
3cdcd0ce 22459
dee91e82 22460 if (dwarf2_per_objfile->die_type_hash == NULL)
f792889a 22461 {
dee91e82
DE
22462 dwarf2_per_objfile->die_type_hash =
22463 htab_create_alloc_ex (127,
22464 per_cu_offset_and_type_hash,
22465 per_cu_offset_and_type_eq,
22466 NULL,
22467 &objfile->objfile_obstack,
22468 hashtab_obstack_allocate,
22469 dummy_obstack_deallocate);
f792889a 22470 }
1c379e20 22471
dee91e82 22472 ofs.per_cu = cu->per_cu;
1c379e20
DJ
22473 ofs.offset = die->offset;
22474 ofs.type = type;
dee91e82
DE
22475 slot = (struct dwarf2_per_cu_offset_and_type **)
22476 htab_find_slot (dwarf2_per_objfile->die_type_hash, &ofs, INSERT);
7e314c57
JK
22477 if (*slot)
22478 complaint (&symfile_complaints,
22479 _("A problem internal to GDB: DIE 0x%x has type already set"),
b64f50a1 22480 die->offset.sect_off);
8d749320
SM
22481 *slot = XOBNEW (&objfile->objfile_obstack,
22482 struct dwarf2_per_cu_offset_and_type);
1c379e20 22483 **slot = ofs;
f792889a 22484 return type;
1c379e20
DJ
22485}
22486
02142a6c
DE
22487/* Look up the type for the die at OFFSET in PER_CU in die_type_hash,
22488 or return NULL if the die does not have a saved type. */
1c379e20
DJ
22489
22490static struct type *
b64f50a1 22491get_die_type_at_offset (sect_offset offset,
673bfd45 22492 struct dwarf2_per_cu_data *per_cu)
1c379e20 22493{
dee91e82 22494 struct dwarf2_per_cu_offset_and_type *slot, ofs;
f792889a 22495
dee91e82 22496 if (dwarf2_per_objfile->die_type_hash == NULL)
f792889a 22497 return NULL;
1c379e20 22498
dee91e82 22499 ofs.per_cu = per_cu;
673bfd45 22500 ofs.offset = offset;
9a3c8263
SM
22501 slot = ((struct dwarf2_per_cu_offset_and_type *)
22502 htab_find (dwarf2_per_objfile->die_type_hash, &ofs));
1c379e20
DJ
22503 if (slot)
22504 return slot->type;
22505 else
22506 return NULL;
22507}
22508
02142a6c 22509/* Look up the type for DIE in CU in die_type_hash,
673bfd45
DE
22510 or return NULL if DIE does not have a saved type. */
22511
22512static struct type *
22513get_die_type (struct die_info *die, struct dwarf2_cu *cu)
22514{
22515 return get_die_type_at_offset (die->offset, cu->per_cu);
22516}
22517
10b3939b
DJ
22518/* Add a dependence relationship from CU to REF_PER_CU. */
22519
22520static void
22521dwarf2_add_dependence (struct dwarf2_cu *cu,
22522 struct dwarf2_per_cu_data *ref_per_cu)
22523{
22524 void **slot;
22525
22526 if (cu->dependencies == NULL)
22527 cu->dependencies
22528 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
22529 NULL, &cu->comp_unit_obstack,
22530 hashtab_obstack_allocate,
22531 dummy_obstack_deallocate);
22532
22533 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
22534 if (*slot == NULL)
22535 *slot = ref_per_cu;
22536}
1c379e20 22537
f504f079
DE
22538/* Subroutine of dwarf2_mark to pass to htab_traverse.
22539 Set the mark field in every compilation unit in the
ae038cb0
DJ
22540 cache that we must keep because we are keeping CU. */
22541
10b3939b
DJ
22542static int
22543dwarf2_mark_helper (void **slot, void *data)
22544{
22545 struct dwarf2_per_cu_data *per_cu;
22546
22547 per_cu = (struct dwarf2_per_cu_data *) *slot;
d07ed419
JK
22548
22549 /* cu->dependencies references may not yet have been ever read if QUIT aborts
22550 reading of the chain. As such dependencies remain valid it is not much
22551 useful to track and undo them during QUIT cleanups. */
22552 if (per_cu->cu == NULL)
22553 return 1;
22554
10b3939b
DJ
22555 if (per_cu->cu->mark)
22556 return 1;
22557 per_cu->cu->mark = 1;
22558
22559 if (per_cu->cu->dependencies != NULL)
22560 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
22561
22562 return 1;
22563}
22564
f504f079
DE
22565/* Set the mark field in CU and in every other compilation unit in the
22566 cache that we must keep because we are keeping CU. */
22567
ae038cb0
DJ
22568static void
22569dwarf2_mark (struct dwarf2_cu *cu)
22570{
22571 if (cu->mark)
22572 return;
22573 cu->mark = 1;
10b3939b
DJ
22574 if (cu->dependencies != NULL)
22575 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
ae038cb0
DJ
22576}
22577
22578static void
22579dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
22580{
22581 while (per_cu)
22582 {
22583 per_cu->cu->mark = 0;
22584 per_cu = per_cu->cu->read_in_chain;
22585 }
72bf9492
DJ
22586}
22587
72bf9492
DJ
22588/* Trivial hash function for partial_die_info: the hash value of a DIE
22589 is its offset in .debug_info for this objfile. */
22590
22591static hashval_t
22592partial_die_hash (const void *item)
22593{
9a3c8263
SM
22594 const struct partial_die_info *part_die
22595 = (const struct partial_die_info *) item;
9a619af0 22596
b64f50a1 22597 return part_die->offset.sect_off;
72bf9492
DJ
22598}
22599
22600/* Trivial comparison function for partial_die_info structures: two DIEs
22601 are equal if they have the same offset. */
22602
22603static int
22604partial_die_eq (const void *item_lhs, const void *item_rhs)
22605{
9a3c8263
SM
22606 const struct partial_die_info *part_die_lhs
22607 = (const struct partial_die_info *) item_lhs;
22608 const struct partial_die_info *part_die_rhs
22609 = (const struct partial_die_info *) item_rhs;
9a619af0 22610
b64f50a1 22611 return part_die_lhs->offset.sect_off == part_die_rhs->offset.sect_off;
72bf9492
DJ
22612}
22613
b4f54984
DE
22614static struct cmd_list_element *set_dwarf_cmdlist;
22615static struct cmd_list_element *show_dwarf_cmdlist;
ae038cb0
DJ
22616
22617static void
b4f54984 22618set_dwarf_cmd (char *args, int from_tty)
ae038cb0 22619{
b4f54984 22620 help_list (set_dwarf_cmdlist, "maintenance set dwarf ", all_commands,
635c7e8a 22621 gdb_stdout);
ae038cb0
DJ
22622}
22623
22624static void
b4f54984 22625show_dwarf_cmd (char *args, int from_tty)
6e70227d 22626{
b4f54984 22627 cmd_show_list (show_dwarf_cmdlist, from_tty, "");
ae038cb0
DJ
22628}
22629
4bf44c1c 22630/* Free data associated with OBJFILE, if necessary. */
dce234bc
PP
22631
22632static void
c1bd65d0 22633dwarf2_per_objfile_free (struct objfile *objfile, void *d)
dce234bc 22634{
9a3c8263 22635 struct dwarf2_per_objfile *data = (struct dwarf2_per_objfile *) d;
8b70b953 22636 int ix;
8b70b953 22637
626f2d1c
TT
22638 /* Make sure we don't accidentally use dwarf2_per_objfile while
22639 cleaning up. */
22640 dwarf2_per_objfile = NULL;
22641
59b0c7c1
JB
22642 for (ix = 0; ix < data->n_comp_units; ++ix)
22643 VEC_free (dwarf2_per_cu_ptr, data->all_comp_units[ix]->imported_symtabs);
796a7ff8 22644
59b0c7c1 22645 for (ix = 0; ix < data->n_type_units; ++ix)
796a7ff8 22646 VEC_free (dwarf2_per_cu_ptr,
59b0c7c1
JB
22647 data->all_type_units[ix]->per_cu.imported_symtabs);
22648 xfree (data->all_type_units);
95554aad 22649
8b70b953 22650 VEC_free (dwarf2_section_info_def, data->types);
3019eac3
DE
22651
22652 if (data->dwo_files)
22653 free_dwo_files (data->dwo_files, objfile);
5c6fa7ab
DE
22654 if (data->dwp_file)
22655 gdb_bfd_unref (data->dwp_file->dbfd);
36586728
TT
22656
22657 if (data->dwz_file && data->dwz_file->dwz_bfd)
22658 gdb_bfd_unref (data->dwz_file->dwz_bfd);
9291a0cd
TT
22659}
22660
22661\f
ae2de4f8 22662/* The "save gdb-index" command. */
9291a0cd
TT
22663
22664/* The contents of the hash table we create when building the string
22665 table. */
22666struct strtab_entry
22667{
22668 offset_type offset;
22669 const char *str;
22670};
22671
559a7a62
JK
22672/* Hash function for a strtab_entry.
22673
22674 Function is used only during write_hash_table so no index format backward
22675 compatibility is needed. */
b89be57b 22676
9291a0cd
TT
22677static hashval_t
22678hash_strtab_entry (const void *e)
22679{
9a3c8263 22680 const struct strtab_entry *entry = (const struct strtab_entry *) e;
559a7a62 22681 return mapped_index_string_hash (INT_MAX, entry->str);
9291a0cd
TT
22682}
22683
22684/* Equality function for a strtab_entry. */
b89be57b 22685
9291a0cd
TT
22686static int
22687eq_strtab_entry (const void *a, const void *b)
22688{
9a3c8263
SM
22689 const struct strtab_entry *ea = (const struct strtab_entry *) a;
22690 const struct strtab_entry *eb = (const struct strtab_entry *) b;
9291a0cd
TT
22691 return !strcmp (ea->str, eb->str);
22692}
22693
22694/* Create a strtab_entry hash table. */
b89be57b 22695
9291a0cd
TT
22696static htab_t
22697create_strtab (void)
22698{
22699 return htab_create_alloc (100, hash_strtab_entry, eq_strtab_entry,
22700 xfree, xcalloc, xfree);
22701}
22702
22703/* Add a string to the constant pool. Return the string's offset in
22704 host order. */
b89be57b 22705
9291a0cd
TT
22706static offset_type
22707add_string (htab_t table, struct obstack *cpool, const char *str)
22708{
22709 void **slot;
22710 struct strtab_entry entry;
22711 struct strtab_entry *result;
22712
22713 entry.str = str;
22714 slot = htab_find_slot (table, &entry, INSERT);
22715 if (*slot)
9a3c8263 22716 result = (struct strtab_entry *) *slot;
9291a0cd
TT
22717 else
22718 {
22719 result = XNEW (struct strtab_entry);
22720 result->offset = obstack_object_size (cpool);
22721 result->str = str;
22722 obstack_grow_str0 (cpool, str);
22723 *slot = result;
22724 }
22725 return result->offset;
22726}
22727
22728/* An entry in the symbol table. */
22729struct symtab_index_entry
22730{
22731 /* The name of the symbol. */
22732 const char *name;
22733 /* The offset of the name in the constant pool. */
22734 offset_type index_offset;
22735 /* A sorted vector of the indices of all the CUs that hold an object
22736 of this name. */
22737 VEC (offset_type) *cu_indices;
22738};
22739
22740/* The symbol table. This is a power-of-2-sized hash table. */
22741struct mapped_symtab
22742{
22743 offset_type n_elements;
22744 offset_type size;
22745 struct symtab_index_entry **data;
22746};
22747
22748/* Hash function for a symtab_index_entry. */
b89be57b 22749
9291a0cd
TT
22750static hashval_t
22751hash_symtab_entry (const void *e)
22752{
9a3c8263
SM
22753 const struct symtab_index_entry *entry
22754 = (const struct symtab_index_entry *) e;
9291a0cd
TT
22755 return iterative_hash (VEC_address (offset_type, entry->cu_indices),
22756 sizeof (offset_type) * VEC_length (offset_type,
22757 entry->cu_indices),
22758 0);
22759}
22760
22761/* Equality function for a symtab_index_entry. */
b89be57b 22762
9291a0cd
TT
22763static int
22764eq_symtab_entry (const void *a, const void *b)
22765{
9a3c8263
SM
22766 const struct symtab_index_entry *ea = (const struct symtab_index_entry *) a;
22767 const struct symtab_index_entry *eb = (const struct symtab_index_entry *) b;
9291a0cd
TT
22768 int len = VEC_length (offset_type, ea->cu_indices);
22769 if (len != VEC_length (offset_type, eb->cu_indices))
22770 return 0;
22771 return !memcmp (VEC_address (offset_type, ea->cu_indices),
22772 VEC_address (offset_type, eb->cu_indices),
22773 sizeof (offset_type) * len);
22774}
22775
22776/* Destroy a symtab_index_entry. */
b89be57b 22777
9291a0cd
TT
22778static void
22779delete_symtab_entry (void *p)
22780{
9a3c8263 22781 struct symtab_index_entry *entry = (struct symtab_index_entry *) p;
9291a0cd
TT
22782 VEC_free (offset_type, entry->cu_indices);
22783 xfree (entry);
22784}
22785
22786/* Create a hash table holding symtab_index_entry objects. */
b89be57b 22787
9291a0cd 22788static htab_t
3876f04e 22789create_symbol_hash_table (void)
9291a0cd
TT
22790{
22791 return htab_create_alloc (100, hash_symtab_entry, eq_symtab_entry,
22792 delete_symtab_entry, xcalloc, xfree);
22793}
22794
22795/* Create a new mapped symtab object. */
b89be57b 22796
9291a0cd
TT
22797static struct mapped_symtab *
22798create_mapped_symtab (void)
22799{
22800 struct mapped_symtab *symtab = XNEW (struct mapped_symtab);
22801 symtab->n_elements = 0;
22802 symtab->size = 1024;
22803 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
22804 return symtab;
22805}
22806
22807/* Destroy a mapped_symtab. */
b89be57b 22808
9291a0cd
TT
22809static void
22810cleanup_mapped_symtab (void *p)
22811{
9a3c8263 22812 struct mapped_symtab *symtab = (struct mapped_symtab *) p;
9291a0cd
TT
22813 /* The contents of the array are freed when the other hash table is
22814 destroyed. */
22815 xfree (symtab->data);
22816 xfree (symtab);
22817}
22818
22819/* Find a slot in SYMTAB for the symbol NAME. Returns a pointer to
559a7a62
JK
22820 the slot.
22821
22822 Function is used only during write_hash_table so no index format backward
22823 compatibility is needed. */
b89be57b 22824
9291a0cd
TT
22825static struct symtab_index_entry **
22826find_slot (struct mapped_symtab *symtab, const char *name)
22827{
559a7a62 22828 offset_type index, step, hash = mapped_index_string_hash (INT_MAX, name);
9291a0cd
TT
22829
22830 index = hash & (symtab->size - 1);
22831 step = ((hash * 17) & (symtab->size - 1)) | 1;
22832
22833 for (;;)
22834 {
22835 if (!symtab->data[index] || !strcmp (name, symtab->data[index]->name))
22836 return &symtab->data[index];
22837 index = (index + step) & (symtab->size - 1);
22838 }
22839}
22840
22841/* Expand SYMTAB's hash table. */
b89be57b 22842
9291a0cd
TT
22843static void
22844hash_expand (struct mapped_symtab *symtab)
22845{
22846 offset_type old_size = symtab->size;
22847 offset_type i;
22848 struct symtab_index_entry **old_entries = symtab->data;
22849
22850 symtab->size *= 2;
22851 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
22852
22853 for (i = 0; i < old_size; ++i)
22854 {
22855 if (old_entries[i])
22856 {
22857 struct symtab_index_entry **slot = find_slot (symtab,
22858 old_entries[i]->name);
22859 *slot = old_entries[i];
22860 }
22861 }
22862
22863 xfree (old_entries);
22864}
22865
156942c7
DE
22866/* Add an entry to SYMTAB. NAME is the name of the symbol.
22867 CU_INDEX is the index of the CU in which the symbol appears.
22868 IS_STATIC is one if the symbol is static, otherwise zero (global). */
b89be57b 22869
9291a0cd
TT
22870static void
22871add_index_entry (struct mapped_symtab *symtab, const char *name,
156942c7 22872 int is_static, gdb_index_symbol_kind kind,
9291a0cd
TT
22873 offset_type cu_index)
22874{
22875 struct symtab_index_entry **slot;
156942c7 22876 offset_type cu_index_and_attrs;
9291a0cd
TT
22877
22878 ++symtab->n_elements;
22879 if (4 * symtab->n_elements / 3 >= symtab->size)
22880 hash_expand (symtab);
22881
22882 slot = find_slot (symtab, name);
22883 if (!*slot)
22884 {
22885 *slot = XNEW (struct symtab_index_entry);
22886 (*slot)->name = name;
156942c7 22887 /* index_offset is set later. */
9291a0cd
TT
22888 (*slot)->cu_indices = NULL;
22889 }
156942c7
DE
22890
22891 cu_index_and_attrs = 0;
22892 DW2_GDB_INDEX_CU_SET_VALUE (cu_index_and_attrs, cu_index);
22893 DW2_GDB_INDEX_SYMBOL_STATIC_SET_VALUE (cu_index_and_attrs, is_static);
22894 DW2_GDB_INDEX_SYMBOL_KIND_SET_VALUE (cu_index_and_attrs, kind);
22895
22896 /* We don't want to record an index value twice as we want to avoid the
22897 duplication.
22898 We process all global symbols and then all static symbols
22899 (which would allow us to avoid the duplication by only having to check
22900 the last entry pushed), but a symbol could have multiple kinds in one CU.
22901 To keep things simple we don't worry about the duplication here and
22902 sort and uniqufy the list after we've processed all symbols. */
22903 VEC_safe_push (offset_type, (*slot)->cu_indices, cu_index_and_attrs);
22904}
22905
22906/* qsort helper routine for uniquify_cu_indices. */
22907
22908static int
22909offset_type_compare (const void *ap, const void *bp)
22910{
22911 offset_type a = *(offset_type *) ap;
22912 offset_type b = *(offset_type *) bp;
22913
22914 return (a > b) - (b > a);
22915}
22916
22917/* Sort and remove duplicates of all symbols' cu_indices lists. */
22918
22919static void
22920uniquify_cu_indices (struct mapped_symtab *symtab)
22921{
22922 int i;
22923
22924 for (i = 0; i < symtab->size; ++i)
22925 {
22926 struct symtab_index_entry *entry = symtab->data[i];
22927
22928 if (entry
22929 && entry->cu_indices != NULL)
22930 {
22931 unsigned int next_to_insert, next_to_check;
22932 offset_type last_value;
22933
22934 qsort (VEC_address (offset_type, entry->cu_indices),
22935 VEC_length (offset_type, entry->cu_indices),
22936 sizeof (offset_type), offset_type_compare);
22937
22938 last_value = VEC_index (offset_type, entry->cu_indices, 0);
22939 next_to_insert = 1;
22940 for (next_to_check = 1;
22941 next_to_check < VEC_length (offset_type, entry->cu_indices);
22942 ++next_to_check)
22943 {
22944 if (VEC_index (offset_type, entry->cu_indices, next_to_check)
22945 != last_value)
22946 {
22947 last_value = VEC_index (offset_type, entry->cu_indices,
22948 next_to_check);
22949 VEC_replace (offset_type, entry->cu_indices, next_to_insert,
22950 last_value);
22951 ++next_to_insert;
22952 }
22953 }
22954 VEC_truncate (offset_type, entry->cu_indices, next_to_insert);
22955 }
22956 }
9291a0cd
TT
22957}
22958
22959/* Add a vector of indices to the constant pool. */
b89be57b 22960
9291a0cd 22961static offset_type
3876f04e 22962add_indices_to_cpool (htab_t symbol_hash_table, struct obstack *cpool,
9291a0cd
TT
22963 struct symtab_index_entry *entry)
22964{
22965 void **slot;
22966
3876f04e 22967 slot = htab_find_slot (symbol_hash_table, entry, INSERT);
9291a0cd
TT
22968 if (!*slot)
22969 {
22970 offset_type len = VEC_length (offset_type, entry->cu_indices);
22971 offset_type val = MAYBE_SWAP (len);
22972 offset_type iter;
22973 int i;
22974
22975 *slot = entry;
22976 entry->index_offset = obstack_object_size (cpool);
22977
22978 obstack_grow (cpool, &val, sizeof (val));
22979 for (i = 0;
22980 VEC_iterate (offset_type, entry->cu_indices, i, iter);
22981 ++i)
22982 {
22983 val = MAYBE_SWAP (iter);
22984 obstack_grow (cpool, &val, sizeof (val));
22985 }
22986 }
22987 else
22988 {
9a3c8263
SM
22989 struct symtab_index_entry *old_entry
22990 = (struct symtab_index_entry *) *slot;
9291a0cd
TT
22991 entry->index_offset = old_entry->index_offset;
22992 entry = old_entry;
22993 }
22994 return entry->index_offset;
22995}
22996
22997/* Write the mapped hash table SYMTAB to the obstack OUTPUT, with
22998 constant pool entries going into the obstack CPOOL. */
b89be57b 22999
9291a0cd
TT
23000static void
23001write_hash_table (struct mapped_symtab *symtab,
23002 struct obstack *output, struct obstack *cpool)
23003{
23004 offset_type i;
3876f04e 23005 htab_t symbol_hash_table;
9291a0cd
TT
23006 htab_t str_table;
23007
3876f04e 23008 symbol_hash_table = create_symbol_hash_table ();
9291a0cd 23009 str_table = create_strtab ();
3876f04e 23010
9291a0cd
TT
23011 /* We add all the index vectors to the constant pool first, to
23012 ensure alignment is ok. */
23013 for (i = 0; i < symtab->size; ++i)
23014 {
23015 if (symtab->data[i])
3876f04e 23016 add_indices_to_cpool (symbol_hash_table, cpool, symtab->data[i]);
9291a0cd
TT
23017 }
23018
23019 /* Now write out the hash table. */
23020 for (i = 0; i < symtab->size; ++i)
23021 {
23022 offset_type str_off, vec_off;
23023
23024 if (symtab->data[i])
23025 {
23026 str_off = add_string (str_table, cpool, symtab->data[i]->name);
23027 vec_off = symtab->data[i]->index_offset;
23028 }
23029 else
23030 {
23031 /* While 0 is a valid constant pool index, it is not valid
23032 to have 0 for both offsets. */
23033 str_off = 0;
23034 vec_off = 0;
23035 }
23036
23037 str_off = MAYBE_SWAP (str_off);
23038 vec_off = MAYBE_SWAP (vec_off);
23039
23040 obstack_grow (output, &str_off, sizeof (str_off));
23041 obstack_grow (output, &vec_off, sizeof (vec_off));
23042 }
23043
23044 htab_delete (str_table);
3876f04e 23045 htab_delete (symbol_hash_table);
9291a0cd
TT
23046}
23047
0a5429f6
DE
23048/* Struct to map psymtab to CU index in the index file. */
23049struct psymtab_cu_index_map
23050{
23051 struct partial_symtab *psymtab;
23052 unsigned int cu_index;
23053};
23054
23055static hashval_t
23056hash_psymtab_cu_index (const void *item)
23057{
9a3c8263
SM
23058 const struct psymtab_cu_index_map *map
23059 = (const struct psymtab_cu_index_map *) item;
0a5429f6
DE
23060
23061 return htab_hash_pointer (map->psymtab);
23062}
23063
23064static int
23065eq_psymtab_cu_index (const void *item_lhs, const void *item_rhs)
23066{
9a3c8263
SM
23067 const struct psymtab_cu_index_map *lhs
23068 = (const struct psymtab_cu_index_map *) item_lhs;
23069 const struct psymtab_cu_index_map *rhs
23070 = (const struct psymtab_cu_index_map *) item_rhs;
0a5429f6
DE
23071
23072 return lhs->psymtab == rhs->psymtab;
23073}
23074
23075/* Helper struct for building the address table. */
23076struct addrmap_index_data
23077{
23078 struct objfile *objfile;
23079 struct obstack *addr_obstack;
23080 htab_t cu_index_htab;
23081
23082 /* Non-zero if the previous_* fields are valid.
23083 We can't write an entry until we see the next entry (since it is only then
23084 that we know the end of the entry). */
23085 int previous_valid;
23086 /* Index of the CU in the table of all CUs in the index file. */
23087 unsigned int previous_cu_index;
0963b4bd 23088 /* Start address of the CU. */
0a5429f6
DE
23089 CORE_ADDR previous_cu_start;
23090};
23091
23092/* Write an address entry to OBSTACK. */
b89be57b 23093
9291a0cd 23094static void
0a5429f6
DE
23095add_address_entry (struct objfile *objfile, struct obstack *obstack,
23096 CORE_ADDR start, CORE_ADDR end, unsigned int cu_index)
9291a0cd 23097{
0a5429f6 23098 offset_type cu_index_to_write;
948f8e3d 23099 gdb_byte addr[8];
9291a0cd
TT
23100 CORE_ADDR baseaddr;
23101
23102 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
23103
0a5429f6
DE
23104 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, start - baseaddr);
23105 obstack_grow (obstack, addr, 8);
23106 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, end - baseaddr);
23107 obstack_grow (obstack, addr, 8);
23108 cu_index_to_write = MAYBE_SWAP (cu_index);
23109 obstack_grow (obstack, &cu_index_to_write, sizeof (offset_type));
23110}
23111
23112/* Worker function for traversing an addrmap to build the address table. */
23113
23114static int
23115add_address_entry_worker (void *datap, CORE_ADDR start_addr, void *obj)
23116{
9a3c8263
SM
23117 struct addrmap_index_data *data = (struct addrmap_index_data *) datap;
23118 struct partial_symtab *pst = (struct partial_symtab *) obj;
0a5429f6
DE
23119
23120 if (data->previous_valid)
23121 add_address_entry (data->objfile, data->addr_obstack,
23122 data->previous_cu_start, start_addr,
23123 data->previous_cu_index);
23124
23125 data->previous_cu_start = start_addr;
23126 if (pst != NULL)
23127 {
23128 struct psymtab_cu_index_map find_map, *map;
23129 find_map.psymtab = pst;
9a3c8263
SM
23130 map = ((struct psymtab_cu_index_map *)
23131 htab_find (data->cu_index_htab, &find_map));
0a5429f6
DE
23132 gdb_assert (map != NULL);
23133 data->previous_cu_index = map->cu_index;
23134 data->previous_valid = 1;
23135 }
23136 else
23137 data->previous_valid = 0;
23138
23139 return 0;
23140}
23141
23142/* Write OBJFILE's address map to OBSTACK.
23143 CU_INDEX_HTAB is used to map addrmap entries to their CU indices
23144 in the index file. */
23145
23146static void
23147write_address_map (struct objfile *objfile, struct obstack *obstack,
23148 htab_t cu_index_htab)
23149{
23150 struct addrmap_index_data addrmap_index_data;
23151
23152 /* When writing the address table, we have to cope with the fact that
23153 the addrmap iterator only provides the start of a region; we have to
23154 wait until the next invocation to get the start of the next region. */
23155
23156 addrmap_index_data.objfile = objfile;
23157 addrmap_index_data.addr_obstack = obstack;
23158 addrmap_index_data.cu_index_htab = cu_index_htab;
23159 addrmap_index_data.previous_valid = 0;
23160
23161 addrmap_foreach (objfile->psymtabs_addrmap, add_address_entry_worker,
23162 &addrmap_index_data);
23163
23164 /* It's highly unlikely the last entry (end address = 0xff...ff)
23165 is valid, but we should still handle it.
23166 The end address is recorded as the start of the next region, but that
23167 doesn't work here. To cope we pass 0xff...ff, this is a rare situation
23168 anyway. */
23169 if (addrmap_index_data.previous_valid)
23170 add_address_entry (objfile, obstack,
23171 addrmap_index_data.previous_cu_start, (CORE_ADDR) -1,
23172 addrmap_index_data.previous_cu_index);
9291a0cd
TT
23173}
23174
156942c7
DE
23175/* Return the symbol kind of PSYM. */
23176
23177static gdb_index_symbol_kind
23178symbol_kind (struct partial_symbol *psym)
23179{
23180 domain_enum domain = PSYMBOL_DOMAIN (psym);
23181 enum address_class aclass = PSYMBOL_CLASS (psym);
23182
23183 switch (domain)
23184 {
23185 case VAR_DOMAIN:
23186 switch (aclass)
23187 {
23188 case LOC_BLOCK:
23189 return GDB_INDEX_SYMBOL_KIND_FUNCTION;
23190 case LOC_TYPEDEF:
23191 return GDB_INDEX_SYMBOL_KIND_TYPE;
23192 case LOC_COMPUTED:
23193 case LOC_CONST_BYTES:
23194 case LOC_OPTIMIZED_OUT:
23195 case LOC_STATIC:
23196 return GDB_INDEX_SYMBOL_KIND_VARIABLE;
23197 case LOC_CONST:
23198 /* Note: It's currently impossible to recognize psyms as enum values
23199 short of reading the type info. For now punt. */
23200 return GDB_INDEX_SYMBOL_KIND_VARIABLE;
23201 default:
23202 /* There are other LOC_FOO values that one might want to classify
23203 as variables, but dwarf2read.c doesn't currently use them. */
23204 return GDB_INDEX_SYMBOL_KIND_OTHER;
23205 }
23206 case STRUCT_DOMAIN:
23207 return GDB_INDEX_SYMBOL_KIND_TYPE;
23208 default:
23209 return GDB_INDEX_SYMBOL_KIND_OTHER;
23210 }
23211}
23212
9291a0cd 23213/* Add a list of partial symbols to SYMTAB. */
b89be57b 23214
9291a0cd
TT
23215static void
23216write_psymbols (struct mapped_symtab *symtab,
987d643c 23217 htab_t psyms_seen,
9291a0cd
TT
23218 struct partial_symbol **psymp,
23219 int count,
987d643c
TT
23220 offset_type cu_index,
23221 int is_static)
9291a0cd
TT
23222{
23223 for (; count-- > 0; ++psymp)
23224 {
156942c7
DE
23225 struct partial_symbol *psym = *psymp;
23226 void **slot;
987d643c 23227
156942c7 23228 if (SYMBOL_LANGUAGE (psym) == language_ada)
9291a0cd 23229 error (_("Ada is not currently supported by the index"));
987d643c 23230
987d643c 23231 /* Only add a given psymbol once. */
156942c7 23232 slot = htab_find_slot (psyms_seen, psym, INSERT);
987d643c
TT
23233 if (!*slot)
23234 {
156942c7
DE
23235 gdb_index_symbol_kind kind = symbol_kind (psym);
23236
23237 *slot = psym;
23238 add_index_entry (symtab, SYMBOL_SEARCH_NAME (psym),
23239 is_static, kind, cu_index);
987d643c 23240 }
9291a0cd
TT
23241 }
23242}
23243
23244/* Write the contents of an ("unfinished") obstack to FILE. Throw an
23245 exception if there is an error. */
b89be57b 23246
9291a0cd
TT
23247static void
23248write_obstack (FILE *file, struct obstack *obstack)
23249{
23250 if (fwrite (obstack_base (obstack), 1, obstack_object_size (obstack),
23251 file)
23252 != obstack_object_size (obstack))
23253 error (_("couldn't data write to file"));
23254}
23255
23256/* Unlink a file if the argument is not NULL. */
b89be57b 23257
9291a0cd
TT
23258static void
23259unlink_if_set (void *p)
23260{
9a3c8263 23261 char **filename = (char **) p;
9291a0cd
TT
23262 if (*filename)
23263 unlink (*filename);
23264}
23265
1fd400ff
TT
23266/* A helper struct used when iterating over debug_types. */
23267struct signatured_type_index_data
23268{
23269 struct objfile *objfile;
23270 struct mapped_symtab *symtab;
23271 struct obstack *types_list;
987d643c 23272 htab_t psyms_seen;
1fd400ff
TT
23273 int cu_index;
23274};
23275
23276/* A helper function that writes a single signatured_type to an
23277 obstack. */
b89be57b 23278
1fd400ff
TT
23279static int
23280write_one_signatured_type (void **slot, void *d)
23281{
9a3c8263
SM
23282 struct signatured_type_index_data *info
23283 = (struct signatured_type_index_data *) d;
1fd400ff 23284 struct signatured_type *entry = (struct signatured_type *) *slot;
0186c6a7 23285 struct partial_symtab *psymtab = entry->per_cu.v.psymtab;
1fd400ff
TT
23286 gdb_byte val[8];
23287
23288 write_psymbols (info->symtab,
987d643c 23289 info->psyms_seen,
3e43a32a
MS
23290 info->objfile->global_psymbols.list
23291 + psymtab->globals_offset,
987d643c
TT
23292 psymtab->n_global_syms, info->cu_index,
23293 0);
1fd400ff 23294 write_psymbols (info->symtab,
987d643c 23295 info->psyms_seen,
3e43a32a
MS
23296 info->objfile->static_psymbols.list
23297 + psymtab->statics_offset,
987d643c
TT
23298 psymtab->n_static_syms, info->cu_index,
23299 1);
1fd400ff 23300
b64f50a1
JK
23301 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
23302 entry->per_cu.offset.sect_off);
1fd400ff 23303 obstack_grow (info->types_list, val, 8);
3019eac3
DE
23304 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
23305 entry->type_offset_in_tu.cu_off);
1fd400ff
TT
23306 obstack_grow (info->types_list, val, 8);
23307 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->signature);
23308 obstack_grow (info->types_list, val, 8);
23309
23310 ++info->cu_index;
23311
23312 return 1;
23313}
23314
95554aad
TT
23315/* Recurse into all "included" dependencies and write their symbols as
23316 if they appeared in this psymtab. */
23317
23318static void
23319recursively_write_psymbols (struct objfile *objfile,
23320 struct partial_symtab *psymtab,
23321 struct mapped_symtab *symtab,
23322 htab_t psyms_seen,
23323 offset_type cu_index)
23324{
23325 int i;
23326
23327 for (i = 0; i < psymtab->number_of_dependencies; ++i)
23328 if (psymtab->dependencies[i]->user != NULL)
23329 recursively_write_psymbols (objfile, psymtab->dependencies[i],
23330 symtab, psyms_seen, cu_index);
23331
23332 write_psymbols (symtab,
23333 psyms_seen,
23334 objfile->global_psymbols.list + psymtab->globals_offset,
23335 psymtab->n_global_syms, cu_index,
23336 0);
23337 write_psymbols (symtab,
23338 psyms_seen,
23339 objfile->static_psymbols.list + psymtab->statics_offset,
23340 psymtab->n_static_syms, cu_index,
23341 1);
23342}
23343
9291a0cd 23344/* Create an index file for OBJFILE in the directory DIR. */
b89be57b 23345
9291a0cd
TT
23346static void
23347write_psymtabs_to_index (struct objfile *objfile, const char *dir)
23348{
23349 struct cleanup *cleanup;
23350 char *filename, *cleanup_filename;
1fd400ff
TT
23351 struct obstack contents, addr_obstack, constant_pool, symtab_obstack;
23352 struct obstack cu_list, types_cu_list;
9291a0cd
TT
23353 int i;
23354 FILE *out_file;
23355 struct mapped_symtab *symtab;
23356 offset_type val, size_of_contents, total_len;
23357 struct stat st;
987d643c 23358 htab_t psyms_seen;
0a5429f6
DE
23359 htab_t cu_index_htab;
23360 struct psymtab_cu_index_map *psymtab_cu_index_map;
9291a0cd 23361
9291a0cd
TT
23362 if (dwarf2_per_objfile->using_index)
23363 error (_("Cannot use an index to create the index"));
23364
8b70b953
TT
23365 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) > 1)
23366 error (_("Cannot make an index when the file has multiple .debug_types sections"));
23367
260b681b
DE
23368 if (!objfile->psymtabs || !objfile->psymtabs_addrmap)
23369 return;
23370
4262abfb
JK
23371 if (stat (objfile_name (objfile), &st) < 0)
23372 perror_with_name (objfile_name (objfile));
9291a0cd 23373
4262abfb 23374 filename = concat (dir, SLASH_STRING, lbasename (objfile_name (objfile)),
9291a0cd
TT
23375 INDEX_SUFFIX, (char *) NULL);
23376 cleanup = make_cleanup (xfree, filename);
23377
614c279d 23378 out_file = gdb_fopen_cloexec (filename, "wb");
9291a0cd
TT
23379 if (!out_file)
23380 error (_("Can't open `%s' for writing"), filename);
23381
23382 cleanup_filename = filename;
23383 make_cleanup (unlink_if_set, &cleanup_filename);
23384
23385 symtab = create_mapped_symtab ();
23386 make_cleanup (cleanup_mapped_symtab, symtab);
23387
23388 obstack_init (&addr_obstack);
23389 make_cleanup_obstack_free (&addr_obstack);
23390
23391 obstack_init (&cu_list);
23392 make_cleanup_obstack_free (&cu_list);
23393
1fd400ff
TT
23394 obstack_init (&types_cu_list);
23395 make_cleanup_obstack_free (&types_cu_list);
23396
987d643c
TT
23397 psyms_seen = htab_create_alloc (100, htab_hash_pointer, htab_eq_pointer,
23398 NULL, xcalloc, xfree);
96408a79 23399 make_cleanup_htab_delete (psyms_seen);
987d643c 23400
0a5429f6
DE
23401 /* While we're scanning CU's create a table that maps a psymtab pointer
23402 (which is what addrmap records) to its index (which is what is recorded
23403 in the index file). This will later be needed to write the address
23404 table. */
23405 cu_index_htab = htab_create_alloc (100,
23406 hash_psymtab_cu_index,
23407 eq_psymtab_cu_index,
23408 NULL, xcalloc, xfree);
96408a79 23409 make_cleanup_htab_delete (cu_index_htab);
8d749320
SM
23410 psymtab_cu_index_map = XNEWVEC (struct psymtab_cu_index_map,
23411 dwarf2_per_objfile->n_comp_units);
0a5429f6
DE
23412 make_cleanup (xfree, psymtab_cu_index_map);
23413
23414 /* The CU list is already sorted, so we don't need to do additional
1fd400ff
TT
23415 work here. Also, the debug_types entries do not appear in
23416 all_comp_units, but only in their own hash table. */
9291a0cd
TT
23417 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
23418 {
3e43a32a
MS
23419 struct dwarf2_per_cu_data *per_cu
23420 = dwarf2_per_objfile->all_comp_units[i];
e254ef6a 23421 struct partial_symtab *psymtab = per_cu->v.psymtab;
9291a0cd 23422 gdb_byte val[8];
0a5429f6
DE
23423 struct psymtab_cu_index_map *map;
23424 void **slot;
9291a0cd 23425
92fac807
JK
23426 /* CU of a shared file from 'dwz -m' may be unused by this main file.
23427 It may be referenced from a local scope but in such case it does not
23428 need to be present in .gdb_index. */
23429 if (psymtab == NULL)
23430 continue;
23431
95554aad
TT
23432 if (psymtab->user == NULL)
23433 recursively_write_psymbols (objfile, psymtab, symtab, psyms_seen, i);
9291a0cd 23434
0a5429f6
DE
23435 map = &psymtab_cu_index_map[i];
23436 map->psymtab = psymtab;
23437 map->cu_index = i;
23438 slot = htab_find_slot (cu_index_htab, map, INSERT);
23439 gdb_assert (slot != NULL);
23440 gdb_assert (*slot == NULL);
23441 *slot = map;
9291a0cd 23442
b64f50a1
JK
23443 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
23444 per_cu->offset.sect_off);
9291a0cd 23445 obstack_grow (&cu_list, val, 8);
e254ef6a 23446 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, per_cu->length);
9291a0cd
TT
23447 obstack_grow (&cu_list, val, 8);
23448 }
23449
0a5429f6
DE
23450 /* Dump the address map. */
23451 write_address_map (objfile, &addr_obstack, cu_index_htab);
23452
1fd400ff
TT
23453 /* Write out the .debug_type entries, if any. */
23454 if (dwarf2_per_objfile->signatured_types)
23455 {
23456 struct signatured_type_index_data sig_data;
23457
23458 sig_data.objfile = objfile;
23459 sig_data.symtab = symtab;
23460 sig_data.types_list = &types_cu_list;
987d643c 23461 sig_data.psyms_seen = psyms_seen;
1fd400ff
TT
23462 sig_data.cu_index = dwarf2_per_objfile->n_comp_units;
23463 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
23464 write_one_signatured_type, &sig_data);
23465 }
23466
156942c7
DE
23467 /* Now that we've processed all symbols we can shrink their cu_indices
23468 lists. */
23469 uniquify_cu_indices (symtab);
23470
9291a0cd
TT
23471 obstack_init (&constant_pool);
23472 make_cleanup_obstack_free (&constant_pool);
23473 obstack_init (&symtab_obstack);
23474 make_cleanup_obstack_free (&symtab_obstack);
23475 write_hash_table (symtab, &symtab_obstack, &constant_pool);
23476
23477 obstack_init (&contents);
23478 make_cleanup_obstack_free (&contents);
1fd400ff 23479 size_of_contents = 6 * sizeof (offset_type);
9291a0cd
TT
23480 total_len = size_of_contents;
23481
23482 /* The version number. */
796a7ff8 23483 val = MAYBE_SWAP (8);
9291a0cd
TT
23484 obstack_grow (&contents, &val, sizeof (val));
23485
23486 /* The offset of the CU list from the start of the file. */
23487 val = MAYBE_SWAP (total_len);
23488 obstack_grow (&contents, &val, sizeof (val));
23489 total_len += obstack_object_size (&cu_list);
23490
1fd400ff
TT
23491 /* The offset of the types CU list from the start of the file. */
23492 val = MAYBE_SWAP (total_len);
23493 obstack_grow (&contents, &val, sizeof (val));
23494 total_len += obstack_object_size (&types_cu_list);
23495
9291a0cd
TT
23496 /* The offset of the address table from the start of the file. */
23497 val = MAYBE_SWAP (total_len);
23498 obstack_grow (&contents, &val, sizeof (val));
23499 total_len += obstack_object_size (&addr_obstack);
23500
23501 /* The offset of the symbol table from the start of the file. */
23502 val = MAYBE_SWAP (total_len);
23503 obstack_grow (&contents, &val, sizeof (val));
23504 total_len += obstack_object_size (&symtab_obstack);
23505
23506 /* The offset of the constant pool from the start of the file. */
23507 val = MAYBE_SWAP (total_len);
23508 obstack_grow (&contents, &val, sizeof (val));
23509 total_len += obstack_object_size (&constant_pool);
23510
23511 gdb_assert (obstack_object_size (&contents) == size_of_contents);
23512
23513 write_obstack (out_file, &contents);
23514 write_obstack (out_file, &cu_list);
1fd400ff 23515 write_obstack (out_file, &types_cu_list);
9291a0cd
TT
23516 write_obstack (out_file, &addr_obstack);
23517 write_obstack (out_file, &symtab_obstack);
23518 write_obstack (out_file, &constant_pool);
23519
23520 fclose (out_file);
23521
23522 /* We want to keep the file, so we set cleanup_filename to NULL
23523 here. See unlink_if_set. */
23524 cleanup_filename = NULL;
23525
23526 do_cleanups (cleanup);
23527}
23528
90476074
TT
23529/* Implementation of the `save gdb-index' command.
23530
23531 Note that the file format used by this command is documented in the
23532 GDB manual. Any changes here must be documented there. */
11570e71 23533
9291a0cd
TT
23534static void
23535save_gdb_index_command (char *arg, int from_tty)
23536{
23537 struct objfile *objfile;
23538
23539 if (!arg || !*arg)
96d19272 23540 error (_("usage: save gdb-index DIRECTORY"));
9291a0cd
TT
23541
23542 ALL_OBJFILES (objfile)
23543 {
23544 struct stat st;
23545
23546 /* If the objfile does not correspond to an actual file, skip it. */
4262abfb 23547 if (stat (objfile_name (objfile), &st) < 0)
9291a0cd
TT
23548 continue;
23549
9a3c8263
SM
23550 dwarf2_per_objfile
23551 = (struct dwarf2_per_objfile *) objfile_data (objfile,
23552 dwarf2_objfile_data_key);
9291a0cd
TT
23553 if (dwarf2_per_objfile)
23554 {
9291a0cd 23555
492d29ea 23556 TRY
9291a0cd
TT
23557 {
23558 write_psymtabs_to_index (objfile, arg);
23559 }
492d29ea
PA
23560 CATCH (except, RETURN_MASK_ERROR)
23561 {
23562 exception_fprintf (gdb_stderr, except,
23563 _("Error while writing index for `%s': "),
23564 objfile_name (objfile));
23565 }
23566 END_CATCH
9291a0cd
TT
23567 }
23568 }
dce234bc
PP
23569}
23570
9291a0cd
TT
23571\f
23572
b4f54984 23573int dwarf_always_disassemble;
9eae7c52
TT
23574
23575static void
b4f54984
DE
23576show_dwarf_always_disassemble (struct ui_file *file, int from_tty,
23577 struct cmd_list_element *c, const char *value)
9eae7c52 23578{
3e43a32a
MS
23579 fprintf_filtered (file,
23580 _("Whether to always disassemble "
23581 "DWARF expressions is %s.\n"),
9eae7c52
TT
23582 value);
23583}
23584
900e11f9
JK
23585static void
23586show_check_physname (struct ui_file *file, int from_tty,
23587 struct cmd_list_element *c, const char *value)
23588{
23589 fprintf_filtered (file,
23590 _("Whether to check \"physname\" is %s.\n"),
23591 value);
23592}
23593
6502dd73
DJ
23594void _initialize_dwarf2_read (void);
23595
23596void
23597_initialize_dwarf2_read (void)
23598{
96d19272
JK
23599 struct cmd_list_element *c;
23600
dce234bc 23601 dwarf2_objfile_data_key
c1bd65d0 23602 = register_objfile_data_with_cleanup (NULL, dwarf2_per_objfile_free);
ae038cb0 23603
b4f54984
DE
23604 add_prefix_cmd ("dwarf", class_maintenance, set_dwarf_cmd, _("\
23605Set DWARF specific variables.\n\
23606Configure DWARF variables such as the cache size"),
23607 &set_dwarf_cmdlist, "maintenance set dwarf ",
ae038cb0
DJ
23608 0/*allow-unknown*/, &maintenance_set_cmdlist);
23609
b4f54984
DE
23610 add_prefix_cmd ("dwarf", class_maintenance, show_dwarf_cmd, _("\
23611Show DWARF specific variables\n\
23612Show DWARF variables such as the cache size"),
23613 &show_dwarf_cmdlist, "maintenance show dwarf ",
ae038cb0
DJ
23614 0/*allow-unknown*/, &maintenance_show_cmdlist);
23615
23616 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
b4f54984
DE
23617 &dwarf_max_cache_age, _("\
23618Set the upper bound on the age of cached DWARF compilation units."), _("\
23619Show the upper bound on the age of cached DWARF compilation units."), _("\
7915a72c
AC
23620A higher limit means that cached compilation units will be stored\n\
23621in memory longer, and more total memory will be used. Zero disables\n\
23622caching, which can slow down startup."),
2c5b56ce 23623 NULL,
b4f54984
DE
23624 show_dwarf_max_cache_age,
23625 &set_dwarf_cmdlist,
23626 &show_dwarf_cmdlist);
d97bc12b 23627
9eae7c52 23628 add_setshow_boolean_cmd ("always-disassemble", class_obscure,
b4f54984 23629 &dwarf_always_disassemble, _("\
9eae7c52
TT
23630Set whether `info address' always disassembles DWARF expressions."), _("\
23631Show whether `info address' always disassembles DWARF expressions."), _("\
23632When enabled, DWARF expressions are always printed in an assembly-like\n\
23633syntax. When disabled, expressions will be printed in a more\n\
23634conversational style, when possible."),
23635 NULL,
b4f54984
DE
23636 show_dwarf_always_disassemble,
23637 &set_dwarf_cmdlist,
23638 &show_dwarf_cmdlist);
23639
23640 add_setshow_zuinteger_cmd ("dwarf-read", no_class, &dwarf_read_debug, _("\
23641Set debugging of the DWARF reader."), _("\
23642Show debugging of the DWARF reader."), _("\
23643When enabled (non-zero), debugging messages are printed during DWARF\n\
73be47f5
DE
23644reading and symtab expansion. A value of 1 (one) provides basic\n\
23645information. A value greater than 1 provides more verbose information."),
45cfd468
DE
23646 NULL,
23647 NULL,
23648 &setdebuglist, &showdebuglist);
23649
b4f54984
DE
23650 add_setshow_zuinteger_cmd ("dwarf-die", no_class, &dwarf_die_debug, _("\
23651Set debugging of the DWARF DIE reader."), _("\
23652Show debugging of the DWARF DIE reader."), _("\
d97bc12b
DE
23653When enabled (non-zero), DIEs are dumped after they are read in.\n\
23654The value is the maximum depth to print."),
ccce17b0
YQ
23655 NULL,
23656 NULL,
23657 &setdebuglist, &showdebuglist);
9291a0cd 23658
27e0867f
DE
23659 add_setshow_zuinteger_cmd ("dwarf-line", no_class, &dwarf_line_debug, _("\
23660Set debugging of the dwarf line reader."), _("\
23661Show debugging of the dwarf line reader."), _("\
23662When enabled (non-zero), line number entries are dumped as they are read in.\n\
23663A value of 1 (one) provides basic information.\n\
23664A value greater than 1 provides more verbose information."),
23665 NULL,
23666 NULL,
23667 &setdebuglist, &showdebuglist);
23668
900e11f9
JK
23669 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
23670Set cross-checking of \"physname\" code against demangler."), _("\
23671Show cross-checking of \"physname\" code against demangler."), _("\
23672When enabled, GDB's internal \"physname\" code is checked against\n\
23673the demangler."),
23674 NULL, show_check_physname,
23675 &setdebuglist, &showdebuglist);
23676
e615022a
DE
23677 add_setshow_boolean_cmd ("use-deprecated-index-sections",
23678 no_class, &use_deprecated_index_sections, _("\
23679Set whether to use deprecated gdb_index sections."), _("\
23680Show whether to use deprecated gdb_index sections."), _("\
23681When enabled, deprecated .gdb_index sections are used anyway.\n\
23682Normally they are ignored either because of a missing feature or\n\
23683performance issue.\n\
23684Warning: This option must be enabled before gdb reads the file."),
23685 NULL,
23686 NULL,
23687 &setlist, &showlist);
23688
96d19272 23689 c = add_cmd ("gdb-index", class_files, save_gdb_index_command,
11570e71 23690 _("\
fc1a9d6e 23691Save a gdb-index file.\n\
11570e71 23692Usage: save gdb-index DIRECTORY"),
96d19272
JK
23693 &save_cmdlist);
23694 set_cmd_completer (c, filename_completer);
f1e6e072
TT
23695
23696 dwarf2_locexpr_index = register_symbol_computed_impl (LOC_COMPUTED,
23697 &dwarf2_locexpr_funcs);
23698 dwarf2_loclist_index = register_symbol_computed_impl (LOC_COMPUTED,
23699 &dwarf2_loclist_funcs);
23700
23701 dwarf2_locexpr_block_index = register_symbol_block_impl (LOC_BLOCK,
23702 &dwarf2_block_frame_base_locexpr_funcs);
23703 dwarf2_loclist_block_index = register_symbol_block_impl (LOC_BLOCK,
23704 &dwarf2_block_frame_base_loclist_funcs);
6502dd73 23705}
This page took 3.670495 seconds and 4 git commands to generate.