dwarf2read.c:mapped_index, use gdb::array_view, simplify symbol table
[deliverable/binutils-gdb.git] / gdb / dwarf2read.c
CommitLineData
c906108c 1/* DWARF 2 debugging format support for GDB.
917c78fc 2
61baf725 3 Copyright (C) 1994-2017 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 55#include "typeprint.h"
ccefe4c4 56#include "psympriv.h"
53ce3c39 57#include <sys/stat.h>
96d19272 58#include "completer.h"
34eaf542 59#include "vec.h"
98bfdba5 60#include "c-lang.h"
a766d390 61#include "go-lang.h"
98bfdba5 62#include "valprint.h"
3019eac3 63#include "gdbcore.h" /* for gnutarget */
156942c7 64#include "gdb/gdb-index.h"
60d5a603 65#include <ctype.h>
cbb099e8 66#include "gdb_bfd.h"
4357ac6c 67#include "f-lang.h"
05cba821 68#include "source.h"
614c279d 69#include "filestuff.h"
dc294be5 70#include "build-id.h"
22cee43f 71#include "namespace.h"
bef155c3 72#include "common/gdb_unlinker.h"
14bc53a8 73#include "common/function-view.h"
ecfb656c
PA
74#include "common/gdb_optional.h"
75#include "common/underlying.h"
d5722aa2 76#include "common/byte-vector.h"
927aa2e7 77#include "common/hash_enum.h"
bbf2f4df 78#include "filename-seen-cache.h"
b32b108a 79#include "producer.h"
c906108c 80#include <fcntl.h>
c906108c 81#include <sys/types.h>
325fac50 82#include <algorithm>
bc8f2430
JK
83#include <unordered_set>
84#include <unordered_map>
c62446b1 85#include "selftest.h"
437afbb8
JK
86#include <cmath>
87#include <set>
88#include <forward_list>
89
90typedef struct symbol *symbolp;
91DEF_VEC_P (symbolp);
d8151005 92
73be47f5
DE
93/* When == 1, print basic high level tracing messages.
94 When > 1, be more verbose.
b4f54984
DE
95 This is in contrast to the low level DIE reading of dwarf_die_debug. */
96static unsigned int dwarf_read_debug = 0;
45cfd468 97
d97bc12b 98/* When non-zero, dump DIEs after they are read in. */
b4f54984 99static unsigned int dwarf_die_debug = 0;
d97bc12b 100
27e0867f
DE
101/* When non-zero, dump line number entries as they are read in. */
102static unsigned int dwarf_line_debug = 0;
103
900e11f9
JK
104/* When non-zero, cross-check physname against demangler. */
105static int check_physname = 0;
106
481860b3 107/* When non-zero, do not reject deprecated .gdb_index sections. */
e615022a 108static int use_deprecated_index_sections = 0;
481860b3 109
6502dd73
DJ
110static const struct objfile_data *dwarf2_objfile_data_key;
111
f1e6e072
TT
112/* The "aclass" indices for various kinds of computed DWARF symbols. */
113
114static int dwarf2_locexpr_index;
115static int dwarf2_loclist_index;
116static int dwarf2_locexpr_block_index;
117static int dwarf2_loclist_block_index;
118
73869dc2
DE
119/* A descriptor for dwarf sections.
120
121 S.ASECTION, SIZE are typically initialized when the objfile is first
122 scanned. BUFFER, READIN are filled in later when the section is read.
123 If the section contained compressed data then SIZE is updated to record
124 the uncompressed size of the section.
125
126 DWP file format V2 introduces a wrinkle that is easiest to handle by
127 creating the concept of virtual sections contained within a real section.
128 In DWP V2 the sections of the input DWO files are concatenated together
129 into one section, but section offsets are kept relative to the original
130 input section.
131 If this is a virtual dwp-v2 section, S.CONTAINING_SECTION is a backlink to
132 the real section this "virtual" section is contained in, and BUFFER,SIZE
133 describe the virtual section. */
134
dce234bc
PP
135struct dwarf2_section_info
136{
73869dc2
DE
137 union
138 {
e5aa3347 139 /* If this is a real section, the bfd section. */
049412e3 140 asection *section;
73869dc2 141 /* If this is a virtual section, pointer to the containing ("real")
e5aa3347 142 section. */
73869dc2
DE
143 struct dwarf2_section_info *containing_section;
144 } s;
19ac8c2e 145 /* Pointer to section data, only valid if readin. */
d521ce57 146 const gdb_byte *buffer;
73869dc2 147 /* The size of the section, real or virtual. */
dce234bc 148 bfd_size_type size;
73869dc2
DE
149 /* If this is a virtual section, the offset in the real section.
150 Only valid if is_virtual. */
151 bfd_size_type virtual_offset;
be391dca 152 /* True if we have tried to read this section. */
73869dc2
DE
153 char readin;
154 /* True if this is a virtual section, False otherwise.
049412e3 155 This specifies which of s.section and s.containing_section to use. */
73869dc2 156 char is_virtual;
dce234bc
PP
157};
158
8b70b953
TT
159typedef struct dwarf2_section_info dwarf2_section_info_def;
160DEF_VEC_O (dwarf2_section_info_def);
161
9291a0cd
TT
162/* All offsets in the index are of this type. It must be
163 architecture-independent. */
164typedef uint32_t offset_type;
165
166DEF_VEC_I (offset_type);
167
156942c7
DE
168/* Ensure only legit values are used. */
169#define DW2_GDB_INDEX_SYMBOL_STATIC_SET_VALUE(cu_index, value) \
170 do { \
171 gdb_assert ((unsigned int) (value) <= 1); \
172 GDB_INDEX_SYMBOL_STATIC_SET_VALUE((cu_index), (value)); \
173 } while (0)
174
175/* Ensure only legit values are used. */
176#define DW2_GDB_INDEX_SYMBOL_KIND_SET_VALUE(cu_index, value) \
177 do { \
178 gdb_assert ((value) >= GDB_INDEX_SYMBOL_KIND_TYPE \
179 && (value) <= GDB_INDEX_SYMBOL_KIND_OTHER); \
180 GDB_INDEX_SYMBOL_KIND_SET_VALUE((cu_index), (value)); \
181 } while (0)
182
183/* Ensure we don't use more than the alloted nuber of bits for the CU. */
184#define DW2_GDB_INDEX_CU_SET_VALUE(cu_index, value) \
185 do { \
186 gdb_assert (((value) & ~GDB_INDEX_CU_MASK) == 0); \
187 GDB_INDEX_CU_SET_VALUE((cu_index), (value)); \
188 } while (0)
189
3f563c84
PA
190#if WORDS_BIGENDIAN
191
192/* Convert VALUE between big- and little-endian. */
193
194static offset_type
195byte_swap (offset_type value)
196{
197 offset_type result;
198
199 result = (value & 0xff) << 24;
200 result |= (value & 0xff00) << 8;
201 result |= (value & 0xff0000) >> 8;
202 result |= (value & 0xff000000) >> 24;
203 return result;
204}
205
206#define MAYBE_SWAP(V) byte_swap (V)
207
208#else
209#define MAYBE_SWAP(V) static_cast<offset_type> (V)
210#endif /* WORDS_BIGENDIAN */
211
212/* An index into a (C++) symbol name component in a symbol name as
213 recorded in the mapped_index's symbol table. For each C++ symbol
214 in the symbol table, we record one entry for the start of each
215 component in the symbol in a table of name components, and then
216 sort the table, in order to be able to binary search symbol names,
217 ignoring leading namespaces, both completion and regular look up.
218 For example, for symbol "A::B::C", we'll have an entry that points
219 to "A::B::C", another that points to "B::C", and another for "C".
220 Note that function symbols in GDB index have no parameter
221 information, just the function/method names. You can convert a
222 name_component to a "const char *" using the
223 'mapped_index::symbol_name_at(offset_type)' method. */
224
225struct name_component
226{
227 /* Offset in the symbol name where the component starts. Stored as
228 a (32-bit) offset instead of a pointer to save memory and improve
229 locality on 64-bit architectures. */
230 offset_type name_offset;
231
232 /* The symbol's index in the symbol and constant pool tables of a
233 mapped_index. */
234 offset_type idx;
235};
236
9291a0cd
TT
237/* A description of the mapped index. The file format is described in
238 a comment by the code that writes the index. */
239struct mapped_index
240{
f00a2de2
PA
241 /* A slot/bucket in the symbol table hash. */
242 struct symbol_table_slot
243 {
244 const offset_type name;
245 const offset_type vec;
246 };
247
559a7a62
JK
248 /* Index data format version. */
249 int version;
250
9291a0cd
TT
251 /* The total length of the buffer. */
252 off_t total_size;
b11b1f88 253
f00a2de2
PA
254 /* The address table data. */
255 gdb::array_view<const gdb_byte> address_table;
b11b1f88 256
3876f04e 257 /* The symbol table, implemented as a hash table. */
f00a2de2 258 gdb::array_view<symbol_table_slot> symbol_table;
b11b1f88 259
9291a0cd
TT
260 /* A pointer to the constant pool. */
261 const char *constant_pool;
3f563c84
PA
262
263 /* The name_component table (a sorted vector). See name_component's
264 description above. */
265 std::vector<name_component> name_components;
266
5c58de74
PA
267 /* How NAME_COMPONENTS is sorted. */
268 enum case_sensitivity name_components_casing;
269
3f563c84
PA
270 /* Convenience method to get at the name of the symbol at IDX in the
271 symbol table. */
272 const char *symbol_name_at (offset_type idx) const
f00a2de2 273 { return this->constant_pool + MAYBE_SWAP (this->symbol_table[idx].name); }
5c58de74
PA
274
275 /* Build the symbol name component sorted vector, if we haven't
276 yet. */
277 void build_name_components ();
278
279 /* Returns the lower (inclusive) and upper (exclusive) bounds of the
280 possible matches for LN_NO_PARAMS in the name component
281 vector. */
282 std::pair<std::vector<name_component>::const_iterator,
283 std::vector<name_component>::const_iterator>
284 find_name_components_bounds (const lookup_name_info &ln_no_params) const;
9291a0cd
TT
285};
286
927aa2e7
JK
287/* A description of the mapped .debug_names.
288 Uninitialized map has CU_COUNT 0. */
289struct mapped_debug_names
290{
291 bfd_endian dwarf5_byte_order;
292 bool dwarf5_is_dwarf64;
293 bool augmentation_is_gdb;
294 uint8_t offset_size;
295 uint32_t cu_count = 0;
296 uint32_t tu_count, bucket_count, name_count;
297 const gdb_byte *cu_table_reordered, *tu_table_reordered;
298 const uint32_t *bucket_table_reordered, *hash_table_reordered;
299 const gdb_byte *name_table_string_offs_reordered;
300 const gdb_byte *name_table_entry_offs_reordered;
301 const gdb_byte *entry_pool;
302
303 struct index_val
304 {
305 ULONGEST dwarf_tag;
306 struct attr
307 {
308 /* Attribute name DW_IDX_*. */
309 ULONGEST dw_idx;
310
311 /* Attribute form DW_FORM_*. */
312 ULONGEST form;
313
314 /* Value if FORM is DW_FORM_implicit_const. */
315 LONGEST implicit_const;
316 };
317 std::vector<attr> attr_vec;
318 };
319
320 std::unordered_map<ULONGEST, index_val> abbrev_map;
321
322 const char *namei_to_name (uint32_t namei) const;
323};
324
95554aad
TT
325typedef struct dwarf2_per_cu_data *dwarf2_per_cu_ptr;
326DEF_VEC_P (dwarf2_per_cu_ptr);
327
52059ffd
TT
328struct tu_stats
329{
330 int nr_uniq_abbrev_tables;
331 int nr_symtabs;
332 int nr_symtab_sharers;
333 int nr_stmt_less_type_units;
334 int nr_all_type_units_reallocs;
335};
336
9cdd5dbd
DE
337/* Collection of data recorded per objfile.
338 This hangs off of dwarf2_objfile_data_key. */
339
6502dd73
DJ
340struct dwarf2_per_objfile
341{
330cdd98
PA
342 /* Construct a dwarf2_per_objfile for OBJFILE. NAMES points to the
343 dwarf2 section names, or is NULL if the standard ELF names are
344 used. */
345 dwarf2_per_objfile (struct objfile *objfile,
346 const dwarf2_debug_sections *names);
ae038cb0 347
330cdd98
PA
348 ~dwarf2_per_objfile ();
349
d6541620 350 DISABLE_COPY_AND_ASSIGN (dwarf2_per_objfile);
330cdd98
PA
351
352 /* Free all cached compilation units. */
353 void free_cached_comp_units ();
354private:
355 /* This function is mapped across the sections and remembers the
356 offset and size of each of the debugging sections we are
357 interested in. */
358 void locate_sections (bfd *abfd, asection *sectp,
359 const dwarf2_debug_sections &names);
360
361public:
362 dwarf2_section_info info {};
363 dwarf2_section_info abbrev {};
364 dwarf2_section_info line {};
365 dwarf2_section_info loc {};
366 dwarf2_section_info loclists {};
367 dwarf2_section_info macinfo {};
368 dwarf2_section_info macro {};
369 dwarf2_section_info str {};
370 dwarf2_section_info line_str {};
371 dwarf2_section_info ranges {};
372 dwarf2_section_info rnglists {};
373 dwarf2_section_info addr {};
374 dwarf2_section_info frame {};
375 dwarf2_section_info eh_frame {};
376 dwarf2_section_info gdb_index {};
927aa2e7
JK
377 dwarf2_section_info debug_names {};
378 dwarf2_section_info debug_aranges {};
330cdd98
PA
379
380 VEC (dwarf2_section_info_def) *types = NULL;
8b70b953 381
be391dca 382 /* Back link. */
330cdd98 383 struct objfile *objfile = NULL;
be391dca 384
d467dd73 385 /* Table of all the compilation units. This is used to locate
10b3939b 386 the target compilation unit of a particular reference. */
330cdd98 387 struct dwarf2_per_cu_data **all_comp_units = NULL;
ae038cb0
DJ
388
389 /* The number of compilation units in ALL_COMP_UNITS. */
330cdd98 390 int n_comp_units = 0;
ae038cb0 391
1fd400ff 392 /* The number of .debug_types-related CUs. */
330cdd98 393 int n_type_units = 0;
1fd400ff 394
6aa5f3a6
DE
395 /* The number of elements allocated in all_type_units.
396 If there are skeleton-less TUs, we add them to all_type_units lazily. */
330cdd98 397 int n_allocated_type_units = 0;
6aa5f3a6 398
a2ce51a0
DE
399 /* The .debug_types-related CUs (TUs).
400 This is stored in malloc space because we may realloc it. */
330cdd98 401 struct signatured_type **all_type_units = NULL;
1fd400ff 402
f4dc4d17
DE
403 /* Table of struct type_unit_group objects.
404 The hash key is the DW_AT_stmt_list value. */
330cdd98 405 htab_t type_unit_groups {};
72dca2f5 406
348e048f
DE
407 /* A table mapping .debug_types signatures to its signatured_type entry.
408 This is NULL if the .debug_types section hasn't been read in yet. */
330cdd98 409 htab_t signatured_types {};
348e048f 410
f4dc4d17
DE
411 /* Type unit statistics, to see how well the scaling improvements
412 are doing. */
330cdd98 413 struct tu_stats tu_stats {};
f4dc4d17
DE
414
415 /* A chain of compilation units that are currently read in, so that
416 they can be freed later. */
330cdd98 417 dwarf2_per_cu_data *read_in_chain = NULL;
f4dc4d17 418
3019eac3
DE
419 /* A table mapping DW_AT_dwo_name values to struct dwo_file objects.
420 This is NULL if the table hasn't been allocated yet. */
330cdd98 421 htab_t dwo_files {};
3019eac3 422
330cdd98
PA
423 /* True if we've checked for whether there is a DWP file. */
424 bool dwp_checked = false;
80626a55
DE
425
426 /* The DWP file if there is one, or NULL. */
330cdd98 427 struct dwp_file *dwp_file = NULL;
80626a55 428
36586728
TT
429 /* The shared '.dwz' file, if one exists. This is used when the
430 original data was compressed using 'dwz -m'. */
330cdd98 431 struct dwz_file *dwz_file = NULL;
36586728 432
330cdd98 433 /* A flag indicating whether this objfile has a section loaded at a
72dca2f5 434 VMA of 0. */
330cdd98 435 bool has_section_at_zero = false;
9291a0cd 436
ae2de4f8
DE
437 /* True if we are using the mapped index,
438 or we are faking it for OBJF_READNOW's sake. */
330cdd98 439 bool using_index = false;
9291a0cd 440
ae2de4f8 441 /* The mapped index, or NULL if .gdb_index is missing or not being used. */
330cdd98 442 mapped_index *index_table = NULL;
98bfdba5 443
927aa2e7
JK
444 /* The mapped index, or NULL if .debug_names is missing or not being used. */
445 std::unique_ptr<mapped_debug_names> debug_names_table;
446
7b9f3c50 447 /* When using index_table, this keeps track of all quick_file_names entries.
56e64610
DE
448 TUs typically share line table entries with a CU, so we maintain a
449 separate table of all line table entries to support the sharing.
450 Note that while there can be way more TUs than CUs, we've already
451 sorted all the TUs into "type unit groups", grouped by their
452 DW_AT_stmt_list value. Therefore the only sharing done here is with a
453 CU and its associated TU group if there is one. */
330cdd98 454 htab_t quick_file_names_table {};
7b9f3c50 455
98bfdba5
PA
456 /* Set during partial symbol reading, to prevent queueing of full
457 symbols. */
330cdd98 458 bool reading_partial_symbols = false;
673bfd45 459
dee91e82 460 /* Table mapping type DIEs to their struct type *.
673bfd45 461 This is NULL if not allocated yet.
02142a6c 462 The mapping is done via (CU/TU + DIE offset) -> type. */
330cdd98 463 htab_t die_type_hash {};
95554aad
TT
464
465 /* The CUs we recently read. */
330cdd98 466 VEC (dwarf2_per_cu_ptr) *just_read_cus = NULL;
527f3840
JK
467
468 /* Table containing line_header indexed by offset and offset_in_dwz. */
330cdd98 469 htab_t line_header_hash {};
bbf2f4df
PA
470
471 /* Table containing all filenames. This is an optional because the
472 table is lazily constructed on first access. */
473 gdb::optional<filename_seen_cache> filenames_cache;
6502dd73
DJ
474};
475
476static struct dwarf2_per_objfile *dwarf2_per_objfile;
c906108c 477
251d32d9 478/* Default names of the debugging sections. */
c906108c 479
233a11ab
CS
480/* Note that if the debugging section has been compressed, it might
481 have a name like .zdebug_info. */
482
9cdd5dbd
DE
483static const struct dwarf2_debug_sections dwarf2_elf_names =
484{
251d32d9
TG
485 { ".debug_info", ".zdebug_info" },
486 { ".debug_abbrev", ".zdebug_abbrev" },
487 { ".debug_line", ".zdebug_line" },
488 { ".debug_loc", ".zdebug_loc" },
43988095 489 { ".debug_loclists", ".zdebug_loclists" },
251d32d9 490 { ".debug_macinfo", ".zdebug_macinfo" },
cf2c3c16 491 { ".debug_macro", ".zdebug_macro" },
251d32d9 492 { ".debug_str", ".zdebug_str" },
43988095 493 { ".debug_line_str", ".zdebug_line_str" },
251d32d9 494 { ".debug_ranges", ".zdebug_ranges" },
43988095 495 { ".debug_rnglists", ".zdebug_rnglists" },
251d32d9 496 { ".debug_types", ".zdebug_types" },
3019eac3 497 { ".debug_addr", ".zdebug_addr" },
251d32d9
TG
498 { ".debug_frame", ".zdebug_frame" },
499 { ".eh_frame", NULL },
24d3216f 500 { ".gdb_index", ".zgdb_index" },
927aa2e7
JK
501 { ".debug_names", ".zdebug_names" },
502 { ".debug_aranges", ".zdebug_aranges" },
24d3216f 503 23
251d32d9 504};
c906108c 505
80626a55 506/* List of DWO/DWP sections. */
3019eac3 507
80626a55 508static const struct dwop_section_names
3019eac3
DE
509{
510 struct dwarf2_section_names abbrev_dwo;
511 struct dwarf2_section_names info_dwo;
512 struct dwarf2_section_names line_dwo;
513 struct dwarf2_section_names loc_dwo;
43988095 514 struct dwarf2_section_names loclists_dwo;
09262596
DE
515 struct dwarf2_section_names macinfo_dwo;
516 struct dwarf2_section_names macro_dwo;
3019eac3
DE
517 struct dwarf2_section_names str_dwo;
518 struct dwarf2_section_names str_offsets_dwo;
519 struct dwarf2_section_names types_dwo;
80626a55
DE
520 struct dwarf2_section_names cu_index;
521 struct dwarf2_section_names tu_index;
3019eac3 522}
80626a55 523dwop_section_names =
3019eac3
DE
524{
525 { ".debug_abbrev.dwo", ".zdebug_abbrev.dwo" },
526 { ".debug_info.dwo", ".zdebug_info.dwo" },
527 { ".debug_line.dwo", ".zdebug_line.dwo" },
528 { ".debug_loc.dwo", ".zdebug_loc.dwo" },
43988095 529 { ".debug_loclists.dwo", ".zdebug_loclists.dwo" },
09262596
DE
530 { ".debug_macinfo.dwo", ".zdebug_macinfo.dwo" },
531 { ".debug_macro.dwo", ".zdebug_macro.dwo" },
3019eac3
DE
532 { ".debug_str.dwo", ".zdebug_str.dwo" },
533 { ".debug_str_offsets.dwo", ".zdebug_str_offsets.dwo" },
534 { ".debug_types.dwo", ".zdebug_types.dwo" },
80626a55
DE
535 { ".debug_cu_index", ".zdebug_cu_index" },
536 { ".debug_tu_index", ".zdebug_tu_index" },
3019eac3
DE
537};
538
c906108c
SS
539/* local data types */
540
107d2387
AC
541/* The data in a compilation unit header, after target2host
542 translation, looks like this. */
c906108c 543struct comp_unit_head
a738430d 544{
c764a876 545 unsigned int length;
a738430d 546 short version;
a738430d
MK
547 unsigned char addr_size;
548 unsigned char signed_addr_p;
9c541725 549 sect_offset abbrev_sect_off;
57349743 550
a738430d
MK
551 /* Size of file offsets; either 4 or 8. */
552 unsigned int offset_size;
57349743 553
a738430d
MK
554 /* Size of the length field; either 4 or 12. */
555 unsigned int initial_length_size;
57349743 556
43988095
JK
557 enum dwarf_unit_type unit_type;
558
a738430d
MK
559 /* Offset to the first byte of this compilation unit header in the
560 .debug_info section, for resolving relative reference dies. */
9c541725 561 sect_offset sect_off;
57349743 562
d00adf39
DE
563 /* Offset to first die in this cu from the start of the cu.
564 This will be the first byte following the compilation unit header. */
9c541725 565 cu_offset first_die_cu_offset;
43988095
JK
566
567 /* 64-bit signature of this type unit - it is valid only for
568 UNIT_TYPE DW_UT_type. */
569 ULONGEST signature;
570
571 /* For types, offset in the type's DIE of the type defined by this TU. */
9c541725 572 cu_offset type_cu_offset_in_tu;
a738430d 573};
c906108c 574
3da10d80
KS
575/* Type used for delaying computation of method physnames.
576 See comments for compute_delayed_physnames. */
577struct delayed_method_info
578{
579 /* The type to which the method is attached, i.e., its parent class. */
580 struct type *type;
581
582 /* The index of the method in the type's function fieldlists. */
583 int fnfield_index;
584
585 /* The index of the method in the fieldlist. */
586 int index;
587
588 /* The name of the DIE. */
589 const char *name;
590
591 /* The DIE associated with this method. */
592 struct die_info *die;
593};
594
595typedef struct delayed_method_info delayed_method_info;
596DEF_VEC_O (delayed_method_info);
597
e7c27a73
DJ
598/* Internal state when decoding a particular compilation unit. */
599struct dwarf2_cu
600{
601 /* The objfile containing this compilation unit. */
602 struct objfile *objfile;
603
d00adf39 604 /* The header of the compilation unit. */
e7c27a73 605 struct comp_unit_head header;
e142c38c 606
d00adf39
DE
607 /* Base address of this compilation unit. */
608 CORE_ADDR base_address;
609
610 /* Non-zero if base_address has been set. */
611 int base_known;
612
e142c38c
DJ
613 /* The language we are debugging. */
614 enum language language;
615 const struct language_defn *language_defn;
616
b0f35d58
DL
617 const char *producer;
618
e142c38c
DJ
619 /* The generic symbol table building routines have separate lists for
620 file scope symbols and all all other scopes (local scopes). So
621 we need to select the right one to pass to add_symbol_to_list().
622 We do it by keeping a pointer to the correct list in list_in_scope.
623
624 FIXME: The original dwarf code just treated the file scope as the
625 first local scope, and all other local scopes as nested local
626 scopes, and worked fine. Check to see if we really need to
627 distinguish these in buildsym.c. */
628 struct pending **list_in_scope;
629
433df2d4
DE
630 /* The abbrev table for this CU.
631 Normally this points to the abbrev table in the objfile.
632 But if DWO_UNIT is non-NULL this is the abbrev table in the DWO file. */
633 struct abbrev_table *abbrev_table;
72bf9492 634
b64f50a1
JK
635 /* Hash table holding all the loaded partial DIEs
636 with partial_die->offset.SECT_OFF as hash. */
72bf9492
DJ
637 htab_t partial_dies;
638
639 /* Storage for things with the same lifetime as this read-in compilation
640 unit, including partial DIEs. */
641 struct obstack comp_unit_obstack;
642
ae038cb0
DJ
643 /* When multiple dwarf2_cu structures are living in memory, this field
644 chains them all together, so that they can be released efficiently.
645 We will probably also want a generation counter so that most-recently-used
646 compilation units are cached... */
647 struct dwarf2_per_cu_data *read_in_chain;
648
69d751e3 649 /* Backlink to our per_cu entry. */
ae038cb0
DJ
650 struct dwarf2_per_cu_data *per_cu;
651
652 /* How many compilation units ago was this CU last referenced? */
653 int last_used;
654
b64f50a1
JK
655 /* A hash table of DIE cu_offset for following references with
656 die_info->offset.sect_off as hash. */
51545339 657 htab_t die_hash;
10b3939b
DJ
658
659 /* Full DIEs if read in. */
660 struct die_info *dies;
661
662 /* A set of pointers to dwarf2_per_cu_data objects for compilation
663 units referenced by this one. Only set during full symbol processing;
664 partial symbol tables do not have dependencies. */
665 htab_t dependencies;
666
cb1df416
DJ
667 /* Header data from the line table, during full symbol processing. */
668 struct line_header *line_header;
4c8aa72d
PA
669 /* Non-NULL if LINE_HEADER is owned by this DWARF_CU. Otherwise,
670 it's owned by dwarf2_per_objfile::line_header_hash. If non-NULL,
671 this is the DW_TAG_compile_unit die for this CU. We'll hold on
672 to the line header as long as this DIE is being processed. See
673 process_die_scope. */
674 die_info *line_header_die_owner;
cb1df416 675
3da10d80
KS
676 /* A list of methods which need to have physnames computed
677 after all type information has been read. */
678 VEC (delayed_method_info) *method_list;
679
96408a79
SA
680 /* To be copied to symtab->call_site_htab. */
681 htab_t call_site_htab;
682
034e5797
DE
683 /* Non-NULL if this CU came from a DWO file.
684 There is an invariant here that is important to remember:
685 Except for attributes copied from the top level DIE in the "main"
686 (or "stub") file in preparation for reading the DWO file
687 (e.g., DW_AT_GNU_addr_base), we KISS: there is only *one* CU.
688 Either there isn't a DWO file (in which case this is NULL and the point
689 is moot), or there is and either we're not going to read it (in which
690 case this is NULL) or there is and we are reading it (in which case this
691 is non-NULL). */
3019eac3
DE
692 struct dwo_unit *dwo_unit;
693
694 /* The DW_AT_addr_base attribute if present, zero otherwise
695 (zero is a valid value though).
1dbab08b 696 Note this value comes from the Fission stub CU/TU's DIE. */
3019eac3
DE
697 ULONGEST addr_base;
698
2e3cf129
DE
699 /* The DW_AT_ranges_base attribute if present, zero otherwise
700 (zero is a valid value though).
1dbab08b 701 Note this value comes from the Fission stub CU/TU's DIE.
2e3cf129 702 Also note that the value is zero in the non-DWO case so this value can
ab435259
DE
703 be used without needing to know whether DWO files are in use or not.
704 N.B. This does not apply to DW_AT_ranges appearing in
705 DW_TAG_compile_unit dies. This is a bit of a wart, consider if ever
706 DW_AT_ranges appeared in the DW_TAG_compile_unit of DWO DIEs: then
707 DW_AT_ranges_base *would* have to be applied, and we'd have to care
708 whether the DW_AT_ranges attribute came from the skeleton or DWO. */
2e3cf129
DE
709 ULONGEST ranges_base;
710
ae038cb0
DJ
711 /* Mark used when releasing cached dies. */
712 unsigned int mark : 1;
713
8be455d7
JK
714 /* This CU references .debug_loc. See the symtab->locations_valid field.
715 This test is imperfect as there may exist optimized debug code not using
716 any location list and still facing inlining issues if handled as
717 unoptimized code. For a future better test see GCC PR other/32998. */
8be455d7 718 unsigned int has_loclist : 1;
ba919b58 719
1b80a9fa
JK
720 /* These cache the results for producer_is_* fields. CHECKED_PRODUCER is set
721 if all the producer_is_* fields are valid. This information is cached
722 because profiling CU expansion showed excessive time spent in
723 producer_is_gxx_lt_4_6. */
ba919b58
TT
724 unsigned int checked_producer : 1;
725 unsigned int producer_is_gxx_lt_4_6 : 1;
1b80a9fa 726 unsigned int producer_is_gcc_lt_4_3 : 1;
5230b05a 727 unsigned int producer_is_icc_lt_14 : 1;
4d4ec4e5
TT
728
729 /* When set, the file that we're processing is known to have
730 debugging info for C++ namespaces. GCC 3.3.x did not produce
731 this information, but later versions do. */
732
733 unsigned int processing_has_namespace_info : 1;
e7c27a73
DJ
734};
735
10b3939b
DJ
736/* Persistent data held for a compilation unit, even when not
737 processing it. We put a pointer to this structure in the
28dee7f5 738 read_symtab_private field of the psymtab. */
10b3939b 739
ae038cb0
DJ
740struct dwarf2_per_cu_data
741{
36586728 742 /* The start offset and length of this compilation unit.
45452591 743 NOTE: Unlike comp_unit_head.length, this length includes
3019eac3
DE
744 initial_length_size.
745 If the DIE refers to a DWO file, this is always of the original die,
746 not the DWO file. */
9c541725 747 sect_offset sect_off;
36586728 748 unsigned int length;
ae038cb0 749
43988095
JK
750 /* DWARF standard version this data has been read from (such as 4 or 5). */
751 short dwarf_version;
752
ae038cb0
DJ
753 /* Flag indicating this compilation unit will be read in before
754 any of the current compilation units are processed. */
c764a876 755 unsigned int queued : 1;
ae038cb0 756
0d99eb77
DE
757 /* This flag will be set when reading partial DIEs if we need to load
758 absolutely all DIEs for this compilation unit, instead of just the ones
759 we think are interesting. It gets set if we look for a DIE in the
5afb4e99
DJ
760 hash table and don't find it. */
761 unsigned int load_all_dies : 1;
762
0186c6a7
DE
763 /* Non-zero if this CU is from .debug_types.
764 Struct dwarf2_per_cu_data is contained in struct signatured_type iff
765 this is non-zero. */
3019eac3
DE
766 unsigned int is_debug_types : 1;
767
36586728
TT
768 /* Non-zero if this CU is from the .dwz file. */
769 unsigned int is_dwz : 1;
770
a2ce51a0
DE
771 /* Non-zero if reading a TU directly from a DWO file, bypassing the stub.
772 This flag is only valid if is_debug_types is true.
773 We can't read a CU directly from a DWO file: There are required
774 attributes in the stub. */
775 unsigned int reading_dwo_directly : 1;
776
7ee85ab1
DE
777 /* Non-zero if the TU has been read.
778 This is used to assist the "Stay in DWO Optimization" for Fission:
779 When reading a DWO, it's faster to read TUs from the DWO instead of
780 fetching them from random other DWOs (due to comdat folding).
781 If the TU has already been read, the optimization is unnecessary
782 (and unwise - we don't want to change where gdb thinks the TU lives
783 "midflight").
784 This flag is only valid if is_debug_types is true. */
785 unsigned int tu_read : 1;
786
3019eac3
DE
787 /* The section this CU/TU lives in.
788 If the DIE refers to a DWO file, this is always the original die,
789 not the DWO file. */
8a0459fd 790 struct dwarf2_section_info *section;
348e048f 791
17ea53c3 792 /* Set to non-NULL iff this CU is currently loaded. When it gets freed out
cc12ce38
DE
793 of the CU cache it gets reset to NULL again. This is left as NULL for
794 dummy CUs (a CU header, but nothing else). */
ae038cb0 795 struct dwarf2_cu *cu;
1c379e20 796
9cdd5dbd
DE
797 /* The corresponding objfile.
798 Normally we can get the objfile from dwarf2_per_objfile.
799 However we can enter this file with just a "per_cu" handle. */
9291a0cd
TT
800 struct objfile *objfile;
801
fffbe6a8
YQ
802 /* When dwarf2_per_objfile->using_index is true, the 'quick' field
803 is active. Otherwise, the 'psymtab' field is active. */
9291a0cd
TT
804 union
805 {
806 /* The partial symbol table associated with this compilation unit,
95554aad 807 or NULL for unread partial units. */
9291a0cd
TT
808 struct partial_symtab *psymtab;
809
810 /* Data needed by the "quick" functions. */
811 struct dwarf2_per_cu_quick_data *quick;
812 } v;
95554aad 813
796a7ff8
DE
814 /* The CUs we import using DW_TAG_imported_unit. This is filled in
815 while reading psymtabs, used to compute the psymtab dependencies,
816 and then cleared. Then it is filled in again while reading full
817 symbols, and only deleted when the objfile is destroyed.
818
819 This is also used to work around a difference between the way gold
820 generates .gdb_index version <=7 and the way gdb does. Arguably this
821 is a gold bug. For symbols coming from TUs, gold records in the index
822 the CU that includes the TU instead of the TU itself. This breaks
823 dw2_lookup_symbol: It assumes that if the index says symbol X lives
824 in CU/TU Y, then one need only expand Y and a subsequent lookup in Y
825 will find X. Alas TUs live in their own symtab, so after expanding CU Y
826 we need to look in TU Z to find X. Fortunately, this is akin to
827 DW_TAG_imported_unit, so we just use the same mechanism: For
828 .gdb_index version <=7 this also records the TUs that the CU referred
829 to. Concurrently with this change gdb was modified to emit version 8
69d751e3
DE
830 indices so we only pay a price for gold generated indices.
831 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
796a7ff8 832 VEC (dwarf2_per_cu_ptr) *imported_symtabs;
ae038cb0
DJ
833};
834
348e048f
DE
835/* Entry in the signatured_types hash table. */
836
837struct signatured_type
838{
42e7ad6c 839 /* The "per_cu" object of this type.
ac9ec31b 840 This struct is used iff per_cu.is_debug_types.
42e7ad6c
DE
841 N.B.: This is the first member so that it's easy to convert pointers
842 between them. */
843 struct dwarf2_per_cu_data per_cu;
844
3019eac3 845 /* The type's signature. */
348e048f
DE
846 ULONGEST signature;
847
3019eac3 848 /* Offset in the TU of the type's DIE, as read from the TU header.
c88ee1f0
DE
849 If this TU is a DWO stub and the definition lives in a DWO file
850 (specified by DW_AT_GNU_dwo_name), this value is unusable. */
3019eac3
DE
851 cu_offset type_offset_in_tu;
852
853 /* Offset in the section of the type's DIE.
854 If the definition lives in a DWO file, this is the offset in the
855 .debug_types.dwo section.
856 The value is zero until the actual value is known.
857 Zero is otherwise not a valid section offset. */
858 sect_offset type_offset_in_section;
0186c6a7
DE
859
860 /* Type units are grouped by their DW_AT_stmt_list entry so that they
861 can share them. This points to the containing symtab. */
862 struct type_unit_group *type_unit_group;
ac9ec31b
DE
863
864 /* The type.
865 The first time we encounter this type we fully read it in and install it
866 in the symbol tables. Subsequent times we only need the type. */
867 struct type *type;
a2ce51a0
DE
868
869 /* Containing DWO unit.
870 This field is valid iff per_cu.reading_dwo_directly. */
871 struct dwo_unit *dwo_unit;
348e048f
DE
872};
873
0186c6a7
DE
874typedef struct signatured_type *sig_type_ptr;
875DEF_VEC_P (sig_type_ptr);
876
094b34ac
DE
877/* A struct that can be used as a hash key for tables based on DW_AT_stmt_list.
878 This includes type_unit_group and quick_file_names. */
879
880struct stmt_list_hash
881{
882 /* The DWO unit this table is from or NULL if there is none. */
883 struct dwo_unit *dwo_unit;
884
885 /* Offset in .debug_line or .debug_line.dwo. */
9c541725 886 sect_offset line_sect_off;
094b34ac
DE
887};
888
f4dc4d17
DE
889/* Each element of dwarf2_per_objfile->type_unit_groups is a pointer to
890 an object of this type. */
891
892struct type_unit_group
893{
0186c6a7 894 /* dwarf2read.c's main "handle" on a TU symtab.
f4dc4d17
DE
895 To simplify things we create an artificial CU that "includes" all the
896 type units using this stmt_list so that the rest of the code still has
897 a "per_cu" handle on the symtab.
898 This PER_CU is recognized by having no section. */
8a0459fd 899#define IS_TYPE_UNIT_GROUP(per_cu) ((per_cu)->section == NULL)
094b34ac
DE
900 struct dwarf2_per_cu_data per_cu;
901
0186c6a7
DE
902 /* The TUs that share this DW_AT_stmt_list entry.
903 This is added to while parsing type units to build partial symtabs,
904 and is deleted afterwards and not used again. */
905 VEC (sig_type_ptr) *tus;
f4dc4d17 906
43f3e411 907 /* The compunit symtab.
094b34ac 908 Type units in a group needn't all be defined in the same source file,
43f3e411
DE
909 so we create an essentially anonymous symtab as the compunit symtab. */
910 struct compunit_symtab *compunit_symtab;
f4dc4d17 911
094b34ac
DE
912 /* The data used to construct the hash key. */
913 struct stmt_list_hash hash;
f4dc4d17
DE
914
915 /* The number of symtabs from the line header.
916 The value here must match line_header.num_file_names. */
917 unsigned int num_symtabs;
918
919 /* The symbol tables for this TU (obtained from the files listed in
920 DW_AT_stmt_list).
921 WARNING: The order of entries here must match the order of entries
922 in the line header. After the first TU using this type_unit_group, the
923 line header for the subsequent TUs is recreated from this. This is done
924 because we need to use the same symtabs for each TU using the same
925 DW_AT_stmt_list value. Also note that symtabs may be repeated here,
926 there's no guarantee the line header doesn't have duplicate entries. */
927 struct symtab **symtabs;
928};
929
73869dc2 930/* These sections are what may appear in a (real or virtual) DWO file. */
3019eac3
DE
931
932struct dwo_sections
933{
934 struct dwarf2_section_info abbrev;
3019eac3
DE
935 struct dwarf2_section_info line;
936 struct dwarf2_section_info loc;
43988095 937 struct dwarf2_section_info loclists;
09262596
DE
938 struct dwarf2_section_info macinfo;
939 struct dwarf2_section_info macro;
3019eac3
DE
940 struct dwarf2_section_info str;
941 struct dwarf2_section_info str_offsets;
80626a55
DE
942 /* In the case of a virtual DWO file, these two are unused. */
943 struct dwarf2_section_info info;
3019eac3
DE
944 VEC (dwarf2_section_info_def) *types;
945};
946
c88ee1f0 947/* CUs/TUs in DWP/DWO files. */
3019eac3
DE
948
949struct dwo_unit
950{
951 /* Backlink to the containing struct dwo_file. */
952 struct dwo_file *dwo_file;
953
954 /* The "id" that distinguishes this CU/TU.
955 .debug_info calls this "dwo_id", .debug_types calls this "signature".
956 Since signatures came first, we stick with it for consistency. */
957 ULONGEST signature;
958
959 /* The section this CU/TU lives in, in the DWO file. */
8a0459fd 960 struct dwarf2_section_info *section;
3019eac3 961
9c541725
PA
962 /* Same as dwarf2_per_cu_data:{sect_off,length} but in the DWO section. */
963 sect_offset sect_off;
3019eac3
DE
964 unsigned int length;
965
966 /* For types, offset in the type's DIE of the type defined by this TU. */
967 cu_offset type_offset_in_tu;
968};
969
73869dc2
DE
970/* include/dwarf2.h defines the DWP section codes.
971 It defines a max value but it doesn't define a min value, which we
972 use for error checking, so provide one. */
973
974enum dwp_v2_section_ids
975{
976 DW_SECT_MIN = 1
977};
978
80626a55 979/* Data for one DWO file.
57d63ce2
DE
980
981 This includes virtual DWO files (a virtual DWO file is a DWO file as it
982 appears in a DWP file). DWP files don't really have DWO files per se -
983 comdat folding of types "loses" the DWO file they came from, and from
984 a high level view DWP files appear to contain a mass of random types.
985 However, to maintain consistency with the non-DWP case we pretend DWP
986 files contain virtual DWO files, and we assign each TU with one virtual
987 DWO file (generally based on the line and abbrev section offsets -
988 a heuristic that seems to work in practice). */
3019eac3
DE
989
990struct dwo_file
991{
0ac5b59e 992 /* The DW_AT_GNU_dwo_name attribute.
80626a55
DE
993 For virtual DWO files the name is constructed from the section offsets
994 of abbrev,line,loc,str_offsets so that we combine virtual DWO files
995 from related CU+TUs. */
0ac5b59e
DE
996 const char *dwo_name;
997
998 /* The DW_AT_comp_dir attribute. */
999 const char *comp_dir;
3019eac3 1000
80626a55
DE
1001 /* The bfd, when the file is open. Otherwise this is NULL.
1002 This is unused(NULL) for virtual DWO files where we use dwp_file.dbfd. */
1003 bfd *dbfd;
3019eac3 1004
73869dc2
DE
1005 /* The sections that make up this DWO file.
1006 Remember that for virtual DWO files in DWP V2, these are virtual
1007 sections (for lack of a better name). */
3019eac3
DE
1008 struct dwo_sections sections;
1009
33c5cd75
DB
1010 /* The CUs in the file.
1011 Each element is a struct dwo_unit. Multiple CUs per DWO are supported as
1012 an extension to handle LLVM's Link Time Optimization output (where
1013 multiple source files may be compiled into a single object/dwo pair). */
1014 htab_t cus;
3019eac3
DE
1015
1016 /* Table of TUs in the file.
1017 Each element is a struct dwo_unit. */
1018 htab_t tus;
1019};
1020
80626a55
DE
1021/* These sections are what may appear in a DWP file. */
1022
1023struct dwp_sections
1024{
73869dc2 1025 /* These are used by both DWP version 1 and 2. */
80626a55
DE
1026 struct dwarf2_section_info str;
1027 struct dwarf2_section_info cu_index;
1028 struct dwarf2_section_info tu_index;
73869dc2
DE
1029
1030 /* These are only used by DWP version 2 files.
1031 In DWP version 1 the .debug_info.dwo, .debug_types.dwo, and other
1032 sections are referenced by section number, and are not recorded here.
1033 In DWP version 2 there is at most one copy of all these sections, each
1034 section being (effectively) comprised of the concatenation of all of the
1035 individual sections that exist in the version 1 format.
1036 To keep the code simple we treat each of these concatenated pieces as a
1037 section itself (a virtual section?). */
1038 struct dwarf2_section_info abbrev;
1039 struct dwarf2_section_info info;
1040 struct dwarf2_section_info line;
1041 struct dwarf2_section_info loc;
1042 struct dwarf2_section_info macinfo;
1043 struct dwarf2_section_info macro;
1044 struct dwarf2_section_info str_offsets;
1045 struct dwarf2_section_info types;
80626a55
DE
1046};
1047
73869dc2
DE
1048/* These sections are what may appear in a virtual DWO file in DWP version 1.
1049 A virtual DWO file is a DWO file as it appears in a DWP file. */
80626a55 1050
73869dc2 1051struct virtual_v1_dwo_sections
80626a55
DE
1052{
1053 struct dwarf2_section_info abbrev;
1054 struct dwarf2_section_info line;
1055 struct dwarf2_section_info loc;
1056 struct dwarf2_section_info macinfo;
1057 struct dwarf2_section_info macro;
1058 struct dwarf2_section_info str_offsets;
1059 /* Each DWP hash table entry records one CU or one TU.
8a0459fd 1060 That is recorded here, and copied to dwo_unit.section. */
80626a55
DE
1061 struct dwarf2_section_info info_or_types;
1062};
1063
73869dc2
DE
1064/* Similar to virtual_v1_dwo_sections, but for DWP version 2.
1065 In version 2, the sections of the DWO files are concatenated together
1066 and stored in one section of that name. Thus each ELF section contains
1067 several "virtual" sections. */
1068
1069struct virtual_v2_dwo_sections
1070{
1071 bfd_size_type abbrev_offset;
1072 bfd_size_type abbrev_size;
1073
1074 bfd_size_type line_offset;
1075 bfd_size_type line_size;
1076
1077 bfd_size_type loc_offset;
1078 bfd_size_type loc_size;
1079
1080 bfd_size_type macinfo_offset;
1081 bfd_size_type macinfo_size;
1082
1083 bfd_size_type macro_offset;
1084 bfd_size_type macro_size;
1085
1086 bfd_size_type str_offsets_offset;
1087 bfd_size_type str_offsets_size;
1088
1089 /* Each DWP hash table entry records one CU or one TU.
1090 That is recorded here, and copied to dwo_unit.section. */
1091 bfd_size_type info_or_types_offset;
1092 bfd_size_type info_or_types_size;
1093};
1094
80626a55
DE
1095/* Contents of DWP hash tables. */
1096
1097struct dwp_hash_table
1098{
73869dc2 1099 uint32_t version, nr_columns;
80626a55 1100 uint32_t nr_units, nr_slots;
73869dc2
DE
1101 const gdb_byte *hash_table, *unit_table;
1102 union
1103 {
1104 struct
1105 {
1106 const gdb_byte *indices;
1107 } v1;
1108 struct
1109 {
1110 /* This is indexed by column number and gives the id of the section
1111 in that column. */
1112#define MAX_NR_V2_DWO_SECTIONS \
1113 (1 /* .debug_info or .debug_types */ \
1114 + 1 /* .debug_abbrev */ \
1115 + 1 /* .debug_line */ \
1116 + 1 /* .debug_loc */ \
1117 + 1 /* .debug_str_offsets */ \
1118 + 1 /* .debug_macro or .debug_macinfo */)
1119 int section_ids[MAX_NR_V2_DWO_SECTIONS];
1120 const gdb_byte *offsets;
1121 const gdb_byte *sizes;
1122 } v2;
1123 } section_pool;
80626a55
DE
1124};
1125
1126/* Data for one DWP file. */
1127
1128struct dwp_file
1129{
1130 /* Name of the file. */
1131 const char *name;
1132
73869dc2
DE
1133 /* File format version. */
1134 int version;
1135
93417882 1136 /* The bfd. */
80626a55
DE
1137 bfd *dbfd;
1138
1139 /* Section info for this file. */
1140 struct dwp_sections sections;
1141
57d63ce2 1142 /* Table of CUs in the file. */
80626a55
DE
1143 const struct dwp_hash_table *cus;
1144
1145 /* Table of TUs in the file. */
1146 const struct dwp_hash_table *tus;
1147
19ac8c2e
DE
1148 /* Tables of loaded CUs/TUs. Each entry is a struct dwo_unit *. */
1149 htab_t loaded_cus;
1150 htab_t loaded_tus;
80626a55 1151
73869dc2
DE
1152 /* Table to map ELF section numbers to their sections.
1153 This is only needed for the DWP V1 file format. */
80626a55
DE
1154 unsigned int num_sections;
1155 asection **elf_sections;
1156};
1157
36586728
TT
1158/* This represents a '.dwz' file. */
1159
1160struct dwz_file
1161{
1162 /* A dwz file can only contain a few sections. */
1163 struct dwarf2_section_info abbrev;
1164 struct dwarf2_section_info info;
1165 struct dwarf2_section_info str;
1166 struct dwarf2_section_info line;
1167 struct dwarf2_section_info macro;
2ec9a5e0 1168 struct dwarf2_section_info gdb_index;
927aa2e7 1169 struct dwarf2_section_info debug_names;
36586728
TT
1170
1171 /* The dwz's BFD. */
1172 bfd *dwz_bfd;
1173};
1174
0963b4bd
MS
1175/* Struct used to pass misc. parameters to read_die_and_children, et
1176 al. which are used for both .debug_info and .debug_types dies.
1177 All parameters here are unchanging for the life of the call. This
dee91e82 1178 struct exists to abstract away the constant parameters of die reading. */
93311388
DE
1179
1180struct die_reader_specs
1181{
a32a8923 1182 /* The bfd of die_section. */
93311388
DE
1183 bfd* abfd;
1184
1185 /* The CU of the DIE we are parsing. */
1186 struct dwarf2_cu *cu;
1187
80626a55 1188 /* Non-NULL if reading a DWO file (including one packaged into a DWP). */
3019eac3
DE
1189 struct dwo_file *dwo_file;
1190
dee91e82 1191 /* The section the die comes from.
3019eac3 1192 This is either .debug_info or .debug_types, or the .dwo variants. */
dee91e82
DE
1193 struct dwarf2_section_info *die_section;
1194
1195 /* die_section->buffer. */
d521ce57 1196 const gdb_byte *buffer;
f664829e
DE
1197
1198 /* The end of the buffer. */
1199 const gdb_byte *buffer_end;
a2ce51a0
DE
1200
1201 /* The value of the DW_AT_comp_dir attribute. */
1202 const char *comp_dir;
93311388
DE
1203};
1204
fd820528 1205/* Type of function passed to init_cutu_and_read_dies, et.al. */
dee91e82 1206typedef void (die_reader_func_ftype) (const struct die_reader_specs *reader,
d521ce57 1207 const gdb_byte *info_ptr,
dee91e82
DE
1208 struct die_info *comp_unit_die,
1209 int has_children,
1210 void *data);
1211
ecfb656c
PA
1212/* A 1-based directory index. This is a strong typedef to prevent
1213 accidentally using a directory index as a 0-based index into an
1214 array/vector. */
1215enum class dir_index : unsigned int {};
1216
1217/* Likewise, a 1-based file name index. */
1218enum class file_name_index : unsigned int {};
1219
52059ffd
TT
1220struct file_entry
1221{
fff8551c
PA
1222 file_entry () = default;
1223
ecfb656c 1224 file_entry (const char *name_, dir_index d_index_,
fff8551c
PA
1225 unsigned int mod_time_, unsigned int length_)
1226 : name (name_),
ecfb656c 1227 d_index (d_index_),
fff8551c
PA
1228 mod_time (mod_time_),
1229 length (length_)
1230 {}
1231
ecfb656c
PA
1232 /* Return the include directory at D_INDEX stored in LH. Returns
1233 NULL if D_INDEX is out of bounds. */
8c43009f
PA
1234 const char *include_dir (const line_header *lh) const;
1235
fff8551c
PA
1236 /* The file name. Note this is an observing pointer. The memory is
1237 owned by debug_line_buffer. */
1238 const char *name {};
1239
8c43009f 1240 /* The directory index (1-based). */
ecfb656c 1241 dir_index d_index {};
fff8551c
PA
1242
1243 unsigned int mod_time {};
1244
1245 unsigned int length {};
1246
1247 /* True if referenced by the Line Number Program. */
1248 bool included_p {};
1249
83769d0b 1250 /* The associated symbol table, if any. */
fff8551c 1251 struct symtab *symtab {};
52059ffd
TT
1252};
1253
debd256d
JB
1254/* The line number information for a compilation unit (found in the
1255 .debug_line section) begins with a "statement program header",
1256 which contains the following information. */
1257struct line_header
1258{
fff8551c
PA
1259 line_header ()
1260 : offset_in_dwz {}
1261 {}
1262
1263 /* Add an entry to the include directory table. */
1264 void add_include_dir (const char *include_dir);
1265
1266 /* Add an entry to the file name table. */
ecfb656c 1267 void add_file_name (const char *name, dir_index d_index,
fff8551c
PA
1268 unsigned int mod_time, unsigned int length);
1269
ecfb656c 1270 /* Return the include dir at INDEX (1-based). Returns NULL if INDEX
8c43009f 1271 is out of bounds. */
ecfb656c 1272 const char *include_dir_at (dir_index index) const
8c43009f 1273 {
ecfb656c
PA
1274 /* Convert directory index number (1-based) to vector index
1275 (0-based). */
1276 size_t vec_index = to_underlying (index) - 1;
1277
1278 if (vec_index >= include_dirs.size ())
8c43009f 1279 return NULL;
ecfb656c 1280 return include_dirs[vec_index];
8c43009f
PA
1281 }
1282
ecfb656c 1283 /* Return the file name at INDEX (1-based). Returns NULL if INDEX
8c43009f 1284 is out of bounds. */
ecfb656c 1285 file_entry *file_name_at (file_name_index index)
8c43009f 1286 {
ecfb656c
PA
1287 /* Convert file name index number (1-based) to vector index
1288 (0-based). */
1289 size_t vec_index = to_underlying (index) - 1;
1290
1291 if (vec_index >= file_names.size ())
fff8551c 1292 return NULL;
ecfb656c 1293 return &file_names[vec_index];
fff8551c
PA
1294 }
1295
1296 /* Const version of the above. */
1297 const file_entry *file_name_at (unsigned int index) const
1298 {
1299 if (index >= file_names.size ())
8c43009f
PA
1300 return NULL;
1301 return &file_names[index];
1302 }
1303
527f3840 1304 /* Offset of line number information in .debug_line section. */
9c541725 1305 sect_offset sect_off {};
527f3840
JK
1306
1307 /* OFFSET is for struct dwz_file associated with dwarf2_per_objfile. */
fff8551c
PA
1308 unsigned offset_in_dwz : 1; /* Can't initialize bitfields in-class. */
1309
1310 unsigned int total_length {};
1311 unsigned short version {};
1312 unsigned int header_length {};
1313 unsigned char minimum_instruction_length {};
1314 unsigned char maximum_ops_per_instruction {};
1315 unsigned char default_is_stmt {};
1316 int line_base {};
1317 unsigned char line_range {};
1318 unsigned char opcode_base {};
debd256d
JB
1319
1320 /* standard_opcode_lengths[i] is the number of operands for the
1321 standard opcode whose value is i. This means that
1322 standard_opcode_lengths[0] is unused, and the last meaningful
1323 element is standard_opcode_lengths[opcode_base - 1]. */
fff8551c 1324 std::unique_ptr<unsigned char[]> standard_opcode_lengths;
debd256d 1325
fff8551c
PA
1326 /* The include_directories table. Note these are observing
1327 pointers. The memory is owned by debug_line_buffer. */
1328 std::vector<const char *> include_dirs;
debd256d 1329
fff8551c
PA
1330 /* The file_names table. */
1331 std::vector<file_entry> file_names;
debd256d
JB
1332
1333 /* The start and end of the statement program following this
6502dd73 1334 header. These point into dwarf2_per_objfile->line_buffer. */
fff8551c 1335 const gdb_byte *statement_program_start {}, *statement_program_end {};
debd256d 1336};
c906108c 1337
fff8551c
PA
1338typedef std::unique_ptr<line_header> line_header_up;
1339
8c43009f
PA
1340const char *
1341file_entry::include_dir (const line_header *lh) const
1342{
ecfb656c 1343 return lh->include_dir_at (d_index);
8c43009f
PA
1344}
1345
c906108c 1346/* When we construct a partial symbol table entry we only
0963b4bd 1347 need this much information. */
c906108c
SS
1348struct partial_die_info
1349 {
72bf9492 1350 /* Offset of this DIE. */
9c541725 1351 sect_offset sect_off;
72bf9492
DJ
1352
1353 /* DWARF-2 tag for this DIE. */
1354 ENUM_BITFIELD(dwarf_tag) tag : 16;
1355
72bf9492
DJ
1356 /* Assorted flags describing the data found in this DIE. */
1357 unsigned int has_children : 1;
1358 unsigned int is_external : 1;
1359 unsigned int is_declaration : 1;
1360 unsigned int has_type : 1;
1361 unsigned int has_specification : 1;
1362 unsigned int has_pc_info : 1;
481860b3 1363 unsigned int may_be_inlined : 1;
72bf9492 1364
0c1b455e
TT
1365 /* This DIE has been marked DW_AT_main_subprogram. */
1366 unsigned int main_subprogram : 1;
1367
72bf9492
DJ
1368 /* Flag set if the SCOPE field of this structure has been
1369 computed. */
1370 unsigned int scope_set : 1;
1371
fa4028e9
JB
1372 /* Flag set if the DIE has a byte_size attribute. */
1373 unsigned int has_byte_size : 1;
1374
ff908ebf
AW
1375 /* Flag set if the DIE has a DW_AT_const_value attribute. */
1376 unsigned int has_const_value : 1;
1377
98bfdba5
PA
1378 /* Flag set if any of the DIE's children are template arguments. */
1379 unsigned int has_template_arguments : 1;
1380
abc72ce4
DE
1381 /* Flag set if fixup_partial_die has been called on this die. */
1382 unsigned int fixup_called : 1;
1383
36586728
TT
1384 /* Flag set if DW_TAG_imported_unit uses DW_FORM_GNU_ref_alt. */
1385 unsigned int is_dwz : 1;
1386
1387 /* Flag set if spec_offset uses DW_FORM_GNU_ref_alt. */
1388 unsigned int spec_is_dwz : 1;
1389
72bf9492 1390 /* The name of this DIE. Normally the value of DW_AT_name, but
94af9270 1391 sometimes a default name for unnamed DIEs. */
15d034d0 1392 const char *name;
72bf9492 1393
abc72ce4
DE
1394 /* The linkage name, if present. */
1395 const char *linkage_name;
1396
72bf9492
DJ
1397 /* The scope to prepend to our children. This is generally
1398 allocated on the comp_unit_obstack, so will disappear
1399 when this compilation unit leaves the cache. */
15d034d0 1400 const char *scope;
72bf9492 1401
95554aad
TT
1402 /* Some data associated with the partial DIE. The tag determines
1403 which field is live. */
1404 union
1405 {
1406 /* The location description associated with this DIE, if any. */
1407 struct dwarf_block *locdesc;
1408 /* The offset of an import, for DW_TAG_imported_unit. */
9c541725 1409 sect_offset sect_off;
95554aad 1410 } d;
72bf9492
DJ
1411
1412 /* If HAS_PC_INFO, the PC range associated with this DIE. */
c906108c
SS
1413 CORE_ADDR lowpc;
1414 CORE_ADDR highpc;
72bf9492 1415
93311388 1416 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
72bf9492 1417 DW_AT_sibling, if any. */
abc72ce4
DE
1418 /* NOTE: This member isn't strictly necessary, read_partial_die could
1419 return DW_AT_sibling values to its caller load_partial_dies. */
d521ce57 1420 const gdb_byte *sibling;
72bf9492
DJ
1421
1422 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
1423 DW_AT_specification (or DW_AT_abstract_origin or
1424 DW_AT_extension). */
b64f50a1 1425 sect_offset spec_offset;
72bf9492
DJ
1426
1427 /* Pointers to this DIE's parent, first child, and next sibling,
1428 if any. */
1429 struct partial_die_info *die_parent, *die_child, *die_sibling;
c906108c
SS
1430 };
1431
0963b4bd 1432/* This data structure holds the information of an abbrev. */
c906108c
SS
1433struct abbrev_info
1434 {
1435 unsigned int number; /* number identifying abbrev */
1436 enum dwarf_tag tag; /* dwarf tag */
f3dd6933
DJ
1437 unsigned short has_children; /* boolean */
1438 unsigned short num_attrs; /* number of attributes */
c906108c
SS
1439 struct attr_abbrev *attrs; /* an array of attribute descriptions */
1440 struct abbrev_info *next; /* next in chain */
1441 };
1442
1443struct attr_abbrev
1444 {
9d25dd43
DE
1445 ENUM_BITFIELD(dwarf_attribute) name : 16;
1446 ENUM_BITFIELD(dwarf_form) form : 16;
43988095
JK
1447
1448 /* It is valid only if FORM is DW_FORM_implicit_const. */
1449 LONGEST implicit_const;
c906108c
SS
1450 };
1451
433df2d4
DE
1452/* Size of abbrev_table.abbrev_hash_table. */
1453#define ABBREV_HASH_SIZE 121
1454
1455/* Top level data structure to contain an abbreviation table. */
1456
1457struct abbrev_table
1458{
f4dc4d17
DE
1459 /* Where the abbrev table came from.
1460 This is used as a sanity check when the table is used. */
9c541725 1461 sect_offset sect_off;
433df2d4
DE
1462
1463 /* Storage for the abbrev table. */
1464 struct obstack abbrev_obstack;
1465
1466 /* Hash table of abbrevs.
1467 This is an array of size ABBREV_HASH_SIZE allocated in abbrev_obstack.
1468 It could be statically allocated, but the previous code didn't so we
1469 don't either. */
1470 struct abbrev_info **abbrevs;
1471};
1472
0963b4bd 1473/* Attributes have a name and a value. */
b60c80d6
DJ
1474struct attribute
1475 {
9d25dd43 1476 ENUM_BITFIELD(dwarf_attribute) name : 16;
8285870a
JK
1477 ENUM_BITFIELD(dwarf_form) form : 15;
1478
1479 /* Has DW_STRING already been updated by dwarf2_canonicalize_name? This
1480 field should be in u.str (existing only for DW_STRING) but it is kept
1481 here for better struct attribute alignment. */
1482 unsigned int string_is_canonical : 1;
1483
b60c80d6
DJ
1484 union
1485 {
15d034d0 1486 const char *str;
b60c80d6 1487 struct dwarf_block *blk;
43bbcdc2
PH
1488 ULONGEST unsnd;
1489 LONGEST snd;
b60c80d6 1490 CORE_ADDR addr;
ac9ec31b 1491 ULONGEST signature;
b60c80d6
DJ
1492 }
1493 u;
1494 };
1495
0963b4bd 1496/* This data structure holds a complete die structure. */
c906108c
SS
1497struct die_info
1498 {
76815b17
DE
1499 /* DWARF-2 tag for this DIE. */
1500 ENUM_BITFIELD(dwarf_tag) tag : 16;
1501
1502 /* Number of attributes */
98bfdba5
PA
1503 unsigned char num_attrs;
1504
1505 /* True if we're presently building the full type name for the
1506 type derived from this DIE. */
1507 unsigned char building_fullname : 1;
76815b17 1508
adde2bff
DE
1509 /* True if this die is in process. PR 16581. */
1510 unsigned char in_process : 1;
1511
76815b17
DE
1512 /* Abbrev number */
1513 unsigned int abbrev;
1514
93311388 1515 /* Offset in .debug_info or .debug_types section. */
9c541725 1516 sect_offset sect_off;
78ba4af6
JB
1517
1518 /* The dies in a compilation unit form an n-ary tree. PARENT
1519 points to this die's parent; CHILD points to the first child of
1520 this node; and all the children of a given node are chained
4950bc1c 1521 together via their SIBLING fields. */
639d11d3
DC
1522 struct die_info *child; /* Its first child, if any. */
1523 struct die_info *sibling; /* Its next sibling, if any. */
1524 struct die_info *parent; /* Its parent, if any. */
c906108c 1525
b60c80d6
DJ
1526 /* An array of attributes, with NUM_ATTRS elements. There may be
1527 zero, but it's not common and zero-sized arrays are not
1528 sufficiently portable C. */
1529 struct attribute attrs[1];
c906108c
SS
1530 };
1531
0963b4bd 1532/* Get at parts of an attribute structure. */
c906108c
SS
1533
1534#define DW_STRING(attr) ((attr)->u.str)
8285870a 1535#define DW_STRING_IS_CANONICAL(attr) ((attr)->string_is_canonical)
c906108c
SS
1536#define DW_UNSND(attr) ((attr)->u.unsnd)
1537#define DW_BLOCK(attr) ((attr)->u.blk)
1538#define DW_SND(attr) ((attr)->u.snd)
1539#define DW_ADDR(attr) ((attr)->u.addr)
ac9ec31b 1540#define DW_SIGNATURE(attr) ((attr)->u.signature)
c906108c 1541
0963b4bd 1542/* Blocks are a bunch of untyped bytes. */
c906108c
SS
1543struct dwarf_block
1544 {
56eb65bd 1545 size_t size;
1d6edc3c
JK
1546
1547 /* Valid only if SIZE is not zero. */
d521ce57 1548 const gdb_byte *data;
c906108c
SS
1549 };
1550
c906108c
SS
1551#ifndef ATTR_ALLOC_CHUNK
1552#define ATTR_ALLOC_CHUNK 4
1553#endif
1554
c906108c
SS
1555/* Allocate fields for structs, unions and enums in this size. */
1556#ifndef DW_FIELD_ALLOC_CHUNK
1557#define DW_FIELD_ALLOC_CHUNK 4
1558#endif
1559
c906108c
SS
1560/* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
1561 but this would require a corresponding change in unpack_field_as_long
1562 and friends. */
1563static int bits_per_byte = 8;
1564
52059ffd
TT
1565struct nextfield
1566{
1567 struct nextfield *next;
1568 int accessibility;
1569 int virtuality;
1570 struct field field;
1571};
1572
1573struct nextfnfield
1574{
1575 struct nextfnfield *next;
1576 struct fn_field fnfield;
1577};
1578
1579struct fnfieldlist
1580{
1581 const char *name;
1582 int length;
1583 struct nextfnfield *head;
1584};
1585
883fd55a 1586struct decl_field_list
52059ffd 1587{
883fd55a
KS
1588 struct decl_field field;
1589 struct decl_field_list *next;
52059ffd
TT
1590};
1591
c906108c
SS
1592/* The routines that read and process dies for a C struct or C++ class
1593 pass lists of data member fields and lists of member function fields
1594 in an instance of a field_info structure, as defined below. */
1595struct field_info
c5aa993b 1596 {
0963b4bd 1597 /* List of data member and baseclasses fields. */
52059ffd 1598 struct nextfield *fields, *baseclasses;
c906108c 1599
7d0ccb61 1600 /* Number of fields (including baseclasses). */
c5aa993b 1601 int nfields;
c906108c 1602
c5aa993b
JM
1603 /* Number of baseclasses. */
1604 int nbaseclasses;
c906108c 1605
c5aa993b
JM
1606 /* Set if the accesibility of one of the fields is not public. */
1607 int non_public_fields;
c906108c 1608
c5aa993b
JM
1609 /* Member function fieldlist array, contains name of possibly overloaded
1610 member function, number of overloaded member functions and a pointer
1611 to the head of the member function field chain. */
52059ffd 1612 struct fnfieldlist *fnfieldlists;
c906108c 1613
c5aa993b
JM
1614 /* Number of entries in the fnfieldlists array. */
1615 int nfnfields;
98751a41
JK
1616
1617 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
1618 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
883fd55a 1619 struct decl_field_list *typedef_field_list;
98751a41 1620 unsigned typedef_field_list_count;
883fd55a
KS
1621
1622 /* Nested types defined by this class and the number of elements in this
1623 list. */
1624 struct decl_field_list *nested_types_list;
1625 unsigned nested_types_list_count;
c5aa993b 1626 };
c906108c 1627
10b3939b
DJ
1628/* One item on the queue of compilation units to read in full symbols
1629 for. */
1630struct dwarf2_queue_item
1631{
1632 struct dwarf2_per_cu_data *per_cu;
95554aad 1633 enum language pretend_language;
10b3939b
DJ
1634 struct dwarf2_queue_item *next;
1635};
1636
1637/* The current queue. */
1638static struct dwarf2_queue_item *dwarf2_queue, *dwarf2_queue_tail;
1639
ae038cb0
DJ
1640/* Loaded secondary compilation units are kept in memory until they
1641 have not been referenced for the processing of this many
1642 compilation units. Set this to zero to disable caching. Cache
1643 sizes of up to at least twenty will improve startup time for
1644 typical inter-CU-reference binaries, at an obvious memory cost. */
b4f54984 1645static int dwarf_max_cache_age = 5;
920d2a44 1646static void
b4f54984
DE
1647show_dwarf_max_cache_age (struct ui_file *file, int from_tty,
1648 struct cmd_list_element *c, const char *value)
920d2a44 1649{
3e43a32a 1650 fprintf_filtered (file, _("The upper bound on the age of cached "
b4f54984 1651 "DWARF compilation units is %s.\n"),
920d2a44
AC
1652 value);
1653}
4390d890 1654\f
c906108c
SS
1655/* local function prototypes */
1656
a32a8923
DE
1657static const char *get_section_name (const struct dwarf2_section_info *);
1658
1659static const char *get_section_file_name (const struct dwarf2_section_info *);
1660
918dd910
JK
1661static void dwarf2_find_base_address (struct die_info *die,
1662 struct dwarf2_cu *cu);
1663
0018ea6f
DE
1664static struct partial_symtab *create_partial_symtab
1665 (struct dwarf2_per_cu_data *per_cu, const char *name);
1666
f1902523
JK
1667static void build_type_psymtabs_reader (const struct die_reader_specs *reader,
1668 const gdb_byte *info_ptr,
1669 struct die_info *type_unit_die,
1670 int has_children, void *data);
1671
c67a9c90 1672static void dwarf2_build_psymtabs_hard (struct objfile *);
c906108c 1673
72bf9492
DJ
1674static void scan_partial_symbols (struct partial_die_info *,
1675 CORE_ADDR *, CORE_ADDR *,
5734ee8b 1676 int, struct dwarf2_cu *);
c906108c 1677
72bf9492
DJ
1678static void add_partial_symbol (struct partial_die_info *,
1679 struct dwarf2_cu *);
63d06c5c 1680
72bf9492
DJ
1681static void add_partial_namespace (struct partial_die_info *pdi,
1682 CORE_ADDR *lowpc, CORE_ADDR *highpc,
cdc07690 1683 int set_addrmap, struct dwarf2_cu *cu);
63d06c5c 1684
5d7cb8df 1685static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
cdc07690 1686 CORE_ADDR *highpc, int set_addrmap,
5d7cb8df
JK
1687 struct dwarf2_cu *cu);
1688
72bf9492
DJ
1689static void add_partial_enumeration (struct partial_die_info *enum_pdi,
1690 struct dwarf2_cu *cu);
91c24f0a 1691
bc30ff58
JB
1692static void add_partial_subprogram (struct partial_die_info *pdi,
1693 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 1694 int need_pc, struct dwarf2_cu *cu);
bc30ff58 1695
257e7a09
YQ
1696static void dwarf2_read_symtab (struct partial_symtab *,
1697 struct objfile *);
c906108c 1698
a14ed312 1699static void psymtab_to_symtab_1 (struct partial_symtab *);
c906108c 1700
433df2d4
DE
1701static struct abbrev_info *abbrev_table_lookup_abbrev
1702 (const struct abbrev_table *, unsigned int);
1703
1704static struct abbrev_table *abbrev_table_read_table
1705 (struct dwarf2_section_info *, sect_offset);
1706
1707static void abbrev_table_free (struct abbrev_table *);
1708
f4dc4d17
DE
1709static void abbrev_table_free_cleanup (void *);
1710
dee91e82
DE
1711static void dwarf2_read_abbrevs (struct dwarf2_cu *,
1712 struct dwarf2_section_info *);
c906108c 1713
f3dd6933 1714static void dwarf2_free_abbrev_table (void *);
c906108c 1715
d521ce57 1716static unsigned int peek_abbrev_code (bfd *, const gdb_byte *);
6caca83c 1717
dee91e82 1718static struct partial_die_info *load_partial_dies
d521ce57 1719 (const struct die_reader_specs *, const gdb_byte *, int);
72bf9492 1720
d521ce57
TT
1721static const gdb_byte *read_partial_die (const struct die_reader_specs *,
1722 struct partial_die_info *,
1723 struct abbrev_info *,
1724 unsigned int,
1725 const gdb_byte *);
c906108c 1726
36586728 1727static struct partial_die_info *find_partial_die (sect_offset, int,
10b3939b 1728 struct dwarf2_cu *);
72bf9492
DJ
1729
1730static void fixup_partial_die (struct partial_die_info *,
1731 struct dwarf2_cu *);
1732
d521ce57
TT
1733static const gdb_byte *read_attribute (const struct die_reader_specs *,
1734 struct attribute *, struct attr_abbrev *,
1735 const gdb_byte *);
a8329558 1736
a1855c1d 1737static unsigned int read_1_byte (bfd *, const gdb_byte *);
c906108c 1738
a1855c1d 1739static int read_1_signed_byte (bfd *, const gdb_byte *);
c906108c 1740
a1855c1d 1741static unsigned int read_2_bytes (bfd *, const gdb_byte *);
c906108c 1742
a1855c1d 1743static unsigned int read_4_bytes (bfd *, const gdb_byte *);
c906108c 1744
a1855c1d 1745static ULONGEST read_8_bytes (bfd *, const gdb_byte *);
c906108c 1746
d521ce57 1747static CORE_ADDR read_address (bfd *, const gdb_byte *ptr, struct dwarf2_cu *,
891d2f0b 1748 unsigned int *);
c906108c 1749
d521ce57 1750static LONGEST read_initial_length (bfd *, const gdb_byte *, unsigned int *);
c764a876
DE
1751
1752static LONGEST read_checked_initial_length_and_offset
d521ce57 1753 (bfd *, const gdb_byte *, const struct comp_unit_head *,
c764a876 1754 unsigned int *, unsigned int *);
613e1657 1755
d521ce57
TT
1756static LONGEST read_offset (bfd *, const gdb_byte *,
1757 const struct comp_unit_head *,
c764a876
DE
1758 unsigned int *);
1759
d521ce57 1760static LONGEST read_offset_1 (bfd *, const gdb_byte *, unsigned int);
613e1657 1761
f4dc4d17
DE
1762static sect_offset read_abbrev_offset (struct dwarf2_section_info *,
1763 sect_offset);
1764
d521ce57 1765static const gdb_byte *read_n_bytes (bfd *, const gdb_byte *, unsigned int);
c906108c 1766
d521ce57 1767static const char *read_direct_string (bfd *, const gdb_byte *, unsigned int *);
c906108c 1768
d521ce57
TT
1769static const char *read_indirect_string (bfd *, const gdb_byte *,
1770 const struct comp_unit_head *,
1771 unsigned int *);
4bdf3d34 1772
43988095
JK
1773static const char *read_indirect_line_string (bfd *, const gdb_byte *,
1774 const struct comp_unit_head *,
1775 unsigned int *);
36586728 1776
927aa2e7
JK
1777static const char *read_indirect_string_at_offset (bfd *abfd,
1778 LONGEST str_offset);
1779
43988095 1780static const char *read_indirect_string_from_dwz (struct dwz_file *, LONGEST);
c906108c 1781
d521ce57 1782static LONGEST read_signed_leb128 (bfd *, const gdb_byte *, unsigned int *);
c906108c 1783
d521ce57
TT
1784static CORE_ADDR read_addr_index_from_leb128 (struct dwarf2_cu *,
1785 const gdb_byte *,
3019eac3
DE
1786 unsigned int *);
1787
d521ce57 1788static const char *read_str_index (const struct die_reader_specs *reader,
342587c4 1789 ULONGEST str_index);
3019eac3 1790
e142c38c 1791static void set_cu_language (unsigned int, struct dwarf2_cu *);
c906108c 1792
e142c38c
DJ
1793static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
1794 struct dwarf2_cu *);
c906108c 1795
348e048f 1796static struct attribute *dwarf2_attr_no_follow (struct die_info *,
45e58e77 1797 unsigned int);
348e048f 1798
7d45c7c3
KB
1799static const char *dwarf2_string_attr (struct die_info *die, unsigned int name,
1800 struct dwarf2_cu *cu);
1801
05cf31d1
JB
1802static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
1803 struct dwarf2_cu *cu);
1804
e142c38c 1805static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
3ca72b44 1806
e142c38c 1807static struct die_info *die_specification (struct die_info *die,
f2f0e013 1808 struct dwarf2_cu **);
63d06c5c 1809
9c541725 1810static line_header_up dwarf_decode_line_header (sect_offset sect_off,
fff8551c 1811 struct dwarf2_cu *cu);
debd256d 1812
f3f5162e 1813static void dwarf_decode_lines (struct line_header *, const char *,
c3b7b696 1814 struct dwarf2_cu *, struct partial_symtab *,
527f3840 1815 CORE_ADDR, int decode_mapping);
c906108c 1816
4d663531 1817static void dwarf2_start_subfile (const char *, const char *);
c906108c 1818
43f3e411
DE
1819static struct compunit_symtab *dwarf2_start_symtab (struct dwarf2_cu *,
1820 const char *, const char *,
1821 CORE_ADDR);
f4dc4d17 1822
a14ed312 1823static struct symbol *new_symbol (struct die_info *, struct type *,
e7c27a73 1824 struct dwarf2_cu *);
c906108c 1825
34eaf542
TT
1826static struct symbol *new_symbol_full (struct die_info *, struct type *,
1827 struct dwarf2_cu *, struct symbol *);
1828
ff39bb5e 1829static void dwarf2_const_value (const struct attribute *, struct symbol *,
e7c27a73 1830 struct dwarf2_cu *);
c906108c 1831
ff39bb5e 1832static void dwarf2_const_value_attr (const struct attribute *attr,
98bfdba5
PA
1833 struct type *type,
1834 const char *name,
1835 struct obstack *obstack,
12df843f 1836 struct dwarf2_cu *cu, LONGEST *value,
d521ce57 1837 const gdb_byte **bytes,
98bfdba5 1838 struct dwarf2_locexpr_baton **baton);
2df3850c 1839
e7c27a73 1840static struct type *die_type (struct die_info *, struct dwarf2_cu *);
c906108c 1841
b4ba55a1
JB
1842static int need_gnat_info (struct dwarf2_cu *);
1843
3e43a32a
MS
1844static struct type *die_descriptive_type (struct die_info *,
1845 struct dwarf2_cu *);
b4ba55a1
JB
1846
1847static void set_descriptive_type (struct type *, struct die_info *,
1848 struct dwarf2_cu *);
1849
e7c27a73
DJ
1850static struct type *die_containing_type (struct die_info *,
1851 struct dwarf2_cu *);
c906108c 1852
ff39bb5e 1853static struct type *lookup_die_type (struct die_info *, const struct attribute *,
673bfd45 1854 struct dwarf2_cu *);
c906108c 1855
f792889a 1856static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
c906108c 1857
673bfd45
DE
1858static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1859
0d5cff50 1860static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
63d06c5c 1861
6e70227d 1862static char *typename_concat (struct obstack *obs, const char *prefix,
f55ee35c
JK
1863 const char *suffix, int physname,
1864 struct dwarf2_cu *cu);
63d06c5c 1865
e7c27a73 1866static void read_file_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1867
348e048f
DE
1868static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1869
e7c27a73 1870static void read_func_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1871
e7c27a73 1872static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1873
96408a79
SA
1874static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1875
71a3c369
TT
1876static void read_variable (struct die_info *die, struct dwarf2_cu *cu);
1877
ff013f42
JK
1878static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
1879 struct dwarf2_cu *, struct partial_symtab *);
1880
3a2b436a 1881/* How dwarf2_get_pc_bounds constructed its *LOWPC and *HIGHPC return
e385593e 1882 values. Keep the items ordered with increasing constraints compliance. */
3a2b436a
JK
1883enum pc_bounds_kind
1884{
e385593e 1885 /* No attribute DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges was found. */
3a2b436a
JK
1886 PC_BOUNDS_NOT_PRESENT,
1887
e385593e
JK
1888 /* Some of the attributes DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges
1889 were present but they do not form a valid range of PC addresses. */
1890 PC_BOUNDS_INVALID,
1891
3a2b436a
JK
1892 /* Discontiguous range was found - that is DW_AT_ranges was found. */
1893 PC_BOUNDS_RANGES,
1894
1895 /* Contiguous range was found - DW_AT_low_pc and DW_AT_high_pc were found. */
1896 PC_BOUNDS_HIGH_LOW,
1897};
1898
1899static enum pc_bounds_kind dwarf2_get_pc_bounds (struct die_info *,
1900 CORE_ADDR *, CORE_ADDR *,
1901 struct dwarf2_cu *,
1902 struct partial_symtab *);
c906108c 1903
fae299cd
DC
1904static void get_scope_pc_bounds (struct die_info *,
1905 CORE_ADDR *, CORE_ADDR *,
1906 struct dwarf2_cu *);
1907
801e3a5b
JB
1908static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1909 CORE_ADDR, struct dwarf2_cu *);
1910
a14ed312 1911static void dwarf2_add_field (struct field_info *, struct die_info *,
e7c27a73 1912 struct dwarf2_cu *);
c906108c 1913
a14ed312 1914static void dwarf2_attach_fields_to_type (struct field_info *,
e7c27a73 1915 struct type *, struct dwarf2_cu *);
c906108c 1916
a14ed312 1917static void dwarf2_add_member_fn (struct field_info *,
e26fb1d7 1918 struct die_info *, struct type *,
e7c27a73 1919 struct dwarf2_cu *);
c906108c 1920
a14ed312 1921static void dwarf2_attach_fn_fields_to_type (struct field_info *,
3e43a32a
MS
1922 struct type *,
1923 struct dwarf2_cu *);
c906108c 1924
134d01f1 1925static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1926
e7c27a73 1927static void read_common_block (struct die_info *, struct dwarf2_cu *);
c906108c 1928
e7c27a73 1929static void read_namespace (struct die_info *die, struct dwarf2_cu *);
d9fa45fe 1930
5d7cb8df
JK
1931static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1932
22cee43f
PMR
1933static struct using_direct **using_directives (enum language);
1934
27aa8d6a
SW
1935static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1936
74921315
KS
1937static int read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu);
1938
f55ee35c
JK
1939static struct type *read_module_type (struct die_info *die,
1940 struct dwarf2_cu *cu);
1941
38d518c9 1942static const char *namespace_name (struct die_info *die,
e142c38c 1943 int *is_anonymous, struct dwarf2_cu *);
38d518c9 1944
134d01f1 1945static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1946
e7c27a73 1947static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *);
c906108c 1948
6e70227d 1949static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
7ca2d3a3
DL
1950 struct dwarf2_cu *);
1951
bf6af496 1952static struct die_info *read_die_and_siblings_1
d521ce57 1953 (const struct die_reader_specs *, const gdb_byte *, const gdb_byte **,
bf6af496 1954 struct die_info *);
639d11d3 1955
dee91e82 1956static struct die_info *read_die_and_siblings (const struct die_reader_specs *,
d521ce57
TT
1957 const gdb_byte *info_ptr,
1958 const gdb_byte **new_info_ptr,
639d11d3
DC
1959 struct die_info *parent);
1960
d521ce57
TT
1961static const gdb_byte *read_full_die_1 (const struct die_reader_specs *,
1962 struct die_info **, const gdb_byte *,
1963 int *, int);
3019eac3 1964
d521ce57
TT
1965static const gdb_byte *read_full_die (const struct die_reader_specs *,
1966 struct die_info **, const gdb_byte *,
1967 int *);
93311388 1968
e7c27a73 1969static void process_die (struct die_info *, struct dwarf2_cu *);
c906108c 1970
15d034d0
TT
1971static const char *dwarf2_canonicalize_name (const char *, struct dwarf2_cu *,
1972 struct obstack *);
71c25dea 1973
15d034d0 1974static const char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
9219021c 1975
15d034d0 1976static const char *dwarf2_full_name (const char *name,
98bfdba5
PA
1977 struct die_info *die,
1978 struct dwarf2_cu *cu);
1979
ca69b9e6
DE
1980static const char *dwarf2_physname (const char *name, struct die_info *die,
1981 struct dwarf2_cu *cu);
1982
e142c38c 1983static struct die_info *dwarf2_extension (struct die_info *die,
f2f0e013 1984 struct dwarf2_cu **);
9219021c 1985
f39c6ffd 1986static const char *dwarf_tag_name (unsigned int);
c906108c 1987
f39c6ffd 1988static const char *dwarf_attr_name (unsigned int);
c906108c 1989
f39c6ffd 1990static const char *dwarf_form_name (unsigned int);
c906108c 1991
a121b7c1 1992static const char *dwarf_bool_name (unsigned int);
c906108c 1993
f39c6ffd 1994static const char *dwarf_type_encoding_name (unsigned int);
c906108c 1995
f9aca02d 1996static struct die_info *sibling_die (struct die_info *);
c906108c 1997
d97bc12b
DE
1998static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1999
2000static void dump_die_for_error (struct die_info *);
2001
2002static void dump_die_1 (struct ui_file *, int level, int max_level,
2003 struct die_info *);
c906108c 2004
d97bc12b 2005/*static*/ void dump_die (struct die_info *, int max_level);
c906108c 2006
51545339 2007static void store_in_ref_table (struct die_info *,
10b3939b 2008 struct dwarf2_cu *);
c906108c 2009
ff39bb5e 2010static sect_offset dwarf2_get_ref_die_offset (const struct attribute *);
c906108c 2011
ff39bb5e 2012static LONGEST dwarf2_get_attr_constant_value (const struct attribute *, int);
a02abb62 2013
348e048f 2014static struct die_info *follow_die_ref_or_sig (struct die_info *,
ff39bb5e 2015 const struct attribute *,
348e048f
DE
2016 struct dwarf2_cu **);
2017
10b3939b 2018static struct die_info *follow_die_ref (struct die_info *,
ff39bb5e 2019 const struct attribute *,
f2f0e013 2020 struct dwarf2_cu **);
c906108c 2021
348e048f 2022static struct die_info *follow_die_sig (struct die_info *,
ff39bb5e 2023 const struct attribute *,
348e048f
DE
2024 struct dwarf2_cu **);
2025
ac9ec31b
DE
2026static struct type *get_signatured_type (struct die_info *, ULONGEST,
2027 struct dwarf2_cu *);
2028
2029static struct type *get_DW_AT_signature_type (struct die_info *,
ff39bb5e 2030 const struct attribute *,
ac9ec31b
DE
2031 struct dwarf2_cu *);
2032
e5fe5e75 2033static void load_full_type_unit (struct dwarf2_per_cu_data *per_cu);
348e048f 2034
52dc124a 2035static void read_signatured_type (struct signatured_type *);
348e048f 2036
63e43d3a
PMR
2037static int attr_to_dynamic_prop (const struct attribute *attr,
2038 struct die_info *die, struct dwarf2_cu *cu,
2039 struct dynamic_prop *prop);
2040
c906108c
SS
2041/* memory allocation interface */
2042
7b5a2f43 2043static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
c906108c 2044
b60c80d6 2045static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
c906108c 2046
43f3e411 2047static void dwarf_decode_macros (struct dwarf2_cu *, unsigned int, int);
2e276125 2048
6e5a29e1 2049static int attr_form_is_block (const struct attribute *);
8e19ed76 2050
6e5a29e1 2051static int attr_form_is_section_offset (const struct attribute *);
3690dd37 2052
6e5a29e1 2053static int attr_form_is_constant (const struct attribute *);
3690dd37 2054
6e5a29e1 2055static int attr_form_is_ref (const struct attribute *);
7771576e 2056
8cf6f0b1
TT
2057static void fill_in_loclist_baton (struct dwarf2_cu *cu,
2058 struct dwarf2_loclist_baton *baton,
ff39bb5e 2059 const struct attribute *attr);
8cf6f0b1 2060
ff39bb5e 2061static void dwarf2_symbol_mark_computed (const struct attribute *attr,
93e7bd98 2062 struct symbol *sym,
f1e6e072
TT
2063 struct dwarf2_cu *cu,
2064 int is_block);
4c2df51b 2065
d521ce57
TT
2066static const gdb_byte *skip_one_die (const struct die_reader_specs *reader,
2067 const gdb_byte *info_ptr,
2068 struct abbrev_info *abbrev);
4bb7a0a7 2069
72bf9492
DJ
2070static void free_stack_comp_unit (void *);
2071
72bf9492
DJ
2072static hashval_t partial_die_hash (const void *item);
2073
2074static int partial_die_eq (const void *item_lhs, const void *item_rhs);
2075
ae038cb0 2076static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
9c541725 2077 (sect_offset sect_off, unsigned int offset_in_dwz, struct objfile *objfile);
ae038cb0 2078
9816fde3 2079static void init_one_comp_unit (struct dwarf2_cu *cu,
23745b47 2080 struct dwarf2_per_cu_data *per_cu);
9816fde3
JK
2081
2082static void prepare_one_comp_unit (struct dwarf2_cu *cu,
95554aad
TT
2083 struct die_info *comp_unit_die,
2084 enum language pretend_language);
93311388 2085
68dc6402 2086static void free_heap_comp_unit (void *);
ae038cb0
DJ
2087
2088static void free_cached_comp_units (void *);
2089
2090static void age_cached_comp_units (void);
2091
dee91e82 2092static void free_one_cached_comp_unit (struct dwarf2_per_cu_data *);
ae038cb0 2093
f792889a
DJ
2094static struct type *set_die_type (struct die_info *, struct type *,
2095 struct dwarf2_cu *);
1c379e20 2096
ae038cb0
DJ
2097static void create_all_comp_units (struct objfile *);
2098
0e50663e 2099static int create_all_type_units (struct objfile *);
1fd400ff 2100
95554aad
TT
2101static void load_full_comp_unit (struct dwarf2_per_cu_data *,
2102 enum language);
10b3939b 2103
95554aad
TT
2104static void process_full_comp_unit (struct dwarf2_per_cu_data *,
2105 enum language);
10b3939b 2106
f4dc4d17
DE
2107static void process_full_type_unit (struct dwarf2_per_cu_data *,
2108 enum language);
2109
10b3939b
DJ
2110static void dwarf2_add_dependence (struct dwarf2_cu *,
2111 struct dwarf2_per_cu_data *);
2112
ae038cb0
DJ
2113static void dwarf2_mark (struct dwarf2_cu *);
2114
2115static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
2116
b64f50a1 2117static struct type *get_die_type_at_offset (sect_offset,
ac9ec31b 2118 struct dwarf2_per_cu_data *);
673bfd45 2119
f792889a 2120static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
72019c9c 2121
9291a0cd
TT
2122static void dwarf2_release_queue (void *dummy);
2123
95554aad
TT
2124static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
2125 enum language pretend_language);
2126
a0f42c21 2127static void process_queue (void);
9291a0cd 2128
d721ba37
PA
2129/* The return type of find_file_and_directory. Note, the enclosed
2130 string pointers are only valid while this object is valid. */
2131
2132struct file_and_directory
2133{
2134 /* The filename. This is never NULL. */
2135 const char *name;
2136
2137 /* The compilation directory. NULL if not known. If we needed to
2138 compute a new string, this points to COMP_DIR_STORAGE, otherwise,
2139 points directly to the DW_AT_comp_dir string attribute owned by
2140 the obstack that owns the DIE. */
2141 const char *comp_dir;
2142
2143 /* If we needed to build a new string for comp_dir, this is what
2144 owns the storage. */
2145 std::string comp_dir_storage;
2146};
2147
2148static file_and_directory find_file_and_directory (struct die_info *die,
2149 struct dwarf2_cu *cu);
9291a0cd
TT
2150
2151static char *file_full_name (int file, struct line_header *lh,
2152 const char *comp_dir);
2153
43988095
JK
2154/* Expected enum dwarf_unit_type for read_comp_unit_head. */
2155enum class rcuh_kind { COMPILE, TYPE };
2156
d521ce57 2157static const gdb_byte *read_and_check_comp_unit_head
36586728
TT
2158 (struct comp_unit_head *header,
2159 struct dwarf2_section_info *section,
d521ce57 2160 struct dwarf2_section_info *abbrev_section, const gdb_byte *info_ptr,
43988095 2161 rcuh_kind section_kind);
36586728 2162
fd820528 2163static void init_cutu_and_read_dies
f4dc4d17
DE
2164 (struct dwarf2_per_cu_data *this_cu, struct abbrev_table *abbrev_table,
2165 int use_existing_cu, int keep,
3019eac3
DE
2166 die_reader_func_ftype *die_reader_func, void *data);
2167
dee91e82
DE
2168static void init_cutu_and_read_dies_simple
2169 (struct dwarf2_per_cu_data *this_cu,
2170 die_reader_func_ftype *die_reader_func, void *data);
9291a0cd 2171
673bfd45 2172static htab_t allocate_signatured_type_table (struct objfile *objfile);
1fd400ff 2173
3019eac3
DE
2174static htab_t allocate_dwo_unit_table (struct objfile *objfile);
2175
57d63ce2
DE
2176static struct dwo_unit *lookup_dwo_unit_in_dwp
2177 (struct dwp_file *dwp_file, const char *comp_dir,
2178 ULONGEST signature, int is_debug_types);
a2ce51a0
DE
2179
2180static struct dwp_file *get_dwp_file (void);
2181
3019eac3 2182static struct dwo_unit *lookup_dwo_comp_unit
a1855c1d 2183 (struct dwarf2_per_cu_data *, const char *, const char *, ULONGEST);
3019eac3
DE
2184
2185static struct dwo_unit *lookup_dwo_type_unit
a1855c1d 2186 (struct signatured_type *, const char *, const char *);
3019eac3 2187
89e63ee4
DE
2188static void queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *);
2189
3019eac3
DE
2190static void free_dwo_file_cleanup (void *);
2191
95554aad
TT
2192static void process_cu_includes (void);
2193
1b80a9fa 2194static void check_producer (struct dwarf2_cu *cu);
527f3840
JK
2195
2196static void free_line_header_voidp (void *arg);
4390d890
DE
2197\f
2198/* Various complaints about symbol reading that don't abort the process. */
2199
2200static void
2201dwarf2_statement_list_fits_in_line_number_section_complaint (void)
2202{
2203 complaint (&symfile_complaints,
2204 _("statement list doesn't fit in .debug_line section"));
2205}
2206
2207static void
2208dwarf2_debug_line_missing_file_complaint (void)
2209{
2210 complaint (&symfile_complaints,
2211 _(".debug_line section has line data without a file"));
2212}
2213
2214static void
2215dwarf2_debug_line_missing_end_sequence_complaint (void)
2216{
2217 complaint (&symfile_complaints,
2218 _(".debug_line section has line "
2219 "program sequence without an end"));
2220}
2221
2222static void
2223dwarf2_complex_location_expr_complaint (void)
2224{
2225 complaint (&symfile_complaints, _("location expression too complex"));
2226}
2227
2228static void
2229dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
2230 int arg3)
2231{
2232 complaint (&symfile_complaints,
2233 _("const value length mismatch for '%s', got %d, expected %d"),
2234 arg1, arg2, arg3);
2235}
2236
2237static void
2238dwarf2_section_buffer_overflow_complaint (struct dwarf2_section_info *section)
2239{
2240 complaint (&symfile_complaints,
2241 _("debug info runs off end of %s section"
2242 " [in module %s]"),
a32a8923
DE
2243 get_section_name (section),
2244 get_section_file_name (section));
4390d890 2245}
1b80a9fa 2246
4390d890
DE
2247static void
2248dwarf2_macro_malformed_definition_complaint (const char *arg1)
2249{
2250 complaint (&symfile_complaints,
2251 _("macro debug info contains a "
2252 "malformed macro definition:\n`%s'"),
2253 arg1);
2254}
2255
2256static void
2257dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
2258{
2259 complaint (&symfile_complaints,
2260 _("invalid attribute class or form for '%s' in '%s'"),
2261 arg1, arg2);
2262}
527f3840
JK
2263
2264/* Hash function for line_header_hash. */
2265
2266static hashval_t
2267line_header_hash (const struct line_header *ofs)
2268{
9c541725 2269 return to_underlying (ofs->sect_off) ^ ofs->offset_in_dwz;
527f3840
JK
2270}
2271
2272/* Hash function for htab_create_alloc_ex for line_header_hash. */
2273
2274static hashval_t
2275line_header_hash_voidp (const void *item)
2276{
9a3c8263 2277 const struct line_header *ofs = (const struct line_header *) item;
527f3840
JK
2278
2279 return line_header_hash (ofs);
2280}
2281
2282/* Equality function for line_header_hash. */
2283
2284static int
2285line_header_eq_voidp (const void *item_lhs, const void *item_rhs)
2286{
9a3c8263
SM
2287 const struct line_header *ofs_lhs = (const struct line_header *) item_lhs;
2288 const struct line_header *ofs_rhs = (const struct line_header *) item_rhs;
527f3840 2289
9c541725 2290 return (ofs_lhs->sect_off == ofs_rhs->sect_off
527f3840
JK
2291 && ofs_lhs->offset_in_dwz == ofs_rhs->offset_in_dwz);
2292}
2293
4390d890 2294\f
9291a0cd 2295
31aa7e4e
JB
2296/* Read the given attribute value as an address, taking the attribute's
2297 form into account. */
2298
2299static CORE_ADDR
2300attr_value_as_address (struct attribute *attr)
2301{
2302 CORE_ADDR addr;
2303
2304 if (attr->form != DW_FORM_addr && attr->form != DW_FORM_GNU_addr_index)
2305 {
2306 /* Aside from a few clearly defined exceptions, attributes that
2307 contain an address must always be in DW_FORM_addr form.
2308 Unfortunately, some compilers happen to be violating this
2309 requirement by encoding addresses using other forms, such
2310 as DW_FORM_data4 for example. For those broken compilers,
2311 we try to do our best, without any guarantee of success,
2312 to interpret the address correctly. It would also be nice
2313 to generate a complaint, but that would require us to maintain
2314 a list of legitimate cases where a non-address form is allowed,
2315 as well as update callers to pass in at least the CU's DWARF
2316 version. This is more overhead than what we're willing to
2317 expand for a pretty rare case. */
2318 addr = DW_UNSND (attr);
2319 }
2320 else
2321 addr = DW_ADDR (attr);
2322
2323 return addr;
2324}
2325
9291a0cd 2326/* The suffix for an index file. */
437afbb8
JK
2327#define INDEX4_SUFFIX ".gdb-index"
2328#define INDEX5_SUFFIX ".debug_names"
2329#define DEBUG_STR_SUFFIX ".debug_str"
9291a0cd 2330
330cdd98
PA
2331/* See declaration. */
2332
2333dwarf2_per_objfile::dwarf2_per_objfile (struct objfile *objfile_,
2334 const dwarf2_debug_sections *names)
2335 : objfile (objfile_)
2336{
2337 if (names == NULL)
2338 names = &dwarf2_elf_names;
2339
2340 bfd *obfd = objfile->obfd;
2341
2342 for (asection *sec = obfd->sections; sec != NULL; sec = sec->next)
2343 locate_sections (obfd, sec, *names);
2344}
2345
2346dwarf2_per_objfile::~dwarf2_per_objfile ()
2347{
2348 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
2349 free_cached_comp_units ();
2350
2351 if (quick_file_names_table)
2352 htab_delete (quick_file_names_table);
2353
2354 if (line_header_hash)
2355 htab_delete (line_header_hash);
2356
2357 /* Everything else should be on the objfile obstack. */
2358}
2359
2360/* See declaration. */
2361
2362void
2363dwarf2_per_objfile::free_cached_comp_units ()
2364{
2365 dwarf2_per_cu_data *per_cu = read_in_chain;
2366 dwarf2_per_cu_data **last_chain = &read_in_chain;
2367 while (per_cu != NULL)
2368 {
2369 dwarf2_per_cu_data *next_cu = per_cu->cu->read_in_chain;
2370
2371 free_heap_comp_unit (per_cu->cu);
2372 *last_chain = next_cu;
2373 per_cu = next_cu;
2374 }
2375}
2376
c906108c 2377/* Try to locate the sections we need for DWARF 2 debugging
251d32d9
TG
2378 information and return true if we have enough to do something.
2379 NAMES points to the dwarf2 section names, or is NULL if the standard
2380 ELF names are used. */
c906108c
SS
2381
2382int
251d32d9
TG
2383dwarf2_has_info (struct objfile *objfile,
2384 const struct dwarf2_debug_sections *names)
c906108c 2385{
97cbe998
SDJ
2386 if (objfile->flags & OBJF_READNEVER)
2387 return 0;
2388
9a3c8263
SM
2389 dwarf2_per_objfile = ((struct dwarf2_per_objfile *)
2390 objfile_data (objfile, dwarf2_objfile_data_key));
be391dca
TT
2391 if (!dwarf2_per_objfile)
2392 {
2393 /* Initialize per-objfile state. */
2394 struct dwarf2_per_objfile *data
8d749320 2395 = XOBNEW (&objfile->objfile_obstack, struct dwarf2_per_objfile);
9a619af0 2396
330cdd98
PA
2397 dwarf2_per_objfile = new (data) struct dwarf2_per_objfile (objfile, names);
2398 set_objfile_data (objfile, dwarf2_objfile_data_key, dwarf2_per_objfile);
be391dca 2399 }
73869dc2 2400 return (!dwarf2_per_objfile->info.is_virtual
049412e3 2401 && dwarf2_per_objfile->info.s.section != NULL
73869dc2 2402 && !dwarf2_per_objfile->abbrev.is_virtual
049412e3 2403 && dwarf2_per_objfile->abbrev.s.section != NULL);
73869dc2
DE
2404}
2405
2406/* Return the containing section of virtual section SECTION. */
2407
2408static struct dwarf2_section_info *
2409get_containing_section (const struct dwarf2_section_info *section)
2410{
2411 gdb_assert (section->is_virtual);
2412 return section->s.containing_section;
c906108c
SS
2413}
2414
a32a8923
DE
2415/* Return the bfd owner of SECTION. */
2416
2417static struct bfd *
2418get_section_bfd_owner (const struct dwarf2_section_info *section)
2419{
73869dc2
DE
2420 if (section->is_virtual)
2421 {
2422 section = get_containing_section (section);
2423 gdb_assert (!section->is_virtual);
2424 }
049412e3 2425 return section->s.section->owner;
a32a8923
DE
2426}
2427
2428/* Return the bfd section of SECTION.
2429 Returns NULL if the section is not present. */
2430
2431static asection *
2432get_section_bfd_section (const struct dwarf2_section_info *section)
2433{
73869dc2
DE
2434 if (section->is_virtual)
2435 {
2436 section = get_containing_section (section);
2437 gdb_assert (!section->is_virtual);
2438 }
049412e3 2439 return section->s.section;
a32a8923
DE
2440}
2441
2442/* Return the name of SECTION. */
2443
2444static const char *
2445get_section_name (const struct dwarf2_section_info *section)
2446{
2447 asection *sectp = get_section_bfd_section (section);
2448
2449 gdb_assert (sectp != NULL);
2450 return bfd_section_name (get_section_bfd_owner (section), sectp);
2451}
2452
2453/* Return the name of the file SECTION is in. */
2454
2455static const char *
2456get_section_file_name (const struct dwarf2_section_info *section)
2457{
2458 bfd *abfd = get_section_bfd_owner (section);
2459
2460 return bfd_get_filename (abfd);
2461}
2462
2463/* Return the id of SECTION.
2464 Returns 0 if SECTION doesn't exist. */
2465
2466static int
2467get_section_id (const struct dwarf2_section_info *section)
2468{
2469 asection *sectp = get_section_bfd_section (section);
2470
2471 if (sectp == NULL)
2472 return 0;
2473 return sectp->id;
2474}
2475
2476/* Return the flags of SECTION.
73869dc2 2477 SECTION (or containing section if this is a virtual section) must exist. */
a32a8923
DE
2478
2479static int
2480get_section_flags (const struct dwarf2_section_info *section)
2481{
2482 asection *sectp = get_section_bfd_section (section);
2483
2484 gdb_assert (sectp != NULL);
2485 return bfd_get_section_flags (sectp->owner, sectp);
2486}
2487
251d32d9
TG
2488/* When loading sections, we look either for uncompressed section or for
2489 compressed section names. */
233a11ab
CS
2490
2491static int
251d32d9
TG
2492section_is_p (const char *section_name,
2493 const struct dwarf2_section_names *names)
233a11ab 2494{
251d32d9
TG
2495 if (names->normal != NULL
2496 && strcmp (section_name, names->normal) == 0)
2497 return 1;
2498 if (names->compressed != NULL
2499 && strcmp (section_name, names->compressed) == 0)
2500 return 1;
2501 return 0;
233a11ab
CS
2502}
2503
330cdd98 2504/* See declaration. */
c906108c 2505
330cdd98
PA
2506void
2507dwarf2_per_objfile::locate_sections (bfd *abfd, asection *sectp,
2508 const dwarf2_debug_sections &names)
c906108c 2509{
dc7650b8 2510 flagword aflag = bfd_get_section_flags (abfd, sectp);
251d32d9 2511
dc7650b8
JK
2512 if ((aflag & SEC_HAS_CONTENTS) == 0)
2513 {
2514 }
330cdd98 2515 else if (section_is_p (sectp->name, &names.info))
c906108c 2516 {
330cdd98
PA
2517 this->info.s.section = sectp;
2518 this->info.size = bfd_get_section_size (sectp);
c906108c 2519 }
330cdd98 2520 else if (section_is_p (sectp->name, &names.abbrev))
c906108c 2521 {
330cdd98
PA
2522 this->abbrev.s.section = sectp;
2523 this->abbrev.size = bfd_get_section_size (sectp);
c906108c 2524 }
330cdd98 2525 else if (section_is_p (sectp->name, &names.line))
c906108c 2526 {
330cdd98
PA
2527 this->line.s.section = sectp;
2528 this->line.size = bfd_get_section_size (sectp);
c906108c 2529 }
330cdd98 2530 else if (section_is_p (sectp->name, &names.loc))
c906108c 2531 {
330cdd98
PA
2532 this->loc.s.section = sectp;
2533 this->loc.size = bfd_get_section_size (sectp);
c906108c 2534 }
330cdd98 2535 else if (section_is_p (sectp->name, &names.loclists))
43988095 2536 {
330cdd98
PA
2537 this->loclists.s.section = sectp;
2538 this->loclists.size = bfd_get_section_size (sectp);
43988095 2539 }
330cdd98 2540 else if (section_is_p (sectp->name, &names.macinfo))
c906108c 2541 {
330cdd98
PA
2542 this->macinfo.s.section = sectp;
2543 this->macinfo.size = bfd_get_section_size (sectp);
c906108c 2544 }
330cdd98 2545 else if (section_is_p (sectp->name, &names.macro))
cf2c3c16 2546 {
330cdd98
PA
2547 this->macro.s.section = sectp;
2548 this->macro.size = bfd_get_section_size (sectp);
cf2c3c16 2549 }
330cdd98 2550 else if (section_is_p (sectp->name, &names.str))
c906108c 2551 {
330cdd98
PA
2552 this->str.s.section = sectp;
2553 this->str.size = bfd_get_section_size (sectp);
c906108c 2554 }
330cdd98 2555 else if (section_is_p (sectp->name, &names.line_str))
43988095 2556 {
330cdd98
PA
2557 this->line_str.s.section = sectp;
2558 this->line_str.size = bfd_get_section_size (sectp);
43988095 2559 }
330cdd98 2560 else if (section_is_p (sectp->name, &names.addr))
3019eac3 2561 {
330cdd98
PA
2562 this->addr.s.section = sectp;
2563 this->addr.size = bfd_get_section_size (sectp);
3019eac3 2564 }
330cdd98 2565 else if (section_is_p (sectp->name, &names.frame))
b6af0555 2566 {
330cdd98
PA
2567 this->frame.s.section = sectp;
2568 this->frame.size = bfd_get_section_size (sectp);
b6af0555 2569 }
330cdd98 2570 else if (section_is_p (sectp->name, &names.eh_frame))
b6af0555 2571 {
330cdd98
PA
2572 this->eh_frame.s.section = sectp;
2573 this->eh_frame.size = bfd_get_section_size (sectp);
b6af0555 2574 }
330cdd98 2575 else if (section_is_p (sectp->name, &names.ranges))
af34e669 2576 {
330cdd98
PA
2577 this->ranges.s.section = sectp;
2578 this->ranges.size = bfd_get_section_size (sectp);
af34e669 2579 }
330cdd98 2580 else if (section_is_p (sectp->name, &names.rnglists))
43988095 2581 {
330cdd98
PA
2582 this->rnglists.s.section = sectp;
2583 this->rnglists.size = bfd_get_section_size (sectp);
43988095 2584 }
330cdd98 2585 else if (section_is_p (sectp->name, &names.types))
348e048f 2586 {
8b70b953
TT
2587 struct dwarf2_section_info type_section;
2588
2589 memset (&type_section, 0, sizeof (type_section));
049412e3 2590 type_section.s.section = sectp;
8b70b953
TT
2591 type_section.size = bfd_get_section_size (sectp);
2592
330cdd98 2593 VEC_safe_push (dwarf2_section_info_def, this->types,
8b70b953 2594 &type_section);
348e048f 2595 }
330cdd98 2596 else if (section_is_p (sectp->name, &names.gdb_index))
9291a0cd 2597 {
330cdd98
PA
2598 this->gdb_index.s.section = sectp;
2599 this->gdb_index.size = bfd_get_section_size (sectp);
9291a0cd 2600 }
927aa2e7
JK
2601 else if (section_is_p (sectp->name, &names.debug_names))
2602 {
2603 this->debug_names.s.section = sectp;
2604 this->debug_names.size = bfd_get_section_size (sectp);
2605 }
2606 else if (section_is_p (sectp->name, &names.debug_aranges))
2607 {
2608 this->debug_aranges.s.section = sectp;
2609 this->debug_aranges.size = bfd_get_section_size (sectp);
2610 }
dce234bc 2611
b4e1fd61 2612 if ((bfd_get_section_flags (abfd, sectp) & (SEC_LOAD | SEC_ALLOC))
72dca2f5 2613 && bfd_section_vma (abfd, sectp) == 0)
330cdd98 2614 this->has_section_at_zero = true;
c906108c
SS
2615}
2616
fceca515
DE
2617/* A helper function that decides whether a section is empty,
2618 or not present. */
9e0ac564
TT
2619
2620static int
19ac8c2e 2621dwarf2_section_empty_p (const struct dwarf2_section_info *section)
9e0ac564 2622{
73869dc2
DE
2623 if (section->is_virtual)
2624 return section->size == 0;
049412e3 2625 return section->s.section == NULL || section->size == 0;
9e0ac564
TT
2626}
2627
3019eac3
DE
2628/* Read the contents of the section INFO.
2629 OBJFILE is the main object file, but not necessarily the file where
a32a8923
DE
2630 the section comes from. E.g., for DWO files the bfd of INFO is the bfd
2631 of the DWO file.
dce234bc 2632 If the section is compressed, uncompress it before returning. */
c906108c 2633
dce234bc
PP
2634static void
2635dwarf2_read_section (struct objfile *objfile, struct dwarf2_section_info *info)
c906108c 2636{
a32a8923 2637 asection *sectp;
3019eac3 2638 bfd *abfd;
dce234bc 2639 gdb_byte *buf, *retbuf;
c906108c 2640
be391dca
TT
2641 if (info->readin)
2642 return;
dce234bc 2643 info->buffer = NULL;
be391dca 2644 info->readin = 1;
188dd5d6 2645
9e0ac564 2646 if (dwarf2_section_empty_p (info))
dce234bc 2647 return;
c906108c 2648
a32a8923 2649 sectp = get_section_bfd_section (info);
3019eac3 2650
73869dc2
DE
2651 /* If this is a virtual section we need to read in the real one first. */
2652 if (info->is_virtual)
2653 {
2654 struct dwarf2_section_info *containing_section =
2655 get_containing_section (info);
2656
2657 gdb_assert (sectp != NULL);
2658 if ((sectp->flags & SEC_RELOC) != 0)
2659 {
2660 error (_("Dwarf Error: DWP format V2 with relocations is not"
2661 " supported in section %s [in module %s]"),
2662 get_section_name (info), get_section_file_name (info));
2663 }
2664 dwarf2_read_section (objfile, containing_section);
2665 /* Other code should have already caught virtual sections that don't
2666 fit. */
2667 gdb_assert (info->virtual_offset + info->size
2668 <= containing_section->size);
2669 /* If the real section is empty or there was a problem reading the
2670 section we shouldn't get here. */
2671 gdb_assert (containing_section->buffer != NULL);
2672 info->buffer = containing_section->buffer + info->virtual_offset;
2673 return;
2674 }
2675
4bf44c1c
TT
2676 /* If the section has relocations, we must read it ourselves.
2677 Otherwise we attach it to the BFD. */
2678 if ((sectp->flags & SEC_RELOC) == 0)
dce234bc 2679 {
d521ce57 2680 info->buffer = gdb_bfd_map_section (sectp, &info->size);
4bf44c1c 2681 return;
dce234bc 2682 }
dce234bc 2683
224c3ddb 2684 buf = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, info->size);
4bf44c1c 2685 info->buffer = buf;
dce234bc
PP
2686
2687 /* When debugging .o files, we may need to apply relocations; see
2688 http://sourceware.org/ml/gdb-patches/2002-04/msg00136.html .
2689 We never compress sections in .o files, so we only need to
2690 try this when the section is not compressed. */
ac8035ab 2691 retbuf = symfile_relocate_debug_section (objfile, sectp, buf);
dce234bc
PP
2692 if (retbuf != NULL)
2693 {
2694 info->buffer = retbuf;
2695 return;
2696 }
2697
a32a8923
DE
2698 abfd = get_section_bfd_owner (info);
2699 gdb_assert (abfd != NULL);
2700
dce234bc
PP
2701 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
2702 || bfd_bread (buf, info->size, abfd) != info->size)
19ac8c2e
DE
2703 {
2704 error (_("Dwarf Error: Can't read DWARF data"
2705 " in section %s [in module %s]"),
2706 bfd_section_name (abfd, sectp), bfd_get_filename (abfd));
2707 }
dce234bc
PP
2708}
2709
9e0ac564
TT
2710/* A helper function that returns the size of a section in a safe way.
2711 If you are positive that the section has been read before using the
2712 size, then it is safe to refer to the dwarf2_section_info object's
2713 "size" field directly. In other cases, you must call this
2714 function, because for compressed sections the size field is not set
2715 correctly until the section has been read. */
2716
2717static bfd_size_type
2718dwarf2_section_size (struct objfile *objfile,
2719 struct dwarf2_section_info *info)
2720{
2721 if (!info->readin)
2722 dwarf2_read_section (objfile, info);
2723 return info->size;
2724}
2725
dce234bc 2726/* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
0963b4bd 2727 SECTION_NAME. */
af34e669 2728
dce234bc 2729void
3017a003
TG
2730dwarf2_get_section_info (struct objfile *objfile,
2731 enum dwarf2_section_enum sect,
d521ce57 2732 asection **sectp, const gdb_byte **bufp,
dce234bc
PP
2733 bfd_size_type *sizep)
2734{
2735 struct dwarf2_per_objfile *data
9a3c8263
SM
2736 = (struct dwarf2_per_objfile *) objfile_data (objfile,
2737 dwarf2_objfile_data_key);
dce234bc 2738 struct dwarf2_section_info *info;
a3b2a86b
TT
2739
2740 /* We may see an objfile without any DWARF, in which case we just
2741 return nothing. */
2742 if (data == NULL)
2743 {
2744 *sectp = NULL;
2745 *bufp = NULL;
2746 *sizep = 0;
2747 return;
2748 }
3017a003
TG
2749 switch (sect)
2750 {
2751 case DWARF2_DEBUG_FRAME:
2752 info = &data->frame;
2753 break;
2754 case DWARF2_EH_FRAME:
2755 info = &data->eh_frame;
2756 break;
2757 default:
2758 gdb_assert_not_reached ("unexpected section");
2759 }
dce234bc 2760
9e0ac564 2761 dwarf2_read_section (objfile, info);
dce234bc 2762
a32a8923 2763 *sectp = get_section_bfd_section (info);
dce234bc
PP
2764 *bufp = info->buffer;
2765 *sizep = info->size;
2766}
2767
36586728
TT
2768/* A helper function to find the sections for a .dwz file. */
2769
2770static void
2771locate_dwz_sections (bfd *abfd, asection *sectp, void *arg)
2772{
9a3c8263 2773 struct dwz_file *dwz_file = (struct dwz_file *) arg;
36586728
TT
2774
2775 /* Note that we only support the standard ELF names, because .dwz
2776 is ELF-only (at the time of writing). */
2777 if (section_is_p (sectp->name, &dwarf2_elf_names.abbrev))
2778 {
049412e3 2779 dwz_file->abbrev.s.section = sectp;
36586728
TT
2780 dwz_file->abbrev.size = bfd_get_section_size (sectp);
2781 }
2782 else if (section_is_p (sectp->name, &dwarf2_elf_names.info))
2783 {
049412e3 2784 dwz_file->info.s.section = sectp;
36586728
TT
2785 dwz_file->info.size = bfd_get_section_size (sectp);
2786 }
2787 else if (section_is_p (sectp->name, &dwarf2_elf_names.str))
2788 {
049412e3 2789 dwz_file->str.s.section = sectp;
36586728
TT
2790 dwz_file->str.size = bfd_get_section_size (sectp);
2791 }
2792 else if (section_is_p (sectp->name, &dwarf2_elf_names.line))
2793 {
049412e3 2794 dwz_file->line.s.section = sectp;
36586728
TT
2795 dwz_file->line.size = bfd_get_section_size (sectp);
2796 }
2797 else if (section_is_p (sectp->name, &dwarf2_elf_names.macro))
2798 {
049412e3 2799 dwz_file->macro.s.section = sectp;
36586728
TT
2800 dwz_file->macro.size = bfd_get_section_size (sectp);
2801 }
2ec9a5e0
TT
2802 else if (section_is_p (sectp->name, &dwarf2_elf_names.gdb_index))
2803 {
049412e3 2804 dwz_file->gdb_index.s.section = sectp;
2ec9a5e0
TT
2805 dwz_file->gdb_index.size = bfd_get_section_size (sectp);
2806 }
927aa2e7
JK
2807 else if (section_is_p (sectp->name, &dwarf2_elf_names.debug_names))
2808 {
2809 dwz_file->debug_names.s.section = sectp;
2810 dwz_file->debug_names.size = bfd_get_section_size (sectp);
2811 }
36586728
TT
2812}
2813
4db1a1dc
TT
2814/* Open the separate '.dwz' debug file, if needed. Return NULL if
2815 there is no .gnu_debugaltlink section in the file. Error if there
2816 is such a section but the file cannot be found. */
36586728
TT
2817
2818static struct dwz_file *
2819dwarf2_get_dwz_file (void)
2820{
36586728
TT
2821 const char *filename;
2822 struct dwz_file *result;
acd13123 2823 bfd_size_type buildid_len_arg;
dc294be5
TT
2824 size_t buildid_len;
2825 bfd_byte *buildid;
36586728
TT
2826
2827 if (dwarf2_per_objfile->dwz_file != NULL)
2828 return dwarf2_per_objfile->dwz_file;
2829
4db1a1dc 2830 bfd_set_error (bfd_error_no_error);
791afaa2
TT
2831 gdb::unique_xmalloc_ptr<char> data
2832 (bfd_get_alt_debug_link_info (dwarf2_per_objfile->objfile->obfd,
2833 &buildid_len_arg, &buildid));
4db1a1dc
TT
2834 if (data == NULL)
2835 {
2836 if (bfd_get_error () == bfd_error_no_error)
2837 return NULL;
2838 error (_("could not read '.gnu_debugaltlink' section: %s"),
2839 bfd_errmsg (bfd_get_error ()));
2840 }
791afaa2
TT
2841
2842 gdb::unique_xmalloc_ptr<bfd_byte> buildid_holder (buildid);
36586728 2843
acd13123
TT
2844 buildid_len = (size_t) buildid_len_arg;
2845
791afaa2 2846 filename = data.get ();
d721ba37
PA
2847
2848 std::string abs_storage;
36586728
TT
2849 if (!IS_ABSOLUTE_PATH (filename))
2850 {
14278e1f
TT
2851 gdb::unique_xmalloc_ptr<char> abs
2852 = gdb_realpath (objfile_name (dwarf2_per_objfile->objfile));
36586728 2853
14278e1f 2854 abs_storage = ldirname (abs.get ()) + SLASH_STRING + filename;
d721ba37 2855 filename = abs_storage.c_str ();
36586728
TT
2856 }
2857
dc294be5
TT
2858 /* First try the file name given in the section. If that doesn't
2859 work, try to use the build-id instead. */
192b62ce 2860 gdb_bfd_ref_ptr dwz_bfd (gdb_bfd_open (filename, gnutarget, -1));
dc294be5 2861 if (dwz_bfd != NULL)
36586728 2862 {
192b62ce
TT
2863 if (!build_id_verify (dwz_bfd.get (), buildid_len, buildid))
2864 dwz_bfd.release ();
36586728
TT
2865 }
2866
dc294be5
TT
2867 if (dwz_bfd == NULL)
2868 dwz_bfd = build_id_to_debug_bfd (buildid_len, buildid);
2869
2870 if (dwz_bfd == NULL)
2871 error (_("could not find '.gnu_debugaltlink' file for %s"),
2872 objfile_name (dwarf2_per_objfile->objfile));
2873
36586728
TT
2874 result = OBSTACK_ZALLOC (&dwarf2_per_objfile->objfile->objfile_obstack,
2875 struct dwz_file);
192b62ce 2876 result->dwz_bfd = dwz_bfd.release ();
36586728 2877
192b62ce 2878 bfd_map_over_sections (result->dwz_bfd, locate_dwz_sections, result);
36586728 2879
192b62ce 2880 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd, result->dwz_bfd);
8d2cc612 2881 dwarf2_per_objfile->dwz_file = result;
36586728
TT
2882 return result;
2883}
9291a0cd 2884\f
7b9f3c50
DE
2885/* DWARF quick_symbols_functions support. */
2886
2887/* TUs can share .debug_line entries, and there can be a lot more TUs than
2888 unique line tables, so we maintain a separate table of all .debug_line
2889 derived entries to support the sharing.
2890 All the quick functions need is the list of file names. We discard the
2891 line_header when we're done and don't need to record it here. */
2892struct quick_file_names
2893{
094b34ac
DE
2894 /* The data used to construct the hash key. */
2895 struct stmt_list_hash hash;
7b9f3c50
DE
2896
2897 /* The number of entries in file_names, real_names. */
2898 unsigned int num_file_names;
2899
2900 /* The file names from the line table, after being run through
2901 file_full_name. */
2902 const char **file_names;
2903
2904 /* The file names from the line table after being run through
2905 gdb_realpath. These are computed lazily. */
2906 const char **real_names;
2907};
2908
2909/* When using the index (and thus not using psymtabs), each CU has an
2910 object of this type. This is used to hold information needed by
2911 the various "quick" methods. */
2912struct dwarf2_per_cu_quick_data
2913{
2914 /* The file table. This can be NULL if there was no file table
2915 or it's currently not read in.
2916 NOTE: This points into dwarf2_per_objfile->quick_file_names_table. */
2917 struct quick_file_names *file_names;
2918
2919 /* The corresponding symbol table. This is NULL if symbols for this
2920 CU have not yet been read. */
43f3e411 2921 struct compunit_symtab *compunit_symtab;
7b9f3c50
DE
2922
2923 /* A temporary mark bit used when iterating over all CUs in
2924 expand_symtabs_matching. */
2925 unsigned int mark : 1;
2926
2927 /* True if we've tried to read the file table and found there isn't one.
2928 There will be no point in trying to read it again next time. */
2929 unsigned int no_file_data : 1;
2930};
2931
094b34ac
DE
2932/* Utility hash function for a stmt_list_hash. */
2933
2934static hashval_t
2935hash_stmt_list_entry (const struct stmt_list_hash *stmt_list_hash)
2936{
2937 hashval_t v = 0;
2938
2939 if (stmt_list_hash->dwo_unit != NULL)
2940 v += (uintptr_t) stmt_list_hash->dwo_unit->dwo_file;
9c541725 2941 v += to_underlying (stmt_list_hash->line_sect_off);
094b34ac
DE
2942 return v;
2943}
2944
2945/* Utility equality function for a stmt_list_hash. */
2946
2947static int
2948eq_stmt_list_entry (const struct stmt_list_hash *lhs,
2949 const struct stmt_list_hash *rhs)
2950{
2951 if ((lhs->dwo_unit != NULL) != (rhs->dwo_unit != NULL))
2952 return 0;
2953 if (lhs->dwo_unit != NULL
2954 && lhs->dwo_unit->dwo_file != rhs->dwo_unit->dwo_file)
2955 return 0;
2956
9c541725 2957 return lhs->line_sect_off == rhs->line_sect_off;
094b34ac
DE
2958}
2959
7b9f3c50
DE
2960/* Hash function for a quick_file_names. */
2961
2962static hashval_t
2963hash_file_name_entry (const void *e)
2964{
9a3c8263
SM
2965 const struct quick_file_names *file_data
2966 = (const struct quick_file_names *) e;
7b9f3c50 2967
094b34ac 2968 return hash_stmt_list_entry (&file_data->hash);
7b9f3c50
DE
2969}
2970
2971/* Equality function for a quick_file_names. */
2972
2973static int
2974eq_file_name_entry (const void *a, const void *b)
2975{
9a3c8263
SM
2976 const struct quick_file_names *ea = (const struct quick_file_names *) a;
2977 const struct quick_file_names *eb = (const struct quick_file_names *) b;
7b9f3c50 2978
094b34ac 2979 return eq_stmt_list_entry (&ea->hash, &eb->hash);
7b9f3c50
DE
2980}
2981
2982/* Delete function for a quick_file_names. */
2983
2984static void
2985delete_file_name_entry (void *e)
2986{
9a3c8263 2987 struct quick_file_names *file_data = (struct quick_file_names *) e;
7b9f3c50
DE
2988 int i;
2989
2990 for (i = 0; i < file_data->num_file_names; ++i)
2991 {
2992 xfree ((void*) file_data->file_names[i]);
2993 if (file_data->real_names)
2994 xfree ((void*) file_data->real_names[i]);
2995 }
2996
2997 /* The space for the struct itself lives on objfile_obstack,
2998 so we don't free it here. */
2999}
3000
3001/* Create a quick_file_names hash table. */
3002
3003static htab_t
3004create_quick_file_names_table (unsigned int nr_initial_entries)
3005{
3006 return htab_create_alloc (nr_initial_entries,
3007 hash_file_name_entry, eq_file_name_entry,
3008 delete_file_name_entry, xcalloc, xfree);
3009}
9291a0cd 3010
918dd910
JK
3011/* Read in PER_CU->CU. This function is unrelated to symtabs, symtab would
3012 have to be created afterwards. You should call age_cached_comp_units after
3013 processing PER_CU->CU. dw2_setup must have been already called. */
3014
3015static void
3016load_cu (struct dwarf2_per_cu_data *per_cu)
3017{
3019eac3 3018 if (per_cu->is_debug_types)
e5fe5e75 3019 load_full_type_unit (per_cu);
918dd910 3020 else
95554aad 3021 load_full_comp_unit (per_cu, language_minimal);
918dd910 3022
cc12ce38
DE
3023 if (per_cu->cu == NULL)
3024 return; /* Dummy CU. */
2dc860c0
DE
3025
3026 dwarf2_find_base_address (per_cu->cu->dies, per_cu->cu);
918dd910
JK
3027}
3028
a0f42c21 3029/* Read in the symbols for PER_CU. */
2fdf6df6 3030
9291a0cd 3031static void
a0f42c21 3032dw2_do_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
9291a0cd
TT
3033{
3034 struct cleanup *back_to;
3035
f4dc4d17
DE
3036 /* Skip type_unit_groups, reading the type units they contain
3037 is handled elsewhere. */
3038 if (IS_TYPE_UNIT_GROUP (per_cu))
3039 return;
3040
9291a0cd
TT
3041 back_to = make_cleanup (dwarf2_release_queue, NULL);
3042
95554aad 3043 if (dwarf2_per_objfile->using_index
43f3e411 3044 ? per_cu->v.quick->compunit_symtab == NULL
95554aad
TT
3045 : (per_cu->v.psymtab == NULL || !per_cu->v.psymtab->readin))
3046 {
3047 queue_comp_unit (per_cu, language_minimal);
3048 load_cu (per_cu);
89e63ee4
DE
3049
3050 /* If we just loaded a CU from a DWO, and we're working with an index
3051 that may badly handle TUs, load all the TUs in that DWO as well.
3052 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
3053 if (!per_cu->is_debug_types
cc12ce38 3054 && per_cu->cu != NULL
89e63ee4
DE
3055 && per_cu->cu->dwo_unit != NULL
3056 && dwarf2_per_objfile->index_table != NULL
3057 && dwarf2_per_objfile->index_table->version <= 7
3058 /* DWP files aren't supported yet. */
3059 && get_dwp_file () == NULL)
3060 queue_and_load_all_dwo_tus (per_cu);
95554aad 3061 }
9291a0cd 3062
a0f42c21 3063 process_queue ();
9291a0cd
TT
3064
3065 /* Age the cache, releasing compilation units that have not
3066 been used recently. */
3067 age_cached_comp_units ();
3068
3069 do_cleanups (back_to);
3070}
3071
3072/* Ensure that the symbols for PER_CU have been read in. OBJFILE is
3073 the objfile from which this CU came. Returns the resulting symbol
3074 table. */
2fdf6df6 3075
43f3e411 3076static struct compunit_symtab *
a0f42c21 3077dw2_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
9291a0cd 3078{
95554aad 3079 gdb_assert (dwarf2_per_objfile->using_index);
43f3e411 3080 if (!per_cu->v.quick->compunit_symtab)
9291a0cd
TT
3081 {
3082 struct cleanup *back_to = make_cleanup (free_cached_comp_units, NULL);
c83dd867 3083 scoped_restore decrementer = increment_reading_symtab ();
a0f42c21 3084 dw2_do_instantiate_symtab (per_cu);
95554aad 3085 process_cu_includes ();
9291a0cd
TT
3086 do_cleanups (back_to);
3087 }
f194fefb 3088
43f3e411 3089 return per_cu->v.quick->compunit_symtab;
9291a0cd
TT
3090}
3091
8832e7e3 3092/* Return the CU/TU given its index.
f4dc4d17
DE
3093
3094 This is intended for loops like:
3095
3096 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
3097 + dwarf2_per_objfile->n_type_units); ++i)
3098 {
8832e7e3 3099 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
f4dc4d17
DE
3100
3101 ...;
3102 }
3103*/
2fdf6df6 3104
1fd400ff 3105static struct dwarf2_per_cu_data *
8832e7e3 3106dw2_get_cutu (int index)
1fd400ff
TT
3107{
3108 if (index >= dwarf2_per_objfile->n_comp_units)
3109 {
f4dc4d17 3110 index -= dwarf2_per_objfile->n_comp_units;
094b34ac
DE
3111 gdb_assert (index < dwarf2_per_objfile->n_type_units);
3112 return &dwarf2_per_objfile->all_type_units[index]->per_cu;
f4dc4d17
DE
3113 }
3114
3115 return dwarf2_per_objfile->all_comp_units[index];
3116}
3117
8832e7e3
DE
3118/* Return the CU given its index.
3119 This differs from dw2_get_cutu in that it's for when you know INDEX
3120 refers to a CU. */
f4dc4d17
DE
3121
3122static struct dwarf2_per_cu_data *
8832e7e3 3123dw2_get_cu (int index)
f4dc4d17 3124{
8832e7e3 3125 gdb_assert (index >= 0 && index < dwarf2_per_objfile->n_comp_units);
f4dc4d17 3126
1fd400ff
TT
3127 return dwarf2_per_objfile->all_comp_units[index];
3128}
3129
4b514bc8
JK
3130/* Return a new dwarf2_per_cu_data allocated on OBJFILE's
3131 objfile_obstack, and constructed with the specified field
3132 values. */
3133
3134static dwarf2_per_cu_data *
3135create_cu_from_index_list (struct objfile *objfile,
3136 struct dwarf2_section_info *section,
3137 int is_dwz,
3138 sect_offset sect_off, ULONGEST length)
3139{
3140 dwarf2_per_cu_data *the_cu
3141 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3142 struct dwarf2_per_cu_data);
3143 the_cu->sect_off = sect_off;
3144 the_cu->length = length;
3145 the_cu->objfile = objfile;
3146 the_cu->section = section;
3147 the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3148 struct dwarf2_per_cu_quick_data);
3149 the_cu->is_dwz = is_dwz;
3150 return the_cu;
3151}
3152
2ec9a5e0
TT
3153/* A helper for create_cus_from_index that handles a given list of
3154 CUs. */
2fdf6df6 3155
74a0d9f6 3156static void
2ec9a5e0
TT
3157create_cus_from_index_list (struct objfile *objfile,
3158 const gdb_byte *cu_list, offset_type n_elements,
3159 struct dwarf2_section_info *section,
3160 int is_dwz,
3161 int base_offset)
9291a0cd
TT
3162{
3163 offset_type i;
9291a0cd 3164
2ec9a5e0 3165 for (i = 0; i < n_elements; i += 2)
9291a0cd 3166 {
74a0d9f6 3167 gdb_static_assert (sizeof (ULONGEST) >= 8);
9c541725
PA
3168
3169 sect_offset sect_off
3170 = (sect_offset) extract_unsigned_integer (cu_list, 8, BFD_ENDIAN_LITTLE);
3171 ULONGEST length = extract_unsigned_integer (cu_list + 8, 8, BFD_ENDIAN_LITTLE);
9291a0cd
TT
3172 cu_list += 2 * 8;
3173
4b514bc8
JK
3174 dwarf2_per_objfile->all_comp_units[base_offset + i / 2]
3175 = create_cu_from_index_list (objfile, section, is_dwz, sect_off, length);
9291a0cd 3176 }
9291a0cd
TT
3177}
3178
2ec9a5e0 3179/* Read the CU list from the mapped index, and use it to create all
74a0d9f6 3180 the CU objects for this objfile. */
2ec9a5e0 3181
74a0d9f6 3182static void
2ec9a5e0
TT
3183create_cus_from_index (struct objfile *objfile,
3184 const gdb_byte *cu_list, offset_type cu_list_elements,
3185 const gdb_byte *dwz_list, offset_type dwz_elements)
3186{
3187 struct dwz_file *dwz;
3188
3189 dwarf2_per_objfile->n_comp_units = (cu_list_elements + dwz_elements) / 2;
8d749320
SM
3190 dwarf2_per_objfile->all_comp_units =
3191 XOBNEWVEC (&objfile->objfile_obstack, struct dwarf2_per_cu_data *,
3192 dwarf2_per_objfile->n_comp_units);
2ec9a5e0 3193
74a0d9f6
JK
3194 create_cus_from_index_list (objfile, cu_list, cu_list_elements,
3195 &dwarf2_per_objfile->info, 0, 0);
2ec9a5e0
TT
3196
3197 if (dwz_elements == 0)
74a0d9f6 3198 return;
2ec9a5e0
TT
3199
3200 dwz = dwarf2_get_dwz_file ();
74a0d9f6
JK
3201 create_cus_from_index_list (objfile, dwz_list, dwz_elements, &dwz->info, 1,
3202 cu_list_elements / 2);
2ec9a5e0
TT
3203}
3204
1fd400ff 3205/* Create the signatured type hash table from the index. */
673bfd45 3206
74a0d9f6 3207static void
673bfd45 3208create_signatured_type_table_from_index (struct objfile *objfile,
8b70b953 3209 struct dwarf2_section_info *section,
673bfd45
DE
3210 const gdb_byte *bytes,
3211 offset_type elements)
1fd400ff
TT
3212{
3213 offset_type i;
673bfd45 3214 htab_t sig_types_hash;
1fd400ff 3215
6aa5f3a6
DE
3216 dwarf2_per_objfile->n_type_units
3217 = dwarf2_per_objfile->n_allocated_type_units
3218 = elements / 3;
8d749320
SM
3219 dwarf2_per_objfile->all_type_units =
3220 XNEWVEC (struct signatured_type *, dwarf2_per_objfile->n_type_units);
1fd400ff 3221
673bfd45 3222 sig_types_hash = allocate_signatured_type_table (objfile);
1fd400ff
TT
3223
3224 for (i = 0; i < elements; i += 3)
3225 {
52dc124a 3226 struct signatured_type *sig_type;
9c541725 3227 ULONGEST signature;
1fd400ff 3228 void **slot;
9c541725 3229 cu_offset type_offset_in_tu;
1fd400ff 3230
74a0d9f6 3231 gdb_static_assert (sizeof (ULONGEST) >= 8);
9c541725
PA
3232 sect_offset sect_off
3233 = (sect_offset) extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
3234 type_offset_in_tu
3235 = (cu_offset) extract_unsigned_integer (bytes + 8, 8,
3236 BFD_ENDIAN_LITTLE);
1fd400ff
TT
3237 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
3238 bytes += 3 * 8;
3239
52dc124a 3240 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1fd400ff 3241 struct signatured_type);
52dc124a 3242 sig_type->signature = signature;
9c541725 3243 sig_type->type_offset_in_tu = type_offset_in_tu;
3019eac3 3244 sig_type->per_cu.is_debug_types = 1;
8a0459fd 3245 sig_type->per_cu.section = section;
9c541725 3246 sig_type->per_cu.sect_off = sect_off;
52dc124a
DE
3247 sig_type->per_cu.objfile = objfile;
3248 sig_type->per_cu.v.quick
1fd400ff
TT
3249 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3250 struct dwarf2_per_cu_quick_data);
3251
52dc124a
DE
3252 slot = htab_find_slot (sig_types_hash, sig_type, INSERT);
3253 *slot = sig_type;
1fd400ff 3254
b4dd5633 3255 dwarf2_per_objfile->all_type_units[i / 3] = sig_type;
1fd400ff
TT
3256 }
3257
673bfd45 3258 dwarf2_per_objfile->signatured_types = sig_types_hash;
1fd400ff
TT
3259}
3260
927aa2e7
JK
3261/* Create the signatured type hash table from .debug_names. */
3262
3263static void
3264create_signatured_type_table_from_debug_names
3265 (struct objfile *objfile,
3266 const mapped_debug_names &map,
3267 struct dwarf2_section_info *section,
3268 struct dwarf2_section_info *abbrev_section)
3269{
3270 dwarf2_read_section (objfile, section);
3271 dwarf2_read_section (objfile, abbrev_section);
3272
3273 dwarf2_per_objfile->n_type_units
3274 = dwarf2_per_objfile->n_allocated_type_units
3275 = map.tu_count;
3276 dwarf2_per_objfile->all_type_units
3277 = XNEWVEC (struct signatured_type *, dwarf2_per_objfile->n_type_units);
3278
3279 htab_t sig_types_hash = allocate_signatured_type_table (objfile);
3280
3281 for (uint32_t i = 0; i < map.tu_count; ++i)
3282 {
3283 struct signatured_type *sig_type;
3284 ULONGEST signature;
3285 void **slot;
3286 cu_offset type_offset_in_tu;
3287
3288 sect_offset sect_off
3289 = (sect_offset) (extract_unsigned_integer
3290 (map.tu_table_reordered + i * map.offset_size,
3291 map.offset_size,
3292 map.dwarf5_byte_order));
3293
3294 comp_unit_head cu_header;
3295 read_and_check_comp_unit_head (&cu_header, section, abbrev_section,
3296 section->buffer + to_underlying (sect_off),
3297 rcuh_kind::TYPE);
3298
3299 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3300 struct signatured_type);
3301 sig_type->signature = cu_header.signature;
3302 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
3303 sig_type->per_cu.is_debug_types = 1;
3304 sig_type->per_cu.section = section;
3305 sig_type->per_cu.sect_off = sect_off;
3306 sig_type->per_cu.objfile = objfile;
3307 sig_type->per_cu.v.quick
3308 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3309 struct dwarf2_per_cu_quick_data);
3310
3311 slot = htab_find_slot (sig_types_hash, sig_type, INSERT);
3312 *slot = sig_type;
3313
3314 dwarf2_per_objfile->all_type_units[i] = sig_type;
3315 }
3316
3317 dwarf2_per_objfile->signatured_types = sig_types_hash;
3318}
3319
9291a0cd
TT
3320/* Read the address map data from the mapped index, and use it to
3321 populate the objfile's psymtabs_addrmap. */
2fdf6df6 3322
9291a0cd
TT
3323static void
3324create_addrmap_from_index (struct objfile *objfile, struct mapped_index *index)
3325{
3e29f34a 3326 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9291a0cd 3327 const gdb_byte *iter, *end;
9291a0cd 3328 struct addrmap *mutable_map;
9291a0cd
TT
3329 CORE_ADDR baseaddr;
3330
8268c778
PA
3331 auto_obstack temp_obstack;
3332
9291a0cd
TT
3333 mutable_map = addrmap_create_mutable (&temp_obstack);
3334
f00a2de2
PA
3335 iter = index->address_table.data ();
3336 end = iter + index->address_table.size ();
9291a0cd
TT
3337
3338 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
3339
3340 while (iter < end)
3341 {
3342 ULONGEST hi, lo, cu_index;
3343 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
3344 iter += 8;
3345 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
3346 iter += 8;
3347 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
3348 iter += 4;
f652bce2 3349
24a55014 3350 if (lo > hi)
f652bce2 3351 {
24a55014
DE
3352 complaint (&symfile_complaints,
3353 _(".gdb_index address table has invalid range (%s - %s)"),
c0cd8254 3354 hex_string (lo), hex_string (hi));
24a55014 3355 continue;
f652bce2 3356 }
24a55014
DE
3357
3358 if (cu_index >= dwarf2_per_objfile->n_comp_units)
f652bce2
DE
3359 {
3360 complaint (&symfile_complaints,
3361 _(".gdb_index address table has invalid CU number %u"),
3362 (unsigned) cu_index);
24a55014 3363 continue;
f652bce2 3364 }
24a55014 3365
3e29f34a
MR
3366 lo = gdbarch_adjust_dwarf2_addr (gdbarch, lo + baseaddr);
3367 hi = gdbarch_adjust_dwarf2_addr (gdbarch, hi + baseaddr);
3368 addrmap_set_empty (mutable_map, lo, hi - 1, dw2_get_cutu (cu_index));
9291a0cd
TT
3369 }
3370
3371 objfile->psymtabs_addrmap = addrmap_create_fixed (mutable_map,
3372 &objfile->objfile_obstack);
9291a0cd
TT
3373}
3374
927aa2e7
JK
3375/* Read the address map data from DWARF-5 .debug_aranges, and use it to
3376 populate the objfile's psymtabs_addrmap. */
3377
3378static void
3379create_addrmap_from_aranges (struct objfile *objfile,
3380 struct dwarf2_section_info *section)
3381{
3382 bfd *abfd = objfile->obfd;
3383 struct gdbarch *gdbarch = get_objfile_arch (objfile);
3384 const CORE_ADDR baseaddr = ANOFFSET (objfile->section_offsets,
3385 SECT_OFF_TEXT (objfile));
3386
3387 auto_obstack temp_obstack;
3388 addrmap *mutable_map = addrmap_create_mutable (&temp_obstack);
3389
3390 std::unordered_map<sect_offset,
3391 dwarf2_per_cu_data *,
3392 gdb::hash_enum<sect_offset>>
3393 debug_info_offset_to_per_cu;
3394 for (int cui = 0; cui < dwarf2_per_objfile->n_comp_units; ++cui)
3395 {
3396 dwarf2_per_cu_data *per_cu = dw2_get_cutu (cui);
3397 const auto insertpair
3398 = debug_info_offset_to_per_cu.emplace (per_cu->sect_off, per_cu);
3399 if (!insertpair.second)
3400 {
3401 warning (_("Section .debug_aranges in %s has duplicate "
3402 "debug_info_offset %u, ignoring .debug_aranges."),
3403 objfile_name (objfile), to_underlying (per_cu->sect_off));
3404 return;
3405 }
3406 }
3407
3408 dwarf2_read_section (objfile, section);
3409
3410 const bfd_endian dwarf5_byte_order = gdbarch_byte_order (gdbarch);
3411
3412 const gdb_byte *addr = section->buffer;
3413
3414 while (addr < section->buffer + section->size)
3415 {
3416 const gdb_byte *const entry_addr = addr;
3417 unsigned int bytes_read;
3418
3419 const LONGEST entry_length = read_initial_length (abfd, addr,
3420 &bytes_read);
3421 addr += bytes_read;
3422
3423 const gdb_byte *const entry_end = addr + entry_length;
3424 const bool dwarf5_is_dwarf64 = bytes_read != 4;
3425 const uint8_t offset_size = dwarf5_is_dwarf64 ? 8 : 4;
3426 if (addr + entry_length > section->buffer + section->size)
3427 {
3428 warning (_("Section .debug_aranges in %s entry at offset %zu "
3429 "length %s exceeds section length %s, "
3430 "ignoring .debug_aranges."),
3431 objfile_name (objfile), entry_addr - section->buffer,
3432 plongest (bytes_read + entry_length),
3433 pulongest (section->size));
3434 return;
3435 }
3436
3437 /* The version number. */
3438 const uint16_t version = read_2_bytes (abfd, addr);
3439 addr += 2;
3440 if (version != 2)
3441 {
3442 warning (_("Section .debug_aranges in %s entry at offset %zu "
3443 "has unsupported version %d, ignoring .debug_aranges."),
3444 objfile_name (objfile), entry_addr - section->buffer,
3445 version);
3446 return;
3447 }
3448
3449 const uint64_t debug_info_offset
3450 = extract_unsigned_integer (addr, offset_size, dwarf5_byte_order);
3451 addr += offset_size;
3452 const auto per_cu_it
3453 = debug_info_offset_to_per_cu.find (sect_offset (debug_info_offset));
3454 if (per_cu_it == debug_info_offset_to_per_cu.cend ())
3455 {
3456 warning (_("Section .debug_aranges in %s entry at offset %zu "
3457 "debug_info_offset %s does not exists, "
3458 "ignoring .debug_aranges."),
3459 objfile_name (objfile), entry_addr - section->buffer,
3460 pulongest (debug_info_offset));
3461 return;
3462 }
3463 dwarf2_per_cu_data *const per_cu = per_cu_it->second;
3464
3465 const uint8_t address_size = *addr++;
3466 if (address_size < 1 || address_size > 8)
3467 {
3468 warning (_("Section .debug_aranges in %s entry at offset %zu "
3469 "address_size %u is invalid, ignoring .debug_aranges."),
3470 objfile_name (objfile), entry_addr - section->buffer,
3471 address_size);
3472 return;
3473 }
3474
3475 const uint8_t segment_selector_size = *addr++;
3476 if (segment_selector_size != 0)
3477 {
3478 warning (_("Section .debug_aranges in %s entry at offset %zu "
3479 "segment_selector_size %u is not supported, "
3480 "ignoring .debug_aranges."),
3481 objfile_name (objfile), entry_addr - section->buffer,
3482 segment_selector_size);
3483 return;
3484 }
3485
3486 /* Must pad to an alignment boundary that is twice the address
3487 size. It is undocumented by the DWARF standard but GCC does
3488 use it. */
3489 for (size_t padding = ((-(addr - section->buffer))
3490 & (2 * address_size - 1));
3491 padding > 0; padding--)
3492 if (*addr++ != 0)
3493 {
3494 warning (_("Section .debug_aranges in %s entry at offset %zu "
3495 "padding is not zero, ignoring .debug_aranges."),
3496 objfile_name (objfile), entry_addr - section->buffer);
3497 return;
3498 }
3499
3500 for (;;)
3501 {
3502 if (addr + 2 * address_size > entry_end)
3503 {
3504 warning (_("Section .debug_aranges in %s entry at offset %zu "
3505 "address list is not properly terminated, "
3506 "ignoring .debug_aranges."),
3507 objfile_name (objfile), entry_addr - section->buffer);
3508 return;
3509 }
3510 ULONGEST start = extract_unsigned_integer (addr, address_size,
3511 dwarf5_byte_order);
3512 addr += address_size;
3513 ULONGEST length = extract_unsigned_integer (addr, address_size,
3514 dwarf5_byte_order);
3515 addr += address_size;
3516 if (start == 0 && length == 0)
3517 break;
3518 if (start == 0 && !dwarf2_per_objfile->has_section_at_zero)
3519 {
3520 /* Symbol was eliminated due to a COMDAT group. */
3521 continue;
3522 }
3523 ULONGEST end = start + length;
3524 start = gdbarch_adjust_dwarf2_addr (gdbarch, start + baseaddr);
3525 end = gdbarch_adjust_dwarf2_addr (gdbarch, end + baseaddr);
3526 addrmap_set_empty (mutable_map, start, end - 1, per_cu);
3527 }
3528 }
3529
3530 objfile->psymtabs_addrmap = addrmap_create_fixed (mutable_map,
3531 &objfile->objfile_obstack);
3532}
3533
59d7bcaf
JK
3534/* The hash function for strings in the mapped index. This is the same as
3535 SYMBOL_HASH_NEXT, but we keep a separate copy to maintain control over the
3536 implementation. This is necessary because the hash function is tied to the
3537 format of the mapped index file. The hash values do not have to match with
559a7a62
JK
3538 SYMBOL_HASH_NEXT.
3539
3540 Use INT_MAX for INDEX_VERSION if you generate the current index format. */
2fdf6df6 3541
9291a0cd 3542static hashval_t
559a7a62 3543mapped_index_string_hash (int index_version, const void *p)
9291a0cd
TT
3544{
3545 const unsigned char *str = (const unsigned char *) p;
3546 hashval_t r = 0;
3547 unsigned char c;
3548
3549 while ((c = *str++) != 0)
559a7a62
JK
3550 {
3551 if (index_version >= 5)
3552 c = tolower (c);
3553 r = r * 67 + c - 113;
3554 }
9291a0cd
TT
3555
3556 return r;
3557}
3558
3559/* Find a slot in the mapped index INDEX for the object named NAME.
3560 If NAME is found, set *VEC_OUT to point to the CU vector in the
109483d9
PA
3561 constant pool and return true. If NAME cannot be found, return
3562 false. */
2fdf6df6 3563
109483d9 3564static bool
9291a0cd
TT
3565find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
3566 offset_type **vec_out)
3567{
0cf03b49 3568 offset_type hash;
9291a0cd 3569 offset_type slot, step;
559a7a62 3570 int (*cmp) (const char *, const char *);
9291a0cd 3571
791afaa2 3572 gdb::unique_xmalloc_ptr<char> without_params;
0cf03b49 3573 if (current_language->la_language == language_cplus
45280282
IB
3574 || current_language->la_language == language_fortran
3575 || current_language->la_language == language_d)
0cf03b49
JK
3576 {
3577 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
3578 not contain any. */
a8719064 3579
72998fb3 3580 if (strchr (name, '(') != NULL)
0cf03b49 3581 {
109483d9 3582 without_params = cp_remove_params (name);
0cf03b49 3583
72998fb3 3584 if (without_params != NULL)
791afaa2 3585 name = without_params.get ();
0cf03b49
JK
3586 }
3587 }
3588
559a7a62 3589 /* Index version 4 did not support case insensitive searches. But the
feea76c2 3590 indices for case insensitive languages are built in lowercase, therefore
559a7a62
JK
3591 simulate our NAME being searched is also lowercased. */
3592 hash = mapped_index_string_hash ((index->version == 4
3593 && case_sensitivity == case_sensitive_off
3594 ? 5 : index->version),
3595 name);
3596
f00a2de2
PA
3597 slot = hash & (index->symbol_table.size () - 1);
3598 step = ((hash * 17) & (index->symbol_table.size () - 1)) | 1;
559a7a62 3599 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
9291a0cd
TT
3600
3601 for (;;)
3602 {
9291a0cd 3603 const char *str;
f00a2de2
PA
3604
3605 const auto &bucket = index->symbol_table[slot];
3606 if (bucket.name == 0 && bucket.vec == 0)
109483d9 3607 return false;
9291a0cd 3608
f00a2de2 3609 str = index->constant_pool + MAYBE_SWAP (bucket.name);
559a7a62 3610 if (!cmp (name, str))
9291a0cd
TT
3611 {
3612 *vec_out = (offset_type *) (index->constant_pool
f00a2de2 3613 + MAYBE_SWAP (bucket.vec));
109483d9 3614 return true;
9291a0cd
TT
3615 }
3616
f00a2de2 3617 slot = (slot + step) & (index->symbol_table.size () - 1);
9291a0cd
TT
3618 }
3619}
3620
2ec9a5e0
TT
3621/* A helper function that reads the .gdb_index from SECTION and fills
3622 in MAP. FILENAME is the name of the file containing the section;
3623 it is used for error reporting. DEPRECATED_OK is nonzero if it is
3624 ok to use deprecated sections.
3625
3626 CU_LIST, CU_LIST_ELEMENTS, TYPES_LIST, and TYPES_LIST_ELEMENTS are
3627 out parameters that are filled in with information about the CU and
3628 TU lists in the section.
3629
3630 Returns 1 if all went well, 0 otherwise. */
2fdf6df6 3631
9291a0cd 3632static int
2ec9a5e0
TT
3633read_index_from_section (struct objfile *objfile,
3634 const char *filename,
3635 int deprecated_ok,
3636 struct dwarf2_section_info *section,
3637 struct mapped_index *map,
3638 const gdb_byte **cu_list,
3639 offset_type *cu_list_elements,
3640 const gdb_byte **types_list,
3641 offset_type *types_list_elements)
9291a0cd 3642{
948f8e3d 3643 const gdb_byte *addr;
2ec9a5e0 3644 offset_type version;
b3b272e1 3645 offset_type *metadata;
1fd400ff 3646 int i;
9291a0cd 3647
2ec9a5e0 3648 if (dwarf2_section_empty_p (section))
9291a0cd 3649 return 0;
82430852
JK
3650
3651 /* Older elfutils strip versions could keep the section in the main
3652 executable while splitting it for the separate debug info file. */
a32a8923 3653 if ((get_section_flags (section) & SEC_HAS_CONTENTS) == 0)
82430852
JK
3654 return 0;
3655
2ec9a5e0 3656 dwarf2_read_section (objfile, section);
9291a0cd 3657
2ec9a5e0 3658 addr = section->buffer;
9291a0cd 3659 /* Version check. */
1fd400ff 3660 version = MAYBE_SWAP (*(offset_type *) addr);
987d643c 3661 /* Versions earlier than 3 emitted every copy of a psymbol. This
a6e293d1 3662 causes the index to behave very poorly for certain requests. Version 3
831adc1f 3663 contained incomplete addrmap. So, it seems better to just ignore such
481860b3 3664 indices. */
831adc1f 3665 if (version < 4)
481860b3
GB
3666 {
3667 static int warning_printed = 0;
3668 if (!warning_printed)
3669 {
3670 warning (_("Skipping obsolete .gdb_index section in %s."),
2ec9a5e0 3671 filename);
481860b3
GB
3672 warning_printed = 1;
3673 }
3674 return 0;
3675 }
3676 /* Index version 4 uses a different hash function than index version
3677 5 and later.
3678
3679 Versions earlier than 6 did not emit psymbols for inlined
3680 functions. Using these files will cause GDB not to be able to
3681 set breakpoints on inlined functions by name, so we ignore these
e615022a
DE
3682 indices unless the user has done
3683 "set use-deprecated-index-sections on". */
2ec9a5e0 3684 if (version < 6 && !deprecated_ok)
481860b3
GB
3685 {
3686 static int warning_printed = 0;
3687 if (!warning_printed)
3688 {
e615022a
DE
3689 warning (_("\
3690Skipping deprecated .gdb_index section in %s.\n\
3691Do \"set use-deprecated-index-sections on\" before the file is read\n\
3692to use the section anyway."),
2ec9a5e0 3693 filename);
481860b3
GB
3694 warning_printed = 1;
3695 }
3696 return 0;
3697 }
796a7ff8 3698 /* Version 7 indices generated by gold refer to the CU for a symbol instead
8943b874
DE
3699 of the TU (for symbols coming from TUs),
3700 http://sourceware.org/bugzilla/show_bug.cgi?id=15021.
3701 Plus gold-generated indices can have duplicate entries for global symbols,
3702 http://sourceware.org/bugzilla/show_bug.cgi?id=15646.
3703 These are just performance bugs, and we can't distinguish gdb-generated
3704 indices from gold-generated ones, so issue no warning here. */
796a7ff8 3705
481860b3 3706 /* Indexes with higher version than the one supported by GDB may be no
594e8718 3707 longer backward compatible. */
796a7ff8 3708 if (version > 8)
594e8718 3709 return 0;
9291a0cd 3710
559a7a62 3711 map->version = version;
2ec9a5e0 3712 map->total_size = section->size;
9291a0cd
TT
3713
3714 metadata = (offset_type *) (addr + sizeof (offset_type));
1fd400ff
TT
3715
3716 i = 0;
2ec9a5e0
TT
3717 *cu_list = addr + MAYBE_SWAP (metadata[i]);
3718 *cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
3719 / 8);
1fd400ff
TT
3720 ++i;
3721
2ec9a5e0
TT
3722 *types_list = addr + MAYBE_SWAP (metadata[i]);
3723 *types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
3724 - MAYBE_SWAP (metadata[i]))
3725 / 8);
987d643c 3726 ++i;
1fd400ff 3727
f00a2de2
PA
3728 const gdb_byte *address_table = addr + MAYBE_SWAP (metadata[i]);
3729 const gdb_byte *address_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
3730 map->address_table
3731 = gdb::array_view<const gdb_byte> (address_table, address_table_end);
1fd400ff
TT
3732 ++i;
3733
f00a2de2
PA
3734 const gdb_byte *symbol_table = addr + MAYBE_SWAP (metadata[i]);
3735 const gdb_byte *symbol_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
3736 map->symbol_table
3737 = gdb::array_view<mapped_index::symbol_table_slot>
3738 ((mapped_index::symbol_table_slot *) symbol_table,
3739 (mapped_index::symbol_table_slot *) symbol_table_end);
9291a0cd 3740
f00a2de2 3741 ++i;
f9d83a0b 3742 map->constant_pool = (char *) (addr + MAYBE_SWAP (metadata[i]));
1fd400ff 3743
2ec9a5e0
TT
3744 return 1;
3745}
3746
927aa2e7 3747/* Read .gdb_index. If everything went ok, initialize the "quick"
2ec9a5e0
TT
3748 elements of all the CUs and return 1. Otherwise, return 0. */
3749
3750static int
3751dwarf2_read_index (struct objfile *objfile)
3752{
3753 struct mapped_index local_map, *map;
3754 const gdb_byte *cu_list, *types_list, *dwz_list = NULL;
3755 offset_type cu_list_elements, types_list_elements, dwz_list_elements = 0;
4db1a1dc 3756 struct dwz_file *dwz;
2ec9a5e0 3757
4262abfb 3758 if (!read_index_from_section (objfile, objfile_name (objfile),
2ec9a5e0
TT
3759 use_deprecated_index_sections,
3760 &dwarf2_per_objfile->gdb_index, &local_map,
3761 &cu_list, &cu_list_elements,
3762 &types_list, &types_list_elements))
3763 return 0;
3764
0fefef59 3765 /* Don't use the index if it's empty. */
f00a2de2 3766 if (local_map.symbol_table.empty ())
0fefef59
DE
3767 return 0;
3768
2ec9a5e0
TT
3769 /* If there is a .dwz file, read it so we can get its CU list as
3770 well. */
4db1a1dc
TT
3771 dwz = dwarf2_get_dwz_file ();
3772 if (dwz != NULL)
2ec9a5e0 3773 {
2ec9a5e0
TT
3774 struct mapped_index dwz_map;
3775 const gdb_byte *dwz_types_ignore;
3776 offset_type dwz_types_elements_ignore;
3777
3778 if (!read_index_from_section (objfile, bfd_get_filename (dwz->dwz_bfd),
3779 1,
3780 &dwz->gdb_index, &dwz_map,
3781 &dwz_list, &dwz_list_elements,
3782 &dwz_types_ignore,
3783 &dwz_types_elements_ignore))
3784 {
3785 warning (_("could not read '.gdb_index' section from %s; skipping"),
3786 bfd_get_filename (dwz->dwz_bfd));
3787 return 0;
3788 }
3789 }
3790
74a0d9f6
JK
3791 create_cus_from_index (objfile, cu_list, cu_list_elements, dwz_list,
3792 dwz_list_elements);
1fd400ff 3793
8b70b953
TT
3794 if (types_list_elements)
3795 {
3796 struct dwarf2_section_info *section;
3797
3798 /* We can only handle a single .debug_types when we have an
3799 index. */
3800 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) != 1)
3801 return 0;
3802
3803 section = VEC_index (dwarf2_section_info_def,
3804 dwarf2_per_objfile->types, 0);
3805
74a0d9f6
JK
3806 create_signatured_type_table_from_index (objfile, section, types_list,
3807 types_list_elements);
8b70b953 3808 }
9291a0cd 3809
2ec9a5e0
TT
3810 create_addrmap_from_index (objfile, &local_map);
3811
8d749320 3812 map = XOBNEW (&objfile->objfile_obstack, struct mapped_index);
3f563c84 3813 map = new (map) mapped_index ();
2ec9a5e0 3814 *map = local_map;
9291a0cd
TT
3815
3816 dwarf2_per_objfile->index_table = map;
3817 dwarf2_per_objfile->using_index = 1;
7b9f3c50
DE
3818 dwarf2_per_objfile->quick_file_names_table =
3819 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
9291a0cd
TT
3820
3821 return 1;
3822}
3823
3824/* A helper for the "quick" functions which sets the global
3825 dwarf2_per_objfile according to OBJFILE. */
2fdf6df6 3826
9291a0cd
TT
3827static void
3828dw2_setup (struct objfile *objfile)
3829{
9a3c8263
SM
3830 dwarf2_per_objfile = ((struct dwarf2_per_objfile *)
3831 objfile_data (objfile, dwarf2_objfile_data_key));
9291a0cd
TT
3832 gdb_assert (dwarf2_per_objfile);
3833}
3834
dee91e82 3835/* die_reader_func for dw2_get_file_names. */
2fdf6df6 3836
dee91e82
DE
3837static void
3838dw2_get_file_names_reader (const struct die_reader_specs *reader,
d521ce57 3839 const gdb_byte *info_ptr,
dee91e82
DE
3840 struct die_info *comp_unit_die,
3841 int has_children,
3842 void *data)
9291a0cd 3843{
dee91e82
DE
3844 struct dwarf2_cu *cu = reader->cu;
3845 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
3846 struct objfile *objfile = dwarf2_per_objfile->objfile;
094b34ac 3847 struct dwarf2_per_cu_data *lh_cu;
9291a0cd 3848 struct attribute *attr;
dee91e82 3849 int i;
7b9f3c50
DE
3850 void **slot;
3851 struct quick_file_names *qfn;
9291a0cd 3852
0186c6a7
DE
3853 gdb_assert (! this_cu->is_debug_types);
3854
07261596
TT
3855 /* Our callers never want to match partial units -- instead they
3856 will match the enclosing full CU. */
3857 if (comp_unit_die->tag == DW_TAG_partial_unit)
3858 {
3859 this_cu->v.quick->no_file_data = 1;
3860 return;
3861 }
3862
0186c6a7 3863 lh_cu = this_cu;
7b9f3c50 3864 slot = NULL;
dee91e82 3865
fff8551c 3866 line_header_up lh;
9c541725 3867 sect_offset line_offset {};
fff8551c 3868
dee91e82 3869 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
9291a0cd
TT
3870 if (attr)
3871 {
7b9f3c50
DE
3872 struct quick_file_names find_entry;
3873
9c541725 3874 line_offset = (sect_offset) DW_UNSND (attr);
7b9f3c50
DE
3875
3876 /* We may have already read in this line header (TU line header sharing).
3877 If we have we're done. */
094b34ac 3878 find_entry.hash.dwo_unit = cu->dwo_unit;
9c541725 3879 find_entry.hash.line_sect_off = line_offset;
7b9f3c50
DE
3880 slot = htab_find_slot (dwarf2_per_objfile->quick_file_names_table,
3881 &find_entry, INSERT);
3882 if (*slot != NULL)
3883 {
9a3c8263 3884 lh_cu->v.quick->file_names = (struct quick_file_names *) *slot;
dee91e82 3885 return;
7b9f3c50
DE
3886 }
3887
3019eac3 3888 lh = dwarf_decode_line_header (line_offset, cu);
9291a0cd
TT
3889 }
3890 if (lh == NULL)
3891 {
094b34ac 3892 lh_cu->v.quick->no_file_data = 1;
dee91e82 3893 return;
9291a0cd
TT
3894 }
3895
8d749320 3896 qfn = XOBNEW (&objfile->objfile_obstack, struct quick_file_names);
094b34ac 3897 qfn->hash.dwo_unit = cu->dwo_unit;
9c541725 3898 qfn->hash.line_sect_off = line_offset;
7b9f3c50
DE
3899 gdb_assert (slot != NULL);
3900 *slot = qfn;
9291a0cd 3901
d721ba37 3902 file_and_directory fnd = find_file_and_directory (comp_unit_die, cu);
9291a0cd 3903
fff8551c 3904 qfn->num_file_names = lh->file_names.size ();
8d749320 3905 qfn->file_names =
fff8551c
PA
3906 XOBNEWVEC (&objfile->objfile_obstack, const char *, lh->file_names.size ());
3907 for (i = 0; i < lh->file_names.size (); ++i)
3908 qfn->file_names[i] = file_full_name (i + 1, lh.get (), fnd.comp_dir);
7b9f3c50 3909 qfn->real_names = NULL;
9291a0cd 3910
094b34ac 3911 lh_cu->v.quick->file_names = qfn;
dee91e82
DE
3912}
3913
3914/* A helper for the "quick" functions which attempts to read the line
3915 table for THIS_CU. */
3916
3917static struct quick_file_names *
e4a48d9d 3918dw2_get_file_names (struct dwarf2_per_cu_data *this_cu)
dee91e82 3919{
0186c6a7
DE
3920 /* This should never be called for TUs. */
3921 gdb_assert (! this_cu->is_debug_types);
3922 /* Nor type unit groups. */
3923 gdb_assert (! IS_TYPE_UNIT_GROUP (this_cu));
f4dc4d17 3924
dee91e82
DE
3925 if (this_cu->v.quick->file_names != NULL)
3926 return this_cu->v.quick->file_names;
3927 /* If we know there is no line data, no point in looking again. */
3928 if (this_cu->v.quick->no_file_data)
3929 return NULL;
3930
0186c6a7 3931 init_cutu_and_read_dies_simple (this_cu, dw2_get_file_names_reader, NULL);
dee91e82
DE
3932
3933 if (this_cu->v.quick->no_file_data)
3934 return NULL;
3935 return this_cu->v.quick->file_names;
9291a0cd
TT
3936}
3937
3938/* A helper for the "quick" functions which computes and caches the
7b9f3c50 3939 real path for a given file name from the line table. */
2fdf6df6 3940
9291a0cd 3941static const char *
7b9f3c50
DE
3942dw2_get_real_path (struct objfile *objfile,
3943 struct quick_file_names *qfn, int index)
9291a0cd 3944{
7b9f3c50
DE
3945 if (qfn->real_names == NULL)
3946 qfn->real_names = OBSTACK_CALLOC (&objfile->objfile_obstack,
26f2dc30 3947 qfn->num_file_names, const char *);
9291a0cd 3948
7b9f3c50 3949 if (qfn->real_names[index] == NULL)
14278e1f 3950 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]).release ();
9291a0cd 3951
7b9f3c50 3952 return qfn->real_names[index];
9291a0cd
TT
3953}
3954
3955static struct symtab *
3956dw2_find_last_source_symtab (struct objfile *objfile)
3957{
43f3e411 3958 struct compunit_symtab *cust;
9291a0cd 3959 int index;
ae2de4f8 3960
9291a0cd
TT
3961 dw2_setup (objfile);
3962 index = dwarf2_per_objfile->n_comp_units - 1;
43f3e411
DE
3963 cust = dw2_instantiate_symtab (dw2_get_cutu (index));
3964 if (cust == NULL)
3965 return NULL;
3966 return compunit_primary_filetab (cust);
9291a0cd
TT
3967}
3968
7b9f3c50
DE
3969/* Traversal function for dw2_forget_cached_source_info. */
3970
3971static int
3972dw2_free_cached_file_names (void **slot, void *info)
9291a0cd 3973{
7b9f3c50 3974 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
9291a0cd 3975
7b9f3c50 3976 if (file_data->real_names)
9291a0cd 3977 {
7b9f3c50 3978 int i;
9291a0cd 3979
7b9f3c50 3980 for (i = 0; i < file_data->num_file_names; ++i)
9291a0cd 3981 {
7b9f3c50
DE
3982 xfree ((void*) file_data->real_names[i]);
3983 file_data->real_names[i] = NULL;
9291a0cd
TT
3984 }
3985 }
7b9f3c50
DE
3986
3987 return 1;
3988}
3989
3990static void
3991dw2_forget_cached_source_info (struct objfile *objfile)
3992{
3993 dw2_setup (objfile);
3994
3995 htab_traverse_noresize (dwarf2_per_objfile->quick_file_names_table,
3996 dw2_free_cached_file_names, NULL);
9291a0cd
TT
3997}
3998
f8eba3c6
TT
3999/* Helper function for dw2_map_symtabs_matching_filename that expands
4000 the symtabs and calls the iterator. */
4001
4002static int
4003dw2_map_expand_apply (struct objfile *objfile,
4004 struct dwarf2_per_cu_data *per_cu,
f5b95b50 4005 const char *name, const char *real_path,
14bc53a8 4006 gdb::function_view<bool (symtab *)> callback)
f8eba3c6 4007{
43f3e411 4008 struct compunit_symtab *last_made = objfile->compunit_symtabs;
f8eba3c6
TT
4009
4010 /* Don't visit already-expanded CUs. */
43f3e411 4011 if (per_cu->v.quick->compunit_symtab)
f8eba3c6
TT
4012 return 0;
4013
4014 /* This may expand more than one symtab, and we want to iterate over
4015 all of them. */
a0f42c21 4016 dw2_instantiate_symtab (per_cu);
f8eba3c6 4017
14bc53a8
PA
4018 return iterate_over_some_symtabs (name, real_path, objfile->compunit_symtabs,
4019 last_made, callback);
f8eba3c6
TT
4020}
4021
4022/* Implementation of the map_symtabs_matching_filename method. */
4023
14bc53a8
PA
4024static bool
4025dw2_map_symtabs_matching_filename
4026 (struct objfile *objfile, const char *name, const char *real_path,
4027 gdb::function_view<bool (symtab *)> callback)
9291a0cd
TT
4028{
4029 int i;
c011a4f4 4030 const char *name_basename = lbasename (name);
9291a0cd
TT
4031
4032 dw2_setup (objfile);
ae2de4f8 4033
848e3e78
DE
4034 /* The rule is CUs specify all the files, including those used by
4035 any TU, so there's no need to scan TUs here. */
f4dc4d17 4036
848e3e78 4037 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
9291a0cd
TT
4038 {
4039 int j;
8832e7e3 4040 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
7b9f3c50 4041 struct quick_file_names *file_data;
9291a0cd 4042
3d7bb9d9 4043 /* We only need to look at symtabs not already expanded. */
43f3e411 4044 if (per_cu->v.quick->compunit_symtab)
9291a0cd
TT
4045 continue;
4046
e4a48d9d 4047 file_data = dw2_get_file_names (per_cu);
7b9f3c50 4048 if (file_data == NULL)
9291a0cd
TT
4049 continue;
4050
7b9f3c50 4051 for (j = 0; j < file_data->num_file_names; ++j)
9291a0cd 4052 {
7b9f3c50 4053 const char *this_name = file_data->file_names[j];
da235a7c 4054 const char *this_real_name;
9291a0cd 4055
af529f8f 4056 if (compare_filenames_for_search (this_name, name))
9291a0cd 4057 {
f5b95b50 4058 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
14bc53a8
PA
4059 callback))
4060 return true;
288e77a7 4061 continue;
4aac40c8 4062 }
9291a0cd 4063
c011a4f4
DE
4064 /* Before we invoke realpath, which can get expensive when many
4065 files are involved, do a quick comparison of the basenames. */
4066 if (! basenames_may_differ
4067 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
4068 continue;
4069
da235a7c
JK
4070 this_real_name = dw2_get_real_path (objfile, file_data, j);
4071 if (compare_filenames_for_search (this_real_name, name))
9291a0cd 4072 {
da235a7c 4073 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
14bc53a8
PA
4074 callback))
4075 return true;
288e77a7 4076 continue;
da235a7c 4077 }
9291a0cd 4078
da235a7c
JK
4079 if (real_path != NULL)
4080 {
af529f8f
JK
4081 gdb_assert (IS_ABSOLUTE_PATH (real_path));
4082 gdb_assert (IS_ABSOLUTE_PATH (name));
7b9f3c50 4083 if (this_real_name != NULL
af529f8f 4084 && FILENAME_CMP (real_path, this_real_name) == 0)
9291a0cd 4085 {
f5b95b50 4086 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
14bc53a8
PA
4087 callback))
4088 return true;
288e77a7 4089 continue;
9291a0cd
TT
4090 }
4091 }
4092 }
4093 }
4094
14bc53a8 4095 return false;
9291a0cd
TT
4096}
4097
da51c347
DE
4098/* Struct used to manage iterating over all CUs looking for a symbol. */
4099
4100struct dw2_symtab_iterator
9291a0cd 4101{
da51c347
DE
4102 /* The internalized form of .gdb_index. */
4103 struct mapped_index *index;
4104 /* If non-zero, only look for symbols that match BLOCK_INDEX. */
4105 int want_specific_block;
4106 /* One of GLOBAL_BLOCK or STATIC_BLOCK.
4107 Unused if !WANT_SPECIFIC_BLOCK. */
4108 int block_index;
4109 /* The kind of symbol we're looking for. */
4110 domain_enum domain;
4111 /* The list of CUs from the index entry of the symbol,
4112 or NULL if not found. */
4113 offset_type *vec;
4114 /* The next element in VEC to look at. */
4115 int next;
4116 /* The number of elements in VEC, or zero if there is no match. */
4117 int length;
8943b874
DE
4118 /* Have we seen a global version of the symbol?
4119 If so we can ignore all further global instances.
4120 This is to work around gold/15646, inefficient gold-generated
4121 indices. */
4122 int global_seen;
da51c347 4123};
9291a0cd 4124
da51c347
DE
4125/* Initialize the index symtab iterator ITER.
4126 If WANT_SPECIFIC_BLOCK is non-zero, only look for symbols
4127 in block BLOCK_INDEX. Otherwise BLOCK_INDEX is ignored. */
2fdf6df6 4128
9291a0cd 4129static void
da51c347
DE
4130dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
4131 struct mapped_index *index,
4132 int want_specific_block,
4133 int block_index,
4134 domain_enum domain,
4135 const char *name)
4136{
4137 iter->index = index;
4138 iter->want_specific_block = want_specific_block;
4139 iter->block_index = block_index;
4140 iter->domain = domain;
4141 iter->next = 0;
8943b874 4142 iter->global_seen = 0;
da51c347
DE
4143
4144 if (find_slot_in_mapped_hash (index, name, &iter->vec))
4145 iter->length = MAYBE_SWAP (*iter->vec);
4146 else
4147 {
4148 iter->vec = NULL;
4149 iter->length = 0;
4150 }
4151}
4152
4153/* Return the next matching CU or NULL if there are no more. */
4154
4155static struct dwarf2_per_cu_data *
4156dw2_symtab_iter_next (struct dw2_symtab_iterator *iter)
4157{
4158 for ( ; iter->next < iter->length; ++iter->next)
4159 {
4160 offset_type cu_index_and_attrs =
4161 MAYBE_SWAP (iter->vec[iter->next + 1]);
4162 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
3190f0c6 4163 struct dwarf2_per_cu_data *per_cu;
da51c347
DE
4164 int want_static = iter->block_index != GLOBAL_BLOCK;
4165 /* This value is only valid for index versions >= 7. */
4166 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
4167 gdb_index_symbol_kind symbol_kind =
4168 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
4169 /* Only check the symbol attributes if they're present.
4170 Indices prior to version 7 don't record them,
4171 and indices >= 7 may elide them for certain symbols
4172 (gold does this). */
4173 int attrs_valid =
4174 (iter->index->version >= 7
4175 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
4176
3190f0c6
DE
4177 /* Don't crash on bad data. */
4178 if (cu_index >= (dwarf2_per_objfile->n_comp_units
4179 + dwarf2_per_objfile->n_type_units))
4180 {
4181 complaint (&symfile_complaints,
4182 _(".gdb_index entry has bad CU index"
4262abfb
JK
4183 " [in module %s]"),
4184 objfile_name (dwarf2_per_objfile->objfile));
3190f0c6
DE
4185 continue;
4186 }
4187
8832e7e3 4188 per_cu = dw2_get_cutu (cu_index);
3190f0c6 4189
da51c347 4190 /* Skip if already read in. */
43f3e411 4191 if (per_cu->v.quick->compunit_symtab)
da51c347
DE
4192 continue;
4193
8943b874
DE
4194 /* Check static vs global. */
4195 if (attrs_valid)
4196 {
4197 if (iter->want_specific_block
4198 && want_static != is_static)
4199 continue;
4200 /* Work around gold/15646. */
4201 if (!is_static && iter->global_seen)
4202 continue;
4203 if (!is_static)
4204 iter->global_seen = 1;
4205 }
da51c347
DE
4206
4207 /* Only check the symbol's kind if it has one. */
4208 if (attrs_valid)
4209 {
4210 switch (iter->domain)
4211 {
4212 case VAR_DOMAIN:
4213 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
4214 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
4215 /* Some types are also in VAR_DOMAIN. */
4216 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
4217 continue;
4218 break;
4219 case STRUCT_DOMAIN:
4220 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
4221 continue;
4222 break;
4223 case LABEL_DOMAIN:
4224 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
4225 continue;
4226 break;
4227 default:
4228 break;
4229 }
4230 }
4231
4232 ++iter->next;
4233 return per_cu;
4234 }
4235
4236 return NULL;
4237}
4238
43f3e411 4239static struct compunit_symtab *
da51c347
DE
4240dw2_lookup_symbol (struct objfile *objfile, int block_index,
4241 const char *name, domain_enum domain)
9291a0cd 4242{
43f3e411 4243 struct compunit_symtab *stab_best = NULL;
156942c7
DE
4244 struct mapped_index *index;
4245
9291a0cd
TT
4246 dw2_setup (objfile);
4247
b5ec771e
PA
4248 lookup_name_info lookup_name (name, symbol_name_match_type::FULL);
4249
156942c7
DE
4250 index = dwarf2_per_objfile->index_table;
4251
da51c347 4252 /* index is NULL if OBJF_READNOW. */
156942c7 4253 if (index)
9291a0cd 4254 {
da51c347
DE
4255 struct dw2_symtab_iterator iter;
4256 struct dwarf2_per_cu_data *per_cu;
4257
4258 dw2_symtab_iter_init (&iter, index, 1, block_index, domain, name);
9291a0cd 4259
da51c347 4260 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
9291a0cd 4261 {
b2e2f908 4262 struct symbol *sym, *with_opaque = NULL;
43f3e411
DE
4263 struct compunit_symtab *stab = dw2_instantiate_symtab (per_cu);
4264 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
f194fefb 4265 struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
da51c347 4266
b2e2f908
DE
4267 sym = block_find_symbol (block, name, domain,
4268 block_find_non_opaque_type_preferred,
4269 &with_opaque);
4270
da51c347
DE
4271 /* Some caution must be observed with overloaded functions
4272 and methods, since the index will not contain any overload
4273 information (but NAME might contain it). */
da51c347 4274
b2e2f908 4275 if (sym != NULL
b5ec771e 4276 && SYMBOL_MATCHES_SEARCH_NAME (sym, lookup_name))
b2e2f908
DE
4277 return stab;
4278 if (with_opaque != NULL
b5ec771e 4279 && SYMBOL_MATCHES_SEARCH_NAME (with_opaque, lookup_name))
b2e2f908 4280 stab_best = stab;
da51c347
DE
4281
4282 /* Keep looking through other CUs. */
9291a0cd
TT
4283 }
4284 }
9291a0cd 4285
da51c347 4286 return stab_best;
9291a0cd
TT
4287}
4288
4289static void
4290dw2_print_stats (struct objfile *objfile)
4291{
e4a48d9d 4292 int i, total, count;
9291a0cd
TT
4293
4294 dw2_setup (objfile);
e4a48d9d 4295 total = dwarf2_per_objfile->n_comp_units + dwarf2_per_objfile->n_type_units;
9291a0cd 4296 count = 0;
e4a48d9d 4297 for (i = 0; i < total; ++i)
9291a0cd 4298 {
8832e7e3 4299 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
9291a0cd 4300
43f3e411 4301 if (!per_cu->v.quick->compunit_symtab)
9291a0cd
TT
4302 ++count;
4303 }
e4a48d9d 4304 printf_filtered (_(" Number of read CUs: %d\n"), total - count);
9291a0cd
TT
4305 printf_filtered (_(" Number of unread CUs: %d\n"), count);
4306}
4307
779bd270
DE
4308/* This dumps minimal information about the index.
4309 It is called via "mt print objfiles".
4310 One use is to verify .gdb_index has been loaded by the
4311 gdb.dwarf2/gdb-index.exp testcase. */
4312
9291a0cd
TT
4313static void
4314dw2_dump (struct objfile *objfile)
4315{
779bd270
DE
4316 dw2_setup (objfile);
4317 gdb_assert (dwarf2_per_objfile->using_index);
4318 printf_filtered (".gdb_index:");
4319 if (dwarf2_per_objfile->index_table != NULL)
4320 {
4321 printf_filtered (" version %d\n",
4322 dwarf2_per_objfile->index_table->version);
4323 }
4324 else
4325 printf_filtered (" faked for \"readnow\"\n");
4326 printf_filtered ("\n");
9291a0cd
TT
4327}
4328
4329static void
3189cb12
DE
4330dw2_relocate (struct objfile *objfile,
4331 const struct section_offsets *new_offsets,
4332 const struct section_offsets *delta)
9291a0cd
TT
4333{
4334 /* There's nothing to relocate here. */
4335}
4336
4337static void
4338dw2_expand_symtabs_for_function (struct objfile *objfile,
4339 const char *func_name)
4340{
da51c347
DE
4341 struct mapped_index *index;
4342
4343 dw2_setup (objfile);
4344
4345 index = dwarf2_per_objfile->index_table;
4346
4347 /* index is NULL if OBJF_READNOW. */
4348 if (index)
4349 {
4350 struct dw2_symtab_iterator iter;
4351 struct dwarf2_per_cu_data *per_cu;
4352
4353 /* Note: It doesn't matter what we pass for block_index here. */
4354 dw2_symtab_iter_init (&iter, index, 0, GLOBAL_BLOCK, VAR_DOMAIN,
4355 func_name);
4356
4357 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
4358 dw2_instantiate_symtab (per_cu);
4359 }
9291a0cd
TT
4360}
4361
4362static void
4363dw2_expand_all_symtabs (struct objfile *objfile)
4364{
4365 int i;
4366
4367 dw2_setup (objfile);
1fd400ff
TT
4368
4369 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
d467dd73 4370 + dwarf2_per_objfile->n_type_units); ++i)
9291a0cd 4371 {
8832e7e3 4372 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
9291a0cd 4373
a0f42c21 4374 dw2_instantiate_symtab (per_cu);
9291a0cd
TT
4375 }
4376}
4377
4378static void
652a8996
JK
4379dw2_expand_symtabs_with_fullname (struct objfile *objfile,
4380 const char *fullname)
9291a0cd
TT
4381{
4382 int i;
4383
4384 dw2_setup (objfile);
d4637a04
DE
4385
4386 /* We don't need to consider type units here.
4387 This is only called for examining code, e.g. expand_line_sal.
4388 There can be an order of magnitude (or more) more type units
4389 than comp units, and we avoid them if we can. */
4390
4391 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
9291a0cd
TT
4392 {
4393 int j;
8832e7e3 4394 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
7b9f3c50 4395 struct quick_file_names *file_data;
9291a0cd 4396
3d7bb9d9 4397 /* We only need to look at symtabs not already expanded. */
43f3e411 4398 if (per_cu->v.quick->compunit_symtab)
9291a0cd
TT
4399 continue;
4400
e4a48d9d 4401 file_data = dw2_get_file_names (per_cu);
7b9f3c50 4402 if (file_data == NULL)
9291a0cd
TT
4403 continue;
4404
7b9f3c50 4405 for (j = 0; j < file_data->num_file_names; ++j)
9291a0cd 4406 {
652a8996
JK
4407 const char *this_fullname = file_data->file_names[j];
4408
4409 if (filename_cmp (this_fullname, fullname) == 0)
9291a0cd 4410 {
a0f42c21 4411 dw2_instantiate_symtab (per_cu);
9291a0cd
TT
4412 break;
4413 }
4414 }
4415 }
4416}
4417
9291a0cd 4418static void
ade7ed9e 4419dw2_map_matching_symbols (struct objfile *objfile,
fe978cb0 4420 const char * name, domain_enum domain,
ade7ed9e 4421 int global,
40658b94
PH
4422 int (*callback) (struct block *,
4423 struct symbol *, void *),
b5ec771e 4424 void *data, symbol_name_match_type match,
2edb89d3 4425 symbol_compare_ftype *ordered_compare)
9291a0cd 4426{
40658b94 4427 /* Currently unimplemented; used for Ada. The function can be called if the
a9e6a4bb
JK
4428 current language is Ada for a non-Ada objfile using GNU index. As Ada
4429 does not look for non-Ada symbols this function should just return. */
9291a0cd
TT
4430}
4431
b5ec771e
PA
4432/* Symbol name matcher for .gdb_index names.
4433
4434 Symbol names in .gdb_index have a few particularities:
4435
4436 - There's no indication of which is the language of each symbol.
4437
4438 Since each language has its own symbol name matching algorithm,
4439 and we don't know which language is the right one, we must match
3f563c84
PA
4440 each symbol against all languages. This would be a potential
4441 performance problem if it were not mitigated by the
4442 mapped_index::name_components lookup table, which significantly
4443 reduces the number of times we need to call into this matcher,
4444 making it a non-issue.
b5ec771e
PA
4445
4446 - Symbol names in the index have no overload (parameter)
4447 information. I.e., in C++, "foo(int)" and "foo(long)" both
4448 appear as "foo" in the index, for example.
4449
4450 This means that the lookup names passed to the symbol name
4451 matcher functions must have no parameter information either
4452 because (e.g.) symbol search name "foo" does not match
4453 lookup-name "foo(int)" [while swapping search name for lookup
4454 name would match].
4455*/
4456class gdb_index_symbol_name_matcher
4457{
4458public:
4459 /* Prepares the vector of comparison functions for LOOKUP_NAME. */
4460 gdb_index_symbol_name_matcher (const lookup_name_info &lookup_name);
4461
4462 /* Walk all the matcher routines and match SYMBOL_NAME against them.
4463 Returns true if any matcher matches. */
4464 bool matches (const char *symbol_name);
4465
4466private:
4467 /* A reference to the lookup name we're matching against. */
4468 const lookup_name_info &m_lookup_name;
4469
4470 /* A vector holding all the different symbol name matchers, for all
4471 languages. */
4472 std::vector<symbol_name_matcher_ftype *> m_symbol_name_matcher_funcs;
4473};
4474
4475gdb_index_symbol_name_matcher::gdb_index_symbol_name_matcher
4476 (const lookup_name_info &lookup_name)
4477 : m_lookup_name (lookup_name)
4478{
4479 /* Prepare the vector of comparison functions upfront, to avoid
4480 doing the same work for each symbol. Care is taken to avoid
4481 matching with the same matcher more than once if/when multiple
4482 languages use the same matcher function. */
4483 auto &matchers = m_symbol_name_matcher_funcs;
4484 matchers.reserve (nr_languages);
4485
4486 matchers.push_back (default_symbol_name_matcher);
4487
4488 for (int i = 0; i < nr_languages; i++)
4489 {
4490 const language_defn *lang = language_def ((enum language) i);
4491 if (lang->la_get_symbol_name_matcher != NULL)
4492 {
4493 symbol_name_matcher_ftype *name_matcher
4494 = lang->la_get_symbol_name_matcher (m_lookup_name);
4495
4496 /* Don't insert the same comparison routine more than once.
4497 Note that we do this linear walk instead of a cheaper
4498 sorted insert, or use a std::set or something like that,
4499 because relative order of function addresses is not
4500 stable. This is not a problem in practice because the
4501 number of supported languages is low, and the cost here
4502 is tiny compared to the number of searches we'll do
4503 afterwards using this object. */
4504 if (std::find (matchers.begin (), matchers.end (), name_matcher)
4505 == matchers.end ())
4506 matchers.push_back (name_matcher);
4507 }
4508 }
4509}
4510
4511bool
4512gdb_index_symbol_name_matcher::matches (const char *symbol_name)
4513{
4514 for (auto matches_name : m_symbol_name_matcher_funcs)
4515 if (matches_name (symbol_name, m_lookup_name, NULL))
4516 return true;
4517
4518 return false;
4519}
4520
e1ef7d7a
PA
4521/* Starting from a search name, return the string that finds the upper
4522 bound of all strings that start with SEARCH_NAME in a sorted name
4523 list. Returns the empty string to indicate that the upper bound is
4524 the end of the list. */
4525
4526static std::string
4527make_sort_after_prefix_name (const char *search_name)
4528{
4529 /* When looking to complete "func", we find the upper bound of all
4530 symbols that start with "func" by looking for where we'd insert
4531 the closest string that would follow "func" in lexicographical
4532 order. Usually, that's "func"-with-last-character-incremented,
4533 i.e. "fund". Mind non-ASCII characters, though. Usually those
4534 will be UTF-8 multi-byte sequences, but we can't be certain.
4535 Especially mind the 0xff character, which is a valid character in
4536 non-UTF-8 source character sets (e.g. Latin1 'ÿ'), and we can't
4537 rule out compilers allowing it in identifiers. Note that
4538 conveniently, strcmp/strcasecmp are specified to compare
4539 characters interpreted as unsigned char. So what we do is treat
4540 the whole string as a base 256 number composed of a sequence of
4541 base 256 "digits" and add 1 to it. I.e., adding 1 to 0xff wraps
4542 to 0, and carries 1 to the following more-significant position.
4543 If the very first character in SEARCH_NAME ends up incremented
4544 and carries/overflows, then the upper bound is the end of the
4545 list. The string after the empty string is also the empty
4546 string.
4547
4548 Some examples of this operation:
4549
4550 SEARCH_NAME => "+1" RESULT
4551
4552 "abc" => "abd"
4553 "ab\xff" => "ac"
4554 "\xff" "a" "\xff" => "\xff" "b"
4555 "\xff" => ""
4556 "\xff\xff" => ""
4557 "" => ""
4558
4559 Then, with these symbols for example:
4560
4561 func
4562 func1
4563 fund
4564
4565 completing "func" looks for symbols between "func" and
4566 "func"-with-last-character-incremented, i.e. "fund" (exclusive),
4567 which finds "func" and "func1", but not "fund".
4568
4569 And with:
4570
4571 funcÿ (Latin1 'ÿ' [0xff])
4572 funcÿ1
4573 fund
4574
4575 completing "funcÿ" looks for symbols between "funcÿ" and "fund"
4576 (exclusive), which finds "funcÿ" and "funcÿ1", but not "fund".
4577
4578 And with:
4579
4580 ÿÿ (Latin1 'ÿ' [0xff])
4581 ÿÿ1
4582
4583 completing "ÿ" or "ÿÿ" looks for symbols between between "ÿÿ" and
4584 the end of the list.
4585 */
4586 std::string after = search_name;
4587 while (!after.empty () && (unsigned char) after.back () == 0xff)
4588 after.pop_back ();
4589 if (!after.empty ())
4590 after.back () = (unsigned char) after.back () + 1;
4591 return after;
4592}
4593
5c58de74 4594/* See declaration. */
61d96d7e 4595
5c58de74
PA
4596std::pair<std::vector<name_component>::const_iterator,
4597 std::vector<name_component>::const_iterator>
4598mapped_index::find_name_components_bounds
4599 (const lookup_name_info &lookup_name_without_params) const
3f563c84 4600{
5c58de74
PA
4601 auto *name_cmp
4602 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
3f563c84
PA
4603
4604 const char *cplus
c62446b1 4605 = lookup_name_without_params.cplus ().lookup_name ().c_str ();
9291a0cd 4606
3f563c84
PA
4607 /* Comparison function object for lower_bound that matches against a
4608 given symbol name. */
4609 auto lookup_compare_lower = [&] (const name_component &elem,
4610 const char *name)
4611 {
5c58de74 4612 const char *elem_qualified = this->symbol_name_at (elem.idx);
3f563c84
PA
4613 const char *elem_name = elem_qualified + elem.name_offset;
4614 return name_cmp (elem_name, name) < 0;
4615 };
4616
4617 /* Comparison function object for upper_bound that matches against a
4618 given symbol name. */
4619 auto lookup_compare_upper = [&] (const char *name,
4620 const name_component &elem)
4621 {
5c58de74 4622 const char *elem_qualified = this->symbol_name_at (elem.idx);
3f563c84
PA
4623 const char *elem_name = elem_qualified + elem.name_offset;
4624 return name_cmp (name, elem_name) < 0;
4625 };
4626
5c58de74
PA
4627 auto begin = this->name_components.begin ();
4628 auto end = this->name_components.end ();
3f563c84
PA
4629
4630 /* Find the lower bound. */
4631 auto lower = [&] ()
4632 {
5c58de74 4633 if (lookup_name_without_params.completion_mode () && cplus[0] == '\0')
3f563c84
PA
4634 return begin;
4635 else
4636 return std::lower_bound (begin, end, cplus, lookup_compare_lower);
4637 } ();
4638
4639 /* Find the upper bound. */
4640 auto upper = [&] ()
4641 {
5c58de74 4642 if (lookup_name_without_params.completion_mode ())
3f563c84 4643 {
e1ef7d7a
PA
4644 /* In completion mode, we want UPPER to point past all
4645 symbols names that have the same prefix. I.e., with
4646 these symbols, and completing "func":
4647
4648 function << lower bound
4649 function1
4650 other_function << upper bound
4651
4652 We find the upper bound by looking for the insertion
4653 point of "func"-with-last-character-incremented,
4654 i.e. "fund". */
4655 std::string after = make_sort_after_prefix_name (cplus);
4656 if (after.empty ())
3f563c84 4657 return end;
e6b2f5ef
PA
4658 return std::lower_bound (lower, end, after.c_str (),
4659 lookup_compare_lower);
3f563c84
PA
4660 }
4661 else
4662 return std::upper_bound (lower, end, cplus, lookup_compare_upper);
4663 } ();
4664
5c58de74
PA
4665 return {lower, upper};
4666}
4667
4668/* See declaration. */
4669
4670void
4671mapped_index::build_name_components ()
4672{
4673 if (!this->name_components.empty ())
4674 return;
4675
4676 this->name_components_casing = case_sensitivity;
4677 auto *name_cmp
4678 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
4679
4680 /* The code below only knows how to break apart components of C++
4681 symbol names (and other languages that use '::' as
4682 namespace/module separator). If we add support for wild matching
4683 to some language that uses some other operator (E.g., Ada, Go and
4684 D use '.'), then we'll need to try splitting the symbol name
4685 according to that language too. Note that Ada does support wild
4686 matching, but doesn't currently support .gdb_index. */
f00a2de2 4687 for (offset_type idx = 0; idx < this->symbol_table.size (); ++idx)
5c58de74 4688 {
f00a2de2 4689 auto &bucket = this->symbol_table[idx];
5c58de74 4690
f00a2de2 4691 if (bucket.name == 0 && bucket.vec == 0)
5c58de74
PA
4692 continue;
4693
4694 const char *name = this->symbol_name_at (idx);
4695
4696 /* Add each name component to the name component table. */
4697 unsigned int previous_len = 0;
4698 for (unsigned int current_len = cp_find_first_component (name);
4699 name[current_len] != '\0';
4700 current_len += cp_find_first_component (name + current_len))
4701 {
4702 gdb_assert (name[current_len] == ':');
4703 this->name_components.push_back ({previous_len, idx});
4704 /* Skip the '::'. */
4705 current_len += 2;
4706 previous_len = current_len;
4707 }
4708 this->name_components.push_back ({previous_len, idx});
4709 }
4710
4711 /* Sort name_components elements by name. */
4712 auto name_comp_compare = [&] (const name_component &left,
4713 const name_component &right)
4714 {
4715 const char *left_qualified = this->symbol_name_at (left.idx);
4716 const char *right_qualified = this->symbol_name_at (right.idx);
4717
4718 const char *left_name = left_qualified + left.name_offset;
4719 const char *right_name = right_qualified + right.name_offset;
4720
4721 return name_cmp (left_name, right_name) < 0;
4722 };
4723
4724 std::sort (this->name_components.begin (),
4725 this->name_components.end (),
4726 name_comp_compare);
4727}
4728
4729/* Helper for dw2_expand_symtabs_matching that works with a
4730 mapped_index instead of the containing objfile. This is split to a
4731 separate function in order to be able to unit test the
4732 name_components matching using a mock mapped_index. For each
4733 symbol name that matches, calls MATCH_CALLBACK, passing it the
4734 symbol's index in the mapped_index symbol table. */
4735
4736static void
4737dw2_expand_symtabs_matching_symbol
4738 (mapped_index &index,
4739 const lookup_name_info &lookup_name_in,
4740 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4741 enum search_domain kind,
4742 gdb::function_view<void (offset_type)> match_callback)
4743{
4744 lookup_name_info lookup_name_without_params
4745 = lookup_name_in.make_ignore_params ();
4746 gdb_index_symbol_name_matcher lookup_name_matcher
4747 (lookup_name_without_params);
4748
4749 /* Build the symbol name component sorted vector, if we haven't
4750 yet. */
4751 index.build_name_components ();
4752
4753 auto bounds = index.find_name_components_bounds (lookup_name_without_params);
4754
3f563c84
PA
4755 /* Now for each symbol name in range, check to see if we have a name
4756 match, and if so, call the MATCH_CALLBACK callback. */
4757
4758 /* The same symbol may appear more than once in the range though.
4759 E.g., if we're looking for symbols that complete "w", and we have
4760 a symbol named "w1::w2", we'll find the two name components for
4761 that same symbol in the range. To be sure we only call the
4762 callback once per symbol, we first collect the symbol name
4763 indexes that matched in a temporary vector and ignore
4764 duplicates. */
4765 std::vector<offset_type> matches;
5c58de74 4766 matches.reserve (std::distance (bounds.first, bounds.second));
3f563c84 4767
5c58de74 4768 for (; bounds.first != bounds.second; ++bounds.first)
3f563c84 4769 {
5c58de74 4770 const char *qualified = index.symbol_name_at (bounds.first->idx);
3f563c84
PA
4771
4772 if (!lookup_name_matcher.matches (qualified)
4773 || (symbol_matcher != NULL && !symbol_matcher (qualified)))
9291a0cd
TT
4774 continue;
4775
5c58de74 4776 matches.push_back (bounds.first->idx);
3f563c84
PA
4777 }
4778
4779 std::sort (matches.begin (), matches.end ());
4780
4781 /* Finally call the callback, once per match. */
4782 ULONGEST prev = -1;
4783 for (offset_type idx : matches)
4784 {
4785 if (prev != idx)
4786 {
4787 match_callback (idx);
4788 prev = idx;
4789 }
4790 }
4791
4792 /* Above we use a type wider than idx's for 'prev', since 0 and
4793 (offset_type)-1 are both possible values. */
4794 static_assert (sizeof (prev) > sizeof (offset_type), "");
4795}
4796
c62446b1
PA
4797#if GDB_SELF_TEST
4798
4799namespace selftests { namespace dw2_expand_symtabs_matching {
4800
4801/* A wrapper around mapped_index that builds a mock mapped_index, from
4802 the symbol list passed as parameter to the constructor. */
4803class mock_mapped_index
4804{
4805public:
4806 template<size_t N>
4807 mock_mapped_index (const char *(&symbols)[N])
4808 : mock_mapped_index (symbols, N)
4809 {}
4810
4811 /* Access the built index. */
4812 mapped_index &index ()
4813 { return m_index; }
4814
4815 /* Disable copy. */
4816 mock_mapped_index(const mock_mapped_index &) = delete;
4817 void operator= (const mock_mapped_index &) = delete;
4818
4819private:
4820 mock_mapped_index (const char **symbols, size_t symbols_size)
4821 {
4822 /* No string can live at offset zero. Add a dummy entry. */
4823 obstack_grow_str0 (&m_constant_pool, "");
4824
4825 for (size_t i = 0; i < symbols_size; i++)
4826 {
4827 const char *sym = symbols[i];
4828 size_t offset = obstack_object_size (&m_constant_pool);
4829 obstack_grow_str0 (&m_constant_pool, sym);
f00a2de2 4830 m_symbol_table.push_back ({offset, 0});
c62446b1
PA
4831 };
4832
4833 m_index.constant_pool = (const char *) obstack_base (&m_constant_pool);
f00a2de2 4834 m_index.symbol_table = m_symbol_table;
c62446b1
PA
4835 }
4836
4837public:
4838 /* The built mapped_index. */
4839 mapped_index m_index{};
4840
4841 /* The storage that the built mapped_index uses for symbol and
4842 constant pool tables. */
f00a2de2 4843 std::vector<mapped_index::symbol_table_slot> m_symbol_table;
c62446b1
PA
4844 auto_obstack m_constant_pool;
4845};
4846
4847/* Convenience function that converts a NULL pointer to a "<null>"
4848 string, to pass to print routines. */
4849
4850static const char *
4851string_or_null (const char *str)
4852{
4853 return str != NULL ? str : "<null>";
4854}
4855
4856/* Check if a lookup_name_info built from
4857 NAME/MATCH_TYPE/COMPLETION_MODE matches the symbols in the mock
4858 index. EXPECTED_LIST is the list of expected matches, in expected
4859 matching order. If no match expected, then an empty list is
4860 specified. Returns true on success. On failure prints a warning
4861 indicating the file:line that failed, and returns false. */
4862
4863static bool
4864check_match (const char *file, int line,
4865 mock_mapped_index &mock_index,
4866 const char *name, symbol_name_match_type match_type,
4867 bool completion_mode,
4868 std::initializer_list<const char *> expected_list)
4869{
4870 lookup_name_info lookup_name (name, match_type, completion_mode);
4871
4872 bool matched = true;
4873
4874 auto mismatch = [&] (const char *expected_str,
4875 const char *got)
4876 {
4877 warning (_("%s:%d: match_type=%s, looking-for=\"%s\", "
4878 "expected=\"%s\", got=\"%s\"\n"),
4879 file, line,
4880 (match_type == symbol_name_match_type::FULL
4881 ? "FULL" : "WILD"),
4882 name, string_or_null (expected_str), string_or_null (got));
4883 matched = false;
4884 };
4885
4886 auto expected_it = expected_list.begin ();
4887 auto expected_end = expected_list.end ();
4888
4889 dw2_expand_symtabs_matching_symbol (mock_index.index (), lookup_name,
4890 NULL, ALL_DOMAIN,
4891 [&] (offset_type idx)
4892 {
4893 const char *matched_name = mock_index.index ().symbol_name_at (idx);
4894 const char *expected_str
4895 = expected_it == expected_end ? NULL : *expected_it++;
4896
4897 if (expected_str == NULL || strcmp (expected_str, matched_name) != 0)
4898 mismatch (expected_str, matched_name);
4899 });
4900
4901 const char *expected_str
4902 = expected_it == expected_end ? NULL : *expected_it++;
4903 if (expected_str != NULL)
4904 mismatch (expected_str, NULL);
4905
4906 return matched;
4907}
4908
4909/* The symbols added to the mock mapped_index for testing (in
4910 canonical form). */
4911static const char *test_symbols[] = {
4912 "function",
4913 "std::bar",
4914 "std::zfunction",
4915 "std::zfunction2",
4916 "w1::w2",
4917 "ns::foo<char*>",
4918 "ns::foo<int>",
4919 "ns::foo<long>",
a20714ff
PA
4920 "ns2::tmpl<int>::foo2",
4921 "(anonymous namespace)::A::B::C",
c62446b1 4922
e1ef7d7a
PA
4923 /* These are used to check that the increment-last-char in the
4924 matching algorithm for completion doesn't match "t1_fund" when
4925 completing "t1_func". */
4926 "t1_func",
4927 "t1_func1",
4928 "t1_fund",
4929 "t1_fund1",
4930
4931 /* A UTF-8 name with multi-byte sequences to make sure that
4932 cp-name-parser understands this as a single identifier ("função"
4933 is "function" in PT). */
4934 u8"u8função",
4935
4936 /* \377 (0xff) is Latin1 'ÿ'. */
4937 "yfunc\377",
4938
4939 /* \377 (0xff) is Latin1 'ÿ'. */
4940 "\377",
4941 "\377\377123",
4942
c62446b1
PA
4943 /* A name with all sorts of complications. Starts with "z" to make
4944 it easier for the completion tests below. */
4945#define Z_SYM_NAME \
4946 "z::std::tuple<(anonymous namespace)::ui*, std::bar<(anonymous namespace)::ui> >" \
4947 "::tuple<(anonymous namespace)::ui*, " \
4948 "std::default_delete<(anonymous namespace)::ui>, void>"
4949
4950 Z_SYM_NAME
4951};
4952
5c58de74
PA
4953/* Returns true if the mapped_index::find_name_component_bounds method
4954 finds EXPECTED_SYMS in INDEX when looking for SEARCH_NAME, in
4955 completion mode. */
4956
4957static bool
4958check_find_bounds_finds (mapped_index &index,
4959 const char *search_name,
4960 gdb::array_view<const char *> expected_syms)
4961{
4962 lookup_name_info lookup_name (search_name,
4963 symbol_name_match_type::FULL, true);
4964
4965 auto bounds = index.find_name_components_bounds (lookup_name);
4966
4967 size_t distance = std::distance (bounds.first, bounds.second);
4968 if (distance != expected_syms.size ())
4969 return false;
4970
4971 for (size_t exp_elem = 0; exp_elem < distance; exp_elem++)
4972 {
4973 auto nc_elem = bounds.first + exp_elem;
4974 const char *qualified = index.symbol_name_at (nc_elem->idx);
4975 if (strcmp (qualified, expected_syms[exp_elem]) != 0)
4976 return false;
4977 }
4978
4979 return true;
4980}
4981
4982/* Test the lower-level mapped_index::find_name_component_bounds
4983 method. */
4984
c62446b1 4985static void
5c58de74
PA
4986test_mapped_index_find_name_component_bounds ()
4987{
4988 mock_mapped_index mock_index (test_symbols);
4989
4990 mock_index.index ().build_name_components ();
4991
4992 /* Test the lower-level mapped_index::find_name_component_bounds
4993 method in completion mode. */
4994 {
4995 static const char *expected_syms[] = {
4996 "t1_func",
4997 "t1_func1",
5c58de74
PA
4998 };
4999
5000 SELF_CHECK (check_find_bounds_finds (mock_index.index (),
5001 "t1_func", expected_syms));
5002 }
5003
5004 /* Check that the increment-last-char in the name matching algorithm
5005 for completion doesn't get confused with Ansi1 'ÿ' / 0xff. */
5006 {
5007 static const char *expected_syms1[] = {
5008 "\377",
5009 "\377\377123",
5010 };
5011 SELF_CHECK (check_find_bounds_finds (mock_index.index (),
5012 "\377", expected_syms1));
5013
5014 static const char *expected_syms2[] = {
5015 "\377\377123",
5016 };
5017 SELF_CHECK (check_find_bounds_finds (mock_index.index (),
5018 "\377\377", expected_syms2));
5019 }
5020}
5021
5022/* Test dw2_expand_symtabs_matching_symbol. */
5023
5024static void
5025test_dw2_expand_symtabs_matching_symbol ()
c62446b1
PA
5026{
5027 mock_mapped_index mock_index (test_symbols);
5028
5029 /* We let all tests run until the end even if some fails, for debug
5030 convenience. */
5031 bool any_mismatch = false;
5032
5033 /* Create the expected symbols list (an initializer_list). Needed
5034 because lists have commas, and we need to pass them to CHECK,
5035 which is a macro. */
5036#define EXPECT(...) { __VA_ARGS__ }
5037
5038 /* Wrapper for check_match that passes down the current
5039 __FILE__/__LINE__. */
5040#define CHECK_MATCH(NAME, MATCH_TYPE, COMPLETION_MODE, EXPECTED_LIST) \
5041 any_mismatch |= !check_match (__FILE__, __LINE__, \
5042 mock_index, \
5043 NAME, MATCH_TYPE, COMPLETION_MODE, \
5044 EXPECTED_LIST)
5045
5046 /* Identity checks. */
5047 for (const char *sym : test_symbols)
5048 {
5049 /* Should be able to match all existing symbols. */
5050 CHECK_MATCH (sym, symbol_name_match_type::FULL, false,
5051 EXPECT (sym));
5052
5053 /* Should be able to match all existing symbols with
5054 parameters. */
5055 std::string with_params = std::string (sym) + "(int)";
5056 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
5057 EXPECT (sym));
5058
5059 /* Should be able to match all existing symbols with
5060 parameters and qualifiers. */
5061 with_params = std::string (sym) + " ( int ) const";
5062 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
5063 EXPECT (sym));
5064
5065 /* This should really find sym, but cp-name-parser.y doesn't
5066 know about lvalue/rvalue qualifiers yet. */
5067 with_params = std::string (sym) + " ( int ) &&";
5068 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
5069 {});
5070 }
5071
e1ef7d7a
PA
5072 /* Check that the name matching algorithm for completion doesn't get
5073 confused with Latin1 'ÿ' / 0xff. */
5074 {
5075 static const char str[] = "\377";
5076 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
5077 EXPECT ("\377", "\377\377123"));
5078 }
5079
5080 /* Check that the increment-last-char in the matching algorithm for
5081 completion doesn't match "t1_fund" when completing "t1_func". */
5082 {
5083 static const char str[] = "t1_func";
5084 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
5085 EXPECT ("t1_func", "t1_func1"));
5086 }
5087
c62446b1
PA
5088 /* Check that completion mode works at each prefix of the expected
5089 symbol name. */
5090 {
5091 static const char str[] = "function(int)";
5092 size_t len = strlen (str);
5093 std::string lookup;
5094
5095 for (size_t i = 1; i < len; i++)
5096 {
5097 lookup.assign (str, i);
5098 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
5099 EXPECT ("function"));
5100 }
5101 }
5102
5103 /* While "w" is a prefix of both components, the match function
5104 should still only be called once. */
5105 {
5106 CHECK_MATCH ("w", symbol_name_match_type::FULL, true,
5107 EXPECT ("w1::w2"));
a20714ff
PA
5108 CHECK_MATCH ("w", symbol_name_match_type::WILD, true,
5109 EXPECT ("w1::w2"));
c62446b1
PA
5110 }
5111
5112 /* Same, with a "complicated" symbol. */
5113 {
5114 static const char str[] = Z_SYM_NAME;
5115 size_t len = strlen (str);
5116 std::string lookup;
5117
5118 for (size_t i = 1; i < len; i++)
5119 {
5120 lookup.assign (str, i);
5121 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
5122 EXPECT (Z_SYM_NAME));
5123 }
5124 }
5125
5126 /* In FULL mode, an incomplete symbol doesn't match. */
5127 {
5128 CHECK_MATCH ("std::zfunction(int", symbol_name_match_type::FULL, false,
5129 {});
5130 }
5131
5132 /* A complete symbol with parameters matches any overload, since the
5133 index has no overload info. */
5134 {
5135 CHECK_MATCH ("std::zfunction(int)", symbol_name_match_type::FULL, true,
5136 EXPECT ("std::zfunction", "std::zfunction2"));
a20714ff
PA
5137 CHECK_MATCH ("zfunction(int)", symbol_name_match_type::WILD, true,
5138 EXPECT ("std::zfunction", "std::zfunction2"));
5139 CHECK_MATCH ("zfunc", symbol_name_match_type::WILD, true,
5140 EXPECT ("std::zfunction", "std::zfunction2"));
c62446b1
PA
5141 }
5142
5143 /* Check that whitespace is ignored appropriately. A symbol with a
5144 template argument list. */
5145 {
5146 static const char expected[] = "ns::foo<int>";
5147 CHECK_MATCH ("ns :: foo < int > ", symbol_name_match_type::FULL, false,
5148 EXPECT (expected));
a20714ff
PA
5149 CHECK_MATCH ("foo < int > ", symbol_name_match_type::WILD, false,
5150 EXPECT (expected));
c62446b1
PA
5151 }
5152
5153 /* Check that whitespace is ignored appropriately. A symbol with a
5154 template argument list that includes a pointer. */
5155 {
5156 static const char expected[] = "ns::foo<char*>";
5157 /* Try both completion and non-completion modes. */
5158 static const bool completion_mode[2] = {false, true};
5159 for (size_t i = 0; i < 2; i++)
5160 {
5161 CHECK_MATCH ("ns :: foo < char * >", symbol_name_match_type::FULL,
5162 completion_mode[i], EXPECT (expected));
a20714ff
PA
5163 CHECK_MATCH ("foo < char * >", symbol_name_match_type::WILD,
5164 completion_mode[i], EXPECT (expected));
c62446b1
PA
5165
5166 CHECK_MATCH ("ns :: foo < char * > (int)", symbol_name_match_type::FULL,
5167 completion_mode[i], EXPECT (expected));
a20714ff
PA
5168 CHECK_MATCH ("foo < char * > (int)", symbol_name_match_type::WILD,
5169 completion_mode[i], EXPECT (expected));
c62446b1
PA
5170 }
5171 }
5172
5173 {
5174 /* Check method qualifiers are ignored. */
5175 static const char expected[] = "ns::foo<char*>";
5176 CHECK_MATCH ("ns :: foo < char * > ( int ) const",
5177 symbol_name_match_type::FULL, true, EXPECT (expected));
5178 CHECK_MATCH ("ns :: foo < char * > ( int ) &&",
5179 symbol_name_match_type::FULL, true, EXPECT (expected));
a20714ff
PA
5180 CHECK_MATCH ("foo < char * > ( int ) const",
5181 symbol_name_match_type::WILD, true, EXPECT (expected));
5182 CHECK_MATCH ("foo < char * > ( int ) &&",
5183 symbol_name_match_type::WILD, true, EXPECT (expected));
c62446b1
PA
5184 }
5185
5186 /* Test lookup names that don't match anything. */
5187 {
a20714ff
PA
5188 CHECK_MATCH ("bar2", symbol_name_match_type::WILD, false,
5189 {});
5190
c62446b1
PA
5191 CHECK_MATCH ("doesntexist", symbol_name_match_type::FULL, false,
5192 {});
5193 }
5194
a20714ff
PA
5195 /* Some wild matching tests, exercising "(anonymous namespace)",
5196 which should not be confused with a parameter list. */
5197 {
5198 static const char *syms[] = {
5199 "A::B::C",
5200 "B::C",
5201 "C",
5202 "A :: B :: C ( int )",
5203 "B :: C ( int )",
5204 "C ( int )",
5205 };
5206
5207 for (const char *s : syms)
5208 {
5209 CHECK_MATCH (s, symbol_name_match_type::WILD, false,
5210 EXPECT ("(anonymous namespace)::A::B::C"));
5211 }
5212 }
5213
5214 {
5215 static const char expected[] = "ns2::tmpl<int>::foo2";
5216 CHECK_MATCH ("tmp", symbol_name_match_type::WILD, true,
5217 EXPECT (expected));
5218 CHECK_MATCH ("tmpl<", symbol_name_match_type::WILD, true,
5219 EXPECT (expected));
5220 }
5221
c62446b1
PA
5222 SELF_CHECK (!any_mismatch);
5223
5224#undef EXPECT
5225#undef CHECK_MATCH
5226}
5227
5c58de74
PA
5228static void
5229run_test ()
5230{
5231 test_mapped_index_find_name_component_bounds ();
5232 test_dw2_expand_symtabs_matching_symbol ();
5233}
5234
c62446b1
PA
5235}} // namespace selftests::dw2_expand_symtabs_matching
5236
5237#endif /* GDB_SELF_TEST */
5238
4b514bc8
JK
5239/* If FILE_MATCHER is NULL or if PER_CU has
5240 dwarf2_per_cu_quick_data::MARK set (see
5241 dw_expand_symtabs_matching_file_matcher), expand the CU and call
5242 EXPANSION_NOTIFY on it. */
5243
5244static void
5245dw2_expand_symtabs_matching_one
5246 (struct dwarf2_per_cu_data *per_cu,
5247 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
5248 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify)
5249{
5250 if (file_matcher == NULL || per_cu->v.quick->mark)
5251 {
5252 bool symtab_was_null
5253 = (per_cu->v.quick->compunit_symtab == NULL);
5254
5255 dw2_instantiate_symtab (per_cu);
5256
5257 if (expansion_notify != NULL
5258 && symtab_was_null
5259 && per_cu->v.quick->compunit_symtab != NULL)
5260 expansion_notify (per_cu->v.quick->compunit_symtab);
5261 }
5262}
5263
3f563c84
PA
5264/* Helper for dw2_expand_matching symtabs. Called on each symbol
5265 matched, to expand corresponding CUs that were marked. IDX is the
5266 index of the symbol name that matched. */
5267
5268static void
5269dw2_expand_marked_cus
5270 (mapped_index &index, offset_type idx,
5271 struct objfile *objfile,
5272 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
5273 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
5274 search_domain kind)
5275{
3f563c84
PA
5276 offset_type *vec, vec_len, vec_idx;
5277 bool global_seen = false;
5278
61920122 5279 vec = (offset_type *) (index.constant_pool
f00a2de2 5280 + MAYBE_SWAP (index.symbol_table[idx].vec));
61920122
PA
5281 vec_len = MAYBE_SWAP (vec[0]);
5282 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
5283 {
5284 struct dwarf2_per_cu_data *per_cu;
5285 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[vec_idx + 1]);
5286 /* This value is only valid for index versions >= 7. */
5287 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
5288 gdb_index_symbol_kind symbol_kind =
5289 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
5290 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
5291 /* Only check the symbol attributes if they're present.
5292 Indices prior to version 7 don't record them,
5293 and indices >= 7 may elide them for certain symbols
5294 (gold does this). */
5295 int attrs_valid =
5296 (index.version >= 7
5297 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
5298
5299 /* Work around gold/15646. */
5300 if (attrs_valid)
9291a0cd 5301 {
61920122
PA
5302 if (!is_static && global_seen)
5303 continue;
5304 if (!is_static)
5305 global_seen = true;
5306 }
3190f0c6 5307
61920122
PA
5308 /* Only check the symbol's kind if it has one. */
5309 if (attrs_valid)
5310 {
5311 switch (kind)
8943b874 5312 {
61920122
PA
5313 case VARIABLES_DOMAIN:
5314 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
5315 continue;
5316 break;
5317 case FUNCTIONS_DOMAIN:
5318 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
8943b874 5319 continue;
61920122
PA
5320 break;
5321 case TYPES_DOMAIN:
5322 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
5323 continue;
5324 break;
5325 default:
5326 break;
8943b874 5327 }
61920122 5328 }
8943b874 5329
61920122
PA
5330 /* Don't crash on bad data. */
5331 if (cu_index >= (dwarf2_per_objfile->n_comp_units
5332 + dwarf2_per_objfile->n_type_units))
5333 {
5334 complaint (&symfile_complaints,
5335 _(".gdb_index entry has bad CU index"
5336 " [in module %s]"), objfile_name (objfile));
5337 continue;
5338 }
5339
5340 per_cu = dw2_get_cutu (cu_index);
4b514bc8
JK
5341 dw2_expand_symtabs_matching_one (per_cu, file_matcher,
5342 expansion_notify);
61920122
PA
5343 }
5344}
5345
4b514bc8
JK
5346/* If FILE_MATCHER is non-NULL, set all the
5347 dwarf2_per_cu_quick_data::MARK of the current DWARF2_PER_OBJFILE
5348 that match FILE_MATCHER. */
5349
61920122 5350static void
4b514bc8
JK
5351dw_expand_symtabs_matching_file_matcher
5352 (gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher)
61920122 5353{
4b514bc8 5354 if (file_matcher == NULL)
61920122
PA
5355 return;
5356
4b514bc8
JK
5357 objfile *const objfile = dwarf2_per_objfile->objfile;
5358
5359 htab_up visited_found (htab_create_alloc (10, htab_hash_pointer,
5360 htab_eq_pointer,
5361 NULL, xcalloc, xfree));
5362 htab_up visited_not_found (htab_create_alloc (10, htab_hash_pointer,
61920122
PA
5363 htab_eq_pointer,
5364 NULL, xcalloc, xfree));
61920122 5365
4b514bc8
JK
5366 /* The rule is CUs specify all the files, including those used by
5367 any TU, so there's no need to scan TUs here. */
61920122 5368
927aa2e7
JK
5369 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
5370 {
5371 int j;
5372 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
5373 struct quick_file_names *file_data;
5374 void **slot;
5375
5376 QUIT;
5377
5378 per_cu->v.quick->mark = 0;
5379
5380 /* We only need to look at symtabs not already expanded. */
5381 if (per_cu->v.quick->compunit_symtab)
5382 continue;
5383
5384 file_data = dw2_get_file_names (per_cu);
5385 if (file_data == NULL)
5386 continue;
5387
5388 if (htab_find (visited_not_found.get (), file_data) != NULL)
5389 continue;
5390 else if (htab_find (visited_found.get (), file_data) != NULL)
5391 {
5392 per_cu->v.quick->mark = 1;
5393 continue;
5394 }
5395
5396 for (j = 0; j < file_data->num_file_names; ++j)
5397 {
5398 const char *this_real_name;
5399
5400 if (file_matcher (file_data->file_names[j], false))
5401 {
5402 per_cu->v.quick->mark = 1;
5403 break;
5404 }
5405
5406 /* Before we invoke realpath, which can get expensive when many
5407 files are involved, do a quick comparison of the basenames. */
5408 if (!basenames_may_differ
5409 && !file_matcher (lbasename (file_data->file_names[j]),
5410 true))
5411 continue;
5412
5413 this_real_name = dw2_get_real_path (objfile, file_data, j);
5414 if (file_matcher (this_real_name, false))
5415 {
5416 per_cu->v.quick->mark = 1;
5417 break;
5418 }
5419 }
5420
5421 slot = htab_find_slot (per_cu->v.quick->mark
5422 ? visited_found.get ()
5423 : visited_not_found.get (),
5424 file_data, INSERT);
5425 *slot = file_data;
5426 }
5427}
5428
5429static void
5430dw2_expand_symtabs_matching
5431 (struct objfile *objfile,
5432 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
5433 const lookup_name_info &lookup_name,
5434 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
5435 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
5436 enum search_domain kind)
5437{
5438 int i;
5439
5440 dw2_setup (objfile);
5441
5442 /* index_table is NULL if OBJF_READNOW. */
5443 if (!dwarf2_per_objfile->index_table)
5444 return;
5445
5446 dw_expand_symtabs_matching_file_matcher (file_matcher);
5447
5448 mapped_index &index = *dwarf2_per_objfile->index_table;
5449
5450 dw2_expand_symtabs_matching_symbol (index, lookup_name,
5451 symbol_matcher,
5452 kind, [&] (offset_type idx)
5453 {
5454 dw2_expand_marked_cus (index, idx, objfile, file_matcher,
5455 expansion_notify, kind);
5456 });
5457}
5458
5459/* A helper for dw2_find_pc_sect_compunit_symtab which finds the most specific
5460 symtab. */
5461
5462static struct compunit_symtab *
5463recursively_find_pc_sect_compunit_symtab (struct compunit_symtab *cust,
5464 CORE_ADDR pc)
5465{
5466 int i;
5467
5468 if (COMPUNIT_BLOCKVECTOR (cust) != NULL
5469 && blockvector_contains_pc (COMPUNIT_BLOCKVECTOR (cust), pc))
5470 return cust;
5471
5472 if (cust->includes == NULL)
5473 return NULL;
5474
5475 for (i = 0; cust->includes[i]; ++i)
5476 {
5477 struct compunit_symtab *s = cust->includes[i];
5478
5479 s = recursively_find_pc_sect_compunit_symtab (s, pc);
5480 if (s != NULL)
5481 return s;
5482 }
5483
5484 return NULL;
5485}
5486
5487static struct compunit_symtab *
5488dw2_find_pc_sect_compunit_symtab (struct objfile *objfile,
5489 struct bound_minimal_symbol msymbol,
5490 CORE_ADDR pc,
5491 struct obj_section *section,
5492 int warn_if_readin)
5493{
5494 struct dwarf2_per_cu_data *data;
5495 struct compunit_symtab *result;
5496
5497 dw2_setup (objfile);
5498
5499 if (!objfile->psymtabs_addrmap)
5500 return NULL;
5501
5502 data = (struct dwarf2_per_cu_data *) addrmap_find (objfile->psymtabs_addrmap,
5503 pc);
5504 if (!data)
5505 return NULL;
5506
5507 if (warn_if_readin && data->v.quick->compunit_symtab)
5508 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
5509 paddress (get_objfile_arch (objfile), pc));
5510
5511 result
5512 = recursively_find_pc_sect_compunit_symtab (dw2_instantiate_symtab (data),
5513 pc);
5514 gdb_assert (result != NULL);
5515 return result;
5516}
5517
5518static void
5519dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
5520 void *data, int need_fullname)
5521{
5522 dw2_setup (objfile);
5523
5524 if (!dwarf2_per_objfile->filenames_cache)
5525 {
5526 dwarf2_per_objfile->filenames_cache.emplace ();
5527
5528 htab_up visited (htab_create_alloc (10,
5529 htab_hash_pointer, htab_eq_pointer,
5530 NULL, xcalloc, xfree));
5531
5532 /* The rule is CUs specify all the files, including those used
5533 by any TU, so there's no need to scan TUs here. We can
5534 ignore file names coming from already-expanded CUs. */
5535
5536 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
5537 {
5538 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
5539
5540 if (per_cu->v.quick->compunit_symtab)
5541 {
5542 void **slot = htab_find_slot (visited.get (),
5543 per_cu->v.quick->file_names,
5544 INSERT);
5545
5546 *slot = per_cu->v.quick->file_names;
5547 }
5548 }
5549
5550 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
5551 {
5552 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
5553 struct quick_file_names *file_data;
5554 void **slot;
5555
5556 /* We only need to look at symtabs not already expanded. */
5557 if (per_cu->v.quick->compunit_symtab)
5558 continue;
5559
5560 file_data = dw2_get_file_names (per_cu);
5561 if (file_data == NULL)
5562 continue;
5563
5564 slot = htab_find_slot (visited.get (), file_data, INSERT);
5565 if (*slot)
5566 {
5567 /* Already visited. */
5568 continue;
5569 }
5570 *slot = file_data;
5571
5572 for (int j = 0; j < file_data->num_file_names; ++j)
5573 {
5574 const char *filename = file_data->file_names[j];
5575 dwarf2_per_objfile->filenames_cache->seen (filename);
5576 }
5577 }
5578 }
5579
5580 dwarf2_per_objfile->filenames_cache->traverse ([&] (const char *filename)
5581 {
5582 gdb::unique_xmalloc_ptr<char> this_real_name;
5583
5584 if (need_fullname)
5585 this_real_name = gdb_realpath (filename);
5586 (*fun) (filename, this_real_name.get (), data);
5587 });
5588}
5589
5590static int
5591dw2_has_symbols (struct objfile *objfile)
5592{
5593 return 1;
5594}
5595
5596const struct quick_symbol_functions dwarf2_gdb_index_functions =
5597{
5598 dw2_has_symbols,
5599 dw2_find_last_source_symtab,
5600 dw2_forget_cached_source_info,
5601 dw2_map_symtabs_matching_filename,
5602 dw2_lookup_symbol,
5603 dw2_print_stats,
5604 dw2_dump,
5605 dw2_relocate,
5606 dw2_expand_symtabs_for_function,
5607 dw2_expand_all_symtabs,
5608 dw2_expand_symtabs_with_fullname,
5609 dw2_map_matching_symbols,
5610 dw2_expand_symtabs_matching,
5611 dw2_find_pc_sect_compunit_symtab,
5612 NULL,
5613 dw2_map_symbol_filenames
5614};
5615
5616/* DWARF-5 debug_names reader. */
5617
5618/* DWARF-5 augmentation string for GDB's DW_IDX_GNU_* extension. */
5619static const gdb_byte dwarf5_augmentation[] = { 'G', 'D', 'B', 0 };
5620
5621/* A helper function that reads the .debug_names section in SECTION
5622 and fills in MAP. FILENAME is the name of the file containing the
5623 section; it is used for error reporting.
5624
5625 Returns true if all went well, false otherwise. */
5626
5627static bool
5628read_debug_names_from_section (struct objfile *objfile,
5629 const char *filename,
5630 struct dwarf2_section_info *section,
5631 mapped_debug_names &map)
5632{
5633 if (dwarf2_section_empty_p (section))
5634 return false;
5635
5636 /* Older elfutils strip versions could keep the section in the main
5637 executable while splitting it for the separate debug info file. */
5638 if ((get_section_flags (section) & SEC_HAS_CONTENTS) == 0)
5639 return false;
5640
5641 dwarf2_read_section (objfile, section);
5642
5643 map.dwarf5_byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
5644
5645 const gdb_byte *addr = section->buffer;
5646
5647 bfd *const abfd = get_section_bfd_owner (section);
5648
5649 unsigned int bytes_read;
5650 LONGEST length = read_initial_length (abfd, addr, &bytes_read);
5651 addr += bytes_read;
5652
5653 map.dwarf5_is_dwarf64 = bytes_read != 4;
5654 map.offset_size = map.dwarf5_is_dwarf64 ? 8 : 4;
5655 if (bytes_read + length != section->size)
5656 {
5657 /* There may be multiple per-CU indices. */
5658 warning (_("Section .debug_names in %s length %s does not match "
5659 "section length %s, ignoring .debug_names."),
5660 filename, plongest (bytes_read + length),
5661 pulongest (section->size));
5662 return false;
5663 }
5664
5665 /* The version number. */
5666 uint16_t version = read_2_bytes (abfd, addr);
5667 addr += 2;
5668 if (version != 5)
5669 {
5670 warning (_("Section .debug_names in %s has unsupported version %d, "
5671 "ignoring .debug_names."),
5672 filename, version);
5673 return false;
5674 }
5675
5676 /* Padding. */
5677 uint16_t padding = read_2_bytes (abfd, addr);
5678 addr += 2;
5679 if (padding != 0)
5680 {
5681 warning (_("Section .debug_names in %s has unsupported padding %d, "
5682 "ignoring .debug_names."),
5683 filename, padding);
5684 return false;
5685 }
5686
5687 /* comp_unit_count - The number of CUs in the CU list. */
5688 map.cu_count = read_4_bytes (abfd, addr);
5689 addr += 4;
5690
5691 /* local_type_unit_count - The number of TUs in the local TU
5692 list. */
5693 map.tu_count = read_4_bytes (abfd, addr);
5694 addr += 4;
5695
5696 /* foreign_type_unit_count - The number of TUs in the foreign TU
5697 list. */
5698 uint32_t foreign_tu_count = read_4_bytes (abfd, addr);
5699 addr += 4;
5700 if (foreign_tu_count != 0)
5701 {
5702 warning (_("Section .debug_names in %s has unsupported %lu foreign TUs, "
5703 "ignoring .debug_names."),
5704 filename, static_cast<unsigned long> (foreign_tu_count));
5705 return false;
5706 }
5707
5708 /* bucket_count - The number of hash buckets in the hash lookup
5709 table. */
5710 map.bucket_count = read_4_bytes (abfd, addr);
5711 addr += 4;
5712
5713 /* name_count - The number of unique names in the index. */
5714 map.name_count = read_4_bytes (abfd, addr);
5715 addr += 4;
5716
5717 /* abbrev_table_size - The size in bytes of the abbreviations
5718 table. */
5719 uint32_t abbrev_table_size = read_4_bytes (abfd, addr);
5720 addr += 4;
5721
5722 /* augmentation_string_size - The size in bytes of the augmentation
5723 string. This value is rounded up to a multiple of 4. */
5724 uint32_t augmentation_string_size = read_4_bytes (abfd, addr);
5725 addr += 4;
5726 map.augmentation_is_gdb = ((augmentation_string_size
5727 == sizeof (dwarf5_augmentation))
5728 && memcmp (addr, dwarf5_augmentation,
5729 sizeof (dwarf5_augmentation)) == 0);
5730 augmentation_string_size += (-augmentation_string_size) & 3;
5731 addr += augmentation_string_size;
5732
5733 /* List of CUs */
5734 map.cu_table_reordered = addr;
5735 addr += map.cu_count * map.offset_size;
5736
5737 /* List of Local TUs */
5738 map.tu_table_reordered = addr;
5739 addr += map.tu_count * map.offset_size;
5740
5741 /* Hash Lookup Table */
5742 map.bucket_table_reordered = reinterpret_cast<const uint32_t *> (addr);
5743 addr += map.bucket_count * 4;
5744 map.hash_table_reordered = reinterpret_cast<const uint32_t *> (addr);
5745 addr += map.name_count * 4;
5746
5747 /* Name Table */
5748 map.name_table_string_offs_reordered = addr;
5749 addr += map.name_count * map.offset_size;
5750 map.name_table_entry_offs_reordered = addr;
5751 addr += map.name_count * map.offset_size;
5752
5753 const gdb_byte *abbrev_table_start = addr;
5754 for (;;)
5755 {
5756 unsigned int bytes_read;
5757 const ULONGEST index_num = read_unsigned_leb128 (abfd, addr, &bytes_read);
5758 addr += bytes_read;
5759 if (index_num == 0)
5760 break;
5761
5762 const auto insertpair
5763 = map.abbrev_map.emplace (index_num, mapped_debug_names::index_val ());
5764 if (!insertpair.second)
5765 {
5766 warning (_("Section .debug_names in %s has duplicate index %s, "
5767 "ignoring .debug_names."),
5768 filename, pulongest (index_num));
5769 return false;
5770 }
5771 mapped_debug_names::index_val &indexval = insertpair.first->second;
5772 indexval.dwarf_tag = read_unsigned_leb128 (abfd, addr, &bytes_read);
5773 addr += bytes_read;
5774
5775 for (;;)
5776 {
5777 mapped_debug_names::index_val::attr attr;
5778 attr.dw_idx = read_unsigned_leb128 (abfd, addr, &bytes_read);
5779 addr += bytes_read;
5780 attr.form = read_unsigned_leb128 (abfd, addr, &bytes_read);
5781 addr += bytes_read;
5782 if (attr.form == DW_FORM_implicit_const)
5783 {
5784 attr.implicit_const = read_signed_leb128 (abfd, addr,
5785 &bytes_read);
5786 addr += bytes_read;
5787 }
5788 if (attr.dw_idx == 0 && attr.form == 0)
5789 break;
5790 indexval.attr_vec.push_back (std::move (attr));
5791 }
5792 }
5793 if (addr != abbrev_table_start + abbrev_table_size)
5794 {
5795 warning (_("Section .debug_names in %s has abbreviation_table "
5796 "of size %zu vs. written as %u, ignoring .debug_names."),
5797 filename, addr - abbrev_table_start, abbrev_table_size);
5798 return false;
5799 }
5800 map.entry_pool = addr;
5801
5802 return true;
5803}
5804
5805/* A helper for create_cus_from_debug_names that handles the MAP's CU
5806 list. */
5807
5808static void
5809create_cus_from_debug_names_list (struct objfile *objfile,
5810 const mapped_debug_names &map,
5811 dwarf2_section_info &section,
5812 bool is_dwz, int base_offset)
5813{
5814 sect_offset sect_off_prev;
5815 for (uint32_t i = 0; i <= map.cu_count; ++i)
5816 {
5817 sect_offset sect_off_next;
5818 if (i < map.cu_count)
5819 {
5820 sect_off_next
5821 = (sect_offset) (extract_unsigned_integer
5822 (map.cu_table_reordered + i * map.offset_size,
5823 map.offset_size,
5824 map.dwarf5_byte_order));
5825 }
5826 else
5827 sect_off_next = (sect_offset) section.size;
5828 if (i >= 1)
5829 {
5830 const ULONGEST length = sect_off_next - sect_off_prev;
5831 dwarf2_per_objfile->all_comp_units[base_offset + (i - 1)]
5832 = create_cu_from_index_list (objfile, &section, is_dwz,
5833 sect_off_prev, length);
5834 }
5835 sect_off_prev = sect_off_next;
5836 }
5837}
5838
5839/* Read the CU list from the mapped index, and use it to create all
5840 the CU objects for this objfile. */
5841
5842static void
5843create_cus_from_debug_names (struct objfile *objfile,
5844 const mapped_debug_names &map,
5845 const mapped_debug_names &dwz_map)
5846{
5847
5848 dwarf2_per_objfile->n_comp_units = map.cu_count + dwz_map.cu_count;
5849 dwarf2_per_objfile->all_comp_units
5850 = XOBNEWVEC (&objfile->objfile_obstack, struct dwarf2_per_cu_data *,
5851 dwarf2_per_objfile->n_comp_units);
5852
5853 create_cus_from_debug_names_list (objfile, map, dwarf2_per_objfile->info,
5854 false /* is_dwz */,
5855 0 /* base_offset */);
5856
5857 if (dwz_map.cu_count == 0)
5858 return;
5859
5860 dwz_file *dwz = dwarf2_get_dwz_file ();
5861 create_cus_from_debug_names_list (objfile, dwz_map, dwz->info,
5862 true /* is_dwz */,
5863 map.cu_count /* base_offset */);
5864}
5865
5866/* Read .debug_names. If everything went ok, initialize the "quick"
5867 elements of all the CUs and return true. Otherwise, return false. */
5868
5869static bool
5870dwarf2_read_debug_names (struct objfile *objfile)
5871{
5872 mapped_debug_names local_map, dwz_map;
5873
5874 if (!read_debug_names_from_section (objfile, objfile_name (objfile),
5875 &dwarf2_per_objfile->debug_names,
5876 local_map))
5877 return false;
5878
5879 /* Don't use the index if it's empty. */
5880 if (local_map.name_count == 0)
5881 return false;
5882
5883 /* If there is a .dwz file, read it so we can get its CU list as
5884 well. */
5885 dwz_file *dwz = dwarf2_get_dwz_file ();
5886 if (dwz != NULL)
5887 {
5888 if (!read_debug_names_from_section (objfile,
5889 bfd_get_filename (dwz->dwz_bfd),
5890 &dwz->debug_names, dwz_map))
5891 {
5892 warning (_("could not read '.debug_names' section from %s; skipping"),
5893 bfd_get_filename (dwz->dwz_bfd));
5894 return false;
5895 }
5896 }
5897
5898 create_cus_from_debug_names (objfile, local_map, dwz_map);
5899
5900 if (local_map.tu_count != 0)
5901 {
5902 /* We can only handle a single .debug_types when we have an
5903 index. */
5904 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) != 1)
5905 return false;
5906
5907 dwarf2_section_info *section = VEC_index (dwarf2_section_info_def,
5908 dwarf2_per_objfile->types, 0);
5909
5910 create_signatured_type_table_from_debug_names
5911 (objfile, local_map, section, &dwarf2_per_objfile->abbrev);
5912 }
5913
5914 create_addrmap_from_aranges (objfile, &dwarf2_per_objfile->debug_aranges);
5915
5916 dwarf2_per_objfile->debug_names_table.reset (new mapped_debug_names);
5917 *dwarf2_per_objfile->debug_names_table = std::move (local_map);
5918 dwarf2_per_objfile->using_index = 1;
5919 dwarf2_per_objfile->quick_file_names_table =
5920 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
5921
5922 return true;
5923}
5924
5925/* Symbol name hashing function as specified by DWARF-5. */
5926
5927static uint32_t
5928dwarf5_djb_hash (const char *str_)
5929{
5930 const unsigned char *str = (const unsigned char *) str_;
5931
5932 /* Note: tolower here ignores UTF-8, which isn't fully compliant.
5933 See http://dwarfstd.org/ShowIssue.php?issue=161027.1. */
5934
5935 uint32_t hash = 5381;
5936 while (int c = *str++)
5937 hash = hash * 33 + tolower (c);
5938 return hash;
5939}
5940
5941/* Type used to manage iterating over all CUs looking for a symbol for
5942 .debug_names. */
5943
5944class dw2_debug_names_iterator
5945{
5946public:
5947 /* If WANT_SPECIFIC_BLOCK is true, only look for symbols in block
5948 BLOCK_INDEX. Otherwise BLOCK_INDEX is ignored. */
5949 dw2_debug_names_iterator (const mapped_debug_names &map,
5950 bool want_specific_block,
5951 block_enum block_index, domain_enum domain,
5952 const char *name)
5953 : m_map (map), m_want_specific_block (want_specific_block),
5954 m_block_index (block_index), m_domain (domain),
5955 m_addr (find_vec_in_debug_names (map, name))
5956 {}
5957
5958 dw2_debug_names_iterator (const mapped_debug_names &map,
5959 search_domain search, uint32_t namei)
5960 : m_map (map),
5961 m_search (search),
5962 m_addr (find_vec_in_debug_names (map, namei))
5963 {}
5964
5965 /* Return the next matching CU or NULL if there are no more. */
5966 dwarf2_per_cu_data *next ();
5967
5968private:
5969 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5970 const char *name);
5971 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5972 uint32_t namei);
5973
5974 /* The internalized form of .debug_names. */
5975 const mapped_debug_names &m_map;
5976
5977 /* If true, only look for symbols that match BLOCK_INDEX. */
5978 const bool m_want_specific_block = false;
5979
5980 /* One of GLOBAL_BLOCK or STATIC_BLOCK.
5981 Unused if !WANT_SPECIFIC_BLOCK - FIRST_LOCAL_BLOCK is an invalid
5982 value. */
5983 const block_enum m_block_index = FIRST_LOCAL_BLOCK;
5984
5985 /* The kind of symbol we're looking for. */
5986 const domain_enum m_domain = UNDEF_DOMAIN;
5987 const search_domain m_search = ALL_DOMAIN;
5988
5989 /* The list of CUs from the index entry of the symbol, or NULL if
5990 not found. */
5991 const gdb_byte *m_addr;
5992};
5993
5994const char *
5995mapped_debug_names::namei_to_name (uint32_t namei) const
5996{
5997 const ULONGEST namei_string_offs
5998 = extract_unsigned_integer ((name_table_string_offs_reordered
5999 + namei * offset_size),
6000 offset_size,
6001 dwarf5_byte_order);
6002 return read_indirect_string_at_offset
6003 (dwarf2_per_objfile->objfile->obfd, namei_string_offs);
6004}
6005
6006/* Find a slot in .debug_names for the object named NAME. If NAME is
6007 found, return pointer to its pool data. If NAME cannot be found,
6008 return NULL. */
6009
6010const gdb_byte *
6011dw2_debug_names_iterator::find_vec_in_debug_names
6012 (const mapped_debug_names &map, const char *name)
6013{
6014 int (*cmp) (const char *, const char *);
6015
6016 if (current_language->la_language == language_cplus
6017 || current_language->la_language == language_fortran
6018 || current_language->la_language == language_d)
6019 {
6020 /* NAME is already canonical. Drop any qualifiers as
6021 .debug_names does not contain any. */
6022
6023 if (strchr (name, '(') != NULL)
6024 {
6025 gdb::unique_xmalloc_ptr<char> without_params
6026 = cp_remove_params (name);
6027
6028 if (without_params != NULL)
6029 {
6030 name = without_params.get();
6031 }
6032 }
6033 }
6034
6035 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
6036
6037 const uint32_t full_hash = dwarf5_djb_hash (name);
6038 uint32_t namei
6039 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
6040 (map.bucket_table_reordered
6041 + (full_hash % map.bucket_count)), 4,
6042 map.dwarf5_byte_order);
6043 if (namei == 0)
6044 return NULL;
6045 --namei;
6046 if (namei >= map.name_count)
6047 {
6048 complaint (&symfile_complaints,
6049 _("Wrong .debug_names with name index %u but name_count=%u "
6050 "[in module %s]"),
6051 namei, map.name_count,
6052 objfile_name (dwarf2_per_objfile->objfile));
6053 return NULL;
6054 }
6055
6056 for (;;)
6057 {
6058 const uint32_t namei_full_hash
6059 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
6060 (map.hash_table_reordered + namei), 4,
6061 map.dwarf5_byte_order);
6062 if (full_hash % map.bucket_count != namei_full_hash % map.bucket_count)
6063 return NULL;
6064
6065 if (full_hash == namei_full_hash)
6066 {
6067 const char *const namei_string = map.namei_to_name (namei);
6068
6069#if 0 /* An expensive sanity check. */
6070 if (namei_full_hash != dwarf5_djb_hash (namei_string))
6071 {
6072 complaint (&symfile_complaints,
6073 _("Wrong .debug_names hash for string at index %u "
6074 "[in module %s]"),
6075 namei, objfile_name (dwarf2_per_objfile->objfile));
6076 return NULL;
6077 }
6078#endif
6079
6080 if (cmp (namei_string, name) == 0)
6081 {
6082 const ULONGEST namei_entry_offs
6083 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
6084 + namei * map.offset_size),
6085 map.offset_size, map.dwarf5_byte_order);
6086 return map.entry_pool + namei_entry_offs;
6087 }
6088 }
6089
6090 ++namei;
6091 if (namei >= map.name_count)
6092 return NULL;
6093 }
6094}
6095
6096const gdb_byte *
6097dw2_debug_names_iterator::find_vec_in_debug_names
6098 (const mapped_debug_names &map, uint32_t namei)
6099{
6100 if (namei >= map.name_count)
6101 {
6102 complaint (&symfile_complaints,
6103 _("Wrong .debug_names with name index %u but name_count=%u "
6104 "[in module %s]"),
6105 namei, map.name_count,
6106 objfile_name (dwarf2_per_objfile->objfile));
6107 return NULL;
6108 }
6109
6110 const ULONGEST namei_entry_offs
6111 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
6112 + namei * map.offset_size),
6113 map.offset_size, map.dwarf5_byte_order);
6114 return map.entry_pool + namei_entry_offs;
6115}
6116
6117/* See dw2_debug_names_iterator. */
6118
6119dwarf2_per_cu_data *
6120dw2_debug_names_iterator::next ()
6121{
6122 if (m_addr == NULL)
6123 return NULL;
6124
6125 bfd *const abfd = dwarf2_per_objfile->objfile->obfd;
6126
6127 again:
6128
6129 unsigned int bytes_read;
6130 const ULONGEST abbrev = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
6131 m_addr += bytes_read;
6132 if (abbrev == 0)
6133 return NULL;
6134
6135 const auto indexval_it = m_map.abbrev_map.find (abbrev);
6136 if (indexval_it == m_map.abbrev_map.cend ())
6137 {
6138 complaint (&symfile_complaints,
6139 _("Wrong .debug_names undefined abbrev code %s "
6140 "[in module %s]"),
6141 pulongest (abbrev), objfile_name (dwarf2_per_objfile->objfile));
6142 return NULL;
6143 }
6144 const mapped_debug_names::index_val &indexval = indexval_it->second;
6145 bool have_is_static = false;
6146 bool is_static;
6147 dwarf2_per_cu_data *per_cu = NULL;
6148 for (const mapped_debug_names::index_val::attr &attr : indexval.attr_vec)
6149 {
6150 ULONGEST ull;
6151 switch (attr.form)
6152 {
6153 case DW_FORM_implicit_const:
6154 ull = attr.implicit_const;
6155 break;
6156 case DW_FORM_flag_present:
6157 ull = 1;
6158 break;
6159 case DW_FORM_udata:
6160 ull = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
6161 m_addr += bytes_read;
6162 break;
6163 default:
6164 complaint (&symfile_complaints,
6165 _("Unsupported .debug_names form %s [in module %s]"),
6166 dwarf_form_name (attr.form),
6167 objfile_name (dwarf2_per_objfile->objfile));
6168 return NULL;
6169 }
6170 switch (attr.dw_idx)
6171 {
6172 case DW_IDX_compile_unit:
6173 /* Don't crash on bad data. */
6174 if (ull >= (dwarf2_per_objfile->n_comp_units
6175 + dwarf2_per_objfile->n_type_units))
6176 {
6177 complaint (&symfile_complaints,
6178 _(".debug_names entry has bad CU index %s"
6179 " [in module %s]"),
6180 pulongest (ull),
6181 objfile_name (dwarf2_per_objfile->objfile));
6182 continue;
6183 }
6184 per_cu = dw2_get_cutu (ull);
6185 break;
6186 case DW_IDX_GNU_internal:
6187 if (!m_map.augmentation_is_gdb)
6188 break;
6189 have_is_static = true;
6190 is_static = true;
6191 break;
6192 case DW_IDX_GNU_external:
6193 if (!m_map.augmentation_is_gdb)
6194 break;
6195 have_is_static = true;
6196 is_static = false;
6197 break;
6198 }
6199 }
6200
6201 /* Skip if already read in. */
6202 if (per_cu->v.quick->compunit_symtab)
6203 goto again;
6204
6205 /* Check static vs global. */
6206 if (have_is_static)
6207 {
6208 const bool want_static = m_block_index != GLOBAL_BLOCK;
6209 if (m_want_specific_block && want_static != is_static)
6210 goto again;
6211 }
6212
6213 /* Match dw2_symtab_iter_next, symbol_kind
6214 and debug_names::psymbol_tag. */
6215 switch (m_domain)
6216 {
6217 case VAR_DOMAIN:
6218 switch (indexval.dwarf_tag)
6219 {
6220 case DW_TAG_variable:
6221 case DW_TAG_subprogram:
6222 /* Some types are also in VAR_DOMAIN. */
6223 case DW_TAG_typedef:
6224 case DW_TAG_structure_type:
6225 break;
6226 default:
6227 goto again;
6228 }
6229 break;
6230 case STRUCT_DOMAIN:
6231 switch (indexval.dwarf_tag)
6232 {
6233 case DW_TAG_typedef:
6234 case DW_TAG_structure_type:
6235 break;
6236 default:
6237 goto again;
6238 }
6239 break;
6240 case LABEL_DOMAIN:
6241 switch (indexval.dwarf_tag)
6242 {
6243 case 0:
6244 case DW_TAG_variable:
6245 break;
6246 default:
6247 goto again;
6248 }
6249 break;
6250 default:
6251 break;
6252 }
6253
6254 /* Match dw2_expand_symtabs_matching, symbol_kind and
6255 debug_names::psymbol_tag. */
6256 switch (m_search)
4b514bc8 6257 {
927aa2e7
JK
6258 case VARIABLES_DOMAIN:
6259 switch (indexval.dwarf_tag)
4b514bc8 6260 {
927aa2e7
JK
6261 case DW_TAG_variable:
6262 break;
6263 default:
6264 goto again;
4b514bc8 6265 }
927aa2e7
JK
6266 break;
6267 case FUNCTIONS_DOMAIN:
6268 switch (indexval.dwarf_tag)
4b514bc8 6269 {
927aa2e7
JK
6270 case DW_TAG_subprogram:
6271 break;
6272 default:
6273 goto again;
4b514bc8 6274 }
927aa2e7
JK
6275 break;
6276 case TYPES_DOMAIN:
6277 switch (indexval.dwarf_tag)
6278 {
6279 case DW_TAG_typedef:
6280 case DW_TAG_structure_type:
6281 break;
6282 default:
6283 goto again;
6284 }
6285 break;
6286 default:
6287 break;
4b514bc8 6288 }
927aa2e7
JK
6289
6290 return per_cu;
4b514bc8 6291}
61920122 6292
927aa2e7
JK
6293static struct compunit_symtab *
6294dw2_debug_names_lookup_symbol (struct objfile *objfile, int block_index_int,
6295 const char *name, domain_enum domain)
4b514bc8 6296{
927aa2e7 6297 const block_enum block_index = static_cast<block_enum> (block_index_int);
4b514bc8 6298 dw2_setup (objfile);
61920122 6299
927aa2e7
JK
6300 const auto &mapp = dwarf2_per_objfile->debug_names_table;
6301 if (!mapp)
61920122 6302 {
927aa2e7
JK
6303 /* index is NULL if OBJF_READNOW. */
6304 return NULL;
6305 }
6306 const auto &map = *mapp;
9291a0cd 6307
927aa2e7
JK
6308 dw2_debug_names_iterator iter (map, true /* want_specific_block */,
6309 block_index, domain, name);
9703b513 6310
927aa2e7
JK
6311 struct compunit_symtab *stab_best = NULL;
6312 struct dwarf2_per_cu_data *per_cu;
6313 while ((per_cu = iter.next ()) != NULL)
6314 {
6315 struct symbol *sym, *with_opaque = NULL;
6316 struct compunit_symtab *stab = dw2_instantiate_symtab (per_cu);
6317 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
6318 struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
9703b513 6319
927aa2e7
JK
6320 sym = block_find_symbol (block, name, domain,
6321 block_find_non_opaque_type_preferred,
6322 &with_opaque);
9703b513 6323
927aa2e7
JK
6324 /* Some caution must be observed with overloaded functions and
6325 methods, since the index will not contain any overload
6326 information (but NAME might contain it). */
a3ec0bb1 6327
927aa2e7
JK
6328 if (sym != NULL
6329 && strcmp_iw (SYMBOL_SEARCH_NAME (sym), name) == 0)
6330 return stab;
6331 if (with_opaque != NULL
6332 && strcmp_iw (SYMBOL_SEARCH_NAME (with_opaque), name) == 0)
6333 stab_best = stab;
9703b513 6334
927aa2e7 6335 /* Keep looking through other CUs. */
9703b513
TT
6336 }
6337
927aa2e7 6338 return stab_best;
9703b513
TT
6339}
6340
927aa2e7
JK
6341/* This dumps minimal information about .debug_names. It is called
6342 via "mt print objfiles". The gdb.dwarf2/gdb-index.exp testcase
6343 uses this to verify that .debug_names has been loaded. */
9291a0cd 6344
927aa2e7
JK
6345static void
6346dw2_debug_names_dump (struct objfile *objfile)
6347{
9291a0cd 6348 dw2_setup (objfile);
927aa2e7
JK
6349 gdb_assert (dwarf2_per_objfile->using_index);
6350 printf_filtered (".debug_names:");
6351 if (dwarf2_per_objfile->debug_names_table)
6352 printf_filtered (" exists\n");
6353 else
6354 printf_filtered (" faked for \"readnow\"\n");
6355 printf_filtered ("\n");
9291a0cd
TT
6356}
6357
9291a0cd 6358static void
927aa2e7
JK
6359dw2_debug_names_expand_symtabs_for_function (struct objfile *objfile,
6360 const char *func_name)
9291a0cd 6361{
9291a0cd 6362 dw2_setup (objfile);
ae2de4f8 6363
927aa2e7
JK
6364 /* dwarf2_per_objfile->debug_names_table is NULL if OBJF_READNOW. */
6365 if (dwarf2_per_objfile->debug_names_table)
24c79950 6366 {
927aa2e7 6367 const mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
24c79950 6368
927aa2e7
JK
6369 /* Note: It doesn't matter what we pass for block_index here. */
6370 dw2_debug_names_iterator iter (map, false /* want_specific_block */,
6371 GLOBAL_BLOCK, VAR_DOMAIN, func_name);
24c79950 6372
927aa2e7
JK
6373 struct dwarf2_per_cu_data *per_cu;
6374 while ((per_cu = iter.next ()) != NULL)
6375 dw2_instantiate_symtab (per_cu);
6376 }
6377}
24c79950 6378
927aa2e7
JK
6379static void
6380dw2_debug_names_expand_symtabs_matching
6381 (struct objfile *objfile,
6382 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
6383 const lookup_name_info &lookup_name,
6384 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
6385 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
6386 enum search_domain kind)
6387{
6388 dw2_setup (objfile);
9291a0cd 6389
927aa2e7
JK
6390 /* debug_names_table is NULL if OBJF_READNOW. */
6391 if (!dwarf2_per_objfile->debug_names_table)
6392 return;
9291a0cd 6393
927aa2e7 6394 dw_expand_symtabs_matching_file_matcher (file_matcher);
24c79950 6395
927aa2e7 6396 const mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
bbf2f4df 6397
927aa2e7
JK
6398 for (uint32_t namei = 0; namei < map.name_count; ++namei)
6399 {
6400 QUIT;
74e2f255 6401
927aa2e7
JK
6402 const char *const namei_string = map.namei_to_name (namei);
6403 if (symbol_matcher != NULL && !symbol_matcher (namei_string))
6404 continue;
bbf2f4df 6405
927aa2e7
JK
6406 /* The name was matched, now expand corresponding CUs that were
6407 marked. */
6408 dw2_debug_names_iterator iter (map, kind, namei);
bbf2f4df 6409
927aa2e7
JK
6410 struct dwarf2_per_cu_data *per_cu;
6411 while ((per_cu = iter.next ()) != NULL)
6412 dw2_expand_symtabs_matching_one (per_cu, file_matcher,
6413 expansion_notify);
9291a0cd 6414 }
9291a0cd
TT
6415}
6416
927aa2e7 6417const struct quick_symbol_functions dwarf2_debug_names_functions =
9291a0cd
TT
6418{
6419 dw2_has_symbols,
6420 dw2_find_last_source_symtab,
6421 dw2_forget_cached_source_info,
f8eba3c6 6422 dw2_map_symtabs_matching_filename,
927aa2e7 6423 dw2_debug_names_lookup_symbol,
9291a0cd 6424 dw2_print_stats,
927aa2e7 6425 dw2_debug_names_dump,
9291a0cd 6426 dw2_relocate,
927aa2e7 6427 dw2_debug_names_expand_symtabs_for_function,
9291a0cd 6428 dw2_expand_all_symtabs,
652a8996 6429 dw2_expand_symtabs_with_fullname,
40658b94 6430 dw2_map_matching_symbols,
927aa2e7 6431 dw2_debug_names_expand_symtabs_matching,
43f3e411 6432 dw2_find_pc_sect_compunit_symtab,
71a3c369 6433 NULL,
9291a0cd
TT
6434 dw2_map_symbol_filenames
6435};
6436
6437/* Initialize for reading DWARF for this objfile. Return 0 if this
6438 file will use psymtabs, or 1 if using the GNU index. */
6439
abccd1e7 6440const sym_fns &
9291a0cd
TT
6441dwarf2_initialize_objfile (struct objfile *objfile)
6442{
6443 /* If we're about to read full symbols, don't bother with the
6444 indices. In this case we also don't care if some other debug
6445 format is making psymtabs, because they are all about to be
6446 expanded anyway. */
6447 if ((objfile->flags & OBJF_READNOW))
6448 {
6449 int i;
6450
6451 dwarf2_per_objfile->using_index = 1;
6452 create_all_comp_units (objfile);
0e50663e 6453 create_all_type_units (objfile);
7b9f3c50
DE
6454 dwarf2_per_objfile->quick_file_names_table =
6455 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
9291a0cd 6456
1fd400ff 6457 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
d467dd73 6458 + dwarf2_per_objfile->n_type_units); ++i)
9291a0cd 6459 {
8832e7e3 6460 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
9291a0cd 6461
e254ef6a
DE
6462 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6463 struct dwarf2_per_cu_quick_data);
9291a0cd
TT
6464 }
6465
6466 /* Return 1 so that gdb sees the "quick" functions. However,
6467 these functions will be no-ops because we will have expanded
6468 all symtabs. */
abccd1e7 6469 return elf_sym_fns_gdb_index;
9291a0cd
TT
6470 }
6471
927aa2e7
JK
6472 if (dwarf2_read_debug_names (objfile))
6473 return elf_sym_fns_debug_names;
6474
9291a0cd 6475 if (dwarf2_read_index (objfile))
abccd1e7 6476 return elf_sym_fns_gdb_index;
9291a0cd 6477
abccd1e7 6478 return elf_sym_fns_lazy_psyms;
9291a0cd
TT
6479}
6480
6481\f
6482
dce234bc
PP
6483/* Build a partial symbol table. */
6484
6485void
f29dff0a 6486dwarf2_build_psymtabs (struct objfile *objfile)
dce234bc 6487{
c9bf0622 6488
af5bf4ad
SM
6489 if (objfile->global_psymbols.capacity () == 0
6490 && objfile->static_psymbols.capacity () == 0)
6491 init_psymbol_list (objfile, 1024);
c906108c 6492
492d29ea 6493 TRY
c9bf0622
TT
6494 {
6495 /* This isn't really ideal: all the data we allocate on the
6496 objfile's obstack is still uselessly kept around. However,
6497 freeing it seems unsafe. */
906768f9 6498 psymtab_discarder psymtabs (objfile);
c9bf0622 6499 dwarf2_build_psymtabs_hard (objfile);
906768f9 6500 psymtabs.keep ();
c9bf0622 6501 }
492d29ea
PA
6502 CATCH (except, RETURN_MASK_ERROR)
6503 {
6504 exception_print (gdb_stderr, except);
6505 }
6506 END_CATCH
c906108c 6507}
c906108c 6508
1ce1cefd
DE
6509/* Return the total length of the CU described by HEADER. */
6510
6511static unsigned int
6512get_cu_length (const struct comp_unit_head *header)
6513{
6514 return header->initial_length_size + header->length;
6515}
6516
9c541725 6517/* Return TRUE if SECT_OFF is within CU_HEADER. */
45452591 6518
9c541725
PA
6519static inline bool
6520offset_in_cu_p (const comp_unit_head *cu_header, sect_offset sect_off)
45452591 6521{
9c541725
PA
6522 sect_offset bottom = cu_header->sect_off;
6523 sect_offset top = cu_header->sect_off + get_cu_length (cu_header);
9a619af0 6524
9c541725 6525 return sect_off >= bottom && sect_off < top;
45452591
DE
6526}
6527
3b80fe9b
DE
6528/* Find the base address of the compilation unit for range lists and
6529 location lists. It will normally be specified by DW_AT_low_pc.
6530 In DWARF-3 draft 4, the base address could be overridden by
6531 DW_AT_entry_pc. It's been removed, but GCC still uses this for
6532 compilation units with discontinuous ranges. */
6533
6534static void
6535dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
6536{
6537 struct attribute *attr;
6538
6539 cu->base_known = 0;
6540 cu->base_address = 0;
6541
6542 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
6543 if (attr)
6544 {
31aa7e4e 6545 cu->base_address = attr_value_as_address (attr);
3b80fe9b
DE
6546 cu->base_known = 1;
6547 }
6548 else
6549 {
6550 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
6551 if (attr)
6552 {
31aa7e4e 6553 cu->base_address = attr_value_as_address (attr);
3b80fe9b
DE
6554 cu->base_known = 1;
6555 }
6556 }
6557}
6558
93311388 6559/* Read in the comp unit header information from the debug_info at info_ptr.
43988095 6560 Use rcuh_kind::COMPILE as the default type if not known by the caller.
93311388
DE
6561 NOTE: This leaves members offset, first_die_offset to be filled in
6562 by the caller. */
107d2387 6563
d521ce57 6564static const gdb_byte *
107d2387 6565read_comp_unit_head (struct comp_unit_head *cu_header,
43988095
JK
6566 const gdb_byte *info_ptr,
6567 struct dwarf2_section_info *section,
6568 rcuh_kind section_kind)
107d2387
AC
6569{
6570 int signed_addr;
891d2f0b 6571 unsigned int bytes_read;
43988095
JK
6572 const char *filename = get_section_file_name (section);
6573 bfd *abfd = get_section_bfd_owner (section);
c764a876
DE
6574
6575 cu_header->length = read_initial_length (abfd, info_ptr, &bytes_read);
6576 cu_header->initial_length_size = bytes_read;
6577 cu_header->offset_size = (bytes_read == 4) ? 4 : 8;
613e1657 6578 info_ptr += bytes_read;
107d2387
AC
6579 cu_header->version = read_2_bytes (abfd, info_ptr);
6580 info_ptr += 2;
43988095
JK
6581 if (cu_header->version < 5)
6582 switch (section_kind)
6583 {
6584 case rcuh_kind::COMPILE:
6585 cu_header->unit_type = DW_UT_compile;
6586 break;
6587 case rcuh_kind::TYPE:
6588 cu_header->unit_type = DW_UT_type;
6589 break;
6590 default:
6591 internal_error (__FILE__, __LINE__,
6592 _("read_comp_unit_head: invalid section_kind"));
6593 }
6594 else
6595 {
6596 cu_header->unit_type = static_cast<enum dwarf_unit_type>
6597 (read_1_byte (abfd, info_ptr));
6598 info_ptr += 1;
6599 switch (cu_header->unit_type)
6600 {
6601 case DW_UT_compile:
6602 if (section_kind != rcuh_kind::COMPILE)
6603 error (_("Dwarf Error: wrong unit_type in compilation unit header "
6604 "(is DW_UT_compile, should be DW_UT_type) [in module %s]"),
6605 filename);
6606 break;
6607 case DW_UT_type:
6608 section_kind = rcuh_kind::TYPE;
6609 break;
6610 default:
6611 error (_("Dwarf Error: wrong unit_type in compilation unit header "
6612 "(is %d, should be %d or %d) [in module %s]"),
6613 cu_header->unit_type, DW_UT_compile, DW_UT_type, filename);
6614 }
6615
6616 cu_header->addr_size = read_1_byte (abfd, info_ptr);
6617 info_ptr += 1;
6618 }
9c541725
PA
6619 cu_header->abbrev_sect_off = (sect_offset) read_offset (abfd, info_ptr,
6620 cu_header,
6621 &bytes_read);
613e1657 6622 info_ptr += bytes_read;
43988095
JK
6623 if (cu_header->version < 5)
6624 {
6625 cu_header->addr_size = read_1_byte (abfd, info_ptr);
6626 info_ptr += 1;
6627 }
107d2387
AC
6628 signed_addr = bfd_get_sign_extend_vma (abfd);
6629 if (signed_addr < 0)
8e65ff28 6630 internal_error (__FILE__, __LINE__,
e2e0b3e5 6631 _("read_comp_unit_head: dwarf from non elf file"));
107d2387 6632 cu_header->signed_addr_p = signed_addr;
c764a876 6633
43988095
JK
6634 if (section_kind == rcuh_kind::TYPE)
6635 {
6636 LONGEST type_offset;
6637
6638 cu_header->signature = read_8_bytes (abfd, info_ptr);
6639 info_ptr += 8;
6640
6641 type_offset = read_offset (abfd, info_ptr, cu_header, &bytes_read);
6642 info_ptr += bytes_read;
9c541725
PA
6643 cu_header->type_cu_offset_in_tu = (cu_offset) type_offset;
6644 if (to_underlying (cu_header->type_cu_offset_in_tu) != type_offset)
43988095
JK
6645 error (_("Dwarf Error: Too big type_offset in compilation unit "
6646 "header (is %s) [in module %s]"), plongest (type_offset),
6647 filename);
6648 }
6649
107d2387
AC
6650 return info_ptr;
6651}
6652
36586728
TT
6653/* Helper function that returns the proper abbrev section for
6654 THIS_CU. */
6655
6656static struct dwarf2_section_info *
6657get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
6658{
6659 struct dwarf2_section_info *abbrev;
6660
6661 if (this_cu->is_dwz)
6662 abbrev = &dwarf2_get_dwz_file ()->abbrev;
6663 else
6664 abbrev = &dwarf2_per_objfile->abbrev;
6665
6666 return abbrev;
6667}
6668
9ff913ba
DE
6669/* Subroutine of read_and_check_comp_unit_head and
6670 read_and_check_type_unit_head to simplify them.
6671 Perform various error checking on the header. */
6672
6673static void
6674error_check_comp_unit_head (struct comp_unit_head *header,
4bdcc0c1
DE
6675 struct dwarf2_section_info *section,
6676 struct dwarf2_section_info *abbrev_section)
9ff913ba 6677{
a32a8923 6678 const char *filename = get_section_file_name (section);
9ff913ba 6679
43988095 6680 if (header->version < 2 || header->version > 5)
9ff913ba 6681 error (_("Dwarf Error: wrong version in compilation unit header "
43988095 6682 "(is %d, should be 2, 3, 4 or 5) [in module %s]"), header->version,
9ff913ba
DE
6683 filename);
6684
9c541725 6685 if (to_underlying (header->abbrev_sect_off)
36586728 6686 >= dwarf2_section_size (dwarf2_per_objfile->objfile, abbrev_section))
9c541725
PA
6687 error (_("Dwarf Error: bad offset (0x%x) in compilation unit header "
6688 "(offset 0x%x + 6) [in module %s]"),
6689 to_underlying (header->abbrev_sect_off),
6690 to_underlying (header->sect_off),
9ff913ba
DE
6691 filename);
6692
9c541725 6693 /* Cast to ULONGEST to use 64-bit arithmetic when possible to
9ff913ba 6694 avoid potential 32-bit overflow. */
9c541725 6695 if (((ULONGEST) header->sect_off + get_cu_length (header))
9ff913ba 6696 > section->size)
9c541725
PA
6697 error (_("Dwarf Error: bad length (0x%x) in compilation unit header "
6698 "(offset 0x%x + 0) [in module %s]"),
6699 header->length, to_underlying (header->sect_off),
9ff913ba
DE
6700 filename);
6701}
6702
6703/* Read in a CU/TU header and perform some basic error checking.
6704 The contents of the header are stored in HEADER.
6705 The result is a pointer to the start of the first DIE. */
adabb602 6706
d521ce57 6707static const gdb_byte *
9ff913ba
DE
6708read_and_check_comp_unit_head (struct comp_unit_head *header,
6709 struct dwarf2_section_info *section,
4bdcc0c1 6710 struct dwarf2_section_info *abbrev_section,
d521ce57 6711 const gdb_byte *info_ptr,
43988095 6712 rcuh_kind section_kind)
72bf9492 6713{
d521ce57 6714 const gdb_byte *beg_of_comp_unit = info_ptr;
72bf9492 6715
9c541725 6716 header->sect_off = (sect_offset) (beg_of_comp_unit - section->buffer);
adabb602 6717
43988095 6718 info_ptr = read_comp_unit_head (header, info_ptr, section, section_kind);
9ff913ba 6719
9c541725 6720 header->first_die_cu_offset = (cu_offset) (info_ptr - beg_of_comp_unit);
348e048f 6721
4bdcc0c1 6722 error_check_comp_unit_head (header, section, abbrev_section);
9ff913ba
DE
6723
6724 return info_ptr;
348e048f
DE
6725}
6726
f4dc4d17
DE
6727/* Fetch the abbreviation table offset from a comp or type unit header. */
6728
6729static sect_offset
6730read_abbrev_offset (struct dwarf2_section_info *section,
9c541725 6731 sect_offset sect_off)
f4dc4d17 6732{
a32a8923 6733 bfd *abfd = get_section_bfd_owner (section);
d521ce57 6734 const gdb_byte *info_ptr;
ac298888 6735 unsigned int initial_length_size, offset_size;
43988095 6736 uint16_t version;
f4dc4d17
DE
6737
6738 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
9c541725 6739 info_ptr = section->buffer + to_underlying (sect_off);
ac298888 6740 read_initial_length (abfd, info_ptr, &initial_length_size);
f4dc4d17 6741 offset_size = initial_length_size == 4 ? 4 : 8;
43988095
JK
6742 info_ptr += initial_length_size;
6743
6744 version = read_2_bytes (abfd, info_ptr);
6745 info_ptr += 2;
6746 if (version >= 5)
6747 {
6748 /* Skip unit type and address size. */
6749 info_ptr += 2;
6750 }
6751
9c541725 6752 return (sect_offset) read_offset_1 (abfd, info_ptr, offset_size);
f4dc4d17
DE
6753}
6754
aaa75496
JB
6755/* Allocate a new partial symtab for file named NAME and mark this new
6756 partial symtab as being an include of PST. */
6757
6758static void
d521ce57 6759dwarf2_create_include_psymtab (const char *name, struct partial_symtab *pst,
aaa75496
JB
6760 struct objfile *objfile)
6761{
6762 struct partial_symtab *subpst = allocate_psymtab (name, objfile);
6763
fbd9ab74
JK
6764 if (!IS_ABSOLUTE_PATH (subpst->filename))
6765 {
6766 /* It shares objfile->objfile_obstack. */
6767 subpst->dirname = pst->dirname;
6768 }
6769
aaa75496
JB
6770 subpst->textlow = 0;
6771 subpst->texthigh = 0;
6772
8d749320
SM
6773 subpst->dependencies
6774 = XOBNEW (&objfile->objfile_obstack, struct partial_symtab *);
aaa75496
JB
6775 subpst->dependencies[0] = pst;
6776 subpst->number_of_dependencies = 1;
6777
6778 subpst->globals_offset = 0;
6779 subpst->n_global_syms = 0;
6780 subpst->statics_offset = 0;
6781 subpst->n_static_syms = 0;
43f3e411 6782 subpst->compunit_symtab = NULL;
aaa75496
JB
6783 subpst->read_symtab = pst->read_symtab;
6784 subpst->readin = 0;
6785
6786 /* No private part is necessary for include psymtabs. This property
6787 can be used to differentiate between such include psymtabs and
10b3939b 6788 the regular ones. */
58a9656e 6789 subpst->read_symtab_private = NULL;
aaa75496
JB
6790}
6791
6792/* Read the Line Number Program data and extract the list of files
6793 included by the source file represented by PST. Build an include
d85a05f0 6794 partial symtab for each of these included files. */
aaa75496
JB
6795
6796static void
6797dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
dee91e82
DE
6798 struct die_info *die,
6799 struct partial_symtab *pst)
aaa75496 6800{
fff8551c 6801 line_header_up lh;
d85a05f0 6802 struct attribute *attr;
aaa75496 6803
d85a05f0
DJ
6804 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
6805 if (attr)
9c541725 6806 lh = dwarf_decode_line_header ((sect_offset) DW_UNSND (attr), cu);
aaa75496
JB
6807 if (lh == NULL)
6808 return; /* No linetable, so no includes. */
6809
c6da4cef 6810 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). */
fff8551c 6811 dwarf_decode_lines (lh.get (), pst->dirname, cu, pst, pst->textlow, 1);
aaa75496
JB
6812}
6813
348e048f 6814static hashval_t
52dc124a 6815hash_signatured_type (const void *item)
348e048f 6816{
9a3c8263
SM
6817 const struct signatured_type *sig_type
6818 = (const struct signatured_type *) item;
9a619af0 6819
348e048f 6820 /* This drops the top 32 bits of the signature, but is ok for a hash. */
52dc124a 6821 return sig_type->signature;
348e048f
DE
6822}
6823
6824static int
52dc124a 6825eq_signatured_type (const void *item_lhs, const void *item_rhs)
348e048f 6826{
9a3c8263
SM
6827 const struct signatured_type *lhs = (const struct signatured_type *) item_lhs;
6828 const struct signatured_type *rhs = (const struct signatured_type *) item_rhs;
9a619af0 6829
348e048f
DE
6830 return lhs->signature == rhs->signature;
6831}
6832
1fd400ff
TT
6833/* Allocate a hash table for signatured types. */
6834
6835static htab_t
673bfd45 6836allocate_signatured_type_table (struct objfile *objfile)
1fd400ff
TT
6837{
6838 return htab_create_alloc_ex (41,
52dc124a
DE
6839 hash_signatured_type,
6840 eq_signatured_type,
1fd400ff
TT
6841 NULL,
6842 &objfile->objfile_obstack,
6843 hashtab_obstack_allocate,
6844 dummy_obstack_deallocate);
6845}
6846
d467dd73 6847/* A helper function to add a signatured type CU to a table. */
1fd400ff
TT
6848
6849static int
d467dd73 6850add_signatured_type_cu_to_table (void **slot, void *datum)
1fd400ff 6851{
9a3c8263
SM
6852 struct signatured_type *sigt = (struct signatured_type *) *slot;
6853 struct signatured_type ***datap = (struct signatured_type ***) datum;
1fd400ff 6854
b4dd5633 6855 **datap = sigt;
1fd400ff
TT
6856 ++*datap;
6857
6858 return 1;
6859}
6860
78d4d2c5 6861/* A helper for create_debug_types_hash_table. Read types from SECTION
43988095
JK
6862 and fill them into TYPES_HTAB. It will process only type units,
6863 therefore DW_UT_type. */
c88ee1f0 6864
78d4d2c5
JK
6865static void
6866create_debug_type_hash_table (struct dwo_file *dwo_file,
43988095
JK
6867 dwarf2_section_info *section, htab_t &types_htab,
6868 rcuh_kind section_kind)
348e048f 6869{
3019eac3 6870 struct objfile *objfile = dwarf2_per_objfile->objfile;
4bdcc0c1 6871 struct dwarf2_section_info *abbrev_section;
78d4d2c5
JK
6872 bfd *abfd;
6873 const gdb_byte *info_ptr, *end_ptr;
348e048f 6874
4bdcc0c1
DE
6875 abbrev_section = (dwo_file != NULL
6876 ? &dwo_file->sections.abbrev
6877 : &dwarf2_per_objfile->abbrev);
6878
b4f54984 6879 if (dwarf_read_debug)
43988095
JK
6880 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
6881 get_section_name (section),
a32a8923 6882 get_section_file_name (abbrev_section));
09406207 6883
78d4d2c5
JK
6884 dwarf2_read_section (objfile, section);
6885 info_ptr = section->buffer;
348e048f 6886
78d4d2c5
JK
6887 if (info_ptr == NULL)
6888 return;
348e048f 6889
78d4d2c5
JK
6890 /* We can't set abfd until now because the section may be empty or
6891 not present, in which case the bfd is unknown. */
6892 abfd = get_section_bfd_owner (section);
348e048f 6893
78d4d2c5
JK
6894 /* We don't use init_cutu_and_read_dies_simple, or some such, here
6895 because we don't need to read any dies: the signature is in the
6896 header. */
3019eac3 6897
78d4d2c5
JK
6898 end_ptr = info_ptr + section->size;
6899 while (info_ptr < end_ptr)
6900 {
78d4d2c5
JK
6901 struct signatured_type *sig_type;
6902 struct dwo_unit *dwo_tu;
6903 void **slot;
6904 const gdb_byte *ptr = info_ptr;
6905 struct comp_unit_head header;
6906 unsigned int length;
8b70b953 6907
9c541725 6908 sect_offset sect_off = (sect_offset) (ptr - section->buffer);
348e048f 6909
a49dd8dd
JK
6910 /* Initialize it due to a false compiler warning. */
6911 header.signature = -1;
9c541725 6912 header.type_cu_offset_in_tu = (cu_offset) -1;
a49dd8dd 6913
78d4d2c5
JK
6914 /* We need to read the type's signature in order to build the hash
6915 table, but we don't need anything else just yet. */
348e048f 6916
43988095
JK
6917 ptr = read_and_check_comp_unit_head (&header, section,
6918 abbrev_section, ptr, section_kind);
348e048f 6919
78d4d2c5 6920 length = get_cu_length (&header);
6caca83c 6921
78d4d2c5
JK
6922 /* Skip dummy type units. */
6923 if (ptr >= info_ptr + length
43988095
JK
6924 || peek_abbrev_code (abfd, ptr) == 0
6925 || header.unit_type != DW_UT_type)
78d4d2c5
JK
6926 {
6927 info_ptr += length;
6928 continue;
6929 }
dee91e82 6930
78d4d2c5
JK
6931 if (types_htab == NULL)
6932 {
6933 if (dwo_file)
6934 types_htab = allocate_dwo_unit_table (objfile);
6935 else
6936 types_htab = allocate_signatured_type_table (objfile);
6937 }
8b70b953 6938
78d4d2c5
JK
6939 if (dwo_file)
6940 {
6941 sig_type = NULL;
6942 dwo_tu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6943 struct dwo_unit);
6944 dwo_tu->dwo_file = dwo_file;
43988095 6945 dwo_tu->signature = header.signature;
9c541725 6946 dwo_tu->type_offset_in_tu = header.type_cu_offset_in_tu;
78d4d2c5 6947 dwo_tu->section = section;
9c541725 6948 dwo_tu->sect_off = sect_off;
78d4d2c5
JK
6949 dwo_tu->length = length;
6950 }
6951 else
6952 {
6953 /* N.B.: type_offset is not usable if this type uses a DWO file.
6954 The real type_offset is in the DWO file. */
6955 dwo_tu = NULL;
6956 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6957 struct signatured_type);
43988095 6958 sig_type->signature = header.signature;
9c541725 6959 sig_type->type_offset_in_tu = header.type_cu_offset_in_tu;
78d4d2c5
JK
6960 sig_type->per_cu.objfile = objfile;
6961 sig_type->per_cu.is_debug_types = 1;
6962 sig_type->per_cu.section = section;
9c541725 6963 sig_type->per_cu.sect_off = sect_off;
78d4d2c5
JK
6964 sig_type->per_cu.length = length;
6965 }
6966
6967 slot = htab_find_slot (types_htab,
6968 dwo_file ? (void*) dwo_tu : (void *) sig_type,
6969 INSERT);
6970 gdb_assert (slot != NULL);
6971 if (*slot != NULL)
6972 {
9c541725 6973 sect_offset dup_sect_off;
0349ea22 6974
3019eac3
DE
6975 if (dwo_file)
6976 {
78d4d2c5
JK
6977 const struct dwo_unit *dup_tu
6978 = (const struct dwo_unit *) *slot;
6979
9c541725 6980 dup_sect_off = dup_tu->sect_off;
3019eac3
DE
6981 }
6982 else
6983 {
78d4d2c5
JK
6984 const struct signatured_type *dup_tu
6985 = (const struct signatured_type *) *slot;
6986
9c541725 6987 dup_sect_off = dup_tu->per_cu.sect_off;
3019eac3 6988 }
8b70b953 6989
78d4d2c5
JK
6990 complaint (&symfile_complaints,
6991 _("debug type entry at offset 0x%x is duplicate to"
6992 " the entry at offset 0x%x, signature %s"),
9c541725 6993 to_underlying (sect_off), to_underlying (dup_sect_off),
43988095 6994 hex_string (header.signature));
78d4d2c5
JK
6995 }
6996 *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
3019eac3 6997
78d4d2c5
JK
6998 if (dwarf_read_debug > 1)
6999 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, signature %s\n",
9c541725 7000 to_underlying (sect_off),
43988095 7001 hex_string (header.signature));
3019eac3 7002
78d4d2c5
JK
7003 info_ptr += length;
7004 }
7005}
3019eac3 7006
78d4d2c5
JK
7007/* Create the hash table of all entries in the .debug_types
7008 (or .debug_types.dwo) section(s).
7009 If reading a DWO file, then DWO_FILE is a pointer to the DWO file object,
7010 otherwise it is NULL.
b3c8eb43 7011
78d4d2c5 7012 The result is a pointer to the hash table or NULL if there are no types.
348e048f 7013
78d4d2c5 7014 Note: This function processes DWO files only, not DWP files. */
348e048f 7015
78d4d2c5
JK
7016static void
7017create_debug_types_hash_table (struct dwo_file *dwo_file,
7018 VEC (dwarf2_section_info_def) *types,
7019 htab_t &types_htab)
7020{
7021 int ix;
7022 struct dwarf2_section_info *section;
7023
7024 if (VEC_empty (dwarf2_section_info_def, types))
7025 return;
348e048f 7026
78d4d2c5
JK
7027 for (ix = 0;
7028 VEC_iterate (dwarf2_section_info_def, types, ix, section);
7029 ++ix)
43988095
JK
7030 create_debug_type_hash_table (dwo_file, section, types_htab,
7031 rcuh_kind::TYPE);
3019eac3
DE
7032}
7033
7034/* Create the hash table of all entries in the .debug_types section,
7035 and initialize all_type_units.
7036 The result is zero if there is an error (e.g. missing .debug_types section),
7037 otherwise non-zero. */
7038
7039static int
7040create_all_type_units (struct objfile *objfile)
7041{
78d4d2c5 7042 htab_t types_htab = NULL;
b4dd5633 7043 struct signatured_type **iter;
3019eac3 7044
43988095
JK
7045 create_debug_type_hash_table (NULL, &dwarf2_per_objfile->info, types_htab,
7046 rcuh_kind::COMPILE);
78d4d2c5 7047 create_debug_types_hash_table (NULL, dwarf2_per_objfile->types, types_htab);
3019eac3
DE
7048 if (types_htab == NULL)
7049 {
7050 dwarf2_per_objfile->signatured_types = NULL;
7051 return 0;
7052 }
7053
348e048f
DE
7054 dwarf2_per_objfile->signatured_types = types_htab;
7055
6aa5f3a6
DE
7056 dwarf2_per_objfile->n_type_units
7057 = dwarf2_per_objfile->n_allocated_type_units
7058 = htab_elements (types_htab);
8d749320
SM
7059 dwarf2_per_objfile->all_type_units =
7060 XNEWVEC (struct signatured_type *, dwarf2_per_objfile->n_type_units);
d467dd73
DE
7061 iter = &dwarf2_per_objfile->all_type_units[0];
7062 htab_traverse_noresize (types_htab, add_signatured_type_cu_to_table, &iter);
7063 gdb_assert (iter - &dwarf2_per_objfile->all_type_units[0]
7064 == dwarf2_per_objfile->n_type_units);
1fd400ff 7065
348e048f
DE
7066 return 1;
7067}
7068
6aa5f3a6
DE
7069/* Add an entry for signature SIG to dwarf2_per_objfile->signatured_types.
7070 If SLOT is non-NULL, it is the entry to use in the hash table.
7071 Otherwise we find one. */
7072
7073static struct signatured_type *
7074add_type_unit (ULONGEST sig, void **slot)
7075{
7076 struct objfile *objfile = dwarf2_per_objfile->objfile;
7077 int n_type_units = dwarf2_per_objfile->n_type_units;
7078 struct signatured_type *sig_type;
7079
7080 gdb_assert (n_type_units <= dwarf2_per_objfile->n_allocated_type_units);
7081 ++n_type_units;
7082 if (n_type_units > dwarf2_per_objfile->n_allocated_type_units)
7083 {
7084 if (dwarf2_per_objfile->n_allocated_type_units == 0)
7085 dwarf2_per_objfile->n_allocated_type_units = 1;
7086 dwarf2_per_objfile->n_allocated_type_units *= 2;
7087 dwarf2_per_objfile->all_type_units
224c3ddb
SM
7088 = XRESIZEVEC (struct signatured_type *,
7089 dwarf2_per_objfile->all_type_units,
7090 dwarf2_per_objfile->n_allocated_type_units);
6aa5f3a6
DE
7091 ++dwarf2_per_objfile->tu_stats.nr_all_type_units_reallocs;
7092 }
7093 dwarf2_per_objfile->n_type_units = n_type_units;
7094
7095 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
7096 struct signatured_type);
7097 dwarf2_per_objfile->all_type_units[n_type_units - 1] = sig_type;
7098 sig_type->signature = sig;
7099 sig_type->per_cu.is_debug_types = 1;
7100 if (dwarf2_per_objfile->using_index)
7101 {
7102 sig_type->per_cu.v.quick =
7103 OBSTACK_ZALLOC (&objfile->objfile_obstack,
7104 struct dwarf2_per_cu_quick_data);
7105 }
7106
7107 if (slot == NULL)
7108 {
7109 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
7110 sig_type, INSERT);
7111 }
7112 gdb_assert (*slot == NULL);
7113 *slot = sig_type;
7114 /* The rest of sig_type must be filled in by the caller. */
7115 return sig_type;
7116}
7117
a2ce51a0
DE
7118/* Subroutine of lookup_dwo_signatured_type and lookup_dwp_signatured_type.
7119 Fill in SIG_ENTRY with DWO_ENTRY. */
7120
7121static void
7122fill_in_sig_entry_from_dwo_entry (struct objfile *objfile,
7123 struct signatured_type *sig_entry,
7124 struct dwo_unit *dwo_entry)
7125{
7ee85ab1 7126 /* Make sure we're not clobbering something we don't expect to. */
a2ce51a0
DE
7127 gdb_assert (! sig_entry->per_cu.queued);
7128 gdb_assert (sig_entry->per_cu.cu == NULL);
6aa5f3a6
DE
7129 if (dwarf2_per_objfile->using_index)
7130 {
7131 gdb_assert (sig_entry->per_cu.v.quick != NULL);
43f3e411 7132 gdb_assert (sig_entry->per_cu.v.quick->compunit_symtab == NULL);
6aa5f3a6
DE
7133 }
7134 else
7135 gdb_assert (sig_entry->per_cu.v.psymtab == NULL);
a2ce51a0 7136 gdb_assert (sig_entry->signature == dwo_entry->signature);
9c541725 7137 gdb_assert (to_underlying (sig_entry->type_offset_in_section) == 0);
a2ce51a0 7138 gdb_assert (sig_entry->type_unit_group == NULL);
7ee85ab1
DE
7139 gdb_assert (sig_entry->dwo_unit == NULL);
7140
7141 sig_entry->per_cu.section = dwo_entry->section;
9c541725 7142 sig_entry->per_cu.sect_off = dwo_entry->sect_off;
7ee85ab1
DE
7143 sig_entry->per_cu.length = dwo_entry->length;
7144 sig_entry->per_cu.reading_dwo_directly = 1;
7145 sig_entry->per_cu.objfile = objfile;
a2ce51a0
DE
7146 sig_entry->type_offset_in_tu = dwo_entry->type_offset_in_tu;
7147 sig_entry->dwo_unit = dwo_entry;
7148}
7149
7150/* Subroutine of lookup_signatured_type.
7ee85ab1
DE
7151 If we haven't read the TU yet, create the signatured_type data structure
7152 for a TU to be read in directly from a DWO file, bypassing the stub.
7153 This is the "Stay in DWO Optimization": When there is no DWP file and we're
7154 using .gdb_index, then when reading a CU we want to stay in the DWO file
7155 containing that CU. Otherwise we could end up reading several other DWO
7156 files (due to comdat folding) to process the transitive closure of all the
7157 mentioned TUs, and that can be slow. The current DWO file will have every
7158 type signature that it needs.
a2ce51a0
DE
7159 We only do this for .gdb_index because in the psymtab case we already have
7160 to read all the DWOs to build the type unit groups. */
7161
7162static struct signatured_type *
7163lookup_dwo_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
7164{
7165 struct objfile *objfile = dwarf2_per_objfile->objfile;
7166 struct dwo_file *dwo_file;
7167 struct dwo_unit find_dwo_entry, *dwo_entry;
7168 struct signatured_type find_sig_entry, *sig_entry;
6aa5f3a6 7169 void **slot;
a2ce51a0
DE
7170
7171 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
7172
6aa5f3a6
DE
7173 /* If TU skeletons have been removed then we may not have read in any
7174 TUs yet. */
7175 if (dwarf2_per_objfile->signatured_types == NULL)
7176 {
7177 dwarf2_per_objfile->signatured_types
7178 = allocate_signatured_type_table (objfile);
7179 }
a2ce51a0
DE
7180
7181 /* We only ever need to read in one copy of a signatured type.
6aa5f3a6
DE
7182 Use the global signatured_types array to do our own comdat-folding
7183 of types. If this is the first time we're reading this TU, and
7184 the TU has an entry in .gdb_index, replace the recorded data from
7185 .gdb_index with this TU. */
a2ce51a0 7186
a2ce51a0 7187 find_sig_entry.signature = sig;
6aa5f3a6
DE
7188 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
7189 &find_sig_entry, INSERT);
9a3c8263 7190 sig_entry = (struct signatured_type *) *slot;
7ee85ab1
DE
7191
7192 /* We can get here with the TU already read, *or* in the process of being
6aa5f3a6
DE
7193 read. Don't reassign the global entry to point to this DWO if that's
7194 the case. Also note that if the TU is already being read, it may not
7195 have come from a DWO, the program may be a mix of Fission-compiled
7196 code and non-Fission-compiled code. */
7197
7198 /* Have we already tried to read this TU?
7199 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
7200 needn't exist in the global table yet). */
7201 if (sig_entry != NULL && sig_entry->per_cu.tu_read)
a2ce51a0
DE
7202 return sig_entry;
7203
6aa5f3a6
DE
7204 /* Note: cu->dwo_unit is the dwo_unit that references this TU, not the
7205 dwo_unit of the TU itself. */
7206 dwo_file = cu->dwo_unit->dwo_file;
7207
a2ce51a0
DE
7208 /* Ok, this is the first time we're reading this TU. */
7209 if (dwo_file->tus == NULL)
7210 return NULL;
7211 find_dwo_entry.signature = sig;
9a3c8263 7212 dwo_entry = (struct dwo_unit *) htab_find (dwo_file->tus, &find_dwo_entry);
a2ce51a0
DE
7213 if (dwo_entry == NULL)
7214 return NULL;
7215
6aa5f3a6
DE
7216 /* If the global table doesn't have an entry for this TU, add one. */
7217 if (sig_entry == NULL)
7218 sig_entry = add_type_unit (sig, slot);
7219
a2ce51a0 7220 fill_in_sig_entry_from_dwo_entry (objfile, sig_entry, dwo_entry);
89e63ee4 7221 sig_entry->per_cu.tu_read = 1;
a2ce51a0
DE
7222 return sig_entry;
7223}
7224
a2ce51a0
DE
7225/* Subroutine of lookup_signatured_type.
7226 Look up the type for signature SIG, and if we can't find SIG in .gdb_index
6aa5f3a6
DE
7227 then try the DWP file. If the TU stub (skeleton) has been removed then
7228 it won't be in .gdb_index. */
a2ce51a0
DE
7229
7230static struct signatured_type *
7231lookup_dwp_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
7232{
7233 struct objfile *objfile = dwarf2_per_objfile->objfile;
7234 struct dwp_file *dwp_file = get_dwp_file ();
7235 struct dwo_unit *dwo_entry;
7236 struct signatured_type find_sig_entry, *sig_entry;
6aa5f3a6 7237 void **slot;
a2ce51a0
DE
7238
7239 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
7240 gdb_assert (dwp_file != NULL);
7241
6aa5f3a6
DE
7242 /* If TU skeletons have been removed then we may not have read in any
7243 TUs yet. */
7244 if (dwarf2_per_objfile->signatured_types == NULL)
a2ce51a0 7245 {
6aa5f3a6
DE
7246 dwarf2_per_objfile->signatured_types
7247 = allocate_signatured_type_table (objfile);
a2ce51a0
DE
7248 }
7249
6aa5f3a6
DE
7250 find_sig_entry.signature = sig;
7251 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
7252 &find_sig_entry, INSERT);
9a3c8263 7253 sig_entry = (struct signatured_type *) *slot;
6aa5f3a6
DE
7254
7255 /* Have we already tried to read this TU?
7256 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
7257 needn't exist in the global table yet). */
7258 if (sig_entry != NULL)
7259 return sig_entry;
7260
a2ce51a0
DE
7261 if (dwp_file->tus == NULL)
7262 return NULL;
57d63ce2
DE
7263 dwo_entry = lookup_dwo_unit_in_dwp (dwp_file, NULL,
7264 sig, 1 /* is_debug_types */);
a2ce51a0
DE
7265 if (dwo_entry == NULL)
7266 return NULL;
7267
6aa5f3a6 7268 sig_entry = add_type_unit (sig, slot);
a2ce51a0
DE
7269 fill_in_sig_entry_from_dwo_entry (objfile, sig_entry, dwo_entry);
7270
a2ce51a0
DE
7271 return sig_entry;
7272}
7273
380bca97 7274/* Lookup a signature based type for DW_FORM_ref_sig8.
5a8b3f62
DE
7275 Returns NULL if signature SIG is not present in the table.
7276 It is up to the caller to complain about this. */
348e048f
DE
7277
7278static struct signatured_type *
a2ce51a0 7279lookup_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
348e048f 7280{
a2ce51a0
DE
7281 if (cu->dwo_unit
7282 && dwarf2_per_objfile->using_index)
7283 {
7284 /* We're in a DWO/DWP file, and we're using .gdb_index.
7285 These cases require special processing. */
7286 if (get_dwp_file () == NULL)
7287 return lookup_dwo_signatured_type (cu, sig);
7288 else
7289 return lookup_dwp_signatured_type (cu, sig);
7290 }
7291 else
7292 {
7293 struct signatured_type find_entry, *entry;
348e048f 7294
a2ce51a0
DE
7295 if (dwarf2_per_objfile->signatured_types == NULL)
7296 return NULL;
7297 find_entry.signature = sig;
9a3c8263
SM
7298 entry = ((struct signatured_type *)
7299 htab_find (dwarf2_per_objfile->signatured_types, &find_entry));
a2ce51a0
DE
7300 return entry;
7301 }
348e048f 7302}
42e7ad6c
DE
7303\f
7304/* Low level DIE reading support. */
348e048f 7305
d85a05f0
DJ
7306/* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
7307
7308static void
7309init_cu_die_reader (struct die_reader_specs *reader,
dee91e82 7310 struct dwarf2_cu *cu,
3019eac3
DE
7311 struct dwarf2_section_info *section,
7312 struct dwo_file *dwo_file)
d85a05f0 7313{
fceca515 7314 gdb_assert (section->readin && section->buffer != NULL);
a32a8923 7315 reader->abfd = get_section_bfd_owner (section);
d85a05f0 7316 reader->cu = cu;
3019eac3 7317 reader->dwo_file = dwo_file;
dee91e82
DE
7318 reader->die_section = section;
7319 reader->buffer = section->buffer;
f664829e 7320 reader->buffer_end = section->buffer + section->size;
a2ce51a0 7321 reader->comp_dir = NULL;
d85a05f0
DJ
7322}
7323
b0c7bfa9
DE
7324/* Subroutine of init_cutu_and_read_dies to simplify it.
7325 Read in the rest of a CU/TU top level DIE from DWO_UNIT.
7326 There's just a lot of work to do, and init_cutu_and_read_dies is big enough
7327 already.
7328
7329 STUB_COMP_UNIT_DIE is for the stub DIE, we copy over certain attributes
7330 from it to the DIE in the DWO. If NULL we are skipping the stub.
a2ce51a0
DE
7331 STUB_COMP_DIR is similar to STUB_COMP_UNIT_DIE: When reading a TU directly
7332 from the DWO file, bypassing the stub, it contains the DW_AT_comp_dir
c54a1dd8
DE
7333 attribute of the referencing CU. At most one of STUB_COMP_UNIT_DIE and
7334 STUB_COMP_DIR may be non-NULL.
b0c7bfa9
DE
7335 *RESULT_READER,*RESULT_INFO_PTR,*RESULT_COMP_UNIT_DIE,*RESULT_HAS_CHILDREN
7336 are filled in with the info of the DIE from the DWO file.
7337 ABBREV_TABLE_PROVIDED is non-zero if the caller of init_cutu_and_read_dies
7338 provided an abbrev table to use.
7339 The result is non-zero if a valid (non-dummy) DIE was found. */
7340
7341static int
7342read_cutu_die_from_dwo (struct dwarf2_per_cu_data *this_cu,
7343 struct dwo_unit *dwo_unit,
7344 int abbrev_table_provided,
7345 struct die_info *stub_comp_unit_die,
a2ce51a0 7346 const char *stub_comp_dir,
b0c7bfa9 7347 struct die_reader_specs *result_reader,
d521ce57 7348 const gdb_byte **result_info_ptr,
b0c7bfa9
DE
7349 struct die_info **result_comp_unit_die,
7350 int *result_has_children)
7351{
7352 struct objfile *objfile = dwarf2_per_objfile->objfile;
7353 struct dwarf2_cu *cu = this_cu->cu;
7354 struct dwarf2_section_info *section;
7355 bfd *abfd;
d521ce57 7356 const gdb_byte *begin_info_ptr, *info_ptr;
b0c7bfa9
DE
7357 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
7358 int i,num_extra_attrs;
7359 struct dwarf2_section_info *dwo_abbrev_section;
7360 struct attribute *attr;
7361 struct die_info *comp_unit_die;
7362
b0aeadb3
DE
7363 /* At most one of these may be provided. */
7364 gdb_assert ((stub_comp_unit_die != NULL) + (stub_comp_dir != NULL) <= 1);
a2ce51a0 7365
b0c7bfa9
DE
7366 /* These attributes aren't processed until later:
7367 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
0d60c288
DE
7368 DW_AT_comp_dir is used now, to find the DWO file, but it is also
7369 referenced later. However, these attributes are found in the stub
7370 which we won't have later. In order to not impose this complication
7371 on the rest of the code, we read them here and copy them to the
7372 DWO CU/TU die. */
b0c7bfa9
DE
7373
7374 stmt_list = NULL;
7375 low_pc = NULL;
7376 high_pc = NULL;
7377 ranges = NULL;
7378 comp_dir = NULL;
7379
7380 if (stub_comp_unit_die != NULL)
7381 {
7382 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
7383 DWO file. */
7384 if (! this_cu->is_debug_types)
7385 stmt_list = dwarf2_attr (stub_comp_unit_die, DW_AT_stmt_list, cu);
7386 low_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_low_pc, cu);
7387 high_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_high_pc, cu);
7388 ranges = dwarf2_attr (stub_comp_unit_die, DW_AT_ranges, cu);
7389 comp_dir = dwarf2_attr (stub_comp_unit_die, DW_AT_comp_dir, cu);
7390
7391 /* There should be a DW_AT_addr_base attribute here (if needed).
7392 We need the value before we can process DW_FORM_GNU_addr_index. */
7393 cu->addr_base = 0;
7394 attr = dwarf2_attr (stub_comp_unit_die, DW_AT_GNU_addr_base, cu);
7395 if (attr)
7396 cu->addr_base = DW_UNSND (attr);
7397
7398 /* There should be a DW_AT_ranges_base attribute here (if needed).
7399 We need the value before we can process DW_AT_ranges. */
7400 cu->ranges_base = 0;
7401 attr = dwarf2_attr (stub_comp_unit_die, DW_AT_GNU_ranges_base, cu);
7402 if (attr)
7403 cu->ranges_base = DW_UNSND (attr);
7404 }
a2ce51a0
DE
7405 else if (stub_comp_dir != NULL)
7406 {
7407 /* Reconstruct the comp_dir attribute to simplify the code below. */
8d749320 7408 comp_dir = XOBNEW (&cu->comp_unit_obstack, struct attribute);
a2ce51a0
DE
7409 comp_dir->name = DW_AT_comp_dir;
7410 comp_dir->form = DW_FORM_string;
7411 DW_STRING_IS_CANONICAL (comp_dir) = 0;
7412 DW_STRING (comp_dir) = stub_comp_dir;
7413 }
b0c7bfa9
DE
7414
7415 /* Set up for reading the DWO CU/TU. */
7416 cu->dwo_unit = dwo_unit;
7417 section = dwo_unit->section;
7418 dwarf2_read_section (objfile, section);
a32a8923 7419 abfd = get_section_bfd_owner (section);
9c541725
PA
7420 begin_info_ptr = info_ptr = (section->buffer
7421 + to_underlying (dwo_unit->sect_off));
b0c7bfa9
DE
7422 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
7423 init_cu_die_reader (result_reader, cu, section, dwo_unit->dwo_file);
7424
7425 if (this_cu->is_debug_types)
7426 {
b0c7bfa9
DE
7427 struct signatured_type *sig_type = (struct signatured_type *) this_cu;
7428
43988095 7429 info_ptr = read_and_check_comp_unit_head (&cu->header, section,
b0c7bfa9 7430 dwo_abbrev_section,
43988095 7431 info_ptr, rcuh_kind::TYPE);
a2ce51a0 7432 /* This is not an assert because it can be caused by bad debug info. */
43988095 7433 if (sig_type->signature != cu->header.signature)
a2ce51a0
DE
7434 {
7435 error (_("Dwarf Error: signature mismatch %s vs %s while reading"
7436 " TU at offset 0x%x [in module %s]"),
7437 hex_string (sig_type->signature),
43988095 7438 hex_string (cu->header.signature),
9c541725 7439 to_underlying (dwo_unit->sect_off),
a2ce51a0
DE
7440 bfd_get_filename (abfd));
7441 }
9c541725 7442 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
b0c7bfa9
DE
7443 /* For DWOs coming from DWP files, we don't know the CU length
7444 nor the type's offset in the TU until now. */
7445 dwo_unit->length = get_cu_length (&cu->header);
9c541725 7446 dwo_unit->type_offset_in_tu = cu->header.type_cu_offset_in_tu;
b0c7bfa9
DE
7447
7448 /* Establish the type offset that can be used to lookup the type.
7449 For DWO files, we don't know it until now. */
9c541725
PA
7450 sig_type->type_offset_in_section
7451 = dwo_unit->sect_off + to_underlying (dwo_unit->type_offset_in_tu);
b0c7bfa9
DE
7452 }
7453 else
7454 {
7455 info_ptr = read_and_check_comp_unit_head (&cu->header, section,
7456 dwo_abbrev_section,
43988095 7457 info_ptr, rcuh_kind::COMPILE);
9c541725 7458 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
b0c7bfa9
DE
7459 /* For DWOs coming from DWP files, we don't know the CU length
7460 until now. */
7461 dwo_unit->length = get_cu_length (&cu->header);
7462 }
7463
02142a6c
DE
7464 /* Replace the CU's original abbrev table with the DWO's.
7465 Reminder: We can't read the abbrev table until we've read the header. */
b0c7bfa9
DE
7466 if (abbrev_table_provided)
7467 {
7468 /* Don't free the provided abbrev table, the caller of
7469 init_cutu_and_read_dies owns it. */
7470 dwarf2_read_abbrevs (cu, dwo_abbrev_section);
02142a6c 7471 /* Ensure the DWO abbrev table gets freed. */
b0c7bfa9
DE
7472 make_cleanup (dwarf2_free_abbrev_table, cu);
7473 }
7474 else
7475 {
7476 dwarf2_free_abbrev_table (cu);
7477 dwarf2_read_abbrevs (cu, dwo_abbrev_section);
02142a6c 7478 /* Leave any existing abbrev table cleanup as is. */
b0c7bfa9
DE
7479 }
7480
7481 /* Read in the die, but leave space to copy over the attributes
7482 from the stub. This has the benefit of simplifying the rest of
7483 the code - all the work to maintain the illusion of a single
7484 DW_TAG_{compile,type}_unit DIE is done here. */
7485 num_extra_attrs = ((stmt_list != NULL)
7486 + (low_pc != NULL)
7487 + (high_pc != NULL)
7488 + (ranges != NULL)
7489 + (comp_dir != NULL));
7490 info_ptr = read_full_die_1 (result_reader, result_comp_unit_die, info_ptr,
7491 result_has_children, num_extra_attrs);
7492
7493 /* Copy over the attributes from the stub to the DIE we just read in. */
7494 comp_unit_die = *result_comp_unit_die;
7495 i = comp_unit_die->num_attrs;
7496 if (stmt_list != NULL)
7497 comp_unit_die->attrs[i++] = *stmt_list;
7498 if (low_pc != NULL)
7499 comp_unit_die->attrs[i++] = *low_pc;
7500 if (high_pc != NULL)
7501 comp_unit_die->attrs[i++] = *high_pc;
7502 if (ranges != NULL)
7503 comp_unit_die->attrs[i++] = *ranges;
7504 if (comp_dir != NULL)
7505 comp_unit_die->attrs[i++] = *comp_dir;
7506 comp_unit_die->num_attrs += num_extra_attrs;
7507
b4f54984 7508 if (dwarf_die_debug)
bf6af496
DE
7509 {
7510 fprintf_unfiltered (gdb_stdlog,
7511 "Read die from %s@0x%x of %s:\n",
a32a8923 7512 get_section_name (section),
bf6af496
DE
7513 (unsigned) (begin_info_ptr - section->buffer),
7514 bfd_get_filename (abfd));
b4f54984 7515 dump_die (comp_unit_die, dwarf_die_debug);
bf6af496
DE
7516 }
7517
a2ce51a0
DE
7518 /* Save the comp_dir attribute. If there is no DWP file then we'll read
7519 TUs by skipping the stub and going directly to the entry in the DWO file.
7520 However, skipping the stub means we won't get DW_AT_comp_dir, so we have
7521 to get it via circuitous means. Blech. */
7522 if (comp_dir != NULL)
7523 result_reader->comp_dir = DW_STRING (comp_dir);
7524
b0c7bfa9
DE
7525 /* Skip dummy compilation units. */
7526 if (info_ptr >= begin_info_ptr + dwo_unit->length
7527 || peek_abbrev_code (abfd, info_ptr) == 0)
7528 return 0;
7529
7530 *result_info_ptr = info_ptr;
7531 return 1;
7532}
7533
7534/* Subroutine of init_cutu_and_read_dies to simplify it.
7535 Look up the DWO unit specified by COMP_UNIT_DIE of THIS_CU.
6a506a2d 7536 Returns NULL if the specified DWO unit cannot be found. */
b0c7bfa9
DE
7537
7538static struct dwo_unit *
7539lookup_dwo_unit (struct dwarf2_per_cu_data *this_cu,
7540 struct die_info *comp_unit_die)
7541{
7542 struct dwarf2_cu *cu = this_cu->cu;
b0c7bfa9
DE
7543 ULONGEST signature;
7544 struct dwo_unit *dwo_unit;
7545 const char *comp_dir, *dwo_name;
7546
a2ce51a0
DE
7547 gdb_assert (cu != NULL);
7548
b0c7bfa9 7549 /* Yeah, we look dwo_name up again, but it simplifies the code. */
7d45c7c3
KB
7550 dwo_name = dwarf2_string_attr (comp_unit_die, DW_AT_GNU_dwo_name, cu);
7551 comp_dir = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
b0c7bfa9
DE
7552
7553 if (this_cu->is_debug_types)
7554 {
7555 struct signatured_type *sig_type;
7556
7557 /* Since this_cu is the first member of struct signatured_type,
7558 we can go from a pointer to one to a pointer to the other. */
7559 sig_type = (struct signatured_type *) this_cu;
7560 signature = sig_type->signature;
7561 dwo_unit = lookup_dwo_type_unit (sig_type, dwo_name, comp_dir);
7562 }
7563 else
7564 {
7565 struct attribute *attr;
7566
7567 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
7568 if (! attr)
7569 error (_("Dwarf Error: missing dwo_id for dwo_name %s"
7570 " [in module %s]"),
4262abfb 7571 dwo_name, objfile_name (this_cu->objfile));
b0c7bfa9
DE
7572 signature = DW_UNSND (attr);
7573 dwo_unit = lookup_dwo_comp_unit (this_cu, dwo_name, comp_dir,
7574 signature);
7575 }
7576
b0c7bfa9
DE
7577 return dwo_unit;
7578}
7579
a2ce51a0 7580/* Subroutine of init_cutu_and_read_dies to simplify it.
6aa5f3a6
DE
7581 See it for a description of the parameters.
7582 Read a TU directly from a DWO file, bypassing the stub.
7583
7584 Note: This function could be a little bit simpler if we shared cleanups
7585 with our caller, init_cutu_and_read_dies. That's generally a fragile thing
7586 to do, so we keep this function self-contained. Or we could move this
7587 into our caller, but it's complex enough already. */
a2ce51a0
DE
7588
7589static void
6aa5f3a6
DE
7590init_tu_and_read_dwo_dies (struct dwarf2_per_cu_data *this_cu,
7591 int use_existing_cu, int keep,
a2ce51a0
DE
7592 die_reader_func_ftype *die_reader_func,
7593 void *data)
7594{
7595 struct dwarf2_cu *cu;
7596 struct signatured_type *sig_type;
6aa5f3a6 7597 struct cleanup *cleanups, *free_cu_cleanup = NULL;
a2ce51a0
DE
7598 struct die_reader_specs reader;
7599 const gdb_byte *info_ptr;
7600 struct die_info *comp_unit_die;
7601 int has_children;
7602
7603 /* Verify we can do the following downcast, and that we have the
7604 data we need. */
7605 gdb_assert (this_cu->is_debug_types && this_cu->reading_dwo_directly);
7606 sig_type = (struct signatured_type *) this_cu;
7607 gdb_assert (sig_type->dwo_unit != NULL);
7608
7609 cleanups = make_cleanup (null_cleanup, NULL);
7610
6aa5f3a6
DE
7611 if (use_existing_cu && this_cu->cu != NULL)
7612 {
7613 gdb_assert (this_cu->cu->dwo_unit == sig_type->dwo_unit);
7614 cu = this_cu->cu;
7615 /* There's no need to do the rereading_dwo_cu handling that
7616 init_cutu_and_read_dies does since we don't read the stub. */
7617 }
7618 else
7619 {
7620 /* If !use_existing_cu, this_cu->cu must be NULL. */
7621 gdb_assert (this_cu->cu == NULL);
8d749320 7622 cu = XNEW (struct dwarf2_cu);
6aa5f3a6
DE
7623 init_one_comp_unit (cu, this_cu);
7624 /* If an error occurs while loading, release our storage. */
7625 free_cu_cleanup = make_cleanup (free_heap_comp_unit, cu);
7626 }
7627
7628 /* A future optimization, if needed, would be to use an existing
7629 abbrev table. When reading DWOs with skeletonless TUs, all the TUs
7630 could share abbrev tables. */
a2ce51a0
DE
7631
7632 if (read_cutu_die_from_dwo (this_cu, sig_type->dwo_unit,
7633 0 /* abbrev_table_provided */,
7634 NULL /* stub_comp_unit_die */,
7635 sig_type->dwo_unit->dwo_file->comp_dir,
7636 &reader, &info_ptr,
7637 &comp_unit_die, &has_children) == 0)
7638 {
7639 /* Dummy die. */
7640 do_cleanups (cleanups);
7641 return;
7642 }
7643
7644 /* All the "real" work is done here. */
7645 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
7646
6aa5f3a6 7647 /* This duplicates the code in init_cutu_and_read_dies,
a2ce51a0
DE
7648 but the alternative is making the latter more complex.
7649 This function is only for the special case of using DWO files directly:
7650 no point in overly complicating the general case just to handle this. */
6aa5f3a6 7651 if (free_cu_cleanup != NULL)
a2ce51a0 7652 {
6aa5f3a6
DE
7653 if (keep)
7654 {
7655 /* We've successfully allocated this compilation unit. Let our
7656 caller clean it up when finished with it. */
7657 discard_cleanups (free_cu_cleanup);
a2ce51a0 7658
6aa5f3a6
DE
7659 /* We can only discard free_cu_cleanup and all subsequent cleanups.
7660 So we have to manually free the abbrev table. */
7661 dwarf2_free_abbrev_table (cu);
a2ce51a0 7662
6aa5f3a6
DE
7663 /* Link this CU into read_in_chain. */
7664 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
7665 dwarf2_per_objfile->read_in_chain = this_cu;
7666 }
7667 else
7668 do_cleanups (free_cu_cleanup);
a2ce51a0 7669 }
a2ce51a0
DE
7670
7671 do_cleanups (cleanups);
7672}
7673
fd820528 7674/* Initialize a CU (or TU) and read its DIEs.
3019eac3 7675 If the CU defers to a DWO file, read the DWO file as well.
dee91e82 7676
f4dc4d17
DE
7677 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
7678 Otherwise the table specified in the comp unit header is read in and used.
7679 This is an optimization for when we already have the abbrev table.
7680
dee91e82
DE
7681 If USE_EXISTING_CU is non-zero, and THIS_CU->cu is non-NULL, then use it.
7682 Otherwise, a new CU is allocated with xmalloc.
7683
7684 If KEEP is non-zero, then if we allocated a dwarf2_cu we add it to
7685 read_in_chain. Otherwise the dwarf2_cu data is freed at the end.
7686
7687 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
fd820528 7688 linker) then DIE_READER_FUNC will not get called. */
aaa75496 7689
70221824 7690static void
fd820528 7691init_cutu_and_read_dies (struct dwarf2_per_cu_data *this_cu,
f4dc4d17 7692 struct abbrev_table *abbrev_table,
fd820528
DE
7693 int use_existing_cu, int keep,
7694 die_reader_func_ftype *die_reader_func,
7695 void *data)
c906108c 7696{
dee91e82 7697 struct objfile *objfile = dwarf2_per_objfile->objfile;
8a0459fd 7698 struct dwarf2_section_info *section = this_cu->section;
a32a8923 7699 bfd *abfd = get_section_bfd_owner (section);
dee91e82 7700 struct dwarf2_cu *cu;
d521ce57 7701 const gdb_byte *begin_info_ptr, *info_ptr;
dee91e82 7702 struct die_reader_specs reader;
d85a05f0 7703 struct die_info *comp_unit_die;
dee91e82 7704 int has_children;
d85a05f0 7705 struct attribute *attr;
365156ad 7706 struct cleanup *cleanups, *free_cu_cleanup = NULL;
dee91e82 7707 struct signatured_type *sig_type = NULL;
4bdcc0c1 7708 struct dwarf2_section_info *abbrev_section;
42e7ad6c
DE
7709 /* Non-zero if CU currently points to a DWO file and we need to
7710 reread it. When this happens we need to reread the skeleton die
a2ce51a0 7711 before we can reread the DWO file (this only applies to CUs, not TUs). */
42e7ad6c 7712 int rereading_dwo_cu = 0;
c906108c 7713
b4f54984 7714 if (dwarf_die_debug)
09406207
DE
7715 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset 0x%x\n",
7716 this_cu->is_debug_types ? "type" : "comp",
9c541725 7717 to_underlying (this_cu->sect_off));
09406207 7718
dee91e82
DE
7719 if (use_existing_cu)
7720 gdb_assert (keep);
23745b47 7721
a2ce51a0
DE
7722 /* If we're reading a TU directly from a DWO file, including a virtual DWO
7723 file (instead of going through the stub), short-circuit all of this. */
7724 if (this_cu->reading_dwo_directly)
7725 {
7726 /* Narrow down the scope of possibilities to have to understand. */
7727 gdb_assert (this_cu->is_debug_types);
7728 gdb_assert (abbrev_table == NULL);
6aa5f3a6
DE
7729 init_tu_and_read_dwo_dies (this_cu, use_existing_cu, keep,
7730 die_reader_func, data);
a2ce51a0
DE
7731 return;
7732 }
7733
dee91e82
DE
7734 cleanups = make_cleanup (null_cleanup, NULL);
7735
7736 /* This is cheap if the section is already read in. */
7737 dwarf2_read_section (objfile, section);
7738
9c541725 7739 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
36586728
TT
7740
7741 abbrev_section = get_abbrev_section_for_cu (this_cu);
dee91e82
DE
7742
7743 if (use_existing_cu && this_cu->cu != NULL)
7744 {
7745 cu = this_cu->cu;
42e7ad6c
DE
7746 /* If this CU is from a DWO file we need to start over, we need to
7747 refetch the attributes from the skeleton CU.
7748 This could be optimized by retrieving those attributes from when we
7749 were here the first time: the previous comp_unit_die was stored in
7750 comp_unit_obstack. But there's no data yet that we need this
7751 optimization. */
7752 if (cu->dwo_unit != NULL)
7753 rereading_dwo_cu = 1;
dee91e82
DE
7754 }
7755 else
7756 {
7757 /* If !use_existing_cu, this_cu->cu must be NULL. */
7758 gdb_assert (this_cu->cu == NULL);
8d749320 7759 cu = XNEW (struct dwarf2_cu);
dee91e82 7760 init_one_comp_unit (cu, this_cu);
dee91e82 7761 /* If an error occurs while loading, release our storage. */
365156ad 7762 free_cu_cleanup = make_cleanup (free_heap_comp_unit, cu);
42e7ad6c 7763 }
dee91e82 7764
b0c7bfa9 7765 /* Get the header. */
9c541725 7766 if (to_underlying (cu->header.first_die_cu_offset) != 0 && !rereading_dwo_cu)
42e7ad6c
DE
7767 {
7768 /* We already have the header, there's no need to read it in again. */
9c541725 7769 info_ptr += to_underlying (cu->header.first_die_cu_offset);
42e7ad6c
DE
7770 }
7771 else
7772 {
3019eac3 7773 if (this_cu->is_debug_types)
dee91e82 7774 {
43988095 7775 info_ptr = read_and_check_comp_unit_head (&cu->header, section,
4bdcc0c1 7776 abbrev_section, info_ptr,
43988095 7777 rcuh_kind::TYPE);
dee91e82 7778
42e7ad6c
DE
7779 /* Since per_cu is the first member of struct signatured_type,
7780 we can go from a pointer to one to a pointer to the other. */
7781 sig_type = (struct signatured_type *) this_cu;
43988095 7782 gdb_assert (sig_type->signature == cu->header.signature);
9c541725
PA
7783 gdb_assert (sig_type->type_offset_in_tu
7784 == cu->header.type_cu_offset_in_tu);
7785 gdb_assert (this_cu->sect_off == cu->header.sect_off);
dee91e82 7786
42e7ad6c
DE
7787 /* LENGTH has not been set yet for type units if we're
7788 using .gdb_index. */
1ce1cefd 7789 this_cu->length = get_cu_length (&cu->header);
3019eac3
DE
7790
7791 /* Establish the type offset that can be used to lookup the type. */
9c541725
PA
7792 sig_type->type_offset_in_section =
7793 this_cu->sect_off + to_underlying (sig_type->type_offset_in_tu);
43988095
JK
7794
7795 this_cu->dwarf_version = cu->header.version;
dee91e82
DE
7796 }
7797 else
7798 {
4bdcc0c1
DE
7799 info_ptr = read_and_check_comp_unit_head (&cu->header, section,
7800 abbrev_section,
43988095
JK
7801 info_ptr,
7802 rcuh_kind::COMPILE);
dee91e82 7803
9c541725 7804 gdb_assert (this_cu->sect_off == cu->header.sect_off);
1ce1cefd 7805 gdb_assert (this_cu->length == get_cu_length (&cu->header));
43988095 7806 this_cu->dwarf_version = cu->header.version;
dee91e82
DE
7807 }
7808 }
10b3939b 7809
6caca83c 7810 /* Skip dummy compilation units. */
dee91e82 7811 if (info_ptr >= begin_info_ptr + this_cu->length
6caca83c
CC
7812 || peek_abbrev_code (abfd, info_ptr) == 0)
7813 {
dee91e82 7814 do_cleanups (cleanups);
21b2bd31 7815 return;
6caca83c
CC
7816 }
7817
433df2d4
DE
7818 /* If we don't have them yet, read the abbrevs for this compilation unit.
7819 And if we need to read them now, make sure they're freed when we're
42e7ad6c
DE
7820 done. Note that it's important that if the CU had an abbrev table
7821 on entry we don't free it when we're done: Somewhere up the call stack
7822 it may be in use. */
f4dc4d17
DE
7823 if (abbrev_table != NULL)
7824 {
7825 gdb_assert (cu->abbrev_table == NULL);
9c541725 7826 gdb_assert (cu->header.abbrev_sect_off == abbrev_table->sect_off);
f4dc4d17
DE
7827 cu->abbrev_table = abbrev_table;
7828 }
7829 else if (cu->abbrev_table == NULL)
dee91e82 7830 {
4bdcc0c1 7831 dwarf2_read_abbrevs (cu, abbrev_section);
dee91e82
DE
7832 make_cleanup (dwarf2_free_abbrev_table, cu);
7833 }
42e7ad6c
DE
7834 else if (rereading_dwo_cu)
7835 {
7836 dwarf2_free_abbrev_table (cu);
7837 dwarf2_read_abbrevs (cu, abbrev_section);
7838 }
af703f96 7839
dee91e82 7840 /* Read the top level CU/TU die. */
3019eac3 7841 init_cu_die_reader (&reader, cu, section, NULL);
dee91e82 7842 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
93311388 7843
b0c7bfa9
DE
7844 /* If we are in a DWO stub, process it and then read in the "real" CU/TU
7845 from the DWO file.
7846 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains a
7847 DWO CU, that this test will fail (the attribute will not be present). */
3019eac3
DE
7848 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_name, cu);
7849 if (attr)
7850 {
3019eac3 7851 struct dwo_unit *dwo_unit;
b0c7bfa9 7852 struct die_info *dwo_comp_unit_die;
3019eac3
DE
7853
7854 if (has_children)
6a506a2d
DE
7855 {
7856 complaint (&symfile_complaints,
7857 _("compilation unit with DW_AT_GNU_dwo_name"
7858 " has children (offset 0x%x) [in module %s]"),
9c541725 7859 to_underlying (this_cu->sect_off), bfd_get_filename (abfd));
6a506a2d 7860 }
b0c7bfa9 7861 dwo_unit = lookup_dwo_unit (this_cu, comp_unit_die);
6a506a2d 7862 if (dwo_unit != NULL)
3019eac3 7863 {
6a506a2d
DE
7864 if (read_cutu_die_from_dwo (this_cu, dwo_unit,
7865 abbrev_table != NULL,
a2ce51a0 7866 comp_unit_die, NULL,
6a506a2d
DE
7867 &reader, &info_ptr,
7868 &dwo_comp_unit_die, &has_children) == 0)
7869 {
7870 /* Dummy die. */
7871 do_cleanups (cleanups);
7872 return;
7873 }
7874 comp_unit_die = dwo_comp_unit_die;
7875 }
7876 else
7877 {
7878 /* Yikes, we couldn't find the rest of the DIE, we only have
7879 the stub. A complaint has already been logged. There's
7880 not much more we can do except pass on the stub DIE to
7881 die_reader_func. We don't want to throw an error on bad
7882 debug info. */
3019eac3
DE
7883 }
7884 }
7885
b0c7bfa9 7886 /* All of the above is setup for this call. Yikes. */
dee91e82
DE
7887 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
7888
b0c7bfa9 7889 /* Done, clean up. */
365156ad 7890 if (free_cu_cleanup != NULL)
348e048f 7891 {
365156ad
TT
7892 if (keep)
7893 {
7894 /* We've successfully allocated this compilation unit. Let our
7895 caller clean it up when finished with it. */
7896 discard_cleanups (free_cu_cleanup);
dee91e82 7897
365156ad
TT
7898 /* We can only discard free_cu_cleanup and all subsequent cleanups.
7899 So we have to manually free the abbrev table. */
7900 dwarf2_free_abbrev_table (cu);
dee91e82 7901
365156ad
TT
7902 /* Link this CU into read_in_chain. */
7903 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
7904 dwarf2_per_objfile->read_in_chain = this_cu;
7905 }
7906 else
7907 do_cleanups (free_cu_cleanup);
348e048f 7908 }
365156ad
TT
7909
7910 do_cleanups (cleanups);
dee91e82
DE
7911}
7912
33e80786
DE
7913/* Read CU/TU THIS_CU but do not follow DW_AT_GNU_dwo_name if present.
7914 DWO_FILE, if non-NULL, is the DWO file to read (the caller is assumed
7915 to have already done the lookup to find the DWO file).
dee91e82
DE
7916
7917 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
3019eac3 7918 THIS_CU->is_debug_types, but nothing else.
dee91e82
DE
7919
7920 We fill in THIS_CU->length.
7921
7922 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
7923 linker) then DIE_READER_FUNC will not get called.
7924
7925 THIS_CU->cu is always freed when done.
3019eac3
DE
7926 This is done in order to not leave THIS_CU->cu in a state where we have
7927 to care whether it refers to the "main" CU or the DWO CU. */
dee91e82
DE
7928
7929static void
7930init_cutu_and_read_dies_no_follow (struct dwarf2_per_cu_data *this_cu,
3019eac3 7931 struct dwo_file *dwo_file,
dee91e82
DE
7932 die_reader_func_ftype *die_reader_func,
7933 void *data)
7934{
7935 struct objfile *objfile = dwarf2_per_objfile->objfile;
8a0459fd 7936 struct dwarf2_section_info *section = this_cu->section;
a32a8923 7937 bfd *abfd = get_section_bfd_owner (section);
33e80786 7938 struct dwarf2_section_info *abbrev_section;
dee91e82 7939 struct dwarf2_cu cu;
d521ce57 7940 const gdb_byte *begin_info_ptr, *info_ptr;
dee91e82
DE
7941 struct die_reader_specs reader;
7942 struct cleanup *cleanups;
7943 struct die_info *comp_unit_die;
7944 int has_children;
7945
b4f54984 7946 if (dwarf_die_debug)
09406207
DE
7947 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset 0x%x\n",
7948 this_cu->is_debug_types ? "type" : "comp",
9c541725 7949 to_underlying (this_cu->sect_off));
09406207 7950
dee91e82
DE
7951 gdb_assert (this_cu->cu == NULL);
7952
33e80786
DE
7953 abbrev_section = (dwo_file != NULL
7954 ? &dwo_file->sections.abbrev
7955 : get_abbrev_section_for_cu (this_cu));
7956
dee91e82
DE
7957 /* This is cheap if the section is already read in. */
7958 dwarf2_read_section (objfile, section);
7959
7960 init_one_comp_unit (&cu, this_cu);
7961
7962 cleanups = make_cleanup (free_stack_comp_unit, &cu);
7963
9c541725 7964 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
4bdcc0c1
DE
7965 info_ptr = read_and_check_comp_unit_head (&cu.header, section,
7966 abbrev_section, info_ptr,
43988095
JK
7967 (this_cu->is_debug_types
7968 ? rcuh_kind::TYPE
7969 : rcuh_kind::COMPILE));
dee91e82 7970
1ce1cefd 7971 this_cu->length = get_cu_length (&cu.header);
dee91e82
DE
7972
7973 /* Skip dummy compilation units. */
7974 if (info_ptr >= begin_info_ptr + this_cu->length
7975 || peek_abbrev_code (abfd, info_ptr) == 0)
c906108c 7976 {
dee91e82 7977 do_cleanups (cleanups);
21b2bd31 7978 return;
93311388 7979 }
72bf9492 7980
dee91e82
DE
7981 dwarf2_read_abbrevs (&cu, abbrev_section);
7982 make_cleanup (dwarf2_free_abbrev_table, &cu);
7983
3019eac3 7984 init_cu_die_reader (&reader, &cu, section, dwo_file);
dee91e82
DE
7985 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
7986
7987 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
7988
7989 do_cleanups (cleanups);
7990}
7991
3019eac3
DE
7992/* Read a CU/TU, except that this does not look for DW_AT_GNU_dwo_name and
7993 does not lookup the specified DWO file.
7994 This cannot be used to read DWO files.
dee91e82
DE
7995
7996 THIS_CU->cu is always freed when done.
3019eac3
DE
7997 This is done in order to not leave THIS_CU->cu in a state where we have
7998 to care whether it refers to the "main" CU or the DWO CU.
7999 We can revisit this if the data shows there's a performance issue. */
dee91e82
DE
8000
8001static void
8002init_cutu_and_read_dies_simple (struct dwarf2_per_cu_data *this_cu,
8003 die_reader_func_ftype *die_reader_func,
8004 void *data)
8005{
33e80786 8006 init_cutu_and_read_dies_no_follow (this_cu, NULL, die_reader_func, data);
dee91e82 8007}
0018ea6f
DE
8008\f
8009/* Type Unit Groups.
dee91e82 8010
0018ea6f
DE
8011 Type Unit Groups are a way to collapse the set of all TUs (type units) into
8012 a more manageable set. The grouping is done by DW_AT_stmt_list entry
8013 so that all types coming from the same compilation (.o file) are grouped
8014 together. A future step could be to put the types in the same symtab as
8015 the CU the types ultimately came from. */
ff013f42 8016
f4dc4d17
DE
8017static hashval_t
8018hash_type_unit_group (const void *item)
8019{
9a3c8263
SM
8020 const struct type_unit_group *tu_group
8021 = (const struct type_unit_group *) item;
f4dc4d17 8022
094b34ac 8023 return hash_stmt_list_entry (&tu_group->hash);
f4dc4d17 8024}
348e048f
DE
8025
8026static int
f4dc4d17 8027eq_type_unit_group (const void *item_lhs, const void *item_rhs)
348e048f 8028{
9a3c8263
SM
8029 const struct type_unit_group *lhs = (const struct type_unit_group *) item_lhs;
8030 const struct type_unit_group *rhs = (const struct type_unit_group *) item_rhs;
348e048f 8031
094b34ac 8032 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
f4dc4d17 8033}
348e048f 8034
f4dc4d17
DE
8035/* Allocate a hash table for type unit groups. */
8036
8037static htab_t
8038allocate_type_unit_groups_table (void)
8039{
8040 return htab_create_alloc_ex (3,
8041 hash_type_unit_group,
8042 eq_type_unit_group,
8043 NULL,
8044 &dwarf2_per_objfile->objfile->objfile_obstack,
8045 hashtab_obstack_allocate,
8046 dummy_obstack_deallocate);
8047}
dee91e82 8048
f4dc4d17
DE
8049/* Type units that don't have DW_AT_stmt_list are grouped into their own
8050 partial symtabs. We combine several TUs per psymtab to not let the size
8051 of any one psymtab grow too big. */
8052#define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
8053#define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
dee91e82 8054
094b34ac 8055/* Helper routine for get_type_unit_group.
f4dc4d17
DE
8056 Create the type_unit_group object used to hold one or more TUs. */
8057
8058static struct type_unit_group *
094b34ac 8059create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
f4dc4d17
DE
8060{
8061 struct objfile *objfile = dwarf2_per_objfile->objfile;
094b34ac 8062 struct dwarf2_per_cu_data *per_cu;
f4dc4d17 8063 struct type_unit_group *tu_group;
f4dc4d17
DE
8064
8065 tu_group = OBSTACK_ZALLOC (&objfile->objfile_obstack,
8066 struct type_unit_group);
094b34ac 8067 per_cu = &tu_group->per_cu;
f4dc4d17 8068 per_cu->objfile = objfile;
f4dc4d17 8069
094b34ac
DE
8070 if (dwarf2_per_objfile->using_index)
8071 {
8072 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
8073 struct dwarf2_per_cu_quick_data);
094b34ac
DE
8074 }
8075 else
8076 {
9c541725 8077 unsigned int line_offset = to_underlying (line_offset_struct);
094b34ac
DE
8078 struct partial_symtab *pst;
8079 char *name;
8080
8081 /* Give the symtab a useful name for debug purposes. */
8082 if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
8083 name = xstrprintf ("<type_units_%d>",
8084 (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
8085 else
8086 name = xstrprintf ("<type_units_at_0x%x>", line_offset);
8087
8088 pst = create_partial_symtab (per_cu, name);
8089 pst->anonymous = 1;
f4dc4d17 8090
094b34ac
DE
8091 xfree (name);
8092 }
f4dc4d17 8093
094b34ac 8094 tu_group->hash.dwo_unit = cu->dwo_unit;
9c541725 8095 tu_group->hash.line_sect_off = line_offset_struct;
f4dc4d17
DE
8096
8097 return tu_group;
8098}
8099
094b34ac
DE
8100/* Look up the type_unit_group for type unit CU, and create it if necessary.
8101 STMT_LIST is a DW_AT_stmt_list attribute. */
f4dc4d17
DE
8102
8103static struct type_unit_group *
ff39bb5e 8104get_type_unit_group (struct dwarf2_cu *cu, const struct attribute *stmt_list)
f4dc4d17
DE
8105{
8106 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
8107 struct type_unit_group *tu_group;
8108 void **slot;
8109 unsigned int line_offset;
8110 struct type_unit_group type_unit_group_for_lookup;
8111
8112 if (dwarf2_per_objfile->type_unit_groups == NULL)
8113 {
8114 dwarf2_per_objfile->type_unit_groups =
8115 allocate_type_unit_groups_table ();
8116 }
8117
8118 /* Do we need to create a new group, or can we use an existing one? */
8119
8120 if (stmt_list)
8121 {
8122 line_offset = DW_UNSND (stmt_list);
8123 ++tu_stats->nr_symtab_sharers;
8124 }
8125 else
8126 {
8127 /* Ugh, no stmt_list. Rare, but we have to handle it.
8128 We can do various things here like create one group per TU or
8129 spread them over multiple groups to split up the expansion work.
8130 To avoid worst case scenarios (too many groups or too large groups)
8131 we, umm, group them in bunches. */
8132 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
8133 | (tu_stats->nr_stmt_less_type_units
8134 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
8135 ++tu_stats->nr_stmt_less_type_units;
8136 }
8137
094b34ac 8138 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
9c541725 8139 type_unit_group_for_lookup.hash.line_sect_off = (sect_offset) line_offset;
f4dc4d17
DE
8140 slot = htab_find_slot (dwarf2_per_objfile->type_unit_groups,
8141 &type_unit_group_for_lookup, INSERT);
8142 if (*slot != NULL)
8143 {
9a3c8263 8144 tu_group = (struct type_unit_group *) *slot;
f4dc4d17
DE
8145 gdb_assert (tu_group != NULL);
8146 }
8147 else
8148 {
9c541725 8149 sect_offset line_offset_struct = (sect_offset) line_offset;
094b34ac 8150 tu_group = create_type_unit_group (cu, line_offset_struct);
f4dc4d17
DE
8151 *slot = tu_group;
8152 ++tu_stats->nr_symtabs;
8153 }
8154
8155 return tu_group;
8156}
0018ea6f
DE
8157\f
8158/* Partial symbol tables. */
8159
8160/* Create a psymtab named NAME and assign it to PER_CU.
8161
8162 The caller must fill in the following details:
8163 dirname, textlow, texthigh. */
8164
8165static struct partial_symtab *
8166create_partial_symtab (struct dwarf2_per_cu_data *per_cu, const char *name)
8167{
8168 struct objfile *objfile = per_cu->objfile;
8169 struct partial_symtab *pst;
8170
18a94d75 8171 pst = start_psymtab_common (objfile, name, 0,
af5bf4ad
SM
8172 objfile->global_psymbols,
8173 objfile->static_psymbols);
0018ea6f
DE
8174
8175 pst->psymtabs_addrmap_supported = 1;
8176
8177 /* This is the glue that links PST into GDB's symbol API. */
8178 pst->read_symtab_private = per_cu;
8179 pst->read_symtab = dwarf2_read_symtab;
8180 per_cu->v.psymtab = pst;
8181
8182 return pst;
8183}
8184
b93601f3
TT
8185/* The DATA object passed to process_psymtab_comp_unit_reader has this
8186 type. */
8187
8188struct process_psymtab_comp_unit_data
8189{
8190 /* True if we are reading a DW_TAG_partial_unit. */
8191
8192 int want_partial_unit;
8193
8194 /* The "pretend" language that is used if the CU doesn't declare a
8195 language. */
8196
8197 enum language pretend_language;
8198};
8199
0018ea6f
DE
8200/* die_reader_func for process_psymtab_comp_unit. */
8201
8202static void
8203process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
d521ce57 8204 const gdb_byte *info_ptr,
0018ea6f
DE
8205 struct die_info *comp_unit_die,
8206 int has_children,
8207 void *data)
8208{
8209 struct dwarf2_cu *cu = reader->cu;
8210 struct objfile *objfile = cu->objfile;
3e29f34a 8211 struct gdbarch *gdbarch = get_objfile_arch (objfile);
0018ea6f 8212 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
0018ea6f
DE
8213 CORE_ADDR baseaddr;
8214 CORE_ADDR best_lowpc = 0, best_highpc = 0;
8215 struct partial_symtab *pst;
3a2b436a 8216 enum pc_bounds_kind cu_bounds_kind;
0018ea6f 8217 const char *filename;
9a3c8263
SM
8218 struct process_psymtab_comp_unit_data *info
8219 = (struct process_psymtab_comp_unit_data *) data;
0018ea6f 8220
b93601f3 8221 if (comp_unit_die->tag == DW_TAG_partial_unit && !info->want_partial_unit)
0018ea6f
DE
8222 return;
8223
8224 gdb_assert (! per_cu->is_debug_types);
8225
b93601f3 8226 prepare_one_comp_unit (cu, comp_unit_die, info->pretend_language);
0018ea6f
DE
8227
8228 cu->list_in_scope = &file_symbols;
8229
8230 /* Allocate a new partial symbol table structure. */
7d45c7c3
KB
8231 filename = dwarf2_string_attr (comp_unit_die, DW_AT_name, cu);
8232 if (filename == NULL)
0018ea6f 8233 filename = "";
0018ea6f
DE
8234
8235 pst = create_partial_symtab (per_cu, filename);
8236
8237 /* This must be done before calling dwarf2_build_include_psymtabs. */
7d45c7c3 8238 pst->dirname = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
0018ea6f
DE
8239
8240 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
8241
8242 dwarf2_find_base_address (comp_unit_die, cu);
8243
8244 /* Possibly set the default values of LOWPC and HIGHPC from
8245 `DW_AT_ranges'. */
3a2b436a
JK
8246 cu_bounds_kind = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
8247 &best_highpc, cu, pst);
8248 if (cu_bounds_kind == PC_BOUNDS_HIGH_LOW && best_lowpc < best_highpc)
0018ea6f
DE
8249 /* Store the contiguous range if it is not empty; it can be empty for
8250 CUs with no code. */
8251 addrmap_set_empty (objfile->psymtabs_addrmap,
3e29f34a
MR
8252 gdbarch_adjust_dwarf2_addr (gdbarch,
8253 best_lowpc + baseaddr),
8254 gdbarch_adjust_dwarf2_addr (gdbarch,
8255 best_highpc + baseaddr) - 1,
8256 pst);
0018ea6f
DE
8257
8258 /* Check if comp unit has_children.
8259 If so, read the rest of the partial symbols from this comp unit.
8260 If not, there's no more debug_info for this comp unit. */
8261 if (has_children)
8262 {
8263 struct partial_die_info *first_die;
8264 CORE_ADDR lowpc, highpc;
8265
8266 lowpc = ((CORE_ADDR) -1);
8267 highpc = ((CORE_ADDR) 0);
8268
8269 first_die = load_partial_dies (reader, info_ptr, 1);
8270
8271 scan_partial_symbols (first_die, &lowpc, &highpc,
e385593e 8272 cu_bounds_kind <= PC_BOUNDS_INVALID, cu);
0018ea6f
DE
8273
8274 /* If we didn't find a lowpc, set it to highpc to avoid
8275 complaints from `maint check'. */
8276 if (lowpc == ((CORE_ADDR) -1))
8277 lowpc = highpc;
8278
8279 /* If the compilation unit didn't have an explicit address range,
8280 then use the information extracted from its child dies. */
e385593e 8281 if (cu_bounds_kind <= PC_BOUNDS_INVALID)
0018ea6f
DE
8282 {
8283 best_lowpc = lowpc;
8284 best_highpc = highpc;
8285 }
8286 }
3e29f34a
MR
8287 pst->textlow = gdbarch_adjust_dwarf2_addr (gdbarch, best_lowpc + baseaddr);
8288 pst->texthigh = gdbarch_adjust_dwarf2_addr (gdbarch, best_highpc + baseaddr);
0018ea6f 8289
8763cede 8290 end_psymtab_common (objfile, pst);
0018ea6f
DE
8291
8292 if (!VEC_empty (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs))
8293 {
8294 int i;
8295 int len = VEC_length (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs);
8296 struct dwarf2_per_cu_data *iter;
8297
8298 /* Fill in 'dependencies' here; we fill in 'users' in a
8299 post-pass. */
8300 pst->number_of_dependencies = len;
8d749320
SM
8301 pst->dependencies =
8302 XOBNEWVEC (&objfile->objfile_obstack, struct partial_symtab *, len);
0018ea6f
DE
8303 for (i = 0;
8304 VEC_iterate (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs,
8305 i, iter);
8306 ++i)
8307 pst->dependencies[i] = iter->v.psymtab;
8308
8309 VEC_free (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs);
8310 }
8311
8312 /* Get the list of files included in the current compilation unit,
8313 and build a psymtab for each of them. */
8314 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
8315
b4f54984 8316 if (dwarf_read_debug)
0018ea6f
DE
8317 {
8318 struct gdbarch *gdbarch = get_objfile_arch (objfile);
8319
8320 fprintf_unfiltered (gdb_stdlog,
8321 "Psymtab for %s unit @0x%x: %s - %s"
8322 ", %d global, %d static syms\n",
8323 per_cu->is_debug_types ? "type" : "comp",
9c541725 8324 to_underlying (per_cu->sect_off),
0018ea6f
DE
8325 paddress (gdbarch, pst->textlow),
8326 paddress (gdbarch, pst->texthigh),
8327 pst->n_global_syms, pst->n_static_syms);
8328 }
8329}
8330
8331/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
8332 Process compilation unit THIS_CU for a psymtab. */
8333
8334static void
8335process_psymtab_comp_unit (struct dwarf2_per_cu_data *this_cu,
b93601f3
TT
8336 int want_partial_unit,
8337 enum language pretend_language)
0018ea6f
DE
8338{
8339 /* If this compilation unit was already read in, free the
8340 cached copy in order to read it in again. This is
8341 necessary because we skipped some symbols when we first
8342 read in the compilation unit (see load_partial_dies).
8343 This problem could be avoided, but the benefit is unclear. */
8344 if (this_cu->cu != NULL)
8345 free_one_cached_comp_unit (this_cu);
8346
f1902523
JK
8347 if (this_cu->is_debug_types)
8348 init_cutu_and_read_dies (this_cu, NULL, 0, 0, build_type_psymtabs_reader,
8349 NULL);
8350 else
8351 {
8352 process_psymtab_comp_unit_data info;
8353 info.want_partial_unit = want_partial_unit;
8354 info.pretend_language = pretend_language;
8355 init_cutu_and_read_dies (this_cu, NULL, 0, 0,
8356 process_psymtab_comp_unit_reader, &info);
8357 }
0018ea6f
DE
8358
8359 /* Age out any secondary CUs. */
8360 age_cached_comp_units ();
8361}
f4dc4d17
DE
8362
8363/* Reader function for build_type_psymtabs. */
8364
8365static void
8366build_type_psymtabs_reader (const struct die_reader_specs *reader,
d521ce57 8367 const gdb_byte *info_ptr,
f4dc4d17
DE
8368 struct die_info *type_unit_die,
8369 int has_children,
8370 void *data)
8371{
8372 struct objfile *objfile = dwarf2_per_objfile->objfile;
8373 struct dwarf2_cu *cu = reader->cu;
8374 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
0186c6a7 8375 struct signatured_type *sig_type;
f4dc4d17
DE
8376 struct type_unit_group *tu_group;
8377 struct attribute *attr;
8378 struct partial_die_info *first_die;
8379 CORE_ADDR lowpc, highpc;
8380 struct partial_symtab *pst;
8381
8382 gdb_assert (data == NULL);
0186c6a7
DE
8383 gdb_assert (per_cu->is_debug_types);
8384 sig_type = (struct signatured_type *) per_cu;
f4dc4d17
DE
8385
8386 if (! has_children)
8387 return;
8388
8389 attr = dwarf2_attr_no_follow (type_unit_die, DW_AT_stmt_list);
094b34ac 8390 tu_group = get_type_unit_group (cu, attr);
f4dc4d17 8391
0186c6a7 8392 VEC_safe_push (sig_type_ptr, tu_group->tus, sig_type);
f4dc4d17
DE
8393
8394 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
8395 cu->list_in_scope = &file_symbols;
8396 pst = create_partial_symtab (per_cu, "");
8397 pst->anonymous = 1;
8398
8399 first_die = load_partial_dies (reader, info_ptr, 1);
8400
8401 lowpc = (CORE_ADDR) -1;
8402 highpc = (CORE_ADDR) 0;
8403 scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
8404
8763cede 8405 end_psymtab_common (objfile, pst);
f4dc4d17
DE
8406}
8407
73051182
DE
8408/* Struct used to sort TUs by their abbreviation table offset. */
8409
8410struct tu_abbrev_offset
8411{
8412 struct signatured_type *sig_type;
8413 sect_offset abbrev_offset;
8414};
8415
8416/* Helper routine for build_type_psymtabs_1, passed to qsort. */
8417
8418static int
8419sort_tu_by_abbrev_offset (const void *ap, const void *bp)
8420{
9a3c8263
SM
8421 const struct tu_abbrev_offset * const *a
8422 = (const struct tu_abbrev_offset * const*) ap;
8423 const struct tu_abbrev_offset * const *b
8424 = (const struct tu_abbrev_offset * const*) bp;
9c541725
PA
8425 sect_offset aoff = (*a)->abbrev_offset;
8426 sect_offset boff = (*b)->abbrev_offset;
73051182
DE
8427
8428 return (aoff > boff) - (aoff < boff);
8429}
8430
8431/* Efficiently read all the type units.
8432 This does the bulk of the work for build_type_psymtabs.
8433
8434 The efficiency is because we sort TUs by the abbrev table they use and
8435 only read each abbrev table once. In one program there are 200K TUs
8436 sharing 8K abbrev tables.
8437
8438 The main purpose of this function is to support building the
8439 dwarf2_per_objfile->type_unit_groups table.
8440 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
8441 can collapse the search space by grouping them by stmt_list.
8442 The savings can be significant, in the same program from above the 200K TUs
8443 share 8K stmt_list tables.
8444
8445 FUNC is expected to call get_type_unit_group, which will create the
8446 struct type_unit_group if necessary and add it to
8447 dwarf2_per_objfile->type_unit_groups. */
8448
8449static void
8450build_type_psymtabs_1 (void)
8451{
73051182
DE
8452 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
8453 struct cleanup *cleanups;
8454 struct abbrev_table *abbrev_table;
8455 sect_offset abbrev_offset;
8456 struct tu_abbrev_offset *sorted_by_abbrev;
73051182
DE
8457 int i;
8458
8459 /* It's up to the caller to not call us multiple times. */
8460 gdb_assert (dwarf2_per_objfile->type_unit_groups == NULL);
8461
8462 if (dwarf2_per_objfile->n_type_units == 0)
8463 return;
8464
8465 /* TUs typically share abbrev tables, and there can be way more TUs than
8466 abbrev tables. Sort by abbrev table to reduce the number of times we
8467 read each abbrev table in.
8468 Alternatives are to punt or to maintain a cache of abbrev tables.
8469 This is simpler and efficient enough for now.
8470
8471 Later we group TUs by their DW_AT_stmt_list value (as this defines the
8472 symtab to use). Typically TUs with the same abbrev offset have the same
8473 stmt_list value too so in practice this should work well.
8474
8475 The basic algorithm here is:
8476
8477 sort TUs by abbrev table
8478 for each TU with same abbrev table:
8479 read abbrev table if first user
8480 read TU top level DIE
8481 [IWBN if DWO skeletons had DW_AT_stmt_list]
8482 call FUNC */
8483
b4f54984 8484 if (dwarf_read_debug)
73051182
DE
8485 fprintf_unfiltered (gdb_stdlog, "Building type unit groups ...\n");
8486
8487 /* Sort in a separate table to maintain the order of all_type_units
8488 for .gdb_index: TU indices directly index all_type_units. */
8489 sorted_by_abbrev = XNEWVEC (struct tu_abbrev_offset,
8490 dwarf2_per_objfile->n_type_units);
8491 for (i = 0; i < dwarf2_per_objfile->n_type_units; ++i)
8492 {
8493 struct signatured_type *sig_type = dwarf2_per_objfile->all_type_units[i];
8494
8495 sorted_by_abbrev[i].sig_type = sig_type;
8496 sorted_by_abbrev[i].abbrev_offset =
8497 read_abbrev_offset (sig_type->per_cu.section,
9c541725 8498 sig_type->per_cu.sect_off);
73051182
DE
8499 }
8500 cleanups = make_cleanup (xfree, sorted_by_abbrev);
8501 qsort (sorted_by_abbrev, dwarf2_per_objfile->n_type_units,
8502 sizeof (struct tu_abbrev_offset), sort_tu_by_abbrev_offset);
8503
9c541725 8504 abbrev_offset = (sect_offset) ~(unsigned) 0;
73051182
DE
8505 abbrev_table = NULL;
8506 make_cleanup (abbrev_table_free_cleanup, &abbrev_table);
8507
8508 for (i = 0; i < dwarf2_per_objfile->n_type_units; ++i)
8509 {
8510 const struct tu_abbrev_offset *tu = &sorted_by_abbrev[i];
8511
8512 /* Switch to the next abbrev table if necessary. */
8513 if (abbrev_table == NULL
9c541725 8514 || tu->abbrev_offset != abbrev_offset)
73051182
DE
8515 {
8516 if (abbrev_table != NULL)
8517 {
8518 abbrev_table_free (abbrev_table);
8519 /* Reset to NULL in case abbrev_table_read_table throws
8520 an error: abbrev_table_free_cleanup will get called. */
8521 abbrev_table = NULL;
8522 }
8523 abbrev_offset = tu->abbrev_offset;
8524 abbrev_table =
8525 abbrev_table_read_table (&dwarf2_per_objfile->abbrev,
8526 abbrev_offset);
8527 ++tu_stats->nr_uniq_abbrev_tables;
8528 }
8529
8530 init_cutu_and_read_dies (&tu->sig_type->per_cu, abbrev_table, 0, 0,
8531 build_type_psymtabs_reader, NULL);
8532 }
8533
73051182 8534 do_cleanups (cleanups);
6aa5f3a6 8535}
73051182 8536
6aa5f3a6
DE
8537/* Print collected type unit statistics. */
8538
8539static void
8540print_tu_stats (void)
8541{
8542 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
8543
8544 fprintf_unfiltered (gdb_stdlog, "Type unit statistics:\n");
8545 fprintf_unfiltered (gdb_stdlog, " %d TUs\n",
8546 dwarf2_per_objfile->n_type_units);
8547 fprintf_unfiltered (gdb_stdlog, " %d uniq abbrev tables\n",
8548 tu_stats->nr_uniq_abbrev_tables);
8549 fprintf_unfiltered (gdb_stdlog, " %d symtabs from stmt_list entries\n",
8550 tu_stats->nr_symtabs);
8551 fprintf_unfiltered (gdb_stdlog, " %d symtab sharers\n",
8552 tu_stats->nr_symtab_sharers);
8553 fprintf_unfiltered (gdb_stdlog, " %d type units without a stmt_list\n",
8554 tu_stats->nr_stmt_less_type_units);
8555 fprintf_unfiltered (gdb_stdlog, " %d all_type_units reallocs\n",
8556 tu_stats->nr_all_type_units_reallocs);
73051182
DE
8557}
8558
f4dc4d17
DE
8559/* Traversal function for build_type_psymtabs. */
8560
8561static int
8562build_type_psymtab_dependencies (void **slot, void *info)
8563{
8564 struct objfile *objfile = dwarf2_per_objfile->objfile;
8565 struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
094b34ac 8566 struct dwarf2_per_cu_data *per_cu = &tu_group->per_cu;
f4dc4d17 8567 struct partial_symtab *pst = per_cu->v.psymtab;
0186c6a7
DE
8568 int len = VEC_length (sig_type_ptr, tu_group->tus);
8569 struct signatured_type *iter;
f4dc4d17
DE
8570 int i;
8571
8572 gdb_assert (len > 0);
0186c6a7 8573 gdb_assert (IS_TYPE_UNIT_GROUP (per_cu));
f4dc4d17
DE
8574
8575 pst->number_of_dependencies = len;
8d749320
SM
8576 pst->dependencies =
8577 XOBNEWVEC (&objfile->objfile_obstack, struct partial_symtab *, len);
f4dc4d17 8578 for (i = 0;
0186c6a7 8579 VEC_iterate (sig_type_ptr, tu_group->tus, i, iter);
f4dc4d17
DE
8580 ++i)
8581 {
0186c6a7
DE
8582 gdb_assert (iter->per_cu.is_debug_types);
8583 pst->dependencies[i] = iter->per_cu.v.psymtab;
796a7ff8 8584 iter->type_unit_group = tu_group;
f4dc4d17
DE
8585 }
8586
0186c6a7 8587 VEC_free (sig_type_ptr, tu_group->tus);
348e048f
DE
8588
8589 return 1;
8590}
8591
8592/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
8593 Build partial symbol tables for the .debug_types comp-units. */
8594
8595static void
8596build_type_psymtabs (struct objfile *objfile)
8597{
0e50663e 8598 if (! create_all_type_units (objfile))
348e048f
DE
8599 return;
8600
73051182 8601 build_type_psymtabs_1 ();
6aa5f3a6 8602}
f4dc4d17 8603
6aa5f3a6
DE
8604/* Traversal function for process_skeletonless_type_unit.
8605 Read a TU in a DWO file and build partial symbols for it. */
8606
8607static int
8608process_skeletonless_type_unit (void **slot, void *info)
8609{
8610 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
9a3c8263 8611 struct objfile *objfile = (struct objfile *) info;
6aa5f3a6
DE
8612 struct signatured_type find_entry, *entry;
8613
8614 /* If this TU doesn't exist in the global table, add it and read it in. */
8615
8616 if (dwarf2_per_objfile->signatured_types == NULL)
8617 {
8618 dwarf2_per_objfile->signatured_types
8619 = allocate_signatured_type_table (objfile);
8620 }
8621
8622 find_entry.signature = dwo_unit->signature;
8623 slot = htab_find_slot (dwarf2_per_objfile->signatured_types, &find_entry,
8624 INSERT);
8625 /* If we've already seen this type there's nothing to do. What's happening
8626 is we're doing our own version of comdat-folding here. */
8627 if (*slot != NULL)
8628 return 1;
8629
8630 /* This does the job that create_all_type_units would have done for
8631 this TU. */
8632 entry = add_type_unit (dwo_unit->signature, slot);
8633 fill_in_sig_entry_from_dwo_entry (objfile, entry, dwo_unit);
8634 *slot = entry;
8635
8636 /* This does the job that build_type_psymtabs_1 would have done. */
8637 init_cutu_and_read_dies (&entry->per_cu, NULL, 0, 0,
8638 build_type_psymtabs_reader, NULL);
8639
8640 return 1;
8641}
8642
8643/* Traversal function for process_skeletonless_type_units. */
8644
8645static int
8646process_dwo_file_for_skeletonless_type_units (void **slot, void *info)
8647{
8648 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
8649
8650 if (dwo_file->tus != NULL)
8651 {
8652 htab_traverse_noresize (dwo_file->tus,
8653 process_skeletonless_type_unit, info);
8654 }
8655
8656 return 1;
8657}
8658
8659/* Scan all TUs of DWO files, verifying we've processed them.
8660 This is needed in case a TU was emitted without its skeleton.
8661 Note: This can't be done until we know what all the DWO files are. */
8662
8663static void
8664process_skeletonless_type_units (struct objfile *objfile)
8665{
8666 /* Skeletonless TUs in DWP files without .gdb_index is not supported yet. */
8667 if (get_dwp_file () == NULL
8668 && dwarf2_per_objfile->dwo_files != NULL)
8669 {
8670 htab_traverse_noresize (dwarf2_per_objfile->dwo_files,
8671 process_dwo_file_for_skeletonless_type_units,
8672 objfile);
8673 }
348e048f
DE
8674}
8675
95554aad
TT
8676/* Compute the 'user' field for each psymtab in OBJFILE. */
8677
8678static void
8679set_partial_user (struct objfile *objfile)
8680{
8681 int i;
8682
8683 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
8684 {
8832e7e3 8685 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
95554aad
TT
8686 struct partial_symtab *pst = per_cu->v.psymtab;
8687 int j;
8688
36586728
TT
8689 if (pst == NULL)
8690 continue;
8691
95554aad
TT
8692 for (j = 0; j < pst->number_of_dependencies; ++j)
8693 {
8694 /* Set the 'user' field only if it is not already set. */
8695 if (pst->dependencies[j]->user == NULL)
8696 pst->dependencies[j]->user = pst;
8697 }
8698 }
8699}
8700
93311388
DE
8701/* Build the partial symbol table by doing a quick pass through the
8702 .debug_info and .debug_abbrev sections. */
72bf9492 8703
93311388 8704static void
c67a9c90 8705dwarf2_build_psymtabs_hard (struct objfile *objfile)
93311388 8706{
791afaa2 8707 struct cleanup *back_to;
21b2bd31 8708 int i;
93311388 8709
b4f54984 8710 if (dwarf_read_debug)
45cfd468
DE
8711 {
8712 fprintf_unfiltered (gdb_stdlog, "Building psymtabs of objfile %s ...\n",
4262abfb 8713 objfile_name (objfile));
45cfd468
DE
8714 }
8715
98bfdba5
PA
8716 dwarf2_per_objfile->reading_partial_symbols = 1;
8717
be391dca 8718 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
91c24f0a 8719
93311388
DE
8720 /* Any cached compilation units will be linked by the per-objfile
8721 read_in_chain. Make sure to free them when we're done. */
8722 back_to = make_cleanup (free_cached_comp_units, NULL);
72bf9492 8723
348e048f
DE
8724 build_type_psymtabs (objfile);
8725
93311388 8726 create_all_comp_units (objfile);
c906108c 8727
60606b2c
TT
8728 /* Create a temporary address map on a temporary obstack. We later
8729 copy this to the final obstack. */
8268c778 8730 auto_obstack temp_obstack;
791afaa2
TT
8731
8732 scoped_restore save_psymtabs_addrmap
8733 = make_scoped_restore (&objfile->psymtabs_addrmap,
8734 addrmap_create_mutable (&temp_obstack));
72bf9492 8735
21b2bd31 8736 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
93311388 8737 {
8832e7e3 8738 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
aaa75496 8739
b93601f3 8740 process_psymtab_comp_unit (per_cu, 0, language_minimal);
c906108c 8741 }
ff013f42 8742
6aa5f3a6
DE
8743 /* This has to wait until we read the CUs, we need the list of DWOs. */
8744 process_skeletonless_type_units (objfile);
8745
8746 /* Now that all TUs have been processed we can fill in the dependencies. */
8747 if (dwarf2_per_objfile->type_unit_groups != NULL)
8748 {
8749 htab_traverse_noresize (dwarf2_per_objfile->type_unit_groups,
8750 build_type_psymtab_dependencies, NULL);
8751 }
8752
b4f54984 8753 if (dwarf_read_debug)
6aa5f3a6
DE
8754 print_tu_stats ();
8755
95554aad
TT
8756 set_partial_user (objfile);
8757
ff013f42
JK
8758 objfile->psymtabs_addrmap = addrmap_create_fixed (objfile->psymtabs_addrmap,
8759 &objfile->objfile_obstack);
791afaa2
TT
8760 /* At this point we want to keep the address map. */
8761 save_psymtabs_addrmap.release ();
ff013f42 8762
ae038cb0 8763 do_cleanups (back_to);
45cfd468 8764
b4f54984 8765 if (dwarf_read_debug)
45cfd468 8766 fprintf_unfiltered (gdb_stdlog, "Done building psymtabs of %s\n",
4262abfb 8767 objfile_name (objfile));
ae038cb0
DJ
8768}
8769
3019eac3 8770/* die_reader_func for load_partial_comp_unit. */
ae038cb0
DJ
8771
8772static void
dee91e82 8773load_partial_comp_unit_reader (const struct die_reader_specs *reader,
d521ce57 8774 const gdb_byte *info_ptr,
dee91e82
DE
8775 struct die_info *comp_unit_die,
8776 int has_children,
8777 void *data)
ae038cb0 8778{
dee91e82 8779 struct dwarf2_cu *cu = reader->cu;
ae038cb0 8780
95554aad 8781 prepare_one_comp_unit (cu, comp_unit_die, language_minimal);
ae038cb0 8782
ae038cb0
DJ
8783 /* Check if comp unit has_children.
8784 If so, read the rest of the partial symbols from this comp unit.
0963b4bd 8785 If not, there's no more debug_info for this comp unit. */
d85a05f0 8786 if (has_children)
dee91e82
DE
8787 load_partial_dies (reader, info_ptr, 0);
8788}
98bfdba5 8789
dee91e82
DE
8790/* Load the partial DIEs for a secondary CU into memory.
8791 This is also used when rereading a primary CU with load_all_dies. */
c5b7e1cb 8792
dee91e82
DE
8793static void
8794load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu)
8795{
f4dc4d17
DE
8796 init_cutu_and_read_dies (this_cu, NULL, 1, 1,
8797 load_partial_comp_unit_reader, NULL);
ae038cb0
DJ
8798}
8799
ae038cb0 8800static void
36586728
TT
8801read_comp_units_from_section (struct objfile *objfile,
8802 struct dwarf2_section_info *section,
f1902523 8803 struct dwarf2_section_info *abbrev_section,
36586728
TT
8804 unsigned int is_dwz,
8805 int *n_allocated,
8806 int *n_comp_units,
8807 struct dwarf2_per_cu_data ***all_comp_units)
ae038cb0 8808{
d521ce57 8809 const gdb_byte *info_ptr;
be391dca 8810
b4f54984 8811 if (dwarf_read_debug)
bf6af496 8812 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s\n",
a32a8923
DE
8813 get_section_name (section),
8814 get_section_file_name (section));
bf6af496 8815
36586728 8816 dwarf2_read_section (objfile, section);
ae038cb0 8817
36586728 8818 info_ptr = section->buffer;
6e70227d 8819
36586728 8820 while (info_ptr < section->buffer + section->size)
ae038cb0 8821 {
ae038cb0 8822 struct dwarf2_per_cu_data *this_cu;
ae038cb0 8823
9c541725 8824 sect_offset sect_off = (sect_offset) (info_ptr - section->buffer);
ae038cb0 8825
f1902523
JK
8826 comp_unit_head cu_header;
8827 read_and_check_comp_unit_head (&cu_header, section, abbrev_section,
8828 info_ptr, rcuh_kind::COMPILE);
ae038cb0
DJ
8829
8830 /* Save the compilation unit for later lookup. */
f1902523
JK
8831 if (cu_header.unit_type != DW_UT_type)
8832 {
8833 this_cu = XOBNEW (&objfile->objfile_obstack,
8834 struct dwarf2_per_cu_data);
8835 memset (this_cu, 0, sizeof (*this_cu));
8836 }
8837 else
8838 {
8839 auto sig_type = XOBNEW (&objfile->objfile_obstack,
8840 struct signatured_type);
8841 memset (sig_type, 0, sizeof (*sig_type));
8842 sig_type->signature = cu_header.signature;
8843 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
8844 this_cu = &sig_type->per_cu;
8845 }
8846 this_cu->is_debug_types = (cu_header.unit_type == DW_UT_type);
9c541725 8847 this_cu->sect_off = sect_off;
f1902523 8848 this_cu->length = cu_header.length + cu_header.initial_length_size;
36586728 8849 this_cu->is_dwz = is_dwz;
9291a0cd 8850 this_cu->objfile = objfile;
8a0459fd 8851 this_cu->section = section;
ae038cb0 8852
36586728 8853 if (*n_comp_units == *n_allocated)
ae038cb0 8854 {
36586728 8855 *n_allocated *= 2;
224c3ddb
SM
8856 *all_comp_units = XRESIZEVEC (struct dwarf2_per_cu_data *,
8857 *all_comp_units, *n_allocated);
ae038cb0 8858 }
36586728
TT
8859 (*all_comp_units)[*n_comp_units] = this_cu;
8860 ++*n_comp_units;
ae038cb0
DJ
8861
8862 info_ptr = info_ptr + this_cu->length;
8863 }
36586728
TT
8864}
8865
8866/* Create a list of all compilation units in OBJFILE.
8867 This is only done for -readnow and building partial symtabs. */
8868
8869static void
8870create_all_comp_units (struct objfile *objfile)
8871{
8872 int n_allocated;
8873 int n_comp_units;
8874 struct dwarf2_per_cu_data **all_comp_units;
4db1a1dc 8875 struct dwz_file *dwz;
36586728
TT
8876
8877 n_comp_units = 0;
8878 n_allocated = 10;
8d749320 8879 all_comp_units = XNEWVEC (struct dwarf2_per_cu_data *, n_allocated);
36586728 8880
f1902523
JK
8881 read_comp_units_from_section (objfile, &dwarf2_per_objfile->info,
8882 &dwarf2_per_objfile->abbrev, 0,
36586728
TT
8883 &n_allocated, &n_comp_units, &all_comp_units);
8884
4db1a1dc
TT
8885 dwz = dwarf2_get_dwz_file ();
8886 if (dwz != NULL)
f1902523 8887 read_comp_units_from_section (objfile, &dwz->info, &dwz->abbrev, 1,
4db1a1dc
TT
8888 &n_allocated, &n_comp_units,
8889 &all_comp_units);
ae038cb0 8890
8d749320
SM
8891 dwarf2_per_objfile->all_comp_units = XOBNEWVEC (&objfile->objfile_obstack,
8892 struct dwarf2_per_cu_data *,
8893 n_comp_units);
ae038cb0
DJ
8894 memcpy (dwarf2_per_objfile->all_comp_units, all_comp_units,
8895 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
8896 xfree (all_comp_units);
8897 dwarf2_per_objfile->n_comp_units = n_comp_units;
c906108c
SS
8898}
8899
5734ee8b 8900/* Process all loaded DIEs for compilation unit CU, starting at
cdc07690 8901 FIRST_DIE. The caller should pass SET_ADDRMAP == 1 if the compilation
5734ee8b 8902 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
cdc07690
YQ
8903 DW_AT_ranges). See the comments of add_partial_subprogram on how
8904 SET_ADDRMAP is used and how *LOWPC and *HIGHPC are updated. */
c906108c 8905
72bf9492
DJ
8906static void
8907scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
cdc07690
YQ
8908 CORE_ADDR *highpc, int set_addrmap,
8909 struct dwarf2_cu *cu)
c906108c 8910{
72bf9492 8911 struct partial_die_info *pdi;
c906108c 8912
91c24f0a
DC
8913 /* Now, march along the PDI's, descending into ones which have
8914 interesting children but skipping the children of the other ones,
8915 until we reach the end of the compilation unit. */
c906108c 8916
72bf9492 8917 pdi = first_die;
91c24f0a 8918
72bf9492
DJ
8919 while (pdi != NULL)
8920 {
8921 fixup_partial_die (pdi, cu);
c906108c 8922
f55ee35c 8923 /* Anonymous namespaces or modules have no name but have interesting
91c24f0a
DC
8924 children, so we need to look at them. Ditto for anonymous
8925 enums. */
933c6fe4 8926
72bf9492 8927 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
95554aad
TT
8928 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
8929 || pdi->tag == DW_TAG_imported_unit)
c906108c 8930 {
72bf9492 8931 switch (pdi->tag)
c906108c
SS
8932 {
8933 case DW_TAG_subprogram:
cdc07690 8934 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
c906108c 8935 break;
72929c62 8936 case DW_TAG_constant:
c906108c
SS
8937 case DW_TAG_variable:
8938 case DW_TAG_typedef:
91c24f0a 8939 case DW_TAG_union_type:
72bf9492 8940 if (!pdi->is_declaration)
63d06c5c 8941 {
72bf9492 8942 add_partial_symbol (pdi, cu);
63d06c5c
DC
8943 }
8944 break;
c906108c 8945 case DW_TAG_class_type:
680b30c7 8946 case DW_TAG_interface_type:
c906108c 8947 case DW_TAG_structure_type:
72bf9492 8948 if (!pdi->is_declaration)
c906108c 8949 {
72bf9492 8950 add_partial_symbol (pdi, cu);
c906108c 8951 }
e98c9e7c
TT
8952 if (cu->language == language_rust && pdi->has_children)
8953 scan_partial_symbols (pdi->die_child, lowpc, highpc,
8954 set_addrmap, cu);
c906108c 8955 break;
91c24f0a 8956 case DW_TAG_enumeration_type:
72bf9492
DJ
8957 if (!pdi->is_declaration)
8958 add_partial_enumeration (pdi, cu);
c906108c
SS
8959 break;
8960 case DW_TAG_base_type:
a02abb62 8961 case DW_TAG_subrange_type:
c906108c 8962 /* File scope base type definitions are added to the partial
c5aa993b 8963 symbol table. */
72bf9492 8964 add_partial_symbol (pdi, cu);
c906108c 8965 break;
d9fa45fe 8966 case DW_TAG_namespace:
cdc07690 8967 add_partial_namespace (pdi, lowpc, highpc, set_addrmap, cu);
91c24f0a 8968 break;
5d7cb8df 8969 case DW_TAG_module:
cdc07690 8970 add_partial_module (pdi, lowpc, highpc, set_addrmap, cu);
5d7cb8df 8971 break;
95554aad
TT
8972 case DW_TAG_imported_unit:
8973 {
8974 struct dwarf2_per_cu_data *per_cu;
8975
f4dc4d17
DE
8976 /* For now we don't handle imported units in type units. */
8977 if (cu->per_cu->is_debug_types)
8978 {
8979 error (_("Dwarf Error: DW_TAG_imported_unit is not"
8980 " supported in type units [in module %s]"),
4262abfb 8981 objfile_name (cu->objfile));
f4dc4d17
DE
8982 }
8983
9c541725 8984 per_cu = dwarf2_find_containing_comp_unit (pdi->d.sect_off,
36586728 8985 pdi->is_dwz,
95554aad
TT
8986 cu->objfile);
8987
8988 /* Go read the partial unit, if needed. */
8989 if (per_cu->v.psymtab == NULL)
b93601f3 8990 process_psymtab_comp_unit (per_cu, 1, cu->language);
95554aad 8991
f4dc4d17 8992 VEC_safe_push (dwarf2_per_cu_ptr,
796a7ff8 8993 cu->per_cu->imported_symtabs, per_cu);
95554aad
TT
8994 }
8995 break;
74921315
KS
8996 case DW_TAG_imported_declaration:
8997 add_partial_symbol (pdi, cu);
8998 break;
c906108c
SS
8999 default:
9000 break;
9001 }
9002 }
9003
72bf9492
DJ
9004 /* If the die has a sibling, skip to the sibling. */
9005
9006 pdi = pdi->die_sibling;
9007 }
9008}
9009
9010/* Functions used to compute the fully scoped name of a partial DIE.
91c24f0a 9011
72bf9492 9012 Normally, this is simple. For C++, the parent DIE's fully scoped
9c37b5ae 9013 name is concatenated with "::" and the partial DIE's name.
72bf9492
DJ
9014 Enumerators are an exception; they use the scope of their parent
9015 enumeration type, i.e. the name of the enumeration type is not
9016 prepended to the enumerator.
91c24f0a 9017
72bf9492
DJ
9018 There are two complexities. One is DW_AT_specification; in this
9019 case "parent" means the parent of the target of the specification,
9020 instead of the direct parent of the DIE. The other is compilers
9021 which do not emit DW_TAG_namespace; in this case we try to guess
9022 the fully qualified name of structure types from their members'
9023 linkage names. This must be done using the DIE's children rather
9024 than the children of any DW_AT_specification target. We only need
9025 to do this for structures at the top level, i.e. if the target of
9026 any DW_AT_specification (if any; otherwise the DIE itself) does not
9027 have a parent. */
9028
9029/* Compute the scope prefix associated with PDI's parent, in
9030 compilation unit CU. The result will be allocated on CU's
9031 comp_unit_obstack, or a copy of the already allocated PDI->NAME
9032 field. NULL is returned if no prefix is necessary. */
15d034d0 9033static const char *
72bf9492
DJ
9034partial_die_parent_scope (struct partial_die_info *pdi,
9035 struct dwarf2_cu *cu)
9036{
15d034d0 9037 const char *grandparent_scope;
72bf9492 9038 struct partial_die_info *parent, *real_pdi;
91c24f0a 9039
72bf9492
DJ
9040 /* We need to look at our parent DIE; if we have a DW_AT_specification,
9041 then this means the parent of the specification DIE. */
9042
9043 real_pdi = pdi;
72bf9492 9044 while (real_pdi->has_specification)
36586728
TT
9045 real_pdi = find_partial_die (real_pdi->spec_offset,
9046 real_pdi->spec_is_dwz, cu);
72bf9492
DJ
9047
9048 parent = real_pdi->die_parent;
9049 if (parent == NULL)
9050 return NULL;
9051
9052 if (parent->scope_set)
9053 return parent->scope;
9054
9055 fixup_partial_die (parent, cu);
9056
10b3939b 9057 grandparent_scope = partial_die_parent_scope (parent, cu);
72bf9492 9058
acebe513
UW
9059 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
9060 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
9061 Work around this problem here. */
9062 if (cu->language == language_cplus
6e70227d 9063 && parent->tag == DW_TAG_namespace
acebe513
UW
9064 && strcmp (parent->name, "::") == 0
9065 && grandparent_scope == NULL)
9066 {
9067 parent->scope = NULL;
9068 parent->scope_set = 1;
9069 return NULL;
9070 }
9071
9c6c53f7
SA
9072 if (pdi->tag == DW_TAG_enumerator)
9073 /* Enumerators should not get the name of the enumeration as a prefix. */
9074 parent->scope = grandparent_scope;
9075 else if (parent->tag == DW_TAG_namespace
f55ee35c 9076 || parent->tag == DW_TAG_module
72bf9492
DJ
9077 || parent->tag == DW_TAG_structure_type
9078 || parent->tag == DW_TAG_class_type
680b30c7 9079 || parent->tag == DW_TAG_interface_type
ceeb3d5a
TT
9080 || parent->tag == DW_TAG_union_type
9081 || parent->tag == DW_TAG_enumeration_type)
72bf9492
DJ
9082 {
9083 if (grandparent_scope == NULL)
9084 parent->scope = parent->name;
9085 else
3e43a32a
MS
9086 parent->scope = typename_concat (&cu->comp_unit_obstack,
9087 grandparent_scope,
f55ee35c 9088 parent->name, 0, cu);
72bf9492 9089 }
72bf9492
DJ
9090 else
9091 {
9092 /* FIXME drow/2004-04-01: What should we be doing with
9093 function-local names? For partial symbols, we should probably be
9094 ignoring them. */
9095 complaint (&symfile_complaints,
e2e0b3e5 9096 _("unhandled containing DIE tag %d for DIE at %d"),
9c541725 9097 parent->tag, to_underlying (pdi->sect_off));
72bf9492 9098 parent->scope = grandparent_scope;
c906108c
SS
9099 }
9100
72bf9492
DJ
9101 parent->scope_set = 1;
9102 return parent->scope;
9103}
9104
9105/* Return the fully scoped name associated with PDI, from compilation unit
9106 CU. The result will be allocated with malloc. */
4568ecf9 9107
72bf9492
DJ
9108static char *
9109partial_die_full_name (struct partial_die_info *pdi,
9110 struct dwarf2_cu *cu)
9111{
15d034d0 9112 const char *parent_scope;
72bf9492 9113
98bfdba5
PA
9114 /* If this is a template instantiation, we can not work out the
9115 template arguments from partial DIEs. So, unfortunately, we have
9116 to go through the full DIEs. At least any work we do building
9117 types here will be reused if full symbols are loaded later. */
9118 if (pdi->has_template_arguments)
9119 {
9120 fixup_partial_die (pdi, cu);
9121
9122 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
9123 {
9124 struct die_info *die;
9125 struct attribute attr;
9126 struct dwarf2_cu *ref_cu = cu;
9127
b64f50a1 9128 /* DW_FORM_ref_addr is using section offset. */
b4069958 9129 attr.name = (enum dwarf_attribute) 0;
98bfdba5 9130 attr.form = DW_FORM_ref_addr;
9c541725 9131 attr.u.unsnd = to_underlying (pdi->sect_off);
98bfdba5
PA
9132 die = follow_die_ref (NULL, &attr, &ref_cu);
9133
9134 return xstrdup (dwarf2_full_name (NULL, die, ref_cu));
9135 }
9136 }
9137
72bf9492
DJ
9138 parent_scope = partial_die_parent_scope (pdi, cu);
9139 if (parent_scope == NULL)
9140 return NULL;
9141 else
f55ee35c 9142 return typename_concat (NULL, parent_scope, pdi->name, 0, cu);
c906108c
SS
9143}
9144
9145static void
72bf9492 9146add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
c906108c 9147{
e7c27a73 9148 struct objfile *objfile = cu->objfile;
3e29f34a 9149 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c 9150 CORE_ADDR addr = 0;
15d034d0 9151 const char *actual_name = NULL;
e142c38c 9152 CORE_ADDR baseaddr;
15d034d0 9153 char *built_actual_name;
e142c38c
DJ
9154
9155 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 9156
15d034d0
TT
9157 built_actual_name = partial_die_full_name (pdi, cu);
9158 if (built_actual_name != NULL)
9159 actual_name = built_actual_name;
63d06c5c 9160
72bf9492
DJ
9161 if (actual_name == NULL)
9162 actual_name = pdi->name;
9163
c906108c
SS
9164 switch (pdi->tag)
9165 {
9166 case DW_TAG_subprogram:
3e29f34a 9167 addr = gdbarch_adjust_dwarf2_addr (gdbarch, pdi->lowpc + baseaddr);
2cfa0c8d 9168 if (pdi->is_external || cu->language == language_ada)
c906108c 9169 {
2cfa0c8d
JB
9170 /* brobecker/2007-12-26: Normally, only "external" DIEs are part
9171 of the global scope. But in Ada, we want to be able to access
9172 nested procedures globally. So all Ada subprograms are stored
9173 in the global scope. */
f47fb265 9174 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 9175 built_actual_name != NULL,
f47fb265
MS
9176 VAR_DOMAIN, LOC_BLOCK,
9177 &objfile->global_psymbols,
1762568f 9178 addr, cu->language, objfile);
c906108c
SS
9179 }
9180 else
9181 {
f47fb265 9182 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 9183 built_actual_name != NULL,
f47fb265
MS
9184 VAR_DOMAIN, LOC_BLOCK,
9185 &objfile->static_psymbols,
1762568f 9186 addr, cu->language, objfile);
c906108c 9187 }
0c1b455e
TT
9188
9189 if (pdi->main_subprogram && actual_name != NULL)
9190 set_objfile_main_name (objfile, actual_name, cu->language);
c906108c 9191 break;
72929c62
JB
9192 case DW_TAG_constant:
9193 {
af5bf4ad 9194 std::vector<partial_symbol *> *list;
72929c62
JB
9195
9196 if (pdi->is_external)
9197 list = &objfile->global_psymbols;
9198 else
9199 list = &objfile->static_psymbols;
f47fb265 9200 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 9201 built_actual_name != NULL, VAR_DOMAIN, LOC_STATIC,
1762568f 9202 list, 0, cu->language, objfile);
72929c62
JB
9203 }
9204 break;
c906108c 9205 case DW_TAG_variable:
95554aad
TT
9206 if (pdi->d.locdesc)
9207 addr = decode_locdesc (pdi->d.locdesc, cu);
caac4577 9208
95554aad 9209 if (pdi->d.locdesc
caac4577
JG
9210 && addr == 0
9211 && !dwarf2_per_objfile->has_section_at_zero)
9212 {
9213 /* A global or static variable may also have been stripped
9214 out by the linker if unused, in which case its address
9215 will be nullified; do not add such variables into partial
9216 symbol table then. */
9217 }
9218 else if (pdi->is_external)
c906108c
SS
9219 {
9220 /* Global Variable.
9221 Don't enter into the minimal symbol tables as there is
9222 a minimal symbol table entry from the ELF symbols already.
9223 Enter into partial symbol table if it has a location
9224 descriptor or a type.
9225 If the location descriptor is missing, new_symbol will create
9226 a LOC_UNRESOLVED symbol, the address of the variable will then
9227 be determined from the minimal symbol table whenever the variable
9228 is referenced.
9229 The address for the partial symbol table entry is not
9230 used by GDB, but it comes in handy for debugging partial symbol
9231 table building. */
9232
95554aad 9233 if (pdi->d.locdesc || pdi->has_type)
f47fb265 9234 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 9235 built_actual_name != NULL,
f47fb265
MS
9236 VAR_DOMAIN, LOC_STATIC,
9237 &objfile->global_psymbols,
1762568f 9238 addr + baseaddr,
f47fb265 9239 cu->language, objfile);
c906108c
SS
9240 }
9241 else
9242 {
ff908ebf
AW
9243 int has_loc = pdi->d.locdesc != NULL;
9244
9245 /* Static Variable. Skip symbols whose value we cannot know (those
9246 without location descriptors or constant values). */
9247 if (!has_loc && !pdi->has_const_value)
decbce07 9248 {
15d034d0 9249 xfree (built_actual_name);
decbce07
MS
9250 return;
9251 }
ff908ebf 9252
f47fb265 9253 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 9254 built_actual_name != NULL,
f47fb265
MS
9255 VAR_DOMAIN, LOC_STATIC,
9256 &objfile->static_psymbols,
ff908ebf 9257 has_loc ? addr + baseaddr : (CORE_ADDR) 0,
f47fb265 9258 cu->language, objfile);
c906108c
SS
9259 }
9260 break;
9261 case DW_TAG_typedef:
9262 case DW_TAG_base_type:
a02abb62 9263 case DW_TAG_subrange_type:
38d518c9 9264 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 9265 built_actual_name != NULL,
176620f1 9266 VAR_DOMAIN, LOC_TYPEDEF,
c906108c 9267 &objfile->static_psymbols,
1762568f 9268 0, cu->language, objfile);
c906108c 9269 break;
74921315 9270 case DW_TAG_imported_declaration:
72bf9492
DJ
9271 case DW_TAG_namespace:
9272 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 9273 built_actual_name != NULL,
72bf9492
DJ
9274 VAR_DOMAIN, LOC_TYPEDEF,
9275 &objfile->global_psymbols,
1762568f 9276 0, cu->language, objfile);
72bf9492 9277 break;
530e8392
KB
9278 case DW_TAG_module:
9279 add_psymbol_to_list (actual_name, strlen (actual_name),
9280 built_actual_name != NULL,
9281 MODULE_DOMAIN, LOC_TYPEDEF,
9282 &objfile->global_psymbols,
1762568f 9283 0, cu->language, objfile);
530e8392 9284 break;
c906108c 9285 case DW_TAG_class_type:
680b30c7 9286 case DW_TAG_interface_type:
c906108c
SS
9287 case DW_TAG_structure_type:
9288 case DW_TAG_union_type:
9289 case DW_TAG_enumeration_type:
fa4028e9
JB
9290 /* Skip external references. The DWARF standard says in the section
9291 about "Structure, Union, and Class Type Entries": "An incomplete
9292 structure, union or class type is represented by a structure,
9293 union or class entry that does not have a byte size attribute
9294 and that has a DW_AT_declaration attribute." */
9295 if (!pdi->has_byte_size && pdi->is_declaration)
decbce07 9296 {
15d034d0 9297 xfree (built_actual_name);
decbce07
MS
9298 return;
9299 }
fa4028e9 9300
63d06c5c
DC
9301 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
9302 static vs. global. */
38d518c9 9303 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 9304 built_actual_name != NULL,
176620f1 9305 STRUCT_DOMAIN, LOC_TYPEDEF,
9c37b5ae 9306 cu->language == language_cplus
63d06c5c
DC
9307 ? &objfile->global_psymbols
9308 : &objfile->static_psymbols,
1762568f 9309 0, cu->language, objfile);
c906108c 9310
c906108c
SS
9311 break;
9312 case DW_TAG_enumerator:
38d518c9 9313 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 9314 built_actual_name != NULL,
176620f1 9315 VAR_DOMAIN, LOC_CONST,
9c37b5ae 9316 cu->language == language_cplus
f6fe98ef
DJ
9317 ? &objfile->global_psymbols
9318 : &objfile->static_psymbols,
1762568f 9319 0, cu->language, objfile);
c906108c
SS
9320 break;
9321 default:
9322 break;
9323 }
5c4e30ca 9324
15d034d0 9325 xfree (built_actual_name);
c906108c
SS
9326}
9327
5c4e30ca
DC
9328/* Read a partial die corresponding to a namespace; also, add a symbol
9329 corresponding to that namespace to the symbol table. NAMESPACE is
9330 the name of the enclosing namespace. */
91c24f0a 9331
72bf9492
DJ
9332static void
9333add_partial_namespace (struct partial_die_info *pdi,
91c24f0a 9334 CORE_ADDR *lowpc, CORE_ADDR *highpc,
cdc07690 9335 int set_addrmap, struct dwarf2_cu *cu)
91c24f0a 9336{
72bf9492 9337 /* Add a symbol for the namespace. */
e7c27a73 9338
72bf9492 9339 add_partial_symbol (pdi, cu);
5c4e30ca
DC
9340
9341 /* Now scan partial symbols in that namespace. */
9342
91c24f0a 9343 if (pdi->has_children)
cdc07690 9344 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
91c24f0a
DC
9345}
9346
5d7cb8df
JK
9347/* Read a partial die corresponding to a Fortran module. */
9348
9349static void
9350add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
cdc07690 9351 CORE_ADDR *highpc, int set_addrmap, struct dwarf2_cu *cu)
5d7cb8df 9352{
530e8392
KB
9353 /* Add a symbol for the namespace. */
9354
9355 add_partial_symbol (pdi, cu);
9356
f55ee35c 9357 /* Now scan partial symbols in that module. */
5d7cb8df
JK
9358
9359 if (pdi->has_children)
cdc07690 9360 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
5d7cb8df
JK
9361}
9362
bc30ff58
JB
9363/* Read a partial die corresponding to a subprogram and create a partial
9364 symbol for that subprogram. When the CU language allows it, this
9365 routine also defines a partial symbol for each nested subprogram
cdc07690 9366 that this subprogram contains. If SET_ADDRMAP is true, record the
428fc5fc
YQ
9367 covered ranges in the addrmap. Set *LOWPC and *HIGHPC to the lowest
9368 and highest PC values found in PDI.
6e70227d 9369
cdc07690
YQ
9370 PDI may also be a lexical block, in which case we simply search
9371 recursively for subprograms defined inside that lexical block.
bc30ff58
JB
9372 Again, this is only performed when the CU language allows this
9373 type of definitions. */
9374
9375static void
9376add_partial_subprogram (struct partial_die_info *pdi,
9377 CORE_ADDR *lowpc, CORE_ADDR *highpc,
cdc07690 9378 int set_addrmap, struct dwarf2_cu *cu)
bc30ff58
JB
9379{
9380 if (pdi->tag == DW_TAG_subprogram)
9381 {
9382 if (pdi->has_pc_info)
9383 {
9384 if (pdi->lowpc < *lowpc)
9385 *lowpc = pdi->lowpc;
9386 if (pdi->highpc > *highpc)
9387 *highpc = pdi->highpc;
cdc07690 9388 if (set_addrmap)
5734ee8b 9389 {
5734ee8b 9390 struct objfile *objfile = cu->objfile;
3e29f34a
MR
9391 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9392 CORE_ADDR baseaddr;
9393 CORE_ADDR highpc;
9394 CORE_ADDR lowpc;
5734ee8b
DJ
9395
9396 baseaddr = ANOFFSET (objfile->section_offsets,
9397 SECT_OFF_TEXT (objfile));
3e29f34a
MR
9398 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch,
9399 pdi->lowpc + baseaddr);
9400 highpc = gdbarch_adjust_dwarf2_addr (gdbarch,
9401 pdi->highpc + baseaddr);
9402 addrmap_set_empty (objfile->psymtabs_addrmap, lowpc, highpc - 1,
9291a0cd 9403 cu->per_cu->v.psymtab);
5734ee8b 9404 }
481860b3
GB
9405 }
9406
9407 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
9408 {
bc30ff58 9409 if (!pdi->is_declaration)
e8d05480
JB
9410 /* Ignore subprogram DIEs that do not have a name, they are
9411 illegal. Do not emit a complaint at this point, we will
9412 do so when we convert this psymtab into a symtab. */
9413 if (pdi->name)
9414 add_partial_symbol (pdi, cu);
bc30ff58
JB
9415 }
9416 }
6e70227d 9417
bc30ff58
JB
9418 if (! pdi->has_children)
9419 return;
9420
9421 if (cu->language == language_ada)
9422 {
9423 pdi = pdi->die_child;
9424 while (pdi != NULL)
9425 {
9426 fixup_partial_die (pdi, cu);
9427 if (pdi->tag == DW_TAG_subprogram
9428 || pdi->tag == DW_TAG_lexical_block)
cdc07690 9429 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
bc30ff58
JB
9430 pdi = pdi->die_sibling;
9431 }
9432 }
9433}
9434
91c24f0a
DC
9435/* Read a partial die corresponding to an enumeration type. */
9436
72bf9492
DJ
9437static void
9438add_partial_enumeration (struct partial_die_info *enum_pdi,
9439 struct dwarf2_cu *cu)
91c24f0a 9440{
72bf9492 9441 struct partial_die_info *pdi;
91c24f0a
DC
9442
9443 if (enum_pdi->name != NULL)
72bf9492
DJ
9444 add_partial_symbol (enum_pdi, cu);
9445
9446 pdi = enum_pdi->die_child;
9447 while (pdi)
91c24f0a 9448 {
72bf9492 9449 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
e2e0b3e5 9450 complaint (&symfile_complaints, _("malformed enumerator DIE ignored"));
91c24f0a 9451 else
72bf9492
DJ
9452 add_partial_symbol (pdi, cu);
9453 pdi = pdi->die_sibling;
91c24f0a 9454 }
91c24f0a
DC
9455}
9456
6caca83c
CC
9457/* Return the initial uleb128 in the die at INFO_PTR. */
9458
9459static unsigned int
d521ce57 9460peek_abbrev_code (bfd *abfd, const gdb_byte *info_ptr)
6caca83c
CC
9461{
9462 unsigned int bytes_read;
9463
9464 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9465}
9466
4bb7a0a7
DJ
9467/* Read the initial uleb128 in the die at INFO_PTR in compilation unit CU.
9468 Return the corresponding abbrev, or NULL if the number is zero (indicating
9469 an empty DIE). In either case *BYTES_READ will be set to the length of
9470 the initial number. */
9471
9472static struct abbrev_info *
d521ce57 9473peek_die_abbrev (const gdb_byte *info_ptr, unsigned int *bytes_read,
891d2f0b 9474 struct dwarf2_cu *cu)
4bb7a0a7
DJ
9475{
9476 bfd *abfd = cu->objfile->obfd;
9477 unsigned int abbrev_number;
9478 struct abbrev_info *abbrev;
9479
9480 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
9481
9482 if (abbrev_number == 0)
9483 return NULL;
9484
433df2d4 9485 abbrev = abbrev_table_lookup_abbrev (cu->abbrev_table, abbrev_number);
4bb7a0a7
DJ
9486 if (!abbrev)
9487 {
422b9917
DE
9488 error (_("Dwarf Error: Could not find abbrev number %d in %s"
9489 " at offset 0x%x [in module %s]"),
9490 abbrev_number, cu->per_cu->is_debug_types ? "TU" : "CU",
9c541725 9491 to_underlying (cu->header.sect_off), bfd_get_filename (abfd));
4bb7a0a7
DJ
9492 }
9493
9494 return abbrev;
9495}
9496
93311388
DE
9497/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
9498 Returns a pointer to the end of a series of DIEs, terminated by an empty
4bb7a0a7
DJ
9499 DIE. Any children of the skipped DIEs will also be skipped. */
9500
d521ce57
TT
9501static const gdb_byte *
9502skip_children (const struct die_reader_specs *reader, const gdb_byte *info_ptr)
4bb7a0a7 9503{
dee91e82 9504 struct dwarf2_cu *cu = reader->cu;
4bb7a0a7
DJ
9505 struct abbrev_info *abbrev;
9506 unsigned int bytes_read;
9507
9508 while (1)
9509 {
9510 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
9511 if (abbrev == NULL)
9512 return info_ptr + bytes_read;
9513 else
dee91e82 9514 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
4bb7a0a7
DJ
9515 }
9516}
9517
93311388
DE
9518/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
9519 INFO_PTR should point just after the initial uleb128 of a DIE, and the
4bb7a0a7
DJ
9520 abbrev corresponding to that skipped uleb128 should be passed in
9521 ABBREV. Returns a pointer to this DIE's sibling, skipping any
9522 children. */
9523
d521ce57
TT
9524static const gdb_byte *
9525skip_one_die (const struct die_reader_specs *reader, const gdb_byte *info_ptr,
dee91e82 9526 struct abbrev_info *abbrev)
4bb7a0a7
DJ
9527{
9528 unsigned int bytes_read;
9529 struct attribute attr;
dee91e82
DE
9530 bfd *abfd = reader->abfd;
9531 struct dwarf2_cu *cu = reader->cu;
d521ce57 9532 const gdb_byte *buffer = reader->buffer;
f664829e 9533 const gdb_byte *buffer_end = reader->buffer_end;
4bb7a0a7
DJ
9534 unsigned int form, i;
9535
9536 for (i = 0; i < abbrev->num_attrs; i++)
9537 {
9538 /* The only abbrev we care about is DW_AT_sibling. */
9539 if (abbrev->attrs[i].name == DW_AT_sibling)
9540 {
dee91e82 9541 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
4bb7a0a7 9542 if (attr.form == DW_FORM_ref_addr)
3e43a32a
MS
9543 complaint (&symfile_complaints,
9544 _("ignoring absolute DW_AT_sibling"));
4bb7a0a7 9545 else
b9502d3f 9546 {
9c541725
PA
9547 sect_offset off = dwarf2_get_ref_die_offset (&attr);
9548 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
b9502d3f
WN
9549
9550 if (sibling_ptr < info_ptr)
9551 complaint (&symfile_complaints,
9552 _("DW_AT_sibling points backwards"));
22869d73
KS
9553 else if (sibling_ptr > reader->buffer_end)
9554 dwarf2_section_buffer_overflow_complaint (reader->die_section);
b9502d3f
WN
9555 else
9556 return sibling_ptr;
9557 }
4bb7a0a7
DJ
9558 }
9559
9560 /* If it isn't DW_AT_sibling, skip this attribute. */
9561 form = abbrev->attrs[i].form;
9562 skip_attribute:
9563 switch (form)
9564 {
4bb7a0a7 9565 case DW_FORM_ref_addr:
ae411497
TT
9566 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
9567 and later it is offset sized. */
9568 if (cu->header.version == 2)
9569 info_ptr += cu->header.addr_size;
9570 else
9571 info_ptr += cu->header.offset_size;
9572 break;
36586728
TT
9573 case DW_FORM_GNU_ref_alt:
9574 info_ptr += cu->header.offset_size;
9575 break;
ae411497 9576 case DW_FORM_addr:
4bb7a0a7
DJ
9577 info_ptr += cu->header.addr_size;
9578 break;
9579 case DW_FORM_data1:
9580 case DW_FORM_ref1:
9581 case DW_FORM_flag:
9582 info_ptr += 1;
9583 break;
2dc7f7b3 9584 case DW_FORM_flag_present:
43988095 9585 case DW_FORM_implicit_const:
2dc7f7b3 9586 break;
4bb7a0a7
DJ
9587 case DW_FORM_data2:
9588 case DW_FORM_ref2:
9589 info_ptr += 2;
9590 break;
9591 case DW_FORM_data4:
9592 case DW_FORM_ref4:
9593 info_ptr += 4;
9594 break;
9595 case DW_FORM_data8:
9596 case DW_FORM_ref8:
55f1336d 9597 case DW_FORM_ref_sig8:
4bb7a0a7
DJ
9598 info_ptr += 8;
9599 break;
0224619f
JK
9600 case DW_FORM_data16:
9601 info_ptr += 16;
9602 break;
4bb7a0a7 9603 case DW_FORM_string:
9b1c24c8 9604 read_direct_string (abfd, info_ptr, &bytes_read);
4bb7a0a7
DJ
9605 info_ptr += bytes_read;
9606 break;
2dc7f7b3 9607 case DW_FORM_sec_offset:
4bb7a0a7 9608 case DW_FORM_strp:
36586728 9609 case DW_FORM_GNU_strp_alt:
4bb7a0a7
DJ
9610 info_ptr += cu->header.offset_size;
9611 break;
2dc7f7b3 9612 case DW_FORM_exprloc:
4bb7a0a7
DJ
9613 case DW_FORM_block:
9614 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9615 info_ptr += bytes_read;
9616 break;
9617 case DW_FORM_block1:
9618 info_ptr += 1 + read_1_byte (abfd, info_ptr);
9619 break;
9620 case DW_FORM_block2:
9621 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
9622 break;
9623 case DW_FORM_block4:
9624 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
9625 break;
9626 case DW_FORM_sdata:
9627 case DW_FORM_udata:
9628 case DW_FORM_ref_udata:
3019eac3
DE
9629 case DW_FORM_GNU_addr_index:
9630 case DW_FORM_GNU_str_index:
d521ce57 9631 info_ptr = safe_skip_leb128 (info_ptr, buffer_end);
4bb7a0a7
DJ
9632 break;
9633 case DW_FORM_indirect:
9634 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9635 info_ptr += bytes_read;
9636 /* We need to continue parsing from here, so just go back to
9637 the top. */
9638 goto skip_attribute;
9639
9640 default:
3e43a32a
MS
9641 error (_("Dwarf Error: Cannot handle %s "
9642 "in DWARF reader [in module %s]"),
4bb7a0a7
DJ
9643 dwarf_form_name (form),
9644 bfd_get_filename (abfd));
9645 }
9646 }
9647
9648 if (abbrev->has_children)
dee91e82 9649 return skip_children (reader, info_ptr);
4bb7a0a7
DJ
9650 else
9651 return info_ptr;
9652}
9653
93311388 9654/* Locate ORIG_PDI's sibling.
dee91e82 9655 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
91c24f0a 9656
d521ce57 9657static const gdb_byte *
dee91e82
DE
9658locate_pdi_sibling (const struct die_reader_specs *reader,
9659 struct partial_die_info *orig_pdi,
d521ce57 9660 const gdb_byte *info_ptr)
91c24f0a
DC
9661{
9662 /* Do we know the sibling already? */
72bf9492 9663
91c24f0a
DC
9664 if (orig_pdi->sibling)
9665 return orig_pdi->sibling;
9666
9667 /* Are there any children to deal with? */
9668
9669 if (!orig_pdi->has_children)
9670 return info_ptr;
9671
4bb7a0a7 9672 /* Skip the children the long way. */
91c24f0a 9673
dee91e82 9674 return skip_children (reader, info_ptr);
91c24f0a
DC
9675}
9676
257e7a09 9677/* Expand this partial symbol table into a full symbol table. SELF is
442e4d9c 9678 not NULL. */
c906108c
SS
9679
9680static void
257e7a09
YQ
9681dwarf2_read_symtab (struct partial_symtab *self,
9682 struct objfile *objfile)
c906108c 9683{
257e7a09 9684 if (self->readin)
c906108c 9685 {
442e4d9c 9686 warning (_("bug: psymtab for %s is already read in."),
257e7a09 9687 self->filename);
442e4d9c
YQ
9688 }
9689 else
9690 {
9691 if (info_verbose)
c906108c 9692 {
442e4d9c 9693 printf_filtered (_("Reading in symbols for %s..."),
257e7a09 9694 self->filename);
442e4d9c 9695 gdb_flush (gdb_stdout);
c906108c 9696 }
c906108c 9697
442e4d9c 9698 /* Restore our global data. */
9a3c8263
SM
9699 dwarf2_per_objfile
9700 = (struct dwarf2_per_objfile *) objfile_data (objfile,
9701 dwarf2_objfile_data_key);
10b3939b 9702
442e4d9c
YQ
9703 /* If this psymtab is constructed from a debug-only objfile, the
9704 has_section_at_zero flag will not necessarily be correct. We
9705 can get the correct value for this flag by looking at the data
9706 associated with the (presumably stripped) associated objfile. */
9707 if (objfile->separate_debug_objfile_backlink)
9708 {
9709 struct dwarf2_per_objfile *dpo_backlink
9a3c8263
SM
9710 = ((struct dwarf2_per_objfile *)
9711 objfile_data (objfile->separate_debug_objfile_backlink,
9712 dwarf2_objfile_data_key));
9a619af0 9713
442e4d9c
YQ
9714 dwarf2_per_objfile->has_section_at_zero
9715 = dpo_backlink->has_section_at_zero;
9716 }
b2ab525c 9717
442e4d9c 9718 dwarf2_per_objfile->reading_partial_symbols = 0;
98bfdba5 9719
257e7a09 9720 psymtab_to_symtab_1 (self);
c906108c 9721
442e4d9c
YQ
9722 /* Finish up the debug error message. */
9723 if (info_verbose)
9724 printf_filtered (_("done.\n"));
c906108c 9725 }
95554aad
TT
9726
9727 process_cu_includes ();
c906108c 9728}
9cdd5dbd
DE
9729\f
9730/* Reading in full CUs. */
c906108c 9731
10b3939b
DJ
9732/* Add PER_CU to the queue. */
9733
9734static void
95554aad
TT
9735queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
9736 enum language pretend_language)
10b3939b
DJ
9737{
9738 struct dwarf2_queue_item *item;
9739
9740 per_cu->queued = 1;
8d749320 9741 item = XNEW (struct dwarf2_queue_item);
10b3939b 9742 item->per_cu = per_cu;
95554aad 9743 item->pretend_language = pretend_language;
10b3939b
DJ
9744 item->next = NULL;
9745
9746 if (dwarf2_queue == NULL)
9747 dwarf2_queue = item;
9748 else
9749 dwarf2_queue_tail->next = item;
9750
9751 dwarf2_queue_tail = item;
9752}
9753
89e63ee4
DE
9754/* If PER_CU is not yet queued, add it to the queue.
9755 If DEPENDENT_CU is non-NULL, it has a reference to PER_CU so add a
9756 dependency.
0907af0c 9757 The result is non-zero if PER_CU was queued, otherwise the result is zero
69d751e3
DE
9758 meaning either PER_CU is already queued or it is already loaded.
9759
9760 N.B. There is an invariant here that if a CU is queued then it is loaded.
9761 The caller is required to load PER_CU if we return non-zero. */
0907af0c
DE
9762
9763static int
89e63ee4 9764maybe_queue_comp_unit (struct dwarf2_cu *dependent_cu,
0907af0c
DE
9765 struct dwarf2_per_cu_data *per_cu,
9766 enum language pretend_language)
9767{
9768 /* We may arrive here during partial symbol reading, if we need full
9769 DIEs to process an unusual case (e.g. template arguments). Do
9770 not queue PER_CU, just tell our caller to load its DIEs. */
9771 if (dwarf2_per_objfile->reading_partial_symbols)
9772 {
9773 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
9774 return 1;
9775 return 0;
9776 }
9777
9778 /* Mark the dependence relation so that we don't flush PER_CU
9779 too early. */
89e63ee4
DE
9780 if (dependent_cu != NULL)
9781 dwarf2_add_dependence (dependent_cu, per_cu);
0907af0c
DE
9782
9783 /* If it's already on the queue, we have nothing to do. */
9784 if (per_cu->queued)
9785 return 0;
9786
9787 /* If the compilation unit is already loaded, just mark it as
9788 used. */
9789 if (per_cu->cu != NULL)
9790 {
9791 per_cu->cu->last_used = 0;
9792 return 0;
9793 }
9794
9795 /* Add it to the queue. */
9796 queue_comp_unit (per_cu, pretend_language);
9797
9798 return 1;
9799}
9800
10b3939b
DJ
9801/* Process the queue. */
9802
9803static void
a0f42c21 9804process_queue (void)
10b3939b
DJ
9805{
9806 struct dwarf2_queue_item *item, *next_item;
9807
b4f54984 9808 if (dwarf_read_debug)
45cfd468
DE
9809 {
9810 fprintf_unfiltered (gdb_stdlog,
9811 "Expanding one or more symtabs of objfile %s ...\n",
4262abfb 9812 objfile_name (dwarf2_per_objfile->objfile));
45cfd468
DE
9813 }
9814
03dd20cc
DJ
9815 /* The queue starts out with one item, but following a DIE reference
9816 may load a new CU, adding it to the end of the queue. */
10b3939b
DJ
9817 for (item = dwarf2_queue; item != NULL; dwarf2_queue = item = next_item)
9818 {
cc12ce38
DE
9819 if ((dwarf2_per_objfile->using_index
9820 ? !item->per_cu->v.quick->compunit_symtab
9821 : (item->per_cu->v.psymtab && !item->per_cu->v.psymtab->readin))
9822 /* Skip dummy CUs. */
9823 && item->per_cu->cu != NULL)
f4dc4d17
DE
9824 {
9825 struct dwarf2_per_cu_data *per_cu = item->per_cu;
73be47f5 9826 unsigned int debug_print_threshold;
247f5c4f 9827 char buf[100];
f4dc4d17 9828
247f5c4f 9829 if (per_cu->is_debug_types)
f4dc4d17 9830 {
247f5c4f
DE
9831 struct signatured_type *sig_type =
9832 (struct signatured_type *) per_cu;
9833
9834 sprintf (buf, "TU %s at offset 0x%x",
73be47f5 9835 hex_string (sig_type->signature),
9c541725 9836 to_underlying (per_cu->sect_off));
73be47f5
DE
9837 /* There can be 100s of TUs.
9838 Only print them in verbose mode. */
9839 debug_print_threshold = 2;
f4dc4d17 9840 }
247f5c4f 9841 else
73be47f5 9842 {
9c541725
PA
9843 sprintf (buf, "CU at offset 0x%x",
9844 to_underlying (per_cu->sect_off));
73be47f5
DE
9845 debug_print_threshold = 1;
9846 }
247f5c4f 9847
b4f54984 9848 if (dwarf_read_debug >= debug_print_threshold)
247f5c4f 9849 fprintf_unfiltered (gdb_stdlog, "Expanding symtab of %s\n", buf);
f4dc4d17
DE
9850
9851 if (per_cu->is_debug_types)
9852 process_full_type_unit (per_cu, item->pretend_language);
9853 else
9854 process_full_comp_unit (per_cu, item->pretend_language);
9855
b4f54984 9856 if (dwarf_read_debug >= debug_print_threshold)
247f5c4f 9857 fprintf_unfiltered (gdb_stdlog, "Done expanding %s\n", buf);
f4dc4d17 9858 }
10b3939b
DJ
9859
9860 item->per_cu->queued = 0;
9861 next_item = item->next;
9862 xfree (item);
9863 }
9864
9865 dwarf2_queue_tail = NULL;
45cfd468 9866
b4f54984 9867 if (dwarf_read_debug)
45cfd468
DE
9868 {
9869 fprintf_unfiltered (gdb_stdlog, "Done expanding symtabs of %s.\n",
4262abfb 9870 objfile_name (dwarf2_per_objfile->objfile));
45cfd468 9871 }
10b3939b
DJ
9872}
9873
9874/* Free all allocated queue entries. This function only releases anything if
9875 an error was thrown; if the queue was processed then it would have been
9876 freed as we went along. */
9877
9878static void
9879dwarf2_release_queue (void *dummy)
9880{
9881 struct dwarf2_queue_item *item, *last;
9882
9883 item = dwarf2_queue;
9884 while (item)
9885 {
9886 /* Anything still marked queued is likely to be in an
9887 inconsistent state, so discard it. */
9888 if (item->per_cu->queued)
9889 {
9890 if (item->per_cu->cu != NULL)
dee91e82 9891 free_one_cached_comp_unit (item->per_cu);
10b3939b
DJ
9892 item->per_cu->queued = 0;
9893 }
9894
9895 last = item;
9896 item = item->next;
9897 xfree (last);
9898 }
9899
9900 dwarf2_queue = dwarf2_queue_tail = NULL;
9901}
9902
9903/* Read in full symbols for PST, and anything it depends on. */
9904
c906108c 9905static void
fba45db2 9906psymtab_to_symtab_1 (struct partial_symtab *pst)
c906108c 9907{
10b3939b 9908 struct dwarf2_per_cu_data *per_cu;
aaa75496
JB
9909 int i;
9910
95554aad
TT
9911 if (pst->readin)
9912 return;
9913
aaa75496 9914 for (i = 0; i < pst->number_of_dependencies; i++)
95554aad
TT
9915 if (!pst->dependencies[i]->readin
9916 && pst->dependencies[i]->user == NULL)
aaa75496
JB
9917 {
9918 /* Inform about additional files that need to be read in. */
9919 if (info_verbose)
9920 {
a3f17187 9921 /* FIXME: i18n: Need to make this a single string. */
aaa75496
JB
9922 fputs_filtered (" ", gdb_stdout);
9923 wrap_here ("");
9924 fputs_filtered ("and ", gdb_stdout);
9925 wrap_here ("");
9926 printf_filtered ("%s...", pst->dependencies[i]->filename);
0963b4bd 9927 wrap_here (""); /* Flush output. */
aaa75496
JB
9928 gdb_flush (gdb_stdout);
9929 }
9930 psymtab_to_symtab_1 (pst->dependencies[i]);
9931 }
9932
9a3c8263 9933 per_cu = (struct dwarf2_per_cu_data *) pst->read_symtab_private;
10b3939b
DJ
9934
9935 if (per_cu == NULL)
aaa75496
JB
9936 {
9937 /* It's an include file, no symbols to read for it.
9938 Everything is in the parent symtab. */
9939 pst->readin = 1;
9940 return;
9941 }
c906108c 9942
a0f42c21 9943 dw2_do_instantiate_symtab (per_cu);
10b3939b
DJ
9944}
9945
dee91e82
DE
9946/* Trivial hash function for die_info: the hash value of a DIE
9947 is its offset in .debug_info for this objfile. */
10b3939b 9948
dee91e82
DE
9949static hashval_t
9950die_hash (const void *item)
10b3939b 9951{
9a3c8263 9952 const struct die_info *die = (const struct die_info *) item;
6502dd73 9953
9c541725 9954 return to_underlying (die->sect_off);
dee91e82 9955}
63d06c5c 9956
dee91e82
DE
9957/* Trivial comparison function for die_info structures: two DIEs
9958 are equal if they have the same offset. */
98bfdba5 9959
dee91e82
DE
9960static int
9961die_eq (const void *item_lhs, const void *item_rhs)
9962{
9a3c8263
SM
9963 const struct die_info *die_lhs = (const struct die_info *) item_lhs;
9964 const struct die_info *die_rhs = (const struct die_info *) item_rhs;
c906108c 9965
9c541725 9966 return die_lhs->sect_off == die_rhs->sect_off;
dee91e82 9967}
c906108c 9968
dee91e82
DE
9969/* die_reader_func for load_full_comp_unit.
9970 This is identical to read_signatured_type_reader,
9971 but is kept separate for now. */
c906108c 9972
dee91e82
DE
9973static void
9974load_full_comp_unit_reader (const struct die_reader_specs *reader,
d521ce57 9975 const gdb_byte *info_ptr,
dee91e82
DE
9976 struct die_info *comp_unit_die,
9977 int has_children,
9978 void *data)
9979{
9980 struct dwarf2_cu *cu = reader->cu;
9a3c8263 9981 enum language *language_ptr = (enum language *) data;
6caca83c 9982
dee91e82
DE
9983 gdb_assert (cu->die_hash == NULL);
9984 cu->die_hash =
9985 htab_create_alloc_ex (cu->header.length / 12,
9986 die_hash,
9987 die_eq,
9988 NULL,
9989 &cu->comp_unit_obstack,
9990 hashtab_obstack_allocate,
9991 dummy_obstack_deallocate);
e142c38c 9992
dee91e82
DE
9993 if (has_children)
9994 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
9995 &info_ptr, comp_unit_die);
9996 cu->dies = comp_unit_die;
9997 /* comp_unit_die is not stored in die_hash, no need. */
10b3939b
DJ
9998
9999 /* We try not to read any attributes in this function, because not
9cdd5dbd 10000 all CUs needed for references have been loaded yet, and symbol
10b3939b 10001 table processing isn't initialized. But we have to set the CU language,
dee91e82
DE
10002 or we won't be able to build types correctly.
10003 Similarly, if we do not read the producer, we can not apply
10004 producer-specific interpretation. */
95554aad 10005 prepare_one_comp_unit (cu, cu->dies, *language_ptr);
dee91e82 10006}
10b3939b 10007
dee91e82 10008/* Load the DIEs associated with PER_CU into memory. */
a6c727b2 10009
dee91e82 10010static void
95554aad
TT
10011load_full_comp_unit (struct dwarf2_per_cu_data *this_cu,
10012 enum language pretend_language)
dee91e82 10013{
3019eac3 10014 gdb_assert (! this_cu->is_debug_types);
c5b7e1cb 10015
f4dc4d17
DE
10016 init_cutu_and_read_dies (this_cu, NULL, 1, 1,
10017 load_full_comp_unit_reader, &pretend_language);
10b3939b
DJ
10018}
10019
3da10d80
KS
10020/* Add a DIE to the delayed physname list. */
10021
10022static void
10023add_to_method_list (struct type *type, int fnfield_index, int index,
10024 const char *name, struct die_info *die,
10025 struct dwarf2_cu *cu)
10026{
10027 struct delayed_method_info mi;
10028 mi.type = type;
10029 mi.fnfield_index = fnfield_index;
10030 mi.index = index;
10031 mi.name = name;
10032 mi.die = die;
10033 VEC_safe_push (delayed_method_info, cu->method_list, &mi);
10034}
10035
10036/* A cleanup for freeing the delayed method list. */
10037
10038static void
10039free_delayed_list (void *ptr)
10040{
10041 struct dwarf2_cu *cu = (struct dwarf2_cu *) ptr;
10042 if (cu->method_list != NULL)
10043 {
10044 VEC_free (delayed_method_info, cu->method_list);
10045 cu->method_list = NULL;
10046 }
10047}
10048
3693fdb3
PA
10049/* Check whether [PHYSNAME, PHYSNAME+LEN) ends with a modifier like
10050 "const" / "volatile". If so, decrements LEN by the length of the
10051 modifier and return true. Otherwise return false. */
10052
10053template<size_t N>
10054static bool
10055check_modifier (const char *physname, size_t &len, const char (&mod)[N])
10056{
10057 size_t mod_len = sizeof (mod) - 1;
10058 if (len > mod_len && startswith (physname + (len - mod_len), mod))
10059 {
10060 len -= mod_len;
10061 return true;
10062 }
10063 return false;
10064}
10065
3da10d80
KS
10066/* Compute the physnames of any methods on the CU's method list.
10067
10068 The computation of method physnames is delayed in order to avoid the
10069 (bad) condition that one of the method's formal parameters is of an as yet
10070 incomplete type. */
10071
10072static void
10073compute_delayed_physnames (struct dwarf2_cu *cu)
10074{
10075 int i;
10076 struct delayed_method_info *mi;
3693fdb3
PA
10077
10078 /* Only C++ delays computing physnames. */
10079 if (VEC_empty (delayed_method_info, cu->method_list))
10080 return;
10081 gdb_assert (cu->language == language_cplus);
10082
3da10d80
KS
10083 for (i = 0; VEC_iterate (delayed_method_info, cu->method_list, i, mi) ; ++i)
10084 {
1d06ead6 10085 const char *physname;
3da10d80
KS
10086 struct fn_fieldlist *fn_flp
10087 = &TYPE_FN_FIELDLIST (mi->type, mi->fnfield_index);
7d455152 10088 physname = dwarf2_physname (mi->name, mi->die, cu);
005e54bb
DE
10089 TYPE_FN_FIELD_PHYSNAME (fn_flp->fn_fields, mi->index)
10090 = physname ? physname : "";
3693fdb3
PA
10091
10092 /* Since there's no tag to indicate whether a method is a
10093 const/volatile overload, extract that information out of the
10094 demangled name. */
10095 if (physname != NULL)
10096 {
10097 size_t len = strlen (physname);
10098
10099 while (1)
10100 {
10101 if (physname[len] == ')') /* shortcut */
10102 break;
10103 else if (check_modifier (physname, len, " const"))
10104 TYPE_FN_FIELD_CONST (fn_flp->fn_fields, mi->index) = 1;
10105 else if (check_modifier (physname, len, " volatile"))
10106 TYPE_FN_FIELD_VOLATILE (fn_flp->fn_fields, mi->index) = 1;
10107 else
10108 break;
10109 }
10110 }
3da10d80
KS
10111 }
10112}
10113
a766d390
DE
10114/* Go objects should be embedded in a DW_TAG_module DIE,
10115 and it's not clear if/how imported objects will appear.
10116 To keep Go support simple until that's worked out,
10117 go back through what we've read and create something usable.
10118 We could do this while processing each DIE, and feels kinda cleaner,
10119 but that way is more invasive.
10120 This is to, for example, allow the user to type "p var" or "b main"
10121 without having to specify the package name, and allow lookups
10122 of module.object to work in contexts that use the expression
10123 parser. */
10124
10125static void
10126fixup_go_packaging (struct dwarf2_cu *cu)
10127{
10128 char *package_name = NULL;
10129 struct pending *list;
10130 int i;
10131
10132 for (list = global_symbols; list != NULL; list = list->next)
10133 {
10134 for (i = 0; i < list->nsyms; ++i)
10135 {
10136 struct symbol *sym = list->symbol[i];
10137
10138 if (SYMBOL_LANGUAGE (sym) == language_go
10139 && SYMBOL_CLASS (sym) == LOC_BLOCK)
10140 {
10141 char *this_package_name = go_symbol_package_name (sym);
10142
10143 if (this_package_name == NULL)
10144 continue;
10145 if (package_name == NULL)
10146 package_name = this_package_name;
10147 else
10148 {
10149 if (strcmp (package_name, this_package_name) != 0)
10150 complaint (&symfile_complaints,
10151 _("Symtab %s has objects from two different Go packages: %s and %s"),
08be3fe3
DE
10152 (symbol_symtab (sym) != NULL
10153 ? symtab_to_filename_for_display
10154 (symbol_symtab (sym))
4262abfb 10155 : objfile_name (cu->objfile)),
a766d390
DE
10156 this_package_name, package_name);
10157 xfree (this_package_name);
10158 }
10159 }
10160 }
10161 }
10162
10163 if (package_name != NULL)
10164 {
10165 struct objfile *objfile = cu->objfile;
34a68019 10166 const char *saved_package_name
224c3ddb
SM
10167 = (const char *) obstack_copy0 (&objfile->per_bfd->storage_obstack,
10168 package_name,
10169 strlen (package_name));
19f392bc
UW
10170 struct type *type = init_type (objfile, TYPE_CODE_MODULE, 0,
10171 saved_package_name);
a766d390
DE
10172 struct symbol *sym;
10173
10174 TYPE_TAG_NAME (type) = TYPE_NAME (type);
10175
e623cf5d 10176 sym = allocate_symbol (objfile);
f85f34ed 10177 SYMBOL_SET_LANGUAGE (sym, language_go, &objfile->objfile_obstack);
86f62fd7
TT
10178 SYMBOL_SET_NAMES (sym, saved_package_name,
10179 strlen (saved_package_name), 0, objfile);
a766d390
DE
10180 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
10181 e.g., "main" finds the "main" module and not C's main(). */
10182 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
f1e6e072 10183 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
a766d390
DE
10184 SYMBOL_TYPE (sym) = type;
10185
10186 add_symbol_to_list (sym, &global_symbols);
10187
10188 xfree (package_name);
10189 }
10190}
10191
95554aad
TT
10192/* Return the symtab for PER_CU. This works properly regardless of
10193 whether we're using the index or psymtabs. */
10194
43f3e411
DE
10195static struct compunit_symtab *
10196get_compunit_symtab (struct dwarf2_per_cu_data *per_cu)
95554aad
TT
10197{
10198 return (dwarf2_per_objfile->using_index
43f3e411
DE
10199 ? per_cu->v.quick->compunit_symtab
10200 : per_cu->v.psymtab->compunit_symtab);
95554aad
TT
10201}
10202
10203/* A helper function for computing the list of all symbol tables
10204 included by PER_CU. */
10205
10206static void
43f3e411 10207recursively_compute_inclusions (VEC (compunit_symtab_ptr) **result,
ec94af83 10208 htab_t all_children, htab_t all_type_symtabs,
f9125b6c 10209 struct dwarf2_per_cu_data *per_cu,
43f3e411 10210 struct compunit_symtab *immediate_parent)
95554aad
TT
10211{
10212 void **slot;
10213 int ix;
43f3e411 10214 struct compunit_symtab *cust;
95554aad
TT
10215 struct dwarf2_per_cu_data *iter;
10216
10217 slot = htab_find_slot (all_children, per_cu, INSERT);
10218 if (*slot != NULL)
10219 {
10220 /* This inclusion and its children have been processed. */
10221 return;
10222 }
10223
10224 *slot = per_cu;
10225 /* Only add a CU if it has a symbol table. */
43f3e411
DE
10226 cust = get_compunit_symtab (per_cu);
10227 if (cust != NULL)
ec94af83
DE
10228 {
10229 /* If this is a type unit only add its symbol table if we haven't
10230 seen it yet (type unit per_cu's can share symtabs). */
10231 if (per_cu->is_debug_types)
10232 {
43f3e411 10233 slot = htab_find_slot (all_type_symtabs, cust, INSERT);
ec94af83
DE
10234 if (*slot == NULL)
10235 {
43f3e411
DE
10236 *slot = cust;
10237 VEC_safe_push (compunit_symtab_ptr, *result, cust);
10238 if (cust->user == NULL)
10239 cust->user = immediate_parent;
ec94af83
DE
10240 }
10241 }
10242 else
f9125b6c 10243 {
43f3e411
DE
10244 VEC_safe_push (compunit_symtab_ptr, *result, cust);
10245 if (cust->user == NULL)
10246 cust->user = immediate_parent;
f9125b6c 10247 }
ec94af83 10248 }
95554aad
TT
10249
10250 for (ix = 0;
796a7ff8 10251 VEC_iterate (dwarf2_per_cu_ptr, per_cu->imported_symtabs, ix, iter);
95554aad 10252 ++ix)
ec94af83
DE
10253 {
10254 recursively_compute_inclusions (result, all_children,
43f3e411 10255 all_type_symtabs, iter, cust);
ec94af83 10256 }
95554aad
TT
10257}
10258
43f3e411 10259/* Compute the compunit_symtab 'includes' fields for the compunit_symtab of
95554aad
TT
10260 PER_CU. */
10261
10262static void
43f3e411 10263compute_compunit_symtab_includes (struct dwarf2_per_cu_data *per_cu)
95554aad 10264{
f4dc4d17
DE
10265 gdb_assert (! per_cu->is_debug_types);
10266
796a7ff8 10267 if (!VEC_empty (dwarf2_per_cu_ptr, per_cu->imported_symtabs))
95554aad
TT
10268 {
10269 int ix, len;
ec94af83 10270 struct dwarf2_per_cu_data *per_cu_iter;
43f3e411
DE
10271 struct compunit_symtab *compunit_symtab_iter;
10272 VEC (compunit_symtab_ptr) *result_symtabs = NULL;
ec94af83 10273 htab_t all_children, all_type_symtabs;
43f3e411 10274 struct compunit_symtab *cust = get_compunit_symtab (per_cu);
95554aad
TT
10275
10276 /* If we don't have a symtab, we can just skip this case. */
43f3e411 10277 if (cust == NULL)
95554aad
TT
10278 return;
10279
10280 all_children = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
10281 NULL, xcalloc, xfree);
ec94af83
DE
10282 all_type_symtabs = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
10283 NULL, xcalloc, xfree);
95554aad
TT
10284
10285 for (ix = 0;
796a7ff8 10286 VEC_iterate (dwarf2_per_cu_ptr, per_cu->imported_symtabs,
ec94af83 10287 ix, per_cu_iter);
95554aad 10288 ++ix)
ec94af83
DE
10289 {
10290 recursively_compute_inclusions (&result_symtabs, all_children,
f9125b6c 10291 all_type_symtabs, per_cu_iter,
43f3e411 10292 cust);
ec94af83 10293 }
95554aad 10294
ec94af83 10295 /* Now we have a transitive closure of all the included symtabs. */
43f3e411
DE
10296 len = VEC_length (compunit_symtab_ptr, result_symtabs);
10297 cust->includes
8d749320
SM
10298 = XOBNEWVEC (&dwarf2_per_objfile->objfile->objfile_obstack,
10299 struct compunit_symtab *, len + 1);
95554aad 10300 for (ix = 0;
43f3e411
DE
10301 VEC_iterate (compunit_symtab_ptr, result_symtabs, ix,
10302 compunit_symtab_iter);
95554aad 10303 ++ix)
43f3e411
DE
10304 cust->includes[ix] = compunit_symtab_iter;
10305 cust->includes[len] = NULL;
95554aad 10306
43f3e411 10307 VEC_free (compunit_symtab_ptr, result_symtabs);
95554aad 10308 htab_delete (all_children);
ec94af83 10309 htab_delete (all_type_symtabs);
95554aad
TT
10310 }
10311}
10312
10313/* Compute the 'includes' field for the symtabs of all the CUs we just
10314 read. */
10315
10316static void
10317process_cu_includes (void)
10318{
10319 int ix;
10320 struct dwarf2_per_cu_data *iter;
10321
10322 for (ix = 0;
10323 VEC_iterate (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus,
10324 ix, iter);
10325 ++ix)
f4dc4d17
DE
10326 {
10327 if (! iter->is_debug_types)
43f3e411 10328 compute_compunit_symtab_includes (iter);
f4dc4d17 10329 }
95554aad
TT
10330
10331 VEC_free (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus);
10332}
10333
9cdd5dbd 10334/* Generate full symbol information for PER_CU, whose DIEs have
10b3939b
DJ
10335 already been loaded into memory. */
10336
10337static void
95554aad
TT
10338process_full_comp_unit (struct dwarf2_per_cu_data *per_cu,
10339 enum language pretend_language)
10b3939b 10340{
10b3939b 10341 struct dwarf2_cu *cu = per_cu->cu;
9291a0cd 10342 struct objfile *objfile = per_cu->objfile;
3e29f34a 10343 struct gdbarch *gdbarch = get_objfile_arch (objfile);
10b3939b 10344 CORE_ADDR lowpc, highpc;
43f3e411 10345 struct compunit_symtab *cust;
33c7c59d 10346 struct cleanup *delayed_list_cleanup;
10b3939b 10347 CORE_ADDR baseaddr;
4359dff1 10348 struct block *static_block;
3e29f34a 10349 CORE_ADDR addr;
10b3939b
DJ
10350
10351 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
10352
10b3939b 10353 buildsym_init ();
33c7c59d 10354 scoped_free_pendings free_pending;
3da10d80 10355 delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
10b3939b
DJ
10356
10357 cu->list_in_scope = &file_symbols;
c906108c 10358
95554aad
TT
10359 cu->language = pretend_language;
10360 cu->language_defn = language_def (cu->language);
10361
c906108c 10362 /* Do line number decoding in read_file_scope () */
10b3939b 10363 process_die (cu->dies, cu);
c906108c 10364
a766d390
DE
10365 /* For now fudge the Go package. */
10366 if (cu->language == language_go)
10367 fixup_go_packaging (cu);
10368
3da10d80
KS
10369 /* Now that we have processed all the DIEs in the CU, all the types
10370 should be complete, and it should now be safe to compute all of the
10371 physnames. */
10372 compute_delayed_physnames (cu);
10373 do_cleanups (delayed_list_cleanup);
10374
fae299cd
DC
10375 /* Some compilers don't define a DW_AT_high_pc attribute for the
10376 compilation unit. If the DW_AT_high_pc is missing, synthesize
10377 it, by scanning the DIE's below the compilation unit. */
10b3939b 10378 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
c906108c 10379
3e29f34a
MR
10380 addr = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
10381 static_block = end_symtab_get_static_block (addr, 0, 1);
4359dff1
JK
10382
10383 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
10384 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
10385 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
10386 addrmap to help ensure it has an accurate map of pc values belonging to
10387 this comp unit. */
10388 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
10389
43f3e411
DE
10390 cust = end_symtab_from_static_block (static_block,
10391 SECT_OFF_TEXT (objfile), 0);
c906108c 10392
43f3e411 10393 if (cust != NULL)
c906108c 10394 {
df15bd07 10395 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
4632c0d0 10396
8be455d7
JK
10397 /* Set symtab language to language from DW_AT_language. If the
10398 compilation is from a C file generated by language preprocessors, do
10399 not set the language if it was already deduced by start_subfile. */
43f3e411 10400 if (!(cu->language == language_c
40e3ad0e 10401 && COMPUNIT_FILETABS (cust)->language != language_unknown))
43f3e411 10402 COMPUNIT_FILETABS (cust)->language = cu->language;
8be455d7
JK
10403
10404 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
10405 produce DW_AT_location with location lists but it can be possibly
ab260dad
JK
10406 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
10407 there were bugs in prologue debug info, fixed later in GCC-4.5
10408 by "unwind info for epilogues" patch (which is not directly related).
8be455d7
JK
10409
10410 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
10411 needed, it would be wrong due to missing DW_AT_producer there.
10412
10413 Still one can confuse GDB by using non-standard GCC compilation
10414 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
10415 */
ab260dad 10416 if (cu->has_loclist && gcc_4_minor >= 5)
43f3e411 10417 cust->locations_valid = 1;
e0d00bc7
JK
10418
10419 if (gcc_4_minor >= 5)
43f3e411 10420 cust->epilogue_unwind_valid = 1;
96408a79 10421
43f3e411 10422 cust->call_site_htab = cu->call_site_htab;
c906108c 10423 }
9291a0cd
TT
10424
10425 if (dwarf2_per_objfile->using_index)
43f3e411 10426 per_cu->v.quick->compunit_symtab = cust;
9291a0cd
TT
10427 else
10428 {
10429 struct partial_symtab *pst = per_cu->v.psymtab;
43f3e411 10430 pst->compunit_symtab = cust;
9291a0cd
TT
10431 pst->readin = 1;
10432 }
c906108c 10433
95554aad
TT
10434 /* Push it for inclusion processing later. */
10435 VEC_safe_push (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus, per_cu);
f4dc4d17 10436}
45cfd468 10437
f4dc4d17
DE
10438/* Generate full symbol information for type unit PER_CU, whose DIEs have
10439 already been loaded into memory. */
10440
10441static void
10442process_full_type_unit (struct dwarf2_per_cu_data *per_cu,
10443 enum language pretend_language)
10444{
10445 struct dwarf2_cu *cu = per_cu->cu;
10446 struct objfile *objfile = per_cu->objfile;
43f3e411 10447 struct compunit_symtab *cust;
33c7c59d 10448 struct cleanup *delayed_list_cleanup;
0186c6a7
DE
10449 struct signatured_type *sig_type;
10450
10451 gdb_assert (per_cu->is_debug_types);
10452 sig_type = (struct signatured_type *) per_cu;
f4dc4d17
DE
10453
10454 buildsym_init ();
33c7c59d 10455 scoped_free_pendings free_pending;
f4dc4d17
DE
10456 delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
10457
10458 cu->list_in_scope = &file_symbols;
10459
10460 cu->language = pretend_language;
10461 cu->language_defn = language_def (cu->language);
10462
10463 /* The symbol tables are set up in read_type_unit_scope. */
10464 process_die (cu->dies, cu);
10465
10466 /* For now fudge the Go package. */
10467 if (cu->language == language_go)
10468 fixup_go_packaging (cu);
10469
10470 /* Now that we have processed all the DIEs in the CU, all the types
10471 should be complete, and it should now be safe to compute all of the
10472 physnames. */
10473 compute_delayed_physnames (cu);
10474 do_cleanups (delayed_list_cleanup);
10475
10476 /* TUs share symbol tables.
10477 If this is the first TU to use this symtab, complete the construction
094b34ac
DE
10478 of it with end_expandable_symtab. Otherwise, complete the addition of
10479 this TU's symbols to the existing symtab. */
43f3e411 10480 if (sig_type->type_unit_group->compunit_symtab == NULL)
45cfd468 10481 {
43f3e411
DE
10482 cust = end_expandable_symtab (0, SECT_OFF_TEXT (objfile));
10483 sig_type->type_unit_group->compunit_symtab = cust;
f4dc4d17 10484
43f3e411 10485 if (cust != NULL)
f4dc4d17
DE
10486 {
10487 /* Set symtab language to language from DW_AT_language. If the
10488 compilation is from a C file generated by language preprocessors,
10489 do not set the language if it was already deduced by
10490 start_subfile. */
43f3e411
DE
10491 if (!(cu->language == language_c
10492 && COMPUNIT_FILETABS (cust)->language != language_c))
10493 COMPUNIT_FILETABS (cust)->language = cu->language;
f4dc4d17
DE
10494 }
10495 }
10496 else
10497 {
0ab9ce85 10498 augment_type_symtab ();
43f3e411 10499 cust = sig_type->type_unit_group->compunit_symtab;
f4dc4d17
DE
10500 }
10501
10502 if (dwarf2_per_objfile->using_index)
43f3e411 10503 per_cu->v.quick->compunit_symtab = cust;
f4dc4d17
DE
10504 else
10505 {
10506 struct partial_symtab *pst = per_cu->v.psymtab;
43f3e411 10507 pst->compunit_symtab = cust;
f4dc4d17 10508 pst->readin = 1;
45cfd468 10509 }
c906108c
SS
10510}
10511
95554aad
TT
10512/* Process an imported unit DIE. */
10513
10514static void
10515process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
10516{
10517 struct attribute *attr;
10518
f4dc4d17
DE
10519 /* For now we don't handle imported units in type units. */
10520 if (cu->per_cu->is_debug_types)
10521 {
10522 error (_("Dwarf Error: DW_TAG_imported_unit is not"
10523 " supported in type units [in module %s]"),
4262abfb 10524 objfile_name (cu->objfile));
f4dc4d17
DE
10525 }
10526
95554aad
TT
10527 attr = dwarf2_attr (die, DW_AT_import, cu);
10528 if (attr != NULL)
10529 {
9c541725
PA
10530 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
10531 bool is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
10532 dwarf2_per_cu_data *per_cu
10533 = dwarf2_find_containing_comp_unit (sect_off, is_dwz, cu->objfile);
95554aad 10534
69d751e3 10535 /* If necessary, add it to the queue and load its DIEs. */
95554aad
TT
10536 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
10537 load_full_comp_unit (per_cu, cu->language);
10538
796a7ff8 10539 VEC_safe_push (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs,
95554aad
TT
10540 per_cu);
10541 }
10542}
10543
4c8aa72d
PA
10544/* RAII object that represents a process_die scope: i.e.,
10545 starts/finishes processing a DIE. */
10546class process_die_scope
adde2bff 10547{
4c8aa72d
PA
10548public:
10549 process_die_scope (die_info *die, dwarf2_cu *cu)
10550 : m_die (die), m_cu (cu)
10551 {
10552 /* We should only be processing DIEs not already in process. */
10553 gdb_assert (!m_die->in_process);
10554 m_die->in_process = true;
10555 }
8c3cb9fa 10556
4c8aa72d
PA
10557 ~process_die_scope ()
10558 {
10559 m_die->in_process = false;
10560
10561 /* If we're done processing the DIE for the CU that owns the line
10562 header, we don't need the line header anymore. */
10563 if (m_cu->line_header_die_owner == m_die)
10564 {
10565 delete m_cu->line_header;
10566 m_cu->line_header = NULL;
10567 m_cu->line_header_die_owner = NULL;
10568 }
10569 }
10570
10571private:
10572 die_info *m_die;
10573 dwarf2_cu *m_cu;
10574};
adde2bff 10575
c906108c
SS
10576/* Process a die and its children. */
10577
10578static void
e7c27a73 10579process_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 10580{
4c8aa72d 10581 process_die_scope scope (die, cu);
adde2bff 10582
c906108c
SS
10583 switch (die->tag)
10584 {
10585 case DW_TAG_padding:
10586 break;
10587 case DW_TAG_compile_unit:
95554aad 10588 case DW_TAG_partial_unit:
e7c27a73 10589 read_file_scope (die, cu);
c906108c 10590 break;
348e048f
DE
10591 case DW_TAG_type_unit:
10592 read_type_unit_scope (die, cu);
10593 break;
c906108c 10594 case DW_TAG_subprogram:
c906108c 10595 case DW_TAG_inlined_subroutine:
edb3359d 10596 read_func_scope (die, cu);
c906108c
SS
10597 break;
10598 case DW_TAG_lexical_block:
14898363
L
10599 case DW_TAG_try_block:
10600 case DW_TAG_catch_block:
e7c27a73 10601 read_lexical_block_scope (die, cu);
c906108c 10602 break;
216f72a1 10603 case DW_TAG_call_site:
96408a79
SA
10604 case DW_TAG_GNU_call_site:
10605 read_call_site_scope (die, cu);
10606 break;
c906108c 10607 case DW_TAG_class_type:
680b30c7 10608 case DW_TAG_interface_type:
c906108c
SS
10609 case DW_TAG_structure_type:
10610 case DW_TAG_union_type:
134d01f1 10611 process_structure_scope (die, cu);
c906108c
SS
10612 break;
10613 case DW_TAG_enumeration_type:
134d01f1 10614 process_enumeration_scope (die, cu);
c906108c 10615 break;
134d01f1 10616
f792889a
DJ
10617 /* These dies have a type, but processing them does not create
10618 a symbol or recurse to process the children. Therefore we can
10619 read them on-demand through read_type_die. */
c906108c 10620 case DW_TAG_subroutine_type:
72019c9c 10621 case DW_TAG_set_type:
c906108c 10622 case DW_TAG_array_type:
c906108c 10623 case DW_TAG_pointer_type:
c906108c 10624 case DW_TAG_ptr_to_member_type:
c906108c 10625 case DW_TAG_reference_type:
4297a3f0 10626 case DW_TAG_rvalue_reference_type:
c906108c 10627 case DW_TAG_string_type:
c906108c 10628 break;
134d01f1 10629
c906108c 10630 case DW_TAG_base_type:
a02abb62 10631 case DW_TAG_subrange_type:
cb249c71 10632 case DW_TAG_typedef:
134d01f1
DJ
10633 /* Add a typedef symbol for the type definition, if it has a
10634 DW_AT_name. */
f792889a 10635 new_symbol (die, read_type_die (die, cu), cu);
a02abb62 10636 break;
c906108c 10637 case DW_TAG_common_block:
e7c27a73 10638 read_common_block (die, cu);
c906108c
SS
10639 break;
10640 case DW_TAG_common_inclusion:
10641 break;
d9fa45fe 10642 case DW_TAG_namespace:
4d4ec4e5 10643 cu->processing_has_namespace_info = 1;
e7c27a73 10644 read_namespace (die, cu);
d9fa45fe 10645 break;
5d7cb8df 10646 case DW_TAG_module:
4d4ec4e5 10647 cu->processing_has_namespace_info = 1;
5d7cb8df
JK
10648 read_module (die, cu);
10649 break;
d9fa45fe 10650 case DW_TAG_imported_declaration:
74921315
KS
10651 cu->processing_has_namespace_info = 1;
10652 if (read_namespace_alias (die, cu))
10653 break;
10654 /* The declaration is not a global namespace alias: fall through. */
d9fa45fe 10655 case DW_TAG_imported_module:
4d4ec4e5 10656 cu->processing_has_namespace_info = 1;
27aa8d6a
SW
10657 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
10658 || cu->language != language_fortran))
10659 complaint (&symfile_complaints, _("Tag '%s' has unexpected children"),
10660 dwarf_tag_name (die->tag));
10661 read_import_statement (die, cu);
d9fa45fe 10662 break;
95554aad
TT
10663
10664 case DW_TAG_imported_unit:
10665 process_imported_unit_die (die, cu);
10666 break;
10667
71a3c369
TT
10668 case DW_TAG_variable:
10669 read_variable (die, cu);
10670 break;
10671
c906108c 10672 default:
e7c27a73 10673 new_symbol (die, NULL, cu);
c906108c
SS
10674 break;
10675 }
10676}
ca69b9e6
DE
10677\f
10678/* DWARF name computation. */
c906108c 10679
94af9270
KS
10680/* A helper function for dwarf2_compute_name which determines whether DIE
10681 needs to have the name of the scope prepended to the name listed in the
10682 die. */
10683
10684static int
10685die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
10686{
1c809c68
TT
10687 struct attribute *attr;
10688
94af9270
KS
10689 switch (die->tag)
10690 {
10691 case DW_TAG_namespace:
10692 case DW_TAG_typedef:
10693 case DW_TAG_class_type:
10694 case DW_TAG_interface_type:
10695 case DW_TAG_structure_type:
10696 case DW_TAG_union_type:
10697 case DW_TAG_enumeration_type:
10698 case DW_TAG_enumerator:
10699 case DW_TAG_subprogram:
08a76f8a 10700 case DW_TAG_inlined_subroutine:
94af9270 10701 case DW_TAG_member:
74921315 10702 case DW_TAG_imported_declaration:
94af9270
KS
10703 return 1;
10704
10705 case DW_TAG_variable:
c2b0a229 10706 case DW_TAG_constant:
94af9270
KS
10707 /* We only need to prefix "globally" visible variables. These include
10708 any variable marked with DW_AT_external or any variable that
10709 lives in a namespace. [Variables in anonymous namespaces
10710 require prefixing, but they are not DW_AT_external.] */
10711
10712 if (dwarf2_attr (die, DW_AT_specification, cu))
10713 {
10714 struct dwarf2_cu *spec_cu = cu;
9a619af0 10715
94af9270
KS
10716 return die_needs_namespace (die_specification (die, &spec_cu),
10717 spec_cu);
10718 }
10719
1c809c68 10720 attr = dwarf2_attr (die, DW_AT_external, cu);
f55ee35c
JK
10721 if (attr == NULL && die->parent->tag != DW_TAG_namespace
10722 && die->parent->tag != DW_TAG_module)
1c809c68
TT
10723 return 0;
10724 /* A variable in a lexical block of some kind does not need a
10725 namespace, even though in C++ such variables may be external
10726 and have a mangled name. */
10727 if (die->parent->tag == DW_TAG_lexical_block
10728 || die->parent->tag == DW_TAG_try_block
1054b214
TT
10729 || die->parent->tag == DW_TAG_catch_block
10730 || die->parent->tag == DW_TAG_subprogram)
1c809c68
TT
10731 return 0;
10732 return 1;
94af9270
KS
10733
10734 default:
10735 return 0;
10736 }
10737}
10738
73b9be8b
KS
10739/* Return the DIE's linkage name attribute, either DW_AT_linkage_name
10740 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10741 defined for the given DIE. */
10742
10743static struct attribute *
10744dw2_linkage_name_attr (struct die_info *die, struct dwarf2_cu *cu)
10745{
10746 struct attribute *attr;
10747
10748 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
10749 if (attr == NULL)
10750 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
10751
10752 return attr;
10753}
10754
10755/* Return the DIE's linkage name as a string, either DW_AT_linkage_name
10756 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10757 defined for the given DIE. */
10758
10759static const char *
10760dw2_linkage_name (struct die_info *die, struct dwarf2_cu *cu)
10761{
10762 const char *linkage_name;
10763
10764 linkage_name = dwarf2_string_attr (die, DW_AT_linkage_name, cu);
10765 if (linkage_name == NULL)
10766 linkage_name = dwarf2_string_attr (die, DW_AT_MIPS_linkage_name, cu);
10767
10768 return linkage_name;
10769}
10770
94af9270 10771/* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
a766d390 10772 compute the physname for the object, which include a method's:
9c37b5ae 10773 - formal parameters (C++),
a766d390 10774 - receiver type (Go),
a766d390
DE
10775
10776 The term "physname" is a bit confusing.
10777 For C++, for example, it is the demangled name.
10778 For Go, for example, it's the mangled name.
94af9270 10779
af6b7be1
JB
10780 For Ada, return the DIE's linkage name rather than the fully qualified
10781 name. PHYSNAME is ignored..
10782
94af9270
KS
10783 The result is allocated on the objfile_obstack and canonicalized. */
10784
10785static const char *
15d034d0
TT
10786dwarf2_compute_name (const char *name,
10787 struct die_info *die, struct dwarf2_cu *cu,
94af9270
KS
10788 int physname)
10789{
bb5ed363
DE
10790 struct objfile *objfile = cu->objfile;
10791
94af9270
KS
10792 if (name == NULL)
10793 name = dwarf2_name (die, cu);
10794
2ee7123e
DE
10795 /* For Fortran GDB prefers DW_AT_*linkage_name for the physname if present
10796 but otherwise compute it by typename_concat inside GDB.
10797 FIXME: Actually this is not really true, or at least not always true.
10798 It's all very confusing. SYMBOL_SET_NAMES doesn't try to demangle
10799 Fortran names because there is no mangling standard. So new_symbol_full
10800 will set the demangled name to the result of dwarf2_full_name, and it is
10801 the demangled name that GDB uses if it exists. */
f55ee35c
JK
10802 if (cu->language == language_ada
10803 || (cu->language == language_fortran && physname))
10804 {
10805 /* For Ada unit, we prefer the linkage name over the name, as
10806 the former contains the exported name, which the user expects
10807 to be able to reference. Ideally, we want the user to be able
10808 to reference this entity using either natural or linkage name,
10809 but we haven't started looking at this enhancement yet. */
73b9be8b 10810 const char *linkage_name = dw2_linkage_name (die, cu);
f55ee35c 10811
2ee7123e
DE
10812 if (linkage_name != NULL)
10813 return linkage_name;
f55ee35c
JK
10814 }
10815
94af9270
KS
10816 /* These are the only languages we know how to qualify names in. */
10817 if (name != NULL
9c37b5ae 10818 && (cu->language == language_cplus
c44af4eb
TT
10819 || cu->language == language_fortran || cu->language == language_d
10820 || cu->language == language_rust))
94af9270
KS
10821 {
10822 if (die_needs_namespace (die, cu))
10823 {
0d5cff50 10824 const char *prefix;
34a68019 10825 const char *canonical_name = NULL;
94af9270 10826
d7e74731
PA
10827 string_file buf;
10828
94af9270 10829 prefix = determine_prefix (die, cu);
94af9270
KS
10830 if (*prefix != '\0')
10831 {
f55ee35c
JK
10832 char *prefixed_name = typename_concat (NULL, prefix, name,
10833 physname, cu);
9a619af0 10834
d7e74731 10835 buf.puts (prefixed_name);
94af9270
KS
10836 xfree (prefixed_name);
10837 }
10838 else
d7e74731 10839 buf.puts (name);
94af9270 10840
98bfdba5
PA
10841 /* Template parameters may be specified in the DIE's DW_AT_name, or
10842 as children with DW_TAG_template_type_param or
10843 DW_TAG_value_type_param. If the latter, add them to the name
10844 here. If the name already has template parameters, then
10845 skip this step; some versions of GCC emit both, and
10846 it is more efficient to use the pre-computed name.
10847
10848 Something to keep in mind about this process: it is very
10849 unlikely, or in some cases downright impossible, to produce
10850 something that will match the mangled name of a function.
10851 If the definition of the function has the same debug info,
10852 we should be able to match up with it anyway. But fallbacks
10853 using the minimal symbol, for instance to find a method
10854 implemented in a stripped copy of libstdc++, will not work.
10855 If we do not have debug info for the definition, we will have to
10856 match them up some other way.
10857
10858 When we do name matching there is a related problem with function
10859 templates; two instantiated function templates are allowed to
10860 differ only by their return types, which we do not add here. */
10861
10862 if (cu->language == language_cplus && strchr (name, '<') == NULL)
10863 {
10864 struct attribute *attr;
10865 struct die_info *child;
10866 int first = 1;
10867
10868 die->building_fullname = 1;
10869
10870 for (child = die->child; child != NULL; child = child->sibling)
10871 {
10872 struct type *type;
12df843f 10873 LONGEST value;
d521ce57 10874 const gdb_byte *bytes;
98bfdba5
PA
10875 struct dwarf2_locexpr_baton *baton;
10876 struct value *v;
10877
10878 if (child->tag != DW_TAG_template_type_param
10879 && child->tag != DW_TAG_template_value_param)
10880 continue;
10881
10882 if (first)
10883 {
d7e74731 10884 buf.puts ("<");
98bfdba5
PA
10885 first = 0;
10886 }
10887 else
d7e74731 10888 buf.puts (", ");
98bfdba5
PA
10889
10890 attr = dwarf2_attr (child, DW_AT_type, cu);
10891 if (attr == NULL)
10892 {
10893 complaint (&symfile_complaints,
10894 _("template parameter missing DW_AT_type"));
d7e74731 10895 buf.puts ("UNKNOWN_TYPE");
98bfdba5
PA
10896 continue;
10897 }
10898 type = die_type (child, cu);
10899
10900 if (child->tag == DW_TAG_template_type_param)
10901 {
d7e74731 10902 c_print_type (type, "", &buf, -1, 0, &type_print_raw_options);
98bfdba5
PA
10903 continue;
10904 }
10905
10906 attr = dwarf2_attr (child, DW_AT_const_value, cu);
10907 if (attr == NULL)
10908 {
10909 complaint (&symfile_complaints,
3e43a32a
MS
10910 _("template parameter missing "
10911 "DW_AT_const_value"));
d7e74731 10912 buf.puts ("UNKNOWN_VALUE");
98bfdba5
PA
10913 continue;
10914 }
10915
10916 dwarf2_const_value_attr (attr, type, name,
10917 &cu->comp_unit_obstack, cu,
10918 &value, &bytes, &baton);
10919
10920 if (TYPE_NOSIGN (type))
10921 /* GDB prints characters as NUMBER 'CHAR'. If that's
10922 changed, this can use value_print instead. */
d7e74731 10923 c_printchar (value, type, &buf);
98bfdba5
PA
10924 else
10925 {
10926 struct value_print_options opts;
10927
10928 if (baton != NULL)
10929 v = dwarf2_evaluate_loc_desc (type, NULL,
10930 baton->data,
10931 baton->size,
10932 baton->per_cu);
10933 else if (bytes != NULL)
10934 {
10935 v = allocate_value (type);
10936 memcpy (value_contents_writeable (v), bytes,
10937 TYPE_LENGTH (type));
10938 }
10939 else
10940 v = value_from_longest (type, value);
10941
3e43a32a
MS
10942 /* Specify decimal so that we do not depend on
10943 the radix. */
98bfdba5
PA
10944 get_formatted_print_options (&opts, 'd');
10945 opts.raw = 1;
d7e74731 10946 value_print (v, &buf, &opts);
98bfdba5
PA
10947 release_value (v);
10948 value_free (v);
10949 }
10950 }
10951
10952 die->building_fullname = 0;
10953
10954 if (!first)
10955 {
10956 /* Close the argument list, with a space if necessary
10957 (nested templates). */
d7e74731
PA
10958 if (!buf.empty () && buf.string ().back () == '>')
10959 buf.puts (" >");
98bfdba5 10960 else
d7e74731 10961 buf.puts (">");
98bfdba5
PA
10962 }
10963 }
10964
9c37b5ae 10965 /* For C++ methods, append formal parameter type
94af9270 10966 information, if PHYSNAME. */
6e70227d 10967
94af9270 10968 if (physname && die->tag == DW_TAG_subprogram
9c37b5ae 10969 && cu->language == language_cplus)
94af9270
KS
10970 {
10971 struct type *type = read_type_die (die, cu);
10972
d7e74731 10973 c_type_print_args (type, &buf, 1, cu->language,
79d43c61 10974 &type_print_raw_options);
94af9270 10975
9c37b5ae 10976 if (cu->language == language_cplus)
94af9270 10977 {
60430eff
DJ
10978 /* Assume that an artificial first parameter is
10979 "this", but do not crash if it is not. RealView
10980 marks unnamed (and thus unused) parameters as
10981 artificial; there is no way to differentiate
10982 the two cases. */
94af9270
KS
10983 if (TYPE_NFIELDS (type) > 0
10984 && TYPE_FIELD_ARTIFICIAL (type, 0)
60430eff 10985 && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
3e43a32a
MS
10986 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
10987 0))))
d7e74731 10988 buf.puts (" const");
94af9270
KS
10989 }
10990 }
10991
d7e74731 10992 const std::string &intermediate_name = buf.string ();
94af9270
KS
10993
10994 if (cu->language == language_cplus)
34a68019 10995 canonical_name
322a8516 10996 = dwarf2_canonicalize_name (intermediate_name.c_str (), cu,
34a68019
TT
10997 &objfile->per_bfd->storage_obstack);
10998
10999 /* If we only computed INTERMEDIATE_NAME, or if
11000 INTERMEDIATE_NAME is already canonical, then we need to
11001 copy it to the appropriate obstack. */
322a8516 11002 if (canonical_name == NULL || canonical_name == intermediate_name.c_str ())
224c3ddb
SM
11003 name = ((const char *)
11004 obstack_copy0 (&objfile->per_bfd->storage_obstack,
322a8516
PA
11005 intermediate_name.c_str (),
11006 intermediate_name.length ()));
34a68019
TT
11007 else
11008 name = canonical_name;
94af9270
KS
11009 }
11010 }
11011
11012 return name;
11013}
11014
0114d602
DJ
11015/* Return the fully qualified name of DIE, based on its DW_AT_name.
11016 If scope qualifiers are appropriate they will be added. The result
34a68019 11017 will be allocated on the storage_obstack, or NULL if the DIE does
94af9270
KS
11018 not have a name. NAME may either be from a previous call to
11019 dwarf2_name or NULL.
11020
9c37b5ae 11021 The output string will be canonicalized (if C++). */
0114d602
DJ
11022
11023static const char *
15d034d0 11024dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
0114d602 11025{
94af9270
KS
11026 return dwarf2_compute_name (name, die, cu, 0);
11027}
0114d602 11028
94af9270
KS
11029/* Construct a physname for the given DIE in CU. NAME may either be
11030 from a previous call to dwarf2_name or NULL. The result will be
11031 allocated on the objfile_objstack or NULL if the DIE does not have a
11032 name.
0114d602 11033
9c37b5ae 11034 The output string will be canonicalized (if C++). */
0114d602 11035
94af9270 11036static const char *
15d034d0 11037dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
94af9270 11038{
bb5ed363 11039 struct objfile *objfile = cu->objfile;
900e11f9 11040 const char *retval, *mangled = NULL, *canon = NULL;
900e11f9
JK
11041 int need_copy = 1;
11042
11043 /* In this case dwarf2_compute_name is just a shortcut not building anything
11044 on its own. */
11045 if (!die_needs_namespace (die, cu))
11046 return dwarf2_compute_name (name, die, cu, 1);
11047
73b9be8b 11048 mangled = dw2_linkage_name (die, cu);
900e11f9 11049
e98c9e7c
TT
11050 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
11051 See https://github.com/rust-lang/rust/issues/32925. */
11052 if (cu->language == language_rust && mangled != NULL
11053 && strchr (mangled, '{') != NULL)
11054 mangled = NULL;
11055
900e11f9
JK
11056 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
11057 has computed. */
791afaa2 11058 gdb::unique_xmalloc_ptr<char> demangled;
7d45c7c3 11059 if (mangled != NULL)
900e11f9 11060 {
900e11f9
JK
11061 /* Use DMGL_RET_DROP for C++ template functions to suppress their return
11062 type. It is easier for GDB users to search for such functions as
11063 `name(params)' than `long name(params)'. In such case the minimal
11064 symbol names do not match the full symbol names but for template
11065 functions there is never a need to look up their definition from their
11066 declaration so the only disadvantage remains the minimal symbol
11067 variant `long name(params)' does not have the proper inferior type.
11068 */
11069
a766d390
DE
11070 if (cu->language == language_go)
11071 {
11072 /* This is a lie, but we already lie to the caller new_symbol_full.
11073 new_symbol_full assumes we return the mangled name.
11074 This just undoes that lie until things are cleaned up. */
a766d390
DE
11075 }
11076 else
11077 {
791afaa2
TT
11078 demangled.reset (gdb_demangle (mangled,
11079 (DMGL_PARAMS | DMGL_ANSI
11080 | DMGL_RET_DROP)));
a766d390 11081 }
900e11f9 11082 if (demangled)
791afaa2 11083 canon = demangled.get ();
900e11f9
JK
11084 else
11085 {
11086 canon = mangled;
11087 need_copy = 0;
11088 }
11089 }
11090
11091 if (canon == NULL || check_physname)
11092 {
11093 const char *physname = dwarf2_compute_name (name, die, cu, 1);
11094
11095 if (canon != NULL && strcmp (physname, canon) != 0)
11096 {
11097 /* It may not mean a bug in GDB. The compiler could also
11098 compute DW_AT_linkage_name incorrectly. But in such case
11099 GDB would need to be bug-to-bug compatible. */
11100
11101 complaint (&symfile_complaints,
11102 _("Computed physname <%s> does not match demangled <%s> "
11103 "(from linkage <%s>) - DIE at 0x%x [in module %s]"),
9c541725 11104 physname, canon, mangled, to_underlying (die->sect_off),
4262abfb 11105 objfile_name (objfile));
900e11f9
JK
11106
11107 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
11108 is available here - over computed PHYSNAME. It is safer
11109 against both buggy GDB and buggy compilers. */
11110
11111 retval = canon;
11112 }
11113 else
11114 {
11115 retval = physname;
11116 need_copy = 0;
11117 }
11118 }
11119 else
11120 retval = canon;
11121
11122 if (need_copy)
224c3ddb
SM
11123 retval = ((const char *)
11124 obstack_copy0 (&objfile->per_bfd->storage_obstack,
11125 retval, strlen (retval)));
900e11f9 11126
900e11f9 11127 return retval;
0114d602
DJ
11128}
11129
74921315
KS
11130/* Inspect DIE in CU for a namespace alias. If one exists, record
11131 a new symbol for it.
11132
11133 Returns 1 if a namespace alias was recorded, 0 otherwise. */
11134
11135static int
11136read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu)
11137{
11138 struct attribute *attr;
11139
11140 /* If the die does not have a name, this is not a namespace
11141 alias. */
11142 attr = dwarf2_attr (die, DW_AT_name, cu);
11143 if (attr != NULL)
11144 {
11145 int num;
11146 struct die_info *d = die;
11147 struct dwarf2_cu *imported_cu = cu;
11148
11149 /* If the compiler has nested DW_AT_imported_declaration DIEs,
11150 keep inspecting DIEs until we hit the underlying import. */
11151#define MAX_NESTED_IMPORTED_DECLARATIONS 100
11152 for (num = 0; num < MAX_NESTED_IMPORTED_DECLARATIONS; ++num)
11153 {
11154 attr = dwarf2_attr (d, DW_AT_import, cu);
11155 if (attr == NULL)
11156 break;
11157
11158 d = follow_die_ref (d, attr, &imported_cu);
11159 if (d->tag != DW_TAG_imported_declaration)
11160 break;
11161 }
11162
11163 if (num == MAX_NESTED_IMPORTED_DECLARATIONS)
11164 {
11165 complaint (&symfile_complaints,
11166 _("DIE at 0x%x has too many recursively imported "
9c541725 11167 "declarations"), to_underlying (d->sect_off));
74921315
KS
11168 return 0;
11169 }
11170
11171 if (attr != NULL)
11172 {
11173 struct type *type;
9c541725 11174 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
74921315 11175
9c541725 11176 type = get_die_type_at_offset (sect_off, cu->per_cu);
74921315
KS
11177 if (type != NULL && TYPE_CODE (type) == TYPE_CODE_NAMESPACE)
11178 {
11179 /* This declaration is a global namespace alias. Add
11180 a symbol for it whose type is the aliased namespace. */
11181 new_symbol (die, type, cu);
11182 return 1;
11183 }
11184 }
11185 }
11186
11187 return 0;
11188}
11189
22cee43f
PMR
11190/* Return the using directives repository (global or local?) to use in the
11191 current context for LANGUAGE.
11192
11193 For Ada, imported declarations can materialize renamings, which *may* be
11194 global. However it is impossible (for now?) in DWARF to distinguish
11195 "external" imported declarations and "static" ones. As all imported
11196 declarations seem to be static in all other languages, make them all CU-wide
11197 global only in Ada. */
11198
11199static struct using_direct **
11200using_directives (enum language language)
11201{
11202 if (language == language_ada && context_stack_depth == 0)
11203 return &global_using_directives;
11204 else
11205 return &local_using_directives;
11206}
11207
27aa8d6a
SW
11208/* Read the import statement specified by the given die and record it. */
11209
11210static void
11211read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
11212{
bb5ed363 11213 struct objfile *objfile = cu->objfile;
27aa8d6a 11214 struct attribute *import_attr;
32019081 11215 struct die_info *imported_die, *child_die;
de4affc9 11216 struct dwarf2_cu *imported_cu;
27aa8d6a 11217 const char *imported_name;
794684b6 11218 const char *imported_name_prefix;
13387711
SW
11219 const char *canonical_name;
11220 const char *import_alias;
11221 const char *imported_declaration = NULL;
794684b6 11222 const char *import_prefix;
eb1e02fd 11223 std::vector<const char *> excludes;
13387711 11224
27aa8d6a
SW
11225 import_attr = dwarf2_attr (die, DW_AT_import, cu);
11226 if (import_attr == NULL)
11227 {
11228 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
11229 dwarf_tag_name (die->tag));
11230 return;
11231 }
11232
de4affc9
CC
11233 imported_cu = cu;
11234 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
11235 imported_name = dwarf2_name (imported_die, imported_cu);
27aa8d6a
SW
11236 if (imported_name == NULL)
11237 {
11238 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
11239
11240 The import in the following code:
11241 namespace A
11242 {
11243 typedef int B;
11244 }
11245
11246 int main ()
11247 {
11248 using A::B;
11249 B b;
11250 return b;
11251 }
11252
11253 ...
11254 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
11255 <52> DW_AT_decl_file : 1
11256 <53> DW_AT_decl_line : 6
11257 <54> DW_AT_import : <0x75>
11258 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
11259 <59> DW_AT_name : B
11260 <5b> DW_AT_decl_file : 1
11261 <5c> DW_AT_decl_line : 2
11262 <5d> DW_AT_type : <0x6e>
11263 ...
11264 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
11265 <76> DW_AT_byte_size : 4
11266 <77> DW_AT_encoding : 5 (signed)
11267
11268 imports the wrong die ( 0x75 instead of 0x58 ).
11269 This case will be ignored until the gcc bug is fixed. */
11270 return;
11271 }
11272
82856980
SW
11273 /* Figure out the local name after import. */
11274 import_alias = dwarf2_name (die, cu);
27aa8d6a 11275
794684b6
SW
11276 /* Figure out where the statement is being imported to. */
11277 import_prefix = determine_prefix (die, cu);
11278
11279 /* Figure out what the scope of the imported die is and prepend it
11280 to the name of the imported die. */
de4affc9 11281 imported_name_prefix = determine_prefix (imported_die, imported_cu);
794684b6 11282
f55ee35c
JK
11283 if (imported_die->tag != DW_TAG_namespace
11284 && imported_die->tag != DW_TAG_module)
794684b6 11285 {
13387711
SW
11286 imported_declaration = imported_name;
11287 canonical_name = imported_name_prefix;
794684b6 11288 }
13387711 11289 else if (strlen (imported_name_prefix) > 0)
12aaed36 11290 canonical_name = obconcat (&objfile->objfile_obstack,
45280282
IB
11291 imported_name_prefix,
11292 (cu->language == language_d ? "." : "::"),
11293 imported_name, (char *) NULL);
13387711
SW
11294 else
11295 canonical_name = imported_name;
794684b6 11296
32019081
JK
11297 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
11298 for (child_die = die->child; child_die && child_die->tag;
11299 child_die = sibling_die (child_die))
11300 {
11301 /* DWARF-4: A Fortran use statement with a “rename list” may be
11302 represented by an imported module entry with an import attribute
11303 referring to the module and owned entries corresponding to those
11304 entities that are renamed as part of being imported. */
11305
11306 if (child_die->tag != DW_TAG_imported_declaration)
11307 {
11308 complaint (&symfile_complaints,
11309 _("child DW_TAG_imported_declaration expected "
11310 "- DIE at 0x%x [in module %s]"),
9c541725 11311 to_underlying (child_die->sect_off), objfile_name (objfile));
32019081
JK
11312 continue;
11313 }
11314
11315 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
11316 if (import_attr == NULL)
11317 {
11318 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
11319 dwarf_tag_name (child_die->tag));
11320 continue;
11321 }
11322
11323 imported_cu = cu;
11324 imported_die = follow_die_ref_or_sig (child_die, import_attr,
11325 &imported_cu);
11326 imported_name = dwarf2_name (imported_die, imported_cu);
11327 if (imported_name == NULL)
11328 {
11329 complaint (&symfile_complaints,
11330 _("child DW_TAG_imported_declaration has unknown "
11331 "imported name - DIE at 0x%x [in module %s]"),
9c541725 11332 to_underlying (child_die->sect_off), objfile_name (objfile));
32019081
JK
11333 continue;
11334 }
11335
eb1e02fd 11336 excludes.push_back (imported_name);
32019081
JK
11337
11338 process_die (child_die, cu);
11339 }
11340
22cee43f
PMR
11341 add_using_directive (using_directives (cu->language),
11342 import_prefix,
11343 canonical_name,
11344 import_alias,
11345 imported_declaration,
11346 excludes,
11347 0,
11348 &objfile->objfile_obstack);
27aa8d6a
SW
11349}
11350
5230b05a
WT
11351/* ICC<14 does not output the required DW_AT_declaration on incomplete
11352 types, but gives them a size of zero. Starting with version 14,
11353 ICC is compatible with GCC. */
11354
11355static int
11356producer_is_icc_lt_14 (struct dwarf2_cu *cu)
11357{
11358 if (!cu->checked_producer)
11359 check_producer (cu);
11360
11361 return cu->producer_is_icc_lt_14;
11362}
11363
1b80a9fa
JK
11364/* Check for possibly missing DW_AT_comp_dir with relative .debug_line
11365 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
11366 this, it was first present in GCC release 4.3.0. */
11367
11368static int
11369producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
11370{
11371 if (!cu->checked_producer)
11372 check_producer (cu);
11373
11374 return cu->producer_is_gcc_lt_4_3;
11375}
11376
d721ba37
PA
11377static file_and_directory
11378find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu)
9291a0cd 11379{
d721ba37
PA
11380 file_and_directory res;
11381
9291a0cd
TT
11382 /* Find the filename. Do not use dwarf2_name here, since the filename
11383 is not a source language identifier. */
d721ba37
PA
11384 res.name = dwarf2_string_attr (die, DW_AT_name, cu);
11385 res.comp_dir = dwarf2_string_attr (die, DW_AT_comp_dir, cu);
9291a0cd 11386
d721ba37
PA
11387 if (res.comp_dir == NULL
11388 && producer_is_gcc_lt_4_3 (cu) && res.name != NULL
11389 && IS_ABSOLUTE_PATH (res.name))
9291a0cd 11390 {
d721ba37
PA
11391 res.comp_dir_storage = ldirname (res.name);
11392 if (!res.comp_dir_storage.empty ())
11393 res.comp_dir = res.comp_dir_storage.c_str ();
9291a0cd 11394 }
d721ba37 11395 if (res.comp_dir != NULL)
9291a0cd
TT
11396 {
11397 /* Irix 6.2 native cc prepends <machine>.: to the compilation
11398 directory, get rid of it. */
d721ba37 11399 const char *cp = strchr (res.comp_dir, ':');
9291a0cd 11400
d721ba37
PA
11401 if (cp && cp != res.comp_dir && cp[-1] == '.' && cp[1] == '/')
11402 res.comp_dir = cp + 1;
9291a0cd
TT
11403 }
11404
d721ba37
PA
11405 if (res.name == NULL)
11406 res.name = "<unknown>";
11407
11408 return res;
9291a0cd
TT
11409}
11410
f4dc4d17
DE
11411/* Handle DW_AT_stmt_list for a compilation unit.
11412 DIE is the DW_TAG_compile_unit die for CU.
c3b7b696
YQ
11413 COMP_DIR is the compilation directory. LOWPC is passed to
11414 dwarf_decode_lines. See dwarf_decode_lines comments about it. */
2ab95328
TT
11415
11416static void
11417handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
c3b7b696 11418 const char *comp_dir, CORE_ADDR lowpc) /* ARI: editCase function */
2ab95328 11419{
527f3840 11420 struct objfile *objfile = dwarf2_per_objfile->objfile;
2ab95328 11421 struct attribute *attr;
527f3840
JK
11422 struct line_header line_header_local;
11423 hashval_t line_header_local_hash;
527f3840
JK
11424 void **slot;
11425 int decode_mapping;
2ab95328 11426
f4dc4d17
DE
11427 gdb_assert (! cu->per_cu->is_debug_types);
11428
2ab95328 11429 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
527f3840
JK
11430 if (attr == NULL)
11431 return;
11432
9c541725 11433 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
527f3840
JK
11434
11435 /* The line header hash table is only created if needed (it exists to
11436 prevent redundant reading of the line table for partial_units).
11437 If we're given a partial_unit, we'll need it. If we're given a
11438 compile_unit, then use the line header hash table if it's already
11439 created, but don't create one just yet. */
11440
11441 if (dwarf2_per_objfile->line_header_hash == NULL
11442 && die->tag == DW_TAG_partial_unit)
2ab95328 11443 {
527f3840
JK
11444 dwarf2_per_objfile->line_header_hash
11445 = htab_create_alloc_ex (127, line_header_hash_voidp,
11446 line_header_eq_voidp,
11447 free_line_header_voidp,
11448 &objfile->objfile_obstack,
11449 hashtab_obstack_allocate,
11450 dummy_obstack_deallocate);
11451 }
2ab95328 11452
9c541725 11453 line_header_local.sect_off = line_offset;
527f3840
JK
11454 line_header_local.offset_in_dwz = cu->per_cu->is_dwz;
11455 line_header_local_hash = line_header_hash (&line_header_local);
11456 if (dwarf2_per_objfile->line_header_hash != NULL)
11457 {
11458 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash,
11459 &line_header_local,
11460 line_header_local_hash, NO_INSERT);
11461
11462 /* For DW_TAG_compile_unit we need info like symtab::linetable which
11463 is not present in *SLOT (since if there is something in *SLOT then
11464 it will be for a partial_unit). */
11465 if (die->tag == DW_TAG_partial_unit && slot != NULL)
dee91e82 11466 {
527f3840 11467 gdb_assert (*slot != NULL);
9a3c8263 11468 cu->line_header = (struct line_header *) *slot;
527f3840 11469 return;
dee91e82 11470 }
2ab95328 11471 }
527f3840
JK
11472
11473 /* dwarf_decode_line_header does not yet provide sufficient information.
11474 We always have to call also dwarf_decode_lines for it. */
fff8551c
PA
11475 line_header_up lh = dwarf_decode_line_header (line_offset, cu);
11476 if (lh == NULL)
527f3840 11477 return;
4c8aa72d
PA
11478
11479 cu->line_header = lh.release ();
11480 cu->line_header_die_owner = die;
527f3840
JK
11481
11482 if (dwarf2_per_objfile->line_header_hash == NULL)
11483 slot = NULL;
11484 else
11485 {
11486 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash,
11487 &line_header_local,
11488 line_header_local_hash, INSERT);
11489 gdb_assert (slot != NULL);
11490 }
11491 if (slot != NULL && *slot == NULL)
11492 {
11493 /* This newly decoded line number information unit will be owned
11494 by line_header_hash hash table. */
11495 *slot = cu->line_header;
4c8aa72d 11496 cu->line_header_die_owner = NULL;
527f3840
JK
11497 }
11498 else
11499 {
11500 /* We cannot free any current entry in (*slot) as that struct line_header
11501 may be already used by multiple CUs. Create only temporary decoded
11502 line_header for this CU - it may happen at most once for each line
11503 number information unit. And if we're not using line_header_hash
11504 then this is what we want as well. */
11505 gdb_assert (die->tag != DW_TAG_partial_unit);
527f3840
JK
11506 }
11507 decode_mapping = (die->tag != DW_TAG_partial_unit);
11508 dwarf_decode_lines (cu->line_header, comp_dir, cu, NULL, lowpc,
11509 decode_mapping);
fff8551c 11510
2ab95328
TT
11511}
11512
95554aad 11513/* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
ae2de4f8 11514
c906108c 11515static void
e7c27a73 11516read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 11517{
dee91e82 11518 struct objfile *objfile = dwarf2_per_objfile->objfile;
3e29f34a 11519 struct gdbarch *gdbarch = get_objfile_arch (objfile);
2acceee2 11520 CORE_ADDR lowpc = ((CORE_ADDR) -1);
c906108c
SS
11521 CORE_ADDR highpc = ((CORE_ADDR) 0);
11522 struct attribute *attr;
c906108c 11523 struct die_info *child_die;
e142c38c 11524 CORE_ADDR baseaddr;
6e70227d 11525
e142c38c 11526 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 11527
fae299cd 11528 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
c906108c
SS
11529
11530 /* If we didn't find a lowpc, set it to highpc to avoid complaints
11531 from finish_block. */
2acceee2 11532 if (lowpc == ((CORE_ADDR) -1))
c906108c 11533 lowpc = highpc;
3e29f34a 11534 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
c906108c 11535
d721ba37 11536 file_and_directory fnd = find_file_and_directory (die, cu);
e1024ff1 11537
95554aad 11538 prepare_one_comp_unit (cu, die, cu->language);
303b6f5d 11539
f4b8a18d
KW
11540 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
11541 standardised yet. As a workaround for the language detection we fall
11542 back to the DW_AT_producer string. */
11543 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
11544 cu->language = language_opencl;
11545
3019eac3
DE
11546 /* Similar hack for Go. */
11547 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
11548 set_cu_language (DW_LANG_Go, cu);
11549
d721ba37 11550 dwarf2_start_symtab (cu, fnd.name, fnd.comp_dir, lowpc);
3019eac3
DE
11551
11552 /* Decode line number information if present. We do this before
11553 processing child DIEs, so that the line header table is available
11554 for DW_AT_decl_file. */
d721ba37 11555 handle_DW_AT_stmt_list (die, cu, fnd.comp_dir, lowpc);
3019eac3
DE
11556
11557 /* Process all dies in compilation unit. */
11558 if (die->child != NULL)
11559 {
11560 child_die = die->child;
11561 while (child_die && child_die->tag)
11562 {
11563 process_die (child_die, cu);
11564 child_die = sibling_die (child_die);
11565 }
11566 }
11567
11568 /* Decode macro information, if present. Dwarf 2 macro information
11569 refers to information in the line number info statement program
11570 header, so we can only read it if we've read the header
11571 successfully. */
0af92d60
JK
11572 attr = dwarf2_attr (die, DW_AT_macros, cu);
11573 if (attr == NULL)
11574 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
3019eac3
DE
11575 if (attr && cu->line_header)
11576 {
11577 if (dwarf2_attr (die, DW_AT_macro_info, cu))
11578 complaint (&symfile_complaints,
0af92d60 11579 _("CU refers to both DW_AT_macros and DW_AT_macro_info"));
3019eac3 11580
43f3e411 11581 dwarf_decode_macros (cu, DW_UNSND (attr), 1);
3019eac3
DE
11582 }
11583 else
11584 {
11585 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
11586 if (attr && cu->line_header)
11587 {
11588 unsigned int macro_offset = DW_UNSND (attr);
11589
43f3e411 11590 dwarf_decode_macros (cu, macro_offset, 0);
3019eac3
DE
11591 }
11592 }
3019eac3
DE
11593}
11594
f4dc4d17
DE
11595/* TU version of handle_DW_AT_stmt_list for read_type_unit_scope.
11596 Create the set of symtabs used by this TU, or if this TU is sharing
11597 symtabs with another TU and the symtabs have already been created
11598 then restore those symtabs in the line header.
11599 We don't need the pc/line-number mapping for type units. */
3019eac3
DE
11600
11601static void
f4dc4d17 11602setup_type_unit_groups (struct die_info *die, struct dwarf2_cu *cu)
3019eac3 11603{
f4dc4d17
DE
11604 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
11605 struct type_unit_group *tu_group;
11606 int first_time;
3019eac3 11607 struct attribute *attr;
9c541725 11608 unsigned int i;
0186c6a7 11609 struct signatured_type *sig_type;
3019eac3 11610
f4dc4d17 11611 gdb_assert (per_cu->is_debug_types);
0186c6a7 11612 sig_type = (struct signatured_type *) per_cu;
3019eac3 11613
f4dc4d17 11614 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
3019eac3 11615
f4dc4d17 11616 /* If we're using .gdb_index (includes -readnow) then
74e04d1c 11617 per_cu->type_unit_group may not have been set up yet. */
0186c6a7
DE
11618 if (sig_type->type_unit_group == NULL)
11619 sig_type->type_unit_group = get_type_unit_group (cu, attr);
11620 tu_group = sig_type->type_unit_group;
f4dc4d17
DE
11621
11622 /* If we've already processed this stmt_list there's no real need to
11623 do it again, we could fake it and just recreate the part we need
11624 (file name,index -> symtab mapping). If data shows this optimization
11625 is useful we can do it then. */
43f3e411 11626 first_time = tu_group->compunit_symtab == NULL;
f4dc4d17
DE
11627
11628 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
11629 debug info. */
fff8551c 11630 line_header_up lh;
f4dc4d17 11631 if (attr != NULL)
3019eac3 11632 {
9c541725 11633 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
f4dc4d17
DE
11634 lh = dwarf_decode_line_header (line_offset, cu);
11635 }
11636 if (lh == NULL)
11637 {
11638 if (first_time)
11639 dwarf2_start_symtab (cu, "", NULL, 0);
11640 else
11641 {
11642 gdb_assert (tu_group->symtabs == NULL);
0ab9ce85 11643 restart_symtab (tu_group->compunit_symtab, "", 0);
f4dc4d17 11644 }
f4dc4d17 11645 return;
3019eac3
DE
11646 }
11647
4c8aa72d
PA
11648 cu->line_header = lh.release ();
11649 cu->line_header_die_owner = die;
3019eac3 11650
f4dc4d17
DE
11651 if (first_time)
11652 {
43f3e411 11653 struct compunit_symtab *cust = dwarf2_start_symtab (cu, "", NULL, 0);
3019eac3 11654
1fd60fc0
DE
11655 /* Note: We don't assign tu_group->compunit_symtab yet because we're
11656 still initializing it, and our caller (a few levels up)
11657 process_full_type_unit still needs to know if this is the first
11658 time. */
11659
4c8aa72d
PA
11660 tu_group->num_symtabs = cu->line_header->file_names.size ();
11661 tu_group->symtabs = XNEWVEC (struct symtab *,
11662 cu->line_header->file_names.size ());
3019eac3 11663
4c8aa72d 11664 for (i = 0; i < cu->line_header->file_names.size (); ++i)
f4dc4d17 11665 {
4c8aa72d 11666 file_entry &fe = cu->line_header->file_names[i];
3019eac3 11667
4c8aa72d 11668 dwarf2_start_subfile (fe.name, fe.include_dir (cu->line_header));
3019eac3 11669
f4dc4d17
DE
11670 if (current_subfile->symtab == NULL)
11671 {
4c8aa72d
PA
11672 /* NOTE: start_subfile will recognize when it's been
11673 passed a file it has already seen. So we can't
11674 assume there's a simple mapping from
11675 cu->line_header->file_names to subfiles, plus
11676 cu->line_header->file_names may contain dups. */
43f3e411
DE
11677 current_subfile->symtab
11678 = allocate_symtab (cust, current_subfile->name);
f4dc4d17
DE
11679 }
11680
8c43009f
PA
11681 fe.symtab = current_subfile->symtab;
11682 tu_group->symtabs[i] = fe.symtab;
f4dc4d17
DE
11683 }
11684 }
11685 else
3019eac3 11686 {
0ab9ce85 11687 restart_symtab (tu_group->compunit_symtab, "", 0);
f4dc4d17 11688
4c8aa72d 11689 for (i = 0; i < cu->line_header->file_names.size (); ++i)
f4dc4d17 11690 {
4c8aa72d 11691 file_entry &fe = cu->line_header->file_names[i];
f4dc4d17 11692
4c8aa72d 11693 fe.symtab = tu_group->symtabs[i];
f4dc4d17 11694 }
3019eac3
DE
11695 }
11696
f4dc4d17
DE
11697 /* The main symtab is allocated last. Type units don't have DW_AT_name
11698 so they don't have a "real" (so to speak) symtab anyway.
11699 There is later code that will assign the main symtab to all symbols
11700 that don't have one. We need to handle the case of a symbol with a
11701 missing symtab (DW_AT_decl_file) anyway. */
11702}
3019eac3 11703
f4dc4d17
DE
11704/* Process DW_TAG_type_unit.
11705 For TUs we want to skip the first top level sibling if it's not the
11706 actual type being defined by this TU. In this case the first top
11707 level sibling is there to provide context only. */
3019eac3 11708
f4dc4d17
DE
11709static void
11710read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
11711{
11712 struct die_info *child_die;
3019eac3 11713
f4dc4d17
DE
11714 prepare_one_comp_unit (cu, die, language_minimal);
11715
11716 /* Initialize (or reinitialize) the machinery for building symtabs.
11717 We do this before processing child DIEs, so that the line header table
11718 is available for DW_AT_decl_file. */
11719 setup_type_unit_groups (die, cu);
11720
11721 if (die->child != NULL)
11722 {
11723 child_die = die->child;
11724 while (child_die && child_die->tag)
11725 {
11726 process_die (child_die, cu);
11727 child_die = sibling_die (child_die);
11728 }
11729 }
3019eac3
DE
11730}
11731\f
80626a55
DE
11732/* DWO/DWP files.
11733
11734 http://gcc.gnu.org/wiki/DebugFission
11735 http://gcc.gnu.org/wiki/DebugFissionDWP
11736
11737 To simplify handling of both DWO files ("object" files with the DWARF info)
11738 and DWP files (a file with the DWOs packaged up into one file), we treat
11739 DWP files as having a collection of virtual DWO files. */
3019eac3
DE
11740
11741static hashval_t
11742hash_dwo_file (const void *item)
11743{
9a3c8263 11744 const struct dwo_file *dwo_file = (const struct dwo_file *) item;
a2ce51a0 11745 hashval_t hash;
3019eac3 11746
a2ce51a0
DE
11747 hash = htab_hash_string (dwo_file->dwo_name);
11748 if (dwo_file->comp_dir != NULL)
11749 hash += htab_hash_string (dwo_file->comp_dir);
11750 return hash;
3019eac3
DE
11751}
11752
11753static int
11754eq_dwo_file (const void *item_lhs, const void *item_rhs)
11755{
9a3c8263
SM
11756 const struct dwo_file *lhs = (const struct dwo_file *) item_lhs;
11757 const struct dwo_file *rhs = (const struct dwo_file *) item_rhs;
3019eac3 11758
a2ce51a0
DE
11759 if (strcmp (lhs->dwo_name, rhs->dwo_name) != 0)
11760 return 0;
11761 if (lhs->comp_dir == NULL || rhs->comp_dir == NULL)
11762 return lhs->comp_dir == rhs->comp_dir;
11763 return strcmp (lhs->comp_dir, rhs->comp_dir) == 0;
3019eac3
DE
11764}
11765
11766/* Allocate a hash table for DWO files. */
11767
11768static htab_t
11769allocate_dwo_file_hash_table (void)
11770{
11771 struct objfile *objfile = dwarf2_per_objfile->objfile;
11772
11773 return htab_create_alloc_ex (41,
11774 hash_dwo_file,
11775 eq_dwo_file,
11776 NULL,
11777 &objfile->objfile_obstack,
11778 hashtab_obstack_allocate,
11779 dummy_obstack_deallocate);
11780}
11781
80626a55
DE
11782/* Lookup DWO file DWO_NAME. */
11783
11784static void **
0ac5b59e 11785lookup_dwo_file_slot (const char *dwo_name, const char *comp_dir)
80626a55
DE
11786{
11787 struct dwo_file find_entry;
11788 void **slot;
11789
11790 if (dwarf2_per_objfile->dwo_files == NULL)
11791 dwarf2_per_objfile->dwo_files = allocate_dwo_file_hash_table ();
11792
11793 memset (&find_entry, 0, sizeof (find_entry));
0ac5b59e
DE
11794 find_entry.dwo_name = dwo_name;
11795 find_entry.comp_dir = comp_dir;
80626a55
DE
11796 slot = htab_find_slot (dwarf2_per_objfile->dwo_files, &find_entry, INSERT);
11797
11798 return slot;
11799}
11800
3019eac3
DE
11801static hashval_t
11802hash_dwo_unit (const void *item)
11803{
9a3c8263 11804 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
3019eac3
DE
11805
11806 /* This drops the top 32 bits of the id, but is ok for a hash. */
11807 return dwo_unit->signature;
11808}
11809
11810static int
11811eq_dwo_unit (const void *item_lhs, const void *item_rhs)
11812{
9a3c8263
SM
11813 const struct dwo_unit *lhs = (const struct dwo_unit *) item_lhs;
11814 const struct dwo_unit *rhs = (const struct dwo_unit *) item_rhs;
3019eac3
DE
11815
11816 /* The signature is assumed to be unique within the DWO file.
11817 So while object file CU dwo_id's always have the value zero,
11818 that's OK, assuming each object file DWO file has only one CU,
11819 and that's the rule for now. */
11820 return lhs->signature == rhs->signature;
11821}
11822
11823/* Allocate a hash table for DWO CUs,TUs.
11824 There is one of these tables for each of CUs,TUs for each DWO file. */
11825
11826static htab_t
11827allocate_dwo_unit_table (struct objfile *objfile)
11828{
11829 /* Start out with a pretty small number.
11830 Generally DWO files contain only one CU and maybe some TUs. */
11831 return htab_create_alloc_ex (3,
11832 hash_dwo_unit,
11833 eq_dwo_unit,
11834 NULL,
11835 &objfile->objfile_obstack,
11836 hashtab_obstack_allocate,
11837 dummy_obstack_deallocate);
11838}
11839
80626a55 11840/* Structure used to pass data to create_dwo_debug_info_hash_table_reader. */
3019eac3 11841
19c3d4c9 11842struct create_dwo_cu_data
3019eac3
DE
11843{
11844 struct dwo_file *dwo_file;
19c3d4c9 11845 struct dwo_unit dwo_unit;
3019eac3
DE
11846};
11847
19c3d4c9 11848/* die_reader_func for create_dwo_cu. */
3019eac3
DE
11849
11850static void
19c3d4c9
DE
11851create_dwo_cu_reader (const struct die_reader_specs *reader,
11852 const gdb_byte *info_ptr,
11853 struct die_info *comp_unit_die,
11854 int has_children,
11855 void *datap)
3019eac3
DE
11856{
11857 struct dwarf2_cu *cu = reader->cu;
9c541725 11858 sect_offset sect_off = cu->per_cu->sect_off;
8a0459fd 11859 struct dwarf2_section_info *section = cu->per_cu->section;
9a3c8263 11860 struct create_dwo_cu_data *data = (struct create_dwo_cu_data *) datap;
3019eac3 11861 struct dwo_file *dwo_file = data->dwo_file;
19c3d4c9 11862 struct dwo_unit *dwo_unit = &data->dwo_unit;
3019eac3 11863 struct attribute *attr;
3019eac3
DE
11864
11865 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
11866 if (attr == NULL)
11867 {
19c3d4c9
DE
11868 complaint (&symfile_complaints,
11869 _("Dwarf Error: debug entry at offset 0x%x is missing"
11870 " its dwo_id [in module %s]"),
9c541725 11871 to_underlying (sect_off), dwo_file->dwo_name);
3019eac3
DE
11872 return;
11873 }
11874
3019eac3
DE
11875 dwo_unit->dwo_file = dwo_file;
11876 dwo_unit->signature = DW_UNSND (attr);
8a0459fd 11877 dwo_unit->section = section;
9c541725 11878 dwo_unit->sect_off = sect_off;
3019eac3
DE
11879 dwo_unit->length = cu->per_cu->length;
11880
b4f54984 11881 if (dwarf_read_debug)
4031ecc5 11882 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, dwo_id %s\n",
9c541725
PA
11883 to_underlying (sect_off),
11884 hex_string (dwo_unit->signature));
3019eac3
DE
11885}
11886
33c5cd75 11887/* Create the dwo_units for the CUs in a DWO_FILE.
19c3d4c9 11888 Note: This function processes DWO files only, not DWP files. */
3019eac3 11889
33c5cd75
DB
11890static void
11891create_cus_hash_table (struct dwo_file &dwo_file, dwarf2_section_info &section,
11892 htab_t &cus_htab)
3019eac3
DE
11893{
11894 struct objfile *objfile = dwarf2_per_objfile->objfile;
d521ce57 11895 const gdb_byte *info_ptr, *end_ptr;
3019eac3 11896
33c5cd75
DB
11897 dwarf2_read_section (objfile, &section);
11898 info_ptr = section.buffer;
3019eac3
DE
11899
11900 if (info_ptr == NULL)
33c5cd75 11901 return;
3019eac3 11902
b4f54984 11903 if (dwarf_read_debug)
19c3d4c9
DE
11904 {
11905 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
33c5cd75
DB
11906 get_section_name (&section),
11907 get_section_file_name (&section));
19c3d4c9 11908 }
3019eac3 11909
33c5cd75 11910 end_ptr = info_ptr + section.size;
3019eac3
DE
11911 while (info_ptr < end_ptr)
11912 {
11913 struct dwarf2_per_cu_data per_cu;
33c5cd75
DB
11914 struct create_dwo_cu_data create_dwo_cu_data;
11915 struct dwo_unit *dwo_unit;
11916 void **slot;
11917 sect_offset sect_off = (sect_offset) (info_ptr - section.buffer);
3019eac3 11918
19c3d4c9
DE
11919 memset (&create_dwo_cu_data.dwo_unit, 0,
11920 sizeof (create_dwo_cu_data.dwo_unit));
3019eac3
DE
11921 memset (&per_cu, 0, sizeof (per_cu));
11922 per_cu.objfile = objfile;
11923 per_cu.is_debug_types = 0;
33c5cd75
DB
11924 per_cu.sect_off = sect_offset (info_ptr - section.buffer);
11925 per_cu.section = &section;
c5ed0576 11926 create_dwo_cu_data.dwo_file = &dwo_file;
33c5cd75
DB
11927
11928 init_cutu_and_read_dies_no_follow (
11929 &per_cu, &dwo_file, create_dwo_cu_reader, &create_dwo_cu_data);
11930 info_ptr += per_cu.length;
11931
11932 // If the unit could not be parsed, skip it.
11933 if (create_dwo_cu_data.dwo_unit.dwo_file == NULL)
11934 continue;
3019eac3 11935
33c5cd75
DB
11936 if (cus_htab == NULL)
11937 cus_htab = allocate_dwo_unit_table (objfile);
19c3d4c9 11938
33c5cd75
DB
11939 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
11940 *dwo_unit = create_dwo_cu_data.dwo_unit;
11941 slot = htab_find_slot (cus_htab, dwo_unit, INSERT);
11942 gdb_assert (slot != NULL);
11943 if (*slot != NULL)
19c3d4c9 11944 {
33c5cd75
DB
11945 const struct dwo_unit *dup_cu = (const struct dwo_unit *)*slot;
11946 sect_offset dup_sect_off = dup_cu->sect_off;
19c3d4c9 11947
33c5cd75
DB
11948 complaint (&symfile_complaints,
11949 _("debug cu entry at offset 0x%x is duplicate to"
11950 " the entry at offset 0x%x, signature %s"),
11951 to_underlying (sect_off), to_underlying (dup_sect_off),
11952 hex_string (dwo_unit->signature));
19c3d4c9 11953 }
33c5cd75 11954 *slot = (void *)dwo_unit;
3019eac3 11955 }
3019eac3
DE
11956}
11957
80626a55
DE
11958/* DWP file .debug_{cu,tu}_index section format:
11959 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
11960
d2415c6c
DE
11961 DWP Version 1:
11962
80626a55
DE
11963 Both index sections have the same format, and serve to map a 64-bit
11964 signature to a set of section numbers. Each section begins with a header,
11965 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
11966 indexes, and a pool of 32-bit section numbers. The index sections will be
11967 aligned at 8-byte boundaries in the file.
11968
d2415c6c
DE
11969 The index section header consists of:
11970
11971 V, 32 bit version number
11972 -, 32 bits unused
11973 N, 32 bit number of compilation units or type units in the index
11974 M, 32 bit number of slots in the hash table
80626a55 11975
d2415c6c 11976 Numbers are recorded using the byte order of the application binary.
80626a55 11977
d2415c6c
DE
11978 The hash table begins at offset 16 in the section, and consists of an array
11979 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
11980 order of the application binary). Unused slots in the hash table are 0.
11981 (We rely on the extreme unlikeliness of a signature being exactly 0.)
80626a55 11982
d2415c6c
DE
11983 The parallel table begins immediately after the hash table
11984 (at offset 16 + 8 * M from the beginning of the section), and consists of an
11985 array of 32-bit indexes (using the byte order of the application binary),
11986 corresponding 1-1 with slots in the hash table. Each entry in the parallel
11987 table contains a 32-bit index into the pool of section numbers. For unused
11988 hash table slots, the corresponding entry in the parallel table will be 0.
80626a55 11989
73869dc2
DE
11990 The pool of section numbers begins immediately following the hash table
11991 (at offset 16 + 12 * M from the beginning of the section). The pool of
11992 section numbers consists of an array of 32-bit words (using the byte order
11993 of the application binary). Each item in the array is indexed starting
11994 from 0. The hash table entry provides the index of the first section
11995 number in the set. Additional section numbers in the set follow, and the
11996 set is terminated by a 0 entry (section number 0 is not used in ELF).
11997
11998 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
11999 section must be the first entry in the set, and the .debug_abbrev.dwo must
12000 be the second entry. Other members of the set may follow in any order.
12001
12002 ---
12003
12004 DWP Version 2:
12005
12006 DWP Version 2 combines all the .debug_info, etc. sections into one,
12007 and the entries in the index tables are now offsets into these sections.
12008 CU offsets begin at 0. TU offsets begin at the size of the .debug_info
12009 section.
12010
12011 Index Section Contents:
12012 Header
12013 Hash Table of Signatures dwp_hash_table.hash_table
12014 Parallel Table of Indices dwp_hash_table.unit_table
12015 Table of Section Offsets dwp_hash_table.v2.{section_ids,offsets}
12016 Table of Section Sizes dwp_hash_table.v2.sizes
12017
12018 The index section header consists of:
12019
12020 V, 32 bit version number
12021 L, 32 bit number of columns in the table of section offsets
12022 N, 32 bit number of compilation units or type units in the index
12023 M, 32 bit number of slots in the hash table
12024
12025 Numbers are recorded using the byte order of the application binary.
12026
12027 The hash table has the same format as version 1.
12028 The parallel table of indices has the same format as version 1,
12029 except that the entries are origin-1 indices into the table of sections
12030 offsets and the table of section sizes.
12031
12032 The table of offsets begins immediately following the parallel table
12033 (at offset 16 + 12 * M from the beginning of the section). The table is
12034 a two-dimensional array of 32-bit words (using the byte order of the
12035 application binary), with L columns and N+1 rows, in row-major order.
12036 Each row in the array is indexed starting from 0. The first row provides
12037 a key to the remaining rows: each column in this row provides an identifier
12038 for a debug section, and the offsets in the same column of subsequent rows
12039 refer to that section. The section identifiers are:
12040
12041 DW_SECT_INFO 1 .debug_info.dwo
12042 DW_SECT_TYPES 2 .debug_types.dwo
12043 DW_SECT_ABBREV 3 .debug_abbrev.dwo
12044 DW_SECT_LINE 4 .debug_line.dwo
12045 DW_SECT_LOC 5 .debug_loc.dwo
12046 DW_SECT_STR_OFFSETS 6 .debug_str_offsets.dwo
12047 DW_SECT_MACINFO 7 .debug_macinfo.dwo
12048 DW_SECT_MACRO 8 .debug_macro.dwo
12049
12050 The offsets provided by the CU and TU index sections are the base offsets
12051 for the contributions made by each CU or TU to the corresponding section
12052 in the package file. Each CU and TU header contains an abbrev_offset
12053 field, used to find the abbreviations table for that CU or TU within the
12054 contribution to the .debug_abbrev.dwo section for that CU or TU, and should
12055 be interpreted as relative to the base offset given in the index section.
12056 Likewise, offsets into .debug_line.dwo from DW_AT_stmt_list attributes
12057 should be interpreted as relative to the base offset for .debug_line.dwo,
12058 and offsets into other debug sections obtained from DWARF attributes should
12059 also be interpreted as relative to the corresponding base offset.
12060
12061 The table of sizes begins immediately following the table of offsets.
12062 Like the table of offsets, it is a two-dimensional array of 32-bit words,
12063 with L columns and N rows, in row-major order. Each row in the array is
12064 indexed starting from 1 (row 0 is shared by the two tables).
12065
12066 ---
12067
12068 Hash table lookup is handled the same in version 1 and 2:
12069
12070 We assume that N and M will not exceed 2^32 - 1.
12071 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
12072
d2415c6c
DE
12073 Given a 64-bit compilation unit signature or a type signature S, an entry
12074 in the hash table is located as follows:
80626a55 12075
d2415c6c
DE
12076 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
12077 the low-order k bits all set to 1.
80626a55 12078
d2415c6c 12079 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
80626a55 12080
d2415c6c
DE
12081 3) If the hash table entry at index H matches the signature, use that
12082 entry. If the hash table entry at index H is unused (all zeroes),
12083 terminate the search: the signature is not present in the table.
80626a55 12084
d2415c6c 12085 4) Let H = (H + H') modulo M. Repeat at Step 3.
80626a55 12086
d2415c6c 12087 Because M > N and H' and M are relatively prime, the search is guaranteed
73869dc2 12088 to stop at an unused slot or find the match. */
80626a55
DE
12089
12090/* Create a hash table to map DWO IDs to their CU/TU entry in
12091 .debug_{info,types}.dwo in DWP_FILE.
12092 Returns NULL if there isn't one.
12093 Note: This function processes DWP files only, not DWO files. */
12094
12095static struct dwp_hash_table *
12096create_dwp_hash_table (struct dwp_file *dwp_file, int is_debug_types)
12097{
12098 struct objfile *objfile = dwarf2_per_objfile->objfile;
12099 bfd *dbfd = dwp_file->dbfd;
948f8e3d 12100 const gdb_byte *index_ptr, *index_end;
80626a55 12101 struct dwarf2_section_info *index;
73869dc2 12102 uint32_t version, nr_columns, nr_units, nr_slots;
80626a55
DE
12103 struct dwp_hash_table *htab;
12104
12105 if (is_debug_types)
12106 index = &dwp_file->sections.tu_index;
12107 else
12108 index = &dwp_file->sections.cu_index;
12109
12110 if (dwarf2_section_empty_p (index))
12111 return NULL;
12112 dwarf2_read_section (objfile, index);
12113
12114 index_ptr = index->buffer;
12115 index_end = index_ptr + index->size;
12116
12117 version = read_4_bytes (dbfd, index_ptr);
73869dc2
DE
12118 index_ptr += 4;
12119 if (version == 2)
12120 nr_columns = read_4_bytes (dbfd, index_ptr);
12121 else
12122 nr_columns = 0;
12123 index_ptr += 4;
80626a55
DE
12124 nr_units = read_4_bytes (dbfd, index_ptr);
12125 index_ptr += 4;
12126 nr_slots = read_4_bytes (dbfd, index_ptr);
12127 index_ptr += 4;
12128
73869dc2 12129 if (version != 1 && version != 2)
80626a55 12130 {
21aa081e 12131 error (_("Dwarf Error: unsupported DWP file version (%s)"
80626a55 12132 " [in module %s]"),
21aa081e 12133 pulongest (version), dwp_file->name);
80626a55
DE
12134 }
12135 if (nr_slots != (nr_slots & -nr_slots))
12136 {
21aa081e 12137 error (_("Dwarf Error: number of slots in DWP hash table (%s)"
80626a55 12138 " is not power of 2 [in module %s]"),
21aa081e 12139 pulongest (nr_slots), dwp_file->name);
80626a55
DE
12140 }
12141
12142 htab = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_hash_table);
73869dc2
DE
12143 htab->version = version;
12144 htab->nr_columns = nr_columns;
80626a55
DE
12145 htab->nr_units = nr_units;
12146 htab->nr_slots = nr_slots;
12147 htab->hash_table = index_ptr;
12148 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
73869dc2
DE
12149
12150 /* Exit early if the table is empty. */
12151 if (nr_slots == 0 || nr_units == 0
12152 || (version == 2 && nr_columns == 0))
12153 {
12154 /* All must be zero. */
12155 if (nr_slots != 0 || nr_units != 0
12156 || (version == 2 && nr_columns != 0))
12157 {
12158 complaint (&symfile_complaints,
12159 _("Empty DWP but nr_slots,nr_units,nr_columns not"
12160 " all zero [in modules %s]"),
12161 dwp_file->name);
12162 }
12163 return htab;
12164 }
12165
12166 if (version == 1)
12167 {
12168 htab->section_pool.v1.indices =
12169 htab->unit_table + sizeof (uint32_t) * nr_slots;
12170 /* It's harder to decide whether the section is too small in v1.
12171 V1 is deprecated anyway so we punt. */
12172 }
12173 else
12174 {
12175 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
12176 int *ids = htab->section_pool.v2.section_ids;
12177 /* Reverse map for error checking. */
12178 int ids_seen[DW_SECT_MAX + 1];
12179 int i;
12180
12181 if (nr_columns < 2)
12182 {
12183 error (_("Dwarf Error: bad DWP hash table, too few columns"
12184 " in section table [in module %s]"),
12185 dwp_file->name);
12186 }
12187 if (nr_columns > MAX_NR_V2_DWO_SECTIONS)
12188 {
12189 error (_("Dwarf Error: bad DWP hash table, too many columns"
12190 " in section table [in module %s]"),
12191 dwp_file->name);
12192 }
12193 memset (ids, 255, (DW_SECT_MAX + 1) * sizeof (int32_t));
12194 memset (ids_seen, 255, (DW_SECT_MAX + 1) * sizeof (int32_t));
12195 for (i = 0; i < nr_columns; ++i)
12196 {
12197 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
12198
12199 if (id < DW_SECT_MIN || id > DW_SECT_MAX)
12200 {
12201 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
12202 " in section table [in module %s]"),
12203 id, dwp_file->name);
12204 }
12205 if (ids_seen[id] != -1)
12206 {
12207 error (_("Dwarf Error: bad DWP hash table, duplicate section"
12208 " id %d in section table [in module %s]"),
12209 id, dwp_file->name);
12210 }
12211 ids_seen[id] = i;
12212 ids[i] = id;
12213 }
12214 /* Must have exactly one info or types section. */
12215 if (((ids_seen[DW_SECT_INFO] != -1)
12216 + (ids_seen[DW_SECT_TYPES] != -1))
12217 != 1)
12218 {
12219 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
12220 " DWO info/types section [in module %s]"),
12221 dwp_file->name);
12222 }
12223 /* Must have an abbrev section. */
12224 if (ids_seen[DW_SECT_ABBREV] == -1)
12225 {
12226 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
12227 " section [in module %s]"),
12228 dwp_file->name);
12229 }
12230 htab->section_pool.v2.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
12231 htab->section_pool.v2.sizes =
12232 htab->section_pool.v2.offsets + (sizeof (uint32_t)
12233 * nr_units * nr_columns);
12234 if ((htab->section_pool.v2.sizes + (sizeof (uint32_t)
12235 * nr_units * nr_columns))
12236 > index_end)
12237 {
12238 error (_("Dwarf Error: DWP index section is corrupt (too small)"
12239 " [in module %s]"),
12240 dwp_file->name);
12241 }
12242 }
80626a55
DE
12243
12244 return htab;
12245}
12246
12247/* Update SECTIONS with the data from SECTP.
12248
12249 This function is like the other "locate" section routines that are
12250 passed to bfd_map_over_sections, but in this context the sections to
73869dc2 12251 read comes from the DWP V1 hash table, not the full ELF section table.
80626a55
DE
12252
12253 The result is non-zero for success, or zero if an error was found. */
12254
12255static int
73869dc2
DE
12256locate_v1_virtual_dwo_sections (asection *sectp,
12257 struct virtual_v1_dwo_sections *sections)
80626a55
DE
12258{
12259 const struct dwop_section_names *names = &dwop_section_names;
12260
12261 if (section_is_p (sectp->name, &names->abbrev_dwo))
12262 {
12263 /* There can be only one. */
049412e3 12264 if (sections->abbrev.s.section != NULL)
80626a55 12265 return 0;
049412e3 12266 sections->abbrev.s.section = sectp;
80626a55
DE
12267 sections->abbrev.size = bfd_get_section_size (sectp);
12268 }
12269 else if (section_is_p (sectp->name, &names->info_dwo)
12270 || section_is_p (sectp->name, &names->types_dwo))
12271 {
12272 /* There can be only one. */
049412e3 12273 if (sections->info_or_types.s.section != NULL)
80626a55 12274 return 0;
049412e3 12275 sections->info_or_types.s.section = sectp;
80626a55
DE
12276 sections->info_or_types.size = bfd_get_section_size (sectp);
12277 }
12278 else if (section_is_p (sectp->name, &names->line_dwo))
12279 {
12280 /* There can be only one. */
049412e3 12281 if (sections->line.s.section != NULL)
80626a55 12282 return 0;
049412e3 12283 sections->line.s.section = sectp;
80626a55
DE
12284 sections->line.size = bfd_get_section_size (sectp);
12285 }
12286 else if (section_is_p (sectp->name, &names->loc_dwo))
12287 {
12288 /* There can be only one. */
049412e3 12289 if (sections->loc.s.section != NULL)
80626a55 12290 return 0;
049412e3 12291 sections->loc.s.section = sectp;
80626a55
DE
12292 sections->loc.size = bfd_get_section_size (sectp);
12293 }
12294 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12295 {
12296 /* There can be only one. */
049412e3 12297 if (sections->macinfo.s.section != NULL)
80626a55 12298 return 0;
049412e3 12299 sections->macinfo.s.section = sectp;
80626a55
DE
12300 sections->macinfo.size = bfd_get_section_size (sectp);
12301 }
12302 else if (section_is_p (sectp->name, &names->macro_dwo))
12303 {
12304 /* There can be only one. */
049412e3 12305 if (sections->macro.s.section != NULL)
80626a55 12306 return 0;
049412e3 12307 sections->macro.s.section = sectp;
80626a55
DE
12308 sections->macro.size = bfd_get_section_size (sectp);
12309 }
12310 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12311 {
12312 /* There can be only one. */
049412e3 12313 if (sections->str_offsets.s.section != NULL)
80626a55 12314 return 0;
049412e3 12315 sections->str_offsets.s.section = sectp;
80626a55
DE
12316 sections->str_offsets.size = bfd_get_section_size (sectp);
12317 }
12318 else
12319 {
12320 /* No other kind of section is valid. */
12321 return 0;
12322 }
12323
12324 return 1;
12325}
12326
73869dc2
DE
12327/* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
12328 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
12329 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
12330 This is for DWP version 1 files. */
80626a55
DE
12331
12332static struct dwo_unit *
73869dc2
DE
12333create_dwo_unit_in_dwp_v1 (struct dwp_file *dwp_file,
12334 uint32_t unit_index,
12335 const char *comp_dir,
12336 ULONGEST signature, int is_debug_types)
80626a55
DE
12337{
12338 struct objfile *objfile = dwarf2_per_objfile->objfile;
73869dc2
DE
12339 const struct dwp_hash_table *dwp_htab =
12340 is_debug_types ? dwp_file->tus : dwp_file->cus;
80626a55
DE
12341 bfd *dbfd = dwp_file->dbfd;
12342 const char *kind = is_debug_types ? "TU" : "CU";
12343 struct dwo_file *dwo_file;
12344 struct dwo_unit *dwo_unit;
73869dc2 12345 struct virtual_v1_dwo_sections sections;
80626a55 12346 void **dwo_file_slot;
80626a55
DE
12347 int i;
12348
73869dc2
DE
12349 gdb_assert (dwp_file->version == 1);
12350
b4f54984 12351 if (dwarf_read_debug)
80626a55 12352 {
73869dc2 12353 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V1 file: %s\n",
80626a55 12354 kind,
73869dc2 12355 pulongest (unit_index), hex_string (signature),
80626a55
DE
12356 dwp_file->name);
12357 }
12358
19ac8c2e 12359 /* Fetch the sections of this DWO unit.
80626a55
DE
12360 Put a limit on the number of sections we look for so that bad data
12361 doesn't cause us to loop forever. */
12362
73869dc2 12363#define MAX_NR_V1_DWO_SECTIONS \
80626a55
DE
12364 (1 /* .debug_info or .debug_types */ \
12365 + 1 /* .debug_abbrev */ \
12366 + 1 /* .debug_line */ \
12367 + 1 /* .debug_loc */ \
12368 + 1 /* .debug_str_offsets */ \
19ac8c2e 12369 + 1 /* .debug_macro or .debug_macinfo */ \
80626a55
DE
12370 + 1 /* trailing zero */)
12371
12372 memset (&sections, 0, sizeof (sections));
80626a55 12373
73869dc2 12374 for (i = 0; i < MAX_NR_V1_DWO_SECTIONS; ++i)
80626a55
DE
12375 {
12376 asection *sectp;
12377 uint32_t section_nr =
12378 read_4_bytes (dbfd,
73869dc2
DE
12379 dwp_htab->section_pool.v1.indices
12380 + (unit_index + i) * sizeof (uint32_t));
80626a55
DE
12381
12382 if (section_nr == 0)
12383 break;
12384 if (section_nr >= dwp_file->num_sections)
12385 {
12386 error (_("Dwarf Error: bad DWP hash table, section number too large"
12387 " [in module %s]"),
12388 dwp_file->name);
12389 }
12390
12391 sectp = dwp_file->elf_sections[section_nr];
73869dc2 12392 if (! locate_v1_virtual_dwo_sections (sectp, &sections))
80626a55
DE
12393 {
12394 error (_("Dwarf Error: bad DWP hash table, invalid section found"
12395 " [in module %s]"),
12396 dwp_file->name);
12397 }
12398 }
12399
12400 if (i < 2
a32a8923
DE
12401 || dwarf2_section_empty_p (&sections.info_or_types)
12402 || dwarf2_section_empty_p (&sections.abbrev))
80626a55
DE
12403 {
12404 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
12405 " [in module %s]"),
12406 dwp_file->name);
12407 }
73869dc2 12408 if (i == MAX_NR_V1_DWO_SECTIONS)
80626a55
DE
12409 {
12410 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
12411 " [in module %s]"),
12412 dwp_file->name);
12413 }
12414
12415 /* It's easier for the rest of the code if we fake a struct dwo_file and
12416 have dwo_unit "live" in that. At least for now.
12417
12418 The DWP file can be made up of a random collection of CUs and TUs.
c766f7ec 12419 However, for each CU + set of TUs that came from the same original DWO
57d63ce2
DE
12420 file, we can combine them back into a virtual DWO file to save space
12421 (fewer struct dwo_file objects to allocate). Remember that for really
80626a55
DE
12422 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
12423
791afaa2
TT
12424 std::string virtual_dwo_name =
12425 string_printf ("virtual-dwo/%d-%d-%d-%d",
12426 get_section_id (&sections.abbrev),
12427 get_section_id (&sections.line),
12428 get_section_id (&sections.loc),
12429 get_section_id (&sections.str_offsets));
80626a55 12430 /* Can we use an existing virtual DWO file? */
791afaa2 12431 dwo_file_slot = lookup_dwo_file_slot (virtual_dwo_name.c_str (), comp_dir);
80626a55
DE
12432 /* Create one if necessary. */
12433 if (*dwo_file_slot == NULL)
12434 {
b4f54984 12435 if (dwarf_read_debug)
80626a55
DE
12436 {
12437 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
791afaa2 12438 virtual_dwo_name.c_str ());
80626a55
DE
12439 }
12440 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
224c3ddb
SM
12441 dwo_file->dwo_name
12442 = (const char *) obstack_copy0 (&objfile->objfile_obstack,
791afaa2
TT
12443 virtual_dwo_name.c_str (),
12444 virtual_dwo_name.size ());
0ac5b59e 12445 dwo_file->comp_dir = comp_dir;
80626a55
DE
12446 dwo_file->sections.abbrev = sections.abbrev;
12447 dwo_file->sections.line = sections.line;
12448 dwo_file->sections.loc = sections.loc;
12449 dwo_file->sections.macinfo = sections.macinfo;
12450 dwo_file->sections.macro = sections.macro;
12451 dwo_file->sections.str_offsets = sections.str_offsets;
12452 /* The "str" section is global to the entire DWP file. */
12453 dwo_file->sections.str = dwp_file->sections.str;
57d63ce2 12454 /* The info or types section is assigned below to dwo_unit,
80626a55
DE
12455 there's no need to record it in dwo_file.
12456 Also, we can't simply record type sections in dwo_file because
12457 we record a pointer into the vector in dwo_unit. As we collect more
12458 types we'll grow the vector and eventually have to reallocate space
57d63ce2
DE
12459 for it, invalidating all copies of pointers into the previous
12460 contents. */
80626a55
DE
12461 *dwo_file_slot = dwo_file;
12462 }
12463 else
12464 {
b4f54984 12465 if (dwarf_read_debug)
80626a55
DE
12466 {
12467 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
791afaa2 12468 virtual_dwo_name.c_str ());
80626a55 12469 }
9a3c8263 12470 dwo_file = (struct dwo_file *) *dwo_file_slot;
80626a55 12471 }
80626a55
DE
12472
12473 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
12474 dwo_unit->dwo_file = dwo_file;
12475 dwo_unit->signature = signature;
8d749320
SM
12476 dwo_unit->section =
12477 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
8a0459fd 12478 *dwo_unit->section = sections.info_or_types;
57d63ce2 12479 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
80626a55
DE
12480
12481 return dwo_unit;
12482}
12483
73869dc2
DE
12484/* Subroutine of create_dwo_unit_in_dwp_v2 to simplify it.
12485 Given a pointer to the containing section SECTION, and OFFSET,SIZE of the
12486 piece within that section used by a TU/CU, return a virtual section
12487 of just that piece. */
12488
12489static struct dwarf2_section_info
12490create_dwp_v2_section (struct dwarf2_section_info *section,
12491 bfd_size_type offset, bfd_size_type size)
12492{
12493 struct dwarf2_section_info result;
12494 asection *sectp;
12495
12496 gdb_assert (section != NULL);
12497 gdb_assert (!section->is_virtual);
12498
12499 memset (&result, 0, sizeof (result));
12500 result.s.containing_section = section;
12501 result.is_virtual = 1;
12502
12503 if (size == 0)
12504 return result;
12505
12506 sectp = get_section_bfd_section (section);
12507
12508 /* Flag an error if the piece denoted by OFFSET,SIZE is outside the
12509 bounds of the real section. This is a pretty-rare event, so just
12510 flag an error (easier) instead of a warning and trying to cope. */
12511 if (sectp == NULL
12512 || offset + size > bfd_get_section_size (sectp))
12513 {
73869dc2
DE
12514 error (_("Dwarf Error: Bad DWP V2 section info, doesn't fit"
12515 " in section %s [in module %s]"),
12516 sectp ? bfd_section_name (abfd, sectp) : "<unknown>",
12517 objfile_name (dwarf2_per_objfile->objfile));
12518 }
12519
12520 result.virtual_offset = offset;
12521 result.size = size;
12522 return result;
12523}
12524
12525/* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
12526 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
12527 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
12528 This is for DWP version 2 files. */
12529
12530static struct dwo_unit *
12531create_dwo_unit_in_dwp_v2 (struct dwp_file *dwp_file,
12532 uint32_t unit_index,
12533 const char *comp_dir,
12534 ULONGEST signature, int is_debug_types)
12535{
12536 struct objfile *objfile = dwarf2_per_objfile->objfile;
12537 const struct dwp_hash_table *dwp_htab =
12538 is_debug_types ? dwp_file->tus : dwp_file->cus;
12539 bfd *dbfd = dwp_file->dbfd;
12540 const char *kind = is_debug_types ? "TU" : "CU";
12541 struct dwo_file *dwo_file;
12542 struct dwo_unit *dwo_unit;
12543 struct virtual_v2_dwo_sections sections;
12544 void **dwo_file_slot;
73869dc2
DE
12545 int i;
12546
12547 gdb_assert (dwp_file->version == 2);
12548
b4f54984 12549 if (dwarf_read_debug)
73869dc2
DE
12550 {
12551 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V2 file: %s\n",
12552 kind,
12553 pulongest (unit_index), hex_string (signature),
12554 dwp_file->name);
12555 }
12556
12557 /* Fetch the section offsets of this DWO unit. */
12558
12559 memset (&sections, 0, sizeof (sections));
73869dc2
DE
12560
12561 for (i = 0; i < dwp_htab->nr_columns; ++i)
12562 {
12563 uint32_t offset = read_4_bytes (dbfd,
12564 dwp_htab->section_pool.v2.offsets
12565 + (((unit_index - 1) * dwp_htab->nr_columns
12566 + i)
12567 * sizeof (uint32_t)));
12568 uint32_t size = read_4_bytes (dbfd,
12569 dwp_htab->section_pool.v2.sizes
12570 + (((unit_index - 1) * dwp_htab->nr_columns
12571 + i)
12572 * sizeof (uint32_t)));
12573
12574 switch (dwp_htab->section_pool.v2.section_ids[i])
12575 {
12576 case DW_SECT_INFO:
12577 case DW_SECT_TYPES:
12578 sections.info_or_types_offset = offset;
12579 sections.info_or_types_size = size;
12580 break;
12581 case DW_SECT_ABBREV:
12582 sections.abbrev_offset = offset;
12583 sections.abbrev_size = size;
12584 break;
12585 case DW_SECT_LINE:
12586 sections.line_offset = offset;
12587 sections.line_size = size;
12588 break;
12589 case DW_SECT_LOC:
12590 sections.loc_offset = offset;
12591 sections.loc_size = size;
12592 break;
12593 case DW_SECT_STR_OFFSETS:
12594 sections.str_offsets_offset = offset;
12595 sections.str_offsets_size = size;
12596 break;
12597 case DW_SECT_MACINFO:
12598 sections.macinfo_offset = offset;
12599 sections.macinfo_size = size;
12600 break;
12601 case DW_SECT_MACRO:
12602 sections.macro_offset = offset;
12603 sections.macro_size = size;
12604 break;
12605 }
12606 }
12607
12608 /* It's easier for the rest of the code if we fake a struct dwo_file and
12609 have dwo_unit "live" in that. At least for now.
12610
12611 The DWP file can be made up of a random collection of CUs and TUs.
12612 However, for each CU + set of TUs that came from the same original DWO
12613 file, we can combine them back into a virtual DWO file to save space
12614 (fewer struct dwo_file objects to allocate). Remember that for really
12615 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
12616
791afaa2
TT
12617 std::string virtual_dwo_name =
12618 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld",
12619 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
12620 (long) (sections.line_size ? sections.line_offset : 0),
12621 (long) (sections.loc_size ? sections.loc_offset : 0),
12622 (long) (sections.str_offsets_size
12623 ? sections.str_offsets_offset : 0));
73869dc2 12624 /* Can we use an existing virtual DWO file? */
791afaa2 12625 dwo_file_slot = lookup_dwo_file_slot (virtual_dwo_name.c_str (), comp_dir);
73869dc2
DE
12626 /* Create one if necessary. */
12627 if (*dwo_file_slot == NULL)
12628 {
b4f54984 12629 if (dwarf_read_debug)
73869dc2
DE
12630 {
12631 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
791afaa2 12632 virtual_dwo_name.c_str ());
73869dc2
DE
12633 }
12634 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
224c3ddb
SM
12635 dwo_file->dwo_name
12636 = (const char *) obstack_copy0 (&objfile->objfile_obstack,
791afaa2
TT
12637 virtual_dwo_name.c_str (),
12638 virtual_dwo_name.size ());
73869dc2
DE
12639 dwo_file->comp_dir = comp_dir;
12640 dwo_file->sections.abbrev =
12641 create_dwp_v2_section (&dwp_file->sections.abbrev,
12642 sections.abbrev_offset, sections.abbrev_size);
12643 dwo_file->sections.line =
12644 create_dwp_v2_section (&dwp_file->sections.line,
12645 sections.line_offset, sections.line_size);
12646 dwo_file->sections.loc =
12647 create_dwp_v2_section (&dwp_file->sections.loc,
12648 sections.loc_offset, sections.loc_size);
12649 dwo_file->sections.macinfo =
12650 create_dwp_v2_section (&dwp_file->sections.macinfo,
12651 sections.macinfo_offset, sections.macinfo_size);
12652 dwo_file->sections.macro =
12653 create_dwp_v2_section (&dwp_file->sections.macro,
12654 sections.macro_offset, sections.macro_size);
12655 dwo_file->sections.str_offsets =
12656 create_dwp_v2_section (&dwp_file->sections.str_offsets,
12657 sections.str_offsets_offset,
12658 sections.str_offsets_size);
12659 /* The "str" section is global to the entire DWP file. */
12660 dwo_file->sections.str = dwp_file->sections.str;
12661 /* The info or types section is assigned below to dwo_unit,
12662 there's no need to record it in dwo_file.
12663 Also, we can't simply record type sections in dwo_file because
12664 we record a pointer into the vector in dwo_unit. As we collect more
12665 types we'll grow the vector and eventually have to reallocate space
12666 for it, invalidating all copies of pointers into the previous
12667 contents. */
12668 *dwo_file_slot = dwo_file;
12669 }
12670 else
12671 {
b4f54984 12672 if (dwarf_read_debug)
73869dc2
DE
12673 {
12674 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
791afaa2 12675 virtual_dwo_name.c_str ());
73869dc2 12676 }
9a3c8263 12677 dwo_file = (struct dwo_file *) *dwo_file_slot;
73869dc2 12678 }
73869dc2
DE
12679
12680 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
12681 dwo_unit->dwo_file = dwo_file;
12682 dwo_unit->signature = signature;
8d749320
SM
12683 dwo_unit->section =
12684 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
73869dc2
DE
12685 *dwo_unit->section = create_dwp_v2_section (is_debug_types
12686 ? &dwp_file->sections.types
12687 : &dwp_file->sections.info,
12688 sections.info_or_types_offset,
12689 sections.info_or_types_size);
12690 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
12691
12692 return dwo_unit;
12693}
12694
57d63ce2
DE
12695/* Lookup the DWO unit with SIGNATURE in DWP_FILE.
12696 Returns NULL if the signature isn't found. */
80626a55
DE
12697
12698static struct dwo_unit *
57d63ce2
DE
12699lookup_dwo_unit_in_dwp (struct dwp_file *dwp_file, const char *comp_dir,
12700 ULONGEST signature, int is_debug_types)
80626a55 12701{
57d63ce2
DE
12702 const struct dwp_hash_table *dwp_htab =
12703 is_debug_types ? dwp_file->tus : dwp_file->cus;
80626a55 12704 bfd *dbfd = dwp_file->dbfd;
57d63ce2 12705 uint32_t mask = dwp_htab->nr_slots - 1;
80626a55
DE
12706 uint32_t hash = signature & mask;
12707 uint32_t hash2 = ((signature >> 32) & mask) | 1;
12708 unsigned int i;
12709 void **slot;
870f88f7 12710 struct dwo_unit find_dwo_cu;
80626a55
DE
12711
12712 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
12713 find_dwo_cu.signature = signature;
19ac8c2e
DE
12714 slot = htab_find_slot (is_debug_types
12715 ? dwp_file->loaded_tus
12716 : dwp_file->loaded_cus,
12717 &find_dwo_cu, INSERT);
80626a55
DE
12718
12719 if (*slot != NULL)
9a3c8263 12720 return (struct dwo_unit *) *slot;
80626a55
DE
12721
12722 /* Use a for loop so that we don't loop forever on bad debug info. */
57d63ce2 12723 for (i = 0; i < dwp_htab->nr_slots; ++i)
80626a55
DE
12724 {
12725 ULONGEST signature_in_table;
12726
12727 signature_in_table =
57d63ce2 12728 read_8_bytes (dbfd, dwp_htab->hash_table + hash * sizeof (uint64_t));
80626a55
DE
12729 if (signature_in_table == signature)
12730 {
57d63ce2
DE
12731 uint32_t unit_index =
12732 read_4_bytes (dbfd,
12733 dwp_htab->unit_table + hash * sizeof (uint32_t));
80626a55 12734
73869dc2
DE
12735 if (dwp_file->version == 1)
12736 {
12737 *slot = create_dwo_unit_in_dwp_v1 (dwp_file, unit_index,
12738 comp_dir, signature,
12739 is_debug_types);
12740 }
12741 else
12742 {
12743 *slot = create_dwo_unit_in_dwp_v2 (dwp_file, unit_index,
12744 comp_dir, signature,
12745 is_debug_types);
12746 }
9a3c8263 12747 return (struct dwo_unit *) *slot;
80626a55
DE
12748 }
12749 if (signature_in_table == 0)
12750 return NULL;
12751 hash = (hash + hash2) & mask;
12752 }
12753
12754 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
12755 " [in module %s]"),
12756 dwp_file->name);
12757}
12758
ab5088bf 12759/* Subroutine of open_dwo_file,open_dwp_file to simplify them.
3019eac3
DE
12760 Open the file specified by FILE_NAME and hand it off to BFD for
12761 preliminary analysis. Return a newly initialized bfd *, which
12762 includes a canonicalized copy of FILE_NAME.
80626a55 12763 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
6ac97d4c
DE
12764 SEARCH_CWD is true if the current directory is to be searched.
12765 It will be searched before debug-file-directory.
13aaf454
DE
12766 If successful, the file is added to the bfd include table of the
12767 objfile's bfd (see gdb_bfd_record_inclusion).
6ac97d4c 12768 If unable to find/open the file, return NULL.
3019eac3
DE
12769 NOTE: This function is derived from symfile_bfd_open. */
12770
192b62ce 12771static gdb_bfd_ref_ptr
6ac97d4c 12772try_open_dwop_file (const char *file_name, int is_dwp, int search_cwd)
3019eac3 12773{
80626a55 12774 int desc, flags;
3019eac3 12775 char *absolute_name;
9c02c129
DE
12776 /* Blech. OPF_TRY_CWD_FIRST also disables searching the path list if
12777 FILE_NAME contains a '/'. So we can't use it. Instead prepend "."
12778 to debug_file_directory. */
12779 char *search_path;
12780 static const char dirname_separator_string[] = { DIRNAME_SEPARATOR, '\0' };
12781
6ac97d4c
DE
12782 if (search_cwd)
12783 {
12784 if (*debug_file_directory != '\0')
12785 search_path = concat (".", dirname_separator_string,
b36cec19 12786 debug_file_directory, (char *) NULL);
6ac97d4c
DE
12787 else
12788 search_path = xstrdup (".");
12789 }
9c02c129 12790 else
6ac97d4c 12791 search_path = xstrdup (debug_file_directory);
3019eac3 12792
492c0ab7 12793 flags = OPF_RETURN_REALPATH;
80626a55
DE
12794 if (is_dwp)
12795 flags |= OPF_SEARCH_IN_PATH;
9c02c129 12796 desc = openp (search_path, flags, file_name,
3019eac3 12797 O_RDONLY | O_BINARY, &absolute_name);
9c02c129 12798 xfree (search_path);
3019eac3
DE
12799 if (desc < 0)
12800 return NULL;
12801
192b62ce 12802 gdb_bfd_ref_ptr sym_bfd (gdb_bfd_open (absolute_name, gnutarget, desc));
a4453b7e 12803 xfree (absolute_name);
9c02c129
DE
12804 if (sym_bfd == NULL)
12805 return NULL;
192b62ce 12806 bfd_set_cacheable (sym_bfd.get (), 1);
3019eac3 12807
192b62ce
TT
12808 if (!bfd_check_format (sym_bfd.get (), bfd_object))
12809 return NULL;
3019eac3 12810
13aaf454
DE
12811 /* Success. Record the bfd as having been included by the objfile's bfd.
12812 This is important because things like demangled_names_hash lives in the
12813 objfile's per_bfd space and may have references to things like symbol
12814 names that live in the DWO/DWP file's per_bfd space. PR 16426. */
192b62ce 12815 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd, sym_bfd.get ());
13aaf454 12816
3019eac3
DE
12817 return sym_bfd;
12818}
12819
ab5088bf 12820/* Try to open DWO file FILE_NAME.
3019eac3
DE
12821 COMP_DIR is the DW_AT_comp_dir attribute.
12822 The result is the bfd handle of the file.
12823 If there is a problem finding or opening the file, return NULL.
12824 Upon success, the canonicalized path of the file is stored in the bfd,
12825 same as symfile_bfd_open. */
12826
192b62ce 12827static gdb_bfd_ref_ptr
ab5088bf 12828open_dwo_file (const char *file_name, const char *comp_dir)
3019eac3 12829{
80626a55 12830 if (IS_ABSOLUTE_PATH (file_name))
6ac97d4c 12831 return try_open_dwop_file (file_name, 0 /*is_dwp*/, 0 /*search_cwd*/);
3019eac3
DE
12832
12833 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
12834
12835 if (comp_dir != NULL)
12836 {
b36cec19
PA
12837 char *path_to_try = concat (comp_dir, SLASH_STRING,
12838 file_name, (char *) NULL);
3019eac3
DE
12839
12840 /* NOTE: If comp_dir is a relative path, this will also try the
12841 search path, which seems useful. */
192b62ce
TT
12842 gdb_bfd_ref_ptr abfd (try_open_dwop_file (path_to_try, 0 /*is_dwp*/,
12843 1 /*search_cwd*/));
3019eac3
DE
12844 xfree (path_to_try);
12845 if (abfd != NULL)
12846 return abfd;
12847 }
12848
12849 /* That didn't work, try debug-file-directory, which, despite its name,
12850 is a list of paths. */
12851
12852 if (*debug_file_directory == '\0')
12853 return NULL;
12854
6ac97d4c 12855 return try_open_dwop_file (file_name, 0 /*is_dwp*/, 1 /*search_cwd*/);
3019eac3
DE
12856}
12857
80626a55
DE
12858/* This function is mapped across the sections and remembers the offset and
12859 size of each of the DWO debugging sections we are interested in. */
12860
12861static void
12862dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp, void *dwo_sections_ptr)
12863{
9a3c8263 12864 struct dwo_sections *dwo_sections = (struct dwo_sections *) dwo_sections_ptr;
80626a55
DE
12865 const struct dwop_section_names *names = &dwop_section_names;
12866
12867 if (section_is_p (sectp->name, &names->abbrev_dwo))
12868 {
049412e3 12869 dwo_sections->abbrev.s.section = sectp;
80626a55
DE
12870 dwo_sections->abbrev.size = bfd_get_section_size (sectp);
12871 }
12872 else if (section_is_p (sectp->name, &names->info_dwo))
12873 {
049412e3 12874 dwo_sections->info.s.section = sectp;
80626a55
DE
12875 dwo_sections->info.size = bfd_get_section_size (sectp);
12876 }
12877 else if (section_is_p (sectp->name, &names->line_dwo))
12878 {
049412e3 12879 dwo_sections->line.s.section = sectp;
80626a55
DE
12880 dwo_sections->line.size = bfd_get_section_size (sectp);
12881 }
12882 else if (section_is_p (sectp->name, &names->loc_dwo))
12883 {
049412e3 12884 dwo_sections->loc.s.section = sectp;
80626a55
DE
12885 dwo_sections->loc.size = bfd_get_section_size (sectp);
12886 }
12887 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12888 {
049412e3 12889 dwo_sections->macinfo.s.section = sectp;
80626a55
DE
12890 dwo_sections->macinfo.size = bfd_get_section_size (sectp);
12891 }
12892 else if (section_is_p (sectp->name, &names->macro_dwo))
12893 {
049412e3 12894 dwo_sections->macro.s.section = sectp;
80626a55
DE
12895 dwo_sections->macro.size = bfd_get_section_size (sectp);
12896 }
12897 else if (section_is_p (sectp->name, &names->str_dwo))
12898 {
049412e3 12899 dwo_sections->str.s.section = sectp;
80626a55
DE
12900 dwo_sections->str.size = bfd_get_section_size (sectp);
12901 }
12902 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12903 {
049412e3 12904 dwo_sections->str_offsets.s.section = sectp;
80626a55
DE
12905 dwo_sections->str_offsets.size = bfd_get_section_size (sectp);
12906 }
12907 else if (section_is_p (sectp->name, &names->types_dwo))
12908 {
12909 struct dwarf2_section_info type_section;
12910
12911 memset (&type_section, 0, sizeof (type_section));
049412e3 12912 type_section.s.section = sectp;
80626a55
DE
12913 type_section.size = bfd_get_section_size (sectp);
12914 VEC_safe_push (dwarf2_section_info_def, dwo_sections->types,
12915 &type_section);
12916 }
12917}
12918
ab5088bf 12919/* Initialize the use of the DWO file specified by DWO_NAME and referenced
19c3d4c9 12920 by PER_CU. This is for the non-DWP case.
80626a55 12921 The result is NULL if DWO_NAME can't be found. */
3019eac3
DE
12922
12923static struct dwo_file *
0ac5b59e
DE
12924open_and_init_dwo_file (struct dwarf2_per_cu_data *per_cu,
12925 const char *dwo_name, const char *comp_dir)
3019eac3
DE
12926{
12927 struct objfile *objfile = dwarf2_per_objfile->objfile;
80626a55 12928 struct dwo_file *dwo_file;
3019eac3
DE
12929 struct cleanup *cleanups;
12930
192b62ce 12931 gdb_bfd_ref_ptr dbfd (open_dwo_file (dwo_name, comp_dir));
80626a55
DE
12932 if (dbfd == NULL)
12933 {
b4f54984 12934 if (dwarf_read_debug)
80626a55
DE
12935 fprintf_unfiltered (gdb_stdlog, "DWO file not found: %s\n", dwo_name);
12936 return NULL;
12937 }
12938 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
0ac5b59e
DE
12939 dwo_file->dwo_name = dwo_name;
12940 dwo_file->comp_dir = comp_dir;
192b62ce 12941 dwo_file->dbfd = dbfd.release ();
3019eac3
DE
12942
12943 cleanups = make_cleanup (free_dwo_file_cleanup, dwo_file);
12944
192b62ce
TT
12945 bfd_map_over_sections (dwo_file->dbfd, dwarf2_locate_dwo_sections,
12946 &dwo_file->sections);
3019eac3 12947
33c5cd75 12948 create_cus_hash_table (*dwo_file, dwo_file->sections.info, dwo_file->cus);
3019eac3 12949
78d4d2c5
JK
12950 create_debug_types_hash_table (dwo_file, dwo_file->sections.types,
12951 dwo_file->tus);
3019eac3
DE
12952
12953 discard_cleanups (cleanups);
12954
b4f54984 12955 if (dwarf_read_debug)
80626a55
DE
12956 fprintf_unfiltered (gdb_stdlog, "DWO file found: %s\n", dwo_name);
12957
3019eac3
DE
12958 return dwo_file;
12959}
12960
80626a55 12961/* This function is mapped across the sections and remembers the offset and
73869dc2
DE
12962 size of each of the DWP debugging sections common to version 1 and 2 that
12963 we are interested in. */
3019eac3 12964
80626a55 12965static void
73869dc2
DE
12966dwarf2_locate_common_dwp_sections (bfd *abfd, asection *sectp,
12967 void *dwp_file_ptr)
3019eac3 12968{
9a3c8263 12969 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
80626a55
DE
12970 const struct dwop_section_names *names = &dwop_section_names;
12971 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
3019eac3 12972
80626a55 12973 /* Record the ELF section number for later lookup: this is what the
73869dc2 12974 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
80626a55
DE
12975 gdb_assert (elf_section_nr < dwp_file->num_sections);
12976 dwp_file->elf_sections[elf_section_nr] = sectp;
3019eac3 12977
80626a55
DE
12978 /* Look for specific sections that we need. */
12979 if (section_is_p (sectp->name, &names->str_dwo))
12980 {
049412e3 12981 dwp_file->sections.str.s.section = sectp;
80626a55
DE
12982 dwp_file->sections.str.size = bfd_get_section_size (sectp);
12983 }
12984 else if (section_is_p (sectp->name, &names->cu_index))
12985 {
049412e3 12986 dwp_file->sections.cu_index.s.section = sectp;
80626a55
DE
12987 dwp_file->sections.cu_index.size = bfd_get_section_size (sectp);
12988 }
12989 else if (section_is_p (sectp->name, &names->tu_index))
12990 {
049412e3 12991 dwp_file->sections.tu_index.s.section = sectp;
80626a55
DE
12992 dwp_file->sections.tu_index.size = bfd_get_section_size (sectp);
12993 }
12994}
3019eac3 12995
73869dc2
DE
12996/* This function is mapped across the sections and remembers the offset and
12997 size of each of the DWP version 2 debugging sections that we are interested
12998 in. This is split into a separate function because we don't know if we
12999 have version 1 or 2 until we parse the cu_index/tu_index sections. */
13000
13001static void
13002dwarf2_locate_v2_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
13003{
9a3c8263 13004 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
73869dc2
DE
13005 const struct dwop_section_names *names = &dwop_section_names;
13006 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
13007
13008 /* Record the ELF section number for later lookup: this is what the
13009 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
13010 gdb_assert (elf_section_nr < dwp_file->num_sections);
13011 dwp_file->elf_sections[elf_section_nr] = sectp;
13012
13013 /* Look for specific sections that we need. */
13014 if (section_is_p (sectp->name, &names->abbrev_dwo))
13015 {
049412e3 13016 dwp_file->sections.abbrev.s.section = sectp;
73869dc2
DE
13017 dwp_file->sections.abbrev.size = bfd_get_section_size (sectp);
13018 }
13019 else if (section_is_p (sectp->name, &names->info_dwo))
13020 {
049412e3 13021 dwp_file->sections.info.s.section = sectp;
73869dc2
DE
13022 dwp_file->sections.info.size = bfd_get_section_size (sectp);
13023 }
13024 else if (section_is_p (sectp->name, &names->line_dwo))
13025 {
049412e3 13026 dwp_file->sections.line.s.section = sectp;
73869dc2
DE
13027 dwp_file->sections.line.size = bfd_get_section_size (sectp);
13028 }
13029 else if (section_is_p (sectp->name, &names->loc_dwo))
13030 {
049412e3 13031 dwp_file->sections.loc.s.section = sectp;
73869dc2
DE
13032 dwp_file->sections.loc.size = bfd_get_section_size (sectp);
13033 }
13034 else if (section_is_p (sectp->name, &names->macinfo_dwo))
13035 {
049412e3 13036 dwp_file->sections.macinfo.s.section = sectp;
73869dc2
DE
13037 dwp_file->sections.macinfo.size = bfd_get_section_size (sectp);
13038 }
13039 else if (section_is_p (sectp->name, &names->macro_dwo))
13040 {
049412e3 13041 dwp_file->sections.macro.s.section = sectp;
73869dc2
DE
13042 dwp_file->sections.macro.size = bfd_get_section_size (sectp);
13043 }
13044 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
13045 {
049412e3 13046 dwp_file->sections.str_offsets.s.section = sectp;
73869dc2
DE
13047 dwp_file->sections.str_offsets.size = bfd_get_section_size (sectp);
13048 }
13049 else if (section_is_p (sectp->name, &names->types_dwo))
13050 {
049412e3 13051 dwp_file->sections.types.s.section = sectp;
73869dc2
DE
13052 dwp_file->sections.types.size = bfd_get_section_size (sectp);
13053 }
13054}
13055
80626a55 13056/* Hash function for dwp_file loaded CUs/TUs. */
3019eac3 13057
80626a55
DE
13058static hashval_t
13059hash_dwp_loaded_cutus (const void *item)
13060{
9a3c8263 13061 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
3019eac3 13062
80626a55
DE
13063 /* This drops the top 32 bits of the signature, but is ok for a hash. */
13064 return dwo_unit->signature;
3019eac3
DE
13065}
13066
80626a55 13067/* Equality function for dwp_file loaded CUs/TUs. */
3019eac3 13068
80626a55
DE
13069static int
13070eq_dwp_loaded_cutus (const void *a, const void *b)
3019eac3 13071{
9a3c8263
SM
13072 const struct dwo_unit *dua = (const struct dwo_unit *) a;
13073 const struct dwo_unit *dub = (const struct dwo_unit *) b;
3019eac3 13074
80626a55
DE
13075 return dua->signature == dub->signature;
13076}
3019eac3 13077
80626a55 13078/* Allocate a hash table for dwp_file loaded CUs/TUs. */
3019eac3 13079
80626a55
DE
13080static htab_t
13081allocate_dwp_loaded_cutus_table (struct objfile *objfile)
13082{
13083 return htab_create_alloc_ex (3,
13084 hash_dwp_loaded_cutus,
13085 eq_dwp_loaded_cutus,
13086 NULL,
13087 &objfile->objfile_obstack,
13088 hashtab_obstack_allocate,
13089 dummy_obstack_deallocate);
13090}
3019eac3 13091
ab5088bf
DE
13092/* Try to open DWP file FILE_NAME.
13093 The result is the bfd handle of the file.
13094 If there is a problem finding or opening the file, return NULL.
13095 Upon success, the canonicalized path of the file is stored in the bfd,
13096 same as symfile_bfd_open. */
13097
192b62ce 13098static gdb_bfd_ref_ptr
ab5088bf
DE
13099open_dwp_file (const char *file_name)
13100{
192b62ce
TT
13101 gdb_bfd_ref_ptr abfd (try_open_dwop_file (file_name, 1 /*is_dwp*/,
13102 1 /*search_cwd*/));
6ac97d4c
DE
13103 if (abfd != NULL)
13104 return abfd;
13105
13106 /* Work around upstream bug 15652.
13107 http://sourceware.org/bugzilla/show_bug.cgi?id=15652
13108 [Whether that's a "bug" is debatable, but it is getting in our way.]
13109 We have no real idea where the dwp file is, because gdb's realpath-ing
13110 of the executable's path may have discarded the needed info.
13111 [IWBN if the dwp file name was recorded in the executable, akin to
13112 .gnu_debuglink, but that doesn't exist yet.]
13113 Strip the directory from FILE_NAME and search again. */
13114 if (*debug_file_directory != '\0')
13115 {
13116 /* Don't implicitly search the current directory here.
13117 If the user wants to search "." to handle this case,
13118 it must be added to debug-file-directory. */
13119 return try_open_dwop_file (lbasename (file_name), 1 /*is_dwp*/,
13120 0 /*search_cwd*/);
13121 }
13122
13123 return NULL;
ab5088bf
DE
13124}
13125
80626a55
DE
13126/* Initialize the use of the DWP file for the current objfile.
13127 By convention the name of the DWP file is ${objfile}.dwp.
13128 The result is NULL if it can't be found. */
a766d390 13129
80626a55 13130static struct dwp_file *
ab5088bf 13131open_and_init_dwp_file (void)
80626a55
DE
13132{
13133 struct objfile *objfile = dwarf2_per_objfile->objfile;
13134 struct dwp_file *dwp_file;
80626a55 13135
82bf32bc
JK
13136 /* Try to find first .dwp for the binary file before any symbolic links
13137 resolving. */
6c447423
DE
13138
13139 /* If the objfile is a debug file, find the name of the real binary
13140 file and get the name of dwp file from there. */
d721ba37 13141 std::string dwp_name;
6c447423
DE
13142 if (objfile->separate_debug_objfile_backlink != NULL)
13143 {
13144 struct objfile *backlink = objfile->separate_debug_objfile_backlink;
13145 const char *backlink_basename = lbasename (backlink->original_name);
6c447423 13146
d721ba37 13147 dwp_name = ldirname (objfile->original_name) + SLASH_STRING + backlink_basename;
6c447423
DE
13148 }
13149 else
d721ba37
PA
13150 dwp_name = objfile->original_name;
13151
13152 dwp_name += ".dwp";
80626a55 13153
d721ba37 13154 gdb_bfd_ref_ptr dbfd (open_dwp_file (dwp_name.c_str ()));
82bf32bc
JK
13155 if (dbfd == NULL
13156 && strcmp (objfile->original_name, objfile_name (objfile)) != 0)
13157 {
13158 /* Try to find .dwp for the binary file after gdb_realpath resolving. */
d721ba37
PA
13159 dwp_name = objfile_name (objfile);
13160 dwp_name += ".dwp";
13161 dbfd = open_dwp_file (dwp_name.c_str ());
82bf32bc
JK
13162 }
13163
80626a55
DE
13164 if (dbfd == NULL)
13165 {
b4f54984 13166 if (dwarf_read_debug)
d721ba37 13167 fprintf_unfiltered (gdb_stdlog, "DWP file not found: %s\n", dwp_name.c_str ());
80626a55 13168 return NULL;
3019eac3 13169 }
80626a55 13170 dwp_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_file);
192b62ce
TT
13171 dwp_file->name = bfd_get_filename (dbfd.get ());
13172 dwp_file->dbfd = dbfd.release ();
c906108c 13173
80626a55 13174 /* +1: section 0 is unused */
192b62ce 13175 dwp_file->num_sections = bfd_count_sections (dwp_file->dbfd) + 1;
80626a55
DE
13176 dwp_file->elf_sections =
13177 OBSTACK_CALLOC (&objfile->objfile_obstack,
13178 dwp_file->num_sections, asection *);
13179
192b62ce
TT
13180 bfd_map_over_sections (dwp_file->dbfd, dwarf2_locate_common_dwp_sections,
13181 dwp_file);
80626a55
DE
13182
13183 dwp_file->cus = create_dwp_hash_table (dwp_file, 0);
13184
13185 dwp_file->tus = create_dwp_hash_table (dwp_file, 1);
13186
73869dc2 13187 /* The DWP file version is stored in the hash table. Oh well. */
08302ed2
DE
13188 if (dwp_file->cus && dwp_file->tus
13189 && dwp_file->cus->version != dwp_file->tus->version)
73869dc2
DE
13190 {
13191 /* Technically speaking, we should try to limp along, but this is
fbcbc3fd 13192 pretty bizarre. We use pulongest here because that's the established
4d65956b 13193 portability solution (e.g, we cannot use %u for uint32_t). */
fbcbc3fd
DE
13194 error (_("Dwarf Error: DWP file CU version %s doesn't match"
13195 " TU version %s [in DWP file %s]"),
13196 pulongest (dwp_file->cus->version),
d721ba37 13197 pulongest (dwp_file->tus->version), dwp_name.c_str ());
73869dc2 13198 }
08302ed2
DE
13199
13200 if (dwp_file->cus)
13201 dwp_file->version = dwp_file->cus->version;
13202 else if (dwp_file->tus)
13203 dwp_file->version = dwp_file->tus->version;
13204 else
13205 dwp_file->version = 2;
73869dc2
DE
13206
13207 if (dwp_file->version == 2)
192b62ce
TT
13208 bfd_map_over_sections (dwp_file->dbfd, dwarf2_locate_v2_dwp_sections,
13209 dwp_file);
73869dc2 13210
19ac8c2e
DE
13211 dwp_file->loaded_cus = allocate_dwp_loaded_cutus_table (objfile);
13212 dwp_file->loaded_tus = allocate_dwp_loaded_cutus_table (objfile);
80626a55 13213
b4f54984 13214 if (dwarf_read_debug)
80626a55
DE
13215 {
13216 fprintf_unfiltered (gdb_stdlog, "DWP file found: %s\n", dwp_file->name);
13217 fprintf_unfiltered (gdb_stdlog,
21aa081e
PA
13218 " %s CUs, %s TUs\n",
13219 pulongest (dwp_file->cus ? dwp_file->cus->nr_units : 0),
13220 pulongest (dwp_file->tus ? dwp_file->tus->nr_units : 0));
80626a55
DE
13221 }
13222
13223 return dwp_file;
3019eac3 13224}
c906108c 13225
ab5088bf
DE
13226/* Wrapper around open_and_init_dwp_file, only open it once. */
13227
13228static struct dwp_file *
13229get_dwp_file (void)
13230{
13231 if (! dwarf2_per_objfile->dwp_checked)
13232 {
13233 dwarf2_per_objfile->dwp_file = open_and_init_dwp_file ();
13234 dwarf2_per_objfile->dwp_checked = 1;
13235 }
13236 return dwarf2_per_objfile->dwp_file;
13237}
13238
80626a55
DE
13239/* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
13240 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
13241 or in the DWP file for the objfile, referenced by THIS_UNIT.
3019eac3 13242 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
80626a55
DE
13243 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
13244
13245 This is called, for example, when wanting to read a variable with a
13246 complex location. Therefore we don't want to do file i/o for every call.
13247 Therefore we don't want to look for a DWO file on every call.
13248 Therefore we first see if we've already seen SIGNATURE in a DWP file,
13249 then we check if we've already seen DWO_NAME, and only THEN do we check
13250 for a DWO file.
13251
1c658ad5 13252 The result is a pointer to the dwo_unit object or NULL if we didn't find it
80626a55 13253 (dwo_id mismatch or couldn't find the DWO/DWP file). */
debd256d 13254
3019eac3 13255static struct dwo_unit *
80626a55
DE
13256lookup_dwo_cutu (struct dwarf2_per_cu_data *this_unit,
13257 const char *dwo_name, const char *comp_dir,
13258 ULONGEST signature, int is_debug_types)
3019eac3
DE
13259{
13260 struct objfile *objfile = dwarf2_per_objfile->objfile;
80626a55
DE
13261 const char *kind = is_debug_types ? "TU" : "CU";
13262 void **dwo_file_slot;
3019eac3 13263 struct dwo_file *dwo_file;
80626a55 13264 struct dwp_file *dwp_file;
cb1df416 13265
6a506a2d
DE
13266 /* First see if there's a DWP file.
13267 If we have a DWP file but didn't find the DWO inside it, don't
13268 look for the original DWO file. It makes gdb behave differently
13269 depending on whether one is debugging in the build tree. */
cf2c3c16 13270
ab5088bf 13271 dwp_file = get_dwp_file ();
80626a55 13272 if (dwp_file != NULL)
cf2c3c16 13273 {
80626a55
DE
13274 const struct dwp_hash_table *dwp_htab =
13275 is_debug_types ? dwp_file->tus : dwp_file->cus;
13276
13277 if (dwp_htab != NULL)
13278 {
13279 struct dwo_unit *dwo_cutu =
57d63ce2
DE
13280 lookup_dwo_unit_in_dwp (dwp_file, comp_dir,
13281 signature, is_debug_types);
80626a55
DE
13282
13283 if (dwo_cutu != NULL)
13284 {
b4f54984 13285 if (dwarf_read_debug)
80626a55
DE
13286 {
13287 fprintf_unfiltered (gdb_stdlog,
13288 "Virtual DWO %s %s found: @%s\n",
13289 kind, hex_string (signature),
13290 host_address_to_string (dwo_cutu));
13291 }
13292 return dwo_cutu;
13293 }
13294 }
13295 }
6a506a2d 13296 else
80626a55 13297 {
6a506a2d 13298 /* No DWP file, look for the DWO file. */
80626a55 13299
6a506a2d
DE
13300 dwo_file_slot = lookup_dwo_file_slot (dwo_name, comp_dir);
13301 if (*dwo_file_slot == NULL)
80626a55 13302 {
6a506a2d
DE
13303 /* Read in the file and build a table of the CUs/TUs it contains. */
13304 *dwo_file_slot = open_and_init_dwo_file (this_unit, dwo_name, comp_dir);
19c3d4c9 13305 }
6a506a2d 13306 /* NOTE: This will be NULL if unable to open the file. */
9a3c8263 13307 dwo_file = (struct dwo_file *) *dwo_file_slot;
3019eac3 13308
6a506a2d 13309 if (dwo_file != NULL)
19c3d4c9 13310 {
6a506a2d
DE
13311 struct dwo_unit *dwo_cutu = NULL;
13312
13313 if (is_debug_types && dwo_file->tus)
13314 {
13315 struct dwo_unit find_dwo_cutu;
13316
13317 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
13318 find_dwo_cutu.signature = signature;
9a3c8263
SM
13319 dwo_cutu
13320 = (struct dwo_unit *) htab_find (dwo_file->tus, &find_dwo_cutu);
6a506a2d 13321 }
33c5cd75 13322 else if (!is_debug_types && dwo_file->cus)
80626a55 13323 {
33c5cd75
DB
13324 struct dwo_unit find_dwo_cutu;
13325
13326 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
13327 find_dwo_cutu.signature = signature;
13328 dwo_cutu = (struct dwo_unit *)htab_find (dwo_file->cus,
13329 &find_dwo_cutu);
6a506a2d
DE
13330 }
13331
13332 if (dwo_cutu != NULL)
13333 {
b4f54984 13334 if (dwarf_read_debug)
6a506a2d
DE
13335 {
13336 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) found: @%s\n",
13337 kind, dwo_name, hex_string (signature),
13338 host_address_to_string (dwo_cutu));
13339 }
13340 return dwo_cutu;
80626a55
DE
13341 }
13342 }
2e276125 13343 }
9cdd5dbd 13344
80626a55
DE
13345 /* We didn't find it. This could mean a dwo_id mismatch, or
13346 someone deleted the DWO/DWP file, or the search path isn't set up
13347 correctly to find the file. */
13348
b4f54984 13349 if (dwarf_read_debug)
80626a55
DE
13350 {
13351 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) not found\n",
13352 kind, dwo_name, hex_string (signature));
13353 }
3019eac3 13354
6656a72d
DE
13355 /* This is a warning and not a complaint because it can be caused by
13356 pilot error (e.g., user accidentally deleting the DWO). */
43942612
DE
13357 {
13358 /* Print the name of the DWP file if we looked there, helps the user
13359 better diagnose the problem. */
791afaa2 13360 std::string dwp_text;
43942612
DE
13361
13362 if (dwp_file != NULL)
791afaa2
TT
13363 dwp_text = string_printf (" [in DWP file %s]",
13364 lbasename (dwp_file->name));
43942612
DE
13365
13366 warning (_("Could not find DWO %s %s(%s)%s referenced by %s at offset 0x%x"
13367 " [in module %s]"),
13368 kind, dwo_name, hex_string (signature),
791afaa2 13369 dwp_text.c_str (),
43942612 13370 this_unit->is_debug_types ? "TU" : "CU",
9c541725 13371 to_underlying (this_unit->sect_off), objfile_name (objfile));
43942612 13372 }
3019eac3 13373 return NULL;
5fb290d7
DJ
13374}
13375
80626a55
DE
13376/* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
13377 See lookup_dwo_cutu_unit for details. */
13378
13379static struct dwo_unit *
13380lookup_dwo_comp_unit (struct dwarf2_per_cu_data *this_cu,
13381 const char *dwo_name, const char *comp_dir,
13382 ULONGEST signature)
13383{
13384 return lookup_dwo_cutu (this_cu, dwo_name, comp_dir, signature, 0);
13385}
13386
13387/* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
13388 See lookup_dwo_cutu_unit for details. */
13389
13390static struct dwo_unit *
13391lookup_dwo_type_unit (struct signatured_type *this_tu,
13392 const char *dwo_name, const char *comp_dir)
13393{
13394 return lookup_dwo_cutu (&this_tu->per_cu, dwo_name, comp_dir, this_tu->signature, 1);
13395}
13396
89e63ee4
DE
13397/* Traversal function for queue_and_load_all_dwo_tus. */
13398
13399static int
13400queue_and_load_dwo_tu (void **slot, void *info)
13401{
13402 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
13403 struct dwarf2_per_cu_data *per_cu = (struct dwarf2_per_cu_data *) info;
13404 ULONGEST signature = dwo_unit->signature;
13405 struct signatured_type *sig_type =
13406 lookup_dwo_signatured_type (per_cu->cu, signature);
13407
13408 if (sig_type != NULL)
13409 {
13410 struct dwarf2_per_cu_data *sig_cu = &sig_type->per_cu;
13411
13412 /* We pass NULL for DEPENDENT_CU because we don't yet know if there's
13413 a real dependency of PER_CU on SIG_TYPE. That is detected later
13414 while processing PER_CU. */
13415 if (maybe_queue_comp_unit (NULL, sig_cu, per_cu->cu->language))
13416 load_full_type_unit (sig_cu);
13417 VEC_safe_push (dwarf2_per_cu_ptr, per_cu->imported_symtabs, sig_cu);
13418 }
13419
13420 return 1;
13421}
13422
13423/* Queue all TUs contained in the DWO of PER_CU to be read in.
13424 The DWO may have the only definition of the type, though it may not be
13425 referenced anywhere in PER_CU. Thus we have to load *all* its TUs.
13426 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
13427
13428static void
13429queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *per_cu)
13430{
13431 struct dwo_unit *dwo_unit;
13432 struct dwo_file *dwo_file;
13433
13434 gdb_assert (!per_cu->is_debug_types);
13435 gdb_assert (get_dwp_file () == NULL);
13436 gdb_assert (per_cu->cu != NULL);
13437
13438 dwo_unit = per_cu->cu->dwo_unit;
13439 gdb_assert (dwo_unit != NULL);
13440
13441 dwo_file = dwo_unit->dwo_file;
13442 if (dwo_file->tus != NULL)
13443 htab_traverse_noresize (dwo_file->tus, queue_and_load_dwo_tu, per_cu);
13444}
13445
3019eac3
DE
13446/* Free all resources associated with DWO_FILE.
13447 Close the DWO file and munmap the sections.
13448 All memory should be on the objfile obstack. */
348e048f
DE
13449
13450static void
3019eac3 13451free_dwo_file (struct dwo_file *dwo_file, struct objfile *objfile)
348e048f 13452{
348e048f 13453
5c6fa7ab 13454 /* Note: dbfd is NULL for virtual DWO files. */
80626a55 13455 gdb_bfd_unref (dwo_file->dbfd);
348e048f 13456
3019eac3
DE
13457 VEC_free (dwarf2_section_info_def, dwo_file->sections.types);
13458}
348e048f 13459
3019eac3 13460/* Wrapper for free_dwo_file for use in cleanups. */
348e048f 13461
3019eac3
DE
13462static void
13463free_dwo_file_cleanup (void *arg)
13464{
13465 struct dwo_file *dwo_file = (struct dwo_file *) arg;
13466 struct objfile *objfile = dwarf2_per_objfile->objfile;
348e048f 13467
3019eac3
DE
13468 free_dwo_file (dwo_file, objfile);
13469}
348e048f 13470
3019eac3 13471/* Traversal function for free_dwo_files. */
2ab95328 13472
3019eac3
DE
13473static int
13474free_dwo_file_from_slot (void **slot, void *info)
13475{
13476 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
13477 struct objfile *objfile = (struct objfile *) info;
348e048f 13478
3019eac3 13479 free_dwo_file (dwo_file, objfile);
348e048f 13480
3019eac3
DE
13481 return 1;
13482}
348e048f 13483
3019eac3 13484/* Free all resources associated with DWO_FILES. */
348e048f 13485
3019eac3
DE
13486static void
13487free_dwo_files (htab_t dwo_files, struct objfile *objfile)
13488{
13489 htab_traverse_noresize (dwo_files, free_dwo_file_from_slot, objfile);
348e048f 13490}
3019eac3
DE
13491\f
13492/* Read in various DIEs. */
348e048f 13493
d389af10 13494/* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
3e43a32a
MS
13495 Inherit only the children of the DW_AT_abstract_origin DIE not being
13496 already referenced by DW_AT_abstract_origin from the children of the
13497 current DIE. */
d389af10
JK
13498
13499static void
13500inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
13501{
13502 struct die_info *child_die;
791afaa2 13503 sect_offset *offsetp;
d389af10
JK
13504 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
13505 struct die_info *origin_die;
13506 /* Iterator of the ORIGIN_DIE children. */
13507 struct die_info *origin_child_die;
d389af10 13508 struct attribute *attr;
cd02d79d
PA
13509 struct dwarf2_cu *origin_cu;
13510 struct pending **origin_previous_list_in_scope;
d389af10
JK
13511
13512 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13513 if (!attr)
13514 return;
13515
cd02d79d
PA
13516 /* Note that following die references may follow to a die in a
13517 different cu. */
13518
13519 origin_cu = cu;
13520 origin_die = follow_die_ref (die, attr, &origin_cu);
13521
13522 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
13523 symbols in. */
13524 origin_previous_list_in_scope = origin_cu->list_in_scope;
13525 origin_cu->list_in_scope = cu->list_in_scope;
13526
edb3359d
DJ
13527 if (die->tag != origin_die->tag
13528 && !(die->tag == DW_TAG_inlined_subroutine
13529 && origin_die->tag == DW_TAG_subprogram))
d389af10
JK
13530 complaint (&symfile_complaints,
13531 _("DIE 0x%x and its abstract origin 0x%x have different tags"),
9c541725
PA
13532 to_underlying (die->sect_off),
13533 to_underlying (origin_die->sect_off));
d389af10 13534
791afaa2 13535 std::vector<sect_offset> offsets;
d389af10 13536
3ea89b92
PMR
13537 for (child_die = die->child;
13538 child_die && child_die->tag;
13539 child_die = sibling_die (child_die))
13540 {
13541 struct die_info *child_origin_die;
13542 struct dwarf2_cu *child_origin_cu;
13543
13544 /* We are trying to process concrete instance entries:
216f72a1 13545 DW_TAG_call_site DIEs indeed have a DW_AT_abstract_origin tag, but
3ea89b92
PMR
13546 it's not relevant to our analysis here. i.e. detecting DIEs that are
13547 present in the abstract instance but not referenced in the concrete
13548 one. */
216f72a1
JK
13549 if (child_die->tag == DW_TAG_call_site
13550 || child_die->tag == DW_TAG_GNU_call_site)
3ea89b92
PMR
13551 continue;
13552
c38f313d
DJ
13553 /* For each CHILD_DIE, find the corresponding child of
13554 ORIGIN_DIE. If there is more than one layer of
13555 DW_AT_abstract_origin, follow them all; there shouldn't be,
13556 but GCC versions at least through 4.4 generate this (GCC PR
13557 40573). */
3ea89b92
PMR
13558 child_origin_die = child_die;
13559 child_origin_cu = cu;
c38f313d
DJ
13560 while (1)
13561 {
cd02d79d
PA
13562 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
13563 child_origin_cu);
c38f313d
DJ
13564 if (attr == NULL)
13565 break;
cd02d79d
PA
13566 child_origin_die = follow_die_ref (child_origin_die, attr,
13567 &child_origin_cu);
c38f313d
DJ
13568 }
13569
d389af10
JK
13570 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
13571 counterpart may exist. */
c38f313d 13572 if (child_origin_die != child_die)
d389af10 13573 {
edb3359d
DJ
13574 if (child_die->tag != child_origin_die->tag
13575 && !(child_die->tag == DW_TAG_inlined_subroutine
13576 && child_origin_die->tag == DW_TAG_subprogram))
d389af10
JK
13577 complaint (&symfile_complaints,
13578 _("Child DIE 0x%x and its abstract origin 0x%x have "
9c541725
PA
13579 "different tags"),
13580 to_underlying (child_die->sect_off),
13581 to_underlying (child_origin_die->sect_off));
c38f313d
DJ
13582 if (child_origin_die->parent != origin_die)
13583 complaint (&symfile_complaints,
13584 _("Child DIE 0x%x and its abstract origin 0x%x have "
9c541725
PA
13585 "different parents"),
13586 to_underlying (child_die->sect_off),
13587 to_underlying (child_origin_die->sect_off));
c38f313d 13588 else
791afaa2 13589 offsets.push_back (child_origin_die->sect_off);
d389af10 13590 }
d389af10 13591 }
791afaa2
TT
13592 std::sort (offsets.begin (), offsets.end ());
13593 sect_offset *offsets_end = offsets.data () + offsets.size ();
13594 for (offsetp = offsets.data () + 1; offsetp < offsets_end; offsetp++)
9c541725 13595 if (offsetp[-1] == *offsetp)
3e43a32a
MS
13596 complaint (&symfile_complaints,
13597 _("Multiple children of DIE 0x%x refer "
13598 "to DIE 0x%x as their abstract origin"),
9c541725 13599 to_underlying (die->sect_off), to_underlying (*offsetp));
d389af10 13600
791afaa2 13601 offsetp = offsets.data ();
d389af10
JK
13602 origin_child_die = origin_die->child;
13603 while (origin_child_die && origin_child_die->tag)
13604 {
13605 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
b64f50a1 13606 while (offsetp < offsets_end
9c541725 13607 && *offsetp < origin_child_die->sect_off)
d389af10 13608 offsetp++;
b64f50a1 13609 if (offsetp >= offsets_end
9c541725 13610 || *offsetp > origin_child_die->sect_off)
d389af10 13611 {
adde2bff
DE
13612 /* Found that ORIGIN_CHILD_DIE is really not referenced.
13613 Check whether we're already processing ORIGIN_CHILD_DIE.
13614 This can happen with mutually referenced abstract_origins.
13615 PR 16581. */
13616 if (!origin_child_die->in_process)
13617 process_die (origin_child_die, origin_cu);
d389af10
JK
13618 }
13619 origin_child_die = sibling_die (origin_child_die);
13620 }
cd02d79d 13621 origin_cu->list_in_scope = origin_previous_list_in_scope;
d389af10
JK
13622}
13623
c906108c 13624static void
e7c27a73 13625read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 13626{
e7c27a73 13627 struct objfile *objfile = cu->objfile;
3e29f34a 13628 struct gdbarch *gdbarch = get_objfile_arch (objfile);
fe978cb0 13629 struct context_stack *newobj;
c906108c
SS
13630 CORE_ADDR lowpc;
13631 CORE_ADDR highpc;
13632 struct die_info *child_die;
edb3359d 13633 struct attribute *attr, *call_line, *call_file;
15d034d0 13634 const char *name;
e142c38c 13635 CORE_ADDR baseaddr;
801e3a5b 13636 struct block *block;
edb3359d 13637 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
2f4732b0 13638 std::vector<struct symbol *> template_args;
34eaf542 13639 struct template_symbol *templ_func = NULL;
edb3359d
DJ
13640
13641 if (inlined_func)
13642 {
13643 /* If we do not have call site information, we can't show the
13644 caller of this inlined function. That's too confusing, so
13645 only use the scope for local variables. */
13646 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
13647 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
13648 if (call_line == NULL || call_file == NULL)
13649 {
13650 read_lexical_block_scope (die, cu);
13651 return;
13652 }
13653 }
c906108c 13654
e142c38c
DJ
13655 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
13656
94af9270 13657 name = dwarf2_name (die, cu);
c906108c 13658
e8d05480
JB
13659 /* Ignore functions with missing or empty names. These are actually
13660 illegal according to the DWARF standard. */
13661 if (name == NULL)
13662 {
13663 complaint (&symfile_complaints,
b64f50a1 13664 _("missing name for subprogram DIE at %d"),
9c541725 13665 to_underlying (die->sect_off));
e8d05480
JB
13666 return;
13667 }
13668
13669 /* Ignore functions with missing or invalid low and high pc attributes. */
3a2b436a 13670 if (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL)
e385593e 13671 <= PC_BOUNDS_INVALID)
e8d05480 13672 {
ae4d0c03
PM
13673 attr = dwarf2_attr (die, DW_AT_external, cu);
13674 if (!attr || !DW_UNSND (attr))
13675 complaint (&symfile_complaints,
3e43a32a
MS
13676 _("cannot get low and high bounds "
13677 "for subprogram DIE at %d"),
9c541725 13678 to_underlying (die->sect_off));
e8d05480
JB
13679 return;
13680 }
c906108c 13681
3e29f34a
MR
13682 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13683 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
c906108c 13684
34eaf542
TT
13685 /* If we have any template arguments, then we must allocate a
13686 different sort of symbol. */
13687 for (child_die = die->child; child_die; child_die = sibling_die (child_die))
13688 {
13689 if (child_die->tag == DW_TAG_template_type_param
13690 || child_die->tag == DW_TAG_template_value_param)
13691 {
e623cf5d 13692 templ_func = allocate_template_symbol (objfile);
cf724bc9 13693 templ_func->subclass = SYMBOL_TEMPLATE;
34eaf542
TT
13694 break;
13695 }
13696 }
13697
fe978cb0
PA
13698 newobj = push_context (0, lowpc);
13699 newobj->name = new_symbol_full (die, read_type_die (die, cu), cu,
34eaf542 13700 (struct symbol *) templ_func);
4c2df51b 13701
4cecd739
DJ
13702 /* If there is a location expression for DW_AT_frame_base, record
13703 it. */
e142c38c 13704 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
4c2df51b 13705 if (attr)
fe978cb0 13706 dwarf2_symbol_mark_computed (attr, newobj->name, cu, 1);
4c2df51b 13707
63e43d3a
PMR
13708 /* If there is a location for the static link, record it. */
13709 newobj->static_link = NULL;
13710 attr = dwarf2_attr (die, DW_AT_static_link, cu);
13711 if (attr)
13712 {
224c3ddb
SM
13713 newobj->static_link
13714 = XOBNEW (&objfile->objfile_obstack, struct dynamic_prop);
63e43d3a
PMR
13715 attr_to_dynamic_prop (attr, die, cu, newobj->static_link);
13716 }
13717
e142c38c 13718 cu->list_in_scope = &local_symbols;
c906108c 13719
639d11d3 13720 if (die->child != NULL)
c906108c 13721 {
639d11d3 13722 child_die = die->child;
c906108c
SS
13723 while (child_die && child_die->tag)
13724 {
34eaf542
TT
13725 if (child_die->tag == DW_TAG_template_type_param
13726 || child_die->tag == DW_TAG_template_value_param)
13727 {
13728 struct symbol *arg = new_symbol (child_die, NULL, cu);
13729
f1078f66 13730 if (arg != NULL)
2f4732b0 13731 template_args.push_back (arg);
34eaf542
TT
13732 }
13733 else
13734 process_die (child_die, cu);
c906108c
SS
13735 child_die = sibling_die (child_die);
13736 }
13737 }
13738
d389af10
JK
13739 inherit_abstract_dies (die, cu);
13740
4a811a97
UW
13741 /* If we have a DW_AT_specification, we might need to import using
13742 directives from the context of the specification DIE. See the
13743 comment in determine_prefix. */
13744 if (cu->language == language_cplus
13745 && dwarf2_attr (die, DW_AT_specification, cu))
13746 {
13747 struct dwarf2_cu *spec_cu = cu;
13748 struct die_info *spec_die = die_specification (die, &spec_cu);
13749
13750 while (spec_die)
13751 {
13752 child_die = spec_die->child;
13753 while (child_die && child_die->tag)
13754 {
13755 if (child_die->tag == DW_TAG_imported_module)
13756 process_die (child_die, spec_cu);
13757 child_die = sibling_die (child_die);
13758 }
13759
13760 /* In some cases, GCC generates specification DIEs that
13761 themselves contain DW_AT_specification attributes. */
13762 spec_die = die_specification (spec_die, &spec_cu);
13763 }
13764 }
13765
fe978cb0 13766 newobj = pop_context ();
c906108c 13767 /* Make a block for the local symbols within. */
fe978cb0 13768 block = finish_block (newobj->name, &local_symbols, newobj->old_blocks,
63e43d3a 13769 newobj->static_link, lowpc, highpc);
801e3a5b 13770
df8a16a1 13771 /* For C++, set the block's scope. */
45280282
IB
13772 if ((cu->language == language_cplus
13773 || cu->language == language_fortran
c44af4eb
TT
13774 || cu->language == language_d
13775 || cu->language == language_rust)
4d4ec4e5 13776 && cu->processing_has_namespace_info)
195a3f6c
TT
13777 block_set_scope (block, determine_prefix (die, cu),
13778 &objfile->objfile_obstack);
df8a16a1 13779
801e3a5b
JB
13780 /* If we have address ranges, record them. */
13781 dwarf2_record_block_ranges (die, block, baseaddr, cu);
6e70227d 13782
fe978cb0 13783 gdbarch_make_symbol_special (gdbarch, newobj->name, objfile);
3e29f34a 13784
34eaf542 13785 /* Attach template arguments to function. */
2f4732b0 13786 if (!template_args.empty ())
34eaf542
TT
13787 {
13788 gdb_assert (templ_func != NULL);
13789
2f4732b0 13790 templ_func->n_template_arguments = template_args.size ();
34eaf542 13791 templ_func->template_arguments
8d749320
SM
13792 = XOBNEWVEC (&objfile->objfile_obstack, struct symbol *,
13793 templ_func->n_template_arguments);
34eaf542 13794 memcpy (templ_func->template_arguments,
2f4732b0 13795 template_args.data (),
34eaf542 13796 (templ_func->n_template_arguments * sizeof (struct symbol *)));
34eaf542
TT
13797 }
13798
208d8187
JB
13799 /* In C++, we can have functions nested inside functions (e.g., when
13800 a function declares a class that has methods). This means that
13801 when we finish processing a function scope, we may need to go
13802 back to building a containing block's symbol lists. */
fe978cb0 13803 local_symbols = newobj->locals;
22cee43f 13804 local_using_directives = newobj->local_using_directives;
208d8187 13805
921e78cf
JB
13806 /* If we've finished processing a top-level function, subsequent
13807 symbols go in the file symbol list. */
13808 if (outermost_context_p ())
e142c38c 13809 cu->list_in_scope = &file_symbols;
c906108c
SS
13810}
13811
13812/* Process all the DIES contained within a lexical block scope. Start
13813 a new scope, process the dies, and then close the scope. */
13814
13815static void
e7c27a73 13816read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 13817{
e7c27a73 13818 struct objfile *objfile = cu->objfile;
3e29f34a 13819 struct gdbarch *gdbarch = get_objfile_arch (objfile);
fe978cb0 13820 struct context_stack *newobj;
c906108c
SS
13821 CORE_ADDR lowpc, highpc;
13822 struct die_info *child_die;
e142c38c
DJ
13823 CORE_ADDR baseaddr;
13824
13825 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c
SS
13826
13827 /* Ignore blocks with missing or invalid low and high pc attributes. */
af34e669
DJ
13828 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
13829 as multiple lexical blocks? Handling children in a sane way would
6e70227d 13830 be nasty. Might be easier to properly extend generic blocks to
af34e669 13831 describe ranges. */
e385593e
JK
13832 switch (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
13833 {
13834 case PC_BOUNDS_NOT_PRESENT:
13835 /* DW_TAG_lexical_block has no attributes, process its children as if
13836 there was no wrapping by that DW_TAG_lexical_block.
13837 GCC does no longer produces such DWARF since GCC r224161. */
13838 for (child_die = die->child;
13839 child_die != NULL && child_die->tag;
13840 child_die = sibling_die (child_die))
13841 process_die (child_die, cu);
13842 return;
13843 case PC_BOUNDS_INVALID:
13844 return;
13845 }
3e29f34a
MR
13846 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13847 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
c906108c
SS
13848
13849 push_context (0, lowpc);
639d11d3 13850 if (die->child != NULL)
c906108c 13851 {
639d11d3 13852 child_die = die->child;
c906108c
SS
13853 while (child_die && child_die->tag)
13854 {
e7c27a73 13855 process_die (child_die, cu);
c906108c
SS
13856 child_die = sibling_die (child_die);
13857 }
13858 }
3ea89b92 13859 inherit_abstract_dies (die, cu);
fe978cb0 13860 newobj = pop_context ();
c906108c 13861
22cee43f 13862 if (local_symbols != NULL || local_using_directives != NULL)
c906108c 13863 {
801e3a5b 13864 struct block *block
63e43d3a 13865 = finish_block (0, &local_symbols, newobj->old_blocks, NULL,
fe978cb0 13866 newobj->start_addr, highpc);
801e3a5b
JB
13867
13868 /* Note that recording ranges after traversing children, as we
13869 do here, means that recording a parent's ranges entails
13870 walking across all its children's ranges as they appear in
13871 the address map, which is quadratic behavior.
13872
13873 It would be nicer to record the parent's ranges before
13874 traversing its children, simply overriding whatever you find
13875 there. But since we don't even decide whether to create a
13876 block until after we've traversed its children, that's hard
13877 to do. */
13878 dwarf2_record_block_ranges (die, block, baseaddr, cu);
c906108c 13879 }
fe978cb0 13880 local_symbols = newobj->locals;
22cee43f 13881 local_using_directives = newobj->local_using_directives;
c906108c
SS
13882}
13883
216f72a1 13884/* Read in DW_TAG_call_site and insert it to CU->call_site_htab. */
96408a79
SA
13885
13886static void
13887read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
13888{
13889 struct objfile *objfile = cu->objfile;
13890 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13891 CORE_ADDR pc, baseaddr;
13892 struct attribute *attr;
13893 struct call_site *call_site, call_site_local;
13894 void **slot;
13895 int nparams;
13896 struct die_info *child_die;
13897
13898 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
13899
216f72a1
JK
13900 attr = dwarf2_attr (die, DW_AT_call_return_pc, cu);
13901 if (attr == NULL)
13902 {
13903 /* This was a pre-DWARF-5 GNU extension alias
13904 for DW_AT_call_return_pc. */
13905 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
13906 }
96408a79
SA
13907 if (!attr)
13908 {
13909 complaint (&symfile_complaints,
216f72a1 13910 _("missing DW_AT_call_return_pc for DW_TAG_call_site "
96408a79 13911 "DIE 0x%x [in module %s]"),
9c541725 13912 to_underlying (die->sect_off), objfile_name (objfile));
96408a79
SA
13913 return;
13914 }
31aa7e4e 13915 pc = attr_value_as_address (attr) + baseaddr;
3e29f34a 13916 pc = gdbarch_adjust_dwarf2_addr (gdbarch, pc);
96408a79
SA
13917
13918 if (cu->call_site_htab == NULL)
13919 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
13920 NULL, &objfile->objfile_obstack,
13921 hashtab_obstack_allocate, NULL);
13922 call_site_local.pc = pc;
13923 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
13924 if (*slot != NULL)
13925 {
13926 complaint (&symfile_complaints,
216f72a1 13927 _("Duplicate PC %s for DW_TAG_call_site "
96408a79 13928 "DIE 0x%x [in module %s]"),
9c541725 13929 paddress (gdbarch, pc), to_underlying (die->sect_off),
4262abfb 13930 objfile_name (objfile));
96408a79
SA
13931 return;
13932 }
13933
13934 /* Count parameters at the caller. */
13935
13936 nparams = 0;
13937 for (child_die = die->child; child_die && child_die->tag;
13938 child_die = sibling_die (child_die))
13939 {
216f72a1
JK
13940 if (child_die->tag != DW_TAG_call_site_parameter
13941 && child_die->tag != DW_TAG_GNU_call_site_parameter)
96408a79
SA
13942 {
13943 complaint (&symfile_complaints,
216f72a1
JK
13944 _("Tag %d is not DW_TAG_call_site_parameter in "
13945 "DW_TAG_call_site child DIE 0x%x [in module %s]"),
9c541725 13946 child_die->tag, to_underlying (child_die->sect_off),
4262abfb 13947 objfile_name (objfile));
96408a79
SA
13948 continue;
13949 }
13950
13951 nparams++;
13952 }
13953
224c3ddb
SM
13954 call_site
13955 = ((struct call_site *)
13956 obstack_alloc (&objfile->objfile_obstack,
13957 sizeof (*call_site)
13958 + (sizeof (*call_site->parameter) * (nparams - 1))));
96408a79
SA
13959 *slot = call_site;
13960 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
13961 call_site->pc = pc;
13962
216f72a1
JK
13963 if (dwarf2_flag_true_p (die, DW_AT_call_tail_call, cu)
13964 || dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
96408a79
SA
13965 {
13966 struct die_info *func_die;
13967
13968 /* Skip also over DW_TAG_inlined_subroutine. */
13969 for (func_die = die->parent;
13970 func_die && func_die->tag != DW_TAG_subprogram
13971 && func_die->tag != DW_TAG_subroutine_type;
13972 func_die = func_die->parent);
13973
216f72a1
JK
13974 /* DW_AT_call_all_calls is a superset
13975 of DW_AT_call_all_tail_calls. */
96408a79 13976 if (func_die
216f72a1 13977 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_calls, cu)
96408a79 13978 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
216f72a1 13979 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_tail_calls, cu)
96408a79
SA
13980 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
13981 {
13982 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
13983 not complete. But keep CALL_SITE for look ups via call_site_htab,
13984 both the initial caller containing the real return address PC and
13985 the final callee containing the current PC of a chain of tail
13986 calls do not need to have the tail call list complete. But any
13987 function candidate for a virtual tail call frame searched via
13988 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
13989 determined unambiguously. */
13990 }
13991 else
13992 {
13993 struct type *func_type = NULL;
13994
13995 if (func_die)
13996 func_type = get_die_type (func_die, cu);
13997 if (func_type != NULL)
13998 {
13999 gdb_assert (TYPE_CODE (func_type) == TYPE_CODE_FUNC);
14000
14001 /* Enlist this call site to the function. */
14002 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
14003 TYPE_TAIL_CALL_LIST (func_type) = call_site;
14004 }
14005 else
14006 complaint (&symfile_complaints,
216f72a1 14007 _("Cannot find function owning DW_TAG_call_site "
96408a79 14008 "DIE 0x%x [in module %s]"),
9c541725 14009 to_underlying (die->sect_off), objfile_name (objfile));
96408a79
SA
14010 }
14011 }
14012
216f72a1
JK
14013 attr = dwarf2_attr (die, DW_AT_call_target, cu);
14014 if (attr == NULL)
14015 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
14016 if (attr == NULL)
14017 attr = dwarf2_attr (die, DW_AT_call_origin, cu);
96408a79 14018 if (attr == NULL)
216f72a1
JK
14019 {
14020 /* This was a pre-DWARF-5 GNU extension alias for DW_AT_call_origin. */
14021 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
14022 }
96408a79
SA
14023 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
14024 if (!attr || (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0))
14025 /* Keep NULL DWARF_BLOCK. */;
14026 else if (attr_form_is_block (attr))
14027 {
14028 struct dwarf2_locexpr_baton *dlbaton;
14029
8d749320 14030 dlbaton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
96408a79
SA
14031 dlbaton->data = DW_BLOCK (attr)->data;
14032 dlbaton->size = DW_BLOCK (attr)->size;
14033 dlbaton->per_cu = cu->per_cu;
14034
14035 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
14036 }
7771576e 14037 else if (attr_form_is_ref (attr))
96408a79 14038 {
96408a79
SA
14039 struct dwarf2_cu *target_cu = cu;
14040 struct die_info *target_die;
14041
ac9ec31b 14042 target_die = follow_die_ref (die, attr, &target_cu);
96408a79
SA
14043 gdb_assert (target_cu->objfile == objfile);
14044 if (die_is_declaration (target_die, target_cu))
14045 {
7d45c7c3 14046 const char *target_physname;
9112db09
JK
14047
14048 /* Prefer the mangled name; otherwise compute the demangled one. */
73b9be8b 14049 target_physname = dw2_linkage_name (target_die, target_cu);
7d45c7c3 14050 if (target_physname == NULL)
9112db09 14051 target_physname = dwarf2_physname (NULL, target_die, target_cu);
96408a79
SA
14052 if (target_physname == NULL)
14053 complaint (&symfile_complaints,
216f72a1 14054 _("DW_AT_call_target target DIE has invalid "
96408a79 14055 "physname, for referencing DIE 0x%x [in module %s]"),
9c541725 14056 to_underlying (die->sect_off), objfile_name (objfile));
96408a79 14057 else
7d455152 14058 SET_FIELD_PHYSNAME (call_site->target, target_physname);
96408a79
SA
14059 }
14060 else
14061 {
14062 CORE_ADDR lowpc;
14063
14064 /* DW_AT_entry_pc should be preferred. */
3a2b436a 14065 if (dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL)
e385593e 14066 <= PC_BOUNDS_INVALID)
96408a79 14067 complaint (&symfile_complaints,
216f72a1 14068 _("DW_AT_call_target target DIE has invalid "
96408a79 14069 "low pc, for referencing DIE 0x%x [in module %s]"),
9c541725 14070 to_underlying (die->sect_off), objfile_name (objfile));
96408a79 14071 else
3e29f34a
MR
14072 {
14073 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
14074 SET_FIELD_PHYSADDR (call_site->target, lowpc);
14075 }
96408a79
SA
14076 }
14077 }
14078 else
14079 complaint (&symfile_complaints,
216f72a1 14080 _("DW_TAG_call_site DW_AT_call_target is neither "
96408a79 14081 "block nor reference, for DIE 0x%x [in module %s]"),
9c541725 14082 to_underlying (die->sect_off), objfile_name (objfile));
96408a79
SA
14083
14084 call_site->per_cu = cu->per_cu;
14085
14086 for (child_die = die->child;
14087 child_die && child_die->tag;
14088 child_die = sibling_die (child_die))
14089 {
96408a79 14090 struct call_site_parameter *parameter;
1788b2d3 14091 struct attribute *loc, *origin;
96408a79 14092
216f72a1
JK
14093 if (child_die->tag != DW_TAG_call_site_parameter
14094 && child_die->tag != DW_TAG_GNU_call_site_parameter)
96408a79
SA
14095 {
14096 /* Already printed the complaint above. */
14097 continue;
14098 }
14099
14100 gdb_assert (call_site->parameter_count < nparams);
14101 parameter = &call_site->parameter[call_site->parameter_count];
14102
1788b2d3
JK
14103 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
14104 specifies DW_TAG_formal_parameter. Value of the data assumed for the
216f72a1 14105 register is contained in DW_AT_call_value. */
96408a79 14106
24c5c679 14107 loc = dwarf2_attr (child_die, DW_AT_location, cu);
216f72a1
JK
14108 origin = dwarf2_attr (child_die, DW_AT_call_parameter, cu);
14109 if (origin == NULL)
14110 {
14111 /* This was a pre-DWARF-5 GNU extension alias
14112 for DW_AT_call_parameter. */
14113 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
14114 }
7771576e 14115 if (loc == NULL && origin != NULL && attr_form_is_ref (origin))
1788b2d3 14116 {
1788b2d3 14117 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
9c541725
PA
14118
14119 sect_offset sect_off
14120 = (sect_offset) dwarf2_get_ref_die_offset (origin);
14121 if (!offset_in_cu_p (&cu->header, sect_off))
d76b7dbc
JK
14122 {
14123 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
14124 binding can be done only inside one CU. Such referenced DIE
14125 therefore cannot be even moved to DW_TAG_partial_unit. */
14126 complaint (&symfile_complaints,
216f72a1
JK
14127 _("DW_AT_call_parameter offset is not in CU for "
14128 "DW_TAG_call_site child DIE 0x%x [in module %s]"),
9c541725
PA
14129 to_underlying (child_die->sect_off),
14130 objfile_name (objfile));
d76b7dbc
JK
14131 continue;
14132 }
9c541725
PA
14133 parameter->u.param_cu_off
14134 = (cu_offset) (sect_off - cu->header.sect_off);
1788b2d3
JK
14135 }
14136 else if (loc == NULL || origin != NULL || !attr_form_is_block (loc))
96408a79
SA
14137 {
14138 complaint (&symfile_complaints,
14139 _("No DW_FORM_block* DW_AT_location for "
216f72a1 14140 "DW_TAG_call_site child DIE 0x%x [in module %s]"),
9c541725 14141 to_underlying (child_die->sect_off), objfile_name (objfile));
96408a79
SA
14142 continue;
14143 }
24c5c679 14144 else
96408a79 14145 {
24c5c679
JK
14146 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
14147 (DW_BLOCK (loc)->data, &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size]);
14148 if (parameter->u.dwarf_reg != -1)
14149 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
14150 else if (dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (loc)->data,
14151 &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size],
14152 &parameter->u.fb_offset))
14153 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
14154 else
14155 {
14156 complaint (&symfile_complaints,
14157 _("Only single DW_OP_reg or DW_OP_fbreg is supported "
14158 "for DW_FORM_block* DW_AT_location is supported for "
216f72a1 14159 "DW_TAG_call_site child DIE 0x%x "
24c5c679 14160 "[in module %s]"),
9c541725
PA
14161 to_underlying (child_die->sect_off),
14162 objfile_name (objfile));
24c5c679
JK
14163 continue;
14164 }
96408a79
SA
14165 }
14166
216f72a1
JK
14167 attr = dwarf2_attr (child_die, DW_AT_call_value, cu);
14168 if (attr == NULL)
14169 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
96408a79
SA
14170 if (!attr_form_is_block (attr))
14171 {
14172 complaint (&symfile_complaints,
216f72a1
JK
14173 _("No DW_FORM_block* DW_AT_call_value for "
14174 "DW_TAG_call_site child DIE 0x%x [in module %s]"),
9c541725
PA
14175 to_underlying (child_die->sect_off),
14176 objfile_name (objfile));
96408a79
SA
14177 continue;
14178 }
14179 parameter->value = DW_BLOCK (attr)->data;
14180 parameter->value_size = DW_BLOCK (attr)->size;
14181
14182 /* Parameters are not pre-cleared by memset above. */
14183 parameter->data_value = NULL;
14184 parameter->data_value_size = 0;
14185 call_site->parameter_count++;
14186
216f72a1
JK
14187 attr = dwarf2_attr (child_die, DW_AT_call_data_value, cu);
14188 if (attr == NULL)
14189 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
96408a79
SA
14190 if (attr)
14191 {
14192 if (!attr_form_is_block (attr))
14193 complaint (&symfile_complaints,
216f72a1
JK
14194 _("No DW_FORM_block* DW_AT_call_data_value for "
14195 "DW_TAG_call_site child DIE 0x%x [in module %s]"),
9c541725
PA
14196 to_underlying (child_die->sect_off),
14197 objfile_name (objfile));
96408a79
SA
14198 else
14199 {
14200 parameter->data_value = DW_BLOCK (attr)->data;
14201 parameter->data_value_size = DW_BLOCK (attr)->size;
14202 }
14203 }
14204 }
14205}
14206
71a3c369
TT
14207/* Helper function for read_variable. If DIE represents a virtual
14208 table, then return the type of the concrete object that is
14209 associated with the virtual table. Otherwise, return NULL. */
14210
14211static struct type *
14212rust_containing_type (struct die_info *die, struct dwarf2_cu *cu)
14213{
14214 struct attribute *attr = dwarf2_attr (die, DW_AT_type, cu);
14215 if (attr == NULL)
14216 return NULL;
14217
14218 /* Find the type DIE. */
14219 struct die_info *type_die = NULL;
14220 struct dwarf2_cu *type_cu = cu;
14221
14222 if (attr_form_is_ref (attr))
14223 type_die = follow_die_ref (die, attr, &type_cu);
14224 if (type_die == NULL)
14225 return NULL;
14226
14227 if (dwarf2_attr (type_die, DW_AT_containing_type, type_cu) == NULL)
14228 return NULL;
14229 return die_containing_type (type_die, type_cu);
14230}
14231
14232/* Read a variable (DW_TAG_variable) DIE and create a new symbol. */
14233
14234static void
14235read_variable (struct die_info *die, struct dwarf2_cu *cu)
14236{
14237 struct rust_vtable_symbol *storage = NULL;
14238
14239 if (cu->language == language_rust)
14240 {
14241 struct type *containing_type = rust_containing_type (die, cu);
14242
14243 if (containing_type != NULL)
14244 {
14245 struct objfile *objfile = cu->objfile;
14246
14247 storage = OBSTACK_ZALLOC (&objfile->objfile_obstack,
14248 struct rust_vtable_symbol);
14249 initialize_objfile_symbol (storage);
14250 storage->concrete_type = containing_type;
cf724bc9 14251 storage->subclass = SYMBOL_RUST_VTABLE;
71a3c369
TT
14252 }
14253 }
14254
14255 new_symbol_full (die, NULL, cu, storage);
14256}
14257
43988095
JK
14258/* Call CALLBACK from DW_AT_ranges attribute value OFFSET
14259 reading .debug_rnglists.
14260 Callback's type should be:
14261 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
14262 Return true if the attributes are present and valid, otherwise,
14263 return false. */
14264
14265template <typename Callback>
14266static bool
14267dwarf2_rnglists_process (unsigned offset, struct dwarf2_cu *cu,
14268 Callback &&callback)
14269{
14270 struct objfile *objfile = cu->objfile;
43988095 14271 bfd *obfd = objfile->obfd;
43988095
JK
14272 /* Base address selection entry. */
14273 CORE_ADDR base;
14274 int found_base;
43988095 14275 const gdb_byte *buffer;
43988095
JK
14276 CORE_ADDR baseaddr;
14277 bool overflow = false;
14278
14279 found_base = cu->base_known;
14280 base = cu->base_address;
14281
14282 dwarf2_read_section (objfile, &dwarf2_per_objfile->rnglists);
14283 if (offset >= dwarf2_per_objfile->rnglists.size)
14284 {
14285 complaint (&symfile_complaints,
14286 _("Offset %d out of bounds for DW_AT_ranges attribute"),
14287 offset);
14288 return false;
14289 }
14290 buffer = dwarf2_per_objfile->rnglists.buffer + offset;
14291
14292 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
14293
14294 while (1)
14295 {
7814882a
JK
14296 /* Initialize it due to a false compiler warning. */
14297 CORE_ADDR range_beginning = 0, range_end = 0;
43988095
JK
14298 const gdb_byte *buf_end = (dwarf2_per_objfile->rnglists.buffer
14299 + dwarf2_per_objfile->rnglists.size);
14300 unsigned int bytes_read;
14301
14302 if (buffer == buf_end)
14303 {
14304 overflow = true;
14305 break;
14306 }
14307 const auto rlet = static_cast<enum dwarf_range_list_entry>(*buffer++);
14308 switch (rlet)
14309 {
14310 case DW_RLE_end_of_list:
14311 break;
14312 case DW_RLE_base_address:
14313 if (buffer + cu->header.addr_size > buf_end)
14314 {
14315 overflow = true;
14316 break;
14317 }
14318 base = read_address (obfd, buffer, cu, &bytes_read);
14319 found_base = 1;
14320 buffer += bytes_read;
14321 break;
14322 case DW_RLE_start_length:
14323 if (buffer + cu->header.addr_size > buf_end)
14324 {
14325 overflow = true;
14326 break;
14327 }
14328 range_beginning = read_address (obfd, buffer, cu, &bytes_read);
14329 buffer += bytes_read;
14330 range_end = (range_beginning
14331 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
14332 buffer += bytes_read;
14333 if (buffer > buf_end)
14334 {
14335 overflow = true;
14336 break;
14337 }
14338 break;
14339 case DW_RLE_offset_pair:
14340 range_beginning = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14341 buffer += bytes_read;
14342 if (buffer > buf_end)
14343 {
14344 overflow = true;
14345 break;
14346 }
14347 range_end = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14348 buffer += bytes_read;
14349 if (buffer > buf_end)
14350 {
14351 overflow = true;
14352 break;
14353 }
14354 break;
14355 case DW_RLE_start_end:
14356 if (buffer + 2 * cu->header.addr_size > buf_end)
14357 {
14358 overflow = true;
14359 break;
14360 }
14361 range_beginning = read_address (obfd, buffer, cu, &bytes_read);
14362 buffer += bytes_read;
14363 range_end = read_address (obfd, buffer, cu, &bytes_read);
14364 buffer += bytes_read;
14365 break;
14366 default:
14367 complaint (&symfile_complaints,
14368 _("Invalid .debug_rnglists data (no base address)"));
14369 return false;
14370 }
14371 if (rlet == DW_RLE_end_of_list || overflow)
14372 break;
14373 if (rlet == DW_RLE_base_address)
14374 continue;
14375
14376 if (!found_base)
14377 {
14378 /* We have no valid base address for the ranges
14379 data. */
14380 complaint (&symfile_complaints,
14381 _("Invalid .debug_rnglists data (no base address)"));
14382 return false;
14383 }
14384
14385 if (range_beginning > range_end)
14386 {
14387 /* Inverted range entries are invalid. */
14388 complaint (&symfile_complaints,
14389 _("Invalid .debug_rnglists data (inverted range)"));
14390 return false;
14391 }
14392
14393 /* Empty range entries have no effect. */
14394 if (range_beginning == range_end)
14395 continue;
14396
14397 range_beginning += base;
14398 range_end += base;
14399
14400 /* A not-uncommon case of bad debug info.
14401 Don't pollute the addrmap with bad data. */
14402 if (range_beginning + baseaddr == 0
14403 && !dwarf2_per_objfile->has_section_at_zero)
14404 {
14405 complaint (&symfile_complaints,
14406 _(".debug_rnglists entry has start address of zero"
14407 " [in module %s]"), objfile_name (objfile));
14408 continue;
14409 }
14410
14411 callback (range_beginning, range_end);
14412 }
14413
14414 if (overflow)
14415 {
14416 complaint (&symfile_complaints,
14417 _("Offset %d is not terminated "
14418 "for DW_AT_ranges attribute"),
14419 offset);
14420 return false;
14421 }
14422
14423 return true;
14424}
14425
14426/* Call CALLBACK from DW_AT_ranges attribute value OFFSET reading .debug_ranges.
14427 Callback's type should be:
14428 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
5f46c5a5 14429 Return 1 if the attributes are present and valid, otherwise, return 0. */
43039443 14430
43988095 14431template <typename Callback>
43039443 14432static int
5f46c5a5 14433dwarf2_ranges_process (unsigned offset, struct dwarf2_cu *cu,
43988095 14434 Callback &&callback)
43039443
JK
14435{
14436 struct objfile *objfile = cu->objfile;
14437 struct comp_unit_head *cu_header = &cu->header;
14438 bfd *obfd = objfile->obfd;
14439 unsigned int addr_size = cu_header->addr_size;
14440 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
14441 /* Base address selection entry. */
14442 CORE_ADDR base;
14443 int found_base;
14444 unsigned int dummy;
d521ce57 14445 const gdb_byte *buffer;
ff013f42 14446 CORE_ADDR baseaddr;
43039443 14447
43988095
JK
14448 if (cu_header->version >= 5)
14449 return dwarf2_rnglists_process (offset, cu, callback);
14450
d00adf39
DE
14451 found_base = cu->base_known;
14452 base = cu->base_address;
43039443 14453
be391dca 14454 dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
dce234bc 14455 if (offset >= dwarf2_per_objfile->ranges.size)
43039443
JK
14456 {
14457 complaint (&symfile_complaints,
14458 _("Offset %d out of bounds for DW_AT_ranges attribute"),
14459 offset);
14460 return 0;
14461 }
dce234bc 14462 buffer = dwarf2_per_objfile->ranges.buffer + offset;
43039443 14463
e7030f15 14464 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
ff013f42 14465
43039443
JK
14466 while (1)
14467 {
14468 CORE_ADDR range_beginning, range_end;
14469
14470 range_beginning = read_address (obfd, buffer, cu, &dummy);
14471 buffer += addr_size;
14472 range_end = read_address (obfd, buffer, cu, &dummy);
14473 buffer += addr_size;
14474 offset += 2 * addr_size;
14475
14476 /* An end of list marker is a pair of zero addresses. */
14477 if (range_beginning == 0 && range_end == 0)
14478 /* Found the end of list entry. */
14479 break;
14480
14481 /* Each base address selection entry is a pair of 2 values.
14482 The first is the largest possible address, the second is
14483 the base address. Check for a base address here. */
14484 if ((range_beginning & mask) == mask)
14485 {
28d2bfb9
AB
14486 /* If we found the largest possible address, then we already
14487 have the base address in range_end. */
14488 base = range_end;
43039443
JK
14489 found_base = 1;
14490 continue;
14491 }
14492
14493 if (!found_base)
14494 {
14495 /* We have no valid base address for the ranges
14496 data. */
14497 complaint (&symfile_complaints,
14498 _("Invalid .debug_ranges data (no base address)"));
14499 return 0;
14500 }
14501
9277c30c
UW
14502 if (range_beginning > range_end)
14503 {
14504 /* Inverted range entries are invalid. */
14505 complaint (&symfile_complaints,
14506 _("Invalid .debug_ranges data (inverted range)"));
14507 return 0;
14508 }
14509
14510 /* Empty range entries have no effect. */
14511 if (range_beginning == range_end)
14512 continue;
14513
43039443
JK
14514 range_beginning += base;
14515 range_end += base;
14516
01093045
DE
14517 /* A not-uncommon case of bad debug info.
14518 Don't pollute the addrmap with bad data. */
14519 if (range_beginning + baseaddr == 0
14520 && !dwarf2_per_objfile->has_section_at_zero)
14521 {
14522 complaint (&symfile_complaints,
14523 _(".debug_ranges entry has start address of zero"
4262abfb 14524 " [in module %s]"), objfile_name (objfile));
01093045
DE
14525 continue;
14526 }
14527
5f46c5a5
JK
14528 callback (range_beginning, range_end);
14529 }
14530
14531 return 1;
14532}
14533
14534/* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
14535 Return 1 if the attributes are present and valid, otherwise, return 0.
14536 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
14537
14538static int
14539dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
14540 CORE_ADDR *high_return, struct dwarf2_cu *cu,
14541 struct partial_symtab *ranges_pst)
14542{
14543 struct objfile *objfile = cu->objfile;
14544 struct gdbarch *gdbarch = get_objfile_arch (objfile);
14545 const CORE_ADDR baseaddr = ANOFFSET (objfile->section_offsets,
14546 SECT_OFF_TEXT (objfile));
14547 int low_set = 0;
14548 CORE_ADDR low = 0;
14549 CORE_ADDR high = 0;
14550 int retval;
14551
14552 retval = dwarf2_ranges_process (offset, cu,
14553 [&] (CORE_ADDR range_beginning, CORE_ADDR range_end)
14554 {
9277c30c 14555 if (ranges_pst != NULL)
3e29f34a
MR
14556 {
14557 CORE_ADDR lowpc;
14558 CORE_ADDR highpc;
14559
14560 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch,
14561 range_beginning + baseaddr);
14562 highpc = gdbarch_adjust_dwarf2_addr (gdbarch,
14563 range_end + baseaddr);
14564 addrmap_set_empty (objfile->psymtabs_addrmap, lowpc, highpc - 1,
14565 ranges_pst);
14566 }
ff013f42 14567
43039443
JK
14568 /* FIXME: This is recording everything as a low-high
14569 segment of consecutive addresses. We should have a
14570 data structure for discontiguous block ranges
14571 instead. */
14572 if (! low_set)
14573 {
14574 low = range_beginning;
14575 high = range_end;
14576 low_set = 1;
14577 }
14578 else
14579 {
14580 if (range_beginning < low)
14581 low = range_beginning;
14582 if (range_end > high)
14583 high = range_end;
14584 }
5f46c5a5
JK
14585 });
14586 if (!retval)
14587 return 0;
43039443
JK
14588
14589 if (! low_set)
14590 /* If the first entry is an end-of-list marker, the range
14591 describes an empty scope, i.e. no instructions. */
14592 return 0;
14593
14594 if (low_return)
14595 *low_return = low;
14596 if (high_return)
14597 *high_return = high;
14598 return 1;
14599}
14600
3a2b436a
JK
14601/* Get low and high pc attributes from a die. See enum pc_bounds_kind
14602 definition for the return value. *LOWPC and *HIGHPC are set iff
e385593e 14603 neither PC_BOUNDS_NOT_PRESENT nor PC_BOUNDS_INVALID are returned. */
380bca97 14604
3a2b436a 14605static enum pc_bounds_kind
af34e669 14606dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
d85a05f0
DJ
14607 CORE_ADDR *highpc, struct dwarf2_cu *cu,
14608 struct partial_symtab *pst)
c906108c
SS
14609{
14610 struct attribute *attr;
91da1414 14611 struct attribute *attr_high;
af34e669
DJ
14612 CORE_ADDR low = 0;
14613 CORE_ADDR high = 0;
e385593e 14614 enum pc_bounds_kind ret;
c906108c 14615
91da1414
MW
14616 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14617 if (attr_high)
af34e669 14618 {
e142c38c 14619 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
af34e669 14620 if (attr)
91da1414 14621 {
31aa7e4e
JB
14622 low = attr_value_as_address (attr);
14623 high = attr_value_as_address (attr_high);
14624 if (cu->header.version >= 4 && attr_form_is_constant (attr_high))
14625 high += low;
91da1414 14626 }
af34e669
DJ
14627 else
14628 /* Found high w/o low attribute. */
e385593e 14629 return PC_BOUNDS_INVALID;
af34e669
DJ
14630
14631 /* Found consecutive range of addresses. */
3a2b436a 14632 ret = PC_BOUNDS_HIGH_LOW;
af34e669 14633 }
c906108c 14634 else
af34e669 14635 {
e142c38c 14636 attr = dwarf2_attr (die, DW_AT_ranges, cu);
af34e669
DJ
14637 if (attr != NULL)
14638 {
ab435259
DE
14639 /* DW_AT_ranges_base does not apply to DIEs from the DWO skeleton.
14640 We take advantage of the fact that DW_AT_ranges does not appear
14641 in DW_TAG_compile_unit of DWO files. */
14642 int need_ranges_base = die->tag != DW_TAG_compile_unit;
14643 unsigned int ranges_offset = (DW_UNSND (attr)
14644 + (need_ranges_base
14645 ? cu->ranges_base
14646 : 0));
2e3cf129 14647
af34e669 14648 /* Value of the DW_AT_ranges attribute is the offset in the
a604369a 14649 .debug_ranges section. */
2e3cf129 14650 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst))
e385593e 14651 return PC_BOUNDS_INVALID;
43039443 14652 /* Found discontinuous range of addresses. */
3a2b436a 14653 ret = PC_BOUNDS_RANGES;
af34e669 14654 }
e385593e
JK
14655 else
14656 return PC_BOUNDS_NOT_PRESENT;
af34e669 14657 }
c906108c 14658
9373cf26
JK
14659 /* read_partial_die has also the strict LOW < HIGH requirement. */
14660 if (high <= low)
e385593e 14661 return PC_BOUNDS_INVALID;
c906108c
SS
14662
14663 /* When using the GNU linker, .gnu.linkonce. sections are used to
14664 eliminate duplicate copies of functions and vtables and such.
14665 The linker will arbitrarily choose one and discard the others.
14666 The AT_*_pc values for such functions refer to local labels in
14667 these sections. If the section from that file was discarded, the
14668 labels are not in the output, so the relocs get a value of 0.
14669 If this is a discarded function, mark the pc bounds as invalid,
14670 so that GDB will ignore it. */
72dca2f5 14671 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
e385593e 14672 return PC_BOUNDS_INVALID;
c906108c
SS
14673
14674 *lowpc = low;
96408a79
SA
14675 if (highpc)
14676 *highpc = high;
af34e669 14677 return ret;
c906108c
SS
14678}
14679
b084d499
JB
14680/* Assuming that DIE represents a subprogram DIE or a lexical block, get
14681 its low and high PC addresses. Do nothing if these addresses could not
14682 be determined. Otherwise, set LOWPC to the low address if it is smaller,
14683 and HIGHPC to the high address if greater than HIGHPC. */
14684
14685static void
14686dwarf2_get_subprogram_pc_bounds (struct die_info *die,
14687 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14688 struct dwarf2_cu *cu)
14689{
14690 CORE_ADDR low, high;
14691 struct die_info *child = die->child;
14692
e385593e 14693 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL) >= PC_BOUNDS_RANGES)
b084d499 14694 {
325fac50
PA
14695 *lowpc = std::min (*lowpc, low);
14696 *highpc = std::max (*highpc, high);
b084d499
JB
14697 }
14698
14699 /* If the language does not allow nested subprograms (either inside
14700 subprograms or lexical blocks), we're done. */
14701 if (cu->language != language_ada)
14702 return;
6e70227d 14703
b084d499
JB
14704 /* Check all the children of the given DIE. If it contains nested
14705 subprograms, then check their pc bounds. Likewise, we need to
14706 check lexical blocks as well, as they may also contain subprogram
14707 definitions. */
14708 while (child && child->tag)
14709 {
14710 if (child->tag == DW_TAG_subprogram
14711 || child->tag == DW_TAG_lexical_block)
14712 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
14713 child = sibling_die (child);
14714 }
14715}
14716
fae299cd
DC
14717/* Get the low and high pc's represented by the scope DIE, and store
14718 them in *LOWPC and *HIGHPC. If the correct values can't be
14719 determined, set *LOWPC to -1 and *HIGHPC to 0. */
14720
14721static void
14722get_scope_pc_bounds (struct die_info *die,
14723 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14724 struct dwarf2_cu *cu)
14725{
14726 CORE_ADDR best_low = (CORE_ADDR) -1;
14727 CORE_ADDR best_high = (CORE_ADDR) 0;
14728 CORE_ADDR current_low, current_high;
14729
3a2b436a 14730 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL)
e385593e 14731 >= PC_BOUNDS_RANGES)
fae299cd
DC
14732 {
14733 best_low = current_low;
14734 best_high = current_high;
14735 }
14736 else
14737 {
14738 struct die_info *child = die->child;
14739
14740 while (child && child->tag)
14741 {
14742 switch (child->tag) {
14743 case DW_TAG_subprogram:
b084d499 14744 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
fae299cd
DC
14745 break;
14746 case DW_TAG_namespace:
f55ee35c 14747 case DW_TAG_module:
fae299cd
DC
14748 /* FIXME: carlton/2004-01-16: Should we do this for
14749 DW_TAG_class_type/DW_TAG_structure_type, too? I think
14750 that current GCC's always emit the DIEs corresponding
14751 to definitions of methods of classes as children of a
14752 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
14753 the DIEs giving the declarations, which could be
14754 anywhere). But I don't see any reason why the
14755 standards says that they have to be there. */
14756 get_scope_pc_bounds (child, &current_low, &current_high, cu);
14757
14758 if (current_low != ((CORE_ADDR) -1))
14759 {
325fac50
PA
14760 best_low = std::min (best_low, current_low);
14761 best_high = std::max (best_high, current_high);
fae299cd
DC
14762 }
14763 break;
14764 default:
0963b4bd 14765 /* Ignore. */
fae299cd
DC
14766 break;
14767 }
14768
14769 child = sibling_die (child);
14770 }
14771 }
14772
14773 *lowpc = best_low;
14774 *highpc = best_high;
14775}
14776
801e3a5b
JB
14777/* Record the address ranges for BLOCK, offset by BASEADDR, as given
14778 in DIE. */
380bca97 14779
801e3a5b
JB
14780static void
14781dwarf2_record_block_ranges (struct die_info *die, struct block *block,
14782 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
14783{
bb5ed363 14784 struct objfile *objfile = cu->objfile;
3e29f34a 14785 struct gdbarch *gdbarch = get_objfile_arch (objfile);
801e3a5b 14786 struct attribute *attr;
91da1414 14787 struct attribute *attr_high;
801e3a5b 14788
91da1414
MW
14789 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14790 if (attr_high)
801e3a5b 14791 {
801e3a5b
JB
14792 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
14793 if (attr)
14794 {
31aa7e4e
JB
14795 CORE_ADDR low = attr_value_as_address (attr);
14796 CORE_ADDR high = attr_value_as_address (attr_high);
14797
14798 if (cu->header.version >= 4 && attr_form_is_constant (attr_high))
14799 high += low;
9a619af0 14800
3e29f34a
MR
14801 low = gdbarch_adjust_dwarf2_addr (gdbarch, low + baseaddr);
14802 high = gdbarch_adjust_dwarf2_addr (gdbarch, high + baseaddr);
14803 record_block_range (block, low, high - 1);
801e3a5b
JB
14804 }
14805 }
14806
14807 attr = dwarf2_attr (die, DW_AT_ranges, cu);
14808 if (attr)
14809 {
ab435259
DE
14810 /* DW_AT_ranges_base does not apply to DIEs from the DWO skeleton.
14811 We take advantage of the fact that DW_AT_ranges does not appear
14812 in DW_TAG_compile_unit of DWO files. */
14813 int need_ranges_base = die->tag != DW_TAG_compile_unit;
801e3a5b
JB
14814
14815 /* The value of the DW_AT_ranges attribute is the offset of the
14816 address range list in the .debug_ranges section. */
ab435259
DE
14817 unsigned long offset = (DW_UNSND (attr)
14818 + (need_ranges_base ? cu->ranges_base : 0));
d62bfeaf 14819 const gdb_byte *buffer;
801e3a5b
JB
14820
14821 /* For some target architectures, but not others, the
14822 read_address function sign-extends the addresses it returns.
14823 To recognize base address selection entries, we need a
14824 mask. */
14825 unsigned int addr_size = cu->header.addr_size;
14826 CORE_ADDR base_select_mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
14827
14828 /* The base address, to which the next pair is relative. Note
14829 that this 'base' is a DWARF concept: most entries in a range
14830 list are relative, to reduce the number of relocs against the
14831 debugging information. This is separate from this function's
14832 'baseaddr' argument, which GDB uses to relocate debugging
14833 information from a shared library based on the address at
14834 which the library was loaded. */
d00adf39
DE
14835 CORE_ADDR base = cu->base_address;
14836 int base_known = cu->base_known;
801e3a5b 14837
5f46c5a5
JK
14838 dwarf2_ranges_process (offset, cu,
14839 [&] (CORE_ADDR start, CORE_ADDR end)
14840 {
58fdfd2c
JK
14841 start += baseaddr;
14842 end += baseaddr;
5f46c5a5
JK
14843 start = gdbarch_adjust_dwarf2_addr (gdbarch, start);
14844 end = gdbarch_adjust_dwarf2_addr (gdbarch, end);
14845 record_block_range (block, start, end - 1);
14846 });
801e3a5b
JB
14847 }
14848}
14849
685b1105
JK
14850/* Check whether the producer field indicates either of GCC < 4.6, or the
14851 Intel C/C++ compiler, and cache the result in CU. */
60d5a603 14852
685b1105
JK
14853static void
14854check_producer (struct dwarf2_cu *cu)
60d5a603 14855{
38360086 14856 int major, minor;
60d5a603
JK
14857
14858 if (cu->producer == NULL)
14859 {
14860 /* For unknown compilers expect their behavior is DWARF version
14861 compliant.
14862
14863 GCC started to support .debug_types sections by -gdwarf-4 since
14864 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
14865 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
14866 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
14867 interpreted incorrectly by GDB now - GCC PR debug/48229. */
60d5a603 14868 }
b1ffba5a 14869 else if (producer_is_gcc (cu->producer, &major, &minor))
60d5a603 14870 {
38360086
MW
14871 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
14872 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
685b1105 14873 }
5230b05a
WT
14874 else if (producer_is_icc (cu->producer, &major, &minor))
14875 cu->producer_is_icc_lt_14 = major < 14;
685b1105
JK
14876 else
14877 {
14878 /* For other non-GCC compilers, expect their behavior is DWARF version
14879 compliant. */
60d5a603
JK
14880 }
14881
ba919b58 14882 cu->checked_producer = 1;
685b1105 14883}
ba919b58 14884
685b1105
JK
14885/* Check for GCC PR debug/45124 fix which is not present in any G++ version up
14886 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
14887 during 4.6.0 experimental. */
14888
14889static int
14890producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
14891{
14892 if (!cu->checked_producer)
14893 check_producer (cu);
14894
14895 return cu->producer_is_gxx_lt_4_6;
60d5a603
JK
14896}
14897
14898/* Return the default accessibility type if it is not overriden by
14899 DW_AT_accessibility. */
14900
14901static enum dwarf_access_attribute
14902dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
14903{
14904 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
14905 {
14906 /* The default DWARF 2 accessibility for members is public, the default
14907 accessibility for inheritance is private. */
14908
14909 if (die->tag != DW_TAG_inheritance)
14910 return DW_ACCESS_public;
14911 else
14912 return DW_ACCESS_private;
14913 }
14914 else
14915 {
14916 /* DWARF 3+ defines the default accessibility a different way. The same
14917 rules apply now for DW_TAG_inheritance as for the members and it only
14918 depends on the container kind. */
14919
14920 if (die->parent->tag == DW_TAG_class_type)
14921 return DW_ACCESS_private;
14922 else
14923 return DW_ACCESS_public;
14924 }
14925}
14926
74ac6d43
TT
14927/* Look for DW_AT_data_member_location. Set *OFFSET to the byte
14928 offset. If the attribute was not found return 0, otherwise return
14929 1. If it was found but could not properly be handled, set *OFFSET
14930 to 0. */
14931
14932static int
14933handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
14934 LONGEST *offset)
14935{
14936 struct attribute *attr;
14937
14938 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
14939 if (attr != NULL)
14940 {
14941 *offset = 0;
14942
14943 /* Note that we do not check for a section offset first here.
14944 This is because DW_AT_data_member_location is new in DWARF 4,
14945 so if we see it, we can assume that a constant form is really
14946 a constant and not a section offset. */
14947 if (attr_form_is_constant (attr))
14948 *offset = dwarf2_get_attr_constant_value (attr, 0);
14949 else if (attr_form_is_section_offset (attr))
14950 dwarf2_complex_location_expr_complaint ();
14951 else if (attr_form_is_block (attr))
14952 *offset = decode_locdesc (DW_BLOCK (attr), cu);
14953 else
14954 dwarf2_complex_location_expr_complaint ();
14955
14956 return 1;
14957 }
14958
14959 return 0;
14960}
14961
c906108c
SS
14962/* Add an aggregate field to the field list. */
14963
14964static void
107d2387 14965dwarf2_add_field (struct field_info *fip, struct die_info *die,
e7c27a73 14966 struct dwarf2_cu *cu)
6e70227d 14967{
e7c27a73 14968 struct objfile *objfile = cu->objfile;
5e2b427d 14969 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
14970 struct nextfield *new_field;
14971 struct attribute *attr;
14972 struct field *fp;
15d034d0 14973 const char *fieldname = "";
c906108c
SS
14974
14975 /* Allocate a new field list entry and link it in. */
8d749320 14976 new_field = XNEW (struct nextfield);
b8c9b27d 14977 make_cleanup (xfree, new_field);
c906108c 14978 memset (new_field, 0, sizeof (struct nextfield));
7d0ccb61
DJ
14979
14980 if (die->tag == DW_TAG_inheritance)
14981 {
14982 new_field->next = fip->baseclasses;
14983 fip->baseclasses = new_field;
14984 }
14985 else
14986 {
14987 new_field->next = fip->fields;
14988 fip->fields = new_field;
14989 }
c906108c
SS
14990 fip->nfields++;
14991
e142c38c 14992 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
c906108c
SS
14993 if (attr)
14994 new_field->accessibility = DW_UNSND (attr);
60d5a603
JK
14995 else
14996 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
c906108c
SS
14997 if (new_field->accessibility != DW_ACCESS_public)
14998 fip->non_public_fields = 1;
60d5a603 14999
e142c38c 15000 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
c906108c
SS
15001 if (attr)
15002 new_field->virtuality = DW_UNSND (attr);
60d5a603
JK
15003 else
15004 new_field->virtuality = DW_VIRTUALITY_none;
c906108c
SS
15005
15006 fp = &new_field->field;
a9a9bd0f 15007
e142c38c 15008 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
c906108c 15009 {
74ac6d43
TT
15010 LONGEST offset;
15011
a9a9bd0f 15012 /* Data member other than a C++ static data member. */
6e70227d 15013
c906108c 15014 /* Get type of field. */
e7c27a73 15015 fp->type = die_type (die, cu);
c906108c 15016
d6a843b5 15017 SET_FIELD_BITPOS (*fp, 0);
01ad7f36 15018
c906108c 15019 /* Get bit size of field (zero if none). */
e142c38c 15020 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
c906108c
SS
15021 if (attr)
15022 {
15023 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
15024 }
15025 else
15026 {
15027 FIELD_BITSIZE (*fp) = 0;
15028 }
15029
15030 /* Get bit offset of field. */
74ac6d43
TT
15031 if (handle_data_member_location (die, cu, &offset))
15032 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
e142c38c 15033 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
c906108c
SS
15034 if (attr)
15035 {
5e2b427d 15036 if (gdbarch_bits_big_endian (gdbarch))
c906108c
SS
15037 {
15038 /* For big endian bits, the DW_AT_bit_offset gives the
c5aa993b
JM
15039 additional bit offset from the MSB of the containing
15040 anonymous object to the MSB of the field. We don't
15041 have to do anything special since we don't need to
15042 know the size of the anonymous object. */
f41f5e61 15043 SET_FIELD_BITPOS (*fp, FIELD_BITPOS (*fp) + DW_UNSND (attr));
c906108c
SS
15044 }
15045 else
15046 {
15047 /* For little endian bits, compute the bit offset to the
c5aa993b
JM
15048 MSB of the anonymous object, subtract off the number of
15049 bits from the MSB of the field to the MSB of the
15050 object, and then subtract off the number of bits of
15051 the field itself. The result is the bit offset of
15052 the LSB of the field. */
c906108c
SS
15053 int anonymous_size;
15054 int bit_offset = DW_UNSND (attr);
15055
e142c38c 15056 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
15057 if (attr)
15058 {
15059 /* The size of the anonymous object containing
15060 the bit field is explicit, so use the
15061 indicated size (in bytes). */
15062 anonymous_size = DW_UNSND (attr);
15063 }
15064 else
15065 {
15066 /* The size of the anonymous object containing
15067 the bit field must be inferred from the type
15068 attribute of the data member containing the
15069 bit field. */
15070 anonymous_size = TYPE_LENGTH (fp->type);
15071 }
f41f5e61
PA
15072 SET_FIELD_BITPOS (*fp,
15073 (FIELD_BITPOS (*fp)
15074 + anonymous_size * bits_per_byte
15075 - bit_offset - FIELD_BITSIZE (*fp)));
c906108c
SS
15076 }
15077 }
da5b30da
AA
15078 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
15079 if (attr != NULL)
15080 SET_FIELD_BITPOS (*fp, (FIELD_BITPOS (*fp)
15081 + dwarf2_get_attr_constant_value (attr, 0)));
c906108c
SS
15082
15083 /* Get name of field. */
39cbfefa
DJ
15084 fieldname = dwarf2_name (die, cu);
15085 if (fieldname == NULL)
15086 fieldname = "";
d8151005
DJ
15087
15088 /* The name is already allocated along with this objfile, so we don't
15089 need to duplicate it for the type. */
15090 fp->name = fieldname;
c906108c
SS
15091
15092 /* Change accessibility for artificial fields (e.g. virtual table
c5aa993b 15093 pointer or virtual base class pointer) to private. */
e142c38c 15094 if (dwarf2_attr (die, DW_AT_artificial, cu))
c906108c 15095 {
d48cc9dd 15096 FIELD_ARTIFICIAL (*fp) = 1;
c906108c
SS
15097 new_field->accessibility = DW_ACCESS_private;
15098 fip->non_public_fields = 1;
15099 }
15100 }
a9a9bd0f 15101 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
c906108c 15102 {
a9a9bd0f
DC
15103 /* C++ static member. */
15104
15105 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
15106 is a declaration, but all versions of G++ as of this writing
15107 (so through at least 3.2.1) incorrectly generate
15108 DW_TAG_variable tags. */
6e70227d 15109
ff355380 15110 const char *physname;
c906108c 15111
a9a9bd0f 15112 /* Get name of field. */
39cbfefa
DJ
15113 fieldname = dwarf2_name (die, cu);
15114 if (fieldname == NULL)
c906108c
SS
15115 return;
15116
254e6b9e 15117 attr = dwarf2_attr (die, DW_AT_const_value, cu);
3863f96c
DE
15118 if (attr
15119 /* Only create a symbol if this is an external value.
15120 new_symbol checks this and puts the value in the global symbol
15121 table, which we want. If it is not external, new_symbol
15122 will try to put the value in cu->list_in_scope which is wrong. */
15123 && dwarf2_flag_true_p (die, DW_AT_external, cu))
254e6b9e
DE
15124 {
15125 /* A static const member, not much different than an enum as far as
15126 we're concerned, except that we can support more types. */
15127 new_symbol (die, NULL, cu);
15128 }
15129
2df3850c 15130 /* Get physical name. */
ff355380 15131 physname = dwarf2_physname (fieldname, die, cu);
c906108c 15132
d8151005
DJ
15133 /* The name is already allocated along with this objfile, so we don't
15134 need to duplicate it for the type. */
15135 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
e7c27a73 15136 FIELD_TYPE (*fp) = die_type (die, cu);
d8151005 15137 FIELD_NAME (*fp) = fieldname;
c906108c
SS
15138 }
15139 else if (die->tag == DW_TAG_inheritance)
15140 {
74ac6d43 15141 LONGEST offset;
d4b96c9a 15142
74ac6d43
TT
15143 /* C++ base class field. */
15144 if (handle_data_member_location (die, cu, &offset))
15145 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
c906108c 15146 FIELD_BITSIZE (*fp) = 0;
e7c27a73 15147 FIELD_TYPE (*fp) = die_type (die, cu);
c906108c
SS
15148 FIELD_NAME (*fp) = type_name_no_tag (fp->type);
15149 fip->nbaseclasses++;
15150 }
15151}
15152
883fd55a
KS
15153/* Can the type given by DIE define another type? */
15154
15155static bool
15156type_can_define_types (const struct die_info *die)
15157{
15158 switch (die->tag)
15159 {
15160 case DW_TAG_typedef:
15161 case DW_TAG_class_type:
15162 case DW_TAG_structure_type:
15163 case DW_TAG_union_type:
15164 case DW_TAG_enumeration_type:
15165 return true;
15166
15167 default:
15168 return false;
15169 }
15170}
15171
15172/* Add a type definition defined in the scope of the FIP's class. */
98751a41
JK
15173
15174static void
883fd55a
KS
15175dwarf2_add_type_defn (struct field_info *fip, struct die_info *die,
15176 struct dwarf2_cu *cu)
6e70227d 15177{
883fd55a
KS
15178 struct decl_field_list *new_field;
15179 struct decl_field *fp;
98751a41
JK
15180
15181 /* Allocate a new field list entry and link it in. */
883fd55a 15182 new_field = XCNEW (struct decl_field_list);
98751a41
JK
15183 make_cleanup (xfree, new_field);
15184
883fd55a 15185 gdb_assert (type_can_define_types (die));
98751a41
JK
15186
15187 fp = &new_field->field;
15188
883fd55a 15189 /* Get name of field. NULL is okay here, meaning an anonymous type. */
98751a41 15190 fp->name = dwarf2_name (die, cu);
98751a41
JK
15191 fp->type = read_type_die (die, cu);
15192
c191a687
KS
15193 /* Save accessibility. */
15194 enum dwarf_access_attribute accessibility;
15195 struct attribute *attr = dwarf2_attr (die, DW_AT_accessibility, cu);
15196 if (attr != NULL)
15197 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
15198 else
15199 accessibility = dwarf2_default_access_attribute (die, cu);
15200 switch (accessibility)
15201 {
15202 case DW_ACCESS_public:
15203 /* The assumed value if neither private nor protected. */
15204 break;
15205 case DW_ACCESS_private:
15206 fp->is_private = 1;
15207 break;
15208 case DW_ACCESS_protected:
15209 fp->is_protected = 1;
15210 break;
15211 default:
37534686
KS
15212 complaint (&symfile_complaints,
15213 _("Unhandled DW_AT_accessibility value (%x)"), accessibility);
c191a687
KS
15214 }
15215
883fd55a
KS
15216 if (die->tag == DW_TAG_typedef)
15217 {
15218 new_field->next = fip->typedef_field_list;
15219 fip->typedef_field_list = new_field;
15220 fip->typedef_field_list_count++;
15221 }
15222 else
15223 {
15224 new_field->next = fip->nested_types_list;
15225 fip->nested_types_list = new_field;
15226 fip->nested_types_list_count++;
15227 }
98751a41
JK
15228}
15229
c906108c
SS
15230/* Create the vector of fields, and attach it to the type. */
15231
15232static void
fba45db2 15233dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 15234 struct dwarf2_cu *cu)
c906108c
SS
15235{
15236 int nfields = fip->nfields;
15237
15238 /* Record the field count, allocate space for the array of fields,
15239 and create blank accessibility bitfields if necessary. */
15240 TYPE_NFIELDS (type) = nfields;
15241 TYPE_FIELDS (type) = (struct field *)
15242 TYPE_ALLOC (type, sizeof (struct field) * nfields);
15243 memset (TYPE_FIELDS (type), 0, sizeof (struct field) * nfields);
15244
b4ba55a1 15245 if (fip->non_public_fields && cu->language != language_ada)
c906108c
SS
15246 {
15247 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15248
15249 TYPE_FIELD_PRIVATE_BITS (type) =
15250 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15251 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
15252
15253 TYPE_FIELD_PROTECTED_BITS (type) =
15254 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15255 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
15256
774b6a14
TT
15257 TYPE_FIELD_IGNORE_BITS (type) =
15258 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15259 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
c906108c
SS
15260 }
15261
15262 /* If the type has baseclasses, allocate and clear a bit vector for
15263 TYPE_FIELD_VIRTUAL_BITS. */
b4ba55a1 15264 if (fip->nbaseclasses && cu->language != language_ada)
c906108c
SS
15265 {
15266 int num_bytes = B_BYTES (fip->nbaseclasses);
fe1b8b76 15267 unsigned char *pointer;
c906108c
SS
15268
15269 ALLOCATE_CPLUS_STRUCT_TYPE (type);
224c3ddb 15270 pointer = (unsigned char *) TYPE_ALLOC (type, num_bytes);
fe1b8b76 15271 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
c906108c
SS
15272 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->nbaseclasses);
15273 TYPE_N_BASECLASSES (type) = fip->nbaseclasses;
15274 }
15275
3e43a32a
MS
15276 /* Copy the saved-up fields into the field vector. Start from the head of
15277 the list, adding to the tail of the field array, so that they end up in
15278 the same order in the array in which they were added to the list. */
c906108c
SS
15279 while (nfields-- > 0)
15280 {
7d0ccb61
DJ
15281 struct nextfield *fieldp;
15282
15283 if (fip->fields)
15284 {
15285 fieldp = fip->fields;
15286 fip->fields = fieldp->next;
15287 }
15288 else
15289 {
15290 fieldp = fip->baseclasses;
15291 fip->baseclasses = fieldp->next;
15292 }
15293
15294 TYPE_FIELD (type, nfields) = fieldp->field;
15295 switch (fieldp->accessibility)
c906108c 15296 {
c5aa993b 15297 case DW_ACCESS_private:
b4ba55a1
JB
15298 if (cu->language != language_ada)
15299 SET_TYPE_FIELD_PRIVATE (type, nfields);
c5aa993b 15300 break;
c906108c 15301
c5aa993b 15302 case DW_ACCESS_protected:
b4ba55a1
JB
15303 if (cu->language != language_ada)
15304 SET_TYPE_FIELD_PROTECTED (type, nfields);
c5aa993b 15305 break;
c906108c 15306
c5aa993b
JM
15307 case DW_ACCESS_public:
15308 break;
c906108c 15309
c5aa993b
JM
15310 default:
15311 /* Unknown accessibility. Complain and treat it as public. */
15312 {
e2e0b3e5 15313 complaint (&symfile_complaints, _("unsupported accessibility %d"),
7d0ccb61 15314 fieldp->accessibility);
c5aa993b
JM
15315 }
15316 break;
c906108c
SS
15317 }
15318 if (nfields < fip->nbaseclasses)
15319 {
7d0ccb61 15320 switch (fieldp->virtuality)
c906108c 15321 {
c5aa993b
JM
15322 case DW_VIRTUALITY_virtual:
15323 case DW_VIRTUALITY_pure_virtual:
b4ba55a1 15324 if (cu->language == language_ada)
a73c6dcd 15325 error (_("unexpected virtuality in component of Ada type"));
c5aa993b
JM
15326 SET_TYPE_FIELD_VIRTUAL (type, nfields);
15327 break;
c906108c
SS
15328 }
15329 }
c906108c
SS
15330 }
15331}
15332
7d27a96d
TT
15333/* Return true if this member function is a constructor, false
15334 otherwise. */
15335
15336static int
15337dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
15338{
15339 const char *fieldname;
fe978cb0 15340 const char *type_name;
7d27a96d
TT
15341 int len;
15342
15343 if (die->parent == NULL)
15344 return 0;
15345
15346 if (die->parent->tag != DW_TAG_structure_type
15347 && die->parent->tag != DW_TAG_union_type
15348 && die->parent->tag != DW_TAG_class_type)
15349 return 0;
15350
15351 fieldname = dwarf2_name (die, cu);
fe978cb0
PA
15352 type_name = dwarf2_name (die->parent, cu);
15353 if (fieldname == NULL || type_name == NULL)
7d27a96d
TT
15354 return 0;
15355
15356 len = strlen (fieldname);
fe978cb0
PA
15357 return (strncmp (fieldname, type_name, len) == 0
15358 && (type_name[len] == '\0' || type_name[len] == '<'));
7d27a96d
TT
15359}
15360
c906108c
SS
15361/* Add a member function to the proper fieldlist. */
15362
15363static void
107d2387 15364dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
e7c27a73 15365 struct type *type, struct dwarf2_cu *cu)
c906108c 15366{
e7c27a73 15367 struct objfile *objfile = cu->objfile;
c906108c
SS
15368 struct attribute *attr;
15369 struct fnfieldlist *flp;
15370 int i;
15371 struct fn_field *fnp;
15d034d0 15372 const char *fieldname;
c906108c 15373 struct nextfnfield *new_fnfield;
f792889a 15374 struct type *this_type;
60d5a603 15375 enum dwarf_access_attribute accessibility;
c906108c 15376
b4ba55a1 15377 if (cu->language == language_ada)
a73c6dcd 15378 error (_("unexpected member function in Ada type"));
b4ba55a1 15379
2df3850c 15380 /* Get name of member function. */
39cbfefa
DJ
15381 fieldname = dwarf2_name (die, cu);
15382 if (fieldname == NULL)
2df3850c 15383 return;
c906108c 15384
c906108c
SS
15385 /* Look up member function name in fieldlist. */
15386 for (i = 0; i < fip->nfnfields; i++)
15387 {
27bfe10e 15388 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
c906108c
SS
15389 break;
15390 }
15391
15392 /* Create new list element if necessary. */
15393 if (i < fip->nfnfields)
15394 flp = &fip->fnfieldlists[i];
15395 else
15396 {
15397 if ((fip->nfnfields % DW_FIELD_ALLOC_CHUNK) == 0)
15398 {
15399 fip->fnfieldlists = (struct fnfieldlist *)
15400 xrealloc (fip->fnfieldlists,
15401 (fip->nfnfields + DW_FIELD_ALLOC_CHUNK)
c5aa993b 15402 * sizeof (struct fnfieldlist));
c906108c 15403 if (fip->nfnfields == 0)
c13c43fd 15404 make_cleanup (free_current_contents, &fip->fnfieldlists);
c906108c
SS
15405 }
15406 flp = &fip->fnfieldlists[fip->nfnfields];
15407 flp->name = fieldname;
15408 flp->length = 0;
15409 flp->head = NULL;
3da10d80 15410 i = fip->nfnfields++;
c906108c
SS
15411 }
15412
15413 /* Create a new member function field and chain it to the field list
0963b4bd 15414 entry. */
8d749320 15415 new_fnfield = XNEW (struct nextfnfield);
b8c9b27d 15416 make_cleanup (xfree, new_fnfield);
c906108c
SS
15417 memset (new_fnfield, 0, sizeof (struct nextfnfield));
15418 new_fnfield->next = flp->head;
15419 flp->head = new_fnfield;
15420 flp->length++;
15421
15422 /* Fill in the member function field info. */
15423 fnp = &new_fnfield->fnfield;
3da10d80
KS
15424
15425 /* Delay processing of the physname until later. */
9c37b5ae 15426 if (cu->language == language_cplus)
3da10d80
KS
15427 {
15428 add_to_method_list (type, i, flp->length - 1, fieldname,
15429 die, cu);
15430 }
15431 else
15432 {
1d06ead6 15433 const char *physname = dwarf2_physname (fieldname, die, cu);
3da10d80
KS
15434 fnp->physname = physname ? physname : "";
15435 }
15436
c906108c 15437 fnp->type = alloc_type (objfile);
f792889a
DJ
15438 this_type = read_type_die (die, cu);
15439 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
c906108c 15440 {
f792889a 15441 int nparams = TYPE_NFIELDS (this_type);
c906108c 15442
f792889a 15443 /* TYPE is the domain of this method, and THIS_TYPE is the type
e26fb1d7
DC
15444 of the method itself (TYPE_CODE_METHOD). */
15445 smash_to_method_type (fnp->type, type,
f792889a
DJ
15446 TYPE_TARGET_TYPE (this_type),
15447 TYPE_FIELDS (this_type),
15448 TYPE_NFIELDS (this_type),
15449 TYPE_VARARGS (this_type));
c906108c
SS
15450
15451 /* Handle static member functions.
c5aa993b 15452 Dwarf2 has no clean way to discern C++ static and non-static
0963b4bd
MS
15453 member functions. G++ helps GDB by marking the first
15454 parameter for non-static member functions (which is the this
15455 pointer) as artificial. We obtain this information from
15456 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
f792889a 15457 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
c906108c
SS
15458 fnp->voffset = VOFFSET_STATIC;
15459 }
15460 else
e2e0b3e5 15461 complaint (&symfile_complaints, _("member function type missing for '%s'"),
3da10d80 15462 dwarf2_full_name (fieldname, die, cu));
c906108c
SS
15463
15464 /* Get fcontext from DW_AT_containing_type if present. */
e142c38c 15465 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
e7c27a73 15466 fnp->fcontext = die_containing_type (die, cu);
c906108c 15467
3e43a32a
MS
15468 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
15469 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
c906108c
SS
15470
15471 /* Get accessibility. */
e142c38c 15472 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
c906108c 15473 if (attr)
aead7601 15474 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
60d5a603
JK
15475 else
15476 accessibility = dwarf2_default_access_attribute (die, cu);
15477 switch (accessibility)
c906108c 15478 {
60d5a603
JK
15479 case DW_ACCESS_private:
15480 fnp->is_private = 1;
15481 break;
15482 case DW_ACCESS_protected:
15483 fnp->is_protected = 1;
15484 break;
c906108c
SS
15485 }
15486
b02dede2 15487 /* Check for artificial methods. */
e142c38c 15488 attr = dwarf2_attr (die, DW_AT_artificial, cu);
b02dede2
DJ
15489 if (attr && DW_UNSND (attr) != 0)
15490 fnp->is_artificial = 1;
15491
7d27a96d
TT
15492 fnp->is_constructor = dwarf2_is_constructor (die, cu);
15493
0d564a31 15494 /* Get index in virtual function table if it is a virtual member
aec5aa8b
TT
15495 function. For older versions of GCC, this is an offset in the
15496 appropriate virtual table, as specified by DW_AT_containing_type.
15497 For everyone else, it is an expression to be evaluated relative
0d564a31
DJ
15498 to the object address. */
15499
e142c38c 15500 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
aec5aa8b 15501 if (attr)
8e19ed76 15502 {
aec5aa8b 15503 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size > 0)
8e19ed76 15504 {
aec5aa8b
TT
15505 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
15506 {
15507 /* Old-style GCC. */
15508 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
15509 }
15510 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
15511 || (DW_BLOCK (attr)->size > 1
15512 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
15513 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
15514 {
aec5aa8b
TT
15515 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
15516 if ((fnp->voffset % cu->header.addr_size) != 0)
15517 dwarf2_complex_location_expr_complaint ();
15518 else
15519 fnp->voffset /= cu->header.addr_size;
15520 fnp->voffset += 2;
15521 }
15522 else
15523 dwarf2_complex_location_expr_complaint ();
15524
15525 if (!fnp->fcontext)
7e993ebf
KS
15526 {
15527 /* If there is no `this' field and no DW_AT_containing_type,
15528 we cannot actually find a base class context for the
15529 vtable! */
15530 if (TYPE_NFIELDS (this_type) == 0
15531 || !TYPE_FIELD_ARTIFICIAL (this_type, 0))
15532 {
15533 complaint (&symfile_complaints,
15534 _("cannot determine context for virtual member "
15535 "function \"%s\" (offset %d)"),
9c541725 15536 fieldname, to_underlying (die->sect_off));
7e993ebf
KS
15537 }
15538 else
15539 {
15540 fnp->fcontext
15541 = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
15542 }
15543 }
aec5aa8b 15544 }
3690dd37 15545 else if (attr_form_is_section_offset (attr))
8e19ed76 15546 {
4d3c2250 15547 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
15548 }
15549 else
15550 {
4d3c2250
KB
15551 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
15552 fieldname);
8e19ed76 15553 }
0d564a31 15554 }
d48cc9dd
DJ
15555 else
15556 {
15557 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
15558 if (attr && DW_UNSND (attr))
15559 {
15560 /* GCC does this, as of 2008-08-25; PR debug/37237. */
15561 complaint (&symfile_complaints,
3e43a32a
MS
15562 _("Member function \"%s\" (offset %d) is virtual "
15563 "but the vtable offset is not specified"),
9c541725 15564 fieldname, to_underlying (die->sect_off));
9655fd1a 15565 ALLOCATE_CPLUS_STRUCT_TYPE (type);
d48cc9dd
DJ
15566 TYPE_CPLUS_DYNAMIC (type) = 1;
15567 }
15568 }
c906108c
SS
15569}
15570
15571/* Create the vector of member function fields, and attach it to the type. */
15572
15573static void
fba45db2 15574dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 15575 struct dwarf2_cu *cu)
c906108c
SS
15576{
15577 struct fnfieldlist *flp;
c906108c
SS
15578 int i;
15579
b4ba55a1 15580 if (cu->language == language_ada)
a73c6dcd 15581 error (_("unexpected member functions in Ada type"));
b4ba55a1 15582
c906108c
SS
15583 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15584 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
15585 TYPE_ALLOC (type, sizeof (struct fn_fieldlist) * fip->nfnfields);
15586
15587 for (i = 0, flp = fip->fnfieldlists; i < fip->nfnfields; i++, flp++)
15588 {
15589 struct nextfnfield *nfp = flp->head;
15590 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
15591 int k;
15592
15593 TYPE_FN_FIELDLIST_NAME (type, i) = flp->name;
15594 TYPE_FN_FIELDLIST_LENGTH (type, i) = flp->length;
15595 fn_flp->fn_fields = (struct fn_field *)
15596 TYPE_ALLOC (type, sizeof (struct fn_field) * flp->length);
15597 for (k = flp->length; (k--, nfp); nfp = nfp->next)
c5aa993b 15598 fn_flp->fn_fields[k] = nfp->fnfield;
c906108c
SS
15599 }
15600
15601 TYPE_NFN_FIELDS (type) = fip->nfnfields;
c906108c
SS
15602}
15603
1168df01
JB
15604/* Returns non-zero if NAME is the name of a vtable member in CU's
15605 language, zero otherwise. */
15606static int
15607is_vtable_name (const char *name, struct dwarf2_cu *cu)
15608{
15609 static const char vptr[] = "_vptr";
15610
9c37b5ae
TT
15611 /* Look for the C++ form of the vtable. */
15612 if (startswith (name, vptr) && is_cplus_marker (name[sizeof (vptr) - 1]))
1168df01
JB
15613 return 1;
15614
15615 return 0;
15616}
15617
c0dd20ea 15618/* GCC outputs unnamed structures that are really pointers to member
0b92b5bb
TT
15619 functions, with the ABI-specified layout. If TYPE describes
15620 such a structure, smash it into a member function type.
61049d3b
DJ
15621
15622 GCC shouldn't do this; it should just output pointer to member DIEs.
15623 This is GCC PR debug/28767. */
c0dd20ea 15624
0b92b5bb
TT
15625static void
15626quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
c0dd20ea 15627{
09e2d7c7 15628 struct type *pfn_type, *self_type, *new_type;
c0dd20ea
DJ
15629
15630 /* Check for a structure with no name and two children. */
0b92b5bb
TT
15631 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
15632 return;
c0dd20ea
DJ
15633
15634 /* Check for __pfn and __delta members. */
0b92b5bb
TT
15635 if (TYPE_FIELD_NAME (type, 0) == NULL
15636 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
15637 || TYPE_FIELD_NAME (type, 1) == NULL
15638 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
15639 return;
c0dd20ea
DJ
15640
15641 /* Find the type of the method. */
0b92b5bb 15642 pfn_type = TYPE_FIELD_TYPE (type, 0);
c0dd20ea
DJ
15643 if (pfn_type == NULL
15644 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
15645 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
0b92b5bb 15646 return;
c0dd20ea
DJ
15647
15648 /* Look for the "this" argument. */
15649 pfn_type = TYPE_TARGET_TYPE (pfn_type);
15650 if (TYPE_NFIELDS (pfn_type) == 0
0b92b5bb 15651 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
c0dd20ea 15652 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
0b92b5bb 15653 return;
c0dd20ea 15654
09e2d7c7 15655 self_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
0b92b5bb 15656 new_type = alloc_type (objfile);
09e2d7c7 15657 smash_to_method_type (new_type, self_type, TYPE_TARGET_TYPE (pfn_type),
c0dd20ea
DJ
15658 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
15659 TYPE_VARARGS (pfn_type));
0b92b5bb 15660 smash_to_methodptr_type (type, new_type);
c0dd20ea 15661}
1168df01 15662
685b1105 15663
c906108c 15664/* Called when we find the DIE that starts a structure or union scope
c767944b
DJ
15665 (definition) to create a type for the structure or union. Fill in
15666 the type's name and general properties; the members will not be
83655187
DE
15667 processed until process_structure_scope. A symbol table entry for
15668 the type will also not be done until process_structure_scope (assuming
15669 the type has a name).
c906108c 15670
c767944b
DJ
15671 NOTE: we need to call these functions regardless of whether or not the
15672 DIE has a DW_AT_name attribute, since it might be an anonymous
c906108c 15673 structure or union. This gets the type entered into our set of
83655187 15674 user defined types. */
c906108c 15675
f792889a 15676static struct type *
134d01f1 15677read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 15678{
e7c27a73 15679 struct objfile *objfile = cu->objfile;
c906108c
SS
15680 struct type *type;
15681 struct attribute *attr;
15d034d0 15682 const char *name;
c906108c 15683
348e048f
DE
15684 /* If the definition of this type lives in .debug_types, read that type.
15685 Don't follow DW_AT_specification though, that will take us back up
15686 the chain and we want to go down. */
45e58e77 15687 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
348e048f
DE
15688 if (attr)
15689 {
ac9ec31b 15690 type = get_DW_AT_signature_type (die, attr, cu);
9dc481d3 15691
ac9ec31b 15692 /* The type's CU may not be the same as CU.
02142a6c 15693 Ensure TYPE is recorded with CU in die_type_hash. */
348e048f
DE
15694 return set_die_type (die, type, cu);
15695 }
15696
c0dd20ea 15697 type = alloc_type (objfile);
c906108c 15698 INIT_CPLUS_SPECIFIC (type);
93311388 15699
39cbfefa
DJ
15700 name = dwarf2_name (die, cu);
15701 if (name != NULL)
c906108c 15702 {
987504bb 15703 if (cu->language == language_cplus
c44af4eb
TT
15704 || cu->language == language_d
15705 || cu->language == language_rust)
63d06c5c 15706 {
15d034d0 15707 const char *full_name = dwarf2_full_name (name, die, cu);
3da10d80
KS
15708
15709 /* dwarf2_full_name might have already finished building the DIE's
15710 type. If so, there is no need to continue. */
15711 if (get_die_type (die, cu) != NULL)
15712 return get_die_type (die, cu);
15713
15714 TYPE_TAG_NAME (type) = full_name;
94af9270
KS
15715 if (die->tag == DW_TAG_structure_type
15716 || die->tag == DW_TAG_class_type)
15717 TYPE_NAME (type) = TYPE_TAG_NAME (type);
63d06c5c
DC
15718 }
15719 else
15720 {
d8151005
DJ
15721 /* The name is already allocated along with this objfile, so
15722 we don't need to duplicate it for the type. */
7d455152 15723 TYPE_TAG_NAME (type) = name;
94af9270
KS
15724 if (die->tag == DW_TAG_class_type)
15725 TYPE_NAME (type) = TYPE_TAG_NAME (type);
63d06c5c 15726 }
c906108c
SS
15727 }
15728
15729 if (die->tag == DW_TAG_structure_type)
15730 {
15731 TYPE_CODE (type) = TYPE_CODE_STRUCT;
15732 }
15733 else if (die->tag == DW_TAG_union_type)
15734 {
15735 TYPE_CODE (type) = TYPE_CODE_UNION;
15736 }
15737 else
15738 {
4753d33b 15739 TYPE_CODE (type) = TYPE_CODE_STRUCT;
c906108c
SS
15740 }
15741
0cc2414c
TT
15742 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
15743 TYPE_DECLARED_CLASS (type) = 1;
15744
e142c38c 15745 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
15746 if (attr)
15747 {
155bfbd3
JB
15748 if (attr_form_is_constant (attr))
15749 TYPE_LENGTH (type) = DW_UNSND (attr);
15750 else
15751 {
15752 /* For the moment, dynamic type sizes are not supported
15753 by GDB's struct type. The actual size is determined
15754 on-demand when resolving the type of a given object,
15755 so set the type's length to zero for now. Otherwise,
15756 we record an expression as the length, and that expression
15757 could lead to a very large value, which could eventually
15758 lead to us trying to allocate that much memory when creating
15759 a value of that type. */
15760 TYPE_LENGTH (type) = 0;
15761 }
c906108c
SS
15762 }
15763 else
15764 {
15765 TYPE_LENGTH (type) = 0;
15766 }
15767
5230b05a 15768 if (producer_is_icc_lt_14 (cu) && (TYPE_LENGTH (type) == 0))
685b1105 15769 {
5230b05a
WT
15770 /* ICC<14 does not output the required DW_AT_declaration on
15771 incomplete types, but gives them a size of zero. */
422b1cb0 15772 TYPE_STUB (type) = 1;
685b1105
JK
15773 }
15774 else
15775 TYPE_STUB_SUPPORTED (type) = 1;
15776
dc718098 15777 if (die_is_declaration (die, cu))
876cecd0 15778 TYPE_STUB (type) = 1;
a6c727b2
DJ
15779 else if (attr == NULL && die->child == NULL
15780 && producer_is_realview (cu->producer))
15781 /* RealView does not output the required DW_AT_declaration
15782 on incomplete types. */
15783 TYPE_STUB (type) = 1;
dc718098 15784
c906108c
SS
15785 /* We need to add the type field to the die immediately so we don't
15786 infinitely recurse when dealing with pointers to the structure
0963b4bd 15787 type within the structure itself. */
1c379e20 15788 set_die_type (die, type, cu);
c906108c 15789
7e314c57
JK
15790 /* set_die_type should be already done. */
15791 set_descriptive_type (type, die, cu);
15792
c767944b
DJ
15793 return type;
15794}
15795
15796/* Finish creating a structure or union type, including filling in
15797 its members and creating a symbol for it. */
15798
15799static void
15800process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
15801{
15802 struct objfile *objfile = cu->objfile;
ca040673 15803 struct die_info *child_die;
c767944b
DJ
15804 struct type *type;
15805
15806 type = get_die_type (die, cu);
15807 if (type == NULL)
15808 type = read_structure_type (die, cu);
15809
e142c38c 15810 if (die->child != NULL && ! die_is_declaration (die, cu))
c906108c
SS
15811 {
15812 struct field_info fi;
2f4732b0 15813 std::vector<struct symbol *> template_args;
c767944b 15814 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
c906108c
SS
15815
15816 memset (&fi, 0, sizeof (struct field_info));
15817
639d11d3 15818 child_die = die->child;
c906108c
SS
15819
15820 while (child_die && child_die->tag)
15821 {
a9a9bd0f
DC
15822 if (child_die->tag == DW_TAG_member
15823 || child_die->tag == DW_TAG_variable)
c906108c 15824 {
a9a9bd0f
DC
15825 /* NOTE: carlton/2002-11-05: A C++ static data member
15826 should be a DW_TAG_member that is a declaration, but
15827 all versions of G++ as of this writing (so through at
15828 least 3.2.1) incorrectly generate DW_TAG_variable
15829 tags for them instead. */
e7c27a73 15830 dwarf2_add_field (&fi, child_die, cu);
c906108c 15831 }
8713b1b1 15832 else if (child_die->tag == DW_TAG_subprogram)
c906108c 15833 {
e98c9e7c
TT
15834 /* Rust doesn't have member functions in the C++ sense.
15835 However, it does emit ordinary functions as children
15836 of a struct DIE. */
15837 if (cu->language == language_rust)
15838 read_func_scope (child_die, cu);
15839 else
15840 {
15841 /* C++ member function. */
15842 dwarf2_add_member_fn (&fi, child_die, type, cu);
15843 }
c906108c
SS
15844 }
15845 else if (child_die->tag == DW_TAG_inheritance)
15846 {
15847 /* C++ base class field. */
e7c27a73 15848 dwarf2_add_field (&fi, child_die, cu);
c906108c 15849 }
883fd55a
KS
15850 else if (type_can_define_types (child_die))
15851 dwarf2_add_type_defn (&fi, child_die, cu);
34eaf542
TT
15852 else if (child_die->tag == DW_TAG_template_type_param
15853 || child_die->tag == DW_TAG_template_value_param)
15854 {
15855 struct symbol *arg = new_symbol (child_die, NULL, cu);
15856
f1078f66 15857 if (arg != NULL)
2f4732b0 15858 template_args.push_back (arg);
34eaf542
TT
15859 }
15860
c906108c
SS
15861 child_die = sibling_die (child_die);
15862 }
15863
34eaf542 15864 /* Attach template arguments to type. */
2f4732b0 15865 if (!template_args.empty ())
34eaf542
TT
15866 {
15867 ALLOCATE_CPLUS_STRUCT_TYPE (type);
2f4732b0 15868 TYPE_N_TEMPLATE_ARGUMENTS (type) = template_args.size ();
34eaf542 15869 TYPE_TEMPLATE_ARGUMENTS (type)
8d749320
SM
15870 = XOBNEWVEC (&objfile->objfile_obstack,
15871 struct symbol *,
15872 TYPE_N_TEMPLATE_ARGUMENTS (type));
34eaf542 15873 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
2f4732b0 15874 template_args.data (),
34eaf542
TT
15875 (TYPE_N_TEMPLATE_ARGUMENTS (type)
15876 * sizeof (struct symbol *)));
34eaf542
TT
15877 }
15878
c906108c
SS
15879 /* Attach fields and member functions to the type. */
15880 if (fi.nfields)
e7c27a73 15881 dwarf2_attach_fields_to_type (&fi, type, cu);
c906108c
SS
15882 if (fi.nfnfields)
15883 {
e7c27a73 15884 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
c906108c 15885
c5aa993b 15886 /* Get the type which refers to the base class (possibly this
c906108c 15887 class itself) which contains the vtable pointer for the current
0d564a31
DJ
15888 class from the DW_AT_containing_type attribute. This use of
15889 DW_AT_containing_type is a GNU extension. */
c906108c 15890
e142c38c 15891 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
c906108c 15892 {
e7c27a73 15893 struct type *t = die_containing_type (die, cu);
c906108c 15894
ae6ae975 15895 set_type_vptr_basetype (type, t);
c906108c
SS
15896 if (type == t)
15897 {
c906108c
SS
15898 int i;
15899
15900 /* Our own class provides vtbl ptr. */
15901 for (i = TYPE_NFIELDS (t) - 1;
15902 i >= TYPE_N_BASECLASSES (t);
15903 --i)
15904 {
0d5cff50 15905 const char *fieldname = TYPE_FIELD_NAME (t, i);
c906108c 15906
1168df01 15907 if (is_vtable_name (fieldname, cu))
c906108c 15908 {
ae6ae975 15909 set_type_vptr_fieldno (type, i);
c906108c
SS
15910 break;
15911 }
15912 }
15913
15914 /* Complain if virtual function table field not found. */
15915 if (i < TYPE_N_BASECLASSES (t))
4d3c2250 15916 complaint (&symfile_complaints,
3e43a32a
MS
15917 _("virtual function table pointer "
15918 "not found when defining class '%s'"),
4d3c2250
KB
15919 TYPE_TAG_NAME (type) ? TYPE_TAG_NAME (type) :
15920 "");
c906108c
SS
15921 }
15922 else
15923 {
ae6ae975 15924 set_type_vptr_fieldno (type, TYPE_VPTR_FIELDNO (t));
c906108c
SS
15925 }
15926 }
f6235d4c 15927 else if (cu->producer
61012eef 15928 && startswith (cu->producer, "IBM(R) XL C/C++ Advanced Edition"))
f6235d4c
EZ
15929 {
15930 /* The IBM XLC compiler does not provide direct indication
15931 of the containing type, but the vtable pointer is
15932 always named __vfp. */
15933
15934 int i;
15935
15936 for (i = TYPE_NFIELDS (type) - 1;
15937 i >= TYPE_N_BASECLASSES (type);
15938 --i)
15939 {
15940 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
15941 {
ae6ae975
DE
15942 set_type_vptr_fieldno (type, i);
15943 set_type_vptr_basetype (type, type);
f6235d4c
EZ
15944 break;
15945 }
15946 }
15947 }
c906108c 15948 }
98751a41
JK
15949
15950 /* Copy fi.typedef_field_list linked list elements content into the
15951 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
15952 if (fi.typedef_field_list)
15953 {
15954 int i = fi.typedef_field_list_count;
15955
a0d7a4ff 15956 ALLOCATE_CPLUS_STRUCT_TYPE (type);
98751a41 15957 TYPE_TYPEDEF_FIELD_ARRAY (type)
883fd55a 15958 = ((struct decl_field *)
224c3ddb 15959 TYPE_ALLOC (type, sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * i));
98751a41
JK
15960 TYPE_TYPEDEF_FIELD_COUNT (type) = i;
15961
15962 /* Reverse the list order to keep the debug info elements order. */
15963 while (--i >= 0)
15964 {
883fd55a 15965 struct decl_field *dest, *src;
6e70227d 15966
98751a41
JK
15967 dest = &TYPE_TYPEDEF_FIELD (type, i);
15968 src = &fi.typedef_field_list->field;
15969 fi.typedef_field_list = fi.typedef_field_list->next;
15970 *dest = *src;
15971 }
15972 }
c767944b 15973
883fd55a
KS
15974 /* Copy fi.nested_types_list linked list elements content into the
15975 allocated array TYPE_NESTED_TYPES_ARRAY (type). */
15976 if (fi.nested_types_list != NULL && cu->language != language_ada)
15977 {
15978 int i = fi.nested_types_list_count;
15979
15980 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15981 TYPE_NESTED_TYPES_ARRAY (type)
15982 = ((struct decl_field *)
15983 TYPE_ALLOC (type, sizeof (struct decl_field) * i));
15984 TYPE_NESTED_TYPES_COUNT (type) = i;
15985
15986 /* Reverse the list order to keep the debug info elements order. */
15987 while (--i >= 0)
15988 {
15989 struct decl_field *dest, *src;
15990
15991 dest = &TYPE_NESTED_TYPES_FIELD (type, i);
15992 src = &fi.nested_types_list->field;
15993 fi.nested_types_list = fi.nested_types_list->next;
15994 *dest = *src;
15995 }
15996 }
15997
c767944b 15998 do_cleanups (back_to);
c906108c 15999 }
63d06c5c 16000
bb5ed363 16001 quirk_gcc_member_function_pointer (type, objfile);
0b92b5bb 16002
90aeadfc
DC
16003 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
16004 snapshots) has been known to create a die giving a declaration
16005 for a class that has, as a child, a die giving a definition for a
16006 nested class. So we have to process our children even if the
16007 current die is a declaration. Normally, of course, a declaration
16008 won't have any children at all. */
134d01f1 16009
ca040673
DE
16010 child_die = die->child;
16011
90aeadfc
DC
16012 while (child_die != NULL && child_die->tag)
16013 {
16014 if (child_die->tag == DW_TAG_member
16015 || child_die->tag == DW_TAG_variable
34eaf542
TT
16016 || child_die->tag == DW_TAG_inheritance
16017 || child_die->tag == DW_TAG_template_value_param
16018 || child_die->tag == DW_TAG_template_type_param)
134d01f1 16019 {
90aeadfc 16020 /* Do nothing. */
134d01f1 16021 }
90aeadfc
DC
16022 else
16023 process_die (child_die, cu);
134d01f1 16024
90aeadfc 16025 child_die = sibling_die (child_die);
134d01f1
DJ
16026 }
16027
fa4028e9
JB
16028 /* Do not consider external references. According to the DWARF standard,
16029 these DIEs are identified by the fact that they have no byte_size
16030 attribute, and a declaration attribute. */
16031 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
16032 || !die_is_declaration (die, cu))
c767944b 16033 new_symbol (die, type, cu);
134d01f1
DJ
16034}
16035
55426c9d
JB
16036/* Assuming DIE is an enumeration type, and TYPE is its associated type,
16037 update TYPE using some information only available in DIE's children. */
16038
16039static void
16040update_enumeration_type_from_children (struct die_info *die,
16041 struct type *type,
16042 struct dwarf2_cu *cu)
16043{
60f7655a 16044 struct die_info *child_die;
55426c9d
JB
16045 int unsigned_enum = 1;
16046 int flag_enum = 1;
16047 ULONGEST mask = 0;
55426c9d 16048
8268c778 16049 auto_obstack obstack;
55426c9d 16050
60f7655a
DE
16051 for (child_die = die->child;
16052 child_die != NULL && child_die->tag;
16053 child_die = sibling_die (child_die))
55426c9d
JB
16054 {
16055 struct attribute *attr;
16056 LONGEST value;
16057 const gdb_byte *bytes;
16058 struct dwarf2_locexpr_baton *baton;
16059 const char *name;
60f7655a 16060
55426c9d
JB
16061 if (child_die->tag != DW_TAG_enumerator)
16062 continue;
16063
16064 attr = dwarf2_attr (child_die, DW_AT_const_value, cu);
16065 if (attr == NULL)
16066 continue;
16067
16068 name = dwarf2_name (child_die, cu);
16069 if (name == NULL)
16070 name = "<anonymous enumerator>";
16071
16072 dwarf2_const_value_attr (attr, type, name, &obstack, cu,
16073 &value, &bytes, &baton);
16074 if (value < 0)
16075 {
16076 unsigned_enum = 0;
16077 flag_enum = 0;
16078 }
16079 else if ((mask & value) != 0)
16080 flag_enum = 0;
16081 else
16082 mask |= value;
16083
16084 /* If we already know that the enum type is neither unsigned, nor
16085 a flag type, no need to look at the rest of the enumerates. */
16086 if (!unsigned_enum && !flag_enum)
16087 break;
55426c9d
JB
16088 }
16089
16090 if (unsigned_enum)
16091 TYPE_UNSIGNED (type) = 1;
16092 if (flag_enum)
16093 TYPE_FLAG_ENUM (type) = 1;
55426c9d
JB
16094}
16095
134d01f1
DJ
16096/* Given a DW_AT_enumeration_type die, set its type. We do not
16097 complete the type's fields yet, or create any symbols. */
c906108c 16098
f792889a 16099static struct type *
134d01f1 16100read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16101{
e7c27a73 16102 struct objfile *objfile = cu->objfile;
c906108c 16103 struct type *type;
c906108c 16104 struct attribute *attr;
0114d602 16105 const char *name;
134d01f1 16106
348e048f
DE
16107 /* If the definition of this type lives in .debug_types, read that type.
16108 Don't follow DW_AT_specification though, that will take us back up
16109 the chain and we want to go down. */
45e58e77 16110 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
348e048f
DE
16111 if (attr)
16112 {
ac9ec31b 16113 type = get_DW_AT_signature_type (die, attr, cu);
9dc481d3 16114
ac9ec31b 16115 /* The type's CU may not be the same as CU.
02142a6c 16116 Ensure TYPE is recorded with CU in die_type_hash. */
348e048f
DE
16117 return set_die_type (die, type, cu);
16118 }
16119
c906108c
SS
16120 type = alloc_type (objfile);
16121
16122 TYPE_CODE (type) = TYPE_CODE_ENUM;
94af9270 16123 name = dwarf2_full_name (NULL, die, cu);
39cbfefa 16124 if (name != NULL)
7d455152 16125 TYPE_TAG_NAME (type) = name;
c906108c 16126
0626fc76
TT
16127 attr = dwarf2_attr (die, DW_AT_type, cu);
16128 if (attr != NULL)
16129 {
16130 struct type *underlying_type = die_type (die, cu);
16131
16132 TYPE_TARGET_TYPE (type) = underlying_type;
16133 }
16134
e142c38c 16135 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
16136 if (attr)
16137 {
16138 TYPE_LENGTH (type) = DW_UNSND (attr);
16139 }
16140 else
16141 {
16142 TYPE_LENGTH (type) = 0;
16143 }
16144
137033e9
JB
16145 /* The enumeration DIE can be incomplete. In Ada, any type can be
16146 declared as private in the package spec, and then defined only
16147 inside the package body. Such types are known as Taft Amendment
16148 Types. When another package uses such a type, an incomplete DIE
16149 may be generated by the compiler. */
02eb380e 16150 if (die_is_declaration (die, cu))
876cecd0 16151 TYPE_STUB (type) = 1;
02eb380e 16152
0626fc76
TT
16153 /* Finish the creation of this type by using the enum's children.
16154 We must call this even when the underlying type has been provided
16155 so that we can determine if we're looking at a "flag" enum. */
55426c9d
JB
16156 update_enumeration_type_from_children (die, type, cu);
16157
0626fc76
TT
16158 /* If this type has an underlying type that is not a stub, then we
16159 may use its attributes. We always use the "unsigned" attribute
16160 in this situation, because ordinarily we guess whether the type
16161 is unsigned -- but the guess can be wrong and the underlying type
16162 can tell us the reality. However, we defer to a local size
16163 attribute if one exists, because this lets the compiler override
16164 the underlying type if needed. */
16165 if (TYPE_TARGET_TYPE (type) != NULL && !TYPE_STUB (TYPE_TARGET_TYPE (type)))
16166 {
16167 TYPE_UNSIGNED (type) = TYPE_UNSIGNED (TYPE_TARGET_TYPE (type));
16168 if (TYPE_LENGTH (type) == 0)
16169 TYPE_LENGTH (type) = TYPE_LENGTH (TYPE_TARGET_TYPE (type));
16170 }
16171
3d567982
TT
16172 TYPE_DECLARED_CLASS (type) = dwarf2_flag_true_p (die, DW_AT_enum_class, cu);
16173
f792889a 16174 return set_die_type (die, type, cu);
134d01f1
DJ
16175}
16176
16177/* Given a pointer to a die which begins an enumeration, process all
16178 the dies that define the members of the enumeration, and create the
16179 symbol for the enumeration type.
16180
16181 NOTE: We reverse the order of the element list. */
16182
16183static void
16184process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
16185{
f792889a 16186 struct type *this_type;
134d01f1 16187
f792889a
DJ
16188 this_type = get_die_type (die, cu);
16189 if (this_type == NULL)
16190 this_type = read_enumeration_type (die, cu);
9dc481d3 16191
639d11d3 16192 if (die->child != NULL)
c906108c 16193 {
9dc481d3
DE
16194 struct die_info *child_die;
16195 struct symbol *sym;
16196 struct field *fields = NULL;
16197 int num_fields = 0;
15d034d0 16198 const char *name;
9dc481d3 16199
639d11d3 16200 child_die = die->child;
c906108c
SS
16201 while (child_die && child_die->tag)
16202 {
16203 if (child_die->tag != DW_TAG_enumerator)
16204 {
e7c27a73 16205 process_die (child_die, cu);
c906108c
SS
16206 }
16207 else
16208 {
39cbfefa
DJ
16209 name = dwarf2_name (child_die, cu);
16210 if (name)
c906108c 16211 {
f792889a 16212 sym = new_symbol (child_die, this_type, cu);
c906108c
SS
16213
16214 if ((num_fields % DW_FIELD_ALLOC_CHUNK) == 0)
16215 {
16216 fields = (struct field *)
16217 xrealloc (fields,
16218 (num_fields + DW_FIELD_ALLOC_CHUNK)
c5aa993b 16219 * sizeof (struct field));
c906108c
SS
16220 }
16221
3567439c 16222 FIELD_NAME (fields[num_fields]) = SYMBOL_LINKAGE_NAME (sym);
c906108c 16223 FIELD_TYPE (fields[num_fields]) = NULL;
14e75d8e 16224 SET_FIELD_ENUMVAL (fields[num_fields], SYMBOL_VALUE (sym));
c906108c
SS
16225 FIELD_BITSIZE (fields[num_fields]) = 0;
16226
16227 num_fields++;
16228 }
16229 }
16230
16231 child_die = sibling_die (child_die);
16232 }
16233
16234 if (num_fields)
16235 {
f792889a
DJ
16236 TYPE_NFIELDS (this_type) = num_fields;
16237 TYPE_FIELDS (this_type) = (struct field *)
16238 TYPE_ALLOC (this_type, sizeof (struct field) * num_fields);
16239 memcpy (TYPE_FIELDS (this_type), fields,
c906108c 16240 sizeof (struct field) * num_fields);
b8c9b27d 16241 xfree (fields);
c906108c 16242 }
c906108c 16243 }
134d01f1 16244
6c83ed52
TT
16245 /* If we are reading an enum from a .debug_types unit, and the enum
16246 is a declaration, and the enum is not the signatured type in the
16247 unit, then we do not want to add a symbol for it. Adding a
16248 symbol would in some cases obscure the true definition of the
16249 enum, giving users an incomplete type when the definition is
16250 actually available. Note that we do not want to do this for all
16251 enums which are just declarations, because C++0x allows forward
16252 enum declarations. */
3019eac3 16253 if (cu->per_cu->is_debug_types
6c83ed52
TT
16254 && die_is_declaration (die, cu))
16255 {
52dc124a 16256 struct signatured_type *sig_type;
6c83ed52 16257
c0f78cd4 16258 sig_type = (struct signatured_type *) cu->per_cu;
9c541725
PA
16259 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
16260 if (sig_type->type_offset_in_section != die->sect_off)
6c83ed52
TT
16261 return;
16262 }
16263
f792889a 16264 new_symbol (die, this_type, cu);
c906108c
SS
16265}
16266
16267/* Extract all information from a DW_TAG_array_type DIE and put it in
16268 the DIE's type field. For now, this only handles one dimensional
16269 arrays. */
16270
f792889a 16271static struct type *
e7c27a73 16272read_array_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16273{
e7c27a73 16274 struct objfile *objfile = cu->objfile;
c906108c 16275 struct die_info *child_die;
7e314c57 16276 struct type *type;
c906108c 16277 struct type *element_type, *range_type, *index_type;
c906108c 16278 struct attribute *attr;
15d034d0 16279 const char *name;
dc53a7ad 16280 unsigned int bit_stride = 0;
c906108c 16281
e7c27a73 16282 element_type = die_type (die, cu);
c906108c 16283
7e314c57
JK
16284 /* The die_type call above may have already set the type for this DIE. */
16285 type = get_die_type (die, cu);
16286 if (type)
16287 return type;
16288
dc53a7ad
JB
16289 attr = dwarf2_attr (die, DW_AT_byte_stride, cu);
16290 if (attr != NULL)
16291 bit_stride = DW_UNSND (attr) * 8;
16292
16293 attr = dwarf2_attr (die, DW_AT_bit_stride, cu);
16294 if (attr != NULL)
16295 bit_stride = DW_UNSND (attr);
16296
c906108c
SS
16297 /* Irix 6.2 native cc creates array types without children for
16298 arrays with unspecified length. */
639d11d3 16299 if (die->child == NULL)
c906108c 16300 {
46bf5051 16301 index_type = objfile_type (objfile)->builtin_int;
0c9c3474 16302 range_type = create_static_range_type (NULL, index_type, 0, -1);
dc53a7ad
JB
16303 type = create_array_type_with_stride (NULL, element_type, range_type,
16304 bit_stride);
f792889a 16305 return set_die_type (die, type, cu);
c906108c
SS
16306 }
16307
791afaa2 16308 std::vector<struct type *> range_types;
639d11d3 16309 child_die = die->child;
c906108c
SS
16310 while (child_die && child_die->tag)
16311 {
16312 if (child_die->tag == DW_TAG_subrange_type)
16313 {
f792889a 16314 struct type *child_type = read_type_die (child_die, cu);
9a619af0 16315
f792889a 16316 if (child_type != NULL)
a02abb62 16317 {
0963b4bd
MS
16318 /* The range type was succesfully read. Save it for the
16319 array type creation. */
791afaa2 16320 range_types.push_back (child_type);
a02abb62 16321 }
c906108c
SS
16322 }
16323 child_die = sibling_die (child_die);
16324 }
16325
16326 /* Dwarf2 dimensions are output from left to right, create the
16327 necessary array types in backwards order. */
7ca2d3a3 16328
c906108c 16329 type = element_type;
7ca2d3a3
DL
16330
16331 if (read_array_order (die, cu) == DW_ORD_col_major)
16332 {
16333 int i = 0;
9a619af0 16334
791afaa2 16335 while (i < range_types.size ())
dc53a7ad
JB
16336 type = create_array_type_with_stride (NULL, type, range_types[i++],
16337 bit_stride);
7ca2d3a3
DL
16338 }
16339 else
16340 {
791afaa2 16341 size_t ndim = range_types.size ();
7ca2d3a3 16342 while (ndim-- > 0)
dc53a7ad
JB
16343 type = create_array_type_with_stride (NULL, type, range_types[ndim],
16344 bit_stride);
7ca2d3a3 16345 }
c906108c 16346
f5f8a009
EZ
16347 /* Understand Dwarf2 support for vector types (like they occur on
16348 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
16349 array type. This is not part of the Dwarf2/3 standard yet, but a
16350 custom vendor extension. The main difference between a regular
16351 array and the vector variant is that vectors are passed by value
16352 to functions. */
e142c38c 16353 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
f5f8a009 16354 if (attr)
ea37ba09 16355 make_vector_type (type);
f5f8a009 16356
dbc98a8b
KW
16357 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
16358 implementation may choose to implement triple vectors using this
16359 attribute. */
16360 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16361 if (attr)
16362 {
16363 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
16364 TYPE_LENGTH (type) = DW_UNSND (attr);
16365 else
3e43a32a
MS
16366 complaint (&symfile_complaints,
16367 _("DW_AT_byte_size for array type smaller "
16368 "than the total size of elements"));
dbc98a8b
KW
16369 }
16370
39cbfefa
DJ
16371 name = dwarf2_name (die, cu);
16372 if (name)
16373 TYPE_NAME (type) = name;
6e70227d 16374
0963b4bd 16375 /* Install the type in the die. */
7e314c57
JK
16376 set_die_type (die, type, cu);
16377
16378 /* set_die_type should be already done. */
b4ba55a1
JB
16379 set_descriptive_type (type, die, cu);
16380
7e314c57 16381 return type;
c906108c
SS
16382}
16383
7ca2d3a3 16384static enum dwarf_array_dim_ordering
6e70227d 16385read_array_order (struct die_info *die, struct dwarf2_cu *cu)
7ca2d3a3
DL
16386{
16387 struct attribute *attr;
16388
16389 attr = dwarf2_attr (die, DW_AT_ordering, cu);
16390
aead7601
SM
16391 if (attr)
16392 return (enum dwarf_array_dim_ordering) DW_SND (attr);
7ca2d3a3 16393
0963b4bd
MS
16394 /* GNU F77 is a special case, as at 08/2004 array type info is the
16395 opposite order to the dwarf2 specification, but data is still
16396 laid out as per normal fortran.
7ca2d3a3 16397
0963b4bd
MS
16398 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
16399 version checking. */
7ca2d3a3 16400
905e0470
PM
16401 if (cu->language == language_fortran
16402 && cu->producer && strstr (cu->producer, "GNU F77"))
7ca2d3a3
DL
16403 {
16404 return DW_ORD_row_major;
16405 }
16406
6e70227d 16407 switch (cu->language_defn->la_array_ordering)
7ca2d3a3
DL
16408 {
16409 case array_column_major:
16410 return DW_ORD_col_major;
16411 case array_row_major:
16412 default:
16413 return DW_ORD_row_major;
16414 };
16415}
16416
72019c9c 16417/* Extract all information from a DW_TAG_set_type DIE and put it in
0963b4bd 16418 the DIE's type field. */
72019c9c 16419
f792889a 16420static struct type *
72019c9c
GM
16421read_set_type (struct die_info *die, struct dwarf2_cu *cu)
16422{
7e314c57
JK
16423 struct type *domain_type, *set_type;
16424 struct attribute *attr;
f792889a 16425
7e314c57
JK
16426 domain_type = die_type (die, cu);
16427
16428 /* The die_type call above may have already set the type for this DIE. */
16429 set_type = get_die_type (die, cu);
16430 if (set_type)
16431 return set_type;
16432
16433 set_type = create_set_type (NULL, domain_type);
16434
16435 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
d09039dd
PM
16436 if (attr)
16437 TYPE_LENGTH (set_type) = DW_UNSND (attr);
7e314c57 16438
f792889a 16439 return set_die_type (die, set_type, cu);
72019c9c 16440}
7ca2d3a3 16441
0971de02
TT
16442/* A helper for read_common_block that creates a locexpr baton.
16443 SYM is the symbol which we are marking as computed.
16444 COMMON_DIE is the DIE for the common block.
16445 COMMON_LOC is the location expression attribute for the common
16446 block itself.
16447 MEMBER_LOC is the location expression attribute for the particular
16448 member of the common block that we are processing.
16449 CU is the CU from which the above come. */
16450
16451static void
16452mark_common_block_symbol_computed (struct symbol *sym,
16453 struct die_info *common_die,
16454 struct attribute *common_loc,
16455 struct attribute *member_loc,
16456 struct dwarf2_cu *cu)
16457{
16458 struct objfile *objfile = dwarf2_per_objfile->objfile;
16459 struct dwarf2_locexpr_baton *baton;
16460 gdb_byte *ptr;
16461 unsigned int cu_off;
16462 enum bfd_endian byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
16463 LONGEST offset = 0;
16464
16465 gdb_assert (common_loc && member_loc);
16466 gdb_assert (attr_form_is_block (common_loc));
16467 gdb_assert (attr_form_is_block (member_loc)
16468 || attr_form_is_constant (member_loc));
16469
8d749320 16470 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
0971de02
TT
16471 baton->per_cu = cu->per_cu;
16472 gdb_assert (baton->per_cu);
16473
16474 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
16475
16476 if (attr_form_is_constant (member_loc))
16477 {
16478 offset = dwarf2_get_attr_constant_value (member_loc, 0);
16479 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
16480 }
16481 else
16482 baton->size += DW_BLOCK (member_loc)->size;
16483
224c3ddb 16484 ptr = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, baton->size);
0971de02
TT
16485 baton->data = ptr;
16486
16487 *ptr++ = DW_OP_call4;
9c541725 16488 cu_off = common_die->sect_off - cu->per_cu->sect_off;
0971de02
TT
16489 store_unsigned_integer (ptr, 4, byte_order, cu_off);
16490 ptr += 4;
16491
16492 if (attr_form_is_constant (member_loc))
16493 {
16494 *ptr++ = DW_OP_addr;
16495 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
16496 ptr += cu->header.addr_size;
16497 }
16498 else
16499 {
16500 /* We have to copy the data here, because DW_OP_call4 will only
16501 use a DW_AT_location attribute. */
16502 memcpy (ptr, DW_BLOCK (member_loc)->data, DW_BLOCK (member_loc)->size);
16503 ptr += DW_BLOCK (member_loc)->size;
16504 }
16505
16506 *ptr++ = DW_OP_plus;
16507 gdb_assert (ptr - baton->data == baton->size);
16508
0971de02 16509 SYMBOL_LOCATION_BATON (sym) = baton;
f1e6e072 16510 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
0971de02
TT
16511}
16512
4357ac6c
TT
16513/* Create appropriate locally-scoped variables for all the
16514 DW_TAG_common_block entries. Also create a struct common_block
16515 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
16516 is used to sepate the common blocks name namespace from regular
16517 variable names. */
c906108c
SS
16518
16519static void
e7c27a73 16520read_common_block (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16521{
0971de02
TT
16522 struct attribute *attr;
16523
16524 attr = dwarf2_attr (die, DW_AT_location, cu);
16525 if (attr)
16526 {
16527 /* Support the .debug_loc offsets. */
16528 if (attr_form_is_block (attr))
16529 {
16530 /* Ok. */
16531 }
16532 else if (attr_form_is_section_offset (attr))
16533 {
16534 dwarf2_complex_location_expr_complaint ();
16535 attr = NULL;
16536 }
16537 else
16538 {
16539 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
16540 "common block member");
16541 attr = NULL;
16542 }
16543 }
16544
639d11d3 16545 if (die->child != NULL)
c906108c 16546 {
4357ac6c
TT
16547 struct objfile *objfile = cu->objfile;
16548 struct die_info *child_die;
16549 size_t n_entries = 0, size;
16550 struct common_block *common_block;
16551 struct symbol *sym;
74ac6d43 16552
4357ac6c
TT
16553 for (child_die = die->child;
16554 child_die && child_die->tag;
16555 child_die = sibling_die (child_die))
16556 ++n_entries;
16557
16558 size = (sizeof (struct common_block)
16559 + (n_entries - 1) * sizeof (struct symbol *));
224c3ddb
SM
16560 common_block
16561 = (struct common_block *) obstack_alloc (&objfile->objfile_obstack,
16562 size);
4357ac6c
TT
16563 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
16564 common_block->n_entries = 0;
16565
16566 for (child_die = die->child;
16567 child_die && child_die->tag;
16568 child_die = sibling_die (child_die))
16569 {
16570 /* Create the symbol in the DW_TAG_common_block block in the current
16571 symbol scope. */
e7c27a73 16572 sym = new_symbol (child_die, NULL, cu);
0971de02
TT
16573 if (sym != NULL)
16574 {
16575 struct attribute *member_loc;
16576
16577 common_block->contents[common_block->n_entries++] = sym;
16578
16579 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
16580 cu);
16581 if (member_loc)
16582 {
16583 /* GDB has handled this for a long time, but it is
16584 not specified by DWARF. It seems to have been
16585 emitted by gfortran at least as recently as:
16586 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
16587 complaint (&symfile_complaints,
16588 _("Variable in common block has "
16589 "DW_AT_data_member_location "
16590 "- DIE at 0x%x [in module %s]"),
9c541725 16591 to_underlying (child_die->sect_off),
4262abfb 16592 objfile_name (cu->objfile));
0971de02
TT
16593
16594 if (attr_form_is_section_offset (member_loc))
16595 dwarf2_complex_location_expr_complaint ();
16596 else if (attr_form_is_constant (member_loc)
16597 || attr_form_is_block (member_loc))
16598 {
16599 if (attr)
16600 mark_common_block_symbol_computed (sym, die, attr,
16601 member_loc, cu);
16602 }
16603 else
16604 dwarf2_complex_location_expr_complaint ();
16605 }
16606 }
c906108c 16607 }
4357ac6c
TT
16608
16609 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
16610 SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
c906108c
SS
16611 }
16612}
16613
0114d602 16614/* Create a type for a C++ namespace. */
d9fa45fe 16615
0114d602
DJ
16616static struct type *
16617read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
d9fa45fe 16618{
e7c27a73 16619 struct objfile *objfile = cu->objfile;
0114d602 16620 const char *previous_prefix, *name;
9219021c 16621 int is_anonymous;
0114d602
DJ
16622 struct type *type;
16623
16624 /* For extensions, reuse the type of the original namespace. */
16625 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
16626 {
16627 struct die_info *ext_die;
16628 struct dwarf2_cu *ext_cu = cu;
9a619af0 16629
0114d602
DJ
16630 ext_die = dwarf2_extension (die, &ext_cu);
16631 type = read_type_die (ext_die, ext_cu);
9dc481d3
DE
16632
16633 /* EXT_CU may not be the same as CU.
02142a6c 16634 Ensure TYPE is recorded with CU in die_type_hash. */
0114d602
DJ
16635 return set_die_type (die, type, cu);
16636 }
9219021c 16637
e142c38c 16638 name = namespace_name (die, &is_anonymous, cu);
9219021c
DC
16639
16640 /* Now build the name of the current namespace. */
16641
0114d602
DJ
16642 previous_prefix = determine_prefix (die, cu);
16643 if (previous_prefix[0] != '\0')
16644 name = typename_concat (&objfile->objfile_obstack,
f55ee35c 16645 previous_prefix, name, 0, cu);
0114d602
DJ
16646
16647 /* Create the type. */
19f392bc 16648 type = init_type (objfile, TYPE_CODE_NAMESPACE, 0, name);
0114d602
DJ
16649 TYPE_TAG_NAME (type) = TYPE_NAME (type);
16650
60531b24 16651 return set_die_type (die, type, cu);
0114d602
DJ
16652}
16653
22cee43f 16654/* Read a namespace scope. */
0114d602
DJ
16655
16656static void
16657read_namespace (struct die_info *die, struct dwarf2_cu *cu)
16658{
16659 struct objfile *objfile = cu->objfile;
0114d602 16660 int is_anonymous;
9219021c 16661
5c4e30ca
DC
16662 /* Add a symbol associated to this if we haven't seen the namespace
16663 before. Also, add a using directive if it's an anonymous
16664 namespace. */
9219021c 16665
f2f0e013 16666 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
5c4e30ca
DC
16667 {
16668 struct type *type;
16669
0114d602 16670 type = read_type_die (die, cu);
e7c27a73 16671 new_symbol (die, type, cu);
5c4e30ca 16672
e8e80198 16673 namespace_name (die, &is_anonymous, cu);
5c4e30ca 16674 if (is_anonymous)
0114d602
DJ
16675 {
16676 const char *previous_prefix = determine_prefix (die, cu);
9a619af0 16677
eb1e02fd 16678 std::vector<const char *> excludes;
22cee43f
PMR
16679 add_using_directive (using_directives (cu->language),
16680 previous_prefix, TYPE_NAME (type), NULL,
eb1e02fd 16681 NULL, excludes, 0, &objfile->objfile_obstack);
0114d602 16682 }
5c4e30ca 16683 }
9219021c 16684
639d11d3 16685 if (die->child != NULL)
d9fa45fe 16686 {
639d11d3 16687 struct die_info *child_die = die->child;
6e70227d 16688
d9fa45fe
DC
16689 while (child_die && child_die->tag)
16690 {
e7c27a73 16691 process_die (child_die, cu);
d9fa45fe
DC
16692 child_die = sibling_die (child_die);
16693 }
16694 }
38d518c9
EZ
16695}
16696
f55ee35c
JK
16697/* Read a Fortran module as type. This DIE can be only a declaration used for
16698 imported module. Still we need that type as local Fortran "use ... only"
16699 declaration imports depend on the created type in determine_prefix. */
16700
16701static struct type *
16702read_module_type (struct die_info *die, struct dwarf2_cu *cu)
16703{
16704 struct objfile *objfile = cu->objfile;
15d034d0 16705 const char *module_name;
f55ee35c
JK
16706 struct type *type;
16707
16708 module_name = dwarf2_name (die, cu);
16709 if (!module_name)
3e43a32a
MS
16710 complaint (&symfile_complaints,
16711 _("DW_TAG_module has no name, offset 0x%x"),
9c541725 16712 to_underlying (die->sect_off));
19f392bc 16713 type = init_type (objfile, TYPE_CODE_MODULE, 0, module_name);
f55ee35c
JK
16714
16715 /* determine_prefix uses TYPE_TAG_NAME. */
16716 TYPE_TAG_NAME (type) = TYPE_NAME (type);
16717
16718 return set_die_type (die, type, cu);
16719}
16720
5d7cb8df
JK
16721/* Read a Fortran module. */
16722
16723static void
16724read_module (struct die_info *die, struct dwarf2_cu *cu)
16725{
16726 struct die_info *child_die = die->child;
530e8392
KB
16727 struct type *type;
16728
16729 type = read_type_die (die, cu);
16730 new_symbol (die, type, cu);
5d7cb8df 16731
5d7cb8df
JK
16732 while (child_die && child_die->tag)
16733 {
16734 process_die (child_die, cu);
16735 child_die = sibling_die (child_die);
16736 }
16737}
16738
38d518c9
EZ
16739/* Return the name of the namespace represented by DIE. Set
16740 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
16741 namespace. */
16742
16743static const char *
e142c38c 16744namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
38d518c9
EZ
16745{
16746 struct die_info *current_die;
16747 const char *name = NULL;
16748
16749 /* Loop through the extensions until we find a name. */
16750
16751 for (current_die = die;
16752 current_die != NULL;
f2f0e013 16753 current_die = dwarf2_extension (die, &cu))
38d518c9 16754 {
96553a0c
DE
16755 /* We don't use dwarf2_name here so that we can detect the absence
16756 of a name -> anonymous namespace. */
7d45c7c3 16757 name = dwarf2_string_attr (die, DW_AT_name, cu);
96553a0c 16758
38d518c9
EZ
16759 if (name != NULL)
16760 break;
16761 }
16762
16763 /* Is it an anonymous namespace? */
16764
16765 *is_anonymous = (name == NULL);
16766 if (*is_anonymous)
2b1dbab0 16767 name = CP_ANONYMOUS_NAMESPACE_STR;
38d518c9
EZ
16768
16769 return name;
d9fa45fe
DC
16770}
16771
c906108c
SS
16772/* Extract all information from a DW_TAG_pointer_type DIE and add to
16773 the user defined type vector. */
16774
f792889a 16775static struct type *
e7c27a73 16776read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16777{
5e2b427d 16778 struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
e7c27a73 16779 struct comp_unit_head *cu_header = &cu->header;
c906108c 16780 struct type *type;
8b2dbe47
KB
16781 struct attribute *attr_byte_size;
16782 struct attribute *attr_address_class;
16783 int byte_size, addr_class;
7e314c57
JK
16784 struct type *target_type;
16785
16786 target_type = die_type (die, cu);
c906108c 16787
7e314c57
JK
16788 /* The die_type call above may have already set the type for this DIE. */
16789 type = get_die_type (die, cu);
16790 if (type)
16791 return type;
16792
16793 type = lookup_pointer_type (target_type);
8b2dbe47 16794
e142c38c 16795 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
8b2dbe47
KB
16796 if (attr_byte_size)
16797 byte_size = DW_UNSND (attr_byte_size);
c906108c 16798 else
8b2dbe47
KB
16799 byte_size = cu_header->addr_size;
16800
e142c38c 16801 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
8b2dbe47
KB
16802 if (attr_address_class)
16803 addr_class = DW_UNSND (attr_address_class);
16804 else
16805 addr_class = DW_ADDR_none;
16806
16807 /* If the pointer size or address class is different than the
16808 default, create a type variant marked as such and set the
16809 length accordingly. */
16810 if (TYPE_LENGTH (type) != byte_size || addr_class != DW_ADDR_none)
c906108c 16811 {
5e2b427d 16812 if (gdbarch_address_class_type_flags_p (gdbarch))
8b2dbe47
KB
16813 {
16814 int type_flags;
16815
849957d9 16816 type_flags = gdbarch_address_class_type_flags
5e2b427d 16817 (gdbarch, byte_size, addr_class);
876cecd0
TT
16818 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
16819 == 0);
8b2dbe47
KB
16820 type = make_type_with_address_space (type, type_flags);
16821 }
16822 else if (TYPE_LENGTH (type) != byte_size)
16823 {
3e43a32a
MS
16824 complaint (&symfile_complaints,
16825 _("invalid pointer size %d"), byte_size);
8b2dbe47 16826 }
6e70227d 16827 else
9a619af0
MS
16828 {
16829 /* Should we also complain about unhandled address classes? */
16830 }
c906108c 16831 }
8b2dbe47
KB
16832
16833 TYPE_LENGTH (type) = byte_size;
f792889a 16834 return set_die_type (die, type, cu);
c906108c
SS
16835}
16836
16837/* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
16838 the user defined type vector. */
16839
f792889a 16840static struct type *
e7c27a73 16841read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
16842{
16843 struct type *type;
16844 struct type *to_type;
16845 struct type *domain;
16846
e7c27a73
DJ
16847 to_type = die_type (die, cu);
16848 domain = die_containing_type (die, cu);
0d5de010 16849
7e314c57
JK
16850 /* The calls above may have already set the type for this DIE. */
16851 type = get_die_type (die, cu);
16852 if (type)
16853 return type;
16854
0d5de010
DJ
16855 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
16856 type = lookup_methodptr_type (to_type);
7078baeb
TT
16857 else if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_FUNC)
16858 {
16859 struct type *new_type = alloc_type (cu->objfile);
16860
16861 smash_to_method_type (new_type, domain, TYPE_TARGET_TYPE (to_type),
16862 TYPE_FIELDS (to_type), TYPE_NFIELDS (to_type),
16863 TYPE_VARARGS (to_type));
16864 type = lookup_methodptr_type (new_type);
16865 }
0d5de010
DJ
16866 else
16867 type = lookup_memberptr_type (to_type, domain);
c906108c 16868
f792889a 16869 return set_die_type (die, type, cu);
c906108c
SS
16870}
16871
4297a3f0 16872/* Extract all information from a DW_TAG_{rvalue_,}reference_type DIE and add to
c906108c
SS
16873 the user defined type vector. */
16874
f792889a 16875static struct type *
4297a3f0
AV
16876read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu,
16877 enum type_code refcode)
c906108c 16878{
e7c27a73 16879 struct comp_unit_head *cu_header = &cu->header;
7e314c57 16880 struct type *type, *target_type;
c906108c
SS
16881 struct attribute *attr;
16882
4297a3f0
AV
16883 gdb_assert (refcode == TYPE_CODE_REF || refcode == TYPE_CODE_RVALUE_REF);
16884
7e314c57
JK
16885 target_type = die_type (die, cu);
16886
16887 /* The die_type call above may have already set the type for this DIE. */
16888 type = get_die_type (die, cu);
16889 if (type)
16890 return type;
16891
4297a3f0 16892 type = lookup_reference_type (target_type, refcode);
e142c38c 16893 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
16894 if (attr)
16895 {
16896 TYPE_LENGTH (type) = DW_UNSND (attr);
16897 }
16898 else
16899 {
107d2387 16900 TYPE_LENGTH (type) = cu_header->addr_size;
c906108c 16901 }
f792889a 16902 return set_die_type (die, type, cu);
c906108c
SS
16903}
16904
cf363f18
MW
16905/* Add the given cv-qualifiers to the element type of the array. GCC
16906 outputs DWARF type qualifiers that apply to an array, not the
16907 element type. But GDB relies on the array element type to carry
16908 the cv-qualifiers. This mimics section 6.7.3 of the C99
16909 specification. */
16910
16911static struct type *
16912add_array_cv_type (struct die_info *die, struct dwarf2_cu *cu,
16913 struct type *base_type, int cnst, int voltl)
16914{
16915 struct type *el_type, *inner_array;
16916
16917 base_type = copy_type (base_type);
16918 inner_array = base_type;
16919
16920 while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
16921 {
16922 TYPE_TARGET_TYPE (inner_array) =
16923 copy_type (TYPE_TARGET_TYPE (inner_array));
16924 inner_array = TYPE_TARGET_TYPE (inner_array);
16925 }
16926
16927 el_type = TYPE_TARGET_TYPE (inner_array);
16928 cnst |= TYPE_CONST (el_type);
16929 voltl |= TYPE_VOLATILE (el_type);
16930 TYPE_TARGET_TYPE (inner_array) = make_cv_type (cnst, voltl, el_type, NULL);
16931
16932 return set_die_type (die, base_type, cu);
16933}
16934
f792889a 16935static struct type *
e7c27a73 16936read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16937{
f792889a 16938 struct type *base_type, *cv_type;
c906108c 16939
e7c27a73 16940 base_type = die_type (die, cu);
7e314c57
JK
16941
16942 /* The die_type call above may have already set the type for this DIE. */
16943 cv_type = get_die_type (die, cu);
16944 if (cv_type)
16945 return cv_type;
16946
2f608a3a
KW
16947 /* In case the const qualifier is applied to an array type, the element type
16948 is so qualified, not the array type (section 6.7.3 of C99). */
16949 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
cf363f18 16950 return add_array_cv_type (die, cu, base_type, 1, 0);
2f608a3a 16951
f792889a
DJ
16952 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
16953 return set_die_type (die, cv_type, cu);
c906108c
SS
16954}
16955
f792889a 16956static struct type *
e7c27a73 16957read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16958{
f792889a 16959 struct type *base_type, *cv_type;
c906108c 16960
e7c27a73 16961 base_type = die_type (die, cu);
7e314c57
JK
16962
16963 /* The die_type call above may have already set the type for this DIE. */
16964 cv_type = get_die_type (die, cu);
16965 if (cv_type)
16966 return cv_type;
16967
cf363f18
MW
16968 /* In case the volatile qualifier is applied to an array type, the
16969 element type is so qualified, not the array type (section 6.7.3
16970 of C99). */
16971 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
16972 return add_array_cv_type (die, cu, base_type, 0, 1);
16973
f792889a
DJ
16974 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
16975 return set_die_type (die, cv_type, cu);
c906108c
SS
16976}
16977
06d66ee9
TT
16978/* Handle DW_TAG_restrict_type. */
16979
16980static struct type *
16981read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
16982{
16983 struct type *base_type, *cv_type;
16984
16985 base_type = die_type (die, cu);
16986
16987 /* The die_type call above may have already set the type for this DIE. */
16988 cv_type = get_die_type (die, cu);
16989 if (cv_type)
16990 return cv_type;
16991
16992 cv_type = make_restrict_type (base_type);
16993 return set_die_type (die, cv_type, cu);
16994}
16995
a2c2acaf
MW
16996/* Handle DW_TAG_atomic_type. */
16997
16998static struct type *
16999read_tag_atomic_type (struct die_info *die, struct dwarf2_cu *cu)
17000{
17001 struct type *base_type, *cv_type;
17002
17003 base_type = die_type (die, cu);
17004
17005 /* The die_type call above may have already set the type for this DIE. */
17006 cv_type = get_die_type (die, cu);
17007 if (cv_type)
17008 return cv_type;
17009
17010 cv_type = make_atomic_type (base_type);
17011 return set_die_type (die, cv_type, cu);
17012}
17013
c906108c
SS
17014/* Extract all information from a DW_TAG_string_type DIE and add to
17015 the user defined type vector. It isn't really a user defined type,
17016 but it behaves like one, with other DIE's using an AT_user_def_type
17017 attribute to reference it. */
17018
f792889a 17019static struct type *
e7c27a73 17020read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17021{
e7c27a73 17022 struct objfile *objfile = cu->objfile;
3b7538c0 17023 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
17024 struct type *type, *range_type, *index_type, *char_type;
17025 struct attribute *attr;
17026 unsigned int length;
17027
e142c38c 17028 attr = dwarf2_attr (die, DW_AT_string_length, cu);
c906108c
SS
17029 if (attr)
17030 {
17031 length = DW_UNSND (attr);
17032 }
17033 else
17034 {
0963b4bd 17035 /* Check for the DW_AT_byte_size attribute. */
e142c38c 17036 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
b21b22e0
PS
17037 if (attr)
17038 {
17039 length = DW_UNSND (attr);
17040 }
17041 else
17042 {
17043 length = 1;
17044 }
c906108c 17045 }
6ccb9162 17046
46bf5051 17047 index_type = objfile_type (objfile)->builtin_int;
0c9c3474 17048 range_type = create_static_range_type (NULL, index_type, 1, length);
3b7538c0
UW
17049 char_type = language_string_char_type (cu->language_defn, gdbarch);
17050 type = create_string_type (NULL, char_type, range_type);
6ccb9162 17051
f792889a 17052 return set_die_type (die, type, cu);
c906108c
SS
17053}
17054
4d804846
JB
17055/* Assuming that DIE corresponds to a function, returns nonzero
17056 if the function is prototyped. */
17057
17058static int
17059prototyped_function_p (struct die_info *die, struct dwarf2_cu *cu)
17060{
17061 struct attribute *attr;
17062
17063 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
17064 if (attr && (DW_UNSND (attr) != 0))
17065 return 1;
17066
17067 /* The DWARF standard implies that the DW_AT_prototyped attribute
17068 is only meaninful for C, but the concept also extends to other
17069 languages that allow unprototyped functions (Eg: Objective C).
17070 For all other languages, assume that functions are always
17071 prototyped. */
17072 if (cu->language != language_c
17073 && cu->language != language_objc
17074 && cu->language != language_opencl)
17075 return 1;
17076
17077 /* RealView does not emit DW_AT_prototyped. We can not distinguish
17078 prototyped and unprototyped functions; default to prototyped,
17079 since that is more common in modern code (and RealView warns
17080 about unprototyped functions). */
17081 if (producer_is_realview (cu->producer))
17082 return 1;
17083
17084 return 0;
17085}
17086
c906108c
SS
17087/* Handle DIES due to C code like:
17088
17089 struct foo
c5aa993b
JM
17090 {
17091 int (*funcp)(int a, long l);
17092 int b;
17093 };
c906108c 17094
0963b4bd 17095 ('funcp' generates a DW_TAG_subroutine_type DIE). */
c906108c 17096
f792889a 17097static struct type *
e7c27a73 17098read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17099{
bb5ed363 17100 struct objfile *objfile = cu->objfile;
0963b4bd
MS
17101 struct type *type; /* Type that this function returns. */
17102 struct type *ftype; /* Function that returns above type. */
c906108c
SS
17103 struct attribute *attr;
17104
e7c27a73 17105 type = die_type (die, cu);
7e314c57
JK
17106
17107 /* The die_type call above may have already set the type for this DIE. */
17108 ftype = get_die_type (die, cu);
17109 if (ftype)
17110 return ftype;
17111
0c8b41f1 17112 ftype = lookup_function_type (type);
c906108c 17113
4d804846 17114 if (prototyped_function_p (die, cu))
a6c727b2 17115 TYPE_PROTOTYPED (ftype) = 1;
c906108c 17116
c055b101
CV
17117 /* Store the calling convention in the type if it's available in
17118 the subroutine die. Otherwise set the calling convention to
17119 the default value DW_CC_normal. */
17120 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
54fcddd0
UW
17121 if (attr)
17122 TYPE_CALLING_CONVENTION (ftype) = DW_UNSND (attr);
17123 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
17124 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
17125 else
17126 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
76c10ea2 17127
743649fd
MW
17128 /* Record whether the function returns normally to its caller or not
17129 if the DWARF producer set that information. */
17130 attr = dwarf2_attr (die, DW_AT_noreturn, cu);
17131 if (attr && (DW_UNSND (attr) != 0))
17132 TYPE_NO_RETURN (ftype) = 1;
17133
76c10ea2
GM
17134 /* We need to add the subroutine type to the die immediately so
17135 we don't infinitely recurse when dealing with parameters
0963b4bd 17136 declared as the same subroutine type. */
76c10ea2 17137 set_die_type (die, ftype, cu);
6e70227d 17138
639d11d3 17139 if (die->child != NULL)
c906108c 17140 {
bb5ed363 17141 struct type *void_type = objfile_type (objfile)->builtin_void;
c906108c 17142 struct die_info *child_die;
8072405b 17143 int nparams, iparams;
c906108c
SS
17144
17145 /* Count the number of parameters.
17146 FIXME: GDB currently ignores vararg functions, but knows about
17147 vararg member functions. */
8072405b 17148 nparams = 0;
639d11d3 17149 child_die = die->child;
c906108c
SS
17150 while (child_die && child_die->tag)
17151 {
17152 if (child_die->tag == DW_TAG_formal_parameter)
17153 nparams++;
17154 else if (child_die->tag == DW_TAG_unspecified_parameters)
876cecd0 17155 TYPE_VARARGS (ftype) = 1;
c906108c
SS
17156 child_die = sibling_die (child_die);
17157 }
17158
17159 /* Allocate storage for parameters and fill them in. */
17160 TYPE_NFIELDS (ftype) = nparams;
17161 TYPE_FIELDS (ftype) = (struct field *)
ae5a43e0 17162 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
c906108c 17163
8072405b
JK
17164 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
17165 even if we error out during the parameters reading below. */
17166 for (iparams = 0; iparams < nparams; iparams++)
17167 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
17168
17169 iparams = 0;
639d11d3 17170 child_die = die->child;
c906108c
SS
17171 while (child_die && child_die->tag)
17172 {
17173 if (child_die->tag == DW_TAG_formal_parameter)
17174 {
3ce3b1ba
PA
17175 struct type *arg_type;
17176
17177 /* DWARF version 2 has no clean way to discern C++
17178 static and non-static member functions. G++ helps
17179 GDB by marking the first parameter for non-static
17180 member functions (which is the this pointer) as
17181 artificial. We pass this information to
17182 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
17183
17184 DWARF version 3 added DW_AT_object_pointer, which GCC
17185 4.5 does not yet generate. */
e142c38c 17186 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
c906108c
SS
17187 if (attr)
17188 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
17189 else
9c37b5ae 17190 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
3ce3b1ba
PA
17191 arg_type = die_type (child_die, cu);
17192
17193 /* RealView does not mark THIS as const, which the testsuite
17194 expects. GCC marks THIS as const in method definitions,
17195 but not in the class specifications (GCC PR 43053). */
17196 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
17197 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
17198 {
17199 int is_this = 0;
17200 struct dwarf2_cu *arg_cu = cu;
17201 const char *name = dwarf2_name (child_die, cu);
17202
17203 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
17204 if (attr)
17205 {
17206 /* If the compiler emits this, use it. */
17207 if (follow_die_ref (die, attr, &arg_cu) == child_die)
17208 is_this = 1;
17209 }
17210 else if (name && strcmp (name, "this") == 0)
17211 /* Function definitions will have the argument names. */
17212 is_this = 1;
17213 else if (name == NULL && iparams == 0)
17214 /* Declarations may not have the names, so like
17215 elsewhere in GDB, assume an artificial first
17216 argument is "this". */
17217 is_this = 1;
17218
17219 if (is_this)
17220 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
17221 arg_type, 0);
17222 }
17223
17224 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
c906108c
SS
17225 iparams++;
17226 }
17227 child_die = sibling_die (child_die);
17228 }
17229 }
17230
76c10ea2 17231 return ftype;
c906108c
SS
17232}
17233
f792889a 17234static struct type *
e7c27a73 17235read_typedef (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17236{
e7c27a73 17237 struct objfile *objfile = cu->objfile;
0114d602 17238 const char *name = NULL;
3c8e0968 17239 struct type *this_type, *target_type;
c906108c 17240
94af9270 17241 name = dwarf2_full_name (NULL, die, cu);
19f392bc
UW
17242 this_type = init_type (objfile, TYPE_CODE_TYPEDEF, 0, name);
17243 TYPE_TARGET_STUB (this_type) = 1;
f792889a 17244 set_die_type (die, this_type, cu);
3c8e0968
DE
17245 target_type = die_type (die, cu);
17246 if (target_type != this_type)
17247 TYPE_TARGET_TYPE (this_type) = target_type;
17248 else
17249 {
17250 /* Self-referential typedefs are, it seems, not allowed by the DWARF
17251 spec and cause infinite loops in GDB. */
17252 complaint (&symfile_complaints,
17253 _("Self-referential DW_TAG_typedef "
17254 "- DIE at 0x%x [in module %s]"),
9c541725 17255 to_underlying (die->sect_off), objfile_name (objfile));
3c8e0968
DE
17256 TYPE_TARGET_TYPE (this_type) = NULL;
17257 }
f792889a 17258 return this_type;
c906108c
SS
17259}
17260
9b790ce7
UW
17261/* Allocate a floating-point type of size BITS and name NAME. Pass NAME_HINT
17262 (which may be different from NAME) to the architecture back-end to allow
17263 it to guess the correct format if necessary. */
17264
17265static struct type *
17266dwarf2_init_float_type (struct objfile *objfile, int bits, const char *name,
17267 const char *name_hint)
17268{
17269 struct gdbarch *gdbarch = get_objfile_arch (objfile);
17270 const struct floatformat **format;
17271 struct type *type;
17272
17273 format = gdbarch_floatformat_for_type (gdbarch, name_hint, bits);
17274 if (format)
17275 type = init_float_type (objfile, bits, name, format);
17276 else
77b7c781 17277 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
9b790ce7
UW
17278
17279 return type;
17280}
17281
c906108c
SS
17282/* Find a representation of a given base type and install
17283 it in the TYPE field of the die. */
17284
f792889a 17285static struct type *
e7c27a73 17286read_base_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17287{
e7c27a73 17288 struct objfile *objfile = cu->objfile;
c906108c
SS
17289 struct type *type;
17290 struct attribute *attr;
19f392bc 17291 int encoding = 0, bits = 0;
15d034d0 17292 const char *name;
c906108c 17293
e142c38c 17294 attr = dwarf2_attr (die, DW_AT_encoding, cu);
c906108c
SS
17295 if (attr)
17296 {
17297 encoding = DW_UNSND (attr);
17298 }
e142c38c 17299 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
17300 if (attr)
17301 {
19f392bc 17302 bits = DW_UNSND (attr) * TARGET_CHAR_BIT;
c906108c 17303 }
39cbfefa 17304 name = dwarf2_name (die, cu);
6ccb9162 17305 if (!name)
c906108c 17306 {
6ccb9162
UW
17307 complaint (&symfile_complaints,
17308 _("DW_AT_name missing from DW_TAG_base_type"));
c906108c 17309 }
6ccb9162
UW
17310
17311 switch (encoding)
c906108c 17312 {
6ccb9162
UW
17313 case DW_ATE_address:
17314 /* Turn DW_ATE_address into a void * pointer. */
77b7c781 17315 type = init_type (objfile, TYPE_CODE_VOID, TARGET_CHAR_BIT, NULL);
19f392bc 17316 type = init_pointer_type (objfile, bits, name, type);
6ccb9162
UW
17317 break;
17318 case DW_ATE_boolean:
19f392bc 17319 type = init_boolean_type (objfile, bits, 1, name);
6ccb9162
UW
17320 break;
17321 case DW_ATE_complex_float:
9b790ce7 17322 type = dwarf2_init_float_type (objfile, bits / 2, NULL, name);
19f392bc 17323 type = init_complex_type (objfile, name, type);
6ccb9162
UW
17324 break;
17325 case DW_ATE_decimal_float:
19f392bc 17326 type = init_decfloat_type (objfile, bits, name);
6ccb9162
UW
17327 break;
17328 case DW_ATE_float:
9b790ce7 17329 type = dwarf2_init_float_type (objfile, bits, name, name);
6ccb9162
UW
17330 break;
17331 case DW_ATE_signed:
19f392bc 17332 type = init_integer_type (objfile, bits, 0, name);
6ccb9162
UW
17333 break;
17334 case DW_ATE_unsigned:
3b2b8fea
TT
17335 if (cu->language == language_fortran
17336 && name
61012eef 17337 && startswith (name, "character("))
19f392bc
UW
17338 type = init_character_type (objfile, bits, 1, name);
17339 else
17340 type = init_integer_type (objfile, bits, 1, name);
6ccb9162
UW
17341 break;
17342 case DW_ATE_signed_char:
6e70227d 17343 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea
TT
17344 || cu->language == language_pascal
17345 || cu->language == language_fortran)
19f392bc
UW
17346 type = init_character_type (objfile, bits, 0, name);
17347 else
17348 type = init_integer_type (objfile, bits, 0, name);
6ccb9162
UW
17349 break;
17350 case DW_ATE_unsigned_char:
868a0084 17351 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea 17352 || cu->language == language_pascal
c44af4eb
TT
17353 || cu->language == language_fortran
17354 || cu->language == language_rust)
19f392bc
UW
17355 type = init_character_type (objfile, bits, 1, name);
17356 else
17357 type = init_integer_type (objfile, bits, 1, name);
6ccb9162 17358 break;
75079b2b 17359 case DW_ATE_UTF:
53e710ac
PA
17360 {
17361 gdbarch *arch = get_objfile_arch (objfile);
17362
17363 if (bits == 16)
17364 type = builtin_type (arch)->builtin_char16;
17365 else if (bits == 32)
17366 type = builtin_type (arch)->builtin_char32;
17367 else
17368 {
17369 complaint (&symfile_complaints,
17370 _("unsupported DW_ATE_UTF bit size: '%d'"),
17371 bits);
17372 type = init_integer_type (objfile, bits, 1, name);
17373 }
17374 return set_die_type (die, type, cu);
17375 }
75079b2b
TT
17376 break;
17377
6ccb9162
UW
17378 default:
17379 complaint (&symfile_complaints, _("unsupported DW_AT_encoding: '%s'"),
17380 dwarf_type_encoding_name (encoding));
77b7c781 17381 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
6ccb9162 17382 break;
c906108c 17383 }
6ccb9162 17384
0114d602 17385 if (name && strcmp (name, "char") == 0)
876cecd0 17386 TYPE_NOSIGN (type) = 1;
0114d602 17387
f792889a 17388 return set_die_type (die, type, cu);
c906108c
SS
17389}
17390
80180f79
SA
17391/* Parse dwarf attribute if it's a block, reference or constant and put the
17392 resulting value of the attribute into struct bound_prop.
17393 Returns 1 if ATTR could be resolved into PROP, 0 otherwise. */
17394
17395static int
17396attr_to_dynamic_prop (const struct attribute *attr, struct die_info *die,
17397 struct dwarf2_cu *cu, struct dynamic_prop *prop)
17398{
17399 struct dwarf2_property_baton *baton;
17400 struct obstack *obstack = &cu->objfile->objfile_obstack;
17401
17402 if (attr == NULL || prop == NULL)
17403 return 0;
17404
17405 if (attr_form_is_block (attr))
17406 {
8d749320 17407 baton = XOBNEW (obstack, struct dwarf2_property_baton);
80180f79
SA
17408 baton->referenced_type = NULL;
17409 baton->locexpr.per_cu = cu->per_cu;
17410 baton->locexpr.size = DW_BLOCK (attr)->size;
17411 baton->locexpr.data = DW_BLOCK (attr)->data;
17412 prop->data.baton = baton;
17413 prop->kind = PROP_LOCEXPR;
17414 gdb_assert (prop->data.baton != NULL);
17415 }
17416 else if (attr_form_is_ref (attr))
17417 {
17418 struct dwarf2_cu *target_cu = cu;
17419 struct die_info *target_die;
17420 struct attribute *target_attr;
17421
17422 target_die = follow_die_ref (die, attr, &target_cu);
17423 target_attr = dwarf2_attr (target_die, DW_AT_location, target_cu);
df25ebbd
JB
17424 if (target_attr == NULL)
17425 target_attr = dwarf2_attr (target_die, DW_AT_data_member_location,
17426 target_cu);
80180f79
SA
17427 if (target_attr == NULL)
17428 return 0;
17429
df25ebbd 17430 switch (target_attr->name)
80180f79 17431 {
df25ebbd
JB
17432 case DW_AT_location:
17433 if (attr_form_is_section_offset (target_attr))
17434 {
8d749320 17435 baton = XOBNEW (obstack, struct dwarf2_property_baton);
df25ebbd
JB
17436 baton->referenced_type = die_type (target_die, target_cu);
17437 fill_in_loclist_baton (cu, &baton->loclist, target_attr);
17438 prop->data.baton = baton;
17439 prop->kind = PROP_LOCLIST;
17440 gdb_assert (prop->data.baton != NULL);
17441 }
17442 else if (attr_form_is_block (target_attr))
17443 {
8d749320 17444 baton = XOBNEW (obstack, struct dwarf2_property_baton);
df25ebbd
JB
17445 baton->referenced_type = die_type (target_die, target_cu);
17446 baton->locexpr.per_cu = cu->per_cu;
17447 baton->locexpr.size = DW_BLOCK (target_attr)->size;
17448 baton->locexpr.data = DW_BLOCK (target_attr)->data;
17449 prop->data.baton = baton;
17450 prop->kind = PROP_LOCEXPR;
17451 gdb_assert (prop->data.baton != NULL);
17452 }
17453 else
17454 {
17455 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
17456 "dynamic property");
17457 return 0;
17458 }
17459 break;
17460 case DW_AT_data_member_location:
17461 {
17462 LONGEST offset;
17463
17464 if (!handle_data_member_location (target_die, target_cu,
17465 &offset))
17466 return 0;
17467
8d749320 17468 baton = XOBNEW (obstack, struct dwarf2_property_baton);
6ad395a7
JB
17469 baton->referenced_type = read_type_die (target_die->parent,
17470 target_cu);
df25ebbd
JB
17471 baton->offset_info.offset = offset;
17472 baton->offset_info.type = die_type (target_die, target_cu);
17473 prop->data.baton = baton;
17474 prop->kind = PROP_ADDR_OFFSET;
17475 break;
17476 }
80180f79
SA
17477 }
17478 }
17479 else if (attr_form_is_constant (attr))
17480 {
17481 prop->data.const_val = dwarf2_get_attr_constant_value (attr, 0);
17482 prop->kind = PROP_CONST;
17483 }
17484 else
17485 {
17486 dwarf2_invalid_attrib_class_complaint (dwarf_form_name (attr->form),
17487 dwarf2_name (die, cu));
17488 return 0;
17489 }
17490
17491 return 1;
17492}
17493
a02abb62
JB
17494/* Read the given DW_AT_subrange DIE. */
17495
f792889a 17496static struct type *
a02abb62
JB
17497read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
17498{
4c9ad8c2 17499 struct type *base_type, *orig_base_type;
a02abb62
JB
17500 struct type *range_type;
17501 struct attribute *attr;
729efb13 17502 struct dynamic_prop low, high;
4fae6e18 17503 int low_default_is_valid;
c451ebe5 17504 int high_bound_is_count = 0;
15d034d0 17505 const char *name;
43bbcdc2 17506 LONGEST negative_mask;
e77813c8 17507
4c9ad8c2
TT
17508 orig_base_type = die_type (die, cu);
17509 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
17510 whereas the real type might be. So, we use ORIG_BASE_TYPE when
17511 creating the range type, but we use the result of check_typedef
17512 when examining properties of the type. */
17513 base_type = check_typedef (orig_base_type);
a02abb62 17514
7e314c57
JK
17515 /* The die_type call above may have already set the type for this DIE. */
17516 range_type = get_die_type (die, cu);
17517 if (range_type)
17518 return range_type;
17519
729efb13
SA
17520 low.kind = PROP_CONST;
17521 high.kind = PROP_CONST;
17522 high.data.const_val = 0;
17523
4fae6e18
JK
17524 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
17525 omitting DW_AT_lower_bound. */
17526 switch (cu->language)
6e70227d 17527 {
4fae6e18
JK
17528 case language_c:
17529 case language_cplus:
729efb13 17530 low.data.const_val = 0;
4fae6e18
JK
17531 low_default_is_valid = 1;
17532 break;
17533 case language_fortran:
729efb13 17534 low.data.const_val = 1;
4fae6e18
JK
17535 low_default_is_valid = 1;
17536 break;
17537 case language_d:
4fae6e18 17538 case language_objc:
c44af4eb 17539 case language_rust:
729efb13 17540 low.data.const_val = 0;
4fae6e18
JK
17541 low_default_is_valid = (cu->header.version >= 4);
17542 break;
17543 case language_ada:
17544 case language_m2:
17545 case language_pascal:
729efb13 17546 low.data.const_val = 1;
4fae6e18
JK
17547 low_default_is_valid = (cu->header.version >= 4);
17548 break;
17549 default:
729efb13 17550 low.data.const_val = 0;
4fae6e18
JK
17551 low_default_is_valid = 0;
17552 break;
a02abb62
JB
17553 }
17554
e142c38c 17555 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
a02abb62 17556 if (attr)
11c1ba78 17557 attr_to_dynamic_prop (attr, die, cu, &low);
4fae6e18
JK
17558 else if (!low_default_is_valid)
17559 complaint (&symfile_complaints, _("Missing DW_AT_lower_bound "
17560 "- DIE at 0x%x [in module %s]"),
9c541725 17561 to_underlying (die->sect_off), objfile_name (cu->objfile));
a02abb62 17562
e142c38c 17563 attr = dwarf2_attr (die, DW_AT_upper_bound, cu);
80180f79 17564 if (!attr_to_dynamic_prop (attr, die, cu, &high))
e77813c8
PM
17565 {
17566 attr = dwarf2_attr (die, DW_AT_count, cu);
c451ebe5 17567 if (attr_to_dynamic_prop (attr, die, cu, &high))
6b662e19 17568 {
c451ebe5
SA
17569 /* If bounds are constant do the final calculation here. */
17570 if (low.kind == PROP_CONST && high.kind == PROP_CONST)
17571 high.data.const_val = low.data.const_val + high.data.const_val - 1;
17572 else
17573 high_bound_is_count = 1;
c2ff108b 17574 }
e77813c8
PM
17575 }
17576
17577 /* Dwarf-2 specifications explicitly allows to create subrange types
17578 without specifying a base type.
17579 In that case, the base type must be set to the type of
17580 the lower bound, upper bound or count, in that order, if any of these
17581 three attributes references an object that has a type.
17582 If no base type is found, the Dwarf-2 specifications say that
17583 a signed integer type of size equal to the size of an address should
17584 be used.
17585 For the following C code: `extern char gdb_int [];'
17586 GCC produces an empty range DIE.
17587 FIXME: muller/2010-05-28: Possible references to object for low bound,
0963b4bd 17588 high bound or count are not yet handled by this code. */
e77813c8
PM
17589 if (TYPE_CODE (base_type) == TYPE_CODE_VOID)
17590 {
17591 struct objfile *objfile = cu->objfile;
17592 struct gdbarch *gdbarch = get_objfile_arch (objfile);
17593 int addr_size = gdbarch_addr_bit (gdbarch) /8;
17594 struct type *int_type = objfile_type (objfile)->builtin_int;
17595
17596 /* Test "int", "long int", and "long long int" objfile types,
17597 and select the first one having a size above or equal to the
17598 architecture address size. */
17599 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
17600 base_type = int_type;
17601 else
17602 {
17603 int_type = objfile_type (objfile)->builtin_long;
17604 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
17605 base_type = int_type;
17606 else
17607 {
17608 int_type = objfile_type (objfile)->builtin_long_long;
17609 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
17610 base_type = int_type;
17611 }
17612 }
17613 }
a02abb62 17614
dbb9c2b1
JB
17615 /* Normally, the DWARF producers are expected to use a signed
17616 constant form (Eg. DW_FORM_sdata) to express negative bounds.
17617 But this is unfortunately not always the case, as witnessed
17618 with GCC, for instance, where the ambiguous DW_FORM_dataN form
17619 is used instead. To work around that ambiguity, we treat
17620 the bounds as signed, and thus sign-extend their values, when
17621 the base type is signed. */
6e70227d 17622 negative_mask =
66c6502d 17623 -((LONGEST) 1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1));
729efb13
SA
17624 if (low.kind == PROP_CONST
17625 && !TYPE_UNSIGNED (base_type) && (low.data.const_val & negative_mask))
17626 low.data.const_val |= negative_mask;
17627 if (high.kind == PROP_CONST
17628 && !TYPE_UNSIGNED (base_type) && (high.data.const_val & negative_mask))
17629 high.data.const_val |= negative_mask;
43bbcdc2 17630
729efb13 17631 range_type = create_range_type (NULL, orig_base_type, &low, &high);
a02abb62 17632
c451ebe5
SA
17633 if (high_bound_is_count)
17634 TYPE_RANGE_DATA (range_type)->flag_upper_bound_is_count = 1;
17635
c2ff108b
JK
17636 /* Ada expects an empty array on no boundary attributes. */
17637 if (attr == NULL && cu->language != language_ada)
729efb13 17638 TYPE_HIGH_BOUND_KIND (range_type) = PROP_UNDEFINED;
c2ff108b 17639
39cbfefa
DJ
17640 name = dwarf2_name (die, cu);
17641 if (name)
17642 TYPE_NAME (range_type) = name;
6e70227d 17643
e142c38c 17644 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
a02abb62
JB
17645 if (attr)
17646 TYPE_LENGTH (range_type) = DW_UNSND (attr);
17647
7e314c57
JK
17648 set_die_type (die, range_type, cu);
17649
17650 /* set_die_type should be already done. */
b4ba55a1
JB
17651 set_descriptive_type (range_type, die, cu);
17652
7e314c57 17653 return range_type;
a02abb62 17654}
6e70227d 17655
f792889a 17656static struct type *
81a17f79
JB
17657read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
17658{
17659 struct type *type;
81a17f79 17660
81a17f79
JB
17661 /* For now, we only support the C meaning of an unspecified type: void. */
17662
19f392bc 17663 type = init_type (cu->objfile, TYPE_CODE_VOID, 0, NULL);
0114d602 17664 TYPE_NAME (type) = dwarf2_name (die, cu);
81a17f79 17665
f792889a 17666 return set_die_type (die, type, cu);
81a17f79 17667}
a02abb62 17668
639d11d3
DC
17669/* Read a single die and all its descendents. Set the die's sibling
17670 field to NULL; set other fields in the die correctly, and set all
17671 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
17672 location of the info_ptr after reading all of those dies. PARENT
17673 is the parent of the die in question. */
17674
17675static struct die_info *
dee91e82 17676read_die_and_children (const struct die_reader_specs *reader,
d521ce57
TT
17677 const gdb_byte *info_ptr,
17678 const gdb_byte **new_info_ptr,
dee91e82 17679 struct die_info *parent)
639d11d3
DC
17680{
17681 struct die_info *die;
d521ce57 17682 const gdb_byte *cur_ptr;
639d11d3
DC
17683 int has_children;
17684
bf6af496 17685 cur_ptr = read_full_die_1 (reader, &die, info_ptr, &has_children, 0);
1d325ec1
DJ
17686 if (die == NULL)
17687 {
17688 *new_info_ptr = cur_ptr;
17689 return NULL;
17690 }
93311388 17691 store_in_ref_table (die, reader->cu);
639d11d3
DC
17692
17693 if (has_children)
bf6af496 17694 die->child = read_die_and_siblings_1 (reader, cur_ptr, new_info_ptr, die);
639d11d3
DC
17695 else
17696 {
17697 die->child = NULL;
17698 *new_info_ptr = cur_ptr;
17699 }
17700
17701 die->sibling = NULL;
17702 die->parent = parent;
17703 return die;
17704}
17705
17706/* Read a die, all of its descendents, and all of its siblings; set
17707 all of the fields of all of the dies correctly. Arguments are as
17708 in read_die_and_children. */
17709
17710static struct die_info *
bf6af496 17711read_die_and_siblings_1 (const struct die_reader_specs *reader,
d521ce57
TT
17712 const gdb_byte *info_ptr,
17713 const gdb_byte **new_info_ptr,
bf6af496 17714 struct die_info *parent)
639d11d3
DC
17715{
17716 struct die_info *first_die, *last_sibling;
d521ce57 17717 const gdb_byte *cur_ptr;
639d11d3 17718
c906108c 17719 cur_ptr = info_ptr;
639d11d3
DC
17720 first_die = last_sibling = NULL;
17721
17722 while (1)
c906108c 17723 {
639d11d3 17724 struct die_info *die
dee91e82 17725 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
639d11d3 17726
1d325ec1 17727 if (die == NULL)
c906108c 17728 {
639d11d3
DC
17729 *new_info_ptr = cur_ptr;
17730 return first_die;
c906108c 17731 }
1d325ec1
DJ
17732
17733 if (!first_die)
17734 first_die = die;
c906108c 17735 else
1d325ec1
DJ
17736 last_sibling->sibling = die;
17737
17738 last_sibling = die;
c906108c 17739 }
c906108c
SS
17740}
17741
bf6af496
DE
17742/* Read a die, all of its descendents, and all of its siblings; set
17743 all of the fields of all of the dies correctly. Arguments are as
17744 in read_die_and_children.
17745 This the main entry point for reading a DIE and all its children. */
17746
17747static struct die_info *
17748read_die_and_siblings (const struct die_reader_specs *reader,
d521ce57
TT
17749 const gdb_byte *info_ptr,
17750 const gdb_byte **new_info_ptr,
bf6af496
DE
17751 struct die_info *parent)
17752{
17753 struct die_info *die = read_die_and_siblings_1 (reader, info_ptr,
17754 new_info_ptr, parent);
17755
b4f54984 17756 if (dwarf_die_debug)
bf6af496
DE
17757 {
17758 fprintf_unfiltered (gdb_stdlog,
17759 "Read die from %s@0x%x of %s:\n",
a32a8923 17760 get_section_name (reader->die_section),
bf6af496
DE
17761 (unsigned) (info_ptr - reader->die_section->buffer),
17762 bfd_get_filename (reader->abfd));
b4f54984 17763 dump_die (die, dwarf_die_debug);
bf6af496
DE
17764 }
17765
17766 return die;
17767}
17768
3019eac3
DE
17769/* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
17770 attributes.
17771 The caller is responsible for filling in the extra attributes
17772 and updating (*DIEP)->num_attrs.
17773 Set DIEP to point to a newly allocated die with its information,
17774 except for its child, sibling, and parent fields.
17775 Set HAS_CHILDREN to tell whether the die has children or not. */
93311388 17776
d521ce57 17777static const gdb_byte *
3019eac3 17778read_full_die_1 (const struct die_reader_specs *reader,
d521ce57 17779 struct die_info **diep, const gdb_byte *info_ptr,
3019eac3 17780 int *has_children, int num_extra_attrs)
93311388 17781{
b64f50a1 17782 unsigned int abbrev_number, bytes_read, i;
93311388
DE
17783 struct abbrev_info *abbrev;
17784 struct die_info *die;
17785 struct dwarf2_cu *cu = reader->cu;
17786 bfd *abfd = reader->abfd;
17787
9c541725 17788 sect_offset sect_off = (sect_offset) (info_ptr - reader->buffer);
93311388
DE
17789 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
17790 info_ptr += bytes_read;
17791 if (!abbrev_number)
17792 {
17793 *diep = NULL;
17794 *has_children = 0;
17795 return info_ptr;
17796 }
17797
433df2d4 17798 abbrev = abbrev_table_lookup_abbrev (cu->abbrev_table, abbrev_number);
93311388 17799 if (!abbrev)
348e048f
DE
17800 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
17801 abbrev_number,
17802 bfd_get_filename (abfd));
17803
3019eac3 17804 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
9c541725 17805 die->sect_off = sect_off;
93311388
DE
17806 die->tag = abbrev->tag;
17807 die->abbrev = abbrev_number;
17808
3019eac3
DE
17809 /* Make the result usable.
17810 The caller needs to update num_attrs after adding the extra
17811 attributes. */
93311388
DE
17812 die->num_attrs = abbrev->num_attrs;
17813
17814 for (i = 0; i < abbrev->num_attrs; ++i)
dee91e82
DE
17815 info_ptr = read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
17816 info_ptr);
93311388
DE
17817
17818 *diep = die;
17819 *has_children = abbrev->has_children;
17820 return info_ptr;
17821}
17822
3019eac3
DE
17823/* Read a die and all its attributes.
17824 Set DIEP to point to a newly allocated die with its information,
17825 except for its child, sibling, and parent fields.
17826 Set HAS_CHILDREN to tell whether the die has children or not. */
17827
d521ce57 17828static const gdb_byte *
3019eac3 17829read_full_die (const struct die_reader_specs *reader,
d521ce57 17830 struct die_info **diep, const gdb_byte *info_ptr,
3019eac3
DE
17831 int *has_children)
17832{
d521ce57 17833 const gdb_byte *result;
bf6af496
DE
17834
17835 result = read_full_die_1 (reader, diep, info_ptr, has_children, 0);
17836
b4f54984 17837 if (dwarf_die_debug)
bf6af496
DE
17838 {
17839 fprintf_unfiltered (gdb_stdlog,
17840 "Read die from %s@0x%x of %s:\n",
a32a8923 17841 get_section_name (reader->die_section),
bf6af496
DE
17842 (unsigned) (info_ptr - reader->die_section->buffer),
17843 bfd_get_filename (reader->abfd));
b4f54984 17844 dump_die (*diep, dwarf_die_debug);
bf6af496
DE
17845 }
17846
17847 return result;
3019eac3 17848}
433df2d4
DE
17849\f
17850/* Abbreviation tables.
3019eac3 17851
433df2d4 17852 In DWARF version 2, the description of the debugging information is
c906108c
SS
17853 stored in a separate .debug_abbrev section. Before we read any
17854 dies from a section we read in all abbreviations and install them
433df2d4
DE
17855 in a hash table. */
17856
17857/* Allocate space for a struct abbrev_info object in ABBREV_TABLE. */
17858
17859static struct abbrev_info *
17860abbrev_table_alloc_abbrev (struct abbrev_table *abbrev_table)
17861{
17862 struct abbrev_info *abbrev;
17863
8d749320 17864 abbrev = XOBNEW (&abbrev_table->abbrev_obstack, struct abbrev_info);
433df2d4 17865 memset (abbrev, 0, sizeof (struct abbrev_info));
8d749320 17866
433df2d4
DE
17867 return abbrev;
17868}
17869
17870/* Add an abbreviation to the table. */
c906108c
SS
17871
17872static void
433df2d4
DE
17873abbrev_table_add_abbrev (struct abbrev_table *abbrev_table,
17874 unsigned int abbrev_number,
17875 struct abbrev_info *abbrev)
17876{
17877 unsigned int hash_number;
17878
17879 hash_number = abbrev_number % ABBREV_HASH_SIZE;
17880 abbrev->next = abbrev_table->abbrevs[hash_number];
17881 abbrev_table->abbrevs[hash_number] = abbrev;
17882}
dee91e82 17883
433df2d4
DE
17884/* Look up an abbrev in the table.
17885 Returns NULL if the abbrev is not found. */
17886
17887static struct abbrev_info *
17888abbrev_table_lookup_abbrev (const struct abbrev_table *abbrev_table,
17889 unsigned int abbrev_number)
c906108c 17890{
433df2d4
DE
17891 unsigned int hash_number;
17892 struct abbrev_info *abbrev;
17893
17894 hash_number = abbrev_number % ABBREV_HASH_SIZE;
17895 abbrev = abbrev_table->abbrevs[hash_number];
17896
17897 while (abbrev)
17898 {
17899 if (abbrev->number == abbrev_number)
17900 return abbrev;
17901 abbrev = abbrev->next;
17902 }
17903 return NULL;
17904}
17905
17906/* Read in an abbrev table. */
17907
17908static struct abbrev_table *
17909abbrev_table_read_table (struct dwarf2_section_info *section,
9c541725 17910 sect_offset sect_off)
433df2d4
DE
17911{
17912 struct objfile *objfile = dwarf2_per_objfile->objfile;
a32a8923 17913 bfd *abfd = get_section_bfd_owner (section);
433df2d4 17914 struct abbrev_table *abbrev_table;
d521ce57 17915 const gdb_byte *abbrev_ptr;
c906108c
SS
17916 struct abbrev_info *cur_abbrev;
17917 unsigned int abbrev_number, bytes_read, abbrev_name;
433df2d4 17918 unsigned int abbrev_form;
f3dd6933
DJ
17919 struct attr_abbrev *cur_attrs;
17920 unsigned int allocated_attrs;
c906108c 17921
70ba0933 17922 abbrev_table = XNEW (struct abbrev_table);
9c541725 17923 abbrev_table->sect_off = sect_off;
433df2d4 17924 obstack_init (&abbrev_table->abbrev_obstack);
8d749320
SM
17925 abbrev_table->abbrevs =
17926 XOBNEWVEC (&abbrev_table->abbrev_obstack, struct abbrev_info *,
17927 ABBREV_HASH_SIZE);
433df2d4
DE
17928 memset (abbrev_table->abbrevs, 0,
17929 ABBREV_HASH_SIZE * sizeof (struct abbrev_info *));
c906108c 17930
433df2d4 17931 dwarf2_read_section (objfile, section);
9c541725 17932 abbrev_ptr = section->buffer + to_underlying (sect_off);
c906108c
SS
17933 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
17934 abbrev_ptr += bytes_read;
17935
f3dd6933 17936 allocated_attrs = ATTR_ALLOC_CHUNK;
8d749320 17937 cur_attrs = XNEWVEC (struct attr_abbrev, allocated_attrs);
6e70227d 17938
0963b4bd 17939 /* Loop until we reach an abbrev number of 0. */
c906108c
SS
17940 while (abbrev_number)
17941 {
433df2d4 17942 cur_abbrev = abbrev_table_alloc_abbrev (abbrev_table);
c906108c
SS
17943
17944 /* read in abbrev header */
17945 cur_abbrev->number = abbrev_number;
aead7601
SM
17946 cur_abbrev->tag
17947 = (enum dwarf_tag) read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
c906108c
SS
17948 abbrev_ptr += bytes_read;
17949 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
17950 abbrev_ptr += 1;
17951
17952 /* now read in declarations */
22d2f3ab 17953 for (;;)
c906108c 17954 {
43988095
JK
17955 LONGEST implicit_const;
17956
22d2f3ab
JK
17957 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
17958 abbrev_ptr += bytes_read;
17959 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
17960 abbrev_ptr += bytes_read;
43988095
JK
17961 if (abbrev_form == DW_FORM_implicit_const)
17962 {
17963 implicit_const = read_signed_leb128 (abfd, abbrev_ptr,
17964 &bytes_read);
17965 abbrev_ptr += bytes_read;
17966 }
17967 else
17968 {
17969 /* Initialize it due to a false compiler warning. */
17970 implicit_const = -1;
17971 }
22d2f3ab
JK
17972
17973 if (abbrev_name == 0)
17974 break;
17975
f3dd6933 17976 if (cur_abbrev->num_attrs == allocated_attrs)
c906108c 17977 {
f3dd6933
DJ
17978 allocated_attrs += ATTR_ALLOC_CHUNK;
17979 cur_attrs
224c3ddb 17980 = XRESIZEVEC (struct attr_abbrev, cur_attrs, allocated_attrs);
c906108c 17981 }
ae038cb0 17982
aead7601
SM
17983 cur_attrs[cur_abbrev->num_attrs].name
17984 = (enum dwarf_attribute) abbrev_name;
22d2f3ab 17985 cur_attrs[cur_abbrev->num_attrs].form
aead7601 17986 = (enum dwarf_form) abbrev_form;
43988095 17987 cur_attrs[cur_abbrev->num_attrs].implicit_const = implicit_const;
22d2f3ab 17988 ++cur_abbrev->num_attrs;
c906108c
SS
17989 }
17990
8d749320
SM
17991 cur_abbrev->attrs =
17992 XOBNEWVEC (&abbrev_table->abbrev_obstack, struct attr_abbrev,
17993 cur_abbrev->num_attrs);
f3dd6933
DJ
17994 memcpy (cur_abbrev->attrs, cur_attrs,
17995 cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
17996
433df2d4 17997 abbrev_table_add_abbrev (abbrev_table, abbrev_number, cur_abbrev);
c906108c
SS
17998
17999 /* Get next abbreviation.
18000 Under Irix6 the abbreviations for a compilation unit are not
c5aa993b
JM
18001 always properly terminated with an abbrev number of 0.
18002 Exit loop if we encounter an abbreviation which we have
18003 already read (which means we are about to read the abbreviations
18004 for the next compile unit) or if the end of the abbreviation
18005 table is reached. */
433df2d4 18006 if ((unsigned int) (abbrev_ptr - section->buffer) >= section->size)
c906108c
SS
18007 break;
18008 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18009 abbrev_ptr += bytes_read;
433df2d4 18010 if (abbrev_table_lookup_abbrev (abbrev_table, abbrev_number) != NULL)
c906108c
SS
18011 break;
18012 }
f3dd6933
DJ
18013
18014 xfree (cur_attrs);
433df2d4 18015 return abbrev_table;
c906108c
SS
18016}
18017
433df2d4 18018/* Free the resources held by ABBREV_TABLE. */
c906108c 18019
c906108c 18020static void
433df2d4 18021abbrev_table_free (struct abbrev_table *abbrev_table)
c906108c 18022{
433df2d4
DE
18023 obstack_free (&abbrev_table->abbrev_obstack, NULL);
18024 xfree (abbrev_table);
c906108c
SS
18025}
18026
f4dc4d17
DE
18027/* Same as abbrev_table_free but as a cleanup.
18028 We pass in a pointer to the pointer to the table so that we can
18029 set the pointer to NULL when we're done. It also simplifies
73051182 18030 build_type_psymtabs_1. */
f4dc4d17
DE
18031
18032static void
18033abbrev_table_free_cleanup (void *table_ptr)
18034{
9a3c8263 18035 struct abbrev_table **abbrev_table_ptr = (struct abbrev_table **) table_ptr;
f4dc4d17
DE
18036
18037 if (*abbrev_table_ptr != NULL)
18038 abbrev_table_free (*abbrev_table_ptr);
18039 *abbrev_table_ptr = NULL;
18040}
18041
433df2d4
DE
18042/* Read the abbrev table for CU from ABBREV_SECTION. */
18043
18044static void
18045dwarf2_read_abbrevs (struct dwarf2_cu *cu,
18046 struct dwarf2_section_info *abbrev_section)
c906108c 18047{
433df2d4 18048 cu->abbrev_table =
9c541725 18049 abbrev_table_read_table (abbrev_section, cu->header.abbrev_sect_off);
433df2d4 18050}
c906108c 18051
433df2d4 18052/* Release the memory used by the abbrev table for a compilation unit. */
c906108c 18053
433df2d4
DE
18054static void
18055dwarf2_free_abbrev_table (void *ptr_to_cu)
18056{
9a3c8263 18057 struct dwarf2_cu *cu = (struct dwarf2_cu *) ptr_to_cu;
c906108c 18058
a2ce51a0
DE
18059 if (cu->abbrev_table != NULL)
18060 abbrev_table_free (cu->abbrev_table);
433df2d4
DE
18061 /* Set this to NULL so that we SEGV if we try to read it later,
18062 and also because free_comp_unit verifies this is NULL. */
18063 cu->abbrev_table = NULL;
18064}
18065\f
72bf9492
DJ
18066/* Returns nonzero if TAG represents a type that we might generate a partial
18067 symbol for. */
18068
18069static int
18070is_type_tag_for_partial (int tag)
18071{
18072 switch (tag)
18073 {
18074#if 0
18075 /* Some types that would be reasonable to generate partial symbols for,
18076 that we don't at present. */
18077 case DW_TAG_array_type:
18078 case DW_TAG_file_type:
18079 case DW_TAG_ptr_to_member_type:
18080 case DW_TAG_set_type:
18081 case DW_TAG_string_type:
18082 case DW_TAG_subroutine_type:
18083#endif
18084 case DW_TAG_base_type:
18085 case DW_TAG_class_type:
680b30c7 18086 case DW_TAG_interface_type:
72bf9492
DJ
18087 case DW_TAG_enumeration_type:
18088 case DW_TAG_structure_type:
18089 case DW_TAG_subrange_type:
18090 case DW_TAG_typedef:
18091 case DW_TAG_union_type:
18092 return 1;
18093 default:
18094 return 0;
18095 }
18096}
18097
18098/* Load all DIEs that are interesting for partial symbols into memory. */
18099
18100static struct partial_die_info *
dee91e82 18101load_partial_dies (const struct die_reader_specs *reader,
d521ce57 18102 const gdb_byte *info_ptr, int building_psymtab)
72bf9492 18103{
dee91e82 18104 struct dwarf2_cu *cu = reader->cu;
bb5ed363 18105 struct objfile *objfile = cu->objfile;
72bf9492
DJ
18106 struct partial_die_info *part_die;
18107 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
18108 struct abbrev_info *abbrev;
18109 unsigned int bytes_read;
5afb4e99 18110 unsigned int load_all = 0;
72bf9492
DJ
18111 int nesting_level = 1;
18112
18113 parent_die = NULL;
18114 last_die = NULL;
18115
7adf1e79
DE
18116 gdb_assert (cu->per_cu != NULL);
18117 if (cu->per_cu->load_all_dies)
5afb4e99
DJ
18118 load_all = 1;
18119
72bf9492
DJ
18120 cu->partial_dies
18121 = htab_create_alloc_ex (cu->header.length / 12,
18122 partial_die_hash,
18123 partial_die_eq,
18124 NULL,
18125 &cu->comp_unit_obstack,
18126 hashtab_obstack_allocate,
18127 dummy_obstack_deallocate);
18128
8d749320 18129 part_die = XOBNEW (&cu->comp_unit_obstack, struct partial_die_info);
72bf9492
DJ
18130
18131 while (1)
18132 {
18133 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
18134
18135 /* A NULL abbrev means the end of a series of children. */
18136 if (abbrev == NULL)
18137 {
18138 if (--nesting_level == 0)
18139 {
18140 /* PART_DIE was probably the last thing allocated on the
18141 comp_unit_obstack, so we could call obstack_free
18142 here. We don't do that because the waste is small,
18143 and will be cleaned up when we're done with this
18144 compilation unit. This way, we're also more robust
18145 against other users of the comp_unit_obstack. */
18146 return first_die;
18147 }
18148 info_ptr += bytes_read;
18149 last_die = parent_die;
18150 parent_die = parent_die->die_parent;
18151 continue;
18152 }
18153
98bfdba5
PA
18154 /* Check for template arguments. We never save these; if
18155 they're seen, we just mark the parent, and go on our way. */
18156 if (parent_die != NULL
18157 && cu->language == language_cplus
18158 && (abbrev->tag == DW_TAG_template_type_param
18159 || abbrev->tag == DW_TAG_template_value_param))
18160 {
18161 parent_die->has_template_arguments = 1;
18162
18163 if (!load_all)
18164 {
18165 /* We don't need a partial DIE for the template argument. */
dee91e82 18166 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
98bfdba5
PA
18167 continue;
18168 }
18169 }
18170
0d99eb77 18171 /* We only recurse into c++ subprograms looking for template arguments.
98bfdba5
PA
18172 Skip their other children. */
18173 if (!load_all
18174 && cu->language == language_cplus
18175 && parent_die != NULL
18176 && parent_die->tag == DW_TAG_subprogram)
18177 {
dee91e82 18178 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
98bfdba5
PA
18179 continue;
18180 }
18181
5afb4e99
DJ
18182 /* Check whether this DIE is interesting enough to save. Normally
18183 we would not be interested in members here, but there may be
18184 later variables referencing them via DW_AT_specification (for
18185 static members). */
18186 if (!load_all
18187 && !is_type_tag_for_partial (abbrev->tag)
72929c62 18188 && abbrev->tag != DW_TAG_constant
72bf9492
DJ
18189 && abbrev->tag != DW_TAG_enumerator
18190 && abbrev->tag != DW_TAG_subprogram
bc30ff58 18191 && abbrev->tag != DW_TAG_lexical_block
72bf9492 18192 && abbrev->tag != DW_TAG_variable
5afb4e99 18193 && abbrev->tag != DW_TAG_namespace
f55ee35c 18194 && abbrev->tag != DW_TAG_module
95554aad 18195 && abbrev->tag != DW_TAG_member
74921315
KS
18196 && abbrev->tag != DW_TAG_imported_unit
18197 && abbrev->tag != DW_TAG_imported_declaration)
72bf9492
DJ
18198 {
18199 /* Otherwise we skip to the next sibling, if any. */
dee91e82 18200 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
72bf9492
DJ
18201 continue;
18202 }
18203
dee91e82
DE
18204 info_ptr = read_partial_die (reader, part_die, abbrev, bytes_read,
18205 info_ptr);
72bf9492
DJ
18206
18207 /* This two-pass algorithm for processing partial symbols has a
18208 high cost in cache pressure. Thus, handle some simple cases
18209 here which cover the majority of C partial symbols. DIEs
18210 which neither have specification tags in them, nor could have
18211 specification tags elsewhere pointing at them, can simply be
18212 processed and discarded.
18213
18214 This segment is also optional; scan_partial_symbols and
18215 add_partial_symbol will handle these DIEs if we chain
18216 them in normally. When compilers which do not emit large
18217 quantities of duplicate debug information are more common,
18218 this code can probably be removed. */
18219
18220 /* Any complete simple types at the top level (pretty much all
18221 of them, for a language without namespaces), can be processed
18222 directly. */
18223 if (parent_die == NULL
18224 && part_die->has_specification == 0
18225 && part_die->is_declaration == 0
d8228535 18226 && ((part_die->tag == DW_TAG_typedef && !part_die->has_children)
72bf9492
DJ
18227 || part_die->tag == DW_TAG_base_type
18228 || part_die->tag == DW_TAG_subrange_type))
18229 {
18230 if (building_psymtab && part_die->name != NULL)
04a679b8 18231 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
72bf9492 18232 VAR_DOMAIN, LOC_TYPEDEF,
bb5ed363 18233 &objfile->static_psymbols,
1762568f 18234 0, cu->language, objfile);
dee91e82 18235 info_ptr = locate_pdi_sibling (reader, part_die, info_ptr);
72bf9492
DJ
18236 continue;
18237 }
18238
d8228535
JK
18239 /* The exception for DW_TAG_typedef with has_children above is
18240 a workaround of GCC PR debug/47510. In the case of this complaint
18241 type_name_no_tag_or_error will error on such types later.
18242
18243 GDB skipped children of DW_TAG_typedef by the shortcut above and then
18244 it could not find the child DIEs referenced later, this is checked
18245 above. In correct DWARF DW_TAG_typedef should have no children. */
18246
18247 if (part_die->tag == DW_TAG_typedef && part_die->has_children)
18248 complaint (&symfile_complaints,
18249 _("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
18250 "- DIE at 0x%x [in module %s]"),
9c541725 18251 to_underlying (part_die->sect_off), objfile_name (objfile));
d8228535 18252
72bf9492
DJ
18253 /* If we're at the second level, and we're an enumerator, and
18254 our parent has no specification (meaning possibly lives in a
18255 namespace elsewhere), then we can add the partial symbol now
18256 instead of queueing it. */
18257 if (part_die->tag == DW_TAG_enumerator
18258 && parent_die != NULL
18259 && parent_die->die_parent == NULL
18260 && parent_die->tag == DW_TAG_enumeration_type
18261 && parent_die->has_specification == 0)
18262 {
18263 if (part_die->name == NULL)
3e43a32a
MS
18264 complaint (&symfile_complaints,
18265 _("malformed enumerator DIE ignored"));
72bf9492 18266 else if (building_psymtab)
04a679b8 18267 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
72bf9492 18268 VAR_DOMAIN, LOC_CONST,
9c37b5ae 18269 cu->language == language_cplus
bb5ed363
DE
18270 ? &objfile->global_psymbols
18271 : &objfile->static_psymbols,
1762568f 18272 0, cu->language, objfile);
72bf9492 18273
dee91e82 18274 info_ptr = locate_pdi_sibling (reader, part_die, info_ptr);
72bf9492
DJ
18275 continue;
18276 }
18277
18278 /* We'll save this DIE so link it in. */
18279 part_die->die_parent = parent_die;
18280 part_die->die_sibling = NULL;
18281 part_die->die_child = NULL;
18282
18283 if (last_die && last_die == parent_die)
18284 last_die->die_child = part_die;
18285 else if (last_die)
18286 last_die->die_sibling = part_die;
18287
18288 last_die = part_die;
18289
18290 if (first_die == NULL)
18291 first_die = part_die;
18292
18293 /* Maybe add the DIE to the hash table. Not all DIEs that we
18294 find interesting need to be in the hash table, because we
18295 also have the parent/sibling/child chains; only those that we
18296 might refer to by offset later during partial symbol reading.
18297
18298 For now this means things that might have be the target of a
18299 DW_AT_specification, DW_AT_abstract_origin, or
18300 DW_AT_extension. DW_AT_extension will refer only to
18301 namespaces; DW_AT_abstract_origin refers to functions (and
18302 many things under the function DIE, but we do not recurse
18303 into function DIEs during partial symbol reading) and
18304 possibly variables as well; DW_AT_specification refers to
18305 declarations. Declarations ought to have the DW_AT_declaration
18306 flag. It happens that GCC forgets to put it in sometimes, but
18307 only for functions, not for types.
18308
18309 Adding more things than necessary to the hash table is harmless
18310 except for the performance cost. Adding too few will result in
5afb4e99
DJ
18311 wasted time in find_partial_die, when we reread the compilation
18312 unit with load_all_dies set. */
72bf9492 18313
5afb4e99 18314 if (load_all
72929c62 18315 || abbrev->tag == DW_TAG_constant
5afb4e99 18316 || abbrev->tag == DW_TAG_subprogram
72bf9492
DJ
18317 || abbrev->tag == DW_TAG_variable
18318 || abbrev->tag == DW_TAG_namespace
18319 || part_die->is_declaration)
18320 {
18321 void **slot;
18322
18323 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
9c541725
PA
18324 to_underlying (part_die->sect_off),
18325 INSERT);
72bf9492
DJ
18326 *slot = part_die;
18327 }
18328
8d749320 18329 part_die = XOBNEW (&cu->comp_unit_obstack, struct partial_die_info);
72bf9492
DJ
18330
18331 /* For some DIEs we want to follow their children (if any). For C
bc30ff58 18332 we have no reason to follow the children of structures; for other
98bfdba5
PA
18333 languages we have to, so that we can get at method physnames
18334 to infer fully qualified class names, for DW_AT_specification,
18335 and for C++ template arguments. For C++, we also look one level
18336 inside functions to find template arguments (if the name of the
18337 function does not already contain the template arguments).
bc30ff58
JB
18338
18339 For Ada, we need to scan the children of subprograms and lexical
18340 blocks as well because Ada allows the definition of nested
18341 entities that could be interesting for the debugger, such as
18342 nested subprograms for instance. */
72bf9492 18343 if (last_die->has_children
5afb4e99
DJ
18344 && (load_all
18345 || last_die->tag == DW_TAG_namespace
f55ee35c 18346 || last_die->tag == DW_TAG_module
72bf9492 18347 || last_die->tag == DW_TAG_enumeration_type
98bfdba5
PA
18348 || (cu->language == language_cplus
18349 && last_die->tag == DW_TAG_subprogram
18350 && (last_die->name == NULL
18351 || strchr (last_die->name, '<') == NULL))
72bf9492
DJ
18352 || (cu->language != language_c
18353 && (last_die->tag == DW_TAG_class_type
680b30c7 18354 || last_die->tag == DW_TAG_interface_type
72bf9492 18355 || last_die->tag == DW_TAG_structure_type
bc30ff58
JB
18356 || last_die->tag == DW_TAG_union_type))
18357 || (cu->language == language_ada
18358 && (last_die->tag == DW_TAG_subprogram
18359 || last_die->tag == DW_TAG_lexical_block))))
72bf9492
DJ
18360 {
18361 nesting_level++;
18362 parent_die = last_die;
18363 continue;
18364 }
18365
18366 /* Otherwise we skip to the next sibling, if any. */
dee91e82 18367 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
72bf9492
DJ
18368
18369 /* Back to the top, do it again. */
18370 }
18371}
18372
c906108c
SS
18373/* Read a minimal amount of information into the minimal die structure. */
18374
d521ce57 18375static const gdb_byte *
dee91e82
DE
18376read_partial_die (const struct die_reader_specs *reader,
18377 struct partial_die_info *part_die,
18378 struct abbrev_info *abbrev, unsigned int abbrev_len,
d521ce57 18379 const gdb_byte *info_ptr)
c906108c 18380{
dee91e82 18381 struct dwarf2_cu *cu = reader->cu;
bb5ed363 18382 struct objfile *objfile = cu->objfile;
d521ce57 18383 const gdb_byte *buffer = reader->buffer;
fa238c03 18384 unsigned int i;
c906108c 18385 struct attribute attr;
c5aa993b 18386 int has_low_pc_attr = 0;
c906108c 18387 int has_high_pc_attr = 0;
91da1414 18388 int high_pc_relative = 0;
c906108c 18389
72bf9492 18390 memset (part_die, 0, sizeof (struct partial_die_info));
c906108c 18391
9c541725 18392 part_die->sect_off = (sect_offset) (info_ptr - buffer);
72bf9492
DJ
18393
18394 info_ptr += abbrev_len;
18395
18396 if (abbrev == NULL)
18397 return info_ptr;
18398
c906108c
SS
18399 part_die->tag = abbrev->tag;
18400 part_die->has_children = abbrev->has_children;
c906108c
SS
18401
18402 for (i = 0; i < abbrev->num_attrs; ++i)
18403 {
dee91e82 18404 info_ptr = read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
c906108c
SS
18405
18406 /* Store the data if it is of an attribute we want to keep in a
c5aa993b 18407 partial symbol table. */
c906108c
SS
18408 switch (attr.name)
18409 {
18410 case DW_AT_name:
71c25dea
TT
18411 switch (part_die->tag)
18412 {
18413 case DW_TAG_compile_unit:
95554aad 18414 case DW_TAG_partial_unit:
348e048f 18415 case DW_TAG_type_unit:
71c25dea
TT
18416 /* Compilation units have a DW_AT_name that is a filename, not
18417 a source language identifier. */
18418 case DW_TAG_enumeration_type:
18419 case DW_TAG_enumerator:
18420 /* These tags always have simple identifiers already; no need
18421 to canonicalize them. */
18422 part_die->name = DW_STRING (&attr);
18423 break;
18424 default:
18425 part_die->name
18426 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
34a68019 18427 &objfile->per_bfd->storage_obstack);
71c25dea
TT
18428 break;
18429 }
c906108c 18430 break;
31ef98ae 18431 case DW_AT_linkage_name:
c906108c 18432 case DW_AT_MIPS_linkage_name:
31ef98ae
TT
18433 /* Note that both forms of linkage name might appear. We
18434 assume they will be the same, and we only store the last
18435 one we see. */
94af9270
KS
18436 if (cu->language == language_ada)
18437 part_die->name = DW_STRING (&attr);
abc72ce4 18438 part_die->linkage_name = DW_STRING (&attr);
c906108c
SS
18439 break;
18440 case DW_AT_low_pc:
18441 has_low_pc_attr = 1;
31aa7e4e 18442 part_die->lowpc = attr_value_as_address (&attr);
c906108c
SS
18443 break;
18444 case DW_AT_high_pc:
18445 has_high_pc_attr = 1;
31aa7e4e
JB
18446 part_die->highpc = attr_value_as_address (&attr);
18447 if (cu->header.version >= 4 && attr_form_is_constant (&attr))
18448 high_pc_relative = 1;
c906108c
SS
18449 break;
18450 case DW_AT_location:
0963b4bd 18451 /* Support the .debug_loc offsets. */
8e19ed76
PS
18452 if (attr_form_is_block (&attr))
18453 {
95554aad 18454 part_die->d.locdesc = DW_BLOCK (&attr);
8e19ed76 18455 }
3690dd37 18456 else if (attr_form_is_section_offset (&attr))
8e19ed76 18457 {
4d3c2250 18458 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
18459 }
18460 else
18461 {
4d3c2250
KB
18462 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
18463 "partial symbol information");
8e19ed76 18464 }
c906108c 18465 break;
c906108c
SS
18466 case DW_AT_external:
18467 part_die->is_external = DW_UNSND (&attr);
18468 break;
18469 case DW_AT_declaration:
18470 part_die->is_declaration = DW_UNSND (&attr);
18471 break;
18472 case DW_AT_type:
18473 part_die->has_type = 1;
18474 break;
18475 case DW_AT_abstract_origin:
18476 case DW_AT_specification:
72bf9492
DJ
18477 case DW_AT_extension:
18478 part_die->has_specification = 1;
c764a876 18479 part_die->spec_offset = dwarf2_get_ref_die_offset (&attr);
36586728
TT
18480 part_die->spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
18481 || cu->per_cu->is_dwz);
c906108c
SS
18482 break;
18483 case DW_AT_sibling:
18484 /* Ignore absolute siblings, they might point outside of
18485 the current compile unit. */
18486 if (attr.form == DW_FORM_ref_addr)
3e43a32a
MS
18487 complaint (&symfile_complaints,
18488 _("ignoring absolute DW_AT_sibling"));
c906108c 18489 else
b9502d3f 18490 {
9c541725
PA
18491 sect_offset off = dwarf2_get_ref_die_offset (&attr);
18492 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
b9502d3f
WN
18493
18494 if (sibling_ptr < info_ptr)
18495 complaint (&symfile_complaints,
18496 _("DW_AT_sibling points backwards"));
22869d73
KS
18497 else if (sibling_ptr > reader->buffer_end)
18498 dwarf2_section_buffer_overflow_complaint (reader->die_section);
b9502d3f
WN
18499 else
18500 part_die->sibling = sibling_ptr;
18501 }
c906108c 18502 break;
fa4028e9
JB
18503 case DW_AT_byte_size:
18504 part_die->has_byte_size = 1;
18505 break;
ff908ebf
AW
18506 case DW_AT_const_value:
18507 part_die->has_const_value = 1;
18508 break;
68511cec
CES
18509 case DW_AT_calling_convention:
18510 /* DWARF doesn't provide a way to identify a program's source-level
18511 entry point. DW_AT_calling_convention attributes are only meant
18512 to describe functions' calling conventions.
18513
18514 However, because it's a necessary piece of information in
0c1b455e
TT
18515 Fortran, and before DWARF 4 DW_CC_program was the only
18516 piece of debugging information whose definition refers to
18517 a 'main program' at all, several compilers marked Fortran
18518 main programs with DW_CC_program --- even when those
18519 functions use the standard calling conventions.
18520
18521 Although DWARF now specifies a way to provide this
18522 information, we support this practice for backward
18523 compatibility. */
68511cec 18524 if (DW_UNSND (&attr) == DW_CC_program
0c1b455e
TT
18525 && cu->language == language_fortran)
18526 part_die->main_subprogram = 1;
68511cec 18527 break;
481860b3
GB
18528 case DW_AT_inline:
18529 if (DW_UNSND (&attr) == DW_INL_inlined
18530 || DW_UNSND (&attr) == DW_INL_declared_inlined)
18531 part_die->may_be_inlined = 1;
18532 break;
95554aad
TT
18533
18534 case DW_AT_import:
18535 if (part_die->tag == DW_TAG_imported_unit)
36586728 18536 {
9c541725 18537 part_die->d.sect_off = dwarf2_get_ref_die_offset (&attr);
36586728
TT
18538 part_die->is_dwz = (attr.form == DW_FORM_GNU_ref_alt
18539 || cu->per_cu->is_dwz);
18540 }
95554aad
TT
18541 break;
18542
0c1b455e
TT
18543 case DW_AT_main_subprogram:
18544 part_die->main_subprogram = DW_UNSND (&attr);
18545 break;
18546
c906108c
SS
18547 default:
18548 break;
18549 }
18550 }
18551
91da1414
MW
18552 if (high_pc_relative)
18553 part_die->highpc += part_die->lowpc;
18554
9373cf26
JK
18555 if (has_low_pc_attr && has_high_pc_attr)
18556 {
18557 /* When using the GNU linker, .gnu.linkonce. sections are used to
18558 eliminate duplicate copies of functions and vtables and such.
18559 The linker will arbitrarily choose one and discard the others.
18560 The AT_*_pc values for such functions refer to local labels in
18561 these sections. If the section from that file was discarded, the
18562 labels are not in the output, so the relocs get a value of 0.
18563 If this is a discarded function, mark the pc bounds as invalid,
18564 so that GDB will ignore it. */
18565 if (part_die->lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
18566 {
bb5ed363 18567 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9373cf26
JK
18568
18569 complaint (&symfile_complaints,
18570 _("DW_AT_low_pc %s is zero "
18571 "for DIE at 0x%x [in module %s]"),
18572 paddress (gdbarch, part_die->lowpc),
9c541725 18573 to_underlying (part_die->sect_off), objfile_name (objfile));
9373cf26
JK
18574 }
18575 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
18576 else if (part_die->lowpc >= part_die->highpc)
18577 {
bb5ed363 18578 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9373cf26
JK
18579
18580 complaint (&symfile_complaints,
18581 _("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
18582 "for DIE at 0x%x [in module %s]"),
18583 paddress (gdbarch, part_die->lowpc),
18584 paddress (gdbarch, part_die->highpc),
9c541725
PA
18585 to_underlying (part_die->sect_off),
18586 objfile_name (objfile));
9373cf26
JK
18587 }
18588 else
18589 part_die->has_pc_info = 1;
18590 }
85cbf3d3 18591
c906108c
SS
18592 return info_ptr;
18593}
18594
72bf9492
DJ
18595/* Find a cached partial DIE at OFFSET in CU. */
18596
18597static struct partial_die_info *
9c541725 18598find_partial_die_in_comp_unit (sect_offset sect_off, struct dwarf2_cu *cu)
72bf9492
DJ
18599{
18600 struct partial_die_info *lookup_die = NULL;
18601 struct partial_die_info part_die;
18602
9c541725 18603 part_die.sect_off = sect_off;
9a3c8263
SM
18604 lookup_die = ((struct partial_die_info *)
18605 htab_find_with_hash (cu->partial_dies, &part_die,
9c541725 18606 to_underlying (sect_off)));
72bf9492 18607
72bf9492
DJ
18608 return lookup_die;
18609}
18610
348e048f
DE
18611/* Find a partial DIE at OFFSET, which may or may not be in CU,
18612 except in the case of .debug_types DIEs which do not reference
18613 outside their CU (they do however referencing other types via
55f1336d 18614 DW_FORM_ref_sig8). */
72bf9492
DJ
18615
18616static struct partial_die_info *
9c541725 18617find_partial_die (sect_offset sect_off, int offset_in_dwz, struct dwarf2_cu *cu)
72bf9492 18618{
bb5ed363 18619 struct objfile *objfile = cu->objfile;
5afb4e99
DJ
18620 struct dwarf2_per_cu_data *per_cu = NULL;
18621 struct partial_die_info *pd = NULL;
72bf9492 18622
36586728 18623 if (offset_in_dwz == cu->per_cu->is_dwz
9c541725 18624 && offset_in_cu_p (&cu->header, sect_off))
5afb4e99 18625 {
9c541725 18626 pd = find_partial_die_in_comp_unit (sect_off, cu);
5afb4e99
DJ
18627 if (pd != NULL)
18628 return pd;
0d99eb77
DE
18629 /* We missed recording what we needed.
18630 Load all dies and try again. */
18631 per_cu = cu->per_cu;
5afb4e99 18632 }
0d99eb77
DE
18633 else
18634 {
18635 /* TUs don't reference other CUs/TUs (except via type signatures). */
3019eac3 18636 if (cu->per_cu->is_debug_types)
0d99eb77 18637 {
9c541725
PA
18638 error (_("Dwarf Error: Type Unit at offset 0x%x contains"
18639 " external reference to offset 0x%x [in module %s].\n"),
18640 to_underlying (cu->header.sect_off), to_underlying (sect_off),
0d99eb77
DE
18641 bfd_get_filename (objfile->obfd));
18642 }
9c541725 18643 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
36586728 18644 objfile);
72bf9492 18645
0d99eb77
DE
18646 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
18647 load_partial_comp_unit (per_cu);
ae038cb0 18648
0d99eb77 18649 per_cu->cu->last_used = 0;
9c541725 18650 pd = find_partial_die_in_comp_unit (sect_off, per_cu->cu);
0d99eb77 18651 }
5afb4e99 18652
dee91e82
DE
18653 /* If we didn't find it, and not all dies have been loaded,
18654 load them all and try again. */
18655
5afb4e99
DJ
18656 if (pd == NULL && per_cu->load_all_dies == 0)
18657 {
5afb4e99 18658 per_cu->load_all_dies = 1;
fd820528
DE
18659
18660 /* This is nasty. When we reread the DIEs, somewhere up the call chain
18661 THIS_CU->cu may already be in use. So we can't just free it and
18662 replace its DIEs with the ones we read in. Instead, we leave those
18663 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
18664 and clobber THIS_CU->cu->partial_dies with the hash table for the new
18665 set. */
dee91e82 18666 load_partial_comp_unit (per_cu);
5afb4e99 18667
9c541725 18668 pd = find_partial_die_in_comp_unit (sect_off, per_cu->cu);
5afb4e99
DJ
18669 }
18670
18671 if (pd == NULL)
18672 internal_error (__FILE__, __LINE__,
3e43a32a
MS
18673 _("could not find partial DIE 0x%x "
18674 "in cache [from module %s]\n"),
9c541725 18675 to_underlying (sect_off), bfd_get_filename (objfile->obfd));
5afb4e99 18676 return pd;
72bf9492
DJ
18677}
18678
abc72ce4
DE
18679/* See if we can figure out if the class lives in a namespace. We do
18680 this by looking for a member function; its demangled name will
18681 contain namespace info, if there is any. */
18682
18683static void
18684guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
18685 struct dwarf2_cu *cu)
18686{
18687 /* NOTE: carlton/2003-10-07: Getting the info this way changes
18688 what template types look like, because the demangler
18689 frequently doesn't give the same name as the debug info. We
18690 could fix this by only using the demangled name to get the
18691 prefix (but see comment in read_structure_type). */
18692
18693 struct partial_die_info *real_pdi;
18694 struct partial_die_info *child_pdi;
18695
18696 /* If this DIE (this DIE's specification, if any) has a parent, then
18697 we should not do this. We'll prepend the parent's fully qualified
18698 name when we create the partial symbol. */
18699
18700 real_pdi = struct_pdi;
18701 while (real_pdi->has_specification)
36586728
TT
18702 real_pdi = find_partial_die (real_pdi->spec_offset,
18703 real_pdi->spec_is_dwz, cu);
abc72ce4
DE
18704
18705 if (real_pdi->die_parent != NULL)
18706 return;
18707
18708 for (child_pdi = struct_pdi->die_child;
18709 child_pdi != NULL;
18710 child_pdi = child_pdi->die_sibling)
18711 {
18712 if (child_pdi->tag == DW_TAG_subprogram
18713 && child_pdi->linkage_name != NULL)
18714 {
18715 char *actual_class_name
18716 = language_class_name_from_physname (cu->language_defn,
18717 child_pdi->linkage_name);
18718 if (actual_class_name != NULL)
18719 {
18720 struct_pdi->name
224c3ddb
SM
18721 = ((const char *)
18722 obstack_copy0 (&cu->objfile->per_bfd->storage_obstack,
18723 actual_class_name,
18724 strlen (actual_class_name)));
abc72ce4
DE
18725 xfree (actual_class_name);
18726 }
18727 break;
18728 }
18729 }
18730}
18731
72bf9492
DJ
18732/* Adjust PART_DIE before generating a symbol for it. This function
18733 may set the is_external flag or change the DIE's name. */
18734
18735static void
18736fixup_partial_die (struct partial_die_info *part_die,
18737 struct dwarf2_cu *cu)
18738{
abc72ce4
DE
18739 /* Once we've fixed up a die, there's no point in doing so again.
18740 This also avoids a memory leak if we were to call
18741 guess_partial_die_structure_name multiple times. */
18742 if (part_die->fixup_called)
18743 return;
18744
72bf9492
DJ
18745 /* If we found a reference attribute and the DIE has no name, try
18746 to find a name in the referred to DIE. */
18747
18748 if (part_die->name == NULL && part_die->has_specification)
18749 {
18750 struct partial_die_info *spec_die;
72bf9492 18751
36586728
TT
18752 spec_die = find_partial_die (part_die->spec_offset,
18753 part_die->spec_is_dwz, cu);
72bf9492 18754
10b3939b 18755 fixup_partial_die (spec_die, cu);
72bf9492
DJ
18756
18757 if (spec_die->name)
18758 {
18759 part_die->name = spec_die->name;
18760
18761 /* Copy DW_AT_external attribute if it is set. */
18762 if (spec_die->is_external)
18763 part_die->is_external = spec_die->is_external;
18764 }
18765 }
18766
18767 /* Set default names for some unnamed DIEs. */
72bf9492
DJ
18768
18769 if (part_die->name == NULL && part_die->tag == DW_TAG_namespace)
2b1dbab0 18770 part_die->name = CP_ANONYMOUS_NAMESPACE_STR;
72bf9492 18771
abc72ce4
DE
18772 /* If there is no parent die to provide a namespace, and there are
18773 children, see if we can determine the namespace from their linkage
122d1940 18774 name. */
abc72ce4 18775 if (cu->language == language_cplus
8b70b953 18776 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
abc72ce4
DE
18777 && part_die->die_parent == NULL
18778 && part_die->has_children
18779 && (part_die->tag == DW_TAG_class_type
18780 || part_die->tag == DW_TAG_structure_type
18781 || part_die->tag == DW_TAG_union_type))
18782 guess_partial_die_structure_name (part_die, cu);
18783
53832f31
TT
18784 /* GCC might emit a nameless struct or union that has a linkage
18785 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
18786 if (part_die->name == NULL
96408a79
SA
18787 && (part_die->tag == DW_TAG_class_type
18788 || part_die->tag == DW_TAG_interface_type
18789 || part_die->tag == DW_TAG_structure_type
18790 || part_die->tag == DW_TAG_union_type)
53832f31
TT
18791 && part_die->linkage_name != NULL)
18792 {
18793 char *demangled;
18794
8de20a37 18795 demangled = gdb_demangle (part_die->linkage_name, DMGL_TYPES);
53832f31
TT
18796 if (demangled)
18797 {
96408a79
SA
18798 const char *base;
18799
18800 /* Strip any leading namespaces/classes, keep only the base name.
18801 DW_AT_name for named DIEs does not contain the prefixes. */
18802 base = strrchr (demangled, ':');
18803 if (base && base > demangled && base[-1] == ':')
18804 base++;
18805 else
18806 base = demangled;
18807
34a68019 18808 part_die->name
224c3ddb
SM
18809 = ((const char *)
18810 obstack_copy0 (&cu->objfile->per_bfd->storage_obstack,
18811 base, strlen (base)));
53832f31
TT
18812 xfree (demangled);
18813 }
18814 }
18815
abc72ce4 18816 part_die->fixup_called = 1;
72bf9492
DJ
18817}
18818
a8329558 18819/* Read an attribute value described by an attribute form. */
c906108c 18820
d521ce57 18821static const gdb_byte *
dee91e82
DE
18822read_attribute_value (const struct die_reader_specs *reader,
18823 struct attribute *attr, unsigned form,
43988095 18824 LONGEST implicit_const, const gdb_byte *info_ptr)
c906108c 18825{
dee91e82 18826 struct dwarf2_cu *cu = reader->cu;
3e29f34a
MR
18827 struct objfile *objfile = cu->objfile;
18828 struct gdbarch *gdbarch = get_objfile_arch (objfile);
dee91e82 18829 bfd *abfd = reader->abfd;
e7c27a73 18830 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
18831 unsigned int bytes_read;
18832 struct dwarf_block *blk;
18833
aead7601 18834 attr->form = (enum dwarf_form) form;
a8329558 18835 switch (form)
c906108c 18836 {
c906108c 18837 case DW_FORM_ref_addr:
ae411497 18838 if (cu->header.version == 2)
4568ecf9 18839 DW_UNSND (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
ae411497 18840 else
4568ecf9
DE
18841 DW_UNSND (attr) = read_offset (abfd, info_ptr,
18842 &cu->header, &bytes_read);
ae411497
TT
18843 info_ptr += bytes_read;
18844 break;
36586728
TT
18845 case DW_FORM_GNU_ref_alt:
18846 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
18847 info_ptr += bytes_read;
18848 break;
ae411497 18849 case DW_FORM_addr:
e7c27a73 18850 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
3e29f34a 18851 DW_ADDR (attr) = gdbarch_adjust_dwarf2_addr (gdbarch, DW_ADDR (attr));
107d2387 18852 info_ptr += bytes_read;
c906108c
SS
18853 break;
18854 case DW_FORM_block2:
7b5a2f43 18855 blk = dwarf_alloc_block (cu);
c906108c
SS
18856 blk->size = read_2_bytes (abfd, info_ptr);
18857 info_ptr += 2;
18858 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
18859 info_ptr += blk->size;
18860 DW_BLOCK (attr) = blk;
18861 break;
18862 case DW_FORM_block4:
7b5a2f43 18863 blk = dwarf_alloc_block (cu);
c906108c
SS
18864 blk->size = read_4_bytes (abfd, info_ptr);
18865 info_ptr += 4;
18866 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
18867 info_ptr += blk->size;
18868 DW_BLOCK (attr) = blk;
18869 break;
18870 case DW_FORM_data2:
18871 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
18872 info_ptr += 2;
18873 break;
18874 case DW_FORM_data4:
18875 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
18876 info_ptr += 4;
18877 break;
18878 case DW_FORM_data8:
18879 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
18880 info_ptr += 8;
18881 break;
0224619f
JK
18882 case DW_FORM_data16:
18883 blk = dwarf_alloc_block (cu);
18884 blk->size = 16;
18885 blk->data = read_n_bytes (abfd, info_ptr, 16);
18886 info_ptr += 16;
18887 DW_BLOCK (attr) = blk;
18888 break;
2dc7f7b3
TT
18889 case DW_FORM_sec_offset:
18890 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
18891 info_ptr += bytes_read;
18892 break;
c906108c 18893 case DW_FORM_string:
9b1c24c8 18894 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
8285870a 18895 DW_STRING_IS_CANONICAL (attr) = 0;
c906108c
SS
18896 info_ptr += bytes_read;
18897 break;
4bdf3d34 18898 case DW_FORM_strp:
36586728
TT
18899 if (!cu->per_cu->is_dwz)
18900 {
18901 DW_STRING (attr) = read_indirect_string (abfd, info_ptr, cu_header,
18902 &bytes_read);
18903 DW_STRING_IS_CANONICAL (attr) = 0;
18904 info_ptr += bytes_read;
18905 break;
18906 }
18907 /* FALLTHROUGH */
43988095
JK
18908 case DW_FORM_line_strp:
18909 if (!cu->per_cu->is_dwz)
18910 {
18911 DW_STRING (attr) = read_indirect_line_string (abfd, info_ptr,
18912 cu_header, &bytes_read);
18913 DW_STRING_IS_CANONICAL (attr) = 0;
18914 info_ptr += bytes_read;
18915 break;
18916 }
18917 /* FALLTHROUGH */
36586728
TT
18918 case DW_FORM_GNU_strp_alt:
18919 {
18920 struct dwz_file *dwz = dwarf2_get_dwz_file ();
18921 LONGEST str_offset = read_offset (abfd, info_ptr, cu_header,
18922 &bytes_read);
18923
18924 DW_STRING (attr) = read_indirect_string_from_dwz (dwz, str_offset);
18925 DW_STRING_IS_CANONICAL (attr) = 0;
18926 info_ptr += bytes_read;
18927 }
4bdf3d34 18928 break;
2dc7f7b3 18929 case DW_FORM_exprloc:
c906108c 18930 case DW_FORM_block:
7b5a2f43 18931 blk = dwarf_alloc_block (cu);
c906108c
SS
18932 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18933 info_ptr += bytes_read;
18934 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
18935 info_ptr += blk->size;
18936 DW_BLOCK (attr) = blk;
18937 break;
18938 case DW_FORM_block1:
7b5a2f43 18939 blk = dwarf_alloc_block (cu);
c906108c
SS
18940 blk->size = read_1_byte (abfd, info_ptr);
18941 info_ptr += 1;
18942 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
18943 info_ptr += blk->size;
18944 DW_BLOCK (attr) = blk;
18945 break;
18946 case DW_FORM_data1:
18947 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
18948 info_ptr += 1;
18949 break;
18950 case DW_FORM_flag:
18951 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
18952 info_ptr += 1;
18953 break;
2dc7f7b3
TT
18954 case DW_FORM_flag_present:
18955 DW_UNSND (attr) = 1;
18956 break;
c906108c
SS
18957 case DW_FORM_sdata:
18958 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
18959 info_ptr += bytes_read;
18960 break;
18961 case DW_FORM_udata:
18962 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18963 info_ptr += bytes_read;
18964 break;
18965 case DW_FORM_ref1:
9c541725 18966 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 18967 + read_1_byte (abfd, info_ptr));
c906108c
SS
18968 info_ptr += 1;
18969 break;
18970 case DW_FORM_ref2:
9c541725 18971 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 18972 + read_2_bytes (abfd, info_ptr));
c906108c
SS
18973 info_ptr += 2;
18974 break;
18975 case DW_FORM_ref4:
9c541725 18976 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 18977 + read_4_bytes (abfd, info_ptr));
c906108c
SS
18978 info_ptr += 4;
18979 break;
613e1657 18980 case DW_FORM_ref8:
9c541725 18981 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 18982 + read_8_bytes (abfd, info_ptr));
613e1657
KB
18983 info_ptr += 8;
18984 break;
55f1336d 18985 case DW_FORM_ref_sig8:
ac9ec31b 18986 DW_SIGNATURE (attr) = read_8_bytes (abfd, info_ptr);
348e048f
DE
18987 info_ptr += 8;
18988 break;
c906108c 18989 case DW_FORM_ref_udata:
9c541725 18990 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 18991 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
c906108c
SS
18992 info_ptr += bytes_read;
18993 break;
c906108c 18994 case DW_FORM_indirect:
a8329558
KW
18995 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18996 info_ptr += bytes_read;
43988095
JK
18997 if (form == DW_FORM_implicit_const)
18998 {
18999 implicit_const = read_signed_leb128 (abfd, info_ptr, &bytes_read);
19000 info_ptr += bytes_read;
19001 }
19002 info_ptr = read_attribute_value (reader, attr, form, implicit_const,
19003 info_ptr);
19004 break;
19005 case DW_FORM_implicit_const:
19006 DW_SND (attr) = implicit_const;
a8329558 19007 break;
3019eac3
DE
19008 case DW_FORM_GNU_addr_index:
19009 if (reader->dwo_file == NULL)
19010 {
19011 /* For now flag a hard error.
19012 Later we can turn this into a complaint. */
19013 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
19014 dwarf_form_name (form),
19015 bfd_get_filename (abfd));
19016 }
19017 DW_ADDR (attr) = read_addr_index_from_leb128 (cu, info_ptr, &bytes_read);
19018 info_ptr += bytes_read;
19019 break;
19020 case DW_FORM_GNU_str_index:
19021 if (reader->dwo_file == NULL)
19022 {
19023 /* For now flag a hard error.
19024 Later we can turn this into a complaint if warranted. */
19025 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
19026 dwarf_form_name (form),
19027 bfd_get_filename (abfd));
19028 }
19029 {
19030 ULONGEST str_index =
19031 read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19032
342587c4 19033 DW_STRING (attr) = read_str_index (reader, str_index);
3019eac3
DE
19034 DW_STRING_IS_CANONICAL (attr) = 0;
19035 info_ptr += bytes_read;
19036 }
19037 break;
c906108c 19038 default:
8a3fe4f8 19039 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
659b0389
ML
19040 dwarf_form_name (form),
19041 bfd_get_filename (abfd));
c906108c 19042 }
28e94949 19043
36586728 19044 /* Super hack. */
7771576e 19045 if (cu->per_cu->is_dwz && attr_form_is_ref (attr))
36586728
TT
19046 attr->form = DW_FORM_GNU_ref_alt;
19047
28e94949
JB
19048 /* We have seen instances where the compiler tried to emit a byte
19049 size attribute of -1 which ended up being encoded as an unsigned
19050 0xffffffff. Although 0xffffffff is technically a valid size value,
19051 an object of this size seems pretty unlikely so we can relatively
19052 safely treat these cases as if the size attribute was invalid and
19053 treat them as zero by default. */
19054 if (attr->name == DW_AT_byte_size
19055 && form == DW_FORM_data4
19056 && DW_UNSND (attr) >= 0xffffffff)
01c66ae6
JB
19057 {
19058 complaint
19059 (&symfile_complaints,
43bbcdc2
PH
19060 _("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
19061 hex_string (DW_UNSND (attr)));
01c66ae6
JB
19062 DW_UNSND (attr) = 0;
19063 }
28e94949 19064
c906108c
SS
19065 return info_ptr;
19066}
19067
a8329558
KW
19068/* Read an attribute described by an abbreviated attribute. */
19069
d521ce57 19070static const gdb_byte *
dee91e82
DE
19071read_attribute (const struct die_reader_specs *reader,
19072 struct attribute *attr, struct attr_abbrev *abbrev,
d521ce57 19073 const gdb_byte *info_ptr)
a8329558
KW
19074{
19075 attr->name = abbrev->name;
43988095
JK
19076 return read_attribute_value (reader, attr, abbrev->form,
19077 abbrev->implicit_const, info_ptr);
a8329558
KW
19078}
19079
0963b4bd 19080/* Read dwarf information from a buffer. */
c906108c
SS
19081
19082static unsigned int
a1855c1d 19083read_1_byte (bfd *abfd, const gdb_byte *buf)
c906108c 19084{
fe1b8b76 19085 return bfd_get_8 (abfd, buf);
c906108c
SS
19086}
19087
19088static int
a1855c1d 19089read_1_signed_byte (bfd *abfd, const gdb_byte *buf)
c906108c 19090{
fe1b8b76 19091 return bfd_get_signed_8 (abfd, buf);
c906108c
SS
19092}
19093
19094static unsigned int
a1855c1d 19095read_2_bytes (bfd *abfd, const gdb_byte *buf)
c906108c 19096{
fe1b8b76 19097 return bfd_get_16 (abfd, buf);
c906108c
SS
19098}
19099
21ae7a4d 19100static int
a1855c1d 19101read_2_signed_bytes (bfd *abfd, const gdb_byte *buf)
21ae7a4d
JK
19102{
19103 return bfd_get_signed_16 (abfd, buf);
19104}
19105
c906108c 19106static unsigned int
a1855c1d 19107read_4_bytes (bfd *abfd, const gdb_byte *buf)
c906108c 19108{
fe1b8b76 19109 return bfd_get_32 (abfd, buf);
c906108c
SS
19110}
19111
21ae7a4d 19112static int
a1855c1d 19113read_4_signed_bytes (bfd *abfd, const gdb_byte *buf)
21ae7a4d
JK
19114{
19115 return bfd_get_signed_32 (abfd, buf);
19116}
19117
93311388 19118static ULONGEST
a1855c1d 19119read_8_bytes (bfd *abfd, const gdb_byte *buf)
c906108c 19120{
fe1b8b76 19121 return bfd_get_64 (abfd, buf);
c906108c
SS
19122}
19123
19124static CORE_ADDR
d521ce57 19125read_address (bfd *abfd, const gdb_byte *buf, struct dwarf2_cu *cu,
891d2f0b 19126 unsigned int *bytes_read)
c906108c 19127{
e7c27a73 19128 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
19129 CORE_ADDR retval = 0;
19130
107d2387 19131 if (cu_header->signed_addr_p)
c906108c 19132 {
107d2387
AC
19133 switch (cu_header->addr_size)
19134 {
19135 case 2:
fe1b8b76 19136 retval = bfd_get_signed_16 (abfd, buf);
107d2387
AC
19137 break;
19138 case 4:
fe1b8b76 19139 retval = bfd_get_signed_32 (abfd, buf);
107d2387
AC
19140 break;
19141 case 8:
fe1b8b76 19142 retval = bfd_get_signed_64 (abfd, buf);
107d2387
AC
19143 break;
19144 default:
8e65ff28 19145 internal_error (__FILE__, __LINE__,
e2e0b3e5 19146 _("read_address: bad switch, signed [in module %s]"),
659b0389 19147 bfd_get_filename (abfd));
107d2387
AC
19148 }
19149 }
19150 else
19151 {
19152 switch (cu_header->addr_size)
19153 {
19154 case 2:
fe1b8b76 19155 retval = bfd_get_16 (abfd, buf);
107d2387
AC
19156 break;
19157 case 4:
fe1b8b76 19158 retval = bfd_get_32 (abfd, buf);
107d2387
AC
19159 break;
19160 case 8:
fe1b8b76 19161 retval = bfd_get_64 (abfd, buf);
107d2387
AC
19162 break;
19163 default:
8e65ff28 19164 internal_error (__FILE__, __LINE__,
a73c6dcd
MS
19165 _("read_address: bad switch, "
19166 "unsigned [in module %s]"),
659b0389 19167 bfd_get_filename (abfd));
107d2387 19168 }
c906108c 19169 }
64367e0a 19170
107d2387
AC
19171 *bytes_read = cu_header->addr_size;
19172 return retval;
c906108c
SS
19173}
19174
f7ef9339 19175/* Read the initial length from a section. The (draft) DWARF 3
613e1657
KB
19176 specification allows the initial length to take up either 4 bytes
19177 or 12 bytes. If the first 4 bytes are 0xffffffff, then the next 8
19178 bytes describe the length and all offsets will be 8 bytes in length
19179 instead of 4.
19180
f7ef9339
KB
19181 An older, non-standard 64-bit format is also handled by this
19182 function. The older format in question stores the initial length
19183 as an 8-byte quantity without an escape value. Lengths greater
19184 than 2^32 aren't very common which means that the initial 4 bytes
19185 is almost always zero. Since a length value of zero doesn't make
19186 sense for the 32-bit format, this initial zero can be considered to
19187 be an escape value which indicates the presence of the older 64-bit
19188 format. As written, the code can't detect (old format) lengths
917c78fc
MK
19189 greater than 4GB. If it becomes necessary to handle lengths
19190 somewhat larger than 4GB, we could allow other small values (such
19191 as the non-sensical values of 1, 2, and 3) to also be used as
19192 escape values indicating the presence of the old format.
f7ef9339 19193
917c78fc
MK
19194 The value returned via bytes_read should be used to increment the
19195 relevant pointer after calling read_initial_length().
c764a876 19196
613e1657
KB
19197 [ Note: read_initial_length() and read_offset() are based on the
19198 document entitled "DWARF Debugging Information Format", revision
f7ef9339 19199 3, draft 8, dated November 19, 2001. This document was obtained
613e1657
KB
19200 from:
19201
f7ef9339 19202 http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
6e70227d 19203
613e1657
KB
19204 This document is only a draft and is subject to change. (So beware.)
19205
f7ef9339 19206 Details regarding the older, non-standard 64-bit format were
917c78fc
MK
19207 determined empirically by examining 64-bit ELF files produced by
19208 the SGI toolchain on an IRIX 6.5 machine.
f7ef9339
KB
19209
19210 - Kevin, July 16, 2002
613e1657
KB
19211 ] */
19212
19213static LONGEST
d521ce57 19214read_initial_length (bfd *abfd, const gdb_byte *buf, unsigned int *bytes_read)
613e1657 19215{
fe1b8b76 19216 LONGEST length = bfd_get_32 (abfd, buf);
613e1657 19217
dd373385 19218 if (length == 0xffffffff)
613e1657 19219 {
fe1b8b76 19220 length = bfd_get_64 (abfd, buf + 4);
613e1657 19221 *bytes_read = 12;
613e1657 19222 }
dd373385 19223 else if (length == 0)
f7ef9339 19224 {
dd373385 19225 /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX. */
fe1b8b76 19226 length = bfd_get_64 (abfd, buf);
f7ef9339 19227 *bytes_read = 8;
f7ef9339 19228 }
613e1657
KB
19229 else
19230 {
19231 *bytes_read = 4;
613e1657
KB
19232 }
19233
c764a876
DE
19234 return length;
19235}
dd373385 19236
c764a876
DE
19237/* Cover function for read_initial_length.
19238 Returns the length of the object at BUF, and stores the size of the
19239 initial length in *BYTES_READ and stores the size that offsets will be in
19240 *OFFSET_SIZE.
19241 If the initial length size is not equivalent to that specified in
19242 CU_HEADER then issue a complaint.
19243 This is useful when reading non-comp-unit headers. */
dd373385 19244
c764a876 19245static LONGEST
d521ce57 19246read_checked_initial_length_and_offset (bfd *abfd, const gdb_byte *buf,
c764a876
DE
19247 const struct comp_unit_head *cu_header,
19248 unsigned int *bytes_read,
19249 unsigned int *offset_size)
19250{
19251 LONGEST length = read_initial_length (abfd, buf, bytes_read);
19252
19253 gdb_assert (cu_header->initial_length_size == 4
19254 || cu_header->initial_length_size == 8
19255 || cu_header->initial_length_size == 12);
19256
19257 if (cu_header->initial_length_size != *bytes_read)
19258 complaint (&symfile_complaints,
19259 _("intermixed 32-bit and 64-bit DWARF sections"));
dd373385 19260
c764a876 19261 *offset_size = (*bytes_read == 4) ? 4 : 8;
dd373385 19262 return length;
613e1657
KB
19263}
19264
19265/* Read an offset from the data stream. The size of the offset is
917c78fc 19266 given by cu_header->offset_size. */
613e1657
KB
19267
19268static LONGEST
d521ce57
TT
19269read_offset (bfd *abfd, const gdb_byte *buf,
19270 const struct comp_unit_head *cu_header,
891d2f0b 19271 unsigned int *bytes_read)
c764a876
DE
19272{
19273 LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
9a619af0 19274
c764a876
DE
19275 *bytes_read = cu_header->offset_size;
19276 return offset;
19277}
19278
19279/* Read an offset from the data stream. */
19280
19281static LONGEST
d521ce57 19282read_offset_1 (bfd *abfd, const gdb_byte *buf, unsigned int offset_size)
613e1657
KB
19283{
19284 LONGEST retval = 0;
19285
c764a876 19286 switch (offset_size)
613e1657
KB
19287 {
19288 case 4:
fe1b8b76 19289 retval = bfd_get_32 (abfd, buf);
613e1657
KB
19290 break;
19291 case 8:
fe1b8b76 19292 retval = bfd_get_64 (abfd, buf);
613e1657
KB
19293 break;
19294 default:
8e65ff28 19295 internal_error (__FILE__, __LINE__,
c764a876 19296 _("read_offset_1: bad switch [in module %s]"),
659b0389 19297 bfd_get_filename (abfd));
613e1657
KB
19298 }
19299
917c78fc 19300 return retval;
613e1657
KB
19301}
19302
d521ce57
TT
19303static const gdb_byte *
19304read_n_bytes (bfd *abfd, const gdb_byte *buf, unsigned int size)
c906108c
SS
19305{
19306 /* If the size of a host char is 8 bits, we can return a pointer
19307 to the buffer, otherwise we have to copy the data to a buffer
19308 allocated on the temporary obstack. */
4bdf3d34 19309 gdb_assert (HOST_CHAR_BIT == 8);
c906108c 19310 return buf;
c906108c
SS
19311}
19312
d521ce57
TT
19313static const char *
19314read_direct_string (bfd *abfd, const gdb_byte *buf,
19315 unsigned int *bytes_read_ptr)
c906108c
SS
19316{
19317 /* If the size of a host char is 8 bits, we can return a pointer
19318 to the string, otherwise we have to copy the string to a buffer
19319 allocated on the temporary obstack. */
4bdf3d34 19320 gdb_assert (HOST_CHAR_BIT == 8);
c906108c
SS
19321 if (*buf == '\0')
19322 {
19323 *bytes_read_ptr = 1;
19324 return NULL;
19325 }
d521ce57
TT
19326 *bytes_read_ptr = strlen ((const char *) buf) + 1;
19327 return (const char *) buf;
4bdf3d34
JJ
19328}
19329
43988095
JK
19330/* Return pointer to string at section SECT offset STR_OFFSET with error
19331 reporting strings FORM_NAME and SECT_NAME. */
19332
d521ce57 19333static const char *
43988095
JK
19334read_indirect_string_at_offset_from (bfd *abfd, LONGEST str_offset,
19335 struct dwarf2_section_info *sect,
19336 const char *form_name,
19337 const char *sect_name)
19338{
19339 dwarf2_read_section (dwarf2_per_objfile->objfile, sect);
19340 if (sect->buffer == NULL)
19341 error (_("%s used without %s section [in module %s]"),
19342 form_name, sect_name, bfd_get_filename (abfd));
19343 if (str_offset >= sect->size)
19344 error (_("%s pointing outside of %s section [in module %s]"),
19345 form_name, sect_name, bfd_get_filename (abfd));
4bdf3d34 19346 gdb_assert (HOST_CHAR_BIT == 8);
43988095 19347 if (sect->buffer[str_offset] == '\0')
4bdf3d34 19348 return NULL;
43988095
JK
19349 return (const char *) (sect->buffer + str_offset);
19350}
19351
19352/* Return pointer to string at .debug_str offset STR_OFFSET. */
19353
19354static const char *
19355read_indirect_string_at_offset (bfd *abfd, LONGEST str_offset)
19356{
19357 return read_indirect_string_at_offset_from (abfd, str_offset,
19358 &dwarf2_per_objfile->str,
19359 "DW_FORM_strp", ".debug_str");
19360}
19361
19362/* Return pointer to string at .debug_line_str offset STR_OFFSET. */
19363
19364static const char *
19365read_indirect_line_string_at_offset (bfd *abfd, LONGEST str_offset)
19366{
19367 return read_indirect_string_at_offset_from (abfd, str_offset,
19368 &dwarf2_per_objfile->line_str,
19369 "DW_FORM_line_strp",
19370 ".debug_line_str");
c906108c
SS
19371}
19372
36586728
TT
19373/* Read a string at offset STR_OFFSET in the .debug_str section from
19374 the .dwz file DWZ. Throw an error if the offset is too large. If
19375 the string consists of a single NUL byte, return NULL; otherwise
19376 return a pointer to the string. */
19377
d521ce57 19378static const char *
36586728
TT
19379read_indirect_string_from_dwz (struct dwz_file *dwz, LONGEST str_offset)
19380{
19381 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwz->str);
19382
19383 if (dwz->str.buffer == NULL)
19384 error (_("DW_FORM_GNU_strp_alt used without .debug_str "
19385 "section [in module %s]"),
19386 bfd_get_filename (dwz->dwz_bfd));
19387 if (str_offset >= dwz->str.size)
19388 error (_("DW_FORM_GNU_strp_alt pointing outside of "
19389 ".debug_str section [in module %s]"),
19390 bfd_get_filename (dwz->dwz_bfd));
19391 gdb_assert (HOST_CHAR_BIT == 8);
19392 if (dwz->str.buffer[str_offset] == '\0')
19393 return NULL;
d521ce57 19394 return (const char *) (dwz->str.buffer + str_offset);
36586728
TT
19395}
19396
43988095
JK
19397/* Return pointer to string at .debug_str offset as read from BUF.
19398 BUF is assumed to be in a compilation unit described by CU_HEADER.
19399 Return *BYTES_READ_PTR count of bytes read from BUF. */
19400
d521ce57
TT
19401static const char *
19402read_indirect_string (bfd *abfd, const gdb_byte *buf,
cf2c3c16
TT
19403 const struct comp_unit_head *cu_header,
19404 unsigned int *bytes_read_ptr)
19405{
19406 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
19407
19408 return read_indirect_string_at_offset (abfd, str_offset);
19409}
19410
43988095
JK
19411/* Return pointer to string at .debug_line_str offset as read from BUF.
19412 BUF is assumed to be in a compilation unit described by CU_HEADER.
19413 Return *BYTES_READ_PTR count of bytes read from BUF. */
19414
19415static const char *
19416read_indirect_line_string (bfd *abfd, const gdb_byte *buf,
19417 const struct comp_unit_head *cu_header,
19418 unsigned int *bytes_read_ptr)
19419{
19420 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
19421
19422 return read_indirect_line_string_at_offset (abfd, str_offset);
19423}
19424
19425ULONGEST
d521ce57 19426read_unsigned_leb128 (bfd *abfd, const gdb_byte *buf,
43988095 19427 unsigned int *bytes_read_ptr)
c906108c 19428{
12df843f 19429 ULONGEST result;
ce5d95e1 19430 unsigned int num_read;
870f88f7 19431 int shift;
c906108c
SS
19432 unsigned char byte;
19433
19434 result = 0;
19435 shift = 0;
19436 num_read = 0;
c906108c
SS
19437 while (1)
19438 {
fe1b8b76 19439 byte = bfd_get_8 (abfd, buf);
c906108c
SS
19440 buf++;
19441 num_read++;
12df843f 19442 result |= ((ULONGEST) (byte & 127) << shift);
c906108c
SS
19443 if ((byte & 128) == 0)
19444 {
19445 break;
19446 }
19447 shift += 7;
19448 }
19449 *bytes_read_ptr = num_read;
19450 return result;
19451}
19452
12df843f 19453static LONGEST
d521ce57
TT
19454read_signed_leb128 (bfd *abfd, const gdb_byte *buf,
19455 unsigned int *bytes_read_ptr)
c906108c 19456{
12df843f 19457 LONGEST result;
870f88f7 19458 int shift, num_read;
c906108c
SS
19459 unsigned char byte;
19460
19461 result = 0;
19462 shift = 0;
c906108c 19463 num_read = 0;
c906108c
SS
19464 while (1)
19465 {
fe1b8b76 19466 byte = bfd_get_8 (abfd, buf);
c906108c
SS
19467 buf++;
19468 num_read++;
12df843f 19469 result |= ((LONGEST) (byte & 127) << shift);
c906108c
SS
19470 shift += 7;
19471 if ((byte & 128) == 0)
19472 {
19473 break;
19474 }
19475 }
77e0b926 19476 if ((shift < 8 * sizeof (result)) && (byte & 0x40))
12df843f 19477 result |= -(((LONGEST) 1) << shift);
c906108c
SS
19478 *bytes_read_ptr = num_read;
19479 return result;
19480}
19481
3019eac3
DE
19482/* Given index ADDR_INDEX in .debug_addr, fetch the value.
19483 ADDR_BASE is the DW_AT_GNU_addr_base attribute or zero.
19484 ADDR_SIZE is the size of addresses from the CU header. */
19485
19486static CORE_ADDR
19487read_addr_index_1 (unsigned int addr_index, ULONGEST addr_base, int addr_size)
19488{
19489 struct objfile *objfile = dwarf2_per_objfile->objfile;
19490 bfd *abfd = objfile->obfd;
19491 const gdb_byte *info_ptr;
19492
19493 dwarf2_read_section (objfile, &dwarf2_per_objfile->addr);
19494 if (dwarf2_per_objfile->addr.buffer == NULL)
19495 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
4262abfb 19496 objfile_name (objfile));
3019eac3
DE
19497 if (addr_base + addr_index * addr_size >= dwarf2_per_objfile->addr.size)
19498 error (_("DW_FORM_addr_index pointing outside of "
19499 ".debug_addr section [in module %s]"),
4262abfb 19500 objfile_name (objfile));
3019eac3
DE
19501 info_ptr = (dwarf2_per_objfile->addr.buffer
19502 + addr_base + addr_index * addr_size);
19503 if (addr_size == 4)
19504 return bfd_get_32 (abfd, info_ptr);
19505 else
19506 return bfd_get_64 (abfd, info_ptr);
19507}
19508
19509/* Given index ADDR_INDEX in .debug_addr, fetch the value. */
19510
19511static CORE_ADDR
19512read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
19513{
19514 return read_addr_index_1 (addr_index, cu->addr_base, cu->header.addr_size);
19515}
19516
19517/* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
19518
19519static CORE_ADDR
d521ce57 19520read_addr_index_from_leb128 (struct dwarf2_cu *cu, const gdb_byte *info_ptr,
3019eac3
DE
19521 unsigned int *bytes_read)
19522{
19523 bfd *abfd = cu->objfile->obfd;
19524 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
19525
19526 return read_addr_index (cu, addr_index);
19527}
19528
19529/* Data structure to pass results from dwarf2_read_addr_index_reader
19530 back to dwarf2_read_addr_index. */
19531
19532struct dwarf2_read_addr_index_data
19533{
19534 ULONGEST addr_base;
19535 int addr_size;
19536};
19537
19538/* die_reader_func for dwarf2_read_addr_index. */
19539
19540static void
19541dwarf2_read_addr_index_reader (const struct die_reader_specs *reader,
d521ce57 19542 const gdb_byte *info_ptr,
3019eac3
DE
19543 struct die_info *comp_unit_die,
19544 int has_children,
19545 void *data)
19546{
19547 struct dwarf2_cu *cu = reader->cu;
19548 struct dwarf2_read_addr_index_data *aidata =
19549 (struct dwarf2_read_addr_index_data *) data;
19550
19551 aidata->addr_base = cu->addr_base;
19552 aidata->addr_size = cu->header.addr_size;
19553}
19554
19555/* Given an index in .debug_addr, fetch the value.
19556 NOTE: This can be called during dwarf expression evaluation,
19557 long after the debug information has been read, and thus per_cu->cu
19558 may no longer exist. */
19559
19560CORE_ADDR
19561dwarf2_read_addr_index (struct dwarf2_per_cu_data *per_cu,
19562 unsigned int addr_index)
19563{
19564 struct objfile *objfile = per_cu->objfile;
19565 struct dwarf2_cu *cu = per_cu->cu;
19566 ULONGEST addr_base;
19567 int addr_size;
19568
19569 /* This is intended to be called from outside this file. */
19570 dw2_setup (objfile);
19571
19572 /* We need addr_base and addr_size.
19573 If we don't have PER_CU->cu, we have to get it.
19574 Nasty, but the alternative is storing the needed info in PER_CU,
19575 which at this point doesn't seem justified: it's not clear how frequently
19576 it would get used and it would increase the size of every PER_CU.
19577 Entry points like dwarf2_per_cu_addr_size do a similar thing
19578 so we're not in uncharted territory here.
19579 Alas we need to be a bit more complicated as addr_base is contained
19580 in the DIE.
19581
19582 We don't need to read the entire CU(/TU).
19583 We just need the header and top level die.
a1b64ce1 19584
3019eac3 19585 IWBN to use the aging mechanism to let us lazily later discard the CU.
a1b64ce1 19586 For now we skip this optimization. */
3019eac3
DE
19587
19588 if (cu != NULL)
19589 {
19590 addr_base = cu->addr_base;
19591 addr_size = cu->header.addr_size;
19592 }
19593 else
19594 {
19595 struct dwarf2_read_addr_index_data aidata;
19596
a1b64ce1
DE
19597 /* Note: We can't use init_cutu_and_read_dies_simple here,
19598 we need addr_base. */
19599 init_cutu_and_read_dies (per_cu, NULL, 0, 0,
19600 dwarf2_read_addr_index_reader, &aidata);
3019eac3
DE
19601 addr_base = aidata.addr_base;
19602 addr_size = aidata.addr_size;
19603 }
19604
19605 return read_addr_index_1 (addr_index, addr_base, addr_size);
19606}
19607
57d63ce2
DE
19608/* Given a DW_FORM_GNU_str_index, fetch the string.
19609 This is only used by the Fission support. */
3019eac3 19610
d521ce57 19611static const char *
342587c4 19612read_str_index (const struct die_reader_specs *reader, ULONGEST str_index)
3019eac3
DE
19613{
19614 struct objfile *objfile = dwarf2_per_objfile->objfile;
c5164cbc 19615 const char *objf_name = objfile_name (objfile);
3019eac3 19616 bfd *abfd = objfile->obfd;
342587c4 19617 struct dwarf2_cu *cu = reader->cu;
73869dc2
DE
19618 struct dwarf2_section_info *str_section = &reader->dwo_file->sections.str;
19619 struct dwarf2_section_info *str_offsets_section =
19620 &reader->dwo_file->sections.str_offsets;
d521ce57 19621 const gdb_byte *info_ptr;
3019eac3 19622 ULONGEST str_offset;
57d63ce2 19623 static const char form_name[] = "DW_FORM_GNU_str_index";
3019eac3 19624
73869dc2
DE
19625 dwarf2_read_section (objfile, str_section);
19626 dwarf2_read_section (objfile, str_offsets_section);
19627 if (str_section->buffer == NULL)
57d63ce2 19628 error (_("%s used without .debug_str.dwo section"
9c541725
PA
19629 " in CU at offset 0x%x [in module %s]"),
19630 form_name, to_underlying (cu->header.sect_off), objf_name);
73869dc2 19631 if (str_offsets_section->buffer == NULL)
57d63ce2 19632 error (_("%s used without .debug_str_offsets.dwo section"
9c541725
PA
19633 " in CU at offset 0x%x [in module %s]"),
19634 form_name, to_underlying (cu->header.sect_off), objf_name);
73869dc2 19635 if (str_index * cu->header.offset_size >= str_offsets_section->size)
57d63ce2 19636 error (_("%s pointing outside of .debug_str_offsets.dwo"
9c541725
PA
19637 " section in CU at offset 0x%x [in module %s]"),
19638 form_name, to_underlying (cu->header.sect_off), objf_name);
73869dc2 19639 info_ptr = (str_offsets_section->buffer
3019eac3
DE
19640 + str_index * cu->header.offset_size);
19641 if (cu->header.offset_size == 4)
19642 str_offset = bfd_get_32 (abfd, info_ptr);
19643 else
19644 str_offset = bfd_get_64 (abfd, info_ptr);
73869dc2 19645 if (str_offset >= str_section->size)
57d63ce2 19646 error (_("Offset from %s pointing outside of"
9c541725
PA
19647 " .debug_str.dwo section in CU at offset 0x%x [in module %s]"),
19648 form_name, to_underlying (cu->header.sect_off), objf_name);
73869dc2 19649 return (const char *) (str_section->buffer + str_offset);
3019eac3
DE
19650}
19651
3019eac3
DE
19652/* Return the length of an LEB128 number in BUF. */
19653
19654static int
19655leb128_size (const gdb_byte *buf)
19656{
19657 const gdb_byte *begin = buf;
19658 gdb_byte byte;
19659
19660 while (1)
19661 {
19662 byte = *buf++;
19663 if ((byte & 128) == 0)
19664 return buf - begin;
19665 }
19666}
19667
c906108c 19668static void
e142c38c 19669set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
c906108c
SS
19670{
19671 switch (lang)
19672 {
19673 case DW_LANG_C89:
76bee0cc 19674 case DW_LANG_C99:
0cfd832f 19675 case DW_LANG_C11:
c906108c 19676 case DW_LANG_C:
d1be3247 19677 case DW_LANG_UPC:
e142c38c 19678 cu->language = language_c;
c906108c 19679 break;
9c37b5ae 19680 case DW_LANG_Java:
c906108c 19681 case DW_LANG_C_plus_plus:
0cfd832f
MW
19682 case DW_LANG_C_plus_plus_11:
19683 case DW_LANG_C_plus_plus_14:
e142c38c 19684 cu->language = language_cplus;
c906108c 19685 break;
6aecb9c2
JB
19686 case DW_LANG_D:
19687 cu->language = language_d;
19688 break;
c906108c
SS
19689 case DW_LANG_Fortran77:
19690 case DW_LANG_Fortran90:
b21b22e0 19691 case DW_LANG_Fortran95:
f7de9aab
MW
19692 case DW_LANG_Fortran03:
19693 case DW_LANG_Fortran08:
e142c38c 19694 cu->language = language_fortran;
c906108c 19695 break;
a766d390
DE
19696 case DW_LANG_Go:
19697 cu->language = language_go;
19698 break;
c906108c 19699 case DW_LANG_Mips_Assembler:
e142c38c 19700 cu->language = language_asm;
c906108c
SS
19701 break;
19702 case DW_LANG_Ada83:
8aaf0b47 19703 case DW_LANG_Ada95:
bc5f45f8
JB
19704 cu->language = language_ada;
19705 break;
72019c9c
GM
19706 case DW_LANG_Modula2:
19707 cu->language = language_m2;
19708 break;
fe8e67fd
PM
19709 case DW_LANG_Pascal83:
19710 cu->language = language_pascal;
19711 break;
22566fbd
DJ
19712 case DW_LANG_ObjC:
19713 cu->language = language_objc;
19714 break;
c44af4eb
TT
19715 case DW_LANG_Rust:
19716 case DW_LANG_Rust_old:
19717 cu->language = language_rust;
19718 break;
c906108c
SS
19719 case DW_LANG_Cobol74:
19720 case DW_LANG_Cobol85:
c906108c 19721 default:
e142c38c 19722 cu->language = language_minimal;
c906108c
SS
19723 break;
19724 }
e142c38c 19725 cu->language_defn = language_def (cu->language);
c906108c
SS
19726}
19727
19728/* Return the named attribute or NULL if not there. */
19729
19730static struct attribute *
e142c38c 19731dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
c906108c 19732{
a48e046c 19733 for (;;)
c906108c 19734 {
a48e046c
TT
19735 unsigned int i;
19736 struct attribute *spec = NULL;
19737
19738 for (i = 0; i < die->num_attrs; ++i)
19739 {
19740 if (die->attrs[i].name == name)
19741 return &die->attrs[i];
19742 if (die->attrs[i].name == DW_AT_specification
19743 || die->attrs[i].name == DW_AT_abstract_origin)
19744 spec = &die->attrs[i];
19745 }
19746
19747 if (!spec)
19748 break;
c906108c 19749
f2f0e013 19750 die = follow_die_ref (die, spec, &cu);
f2f0e013 19751 }
c5aa993b 19752
c906108c
SS
19753 return NULL;
19754}
19755
348e048f
DE
19756/* Return the named attribute or NULL if not there,
19757 but do not follow DW_AT_specification, etc.
19758 This is for use in contexts where we're reading .debug_types dies.
19759 Following DW_AT_specification, DW_AT_abstract_origin will take us
19760 back up the chain, and we want to go down. */
19761
19762static struct attribute *
45e58e77 19763dwarf2_attr_no_follow (struct die_info *die, unsigned int name)
348e048f
DE
19764{
19765 unsigned int i;
19766
19767 for (i = 0; i < die->num_attrs; ++i)
19768 if (die->attrs[i].name == name)
19769 return &die->attrs[i];
19770
19771 return NULL;
19772}
19773
7d45c7c3
KB
19774/* Return the string associated with a string-typed attribute, or NULL if it
19775 is either not found or is of an incorrect type. */
19776
19777static const char *
19778dwarf2_string_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
19779{
19780 struct attribute *attr;
19781 const char *str = NULL;
19782
19783 attr = dwarf2_attr (die, name, cu);
19784
19785 if (attr != NULL)
19786 {
43988095 19787 if (attr->form == DW_FORM_strp || attr->form == DW_FORM_line_strp
b3340438
L
19788 || attr->form == DW_FORM_string
19789 || attr->form == DW_FORM_GNU_str_index
16eb6b2d 19790 || attr->form == DW_FORM_GNU_strp_alt)
7d45c7c3
KB
19791 str = DW_STRING (attr);
19792 else
19793 complaint (&symfile_complaints,
19794 _("string type expected for attribute %s for "
19795 "DIE at 0x%x in module %s"),
9c541725 19796 dwarf_attr_name (name), to_underlying (die->sect_off),
7d45c7c3
KB
19797 objfile_name (cu->objfile));
19798 }
19799
19800 return str;
19801}
19802
05cf31d1
JB
19803/* Return non-zero iff the attribute NAME is defined for the given DIE,
19804 and holds a non-zero value. This function should only be used for
2dc7f7b3 19805 DW_FORM_flag or DW_FORM_flag_present attributes. */
05cf31d1
JB
19806
19807static int
19808dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
19809{
19810 struct attribute *attr = dwarf2_attr (die, name, cu);
19811
19812 return (attr && DW_UNSND (attr));
19813}
19814
3ca72b44 19815static int
e142c38c 19816die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
3ca72b44 19817{
05cf31d1
JB
19818 /* A DIE is a declaration if it has a DW_AT_declaration attribute
19819 which value is non-zero. However, we have to be careful with
19820 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
19821 (via dwarf2_flag_true_p) follows this attribute. So we may
19822 end up accidently finding a declaration attribute that belongs
19823 to a different DIE referenced by the specification attribute,
19824 even though the given DIE does not have a declaration attribute. */
19825 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
19826 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
3ca72b44
AC
19827}
19828
63d06c5c 19829/* Return the die giving the specification for DIE, if there is
f2f0e013 19830 one. *SPEC_CU is the CU containing DIE on input, and the CU
edb3359d
DJ
19831 containing the return value on output. If there is no
19832 specification, but there is an abstract origin, that is
19833 returned. */
63d06c5c
DC
19834
19835static struct die_info *
f2f0e013 19836die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
63d06c5c 19837{
f2f0e013
DJ
19838 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
19839 *spec_cu);
63d06c5c 19840
edb3359d
DJ
19841 if (spec_attr == NULL)
19842 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
19843
63d06c5c
DC
19844 if (spec_attr == NULL)
19845 return NULL;
19846 else
f2f0e013 19847 return follow_die_ref (die, spec_attr, spec_cu);
63d06c5c 19848}
c906108c 19849
527f3840
JK
19850/* Stub for free_line_header to match void * callback types. */
19851
19852static void
19853free_line_header_voidp (void *arg)
19854{
9a3c8263 19855 struct line_header *lh = (struct line_header *) arg;
527f3840 19856
fff8551c 19857 delete lh;
527f3840
JK
19858}
19859
fff8551c
PA
19860void
19861line_header::add_include_dir (const char *include_dir)
c906108c 19862{
27e0867f 19863 if (dwarf_line_debug >= 2)
fff8551c
PA
19864 fprintf_unfiltered (gdb_stdlog, "Adding dir %zu: %s\n",
19865 include_dirs.size () + 1, include_dir);
27e0867f 19866
fff8551c 19867 include_dirs.push_back (include_dir);
debd256d 19868}
6e70227d 19869
fff8551c
PA
19870void
19871line_header::add_file_name (const char *name,
ecfb656c 19872 dir_index d_index,
fff8551c
PA
19873 unsigned int mod_time,
19874 unsigned int length)
debd256d 19875{
27e0867f
DE
19876 if (dwarf_line_debug >= 2)
19877 fprintf_unfiltered (gdb_stdlog, "Adding file %u: %s\n",
fff8551c 19878 (unsigned) file_names.size () + 1, name);
27e0867f 19879
ecfb656c 19880 file_names.emplace_back (name, d_index, mod_time, length);
debd256d 19881}
6e70227d 19882
83769d0b 19883/* A convenience function to find the proper .debug_line section for a CU. */
36586728
TT
19884
19885static struct dwarf2_section_info *
19886get_debug_line_section (struct dwarf2_cu *cu)
19887{
19888 struct dwarf2_section_info *section;
19889
19890 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
19891 DWO file. */
19892 if (cu->dwo_unit && cu->per_cu->is_debug_types)
19893 section = &cu->dwo_unit->dwo_file->sections.line;
19894 else if (cu->per_cu->is_dwz)
19895 {
19896 struct dwz_file *dwz = dwarf2_get_dwz_file ();
19897
19898 section = &dwz->line;
19899 }
19900 else
19901 section = &dwarf2_per_objfile->line;
19902
19903 return section;
19904}
19905
43988095
JK
19906/* Read directory or file name entry format, starting with byte of
19907 format count entries, ULEB128 pairs of entry formats, ULEB128 of
19908 entries count and the entries themselves in the described entry
19909 format. */
19910
19911static void
19912read_formatted_entries (bfd *abfd, const gdb_byte **bufp,
19913 struct line_header *lh,
19914 const struct comp_unit_head *cu_header,
19915 void (*callback) (struct line_header *lh,
19916 const char *name,
ecfb656c 19917 dir_index d_index,
43988095
JK
19918 unsigned int mod_time,
19919 unsigned int length))
19920{
19921 gdb_byte format_count, formati;
19922 ULONGEST data_count, datai;
19923 const gdb_byte *buf = *bufp;
19924 const gdb_byte *format_header_data;
43988095
JK
19925 unsigned int bytes_read;
19926
19927 format_count = read_1_byte (abfd, buf);
19928 buf += 1;
19929 format_header_data = buf;
19930 for (formati = 0; formati < format_count; formati++)
19931 {
19932 read_unsigned_leb128 (abfd, buf, &bytes_read);
19933 buf += bytes_read;
19934 read_unsigned_leb128 (abfd, buf, &bytes_read);
19935 buf += bytes_read;
19936 }
19937
19938 data_count = read_unsigned_leb128 (abfd, buf, &bytes_read);
19939 buf += bytes_read;
19940 for (datai = 0; datai < data_count; datai++)
19941 {
19942 const gdb_byte *format = format_header_data;
19943 struct file_entry fe;
19944
43988095
JK
19945 for (formati = 0; formati < format_count; formati++)
19946 {
ecfb656c 19947 ULONGEST content_type = read_unsigned_leb128 (abfd, format, &bytes_read);
43988095 19948 format += bytes_read;
43988095 19949
ecfb656c 19950 ULONGEST form = read_unsigned_leb128 (abfd, format, &bytes_read);
43988095 19951 format += bytes_read;
ecfb656c
PA
19952
19953 gdb::optional<const char *> string;
19954 gdb::optional<unsigned int> uint;
19955
43988095
JK
19956 switch (form)
19957 {
19958 case DW_FORM_string:
ecfb656c 19959 string.emplace (read_direct_string (abfd, buf, &bytes_read));
43988095
JK
19960 buf += bytes_read;
19961 break;
19962
19963 case DW_FORM_line_strp:
ecfb656c
PA
19964 string.emplace (read_indirect_line_string (abfd, buf,
19965 cu_header,
19966 &bytes_read));
43988095
JK
19967 buf += bytes_read;
19968 break;
19969
19970 case DW_FORM_data1:
ecfb656c 19971 uint.emplace (read_1_byte (abfd, buf));
43988095
JK
19972 buf += 1;
19973 break;
19974
19975 case DW_FORM_data2:
ecfb656c 19976 uint.emplace (read_2_bytes (abfd, buf));
43988095
JK
19977 buf += 2;
19978 break;
19979
19980 case DW_FORM_data4:
ecfb656c 19981 uint.emplace (read_4_bytes (abfd, buf));
43988095
JK
19982 buf += 4;
19983 break;
19984
19985 case DW_FORM_data8:
ecfb656c 19986 uint.emplace (read_8_bytes (abfd, buf));
43988095
JK
19987 buf += 8;
19988 break;
19989
19990 case DW_FORM_udata:
ecfb656c 19991 uint.emplace (read_unsigned_leb128 (abfd, buf, &bytes_read));
43988095
JK
19992 buf += bytes_read;
19993 break;
19994
19995 case DW_FORM_block:
19996 /* It is valid only for DW_LNCT_timestamp which is ignored by
19997 current GDB. */
19998 break;
19999 }
ecfb656c
PA
20000
20001 switch (content_type)
20002 {
20003 case DW_LNCT_path:
20004 if (string.has_value ())
20005 fe.name = *string;
20006 break;
20007 case DW_LNCT_directory_index:
20008 if (uint.has_value ())
20009 fe.d_index = (dir_index) *uint;
20010 break;
20011 case DW_LNCT_timestamp:
20012 if (uint.has_value ())
20013 fe.mod_time = *uint;
20014 break;
20015 case DW_LNCT_size:
20016 if (uint.has_value ())
20017 fe.length = *uint;
20018 break;
20019 case DW_LNCT_MD5:
20020 break;
20021 default:
20022 complaint (&symfile_complaints,
20023 _("Unknown format content type %s"),
20024 pulongest (content_type));
20025 }
43988095
JK
20026 }
20027
ecfb656c 20028 callback (lh, fe.name, fe.d_index, fe.mod_time, fe.length);
43988095
JK
20029 }
20030
20031 *bufp = buf;
20032}
20033
debd256d 20034/* Read the statement program header starting at OFFSET in
3019eac3 20035 .debug_line, or .debug_line.dwo. Return a pointer
6502dd73 20036 to a struct line_header, allocated using xmalloc.
cd366ee8
DE
20037 Returns NULL if there is a problem reading the header, e.g., if it
20038 has a version we don't understand.
debd256d
JB
20039
20040 NOTE: the strings in the include directory and file name tables of
3019eac3
DE
20041 the returned object point into the dwarf line section buffer,
20042 and must not be freed. */
ae2de4f8 20043
fff8551c 20044static line_header_up
9c541725 20045dwarf_decode_line_header (sect_offset sect_off, struct dwarf2_cu *cu)
debd256d 20046{
d521ce57 20047 const gdb_byte *line_ptr;
c764a876 20048 unsigned int bytes_read, offset_size;
debd256d 20049 int i;
d521ce57 20050 const char *cur_dir, *cur_file;
3019eac3
DE
20051 struct dwarf2_section_info *section;
20052 bfd *abfd;
20053
36586728 20054 section = get_debug_line_section (cu);
3019eac3
DE
20055 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
20056 if (section->buffer == NULL)
debd256d 20057 {
3019eac3
DE
20058 if (cu->dwo_unit && cu->per_cu->is_debug_types)
20059 complaint (&symfile_complaints, _("missing .debug_line.dwo section"));
20060 else
20061 complaint (&symfile_complaints, _("missing .debug_line section"));
debd256d
JB
20062 return 0;
20063 }
20064
fceca515
DE
20065 /* We can't do this until we know the section is non-empty.
20066 Only then do we know we have such a section. */
a32a8923 20067 abfd = get_section_bfd_owner (section);
fceca515 20068
a738430d
MK
20069 /* Make sure that at least there's room for the total_length field.
20070 That could be 12 bytes long, but we're just going to fudge that. */
9c541725 20071 if (to_underlying (sect_off) + 4 >= section->size)
debd256d 20072 {
4d3c2250 20073 dwarf2_statement_list_fits_in_line_number_section_complaint ();
debd256d
JB
20074 return 0;
20075 }
20076
fff8551c 20077 line_header_up lh (new line_header ());
debd256d 20078
9c541725 20079 lh->sect_off = sect_off;
527f3840
JK
20080 lh->offset_in_dwz = cu->per_cu->is_dwz;
20081
9c541725 20082 line_ptr = section->buffer + to_underlying (sect_off);
debd256d 20083
a738430d 20084 /* Read in the header. */
6e70227d 20085 lh->total_length =
c764a876
DE
20086 read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
20087 &bytes_read, &offset_size);
debd256d 20088 line_ptr += bytes_read;
3019eac3 20089 if (line_ptr + lh->total_length > (section->buffer + section->size))
debd256d 20090 {
4d3c2250 20091 dwarf2_statement_list_fits_in_line_number_section_complaint ();
debd256d
JB
20092 return 0;
20093 }
20094 lh->statement_program_end = line_ptr + lh->total_length;
20095 lh->version = read_2_bytes (abfd, line_ptr);
20096 line_ptr += 2;
43988095 20097 if (lh->version > 5)
cd366ee8
DE
20098 {
20099 /* This is a version we don't understand. The format could have
20100 changed in ways we don't handle properly so just punt. */
20101 complaint (&symfile_complaints,
20102 _("unsupported version in .debug_line section"));
20103 return NULL;
20104 }
43988095
JK
20105 if (lh->version >= 5)
20106 {
20107 gdb_byte segment_selector_size;
20108
20109 /* Skip address size. */
20110 read_1_byte (abfd, line_ptr);
20111 line_ptr += 1;
20112
20113 segment_selector_size = read_1_byte (abfd, line_ptr);
20114 line_ptr += 1;
20115 if (segment_selector_size != 0)
20116 {
20117 complaint (&symfile_complaints,
20118 _("unsupported segment selector size %u "
20119 "in .debug_line section"),
20120 segment_selector_size);
20121 return NULL;
20122 }
20123 }
c764a876
DE
20124 lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
20125 line_ptr += offset_size;
debd256d
JB
20126 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
20127 line_ptr += 1;
2dc7f7b3
TT
20128 if (lh->version >= 4)
20129 {
20130 lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
20131 line_ptr += 1;
20132 }
20133 else
20134 lh->maximum_ops_per_instruction = 1;
20135
20136 if (lh->maximum_ops_per_instruction == 0)
20137 {
20138 lh->maximum_ops_per_instruction = 1;
20139 complaint (&symfile_complaints,
3e43a32a
MS
20140 _("invalid maximum_ops_per_instruction "
20141 "in `.debug_line' section"));
2dc7f7b3
TT
20142 }
20143
debd256d
JB
20144 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
20145 line_ptr += 1;
20146 lh->line_base = read_1_signed_byte (abfd, line_ptr);
20147 line_ptr += 1;
20148 lh->line_range = read_1_byte (abfd, line_ptr);
20149 line_ptr += 1;
20150 lh->opcode_base = read_1_byte (abfd, line_ptr);
20151 line_ptr += 1;
fff8551c 20152 lh->standard_opcode_lengths.reset (new unsigned char[lh->opcode_base]);
debd256d
JB
20153
20154 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
20155 for (i = 1; i < lh->opcode_base; ++i)
20156 {
20157 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
20158 line_ptr += 1;
20159 }
20160
43988095 20161 if (lh->version >= 5)
debd256d 20162 {
43988095 20163 /* Read directory table. */
fff8551c
PA
20164 read_formatted_entries (abfd, &line_ptr, lh.get (), &cu->header,
20165 [] (struct line_header *lh, const char *name,
ecfb656c 20166 dir_index d_index, unsigned int mod_time,
fff8551c
PA
20167 unsigned int length)
20168 {
20169 lh->add_include_dir (name);
20170 });
debd256d 20171
43988095 20172 /* Read file name table. */
fff8551c
PA
20173 read_formatted_entries (abfd, &line_ptr, lh.get (), &cu->header,
20174 [] (struct line_header *lh, const char *name,
ecfb656c 20175 dir_index d_index, unsigned int mod_time,
fff8551c
PA
20176 unsigned int length)
20177 {
ecfb656c 20178 lh->add_file_name (name, d_index, mod_time, length);
fff8551c 20179 });
43988095
JK
20180 }
20181 else
debd256d 20182 {
43988095
JK
20183 /* Read directory table. */
20184 while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
20185 {
20186 line_ptr += bytes_read;
fff8551c 20187 lh->add_include_dir (cur_dir);
43988095 20188 }
debd256d
JB
20189 line_ptr += bytes_read;
20190
43988095
JK
20191 /* Read file name table. */
20192 while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
20193 {
ecfb656c
PA
20194 unsigned int mod_time, length;
20195 dir_index d_index;
43988095
JK
20196
20197 line_ptr += bytes_read;
ecfb656c 20198 d_index = (dir_index) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
43988095
JK
20199 line_ptr += bytes_read;
20200 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20201 line_ptr += bytes_read;
20202 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20203 line_ptr += bytes_read;
20204
ecfb656c 20205 lh->add_file_name (cur_file, d_index, mod_time, length);
43988095
JK
20206 }
20207 line_ptr += bytes_read;
debd256d 20208 }
6e70227d 20209 lh->statement_program_start = line_ptr;
debd256d 20210
3019eac3 20211 if (line_ptr > (section->buffer + section->size))
4d3c2250 20212 complaint (&symfile_complaints,
3e43a32a
MS
20213 _("line number info header doesn't "
20214 "fit in `.debug_line' section"));
debd256d 20215
debd256d
JB
20216 return lh;
20217}
c906108c 20218
c6da4cef
DE
20219/* Subroutine of dwarf_decode_lines to simplify it.
20220 Return the file name of the psymtab for included file FILE_INDEX
20221 in line header LH of PST.
20222 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
20223 If space for the result is malloc'd, it will be freed by a cleanup.
1ed59174
JK
20224 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename.
20225
20226 The function creates dangling cleanup registration. */
c6da4cef 20227
d521ce57 20228static const char *
c6da4cef
DE
20229psymtab_include_file_name (const struct line_header *lh, int file_index,
20230 const struct partial_symtab *pst,
20231 const char *comp_dir)
20232{
8c43009f 20233 const file_entry &fe = lh->file_names[file_index];
d521ce57
TT
20234 const char *include_name = fe.name;
20235 const char *include_name_to_compare = include_name;
72b9f47f
TT
20236 const char *pst_filename;
20237 char *copied_name = NULL;
c6da4cef
DE
20238 int file_is_pst;
20239
8c43009f 20240 const char *dir_name = fe.include_dir (lh);
c6da4cef
DE
20241
20242 if (!IS_ABSOLUTE_PATH (include_name)
20243 && (dir_name != NULL || comp_dir != NULL))
20244 {
20245 /* Avoid creating a duplicate psymtab for PST.
20246 We do this by comparing INCLUDE_NAME and PST_FILENAME.
20247 Before we do the comparison, however, we need to account
20248 for DIR_NAME and COMP_DIR.
20249 First prepend dir_name (if non-NULL). If we still don't
20250 have an absolute path prepend comp_dir (if non-NULL).
20251 However, the directory we record in the include-file's
20252 psymtab does not contain COMP_DIR (to match the
20253 corresponding symtab(s)).
20254
20255 Example:
20256
20257 bash$ cd /tmp
20258 bash$ gcc -g ./hello.c
20259 include_name = "hello.c"
20260 dir_name = "."
20261 DW_AT_comp_dir = comp_dir = "/tmp"
5f52445b
YQ
20262 DW_AT_name = "./hello.c"
20263
20264 */
c6da4cef
DE
20265
20266 if (dir_name != NULL)
20267 {
d521ce57
TT
20268 char *tem = concat (dir_name, SLASH_STRING,
20269 include_name, (char *)NULL);
20270
20271 make_cleanup (xfree, tem);
20272 include_name = tem;
c6da4cef 20273 include_name_to_compare = include_name;
c6da4cef
DE
20274 }
20275 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
20276 {
d521ce57
TT
20277 char *tem = concat (comp_dir, SLASH_STRING,
20278 include_name, (char *)NULL);
20279
20280 make_cleanup (xfree, tem);
20281 include_name_to_compare = tem;
c6da4cef
DE
20282 }
20283 }
20284
20285 pst_filename = pst->filename;
20286 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
20287 {
72b9f47f
TT
20288 copied_name = concat (pst->dirname, SLASH_STRING,
20289 pst_filename, (char *)NULL);
20290 pst_filename = copied_name;
c6da4cef
DE
20291 }
20292
1e3fad37 20293 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
c6da4cef 20294
72b9f47f
TT
20295 if (copied_name != NULL)
20296 xfree (copied_name);
c6da4cef
DE
20297
20298 if (file_is_pst)
20299 return NULL;
20300 return include_name;
20301}
20302
d9b3de22
DE
20303/* State machine to track the state of the line number program. */
20304
6f77053d 20305class lnp_state_machine
d9b3de22 20306{
6f77053d
PA
20307public:
20308 /* Initialize a machine state for the start of a line number
20309 program. */
20310 lnp_state_machine (gdbarch *arch, line_header *lh, bool record_lines_p);
20311
8c43009f
PA
20312 file_entry *current_file ()
20313 {
20314 /* lh->file_names is 0-based, but the file name numbers in the
20315 statement program are 1-based. */
6f77053d
PA
20316 return m_line_header->file_name_at (m_file);
20317 }
20318
20319 /* Record the line in the state machine. END_SEQUENCE is true if
20320 we're processing the end of a sequence. */
20321 void record_line (bool end_sequence);
20322
20323 /* Check address and if invalid nop-out the rest of the lines in this
20324 sequence. */
20325 void check_line_address (struct dwarf2_cu *cu,
20326 const gdb_byte *line_ptr,
20327 CORE_ADDR lowpc, CORE_ADDR address);
20328
20329 void handle_set_discriminator (unsigned int discriminator)
20330 {
20331 m_discriminator = discriminator;
20332 m_line_has_non_zero_discriminator |= discriminator != 0;
20333 }
20334
20335 /* Handle DW_LNE_set_address. */
20336 void handle_set_address (CORE_ADDR baseaddr, CORE_ADDR address)
20337 {
20338 m_op_index = 0;
20339 address += baseaddr;
20340 m_address = gdbarch_adjust_dwarf2_line (m_gdbarch, address, false);
20341 }
20342
20343 /* Handle DW_LNS_advance_pc. */
20344 void handle_advance_pc (CORE_ADDR adjust);
20345
20346 /* Handle a special opcode. */
20347 void handle_special_opcode (unsigned char op_code);
20348
20349 /* Handle DW_LNS_advance_line. */
20350 void handle_advance_line (int line_delta)
20351 {
20352 advance_line (line_delta);
20353 }
20354
20355 /* Handle DW_LNS_set_file. */
20356 void handle_set_file (file_name_index file);
20357
20358 /* Handle DW_LNS_negate_stmt. */
20359 void handle_negate_stmt ()
20360 {
20361 m_is_stmt = !m_is_stmt;
20362 }
20363
20364 /* Handle DW_LNS_const_add_pc. */
20365 void handle_const_add_pc ();
20366
20367 /* Handle DW_LNS_fixed_advance_pc. */
20368 void handle_fixed_advance_pc (CORE_ADDR addr_adj)
20369 {
20370 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20371 m_op_index = 0;
20372 }
20373
20374 /* Handle DW_LNS_copy. */
20375 void handle_copy ()
20376 {
20377 record_line (false);
20378 m_discriminator = 0;
20379 }
20380
20381 /* Handle DW_LNE_end_sequence. */
20382 void handle_end_sequence ()
20383 {
20384 m_record_line_callback = ::record_line;
20385 }
20386
20387private:
20388 /* Advance the line by LINE_DELTA. */
20389 void advance_line (int line_delta)
20390 {
20391 m_line += line_delta;
20392
20393 if (line_delta != 0)
20394 m_line_has_non_zero_discriminator = m_discriminator != 0;
8c43009f
PA
20395 }
20396
6f77053d
PA
20397 gdbarch *m_gdbarch;
20398
20399 /* True if we're recording lines.
20400 Otherwise we're building partial symtabs and are just interested in
20401 finding include files mentioned by the line number program. */
20402 bool m_record_lines_p;
20403
8c43009f 20404 /* The line number header. */
6f77053d 20405 line_header *m_line_header;
8c43009f 20406
6f77053d
PA
20407 /* These are part of the standard DWARF line number state machine,
20408 and initialized according to the DWARF spec. */
d9b3de22 20409
6f77053d 20410 unsigned char m_op_index = 0;
8c43009f 20411 /* The line table index (1-based) of the current file. */
6f77053d
PA
20412 file_name_index m_file = (file_name_index) 1;
20413 unsigned int m_line = 1;
20414
20415 /* These are initialized in the constructor. */
20416
20417 CORE_ADDR m_address;
20418 bool m_is_stmt;
20419 unsigned int m_discriminator;
d9b3de22
DE
20420
20421 /* Additional bits of state we need to track. */
20422
20423 /* The last file that we called dwarf2_start_subfile for.
20424 This is only used for TLLs. */
6f77053d 20425 unsigned int m_last_file = 0;
d9b3de22 20426 /* The last file a line number was recorded for. */
6f77053d 20427 struct subfile *m_last_subfile = NULL;
d9b3de22
DE
20428
20429 /* The function to call to record a line. */
6f77053d 20430 record_line_ftype *m_record_line_callback = NULL;
d9b3de22
DE
20431
20432 /* The last line number that was recorded, used to coalesce
20433 consecutive entries for the same line. This can happen, for
20434 example, when discriminators are present. PR 17276. */
6f77053d
PA
20435 unsigned int m_last_line = 0;
20436 bool m_line_has_non_zero_discriminator = false;
8c43009f 20437};
d9b3de22 20438
6f77053d
PA
20439void
20440lnp_state_machine::handle_advance_pc (CORE_ADDR adjust)
20441{
20442 CORE_ADDR addr_adj = (((m_op_index + adjust)
20443 / m_line_header->maximum_ops_per_instruction)
20444 * m_line_header->minimum_instruction_length);
20445 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20446 m_op_index = ((m_op_index + adjust)
20447 % m_line_header->maximum_ops_per_instruction);
20448}
d9b3de22 20449
6f77053d
PA
20450void
20451lnp_state_machine::handle_special_opcode (unsigned char op_code)
d9b3de22 20452{
6f77053d
PA
20453 unsigned char adj_opcode = op_code - m_line_header->opcode_base;
20454 CORE_ADDR addr_adj = (((m_op_index
20455 + (adj_opcode / m_line_header->line_range))
20456 / m_line_header->maximum_ops_per_instruction)
20457 * m_line_header->minimum_instruction_length);
20458 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20459 m_op_index = ((m_op_index + (adj_opcode / m_line_header->line_range))
20460 % m_line_header->maximum_ops_per_instruction);
d9b3de22 20461
6f77053d
PA
20462 int line_delta = (m_line_header->line_base
20463 + (adj_opcode % m_line_header->line_range));
20464 advance_line (line_delta);
20465 record_line (false);
20466 m_discriminator = 0;
20467}
d9b3de22 20468
6f77053d
PA
20469void
20470lnp_state_machine::handle_set_file (file_name_index file)
20471{
20472 m_file = file;
20473
20474 const file_entry *fe = current_file ();
20475 if (fe == NULL)
20476 dwarf2_debug_line_missing_file_complaint ();
20477 else if (m_record_lines_p)
20478 {
20479 const char *dir = fe->include_dir (m_line_header);
20480
20481 m_last_subfile = current_subfile;
20482 m_line_has_non_zero_discriminator = m_discriminator != 0;
20483 dwarf2_start_subfile (fe->name, dir);
20484 }
20485}
20486
20487void
20488lnp_state_machine::handle_const_add_pc ()
20489{
20490 CORE_ADDR adjust
20491 = (255 - m_line_header->opcode_base) / m_line_header->line_range;
20492
20493 CORE_ADDR addr_adj
20494 = (((m_op_index + adjust)
20495 / m_line_header->maximum_ops_per_instruction)
20496 * m_line_header->minimum_instruction_length);
20497
20498 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20499 m_op_index = ((m_op_index + adjust)
20500 % m_line_header->maximum_ops_per_instruction);
20501}
d9b3de22 20502
c91513d8
PP
20503/* Ignore this record_line request. */
20504
20505static void
20506noop_record_line (struct subfile *subfile, int line, CORE_ADDR pc)
20507{
20508 return;
20509}
20510
a05a36a5
DE
20511/* Return non-zero if we should add LINE to the line number table.
20512 LINE is the line to add, LAST_LINE is the last line that was added,
20513 LAST_SUBFILE is the subfile for LAST_LINE.
20514 LINE_HAS_NON_ZERO_DISCRIMINATOR is non-zero if LINE has ever
20515 had a non-zero discriminator.
20516
20517 We have to be careful in the presence of discriminators.
20518 E.g., for this line:
20519
20520 for (i = 0; i < 100000; i++);
20521
20522 clang can emit four line number entries for that one line,
20523 each with a different discriminator.
20524 See gdb.dwarf2/dw2-single-line-discriminators.exp for an example.
20525
20526 However, we want gdb to coalesce all four entries into one.
20527 Otherwise the user could stepi into the middle of the line and
20528 gdb would get confused about whether the pc really was in the
20529 middle of the line.
20530
20531 Things are further complicated by the fact that two consecutive
20532 line number entries for the same line is a heuristic used by gcc
20533 to denote the end of the prologue. So we can't just discard duplicate
20534 entries, we have to be selective about it. The heuristic we use is
20535 that we only collapse consecutive entries for the same line if at least
20536 one of those entries has a non-zero discriminator. PR 17276.
20537
20538 Note: Addresses in the line number state machine can never go backwards
20539 within one sequence, thus this coalescing is ok. */
20540
20541static int
20542dwarf_record_line_p (unsigned int line, unsigned int last_line,
20543 int line_has_non_zero_discriminator,
20544 struct subfile *last_subfile)
20545{
20546 if (current_subfile != last_subfile)
20547 return 1;
20548 if (line != last_line)
20549 return 1;
20550 /* Same line for the same file that we've seen already.
20551 As a last check, for pr 17276, only record the line if the line
20552 has never had a non-zero discriminator. */
20553 if (!line_has_non_zero_discriminator)
20554 return 1;
20555 return 0;
20556}
20557
252a6764
DE
20558/* Use P_RECORD_LINE to record line number LINE beginning at address ADDRESS
20559 in the line table of subfile SUBFILE. */
20560
20561static void
d9b3de22
DE
20562dwarf_record_line_1 (struct gdbarch *gdbarch, struct subfile *subfile,
20563 unsigned int line, CORE_ADDR address,
20564 record_line_ftype p_record_line)
252a6764
DE
20565{
20566 CORE_ADDR addr = gdbarch_addr_bits_remove (gdbarch, address);
20567
27e0867f
DE
20568 if (dwarf_line_debug)
20569 {
20570 fprintf_unfiltered (gdb_stdlog,
20571 "Recording line %u, file %s, address %s\n",
20572 line, lbasename (subfile->name),
20573 paddress (gdbarch, address));
20574 }
20575
d5962de5 20576 (*p_record_line) (subfile, line, addr);
252a6764
DE
20577}
20578
20579/* Subroutine of dwarf_decode_lines_1 to simplify it.
20580 Mark the end of a set of line number records.
d9b3de22 20581 The arguments are the same as for dwarf_record_line_1.
252a6764
DE
20582 If SUBFILE is NULL the request is ignored. */
20583
20584static void
20585dwarf_finish_line (struct gdbarch *gdbarch, struct subfile *subfile,
20586 CORE_ADDR address, record_line_ftype p_record_line)
20587{
27e0867f
DE
20588 if (subfile == NULL)
20589 return;
20590
20591 if (dwarf_line_debug)
20592 {
20593 fprintf_unfiltered (gdb_stdlog,
20594 "Finishing current line, file %s, address %s\n",
20595 lbasename (subfile->name),
20596 paddress (gdbarch, address));
20597 }
20598
d9b3de22
DE
20599 dwarf_record_line_1 (gdbarch, subfile, 0, address, p_record_line);
20600}
20601
6f77053d
PA
20602void
20603lnp_state_machine::record_line (bool end_sequence)
d9b3de22 20604{
d9b3de22
DE
20605 if (dwarf_line_debug)
20606 {
20607 fprintf_unfiltered (gdb_stdlog,
20608 "Processing actual line %u: file %u,"
20609 " address %s, is_stmt %u, discrim %u\n",
6f77053d
PA
20610 m_line, to_underlying (m_file),
20611 paddress (m_gdbarch, m_address),
20612 m_is_stmt, m_discriminator);
d9b3de22
DE
20613 }
20614
6f77053d 20615 file_entry *fe = current_file ();
8c43009f
PA
20616
20617 if (fe == NULL)
d9b3de22
DE
20618 dwarf2_debug_line_missing_file_complaint ();
20619 /* For now we ignore lines not starting on an instruction boundary.
20620 But not when processing end_sequence for compatibility with the
20621 previous version of the code. */
6f77053d 20622 else if (m_op_index == 0 || end_sequence)
d9b3de22 20623 {
8c43009f 20624 fe->included_p = 1;
6f77053d 20625 if (m_record_lines_p && m_is_stmt)
d9b3de22 20626 {
6f77053d 20627 if (m_last_subfile != current_subfile || end_sequence)
d9b3de22 20628 {
6f77053d
PA
20629 dwarf_finish_line (m_gdbarch, m_last_subfile,
20630 m_address, m_record_line_callback);
d9b3de22
DE
20631 }
20632
20633 if (!end_sequence)
20634 {
6f77053d
PA
20635 if (dwarf_record_line_p (m_line, m_last_line,
20636 m_line_has_non_zero_discriminator,
20637 m_last_subfile))
d9b3de22 20638 {
6f77053d
PA
20639 dwarf_record_line_1 (m_gdbarch, current_subfile,
20640 m_line, m_address,
20641 m_record_line_callback);
d9b3de22 20642 }
6f77053d
PA
20643 m_last_subfile = current_subfile;
20644 m_last_line = m_line;
d9b3de22
DE
20645 }
20646 }
20647 }
20648}
20649
6f77053d
PA
20650lnp_state_machine::lnp_state_machine (gdbarch *arch, line_header *lh,
20651 bool record_lines_p)
d9b3de22 20652{
6f77053d
PA
20653 m_gdbarch = arch;
20654 m_record_lines_p = record_lines_p;
20655 m_line_header = lh;
d9b3de22 20656
6f77053d 20657 m_record_line_callback = ::record_line;
d9b3de22 20658
d9b3de22
DE
20659 /* Call `gdbarch_adjust_dwarf2_line' on the initial 0 address as if there
20660 was a line entry for it so that the backend has a chance to adjust it
20661 and also record it in case it needs it. This is currently used by MIPS
20662 code, cf. `mips_adjust_dwarf2_line'. */
6f77053d
PA
20663 m_address = gdbarch_adjust_dwarf2_line (arch, 0, 0);
20664 m_is_stmt = lh->default_is_stmt;
20665 m_discriminator = 0;
252a6764
DE
20666}
20667
6f77053d
PA
20668void
20669lnp_state_machine::check_line_address (struct dwarf2_cu *cu,
20670 const gdb_byte *line_ptr,
20671 CORE_ADDR lowpc, CORE_ADDR address)
924c2928
DE
20672{
20673 /* If address < lowpc then it's not a usable value, it's outside the
20674 pc range of the CU. However, we restrict the test to only address
20675 values of zero to preserve GDB's previous behaviour which is to
20676 handle the specific case of a function being GC'd by the linker. */
20677
20678 if (address == 0 && address < lowpc)
20679 {
20680 /* This line table is for a function which has been
20681 GCd by the linker. Ignore it. PR gdb/12528 */
20682
20683 struct objfile *objfile = cu->objfile;
20684 long line_offset = line_ptr - get_debug_line_section (cu)->buffer;
20685
20686 complaint (&symfile_complaints,
20687 _(".debug_line address at offset 0x%lx is 0 [in module %s]"),
20688 line_offset, objfile_name (objfile));
6f77053d
PA
20689 m_record_line_callback = noop_record_line;
20690 /* Note: record_line_callback is left as noop_record_line until
20691 we see DW_LNE_end_sequence. */
924c2928
DE
20692 }
20693}
20694
f3f5162e 20695/* Subroutine of dwarf_decode_lines to simplify it.
d9b3de22
DE
20696 Process the line number information in LH.
20697 If DECODE_FOR_PST_P is non-zero, all we do is process the line number
20698 program in order to set included_p for every referenced header. */
debd256d 20699
c906108c 20700static void
43f3e411
DE
20701dwarf_decode_lines_1 (struct line_header *lh, struct dwarf2_cu *cu,
20702 const int decode_for_pst_p, CORE_ADDR lowpc)
c906108c 20703{
d521ce57
TT
20704 const gdb_byte *line_ptr, *extended_end;
20705 const gdb_byte *line_end;
a8c50c1f 20706 unsigned int bytes_read, extended_len;
699ca60a 20707 unsigned char op_code, extended_op;
e142c38c
DJ
20708 CORE_ADDR baseaddr;
20709 struct objfile *objfile = cu->objfile;
f3f5162e 20710 bfd *abfd = objfile->obfd;
fbf65064 20711 struct gdbarch *gdbarch = get_objfile_arch (objfile);
6f77053d
PA
20712 /* True if we're recording line info (as opposed to building partial
20713 symtabs and just interested in finding include files mentioned by
20714 the line number program). */
20715 bool record_lines_p = !decode_for_pst_p;
e142c38c
DJ
20716
20717 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 20718
debd256d
JB
20719 line_ptr = lh->statement_program_start;
20720 line_end = lh->statement_program_end;
c906108c
SS
20721
20722 /* Read the statement sequences until there's nothing left. */
20723 while (line_ptr < line_end)
20724 {
6f77053d
PA
20725 /* The DWARF line number program state machine. Reset the state
20726 machine at the start of each sequence. */
20727 lnp_state_machine state_machine (gdbarch, lh, record_lines_p);
20728 bool end_sequence = false;
d9b3de22 20729
8c43009f 20730 if (record_lines_p)
c906108c 20731 {
8c43009f
PA
20732 /* Start a subfile for the current file of the state
20733 machine. */
20734 const file_entry *fe = state_machine.current_file ();
20735
20736 if (fe != NULL)
20737 dwarf2_start_subfile (fe->name, fe->include_dir (lh));
c906108c
SS
20738 }
20739
a738430d 20740 /* Decode the table. */
d9b3de22 20741 while (line_ptr < line_end && !end_sequence)
c906108c
SS
20742 {
20743 op_code = read_1_byte (abfd, line_ptr);
20744 line_ptr += 1;
9aa1fe7e 20745
debd256d 20746 if (op_code >= lh->opcode_base)
6e70227d 20747 {
8e07a239 20748 /* Special opcode. */
6f77053d 20749 state_machine.handle_special_opcode (op_code);
9aa1fe7e
GK
20750 }
20751 else switch (op_code)
c906108c
SS
20752 {
20753 case DW_LNS_extended_op:
3e43a32a
MS
20754 extended_len = read_unsigned_leb128 (abfd, line_ptr,
20755 &bytes_read);
473b7be6 20756 line_ptr += bytes_read;
a8c50c1f 20757 extended_end = line_ptr + extended_len;
c906108c
SS
20758 extended_op = read_1_byte (abfd, line_ptr);
20759 line_ptr += 1;
20760 switch (extended_op)
20761 {
20762 case DW_LNE_end_sequence:
6f77053d
PA
20763 state_machine.handle_end_sequence ();
20764 end_sequence = true;
c906108c
SS
20765 break;
20766 case DW_LNE_set_address:
d9b3de22
DE
20767 {
20768 CORE_ADDR address
20769 = read_address (abfd, line_ptr, cu, &bytes_read);
d9b3de22 20770 line_ptr += bytes_read;
6f77053d
PA
20771
20772 state_machine.check_line_address (cu, line_ptr,
20773 lowpc, address);
20774 state_machine.handle_set_address (baseaddr, address);
d9b3de22 20775 }
c906108c
SS
20776 break;
20777 case DW_LNE_define_file:
debd256d 20778 {
d521ce57 20779 const char *cur_file;
ecfb656c
PA
20780 unsigned int mod_time, length;
20781 dir_index dindex;
6e70227d 20782
3e43a32a
MS
20783 cur_file = read_direct_string (abfd, line_ptr,
20784 &bytes_read);
debd256d 20785 line_ptr += bytes_read;
ecfb656c 20786 dindex = (dir_index)
debd256d
JB
20787 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20788 line_ptr += bytes_read;
20789 mod_time =
20790 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20791 line_ptr += bytes_read;
20792 length =
20793 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20794 line_ptr += bytes_read;
ecfb656c 20795 lh->add_file_name (cur_file, dindex, mod_time, length);
debd256d 20796 }
c906108c 20797 break;
d0c6ba3d 20798 case DW_LNE_set_discriminator:
6f77053d
PA
20799 {
20800 /* The discriminator is not interesting to the
20801 debugger; just ignore it. We still need to
20802 check its value though:
20803 if there are consecutive entries for the same
20804 (non-prologue) line we want to coalesce them.
20805 PR 17276. */
20806 unsigned int discr
20807 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20808 line_ptr += bytes_read;
20809
20810 state_machine.handle_set_discriminator (discr);
20811 }
d0c6ba3d 20812 break;
c906108c 20813 default:
4d3c2250 20814 complaint (&symfile_complaints,
e2e0b3e5 20815 _("mangled .debug_line section"));
debd256d 20816 return;
c906108c 20817 }
a8c50c1f
DJ
20818 /* Make sure that we parsed the extended op correctly. If e.g.
20819 we expected a different address size than the producer used,
20820 we may have read the wrong number of bytes. */
20821 if (line_ptr != extended_end)
20822 {
20823 complaint (&symfile_complaints,
20824 _("mangled .debug_line section"));
20825 return;
20826 }
c906108c
SS
20827 break;
20828 case DW_LNS_copy:
6f77053d 20829 state_machine.handle_copy ();
c906108c
SS
20830 break;
20831 case DW_LNS_advance_pc:
2dc7f7b3
TT
20832 {
20833 CORE_ADDR adjust
20834 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
2dc7f7b3 20835 line_ptr += bytes_read;
6f77053d
PA
20836
20837 state_machine.handle_advance_pc (adjust);
2dc7f7b3 20838 }
c906108c
SS
20839 break;
20840 case DW_LNS_advance_line:
a05a36a5
DE
20841 {
20842 int line_delta
20843 = read_signed_leb128 (abfd, line_ptr, &bytes_read);
a05a36a5 20844 line_ptr += bytes_read;
6f77053d
PA
20845
20846 state_machine.handle_advance_line (line_delta);
a05a36a5 20847 }
c906108c
SS
20848 break;
20849 case DW_LNS_set_file:
d9b3de22 20850 {
6f77053d 20851 file_name_index file
ecfb656c
PA
20852 = (file_name_index) read_unsigned_leb128 (abfd, line_ptr,
20853 &bytes_read);
d9b3de22 20854 line_ptr += bytes_read;
8c43009f 20855
6f77053d 20856 state_machine.handle_set_file (file);
d9b3de22 20857 }
c906108c
SS
20858 break;
20859 case DW_LNS_set_column:
0ad93d4f 20860 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
c906108c
SS
20861 line_ptr += bytes_read;
20862 break;
20863 case DW_LNS_negate_stmt:
6f77053d 20864 state_machine.handle_negate_stmt ();
c906108c
SS
20865 break;
20866 case DW_LNS_set_basic_block:
c906108c 20867 break;
c2c6d25f
JM
20868 /* Add to the address register of the state machine the
20869 address increment value corresponding to special opcode
a738430d
MK
20870 255. I.e., this value is scaled by the minimum
20871 instruction length since special opcode 255 would have
b021a221 20872 scaled the increment. */
c906108c 20873 case DW_LNS_const_add_pc:
6f77053d 20874 state_machine.handle_const_add_pc ();
c906108c
SS
20875 break;
20876 case DW_LNS_fixed_advance_pc:
3e29f34a 20877 {
6f77053d 20878 CORE_ADDR addr_adj = read_2_bytes (abfd, line_ptr);
3e29f34a 20879 line_ptr += 2;
6f77053d
PA
20880
20881 state_machine.handle_fixed_advance_pc (addr_adj);
3e29f34a 20882 }
c906108c 20883 break;
9aa1fe7e 20884 default:
a738430d
MK
20885 {
20886 /* Unknown standard opcode, ignore it. */
9aa1fe7e 20887 int i;
a738430d 20888
debd256d 20889 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
9aa1fe7e
GK
20890 {
20891 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20892 line_ptr += bytes_read;
20893 }
20894 }
c906108c
SS
20895 }
20896 }
d9b3de22
DE
20897
20898 if (!end_sequence)
20899 dwarf2_debug_line_missing_end_sequence_complaint ();
20900
20901 /* We got a DW_LNE_end_sequence (or we ran off the end of the buffer,
20902 in which case we still finish recording the last line). */
6f77053d 20903 state_machine.record_line (true);
c906108c 20904 }
f3f5162e
DE
20905}
20906
20907/* Decode the Line Number Program (LNP) for the given line_header
20908 structure and CU. The actual information extracted and the type
20909 of structures created from the LNP depends on the value of PST.
20910
20911 1. If PST is NULL, then this procedure uses the data from the program
20912 to create all necessary symbol tables, and their linetables.
20913
20914 2. If PST is not NULL, this procedure reads the program to determine
20915 the list of files included by the unit represented by PST, and
20916 builds all the associated partial symbol tables.
20917
20918 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
20919 It is used for relative paths in the line table.
20920 NOTE: When processing partial symtabs (pst != NULL),
20921 comp_dir == pst->dirname.
20922
20923 NOTE: It is important that psymtabs have the same file name (via strcmp)
20924 as the corresponding symtab. Since COMP_DIR is not used in the name of the
20925 symtab we don't use it in the name of the psymtabs we create.
20926 E.g. expand_line_sal requires this when finding psymtabs to expand.
c3b7b696
YQ
20927 A good testcase for this is mb-inline.exp.
20928
527f3840
JK
20929 LOWPC is the lowest address in CU (or 0 if not known).
20930
20931 Boolean DECODE_MAPPING specifies we need to fully decode .debug_line
20932 for its PC<->lines mapping information. Otherwise only the filename
20933 table is read in. */
f3f5162e
DE
20934
20935static void
20936dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
c3b7b696 20937 struct dwarf2_cu *cu, struct partial_symtab *pst,
527f3840 20938 CORE_ADDR lowpc, int decode_mapping)
f3f5162e
DE
20939{
20940 struct objfile *objfile = cu->objfile;
20941 const int decode_for_pst_p = (pst != NULL);
f3f5162e 20942
527f3840
JK
20943 if (decode_mapping)
20944 dwarf_decode_lines_1 (lh, cu, decode_for_pst_p, lowpc);
aaa75496
JB
20945
20946 if (decode_for_pst_p)
20947 {
20948 int file_index;
20949
20950 /* Now that we're done scanning the Line Header Program, we can
20951 create the psymtab of each included file. */
fff8551c 20952 for (file_index = 0; file_index < lh->file_names.size (); file_index++)
aaa75496
JB
20953 if (lh->file_names[file_index].included_p == 1)
20954 {
d521ce57 20955 const char *include_name =
c6da4cef
DE
20956 psymtab_include_file_name (lh, file_index, pst, comp_dir);
20957 if (include_name != NULL)
aaa75496
JB
20958 dwarf2_create_include_psymtab (include_name, pst, objfile);
20959 }
20960 }
cb1df416
DJ
20961 else
20962 {
20963 /* Make sure a symtab is created for every file, even files
20964 which contain only variables (i.e. no code with associated
20965 line numbers). */
43f3e411 20966 struct compunit_symtab *cust = buildsym_compunit_symtab ();
cb1df416 20967 int i;
cb1df416 20968
fff8551c 20969 for (i = 0; i < lh->file_names.size (); i++)
cb1df416 20970 {
8c43009f 20971 file_entry &fe = lh->file_names[i];
9a619af0 20972
8c43009f 20973 dwarf2_start_subfile (fe.name, fe.include_dir (lh));
cb1df416 20974
cb1df416 20975 if (current_subfile->symtab == NULL)
43f3e411
DE
20976 {
20977 current_subfile->symtab
20978 = allocate_symtab (cust, current_subfile->name);
20979 }
8c43009f 20980 fe.symtab = current_subfile->symtab;
cb1df416
DJ
20981 }
20982 }
c906108c
SS
20983}
20984
20985/* Start a subfile for DWARF. FILENAME is the name of the file and
20986 DIRNAME the name of the source directory which contains FILENAME
4d663531 20987 or NULL if not known.
c906108c
SS
20988 This routine tries to keep line numbers from identical absolute and
20989 relative file names in a common subfile.
20990
20991 Using the `list' example from the GDB testsuite, which resides in
20992 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
20993 of /srcdir/list0.c yields the following debugging information for list0.c:
20994
c5aa993b 20995 DW_AT_name: /srcdir/list0.c
4d663531 20996 DW_AT_comp_dir: /compdir
357e46e7 20997 files.files[0].name: list0.h
c5aa993b 20998 files.files[0].dir: /srcdir
357e46e7 20999 files.files[1].name: list0.c
c5aa993b 21000 files.files[1].dir: /srcdir
c906108c
SS
21001
21002 The line number information for list0.c has to end up in a single
4f1520fb
FR
21003 subfile, so that `break /srcdir/list0.c:1' works as expected.
21004 start_subfile will ensure that this happens provided that we pass the
21005 concatenation of files.files[1].dir and files.files[1].name as the
21006 subfile's name. */
c906108c
SS
21007
21008static void
4d663531 21009dwarf2_start_subfile (const char *filename, const char *dirname)
c906108c 21010{
d521ce57 21011 char *copy = NULL;
4f1520fb 21012
4d663531 21013 /* In order not to lose the line information directory,
4f1520fb
FR
21014 we concatenate it to the filename when it makes sense.
21015 Note that the Dwarf3 standard says (speaking of filenames in line
21016 information): ``The directory index is ignored for file names
21017 that represent full path names''. Thus ignoring dirname in the
21018 `else' branch below isn't an issue. */
c906108c 21019
d5166ae1 21020 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
d521ce57
TT
21021 {
21022 copy = concat (dirname, SLASH_STRING, filename, (char *)NULL);
21023 filename = copy;
21024 }
c906108c 21025
4d663531 21026 start_subfile (filename);
4f1520fb 21027
d521ce57
TT
21028 if (copy != NULL)
21029 xfree (copy);
c906108c
SS
21030}
21031
f4dc4d17
DE
21032/* Start a symtab for DWARF.
21033 NAME, COMP_DIR, LOW_PC are passed to start_symtab. */
21034
43f3e411 21035static struct compunit_symtab *
f4dc4d17 21036dwarf2_start_symtab (struct dwarf2_cu *cu,
15d034d0 21037 const char *name, const char *comp_dir, CORE_ADDR low_pc)
f4dc4d17 21038{
43f3e411 21039 struct compunit_symtab *cust
5ffa0793 21040 = start_symtab (cu->objfile, name, comp_dir, low_pc, cu->language);
43f3e411 21041
f4dc4d17
DE
21042 record_debugformat ("DWARF 2");
21043 record_producer (cu->producer);
21044
21045 /* We assume that we're processing GCC output. */
21046 processing_gcc_compilation = 2;
21047
4d4ec4e5 21048 cu->processing_has_namespace_info = 0;
43f3e411
DE
21049
21050 return cust;
f4dc4d17
DE
21051}
21052
4c2df51b
DJ
21053static void
21054var_decode_location (struct attribute *attr, struct symbol *sym,
e7c27a73 21055 struct dwarf2_cu *cu)
4c2df51b 21056{
e7c27a73
DJ
21057 struct objfile *objfile = cu->objfile;
21058 struct comp_unit_head *cu_header = &cu->header;
21059
4c2df51b
DJ
21060 /* NOTE drow/2003-01-30: There used to be a comment and some special
21061 code here to turn a symbol with DW_AT_external and a
21062 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
21063 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
21064 with some versions of binutils) where shared libraries could have
21065 relocations against symbols in their debug information - the
21066 minimal symbol would have the right address, but the debug info
21067 would not. It's no longer necessary, because we will explicitly
21068 apply relocations when we read in the debug information now. */
21069
21070 /* A DW_AT_location attribute with no contents indicates that a
21071 variable has been optimized away. */
21072 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
21073 {
f1e6e072 21074 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
4c2df51b
DJ
21075 return;
21076 }
21077
21078 /* Handle one degenerate form of location expression specially, to
21079 preserve GDB's previous behavior when section offsets are
3019eac3
DE
21080 specified. If this is just a DW_OP_addr or DW_OP_GNU_addr_index
21081 then mark this symbol as LOC_STATIC. */
4c2df51b
DJ
21082
21083 if (attr_form_is_block (attr)
3019eac3
DE
21084 && ((DW_BLOCK (attr)->data[0] == DW_OP_addr
21085 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size)
21086 || (DW_BLOCK (attr)->data[0] == DW_OP_GNU_addr_index
21087 && (DW_BLOCK (attr)->size
21088 == 1 + leb128_size (&DW_BLOCK (attr)->data[1])))))
4c2df51b 21089 {
891d2f0b 21090 unsigned int dummy;
4c2df51b 21091
3019eac3
DE
21092 if (DW_BLOCK (attr)->data[0] == DW_OP_addr)
21093 SYMBOL_VALUE_ADDRESS (sym) =
21094 read_address (objfile->obfd, DW_BLOCK (attr)->data + 1, cu, &dummy);
21095 else
21096 SYMBOL_VALUE_ADDRESS (sym) =
21097 read_addr_index_from_leb128 (cu, DW_BLOCK (attr)->data + 1, &dummy);
f1e6e072 21098 SYMBOL_ACLASS_INDEX (sym) = LOC_STATIC;
4c2df51b
DJ
21099 fixup_symbol_section (sym, objfile);
21100 SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (objfile->section_offsets,
21101 SYMBOL_SECTION (sym));
4c2df51b
DJ
21102 return;
21103 }
21104
21105 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
21106 expression evaluator, and use LOC_COMPUTED only when necessary
21107 (i.e. when the value of a register or memory location is
21108 referenced, or a thread-local block, etc.). Then again, it might
21109 not be worthwhile. I'm assuming that it isn't unless performance
21110 or memory numbers show me otherwise. */
21111
f1e6e072 21112 dwarf2_symbol_mark_computed (attr, sym, cu, 0);
8be455d7 21113
f1e6e072 21114 if (SYMBOL_COMPUTED_OPS (sym)->location_has_loclist)
8be455d7 21115 cu->has_loclist = 1;
4c2df51b
DJ
21116}
21117
c906108c
SS
21118/* Given a pointer to a DWARF information entry, figure out if we need
21119 to make a symbol table entry for it, and if so, create a new entry
21120 and return a pointer to it.
21121 If TYPE is NULL, determine symbol type from the die, otherwise
34eaf542
TT
21122 used the passed type.
21123 If SPACE is not NULL, use it to hold the new symbol. If it is
21124 NULL, allocate a new symbol on the objfile's obstack. */
c906108c
SS
21125
21126static struct symbol *
34eaf542
TT
21127new_symbol_full (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
21128 struct symbol *space)
c906108c 21129{
e7c27a73 21130 struct objfile *objfile = cu->objfile;
3e29f34a 21131 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c 21132 struct symbol *sym = NULL;
15d034d0 21133 const char *name;
c906108c
SS
21134 struct attribute *attr = NULL;
21135 struct attribute *attr2 = NULL;
e142c38c 21136 CORE_ADDR baseaddr;
e37fd15a
SW
21137 struct pending **list_to_add = NULL;
21138
edb3359d 21139 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
e142c38c
DJ
21140
21141 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 21142
94af9270 21143 name = dwarf2_name (die, cu);
c906108c
SS
21144 if (name)
21145 {
94af9270 21146 const char *linkagename;
34eaf542 21147 int suppress_add = 0;
94af9270 21148
34eaf542
TT
21149 if (space)
21150 sym = space;
21151 else
e623cf5d 21152 sym = allocate_symbol (objfile);
c906108c 21153 OBJSTAT (objfile, n_syms++);
2de7ced7
DJ
21154
21155 /* Cache this symbol's name and the name's demangled form (if any). */
f85f34ed 21156 SYMBOL_SET_LANGUAGE (sym, cu->language, &objfile->objfile_obstack);
94af9270
KS
21157 linkagename = dwarf2_physname (name, die, cu);
21158 SYMBOL_SET_NAMES (sym, linkagename, strlen (linkagename), 0, objfile);
c906108c 21159
f55ee35c
JK
21160 /* Fortran does not have mangling standard and the mangling does differ
21161 between gfortran, iFort etc. */
21162 if (cu->language == language_fortran
b250c185 21163 && symbol_get_demangled_name (&(sym->ginfo)) == NULL)
29df156d 21164 symbol_set_demangled_name (&(sym->ginfo),
cfc594ee 21165 dwarf2_full_name (name, die, cu),
29df156d 21166 NULL);
f55ee35c 21167
c906108c 21168 /* Default assumptions.
c5aa993b 21169 Use the passed type or decode it from the die. */
176620f1 21170 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
f1e6e072 21171 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
c906108c
SS
21172 if (type != NULL)
21173 SYMBOL_TYPE (sym) = type;
21174 else
e7c27a73 21175 SYMBOL_TYPE (sym) = die_type (die, cu);
edb3359d
DJ
21176 attr = dwarf2_attr (die,
21177 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
21178 cu);
c906108c
SS
21179 if (attr)
21180 {
21181 SYMBOL_LINE (sym) = DW_UNSND (attr);
21182 }
cb1df416 21183
edb3359d
DJ
21184 attr = dwarf2_attr (die,
21185 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
21186 cu);
cb1df416
DJ
21187 if (attr)
21188 {
ecfb656c 21189 file_name_index file_index = (file_name_index) DW_UNSND (attr);
8c43009f 21190 struct file_entry *fe;
9a619af0 21191
ecfb656c
PA
21192 if (cu->line_header != NULL)
21193 fe = cu->line_header->file_name_at (file_index);
8c43009f
PA
21194 else
21195 fe = NULL;
21196
21197 if (fe == NULL)
cb1df416
DJ
21198 complaint (&symfile_complaints,
21199 _("file index out of range"));
8c43009f
PA
21200 else
21201 symbol_set_symtab (sym, fe->symtab);
cb1df416
DJ
21202 }
21203
c906108c
SS
21204 switch (die->tag)
21205 {
21206 case DW_TAG_label:
e142c38c 21207 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
c906108c 21208 if (attr)
3e29f34a
MR
21209 {
21210 CORE_ADDR addr;
21211
21212 addr = attr_value_as_address (attr);
21213 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr + baseaddr);
21214 SYMBOL_VALUE_ADDRESS (sym) = addr;
21215 }
0f5238ed
TT
21216 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
21217 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
f1e6e072 21218 SYMBOL_ACLASS_INDEX (sym) = LOC_LABEL;
0f5238ed 21219 add_symbol_to_list (sym, cu->list_in_scope);
c906108c
SS
21220 break;
21221 case DW_TAG_subprogram:
21222 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
21223 finish_block. */
f1e6e072 21224 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
e142c38c 21225 attr2 = dwarf2_attr (die, DW_AT_external, cu);
2cfa0c8d
JB
21226 if ((attr2 && (DW_UNSND (attr2) != 0))
21227 || cu->language == language_ada)
c906108c 21228 {
2cfa0c8d
JB
21229 /* Subprograms marked external are stored as a global symbol.
21230 Ada subprograms, whether marked external or not, are always
21231 stored as a global symbol, because we want to be able to
21232 access them globally. For instance, we want to be able
21233 to break on a nested subprogram without having to
21234 specify the context. */
e37fd15a 21235 list_to_add = &global_symbols;
c906108c
SS
21236 }
21237 else
21238 {
e37fd15a 21239 list_to_add = cu->list_in_scope;
c906108c
SS
21240 }
21241 break;
edb3359d
DJ
21242 case DW_TAG_inlined_subroutine:
21243 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
21244 finish_block. */
f1e6e072 21245 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
edb3359d 21246 SYMBOL_INLINED (sym) = 1;
481860b3 21247 list_to_add = cu->list_in_scope;
edb3359d 21248 break;
34eaf542
TT
21249 case DW_TAG_template_value_param:
21250 suppress_add = 1;
21251 /* Fall through. */
72929c62 21252 case DW_TAG_constant:
c906108c 21253 case DW_TAG_variable:
254e6b9e 21254 case DW_TAG_member:
0963b4bd
MS
21255 /* Compilation with minimal debug info may result in
21256 variables with missing type entries. Change the
21257 misleading `void' type to something sensible. */
c906108c 21258 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
46a4882b 21259 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_int;
64c50499 21260
e142c38c 21261 attr = dwarf2_attr (die, DW_AT_const_value, cu);
254e6b9e
DE
21262 /* In the case of DW_TAG_member, we should only be called for
21263 static const members. */
21264 if (die->tag == DW_TAG_member)
21265 {
3863f96c
DE
21266 /* dwarf2_add_field uses die_is_declaration,
21267 so we do the same. */
254e6b9e
DE
21268 gdb_assert (die_is_declaration (die, cu));
21269 gdb_assert (attr);
21270 }
c906108c
SS
21271 if (attr)
21272 {
e7c27a73 21273 dwarf2_const_value (attr, sym, cu);
e142c38c 21274 attr2 = dwarf2_attr (die, DW_AT_external, cu);
e37fd15a 21275 if (!suppress_add)
34eaf542
TT
21276 {
21277 if (attr2 && (DW_UNSND (attr2) != 0))
e37fd15a 21278 list_to_add = &global_symbols;
34eaf542 21279 else
e37fd15a 21280 list_to_add = cu->list_in_scope;
34eaf542 21281 }
c906108c
SS
21282 break;
21283 }
e142c38c 21284 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
21285 if (attr)
21286 {
e7c27a73 21287 var_decode_location (attr, sym, cu);
e142c38c 21288 attr2 = dwarf2_attr (die, DW_AT_external, cu);
4357ac6c
TT
21289
21290 /* Fortran explicitly imports any global symbols to the local
21291 scope by DW_TAG_common_block. */
21292 if (cu->language == language_fortran && die->parent
21293 && die->parent->tag == DW_TAG_common_block)
21294 attr2 = NULL;
21295
caac4577
JG
21296 if (SYMBOL_CLASS (sym) == LOC_STATIC
21297 && SYMBOL_VALUE_ADDRESS (sym) == 0
21298 && !dwarf2_per_objfile->has_section_at_zero)
21299 {
21300 /* When a static variable is eliminated by the linker,
21301 the corresponding debug information is not stripped
21302 out, but the variable address is set to null;
21303 do not add such variables into symbol table. */
21304 }
21305 else if (attr2 && (DW_UNSND (attr2) != 0))
1c809c68 21306 {
f55ee35c
JK
21307 /* Workaround gfortran PR debug/40040 - it uses
21308 DW_AT_location for variables in -fPIC libraries which may
21309 get overriden by other libraries/executable and get
21310 a different address. Resolve it by the minimal symbol
21311 which may come from inferior's executable using copy
21312 relocation. Make this workaround only for gfortran as for
21313 other compilers GDB cannot guess the minimal symbol
21314 Fortran mangling kind. */
21315 if (cu->language == language_fortran && die->parent
21316 && die->parent->tag == DW_TAG_module
21317 && cu->producer
28586665 21318 && startswith (cu->producer, "GNU Fortran"))
f1e6e072 21319 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
f55ee35c 21320
1c809c68
TT
21321 /* A variable with DW_AT_external is never static,
21322 but it may be block-scoped. */
21323 list_to_add = (cu->list_in_scope == &file_symbols
21324 ? &global_symbols : cu->list_in_scope);
1c809c68 21325 }
c906108c 21326 else
e37fd15a 21327 list_to_add = cu->list_in_scope;
c906108c
SS
21328 }
21329 else
21330 {
21331 /* We do not know the address of this symbol.
c5aa993b
JM
21332 If it is an external symbol and we have type information
21333 for it, enter the symbol as a LOC_UNRESOLVED symbol.
21334 The address of the variable will then be determined from
21335 the minimal symbol table whenever the variable is
21336 referenced. */
e142c38c 21337 attr2 = dwarf2_attr (die, DW_AT_external, cu);
0971de02
TT
21338
21339 /* Fortran explicitly imports any global symbols to the local
21340 scope by DW_TAG_common_block. */
21341 if (cu->language == language_fortran && die->parent
21342 && die->parent->tag == DW_TAG_common_block)
21343 {
21344 /* SYMBOL_CLASS doesn't matter here because
21345 read_common_block is going to reset it. */
21346 if (!suppress_add)
21347 list_to_add = cu->list_in_scope;
21348 }
21349 else if (attr2 && (DW_UNSND (attr2) != 0)
21350 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
c906108c 21351 {
0fe7935b
DJ
21352 /* A variable with DW_AT_external is never static, but it
21353 may be block-scoped. */
21354 list_to_add = (cu->list_in_scope == &file_symbols
21355 ? &global_symbols : cu->list_in_scope);
21356
f1e6e072 21357 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
c906108c 21358 }
442ddf59
JK
21359 else if (!die_is_declaration (die, cu))
21360 {
21361 /* Use the default LOC_OPTIMIZED_OUT class. */
21362 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
e37fd15a
SW
21363 if (!suppress_add)
21364 list_to_add = cu->list_in_scope;
442ddf59 21365 }
c906108c
SS
21366 }
21367 break;
21368 case DW_TAG_formal_parameter:
edb3359d
DJ
21369 /* If we are inside a function, mark this as an argument. If
21370 not, we might be looking at an argument to an inlined function
21371 when we do not have enough information to show inlined frames;
21372 pretend it's a local variable in that case so that the user can
21373 still see it. */
21374 if (context_stack_depth > 0
21375 && context_stack[context_stack_depth - 1].name != NULL)
21376 SYMBOL_IS_ARGUMENT (sym) = 1;
e142c38c 21377 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
21378 if (attr)
21379 {
e7c27a73 21380 var_decode_location (attr, sym, cu);
c906108c 21381 }
e142c38c 21382 attr = dwarf2_attr (die, DW_AT_const_value, cu);
c906108c
SS
21383 if (attr)
21384 {
e7c27a73 21385 dwarf2_const_value (attr, sym, cu);
c906108c 21386 }
f346a30d 21387
e37fd15a 21388 list_to_add = cu->list_in_scope;
c906108c
SS
21389 break;
21390 case DW_TAG_unspecified_parameters:
21391 /* From varargs functions; gdb doesn't seem to have any
21392 interest in this information, so just ignore it for now.
21393 (FIXME?) */
21394 break;
34eaf542
TT
21395 case DW_TAG_template_type_param:
21396 suppress_add = 1;
21397 /* Fall through. */
c906108c 21398 case DW_TAG_class_type:
680b30c7 21399 case DW_TAG_interface_type:
c906108c
SS
21400 case DW_TAG_structure_type:
21401 case DW_TAG_union_type:
72019c9c 21402 case DW_TAG_set_type:
c906108c 21403 case DW_TAG_enumeration_type:
f1e6e072 21404 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
176620f1 21405 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
c906108c 21406
63d06c5c 21407 {
9c37b5ae 21408 /* NOTE: carlton/2003-11-10: C++ class symbols shouldn't
63d06c5c
DC
21409 really ever be static objects: otherwise, if you try
21410 to, say, break of a class's method and you're in a file
21411 which doesn't mention that class, it won't work unless
21412 the check for all static symbols in lookup_symbol_aux
21413 saves you. See the OtherFileClass tests in
21414 gdb.c++/namespace.exp. */
21415
e37fd15a 21416 if (!suppress_add)
34eaf542 21417 {
34eaf542 21418 list_to_add = (cu->list_in_scope == &file_symbols
9c37b5ae 21419 && cu->language == language_cplus
34eaf542 21420 ? &global_symbols : cu->list_in_scope);
63d06c5c 21421
64382290 21422 /* The semantics of C++ state that "struct foo {
9c37b5ae 21423 ... }" also defines a typedef for "foo". */
64382290 21424 if (cu->language == language_cplus
45280282 21425 || cu->language == language_ada
c44af4eb
TT
21426 || cu->language == language_d
21427 || cu->language == language_rust)
64382290
TT
21428 {
21429 /* The symbol's name is already allocated along
21430 with this objfile, so we don't need to
21431 duplicate it for the type. */
21432 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
21433 TYPE_NAME (SYMBOL_TYPE (sym)) = SYMBOL_SEARCH_NAME (sym);
21434 }
63d06c5c
DC
21435 }
21436 }
c906108c
SS
21437 break;
21438 case DW_TAG_typedef:
f1e6e072 21439 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
63d06c5c 21440 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 21441 list_to_add = cu->list_in_scope;
63d06c5c 21442 break;
c906108c 21443 case DW_TAG_base_type:
a02abb62 21444 case DW_TAG_subrange_type:
f1e6e072 21445 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
176620f1 21446 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 21447 list_to_add = cu->list_in_scope;
c906108c
SS
21448 break;
21449 case DW_TAG_enumerator:
e142c38c 21450 attr = dwarf2_attr (die, DW_AT_const_value, cu);
c906108c
SS
21451 if (attr)
21452 {
e7c27a73 21453 dwarf2_const_value (attr, sym, cu);
c906108c 21454 }
63d06c5c
DC
21455 {
21456 /* NOTE: carlton/2003-11-10: See comment above in the
21457 DW_TAG_class_type, etc. block. */
21458
e142c38c 21459 list_to_add = (cu->list_in_scope == &file_symbols
9c37b5ae 21460 && cu->language == language_cplus
e142c38c 21461 ? &global_symbols : cu->list_in_scope);
63d06c5c 21462 }
c906108c 21463 break;
74921315 21464 case DW_TAG_imported_declaration:
5c4e30ca 21465 case DW_TAG_namespace:
f1e6e072 21466 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
e37fd15a 21467 list_to_add = &global_symbols;
5c4e30ca 21468 break;
530e8392
KB
21469 case DW_TAG_module:
21470 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21471 SYMBOL_DOMAIN (sym) = MODULE_DOMAIN;
21472 list_to_add = &global_symbols;
21473 break;
4357ac6c 21474 case DW_TAG_common_block:
f1e6e072 21475 SYMBOL_ACLASS_INDEX (sym) = LOC_COMMON_BLOCK;
4357ac6c
TT
21476 SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
21477 add_symbol_to_list (sym, cu->list_in_scope);
21478 break;
c906108c
SS
21479 default:
21480 /* Not a tag we recognize. Hopefully we aren't processing
21481 trash data, but since we must specifically ignore things
21482 we don't recognize, there is nothing else we should do at
0963b4bd 21483 this point. */
e2e0b3e5 21484 complaint (&symfile_complaints, _("unsupported tag: '%s'"),
4d3c2250 21485 dwarf_tag_name (die->tag));
c906108c
SS
21486 break;
21487 }
df8a16a1 21488
e37fd15a
SW
21489 if (suppress_add)
21490 {
21491 sym->hash_next = objfile->template_symbols;
21492 objfile->template_symbols = sym;
21493 list_to_add = NULL;
21494 }
21495
21496 if (list_to_add != NULL)
21497 add_symbol_to_list (sym, list_to_add);
21498
df8a16a1
DJ
21499 /* For the benefit of old versions of GCC, check for anonymous
21500 namespaces based on the demangled name. */
4d4ec4e5 21501 if (!cu->processing_has_namespace_info
94af9270 21502 && cu->language == language_cplus)
a10964d1 21503 cp_scan_for_anonymous_namespaces (sym, objfile);
c906108c
SS
21504 }
21505 return (sym);
21506}
21507
34eaf542
TT
21508/* A wrapper for new_symbol_full that always allocates a new symbol. */
21509
21510static struct symbol *
21511new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
21512{
21513 return new_symbol_full (die, type, cu, NULL);
21514}
21515
98bfdba5
PA
21516/* Given an attr with a DW_FORM_dataN value in host byte order,
21517 zero-extend it as appropriate for the symbol's type. The DWARF
21518 standard (v4) is not entirely clear about the meaning of using
21519 DW_FORM_dataN for a constant with a signed type, where the type is
21520 wider than the data. The conclusion of a discussion on the DWARF
21521 list was that this is unspecified. We choose to always zero-extend
21522 because that is the interpretation long in use by GCC. */
c906108c 21523
98bfdba5 21524static gdb_byte *
ff39bb5e 21525dwarf2_const_value_data (const struct attribute *attr, struct obstack *obstack,
12df843f 21526 struct dwarf2_cu *cu, LONGEST *value, int bits)
c906108c 21527{
e7c27a73 21528 struct objfile *objfile = cu->objfile;
e17a4113
UW
21529 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
21530 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
98bfdba5
PA
21531 LONGEST l = DW_UNSND (attr);
21532
21533 if (bits < sizeof (*value) * 8)
21534 {
21535 l &= ((LONGEST) 1 << bits) - 1;
21536 *value = l;
21537 }
21538 else if (bits == sizeof (*value) * 8)
21539 *value = l;
21540 else
21541 {
224c3ddb 21542 gdb_byte *bytes = (gdb_byte *) obstack_alloc (obstack, bits / 8);
98bfdba5
PA
21543 store_unsigned_integer (bytes, bits / 8, byte_order, l);
21544 return bytes;
21545 }
21546
21547 return NULL;
21548}
21549
21550/* Read a constant value from an attribute. Either set *VALUE, or if
21551 the value does not fit in *VALUE, set *BYTES - either already
21552 allocated on the objfile obstack, or newly allocated on OBSTACK,
21553 or, set *BATON, if we translated the constant to a location
21554 expression. */
21555
21556static void
ff39bb5e 21557dwarf2_const_value_attr (const struct attribute *attr, struct type *type,
98bfdba5
PA
21558 const char *name, struct obstack *obstack,
21559 struct dwarf2_cu *cu,
d521ce57 21560 LONGEST *value, const gdb_byte **bytes,
98bfdba5
PA
21561 struct dwarf2_locexpr_baton **baton)
21562{
21563 struct objfile *objfile = cu->objfile;
21564 struct comp_unit_head *cu_header = &cu->header;
c906108c 21565 struct dwarf_block *blk;
98bfdba5
PA
21566 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
21567 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
21568
21569 *value = 0;
21570 *bytes = NULL;
21571 *baton = NULL;
c906108c
SS
21572
21573 switch (attr->form)
21574 {
21575 case DW_FORM_addr:
3019eac3 21576 case DW_FORM_GNU_addr_index:
ac56253d 21577 {
ac56253d
TT
21578 gdb_byte *data;
21579
98bfdba5
PA
21580 if (TYPE_LENGTH (type) != cu_header->addr_size)
21581 dwarf2_const_value_length_mismatch_complaint (name,
ac56253d 21582 cu_header->addr_size,
98bfdba5 21583 TYPE_LENGTH (type));
ac56253d
TT
21584 /* Symbols of this form are reasonably rare, so we just
21585 piggyback on the existing location code rather than writing
21586 a new implementation of symbol_computed_ops. */
8d749320 21587 *baton = XOBNEW (obstack, struct dwarf2_locexpr_baton);
98bfdba5
PA
21588 (*baton)->per_cu = cu->per_cu;
21589 gdb_assert ((*baton)->per_cu);
ac56253d 21590
98bfdba5 21591 (*baton)->size = 2 + cu_header->addr_size;
224c3ddb 21592 data = (gdb_byte *) obstack_alloc (obstack, (*baton)->size);
98bfdba5 21593 (*baton)->data = data;
ac56253d
TT
21594
21595 data[0] = DW_OP_addr;
21596 store_unsigned_integer (&data[1], cu_header->addr_size,
21597 byte_order, DW_ADDR (attr));
21598 data[cu_header->addr_size + 1] = DW_OP_stack_value;
ac56253d 21599 }
c906108c 21600 break;
4ac36638 21601 case DW_FORM_string:
93b5768b 21602 case DW_FORM_strp:
3019eac3 21603 case DW_FORM_GNU_str_index:
36586728 21604 case DW_FORM_GNU_strp_alt:
98bfdba5
PA
21605 /* DW_STRING is already allocated on the objfile obstack, point
21606 directly to it. */
d521ce57 21607 *bytes = (const gdb_byte *) DW_STRING (attr);
93b5768b 21608 break;
c906108c
SS
21609 case DW_FORM_block1:
21610 case DW_FORM_block2:
21611 case DW_FORM_block4:
21612 case DW_FORM_block:
2dc7f7b3 21613 case DW_FORM_exprloc:
0224619f 21614 case DW_FORM_data16:
c906108c 21615 blk = DW_BLOCK (attr);
98bfdba5
PA
21616 if (TYPE_LENGTH (type) != blk->size)
21617 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
21618 TYPE_LENGTH (type));
21619 *bytes = blk->data;
c906108c 21620 break;
2df3850c
JM
21621
21622 /* The DW_AT_const_value attributes are supposed to carry the
21623 symbol's value "represented as it would be on the target
21624 architecture." By the time we get here, it's already been
21625 converted to host endianness, so we just need to sign- or
21626 zero-extend it as appropriate. */
21627 case DW_FORM_data1:
3aef2284 21628 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 8);
2df3850c 21629 break;
c906108c 21630 case DW_FORM_data2:
3aef2284 21631 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 16);
2df3850c 21632 break;
c906108c 21633 case DW_FORM_data4:
3aef2284 21634 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 32);
2df3850c 21635 break;
c906108c 21636 case DW_FORM_data8:
3aef2284 21637 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 64);
2df3850c
JM
21638 break;
21639
c906108c 21640 case DW_FORM_sdata:
663c44ac 21641 case DW_FORM_implicit_const:
98bfdba5 21642 *value = DW_SND (attr);
2df3850c
JM
21643 break;
21644
c906108c 21645 case DW_FORM_udata:
98bfdba5 21646 *value = DW_UNSND (attr);
c906108c 21647 break;
2df3850c 21648
c906108c 21649 default:
4d3c2250 21650 complaint (&symfile_complaints,
e2e0b3e5 21651 _("unsupported const value attribute form: '%s'"),
4d3c2250 21652 dwarf_form_name (attr->form));
98bfdba5 21653 *value = 0;
c906108c
SS
21654 break;
21655 }
21656}
21657
2df3850c 21658
98bfdba5
PA
21659/* Copy constant value from an attribute to a symbol. */
21660
2df3850c 21661static void
ff39bb5e 21662dwarf2_const_value (const struct attribute *attr, struct symbol *sym,
98bfdba5 21663 struct dwarf2_cu *cu)
2df3850c 21664{
98bfdba5 21665 struct objfile *objfile = cu->objfile;
12df843f 21666 LONGEST value;
d521ce57 21667 const gdb_byte *bytes;
98bfdba5 21668 struct dwarf2_locexpr_baton *baton;
2df3850c 21669
98bfdba5
PA
21670 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
21671 SYMBOL_PRINT_NAME (sym),
21672 &objfile->objfile_obstack, cu,
21673 &value, &bytes, &baton);
2df3850c 21674
98bfdba5
PA
21675 if (baton != NULL)
21676 {
98bfdba5 21677 SYMBOL_LOCATION_BATON (sym) = baton;
f1e6e072 21678 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
98bfdba5
PA
21679 }
21680 else if (bytes != NULL)
21681 {
21682 SYMBOL_VALUE_BYTES (sym) = bytes;
f1e6e072 21683 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST_BYTES;
98bfdba5
PA
21684 }
21685 else
21686 {
21687 SYMBOL_VALUE (sym) = value;
f1e6e072 21688 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST;
98bfdba5 21689 }
2df3850c
JM
21690}
21691
c906108c
SS
21692/* Return the type of the die in question using its DW_AT_type attribute. */
21693
21694static struct type *
e7c27a73 21695die_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 21696{
c906108c 21697 struct attribute *type_attr;
c906108c 21698
e142c38c 21699 type_attr = dwarf2_attr (die, DW_AT_type, cu);
c906108c
SS
21700 if (!type_attr)
21701 {
21702 /* A missing DW_AT_type represents a void type. */
46bf5051 21703 return objfile_type (cu->objfile)->builtin_void;
c906108c 21704 }
348e048f 21705
673bfd45 21706 return lookup_die_type (die, type_attr, cu);
c906108c
SS
21707}
21708
b4ba55a1
JB
21709/* True iff CU's producer generates GNAT Ada auxiliary information
21710 that allows to find parallel types through that information instead
21711 of having to do expensive parallel lookups by type name. */
21712
21713static int
21714need_gnat_info (struct dwarf2_cu *cu)
21715{
21716 /* FIXME: brobecker/2010-10-12: As of now, only the AdaCore version
21717 of GNAT produces this auxiliary information, without any indication
21718 that it is produced. Part of enhancing the FSF version of GNAT
21719 to produce that information will be to put in place an indicator
21720 that we can use in order to determine whether the descriptive type
21721 info is available or not. One suggestion that has been made is
21722 to use a new attribute, attached to the CU die. For now, assume
21723 that the descriptive type info is not available. */
21724 return 0;
21725}
21726
b4ba55a1
JB
21727/* Return the auxiliary type of the die in question using its
21728 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
21729 attribute is not present. */
21730
21731static struct type *
21732die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
21733{
b4ba55a1 21734 struct attribute *type_attr;
b4ba55a1
JB
21735
21736 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
21737 if (!type_attr)
21738 return NULL;
21739
673bfd45 21740 return lookup_die_type (die, type_attr, cu);
b4ba55a1
JB
21741}
21742
21743/* If DIE has a descriptive_type attribute, then set the TYPE's
21744 descriptive type accordingly. */
21745
21746static void
21747set_descriptive_type (struct type *type, struct die_info *die,
21748 struct dwarf2_cu *cu)
21749{
21750 struct type *descriptive_type = die_descriptive_type (die, cu);
21751
21752 if (descriptive_type)
21753 {
21754 ALLOCATE_GNAT_AUX_TYPE (type);
21755 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
21756 }
21757}
21758
c906108c
SS
21759/* Return the containing type of the die in question using its
21760 DW_AT_containing_type attribute. */
21761
21762static struct type *
e7c27a73 21763die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 21764{
c906108c 21765 struct attribute *type_attr;
c906108c 21766
e142c38c 21767 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
33ac96f0
JK
21768 if (!type_attr)
21769 error (_("Dwarf Error: Problem turning containing type into gdb type "
4262abfb 21770 "[in module %s]"), objfile_name (cu->objfile));
33ac96f0 21771
673bfd45 21772 return lookup_die_type (die, type_attr, cu);
c906108c
SS
21773}
21774
ac9ec31b
DE
21775/* Return an error marker type to use for the ill formed type in DIE/CU. */
21776
21777static struct type *
21778build_error_marker_type (struct dwarf2_cu *cu, struct die_info *die)
21779{
21780 struct objfile *objfile = dwarf2_per_objfile->objfile;
21781 char *message, *saved;
21782
21783 message = xstrprintf (_("<unknown type in %s, CU 0x%x, DIE 0x%x>"),
4262abfb 21784 objfile_name (objfile),
9c541725
PA
21785 to_underlying (cu->header.sect_off),
21786 to_underlying (die->sect_off));
224c3ddb
SM
21787 saved = (char *) obstack_copy0 (&objfile->objfile_obstack,
21788 message, strlen (message));
ac9ec31b
DE
21789 xfree (message);
21790
19f392bc 21791 return init_type (objfile, TYPE_CODE_ERROR, 0, saved);
ac9ec31b
DE
21792}
21793
673bfd45 21794/* Look up the type of DIE in CU using its type attribute ATTR.
ac9ec31b
DE
21795 ATTR must be one of: DW_AT_type, DW_AT_GNAT_descriptive_type,
21796 DW_AT_containing_type.
673bfd45
DE
21797 If there is no type substitute an error marker. */
21798
c906108c 21799static struct type *
ff39bb5e 21800lookup_die_type (struct die_info *die, const struct attribute *attr,
673bfd45 21801 struct dwarf2_cu *cu)
c906108c 21802{
bb5ed363 21803 struct objfile *objfile = cu->objfile;
f792889a
DJ
21804 struct type *this_type;
21805
ac9ec31b
DE
21806 gdb_assert (attr->name == DW_AT_type
21807 || attr->name == DW_AT_GNAT_descriptive_type
21808 || attr->name == DW_AT_containing_type);
21809
673bfd45
DE
21810 /* First see if we have it cached. */
21811
36586728
TT
21812 if (attr->form == DW_FORM_GNU_ref_alt)
21813 {
21814 struct dwarf2_per_cu_data *per_cu;
9c541725 21815 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
36586728 21816
9c541725
PA
21817 per_cu = dwarf2_find_containing_comp_unit (sect_off, 1, cu->objfile);
21818 this_type = get_die_type_at_offset (sect_off, per_cu);
36586728 21819 }
7771576e 21820 else if (attr_form_is_ref (attr))
673bfd45 21821 {
9c541725 21822 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
673bfd45 21823
9c541725 21824 this_type = get_die_type_at_offset (sect_off, cu->per_cu);
673bfd45 21825 }
55f1336d 21826 else if (attr->form == DW_FORM_ref_sig8)
673bfd45 21827 {
ac9ec31b 21828 ULONGEST signature = DW_SIGNATURE (attr);
673bfd45 21829
ac9ec31b 21830 return get_signatured_type (die, signature, cu);
673bfd45
DE
21831 }
21832 else
21833 {
ac9ec31b
DE
21834 complaint (&symfile_complaints,
21835 _("Dwarf Error: Bad type attribute %s in DIE"
21836 " at 0x%x [in module %s]"),
9c541725 21837 dwarf_attr_name (attr->name), to_underlying (die->sect_off),
4262abfb 21838 objfile_name (objfile));
ac9ec31b 21839 return build_error_marker_type (cu, die);
673bfd45
DE
21840 }
21841
21842 /* If not cached we need to read it in. */
21843
21844 if (this_type == NULL)
21845 {
ac9ec31b 21846 struct die_info *type_die = NULL;
673bfd45
DE
21847 struct dwarf2_cu *type_cu = cu;
21848
7771576e 21849 if (attr_form_is_ref (attr))
ac9ec31b
DE
21850 type_die = follow_die_ref (die, attr, &type_cu);
21851 if (type_die == NULL)
21852 return build_error_marker_type (cu, die);
21853 /* If we find the type now, it's probably because the type came
3019eac3
DE
21854 from an inter-CU reference and the type's CU got expanded before
21855 ours. */
ac9ec31b 21856 this_type = read_type_die (type_die, type_cu);
673bfd45
DE
21857 }
21858
21859 /* If we still don't have a type use an error marker. */
21860
21861 if (this_type == NULL)
ac9ec31b 21862 return build_error_marker_type (cu, die);
673bfd45 21863
f792889a 21864 return this_type;
c906108c
SS
21865}
21866
673bfd45
DE
21867/* Return the type in DIE, CU.
21868 Returns NULL for invalid types.
21869
02142a6c 21870 This first does a lookup in die_type_hash,
673bfd45
DE
21871 and only reads the die in if necessary.
21872
21873 NOTE: This can be called when reading in partial or full symbols. */
21874
f792889a 21875static struct type *
e7c27a73 21876read_type_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 21877{
f792889a
DJ
21878 struct type *this_type;
21879
21880 this_type = get_die_type (die, cu);
21881 if (this_type)
21882 return this_type;
21883
673bfd45
DE
21884 return read_type_die_1 (die, cu);
21885}
21886
21887/* Read the type in DIE, CU.
21888 Returns NULL for invalid types. */
21889
21890static struct type *
21891read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
21892{
21893 struct type *this_type = NULL;
21894
c906108c
SS
21895 switch (die->tag)
21896 {
21897 case DW_TAG_class_type:
680b30c7 21898 case DW_TAG_interface_type:
c906108c
SS
21899 case DW_TAG_structure_type:
21900 case DW_TAG_union_type:
f792889a 21901 this_type = read_structure_type (die, cu);
c906108c
SS
21902 break;
21903 case DW_TAG_enumeration_type:
f792889a 21904 this_type = read_enumeration_type (die, cu);
c906108c
SS
21905 break;
21906 case DW_TAG_subprogram:
21907 case DW_TAG_subroutine_type:
edb3359d 21908 case DW_TAG_inlined_subroutine:
f792889a 21909 this_type = read_subroutine_type (die, cu);
c906108c
SS
21910 break;
21911 case DW_TAG_array_type:
f792889a 21912 this_type = read_array_type (die, cu);
c906108c 21913 break;
72019c9c 21914 case DW_TAG_set_type:
f792889a 21915 this_type = read_set_type (die, cu);
72019c9c 21916 break;
c906108c 21917 case DW_TAG_pointer_type:
f792889a 21918 this_type = read_tag_pointer_type (die, cu);
c906108c
SS
21919 break;
21920 case DW_TAG_ptr_to_member_type:
f792889a 21921 this_type = read_tag_ptr_to_member_type (die, cu);
c906108c
SS
21922 break;
21923 case DW_TAG_reference_type:
4297a3f0
AV
21924 this_type = read_tag_reference_type (die, cu, TYPE_CODE_REF);
21925 break;
21926 case DW_TAG_rvalue_reference_type:
21927 this_type = read_tag_reference_type (die, cu, TYPE_CODE_RVALUE_REF);
c906108c
SS
21928 break;
21929 case DW_TAG_const_type:
f792889a 21930 this_type = read_tag_const_type (die, cu);
c906108c
SS
21931 break;
21932 case DW_TAG_volatile_type:
f792889a 21933 this_type = read_tag_volatile_type (die, cu);
c906108c 21934 break;
06d66ee9
TT
21935 case DW_TAG_restrict_type:
21936 this_type = read_tag_restrict_type (die, cu);
21937 break;
c906108c 21938 case DW_TAG_string_type:
f792889a 21939 this_type = read_tag_string_type (die, cu);
c906108c
SS
21940 break;
21941 case DW_TAG_typedef:
f792889a 21942 this_type = read_typedef (die, cu);
c906108c 21943 break;
a02abb62 21944 case DW_TAG_subrange_type:
f792889a 21945 this_type = read_subrange_type (die, cu);
a02abb62 21946 break;
c906108c 21947 case DW_TAG_base_type:
f792889a 21948 this_type = read_base_type (die, cu);
c906108c 21949 break;
81a17f79 21950 case DW_TAG_unspecified_type:
f792889a 21951 this_type = read_unspecified_type (die, cu);
81a17f79 21952 break;
0114d602
DJ
21953 case DW_TAG_namespace:
21954 this_type = read_namespace_type (die, cu);
21955 break;
f55ee35c
JK
21956 case DW_TAG_module:
21957 this_type = read_module_type (die, cu);
21958 break;
a2c2acaf
MW
21959 case DW_TAG_atomic_type:
21960 this_type = read_tag_atomic_type (die, cu);
21961 break;
c906108c 21962 default:
3e43a32a
MS
21963 complaint (&symfile_complaints,
21964 _("unexpected tag in read_type_die: '%s'"),
4d3c2250 21965 dwarf_tag_name (die->tag));
c906108c
SS
21966 break;
21967 }
63d06c5c 21968
f792889a 21969 return this_type;
63d06c5c
DC
21970}
21971
abc72ce4
DE
21972/* See if we can figure out if the class lives in a namespace. We do
21973 this by looking for a member function; its demangled name will
21974 contain namespace info, if there is any.
21975 Return the computed name or NULL.
21976 Space for the result is allocated on the objfile's obstack.
21977 This is the full-die version of guess_partial_die_structure_name.
21978 In this case we know DIE has no useful parent. */
21979
21980static char *
21981guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
21982{
21983 struct die_info *spec_die;
21984 struct dwarf2_cu *spec_cu;
21985 struct die_info *child;
21986
21987 spec_cu = cu;
21988 spec_die = die_specification (die, &spec_cu);
21989 if (spec_die != NULL)
21990 {
21991 die = spec_die;
21992 cu = spec_cu;
21993 }
21994
21995 for (child = die->child;
21996 child != NULL;
21997 child = child->sibling)
21998 {
21999 if (child->tag == DW_TAG_subprogram)
22000 {
73b9be8b 22001 const char *linkage_name = dw2_linkage_name (child, cu);
abc72ce4 22002
7d45c7c3 22003 if (linkage_name != NULL)
abc72ce4
DE
22004 {
22005 char *actual_name
22006 = language_class_name_from_physname (cu->language_defn,
7d45c7c3 22007 linkage_name);
abc72ce4
DE
22008 char *name = NULL;
22009
22010 if (actual_name != NULL)
22011 {
15d034d0 22012 const char *die_name = dwarf2_name (die, cu);
abc72ce4
DE
22013
22014 if (die_name != NULL
22015 && strcmp (die_name, actual_name) != 0)
22016 {
22017 /* Strip off the class name from the full name.
22018 We want the prefix. */
22019 int die_name_len = strlen (die_name);
22020 int actual_name_len = strlen (actual_name);
22021
22022 /* Test for '::' as a sanity check. */
22023 if (actual_name_len > die_name_len + 2
3e43a32a
MS
22024 && actual_name[actual_name_len
22025 - die_name_len - 1] == ':')
224c3ddb
SM
22026 name = (char *) obstack_copy0 (
22027 &cu->objfile->per_bfd->storage_obstack,
22028 actual_name, actual_name_len - die_name_len - 2);
abc72ce4
DE
22029 }
22030 }
22031 xfree (actual_name);
22032 return name;
22033 }
22034 }
22035 }
22036
22037 return NULL;
22038}
22039
96408a79
SA
22040/* GCC might emit a nameless typedef that has a linkage name. Determine the
22041 prefix part in such case. See
22042 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
22043
a121b7c1 22044static const char *
96408a79
SA
22045anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
22046{
22047 struct attribute *attr;
e6a959d6 22048 const char *base;
96408a79
SA
22049
22050 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
22051 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
22052 return NULL;
22053
7d45c7c3 22054 if (dwarf2_string_attr (die, DW_AT_name, cu) != NULL)
96408a79
SA
22055 return NULL;
22056
73b9be8b 22057 attr = dw2_linkage_name_attr (die, cu);
96408a79
SA
22058 if (attr == NULL || DW_STRING (attr) == NULL)
22059 return NULL;
22060
22061 /* dwarf2_name had to be already called. */
22062 gdb_assert (DW_STRING_IS_CANONICAL (attr));
22063
22064 /* Strip the base name, keep any leading namespaces/classes. */
22065 base = strrchr (DW_STRING (attr), ':');
22066 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
22067 return "";
22068
224c3ddb
SM
22069 return (char *) obstack_copy0 (&cu->objfile->per_bfd->storage_obstack,
22070 DW_STRING (attr),
22071 &base[-1] - DW_STRING (attr));
96408a79
SA
22072}
22073
fdde2d81 22074/* Return the name of the namespace/class that DIE is defined within,
0114d602 22075 or "" if we can't tell. The caller should not xfree the result.
fdde2d81 22076
0114d602
DJ
22077 For example, if we're within the method foo() in the following
22078 code:
22079
22080 namespace N {
22081 class C {
22082 void foo () {
22083 }
22084 };
22085 }
22086
22087 then determine_prefix on foo's die will return "N::C". */
fdde2d81 22088
0d5cff50 22089static const char *
e142c38c 22090determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
63d06c5c 22091{
0114d602
DJ
22092 struct die_info *parent, *spec_die;
22093 struct dwarf2_cu *spec_cu;
22094 struct type *parent_type;
a121b7c1 22095 const char *retval;
63d06c5c 22096
9c37b5ae 22097 if (cu->language != language_cplus
c44af4eb
TT
22098 && cu->language != language_fortran && cu->language != language_d
22099 && cu->language != language_rust)
0114d602
DJ
22100 return "";
22101
96408a79
SA
22102 retval = anonymous_struct_prefix (die, cu);
22103 if (retval)
22104 return retval;
22105
0114d602
DJ
22106 /* We have to be careful in the presence of DW_AT_specification.
22107 For example, with GCC 3.4, given the code
22108
22109 namespace N {
22110 void foo() {
22111 // Definition of N::foo.
22112 }
22113 }
22114
22115 then we'll have a tree of DIEs like this:
22116
22117 1: DW_TAG_compile_unit
22118 2: DW_TAG_namespace // N
22119 3: DW_TAG_subprogram // declaration of N::foo
22120 4: DW_TAG_subprogram // definition of N::foo
22121 DW_AT_specification // refers to die #3
22122
22123 Thus, when processing die #4, we have to pretend that we're in
22124 the context of its DW_AT_specification, namely the contex of die
22125 #3. */
22126 spec_cu = cu;
22127 spec_die = die_specification (die, &spec_cu);
22128 if (spec_die == NULL)
22129 parent = die->parent;
22130 else
63d06c5c 22131 {
0114d602
DJ
22132 parent = spec_die->parent;
22133 cu = spec_cu;
63d06c5c 22134 }
0114d602
DJ
22135
22136 if (parent == NULL)
22137 return "";
98bfdba5
PA
22138 else if (parent->building_fullname)
22139 {
22140 const char *name;
22141 const char *parent_name;
22142
22143 /* It has been seen on RealView 2.2 built binaries,
22144 DW_TAG_template_type_param types actually _defined_ as
22145 children of the parent class:
22146
22147 enum E {};
22148 template class <class Enum> Class{};
22149 Class<enum E> class_e;
22150
22151 1: DW_TAG_class_type (Class)
22152 2: DW_TAG_enumeration_type (E)
22153 3: DW_TAG_enumerator (enum1:0)
22154 3: DW_TAG_enumerator (enum2:1)
22155 ...
22156 2: DW_TAG_template_type_param
22157 DW_AT_type DW_FORM_ref_udata (E)
22158
22159 Besides being broken debug info, it can put GDB into an
22160 infinite loop. Consider:
22161
22162 When we're building the full name for Class<E>, we'll start
22163 at Class, and go look over its template type parameters,
22164 finding E. We'll then try to build the full name of E, and
22165 reach here. We're now trying to build the full name of E,
22166 and look over the parent DIE for containing scope. In the
22167 broken case, if we followed the parent DIE of E, we'd again
22168 find Class, and once again go look at its template type
22169 arguments, etc., etc. Simply don't consider such parent die
22170 as source-level parent of this die (it can't be, the language
22171 doesn't allow it), and break the loop here. */
22172 name = dwarf2_name (die, cu);
22173 parent_name = dwarf2_name (parent, cu);
22174 complaint (&symfile_complaints,
22175 _("template param type '%s' defined within parent '%s'"),
22176 name ? name : "<unknown>",
22177 parent_name ? parent_name : "<unknown>");
22178 return "";
22179 }
63d06c5c 22180 else
0114d602
DJ
22181 switch (parent->tag)
22182 {
63d06c5c 22183 case DW_TAG_namespace:
0114d602 22184 parent_type = read_type_die (parent, cu);
acebe513
UW
22185 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
22186 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
22187 Work around this problem here. */
22188 if (cu->language == language_cplus
22189 && strcmp (TYPE_TAG_NAME (parent_type), "::") == 0)
22190 return "";
0114d602
DJ
22191 /* We give a name to even anonymous namespaces. */
22192 return TYPE_TAG_NAME (parent_type);
63d06c5c 22193 case DW_TAG_class_type:
680b30c7 22194 case DW_TAG_interface_type:
63d06c5c 22195 case DW_TAG_structure_type:
0114d602 22196 case DW_TAG_union_type:
f55ee35c 22197 case DW_TAG_module:
0114d602
DJ
22198 parent_type = read_type_die (parent, cu);
22199 if (TYPE_TAG_NAME (parent_type) != NULL)
22200 return TYPE_TAG_NAME (parent_type);
22201 else
22202 /* An anonymous structure is only allowed non-static data
22203 members; no typedefs, no member functions, et cetera.
22204 So it does not need a prefix. */
22205 return "";
abc72ce4 22206 case DW_TAG_compile_unit:
95554aad 22207 case DW_TAG_partial_unit:
abc72ce4
DE
22208 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
22209 if (cu->language == language_cplus
8b70b953 22210 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
abc72ce4
DE
22211 && die->child != NULL
22212 && (die->tag == DW_TAG_class_type
22213 || die->tag == DW_TAG_structure_type
22214 || die->tag == DW_TAG_union_type))
22215 {
22216 char *name = guess_full_die_structure_name (die, cu);
22217 if (name != NULL)
22218 return name;
22219 }
22220 return "";
3d567982
TT
22221 case DW_TAG_enumeration_type:
22222 parent_type = read_type_die (parent, cu);
22223 if (TYPE_DECLARED_CLASS (parent_type))
22224 {
22225 if (TYPE_TAG_NAME (parent_type) != NULL)
22226 return TYPE_TAG_NAME (parent_type);
22227 return "";
22228 }
22229 /* Fall through. */
63d06c5c 22230 default:
8176b9b8 22231 return determine_prefix (parent, cu);
63d06c5c 22232 }
63d06c5c
DC
22233}
22234
3e43a32a
MS
22235/* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
22236 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
22237 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
22238 an obconcat, otherwise allocate storage for the result. The CU argument is
22239 used to determine the language and hence, the appropriate separator. */
987504bb 22240
f55ee35c 22241#define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
63d06c5c
DC
22242
22243static char *
f55ee35c
JK
22244typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
22245 int physname, struct dwarf2_cu *cu)
63d06c5c 22246{
f55ee35c 22247 const char *lead = "";
5c315b68 22248 const char *sep;
63d06c5c 22249
3e43a32a
MS
22250 if (suffix == NULL || suffix[0] == '\0'
22251 || prefix == NULL || prefix[0] == '\0')
987504bb 22252 sep = "";
45280282
IB
22253 else if (cu->language == language_d)
22254 {
22255 /* For D, the 'main' function could be defined in any module, but it
22256 should never be prefixed. */
22257 if (strcmp (suffix, "D main") == 0)
22258 {
22259 prefix = "";
22260 sep = "";
22261 }
22262 else
22263 sep = ".";
22264 }
f55ee35c
JK
22265 else if (cu->language == language_fortran && physname)
22266 {
22267 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
22268 DW_AT_MIPS_linkage_name is preferred and used instead. */
22269
22270 lead = "__";
22271 sep = "_MOD_";
22272 }
987504bb
JJ
22273 else
22274 sep = "::";
63d06c5c 22275
6dd47d34
DE
22276 if (prefix == NULL)
22277 prefix = "";
22278 if (suffix == NULL)
22279 suffix = "";
22280
987504bb
JJ
22281 if (obs == NULL)
22282 {
3e43a32a 22283 char *retval
224c3ddb
SM
22284 = ((char *)
22285 xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1));
9a619af0 22286
f55ee35c
JK
22287 strcpy (retval, lead);
22288 strcat (retval, prefix);
6dd47d34
DE
22289 strcat (retval, sep);
22290 strcat (retval, suffix);
63d06c5c
DC
22291 return retval;
22292 }
987504bb
JJ
22293 else
22294 {
22295 /* We have an obstack. */
f55ee35c 22296 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
987504bb 22297 }
63d06c5c
DC
22298}
22299
c906108c
SS
22300/* Return sibling of die, NULL if no sibling. */
22301
f9aca02d 22302static struct die_info *
fba45db2 22303sibling_die (struct die_info *die)
c906108c 22304{
639d11d3 22305 return die->sibling;
c906108c
SS
22306}
22307
71c25dea
TT
22308/* Get name of a die, return NULL if not found. */
22309
15d034d0
TT
22310static const char *
22311dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
71c25dea
TT
22312 struct obstack *obstack)
22313{
22314 if (name && cu->language == language_cplus)
22315 {
2f408ecb 22316 std::string canon_name = cp_canonicalize_string (name);
71c25dea 22317
2f408ecb 22318 if (!canon_name.empty ())
71c25dea 22319 {
2f408ecb
PA
22320 if (canon_name != name)
22321 name = (const char *) obstack_copy0 (obstack,
22322 canon_name.c_str (),
22323 canon_name.length ());
71c25dea
TT
22324 }
22325 }
22326
22327 return name;
c906108c
SS
22328}
22329
96553a0c
DE
22330/* Get name of a die, return NULL if not found.
22331 Anonymous namespaces are converted to their magic string. */
9219021c 22332
15d034d0 22333static const char *
e142c38c 22334dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
9219021c
DC
22335{
22336 struct attribute *attr;
22337
e142c38c 22338 attr = dwarf2_attr (die, DW_AT_name, cu);
53832f31 22339 if ((!attr || !DW_STRING (attr))
96553a0c 22340 && die->tag != DW_TAG_namespace
53832f31
TT
22341 && die->tag != DW_TAG_class_type
22342 && die->tag != DW_TAG_interface_type
22343 && die->tag != DW_TAG_structure_type
22344 && die->tag != DW_TAG_union_type)
71c25dea
TT
22345 return NULL;
22346
22347 switch (die->tag)
22348 {
22349 case DW_TAG_compile_unit:
95554aad 22350 case DW_TAG_partial_unit:
71c25dea
TT
22351 /* Compilation units have a DW_AT_name that is a filename, not
22352 a source language identifier. */
22353 case DW_TAG_enumeration_type:
22354 case DW_TAG_enumerator:
22355 /* These tags always have simple identifiers already; no need
22356 to canonicalize them. */
22357 return DW_STRING (attr);
907af001 22358
96553a0c
DE
22359 case DW_TAG_namespace:
22360 if (attr != NULL && DW_STRING (attr) != NULL)
22361 return DW_STRING (attr);
22362 return CP_ANONYMOUS_NAMESPACE_STR;
22363
907af001
UW
22364 case DW_TAG_class_type:
22365 case DW_TAG_interface_type:
22366 case DW_TAG_structure_type:
22367 case DW_TAG_union_type:
22368 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
22369 structures or unions. These were of the form "._%d" in GCC 4.1,
22370 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
22371 and GCC 4.4. We work around this problem by ignoring these. */
53832f31 22372 if (attr && DW_STRING (attr)
61012eef
GB
22373 && (startswith (DW_STRING (attr), "._")
22374 || startswith (DW_STRING (attr), "<anonymous")))
907af001 22375 return NULL;
53832f31
TT
22376
22377 /* GCC might emit a nameless typedef that has a linkage name. See
22378 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
22379 if (!attr || DW_STRING (attr) == NULL)
22380 {
df5c6c50 22381 char *demangled = NULL;
53832f31 22382
73b9be8b 22383 attr = dw2_linkage_name_attr (die, cu);
53832f31
TT
22384 if (attr == NULL || DW_STRING (attr) == NULL)
22385 return NULL;
22386
df5c6c50
JK
22387 /* Avoid demangling DW_STRING (attr) the second time on a second
22388 call for the same DIE. */
22389 if (!DW_STRING_IS_CANONICAL (attr))
8de20a37 22390 demangled = gdb_demangle (DW_STRING (attr), DMGL_TYPES);
53832f31
TT
22391
22392 if (demangled)
22393 {
e6a959d6 22394 const char *base;
96408a79 22395
53832f31 22396 /* FIXME: we already did this for the partial symbol... */
34a68019 22397 DW_STRING (attr)
224c3ddb
SM
22398 = ((const char *)
22399 obstack_copy0 (&cu->objfile->per_bfd->storage_obstack,
22400 demangled, strlen (demangled)));
53832f31
TT
22401 DW_STRING_IS_CANONICAL (attr) = 1;
22402 xfree (demangled);
96408a79
SA
22403
22404 /* Strip any leading namespaces/classes, keep only the base name.
22405 DW_AT_name for named DIEs does not contain the prefixes. */
22406 base = strrchr (DW_STRING (attr), ':');
22407 if (base && base > DW_STRING (attr) && base[-1] == ':')
22408 return &base[1];
22409 else
22410 return DW_STRING (attr);
53832f31
TT
22411 }
22412 }
907af001
UW
22413 break;
22414
71c25dea 22415 default:
907af001
UW
22416 break;
22417 }
22418
22419 if (!DW_STRING_IS_CANONICAL (attr))
22420 {
22421 DW_STRING (attr)
22422 = dwarf2_canonicalize_name (DW_STRING (attr), cu,
34a68019 22423 &cu->objfile->per_bfd->storage_obstack);
907af001 22424 DW_STRING_IS_CANONICAL (attr) = 1;
71c25dea 22425 }
907af001 22426 return DW_STRING (attr);
9219021c
DC
22427}
22428
22429/* Return the die that this die in an extension of, or NULL if there
f2f0e013
DJ
22430 is none. *EXT_CU is the CU containing DIE on input, and the CU
22431 containing the return value on output. */
9219021c
DC
22432
22433static struct die_info *
f2f0e013 22434dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
9219021c
DC
22435{
22436 struct attribute *attr;
9219021c 22437
f2f0e013 22438 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
9219021c
DC
22439 if (attr == NULL)
22440 return NULL;
22441
f2f0e013 22442 return follow_die_ref (die, attr, ext_cu);
9219021c
DC
22443}
22444
c906108c
SS
22445/* Convert a DIE tag into its string name. */
22446
f39c6ffd 22447static const char *
aa1ee363 22448dwarf_tag_name (unsigned tag)
c906108c 22449{
f39c6ffd
TT
22450 const char *name = get_DW_TAG_name (tag);
22451
22452 if (name == NULL)
22453 return "DW_TAG_<unknown>";
22454
22455 return name;
c906108c
SS
22456}
22457
22458/* Convert a DWARF attribute code into its string name. */
22459
f39c6ffd 22460static const char *
aa1ee363 22461dwarf_attr_name (unsigned attr)
c906108c 22462{
f39c6ffd
TT
22463 const char *name;
22464
c764a876 22465#ifdef MIPS /* collides with DW_AT_HP_block_index */
f39c6ffd
TT
22466 if (attr == DW_AT_MIPS_fde)
22467 return "DW_AT_MIPS_fde";
22468#else
22469 if (attr == DW_AT_HP_block_index)
22470 return "DW_AT_HP_block_index";
c764a876 22471#endif
f39c6ffd
TT
22472
22473 name = get_DW_AT_name (attr);
22474
22475 if (name == NULL)
22476 return "DW_AT_<unknown>";
22477
22478 return name;
c906108c
SS
22479}
22480
22481/* Convert a DWARF value form code into its string name. */
22482
f39c6ffd 22483static const char *
aa1ee363 22484dwarf_form_name (unsigned form)
c906108c 22485{
f39c6ffd
TT
22486 const char *name = get_DW_FORM_name (form);
22487
22488 if (name == NULL)
22489 return "DW_FORM_<unknown>";
22490
22491 return name;
c906108c
SS
22492}
22493
a121b7c1 22494static const char *
fba45db2 22495dwarf_bool_name (unsigned mybool)
c906108c
SS
22496{
22497 if (mybool)
22498 return "TRUE";
22499 else
22500 return "FALSE";
22501}
22502
22503/* Convert a DWARF type code into its string name. */
22504
f39c6ffd 22505static const char *
aa1ee363 22506dwarf_type_encoding_name (unsigned enc)
c906108c 22507{
f39c6ffd 22508 const char *name = get_DW_ATE_name (enc);
c906108c 22509
f39c6ffd
TT
22510 if (name == NULL)
22511 return "DW_ATE_<unknown>";
c906108c 22512
f39c6ffd 22513 return name;
c906108c 22514}
c906108c 22515
f9aca02d 22516static void
d97bc12b 22517dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
c906108c
SS
22518{
22519 unsigned int i;
22520
d97bc12b
DE
22521 print_spaces (indent, f);
22522 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset 0x%x)\n",
9c541725
PA
22523 dwarf_tag_name (die->tag), die->abbrev,
22524 to_underlying (die->sect_off));
d97bc12b
DE
22525
22526 if (die->parent != NULL)
22527 {
22528 print_spaces (indent, f);
22529 fprintf_unfiltered (f, " parent at offset: 0x%x\n",
9c541725 22530 to_underlying (die->parent->sect_off));
d97bc12b
DE
22531 }
22532
22533 print_spaces (indent, f);
22534 fprintf_unfiltered (f, " has children: %s\n",
639d11d3 22535 dwarf_bool_name (die->child != NULL));
c906108c 22536
d97bc12b
DE
22537 print_spaces (indent, f);
22538 fprintf_unfiltered (f, " attributes:\n");
22539
c906108c
SS
22540 for (i = 0; i < die->num_attrs; ++i)
22541 {
d97bc12b
DE
22542 print_spaces (indent, f);
22543 fprintf_unfiltered (f, " %s (%s) ",
c906108c
SS
22544 dwarf_attr_name (die->attrs[i].name),
22545 dwarf_form_name (die->attrs[i].form));
d97bc12b 22546
c906108c
SS
22547 switch (die->attrs[i].form)
22548 {
c906108c 22549 case DW_FORM_addr:
3019eac3 22550 case DW_FORM_GNU_addr_index:
d97bc12b 22551 fprintf_unfiltered (f, "address: ");
5af949e3 22552 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
c906108c
SS
22553 break;
22554 case DW_FORM_block2:
22555 case DW_FORM_block4:
22556 case DW_FORM_block:
22557 case DW_FORM_block1:
56eb65bd
SP
22558 fprintf_unfiltered (f, "block: size %s",
22559 pulongest (DW_BLOCK (&die->attrs[i])->size));
c906108c 22560 break;
2dc7f7b3 22561 case DW_FORM_exprloc:
56eb65bd
SP
22562 fprintf_unfiltered (f, "expression: size %s",
22563 pulongest (DW_BLOCK (&die->attrs[i])->size));
2dc7f7b3 22564 break;
0224619f
JK
22565 case DW_FORM_data16:
22566 fprintf_unfiltered (f, "constant of 16 bytes");
22567 break;
4568ecf9
DE
22568 case DW_FORM_ref_addr:
22569 fprintf_unfiltered (f, "ref address: ");
22570 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
22571 break;
36586728
TT
22572 case DW_FORM_GNU_ref_alt:
22573 fprintf_unfiltered (f, "alt ref address: ");
22574 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
22575 break;
10b3939b
DJ
22576 case DW_FORM_ref1:
22577 case DW_FORM_ref2:
22578 case DW_FORM_ref4:
4568ecf9
DE
22579 case DW_FORM_ref8:
22580 case DW_FORM_ref_udata:
d97bc12b 22581 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
4568ecf9 22582 (long) (DW_UNSND (&die->attrs[i])));
10b3939b 22583 break;
c906108c
SS
22584 case DW_FORM_data1:
22585 case DW_FORM_data2:
22586 case DW_FORM_data4:
ce5d95e1 22587 case DW_FORM_data8:
c906108c
SS
22588 case DW_FORM_udata:
22589 case DW_FORM_sdata:
43bbcdc2
PH
22590 fprintf_unfiltered (f, "constant: %s",
22591 pulongest (DW_UNSND (&die->attrs[i])));
c906108c 22592 break;
2dc7f7b3
TT
22593 case DW_FORM_sec_offset:
22594 fprintf_unfiltered (f, "section offset: %s",
22595 pulongest (DW_UNSND (&die->attrs[i])));
22596 break;
55f1336d 22597 case DW_FORM_ref_sig8:
ac9ec31b
DE
22598 fprintf_unfiltered (f, "signature: %s",
22599 hex_string (DW_SIGNATURE (&die->attrs[i])));
348e048f 22600 break;
c906108c 22601 case DW_FORM_string:
4bdf3d34 22602 case DW_FORM_strp:
43988095 22603 case DW_FORM_line_strp:
3019eac3 22604 case DW_FORM_GNU_str_index:
36586728 22605 case DW_FORM_GNU_strp_alt:
8285870a 22606 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
c906108c 22607 DW_STRING (&die->attrs[i])
8285870a
JK
22608 ? DW_STRING (&die->attrs[i]) : "",
22609 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
c906108c
SS
22610 break;
22611 case DW_FORM_flag:
22612 if (DW_UNSND (&die->attrs[i]))
d97bc12b 22613 fprintf_unfiltered (f, "flag: TRUE");
c906108c 22614 else
d97bc12b 22615 fprintf_unfiltered (f, "flag: FALSE");
c906108c 22616 break;
2dc7f7b3
TT
22617 case DW_FORM_flag_present:
22618 fprintf_unfiltered (f, "flag: TRUE");
22619 break;
a8329558 22620 case DW_FORM_indirect:
0963b4bd
MS
22621 /* The reader will have reduced the indirect form to
22622 the "base form" so this form should not occur. */
3e43a32a
MS
22623 fprintf_unfiltered (f,
22624 "unexpected attribute form: DW_FORM_indirect");
a8329558 22625 break;
663c44ac
JK
22626 case DW_FORM_implicit_const:
22627 fprintf_unfiltered (f, "constant: %s",
22628 plongest (DW_SND (&die->attrs[i])));
22629 break;
c906108c 22630 default:
d97bc12b 22631 fprintf_unfiltered (f, "unsupported attribute form: %d.",
c5aa993b 22632 die->attrs[i].form);
d97bc12b 22633 break;
c906108c 22634 }
d97bc12b 22635 fprintf_unfiltered (f, "\n");
c906108c
SS
22636 }
22637}
22638
f9aca02d 22639static void
d97bc12b 22640dump_die_for_error (struct die_info *die)
c906108c 22641{
d97bc12b
DE
22642 dump_die_shallow (gdb_stderr, 0, die);
22643}
22644
22645static void
22646dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
22647{
22648 int indent = level * 4;
22649
22650 gdb_assert (die != NULL);
22651
22652 if (level >= max_level)
22653 return;
22654
22655 dump_die_shallow (f, indent, die);
22656
22657 if (die->child != NULL)
c906108c 22658 {
d97bc12b
DE
22659 print_spaces (indent, f);
22660 fprintf_unfiltered (f, " Children:");
22661 if (level + 1 < max_level)
22662 {
22663 fprintf_unfiltered (f, "\n");
22664 dump_die_1 (f, level + 1, max_level, die->child);
22665 }
22666 else
22667 {
3e43a32a
MS
22668 fprintf_unfiltered (f,
22669 " [not printed, max nesting level reached]\n");
d97bc12b
DE
22670 }
22671 }
22672
22673 if (die->sibling != NULL && level > 0)
22674 {
22675 dump_die_1 (f, level, max_level, die->sibling);
c906108c
SS
22676 }
22677}
22678
d97bc12b
DE
22679/* This is called from the pdie macro in gdbinit.in.
22680 It's not static so gcc will keep a copy callable from gdb. */
22681
22682void
22683dump_die (struct die_info *die, int max_level)
22684{
22685 dump_die_1 (gdb_stdlog, 0, max_level, die);
22686}
22687
f9aca02d 22688static void
51545339 22689store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
c906108c 22690{
51545339 22691 void **slot;
c906108c 22692
9c541725
PA
22693 slot = htab_find_slot_with_hash (cu->die_hash, die,
22694 to_underlying (die->sect_off),
b64f50a1 22695 INSERT);
51545339
DJ
22696
22697 *slot = die;
c906108c
SS
22698}
22699
b64f50a1
JK
22700/* Return DIE offset of ATTR. Return 0 with complaint if ATTR is not of the
22701 required kind. */
22702
22703static sect_offset
ff39bb5e 22704dwarf2_get_ref_die_offset (const struct attribute *attr)
93311388 22705{
7771576e 22706 if (attr_form_is_ref (attr))
9c541725 22707 return (sect_offset) DW_UNSND (attr);
93311388
DE
22708
22709 complaint (&symfile_complaints,
22710 _("unsupported die ref attribute form: '%s'"),
22711 dwarf_form_name (attr->form));
9c541725 22712 return {};
c906108c
SS
22713}
22714
43bbcdc2
PH
22715/* Return the constant value held by ATTR. Return DEFAULT_VALUE if
22716 * the value held by the attribute is not constant. */
a02abb62 22717
43bbcdc2 22718static LONGEST
ff39bb5e 22719dwarf2_get_attr_constant_value (const struct attribute *attr, int default_value)
a02abb62 22720{
663c44ac 22721 if (attr->form == DW_FORM_sdata || attr->form == DW_FORM_implicit_const)
a02abb62
JB
22722 return DW_SND (attr);
22723 else if (attr->form == DW_FORM_udata
22724 || attr->form == DW_FORM_data1
22725 || attr->form == DW_FORM_data2
22726 || attr->form == DW_FORM_data4
22727 || attr->form == DW_FORM_data8)
22728 return DW_UNSND (attr);
22729 else
22730 {
0224619f 22731 /* For DW_FORM_data16 see attr_form_is_constant. */
3e43a32a
MS
22732 complaint (&symfile_complaints,
22733 _("Attribute value is not a constant (%s)"),
a02abb62
JB
22734 dwarf_form_name (attr->form));
22735 return default_value;
22736 }
22737}
22738
348e048f
DE
22739/* Follow reference or signature attribute ATTR of SRC_DIE.
22740 On entry *REF_CU is the CU of SRC_DIE.
22741 On exit *REF_CU is the CU of the result. */
22742
22743static struct die_info *
ff39bb5e 22744follow_die_ref_or_sig (struct die_info *src_die, const struct attribute *attr,
348e048f
DE
22745 struct dwarf2_cu **ref_cu)
22746{
22747 struct die_info *die;
22748
7771576e 22749 if (attr_form_is_ref (attr))
348e048f 22750 die = follow_die_ref (src_die, attr, ref_cu);
55f1336d 22751 else if (attr->form == DW_FORM_ref_sig8)
348e048f
DE
22752 die = follow_die_sig (src_die, attr, ref_cu);
22753 else
22754 {
22755 dump_die_for_error (src_die);
22756 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
4262abfb 22757 objfile_name ((*ref_cu)->objfile));
348e048f
DE
22758 }
22759
22760 return die;
03dd20cc
DJ
22761}
22762
5c631832 22763/* Follow reference OFFSET.
673bfd45
DE
22764 On entry *REF_CU is the CU of the source die referencing OFFSET.
22765 On exit *REF_CU is the CU of the result.
22766 Returns NULL if OFFSET is invalid. */
f504f079 22767
f9aca02d 22768static struct die_info *
9c541725 22769follow_die_offset (sect_offset sect_off, int offset_in_dwz,
36586728 22770 struct dwarf2_cu **ref_cu)
c906108c 22771{
10b3939b 22772 struct die_info temp_die;
f2f0e013 22773 struct dwarf2_cu *target_cu, *cu = *ref_cu;
10b3939b 22774
348e048f
DE
22775 gdb_assert (cu->per_cu != NULL);
22776
98bfdba5
PA
22777 target_cu = cu;
22778
3019eac3 22779 if (cu->per_cu->is_debug_types)
348e048f
DE
22780 {
22781 /* .debug_types CUs cannot reference anything outside their CU.
22782 If they need to, they have to reference a signatured type via
55f1336d 22783 DW_FORM_ref_sig8. */
9c541725 22784 if (!offset_in_cu_p (&cu->header, sect_off))
5c631832 22785 return NULL;
348e048f 22786 }
36586728 22787 else if (offset_in_dwz != cu->per_cu->is_dwz
9c541725 22788 || !offset_in_cu_p (&cu->header, sect_off))
10b3939b
DJ
22789 {
22790 struct dwarf2_per_cu_data *per_cu;
9a619af0 22791
9c541725 22792 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
36586728 22793 cu->objfile);
03dd20cc
DJ
22794
22795 /* If necessary, add it to the queue and load its DIEs. */
95554aad
TT
22796 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
22797 load_full_comp_unit (per_cu, cu->language);
03dd20cc 22798
10b3939b
DJ
22799 target_cu = per_cu->cu;
22800 }
98bfdba5
PA
22801 else if (cu->dies == NULL)
22802 {
22803 /* We're loading full DIEs during partial symbol reading. */
22804 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
95554aad 22805 load_full_comp_unit (cu->per_cu, language_minimal);
98bfdba5 22806 }
c906108c 22807
f2f0e013 22808 *ref_cu = target_cu;
9c541725 22809 temp_die.sect_off = sect_off;
9a3c8263 22810 return (struct die_info *) htab_find_with_hash (target_cu->die_hash,
9c541725
PA
22811 &temp_die,
22812 to_underlying (sect_off));
5c631832 22813}
10b3939b 22814
5c631832
JK
22815/* Follow reference attribute ATTR of SRC_DIE.
22816 On entry *REF_CU is the CU of SRC_DIE.
22817 On exit *REF_CU is the CU of the result. */
22818
22819static struct die_info *
ff39bb5e 22820follow_die_ref (struct die_info *src_die, const struct attribute *attr,
5c631832
JK
22821 struct dwarf2_cu **ref_cu)
22822{
9c541725 22823 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
5c631832
JK
22824 struct dwarf2_cu *cu = *ref_cu;
22825 struct die_info *die;
22826
9c541725 22827 die = follow_die_offset (sect_off,
36586728
TT
22828 (attr->form == DW_FORM_GNU_ref_alt
22829 || cu->per_cu->is_dwz),
22830 ref_cu);
5c631832
JK
22831 if (!die)
22832 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced from DIE "
22833 "at 0x%x [in module %s]"),
9c541725 22834 to_underlying (sect_off), to_underlying (src_die->sect_off),
4262abfb 22835 objfile_name (cu->objfile));
348e048f 22836
5c631832
JK
22837 return die;
22838}
22839
9c541725 22840/* Return DWARF block referenced by DW_AT_location of DIE at SECT_OFF at PER_CU.
d83e736b
JK
22841 Returned value is intended for DW_OP_call*. Returned
22842 dwarf2_locexpr_baton->data has lifetime of PER_CU->OBJFILE. */
5c631832
JK
22843
22844struct dwarf2_locexpr_baton
9c541725 22845dwarf2_fetch_die_loc_sect_off (sect_offset sect_off,
8b9737bf
TT
22846 struct dwarf2_per_cu_data *per_cu,
22847 CORE_ADDR (*get_frame_pc) (void *baton),
22848 void *baton)
5c631832 22849{
918dd910 22850 struct dwarf2_cu *cu;
5c631832
JK
22851 struct die_info *die;
22852 struct attribute *attr;
22853 struct dwarf2_locexpr_baton retval;
22854
8cf6f0b1
TT
22855 dw2_setup (per_cu->objfile);
22856
918dd910
JK
22857 if (per_cu->cu == NULL)
22858 load_cu (per_cu);
22859 cu = per_cu->cu;
cc12ce38
DE
22860 if (cu == NULL)
22861 {
22862 /* We shouldn't get here for a dummy CU, but don't crash on the user.
22863 Instead just throw an error, not much else we can do. */
22864 error (_("Dwarf Error: Dummy CU at 0x%x referenced in module %s"),
9c541725 22865 to_underlying (sect_off), objfile_name (per_cu->objfile));
cc12ce38 22866 }
918dd910 22867
9c541725 22868 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
5c631832
JK
22869 if (!die)
22870 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
9c541725 22871 to_underlying (sect_off), objfile_name (per_cu->objfile));
5c631832
JK
22872
22873 attr = dwarf2_attr (die, DW_AT_location, cu);
22874 if (!attr)
22875 {
e103e986
JK
22876 /* DWARF: "If there is no such attribute, then there is no effect.".
22877 DATA is ignored if SIZE is 0. */
5c631832 22878
e103e986 22879 retval.data = NULL;
5c631832
JK
22880 retval.size = 0;
22881 }
8cf6f0b1
TT
22882 else if (attr_form_is_section_offset (attr))
22883 {
22884 struct dwarf2_loclist_baton loclist_baton;
22885 CORE_ADDR pc = (*get_frame_pc) (baton);
22886 size_t size;
22887
22888 fill_in_loclist_baton (cu, &loclist_baton, attr);
22889
22890 retval.data = dwarf2_find_location_expression (&loclist_baton,
22891 &size, pc);
22892 retval.size = size;
22893 }
5c631832
JK
22894 else
22895 {
22896 if (!attr_form_is_block (attr))
22897 error (_("Dwarf Error: DIE at 0x%x referenced in module %s "
22898 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
9c541725 22899 to_underlying (sect_off), objfile_name (per_cu->objfile));
5c631832
JK
22900
22901 retval.data = DW_BLOCK (attr)->data;
22902 retval.size = DW_BLOCK (attr)->size;
22903 }
22904 retval.per_cu = cu->per_cu;
918dd910 22905
918dd910
JK
22906 age_cached_comp_units ();
22907
5c631832 22908 return retval;
348e048f
DE
22909}
22910
8b9737bf
TT
22911/* Like dwarf2_fetch_die_loc_sect_off, but take a CU
22912 offset. */
22913
22914struct dwarf2_locexpr_baton
22915dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
22916 struct dwarf2_per_cu_data *per_cu,
22917 CORE_ADDR (*get_frame_pc) (void *baton),
22918 void *baton)
22919{
9c541725 22920 sect_offset sect_off = per_cu->sect_off + to_underlying (offset_in_cu);
8b9737bf 22921
9c541725 22922 return dwarf2_fetch_die_loc_sect_off (sect_off, per_cu, get_frame_pc, baton);
8b9737bf
TT
22923}
22924
b6807d98
TT
22925/* Write a constant of a given type as target-ordered bytes into
22926 OBSTACK. */
22927
22928static const gdb_byte *
22929write_constant_as_bytes (struct obstack *obstack,
22930 enum bfd_endian byte_order,
22931 struct type *type,
22932 ULONGEST value,
22933 LONGEST *len)
22934{
22935 gdb_byte *result;
22936
22937 *len = TYPE_LENGTH (type);
224c3ddb 22938 result = (gdb_byte *) obstack_alloc (obstack, *len);
b6807d98
TT
22939 store_unsigned_integer (result, *len, byte_order, value);
22940
22941 return result;
22942}
22943
22944/* If the DIE at OFFSET in PER_CU has a DW_AT_const_value, return a
22945 pointer to the constant bytes and set LEN to the length of the
22946 data. If memory is needed, allocate it on OBSTACK. If the DIE
22947 does not have a DW_AT_const_value, return NULL. */
22948
22949const gdb_byte *
9c541725 22950dwarf2_fetch_constant_bytes (sect_offset sect_off,
b6807d98
TT
22951 struct dwarf2_per_cu_data *per_cu,
22952 struct obstack *obstack,
22953 LONGEST *len)
22954{
22955 struct dwarf2_cu *cu;
22956 struct die_info *die;
22957 struct attribute *attr;
22958 const gdb_byte *result = NULL;
22959 struct type *type;
22960 LONGEST value;
22961 enum bfd_endian byte_order;
22962
22963 dw2_setup (per_cu->objfile);
22964
22965 if (per_cu->cu == NULL)
22966 load_cu (per_cu);
22967 cu = per_cu->cu;
cc12ce38
DE
22968 if (cu == NULL)
22969 {
22970 /* We shouldn't get here for a dummy CU, but don't crash on the user.
22971 Instead just throw an error, not much else we can do. */
22972 error (_("Dwarf Error: Dummy CU at 0x%x referenced in module %s"),
9c541725 22973 to_underlying (sect_off), objfile_name (per_cu->objfile));
cc12ce38 22974 }
b6807d98 22975
9c541725 22976 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
b6807d98
TT
22977 if (!die)
22978 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
9c541725 22979 to_underlying (sect_off), objfile_name (per_cu->objfile));
b6807d98
TT
22980
22981
22982 attr = dwarf2_attr (die, DW_AT_const_value, cu);
22983 if (attr == NULL)
22984 return NULL;
22985
22986 byte_order = (bfd_big_endian (per_cu->objfile->obfd)
22987 ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
22988
22989 switch (attr->form)
22990 {
22991 case DW_FORM_addr:
22992 case DW_FORM_GNU_addr_index:
22993 {
22994 gdb_byte *tem;
22995
22996 *len = cu->header.addr_size;
224c3ddb 22997 tem = (gdb_byte *) obstack_alloc (obstack, *len);
b6807d98
TT
22998 store_unsigned_integer (tem, *len, byte_order, DW_ADDR (attr));
22999 result = tem;
23000 }
23001 break;
23002 case DW_FORM_string:
23003 case DW_FORM_strp:
23004 case DW_FORM_GNU_str_index:
23005 case DW_FORM_GNU_strp_alt:
23006 /* DW_STRING is already allocated on the objfile obstack, point
23007 directly to it. */
23008 result = (const gdb_byte *) DW_STRING (attr);
23009 *len = strlen (DW_STRING (attr));
23010 break;
23011 case DW_FORM_block1:
23012 case DW_FORM_block2:
23013 case DW_FORM_block4:
23014 case DW_FORM_block:
23015 case DW_FORM_exprloc:
0224619f 23016 case DW_FORM_data16:
b6807d98
TT
23017 result = DW_BLOCK (attr)->data;
23018 *len = DW_BLOCK (attr)->size;
23019 break;
23020
23021 /* The DW_AT_const_value attributes are supposed to carry the
23022 symbol's value "represented as it would be on the target
23023 architecture." By the time we get here, it's already been
23024 converted to host endianness, so we just need to sign- or
23025 zero-extend it as appropriate. */
23026 case DW_FORM_data1:
23027 type = die_type (die, cu);
23028 result = dwarf2_const_value_data (attr, obstack, cu, &value, 8);
23029 if (result == NULL)
23030 result = write_constant_as_bytes (obstack, byte_order,
23031 type, value, len);
23032 break;
23033 case DW_FORM_data2:
23034 type = die_type (die, cu);
23035 result = dwarf2_const_value_data (attr, obstack, cu, &value, 16);
23036 if (result == NULL)
23037 result = write_constant_as_bytes (obstack, byte_order,
23038 type, value, len);
23039 break;
23040 case DW_FORM_data4:
23041 type = die_type (die, cu);
23042 result = dwarf2_const_value_data (attr, obstack, cu, &value, 32);
23043 if (result == NULL)
23044 result = write_constant_as_bytes (obstack, byte_order,
23045 type, value, len);
23046 break;
23047 case DW_FORM_data8:
23048 type = die_type (die, cu);
23049 result = dwarf2_const_value_data (attr, obstack, cu, &value, 64);
23050 if (result == NULL)
23051 result = write_constant_as_bytes (obstack, byte_order,
23052 type, value, len);
23053 break;
23054
23055 case DW_FORM_sdata:
663c44ac 23056 case DW_FORM_implicit_const:
b6807d98
TT
23057 type = die_type (die, cu);
23058 result = write_constant_as_bytes (obstack, byte_order,
23059 type, DW_SND (attr), len);
23060 break;
23061
23062 case DW_FORM_udata:
23063 type = die_type (die, cu);
23064 result = write_constant_as_bytes (obstack, byte_order,
23065 type, DW_UNSND (attr), len);
23066 break;
23067
23068 default:
23069 complaint (&symfile_complaints,
23070 _("unsupported const value attribute form: '%s'"),
23071 dwarf_form_name (attr->form));
23072 break;
23073 }
23074
23075 return result;
23076}
23077
7942e96e
AA
23078/* Return the type of the die at OFFSET in PER_CU. Return NULL if no
23079 valid type for this die is found. */
23080
23081struct type *
9c541725 23082dwarf2_fetch_die_type_sect_off (sect_offset sect_off,
7942e96e
AA
23083 struct dwarf2_per_cu_data *per_cu)
23084{
23085 struct dwarf2_cu *cu;
23086 struct die_info *die;
23087
23088 dw2_setup (per_cu->objfile);
23089
23090 if (per_cu->cu == NULL)
23091 load_cu (per_cu);
23092 cu = per_cu->cu;
23093 if (!cu)
23094 return NULL;
23095
9c541725 23096 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
7942e96e
AA
23097 if (!die)
23098 return NULL;
23099
23100 return die_type (die, cu);
23101}
23102
8a9b8146
TT
23103/* Return the type of the DIE at DIE_OFFSET in the CU named by
23104 PER_CU. */
23105
23106struct type *
b64f50a1 23107dwarf2_get_die_type (cu_offset die_offset,
8a9b8146
TT
23108 struct dwarf2_per_cu_data *per_cu)
23109{
8a9b8146 23110 dw2_setup (per_cu->objfile);
b64f50a1 23111
9c541725 23112 sect_offset die_offset_sect = per_cu->sect_off + to_underlying (die_offset);
b64f50a1 23113 return get_die_type_at_offset (die_offset_sect, per_cu);
8a9b8146
TT
23114}
23115
ac9ec31b 23116/* Follow type unit SIG_TYPE referenced by SRC_DIE.
348e048f 23117 On entry *REF_CU is the CU of SRC_DIE.
ac9ec31b
DE
23118 On exit *REF_CU is the CU of the result.
23119 Returns NULL if the referenced DIE isn't found. */
348e048f
DE
23120
23121static struct die_info *
ac9ec31b
DE
23122follow_die_sig_1 (struct die_info *src_die, struct signatured_type *sig_type,
23123 struct dwarf2_cu **ref_cu)
348e048f 23124{
348e048f 23125 struct die_info temp_die;
348e048f
DE
23126 struct dwarf2_cu *sig_cu;
23127 struct die_info *die;
23128
ac9ec31b
DE
23129 /* While it might be nice to assert sig_type->type == NULL here,
23130 we can get here for DW_AT_imported_declaration where we need
23131 the DIE not the type. */
348e048f
DE
23132
23133 /* If necessary, add it to the queue and load its DIEs. */
23134
95554aad 23135 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, language_minimal))
a0f42c21 23136 read_signatured_type (sig_type);
348e048f 23137
348e048f 23138 sig_cu = sig_type->per_cu.cu;
69d751e3 23139 gdb_assert (sig_cu != NULL);
9c541725
PA
23140 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
23141 temp_die.sect_off = sig_type->type_offset_in_section;
9a3c8263 23142 die = (struct die_info *) htab_find_with_hash (sig_cu->die_hash, &temp_die,
9c541725 23143 to_underlying (temp_die.sect_off));
348e048f
DE
23144 if (die)
23145 {
796a7ff8
DE
23146 /* For .gdb_index version 7 keep track of included TUs.
23147 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
23148 if (dwarf2_per_objfile->index_table != NULL
23149 && dwarf2_per_objfile->index_table->version <= 7)
23150 {
23151 VEC_safe_push (dwarf2_per_cu_ptr,
23152 (*ref_cu)->per_cu->imported_symtabs,
23153 sig_cu->per_cu);
23154 }
23155
348e048f
DE
23156 *ref_cu = sig_cu;
23157 return die;
23158 }
23159
ac9ec31b
DE
23160 return NULL;
23161}
23162
23163/* Follow signatured type referenced by ATTR in SRC_DIE.
23164 On entry *REF_CU is the CU of SRC_DIE.
23165 On exit *REF_CU is the CU of the result.
23166 The result is the DIE of the type.
23167 If the referenced type cannot be found an error is thrown. */
23168
23169static struct die_info *
ff39bb5e 23170follow_die_sig (struct die_info *src_die, const struct attribute *attr,
ac9ec31b
DE
23171 struct dwarf2_cu **ref_cu)
23172{
23173 ULONGEST signature = DW_SIGNATURE (attr);
23174 struct signatured_type *sig_type;
23175 struct die_info *die;
23176
23177 gdb_assert (attr->form == DW_FORM_ref_sig8);
23178
a2ce51a0 23179 sig_type = lookup_signatured_type (*ref_cu, signature);
ac9ec31b
DE
23180 /* sig_type will be NULL if the signatured type is missing from
23181 the debug info. */
23182 if (sig_type == NULL)
23183 {
23184 error (_("Dwarf Error: Cannot find signatured DIE %s referenced"
23185 " from DIE at 0x%x [in module %s]"),
9c541725 23186 hex_string (signature), to_underlying (src_die->sect_off),
4262abfb 23187 objfile_name ((*ref_cu)->objfile));
ac9ec31b
DE
23188 }
23189
23190 die = follow_die_sig_1 (src_die, sig_type, ref_cu);
23191 if (die == NULL)
23192 {
23193 dump_die_for_error (src_die);
23194 error (_("Dwarf Error: Problem reading signatured DIE %s referenced"
23195 " from DIE at 0x%x [in module %s]"),
9c541725 23196 hex_string (signature), to_underlying (src_die->sect_off),
4262abfb 23197 objfile_name ((*ref_cu)->objfile));
ac9ec31b
DE
23198 }
23199
23200 return die;
23201}
23202
23203/* Get the type specified by SIGNATURE referenced in DIE/CU,
23204 reading in and processing the type unit if necessary. */
23205
23206static struct type *
23207get_signatured_type (struct die_info *die, ULONGEST signature,
23208 struct dwarf2_cu *cu)
23209{
23210 struct signatured_type *sig_type;
23211 struct dwarf2_cu *type_cu;
23212 struct die_info *type_die;
23213 struct type *type;
23214
a2ce51a0 23215 sig_type = lookup_signatured_type (cu, signature);
ac9ec31b
DE
23216 /* sig_type will be NULL if the signatured type is missing from
23217 the debug info. */
23218 if (sig_type == NULL)
23219 {
23220 complaint (&symfile_complaints,
23221 _("Dwarf Error: Cannot find signatured DIE %s referenced"
23222 " from DIE at 0x%x [in module %s]"),
9c541725 23223 hex_string (signature), to_underlying (die->sect_off),
4262abfb 23224 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
23225 return build_error_marker_type (cu, die);
23226 }
23227
23228 /* If we already know the type we're done. */
23229 if (sig_type->type != NULL)
23230 return sig_type->type;
23231
23232 type_cu = cu;
23233 type_die = follow_die_sig_1 (die, sig_type, &type_cu);
23234 if (type_die != NULL)
23235 {
23236 /* N.B. We need to call get_die_type to ensure only one type for this DIE
23237 is created. This is important, for example, because for c++ classes
23238 we need TYPE_NAME set which is only done by new_symbol. Blech. */
23239 type = read_type_die (type_die, type_cu);
23240 if (type == NULL)
23241 {
23242 complaint (&symfile_complaints,
23243 _("Dwarf Error: Cannot build signatured type %s"
23244 " referenced from DIE at 0x%x [in module %s]"),
9c541725 23245 hex_string (signature), to_underlying (die->sect_off),
4262abfb 23246 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
23247 type = build_error_marker_type (cu, die);
23248 }
23249 }
23250 else
23251 {
23252 complaint (&symfile_complaints,
23253 _("Dwarf Error: Problem reading signatured DIE %s referenced"
23254 " from DIE at 0x%x [in module %s]"),
9c541725 23255 hex_string (signature), to_underlying (die->sect_off),
4262abfb 23256 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
23257 type = build_error_marker_type (cu, die);
23258 }
23259 sig_type->type = type;
23260
23261 return type;
23262}
23263
23264/* Get the type specified by the DW_AT_signature ATTR in DIE/CU,
23265 reading in and processing the type unit if necessary. */
23266
23267static struct type *
ff39bb5e 23268get_DW_AT_signature_type (struct die_info *die, const struct attribute *attr,
b385a60d 23269 struct dwarf2_cu *cu) /* ARI: editCase function */
ac9ec31b
DE
23270{
23271 /* Yes, DW_AT_signature can use a non-ref_sig8 reference. */
7771576e 23272 if (attr_form_is_ref (attr))
ac9ec31b
DE
23273 {
23274 struct dwarf2_cu *type_cu = cu;
23275 struct die_info *type_die = follow_die_ref (die, attr, &type_cu);
23276
23277 return read_type_die (type_die, type_cu);
23278 }
23279 else if (attr->form == DW_FORM_ref_sig8)
23280 {
23281 return get_signatured_type (die, DW_SIGNATURE (attr), cu);
23282 }
23283 else
23284 {
23285 complaint (&symfile_complaints,
23286 _("Dwarf Error: DW_AT_signature has bad form %s in DIE"
23287 " at 0x%x [in module %s]"),
9c541725 23288 dwarf_form_name (attr->form), to_underlying (die->sect_off),
4262abfb 23289 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
23290 return build_error_marker_type (cu, die);
23291 }
348e048f
DE
23292}
23293
e5fe5e75 23294/* Load the DIEs associated with type unit PER_CU into memory. */
348e048f
DE
23295
23296static void
e5fe5e75 23297load_full_type_unit (struct dwarf2_per_cu_data *per_cu)
348e048f 23298{
52dc124a 23299 struct signatured_type *sig_type;
348e048f 23300
f4dc4d17
DE
23301 /* Caller is responsible for ensuring type_unit_groups don't get here. */
23302 gdb_assert (! IS_TYPE_UNIT_GROUP (per_cu));
23303
6721b2ec
DE
23304 /* We have the per_cu, but we need the signatured_type.
23305 Fortunately this is an easy translation. */
23306 gdb_assert (per_cu->is_debug_types);
23307 sig_type = (struct signatured_type *) per_cu;
348e048f 23308
6721b2ec 23309 gdb_assert (per_cu->cu == NULL);
348e048f 23310
52dc124a 23311 read_signatured_type (sig_type);
348e048f 23312
6721b2ec 23313 gdb_assert (per_cu->cu != NULL);
348e048f
DE
23314}
23315
dee91e82
DE
23316/* die_reader_func for read_signatured_type.
23317 This is identical to load_full_comp_unit_reader,
23318 but is kept separate for now. */
348e048f
DE
23319
23320static void
dee91e82 23321read_signatured_type_reader (const struct die_reader_specs *reader,
d521ce57 23322 const gdb_byte *info_ptr,
dee91e82
DE
23323 struct die_info *comp_unit_die,
23324 int has_children,
23325 void *data)
348e048f 23326{
dee91e82 23327 struct dwarf2_cu *cu = reader->cu;
348e048f 23328
dee91e82
DE
23329 gdb_assert (cu->die_hash == NULL);
23330 cu->die_hash =
23331 htab_create_alloc_ex (cu->header.length / 12,
23332 die_hash,
23333 die_eq,
23334 NULL,
23335 &cu->comp_unit_obstack,
23336 hashtab_obstack_allocate,
23337 dummy_obstack_deallocate);
348e048f 23338
dee91e82
DE
23339 if (has_children)
23340 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
23341 &info_ptr, comp_unit_die);
23342 cu->dies = comp_unit_die;
23343 /* comp_unit_die is not stored in die_hash, no need. */
348e048f
DE
23344
23345 /* We try not to read any attributes in this function, because not
9cdd5dbd 23346 all CUs needed for references have been loaded yet, and symbol
348e048f 23347 table processing isn't initialized. But we have to set the CU language,
dee91e82
DE
23348 or we won't be able to build types correctly.
23349 Similarly, if we do not read the producer, we can not apply
23350 producer-specific interpretation. */
95554aad 23351 prepare_one_comp_unit (cu, cu->dies, language_minimal);
dee91e82 23352}
348e048f 23353
3019eac3
DE
23354/* Read in a signatured type and build its CU and DIEs.
23355 If the type is a stub for the real type in a DWO file,
23356 read in the real type from the DWO file as well. */
dee91e82
DE
23357
23358static void
23359read_signatured_type (struct signatured_type *sig_type)
23360{
23361 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
348e048f 23362
3019eac3 23363 gdb_assert (per_cu->is_debug_types);
dee91e82 23364 gdb_assert (per_cu->cu == NULL);
348e048f 23365
f4dc4d17
DE
23366 init_cutu_and_read_dies (per_cu, NULL, 0, 1,
23367 read_signatured_type_reader, NULL);
7ee85ab1 23368 sig_type->per_cu.tu_read = 1;
c906108c
SS
23369}
23370
c906108c
SS
23371/* Decode simple location descriptions.
23372 Given a pointer to a dwarf block that defines a location, compute
23373 the location and return the value.
23374
4cecd739
DJ
23375 NOTE drow/2003-11-18: This function is called in two situations
23376 now: for the address of static or global variables (partial symbols
23377 only) and for offsets into structures which are expected to be
23378 (more or less) constant. The partial symbol case should go away,
23379 and only the constant case should remain. That will let this
23380 function complain more accurately. A few special modes are allowed
23381 without complaint for global variables (for instance, global
23382 register values and thread-local values).
c906108c
SS
23383
23384 A location description containing no operations indicates that the
4cecd739 23385 object is optimized out. The return value is 0 for that case.
6b992462
DJ
23386 FIXME drow/2003-11-16: No callers check for this case any more; soon all
23387 callers will only want a very basic result and this can become a
21ae7a4d
JK
23388 complaint.
23389
23390 Note that stack[0] is unused except as a default error return. */
c906108c
SS
23391
23392static CORE_ADDR
e7c27a73 23393decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
c906108c 23394{
e7c27a73 23395 struct objfile *objfile = cu->objfile;
56eb65bd
SP
23396 size_t i;
23397 size_t size = blk->size;
d521ce57 23398 const gdb_byte *data = blk->data;
21ae7a4d
JK
23399 CORE_ADDR stack[64];
23400 int stacki;
23401 unsigned int bytes_read, unsnd;
23402 gdb_byte op;
c906108c 23403
21ae7a4d
JK
23404 i = 0;
23405 stacki = 0;
23406 stack[stacki] = 0;
23407 stack[++stacki] = 0;
23408
23409 while (i < size)
23410 {
23411 op = data[i++];
23412 switch (op)
23413 {
23414 case DW_OP_lit0:
23415 case DW_OP_lit1:
23416 case DW_OP_lit2:
23417 case DW_OP_lit3:
23418 case DW_OP_lit4:
23419 case DW_OP_lit5:
23420 case DW_OP_lit6:
23421 case DW_OP_lit7:
23422 case DW_OP_lit8:
23423 case DW_OP_lit9:
23424 case DW_OP_lit10:
23425 case DW_OP_lit11:
23426 case DW_OP_lit12:
23427 case DW_OP_lit13:
23428 case DW_OP_lit14:
23429 case DW_OP_lit15:
23430 case DW_OP_lit16:
23431 case DW_OP_lit17:
23432 case DW_OP_lit18:
23433 case DW_OP_lit19:
23434 case DW_OP_lit20:
23435 case DW_OP_lit21:
23436 case DW_OP_lit22:
23437 case DW_OP_lit23:
23438 case DW_OP_lit24:
23439 case DW_OP_lit25:
23440 case DW_OP_lit26:
23441 case DW_OP_lit27:
23442 case DW_OP_lit28:
23443 case DW_OP_lit29:
23444 case DW_OP_lit30:
23445 case DW_OP_lit31:
23446 stack[++stacki] = op - DW_OP_lit0;
23447 break;
f1bea926 23448
21ae7a4d
JK
23449 case DW_OP_reg0:
23450 case DW_OP_reg1:
23451 case DW_OP_reg2:
23452 case DW_OP_reg3:
23453 case DW_OP_reg4:
23454 case DW_OP_reg5:
23455 case DW_OP_reg6:
23456 case DW_OP_reg7:
23457 case DW_OP_reg8:
23458 case DW_OP_reg9:
23459 case DW_OP_reg10:
23460 case DW_OP_reg11:
23461 case DW_OP_reg12:
23462 case DW_OP_reg13:
23463 case DW_OP_reg14:
23464 case DW_OP_reg15:
23465 case DW_OP_reg16:
23466 case DW_OP_reg17:
23467 case DW_OP_reg18:
23468 case DW_OP_reg19:
23469 case DW_OP_reg20:
23470 case DW_OP_reg21:
23471 case DW_OP_reg22:
23472 case DW_OP_reg23:
23473 case DW_OP_reg24:
23474 case DW_OP_reg25:
23475 case DW_OP_reg26:
23476 case DW_OP_reg27:
23477 case DW_OP_reg28:
23478 case DW_OP_reg29:
23479 case DW_OP_reg30:
23480 case DW_OP_reg31:
23481 stack[++stacki] = op - DW_OP_reg0;
23482 if (i < size)
23483 dwarf2_complex_location_expr_complaint ();
23484 break;
c906108c 23485
21ae7a4d
JK
23486 case DW_OP_regx:
23487 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
23488 i += bytes_read;
23489 stack[++stacki] = unsnd;
23490 if (i < size)
23491 dwarf2_complex_location_expr_complaint ();
23492 break;
c906108c 23493
21ae7a4d
JK
23494 case DW_OP_addr:
23495 stack[++stacki] = read_address (objfile->obfd, &data[i],
23496 cu, &bytes_read);
23497 i += bytes_read;
23498 break;
d53d4ac5 23499
21ae7a4d
JK
23500 case DW_OP_const1u:
23501 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
23502 i += 1;
23503 break;
23504
23505 case DW_OP_const1s:
23506 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
23507 i += 1;
23508 break;
23509
23510 case DW_OP_const2u:
23511 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
23512 i += 2;
23513 break;
23514
23515 case DW_OP_const2s:
23516 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
23517 i += 2;
23518 break;
d53d4ac5 23519
21ae7a4d
JK
23520 case DW_OP_const4u:
23521 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
23522 i += 4;
23523 break;
23524
23525 case DW_OP_const4s:
23526 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
23527 i += 4;
23528 break;
23529
585861ea
JK
23530 case DW_OP_const8u:
23531 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
23532 i += 8;
23533 break;
23534
21ae7a4d
JK
23535 case DW_OP_constu:
23536 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
23537 &bytes_read);
23538 i += bytes_read;
23539 break;
23540
23541 case DW_OP_consts:
23542 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
23543 i += bytes_read;
23544 break;
23545
23546 case DW_OP_dup:
23547 stack[stacki + 1] = stack[stacki];
23548 stacki++;
23549 break;
23550
23551 case DW_OP_plus:
23552 stack[stacki - 1] += stack[stacki];
23553 stacki--;
23554 break;
23555
23556 case DW_OP_plus_uconst:
23557 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
23558 &bytes_read);
23559 i += bytes_read;
23560 break;
23561
23562 case DW_OP_minus:
23563 stack[stacki - 1] -= stack[stacki];
23564 stacki--;
23565 break;
23566
23567 case DW_OP_deref:
23568 /* If we're not the last op, then we definitely can't encode
23569 this using GDB's address_class enum. This is valid for partial
23570 global symbols, although the variable's address will be bogus
23571 in the psymtab. */
23572 if (i < size)
23573 dwarf2_complex_location_expr_complaint ();
23574 break;
23575
23576 case DW_OP_GNU_push_tls_address:
4aa4e28b 23577 case DW_OP_form_tls_address:
21ae7a4d
JK
23578 /* The top of the stack has the offset from the beginning
23579 of the thread control block at which the variable is located. */
23580 /* Nothing should follow this operator, so the top of stack would
23581 be returned. */
23582 /* This is valid for partial global symbols, but the variable's
585861ea
JK
23583 address will be bogus in the psymtab. Make it always at least
23584 non-zero to not look as a variable garbage collected by linker
23585 which have DW_OP_addr 0. */
21ae7a4d
JK
23586 if (i < size)
23587 dwarf2_complex_location_expr_complaint ();
585861ea 23588 stack[stacki]++;
21ae7a4d
JK
23589 break;
23590
23591 case DW_OP_GNU_uninit:
23592 break;
23593
3019eac3 23594 case DW_OP_GNU_addr_index:
49f6c839 23595 case DW_OP_GNU_const_index:
3019eac3
DE
23596 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
23597 &bytes_read);
23598 i += bytes_read;
23599 break;
23600
21ae7a4d
JK
23601 default:
23602 {
f39c6ffd 23603 const char *name = get_DW_OP_name (op);
21ae7a4d
JK
23604
23605 if (name)
23606 complaint (&symfile_complaints, _("unsupported stack op: '%s'"),
23607 name);
23608 else
23609 complaint (&symfile_complaints, _("unsupported stack op: '%02x'"),
23610 op);
23611 }
23612
23613 return (stack[stacki]);
d53d4ac5 23614 }
3c6e0cb3 23615
21ae7a4d
JK
23616 /* Enforce maximum stack depth of SIZE-1 to avoid writing
23617 outside of the allocated space. Also enforce minimum>0. */
23618 if (stacki >= ARRAY_SIZE (stack) - 1)
23619 {
23620 complaint (&symfile_complaints,
23621 _("location description stack overflow"));
23622 return 0;
23623 }
23624
23625 if (stacki <= 0)
23626 {
23627 complaint (&symfile_complaints,
23628 _("location description stack underflow"));
23629 return 0;
23630 }
23631 }
23632 return (stack[stacki]);
c906108c
SS
23633}
23634
23635/* memory allocation interface */
23636
c906108c 23637static struct dwarf_block *
7b5a2f43 23638dwarf_alloc_block (struct dwarf2_cu *cu)
c906108c 23639{
8d749320 23640 return XOBNEW (&cu->comp_unit_obstack, struct dwarf_block);
c906108c
SS
23641}
23642
c906108c 23643static struct die_info *
b60c80d6 23644dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
c906108c
SS
23645{
23646 struct die_info *die;
b60c80d6
DJ
23647 size_t size = sizeof (struct die_info);
23648
23649 if (num_attrs > 1)
23650 size += (num_attrs - 1) * sizeof (struct attribute);
c906108c 23651
b60c80d6 23652 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
c906108c
SS
23653 memset (die, 0, sizeof (struct die_info));
23654 return (die);
23655}
2e276125
JB
23656
23657\f
23658/* Macro support. */
23659
233d95b5
JK
23660/* Return file name relative to the compilation directory of file number I in
23661 *LH's file name table. The result is allocated using xmalloc; the caller is
2e276125 23662 responsible for freeing it. */
233d95b5 23663
2e276125 23664static char *
233d95b5 23665file_file_name (int file, struct line_header *lh)
2e276125 23666{
6a83a1e6
EZ
23667 /* Is the file number a valid index into the line header's file name
23668 table? Remember that file numbers start with one, not zero. */
fff8551c 23669 if (1 <= file && file <= lh->file_names.size ())
6a83a1e6 23670 {
8c43009f 23671 const file_entry &fe = lh->file_names[file - 1];
6e70227d 23672
8c43009f
PA
23673 if (!IS_ABSOLUTE_PATH (fe.name))
23674 {
23675 const char *dir = fe.include_dir (lh);
23676 if (dir != NULL)
23677 return concat (dir, SLASH_STRING, fe.name, (char *) NULL);
23678 }
23679 return xstrdup (fe.name);
6a83a1e6 23680 }
2e276125
JB
23681 else
23682 {
6a83a1e6
EZ
23683 /* The compiler produced a bogus file number. We can at least
23684 record the macro definitions made in the file, even if we
23685 won't be able to find the file by name. */
23686 char fake_name[80];
9a619af0 23687
8c042590
PM
23688 xsnprintf (fake_name, sizeof (fake_name),
23689 "<bad macro file number %d>", file);
2e276125 23690
6e70227d 23691 complaint (&symfile_complaints,
6a83a1e6
EZ
23692 _("bad file number in macro information (%d)"),
23693 file);
2e276125 23694
6a83a1e6 23695 return xstrdup (fake_name);
2e276125
JB
23696 }
23697}
23698
233d95b5
JK
23699/* Return the full name of file number I in *LH's file name table.
23700 Use COMP_DIR as the name of the current directory of the
23701 compilation. The result is allocated using xmalloc; the caller is
23702 responsible for freeing it. */
23703static char *
23704file_full_name (int file, struct line_header *lh, const char *comp_dir)
23705{
23706 /* Is the file number a valid index into the line header's file name
23707 table? Remember that file numbers start with one, not zero. */
fff8551c 23708 if (1 <= file && file <= lh->file_names.size ())
233d95b5
JK
23709 {
23710 char *relative = file_file_name (file, lh);
23711
23712 if (IS_ABSOLUTE_PATH (relative) || comp_dir == NULL)
23713 return relative;
b36cec19
PA
23714 return reconcat (relative, comp_dir, SLASH_STRING,
23715 relative, (char *) NULL);
233d95b5
JK
23716 }
23717 else
23718 return file_file_name (file, lh);
23719}
23720
2e276125
JB
23721
23722static struct macro_source_file *
23723macro_start_file (int file, int line,
23724 struct macro_source_file *current_file,
43f3e411 23725 struct line_header *lh)
2e276125 23726{
233d95b5
JK
23727 /* File name relative to the compilation directory of this source file. */
23728 char *file_name = file_file_name (file, lh);
2e276125 23729
2e276125 23730 if (! current_file)
abc9d0dc 23731 {
fc474241
DE
23732 /* Note: We don't create a macro table for this compilation unit
23733 at all until we actually get a filename. */
43f3e411 23734 struct macro_table *macro_table = get_macro_table ();
fc474241 23735
abc9d0dc
TT
23736 /* If we have no current file, then this must be the start_file
23737 directive for the compilation unit's main source file. */
fc474241
DE
23738 current_file = macro_set_main (macro_table, file_name);
23739 macro_define_special (macro_table);
abc9d0dc 23740 }
2e276125 23741 else
233d95b5 23742 current_file = macro_include (current_file, line, file_name);
2e276125 23743
233d95b5 23744 xfree (file_name);
6e70227d 23745
2e276125
JB
23746 return current_file;
23747}
23748
2e276125
JB
23749static const char *
23750consume_improper_spaces (const char *p, const char *body)
23751{
23752 if (*p == ' ')
23753 {
4d3c2250 23754 complaint (&symfile_complaints,
3e43a32a
MS
23755 _("macro definition contains spaces "
23756 "in formal argument list:\n`%s'"),
4d3c2250 23757 body);
2e276125
JB
23758
23759 while (*p == ' ')
23760 p++;
23761 }
23762
23763 return p;
23764}
23765
23766
23767static void
23768parse_macro_definition (struct macro_source_file *file, int line,
23769 const char *body)
23770{
23771 const char *p;
23772
23773 /* The body string takes one of two forms. For object-like macro
23774 definitions, it should be:
23775
23776 <macro name> " " <definition>
23777
23778 For function-like macro definitions, it should be:
23779
23780 <macro name> "() " <definition>
23781 or
23782 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
23783
23784 Spaces may appear only where explicitly indicated, and in the
23785 <definition>.
23786
23787 The Dwarf 2 spec says that an object-like macro's name is always
23788 followed by a space, but versions of GCC around March 2002 omit
6e70227d 23789 the space when the macro's definition is the empty string.
2e276125
JB
23790
23791 The Dwarf 2 spec says that there should be no spaces between the
23792 formal arguments in a function-like macro's formal argument list,
23793 but versions of GCC around March 2002 include spaces after the
23794 commas. */
23795
23796
23797 /* Find the extent of the macro name. The macro name is terminated
23798 by either a space or null character (for an object-like macro) or
23799 an opening paren (for a function-like macro). */
23800 for (p = body; *p; p++)
23801 if (*p == ' ' || *p == '(')
23802 break;
23803
23804 if (*p == ' ' || *p == '\0')
23805 {
23806 /* It's an object-like macro. */
23807 int name_len = p - body;
3f8a7804 23808 char *name = savestring (body, name_len);
2e276125
JB
23809 const char *replacement;
23810
23811 if (*p == ' ')
23812 replacement = body + name_len + 1;
23813 else
23814 {
4d3c2250 23815 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
23816 replacement = body + name_len;
23817 }
6e70227d 23818
2e276125
JB
23819 macro_define_object (file, line, name, replacement);
23820
23821 xfree (name);
23822 }
23823 else if (*p == '(')
23824 {
23825 /* It's a function-like macro. */
3f8a7804 23826 char *name = savestring (body, p - body);
2e276125
JB
23827 int argc = 0;
23828 int argv_size = 1;
8d749320 23829 char **argv = XNEWVEC (char *, argv_size);
2e276125
JB
23830
23831 p++;
23832
23833 p = consume_improper_spaces (p, body);
23834
23835 /* Parse the formal argument list. */
23836 while (*p && *p != ')')
23837 {
23838 /* Find the extent of the current argument name. */
23839 const char *arg_start = p;
23840
23841 while (*p && *p != ',' && *p != ')' && *p != ' ')
23842 p++;
23843
23844 if (! *p || p == arg_start)
4d3c2250 23845 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
23846 else
23847 {
23848 /* Make sure argv has room for the new argument. */
23849 if (argc >= argv_size)
23850 {
23851 argv_size *= 2;
224c3ddb 23852 argv = XRESIZEVEC (char *, argv, argv_size);
2e276125
JB
23853 }
23854
3f8a7804 23855 argv[argc++] = savestring (arg_start, p - arg_start);
2e276125
JB
23856 }
23857
23858 p = consume_improper_spaces (p, body);
23859
23860 /* Consume the comma, if present. */
23861 if (*p == ',')
23862 {
23863 p++;
23864
23865 p = consume_improper_spaces (p, body);
23866 }
23867 }
23868
23869 if (*p == ')')
23870 {
23871 p++;
23872
23873 if (*p == ' ')
23874 /* Perfectly formed definition, no complaints. */
23875 macro_define_function (file, line, name,
6e70227d 23876 argc, (const char **) argv,
2e276125
JB
23877 p + 1);
23878 else if (*p == '\0')
23879 {
23880 /* Complain, but do define it. */
4d3c2250 23881 dwarf2_macro_malformed_definition_complaint (body);
2e276125 23882 macro_define_function (file, line, name,
6e70227d 23883 argc, (const char **) argv,
2e276125
JB
23884 p);
23885 }
23886 else
23887 /* Just complain. */
4d3c2250 23888 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
23889 }
23890 else
23891 /* Just complain. */
4d3c2250 23892 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
23893
23894 xfree (name);
23895 {
23896 int i;
23897
23898 for (i = 0; i < argc; i++)
23899 xfree (argv[i]);
23900 }
23901 xfree (argv);
23902 }
23903 else
4d3c2250 23904 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
23905}
23906
cf2c3c16
TT
23907/* Skip some bytes from BYTES according to the form given in FORM.
23908 Returns the new pointer. */
2e276125 23909
d521ce57
TT
23910static const gdb_byte *
23911skip_form_bytes (bfd *abfd, const gdb_byte *bytes, const gdb_byte *buffer_end,
cf2c3c16
TT
23912 enum dwarf_form form,
23913 unsigned int offset_size,
23914 struct dwarf2_section_info *section)
2e276125 23915{
cf2c3c16 23916 unsigned int bytes_read;
2e276125 23917
cf2c3c16 23918 switch (form)
2e276125 23919 {
cf2c3c16
TT
23920 case DW_FORM_data1:
23921 case DW_FORM_flag:
23922 ++bytes;
23923 break;
23924
23925 case DW_FORM_data2:
23926 bytes += 2;
23927 break;
23928
23929 case DW_FORM_data4:
23930 bytes += 4;
23931 break;
23932
23933 case DW_FORM_data8:
23934 bytes += 8;
23935 break;
23936
0224619f
JK
23937 case DW_FORM_data16:
23938 bytes += 16;
23939 break;
23940
cf2c3c16
TT
23941 case DW_FORM_string:
23942 read_direct_string (abfd, bytes, &bytes_read);
23943 bytes += bytes_read;
23944 break;
23945
23946 case DW_FORM_sec_offset:
23947 case DW_FORM_strp:
36586728 23948 case DW_FORM_GNU_strp_alt:
cf2c3c16
TT
23949 bytes += offset_size;
23950 break;
23951
23952 case DW_FORM_block:
23953 bytes += read_unsigned_leb128 (abfd, bytes, &bytes_read);
23954 bytes += bytes_read;
23955 break;
23956
23957 case DW_FORM_block1:
23958 bytes += 1 + read_1_byte (abfd, bytes);
23959 break;
23960 case DW_FORM_block2:
23961 bytes += 2 + read_2_bytes (abfd, bytes);
23962 break;
23963 case DW_FORM_block4:
23964 bytes += 4 + read_4_bytes (abfd, bytes);
23965 break;
23966
23967 case DW_FORM_sdata:
23968 case DW_FORM_udata:
3019eac3
DE
23969 case DW_FORM_GNU_addr_index:
23970 case DW_FORM_GNU_str_index:
d521ce57 23971 bytes = gdb_skip_leb128 (bytes, buffer_end);
f664829e
DE
23972 if (bytes == NULL)
23973 {
23974 dwarf2_section_buffer_overflow_complaint (section);
23975 return NULL;
23976 }
cf2c3c16
TT
23977 break;
23978
663c44ac
JK
23979 case DW_FORM_implicit_const:
23980 break;
23981
cf2c3c16
TT
23982 default:
23983 {
cf2c3c16
TT
23984 complaint (&symfile_complaints,
23985 _("invalid form 0x%x in `%s'"),
a32a8923 23986 form, get_section_name (section));
cf2c3c16
TT
23987 return NULL;
23988 }
2e276125
JB
23989 }
23990
cf2c3c16
TT
23991 return bytes;
23992}
757a13d0 23993
cf2c3c16
TT
23994/* A helper for dwarf_decode_macros that handles skipping an unknown
23995 opcode. Returns an updated pointer to the macro data buffer; or,
23996 on error, issues a complaint and returns NULL. */
757a13d0 23997
d521ce57 23998static const gdb_byte *
cf2c3c16 23999skip_unknown_opcode (unsigned int opcode,
d521ce57
TT
24000 const gdb_byte **opcode_definitions,
24001 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
cf2c3c16
TT
24002 bfd *abfd,
24003 unsigned int offset_size,
24004 struct dwarf2_section_info *section)
24005{
24006 unsigned int bytes_read, i;
24007 unsigned long arg;
d521ce57 24008 const gdb_byte *defn;
2e276125 24009
cf2c3c16 24010 if (opcode_definitions[opcode] == NULL)
2e276125 24011 {
cf2c3c16
TT
24012 complaint (&symfile_complaints,
24013 _("unrecognized DW_MACFINO opcode 0x%x"),
24014 opcode);
24015 return NULL;
24016 }
2e276125 24017
cf2c3c16
TT
24018 defn = opcode_definitions[opcode];
24019 arg = read_unsigned_leb128 (abfd, defn, &bytes_read);
24020 defn += bytes_read;
2e276125 24021
cf2c3c16
TT
24022 for (i = 0; i < arg; ++i)
24023 {
aead7601
SM
24024 mac_ptr = skip_form_bytes (abfd, mac_ptr, mac_end,
24025 (enum dwarf_form) defn[i], offset_size,
f664829e 24026 section);
cf2c3c16
TT
24027 if (mac_ptr == NULL)
24028 {
24029 /* skip_form_bytes already issued the complaint. */
24030 return NULL;
24031 }
24032 }
757a13d0 24033
cf2c3c16
TT
24034 return mac_ptr;
24035}
757a13d0 24036
cf2c3c16
TT
24037/* A helper function which parses the header of a macro section.
24038 If the macro section is the extended (for now called "GNU") type,
24039 then this updates *OFFSET_SIZE. Returns a pointer to just after
24040 the header, or issues a complaint and returns NULL on error. */
757a13d0 24041
d521ce57
TT
24042static const gdb_byte *
24043dwarf_parse_macro_header (const gdb_byte **opcode_definitions,
cf2c3c16 24044 bfd *abfd,
d521ce57 24045 const gdb_byte *mac_ptr,
cf2c3c16
TT
24046 unsigned int *offset_size,
24047 int section_is_gnu)
24048{
24049 memset (opcode_definitions, 0, 256 * sizeof (gdb_byte *));
757a13d0 24050
cf2c3c16
TT
24051 if (section_is_gnu)
24052 {
24053 unsigned int version, flags;
757a13d0 24054
cf2c3c16 24055 version = read_2_bytes (abfd, mac_ptr);
0af92d60 24056 if (version != 4 && version != 5)
cf2c3c16
TT
24057 {
24058 complaint (&symfile_complaints,
24059 _("unrecognized version `%d' in .debug_macro section"),
24060 version);
24061 return NULL;
24062 }
24063 mac_ptr += 2;
757a13d0 24064
cf2c3c16
TT
24065 flags = read_1_byte (abfd, mac_ptr);
24066 ++mac_ptr;
24067 *offset_size = (flags & 1) ? 8 : 4;
757a13d0 24068
cf2c3c16
TT
24069 if ((flags & 2) != 0)
24070 /* We don't need the line table offset. */
24071 mac_ptr += *offset_size;
757a13d0 24072
cf2c3c16
TT
24073 /* Vendor opcode descriptions. */
24074 if ((flags & 4) != 0)
24075 {
24076 unsigned int i, count;
757a13d0 24077
cf2c3c16
TT
24078 count = read_1_byte (abfd, mac_ptr);
24079 ++mac_ptr;
24080 for (i = 0; i < count; ++i)
24081 {
24082 unsigned int opcode, bytes_read;
24083 unsigned long arg;
24084
24085 opcode = read_1_byte (abfd, mac_ptr);
24086 ++mac_ptr;
24087 opcode_definitions[opcode] = mac_ptr;
24088 arg = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24089 mac_ptr += bytes_read;
24090 mac_ptr += arg;
24091 }
757a13d0 24092 }
cf2c3c16 24093 }
757a13d0 24094
cf2c3c16
TT
24095 return mac_ptr;
24096}
757a13d0 24097
cf2c3c16 24098/* A helper for dwarf_decode_macros that handles the GNU extensions,
0af92d60 24099 including DW_MACRO_import. */
cf2c3c16
TT
24100
24101static void
d521ce57
TT
24102dwarf_decode_macro_bytes (bfd *abfd,
24103 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
cf2c3c16 24104 struct macro_source_file *current_file,
43f3e411 24105 struct line_header *lh,
cf2c3c16 24106 struct dwarf2_section_info *section,
36586728 24107 int section_is_gnu, int section_is_dwz,
cf2c3c16 24108 unsigned int offset_size,
8fc3fc34 24109 htab_t include_hash)
cf2c3c16 24110{
4d663531 24111 struct objfile *objfile = dwarf2_per_objfile->objfile;
cf2c3c16
TT
24112 enum dwarf_macro_record_type macinfo_type;
24113 int at_commandline;
d521ce57 24114 const gdb_byte *opcode_definitions[256];
757a13d0 24115
cf2c3c16
TT
24116 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
24117 &offset_size, section_is_gnu);
24118 if (mac_ptr == NULL)
24119 {
24120 /* We already issued a complaint. */
24121 return;
24122 }
757a13d0
JK
24123
24124 /* Determines if GDB is still before first DW_MACINFO_start_file. If true
24125 GDB is still reading the definitions from command line. First
24126 DW_MACINFO_start_file will need to be ignored as it was already executed
24127 to create CURRENT_FILE for the main source holding also the command line
24128 definitions. On first met DW_MACINFO_start_file this flag is reset to
24129 normally execute all the remaining DW_MACINFO_start_file macinfos. */
24130
24131 at_commandline = 1;
24132
24133 do
24134 {
24135 /* Do we at least have room for a macinfo type byte? */
24136 if (mac_ptr >= mac_end)
24137 {
f664829e 24138 dwarf2_section_buffer_overflow_complaint (section);
757a13d0
JK
24139 break;
24140 }
24141
aead7601 24142 macinfo_type = (enum dwarf_macro_record_type) read_1_byte (abfd, mac_ptr);
757a13d0
JK
24143 mac_ptr++;
24144
cf2c3c16
TT
24145 /* Note that we rely on the fact that the corresponding GNU and
24146 DWARF constants are the same. */
757a13d0
JK
24147 switch (macinfo_type)
24148 {
24149 /* A zero macinfo type indicates the end of the macro
24150 information. */
24151 case 0:
24152 break;
2e276125 24153
0af92d60
JK
24154 case DW_MACRO_define:
24155 case DW_MACRO_undef:
24156 case DW_MACRO_define_strp:
24157 case DW_MACRO_undef_strp:
24158 case DW_MACRO_define_sup:
24159 case DW_MACRO_undef_sup:
2e276125 24160 {
891d2f0b 24161 unsigned int bytes_read;
2e276125 24162 int line;
d521ce57 24163 const char *body;
cf2c3c16 24164 int is_define;
2e276125 24165
cf2c3c16
TT
24166 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24167 mac_ptr += bytes_read;
24168
0af92d60
JK
24169 if (macinfo_type == DW_MACRO_define
24170 || macinfo_type == DW_MACRO_undef)
cf2c3c16
TT
24171 {
24172 body = read_direct_string (abfd, mac_ptr, &bytes_read);
24173 mac_ptr += bytes_read;
24174 }
24175 else
24176 {
24177 LONGEST str_offset;
24178
24179 str_offset = read_offset_1 (abfd, mac_ptr, offset_size);
24180 mac_ptr += offset_size;
2e276125 24181
0af92d60
JK
24182 if (macinfo_type == DW_MACRO_define_sup
24183 || macinfo_type == DW_MACRO_undef_sup
f7a35f02 24184 || section_is_dwz)
36586728
TT
24185 {
24186 struct dwz_file *dwz = dwarf2_get_dwz_file ();
24187
24188 body = read_indirect_string_from_dwz (dwz, str_offset);
24189 }
24190 else
24191 body = read_indirect_string_at_offset (abfd, str_offset);
cf2c3c16
TT
24192 }
24193
0af92d60
JK
24194 is_define = (macinfo_type == DW_MACRO_define
24195 || macinfo_type == DW_MACRO_define_strp
24196 || macinfo_type == DW_MACRO_define_sup);
2e276125 24197 if (! current_file)
757a13d0
JK
24198 {
24199 /* DWARF violation as no main source is present. */
24200 complaint (&symfile_complaints,
24201 _("debug info with no main source gives macro %s "
24202 "on line %d: %s"),
cf2c3c16
TT
24203 is_define ? _("definition") : _("undefinition"),
24204 line, body);
757a13d0
JK
24205 break;
24206 }
3e43a32a
MS
24207 if ((line == 0 && !at_commandline)
24208 || (line != 0 && at_commandline))
4d3c2250 24209 complaint (&symfile_complaints,
757a13d0
JK
24210 _("debug info gives %s macro %s with %s line %d: %s"),
24211 at_commandline ? _("command-line") : _("in-file"),
cf2c3c16 24212 is_define ? _("definition") : _("undefinition"),
757a13d0
JK
24213 line == 0 ? _("zero") : _("non-zero"), line, body);
24214
cf2c3c16 24215 if (is_define)
757a13d0 24216 parse_macro_definition (current_file, line, body);
cf2c3c16
TT
24217 else
24218 {
0af92d60
JK
24219 gdb_assert (macinfo_type == DW_MACRO_undef
24220 || macinfo_type == DW_MACRO_undef_strp
24221 || macinfo_type == DW_MACRO_undef_sup);
cf2c3c16
TT
24222 macro_undef (current_file, line, body);
24223 }
2e276125
JB
24224 }
24225 break;
24226
0af92d60 24227 case DW_MACRO_start_file:
2e276125 24228 {
891d2f0b 24229 unsigned int bytes_read;
2e276125
JB
24230 int line, file;
24231
24232 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24233 mac_ptr += bytes_read;
24234 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24235 mac_ptr += bytes_read;
24236
3e43a32a
MS
24237 if ((line == 0 && !at_commandline)
24238 || (line != 0 && at_commandline))
757a13d0
JK
24239 complaint (&symfile_complaints,
24240 _("debug info gives source %d included "
24241 "from %s at %s line %d"),
24242 file, at_commandline ? _("command-line") : _("file"),
24243 line == 0 ? _("zero") : _("non-zero"), line);
24244
24245 if (at_commandline)
24246 {
0af92d60 24247 /* This DW_MACRO_start_file was executed in the
cf2c3c16 24248 pass one. */
757a13d0
JK
24249 at_commandline = 0;
24250 }
24251 else
43f3e411 24252 current_file = macro_start_file (file, line, current_file, lh);
2e276125
JB
24253 }
24254 break;
24255
0af92d60 24256 case DW_MACRO_end_file:
2e276125 24257 if (! current_file)
4d3c2250 24258 complaint (&symfile_complaints,
3e43a32a
MS
24259 _("macro debug info has an unmatched "
24260 "`close_file' directive"));
2e276125
JB
24261 else
24262 {
24263 current_file = current_file->included_by;
24264 if (! current_file)
24265 {
cf2c3c16 24266 enum dwarf_macro_record_type next_type;
2e276125
JB
24267
24268 /* GCC circa March 2002 doesn't produce the zero
24269 type byte marking the end of the compilation
24270 unit. Complain if it's not there, but exit no
24271 matter what. */
24272
24273 /* Do we at least have room for a macinfo type byte? */
24274 if (mac_ptr >= mac_end)
24275 {
f664829e 24276 dwarf2_section_buffer_overflow_complaint (section);
2e276125
JB
24277 return;
24278 }
24279
24280 /* We don't increment mac_ptr here, so this is just
24281 a look-ahead. */
aead7601
SM
24282 next_type
24283 = (enum dwarf_macro_record_type) read_1_byte (abfd,
24284 mac_ptr);
2e276125 24285 if (next_type != 0)
4d3c2250 24286 complaint (&symfile_complaints,
3e43a32a
MS
24287 _("no terminating 0-type entry for "
24288 "macros in `.debug_macinfo' section"));
2e276125
JB
24289
24290 return;
24291 }
24292 }
24293 break;
24294
0af92d60
JK
24295 case DW_MACRO_import:
24296 case DW_MACRO_import_sup:
cf2c3c16
TT
24297 {
24298 LONGEST offset;
8fc3fc34 24299 void **slot;
a036ba48
TT
24300 bfd *include_bfd = abfd;
24301 struct dwarf2_section_info *include_section = section;
d521ce57 24302 const gdb_byte *include_mac_end = mac_end;
a036ba48 24303 int is_dwz = section_is_dwz;
d521ce57 24304 const gdb_byte *new_mac_ptr;
cf2c3c16
TT
24305
24306 offset = read_offset_1 (abfd, mac_ptr, offset_size);
24307 mac_ptr += offset_size;
24308
0af92d60 24309 if (macinfo_type == DW_MACRO_import_sup)
a036ba48
TT
24310 {
24311 struct dwz_file *dwz = dwarf2_get_dwz_file ();
24312
4d663531 24313 dwarf2_read_section (objfile, &dwz->macro);
a036ba48 24314
a036ba48 24315 include_section = &dwz->macro;
a32a8923 24316 include_bfd = get_section_bfd_owner (include_section);
a036ba48
TT
24317 include_mac_end = dwz->macro.buffer + dwz->macro.size;
24318 is_dwz = 1;
24319 }
24320
24321 new_mac_ptr = include_section->buffer + offset;
24322 slot = htab_find_slot (include_hash, new_mac_ptr, INSERT);
24323
8fc3fc34
TT
24324 if (*slot != NULL)
24325 {
24326 /* This has actually happened; see
24327 http://sourceware.org/bugzilla/show_bug.cgi?id=13568. */
24328 complaint (&symfile_complaints,
0af92d60 24329 _("recursive DW_MACRO_import in "
8fc3fc34
TT
24330 ".debug_macro section"));
24331 }
24332 else
24333 {
d521ce57 24334 *slot = (void *) new_mac_ptr;
36586728 24335
a036ba48 24336 dwarf_decode_macro_bytes (include_bfd, new_mac_ptr,
43f3e411 24337 include_mac_end, current_file, lh,
36586728 24338 section, section_is_gnu, is_dwz,
4d663531 24339 offset_size, include_hash);
8fc3fc34 24340
d521ce57 24341 htab_remove_elt (include_hash, (void *) new_mac_ptr);
8fc3fc34 24342 }
cf2c3c16
TT
24343 }
24344 break;
24345
2e276125 24346 case DW_MACINFO_vendor_ext:
cf2c3c16
TT
24347 if (!section_is_gnu)
24348 {
24349 unsigned int bytes_read;
2e276125 24350
ac298888
TT
24351 /* This reads the constant, but since we don't recognize
24352 any vendor extensions, we ignore it. */
24353 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
cf2c3c16
TT
24354 mac_ptr += bytes_read;
24355 read_direct_string (abfd, mac_ptr, &bytes_read);
24356 mac_ptr += bytes_read;
2e276125 24357
cf2c3c16
TT
24358 /* We don't recognize any vendor extensions. */
24359 break;
24360 }
24361 /* FALLTHROUGH */
24362
24363 default:
24364 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
f664829e 24365 mac_ptr, mac_end, abfd, offset_size,
cf2c3c16
TT
24366 section);
24367 if (mac_ptr == NULL)
24368 return;
24369 break;
2e276125 24370 }
757a13d0 24371 } while (macinfo_type != 0);
2e276125 24372}
8e19ed76 24373
cf2c3c16 24374static void
09262596 24375dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
43f3e411 24376 int section_is_gnu)
cf2c3c16 24377{
bb5ed363 24378 struct objfile *objfile = dwarf2_per_objfile->objfile;
09262596
DE
24379 struct line_header *lh = cu->line_header;
24380 bfd *abfd;
d521ce57 24381 const gdb_byte *mac_ptr, *mac_end;
cf2c3c16
TT
24382 struct macro_source_file *current_file = 0;
24383 enum dwarf_macro_record_type macinfo_type;
24384 unsigned int offset_size = cu->header.offset_size;
d521ce57 24385 const gdb_byte *opcode_definitions[256];
8fc3fc34 24386 void **slot;
09262596
DE
24387 struct dwarf2_section_info *section;
24388 const char *section_name;
24389
24390 if (cu->dwo_unit != NULL)
24391 {
24392 if (section_is_gnu)
24393 {
24394 section = &cu->dwo_unit->dwo_file->sections.macro;
24395 section_name = ".debug_macro.dwo";
24396 }
24397 else
24398 {
24399 section = &cu->dwo_unit->dwo_file->sections.macinfo;
24400 section_name = ".debug_macinfo.dwo";
24401 }
24402 }
24403 else
24404 {
24405 if (section_is_gnu)
24406 {
24407 section = &dwarf2_per_objfile->macro;
24408 section_name = ".debug_macro";
24409 }
24410 else
24411 {
24412 section = &dwarf2_per_objfile->macinfo;
24413 section_name = ".debug_macinfo";
24414 }
24415 }
cf2c3c16 24416
bb5ed363 24417 dwarf2_read_section (objfile, section);
cf2c3c16
TT
24418 if (section->buffer == NULL)
24419 {
fceca515 24420 complaint (&symfile_complaints, _("missing %s section"), section_name);
cf2c3c16
TT
24421 return;
24422 }
a32a8923 24423 abfd = get_section_bfd_owner (section);
cf2c3c16
TT
24424
24425 /* First pass: Find the name of the base filename.
24426 This filename is needed in order to process all macros whose definition
24427 (or undefinition) comes from the command line. These macros are defined
24428 before the first DW_MACINFO_start_file entry, and yet still need to be
24429 associated to the base file.
24430
24431 To determine the base file name, we scan the macro definitions until we
24432 reach the first DW_MACINFO_start_file entry. We then initialize
24433 CURRENT_FILE accordingly so that any macro definition found before the
24434 first DW_MACINFO_start_file can still be associated to the base file. */
24435
24436 mac_ptr = section->buffer + offset;
24437 mac_end = section->buffer + section->size;
24438
24439 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
24440 &offset_size, section_is_gnu);
24441 if (mac_ptr == NULL)
24442 {
24443 /* We already issued a complaint. */
24444 return;
24445 }
24446
24447 do
24448 {
24449 /* Do we at least have room for a macinfo type byte? */
24450 if (mac_ptr >= mac_end)
24451 {
24452 /* Complaint is printed during the second pass as GDB will probably
24453 stop the first pass earlier upon finding
24454 DW_MACINFO_start_file. */
24455 break;
24456 }
24457
aead7601 24458 macinfo_type = (enum dwarf_macro_record_type) read_1_byte (abfd, mac_ptr);
cf2c3c16
TT
24459 mac_ptr++;
24460
24461 /* Note that we rely on the fact that the corresponding GNU and
24462 DWARF constants are the same. */
24463 switch (macinfo_type)
24464 {
24465 /* A zero macinfo type indicates the end of the macro
24466 information. */
24467 case 0:
24468 break;
24469
0af92d60
JK
24470 case DW_MACRO_define:
24471 case DW_MACRO_undef:
cf2c3c16
TT
24472 /* Only skip the data by MAC_PTR. */
24473 {
24474 unsigned int bytes_read;
24475
24476 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24477 mac_ptr += bytes_read;
24478 read_direct_string (abfd, mac_ptr, &bytes_read);
24479 mac_ptr += bytes_read;
24480 }
24481 break;
24482
0af92d60 24483 case DW_MACRO_start_file:
cf2c3c16
TT
24484 {
24485 unsigned int bytes_read;
24486 int line, file;
24487
24488 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24489 mac_ptr += bytes_read;
24490 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24491 mac_ptr += bytes_read;
24492
43f3e411 24493 current_file = macro_start_file (file, line, current_file, lh);
cf2c3c16
TT
24494 }
24495 break;
24496
0af92d60 24497 case DW_MACRO_end_file:
cf2c3c16
TT
24498 /* No data to skip by MAC_PTR. */
24499 break;
24500
0af92d60
JK
24501 case DW_MACRO_define_strp:
24502 case DW_MACRO_undef_strp:
24503 case DW_MACRO_define_sup:
24504 case DW_MACRO_undef_sup:
cf2c3c16
TT
24505 {
24506 unsigned int bytes_read;
24507
24508 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24509 mac_ptr += bytes_read;
24510 mac_ptr += offset_size;
24511 }
24512 break;
24513
0af92d60
JK
24514 case DW_MACRO_import:
24515 case DW_MACRO_import_sup:
cf2c3c16 24516 /* Note that, according to the spec, a transparent include
0af92d60 24517 chain cannot call DW_MACRO_start_file. So, we can just
cf2c3c16
TT
24518 skip this opcode. */
24519 mac_ptr += offset_size;
24520 break;
24521
24522 case DW_MACINFO_vendor_ext:
24523 /* Only skip the data by MAC_PTR. */
24524 if (!section_is_gnu)
24525 {
24526 unsigned int bytes_read;
24527
24528 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24529 mac_ptr += bytes_read;
24530 read_direct_string (abfd, mac_ptr, &bytes_read);
24531 mac_ptr += bytes_read;
24532 }
24533 /* FALLTHROUGH */
24534
24535 default:
24536 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
f664829e 24537 mac_ptr, mac_end, abfd, offset_size,
cf2c3c16
TT
24538 section);
24539 if (mac_ptr == NULL)
24540 return;
24541 break;
24542 }
24543 } while (macinfo_type != 0 && current_file == NULL);
24544
24545 /* Second pass: Process all entries.
24546
24547 Use the AT_COMMAND_LINE flag to determine whether we are still processing
24548 command-line macro definitions/undefinitions. This flag is unset when we
24549 reach the first DW_MACINFO_start_file entry. */
24550
fc4007c9
TT
24551 htab_up include_hash (htab_create_alloc (1, htab_hash_pointer,
24552 htab_eq_pointer,
24553 NULL, xcalloc, xfree));
8fc3fc34 24554 mac_ptr = section->buffer + offset;
fc4007c9 24555 slot = htab_find_slot (include_hash.get (), mac_ptr, INSERT);
d521ce57 24556 *slot = (void *) mac_ptr;
8fc3fc34 24557 dwarf_decode_macro_bytes (abfd, mac_ptr, mac_end,
43f3e411 24558 current_file, lh, section,
fc4007c9
TT
24559 section_is_gnu, 0, offset_size,
24560 include_hash.get ());
cf2c3c16
TT
24561}
24562
8e19ed76 24563/* Check if the attribute's form is a DW_FORM_block*
0963b4bd 24564 if so return true else false. */
380bca97 24565
8e19ed76 24566static int
6e5a29e1 24567attr_form_is_block (const struct attribute *attr)
8e19ed76
PS
24568{
24569 return (attr == NULL ? 0 :
24570 attr->form == DW_FORM_block1
24571 || attr->form == DW_FORM_block2
24572 || attr->form == DW_FORM_block4
2dc7f7b3
TT
24573 || attr->form == DW_FORM_block
24574 || attr->form == DW_FORM_exprloc);
8e19ed76 24575}
4c2df51b 24576
c6a0999f
JB
24577/* Return non-zero if ATTR's value is a section offset --- classes
24578 lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
24579 You may use DW_UNSND (attr) to retrieve such offsets.
24580
24581 Section 7.5.4, "Attribute Encodings", explains that no attribute
24582 may have a value that belongs to more than one of these classes; it
24583 would be ambiguous if we did, because we use the same forms for all
24584 of them. */
380bca97 24585
3690dd37 24586static int
6e5a29e1 24587attr_form_is_section_offset (const struct attribute *attr)
3690dd37
JB
24588{
24589 return (attr->form == DW_FORM_data4
2dc7f7b3
TT
24590 || attr->form == DW_FORM_data8
24591 || attr->form == DW_FORM_sec_offset);
3690dd37
JB
24592}
24593
3690dd37
JB
24594/* Return non-zero if ATTR's value falls in the 'constant' class, or
24595 zero otherwise. When this function returns true, you can apply
24596 dwarf2_get_attr_constant_value to it.
24597
24598 However, note that for some attributes you must check
24599 attr_form_is_section_offset before using this test. DW_FORM_data4
24600 and DW_FORM_data8 are members of both the constant class, and of
24601 the classes that contain offsets into other debug sections
24602 (lineptr, loclistptr, macptr or rangelistptr). The DWARF spec says
24603 that, if an attribute's can be either a constant or one of the
24604 section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
0224619f
JK
24605 taken as section offsets, not constants.
24606
24607 DW_FORM_data16 is not considered as dwarf2_get_attr_constant_value
24608 cannot handle that. */
380bca97 24609
3690dd37 24610static int
6e5a29e1 24611attr_form_is_constant (const struct attribute *attr)
3690dd37
JB
24612{
24613 switch (attr->form)
24614 {
24615 case DW_FORM_sdata:
24616 case DW_FORM_udata:
24617 case DW_FORM_data1:
24618 case DW_FORM_data2:
24619 case DW_FORM_data4:
24620 case DW_FORM_data8:
663c44ac 24621 case DW_FORM_implicit_const:
3690dd37
JB
24622 return 1;
24623 default:
24624 return 0;
24625 }
24626}
24627
7771576e
SA
24628
24629/* DW_ADDR is always stored already as sect_offset; despite for the forms
24630 besides DW_FORM_ref_addr it is stored as cu_offset in the DWARF file. */
24631
24632static int
6e5a29e1 24633attr_form_is_ref (const struct attribute *attr)
7771576e
SA
24634{
24635 switch (attr->form)
24636 {
24637 case DW_FORM_ref_addr:
24638 case DW_FORM_ref1:
24639 case DW_FORM_ref2:
24640 case DW_FORM_ref4:
24641 case DW_FORM_ref8:
24642 case DW_FORM_ref_udata:
24643 case DW_FORM_GNU_ref_alt:
24644 return 1;
24645 default:
24646 return 0;
24647 }
24648}
24649
3019eac3
DE
24650/* Return the .debug_loc section to use for CU.
24651 For DWO files use .debug_loc.dwo. */
24652
24653static struct dwarf2_section_info *
24654cu_debug_loc_section (struct dwarf2_cu *cu)
24655{
24656 if (cu->dwo_unit)
43988095
JK
24657 {
24658 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
24659
24660 return cu->header.version >= 5 ? &sections->loclists : &sections->loc;
24661 }
24662 return (cu->header.version >= 5 ? &dwarf2_per_objfile->loclists
24663 : &dwarf2_per_objfile->loc);
3019eac3
DE
24664}
24665
8cf6f0b1
TT
24666/* A helper function that fills in a dwarf2_loclist_baton. */
24667
24668static void
24669fill_in_loclist_baton (struct dwarf2_cu *cu,
24670 struct dwarf2_loclist_baton *baton,
ff39bb5e 24671 const struct attribute *attr)
8cf6f0b1 24672{
3019eac3
DE
24673 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
24674
24675 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
8cf6f0b1
TT
24676
24677 baton->per_cu = cu->per_cu;
24678 gdb_assert (baton->per_cu);
24679 /* We don't know how long the location list is, but make sure we
24680 don't run off the edge of the section. */
3019eac3
DE
24681 baton->size = section->size - DW_UNSND (attr);
24682 baton->data = section->buffer + DW_UNSND (attr);
8cf6f0b1 24683 baton->base_address = cu->base_address;
f664829e 24684 baton->from_dwo = cu->dwo_unit != NULL;
8cf6f0b1
TT
24685}
24686
4c2df51b 24687static void
ff39bb5e 24688dwarf2_symbol_mark_computed (const struct attribute *attr, struct symbol *sym,
f1e6e072 24689 struct dwarf2_cu *cu, int is_block)
4c2df51b 24690{
bb5ed363 24691 struct objfile *objfile = dwarf2_per_objfile->objfile;
3019eac3 24692 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
bb5ed363 24693
3690dd37 24694 if (attr_form_is_section_offset (attr)
3019eac3 24695 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
99bcc461
DJ
24696 the section. If so, fall through to the complaint in the
24697 other branch. */
3019eac3 24698 && DW_UNSND (attr) < dwarf2_section_size (objfile, section))
4c2df51b 24699 {
0d53c4c4 24700 struct dwarf2_loclist_baton *baton;
4c2df51b 24701
8d749320 24702 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_loclist_baton);
4c2df51b 24703
8cf6f0b1 24704 fill_in_loclist_baton (cu, baton, attr);
be391dca 24705
d00adf39 24706 if (cu->base_known == 0)
0d53c4c4 24707 complaint (&symfile_complaints,
3e43a32a
MS
24708 _("Location list used without "
24709 "specifying the CU base address."));
4c2df51b 24710
f1e6e072
TT
24711 SYMBOL_ACLASS_INDEX (sym) = (is_block
24712 ? dwarf2_loclist_block_index
24713 : dwarf2_loclist_index);
0d53c4c4
DJ
24714 SYMBOL_LOCATION_BATON (sym) = baton;
24715 }
24716 else
24717 {
24718 struct dwarf2_locexpr_baton *baton;
24719
8d749320 24720 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
ae0d2f24
UW
24721 baton->per_cu = cu->per_cu;
24722 gdb_assert (baton->per_cu);
0d53c4c4
DJ
24723
24724 if (attr_form_is_block (attr))
24725 {
24726 /* Note that we're just copying the block's data pointer
24727 here, not the actual data. We're still pointing into the
6502dd73
DJ
24728 info_buffer for SYM's objfile; right now we never release
24729 that buffer, but when we do clean up properly this may
24730 need to change. */
0d53c4c4
DJ
24731 baton->size = DW_BLOCK (attr)->size;
24732 baton->data = DW_BLOCK (attr)->data;
24733 }
24734 else
24735 {
24736 dwarf2_invalid_attrib_class_complaint ("location description",
24737 SYMBOL_NATURAL_NAME (sym));
24738 baton->size = 0;
0d53c4c4 24739 }
6e70227d 24740
f1e6e072
TT
24741 SYMBOL_ACLASS_INDEX (sym) = (is_block
24742 ? dwarf2_locexpr_block_index
24743 : dwarf2_locexpr_index);
0d53c4c4
DJ
24744 SYMBOL_LOCATION_BATON (sym) = baton;
24745 }
4c2df51b 24746}
6502dd73 24747
9aa1f1e3
TT
24748/* Return the OBJFILE associated with the compilation unit CU. If CU
24749 came from a separate debuginfo file, then the master objfile is
24750 returned. */
ae0d2f24
UW
24751
24752struct objfile *
24753dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
24754{
9291a0cd 24755 struct objfile *objfile = per_cu->objfile;
ae0d2f24
UW
24756
24757 /* Return the master objfile, so that we can report and look up the
24758 correct file containing this variable. */
24759 if (objfile->separate_debug_objfile_backlink)
24760 objfile = objfile->separate_debug_objfile_backlink;
24761
24762 return objfile;
24763}
24764
96408a79
SA
24765/* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
24766 (CU_HEADERP is unused in such case) or prepare a temporary copy at
24767 CU_HEADERP first. */
24768
24769static const struct comp_unit_head *
24770per_cu_header_read_in (struct comp_unit_head *cu_headerp,
24771 struct dwarf2_per_cu_data *per_cu)
24772{
d521ce57 24773 const gdb_byte *info_ptr;
96408a79
SA
24774
24775 if (per_cu->cu)
24776 return &per_cu->cu->header;
24777
9c541725 24778 info_ptr = per_cu->section->buffer + to_underlying (per_cu->sect_off);
96408a79
SA
24779
24780 memset (cu_headerp, 0, sizeof (*cu_headerp));
43988095
JK
24781 read_comp_unit_head (cu_headerp, info_ptr, per_cu->section,
24782 rcuh_kind::COMPILE);
96408a79
SA
24783
24784 return cu_headerp;
24785}
24786
ae0d2f24
UW
24787/* Return the address size given in the compilation unit header for CU. */
24788
98714339 24789int
ae0d2f24
UW
24790dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
24791{
96408a79
SA
24792 struct comp_unit_head cu_header_local;
24793 const struct comp_unit_head *cu_headerp;
c471e790 24794
96408a79
SA
24795 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
24796
24797 return cu_headerp->addr_size;
ae0d2f24
UW
24798}
24799
9eae7c52
TT
24800/* Return the offset size given in the compilation unit header for CU. */
24801
24802int
24803dwarf2_per_cu_offset_size (struct dwarf2_per_cu_data *per_cu)
24804{
96408a79
SA
24805 struct comp_unit_head cu_header_local;
24806 const struct comp_unit_head *cu_headerp;
9c6c53f7 24807
96408a79
SA
24808 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
24809
24810 return cu_headerp->offset_size;
24811}
24812
24813/* See its dwarf2loc.h declaration. */
24814
24815int
24816dwarf2_per_cu_ref_addr_size (struct dwarf2_per_cu_data *per_cu)
24817{
24818 struct comp_unit_head cu_header_local;
24819 const struct comp_unit_head *cu_headerp;
24820
24821 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
24822
24823 if (cu_headerp->version == 2)
24824 return cu_headerp->addr_size;
24825 else
24826 return cu_headerp->offset_size;
181cebd4
JK
24827}
24828
9aa1f1e3
TT
24829/* Return the text offset of the CU. The returned offset comes from
24830 this CU's objfile. If this objfile came from a separate debuginfo
24831 file, then the offset may be different from the corresponding
24832 offset in the parent objfile. */
24833
24834CORE_ADDR
24835dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
24836{
bb3fa9d0 24837 struct objfile *objfile = per_cu->objfile;
9aa1f1e3
TT
24838
24839 return ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
24840}
24841
43988095
JK
24842/* Return DWARF version number of PER_CU. */
24843
24844short
24845dwarf2_version (struct dwarf2_per_cu_data *per_cu)
24846{
24847 return per_cu->dwarf_version;
24848}
24849
348e048f
DE
24850/* Locate the .debug_info compilation unit from CU's objfile which contains
24851 the DIE at OFFSET. Raises an error on failure. */
ae038cb0
DJ
24852
24853static struct dwarf2_per_cu_data *
9c541725 24854dwarf2_find_containing_comp_unit (sect_offset sect_off,
36586728 24855 unsigned int offset_in_dwz,
ae038cb0
DJ
24856 struct objfile *objfile)
24857{
24858 struct dwarf2_per_cu_data *this_cu;
24859 int low, high;
36586728 24860 const sect_offset *cu_off;
ae038cb0 24861
ae038cb0
DJ
24862 low = 0;
24863 high = dwarf2_per_objfile->n_comp_units - 1;
24864 while (high > low)
24865 {
36586728 24866 struct dwarf2_per_cu_data *mid_cu;
ae038cb0 24867 int mid = low + (high - low) / 2;
9a619af0 24868
36586728 24869 mid_cu = dwarf2_per_objfile->all_comp_units[mid];
9c541725 24870 cu_off = &mid_cu->sect_off;
36586728 24871 if (mid_cu->is_dwz > offset_in_dwz
9c541725 24872 || (mid_cu->is_dwz == offset_in_dwz && *cu_off >= sect_off))
ae038cb0
DJ
24873 high = mid;
24874 else
24875 low = mid + 1;
24876 }
24877 gdb_assert (low == high);
36586728 24878 this_cu = dwarf2_per_objfile->all_comp_units[low];
9c541725
PA
24879 cu_off = &this_cu->sect_off;
24880 if (this_cu->is_dwz != offset_in_dwz || *cu_off > sect_off)
ae038cb0 24881 {
36586728 24882 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
8a3fe4f8 24883 error (_("Dwarf Error: could not find partial DIE containing "
9c541725
PA
24884 "offset 0x%x [in module %s]"),
24885 to_underlying (sect_off), bfd_get_filename (objfile->obfd));
10b3939b 24886
9c541725
PA
24887 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->sect_off
24888 <= sect_off);
ae038cb0
DJ
24889 return dwarf2_per_objfile->all_comp_units[low-1];
24890 }
24891 else
24892 {
24893 this_cu = dwarf2_per_objfile->all_comp_units[low];
24894 if (low == dwarf2_per_objfile->n_comp_units - 1
9c541725
PA
24895 && sect_off >= this_cu->sect_off + this_cu->length)
24896 error (_("invalid dwarf2 offset %u"), to_underlying (sect_off));
24897 gdb_assert (sect_off < this_cu->sect_off + this_cu->length);
ae038cb0
DJ
24898 return this_cu;
24899 }
24900}
24901
23745b47 24902/* Initialize dwarf2_cu CU, owned by PER_CU. */
93311388 24903
9816fde3 24904static void
23745b47 24905init_one_comp_unit (struct dwarf2_cu *cu, struct dwarf2_per_cu_data *per_cu)
93311388 24906{
9816fde3 24907 memset (cu, 0, sizeof (*cu));
23745b47
DE
24908 per_cu->cu = cu;
24909 cu->per_cu = per_cu;
24910 cu->objfile = per_cu->objfile;
93311388 24911 obstack_init (&cu->comp_unit_obstack);
9816fde3
JK
24912}
24913
24914/* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
24915
24916static void
95554aad
TT
24917prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
24918 enum language pretend_language)
9816fde3
JK
24919{
24920 struct attribute *attr;
24921
24922 /* Set the language we're debugging. */
24923 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
24924 if (attr)
24925 set_cu_language (DW_UNSND (attr), cu);
24926 else
9cded63f 24927 {
95554aad 24928 cu->language = pretend_language;
9cded63f
TT
24929 cu->language_defn = language_def (cu->language);
24930 }
dee91e82 24931
7d45c7c3 24932 cu->producer = dwarf2_string_attr (comp_unit_die, DW_AT_producer, cu);
93311388
DE
24933}
24934
ae038cb0
DJ
24935/* Release one cached compilation unit, CU. We unlink it from the tree
24936 of compilation units, but we don't remove it from the read_in_chain;
93311388
DE
24937 the caller is responsible for that.
24938 NOTE: DATA is a void * because this function is also used as a
24939 cleanup routine. */
ae038cb0
DJ
24940
24941static void
68dc6402 24942free_heap_comp_unit (void *data)
ae038cb0 24943{
9a3c8263 24944 struct dwarf2_cu *cu = (struct dwarf2_cu *) data;
ae038cb0 24945
23745b47
DE
24946 gdb_assert (cu->per_cu != NULL);
24947 cu->per_cu->cu = NULL;
ae038cb0
DJ
24948 cu->per_cu = NULL;
24949
24950 obstack_free (&cu->comp_unit_obstack, NULL);
24951
24952 xfree (cu);
24953}
24954
72bf9492 24955/* This cleanup function is passed the address of a dwarf2_cu on the stack
ae038cb0 24956 when we're finished with it. We can't free the pointer itself, but be
dee91e82 24957 sure to unlink it from the cache. Also release any associated storage. */
72bf9492
DJ
24958
24959static void
24960free_stack_comp_unit (void *data)
24961{
9a3c8263 24962 struct dwarf2_cu *cu = (struct dwarf2_cu *) data;
72bf9492 24963
23745b47
DE
24964 gdb_assert (cu->per_cu != NULL);
24965 cu->per_cu->cu = NULL;
24966 cu->per_cu = NULL;
24967
72bf9492
DJ
24968 obstack_free (&cu->comp_unit_obstack, NULL);
24969 cu->partial_dies = NULL;
ae038cb0
DJ
24970}
24971
24972/* Free all cached compilation units. */
24973
24974static void
24975free_cached_comp_units (void *data)
24976{
330cdd98 24977 dwarf2_per_objfile->free_cached_comp_units ();
ae038cb0
DJ
24978}
24979
24980/* Increase the age counter on each cached compilation unit, and free
24981 any that are too old. */
24982
24983static void
24984age_cached_comp_units (void)
24985{
24986 struct dwarf2_per_cu_data *per_cu, **last_chain;
24987
24988 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
24989 per_cu = dwarf2_per_objfile->read_in_chain;
24990 while (per_cu != NULL)
24991 {
24992 per_cu->cu->last_used ++;
b4f54984 24993 if (per_cu->cu->last_used <= dwarf_max_cache_age)
ae038cb0
DJ
24994 dwarf2_mark (per_cu->cu);
24995 per_cu = per_cu->cu->read_in_chain;
24996 }
24997
24998 per_cu = dwarf2_per_objfile->read_in_chain;
24999 last_chain = &dwarf2_per_objfile->read_in_chain;
25000 while (per_cu != NULL)
25001 {
25002 struct dwarf2_per_cu_data *next_cu;
25003
25004 next_cu = per_cu->cu->read_in_chain;
25005
25006 if (!per_cu->cu->mark)
25007 {
68dc6402 25008 free_heap_comp_unit (per_cu->cu);
ae038cb0
DJ
25009 *last_chain = next_cu;
25010 }
25011 else
25012 last_chain = &per_cu->cu->read_in_chain;
25013
25014 per_cu = next_cu;
25015 }
25016}
25017
25018/* Remove a single compilation unit from the cache. */
25019
25020static void
dee91e82 25021free_one_cached_comp_unit (struct dwarf2_per_cu_data *target_per_cu)
ae038cb0
DJ
25022{
25023 struct dwarf2_per_cu_data *per_cu, **last_chain;
25024
25025 per_cu = dwarf2_per_objfile->read_in_chain;
25026 last_chain = &dwarf2_per_objfile->read_in_chain;
25027 while (per_cu != NULL)
25028 {
25029 struct dwarf2_per_cu_data *next_cu;
25030
25031 next_cu = per_cu->cu->read_in_chain;
25032
dee91e82 25033 if (per_cu == target_per_cu)
ae038cb0 25034 {
68dc6402 25035 free_heap_comp_unit (per_cu->cu);
dee91e82 25036 per_cu->cu = NULL;
ae038cb0
DJ
25037 *last_chain = next_cu;
25038 break;
25039 }
25040 else
25041 last_chain = &per_cu->cu->read_in_chain;
25042
25043 per_cu = next_cu;
25044 }
25045}
25046
fe3e1990
DJ
25047/* Release all extra memory associated with OBJFILE. */
25048
25049void
25050dwarf2_free_objfile (struct objfile *objfile)
25051{
9a3c8263
SM
25052 dwarf2_per_objfile
25053 = (struct dwarf2_per_objfile *) objfile_data (objfile,
25054 dwarf2_objfile_data_key);
fe3e1990
DJ
25055
25056 if (dwarf2_per_objfile == NULL)
25057 return;
25058
330cdd98 25059 dwarf2_per_objfile->~dwarf2_per_objfile ();
fe3e1990
DJ
25060}
25061
dee91e82
DE
25062/* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
25063 We store these in a hash table separate from the DIEs, and preserve them
25064 when the DIEs are flushed out of cache.
25065
25066 The CU "per_cu" pointer is needed because offset alone is not enough to
3019eac3 25067 uniquely identify the type. A file may have multiple .debug_types sections,
c88ee1f0
DE
25068 or the type may come from a DWO file. Furthermore, while it's more logical
25069 to use per_cu->section+offset, with Fission the section with the data is in
25070 the DWO file but we don't know that section at the point we need it.
25071 We have to use something in dwarf2_per_cu_data (or the pointer to it)
25072 because we can enter the lookup routine, get_die_type_at_offset, from
25073 outside this file, and thus won't necessarily have PER_CU->cu.
25074 Fortunately, PER_CU is stable for the life of the objfile. */
1c379e20 25075
dee91e82 25076struct dwarf2_per_cu_offset_and_type
1c379e20 25077{
dee91e82 25078 const struct dwarf2_per_cu_data *per_cu;
9c541725 25079 sect_offset sect_off;
1c379e20
DJ
25080 struct type *type;
25081};
25082
dee91e82 25083/* Hash function for a dwarf2_per_cu_offset_and_type. */
1c379e20
DJ
25084
25085static hashval_t
dee91e82 25086per_cu_offset_and_type_hash (const void *item)
1c379e20 25087{
9a3c8263
SM
25088 const struct dwarf2_per_cu_offset_and_type *ofs
25089 = (const struct dwarf2_per_cu_offset_and_type *) item;
9a619af0 25090
9c541725 25091 return (uintptr_t) ofs->per_cu + to_underlying (ofs->sect_off);
1c379e20
DJ
25092}
25093
dee91e82 25094/* Equality function for a dwarf2_per_cu_offset_and_type. */
1c379e20
DJ
25095
25096static int
dee91e82 25097per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
1c379e20 25098{
9a3c8263
SM
25099 const struct dwarf2_per_cu_offset_and_type *ofs_lhs
25100 = (const struct dwarf2_per_cu_offset_and_type *) item_lhs;
25101 const struct dwarf2_per_cu_offset_and_type *ofs_rhs
25102 = (const struct dwarf2_per_cu_offset_and_type *) item_rhs;
9a619af0 25103
dee91e82 25104 return (ofs_lhs->per_cu == ofs_rhs->per_cu
9c541725 25105 && ofs_lhs->sect_off == ofs_rhs->sect_off);
1c379e20
DJ
25106}
25107
25108/* Set the type associated with DIE to TYPE. Save it in CU's hash
7e314c57
JK
25109 table if necessary. For convenience, return TYPE.
25110
25111 The DIEs reading must have careful ordering to:
25112 * Not cause infite loops trying to read in DIEs as a prerequisite for
25113 reading current DIE.
25114 * Not trying to dereference contents of still incompletely read in types
25115 while reading in other DIEs.
25116 * Enable referencing still incompletely read in types just by a pointer to
25117 the type without accessing its fields.
25118
25119 Therefore caller should follow these rules:
25120 * Try to fetch any prerequisite types we may need to build this DIE type
25121 before building the type and calling set_die_type.
e71ec853 25122 * After building type call set_die_type for current DIE as soon as
7e314c57
JK
25123 possible before fetching more types to complete the current type.
25124 * Make the type as complete as possible before fetching more types. */
1c379e20 25125
f792889a 25126static struct type *
1c379e20
DJ
25127set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
25128{
dee91e82 25129 struct dwarf2_per_cu_offset_and_type **slot, ofs;
673bfd45 25130 struct objfile *objfile = cu->objfile;
3cdcd0ce
JB
25131 struct attribute *attr;
25132 struct dynamic_prop prop;
1c379e20 25133
b4ba55a1
JB
25134 /* For Ada types, make sure that the gnat-specific data is always
25135 initialized (if not already set). There are a few types where
25136 we should not be doing so, because the type-specific area is
25137 already used to hold some other piece of info (eg: TYPE_CODE_FLT
25138 where the type-specific area is used to store the floatformat).
25139 But this is not a problem, because the gnat-specific information
25140 is actually not needed for these types. */
25141 if (need_gnat_info (cu)
25142 && TYPE_CODE (type) != TYPE_CODE_FUNC
25143 && TYPE_CODE (type) != TYPE_CODE_FLT
09e2d7c7
DE
25144 && TYPE_CODE (type) != TYPE_CODE_METHODPTR
25145 && TYPE_CODE (type) != TYPE_CODE_MEMBERPTR
25146 && TYPE_CODE (type) != TYPE_CODE_METHOD
b4ba55a1
JB
25147 && !HAVE_GNAT_AUX_INFO (type))
25148 INIT_GNAT_SPECIFIC (type);
25149
3f2f83dd
KB
25150 /* Read DW_AT_allocated and set in type. */
25151 attr = dwarf2_attr (die, DW_AT_allocated, cu);
25152 if (attr_form_is_block (attr))
25153 {
25154 if (attr_to_dynamic_prop (attr, die, cu, &prop))
25155 add_dyn_prop (DYN_PROP_ALLOCATED, prop, type, objfile);
25156 }
25157 else if (attr != NULL)
25158 {
25159 complaint (&symfile_complaints,
9c541725
PA
25160 _("DW_AT_allocated has the wrong form (%s) at DIE 0x%x"),
25161 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
25162 to_underlying (die->sect_off));
3f2f83dd
KB
25163 }
25164
25165 /* Read DW_AT_associated and set in type. */
25166 attr = dwarf2_attr (die, DW_AT_associated, cu);
25167 if (attr_form_is_block (attr))
25168 {
25169 if (attr_to_dynamic_prop (attr, die, cu, &prop))
25170 add_dyn_prop (DYN_PROP_ASSOCIATED, prop, type, objfile);
25171 }
25172 else if (attr != NULL)
25173 {
25174 complaint (&symfile_complaints,
9c541725
PA
25175 _("DW_AT_associated has the wrong form (%s) at DIE 0x%x"),
25176 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
25177 to_underlying (die->sect_off));
3f2f83dd
KB
25178 }
25179
3cdcd0ce
JB
25180 /* Read DW_AT_data_location and set in type. */
25181 attr = dwarf2_attr (die, DW_AT_data_location, cu);
25182 if (attr_to_dynamic_prop (attr, die, cu, &prop))
93a8e227 25183 add_dyn_prop (DYN_PROP_DATA_LOCATION, prop, type, objfile);
3cdcd0ce 25184
dee91e82 25185 if (dwarf2_per_objfile->die_type_hash == NULL)
f792889a 25186 {
dee91e82
DE
25187 dwarf2_per_objfile->die_type_hash =
25188 htab_create_alloc_ex (127,
25189 per_cu_offset_and_type_hash,
25190 per_cu_offset_and_type_eq,
25191 NULL,
25192 &objfile->objfile_obstack,
25193 hashtab_obstack_allocate,
25194 dummy_obstack_deallocate);
f792889a 25195 }
1c379e20 25196
dee91e82 25197 ofs.per_cu = cu->per_cu;
9c541725 25198 ofs.sect_off = die->sect_off;
1c379e20 25199 ofs.type = type;
dee91e82
DE
25200 slot = (struct dwarf2_per_cu_offset_and_type **)
25201 htab_find_slot (dwarf2_per_objfile->die_type_hash, &ofs, INSERT);
7e314c57
JK
25202 if (*slot)
25203 complaint (&symfile_complaints,
25204 _("A problem internal to GDB: DIE 0x%x has type already set"),
9c541725 25205 to_underlying (die->sect_off));
8d749320
SM
25206 *slot = XOBNEW (&objfile->objfile_obstack,
25207 struct dwarf2_per_cu_offset_and_type);
1c379e20 25208 **slot = ofs;
f792889a 25209 return type;
1c379e20
DJ
25210}
25211
9c541725 25212/* Look up the type for the die at SECT_OFF in PER_CU in die_type_hash,
02142a6c 25213 or return NULL if the die does not have a saved type. */
1c379e20
DJ
25214
25215static struct type *
9c541725 25216get_die_type_at_offset (sect_offset sect_off,
673bfd45 25217 struct dwarf2_per_cu_data *per_cu)
1c379e20 25218{
dee91e82 25219 struct dwarf2_per_cu_offset_and_type *slot, ofs;
f792889a 25220
dee91e82 25221 if (dwarf2_per_objfile->die_type_hash == NULL)
f792889a 25222 return NULL;
1c379e20 25223
dee91e82 25224 ofs.per_cu = per_cu;
9c541725 25225 ofs.sect_off = sect_off;
9a3c8263
SM
25226 slot = ((struct dwarf2_per_cu_offset_and_type *)
25227 htab_find (dwarf2_per_objfile->die_type_hash, &ofs));
1c379e20
DJ
25228 if (slot)
25229 return slot->type;
25230 else
25231 return NULL;
25232}
25233
02142a6c 25234/* Look up the type for DIE in CU in die_type_hash,
673bfd45
DE
25235 or return NULL if DIE does not have a saved type. */
25236
25237static struct type *
25238get_die_type (struct die_info *die, struct dwarf2_cu *cu)
25239{
9c541725 25240 return get_die_type_at_offset (die->sect_off, cu->per_cu);
673bfd45
DE
25241}
25242
10b3939b
DJ
25243/* Add a dependence relationship from CU to REF_PER_CU. */
25244
25245static void
25246dwarf2_add_dependence (struct dwarf2_cu *cu,
25247 struct dwarf2_per_cu_data *ref_per_cu)
25248{
25249 void **slot;
25250
25251 if (cu->dependencies == NULL)
25252 cu->dependencies
25253 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
25254 NULL, &cu->comp_unit_obstack,
25255 hashtab_obstack_allocate,
25256 dummy_obstack_deallocate);
25257
25258 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
25259 if (*slot == NULL)
25260 *slot = ref_per_cu;
25261}
1c379e20 25262
f504f079
DE
25263/* Subroutine of dwarf2_mark to pass to htab_traverse.
25264 Set the mark field in every compilation unit in the
ae038cb0
DJ
25265 cache that we must keep because we are keeping CU. */
25266
10b3939b
DJ
25267static int
25268dwarf2_mark_helper (void **slot, void *data)
25269{
25270 struct dwarf2_per_cu_data *per_cu;
25271
25272 per_cu = (struct dwarf2_per_cu_data *) *slot;
d07ed419
JK
25273
25274 /* cu->dependencies references may not yet have been ever read if QUIT aborts
25275 reading of the chain. As such dependencies remain valid it is not much
25276 useful to track and undo them during QUIT cleanups. */
25277 if (per_cu->cu == NULL)
25278 return 1;
25279
10b3939b
DJ
25280 if (per_cu->cu->mark)
25281 return 1;
25282 per_cu->cu->mark = 1;
25283
25284 if (per_cu->cu->dependencies != NULL)
25285 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
25286
25287 return 1;
25288}
25289
f504f079
DE
25290/* Set the mark field in CU and in every other compilation unit in the
25291 cache that we must keep because we are keeping CU. */
25292
ae038cb0
DJ
25293static void
25294dwarf2_mark (struct dwarf2_cu *cu)
25295{
25296 if (cu->mark)
25297 return;
25298 cu->mark = 1;
10b3939b
DJ
25299 if (cu->dependencies != NULL)
25300 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
ae038cb0
DJ
25301}
25302
25303static void
25304dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
25305{
25306 while (per_cu)
25307 {
25308 per_cu->cu->mark = 0;
25309 per_cu = per_cu->cu->read_in_chain;
25310 }
72bf9492
DJ
25311}
25312
72bf9492
DJ
25313/* Trivial hash function for partial_die_info: the hash value of a DIE
25314 is its offset in .debug_info for this objfile. */
25315
25316static hashval_t
25317partial_die_hash (const void *item)
25318{
9a3c8263
SM
25319 const struct partial_die_info *part_die
25320 = (const struct partial_die_info *) item;
9a619af0 25321
9c541725 25322 return to_underlying (part_die->sect_off);
72bf9492
DJ
25323}
25324
25325/* Trivial comparison function for partial_die_info structures: two DIEs
25326 are equal if they have the same offset. */
25327
25328static int
25329partial_die_eq (const void *item_lhs, const void *item_rhs)
25330{
9a3c8263
SM
25331 const struct partial_die_info *part_die_lhs
25332 = (const struct partial_die_info *) item_lhs;
25333 const struct partial_die_info *part_die_rhs
25334 = (const struct partial_die_info *) item_rhs;
9a619af0 25335
9c541725 25336 return part_die_lhs->sect_off == part_die_rhs->sect_off;
72bf9492
DJ
25337}
25338
b4f54984
DE
25339static struct cmd_list_element *set_dwarf_cmdlist;
25340static struct cmd_list_element *show_dwarf_cmdlist;
ae038cb0
DJ
25341
25342static void
981a3fb3 25343set_dwarf_cmd (const char *args, int from_tty)
ae038cb0 25344{
b4f54984 25345 help_list (set_dwarf_cmdlist, "maintenance set dwarf ", all_commands,
635c7e8a 25346 gdb_stdout);
ae038cb0
DJ
25347}
25348
25349static void
981a3fb3 25350show_dwarf_cmd (const char *args, int from_tty)
6e70227d 25351{
b4f54984 25352 cmd_show_list (show_dwarf_cmdlist, from_tty, "");
ae038cb0
DJ
25353}
25354
4bf44c1c 25355/* Free data associated with OBJFILE, if necessary. */
dce234bc
PP
25356
25357static void
c1bd65d0 25358dwarf2_per_objfile_free (struct objfile *objfile, void *d)
dce234bc 25359{
9a3c8263 25360 struct dwarf2_per_objfile *data = (struct dwarf2_per_objfile *) d;
8b70b953 25361 int ix;
8b70b953 25362
626f2d1c
TT
25363 /* Make sure we don't accidentally use dwarf2_per_objfile while
25364 cleaning up. */
25365 dwarf2_per_objfile = NULL;
25366
59b0c7c1
JB
25367 for (ix = 0; ix < data->n_comp_units; ++ix)
25368 VEC_free (dwarf2_per_cu_ptr, data->all_comp_units[ix]->imported_symtabs);
796a7ff8 25369
59b0c7c1 25370 for (ix = 0; ix < data->n_type_units; ++ix)
796a7ff8 25371 VEC_free (dwarf2_per_cu_ptr,
59b0c7c1
JB
25372 data->all_type_units[ix]->per_cu.imported_symtabs);
25373 xfree (data->all_type_units);
95554aad 25374
8b70b953 25375 VEC_free (dwarf2_section_info_def, data->types);
3019eac3
DE
25376
25377 if (data->dwo_files)
25378 free_dwo_files (data->dwo_files, objfile);
5c6fa7ab
DE
25379 if (data->dwp_file)
25380 gdb_bfd_unref (data->dwp_file->dbfd);
36586728
TT
25381
25382 if (data->dwz_file && data->dwz_file->dwz_bfd)
25383 gdb_bfd_unref (data->dwz_file->dwz_bfd);
3f563c84
PA
25384
25385 if (data->index_table != NULL)
25386 data->index_table->~mapped_index ();
9291a0cd
TT
25387}
25388
25389\f
ae2de4f8 25390/* The "save gdb-index" command. */
9291a0cd 25391
437afbb8
JK
25392/* Write SIZE bytes from the buffer pointed to by DATA to FILE, with
25393 error checking. */
25394
25395static void
25396file_write (FILE *file, const void *data, size_t size)
25397{
25398 if (fwrite (data, 1, size, file) != size)
25399 error (_("couldn't data write to file"));
25400}
25401
25402/* Write the contents of VEC to FILE, with error checking. */
25403
25404template<typename Elem, typename Alloc>
25405static void
25406file_write (FILE *file, const std::vector<Elem, Alloc> &vec)
25407{
25408 file_write (file, vec.data (), vec.size () * sizeof (vec[0]));
25409}
25410
bc8f2430
JK
25411/* In-memory buffer to prepare data to be written later to a file. */
25412class data_buf
9291a0cd 25413{
bc8f2430 25414public:
bc8f2430
JK
25415 /* Copy DATA to the end of the buffer. */
25416 template<typename T>
25417 void append_data (const T &data)
25418 {
25419 std::copy (reinterpret_cast<const gdb_byte *> (&data),
25420 reinterpret_cast<const gdb_byte *> (&data + 1),
c2f134ac 25421 grow (sizeof (data)));
bc8f2430 25422 }
b89be57b 25423
c2f134ac
PA
25424 /* Copy CSTR (a zero-terminated string) to the end of buffer. The
25425 terminating zero is appended too. */
bc8f2430
JK
25426 void append_cstr0 (const char *cstr)
25427 {
25428 const size_t size = strlen (cstr) + 1;
c2f134ac
PA
25429 std::copy (cstr, cstr + size, grow (size));
25430 }
25431
437afbb8
JK
25432 /* Store INPUT as ULEB128 to the end of buffer. */
25433 void append_unsigned_leb128 (ULONGEST input)
25434 {
25435 for (;;)
25436 {
25437 gdb_byte output = input & 0x7f;
25438 input >>= 7;
25439 if (input)
25440 output |= 0x80;
25441 append_data (output);
25442 if (input == 0)
25443 break;
25444 }
25445 }
25446
c2f134ac
PA
25447 /* Accept a host-format integer in VAL and append it to the buffer
25448 as a target-format integer which is LEN bytes long. */
25449 void append_uint (size_t len, bfd_endian byte_order, ULONGEST val)
25450 {
25451 ::store_unsigned_integer (grow (len), len, byte_order, val);
bc8f2430 25452 }
9291a0cd 25453
bc8f2430
JK
25454 /* Return the size of the buffer. */
25455 size_t size () const
25456 {
25457 return m_vec.size ();
25458 }
25459
437afbb8
JK
25460 /* Return true iff the buffer is empty. */
25461 bool empty () const
25462 {
25463 return m_vec.empty ();
25464 }
25465
bc8f2430
JK
25466 /* Write the buffer to FILE. */
25467 void file_write (FILE *file) const
25468 {
437afbb8 25469 ::file_write (file, m_vec);
bc8f2430
JK
25470 }
25471
25472private:
c2f134ac
PA
25473 /* Grow SIZE bytes at the end of the buffer. Returns a pointer to
25474 the start of the new block. */
25475 gdb_byte *grow (size_t size)
25476 {
25477 m_vec.resize (m_vec.size () + size);
25478 return &*m_vec.end () - size;
25479 }
25480
d5722aa2 25481 gdb::byte_vector m_vec;
bc8f2430 25482};
9291a0cd
TT
25483
25484/* An entry in the symbol table. */
25485struct symtab_index_entry
25486{
25487 /* The name of the symbol. */
25488 const char *name;
25489 /* The offset of the name in the constant pool. */
25490 offset_type index_offset;
25491 /* A sorted vector of the indices of all the CUs that hold an object
25492 of this name. */
bc8f2430 25493 std::vector<offset_type> cu_indices;
9291a0cd
TT
25494};
25495
25496/* The symbol table. This is a power-of-2-sized hash table. */
25497struct mapped_symtab
25498{
bc8f2430
JK
25499 mapped_symtab ()
25500 {
25501 data.resize (1024);
25502 }
b89be57b 25503
bc8f2430 25504 offset_type n_elements = 0;
4b76cda9 25505 std::vector<symtab_index_entry> data;
bc8f2430 25506};
9291a0cd 25507
bc8f2430 25508/* Find a slot in SYMTAB for the symbol NAME. Returns a reference to
559a7a62
JK
25509 the slot.
25510
25511 Function is used only during write_hash_table so no index format backward
25512 compatibility is needed. */
b89be57b 25513
4b76cda9 25514static symtab_index_entry &
9291a0cd
TT
25515find_slot (struct mapped_symtab *symtab, const char *name)
25516{
559a7a62 25517 offset_type index, step, hash = mapped_index_string_hash (INT_MAX, name);
9291a0cd 25518
bc8f2430
JK
25519 index = hash & (symtab->data.size () - 1);
25520 step = ((hash * 17) & (symtab->data.size () - 1)) | 1;
9291a0cd
TT
25521
25522 for (;;)
25523 {
4b76cda9
PA
25524 if (symtab->data[index].name == NULL
25525 || strcmp (name, symtab->data[index].name) == 0)
bc8f2430
JK
25526 return symtab->data[index];
25527 index = (index + step) & (symtab->data.size () - 1);
9291a0cd
TT
25528 }
25529}
25530
25531/* Expand SYMTAB's hash table. */
b89be57b 25532
9291a0cd
TT
25533static void
25534hash_expand (struct mapped_symtab *symtab)
25535{
bc8f2430 25536 auto old_entries = std::move (symtab->data);
9291a0cd 25537
bc8f2430
JK
25538 symtab->data.clear ();
25539 symtab->data.resize (old_entries.size () * 2);
9291a0cd 25540
bc8f2430 25541 for (auto &it : old_entries)
4b76cda9 25542 if (it.name != NULL)
bc8f2430 25543 {
4b76cda9 25544 auto &ref = find_slot (symtab, it.name);
bc8f2430
JK
25545 ref = std::move (it);
25546 }
9291a0cd
TT
25547}
25548
156942c7
DE
25549/* Add an entry to SYMTAB. NAME is the name of the symbol.
25550 CU_INDEX is the index of the CU in which the symbol appears.
25551 IS_STATIC is one if the symbol is static, otherwise zero (global). */
b89be57b 25552
9291a0cd
TT
25553static void
25554add_index_entry (struct mapped_symtab *symtab, const char *name,
156942c7 25555 int is_static, gdb_index_symbol_kind kind,
9291a0cd
TT
25556 offset_type cu_index)
25557{
156942c7 25558 offset_type cu_index_and_attrs;
9291a0cd
TT
25559
25560 ++symtab->n_elements;
bc8f2430 25561 if (4 * symtab->n_elements / 3 >= symtab->data.size ())
9291a0cd
TT
25562 hash_expand (symtab);
25563
4b76cda9
PA
25564 symtab_index_entry &slot = find_slot (symtab, name);
25565 if (slot.name == NULL)
9291a0cd 25566 {
4b76cda9 25567 slot.name = name;
156942c7 25568 /* index_offset is set later. */
9291a0cd 25569 }
156942c7
DE
25570
25571 cu_index_and_attrs = 0;
25572 DW2_GDB_INDEX_CU_SET_VALUE (cu_index_and_attrs, cu_index);
25573 DW2_GDB_INDEX_SYMBOL_STATIC_SET_VALUE (cu_index_and_attrs, is_static);
25574 DW2_GDB_INDEX_SYMBOL_KIND_SET_VALUE (cu_index_and_attrs, kind);
25575
25576 /* We don't want to record an index value twice as we want to avoid the
25577 duplication.
25578 We process all global symbols and then all static symbols
25579 (which would allow us to avoid the duplication by only having to check
25580 the last entry pushed), but a symbol could have multiple kinds in one CU.
25581 To keep things simple we don't worry about the duplication here and
25582 sort and uniqufy the list after we've processed all symbols. */
4b76cda9 25583 slot.cu_indices.push_back (cu_index_and_attrs);
156942c7
DE
25584}
25585
25586/* Sort and remove duplicates of all symbols' cu_indices lists. */
25587
25588static void
25589uniquify_cu_indices (struct mapped_symtab *symtab)
25590{
4b76cda9 25591 for (auto &entry : symtab->data)
156942c7 25592 {
4b76cda9 25593 if (entry.name != NULL && !entry.cu_indices.empty ())
156942c7 25594 {
4b76cda9 25595 auto &cu_indices = entry.cu_indices;
6fd931f2
PA
25596 std::sort (cu_indices.begin (), cu_indices.end ());
25597 auto from = std::unique (cu_indices.begin (), cu_indices.end ());
25598 cu_indices.erase (from, cu_indices.end ());
156942c7
DE
25599 }
25600 }
9291a0cd
TT
25601}
25602
bc8f2430
JK
25603/* A form of 'const char *' suitable for container keys. Only the
25604 pointer is stored. The strings themselves are compared, not the
25605 pointers. */
25606class c_str_view
9291a0cd 25607{
bc8f2430
JK
25608public:
25609 c_str_view (const char *cstr)
25610 : m_cstr (cstr)
25611 {}
9291a0cd 25612
bc8f2430
JK
25613 bool operator== (const c_str_view &other) const
25614 {
25615 return strcmp (m_cstr, other.m_cstr) == 0;
25616 }
9291a0cd 25617
437afbb8
JK
25618 /* Return the underlying C string. Note, the returned string is
25619 only a reference with lifetime of this object. */
25620 const char *c_str () const
25621 {
25622 return m_cstr;
25623 }
25624
bc8f2430
JK
25625private:
25626 friend class c_str_view_hasher;
25627 const char *const m_cstr;
25628};
9291a0cd 25629
bc8f2430
JK
25630/* A std::unordered_map::hasher for c_str_view that uses the right
25631 hash function for strings in a mapped index. */
25632class c_str_view_hasher
25633{
25634public:
25635 size_t operator () (const c_str_view &x) const
25636 {
25637 return mapped_index_string_hash (INT_MAX, x.m_cstr);
25638 }
25639};
b89be57b 25640
bc8f2430
JK
25641/* A std::unordered_map::hasher for std::vector<>. */
25642template<typename T>
25643class vector_hasher
9291a0cd 25644{
bc8f2430
JK
25645public:
25646 size_t operator () (const std::vector<T> &key) const
25647 {
25648 return iterative_hash (key.data (),
25649 sizeof (key.front ()) * key.size (), 0);
25650 }
25651};
9291a0cd 25652
bc8f2430
JK
25653/* Write the mapped hash table SYMTAB to the data buffer OUTPUT, with
25654 constant pool entries going into the data buffer CPOOL. */
3876f04e 25655
bc8f2430
JK
25656static void
25657write_hash_table (mapped_symtab *symtab, data_buf &output, data_buf &cpool)
25658{
25659 {
25660 /* Elements are sorted vectors of the indices of all the CUs that
25661 hold an object of this name. */
25662 std::unordered_map<std::vector<offset_type>, offset_type,
25663 vector_hasher<offset_type>>
25664 symbol_hash_table;
25665
25666 /* We add all the index vectors to the constant pool first, to
25667 ensure alignment is ok. */
4b76cda9 25668 for (symtab_index_entry &entry : symtab->data)
bc8f2430 25669 {
4b76cda9 25670 if (entry.name == NULL)
bc8f2430 25671 continue;
4b76cda9 25672 gdb_assert (entry.index_offset == 0);
70a1152b
PA
25673
25674 /* Finding before inserting is faster than always trying to
25675 insert, because inserting always allocates a node, does the
25676 lookup, and then destroys the new node if another node
25677 already had the same key. C++17 try_emplace will avoid
25678 this. */
25679 const auto found
4b76cda9 25680 = symbol_hash_table.find (entry.cu_indices);
70a1152b
PA
25681 if (found != symbol_hash_table.end ())
25682 {
4b76cda9 25683 entry.index_offset = found->second;
70a1152b
PA
25684 continue;
25685 }
25686
4b76cda9
PA
25687 symbol_hash_table.emplace (entry.cu_indices, cpool.size ());
25688 entry.index_offset = cpool.size ();
25689 cpool.append_data (MAYBE_SWAP (entry.cu_indices.size ()));
25690 for (const auto index : entry.cu_indices)
25691 cpool.append_data (MAYBE_SWAP (index));
bc8f2430
JK
25692 }
25693 }
9291a0cd
TT
25694
25695 /* Now write out the hash table. */
bc8f2430 25696 std::unordered_map<c_str_view, offset_type, c_str_view_hasher> str_table;
4b76cda9 25697 for (const auto &entry : symtab->data)
9291a0cd
TT
25698 {
25699 offset_type str_off, vec_off;
25700
4b76cda9 25701 if (entry.name != NULL)
9291a0cd 25702 {
4b76cda9 25703 const auto insertpair = str_table.emplace (entry.name, cpool.size ());
bc8f2430 25704 if (insertpair.second)
4b76cda9 25705 cpool.append_cstr0 (entry.name);
bc8f2430 25706 str_off = insertpair.first->second;
4b76cda9 25707 vec_off = entry.index_offset;
9291a0cd
TT
25708 }
25709 else
25710 {
25711 /* While 0 is a valid constant pool index, it is not valid
25712 to have 0 for both offsets. */
25713 str_off = 0;
25714 vec_off = 0;
25715 }
25716
bc8f2430
JK
25717 output.append_data (MAYBE_SWAP (str_off));
25718 output.append_data (MAYBE_SWAP (vec_off));
9291a0cd 25719 }
9291a0cd
TT
25720}
25721
bc8f2430 25722typedef std::unordered_map<partial_symtab *, unsigned int> psym_index_map;
0a5429f6
DE
25723
25724/* Helper struct for building the address table. */
25725struct addrmap_index_data
25726{
bc8f2430
JK
25727 addrmap_index_data (data_buf &addr_vec_, psym_index_map &cu_index_htab_)
25728 : addr_vec (addr_vec_), cu_index_htab (cu_index_htab_)
25729 {}
25730
0a5429f6 25731 struct objfile *objfile;
bc8f2430
JK
25732 data_buf &addr_vec;
25733 psym_index_map &cu_index_htab;
0a5429f6
DE
25734
25735 /* Non-zero if the previous_* fields are valid.
25736 We can't write an entry until we see the next entry (since it is only then
25737 that we know the end of the entry). */
25738 int previous_valid;
25739 /* Index of the CU in the table of all CUs in the index file. */
25740 unsigned int previous_cu_index;
0963b4bd 25741 /* Start address of the CU. */
0a5429f6
DE
25742 CORE_ADDR previous_cu_start;
25743};
25744
bc8f2430 25745/* Write an address entry to ADDR_VEC. */
b89be57b 25746
9291a0cd 25747static void
bc8f2430 25748add_address_entry (struct objfile *objfile, data_buf &addr_vec,
0a5429f6 25749 CORE_ADDR start, CORE_ADDR end, unsigned int cu_index)
9291a0cd 25750{
9291a0cd
TT
25751 CORE_ADDR baseaddr;
25752
25753 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
25754
c2f134ac
PA
25755 addr_vec.append_uint (8, BFD_ENDIAN_LITTLE, start - baseaddr);
25756 addr_vec.append_uint (8, BFD_ENDIAN_LITTLE, end - baseaddr);
bc8f2430 25757 addr_vec.append_data (MAYBE_SWAP (cu_index));
0a5429f6
DE
25758}
25759
25760/* Worker function for traversing an addrmap to build the address table. */
25761
25762static int
25763add_address_entry_worker (void *datap, CORE_ADDR start_addr, void *obj)
25764{
9a3c8263
SM
25765 struct addrmap_index_data *data = (struct addrmap_index_data *) datap;
25766 struct partial_symtab *pst = (struct partial_symtab *) obj;
0a5429f6
DE
25767
25768 if (data->previous_valid)
bc8f2430 25769 add_address_entry (data->objfile, data->addr_vec,
0a5429f6
DE
25770 data->previous_cu_start, start_addr,
25771 data->previous_cu_index);
25772
25773 data->previous_cu_start = start_addr;
25774 if (pst != NULL)
25775 {
bc8f2430
JK
25776 const auto it = data->cu_index_htab.find (pst);
25777 gdb_assert (it != data->cu_index_htab.cend ());
25778 data->previous_cu_index = it->second;
0a5429f6
DE
25779 data->previous_valid = 1;
25780 }
25781 else
bc8f2430 25782 data->previous_valid = 0;
0a5429f6
DE
25783
25784 return 0;
25785}
25786
bc8f2430 25787/* Write OBJFILE's address map to ADDR_VEC.
0a5429f6
DE
25788 CU_INDEX_HTAB is used to map addrmap entries to their CU indices
25789 in the index file. */
25790
25791static void
bc8f2430
JK
25792write_address_map (struct objfile *objfile, data_buf &addr_vec,
25793 psym_index_map &cu_index_htab)
0a5429f6 25794{
bc8f2430 25795 struct addrmap_index_data addrmap_index_data (addr_vec, cu_index_htab);
0a5429f6
DE
25796
25797 /* When writing the address table, we have to cope with the fact that
25798 the addrmap iterator only provides the start of a region; we have to
25799 wait until the next invocation to get the start of the next region. */
25800
25801 addrmap_index_data.objfile = objfile;
0a5429f6
DE
25802 addrmap_index_data.previous_valid = 0;
25803
25804 addrmap_foreach (objfile->psymtabs_addrmap, add_address_entry_worker,
25805 &addrmap_index_data);
25806
25807 /* It's highly unlikely the last entry (end address = 0xff...ff)
25808 is valid, but we should still handle it.
25809 The end address is recorded as the start of the next region, but that
25810 doesn't work here. To cope we pass 0xff...ff, this is a rare situation
25811 anyway. */
25812 if (addrmap_index_data.previous_valid)
bc8f2430 25813 add_address_entry (objfile, addr_vec,
0a5429f6
DE
25814 addrmap_index_data.previous_cu_start, (CORE_ADDR) -1,
25815 addrmap_index_data.previous_cu_index);
9291a0cd
TT
25816}
25817
156942c7
DE
25818/* Return the symbol kind of PSYM. */
25819
25820static gdb_index_symbol_kind
25821symbol_kind (struct partial_symbol *psym)
25822{
25823 domain_enum domain = PSYMBOL_DOMAIN (psym);
25824 enum address_class aclass = PSYMBOL_CLASS (psym);
25825
25826 switch (domain)
25827 {
25828 case VAR_DOMAIN:
25829 switch (aclass)
25830 {
25831 case LOC_BLOCK:
25832 return GDB_INDEX_SYMBOL_KIND_FUNCTION;
25833 case LOC_TYPEDEF:
25834 return GDB_INDEX_SYMBOL_KIND_TYPE;
25835 case LOC_COMPUTED:
25836 case LOC_CONST_BYTES:
25837 case LOC_OPTIMIZED_OUT:
25838 case LOC_STATIC:
25839 return GDB_INDEX_SYMBOL_KIND_VARIABLE;
25840 case LOC_CONST:
25841 /* Note: It's currently impossible to recognize psyms as enum values
25842 short of reading the type info. For now punt. */
25843 return GDB_INDEX_SYMBOL_KIND_VARIABLE;
25844 default:
25845 /* There are other LOC_FOO values that one might want to classify
25846 as variables, but dwarf2read.c doesn't currently use them. */
25847 return GDB_INDEX_SYMBOL_KIND_OTHER;
25848 }
25849 case STRUCT_DOMAIN:
25850 return GDB_INDEX_SYMBOL_KIND_TYPE;
25851 default:
25852 return GDB_INDEX_SYMBOL_KIND_OTHER;
25853 }
25854}
25855
9291a0cd 25856/* Add a list of partial symbols to SYMTAB. */
b89be57b 25857
9291a0cd
TT
25858static void
25859write_psymbols (struct mapped_symtab *symtab,
bc8f2430 25860 std::unordered_set<partial_symbol *> &psyms_seen,
9291a0cd
TT
25861 struct partial_symbol **psymp,
25862 int count,
987d643c
TT
25863 offset_type cu_index,
25864 int is_static)
9291a0cd
TT
25865{
25866 for (; count-- > 0; ++psymp)
25867 {
156942c7 25868 struct partial_symbol *psym = *psymp;
987d643c 25869
156942c7 25870 if (SYMBOL_LANGUAGE (psym) == language_ada)
9291a0cd 25871 error (_("Ada is not currently supported by the index"));
987d643c 25872
987d643c 25873 /* Only add a given psymbol once. */
bc8f2430 25874 if (psyms_seen.insert (psym).second)
987d643c 25875 {
156942c7
DE
25876 gdb_index_symbol_kind kind = symbol_kind (psym);
25877
156942c7
DE
25878 add_index_entry (symtab, SYMBOL_SEARCH_NAME (psym),
25879 is_static, kind, cu_index);
987d643c 25880 }
9291a0cd
TT
25881 }
25882}
25883
1fd400ff
TT
25884/* A helper struct used when iterating over debug_types. */
25885struct signatured_type_index_data
25886{
bc8f2430
JK
25887 signatured_type_index_data (data_buf &types_list_,
25888 std::unordered_set<partial_symbol *> &psyms_seen_)
25889 : types_list (types_list_), psyms_seen (psyms_seen_)
25890 {}
25891
1fd400ff
TT
25892 struct objfile *objfile;
25893 struct mapped_symtab *symtab;
bc8f2430
JK
25894 data_buf &types_list;
25895 std::unordered_set<partial_symbol *> &psyms_seen;
1fd400ff
TT
25896 int cu_index;
25897};
25898
25899/* A helper function that writes a single signatured_type to an
25900 obstack. */
b89be57b 25901
1fd400ff
TT
25902static int
25903write_one_signatured_type (void **slot, void *d)
25904{
9a3c8263
SM
25905 struct signatured_type_index_data *info
25906 = (struct signatured_type_index_data *) d;
1fd400ff 25907 struct signatured_type *entry = (struct signatured_type *) *slot;
0186c6a7 25908 struct partial_symtab *psymtab = entry->per_cu.v.psymtab;
1fd400ff
TT
25909
25910 write_psymbols (info->symtab,
987d643c 25911 info->psyms_seen,
af5bf4ad 25912 &info->objfile->global_psymbols[psymtab->globals_offset],
987d643c
TT
25913 psymtab->n_global_syms, info->cu_index,
25914 0);
1fd400ff 25915 write_psymbols (info->symtab,
987d643c 25916 info->psyms_seen,
af5bf4ad 25917 &info->objfile->static_psymbols[psymtab->statics_offset],
987d643c
TT
25918 psymtab->n_static_syms, info->cu_index,
25919 1);
1fd400ff 25920
c2f134ac
PA
25921 info->types_list.append_uint (8, BFD_ENDIAN_LITTLE,
25922 to_underlying (entry->per_cu.sect_off));
25923 info->types_list.append_uint (8, BFD_ENDIAN_LITTLE,
25924 to_underlying (entry->type_offset_in_tu));
25925 info->types_list.append_uint (8, BFD_ENDIAN_LITTLE, entry->signature);
1fd400ff
TT
25926
25927 ++info->cu_index;
25928
25929 return 1;
25930}
25931
e8f8bcb3
PA
25932/* Recurse into all "included" dependencies and count their symbols as
25933 if they appeared in this psymtab. */
25934
25935static void
25936recursively_count_psymbols (struct partial_symtab *psymtab,
25937 size_t &psyms_seen)
25938{
25939 for (int i = 0; i < psymtab->number_of_dependencies; ++i)
25940 if (psymtab->dependencies[i]->user != NULL)
25941 recursively_count_psymbols (psymtab->dependencies[i],
25942 psyms_seen);
25943
25944 psyms_seen += psymtab->n_global_syms;
25945 psyms_seen += psymtab->n_static_syms;
25946}
25947
95554aad
TT
25948/* Recurse into all "included" dependencies and write their symbols as
25949 if they appeared in this psymtab. */
25950
25951static void
25952recursively_write_psymbols (struct objfile *objfile,
25953 struct partial_symtab *psymtab,
25954 struct mapped_symtab *symtab,
bc8f2430 25955 std::unordered_set<partial_symbol *> &psyms_seen,
95554aad
TT
25956 offset_type cu_index)
25957{
25958 int i;
25959
25960 for (i = 0; i < psymtab->number_of_dependencies; ++i)
25961 if (psymtab->dependencies[i]->user != NULL)
25962 recursively_write_psymbols (objfile, psymtab->dependencies[i],
25963 symtab, psyms_seen, cu_index);
25964
25965 write_psymbols (symtab,
25966 psyms_seen,
af5bf4ad 25967 &objfile->global_psymbols[psymtab->globals_offset],
95554aad
TT
25968 psymtab->n_global_syms, cu_index,
25969 0);
25970 write_psymbols (symtab,
25971 psyms_seen,
af5bf4ad 25972 &objfile->static_psymbols[psymtab->statics_offset],
95554aad
TT
25973 psymtab->n_static_syms, cu_index,
25974 1);
25975}
25976
437afbb8
JK
25977/* DWARF-5 .debug_names builder. */
25978class debug_names
25979{
25980public:
25981 debug_names (bool is_dwarf64, bfd_endian dwarf5_byte_order)
25982 : m_dwarf5_byte_order (dwarf5_byte_order),
25983 m_dwarf32 (dwarf5_byte_order),
25984 m_dwarf64 (dwarf5_byte_order),
25985 m_dwarf (is_dwarf64
25986 ? static_cast<dwarf &> (m_dwarf64)
25987 : static_cast<dwarf &> (m_dwarf32)),
25988 m_name_table_string_offs (m_dwarf.name_table_string_offs),
25989 m_name_table_entry_offs (m_dwarf.name_table_entry_offs)
25990 {}
9291a0cd 25991
437afbb8
JK
25992 /* Insert one symbol. */
25993 void insert (const partial_symbol *psym, int cu_index, bool is_static)
25994 {
25995 const int dwarf_tag = psymbol_tag (psym);
25996 if (dwarf_tag == 0)
25997 return;
25998 const char *const name = SYMBOL_SEARCH_NAME (psym);
25999 const auto insertpair
26000 = m_name_to_value_set.emplace (c_str_view (name),
26001 std::set<symbol_value> ());
26002 std::set<symbol_value> &value_set = insertpair.first->second;
26003 value_set.emplace (symbol_value (dwarf_tag, cu_index, is_static));
26004 }
9291a0cd 26005
437afbb8
JK
26006 /* Build all the tables. All symbols must be already inserted.
26007 This function does not call file_write, caller has to do it
26008 afterwards. */
26009 void build ()
26010 {
26011 /* Verify the build method has not be called twice. */
26012 gdb_assert (m_abbrev_table.empty ());
26013 const size_t name_count = m_name_to_value_set.size ();
26014 m_bucket_table.resize
26015 (std::pow (2, std::ceil (std::log2 (name_count * 4 / 3))));
26016 m_hash_table.reserve (name_count);
26017 m_name_table_string_offs.reserve (name_count);
26018 m_name_table_entry_offs.reserve (name_count);
26019
26020 /* Map each hash of symbol to its name and value. */
26021 struct hash_it_pair
26022 {
26023 uint32_t hash;
26024 decltype (m_name_to_value_set)::const_iterator it;
26025 };
26026 std::vector<std::forward_list<hash_it_pair>> bucket_hash;
26027 bucket_hash.resize (m_bucket_table.size ());
26028 for (decltype (m_name_to_value_set)::const_iterator it
26029 = m_name_to_value_set.cbegin ();
26030 it != m_name_to_value_set.cend ();
26031 ++it)
26032 {
26033 const char *const name = it->first.c_str ();
26034 const uint32_t hash = dwarf5_djb_hash (name);
26035 hash_it_pair hashitpair;
26036 hashitpair.hash = hash;
26037 hashitpair.it = it;
26038 auto &slot = bucket_hash[hash % bucket_hash.size()];
26039 slot.push_front (std::move (hashitpair));
26040 }
26041 for (size_t bucket_ix = 0; bucket_ix < bucket_hash.size (); ++bucket_ix)
26042 {
26043 const std::forward_list<hash_it_pair> &hashitlist
26044 = bucket_hash[bucket_ix];
26045 if (hashitlist.empty ())
26046 continue;
26047 uint32_t &bucket_slot = m_bucket_table[bucket_ix];
26048 /* The hashes array is indexed starting at 1. */
26049 store_unsigned_integer (reinterpret_cast<gdb_byte *> (&bucket_slot),
26050 sizeof (bucket_slot), m_dwarf5_byte_order,
26051 m_hash_table.size () + 1);
26052 for (const hash_it_pair &hashitpair : hashitlist)
26053 {
26054 m_hash_table.push_back (0);
26055 store_unsigned_integer (reinterpret_cast<gdb_byte *>
26056 (&m_hash_table.back ()),
26057 sizeof (m_hash_table.back ()),
26058 m_dwarf5_byte_order, hashitpair.hash);
26059 const c_str_view &name = hashitpair.it->first;
26060 const std::set<symbol_value> &value_set = hashitpair.it->second;
26061 m_name_table_string_offs.push_back_reorder
26062 (m_debugstrlookup.lookup (name.c_str ()));
26063 m_name_table_entry_offs.push_back_reorder (m_entry_pool.size ());
26064 gdb_assert (!value_set.empty ());
26065 for (const symbol_value &value : value_set)
26066 {
26067 int &idx = m_indexkey_to_idx[index_key (value.dwarf_tag,
26068 value.is_static)];
26069 if (idx == 0)
26070 {
26071 idx = m_idx_next++;
26072 m_abbrev_table.append_unsigned_leb128 (idx);
26073 m_abbrev_table.append_unsigned_leb128 (value.dwarf_tag);
26074 m_abbrev_table.append_unsigned_leb128 (DW_IDX_compile_unit);
26075 m_abbrev_table.append_unsigned_leb128 (DW_FORM_udata);
26076 m_abbrev_table.append_unsigned_leb128 (value.is_static
26077 ? DW_IDX_GNU_internal
26078 : DW_IDX_GNU_external);
26079 m_abbrev_table.append_unsigned_leb128 (DW_FORM_flag_present);
26080
26081 /* Terminate attributes list. */
26082 m_abbrev_table.append_unsigned_leb128 (0);
26083 m_abbrev_table.append_unsigned_leb128 (0);
26084 }
9291a0cd 26085
437afbb8
JK
26086 m_entry_pool.append_unsigned_leb128 (idx);
26087 m_entry_pool.append_unsigned_leb128 (value.cu_index);
26088 }
9291a0cd 26089
437afbb8
JK
26090 /* Terminate the list of CUs. */
26091 m_entry_pool.append_unsigned_leb128 (0);
26092 }
26093 }
26094 gdb_assert (m_hash_table.size () == name_count);
987d643c 26095
437afbb8
JK
26096 /* Terminate tags list. */
26097 m_abbrev_table.append_unsigned_leb128 (0);
26098 }
0a5429f6 26099
437afbb8
JK
26100 /* Return .debug_names bucket count. This must be called only after
26101 calling the build method. */
26102 uint32_t bucket_count () const
26103 {
26104 /* Verify the build method has been already called. */
26105 gdb_assert (!m_abbrev_table.empty ());
26106 const uint32_t retval = m_bucket_table.size ();
26107
26108 /* Check for overflow. */
26109 gdb_assert (retval == m_bucket_table.size ());
26110 return retval;
26111 }
26112
26113 /* Return .debug_names names count. This must be called only after
26114 calling the build method. */
26115 uint32_t name_count () const
26116 {
26117 /* Verify the build method has been already called. */
26118 gdb_assert (!m_abbrev_table.empty ());
26119 const uint32_t retval = m_hash_table.size ();
26120
26121 /* Check for overflow. */
26122 gdb_assert (retval == m_hash_table.size ());
26123 return retval;
26124 }
26125
26126 /* Return number of bytes of .debug_names abbreviation table. This
26127 must be called only after calling the build method. */
26128 uint32_t abbrev_table_bytes () const
26129 {
26130 gdb_assert (!m_abbrev_table.empty ());
26131 return m_abbrev_table.size ();
26132 }
26133
26134 /* Recurse into all "included" dependencies and store their symbols
26135 as if they appeared in this psymtab. */
26136 void recursively_write_psymbols
26137 (struct objfile *objfile,
26138 struct partial_symtab *psymtab,
26139 std::unordered_set<partial_symbol *> &psyms_seen,
26140 int cu_index)
26141 {
26142 for (int i = 0; i < psymtab->number_of_dependencies; ++i)
26143 if (psymtab->dependencies[i]->user != NULL)
26144 recursively_write_psymbols (objfile, psymtab->dependencies[i],
26145 psyms_seen, cu_index);
26146
26147 write_psymbols (psyms_seen,
26148 &objfile->global_psymbols[psymtab->globals_offset],
26149 psymtab->n_global_syms, cu_index, false);
26150 write_psymbols (psyms_seen,
26151 &objfile->static_psymbols[psymtab->statics_offset],
26152 psymtab->n_static_syms, cu_index, true);
26153 }
26154
26155 /* Return number of bytes the .debug_names section will have. This
26156 must be called only after calling the build method. */
26157 size_t bytes () const
26158 {
26159 /* Verify the build method has been already called. */
26160 gdb_assert (!m_abbrev_table.empty ());
26161 size_t expected_bytes = 0;
26162 expected_bytes += m_bucket_table.size () * sizeof (m_bucket_table[0]);
26163 expected_bytes += m_hash_table.size () * sizeof (m_hash_table[0]);
26164 expected_bytes += m_name_table_string_offs.bytes ();
26165 expected_bytes += m_name_table_entry_offs.bytes ();
26166 expected_bytes += m_abbrev_table.size ();
26167 expected_bytes += m_entry_pool.size ();
26168 return expected_bytes;
26169 }
26170
26171 /* Write .debug_names to FILE_NAMES and .debug_str addition to
26172 FILE_STR. This must be called only after calling the build
26173 method. */
26174 void file_write (FILE *file_names, FILE *file_str) const
26175 {
26176 /* Verify the build method has been already called. */
26177 gdb_assert (!m_abbrev_table.empty ());
26178 ::file_write (file_names, m_bucket_table);
26179 ::file_write (file_names, m_hash_table);
26180 m_name_table_string_offs.file_write (file_names);
26181 m_name_table_entry_offs.file_write (file_names);
26182 m_abbrev_table.file_write (file_names);
26183 m_entry_pool.file_write (file_names);
26184 m_debugstrlookup.file_write (file_str);
26185 }
26186
26187private:
26188
26189 /* Storage for symbol names mapping them to their .debug_str section
26190 offsets. */
26191 class debug_str_lookup
26192 {
26193 public:
26194
26195 /* Object costructor to be called for current DWARF2_PER_OBJFILE.
26196 All .debug_str section strings are automatically stored. */
26197 debug_str_lookup ()
26198 : m_abfd (dwarf2_per_objfile->objfile->obfd)
26199 {
26200 dwarf2_read_section (dwarf2_per_objfile->objfile,
26201 &dwarf2_per_objfile->str);
26202 if (dwarf2_per_objfile->str.buffer == NULL)
26203 return;
26204 for (const gdb_byte *data = dwarf2_per_objfile->str.buffer;
26205 data < (dwarf2_per_objfile->str.buffer
26206 + dwarf2_per_objfile->str.size);)
26207 {
26208 const char *const s = reinterpret_cast<const char *> (data);
26209 const auto insertpair
26210 = m_str_table.emplace (c_str_view (s),
26211 data - dwarf2_per_objfile->str.buffer);
26212 if (!insertpair.second)
26213 complaint (&symfile_complaints,
26214 _("Duplicate string \"%s\" in "
26215 ".debug_str section [in module %s]"),
26216 s, bfd_get_filename (m_abfd));
26217 data += strlen (s) + 1;
26218 }
26219 }
26220
26221 /* Return offset of symbol name S in the .debug_str section. Add
26222 such symbol to the section's end if it does not exist there
26223 yet. */
26224 size_t lookup (const char *s)
26225 {
26226 const auto it = m_str_table.find (c_str_view (s));
26227 if (it != m_str_table.end ())
26228 return it->second;
26229 const size_t offset = (dwarf2_per_objfile->str.size
26230 + m_str_add_buf.size ());
26231 m_str_table.emplace (c_str_view (s), offset);
26232 m_str_add_buf.append_cstr0 (s);
26233 return offset;
26234 }
26235
26236 /* Append the end of the .debug_str section to FILE. */
26237 void file_write (FILE *file) const
26238 {
26239 m_str_add_buf.file_write (file);
26240 }
26241
26242 private:
26243 std::unordered_map<c_str_view, size_t, c_str_view_hasher> m_str_table;
26244 bfd *const m_abfd;
26245
26246 /* Data to add at the end of .debug_str for new needed symbol names. */
26247 data_buf m_str_add_buf;
26248 };
26249
26250 /* Container to map used DWARF tags to their .debug_names abbreviation
26251 tags. */
26252 class index_key
26253 {
26254 public:
26255 index_key (int dwarf_tag_, bool is_static_)
26256 : dwarf_tag (dwarf_tag_), is_static (is_static_)
26257 {
26258 }
26259
26260 bool
26261 operator== (const index_key &other) const
26262 {
26263 return dwarf_tag == other.dwarf_tag && is_static == other.is_static;
26264 }
26265
26266 const int dwarf_tag;
26267 const bool is_static;
26268 };
26269
26270 /* Provide std::unordered_map::hasher for index_key. */
26271 class index_key_hasher
26272 {
26273 public:
26274 size_t
26275 operator () (const index_key &key) const
26276 {
26277 return (std::hash<int>() (key.dwarf_tag) << 1) | key.is_static;
26278 }
26279 };
26280
26281 /* Parameters of one symbol entry. */
26282 class symbol_value
26283 {
26284 public:
26285 const int dwarf_tag, cu_index;
26286 const bool is_static;
26287
26288 symbol_value (int dwarf_tag_, int cu_index_, bool is_static_)
26289 : dwarf_tag (dwarf_tag_), cu_index (cu_index_), is_static (is_static_)
26290 {}
26291
26292 bool
26293 operator< (const symbol_value &other) const
26294 {
26295#define X(n) \
26296 do \
26297 { \
26298 if (n < other.n) \
26299 return true; \
26300 if (n > other.n) \
26301 return false; \
26302 } \
26303 while (0)
26304 X (dwarf_tag);
26305 X (is_static);
26306 X (cu_index);
26307#undef X
26308 return false;
26309 }
26310 };
26311
26312 /* Abstract base class to unify DWARF-32 and DWARF-64 name table
26313 output. */
26314 class offset_vec
26315 {
26316 protected:
26317 const bfd_endian dwarf5_byte_order;
26318 public:
26319 explicit offset_vec (bfd_endian dwarf5_byte_order_)
26320 : dwarf5_byte_order (dwarf5_byte_order_)
26321 {}
26322
26323 /* Call std::vector::reserve for NELEM elements. */
26324 virtual void reserve (size_t nelem) = 0;
26325
26326 /* Call std::vector::push_back with store_unsigned_integer byte
26327 reordering for ELEM. */
26328 virtual void push_back_reorder (size_t elem) = 0;
26329
26330 /* Return expected output size in bytes. */
26331 virtual size_t bytes () const = 0;
26332
26333 /* Write name table to FILE. */
26334 virtual void file_write (FILE *file) const = 0;
26335 };
26336
26337 /* Template to unify DWARF-32 and DWARF-64 output. */
26338 template<typename OffsetSize>
26339 class offset_vec_tmpl : public offset_vec
26340 {
26341 public:
26342 explicit offset_vec_tmpl (bfd_endian dwarf5_byte_order_)
26343 : offset_vec (dwarf5_byte_order_)
26344 {}
26345
26346 /* Implement offset_vec::reserve. */
26347 void reserve (size_t nelem) override
26348 {
26349 m_vec.reserve (nelem);
26350 }
26351
26352 /* Implement offset_vec::push_back_reorder. */
26353 void push_back_reorder (size_t elem) override
26354 {
26355 m_vec.push_back (elem);
26356 /* Check for overflow. */
26357 gdb_assert (m_vec.back () == elem);
26358 store_unsigned_integer (reinterpret_cast<gdb_byte *> (&m_vec.back ()),
26359 sizeof (m_vec.back ()), dwarf5_byte_order, elem);
26360 }
26361
26362 /* Implement offset_vec::bytes. */
26363 size_t bytes () const override
26364 {
26365 return m_vec.size () * sizeof (m_vec[0]);
26366 }
26367
26368 /* Implement offset_vec::file_write. */
26369 void file_write (FILE *file) const override
26370 {
26371 ::file_write (file, m_vec);
26372 }
26373
26374 private:
26375 std::vector<OffsetSize> m_vec;
26376 };
26377
26378 /* Base class to unify DWARF-32 and DWARF-64 .debug_names output
26379 respecting name table width. */
26380 class dwarf
26381 {
26382 public:
26383 offset_vec &name_table_string_offs, &name_table_entry_offs;
26384
26385 dwarf (offset_vec &name_table_string_offs_,
26386 offset_vec &name_table_entry_offs_)
26387 : name_table_string_offs (name_table_string_offs_),
26388 name_table_entry_offs (name_table_entry_offs_)
26389 {
26390 }
26391 };
e8f8bcb3 26392
437afbb8
JK
26393 /* Template to unify DWARF-32 and DWARF-64 .debug_names output
26394 respecting name table width. */
26395 template<typename OffsetSize>
26396 class dwarf_tmpl : public dwarf
26397 {
26398 public:
26399 explicit dwarf_tmpl (bfd_endian dwarf5_byte_order_)
26400 : dwarf (m_name_table_string_offs, m_name_table_entry_offs),
26401 m_name_table_string_offs (dwarf5_byte_order_),
26402 m_name_table_entry_offs (dwarf5_byte_order_)
26403 {}
26404
26405 private:
26406 offset_vec_tmpl<OffsetSize> m_name_table_string_offs;
26407 offset_vec_tmpl<OffsetSize> m_name_table_entry_offs;
26408 };
26409
26410 /* Try to reconstruct original DWARF tag for given partial_symbol.
26411 This function is not DWARF-5 compliant but it is sufficient for
26412 GDB as a DWARF-5 index consumer. */
26413 static int psymbol_tag (const struct partial_symbol *psym)
26414 {
26415 domain_enum domain = PSYMBOL_DOMAIN (psym);
26416 enum address_class aclass = PSYMBOL_CLASS (psym);
26417
26418 switch (domain)
26419 {
26420 case VAR_DOMAIN:
26421 switch (aclass)
26422 {
26423 case LOC_BLOCK:
26424 return DW_TAG_subprogram;
26425 case LOC_TYPEDEF:
26426 return DW_TAG_typedef;
26427 case LOC_COMPUTED:
26428 case LOC_CONST_BYTES:
26429 case LOC_OPTIMIZED_OUT:
26430 case LOC_STATIC:
26431 return DW_TAG_variable;
26432 case LOC_CONST:
26433 /* Note: It's currently impossible to recognize psyms as enum values
26434 short of reading the type info. For now punt. */
26435 return DW_TAG_variable;
26436 default:
26437 /* There are other LOC_FOO values that one might want to classify
26438 as variables, but dwarf2read.c doesn't currently use them. */
26439 return DW_TAG_variable;
26440 }
26441 case STRUCT_DOMAIN:
26442 return DW_TAG_structure_type;
26443 default:
26444 return 0;
26445 }
26446 }
26447
26448 /* Call insert for all partial symbols and mark them in PSYMS_SEEN. */
26449 void write_psymbols (std::unordered_set<partial_symbol *> &psyms_seen,
26450 struct partial_symbol **psymp, int count, int cu_index,
26451 bool is_static)
26452 {
26453 for (; count-- > 0; ++psymp)
26454 {
26455 struct partial_symbol *psym = *psymp;
26456
26457 if (SYMBOL_LANGUAGE (psym) == language_ada)
26458 error (_("Ada is not currently supported by the index"));
26459
26460 /* Only add a given psymbol once. */
26461 if (psyms_seen.insert (psym).second)
26462 insert (psym, cu_index, is_static);
26463 }
26464 }
26465
26466 /* Store value of each symbol. */
26467 std::unordered_map<c_str_view, std::set<symbol_value>, c_str_view_hasher>
26468 m_name_to_value_set;
26469
26470 /* Tables of DWARF-5 .debug_names. They are in object file byte
26471 order. */
26472 std::vector<uint32_t> m_bucket_table;
26473 std::vector<uint32_t> m_hash_table;
26474
26475 const bfd_endian m_dwarf5_byte_order;
26476 dwarf_tmpl<uint32_t> m_dwarf32;
26477 dwarf_tmpl<uint64_t> m_dwarf64;
26478 dwarf &m_dwarf;
26479 offset_vec &m_name_table_string_offs, &m_name_table_entry_offs;
26480 debug_str_lookup m_debugstrlookup;
26481
26482 /* Map each used .debug_names abbreviation tag parameter to its
26483 index value. */
26484 std::unordered_map<index_key, int, index_key_hasher> m_indexkey_to_idx;
26485
26486 /* Next unused .debug_names abbreviation tag for
26487 m_indexkey_to_idx. */
26488 int m_idx_next = 1;
26489
26490 /* .debug_names abbreviation table. */
26491 data_buf m_abbrev_table;
26492
26493 /* .debug_names entry pool. */
26494 data_buf m_entry_pool;
26495};
26496
26497/* Return iff any of the needed offsets does not fit into 32-bit
26498 .debug_names section. */
26499
26500static bool
26501check_dwarf64_offsets ()
26502{
26503 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
26504 {
26505 const dwarf2_per_cu_data &per_cu = *dwarf2_per_objfile->all_comp_units[i];
26506
26507 if (to_underlying (per_cu.sect_off) >= (static_cast<uint64_t> (1) << 32))
26508 return true;
26509 }
26510 for (int i = 0; i < dwarf2_per_objfile->n_type_units; ++i)
26511 {
26512 const signatured_type &sigtype = *dwarf2_per_objfile->all_type_units[i];
26513 const dwarf2_per_cu_data &per_cu = sigtype.per_cu;
26514
26515 if (to_underlying (per_cu.sect_off) >= (static_cast<uint64_t> (1) << 32))
26516 return true;
26517 }
26518 return false;
26519}
26520
26521/* The psyms_seen set is potentially going to be largish (~40k
26522 elements when indexing a -g3 build of GDB itself). Estimate the
26523 number of elements in order to avoid too many rehashes, which
26524 require rebuilding buckets and thus many trips to
26525 malloc/free. */
26526
26527static size_t
26528psyms_seen_size ()
26529{
e8f8bcb3
PA
26530 size_t psyms_count = 0;
26531 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
26532 {
26533 struct dwarf2_per_cu_data *per_cu
26534 = dwarf2_per_objfile->all_comp_units[i];
26535 struct partial_symtab *psymtab = per_cu->v.psymtab;
26536
26537 if (psymtab != NULL && psymtab->user == NULL)
26538 recursively_count_psymbols (psymtab, psyms_count);
26539 }
26540 /* Generating an index for gdb itself shows a ratio of
26541 TOTAL_SEEN_SYMS/UNIQUE_SYMS or ~5. 4 seems like a good bet. */
437afbb8
JK
26542 return psyms_count / 4;
26543}
26544
26545/* Write new .gdb_index section for OBJFILE into OUT_FILE.
26546 Return how many bytes were expected to be written into OUT_FILE. */
26547
26548static size_t
26549write_gdbindex (struct objfile *objfile, FILE *out_file)
26550{
26551 mapped_symtab symtab;
26552 data_buf cu_list;
26553
26554 /* While we're scanning CU's create a table that maps a psymtab pointer
26555 (which is what addrmap records) to its index (which is what is recorded
26556 in the index file). This will later be needed to write the address
26557 table. */
26558 psym_index_map cu_index_htab;
26559 cu_index_htab.reserve (dwarf2_per_objfile->n_comp_units);
26560
26561 /* The CU list is already sorted, so we don't need to do additional
26562 work here. Also, the debug_types entries do not appear in
26563 all_comp_units, but only in their own hash table. */
26564
26565 std::unordered_set<partial_symbol *> psyms_seen (psyms_seen_size ());
bc8f2430 26566 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
9291a0cd 26567 {
3e43a32a
MS
26568 struct dwarf2_per_cu_data *per_cu
26569 = dwarf2_per_objfile->all_comp_units[i];
e254ef6a 26570 struct partial_symtab *psymtab = per_cu->v.psymtab;
9291a0cd 26571
92fac807
JK
26572 /* CU of a shared file from 'dwz -m' may be unused by this main file.
26573 It may be referenced from a local scope but in such case it does not
26574 need to be present in .gdb_index. */
26575 if (psymtab == NULL)
26576 continue;
26577
95554aad 26578 if (psymtab->user == NULL)
bc8f2430
JK
26579 recursively_write_psymbols (objfile, psymtab, &symtab,
26580 psyms_seen, i);
9291a0cd 26581
bc8f2430
JK
26582 const auto insertpair = cu_index_htab.emplace (psymtab, i);
26583 gdb_assert (insertpair.second);
9291a0cd 26584
c2f134ac
PA
26585 cu_list.append_uint (8, BFD_ENDIAN_LITTLE,
26586 to_underlying (per_cu->sect_off));
26587 cu_list.append_uint (8, BFD_ENDIAN_LITTLE, per_cu->length);
9291a0cd
TT
26588 }
26589
0a5429f6 26590 /* Dump the address map. */
bc8f2430
JK
26591 data_buf addr_vec;
26592 write_address_map (objfile, addr_vec, cu_index_htab);
0a5429f6 26593
1fd400ff 26594 /* Write out the .debug_type entries, if any. */
bc8f2430 26595 data_buf types_cu_list;
1fd400ff
TT
26596 if (dwarf2_per_objfile->signatured_types)
26597 {
bc8f2430
JK
26598 signatured_type_index_data sig_data (types_cu_list,
26599 psyms_seen);
1fd400ff
TT
26600
26601 sig_data.objfile = objfile;
bc8f2430 26602 sig_data.symtab = &symtab;
1fd400ff
TT
26603 sig_data.cu_index = dwarf2_per_objfile->n_comp_units;
26604 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
26605 write_one_signatured_type, &sig_data);
26606 }
26607
156942c7
DE
26608 /* Now that we've processed all symbols we can shrink their cu_indices
26609 lists. */
bc8f2430 26610 uniquify_cu_indices (&symtab);
156942c7 26611
bc8f2430
JK
26612 data_buf symtab_vec, constant_pool;
26613 write_hash_table (&symtab, symtab_vec, constant_pool);
9291a0cd 26614
bc8f2430
JK
26615 data_buf contents;
26616 const offset_type size_of_contents = 6 * sizeof (offset_type);
26617 offset_type total_len = size_of_contents;
9291a0cd
TT
26618
26619 /* The version number. */
bc8f2430 26620 contents.append_data (MAYBE_SWAP (8));
9291a0cd
TT
26621
26622 /* The offset of the CU list from the start of the file. */
bc8f2430
JK
26623 contents.append_data (MAYBE_SWAP (total_len));
26624 total_len += cu_list.size ();
9291a0cd 26625
1fd400ff 26626 /* The offset of the types CU list from the start of the file. */
bc8f2430
JK
26627 contents.append_data (MAYBE_SWAP (total_len));
26628 total_len += types_cu_list.size ();
1fd400ff 26629
9291a0cd 26630 /* The offset of the address table from the start of the file. */
bc8f2430
JK
26631 contents.append_data (MAYBE_SWAP (total_len));
26632 total_len += addr_vec.size ();
9291a0cd
TT
26633
26634 /* The offset of the symbol table from the start of the file. */
bc8f2430
JK
26635 contents.append_data (MAYBE_SWAP (total_len));
26636 total_len += symtab_vec.size ();
9291a0cd
TT
26637
26638 /* The offset of the constant pool from the start of the file. */
bc8f2430
JK
26639 contents.append_data (MAYBE_SWAP (total_len));
26640 total_len += constant_pool.size ();
9291a0cd 26641
bc8f2430 26642 gdb_assert (contents.size () == size_of_contents);
9291a0cd 26643
bc8f2430
JK
26644 contents.file_write (out_file);
26645 cu_list.file_write (out_file);
26646 types_cu_list.file_write (out_file);
26647 addr_vec.file_write (out_file);
26648 symtab_vec.file_write (out_file);
26649 constant_pool.file_write (out_file);
9291a0cd 26650
437afbb8
JK
26651 return total_len;
26652}
26653
26654/* DWARF-5 augmentation string for GDB's DW_IDX_GNU_* extension. */
26655static const gdb_byte dwarf5_gdb_augmentation[] = { 'G', 'D', 'B', 0 };
26656
26657/* Write a new .debug_names section for OBJFILE into OUT_FILE, write
26658 needed addition to .debug_str section to OUT_FILE_STR. Return how
26659 many bytes were expected to be written into OUT_FILE. */
26660
26661static size_t
26662write_debug_names (struct objfile *objfile, FILE *out_file, FILE *out_file_str)
26663{
26664 const bool dwarf5_is_dwarf64 = check_dwarf64_offsets ();
26665 const int dwarf5_offset_size = dwarf5_is_dwarf64 ? 8 : 4;
26666 const enum bfd_endian dwarf5_byte_order
26667 = gdbarch_byte_order (get_objfile_arch (objfile));
26668
26669 /* The CU list is already sorted, so we don't need to do additional
26670 work here. Also, the debug_types entries do not appear in
26671 all_comp_units, but only in their own hash table. */
26672 data_buf cu_list;
26673 debug_names nametable (dwarf5_is_dwarf64, dwarf5_byte_order);
26674 std::unordered_set<partial_symbol *> psyms_seen (psyms_seen_size ());
26675 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
26676 {
26677 const dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->all_comp_units[i];
26678 partial_symtab *psymtab = per_cu->v.psymtab;
26679
26680 /* CU of a shared file from 'dwz -m' may be unused by this main
26681 file. It may be referenced from a local scope but in such
26682 case it does not need to be present in .debug_names. */
26683 if (psymtab == NULL)
26684 continue;
26685
26686 if (psymtab->user == NULL)
26687 nametable.recursively_write_psymbols (objfile, psymtab, psyms_seen, i);
26688
26689 cu_list.append_uint (dwarf5_offset_size, dwarf5_byte_order,
26690 to_underlying (per_cu->sect_off));
26691 }
26692 nametable.build ();
26693
26694 /* No addr_vec - DWARF-5 uses .debug_aranges generated by GCC. */
26695
26696 data_buf types_cu_list;
26697 for (int i = 0; i < dwarf2_per_objfile->n_type_units; ++i)
26698 {
26699 const signatured_type &sigtype = *dwarf2_per_objfile->all_type_units[i];
26700 const dwarf2_per_cu_data &per_cu = sigtype.per_cu;
26701
26702 types_cu_list.append_uint (dwarf5_offset_size, dwarf5_byte_order,
26703 to_underlying (per_cu.sect_off));
26704 }
26705
26706 const offset_type bytes_of_header
26707 = ((dwarf5_is_dwarf64 ? 12 : 4)
26708 + 2 + 2 + 7 * 4
26709 + sizeof (dwarf5_gdb_augmentation));
26710 size_t expected_bytes = 0;
26711 expected_bytes += bytes_of_header;
26712 expected_bytes += cu_list.size ();
26713 expected_bytes += types_cu_list.size ();
26714 expected_bytes += nametable.bytes ();
26715 data_buf header;
26716
26717 if (!dwarf5_is_dwarf64)
26718 {
26719 const uint64_t size64 = expected_bytes - 4;
26720 gdb_assert (size64 < 0xfffffff0);
26721 header.append_uint (4, dwarf5_byte_order, size64);
26722 }
26723 else
26724 {
26725 header.append_uint (4, dwarf5_byte_order, 0xffffffff);
26726 header.append_uint (8, dwarf5_byte_order, expected_bytes - 12);
26727 }
26728
26729 /* The version number. */
26730 header.append_uint (2, dwarf5_byte_order, 5);
26731
26732 /* Padding. */
26733 header.append_uint (2, dwarf5_byte_order, 0);
26734
26735 /* comp_unit_count - The number of CUs in the CU list. */
26736 header.append_uint (4, dwarf5_byte_order, dwarf2_per_objfile->n_comp_units);
26737
26738 /* local_type_unit_count - The number of TUs in the local TU
26739 list. */
26740 header.append_uint (4, dwarf5_byte_order, dwarf2_per_objfile->n_type_units);
26741
26742 /* foreign_type_unit_count - The number of TUs in the foreign TU
26743 list. */
26744 header.append_uint (4, dwarf5_byte_order, 0);
26745
26746 /* bucket_count - The number of hash buckets in the hash lookup
26747 table. */
26748 header.append_uint (4, dwarf5_byte_order, nametable.bucket_count ());
26749
26750 /* name_count - The number of unique names in the index. */
26751 header.append_uint (4, dwarf5_byte_order, nametable.name_count ());
26752
26753 /* abbrev_table_size - The size in bytes of the abbreviations
26754 table. */
26755 header.append_uint (4, dwarf5_byte_order, nametable.abbrev_table_bytes ());
26756
26757 /* augmentation_string_size - The size in bytes of the augmentation
26758 string. This value is rounded up to a multiple of 4. */
26759 static_assert (sizeof (dwarf5_gdb_augmentation) % 4 == 0, "");
26760 header.append_uint (4, dwarf5_byte_order, sizeof (dwarf5_gdb_augmentation));
26761 header.append_data (dwarf5_gdb_augmentation);
26762
26763 gdb_assert (header.size () == bytes_of_header);
26764
26765 header.file_write (out_file);
26766 cu_list.file_write (out_file);
26767 types_cu_list.file_write (out_file);
26768 nametable.file_write (out_file, out_file_str);
26769
26770 return expected_bytes;
26771}
26772
26773/* Assert that FILE's size is EXPECTED_SIZE. Assumes file's seek
26774 position is at the end of the file. */
26775
26776static void
26777assert_file_size (FILE *file, const char *filename, size_t expected_size)
26778{
26779 const auto file_size = ftell (file);
26780 if (file_size == -1)
26781 error (_("Can't get `%s' size"), filename);
26782 gdb_assert (file_size == expected_size);
26783}
26784
26785/* An index variant. */
26786enum dw_index_kind
26787{
26788 /* GDB's own .gdb_index format. */
26789 GDB_INDEX,
26790
26791 /* DWARF5 .debug_names. */
26792 DEBUG_NAMES,
26793};
26794
26795/* Create an index file for OBJFILE in the directory DIR. */
26796
26797static void
26798write_psymtabs_to_index (struct objfile *objfile, const char *dir,
26799 dw_index_kind index_kind)
26800{
26801 if (dwarf2_per_objfile->using_index)
26802 error (_("Cannot use an index to create the index"));
26803
26804 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) > 1)
26805 error (_("Cannot make an index when the file has multiple .debug_types sections"));
26806
26807 if (!objfile->psymtabs || !objfile->psymtabs_addrmap)
26808 return;
26809
26810 struct stat st;
26811 if (stat (objfile_name (objfile), &st) < 0)
26812 perror_with_name (objfile_name (objfile));
26813
26814 std::string filename (std::string (dir) + SLASH_STRING
26815 + lbasename (objfile_name (objfile))
26816 + (index_kind == dw_index_kind::DEBUG_NAMES
26817 ? INDEX5_SUFFIX : INDEX4_SUFFIX));
26818
26819 FILE *out_file = gdb_fopen_cloexec (filename.c_str (), "wb").release ();
26820 if (!out_file)
26821 error (_("Can't open `%s' for writing"), filename.c_str ());
26822
26823 /* Order matters here; we want FILE to be closed before FILENAME is
26824 unlinked, because on MS-Windows one cannot delete a file that is
26825 still open. (Don't call anything here that might throw until
26826 file_closer is created.) */
26827 gdb::unlinker unlink_file (filename.c_str ());
26828 gdb_file_up close_out_file (out_file);
26829
26830 if (index_kind == dw_index_kind::DEBUG_NAMES)
26831 {
26832 std::string filename_str (std::string (dir) + SLASH_STRING
26833 + lbasename (objfile_name (objfile))
26834 + DEBUG_STR_SUFFIX);
26835 FILE *out_file_str
26836 = gdb_fopen_cloexec (filename_str.c_str (), "wb").release ();
26837 if (!out_file_str)
26838 error (_("Can't open `%s' for writing"), filename_str.c_str ());
26839 gdb::unlinker unlink_file_str (filename_str.c_str ());
26840 gdb_file_up close_out_file_str (out_file_str);
26841
26842 const size_t total_len
26843 = write_debug_names (objfile, out_file, out_file_str);
26844 assert_file_size (out_file, filename.c_str (), total_len);
26845
26846 /* We want to keep the file .debug_str file too. */
26847 unlink_file_str.keep ();
26848 }
26849 else
26850 {
26851 const size_t total_len
26852 = write_gdbindex (objfile, out_file);
26853 assert_file_size (out_file, filename.c_str (), total_len);
26854 }
26855
bef155c3
TT
26856 /* We want to keep the file. */
26857 unlink_file.keep ();
9291a0cd
TT
26858}
26859
90476074
TT
26860/* Implementation of the `save gdb-index' command.
26861
437afbb8
JK
26862 Note that the .gdb_index file format used by this command is
26863 documented in the GDB manual. Any changes here must be documented
26864 there. */
11570e71 26865
9291a0cd 26866static void
8384c356 26867save_gdb_index_command (const char *arg, int from_tty)
9291a0cd
TT
26868{
26869 struct objfile *objfile;
437afbb8
JK
26870 const char dwarf5space[] = "-dwarf-5 ";
26871 dw_index_kind index_kind = dw_index_kind::GDB_INDEX;
26872
26873 if (!arg)
26874 arg = "";
26875
26876 arg = skip_spaces (arg);
26877 if (strncmp (arg, dwarf5space, strlen (dwarf5space)) == 0)
26878 {
26879 index_kind = dw_index_kind::DEBUG_NAMES;
26880 arg += strlen (dwarf5space);
26881 arg = skip_spaces (arg);
26882 }
9291a0cd 26883
437afbb8
JK
26884 if (!*arg)
26885 error (_("usage: save gdb-index [-dwarf-5] DIRECTORY"));
9291a0cd
TT
26886
26887 ALL_OBJFILES (objfile)
26888 {
26889 struct stat st;
26890
26891 /* If the objfile does not correspond to an actual file, skip it. */
4262abfb 26892 if (stat (objfile_name (objfile), &st) < 0)
9291a0cd
TT
26893 continue;
26894
9a3c8263
SM
26895 dwarf2_per_objfile
26896 = (struct dwarf2_per_objfile *) objfile_data (objfile,
26897 dwarf2_objfile_data_key);
9291a0cd
TT
26898 if (dwarf2_per_objfile)
26899 {
9291a0cd 26900
492d29ea 26901 TRY
9291a0cd 26902 {
437afbb8 26903 write_psymtabs_to_index (objfile, arg, index_kind);
9291a0cd 26904 }
492d29ea
PA
26905 CATCH (except, RETURN_MASK_ERROR)
26906 {
26907 exception_fprintf (gdb_stderr, except,
26908 _("Error while writing index for `%s': "),
26909 objfile_name (objfile));
26910 }
26911 END_CATCH
9291a0cd
TT
26912 }
26913 }
dce234bc
PP
26914}
26915
9291a0cd
TT
26916\f
26917
b4f54984 26918int dwarf_always_disassemble;
9eae7c52
TT
26919
26920static void
b4f54984
DE
26921show_dwarf_always_disassemble (struct ui_file *file, int from_tty,
26922 struct cmd_list_element *c, const char *value)
9eae7c52 26923{
3e43a32a
MS
26924 fprintf_filtered (file,
26925 _("Whether to always disassemble "
26926 "DWARF expressions is %s.\n"),
9eae7c52
TT
26927 value);
26928}
26929
900e11f9
JK
26930static void
26931show_check_physname (struct ui_file *file, int from_tty,
26932 struct cmd_list_element *c, const char *value)
26933{
26934 fprintf_filtered (file,
26935 _("Whether to check \"physname\" is %s.\n"),
26936 value);
26937}
26938
6502dd73
DJ
26939void
26940_initialize_dwarf2_read (void)
26941{
96d19272
JK
26942 struct cmd_list_element *c;
26943
dce234bc 26944 dwarf2_objfile_data_key
c1bd65d0 26945 = register_objfile_data_with_cleanup (NULL, dwarf2_per_objfile_free);
ae038cb0 26946
b4f54984
DE
26947 add_prefix_cmd ("dwarf", class_maintenance, set_dwarf_cmd, _("\
26948Set DWARF specific variables.\n\
26949Configure DWARF variables such as the cache size"),
26950 &set_dwarf_cmdlist, "maintenance set dwarf ",
ae038cb0
DJ
26951 0/*allow-unknown*/, &maintenance_set_cmdlist);
26952
b4f54984
DE
26953 add_prefix_cmd ("dwarf", class_maintenance, show_dwarf_cmd, _("\
26954Show DWARF specific variables\n\
26955Show DWARF variables such as the cache size"),
26956 &show_dwarf_cmdlist, "maintenance show dwarf ",
ae038cb0
DJ
26957 0/*allow-unknown*/, &maintenance_show_cmdlist);
26958
26959 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
b4f54984
DE
26960 &dwarf_max_cache_age, _("\
26961Set the upper bound on the age of cached DWARF compilation units."), _("\
26962Show the upper bound on the age of cached DWARF compilation units."), _("\
7915a72c
AC
26963A higher limit means that cached compilation units will be stored\n\
26964in memory longer, and more total memory will be used. Zero disables\n\
26965caching, which can slow down startup."),
2c5b56ce 26966 NULL,
b4f54984
DE
26967 show_dwarf_max_cache_age,
26968 &set_dwarf_cmdlist,
26969 &show_dwarf_cmdlist);
d97bc12b 26970
9eae7c52 26971 add_setshow_boolean_cmd ("always-disassemble", class_obscure,
b4f54984 26972 &dwarf_always_disassemble, _("\
9eae7c52
TT
26973Set whether `info address' always disassembles DWARF expressions."), _("\
26974Show whether `info address' always disassembles DWARF expressions."), _("\
26975When enabled, DWARF expressions are always printed in an assembly-like\n\
26976syntax. When disabled, expressions will be printed in a more\n\
26977conversational style, when possible."),
26978 NULL,
b4f54984
DE
26979 show_dwarf_always_disassemble,
26980 &set_dwarf_cmdlist,
26981 &show_dwarf_cmdlist);
26982
26983 add_setshow_zuinteger_cmd ("dwarf-read", no_class, &dwarf_read_debug, _("\
26984Set debugging of the DWARF reader."), _("\
26985Show debugging of the DWARF reader."), _("\
26986When enabled (non-zero), debugging messages are printed during DWARF\n\
73be47f5
DE
26987reading and symtab expansion. A value of 1 (one) provides basic\n\
26988information. A value greater than 1 provides more verbose information."),
45cfd468
DE
26989 NULL,
26990 NULL,
26991 &setdebuglist, &showdebuglist);
26992
b4f54984
DE
26993 add_setshow_zuinteger_cmd ("dwarf-die", no_class, &dwarf_die_debug, _("\
26994Set debugging of the DWARF DIE reader."), _("\
26995Show debugging of the DWARF DIE reader."), _("\
d97bc12b
DE
26996When enabled (non-zero), DIEs are dumped after they are read in.\n\
26997The value is the maximum depth to print."),
ccce17b0
YQ
26998 NULL,
26999 NULL,
27000 &setdebuglist, &showdebuglist);
9291a0cd 27001
27e0867f
DE
27002 add_setshow_zuinteger_cmd ("dwarf-line", no_class, &dwarf_line_debug, _("\
27003Set debugging of the dwarf line reader."), _("\
27004Show debugging of the dwarf line reader."), _("\
27005When enabled (non-zero), line number entries are dumped as they are read in.\n\
27006A value of 1 (one) provides basic information.\n\
27007A value greater than 1 provides more verbose information."),
27008 NULL,
27009 NULL,
27010 &setdebuglist, &showdebuglist);
27011
900e11f9
JK
27012 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
27013Set cross-checking of \"physname\" code against demangler."), _("\
27014Show cross-checking of \"physname\" code against demangler."), _("\
27015When enabled, GDB's internal \"physname\" code is checked against\n\
27016the demangler."),
27017 NULL, show_check_physname,
27018 &setdebuglist, &showdebuglist);
27019
e615022a
DE
27020 add_setshow_boolean_cmd ("use-deprecated-index-sections",
27021 no_class, &use_deprecated_index_sections, _("\
27022Set whether to use deprecated gdb_index sections."), _("\
27023Show whether to use deprecated gdb_index sections."), _("\
27024When enabled, deprecated .gdb_index sections are used anyway.\n\
27025Normally they are ignored either because of a missing feature or\n\
27026performance issue.\n\
27027Warning: This option must be enabled before gdb reads the file."),
27028 NULL,
27029 NULL,
27030 &setlist, &showlist);
27031
96d19272 27032 c = add_cmd ("gdb-index", class_files, save_gdb_index_command,
11570e71 27033 _("\
fc1a9d6e 27034Save a gdb-index file.\n\
437afbb8
JK
27035Usage: save gdb-index [-dwarf-5] DIRECTORY\n\
27036\n\
27037No options create one file with .gdb-index extension for pre-DWARF-5\n\
27038compatible .gdb_index section. With -dwarf-5 creates two files with\n\
27039extension .debug_names and .debug_str for DWARF-5 .debug_names section."),
96d19272
JK
27040 &save_cmdlist);
27041 set_cmd_completer (c, filename_completer);
f1e6e072
TT
27042
27043 dwarf2_locexpr_index = register_symbol_computed_impl (LOC_COMPUTED,
27044 &dwarf2_locexpr_funcs);
27045 dwarf2_loclist_index = register_symbol_computed_impl (LOC_COMPUTED,
27046 &dwarf2_loclist_funcs);
27047
27048 dwarf2_locexpr_block_index = register_symbol_block_impl (LOC_BLOCK,
27049 &dwarf2_block_frame_base_locexpr_funcs);
27050 dwarf2_loclist_block_index = register_symbol_block_impl (LOC_BLOCK,
27051 &dwarf2_block_frame_base_loclist_funcs);
c62446b1
PA
27052
27053#if GDB_SELF_TEST
27054 selftests::register_test ("dw2_expand_symtabs_matching",
27055 selftests::dw2_expand_symtabs_matching::run_test);
27056#endif
6502dd73 27057}
This page took 4.280447 seconds and 4 git commands to generate.