Use new to allocate mapped_index
[deliverable/binutils-gdb.git] / gdb / dwarf2read.c
CommitLineData
c906108c 1/* DWARF 2 debugging format support for GDB.
917c78fc 2
e2882c85 3 Copyright (C) 1994-2018 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 31#include "defs.h"
cd4fb1b2
SM
32#include "dwarf2read.h"
33#include "dwarf-index-common.h"
c906108c 34#include "bfd.h"
80626a55 35#include "elf-bfd.h"
c906108c
SS
36#include "symtab.h"
37#include "gdbtypes.h"
c906108c 38#include "objfiles.h"
fa8f86ff 39#include "dwarf2.h"
c906108c
SS
40#include "buildsym.h"
41#include "demangle.h"
50f182aa 42#include "gdb-demangle.h"
c906108c 43#include "expression.h"
d5166ae1 44#include "filenames.h" /* for DOSish file names */
2e276125 45#include "macrotab.h"
c906108c
SS
46#include "language.h"
47#include "complaints.h"
357e46e7 48#include "bcache.h"
4c2df51b
DJ
49#include "dwarf2expr.h"
50#include "dwarf2loc.h"
9219021c 51#include "cp-support.h"
72bf9492 52#include "hashtab.h"
ae038cb0
DJ
53#include "command.h"
54#include "gdbcmd.h"
edb3359d 55#include "block.h"
ff013f42 56#include "addrmap.h"
94af9270 57#include "typeprint.h"
ccefe4c4 58#include "psympriv.h"
53ce3c39 59#include <sys/stat.h>
96d19272 60#include "completer.h"
34eaf542 61#include "vec.h"
98bfdba5 62#include "c-lang.h"
a766d390 63#include "go-lang.h"
98bfdba5 64#include "valprint.h"
3019eac3 65#include "gdbcore.h" /* for gnutarget */
156942c7 66#include "gdb/gdb-index.h"
60d5a603 67#include <ctype.h>
cbb099e8 68#include "gdb_bfd.h"
4357ac6c 69#include "f-lang.h"
05cba821 70#include "source.h"
614c279d 71#include "filestuff.h"
dc294be5 72#include "build-id.h"
22cee43f 73#include "namespace.h"
bef155c3 74#include "common/gdb_unlinker.h"
14bc53a8 75#include "common/function-view.h"
ecfb656c
PA
76#include "common/gdb_optional.h"
77#include "common/underlying.h"
d5722aa2 78#include "common/byte-vector.h"
927aa2e7 79#include "common/hash_enum.h"
bbf2f4df 80#include "filename-seen-cache.h"
b32b108a 81#include "producer.h"
c906108c 82#include <fcntl.h>
c906108c 83#include <sys/types.h>
325fac50 84#include <algorithm>
bc8f2430
JK
85#include <unordered_set>
86#include <unordered_map>
c62446b1 87#include "selftest.h"
437afbb8
JK
88#include <cmath>
89#include <set>
90#include <forward_list>
c9317f21 91#include "rust-lang.h"
b4987c95 92#include "common/pathstuff.h"
437afbb8 93
73be47f5
DE
94/* When == 1, print basic high level tracing messages.
95 When > 1, be more verbose.
b4f54984
DE
96 This is in contrast to the low level DIE reading of dwarf_die_debug. */
97static unsigned int dwarf_read_debug = 0;
45cfd468 98
d97bc12b 99/* When non-zero, dump DIEs after they are read in. */
b4f54984 100static unsigned int dwarf_die_debug = 0;
d97bc12b 101
27e0867f
DE
102/* When non-zero, dump line number entries as they are read in. */
103static unsigned int dwarf_line_debug = 0;
104
900e11f9
JK
105/* When non-zero, cross-check physname against demangler. */
106static int check_physname = 0;
107
481860b3 108/* When non-zero, do not reject deprecated .gdb_index sections. */
e615022a 109static int use_deprecated_index_sections = 0;
481860b3 110
6502dd73
DJ
111static const struct objfile_data *dwarf2_objfile_data_key;
112
f1e6e072
TT
113/* The "aclass" indices for various kinds of computed DWARF symbols. */
114
115static int dwarf2_locexpr_index;
116static int dwarf2_loclist_index;
117static int dwarf2_locexpr_block_index;
118static int dwarf2_loclist_block_index;
119
3f563c84
PA
120/* An index into a (C++) symbol name component in a symbol name as
121 recorded in the mapped_index's symbol table. For each C++ symbol
122 in the symbol table, we record one entry for the start of each
123 component in the symbol in a table of name components, and then
124 sort the table, in order to be able to binary search symbol names,
125 ignoring leading namespaces, both completion and regular look up.
126 For example, for symbol "A::B::C", we'll have an entry that points
127 to "A::B::C", another that points to "B::C", and another for "C".
128 Note that function symbols in GDB index have no parameter
129 information, just the function/method names. You can convert a
130 name_component to a "const char *" using the
131 'mapped_index::symbol_name_at(offset_type)' method. */
132
133struct name_component
134{
135 /* Offset in the symbol name where the component starts. Stored as
136 a (32-bit) offset instead of a pointer to save memory and improve
137 locality on 64-bit architectures. */
138 offset_type name_offset;
139
140 /* The symbol's index in the symbol and constant pool tables of a
141 mapped_index. */
142 offset_type idx;
143};
144
44ed8f3e
PA
145/* Base class containing bits shared by both .gdb_index and
146 .debug_name indexes. */
147
148struct mapped_index_base
149{
150 /* The name_component table (a sorted vector). See name_component's
151 description above. */
152 std::vector<name_component> name_components;
153
154 /* How NAME_COMPONENTS is sorted. */
155 enum case_sensitivity name_components_casing;
156
157 /* Return the number of names in the symbol table. */
158 virtual size_t symbol_name_count () const = 0;
159
160 /* Get the name of the symbol at IDX in the symbol table. */
161 virtual const char *symbol_name_at (offset_type idx) const = 0;
162
163 /* Return whether the name at IDX in the symbol table should be
164 ignored. */
165 virtual bool symbol_name_slot_invalid (offset_type idx) const
166 {
167 return false;
168 }
169
170 /* Build the symbol name component sorted vector, if we haven't
171 yet. */
172 void build_name_components ();
173
174 /* Returns the lower (inclusive) and upper (exclusive) bounds of the
175 possible matches for LN_NO_PARAMS in the name component
176 vector. */
177 std::pair<std::vector<name_component>::const_iterator,
178 std::vector<name_component>::const_iterator>
179 find_name_components_bounds (const lookup_name_info &ln_no_params) const;
180
181 /* Prevent deleting/destroying via a base class pointer. */
182protected:
183 ~mapped_index_base() = default;
184};
185
9291a0cd
TT
186/* A description of the mapped index. The file format is described in
187 a comment by the code that writes the index. */
fc898b42 188struct mapped_index final : public mapped_index_base
9291a0cd 189{
f00a2de2
PA
190 /* A slot/bucket in the symbol table hash. */
191 struct symbol_table_slot
192 {
193 const offset_type name;
194 const offset_type vec;
195 };
196
559a7a62 197 /* Index data format version. */
3063847f 198 int version = 0;
559a7a62 199
f00a2de2
PA
200 /* The address table data. */
201 gdb::array_view<const gdb_byte> address_table;
b11b1f88 202
3876f04e 203 /* The symbol table, implemented as a hash table. */
f00a2de2 204 gdb::array_view<symbol_table_slot> symbol_table;
b11b1f88 205
9291a0cd 206 /* A pointer to the constant pool. */
3063847f 207 const char *constant_pool = nullptr;
3f563c84 208
44ed8f3e
PA
209 bool symbol_name_slot_invalid (offset_type idx) const override
210 {
211 const auto &bucket = this->symbol_table[idx];
212 return bucket.name == 0 && bucket.vec;
213 }
5c58de74 214
3f563c84
PA
215 /* Convenience method to get at the name of the symbol at IDX in the
216 symbol table. */
44ed8f3e 217 const char *symbol_name_at (offset_type idx) const override
f00a2de2 218 { return this->constant_pool + MAYBE_SWAP (this->symbol_table[idx].name); }
5c58de74 219
44ed8f3e
PA
220 size_t symbol_name_count () const override
221 { return this->symbol_table.size (); }
9291a0cd
TT
222};
223
927aa2e7
JK
224/* A description of the mapped .debug_names.
225 Uninitialized map has CU_COUNT 0. */
fc898b42 226struct mapped_debug_names final : public mapped_index_base
927aa2e7 227{
ed2dc618
SM
228 mapped_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile_)
229 : dwarf2_per_objfile (dwarf2_per_objfile_)
230 {}
231
232 struct dwarf2_per_objfile *dwarf2_per_objfile;
927aa2e7
JK
233 bfd_endian dwarf5_byte_order;
234 bool dwarf5_is_dwarf64;
235 bool augmentation_is_gdb;
236 uint8_t offset_size;
237 uint32_t cu_count = 0;
238 uint32_t tu_count, bucket_count, name_count;
239 const gdb_byte *cu_table_reordered, *tu_table_reordered;
240 const uint32_t *bucket_table_reordered, *hash_table_reordered;
241 const gdb_byte *name_table_string_offs_reordered;
242 const gdb_byte *name_table_entry_offs_reordered;
243 const gdb_byte *entry_pool;
244
245 struct index_val
246 {
247 ULONGEST dwarf_tag;
248 struct attr
249 {
250 /* Attribute name DW_IDX_*. */
251 ULONGEST dw_idx;
252
253 /* Attribute form DW_FORM_*. */
254 ULONGEST form;
255
256 /* Value if FORM is DW_FORM_implicit_const. */
257 LONGEST implicit_const;
258 };
259 std::vector<attr> attr_vec;
260 };
261
262 std::unordered_map<ULONGEST, index_val> abbrev_map;
263
264 const char *namei_to_name (uint32_t namei) const;
44ed8f3e
PA
265
266 /* Implementation of the mapped_index_base virtual interface, for
267 the name_components cache. */
268
269 const char *symbol_name_at (offset_type idx) const override
270 { return namei_to_name (idx); }
271
272 size_t symbol_name_count () const override
273 { return this->name_count; }
927aa2e7
JK
274};
275
cd4fb1b2 276/* See dwarf2read.h. */
ed2dc618 277
cd4fb1b2 278dwarf2_per_objfile *
ed2dc618
SM
279get_dwarf2_per_objfile (struct objfile *objfile)
280{
281 return ((struct dwarf2_per_objfile *)
282 objfile_data (objfile, dwarf2_objfile_data_key));
283}
284
285/* Set the dwarf2_per_objfile associated to OBJFILE. */
286
287void
288set_dwarf2_per_objfile (struct objfile *objfile,
289 struct dwarf2_per_objfile *dwarf2_per_objfile)
290{
291 gdb_assert (get_dwarf2_per_objfile (objfile) == NULL);
292 set_objfile_data (objfile, dwarf2_objfile_data_key, dwarf2_per_objfile);
293}
c906108c 294
251d32d9 295/* Default names of the debugging sections. */
c906108c 296
233a11ab
CS
297/* Note that if the debugging section has been compressed, it might
298 have a name like .zdebug_info. */
299
9cdd5dbd
DE
300static const struct dwarf2_debug_sections dwarf2_elf_names =
301{
251d32d9
TG
302 { ".debug_info", ".zdebug_info" },
303 { ".debug_abbrev", ".zdebug_abbrev" },
304 { ".debug_line", ".zdebug_line" },
305 { ".debug_loc", ".zdebug_loc" },
43988095 306 { ".debug_loclists", ".zdebug_loclists" },
251d32d9 307 { ".debug_macinfo", ".zdebug_macinfo" },
cf2c3c16 308 { ".debug_macro", ".zdebug_macro" },
251d32d9 309 { ".debug_str", ".zdebug_str" },
43988095 310 { ".debug_line_str", ".zdebug_line_str" },
251d32d9 311 { ".debug_ranges", ".zdebug_ranges" },
43988095 312 { ".debug_rnglists", ".zdebug_rnglists" },
251d32d9 313 { ".debug_types", ".zdebug_types" },
3019eac3 314 { ".debug_addr", ".zdebug_addr" },
251d32d9
TG
315 { ".debug_frame", ".zdebug_frame" },
316 { ".eh_frame", NULL },
24d3216f 317 { ".gdb_index", ".zgdb_index" },
927aa2e7
JK
318 { ".debug_names", ".zdebug_names" },
319 { ".debug_aranges", ".zdebug_aranges" },
24d3216f 320 23
251d32d9 321};
c906108c 322
80626a55 323/* List of DWO/DWP sections. */
3019eac3 324
80626a55 325static const struct dwop_section_names
3019eac3
DE
326{
327 struct dwarf2_section_names abbrev_dwo;
328 struct dwarf2_section_names info_dwo;
329 struct dwarf2_section_names line_dwo;
330 struct dwarf2_section_names loc_dwo;
43988095 331 struct dwarf2_section_names loclists_dwo;
09262596
DE
332 struct dwarf2_section_names macinfo_dwo;
333 struct dwarf2_section_names macro_dwo;
3019eac3
DE
334 struct dwarf2_section_names str_dwo;
335 struct dwarf2_section_names str_offsets_dwo;
336 struct dwarf2_section_names types_dwo;
80626a55
DE
337 struct dwarf2_section_names cu_index;
338 struct dwarf2_section_names tu_index;
3019eac3 339}
80626a55 340dwop_section_names =
3019eac3
DE
341{
342 { ".debug_abbrev.dwo", ".zdebug_abbrev.dwo" },
343 { ".debug_info.dwo", ".zdebug_info.dwo" },
344 { ".debug_line.dwo", ".zdebug_line.dwo" },
345 { ".debug_loc.dwo", ".zdebug_loc.dwo" },
43988095 346 { ".debug_loclists.dwo", ".zdebug_loclists.dwo" },
09262596
DE
347 { ".debug_macinfo.dwo", ".zdebug_macinfo.dwo" },
348 { ".debug_macro.dwo", ".zdebug_macro.dwo" },
3019eac3
DE
349 { ".debug_str.dwo", ".zdebug_str.dwo" },
350 { ".debug_str_offsets.dwo", ".zdebug_str_offsets.dwo" },
351 { ".debug_types.dwo", ".zdebug_types.dwo" },
80626a55
DE
352 { ".debug_cu_index", ".zdebug_cu_index" },
353 { ".debug_tu_index", ".zdebug_tu_index" },
3019eac3
DE
354};
355
c906108c
SS
356/* local data types */
357
107d2387
AC
358/* The data in a compilation unit header, after target2host
359 translation, looks like this. */
c906108c 360struct comp_unit_head
a738430d 361{
c764a876 362 unsigned int length;
a738430d 363 short version;
a738430d
MK
364 unsigned char addr_size;
365 unsigned char signed_addr_p;
9c541725 366 sect_offset abbrev_sect_off;
57349743 367
a738430d
MK
368 /* Size of file offsets; either 4 or 8. */
369 unsigned int offset_size;
57349743 370
a738430d
MK
371 /* Size of the length field; either 4 or 12. */
372 unsigned int initial_length_size;
57349743 373
43988095
JK
374 enum dwarf_unit_type unit_type;
375
a738430d
MK
376 /* Offset to the first byte of this compilation unit header in the
377 .debug_info section, for resolving relative reference dies. */
9c541725 378 sect_offset sect_off;
57349743 379
d00adf39
DE
380 /* Offset to first die in this cu from the start of the cu.
381 This will be the first byte following the compilation unit header. */
9c541725 382 cu_offset first_die_cu_offset;
43988095
JK
383
384 /* 64-bit signature of this type unit - it is valid only for
385 UNIT_TYPE DW_UT_type. */
386 ULONGEST signature;
387
388 /* For types, offset in the type's DIE of the type defined by this TU. */
9c541725 389 cu_offset type_cu_offset_in_tu;
a738430d 390};
c906108c 391
3da10d80
KS
392/* Type used for delaying computation of method physnames.
393 See comments for compute_delayed_physnames. */
394struct delayed_method_info
395{
396 /* The type to which the method is attached, i.e., its parent class. */
397 struct type *type;
398
399 /* The index of the method in the type's function fieldlists. */
400 int fnfield_index;
401
402 /* The index of the method in the fieldlist. */
403 int index;
404
405 /* The name of the DIE. */
406 const char *name;
407
408 /* The DIE associated with this method. */
409 struct die_info *die;
410};
411
e7c27a73
DJ
412/* Internal state when decoding a particular compilation unit. */
413struct dwarf2_cu
414{
fcd3b13d
SM
415 explicit dwarf2_cu (struct dwarf2_per_cu_data *per_cu);
416 ~dwarf2_cu ();
417
418 DISABLE_COPY_AND_ASSIGN (dwarf2_cu);
419
d00adf39 420 /* The header of the compilation unit. */
fcd3b13d 421 struct comp_unit_head header {};
e142c38c 422
d00adf39 423 /* Base address of this compilation unit. */
fcd3b13d 424 CORE_ADDR base_address = 0;
d00adf39
DE
425
426 /* Non-zero if base_address has been set. */
fcd3b13d 427 int base_known = 0;
d00adf39 428
e142c38c 429 /* The language we are debugging. */
fcd3b13d
SM
430 enum language language = language_unknown;
431 const struct language_defn *language_defn = nullptr;
e142c38c 432
fcd3b13d 433 const char *producer = nullptr;
b0f35d58 434
e142c38c
DJ
435 /* The generic symbol table building routines have separate lists for
436 file scope symbols and all all other scopes (local scopes). So
437 we need to select the right one to pass to add_symbol_to_list().
438 We do it by keeping a pointer to the correct list in list_in_scope.
439
440 FIXME: The original dwarf code just treated the file scope as the
441 first local scope, and all other local scopes as nested local
442 scopes, and worked fine. Check to see if we really need to
443 distinguish these in buildsym.c. */
fcd3b13d 444 struct pending **list_in_scope = nullptr;
e142c38c 445
b64f50a1
JK
446 /* Hash table holding all the loaded partial DIEs
447 with partial_die->offset.SECT_OFF as hash. */
fcd3b13d 448 htab_t partial_dies = nullptr;
72bf9492
DJ
449
450 /* Storage for things with the same lifetime as this read-in compilation
451 unit, including partial DIEs. */
fcd3b13d 452 auto_obstack comp_unit_obstack;
72bf9492 453
ae038cb0
DJ
454 /* When multiple dwarf2_cu structures are living in memory, this field
455 chains them all together, so that they can be released efficiently.
456 We will probably also want a generation counter so that most-recently-used
457 compilation units are cached... */
fcd3b13d 458 struct dwarf2_per_cu_data *read_in_chain = nullptr;
ae038cb0 459
69d751e3 460 /* Backlink to our per_cu entry. */
ae038cb0
DJ
461 struct dwarf2_per_cu_data *per_cu;
462
463 /* How many compilation units ago was this CU last referenced? */
fcd3b13d 464 int last_used = 0;
ae038cb0 465
b64f50a1
JK
466 /* A hash table of DIE cu_offset for following references with
467 die_info->offset.sect_off as hash. */
fcd3b13d 468 htab_t die_hash = nullptr;
10b3939b
DJ
469
470 /* Full DIEs if read in. */
fcd3b13d 471 struct die_info *dies = nullptr;
10b3939b
DJ
472
473 /* A set of pointers to dwarf2_per_cu_data objects for compilation
474 units referenced by this one. Only set during full symbol processing;
475 partial symbol tables do not have dependencies. */
fcd3b13d 476 htab_t dependencies = nullptr;
10b3939b 477
cb1df416 478 /* Header data from the line table, during full symbol processing. */
fcd3b13d 479 struct line_header *line_header = nullptr;
4c8aa72d
PA
480 /* Non-NULL if LINE_HEADER is owned by this DWARF_CU. Otherwise,
481 it's owned by dwarf2_per_objfile::line_header_hash. If non-NULL,
482 this is the DW_TAG_compile_unit die for this CU. We'll hold on
483 to the line header as long as this DIE is being processed. See
484 process_die_scope. */
fcd3b13d 485 die_info *line_header_die_owner = nullptr;
cb1df416 486
3da10d80
KS
487 /* A list of methods which need to have physnames computed
488 after all type information has been read. */
c89b44cd 489 std::vector<delayed_method_info> method_list;
3da10d80 490
96408a79 491 /* To be copied to symtab->call_site_htab. */
fcd3b13d 492 htab_t call_site_htab = nullptr;
96408a79 493
034e5797
DE
494 /* Non-NULL if this CU came from a DWO file.
495 There is an invariant here that is important to remember:
496 Except for attributes copied from the top level DIE in the "main"
497 (or "stub") file in preparation for reading the DWO file
498 (e.g., DW_AT_GNU_addr_base), we KISS: there is only *one* CU.
499 Either there isn't a DWO file (in which case this is NULL and the point
500 is moot), or there is and either we're not going to read it (in which
501 case this is NULL) or there is and we are reading it (in which case this
502 is non-NULL). */
fcd3b13d 503 struct dwo_unit *dwo_unit = nullptr;
3019eac3
DE
504
505 /* The DW_AT_addr_base attribute if present, zero otherwise
506 (zero is a valid value though).
1dbab08b 507 Note this value comes from the Fission stub CU/TU's DIE. */
fcd3b13d 508 ULONGEST addr_base = 0;
3019eac3 509
2e3cf129
DE
510 /* The DW_AT_ranges_base attribute if present, zero otherwise
511 (zero is a valid value though).
1dbab08b 512 Note this value comes from the Fission stub CU/TU's DIE.
2e3cf129 513 Also note that the value is zero in the non-DWO case so this value can
ab435259
DE
514 be used without needing to know whether DWO files are in use or not.
515 N.B. This does not apply to DW_AT_ranges appearing in
516 DW_TAG_compile_unit dies. This is a bit of a wart, consider if ever
517 DW_AT_ranges appeared in the DW_TAG_compile_unit of DWO DIEs: then
518 DW_AT_ranges_base *would* have to be applied, and we'd have to care
519 whether the DW_AT_ranges attribute came from the skeleton or DWO. */
fcd3b13d 520 ULONGEST ranges_base = 0;
2e3cf129 521
c9317f21
TT
522 /* When reading debug info generated by older versions of rustc, we
523 have to rewrite some union types to be struct types with a
524 variant part. This rewriting must be done after the CU is fully
525 read in, because otherwise at the point of rewriting some struct
526 type might not have been fully processed. So, we keep a list of
527 all such types here and process them after expansion. */
528 std::vector<struct type *> rust_unions;
529
ae038cb0
DJ
530 /* Mark used when releasing cached dies. */
531 unsigned int mark : 1;
532
8be455d7
JK
533 /* This CU references .debug_loc. See the symtab->locations_valid field.
534 This test is imperfect as there may exist optimized debug code not using
535 any location list and still facing inlining issues if handled as
536 unoptimized code. For a future better test see GCC PR other/32998. */
8be455d7 537 unsigned int has_loclist : 1;
ba919b58 538
1b80a9fa
JK
539 /* These cache the results for producer_is_* fields. CHECKED_PRODUCER is set
540 if all the producer_is_* fields are valid. This information is cached
541 because profiling CU expansion showed excessive time spent in
542 producer_is_gxx_lt_4_6. */
ba919b58
TT
543 unsigned int checked_producer : 1;
544 unsigned int producer_is_gxx_lt_4_6 : 1;
1b80a9fa 545 unsigned int producer_is_gcc_lt_4_3 : 1;
5230b05a 546 unsigned int producer_is_icc_lt_14 : 1;
4d4ec4e5
TT
547
548 /* When set, the file that we're processing is known to have
549 debugging info for C++ namespaces. GCC 3.3.x did not produce
550 this information, but later versions do. */
551
552 unsigned int processing_has_namespace_info : 1;
d590ff25
YQ
553
554 struct partial_die_info *find_partial_die (sect_offset sect_off);
e7c27a73
DJ
555};
556
094b34ac
DE
557/* A struct that can be used as a hash key for tables based on DW_AT_stmt_list.
558 This includes type_unit_group and quick_file_names. */
559
560struct stmt_list_hash
561{
562 /* The DWO unit this table is from or NULL if there is none. */
563 struct dwo_unit *dwo_unit;
564
565 /* Offset in .debug_line or .debug_line.dwo. */
9c541725 566 sect_offset line_sect_off;
094b34ac
DE
567};
568
f4dc4d17
DE
569/* Each element of dwarf2_per_objfile->type_unit_groups is a pointer to
570 an object of this type. */
571
572struct type_unit_group
573{
0186c6a7 574 /* dwarf2read.c's main "handle" on a TU symtab.
f4dc4d17
DE
575 To simplify things we create an artificial CU that "includes" all the
576 type units using this stmt_list so that the rest of the code still has
577 a "per_cu" handle on the symtab.
578 This PER_CU is recognized by having no section. */
8a0459fd 579#define IS_TYPE_UNIT_GROUP(per_cu) ((per_cu)->section == NULL)
094b34ac
DE
580 struct dwarf2_per_cu_data per_cu;
581
0186c6a7
DE
582 /* The TUs that share this DW_AT_stmt_list entry.
583 This is added to while parsing type units to build partial symtabs,
584 and is deleted afterwards and not used again. */
585 VEC (sig_type_ptr) *tus;
f4dc4d17 586
43f3e411 587 /* The compunit symtab.
094b34ac 588 Type units in a group needn't all be defined in the same source file,
43f3e411
DE
589 so we create an essentially anonymous symtab as the compunit symtab. */
590 struct compunit_symtab *compunit_symtab;
f4dc4d17 591
094b34ac
DE
592 /* The data used to construct the hash key. */
593 struct stmt_list_hash hash;
f4dc4d17
DE
594
595 /* The number of symtabs from the line header.
596 The value here must match line_header.num_file_names. */
597 unsigned int num_symtabs;
598
599 /* The symbol tables for this TU (obtained from the files listed in
600 DW_AT_stmt_list).
601 WARNING: The order of entries here must match the order of entries
602 in the line header. After the first TU using this type_unit_group, the
603 line header for the subsequent TUs is recreated from this. This is done
604 because we need to use the same symtabs for each TU using the same
605 DW_AT_stmt_list value. Also note that symtabs may be repeated here,
606 there's no guarantee the line header doesn't have duplicate entries. */
607 struct symtab **symtabs;
608};
609
73869dc2 610/* These sections are what may appear in a (real or virtual) DWO file. */
3019eac3
DE
611
612struct dwo_sections
613{
614 struct dwarf2_section_info abbrev;
3019eac3
DE
615 struct dwarf2_section_info line;
616 struct dwarf2_section_info loc;
43988095 617 struct dwarf2_section_info loclists;
09262596
DE
618 struct dwarf2_section_info macinfo;
619 struct dwarf2_section_info macro;
3019eac3
DE
620 struct dwarf2_section_info str;
621 struct dwarf2_section_info str_offsets;
80626a55
DE
622 /* In the case of a virtual DWO file, these two are unused. */
623 struct dwarf2_section_info info;
3019eac3
DE
624 VEC (dwarf2_section_info_def) *types;
625};
626
c88ee1f0 627/* CUs/TUs in DWP/DWO files. */
3019eac3
DE
628
629struct dwo_unit
630{
631 /* Backlink to the containing struct dwo_file. */
632 struct dwo_file *dwo_file;
633
634 /* The "id" that distinguishes this CU/TU.
635 .debug_info calls this "dwo_id", .debug_types calls this "signature".
636 Since signatures came first, we stick with it for consistency. */
637 ULONGEST signature;
638
639 /* The section this CU/TU lives in, in the DWO file. */
8a0459fd 640 struct dwarf2_section_info *section;
3019eac3 641
9c541725
PA
642 /* Same as dwarf2_per_cu_data:{sect_off,length} but in the DWO section. */
643 sect_offset sect_off;
3019eac3
DE
644 unsigned int length;
645
646 /* For types, offset in the type's DIE of the type defined by this TU. */
647 cu_offset type_offset_in_tu;
648};
649
73869dc2
DE
650/* include/dwarf2.h defines the DWP section codes.
651 It defines a max value but it doesn't define a min value, which we
652 use for error checking, so provide one. */
653
654enum dwp_v2_section_ids
655{
656 DW_SECT_MIN = 1
657};
658
80626a55 659/* Data for one DWO file.
57d63ce2
DE
660
661 This includes virtual DWO files (a virtual DWO file is a DWO file as it
662 appears in a DWP file). DWP files don't really have DWO files per se -
663 comdat folding of types "loses" the DWO file they came from, and from
664 a high level view DWP files appear to contain a mass of random types.
665 However, to maintain consistency with the non-DWP case we pretend DWP
666 files contain virtual DWO files, and we assign each TU with one virtual
667 DWO file (generally based on the line and abbrev section offsets -
668 a heuristic that seems to work in practice). */
3019eac3
DE
669
670struct dwo_file
671{
0ac5b59e 672 /* The DW_AT_GNU_dwo_name attribute.
80626a55
DE
673 For virtual DWO files the name is constructed from the section offsets
674 of abbrev,line,loc,str_offsets so that we combine virtual DWO files
675 from related CU+TUs. */
0ac5b59e
DE
676 const char *dwo_name;
677
678 /* The DW_AT_comp_dir attribute. */
679 const char *comp_dir;
3019eac3 680
80626a55
DE
681 /* The bfd, when the file is open. Otherwise this is NULL.
682 This is unused(NULL) for virtual DWO files where we use dwp_file.dbfd. */
683 bfd *dbfd;
3019eac3 684
73869dc2
DE
685 /* The sections that make up this DWO file.
686 Remember that for virtual DWO files in DWP V2, these are virtual
687 sections (for lack of a better name). */
3019eac3
DE
688 struct dwo_sections sections;
689
33c5cd75
DB
690 /* The CUs in the file.
691 Each element is a struct dwo_unit. Multiple CUs per DWO are supported as
692 an extension to handle LLVM's Link Time Optimization output (where
693 multiple source files may be compiled into a single object/dwo pair). */
694 htab_t cus;
3019eac3
DE
695
696 /* Table of TUs in the file.
697 Each element is a struct dwo_unit. */
698 htab_t tus;
699};
700
80626a55
DE
701/* These sections are what may appear in a DWP file. */
702
703struct dwp_sections
704{
73869dc2 705 /* These are used by both DWP version 1 and 2. */
80626a55
DE
706 struct dwarf2_section_info str;
707 struct dwarf2_section_info cu_index;
708 struct dwarf2_section_info tu_index;
73869dc2
DE
709
710 /* These are only used by DWP version 2 files.
711 In DWP version 1 the .debug_info.dwo, .debug_types.dwo, and other
712 sections are referenced by section number, and are not recorded here.
713 In DWP version 2 there is at most one copy of all these sections, each
714 section being (effectively) comprised of the concatenation of all of the
715 individual sections that exist in the version 1 format.
716 To keep the code simple we treat each of these concatenated pieces as a
717 section itself (a virtual section?). */
718 struct dwarf2_section_info abbrev;
719 struct dwarf2_section_info info;
720 struct dwarf2_section_info line;
721 struct dwarf2_section_info loc;
722 struct dwarf2_section_info macinfo;
723 struct dwarf2_section_info macro;
724 struct dwarf2_section_info str_offsets;
725 struct dwarf2_section_info types;
80626a55
DE
726};
727
73869dc2
DE
728/* These sections are what may appear in a virtual DWO file in DWP version 1.
729 A virtual DWO file is a DWO file as it appears in a DWP file. */
80626a55 730
73869dc2 731struct virtual_v1_dwo_sections
80626a55
DE
732{
733 struct dwarf2_section_info abbrev;
734 struct dwarf2_section_info line;
735 struct dwarf2_section_info loc;
736 struct dwarf2_section_info macinfo;
737 struct dwarf2_section_info macro;
738 struct dwarf2_section_info str_offsets;
739 /* Each DWP hash table entry records one CU or one TU.
8a0459fd 740 That is recorded here, and copied to dwo_unit.section. */
80626a55
DE
741 struct dwarf2_section_info info_or_types;
742};
743
73869dc2
DE
744/* Similar to virtual_v1_dwo_sections, but for DWP version 2.
745 In version 2, the sections of the DWO files are concatenated together
746 and stored in one section of that name. Thus each ELF section contains
747 several "virtual" sections. */
748
749struct virtual_v2_dwo_sections
750{
751 bfd_size_type abbrev_offset;
752 bfd_size_type abbrev_size;
753
754 bfd_size_type line_offset;
755 bfd_size_type line_size;
756
757 bfd_size_type loc_offset;
758 bfd_size_type loc_size;
759
760 bfd_size_type macinfo_offset;
761 bfd_size_type macinfo_size;
762
763 bfd_size_type macro_offset;
764 bfd_size_type macro_size;
765
766 bfd_size_type str_offsets_offset;
767 bfd_size_type str_offsets_size;
768
769 /* Each DWP hash table entry records one CU or one TU.
770 That is recorded here, and copied to dwo_unit.section. */
771 bfd_size_type info_or_types_offset;
772 bfd_size_type info_or_types_size;
773};
774
80626a55
DE
775/* Contents of DWP hash tables. */
776
777struct dwp_hash_table
778{
73869dc2 779 uint32_t version, nr_columns;
80626a55 780 uint32_t nr_units, nr_slots;
73869dc2
DE
781 const gdb_byte *hash_table, *unit_table;
782 union
783 {
784 struct
785 {
786 const gdb_byte *indices;
787 } v1;
788 struct
789 {
790 /* This is indexed by column number and gives the id of the section
791 in that column. */
792#define MAX_NR_V2_DWO_SECTIONS \
793 (1 /* .debug_info or .debug_types */ \
794 + 1 /* .debug_abbrev */ \
795 + 1 /* .debug_line */ \
796 + 1 /* .debug_loc */ \
797 + 1 /* .debug_str_offsets */ \
798 + 1 /* .debug_macro or .debug_macinfo */)
799 int section_ids[MAX_NR_V2_DWO_SECTIONS];
800 const gdb_byte *offsets;
801 const gdb_byte *sizes;
802 } v2;
803 } section_pool;
80626a55
DE
804};
805
806/* Data for one DWP file. */
807
808struct dwp_file
809{
810 /* Name of the file. */
811 const char *name;
812
73869dc2
DE
813 /* File format version. */
814 int version;
815
93417882 816 /* The bfd. */
80626a55
DE
817 bfd *dbfd;
818
819 /* Section info for this file. */
820 struct dwp_sections sections;
821
57d63ce2 822 /* Table of CUs in the file. */
80626a55
DE
823 const struct dwp_hash_table *cus;
824
825 /* Table of TUs in the file. */
826 const struct dwp_hash_table *tus;
827
19ac8c2e
DE
828 /* Tables of loaded CUs/TUs. Each entry is a struct dwo_unit *. */
829 htab_t loaded_cus;
830 htab_t loaded_tus;
80626a55 831
73869dc2
DE
832 /* Table to map ELF section numbers to their sections.
833 This is only needed for the DWP V1 file format. */
80626a55
DE
834 unsigned int num_sections;
835 asection **elf_sections;
836};
837
36586728
TT
838/* This represents a '.dwz' file. */
839
840struct dwz_file
841{
842 /* A dwz file can only contain a few sections. */
843 struct dwarf2_section_info abbrev;
844 struct dwarf2_section_info info;
845 struct dwarf2_section_info str;
846 struct dwarf2_section_info line;
847 struct dwarf2_section_info macro;
2ec9a5e0 848 struct dwarf2_section_info gdb_index;
927aa2e7 849 struct dwarf2_section_info debug_names;
36586728
TT
850
851 /* The dwz's BFD. */
852 bfd *dwz_bfd;
853};
854
0963b4bd
MS
855/* Struct used to pass misc. parameters to read_die_and_children, et
856 al. which are used for both .debug_info and .debug_types dies.
857 All parameters here are unchanging for the life of the call. This
dee91e82 858 struct exists to abstract away the constant parameters of die reading. */
93311388
DE
859
860struct die_reader_specs
861{
a32a8923 862 /* The bfd of die_section. */
93311388
DE
863 bfd* abfd;
864
865 /* The CU of the DIE we are parsing. */
866 struct dwarf2_cu *cu;
867
80626a55 868 /* Non-NULL if reading a DWO file (including one packaged into a DWP). */
3019eac3
DE
869 struct dwo_file *dwo_file;
870
dee91e82 871 /* The section the die comes from.
3019eac3 872 This is either .debug_info or .debug_types, or the .dwo variants. */
dee91e82
DE
873 struct dwarf2_section_info *die_section;
874
875 /* die_section->buffer. */
d521ce57 876 const gdb_byte *buffer;
f664829e
DE
877
878 /* The end of the buffer. */
879 const gdb_byte *buffer_end;
a2ce51a0
DE
880
881 /* The value of the DW_AT_comp_dir attribute. */
882 const char *comp_dir;
685af9cd
TT
883
884 /* The abbreviation table to use when reading the DIEs. */
885 struct abbrev_table *abbrev_table;
93311388
DE
886};
887
fd820528 888/* Type of function passed to init_cutu_and_read_dies, et.al. */
dee91e82 889typedef void (die_reader_func_ftype) (const struct die_reader_specs *reader,
d521ce57 890 const gdb_byte *info_ptr,
dee91e82
DE
891 struct die_info *comp_unit_die,
892 int has_children,
893 void *data);
894
ecfb656c
PA
895/* A 1-based directory index. This is a strong typedef to prevent
896 accidentally using a directory index as a 0-based index into an
897 array/vector. */
898enum class dir_index : unsigned int {};
899
900/* Likewise, a 1-based file name index. */
901enum class file_name_index : unsigned int {};
902
52059ffd
TT
903struct file_entry
904{
fff8551c
PA
905 file_entry () = default;
906
ecfb656c 907 file_entry (const char *name_, dir_index d_index_,
fff8551c
PA
908 unsigned int mod_time_, unsigned int length_)
909 : name (name_),
ecfb656c 910 d_index (d_index_),
fff8551c
PA
911 mod_time (mod_time_),
912 length (length_)
913 {}
914
ecfb656c
PA
915 /* Return the include directory at D_INDEX stored in LH. Returns
916 NULL if D_INDEX is out of bounds. */
8c43009f
PA
917 const char *include_dir (const line_header *lh) const;
918
fff8551c
PA
919 /* The file name. Note this is an observing pointer. The memory is
920 owned by debug_line_buffer. */
921 const char *name {};
922
8c43009f 923 /* The directory index (1-based). */
ecfb656c 924 dir_index d_index {};
fff8551c
PA
925
926 unsigned int mod_time {};
927
928 unsigned int length {};
929
930 /* True if referenced by the Line Number Program. */
931 bool included_p {};
932
83769d0b 933 /* The associated symbol table, if any. */
fff8551c 934 struct symtab *symtab {};
52059ffd
TT
935};
936
debd256d
JB
937/* The line number information for a compilation unit (found in the
938 .debug_line section) begins with a "statement program header",
939 which contains the following information. */
940struct line_header
941{
fff8551c
PA
942 line_header ()
943 : offset_in_dwz {}
944 {}
945
946 /* Add an entry to the include directory table. */
947 void add_include_dir (const char *include_dir);
948
949 /* Add an entry to the file name table. */
ecfb656c 950 void add_file_name (const char *name, dir_index d_index,
fff8551c
PA
951 unsigned int mod_time, unsigned int length);
952
ecfb656c 953 /* Return the include dir at INDEX (1-based). Returns NULL if INDEX
8c43009f 954 is out of bounds. */
ecfb656c 955 const char *include_dir_at (dir_index index) const
8c43009f 956 {
ecfb656c
PA
957 /* Convert directory index number (1-based) to vector index
958 (0-based). */
959 size_t vec_index = to_underlying (index) - 1;
960
961 if (vec_index >= include_dirs.size ())
8c43009f 962 return NULL;
ecfb656c 963 return include_dirs[vec_index];
8c43009f
PA
964 }
965
ecfb656c 966 /* Return the file name at INDEX (1-based). Returns NULL if INDEX
8c43009f 967 is out of bounds. */
ecfb656c 968 file_entry *file_name_at (file_name_index index)
8c43009f 969 {
ecfb656c
PA
970 /* Convert file name index number (1-based) to vector index
971 (0-based). */
972 size_t vec_index = to_underlying (index) - 1;
973
974 if (vec_index >= file_names.size ())
fff8551c 975 return NULL;
ecfb656c 976 return &file_names[vec_index];
fff8551c
PA
977 }
978
979 /* Const version of the above. */
980 const file_entry *file_name_at (unsigned int index) const
981 {
982 if (index >= file_names.size ())
8c43009f
PA
983 return NULL;
984 return &file_names[index];
985 }
986
527f3840 987 /* Offset of line number information in .debug_line section. */
9c541725 988 sect_offset sect_off {};
527f3840
JK
989
990 /* OFFSET is for struct dwz_file associated with dwarf2_per_objfile. */
fff8551c
PA
991 unsigned offset_in_dwz : 1; /* Can't initialize bitfields in-class. */
992
993 unsigned int total_length {};
994 unsigned short version {};
995 unsigned int header_length {};
996 unsigned char minimum_instruction_length {};
997 unsigned char maximum_ops_per_instruction {};
998 unsigned char default_is_stmt {};
999 int line_base {};
1000 unsigned char line_range {};
1001 unsigned char opcode_base {};
debd256d
JB
1002
1003 /* standard_opcode_lengths[i] is the number of operands for the
1004 standard opcode whose value is i. This means that
1005 standard_opcode_lengths[0] is unused, and the last meaningful
1006 element is standard_opcode_lengths[opcode_base - 1]. */
fff8551c 1007 std::unique_ptr<unsigned char[]> standard_opcode_lengths;
debd256d 1008
fff8551c
PA
1009 /* The include_directories table. Note these are observing
1010 pointers. The memory is owned by debug_line_buffer. */
1011 std::vector<const char *> include_dirs;
debd256d 1012
fff8551c
PA
1013 /* The file_names table. */
1014 std::vector<file_entry> file_names;
debd256d
JB
1015
1016 /* The start and end of the statement program following this
6502dd73 1017 header. These point into dwarf2_per_objfile->line_buffer. */
fff8551c 1018 const gdb_byte *statement_program_start {}, *statement_program_end {};
debd256d 1019};
c906108c 1020
fff8551c
PA
1021typedef std::unique_ptr<line_header> line_header_up;
1022
8c43009f
PA
1023const char *
1024file_entry::include_dir (const line_header *lh) const
1025{
ecfb656c 1026 return lh->include_dir_at (d_index);
8c43009f
PA
1027}
1028
c906108c 1029/* When we construct a partial symbol table entry we only
0963b4bd 1030 need this much information. */
6f06d47b 1031struct partial_die_info : public allocate_on_obstack
c906108c 1032 {
6f06d47b
YQ
1033 partial_die_info (sect_offset sect_off, struct abbrev_info *abbrev);
1034
1035 /* Disable assign but still keep copy ctor, which is needed
1036 load_partial_dies. */
1037 partial_die_info& operator=(const partial_die_info& rhs) = delete;
1038
52356b79
YQ
1039 /* Adjust the partial die before generating a symbol for it. This
1040 function may set the is_external flag or change the DIE's
1041 name. */
1042 void fixup (struct dwarf2_cu *cu);
1043
48fbe735
YQ
1044 /* Read a minimal amount of information into the minimal die
1045 structure. */
1046 const gdb_byte *read (const struct die_reader_specs *reader,
1047 const struct abbrev_info &abbrev,
1048 const gdb_byte *info_ptr);
1049
72bf9492 1050 /* Offset of this DIE. */
6f06d47b 1051 const sect_offset sect_off;
72bf9492
DJ
1052
1053 /* DWARF-2 tag for this DIE. */
6f06d47b 1054 const ENUM_BITFIELD(dwarf_tag) tag : 16;
72bf9492 1055
72bf9492 1056 /* Assorted flags describing the data found in this DIE. */
6f06d47b
YQ
1057 const unsigned int has_children : 1;
1058
72bf9492
DJ
1059 unsigned int is_external : 1;
1060 unsigned int is_declaration : 1;
1061 unsigned int has_type : 1;
1062 unsigned int has_specification : 1;
1063 unsigned int has_pc_info : 1;
481860b3 1064 unsigned int may_be_inlined : 1;
72bf9492 1065
0c1b455e
TT
1066 /* This DIE has been marked DW_AT_main_subprogram. */
1067 unsigned int main_subprogram : 1;
1068
72bf9492
DJ
1069 /* Flag set if the SCOPE field of this structure has been
1070 computed. */
1071 unsigned int scope_set : 1;
1072
fa4028e9
JB
1073 /* Flag set if the DIE has a byte_size attribute. */
1074 unsigned int has_byte_size : 1;
1075
ff908ebf
AW
1076 /* Flag set if the DIE has a DW_AT_const_value attribute. */
1077 unsigned int has_const_value : 1;
1078
98bfdba5
PA
1079 /* Flag set if any of the DIE's children are template arguments. */
1080 unsigned int has_template_arguments : 1;
1081
52356b79 1082 /* Flag set if fixup has been called on this die. */
abc72ce4
DE
1083 unsigned int fixup_called : 1;
1084
36586728
TT
1085 /* Flag set if DW_TAG_imported_unit uses DW_FORM_GNU_ref_alt. */
1086 unsigned int is_dwz : 1;
1087
1088 /* Flag set if spec_offset uses DW_FORM_GNU_ref_alt. */
1089 unsigned int spec_is_dwz : 1;
1090
72bf9492 1091 /* The name of this DIE. Normally the value of DW_AT_name, but
94af9270 1092 sometimes a default name for unnamed DIEs. */
6f06d47b 1093 const char *name = nullptr;
72bf9492 1094
abc72ce4 1095 /* The linkage name, if present. */
6f06d47b 1096 const char *linkage_name = nullptr;
abc72ce4 1097
72bf9492
DJ
1098 /* The scope to prepend to our children. This is generally
1099 allocated on the comp_unit_obstack, so will disappear
1100 when this compilation unit leaves the cache. */
6f06d47b 1101 const char *scope = nullptr;
72bf9492 1102
95554aad
TT
1103 /* Some data associated with the partial DIE. The tag determines
1104 which field is live. */
1105 union
1106 {
1107 /* The location description associated with this DIE, if any. */
1108 struct dwarf_block *locdesc;
1109 /* The offset of an import, for DW_TAG_imported_unit. */
9c541725 1110 sect_offset sect_off;
6f06d47b 1111 } d {};
72bf9492
DJ
1112
1113 /* If HAS_PC_INFO, the PC range associated with this DIE. */
6f06d47b
YQ
1114 CORE_ADDR lowpc = 0;
1115 CORE_ADDR highpc = 0;
72bf9492 1116
93311388 1117 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
72bf9492 1118 DW_AT_sibling, if any. */
48fbe735
YQ
1119 /* NOTE: This member isn't strictly necessary, partial_die_info::read
1120 could return DW_AT_sibling values to its caller load_partial_dies. */
6f06d47b 1121 const gdb_byte *sibling = nullptr;
72bf9492
DJ
1122
1123 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
1124 DW_AT_specification (or DW_AT_abstract_origin or
1125 DW_AT_extension). */
6f06d47b 1126 sect_offset spec_offset {};
72bf9492
DJ
1127
1128 /* Pointers to this DIE's parent, first child, and next sibling,
1129 if any. */
6f06d47b
YQ
1130 struct partial_die_info *die_parent = nullptr;
1131 struct partial_die_info *die_child = nullptr;
1132 struct partial_die_info *die_sibling = nullptr;
1133
1134 friend struct partial_die_info *
1135 dwarf2_cu::find_partial_die (sect_offset sect_off);
1136
1137 private:
1138 /* Only need to do look up in dwarf2_cu::find_partial_die. */
1139 partial_die_info (sect_offset sect_off)
1140 : partial_die_info (sect_off, DW_TAG_padding, 0)
1141 {
1142 }
1143
1144 partial_die_info (sect_offset sect_off_, enum dwarf_tag tag_,
1145 int has_children_)
1146 : sect_off (sect_off_), tag (tag_), has_children (has_children_)
1147 {
1148 is_external = 0;
1149 is_declaration = 0;
1150 has_type = 0;
1151 has_specification = 0;
1152 has_pc_info = 0;
1153 may_be_inlined = 0;
1154 main_subprogram = 0;
1155 scope_set = 0;
1156 has_byte_size = 0;
1157 has_const_value = 0;
1158 has_template_arguments = 0;
1159 fixup_called = 0;
1160 is_dwz = 0;
1161 spec_is_dwz = 0;
1162 }
c906108c
SS
1163 };
1164
0963b4bd 1165/* This data structure holds the information of an abbrev. */
c906108c
SS
1166struct abbrev_info
1167 {
1168 unsigned int number; /* number identifying abbrev */
1169 enum dwarf_tag tag; /* dwarf tag */
f3dd6933
DJ
1170 unsigned short has_children; /* boolean */
1171 unsigned short num_attrs; /* number of attributes */
c906108c
SS
1172 struct attr_abbrev *attrs; /* an array of attribute descriptions */
1173 struct abbrev_info *next; /* next in chain */
1174 };
1175
1176struct attr_abbrev
1177 {
9d25dd43
DE
1178 ENUM_BITFIELD(dwarf_attribute) name : 16;
1179 ENUM_BITFIELD(dwarf_form) form : 16;
43988095
JK
1180
1181 /* It is valid only if FORM is DW_FORM_implicit_const. */
1182 LONGEST implicit_const;
c906108c
SS
1183 };
1184
433df2d4
DE
1185/* Size of abbrev_table.abbrev_hash_table. */
1186#define ABBREV_HASH_SIZE 121
1187
1188/* Top level data structure to contain an abbreviation table. */
1189
1190struct abbrev_table
1191{
685af9cd
TT
1192 explicit abbrev_table (sect_offset off)
1193 : sect_off (off)
1194 {
4a17f768 1195 m_abbrevs =
685af9cd 1196 XOBNEWVEC (&abbrev_obstack, struct abbrev_info *, ABBREV_HASH_SIZE);
4a17f768 1197 memset (m_abbrevs, 0, ABBREV_HASH_SIZE * sizeof (struct abbrev_info *));
685af9cd
TT
1198 }
1199
1200 DISABLE_COPY_AND_ASSIGN (abbrev_table);
1201
1202 /* Allocate space for a struct abbrev_info object in
1203 ABBREV_TABLE. */
1204 struct abbrev_info *alloc_abbrev ();
1205
1206 /* Add an abbreviation to the table. */
1207 void add_abbrev (unsigned int abbrev_number, struct abbrev_info *abbrev);
1208
1209 /* Look up an abbrev in the table.
1210 Returns NULL if the abbrev is not found. */
1211
1212 struct abbrev_info *lookup_abbrev (unsigned int abbrev_number);
1213
1214
f4dc4d17
DE
1215 /* Where the abbrev table came from.
1216 This is used as a sanity check when the table is used. */
685af9cd 1217 const sect_offset sect_off;
433df2d4
DE
1218
1219 /* Storage for the abbrev table. */
685af9cd 1220 auto_obstack abbrev_obstack;
433df2d4 1221
4a17f768
YQ
1222private:
1223
433df2d4
DE
1224 /* Hash table of abbrevs.
1225 This is an array of size ABBREV_HASH_SIZE allocated in abbrev_obstack.
1226 It could be statically allocated, but the previous code didn't so we
1227 don't either. */
4a17f768 1228 struct abbrev_info **m_abbrevs;
433df2d4
DE
1229};
1230
685af9cd
TT
1231typedef std::unique_ptr<struct abbrev_table> abbrev_table_up;
1232
0963b4bd 1233/* Attributes have a name and a value. */
b60c80d6
DJ
1234struct attribute
1235 {
9d25dd43 1236 ENUM_BITFIELD(dwarf_attribute) name : 16;
8285870a
JK
1237 ENUM_BITFIELD(dwarf_form) form : 15;
1238
1239 /* Has DW_STRING already been updated by dwarf2_canonicalize_name? This
1240 field should be in u.str (existing only for DW_STRING) but it is kept
1241 here for better struct attribute alignment. */
1242 unsigned int string_is_canonical : 1;
1243
b60c80d6
DJ
1244 union
1245 {
15d034d0 1246 const char *str;
b60c80d6 1247 struct dwarf_block *blk;
43bbcdc2
PH
1248 ULONGEST unsnd;
1249 LONGEST snd;
b60c80d6 1250 CORE_ADDR addr;
ac9ec31b 1251 ULONGEST signature;
b60c80d6
DJ
1252 }
1253 u;
1254 };
1255
0963b4bd 1256/* This data structure holds a complete die structure. */
c906108c
SS
1257struct die_info
1258 {
76815b17
DE
1259 /* DWARF-2 tag for this DIE. */
1260 ENUM_BITFIELD(dwarf_tag) tag : 16;
1261
1262 /* Number of attributes */
98bfdba5
PA
1263 unsigned char num_attrs;
1264
1265 /* True if we're presently building the full type name for the
1266 type derived from this DIE. */
1267 unsigned char building_fullname : 1;
76815b17 1268
adde2bff
DE
1269 /* True if this die is in process. PR 16581. */
1270 unsigned char in_process : 1;
1271
76815b17
DE
1272 /* Abbrev number */
1273 unsigned int abbrev;
1274
93311388 1275 /* Offset in .debug_info or .debug_types section. */
9c541725 1276 sect_offset sect_off;
78ba4af6
JB
1277
1278 /* The dies in a compilation unit form an n-ary tree. PARENT
1279 points to this die's parent; CHILD points to the first child of
1280 this node; and all the children of a given node are chained
4950bc1c 1281 together via their SIBLING fields. */
639d11d3
DC
1282 struct die_info *child; /* Its first child, if any. */
1283 struct die_info *sibling; /* Its next sibling, if any. */
1284 struct die_info *parent; /* Its parent, if any. */
c906108c 1285
b60c80d6
DJ
1286 /* An array of attributes, with NUM_ATTRS elements. There may be
1287 zero, but it's not common and zero-sized arrays are not
1288 sufficiently portable C. */
1289 struct attribute attrs[1];
c906108c
SS
1290 };
1291
0963b4bd 1292/* Get at parts of an attribute structure. */
c906108c
SS
1293
1294#define DW_STRING(attr) ((attr)->u.str)
8285870a 1295#define DW_STRING_IS_CANONICAL(attr) ((attr)->string_is_canonical)
c906108c
SS
1296#define DW_UNSND(attr) ((attr)->u.unsnd)
1297#define DW_BLOCK(attr) ((attr)->u.blk)
1298#define DW_SND(attr) ((attr)->u.snd)
1299#define DW_ADDR(attr) ((attr)->u.addr)
ac9ec31b 1300#define DW_SIGNATURE(attr) ((attr)->u.signature)
c906108c 1301
0963b4bd 1302/* Blocks are a bunch of untyped bytes. */
c906108c
SS
1303struct dwarf_block
1304 {
56eb65bd 1305 size_t size;
1d6edc3c
JK
1306
1307 /* Valid only if SIZE is not zero. */
d521ce57 1308 const gdb_byte *data;
c906108c
SS
1309 };
1310
c906108c
SS
1311#ifndef ATTR_ALLOC_CHUNK
1312#define ATTR_ALLOC_CHUNK 4
1313#endif
1314
c906108c
SS
1315/* Allocate fields for structs, unions and enums in this size. */
1316#ifndef DW_FIELD_ALLOC_CHUNK
1317#define DW_FIELD_ALLOC_CHUNK 4
1318#endif
1319
c906108c
SS
1320/* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
1321 but this would require a corresponding change in unpack_field_as_long
1322 and friends. */
1323static int bits_per_byte = 8;
1324
2ddeaf8a
TT
1325/* When reading a variant or variant part, we track a bit more
1326 information about the field, and store it in an object of this
1327 type. */
1328
1329struct variant_field
1330{
1331 /* If we see a DW_TAG_variant, then this will be the discriminant
1332 value. */
1333 ULONGEST discriminant_value;
1334 /* If we see a DW_TAG_variant, then this will be set if this is the
1335 default branch. */
1336 bool default_branch;
1337 /* While reading a DW_TAG_variant_part, this will be set if this
1338 field is the discriminant. */
1339 bool is_discriminant;
1340};
1341
52059ffd
TT
1342struct nextfield
1343{
be2daae6
TT
1344 int accessibility = 0;
1345 int virtuality = 0;
2ddeaf8a 1346 /* Extra information to describe a variant or variant part. */
be2daae6
TT
1347 struct variant_field variant {};
1348 struct field field {};
52059ffd
TT
1349};
1350
1351struct fnfieldlist
1352{
be2daae6
TT
1353 const char *name = nullptr;
1354 std::vector<struct fn_field> fnfields;
52059ffd
TT
1355};
1356
c906108c
SS
1357/* The routines that read and process dies for a C struct or C++ class
1358 pass lists of data member fields and lists of member function fields
1359 in an instance of a field_info structure, as defined below. */
1360struct field_info
c5aa993b 1361 {
0963b4bd 1362 /* List of data member and baseclasses fields. */
be2daae6
TT
1363 std::vector<struct nextfield> fields;
1364 std::vector<struct nextfield> baseclasses;
c906108c 1365
7d0ccb61 1366 /* Number of fields (including baseclasses). */
be2daae6 1367 int nfields = 0;
c906108c 1368
c5aa993b 1369 /* Set if the accesibility of one of the fields is not public. */
be2daae6 1370 int non_public_fields = 0;
c906108c 1371
c5aa993b
JM
1372 /* Member function fieldlist array, contains name of possibly overloaded
1373 member function, number of overloaded member functions and a pointer
1374 to the head of the member function field chain. */
be2daae6 1375 std::vector<struct fnfieldlist> fnfieldlists;
98751a41
JK
1376
1377 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
1378 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
be2daae6 1379 std::vector<struct decl_field> typedef_field_list;
883fd55a
KS
1380
1381 /* Nested types defined by this class and the number of elements in this
1382 list. */
be2daae6 1383 std::vector<struct decl_field> nested_types_list;
c5aa993b 1384 };
c906108c 1385
10b3939b
DJ
1386/* One item on the queue of compilation units to read in full symbols
1387 for. */
1388struct dwarf2_queue_item
1389{
1390 struct dwarf2_per_cu_data *per_cu;
95554aad 1391 enum language pretend_language;
10b3939b
DJ
1392 struct dwarf2_queue_item *next;
1393};
1394
1395/* The current queue. */
1396static struct dwarf2_queue_item *dwarf2_queue, *dwarf2_queue_tail;
1397
ae038cb0
DJ
1398/* Loaded secondary compilation units are kept in memory until they
1399 have not been referenced for the processing of this many
1400 compilation units. Set this to zero to disable caching. Cache
1401 sizes of up to at least twenty will improve startup time for
1402 typical inter-CU-reference binaries, at an obvious memory cost. */
b4f54984 1403static int dwarf_max_cache_age = 5;
920d2a44 1404static void
b4f54984
DE
1405show_dwarf_max_cache_age (struct ui_file *file, int from_tty,
1406 struct cmd_list_element *c, const char *value)
920d2a44 1407{
3e43a32a 1408 fprintf_filtered (file, _("The upper bound on the age of cached "
b4f54984 1409 "DWARF compilation units is %s.\n"),
920d2a44
AC
1410 value);
1411}
4390d890 1412\f
c906108c
SS
1413/* local function prototypes */
1414
a32a8923
DE
1415static const char *get_section_name (const struct dwarf2_section_info *);
1416
1417static const char *get_section_file_name (const struct dwarf2_section_info *);
1418
918dd910
JK
1419static void dwarf2_find_base_address (struct die_info *die,
1420 struct dwarf2_cu *cu);
1421
0018ea6f
DE
1422static struct partial_symtab *create_partial_symtab
1423 (struct dwarf2_per_cu_data *per_cu, const char *name);
1424
f1902523
JK
1425static void build_type_psymtabs_reader (const struct die_reader_specs *reader,
1426 const gdb_byte *info_ptr,
1427 struct die_info *type_unit_die,
1428 int has_children, void *data);
1429
ed2dc618
SM
1430static void dwarf2_build_psymtabs_hard
1431 (struct dwarf2_per_objfile *dwarf2_per_objfile);
c906108c 1432
72bf9492
DJ
1433static void scan_partial_symbols (struct partial_die_info *,
1434 CORE_ADDR *, CORE_ADDR *,
5734ee8b 1435 int, struct dwarf2_cu *);
c906108c 1436
72bf9492
DJ
1437static void add_partial_symbol (struct partial_die_info *,
1438 struct dwarf2_cu *);
63d06c5c 1439
72bf9492
DJ
1440static void add_partial_namespace (struct partial_die_info *pdi,
1441 CORE_ADDR *lowpc, CORE_ADDR *highpc,
cdc07690 1442 int set_addrmap, struct dwarf2_cu *cu);
63d06c5c 1443
5d7cb8df 1444static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
cdc07690 1445 CORE_ADDR *highpc, int set_addrmap,
5d7cb8df
JK
1446 struct dwarf2_cu *cu);
1447
72bf9492
DJ
1448static void add_partial_enumeration (struct partial_die_info *enum_pdi,
1449 struct dwarf2_cu *cu);
91c24f0a 1450
bc30ff58
JB
1451static void add_partial_subprogram (struct partial_die_info *pdi,
1452 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 1453 int need_pc, struct dwarf2_cu *cu);
bc30ff58 1454
257e7a09
YQ
1455static void dwarf2_read_symtab (struct partial_symtab *,
1456 struct objfile *);
c906108c 1457
a14ed312 1458static void psymtab_to_symtab_1 (struct partial_symtab *);
c906108c 1459
685af9cd 1460static abbrev_table_up abbrev_table_read_table
ed2dc618
SM
1461 (struct dwarf2_per_objfile *dwarf2_per_objfile, struct dwarf2_section_info *,
1462 sect_offset);
433df2d4 1463
d521ce57 1464static unsigned int peek_abbrev_code (bfd *, const gdb_byte *);
6caca83c 1465
dee91e82 1466static struct partial_die_info *load_partial_dies
d521ce57 1467 (const struct die_reader_specs *, const gdb_byte *, int);
72bf9492 1468
36586728 1469static struct partial_die_info *find_partial_die (sect_offset, int,
10b3939b 1470 struct dwarf2_cu *);
72bf9492 1471
d521ce57
TT
1472static const gdb_byte *read_attribute (const struct die_reader_specs *,
1473 struct attribute *, struct attr_abbrev *,
1474 const gdb_byte *);
a8329558 1475
a1855c1d 1476static unsigned int read_1_byte (bfd *, const gdb_byte *);
c906108c 1477
a1855c1d 1478static int read_1_signed_byte (bfd *, const gdb_byte *);
c906108c 1479
a1855c1d 1480static unsigned int read_2_bytes (bfd *, const gdb_byte *);
c906108c 1481
a1855c1d 1482static unsigned int read_4_bytes (bfd *, const gdb_byte *);
c906108c 1483
a1855c1d 1484static ULONGEST read_8_bytes (bfd *, const gdb_byte *);
c906108c 1485
d521ce57 1486static CORE_ADDR read_address (bfd *, const gdb_byte *ptr, struct dwarf2_cu *,
891d2f0b 1487 unsigned int *);
c906108c 1488
d521ce57 1489static LONGEST read_initial_length (bfd *, const gdb_byte *, unsigned int *);
c764a876
DE
1490
1491static LONGEST read_checked_initial_length_and_offset
d521ce57 1492 (bfd *, const gdb_byte *, const struct comp_unit_head *,
c764a876 1493 unsigned int *, unsigned int *);
613e1657 1494
d521ce57
TT
1495static LONGEST read_offset (bfd *, const gdb_byte *,
1496 const struct comp_unit_head *,
c764a876
DE
1497 unsigned int *);
1498
d521ce57 1499static LONGEST read_offset_1 (bfd *, const gdb_byte *, unsigned int);
613e1657 1500
ed2dc618
SM
1501static sect_offset read_abbrev_offset
1502 (struct dwarf2_per_objfile *dwarf2_per_objfile,
1503 struct dwarf2_section_info *, sect_offset);
f4dc4d17 1504
d521ce57 1505static const gdb_byte *read_n_bytes (bfd *, const gdb_byte *, unsigned int);
c906108c 1506
d521ce57 1507static const char *read_direct_string (bfd *, const gdb_byte *, unsigned int *);
c906108c 1508
ed2dc618
SM
1509static const char *read_indirect_string
1510 (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *, const gdb_byte *,
1511 const struct comp_unit_head *, unsigned int *);
4bdf3d34 1512
ed2dc618
SM
1513static const char *read_indirect_line_string
1514 (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *, const gdb_byte *,
1515 const struct comp_unit_head *, unsigned int *);
36586728 1516
ed2dc618
SM
1517static const char *read_indirect_string_at_offset
1518 (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *abfd,
1519 LONGEST str_offset);
927aa2e7 1520
ed2dc618
SM
1521static const char *read_indirect_string_from_dwz
1522 (struct objfile *objfile, struct dwz_file *, LONGEST);
c906108c 1523
d521ce57 1524static LONGEST read_signed_leb128 (bfd *, const gdb_byte *, unsigned int *);
c906108c 1525
d521ce57
TT
1526static CORE_ADDR read_addr_index_from_leb128 (struct dwarf2_cu *,
1527 const gdb_byte *,
3019eac3
DE
1528 unsigned int *);
1529
d521ce57 1530static const char *read_str_index (const struct die_reader_specs *reader,
342587c4 1531 ULONGEST str_index);
3019eac3 1532
e142c38c 1533static void set_cu_language (unsigned int, struct dwarf2_cu *);
c906108c 1534
e142c38c
DJ
1535static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
1536 struct dwarf2_cu *);
c906108c 1537
348e048f 1538static struct attribute *dwarf2_attr_no_follow (struct die_info *,
45e58e77 1539 unsigned int);
348e048f 1540
7d45c7c3
KB
1541static const char *dwarf2_string_attr (struct die_info *die, unsigned int name,
1542 struct dwarf2_cu *cu);
1543
05cf31d1
JB
1544static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
1545 struct dwarf2_cu *cu);
1546
e142c38c 1547static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
3ca72b44 1548
e142c38c 1549static struct die_info *die_specification (struct die_info *die,
f2f0e013 1550 struct dwarf2_cu **);
63d06c5c 1551
9c541725 1552static line_header_up dwarf_decode_line_header (sect_offset sect_off,
fff8551c 1553 struct dwarf2_cu *cu);
debd256d 1554
f3f5162e 1555static void dwarf_decode_lines (struct line_header *, const char *,
c3b7b696 1556 struct dwarf2_cu *, struct partial_symtab *,
527f3840 1557 CORE_ADDR, int decode_mapping);
c906108c 1558
4d663531 1559static void dwarf2_start_subfile (const char *, const char *);
c906108c 1560
43f3e411
DE
1561static struct compunit_symtab *dwarf2_start_symtab (struct dwarf2_cu *,
1562 const char *, const char *,
1563 CORE_ADDR);
f4dc4d17 1564
a14ed312 1565static struct symbol *new_symbol (struct die_info *, struct type *,
5e2db402 1566 struct dwarf2_cu *, struct symbol * = NULL);
34eaf542 1567
ff39bb5e 1568static void dwarf2_const_value (const struct attribute *, struct symbol *,
e7c27a73 1569 struct dwarf2_cu *);
c906108c 1570
ff39bb5e 1571static void dwarf2_const_value_attr (const struct attribute *attr,
98bfdba5
PA
1572 struct type *type,
1573 const char *name,
1574 struct obstack *obstack,
12df843f 1575 struct dwarf2_cu *cu, LONGEST *value,
d521ce57 1576 const gdb_byte **bytes,
98bfdba5 1577 struct dwarf2_locexpr_baton **baton);
2df3850c 1578
e7c27a73 1579static struct type *die_type (struct die_info *, struct dwarf2_cu *);
c906108c 1580
b4ba55a1
JB
1581static int need_gnat_info (struct dwarf2_cu *);
1582
3e43a32a
MS
1583static struct type *die_descriptive_type (struct die_info *,
1584 struct dwarf2_cu *);
b4ba55a1
JB
1585
1586static void set_descriptive_type (struct type *, struct die_info *,
1587 struct dwarf2_cu *);
1588
e7c27a73
DJ
1589static struct type *die_containing_type (struct die_info *,
1590 struct dwarf2_cu *);
c906108c 1591
ff39bb5e 1592static struct type *lookup_die_type (struct die_info *, const struct attribute *,
673bfd45 1593 struct dwarf2_cu *);
c906108c 1594
f792889a 1595static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
c906108c 1596
673bfd45
DE
1597static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1598
0d5cff50 1599static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
63d06c5c 1600
6e70227d 1601static char *typename_concat (struct obstack *obs, const char *prefix,
f55ee35c
JK
1602 const char *suffix, int physname,
1603 struct dwarf2_cu *cu);
63d06c5c 1604
e7c27a73 1605static void read_file_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1606
348e048f
DE
1607static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1608
e7c27a73 1609static void read_func_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1610
e7c27a73 1611static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1612
96408a79
SA
1613static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1614
71a3c369
TT
1615static void read_variable (struct die_info *die, struct dwarf2_cu *cu);
1616
ff013f42
JK
1617static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
1618 struct dwarf2_cu *, struct partial_symtab *);
1619
3a2b436a 1620/* How dwarf2_get_pc_bounds constructed its *LOWPC and *HIGHPC return
e385593e 1621 values. Keep the items ordered with increasing constraints compliance. */
3a2b436a
JK
1622enum pc_bounds_kind
1623{
e385593e 1624 /* No attribute DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges was found. */
3a2b436a
JK
1625 PC_BOUNDS_NOT_PRESENT,
1626
e385593e
JK
1627 /* Some of the attributes DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges
1628 were present but they do not form a valid range of PC addresses. */
1629 PC_BOUNDS_INVALID,
1630
3a2b436a
JK
1631 /* Discontiguous range was found - that is DW_AT_ranges was found. */
1632 PC_BOUNDS_RANGES,
1633
1634 /* Contiguous range was found - DW_AT_low_pc and DW_AT_high_pc were found. */
1635 PC_BOUNDS_HIGH_LOW,
1636};
1637
1638static enum pc_bounds_kind dwarf2_get_pc_bounds (struct die_info *,
1639 CORE_ADDR *, CORE_ADDR *,
1640 struct dwarf2_cu *,
1641 struct partial_symtab *);
c906108c 1642
fae299cd
DC
1643static void get_scope_pc_bounds (struct die_info *,
1644 CORE_ADDR *, CORE_ADDR *,
1645 struct dwarf2_cu *);
1646
801e3a5b
JB
1647static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1648 CORE_ADDR, struct dwarf2_cu *);
1649
a14ed312 1650static void dwarf2_add_field (struct field_info *, struct die_info *,
e7c27a73 1651 struct dwarf2_cu *);
c906108c 1652
a14ed312 1653static void dwarf2_attach_fields_to_type (struct field_info *,
e7c27a73 1654 struct type *, struct dwarf2_cu *);
c906108c 1655
a14ed312 1656static void dwarf2_add_member_fn (struct field_info *,
e26fb1d7 1657 struct die_info *, struct type *,
e7c27a73 1658 struct dwarf2_cu *);
c906108c 1659
a14ed312 1660static void dwarf2_attach_fn_fields_to_type (struct field_info *,
3e43a32a
MS
1661 struct type *,
1662 struct dwarf2_cu *);
c906108c 1663
134d01f1 1664static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1665
e7c27a73 1666static void read_common_block (struct die_info *, struct dwarf2_cu *);
c906108c 1667
e7c27a73 1668static void read_namespace (struct die_info *die, struct dwarf2_cu *);
d9fa45fe 1669
5d7cb8df
JK
1670static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1671
22cee43f
PMR
1672static struct using_direct **using_directives (enum language);
1673
27aa8d6a
SW
1674static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1675
74921315
KS
1676static int read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu);
1677
f55ee35c
JK
1678static struct type *read_module_type (struct die_info *die,
1679 struct dwarf2_cu *cu);
1680
38d518c9 1681static const char *namespace_name (struct die_info *die,
e142c38c 1682 int *is_anonymous, struct dwarf2_cu *);
38d518c9 1683
134d01f1 1684static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1685
e7c27a73 1686static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *);
c906108c 1687
6e70227d 1688static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
7ca2d3a3
DL
1689 struct dwarf2_cu *);
1690
bf6af496 1691static struct die_info *read_die_and_siblings_1
d521ce57 1692 (const struct die_reader_specs *, const gdb_byte *, const gdb_byte **,
bf6af496 1693 struct die_info *);
639d11d3 1694
dee91e82 1695static struct die_info *read_die_and_siblings (const struct die_reader_specs *,
d521ce57
TT
1696 const gdb_byte *info_ptr,
1697 const gdb_byte **new_info_ptr,
639d11d3
DC
1698 struct die_info *parent);
1699
d521ce57
TT
1700static const gdb_byte *read_full_die_1 (const struct die_reader_specs *,
1701 struct die_info **, const gdb_byte *,
1702 int *, int);
3019eac3 1703
d521ce57
TT
1704static const gdb_byte *read_full_die (const struct die_reader_specs *,
1705 struct die_info **, const gdb_byte *,
1706 int *);
93311388 1707
e7c27a73 1708static void process_die (struct die_info *, struct dwarf2_cu *);
c906108c 1709
15d034d0
TT
1710static const char *dwarf2_canonicalize_name (const char *, struct dwarf2_cu *,
1711 struct obstack *);
71c25dea 1712
15d034d0 1713static const char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
9219021c 1714
15d034d0 1715static const char *dwarf2_full_name (const char *name,
98bfdba5
PA
1716 struct die_info *die,
1717 struct dwarf2_cu *cu);
1718
ca69b9e6
DE
1719static const char *dwarf2_physname (const char *name, struct die_info *die,
1720 struct dwarf2_cu *cu);
1721
e142c38c 1722static struct die_info *dwarf2_extension (struct die_info *die,
f2f0e013 1723 struct dwarf2_cu **);
9219021c 1724
f39c6ffd 1725static const char *dwarf_tag_name (unsigned int);
c906108c 1726
f39c6ffd 1727static const char *dwarf_attr_name (unsigned int);
c906108c 1728
f39c6ffd 1729static const char *dwarf_form_name (unsigned int);
c906108c 1730
a121b7c1 1731static const char *dwarf_bool_name (unsigned int);
c906108c 1732
f39c6ffd 1733static const char *dwarf_type_encoding_name (unsigned int);
c906108c 1734
f9aca02d 1735static struct die_info *sibling_die (struct die_info *);
c906108c 1736
d97bc12b
DE
1737static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1738
1739static void dump_die_for_error (struct die_info *);
1740
1741static void dump_die_1 (struct ui_file *, int level, int max_level,
1742 struct die_info *);
c906108c 1743
d97bc12b 1744/*static*/ void dump_die (struct die_info *, int max_level);
c906108c 1745
51545339 1746static void store_in_ref_table (struct die_info *,
10b3939b 1747 struct dwarf2_cu *);
c906108c 1748
ff39bb5e 1749static sect_offset dwarf2_get_ref_die_offset (const struct attribute *);
c906108c 1750
ff39bb5e 1751static LONGEST dwarf2_get_attr_constant_value (const struct attribute *, int);
a02abb62 1752
348e048f 1753static struct die_info *follow_die_ref_or_sig (struct die_info *,
ff39bb5e 1754 const struct attribute *,
348e048f
DE
1755 struct dwarf2_cu **);
1756
10b3939b 1757static struct die_info *follow_die_ref (struct die_info *,
ff39bb5e 1758 const struct attribute *,
f2f0e013 1759 struct dwarf2_cu **);
c906108c 1760
348e048f 1761static struct die_info *follow_die_sig (struct die_info *,
ff39bb5e 1762 const struct attribute *,
348e048f
DE
1763 struct dwarf2_cu **);
1764
ac9ec31b
DE
1765static struct type *get_signatured_type (struct die_info *, ULONGEST,
1766 struct dwarf2_cu *);
1767
1768static struct type *get_DW_AT_signature_type (struct die_info *,
ff39bb5e 1769 const struct attribute *,
ac9ec31b
DE
1770 struct dwarf2_cu *);
1771
e5fe5e75 1772static void load_full_type_unit (struct dwarf2_per_cu_data *per_cu);
348e048f 1773
52dc124a 1774static void read_signatured_type (struct signatured_type *);
348e048f 1775
63e43d3a
PMR
1776static int attr_to_dynamic_prop (const struct attribute *attr,
1777 struct die_info *die, struct dwarf2_cu *cu,
1778 struct dynamic_prop *prop);
1779
c906108c
SS
1780/* memory allocation interface */
1781
7b5a2f43 1782static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
c906108c 1783
b60c80d6 1784static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
c906108c 1785
43f3e411 1786static void dwarf_decode_macros (struct dwarf2_cu *, unsigned int, int);
2e276125 1787
6e5a29e1 1788static int attr_form_is_block (const struct attribute *);
8e19ed76 1789
6e5a29e1 1790static int attr_form_is_section_offset (const struct attribute *);
3690dd37 1791
6e5a29e1 1792static int attr_form_is_constant (const struct attribute *);
3690dd37 1793
6e5a29e1 1794static int attr_form_is_ref (const struct attribute *);
7771576e 1795
8cf6f0b1
TT
1796static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1797 struct dwarf2_loclist_baton *baton,
ff39bb5e 1798 const struct attribute *attr);
8cf6f0b1 1799
ff39bb5e 1800static void dwarf2_symbol_mark_computed (const struct attribute *attr,
93e7bd98 1801 struct symbol *sym,
f1e6e072
TT
1802 struct dwarf2_cu *cu,
1803 int is_block);
4c2df51b 1804
d521ce57
TT
1805static const gdb_byte *skip_one_die (const struct die_reader_specs *reader,
1806 const gdb_byte *info_ptr,
1807 struct abbrev_info *abbrev);
4bb7a0a7 1808
72bf9492
DJ
1809static hashval_t partial_die_hash (const void *item);
1810
1811static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1812
ae038cb0 1813static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
ed2dc618
SM
1814 (sect_offset sect_off, unsigned int offset_in_dwz,
1815 struct dwarf2_per_objfile *dwarf2_per_objfile);
ae038cb0 1816
9816fde3 1817static void prepare_one_comp_unit (struct dwarf2_cu *cu,
95554aad
TT
1818 struct die_info *comp_unit_die,
1819 enum language pretend_language);
93311388 1820
ed2dc618 1821static void age_cached_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
ae038cb0 1822
dee91e82 1823static void free_one_cached_comp_unit (struct dwarf2_per_cu_data *);
ae038cb0 1824
f792889a
DJ
1825static struct type *set_die_type (struct die_info *, struct type *,
1826 struct dwarf2_cu *);
1c379e20 1827
ed2dc618 1828static void create_all_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
ae038cb0 1829
ed2dc618 1830static int create_all_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
1fd400ff 1831
58f0c718 1832static void load_full_comp_unit (struct dwarf2_per_cu_data *, bool,
95554aad 1833 enum language);
10b3939b 1834
95554aad
TT
1835static void process_full_comp_unit (struct dwarf2_per_cu_data *,
1836 enum language);
10b3939b 1837
f4dc4d17
DE
1838static void process_full_type_unit (struct dwarf2_per_cu_data *,
1839 enum language);
1840
10b3939b
DJ
1841static void dwarf2_add_dependence (struct dwarf2_cu *,
1842 struct dwarf2_per_cu_data *);
1843
ae038cb0
DJ
1844static void dwarf2_mark (struct dwarf2_cu *);
1845
1846static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
1847
b64f50a1 1848static struct type *get_die_type_at_offset (sect_offset,
ac9ec31b 1849 struct dwarf2_per_cu_data *);
673bfd45 1850
f792889a 1851static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
72019c9c 1852
95554aad
TT
1853static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
1854 enum language pretend_language);
1855
ed2dc618 1856static void process_queue (struct dwarf2_per_objfile *dwarf2_per_objfile);
9291a0cd 1857
b303c6f6
AB
1858/* Class, the destructor of which frees all allocated queue entries. This
1859 will only have work to do if an error was thrown while processing the
1860 dwarf. If no error was thrown then the queue entries should have all
1861 been processed, and freed, as we went along. */
1862
1863class dwarf2_queue_guard
1864{
1865public:
1866 dwarf2_queue_guard () = default;
1867
1868 /* Free any entries remaining on the queue. There should only be
1869 entries left if we hit an error while processing the dwarf. */
1870 ~dwarf2_queue_guard ()
1871 {
1872 struct dwarf2_queue_item *item, *last;
1873
1874 item = dwarf2_queue;
1875 while (item)
1876 {
1877 /* Anything still marked queued is likely to be in an
1878 inconsistent state, so discard it. */
1879 if (item->per_cu->queued)
1880 {
1881 if (item->per_cu->cu != NULL)
1882 free_one_cached_comp_unit (item->per_cu);
1883 item->per_cu->queued = 0;
1884 }
1885
1886 last = item;
1887 item = item->next;
1888 xfree (last);
1889 }
1890
1891 dwarf2_queue = dwarf2_queue_tail = NULL;
1892 }
1893};
1894
d721ba37
PA
1895/* The return type of find_file_and_directory. Note, the enclosed
1896 string pointers are only valid while this object is valid. */
1897
1898struct file_and_directory
1899{
1900 /* The filename. This is never NULL. */
1901 const char *name;
1902
1903 /* The compilation directory. NULL if not known. If we needed to
1904 compute a new string, this points to COMP_DIR_STORAGE, otherwise,
1905 points directly to the DW_AT_comp_dir string attribute owned by
1906 the obstack that owns the DIE. */
1907 const char *comp_dir;
1908
1909 /* If we needed to build a new string for comp_dir, this is what
1910 owns the storage. */
1911 std::string comp_dir_storage;
1912};
1913
1914static file_and_directory find_file_and_directory (struct die_info *die,
1915 struct dwarf2_cu *cu);
9291a0cd
TT
1916
1917static char *file_full_name (int file, struct line_header *lh,
1918 const char *comp_dir);
1919
43988095
JK
1920/* Expected enum dwarf_unit_type for read_comp_unit_head. */
1921enum class rcuh_kind { COMPILE, TYPE };
1922
d521ce57 1923static const gdb_byte *read_and_check_comp_unit_head
ed2dc618
SM
1924 (struct dwarf2_per_objfile* dwarf2_per_objfile,
1925 struct comp_unit_head *header,
36586728 1926 struct dwarf2_section_info *section,
d521ce57 1927 struct dwarf2_section_info *abbrev_section, const gdb_byte *info_ptr,
43988095 1928 rcuh_kind section_kind);
36586728 1929
fd820528 1930static void init_cutu_and_read_dies
f4dc4d17 1931 (struct dwarf2_per_cu_data *this_cu, struct abbrev_table *abbrev_table,
58f0c718 1932 int use_existing_cu, int keep, bool skip_partial,
3019eac3
DE
1933 die_reader_func_ftype *die_reader_func, void *data);
1934
dee91e82
DE
1935static void init_cutu_and_read_dies_simple
1936 (struct dwarf2_per_cu_data *this_cu,
1937 die_reader_func_ftype *die_reader_func, void *data);
9291a0cd 1938
673bfd45 1939static htab_t allocate_signatured_type_table (struct objfile *objfile);
1fd400ff 1940
3019eac3
DE
1941static htab_t allocate_dwo_unit_table (struct objfile *objfile);
1942
57d63ce2 1943static struct dwo_unit *lookup_dwo_unit_in_dwp
ed2dc618
SM
1944 (struct dwarf2_per_objfile *dwarf2_per_objfile,
1945 struct dwp_file *dwp_file, const char *comp_dir,
57d63ce2 1946 ULONGEST signature, int is_debug_types);
a2ce51a0 1947
ed2dc618
SM
1948static struct dwp_file *get_dwp_file
1949 (struct dwarf2_per_objfile *dwarf2_per_objfile);
a2ce51a0 1950
3019eac3 1951static struct dwo_unit *lookup_dwo_comp_unit
a1855c1d 1952 (struct dwarf2_per_cu_data *, const char *, const char *, ULONGEST);
3019eac3
DE
1953
1954static struct dwo_unit *lookup_dwo_type_unit
a1855c1d 1955 (struct signatured_type *, const char *, const char *);
3019eac3 1956
89e63ee4
DE
1957static void queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *);
1958
263db9a1 1959static void free_dwo_file (struct dwo_file *);
3019eac3 1960
263db9a1
TT
1961/* A unique_ptr helper to free a dwo_file. */
1962
1963struct dwo_file_deleter
ed2dc618 1964{
263db9a1
TT
1965 void operator() (struct dwo_file *df) const
1966 {
1967 free_dwo_file (df);
1968 }
ed2dc618
SM
1969};
1970
263db9a1
TT
1971/* A unique pointer to a dwo_file. */
1972
1973typedef std::unique_ptr<struct dwo_file, dwo_file_deleter> dwo_file_up;
1974
ed2dc618 1975static void process_cu_includes (struct dwarf2_per_objfile *dwarf2_per_objfile);
95554aad 1976
1b80a9fa 1977static void check_producer (struct dwarf2_cu *cu);
527f3840
JK
1978
1979static void free_line_header_voidp (void *arg);
4390d890
DE
1980\f
1981/* Various complaints about symbol reading that don't abort the process. */
1982
1983static void
1984dwarf2_statement_list_fits_in_line_number_section_complaint (void)
1985{
1986 complaint (&symfile_complaints,
1987 _("statement list doesn't fit in .debug_line section"));
1988}
1989
1990static void
1991dwarf2_debug_line_missing_file_complaint (void)
1992{
1993 complaint (&symfile_complaints,
1994 _(".debug_line section has line data without a file"));
1995}
1996
1997static void
1998dwarf2_debug_line_missing_end_sequence_complaint (void)
1999{
2000 complaint (&symfile_complaints,
2001 _(".debug_line section has line "
2002 "program sequence without an end"));
2003}
2004
2005static void
2006dwarf2_complex_location_expr_complaint (void)
2007{
2008 complaint (&symfile_complaints, _("location expression too complex"));
2009}
2010
2011static void
2012dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
2013 int arg3)
2014{
2015 complaint (&symfile_complaints,
2016 _("const value length mismatch for '%s', got %d, expected %d"),
2017 arg1, arg2, arg3);
2018}
2019
2020static void
2021dwarf2_section_buffer_overflow_complaint (struct dwarf2_section_info *section)
2022{
2023 complaint (&symfile_complaints,
2024 _("debug info runs off end of %s section"
2025 " [in module %s]"),
a32a8923
DE
2026 get_section_name (section),
2027 get_section_file_name (section));
4390d890 2028}
1b80a9fa 2029
4390d890
DE
2030static void
2031dwarf2_macro_malformed_definition_complaint (const char *arg1)
2032{
2033 complaint (&symfile_complaints,
2034 _("macro debug info contains a "
2035 "malformed macro definition:\n`%s'"),
2036 arg1);
2037}
2038
2039static void
2040dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
2041{
2042 complaint (&symfile_complaints,
2043 _("invalid attribute class or form for '%s' in '%s'"),
2044 arg1, arg2);
2045}
527f3840
JK
2046
2047/* Hash function for line_header_hash. */
2048
2049static hashval_t
2050line_header_hash (const struct line_header *ofs)
2051{
9c541725 2052 return to_underlying (ofs->sect_off) ^ ofs->offset_in_dwz;
527f3840
JK
2053}
2054
2055/* Hash function for htab_create_alloc_ex for line_header_hash. */
2056
2057static hashval_t
2058line_header_hash_voidp (const void *item)
2059{
9a3c8263 2060 const struct line_header *ofs = (const struct line_header *) item;
527f3840
JK
2061
2062 return line_header_hash (ofs);
2063}
2064
2065/* Equality function for line_header_hash. */
2066
2067static int
2068line_header_eq_voidp (const void *item_lhs, const void *item_rhs)
2069{
9a3c8263
SM
2070 const struct line_header *ofs_lhs = (const struct line_header *) item_lhs;
2071 const struct line_header *ofs_rhs = (const struct line_header *) item_rhs;
527f3840 2072
9c541725 2073 return (ofs_lhs->sect_off == ofs_rhs->sect_off
527f3840
JK
2074 && ofs_lhs->offset_in_dwz == ofs_rhs->offset_in_dwz);
2075}
2076
4390d890 2077\f
9291a0cd 2078
31aa7e4e
JB
2079/* Read the given attribute value as an address, taking the attribute's
2080 form into account. */
2081
2082static CORE_ADDR
2083attr_value_as_address (struct attribute *attr)
2084{
2085 CORE_ADDR addr;
2086
2087 if (attr->form != DW_FORM_addr && attr->form != DW_FORM_GNU_addr_index)
2088 {
2089 /* Aside from a few clearly defined exceptions, attributes that
2090 contain an address must always be in DW_FORM_addr form.
2091 Unfortunately, some compilers happen to be violating this
2092 requirement by encoding addresses using other forms, such
2093 as DW_FORM_data4 for example. For those broken compilers,
2094 we try to do our best, without any guarantee of success,
2095 to interpret the address correctly. It would also be nice
2096 to generate a complaint, but that would require us to maintain
2097 a list of legitimate cases where a non-address form is allowed,
2098 as well as update callers to pass in at least the CU's DWARF
2099 version. This is more overhead than what we're willing to
2100 expand for a pretty rare case. */
2101 addr = DW_UNSND (attr);
2102 }
2103 else
2104 addr = DW_ADDR (attr);
2105
2106 return addr;
2107}
2108
330cdd98
PA
2109/* See declaration. */
2110
2111dwarf2_per_objfile::dwarf2_per_objfile (struct objfile *objfile_,
2112 const dwarf2_debug_sections *names)
2113 : objfile (objfile_)
2114{
2115 if (names == NULL)
2116 names = &dwarf2_elf_names;
2117
2118 bfd *obfd = objfile->obfd;
2119
2120 for (asection *sec = obfd->sections; sec != NULL; sec = sec->next)
2121 locate_sections (obfd, sec, *names);
2122}
2123
fc8e7e75
SM
2124static void free_dwo_files (htab_t dwo_files, struct objfile *objfile);
2125
330cdd98
PA
2126dwarf2_per_objfile::~dwarf2_per_objfile ()
2127{
2128 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
2129 free_cached_comp_units ();
2130
2131 if (quick_file_names_table)
2132 htab_delete (quick_file_names_table);
2133
2134 if (line_header_hash)
2135 htab_delete (line_header_hash);
2136
b76e467d
SM
2137 for (dwarf2_per_cu_data *per_cu : all_comp_units)
2138 VEC_free (dwarf2_per_cu_ptr, per_cu->imported_symtabs);
fc8e7e75 2139
b2bdb8cf
SM
2140 for (signatured_type *sig_type : all_type_units)
2141 VEC_free (dwarf2_per_cu_ptr, sig_type->per_cu.imported_symtabs);
fc8e7e75
SM
2142
2143 VEC_free (dwarf2_section_info_def, types);
2144
2145 if (dwo_files != NULL)
2146 free_dwo_files (dwo_files, objfile);
2147 if (dwp_file != NULL)
2148 gdb_bfd_unref (dwp_file->dbfd);
2149
2150 if (dwz_file != NULL && dwz_file->dwz_bfd)
2151 gdb_bfd_unref (dwz_file->dwz_bfd);
2152
330cdd98
PA
2153 /* Everything else should be on the objfile obstack. */
2154}
2155
2156/* See declaration. */
2157
2158void
2159dwarf2_per_objfile::free_cached_comp_units ()
2160{
2161 dwarf2_per_cu_data *per_cu = read_in_chain;
2162 dwarf2_per_cu_data **last_chain = &read_in_chain;
2163 while (per_cu != NULL)
2164 {
2165 dwarf2_per_cu_data *next_cu = per_cu->cu->read_in_chain;
2166
fcd3b13d 2167 delete per_cu->cu;
330cdd98
PA
2168 *last_chain = next_cu;
2169 per_cu = next_cu;
2170 }
2171}
2172
11ed8cad
TT
2173/* A helper class that calls free_cached_comp_units on
2174 destruction. */
2175
2176class free_cached_comp_units
2177{
2178public:
2179
2180 explicit free_cached_comp_units (dwarf2_per_objfile *per_objfile)
2181 : m_per_objfile (per_objfile)
2182 {
2183 }
2184
2185 ~free_cached_comp_units ()
2186 {
2187 m_per_objfile->free_cached_comp_units ();
2188 }
2189
2190 DISABLE_COPY_AND_ASSIGN (free_cached_comp_units);
2191
2192private:
2193
2194 dwarf2_per_objfile *m_per_objfile;
2195};
2196
c906108c 2197/* Try to locate the sections we need for DWARF 2 debugging
251d32d9
TG
2198 information and return true if we have enough to do something.
2199 NAMES points to the dwarf2 section names, or is NULL if the standard
2200 ELF names are used. */
c906108c
SS
2201
2202int
251d32d9
TG
2203dwarf2_has_info (struct objfile *objfile,
2204 const struct dwarf2_debug_sections *names)
c906108c 2205{
97cbe998
SDJ
2206 if (objfile->flags & OBJF_READNEVER)
2207 return 0;
2208
ed2dc618
SM
2209 struct dwarf2_per_objfile *dwarf2_per_objfile
2210 = get_dwarf2_per_objfile (objfile);
2211
2212 if (dwarf2_per_objfile == NULL)
be391dca
TT
2213 {
2214 /* Initialize per-objfile state. */
fd90ace4
YQ
2215 dwarf2_per_objfile
2216 = new (&objfile->objfile_obstack) struct dwarf2_per_objfile (objfile,
2217 names);
ed2dc618 2218 set_dwarf2_per_objfile (objfile, dwarf2_per_objfile);
be391dca 2219 }
73869dc2 2220 return (!dwarf2_per_objfile->info.is_virtual
049412e3 2221 && dwarf2_per_objfile->info.s.section != NULL
73869dc2 2222 && !dwarf2_per_objfile->abbrev.is_virtual
049412e3 2223 && dwarf2_per_objfile->abbrev.s.section != NULL);
73869dc2
DE
2224}
2225
2226/* Return the containing section of virtual section SECTION. */
2227
2228static struct dwarf2_section_info *
2229get_containing_section (const struct dwarf2_section_info *section)
2230{
2231 gdb_assert (section->is_virtual);
2232 return section->s.containing_section;
c906108c
SS
2233}
2234
a32a8923
DE
2235/* Return the bfd owner of SECTION. */
2236
2237static struct bfd *
2238get_section_bfd_owner (const struct dwarf2_section_info *section)
2239{
73869dc2
DE
2240 if (section->is_virtual)
2241 {
2242 section = get_containing_section (section);
2243 gdb_assert (!section->is_virtual);
2244 }
049412e3 2245 return section->s.section->owner;
a32a8923
DE
2246}
2247
2248/* Return the bfd section of SECTION.
2249 Returns NULL if the section is not present. */
2250
2251static asection *
2252get_section_bfd_section (const struct dwarf2_section_info *section)
2253{
73869dc2
DE
2254 if (section->is_virtual)
2255 {
2256 section = get_containing_section (section);
2257 gdb_assert (!section->is_virtual);
2258 }
049412e3 2259 return section->s.section;
a32a8923
DE
2260}
2261
2262/* Return the name of SECTION. */
2263
2264static const char *
2265get_section_name (const struct dwarf2_section_info *section)
2266{
2267 asection *sectp = get_section_bfd_section (section);
2268
2269 gdb_assert (sectp != NULL);
2270 return bfd_section_name (get_section_bfd_owner (section), sectp);
2271}
2272
2273/* Return the name of the file SECTION is in. */
2274
2275static const char *
2276get_section_file_name (const struct dwarf2_section_info *section)
2277{
2278 bfd *abfd = get_section_bfd_owner (section);
2279
2280 return bfd_get_filename (abfd);
2281}
2282
2283/* Return the id of SECTION.
2284 Returns 0 if SECTION doesn't exist. */
2285
2286static int
2287get_section_id (const struct dwarf2_section_info *section)
2288{
2289 asection *sectp = get_section_bfd_section (section);
2290
2291 if (sectp == NULL)
2292 return 0;
2293 return sectp->id;
2294}
2295
2296/* Return the flags of SECTION.
73869dc2 2297 SECTION (or containing section if this is a virtual section) must exist. */
a32a8923
DE
2298
2299static int
2300get_section_flags (const struct dwarf2_section_info *section)
2301{
2302 asection *sectp = get_section_bfd_section (section);
2303
2304 gdb_assert (sectp != NULL);
2305 return bfd_get_section_flags (sectp->owner, sectp);
2306}
2307
251d32d9
TG
2308/* When loading sections, we look either for uncompressed section or for
2309 compressed section names. */
233a11ab
CS
2310
2311static int
251d32d9
TG
2312section_is_p (const char *section_name,
2313 const struct dwarf2_section_names *names)
233a11ab 2314{
251d32d9
TG
2315 if (names->normal != NULL
2316 && strcmp (section_name, names->normal) == 0)
2317 return 1;
2318 if (names->compressed != NULL
2319 && strcmp (section_name, names->compressed) == 0)
2320 return 1;
2321 return 0;
233a11ab
CS
2322}
2323
330cdd98 2324/* See declaration. */
c906108c 2325
330cdd98
PA
2326void
2327dwarf2_per_objfile::locate_sections (bfd *abfd, asection *sectp,
2328 const dwarf2_debug_sections &names)
c906108c 2329{
dc7650b8 2330 flagword aflag = bfd_get_section_flags (abfd, sectp);
251d32d9 2331
dc7650b8
JK
2332 if ((aflag & SEC_HAS_CONTENTS) == 0)
2333 {
2334 }
330cdd98 2335 else if (section_is_p (sectp->name, &names.info))
c906108c 2336 {
330cdd98
PA
2337 this->info.s.section = sectp;
2338 this->info.size = bfd_get_section_size (sectp);
c906108c 2339 }
330cdd98 2340 else if (section_is_p (sectp->name, &names.abbrev))
c906108c 2341 {
330cdd98
PA
2342 this->abbrev.s.section = sectp;
2343 this->abbrev.size = bfd_get_section_size (sectp);
c906108c 2344 }
330cdd98 2345 else if (section_is_p (sectp->name, &names.line))
c906108c 2346 {
330cdd98
PA
2347 this->line.s.section = sectp;
2348 this->line.size = bfd_get_section_size (sectp);
c906108c 2349 }
330cdd98 2350 else if (section_is_p (sectp->name, &names.loc))
c906108c 2351 {
330cdd98
PA
2352 this->loc.s.section = sectp;
2353 this->loc.size = bfd_get_section_size (sectp);
c906108c 2354 }
330cdd98 2355 else if (section_is_p (sectp->name, &names.loclists))
43988095 2356 {
330cdd98
PA
2357 this->loclists.s.section = sectp;
2358 this->loclists.size = bfd_get_section_size (sectp);
43988095 2359 }
330cdd98 2360 else if (section_is_p (sectp->name, &names.macinfo))
c906108c 2361 {
330cdd98
PA
2362 this->macinfo.s.section = sectp;
2363 this->macinfo.size = bfd_get_section_size (sectp);
c906108c 2364 }
330cdd98 2365 else if (section_is_p (sectp->name, &names.macro))
cf2c3c16 2366 {
330cdd98
PA
2367 this->macro.s.section = sectp;
2368 this->macro.size = bfd_get_section_size (sectp);
cf2c3c16 2369 }
330cdd98 2370 else if (section_is_p (sectp->name, &names.str))
c906108c 2371 {
330cdd98
PA
2372 this->str.s.section = sectp;
2373 this->str.size = bfd_get_section_size (sectp);
c906108c 2374 }
330cdd98 2375 else if (section_is_p (sectp->name, &names.line_str))
43988095 2376 {
330cdd98
PA
2377 this->line_str.s.section = sectp;
2378 this->line_str.size = bfd_get_section_size (sectp);
43988095 2379 }
330cdd98 2380 else if (section_is_p (sectp->name, &names.addr))
3019eac3 2381 {
330cdd98
PA
2382 this->addr.s.section = sectp;
2383 this->addr.size = bfd_get_section_size (sectp);
3019eac3 2384 }
330cdd98 2385 else if (section_is_p (sectp->name, &names.frame))
b6af0555 2386 {
330cdd98
PA
2387 this->frame.s.section = sectp;
2388 this->frame.size = bfd_get_section_size (sectp);
b6af0555 2389 }
330cdd98 2390 else if (section_is_p (sectp->name, &names.eh_frame))
b6af0555 2391 {
330cdd98
PA
2392 this->eh_frame.s.section = sectp;
2393 this->eh_frame.size = bfd_get_section_size (sectp);
b6af0555 2394 }
330cdd98 2395 else if (section_is_p (sectp->name, &names.ranges))
af34e669 2396 {
330cdd98
PA
2397 this->ranges.s.section = sectp;
2398 this->ranges.size = bfd_get_section_size (sectp);
af34e669 2399 }
330cdd98 2400 else if (section_is_p (sectp->name, &names.rnglists))
43988095 2401 {
330cdd98
PA
2402 this->rnglists.s.section = sectp;
2403 this->rnglists.size = bfd_get_section_size (sectp);
43988095 2404 }
330cdd98 2405 else if (section_is_p (sectp->name, &names.types))
348e048f 2406 {
8b70b953
TT
2407 struct dwarf2_section_info type_section;
2408
2409 memset (&type_section, 0, sizeof (type_section));
049412e3 2410 type_section.s.section = sectp;
8b70b953
TT
2411 type_section.size = bfd_get_section_size (sectp);
2412
330cdd98 2413 VEC_safe_push (dwarf2_section_info_def, this->types,
8b70b953 2414 &type_section);
348e048f 2415 }
330cdd98 2416 else if (section_is_p (sectp->name, &names.gdb_index))
9291a0cd 2417 {
330cdd98
PA
2418 this->gdb_index.s.section = sectp;
2419 this->gdb_index.size = bfd_get_section_size (sectp);
9291a0cd 2420 }
927aa2e7
JK
2421 else if (section_is_p (sectp->name, &names.debug_names))
2422 {
2423 this->debug_names.s.section = sectp;
2424 this->debug_names.size = bfd_get_section_size (sectp);
2425 }
2426 else if (section_is_p (sectp->name, &names.debug_aranges))
2427 {
2428 this->debug_aranges.s.section = sectp;
2429 this->debug_aranges.size = bfd_get_section_size (sectp);
2430 }
dce234bc 2431
b4e1fd61 2432 if ((bfd_get_section_flags (abfd, sectp) & (SEC_LOAD | SEC_ALLOC))
72dca2f5 2433 && bfd_section_vma (abfd, sectp) == 0)
330cdd98 2434 this->has_section_at_zero = true;
c906108c
SS
2435}
2436
fceca515
DE
2437/* A helper function that decides whether a section is empty,
2438 or not present. */
9e0ac564
TT
2439
2440static int
19ac8c2e 2441dwarf2_section_empty_p (const struct dwarf2_section_info *section)
9e0ac564 2442{
73869dc2
DE
2443 if (section->is_virtual)
2444 return section->size == 0;
049412e3 2445 return section->s.section == NULL || section->size == 0;
9e0ac564
TT
2446}
2447
cd4fb1b2 2448/* See dwarf2read.h. */
c906108c 2449
cd4fb1b2
SM
2450void
2451dwarf2_read_section (struct objfile *objfile, dwarf2_section_info *info)
c906108c 2452{
a32a8923 2453 asection *sectp;
3019eac3 2454 bfd *abfd;
dce234bc 2455 gdb_byte *buf, *retbuf;
c906108c 2456
be391dca
TT
2457 if (info->readin)
2458 return;
dce234bc 2459 info->buffer = NULL;
be391dca 2460 info->readin = 1;
188dd5d6 2461
9e0ac564 2462 if (dwarf2_section_empty_p (info))
dce234bc 2463 return;
c906108c 2464
a32a8923 2465 sectp = get_section_bfd_section (info);
3019eac3 2466
73869dc2
DE
2467 /* If this is a virtual section we need to read in the real one first. */
2468 if (info->is_virtual)
2469 {
2470 struct dwarf2_section_info *containing_section =
2471 get_containing_section (info);
2472
2473 gdb_assert (sectp != NULL);
2474 if ((sectp->flags & SEC_RELOC) != 0)
2475 {
2476 error (_("Dwarf Error: DWP format V2 with relocations is not"
2477 " supported in section %s [in module %s]"),
2478 get_section_name (info), get_section_file_name (info));
2479 }
2480 dwarf2_read_section (objfile, containing_section);
2481 /* Other code should have already caught virtual sections that don't
2482 fit. */
2483 gdb_assert (info->virtual_offset + info->size
2484 <= containing_section->size);
2485 /* If the real section is empty or there was a problem reading the
2486 section we shouldn't get here. */
2487 gdb_assert (containing_section->buffer != NULL);
2488 info->buffer = containing_section->buffer + info->virtual_offset;
2489 return;
2490 }
2491
4bf44c1c
TT
2492 /* If the section has relocations, we must read it ourselves.
2493 Otherwise we attach it to the BFD. */
2494 if ((sectp->flags & SEC_RELOC) == 0)
dce234bc 2495 {
d521ce57 2496 info->buffer = gdb_bfd_map_section (sectp, &info->size);
4bf44c1c 2497 return;
dce234bc 2498 }
dce234bc 2499
224c3ddb 2500 buf = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, info->size);
4bf44c1c 2501 info->buffer = buf;
dce234bc
PP
2502
2503 /* When debugging .o files, we may need to apply relocations; see
2504 http://sourceware.org/ml/gdb-patches/2002-04/msg00136.html .
2505 We never compress sections in .o files, so we only need to
2506 try this when the section is not compressed. */
ac8035ab 2507 retbuf = symfile_relocate_debug_section (objfile, sectp, buf);
dce234bc
PP
2508 if (retbuf != NULL)
2509 {
2510 info->buffer = retbuf;
2511 return;
2512 }
2513
a32a8923
DE
2514 abfd = get_section_bfd_owner (info);
2515 gdb_assert (abfd != NULL);
2516
dce234bc
PP
2517 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
2518 || bfd_bread (buf, info->size, abfd) != info->size)
19ac8c2e
DE
2519 {
2520 error (_("Dwarf Error: Can't read DWARF data"
2521 " in section %s [in module %s]"),
2522 bfd_section_name (abfd, sectp), bfd_get_filename (abfd));
2523 }
dce234bc
PP
2524}
2525
9e0ac564
TT
2526/* A helper function that returns the size of a section in a safe way.
2527 If you are positive that the section has been read before using the
2528 size, then it is safe to refer to the dwarf2_section_info object's
2529 "size" field directly. In other cases, you must call this
2530 function, because for compressed sections the size field is not set
2531 correctly until the section has been read. */
2532
2533static bfd_size_type
2534dwarf2_section_size (struct objfile *objfile,
2535 struct dwarf2_section_info *info)
2536{
2537 if (!info->readin)
2538 dwarf2_read_section (objfile, info);
2539 return info->size;
2540}
2541
dce234bc 2542/* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
0963b4bd 2543 SECTION_NAME. */
af34e669 2544
dce234bc 2545void
3017a003
TG
2546dwarf2_get_section_info (struct objfile *objfile,
2547 enum dwarf2_section_enum sect,
d521ce57 2548 asection **sectp, const gdb_byte **bufp,
dce234bc
PP
2549 bfd_size_type *sizep)
2550{
2551 struct dwarf2_per_objfile *data
9a3c8263
SM
2552 = (struct dwarf2_per_objfile *) objfile_data (objfile,
2553 dwarf2_objfile_data_key);
dce234bc 2554 struct dwarf2_section_info *info;
a3b2a86b
TT
2555
2556 /* We may see an objfile without any DWARF, in which case we just
2557 return nothing. */
2558 if (data == NULL)
2559 {
2560 *sectp = NULL;
2561 *bufp = NULL;
2562 *sizep = 0;
2563 return;
2564 }
3017a003
TG
2565 switch (sect)
2566 {
2567 case DWARF2_DEBUG_FRAME:
2568 info = &data->frame;
2569 break;
2570 case DWARF2_EH_FRAME:
2571 info = &data->eh_frame;
2572 break;
2573 default:
2574 gdb_assert_not_reached ("unexpected section");
2575 }
dce234bc 2576
9e0ac564 2577 dwarf2_read_section (objfile, info);
dce234bc 2578
a32a8923 2579 *sectp = get_section_bfd_section (info);
dce234bc
PP
2580 *bufp = info->buffer;
2581 *sizep = info->size;
2582}
2583
36586728
TT
2584/* A helper function to find the sections for a .dwz file. */
2585
2586static void
2587locate_dwz_sections (bfd *abfd, asection *sectp, void *arg)
2588{
9a3c8263 2589 struct dwz_file *dwz_file = (struct dwz_file *) arg;
36586728
TT
2590
2591 /* Note that we only support the standard ELF names, because .dwz
2592 is ELF-only (at the time of writing). */
2593 if (section_is_p (sectp->name, &dwarf2_elf_names.abbrev))
2594 {
049412e3 2595 dwz_file->abbrev.s.section = sectp;
36586728
TT
2596 dwz_file->abbrev.size = bfd_get_section_size (sectp);
2597 }
2598 else if (section_is_p (sectp->name, &dwarf2_elf_names.info))
2599 {
049412e3 2600 dwz_file->info.s.section = sectp;
36586728
TT
2601 dwz_file->info.size = bfd_get_section_size (sectp);
2602 }
2603 else if (section_is_p (sectp->name, &dwarf2_elf_names.str))
2604 {
049412e3 2605 dwz_file->str.s.section = sectp;
36586728
TT
2606 dwz_file->str.size = bfd_get_section_size (sectp);
2607 }
2608 else if (section_is_p (sectp->name, &dwarf2_elf_names.line))
2609 {
049412e3 2610 dwz_file->line.s.section = sectp;
36586728
TT
2611 dwz_file->line.size = bfd_get_section_size (sectp);
2612 }
2613 else if (section_is_p (sectp->name, &dwarf2_elf_names.macro))
2614 {
049412e3 2615 dwz_file->macro.s.section = sectp;
36586728
TT
2616 dwz_file->macro.size = bfd_get_section_size (sectp);
2617 }
2ec9a5e0
TT
2618 else if (section_is_p (sectp->name, &dwarf2_elf_names.gdb_index))
2619 {
049412e3 2620 dwz_file->gdb_index.s.section = sectp;
2ec9a5e0
TT
2621 dwz_file->gdb_index.size = bfd_get_section_size (sectp);
2622 }
927aa2e7
JK
2623 else if (section_is_p (sectp->name, &dwarf2_elf_names.debug_names))
2624 {
2625 dwz_file->debug_names.s.section = sectp;
2626 dwz_file->debug_names.size = bfd_get_section_size (sectp);
2627 }
36586728
TT
2628}
2629
4db1a1dc
TT
2630/* Open the separate '.dwz' debug file, if needed. Return NULL if
2631 there is no .gnu_debugaltlink section in the file. Error if there
2632 is such a section but the file cannot be found. */
36586728
TT
2633
2634static struct dwz_file *
ed2dc618 2635dwarf2_get_dwz_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
36586728 2636{
36586728
TT
2637 const char *filename;
2638 struct dwz_file *result;
acd13123 2639 bfd_size_type buildid_len_arg;
dc294be5
TT
2640 size_t buildid_len;
2641 bfd_byte *buildid;
36586728
TT
2642
2643 if (dwarf2_per_objfile->dwz_file != NULL)
2644 return dwarf2_per_objfile->dwz_file;
2645
4db1a1dc 2646 bfd_set_error (bfd_error_no_error);
791afaa2
TT
2647 gdb::unique_xmalloc_ptr<char> data
2648 (bfd_get_alt_debug_link_info (dwarf2_per_objfile->objfile->obfd,
2649 &buildid_len_arg, &buildid));
4db1a1dc
TT
2650 if (data == NULL)
2651 {
2652 if (bfd_get_error () == bfd_error_no_error)
2653 return NULL;
2654 error (_("could not read '.gnu_debugaltlink' section: %s"),
2655 bfd_errmsg (bfd_get_error ()));
2656 }
791afaa2
TT
2657
2658 gdb::unique_xmalloc_ptr<bfd_byte> buildid_holder (buildid);
36586728 2659
acd13123
TT
2660 buildid_len = (size_t) buildid_len_arg;
2661
791afaa2 2662 filename = data.get ();
d721ba37
PA
2663
2664 std::string abs_storage;
36586728
TT
2665 if (!IS_ABSOLUTE_PATH (filename))
2666 {
14278e1f
TT
2667 gdb::unique_xmalloc_ptr<char> abs
2668 = gdb_realpath (objfile_name (dwarf2_per_objfile->objfile));
36586728 2669
14278e1f 2670 abs_storage = ldirname (abs.get ()) + SLASH_STRING + filename;
d721ba37 2671 filename = abs_storage.c_str ();
36586728
TT
2672 }
2673
dc294be5
TT
2674 /* First try the file name given in the section. If that doesn't
2675 work, try to use the build-id instead. */
192b62ce 2676 gdb_bfd_ref_ptr dwz_bfd (gdb_bfd_open (filename, gnutarget, -1));
dc294be5 2677 if (dwz_bfd != NULL)
36586728 2678 {
192b62ce
TT
2679 if (!build_id_verify (dwz_bfd.get (), buildid_len, buildid))
2680 dwz_bfd.release ();
36586728
TT
2681 }
2682
dc294be5
TT
2683 if (dwz_bfd == NULL)
2684 dwz_bfd = build_id_to_debug_bfd (buildid_len, buildid);
2685
2686 if (dwz_bfd == NULL)
2687 error (_("could not find '.gnu_debugaltlink' file for %s"),
2688 objfile_name (dwarf2_per_objfile->objfile));
2689
36586728
TT
2690 result = OBSTACK_ZALLOC (&dwarf2_per_objfile->objfile->objfile_obstack,
2691 struct dwz_file);
192b62ce 2692 result->dwz_bfd = dwz_bfd.release ();
36586728 2693
192b62ce 2694 bfd_map_over_sections (result->dwz_bfd, locate_dwz_sections, result);
36586728 2695
192b62ce 2696 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd, result->dwz_bfd);
8d2cc612 2697 dwarf2_per_objfile->dwz_file = result;
36586728
TT
2698 return result;
2699}
9291a0cd 2700\f
7b9f3c50
DE
2701/* DWARF quick_symbols_functions support. */
2702
2703/* TUs can share .debug_line entries, and there can be a lot more TUs than
2704 unique line tables, so we maintain a separate table of all .debug_line
2705 derived entries to support the sharing.
2706 All the quick functions need is the list of file names. We discard the
2707 line_header when we're done and don't need to record it here. */
2708struct quick_file_names
2709{
094b34ac
DE
2710 /* The data used to construct the hash key. */
2711 struct stmt_list_hash hash;
7b9f3c50
DE
2712
2713 /* The number of entries in file_names, real_names. */
2714 unsigned int num_file_names;
2715
2716 /* The file names from the line table, after being run through
2717 file_full_name. */
2718 const char **file_names;
2719
2720 /* The file names from the line table after being run through
2721 gdb_realpath. These are computed lazily. */
2722 const char **real_names;
2723};
2724
2725/* When using the index (and thus not using psymtabs), each CU has an
2726 object of this type. This is used to hold information needed by
2727 the various "quick" methods. */
2728struct dwarf2_per_cu_quick_data
2729{
2730 /* The file table. This can be NULL if there was no file table
2731 or it's currently not read in.
2732 NOTE: This points into dwarf2_per_objfile->quick_file_names_table. */
2733 struct quick_file_names *file_names;
2734
2735 /* The corresponding symbol table. This is NULL if symbols for this
2736 CU have not yet been read. */
43f3e411 2737 struct compunit_symtab *compunit_symtab;
7b9f3c50
DE
2738
2739 /* A temporary mark bit used when iterating over all CUs in
2740 expand_symtabs_matching. */
2741 unsigned int mark : 1;
2742
2743 /* True if we've tried to read the file table and found there isn't one.
2744 There will be no point in trying to read it again next time. */
2745 unsigned int no_file_data : 1;
2746};
2747
094b34ac
DE
2748/* Utility hash function for a stmt_list_hash. */
2749
2750static hashval_t
2751hash_stmt_list_entry (const struct stmt_list_hash *stmt_list_hash)
2752{
2753 hashval_t v = 0;
2754
2755 if (stmt_list_hash->dwo_unit != NULL)
2756 v += (uintptr_t) stmt_list_hash->dwo_unit->dwo_file;
9c541725 2757 v += to_underlying (stmt_list_hash->line_sect_off);
094b34ac
DE
2758 return v;
2759}
2760
2761/* Utility equality function for a stmt_list_hash. */
2762
2763static int
2764eq_stmt_list_entry (const struct stmt_list_hash *lhs,
2765 const struct stmt_list_hash *rhs)
2766{
2767 if ((lhs->dwo_unit != NULL) != (rhs->dwo_unit != NULL))
2768 return 0;
2769 if (lhs->dwo_unit != NULL
2770 && lhs->dwo_unit->dwo_file != rhs->dwo_unit->dwo_file)
2771 return 0;
2772
9c541725 2773 return lhs->line_sect_off == rhs->line_sect_off;
094b34ac
DE
2774}
2775
7b9f3c50
DE
2776/* Hash function for a quick_file_names. */
2777
2778static hashval_t
2779hash_file_name_entry (const void *e)
2780{
9a3c8263
SM
2781 const struct quick_file_names *file_data
2782 = (const struct quick_file_names *) e;
7b9f3c50 2783
094b34ac 2784 return hash_stmt_list_entry (&file_data->hash);
7b9f3c50
DE
2785}
2786
2787/* Equality function for a quick_file_names. */
2788
2789static int
2790eq_file_name_entry (const void *a, const void *b)
2791{
9a3c8263
SM
2792 const struct quick_file_names *ea = (const struct quick_file_names *) a;
2793 const struct quick_file_names *eb = (const struct quick_file_names *) b;
7b9f3c50 2794
094b34ac 2795 return eq_stmt_list_entry (&ea->hash, &eb->hash);
7b9f3c50
DE
2796}
2797
2798/* Delete function for a quick_file_names. */
2799
2800static void
2801delete_file_name_entry (void *e)
2802{
9a3c8263 2803 struct quick_file_names *file_data = (struct quick_file_names *) e;
7b9f3c50
DE
2804 int i;
2805
2806 for (i = 0; i < file_data->num_file_names; ++i)
2807 {
2808 xfree ((void*) file_data->file_names[i]);
2809 if (file_data->real_names)
2810 xfree ((void*) file_data->real_names[i]);
2811 }
2812
2813 /* The space for the struct itself lives on objfile_obstack,
2814 so we don't free it here. */
2815}
2816
2817/* Create a quick_file_names hash table. */
2818
2819static htab_t
2820create_quick_file_names_table (unsigned int nr_initial_entries)
2821{
2822 return htab_create_alloc (nr_initial_entries,
2823 hash_file_name_entry, eq_file_name_entry,
2824 delete_file_name_entry, xcalloc, xfree);
2825}
9291a0cd 2826
918dd910
JK
2827/* Read in PER_CU->CU. This function is unrelated to symtabs, symtab would
2828 have to be created afterwards. You should call age_cached_comp_units after
2829 processing PER_CU->CU. dw2_setup must have been already called. */
2830
2831static void
58f0c718 2832load_cu (struct dwarf2_per_cu_data *per_cu, bool skip_partial)
918dd910 2833{
3019eac3 2834 if (per_cu->is_debug_types)
e5fe5e75 2835 load_full_type_unit (per_cu);
918dd910 2836 else
58f0c718 2837 load_full_comp_unit (per_cu, skip_partial, language_minimal);
918dd910 2838
cc12ce38
DE
2839 if (per_cu->cu == NULL)
2840 return; /* Dummy CU. */
2dc860c0
DE
2841
2842 dwarf2_find_base_address (per_cu->cu->dies, per_cu->cu);
918dd910
JK
2843}
2844
a0f42c21 2845/* Read in the symbols for PER_CU. */
2fdf6df6 2846
9291a0cd 2847static void
58f0c718 2848dw2_do_instantiate_symtab (struct dwarf2_per_cu_data *per_cu, bool skip_partial)
9291a0cd 2849{
ed2dc618 2850 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
9291a0cd 2851
f4dc4d17
DE
2852 /* Skip type_unit_groups, reading the type units they contain
2853 is handled elsewhere. */
2854 if (IS_TYPE_UNIT_GROUP (per_cu))
2855 return;
2856
b303c6f6
AB
2857 /* The destructor of dwarf2_queue_guard frees any entries left on
2858 the queue. After this point we're guaranteed to leave this function
2859 with the dwarf queue empty. */
2860 dwarf2_queue_guard q_guard;
9291a0cd 2861
95554aad 2862 if (dwarf2_per_objfile->using_index
43f3e411 2863 ? per_cu->v.quick->compunit_symtab == NULL
95554aad
TT
2864 : (per_cu->v.psymtab == NULL || !per_cu->v.psymtab->readin))
2865 {
2866 queue_comp_unit (per_cu, language_minimal);
58f0c718 2867 load_cu (per_cu, skip_partial);
89e63ee4
DE
2868
2869 /* If we just loaded a CU from a DWO, and we're working with an index
2870 that may badly handle TUs, load all the TUs in that DWO as well.
2871 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
2872 if (!per_cu->is_debug_types
cc12ce38 2873 && per_cu->cu != NULL
89e63ee4
DE
2874 && per_cu->cu->dwo_unit != NULL
2875 && dwarf2_per_objfile->index_table != NULL
2876 && dwarf2_per_objfile->index_table->version <= 7
2877 /* DWP files aren't supported yet. */
ed2dc618 2878 && get_dwp_file (dwarf2_per_objfile) == NULL)
89e63ee4 2879 queue_and_load_all_dwo_tus (per_cu);
95554aad 2880 }
9291a0cd 2881
ed2dc618 2882 process_queue (dwarf2_per_objfile);
9291a0cd
TT
2883
2884 /* Age the cache, releasing compilation units that have not
2885 been used recently. */
ed2dc618 2886 age_cached_comp_units (dwarf2_per_objfile);
9291a0cd
TT
2887}
2888
2889/* Ensure that the symbols for PER_CU have been read in. OBJFILE is
2890 the objfile from which this CU came. Returns the resulting symbol
2891 table. */
2fdf6df6 2892
43f3e411 2893static struct compunit_symtab *
58f0c718 2894dw2_instantiate_symtab (struct dwarf2_per_cu_data *per_cu, bool skip_partial)
9291a0cd 2895{
ed2dc618
SM
2896 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
2897
95554aad 2898 gdb_assert (dwarf2_per_objfile->using_index);
43f3e411 2899 if (!per_cu->v.quick->compunit_symtab)
9291a0cd 2900 {
11ed8cad 2901 free_cached_comp_units freer (dwarf2_per_objfile);
c83dd867 2902 scoped_restore decrementer = increment_reading_symtab ();
58f0c718 2903 dw2_do_instantiate_symtab (per_cu, skip_partial);
ed2dc618 2904 process_cu_includes (dwarf2_per_objfile);
9291a0cd 2905 }
f194fefb 2906
43f3e411 2907 return per_cu->v.quick->compunit_symtab;
9291a0cd
TT
2908}
2909
ff4c9fec 2910/* See declaration. */
f4dc4d17 2911
ff4c9fec
SM
2912dwarf2_per_cu_data *
2913dwarf2_per_objfile::get_cutu (int index)
2914{
b76e467d 2915 if (index >= this->all_comp_units.size ())
ff4c9fec 2916 {
b76e467d 2917 index -= this->all_comp_units.size ();
b2bdb8cf 2918 gdb_assert (index < this->all_type_units.size ());
ff4c9fec
SM
2919 return &this->all_type_units[index]->per_cu;
2920 }
f4dc4d17 2921
ff4c9fec
SM
2922 return this->all_comp_units[index];
2923}
f4dc4d17 2924
ff4c9fec 2925/* See declaration. */
2fdf6df6 2926
ff4c9fec
SM
2927dwarf2_per_cu_data *
2928dwarf2_per_objfile::get_cu (int index)
1fd400ff 2929{
b76e467d 2930 gdb_assert (index >= 0 && index < this->all_comp_units.size ());
f4dc4d17 2931
ff4c9fec 2932 return this->all_comp_units[index];
f4dc4d17
DE
2933}
2934
ff4c9fec 2935/* See declaration. */
f4dc4d17 2936
ff4c9fec
SM
2937signatured_type *
2938dwarf2_per_objfile::get_tu (int index)
f4dc4d17 2939{
b2bdb8cf 2940 gdb_assert (index >= 0 && index < this->all_type_units.size ());
f4dc4d17 2941
ff4c9fec 2942 return this->all_type_units[index];
1fd400ff
TT
2943}
2944
4b514bc8
JK
2945/* Return a new dwarf2_per_cu_data allocated on OBJFILE's
2946 objfile_obstack, and constructed with the specified field
2947 values. */
2948
2949static dwarf2_per_cu_data *
ed2dc618 2950create_cu_from_index_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
4b514bc8
JK
2951 struct dwarf2_section_info *section,
2952 int is_dwz,
2953 sect_offset sect_off, ULONGEST length)
2954{
ed2dc618 2955 struct objfile *objfile = dwarf2_per_objfile->objfile;
4b514bc8
JK
2956 dwarf2_per_cu_data *the_cu
2957 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2958 struct dwarf2_per_cu_data);
2959 the_cu->sect_off = sect_off;
2960 the_cu->length = length;
e3b94546 2961 the_cu->dwarf2_per_objfile = dwarf2_per_objfile;
4b514bc8
JK
2962 the_cu->section = section;
2963 the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2964 struct dwarf2_per_cu_quick_data);
2965 the_cu->is_dwz = is_dwz;
2966 return the_cu;
2967}
2968
2ec9a5e0
TT
2969/* A helper for create_cus_from_index that handles a given list of
2970 CUs. */
2fdf6df6 2971
74a0d9f6 2972static void
12359b5e 2973create_cus_from_index_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
2ec9a5e0
TT
2974 const gdb_byte *cu_list, offset_type n_elements,
2975 struct dwarf2_section_info *section,
b76e467d 2976 int is_dwz)
9291a0cd 2977{
12359b5e 2978 for (offset_type i = 0; i < n_elements; i += 2)
9291a0cd 2979 {
74a0d9f6 2980 gdb_static_assert (sizeof (ULONGEST) >= 8);
9c541725
PA
2981
2982 sect_offset sect_off
2983 = (sect_offset) extract_unsigned_integer (cu_list, 8, BFD_ENDIAN_LITTLE);
2984 ULONGEST length = extract_unsigned_integer (cu_list + 8, 8, BFD_ENDIAN_LITTLE);
9291a0cd
TT
2985 cu_list += 2 * 8;
2986
b76e467d 2987 dwarf2_per_cu_data *per_cu
ed2dc618
SM
2988 = create_cu_from_index_list (dwarf2_per_objfile, section, is_dwz,
2989 sect_off, length);
b76e467d 2990 dwarf2_per_objfile->all_comp_units.push_back (per_cu);
9291a0cd 2991 }
9291a0cd
TT
2992}
2993
2ec9a5e0 2994/* Read the CU list from the mapped index, and use it to create all
74a0d9f6 2995 the CU objects for this objfile. */
2ec9a5e0 2996
74a0d9f6 2997static void
12359b5e 2998create_cus_from_index (struct dwarf2_per_objfile *dwarf2_per_objfile,
2ec9a5e0
TT
2999 const gdb_byte *cu_list, offset_type cu_list_elements,
3000 const gdb_byte *dwz_list, offset_type dwz_elements)
3001{
b76e467d
SM
3002 gdb_assert (dwarf2_per_objfile->all_comp_units.empty ());
3003 dwarf2_per_objfile->all_comp_units.reserve
3004 ((cu_list_elements + dwz_elements) / 2);
2ec9a5e0 3005
12359b5e 3006 create_cus_from_index_list (dwarf2_per_objfile, cu_list, cu_list_elements,
b76e467d 3007 &dwarf2_per_objfile->info, 0);
2ec9a5e0
TT
3008
3009 if (dwz_elements == 0)
74a0d9f6 3010 return;
2ec9a5e0 3011
12359b5e
SM
3012 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
3013 create_cus_from_index_list (dwarf2_per_objfile, dwz_list, dwz_elements,
b76e467d 3014 &dwz->info, 1);
2ec9a5e0
TT
3015}
3016
1fd400ff 3017/* Create the signatured type hash table from the index. */
673bfd45 3018
74a0d9f6 3019static void
12359b5e
SM
3020create_signatured_type_table_from_index
3021 (struct dwarf2_per_objfile *dwarf2_per_objfile,
3022 struct dwarf2_section_info *section,
3023 const gdb_byte *bytes,
3024 offset_type elements)
1fd400ff 3025{
12359b5e 3026 struct objfile *objfile = dwarf2_per_objfile->objfile;
1fd400ff 3027
b2bdb8cf
SM
3028 gdb_assert (dwarf2_per_objfile->all_type_units.empty ());
3029 dwarf2_per_objfile->all_type_units.reserve (elements / 3);
1fd400ff 3030
12359b5e 3031 htab_t sig_types_hash = allocate_signatured_type_table (objfile);
1fd400ff 3032
12359b5e 3033 for (offset_type i = 0; i < elements; i += 3)
1fd400ff 3034 {
52dc124a 3035 struct signatured_type *sig_type;
9c541725 3036 ULONGEST signature;
1fd400ff 3037 void **slot;
9c541725 3038 cu_offset type_offset_in_tu;
1fd400ff 3039
74a0d9f6 3040 gdb_static_assert (sizeof (ULONGEST) >= 8);
9c541725
PA
3041 sect_offset sect_off
3042 = (sect_offset) extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
3043 type_offset_in_tu
3044 = (cu_offset) extract_unsigned_integer (bytes + 8, 8,
3045 BFD_ENDIAN_LITTLE);
1fd400ff
TT
3046 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
3047 bytes += 3 * 8;
3048
52dc124a 3049 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1fd400ff 3050 struct signatured_type);
52dc124a 3051 sig_type->signature = signature;
9c541725 3052 sig_type->type_offset_in_tu = type_offset_in_tu;
3019eac3 3053 sig_type->per_cu.is_debug_types = 1;
8a0459fd 3054 sig_type->per_cu.section = section;
9c541725 3055 sig_type->per_cu.sect_off = sect_off;
e3b94546 3056 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
52dc124a 3057 sig_type->per_cu.v.quick
1fd400ff
TT
3058 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3059 struct dwarf2_per_cu_quick_data);
3060
52dc124a
DE
3061 slot = htab_find_slot (sig_types_hash, sig_type, INSERT);
3062 *slot = sig_type;
1fd400ff 3063
b2bdb8cf 3064 dwarf2_per_objfile->all_type_units.push_back (sig_type);
1fd400ff
TT
3065 }
3066
673bfd45 3067 dwarf2_per_objfile->signatured_types = sig_types_hash;
1fd400ff
TT
3068}
3069
927aa2e7
JK
3070/* Create the signatured type hash table from .debug_names. */
3071
3072static void
3073create_signatured_type_table_from_debug_names
ed2dc618 3074 (struct dwarf2_per_objfile *dwarf2_per_objfile,
927aa2e7
JK
3075 const mapped_debug_names &map,
3076 struct dwarf2_section_info *section,
3077 struct dwarf2_section_info *abbrev_section)
3078{
ed2dc618
SM
3079 struct objfile *objfile = dwarf2_per_objfile->objfile;
3080
927aa2e7
JK
3081 dwarf2_read_section (objfile, section);
3082 dwarf2_read_section (objfile, abbrev_section);
3083
b2bdb8cf
SM
3084 gdb_assert (dwarf2_per_objfile->all_type_units.empty ());
3085 dwarf2_per_objfile->all_type_units.reserve (map.tu_count);
927aa2e7
JK
3086
3087 htab_t sig_types_hash = allocate_signatured_type_table (objfile);
3088
3089 for (uint32_t i = 0; i < map.tu_count; ++i)
3090 {
3091 struct signatured_type *sig_type;
927aa2e7 3092 void **slot;
927aa2e7
JK
3093
3094 sect_offset sect_off
3095 = (sect_offset) (extract_unsigned_integer
3096 (map.tu_table_reordered + i * map.offset_size,
3097 map.offset_size,
3098 map.dwarf5_byte_order));
3099
3100 comp_unit_head cu_header;
ed2dc618
SM
3101 read_and_check_comp_unit_head (dwarf2_per_objfile, &cu_header, section,
3102 abbrev_section,
927aa2e7
JK
3103 section->buffer + to_underlying (sect_off),
3104 rcuh_kind::TYPE);
3105
3106 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3107 struct signatured_type);
3108 sig_type->signature = cu_header.signature;
3109 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
3110 sig_type->per_cu.is_debug_types = 1;
3111 sig_type->per_cu.section = section;
3112 sig_type->per_cu.sect_off = sect_off;
e3b94546 3113 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
927aa2e7
JK
3114 sig_type->per_cu.v.quick
3115 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3116 struct dwarf2_per_cu_quick_data);
3117
3118 slot = htab_find_slot (sig_types_hash, sig_type, INSERT);
3119 *slot = sig_type;
3120
b2bdb8cf 3121 dwarf2_per_objfile->all_type_units.push_back (sig_type);
927aa2e7
JK
3122 }
3123
3124 dwarf2_per_objfile->signatured_types = sig_types_hash;
3125}
3126
9291a0cd
TT
3127/* Read the address map data from the mapped index, and use it to
3128 populate the objfile's psymtabs_addrmap. */
2fdf6df6 3129
9291a0cd 3130static void
ed2dc618
SM
3131create_addrmap_from_index (struct dwarf2_per_objfile *dwarf2_per_objfile,
3132 struct mapped_index *index)
9291a0cd 3133{
ed2dc618 3134 struct objfile *objfile = dwarf2_per_objfile->objfile;
3e29f34a 3135 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9291a0cd 3136 const gdb_byte *iter, *end;
9291a0cd 3137 struct addrmap *mutable_map;
9291a0cd
TT
3138 CORE_ADDR baseaddr;
3139
8268c778
PA
3140 auto_obstack temp_obstack;
3141
9291a0cd
TT
3142 mutable_map = addrmap_create_mutable (&temp_obstack);
3143
f00a2de2
PA
3144 iter = index->address_table.data ();
3145 end = iter + index->address_table.size ();
9291a0cd
TT
3146
3147 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
3148
3149 while (iter < end)
3150 {
3151 ULONGEST hi, lo, cu_index;
3152 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
3153 iter += 8;
3154 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
3155 iter += 8;
3156 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
3157 iter += 4;
f652bce2 3158
24a55014 3159 if (lo > hi)
f652bce2 3160 {
24a55014
DE
3161 complaint (&symfile_complaints,
3162 _(".gdb_index address table has invalid range (%s - %s)"),
c0cd8254 3163 hex_string (lo), hex_string (hi));
24a55014 3164 continue;
f652bce2 3165 }
24a55014 3166
b76e467d 3167 if (cu_index >= dwarf2_per_objfile->all_comp_units.size ())
f652bce2
DE
3168 {
3169 complaint (&symfile_complaints,
3170 _(".gdb_index address table has invalid CU number %u"),
3171 (unsigned) cu_index);
24a55014 3172 continue;
f652bce2 3173 }
24a55014 3174
3e29f34a
MR
3175 lo = gdbarch_adjust_dwarf2_addr (gdbarch, lo + baseaddr);
3176 hi = gdbarch_adjust_dwarf2_addr (gdbarch, hi + baseaddr);
ed2dc618 3177 addrmap_set_empty (mutable_map, lo, hi - 1,
ff4c9fec 3178 dwarf2_per_objfile->get_cu (cu_index));
9291a0cd
TT
3179 }
3180
3181 objfile->psymtabs_addrmap = addrmap_create_fixed (mutable_map,
3182 &objfile->objfile_obstack);
9291a0cd
TT
3183}
3184
927aa2e7
JK
3185/* Read the address map data from DWARF-5 .debug_aranges, and use it to
3186 populate the objfile's psymtabs_addrmap. */
3187
3188static void
ed2dc618 3189create_addrmap_from_aranges (struct dwarf2_per_objfile *dwarf2_per_objfile,
927aa2e7
JK
3190 struct dwarf2_section_info *section)
3191{
ed2dc618 3192 struct objfile *objfile = dwarf2_per_objfile->objfile;
927aa2e7
JK
3193 bfd *abfd = objfile->obfd;
3194 struct gdbarch *gdbarch = get_objfile_arch (objfile);
3195 const CORE_ADDR baseaddr = ANOFFSET (objfile->section_offsets,
3196 SECT_OFF_TEXT (objfile));
3197
3198 auto_obstack temp_obstack;
3199 addrmap *mutable_map = addrmap_create_mutable (&temp_obstack);
3200
3201 std::unordered_map<sect_offset,
3202 dwarf2_per_cu_data *,
3203 gdb::hash_enum<sect_offset>>
3204 debug_info_offset_to_per_cu;
b76e467d 3205 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
927aa2e7 3206 {
927aa2e7
JK
3207 const auto insertpair
3208 = debug_info_offset_to_per_cu.emplace (per_cu->sect_off, per_cu);
3209 if (!insertpair.second)
3210 {
3211 warning (_("Section .debug_aranges in %s has duplicate "
9d8780f0
SM
3212 "debug_info_offset %s, ignoring .debug_aranges."),
3213 objfile_name (objfile), sect_offset_str (per_cu->sect_off));
927aa2e7
JK
3214 return;
3215 }
3216 }
3217
3218 dwarf2_read_section (objfile, section);
3219
3220 const bfd_endian dwarf5_byte_order = gdbarch_byte_order (gdbarch);
3221
3222 const gdb_byte *addr = section->buffer;
3223
3224 while (addr < section->buffer + section->size)
3225 {
3226 const gdb_byte *const entry_addr = addr;
3227 unsigned int bytes_read;
3228
3229 const LONGEST entry_length = read_initial_length (abfd, addr,
3230 &bytes_read);
3231 addr += bytes_read;
3232
3233 const gdb_byte *const entry_end = addr + entry_length;
3234 const bool dwarf5_is_dwarf64 = bytes_read != 4;
3235 const uint8_t offset_size = dwarf5_is_dwarf64 ? 8 : 4;
3236 if (addr + entry_length > section->buffer + section->size)
3237 {
3238 warning (_("Section .debug_aranges in %s entry at offset %zu "
3239 "length %s exceeds section length %s, "
3240 "ignoring .debug_aranges."),
3241 objfile_name (objfile), entry_addr - section->buffer,
3242 plongest (bytes_read + entry_length),
3243 pulongest (section->size));
3244 return;
3245 }
3246
3247 /* The version number. */
3248 const uint16_t version = read_2_bytes (abfd, addr);
3249 addr += 2;
3250 if (version != 2)
3251 {
3252 warning (_("Section .debug_aranges in %s entry at offset %zu "
3253 "has unsupported version %d, ignoring .debug_aranges."),
3254 objfile_name (objfile), entry_addr - section->buffer,
3255 version);
3256 return;
3257 }
3258
3259 const uint64_t debug_info_offset
3260 = extract_unsigned_integer (addr, offset_size, dwarf5_byte_order);
3261 addr += offset_size;
3262 const auto per_cu_it
3263 = debug_info_offset_to_per_cu.find (sect_offset (debug_info_offset));
3264 if (per_cu_it == debug_info_offset_to_per_cu.cend ())
3265 {
3266 warning (_("Section .debug_aranges in %s entry at offset %zu "
3267 "debug_info_offset %s does not exists, "
3268 "ignoring .debug_aranges."),
3269 objfile_name (objfile), entry_addr - section->buffer,
3270 pulongest (debug_info_offset));
3271 return;
3272 }
3273 dwarf2_per_cu_data *const per_cu = per_cu_it->second;
3274
3275 const uint8_t address_size = *addr++;
3276 if (address_size < 1 || address_size > 8)
3277 {
3278 warning (_("Section .debug_aranges in %s entry at offset %zu "
3279 "address_size %u is invalid, ignoring .debug_aranges."),
3280 objfile_name (objfile), entry_addr - section->buffer,
3281 address_size);
3282 return;
3283 }
3284
3285 const uint8_t segment_selector_size = *addr++;
3286 if (segment_selector_size != 0)
3287 {
3288 warning (_("Section .debug_aranges in %s entry at offset %zu "
3289 "segment_selector_size %u is not supported, "
3290 "ignoring .debug_aranges."),
3291 objfile_name (objfile), entry_addr - section->buffer,
3292 segment_selector_size);
3293 return;
3294 }
3295
3296 /* Must pad to an alignment boundary that is twice the address
3297 size. It is undocumented by the DWARF standard but GCC does
3298 use it. */
3299 for (size_t padding = ((-(addr - section->buffer))
3300 & (2 * address_size - 1));
3301 padding > 0; padding--)
3302 if (*addr++ != 0)
3303 {
3304 warning (_("Section .debug_aranges in %s entry at offset %zu "
3305 "padding is not zero, ignoring .debug_aranges."),
3306 objfile_name (objfile), entry_addr - section->buffer);
3307 return;
3308 }
3309
3310 for (;;)
3311 {
3312 if (addr + 2 * address_size > entry_end)
3313 {
3314 warning (_("Section .debug_aranges in %s entry at offset %zu "
3315 "address list is not properly terminated, "
3316 "ignoring .debug_aranges."),
3317 objfile_name (objfile), entry_addr - section->buffer);
3318 return;
3319 }
3320 ULONGEST start = extract_unsigned_integer (addr, address_size,
3321 dwarf5_byte_order);
3322 addr += address_size;
3323 ULONGEST length = extract_unsigned_integer (addr, address_size,
3324 dwarf5_byte_order);
3325 addr += address_size;
3326 if (start == 0 && length == 0)
3327 break;
3328 if (start == 0 && !dwarf2_per_objfile->has_section_at_zero)
3329 {
3330 /* Symbol was eliminated due to a COMDAT group. */
3331 continue;
3332 }
3333 ULONGEST end = start + length;
3334 start = gdbarch_adjust_dwarf2_addr (gdbarch, start + baseaddr);
3335 end = gdbarch_adjust_dwarf2_addr (gdbarch, end + baseaddr);
3336 addrmap_set_empty (mutable_map, start, end - 1, per_cu);
3337 }
3338 }
3339
3340 objfile->psymtabs_addrmap = addrmap_create_fixed (mutable_map,
3341 &objfile->objfile_obstack);
3342}
3343
9291a0cd
TT
3344/* Find a slot in the mapped index INDEX for the object named NAME.
3345 If NAME is found, set *VEC_OUT to point to the CU vector in the
109483d9
PA
3346 constant pool and return true. If NAME cannot be found, return
3347 false. */
2fdf6df6 3348
109483d9 3349static bool
9291a0cd
TT
3350find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
3351 offset_type **vec_out)
3352{
0cf03b49 3353 offset_type hash;
9291a0cd 3354 offset_type slot, step;
559a7a62 3355 int (*cmp) (const char *, const char *);
9291a0cd 3356
791afaa2 3357 gdb::unique_xmalloc_ptr<char> without_params;
0cf03b49 3358 if (current_language->la_language == language_cplus
45280282
IB
3359 || current_language->la_language == language_fortran
3360 || current_language->la_language == language_d)
0cf03b49
JK
3361 {
3362 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
3363 not contain any. */
a8719064 3364
72998fb3 3365 if (strchr (name, '(') != NULL)
0cf03b49 3366 {
109483d9 3367 without_params = cp_remove_params (name);
0cf03b49 3368
72998fb3 3369 if (without_params != NULL)
791afaa2 3370 name = without_params.get ();
0cf03b49
JK
3371 }
3372 }
3373
559a7a62 3374 /* Index version 4 did not support case insensitive searches. But the
feea76c2 3375 indices for case insensitive languages are built in lowercase, therefore
559a7a62
JK
3376 simulate our NAME being searched is also lowercased. */
3377 hash = mapped_index_string_hash ((index->version == 4
3378 && case_sensitivity == case_sensitive_off
3379 ? 5 : index->version),
3380 name);
3381
f00a2de2
PA
3382 slot = hash & (index->symbol_table.size () - 1);
3383 step = ((hash * 17) & (index->symbol_table.size () - 1)) | 1;
559a7a62 3384 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
9291a0cd
TT
3385
3386 for (;;)
3387 {
9291a0cd 3388 const char *str;
f00a2de2
PA
3389
3390 const auto &bucket = index->symbol_table[slot];
3391 if (bucket.name == 0 && bucket.vec == 0)
109483d9 3392 return false;
9291a0cd 3393
f00a2de2 3394 str = index->constant_pool + MAYBE_SWAP (bucket.name);
559a7a62 3395 if (!cmp (name, str))
9291a0cd
TT
3396 {
3397 *vec_out = (offset_type *) (index->constant_pool
f00a2de2 3398 + MAYBE_SWAP (bucket.vec));
109483d9 3399 return true;
9291a0cd
TT
3400 }
3401
f00a2de2 3402 slot = (slot + step) & (index->symbol_table.size () - 1);
9291a0cd
TT
3403 }
3404}
3405
2ec9a5e0
TT
3406/* A helper function that reads the .gdb_index from SECTION and fills
3407 in MAP. FILENAME is the name of the file containing the section;
d33bc52e 3408 it is used for error reporting. DEPRECATED_OK is true if it is
2ec9a5e0
TT
3409 ok to use deprecated sections.
3410
3411 CU_LIST, CU_LIST_ELEMENTS, TYPES_LIST, and TYPES_LIST_ELEMENTS are
3412 out parameters that are filled in with information about the CU and
3413 TU lists in the section.
3414
3415 Returns 1 if all went well, 0 otherwise. */
2fdf6df6 3416
d33bc52e 3417static bool
2ec9a5e0
TT
3418read_index_from_section (struct objfile *objfile,
3419 const char *filename,
d33bc52e 3420 bool deprecated_ok,
2ec9a5e0
TT
3421 struct dwarf2_section_info *section,
3422 struct mapped_index *map,
3423 const gdb_byte **cu_list,
3424 offset_type *cu_list_elements,
3425 const gdb_byte **types_list,
3426 offset_type *types_list_elements)
9291a0cd 3427{
948f8e3d 3428 const gdb_byte *addr;
2ec9a5e0 3429 offset_type version;
b3b272e1 3430 offset_type *metadata;
1fd400ff 3431 int i;
9291a0cd 3432
2ec9a5e0 3433 if (dwarf2_section_empty_p (section))
9291a0cd 3434 return 0;
82430852
JK
3435
3436 /* Older elfutils strip versions could keep the section in the main
3437 executable while splitting it for the separate debug info file. */
a32a8923 3438 if ((get_section_flags (section) & SEC_HAS_CONTENTS) == 0)
82430852
JK
3439 return 0;
3440
2ec9a5e0 3441 dwarf2_read_section (objfile, section);
9291a0cd 3442
2ec9a5e0 3443 addr = section->buffer;
9291a0cd 3444 /* Version check. */
1fd400ff 3445 version = MAYBE_SWAP (*(offset_type *) addr);
987d643c 3446 /* Versions earlier than 3 emitted every copy of a psymbol. This
a6e293d1 3447 causes the index to behave very poorly for certain requests. Version 3
831adc1f 3448 contained incomplete addrmap. So, it seems better to just ignore such
481860b3 3449 indices. */
831adc1f 3450 if (version < 4)
481860b3
GB
3451 {
3452 static int warning_printed = 0;
3453 if (!warning_printed)
3454 {
3455 warning (_("Skipping obsolete .gdb_index section in %s."),
2ec9a5e0 3456 filename);
481860b3
GB
3457 warning_printed = 1;
3458 }
3459 return 0;
3460 }
3461 /* Index version 4 uses a different hash function than index version
3462 5 and later.
3463
3464 Versions earlier than 6 did not emit psymbols for inlined
3465 functions. Using these files will cause GDB not to be able to
3466 set breakpoints on inlined functions by name, so we ignore these
e615022a
DE
3467 indices unless the user has done
3468 "set use-deprecated-index-sections on". */
2ec9a5e0 3469 if (version < 6 && !deprecated_ok)
481860b3
GB
3470 {
3471 static int warning_printed = 0;
3472 if (!warning_printed)
3473 {
e615022a
DE
3474 warning (_("\
3475Skipping deprecated .gdb_index section in %s.\n\
3476Do \"set use-deprecated-index-sections on\" before the file is read\n\
3477to use the section anyway."),
2ec9a5e0 3478 filename);
481860b3
GB
3479 warning_printed = 1;
3480 }
3481 return 0;
3482 }
796a7ff8 3483 /* Version 7 indices generated by gold refer to the CU for a symbol instead
8943b874
DE
3484 of the TU (for symbols coming from TUs),
3485 http://sourceware.org/bugzilla/show_bug.cgi?id=15021.
3486 Plus gold-generated indices can have duplicate entries for global symbols,
3487 http://sourceware.org/bugzilla/show_bug.cgi?id=15646.
3488 These are just performance bugs, and we can't distinguish gdb-generated
3489 indices from gold-generated ones, so issue no warning here. */
796a7ff8 3490
481860b3 3491 /* Indexes with higher version than the one supported by GDB may be no
594e8718 3492 longer backward compatible. */
796a7ff8 3493 if (version > 8)
594e8718 3494 return 0;
9291a0cd 3495
559a7a62 3496 map->version = version;
9291a0cd
TT
3497
3498 metadata = (offset_type *) (addr + sizeof (offset_type));
1fd400ff
TT
3499
3500 i = 0;
2ec9a5e0
TT
3501 *cu_list = addr + MAYBE_SWAP (metadata[i]);
3502 *cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
3503 / 8);
1fd400ff
TT
3504 ++i;
3505
2ec9a5e0
TT
3506 *types_list = addr + MAYBE_SWAP (metadata[i]);
3507 *types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
3508 - MAYBE_SWAP (metadata[i]))
3509 / 8);
987d643c 3510 ++i;
1fd400ff 3511
f00a2de2
PA
3512 const gdb_byte *address_table = addr + MAYBE_SWAP (metadata[i]);
3513 const gdb_byte *address_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
3514 map->address_table
3515 = gdb::array_view<const gdb_byte> (address_table, address_table_end);
1fd400ff
TT
3516 ++i;
3517
f00a2de2
PA
3518 const gdb_byte *symbol_table = addr + MAYBE_SWAP (metadata[i]);
3519 const gdb_byte *symbol_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
3520 map->symbol_table
3521 = gdb::array_view<mapped_index::symbol_table_slot>
3522 ((mapped_index::symbol_table_slot *) symbol_table,
3523 (mapped_index::symbol_table_slot *) symbol_table_end);
9291a0cd 3524
f00a2de2 3525 ++i;
f9d83a0b 3526 map->constant_pool = (char *) (addr + MAYBE_SWAP (metadata[i]));
1fd400ff 3527
2ec9a5e0
TT
3528 return 1;
3529}
3530
927aa2e7 3531/* Read .gdb_index. If everything went ok, initialize the "quick"
2ec9a5e0
TT
3532 elements of all the CUs and return 1. Otherwise, return 0. */
3533
3534static int
12359b5e 3535dwarf2_read_index (struct dwarf2_per_objfile *dwarf2_per_objfile)
2ec9a5e0 3536{
2ec9a5e0
TT
3537 const gdb_byte *cu_list, *types_list, *dwz_list = NULL;
3538 offset_type cu_list_elements, types_list_elements, dwz_list_elements = 0;
4db1a1dc 3539 struct dwz_file *dwz;
12359b5e 3540 struct objfile *objfile = dwarf2_per_objfile->objfile;
2ec9a5e0 3541
3063847f 3542 std::unique_ptr<struct mapped_index> map (new struct mapped_index);
4262abfb 3543 if (!read_index_from_section (objfile, objfile_name (objfile),
2ec9a5e0 3544 use_deprecated_index_sections,
3063847f 3545 &dwarf2_per_objfile->gdb_index, map.get (),
2ec9a5e0
TT
3546 &cu_list, &cu_list_elements,
3547 &types_list, &types_list_elements))
3548 return 0;
3549
0fefef59 3550 /* Don't use the index if it's empty. */
3063847f 3551 if (map->symbol_table.empty ())
0fefef59
DE
3552 return 0;
3553
2ec9a5e0
TT
3554 /* If there is a .dwz file, read it so we can get its CU list as
3555 well. */
ed2dc618 3556 dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
4db1a1dc 3557 if (dwz != NULL)
2ec9a5e0 3558 {
2ec9a5e0
TT
3559 struct mapped_index dwz_map;
3560 const gdb_byte *dwz_types_ignore;
3561 offset_type dwz_types_elements_ignore;
3562
3563 if (!read_index_from_section (objfile, bfd_get_filename (dwz->dwz_bfd),
3564 1,
3565 &dwz->gdb_index, &dwz_map,
3566 &dwz_list, &dwz_list_elements,
3567 &dwz_types_ignore,
3568 &dwz_types_elements_ignore))
3569 {
3570 warning (_("could not read '.gdb_index' section from %s; skipping"),
3571 bfd_get_filename (dwz->dwz_bfd));
3572 return 0;
3573 }
3574 }
3575
12359b5e
SM
3576 create_cus_from_index (dwarf2_per_objfile, cu_list, cu_list_elements,
3577 dwz_list, dwz_list_elements);
1fd400ff 3578
8b70b953
TT
3579 if (types_list_elements)
3580 {
3581 struct dwarf2_section_info *section;
3582
3583 /* We can only handle a single .debug_types when we have an
3584 index. */
3585 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) != 1)
3586 return 0;
3587
3588 section = VEC_index (dwarf2_section_info_def,
3589 dwarf2_per_objfile->types, 0);
3590
12359b5e
SM
3591 create_signatured_type_table_from_index (dwarf2_per_objfile, section,
3592 types_list, types_list_elements);
8b70b953 3593 }
9291a0cd 3594
3063847f 3595 create_addrmap_from_index (dwarf2_per_objfile, map.get ());
9291a0cd 3596
3063847f 3597 dwarf2_per_objfile->index_table = std::move (map);
9291a0cd 3598 dwarf2_per_objfile->using_index = 1;
7b9f3c50 3599 dwarf2_per_objfile->quick_file_names_table =
b76e467d 3600 create_quick_file_names_table (dwarf2_per_objfile->all_comp_units.size ());
9291a0cd
TT
3601
3602 return 1;
3603}
3604
dee91e82 3605/* die_reader_func for dw2_get_file_names. */
2fdf6df6 3606
dee91e82
DE
3607static void
3608dw2_get_file_names_reader (const struct die_reader_specs *reader,
d521ce57 3609 const gdb_byte *info_ptr,
dee91e82
DE
3610 struct die_info *comp_unit_die,
3611 int has_children,
3612 void *data)
9291a0cd 3613{
dee91e82 3614 struct dwarf2_cu *cu = reader->cu;
ed2dc618 3615 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
518817b3
SM
3616 struct dwarf2_per_objfile *dwarf2_per_objfile
3617 = cu->per_cu->dwarf2_per_objfile;
dee91e82 3618 struct objfile *objfile = dwarf2_per_objfile->objfile;
094b34ac 3619 struct dwarf2_per_cu_data *lh_cu;
9291a0cd 3620 struct attribute *attr;
dee91e82 3621 int i;
7b9f3c50
DE
3622 void **slot;
3623 struct quick_file_names *qfn;
9291a0cd 3624
0186c6a7
DE
3625 gdb_assert (! this_cu->is_debug_types);
3626
07261596
TT
3627 /* Our callers never want to match partial units -- instead they
3628 will match the enclosing full CU. */
3629 if (comp_unit_die->tag == DW_TAG_partial_unit)
3630 {
3631 this_cu->v.quick->no_file_data = 1;
3632 return;
3633 }
3634
0186c6a7 3635 lh_cu = this_cu;
7b9f3c50 3636 slot = NULL;
dee91e82 3637
fff8551c 3638 line_header_up lh;
9c541725 3639 sect_offset line_offset {};
fff8551c 3640
dee91e82 3641 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
9291a0cd
TT
3642 if (attr)
3643 {
7b9f3c50
DE
3644 struct quick_file_names find_entry;
3645
9c541725 3646 line_offset = (sect_offset) DW_UNSND (attr);
7b9f3c50
DE
3647
3648 /* We may have already read in this line header (TU line header sharing).
3649 If we have we're done. */
094b34ac 3650 find_entry.hash.dwo_unit = cu->dwo_unit;
9c541725 3651 find_entry.hash.line_sect_off = line_offset;
7b9f3c50
DE
3652 slot = htab_find_slot (dwarf2_per_objfile->quick_file_names_table,
3653 &find_entry, INSERT);
3654 if (*slot != NULL)
3655 {
9a3c8263 3656 lh_cu->v.quick->file_names = (struct quick_file_names *) *slot;
dee91e82 3657 return;
7b9f3c50
DE
3658 }
3659
3019eac3 3660 lh = dwarf_decode_line_header (line_offset, cu);
9291a0cd
TT
3661 }
3662 if (lh == NULL)
3663 {
094b34ac 3664 lh_cu->v.quick->no_file_data = 1;
dee91e82 3665 return;
9291a0cd
TT
3666 }
3667
8d749320 3668 qfn = XOBNEW (&objfile->objfile_obstack, struct quick_file_names);
094b34ac 3669 qfn->hash.dwo_unit = cu->dwo_unit;
9c541725 3670 qfn->hash.line_sect_off = line_offset;
7b9f3c50
DE
3671 gdb_assert (slot != NULL);
3672 *slot = qfn;
9291a0cd 3673
d721ba37 3674 file_and_directory fnd = find_file_and_directory (comp_unit_die, cu);
9291a0cd 3675
fff8551c 3676 qfn->num_file_names = lh->file_names.size ();
8d749320 3677 qfn->file_names =
fff8551c
PA
3678 XOBNEWVEC (&objfile->objfile_obstack, const char *, lh->file_names.size ());
3679 for (i = 0; i < lh->file_names.size (); ++i)
3680 qfn->file_names[i] = file_full_name (i + 1, lh.get (), fnd.comp_dir);
7b9f3c50 3681 qfn->real_names = NULL;
9291a0cd 3682
094b34ac 3683 lh_cu->v.quick->file_names = qfn;
dee91e82
DE
3684}
3685
3686/* A helper for the "quick" functions which attempts to read the line
3687 table for THIS_CU. */
3688
3689static struct quick_file_names *
e4a48d9d 3690dw2_get_file_names (struct dwarf2_per_cu_data *this_cu)
dee91e82 3691{
0186c6a7
DE
3692 /* This should never be called for TUs. */
3693 gdb_assert (! this_cu->is_debug_types);
3694 /* Nor type unit groups. */
3695 gdb_assert (! IS_TYPE_UNIT_GROUP (this_cu));
f4dc4d17 3696
dee91e82
DE
3697 if (this_cu->v.quick->file_names != NULL)
3698 return this_cu->v.quick->file_names;
3699 /* If we know there is no line data, no point in looking again. */
3700 if (this_cu->v.quick->no_file_data)
3701 return NULL;
3702
0186c6a7 3703 init_cutu_and_read_dies_simple (this_cu, dw2_get_file_names_reader, NULL);
dee91e82
DE
3704
3705 if (this_cu->v.quick->no_file_data)
3706 return NULL;
3707 return this_cu->v.quick->file_names;
9291a0cd
TT
3708}
3709
3710/* A helper for the "quick" functions which computes and caches the
7b9f3c50 3711 real path for a given file name from the line table. */
2fdf6df6 3712
9291a0cd 3713static const char *
7b9f3c50
DE
3714dw2_get_real_path (struct objfile *objfile,
3715 struct quick_file_names *qfn, int index)
9291a0cd 3716{
7b9f3c50
DE
3717 if (qfn->real_names == NULL)
3718 qfn->real_names = OBSTACK_CALLOC (&objfile->objfile_obstack,
26f2dc30 3719 qfn->num_file_names, const char *);
9291a0cd 3720
7b9f3c50 3721 if (qfn->real_names[index] == NULL)
14278e1f 3722 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]).release ();
9291a0cd 3723
7b9f3c50 3724 return qfn->real_names[index];
9291a0cd
TT
3725}
3726
3727static struct symtab *
3728dw2_find_last_source_symtab (struct objfile *objfile)
3729{
ed2dc618
SM
3730 struct dwarf2_per_objfile *dwarf2_per_objfile
3731 = get_dwarf2_per_objfile (objfile);
b76e467d 3732 dwarf2_per_cu_data *dwarf_cu = dwarf2_per_objfile->all_comp_units.back ();
58f0c718 3733 compunit_symtab *cust = dw2_instantiate_symtab (dwarf_cu, false);
ae2de4f8 3734
43f3e411
DE
3735 if (cust == NULL)
3736 return NULL;
ed2dc618 3737
43f3e411 3738 return compunit_primary_filetab (cust);
9291a0cd
TT
3739}
3740
7b9f3c50
DE
3741/* Traversal function for dw2_forget_cached_source_info. */
3742
3743static int
3744dw2_free_cached_file_names (void **slot, void *info)
9291a0cd 3745{
7b9f3c50 3746 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
9291a0cd 3747
7b9f3c50 3748 if (file_data->real_names)
9291a0cd 3749 {
7b9f3c50 3750 int i;
9291a0cd 3751
7b9f3c50 3752 for (i = 0; i < file_data->num_file_names; ++i)
9291a0cd 3753 {
7b9f3c50
DE
3754 xfree ((void*) file_data->real_names[i]);
3755 file_data->real_names[i] = NULL;
9291a0cd
TT
3756 }
3757 }
7b9f3c50
DE
3758
3759 return 1;
3760}
3761
3762static void
3763dw2_forget_cached_source_info (struct objfile *objfile)
3764{
ed2dc618
SM
3765 struct dwarf2_per_objfile *dwarf2_per_objfile
3766 = get_dwarf2_per_objfile (objfile);
7b9f3c50
DE
3767
3768 htab_traverse_noresize (dwarf2_per_objfile->quick_file_names_table,
3769 dw2_free_cached_file_names, NULL);
9291a0cd
TT
3770}
3771
f8eba3c6
TT
3772/* Helper function for dw2_map_symtabs_matching_filename that expands
3773 the symtabs and calls the iterator. */
3774
3775static int
3776dw2_map_expand_apply (struct objfile *objfile,
3777 struct dwarf2_per_cu_data *per_cu,
f5b95b50 3778 const char *name, const char *real_path,
14bc53a8 3779 gdb::function_view<bool (symtab *)> callback)
f8eba3c6 3780{
43f3e411 3781 struct compunit_symtab *last_made = objfile->compunit_symtabs;
f8eba3c6
TT
3782
3783 /* Don't visit already-expanded CUs. */
43f3e411 3784 if (per_cu->v.quick->compunit_symtab)
f8eba3c6
TT
3785 return 0;
3786
3787 /* This may expand more than one symtab, and we want to iterate over
3788 all of them. */
58f0c718 3789 dw2_instantiate_symtab (per_cu, false);
f8eba3c6 3790
14bc53a8
PA
3791 return iterate_over_some_symtabs (name, real_path, objfile->compunit_symtabs,
3792 last_made, callback);
f8eba3c6
TT
3793}
3794
3795/* Implementation of the map_symtabs_matching_filename method. */
3796
14bc53a8
PA
3797static bool
3798dw2_map_symtabs_matching_filename
3799 (struct objfile *objfile, const char *name, const char *real_path,
3800 gdb::function_view<bool (symtab *)> callback)
9291a0cd 3801{
c011a4f4 3802 const char *name_basename = lbasename (name);
ed2dc618
SM
3803 struct dwarf2_per_objfile *dwarf2_per_objfile
3804 = get_dwarf2_per_objfile (objfile);
ae2de4f8 3805
848e3e78
DE
3806 /* The rule is CUs specify all the files, including those used by
3807 any TU, so there's no need to scan TUs here. */
f4dc4d17 3808
b76e467d 3809 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
9291a0cd 3810 {
3d7bb9d9 3811 /* We only need to look at symtabs not already expanded. */
43f3e411 3812 if (per_cu->v.quick->compunit_symtab)
9291a0cd
TT
3813 continue;
3814
b76e467d 3815 quick_file_names *file_data = dw2_get_file_names (per_cu);
7b9f3c50 3816 if (file_data == NULL)
9291a0cd
TT
3817 continue;
3818
b76e467d 3819 for (int j = 0; j < file_data->num_file_names; ++j)
9291a0cd 3820 {
7b9f3c50 3821 const char *this_name = file_data->file_names[j];
da235a7c 3822 const char *this_real_name;
9291a0cd 3823
af529f8f 3824 if (compare_filenames_for_search (this_name, name))
9291a0cd 3825 {
f5b95b50 3826 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
14bc53a8
PA
3827 callback))
3828 return true;
288e77a7 3829 continue;
4aac40c8 3830 }
9291a0cd 3831
c011a4f4
DE
3832 /* Before we invoke realpath, which can get expensive when many
3833 files are involved, do a quick comparison of the basenames. */
3834 if (! basenames_may_differ
3835 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
3836 continue;
3837
da235a7c
JK
3838 this_real_name = dw2_get_real_path (objfile, file_data, j);
3839 if (compare_filenames_for_search (this_real_name, name))
9291a0cd 3840 {
da235a7c 3841 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
14bc53a8
PA
3842 callback))
3843 return true;
288e77a7 3844 continue;
da235a7c 3845 }
9291a0cd 3846
da235a7c
JK
3847 if (real_path != NULL)
3848 {
af529f8f
JK
3849 gdb_assert (IS_ABSOLUTE_PATH (real_path));
3850 gdb_assert (IS_ABSOLUTE_PATH (name));
7b9f3c50 3851 if (this_real_name != NULL
af529f8f 3852 && FILENAME_CMP (real_path, this_real_name) == 0)
9291a0cd 3853 {
f5b95b50 3854 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
14bc53a8
PA
3855 callback))
3856 return true;
288e77a7 3857 continue;
9291a0cd
TT
3858 }
3859 }
3860 }
3861 }
3862
14bc53a8 3863 return false;
9291a0cd
TT
3864}
3865
da51c347
DE
3866/* Struct used to manage iterating over all CUs looking for a symbol. */
3867
3868struct dw2_symtab_iterator
9291a0cd 3869{
ed2dc618
SM
3870 /* The dwarf2_per_objfile owning the CUs we are iterating on. */
3871 struct dwarf2_per_objfile *dwarf2_per_objfile;
da51c347
DE
3872 /* If non-zero, only look for symbols that match BLOCK_INDEX. */
3873 int want_specific_block;
3874 /* One of GLOBAL_BLOCK or STATIC_BLOCK.
3875 Unused if !WANT_SPECIFIC_BLOCK. */
3876 int block_index;
3877 /* The kind of symbol we're looking for. */
3878 domain_enum domain;
3879 /* The list of CUs from the index entry of the symbol,
3880 or NULL if not found. */
3881 offset_type *vec;
3882 /* The next element in VEC to look at. */
3883 int next;
3884 /* The number of elements in VEC, or zero if there is no match. */
3885 int length;
8943b874
DE
3886 /* Have we seen a global version of the symbol?
3887 If so we can ignore all further global instances.
3888 This is to work around gold/15646, inefficient gold-generated
3889 indices. */
3890 int global_seen;
da51c347 3891};
9291a0cd 3892
da51c347
DE
3893/* Initialize the index symtab iterator ITER.
3894 If WANT_SPECIFIC_BLOCK is non-zero, only look for symbols
3895 in block BLOCK_INDEX. Otherwise BLOCK_INDEX is ignored. */
2fdf6df6 3896
9291a0cd 3897static void
da51c347 3898dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
ed2dc618 3899 struct dwarf2_per_objfile *dwarf2_per_objfile,
da51c347
DE
3900 int want_specific_block,
3901 int block_index,
3902 domain_enum domain,
3903 const char *name)
3904{
ed2dc618 3905 iter->dwarf2_per_objfile = dwarf2_per_objfile;
da51c347
DE
3906 iter->want_specific_block = want_specific_block;
3907 iter->block_index = block_index;
3908 iter->domain = domain;
3909 iter->next = 0;
8943b874 3910 iter->global_seen = 0;
da51c347 3911
3063847f 3912 mapped_index *index = dwarf2_per_objfile->index_table.get ();
ed2dc618
SM
3913
3914 /* index is NULL if OBJF_READNOW. */
3915 if (index != NULL && find_slot_in_mapped_hash (index, name, &iter->vec))
da51c347
DE
3916 iter->length = MAYBE_SWAP (*iter->vec);
3917 else
3918 {
3919 iter->vec = NULL;
3920 iter->length = 0;
3921 }
3922}
3923
3924/* Return the next matching CU or NULL if there are no more. */
3925
3926static struct dwarf2_per_cu_data *
3927dw2_symtab_iter_next (struct dw2_symtab_iterator *iter)
3928{
ed2dc618
SM
3929 struct dwarf2_per_objfile *dwarf2_per_objfile = iter->dwarf2_per_objfile;
3930
da51c347
DE
3931 for ( ; iter->next < iter->length; ++iter->next)
3932 {
3933 offset_type cu_index_and_attrs =
3934 MAYBE_SWAP (iter->vec[iter->next + 1]);
3935 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
da51c347
DE
3936 int want_static = iter->block_index != GLOBAL_BLOCK;
3937 /* This value is only valid for index versions >= 7. */
3938 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
3939 gdb_index_symbol_kind symbol_kind =
3940 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3941 /* Only check the symbol attributes if they're present.
3942 Indices prior to version 7 don't record them,
3943 and indices >= 7 may elide them for certain symbols
3944 (gold does this). */
3945 int attrs_valid =
ed2dc618 3946 (dwarf2_per_objfile->index_table->version >= 7
da51c347
DE
3947 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
3948
3190f0c6 3949 /* Don't crash on bad data. */
b76e467d 3950 if (cu_index >= (dwarf2_per_objfile->all_comp_units.size ()
b2bdb8cf 3951 + dwarf2_per_objfile->all_type_units.size ()))
3190f0c6
DE
3952 {
3953 complaint (&symfile_complaints,
3954 _(".gdb_index entry has bad CU index"
4262abfb
JK
3955 " [in module %s]"),
3956 objfile_name (dwarf2_per_objfile->objfile));
3190f0c6
DE
3957 continue;
3958 }
3959
ff4c9fec 3960 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (cu_index);
3190f0c6 3961
da51c347 3962 /* Skip if already read in. */
43f3e411 3963 if (per_cu->v.quick->compunit_symtab)
da51c347
DE
3964 continue;
3965
8943b874
DE
3966 /* Check static vs global. */
3967 if (attrs_valid)
3968 {
3969 if (iter->want_specific_block
3970 && want_static != is_static)
3971 continue;
3972 /* Work around gold/15646. */
3973 if (!is_static && iter->global_seen)
3974 continue;
3975 if (!is_static)
3976 iter->global_seen = 1;
3977 }
da51c347
DE
3978
3979 /* Only check the symbol's kind if it has one. */
3980 if (attrs_valid)
3981 {
3982 switch (iter->domain)
3983 {
3984 case VAR_DOMAIN:
3985 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
3986 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
3987 /* Some types are also in VAR_DOMAIN. */
3988 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3989 continue;
3990 break;
3991 case STRUCT_DOMAIN:
3992 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3993 continue;
3994 break;
3995 case LABEL_DOMAIN:
3996 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3997 continue;
3998 break;
3999 default:
4000 break;
4001 }
4002 }
4003
4004 ++iter->next;
4005 return per_cu;
4006 }
4007
4008 return NULL;
4009}
4010
43f3e411 4011static struct compunit_symtab *
da51c347
DE
4012dw2_lookup_symbol (struct objfile *objfile, int block_index,
4013 const char *name, domain_enum domain)
9291a0cd 4014{
43f3e411 4015 struct compunit_symtab *stab_best = NULL;
ed2dc618
SM
4016 struct dwarf2_per_objfile *dwarf2_per_objfile
4017 = get_dwarf2_per_objfile (objfile);
9291a0cd 4018
b5ec771e
PA
4019 lookup_name_info lookup_name (name, symbol_name_match_type::FULL);
4020
ed2dc618
SM
4021 struct dw2_symtab_iterator iter;
4022 struct dwarf2_per_cu_data *per_cu;
da51c347 4023
ed2dc618 4024 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, 1, block_index, domain, name);
9291a0cd 4025
ed2dc618
SM
4026 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
4027 {
4028 struct symbol *sym, *with_opaque = NULL;
58f0c718 4029 struct compunit_symtab *stab = dw2_instantiate_symtab (per_cu, false);
ed2dc618
SM
4030 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
4031 struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
da51c347 4032
ed2dc618
SM
4033 sym = block_find_symbol (block, name, domain,
4034 block_find_non_opaque_type_preferred,
4035 &with_opaque);
b2e2f908 4036
ed2dc618
SM
4037 /* Some caution must be observed with overloaded functions
4038 and methods, since the index will not contain any overload
4039 information (but NAME might contain it). */
da51c347 4040
ed2dc618
SM
4041 if (sym != NULL
4042 && SYMBOL_MATCHES_SEARCH_NAME (sym, lookup_name))
4043 return stab;
4044 if (with_opaque != NULL
4045 && SYMBOL_MATCHES_SEARCH_NAME (with_opaque, lookup_name))
4046 stab_best = stab;
da51c347 4047
ed2dc618 4048 /* Keep looking through other CUs. */
9291a0cd 4049 }
9291a0cd 4050
da51c347 4051 return stab_best;
9291a0cd
TT
4052}
4053
4054static void
4055dw2_print_stats (struct objfile *objfile)
4056{
ed2dc618
SM
4057 struct dwarf2_per_objfile *dwarf2_per_objfile
4058 = get_dwarf2_per_objfile (objfile);
b76e467d 4059 int total = (dwarf2_per_objfile->all_comp_units.size ()
b2bdb8cf 4060 + dwarf2_per_objfile->all_type_units.size ());
ed2dc618 4061 int count = 0;
9291a0cd 4062
ed2dc618 4063 for (int i = 0; i < total; ++i)
9291a0cd 4064 {
ff4c9fec 4065 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (i);
9291a0cd 4066
43f3e411 4067 if (!per_cu->v.quick->compunit_symtab)
9291a0cd
TT
4068 ++count;
4069 }
e4a48d9d 4070 printf_filtered (_(" Number of read CUs: %d\n"), total - count);
9291a0cd
TT
4071 printf_filtered (_(" Number of unread CUs: %d\n"), count);
4072}
4073
779bd270
DE
4074/* This dumps minimal information about the index.
4075 It is called via "mt print objfiles".
4076 One use is to verify .gdb_index has been loaded by the
4077 gdb.dwarf2/gdb-index.exp testcase. */
4078
9291a0cd
TT
4079static void
4080dw2_dump (struct objfile *objfile)
4081{
ed2dc618
SM
4082 struct dwarf2_per_objfile *dwarf2_per_objfile
4083 = get_dwarf2_per_objfile (objfile);
4084
779bd270
DE
4085 gdb_assert (dwarf2_per_objfile->using_index);
4086 printf_filtered (".gdb_index:");
4087 if (dwarf2_per_objfile->index_table != NULL)
4088 {
4089 printf_filtered (" version %d\n",
4090 dwarf2_per_objfile->index_table->version);
4091 }
4092 else
4093 printf_filtered (" faked for \"readnow\"\n");
4094 printf_filtered ("\n");
9291a0cd
TT
4095}
4096
4097static void
3189cb12
DE
4098dw2_relocate (struct objfile *objfile,
4099 const struct section_offsets *new_offsets,
4100 const struct section_offsets *delta)
9291a0cd
TT
4101{
4102 /* There's nothing to relocate here. */
4103}
4104
4105static void
4106dw2_expand_symtabs_for_function (struct objfile *objfile,
4107 const char *func_name)
4108{
ed2dc618
SM
4109 struct dwarf2_per_objfile *dwarf2_per_objfile
4110 = get_dwarf2_per_objfile (objfile);
da51c347 4111
ed2dc618
SM
4112 struct dw2_symtab_iterator iter;
4113 struct dwarf2_per_cu_data *per_cu;
da51c347 4114
ed2dc618
SM
4115 /* Note: It doesn't matter what we pass for block_index here. */
4116 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, 0, GLOBAL_BLOCK, VAR_DOMAIN,
4117 func_name);
da51c347 4118
ed2dc618 4119 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
58f0c718 4120 dw2_instantiate_symtab (per_cu, false);
da51c347 4121
9291a0cd
TT
4122}
4123
4124static void
4125dw2_expand_all_symtabs (struct objfile *objfile)
4126{
ed2dc618
SM
4127 struct dwarf2_per_objfile *dwarf2_per_objfile
4128 = get_dwarf2_per_objfile (objfile);
b76e467d 4129 int total_units = (dwarf2_per_objfile->all_comp_units.size ()
b2bdb8cf 4130 + dwarf2_per_objfile->all_type_units.size ());
9291a0cd 4131
ed2dc618 4132 for (int i = 0; i < total_units; ++i)
9291a0cd 4133 {
ff4c9fec 4134 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (i);
9291a0cd 4135
58f0c718
TT
4136 /* We don't want to directly expand a partial CU, because if we
4137 read it with the wrong language, then assertion failures can
4138 be triggered later on. See PR symtab/23010. So, tell
4139 dw2_instantiate_symtab to skip partial CUs -- any important
4140 partial CU will be read via DW_TAG_imported_unit anyway. */
4141 dw2_instantiate_symtab (per_cu, true);
9291a0cd
TT
4142 }
4143}
4144
4145static void
652a8996
JK
4146dw2_expand_symtabs_with_fullname (struct objfile *objfile,
4147 const char *fullname)
9291a0cd 4148{
ed2dc618
SM
4149 struct dwarf2_per_objfile *dwarf2_per_objfile
4150 = get_dwarf2_per_objfile (objfile);
d4637a04
DE
4151
4152 /* We don't need to consider type units here.
4153 This is only called for examining code, e.g. expand_line_sal.
4154 There can be an order of magnitude (or more) more type units
4155 than comp units, and we avoid them if we can. */
4156
b76e467d 4157 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
9291a0cd 4158 {
3d7bb9d9 4159 /* We only need to look at symtabs not already expanded. */
43f3e411 4160 if (per_cu->v.quick->compunit_symtab)
9291a0cd
TT
4161 continue;
4162
b76e467d 4163 quick_file_names *file_data = dw2_get_file_names (per_cu);
7b9f3c50 4164 if (file_data == NULL)
9291a0cd
TT
4165 continue;
4166
b76e467d 4167 for (int j = 0; j < file_data->num_file_names; ++j)
9291a0cd 4168 {
652a8996
JK
4169 const char *this_fullname = file_data->file_names[j];
4170
4171 if (filename_cmp (this_fullname, fullname) == 0)
9291a0cd 4172 {
58f0c718 4173 dw2_instantiate_symtab (per_cu, false);
9291a0cd
TT
4174 break;
4175 }
4176 }
4177 }
4178}
4179
9291a0cd 4180static void
ade7ed9e 4181dw2_map_matching_symbols (struct objfile *objfile,
fe978cb0 4182 const char * name, domain_enum domain,
ade7ed9e 4183 int global,
40658b94
PH
4184 int (*callback) (struct block *,
4185 struct symbol *, void *),
b5ec771e 4186 void *data, symbol_name_match_type match,
2edb89d3 4187 symbol_compare_ftype *ordered_compare)
9291a0cd 4188{
40658b94 4189 /* Currently unimplemented; used for Ada. The function can be called if the
a9e6a4bb
JK
4190 current language is Ada for a non-Ada objfile using GNU index. As Ada
4191 does not look for non-Ada symbols this function should just return. */
9291a0cd
TT
4192}
4193
b5ec771e
PA
4194/* Symbol name matcher for .gdb_index names.
4195
4196 Symbol names in .gdb_index have a few particularities:
4197
4198 - There's no indication of which is the language of each symbol.
4199
4200 Since each language has its own symbol name matching algorithm,
4201 and we don't know which language is the right one, we must match
3f563c84
PA
4202 each symbol against all languages. This would be a potential
4203 performance problem if it were not mitigated by the
4204 mapped_index::name_components lookup table, which significantly
4205 reduces the number of times we need to call into this matcher,
4206 making it a non-issue.
b5ec771e
PA
4207
4208 - Symbol names in the index have no overload (parameter)
4209 information. I.e., in C++, "foo(int)" and "foo(long)" both
4210 appear as "foo" in the index, for example.
4211
4212 This means that the lookup names passed to the symbol name
4213 matcher functions must have no parameter information either
4214 because (e.g.) symbol search name "foo" does not match
4215 lookup-name "foo(int)" [while swapping search name for lookup
4216 name would match].
4217*/
4218class gdb_index_symbol_name_matcher
4219{
4220public:
4221 /* Prepares the vector of comparison functions for LOOKUP_NAME. */
4222 gdb_index_symbol_name_matcher (const lookup_name_info &lookup_name);
4223
4224 /* Walk all the matcher routines and match SYMBOL_NAME against them.
4225 Returns true if any matcher matches. */
4226 bool matches (const char *symbol_name);
4227
4228private:
4229 /* A reference to the lookup name we're matching against. */
4230 const lookup_name_info &m_lookup_name;
4231
4232 /* A vector holding all the different symbol name matchers, for all
4233 languages. */
4234 std::vector<symbol_name_matcher_ftype *> m_symbol_name_matcher_funcs;
4235};
4236
4237gdb_index_symbol_name_matcher::gdb_index_symbol_name_matcher
4238 (const lookup_name_info &lookup_name)
4239 : m_lookup_name (lookup_name)
4240{
4241 /* Prepare the vector of comparison functions upfront, to avoid
4242 doing the same work for each symbol. Care is taken to avoid
4243 matching with the same matcher more than once if/when multiple
4244 languages use the same matcher function. */
4245 auto &matchers = m_symbol_name_matcher_funcs;
4246 matchers.reserve (nr_languages);
4247
4248 matchers.push_back (default_symbol_name_matcher);
4249
4250 for (int i = 0; i < nr_languages; i++)
4251 {
4252 const language_defn *lang = language_def ((enum language) i);
c63d3e8d 4253 symbol_name_matcher_ftype *name_matcher
618daa93 4254 = get_symbol_name_matcher (lang, m_lookup_name);
c63d3e8d
PA
4255
4256 /* Don't insert the same comparison routine more than once.
4257 Note that we do this linear walk instead of a seemingly
4258 cheaper sorted insert, or use a std::set or something like
4259 that, because relative order of function addresses is not
4260 stable. This is not a problem in practice because the number
4261 of supported languages is low, and the cost here is tiny
4262 compared to the number of searches we'll do afterwards using
4263 this object. */
4264 if (name_matcher != default_symbol_name_matcher
4265 && (std::find (matchers.begin (), matchers.end (), name_matcher)
4266 == matchers.end ()))
4267 matchers.push_back (name_matcher);
b5ec771e
PA
4268 }
4269}
4270
4271bool
4272gdb_index_symbol_name_matcher::matches (const char *symbol_name)
4273{
4274 for (auto matches_name : m_symbol_name_matcher_funcs)
4275 if (matches_name (symbol_name, m_lookup_name, NULL))
4276 return true;
4277
4278 return false;
4279}
4280
e1ef7d7a
PA
4281/* Starting from a search name, return the string that finds the upper
4282 bound of all strings that start with SEARCH_NAME in a sorted name
4283 list. Returns the empty string to indicate that the upper bound is
4284 the end of the list. */
4285
4286static std::string
4287make_sort_after_prefix_name (const char *search_name)
4288{
4289 /* When looking to complete "func", we find the upper bound of all
4290 symbols that start with "func" by looking for where we'd insert
4291 the closest string that would follow "func" in lexicographical
4292 order. Usually, that's "func"-with-last-character-incremented,
4293 i.e. "fund". Mind non-ASCII characters, though. Usually those
4294 will be UTF-8 multi-byte sequences, but we can't be certain.
4295 Especially mind the 0xff character, which is a valid character in
4296 non-UTF-8 source character sets (e.g. Latin1 'ÿ'), and we can't
4297 rule out compilers allowing it in identifiers. Note that
4298 conveniently, strcmp/strcasecmp are specified to compare
4299 characters interpreted as unsigned char. So what we do is treat
4300 the whole string as a base 256 number composed of a sequence of
4301 base 256 "digits" and add 1 to it. I.e., adding 1 to 0xff wraps
4302 to 0, and carries 1 to the following more-significant position.
4303 If the very first character in SEARCH_NAME ends up incremented
4304 and carries/overflows, then the upper bound is the end of the
4305 list. The string after the empty string is also the empty
4306 string.
4307
4308 Some examples of this operation:
4309
4310 SEARCH_NAME => "+1" RESULT
4311
4312 "abc" => "abd"
4313 "ab\xff" => "ac"
4314 "\xff" "a" "\xff" => "\xff" "b"
4315 "\xff" => ""
4316 "\xff\xff" => ""
4317 "" => ""
4318
4319 Then, with these symbols for example:
4320
4321 func
4322 func1
4323 fund
4324
4325 completing "func" looks for symbols between "func" and
4326 "func"-with-last-character-incremented, i.e. "fund" (exclusive),
4327 which finds "func" and "func1", but not "fund".
4328
4329 And with:
4330
4331 funcÿ (Latin1 'ÿ' [0xff])
4332 funcÿ1
4333 fund
4334
4335 completing "funcÿ" looks for symbols between "funcÿ" and "fund"
4336 (exclusive), which finds "funcÿ" and "funcÿ1", but not "fund".
4337
4338 And with:
4339
4340 ÿÿ (Latin1 'ÿ' [0xff])
4341 ÿÿ1
4342
4343 completing "ÿ" or "ÿÿ" looks for symbols between between "ÿÿ" and
4344 the end of the list.
4345 */
4346 std::string after = search_name;
4347 while (!after.empty () && (unsigned char) after.back () == 0xff)
4348 after.pop_back ();
4349 if (!after.empty ())
4350 after.back () = (unsigned char) after.back () + 1;
4351 return after;
4352}
4353
5c58de74 4354/* See declaration. */
61d96d7e 4355
5c58de74
PA
4356std::pair<std::vector<name_component>::const_iterator,
4357 std::vector<name_component>::const_iterator>
44ed8f3e 4358mapped_index_base::find_name_components_bounds
5c58de74 4359 (const lookup_name_info &lookup_name_without_params) const
3f563c84 4360{
5c58de74
PA
4361 auto *name_cmp
4362 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
3f563c84
PA
4363
4364 const char *cplus
c62446b1 4365 = lookup_name_without_params.cplus ().lookup_name ().c_str ();
9291a0cd 4366
3f563c84
PA
4367 /* Comparison function object for lower_bound that matches against a
4368 given symbol name. */
4369 auto lookup_compare_lower = [&] (const name_component &elem,
4370 const char *name)
4371 {
5c58de74 4372 const char *elem_qualified = this->symbol_name_at (elem.idx);
3f563c84
PA
4373 const char *elem_name = elem_qualified + elem.name_offset;
4374 return name_cmp (elem_name, name) < 0;
4375 };
4376
4377 /* Comparison function object for upper_bound that matches against a
4378 given symbol name. */
4379 auto lookup_compare_upper = [&] (const char *name,
4380 const name_component &elem)
4381 {
5c58de74 4382 const char *elem_qualified = this->symbol_name_at (elem.idx);
3f563c84
PA
4383 const char *elem_name = elem_qualified + elem.name_offset;
4384 return name_cmp (name, elem_name) < 0;
4385 };
4386
5c58de74
PA
4387 auto begin = this->name_components.begin ();
4388 auto end = this->name_components.end ();
3f563c84
PA
4389
4390 /* Find the lower bound. */
4391 auto lower = [&] ()
4392 {
5c58de74 4393 if (lookup_name_without_params.completion_mode () && cplus[0] == '\0')
3f563c84
PA
4394 return begin;
4395 else
4396 return std::lower_bound (begin, end, cplus, lookup_compare_lower);
4397 } ();
4398
4399 /* Find the upper bound. */
4400 auto upper = [&] ()
4401 {
5c58de74 4402 if (lookup_name_without_params.completion_mode ())
3f563c84 4403 {
e1ef7d7a
PA
4404 /* In completion mode, we want UPPER to point past all
4405 symbols names that have the same prefix. I.e., with
4406 these symbols, and completing "func":
4407
4408 function << lower bound
4409 function1
4410 other_function << upper bound
4411
4412 We find the upper bound by looking for the insertion
4413 point of "func"-with-last-character-incremented,
4414 i.e. "fund". */
4415 std::string after = make_sort_after_prefix_name (cplus);
4416 if (after.empty ())
3f563c84 4417 return end;
e6b2f5ef
PA
4418 return std::lower_bound (lower, end, after.c_str (),
4419 lookup_compare_lower);
3f563c84
PA
4420 }
4421 else
4422 return std::upper_bound (lower, end, cplus, lookup_compare_upper);
4423 } ();
4424
5c58de74
PA
4425 return {lower, upper};
4426}
4427
4428/* See declaration. */
4429
4430void
44ed8f3e 4431mapped_index_base::build_name_components ()
5c58de74
PA
4432{
4433 if (!this->name_components.empty ())
4434 return;
4435
4436 this->name_components_casing = case_sensitivity;
4437 auto *name_cmp
4438 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
4439
4440 /* The code below only knows how to break apart components of C++
4441 symbol names (and other languages that use '::' as
4442 namespace/module separator). If we add support for wild matching
4443 to some language that uses some other operator (E.g., Ada, Go and
4444 D use '.'), then we'll need to try splitting the symbol name
4445 according to that language too. Note that Ada does support wild
4446 matching, but doesn't currently support .gdb_index. */
44ed8f3e
PA
4447 auto count = this->symbol_name_count ();
4448 for (offset_type idx = 0; idx < count; idx++)
5c58de74 4449 {
44ed8f3e 4450 if (this->symbol_name_slot_invalid (idx))
5c58de74
PA
4451 continue;
4452
4453 const char *name = this->symbol_name_at (idx);
4454
4455 /* Add each name component to the name component table. */
4456 unsigned int previous_len = 0;
4457 for (unsigned int current_len = cp_find_first_component (name);
4458 name[current_len] != '\0';
4459 current_len += cp_find_first_component (name + current_len))
4460 {
4461 gdb_assert (name[current_len] == ':');
4462 this->name_components.push_back ({previous_len, idx});
4463 /* Skip the '::'. */
4464 current_len += 2;
4465 previous_len = current_len;
4466 }
4467 this->name_components.push_back ({previous_len, idx});
4468 }
4469
4470 /* Sort name_components elements by name. */
4471 auto name_comp_compare = [&] (const name_component &left,
4472 const name_component &right)
4473 {
4474 const char *left_qualified = this->symbol_name_at (left.idx);
4475 const char *right_qualified = this->symbol_name_at (right.idx);
4476
4477 const char *left_name = left_qualified + left.name_offset;
4478 const char *right_name = right_qualified + right.name_offset;
4479
4480 return name_cmp (left_name, right_name) < 0;
4481 };
4482
4483 std::sort (this->name_components.begin (),
4484 this->name_components.end (),
4485 name_comp_compare);
4486}
4487
4488/* Helper for dw2_expand_symtabs_matching that works with a
44ed8f3e
PA
4489 mapped_index_base instead of the containing objfile. This is split
4490 to a separate function in order to be able to unit test the
4491 name_components matching using a mock mapped_index_base. For each
5c58de74 4492 symbol name that matches, calls MATCH_CALLBACK, passing it the
44ed8f3e 4493 symbol's index in the mapped_index_base symbol table. */
5c58de74
PA
4494
4495static void
4496dw2_expand_symtabs_matching_symbol
44ed8f3e 4497 (mapped_index_base &index,
5c58de74
PA
4498 const lookup_name_info &lookup_name_in,
4499 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4500 enum search_domain kind,
4501 gdb::function_view<void (offset_type)> match_callback)
4502{
4503 lookup_name_info lookup_name_without_params
4504 = lookup_name_in.make_ignore_params ();
4505 gdb_index_symbol_name_matcher lookup_name_matcher
4506 (lookup_name_without_params);
4507
4508 /* Build the symbol name component sorted vector, if we haven't
4509 yet. */
4510 index.build_name_components ();
4511
4512 auto bounds = index.find_name_components_bounds (lookup_name_without_params);
4513
3f563c84
PA
4514 /* Now for each symbol name in range, check to see if we have a name
4515 match, and if so, call the MATCH_CALLBACK callback. */
4516
4517 /* The same symbol may appear more than once in the range though.
4518 E.g., if we're looking for symbols that complete "w", and we have
4519 a symbol named "w1::w2", we'll find the two name components for
4520 that same symbol in the range. To be sure we only call the
4521 callback once per symbol, we first collect the symbol name
4522 indexes that matched in a temporary vector and ignore
4523 duplicates. */
4524 std::vector<offset_type> matches;
5c58de74 4525 matches.reserve (std::distance (bounds.first, bounds.second));
3f563c84 4526
5c58de74 4527 for (; bounds.first != bounds.second; ++bounds.first)
3f563c84 4528 {
5c58de74 4529 const char *qualified = index.symbol_name_at (bounds.first->idx);
3f563c84
PA
4530
4531 if (!lookup_name_matcher.matches (qualified)
4532 || (symbol_matcher != NULL && !symbol_matcher (qualified)))
9291a0cd
TT
4533 continue;
4534
5c58de74 4535 matches.push_back (bounds.first->idx);
3f563c84
PA
4536 }
4537
4538 std::sort (matches.begin (), matches.end ());
4539
4540 /* Finally call the callback, once per match. */
4541 ULONGEST prev = -1;
4542 for (offset_type idx : matches)
4543 {
4544 if (prev != idx)
4545 {
4546 match_callback (idx);
4547 prev = idx;
4548 }
4549 }
4550
4551 /* Above we use a type wider than idx's for 'prev', since 0 and
4552 (offset_type)-1 are both possible values. */
4553 static_assert (sizeof (prev) > sizeof (offset_type), "");
4554}
4555
c62446b1
PA
4556#if GDB_SELF_TEST
4557
4558namespace selftests { namespace dw2_expand_symtabs_matching {
4559
a3c5fafd
PA
4560/* A mock .gdb_index/.debug_names-like name index table, enough to
4561 exercise dw2_expand_symtabs_matching_symbol, which works with the
4562 mapped_index_base interface. Builds an index from the symbol list
4563 passed as parameter to the constructor. */
4564class mock_mapped_index : public mapped_index_base
c62446b1
PA
4565{
4566public:
a3c5fafd
PA
4567 mock_mapped_index (gdb::array_view<const char *> symbols)
4568 : m_symbol_table (symbols)
c62446b1
PA
4569 {}
4570
a3c5fafd 4571 DISABLE_COPY_AND_ASSIGN (mock_mapped_index);
c62446b1 4572
a3c5fafd 4573 /* Return the number of names in the symbol table. */
632e107b 4574 size_t symbol_name_count () const override
c62446b1 4575 {
a3c5fafd 4576 return m_symbol_table.size ();
c62446b1
PA
4577 }
4578
a3c5fafd 4579 /* Get the name of the symbol at IDX in the symbol table. */
632e107b 4580 const char *symbol_name_at (offset_type idx) const override
a3c5fafd
PA
4581 {
4582 return m_symbol_table[idx];
4583 }
c62446b1 4584
a3c5fafd
PA
4585private:
4586 gdb::array_view<const char *> m_symbol_table;
c62446b1
PA
4587};
4588
4589/* Convenience function that converts a NULL pointer to a "<null>"
4590 string, to pass to print routines. */
4591
4592static const char *
4593string_or_null (const char *str)
4594{
4595 return str != NULL ? str : "<null>";
4596}
4597
4598/* Check if a lookup_name_info built from
4599 NAME/MATCH_TYPE/COMPLETION_MODE matches the symbols in the mock
4600 index. EXPECTED_LIST is the list of expected matches, in expected
4601 matching order. If no match expected, then an empty list is
4602 specified. Returns true on success. On failure prints a warning
4603 indicating the file:line that failed, and returns false. */
4604
4605static bool
4606check_match (const char *file, int line,
4607 mock_mapped_index &mock_index,
4608 const char *name, symbol_name_match_type match_type,
4609 bool completion_mode,
4610 std::initializer_list<const char *> expected_list)
4611{
4612 lookup_name_info lookup_name (name, match_type, completion_mode);
4613
4614 bool matched = true;
4615
4616 auto mismatch = [&] (const char *expected_str,
4617 const char *got)
4618 {
4619 warning (_("%s:%d: match_type=%s, looking-for=\"%s\", "
4620 "expected=\"%s\", got=\"%s\"\n"),
4621 file, line,
4622 (match_type == symbol_name_match_type::FULL
4623 ? "FULL" : "WILD"),
4624 name, string_or_null (expected_str), string_or_null (got));
4625 matched = false;
4626 };
4627
4628 auto expected_it = expected_list.begin ();
4629 auto expected_end = expected_list.end ();
4630
a3c5fafd 4631 dw2_expand_symtabs_matching_symbol (mock_index, lookup_name,
c62446b1
PA
4632 NULL, ALL_DOMAIN,
4633 [&] (offset_type idx)
4634 {
a3c5fafd 4635 const char *matched_name = mock_index.symbol_name_at (idx);
c62446b1
PA
4636 const char *expected_str
4637 = expected_it == expected_end ? NULL : *expected_it++;
4638
4639 if (expected_str == NULL || strcmp (expected_str, matched_name) != 0)
4640 mismatch (expected_str, matched_name);
4641 });
4642
4643 const char *expected_str
4644 = expected_it == expected_end ? NULL : *expected_it++;
4645 if (expected_str != NULL)
4646 mismatch (expected_str, NULL);
4647
4648 return matched;
4649}
4650
4651/* The symbols added to the mock mapped_index for testing (in
4652 canonical form). */
4653static const char *test_symbols[] = {
4654 "function",
4655 "std::bar",
4656 "std::zfunction",
4657 "std::zfunction2",
4658 "w1::w2",
4659 "ns::foo<char*>",
4660 "ns::foo<int>",
4661 "ns::foo<long>",
a20714ff
PA
4662 "ns2::tmpl<int>::foo2",
4663 "(anonymous namespace)::A::B::C",
c62446b1 4664
e1ef7d7a
PA
4665 /* These are used to check that the increment-last-char in the
4666 matching algorithm for completion doesn't match "t1_fund" when
4667 completing "t1_func". */
4668 "t1_func",
4669 "t1_func1",
4670 "t1_fund",
4671 "t1_fund1",
4672
4673 /* A UTF-8 name with multi-byte sequences to make sure that
4674 cp-name-parser understands this as a single identifier ("função"
4675 is "function" in PT). */
4676 u8"u8função",
4677
4678 /* \377 (0xff) is Latin1 'ÿ'. */
4679 "yfunc\377",
4680
4681 /* \377 (0xff) is Latin1 'ÿ'. */
4682 "\377",
4683 "\377\377123",
4684
c62446b1
PA
4685 /* A name with all sorts of complications. Starts with "z" to make
4686 it easier for the completion tests below. */
4687#define Z_SYM_NAME \
4688 "z::std::tuple<(anonymous namespace)::ui*, std::bar<(anonymous namespace)::ui> >" \
4689 "::tuple<(anonymous namespace)::ui*, " \
4690 "std::default_delete<(anonymous namespace)::ui>, void>"
4691
4692 Z_SYM_NAME
4693};
4694
a3c5fafd
PA
4695/* Returns true if the mapped_index_base::find_name_component_bounds
4696 method finds EXPECTED_SYMS in INDEX when looking for SEARCH_NAME,
4697 in completion mode. */
5c58de74
PA
4698
4699static bool
a3c5fafd 4700check_find_bounds_finds (mapped_index_base &index,
5c58de74
PA
4701 const char *search_name,
4702 gdb::array_view<const char *> expected_syms)
4703{
4704 lookup_name_info lookup_name (search_name,
4705 symbol_name_match_type::FULL, true);
4706
4707 auto bounds = index.find_name_components_bounds (lookup_name);
4708
4709 size_t distance = std::distance (bounds.first, bounds.second);
4710 if (distance != expected_syms.size ())
4711 return false;
4712
4713 for (size_t exp_elem = 0; exp_elem < distance; exp_elem++)
4714 {
4715 auto nc_elem = bounds.first + exp_elem;
4716 const char *qualified = index.symbol_name_at (nc_elem->idx);
4717 if (strcmp (qualified, expected_syms[exp_elem]) != 0)
4718 return false;
4719 }
4720
4721 return true;
4722}
4723
4724/* Test the lower-level mapped_index::find_name_component_bounds
4725 method. */
4726
c62446b1 4727static void
5c58de74
PA
4728test_mapped_index_find_name_component_bounds ()
4729{
4730 mock_mapped_index mock_index (test_symbols);
4731
a3c5fafd 4732 mock_index.build_name_components ();
5c58de74
PA
4733
4734 /* Test the lower-level mapped_index::find_name_component_bounds
4735 method in completion mode. */
4736 {
4737 static const char *expected_syms[] = {
4738 "t1_func",
4739 "t1_func1",
5c58de74
PA
4740 };
4741
a3c5fafd 4742 SELF_CHECK (check_find_bounds_finds (mock_index,
5c58de74
PA
4743 "t1_func", expected_syms));
4744 }
4745
4746 /* Check that the increment-last-char in the name matching algorithm
4747 for completion doesn't get confused with Ansi1 'ÿ' / 0xff. */
4748 {
4749 static const char *expected_syms1[] = {
4750 "\377",
4751 "\377\377123",
4752 };
a3c5fafd 4753 SELF_CHECK (check_find_bounds_finds (mock_index,
5c58de74
PA
4754 "\377", expected_syms1));
4755
4756 static const char *expected_syms2[] = {
4757 "\377\377123",
4758 };
a3c5fafd 4759 SELF_CHECK (check_find_bounds_finds (mock_index,
5c58de74
PA
4760 "\377\377", expected_syms2));
4761 }
4762}
4763
4764/* Test dw2_expand_symtabs_matching_symbol. */
4765
4766static void
4767test_dw2_expand_symtabs_matching_symbol ()
c62446b1
PA
4768{
4769 mock_mapped_index mock_index (test_symbols);
4770
4771 /* We let all tests run until the end even if some fails, for debug
4772 convenience. */
4773 bool any_mismatch = false;
4774
4775 /* Create the expected symbols list (an initializer_list). Needed
4776 because lists have commas, and we need to pass them to CHECK,
4777 which is a macro. */
4778#define EXPECT(...) { __VA_ARGS__ }
4779
4780 /* Wrapper for check_match that passes down the current
4781 __FILE__/__LINE__. */
4782#define CHECK_MATCH(NAME, MATCH_TYPE, COMPLETION_MODE, EXPECTED_LIST) \
4783 any_mismatch |= !check_match (__FILE__, __LINE__, \
4784 mock_index, \
4785 NAME, MATCH_TYPE, COMPLETION_MODE, \
4786 EXPECTED_LIST)
4787
4788 /* Identity checks. */
4789 for (const char *sym : test_symbols)
4790 {
4791 /* Should be able to match all existing symbols. */
4792 CHECK_MATCH (sym, symbol_name_match_type::FULL, false,
4793 EXPECT (sym));
4794
4795 /* Should be able to match all existing symbols with
4796 parameters. */
4797 std::string with_params = std::string (sym) + "(int)";
4798 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4799 EXPECT (sym));
4800
4801 /* Should be able to match all existing symbols with
4802 parameters and qualifiers. */
4803 with_params = std::string (sym) + " ( int ) const";
4804 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4805 EXPECT (sym));
4806
4807 /* This should really find sym, but cp-name-parser.y doesn't
4808 know about lvalue/rvalue qualifiers yet. */
4809 with_params = std::string (sym) + " ( int ) &&";
4810 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4811 {});
4812 }
4813
e1ef7d7a
PA
4814 /* Check that the name matching algorithm for completion doesn't get
4815 confused with Latin1 'ÿ' / 0xff. */
4816 {
4817 static const char str[] = "\377";
4818 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4819 EXPECT ("\377", "\377\377123"));
4820 }
4821
4822 /* Check that the increment-last-char in the matching algorithm for
4823 completion doesn't match "t1_fund" when completing "t1_func". */
4824 {
4825 static const char str[] = "t1_func";
4826 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4827 EXPECT ("t1_func", "t1_func1"));
4828 }
4829
c62446b1
PA
4830 /* Check that completion mode works at each prefix of the expected
4831 symbol name. */
4832 {
4833 static const char str[] = "function(int)";
4834 size_t len = strlen (str);
4835 std::string lookup;
4836
4837 for (size_t i = 1; i < len; i++)
4838 {
4839 lookup.assign (str, i);
4840 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4841 EXPECT ("function"));
4842 }
4843 }
4844
4845 /* While "w" is a prefix of both components, the match function
4846 should still only be called once. */
4847 {
4848 CHECK_MATCH ("w", symbol_name_match_type::FULL, true,
4849 EXPECT ("w1::w2"));
a20714ff
PA
4850 CHECK_MATCH ("w", symbol_name_match_type::WILD, true,
4851 EXPECT ("w1::w2"));
c62446b1
PA
4852 }
4853
4854 /* Same, with a "complicated" symbol. */
4855 {
4856 static const char str[] = Z_SYM_NAME;
4857 size_t len = strlen (str);
4858 std::string lookup;
4859
4860 for (size_t i = 1; i < len; i++)
4861 {
4862 lookup.assign (str, i);
4863 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4864 EXPECT (Z_SYM_NAME));
4865 }
4866 }
4867
4868 /* In FULL mode, an incomplete symbol doesn't match. */
4869 {
4870 CHECK_MATCH ("std::zfunction(int", symbol_name_match_type::FULL, false,
4871 {});
4872 }
4873
4874 /* A complete symbol with parameters matches any overload, since the
4875 index has no overload info. */
4876 {
4877 CHECK_MATCH ("std::zfunction(int)", symbol_name_match_type::FULL, true,
4878 EXPECT ("std::zfunction", "std::zfunction2"));
a20714ff
PA
4879 CHECK_MATCH ("zfunction(int)", symbol_name_match_type::WILD, true,
4880 EXPECT ("std::zfunction", "std::zfunction2"));
4881 CHECK_MATCH ("zfunc", symbol_name_match_type::WILD, true,
4882 EXPECT ("std::zfunction", "std::zfunction2"));
c62446b1
PA
4883 }
4884
4885 /* Check that whitespace is ignored appropriately. A symbol with a
4886 template argument list. */
4887 {
4888 static const char expected[] = "ns::foo<int>";
4889 CHECK_MATCH ("ns :: foo < int > ", symbol_name_match_type::FULL, false,
4890 EXPECT (expected));
a20714ff
PA
4891 CHECK_MATCH ("foo < int > ", symbol_name_match_type::WILD, false,
4892 EXPECT (expected));
c62446b1
PA
4893 }
4894
4895 /* Check that whitespace is ignored appropriately. A symbol with a
4896 template argument list that includes a pointer. */
4897 {
4898 static const char expected[] = "ns::foo<char*>";
4899 /* Try both completion and non-completion modes. */
4900 static const bool completion_mode[2] = {false, true};
4901 for (size_t i = 0; i < 2; i++)
4902 {
4903 CHECK_MATCH ("ns :: foo < char * >", symbol_name_match_type::FULL,
4904 completion_mode[i], EXPECT (expected));
a20714ff
PA
4905 CHECK_MATCH ("foo < char * >", symbol_name_match_type::WILD,
4906 completion_mode[i], EXPECT (expected));
c62446b1
PA
4907
4908 CHECK_MATCH ("ns :: foo < char * > (int)", symbol_name_match_type::FULL,
4909 completion_mode[i], EXPECT (expected));
a20714ff
PA
4910 CHECK_MATCH ("foo < char * > (int)", symbol_name_match_type::WILD,
4911 completion_mode[i], EXPECT (expected));
c62446b1
PA
4912 }
4913 }
4914
4915 {
4916 /* Check method qualifiers are ignored. */
4917 static const char expected[] = "ns::foo<char*>";
4918 CHECK_MATCH ("ns :: foo < char * > ( int ) const",
4919 symbol_name_match_type::FULL, true, EXPECT (expected));
4920 CHECK_MATCH ("ns :: foo < char * > ( int ) &&",
4921 symbol_name_match_type::FULL, true, EXPECT (expected));
a20714ff
PA
4922 CHECK_MATCH ("foo < char * > ( int ) const",
4923 symbol_name_match_type::WILD, true, EXPECT (expected));
4924 CHECK_MATCH ("foo < char * > ( int ) &&",
4925 symbol_name_match_type::WILD, true, EXPECT (expected));
c62446b1
PA
4926 }
4927
4928 /* Test lookup names that don't match anything. */
4929 {
a20714ff
PA
4930 CHECK_MATCH ("bar2", symbol_name_match_type::WILD, false,
4931 {});
4932
c62446b1
PA
4933 CHECK_MATCH ("doesntexist", symbol_name_match_type::FULL, false,
4934 {});
4935 }
4936
a20714ff
PA
4937 /* Some wild matching tests, exercising "(anonymous namespace)",
4938 which should not be confused with a parameter list. */
4939 {
4940 static const char *syms[] = {
4941 "A::B::C",
4942 "B::C",
4943 "C",
4944 "A :: B :: C ( int )",
4945 "B :: C ( int )",
4946 "C ( int )",
4947 };
4948
4949 for (const char *s : syms)
4950 {
4951 CHECK_MATCH (s, symbol_name_match_type::WILD, false,
4952 EXPECT ("(anonymous namespace)::A::B::C"));
4953 }
4954 }
4955
4956 {
4957 static const char expected[] = "ns2::tmpl<int>::foo2";
4958 CHECK_MATCH ("tmp", symbol_name_match_type::WILD, true,
4959 EXPECT (expected));
4960 CHECK_MATCH ("tmpl<", symbol_name_match_type::WILD, true,
4961 EXPECT (expected));
4962 }
4963
c62446b1
PA
4964 SELF_CHECK (!any_mismatch);
4965
4966#undef EXPECT
4967#undef CHECK_MATCH
4968}
4969
5c58de74
PA
4970static void
4971run_test ()
4972{
4973 test_mapped_index_find_name_component_bounds ();
4974 test_dw2_expand_symtabs_matching_symbol ();
4975}
4976
c62446b1
PA
4977}} // namespace selftests::dw2_expand_symtabs_matching
4978
4979#endif /* GDB_SELF_TEST */
4980
4b514bc8
JK
4981/* If FILE_MATCHER is NULL or if PER_CU has
4982 dwarf2_per_cu_quick_data::MARK set (see
4983 dw_expand_symtabs_matching_file_matcher), expand the CU and call
4984 EXPANSION_NOTIFY on it. */
4985
4986static void
4987dw2_expand_symtabs_matching_one
4988 (struct dwarf2_per_cu_data *per_cu,
4989 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4990 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify)
4991{
4992 if (file_matcher == NULL || per_cu->v.quick->mark)
4993 {
4994 bool symtab_was_null
4995 = (per_cu->v.quick->compunit_symtab == NULL);
4996
58f0c718 4997 dw2_instantiate_symtab (per_cu, false);
4b514bc8
JK
4998
4999 if (expansion_notify != NULL
5000 && symtab_was_null
5001 && per_cu->v.quick->compunit_symtab != NULL)
5002 expansion_notify (per_cu->v.quick->compunit_symtab);
5003 }
5004}
5005
3f563c84
PA
5006/* Helper for dw2_expand_matching symtabs. Called on each symbol
5007 matched, to expand corresponding CUs that were marked. IDX is the
5008 index of the symbol name that matched. */
5009
5010static void
5011dw2_expand_marked_cus
ed2dc618 5012 (struct dwarf2_per_objfile *dwarf2_per_objfile, offset_type idx,
3f563c84
PA
5013 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
5014 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
5015 search_domain kind)
5016{
3f563c84
PA
5017 offset_type *vec, vec_len, vec_idx;
5018 bool global_seen = false;
ed2dc618 5019 mapped_index &index = *dwarf2_per_objfile->index_table;
3f563c84 5020
61920122 5021 vec = (offset_type *) (index.constant_pool
f00a2de2 5022 + MAYBE_SWAP (index.symbol_table[idx].vec));
61920122
PA
5023 vec_len = MAYBE_SWAP (vec[0]);
5024 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
5025 {
61920122
PA
5026 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[vec_idx + 1]);
5027 /* This value is only valid for index versions >= 7. */
5028 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
5029 gdb_index_symbol_kind symbol_kind =
5030 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
5031 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
5032 /* Only check the symbol attributes if they're present.
5033 Indices prior to version 7 don't record them,
5034 and indices >= 7 may elide them for certain symbols
5035 (gold does this). */
5036 int attrs_valid =
5037 (index.version >= 7
5038 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
5039
5040 /* Work around gold/15646. */
5041 if (attrs_valid)
9291a0cd 5042 {
61920122
PA
5043 if (!is_static && global_seen)
5044 continue;
5045 if (!is_static)
5046 global_seen = true;
5047 }
3190f0c6 5048
61920122
PA
5049 /* Only check the symbol's kind if it has one. */
5050 if (attrs_valid)
5051 {
5052 switch (kind)
8943b874 5053 {
61920122
PA
5054 case VARIABLES_DOMAIN:
5055 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
5056 continue;
5057 break;
5058 case FUNCTIONS_DOMAIN:
5059 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
8943b874 5060 continue;
61920122
PA
5061 break;
5062 case TYPES_DOMAIN:
5063 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
5064 continue;
5065 break;
5066 default:
5067 break;
8943b874 5068 }
61920122 5069 }
8943b874 5070
61920122 5071 /* Don't crash on bad data. */
b76e467d 5072 if (cu_index >= (dwarf2_per_objfile->all_comp_units.size ()
b2bdb8cf 5073 + dwarf2_per_objfile->all_type_units.size ()))
61920122
PA
5074 {
5075 complaint (&symfile_complaints,
5076 _(".gdb_index entry has bad CU index"
ed2dc618
SM
5077 " [in module %s]"),
5078 objfile_name (dwarf2_per_objfile->objfile));
61920122
PA
5079 continue;
5080 }
5081
ff4c9fec 5082 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (cu_index);
4b514bc8
JK
5083 dw2_expand_symtabs_matching_one (per_cu, file_matcher,
5084 expansion_notify);
61920122
PA
5085 }
5086}
5087
4b514bc8
JK
5088/* If FILE_MATCHER is non-NULL, set all the
5089 dwarf2_per_cu_quick_data::MARK of the current DWARF2_PER_OBJFILE
5090 that match FILE_MATCHER. */
5091
61920122 5092static void
4b514bc8 5093dw_expand_symtabs_matching_file_matcher
ed2dc618
SM
5094 (struct dwarf2_per_objfile *dwarf2_per_objfile,
5095 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher)
61920122 5096{
4b514bc8 5097 if (file_matcher == NULL)
61920122
PA
5098 return;
5099
4b514bc8
JK
5100 objfile *const objfile = dwarf2_per_objfile->objfile;
5101
5102 htab_up visited_found (htab_create_alloc (10, htab_hash_pointer,
5103 htab_eq_pointer,
5104 NULL, xcalloc, xfree));
5105 htab_up visited_not_found (htab_create_alloc (10, htab_hash_pointer,
61920122
PA
5106 htab_eq_pointer,
5107 NULL, xcalloc, xfree));
61920122 5108
4b514bc8
JK
5109 /* The rule is CUs specify all the files, including those used by
5110 any TU, so there's no need to scan TUs here. */
61920122 5111
b76e467d 5112 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
927aa2e7 5113 {
927aa2e7
JK
5114 QUIT;
5115
5116 per_cu->v.quick->mark = 0;
5117
5118 /* We only need to look at symtabs not already expanded. */
5119 if (per_cu->v.quick->compunit_symtab)
5120 continue;
5121
b76e467d 5122 quick_file_names *file_data = dw2_get_file_names (per_cu);
927aa2e7
JK
5123 if (file_data == NULL)
5124 continue;
5125
5126 if (htab_find (visited_not_found.get (), file_data) != NULL)
5127 continue;
5128 else if (htab_find (visited_found.get (), file_data) != NULL)
5129 {
5130 per_cu->v.quick->mark = 1;
5131 continue;
5132 }
5133
b76e467d 5134 for (int j = 0; j < file_data->num_file_names; ++j)
927aa2e7
JK
5135 {
5136 const char *this_real_name;
5137
5138 if (file_matcher (file_data->file_names[j], false))
5139 {
5140 per_cu->v.quick->mark = 1;
5141 break;
5142 }
5143
5144 /* Before we invoke realpath, which can get expensive when many
5145 files are involved, do a quick comparison of the basenames. */
5146 if (!basenames_may_differ
5147 && !file_matcher (lbasename (file_data->file_names[j]),
5148 true))
5149 continue;
5150
5151 this_real_name = dw2_get_real_path (objfile, file_data, j);
5152 if (file_matcher (this_real_name, false))
5153 {
5154 per_cu->v.quick->mark = 1;
5155 break;
5156 }
5157 }
5158
b76e467d
SM
5159 void **slot = htab_find_slot (per_cu->v.quick->mark
5160 ? visited_found.get ()
5161 : visited_not_found.get (),
5162 file_data, INSERT);
927aa2e7
JK
5163 *slot = file_data;
5164 }
5165}
5166
5167static void
5168dw2_expand_symtabs_matching
5169 (struct objfile *objfile,
5170 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
5171 const lookup_name_info &lookup_name,
5172 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
5173 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
5174 enum search_domain kind)
5175{
ed2dc618
SM
5176 struct dwarf2_per_objfile *dwarf2_per_objfile
5177 = get_dwarf2_per_objfile (objfile);
927aa2e7
JK
5178
5179 /* index_table is NULL if OBJF_READNOW. */
5180 if (!dwarf2_per_objfile->index_table)
5181 return;
5182
ed2dc618 5183 dw_expand_symtabs_matching_file_matcher (dwarf2_per_objfile, file_matcher);
927aa2e7
JK
5184
5185 mapped_index &index = *dwarf2_per_objfile->index_table;
5186
5187 dw2_expand_symtabs_matching_symbol (index, lookup_name,
5188 symbol_matcher,
5189 kind, [&] (offset_type idx)
5190 {
ed2dc618 5191 dw2_expand_marked_cus (dwarf2_per_objfile, idx, file_matcher,
927aa2e7
JK
5192 expansion_notify, kind);
5193 });
5194}
5195
5196/* A helper for dw2_find_pc_sect_compunit_symtab which finds the most specific
5197 symtab. */
5198
5199static struct compunit_symtab *
5200recursively_find_pc_sect_compunit_symtab (struct compunit_symtab *cust,
5201 CORE_ADDR pc)
5202{
5203 int i;
5204
5205 if (COMPUNIT_BLOCKVECTOR (cust) != NULL
5206 && blockvector_contains_pc (COMPUNIT_BLOCKVECTOR (cust), pc))
5207 return cust;
5208
5209 if (cust->includes == NULL)
5210 return NULL;
5211
5212 for (i = 0; cust->includes[i]; ++i)
5213 {
5214 struct compunit_symtab *s = cust->includes[i];
5215
5216 s = recursively_find_pc_sect_compunit_symtab (s, pc);
5217 if (s != NULL)
5218 return s;
5219 }
5220
5221 return NULL;
5222}
5223
5224static struct compunit_symtab *
5225dw2_find_pc_sect_compunit_symtab (struct objfile *objfile,
5226 struct bound_minimal_symbol msymbol,
5227 CORE_ADDR pc,
5228 struct obj_section *section,
5229 int warn_if_readin)
5230{
5231 struct dwarf2_per_cu_data *data;
5232 struct compunit_symtab *result;
5233
927aa2e7
JK
5234 if (!objfile->psymtabs_addrmap)
5235 return NULL;
5236
5237 data = (struct dwarf2_per_cu_data *) addrmap_find (objfile->psymtabs_addrmap,
5238 pc);
5239 if (!data)
5240 return NULL;
5241
5242 if (warn_if_readin && data->v.quick->compunit_symtab)
5243 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
5244 paddress (get_objfile_arch (objfile), pc));
5245
5246 result
58f0c718
TT
5247 = recursively_find_pc_sect_compunit_symtab (dw2_instantiate_symtab (data,
5248 false),
927aa2e7
JK
5249 pc);
5250 gdb_assert (result != NULL);
5251 return result;
5252}
5253
5254static void
5255dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
5256 void *data, int need_fullname)
5257{
ed2dc618
SM
5258 struct dwarf2_per_objfile *dwarf2_per_objfile
5259 = get_dwarf2_per_objfile (objfile);
927aa2e7
JK
5260
5261 if (!dwarf2_per_objfile->filenames_cache)
5262 {
5263 dwarf2_per_objfile->filenames_cache.emplace ();
5264
5265 htab_up visited (htab_create_alloc (10,
5266 htab_hash_pointer, htab_eq_pointer,
5267 NULL, xcalloc, xfree));
5268
5269 /* The rule is CUs specify all the files, including those used
5270 by any TU, so there's no need to scan TUs here. We can
5271 ignore file names coming from already-expanded CUs. */
5272
b76e467d 5273 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
927aa2e7 5274 {
927aa2e7
JK
5275 if (per_cu->v.quick->compunit_symtab)
5276 {
5277 void **slot = htab_find_slot (visited.get (),
5278 per_cu->v.quick->file_names,
5279 INSERT);
5280
5281 *slot = per_cu->v.quick->file_names;
5282 }
5283 }
5284
b76e467d 5285 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
927aa2e7 5286 {
927aa2e7
JK
5287 /* We only need to look at symtabs not already expanded. */
5288 if (per_cu->v.quick->compunit_symtab)
5289 continue;
5290
b76e467d 5291 quick_file_names *file_data = dw2_get_file_names (per_cu);
927aa2e7
JK
5292 if (file_data == NULL)
5293 continue;
5294
b76e467d 5295 void **slot = htab_find_slot (visited.get (), file_data, INSERT);
927aa2e7
JK
5296 if (*slot)
5297 {
5298 /* Already visited. */
5299 continue;
5300 }
5301 *slot = file_data;
5302
5303 for (int j = 0; j < file_data->num_file_names; ++j)
5304 {
5305 const char *filename = file_data->file_names[j];
5306 dwarf2_per_objfile->filenames_cache->seen (filename);
5307 }
5308 }
5309 }
5310
5311 dwarf2_per_objfile->filenames_cache->traverse ([&] (const char *filename)
5312 {
5313 gdb::unique_xmalloc_ptr<char> this_real_name;
5314
5315 if (need_fullname)
5316 this_real_name = gdb_realpath (filename);
5317 (*fun) (filename, this_real_name.get (), data);
5318 });
5319}
5320
5321static int
5322dw2_has_symbols (struct objfile *objfile)
5323{
5324 return 1;
5325}
5326
5327const struct quick_symbol_functions dwarf2_gdb_index_functions =
5328{
5329 dw2_has_symbols,
5330 dw2_find_last_source_symtab,
5331 dw2_forget_cached_source_info,
5332 dw2_map_symtabs_matching_filename,
5333 dw2_lookup_symbol,
5334 dw2_print_stats,
5335 dw2_dump,
5336 dw2_relocate,
5337 dw2_expand_symtabs_for_function,
5338 dw2_expand_all_symtabs,
5339 dw2_expand_symtabs_with_fullname,
5340 dw2_map_matching_symbols,
5341 dw2_expand_symtabs_matching,
5342 dw2_find_pc_sect_compunit_symtab,
5343 NULL,
5344 dw2_map_symbol_filenames
5345};
5346
5347/* DWARF-5 debug_names reader. */
5348
5349/* DWARF-5 augmentation string for GDB's DW_IDX_GNU_* extension. */
5350static const gdb_byte dwarf5_augmentation[] = { 'G', 'D', 'B', 0 };
5351
5352/* A helper function that reads the .debug_names section in SECTION
5353 and fills in MAP. FILENAME is the name of the file containing the
5354 section; it is used for error reporting.
5355
5356 Returns true if all went well, false otherwise. */
5357
5358static bool
5359read_debug_names_from_section (struct objfile *objfile,
5360 const char *filename,
5361 struct dwarf2_section_info *section,
5362 mapped_debug_names &map)
5363{
5364 if (dwarf2_section_empty_p (section))
5365 return false;
5366
5367 /* Older elfutils strip versions could keep the section in the main
5368 executable while splitting it for the separate debug info file. */
5369 if ((get_section_flags (section) & SEC_HAS_CONTENTS) == 0)
5370 return false;
5371
5372 dwarf2_read_section (objfile, section);
5373
5374 map.dwarf5_byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
5375
5376 const gdb_byte *addr = section->buffer;
5377
5378 bfd *const abfd = get_section_bfd_owner (section);
5379
5380 unsigned int bytes_read;
5381 LONGEST length = read_initial_length (abfd, addr, &bytes_read);
5382 addr += bytes_read;
5383
5384 map.dwarf5_is_dwarf64 = bytes_read != 4;
5385 map.offset_size = map.dwarf5_is_dwarf64 ? 8 : 4;
5386 if (bytes_read + length != section->size)
5387 {
5388 /* There may be multiple per-CU indices. */
5389 warning (_("Section .debug_names in %s length %s does not match "
5390 "section length %s, ignoring .debug_names."),
5391 filename, plongest (bytes_read + length),
5392 pulongest (section->size));
5393 return false;
5394 }
5395
5396 /* The version number. */
5397 uint16_t version = read_2_bytes (abfd, addr);
5398 addr += 2;
5399 if (version != 5)
5400 {
5401 warning (_("Section .debug_names in %s has unsupported version %d, "
5402 "ignoring .debug_names."),
5403 filename, version);
5404 return false;
5405 }
5406
5407 /* Padding. */
5408 uint16_t padding = read_2_bytes (abfd, addr);
5409 addr += 2;
5410 if (padding != 0)
5411 {
5412 warning (_("Section .debug_names in %s has unsupported padding %d, "
5413 "ignoring .debug_names."),
5414 filename, padding);
5415 return false;
5416 }
5417
5418 /* comp_unit_count - The number of CUs in the CU list. */
5419 map.cu_count = read_4_bytes (abfd, addr);
5420 addr += 4;
5421
5422 /* local_type_unit_count - The number of TUs in the local TU
5423 list. */
5424 map.tu_count = read_4_bytes (abfd, addr);
5425 addr += 4;
5426
5427 /* foreign_type_unit_count - The number of TUs in the foreign TU
5428 list. */
5429 uint32_t foreign_tu_count = read_4_bytes (abfd, addr);
5430 addr += 4;
5431 if (foreign_tu_count != 0)
5432 {
5433 warning (_("Section .debug_names in %s has unsupported %lu foreign TUs, "
5434 "ignoring .debug_names."),
5435 filename, static_cast<unsigned long> (foreign_tu_count));
5436 return false;
5437 }
5438
5439 /* bucket_count - The number of hash buckets in the hash lookup
5440 table. */
5441 map.bucket_count = read_4_bytes (abfd, addr);
5442 addr += 4;
5443
5444 /* name_count - The number of unique names in the index. */
5445 map.name_count = read_4_bytes (abfd, addr);
5446 addr += 4;
5447
5448 /* abbrev_table_size - The size in bytes of the abbreviations
5449 table. */
5450 uint32_t abbrev_table_size = read_4_bytes (abfd, addr);
5451 addr += 4;
5452
5453 /* augmentation_string_size - The size in bytes of the augmentation
5454 string. This value is rounded up to a multiple of 4. */
5455 uint32_t augmentation_string_size = read_4_bytes (abfd, addr);
5456 addr += 4;
5457 map.augmentation_is_gdb = ((augmentation_string_size
5458 == sizeof (dwarf5_augmentation))
5459 && memcmp (addr, dwarf5_augmentation,
5460 sizeof (dwarf5_augmentation)) == 0);
5461 augmentation_string_size += (-augmentation_string_size) & 3;
5462 addr += augmentation_string_size;
5463
5464 /* List of CUs */
5465 map.cu_table_reordered = addr;
5466 addr += map.cu_count * map.offset_size;
5467
5468 /* List of Local TUs */
5469 map.tu_table_reordered = addr;
5470 addr += map.tu_count * map.offset_size;
5471
5472 /* Hash Lookup Table */
5473 map.bucket_table_reordered = reinterpret_cast<const uint32_t *> (addr);
5474 addr += map.bucket_count * 4;
5475 map.hash_table_reordered = reinterpret_cast<const uint32_t *> (addr);
5476 addr += map.name_count * 4;
5477
5478 /* Name Table */
5479 map.name_table_string_offs_reordered = addr;
5480 addr += map.name_count * map.offset_size;
5481 map.name_table_entry_offs_reordered = addr;
5482 addr += map.name_count * map.offset_size;
5483
5484 const gdb_byte *abbrev_table_start = addr;
5485 for (;;)
5486 {
5487 unsigned int bytes_read;
5488 const ULONGEST index_num = read_unsigned_leb128 (abfd, addr, &bytes_read);
5489 addr += bytes_read;
5490 if (index_num == 0)
5491 break;
5492
5493 const auto insertpair
5494 = map.abbrev_map.emplace (index_num, mapped_debug_names::index_val ());
5495 if (!insertpair.second)
5496 {
5497 warning (_("Section .debug_names in %s has duplicate index %s, "
5498 "ignoring .debug_names."),
5499 filename, pulongest (index_num));
5500 return false;
5501 }
5502 mapped_debug_names::index_val &indexval = insertpair.first->second;
5503 indexval.dwarf_tag = read_unsigned_leb128 (abfd, addr, &bytes_read);
5504 addr += bytes_read;
5505
5506 for (;;)
5507 {
5508 mapped_debug_names::index_val::attr attr;
5509 attr.dw_idx = read_unsigned_leb128 (abfd, addr, &bytes_read);
5510 addr += bytes_read;
5511 attr.form = read_unsigned_leb128 (abfd, addr, &bytes_read);
5512 addr += bytes_read;
5513 if (attr.form == DW_FORM_implicit_const)
5514 {
5515 attr.implicit_const = read_signed_leb128 (abfd, addr,
5516 &bytes_read);
5517 addr += bytes_read;
5518 }
5519 if (attr.dw_idx == 0 && attr.form == 0)
5520 break;
5521 indexval.attr_vec.push_back (std::move (attr));
5522 }
5523 }
5524 if (addr != abbrev_table_start + abbrev_table_size)
5525 {
5526 warning (_("Section .debug_names in %s has abbreviation_table "
5527 "of size %zu vs. written as %u, ignoring .debug_names."),
5528 filename, addr - abbrev_table_start, abbrev_table_size);
5529 return false;
5530 }
5531 map.entry_pool = addr;
5532
5533 return true;
5534}
5535
5536/* A helper for create_cus_from_debug_names that handles the MAP's CU
5537 list. */
5538
5539static void
ed2dc618 5540create_cus_from_debug_names_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
927aa2e7
JK
5541 const mapped_debug_names &map,
5542 dwarf2_section_info &section,
b76e467d 5543 bool is_dwz)
927aa2e7
JK
5544{
5545 sect_offset sect_off_prev;
5546 for (uint32_t i = 0; i <= map.cu_count; ++i)
5547 {
5548 sect_offset sect_off_next;
5549 if (i < map.cu_count)
5550 {
5551 sect_off_next
5552 = (sect_offset) (extract_unsigned_integer
5553 (map.cu_table_reordered + i * map.offset_size,
5554 map.offset_size,
5555 map.dwarf5_byte_order));
5556 }
5557 else
5558 sect_off_next = (sect_offset) section.size;
5559 if (i >= 1)
5560 {
5561 const ULONGEST length = sect_off_next - sect_off_prev;
b76e467d 5562 dwarf2_per_cu_data *per_cu
ed2dc618 5563 = create_cu_from_index_list (dwarf2_per_objfile, &section, is_dwz,
927aa2e7 5564 sect_off_prev, length);
b76e467d 5565 dwarf2_per_objfile->all_comp_units.push_back (per_cu);
927aa2e7
JK
5566 }
5567 sect_off_prev = sect_off_next;
5568 }
5569}
5570
5571/* Read the CU list from the mapped index, and use it to create all
ed2dc618 5572 the CU objects for this dwarf2_per_objfile. */
927aa2e7
JK
5573
5574static void
ed2dc618 5575create_cus_from_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile,
927aa2e7
JK
5576 const mapped_debug_names &map,
5577 const mapped_debug_names &dwz_map)
5578{
b76e467d
SM
5579 gdb_assert (dwarf2_per_objfile->all_comp_units.empty ());
5580 dwarf2_per_objfile->all_comp_units.reserve (map.cu_count + dwz_map.cu_count);
927aa2e7 5581
ed2dc618
SM
5582 create_cus_from_debug_names_list (dwarf2_per_objfile, map,
5583 dwarf2_per_objfile->info,
b76e467d 5584 false /* is_dwz */);
927aa2e7
JK
5585
5586 if (dwz_map.cu_count == 0)
5587 return;
5588
ed2dc618
SM
5589 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
5590 create_cus_from_debug_names_list (dwarf2_per_objfile, dwz_map, dwz->info,
b76e467d 5591 true /* is_dwz */);
927aa2e7
JK
5592}
5593
5594/* Read .debug_names. If everything went ok, initialize the "quick"
5595 elements of all the CUs and return true. Otherwise, return false. */
5596
5597static bool
ed2dc618 5598dwarf2_read_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile)
927aa2e7 5599{
ed2dc618
SM
5600 mapped_debug_names local_map (dwarf2_per_objfile);
5601 mapped_debug_names dwz_map (dwarf2_per_objfile);
5602 struct objfile *objfile = dwarf2_per_objfile->objfile;
927aa2e7
JK
5603
5604 if (!read_debug_names_from_section (objfile, objfile_name (objfile),
5605 &dwarf2_per_objfile->debug_names,
5606 local_map))
5607 return false;
5608
5609 /* Don't use the index if it's empty. */
5610 if (local_map.name_count == 0)
5611 return false;
5612
5613 /* If there is a .dwz file, read it so we can get its CU list as
5614 well. */
ed2dc618 5615 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
927aa2e7
JK
5616 if (dwz != NULL)
5617 {
5618 if (!read_debug_names_from_section (objfile,
5619 bfd_get_filename (dwz->dwz_bfd),
5620 &dwz->debug_names, dwz_map))
5621 {
5622 warning (_("could not read '.debug_names' section from %s; skipping"),
5623 bfd_get_filename (dwz->dwz_bfd));
5624 return false;
5625 }
5626 }
5627
ed2dc618 5628 create_cus_from_debug_names (dwarf2_per_objfile, local_map, dwz_map);
927aa2e7
JK
5629
5630 if (local_map.tu_count != 0)
5631 {
5632 /* We can only handle a single .debug_types when we have an
5633 index. */
5634 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) != 1)
5635 return false;
5636
5637 dwarf2_section_info *section = VEC_index (dwarf2_section_info_def,
5638 dwarf2_per_objfile->types, 0);
5639
5640 create_signatured_type_table_from_debug_names
ed2dc618 5641 (dwarf2_per_objfile, local_map, section, &dwarf2_per_objfile->abbrev);
927aa2e7
JK
5642 }
5643
ed2dc618
SM
5644 create_addrmap_from_aranges (dwarf2_per_objfile,
5645 &dwarf2_per_objfile->debug_aranges);
927aa2e7 5646
ed2dc618
SM
5647 dwarf2_per_objfile->debug_names_table.reset
5648 (new mapped_debug_names (dwarf2_per_objfile));
927aa2e7
JK
5649 *dwarf2_per_objfile->debug_names_table = std::move (local_map);
5650 dwarf2_per_objfile->using_index = 1;
5651 dwarf2_per_objfile->quick_file_names_table =
b76e467d 5652 create_quick_file_names_table (dwarf2_per_objfile->all_comp_units.size ());
927aa2e7
JK
5653
5654 return true;
5655}
5656
927aa2e7
JK
5657/* Type used to manage iterating over all CUs looking for a symbol for
5658 .debug_names. */
5659
5660class dw2_debug_names_iterator
5661{
5662public:
5663 /* If WANT_SPECIFIC_BLOCK is true, only look for symbols in block
5664 BLOCK_INDEX. Otherwise BLOCK_INDEX is ignored. */
5665 dw2_debug_names_iterator (const mapped_debug_names &map,
5666 bool want_specific_block,
5667 block_enum block_index, domain_enum domain,
5668 const char *name)
5669 : m_map (map), m_want_specific_block (want_specific_block),
5670 m_block_index (block_index), m_domain (domain),
5671 m_addr (find_vec_in_debug_names (map, name))
5672 {}
5673
5674 dw2_debug_names_iterator (const mapped_debug_names &map,
5675 search_domain search, uint32_t namei)
5676 : m_map (map),
5677 m_search (search),
5678 m_addr (find_vec_in_debug_names (map, namei))
5679 {}
5680
5681 /* Return the next matching CU or NULL if there are no more. */
5682 dwarf2_per_cu_data *next ();
5683
5684private:
5685 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5686 const char *name);
5687 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5688 uint32_t namei);
5689
5690 /* The internalized form of .debug_names. */
5691 const mapped_debug_names &m_map;
5692
5693 /* If true, only look for symbols that match BLOCK_INDEX. */
5694 const bool m_want_specific_block = false;
5695
5696 /* One of GLOBAL_BLOCK or STATIC_BLOCK.
5697 Unused if !WANT_SPECIFIC_BLOCK - FIRST_LOCAL_BLOCK is an invalid
5698 value. */
5699 const block_enum m_block_index = FIRST_LOCAL_BLOCK;
5700
5701 /* The kind of symbol we're looking for. */
5702 const domain_enum m_domain = UNDEF_DOMAIN;
5703 const search_domain m_search = ALL_DOMAIN;
5704
5705 /* The list of CUs from the index entry of the symbol, or NULL if
5706 not found. */
5707 const gdb_byte *m_addr;
5708};
5709
5710const char *
5711mapped_debug_names::namei_to_name (uint32_t namei) const
5712{
5713 const ULONGEST namei_string_offs
5714 = extract_unsigned_integer ((name_table_string_offs_reordered
5715 + namei * offset_size),
5716 offset_size,
5717 dwarf5_byte_order);
5718 return read_indirect_string_at_offset
ed2dc618 5719 (dwarf2_per_objfile, dwarf2_per_objfile->objfile->obfd, namei_string_offs);
927aa2e7
JK
5720}
5721
5722/* Find a slot in .debug_names for the object named NAME. If NAME is
5723 found, return pointer to its pool data. If NAME cannot be found,
5724 return NULL. */
5725
5726const gdb_byte *
5727dw2_debug_names_iterator::find_vec_in_debug_names
5728 (const mapped_debug_names &map, const char *name)
5729{
5730 int (*cmp) (const char *, const char *);
5731
5732 if (current_language->la_language == language_cplus
5733 || current_language->la_language == language_fortran
5734 || current_language->la_language == language_d)
5735 {
5736 /* NAME is already canonical. Drop any qualifiers as
5737 .debug_names does not contain any. */
5738
5739 if (strchr (name, '(') != NULL)
5740 {
5741 gdb::unique_xmalloc_ptr<char> without_params
5742 = cp_remove_params (name);
5743
5744 if (without_params != NULL)
5745 {
5746 name = without_params.get();
5747 }
5748 }
5749 }
5750
5751 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
5752
5753 const uint32_t full_hash = dwarf5_djb_hash (name);
5754 uint32_t namei
5755 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5756 (map.bucket_table_reordered
5757 + (full_hash % map.bucket_count)), 4,
5758 map.dwarf5_byte_order);
5759 if (namei == 0)
5760 return NULL;
5761 --namei;
5762 if (namei >= map.name_count)
5763 {
5764 complaint (&symfile_complaints,
5765 _("Wrong .debug_names with name index %u but name_count=%u "
5766 "[in module %s]"),
5767 namei, map.name_count,
ed2dc618 5768 objfile_name (map.dwarf2_per_objfile->objfile));
927aa2e7
JK
5769 return NULL;
5770 }
5771
5772 for (;;)
5773 {
5774 const uint32_t namei_full_hash
5775 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5776 (map.hash_table_reordered + namei), 4,
5777 map.dwarf5_byte_order);
5778 if (full_hash % map.bucket_count != namei_full_hash % map.bucket_count)
5779 return NULL;
5780
5781 if (full_hash == namei_full_hash)
5782 {
5783 const char *const namei_string = map.namei_to_name (namei);
5784
5785#if 0 /* An expensive sanity check. */
5786 if (namei_full_hash != dwarf5_djb_hash (namei_string))
5787 {
5788 complaint (&symfile_complaints,
5789 _("Wrong .debug_names hash for string at index %u "
5790 "[in module %s]"),
5791 namei, objfile_name (dwarf2_per_objfile->objfile));
5792 return NULL;
5793 }
5794#endif
5795
5796 if (cmp (namei_string, name) == 0)
5797 {
5798 const ULONGEST namei_entry_offs
5799 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5800 + namei * map.offset_size),
5801 map.offset_size, map.dwarf5_byte_order);
5802 return map.entry_pool + namei_entry_offs;
5803 }
5804 }
5805
5806 ++namei;
5807 if (namei >= map.name_count)
5808 return NULL;
5809 }
5810}
5811
5812const gdb_byte *
5813dw2_debug_names_iterator::find_vec_in_debug_names
5814 (const mapped_debug_names &map, uint32_t namei)
5815{
5816 if (namei >= map.name_count)
5817 {
5818 complaint (&symfile_complaints,
5819 _("Wrong .debug_names with name index %u but name_count=%u "
5820 "[in module %s]"),
5821 namei, map.name_count,
ed2dc618 5822 objfile_name (map.dwarf2_per_objfile->objfile));
927aa2e7
JK
5823 return NULL;
5824 }
5825
5826 const ULONGEST namei_entry_offs
5827 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5828 + namei * map.offset_size),
5829 map.offset_size, map.dwarf5_byte_order);
5830 return map.entry_pool + namei_entry_offs;
5831}
5832
5833/* See dw2_debug_names_iterator. */
5834
5835dwarf2_per_cu_data *
5836dw2_debug_names_iterator::next ()
5837{
5838 if (m_addr == NULL)
5839 return NULL;
5840
ed2dc618
SM
5841 struct dwarf2_per_objfile *dwarf2_per_objfile = m_map.dwarf2_per_objfile;
5842 struct objfile *objfile = dwarf2_per_objfile->objfile;
5843 bfd *const abfd = objfile->obfd;
927aa2e7
JK
5844
5845 again:
5846
5847 unsigned int bytes_read;
5848 const ULONGEST abbrev = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5849 m_addr += bytes_read;
5850 if (abbrev == 0)
5851 return NULL;
5852
5853 const auto indexval_it = m_map.abbrev_map.find (abbrev);
5854 if (indexval_it == m_map.abbrev_map.cend ())
5855 {
5856 complaint (&symfile_complaints,
5857 _("Wrong .debug_names undefined abbrev code %s "
5858 "[in module %s]"),
ed2dc618 5859 pulongest (abbrev), objfile_name (objfile));
927aa2e7
JK
5860 return NULL;
5861 }
5862 const mapped_debug_names::index_val &indexval = indexval_it->second;
5863 bool have_is_static = false;
5864 bool is_static;
5865 dwarf2_per_cu_data *per_cu = NULL;
5866 for (const mapped_debug_names::index_val::attr &attr : indexval.attr_vec)
5867 {
5868 ULONGEST ull;
5869 switch (attr.form)
5870 {
5871 case DW_FORM_implicit_const:
5872 ull = attr.implicit_const;
5873 break;
5874 case DW_FORM_flag_present:
5875 ull = 1;
5876 break;
5877 case DW_FORM_udata:
5878 ull = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5879 m_addr += bytes_read;
5880 break;
5881 default:
5882 complaint (&symfile_complaints,
5883 _("Unsupported .debug_names form %s [in module %s]"),
5884 dwarf_form_name (attr.form),
ed2dc618 5885 objfile_name (objfile));
927aa2e7
JK
5886 return NULL;
5887 }
5888 switch (attr.dw_idx)
5889 {
5890 case DW_IDX_compile_unit:
5891 /* Don't crash on bad data. */
b76e467d 5892 if (ull >= dwarf2_per_objfile->all_comp_units.size ())
927aa2e7
JK
5893 {
5894 complaint (&symfile_complaints,
5895 _(".debug_names entry has bad CU index %s"
5896 " [in module %s]"),
5897 pulongest (ull),
5898 objfile_name (dwarf2_per_objfile->objfile));
5899 continue;
5900 }
ff4c9fec 5901 per_cu = dwarf2_per_objfile->get_cutu (ull);
927aa2e7 5902 break;
8af5c486
JK
5903 case DW_IDX_type_unit:
5904 /* Don't crash on bad data. */
b2bdb8cf 5905 if (ull >= dwarf2_per_objfile->all_type_units.size ())
8af5c486
JK
5906 {
5907 complaint (&symfile_complaints,
5908 _(".debug_names entry has bad TU index %s"
5909 " [in module %s]"),
5910 pulongest (ull),
5911 objfile_name (dwarf2_per_objfile->objfile));
5912 continue;
5913 }
ff4c9fec 5914 per_cu = &dwarf2_per_objfile->get_tu (ull)->per_cu;
8af5c486 5915 break;
927aa2e7
JK
5916 case DW_IDX_GNU_internal:
5917 if (!m_map.augmentation_is_gdb)
5918 break;
5919 have_is_static = true;
5920 is_static = true;
5921 break;
5922 case DW_IDX_GNU_external:
5923 if (!m_map.augmentation_is_gdb)
5924 break;
5925 have_is_static = true;
5926 is_static = false;
5927 break;
5928 }
5929 }
5930
5931 /* Skip if already read in. */
5932 if (per_cu->v.quick->compunit_symtab)
5933 goto again;
5934
5935 /* Check static vs global. */
5936 if (have_is_static)
5937 {
5938 const bool want_static = m_block_index != GLOBAL_BLOCK;
5939 if (m_want_specific_block && want_static != is_static)
5940 goto again;
5941 }
5942
5943 /* Match dw2_symtab_iter_next, symbol_kind
5944 and debug_names::psymbol_tag. */
5945 switch (m_domain)
5946 {
5947 case VAR_DOMAIN:
5948 switch (indexval.dwarf_tag)
5949 {
5950 case DW_TAG_variable:
5951 case DW_TAG_subprogram:
5952 /* Some types are also in VAR_DOMAIN. */
5953 case DW_TAG_typedef:
5954 case DW_TAG_structure_type:
5955 break;
5956 default:
5957 goto again;
5958 }
5959 break;
5960 case STRUCT_DOMAIN:
5961 switch (indexval.dwarf_tag)
5962 {
5963 case DW_TAG_typedef:
5964 case DW_TAG_structure_type:
5965 break;
5966 default:
5967 goto again;
5968 }
5969 break;
5970 case LABEL_DOMAIN:
5971 switch (indexval.dwarf_tag)
5972 {
5973 case 0:
5974 case DW_TAG_variable:
5975 break;
5976 default:
5977 goto again;
5978 }
5979 break;
5980 default:
5981 break;
5982 }
5983
5984 /* Match dw2_expand_symtabs_matching, symbol_kind and
5985 debug_names::psymbol_tag. */
5986 switch (m_search)
4b514bc8 5987 {
927aa2e7
JK
5988 case VARIABLES_DOMAIN:
5989 switch (indexval.dwarf_tag)
4b514bc8 5990 {
927aa2e7
JK
5991 case DW_TAG_variable:
5992 break;
5993 default:
5994 goto again;
4b514bc8 5995 }
927aa2e7
JK
5996 break;
5997 case FUNCTIONS_DOMAIN:
5998 switch (indexval.dwarf_tag)
4b514bc8 5999 {
927aa2e7
JK
6000 case DW_TAG_subprogram:
6001 break;
6002 default:
6003 goto again;
4b514bc8 6004 }
927aa2e7
JK
6005 break;
6006 case TYPES_DOMAIN:
6007 switch (indexval.dwarf_tag)
6008 {
6009 case DW_TAG_typedef:
6010 case DW_TAG_structure_type:
6011 break;
6012 default:
6013 goto again;
6014 }
6015 break;
6016 default:
6017 break;
4b514bc8 6018 }
927aa2e7
JK
6019
6020 return per_cu;
4b514bc8 6021}
61920122 6022
927aa2e7
JK
6023static struct compunit_symtab *
6024dw2_debug_names_lookup_symbol (struct objfile *objfile, int block_index_int,
6025 const char *name, domain_enum domain)
4b514bc8 6026{
927aa2e7 6027 const block_enum block_index = static_cast<block_enum> (block_index_int);
ed2dc618
SM
6028 struct dwarf2_per_objfile *dwarf2_per_objfile
6029 = get_dwarf2_per_objfile (objfile);
61920122 6030
927aa2e7
JK
6031 const auto &mapp = dwarf2_per_objfile->debug_names_table;
6032 if (!mapp)
61920122 6033 {
927aa2e7
JK
6034 /* index is NULL if OBJF_READNOW. */
6035 return NULL;
6036 }
6037 const auto &map = *mapp;
9291a0cd 6038
927aa2e7
JK
6039 dw2_debug_names_iterator iter (map, true /* want_specific_block */,
6040 block_index, domain, name);
9703b513 6041
927aa2e7
JK
6042 struct compunit_symtab *stab_best = NULL;
6043 struct dwarf2_per_cu_data *per_cu;
6044 while ((per_cu = iter.next ()) != NULL)
6045 {
6046 struct symbol *sym, *with_opaque = NULL;
58f0c718 6047 struct compunit_symtab *stab = dw2_instantiate_symtab (per_cu, false);
927aa2e7
JK
6048 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
6049 struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
9703b513 6050
927aa2e7
JK
6051 sym = block_find_symbol (block, name, domain,
6052 block_find_non_opaque_type_preferred,
6053 &with_opaque);
9703b513 6054
927aa2e7
JK
6055 /* Some caution must be observed with overloaded functions and
6056 methods, since the index will not contain any overload
6057 information (but NAME might contain it). */
a3ec0bb1 6058
927aa2e7
JK
6059 if (sym != NULL
6060 && strcmp_iw (SYMBOL_SEARCH_NAME (sym), name) == 0)
6061 return stab;
6062 if (with_opaque != NULL
6063 && strcmp_iw (SYMBOL_SEARCH_NAME (with_opaque), name) == 0)
6064 stab_best = stab;
9703b513 6065
927aa2e7 6066 /* Keep looking through other CUs. */
9703b513
TT
6067 }
6068
927aa2e7 6069 return stab_best;
9703b513
TT
6070}
6071
927aa2e7
JK
6072/* This dumps minimal information about .debug_names. It is called
6073 via "mt print objfiles". The gdb.dwarf2/gdb-index.exp testcase
6074 uses this to verify that .debug_names has been loaded. */
9291a0cd 6075
927aa2e7
JK
6076static void
6077dw2_debug_names_dump (struct objfile *objfile)
6078{
ed2dc618
SM
6079 struct dwarf2_per_objfile *dwarf2_per_objfile
6080 = get_dwarf2_per_objfile (objfile);
6081
927aa2e7
JK
6082 gdb_assert (dwarf2_per_objfile->using_index);
6083 printf_filtered (".debug_names:");
6084 if (dwarf2_per_objfile->debug_names_table)
6085 printf_filtered (" exists\n");
6086 else
6087 printf_filtered (" faked for \"readnow\"\n");
6088 printf_filtered ("\n");
9291a0cd
TT
6089}
6090
9291a0cd 6091static void
927aa2e7
JK
6092dw2_debug_names_expand_symtabs_for_function (struct objfile *objfile,
6093 const char *func_name)
9291a0cd 6094{
ed2dc618
SM
6095 struct dwarf2_per_objfile *dwarf2_per_objfile
6096 = get_dwarf2_per_objfile (objfile);
ae2de4f8 6097
927aa2e7
JK
6098 /* dwarf2_per_objfile->debug_names_table is NULL if OBJF_READNOW. */
6099 if (dwarf2_per_objfile->debug_names_table)
24c79950 6100 {
927aa2e7 6101 const mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
24c79950 6102
927aa2e7
JK
6103 /* Note: It doesn't matter what we pass for block_index here. */
6104 dw2_debug_names_iterator iter (map, false /* want_specific_block */,
6105 GLOBAL_BLOCK, VAR_DOMAIN, func_name);
24c79950 6106
927aa2e7
JK
6107 struct dwarf2_per_cu_data *per_cu;
6108 while ((per_cu = iter.next ()) != NULL)
58f0c718 6109 dw2_instantiate_symtab (per_cu, false);
927aa2e7
JK
6110 }
6111}
24c79950 6112
927aa2e7
JK
6113static void
6114dw2_debug_names_expand_symtabs_matching
6115 (struct objfile *objfile,
6116 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
6117 const lookup_name_info &lookup_name,
6118 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
6119 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
6120 enum search_domain kind)
6121{
ed2dc618
SM
6122 struct dwarf2_per_objfile *dwarf2_per_objfile
6123 = get_dwarf2_per_objfile (objfile);
9291a0cd 6124
927aa2e7
JK
6125 /* debug_names_table is NULL if OBJF_READNOW. */
6126 if (!dwarf2_per_objfile->debug_names_table)
6127 return;
9291a0cd 6128
ed2dc618 6129 dw_expand_symtabs_matching_file_matcher (dwarf2_per_objfile, file_matcher);
24c79950 6130
44ed8f3e 6131 mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
bbf2f4df 6132
44ed8f3e
PA
6133 dw2_expand_symtabs_matching_symbol (map, lookup_name,
6134 symbol_matcher,
6135 kind, [&] (offset_type namei)
927aa2e7 6136 {
927aa2e7
JK
6137 /* The name was matched, now expand corresponding CUs that were
6138 marked. */
6139 dw2_debug_names_iterator iter (map, kind, namei);
bbf2f4df 6140
927aa2e7
JK
6141 struct dwarf2_per_cu_data *per_cu;
6142 while ((per_cu = iter.next ()) != NULL)
6143 dw2_expand_symtabs_matching_one (per_cu, file_matcher,
6144 expansion_notify);
44ed8f3e 6145 });
9291a0cd
TT
6146}
6147
927aa2e7 6148const struct quick_symbol_functions dwarf2_debug_names_functions =
9291a0cd
TT
6149{
6150 dw2_has_symbols,
6151 dw2_find_last_source_symtab,
6152 dw2_forget_cached_source_info,
f8eba3c6 6153 dw2_map_symtabs_matching_filename,
927aa2e7 6154 dw2_debug_names_lookup_symbol,
9291a0cd 6155 dw2_print_stats,
927aa2e7 6156 dw2_debug_names_dump,
9291a0cd 6157 dw2_relocate,
927aa2e7 6158 dw2_debug_names_expand_symtabs_for_function,
9291a0cd 6159 dw2_expand_all_symtabs,
652a8996 6160 dw2_expand_symtabs_with_fullname,
40658b94 6161 dw2_map_matching_symbols,
927aa2e7 6162 dw2_debug_names_expand_symtabs_matching,
43f3e411 6163 dw2_find_pc_sect_compunit_symtab,
71a3c369 6164 NULL,
9291a0cd
TT
6165 dw2_map_symbol_filenames
6166};
6167
3c0aa29a 6168/* See symfile.h. */
9291a0cd 6169
3c0aa29a
PA
6170bool
6171dwarf2_initialize_objfile (struct objfile *objfile, dw_index_kind *index_kind)
9291a0cd 6172{
ed2dc618
SM
6173 struct dwarf2_per_objfile *dwarf2_per_objfile
6174 = get_dwarf2_per_objfile (objfile);
6175
9291a0cd
TT
6176 /* If we're about to read full symbols, don't bother with the
6177 indices. In this case we also don't care if some other debug
6178 format is making psymtabs, because they are all about to be
6179 expanded anyway. */
6180 if ((objfile->flags & OBJF_READNOW))
6181 {
9291a0cd 6182 dwarf2_per_objfile->using_index = 1;
ed2dc618
SM
6183 create_all_comp_units (dwarf2_per_objfile);
6184 create_all_type_units (dwarf2_per_objfile);
b76e467d
SM
6185 dwarf2_per_objfile->quick_file_names_table
6186 = create_quick_file_names_table
6187 (dwarf2_per_objfile->all_comp_units.size ());
9291a0cd 6188
b76e467d 6189 for (int i = 0; i < (dwarf2_per_objfile->all_comp_units.size ()
b2bdb8cf 6190 + dwarf2_per_objfile->all_type_units.size ()); ++i)
9291a0cd 6191 {
ff4c9fec 6192 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (i);
9291a0cd 6193
e254ef6a
DE
6194 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6195 struct dwarf2_per_cu_quick_data);
9291a0cd
TT
6196 }
6197
6198 /* Return 1 so that gdb sees the "quick" functions. However,
6199 these functions will be no-ops because we will have expanded
6200 all symtabs. */
3c0aa29a
PA
6201 *index_kind = dw_index_kind::GDB_INDEX;
6202 return true;
9291a0cd
TT
6203 }
6204
ed2dc618 6205 if (dwarf2_read_debug_names (dwarf2_per_objfile))
3c0aa29a
PA
6206 {
6207 *index_kind = dw_index_kind::DEBUG_NAMES;
6208 return true;
6209 }
927aa2e7 6210
12359b5e 6211 if (dwarf2_read_index (dwarf2_per_objfile))
3c0aa29a
PA
6212 {
6213 *index_kind = dw_index_kind::GDB_INDEX;
6214 return true;
6215 }
9291a0cd 6216
3c0aa29a 6217 return false;
9291a0cd
TT
6218}
6219
6220\f
6221
dce234bc
PP
6222/* Build a partial symbol table. */
6223
6224void
f29dff0a 6225dwarf2_build_psymtabs (struct objfile *objfile)
dce234bc 6226{
ed2dc618
SM
6227 struct dwarf2_per_objfile *dwarf2_per_objfile
6228 = get_dwarf2_per_objfile (objfile);
c9bf0622 6229
af5bf4ad
SM
6230 if (objfile->global_psymbols.capacity () == 0
6231 && objfile->static_psymbols.capacity () == 0)
6232 init_psymbol_list (objfile, 1024);
c906108c 6233
492d29ea 6234 TRY
c9bf0622
TT
6235 {
6236 /* This isn't really ideal: all the data we allocate on the
6237 objfile's obstack is still uselessly kept around. However,
6238 freeing it seems unsafe. */
906768f9 6239 psymtab_discarder psymtabs (objfile);
ed2dc618 6240 dwarf2_build_psymtabs_hard (dwarf2_per_objfile);
906768f9 6241 psymtabs.keep ();
c9bf0622 6242 }
492d29ea
PA
6243 CATCH (except, RETURN_MASK_ERROR)
6244 {
6245 exception_print (gdb_stderr, except);
6246 }
6247 END_CATCH
c906108c 6248}
c906108c 6249
1ce1cefd
DE
6250/* Return the total length of the CU described by HEADER. */
6251
6252static unsigned int
6253get_cu_length (const struct comp_unit_head *header)
6254{
6255 return header->initial_length_size + header->length;
6256}
6257
9c541725 6258/* Return TRUE if SECT_OFF is within CU_HEADER. */
45452591 6259
9c541725
PA
6260static inline bool
6261offset_in_cu_p (const comp_unit_head *cu_header, sect_offset sect_off)
45452591 6262{
9c541725
PA
6263 sect_offset bottom = cu_header->sect_off;
6264 sect_offset top = cu_header->sect_off + get_cu_length (cu_header);
9a619af0 6265
9c541725 6266 return sect_off >= bottom && sect_off < top;
45452591
DE
6267}
6268
3b80fe9b
DE
6269/* Find the base address of the compilation unit for range lists and
6270 location lists. It will normally be specified by DW_AT_low_pc.
6271 In DWARF-3 draft 4, the base address could be overridden by
6272 DW_AT_entry_pc. It's been removed, but GCC still uses this for
6273 compilation units with discontinuous ranges. */
6274
6275static void
6276dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
6277{
6278 struct attribute *attr;
6279
6280 cu->base_known = 0;
6281 cu->base_address = 0;
6282
6283 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
6284 if (attr)
6285 {
31aa7e4e 6286 cu->base_address = attr_value_as_address (attr);
3b80fe9b
DE
6287 cu->base_known = 1;
6288 }
6289 else
6290 {
6291 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
6292 if (attr)
6293 {
31aa7e4e 6294 cu->base_address = attr_value_as_address (attr);
3b80fe9b
DE
6295 cu->base_known = 1;
6296 }
6297 }
6298}
6299
93311388 6300/* Read in the comp unit header information from the debug_info at info_ptr.
43988095 6301 Use rcuh_kind::COMPILE as the default type if not known by the caller.
93311388
DE
6302 NOTE: This leaves members offset, first_die_offset to be filled in
6303 by the caller. */
107d2387 6304
d521ce57 6305static const gdb_byte *
107d2387 6306read_comp_unit_head (struct comp_unit_head *cu_header,
43988095
JK
6307 const gdb_byte *info_ptr,
6308 struct dwarf2_section_info *section,
6309 rcuh_kind section_kind)
107d2387
AC
6310{
6311 int signed_addr;
891d2f0b 6312 unsigned int bytes_read;
43988095
JK
6313 const char *filename = get_section_file_name (section);
6314 bfd *abfd = get_section_bfd_owner (section);
c764a876
DE
6315
6316 cu_header->length = read_initial_length (abfd, info_ptr, &bytes_read);
6317 cu_header->initial_length_size = bytes_read;
6318 cu_header->offset_size = (bytes_read == 4) ? 4 : 8;
613e1657 6319 info_ptr += bytes_read;
107d2387
AC
6320 cu_header->version = read_2_bytes (abfd, info_ptr);
6321 info_ptr += 2;
43988095
JK
6322 if (cu_header->version < 5)
6323 switch (section_kind)
6324 {
6325 case rcuh_kind::COMPILE:
6326 cu_header->unit_type = DW_UT_compile;
6327 break;
6328 case rcuh_kind::TYPE:
6329 cu_header->unit_type = DW_UT_type;
6330 break;
6331 default:
6332 internal_error (__FILE__, __LINE__,
6333 _("read_comp_unit_head: invalid section_kind"));
6334 }
6335 else
6336 {
6337 cu_header->unit_type = static_cast<enum dwarf_unit_type>
6338 (read_1_byte (abfd, info_ptr));
6339 info_ptr += 1;
6340 switch (cu_header->unit_type)
6341 {
6342 case DW_UT_compile:
6343 if (section_kind != rcuh_kind::COMPILE)
6344 error (_("Dwarf Error: wrong unit_type in compilation unit header "
6345 "(is DW_UT_compile, should be DW_UT_type) [in module %s]"),
6346 filename);
6347 break;
6348 case DW_UT_type:
6349 section_kind = rcuh_kind::TYPE;
6350 break;
6351 default:
6352 error (_("Dwarf Error: wrong unit_type in compilation unit header "
6353 "(is %d, should be %d or %d) [in module %s]"),
6354 cu_header->unit_type, DW_UT_compile, DW_UT_type, filename);
6355 }
6356
6357 cu_header->addr_size = read_1_byte (abfd, info_ptr);
6358 info_ptr += 1;
6359 }
9c541725
PA
6360 cu_header->abbrev_sect_off = (sect_offset) read_offset (abfd, info_ptr,
6361 cu_header,
6362 &bytes_read);
613e1657 6363 info_ptr += bytes_read;
43988095
JK
6364 if (cu_header->version < 5)
6365 {
6366 cu_header->addr_size = read_1_byte (abfd, info_ptr);
6367 info_ptr += 1;
6368 }
107d2387
AC
6369 signed_addr = bfd_get_sign_extend_vma (abfd);
6370 if (signed_addr < 0)
8e65ff28 6371 internal_error (__FILE__, __LINE__,
e2e0b3e5 6372 _("read_comp_unit_head: dwarf from non elf file"));
107d2387 6373 cu_header->signed_addr_p = signed_addr;
c764a876 6374
43988095
JK
6375 if (section_kind == rcuh_kind::TYPE)
6376 {
6377 LONGEST type_offset;
6378
6379 cu_header->signature = read_8_bytes (abfd, info_ptr);
6380 info_ptr += 8;
6381
6382 type_offset = read_offset (abfd, info_ptr, cu_header, &bytes_read);
6383 info_ptr += bytes_read;
9c541725
PA
6384 cu_header->type_cu_offset_in_tu = (cu_offset) type_offset;
6385 if (to_underlying (cu_header->type_cu_offset_in_tu) != type_offset)
43988095
JK
6386 error (_("Dwarf Error: Too big type_offset in compilation unit "
6387 "header (is %s) [in module %s]"), plongest (type_offset),
6388 filename);
6389 }
6390
107d2387
AC
6391 return info_ptr;
6392}
6393
36586728
TT
6394/* Helper function that returns the proper abbrev section for
6395 THIS_CU. */
6396
6397static struct dwarf2_section_info *
6398get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
6399{
6400 struct dwarf2_section_info *abbrev;
ed2dc618 6401 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
36586728
TT
6402
6403 if (this_cu->is_dwz)
ed2dc618 6404 abbrev = &dwarf2_get_dwz_file (dwarf2_per_objfile)->abbrev;
36586728
TT
6405 else
6406 abbrev = &dwarf2_per_objfile->abbrev;
6407
6408 return abbrev;
6409}
6410
9ff913ba
DE
6411/* Subroutine of read_and_check_comp_unit_head and
6412 read_and_check_type_unit_head to simplify them.
6413 Perform various error checking on the header. */
6414
6415static void
ed2dc618
SM
6416error_check_comp_unit_head (struct dwarf2_per_objfile *dwarf2_per_objfile,
6417 struct comp_unit_head *header,
4bdcc0c1
DE
6418 struct dwarf2_section_info *section,
6419 struct dwarf2_section_info *abbrev_section)
9ff913ba 6420{
a32a8923 6421 const char *filename = get_section_file_name (section);
9ff913ba 6422
43988095 6423 if (header->version < 2 || header->version > 5)
9ff913ba 6424 error (_("Dwarf Error: wrong version in compilation unit header "
43988095 6425 "(is %d, should be 2, 3, 4 or 5) [in module %s]"), header->version,
9ff913ba
DE
6426 filename);
6427
9c541725 6428 if (to_underlying (header->abbrev_sect_off)
36586728 6429 >= dwarf2_section_size (dwarf2_per_objfile->objfile, abbrev_section))
9d8780f0
SM
6430 error (_("Dwarf Error: bad offset (%s) in compilation unit header "
6431 "(offset %s + 6) [in module %s]"),
6432 sect_offset_str (header->abbrev_sect_off),
6433 sect_offset_str (header->sect_off),
9ff913ba
DE
6434 filename);
6435
9c541725 6436 /* Cast to ULONGEST to use 64-bit arithmetic when possible to
9ff913ba 6437 avoid potential 32-bit overflow. */
9c541725 6438 if (((ULONGEST) header->sect_off + get_cu_length (header))
9ff913ba 6439 > section->size)
9c541725 6440 error (_("Dwarf Error: bad length (0x%x) in compilation unit header "
9d8780f0
SM
6441 "(offset %s + 0) [in module %s]"),
6442 header->length, sect_offset_str (header->sect_off),
9ff913ba
DE
6443 filename);
6444}
6445
6446/* Read in a CU/TU header and perform some basic error checking.
6447 The contents of the header are stored in HEADER.
6448 The result is a pointer to the start of the first DIE. */
adabb602 6449
d521ce57 6450static const gdb_byte *
ed2dc618
SM
6451read_and_check_comp_unit_head (struct dwarf2_per_objfile *dwarf2_per_objfile,
6452 struct comp_unit_head *header,
9ff913ba 6453 struct dwarf2_section_info *section,
4bdcc0c1 6454 struct dwarf2_section_info *abbrev_section,
d521ce57 6455 const gdb_byte *info_ptr,
43988095 6456 rcuh_kind section_kind)
72bf9492 6457{
d521ce57 6458 const gdb_byte *beg_of_comp_unit = info_ptr;
72bf9492 6459
9c541725 6460 header->sect_off = (sect_offset) (beg_of_comp_unit - section->buffer);
adabb602 6461
43988095 6462 info_ptr = read_comp_unit_head (header, info_ptr, section, section_kind);
9ff913ba 6463
9c541725 6464 header->first_die_cu_offset = (cu_offset) (info_ptr - beg_of_comp_unit);
348e048f 6465
ed2dc618
SM
6466 error_check_comp_unit_head (dwarf2_per_objfile, header, section,
6467 abbrev_section);
9ff913ba
DE
6468
6469 return info_ptr;
348e048f
DE
6470}
6471
f4dc4d17
DE
6472/* Fetch the abbreviation table offset from a comp or type unit header. */
6473
6474static sect_offset
ed2dc618
SM
6475read_abbrev_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
6476 struct dwarf2_section_info *section,
9c541725 6477 sect_offset sect_off)
f4dc4d17 6478{
a32a8923 6479 bfd *abfd = get_section_bfd_owner (section);
d521ce57 6480 const gdb_byte *info_ptr;
ac298888 6481 unsigned int initial_length_size, offset_size;
43988095 6482 uint16_t version;
f4dc4d17
DE
6483
6484 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
9c541725 6485 info_ptr = section->buffer + to_underlying (sect_off);
ac298888 6486 read_initial_length (abfd, info_ptr, &initial_length_size);
f4dc4d17 6487 offset_size = initial_length_size == 4 ? 4 : 8;
43988095
JK
6488 info_ptr += initial_length_size;
6489
6490 version = read_2_bytes (abfd, info_ptr);
6491 info_ptr += 2;
6492 if (version >= 5)
6493 {
6494 /* Skip unit type and address size. */
6495 info_ptr += 2;
6496 }
6497
9c541725 6498 return (sect_offset) read_offset_1 (abfd, info_ptr, offset_size);
f4dc4d17
DE
6499}
6500
aaa75496
JB
6501/* Allocate a new partial symtab for file named NAME and mark this new
6502 partial symtab as being an include of PST. */
6503
6504static void
d521ce57 6505dwarf2_create_include_psymtab (const char *name, struct partial_symtab *pst,
aaa75496
JB
6506 struct objfile *objfile)
6507{
6508 struct partial_symtab *subpst = allocate_psymtab (name, objfile);
6509
fbd9ab74
JK
6510 if (!IS_ABSOLUTE_PATH (subpst->filename))
6511 {
6512 /* It shares objfile->objfile_obstack. */
6513 subpst->dirname = pst->dirname;
6514 }
6515
aaa75496
JB
6516 subpst->textlow = 0;
6517 subpst->texthigh = 0;
6518
8d749320
SM
6519 subpst->dependencies
6520 = XOBNEW (&objfile->objfile_obstack, struct partial_symtab *);
aaa75496
JB
6521 subpst->dependencies[0] = pst;
6522 subpst->number_of_dependencies = 1;
6523
6524 subpst->globals_offset = 0;
6525 subpst->n_global_syms = 0;
6526 subpst->statics_offset = 0;
6527 subpst->n_static_syms = 0;
43f3e411 6528 subpst->compunit_symtab = NULL;
aaa75496
JB
6529 subpst->read_symtab = pst->read_symtab;
6530 subpst->readin = 0;
6531
6532 /* No private part is necessary for include psymtabs. This property
6533 can be used to differentiate between such include psymtabs and
10b3939b 6534 the regular ones. */
58a9656e 6535 subpst->read_symtab_private = NULL;
aaa75496
JB
6536}
6537
6538/* Read the Line Number Program data and extract the list of files
6539 included by the source file represented by PST. Build an include
d85a05f0 6540 partial symtab for each of these included files. */
aaa75496
JB
6541
6542static void
6543dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
dee91e82
DE
6544 struct die_info *die,
6545 struct partial_symtab *pst)
aaa75496 6546{
fff8551c 6547 line_header_up lh;
d85a05f0 6548 struct attribute *attr;
aaa75496 6549
d85a05f0
DJ
6550 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
6551 if (attr)
9c541725 6552 lh = dwarf_decode_line_header ((sect_offset) DW_UNSND (attr), cu);
aaa75496
JB
6553 if (lh == NULL)
6554 return; /* No linetable, so no includes. */
6555
c6da4cef 6556 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). */
fff8551c 6557 dwarf_decode_lines (lh.get (), pst->dirname, cu, pst, pst->textlow, 1);
aaa75496
JB
6558}
6559
348e048f 6560static hashval_t
52dc124a 6561hash_signatured_type (const void *item)
348e048f 6562{
9a3c8263
SM
6563 const struct signatured_type *sig_type
6564 = (const struct signatured_type *) item;
9a619af0 6565
348e048f 6566 /* This drops the top 32 bits of the signature, but is ok for a hash. */
52dc124a 6567 return sig_type->signature;
348e048f
DE
6568}
6569
6570static int
52dc124a 6571eq_signatured_type (const void *item_lhs, const void *item_rhs)
348e048f 6572{
9a3c8263
SM
6573 const struct signatured_type *lhs = (const struct signatured_type *) item_lhs;
6574 const struct signatured_type *rhs = (const struct signatured_type *) item_rhs;
9a619af0 6575
348e048f
DE
6576 return lhs->signature == rhs->signature;
6577}
6578
1fd400ff
TT
6579/* Allocate a hash table for signatured types. */
6580
6581static htab_t
673bfd45 6582allocate_signatured_type_table (struct objfile *objfile)
1fd400ff
TT
6583{
6584 return htab_create_alloc_ex (41,
52dc124a
DE
6585 hash_signatured_type,
6586 eq_signatured_type,
1fd400ff
TT
6587 NULL,
6588 &objfile->objfile_obstack,
6589 hashtab_obstack_allocate,
6590 dummy_obstack_deallocate);
6591}
6592
d467dd73 6593/* A helper function to add a signatured type CU to a table. */
1fd400ff
TT
6594
6595static int
d467dd73 6596add_signatured_type_cu_to_table (void **slot, void *datum)
1fd400ff 6597{
9a3c8263 6598 struct signatured_type *sigt = (struct signatured_type *) *slot;
b2bdb8cf
SM
6599 std::vector<signatured_type *> *all_type_units
6600 = (std::vector<signatured_type *> *) datum;
1fd400ff 6601
b2bdb8cf 6602 all_type_units->push_back (sigt);
1fd400ff
TT
6603
6604 return 1;
6605}
6606
78d4d2c5 6607/* A helper for create_debug_types_hash_table. Read types from SECTION
43988095
JK
6608 and fill them into TYPES_HTAB. It will process only type units,
6609 therefore DW_UT_type. */
c88ee1f0 6610
78d4d2c5 6611static void
ed2dc618
SM
6612create_debug_type_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
6613 struct dwo_file *dwo_file,
43988095
JK
6614 dwarf2_section_info *section, htab_t &types_htab,
6615 rcuh_kind section_kind)
348e048f 6616{
3019eac3 6617 struct objfile *objfile = dwarf2_per_objfile->objfile;
4bdcc0c1 6618 struct dwarf2_section_info *abbrev_section;
78d4d2c5
JK
6619 bfd *abfd;
6620 const gdb_byte *info_ptr, *end_ptr;
348e048f 6621
4bdcc0c1
DE
6622 abbrev_section = (dwo_file != NULL
6623 ? &dwo_file->sections.abbrev
6624 : &dwarf2_per_objfile->abbrev);
6625
b4f54984 6626 if (dwarf_read_debug)
43988095
JK
6627 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
6628 get_section_name (section),
a32a8923 6629 get_section_file_name (abbrev_section));
09406207 6630
78d4d2c5
JK
6631 dwarf2_read_section (objfile, section);
6632 info_ptr = section->buffer;
348e048f 6633
78d4d2c5
JK
6634 if (info_ptr == NULL)
6635 return;
348e048f 6636
78d4d2c5
JK
6637 /* We can't set abfd until now because the section may be empty or
6638 not present, in which case the bfd is unknown. */
6639 abfd = get_section_bfd_owner (section);
348e048f 6640
78d4d2c5
JK
6641 /* We don't use init_cutu_and_read_dies_simple, or some such, here
6642 because we don't need to read any dies: the signature is in the
6643 header. */
3019eac3 6644
78d4d2c5
JK
6645 end_ptr = info_ptr + section->size;
6646 while (info_ptr < end_ptr)
6647 {
78d4d2c5
JK
6648 struct signatured_type *sig_type;
6649 struct dwo_unit *dwo_tu;
6650 void **slot;
6651 const gdb_byte *ptr = info_ptr;
6652 struct comp_unit_head header;
6653 unsigned int length;
8b70b953 6654
9c541725 6655 sect_offset sect_off = (sect_offset) (ptr - section->buffer);
348e048f 6656
a49dd8dd
JK
6657 /* Initialize it due to a false compiler warning. */
6658 header.signature = -1;
9c541725 6659 header.type_cu_offset_in_tu = (cu_offset) -1;
a49dd8dd 6660
78d4d2c5
JK
6661 /* We need to read the type's signature in order to build the hash
6662 table, but we don't need anything else just yet. */
348e048f 6663
ed2dc618 6664 ptr = read_and_check_comp_unit_head (dwarf2_per_objfile, &header, section,
43988095 6665 abbrev_section, ptr, section_kind);
348e048f 6666
78d4d2c5 6667 length = get_cu_length (&header);
6caca83c 6668
78d4d2c5
JK
6669 /* Skip dummy type units. */
6670 if (ptr >= info_ptr + length
43988095
JK
6671 || peek_abbrev_code (abfd, ptr) == 0
6672 || header.unit_type != DW_UT_type)
78d4d2c5
JK
6673 {
6674 info_ptr += length;
6675 continue;
6676 }
dee91e82 6677
78d4d2c5
JK
6678 if (types_htab == NULL)
6679 {
6680 if (dwo_file)
6681 types_htab = allocate_dwo_unit_table (objfile);
6682 else
6683 types_htab = allocate_signatured_type_table (objfile);
6684 }
8b70b953 6685
78d4d2c5
JK
6686 if (dwo_file)
6687 {
6688 sig_type = NULL;
6689 dwo_tu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6690 struct dwo_unit);
6691 dwo_tu->dwo_file = dwo_file;
43988095 6692 dwo_tu->signature = header.signature;
9c541725 6693 dwo_tu->type_offset_in_tu = header.type_cu_offset_in_tu;
78d4d2c5 6694 dwo_tu->section = section;
9c541725 6695 dwo_tu->sect_off = sect_off;
78d4d2c5
JK
6696 dwo_tu->length = length;
6697 }
6698 else
6699 {
6700 /* N.B.: type_offset is not usable if this type uses a DWO file.
6701 The real type_offset is in the DWO file. */
6702 dwo_tu = NULL;
6703 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6704 struct signatured_type);
43988095 6705 sig_type->signature = header.signature;
9c541725 6706 sig_type->type_offset_in_tu = header.type_cu_offset_in_tu;
e3b94546 6707 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
78d4d2c5
JK
6708 sig_type->per_cu.is_debug_types = 1;
6709 sig_type->per_cu.section = section;
9c541725 6710 sig_type->per_cu.sect_off = sect_off;
78d4d2c5
JK
6711 sig_type->per_cu.length = length;
6712 }
6713
6714 slot = htab_find_slot (types_htab,
6715 dwo_file ? (void*) dwo_tu : (void *) sig_type,
6716 INSERT);
6717 gdb_assert (slot != NULL);
6718 if (*slot != NULL)
6719 {
9c541725 6720 sect_offset dup_sect_off;
0349ea22 6721
3019eac3
DE
6722 if (dwo_file)
6723 {
78d4d2c5
JK
6724 const struct dwo_unit *dup_tu
6725 = (const struct dwo_unit *) *slot;
6726
9c541725 6727 dup_sect_off = dup_tu->sect_off;
3019eac3
DE
6728 }
6729 else
6730 {
78d4d2c5
JK
6731 const struct signatured_type *dup_tu
6732 = (const struct signatured_type *) *slot;
6733
9c541725 6734 dup_sect_off = dup_tu->per_cu.sect_off;
3019eac3 6735 }
8b70b953 6736
78d4d2c5 6737 complaint (&symfile_complaints,
9d8780f0
SM
6738 _("debug type entry at offset %s is duplicate to"
6739 " the entry at offset %s, signature %s"),
6740 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
43988095 6741 hex_string (header.signature));
78d4d2c5
JK
6742 }
6743 *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
3019eac3 6744
78d4d2c5 6745 if (dwarf_read_debug > 1)
9d8780f0
SM
6746 fprintf_unfiltered (gdb_stdlog, " offset %s, signature %s\n",
6747 sect_offset_str (sect_off),
43988095 6748 hex_string (header.signature));
3019eac3 6749
78d4d2c5
JK
6750 info_ptr += length;
6751 }
6752}
3019eac3 6753
78d4d2c5
JK
6754/* Create the hash table of all entries in the .debug_types
6755 (or .debug_types.dwo) section(s).
6756 If reading a DWO file, then DWO_FILE is a pointer to the DWO file object,
6757 otherwise it is NULL.
b3c8eb43 6758
78d4d2c5 6759 The result is a pointer to the hash table or NULL if there are no types.
348e048f 6760
78d4d2c5 6761 Note: This function processes DWO files only, not DWP files. */
348e048f 6762
78d4d2c5 6763static void
ed2dc618
SM
6764create_debug_types_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
6765 struct dwo_file *dwo_file,
78d4d2c5
JK
6766 VEC (dwarf2_section_info_def) *types,
6767 htab_t &types_htab)
6768{
6769 int ix;
6770 struct dwarf2_section_info *section;
6771
6772 if (VEC_empty (dwarf2_section_info_def, types))
6773 return;
348e048f 6774
78d4d2c5
JK
6775 for (ix = 0;
6776 VEC_iterate (dwarf2_section_info_def, types, ix, section);
6777 ++ix)
ed2dc618
SM
6778 create_debug_type_hash_table (dwarf2_per_objfile, dwo_file, section,
6779 types_htab, rcuh_kind::TYPE);
3019eac3
DE
6780}
6781
6782/* Create the hash table of all entries in the .debug_types section,
6783 and initialize all_type_units.
6784 The result is zero if there is an error (e.g. missing .debug_types section),
6785 otherwise non-zero. */
6786
6787static int
ed2dc618 6788create_all_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
3019eac3 6789{
78d4d2c5 6790 htab_t types_htab = NULL;
3019eac3 6791
ed2dc618
SM
6792 create_debug_type_hash_table (dwarf2_per_objfile, NULL,
6793 &dwarf2_per_objfile->info, types_htab,
43988095 6794 rcuh_kind::COMPILE);
ed2dc618
SM
6795 create_debug_types_hash_table (dwarf2_per_objfile, NULL,
6796 dwarf2_per_objfile->types, types_htab);
3019eac3
DE
6797 if (types_htab == NULL)
6798 {
6799 dwarf2_per_objfile->signatured_types = NULL;
6800 return 0;
6801 }
6802
348e048f
DE
6803 dwarf2_per_objfile->signatured_types = types_htab;
6804
b2bdb8cf
SM
6805 gdb_assert (dwarf2_per_objfile->all_type_units.empty ());
6806 dwarf2_per_objfile->all_type_units.reserve (htab_elements (types_htab));
6807
6808 htab_traverse_noresize (types_htab, add_signatured_type_cu_to_table,
6809 &dwarf2_per_objfile->all_type_units);
1fd400ff 6810
348e048f
DE
6811 return 1;
6812}
6813
6aa5f3a6
DE
6814/* Add an entry for signature SIG to dwarf2_per_objfile->signatured_types.
6815 If SLOT is non-NULL, it is the entry to use in the hash table.
6816 Otherwise we find one. */
6817
6818static struct signatured_type *
ed2dc618
SM
6819add_type_unit (struct dwarf2_per_objfile *dwarf2_per_objfile, ULONGEST sig,
6820 void **slot)
6aa5f3a6
DE
6821{
6822 struct objfile *objfile = dwarf2_per_objfile->objfile;
6aa5f3a6 6823
b2bdb8cf
SM
6824 if (dwarf2_per_objfile->all_type_units.size ()
6825 == dwarf2_per_objfile->all_type_units.capacity ())
6826 ++dwarf2_per_objfile->tu_stats.nr_all_type_units_reallocs;
6aa5f3a6 6827
b2bdb8cf
SM
6828 signatured_type *sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6829 struct signatured_type);
6830
6831 dwarf2_per_objfile->all_type_units.push_back (sig_type);
6aa5f3a6
DE
6832 sig_type->signature = sig;
6833 sig_type->per_cu.is_debug_types = 1;
6834 if (dwarf2_per_objfile->using_index)
6835 {
6836 sig_type->per_cu.v.quick =
6837 OBSTACK_ZALLOC (&objfile->objfile_obstack,
6838 struct dwarf2_per_cu_quick_data);
6839 }
6840
6841 if (slot == NULL)
6842 {
6843 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
6844 sig_type, INSERT);
6845 }
6846 gdb_assert (*slot == NULL);
6847 *slot = sig_type;
6848 /* The rest of sig_type must be filled in by the caller. */
6849 return sig_type;
6850}
6851
a2ce51a0
DE
6852/* Subroutine of lookup_dwo_signatured_type and lookup_dwp_signatured_type.
6853 Fill in SIG_ENTRY with DWO_ENTRY. */
6854
6855static void
ed2dc618 6856fill_in_sig_entry_from_dwo_entry (struct dwarf2_per_objfile *dwarf2_per_objfile,
a2ce51a0
DE
6857 struct signatured_type *sig_entry,
6858 struct dwo_unit *dwo_entry)
6859{
7ee85ab1 6860 /* Make sure we're not clobbering something we don't expect to. */
a2ce51a0
DE
6861 gdb_assert (! sig_entry->per_cu.queued);
6862 gdb_assert (sig_entry->per_cu.cu == NULL);
6aa5f3a6
DE
6863 if (dwarf2_per_objfile->using_index)
6864 {
6865 gdb_assert (sig_entry->per_cu.v.quick != NULL);
43f3e411 6866 gdb_assert (sig_entry->per_cu.v.quick->compunit_symtab == NULL);
6aa5f3a6
DE
6867 }
6868 else
6869 gdb_assert (sig_entry->per_cu.v.psymtab == NULL);
a2ce51a0 6870 gdb_assert (sig_entry->signature == dwo_entry->signature);
9c541725 6871 gdb_assert (to_underlying (sig_entry->type_offset_in_section) == 0);
a2ce51a0 6872 gdb_assert (sig_entry->type_unit_group == NULL);
7ee85ab1
DE
6873 gdb_assert (sig_entry->dwo_unit == NULL);
6874
6875 sig_entry->per_cu.section = dwo_entry->section;
9c541725 6876 sig_entry->per_cu.sect_off = dwo_entry->sect_off;
7ee85ab1
DE
6877 sig_entry->per_cu.length = dwo_entry->length;
6878 sig_entry->per_cu.reading_dwo_directly = 1;
e3b94546 6879 sig_entry->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
a2ce51a0
DE
6880 sig_entry->type_offset_in_tu = dwo_entry->type_offset_in_tu;
6881 sig_entry->dwo_unit = dwo_entry;
6882}
6883
6884/* Subroutine of lookup_signatured_type.
7ee85ab1
DE
6885 If we haven't read the TU yet, create the signatured_type data structure
6886 for a TU to be read in directly from a DWO file, bypassing the stub.
6887 This is the "Stay in DWO Optimization": When there is no DWP file and we're
6888 using .gdb_index, then when reading a CU we want to stay in the DWO file
6889 containing that CU. Otherwise we could end up reading several other DWO
6890 files (due to comdat folding) to process the transitive closure of all the
6891 mentioned TUs, and that can be slow. The current DWO file will have every
6892 type signature that it needs.
a2ce51a0
DE
6893 We only do this for .gdb_index because in the psymtab case we already have
6894 to read all the DWOs to build the type unit groups. */
6895
6896static struct signatured_type *
6897lookup_dwo_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6898{
518817b3
SM
6899 struct dwarf2_per_objfile *dwarf2_per_objfile
6900 = cu->per_cu->dwarf2_per_objfile;
a2ce51a0
DE
6901 struct objfile *objfile = dwarf2_per_objfile->objfile;
6902 struct dwo_file *dwo_file;
6903 struct dwo_unit find_dwo_entry, *dwo_entry;
6904 struct signatured_type find_sig_entry, *sig_entry;
6aa5f3a6 6905 void **slot;
a2ce51a0
DE
6906
6907 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
6908
6aa5f3a6
DE
6909 /* If TU skeletons have been removed then we may not have read in any
6910 TUs yet. */
6911 if (dwarf2_per_objfile->signatured_types == NULL)
6912 {
6913 dwarf2_per_objfile->signatured_types
6914 = allocate_signatured_type_table (objfile);
6915 }
a2ce51a0
DE
6916
6917 /* We only ever need to read in one copy of a signatured type.
6aa5f3a6
DE
6918 Use the global signatured_types array to do our own comdat-folding
6919 of types. If this is the first time we're reading this TU, and
6920 the TU has an entry in .gdb_index, replace the recorded data from
6921 .gdb_index with this TU. */
a2ce51a0 6922
a2ce51a0 6923 find_sig_entry.signature = sig;
6aa5f3a6
DE
6924 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
6925 &find_sig_entry, INSERT);
9a3c8263 6926 sig_entry = (struct signatured_type *) *slot;
7ee85ab1
DE
6927
6928 /* We can get here with the TU already read, *or* in the process of being
6aa5f3a6
DE
6929 read. Don't reassign the global entry to point to this DWO if that's
6930 the case. Also note that if the TU is already being read, it may not
6931 have come from a DWO, the program may be a mix of Fission-compiled
6932 code and non-Fission-compiled code. */
6933
6934 /* Have we already tried to read this TU?
6935 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
6936 needn't exist in the global table yet). */
6937 if (sig_entry != NULL && sig_entry->per_cu.tu_read)
a2ce51a0
DE
6938 return sig_entry;
6939
6aa5f3a6
DE
6940 /* Note: cu->dwo_unit is the dwo_unit that references this TU, not the
6941 dwo_unit of the TU itself. */
6942 dwo_file = cu->dwo_unit->dwo_file;
6943
a2ce51a0
DE
6944 /* Ok, this is the first time we're reading this TU. */
6945 if (dwo_file->tus == NULL)
6946 return NULL;
6947 find_dwo_entry.signature = sig;
9a3c8263 6948 dwo_entry = (struct dwo_unit *) htab_find (dwo_file->tus, &find_dwo_entry);
a2ce51a0
DE
6949 if (dwo_entry == NULL)
6950 return NULL;
6951
6aa5f3a6
DE
6952 /* If the global table doesn't have an entry for this TU, add one. */
6953 if (sig_entry == NULL)
ed2dc618 6954 sig_entry = add_type_unit (dwarf2_per_objfile, sig, slot);
6aa5f3a6 6955
ed2dc618 6956 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, sig_entry, dwo_entry);
89e63ee4 6957 sig_entry->per_cu.tu_read = 1;
a2ce51a0
DE
6958 return sig_entry;
6959}
6960
a2ce51a0
DE
6961/* Subroutine of lookup_signatured_type.
6962 Look up the type for signature SIG, and if we can't find SIG in .gdb_index
6aa5f3a6
DE
6963 then try the DWP file. If the TU stub (skeleton) has been removed then
6964 it won't be in .gdb_index. */
a2ce51a0
DE
6965
6966static struct signatured_type *
6967lookup_dwp_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6968{
518817b3
SM
6969 struct dwarf2_per_objfile *dwarf2_per_objfile
6970 = cu->per_cu->dwarf2_per_objfile;
a2ce51a0 6971 struct objfile *objfile = dwarf2_per_objfile->objfile;
ed2dc618 6972 struct dwp_file *dwp_file = get_dwp_file (dwarf2_per_objfile);
a2ce51a0
DE
6973 struct dwo_unit *dwo_entry;
6974 struct signatured_type find_sig_entry, *sig_entry;
6aa5f3a6 6975 void **slot;
a2ce51a0
DE
6976
6977 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
6978 gdb_assert (dwp_file != NULL);
6979
6aa5f3a6
DE
6980 /* If TU skeletons have been removed then we may not have read in any
6981 TUs yet. */
6982 if (dwarf2_per_objfile->signatured_types == NULL)
a2ce51a0 6983 {
6aa5f3a6
DE
6984 dwarf2_per_objfile->signatured_types
6985 = allocate_signatured_type_table (objfile);
a2ce51a0
DE
6986 }
6987
6aa5f3a6
DE
6988 find_sig_entry.signature = sig;
6989 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
6990 &find_sig_entry, INSERT);
9a3c8263 6991 sig_entry = (struct signatured_type *) *slot;
6aa5f3a6
DE
6992
6993 /* Have we already tried to read this TU?
6994 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
6995 needn't exist in the global table yet). */
6996 if (sig_entry != NULL)
6997 return sig_entry;
6998
a2ce51a0
DE
6999 if (dwp_file->tus == NULL)
7000 return NULL;
ed2dc618 7001 dwo_entry = lookup_dwo_unit_in_dwp (dwarf2_per_objfile, dwp_file, NULL,
57d63ce2 7002 sig, 1 /* is_debug_types */);
a2ce51a0
DE
7003 if (dwo_entry == NULL)
7004 return NULL;
7005
ed2dc618
SM
7006 sig_entry = add_type_unit (dwarf2_per_objfile, sig, slot);
7007 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, sig_entry, dwo_entry);
a2ce51a0 7008
a2ce51a0
DE
7009 return sig_entry;
7010}
7011
380bca97 7012/* Lookup a signature based type for DW_FORM_ref_sig8.
5a8b3f62
DE
7013 Returns NULL if signature SIG is not present in the table.
7014 It is up to the caller to complain about this. */
348e048f
DE
7015
7016static struct signatured_type *
a2ce51a0 7017lookup_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
348e048f 7018{
518817b3
SM
7019 struct dwarf2_per_objfile *dwarf2_per_objfile
7020 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 7021
a2ce51a0
DE
7022 if (cu->dwo_unit
7023 && dwarf2_per_objfile->using_index)
7024 {
7025 /* We're in a DWO/DWP file, and we're using .gdb_index.
7026 These cases require special processing. */
ed2dc618 7027 if (get_dwp_file (dwarf2_per_objfile) == NULL)
a2ce51a0
DE
7028 return lookup_dwo_signatured_type (cu, sig);
7029 else
7030 return lookup_dwp_signatured_type (cu, sig);
7031 }
7032 else
7033 {
7034 struct signatured_type find_entry, *entry;
348e048f 7035
a2ce51a0
DE
7036 if (dwarf2_per_objfile->signatured_types == NULL)
7037 return NULL;
7038 find_entry.signature = sig;
9a3c8263
SM
7039 entry = ((struct signatured_type *)
7040 htab_find (dwarf2_per_objfile->signatured_types, &find_entry));
a2ce51a0
DE
7041 return entry;
7042 }
348e048f 7043}
42e7ad6c
DE
7044\f
7045/* Low level DIE reading support. */
348e048f 7046
d85a05f0
DJ
7047/* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
7048
7049static void
7050init_cu_die_reader (struct die_reader_specs *reader,
dee91e82 7051 struct dwarf2_cu *cu,
3019eac3 7052 struct dwarf2_section_info *section,
685af9cd
TT
7053 struct dwo_file *dwo_file,
7054 struct abbrev_table *abbrev_table)
d85a05f0 7055{
fceca515 7056 gdb_assert (section->readin && section->buffer != NULL);
a32a8923 7057 reader->abfd = get_section_bfd_owner (section);
d85a05f0 7058 reader->cu = cu;
3019eac3 7059 reader->dwo_file = dwo_file;
dee91e82
DE
7060 reader->die_section = section;
7061 reader->buffer = section->buffer;
f664829e 7062 reader->buffer_end = section->buffer + section->size;
a2ce51a0 7063 reader->comp_dir = NULL;
685af9cd 7064 reader->abbrev_table = abbrev_table;
d85a05f0
DJ
7065}
7066
b0c7bfa9
DE
7067/* Subroutine of init_cutu_and_read_dies to simplify it.
7068 Read in the rest of a CU/TU top level DIE from DWO_UNIT.
7069 There's just a lot of work to do, and init_cutu_and_read_dies is big enough
7070 already.
7071
7072 STUB_COMP_UNIT_DIE is for the stub DIE, we copy over certain attributes
7073 from it to the DIE in the DWO. If NULL we are skipping the stub.
a2ce51a0
DE
7074 STUB_COMP_DIR is similar to STUB_COMP_UNIT_DIE: When reading a TU directly
7075 from the DWO file, bypassing the stub, it contains the DW_AT_comp_dir
c54a1dd8
DE
7076 attribute of the referencing CU. At most one of STUB_COMP_UNIT_DIE and
7077 STUB_COMP_DIR may be non-NULL.
b0c7bfa9
DE
7078 *RESULT_READER,*RESULT_INFO_PTR,*RESULT_COMP_UNIT_DIE,*RESULT_HAS_CHILDREN
7079 are filled in with the info of the DIE from the DWO file.
685af9cd
TT
7080 *RESULT_DWO_ABBREV_TABLE will be filled in with the abbrev table allocated
7081 from the dwo. Since *RESULT_READER references this abbrev table, it must be
7082 kept around for at least as long as *RESULT_READER.
7083
b0c7bfa9
DE
7084 The result is non-zero if a valid (non-dummy) DIE was found. */
7085
7086static int
7087read_cutu_die_from_dwo (struct dwarf2_per_cu_data *this_cu,
7088 struct dwo_unit *dwo_unit,
b0c7bfa9 7089 struct die_info *stub_comp_unit_die,
a2ce51a0 7090 const char *stub_comp_dir,
b0c7bfa9 7091 struct die_reader_specs *result_reader,
d521ce57 7092 const gdb_byte **result_info_ptr,
b0c7bfa9 7093 struct die_info **result_comp_unit_die,
685af9cd
TT
7094 int *result_has_children,
7095 abbrev_table_up *result_dwo_abbrev_table)
b0c7bfa9 7096{
ed2dc618 7097 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
b0c7bfa9
DE
7098 struct objfile *objfile = dwarf2_per_objfile->objfile;
7099 struct dwarf2_cu *cu = this_cu->cu;
b0c7bfa9 7100 bfd *abfd;
d521ce57 7101 const gdb_byte *begin_info_ptr, *info_ptr;
b0c7bfa9
DE
7102 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
7103 int i,num_extra_attrs;
7104 struct dwarf2_section_info *dwo_abbrev_section;
7105 struct attribute *attr;
7106 struct die_info *comp_unit_die;
7107
b0aeadb3
DE
7108 /* At most one of these may be provided. */
7109 gdb_assert ((stub_comp_unit_die != NULL) + (stub_comp_dir != NULL) <= 1);
a2ce51a0 7110
b0c7bfa9
DE
7111 /* These attributes aren't processed until later:
7112 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
0d60c288
DE
7113 DW_AT_comp_dir is used now, to find the DWO file, but it is also
7114 referenced later. However, these attributes are found in the stub
7115 which we won't have later. In order to not impose this complication
7116 on the rest of the code, we read them here and copy them to the
7117 DWO CU/TU die. */
b0c7bfa9
DE
7118
7119 stmt_list = NULL;
7120 low_pc = NULL;
7121 high_pc = NULL;
7122 ranges = NULL;
7123 comp_dir = NULL;
7124
7125 if (stub_comp_unit_die != NULL)
7126 {
7127 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
7128 DWO file. */
7129 if (! this_cu->is_debug_types)
7130 stmt_list = dwarf2_attr (stub_comp_unit_die, DW_AT_stmt_list, cu);
7131 low_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_low_pc, cu);
7132 high_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_high_pc, cu);
7133 ranges = dwarf2_attr (stub_comp_unit_die, DW_AT_ranges, cu);
7134 comp_dir = dwarf2_attr (stub_comp_unit_die, DW_AT_comp_dir, cu);
7135
7136 /* There should be a DW_AT_addr_base attribute here (if needed).
7137 We need the value before we can process DW_FORM_GNU_addr_index. */
7138 cu->addr_base = 0;
7139 attr = dwarf2_attr (stub_comp_unit_die, DW_AT_GNU_addr_base, cu);
7140 if (attr)
7141 cu->addr_base = DW_UNSND (attr);
7142
7143 /* There should be a DW_AT_ranges_base attribute here (if needed).
7144 We need the value before we can process DW_AT_ranges. */
7145 cu->ranges_base = 0;
7146 attr = dwarf2_attr (stub_comp_unit_die, DW_AT_GNU_ranges_base, cu);
7147 if (attr)
7148 cu->ranges_base = DW_UNSND (attr);
7149 }
a2ce51a0
DE
7150 else if (stub_comp_dir != NULL)
7151 {
7152 /* Reconstruct the comp_dir attribute to simplify the code below. */
8d749320 7153 comp_dir = XOBNEW (&cu->comp_unit_obstack, struct attribute);
a2ce51a0
DE
7154 comp_dir->name = DW_AT_comp_dir;
7155 comp_dir->form = DW_FORM_string;
7156 DW_STRING_IS_CANONICAL (comp_dir) = 0;
7157 DW_STRING (comp_dir) = stub_comp_dir;
7158 }
b0c7bfa9
DE
7159
7160 /* Set up for reading the DWO CU/TU. */
7161 cu->dwo_unit = dwo_unit;
685af9cd 7162 dwarf2_section_info *section = dwo_unit->section;
b0c7bfa9 7163 dwarf2_read_section (objfile, section);
a32a8923 7164 abfd = get_section_bfd_owner (section);
9c541725
PA
7165 begin_info_ptr = info_ptr = (section->buffer
7166 + to_underlying (dwo_unit->sect_off));
b0c7bfa9 7167 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
b0c7bfa9
DE
7168
7169 if (this_cu->is_debug_types)
7170 {
b0c7bfa9
DE
7171 struct signatured_type *sig_type = (struct signatured_type *) this_cu;
7172
ed2dc618
SM
7173 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7174 &cu->header, section,
b0c7bfa9 7175 dwo_abbrev_section,
43988095 7176 info_ptr, rcuh_kind::TYPE);
a2ce51a0 7177 /* This is not an assert because it can be caused by bad debug info. */
43988095 7178 if (sig_type->signature != cu->header.signature)
a2ce51a0
DE
7179 {
7180 error (_("Dwarf Error: signature mismatch %s vs %s while reading"
9d8780f0 7181 " TU at offset %s [in module %s]"),
a2ce51a0 7182 hex_string (sig_type->signature),
43988095 7183 hex_string (cu->header.signature),
9d8780f0 7184 sect_offset_str (dwo_unit->sect_off),
a2ce51a0
DE
7185 bfd_get_filename (abfd));
7186 }
9c541725 7187 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
b0c7bfa9
DE
7188 /* For DWOs coming from DWP files, we don't know the CU length
7189 nor the type's offset in the TU until now. */
7190 dwo_unit->length = get_cu_length (&cu->header);
9c541725 7191 dwo_unit->type_offset_in_tu = cu->header.type_cu_offset_in_tu;
b0c7bfa9
DE
7192
7193 /* Establish the type offset that can be used to lookup the type.
7194 For DWO files, we don't know it until now. */
9c541725
PA
7195 sig_type->type_offset_in_section
7196 = dwo_unit->sect_off + to_underlying (dwo_unit->type_offset_in_tu);
b0c7bfa9
DE
7197 }
7198 else
7199 {
ed2dc618
SM
7200 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7201 &cu->header, section,
b0c7bfa9 7202 dwo_abbrev_section,
43988095 7203 info_ptr, rcuh_kind::COMPILE);
9c541725 7204 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
b0c7bfa9
DE
7205 /* For DWOs coming from DWP files, we don't know the CU length
7206 until now. */
7207 dwo_unit->length = get_cu_length (&cu->header);
7208 }
7209
685af9cd
TT
7210 *result_dwo_abbrev_table
7211 = abbrev_table_read_table (dwarf2_per_objfile, dwo_abbrev_section,
7212 cu->header.abbrev_sect_off);
7213 init_cu_die_reader (result_reader, cu, section, dwo_unit->dwo_file,
7214 result_dwo_abbrev_table->get ());
b0c7bfa9
DE
7215
7216 /* Read in the die, but leave space to copy over the attributes
7217 from the stub. This has the benefit of simplifying the rest of
7218 the code - all the work to maintain the illusion of a single
7219 DW_TAG_{compile,type}_unit DIE is done here. */
7220 num_extra_attrs = ((stmt_list != NULL)
7221 + (low_pc != NULL)
7222 + (high_pc != NULL)
7223 + (ranges != NULL)
7224 + (comp_dir != NULL));
7225 info_ptr = read_full_die_1 (result_reader, result_comp_unit_die, info_ptr,
7226 result_has_children, num_extra_attrs);
7227
7228 /* Copy over the attributes from the stub to the DIE we just read in. */
7229 comp_unit_die = *result_comp_unit_die;
7230 i = comp_unit_die->num_attrs;
7231 if (stmt_list != NULL)
7232 comp_unit_die->attrs[i++] = *stmt_list;
7233 if (low_pc != NULL)
7234 comp_unit_die->attrs[i++] = *low_pc;
7235 if (high_pc != NULL)
7236 comp_unit_die->attrs[i++] = *high_pc;
7237 if (ranges != NULL)
7238 comp_unit_die->attrs[i++] = *ranges;
7239 if (comp_dir != NULL)
7240 comp_unit_die->attrs[i++] = *comp_dir;
7241 comp_unit_die->num_attrs += num_extra_attrs;
7242
b4f54984 7243 if (dwarf_die_debug)
bf6af496
DE
7244 {
7245 fprintf_unfiltered (gdb_stdlog,
7246 "Read die from %s@0x%x of %s:\n",
a32a8923 7247 get_section_name (section),
bf6af496
DE
7248 (unsigned) (begin_info_ptr - section->buffer),
7249 bfd_get_filename (abfd));
b4f54984 7250 dump_die (comp_unit_die, dwarf_die_debug);
bf6af496
DE
7251 }
7252
a2ce51a0
DE
7253 /* Save the comp_dir attribute. If there is no DWP file then we'll read
7254 TUs by skipping the stub and going directly to the entry in the DWO file.
7255 However, skipping the stub means we won't get DW_AT_comp_dir, so we have
7256 to get it via circuitous means. Blech. */
7257 if (comp_dir != NULL)
7258 result_reader->comp_dir = DW_STRING (comp_dir);
7259
b0c7bfa9
DE
7260 /* Skip dummy compilation units. */
7261 if (info_ptr >= begin_info_ptr + dwo_unit->length
7262 || peek_abbrev_code (abfd, info_ptr) == 0)
7263 return 0;
7264
7265 *result_info_ptr = info_ptr;
7266 return 1;
7267}
7268
7269/* Subroutine of init_cutu_and_read_dies to simplify it.
7270 Look up the DWO unit specified by COMP_UNIT_DIE of THIS_CU.
6a506a2d 7271 Returns NULL if the specified DWO unit cannot be found. */
b0c7bfa9
DE
7272
7273static struct dwo_unit *
7274lookup_dwo_unit (struct dwarf2_per_cu_data *this_cu,
7275 struct die_info *comp_unit_die)
7276{
7277 struct dwarf2_cu *cu = this_cu->cu;
b0c7bfa9
DE
7278 ULONGEST signature;
7279 struct dwo_unit *dwo_unit;
7280 const char *comp_dir, *dwo_name;
7281
a2ce51a0
DE
7282 gdb_assert (cu != NULL);
7283
b0c7bfa9 7284 /* Yeah, we look dwo_name up again, but it simplifies the code. */
7d45c7c3
KB
7285 dwo_name = dwarf2_string_attr (comp_unit_die, DW_AT_GNU_dwo_name, cu);
7286 comp_dir = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
b0c7bfa9
DE
7287
7288 if (this_cu->is_debug_types)
7289 {
7290 struct signatured_type *sig_type;
7291
7292 /* Since this_cu is the first member of struct signatured_type,
7293 we can go from a pointer to one to a pointer to the other. */
7294 sig_type = (struct signatured_type *) this_cu;
7295 signature = sig_type->signature;
7296 dwo_unit = lookup_dwo_type_unit (sig_type, dwo_name, comp_dir);
7297 }
7298 else
7299 {
7300 struct attribute *attr;
7301
7302 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
7303 if (! attr)
7304 error (_("Dwarf Error: missing dwo_id for dwo_name %s"
7305 " [in module %s]"),
e3b94546 7306 dwo_name, objfile_name (this_cu->dwarf2_per_objfile->objfile));
b0c7bfa9
DE
7307 signature = DW_UNSND (attr);
7308 dwo_unit = lookup_dwo_comp_unit (this_cu, dwo_name, comp_dir,
7309 signature);
7310 }
7311
b0c7bfa9
DE
7312 return dwo_unit;
7313}
7314
a2ce51a0 7315/* Subroutine of init_cutu_and_read_dies to simplify it.
6aa5f3a6 7316 See it for a description of the parameters.
fcd3b13d 7317 Read a TU directly from a DWO file, bypassing the stub. */
a2ce51a0
DE
7318
7319static void
6aa5f3a6
DE
7320init_tu_and_read_dwo_dies (struct dwarf2_per_cu_data *this_cu,
7321 int use_existing_cu, int keep,
a2ce51a0
DE
7322 die_reader_func_ftype *die_reader_func,
7323 void *data)
7324{
fcd3b13d 7325 std::unique_ptr<dwarf2_cu> new_cu;
a2ce51a0 7326 struct signatured_type *sig_type;
a2ce51a0
DE
7327 struct die_reader_specs reader;
7328 const gdb_byte *info_ptr;
7329 struct die_info *comp_unit_die;
7330 int has_children;
ed2dc618 7331 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
a2ce51a0
DE
7332
7333 /* Verify we can do the following downcast, and that we have the
7334 data we need. */
7335 gdb_assert (this_cu->is_debug_types && this_cu->reading_dwo_directly);
7336 sig_type = (struct signatured_type *) this_cu;
7337 gdb_assert (sig_type->dwo_unit != NULL);
7338
6aa5f3a6
DE
7339 if (use_existing_cu && this_cu->cu != NULL)
7340 {
7341 gdb_assert (this_cu->cu->dwo_unit == sig_type->dwo_unit);
6aa5f3a6
DE
7342 /* There's no need to do the rereading_dwo_cu handling that
7343 init_cutu_and_read_dies does since we don't read the stub. */
7344 }
7345 else
7346 {
7347 /* If !use_existing_cu, this_cu->cu must be NULL. */
7348 gdb_assert (this_cu->cu == NULL);
fcd3b13d 7349 new_cu.reset (new dwarf2_cu (this_cu));
6aa5f3a6
DE
7350 }
7351
7352 /* A future optimization, if needed, would be to use an existing
7353 abbrev table. When reading DWOs with skeletonless TUs, all the TUs
7354 could share abbrev tables. */
a2ce51a0 7355
685af9cd
TT
7356 /* The abbreviation table used by READER, this must live at least as long as
7357 READER. */
7358 abbrev_table_up dwo_abbrev_table;
7359
a2ce51a0 7360 if (read_cutu_die_from_dwo (this_cu, sig_type->dwo_unit,
a2ce51a0
DE
7361 NULL /* stub_comp_unit_die */,
7362 sig_type->dwo_unit->dwo_file->comp_dir,
7363 &reader, &info_ptr,
685af9cd
TT
7364 &comp_unit_die, &has_children,
7365 &dwo_abbrev_table) == 0)
a2ce51a0
DE
7366 {
7367 /* Dummy die. */
a2ce51a0
DE
7368 return;
7369 }
7370
7371 /* All the "real" work is done here. */
7372 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
7373
6aa5f3a6 7374 /* This duplicates the code in init_cutu_and_read_dies,
a2ce51a0
DE
7375 but the alternative is making the latter more complex.
7376 This function is only for the special case of using DWO files directly:
7377 no point in overly complicating the general case just to handle this. */
fcd3b13d 7378 if (new_cu != NULL && keep)
a2ce51a0 7379 {
fcd3b13d
SM
7380 /* Link this CU into read_in_chain. */
7381 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
7382 dwarf2_per_objfile->read_in_chain = this_cu;
7383 /* The chain owns it now. */
7384 new_cu.release ();
a2ce51a0 7385 }
a2ce51a0
DE
7386}
7387
fd820528 7388/* Initialize a CU (or TU) and read its DIEs.
3019eac3 7389 If the CU defers to a DWO file, read the DWO file as well.
dee91e82 7390
f4dc4d17
DE
7391 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
7392 Otherwise the table specified in the comp unit header is read in and used.
7393 This is an optimization for when we already have the abbrev table.
7394
dee91e82
DE
7395 If USE_EXISTING_CU is non-zero, and THIS_CU->cu is non-NULL, then use it.
7396 Otherwise, a new CU is allocated with xmalloc.
7397
7398 If KEEP is non-zero, then if we allocated a dwarf2_cu we add it to
7399 read_in_chain. Otherwise the dwarf2_cu data is freed at the end.
7400
7401 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
fd820528 7402 linker) then DIE_READER_FUNC will not get called. */
aaa75496 7403
70221824 7404static void
fd820528 7405init_cutu_and_read_dies (struct dwarf2_per_cu_data *this_cu,
f4dc4d17 7406 struct abbrev_table *abbrev_table,
fd820528 7407 int use_existing_cu, int keep,
58f0c718 7408 bool skip_partial,
fd820528
DE
7409 die_reader_func_ftype *die_reader_func,
7410 void *data)
c906108c 7411{
ed2dc618 7412 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
dee91e82 7413 struct objfile *objfile = dwarf2_per_objfile->objfile;
8a0459fd 7414 struct dwarf2_section_info *section = this_cu->section;
a32a8923 7415 bfd *abfd = get_section_bfd_owner (section);
dee91e82 7416 struct dwarf2_cu *cu;
d521ce57 7417 const gdb_byte *begin_info_ptr, *info_ptr;
dee91e82 7418 struct die_reader_specs reader;
d85a05f0 7419 struct die_info *comp_unit_die;
dee91e82 7420 int has_children;
d85a05f0 7421 struct attribute *attr;
dee91e82 7422 struct signatured_type *sig_type = NULL;
4bdcc0c1 7423 struct dwarf2_section_info *abbrev_section;
42e7ad6c
DE
7424 /* Non-zero if CU currently points to a DWO file and we need to
7425 reread it. When this happens we need to reread the skeleton die
a2ce51a0 7426 before we can reread the DWO file (this only applies to CUs, not TUs). */
42e7ad6c 7427 int rereading_dwo_cu = 0;
c906108c 7428
b4f54984 7429 if (dwarf_die_debug)
9d8780f0 7430 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
09406207 7431 this_cu->is_debug_types ? "type" : "comp",
9d8780f0 7432 sect_offset_str (this_cu->sect_off));
09406207 7433
dee91e82
DE
7434 if (use_existing_cu)
7435 gdb_assert (keep);
23745b47 7436
a2ce51a0
DE
7437 /* If we're reading a TU directly from a DWO file, including a virtual DWO
7438 file (instead of going through the stub), short-circuit all of this. */
7439 if (this_cu->reading_dwo_directly)
7440 {
7441 /* Narrow down the scope of possibilities to have to understand. */
7442 gdb_assert (this_cu->is_debug_types);
7443 gdb_assert (abbrev_table == NULL);
6aa5f3a6
DE
7444 init_tu_and_read_dwo_dies (this_cu, use_existing_cu, keep,
7445 die_reader_func, data);
a2ce51a0
DE
7446 return;
7447 }
7448
dee91e82
DE
7449 /* This is cheap if the section is already read in. */
7450 dwarf2_read_section (objfile, section);
7451
9c541725 7452 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
36586728
TT
7453
7454 abbrev_section = get_abbrev_section_for_cu (this_cu);
dee91e82 7455
fcd3b13d 7456 std::unique_ptr<dwarf2_cu> new_cu;
dee91e82
DE
7457 if (use_existing_cu && this_cu->cu != NULL)
7458 {
7459 cu = this_cu->cu;
42e7ad6c
DE
7460 /* If this CU is from a DWO file we need to start over, we need to
7461 refetch the attributes from the skeleton CU.
7462 This could be optimized by retrieving those attributes from when we
7463 were here the first time: the previous comp_unit_die was stored in
7464 comp_unit_obstack. But there's no data yet that we need this
7465 optimization. */
7466 if (cu->dwo_unit != NULL)
7467 rereading_dwo_cu = 1;
dee91e82
DE
7468 }
7469 else
7470 {
7471 /* If !use_existing_cu, this_cu->cu must be NULL. */
7472 gdb_assert (this_cu->cu == NULL);
fcd3b13d
SM
7473 new_cu.reset (new dwarf2_cu (this_cu));
7474 cu = new_cu.get ();
42e7ad6c 7475 }
dee91e82 7476
b0c7bfa9 7477 /* Get the header. */
9c541725 7478 if (to_underlying (cu->header.first_die_cu_offset) != 0 && !rereading_dwo_cu)
42e7ad6c
DE
7479 {
7480 /* We already have the header, there's no need to read it in again. */
9c541725 7481 info_ptr += to_underlying (cu->header.first_die_cu_offset);
42e7ad6c
DE
7482 }
7483 else
7484 {
3019eac3 7485 if (this_cu->is_debug_types)
dee91e82 7486 {
ed2dc618
SM
7487 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7488 &cu->header, section,
4bdcc0c1 7489 abbrev_section, info_ptr,
43988095 7490 rcuh_kind::TYPE);
dee91e82 7491
42e7ad6c
DE
7492 /* Since per_cu is the first member of struct signatured_type,
7493 we can go from a pointer to one to a pointer to the other. */
7494 sig_type = (struct signatured_type *) this_cu;
43988095 7495 gdb_assert (sig_type->signature == cu->header.signature);
9c541725
PA
7496 gdb_assert (sig_type->type_offset_in_tu
7497 == cu->header.type_cu_offset_in_tu);
7498 gdb_assert (this_cu->sect_off == cu->header.sect_off);
dee91e82 7499
42e7ad6c
DE
7500 /* LENGTH has not been set yet for type units if we're
7501 using .gdb_index. */
1ce1cefd 7502 this_cu->length = get_cu_length (&cu->header);
3019eac3
DE
7503
7504 /* Establish the type offset that can be used to lookup the type. */
9c541725
PA
7505 sig_type->type_offset_in_section =
7506 this_cu->sect_off + to_underlying (sig_type->type_offset_in_tu);
43988095
JK
7507
7508 this_cu->dwarf_version = cu->header.version;
dee91e82
DE
7509 }
7510 else
7511 {
ed2dc618
SM
7512 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7513 &cu->header, section,
4bdcc0c1 7514 abbrev_section,
43988095
JK
7515 info_ptr,
7516 rcuh_kind::COMPILE);
dee91e82 7517
9c541725 7518 gdb_assert (this_cu->sect_off == cu->header.sect_off);
1ce1cefd 7519 gdb_assert (this_cu->length == get_cu_length (&cu->header));
43988095 7520 this_cu->dwarf_version = cu->header.version;
dee91e82
DE
7521 }
7522 }
10b3939b 7523
6caca83c 7524 /* Skip dummy compilation units. */
dee91e82 7525 if (info_ptr >= begin_info_ptr + this_cu->length
6caca83c 7526 || peek_abbrev_code (abfd, info_ptr) == 0)
fcd3b13d 7527 return;
6caca83c 7528
433df2d4
DE
7529 /* If we don't have them yet, read the abbrevs for this compilation unit.
7530 And if we need to read them now, make sure they're freed when we're
685af9cd
TT
7531 done (own the table through ABBREV_TABLE_HOLDER). */
7532 abbrev_table_up abbrev_table_holder;
f4dc4d17 7533 if (abbrev_table != NULL)
685af9cd
TT
7534 gdb_assert (cu->header.abbrev_sect_off == abbrev_table->sect_off);
7535 else
f4dc4d17 7536 {
685af9cd
TT
7537 abbrev_table_holder
7538 = abbrev_table_read_table (dwarf2_per_objfile, abbrev_section,
7539 cu->header.abbrev_sect_off);
7540 abbrev_table = abbrev_table_holder.get ();
42e7ad6c 7541 }
af703f96 7542
dee91e82 7543 /* Read the top level CU/TU die. */
685af9cd 7544 init_cu_die_reader (&reader, cu, section, NULL, abbrev_table);
dee91e82 7545 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
93311388 7546
58f0c718
TT
7547 if (skip_partial && comp_unit_die->tag == DW_TAG_partial_unit)
7548 return;
7549
b0c7bfa9 7550 /* If we are in a DWO stub, process it and then read in the "real" CU/TU
685af9cd
TT
7551 from the DWO file. read_cutu_die_from_dwo will allocate the abbreviation
7552 table from the DWO file and pass the ownership over to us. It will be
7553 referenced from READER, so we must make sure to free it after we're done
7554 with READER.
7555
b0c7bfa9
DE
7556 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains a
7557 DWO CU, that this test will fail (the attribute will not be present). */
3019eac3 7558 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_name, cu);
685af9cd 7559 abbrev_table_up dwo_abbrev_table;
3019eac3
DE
7560 if (attr)
7561 {
3019eac3 7562 struct dwo_unit *dwo_unit;
b0c7bfa9 7563 struct die_info *dwo_comp_unit_die;
3019eac3
DE
7564
7565 if (has_children)
6a506a2d
DE
7566 {
7567 complaint (&symfile_complaints,
7568 _("compilation unit with DW_AT_GNU_dwo_name"
9d8780f0
SM
7569 " has children (offset %s) [in module %s]"),
7570 sect_offset_str (this_cu->sect_off),
7571 bfd_get_filename (abfd));
6a506a2d 7572 }
b0c7bfa9 7573 dwo_unit = lookup_dwo_unit (this_cu, comp_unit_die);
6a506a2d 7574 if (dwo_unit != NULL)
3019eac3 7575 {
6a506a2d 7576 if (read_cutu_die_from_dwo (this_cu, dwo_unit,
a2ce51a0 7577 comp_unit_die, NULL,
6a506a2d 7578 &reader, &info_ptr,
685af9cd
TT
7579 &dwo_comp_unit_die, &has_children,
7580 &dwo_abbrev_table) == 0)
6a506a2d
DE
7581 {
7582 /* Dummy die. */
6a506a2d
DE
7583 return;
7584 }
7585 comp_unit_die = dwo_comp_unit_die;
7586 }
7587 else
7588 {
7589 /* Yikes, we couldn't find the rest of the DIE, we only have
7590 the stub. A complaint has already been logged. There's
7591 not much more we can do except pass on the stub DIE to
7592 die_reader_func. We don't want to throw an error on bad
7593 debug info. */
3019eac3
DE
7594 }
7595 }
7596
b0c7bfa9 7597 /* All of the above is setup for this call. Yikes. */
dee91e82
DE
7598 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
7599
b0c7bfa9 7600 /* Done, clean up. */
fcd3b13d 7601 if (new_cu != NULL && keep)
348e048f 7602 {
fcd3b13d
SM
7603 /* Link this CU into read_in_chain. */
7604 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
7605 dwarf2_per_objfile->read_in_chain = this_cu;
7606 /* The chain owns it now. */
7607 new_cu.release ();
348e048f 7608 }
dee91e82
DE
7609}
7610
33e80786
DE
7611/* Read CU/TU THIS_CU but do not follow DW_AT_GNU_dwo_name if present.
7612 DWO_FILE, if non-NULL, is the DWO file to read (the caller is assumed
7613 to have already done the lookup to find the DWO file).
dee91e82
DE
7614
7615 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
3019eac3 7616 THIS_CU->is_debug_types, but nothing else.
dee91e82
DE
7617
7618 We fill in THIS_CU->length.
7619
7620 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
7621 linker) then DIE_READER_FUNC will not get called.
7622
7623 THIS_CU->cu is always freed when done.
3019eac3
DE
7624 This is done in order to not leave THIS_CU->cu in a state where we have
7625 to care whether it refers to the "main" CU or the DWO CU. */
dee91e82
DE
7626
7627static void
7628init_cutu_and_read_dies_no_follow (struct dwarf2_per_cu_data *this_cu,
3019eac3 7629 struct dwo_file *dwo_file,
dee91e82
DE
7630 die_reader_func_ftype *die_reader_func,
7631 void *data)
7632{
ed2dc618 7633 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
dee91e82 7634 struct objfile *objfile = dwarf2_per_objfile->objfile;
8a0459fd 7635 struct dwarf2_section_info *section = this_cu->section;
a32a8923 7636 bfd *abfd = get_section_bfd_owner (section);
33e80786 7637 struct dwarf2_section_info *abbrev_section;
d521ce57 7638 const gdb_byte *begin_info_ptr, *info_ptr;
dee91e82 7639 struct die_reader_specs reader;
dee91e82
DE
7640 struct die_info *comp_unit_die;
7641 int has_children;
7642
b4f54984 7643 if (dwarf_die_debug)
9d8780f0 7644 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
09406207 7645 this_cu->is_debug_types ? "type" : "comp",
9d8780f0 7646 sect_offset_str (this_cu->sect_off));
09406207 7647
dee91e82
DE
7648 gdb_assert (this_cu->cu == NULL);
7649
33e80786
DE
7650 abbrev_section = (dwo_file != NULL
7651 ? &dwo_file->sections.abbrev
7652 : get_abbrev_section_for_cu (this_cu));
7653
dee91e82
DE
7654 /* This is cheap if the section is already read in. */
7655 dwarf2_read_section (objfile, section);
7656
fcd3b13d 7657 struct dwarf2_cu cu (this_cu);
dee91e82 7658
9c541725 7659 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
ed2dc618
SM
7660 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7661 &cu.header, section,
4bdcc0c1 7662 abbrev_section, info_ptr,
43988095
JK
7663 (this_cu->is_debug_types
7664 ? rcuh_kind::TYPE
7665 : rcuh_kind::COMPILE));
dee91e82 7666
1ce1cefd 7667 this_cu->length = get_cu_length (&cu.header);
dee91e82
DE
7668
7669 /* Skip dummy compilation units. */
7670 if (info_ptr >= begin_info_ptr + this_cu->length
7671 || peek_abbrev_code (abfd, info_ptr) == 0)
fcd3b13d 7672 return;
72bf9492 7673
685af9cd
TT
7674 abbrev_table_up abbrev_table
7675 = abbrev_table_read_table (dwarf2_per_objfile, abbrev_section,
7676 cu.header.abbrev_sect_off);
dee91e82 7677
685af9cd 7678 init_cu_die_reader (&reader, &cu, section, dwo_file, abbrev_table.get ());
dee91e82
DE
7679 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
7680
7681 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
dee91e82
DE
7682}
7683
3019eac3
DE
7684/* Read a CU/TU, except that this does not look for DW_AT_GNU_dwo_name and
7685 does not lookup the specified DWO file.
7686 This cannot be used to read DWO files.
dee91e82
DE
7687
7688 THIS_CU->cu is always freed when done.
3019eac3
DE
7689 This is done in order to not leave THIS_CU->cu in a state where we have
7690 to care whether it refers to the "main" CU or the DWO CU.
7691 We can revisit this if the data shows there's a performance issue. */
dee91e82
DE
7692
7693static void
7694init_cutu_and_read_dies_simple (struct dwarf2_per_cu_data *this_cu,
7695 die_reader_func_ftype *die_reader_func,
7696 void *data)
7697{
33e80786 7698 init_cutu_and_read_dies_no_follow (this_cu, NULL, die_reader_func, data);
dee91e82 7699}
0018ea6f
DE
7700\f
7701/* Type Unit Groups.
dee91e82 7702
0018ea6f
DE
7703 Type Unit Groups are a way to collapse the set of all TUs (type units) into
7704 a more manageable set. The grouping is done by DW_AT_stmt_list entry
7705 so that all types coming from the same compilation (.o file) are grouped
7706 together. A future step could be to put the types in the same symtab as
7707 the CU the types ultimately came from. */
ff013f42 7708
f4dc4d17
DE
7709static hashval_t
7710hash_type_unit_group (const void *item)
7711{
9a3c8263
SM
7712 const struct type_unit_group *tu_group
7713 = (const struct type_unit_group *) item;
f4dc4d17 7714
094b34ac 7715 return hash_stmt_list_entry (&tu_group->hash);
f4dc4d17 7716}
348e048f
DE
7717
7718static int
f4dc4d17 7719eq_type_unit_group (const void *item_lhs, const void *item_rhs)
348e048f 7720{
9a3c8263
SM
7721 const struct type_unit_group *lhs = (const struct type_unit_group *) item_lhs;
7722 const struct type_unit_group *rhs = (const struct type_unit_group *) item_rhs;
348e048f 7723
094b34ac 7724 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
f4dc4d17 7725}
348e048f 7726
f4dc4d17
DE
7727/* Allocate a hash table for type unit groups. */
7728
7729static htab_t
ed2dc618 7730allocate_type_unit_groups_table (struct objfile *objfile)
f4dc4d17
DE
7731{
7732 return htab_create_alloc_ex (3,
7733 hash_type_unit_group,
7734 eq_type_unit_group,
7735 NULL,
ed2dc618 7736 &objfile->objfile_obstack,
f4dc4d17
DE
7737 hashtab_obstack_allocate,
7738 dummy_obstack_deallocate);
7739}
dee91e82 7740
f4dc4d17
DE
7741/* Type units that don't have DW_AT_stmt_list are grouped into their own
7742 partial symtabs. We combine several TUs per psymtab to not let the size
7743 of any one psymtab grow too big. */
7744#define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
7745#define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
dee91e82 7746
094b34ac 7747/* Helper routine for get_type_unit_group.
f4dc4d17
DE
7748 Create the type_unit_group object used to hold one or more TUs. */
7749
7750static struct type_unit_group *
094b34ac 7751create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
f4dc4d17 7752{
518817b3
SM
7753 struct dwarf2_per_objfile *dwarf2_per_objfile
7754 = cu->per_cu->dwarf2_per_objfile;
f4dc4d17 7755 struct objfile *objfile = dwarf2_per_objfile->objfile;
094b34ac 7756 struct dwarf2_per_cu_data *per_cu;
f4dc4d17 7757 struct type_unit_group *tu_group;
f4dc4d17
DE
7758
7759 tu_group = OBSTACK_ZALLOC (&objfile->objfile_obstack,
7760 struct type_unit_group);
094b34ac 7761 per_cu = &tu_group->per_cu;
518817b3 7762 per_cu->dwarf2_per_objfile = dwarf2_per_objfile;
f4dc4d17 7763
094b34ac
DE
7764 if (dwarf2_per_objfile->using_index)
7765 {
7766 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
7767 struct dwarf2_per_cu_quick_data);
094b34ac
DE
7768 }
7769 else
7770 {
9c541725 7771 unsigned int line_offset = to_underlying (line_offset_struct);
094b34ac
DE
7772 struct partial_symtab *pst;
7773 char *name;
7774
7775 /* Give the symtab a useful name for debug purposes. */
7776 if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
7777 name = xstrprintf ("<type_units_%d>",
7778 (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
7779 else
7780 name = xstrprintf ("<type_units_at_0x%x>", line_offset);
7781
7782 pst = create_partial_symtab (per_cu, name);
7783 pst->anonymous = 1;
f4dc4d17 7784
094b34ac
DE
7785 xfree (name);
7786 }
f4dc4d17 7787
094b34ac 7788 tu_group->hash.dwo_unit = cu->dwo_unit;
9c541725 7789 tu_group->hash.line_sect_off = line_offset_struct;
f4dc4d17
DE
7790
7791 return tu_group;
7792}
7793
094b34ac
DE
7794/* Look up the type_unit_group for type unit CU, and create it if necessary.
7795 STMT_LIST is a DW_AT_stmt_list attribute. */
f4dc4d17
DE
7796
7797static struct type_unit_group *
ff39bb5e 7798get_type_unit_group (struct dwarf2_cu *cu, const struct attribute *stmt_list)
f4dc4d17 7799{
518817b3
SM
7800 struct dwarf2_per_objfile *dwarf2_per_objfile
7801 = cu->per_cu->dwarf2_per_objfile;
f4dc4d17
DE
7802 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
7803 struct type_unit_group *tu_group;
7804 void **slot;
7805 unsigned int line_offset;
7806 struct type_unit_group type_unit_group_for_lookup;
7807
7808 if (dwarf2_per_objfile->type_unit_groups == NULL)
7809 {
7810 dwarf2_per_objfile->type_unit_groups =
ed2dc618 7811 allocate_type_unit_groups_table (dwarf2_per_objfile->objfile);
f4dc4d17
DE
7812 }
7813
7814 /* Do we need to create a new group, or can we use an existing one? */
7815
7816 if (stmt_list)
7817 {
7818 line_offset = DW_UNSND (stmt_list);
7819 ++tu_stats->nr_symtab_sharers;
7820 }
7821 else
7822 {
7823 /* Ugh, no stmt_list. Rare, but we have to handle it.
7824 We can do various things here like create one group per TU or
7825 spread them over multiple groups to split up the expansion work.
7826 To avoid worst case scenarios (too many groups or too large groups)
7827 we, umm, group them in bunches. */
7828 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
7829 | (tu_stats->nr_stmt_less_type_units
7830 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
7831 ++tu_stats->nr_stmt_less_type_units;
7832 }
7833
094b34ac 7834 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
9c541725 7835 type_unit_group_for_lookup.hash.line_sect_off = (sect_offset) line_offset;
f4dc4d17
DE
7836 slot = htab_find_slot (dwarf2_per_objfile->type_unit_groups,
7837 &type_unit_group_for_lookup, INSERT);
7838 if (*slot != NULL)
7839 {
9a3c8263 7840 tu_group = (struct type_unit_group *) *slot;
f4dc4d17
DE
7841 gdb_assert (tu_group != NULL);
7842 }
7843 else
7844 {
9c541725 7845 sect_offset line_offset_struct = (sect_offset) line_offset;
094b34ac 7846 tu_group = create_type_unit_group (cu, line_offset_struct);
f4dc4d17
DE
7847 *slot = tu_group;
7848 ++tu_stats->nr_symtabs;
7849 }
7850
7851 return tu_group;
7852}
0018ea6f
DE
7853\f
7854/* Partial symbol tables. */
7855
7856/* Create a psymtab named NAME and assign it to PER_CU.
7857
7858 The caller must fill in the following details:
7859 dirname, textlow, texthigh. */
7860
7861static struct partial_symtab *
7862create_partial_symtab (struct dwarf2_per_cu_data *per_cu, const char *name)
7863{
e3b94546 7864 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
0018ea6f
DE
7865 struct partial_symtab *pst;
7866
18a94d75 7867 pst = start_psymtab_common (objfile, name, 0,
af5bf4ad
SM
7868 objfile->global_psymbols,
7869 objfile->static_psymbols);
0018ea6f
DE
7870
7871 pst->psymtabs_addrmap_supported = 1;
7872
7873 /* This is the glue that links PST into GDB's symbol API. */
7874 pst->read_symtab_private = per_cu;
7875 pst->read_symtab = dwarf2_read_symtab;
7876 per_cu->v.psymtab = pst;
7877
7878 return pst;
7879}
7880
b93601f3
TT
7881/* The DATA object passed to process_psymtab_comp_unit_reader has this
7882 type. */
7883
7884struct process_psymtab_comp_unit_data
7885{
7886 /* True if we are reading a DW_TAG_partial_unit. */
7887
7888 int want_partial_unit;
7889
7890 /* The "pretend" language that is used if the CU doesn't declare a
7891 language. */
7892
7893 enum language pretend_language;
7894};
7895
0018ea6f
DE
7896/* die_reader_func for process_psymtab_comp_unit. */
7897
7898static void
7899process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
d521ce57 7900 const gdb_byte *info_ptr,
0018ea6f
DE
7901 struct die_info *comp_unit_die,
7902 int has_children,
7903 void *data)
7904{
7905 struct dwarf2_cu *cu = reader->cu;
518817b3 7906 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
3e29f34a 7907 struct gdbarch *gdbarch = get_objfile_arch (objfile);
0018ea6f 7908 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
0018ea6f
DE
7909 CORE_ADDR baseaddr;
7910 CORE_ADDR best_lowpc = 0, best_highpc = 0;
7911 struct partial_symtab *pst;
3a2b436a 7912 enum pc_bounds_kind cu_bounds_kind;
0018ea6f 7913 const char *filename;
9a3c8263
SM
7914 struct process_psymtab_comp_unit_data *info
7915 = (struct process_psymtab_comp_unit_data *) data;
0018ea6f 7916
b93601f3 7917 if (comp_unit_die->tag == DW_TAG_partial_unit && !info->want_partial_unit)
0018ea6f
DE
7918 return;
7919
7920 gdb_assert (! per_cu->is_debug_types);
7921
b93601f3 7922 prepare_one_comp_unit (cu, comp_unit_die, info->pretend_language);
0018ea6f
DE
7923
7924 cu->list_in_scope = &file_symbols;
7925
7926 /* Allocate a new partial symbol table structure. */
7d45c7c3
KB
7927 filename = dwarf2_string_attr (comp_unit_die, DW_AT_name, cu);
7928 if (filename == NULL)
0018ea6f 7929 filename = "";
0018ea6f
DE
7930
7931 pst = create_partial_symtab (per_cu, filename);
7932
7933 /* This must be done before calling dwarf2_build_include_psymtabs. */
7d45c7c3 7934 pst->dirname = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
0018ea6f
DE
7935
7936 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
7937
7938 dwarf2_find_base_address (comp_unit_die, cu);
7939
7940 /* Possibly set the default values of LOWPC and HIGHPC from
7941 `DW_AT_ranges'. */
3a2b436a
JK
7942 cu_bounds_kind = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
7943 &best_highpc, cu, pst);
7944 if (cu_bounds_kind == PC_BOUNDS_HIGH_LOW && best_lowpc < best_highpc)
0018ea6f
DE
7945 /* Store the contiguous range if it is not empty; it can be empty for
7946 CUs with no code. */
7947 addrmap_set_empty (objfile->psymtabs_addrmap,
3e29f34a
MR
7948 gdbarch_adjust_dwarf2_addr (gdbarch,
7949 best_lowpc + baseaddr),
7950 gdbarch_adjust_dwarf2_addr (gdbarch,
7951 best_highpc + baseaddr) - 1,
7952 pst);
0018ea6f
DE
7953
7954 /* Check if comp unit has_children.
7955 If so, read the rest of the partial symbols from this comp unit.
7956 If not, there's no more debug_info for this comp unit. */
7957 if (has_children)
7958 {
7959 struct partial_die_info *first_die;
7960 CORE_ADDR lowpc, highpc;
7961
7962 lowpc = ((CORE_ADDR) -1);
7963 highpc = ((CORE_ADDR) 0);
7964
7965 first_die = load_partial_dies (reader, info_ptr, 1);
7966
7967 scan_partial_symbols (first_die, &lowpc, &highpc,
e385593e 7968 cu_bounds_kind <= PC_BOUNDS_INVALID, cu);
0018ea6f
DE
7969
7970 /* If we didn't find a lowpc, set it to highpc to avoid
7971 complaints from `maint check'. */
7972 if (lowpc == ((CORE_ADDR) -1))
7973 lowpc = highpc;
7974
7975 /* If the compilation unit didn't have an explicit address range,
7976 then use the information extracted from its child dies. */
e385593e 7977 if (cu_bounds_kind <= PC_BOUNDS_INVALID)
0018ea6f
DE
7978 {
7979 best_lowpc = lowpc;
7980 best_highpc = highpc;
7981 }
7982 }
3e29f34a
MR
7983 pst->textlow = gdbarch_adjust_dwarf2_addr (gdbarch, best_lowpc + baseaddr);
7984 pst->texthigh = gdbarch_adjust_dwarf2_addr (gdbarch, best_highpc + baseaddr);
0018ea6f 7985
8763cede 7986 end_psymtab_common (objfile, pst);
0018ea6f
DE
7987
7988 if (!VEC_empty (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs))
7989 {
7990 int i;
7991 int len = VEC_length (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs);
7992 struct dwarf2_per_cu_data *iter;
7993
7994 /* Fill in 'dependencies' here; we fill in 'users' in a
7995 post-pass. */
7996 pst->number_of_dependencies = len;
8d749320
SM
7997 pst->dependencies =
7998 XOBNEWVEC (&objfile->objfile_obstack, struct partial_symtab *, len);
0018ea6f
DE
7999 for (i = 0;
8000 VEC_iterate (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs,
8001 i, iter);
8002 ++i)
8003 pst->dependencies[i] = iter->v.psymtab;
8004
8005 VEC_free (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs);
8006 }
8007
8008 /* Get the list of files included in the current compilation unit,
8009 and build a psymtab for each of them. */
8010 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
8011
b4f54984 8012 if (dwarf_read_debug)
0018ea6f
DE
8013 {
8014 struct gdbarch *gdbarch = get_objfile_arch (objfile);
8015
8016 fprintf_unfiltered (gdb_stdlog,
9d8780f0 8017 "Psymtab for %s unit @%s: %s - %s"
0018ea6f
DE
8018 ", %d global, %d static syms\n",
8019 per_cu->is_debug_types ? "type" : "comp",
9d8780f0 8020 sect_offset_str (per_cu->sect_off),
0018ea6f
DE
8021 paddress (gdbarch, pst->textlow),
8022 paddress (gdbarch, pst->texthigh),
8023 pst->n_global_syms, pst->n_static_syms);
8024 }
8025}
8026
8027/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
8028 Process compilation unit THIS_CU for a psymtab. */
8029
8030static void
8031process_psymtab_comp_unit (struct dwarf2_per_cu_data *this_cu,
b93601f3
TT
8032 int want_partial_unit,
8033 enum language pretend_language)
0018ea6f
DE
8034{
8035 /* If this compilation unit was already read in, free the
8036 cached copy in order to read it in again. This is
8037 necessary because we skipped some symbols when we first
8038 read in the compilation unit (see load_partial_dies).
8039 This problem could be avoided, but the benefit is unclear. */
8040 if (this_cu->cu != NULL)
8041 free_one_cached_comp_unit (this_cu);
8042
f1902523 8043 if (this_cu->is_debug_types)
58f0c718
TT
8044 init_cutu_and_read_dies (this_cu, NULL, 0, 0, false,
8045 build_type_psymtabs_reader, NULL);
f1902523
JK
8046 else
8047 {
8048 process_psymtab_comp_unit_data info;
8049 info.want_partial_unit = want_partial_unit;
8050 info.pretend_language = pretend_language;
58f0c718 8051 init_cutu_and_read_dies (this_cu, NULL, 0, 0, false,
f1902523
JK
8052 process_psymtab_comp_unit_reader, &info);
8053 }
0018ea6f
DE
8054
8055 /* Age out any secondary CUs. */
ed2dc618 8056 age_cached_comp_units (this_cu->dwarf2_per_objfile);
0018ea6f 8057}
f4dc4d17
DE
8058
8059/* Reader function for build_type_psymtabs. */
8060
8061static void
8062build_type_psymtabs_reader (const struct die_reader_specs *reader,
d521ce57 8063 const gdb_byte *info_ptr,
f4dc4d17
DE
8064 struct die_info *type_unit_die,
8065 int has_children,
8066 void *data)
8067{
ed2dc618 8068 struct dwarf2_per_objfile *dwarf2_per_objfile
518817b3 8069 = reader->cu->per_cu->dwarf2_per_objfile;
f4dc4d17
DE
8070 struct objfile *objfile = dwarf2_per_objfile->objfile;
8071 struct dwarf2_cu *cu = reader->cu;
8072 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
0186c6a7 8073 struct signatured_type *sig_type;
f4dc4d17
DE
8074 struct type_unit_group *tu_group;
8075 struct attribute *attr;
8076 struct partial_die_info *first_die;
8077 CORE_ADDR lowpc, highpc;
8078 struct partial_symtab *pst;
8079
8080 gdb_assert (data == NULL);
0186c6a7
DE
8081 gdb_assert (per_cu->is_debug_types);
8082 sig_type = (struct signatured_type *) per_cu;
f4dc4d17
DE
8083
8084 if (! has_children)
8085 return;
8086
8087 attr = dwarf2_attr_no_follow (type_unit_die, DW_AT_stmt_list);
094b34ac 8088 tu_group = get_type_unit_group (cu, attr);
f4dc4d17 8089
0186c6a7 8090 VEC_safe_push (sig_type_ptr, tu_group->tus, sig_type);
f4dc4d17
DE
8091
8092 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
8093 cu->list_in_scope = &file_symbols;
8094 pst = create_partial_symtab (per_cu, "");
8095 pst->anonymous = 1;
8096
8097 first_die = load_partial_dies (reader, info_ptr, 1);
8098
8099 lowpc = (CORE_ADDR) -1;
8100 highpc = (CORE_ADDR) 0;
8101 scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
8102
8763cede 8103 end_psymtab_common (objfile, pst);
f4dc4d17
DE
8104}
8105
73051182
DE
8106/* Struct used to sort TUs by their abbreviation table offset. */
8107
8108struct tu_abbrev_offset
8109{
b2bdb8cf
SM
8110 tu_abbrev_offset (signatured_type *sig_type_, sect_offset abbrev_offset_)
8111 : sig_type (sig_type_), abbrev_offset (abbrev_offset_)
8112 {}
8113
8114 signatured_type *sig_type;
73051182
DE
8115 sect_offset abbrev_offset;
8116};
8117
484cf504 8118/* Helper routine for build_type_psymtabs_1, passed to std::sort. */
73051182 8119
484cf504
TT
8120static bool
8121sort_tu_by_abbrev_offset (const struct tu_abbrev_offset &a,
8122 const struct tu_abbrev_offset &b)
73051182 8123{
484cf504 8124 return a.abbrev_offset < b.abbrev_offset;
73051182
DE
8125}
8126
8127/* Efficiently read all the type units.
8128 This does the bulk of the work for build_type_psymtabs.
8129
8130 The efficiency is because we sort TUs by the abbrev table they use and
8131 only read each abbrev table once. In one program there are 200K TUs
8132 sharing 8K abbrev tables.
8133
8134 The main purpose of this function is to support building the
8135 dwarf2_per_objfile->type_unit_groups table.
8136 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
8137 can collapse the search space by grouping them by stmt_list.
8138 The savings can be significant, in the same program from above the 200K TUs
8139 share 8K stmt_list tables.
8140
8141 FUNC is expected to call get_type_unit_group, which will create the
8142 struct type_unit_group if necessary and add it to
8143 dwarf2_per_objfile->type_unit_groups. */
8144
8145static void
ed2dc618 8146build_type_psymtabs_1 (struct dwarf2_per_objfile *dwarf2_per_objfile)
73051182 8147{
73051182 8148 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
685af9cd 8149 abbrev_table_up abbrev_table;
73051182 8150 sect_offset abbrev_offset;
73051182
DE
8151
8152 /* It's up to the caller to not call us multiple times. */
8153 gdb_assert (dwarf2_per_objfile->type_unit_groups == NULL);
8154
b2bdb8cf 8155 if (dwarf2_per_objfile->all_type_units.empty ())
73051182
DE
8156 return;
8157
8158 /* TUs typically share abbrev tables, and there can be way more TUs than
8159 abbrev tables. Sort by abbrev table to reduce the number of times we
8160 read each abbrev table in.
8161 Alternatives are to punt or to maintain a cache of abbrev tables.
8162 This is simpler and efficient enough for now.
8163
8164 Later we group TUs by their DW_AT_stmt_list value (as this defines the
8165 symtab to use). Typically TUs with the same abbrev offset have the same
8166 stmt_list value too so in practice this should work well.
8167
8168 The basic algorithm here is:
8169
8170 sort TUs by abbrev table
8171 for each TU with same abbrev table:
8172 read abbrev table if first user
8173 read TU top level DIE
8174 [IWBN if DWO skeletons had DW_AT_stmt_list]
8175 call FUNC */
8176
b4f54984 8177 if (dwarf_read_debug)
73051182
DE
8178 fprintf_unfiltered (gdb_stdlog, "Building type unit groups ...\n");
8179
8180 /* Sort in a separate table to maintain the order of all_type_units
8181 for .gdb_index: TU indices directly index all_type_units. */
b2bdb8cf
SM
8182 std::vector<tu_abbrev_offset> sorted_by_abbrev;
8183 sorted_by_abbrev.reserve (dwarf2_per_objfile->all_type_units.size ());
8184
8185 for (signatured_type *sig_type : dwarf2_per_objfile->all_type_units)
8186 sorted_by_abbrev.emplace_back
8187 (sig_type, read_abbrev_offset (dwarf2_per_objfile,
8188 sig_type->per_cu.section,
8189 sig_type->per_cu.sect_off));
73051182 8190
484cf504
TT
8191 std::sort (sorted_by_abbrev.begin (), sorted_by_abbrev.end (),
8192 sort_tu_by_abbrev_offset);
73051182 8193
9c541725 8194 abbrev_offset = (sect_offset) ~(unsigned) 0;
73051182 8195
b2bdb8cf 8196 for (const tu_abbrev_offset &tu : sorted_by_abbrev)
73051182 8197 {
73051182
DE
8198 /* Switch to the next abbrev table if necessary. */
8199 if (abbrev_table == NULL
b2bdb8cf 8200 || tu.abbrev_offset != abbrev_offset)
73051182 8201 {
b2bdb8cf 8202 abbrev_offset = tu.abbrev_offset;
73051182 8203 abbrev_table =
ed2dc618
SM
8204 abbrev_table_read_table (dwarf2_per_objfile,
8205 &dwarf2_per_objfile->abbrev,
73051182
DE
8206 abbrev_offset);
8207 ++tu_stats->nr_uniq_abbrev_tables;
8208 }
8209
b2bdb8cf 8210 init_cutu_and_read_dies (&tu.sig_type->per_cu, abbrev_table.get (),
58f0c718 8211 0, 0, false, build_type_psymtabs_reader, NULL);
73051182 8212 }
6aa5f3a6 8213}
73051182 8214
6aa5f3a6
DE
8215/* Print collected type unit statistics. */
8216
8217static void
ed2dc618 8218print_tu_stats (struct dwarf2_per_objfile *dwarf2_per_objfile)
6aa5f3a6
DE
8219{
8220 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
8221
8222 fprintf_unfiltered (gdb_stdlog, "Type unit statistics:\n");
b2bdb8cf
SM
8223 fprintf_unfiltered (gdb_stdlog, " %zu TUs\n",
8224 dwarf2_per_objfile->all_type_units.size ());
6aa5f3a6
DE
8225 fprintf_unfiltered (gdb_stdlog, " %d uniq abbrev tables\n",
8226 tu_stats->nr_uniq_abbrev_tables);
8227 fprintf_unfiltered (gdb_stdlog, " %d symtabs from stmt_list entries\n",
8228 tu_stats->nr_symtabs);
8229 fprintf_unfiltered (gdb_stdlog, " %d symtab sharers\n",
8230 tu_stats->nr_symtab_sharers);
8231 fprintf_unfiltered (gdb_stdlog, " %d type units without a stmt_list\n",
8232 tu_stats->nr_stmt_less_type_units);
8233 fprintf_unfiltered (gdb_stdlog, " %d all_type_units reallocs\n",
8234 tu_stats->nr_all_type_units_reallocs);
73051182
DE
8235}
8236
f4dc4d17
DE
8237/* Traversal function for build_type_psymtabs. */
8238
8239static int
8240build_type_psymtab_dependencies (void **slot, void *info)
8241{
ed2dc618
SM
8242 struct dwarf2_per_objfile *dwarf2_per_objfile
8243 = (struct dwarf2_per_objfile *) info;
f4dc4d17
DE
8244 struct objfile *objfile = dwarf2_per_objfile->objfile;
8245 struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
094b34ac 8246 struct dwarf2_per_cu_data *per_cu = &tu_group->per_cu;
f4dc4d17 8247 struct partial_symtab *pst = per_cu->v.psymtab;
0186c6a7
DE
8248 int len = VEC_length (sig_type_ptr, tu_group->tus);
8249 struct signatured_type *iter;
f4dc4d17
DE
8250 int i;
8251
8252 gdb_assert (len > 0);
0186c6a7 8253 gdb_assert (IS_TYPE_UNIT_GROUP (per_cu));
f4dc4d17
DE
8254
8255 pst->number_of_dependencies = len;
8d749320
SM
8256 pst->dependencies =
8257 XOBNEWVEC (&objfile->objfile_obstack, struct partial_symtab *, len);
f4dc4d17 8258 for (i = 0;
0186c6a7 8259 VEC_iterate (sig_type_ptr, tu_group->tus, i, iter);
f4dc4d17
DE
8260 ++i)
8261 {
0186c6a7
DE
8262 gdb_assert (iter->per_cu.is_debug_types);
8263 pst->dependencies[i] = iter->per_cu.v.psymtab;
796a7ff8 8264 iter->type_unit_group = tu_group;
f4dc4d17
DE
8265 }
8266
0186c6a7 8267 VEC_free (sig_type_ptr, tu_group->tus);
348e048f
DE
8268
8269 return 1;
8270}
8271
8272/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
8273 Build partial symbol tables for the .debug_types comp-units. */
8274
8275static void
ed2dc618 8276build_type_psymtabs (struct dwarf2_per_objfile *dwarf2_per_objfile)
348e048f 8277{
ed2dc618 8278 if (! create_all_type_units (dwarf2_per_objfile))
348e048f
DE
8279 return;
8280
ed2dc618 8281 build_type_psymtabs_1 (dwarf2_per_objfile);
6aa5f3a6 8282}
f4dc4d17 8283
6aa5f3a6
DE
8284/* Traversal function for process_skeletonless_type_unit.
8285 Read a TU in a DWO file and build partial symbols for it. */
8286
8287static int
8288process_skeletonless_type_unit (void **slot, void *info)
8289{
8290 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
ed2dc618
SM
8291 struct dwarf2_per_objfile *dwarf2_per_objfile
8292 = (struct dwarf2_per_objfile *) info;
6aa5f3a6
DE
8293 struct signatured_type find_entry, *entry;
8294
8295 /* If this TU doesn't exist in the global table, add it and read it in. */
8296
8297 if (dwarf2_per_objfile->signatured_types == NULL)
8298 {
8299 dwarf2_per_objfile->signatured_types
ed2dc618 8300 = allocate_signatured_type_table (dwarf2_per_objfile->objfile);
6aa5f3a6
DE
8301 }
8302
8303 find_entry.signature = dwo_unit->signature;
8304 slot = htab_find_slot (dwarf2_per_objfile->signatured_types, &find_entry,
8305 INSERT);
8306 /* If we've already seen this type there's nothing to do. What's happening
8307 is we're doing our own version of comdat-folding here. */
8308 if (*slot != NULL)
8309 return 1;
8310
8311 /* This does the job that create_all_type_units would have done for
8312 this TU. */
ed2dc618
SM
8313 entry = add_type_unit (dwarf2_per_objfile, dwo_unit->signature, slot);
8314 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, entry, dwo_unit);
6aa5f3a6
DE
8315 *slot = entry;
8316
8317 /* This does the job that build_type_psymtabs_1 would have done. */
58f0c718 8318 init_cutu_and_read_dies (&entry->per_cu, NULL, 0, 0, false,
6aa5f3a6
DE
8319 build_type_psymtabs_reader, NULL);
8320
8321 return 1;
8322}
8323
8324/* Traversal function for process_skeletonless_type_units. */
8325
8326static int
8327process_dwo_file_for_skeletonless_type_units (void **slot, void *info)
8328{
8329 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
8330
8331 if (dwo_file->tus != NULL)
8332 {
8333 htab_traverse_noresize (dwo_file->tus,
8334 process_skeletonless_type_unit, info);
8335 }
8336
8337 return 1;
8338}
8339
8340/* Scan all TUs of DWO files, verifying we've processed them.
8341 This is needed in case a TU was emitted without its skeleton.
8342 Note: This can't be done until we know what all the DWO files are. */
8343
8344static void
ed2dc618 8345process_skeletonless_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
6aa5f3a6
DE
8346{
8347 /* Skeletonless TUs in DWP files without .gdb_index is not supported yet. */
ed2dc618 8348 if (get_dwp_file (dwarf2_per_objfile) == NULL
6aa5f3a6
DE
8349 && dwarf2_per_objfile->dwo_files != NULL)
8350 {
8351 htab_traverse_noresize (dwarf2_per_objfile->dwo_files,
8352 process_dwo_file_for_skeletonless_type_units,
ed2dc618 8353 dwarf2_per_objfile);
6aa5f3a6 8354 }
348e048f
DE
8355}
8356
ed2dc618 8357/* Compute the 'user' field for each psymtab in DWARF2_PER_OBJFILE. */
95554aad
TT
8358
8359static void
ed2dc618 8360set_partial_user (struct dwarf2_per_objfile *dwarf2_per_objfile)
95554aad 8361{
b76e467d 8362 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
95554aad 8363 {
95554aad 8364 struct partial_symtab *pst = per_cu->v.psymtab;
95554aad 8365
36586728
TT
8366 if (pst == NULL)
8367 continue;
8368
b76e467d 8369 for (int j = 0; j < pst->number_of_dependencies; ++j)
95554aad
TT
8370 {
8371 /* Set the 'user' field only if it is not already set. */
8372 if (pst->dependencies[j]->user == NULL)
8373 pst->dependencies[j]->user = pst;
8374 }
8375 }
8376}
8377
93311388
DE
8378/* Build the partial symbol table by doing a quick pass through the
8379 .debug_info and .debug_abbrev sections. */
72bf9492 8380
93311388 8381static void
ed2dc618 8382dwarf2_build_psymtabs_hard (struct dwarf2_per_objfile *dwarf2_per_objfile)
93311388 8383{
ed2dc618 8384 struct objfile *objfile = dwarf2_per_objfile->objfile;
93311388 8385
b4f54984 8386 if (dwarf_read_debug)
45cfd468
DE
8387 {
8388 fprintf_unfiltered (gdb_stdlog, "Building psymtabs of objfile %s ...\n",
4262abfb 8389 objfile_name (objfile));
45cfd468
DE
8390 }
8391
98bfdba5
PA
8392 dwarf2_per_objfile->reading_partial_symbols = 1;
8393
be391dca 8394 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
91c24f0a 8395
93311388
DE
8396 /* Any cached compilation units will be linked by the per-objfile
8397 read_in_chain. Make sure to free them when we're done. */
11ed8cad 8398 free_cached_comp_units freer (dwarf2_per_objfile);
72bf9492 8399
ed2dc618 8400 build_type_psymtabs (dwarf2_per_objfile);
348e048f 8401
ed2dc618 8402 create_all_comp_units (dwarf2_per_objfile);
c906108c 8403
60606b2c
TT
8404 /* Create a temporary address map on a temporary obstack. We later
8405 copy this to the final obstack. */
8268c778 8406 auto_obstack temp_obstack;
791afaa2
TT
8407
8408 scoped_restore save_psymtabs_addrmap
8409 = make_scoped_restore (&objfile->psymtabs_addrmap,
8410 addrmap_create_mutable (&temp_obstack));
72bf9492 8411
b76e467d
SM
8412 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
8413 process_psymtab_comp_unit (per_cu, 0, language_minimal);
ff013f42 8414
6aa5f3a6 8415 /* This has to wait until we read the CUs, we need the list of DWOs. */
ed2dc618 8416 process_skeletonless_type_units (dwarf2_per_objfile);
6aa5f3a6
DE
8417
8418 /* Now that all TUs have been processed we can fill in the dependencies. */
8419 if (dwarf2_per_objfile->type_unit_groups != NULL)
8420 {
8421 htab_traverse_noresize (dwarf2_per_objfile->type_unit_groups,
ed2dc618 8422 build_type_psymtab_dependencies, dwarf2_per_objfile);
6aa5f3a6
DE
8423 }
8424
b4f54984 8425 if (dwarf_read_debug)
ed2dc618 8426 print_tu_stats (dwarf2_per_objfile);
6aa5f3a6 8427
ed2dc618 8428 set_partial_user (dwarf2_per_objfile);
95554aad 8429
ff013f42
JK
8430 objfile->psymtabs_addrmap = addrmap_create_fixed (objfile->psymtabs_addrmap,
8431 &objfile->objfile_obstack);
791afaa2
TT
8432 /* At this point we want to keep the address map. */
8433 save_psymtabs_addrmap.release ();
ff013f42 8434
b4f54984 8435 if (dwarf_read_debug)
45cfd468 8436 fprintf_unfiltered (gdb_stdlog, "Done building psymtabs of %s\n",
4262abfb 8437 objfile_name (objfile));
ae038cb0
DJ
8438}
8439
3019eac3 8440/* die_reader_func for load_partial_comp_unit. */
ae038cb0
DJ
8441
8442static void
dee91e82 8443load_partial_comp_unit_reader (const struct die_reader_specs *reader,
d521ce57 8444 const gdb_byte *info_ptr,
dee91e82
DE
8445 struct die_info *comp_unit_die,
8446 int has_children,
8447 void *data)
ae038cb0 8448{
dee91e82 8449 struct dwarf2_cu *cu = reader->cu;
ae038cb0 8450
95554aad 8451 prepare_one_comp_unit (cu, comp_unit_die, language_minimal);
ae038cb0 8452
ae038cb0
DJ
8453 /* Check if comp unit has_children.
8454 If so, read the rest of the partial symbols from this comp unit.
0963b4bd 8455 If not, there's no more debug_info for this comp unit. */
d85a05f0 8456 if (has_children)
dee91e82
DE
8457 load_partial_dies (reader, info_ptr, 0);
8458}
98bfdba5 8459
dee91e82
DE
8460/* Load the partial DIEs for a secondary CU into memory.
8461 This is also used when rereading a primary CU with load_all_dies. */
c5b7e1cb 8462
dee91e82
DE
8463static void
8464load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu)
8465{
58f0c718 8466 init_cutu_and_read_dies (this_cu, NULL, 1, 1, false,
f4dc4d17 8467 load_partial_comp_unit_reader, NULL);
ae038cb0
DJ
8468}
8469
ae038cb0 8470static void
ed2dc618 8471read_comp_units_from_section (struct dwarf2_per_objfile *dwarf2_per_objfile,
36586728 8472 struct dwarf2_section_info *section,
f1902523 8473 struct dwarf2_section_info *abbrev_section,
b76e467d 8474 unsigned int is_dwz)
ae038cb0 8475{
d521ce57 8476 const gdb_byte *info_ptr;
ed2dc618 8477 struct objfile *objfile = dwarf2_per_objfile->objfile;
be391dca 8478
b4f54984 8479 if (dwarf_read_debug)
bf6af496 8480 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s\n",
a32a8923
DE
8481 get_section_name (section),
8482 get_section_file_name (section));
bf6af496 8483
36586728 8484 dwarf2_read_section (objfile, section);
ae038cb0 8485
36586728 8486 info_ptr = section->buffer;
6e70227d 8487
36586728 8488 while (info_ptr < section->buffer + section->size)
ae038cb0 8489 {
ae038cb0 8490 struct dwarf2_per_cu_data *this_cu;
ae038cb0 8491
9c541725 8492 sect_offset sect_off = (sect_offset) (info_ptr - section->buffer);
ae038cb0 8493
f1902523 8494 comp_unit_head cu_header;
ed2dc618
SM
8495 read_and_check_comp_unit_head (dwarf2_per_objfile, &cu_header, section,
8496 abbrev_section, info_ptr,
8497 rcuh_kind::COMPILE);
ae038cb0
DJ
8498
8499 /* Save the compilation unit for later lookup. */
f1902523
JK
8500 if (cu_header.unit_type != DW_UT_type)
8501 {
8502 this_cu = XOBNEW (&objfile->objfile_obstack,
8503 struct dwarf2_per_cu_data);
8504 memset (this_cu, 0, sizeof (*this_cu));
8505 }
8506 else
8507 {
8508 auto sig_type = XOBNEW (&objfile->objfile_obstack,
8509 struct signatured_type);
8510 memset (sig_type, 0, sizeof (*sig_type));
8511 sig_type->signature = cu_header.signature;
8512 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
8513 this_cu = &sig_type->per_cu;
8514 }
8515 this_cu->is_debug_types = (cu_header.unit_type == DW_UT_type);
9c541725 8516 this_cu->sect_off = sect_off;
f1902523 8517 this_cu->length = cu_header.length + cu_header.initial_length_size;
36586728 8518 this_cu->is_dwz = is_dwz;
e3b94546 8519 this_cu->dwarf2_per_objfile = dwarf2_per_objfile;
8a0459fd 8520 this_cu->section = section;
ae038cb0 8521
b76e467d 8522 dwarf2_per_objfile->all_comp_units.push_back (this_cu);
ae038cb0
DJ
8523
8524 info_ptr = info_ptr + this_cu->length;
8525 }
36586728
TT
8526}
8527
8528/* Create a list of all compilation units in OBJFILE.
8529 This is only done for -readnow and building partial symtabs. */
8530
8531static void
ed2dc618 8532create_all_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
36586728 8533{
b76e467d 8534 gdb_assert (dwarf2_per_objfile->all_comp_units.empty ());
ed2dc618 8535 read_comp_units_from_section (dwarf2_per_objfile, &dwarf2_per_objfile->info,
b76e467d 8536 &dwarf2_per_objfile->abbrev, 0);
36586728 8537
b76e467d 8538 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
4db1a1dc 8539 if (dwz != NULL)
ed2dc618 8540 read_comp_units_from_section (dwarf2_per_objfile, &dwz->info, &dwz->abbrev,
b76e467d 8541 1);
c906108c
SS
8542}
8543
5734ee8b 8544/* Process all loaded DIEs for compilation unit CU, starting at
cdc07690 8545 FIRST_DIE. The caller should pass SET_ADDRMAP == 1 if the compilation
5734ee8b 8546 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
cdc07690
YQ
8547 DW_AT_ranges). See the comments of add_partial_subprogram on how
8548 SET_ADDRMAP is used and how *LOWPC and *HIGHPC are updated. */
c906108c 8549
72bf9492
DJ
8550static void
8551scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
cdc07690
YQ
8552 CORE_ADDR *highpc, int set_addrmap,
8553 struct dwarf2_cu *cu)
c906108c 8554{
72bf9492 8555 struct partial_die_info *pdi;
c906108c 8556
91c24f0a
DC
8557 /* Now, march along the PDI's, descending into ones which have
8558 interesting children but skipping the children of the other ones,
8559 until we reach the end of the compilation unit. */
c906108c 8560
72bf9492 8561 pdi = first_die;
91c24f0a 8562
72bf9492
DJ
8563 while (pdi != NULL)
8564 {
52356b79 8565 pdi->fixup (cu);
c906108c 8566
f55ee35c 8567 /* Anonymous namespaces or modules have no name but have interesting
91c24f0a
DC
8568 children, so we need to look at them. Ditto for anonymous
8569 enums. */
933c6fe4 8570
72bf9492 8571 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
95554aad 8572 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
b1dc1806
XR
8573 || pdi->tag == DW_TAG_imported_unit
8574 || pdi->tag == DW_TAG_inlined_subroutine)
c906108c 8575 {
72bf9492 8576 switch (pdi->tag)
c906108c
SS
8577 {
8578 case DW_TAG_subprogram:
b1dc1806 8579 case DW_TAG_inlined_subroutine:
cdc07690 8580 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
c906108c 8581 break;
72929c62 8582 case DW_TAG_constant:
c906108c
SS
8583 case DW_TAG_variable:
8584 case DW_TAG_typedef:
91c24f0a 8585 case DW_TAG_union_type:
72bf9492 8586 if (!pdi->is_declaration)
63d06c5c 8587 {
72bf9492 8588 add_partial_symbol (pdi, cu);
63d06c5c
DC
8589 }
8590 break;
c906108c 8591 case DW_TAG_class_type:
680b30c7 8592 case DW_TAG_interface_type:
c906108c 8593 case DW_TAG_structure_type:
72bf9492 8594 if (!pdi->is_declaration)
c906108c 8595 {
72bf9492 8596 add_partial_symbol (pdi, cu);
c906108c 8597 }
b7fee5a3
KS
8598 if ((cu->language == language_rust
8599 || cu->language == language_cplus) && pdi->has_children)
e98c9e7c
TT
8600 scan_partial_symbols (pdi->die_child, lowpc, highpc,
8601 set_addrmap, cu);
c906108c 8602 break;
91c24f0a 8603 case DW_TAG_enumeration_type:
72bf9492
DJ
8604 if (!pdi->is_declaration)
8605 add_partial_enumeration (pdi, cu);
c906108c
SS
8606 break;
8607 case DW_TAG_base_type:
a02abb62 8608 case DW_TAG_subrange_type:
c906108c 8609 /* File scope base type definitions are added to the partial
c5aa993b 8610 symbol table. */
72bf9492 8611 add_partial_symbol (pdi, cu);
c906108c 8612 break;
d9fa45fe 8613 case DW_TAG_namespace:
cdc07690 8614 add_partial_namespace (pdi, lowpc, highpc, set_addrmap, cu);
91c24f0a 8615 break;
5d7cb8df 8616 case DW_TAG_module:
cdc07690 8617 add_partial_module (pdi, lowpc, highpc, set_addrmap, cu);
5d7cb8df 8618 break;
95554aad
TT
8619 case DW_TAG_imported_unit:
8620 {
8621 struct dwarf2_per_cu_data *per_cu;
8622
f4dc4d17
DE
8623 /* For now we don't handle imported units in type units. */
8624 if (cu->per_cu->is_debug_types)
8625 {
8626 error (_("Dwarf Error: DW_TAG_imported_unit is not"
8627 " supported in type units [in module %s]"),
518817b3 8628 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
f4dc4d17
DE
8629 }
8630
e3b94546
SM
8631 per_cu = dwarf2_find_containing_comp_unit
8632 (pdi->d.sect_off, pdi->is_dwz,
518817b3 8633 cu->per_cu->dwarf2_per_objfile);
95554aad
TT
8634
8635 /* Go read the partial unit, if needed. */
8636 if (per_cu->v.psymtab == NULL)
b93601f3 8637 process_psymtab_comp_unit (per_cu, 1, cu->language);
95554aad 8638
f4dc4d17 8639 VEC_safe_push (dwarf2_per_cu_ptr,
796a7ff8 8640 cu->per_cu->imported_symtabs, per_cu);
95554aad
TT
8641 }
8642 break;
74921315
KS
8643 case DW_TAG_imported_declaration:
8644 add_partial_symbol (pdi, cu);
8645 break;
c906108c
SS
8646 default:
8647 break;
8648 }
8649 }
8650
72bf9492
DJ
8651 /* If the die has a sibling, skip to the sibling. */
8652
8653 pdi = pdi->die_sibling;
8654 }
8655}
8656
8657/* Functions used to compute the fully scoped name of a partial DIE.
91c24f0a 8658
72bf9492 8659 Normally, this is simple. For C++, the parent DIE's fully scoped
9c37b5ae 8660 name is concatenated with "::" and the partial DIE's name.
72bf9492
DJ
8661 Enumerators are an exception; they use the scope of their parent
8662 enumeration type, i.e. the name of the enumeration type is not
8663 prepended to the enumerator.
91c24f0a 8664
72bf9492
DJ
8665 There are two complexities. One is DW_AT_specification; in this
8666 case "parent" means the parent of the target of the specification,
8667 instead of the direct parent of the DIE. The other is compilers
8668 which do not emit DW_TAG_namespace; in this case we try to guess
8669 the fully qualified name of structure types from their members'
8670 linkage names. This must be done using the DIE's children rather
8671 than the children of any DW_AT_specification target. We only need
8672 to do this for structures at the top level, i.e. if the target of
8673 any DW_AT_specification (if any; otherwise the DIE itself) does not
8674 have a parent. */
8675
8676/* Compute the scope prefix associated with PDI's parent, in
8677 compilation unit CU. The result will be allocated on CU's
8678 comp_unit_obstack, or a copy of the already allocated PDI->NAME
8679 field. NULL is returned if no prefix is necessary. */
15d034d0 8680static const char *
72bf9492
DJ
8681partial_die_parent_scope (struct partial_die_info *pdi,
8682 struct dwarf2_cu *cu)
8683{
15d034d0 8684 const char *grandparent_scope;
72bf9492 8685 struct partial_die_info *parent, *real_pdi;
91c24f0a 8686
72bf9492
DJ
8687 /* We need to look at our parent DIE; if we have a DW_AT_specification,
8688 then this means the parent of the specification DIE. */
8689
8690 real_pdi = pdi;
72bf9492 8691 while (real_pdi->has_specification)
36586728
TT
8692 real_pdi = find_partial_die (real_pdi->spec_offset,
8693 real_pdi->spec_is_dwz, cu);
72bf9492
DJ
8694
8695 parent = real_pdi->die_parent;
8696 if (parent == NULL)
8697 return NULL;
8698
8699 if (parent->scope_set)
8700 return parent->scope;
8701
52356b79 8702 parent->fixup (cu);
72bf9492 8703
10b3939b 8704 grandparent_scope = partial_die_parent_scope (parent, cu);
72bf9492 8705
acebe513
UW
8706 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
8707 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
8708 Work around this problem here. */
8709 if (cu->language == language_cplus
6e70227d 8710 && parent->tag == DW_TAG_namespace
acebe513
UW
8711 && strcmp (parent->name, "::") == 0
8712 && grandparent_scope == NULL)
8713 {
8714 parent->scope = NULL;
8715 parent->scope_set = 1;
8716 return NULL;
8717 }
8718
9c6c53f7
SA
8719 if (pdi->tag == DW_TAG_enumerator)
8720 /* Enumerators should not get the name of the enumeration as a prefix. */
8721 parent->scope = grandparent_scope;
8722 else if (parent->tag == DW_TAG_namespace
f55ee35c 8723 || parent->tag == DW_TAG_module
72bf9492
DJ
8724 || parent->tag == DW_TAG_structure_type
8725 || parent->tag == DW_TAG_class_type
680b30c7 8726 || parent->tag == DW_TAG_interface_type
ceeb3d5a
TT
8727 || parent->tag == DW_TAG_union_type
8728 || parent->tag == DW_TAG_enumeration_type)
72bf9492
DJ
8729 {
8730 if (grandparent_scope == NULL)
8731 parent->scope = parent->name;
8732 else
3e43a32a
MS
8733 parent->scope = typename_concat (&cu->comp_unit_obstack,
8734 grandparent_scope,
f55ee35c 8735 parent->name, 0, cu);
72bf9492 8736 }
72bf9492
DJ
8737 else
8738 {
8739 /* FIXME drow/2004-04-01: What should we be doing with
8740 function-local names? For partial symbols, we should probably be
8741 ignoring them. */
8742 complaint (&symfile_complaints,
9d8780f0
SM
8743 _("unhandled containing DIE tag %d for DIE at %s"),
8744 parent->tag, sect_offset_str (pdi->sect_off));
72bf9492 8745 parent->scope = grandparent_scope;
c906108c
SS
8746 }
8747
72bf9492
DJ
8748 parent->scope_set = 1;
8749 return parent->scope;
8750}
8751
8752/* Return the fully scoped name associated with PDI, from compilation unit
8753 CU. The result will be allocated with malloc. */
4568ecf9 8754
72bf9492
DJ
8755static char *
8756partial_die_full_name (struct partial_die_info *pdi,
8757 struct dwarf2_cu *cu)
8758{
15d034d0 8759 const char *parent_scope;
72bf9492 8760
98bfdba5
PA
8761 /* If this is a template instantiation, we can not work out the
8762 template arguments from partial DIEs. So, unfortunately, we have
8763 to go through the full DIEs. At least any work we do building
8764 types here will be reused if full symbols are loaded later. */
8765 if (pdi->has_template_arguments)
8766 {
52356b79 8767 pdi->fixup (cu);
98bfdba5
PA
8768
8769 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
8770 {
8771 struct die_info *die;
8772 struct attribute attr;
8773 struct dwarf2_cu *ref_cu = cu;
8774
b64f50a1 8775 /* DW_FORM_ref_addr is using section offset. */
b4069958 8776 attr.name = (enum dwarf_attribute) 0;
98bfdba5 8777 attr.form = DW_FORM_ref_addr;
9c541725 8778 attr.u.unsnd = to_underlying (pdi->sect_off);
98bfdba5
PA
8779 die = follow_die_ref (NULL, &attr, &ref_cu);
8780
8781 return xstrdup (dwarf2_full_name (NULL, die, ref_cu));
8782 }
8783 }
8784
72bf9492
DJ
8785 parent_scope = partial_die_parent_scope (pdi, cu);
8786 if (parent_scope == NULL)
8787 return NULL;
8788 else
f55ee35c 8789 return typename_concat (NULL, parent_scope, pdi->name, 0, cu);
c906108c
SS
8790}
8791
8792static void
72bf9492 8793add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
c906108c 8794{
518817b3
SM
8795 struct dwarf2_per_objfile *dwarf2_per_objfile
8796 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 8797 struct objfile *objfile = dwarf2_per_objfile->objfile;
3e29f34a 8798 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c 8799 CORE_ADDR addr = 0;
15d034d0 8800 const char *actual_name = NULL;
e142c38c 8801 CORE_ADDR baseaddr;
15d034d0 8802 char *built_actual_name;
e142c38c
DJ
8803
8804 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 8805
15d034d0
TT
8806 built_actual_name = partial_die_full_name (pdi, cu);
8807 if (built_actual_name != NULL)
8808 actual_name = built_actual_name;
63d06c5c 8809
72bf9492
DJ
8810 if (actual_name == NULL)
8811 actual_name = pdi->name;
8812
c906108c
SS
8813 switch (pdi->tag)
8814 {
b1dc1806 8815 case DW_TAG_inlined_subroutine:
c906108c 8816 case DW_TAG_subprogram:
3e29f34a 8817 addr = gdbarch_adjust_dwarf2_addr (gdbarch, pdi->lowpc + baseaddr);
2cfa0c8d 8818 if (pdi->is_external || cu->language == language_ada)
c906108c 8819 {
2cfa0c8d
JB
8820 /* brobecker/2007-12-26: Normally, only "external" DIEs are part
8821 of the global scope. But in Ada, we want to be able to access
8822 nested procedures globally. So all Ada subprograms are stored
8823 in the global scope. */
f47fb265 8824 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 8825 built_actual_name != NULL,
f47fb265
MS
8826 VAR_DOMAIN, LOC_BLOCK,
8827 &objfile->global_psymbols,
1762568f 8828 addr, cu->language, objfile);
c906108c
SS
8829 }
8830 else
8831 {
f47fb265 8832 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 8833 built_actual_name != NULL,
f47fb265
MS
8834 VAR_DOMAIN, LOC_BLOCK,
8835 &objfile->static_psymbols,
1762568f 8836 addr, cu->language, objfile);
c906108c 8837 }
0c1b455e
TT
8838
8839 if (pdi->main_subprogram && actual_name != NULL)
8840 set_objfile_main_name (objfile, actual_name, cu->language);
c906108c 8841 break;
72929c62
JB
8842 case DW_TAG_constant:
8843 {
af5bf4ad 8844 std::vector<partial_symbol *> *list;
72929c62
JB
8845
8846 if (pdi->is_external)
8847 list = &objfile->global_psymbols;
8848 else
8849 list = &objfile->static_psymbols;
f47fb265 8850 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 8851 built_actual_name != NULL, VAR_DOMAIN, LOC_STATIC,
1762568f 8852 list, 0, cu->language, objfile);
72929c62
JB
8853 }
8854 break;
c906108c 8855 case DW_TAG_variable:
95554aad
TT
8856 if (pdi->d.locdesc)
8857 addr = decode_locdesc (pdi->d.locdesc, cu);
caac4577 8858
95554aad 8859 if (pdi->d.locdesc
caac4577
JG
8860 && addr == 0
8861 && !dwarf2_per_objfile->has_section_at_zero)
8862 {
8863 /* A global or static variable may also have been stripped
8864 out by the linker if unused, in which case its address
8865 will be nullified; do not add such variables into partial
8866 symbol table then. */
8867 }
8868 else if (pdi->is_external)
c906108c
SS
8869 {
8870 /* Global Variable.
8871 Don't enter into the minimal symbol tables as there is
8872 a minimal symbol table entry from the ELF symbols already.
8873 Enter into partial symbol table if it has a location
8874 descriptor or a type.
8875 If the location descriptor is missing, new_symbol will create
8876 a LOC_UNRESOLVED symbol, the address of the variable will then
8877 be determined from the minimal symbol table whenever the variable
8878 is referenced.
8879 The address for the partial symbol table entry is not
8880 used by GDB, but it comes in handy for debugging partial symbol
8881 table building. */
8882
95554aad 8883 if (pdi->d.locdesc || pdi->has_type)
f47fb265 8884 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 8885 built_actual_name != NULL,
f47fb265
MS
8886 VAR_DOMAIN, LOC_STATIC,
8887 &objfile->global_psymbols,
1762568f 8888 addr + baseaddr,
f47fb265 8889 cu->language, objfile);
c906108c
SS
8890 }
8891 else
8892 {
ff908ebf
AW
8893 int has_loc = pdi->d.locdesc != NULL;
8894
8895 /* Static Variable. Skip symbols whose value we cannot know (those
8896 without location descriptors or constant values). */
8897 if (!has_loc && !pdi->has_const_value)
decbce07 8898 {
15d034d0 8899 xfree (built_actual_name);
decbce07
MS
8900 return;
8901 }
ff908ebf 8902
f47fb265 8903 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 8904 built_actual_name != NULL,
f47fb265
MS
8905 VAR_DOMAIN, LOC_STATIC,
8906 &objfile->static_psymbols,
ff908ebf 8907 has_loc ? addr + baseaddr : (CORE_ADDR) 0,
f47fb265 8908 cu->language, objfile);
c906108c
SS
8909 }
8910 break;
8911 case DW_TAG_typedef:
8912 case DW_TAG_base_type:
a02abb62 8913 case DW_TAG_subrange_type:
38d518c9 8914 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 8915 built_actual_name != NULL,
176620f1 8916 VAR_DOMAIN, LOC_TYPEDEF,
c906108c 8917 &objfile->static_psymbols,
1762568f 8918 0, cu->language, objfile);
c906108c 8919 break;
74921315 8920 case DW_TAG_imported_declaration:
72bf9492
DJ
8921 case DW_TAG_namespace:
8922 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 8923 built_actual_name != NULL,
72bf9492
DJ
8924 VAR_DOMAIN, LOC_TYPEDEF,
8925 &objfile->global_psymbols,
1762568f 8926 0, cu->language, objfile);
72bf9492 8927 break;
530e8392
KB
8928 case DW_TAG_module:
8929 add_psymbol_to_list (actual_name, strlen (actual_name),
8930 built_actual_name != NULL,
8931 MODULE_DOMAIN, LOC_TYPEDEF,
8932 &objfile->global_psymbols,
1762568f 8933 0, cu->language, objfile);
530e8392 8934 break;
c906108c 8935 case DW_TAG_class_type:
680b30c7 8936 case DW_TAG_interface_type:
c906108c
SS
8937 case DW_TAG_structure_type:
8938 case DW_TAG_union_type:
8939 case DW_TAG_enumeration_type:
fa4028e9
JB
8940 /* Skip external references. The DWARF standard says in the section
8941 about "Structure, Union, and Class Type Entries": "An incomplete
8942 structure, union or class type is represented by a structure,
8943 union or class entry that does not have a byte size attribute
8944 and that has a DW_AT_declaration attribute." */
8945 if (!pdi->has_byte_size && pdi->is_declaration)
decbce07 8946 {
15d034d0 8947 xfree (built_actual_name);
decbce07
MS
8948 return;
8949 }
fa4028e9 8950
63d06c5c
DC
8951 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
8952 static vs. global. */
38d518c9 8953 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 8954 built_actual_name != NULL,
176620f1 8955 STRUCT_DOMAIN, LOC_TYPEDEF,
9c37b5ae 8956 cu->language == language_cplus
63d06c5c
DC
8957 ? &objfile->global_psymbols
8958 : &objfile->static_psymbols,
1762568f 8959 0, cu->language, objfile);
c906108c 8960
c906108c
SS
8961 break;
8962 case DW_TAG_enumerator:
38d518c9 8963 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 8964 built_actual_name != NULL,
176620f1 8965 VAR_DOMAIN, LOC_CONST,
9c37b5ae 8966 cu->language == language_cplus
f6fe98ef
DJ
8967 ? &objfile->global_psymbols
8968 : &objfile->static_psymbols,
1762568f 8969 0, cu->language, objfile);
c906108c
SS
8970 break;
8971 default:
8972 break;
8973 }
5c4e30ca 8974
15d034d0 8975 xfree (built_actual_name);
c906108c
SS
8976}
8977
5c4e30ca
DC
8978/* Read a partial die corresponding to a namespace; also, add a symbol
8979 corresponding to that namespace to the symbol table. NAMESPACE is
8980 the name of the enclosing namespace. */
91c24f0a 8981
72bf9492
DJ
8982static void
8983add_partial_namespace (struct partial_die_info *pdi,
91c24f0a 8984 CORE_ADDR *lowpc, CORE_ADDR *highpc,
cdc07690 8985 int set_addrmap, struct dwarf2_cu *cu)
91c24f0a 8986{
72bf9492 8987 /* Add a symbol for the namespace. */
e7c27a73 8988
72bf9492 8989 add_partial_symbol (pdi, cu);
5c4e30ca
DC
8990
8991 /* Now scan partial symbols in that namespace. */
8992
91c24f0a 8993 if (pdi->has_children)
cdc07690 8994 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
91c24f0a
DC
8995}
8996
5d7cb8df
JK
8997/* Read a partial die corresponding to a Fortran module. */
8998
8999static void
9000add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
cdc07690 9001 CORE_ADDR *highpc, int set_addrmap, struct dwarf2_cu *cu)
5d7cb8df 9002{
530e8392
KB
9003 /* Add a symbol for the namespace. */
9004
9005 add_partial_symbol (pdi, cu);
9006
f55ee35c 9007 /* Now scan partial symbols in that module. */
5d7cb8df
JK
9008
9009 if (pdi->has_children)
cdc07690 9010 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
5d7cb8df
JK
9011}
9012
b1dc1806
XR
9013/* Read a partial die corresponding to a subprogram or an inlined
9014 subprogram and create a partial symbol for that subprogram.
9015 When the CU language allows it, this routine also defines a partial
9016 symbol for each nested subprogram that this subprogram contains.
9017 If SET_ADDRMAP is true, record the covered ranges in the addrmap.
9018 Set *LOWPC and *HIGHPC to the lowest and highest PC values found in PDI.
6e70227d 9019
cdc07690
YQ
9020 PDI may also be a lexical block, in which case we simply search
9021 recursively for subprograms defined inside that lexical block.
bc30ff58
JB
9022 Again, this is only performed when the CU language allows this
9023 type of definitions. */
9024
9025static void
9026add_partial_subprogram (struct partial_die_info *pdi,
9027 CORE_ADDR *lowpc, CORE_ADDR *highpc,
cdc07690 9028 int set_addrmap, struct dwarf2_cu *cu)
bc30ff58 9029{
b1dc1806 9030 if (pdi->tag == DW_TAG_subprogram || pdi->tag == DW_TAG_inlined_subroutine)
bc30ff58
JB
9031 {
9032 if (pdi->has_pc_info)
9033 {
9034 if (pdi->lowpc < *lowpc)
9035 *lowpc = pdi->lowpc;
9036 if (pdi->highpc > *highpc)
9037 *highpc = pdi->highpc;
cdc07690 9038 if (set_addrmap)
5734ee8b 9039 {
518817b3 9040 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
3e29f34a
MR
9041 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9042 CORE_ADDR baseaddr;
9043 CORE_ADDR highpc;
9044 CORE_ADDR lowpc;
5734ee8b
DJ
9045
9046 baseaddr = ANOFFSET (objfile->section_offsets,
9047 SECT_OFF_TEXT (objfile));
3e29f34a
MR
9048 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch,
9049 pdi->lowpc + baseaddr);
9050 highpc = gdbarch_adjust_dwarf2_addr (gdbarch,
9051 pdi->highpc + baseaddr);
9052 addrmap_set_empty (objfile->psymtabs_addrmap, lowpc, highpc - 1,
9291a0cd 9053 cu->per_cu->v.psymtab);
5734ee8b 9054 }
481860b3
GB
9055 }
9056
9057 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
9058 {
bc30ff58 9059 if (!pdi->is_declaration)
e8d05480
JB
9060 /* Ignore subprogram DIEs that do not have a name, they are
9061 illegal. Do not emit a complaint at this point, we will
9062 do so when we convert this psymtab into a symtab. */
9063 if (pdi->name)
9064 add_partial_symbol (pdi, cu);
bc30ff58
JB
9065 }
9066 }
6e70227d 9067
bc30ff58
JB
9068 if (! pdi->has_children)
9069 return;
9070
9071 if (cu->language == language_ada)
9072 {
9073 pdi = pdi->die_child;
9074 while (pdi != NULL)
9075 {
52356b79 9076 pdi->fixup (cu);
bc30ff58 9077 if (pdi->tag == DW_TAG_subprogram
b1dc1806 9078 || pdi->tag == DW_TAG_inlined_subroutine
bc30ff58 9079 || pdi->tag == DW_TAG_lexical_block)
cdc07690 9080 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
bc30ff58
JB
9081 pdi = pdi->die_sibling;
9082 }
9083 }
9084}
9085
91c24f0a
DC
9086/* Read a partial die corresponding to an enumeration type. */
9087
72bf9492
DJ
9088static void
9089add_partial_enumeration (struct partial_die_info *enum_pdi,
9090 struct dwarf2_cu *cu)
91c24f0a 9091{
72bf9492 9092 struct partial_die_info *pdi;
91c24f0a
DC
9093
9094 if (enum_pdi->name != NULL)
72bf9492
DJ
9095 add_partial_symbol (enum_pdi, cu);
9096
9097 pdi = enum_pdi->die_child;
9098 while (pdi)
91c24f0a 9099 {
72bf9492 9100 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
e2e0b3e5 9101 complaint (&symfile_complaints, _("malformed enumerator DIE ignored"));
91c24f0a 9102 else
72bf9492
DJ
9103 add_partial_symbol (pdi, cu);
9104 pdi = pdi->die_sibling;
91c24f0a 9105 }
91c24f0a
DC
9106}
9107
6caca83c
CC
9108/* Return the initial uleb128 in the die at INFO_PTR. */
9109
9110static unsigned int
d521ce57 9111peek_abbrev_code (bfd *abfd, const gdb_byte *info_ptr)
6caca83c
CC
9112{
9113 unsigned int bytes_read;
9114
9115 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9116}
9117
685af9cd
TT
9118/* Read the initial uleb128 in the die at INFO_PTR in compilation unit
9119 READER::CU. Use READER::ABBREV_TABLE to lookup any abbreviation.
9120
4bb7a0a7
DJ
9121 Return the corresponding abbrev, or NULL if the number is zero (indicating
9122 an empty DIE). In either case *BYTES_READ will be set to the length of
9123 the initial number. */
9124
9125static struct abbrev_info *
685af9cd
TT
9126peek_die_abbrev (const die_reader_specs &reader,
9127 const gdb_byte *info_ptr, unsigned int *bytes_read)
4bb7a0a7 9128{
685af9cd 9129 dwarf2_cu *cu = reader.cu;
518817b3 9130 bfd *abfd = cu->per_cu->dwarf2_per_objfile->objfile->obfd;
685af9cd
TT
9131 unsigned int abbrev_number
9132 = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
4bb7a0a7
DJ
9133
9134 if (abbrev_number == 0)
9135 return NULL;
9136
685af9cd 9137 abbrev_info *abbrev = reader.abbrev_table->lookup_abbrev (abbrev_number);
4bb7a0a7
DJ
9138 if (!abbrev)
9139 {
422b9917 9140 error (_("Dwarf Error: Could not find abbrev number %d in %s"
9d8780f0 9141 " at offset %s [in module %s]"),
422b9917 9142 abbrev_number, cu->per_cu->is_debug_types ? "TU" : "CU",
9d8780f0 9143 sect_offset_str (cu->header.sect_off), bfd_get_filename (abfd));
4bb7a0a7
DJ
9144 }
9145
9146 return abbrev;
9147}
9148
93311388
DE
9149/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
9150 Returns a pointer to the end of a series of DIEs, terminated by an empty
4bb7a0a7
DJ
9151 DIE. Any children of the skipped DIEs will also be skipped. */
9152
d521ce57
TT
9153static const gdb_byte *
9154skip_children (const struct die_reader_specs *reader, const gdb_byte *info_ptr)
4bb7a0a7 9155{
4bb7a0a7
DJ
9156 while (1)
9157 {
685af9cd
TT
9158 unsigned int bytes_read;
9159 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
9160
4bb7a0a7
DJ
9161 if (abbrev == NULL)
9162 return info_ptr + bytes_read;
9163 else
dee91e82 9164 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
4bb7a0a7
DJ
9165 }
9166}
9167
93311388
DE
9168/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
9169 INFO_PTR should point just after the initial uleb128 of a DIE, and the
4bb7a0a7
DJ
9170 abbrev corresponding to that skipped uleb128 should be passed in
9171 ABBREV. Returns a pointer to this DIE's sibling, skipping any
9172 children. */
9173
d521ce57
TT
9174static const gdb_byte *
9175skip_one_die (const struct die_reader_specs *reader, const gdb_byte *info_ptr,
dee91e82 9176 struct abbrev_info *abbrev)
4bb7a0a7
DJ
9177{
9178 unsigned int bytes_read;
9179 struct attribute attr;
dee91e82
DE
9180 bfd *abfd = reader->abfd;
9181 struct dwarf2_cu *cu = reader->cu;
d521ce57 9182 const gdb_byte *buffer = reader->buffer;
f664829e 9183 const gdb_byte *buffer_end = reader->buffer_end;
4bb7a0a7
DJ
9184 unsigned int form, i;
9185
9186 for (i = 0; i < abbrev->num_attrs; i++)
9187 {
9188 /* The only abbrev we care about is DW_AT_sibling. */
9189 if (abbrev->attrs[i].name == DW_AT_sibling)
9190 {
dee91e82 9191 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
4bb7a0a7 9192 if (attr.form == DW_FORM_ref_addr)
3e43a32a
MS
9193 complaint (&symfile_complaints,
9194 _("ignoring absolute DW_AT_sibling"));
4bb7a0a7 9195 else
b9502d3f 9196 {
9c541725
PA
9197 sect_offset off = dwarf2_get_ref_die_offset (&attr);
9198 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
b9502d3f
WN
9199
9200 if (sibling_ptr < info_ptr)
9201 complaint (&symfile_complaints,
9202 _("DW_AT_sibling points backwards"));
22869d73
KS
9203 else if (sibling_ptr > reader->buffer_end)
9204 dwarf2_section_buffer_overflow_complaint (reader->die_section);
b9502d3f
WN
9205 else
9206 return sibling_ptr;
9207 }
4bb7a0a7
DJ
9208 }
9209
9210 /* If it isn't DW_AT_sibling, skip this attribute. */
9211 form = abbrev->attrs[i].form;
9212 skip_attribute:
9213 switch (form)
9214 {
4bb7a0a7 9215 case DW_FORM_ref_addr:
ae411497
TT
9216 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
9217 and later it is offset sized. */
9218 if (cu->header.version == 2)
9219 info_ptr += cu->header.addr_size;
9220 else
9221 info_ptr += cu->header.offset_size;
9222 break;
36586728
TT
9223 case DW_FORM_GNU_ref_alt:
9224 info_ptr += cu->header.offset_size;
9225 break;
ae411497 9226 case DW_FORM_addr:
4bb7a0a7
DJ
9227 info_ptr += cu->header.addr_size;
9228 break;
9229 case DW_FORM_data1:
9230 case DW_FORM_ref1:
9231 case DW_FORM_flag:
9232 info_ptr += 1;
9233 break;
2dc7f7b3 9234 case DW_FORM_flag_present:
43988095 9235 case DW_FORM_implicit_const:
2dc7f7b3 9236 break;
4bb7a0a7
DJ
9237 case DW_FORM_data2:
9238 case DW_FORM_ref2:
9239 info_ptr += 2;
9240 break;
9241 case DW_FORM_data4:
9242 case DW_FORM_ref4:
9243 info_ptr += 4;
9244 break;
9245 case DW_FORM_data8:
9246 case DW_FORM_ref8:
55f1336d 9247 case DW_FORM_ref_sig8:
4bb7a0a7
DJ
9248 info_ptr += 8;
9249 break;
0224619f
JK
9250 case DW_FORM_data16:
9251 info_ptr += 16;
9252 break;
4bb7a0a7 9253 case DW_FORM_string:
9b1c24c8 9254 read_direct_string (abfd, info_ptr, &bytes_read);
4bb7a0a7
DJ
9255 info_ptr += bytes_read;
9256 break;
2dc7f7b3 9257 case DW_FORM_sec_offset:
4bb7a0a7 9258 case DW_FORM_strp:
36586728 9259 case DW_FORM_GNU_strp_alt:
4bb7a0a7
DJ
9260 info_ptr += cu->header.offset_size;
9261 break;
2dc7f7b3 9262 case DW_FORM_exprloc:
4bb7a0a7
DJ
9263 case DW_FORM_block:
9264 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9265 info_ptr += bytes_read;
9266 break;
9267 case DW_FORM_block1:
9268 info_ptr += 1 + read_1_byte (abfd, info_ptr);
9269 break;
9270 case DW_FORM_block2:
9271 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
9272 break;
9273 case DW_FORM_block4:
9274 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
9275 break;
9276 case DW_FORM_sdata:
9277 case DW_FORM_udata:
9278 case DW_FORM_ref_udata:
3019eac3
DE
9279 case DW_FORM_GNU_addr_index:
9280 case DW_FORM_GNU_str_index:
d521ce57 9281 info_ptr = safe_skip_leb128 (info_ptr, buffer_end);
4bb7a0a7
DJ
9282 break;
9283 case DW_FORM_indirect:
9284 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9285 info_ptr += bytes_read;
9286 /* We need to continue parsing from here, so just go back to
9287 the top. */
9288 goto skip_attribute;
9289
9290 default:
3e43a32a
MS
9291 error (_("Dwarf Error: Cannot handle %s "
9292 "in DWARF reader [in module %s]"),
4bb7a0a7
DJ
9293 dwarf_form_name (form),
9294 bfd_get_filename (abfd));
9295 }
9296 }
9297
9298 if (abbrev->has_children)
dee91e82 9299 return skip_children (reader, info_ptr);
4bb7a0a7
DJ
9300 else
9301 return info_ptr;
9302}
9303
93311388 9304/* Locate ORIG_PDI's sibling.
dee91e82 9305 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
91c24f0a 9306
d521ce57 9307static const gdb_byte *
dee91e82
DE
9308locate_pdi_sibling (const struct die_reader_specs *reader,
9309 struct partial_die_info *orig_pdi,
d521ce57 9310 const gdb_byte *info_ptr)
91c24f0a
DC
9311{
9312 /* Do we know the sibling already? */
72bf9492 9313
91c24f0a
DC
9314 if (orig_pdi->sibling)
9315 return orig_pdi->sibling;
9316
9317 /* Are there any children to deal with? */
9318
9319 if (!orig_pdi->has_children)
9320 return info_ptr;
9321
4bb7a0a7 9322 /* Skip the children the long way. */
91c24f0a 9323
dee91e82 9324 return skip_children (reader, info_ptr);
91c24f0a
DC
9325}
9326
257e7a09 9327/* Expand this partial symbol table into a full symbol table. SELF is
442e4d9c 9328 not NULL. */
c906108c
SS
9329
9330static void
257e7a09
YQ
9331dwarf2_read_symtab (struct partial_symtab *self,
9332 struct objfile *objfile)
c906108c 9333{
ed2dc618
SM
9334 struct dwarf2_per_objfile *dwarf2_per_objfile
9335 = get_dwarf2_per_objfile (objfile);
9336
257e7a09 9337 if (self->readin)
c906108c 9338 {
442e4d9c 9339 warning (_("bug: psymtab for %s is already read in."),
257e7a09 9340 self->filename);
442e4d9c
YQ
9341 }
9342 else
9343 {
9344 if (info_verbose)
c906108c 9345 {
442e4d9c 9346 printf_filtered (_("Reading in symbols for %s..."),
257e7a09 9347 self->filename);
442e4d9c 9348 gdb_flush (gdb_stdout);
c906108c 9349 }
c906108c 9350
442e4d9c
YQ
9351 /* If this psymtab is constructed from a debug-only objfile, the
9352 has_section_at_zero flag will not necessarily be correct. We
9353 can get the correct value for this flag by looking at the data
9354 associated with the (presumably stripped) associated objfile. */
9355 if (objfile->separate_debug_objfile_backlink)
9356 {
9357 struct dwarf2_per_objfile *dpo_backlink
ed2dc618 9358 = get_dwarf2_per_objfile (objfile->separate_debug_objfile_backlink);
9a619af0 9359
442e4d9c
YQ
9360 dwarf2_per_objfile->has_section_at_zero
9361 = dpo_backlink->has_section_at_zero;
9362 }
b2ab525c 9363
442e4d9c 9364 dwarf2_per_objfile->reading_partial_symbols = 0;
98bfdba5 9365
257e7a09 9366 psymtab_to_symtab_1 (self);
c906108c 9367
442e4d9c
YQ
9368 /* Finish up the debug error message. */
9369 if (info_verbose)
9370 printf_filtered (_("done.\n"));
c906108c 9371 }
95554aad 9372
ed2dc618 9373 process_cu_includes (dwarf2_per_objfile);
c906108c 9374}
9cdd5dbd
DE
9375\f
9376/* Reading in full CUs. */
c906108c 9377
10b3939b
DJ
9378/* Add PER_CU to the queue. */
9379
9380static void
95554aad
TT
9381queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
9382 enum language pretend_language)
10b3939b
DJ
9383{
9384 struct dwarf2_queue_item *item;
9385
9386 per_cu->queued = 1;
8d749320 9387 item = XNEW (struct dwarf2_queue_item);
10b3939b 9388 item->per_cu = per_cu;
95554aad 9389 item->pretend_language = pretend_language;
10b3939b
DJ
9390 item->next = NULL;
9391
9392 if (dwarf2_queue == NULL)
9393 dwarf2_queue = item;
9394 else
9395 dwarf2_queue_tail->next = item;
9396
9397 dwarf2_queue_tail = item;
9398}
9399
89e63ee4
DE
9400/* If PER_CU is not yet queued, add it to the queue.
9401 If DEPENDENT_CU is non-NULL, it has a reference to PER_CU so add a
9402 dependency.
0907af0c 9403 The result is non-zero if PER_CU was queued, otherwise the result is zero
69d751e3
DE
9404 meaning either PER_CU is already queued or it is already loaded.
9405
9406 N.B. There is an invariant here that if a CU is queued then it is loaded.
9407 The caller is required to load PER_CU if we return non-zero. */
0907af0c
DE
9408
9409static int
89e63ee4 9410maybe_queue_comp_unit (struct dwarf2_cu *dependent_cu,
0907af0c
DE
9411 struct dwarf2_per_cu_data *per_cu,
9412 enum language pretend_language)
9413{
9414 /* We may arrive here during partial symbol reading, if we need full
9415 DIEs to process an unusual case (e.g. template arguments). Do
9416 not queue PER_CU, just tell our caller to load its DIEs. */
ed2dc618 9417 if (per_cu->dwarf2_per_objfile->reading_partial_symbols)
0907af0c
DE
9418 {
9419 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
9420 return 1;
9421 return 0;
9422 }
9423
9424 /* Mark the dependence relation so that we don't flush PER_CU
9425 too early. */
89e63ee4
DE
9426 if (dependent_cu != NULL)
9427 dwarf2_add_dependence (dependent_cu, per_cu);
0907af0c
DE
9428
9429 /* If it's already on the queue, we have nothing to do. */
9430 if (per_cu->queued)
9431 return 0;
9432
9433 /* If the compilation unit is already loaded, just mark it as
9434 used. */
9435 if (per_cu->cu != NULL)
9436 {
9437 per_cu->cu->last_used = 0;
9438 return 0;
9439 }
9440
9441 /* Add it to the queue. */
9442 queue_comp_unit (per_cu, pretend_language);
9443
9444 return 1;
9445}
9446
10b3939b
DJ
9447/* Process the queue. */
9448
9449static void
ed2dc618 9450process_queue (struct dwarf2_per_objfile *dwarf2_per_objfile)
10b3939b
DJ
9451{
9452 struct dwarf2_queue_item *item, *next_item;
9453
b4f54984 9454 if (dwarf_read_debug)
45cfd468
DE
9455 {
9456 fprintf_unfiltered (gdb_stdlog,
9457 "Expanding one or more symtabs of objfile %s ...\n",
4262abfb 9458 objfile_name (dwarf2_per_objfile->objfile));
45cfd468
DE
9459 }
9460
03dd20cc
DJ
9461 /* The queue starts out with one item, but following a DIE reference
9462 may load a new CU, adding it to the end of the queue. */
10b3939b
DJ
9463 for (item = dwarf2_queue; item != NULL; dwarf2_queue = item = next_item)
9464 {
cc12ce38
DE
9465 if ((dwarf2_per_objfile->using_index
9466 ? !item->per_cu->v.quick->compunit_symtab
9467 : (item->per_cu->v.psymtab && !item->per_cu->v.psymtab->readin))
9468 /* Skip dummy CUs. */
9469 && item->per_cu->cu != NULL)
f4dc4d17
DE
9470 {
9471 struct dwarf2_per_cu_data *per_cu = item->per_cu;
73be47f5 9472 unsigned int debug_print_threshold;
247f5c4f 9473 char buf[100];
f4dc4d17 9474
247f5c4f 9475 if (per_cu->is_debug_types)
f4dc4d17 9476 {
247f5c4f
DE
9477 struct signatured_type *sig_type =
9478 (struct signatured_type *) per_cu;
9479
9d8780f0 9480 sprintf (buf, "TU %s at offset %s",
73be47f5 9481 hex_string (sig_type->signature),
9d8780f0 9482 sect_offset_str (per_cu->sect_off));
73be47f5
DE
9483 /* There can be 100s of TUs.
9484 Only print them in verbose mode. */
9485 debug_print_threshold = 2;
f4dc4d17 9486 }
247f5c4f 9487 else
73be47f5 9488 {
9d8780f0
SM
9489 sprintf (buf, "CU at offset %s",
9490 sect_offset_str (per_cu->sect_off));
73be47f5
DE
9491 debug_print_threshold = 1;
9492 }
247f5c4f 9493
b4f54984 9494 if (dwarf_read_debug >= debug_print_threshold)
247f5c4f 9495 fprintf_unfiltered (gdb_stdlog, "Expanding symtab of %s\n", buf);
f4dc4d17
DE
9496
9497 if (per_cu->is_debug_types)
9498 process_full_type_unit (per_cu, item->pretend_language);
9499 else
9500 process_full_comp_unit (per_cu, item->pretend_language);
9501
b4f54984 9502 if (dwarf_read_debug >= debug_print_threshold)
247f5c4f 9503 fprintf_unfiltered (gdb_stdlog, "Done expanding %s\n", buf);
f4dc4d17 9504 }
10b3939b
DJ
9505
9506 item->per_cu->queued = 0;
9507 next_item = item->next;
9508 xfree (item);
9509 }
9510
9511 dwarf2_queue_tail = NULL;
45cfd468 9512
b4f54984 9513 if (dwarf_read_debug)
45cfd468
DE
9514 {
9515 fprintf_unfiltered (gdb_stdlog, "Done expanding symtabs of %s.\n",
4262abfb 9516 objfile_name (dwarf2_per_objfile->objfile));
45cfd468 9517 }
10b3939b
DJ
9518}
9519
10b3939b
DJ
9520/* Read in full symbols for PST, and anything it depends on. */
9521
c906108c 9522static void
fba45db2 9523psymtab_to_symtab_1 (struct partial_symtab *pst)
c906108c 9524{
10b3939b 9525 struct dwarf2_per_cu_data *per_cu;
aaa75496
JB
9526 int i;
9527
95554aad
TT
9528 if (pst->readin)
9529 return;
9530
aaa75496 9531 for (i = 0; i < pst->number_of_dependencies; i++)
95554aad
TT
9532 if (!pst->dependencies[i]->readin
9533 && pst->dependencies[i]->user == NULL)
aaa75496
JB
9534 {
9535 /* Inform about additional files that need to be read in. */
9536 if (info_verbose)
9537 {
a3f17187 9538 /* FIXME: i18n: Need to make this a single string. */
aaa75496
JB
9539 fputs_filtered (" ", gdb_stdout);
9540 wrap_here ("");
9541 fputs_filtered ("and ", gdb_stdout);
9542 wrap_here ("");
9543 printf_filtered ("%s...", pst->dependencies[i]->filename);
0963b4bd 9544 wrap_here (""); /* Flush output. */
aaa75496
JB
9545 gdb_flush (gdb_stdout);
9546 }
9547 psymtab_to_symtab_1 (pst->dependencies[i]);
9548 }
9549
9a3c8263 9550 per_cu = (struct dwarf2_per_cu_data *) pst->read_symtab_private;
10b3939b
DJ
9551
9552 if (per_cu == NULL)
aaa75496
JB
9553 {
9554 /* It's an include file, no symbols to read for it.
9555 Everything is in the parent symtab. */
9556 pst->readin = 1;
9557 return;
9558 }
c906108c 9559
58f0c718 9560 dw2_do_instantiate_symtab (per_cu, false);
10b3939b
DJ
9561}
9562
dee91e82
DE
9563/* Trivial hash function for die_info: the hash value of a DIE
9564 is its offset in .debug_info for this objfile. */
10b3939b 9565
dee91e82
DE
9566static hashval_t
9567die_hash (const void *item)
10b3939b 9568{
9a3c8263 9569 const struct die_info *die = (const struct die_info *) item;
6502dd73 9570
9c541725 9571 return to_underlying (die->sect_off);
dee91e82 9572}
63d06c5c 9573
dee91e82
DE
9574/* Trivial comparison function for die_info structures: two DIEs
9575 are equal if they have the same offset. */
98bfdba5 9576
dee91e82
DE
9577static int
9578die_eq (const void *item_lhs, const void *item_rhs)
9579{
9a3c8263
SM
9580 const struct die_info *die_lhs = (const struct die_info *) item_lhs;
9581 const struct die_info *die_rhs = (const struct die_info *) item_rhs;
c906108c 9582
9c541725 9583 return die_lhs->sect_off == die_rhs->sect_off;
dee91e82 9584}
c906108c 9585
dee91e82
DE
9586/* die_reader_func for load_full_comp_unit.
9587 This is identical to read_signatured_type_reader,
9588 but is kept separate for now. */
c906108c 9589
dee91e82
DE
9590static void
9591load_full_comp_unit_reader (const struct die_reader_specs *reader,
d521ce57 9592 const gdb_byte *info_ptr,
dee91e82
DE
9593 struct die_info *comp_unit_die,
9594 int has_children,
9595 void *data)
9596{
9597 struct dwarf2_cu *cu = reader->cu;
9a3c8263 9598 enum language *language_ptr = (enum language *) data;
6caca83c 9599
dee91e82
DE
9600 gdb_assert (cu->die_hash == NULL);
9601 cu->die_hash =
9602 htab_create_alloc_ex (cu->header.length / 12,
9603 die_hash,
9604 die_eq,
9605 NULL,
9606 &cu->comp_unit_obstack,
9607 hashtab_obstack_allocate,
9608 dummy_obstack_deallocate);
e142c38c 9609
dee91e82
DE
9610 if (has_children)
9611 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
9612 &info_ptr, comp_unit_die);
9613 cu->dies = comp_unit_die;
9614 /* comp_unit_die is not stored in die_hash, no need. */
10b3939b
DJ
9615
9616 /* We try not to read any attributes in this function, because not
9cdd5dbd 9617 all CUs needed for references have been loaded yet, and symbol
10b3939b 9618 table processing isn't initialized. But we have to set the CU language,
dee91e82
DE
9619 or we won't be able to build types correctly.
9620 Similarly, if we do not read the producer, we can not apply
9621 producer-specific interpretation. */
95554aad 9622 prepare_one_comp_unit (cu, cu->dies, *language_ptr);
dee91e82 9623}
10b3939b 9624
dee91e82 9625/* Load the DIEs associated with PER_CU into memory. */
a6c727b2 9626
dee91e82 9627static void
95554aad 9628load_full_comp_unit (struct dwarf2_per_cu_data *this_cu,
58f0c718 9629 bool skip_partial,
95554aad 9630 enum language pretend_language)
dee91e82 9631{
3019eac3 9632 gdb_assert (! this_cu->is_debug_types);
c5b7e1cb 9633
58f0c718 9634 init_cutu_and_read_dies (this_cu, NULL, 1, 1, skip_partial,
f4dc4d17 9635 load_full_comp_unit_reader, &pretend_language);
10b3939b
DJ
9636}
9637
3da10d80
KS
9638/* Add a DIE to the delayed physname list. */
9639
9640static void
9641add_to_method_list (struct type *type, int fnfield_index, int index,
9642 const char *name, struct die_info *die,
9643 struct dwarf2_cu *cu)
9644{
9645 struct delayed_method_info mi;
9646 mi.type = type;
9647 mi.fnfield_index = fnfield_index;
9648 mi.index = index;
9649 mi.name = name;
9650 mi.die = die;
c89b44cd 9651 cu->method_list.push_back (mi);
3da10d80
KS
9652}
9653
3693fdb3
PA
9654/* Check whether [PHYSNAME, PHYSNAME+LEN) ends with a modifier like
9655 "const" / "volatile". If so, decrements LEN by the length of the
9656 modifier and return true. Otherwise return false. */
9657
9658template<size_t N>
9659static bool
9660check_modifier (const char *physname, size_t &len, const char (&mod)[N])
9661{
9662 size_t mod_len = sizeof (mod) - 1;
9663 if (len > mod_len && startswith (physname + (len - mod_len), mod))
9664 {
9665 len -= mod_len;
9666 return true;
9667 }
9668 return false;
9669}
9670
3da10d80
KS
9671/* Compute the physnames of any methods on the CU's method list.
9672
9673 The computation of method physnames is delayed in order to avoid the
9674 (bad) condition that one of the method's formal parameters is of an as yet
9675 incomplete type. */
9676
9677static void
9678compute_delayed_physnames (struct dwarf2_cu *cu)
9679{
3693fdb3 9680 /* Only C++ delays computing physnames. */
c89b44cd 9681 if (cu->method_list.empty ())
3693fdb3
PA
9682 return;
9683 gdb_assert (cu->language == language_cplus);
9684
c89b44cd 9685 for (struct delayed_method_info &mi : cu->method_list)
3da10d80 9686 {
1d06ead6 9687 const char *physname;
3da10d80 9688 struct fn_fieldlist *fn_flp
c89b44cd
TT
9689 = &TYPE_FN_FIELDLIST (mi.type, mi.fnfield_index);
9690 physname = dwarf2_physname (mi.name, mi.die, cu);
9691 TYPE_FN_FIELD_PHYSNAME (fn_flp->fn_fields, mi.index)
005e54bb 9692 = physname ? physname : "";
3693fdb3
PA
9693
9694 /* Since there's no tag to indicate whether a method is a
9695 const/volatile overload, extract that information out of the
9696 demangled name. */
9697 if (physname != NULL)
9698 {
9699 size_t len = strlen (physname);
9700
9701 while (1)
9702 {
9703 if (physname[len] == ')') /* shortcut */
9704 break;
9705 else if (check_modifier (physname, len, " const"))
c89b44cd 9706 TYPE_FN_FIELD_CONST (fn_flp->fn_fields, mi.index) = 1;
3693fdb3 9707 else if (check_modifier (physname, len, " volatile"))
c89b44cd 9708 TYPE_FN_FIELD_VOLATILE (fn_flp->fn_fields, mi.index) = 1;
3693fdb3
PA
9709 else
9710 break;
9711 }
9712 }
3da10d80 9713 }
c89b44cd
TT
9714
9715 /* The list is no longer needed. */
9716 cu->method_list.clear ();
3da10d80
KS
9717}
9718
a766d390
DE
9719/* Go objects should be embedded in a DW_TAG_module DIE,
9720 and it's not clear if/how imported objects will appear.
9721 To keep Go support simple until that's worked out,
9722 go back through what we've read and create something usable.
9723 We could do this while processing each DIE, and feels kinda cleaner,
9724 but that way is more invasive.
9725 This is to, for example, allow the user to type "p var" or "b main"
9726 without having to specify the package name, and allow lookups
9727 of module.object to work in contexts that use the expression
9728 parser. */
9729
9730static void
9731fixup_go_packaging (struct dwarf2_cu *cu)
9732{
9733 char *package_name = NULL;
9734 struct pending *list;
9735 int i;
9736
9737 for (list = global_symbols; list != NULL; list = list->next)
9738 {
9739 for (i = 0; i < list->nsyms; ++i)
9740 {
9741 struct symbol *sym = list->symbol[i];
9742
9743 if (SYMBOL_LANGUAGE (sym) == language_go
9744 && SYMBOL_CLASS (sym) == LOC_BLOCK)
9745 {
9746 char *this_package_name = go_symbol_package_name (sym);
9747
9748 if (this_package_name == NULL)
9749 continue;
9750 if (package_name == NULL)
9751 package_name = this_package_name;
9752 else
9753 {
518817b3
SM
9754 struct objfile *objfile
9755 = cu->per_cu->dwarf2_per_objfile->objfile;
a766d390
DE
9756 if (strcmp (package_name, this_package_name) != 0)
9757 complaint (&symfile_complaints,
9758 _("Symtab %s has objects from two different Go packages: %s and %s"),
08be3fe3
DE
9759 (symbol_symtab (sym) != NULL
9760 ? symtab_to_filename_for_display
9761 (symbol_symtab (sym))
e3b94546 9762 : objfile_name (objfile)),
a766d390
DE
9763 this_package_name, package_name);
9764 xfree (this_package_name);
9765 }
9766 }
9767 }
9768 }
9769
9770 if (package_name != NULL)
9771 {
518817b3 9772 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
34a68019 9773 const char *saved_package_name
224c3ddb
SM
9774 = (const char *) obstack_copy0 (&objfile->per_bfd->storage_obstack,
9775 package_name,
9776 strlen (package_name));
19f392bc
UW
9777 struct type *type = init_type (objfile, TYPE_CODE_MODULE, 0,
9778 saved_package_name);
a766d390
DE
9779 struct symbol *sym;
9780
9781 TYPE_TAG_NAME (type) = TYPE_NAME (type);
9782
e623cf5d 9783 sym = allocate_symbol (objfile);
f85f34ed 9784 SYMBOL_SET_LANGUAGE (sym, language_go, &objfile->objfile_obstack);
86f62fd7
TT
9785 SYMBOL_SET_NAMES (sym, saved_package_name,
9786 strlen (saved_package_name), 0, objfile);
a766d390
DE
9787 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
9788 e.g., "main" finds the "main" module and not C's main(). */
9789 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
f1e6e072 9790 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
a766d390
DE
9791 SYMBOL_TYPE (sym) = type;
9792
9793 add_symbol_to_list (sym, &global_symbols);
9794
9795 xfree (package_name);
9796 }
9797}
9798
c9317f21
TT
9799/* Allocate a fully-qualified name consisting of the two parts on the
9800 obstack. */
9801
9802static const char *
9803rust_fully_qualify (struct obstack *obstack, const char *p1, const char *p2)
9804{
9805 return obconcat (obstack, p1, "::", p2, (char *) NULL);
9806}
9807
9808/* A helper that allocates a struct discriminant_info to attach to a
9809 union type. */
9810
9811static struct discriminant_info *
9812alloc_discriminant_info (struct type *type, int discriminant_index,
9813 int default_index)
9814{
9815 gdb_assert (TYPE_CODE (type) == TYPE_CODE_UNION);
c7b15a66
TT
9816 gdb_assert (discriminant_index == -1
9817 || (discriminant_index >= 0
9818 && discriminant_index < TYPE_NFIELDS (type)));
c9317f21 9819 gdb_assert (default_index == -1
c7b15a66 9820 || (default_index >= 0 && default_index < TYPE_NFIELDS (type)));
c9317f21
TT
9821
9822 TYPE_FLAG_DISCRIMINATED_UNION (type) = 1;
9823
9824 struct discriminant_info *disc
9825 = ((struct discriminant_info *)
9826 TYPE_ZALLOC (type,
9827 offsetof (struct discriminant_info, discriminants)
9828 + TYPE_NFIELDS (type) * sizeof (disc->discriminants[0])));
9829 disc->default_index = default_index;
9830 disc->discriminant_index = discriminant_index;
9831
9832 struct dynamic_prop prop;
9833 prop.kind = PROP_UNDEFINED;
9834 prop.data.baton = disc;
9835
9836 add_dyn_prop (DYN_PROP_DISCRIMINATED, prop, type);
9837
9838 return disc;
9839}
9840
9841/* Some versions of rustc emitted enums in an unusual way.
9842
9843 Ordinary enums were emitted as unions. The first element of each
9844 structure in the union was named "RUST$ENUM$DISR". This element
9845 held the discriminant.
9846
9847 These versions of Rust also implemented the "non-zero"
9848 optimization. When the enum had two values, and one is empty and
9849 the other holds a pointer that cannot be zero, the pointer is used
9850 as the discriminant, with a zero value meaning the empty variant.
9851 Here, the union's first member is of the form
9852 RUST$ENCODED$ENUM$<fieldno>$<fieldno>$...$<variantname>
9853 where the fieldnos are the indices of the fields that should be
9854 traversed in order to find the field (which may be several fields deep)
9855 and the variantname is the name of the variant of the case when the
9856 field is zero.
9857
9858 This function recognizes whether TYPE is of one of these forms,
9859 and, if so, smashes it to be a variant type. */
9860
9861static void
9862quirk_rust_enum (struct type *type, struct objfile *objfile)
9863{
9864 gdb_assert (TYPE_CODE (type) == TYPE_CODE_UNION);
9865
9866 /* We don't need to deal with empty enums. */
9867 if (TYPE_NFIELDS (type) == 0)
9868 return;
9869
9870#define RUST_ENUM_PREFIX "RUST$ENCODED$ENUM$"
9871 if (TYPE_NFIELDS (type) == 1
9872 && startswith (TYPE_FIELD_NAME (type, 0), RUST_ENUM_PREFIX))
9873 {
9874 const char *name = TYPE_FIELD_NAME (type, 0) + strlen (RUST_ENUM_PREFIX);
9875
9876 /* Decode the field name to find the offset of the
9877 discriminant. */
9878 ULONGEST bit_offset = 0;
9879 struct type *field_type = TYPE_FIELD_TYPE (type, 0);
9880 while (name[0] >= '0' && name[0] <= '9')
9881 {
9882 char *tail;
9883 unsigned long index = strtoul (name, &tail, 10);
9884 name = tail;
9885 if (*name != '$'
9886 || index >= TYPE_NFIELDS (field_type)
9887 || (TYPE_FIELD_LOC_KIND (field_type, index)
9888 != FIELD_LOC_KIND_BITPOS))
9889 {
9890 complaint (&symfile_complaints,
9891 _("Could not parse Rust enum encoding string \"%s\""
9892 "[in module %s]"),
9893 TYPE_FIELD_NAME (type, 0),
9894 objfile_name (objfile));
9895 return;
9896 }
9897 ++name;
9898
9899 bit_offset += TYPE_FIELD_BITPOS (field_type, index);
9900 field_type = TYPE_FIELD_TYPE (field_type, index);
9901 }
9902
9903 /* Make a union to hold the variants. */
9904 struct type *union_type = alloc_type (objfile);
9905 TYPE_CODE (union_type) = TYPE_CODE_UNION;
9906 TYPE_NFIELDS (union_type) = 3;
9907 TYPE_FIELDS (union_type)
9908 = (struct field *) TYPE_ZALLOC (type, 3 * sizeof (struct field));
9909 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
2b4424c3 9910 set_type_align (union_type, TYPE_RAW_ALIGN (type));
c9317f21
TT
9911
9912 /* Put the discriminant must at index 0. */
9913 TYPE_FIELD_TYPE (union_type, 0) = field_type;
9914 TYPE_FIELD_ARTIFICIAL (union_type, 0) = 1;
9915 TYPE_FIELD_NAME (union_type, 0) = "<<discriminant>>";
9916 SET_FIELD_BITPOS (TYPE_FIELD (union_type, 0), bit_offset);
9917
9918 /* The order of fields doesn't really matter, so put the real
9919 field at index 1 and the data-less field at index 2. */
9920 struct discriminant_info *disc
9921 = alloc_discriminant_info (union_type, 0, 1);
9922 TYPE_FIELD (union_type, 1) = TYPE_FIELD (type, 0);
9923 TYPE_FIELD_NAME (union_type, 1)
9924 = rust_last_path_segment (TYPE_NAME (TYPE_FIELD_TYPE (union_type, 1)));
9925 TYPE_NAME (TYPE_FIELD_TYPE (union_type, 1))
9926 = rust_fully_qualify (&objfile->objfile_obstack, TYPE_NAME (type),
9927 TYPE_FIELD_NAME (union_type, 1));
9928
9929 const char *dataless_name
9930 = rust_fully_qualify (&objfile->objfile_obstack, TYPE_NAME (type),
9931 name);
9932 struct type *dataless_type = init_type (objfile, TYPE_CODE_VOID, 0,
9933 dataless_name);
9934 TYPE_FIELD_TYPE (union_type, 2) = dataless_type;
9935 /* NAME points into the original discriminant name, which
9936 already has the correct lifetime. */
9937 TYPE_FIELD_NAME (union_type, 2) = name;
9938 SET_FIELD_BITPOS (TYPE_FIELD (union_type, 2), 0);
9939 disc->discriminants[2] = 0;
9940
9941 /* Smash this type to be a structure type. We have to do this
9942 because the type has already been recorded. */
9943 TYPE_CODE (type) = TYPE_CODE_STRUCT;
9944 TYPE_NFIELDS (type) = 1;
9945 TYPE_FIELDS (type)
9946 = (struct field *) TYPE_ZALLOC (type, sizeof (struct field));
9947
9948 /* Install the variant part. */
9949 TYPE_FIELD_TYPE (type, 0) = union_type;
9950 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
9951 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
9952 }
9953 else if (TYPE_NFIELDS (type) == 1)
9954 {
9955 /* We assume that a union with a single field is a univariant
9956 enum. */
9957 /* Smash this type to be a structure type. We have to do this
9958 because the type has already been recorded. */
9959 TYPE_CODE (type) = TYPE_CODE_STRUCT;
9960
9961 /* Make a union to hold the variants. */
9962 struct type *union_type = alloc_type (objfile);
9963 TYPE_CODE (union_type) = TYPE_CODE_UNION;
9964 TYPE_NFIELDS (union_type) = TYPE_NFIELDS (type);
9965 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
2b4424c3 9966 set_type_align (union_type, TYPE_RAW_ALIGN (type));
c9317f21
TT
9967 TYPE_FIELDS (union_type) = TYPE_FIELDS (type);
9968
9969 struct type *field_type = TYPE_FIELD_TYPE (union_type, 0);
9970 const char *variant_name
9971 = rust_last_path_segment (TYPE_NAME (field_type));
9972 TYPE_FIELD_NAME (union_type, 0) = variant_name;
9973 TYPE_NAME (field_type)
9974 = rust_fully_qualify (&objfile->objfile_obstack,
c7b15a66 9975 TYPE_NAME (type), variant_name);
c9317f21
TT
9976
9977 /* Install the union in the outer struct type. */
9978 TYPE_NFIELDS (type) = 1;
9979 TYPE_FIELDS (type)
9980 = (struct field *) TYPE_ZALLOC (union_type, sizeof (struct field));
9981 TYPE_FIELD_TYPE (type, 0) = union_type;
9982 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
9983 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
9984
9985 alloc_discriminant_info (union_type, -1, 0);
9986 }
9987 else
9988 {
9989 struct type *disr_type = nullptr;
9990 for (int i = 0; i < TYPE_NFIELDS (type); ++i)
9991 {
9992 disr_type = TYPE_FIELD_TYPE (type, i);
9993
a037790e
TT
9994 if (TYPE_CODE (disr_type) != TYPE_CODE_STRUCT)
9995 {
9996 /* All fields of a true enum will be structs. */
9997 return;
9998 }
9999 else if (TYPE_NFIELDS (disr_type) == 0)
c9317f21
TT
10000 {
10001 /* Could be data-less variant, so keep going. */
a037790e 10002 disr_type = nullptr;
c9317f21
TT
10003 }
10004 else if (strcmp (TYPE_FIELD_NAME (disr_type, 0),
10005 "RUST$ENUM$DISR") != 0)
10006 {
10007 /* Not a Rust enum. */
10008 return;
10009 }
10010 else
10011 {
10012 /* Found one. */
10013 break;
10014 }
10015 }
10016
10017 /* If we got here without a discriminant, then it's probably
10018 just a union. */
10019 if (disr_type == nullptr)
10020 return;
10021
10022 /* Smash this type to be a structure type. We have to do this
10023 because the type has already been recorded. */
10024 TYPE_CODE (type) = TYPE_CODE_STRUCT;
10025
10026 /* Make a union to hold the variants. */
10027 struct field *disr_field = &TYPE_FIELD (disr_type, 0);
10028 struct type *union_type = alloc_type (objfile);
10029 TYPE_CODE (union_type) = TYPE_CODE_UNION;
10030 TYPE_NFIELDS (union_type) = 1 + TYPE_NFIELDS (type);
10031 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
2b4424c3 10032 set_type_align (union_type, TYPE_RAW_ALIGN (type));
c9317f21
TT
10033 TYPE_FIELDS (union_type)
10034 = (struct field *) TYPE_ZALLOC (union_type,
10035 (TYPE_NFIELDS (union_type)
10036 * sizeof (struct field)));
10037
10038 memcpy (TYPE_FIELDS (union_type) + 1, TYPE_FIELDS (type),
10039 TYPE_NFIELDS (type) * sizeof (struct field));
10040
10041 /* Install the discriminant at index 0 in the union. */
10042 TYPE_FIELD (union_type, 0) = *disr_field;
10043 TYPE_FIELD_ARTIFICIAL (union_type, 0) = 1;
10044 TYPE_FIELD_NAME (union_type, 0) = "<<discriminant>>";
10045
10046 /* Install the union in the outer struct type. */
10047 TYPE_FIELD_TYPE (type, 0) = union_type;
10048 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
10049 TYPE_NFIELDS (type) = 1;
10050
10051 /* Set the size and offset of the union type. */
10052 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
10053
10054 /* We need a way to find the correct discriminant given a
10055 variant name. For convenience we build a map here. */
10056 struct type *enum_type = FIELD_TYPE (*disr_field);
10057 std::unordered_map<std::string, ULONGEST> discriminant_map;
10058 for (int i = 0; i < TYPE_NFIELDS (enum_type); ++i)
10059 {
10060 if (TYPE_FIELD_LOC_KIND (enum_type, i) == FIELD_LOC_KIND_ENUMVAL)
10061 {
10062 const char *name
10063 = rust_last_path_segment (TYPE_FIELD_NAME (enum_type, i));
10064 discriminant_map[name] = TYPE_FIELD_ENUMVAL (enum_type, i);
10065 }
10066 }
10067
10068 int n_fields = TYPE_NFIELDS (union_type);
10069 struct discriminant_info *disc
10070 = alloc_discriminant_info (union_type, 0, -1);
10071 /* Skip the discriminant here. */
10072 for (int i = 1; i < n_fields; ++i)
10073 {
10074 /* Find the final word in the name of this variant's type.
10075 That name can be used to look up the correct
10076 discriminant. */
10077 const char *variant_name
10078 = rust_last_path_segment (TYPE_NAME (TYPE_FIELD_TYPE (union_type,
10079 i)));
10080
10081 auto iter = discriminant_map.find (variant_name);
10082 if (iter != discriminant_map.end ())
10083 disc->discriminants[i] = iter->second;
10084
bedda9ac 10085 /* Remove the discriminant field, if it exists. */
c9317f21 10086 struct type *sub_type = TYPE_FIELD_TYPE (union_type, i);
bedda9ac
TT
10087 if (TYPE_NFIELDS (sub_type) > 0)
10088 {
10089 --TYPE_NFIELDS (sub_type);
10090 ++TYPE_FIELDS (sub_type);
10091 }
c9317f21
TT
10092 TYPE_FIELD_NAME (union_type, i) = variant_name;
10093 TYPE_NAME (sub_type)
10094 = rust_fully_qualify (&objfile->objfile_obstack,
10095 TYPE_NAME (type), variant_name);
10096 }
10097 }
10098}
10099
10100/* Rewrite some Rust unions to be structures with variants parts. */
10101
10102static void
10103rust_union_quirks (struct dwarf2_cu *cu)
10104{
10105 gdb_assert (cu->language == language_rust);
10106 for (struct type *type : cu->rust_unions)
10107 quirk_rust_enum (type, cu->per_cu->dwarf2_per_objfile->objfile);
2d79090e
TT
10108 /* We don't need this any more. */
10109 cu->rust_unions.clear ();
c9317f21
TT
10110}
10111
95554aad
TT
10112/* Return the symtab for PER_CU. This works properly regardless of
10113 whether we're using the index or psymtabs. */
10114
43f3e411
DE
10115static struct compunit_symtab *
10116get_compunit_symtab (struct dwarf2_per_cu_data *per_cu)
95554aad 10117{
ed2dc618 10118 return (per_cu->dwarf2_per_objfile->using_index
43f3e411
DE
10119 ? per_cu->v.quick->compunit_symtab
10120 : per_cu->v.psymtab->compunit_symtab);
95554aad
TT
10121}
10122
10123/* A helper function for computing the list of all symbol tables
10124 included by PER_CU. */
10125
10126static void
43f3e411 10127recursively_compute_inclusions (VEC (compunit_symtab_ptr) **result,
ec94af83 10128 htab_t all_children, htab_t all_type_symtabs,
f9125b6c 10129 struct dwarf2_per_cu_data *per_cu,
43f3e411 10130 struct compunit_symtab *immediate_parent)
95554aad
TT
10131{
10132 void **slot;
10133 int ix;
43f3e411 10134 struct compunit_symtab *cust;
95554aad
TT
10135 struct dwarf2_per_cu_data *iter;
10136
10137 slot = htab_find_slot (all_children, per_cu, INSERT);
10138 if (*slot != NULL)
10139 {
10140 /* This inclusion and its children have been processed. */
10141 return;
10142 }
10143
10144 *slot = per_cu;
10145 /* Only add a CU if it has a symbol table. */
43f3e411
DE
10146 cust = get_compunit_symtab (per_cu);
10147 if (cust != NULL)
ec94af83
DE
10148 {
10149 /* If this is a type unit only add its symbol table if we haven't
10150 seen it yet (type unit per_cu's can share symtabs). */
10151 if (per_cu->is_debug_types)
10152 {
43f3e411 10153 slot = htab_find_slot (all_type_symtabs, cust, INSERT);
ec94af83
DE
10154 if (*slot == NULL)
10155 {
43f3e411
DE
10156 *slot = cust;
10157 VEC_safe_push (compunit_symtab_ptr, *result, cust);
10158 if (cust->user == NULL)
10159 cust->user = immediate_parent;
ec94af83
DE
10160 }
10161 }
10162 else
f9125b6c 10163 {
43f3e411
DE
10164 VEC_safe_push (compunit_symtab_ptr, *result, cust);
10165 if (cust->user == NULL)
10166 cust->user = immediate_parent;
f9125b6c 10167 }
ec94af83 10168 }
95554aad
TT
10169
10170 for (ix = 0;
796a7ff8 10171 VEC_iterate (dwarf2_per_cu_ptr, per_cu->imported_symtabs, ix, iter);
95554aad 10172 ++ix)
ec94af83
DE
10173 {
10174 recursively_compute_inclusions (result, all_children,
43f3e411 10175 all_type_symtabs, iter, cust);
ec94af83 10176 }
95554aad
TT
10177}
10178
43f3e411 10179/* Compute the compunit_symtab 'includes' fields for the compunit_symtab of
95554aad
TT
10180 PER_CU. */
10181
10182static void
43f3e411 10183compute_compunit_symtab_includes (struct dwarf2_per_cu_data *per_cu)
95554aad 10184{
f4dc4d17
DE
10185 gdb_assert (! per_cu->is_debug_types);
10186
796a7ff8 10187 if (!VEC_empty (dwarf2_per_cu_ptr, per_cu->imported_symtabs))
95554aad
TT
10188 {
10189 int ix, len;
ec94af83 10190 struct dwarf2_per_cu_data *per_cu_iter;
43f3e411
DE
10191 struct compunit_symtab *compunit_symtab_iter;
10192 VEC (compunit_symtab_ptr) *result_symtabs = NULL;
ec94af83 10193 htab_t all_children, all_type_symtabs;
43f3e411 10194 struct compunit_symtab *cust = get_compunit_symtab (per_cu);
95554aad
TT
10195
10196 /* If we don't have a symtab, we can just skip this case. */
43f3e411 10197 if (cust == NULL)
95554aad
TT
10198 return;
10199
10200 all_children = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
10201 NULL, xcalloc, xfree);
ec94af83
DE
10202 all_type_symtabs = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
10203 NULL, xcalloc, xfree);
95554aad
TT
10204
10205 for (ix = 0;
796a7ff8 10206 VEC_iterate (dwarf2_per_cu_ptr, per_cu->imported_symtabs,
ec94af83 10207 ix, per_cu_iter);
95554aad 10208 ++ix)
ec94af83
DE
10209 {
10210 recursively_compute_inclusions (&result_symtabs, all_children,
f9125b6c 10211 all_type_symtabs, per_cu_iter,
43f3e411 10212 cust);
ec94af83 10213 }
95554aad 10214
ec94af83 10215 /* Now we have a transitive closure of all the included symtabs. */
43f3e411
DE
10216 len = VEC_length (compunit_symtab_ptr, result_symtabs);
10217 cust->includes
ed2dc618 10218 = XOBNEWVEC (&per_cu->dwarf2_per_objfile->objfile->objfile_obstack,
8d749320 10219 struct compunit_symtab *, len + 1);
95554aad 10220 for (ix = 0;
43f3e411
DE
10221 VEC_iterate (compunit_symtab_ptr, result_symtabs, ix,
10222 compunit_symtab_iter);
95554aad 10223 ++ix)
43f3e411
DE
10224 cust->includes[ix] = compunit_symtab_iter;
10225 cust->includes[len] = NULL;
95554aad 10226
43f3e411 10227 VEC_free (compunit_symtab_ptr, result_symtabs);
95554aad 10228 htab_delete (all_children);
ec94af83 10229 htab_delete (all_type_symtabs);
95554aad
TT
10230 }
10231}
10232
10233/* Compute the 'includes' field for the symtabs of all the CUs we just
10234 read. */
10235
10236static void
ed2dc618 10237process_cu_includes (struct dwarf2_per_objfile *dwarf2_per_objfile)
95554aad
TT
10238{
10239 int ix;
10240 struct dwarf2_per_cu_data *iter;
10241
10242 for (ix = 0;
10243 VEC_iterate (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus,
10244 ix, iter);
10245 ++ix)
f4dc4d17
DE
10246 {
10247 if (! iter->is_debug_types)
43f3e411 10248 compute_compunit_symtab_includes (iter);
f4dc4d17 10249 }
95554aad
TT
10250
10251 VEC_free (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus);
10252}
10253
9cdd5dbd 10254/* Generate full symbol information for PER_CU, whose DIEs have
10b3939b
DJ
10255 already been loaded into memory. */
10256
10257static void
95554aad
TT
10258process_full_comp_unit (struct dwarf2_per_cu_data *per_cu,
10259 enum language pretend_language)
10b3939b 10260{
10b3939b 10261 struct dwarf2_cu *cu = per_cu->cu;
ed2dc618
SM
10262 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
10263 struct objfile *objfile = dwarf2_per_objfile->objfile;
3e29f34a 10264 struct gdbarch *gdbarch = get_objfile_arch (objfile);
10b3939b 10265 CORE_ADDR lowpc, highpc;
43f3e411 10266 struct compunit_symtab *cust;
10b3939b 10267 CORE_ADDR baseaddr;
4359dff1 10268 struct block *static_block;
3e29f34a 10269 CORE_ADDR addr;
10b3939b
DJ
10270
10271 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
10272
10b3939b 10273 buildsym_init ();
33c7c59d 10274 scoped_free_pendings free_pending;
c89b44cd
TT
10275
10276 /* Clear the list here in case something was left over. */
10277 cu->method_list.clear ();
10b3939b
DJ
10278
10279 cu->list_in_scope = &file_symbols;
c906108c 10280
95554aad
TT
10281 cu->language = pretend_language;
10282 cu->language_defn = language_def (cu->language);
10283
c906108c 10284 /* Do line number decoding in read_file_scope () */
10b3939b 10285 process_die (cu->dies, cu);
c906108c 10286
a766d390
DE
10287 /* For now fudge the Go package. */
10288 if (cu->language == language_go)
10289 fixup_go_packaging (cu);
10290
3da10d80
KS
10291 /* Now that we have processed all the DIEs in the CU, all the types
10292 should be complete, and it should now be safe to compute all of the
10293 physnames. */
10294 compute_delayed_physnames (cu);
3da10d80 10295
c9317f21
TT
10296 if (cu->language == language_rust)
10297 rust_union_quirks (cu);
10298
fae299cd
DC
10299 /* Some compilers don't define a DW_AT_high_pc attribute for the
10300 compilation unit. If the DW_AT_high_pc is missing, synthesize
10301 it, by scanning the DIE's below the compilation unit. */
10b3939b 10302 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
c906108c 10303
3e29f34a
MR
10304 addr = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
10305 static_block = end_symtab_get_static_block (addr, 0, 1);
4359dff1
JK
10306
10307 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
10308 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
10309 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
10310 addrmap to help ensure it has an accurate map of pc values belonging to
10311 this comp unit. */
10312 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
10313
43f3e411
DE
10314 cust = end_symtab_from_static_block (static_block,
10315 SECT_OFF_TEXT (objfile), 0);
c906108c 10316
43f3e411 10317 if (cust != NULL)
c906108c 10318 {
df15bd07 10319 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
4632c0d0 10320
8be455d7
JK
10321 /* Set symtab language to language from DW_AT_language. If the
10322 compilation is from a C file generated by language preprocessors, do
10323 not set the language if it was already deduced by start_subfile. */
43f3e411 10324 if (!(cu->language == language_c
40e3ad0e 10325 && COMPUNIT_FILETABS (cust)->language != language_unknown))
43f3e411 10326 COMPUNIT_FILETABS (cust)->language = cu->language;
8be455d7
JK
10327
10328 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
10329 produce DW_AT_location with location lists but it can be possibly
ab260dad
JK
10330 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
10331 there were bugs in prologue debug info, fixed later in GCC-4.5
10332 by "unwind info for epilogues" patch (which is not directly related).
8be455d7
JK
10333
10334 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
10335 needed, it would be wrong due to missing DW_AT_producer there.
10336
10337 Still one can confuse GDB by using non-standard GCC compilation
10338 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
10339 */
ab260dad 10340 if (cu->has_loclist && gcc_4_minor >= 5)
43f3e411 10341 cust->locations_valid = 1;
e0d00bc7
JK
10342
10343 if (gcc_4_minor >= 5)
43f3e411 10344 cust->epilogue_unwind_valid = 1;
96408a79 10345
43f3e411 10346 cust->call_site_htab = cu->call_site_htab;
c906108c 10347 }
9291a0cd
TT
10348
10349 if (dwarf2_per_objfile->using_index)
43f3e411 10350 per_cu->v.quick->compunit_symtab = cust;
9291a0cd
TT
10351 else
10352 {
10353 struct partial_symtab *pst = per_cu->v.psymtab;
43f3e411 10354 pst->compunit_symtab = cust;
9291a0cd
TT
10355 pst->readin = 1;
10356 }
c906108c 10357
95554aad
TT
10358 /* Push it for inclusion processing later. */
10359 VEC_safe_push (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus, per_cu);
f4dc4d17 10360}
45cfd468 10361
f4dc4d17
DE
10362/* Generate full symbol information for type unit PER_CU, whose DIEs have
10363 already been loaded into memory. */
10364
10365static void
10366process_full_type_unit (struct dwarf2_per_cu_data *per_cu,
10367 enum language pretend_language)
10368{
10369 struct dwarf2_cu *cu = per_cu->cu;
ed2dc618
SM
10370 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
10371 struct objfile *objfile = dwarf2_per_objfile->objfile;
43f3e411 10372 struct compunit_symtab *cust;
0186c6a7
DE
10373 struct signatured_type *sig_type;
10374
10375 gdb_assert (per_cu->is_debug_types);
10376 sig_type = (struct signatured_type *) per_cu;
f4dc4d17
DE
10377
10378 buildsym_init ();
33c7c59d 10379 scoped_free_pendings free_pending;
c89b44cd
TT
10380
10381 /* Clear the list here in case something was left over. */
10382 cu->method_list.clear ();
f4dc4d17
DE
10383
10384 cu->list_in_scope = &file_symbols;
10385
10386 cu->language = pretend_language;
10387 cu->language_defn = language_def (cu->language);
10388
10389 /* The symbol tables are set up in read_type_unit_scope. */
10390 process_die (cu->dies, cu);
10391
10392 /* For now fudge the Go package. */
10393 if (cu->language == language_go)
10394 fixup_go_packaging (cu);
10395
10396 /* Now that we have processed all the DIEs in the CU, all the types
10397 should be complete, and it should now be safe to compute all of the
10398 physnames. */
10399 compute_delayed_physnames (cu);
f4dc4d17 10400
c9317f21
TT
10401 if (cu->language == language_rust)
10402 rust_union_quirks (cu);
10403
f4dc4d17
DE
10404 /* TUs share symbol tables.
10405 If this is the first TU to use this symtab, complete the construction
094b34ac
DE
10406 of it with end_expandable_symtab. Otherwise, complete the addition of
10407 this TU's symbols to the existing symtab. */
43f3e411 10408 if (sig_type->type_unit_group->compunit_symtab == NULL)
45cfd468 10409 {
43f3e411
DE
10410 cust = end_expandable_symtab (0, SECT_OFF_TEXT (objfile));
10411 sig_type->type_unit_group->compunit_symtab = cust;
f4dc4d17 10412
43f3e411 10413 if (cust != NULL)
f4dc4d17
DE
10414 {
10415 /* Set symtab language to language from DW_AT_language. If the
10416 compilation is from a C file generated by language preprocessors,
10417 do not set the language if it was already deduced by
10418 start_subfile. */
43f3e411
DE
10419 if (!(cu->language == language_c
10420 && COMPUNIT_FILETABS (cust)->language != language_c))
10421 COMPUNIT_FILETABS (cust)->language = cu->language;
f4dc4d17
DE
10422 }
10423 }
10424 else
10425 {
0ab9ce85 10426 augment_type_symtab ();
43f3e411 10427 cust = sig_type->type_unit_group->compunit_symtab;
f4dc4d17
DE
10428 }
10429
10430 if (dwarf2_per_objfile->using_index)
43f3e411 10431 per_cu->v.quick->compunit_symtab = cust;
f4dc4d17
DE
10432 else
10433 {
10434 struct partial_symtab *pst = per_cu->v.psymtab;
43f3e411 10435 pst->compunit_symtab = cust;
f4dc4d17 10436 pst->readin = 1;
45cfd468 10437 }
c906108c
SS
10438}
10439
95554aad
TT
10440/* Process an imported unit DIE. */
10441
10442static void
10443process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
10444{
10445 struct attribute *attr;
10446
f4dc4d17
DE
10447 /* For now we don't handle imported units in type units. */
10448 if (cu->per_cu->is_debug_types)
10449 {
10450 error (_("Dwarf Error: DW_TAG_imported_unit is not"
10451 " supported in type units [in module %s]"),
518817b3 10452 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
f4dc4d17
DE
10453 }
10454
95554aad
TT
10455 attr = dwarf2_attr (die, DW_AT_import, cu);
10456 if (attr != NULL)
10457 {
9c541725
PA
10458 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
10459 bool is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
10460 dwarf2_per_cu_data *per_cu
e3b94546 10461 = dwarf2_find_containing_comp_unit (sect_off, is_dwz,
518817b3 10462 cu->per_cu->dwarf2_per_objfile);
95554aad 10463
69d751e3 10464 /* If necessary, add it to the queue and load its DIEs. */
95554aad 10465 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
58f0c718 10466 load_full_comp_unit (per_cu, false, cu->language);
95554aad 10467
796a7ff8 10468 VEC_safe_push (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs,
95554aad
TT
10469 per_cu);
10470 }
10471}
10472
4c8aa72d
PA
10473/* RAII object that represents a process_die scope: i.e.,
10474 starts/finishes processing a DIE. */
10475class process_die_scope
adde2bff 10476{
4c8aa72d
PA
10477public:
10478 process_die_scope (die_info *die, dwarf2_cu *cu)
10479 : m_die (die), m_cu (cu)
10480 {
10481 /* We should only be processing DIEs not already in process. */
10482 gdb_assert (!m_die->in_process);
10483 m_die->in_process = true;
10484 }
8c3cb9fa 10485
4c8aa72d
PA
10486 ~process_die_scope ()
10487 {
10488 m_die->in_process = false;
10489
10490 /* If we're done processing the DIE for the CU that owns the line
10491 header, we don't need the line header anymore. */
10492 if (m_cu->line_header_die_owner == m_die)
10493 {
10494 delete m_cu->line_header;
10495 m_cu->line_header = NULL;
10496 m_cu->line_header_die_owner = NULL;
10497 }
10498 }
10499
10500private:
10501 die_info *m_die;
10502 dwarf2_cu *m_cu;
10503};
adde2bff 10504
c906108c
SS
10505/* Process a die and its children. */
10506
10507static void
e7c27a73 10508process_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 10509{
4c8aa72d 10510 process_die_scope scope (die, cu);
adde2bff 10511
c906108c
SS
10512 switch (die->tag)
10513 {
10514 case DW_TAG_padding:
10515 break;
10516 case DW_TAG_compile_unit:
95554aad 10517 case DW_TAG_partial_unit:
e7c27a73 10518 read_file_scope (die, cu);
c906108c 10519 break;
348e048f
DE
10520 case DW_TAG_type_unit:
10521 read_type_unit_scope (die, cu);
10522 break;
c906108c 10523 case DW_TAG_subprogram:
c906108c 10524 case DW_TAG_inlined_subroutine:
edb3359d 10525 read_func_scope (die, cu);
c906108c
SS
10526 break;
10527 case DW_TAG_lexical_block:
14898363
L
10528 case DW_TAG_try_block:
10529 case DW_TAG_catch_block:
e7c27a73 10530 read_lexical_block_scope (die, cu);
c906108c 10531 break;
216f72a1 10532 case DW_TAG_call_site:
96408a79
SA
10533 case DW_TAG_GNU_call_site:
10534 read_call_site_scope (die, cu);
10535 break;
c906108c 10536 case DW_TAG_class_type:
680b30c7 10537 case DW_TAG_interface_type:
c906108c
SS
10538 case DW_TAG_structure_type:
10539 case DW_TAG_union_type:
134d01f1 10540 process_structure_scope (die, cu);
c906108c
SS
10541 break;
10542 case DW_TAG_enumeration_type:
134d01f1 10543 process_enumeration_scope (die, cu);
c906108c 10544 break;
134d01f1 10545
f792889a
DJ
10546 /* These dies have a type, but processing them does not create
10547 a symbol or recurse to process the children. Therefore we can
10548 read them on-demand through read_type_die. */
c906108c 10549 case DW_TAG_subroutine_type:
72019c9c 10550 case DW_TAG_set_type:
c906108c 10551 case DW_TAG_array_type:
c906108c 10552 case DW_TAG_pointer_type:
c906108c 10553 case DW_TAG_ptr_to_member_type:
c906108c 10554 case DW_TAG_reference_type:
4297a3f0 10555 case DW_TAG_rvalue_reference_type:
c906108c 10556 case DW_TAG_string_type:
c906108c 10557 break;
134d01f1 10558
c906108c 10559 case DW_TAG_base_type:
a02abb62 10560 case DW_TAG_subrange_type:
cb249c71 10561 case DW_TAG_typedef:
134d01f1
DJ
10562 /* Add a typedef symbol for the type definition, if it has a
10563 DW_AT_name. */
f792889a 10564 new_symbol (die, read_type_die (die, cu), cu);
a02abb62 10565 break;
c906108c 10566 case DW_TAG_common_block:
e7c27a73 10567 read_common_block (die, cu);
c906108c
SS
10568 break;
10569 case DW_TAG_common_inclusion:
10570 break;
d9fa45fe 10571 case DW_TAG_namespace:
4d4ec4e5 10572 cu->processing_has_namespace_info = 1;
e7c27a73 10573 read_namespace (die, cu);
d9fa45fe 10574 break;
5d7cb8df 10575 case DW_TAG_module:
4d4ec4e5 10576 cu->processing_has_namespace_info = 1;
5d7cb8df
JK
10577 read_module (die, cu);
10578 break;
d9fa45fe 10579 case DW_TAG_imported_declaration:
74921315
KS
10580 cu->processing_has_namespace_info = 1;
10581 if (read_namespace_alias (die, cu))
10582 break;
86a73007
TT
10583 /* The declaration is not a global namespace alias. */
10584 /* Fall through. */
d9fa45fe 10585 case DW_TAG_imported_module:
4d4ec4e5 10586 cu->processing_has_namespace_info = 1;
27aa8d6a
SW
10587 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
10588 || cu->language != language_fortran))
10589 complaint (&symfile_complaints, _("Tag '%s' has unexpected children"),
10590 dwarf_tag_name (die->tag));
10591 read_import_statement (die, cu);
d9fa45fe 10592 break;
95554aad
TT
10593
10594 case DW_TAG_imported_unit:
10595 process_imported_unit_die (die, cu);
10596 break;
10597
71a3c369
TT
10598 case DW_TAG_variable:
10599 read_variable (die, cu);
10600 break;
10601
c906108c 10602 default:
e7c27a73 10603 new_symbol (die, NULL, cu);
c906108c
SS
10604 break;
10605 }
10606}
ca69b9e6
DE
10607\f
10608/* DWARF name computation. */
c906108c 10609
94af9270
KS
10610/* A helper function for dwarf2_compute_name which determines whether DIE
10611 needs to have the name of the scope prepended to the name listed in the
10612 die. */
10613
10614static int
10615die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
10616{
1c809c68
TT
10617 struct attribute *attr;
10618
94af9270
KS
10619 switch (die->tag)
10620 {
10621 case DW_TAG_namespace:
10622 case DW_TAG_typedef:
10623 case DW_TAG_class_type:
10624 case DW_TAG_interface_type:
10625 case DW_TAG_structure_type:
10626 case DW_TAG_union_type:
10627 case DW_TAG_enumeration_type:
10628 case DW_TAG_enumerator:
10629 case DW_TAG_subprogram:
08a76f8a 10630 case DW_TAG_inlined_subroutine:
94af9270 10631 case DW_TAG_member:
74921315 10632 case DW_TAG_imported_declaration:
94af9270
KS
10633 return 1;
10634
10635 case DW_TAG_variable:
c2b0a229 10636 case DW_TAG_constant:
94af9270
KS
10637 /* We only need to prefix "globally" visible variables. These include
10638 any variable marked with DW_AT_external or any variable that
10639 lives in a namespace. [Variables in anonymous namespaces
10640 require prefixing, but they are not DW_AT_external.] */
10641
10642 if (dwarf2_attr (die, DW_AT_specification, cu))
10643 {
10644 struct dwarf2_cu *spec_cu = cu;
9a619af0 10645
94af9270
KS
10646 return die_needs_namespace (die_specification (die, &spec_cu),
10647 spec_cu);
10648 }
10649
1c809c68 10650 attr = dwarf2_attr (die, DW_AT_external, cu);
f55ee35c
JK
10651 if (attr == NULL && die->parent->tag != DW_TAG_namespace
10652 && die->parent->tag != DW_TAG_module)
1c809c68
TT
10653 return 0;
10654 /* A variable in a lexical block of some kind does not need a
10655 namespace, even though in C++ such variables may be external
10656 and have a mangled name. */
10657 if (die->parent->tag == DW_TAG_lexical_block
10658 || die->parent->tag == DW_TAG_try_block
1054b214
TT
10659 || die->parent->tag == DW_TAG_catch_block
10660 || die->parent->tag == DW_TAG_subprogram)
1c809c68
TT
10661 return 0;
10662 return 1;
94af9270
KS
10663
10664 default:
10665 return 0;
10666 }
10667}
10668
73b9be8b
KS
10669/* Return the DIE's linkage name attribute, either DW_AT_linkage_name
10670 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10671 defined for the given DIE. */
10672
10673static struct attribute *
10674dw2_linkage_name_attr (struct die_info *die, struct dwarf2_cu *cu)
10675{
10676 struct attribute *attr;
10677
10678 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
10679 if (attr == NULL)
10680 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
10681
10682 return attr;
10683}
10684
10685/* Return the DIE's linkage name as a string, either DW_AT_linkage_name
10686 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10687 defined for the given DIE. */
10688
10689static const char *
10690dw2_linkage_name (struct die_info *die, struct dwarf2_cu *cu)
10691{
10692 const char *linkage_name;
10693
10694 linkage_name = dwarf2_string_attr (die, DW_AT_linkage_name, cu);
10695 if (linkage_name == NULL)
10696 linkage_name = dwarf2_string_attr (die, DW_AT_MIPS_linkage_name, cu);
10697
10698 return linkage_name;
10699}
10700
94af9270 10701/* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
a766d390 10702 compute the physname for the object, which include a method's:
9c37b5ae 10703 - formal parameters (C++),
a766d390 10704 - receiver type (Go),
a766d390
DE
10705
10706 The term "physname" is a bit confusing.
10707 For C++, for example, it is the demangled name.
10708 For Go, for example, it's the mangled name.
94af9270 10709
af6b7be1
JB
10710 For Ada, return the DIE's linkage name rather than the fully qualified
10711 name. PHYSNAME is ignored..
10712
94af9270
KS
10713 The result is allocated on the objfile_obstack and canonicalized. */
10714
10715static const char *
15d034d0
TT
10716dwarf2_compute_name (const char *name,
10717 struct die_info *die, struct dwarf2_cu *cu,
94af9270
KS
10718 int physname)
10719{
518817b3 10720 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
bb5ed363 10721
94af9270
KS
10722 if (name == NULL)
10723 name = dwarf2_name (die, cu);
10724
2ee7123e
DE
10725 /* For Fortran GDB prefers DW_AT_*linkage_name for the physname if present
10726 but otherwise compute it by typename_concat inside GDB.
10727 FIXME: Actually this is not really true, or at least not always true.
10728 It's all very confusing. SYMBOL_SET_NAMES doesn't try to demangle
5e2db402 10729 Fortran names because there is no mangling standard. So new_symbol
2ee7123e
DE
10730 will set the demangled name to the result of dwarf2_full_name, and it is
10731 the demangled name that GDB uses if it exists. */
f55ee35c
JK
10732 if (cu->language == language_ada
10733 || (cu->language == language_fortran && physname))
10734 {
10735 /* For Ada unit, we prefer the linkage name over the name, as
10736 the former contains the exported name, which the user expects
10737 to be able to reference. Ideally, we want the user to be able
10738 to reference this entity using either natural or linkage name,
10739 but we haven't started looking at this enhancement yet. */
73b9be8b 10740 const char *linkage_name = dw2_linkage_name (die, cu);
f55ee35c 10741
2ee7123e
DE
10742 if (linkage_name != NULL)
10743 return linkage_name;
f55ee35c
JK
10744 }
10745
94af9270
KS
10746 /* These are the only languages we know how to qualify names in. */
10747 if (name != NULL
9c37b5ae 10748 && (cu->language == language_cplus
c44af4eb
TT
10749 || cu->language == language_fortran || cu->language == language_d
10750 || cu->language == language_rust))
94af9270
KS
10751 {
10752 if (die_needs_namespace (die, cu))
10753 {
0d5cff50 10754 const char *prefix;
34a68019 10755 const char *canonical_name = NULL;
94af9270 10756
d7e74731
PA
10757 string_file buf;
10758
94af9270 10759 prefix = determine_prefix (die, cu);
94af9270
KS
10760 if (*prefix != '\0')
10761 {
f55ee35c
JK
10762 char *prefixed_name = typename_concat (NULL, prefix, name,
10763 physname, cu);
9a619af0 10764
d7e74731 10765 buf.puts (prefixed_name);
94af9270
KS
10766 xfree (prefixed_name);
10767 }
10768 else
d7e74731 10769 buf.puts (name);
94af9270 10770
98bfdba5
PA
10771 /* Template parameters may be specified in the DIE's DW_AT_name, or
10772 as children with DW_TAG_template_type_param or
10773 DW_TAG_value_type_param. If the latter, add them to the name
10774 here. If the name already has template parameters, then
10775 skip this step; some versions of GCC emit both, and
10776 it is more efficient to use the pre-computed name.
10777
10778 Something to keep in mind about this process: it is very
10779 unlikely, or in some cases downright impossible, to produce
10780 something that will match the mangled name of a function.
10781 If the definition of the function has the same debug info,
10782 we should be able to match up with it anyway. But fallbacks
10783 using the minimal symbol, for instance to find a method
10784 implemented in a stripped copy of libstdc++, will not work.
10785 If we do not have debug info for the definition, we will have to
10786 match them up some other way.
10787
10788 When we do name matching there is a related problem with function
10789 templates; two instantiated function templates are allowed to
10790 differ only by their return types, which we do not add here. */
10791
10792 if (cu->language == language_cplus && strchr (name, '<') == NULL)
10793 {
10794 struct attribute *attr;
10795 struct die_info *child;
10796 int first = 1;
10797
10798 die->building_fullname = 1;
10799
10800 for (child = die->child; child != NULL; child = child->sibling)
10801 {
10802 struct type *type;
12df843f 10803 LONGEST value;
d521ce57 10804 const gdb_byte *bytes;
98bfdba5
PA
10805 struct dwarf2_locexpr_baton *baton;
10806 struct value *v;
10807
10808 if (child->tag != DW_TAG_template_type_param
10809 && child->tag != DW_TAG_template_value_param)
10810 continue;
10811
10812 if (first)
10813 {
d7e74731 10814 buf.puts ("<");
98bfdba5
PA
10815 first = 0;
10816 }
10817 else
d7e74731 10818 buf.puts (", ");
98bfdba5
PA
10819
10820 attr = dwarf2_attr (child, DW_AT_type, cu);
10821 if (attr == NULL)
10822 {
10823 complaint (&symfile_complaints,
10824 _("template parameter missing DW_AT_type"));
d7e74731 10825 buf.puts ("UNKNOWN_TYPE");
98bfdba5
PA
10826 continue;
10827 }
10828 type = die_type (child, cu);
10829
10830 if (child->tag == DW_TAG_template_type_param)
10831 {
d7e74731 10832 c_print_type (type, "", &buf, -1, 0, &type_print_raw_options);
98bfdba5
PA
10833 continue;
10834 }
10835
10836 attr = dwarf2_attr (child, DW_AT_const_value, cu);
10837 if (attr == NULL)
10838 {
10839 complaint (&symfile_complaints,
3e43a32a
MS
10840 _("template parameter missing "
10841 "DW_AT_const_value"));
d7e74731 10842 buf.puts ("UNKNOWN_VALUE");
98bfdba5
PA
10843 continue;
10844 }
10845
10846 dwarf2_const_value_attr (attr, type, name,
10847 &cu->comp_unit_obstack, cu,
10848 &value, &bytes, &baton);
10849
10850 if (TYPE_NOSIGN (type))
10851 /* GDB prints characters as NUMBER 'CHAR'. If that's
10852 changed, this can use value_print instead. */
d7e74731 10853 c_printchar (value, type, &buf);
98bfdba5
PA
10854 else
10855 {
10856 struct value_print_options opts;
10857
10858 if (baton != NULL)
10859 v = dwarf2_evaluate_loc_desc (type, NULL,
10860 baton->data,
10861 baton->size,
10862 baton->per_cu);
10863 else if (bytes != NULL)
10864 {
10865 v = allocate_value (type);
10866 memcpy (value_contents_writeable (v), bytes,
10867 TYPE_LENGTH (type));
10868 }
10869 else
10870 v = value_from_longest (type, value);
10871
3e43a32a
MS
10872 /* Specify decimal so that we do not depend on
10873 the radix. */
98bfdba5
PA
10874 get_formatted_print_options (&opts, 'd');
10875 opts.raw = 1;
d7e74731 10876 value_print (v, &buf, &opts);
98bfdba5 10877 release_value (v);
98bfdba5
PA
10878 }
10879 }
10880
10881 die->building_fullname = 0;
10882
10883 if (!first)
10884 {
10885 /* Close the argument list, with a space if necessary
10886 (nested templates). */
d7e74731
PA
10887 if (!buf.empty () && buf.string ().back () == '>')
10888 buf.puts (" >");
98bfdba5 10889 else
d7e74731 10890 buf.puts (">");
98bfdba5
PA
10891 }
10892 }
10893
9c37b5ae 10894 /* For C++ methods, append formal parameter type
94af9270 10895 information, if PHYSNAME. */
6e70227d 10896
94af9270 10897 if (physname && die->tag == DW_TAG_subprogram
9c37b5ae 10898 && cu->language == language_cplus)
94af9270
KS
10899 {
10900 struct type *type = read_type_die (die, cu);
10901
d7e74731 10902 c_type_print_args (type, &buf, 1, cu->language,
79d43c61 10903 &type_print_raw_options);
94af9270 10904
9c37b5ae 10905 if (cu->language == language_cplus)
94af9270 10906 {
60430eff
DJ
10907 /* Assume that an artificial first parameter is
10908 "this", but do not crash if it is not. RealView
10909 marks unnamed (and thus unused) parameters as
10910 artificial; there is no way to differentiate
10911 the two cases. */
94af9270
KS
10912 if (TYPE_NFIELDS (type) > 0
10913 && TYPE_FIELD_ARTIFICIAL (type, 0)
60430eff 10914 && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
3e43a32a
MS
10915 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
10916 0))))
d7e74731 10917 buf.puts (" const");
94af9270
KS
10918 }
10919 }
10920
d7e74731 10921 const std::string &intermediate_name = buf.string ();
94af9270
KS
10922
10923 if (cu->language == language_cplus)
34a68019 10924 canonical_name
322a8516 10925 = dwarf2_canonicalize_name (intermediate_name.c_str (), cu,
34a68019
TT
10926 &objfile->per_bfd->storage_obstack);
10927
10928 /* If we only computed INTERMEDIATE_NAME, or if
10929 INTERMEDIATE_NAME is already canonical, then we need to
10930 copy it to the appropriate obstack. */
322a8516 10931 if (canonical_name == NULL || canonical_name == intermediate_name.c_str ())
224c3ddb
SM
10932 name = ((const char *)
10933 obstack_copy0 (&objfile->per_bfd->storage_obstack,
322a8516
PA
10934 intermediate_name.c_str (),
10935 intermediate_name.length ()));
34a68019
TT
10936 else
10937 name = canonical_name;
94af9270
KS
10938 }
10939 }
10940
10941 return name;
10942}
10943
0114d602
DJ
10944/* Return the fully qualified name of DIE, based on its DW_AT_name.
10945 If scope qualifiers are appropriate they will be added. The result
34a68019 10946 will be allocated on the storage_obstack, or NULL if the DIE does
94af9270
KS
10947 not have a name. NAME may either be from a previous call to
10948 dwarf2_name or NULL.
10949
9c37b5ae 10950 The output string will be canonicalized (if C++). */
0114d602
DJ
10951
10952static const char *
15d034d0 10953dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
0114d602 10954{
94af9270
KS
10955 return dwarf2_compute_name (name, die, cu, 0);
10956}
0114d602 10957
94af9270
KS
10958/* Construct a physname for the given DIE in CU. NAME may either be
10959 from a previous call to dwarf2_name or NULL. The result will be
10960 allocated on the objfile_objstack or NULL if the DIE does not have a
10961 name.
0114d602 10962
9c37b5ae 10963 The output string will be canonicalized (if C++). */
0114d602 10964
94af9270 10965static const char *
15d034d0 10966dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
94af9270 10967{
518817b3 10968 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
900e11f9 10969 const char *retval, *mangled = NULL, *canon = NULL;
900e11f9
JK
10970 int need_copy = 1;
10971
10972 /* In this case dwarf2_compute_name is just a shortcut not building anything
10973 on its own. */
10974 if (!die_needs_namespace (die, cu))
10975 return dwarf2_compute_name (name, die, cu, 1);
10976
73b9be8b 10977 mangled = dw2_linkage_name (die, cu);
900e11f9 10978
e98c9e7c
TT
10979 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
10980 See https://github.com/rust-lang/rust/issues/32925. */
10981 if (cu->language == language_rust && mangled != NULL
10982 && strchr (mangled, '{') != NULL)
10983 mangled = NULL;
10984
900e11f9
JK
10985 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
10986 has computed. */
791afaa2 10987 gdb::unique_xmalloc_ptr<char> demangled;
7d45c7c3 10988 if (mangled != NULL)
900e11f9 10989 {
900e11f9 10990
59cc4834
JB
10991 if (language_def (cu->language)->la_store_sym_names_in_linkage_form_p)
10992 {
10993 /* Do nothing (do not demangle the symbol name). */
10994 }
10995 else if (cu->language == language_go)
a766d390 10996 {
5e2db402
TT
10997 /* This is a lie, but we already lie to the caller new_symbol.
10998 new_symbol assumes we return the mangled name.
a766d390 10999 This just undoes that lie until things are cleaned up. */
a766d390
DE
11000 }
11001 else
11002 {
0eb876f5
JB
11003 /* Use DMGL_RET_DROP for C++ template functions to suppress
11004 their return type. It is easier for GDB users to search
11005 for such functions as `name(params)' than `long name(params)'.
11006 In such case the minimal symbol names do not match the full
11007 symbol names but for template functions there is never a need
11008 to look up their definition from their declaration so
11009 the only disadvantage remains the minimal symbol variant
11010 `long name(params)' does not have the proper inferior type. */
791afaa2
TT
11011 demangled.reset (gdb_demangle (mangled,
11012 (DMGL_PARAMS | DMGL_ANSI
11013 | DMGL_RET_DROP)));
a766d390 11014 }
900e11f9 11015 if (demangled)
791afaa2 11016 canon = demangled.get ();
900e11f9
JK
11017 else
11018 {
11019 canon = mangled;
11020 need_copy = 0;
11021 }
11022 }
11023
11024 if (canon == NULL || check_physname)
11025 {
11026 const char *physname = dwarf2_compute_name (name, die, cu, 1);
11027
11028 if (canon != NULL && strcmp (physname, canon) != 0)
11029 {
11030 /* It may not mean a bug in GDB. The compiler could also
11031 compute DW_AT_linkage_name incorrectly. But in such case
11032 GDB would need to be bug-to-bug compatible. */
11033
11034 complaint (&symfile_complaints,
11035 _("Computed physname <%s> does not match demangled <%s> "
9d8780f0
SM
11036 "(from linkage <%s>) - DIE at %s [in module %s]"),
11037 physname, canon, mangled, sect_offset_str (die->sect_off),
4262abfb 11038 objfile_name (objfile));
900e11f9
JK
11039
11040 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
11041 is available here - over computed PHYSNAME. It is safer
11042 against both buggy GDB and buggy compilers. */
11043
11044 retval = canon;
11045 }
11046 else
11047 {
11048 retval = physname;
11049 need_copy = 0;
11050 }
11051 }
11052 else
11053 retval = canon;
11054
11055 if (need_copy)
224c3ddb
SM
11056 retval = ((const char *)
11057 obstack_copy0 (&objfile->per_bfd->storage_obstack,
11058 retval, strlen (retval)));
900e11f9 11059
900e11f9 11060 return retval;
0114d602
DJ
11061}
11062
74921315
KS
11063/* Inspect DIE in CU for a namespace alias. If one exists, record
11064 a new symbol for it.
11065
11066 Returns 1 if a namespace alias was recorded, 0 otherwise. */
11067
11068static int
11069read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu)
11070{
11071 struct attribute *attr;
11072
11073 /* If the die does not have a name, this is not a namespace
11074 alias. */
11075 attr = dwarf2_attr (die, DW_AT_name, cu);
11076 if (attr != NULL)
11077 {
11078 int num;
11079 struct die_info *d = die;
11080 struct dwarf2_cu *imported_cu = cu;
11081
11082 /* If the compiler has nested DW_AT_imported_declaration DIEs,
11083 keep inspecting DIEs until we hit the underlying import. */
11084#define MAX_NESTED_IMPORTED_DECLARATIONS 100
11085 for (num = 0; num < MAX_NESTED_IMPORTED_DECLARATIONS; ++num)
11086 {
11087 attr = dwarf2_attr (d, DW_AT_import, cu);
11088 if (attr == NULL)
11089 break;
11090
11091 d = follow_die_ref (d, attr, &imported_cu);
11092 if (d->tag != DW_TAG_imported_declaration)
11093 break;
11094 }
11095
11096 if (num == MAX_NESTED_IMPORTED_DECLARATIONS)
11097 {
11098 complaint (&symfile_complaints,
9d8780f0
SM
11099 _("DIE at %s has too many recursively imported "
11100 "declarations"), sect_offset_str (d->sect_off));
74921315
KS
11101 return 0;
11102 }
11103
11104 if (attr != NULL)
11105 {
11106 struct type *type;
9c541725 11107 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
74921315 11108
9c541725 11109 type = get_die_type_at_offset (sect_off, cu->per_cu);
74921315
KS
11110 if (type != NULL && TYPE_CODE (type) == TYPE_CODE_NAMESPACE)
11111 {
11112 /* This declaration is a global namespace alias. Add
11113 a symbol for it whose type is the aliased namespace. */
11114 new_symbol (die, type, cu);
11115 return 1;
11116 }
11117 }
11118 }
11119
11120 return 0;
11121}
11122
22cee43f
PMR
11123/* Return the using directives repository (global or local?) to use in the
11124 current context for LANGUAGE.
11125
11126 For Ada, imported declarations can materialize renamings, which *may* be
11127 global. However it is impossible (for now?) in DWARF to distinguish
11128 "external" imported declarations and "static" ones. As all imported
11129 declarations seem to be static in all other languages, make them all CU-wide
11130 global only in Ada. */
11131
11132static struct using_direct **
11133using_directives (enum language language)
11134{
11135 if (language == language_ada && context_stack_depth == 0)
11136 return &global_using_directives;
11137 else
11138 return &local_using_directives;
11139}
11140
27aa8d6a
SW
11141/* Read the import statement specified by the given die and record it. */
11142
11143static void
11144read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
11145{
518817b3 11146 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
27aa8d6a 11147 struct attribute *import_attr;
32019081 11148 struct die_info *imported_die, *child_die;
de4affc9 11149 struct dwarf2_cu *imported_cu;
27aa8d6a 11150 const char *imported_name;
794684b6 11151 const char *imported_name_prefix;
13387711
SW
11152 const char *canonical_name;
11153 const char *import_alias;
11154 const char *imported_declaration = NULL;
794684b6 11155 const char *import_prefix;
eb1e02fd 11156 std::vector<const char *> excludes;
13387711 11157
27aa8d6a
SW
11158 import_attr = dwarf2_attr (die, DW_AT_import, cu);
11159 if (import_attr == NULL)
11160 {
11161 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
11162 dwarf_tag_name (die->tag));
11163 return;
11164 }
11165
de4affc9
CC
11166 imported_cu = cu;
11167 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
11168 imported_name = dwarf2_name (imported_die, imported_cu);
27aa8d6a
SW
11169 if (imported_name == NULL)
11170 {
11171 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
11172
11173 The import in the following code:
11174 namespace A
11175 {
11176 typedef int B;
11177 }
11178
11179 int main ()
11180 {
11181 using A::B;
11182 B b;
11183 return b;
11184 }
11185
11186 ...
11187 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
11188 <52> DW_AT_decl_file : 1
11189 <53> DW_AT_decl_line : 6
11190 <54> DW_AT_import : <0x75>
11191 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
11192 <59> DW_AT_name : B
11193 <5b> DW_AT_decl_file : 1
11194 <5c> DW_AT_decl_line : 2
11195 <5d> DW_AT_type : <0x6e>
11196 ...
11197 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
11198 <76> DW_AT_byte_size : 4
11199 <77> DW_AT_encoding : 5 (signed)
11200
11201 imports the wrong die ( 0x75 instead of 0x58 ).
11202 This case will be ignored until the gcc bug is fixed. */
11203 return;
11204 }
11205
82856980
SW
11206 /* Figure out the local name after import. */
11207 import_alias = dwarf2_name (die, cu);
27aa8d6a 11208
794684b6
SW
11209 /* Figure out where the statement is being imported to. */
11210 import_prefix = determine_prefix (die, cu);
11211
11212 /* Figure out what the scope of the imported die is and prepend it
11213 to the name of the imported die. */
de4affc9 11214 imported_name_prefix = determine_prefix (imported_die, imported_cu);
794684b6 11215
f55ee35c
JK
11216 if (imported_die->tag != DW_TAG_namespace
11217 && imported_die->tag != DW_TAG_module)
794684b6 11218 {
13387711
SW
11219 imported_declaration = imported_name;
11220 canonical_name = imported_name_prefix;
794684b6 11221 }
13387711 11222 else if (strlen (imported_name_prefix) > 0)
12aaed36 11223 canonical_name = obconcat (&objfile->objfile_obstack,
45280282
IB
11224 imported_name_prefix,
11225 (cu->language == language_d ? "." : "::"),
11226 imported_name, (char *) NULL);
13387711
SW
11227 else
11228 canonical_name = imported_name;
794684b6 11229
32019081
JK
11230 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
11231 for (child_die = die->child; child_die && child_die->tag;
11232 child_die = sibling_die (child_die))
11233 {
11234 /* DWARF-4: A Fortran use statement with a “rename list” may be
11235 represented by an imported module entry with an import attribute
11236 referring to the module and owned entries corresponding to those
11237 entities that are renamed as part of being imported. */
11238
11239 if (child_die->tag != DW_TAG_imported_declaration)
11240 {
11241 complaint (&symfile_complaints,
11242 _("child DW_TAG_imported_declaration expected "
9d8780f0
SM
11243 "- DIE at %s [in module %s]"),
11244 sect_offset_str (child_die->sect_off),
11245 objfile_name (objfile));
32019081
JK
11246 continue;
11247 }
11248
11249 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
11250 if (import_attr == NULL)
11251 {
11252 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
11253 dwarf_tag_name (child_die->tag));
11254 continue;
11255 }
11256
11257 imported_cu = cu;
11258 imported_die = follow_die_ref_or_sig (child_die, import_attr,
11259 &imported_cu);
11260 imported_name = dwarf2_name (imported_die, imported_cu);
11261 if (imported_name == NULL)
11262 {
11263 complaint (&symfile_complaints,
11264 _("child DW_TAG_imported_declaration has unknown "
9d8780f0
SM
11265 "imported name - DIE at %s [in module %s]"),
11266 sect_offset_str (child_die->sect_off),
11267 objfile_name (objfile));
32019081
JK
11268 continue;
11269 }
11270
eb1e02fd 11271 excludes.push_back (imported_name);
32019081
JK
11272
11273 process_die (child_die, cu);
11274 }
11275
22cee43f
PMR
11276 add_using_directive (using_directives (cu->language),
11277 import_prefix,
11278 canonical_name,
11279 import_alias,
11280 imported_declaration,
11281 excludes,
11282 0,
11283 &objfile->objfile_obstack);
27aa8d6a
SW
11284}
11285
5230b05a
WT
11286/* ICC<14 does not output the required DW_AT_declaration on incomplete
11287 types, but gives them a size of zero. Starting with version 14,
11288 ICC is compatible with GCC. */
11289
11290static int
11291producer_is_icc_lt_14 (struct dwarf2_cu *cu)
11292{
11293 if (!cu->checked_producer)
11294 check_producer (cu);
11295
11296 return cu->producer_is_icc_lt_14;
11297}
11298
1b80a9fa
JK
11299/* Check for possibly missing DW_AT_comp_dir with relative .debug_line
11300 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
11301 this, it was first present in GCC release 4.3.0. */
11302
11303static int
11304producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
11305{
11306 if (!cu->checked_producer)
11307 check_producer (cu);
11308
11309 return cu->producer_is_gcc_lt_4_3;
11310}
11311
d721ba37
PA
11312static file_and_directory
11313find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu)
9291a0cd 11314{
d721ba37
PA
11315 file_and_directory res;
11316
9291a0cd
TT
11317 /* Find the filename. Do not use dwarf2_name here, since the filename
11318 is not a source language identifier. */
d721ba37
PA
11319 res.name = dwarf2_string_attr (die, DW_AT_name, cu);
11320 res.comp_dir = dwarf2_string_attr (die, DW_AT_comp_dir, cu);
9291a0cd 11321
d721ba37
PA
11322 if (res.comp_dir == NULL
11323 && producer_is_gcc_lt_4_3 (cu) && res.name != NULL
11324 && IS_ABSOLUTE_PATH (res.name))
9291a0cd 11325 {
d721ba37
PA
11326 res.comp_dir_storage = ldirname (res.name);
11327 if (!res.comp_dir_storage.empty ())
11328 res.comp_dir = res.comp_dir_storage.c_str ();
9291a0cd 11329 }
d721ba37 11330 if (res.comp_dir != NULL)
9291a0cd
TT
11331 {
11332 /* Irix 6.2 native cc prepends <machine>.: to the compilation
11333 directory, get rid of it. */
d721ba37 11334 const char *cp = strchr (res.comp_dir, ':');
9291a0cd 11335
d721ba37
PA
11336 if (cp && cp != res.comp_dir && cp[-1] == '.' && cp[1] == '/')
11337 res.comp_dir = cp + 1;
9291a0cd
TT
11338 }
11339
d721ba37
PA
11340 if (res.name == NULL)
11341 res.name = "<unknown>";
11342
11343 return res;
9291a0cd
TT
11344}
11345
f4dc4d17
DE
11346/* Handle DW_AT_stmt_list for a compilation unit.
11347 DIE is the DW_TAG_compile_unit die for CU.
c3b7b696
YQ
11348 COMP_DIR is the compilation directory. LOWPC is passed to
11349 dwarf_decode_lines. See dwarf_decode_lines comments about it. */
2ab95328
TT
11350
11351static void
11352handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
c3b7b696 11353 const char *comp_dir, CORE_ADDR lowpc) /* ARI: editCase function */
2ab95328 11354{
518817b3
SM
11355 struct dwarf2_per_objfile *dwarf2_per_objfile
11356 = cu->per_cu->dwarf2_per_objfile;
527f3840 11357 struct objfile *objfile = dwarf2_per_objfile->objfile;
2ab95328 11358 struct attribute *attr;
527f3840
JK
11359 struct line_header line_header_local;
11360 hashval_t line_header_local_hash;
527f3840
JK
11361 void **slot;
11362 int decode_mapping;
2ab95328 11363
f4dc4d17
DE
11364 gdb_assert (! cu->per_cu->is_debug_types);
11365
2ab95328 11366 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
527f3840
JK
11367 if (attr == NULL)
11368 return;
11369
9c541725 11370 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
527f3840
JK
11371
11372 /* The line header hash table is only created if needed (it exists to
11373 prevent redundant reading of the line table for partial_units).
11374 If we're given a partial_unit, we'll need it. If we're given a
11375 compile_unit, then use the line header hash table if it's already
11376 created, but don't create one just yet. */
11377
11378 if (dwarf2_per_objfile->line_header_hash == NULL
11379 && die->tag == DW_TAG_partial_unit)
2ab95328 11380 {
527f3840
JK
11381 dwarf2_per_objfile->line_header_hash
11382 = htab_create_alloc_ex (127, line_header_hash_voidp,
11383 line_header_eq_voidp,
11384 free_line_header_voidp,
11385 &objfile->objfile_obstack,
11386 hashtab_obstack_allocate,
11387 dummy_obstack_deallocate);
11388 }
2ab95328 11389
9c541725 11390 line_header_local.sect_off = line_offset;
527f3840
JK
11391 line_header_local.offset_in_dwz = cu->per_cu->is_dwz;
11392 line_header_local_hash = line_header_hash (&line_header_local);
11393 if (dwarf2_per_objfile->line_header_hash != NULL)
11394 {
11395 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash,
11396 &line_header_local,
11397 line_header_local_hash, NO_INSERT);
11398
11399 /* For DW_TAG_compile_unit we need info like symtab::linetable which
11400 is not present in *SLOT (since if there is something in *SLOT then
11401 it will be for a partial_unit). */
11402 if (die->tag == DW_TAG_partial_unit && slot != NULL)
dee91e82 11403 {
527f3840 11404 gdb_assert (*slot != NULL);
9a3c8263 11405 cu->line_header = (struct line_header *) *slot;
527f3840 11406 return;
dee91e82 11407 }
2ab95328 11408 }
527f3840
JK
11409
11410 /* dwarf_decode_line_header does not yet provide sufficient information.
11411 We always have to call also dwarf_decode_lines for it. */
fff8551c
PA
11412 line_header_up lh = dwarf_decode_line_header (line_offset, cu);
11413 if (lh == NULL)
527f3840 11414 return;
4c8aa72d
PA
11415
11416 cu->line_header = lh.release ();
11417 cu->line_header_die_owner = die;
527f3840
JK
11418
11419 if (dwarf2_per_objfile->line_header_hash == NULL)
11420 slot = NULL;
11421 else
11422 {
11423 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash,
11424 &line_header_local,
11425 line_header_local_hash, INSERT);
11426 gdb_assert (slot != NULL);
11427 }
11428 if (slot != NULL && *slot == NULL)
11429 {
11430 /* This newly decoded line number information unit will be owned
11431 by line_header_hash hash table. */
11432 *slot = cu->line_header;
4c8aa72d 11433 cu->line_header_die_owner = NULL;
527f3840
JK
11434 }
11435 else
11436 {
11437 /* We cannot free any current entry in (*slot) as that struct line_header
11438 may be already used by multiple CUs. Create only temporary decoded
11439 line_header for this CU - it may happen at most once for each line
11440 number information unit. And if we're not using line_header_hash
11441 then this is what we want as well. */
11442 gdb_assert (die->tag != DW_TAG_partial_unit);
527f3840
JK
11443 }
11444 decode_mapping = (die->tag != DW_TAG_partial_unit);
11445 dwarf_decode_lines (cu->line_header, comp_dir, cu, NULL, lowpc,
11446 decode_mapping);
fff8551c 11447
2ab95328
TT
11448}
11449
95554aad 11450/* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
ae2de4f8 11451
c906108c 11452static void
e7c27a73 11453read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 11454{
518817b3
SM
11455 struct dwarf2_per_objfile *dwarf2_per_objfile
11456 = cu->per_cu->dwarf2_per_objfile;
dee91e82 11457 struct objfile *objfile = dwarf2_per_objfile->objfile;
3e29f34a 11458 struct gdbarch *gdbarch = get_objfile_arch (objfile);
2acceee2 11459 CORE_ADDR lowpc = ((CORE_ADDR) -1);
c906108c
SS
11460 CORE_ADDR highpc = ((CORE_ADDR) 0);
11461 struct attribute *attr;
c906108c 11462 struct die_info *child_die;
e142c38c 11463 CORE_ADDR baseaddr;
6e70227d 11464
e142c38c 11465 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 11466
fae299cd 11467 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
c906108c
SS
11468
11469 /* If we didn't find a lowpc, set it to highpc to avoid complaints
11470 from finish_block. */
2acceee2 11471 if (lowpc == ((CORE_ADDR) -1))
c906108c 11472 lowpc = highpc;
3e29f34a 11473 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
c906108c 11474
d721ba37 11475 file_and_directory fnd = find_file_and_directory (die, cu);
e1024ff1 11476
95554aad 11477 prepare_one_comp_unit (cu, die, cu->language);
303b6f5d 11478
f4b8a18d
KW
11479 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
11480 standardised yet. As a workaround for the language detection we fall
11481 back to the DW_AT_producer string. */
11482 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
11483 cu->language = language_opencl;
11484
3019eac3
DE
11485 /* Similar hack for Go. */
11486 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
11487 set_cu_language (DW_LANG_Go, cu);
11488
d721ba37 11489 dwarf2_start_symtab (cu, fnd.name, fnd.comp_dir, lowpc);
3019eac3
DE
11490
11491 /* Decode line number information if present. We do this before
11492 processing child DIEs, so that the line header table is available
11493 for DW_AT_decl_file. */
d721ba37 11494 handle_DW_AT_stmt_list (die, cu, fnd.comp_dir, lowpc);
3019eac3
DE
11495
11496 /* Process all dies in compilation unit. */
11497 if (die->child != NULL)
11498 {
11499 child_die = die->child;
11500 while (child_die && child_die->tag)
11501 {
11502 process_die (child_die, cu);
11503 child_die = sibling_die (child_die);
11504 }
11505 }
11506
11507 /* Decode macro information, if present. Dwarf 2 macro information
11508 refers to information in the line number info statement program
11509 header, so we can only read it if we've read the header
11510 successfully. */
0af92d60
JK
11511 attr = dwarf2_attr (die, DW_AT_macros, cu);
11512 if (attr == NULL)
11513 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
3019eac3
DE
11514 if (attr && cu->line_header)
11515 {
11516 if (dwarf2_attr (die, DW_AT_macro_info, cu))
11517 complaint (&symfile_complaints,
0af92d60 11518 _("CU refers to both DW_AT_macros and DW_AT_macro_info"));
3019eac3 11519
43f3e411 11520 dwarf_decode_macros (cu, DW_UNSND (attr), 1);
3019eac3
DE
11521 }
11522 else
11523 {
11524 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
11525 if (attr && cu->line_header)
11526 {
11527 unsigned int macro_offset = DW_UNSND (attr);
11528
43f3e411 11529 dwarf_decode_macros (cu, macro_offset, 0);
3019eac3
DE
11530 }
11531 }
3019eac3
DE
11532}
11533
f4dc4d17
DE
11534/* TU version of handle_DW_AT_stmt_list for read_type_unit_scope.
11535 Create the set of symtabs used by this TU, or if this TU is sharing
11536 symtabs with another TU and the symtabs have already been created
11537 then restore those symtabs in the line header.
11538 We don't need the pc/line-number mapping for type units. */
3019eac3
DE
11539
11540static void
f4dc4d17 11541setup_type_unit_groups (struct die_info *die, struct dwarf2_cu *cu)
3019eac3 11542{
f4dc4d17
DE
11543 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
11544 struct type_unit_group *tu_group;
11545 int first_time;
3019eac3 11546 struct attribute *attr;
9c541725 11547 unsigned int i;
0186c6a7 11548 struct signatured_type *sig_type;
3019eac3 11549
f4dc4d17 11550 gdb_assert (per_cu->is_debug_types);
0186c6a7 11551 sig_type = (struct signatured_type *) per_cu;
3019eac3 11552
f4dc4d17 11553 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
3019eac3 11554
f4dc4d17 11555 /* If we're using .gdb_index (includes -readnow) then
74e04d1c 11556 per_cu->type_unit_group may not have been set up yet. */
0186c6a7
DE
11557 if (sig_type->type_unit_group == NULL)
11558 sig_type->type_unit_group = get_type_unit_group (cu, attr);
11559 tu_group = sig_type->type_unit_group;
f4dc4d17
DE
11560
11561 /* If we've already processed this stmt_list there's no real need to
11562 do it again, we could fake it and just recreate the part we need
11563 (file name,index -> symtab mapping). If data shows this optimization
11564 is useful we can do it then. */
43f3e411 11565 first_time = tu_group->compunit_symtab == NULL;
f4dc4d17
DE
11566
11567 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
11568 debug info. */
fff8551c 11569 line_header_up lh;
f4dc4d17 11570 if (attr != NULL)
3019eac3 11571 {
9c541725 11572 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
f4dc4d17
DE
11573 lh = dwarf_decode_line_header (line_offset, cu);
11574 }
11575 if (lh == NULL)
11576 {
11577 if (first_time)
11578 dwarf2_start_symtab (cu, "", NULL, 0);
11579 else
11580 {
11581 gdb_assert (tu_group->symtabs == NULL);
0ab9ce85 11582 restart_symtab (tu_group->compunit_symtab, "", 0);
f4dc4d17 11583 }
f4dc4d17 11584 return;
3019eac3
DE
11585 }
11586
4c8aa72d
PA
11587 cu->line_header = lh.release ();
11588 cu->line_header_die_owner = die;
3019eac3 11589
f4dc4d17
DE
11590 if (first_time)
11591 {
43f3e411 11592 struct compunit_symtab *cust = dwarf2_start_symtab (cu, "", NULL, 0);
3019eac3 11593
1fd60fc0
DE
11594 /* Note: We don't assign tu_group->compunit_symtab yet because we're
11595 still initializing it, and our caller (a few levels up)
11596 process_full_type_unit still needs to know if this is the first
11597 time. */
11598
4c8aa72d
PA
11599 tu_group->num_symtabs = cu->line_header->file_names.size ();
11600 tu_group->symtabs = XNEWVEC (struct symtab *,
11601 cu->line_header->file_names.size ());
3019eac3 11602
4c8aa72d 11603 for (i = 0; i < cu->line_header->file_names.size (); ++i)
f4dc4d17 11604 {
4c8aa72d 11605 file_entry &fe = cu->line_header->file_names[i];
3019eac3 11606
4c8aa72d 11607 dwarf2_start_subfile (fe.name, fe.include_dir (cu->line_header));
3019eac3 11608
f4dc4d17
DE
11609 if (current_subfile->symtab == NULL)
11610 {
4c8aa72d
PA
11611 /* NOTE: start_subfile will recognize when it's been
11612 passed a file it has already seen. So we can't
11613 assume there's a simple mapping from
11614 cu->line_header->file_names to subfiles, plus
11615 cu->line_header->file_names may contain dups. */
43f3e411
DE
11616 current_subfile->symtab
11617 = allocate_symtab (cust, current_subfile->name);
f4dc4d17
DE
11618 }
11619
8c43009f
PA
11620 fe.symtab = current_subfile->symtab;
11621 tu_group->symtabs[i] = fe.symtab;
f4dc4d17
DE
11622 }
11623 }
11624 else
3019eac3 11625 {
0ab9ce85 11626 restart_symtab (tu_group->compunit_symtab, "", 0);
f4dc4d17 11627
4c8aa72d 11628 for (i = 0; i < cu->line_header->file_names.size (); ++i)
f4dc4d17 11629 {
4c8aa72d 11630 file_entry &fe = cu->line_header->file_names[i];
f4dc4d17 11631
4c8aa72d 11632 fe.symtab = tu_group->symtabs[i];
f4dc4d17 11633 }
3019eac3
DE
11634 }
11635
f4dc4d17
DE
11636 /* The main symtab is allocated last. Type units don't have DW_AT_name
11637 so they don't have a "real" (so to speak) symtab anyway.
11638 There is later code that will assign the main symtab to all symbols
11639 that don't have one. We need to handle the case of a symbol with a
11640 missing symtab (DW_AT_decl_file) anyway. */
11641}
3019eac3 11642
f4dc4d17
DE
11643/* Process DW_TAG_type_unit.
11644 For TUs we want to skip the first top level sibling if it's not the
11645 actual type being defined by this TU. In this case the first top
11646 level sibling is there to provide context only. */
3019eac3 11647
f4dc4d17
DE
11648static void
11649read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
11650{
11651 struct die_info *child_die;
3019eac3 11652
f4dc4d17
DE
11653 prepare_one_comp_unit (cu, die, language_minimal);
11654
11655 /* Initialize (or reinitialize) the machinery for building symtabs.
11656 We do this before processing child DIEs, so that the line header table
11657 is available for DW_AT_decl_file. */
11658 setup_type_unit_groups (die, cu);
11659
11660 if (die->child != NULL)
11661 {
11662 child_die = die->child;
11663 while (child_die && child_die->tag)
11664 {
11665 process_die (child_die, cu);
11666 child_die = sibling_die (child_die);
11667 }
11668 }
3019eac3
DE
11669}
11670\f
80626a55
DE
11671/* DWO/DWP files.
11672
11673 http://gcc.gnu.org/wiki/DebugFission
11674 http://gcc.gnu.org/wiki/DebugFissionDWP
11675
11676 To simplify handling of both DWO files ("object" files with the DWARF info)
11677 and DWP files (a file with the DWOs packaged up into one file), we treat
11678 DWP files as having a collection of virtual DWO files. */
3019eac3
DE
11679
11680static hashval_t
11681hash_dwo_file (const void *item)
11682{
9a3c8263 11683 const struct dwo_file *dwo_file = (const struct dwo_file *) item;
a2ce51a0 11684 hashval_t hash;
3019eac3 11685
a2ce51a0
DE
11686 hash = htab_hash_string (dwo_file->dwo_name);
11687 if (dwo_file->comp_dir != NULL)
11688 hash += htab_hash_string (dwo_file->comp_dir);
11689 return hash;
3019eac3
DE
11690}
11691
11692static int
11693eq_dwo_file (const void *item_lhs, const void *item_rhs)
11694{
9a3c8263
SM
11695 const struct dwo_file *lhs = (const struct dwo_file *) item_lhs;
11696 const struct dwo_file *rhs = (const struct dwo_file *) item_rhs;
3019eac3 11697
a2ce51a0
DE
11698 if (strcmp (lhs->dwo_name, rhs->dwo_name) != 0)
11699 return 0;
11700 if (lhs->comp_dir == NULL || rhs->comp_dir == NULL)
11701 return lhs->comp_dir == rhs->comp_dir;
11702 return strcmp (lhs->comp_dir, rhs->comp_dir) == 0;
3019eac3
DE
11703}
11704
11705/* Allocate a hash table for DWO files. */
11706
11707static htab_t
ed2dc618 11708allocate_dwo_file_hash_table (struct objfile *objfile)
3019eac3 11709{
3019eac3
DE
11710 return htab_create_alloc_ex (41,
11711 hash_dwo_file,
11712 eq_dwo_file,
11713 NULL,
11714 &objfile->objfile_obstack,
11715 hashtab_obstack_allocate,
11716 dummy_obstack_deallocate);
11717}
11718
80626a55
DE
11719/* Lookup DWO file DWO_NAME. */
11720
11721static void **
ed2dc618
SM
11722lookup_dwo_file_slot (struct dwarf2_per_objfile *dwarf2_per_objfile,
11723 const char *dwo_name,
11724 const char *comp_dir)
80626a55
DE
11725{
11726 struct dwo_file find_entry;
11727 void **slot;
11728
11729 if (dwarf2_per_objfile->dwo_files == NULL)
ed2dc618
SM
11730 dwarf2_per_objfile->dwo_files
11731 = allocate_dwo_file_hash_table (dwarf2_per_objfile->objfile);
80626a55
DE
11732
11733 memset (&find_entry, 0, sizeof (find_entry));
0ac5b59e
DE
11734 find_entry.dwo_name = dwo_name;
11735 find_entry.comp_dir = comp_dir;
80626a55
DE
11736 slot = htab_find_slot (dwarf2_per_objfile->dwo_files, &find_entry, INSERT);
11737
11738 return slot;
11739}
11740
3019eac3
DE
11741static hashval_t
11742hash_dwo_unit (const void *item)
11743{
9a3c8263 11744 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
3019eac3
DE
11745
11746 /* This drops the top 32 bits of the id, but is ok for a hash. */
11747 return dwo_unit->signature;
11748}
11749
11750static int
11751eq_dwo_unit (const void *item_lhs, const void *item_rhs)
11752{
9a3c8263
SM
11753 const struct dwo_unit *lhs = (const struct dwo_unit *) item_lhs;
11754 const struct dwo_unit *rhs = (const struct dwo_unit *) item_rhs;
3019eac3
DE
11755
11756 /* The signature is assumed to be unique within the DWO file.
11757 So while object file CU dwo_id's always have the value zero,
11758 that's OK, assuming each object file DWO file has only one CU,
11759 and that's the rule for now. */
11760 return lhs->signature == rhs->signature;
11761}
11762
11763/* Allocate a hash table for DWO CUs,TUs.
11764 There is one of these tables for each of CUs,TUs for each DWO file. */
11765
11766static htab_t
11767allocate_dwo_unit_table (struct objfile *objfile)
11768{
11769 /* Start out with a pretty small number.
11770 Generally DWO files contain only one CU and maybe some TUs. */
11771 return htab_create_alloc_ex (3,
11772 hash_dwo_unit,
11773 eq_dwo_unit,
11774 NULL,
11775 &objfile->objfile_obstack,
11776 hashtab_obstack_allocate,
11777 dummy_obstack_deallocate);
11778}
11779
80626a55 11780/* Structure used to pass data to create_dwo_debug_info_hash_table_reader. */
3019eac3 11781
19c3d4c9 11782struct create_dwo_cu_data
3019eac3
DE
11783{
11784 struct dwo_file *dwo_file;
19c3d4c9 11785 struct dwo_unit dwo_unit;
3019eac3
DE
11786};
11787
19c3d4c9 11788/* die_reader_func for create_dwo_cu. */
3019eac3
DE
11789
11790static void
19c3d4c9
DE
11791create_dwo_cu_reader (const struct die_reader_specs *reader,
11792 const gdb_byte *info_ptr,
11793 struct die_info *comp_unit_die,
11794 int has_children,
11795 void *datap)
3019eac3
DE
11796{
11797 struct dwarf2_cu *cu = reader->cu;
9c541725 11798 sect_offset sect_off = cu->per_cu->sect_off;
8a0459fd 11799 struct dwarf2_section_info *section = cu->per_cu->section;
9a3c8263 11800 struct create_dwo_cu_data *data = (struct create_dwo_cu_data *) datap;
3019eac3 11801 struct dwo_file *dwo_file = data->dwo_file;
19c3d4c9 11802 struct dwo_unit *dwo_unit = &data->dwo_unit;
3019eac3 11803 struct attribute *attr;
3019eac3
DE
11804
11805 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
11806 if (attr == NULL)
11807 {
19c3d4c9 11808 complaint (&symfile_complaints,
9d8780f0 11809 _("Dwarf Error: debug entry at offset %s is missing"
19c3d4c9 11810 " its dwo_id [in module %s]"),
9d8780f0 11811 sect_offset_str (sect_off), dwo_file->dwo_name);
3019eac3
DE
11812 return;
11813 }
11814
3019eac3
DE
11815 dwo_unit->dwo_file = dwo_file;
11816 dwo_unit->signature = DW_UNSND (attr);
8a0459fd 11817 dwo_unit->section = section;
9c541725 11818 dwo_unit->sect_off = sect_off;
3019eac3
DE
11819 dwo_unit->length = cu->per_cu->length;
11820
b4f54984 11821 if (dwarf_read_debug)
9d8780f0
SM
11822 fprintf_unfiltered (gdb_stdlog, " offset %s, dwo_id %s\n",
11823 sect_offset_str (sect_off),
9c541725 11824 hex_string (dwo_unit->signature));
3019eac3
DE
11825}
11826
33c5cd75 11827/* Create the dwo_units for the CUs in a DWO_FILE.
19c3d4c9 11828 Note: This function processes DWO files only, not DWP files. */
3019eac3 11829
33c5cd75 11830static void
ed2dc618
SM
11831create_cus_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
11832 struct dwo_file &dwo_file, dwarf2_section_info &section,
33c5cd75 11833 htab_t &cus_htab)
3019eac3
DE
11834{
11835 struct objfile *objfile = dwarf2_per_objfile->objfile;
d521ce57 11836 const gdb_byte *info_ptr, *end_ptr;
3019eac3 11837
33c5cd75
DB
11838 dwarf2_read_section (objfile, &section);
11839 info_ptr = section.buffer;
3019eac3
DE
11840
11841 if (info_ptr == NULL)
33c5cd75 11842 return;
3019eac3 11843
b4f54984 11844 if (dwarf_read_debug)
19c3d4c9
DE
11845 {
11846 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
33c5cd75
DB
11847 get_section_name (&section),
11848 get_section_file_name (&section));
19c3d4c9 11849 }
3019eac3 11850
33c5cd75 11851 end_ptr = info_ptr + section.size;
3019eac3
DE
11852 while (info_ptr < end_ptr)
11853 {
11854 struct dwarf2_per_cu_data per_cu;
33c5cd75
DB
11855 struct create_dwo_cu_data create_dwo_cu_data;
11856 struct dwo_unit *dwo_unit;
11857 void **slot;
11858 sect_offset sect_off = (sect_offset) (info_ptr - section.buffer);
3019eac3 11859
19c3d4c9
DE
11860 memset (&create_dwo_cu_data.dwo_unit, 0,
11861 sizeof (create_dwo_cu_data.dwo_unit));
3019eac3 11862 memset (&per_cu, 0, sizeof (per_cu));
e3b94546 11863 per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
3019eac3 11864 per_cu.is_debug_types = 0;
33c5cd75
DB
11865 per_cu.sect_off = sect_offset (info_ptr - section.buffer);
11866 per_cu.section = &section;
c5ed0576 11867 create_dwo_cu_data.dwo_file = &dwo_file;
33c5cd75
DB
11868
11869 init_cutu_and_read_dies_no_follow (
11870 &per_cu, &dwo_file, create_dwo_cu_reader, &create_dwo_cu_data);
11871 info_ptr += per_cu.length;
11872
11873 // If the unit could not be parsed, skip it.
11874 if (create_dwo_cu_data.dwo_unit.dwo_file == NULL)
11875 continue;
3019eac3 11876
33c5cd75
DB
11877 if (cus_htab == NULL)
11878 cus_htab = allocate_dwo_unit_table (objfile);
19c3d4c9 11879
33c5cd75
DB
11880 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
11881 *dwo_unit = create_dwo_cu_data.dwo_unit;
11882 slot = htab_find_slot (cus_htab, dwo_unit, INSERT);
11883 gdb_assert (slot != NULL);
11884 if (*slot != NULL)
19c3d4c9 11885 {
33c5cd75
DB
11886 const struct dwo_unit *dup_cu = (const struct dwo_unit *)*slot;
11887 sect_offset dup_sect_off = dup_cu->sect_off;
19c3d4c9 11888
33c5cd75 11889 complaint (&symfile_complaints,
9d8780f0
SM
11890 _("debug cu entry at offset %s is duplicate to"
11891 " the entry at offset %s, signature %s"),
11892 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
33c5cd75 11893 hex_string (dwo_unit->signature));
19c3d4c9 11894 }
33c5cd75 11895 *slot = (void *)dwo_unit;
3019eac3 11896 }
3019eac3
DE
11897}
11898
80626a55
DE
11899/* DWP file .debug_{cu,tu}_index section format:
11900 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
11901
d2415c6c
DE
11902 DWP Version 1:
11903
80626a55
DE
11904 Both index sections have the same format, and serve to map a 64-bit
11905 signature to a set of section numbers. Each section begins with a header,
11906 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
11907 indexes, and a pool of 32-bit section numbers. The index sections will be
11908 aligned at 8-byte boundaries in the file.
11909
d2415c6c
DE
11910 The index section header consists of:
11911
11912 V, 32 bit version number
11913 -, 32 bits unused
11914 N, 32 bit number of compilation units or type units in the index
11915 M, 32 bit number of slots in the hash table
80626a55 11916
d2415c6c 11917 Numbers are recorded using the byte order of the application binary.
80626a55 11918
d2415c6c
DE
11919 The hash table begins at offset 16 in the section, and consists of an array
11920 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
11921 order of the application binary). Unused slots in the hash table are 0.
11922 (We rely on the extreme unlikeliness of a signature being exactly 0.)
80626a55 11923
d2415c6c
DE
11924 The parallel table begins immediately after the hash table
11925 (at offset 16 + 8 * M from the beginning of the section), and consists of an
11926 array of 32-bit indexes (using the byte order of the application binary),
11927 corresponding 1-1 with slots in the hash table. Each entry in the parallel
11928 table contains a 32-bit index into the pool of section numbers. For unused
11929 hash table slots, the corresponding entry in the parallel table will be 0.
80626a55 11930
73869dc2
DE
11931 The pool of section numbers begins immediately following the hash table
11932 (at offset 16 + 12 * M from the beginning of the section). The pool of
11933 section numbers consists of an array of 32-bit words (using the byte order
11934 of the application binary). Each item in the array is indexed starting
11935 from 0. The hash table entry provides the index of the first section
11936 number in the set. Additional section numbers in the set follow, and the
11937 set is terminated by a 0 entry (section number 0 is not used in ELF).
11938
11939 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
11940 section must be the first entry in the set, and the .debug_abbrev.dwo must
11941 be the second entry. Other members of the set may follow in any order.
11942
11943 ---
11944
11945 DWP Version 2:
11946
11947 DWP Version 2 combines all the .debug_info, etc. sections into one,
11948 and the entries in the index tables are now offsets into these sections.
11949 CU offsets begin at 0. TU offsets begin at the size of the .debug_info
11950 section.
11951
11952 Index Section Contents:
11953 Header
11954 Hash Table of Signatures dwp_hash_table.hash_table
11955 Parallel Table of Indices dwp_hash_table.unit_table
11956 Table of Section Offsets dwp_hash_table.v2.{section_ids,offsets}
11957 Table of Section Sizes dwp_hash_table.v2.sizes
11958
11959 The index section header consists of:
11960
11961 V, 32 bit version number
11962 L, 32 bit number of columns in the table of section offsets
11963 N, 32 bit number of compilation units or type units in the index
11964 M, 32 bit number of slots in the hash table
11965
11966 Numbers are recorded using the byte order of the application binary.
11967
11968 The hash table has the same format as version 1.
11969 The parallel table of indices has the same format as version 1,
11970 except that the entries are origin-1 indices into the table of sections
11971 offsets and the table of section sizes.
11972
11973 The table of offsets begins immediately following the parallel table
11974 (at offset 16 + 12 * M from the beginning of the section). The table is
11975 a two-dimensional array of 32-bit words (using the byte order of the
11976 application binary), with L columns and N+1 rows, in row-major order.
11977 Each row in the array is indexed starting from 0. The first row provides
11978 a key to the remaining rows: each column in this row provides an identifier
11979 for a debug section, and the offsets in the same column of subsequent rows
11980 refer to that section. The section identifiers are:
11981
11982 DW_SECT_INFO 1 .debug_info.dwo
11983 DW_SECT_TYPES 2 .debug_types.dwo
11984 DW_SECT_ABBREV 3 .debug_abbrev.dwo
11985 DW_SECT_LINE 4 .debug_line.dwo
11986 DW_SECT_LOC 5 .debug_loc.dwo
11987 DW_SECT_STR_OFFSETS 6 .debug_str_offsets.dwo
11988 DW_SECT_MACINFO 7 .debug_macinfo.dwo
11989 DW_SECT_MACRO 8 .debug_macro.dwo
11990
11991 The offsets provided by the CU and TU index sections are the base offsets
11992 for the contributions made by each CU or TU to the corresponding section
11993 in the package file. Each CU and TU header contains an abbrev_offset
11994 field, used to find the abbreviations table for that CU or TU within the
11995 contribution to the .debug_abbrev.dwo section for that CU or TU, and should
11996 be interpreted as relative to the base offset given in the index section.
11997 Likewise, offsets into .debug_line.dwo from DW_AT_stmt_list attributes
11998 should be interpreted as relative to the base offset for .debug_line.dwo,
11999 and offsets into other debug sections obtained from DWARF attributes should
12000 also be interpreted as relative to the corresponding base offset.
12001
12002 The table of sizes begins immediately following the table of offsets.
12003 Like the table of offsets, it is a two-dimensional array of 32-bit words,
12004 with L columns and N rows, in row-major order. Each row in the array is
12005 indexed starting from 1 (row 0 is shared by the two tables).
12006
12007 ---
12008
12009 Hash table lookup is handled the same in version 1 and 2:
12010
12011 We assume that N and M will not exceed 2^32 - 1.
12012 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
12013
d2415c6c
DE
12014 Given a 64-bit compilation unit signature or a type signature S, an entry
12015 in the hash table is located as follows:
80626a55 12016
d2415c6c
DE
12017 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
12018 the low-order k bits all set to 1.
80626a55 12019
d2415c6c 12020 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
80626a55 12021
d2415c6c
DE
12022 3) If the hash table entry at index H matches the signature, use that
12023 entry. If the hash table entry at index H is unused (all zeroes),
12024 terminate the search: the signature is not present in the table.
80626a55 12025
d2415c6c 12026 4) Let H = (H + H') modulo M. Repeat at Step 3.
80626a55 12027
d2415c6c 12028 Because M > N and H' and M are relatively prime, the search is guaranteed
73869dc2 12029 to stop at an unused slot or find the match. */
80626a55
DE
12030
12031/* Create a hash table to map DWO IDs to their CU/TU entry in
12032 .debug_{info,types}.dwo in DWP_FILE.
12033 Returns NULL if there isn't one.
12034 Note: This function processes DWP files only, not DWO files. */
12035
12036static struct dwp_hash_table *
ed2dc618
SM
12037create_dwp_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
12038 struct dwp_file *dwp_file, int is_debug_types)
80626a55
DE
12039{
12040 struct objfile *objfile = dwarf2_per_objfile->objfile;
12041 bfd *dbfd = dwp_file->dbfd;
948f8e3d 12042 const gdb_byte *index_ptr, *index_end;
80626a55 12043 struct dwarf2_section_info *index;
73869dc2 12044 uint32_t version, nr_columns, nr_units, nr_slots;
80626a55
DE
12045 struct dwp_hash_table *htab;
12046
12047 if (is_debug_types)
12048 index = &dwp_file->sections.tu_index;
12049 else
12050 index = &dwp_file->sections.cu_index;
12051
12052 if (dwarf2_section_empty_p (index))
12053 return NULL;
12054 dwarf2_read_section (objfile, index);
12055
12056 index_ptr = index->buffer;
12057 index_end = index_ptr + index->size;
12058
12059 version = read_4_bytes (dbfd, index_ptr);
73869dc2
DE
12060 index_ptr += 4;
12061 if (version == 2)
12062 nr_columns = read_4_bytes (dbfd, index_ptr);
12063 else
12064 nr_columns = 0;
12065 index_ptr += 4;
80626a55
DE
12066 nr_units = read_4_bytes (dbfd, index_ptr);
12067 index_ptr += 4;
12068 nr_slots = read_4_bytes (dbfd, index_ptr);
12069 index_ptr += 4;
12070
73869dc2 12071 if (version != 1 && version != 2)
80626a55 12072 {
21aa081e 12073 error (_("Dwarf Error: unsupported DWP file version (%s)"
80626a55 12074 " [in module %s]"),
21aa081e 12075 pulongest (version), dwp_file->name);
80626a55
DE
12076 }
12077 if (nr_slots != (nr_slots & -nr_slots))
12078 {
21aa081e 12079 error (_("Dwarf Error: number of slots in DWP hash table (%s)"
80626a55 12080 " is not power of 2 [in module %s]"),
21aa081e 12081 pulongest (nr_slots), dwp_file->name);
80626a55
DE
12082 }
12083
12084 htab = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_hash_table);
73869dc2
DE
12085 htab->version = version;
12086 htab->nr_columns = nr_columns;
80626a55
DE
12087 htab->nr_units = nr_units;
12088 htab->nr_slots = nr_slots;
12089 htab->hash_table = index_ptr;
12090 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
73869dc2
DE
12091
12092 /* Exit early if the table is empty. */
12093 if (nr_slots == 0 || nr_units == 0
12094 || (version == 2 && nr_columns == 0))
12095 {
12096 /* All must be zero. */
12097 if (nr_slots != 0 || nr_units != 0
12098 || (version == 2 && nr_columns != 0))
12099 {
12100 complaint (&symfile_complaints,
12101 _("Empty DWP but nr_slots,nr_units,nr_columns not"
12102 " all zero [in modules %s]"),
12103 dwp_file->name);
12104 }
12105 return htab;
12106 }
12107
12108 if (version == 1)
12109 {
12110 htab->section_pool.v1.indices =
12111 htab->unit_table + sizeof (uint32_t) * nr_slots;
12112 /* It's harder to decide whether the section is too small in v1.
12113 V1 is deprecated anyway so we punt. */
12114 }
12115 else
12116 {
12117 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
12118 int *ids = htab->section_pool.v2.section_ids;
12119 /* Reverse map for error checking. */
12120 int ids_seen[DW_SECT_MAX + 1];
12121 int i;
12122
12123 if (nr_columns < 2)
12124 {
12125 error (_("Dwarf Error: bad DWP hash table, too few columns"
12126 " in section table [in module %s]"),
12127 dwp_file->name);
12128 }
12129 if (nr_columns > MAX_NR_V2_DWO_SECTIONS)
12130 {
12131 error (_("Dwarf Error: bad DWP hash table, too many columns"
12132 " in section table [in module %s]"),
12133 dwp_file->name);
12134 }
12135 memset (ids, 255, (DW_SECT_MAX + 1) * sizeof (int32_t));
12136 memset (ids_seen, 255, (DW_SECT_MAX + 1) * sizeof (int32_t));
12137 for (i = 0; i < nr_columns; ++i)
12138 {
12139 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
12140
12141 if (id < DW_SECT_MIN || id > DW_SECT_MAX)
12142 {
12143 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
12144 " in section table [in module %s]"),
12145 id, dwp_file->name);
12146 }
12147 if (ids_seen[id] != -1)
12148 {
12149 error (_("Dwarf Error: bad DWP hash table, duplicate section"
12150 " id %d in section table [in module %s]"),
12151 id, dwp_file->name);
12152 }
12153 ids_seen[id] = i;
12154 ids[i] = id;
12155 }
12156 /* Must have exactly one info or types section. */
12157 if (((ids_seen[DW_SECT_INFO] != -1)
12158 + (ids_seen[DW_SECT_TYPES] != -1))
12159 != 1)
12160 {
12161 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
12162 " DWO info/types section [in module %s]"),
12163 dwp_file->name);
12164 }
12165 /* Must have an abbrev section. */
12166 if (ids_seen[DW_SECT_ABBREV] == -1)
12167 {
12168 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
12169 " section [in module %s]"),
12170 dwp_file->name);
12171 }
12172 htab->section_pool.v2.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
12173 htab->section_pool.v2.sizes =
12174 htab->section_pool.v2.offsets + (sizeof (uint32_t)
12175 * nr_units * nr_columns);
12176 if ((htab->section_pool.v2.sizes + (sizeof (uint32_t)
12177 * nr_units * nr_columns))
12178 > index_end)
12179 {
12180 error (_("Dwarf Error: DWP index section is corrupt (too small)"
12181 " [in module %s]"),
12182 dwp_file->name);
12183 }
12184 }
80626a55
DE
12185
12186 return htab;
12187}
12188
12189/* Update SECTIONS with the data from SECTP.
12190
12191 This function is like the other "locate" section routines that are
12192 passed to bfd_map_over_sections, but in this context the sections to
73869dc2 12193 read comes from the DWP V1 hash table, not the full ELF section table.
80626a55
DE
12194
12195 The result is non-zero for success, or zero if an error was found. */
12196
12197static int
73869dc2
DE
12198locate_v1_virtual_dwo_sections (asection *sectp,
12199 struct virtual_v1_dwo_sections *sections)
80626a55
DE
12200{
12201 const struct dwop_section_names *names = &dwop_section_names;
12202
12203 if (section_is_p (sectp->name, &names->abbrev_dwo))
12204 {
12205 /* There can be only one. */
049412e3 12206 if (sections->abbrev.s.section != NULL)
80626a55 12207 return 0;
049412e3 12208 sections->abbrev.s.section = sectp;
80626a55
DE
12209 sections->abbrev.size = bfd_get_section_size (sectp);
12210 }
12211 else if (section_is_p (sectp->name, &names->info_dwo)
12212 || section_is_p (sectp->name, &names->types_dwo))
12213 {
12214 /* There can be only one. */
049412e3 12215 if (sections->info_or_types.s.section != NULL)
80626a55 12216 return 0;
049412e3 12217 sections->info_or_types.s.section = sectp;
80626a55
DE
12218 sections->info_or_types.size = bfd_get_section_size (sectp);
12219 }
12220 else if (section_is_p (sectp->name, &names->line_dwo))
12221 {
12222 /* There can be only one. */
049412e3 12223 if (sections->line.s.section != NULL)
80626a55 12224 return 0;
049412e3 12225 sections->line.s.section = sectp;
80626a55
DE
12226 sections->line.size = bfd_get_section_size (sectp);
12227 }
12228 else if (section_is_p (sectp->name, &names->loc_dwo))
12229 {
12230 /* There can be only one. */
049412e3 12231 if (sections->loc.s.section != NULL)
80626a55 12232 return 0;
049412e3 12233 sections->loc.s.section = sectp;
80626a55
DE
12234 sections->loc.size = bfd_get_section_size (sectp);
12235 }
12236 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12237 {
12238 /* There can be only one. */
049412e3 12239 if (sections->macinfo.s.section != NULL)
80626a55 12240 return 0;
049412e3 12241 sections->macinfo.s.section = sectp;
80626a55
DE
12242 sections->macinfo.size = bfd_get_section_size (sectp);
12243 }
12244 else if (section_is_p (sectp->name, &names->macro_dwo))
12245 {
12246 /* There can be only one. */
049412e3 12247 if (sections->macro.s.section != NULL)
80626a55 12248 return 0;
049412e3 12249 sections->macro.s.section = sectp;
80626a55
DE
12250 sections->macro.size = bfd_get_section_size (sectp);
12251 }
12252 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12253 {
12254 /* There can be only one. */
049412e3 12255 if (sections->str_offsets.s.section != NULL)
80626a55 12256 return 0;
049412e3 12257 sections->str_offsets.s.section = sectp;
80626a55
DE
12258 sections->str_offsets.size = bfd_get_section_size (sectp);
12259 }
12260 else
12261 {
12262 /* No other kind of section is valid. */
12263 return 0;
12264 }
12265
12266 return 1;
12267}
12268
73869dc2
DE
12269/* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
12270 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
12271 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
12272 This is for DWP version 1 files. */
80626a55
DE
12273
12274static struct dwo_unit *
ed2dc618
SM
12275create_dwo_unit_in_dwp_v1 (struct dwarf2_per_objfile *dwarf2_per_objfile,
12276 struct dwp_file *dwp_file,
73869dc2
DE
12277 uint32_t unit_index,
12278 const char *comp_dir,
12279 ULONGEST signature, int is_debug_types)
80626a55
DE
12280{
12281 struct objfile *objfile = dwarf2_per_objfile->objfile;
73869dc2
DE
12282 const struct dwp_hash_table *dwp_htab =
12283 is_debug_types ? dwp_file->tus : dwp_file->cus;
80626a55
DE
12284 bfd *dbfd = dwp_file->dbfd;
12285 const char *kind = is_debug_types ? "TU" : "CU";
12286 struct dwo_file *dwo_file;
12287 struct dwo_unit *dwo_unit;
73869dc2 12288 struct virtual_v1_dwo_sections sections;
80626a55 12289 void **dwo_file_slot;
80626a55
DE
12290 int i;
12291
73869dc2
DE
12292 gdb_assert (dwp_file->version == 1);
12293
b4f54984 12294 if (dwarf_read_debug)
80626a55 12295 {
73869dc2 12296 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V1 file: %s\n",
80626a55 12297 kind,
73869dc2 12298 pulongest (unit_index), hex_string (signature),
80626a55
DE
12299 dwp_file->name);
12300 }
12301
19ac8c2e 12302 /* Fetch the sections of this DWO unit.
80626a55
DE
12303 Put a limit on the number of sections we look for so that bad data
12304 doesn't cause us to loop forever. */
12305
73869dc2 12306#define MAX_NR_V1_DWO_SECTIONS \
80626a55
DE
12307 (1 /* .debug_info or .debug_types */ \
12308 + 1 /* .debug_abbrev */ \
12309 + 1 /* .debug_line */ \
12310 + 1 /* .debug_loc */ \
12311 + 1 /* .debug_str_offsets */ \
19ac8c2e 12312 + 1 /* .debug_macro or .debug_macinfo */ \
80626a55
DE
12313 + 1 /* trailing zero */)
12314
12315 memset (&sections, 0, sizeof (sections));
80626a55 12316
73869dc2 12317 for (i = 0; i < MAX_NR_V1_DWO_SECTIONS; ++i)
80626a55
DE
12318 {
12319 asection *sectp;
12320 uint32_t section_nr =
12321 read_4_bytes (dbfd,
73869dc2
DE
12322 dwp_htab->section_pool.v1.indices
12323 + (unit_index + i) * sizeof (uint32_t));
80626a55
DE
12324
12325 if (section_nr == 0)
12326 break;
12327 if (section_nr >= dwp_file->num_sections)
12328 {
12329 error (_("Dwarf Error: bad DWP hash table, section number too large"
12330 " [in module %s]"),
12331 dwp_file->name);
12332 }
12333
12334 sectp = dwp_file->elf_sections[section_nr];
73869dc2 12335 if (! locate_v1_virtual_dwo_sections (sectp, &sections))
80626a55
DE
12336 {
12337 error (_("Dwarf Error: bad DWP hash table, invalid section found"
12338 " [in module %s]"),
12339 dwp_file->name);
12340 }
12341 }
12342
12343 if (i < 2
a32a8923
DE
12344 || dwarf2_section_empty_p (&sections.info_or_types)
12345 || dwarf2_section_empty_p (&sections.abbrev))
80626a55
DE
12346 {
12347 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
12348 " [in module %s]"),
12349 dwp_file->name);
12350 }
73869dc2 12351 if (i == MAX_NR_V1_DWO_SECTIONS)
80626a55
DE
12352 {
12353 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
12354 " [in module %s]"),
12355 dwp_file->name);
12356 }
12357
12358 /* It's easier for the rest of the code if we fake a struct dwo_file and
12359 have dwo_unit "live" in that. At least for now.
12360
12361 The DWP file can be made up of a random collection of CUs and TUs.
c766f7ec 12362 However, for each CU + set of TUs that came from the same original DWO
57d63ce2
DE
12363 file, we can combine them back into a virtual DWO file to save space
12364 (fewer struct dwo_file objects to allocate). Remember that for really
80626a55
DE
12365 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
12366
791afaa2
TT
12367 std::string virtual_dwo_name =
12368 string_printf ("virtual-dwo/%d-%d-%d-%d",
12369 get_section_id (&sections.abbrev),
12370 get_section_id (&sections.line),
12371 get_section_id (&sections.loc),
12372 get_section_id (&sections.str_offsets));
80626a55 12373 /* Can we use an existing virtual DWO file? */
ed2dc618
SM
12374 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
12375 virtual_dwo_name.c_str (),
12376 comp_dir);
80626a55
DE
12377 /* Create one if necessary. */
12378 if (*dwo_file_slot == NULL)
12379 {
b4f54984 12380 if (dwarf_read_debug)
80626a55
DE
12381 {
12382 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
791afaa2 12383 virtual_dwo_name.c_str ());
80626a55
DE
12384 }
12385 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
224c3ddb
SM
12386 dwo_file->dwo_name
12387 = (const char *) obstack_copy0 (&objfile->objfile_obstack,
791afaa2
TT
12388 virtual_dwo_name.c_str (),
12389 virtual_dwo_name.size ());
0ac5b59e 12390 dwo_file->comp_dir = comp_dir;
80626a55
DE
12391 dwo_file->sections.abbrev = sections.abbrev;
12392 dwo_file->sections.line = sections.line;
12393 dwo_file->sections.loc = sections.loc;
12394 dwo_file->sections.macinfo = sections.macinfo;
12395 dwo_file->sections.macro = sections.macro;
12396 dwo_file->sections.str_offsets = sections.str_offsets;
12397 /* The "str" section is global to the entire DWP file. */
12398 dwo_file->sections.str = dwp_file->sections.str;
57d63ce2 12399 /* The info or types section is assigned below to dwo_unit,
80626a55
DE
12400 there's no need to record it in dwo_file.
12401 Also, we can't simply record type sections in dwo_file because
12402 we record a pointer into the vector in dwo_unit. As we collect more
12403 types we'll grow the vector and eventually have to reallocate space
57d63ce2
DE
12404 for it, invalidating all copies of pointers into the previous
12405 contents. */
80626a55
DE
12406 *dwo_file_slot = dwo_file;
12407 }
12408 else
12409 {
b4f54984 12410 if (dwarf_read_debug)
80626a55
DE
12411 {
12412 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
791afaa2 12413 virtual_dwo_name.c_str ());
80626a55 12414 }
9a3c8263 12415 dwo_file = (struct dwo_file *) *dwo_file_slot;
80626a55 12416 }
80626a55
DE
12417
12418 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
12419 dwo_unit->dwo_file = dwo_file;
12420 dwo_unit->signature = signature;
8d749320
SM
12421 dwo_unit->section =
12422 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
8a0459fd 12423 *dwo_unit->section = sections.info_or_types;
57d63ce2 12424 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
80626a55
DE
12425
12426 return dwo_unit;
12427}
12428
73869dc2
DE
12429/* Subroutine of create_dwo_unit_in_dwp_v2 to simplify it.
12430 Given a pointer to the containing section SECTION, and OFFSET,SIZE of the
12431 piece within that section used by a TU/CU, return a virtual section
12432 of just that piece. */
12433
12434static struct dwarf2_section_info
ed2dc618
SM
12435create_dwp_v2_section (struct dwarf2_per_objfile *dwarf2_per_objfile,
12436 struct dwarf2_section_info *section,
73869dc2
DE
12437 bfd_size_type offset, bfd_size_type size)
12438{
12439 struct dwarf2_section_info result;
12440 asection *sectp;
12441
12442 gdb_assert (section != NULL);
12443 gdb_assert (!section->is_virtual);
12444
12445 memset (&result, 0, sizeof (result));
12446 result.s.containing_section = section;
12447 result.is_virtual = 1;
12448
12449 if (size == 0)
12450 return result;
12451
12452 sectp = get_section_bfd_section (section);
12453
12454 /* Flag an error if the piece denoted by OFFSET,SIZE is outside the
12455 bounds of the real section. This is a pretty-rare event, so just
12456 flag an error (easier) instead of a warning and trying to cope. */
12457 if (sectp == NULL
12458 || offset + size > bfd_get_section_size (sectp))
12459 {
73869dc2
DE
12460 error (_("Dwarf Error: Bad DWP V2 section info, doesn't fit"
12461 " in section %s [in module %s]"),
12462 sectp ? bfd_section_name (abfd, sectp) : "<unknown>",
12463 objfile_name (dwarf2_per_objfile->objfile));
12464 }
12465
12466 result.virtual_offset = offset;
12467 result.size = size;
12468 return result;
12469}
12470
12471/* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
12472 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
12473 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
12474 This is for DWP version 2 files. */
12475
12476static struct dwo_unit *
ed2dc618
SM
12477create_dwo_unit_in_dwp_v2 (struct dwarf2_per_objfile *dwarf2_per_objfile,
12478 struct dwp_file *dwp_file,
73869dc2
DE
12479 uint32_t unit_index,
12480 const char *comp_dir,
12481 ULONGEST signature, int is_debug_types)
12482{
12483 struct objfile *objfile = dwarf2_per_objfile->objfile;
12484 const struct dwp_hash_table *dwp_htab =
12485 is_debug_types ? dwp_file->tus : dwp_file->cus;
12486 bfd *dbfd = dwp_file->dbfd;
12487 const char *kind = is_debug_types ? "TU" : "CU";
12488 struct dwo_file *dwo_file;
12489 struct dwo_unit *dwo_unit;
12490 struct virtual_v2_dwo_sections sections;
12491 void **dwo_file_slot;
73869dc2
DE
12492 int i;
12493
12494 gdb_assert (dwp_file->version == 2);
12495
b4f54984 12496 if (dwarf_read_debug)
73869dc2
DE
12497 {
12498 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V2 file: %s\n",
12499 kind,
12500 pulongest (unit_index), hex_string (signature),
12501 dwp_file->name);
12502 }
12503
12504 /* Fetch the section offsets of this DWO unit. */
12505
12506 memset (&sections, 0, sizeof (sections));
73869dc2
DE
12507
12508 for (i = 0; i < dwp_htab->nr_columns; ++i)
12509 {
12510 uint32_t offset = read_4_bytes (dbfd,
12511 dwp_htab->section_pool.v2.offsets
12512 + (((unit_index - 1) * dwp_htab->nr_columns
12513 + i)
12514 * sizeof (uint32_t)));
12515 uint32_t size = read_4_bytes (dbfd,
12516 dwp_htab->section_pool.v2.sizes
12517 + (((unit_index - 1) * dwp_htab->nr_columns
12518 + i)
12519 * sizeof (uint32_t)));
12520
12521 switch (dwp_htab->section_pool.v2.section_ids[i])
12522 {
12523 case DW_SECT_INFO:
12524 case DW_SECT_TYPES:
12525 sections.info_or_types_offset = offset;
12526 sections.info_or_types_size = size;
12527 break;
12528 case DW_SECT_ABBREV:
12529 sections.abbrev_offset = offset;
12530 sections.abbrev_size = size;
12531 break;
12532 case DW_SECT_LINE:
12533 sections.line_offset = offset;
12534 sections.line_size = size;
12535 break;
12536 case DW_SECT_LOC:
12537 sections.loc_offset = offset;
12538 sections.loc_size = size;
12539 break;
12540 case DW_SECT_STR_OFFSETS:
12541 sections.str_offsets_offset = offset;
12542 sections.str_offsets_size = size;
12543 break;
12544 case DW_SECT_MACINFO:
12545 sections.macinfo_offset = offset;
12546 sections.macinfo_size = size;
12547 break;
12548 case DW_SECT_MACRO:
12549 sections.macro_offset = offset;
12550 sections.macro_size = size;
12551 break;
12552 }
12553 }
12554
12555 /* It's easier for the rest of the code if we fake a struct dwo_file and
12556 have dwo_unit "live" in that. At least for now.
12557
12558 The DWP file can be made up of a random collection of CUs and TUs.
12559 However, for each CU + set of TUs that came from the same original DWO
12560 file, we can combine them back into a virtual DWO file to save space
12561 (fewer struct dwo_file objects to allocate). Remember that for really
12562 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
12563
791afaa2
TT
12564 std::string virtual_dwo_name =
12565 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld",
12566 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
12567 (long) (sections.line_size ? sections.line_offset : 0),
12568 (long) (sections.loc_size ? sections.loc_offset : 0),
12569 (long) (sections.str_offsets_size
12570 ? sections.str_offsets_offset : 0));
73869dc2 12571 /* Can we use an existing virtual DWO file? */
ed2dc618
SM
12572 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
12573 virtual_dwo_name.c_str (),
12574 comp_dir);
73869dc2
DE
12575 /* Create one if necessary. */
12576 if (*dwo_file_slot == NULL)
12577 {
b4f54984 12578 if (dwarf_read_debug)
73869dc2
DE
12579 {
12580 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
791afaa2 12581 virtual_dwo_name.c_str ());
73869dc2
DE
12582 }
12583 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
224c3ddb
SM
12584 dwo_file->dwo_name
12585 = (const char *) obstack_copy0 (&objfile->objfile_obstack,
791afaa2
TT
12586 virtual_dwo_name.c_str (),
12587 virtual_dwo_name.size ());
73869dc2
DE
12588 dwo_file->comp_dir = comp_dir;
12589 dwo_file->sections.abbrev =
ed2dc618 12590 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.abbrev,
73869dc2
DE
12591 sections.abbrev_offset, sections.abbrev_size);
12592 dwo_file->sections.line =
ed2dc618 12593 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.line,
73869dc2
DE
12594 sections.line_offset, sections.line_size);
12595 dwo_file->sections.loc =
ed2dc618 12596 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.loc,
73869dc2
DE
12597 sections.loc_offset, sections.loc_size);
12598 dwo_file->sections.macinfo =
ed2dc618 12599 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.macinfo,
73869dc2
DE
12600 sections.macinfo_offset, sections.macinfo_size);
12601 dwo_file->sections.macro =
ed2dc618 12602 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.macro,
73869dc2
DE
12603 sections.macro_offset, sections.macro_size);
12604 dwo_file->sections.str_offsets =
ed2dc618
SM
12605 create_dwp_v2_section (dwarf2_per_objfile,
12606 &dwp_file->sections.str_offsets,
73869dc2
DE
12607 sections.str_offsets_offset,
12608 sections.str_offsets_size);
12609 /* The "str" section is global to the entire DWP file. */
12610 dwo_file->sections.str = dwp_file->sections.str;
12611 /* The info or types section is assigned below to dwo_unit,
12612 there's no need to record it in dwo_file.
12613 Also, we can't simply record type sections in dwo_file because
12614 we record a pointer into the vector in dwo_unit. As we collect more
12615 types we'll grow the vector and eventually have to reallocate space
12616 for it, invalidating all copies of pointers into the previous
12617 contents. */
12618 *dwo_file_slot = dwo_file;
12619 }
12620 else
12621 {
b4f54984 12622 if (dwarf_read_debug)
73869dc2
DE
12623 {
12624 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
791afaa2 12625 virtual_dwo_name.c_str ());
73869dc2 12626 }
9a3c8263 12627 dwo_file = (struct dwo_file *) *dwo_file_slot;
73869dc2 12628 }
73869dc2
DE
12629
12630 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
12631 dwo_unit->dwo_file = dwo_file;
12632 dwo_unit->signature = signature;
8d749320
SM
12633 dwo_unit->section =
12634 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
ed2dc618
SM
12635 *dwo_unit->section = create_dwp_v2_section (dwarf2_per_objfile,
12636 is_debug_types
73869dc2
DE
12637 ? &dwp_file->sections.types
12638 : &dwp_file->sections.info,
12639 sections.info_or_types_offset,
12640 sections.info_or_types_size);
12641 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
12642
12643 return dwo_unit;
12644}
12645
57d63ce2
DE
12646/* Lookup the DWO unit with SIGNATURE in DWP_FILE.
12647 Returns NULL if the signature isn't found. */
80626a55
DE
12648
12649static struct dwo_unit *
ed2dc618
SM
12650lookup_dwo_unit_in_dwp (struct dwarf2_per_objfile *dwarf2_per_objfile,
12651 struct dwp_file *dwp_file, const char *comp_dir,
57d63ce2 12652 ULONGEST signature, int is_debug_types)
80626a55 12653{
57d63ce2
DE
12654 const struct dwp_hash_table *dwp_htab =
12655 is_debug_types ? dwp_file->tus : dwp_file->cus;
80626a55 12656 bfd *dbfd = dwp_file->dbfd;
57d63ce2 12657 uint32_t mask = dwp_htab->nr_slots - 1;
80626a55
DE
12658 uint32_t hash = signature & mask;
12659 uint32_t hash2 = ((signature >> 32) & mask) | 1;
12660 unsigned int i;
12661 void **slot;
870f88f7 12662 struct dwo_unit find_dwo_cu;
80626a55
DE
12663
12664 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
12665 find_dwo_cu.signature = signature;
19ac8c2e
DE
12666 slot = htab_find_slot (is_debug_types
12667 ? dwp_file->loaded_tus
12668 : dwp_file->loaded_cus,
12669 &find_dwo_cu, INSERT);
80626a55
DE
12670
12671 if (*slot != NULL)
9a3c8263 12672 return (struct dwo_unit *) *slot;
80626a55
DE
12673
12674 /* Use a for loop so that we don't loop forever on bad debug info. */
57d63ce2 12675 for (i = 0; i < dwp_htab->nr_slots; ++i)
80626a55
DE
12676 {
12677 ULONGEST signature_in_table;
12678
12679 signature_in_table =
57d63ce2 12680 read_8_bytes (dbfd, dwp_htab->hash_table + hash * sizeof (uint64_t));
80626a55
DE
12681 if (signature_in_table == signature)
12682 {
57d63ce2
DE
12683 uint32_t unit_index =
12684 read_4_bytes (dbfd,
12685 dwp_htab->unit_table + hash * sizeof (uint32_t));
80626a55 12686
73869dc2
DE
12687 if (dwp_file->version == 1)
12688 {
ed2dc618
SM
12689 *slot = create_dwo_unit_in_dwp_v1 (dwarf2_per_objfile,
12690 dwp_file, unit_index,
73869dc2
DE
12691 comp_dir, signature,
12692 is_debug_types);
12693 }
12694 else
12695 {
ed2dc618
SM
12696 *slot = create_dwo_unit_in_dwp_v2 (dwarf2_per_objfile,
12697 dwp_file, unit_index,
73869dc2
DE
12698 comp_dir, signature,
12699 is_debug_types);
12700 }
9a3c8263 12701 return (struct dwo_unit *) *slot;
80626a55
DE
12702 }
12703 if (signature_in_table == 0)
12704 return NULL;
12705 hash = (hash + hash2) & mask;
12706 }
12707
12708 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
12709 " [in module %s]"),
12710 dwp_file->name);
12711}
12712
ab5088bf 12713/* Subroutine of open_dwo_file,open_dwp_file to simplify them.
3019eac3
DE
12714 Open the file specified by FILE_NAME and hand it off to BFD for
12715 preliminary analysis. Return a newly initialized bfd *, which
12716 includes a canonicalized copy of FILE_NAME.
80626a55 12717 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
6ac97d4c
DE
12718 SEARCH_CWD is true if the current directory is to be searched.
12719 It will be searched before debug-file-directory.
13aaf454
DE
12720 If successful, the file is added to the bfd include table of the
12721 objfile's bfd (see gdb_bfd_record_inclusion).
6ac97d4c 12722 If unable to find/open the file, return NULL.
3019eac3
DE
12723 NOTE: This function is derived from symfile_bfd_open. */
12724
192b62ce 12725static gdb_bfd_ref_ptr
ed2dc618
SM
12726try_open_dwop_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12727 const char *file_name, int is_dwp, int search_cwd)
3019eac3 12728{
24b9144d 12729 int desc;
9c02c129
DE
12730 /* Blech. OPF_TRY_CWD_FIRST also disables searching the path list if
12731 FILE_NAME contains a '/'. So we can't use it. Instead prepend "."
12732 to debug_file_directory. */
e0cc99a6 12733 const char *search_path;
9c02c129
DE
12734 static const char dirname_separator_string[] = { DIRNAME_SEPARATOR, '\0' };
12735
e0cc99a6 12736 gdb::unique_xmalloc_ptr<char> search_path_holder;
6ac97d4c
DE
12737 if (search_cwd)
12738 {
12739 if (*debug_file_directory != '\0')
e0cc99a6
TT
12740 {
12741 search_path_holder.reset (concat (".", dirname_separator_string,
12742 debug_file_directory,
12743 (char *) NULL));
12744 search_path = search_path_holder.get ();
12745 }
6ac97d4c 12746 else
e0cc99a6 12747 search_path = ".";
6ac97d4c 12748 }
9c02c129 12749 else
e0cc99a6 12750 search_path = debug_file_directory;
3019eac3 12751
24b9144d 12752 openp_flags flags = OPF_RETURN_REALPATH;
80626a55
DE
12753 if (is_dwp)
12754 flags |= OPF_SEARCH_IN_PATH;
e0cc99a6
TT
12755
12756 gdb::unique_xmalloc_ptr<char> absolute_name;
9c02c129 12757 desc = openp (search_path, flags, file_name,
3019eac3
DE
12758 O_RDONLY | O_BINARY, &absolute_name);
12759 if (desc < 0)
12760 return NULL;
12761
e0cc99a6
TT
12762 gdb_bfd_ref_ptr sym_bfd (gdb_bfd_open (absolute_name.get (),
12763 gnutarget, desc));
9c02c129
DE
12764 if (sym_bfd == NULL)
12765 return NULL;
192b62ce 12766 bfd_set_cacheable (sym_bfd.get (), 1);
3019eac3 12767
192b62ce
TT
12768 if (!bfd_check_format (sym_bfd.get (), bfd_object))
12769 return NULL;
3019eac3 12770
13aaf454
DE
12771 /* Success. Record the bfd as having been included by the objfile's bfd.
12772 This is important because things like demangled_names_hash lives in the
12773 objfile's per_bfd space and may have references to things like symbol
12774 names that live in the DWO/DWP file's per_bfd space. PR 16426. */
192b62ce 12775 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd, sym_bfd.get ());
13aaf454 12776
3019eac3
DE
12777 return sym_bfd;
12778}
12779
ab5088bf 12780/* Try to open DWO file FILE_NAME.
3019eac3
DE
12781 COMP_DIR is the DW_AT_comp_dir attribute.
12782 The result is the bfd handle of the file.
12783 If there is a problem finding or opening the file, return NULL.
12784 Upon success, the canonicalized path of the file is stored in the bfd,
12785 same as symfile_bfd_open. */
12786
192b62ce 12787static gdb_bfd_ref_ptr
ed2dc618
SM
12788open_dwo_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12789 const char *file_name, const char *comp_dir)
3019eac3 12790{
80626a55 12791 if (IS_ABSOLUTE_PATH (file_name))
ed2dc618
SM
12792 return try_open_dwop_file (dwarf2_per_objfile, file_name,
12793 0 /*is_dwp*/, 0 /*search_cwd*/);
3019eac3
DE
12794
12795 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
12796
12797 if (comp_dir != NULL)
12798 {
b36cec19
PA
12799 char *path_to_try = concat (comp_dir, SLASH_STRING,
12800 file_name, (char *) NULL);
3019eac3
DE
12801
12802 /* NOTE: If comp_dir is a relative path, this will also try the
12803 search path, which seems useful. */
ed2dc618
SM
12804 gdb_bfd_ref_ptr abfd (try_open_dwop_file (dwarf2_per_objfile,
12805 path_to_try,
12806 0 /*is_dwp*/,
192b62ce 12807 1 /*search_cwd*/));
3019eac3
DE
12808 xfree (path_to_try);
12809 if (abfd != NULL)
12810 return abfd;
12811 }
12812
12813 /* That didn't work, try debug-file-directory, which, despite its name,
12814 is a list of paths. */
12815
12816 if (*debug_file_directory == '\0')
12817 return NULL;
12818
ed2dc618
SM
12819 return try_open_dwop_file (dwarf2_per_objfile, file_name,
12820 0 /*is_dwp*/, 1 /*search_cwd*/);
3019eac3
DE
12821}
12822
80626a55
DE
12823/* This function is mapped across the sections and remembers the offset and
12824 size of each of the DWO debugging sections we are interested in. */
12825
12826static void
12827dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp, void *dwo_sections_ptr)
12828{
9a3c8263 12829 struct dwo_sections *dwo_sections = (struct dwo_sections *) dwo_sections_ptr;
80626a55
DE
12830 const struct dwop_section_names *names = &dwop_section_names;
12831
12832 if (section_is_p (sectp->name, &names->abbrev_dwo))
12833 {
049412e3 12834 dwo_sections->abbrev.s.section = sectp;
80626a55
DE
12835 dwo_sections->abbrev.size = bfd_get_section_size (sectp);
12836 }
12837 else if (section_is_p (sectp->name, &names->info_dwo))
12838 {
049412e3 12839 dwo_sections->info.s.section = sectp;
80626a55
DE
12840 dwo_sections->info.size = bfd_get_section_size (sectp);
12841 }
12842 else if (section_is_p (sectp->name, &names->line_dwo))
12843 {
049412e3 12844 dwo_sections->line.s.section = sectp;
80626a55
DE
12845 dwo_sections->line.size = bfd_get_section_size (sectp);
12846 }
12847 else if (section_is_p (sectp->name, &names->loc_dwo))
12848 {
049412e3 12849 dwo_sections->loc.s.section = sectp;
80626a55
DE
12850 dwo_sections->loc.size = bfd_get_section_size (sectp);
12851 }
12852 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12853 {
049412e3 12854 dwo_sections->macinfo.s.section = sectp;
80626a55
DE
12855 dwo_sections->macinfo.size = bfd_get_section_size (sectp);
12856 }
12857 else if (section_is_p (sectp->name, &names->macro_dwo))
12858 {
049412e3 12859 dwo_sections->macro.s.section = sectp;
80626a55
DE
12860 dwo_sections->macro.size = bfd_get_section_size (sectp);
12861 }
12862 else if (section_is_p (sectp->name, &names->str_dwo))
12863 {
049412e3 12864 dwo_sections->str.s.section = sectp;
80626a55
DE
12865 dwo_sections->str.size = bfd_get_section_size (sectp);
12866 }
12867 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12868 {
049412e3 12869 dwo_sections->str_offsets.s.section = sectp;
80626a55
DE
12870 dwo_sections->str_offsets.size = bfd_get_section_size (sectp);
12871 }
12872 else if (section_is_p (sectp->name, &names->types_dwo))
12873 {
12874 struct dwarf2_section_info type_section;
12875
12876 memset (&type_section, 0, sizeof (type_section));
049412e3 12877 type_section.s.section = sectp;
80626a55
DE
12878 type_section.size = bfd_get_section_size (sectp);
12879 VEC_safe_push (dwarf2_section_info_def, dwo_sections->types,
12880 &type_section);
12881 }
12882}
12883
ab5088bf 12884/* Initialize the use of the DWO file specified by DWO_NAME and referenced
19c3d4c9 12885 by PER_CU. This is for the non-DWP case.
80626a55 12886 The result is NULL if DWO_NAME can't be found. */
3019eac3
DE
12887
12888static struct dwo_file *
0ac5b59e
DE
12889open_and_init_dwo_file (struct dwarf2_per_cu_data *per_cu,
12890 const char *dwo_name, const char *comp_dir)
3019eac3 12891{
ed2dc618 12892 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
3019eac3 12893 struct objfile *objfile = dwarf2_per_objfile->objfile;
3019eac3 12894
ed2dc618 12895 gdb_bfd_ref_ptr dbfd (open_dwo_file (dwarf2_per_objfile, dwo_name, comp_dir));
80626a55
DE
12896 if (dbfd == NULL)
12897 {
b4f54984 12898 if (dwarf_read_debug)
80626a55
DE
12899 fprintf_unfiltered (gdb_stdlog, "DWO file not found: %s\n", dwo_name);
12900 return NULL;
12901 }
263db9a1
TT
12902
12903 /* We use a unique pointer here, despite the obstack allocation,
12904 because a dwo_file needs some cleanup if it is abandoned. */
12905 dwo_file_up dwo_file (OBSTACK_ZALLOC (&objfile->objfile_obstack,
12906 struct dwo_file));
0ac5b59e
DE
12907 dwo_file->dwo_name = dwo_name;
12908 dwo_file->comp_dir = comp_dir;
192b62ce 12909 dwo_file->dbfd = dbfd.release ();
3019eac3 12910
192b62ce
TT
12911 bfd_map_over_sections (dwo_file->dbfd, dwarf2_locate_dwo_sections,
12912 &dwo_file->sections);
3019eac3 12913
ed2dc618
SM
12914 create_cus_hash_table (dwarf2_per_objfile, *dwo_file, dwo_file->sections.info,
12915 dwo_file->cus);
3019eac3 12916
263db9a1 12917 create_debug_types_hash_table (dwarf2_per_objfile, dwo_file.get (),
ed2dc618 12918 dwo_file->sections.types, dwo_file->tus);
3019eac3 12919
b4f54984 12920 if (dwarf_read_debug)
80626a55
DE
12921 fprintf_unfiltered (gdb_stdlog, "DWO file found: %s\n", dwo_name);
12922
263db9a1 12923 return dwo_file.release ();
3019eac3
DE
12924}
12925
80626a55 12926/* This function is mapped across the sections and remembers the offset and
73869dc2
DE
12927 size of each of the DWP debugging sections common to version 1 and 2 that
12928 we are interested in. */
3019eac3 12929
80626a55 12930static void
73869dc2
DE
12931dwarf2_locate_common_dwp_sections (bfd *abfd, asection *sectp,
12932 void *dwp_file_ptr)
3019eac3 12933{
9a3c8263 12934 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
80626a55
DE
12935 const struct dwop_section_names *names = &dwop_section_names;
12936 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
3019eac3 12937
80626a55 12938 /* Record the ELF section number for later lookup: this is what the
73869dc2 12939 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
80626a55
DE
12940 gdb_assert (elf_section_nr < dwp_file->num_sections);
12941 dwp_file->elf_sections[elf_section_nr] = sectp;
3019eac3 12942
80626a55
DE
12943 /* Look for specific sections that we need. */
12944 if (section_is_p (sectp->name, &names->str_dwo))
12945 {
049412e3 12946 dwp_file->sections.str.s.section = sectp;
80626a55
DE
12947 dwp_file->sections.str.size = bfd_get_section_size (sectp);
12948 }
12949 else if (section_is_p (sectp->name, &names->cu_index))
12950 {
049412e3 12951 dwp_file->sections.cu_index.s.section = sectp;
80626a55
DE
12952 dwp_file->sections.cu_index.size = bfd_get_section_size (sectp);
12953 }
12954 else if (section_is_p (sectp->name, &names->tu_index))
12955 {
049412e3 12956 dwp_file->sections.tu_index.s.section = sectp;
80626a55
DE
12957 dwp_file->sections.tu_index.size = bfd_get_section_size (sectp);
12958 }
12959}
3019eac3 12960
73869dc2
DE
12961/* This function is mapped across the sections and remembers the offset and
12962 size of each of the DWP version 2 debugging sections that we are interested
12963 in. This is split into a separate function because we don't know if we
12964 have version 1 or 2 until we parse the cu_index/tu_index sections. */
12965
12966static void
12967dwarf2_locate_v2_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
12968{
9a3c8263 12969 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
73869dc2
DE
12970 const struct dwop_section_names *names = &dwop_section_names;
12971 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
12972
12973 /* Record the ELF section number for later lookup: this is what the
12974 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
12975 gdb_assert (elf_section_nr < dwp_file->num_sections);
12976 dwp_file->elf_sections[elf_section_nr] = sectp;
12977
12978 /* Look for specific sections that we need. */
12979 if (section_is_p (sectp->name, &names->abbrev_dwo))
12980 {
049412e3 12981 dwp_file->sections.abbrev.s.section = sectp;
73869dc2
DE
12982 dwp_file->sections.abbrev.size = bfd_get_section_size (sectp);
12983 }
12984 else if (section_is_p (sectp->name, &names->info_dwo))
12985 {
049412e3 12986 dwp_file->sections.info.s.section = sectp;
73869dc2
DE
12987 dwp_file->sections.info.size = bfd_get_section_size (sectp);
12988 }
12989 else if (section_is_p (sectp->name, &names->line_dwo))
12990 {
049412e3 12991 dwp_file->sections.line.s.section = sectp;
73869dc2
DE
12992 dwp_file->sections.line.size = bfd_get_section_size (sectp);
12993 }
12994 else if (section_is_p (sectp->name, &names->loc_dwo))
12995 {
049412e3 12996 dwp_file->sections.loc.s.section = sectp;
73869dc2
DE
12997 dwp_file->sections.loc.size = bfd_get_section_size (sectp);
12998 }
12999 else if (section_is_p (sectp->name, &names->macinfo_dwo))
13000 {
049412e3 13001 dwp_file->sections.macinfo.s.section = sectp;
73869dc2
DE
13002 dwp_file->sections.macinfo.size = bfd_get_section_size (sectp);
13003 }
13004 else if (section_is_p (sectp->name, &names->macro_dwo))
13005 {
049412e3 13006 dwp_file->sections.macro.s.section = sectp;
73869dc2
DE
13007 dwp_file->sections.macro.size = bfd_get_section_size (sectp);
13008 }
13009 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
13010 {
049412e3 13011 dwp_file->sections.str_offsets.s.section = sectp;
73869dc2
DE
13012 dwp_file->sections.str_offsets.size = bfd_get_section_size (sectp);
13013 }
13014 else if (section_is_p (sectp->name, &names->types_dwo))
13015 {
049412e3 13016 dwp_file->sections.types.s.section = sectp;
73869dc2
DE
13017 dwp_file->sections.types.size = bfd_get_section_size (sectp);
13018 }
13019}
13020
80626a55 13021/* Hash function for dwp_file loaded CUs/TUs. */
3019eac3 13022
80626a55
DE
13023static hashval_t
13024hash_dwp_loaded_cutus (const void *item)
13025{
9a3c8263 13026 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
3019eac3 13027
80626a55
DE
13028 /* This drops the top 32 bits of the signature, but is ok for a hash. */
13029 return dwo_unit->signature;
3019eac3
DE
13030}
13031
80626a55 13032/* Equality function for dwp_file loaded CUs/TUs. */
3019eac3 13033
80626a55
DE
13034static int
13035eq_dwp_loaded_cutus (const void *a, const void *b)
3019eac3 13036{
9a3c8263
SM
13037 const struct dwo_unit *dua = (const struct dwo_unit *) a;
13038 const struct dwo_unit *dub = (const struct dwo_unit *) b;
3019eac3 13039
80626a55
DE
13040 return dua->signature == dub->signature;
13041}
3019eac3 13042
80626a55 13043/* Allocate a hash table for dwp_file loaded CUs/TUs. */
3019eac3 13044
80626a55
DE
13045static htab_t
13046allocate_dwp_loaded_cutus_table (struct objfile *objfile)
13047{
13048 return htab_create_alloc_ex (3,
13049 hash_dwp_loaded_cutus,
13050 eq_dwp_loaded_cutus,
13051 NULL,
13052 &objfile->objfile_obstack,
13053 hashtab_obstack_allocate,
13054 dummy_obstack_deallocate);
13055}
3019eac3 13056
ab5088bf
DE
13057/* Try to open DWP file FILE_NAME.
13058 The result is the bfd handle of the file.
13059 If there is a problem finding or opening the file, return NULL.
13060 Upon success, the canonicalized path of the file is stored in the bfd,
13061 same as symfile_bfd_open. */
13062
192b62ce 13063static gdb_bfd_ref_ptr
ed2dc618
SM
13064open_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
13065 const char *file_name)
ab5088bf 13066{
ed2dc618
SM
13067 gdb_bfd_ref_ptr abfd (try_open_dwop_file (dwarf2_per_objfile, file_name,
13068 1 /*is_dwp*/,
192b62ce 13069 1 /*search_cwd*/));
6ac97d4c
DE
13070 if (abfd != NULL)
13071 return abfd;
13072
13073 /* Work around upstream bug 15652.
13074 http://sourceware.org/bugzilla/show_bug.cgi?id=15652
13075 [Whether that's a "bug" is debatable, but it is getting in our way.]
13076 We have no real idea where the dwp file is, because gdb's realpath-ing
13077 of the executable's path may have discarded the needed info.
13078 [IWBN if the dwp file name was recorded in the executable, akin to
13079 .gnu_debuglink, but that doesn't exist yet.]
13080 Strip the directory from FILE_NAME and search again. */
13081 if (*debug_file_directory != '\0')
13082 {
13083 /* Don't implicitly search the current directory here.
13084 If the user wants to search "." to handle this case,
13085 it must be added to debug-file-directory. */
ed2dc618
SM
13086 return try_open_dwop_file (dwarf2_per_objfile,
13087 lbasename (file_name), 1 /*is_dwp*/,
6ac97d4c
DE
13088 0 /*search_cwd*/);
13089 }
13090
13091 return NULL;
ab5088bf
DE
13092}
13093
80626a55
DE
13094/* Initialize the use of the DWP file for the current objfile.
13095 By convention the name of the DWP file is ${objfile}.dwp.
13096 The result is NULL if it can't be found. */
a766d390 13097
80626a55 13098static struct dwp_file *
ed2dc618 13099open_and_init_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
80626a55
DE
13100{
13101 struct objfile *objfile = dwarf2_per_objfile->objfile;
13102 struct dwp_file *dwp_file;
80626a55 13103
82bf32bc
JK
13104 /* Try to find first .dwp for the binary file before any symbolic links
13105 resolving. */
6c447423
DE
13106
13107 /* If the objfile is a debug file, find the name of the real binary
13108 file and get the name of dwp file from there. */
d721ba37 13109 std::string dwp_name;
6c447423
DE
13110 if (objfile->separate_debug_objfile_backlink != NULL)
13111 {
13112 struct objfile *backlink = objfile->separate_debug_objfile_backlink;
13113 const char *backlink_basename = lbasename (backlink->original_name);
6c447423 13114
d721ba37 13115 dwp_name = ldirname (objfile->original_name) + SLASH_STRING + backlink_basename;
6c447423
DE
13116 }
13117 else
d721ba37
PA
13118 dwp_name = objfile->original_name;
13119
13120 dwp_name += ".dwp";
80626a55 13121
ed2dc618 13122 gdb_bfd_ref_ptr dbfd (open_dwp_file (dwarf2_per_objfile, dwp_name.c_str ()));
82bf32bc
JK
13123 if (dbfd == NULL
13124 && strcmp (objfile->original_name, objfile_name (objfile)) != 0)
13125 {
13126 /* Try to find .dwp for the binary file after gdb_realpath resolving. */
d721ba37
PA
13127 dwp_name = objfile_name (objfile);
13128 dwp_name += ".dwp";
ed2dc618 13129 dbfd = open_dwp_file (dwarf2_per_objfile, dwp_name.c_str ());
82bf32bc
JK
13130 }
13131
80626a55
DE
13132 if (dbfd == NULL)
13133 {
b4f54984 13134 if (dwarf_read_debug)
d721ba37 13135 fprintf_unfiltered (gdb_stdlog, "DWP file not found: %s\n", dwp_name.c_str ());
80626a55 13136 return NULL;
3019eac3 13137 }
80626a55 13138 dwp_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_file);
192b62ce
TT
13139 dwp_file->name = bfd_get_filename (dbfd.get ());
13140 dwp_file->dbfd = dbfd.release ();
c906108c 13141
80626a55 13142 /* +1: section 0 is unused */
192b62ce 13143 dwp_file->num_sections = bfd_count_sections (dwp_file->dbfd) + 1;
80626a55
DE
13144 dwp_file->elf_sections =
13145 OBSTACK_CALLOC (&objfile->objfile_obstack,
13146 dwp_file->num_sections, asection *);
13147
192b62ce
TT
13148 bfd_map_over_sections (dwp_file->dbfd, dwarf2_locate_common_dwp_sections,
13149 dwp_file);
80626a55 13150
ed2dc618 13151 dwp_file->cus = create_dwp_hash_table (dwarf2_per_objfile, dwp_file, 0);
80626a55 13152
ed2dc618 13153 dwp_file->tus = create_dwp_hash_table (dwarf2_per_objfile, dwp_file, 1);
80626a55 13154
73869dc2 13155 /* The DWP file version is stored in the hash table. Oh well. */
08302ed2
DE
13156 if (dwp_file->cus && dwp_file->tus
13157 && dwp_file->cus->version != dwp_file->tus->version)
73869dc2
DE
13158 {
13159 /* Technically speaking, we should try to limp along, but this is
fbcbc3fd 13160 pretty bizarre. We use pulongest here because that's the established
4d65956b 13161 portability solution (e.g, we cannot use %u for uint32_t). */
fbcbc3fd
DE
13162 error (_("Dwarf Error: DWP file CU version %s doesn't match"
13163 " TU version %s [in DWP file %s]"),
13164 pulongest (dwp_file->cus->version),
d721ba37 13165 pulongest (dwp_file->tus->version), dwp_name.c_str ());
73869dc2 13166 }
08302ed2
DE
13167
13168 if (dwp_file->cus)
13169 dwp_file->version = dwp_file->cus->version;
13170 else if (dwp_file->tus)
13171 dwp_file->version = dwp_file->tus->version;
13172 else
13173 dwp_file->version = 2;
73869dc2
DE
13174
13175 if (dwp_file->version == 2)
192b62ce
TT
13176 bfd_map_over_sections (dwp_file->dbfd, dwarf2_locate_v2_dwp_sections,
13177 dwp_file);
73869dc2 13178
19ac8c2e
DE
13179 dwp_file->loaded_cus = allocate_dwp_loaded_cutus_table (objfile);
13180 dwp_file->loaded_tus = allocate_dwp_loaded_cutus_table (objfile);
80626a55 13181
b4f54984 13182 if (dwarf_read_debug)
80626a55
DE
13183 {
13184 fprintf_unfiltered (gdb_stdlog, "DWP file found: %s\n", dwp_file->name);
13185 fprintf_unfiltered (gdb_stdlog,
21aa081e
PA
13186 " %s CUs, %s TUs\n",
13187 pulongest (dwp_file->cus ? dwp_file->cus->nr_units : 0),
13188 pulongest (dwp_file->tus ? dwp_file->tus->nr_units : 0));
80626a55
DE
13189 }
13190
13191 return dwp_file;
3019eac3 13192}
c906108c 13193
ab5088bf
DE
13194/* Wrapper around open_and_init_dwp_file, only open it once. */
13195
13196static struct dwp_file *
ed2dc618 13197get_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
ab5088bf
DE
13198{
13199 if (! dwarf2_per_objfile->dwp_checked)
13200 {
ed2dc618
SM
13201 dwarf2_per_objfile->dwp_file
13202 = open_and_init_dwp_file (dwarf2_per_objfile);
ab5088bf
DE
13203 dwarf2_per_objfile->dwp_checked = 1;
13204 }
13205 return dwarf2_per_objfile->dwp_file;
13206}
13207
80626a55
DE
13208/* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
13209 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
13210 or in the DWP file for the objfile, referenced by THIS_UNIT.
3019eac3 13211 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
80626a55
DE
13212 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
13213
13214 This is called, for example, when wanting to read a variable with a
13215 complex location. Therefore we don't want to do file i/o for every call.
13216 Therefore we don't want to look for a DWO file on every call.
13217 Therefore we first see if we've already seen SIGNATURE in a DWP file,
13218 then we check if we've already seen DWO_NAME, and only THEN do we check
13219 for a DWO file.
13220
1c658ad5 13221 The result is a pointer to the dwo_unit object or NULL if we didn't find it
80626a55 13222 (dwo_id mismatch or couldn't find the DWO/DWP file). */
debd256d 13223
3019eac3 13224static struct dwo_unit *
80626a55
DE
13225lookup_dwo_cutu (struct dwarf2_per_cu_data *this_unit,
13226 const char *dwo_name, const char *comp_dir,
13227 ULONGEST signature, int is_debug_types)
3019eac3 13228{
ed2dc618 13229 struct dwarf2_per_objfile *dwarf2_per_objfile = this_unit->dwarf2_per_objfile;
3019eac3 13230 struct objfile *objfile = dwarf2_per_objfile->objfile;
80626a55
DE
13231 const char *kind = is_debug_types ? "TU" : "CU";
13232 void **dwo_file_slot;
3019eac3 13233 struct dwo_file *dwo_file;
80626a55 13234 struct dwp_file *dwp_file;
cb1df416 13235
6a506a2d
DE
13236 /* First see if there's a DWP file.
13237 If we have a DWP file but didn't find the DWO inside it, don't
13238 look for the original DWO file. It makes gdb behave differently
13239 depending on whether one is debugging in the build tree. */
cf2c3c16 13240
ed2dc618 13241 dwp_file = get_dwp_file (dwarf2_per_objfile);
80626a55 13242 if (dwp_file != NULL)
cf2c3c16 13243 {
80626a55
DE
13244 const struct dwp_hash_table *dwp_htab =
13245 is_debug_types ? dwp_file->tus : dwp_file->cus;
13246
13247 if (dwp_htab != NULL)
13248 {
13249 struct dwo_unit *dwo_cutu =
ed2dc618 13250 lookup_dwo_unit_in_dwp (dwarf2_per_objfile, dwp_file, comp_dir,
57d63ce2 13251 signature, is_debug_types);
80626a55
DE
13252
13253 if (dwo_cutu != NULL)
13254 {
b4f54984 13255 if (dwarf_read_debug)
80626a55
DE
13256 {
13257 fprintf_unfiltered (gdb_stdlog,
13258 "Virtual DWO %s %s found: @%s\n",
13259 kind, hex_string (signature),
13260 host_address_to_string (dwo_cutu));
13261 }
13262 return dwo_cutu;
13263 }
13264 }
13265 }
6a506a2d 13266 else
80626a55 13267 {
6a506a2d 13268 /* No DWP file, look for the DWO file. */
80626a55 13269
ed2dc618
SM
13270 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
13271 dwo_name, comp_dir);
6a506a2d 13272 if (*dwo_file_slot == NULL)
80626a55 13273 {
6a506a2d
DE
13274 /* Read in the file and build a table of the CUs/TUs it contains. */
13275 *dwo_file_slot = open_and_init_dwo_file (this_unit, dwo_name, comp_dir);
19c3d4c9 13276 }
6a506a2d 13277 /* NOTE: This will be NULL if unable to open the file. */
9a3c8263 13278 dwo_file = (struct dwo_file *) *dwo_file_slot;
3019eac3 13279
6a506a2d 13280 if (dwo_file != NULL)
19c3d4c9 13281 {
6a506a2d
DE
13282 struct dwo_unit *dwo_cutu = NULL;
13283
13284 if (is_debug_types && dwo_file->tus)
13285 {
13286 struct dwo_unit find_dwo_cutu;
13287
13288 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
13289 find_dwo_cutu.signature = signature;
9a3c8263
SM
13290 dwo_cutu
13291 = (struct dwo_unit *) htab_find (dwo_file->tus, &find_dwo_cutu);
6a506a2d 13292 }
33c5cd75 13293 else if (!is_debug_types && dwo_file->cus)
80626a55 13294 {
33c5cd75
DB
13295 struct dwo_unit find_dwo_cutu;
13296
13297 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
13298 find_dwo_cutu.signature = signature;
13299 dwo_cutu = (struct dwo_unit *)htab_find (dwo_file->cus,
13300 &find_dwo_cutu);
6a506a2d
DE
13301 }
13302
13303 if (dwo_cutu != NULL)
13304 {
b4f54984 13305 if (dwarf_read_debug)
6a506a2d
DE
13306 {
13307 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) found: @%s\n",
13308 kind, dwo_name, hex_string (signature),
13309 host_address_to_string (dwo_cutu));
13310 }
13311 return dwo_cutu;
80626a55
DE
13312 }
13313 }
2e276125 13314 }
9cdd5dbd 13315
80626a55
DE
13316 /* We didn't find it. This could mean a dwo_id mismatch, or
13317 someone deleted the DWO/DWP file, or the search path isn't set up
13318 correctly to find the file. */
13319
b4f54984 13320 if (dwarf_read_debug)
80626a55
DE
13321 {
13322 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) not found\n",
13323 kind, dwo_name, hex_string (signature));
13324 }
3019eac3 13325
6656a72d
DE
13326 /* This is a warning and not a complaint because it can be caused by
13327 pilot error (e.g., user accidentally deleting the DWO). */
43942612
DE
13328 {
13329 /* Print the name of the DWP file if we looked there, helps the user
13330 better diagnose the problem. */
791afaa2 13331 std::string dwp_text;
43942612
DE
13332
13333 if (dwp_file != NULL)
791afaa2
TT
13334 dwp_text = string_printf (" [in DWP file %s]",
13335 lbasename (dwp_file->name));
43942612 13336
9d8780f0 13337 warning (_("Could not find DWO %s %s(%s)%s referenced by %s at offset %s"
43942612
DE
13338 " [in module %s]"),
13339 kind, dwo_name, hex_string (signature),
791afaa2 13340 dwp_text.c_str (),
43942612 13341 this_unit->is_debug_types ? "TU" : "CU",
9d8780f0 13342 sect_offset_str (this_unit->sect_off), objfile_name (objfile));
43942612 13343 }
3019eac3 13344 return NULL;
5fb290d7
DJ
13345}
13346
80626a55
DE
13347/* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
13348 See lookup_dwo_cutu_unit for details. */
13349
13350static struct dwo_unit *
13351lookup_dwo_comp_unit (struct dwarf2_per_cu_data *this_cu,
13352 const char *dwo_name, const char *comp_dir,
13353 ULONGEST signature)
13354{
13355 return lookup_dwo_cutu (this_cu, dwo_name, comp_dir, signature, 0);
13356}
13357
13358/* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
13359 See lookup_dwo_cutu_unit for details. */
13360
13361static struct dwo_unit *
13362lookup_dwo_type_unit (struct signatured_type *this_tu,
13363 const char *dwo_name, const char *comp_dir)
13364{
13365 return lookup_dwo_cutu (&this_tu->per_cu, dwo_name, comp_dir, this_tu->signature, 1);
13366}
13367
89e63ee4
DE
13368/* Traversal function for queue_and_load_all_dwo_tus. */
13369
13370static int
13371queue_and_load_dwo_tu (void **slot, void *info)
13372{
13373 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
13374 struct dwarf2_per_cu_data *per_cu = (struct dwarf2_per_cu_data *) info;
13375 ULONGEST signature = dwo_unit->signature;
13376 struct signatured_type *sig_type =
13377 lookup_dwo_signatured_type (per_cu->cu, signature);
13378
13379 if (sig_type != NULL)
13380 {
13381 struct dwarf2_per_cu_data *sig_cu = &sig_type->per_cu;
13382
13383 /* We pass NULL for DEPENDENT_CU because we don't yet know if there's
13384 a real dependency of PER_CU on SIG_TYPE. That is detected later
13385 while processing PER_CU. */
13386 if (maybe_queue_comp_unit (NULL, sig_cu, per_cu->cu->language))
13387 load_full_type_unit (sig_cu);
13388 VEC_safe_push (dwarf2_per_cu_ptr, per_cu->imported_symtabs, sig_cu);
13389 }
13390
13391 return 1;
13392}
13393
13394/* Queue all TUs contained in the DWO of PER_CU to be read in.
13395 The DWO may have the only definition of the type, though it may not be
13396 referenced anywhere in PER_CU. Thus we have to load *all* its TUs.
13397 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
13398
13399static void
13400queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *per_cu)
13401{
13402 struct dwo_unit *dwo_unit;
13403 struct dwo_file *dwo_file;
13404
13405 gdb_assert (!per_cu->is_debug_types);
ed2dc618 13406 gdb_assert (get_dwp_file (per_cu->dwarf2_per_objfile) == NULL);
89e63ee4
DE
13407 gdb_assert (per_cu->cu != NULL);
13408
13409 dwo_unit = per_cu->cu->dwo_unit;
13410 gdb_assert (dwo_unit != NULL);
13411
13412 dwo_file = dwo_unit->dwo_file;
13413 if (dwo_file->tus != NULL)
13414 htab_traverse_noresize (dwo_file->tus, queue_and_load_dwo_tu, per_cu);
13415}
13416
3019eac3 13417/* Free all resources associated with DWO_FILE.
5dafb3d1 13418 Close the DWO file and munmap the sections. */
348e048f
DE
13419
13420static void
5dafb3d1 13421free_dwo_file (struct dwo_file *dwo_file)
348e048f 13422{
5c6fa7ab 13423 /* Note: dbfd is NULL for virtual DWO files. */
80626a55 13424 gdb_bfd_unref (dwo_file->dbfd);
348e048f 13425
3019eac3
DE
13426 VEC_free (dwarf2_section_info_def, dwo_file->sections.types);
13427}
348e048f 13428
3019eac3 13429/* Traversal function for free_dwo_files. */
2ab95328 13430
3019eac3
DE
13431static int
13432free_dwo_file_from_slot (void **slot, void *info)
13433{
13434 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
348e048f 13435
5dafb3d1 13436 free_dwo_file (dwo_file);
348e048f 13437
3019eac3
DE
13438 return 1;
13439}
348e048f 13440
3019eac3 13441/* Free all resources associated with DWO_FILES. */
348e048f 13442
3019eac3
DE
13443static void
13444free_dwo_files (htab_t dwo_files, struct objfile *objfile)
13445{
13446 htab_traverse_noresize (dwo_files, free_dwo_file_from_slot, objfile);
348e048f 13447}
3019eac3
DE
13448\f
13449/* Read in various DIEs. */
348e048f 13450
d389af10 13451/* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
3e43a32a
MS
13452 Inherit only the children of the DW_AT_abstract_origin DIE not being
13453 already referenced by DW_AT_abstract_origin from the children of the
13454 current DIE. */
d389af10
JK
13455
13456static void
13457inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
13458{
13459 struct die_info *child_die;
791afaa2 13460 sect_offset *offsetp;
d389af10
JK
13461 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
13462 struct die_info *origin_die;
13463 /* Iterator of the ORIGIN_DIE children. */
13464 struct die_info *origin_child_die;
d389af10 13465 struct attribute *attr;
cd02d79d
PA
13466 struct dwarf2_cu *origin_cu;
13467 struct pending **origin_previous_list_in_scope;
d389af10
JK
13468
13469 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13470 if (!attr)
13471 return;
13472
cd02d79d
PA
13473 /* Note that following die references may follow to a die in a
13474 different cu. */
13475
13476 origin_cu = cu;
13477 origin_die = follow_die_ref (die, attr, &origin_cu);
13478
13479 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
13480 symbols in. */
13481 origin_previous_list_in_scope = origin_cu->list_in_scope;
13482 origin_cu->list_in_scope = cu->list_in_scope;
13483
edb3359d
DJ
13484 if (die->tag != origin_die->tag
13485 && !(die->tag == DW_TAG_inlined_subroutine
13486 && origin_die->tag == DW_TAG_subprogram))
d389af10 13487 complaint (&symfile_complaints,
9d8780f0
SM
13488 _("DIE %s and its abstract origin %s have different tags"),
13489 sect_offset_str (die->sect_off),
13490 sect_offset_str (origin_die->sect_off));
d389af10 13491
791afaa2 13492 std::vector<sect_offset> offsets;
d389af10 13493
3ea89b92
PMR
13494 for (child_die = die->child;
13495 child_die && child_die->tag;
13496 child_die = sibling_die (child_die))
13497 {
13498 struct die_info *child_origin_die;
13499 struct dwarf2_cu *child_origin_cu;
13500
13501 /* We are trying to process concrete instance entries:
216f72a1 13502 DW_TAG_call_site DIEs indeed have a DW_AT_abstract_origin tag, but
3ea89b92
PMR
13503 it's not relevant to our analysis here. i.e. detecting DIEs that are
13504 present in the abstract instance but not referenced in the concrete
13505 one. */
216f72a1
JK
13506 if (child_die->tag == DW_TAG_call_site
13507 || child_die->tag == DW_TAG_GNU_call_site)
3ea89b92
PMR
13508 continue;
13509
c38f313d
DJ
13510 /* For each CHILD_DIE, find the corresponding child of
13511 ORIGIN_DIE. If there is more than one layer of
13512 DW_AT_abstract_origin, follow them all; there shouldn't be,
13513 but GCC versions at least through 4.4 generate this (GCC PR
13514 40573). */
3ea89b92
PMR
13515 child_origin_die = child_die;
13516 child_origin_cu = cu;
c38f313d
DJ
13517 while (1)
13518 {
cd02d79d
PA
13519 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
13520 child_origin_cu);
c38f313d
DJ
13521 if (attr == NULL)
13522 break;
cd02d79d
PA
13523 child_origin_die = follow_die_ref (child_origin_die, attr,
13524 &child_origin_cu);
c38f313d
DJ
13525 }
13526
d389af10
JK
13527 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
13528 counterpart may exist. */
c38f313d 13529 if (child_origin_die != child_die)
d389af10 13530 {
edb3359d
DJ
13531 if (child_die->tag != child_origin_die->tag
13532 && !(child_die->tag == DW_TAG_inlined_subroutine
13533 && child_origin_die->tag == DW_TAG_subprogram))
d389af10 13534 complaint (&symfile_complaints,
9d8780f0 13535 _("Child DIE %s and its abstract origin %s have "
9c541725 13536 "different tags"),
9d8780f0
SM
13537 sect_offset_str (child_die->sect_off),
13538 sect_offset_str (child_origin_die->sect_off));
c38f313d
DJ
13539 if (child_origin_die->parent != origin_die)
13540 complaint (&symfile_complaints,
9d8780f0 13541 _("Child DIE %s and its abstract origin %s have "
9c541725 13542 "different parents"),
9d8780f0
SM
13543 sect_offset_str (child_die->sect_off),
13544 sect_offset_str (child_origin_die->sect_off));
c38f313d 13545 else
791afaa2 13546 offsets.push_back (child_origin_die->sect_off);
d389af10 13547 }
d389af10 13548 }
791afaa2
TT
13549 std::sort (offsets.begin (), offsets.end ());
13550 sect_offset *offsets_end = offsets.data () + offsets.size ();
13551 for (offsetp = offsets.data () + 1; offsetp < offsets_end; offsetp++)
9c541725 13552 if (offsetp[-1] == *offsetp)
3e43a32a 13553 complaint (&symfile_complaints,
9d8780f0
SM
13554 _("Multiple children of DIE %s refer "
13555 "to DIE %s as their abstract origin"),
13556 sect_offset_str (die->sect_off), sect_offset_str (*offsetp));
d389af10 13557
791afaa2 13558 offsetp = offsets.data ();
d389af10
JK
13559 origin_child_die = origin_die->child;
13560 while (origin_child_die && origin_child_die->tag)
13561 {
13562 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
b64f50a1 13563 while (offsetp < offsets_end
9c541725 13564 && *offsetp < origin_child_die->sect_off)
d389af10 13565 offsetp++;
b64f50a1 13566 if (offsetp >= offsets_end
9c541725 13567 || *offsetp > origin_child_die->sect_off)
d389af10 13568 {
adde2bff
DE
13569 /* Found that ORIGIN_CHILD_DIE is really not referenced.
13570 Check whether we're already processing ORIGIN_CHILD_DIE.
13571 This can happen with mutually referenced abstract_origins.
13572 PR 16581. */
13573 if (!origin_child_die->in_process)
13574 process_die (origin_child_die, origin_cu);
d389af10
JK
13575 }
13576 origin_child_die = sibling_die (origin_child_die);
13577 }
cd02d79d 13578 origin_cu->list_in_scope = origin_previous_list_in_scope;
d389af10
JK
13579}
13580
c906108c 13581static void
e7c27a73 13582read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 13583{
518817b3 13584 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
3e29f34a 13585 struct gdbarch *gdbarch = get_objfile_arch (objfile);
fe978cb0 13586 struct context_stack *newobj;
c906108c
SS
13587 CORE_ADDR lowpc;
13588 CORE_ADDR highpc;
13589 struct die_info *child_die;
edb3359d 13590 struct attribute *attr, *call_line, *call_file;
15d034d0 13591 const char *name;
e142c38c 13592 CORE_ADDR baseaddr;
801e3a5b 13593 struct block *block;
edb3359d 13594 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
2f4732b0 13595 std::vector<struct symbol *> template_args;
34eaf542 13596 struct template_symbol *templ_func = NULL;
edb3359d
DJ
13597
13598 if (inlined_func)
13599 {
13600 /* If we do not have call site information, we can't show the
13601 caller of this inlined function. That's too confusing, so
13602 only use the scope for local variables. */
13603 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
13604 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
13605 if (call_line == NULL || call_file == NULL)
13606 {
13607 read_lexical_block_scope (die, cu);
13608 return;
13609 }
13610 }
c906108c 13611
e142c38c
DJ
13612 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
13613
94af9270 13614 name = dwarf2_name (die, cu);
c906108c 13615
e8d05480
JB
13616 /* Ignore functions with missing or empty names. These are actually
13617 illegal according to the DWARF standard. */
13618 if (name == NULL)
13619 {
13620 complaint (&symfile_complaints,
9d8780f0
SM
13621 _("missing name for subprogram DIE at %s"),
13622 sect_offset_str (die->sect_off));
e8d05480
JB
13623 return;
13624 }
13625
13626 /* Ignore functions with missing or invalid low and high pc attributes. */
3a2b436a 13627 if (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL)
e385593e 13628 <= PC_BOUNDS_INVALID)
e8d05480 13629 {
ae4d0c03
PM
13630 attr = dwarf2_attr (die, DW_AT_external, cu);
13631 if (!attr || !DW_UNSND (attr))
13632 complaint (&symfile_complaints,
3e43a32a 13633 _("cannot get low and high bounds "
9d8780f0
SM
13634 "for subprogram DIE at %s"),
13635 sect_offset_str (die->sect_off));
e8d05480
JB
13636 return;
13637 }
c906108c 13638
3e29f34a
MR
13639 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13640 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
c906108c 13641
34eaf542
TT
13642 /* If we have any template arguments, then we must allocate a
13643 different sort of symbol. */
13644 for (child_die = die->child; child_die; child_die = sibling_die (child_die))
13645 {
13646 if (child_die->tag == DW_TAG_template_type_param
13647 || child_die->tag == DW_TAG_template_value_param)
13648 {
e623cf5d 13649 templ_func = allocate_template_symbol (objfile);
cf724bc9 13650 templ_func->subclass = SYMBOL_TEMPLATE;
34eaf542
TT
13651 break;
13652 }
13653 }
13654
fe978cb0 13655 newobj = push_context (0, lowpc);
5e2db402
TT
13656 newobj->name = new_symbol (die, read_type_die (die, cu), cu,
13657 (struct symbol *) templ_func);
4c2df51b 13658
4cecd739
DJ
13659 /* If there is a location expression for DW_AT_frame_base, record
13660 it. */
e142c38c 13661 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
4c2df51b 13662 if (attr)
fe978cb0 13663 dwarf2_symbol_mark_computed (attr, newobj->name, cu, 1);
4c2df51b 13664
63e43d3a
PMR
13665 /* If there is a location for the static link, record it. */
13666 newobj->static_link = NULL;
13667 attr = dwarf2_attr (die, DW_AT_static_link, cu);
13668 if (attr)
13669 {
224c3ddb
SM
13670 newobj->static_link
13671 = XOBNEW (&objfile->objfile_obstack, struct dynamic_prop);
63e43d3a
PMR
13672 attr_to_dynamic_prop (attr, die, cu, newobj->static_link);
13673 }
13674
e142c38c 13675 cu->list_in_scope = &local_symbols;
c906108c 13676
639d11d3 13677 if (die->child != NULL)
c906108c 13678 {
639d11d3 13679 child_die = die->child;
c906108c
SS
13680 while (child_die && child_die->tag)
13681 {
34eaf542
TT
13682 if (child_die->tag == DW_TAG_template_type_param
13683 || child_die->tag == DW_TAG_template_value_param)
13684 {
13685 struct symbol *arg = new_symbol (child_die, NULL, cu);
13686
f1078f66 13687 if (arg != NULL)
2f4732b0 13688 template_args.push_back (arg);
34eaf542
TT
13689 }
13690 else
13691 process_die (child_die, cu);
c906108c
SS
13692 child_die = sibling_die (child_die);
13693 }
13694 }
13695
d389af10
JK
13696 inherit_abstract_dies (die, cu);
13697
4a811a97
UW
13698 /* If we have a DW_AT_specification, we might need to import using
13699 directives from the context of the specification DIE. See the
13700 comment in determine_prefix. */
13701 if (cu->language == language_cplus
13702 && dwarf2_attr (die, DW_AT_specification, cu))
13703 {
13704 struct dwarf2_cu *spec_cu = cu;
13705 struct die_info *spec_die = die_specification (die, &spec_cu);
13706
13707 while (spec_die)
13708 {
13709 child_die = spec_die->child;
13710 while (child_die && child_die->tag)
13711 {
13712 if (child_die->tag == DW_TAG_imported_module)
13713 process_die (child_die, spec_cu);
13714 child_die = sibling_die (child_die);
13715 }
13716
13717 /* In some cases, GCC generates specification DIEs that
13718 themselves contain DW_AT_specification attributes. */
13719 spec_die = die_specification (spec_die, &spec_cu);
13720 }
13721 }
13722
fe978cb0 13723 newobj = pop_context ();
c906108c 13724 /* Make a block for the local symbols within. */
fe978cb0 13725 block = finish_block (newobj->name, &local_symbols, newobj->old_blocks,
63e43d3a 13726 newobj->static_link, lowpc, highpc);
801e3a5b 13727
df8a16a1 13728 /* For C++, set the block's scope. */
45280282
IB
13729 if ((cu->language == language_cplus
13730 || cu->language == language_fortran
c44af4eb
TT
13731 || cu->language == language_d
13732 || cu->language == language_rust)
4d4ec4e5 13733 && cu->processing_has_namespace_info)
195a3f6c
TT
13734 block_set_scope (block, determine_prefix (die, cu),
13735 &objfile->objfile_obstack);
df8a16a1 13736
801e3a5b
JB
13737 /* If we have address ranges, record them. */
13738 dwarf2_record_block_ranges (die, block, baseaddr, cu);
6e70227d 13739
fe978cb0 13740 gdbarch_make_symbol_special (gdbarch, newobj->name, objfile);
3e29f34a 13741
34eaf542 13742 /* Attach template arguments to function. */
2f4732b0 13743 if (!template_args.empty ())
34eaf542
TT
13744 {
13745 gdb_assert (templ_func != NULL);
13746
2f4732b0 13747 templ_func->n_template_arguments = template_args.size ();
34eaf542 13748 templ_func->template_arguments
8d749320
SM
13749 = XOBNEWVEC (&objfile->objfile_obstack, struct symbol *,
13750 templ_func->n_template_arguments);
34eaf542 13751 memcpy (templ_func->template_arguments,
2f4732b0 13752 template_args.data (),
34eaf542 13753 (templ_func->n_template_arguments * sizeof (struct symbol *)));
34eaf542
TT
13754 }
13755
208d8187
JB
13756 /* In C++, we can have functions nested inside functions (e.g., when
13757 a function declares a class that has methods). This means that
13758 when we finish processing a function scope, we may need to go
13759 back to building a containing block's symbol lists. */
fe978cb0 13760 local_symbols = newobj->locals;
22cee43f 13761 local_using_directives = newobj->local_using_directives;
208d8187 13762
921e78cf
JB
13763 /* If we've finished processing a top-level function, subsequent
13764 symbols go in the file symbol list. */
13765 if (outermost_context_p ())
e142c38c 13766 cu->list_in_scope = &file_symbols;
c906108c
SS
13767}
13768
13769/* Process all the DIES contained within a lexical block scope. Start
13770 a new scope, process the dies, and then close the scope. */
13771
13772static void
e7c27a73 13773read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 13774{
518817b3 13775 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
3e29f34a 13776 struct gdbarch *gdbarch = get_objfile_arch (objfile);
fe978cb0 13777 struct context_stack *newobj;
c906108c
SS
13778 CORE_ADDR lowpc, highpc;
13779 struct die_info *child_die;
e142c38c
DJ
13780 CORE_ADDR baseaddr;
13781
13782 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c
SS
13783
13784 /* Ignore blocks with missing or invalid low and high pc attributes. */
af34e669
DJ
13785 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
13786 as multiple lexical blocks? Handling children in a sane way would
6e70227d 13787 be nasty. Might be easier to properly extend generic blocks to
af34e669 13788 describe ranges. */
e385593e
JK
13789 switch (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
13790 {
13791 case PC_BOUNDS_NOT_PRESENT:
13792 /* DW_TAG_lexical_block has no attributes, process its children as if
13793 there was no wrapping by that DW_TAG_lexical_block.
13794 GCC does no longer produces such DWARF since GCC r224161. */
13795 for (child_die = die->child;
13796 child_die != NULL && child_die->tag;
13797 child_die = sibling_die (child_die))
13798 process_die (child_die, cu);
13799 return;
13800 case PC_BOUNDS_INVALID:
13801 return;
13802 }
3e29f34a
MR
13803 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13804 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
c906108c
SS
13805
13806 push_context (0, lowpc);
639d11d3 13807 if (die->child != NULL)
c906108c 13808 {
639d11d3 13809 child_die = die->child;
c906108c
SS
13810 while (child_die && child_die->tag)
13811 {
e7c27a73 13812 process_die (child_die, cu);
c906108c
SS
13813 child_die = sibling_die (child_die);
13814 }
13815 }
3ea89b92 13816 inherit_abstract_dies (die, cu);
fe978cb0 13817 newobj = pop_context ();
c906108c 13818
22cee43f 13819 if (local_symbols != NULL || local_using_directives != NULL)
c906108c 13820 {
801e3a5b 13821 struct block *block
63e43d3a 13822 = finish_block (0, &local_symbols, newobj->old_blocks, NULL,
fe978cb0 13823 newobj->start_addr, highpc);
801e3a5b
JB
13824
13825 /* Note that recording ranges after traversing children, as we
13826 do here, means that recording a parent's ranges entails
13827 walking across all its children's ranges as they appear in
13828 the address map, which is quadratic behavior.
13829
13830 It would be nicer to record the parent's ranges before
13831 traversing its children, simply overriding whatever you find
13832 there. But since we don't even decide whether to create a
13833 block until after we've traversed its children, that's hard
13834 to do. */
13835 dwarf2_record_block_ranges (die, block, baseaddr, cu);
c906108c 13836 }
fe978cb0 13837 local_symbols = newobj->locals;
22cee43f 13838 local_using_directives = newobj->local_using_directives;
c906108c
SS
13839}
13840
216f72a1 13841/* Read in DW_TAG_call_site and insert it to CU->call_site_htab. */
96408a79
SA
13842
13843static void
13844read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
13845{
518817b3 13846 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
96408a79
SA
13847 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13848 CORE_ADDR pc, baseaddr;
13849 struct attribute *attr;
13850 struct call_site *call_site, call_site_local;
13851 void **slot;
13852 int nparams;
13853 struct die_info *child_die;
13854
13855 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
13856
216f72a1
JK
13857 attr = dwarf2_attr (die, DW_AT_call_return_pc, cu);
13858 if (attr == NULL)
13859 {
13860 /* This was a pre-DWARF-5 GNU extension alias
13861 for DW_AT_call_return_pc. */
13862 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
13863 }
96408a79
SA
13864 if (!attr)
13865 {
13866 complaint (&symfile_complaints,
216f72a1 13867 _("missing DW_AT_call_return_pc for DW_TAG_call_site "
9d8780f0
SM
13868 "DIE %s [in module %s]"),
13869 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79
SA
13870 return;
13871 }
31aa7e4e 13872 pc = attr_value_as_address (attr) + baseaddr;
3e29f34a 13873 pc = gdbarch_adjust_dwarf2_addr (gdbarch, pc);
96408a79
SA
13874
13875 if (cu->call_site_htab == NULL)
13876 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
13877 NULL, &objfile->objfile_obstack,
13878 hashtab_obstack_allocate, NULL);
13879 call_site_local.pc = pc;
13880 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
13881 if (*slot != NULL)
13882 {
13883 complaint (&symfile_complaints,
216f72a1 13884 _("Duplicate PC %s for DW_TAG_call_site "
9d8780f0
SM
13885 "DIE %s [in module %s]"),
13886 paddress (gdbarch, pc), sect_offset_str (die->sect_off),
4262abfb 13887 objfile_name (objfile));
96408a79
SA
13888 return;
13889 }
13890
13891 /* Count parameters at the caller. */
13892
13893 nparams = 0;
13894 for (child_die = die->child; child_die && child_die->tag;
13895 child_die = sibling_die (child_die))
13896 {
216f72a1
JK
13897 if (child_die->tag != DW_TAG_call_site_parameter
13898 && child_die->tag != DW_TAG_GNU_call_site_parameter)
96408a79
SA
13899 {
13900 complaint (&symfile_complaints,
216f72a1 13901 _("Tag %d is not DW_TAG_call_site_parameter in "
9d8780f0
SM
13902 "DW_TAG_call_site child DIE %s [in module %s]"),
13903 child_die->tag, sect_offset_str (child_die->sect_off),
4262abfb 13904 objfile_name (objfile));
96408a79
SA
13905 continue;
13906 }
13907
13908 nparams++;
13909 }
13910
224c3ddb
SM
13911 call_site
13912 = ((struct call_site *)
13913 obstack_alloc (&objfile->objfile_obstack,
13914 sizeof (*call_site)
13915 + (sizeof (*call_site->parameter) * (nparams - 1))));
96408a79
SA
13916 *slot = call_site;
13917 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
13918 call_site->pc = pc;
13919
216f72a1
JK
13920 if (dwarf2_flag_true_p (die, DW_AT_call_tail_call, cu)
13921 || dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
96408a79
SA
13922 {
13923 struct die_info *func_die;
13924
13925 /* Skip also over DW_TAG_inlined_subroutine. */
13926 for (func_die = die->parent;
13927 func_die && func_die->tag != DW_TAG_subprogram
13928 && func_die->tag != DW_TAG_subroutine_type;
13929 func_die = func_die->parent);
13930
216f72a1
JK
13931 /* DW_AT_call_all_calls is a superset
13932 of DW_AT_call_all_tail_calls. */
96408a79 13933 if (func_die
216f72a1 13934 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_calls, cu)
96408a79 13935 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
216f72a1 13936 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_tail_calls, cu)
96408a79
SA
13937 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
13938 {
13939 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
13940 not complete. But keep CALL_SITE for look ups via call_site_htab,
13941 both the initial caller containing the real return address PC and
13942 the final callee containing the current PC of a chain of tail
13943 calls do not need to have the tail call list complete. But any
13944 function candidate for a virtual tail call frame searched via
13945 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
13946 determined unambiguously. */
13947 }
13948 else
13949 {
13950 struct type *func_type = NULL;
13951
13952 if (func_die)
13953 func_type = get_die_type (func_die, cu);
13954 if (func_type != NULL)
13955 {
13956 gdb_assert (TYPE_CODE (func_type) == TYPE_CODE_FUNC);
13957
13958 /* Enlist this call site to the function. */
13959 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
13960 TYPE_TAIL_CALL_LIST (func_type) = call_site;
13961 }
13962 else
13963 complaint (&symfile_complaints,
216f72a1 13964 _("Cannot find function owning DW_TAG_call_site "
9d8780f0
SM
13965 "DIE %s [in module %s]"),
13966 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79
SA
13967 }
13968 }
13969
216f72a1
JK
13970 attr = dwarf2_attr (die, DW_AT_call_target, cu);
13971 if (attr == NULL)
13972 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
13973 if (attr == NULL)
13974 attr = dwarf2_attr (die, DW_AT_call_origin, cu);
96408a79 13975 if (attr == NULL)
216f72a1
JK
13976 {
13977 /* This was a pre-DWARF-5 GNU extension alias for DW_AT_call_origin. */
13978 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13979 }
96408a79
SA
13980 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
13981 if (!attr || (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0))
13982 /* Keep NULL DWARF_BLOCK. */;
13983 else if (attr_form_is_block (attr))
13984 {
13985 struct dwarf2_locexpr_baton *dlbaton;
13986
8d749320 13987 dlbaton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
96408a79
SA
13988 dlbaton->data = DW_BLOCK (attr)->data;
13989 dlbaton->size = DW_BLOCK (attr)->size;
13990 dlbaton->per_cu = cu->per_cu;
13991
13992 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
13993 }
7771576e 13994 else if (attr_form_is_ref (attr))
96408a79 13995 {
96408a79
SA
13996 struct dwarf2_cu *target_cu = cu;
13997 struct die_info *target_die;
13998
ac9ec31b 13999 target_die = follow_die_ref (die, attr, &target_cu);
518817b3 14000 gdb_assert (target_cu->per_cu->dwarf2_per_objfile->objfile == objfile);
96408a79
SA
14001 if (die_is_declaration (target_die, target_cu))
14002 {
7d45c7c3 14003 const char *target_physname;
9112db09
JK
14004
14005 /* Prefer the mangled name; otherwise compute the demangled one. */
73b9be8b 14006 target_physname = dw2_linkage_name (target_die, target_cu);
7d45c7c3 14007 if (target_physname == NULL)
9112db09 14008 target_physname = dwarf2_physname (NULL, target_die, target_cu);
96408a79
SA
14009 if (target_physname == NULL)
14010 complaint (&symfile_complaints,
216f72a1 14011 _("DW_AT_call_target target DIE has invalid "
9d8780f0
SM
14012 "physname, for referencing DIE %s [in module %s]"),
14013 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79 14014 else
7d455152 14015 SET_FIELD_PHYSNAME (call_site->target, target_physname);
96408a79
SA
14016 }
14017 else
14018 {
14019 CORE_ADDR lowpc;
14020
14021 /* DW_AT_entry_pc should be preferred. */
3a2b436a 14022 if (dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL)
e385593e 14023 <= PC_BOUNDS_INVALID)
96408a79 14024 complaint (&symfile_complaints,
216f72a1 14025 _("DW_AT_call_target target DIE has invalid "
9d8780f0
SM
14026 "low pc, for referencing DIE %s [in module %s]"),
14027 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79 14028 else
3e29f34a
MR
14029 {
14030 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
14031 SET_FIELD_PHYSADDR (call_site->target, lowpc);
14032 }
96408a79
SA
14033 }
14034 }
14035 else
14036 complaint (&symfile_complaints,
216f72a1 14037 _("DW_TAG_call_site DW_AT_call_target is neither "
9d8780f0
SM
14038 "block nor reference, for DIE %s [in module %s]"),
14039 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79
SA
14040
14041 call_site->per_cu = cu->per_cu;
14042
14043 for (child_die = die->child;
14044 child_die && child_die->tag;
14045 child_die = sibling_die (child_die))
14046 {
96408a79 14047 struct call_site_parameter *parameter;
1788b2d3 14048 struct attribute *loc, *origin;
96408a79 14049
216f72a1
JK
14050 if (child_die->tag != DW_TAG_call_site_parameter
14051 && child_die->tag != DW_TAG_GNU_call_site_parameter)
96408a79
SA
14052 {
14053 /* Already printed the complaint above. */
14054 continue;
14055 }
14056
14057 gdb_assert (call_site->parameter_count < nparams);
14058 parameter = &call_site->parameter[call_site->parameter_count];
14059
1788b2d3
JK
14060 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
14061 specifies DW_TAG_formal_parameter. Value of the data assumed for the
216f72a1 14062 register is contained in DW_AT_call_value. */
96408a79 14063
24c5c679 14064 loc = dwarf2_attr (child_die, DW_AT_location, cu);
216f72a1
JK
14065 origin = dwarf2_attr (child_die, DW_AT_call_parameter, cu);
14066 if (origin == NULL)
14067 {
14068 /* This was a pre-DWARF-5 GNU extension alias
14069 for DW_AT_call_parameter. */
14070 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
14071 }
7771576e 14072 if (loc == NULL && origin != NULL && attr_form_is_ref (origin))
1788b2d3 14073 {
1788b2d3 14074 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
9c541725
PA
14075
14076 sect_offset sect_off
14077 = (sect_offset) dwarf2_get_ref_die_offset (origin);
14078 if (!offset_in_cu_p (&cu->header, sect_off))
d76b7dbc
JK
14079 {
14080 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
14081 binding can be done only inside one CU. Such referenced DIE
14082 therefore cannot be even moved to DW_TAG_partial_unit. */
14083 complaint (&symfile_complaints,
216f72a1 14084 _("DW_AT_call_parameter offset is not in CU for "
9d8780f0
SM
14085 "DW_TAG_call_site child DIE %s [in module %s]"),
14086 sect_offset_str (child_die->sect_off),
9c541725 14087 objfile_name (objfile));
d76b7dbc
JK
14088 continue;
14089 }
9c541725
PA
14090 parameter->u.param_cu_off
14091 = (cu_offset) (sect_off - cu->header.sect_off);
1788b2d3
JK
14092 }
14093 else if (loc == NULL || origin != NULL || !attr_form_is_block (loc))
96408a79
SA
14094 {
14095 complaint (&symfile_complaints,
14096 _("No DW_FORM_block* DW_AT_location for "
9d8780f0
SM
14097 "DW_TAG_call_site child DIE %s [in module %s]"),
14098 sect_offset_str (child_die->sect_off), objfile_name (objfile));
96408a79
SA
14099 continue;
14100 }
24c5c679 14101 else
96408a79 14102 {
24c5c679
JK
14103 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
14104 (DW_BLOCK (loc)->data, &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size]);
14105 if (parameter->u.dwarf_reg != -1)
14106 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
14107 else if (dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (loc)->data,
14108 &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size],
14109 &parameter->u.fb_offset))
14110 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
14111 else
14112 {
14113 complaint (&symfile_complaints,
14114 _("Only single DW_OP_reg or DW_OP_fbreg is supported "
14115 "for DW_FORM_block* DW_AT_location is supported for "
9d8780f0 14116 "DW_TAG_call_site child DIE %s "
24c5c679 14117 "[in module %s]"),
9d8780f0 14118 sect_offset_str (child_die->sect_off),
9c541725 14119 objfile_name (objfile));
24c5c679
JK
14120 continue;
14121 }
96408a79
SA
14122 }
14123
216f72a1
JK
14124 attr = dwarf2_attr (child_die, DW_AT_call_value, cu);
14125 if (attr == NULL)
14126 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
96408a79
SA
14127 if (!attr_form_is_block (attr))
14128 {
14129 complaint (&symfile_complaints,
216f72a1 14130 _("No DW_FORM_block* DW_AT_call_value for "
9d8780f0
SM
14131 "DW_TAG_call_site child DIE %s [in module %s]"),
14132 sect_offset_str (child_die->sect_off),
9c541725 14133 objfile_name (objfile));
96408a79
SA
14134 continue;
14135 }
14136 parameter->value = DW_BLOCK (attr)->data;
14137 parameter->value_size = DW_BLOCK (attr)->size;
14138
14139 /* Parameters are not pre-cleared by memset above. */
14140 parameter->data_value = NULL;
14141 parameter->data_value_size = 0;
14142 call_site->parameter_count++;
14143
216f72a1
JK
14144 attr = dwarf2_attr (child_die, DW_AT_call_data_value, cu);
14145 if (attr == NULL)
14146 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
96408a79
SA
14147 if (attr)
14148 {
14149 if (!attr_form_is_block (attr))
14150 complaint (&symfile_complaints,
216f72a1 14151 _("No DW_FORM_block* DW_AT_call_data_value for "
9d8780f0
SM
14152 "DW_TAG_call_site child DIE %s [in module %s]"),
14153 sect_offset_str (child_die->sect_off),
9c541725 14154 objfile_name (objfile));
96408a79
SA
14155 else
14156 {
14157 parameter->data_value = DW_BLOCK (attr)->data;
14158 parameter->data_value_size = DW_BLOCK (attr)->size;
14159 }
14160 }
14161 }
14162}
14163
71a3c369
TT
14164/* Helper function for read_variable. If DIE represents a virtual
14165 table, then return the type of the concrete object that is
14166 associated with the virtual table. Otherwise, return NULL. */
14167
14168static struct type *
14169rust_containing_type (struct die_info *die, struct dwarf2_cu *cu)
14170{
14171 struct attribute *attr = dwarf2_attr (die, DW_AT_type, cu);
14172 if (attr == NULL)
14173 return NULL;
14174
14175 /* Find the type DIE. */
14176 struct die_info *type_die = NULL;
14177 struct dwarf2_cu *type_cu = cu;
14178
14179 if (attr_form_is_ref (attr))
14180 type_die = follow_die_ref (die, attr, &type_cu);
14181 if (type_die == NULL)
14182 return NULL;
14183
14184 if (dwarf2_attr (type_die, DW_AT_containing_type, type_cu) == NULL)
14185 return NULL;
14186 return die_containing_type (type_die, type_cu);
14187}
14188
14189/* Read a variable (DW_TAG_variable) DIE and create a new symbol. */
14190
14191static void
14192read_variable (struct die_info *die, struct dwarf2_cu *cu)
14193{
14194 struct rust_vtable_symbol *storage = NULL;
14195
14196 if (cu->language == language_rust)
14197 {
14198 struct type *containing_type = rust_containing_type (die, cu);
14199
14200 if (containing_type != NULL)
14201 {
518817b3 14202 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
71a3c369
TT
14203
14204 storage = OBSTACK_ZALLOC (&objfile->objfile_obstack,
14205 struct rust_vtable_symbol);
14206 initialize_objfile_symbol (storage);
14207 storage->concrete_type = containing_type;
cf724bc9 14208 storage->subclass = SYMBOL_RUST_VTABLE;
71a3c369
TT
14209 }
14210 }
14211
5e2db402 14212 new_symbol (die, NULL, cu, storage);
71a3c369
TT
14213}
14214
43988095
JK
14215/* Call CALLBACK from DW_AT_ranges attribute value OFFSET
14216 reading .debug_rnglists.
14217 Callback's type should be:
14218 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
14219 Return true if the attributes are present and valid, otherwise,
14220 return false. */
14221
14222template <typename Callback>
14223static bool
14224dwarf2_rnglists_process (unsigned offset, struct dwarf2_cu *cu,
14225 Callback &&callback)
14226{
ed2dc618 14227 struct dwarf2_per_objfile *dwarf2_per_objfile
518817b3 14228 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 14229 struct objfile *objfile = dwarf2_per_objfile->objfile;
43988095 14230 bfd *obfd = objfile->obfd;
43988095
JK
14231 /* Base address selection entry. */
14232 CORE_ADDR base;
14233 int found_base;
43988095 14234 const gdb_byte *buffer;
43988095
JK
14235 CORE_ADDR baseaddr;
14236 bool overflow = false;
14237
14238 found_base = cu->base_known;
14239 base = cu->base_address;
14240
14241 dwarf2_read_section (objfile, &dwarf2_per_objfile->rnglists);
14242 if (offset >= dwarf2_per_objfile->rnglists.size)
14243 {
14244 complaint (&symfile_complaints,
14245 _("Offset %d out of bounds for DW_AT_ranges attribute"),
14246 offset);
14247 return false;
14248 }
14249 buffer = dwarf2_per_objfile->rnglists.buffer + offset;
14250
14251 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
14252
14253 while (1)
14254 {
7814882a
JK
14255 /* Initialize it due to a false compiler warning. */
14256 CORE_ADDR range_beginning = 0, range_end = 0;
43988095
JK
14257 const gdb_byte *buf_end = (dwarf2_per_objfile->rnglists.buffer
14258 + dwarf2_per_objfile->rnglists.size);
14259 unsigned int bytes_read;
14260
14261 if (buffer == buf_end)
14262 {
14263 overflow = true;
14264 break;
14265 }
14266 const auto rlet = static_cast<enum dwarf_range_list_entry>(*buffer++);
14267 switch (rlet)
14268 {
14269 case DW_RLE_end_of_list:
14270 break;
14271 case DW_RLE_base_address:
14272 if (buffer + cu->header.addr_size > buf_end)
14273 {
14274 overflow = true;
14275 break;
14276 }
14277 base = read_address (obfd, buffer, cu, &bytes_read);
14278 found_base = 1;
14279 buffer += bytes_read;
14280 break;
14281 case DW_RLE_start_length:
14282 if (buffer + cu->header.addr_size > buf_end)
14283 {
14284 overflow = true;
14285 break;
14286 }
14287 range_beginning = read_address (obfd, buffer, cu, &bytes_read);
14288 buffer += bytes_read;
14289 range_end = (range_beginning
14290 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
14291 buffer += bytes_read;
14292 if (buffer > buf_end)
14293 {
14294 overflow = true;
14295 break;
14296 }
14297 break;
14298 case DW_RLE_offset_pair:
14299 range_beginning = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14300 buffer += bytes_read;
14301 if (buffer > buf_end)
14302 {
14303 overflow = true;
14304 break;
14305 }
14306 range_end = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14307 buffer += bytes_read;
14308 if (buffer > buf_end)
14309 {
14310 overflow = true;
14311 break;
14312 }
14313 break;
14314 case DW_RLE_start_end:
14315 if (buffer + 2 * cu->header.addr_size > buf_end)
14316 {
14317 overflow = true;
14318 break;
14319 }
14320 range_beginning = read_address (obfd, buffer, cu, &bytes_read);
14321 buffer += bytes_read;
14322 range_end = read_address (obfd, buffer, cu, &bytes_read);
14323 buffer += bytes_read;
14324 break;
14325 default:
14326 complaint (&symfile_complaints,
14327 _("Invalid .debug_rnglists data (no base address)"));
14328 return false;
14329 }
14330 if (rlet == DW_RLE_end_of_list || overflow)
14331 break;
14332 if (rlet == DW_RLE_base_address)
14333 continue;
14334
14335 if (!found_base)
14336 {
14337 /* We have no valid base address for the ranges
14338 data. */
14339 complaint (&symfile_complaints,
14340 _("Invalid .debug_rnglists data (no base address)"));
14341 return false;
14342 }
14343
14344 if (range_beginning > range_end)
14345 {
14346 /* Inverted range entries are invalid. */
14347 complaint (&symfile_complaints,
14348 _("Invalid .debug_rnglists data (inverted range)"));
14349 return false;
14350 }
14351
14352 /* Empty range entries have no effect. */
14353 if (range_beginning == range_end)
14354 continue;
14355
14356 range_beginning += base;
14357 range_end += base;
14358
14359 /* A not-uncommon case of bad debug info.
14360 Don't pollute the addrmap with bad data. */
14361 if (range_beginning + baseaddr == 0
14362 && !dwarf2_per_objfile->has_section_at_zero)
14363 {
14364 complaint (&symfile_complaints,
14365 _(".debug_rnglists entry has start address of zero"
14366 " [in module %s]"), objfile_name (objfile));
14367 continue;
14368 }
14369
14370 callback (range_beginning, range_end);
14371 }
14372
14373 if (overflow)
14374 {
14375 complaint (&symfile_complaints,
14376 _("Offset %d is not terminated "
14377 "for DW_AT_ranges attribute"),
14378 offset);
14379 return false;
14380 }
14381
14382 return true;
14383}
14384
14385/* Call CALLBACK from DW_AT_ranges attribute value OFFSET reading .debug_ranges.
14386 Callback's type should be:
14387 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
5f46c5a5 14388 Return 1 if the attributes are present and valid, otherwise, return 0. */
43039443 14389
43988095 14390template <typename Callback>
43039443 14391static int
5f46c5a5 14392dwarf2_ranges_process (unsigned offset, struct dwarf2_cu *cu,
43988095 14393 Callback &&callback)
43039443 14394{
ed2dc618 14395 struct dwarf2_per_objfile *dwarf2_per_objfile
518817b3 14396 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 14397 struct objfile *objfile = dwarf2_per_objfile->objfile;
43039443
JK
14398 struct comp_unit_head *cu_header = &cu->header;
14399 bfd *obfd = objfile->obfd;
14400 unsigned int addr_size = cu_header->addr_size;
14401 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
14402 /* Base address selection entry. */
14403 CORE_ADDR base;
14404 int found_base;
14405 unsigned int dummy;
d521ce57 14406 const gdb_byte *buffer;
ff013f42 14407 CORE_ADDR baseaddr;
43039443 14408
43988095
JK
14409 if (cu_header->version >= 5)
14410 return dwarf2_rnglists_process (offset, cu, callback);
14411
d00adf39
DE
14412 found_base = cu->base_known;
14413 base = cu->base_address;
43039443 14414
be391dca 14415 dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
dce234bc 14416 if (offset >= dwarf2_per_objfile->ranges.size)
43039443
JK
14417 {
14418 complaint (&symfile_complaints,
14419 _("Offset %d out of bounds for DW_AT_ranges attribute"),
14420 offset);
14421 return 0;
14422 }
dce234bc 14423 buffer = dwarf2_per_objfile->ranges.buffer + offset;
43039443 14424
e7030f15 14425 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
ff013f42 14426
43039443
JK
14427 while (1)
14428 {
14429 CORE_ADDR range_beginning, range_end;
14430
14431 range_beginning = read_address (obfd, buffer, cu, &dummy);
14432 buffer += addr_size;
14433 range_end = read_address (obfd, buffer, cu, &dummy);
14434 buffer += addr_size;
14435 offset += 2 * addr_size;
14436
14437 /* An end of list marker is a pair of zero addresses. */
14438 if (range_beginning == 0 && range_end == 0)
14439 /* Found the end of list entry. */
14440 break;
14441
14442 /* Each base address selection entry is a pair of 2 values.
14443 The first is the largest possible address, the second is
14444 the base address. Check for a base address here. */
14445 if ((range_beginning & mask) == mask)
14446 {
28d2bfb9
AB
14447 /* If we found the largest possible address, then we already
14448 have the base address in range_end. */
14449 base = range_end;
43039443
JK
14450 found_base = 1;
14451 continue;
14452 }
14453
14454 if (!found_base)
14455 {
14456 /* We have no valid base address for the ranges
14457 data. */
14458 complaint (&symfile_complaints,
14459 _("Invalid .debug_ranges data (no base address)"));
14460 return 0;
14461 }
14462
9277c30c
UW
14463 if (range_beginning > range_end)
14464 {
14465 /* Inverted range entries are invalid. */
14466 complaint (&symfile_complaints,
14467 _("Invalid .debug_ranges data (inverted range)"));
14468 return 0;
14469 }
14470
14471 /* Empty range entries have no effect. */
14472 if (range_beginning == range_end)
14473 continue;
14474
43039443
JK
14475 range_beginning += base;
14476 range_end += base;
14477
01093045
DE
14478 /* A not-uncommon case of bad debug info.
14479 Don't pollute the addrmap with bad data. */
14480 if (range_beginning + baseaddr == 0
14481 && !dwarf2_per_objfile->has_section_at_zero)
14482 {
14483 complaint (&symfile_complaints,
14484 _(".debug_ranges entry has start address of zero"
4262abfb 14485 " [in module %s]"), objfile_name (objfile));
01093045
DE
14486 continue;
14487 }
14488
5f46c5a5
JK
14489 callback (range_beginning, range_end);
14490 }
14491
14492 return 1;
14493}
14494
14495/* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
14496 Return 1 if the attributes are present and valid, otherwise, return 0.
14497 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
14498
14499static int
14500dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
14501 CORE_ADDR *high_return, struct dwarf2_cu *cu,
14502 struct partial_symtab *ranges_pst)
14503{
518817b3 14504 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
5f46c5a5
JK
14505 struct gdbarch *gdbarch = get_objfile_arch (objfile);
14506 const CORE_ADDR baseaddr = ANOFFSET (objfile->section_offsets,
14507 SECT_OFF_TEXT (objfile));
14508 int low_set = 0;
14509 CORE_ADDR low = 0;
14510 CORE_ADDR high = 0;
14511 int retval;
14512
14513 retval = dwarf2_ranges_process (offset, cu,
14514 [&] (CORE_ADDR range_beginning, CORE_ADDR range_end)
14515 {
9277c30c 14516 if (ranges_pst != NULL)
3e29f34a
MR
14517 {
14518 CORE_ADDR lowpc;
14519 CORE_ADDR highpc;
14520
14521 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch,
14522 range_beginning + baseaddr);
14523 highpc = gdbarch_adjust_dwarf2_addr (gdbarch,
14524 range_end + baseaddr);
14525 addrmap_set_empty (objfile->psymtabs_addrmap, lowpc, highpc - 1,
14526 ranges_pst);
14527 }
ff013f42 14528
43039443
JK
14529 /* FIXME: This is recording everything as a low-high
14530 segment of consecutive addresses. We should have a
14531 data structure for discontiguous block ranges
14532 instead. */
14533 if (! low_set)
14534 {
14535 low = range_beginning;
14536 high = range_end;
14537 low_set = 1;
14538 }
14539 else
14540 {
14541 if (range_beginning < low)
14542 low = range_beginning;
14543 if (range_end > high)
14544 high = range_end;
14545 }
5f46c5a5
JK
14546 });
14547 if (!retval)
14548 return 0;
43039443
JK
14549
14550 if (! low_set)
14551 /* If the first entry is an end-of-list marker, the range
14552 describes an empty scope, i.e. no instructions. */
14553 return 0;
14554
14555 if (low_return)
14556 *low_return = low;
14557 if (high_return)
14558 *high_return = high;
14559 return 1;
14560}
14561
3a2b436a
JK
14562/* Get low and high pc attributes from a die. See enum pc_bounds_kind
14563 definition for the return value. *LOWPC and *HIGHPC are set iff
e385593e 14564 neither PC_BOUNDS_NOT_PRESENT nor PC_BOUNDS_INVALID are returned. */
380bca97 14565
3a2b436a 14566static enum pc_bounds_kind
af34e669 14567dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
d85a05f0
DJ
14568 CORE_ADDR *highpc, struct dwarf2_cu *cu,
14569 struct partial_symtab *pst)
c906108c 14570{
518817b3
SM
14571 struct dwarf2_per_objfile *dwarf2_per_objfile
14572 = cu->per_cu->dwarf2_per_objfile;
c906108c 14573 struct attribute *attr;
91da1414 14574 struct attribute *attr_high;
af34e669
DJ
14575 CORE_ADDR low = 0;
14576 CORE_ADDR high = 0;
e385593e 14577 enum pc_bounds_kind ret;
c906108c 14578
91da1414
MW
14579 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14580 if (attr_high)
af34e669 14581 {
e142c38c 14582 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
af34e669 14583 if (attr)
91da1414 14584 {
31aa7e4e
JB
14585 low = attr_value_as_address (attr);
14586 high = attr_value_as_address (attr_high);
14587 if (cu->header.version >= 4 && attr_form_is_constant (attr_high))
14588 high += low;
91da1414 14589 }
af34e669
DJ
14590 else
14591 /* Found high w/o low attribute. */
e385593e 14592 return PC_BOUNDS_INVALID;
af34e669
DJ
14593
14594 /* Found consecutive range of addresses. */
3a2b436a 14595 ret = PC_BOUNDS_HIGH_LOW;
af34e669 14596 }
c906108c 14597 else
af34e669 14598 {
e142c38c 14599 attr = dwarf2_attr (die, DW_AT_ranges, cu);
af34e669
DJ
14600 if (attr != NULL)
14601 {
ab435259
DE
14602 /* DW_AT_ranges_base does not apply to DIEs from the DWO skeleton.
14603 We take advantage of the fact that DW_AT_ranges does not appear
14604 in DW_TAG_compile_unit of DWO files. */
14605 int need_ranges_base = die->tag != DW_TAG_compile_unit;
14606 unsigned int ranges_offset = (DW_UNSND (attr)
14607 + (need_ranges_base
14608 ? cu->ranges_base
14609 : 0));
2e3cf129 14610
af34e669 14611 /* Value of the DW_AT_ranges attribute is the offset in the
a604369a 14612 .debug_ranges section. */
2e3cf129 14613 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst))
e385593e 14614 return PC_BOUNDS_INVALID;
43039443 14615 /* Found discontinuous range of addresses. */
3a2b436a 14616 ret = PC_BOUNDS_RANGES;
af34e669 14617 }
e385593e
JK
14618 else
14619 return PC_BOUNDS_NOT_PRESENT;
af34e669 14620 }
c906108c 14621
48fbe735 14622 /* partial_die_info::read has also the strict LOW < HIGH requirement. */
9373cf26 14623 if (high <= low)
e385593e 14624 return PC_BOUNDS_INVALID;
c906108c
SS
14625
14626 /* When using the GNU linker, .gnu.linkonce. sections are used to
14627 eliminate duplicate copies of functions and vtables and such.
14628 The linker will arbitrarily choose one and discard the others.
14629 The AT_*_pc values for such functions refer to local labels in
14630 these sections. If the section from that file was discarded, the
14631 labels are not in the output, so the relocs get a value of 0.
14632 If this is a discarded function, mark the pc bounds as invalid,
14633 so that GDB will ignore it. */
72dca2f5 14634 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
e385593e 14635 return PC_BOUNDS_INVALID;
c906108c
SS
14636
14637 *lowpc = low;
96408a79
SA
14638 if (highpc)
14639 *highpc = high;
af34e669 14640 return ret;
c906108c
SS
14641}
14642
b084d499
JB
14643/* Assuming that DIE represents a subprogram DIE or a lexical block, get
14644 its low and high PC addresses. Do nothing if these addresses could not
14645 be determined. Otherwise, set LOWPC to the low address if it is smaller,
14646 and HIGHPC to the high address if greater than HIGHPC. */
14647
14648static void
14649dwarf2_get_subprogram_pc_bounds (struct die_info *die,
14650 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14651 struct dwarf2_cu *cu)
14652{
14653 CORE_ADDR low, high;
14654 struct die_info *child = die->child;
14655
e385593e 14656 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL) >= PC_BOUNDS_RANGES)
b084d499 14657 {
325fac50
PA
14658 *lowpc = std::min (*lowpc, low);
14659 *highpc = std::max (*highpc, high);
b084d499
JB
14660 }
14661
14662 /* If the language does not allow nested subprograms (either inside
14663 subprograms or lexical blocks), we're done. */
14664 if (cu->language != language_ada)
14665 return;
6e70227d 14666
b084d499
JB
14667 /* Check all the children of the given DIE. If it contains nested
14668 subprograms, then check their pc bounds. Likewise, we need to
14669 check lexical blocks as well, as they may also contain subprogram
14670 definitions. */
14671 while (child && child->tag)
14672 {
14673 if (child->tag == DW_TAG_subprogram
14674 || child->tag == DW_TAG_lexical_block)
14675 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
14676 child = sibling_die (child);
14677 }
14678}
14679
fae299cd
DC
14680/* Get the low and high pc's represented by the scope DIE, and store
14681 them in *LOWPC and *HIGHPC. If the correct values can't be
14682 determined, set *LOWPC to -1 and *HIGHPC to 0. */
14683
14684static void
14685get_scope_pc_bounds (struct die_info *die,
14686 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14687 struct dwarf2_cu *cu)
14688{
14689 CORE_ADDR best_low = (CORE_ADDR) -1;
14690 CORE_ADDR best_high = (CORE_ADDR) 0;
14691 CORE_ADDR current_low, current_high;
14692
3a2b436a 14693 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL)
e385593e 14694 >= PC_BOUNDS_RANGES)
fae299cd
DC
14695 {
14696 best_low = current_low;
14697 best_high = current_high;
14698 }
14699 else
14700 {
14701 struct die_info *child = die->child;
14702
14703 while (child && child->tag)
14704 {
14705 switch (child->tag) {
14706 case DW_TAG_subprogram:
b084d499 14707 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
fae299cd
DC
14708 break;
14709 case DW_TAG_namespace:
f55ee35c 14710 case DW_TAG_module:
fae299cd
DC
14711 /* FIXME: carlton/2004-01-16: Should we do this for
14712 DW_TAG_class_type/DW_TAG_structure_type, too? I think
14713 that current GCC's always emit the DIEs corresponding
14714 to definitions of methods of classes as children of a
14715 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
14716 the DIEs giving the declarations, which could be
14717 anywhere). But I don't see any reason why the
14718 standards says that they have to be there. */
14719 get_scope_pc_bounds (child, &current_low, &current_high, cu);
14720
14721 if (current_low != ((CORE_ADDR) -1))
14722 {
325fac50
PA
14723 best_low = std::min (best_low, current_low);
14724 best_high = std::max (best_high, current_high);
fae299cd
DC
14725 }
14726 break;
14727 default:
0963b4bd 14728 /* Ignore. */
fae299cd
DC
14729 break;
14730 }
14731
14732 child = sibling_die (child);
14733 }
14734 }
14735
14736 *lowpc = best_low;
14737 *highpc = best_high;
14738}
14739
801e3a5b
JB
14740/* Record the address ranges for BLOCK, offset by BASEADDR, as given
14741 in DIE. */
380bca97 14742
801e3a5b
JB
14743static void
14744dwarf2_record_block_ranges (struct die_info *die, struct block *block,
14745 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
14746{
518817b3 14747 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
3e29f34a 14748 struct gdbarch *gdbarch = get_objfile_arch (objfile);
801e3a5b 14749 struct attribute *attr;
91da1414 14750 struct attribute *attr_high;
801e3a5b 14751
91da1414
MW
14752 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14753 if (attr_high)
801e3a5b 14754 {
801e3a5b
JB
14755 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
14756 if (attr)
14757 {
31aa7e4e
JB
14758 CORE_ADDR low = attr_value_as_address (attr);
14759 CORE_ADDR high = attr_value_as_address (attr_high);
14760
14761 if (cu->header.version >= 4 && attr_form_is_constant (attr_high))
14762 high += low;
9a619af0 14763
3e29f34a
MR
14764 low = gdbarch_adjust_dwarf2_addr (gdbarch, low + baseaddr);
14765 high = gdbarch_adjust_dwarf2_addr (gdbarch, high + baseaddr);
14766 record_block_range (block, low, high - 1);
801e3a5b
JB
14767 }
14768 }
14769
14770 attr = dwarf2_attr (die, DW_AT_ranges, cu);
14771 if (attr)
14772 {
ab435259
DE
14773 /* DW_AT_ranges_base does not apply to DIEs from the DWO skeleton.
14774 We take advantage of the fact that DW_AT_ranges does not appear
14775 in DW_TAG_compile_unit of DWO files. */
14776 int need_ranges_base = die->tag != DW_TAG_compile_unit;
801e3a5b
JB
14777
14778 /* The value of the DW_AT_ranges attribute is the offset of the
14779 address range list in the .debug_ranges section. */
ab435259
DE
14780 unsigned long offset = (DW_UNSND (attr)
14781 + (need_ranges_base ? cu->ranges_base : 0));
801e3a5b 14782
5f46c5a5
JK
14783 dwarf2_ranges_process (offset, cu,
14784 [&] (CORE_ADDR start, CORE_ADDR end)
14785 {
58fdfd2c
JK
14786 start += baseaddr;
14787 end += baseaddr;
5f46c5a5
JK
14788 start = gdbarch_adjust_dwarf2_addr (gdbarch, start);
14789 end = gdbarch_adjust_dwarf2_addr (gdbarch, end);
14790 record_block_range (block, start, end - 1);
14791 });
801e3a5b
JB
14792 }
14793}
14794
685b1105
JK
14795/* Check whether the producer field indicates either of GCC < 4.6, or the
14796 Intel C/C++ compiler, and cache the result in CU. */
60d5a603 14797
685b1105
JK
14798static void
14799check_producer (struct dwarf2_cu *cu)
60d5a603 14800{
38360086 14801 int major, minor;
60d5a603
JK
14802
14803 if (cu->producer == NULL)
14804 {
14805 /* For unknown compilers expect their behavior is DWARF version
14806 compliant.
14807
14808 GCC started to support .debug_types sections by -gdwarf-4 since
14809 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
14810 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
14811 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
14812 interpreted incorrectly by GDB now - GCC PR debug/48229. */
60d5a603 14813 }
b1ffba5a 14814 else if (producer_is_gcc (cu->producer, &major, &minor))
60d5a603 14815 {
38360086
MW
14816 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
14817 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
685b1105 14818 }
5230b05a
WT
14819 else if (producer_is_icc (cu->producer, &major, &minor))
14820 cu->producer_is_icc_lt_14 = major < 14;
685b1105
JK
14821 else
14822 {
14823 /* For other non-GCC compilers, expect their behavior is DWARF version
14824 compliant. */
60d5a603
JK
14825 }
14826
ba919b58 14827 cu->checked_producer = 1;
685b1105 14828}
ba919b58 14829
685b1105
JK
14830/* Check for GCC PR debug/45124 fix which is not present in any G++ version up
14831 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
14832 during 4.6.0 experimental. */
14833
14834static int
14835producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
14836{
14837 if (!cu->checked_producer)
14838 check_producer (cu);
14839
14840 return cu->producer_is_gxx_lt_4_6;
60d5a603
JK
14841}
14842
14843/* Return the default accessibility type if it is not overriden by
14844 DW_AT_accessibility. */
14845
14846static enum dwarf_access_attribute
14847dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
14848{
14849 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
14850 {
14851 /* The default DWARF 2 accessibility for members is public, the default
14852 accessibility for inheritance is private. */
14853
14854 if (die->tag != DW_TAG_inheritance)
14855 return DW_ACCESS_public;
14856 else
14857 return DW_ACCESS_private;
14858 }
14859 else
14860 {
14861 /* DWARF 3+ defines the default accessibility a different way. The same
14862 rules apply now for DW_TAG_inheritance as for the members and it only
14863 depends on the container kind. */
14864
14865 if (die->parent->tag == DW_TAG_class_type)
14866 return DW_ACCESS_private;
14867 else
14868 return DW_ACCESS_public;
14869 }
14870}
14871
74ac6d43
TT
14872/* Look for DW_AT_data_member_location. Set *OFFSET to the byte
14873 offset. If the attribute was not found return 0, otherwise return
14874 1. If it was found but could not properly be handled, set *OFFSET
14875 to 0. */
14876
14877static int
14878handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
14879 LONGEST *offset)
14880{
14881 struct attribute *attr;
14882
14883 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
14884 if (attr != NULL)
14885 {
14886 *offset = 0;
14887
14888 /* Note that we do not check for a section offset first here.
14889 This is because DW_AT_data_member_location is new in DWARF 4,
14890 so if we see it, we can assume that a constant form is really
14891 a constant and not a section offset. */
14892 if (attr_form_is_constant (attr))
14893 *offset = dwarf2_get_attr_constant_value (attr, 0);
14894 else if (attr_form_is_section_offset (attr))
14895 dwarf2_complex_location_expr_complaint ();
14896 else if (attr_form_is_block (attr))
14897 *offset = decode_locdesc (DW_BLOCK (attr), cu);
14898 else
14899 dwarf2_complex_location_expr_complaint ();
14900
14901 return 1;
14902 }
14903
14904 return 0;
14905}
14906
c906108c
SS
14907/* Add an aggregate field to the field list. */
14908
14909static void
107d2387 14910dwarf2_add_field (struct field_info *fip, struct die_info *die,
e7c27a73 14911 struct dwarf2_cu *cu)
6e70227d 14912{
518817b3 14913 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
5e2b427d 14914 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
14915 struct nextfield *new_field;
14916 struct attribute *attr;
14917 struct field *fp;
15d034d0 14918 const char *fieldname = "";
c906108c 14919
7d0ccb61
DJ
14920 if (die->tag == DW_TAG_inheritance)
14921 {
be2daae6
TT
14922 fip->baseclasses.emplace_back ();
14923 new_field = &fip->baseclasses.back ();
7d0ccb61
DJ
14924 }
14925 else
14926 {
be2daae6
TT
14927 fip->fields.emplace_back ();
14928 new_field = &fip->fields.back ();
7d0ccb61 14929 }
be2daae6 14930
c906108c
SS
14931 fip->nfields++;
14932
e142c38c 14933 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
c906108c
SS
14934 if (attr)
14935 new_field->accessibility = DW_UNSND (attr);
60d5a603
JK
14936 else
14937 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
c906108c
SS
14938 if (new_field->accessibility != DW_ACCESS_public)
14939 fip->non_public_fields = 1;
60d5a603 14940
e142c38c 14941 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
c906108c
SS
14942 if (attr)
14943 new_field->virtuality = DW_UNSND (attr);
60d5a603
JK
14944 else
14945 new_field->virtuality = DW_VIRTUALITY_none;
c906108c
SS
14946
14947 fp = &new_field->field;
a9a9bd0f 14948
e142c38c 14949 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
c906108c 14950 {
74ac6d43
TT
14951 LONGEST offset;
14952
a9a9bd0f 14953 /* Data member other than a C++ static data member. */
6e70227d 14954
c906108c 14955 /* Get type of field. */
e7c27a73 14956 fp->type = die_type (die, cu);
c906108c 14957
d6a843b5 14958 SET_FIELD_BITPOS (*fp, 0);
01ad7f36 14959
c906108c 14960 /* Get bit size of field (zero if none). */
e142c38c 14961 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
c906108c
SS
14962 if (attr)
14963 {
14964 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
14965 }
14966 else
14967 {
14968 FIELD_BITSIZE (*fp) = 0;
14969 }
14970
14971 /* Get bit offset of field. */
74ac6d43
TT
14972 if (handle_data_member_location (die, cu, &offset))
14973 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
e142c38c 14974 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
c906108c
SS
14975 if (attr)
14976 {
5e2b427d 14977 if (gdbarch_bits_big_endian (gdbarch))
c906108c
SS
14978 {
14979 /* For big endian bits, the DW_AT_bit_offset gives the
c5aa993b
JM
14980 additional bit offset from the MSB of the containing
14981 anonymous object to the MSB of the field. We don't
14982 have to do anything special since we don't need to
14983 know the size of the anonymous object. */
f41f5e61 14984 SET_FIELD_BITPOS (*fp, FIELD_BITPOS (*fp) + DW_UNSND (attr));
c906108c
SS
14985 }
14986 else
14987 {
14988 /* For little endian bits, compute the bit offset to the
c5aa993b
JM
14989 MSB of the anonymous object, subtract off the number of
14990 bits from the MSB of the field to the MSB of the
14991 object, and then subtract off the number of bits of
14992 the field itself. The result is the bit offset of
14993 the LSB of the field. */
c906108c
SS
14994 int anonymous_size;
14995 int bit_offset = DW_UNSND (attr);
14996
e142c38c 14997 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
14998 if (attr)
14999 {
15000 /* The size of the anonymous object containing
15001 the bit field is explicit, so use the
15002 indicated size (in bytes). */
15003 anonymous_size = DW_UNSND (attr);
15004 }
15005 else
15006 {
15007 /* The size of the anonymous object containing
15008 the bit field must be inferred from the type
15009 attribute of the data member containing the
15010 bit field. */
15011 anonymous_size = TYPE_LENGTH (fp->type);
15012 }
f41f5e61
PA
15013 SET_FIELD_BITPOS (*fp,
15014 (FIELD_BITPOS (*fp)
15015 + anonymous_size * bits_per_byte
15016 - bit_offset - FIELD_BITSIZE (*fp)));
c906108c
SS
15017 }
15018 }
da5b30da
AA
15019 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
15020 if (attr != NULL)
15021 SET_FIELD_BITPOS (*fp, (FIELD_BITPOS (*fp)
15022 + dwarf2_get_attr_constant_value (attr, 0)));
c906108c
SS
15023
15024 /* Get name of field. */
39cbfefa
DJ
15025 fieldname = dwarf2_name (die, cu);
15026 if (fieldname == NULL)
15027 fieldname = "";
d8151005
DJ
15028
15029 /* The name is already allocated along with this objfile, so we don't
15030 need to duplicate it for the type. */
15031 fp->name = fieldname;
c906108c
SS
15032
15033 /* Change accessibility for artificial fields (e.g. virtual table
c5aa993b 15034 pointer or virtual base class pointer) to private. */
e142c38c 15035 if (dwarf2_attr (die, DW_AT_artificial, cu))
c906108c 15036 {
d48cc9dd 15037 FIELD_ARTIFICIAL (*fp) = 1;
c906108c
SS
15038 new_field->accessibility = DW_ACCESS_private;
15039 fip->non_public_fields = 1;
15040 }
15041 }
a9a9bd0f 15042 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
c906108c 15043 {
a9a9bd0f
DC
15044 /* C++ static member. */
15045
15046 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
15047 is a declaration, but all versions of G++ as of this writing
15048 (so through at least 3.2.1) incorrectly generate
15049 DW_TAG_variable tags. */
6e70227d 15050
ff355380 15051 const char *physname;
c906108c 15052
a9a9bd0f 15053 /* Get name of field. */
39cbfefa
DJ
15054 fieldname = dwarf2_name (die, cu);
15055 if (fieldname == NULL)
c906108c
SS
15056 return;
15057
254e6b9e 15058 attr = dwarf2_attr (die, DW_AT_const_value, cu);
3863f96c
DE
15059 if (attr
15060 /* Only create a symbol if this is an external value.
15061 new_symbol checks this and puts the value in the global symbol
15062 table, which we want. If it is not external, new_symbol
15063 will try to put the value in cu->list_in_scope which is wrong. */
15064 && dwarf2_flag_true_p (die, DW_AT_external, cu))
254e6b9e
DE
15065 {
15066 /* A static const member, not much different than an enum as far as
15067 we're concerned, except that we can support more types. */
15068 new_symbol (die, NULL, cu);
15069 }
15070
2df3850c 15071 /* Get physical name. */
ff355380 15072 physname = dwarf2_physname (fieldname, die, cu);
c906108c 15073
d8151005
DJ
15074 /* The name is already allocated along with this objfile, so we don't
15075 need to duplicate it for the type. */
15076 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
e7c27a73 15077 FIELD_TYPE (*fp) = die_type (die, cu);
d8151005 15078 FIELD_NAME (*fp) = fieldname;
c906108c
SS
15079 }
15080 else if (die->tag == DW_TAG_inheritance)
15081 {
74ac6d43 15082 LONGEST offset;
d4b96c9a 15083
74ac6d43
TT
15084 /* C++ base class field. */
15085 if (handle_data_member_location (die, cu, &offset))
15086 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
c906108c 15087 FIELD_BITSIZE (*fp) = 0;
e7c27a73 15088 FIELD_TYPE (*fp) = die_type (die, cu);
c906108c 15089 FIELD_NAME (*fp) = type_name_no_tag (fp->type);
c906108c 15090 }
2ddeaf8a
TT
15091 else if (die->tag == DW_TAG_variant_part)
15092 {
15093 /* process_structure_scope will treat this DIE as a union. */
15094 process_structure_scope (die, cu);
15095
15096 /* The variant part is relative to the start of the enclosing
15097 structure. */
15098 SET_FIELD_BITPOS (*fp, 0);
15099 fp->type = get_die_type (die, cu);
15100 fp->artificial = 1;
15101 fp->name = "<<variant>>";
15102 }
15103 else
15104 gdb_assert_not_reached ("missing case in dwarf2_add_field");
c906108c
SS
15105}
15106
883fd55a
KS
15107/* Can the type given by DIE define another type? */
15108
15109static bool
15110type_can_define_types (const struct die_info *die)
15111{
15112 switch (die->tag)
15113 {
15114 case DW_TAG_typedef:
15115 case DW_TAG_class_type:
15116 case DW_TAG_structure_type:
15117 case DW_TAG_union_type:
15118 case DW_TAG_enumeration_type:
15119 return true;
15120
15121 default:
15122 return false;
15123 }
15124}
15125
15126/* Add a type definition defined in the scope of the FIP's class. */
98751a41
JK
15127
15128static void
883fd55a
KS
15129dwarf2_add_type_defn (struct field_info *fip, struct die_info *die,
15130 struct dwarf2_cu *cu)
6e70227d 15131{
be2daae6
TT
15132 struct decl_field fp;
15133 memset (&fp, 0, sizeof (fp));
98751a41 15134
883fd55a 15135 gdb_assert (type_can_define_types (die));
98751a41 15136
883fd55a 15137 /* Get name of field. NULL is okay here, meaning an anonymous type. */
be2daae6
TT
15138 fp.name = dwarf2_name (die, cu);
15139 fp.type = read_type_die (die, cu);
98751a41 15140
c191a687
KS
15141 /* Save accessibility. */
15142 enum dwarf_access_attribute accessibility;
15143 struct attribute *attr = dwarf2_attr (die, DW_AT_accessibility, cu);
15144 if (attr != NULL)
15145 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
15146 else
15147 accessibility = dwarf2_default_access_attribute (die, cu);
15148 switch (accessibility)
15149 {
15150 case DW_ACCESS_public:
15151 /* The assumed value if neither private nor protected. */
15152 break;
15153 case DW_ACCESS_private:
be2daae6 15154 fp.is_private = 1;
c191a687
KS
15155 break;
15156 case DW_ACCESS_protected:
be2daae6 15157 fp.is_protected = 1;
c191a687
KS
15158 break;
15159 default:
37534686
KS
15160 complaint (&symfile_complaints,
15161 _("Unhandled DW_AT_accessibility value (%x)"), accessibility);
c191a687
KS
15162 }
15163
883fd55a 15164 if (die->tag == DW_TAG_typedef)
be2daae6 15165 fip->typedef_field_list.push_back (fp);
883fd55a 15166 else
be2daae6 15167 fip->nested_types_list.push_back (fp);
98751a41
JK
15168}
15169
c906108c
SS
15170/* Create the vector of fields, and attach it to the type. */
15171
15172static void
fba45db2 15173dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 15174 struct dwarf2_cu *cu)
c906108c
SS
15175{
15176 int nfields = fip->nfields;
15177
15178 /* Record the field count, allocate space for the array of fields,
15179 and create blank accessibility bitfields if necessary. */
15180 TYPE_NFIELDS (type) = nfields;
15181 TYPE_FIELDS (type) = (struct field *)
be2daae6 15182 TYPE_ZALLOC (type, sizeof (struct field) * nfields);
c906108c 15183
b4ba55a1 15184 if (fip->non_public_fields && cu->language != language_ada)
c906108c
SS
15185 {
15186 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15187
15188 TYPE_FIELD_PRIVATE_BITS (type) =
15189 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15190 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
15191
15192 TYPE_FIELD_PROTECTED_BITS (type) =
15193 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15194 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
15195
774b6a14
TT
15196 TYPE_FIELD_IGNORE_BITS (type) =
15197 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15198 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
c906108c
SS
15199 }
15200
15201 /* If the type has baseclasses, allocate and clear a bit vector for
15202 TYPE_FIELD_VIRTUAL_BITS. */
be2daae6 15203 if (!fip->baseclasses.empty () && cu->language != language_ada)
c906108c 15204 {
be2daae6 15205 int num_bytes = B_BYTES (fip->baseclasses.size ());
fe1b8b76 15206 unsigned char *pointer;
c906108c
SS
15207
15208 ALLOCATE_CPLUS_STRUCT_TYPE (type);
224c3ddb 15209 pointer = (unsigned char *) TYPE_ALLOC (type, num_bytes);
fe1b8b76 15210 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
be2daae6
TT
15211 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->baseclasses.size ());
15212 TYPE_N_BASECLASSES (type) = fip->baseclasses.size ();
c906108c
SS
15213 }
15214
2ddeaf8a
TT
15215 if (TYPE_FLAG_DISCRIMINATED_UNION (type))
15216 {
15217 struct discriminant_info *di = alloc_discriminant_info (type, -1, -1);
15218
be2daae6 15219 for (int index = 0; index < nfields; ++index)
2ddeaf8a 15220 {
be2daae6
TT
15221 struct nextfield &field = fip->fields[index];
15222
15223 if (field.variant.is_discriminant)
2ddeaf8a 15224 di->discriminant_index = index;
be2daae6 15225 else if (field.variant.default_branch)
2ddeaf8a
TT
15226 di->default_index = index;
15227 else
be2daae6 15228 di->discriminants[index] = field.variant.discriminant_value;
2ddeaf8a
TT
15229 }
15230 }
15231
be2daae6
TT
15232 /* Copy the saved-up fields into the field vector. */
15233 for (int i = 0; i < nfields; ++i)
c906108c 15234 {
be2daae6
TT
15235 struct nextfield &field
15236 = ((i < fip->baseclasses.size ()) ? fip->baseclasses[i]
15237 : fip->fields[i - fip->baseclasses.size ()]);
7d0ccb61 15238
be2daae6
TT
15239 TYPE_FIELD (type, i) = field.field;
15240 switch (field.accessibility)
c906108c 15241 {
c5aa993b 15242 case DW_ACCESS_private:
b4ba55a1 15243 if (cu->language != language_ada)
be2daae6 15244 SET_TYPE_FIELD_PRIVATE (type, i);
c5aa993b 15245 break;
c906108c 15246
c5aa993b 15247 case DW_ACCESS_protected:
b4ba55a1 15248 if (cu->language != language_ada)
be2daae6 15249 SET_TYPE_FIELD_PROTECTED (type, i);
c5aa993b 15250 break;
c906108c 15251
c5aa993b
JM
15252 case DW_ACCESS_public:
15253 break;
c906108c 15254
c5aa993b
JM
15255 default:
15256 /* Unknown accessibility. Complain and treat it as public. */
15257 {
e2e0b3e5 15258 complaint (&symfile_complaints, _("unsupported accessibility %d"),
be2daae6 15259 field.accessibility);
c5aa993b
JM
15260 }
15261 break;
c906108c 15262 }
be2daae6 15263 if (i < fip->baseclasses.size ())
c906108c 15264 {
be2daae6 15265 switch (field.virtuality)
c906108c 15266 {
c5aa993b
JM
15267 case DW_VIRTUALITY_virtual:
15268 case DW_VIRTUALITY_pure_virtual:
b4ba55a1 15269 if (cu->language == language_ada)
a73c6dcd 15270 error (_("unexpected virtuality in component of Ada type"));
be2daae6 15271 SET_TYPE_FIELD_VIRTUAL (type, i);
c5aa993b 15272 break;
c906108c
SS
15273 }
15274 }
c906108c
SS
15275 }
15276}
15277
7d27a96d
TT
15278/* Return true if this member function is a constructor, false
15279 otherwise. */
15280
15281static int
15282dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
15283{
15284 const char *fieldname;
fe978cb0 15285 const char *type_name;
7d27a96d
TT
15286 int len;
15287
15288 if (die->parent == NULL)
15289 return 0;
15290
15291 if (die->parent->tag != DW_TAG_structure_type
15292 && die->parent->tag != DW_TAG_union_type
15293 && die->parent->tag != DW_TAG_class_type)
15294 return 0;
15295
15296 fieldname = dwarf2_name (die, cu);
fe978cb0
PA
15297 type_name = dwarf2_name (die->parent, cu);
15298 if (fieldname == NULL || type_name == NULL)
7d27a96d
TT
15299 return 0;
15300
15301 len = strlen (fieldname);
fe978cb0
PA
15302 return (strncmp (fieldname, type_name, len) == 0
15303 && (type_name[len] == '\0' || type_name[len] == '<'));
7d27a96d
TT
15304}
15305
c906108c
SS
15306/* Add a member function to the proper fieldlist. */
15307
15308static void
107d2387 15309dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
e7c27a73 15310 struct type *type, struct dwarf2_cu *cu)
c906108c 15311{
518817b3 15312 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c 15313 struct attribute *attr;
c906108c 15314 int i;
be2daae6 15315 struct fnfieldlist *flp = nullptr;
c906108c 15316 struct fn_field *fnp;
15d034d0 15317 const char *fieldname;
f792889a 15318 struct type *this_type;
60d5a603 15319 enum dwarf_access_attribute accessibility;
c906108c 15320
b4ba55a1 15321 if (cu->language == language_ada)
a73c6dcd 15322 error (_("unexpected member function in Ada type"));
b4ba55a1 15323
2df3850c 15324 /* Get name of member function. */
39cbfefa
DJ
15325 fieldname = dwarf2_name (die, cu);
15326 if (fieldname == NULL)
2df3850c 15327 return;
c906108c 15328
c906108c 15329 /* Look up member function name in fieldlist. */
be2daae6 15330 for (i = 0; i < fip->fnfieldlists.size (); i++)
c906108c 15331 {
27bfe10e 15332 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
be2daae6
TT
15333 {
15334 flp = &fip->fnfieldlists[i];
15335 break;
15336 }
c906108c
SS
15337 }
15338
be2daae6
TT
15339 /* Create a new fnfieldlist if necessary. */
15340 if (flp == nullptr)
c906108c 15341 {
be2daae6
TT
15342 fip->fnfieldlists.emplace_back ();
15343 flp = &fip->fnfieldlists.back ();
c906108c 15344 flp->name = fieldname;
be2daae6 15345 i = fip->fnfieldlists.size () - 1;
c906108c
SS
15346 }
15347
be2daae6
TT
15348 /* Create a new member function field and add it to the vector of
15349 fnfieldlists. */
15350 flp->fnfields.emplace_back ();
15351 fnp = &flp->fnfields.back ();
3da10d80
KS
15352
15353 /* Delay processing of the physname until later. */
9c37b5ae 15354 if (cu->language == language_cplus)
be2daae6
TT
15355 add_to_method_list (type, i, flp->fnfields.size () - 1, fieldname,
15356 die, cu);
3da10d80
KS
15357 else
15358 {
1d06ead6 15359 const char *physname = dwarf2_physname (fieldname, die, cu);
3da10d80
KS
15360 fnp->physname = physname ? physname : "";
15361 }
15362
c906108c 15363 fnp->type = alloc_type (objfile);
f792889a
DJ
15364 this_type = read_type_die (die, cu);
15365 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
c906108c 15366 {
f792889a 15367 int nparams = TYPE_NFIELDS (this_type);
c906108c 15368
f792889a 15369 /* TYPE is the domain of this method, and THIS_TYPE is the type
e26fb1d7
DC
15370 of the method itself (TYPE_CODE_METHOD). */
15371 smash_to_method_type (fnp->type, type,
f792889a
DJ
15372 TYPE_TARGET_TYPE (this_type),
15373 TYPE_FIELDS (this_type),
15374 TYPE_NFIELDS (this_type),
15375 TYPE_VARARGS (this_type));
c906108c
SS
15376
15377 /* Handle static member functions.
c5aa993b 15378 Dwarf2 has no clean way to discern C++ static and non-static
0963b4bd
MS
15379 member functions. G++ helps GDB by marking the first
15380 parameter for non-static member functions (which is the this
15381 pointer) as artificial. We obtain this information from
15382 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
f792889a 15383 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
c906108c
SS
15384 fnp->voffset = VOFFSET_STATIC;
15385 }
15386 else
e2e0b3e5 15387 complaint (&symfile_complaints, _("member function type missing for '%s'"),
3da10d80 15388 dwarf2_full_name (fieldname, die, cu));
c906108c
SS
15389
15390 /* Get fcontext from DW_AT_containing_type if present. */
e142c38c 15391 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
e7c27a73 15392 fnp->fcontext = die_containing_type (die, cu);
c906108c 15393
3e43a32a
MS
15394 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
15395 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
c906108c
SS
15396
15397 /* Get accessibility. */
e142c38c 15398 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
c906108c 15399 if (attr)
aead7601 15400 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
60d5a603
JK
15401 else
15402 accessibility = dwarf2_default_access_attribute (die, cu);
15403 switch (accessibility)
c906108c 15404 {
60d5a603
JK
15405 case DW_ACCESS_private:
15406 fnp->is_private = 1;
15407 break;
15408 case DW_ACCESS_protected:
15409 fnp->is_protected = 1;
15410 break;
c906108c
SS
15411 }
15412
b02dede2 15413 /* Check for artificial methods. */
e142c38c 15414 attr = dwarf2_attr (die, DW_AT_artificial, cu);
b02dede2
DJ
15415 if (attr && DW_UNSND (attr) != 0)
15416 fnp->is_artificial = 1;
15417
7d27a96d
TT
15418 fnp->is_constructor = dwarf2_is_constructor (die, cu);
15419
0d564a31 15420 /* Get index in virtual function table if it is a virtual member
aec5aa8b
TT
15421 function. For older versions of GCC, this is an offset in the
15422 appropriate virtual table, as specified by DW_AT_containing_type.
15423 For everyone else, it is an expression to be evaluated relative
0d564a31
DJ
15424 to the object address. */
15425
e142c38c 15426 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
aec5aa8b 15427 if (attr)
8e19ed76 15428 {
aec5aa8b 15429 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size > 0)
8e19ed76 15430 {
aec5aa8b
TT
15431 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
15432 {
15433 /* Old-style GCC. */
15434 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
15435 }
15436 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
15437 || (DW_BLOCK (attr)->size > 1
15438 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
15439 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
15440 {
aec5aa8b
TT
15441 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
15442 if ((fnp->voffset % cu->header.addr_size) != 0)
15443 dwarf2_complex_location_expr_complaint ();
15444 else
15445 fnp->voffset /= cu->header.addr_size;
15446 fnp->voffset += 2;
15447 }
15448 else
15449 dwarf2_complex_location_expr_complaint ();
15450
15451 if (!fnp->fcontext)
7e993ebf
KS
15452 {
15453 /* If there is no `this' field and no DW_AT_containing_type,
15454 we cannot actually find a base class context for the
15455 vtable! */
15456 if (TYPE_NFIELDS (this_type) == 0
15457 || !TYPE_FIELD_ARTIFICIAL (this_type, 0))
15458 {
15459 complaint (&symfile_complaints,
15460 _("cannot determine context for virtual member "
9d8780f0
SM
15461 "function \"%s\" (offset %s)"),
15462 fieldname, sect_offset_str (die->sect_off));
7e993ebf
KS
15463 }
15464 else
15465 {
15466 fnp->fcontext
15467 = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
15468 }
15469 }
aec5aa8b 15470 }
3690dd37 15471 else if (attr_form_is_section_offset (attr))
8e19ed76 15472 {
4d3c2250 15473 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
15474 }
15475 else
15476 {
4d3c2250
KB
15477 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
15478 fieldname);
8e19ed76 15479 }
0d564a31 15480 }
d48cc9dd
DJ
15481 else
15482 {
15483 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
15484 if (attr && DW_UNSND (attr))
15485 {
15486 /* GCC does this, as of 2008-08-25; PR debug/37237. */
15487 complaint (&symfile_complaints,
9d8780f0 15488 _("Member function \"%s\" (offset %s) is virtual "
3e43a32a 15489 "but the vtable offset is not specified"),
9d8780f0 15490 fieldname, sect_offset_str (die->sect_off));
9655fd1a 15491 ALLOCATE_CPLUS_STRUCT_TYPE (type);
d48cc9dd
DJ
15492 TYPE_CPLUS_DYNAMIC (type) = 1;
15493 }
15494 }
c906108c
SS
15495}
15496
15497/* Create the vector of member function fields, and attach it to the type. */
15498
15499static void
fba45db2 15500dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 15501 struct dwarf2_cu *cu)
c906108c 15502{
b4ba55a1 15503 if (cu->language == language_ada)
a73c6dcd 15504 error (_("unexpected member functions in Ada type"));
b4ba55a1 15505
c906108c
SS
15506 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15507 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
be2daae6
TT
15508 TYPE_ALLOC (type,
15509 sizeof (struct fn_fieldlist) * fip->fnfieldlists.size ());
c906108c 15510
be2daae6 15511 for (int i = 0; i < fip->fnfieldlists.size (); i++)
c906108c 15512 {
be2daae6 15513 struct fnfieldlist &nf = fip->fnfieldlists[i];
c906108c 15514 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
c906108c 15515
be2daae6
TT
15516 TYPE_FN_FIELDLIST_NAME (type, i) = nf.name;
15517 TYPE_FN_FIELDLIST_LENGTH (type, i) = nf.fnfields.size ();
c906108c 15518 fn_flp->fn_fields = (struct fn_field *)
be2daae6
TT
15519 TYPE_ALLOC (type, sizeof (struct fn_field) * nf.fnfields.size ());
15520
15521 for (int k = 0; k < nf.fnfields.size (); ++k)
15522 fn_flp->fn_fields[k] = nf.fnfields[k];
c906108c
SS
15523 }
15524
be2daae6 15525 TYPE_NFN_FIELDS (type) = fip->fnfieldlists.size ();
c906108c
SS
15526}
15527
1168df01
JB
15528/* Returns non-zero if NAME is the name of a vtable member in CU's
15529 language, zero otherwise. */
15530static int
15531is_vtable_name (const char *name, struct dwarf2_cu *cu)
15532{
15533 static const char vptr[] = "_vptr";
15534
9c37b5ae
TT
15535 /* Look for the C++ form of the vtable. */
15536 if (startswith (name, vptr) && is_cplus_marker (name[sizeof (vptr) - 1]))
1168df01
JB
15537 return 1;
15538
15539 return 0;
15540}
15541
c0dd20ea 15542/* GCC outputs unnamed structures that are really pointers to member
0b92b5bb
TT
15543 functions, with the ABI-specified layout. If TYPE describes
15544 such a structure, smash it into a member function type.
61049d3b
DJ
15545
15546 GCC shouldn't do this; it should just output pointer to member DIEs.
15547 This is GCC PR debug/28767. */
c0dd20ea 15548
0b92b5bb
TT
15549static void
15550quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
c0dd20ea 15551{
09e2d7c7 15552 struct type *pfn_type, *self_type, *new_type;
c0dd20ea
DJ
15553
15554 /* Check for a structure with no name and two children. */
0b92b5bb
TT
15555 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
15556 return;
c0dd20ea
DJ
15557
15558 /* Check for __pfn and __delta members. */
0b92b5bb
TT
15559 if (TYPE_FIELD_NAME (type, 0) == NULL
15560 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
15561 || TYPE_FIELD_NAME (type, 1) == NULL
15562 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
15563 return;
c0dd20ea
DJ
15564
15565 /* Find the type of the method. */
0b92b5bb 15566 pfn_type = TYPE_FIELD_TYPE (type, 0);
c0dd20ea
DJ
15567 if (pfn_type == NULL
15568 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
15569 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
0b92b5bb 15570 return;
c0dd20ea
DJ
15571
15572 /* Look for the "this" argument. */
15573 pfn_type = TYPE_TARGET_TYPE (pfn_type);
15574 if (TYPE_NFIELDS (pfn_type) == 0
0b92b5bb 15575 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
c0dd20ea 15576 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
0b92b5bb 15577 return;
c0dd20ea 15578
09e2d7c7 15579 self_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
0b92b5bb 15580 new_type = alloc_type (objfile);
09e2d7c7 15581 smash_to_method_type (new_type, self_type, TYPE_TARGET_TYPE (pfn_type),
c0dd20ea
DJ
15582 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
15583 TYPE_VARARGS (pfn_type));
0b92b5bb 15584 smash_to_methodptr_type (type, new_type);
c0dd20ea 15585}
1168df01 15586
2b4424c3
TT
15587/* If the DIE has a DW_AT_alignment attribute, return its value, doing
15588 appropriate error checking and issuing complaints if there is a
15589 problem. */
15590
15591static ULONGEST
15592get_alignment (struct dwarf2_cu *cu, struct die_info *die)
15593{
15594 struct attribute *attr = dwarf2_attr (die, DW_AT_alignment, cu);
15595
15596 if (attr == nullptr)
15597 return 0;
15598
15599 if (!attr_form_is_constant (attr))
15600 {
15601 complaint (&symfile_complaints,
15602 _("DW_AT_alignment must have constant form"
15603 " - DIE at %s [in module %s]"),
15604 sect_offset_str (die->sect_off),
15605 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15606 return 0;
15607 }
15608
15609 ULONGEST align;
15610 if (attr->form == DW_FORM_sdata)
15611 {
15612 LONGEST val = DW_SND (attr);
15613 if (val < 0)
15614 {
15615 complaint (&symfile_complaints,
15616 _("DW_AT_alignment value must not be negative"
15617 " - DIE at %s [in module %s]"),
15618 sect_offset_str (die->sect_off),
15619 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15620 return 0;
15621 }
15622 align = val;
15623 }
15624 else
15625 align = DW_UNSND (attr);
15626
15627 if (align == 0)
15628 {
15629 complaint (&symfile_complaints,
15630 _("DW_AT_alignment value must not be zero"
15631 " - DIE at %s [in module %s]"),
15632 sect_offset_str (die->sect_off),
15633 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15634 return 0;
15635 }
15636 if ((align & (align - 1)) != 0)
15637 {
15638 complaint (&symfile_complaints,
15639 _("DW_AT_alignment value must be a power of 2"
15640 " - DIE at %s [in module %s]"),
15641 sect_offset_str (die->sect_off),
15642 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15643 return 0;
15644 }
15645
15646 return align;
15647}
15648
15649/* If the DIE has a DW_AT_alignment attribute, use its value to set
15650 the alignment for TYPE. */
15651
15652static void
15653maybe_set_alignment (struct dwarf2_cu *cu, struct die_info *die,
15654 struct type *type)
15655{
15656 if (!set_type_align (type, get_alignment (cu, die)))
15657 complaint (&symfile_complaints,
15658 _("DW_AT_alignment value too large"
15659 " - DIE at %s [in module %s]"),
15660 sect_offset_str (die->sect_off),
15661 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
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{
518817b3 15679 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->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 }
2ddeaf8a
TT
15737 else if (die->tag == DW_TAG_variant_part)
15738 {
15739 TYPE_CODE (type) = TYPE_CODE_UNION;
15740 TYPE_FLAG_DISCRIMINATED_UNION (type) = 1;
15741 }
c906108c
SS
15742 else
15743 {
4753d33b 15744 TYPE_CODE (type) = TYPE_CODE_STRUCT;
c906108c
SS
15745 }
15746
0cc2414c
TT
15747 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
15748 TYPE_DECLARED_CLASS (type) = 1;
15749
e142c38c 15750 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
15751 if (attr)
15752 {
155bfbd3
JB
15753 if (attr_form_is_constant (attr))
15754 TYPE_LENGTH (type) = DW_UNSND (attr);
15755 else
15756 {
15757 /* For the moment, dynamic type sizes are not supported
15758 by GDB's struct type. The actual size is determined
15759 on-demand when resolving the type of a given object,
15760 so set the type's length to zero for now. Otherwise,
15761 we record an expression as the length, and that expression
15762 could lead to a very large value, which could eventually
15763 lead to us trying to allocate that much memory when creating
15764 a value of that type. */
15765 TYPE_LENGTH (type) = 0;
15766 }
c906108c
SS
15767 }
15768 else
15769 {
15770 TYPE_LENGTH (type) = 0;
15771 }
15772
2b4424c3
TT
15773 maybe_set_alignment (cu, die, type);
15774
5230b05a 15775 if (producer_is_icc_lt_14 (cu) && (TYPE_LENGTH (type) == 0))
685b1105 15776 {
5230b05a
WT
15777 /* ICC<14 does not output the required DW_AT_declaration on
15778 incomplete types, but gives them a size of zero. */
422b1cb0 15779 TYPE_STUB (type) = 1;
685b1105
JK
15780 }
15781 else
15782 TYPE_STUB_SUPPORTED (type) = 1;
15783
dc718098 15784 if (die_is_declaration (die, cu))
876cecd0 15785 TYPE_STUB (type) = 1;
a6c727b2
DJ
15786 else if (attr == NULL && die->child == NULL
15787 && producer_is_realview (cu->producer))
15788 /* RealView does not output the required DW_AT_declaration
15789 on incomplete types. */
15790 TYPE_STUB (type) = 1;
dc718098 15791
c906108c
SS
15792 /* We need to add the type field to the die immediately so we don't
15793 infinitely recurse when dealing with pointers to the structure
0963b4bd 15794 type within the structure itself. */
1c379e20 15795 set_die_type (die, type, cu);
c906108c 15796
7e314c57
JK
15797 /* set_die_type should be already done. */
15798 set_descriptive_type (type, die, cu);
15799
c767944b
DJ
15800 return type;
15801}
15802
2ddeaf8a
TT
15803/* A helper for process_structure_scope that handles a single member
15804 DIE. */
15805
15806static void
15807handle_struct_member_die (struct die_info *child_die, struct type *type,
15808 struct field_info *fi,
15809 std::vector<struct symbol *> *template_args,
15810 struct dwarf2_cu *cu)
15811{
15812 if (child_die->tag == DW_TAG_member
15813 || child_die->tag == DW_TAG_variable
15814 || child_die->tag == DW_TAG_variant_part)
15815 {
15816 /* NOTE: carlton/2002-11-05: A C++ static data member
15817 should be a DW_TAG_member that is a declaration, but
15818 all versions of G++ as of this writing (so through at
15819 least 3.2.1) incorrectly generate DW_TAG_variable
15820 tags for them instead. */
15821 dwarf2_add_field (fi, child_die, cu);
15822 }
15823 else if (child_die->tag == DW_TAG_subprogram)
15824 {
15825 /* Rust doesn't have member functions in the C++ sense.
15826 However, it does emit ordinary functions as children
15827 of a struct DIE. */
15828 if (cu->language == language_rust)
15829 read_func_scope (child_die, cu);
15830 else
15831 {
15832 /* C++ member function. */
15833 dwarf2_add_member_fn (fi, child_die, type, cu);
15834 }
15835 }
15836 else if (child_die->tag == DW_TAG_inheritance)
15837 {
15838 /* C++ base class field. */
15839 dwarf2_add_field (fi, child_die, cu);
15840 }
15841 else if (type_can_define_types (child_die))
15842 dwarf2_add_type_defn (fi, child_die, cu);
15843 else if (child_die->tag == DW_TAG_template_type_param
15844 || child_die->tag == DW_TAG_template_value_param)
15845 {
15846 struct symbol *arg = new_symbol (child_die, NULL, cu);
15847
15848 if (arg != NULL)
15849 template_args->push_back (arg);
15850 }
15851 else if (child_die->tag == DW_TAG_variant)
15852 {
15853 /* In a variant we want to get the discriminant and also add a
15854 field for our sole member child. */
15855 struct attribute *discr = dwarf2_attr (child_die, DW_AT_discr_value, cu);
15856
15857 for (struct die_info *variant_child = child_die->child;
15858 variant_child != NULL;
15859 variant_child = sibling_die (variant_child))
15860 {
15861 if (variant_child->tag == DW_TAG_member)
15862 {
15863 handle_struct_member_die (variant_child, type, fi,
15864 template_args, cu);
15865 /* Only handle the one. */
15866 break;
15867 }
15868 }
15869
15870 /* We don't handle this but we might as well report it if we see
15871 it. */
15872 if (dwarf2_attr (child_die, DW_AT_discr_list, cu) != nullptr)
15873 complaint (&symfile_complaints,
15874 _("DW_AT_discr_list is not supported yet"
15875 " - DIE at %s [in module %s]"),
15876 sect_offset_str (child_die->sect_off),
15877 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15878
15879 /* The first field was just added, so we can stash the
15880 discriminant there. */
be2daae6 15881 gdb_assert (!fi->fields.empty ());
2ddeaf8a 15882 if (discr == NULL)
be2daae6 15883 fi->fields.back ().variant.default_branch = true;
2ddeaf8a 15884 else
be2daae6 15885 fi->fields.back ().variant.discriminant_value = DW_UNSND (discr);
2ddeaf8a
TT
15886 }
15887}
15888
c767944b
DJ
15889/* Finish creating a structure or union type, including filling in
15890 its members and creating a symbol for it. */
15891
15892static void
15893process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
15894{
518817b3 15895 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
ca040673 15896 struct die_info *child_die;
c767944b
DJ
15897 struct type *type;
15898
15899 type = get_die_type (die, cu);
15900 if (type == NULL)
15901 type = read_structure_type (die, cu);
15902
2ddeaf8a
TT
15903 /* When reading a DW_TAG_variant_part, we need to notice when we
15904 read the discriminant member, so we can record it later in the
15905 discriminant_info. */
15906 bool is_variant_part = TYPE_FLAG_DISCRIMINATED_UNION (type);
15907 sect_offset discr_offset;
15908
15909 if (is_variant_part)
15910 {
15911 struct attribute *discr = dwarf2_attr (die, DW_AT_discr, cu);
15912 if (discr == NULL)
15913 {
15914 /* Maybe it's a univariant form, an extension we support.
15915 In this case arrange not to check the offset. */
15916 is_variant_part = false;
15917 }
15918 else if (attr_form_is_ref (discr))
15919 {
15920 struct dwarf2_cu *target_cu = cu;
15921 struct die_info *target_die = follow_die_ref (die, discr, &target_cu);
15922
15923 discr_offset = target_die->sect_off;
15924 }
15925 else
15926 {
15927 complaint (&symfile_complaints,
15928 _("DW_AT_discr does not have DIE reference form"
15929 " - DIE at %s [in module %s]"),
15930 sect_offset_str (die->sect_off),
15931 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15932 is_variant_part = false;
15933 }
15934 }
15935
e142c38c 15936 if (die->child != NULL && ! die_is_declaration (die, cu))
c906108c
SS
15937 {
15938 struct field_info fi;
2f4732b0 15939 std::vector<struct symbol *> template_args;
c906108c 15940
639d11d3 15941 child_die = die->child;
c906108c
SS
15942
15943 while (child_die && child_die->tag)
15944 {
2ddeaf8a 15945 handle_struct_member_die (child_die, type, &fi, &template_args, cu);
34eaf542 15946
2ddeaf8a 15947 if (is_variant_part && discr_offset == child_die->sect_off)
be2daae6 15948 fi.fields.back ().variant.is_discriminant = true;
34eaf542 15949
c906108c
SS
15950 child_die = sibling_die (child_die);
15951 }
15952
34eaf542 15953 /* Attach template arguments to type. */
2f4732b0 15954 if (!template_args.empty ())
34eaf542
TT
15955 {
15956 ALLOCATE_CPLUS_STRUCT_TYPE (type);
2f4732b0 15957 TYPE_N_TEMPLATE_ARGUMENTS (type) = template_args.size ();
34eaf542 15958 TYPE_TEMPLATE_ARGUMENTS (type)
8d749320
SM
15959 = XOBNEWVEC (&objfile->objfile_obstack,
15960 struct symbol *,
15961 TYPE_N_TEMPLATE_ARGUMENTS (type));
34eaf542 15962 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
2f4732b0 15963 template_args.data (),
34eaf542
TT
15964 (TYPE_N_TEMPLATE_ARGUMENTS (type)
15965 * sizeof (struct symbol *)));
34eaf542
TT
15966 }
15967
c906108c
SS
15968 /* Attach fields and member functions to the type. */
15969 if (fi.nfields)
e7c27a73 15970 dwarf2_attach_fields_to_type (&fi, type, cu);
be2daae6 15971 if (!fi.fnfieldlists.empty ())
c906108c 15972 {
e7c27a73 15973 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
c906108c 15974
c5aa993b 15975 /* Get the type which refers to the base class (possibly this
c906108c 15976 class itself) which contains the vtable pointer for the current
0d564a31
DJ
15977 class from the DW_AT_containing_type attribute. This use of
15978 DW_AT_containing_type is a GNU extension. */
c906108c 15979
e142c38c 15980 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
c906108c 15981 {
e7c27a73 15982 struct type *t = die_containing_type (die, cu);
c906108c 15983
ae6ae975 15984 set_type_vptr_basetype (type, t);
c906108c
SS
15985 if (type == t)
15986 {
c906108c
SS
15987 int i;
15988
15989 /* Our own class provides vtbl ptr. */
15990 for (i = TYPE_NFIELDS (t) - 1;
15991 i >= TYPE_N_BASECLASSES (t);
15992 --i)
15993 {
0d5cff50 15994 const char *fieldname = TYPE_FIELD_NAME (t, i);
c906108c 15995
1168df01 15996 if (is_vtable_name (fieldname, cu))
c906108c 15997 {
ae6ae975 15998 set_type_vptr_fieldno (type, i);
c906108c
SS
15999 break;
16000 }
16001 }
16002
16003 /* Complain if virtual function table field not found. */
16004 if (i < TYPE_N_BASECLASSES (t))
4d3c2250 16005 complaint (&symfile_complaints,
3e43a32a
MS
16006 _("virtual function table pointer "
16007 "not found when defining class '%s'"),
4d3c2250
KB
16008 TYPE_TAG_NAME (type) ? TYPE_TAG_NAME (type) :
16009 "");
c906108c
SS
16010 }
16011 else
16012 {
ae6ae975 16013 set_type_vptr_fieldno (type, TYPE_VPTR_FIELDNO (t));
c906108c
SS
16014 }
16015 }
f6235d4c 16016 else if (cu->producer
61012eef 16017 && startswith (cu->producer, "IBM(R) XL C/C++ Advanced Edition"))
f6235d4c
EZ
16018 {
16019 /* The IBM XLC compiler does not provide direct indication
16020 of the containing type, but the vtable pointer is
16021 always named __vfp. */
16022
16023 int i;
16024
16025 for (i = TYPE_NFIELDS (type) - 1;
16026 i >= TYPE_N_BASECLASSES (type);
16027 --i)
16028 {
16029 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
16030 {
ae6ae975
DE
16031 set_type_vptr_fieldno (type, i);
16032 set_type_vptr_basetype (type, type);
f6235d4c
EZ
16033 break;
16034 }
16035 }
16036 }
c906108c 16037 }
98751a41
JK
16038
16039 /* Copy fi.typedef_field_list linked list elements content into the
16040 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
be2daae6 16041 if (!fi.typedef_field_list.empty ())
98751a41 16042 {
be2daae6 16043 int count = fi.typedef_field_list.size ();
98751a41 16044
a0d7a4ff 16045 ALLOCATE_CPLUS_STRUCT_TYPE (type);
98751a41 16046 TYPE_TYPEDEF_FIELD_ARRAY (type)
883fd55a 16047 = ((struct decl_field *)
be2daae6
TT
16048 TYPE_ALLOC (type,
16049 sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * count));
16050 TYPE_TYPEDEF_FIELD_COUNT (type) = count;
6e70227d 16051
be2daae6
TT
16052 for (int i = 0; i < fi.typedef_field_list.size (); ++i)
16053 TYPE_TYPEDEF_FIELD (type, i) = fi.typedef_field_list[i];
98751a41 16054 }
c767944b 16055
883fd55a
KS
16056 /* Copy fi.nested_types_list linked list elements content into the
16057 allocated array TYPE_NESTED_TYPES_ARRAY (type). */
be2daae6 16058 if (!fi.nested_types_list.empty () && cu->language != language_ada)
883fd55a 16059 {
be2daae6 16060 int count = fi.nested_types_list.size ();
883fd55a
KS
16061
16062 ALLOCATE_CPLUS_STRUCT_TYPE (type);
16063 TYPE_NESTED_TYPES_ARRAY (type)
16064 = ((struct decl_field *)
be2daae6
TT
16065 TYPE_ALLOC (type, sizeof (struct decl_field) * count));
16066 TYPE_NESTED_TYPES_COUNT (type) = count;
883fd55a 16067
be2daae6
TT
16068 for (int i = 0; i < fi.nested_types_list.size (); ++i)
16069 TYPE_NESTED_TYPES_FIELD (type, i) = fi.nested_types_list[i];
883fd55a 16070 }
c906108c 16071 }
63d06c5c 16072
bb5ed363 16073 quirk_gcc_member_function_pointer (type, objfile);
c9317f21
TT
16074 if (cu->language == language_rust && die->tag == DW_TAG_union_type)
16075 cu->rust_unions.push_back (type);
0b92b5bb 16076
90aeadfc
DC
16077 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
16078 snapshots) has been known to create a die giving a declaration
16079 for a class that has, as a child, a die giving a definition for a
16080 nested class. So we have to process our children even if the
16081 current die is a declaration. Normally, of course, a declaration
16082 won't have any children at all. */
134d01f1 16083
ca040673
DE
16084 child_die = die->child;
16085
90aeadfc
DC
16086 while (child_die != NULL && child_die->tag)
16087 {
16088 if (child_die->tag == DW_TAG_member
16089 || child_die->tag == DW_TAG_variable
34eaf542
TT
16090 || child_die->tag == DW_TAG_inheritance
16091 || child_die->tag == DW_TAG_template_value_param
16092 || child_die->tag == DW_TAG_template_type_param)
134d01f1 16093 {
90aeadfc 16094 /* Do nothing. */
134d01f1 16095 }
90aeadfc
DC
16096 else
16097 process_die (child_die, cu);
134d01f1 16098
90aeadfc 16099 child_die = sibling_die (child_die);
134d01f1
DJ
16100 }
16101
fa4028e9
JB
16102 /* Do not consider external references. According to the DWARF standard,
16103 these DIEs are identified by the fact that they have no byte_size
16104 attribute, and a declaration attribute. */
16105 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
16106 || !die_is_declaration (die, cu))
c767944b 16107 new_symbol (die, type, cu);
134d01f1
DJ
16108}
16109
55426c9d
JB
16110/* Assuming DIE is an enumeration type, and TYPE is its associated type,
16111 update TYPE using some information only available in DIE's children. */
16112
16113static void
16114update_enumeration_type_from_children (struct die_info *die,
16115 struct type *type,
16116 struct dwarf2_cu *cu)
16117{
60f7655a 16118 struct die_info *child_die;
55426c9d
JB
16119 int unsigned_enum = 1;
16120 int flag_enum = 1;
16121 ULONGEST mask = 0;
55426c9d 16122
8268c778 16123 auto_obstack obstack;
55426c9d 16124
60f7655a
DE
16125 for (child_die = die->child;
16126 child_die != NULL && child_die->tag;
16127 child_die = sibling_die (child_die))
55426c9d
JB
16128 {
16129 struct attribute *attr;
16130 LONGEST value;
16131 const gdb_byte *bytes;
16132 struct dwarf2_locexpr_baton *baton;
16133 const char *name;
60f7655a 16134
55426c9d
JB
16135 if (child_die->tag != DW_TAG_enumerator)
16136 continue;
16137
16138 attr = dwarf2_attr (child_die, DW_AT_const_value, cu);
16139 if (attr == NULL)
16140 continue;
16141
16142 name = dwarf2_name (child_die, cu);
16143 if (name == NULL)
16144 name = "<anonymous enumerator>";
16145
16146 dwarf2_const_value_attr (attr, type, name, &obstack, cu,
16147 &value, &bytes, &baton);
16148 if (value < 0)
16149 {
16150 unsigned_enum = 0;
16151 flag_enum = 0;
16152 }
16153 else if ((mask & value) != 0)
16154 flag_enum = 0;
16155 else
16156 mask |= value;
16157
16158 /* If we already know that the enum type is neither unsigned, nor
16159 a flag type, no need to look at the rest of the enumerates. */
16160 if (!unsigned_enum && !flag_enum)
16161 break;
55426c9d
JB
16162 }
16163
16164 if (unsigned_enum)
16165 TYPE_UNSIGNED (type) = 1;
16166 if (flag_enum)
16167 TYPE_FLAG_ENUM (type) = 1;
55426c9d
JB
16168}
16169
134d01f1
DJ
16170/* Given a DW_AT_enumeration_type die, set its type. We do not
16171 complete the type's fields yet, or create any symbols. */
c906108c 16172
f792889a 16173static struct type *
134d01f1 16174read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16175{
518817b3 16176 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c 16177 struct type *type;
c906108c 16178 struct attribute *attr;
0114d602 16179 const char *name;
134d01f1 16180
348e048f
DE
16181 /* If the definition of this type lives in .debug_types, read that type.
16182 Don't follow DW_AT_specification though, that will take us back up
16183 the chain and we want to go down. */
45e58e77 16184 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
348e048f
DE
16185 if (attr)
16186 {
ac9ec31b 16187 type = get_DW_AT_signature_type (die, attr, cu);
9dc481d3 16188
ac9ec31b 16189 /* The type's CU may not be the same as CU.
02142a6c 16190 Ensure TYPE is recorded with CU in die_type_hash. */
348e048f
DE
16191 return set_die_type (die, type, cu);
16192 }
16193
c906108c
SS
16194 type = alloc_type (objfile);
16195
16196 TYPE_CODE (type) = TYPE_CODE_ENUM;
94af9270 16197 name = dwarf2_full_name (NULL, die, cu);
39cbfefa 16198 if (name != NULL)
7d455152 16199 TYPE_TAG_NAME (type) = name;
c906108c 16200
0626fc76
TT
16201 attr = dwarf2_attr (die, DW_AT_type, cu);
16202 if (attr != NULL)
16203 {
16204 struct type *underlying_type = die_type (die, cu);
16205
16206 TYPE_TARGET_TYPE (type) = underlying_type;
16207 }
16208
e142c38c 16209 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
16210 if (attr)
16211 {
16212 TYPE_LENGTH (type) = DW_UNSND (attr);
16213 }
16214 else
16215 {
16216 TYPE_LENGTH (type) = 0;
16217 }
16218
2b4424c3
TT
16219 maybe_set_alignment (cu, die, type);
16220
137033e9
JB
16221 /* The enumeration DIE can be incomplete. In Ada, any type can be
16222 declared as private in the package spec, and then defined only
16223 inside the package body. Such types are known as Taft Amendment
16224 Types. When another package uses such a type, an incomplete DIE
16225 may be generated by the compiler. */
02eb380e 16226 if (die_is_declaration (die, cu))
876cecd0 16227 TYPE_STUB (type) = 1;
02eb380e 16228
0626fc76
TT
16229 /* Finish the creation of this type by using the enum's children.
16230 We must call this even when the underlying type has been provided
16231 so that we can determine if we're looking at a "flag" enum. */
55426c9d
JB
16232 update_enumeration_type_from_children (die, type, cu);
16233
0626fc76
TT
16234 /* If this type has an underlying type that is not a stub, then we
16235 may use its attributes. We always use the "unsigned" attribute
16236 in this situation, because ordinarily we guess whether the type
16237 is unsigned -- but the guess can be wrong and the underlying type
16238 can tell us the reality. However, we defer to a local size
16239 attribute if one exists, because this lets the compiler override
16240 the underlying type if needed. */
16241 if (TYPE_TARGET_TYPE (type) != NULL && !TYPE_STUB (TYPE_TARGET_TYPE (type)))
16242 {
16243 TYPE_UNSIGNED (type) = TYPE_UNSIGNED (TYPE_TARGET_TYPE (type));
16244 if (TYPE_LENGTH (type) == 0)
16245 TYPE_LENGTH (type) = TYPE_LENGTH (TYPE_TARGET_TYPE (type));
2b4424c3
TT
16246 if (TYPE_RAW_ALIGN (type) == 0
16247 && TYPE_RAW_ALIGN (TYPE_TARGET_TYPE (type)) != 0)
16248 set_type_align (type, TYPE_RAW_ALIGN (TYPE_TARGET_TYPE (type)));
0626fc76
TT
16249 }
16250
3d567982
TT
16251 TYPE_DECLARED_CLASS (type) = dwarf2_flag_true_p (die, DW_AT_enum_class, cu);
16252
f792889a 16253 return set_die_type (die, type, cu);
134d01f1
DJ
16254}
16255
16256/* Given a pointer to a die which begins an enumeration, process all
16257 the dies that define the members of the enumeration, and create the
16258 symbol for the enumeration type.
16259
16260 NOTE: We reverse the order of the element list. */
16261
16262static void
16263process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
16264{
f792889a 16265 struct type *this_type;
134d01f1 16266
f792889a
DJ
16267 this_type = get_die_type (die, cu);
16268 if (this_type == NULL)
16269 this_type = read_enumeration_type (die, cu);
9dc481d3 16270
639d11d3 16271 if (die->child != NULL)
c906108c 16272 {
9dc481d3
DE
16273 struct die_info *child_die;
16274 struct symbol *sym;
16275 struct field *fields = NULL;
16276 int num_fields = 0;
15d034d0 16277 const char *name;
9dc481d3 16278
639d11d3 16279 child_die = die->child;
c906108c
SS
16280 while (child_die && child_die->tag)
16281 {
16282 if (child_die->tag != DW_TAG_enumerator)
16283 {
e7c27a73 16284 process_die (child_die, cu);
c906108c
SS
16285 }
16286 else
16287 {
39cbfefa
DJ
16288 name = dwarf2_name (child_die, cu);
16289 if (name)
c906108c 16290 {
f792889a 16291 sym = new_symbol (child_die, this_type, cu);
c906108c
SS
16292
16293 if ((num_fields % DW_FIELD_ALLOC_CHUNK) == 0)
16294 {
16295 fields = (struct field *)
16296 xrealloc (fields,
16297 (num_fields + DW_FIELD_ALLOC_CHUNK)
c5aa993b 16298 * sizeof (struct field));
c906108c
SS
16299 }
16300
3567439c 16301 FIELD_NAME (fields[num_fields]) = SYMBOL_LINKAGE_NAME (sym);
c906108c 16302 FIELD_TYPE (fields[num_fields]) = NULL;
14e75d8e 16303 SET_FIELD_ENUMVAL (fields[num_fields], SYMBOL_VALUE (sym));
c906108c
SS
16304 FIELD_BITSIZE (fields[num_fields]) = 0;
16305
16306 num_fields++;
16307 }
16308 }
16309
16310 child_die = sibling_die (child_die);
16311 }
16312
16313 if (num_fields)
16314 {
f792889a
DJ
16315 TYPE_NFIELDS (this_type) = num_fields;
16316 TYPE_FIELDS (this_type) = (struct field *)
16317 TYPE_ALLOC (this_type, sizeof (struct field) * num_fields);
16318 memcpy (TYPE_FIELDS (this_type), fields,
c906108c 16319 sizeof (struct field) * num_fields);
b8c9b27d 16320 xfree (fields);
c906108c 16321 }
c906108c 16322 }
134d01f1 16323
6c83ed52
TT
16324 /* If we are reading an enum from a .debug_types unit, and the enum
16325 is a declaration, and the enum is not the signatured type in the
16326 unit, then we do not want to add a symbol for it. Adding a
16327 symbol would in some cases obscure the true definition of the
16328 enum, giving users an incomplete type when the definition is
16329 actually available. Note that we do not want to do this for all
16330 enums which are just declarations, because C++0x allows forward
16331 enum declarations. */
3019eac3 16332 if (cu->per_cu->is_debug_types
6c83ed52
TT
16333 && die_is_declaration (die, cu))
16334 {
52dc124a 16335 struct signatured_type *sig_type;
6c83ed52 16336
c0f78cd4 16337 sig_type = (struct signatured_type *) cu->per_cu;
9c541725
PA
16338 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
16339 if (sig_type->type_offset_in_section != die->sect_off)
6c83ed52
TT
16340 return;
16341 }
16342
f792889a 16343 new_symbol (die, this_type, cu);
c906108c
SS
16344}
16345
16346/* Extract all information from a DW_TAG_array_type DIE and put it in
16347 the DIE's type field. For now, this only handles one dimensional
16348 arrays. */
16349
f792889a 16350static struct type *
e7c27a73 16351read_array_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16352{
518817b3 16353 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c 16354 struct die_info *child_die;
7e314c57 16355 struct type *type;
c906108c 16356 struct type *element_type, *range_type, *index_type;
c906108c 16357 struct attribute *attr;
15d034d0 16358 const char *name;
a405673c 16359 struct dynamic_prop *byte_stride_prop = NULL;
dc53a7ad 16360 unsigned int bit_stride = 0;
c906108c 16361
e7c27a73 16362 element_type = die_type (die, cu);
c906108c 16363
7e314c57
JK
16364 /* The die_type call above may have already set the type for this DIE. */
16365 type = get_die_type (die, cu);
16366 if (type)
16367 return type;
16368
dc53a7ad
JB
16369 attr = dwarf2_attr (die, DW_AT_byte_stride, cu);
16370 if (attr != NULL)
a405673c
JB
16371 {
16372 int stride_ok;
16373
16374 byte_stride_prop
16375 = (struct dynamic_prop *) alloca (sizeof (struct dynamic_prop));
16376 stride_ok = attr_to_dynamic_prop (attr, die, cu, byte_stride_prop);
16377 if (!stride_ok)
16378 {
16379 complaint (&symfile_complaints,
16380 _("unable to read array DW_AT_byte_stride "
9d8780f0
SM
16381 " - DIE at %s [in module %s]"),
16382 sect_offset_str (die->sect_off),
518817b3 16383 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
a405673c
JB
16384 /* Ignore this attribute. We will likely not be able to print
16385 arrays of this type correctly, but there is little we can do
16386 to help if we cannot read the attribute's value. */
16387 byte_stride_prop = NULL;
16388 }
16389 }
dc53a7ad
JB
16390
16391 attr = dwarf2_attr (die, DW_AT_bit_stride, cu);
16392 if (attr != NULL)
16393 bit_stride = DW_UNSND (attr);
16394
c906108c
SS
16395 /* Irix 6.2 native cc creates array types without children for
16396 arrays with unspecified length. */
639d11d3 16397 if (die->child == NULL)
c906108c 16398 {
46bf5051 16399 index_type = objfile_type (objfile)->builtin_int;
0c9c3474 16400 range_type = create_static_range_type (NULL, index_type, 0, -1);
dc53a7ad 16401 type = create_array_type_with_stride (NULL, element_type, range_type,
a405673c 16402 byte_stride_prop, bit_stride);
f792889a 16403 return set_die_type (die, type, cu);
c906108c
SS
16404 }
16405
791afaa2 16406 std::vector<struct type *> range_types;
639d11d3 16407 child_die = die->child;
c906108c
SS
16408 while (child_die && child_die->tag)
16409 {
16410 if (child_die->tag == DW_TAG_subrange_type)
16411 {
f792889a 16412 struct type *child_type = read_type_die (child_die, cu);
9a619af0 16413
f792889a 16414 if (child_type != NULL)
a02abb62 16415 {
0963b4bd
MS
16416 /* The range type was succesfully read. Save it for the
16417 array type creation. */
791afaa2 16418 range_types.push_back (child_type);
a02abb62 16419 }
c906108c
SS
16420 }
16421 child_die = sibling_die (child_die);
16422 }
16423
16424 /* Dwarf2 dimensions are output from left to right, create the
16425 necessary array types in backwards order. */
7ca2d3a3 16426
c906108c 16427 type = element_type;
7ca2d3a3
DL
16428
16429 if (read_array_order (die, cu) == DW_ORD_col_major)
16430 {
16431 int i = 0;
9a619af0 16432
791afaa2 16433 while (i < range_types.size ())
dc53a7ad 16434 type = create_array_type_with_stride (NULL, type, range_types[i++],
a405673c 16435 byte_stride_prop, bit_stride);
7ca2d3a3
DL
16436 }
16437 else
16438 {
791afaa2 16439 size_t ndim = range_types.size ();
7ca2d3a3 16440 while (ndim-- > 0)
dc53a7ad 16441 type = create_array_type_with_stride (NULL, type, range_types[ndim],
a405673c 16442 byte_stride_prop, bit_stride);
7ca2d3a3 16443 }
c906108c 16444
f5f8a009
EZ
16445 /* Understand Dwarf2 support for vector types (like they occur on
16446 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
16447 array type. This is not part of the Dwarf2/3 standard yet, but a
16448 custom vendor extension. The main difference between a regular
16449 array and the vector variant is that vectors are passed by value
16450 to functions. */
e142c38c 16451 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
f5f8a009 16452 if (attr)
ea37ba09 16453 make_vector_type (type);
f5f8a009 16454
dbc98a8b
KW
16455 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
16456 implementation may choose to implement triple vectors using this
16457 attribute. */
16458 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16459 if (attr)
16460 {
16461 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
16462 TYPE_LENGTH (type) = DW_UNSND (attr);
16463 else
3e43a32a
MS
16464 complaint (&symfile_complaints,
16465 _("DW_AT_byte_size for array type smaller "
16466 "than the total size of elements"));
dbc98a8b
KW
16467 }
16468
39cbfefa
DJ
16469 name = dwarf2_name (die, cu);
16470 if (name)
16471 TYPE_NAME (type) = name;
6e70227d 16472
2b4424c3
TT
16473 maybe_set_alignment (cu, die, type);
16474
0963b4bd 16475 /* Install the type in the die. */
7e314c57
JK
16476 set_die_type (die, type, cu);
16477
16478 /* set_die_type should be already done. */
b4ba55a1
JB
16479 set_descriptive_type (type, die, cu);
16480
7e314c57 16481 return type;
c906108c
SS
16482}
16483
7ca2d3a3 16484static enum dwarf_array_dim_ordering
6e70227d 16485read_array_order (struct die_info *die, struct dwarf2_cu *cu)
7ca2d3a3
DL
16486{
16487 struct attribute *attr;
16488
16489 attr = dwarf2_attr (die, DW_AT_ordering, cu);
16490
aead7601
SM
16491 if (attr)
16492 return (enum dwarf_array_dim_ordering) DW_SND (attr);
7ca2d3a3 16493
0963b4bd
MS
16494 /* GNU F77 is a special case, as at 08/2004 array type info is the
16495 opposite order to the dwarf2 specification, but data is still
16496 laid out as per normal fortran.
7ca2d3a3 16497
0963b4bd
MS
16498 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
16499 version checking. */
7ca2d3a3 16500
905e0470
PM
16501 if (cu->language == language_fortran
16502 && cu->producer && strstr (cu->producer, "GNU F77"))
7ca2d3a3
DL
16503 {
16504 return DW_ORD_row_major;
16505 }
16506
6e70227d 16507 switch (cu->language_defn->la_array_ordering)
7ca2d3a3
DL
16508 {
16509 case array_column_major:
16510 return DW_ORD_col_major;
16511 case array_row_major:
16512 default:
16513 return DW_ORD_row_major;
16514 };
16515}
16516
72019c9c 16517/* Extract all information from a DW_TAG_set_type DIE and put it in
0963b4bd 16518 the DIE's type field. */
72019c9c 16519
f792889a 16520static struct type *
72019c9c
GM
16521read_set_type (struct die_info *die, struct dwarf2_cu *cu)
16522{
7e314c57
JK
16523 struct type *domain_type, *set_type;
16524 struct attribute *attr;
f792889a 16525
7e314c57
JK
16526 domain_type = die_type (die, cu);
16527
16528 /* The die_type call above may have already set the type for this DIE. */
16529 set_type = get_die_type (die, cu);
16530 if (set_type)
16531 return set_type;
16532
16533 set_type = create_set_type (NULL, domain_type);
16534
16535 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
d09039dd
PM
16536 if (attr)
16537 TYPE_LENGTH (set_type) = DW_UNSND (attr);
7e314c57 16538
2b4424c3
TT
16539 maybe_set_alignment (cu, die, set_type);
16540
f792889a 16541 return set_die_type (die, set_type, cu);
72019c9c 16542}
7ca2d3a3 16543
0971de02
TT
16544/* A helper for read_common_block that creates a locexpr baton.
16545 SYM is the symbol which we are marking as computed.
16546 COMMON_DIE is the DIE for the common block.
16547 COMMON_LOC is the location expression attribute for the common
16548 block itself.
16549 MEMBER_LOC is the location expression attribute for the particular
16550 member of the common block that we are processing.
16551 CU is the CU from which the above come. */
16552
16553static void
16554mark_common_block_symbol_computed (struct symbol *sym,
16555 struct die_info *common_die,
16556 struct attribute *common_loc,
16557 struct attribute *member_loc,
16558 struct dwarf2_cu *cu)
16559{
518817b3
SM
16560 struct dwarf2_per_objfile *dwarf2_per_objfile
16561 = cu->per_cu->dwarf2_per_objfile;
0971de02
TT
16562 struct objfile *objfile = dwarf2_per_objfile->objfile;
16563 struct dwarf2_locexpr_baton *baton;
16564 gdb_byte *ptr;
16565 unsigned int cu_off;
16566 enum bfd_endian byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
16567 LONGEST offset = 0;
16568
16569 gdb_assert (common_loc && member_loc);
16570 gdb_assert (attr_form_is_block (common_loc));
16571 gdb_assert (attr_form_is_block (member_loc)
16572 || attr_form_is_constant (member_loc));
16573
8d749320 16574 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
0971de02
TT
16575 baton->per_cu = cu->per_cu;
16576 gdb_assert (baton->per_cu);
16577
16578 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
16579
16580 if (attr_form_is_constant (member_loc))
16581 {
16582 offset = dwarf2_get_attr_constant_value (member_loc, 0);
16583 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
16584 }
16585 else
16586 baton->size += DW_BLOCK (member_loc)->size;
16587
224c3ddb 16588 ptr = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, baton->size);
0971de02
TT
16589 baton->data = ptr;
16590
16591 *ptr++ = DW_OP_call4;
9c541725 16592 cu_off = common_die->sect_off - cu->per_cu->sect_off;
0971de02
TT
16593 store_unsigned_integer (ptr, 4, byte_order, cu_off);
16594 ptr += 4;
16595
16596 if (attr_form_is_constant (member_loc))
16597 {
16598 *ptr++ = DW_OP_addr;
16599 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
16600 ptr += cu->header.addr_size;
16601 }
16602 else
16603 {
16604 /* We have to copy the data here, because DW_OP_call4 will only
16605 use a DW_AT_location attribute. */
16606 memcpy (ptr, DW_BLOCK (member_loc)->data, DW_BLOCK (member_loc)->size);
16607 ptr += DW_BLOCK (member_loc)->size;
16608 }
16609
16610 *ptr++ = DW_OP_plus;
16611 gdb_assert (ptr - baton->data == baton->size);
16612
0971de02 16613 SYMBOL_LOCATION_BATON (sym) = baton;
f1e6e072 16614 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
0971de02
TT
16615}
16616
4357ac6c
TT
16617/* Create appropriate locally-scoped variables for all the
16618 DW_TAG_common_block entries. Also create a struct common_block
16619 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
16620 is used to sepate the common blocks name namespace from regular
16621 variable names. */
c906108c
SS
16622
16623static void
e7c27a73 16624read_common_block (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16625{
0971de02
TT
16626 struct attribute *attr;
16627
16628 attr = dwarf2_attr (die, DW_AT_location, cu);
16629 if (attr)
16630 {
16631 /* Support the .debug_loc offsets. */
16632 if (attr_form_is_block (attr))
16633 {
16634 /* Ok. */
16635 }
16636 else if (attr_form_is_section_offset (attr))
16637 {
16638 dwarf2_complex_location_expr_complaint ();
16639 attr = NULL;
16640 }
16641 else
16642 {
16643 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
16644 "common block member");
16645 attr = NULL;
16646 }
16647 }
16648
639d11d3 16649 if (die->child != NULL)
c906108c 16650 {
518817b3 16651 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
4357ac6c
TT
16652 struct die_info *child_die;
16653 size_t n_entries = 0, size;
16654 struct common_block *common_block;
16655 struct symbol *sym;
74ac6d43 16656
4357ac6c
TT
16657 for (child_die = die->child;
16658 child_die && child_die->tag;
16659 child_die = sibling_die (child_die))
16660 ++n_entries;
16661
16662 size = (sizeof (struct common_block)
16663 + (n_entries - 1) * sizeof (struct symbol *));
224c3ddb
SM
16664 common_block
16665 = (struct common_block *) obstack_alloc (&objfile->objfile_obstack,
16666 size);
4357ac6c
TT
16667 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
16668 common_block->n_entries = 0;
16669
16670 for (child_die = die->child;
16671 child_die && child_die->tag;
16672 child_die = sibling_die (child_die))
16673 {
16674 /* Create the symbol in the DW_TAG_common_block block in the current
16675 symbol scope. */
e7c27a73 16676 sym = new_symbol (child_die, NULL, cu);
0971de02
TT
16677 if (sym != NULL)
16678 {
16679 struct attribute *member_loc;
16680
16681 common_block->contents[common_block->n_entries++] = sym;
16682
16683 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
16684 cu);
16685 if (member_loc)
16686 {
16687 /* GDB has handled this for a long time, but it is
16688 not specified by DWARF. It seems to have been
16689 emitted by gfortran at least as recently as:
16690 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
16691 complaint (&symfile_complaints,
16692 _("Variable in common block has "
16693 "DW_AT_data_member_location "
9d8780f0
SM
16694 "- DIE at %s [in module %s]"),
16695 sect_offset_str (child_die->sect_off),
518817b3 16696 objfile_name (objfile));
0971de02
TT
16697
16698 if (attr_form_is_section_offset (member_loc))
16699 dwarf2_complex_location_expr_complaint ();
16700 else if (attr_form_is_constant (member_loc)
16701 || attr_form_is_block (member_loc))
16702 {
16703 if (attr)
16704 mark_common_block_symbol_computed (sym, die, attr,
16705 member_loc, cu);
16706 }
16707 else
16708 dwarf2_complex_location_expr_complaint ();
16709 }
16710 }
c906108c 16711 }
4357ac6c
TT
16712
16713 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
16714 SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
c906108c
SS
16715 }
16716}
16717
0114d602 16718/* Create a type for a C++ namespace. */
d9fa45fe 16719
0114d602
DJ
16720static struct type *
16721read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
d9fa45fe 16722{
518817b3 16723 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
0114d602 16724 const char *previous_prefix, *name;
9219021c 16725 int is_anonymous;
0114d602
DJ
16726 struct type *type;
16727
16728 /* For extensions, reuse the type of the original namespace. */
16729 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
16730 {
16731 struct die_info *ext_die;
16732 struct dwarf2_cu *ext_cu = cu;
9a619af0 16733
0114d602
DJ
16734 ext_die = dwarf2_extension (die, &ext_cu);
16735 type = read_type_die (ext_die, ext_cu);
9dc481d3
DE
16736
16737 /* EXT_CU may not be the same as CU.
02142a6c 16738 Ensure TYPE is recorded with CU in die_type_hash. */
0114d602
DJ
16739 return set_die_type (die, type, cu);
16740 }
9219021c 16741
e142c38c 16742 name = namespace_name (die, &is_anonymous, cu);
9219021c
DC
16743
16744 /* Now build the name of the current namespace. */
16745
0114d602
DJ
16746 previous_prefix = determine_prefix (die, cu);
16747 if (previous_prefix[0] != '\0')
16748 name = typename_concat (&objfile->objfile_obstack,
f55ee35c 16749 previous_prefix, name, 0, cu);
0114d602
DJ
16750
16751 /* Create the type. */
19f392bc 16752 type = init_type (objfile, TYPE_CODE_NAMESPACE, 0, name);
0114d602
DJ
16753 TYPE_TAG_NAME (type) = TYPE_NAME (type);
16754
60531b24 16755 return set_die_type (die, type, cu);
0114d602
DJ
16756}
16757
22cee43f 16758/* Read a namespace scope. */
0114d602
DJ
16759
16760static void
16761read_namespace (struct die_info *die, struct dwarf2_cu *cu)
16762{
518817b3 16763 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
0114d602 16764 int is_anonymous;
9219021c 16765
5c4e30ca
DC
16766 /* Add a symbol associated to this if we haven't seen the namespace
16767 before. Also, add a using directive if it's an anonymous
16768 namespace. */
9219021c 16769
f2f0e013 16770 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
5c4e30ca
DC
16771 {
16772 struct type *type;
16773
0114d602 16774 type = read_type_die (die, cu);
e7c27a73 16775 new_symbol (die, type, cu);
5c4e30ca 16776
e8e80198 16777 namespace_name (die, &is_anonymous, cu);
5c4e30ca 16778 if (is_anonymous)
0114d602
DJ
16779 {
16780 const char *previous_prefix = determine_prefix (die, cu);
9a619af0 16781
eb1e02fd 16782 std::vector<const char *> excludes;
22cee43f
PMR
16783 add_using_directive (using_directives (cu->language),
16784 previous_prefix, TYPE_NAME (type), NULL,
eb1e02fd 16785 NULL, excludes, 0, &objfile->objfile_obstack);
0114d602 16786 }
5c4e30ca 16787 }
9219021c 16788
639d11d3 16789 if (die->child != NULL)
d9fa45fe 16790 {
639d11d3 16791 struct die_info *child_die = die->child;
6e70227d 16792
d9fa45fe
DC
16793 while (child_die && child_die->tag)
16794 {
e7c27a73 16795 process_die (child_die, cu);
d9fa45fe
DC
16796 child_die = sibling_die (child_die);
16797 }
16798 }
38d518c9
EZ
16799}
16800
f55ee35c
JK
16801/* Read a Fortran module as type. This DIE can be only a declaration used for
16802 imported module. Still we need that type as local Fortran "use ... only"
16803 declaration imports depend on the created type in determine_prefix. */
16804
16805static struct type *
16806read_module_type (struct die_info *die, struct dwarf2_cu *cu)
16807{
518817b3 16808 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15d034d0 16809 const char *module_name;
f55ee35c
JK
16810 struct type *type;
16811
16812 module_name = dwarf2_name (die, cu);
16813 if (!module_name)
3e43a32a 16814 complaint (&symfile_complaints,
9d8780f0
SM
16815 _("DW_TAG_module has no name, offset %s"),
16816 sect_offset_str (die->sect_off));
19f392bc 16817 type = init_type (objfile, TYPE_CODE_MODULE, 0, module_name);
f55ee35c
JK
16818
16819 /* determine_prefix uses TYPE_TAG_NAME. */
16820 TYPE_TAG_NAME (type) = TYPE_NAME (type);
16821
16822 return set_die_type (die, type, cu);
16823}
16824
5d7cb8df
JK
16825/* Read a Fortran module. */
16826
16827static void
16828read_module (struct die_info *die, struct dwarf2_cu *cu)
16829{
16830 struct die_info *child_die = die->child;
530e8392
KB
16831 struct type *type;
16832
16833 type = read_type_die (die, cu);
16834 new_symbol (die, type, cu);
5d7cb8df 16835
5d7cb8df
JK
16836 while (child_die && child_die->tag)
16837 {
16838 process_die (child_die, cu);
16839 child_die = sibling_die (child_die);
16840 }
16841}
16842
38d518c9
EZ
16843/* Return the name of the namespace represented by DIE. Set
16844 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
16845 namespace. */
16846
16847static const char *
e142c38c 16848namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
38d518c9
EZ
16849{
16850 struct die_info *current_die;
16851 const char *name = NULL;
16852
16853 /* Loop through the extensions until we find a name. */
16854
16855 for (current_die = die;
16856 current_die != NULL;
f2f0e013 16857 current_die = dwarf2_extension (die, &cu))
38d518c9 16858 {
96553a0c
DE
16859 /* We don't use dwarf2_name here so that we can detect the absence
16860 of a name -> anonymous namespace. */
7d45c7c3 16861 name = dwarf2_string_attr (die, DW_AT_name, cu);
96553a0c 16862
38d518c9
EZ
16863 if (name != NULL)
16864 break;
16865 }
16866
16867 /* Is it an anonymous namespace? */
16868
16869 *is_anonymous = (name == NULL);
16870 if (*is_anonymous)
2b1dbab0 16871 name = CP_ANONYMOUS_NAMESPACE_STR;
38d518c9
EZ
16872
16873 return name;
d9fa45fe
DC
16874}
16875
c906108c
SS
16876/* Extract all information from a DW_TAG_pointer_type DIE and add to
16877 the user defined type vector. */
16878
f792889a 16879static struct type *
e7c27a73 16880read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16881{
518817b3
SM
16882 struct gdbarch *gdbarch
16883 = get_objfile_arch (cu->per_cu->dwarf2_per_objfile->objfile);
e7c27a73 16884 struct comp_unit_head *cu_header = &cu->header;
c906108c 16885 struct type *type;
8b2dbe47
KB
16886 struct attribute *attr_byte_size;
16887 struct attribute *attr_address_class;
16888 int byte_size, addr_class;
7e314c57
JK
16889 struct type *target_type;
16890
16891 target_type = die_type (die, cu);
c906108c 16892
7e314c57
JK
16893 /* The die_type call above may have already set the type for this DIE. */
16894 type = get_die_type (die, cu);
16895 if (type)
16896 return type;
16897
16898 type = lookup_pointer_type (target_type);
8b2dbe47 16899
e142c38c 16900 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
8b2dbe47
KB
16901 if (attr_byte_size)
16902 byte_size = DW_UNSND (attr_byte_size);
c906108c 16903 else
8b2dbe47
KB
16904 byte_size = cu_header->addr_size;
16905
e142c38c 16906 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
8b2dbe47
KB
16907 if (attr_address_class)
16908 addr_class = DW_UNSND (attr_address_class);
16909 else
16910 addr_class = DW_ADDR_none;
16911
2b4424c3
TT
16912 ULONGEST alignment = get_alignment (cu, die);
16913
16914 /* If the pointer size, alignment, or address class is different
16915 than the default, create a type variant marked as such and set
16916 the length accordingly. */
16917 if (TYPE_LENGTH (type) != byte_size
16918 || (alignment != 0 && TYPE_RAW_ALIGN (type) != 0
16919 && alignment != TYPE_RAW_ALIGN (type))
16920 || addr_class != DW_ADDR_none)
c906108c 16921 {
5e2b427d 16922 if (gdbarch_address_class_type_flags_p (gdbarch))
8b2dbe47
KB
16923 {
16924 int type_flags;
16925
849957d9 16926 type_flags = gdbarch_address_class_type_flags
5e2b427d 16927 (gdbarch, byte_size, addr_class);
876cecd0
TT
16928 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
16929 == 0);
8b2dbe47
KB
16930 type = make_type_with_address_space (type, type_flags);
16931 }
16932 else if (TYPE_LENGTH (type) != byte_size)
16933 {
3e43a32a
MS
16934 complaint (&symfile_complaints,
16935 _("invalid pointer size %d"), byte_size);
8b2dbe47 16936 }
2b4424c3
TT
16937 else if (TYPE_RAW_ALIGN (type) != alignment)
16938 {
16939 complaint (&symfile_complaints,
16940 _("Invalid DW_AT_alignment"
16941 " - DIE at %s [in module %s]"),
16942 sect_offset_str (die->sect_off),
16943 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
16944 }
6e70227d 16945 else
9a619af0
MS
16946 {
16947 /* Should we also complain about unhandled address classes? */
16948 }
c906108c 16949 }
8b2dbe47
KB
16950
16951 TYPE_LENGTH (type) = byte_size;
2b4424c3 16952 set_type_align (type, alignment);
f792889a 16953 return set_die_type (die, type, cu);
c906108c
SS
16954}
16955
16956/* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
16957 the user defined type vector. */
16958
f792889a 16959static struct type *
e7c27a73 16960read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
16961{
16962 struct type *type;
16963 struct type *to_type;
16964 struct type *domain;
16965
e7c27a73
DJ
16966 to_type = die_type (die, cu);
16967 domain = die_containing_type (die, cu);
0d5de010 16968
7e314c57
JK
16969 /* The calls above may have already set the type for this DIE. */
16970 type = get_die_type (die, cu);
16971 if (type)
16972 return type;
16973
0d5de010
DJ
16974 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
16975 type = lookup_methodptr_type (to_type);
7078baeb
TT
16976 else if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_FUNC)
16977 {
518817b3
SM
16978 struct type *new_type
16979 = alloc_type (cu->per_cu->dwarf2_per_objfile->objfile);
7078baeb
TT
16980
16981 smash_to_method_type (new_type, domain, TYPE_TARGET_TYPE (to_type),
16982 TYPE_FIELDS (to_type), TYPE_NFIELDS (to_type),
16983 TYPE_VARARGS (to_type));
16984 type = lookup_methodptr_type (new_type);
16985 }
0d5de010
DJ
16986 else
16987 type = lookup_memberptr_type (to_type, domain);
c906108c 16988
f792889a 16989 return set_die_type (die, type, cu);
c906108c
SS
16990}
16991
4297a3f0 16992/* Extract all information from a DW_TAG_{rvalue_,}reference_type DIE and add to
c906108c
SS
16993 the user defined type vector. */
16994
f792889a 16995static struct type *
4297a3f0
AV
16996read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu,
16997 enum type_code refcode)
c906108c 16998{
e7c27a73 16999 struct comp_unit_head *cu_header = &cu->header;
7e314c57 17000 struct type *type, *target_type;
c906108c
SS
17001 struct attribute *attr;
17002
4297a3f0
AV
17003 gdb_assert (refcode == TYPE_CODE_REF || refcode == TYPE_CODE_RVALUE_REF);
17004
7e314c57
JK
17005 target_type = die_type (die, cu);
17006
17007 /* The die_type call above may have already set the type for this DIE. */
17008 type = get_die_type (die, cu);
17009 if (type)
17010 return type;
17011
4297a3f0 17012 type = lookup_reference_type (target_type, refcode);
e142c38c 17013 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
17014 if (attr)
17015 {
17016 TYPE_LENGTH (type) = DW_UNSND (attr);
17017 }
17018 else
17019 {
107d2387 17020 TYPE_LENGTH (type) = cu_header->addr_size;
c906108c 17021 }
2b4424c3 17022 maybe_set_alignment (cu, die, type);
f792889a 17023 return set_die_type (die, type, cu);
c906108c
SS
17024}
17025
cf363f18
MW
17026/* Add the given cv-qualifiers to the element type of the array. GCC
17027 outputs DWARF type qualifiers that apply to an array, not the
17028 element type. But GDB relies on the array element type to carry
17029 the cv-qualifiers. This mimics section 6.7.3 of the C99
17030 specification. */
17031
17032static struct type *
17033add_array_cv_type (struct die_info *die, struct dwarf2_cu *cu,
17034 struct type *base_type, int cnst, int voltl)
17035{
17036 struct type *el_type, *inner_array;
17037
17038 base_type = copy_type (base_type);
17039 inner_array = base_type;
17040
17041 while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
17042 {
17043 TYPE_TARGET_TYPE (inner_array) =
17044 copy_type (TYPE_TARGET_TYPE (inner_array));
17045 inner_array = TYPE_TARGET_TYPE (inner_array);
17046 }
17047
17048 el_type = TYPE_TARGET_TYPE (inner_array);
17049 cnst |= TYPE_CONST (el_type);
17050 voltl |= TYPE_VOLATILE (el_type);
17051 TYPE_TARGET_TYPE (inner_array) = make_cv_type (cnst, voltl, el_type, NULL);
17052
17053 return set_die_type (die, base_type, cu);
17054}
17055
f792889a 17056static struct type *
e7c27a73 17057read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17058{
f792889a 17059 struct type *base_type, *cv_type;
c906108c 17060
e7c27a73 17061 base_type = die_type (die, cu);
7e314c57
JK
17062
17063 /* The die_type call above may have already set the type for this DIE. */
17064 cv_type = get_die_type (die, cu);
17065 if (cv_type)
17066 return cv_type;
17067
2f608a3a
KW
17068 /* In case the const qualifier is applied to an array type, the element type
17069 is so qualified, not the array type (section 6.7.3 of C99). */
17070 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
cf363f18 17071 return add_array_cv_type (die, cu, base_type, 1, 0);
2f608a3a 17072
f792889a
DJ
17073 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
17074 return set_die_type (die, cv_type, cu);
c906108c
SS
17075}
17076
f792889a 17077static struct type *
e7c27a73 17078read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17079{
f792889a 17080 struct type *base_type, *cv_type;
c906108c 17081
e7c27a73 17082 base_type = die_type (die, cu);
7e314c57
JK
17083
17084 /* The die_type call above may have already set the type for this DIE. */
17085 cv_type = get_die_type (die, cu);
17086 if (cv_type)
17087 return cv_type;
17088
cf363f18
MW
17089 /* In case the volatile qualifier is applied to an array type, the
17090 element type is so qualified, not the array type (section 6.7.3
17091 of C99). */
17092 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
17093 return add_array_cv_type (die, cu, base_type, 0, 1);
17094
f792889a
DJ
17095 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
17096 return set_die_type (die, cv_type, cu);
c906108c
SS
17097}
17098
06d66ee9
TT
17099/* Handle DW_TAG_restrict_type. */
17100
17101static struct type *
17102read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
17103{
17104 struct type *base_type, *cv_type;
17105
17106 base_type = die_type (die, cu);
17107
17108 /* The die_type call above may have already set the type for this DIE. */
17109 cv_type = get_die_type (die, cu);
17110 if (cv_type)
17111 return cv_type;
17112
17113 cv_type = make_restrict_type (base_type);
17114 return set_die_type (die, cv_type, cu);
17115}
17116
a2c2acaf
MW
17117/* Handle DW_TAG_atomic_type. */
17118
17119static struct type *
17120read_tag_atomic_type (struct die_info *die, struct dwarf2_cu *cu)
17121{
17122 struct type *base_type, *cv_type;
17123
17124 base_type = die_type (die, cu);
17125
17126 /* The die_type call above may have already set the type for this DIE. */
17127 cv_type = get_die_type (die, cu);
17128 if (cv_type)
17129 return cv_type;
17130
17131 cv_type = make_atomic_type (base_type);
17132 return set_die_type (die, cv_type, cu);
17133}
17134
c906108c
SS
17135/* Extract all information from a DW_TAG_string_type DIE and add to
17136 the user defined type vector. It isn't really a user defined type,
17137 but it behaves like one, with other DIE's using an AT_user_def_type
17138 attribute to reference it. */
17139
f792889a 17140static struct type *
e7c27a73 17141read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17142{
518817b3 17143 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
3b7538c0 17144 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
17145 struct type *type, *range_type, *index_type, *char_type;
17146 struct attribute *attr;
17147 unsigned int length;
17148
e142c38c 17149 attr = dwarf2_attr (die, DW_AT_string_length, cu);
c906108c
SS
17150 if (attr)
17151 {
17152 length = DW_UNSND (attr);
17153 }
17154 else
17155 {
0963b4bd 17156 /* Check for the DW_AT_byte_size attribute. */
e142c38c 17157 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
b21b22e0
PS
17158 if (attr)
17159 {
17160 length = DW_UNSND (attr);
17161 }
17162 else
17163 {
17164 length = 1;
17165 }
c906108c 17166 }
6ccb9162 17167
46bf5051 17168 index_type = objfile_type (objfile)->builtin_int;
0c9c3474 17169 range_type = create_static_range_type (NULL, index_type, 1, length);
3b7538c0
UW
17170 char_type = language_string_char_type (cu->language_defn, gdbarch);
17171 type = create_string_type (NULL, char_type, range_type);
6ccb9162 17172
f792889a 17173 return set_die_type (die, type, cu);
c906108c
SS
17174}
17175
4d804846
JB
17176/* Assuming that DIE corresponds to a function, returns nonzero
17177 if the function is prototyped. */
17178
17179static int
17180prototyped_function_p (struct die_info *die, struct dwarf2_cu *cu)
17181{
17182 struct attribute *attr;
17183
17184 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
17185 if (attr && (DW_UNSND (attr) != 0))
17186 return 1;
17187
17188 /* The DWARF standard implies that the DW_AT_prototyped attribute
17189 is only meaninful for C, but the concept also extends to other
17190 languages that allow unprototyped functions (Eg: Objective C).
17191 For all other languages, assume that functions are always
17192 prototyped. */
17193 if (cu->language != language_c
17194 && cu->language != language_objc
17195 && cu->language != language_opencl)
17196 return 1;
17197
17198 /* RealView does not emit DW_AT_prototyped. We can not distinguish
17199 prototyped and unprototyped functions; default to prototyped,
17200 since that is more common in modern code (and RealView warns
17201 about unprototyped functions). */
17202 if (producer_is_realview (cu->producer))
17203 return 1;
17204
17205 return 0;
17206}
17207
c906108c
SS
17208/* Handle DIES due to C code like:
17209
17210 struct foo
c5aa993b
JM
17211 {
17212 int (*funcp)(int a, long l);
17213 int b;
17214 };
c906108c 17215
0963b4bd 17216 ('funcp' generates a DW_TAG_subroutine_type DIE). */
c906108c 17217
f792889a 17218static struct type *
e7c27a73 17219read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17220{
518817b3 17221 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
0963b4bd
MS
17222 struct type *type; /* Type that this function returns. */
17223 struct type *ftype; /* Function that returns above type. */
c906108c
SS
17224 struct attribute *attr;
17225
e7c27a73 17226 type = die_type (die, cu);
7e314c57
JK
17227
17228 /* The die_type call above may have already set the type for this DIE. */
17229 ftype = get_die_type (die, cu);
17230 if (ftype)
17231 return ftype;
17232
0c8b41f1 17233 ftype = lookup_function_type (type);
c906108c 17234
4d804846 17235 if (prototyped_function_p (die, cu))
a6c727b2 17236 TYPE_PROTOTYPED (ftype) = 1;
c906108c 17237
c055b101
CV
17238 /* Store the calling convention in the type if it's available in
17239 the subroutine die. Otherwise set the calling convention to
17240 the default value DW_CC_normal. */
17241 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
54fcddd0
UW
17242 if (attr)
17243 TYPE_CALLING_CONVENTION (ftype) = DW_UNSND (attr);
17244 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
17245 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
17246 else
17247 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
76c10ea2 17248
743649fd
MW
17249 /* Record whether the function returns normally to its caller or not
17250 if the DWARF producer set that information. */
17251 attr = dwarf2_attr (die, DW_AT_noreturn, cu);
17252 if (attr && (DW_UNSND (attr) != 0))
17253 TYPE_NO_RETURN (ftype) = 1;
17254
76c10ea2
GM
17255 /* We need to add the subroutine type to the die immediately so
17256 we don't infinitely recurse when dealing with parameters
0963b4bd 17257 declared as the same subroutine type. */
76c10ea2 17258 set_die_type (die, ftype, cu);
6e70227d 17259
639d11d3 17260 if (die->child != NULL)
c906108c 17261 {
bb5ed363 17262 struct type *void_type = objfile_type (objfile)->builtin_void;
c906108c 17263 struct die_info *child_die;
8072405b 17264 int nparams, iparams;
c906108c
SS
17265
17266 /* Count the number of parameters.
17267 FIXME: GDB currently ignores vararg functions, but knows about
17268 vararg member functions. */
8072405b 17269 nparams = 0;
639d11d3 17270 child_die = die->child;
c906108c
SS
17271 while (child_die && child_die->tag)
17272 {
17273 if (child_die->tag == DW_TAG_formal_parameter)
17274 nparams++;
17275 else if (child_die->tag == DW_TAG_unspecified_parameters)
876cecd0 17276 TYPE_VARARGS (ftype) = 1;
c906108c
SS
17277 child_die = sibling_die (child_die);
17278 }
17279
17280 /* Allocate storage for parameters and fill them in. */
17281 TYPE_NFIELDS (ftype) = nparams;
17282 TYPE_FIELDS (ftype) = (struct field *)
ae5a43e0 17283 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
c906108c 17284
8072405b
JK
17285 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
17286 even if we error out during the parameters reading below. */
17287 for (iparams = 0; iparams < nparams; iparams++)
17288 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
17289
17290 iparams = 0;
639d11d3 17291 child_die = die->child;
c906108c
SS
17292 while (child_die && child_die->tag)
17293 {
17294 if (child_die->tag == DW_TAG_formal_parameter)
17295 {
3ce3b1ba
PA
17296 struct type *arg_type;
17297
17298 /* DWARF version 2 has no clean way to discern C++
17299 static and non-static member functions. G++ helps
17300 GDB by marking the first parameter for non-static
17301 member functions (which is the this pointer) as
17302 artificial. We pass this information to
17303 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
17304
17305 DWARF version 3 added DW_AT_object_pointer, which GCC
17306 4.5 does not yet generate. */
e142c38c 17307 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
c906108c
SS
17308 if (attr)
17309 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
17310 else
9c37b5ae 17311 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
3ce3b1ba
PA
17312 arg_type = die_type (child_die, cu);
17313
17314 /* RealView does not mark THIS as const, which the testsuite
17315 expects. GCC marks THIS as const in method definitions,
17316 but not in the class specifications (GCC PR 43053). */
17317 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
17318 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
17319 {
17320 int is_this = 0;
17321 struct dwarf2_cu *arg_cu = cu;
17322 const char *name = dwarf2_name (child_die, cu);
17323
17324 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
17325 if (attr)
17326 {
17327 /* If the compiler emits this, use it. */
17328 if (follow_die_ref (die, attr, &arg_cu) == child_die)
17329 is_this = 1;
17330 }
17331 else if (name && strcmp (name, "this") == 0)
17332 /* Function definitions will have the argument names. */
17333 is_this = 1;
17334 else if (name == NULL && iparams == 0)
17335 /* Declarations may not have the names, so like
17336 elsewhere in GDB, assume an artificial first
17337 argument is "this". */
17338 is_this = 1;
17339
17340 if (is_this)
17341 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
17342 arg_type, 0);
17343 }
17344
17345 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
c906108c
SS
17346 iparams++;
17347 }
17348 child_die = sibling_die (child_die);
17349 }
17350 }
17351
76c10ea2 17352 return ftype;
c906108c
SS
17353}
17354
f792889a 17355static struct type *
e7c27a73 17356read_typedef (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17357{
518817b3 17358 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
0114d602 17359 const char *name = NULL;
3c8e0968 17360 struct type *this_type, *target_type;
c906108c 17361
94af9270 17362 name = dwarf2_full_name (NULL, die, cu);
19f392bc
UW
17363 this_type = init_type (objfile, TYPE_CODE_TYPEDEF, 0, name);
17364 TYPE_TARGET_STUB (this_type) = 1;
f792889a 17365 set_die_type (die, this_type, cu);
3c8e0968
DE
17366 target_type = die_type (die, cu);
17367 if (target_type != this_type)
17368 TYPE_TARGET_TYPE (this_type) = target_type;
17369 else
17370 {
17371 /* Self-referential typedefs are, it seems, not allowed by the DWARF
17372 spec and cause infinite loops in GDB. */
17373 complaint (&symfile_complaints,
17374 _("Self-referential DW_TAG_typedef "
9d8780f0
SM
17375 "- DIE at %s [in module %s]"),
17376 sect_offset_str (die->sect_off), objfile_name (objfile));
3c8e0968
DE
17377 TYPE_TARGET_TYPE (this_type) = NULL;
17378 }
f792889a 17379 return this_type;
c906108c
SS
17380}
17381
9b790ce7
UW
17382/* Allocate a floating-point type of size BITS and name NAME. Pass NAME_HINT
17383 (which may be different from NAME) to the architecture back-end to allow
17384 it to guess the correct format if necessary. */
17385
17386static struct type *
17387dwarf2_init_float_type (struct objfile *objfile, int bits, const char *name,
17388 const char *name_hint)
17389{
17390 struct gdbarch *gdbarch = get_objfile_arch (objfile);
17391 const struct floatformat **format;
17392 struct type *type;
17393
17394 format = gdbarch_floatformat_for_type (gdbarch, name_hint, bits);
17395 if (format)
17396 type = init_float_type (objfile, bits, name, format);
17397 else
77b7c781 17398 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
9b790ce7
UW
17399
17400 return type;
17401}
17402
c906108c
SS
17403/* Find a representation of a given base type and install
17404 it in the TYPE field of the die. */
17405
f792889a 17406static struct type *
e7c27a73 17407read_base_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17408{
518817b3 17409 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c
SS
17410 struct type *type;
17411 struct attribute *attr;
19f392bc 17412 int encoding = 0, bits = 0;
15d034d0 17413 const char *name;
c906108c 17414
e142c38c 17415 attr = dwarf2_attr (die, DW_AT_encoding, cu);
c906108c
SS
17416 if (attr)
17417 {
17418 encoding = DW_UNSND (attr);
17419 }
e142c38c 17420 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
17421 if (attr)
17422 {
19f392bc 17423 bits = DW_UNSND (attr) * TARGET_CHAR_BIT;
c906108c 17424 }
39cbfefa 17425 name = dwarf2_name (die, cu);
6ccb9162 17426 if (!name)
c906108c 17427 {
6ccb9162
UW
17428 complaint (&symfile_complaints,
17429 _("DW_AT_name missing from DW_TAG_base_type"));
c906108c 17430 }
6ccb9162
UW
17431
17432 switch (encoding)
c906108c 17433 {
6ccb9162
UW
17434 case DW_ATE_address:
17435 /* Turn DW_ATE_address into a void * pointer. */
77b7c781 17436 type = init_type (objfile, TYPE_CODE_VOID, TARGET_CHAR_BIT, NULL);
19f392bc 17437 type = init_pointer_type (objfile, bits, name, type);
6ccb9162
UW
17438 break;
17439 case DW_ATE_boolean:
19f392bc 17440 type = init_boolean_type (objfile, bits, 1, name);
6ccb9162
UW
17441 break;
17442 case DW_ATE_complex_float:
9b790ce7 17443 type = dwarf2_init_float_type (objfile, bits / 2, NULL, name);
19f392bc 17444 type = init_complex_type (objfile, name, type);
6ccb9162
UW
17445 break;
17446 case DW_ATE_decimal_float:
19f392bc 17447 type = init_decfloat_type (objfile, bits, name);
6ccb9162
UW
17448 break;
17449 case DW_ATE_float:
9b790ce7 17450 type = dwarf2_init_float_type (objfile, bits, name, name);
6ccb9162
UW
17451 break;
17452 case DW_ATE_signed:
19f392bc 17453 type = init_integer_type (objfile, bits, 0, name);
6ccb9162
UW
17454 break;
17455 case DW_ATE_unsigned:
3b2b8fea
TT
17456 if (cu->language == language_fortran
17457 && name
61012eef 17458 && startswith (name, "character("))
19f392bc
UW
17459 type = init_character_type (objfile, bits, 1, name);
17460 else
17461 type = init_integer_type (objfile, bits, 1, name);
6ccb9162
UW
17462 break;
17463 case DW_ATE_signed_char:
6e70227d 17464 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea
TT
17465 || cu->language == language_pascal
17466 || cu->language == language_fortran)
19f392bc
UW
17467 type = init_character_type (objfile, bits, 0, name);
17468 else
17469 type = init_integer_type (objfile, bits, 0, name);
6ccb9162
UW
17470 break;
17471 case DW_ATE_unsigned_char:
868a0084 17472 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea 17473 || cu->language == language_pascal
c44af4eb
TT
17474 || cu->language == language_fortran
17475 || cu->language == language_rust)
19f392bc
UW
17476 type = init_character_type (objfile, bits, 1, name);
17477 else
17478 type = init_integer_type (objfile, bits, 1, name);
6ccb9162 17479 break;
75079b2b 17480 case DW_ATE_UTF:
53e710ac
PA
17481 {
17482 gdbarch *arch = get_objfile_arch (objfile);
17483
17484 if (bits == 16)
17485 type = builtin_type (arch)->builtin_char16;
17486 else if (bits == 32)
17487 type = builtin_type (arch)->builtin_char32;
17488 else
17489 {
17490 complaint (&symfile_complaints,
17491 _("unsupported DW_ATE_UTF bit size: '%d'"),
17492 bits);
17493 type = init_integer_type (objfile, bits, 1, name);
17494 }
17495 return set_die_type (die, type, cu);
17496 }
75079b2b
TT
17497 break;
17498
6ccb9162
UW
17499 default:
17500 complaint (&symfile_complaints, _("unsupported DW_AT_encoding: '%s'"),
17501 dwarf_type_encoding_name (encoding));
77b7c781 17502 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
6ccb9162 17503 break;
c906108c 17504 }
6ccb9162 17505
0114d602 17506 if (name && strcmp (name, "char") == 0)
876cecd0 17507 TYPE_NOSIGN (type) = 1;
0114d602 17508
2b4424c3
TT
17509 maybe_set_alignment (cu, die, type);
17510
f792889a 17511 return set_die_type (die, type, cu);
c906108c
SS
17512}
17513
80180f79
SA
17514/* Parse dwarf attribute if it's a block, reference or constant and put the
17515 resulting value of the attribute into struct bound_prop.
17516 Returns 1 if ATTR could be resolved into PROP, 0 otherwise. */
17517
17518static int
17519attr_to_dynamic_prop (const struct attribute *attr, struct die_info *die,
17520 struct dwarf2_cu *cu, struct dynamic_prop *prop)
17521{
17522 struct dwarf2_property_baton *baton;
518817b3
SM
17523 struct obstack *obstack
17524 = &cu->per_cu->dwarf2_per_objfile->objfile->objfile_obstack;
80180f79
SA
17525
17526 if (attr == NULL || prop == NULL)
17527 return 0;
17528
17529 if (attr_form_is_block (attr))
17530 {
8d749320 17531 baton = XOBNEW (obstack, struct dwarf2_property_baton);
80180f79
SA
17532 baton->referenced_type = NULL;
17533 baton->locexpr.per_cu = cu->per_cu;
17534 baton->locexpr.size = DW_BLOCK (attr)->size;
17535 baton->locexpr.data = DW_BLOCK (attr)->data;
17536 prop->data.baton = baton;
17537 prop->kind = PROP_LOCEXPR;
17538 gdb_assert (prop->data.baton != NULL);
17539 }
17540 else if (attr_form_is_ref (attr))
17541 {
17542 struct dwarf2_cu *target_cu = cu;
17543 struct die_info *target_die;
17544 struct attribute *target_attr;
17545
17546 target_die = follow_die_ref (die, attr, &target_cu);
17547 target_attr = dwarf2_attr (target_die, DW_AT_location, target_cu);
df25ebbd
JB
17548 if (target_attr == NULL)
17549 target_attr = dwarf2_attr (target_die, DW_AT_data_member_location,
17550 target_cu);
80180f79
SA
17551 if (target_attr == NULL)
17552 return 0;
17553
df25ebbd 17554 switch (target_attr->name)
80180f79 17555 {
df25ebbd
JB
17556 case DW_AT_location:
17557 if (attr_form_is_section_offset (target_attr))
17558 {
8d749320 17559 baton = XOBNEW (obstack, struct dwarf2_property_baton);
df25ebbd
JB
17560 baton->referenced_type = die_type (target_die, target_cu);
17561 fill_in_loclist_baton (cu, &baton->loclist, target_attr);
17562 prop->data.baton = baton;
17563 prop->kind = PROP_LOCLIST;
17564 gdb_assert (prop->data.baton != NULL);
17565 }
17566 else if (attr_form_is_block (target_attr))
17567 {
8d749320 17568 baton = XOBNEW (obstack, struct dwarf2_property_baton);
df25ebbd
JB
17569 baton->referenced_type = die_type (target_die, target_cu);
17570 baton->locexpr.per_cu = cu->per_cu;
17571 baton->locexpr.size = DW_BLOCK (target_attr)->size;
17572 baton->locexpr.data = DW_BLOCK (target_attr)->data;
17573 prop->data.baton = baton;
17574 prop->kind = PROP_LOCEXPR;
17575 gdb_assert (prop->data.baton != NULL);
17576 }
17577 else
17578 {
17579 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
17580 "dynamic property");
17581 return 0;
17582 }
17583 break;
17584 case DW_AT_data_member_location:
17585 {
17586 LONGEST offset;
17587
17588 if (!handle_data_member_location (target_die, target_cu,
17589 &offset))
17590 return 0;
17591
8d749320 17592 baton = XOBNEW (obstack, struct dwarf2_property_baton);
6ad395a7
JB
17593 baton->referenced_type = read_type_die (target_die->parent,
17594 target_cu);
df25ebbd
JB
17595 baton->offset_info.offset = offset;
17596 baton->offset_info.type = die_type (target_die, target_cu);
17597 prop->data.baton = baton;
17598 prop->kind = PROP_ADDR_OFFSET;
17599 break;
17600 }
80180f79
SA
17601 }
17602 }
17603 else if (attr_form_is_constant (attr))
17604 {
17605 prop->data.const_val = dwarf2_get_attr_constant_value (attr, 0);
17606 prop->kind = PROP_CONST;
17607 }
17608 else
17609 {
17610 dwarf2_invalid_attrib_class_complaint (dwarf_form_name (attr->form),
17611 dwarf2_name (die, cu));
17612 return 0;
17613 }
17614
17615 return 1;
17616}
17617
a02abb62
JB
17618/* Read the given DW_AT_subrange DIE. */
17619
f792889a 17620static struct type *
a02abb62
JB
17621read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
17622{
4c9ad8c2 17623 struct type *base_type, *orig_base_type;
a02abb62
JB
17624 struct type *range_type;
17625 struct attribute *attr;
729efb13 17626 struct dynamic_prop low, high;
4fae6e18 17627 int low_default_is_valid;
c451ebe5 17628 int high_bound_is_count = 0;
15d034d0 17629 const char *name;
43bbcdc2 17630 LONGEST negative_mask;
e77813c8 17631
4c9ad8c2
TT
17632 orig_base_type = die_type (die, cu);
17633 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
17634 whereas the real type might be. So, we use ORIG_BASE_TYPE when
17635 creating the range type, but we use the result of check_typedef
17636 when examining properties of the type. */
17637 base_type = check_typedef (orig_base_type);
a02abb62 17638
7e314c57
JK
17639 /* The die_type call above may have already set the type for this DIE. */
17640 range_type = get_die_type (die, cu);
17641 if (range_type)
17642 return range_type;
17643
729efb13
SA
17644 low.kind = PROP_CONST;
17645 high.kind = PROP_CONST;
17646 high.data.const_val = 0;
17647
4fae6e18
JK
17648 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
17649 omitting DW_AT_lower_bound. */
17650 switch (cu->language)
6e70227d 17651 {
4fae6e18
JK
17652 case language_c:
17653 case language_cplus:
729efb13 17654 low.data.const_val = 0;
4fae6e18
JK
17655 low_default_is_valid = 1;
17656 break;
17657 case language_fortran:
729efb13 17658 low.data.const_val = 1;
4fae6e18
JK
17659 low_default_is_valid = 1;
17660 break;
17661 case language_d:
4fae6e18 17662 case language_objc:
c44af4eb 17663 case language_rust:
729efb13 17664 low.data.const_val = 0;
4fae6e18
JK
17665 low_default_is_valid = (cu->header.version >= 4);
17666 break;
17667 case language_ada:
17668 case language_m2:
17669 case language_pascal:
729efb13 17670 low.data.const_val = 1;
4fae6e18
JK
17671 low_default_is_valid = (cu->header.version >= 4);
17672 break;
17673 default:
729efb13 17674 low.data.const_val = 0;
4fae6e18
JK
17675 low_default_is_valid = 0;
17676 break;
a02abb62
JB
17677 }
17678
e142c38c 17679 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
a02abb62 17680 if (attr)
11c1ba78 17681 attr_to_dynamic_prop (attr, die, cu, &low);
4fae6e18
JK
17682 else if (!low_default_is_valid)
17683 complaint (&symfile_complaints, _("Missing DW_AT_lower_bound "
9d8780f0
SM
17684 "- DIE at %s [in module %s]"),
17685 sect_offset_str (die->sect_off),
518817b3 17686 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
a02abb62 17687
e142c38c 17688 attr = dwarf2_attr (die, DW_AT_upper_bound, cu);
80180f79 17689 if (!attr_to_dynamic_prop (attr, die, cu, &high))
e77813c8
PM
17690 {
17691 attr = dwarf2_attr (die, DW_AT_count, cu);
c451ebe5 17692 if (attr_to_dynamic_prop (attr, die, cu, &high))
6b662e19 17693 {
c451ebe5
SA
17694 /* If bounds are constant do the final calculation here. */
17695 if (low.kind == PROP_CONST && high.kind == PROP_CONST)
17696 high.data.const_val = low.data.const_val + high.data.const_val - 1;
17697 else
17698 high_bound_is_count = 1;
c2ff108b 17699 }
e77813c8
PM
17700 }
17701
17702 /* Dwarf-2 specifications explicitly allows to create subrange types
17703 without specifying a base type.
17704 In that case, the base type must be set to the type of
17705 the lower bound, upper bound or count, in that order, if any of these
17706 three attributes references an object that has a type.
17707 If no base type is found, the Dwarf-2 specifications say that
17708 a signed integer type of size equal to the size of an address should
17709 be used.
17710 For the following C code: `extern char gdb_int [];'
17711 GCC produces an empty range DIE.
17712 FIXME: muller/2010-05-28: Possible references to object for low bound,
0963b4bd 17713 high bound or count are not yet handled by this code. */
e77813c8
PM
17714 if (TYPE_CODE (base_type) == TYPE_CODE_VOID)
17715 {
518817b3 17716 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
e77813c8
PM
17717 struct gdbarch *gdbarch = get_objfile_arch (objfile);
17718 int addr_size = gdbarch_addr_bit (gdbarch) /8;
17719 struct type *int_type = objfile_type (objfile)->builtin_int;
17720
17721 /* Test "int", "long int", and "long long int" objfile types,
17722 and select the first one having a size above or equal to the
17723 architecture address size. */
17724 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
17725 base_type = int_type;
17726 else
17727 {
17728 int_type = objfile_type (objfile)->builtin_long;
17729 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
17730 base_type = int_type;
17731 else
17732 {
17733 int_type = objfile_type (objfile)->builtin_long_long;
17734 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
17735 base_type = int_type;
17736 }
17737 }
17738 }
a02abb62 17739
dbb9c2b1
JB
17740 /* Normally, the DWARF producers are expected to use a signed
17741 constant form (Eg. DW_FORM_sdata) to express negative bounds.
17742 But this is unfortunately not always the case, as witnessed
17743 with GCC, for instance, where the ambiguous DW_FORM_dataN form
17744 is used instead. To work around that ambiguity, we treat
17745 the bounds as signed, and thus sign-extend their values, when
17746 the base type is signed. */
6e70227d 17747 negative_mask =
66c6502d 17748 -((LONGEST) 1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1));
729efb13
SA
17749 if (low.kind == PROP_CONST
17750 && !TYPE_UNSIGNED (base_type) && (low.data.const_val & negative_mask))
17751 low.data.const_val |= negative_mask;
17752 if (high.kind == PROP_CONST
17753 && !TYPE_UNSIGNED (base_type) && (high.data.const_val & negative_mask))
17754 high.data.const_val |= negative_mask;
43bbcdc2 17755
729efb13 17756 range_type = create_range_type (NULL, orig_base_type, &low, &high);
a02abb62 17757
c451ebe5
SA
17758 if (high_bound_is_count)
17759 TYPE_RANGE_DATA (range_type)->flag_upper_bound_is_count = 1;
17760
c2ff108b
JK
17761 /* Ada expects an empty array on no boundary attributes. */
17762 if (attr == NULL && cu->language != language_ada)
729efb13 17763 TYPE_HIGH_BOUND_KIND (range_type) = PROP_UNDEFINED;
c2ff108b 17764
39cbfefa
DJ
17765 name = dwarf2_name (die, cu);
17766 if (name)
17767 TYPE_NAME (range_type) = name;
6e70227d 17768
e142c38c 17769 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
a02abb62
JB
17770 if (attr)
17771 TYPE_LENGTH (range_type) = DW_UNSND (attr);
17772
2b4424c3
TT
17773 maybe_set_alignment (cu, die, range_type);
17774
7e314c57
JK
17775 set_die_type (die, range_type, cu);
17776
17777 /* set_die_type should be already done. */
b4ba55a1
JB
17778 set_descriptive_type (range_type, die, cu);
17779
7e314c57 17780 return range_type;
a02abb62 17781}
6e70227d 17782
f792889a 17783static struct type *
81a17f79
JB
17784read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
17785{
17786 struct type *type;
81a17f79 17787
518817b3
SM
17788 type = init_type (cu->per_cu->dwarf2_per_objfile->objfile, TYPE_CODE_VOID,0,
17789 NULL);
0114d602 17790 TYPE_NAME (type) = dwarf2_name (die, cu);
81a17f79 17791
74a2f8ff
JB
17792 /* In Ada, an unspecified type is typically used when the description
17793 of the type is defered to a different unit. When encountering
17794 such a type, we treat it as a stub, and try to resolve it later on,
17795 when needed. */
17796 if (cu->language == language_ada)
17797 TYPE_STUB (type) = 1;
17798
f792889a 17799 return set_die_type (die, type, cu);
81a17f79 17800}
a02abb62 17801
639d11d3
DC
17802/* Read a single die and all its descendents. Set the die's sibling
17803 field to NULL; set other fields in the die correctly, and set all
17804 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
17805 location of the info_ptr after reading all of those dies. PARENT
17806 is the parent of the die in question. */
17807
17808static struct die_info *
dee91e82 17809read_die_and_children (const struct die_reader_specs *reader,
d521ce57
TT
17810 const gdb_byte *info_ptr,
17811 const gdb_byte **new_info_ptr,
dee91e82 17812 struct die_info *parent)
639d11d3
DC
17813{
17814 struct die_info *die;
d521ce57 17815 const gdb_byte *cur_ptr;
639d11d3
DC
17816 int has_children;
17817
bf6af496 17818 cur_ptr = read_full_die_1 (reader, &die, info_ptr, &has_children, 0);
1d325ec1
DJ
17819 if (die == NULL)
17820 {
17821 *new_info_ptr = cur_ptr;
17822 return NULL;
17823 }
93311388 17824 store_in_ref_table (die, reader->cu);
639d11d3
DC
17825
17826 if (has_children)
bf6af496 17827 die->child = read_die_and_siblings_1 (reader, cur_ptr, new_info_ptr, die);
639d11d3
DC
17828 else
17829 {
17830 die->child = NULL;
17831 *new_info_ptr = cur_ptr;
17832 }
17833
17834 die->sibling = NULL;
17835 die->parent = parent;
17836 return die;
17837}
17838
17839/* Read a die, all of its descendents, and all of its siblings; set
17840 all of the fields of all of the dies correctly. Arguments are as
17841 in read_die_and_children. */
17842
17843static struct die_info *
bf6af496 17844read_die_and_siblings_1 (const struct die_reader_specs *reader,
d521ce57
TT
17845 const gdb_byte *info_ptr,
17846 const gdb_byte **new_info_ptr,
bf6af496 17847 struct die_info *parent)
639d11d3
DC
17848{
17849 struct die_info *first_die, *last_sibling;
d521ce57 17850 const gdb_byte *cur_ptr;
639d11d3 17851
c906108c 17852 cur_ptr = info_ptr;
639d11d3
DC
17853 first_die = last_sibling = NULL;
17854
17855 while (1)
c906108c 17856 {
639d11d3 17857 struct die_info *die
dee91e82 17858 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
639d11d3 17859
1d325ec1 17860 if (die == NULL)
c906108c 17861 {
639d11d3
DC
17862 *new_info_ptr = cur_ptr;
17863 return first_die;
c906108c 17864 }
1d325ec1
DJ
17865
17866 if (!first_die)
17867 first_die = die;
c906108c 17868 else
1d325ec1
DJ
17869 last_sibling->sibling = die;
17870
17871 last_sibling = die;
c906108c 17872 }
c906108c
SS
17873}
17874
bf6af496
DE
17875/* Read a die, all of its descendents, and all of its siblings; set
17876 all of the fields of all of the dies correctly. Arguments are as
17877 in read_die_and_children.
17878 This the main entry point for reading a DIE and all its children. */
17879
17880static struct die_info *
17881read_die_and_siblings (const struct die_reader_specs *reader,
d521ce57
TT
17882 const gdb_byte *info_ptr,
17883 const gdb_byte **new_info_ptr,
bf6af496
DE
17884 struct die_info *parent)
17885{
17886 struct die_info *die = read_die_and_siblings_1 (reader, info_ptr,
17887 new_info_ptr, parent);
17888
b4f54984 17889 if (dwarf_die_debug)
bf6af496
DE
17890 {
17891 fprintf_unfiltered (gdb_stdlog,
17892 "Read die from %s@0x%x of %s:\n",
a32a8923 17893 get_section_name (reader->die_section),
bf6af496
DE
17894 (unsigned) (info_ptr - reader->die_section->buffer),
17895 bfd_get_filename (reader->abfd));
b4f54984 17896 dump_die (die, dwarf_die_debug);
bf6af496
DE
17897 }
17898
17899 return die;
17900}
17901
3019eac3
DE
17902/* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
17903 attributes.
17904 The caller is responsible for filling in the extra attributes
17905 and updating (*DIEP)->num_attrs.
17906 Set DIEP to point to a newly allocated die with its information,
17907 except for its child, sibling, and parent fields.
17908 Set HAS_CHILDREN to tell whether the die has children or not. */
93311388 17909
d521ce57 17910static const gdb_byte *
3019eac3 17911read_full_die_1 (const struct die_reader_specs *reader,
d521ce57 17912 struct die_info **diep, const gdb_byte *info_ptr,
3019eac3 17913 int *has_children, int num_extra_attrs)
93311388 17914{
b64f50a1 17915 unsigned int abbrev_number, bytes_read, i;
93311388
DE
17916 struct abbrev_info *abbrev;
17917 struct die_info *die;
17918 struct dwarf2_cu *cu = reader->cu;
17919 bfd *abfd = reader->abfd;
17920
9c541725 17921 sect_offset sect_off = (sect_offset) (info_ptr - reader->buffer);
93311388
DE
17922 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
17923 info_ptr += bytes_read;
17924 if (!abbrev_number)
17925 {
17926 *diep = NULL;
17927 *has_children = 0;
17928 return info_ptr;
17929 }
17930
685af9cd 17931 abbrev = reader->abbrev_table->lookup_abbrev (abbrev_number);
93311388 17932 if (!abbrev)
348e048f
DE
17933 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
17934 abbrev_number,
17935 bfd_get_filename (abfd));
17936
3019eac3 17937 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
9c541725 17938 die->sect_off = sect_off;
93311388
DE
17939 die->tag = abbrev->tag;
17940 die->abbrev = abbrev_number;
17941
3019eac3
DE
17942 /* Make the result usable.
17943 The caller needs to update num_attrs after adding the extra
17944 attributes. */
93311388
DE
17945 die->num_attrs = abbrev->num_attrs;
17946
17947 for (i = 0; i < abbrev->num_attrs; ++i)
dee91e82
DE
17948 info_ptr = read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
17949 info_ptr);
93311388
DE
17950
17951 *diep = die;
17952 *has_children = abbrev->has_children;
17953 return info_ptr;
17954}
17955
3019eac3
DE
17956/* Read a die and all its attributes.
17957 Set DIEP to point to a newly allocated die with its information,
17958 except for its child, sibling, and parent fields.
17959 Set HAS_CHILDREN to tell whether the die has children or not. */
17960
d521ce57 17961static const gdb_byte *
3019eac3 17962read_full_die (const struct die_reader_specs *reader,
d521ce57 17963 struct die_info **diep, const gdb_byte *info_ptr,
3019eac3
DE
17964 int *has_children)
17965{
d521ce57 17966 const gdb_byte *result;
bf6af496
DE
17967
17968 result = read_full_die_1 (reader, diep, info_ptr, has_children, 0);
17969
b4f54984 17970 if (dwarf_die_debug)
bf6af496
DE
17971 {
17972 fprintf_unfiltered (gdb_stdlog,
17973 "Read die from %s@0x%x of %s:\n",
a32a8923 17974 get_section_name (reader->die_section),
bf6af496
DE
17975 (unsigned) (info_ptr - reader->die_section->buffer),
17976 bfd_get_filename (reader->abfd));
b4f54984 17977 dump_die (*diep, dwarf_die_debug);
bf6af496
DE
17978 }
17979
17980 return result;
3019eac3 17981}
433df2d4
DE
17982\f
17983/* Abbreviation tables.
3019eac3 17984
433df2d4 17985 In DWARF version 2, the description of the debugging information is
c906108c
SS
17986 stored in a separate .debug_abbrev section. Before we read any
17987 dies from a section we read in all abbreviations and install them
433df2d4
DE
17988 in a hash table. */
17989
17990/* Allocate space for a struct abbrev_info object in ABBREV_TABLE. */
17991
685af9cd
TT
17992struct abbrev_info *
17993abbrev_table::alloc_abbrev ()
433df2d4
DE
17994{
17995 struct abbrev_info *abbrev;
17996
685af9cd 17997 abbrev = XOBNEW (&abbrev_obstack, struct abbrev_info);
433df2d4 17998 memset (abbrev, 0, sizeof (struct abbrev_info));
8d749320 17999
433df2d4
DE
18000 return abbrev;
18001}
18002
18003/* Add an abbreviation to the table. */
c906108c 18004
685af9cd
TT
18005void
18006abbrev_table::add_abbrev (unsigned int abbrev_number,
18007 struct abbrev_info *abbrev)
433df2d4
DE
18008{
18009 unsigned int hash_number;
18010
18011 hash_number = abbrev_number % ABBREV_HASH_SIZE;
4a17f768
YQ
18012 abbrev->next = m_abbrevs[hash_number];
18013 m_abbrevs[hash_number] = abbrev;
433df2d4 18014}
dee91e82 18015
433df2d4
DE
18016/* Look up an abbrev in the table.
18017 Returns NULL if the abbrev is not found. */
18018
685af9cd
TT
18019struct abbrev_info *
18020abbrev_table::lookup_abbrev (unsigned int abbrev_number)
c906108c 18021{
433df2d4
DE
18022 unsigned int hash_number;
18023 struct abbrev_info *abbrev;
18024
18025 hash_number = abbrev_number % ABBREV_HASH_SIZE;
4a17f768 18026 abbrev = m_abbrevs[hash_number];
433df2d4
DE
18027
18028 while (abbrev)
18029 {
18030 if (abbrev->number == abbrev_number)
18031 return abbrev;
18032 abbrev = abbrev->next;
18033 }
18034 return NULL;
18035}
18036
18037/* Read in an abbrev table. */
18038
685af9cd 18039static abbrev_table_up
ed2dc618
SM
18040abbrev_table_read_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
18041 struct dwarf2_section_info *section,
9c541725 18042 sect_offset sect_off)
433df2d4
DE
18043{
18044 struct objfile *objfile = dwarf2_per_objfile->objfile;
a32a8923 18045 bfd *abfd = get_section_bfd_owner (section);
d521ce57 18046 const gdb_byte *abbrev_ptr;
c906108c
SS
18047 struct abbrev_info *cur_abbrev;
18048 unsigned int abbrev_number, bytes_read, abbrev_name;
433df2d4 18049 unsigned int abbrev_form;
f3dd6933
DJ
18050 struct attr_abbrev *cur_attrs;
18051 unsigned int allocated_attrs;
c906108c 18052
685af9cd 18053 abbrev_table_up abbrev_table (new struct abbrev_table (sect_off));
c906108c 18054
433df2d4 18055 dwarf2_read_section (objfile, section);
9c541725 18056 abbrev_ptr = section->buffer + to_underlying (sect_off);
c906108c
SS
18057 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18058 abbrev_ptr += bytes_read;
18059
f3dd6933 18060 allocated_attrs = ATTR_ALLOC_CHUNK;
8d749320 18061 cur_attrs = XNEWVEC (struct attr_abbrev, allocated_attrs);
6e70227d 18062
0963b4bd 18063 /* Loop until we reach an abbrev number of 0. */
c906108c
SS
18064 while (abbrev_number)
18065 {
685af9cd 18066 cur_abbrev = abbrev_table->alloc_abbrev ();
c906108c
SS
18067
18068 /* read in abbrev header */
18069 cur_abbrev->number = abbrev_number;
aead7601
SM
18070 cur_abbrev->tag
18071 = (enum dwarf_tag) read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
c906108c
SS
18072 abbrev_ptr += bytes_read;
18073 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
18074 abbrev_ptr += 1;
18075
18076 /* now read in declarations */
22d2f3ab 18077 for (;;)
c906108c 18078 {
43988095
JK
18079 LONGEST implicit_const;
18080
22d2f3ab
JK
18081 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18082 abbrev_ptr += bytes_read;
18083 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18084 abbrev_ptr += bytes_read;
43988095
JK
18085 if (abbrev_form == DW_FORM_implicit_const)
18086 {
18087 implicit_const = read_signed_leb128 (abfd, abbrev_ptr,
18088 &bytes_read);
18089 abbrev_ptr += bytes_read;
18090 }
18091 else
18092 {
18093 /* Initialize it due to a false compiler warning. */
18094 implicit_const = -1;
18095 }
22d2f3ab
JK
18096
18097 if (abbrev_name == 0)
18098 break;
18099
f3dd6933 18100 if (cur_abbrev->num_attrs == allocated_attrs)
c906108c 18101 {
f3dd6933
DJ
18102 allocated_attrs += ATTR_ALLOC_CHUNK;
18103 cur_attrs
224c3ddb 18104 = XRESIZEVEC (struct attr_abbrev, cur_attrs, allocated_attrs);
c906108c 18105 }
ae038cb0 18106
aead7601
SM
18107 cur_attrs[cur_abbrev->num_attrs].name
18108 = (enum dwarf_attribute) abbrev_name;
22d2f3ab 18109 cur_attrs[cur_abbrev->num_attrs].form
aead7601 18110 = (enum dwarf_form) abbrev_form;
43988095 18111 cur_attrs[cur_abbrev->num_attrs].implicit_const = implicit_const;
22d2f3ab 18112 ++cur_abbrev->num_attrs;
c906108c
SS
18113 }
18114
8d749320
SM
18115 cur_abbrev->attrs =
18116 XOBNEWVEC (&abbrev_table->abbrev_obstack, struct attr_abbrev,
18117 cur_abbrev->num_attrs);
f3dd6933
DJ
18118 memcpy (cur_abbrev->attrs, cur_attrs,
18119 cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
18120
685af9cd 18121 abbrev_table->add_abbrev (abbrev_number, cur_abbrev);
c906108c
SS
18122
18123 /* Get next abbreviation.
18124 Under Irix6 the abbreviations for a compilation unit are not
c5aa993b
JM
18125 always properly terminated with an abbrev number of 0.
18126 Exit loop if we encounter an abbreviation which we have
18127 already read (which means we are about to read the abbreviations
18128 for the next compile unit) or if the end of the abbreviation
18129 table is reached. */
433df2d4 18130 if ((unsigned int) (abbrev_ptr - section->buffer) >= section->size)
c906108c
SS
18131 break;
18132 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18133 abbrev_ptr += bytes_read;
685af9cd 18134 if (abbrev_table->lookup_abbrev (abbrev_number) != NULL)
c906108c
SS
18135 break;
18136 }
f3dd6933
DJ
18137
18138 xfree (cur_attrs);
433df2d4 18139 return abbrev_table;
c906108c
SS
18140}
18141
72bf9492
DJ
18142/* Returns nonzero if TAG represents a type that we might generate a partial
18143 symbol for. */
18144
18145static int
18146is_type_tag_for_partial (int tag)
18147{
18148 switch (tag)
18149 {
18150#if 0
18151 /* Some types that would be reasonable to generate partial symbols for,
18152 that we don't at present. */
18153 case DW_TAG_array_type:
18154 case DW_TAG_file_type:
18155 case DW_TAG_ptr_to_member_type:
18156 case DW_TAG_set_type:
18157 case DW_TAG_string_type:
18158 case DW_TAG_subroutine_type:
18159#endif
18160 case DW_TAG_base_type:
18161 case DW_TAG_class_type:
680b30c7 18162 case DW_TAG_interface_type:
72bf9492
DJ
18163 case DW_TAG_enumeration_type:
18164 case DW_TAG_structure_type:
18165 case DW_TAG_subrange_type:
18166 case DW_TAG_typedef:
18167 case DW_TAG_union_type:
18168 return 1;
18169 default:
18170 return 0;
18171 }
18172}
18173
18174/* Load all DIEs that are interesting for partial symbols into memory. */
18175
18176static struct partial_die_info *
dee91e82 18177load_partial_dies (const struct die_reader_specs *reader,
d521ce57 18178 const gdb_byte *info_ptr, int building_psymtab)
72bf9492 18179{
dee91e82 18180 struct dwarf2_cu *cu = reader->cu;
518817b3 18181 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
72bf9492 18182 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
72bf9492 18183 unsigned int bytes_read;
5afb4e99 18184 unsigned int load_all = 0;
72bf9492
DJ
18185 int nesting_level = 1;
18186
18187 parent_die = NULL;
18188 last_die = NULL;
18189
7adf1e79
DE
18190 gdb_assert (cu->per_cu != NULL);
18191 if (cu->per_cu->load_all_dies)
5afb4e99
DJ
18192 load_all = 1;
18193
72bf9492
DJ
18194 cu->partial_dies
18195 = htab_create_alloc_ex (cu->header.length / 12,
18196 partial_die_hash,
18197 partial_die_eq,
18198 NULL,
18199 &cu->comp_unit_obstack,
18200 hashtab_obstack_allocate,
18201 dummy_obstack_deallocate);
18202
72bf9492
DJ
18203 while (1)
18204 {
685af9cd 18205 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
72bf9492
DJ
18206
18207 /* A NULL abbrev means the end of a series of children. */
18208 if (abbrev == NULL)
18209 {
18210 if (--nesting_level == 0)
cd9983dd
YQ
18211 return first_die;
18212
72bf9492
DJ
18213 info_ptr += bytes_read;
18214 last_die = parent_die;
18215 parent_die = parent_die->die_parent;
18216 continue;
18217 }
18218
98bfdba5
PA
18219 /* Check for template arguments. We never save these; if
18220 they're seen, we just mark the parent, and go on our way. */
18221 if (parent_die != NULL
18222 && cu->language == language_cplus
18223 && (abbrev->tag == DW_TAG_template_type_param
18224 || abbrev->tag == DW_TAG_template_value_param))
18225 {
18226 parent_die->has_template_arguments = 1;
18227
18228 if (!load_all)
18229 {
18230 /* We don't need a partial DIE for the template argument. */
dee91e82 18231 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
98bfdba5
PA
18232 continue;
18233 }
18234 }
18235
0d99eb77 18236 /* We only recurse into c++ subprograms looking for template arguments.
98bfdba5
PA
18237 Skip their other children. */
18238 if (!load_all
18239 && cu->language == language_cplus
18240 && parent_die != NULL
18241 && parent_die->tag == DW_TAG_subprogram)
18242 {
dee91e82 18243 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
98bfdba5
PA
18244 continue;
18245 }
18246
5afb4e99
DJ
18247 /* Check whether this DIE is interesting enough to save. Normally
18248 we would not be interested in members here, but there may be
18249 later variables referencing them via DW_AT_specification (for
18250 static members). */
18251 if (!load_all
18252 && !is_type_tag_for_partial (abbrev->tag)
72929c62 18253 && abbrev->tag != DW_TAG_constant
72bf9492
DJ
18254 && abbrev->tag != DW_TAG_enumerator
18255 && abbrev->tag != DW_TAG_subprogram
b1dc1806 18256 && abbrev->tag != DW_TAG_inlined_subroutine
bc30ff58 18257 && abbrev->tag != DW_TAG_lexical_block
72bf9492 18258 && abbrev->tag != DW_TAG_variable
5afb4e99 18259 && abbrev->tag != DW_TAG_namespace
f55ee35c 18260 && abbrev->tag != DW_TAG_module
95554aad 18261 && abbrev->tag != DW_TAG_member
74921315
KS
18262 && abbrev->tag != DW_TAG_imported_unit
18263 && abbrev->tag != DW_TAG_imported_declaration)
72bf9492
DJ
18264 {
18265 /* Otherwise we skip to the next sibling, if any. */
dee91e82 18266 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
72bf9492
DJ
18267 continue;
18268 }
18269
6f06d47b
YQ
18270 struct partial_die_info pdi ((sect_offset) (info_ptr - reader->buffer),
18271 abbrev);
cd9983dd 18272
48fbe735 18273 info_ptr = pdi.read (reader, *abbrev, info_ptr + bytes_read);
72bf9492
DJ
18274
18275 /* This two-pass algorithm for processing partial symbols has a
18276 high cost in cache pressure. Thus, handle some simple cases
18277 here which cover the majority of C partial symbols. DIEs
18278 which neither have specification tags in them, nor could have
18279 specification tags elsewhere pointing at them, can simply be
18280 processed and discarded.
18281
18282 This segment is also optional; scan_partial_symbols and
18283 add_partial_symbol will handle these DIEs if we chain
18284 them in normally. When compilers which do not emit large
18285 quantities of duplicate debug information are more common,
18286 this code can probably be removed. */
18287
18288 /* Any complete simple types at the top level (pretty much all
18289 of them, for a language without namespaces), can be processed
18290 directly. */
18291 if (parent_die == NULL
cd9983dd
YQ
18292 && pdi.has_specification == 0
18293 && pdi.is_declaration == 0
18294 && ((pdi.tag == DW_TAG_typedef && !pdi.has_children)
18295 || pdi.tag == DW_TAG_base_type
18296 || pdi.tag == DW_TAG_subrange_type))
72bf9492 18297 {
cd9983dd
YQ
18298 if (building_psymtab && pdi.name != NULL)
18299 add_psymbol_to_list (pdi.name, strlen (pdi.name), 0,
72bf9492 18300 VAR_DOMAIN, LOC_TYPEDEF,
bb5ed363 18301 &objfile->static_psymbols,
1762568f 18302 0, cu->language, objfile);
cd9983dd 18303 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
72bf9492
DJ
18304 continue;
18305 }
18306
d8228535
JK
18307 /* The exception for DW_TAG_typedef with has_children above is
18308 a workaround of GCC PR debug/47510. In the case of this complaint
18309 type_name_no_tag_or_error will error on such types later.
18310
18311 GDB skipped children of DW_TAG_typedef by the shortcut above and then
18312 it could not find the child DIEs referenced later, this is checked
18313 above. In correct DWARF DW_TAG_typedef should have no children. */
18314
cd9983dd 18315 if (pdi.tag == DW_TAG_typedef && pdi.has_children)
d8228535
JK
18316 complaint (&symfile_complaints,
18317 _("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
9d8780f0 18318 "- DIE at %s [in module %s]"),
cd9983dd 18319 sect_offset_str (pdi.sect_off), objfile_name (objfile));
d8228535 18320
72bf9492
DJ
18321 /* If we're at the second level, and we're an enumerator, and
18322 our parent has no specification (meaning possibly lives in a
18323 namespace elsewhere), then we can add the partial symbol now
18324 instead of queueing it. */
cd9983dd 18325 if (pdi.tag == DW_TAG_enumerator
72bf9492
DJ
18326 && parent_die != NULL
18327 && parent_die->die_parent == NULL
18328 && parent_die->tag == DW_TAG_enumeration_type
18329 && parent_die->has_specification == 0)
18330 {
cd9983dd 18331 if (pdi.name == NULL)
3e43a32a
MS
18332 complaint (&symfile_complaints,
18333 _("malformed enumerator DIE ignored"));
72bf9492 18334 else if (building_psymtab)
cd9983dd 18335 add_psymbol_to_list (pdi.name, strlen (pdi.name), 0,
72bf9492 18336 VAR_DOMAIN, LOC_CONST,
9c37b5ae 18337 cu->language == language_cplus
bb5ed363
DE
18338 ? &objfile->global_psymbols
18339 : &objfile->static_psymbols,
1762568f 18340 0, cu->language, objfile);
72bf9492 18341
cd9983dd 18342 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
72bf9492
DJ
18343 continue;
18344 }
18345
cd9983dd 18346 struct partial_die_info *part_die
6f06d47b 18347 = new (&cu->comp_unit_obstack) partial_die_info (pdi);
cd9983dd 18348
72bf9492
DJ
18349 /* We'll save this DIE so link it in. */
18350 part_die->die_parent = parent_die;
18351 part_die->die_sibling = NULL;
18352 part_die->die_child = NULL;
18353
18354 if (last_die && last_die == parent_die)
18355 last_die->die_child = part_die;
18356 else if (last_die)
18357 last_die->die_sibling = part_die;
18358
18359 last_die = part_die;
18360
18361 if (first_die == NULL)
18362 first_die = part_die;
18363
18364 /* Maybe add the DIE to the hash table. Not all DIEs that we
18365 find interesting need to be in the hash table, because we
18366 also have the parent/sibling/child chains; only those that we
18367 might refer to by offset later during partial symbol reading.
18368
18369 For now this means things that might have be the target of a
18370 DW_AT_specification, DW_AT_abstract_origin, or
18371 DW_AT_extension. DW_AT_extension will refer only to
18372 namespaces; DW_AT_abstract_origin refers to functions (and
18373 many things under the function DIE, but we do not recurse
18374 into function DIEs during partial symbol reading) and
18375 possibly variables as well; DW_AT_specification refers to
18376 declarations. Declarations ought to have the DW_AT_declaration
18377 flag. It happens that GCC forgets to put it in sometimes, but
18378 only for functions, not for types.
18379
18380 Adding more things than necessary to the hash table is harmless
18381 except for the performance cost. Adding too few will result in
5afb4e99
DJ
18382 wasted time in find_partial_die, when we reread the compilation
18383 unit with load_all_dies set. */
72bf9492 18384
5afb4e99 18385 if (load_all
72929c62 18386 || abbrev->tag == DW_TAG_constant
5afb4e99 18387 || abbrev->tag == DW_TAG_subprogram
72bf9492
DJ
18388 || abbrev->tag == DW_TAG_variable
18389 || abbrev->tag == DW_TAG_namespace
18390 || part_die->is_declaration)
18391 {
18392 void **slot;
18393
18394 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
9c541725
PA
18395 to_underlying (part_die->sect_off),
18396 INSERT);
72bf9492
DJ
18397 *slot = part_die;
18398 }
18399
72bf9492 18400 /* For some DIEs we want to follow their children (if any). For C
bc30ff58 18401 we have no reason to follow the children of structures; for other
98bfdba5
PA
18402 languages we have to, so that we can get at method physnames
18403 to infer fully qualified class names, for DW_AT_specification,
18404 and for C++ template arguments. For C++, we also look one level
18405 inside functions to find template arguments (if the name of the
18406 function does not already contain the template arguments).
bc30ff58
JB
18407
18408 For Ada, we need to scan the children of subprograms and lexical
18409 blocks as well because Ada allows the definition of nested
18410 entities that could be interesting for the debugger, such as
18411 nested subprograms for instance. */
72bf9492 18412 if (last_die->has_children
5afb4e99
DJ
18413 && (load_all
18414 || last_die->tag == DW_TAG_namespace
f55ee35c 18415 || last_die->tag == DW_TAG_module
72bf9492 18416 || last_die->tag == DW_TAG_enumeration_type
98bfdba5
PA
18417 || (cu->language == language_cplus
18418 && last_die->tag == DW_TAG_subprogram
18419 && (last_die->name == NULL
18420 || strchr (last_die->name, '<') == NULL))
72bf9492
DJ
18421 || (cu->language != language_c
18422 && (last_die->tag == DW_TAG_class_type
680b30c7 18423 || last_die->tag == DW_TAG_interface_type
72bf9492 18424 || last_die->tag == DW_TAG_structure_type
bc30ff58
JB
18425 || last_die->tag == DW_TAG_union_type))
18426 || (cu->language == language_ada
18427 && (last_die->tag == DW_TAG_subprogram
18428 || last_die->tag == DW_TAG_lexical_block))))
72bf9492
DJ
18429 {
18430 nesting_level++;
18431 parent_die = last_die;
18432 continue;
18433 }
18434
18435 /* Otherwise we skip to the next sibling, if any. */
dee91e82 18436 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
72bf9492
DJ
18437
18438 /* Back to the top, do it again. */
18439 }
18440}
18441
6f06d47b
YQ
18442partial_die_info::partial_die_info (sect_offset sect_off_,
18443 struct abbrev_info *abbrev)
18444 : partial_die_info (sect_off_, abbrev->tag, abbrev->has_children)
18445{
18446}
18447
35cc7ed7
YQ
18448/* Read a minimal amount of information into the minimal die structure.
18449 INFO_PTR should point just after the initial uleb128 of a DIE. */
c906108c 18450
48fbe735
YQ
18451const gdb_byte *
18452partial_die_info::read (const struct die_reader_specs *reader,
18453 const struct abbrev_info &abbrev, const gdb_byte *info_ptr)
c906108c 18454{
dee91e82 18455 struct dwarf2_cu *cu = reader->cu;
518817b3
SM
18456 struct dwarf2_per_objfile *dwarf2_per_objfile
18457 = cu->per_cu->dwarf2_per_objfile;
fa238c03 18458 unsigned int i;
c5aa993b 18459 int has_low_pc_attr = 0;
c906108c 18460 int has_high_pc_attr = 0;
91da1414 18461 int high_pc_relative = 0;
c906108c 18462
fd0a254f 18463 for (i = 0; i < abbrev.num_attrs; ++i)
c906108c 18464 {
48fbe735
YQ
18465 struct attribute attr;
18466
fd0a254f 18467 info_ptr = read_attribute (reader, &attr, &abbrev.attrs[i], info_ptr);
c906108c
SS
18468
18469 /* Store the data if it is of an attribute we want to keep in a
c5aa993b 18470 partial symbol table. */
c906108c
SS
18471 switch (attr.name)
18472 {
18473 case DW_AT_name:
48fbe735 18474 switch (tag)
71c25dea
TT
18475 {
18476 case DW_TAG_compile_unit:
95554aad 18477 case DW_TAG_partial_unit:
348e048f 18478 case DW_TAG_type_unit:
71c25dea
TT
18479 /* Compilation units have a DW_AT_name that is a filename, not
18480 a source language identifier. */
18481 case DW_TAG_enumeration_type:
18482 case DW_TAG_enumerator:
18483 /* These tags always have simple identifiers already; no need
18484 to canonicalize them. */
48fbe735 18485 name = DW_STRING (&attr);
71c25dea
TT
18486 break;
18487 default:
48fbe735
YQ
18488 {
18489 struct objfile *objfile = dwarf2_per_objfile->objfile;
18490
18491 name
18492 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
18493 &objfile->per_bfd->storage_obstack);
18494 }
71c25dea
TT
18495 break;
18496 }
c906108c 18497 break;
31ef98ae 18498 case DW_AT_linkage_name:
c906108c 18499 case DW_AT_MIPS_linkage_name:
31ef98ae
TT
18500 /* Note that both forms of linkage name might appear. We
18501 assume they will be the same, and we only store the last
18502 one we see. */
94af9270 18503 if (cu->language == language_ada)
48fbe735
YQ
18504 name = DW_STRING (&attr);
18505 linkage_name = DW_STRING (&attr);
c906108c
SS
18506 break;
18507 case DW_AT_low_pc:
18508 has_low_pc_attr = 1;
48fbe735 18509 lowpc = attr_value_as_address (&attr);
c906108c
SS
18510 break;
18511 case DW_AT_high_pc:
18512 has_high_pc_attr = 1;
48fbe735 18513 highpc = attr_value_as_address (&attr);
31aa7e4e
JB
18514 if (cu->header.version >= 4 && attr_form_is_constant (&attr))
18515 high_pc_relative = 1;
c906108c
SS
18516 break;
18517 case DW_AT_location:
0963b4bd 18518 /* Support the .debug_loc offsets. */
8e19ed76
PS
18519 if (attr_form_is_block (&attr))
18520 {
48fbe735 18521 d.locdesc = DW_BLOCK (&attr);
8e19ed76 18522 }
3690dd37 18523 else if (attr_form_is_section_offset (&attr))
8e19ed76 18524 {
4d3c2250 18525 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
18526 }
18527 else
18528 {
4d3c2250
KB
18529 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
18530 "partial symbol information");
8e19ed76 18531 }
c906108c 18532 break;
c906108c 18533 case DW_AT_external:
48fbe735 18534 is_external = DW_UNSND (&attr);
c906108c
SS
18535 break;
18536 case DW_AT_declaration:
48fbe735 18537 is_declaration = DW_UNSND (&attr);
c906108c
SS
18538 break;
18539 case DW_AT_type:
48fbe735 18540 has_type = 1;
c906108c
SS
18541 break;
18542 case DW_AT_abstract_origin:
18543 case DW_AT_specification:
72bf9492 18544 case DW_AT_extension:
48fbe735
YQ
18545 has_specification = 1;
18546 spec_offset = dwarf2_get_ref_die_offset (&attr);
18547 spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
36586728 18548 || cu->per_cu->is_dwz);
c906108c
SS
18549 break;
18550 case DW_AT_sibling:
18551 /* Ignore absolute siblings, they might point outside of
18552 the current compile unit. */
18553 if (attr.form == DW_FORM_ref_addr)
3e43a32a
MS
18554 complaint (&symfile_complaints,
18555 _("ignoring absolute DW_AT_sibling"));
c906108c 18556 else
b9502d3f 18557 {
48fbe735 18558 const gdb_byte *buffer = reader->buffer;
9c541725
PA
18559 sect_offset off = dwarf2_get_ref_die_offset (&attr);
18560 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
b9502d3f
WN
18561
18562 if (sibling_ptr < info_ptr)
18563 complaint (&symfile_complaints,
18564 _("DW_AT_sibling points backwards"));
22869d73
KS
18565 else if (sibling_ptr > reader->buffer_end)
18566 dwarf2_section_buffer_overflow_complaint (reader->die_section);
b9502d3f 18567 else
48fbe735 18568 sibling = sibling_ptr;
b9502d3f 18569 }
c906108c 18570 break;
fa4028e9 18571 case DW_AT_byte_size:
48fbe735 18572 has_byte_size = 1;
fa4028e9 18573 break;
ff908ebf 18574 case DW_AT_const_value:
48fbe735 18575 has_const_value = 1;
ff908ebf 18576 break;
68511cec
CES
18577 case DW_AT_calling_convention:
18578 /* DWARF doesn't provide a way to identify a program's source-level
18579 entry point. DW_AT_calling_convention attributes are only meant
18580 to describe functions' calling conventions.
18581
18582 However, because it's a necessary piece of information in
0c1b455e
TT
18583 Fortran, and before DWARF 4 DW_CC_program was the only
18584 piece of debugging information whose definition refers to
18585 a 'main program' at all, several compilers marked Fortran
18586 main programs with DW_CC_program --- even when those
18587 functions use the standard calling conventions.
18588
18589 Although DWARF now specifies a way to provide this
18590 information, we support this practice for backward
18591 compatibility. */
68511cec 18592 if (DW_UNSND (&attr) == DW_CC_program
0c1b455e 18593 && cu->language == language_fortran)
48fbe735 18594 main_subprogram = 1;
68511cec 18595 break;
481860b3
GB
18596 case DW_AT_inline:
18597 if (DW_UNSND (&attr) == DW_INL_inlined
18598 || DW_UNSND (&attr) == DW_INL_declared_inlined)
48fbe735 18599 may_be_inlined = 1;
481860b3 18600 break;
95554aad
TT
18601
18602 case DW_AT_import:
48fbe735 18603 if (tag == DW_TAG_imported_unit)
36586728 18604 {
48fbe735
YQ
18605 d.sect_off = dwarf2_get_ref_die_offset (&attr);
18606 is_dwz = (attr.form == DW_FORM_GNU_ref_alt
36586728
TT
18607 || cu->per_cu->is_dwz);
18608 }
95554aad
TT
18609 break;
18610
0c1b455e 18611 case DW_AT_main_subprogram:
48fbe735 18612 main_subprogram = DW_UNSND (&attr);
0c1b455e
TT
18613 break;
18614
c906108c
SS
18615 default:
18616 break;
18617 }
18618 }
18619
91da1414 18620 if (high_pc_relative)
48fbe735 18621 highpc += lowpc;
91da1414 18622
9373cf26
JK
18623 if (has_low_pc_attr && has_high_pc_attr)
18624 {
18625 /* When using the GNU linker, .gnu.linkonce. sections are used to
18626 eliminate duplicate copies of functions and vtables and such.
18627 The linker will arbitrarily choose one and discard the others.
18628 The AT_*_pc values for such functions refer to local labels in
18629 these sections. If the section from that file was discarded, the
18630 labels are not in the output, so the relocs get a value of 0.
18631 If this is a discarded function, mark the pc bounds as invalid,
18632 so that GDB will ignore it. */
48fbe735 18633 if (lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
9373cf26 18634 {
48fbe735 18635 struct objfile *objfile = dwarf2_per_objfile->objfile;
bb5ed363 18636 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9373cf26
JK
18637
18638 complaint (&symfile_complaints,
18639 _("DW_AT_low_pc %s is zero "
9d8780f0 18640 "for DIE at %s [in module %s]"),
48fbe735
YQ
18641 paddress (gdbarch, lowpc),
18642 sect_offset_str (sect_off),
9d8780f0 18643 objfile_name (objfile));
9373cf26
JK
18644 }
18645 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
48fbe735 18646 else if (lowpc >= highpc)
9373cf26 18647 {
48fbe735 18648 struct objfile *objfile = dwarf2_per_objfile->objfile;
bb5ed363 18649 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9373cf26
JK
18650
18651 complaint (&symfile_complaints,
18652 _("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
9d8780f0 18653 "for DIE at %s [in module %s]"),
48fbe735
YQ
18654 paddress (gdbarch, lowpc),
18655 paddress (gdbarch, highpc),
18656 sect_offset_str (sect_off),
9c541725 18657 objfile_name (objfile));
9373cf26
JK
18658 }
18659 else
48fbe735 18660 has_pc_info = 1;
9373cf26 18661 }
85cbf3d3 18662
c906108c
SS
18663 return info_ptr;
18664}
18665
72bf9492
DJ
18666/* Find a cached partial DIE at OFFSET in CU. */
18667
d590ff25
YQ
18668struct partial_die_info *
18669dwarf2_cu::find_partial_die (sect_offset sect_off)
72bf9492
DJ
18670{
18671 struct partial_die_info *lookup_die = NULL;
6f06d47b 18672 struct partial_die_info part_die (sect_off);
72bf9492 18673
9a3c8263 18674 lookup_die = ((struct partial_die_info *)
d590ff25 18675 htab_find_with_hash (partial_dies, &part_die,
9c541725 18676 to_underlying (sect_off)));
72bf9492 18677
72bf9492
DJ
18678 return lookup_die;
18679}
18680
348e048f
DE
18681/* Find a partial DIE at OFFSET, which may or may not be in CU,
18682 except in the case of .debug_types DIEs which do not reference
18683 outside their CU (they do however referencing other types via
55f1336d 18684 DW_FORM_ref_sig8). */
72bf9492
DJ
18685
18686static struct partial_die_info *
9c541725 18687find_partial_die (sect_offset sect_off, int offset_in_dwz, struct dwarf2_cu *cu)
72bf9492 18688{
518817b3
SM
18689 struct dwarf2_per_objfile *dwarf2_per_objfile
18690 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 18691 struct objfile *objfile = dwarf2_per_objfile->objfile;
5afb4e99
DJ
18692 struct dwarf2_per_cu_data *per_cu = NULL;
18693 struct partial_die_info *pd = NULL;
72bf9492 18694
36586728 18695 if (offset_in_dwz == cu->per_cu->is_dwz
9c541725 18696 && offset_in_cu_p (&cu->header, sect_off))
5afb4e99 18697 {
d590ff25 18698 pd = cu->find_partial_die (sect_off);
5afb4e99
DJ
18699 if (pd != NULL)
18700 return pd;
0d99eb77
DE
18701 /* We missed recording what we needed.
18702 Load all dies and try again. */
18703 per_cu = cu->per_cu;
5afb4e99 18704 }
0d99eb77
DE
18705 else
18706 {
18707 /* TUs don't reference other CUs/TUs (except via type signatures). */
3019eac3 18708 if (cu->per_cu->is_debug_types)
0d99eb77 18709 {
9d8780f0
SM
18710 error (_("Dwarf Error: Type Unit at offset %s contains"
18711 " external reference to offset %s [in module %s].\n"),
18712 sect_offset_str (cu->header.sect_off), sect_offset_str (sect_off),
0d99eb77
DE
18713 bfd_get_filename (objfile->obfd));
18714 }
9c541725 18715 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
ed2dc618 18716 dwarf2_per_objfile);
72bf9492 18717
0d99eb77
DE
18718 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
18719 load_partial_comp_unit (per_cu);
ae038cb0 18720
0d99eb77 18721 per_cu->cu->last_used = 0;
d590ff25 18722 pd = per_cu->cu->find_partial_die (sect_off);
0d99eb77 18723 }
5afb4e99 18724
dee91e82
DE
18725 /* If we didn't find it, and not all dies have been loaded,
18726 load them all and try again. */
18727
5afb4e99
DJ
18728 if (pd == NULL && per_cu->load_all_dies == 0)
18729 {
5afb4e99 18730 per_cu->load_all_dies = 1;
fd820528
DE
18731
18732 /* This is nasty. When we reread the DIEs, somewhere up the call chain
18733 THIS_CU->cu may already be in use. So we can't just free it and
18734 replace its DIEs with the ones we read in. Instead, we leave those
18735 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
18736 and clobber THIS_CU->cu->partial_dies with the hash table for the new
18737 set. */
dee91e82 18738 load_partial_comp_unit (per_cu);
5afb4e99 18739
d590ff25 18740 pd = per_cu->cu->find_partial_die (sect_off);
5afb4e99
DJ
18741 }
18742
18743 if (pd == NULL)
18744 internal_error (__FILE__, __LINE__,
9d8780f0 18745 _("could not find partial DIE %s "
3e43a32a 18746 "in cache [from module %s]\n"),
9d8780f0 18747 sect_offset_str (sect_off), bfd_get_filename (objfile->obfd));
5afb4e99 18748 return pd;
72bf9492
DJ
18749}
18750
abc72ce4
DE
18751/* See if we can figure out if the class lives in a namespace. We do
18752 this by looking for a member function; its demangled name will
18753 contain namespace info, if there is any. */
18754
18755static void
18756guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
18757 struct dwarf2_cu *cu)
18758{
18759 /* NOTE: carlton/2003-10-07: Getting the info this way changes
18760 what template types look like, because the demangler
18761 frequently doesn't give the same name as the debug info. We
18762 could fix this by only using the demangled name to get the
18763 prefix (but see comment in read_structure_type). */
18764
18765 struct partial_die_info *real_pdi;
18766 struct partial_die_info *child_pdi;
18767
18768 /* If this DIE (this DIE's specification, if any) has a parent, then
18769 we should not do this. We'll prepend the parent's fully qualified
18770 name when we create the partial symbol. */
18771
18772 real_pdi = struct_pdi;
18773 while (real_pdi->has_specification)
36586728
TT
18774 real_pdi = find_partial_die (real_pdi->spec_offset,
18775 real_pdi->spec_is_dwz, cu);
abc72ce4
DE
18776
18777 if (real_pdi->die_parent != NULL)
18778 return;
18779
18780 for (child_pdi = struct_pdi->die_child;
18781 child_pdi != NULL;
18782 child_pdi = child_pdi->die_sibling)
18783 {
18784 if (child_pdi->tag == DW_TAG_subprogram
18785 && child_pdi->linkage_name != NULL)
18786 {
18787 char *actual_class_name
18788 = language_class_name_from_physname (cu->language_defn,
18789 child_pdi->linkage_name);
18790 if (actual_class_name != NULL)
18791 {
518817b3 18792 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
abc72ce4 18793 struct_pdi->name
224c3ddb 18794 = ((const char *)
e3b94546 18795 obstack_copy0 (&objfile->per_bfd->storage_obstack,
224c3ddb
SM
18796 actual_class_name,
18797 strlen (actual_class_name)));
abc72ce4
DE
18798 xfree (actual_class_name);
18799 }
18800 break;
18801 }
18802 }
18803}
18804
52356b79
YQ
18805void
18806partial_die_info::fixup (struct dwarf2_cu *cu)
72bf9492 18807{
abc72ce4
DE
18808 /* Once we've fixed up a die, there's no point in doing so again.
18809 This also avoids a memory leak if we were to call
18810 guess_partial_die_structure_name multiple times. */
52356b79 18811 if (fixup_called)
abc72ce4
DE
18812 return;
18813
72bf9492
DJ
18814 /* If we found a reference attribute and the DIE has no name, try
18815 to find a name in the referred to DIE. */
18816
52356b79 18817 if (name == NULL && has_specification)
72bf9492
DJ
18818 {
18819 struct partial_die_info *spec_die;
72bf9492 18820
52356b79 18821 spec_die = find_partial_die (spec_offset, spec_is_dwz, cu);
72bf9492 18822
52356b79 18823 spec_die->fixup (cu);
72bf9492
DJ
18824
18825 if (spec_die->name)
18826 {
52356b79 18827 name = spec_die->name;
72bf9492
DJ
18828
18829 /* Copy DW_AT_external attribute if it is set. */
18830 if (spec_die->is_external)
52356b79 18831 is_external = spec_die->is_external;
72bf9492
DJ
18832 }
18833 }
18834
18835 /* Set default names for some unnamed DIEs. */
72bf9492 18836
52356b79
YQ
18837 if (name == NULL && tag == DW_TAG_namespace)
18838 name = CP_ANONYMOUS_NAMESPACE_STR;
72bf9492 18839
abc72ce4
DE
18840 /* If there is no parent die to provide a namespace, and there are
18841 children, see if we can determine the namespace from their linkage
122d1940 18842 name. */
abc72ce4 18843 if (cu->language == language_cplus
518817b3
SM
18844 && !VEC_empty (dwarf2_section_info_def,
18845 cu->per_cu->dwarf2_per_objfile->types)
52356b79
YQ
18846 && die_parent == NULL
18847 && has_children
18848 && (tag == DW_TAG_class_type
18849 || tag == DW_TAG_structure_type
18850 || tag == DW_TAG_union_type))
18851 guess_partial_die_structure_name (this, cu);
abc72ce4 18852
53832f31
TT
18853 /* GCC might emit a nameless struct or union that has a linkage
18854 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
52356b79
YQ
18855 if (name == NULL
18856 && (tag == DW_TAG_class_type
18857 || tag == DW_TAG_interface_type
18858 || tag == DW_TAG_structure_type
18859 || tag == DW_TAG_union_type)
18860 && linkage_name != NULL)
53832f31
TT
18861 {
18862 char *demangled;
18863
52356b79 18864 demangled = gdb_demangle (linkage_name, DMGL_TYPES);
53832f31
TT
18865 if (demangled)
18866 {
96408a79
SA
18867 const char *base;
18868
18869 /* Strip any leading namespaces/classes, keep only the base name.
18870 DW_AT_name for named DIEs does not contain the prefixes. */
18871 base = strrchr (demangled, ':');
18872 if (base && base > demangled && base[-1] == ':')
18873 base++;
18874 else
18875 base = demangled;
18876
518817b3 18877 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
52356b79 18878 name
224c3ddb 18879 = ((const char *)
e3b94546 18880 obstack_copy0 (&objfile->per_bfd->storage_obstack,
224c3ddb 18881 base, strlen (base)));
53832f31
TT
18882 xfree (demangled);
18883 }
18884 }
18885
52356b79 18886 fixup_called = 1;
72bf9492
DJ
18887}
18888
a8329558 18889/* Read an attribute value described by an attribute form. */
c906108c 18890
d521ce57 18891static const gdb_byte *
dee91e82
DE
18892read_attribute_value (const struct die_reader_specs *reader,
18893 struct attribute *attr, unsigned form,
43988095 18894 LONGEST implicit_const, const gdb_byte *info_ptr)
c906108c 18895{
dee91e82 18896 struct dwarf2_cu *cu = reader->cu;
518817b3
SM
18897 struct dwarf2_per_objfile *dwarf2_per_objfile
18898 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 18899 struct objfile *objfile = dwarf2_per_objfile->objfile;
3e29f34a 18900 struct gdbarch *gdbarch = get_objfile_arch (objfile);
dee91e82 18901 bfd *abfd = reader->abfd;
e7c27a73 18902 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
18903 unsigned int bytes_read;
18904 struct dwarf_block *blk;
18905
aead7601 18906 attr->form = (enum dwarf_form) form;
a8329558 18907 switch (form)
c906108c 18908 {
c906108c 18909 case DW_FORM_ref_addr:
ae411497 18910 if (cu->header.version == 2)
4568ecf9 18911 DW_UNSND (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
ae411497 18912 else
4568ecf9
DE
18913 DW_UNSND (attr) = read_offset (abfd, info_ptr,
18914 &cu->header, &bytes_read);
ae411497
TT
18915 info_ptr += bytes_read;
18916 break;
36586728
TT
18917 case DW_FORM_GNU_ref_alt:
18918 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
18919 info_ptr += bytes_read;
18920 break;
ae411497 18921 case DW_FORM_addr:
e7c27a73 18922 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
3e29f34a 18923 DW_ADDR (attr) = gdbarch_adjust_dwarf2_addr (gdbarch, DW_ADDR (attr));
107d2387 18924 info_ptr += bytes_read;
c906108c
SS
18925 break;
18926 case DW_FORM_block2:
7b5a2f43 18927 blk = dwarf_alloc_block (cu);
c906108c
SS
18928 blk->size = read_2_bytes (abfd, info_ptr);
18929 info_ptr += 2;
18930 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
18931 info_ptr += blk->size;
18932 DW_BLOCK (attr) = blk;
18933 break;
18934 case DW_FORM_block4:
7b5a2f43 18935 blk = dwarf_alloc_block (cu);
c906108c
SS
18936 blk->size = read_4_bytes (abfd, info_ptr);
18937 info_ptr += 4;
18938 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
18939 info_ptr += blk->size;
18940 DW_BLOCK (attr) = blk;
18941 break;
18942 case DW_FORM_data2:
18943 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
18944 info_ptr += 2;
18945 break;
18946 case DW_FORM_data4:
18947 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
18948 info_ptr += 4;
18949 break;
18950 case DW_FORM_data8:
18951 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
18952 info_ptr += 8;
18953 break;
0224619f
JK
18954 case DW_FORM_data16:
18955 blk = dwarf_alloc_block (cu);
18956 blk->size = 16;
18957 blk->data = read_n_bytes (abfd, info_ptr, 16);
18958 info_ptr += 16;
18959 DW_BLOCK (attr) = blk;
18960 break;
2dc7f7b3
TT
18961 case DW_FORM_sec_offset:
18962 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
18963 info_ptr += bytes_read;
18964 break;
c906108c 18965 case DW_FORM_string:
9b1c24c8 18966 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
8285870a 18967 DW_STRING_IS_CANONICAL (attr) = 0;
c906108c
SS
18968 info_ptr += bytes_read;
18969 break;
4bdf3d34 18970 case DW_FORM_strp:
36586728
TT
18971 if (!cu->per_cu->is_dwz)
18972 {
ed2dc618
SM
18973 DW_STRING (attr) = read_indirect_string (dwarf2_per_objfile,
18974 abfd, info_ptr, cu_header,
36586728
TT
18975 &bytes_read);
18976 DW_STRING_IS_CANONICAL (attr) = 0;
18977 info_ptr += bytes_read;
18978 break;
18979 }
18980 /* FALLTHROUGH */
43988095
JK
18981 case DW_FORM_line_strp:
18982 if (!cu->per_cu->is_dwz)
18983 {
ed2dc618
SM
18984 DW_STRING (attr) = read_indirect_line_string (dwarf2_per_objfile,
18985 abfd, info_ptr,
43988095
JK
18986 cu_header, &bytes_read);
18987 DW_STRING_IS_CANONICAL (attr) = 0;
18988 info_ptr += bytes_read;
18989 break;
18990 }
18991 /* FALLTHROUGH */
36586728
TT
18992 case DW_FORM_GNU_strp_alt:
18993 {
ed2dc618 18994 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
36586728
TT
18995 LONGEST str_offset = read_offset (abfd, info_ptr, cu_header,
18996 &bytes_read);
18997
ed2dc618
SM
18998 DW_STRING (attr) = read_indirect_string_from_dwz (objfile,
18999 dwz, str_offset);
36586728
TT
19000 DW_STRING_IS_CANONICAL (attr) = 0;
19001 info_ptr += bytes_read;
19002 }
4bdf3d34 19003 break;
2dc7f7b3 19004 case DW_FORM_exprloc:
c906108c 19005 case DW_FORM_block:
7b5a2f43 19006 blk = dwarf_alloc_block (cu);
c906108c
SS
19007 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19008 info_ptr += bytes_read;
19009 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19010 info_ptr += blk->size;
19011 DW_BLOCK (attr) = blk;
19012 break;
19013 case DW_FORM_block1:
7b5a2f43 19014 blk = dwarf_alloc_block (cu);
c906108c
SS
19015 blk->size = read_1_byte (abfd, info_ptr);
19016 info_ptr += 1;
19017 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19018 info_ptr += blk->size;
19019 DW_BLOCK (attr) = blk;
19020 break;
19021 case DW_FORM_data1:
19022 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
19023 info_ptr += 1;
19024 break;
19025 case DW_FORM_flag:
19026 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
19027 info_ptr += 1;
19028 break;
2dc7f7b3
TT
19029 case DW_FORM_flag_present:
19030 DW_UNSND (attr) = 1;
19031 break;
c906108c
SS
19032 case DW_FORM_sdata:
19033 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
19034 info_ptr += bytes_read;
19035 break;
19036 case DW_FORM_udata:
19037 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19038 info_ptr += bytes_read;
19039 break;
19040 case DW_FORM_ref1:
9c541725 19041 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 19042 + read_1_byte (abfd, info_ptr));
c906108c
SS
19043 info_ptr += 1;
19044 break;
19045 case DW_FORM_ref2:
9c541725 19046 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 19047 + read_2_bytes (abfd, info_ptr));
c906108c
SS
19048 info_ptr += 2;
19049 break;
19050 case DW_FORM_ref4:
9c541725 19051 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 19052 + read_4_bytes (abfd, info_ptr));
c906108c
SS
19053 info_ptr += 4;
19054 break;
613e1657 19055 case DW_FORM_ref8:
9c541725 19056 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 19057 + read_8_bytes (abfd, info_ptr));
613e1657
KB
19058 info_ptr += 8;
19059 break;
55f1336d 19060 case DW_FORM_ref_sig8:
ac9ec31b 19061 DW_SIGNATURE (attr) = read_8_bytes (abfd, info_ptr);
348e048f
DE
19062 info_ptr += 8;
19063 break;
c906108c 19064 case DW_FORM_ref_udata:
9c541725 19065 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 19066 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
c906108c
SS
19067 info_ptr += bytes_read;
19068 break;
c906108c 19069 case DW_FORM_indirect:
a8329558
KW
19070 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19071 info_ptr += bytes_read;
43988095
JK
19072 if (form == DW_FORM_implicit_const)
19073 {
19074 implicit_const = read_signed_leb128 (abfd, info_ptr, &bytes_read);
19075 info_ptr += bytes_read;
19076 }
19077 info_ptr = read_attribute_value (reader, attr, form, implicit_const,
19078 info_ptr);
19079 break;
19080 case DW_FORM_implicit_const:
19081 DW_SND (attr) = implicit_const;
a8329558 19082 break;
3019eac3
DE
19083 case DW_FORM_GNU_addr_index:
19084 if (reader->dwo_file == NULL)
19085 {
19086 /* For now flag a hard error.
19087 Later we can turn this into a complaint. */
19088 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
19089 dwarf_form_name (form),
19090 bfd_get_filename (abfd));
19091 }
19092 DW_ADDR (attr) = read_addr_index_from_leb128 (cu, info_ptr, &bytes_read);
19093 info_ptr += bytes_read;
19094 break;
19095 case DW_FORM_GNU_str_index:
19096 if (reader->dwo_file == NULL)
19097 {
19098 /* For now flag a hard error.
19099 Later we can turn this into a complaint if warranted. */
19100 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
19101 dwarf_form_name (form),
19102 bfd_get_filename (abfd));
19103 }
19104 {
19105 ULONGEST str_index =
19106 read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19107
342587c4 19108 DW_STRING (attr) = read_str_index (reader, str_index);
3019eac3
DE
19109 DW_STRING_IS_CANONICAL (attr) = 0;
19110 info_ptr += bytes_read;
19111 }
19112 break;
c906108c 19113 default:
8a3fe4f8 19114 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
659b0389
ML
19115 dwarf_form_name (form),
19116 bfd_get_filename (abfd));
c906108c 19117 }
28e94949 19118
36586728 19119 /* Super hack. */
7771576e 19120 if (cu->per_cu->is_dwz && attr_form_is_ref (attr))
36586728
TT
19121 attr->form = DW_FORM_GNU_ref_alt;
19122
28e94949
JB
19123 /* We have seen instances where the compiler tried to emit a byte
19124 size attribute of -1 which ended up being encoded as an unsigned
19125 0xffffffff. Although 0xffffffff is technically a valid size value,
19126 an object of this size seems pretty unlikely so we can relatively
19127 safely treat these cases as if the size attribute was invalid and
19128 treat them as zero by default. */
19129 if (attr->name == DW_AT_byte_size
19130 && form == DW_FORM_data4
19131 && DW_UNSND (attr) >= 0xffffffff)
01c66ae6
JB
19132 {
19133 complaint
19134 (&symfile_complaints,
43bbcdc2
PH
19135 _("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
19136 hex_string (DW_UNSND (attr)));
01c66ae6
JB
19137 DW_UNSND (attr) = 0;
19138 }
28e94949 19139
c906108c
SS
19140 return info_ptr;
19141}
19142
a8329558
KW
19143/* Read an attribute described by an abbreviated attribute. */
19144
d521ce57 19145static const gdb_byte *
dee91e82
DE
19146read_attribute (const struct die_reader_specs *reader,
19147 struct attribute *attr, struct attr_abbrev *abbrev,
d521ce57 19148 const gdb_byte *info_ptr)
a8329558
KW
19149{
19150 attr->name = abbrev->name;
43988095
JK
19151 return read_attribute_value (reader, attr, abbrev->form,
19152 abbrev->implicit_const, info_ptr);
a8329558
KW
19153}
19154
0963b4bd 19155/* Read dwarf information from a buffer. */
c906108c
SS
19156
19157static unsigned int
a1855c1d 19158read_1_byte (bfd *abfd, const gdb_byte *buf)
c906108c 19159{
fe1b8b76 19160 return bfd_get_8 (abfd, buf);
c906108c
SS
19161}
19162
19163static int
a1855c1d 19164read_1_signed_byte (bfd *abfd, const gdb_byte *buf)
c906108c 19165{
fe1b8b76 19166 return bfd_get_signed_8 (abfd, buf);
c906108c
SS
19167}
19168
19169static unsigned int
a1855c1d 19170read_2_bytes (bfd *abfd, const gdb_byte *buf)
c906108c 19171{
fe1b8b76 19172 return bfd_get_16 (abfd, buf);
c906108c
SS
19173}
19174
21ae7a4d 19175static int
a1855c1d 19176read_2_signed_bytes (bfd *abfd, const gdb_byte *buf)
21ae7a4d
JK
19177{
19178 return bfd_get_signed_16 (abfd, buf);
19179}
19180
c906108c 19181static unsigned int
a1855c1d 19182read_4_bytes (bfd *abfd, const gdb_byte *buf)
c906108c 19183{
fe1b8b76 19184 return bfd_get_32 (abfd, buf);
c906108c
SS
19185}
19186
21ae7a4d 19187static int
a1855c1d 19188read_4_signed_bytes (bfd *abfd, const gdb_byte *buf)
21ae7a4d
JK
19189{
19190 return bfd_get_signed_32 (abfd, buf);
19191}
19192
93311388 19193static ULONGEST
a1855c1d 19194read_8_bytes (bfd *abfd, const gdb_byte *buf)
c906108c 19195{
fe1b8b76 19196 return bfd_get_64 (abfd, buf);
c906108c
SS
19197}
19198
19199static CORE_ADDR
d521ce57 19200read_address (bfd *abfd, const gdb_byte *buf, struct dwarf2_cu *cu,
891d2f0b 19201 unsigned int *bytes_read)
c906108c 19202{
e7c27a73 19203 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
19204 CORE_ADDR retval = 0;
19205
107d2387 19206 if (cu_header->signed_addr_p)
c906108c 19207 {
107d2387
AC
19208 switch (cu_header->addr_size)
19209 {
19210 case 2:
fe1b8b76 19211 retval = bfd_get_signed_16 (abfd, buf);
107d2387
AC
19212 break;
19213 case 4:
fe1b8b76 19214 retval = bfd_get_signed_32 (abfd, buf);
107d2387
AC
19215 break;
19216 case 8:
fe1b8b76 19217 retval = bfd_get_signed_64 (abfd, buf);
107d2387
AC
19218 break;
19219 default:
8e65ff28 19220 internal_error (__FILE__, __LINE__,
e2e0b3e5 19221 _("read_address: bad switch, signed [in module %s]"),
659b0389 19222 bfd_get_filename (abfd));
107d2387
AC
19223 }
19224 }
19225 else
19226 {
19227 switch (cu_header->addr_size)
19228 {
19229 case 2:
fe1b8b76 19230 retval = bfd_get_16 (abfd, buf);
107d2387
AC
19231 break;
19232 case 4:
fe1b8b76 19233 retval = bfd_get_32 (abfd, buf);
107d2387
AC
19234 break;
19235 case 8:
fe1b8b76 19236 retval = bfd_get_64 (abfd, buf);
107d2387
AC
19237 break;
19238 default:
8e65ff28 19239 internal_error (__FILE__, __LINE__,
a73c6dcd
MS
19240 _("read_address: bad switch, "
19241 "unsigned [in module %s]"),
659b0389 19242 bfd_get_filename (abfd));
107d2387 19243 }
c906108c 19244 }
64367e0a 19245
107d2387
AC
19246 *bytes_read = cu_header->addr_size;
19247 return retval;
c906108c
SS
19248}
19249
f7ef9339 19250/* Read the initial length from a section. The (draft) DWARF 3
613e1657
KB
19251 specification allows the initial length to take up either 4 bytes
19252 or 12 bytes. If the first 4 bytes are 0xffffffff, then the next 8
19253 bytes describe the length and all offsets will be 8 bytes in length
19254 instead of 4.
19255
f7ef9339
KB
19256 An older, non-standard 64-bit format is also handled by this
19257 function. The older format in question stores the initial length
19258 as an 8-byte quantity without an escape value. Lengths greater
19259 than 2^32 aren't very common which means that the initial 4 bytes
19260 is almost always zero. Since a length value of zero doesn't make
19261 sense for the 32-bit format, this initial zero can be considered to
19262 be an escape value which indicates the presence of the older 64-bit
19263 format. As written, the code can't detect (old format) lengths
917c78fc
MK
19264 greater than 4GB. If it becomes necessary to handle lengths
19265 somewhat larger than 4GB, we could allow other small values (such
19266 as the non-sensical values of 1, 2, and 3) to also be used as
19267 escape values indicating the presence of the old format.
f7ef9339 19268
917c78fc
MK
19269 The value returned via bytes_read should be used to increment the
19270 relevant pointer after calling read_initial_length().
c764a876 19271
613e1657
KB
19272 [ Note: read_initial_length() and read_offset() are based on the
19273 document entitled "DWARF Debugging Information Format", revision
f7ef9339 19274 3, draft 8, dated November 19, 2001. This document was obtained
613e1657
KB
19275 from:
19276
f7ef9339 19277 http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
6e70227d 19278
613e1657
KB
19279 This document is only a draft and is subject to change. (So beware.)
19280
f7ef9339 19281 Details regarding the older, non-standard 64-bit format were
917c78fc
MK
19282 determined empirically by examining 64-bit ELF files produced by
19283 the SGI toolchain on an IRIX 6.5 machine.
f7ef9339
KB
19284
19285 - Kevin, July 16, 2002
613e1657
KB
19286 ] */
19287
19288static LONGEST
d521ce57 19289read_initial_length (bfd *abfd, const gdb_byte *buf, unsigned int *bytes_read)
613e1657 19290{
fe1b8b76 19291 LONGEST length = bfd_get_32 (abfd, buf);
613e1657 19292
dd373385 19293 if (length == 0xffffffff)
613e1657 19294 {
fe1b8b76 19295 length = bfd_get_64 (abfd, buf + 4);
613e1657 19296 *bytes_read = 12;
613e1657 19297 }
dd373385 19298 else if (length == 0)
f7ef9339 19299 {
dd373385 19300 /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX. */
fe1b8b76 19301 length = bfd_get_64 (abfd, buf);
f7ef9339 19302 *bytes_read = 8;
f7ef9339 19303 }
613e1657
KB
19304 else
19305 {
19306 *bytes_read = 4;
613e1657
KB
19307 }
19308
c764a876
DE
19309 return length;
19310}
dd373385 19311
c764a876
DE
19312/* Cover function for read_initial_length.
19313 Returns the length of the object at BUF, and stores the size of the
19314 initial length in *BYTES_READ and stores the size that offsets will be in
19315 *OFFSET_SIZE.
19316 If the initial length size is not equivalent to that specified in
19317 CU_HEADER then issue a complaint.
19318 This is useful when reading non-comp-unit headers. */
dd373385 19319
c764a876 19320static LONGEST
d521ce57 19321read_checked_initial_length_and_offset (bfd *abfd, const gdb_byte *buf,
c764a876
DE
19322 const struct comp_unit_head *cu_header,
19323 unsigned int *bytes_read,
19324 unsigned int *offset_size)
19325{
19326 LONGEST length = read_initial_length (abfd, buf, bytes_read);
19327
19328 gdb_assert (cu_header->initial_length_size == 4
19329 || cu_header->initial_length_size == 8
19330 || cu_header->initial_length_size == 12);
19331
19332 if (cu_header->initial_length_size != *bytes_read)
19333 complaint (&symfile_complaints,
19334 _("intermixed 32-bit and 64-bit DWARF sections"));
dd373385 19335
c764a876 19336 *offset_size = (*bytes_read == 4) ? 4 : 8;
dd373385 19337 return length;
613e1657
KB
19338}
19339
19340/* Read an offset from the data stream. The size of the offset is
917c78fc 19341 given by cu_header->offset_size. */
613e1657
KB
19342
19343static LONGEST
d521ce57
TT
19344read_offset (bfd *abfd, const gdb_byte *buf,
19345 const struct comp_unit_head *cu_header,
891d2f0b 19346 unsigned int *bytes_read)
c764a876
DE
19347{
19348 LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
9a619af0 19349
c764a876
DE
19350 *bytes_read = cu_header->offset_size;
19351 return offset;
19352}
19353
19354/* Read an offset from the data stream. */
19355
19356static LONGEST
d521ce57 19357read_offset_1 (bfd *abfd, const gdb_byte *buf, unsigned int offset_size)
613e1657
KB
19358{
19359 LONGEST retval = 0;
19360
c764a876 19361 switch (offset_size)
613e1657
KB
19362 {
19363 case 4:
fe1b8b76 19364 retval = bfd_get_32 (abfd, buf);
613e1657
KB
19365 break;
19366 case 8:
fe1b8b76 19367 retval = bfd_get_64 (abfd, buf);
613e1657
KB
19368 break;
19369 default:
8e65ff28 19370 internal_error (__FILE__, __LINE__,
c764a876 19371 _("read_offset_1: bad switch [in module %s]"),
659b0389 19372 bfd_get_filename (abfd));
613e1657
KB
19373 }
19374
917c78fc 19375 return retval;
613e1657
KB
19376}
19377
d521ce57
TT
19378static const gdb_byte *
19379read_n_bytes (bfd *abfd, const gdb_byte *buf, unsigned int size)
c906108c
SS
19380{
19381 /* If the size of a host char is 8 bits, we can return a pointer
19382 to the buffer, otherwise we have to copy the data to a buffer
19383 allocated on the temporary obstack. */
4bdf3d34 19384 gdb_assert (HOST_CHAR_BIT == 8);
c906108c 19385 return buf;
c906108c
SS
19386}
19387
d521ce57
TT
19388static const char *
19389read_direct_string (bfd *abfd, const gdb_byte *buf,
19390 unsigned int *bytes_read_ptr)
c906108c
SS
19391{
19392 /* If the size of a host char is 8 bits, we can return a pointer
19393 to the string, otherwise we have to copy the string to a buffer
19394 allocated on the temporary obstack. */
4bdf3d34 19395 gdb_assert (HOST_CHAR_BIT == 8);
c906108c
SS
19396 if (*buf == '\0')
19397 {
19398 *bytes_read_ptr = 1;
19399 return NULL;
19400 }
d521ce57
TT
19401 *bytes_read_ptr = strlen ((const char *) buf) + 1;
19402 return (const char *) buf;
4bdf3d34
JJ
19403}
19404
43988095
JK
19405/* Return pointer to string at section SECT offset STR_OFFSET with error
19406 reporting strings FORM_NAME and SECT_NAME. */
19407
d521ce57 19408static const char *
ed2dc618
SM
19409read_indirect_string_at_offset_from (struct objfile *objfile,
19410 bfd *abfd, LONGEST str_offset,
43988095
JK
19411 struct dwarf2_section_info *sect,
19412 const char *form_name,
19413 const char *sect_name)
19414{
ed2dc618 19415 dwarf2_read_section (objfile, sect);
43988095
JK
19416 if (sect->buffer == NULL)
19417 error (_("%s used without %s section [in module %s]"),
19418 form_name, sect_name, bfd_get_filename (abfd));
19419 if (str_offset >= sect->size)
19420 error (_("%s pointing outside of %s section [in module %s]"),
19421 form_name, sect_name, bfd_get_filename (abfd));
4bdf3d34 19422 gdb_assert (HOST_CHAR_BIT == 8);
43988095 19423 if (sect->buffer[str_offset] == '\0')
4bdf3d34 19424 return NULL;
43988095
JK
19425 return (const char *) (sect->buffer + str_offset);
19426}
19427
19428/* Return pointer to string at .debug_str offset STR_OFFSET. */
19429
19430static const char *
ed2dc618
SM
19431read_indirect_string_at_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
19432 bfd *abfd, LONGEST str_offset)
43988095 19433{
ed2dc618
SM
19434 return read_indirect_string_at_offset_from (dwarf2_per_objfile->objfile,
19435 abfd, str_offset,
43988095
JK
19436 &dwarf2_per_objfile->str,
19437 "DW_FORM_strp", ".debug_str");
19438}
19439
19440/* Return pointer to string at .debug_line_str offset STR_OFFSET. */
19441
19442static const char *
ed2dc618
SM
19443read_indirect_line_string_at_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
19444 bfd *abfd, LONGEST str_offset)
43988095 19445{
ed2dc618
SM
19446 return read_indirect_string_at_offset_from (dwarf2_per_objfile->objfile,
19447 abfd, str_offset,
43988095
JK
19448 &dwarf2_per_objfile->line_str,
19449 "DW_FORM_line_strp",
19450 ".debug_line_str");
c906108c
SS
19451}
19452
36586728
TT
19453/* Read a string at offset STR_OFFSET in the .debug_str section from
19454 the .dwz file DWZ. Throw an error if the offset is too large. If
19455 the string consists of a single NUL byte, return NULL; otherwise
19456 return a pointer to the string. */
19457
d521ce57 19458static const char *
ed2dc618
SM
19459read_indirect_string_from_dwz (struct objfile *objfile, struct dwz_file *dwz,
19460 LONGEST str_offset)
36586728 19461{
ed2dc618 19462 dwarf2_read_section (objfile, &dwz->str);
36586728
TT
19463
19464 if (dwz->str.buffer == NULL)
19465 error (_("DW_FORM_GNU_strp_alt used without .debug_str "
19466 "section [in module %s]"),
19467 bfd_get_filename (dwz->dwz_bfd));
19468 if (str_offset >= dwz->str.size)
19469 error (_("DW_FORM_GNU_strp_alt pointing outside of "
19470 ".debug_str section [in module %s]"),
19471 bfd_get_filename (dwz->dwz_bfd));
19472 gdb_assert (HOST_CHAR_BIT == 8);
19473 if (dwz->str.buffer[str_offset] == '\0')
19474 return NULL;
d521ce57 19475 return (const char *) (dwz->str.buffer + str_offset);
36586728
TT
19476}
19477
43988095
JK
19478/* Return pointer to string at .debug_str offset as read from BUF.
19479 BUF is assumed to be in a compilation unit described by CU_HEADER.
19480 Return *BYTES_READ_PTR count of bytes read from BUF. */
19481
d521ce57 19482static const char *
ed2dc618
SM
19483read_indirect_string (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *abfd,
19484 const gdb_byte *buf,
cf2c3c16
TT
19485 const struct comp_unit_head *cu_header,
19486 unsigned int *bytes_read_ptr)
19487{
19488 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
19489
ed2dc618 19490 return read_indirect_string_at_offset (dwarf2_per_objfile, abfd, str_offset);
cf2c3c16
TT
19491}
19492
43988095
JK
19493/* Return pointer to string at .debug_line_str offset as read from BUF.
19494 BUF is assumed to be in a compilation unit described by CU_HEADER.
19495 Return *BYTES_READ_PTR count of bytes read from BUF. */
19496
19497static const char *
ed2dc618
SM
19498read_indirect_line_string (struct dwarf2_per_objfile *dwarf2_per_objfile,
19499 bfd *abfd, const gdb_byte *buf,
43988095
JK
19500 const struct comp_unit_head *cu_header,
19501 unsigned int *bytes_read_ptr)
19502{
19503 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
19504
ed2dc618
SM
19505 return read_indirect_line_string_at_offset (dwarf2_per_objfile, abfd,
19506 str_offset);
43988095
JK
19507}
19508
19509ULONGEST
d521ce57 19510read_unsigned_leb128 (bfd *abfd, const gdb_byte *buf,
43988095 19511 unsigned int *bytes_read_ptr)
c906108c 19512{
12df843f 19513 ULONGEST result;
ce5d95e1 19514 unsigned int num_read;
870f88f7 19515 int shift;
c906108c
SS
19516 unsigned char byte;
19517
19518 result = 0;
19519 shift = 0;
19520 num_read = 0;
c906108c
SS
19521 while (1)
19522 {
fe1b8b76 19523 byte = bfd_get_8 (abfd, buf);
c906108c
SS
19524 buf++;
19525 num_read++;
12df843f 19526 result |= ((ULONGEST) (byte & 127) << shift);
c906108c
SS
19527 if ((byte & 128) == 0)
19528 {
19529 break;
19530 }
19531 shift += 7;
19532 }
19533 *bytes_read_ptr = num_read;
19534 return result;
19535}
19536
12df843f 19537static LONGEST
d521ce57
TT
19538read_signed_leb128 (bfd *abfd, const gdb_byte *buf,
19539 unsigned int *bytes_read_ptr)
c906108c 19540{
12df843f 19541 LONGEST result;
870f88f7 19542 int shift, num_read;
c906108c
SS
19543 unsigned char byte;
19544
19545 result = 0;
19546 shift = 0;
c906108c 19547 num_read = 0;
c906108c
SS
19548 while (1)
19549 {
fe1b8b76 19550 byte = bfd_get_8 (abfd, buf);
c906108c
SS
19551 buf++;
19552 num_read++;
12df843f 19553 result |= ((LONGEST) (byte & 127) << shift);
c906108c
SS
19554 shift += 7;
19555 if ((byte & 128) == 0)
19556 {
19557 break;
19558 }
19559 }
77e0b926 19560 if ((shift < 8 * sizeof (result)) && (byte & 0x40))
12df843f 19561 result |= -(((LONGEST) 1) << shift);
c906108c
SS
19562 *bytes_read_ptr = num_read;
19563 return result;
19564}
19565
3019eac3
DE
19566/* Given index ADDR_INDEX in .debug_addr, fetch the value.
19567 ADDR_BASE is the DW_AT_GNU_addr_base attribute or zero.
19568 ADDR_SIZE is the size of addresses from the CU header. */
19569
19570static CORE_ADDR
ed2dc618
SM
19571read_addr_index_1 (struct dwarf2_per_objfile *dwarf2_per_objfile,
19572 unsigned int addr_index, ULONGEST addr_base, int addr_size)
3019eac3
DE
19573{
19574 struct objfile *objfile = dwarf2_per_objfile->objfile;
19575 bfd *abfd = objfile->obfd;
19576 const gdb_byte *info_ptr;
19577
19578 dwarf2_read_section (objfile, &dwarf2_per_objfile->addr);
19579 if (dwarf2_per_objfile->addr.buffer == NULL)
19580 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
4262abfb 19581 objfile_name (objfile));
3019eac3
DE
19582 if (addr_base + addr_index * addr_size >= dwarf2_per_objfile->addr.size)
19583 error (_("DW_FORM_addr_index pointing outside of "
19584 ".debug_addr section [in module %s]"),
4262abfb 19585 objfile_name (objfile));
3019eac3
DE
19586 info_ptr = (dwarf2_per_objfile->addr.buffer
19587 + addr_base + addr_index * addr_size);
19588 if (addr_size == 4)
19589 return bfd_get_32 (abfd, info_ptr);
19590 else
19591 return bfd_get_64 (abfd, info_ptr);
19592}
19593
19594/* Given index ADDR_INDEX in .debug_addr, fetch the value. */
19595
19596static CORE_ADDR
19597read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
19598{
518817b3
SM
19599 return read_addr_index_1 (cu->per_cu->dwarf2_per_objfile, addr_index,
19600 cu->addr_base, cu->header.addr_size);
3019eac3
DE
19601}
19602
19603/* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
19604
19605static CORE_ADDR
d521ce57 19606read_addr_index_from_leb128 (struct dwarf2_cu *cu, const gdb_byte *info_ptr,
3019eac3
DE
19607 unsigned int *bytes_read)
19608{
518817b3 19609 bfd *abfd = cu->per_cu->dwarf2_per_objfile->objfile->obfd;
3019eac3
DE
19610 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
19611
19612 return read_addr_index (cu, addr_index);
19613}
19614
19615/* Data structure to pass results from dwarf2_read_addr_index_reader
19616 back to dwarf2_read_addr_index. */
19617
19618struct dwarf2_read_addr_index_data
19619{
19620 ULONGEST addr_base;
19621 int addr_size;
19622};
19623
19624/* die_reader_func for dwarf2_read_addr_index. */
19625
19626static void
19627dwarf2_read_addr_index_reader (const struct die_reader_specs *reader,
d521ce57 19628 const gdb_byte *info_ptr,
3019eac3
DE
19629 struct die_info *comp_unit_die,
19630 int has_children,
19631 void *data)
19632{
19633 struct dwarf2_cu *cu = reader->cu;
19634 struct dwarf2_read_addr_index_data *aidata =
19635 (struct dwarf2_read_addr_index_data *) data;
19636
19637 aidata->addr_base = cu->addr_base;
19638 aidata->addr_size = cu->header.addr_size;
19639}
19640
19641/* Given an index in .debug_addr, fetch the value.
19642 NOTE: This can be called during dwarf expression evaluation,
19643 long after the debug information has been read, and thus per_cu->cu
19644 may no longer exist. */
19645
19646CORE_ADDR
19647dwarf2_read_addr_index (struct dwarf2_per_cu_data *per_cu,
19648 unsigned int addr_index)
19649{
ed2dc618 19650 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
3019eac3
DE
19651 struct dwarf2_cu *cu = per_cu->cu;
19652 ULONGEST addr_base;
19653 int addr_size;
19654
3019eac3
DE
19655 /* We need addr_base and addr_size.
19656 If we don't have PER_CU->cu, we have to get it.
19657 Nasty, but the alternative is storing the needed info in PER_CU,
19658 which at this point doesn't seem justified: it's not clear how frequently
19659 it would get used and it would increase the size of every PER_CU.
19660 Entry points like dwarf2_per_cu_addr_size do a similar thing
19661 so we're not in uncharted territory here.
19662 Alas we need to be a bit more complicated as addr_base is contained
19663 in the DIE.
19664
19665 We don't need to read the entire CU(/TU).
19666 We just need the header and top level die.
a1b64ce1 19667
3019eac3 19668 IWBN to use the aging mechanism to let us lazily later discard the CU.
a1b64ce1 19669 For now we skip this optimization. */
3019eac3
DE
19670
19671 if (cu != NULL)
19672 {
19673 addr_base = cu->addr_base;
19674 addr_size = cu->header.addr_size;
19675 }
19676 else
19677 {
19678 struct dwarf2_read_addr_index_data aidata;
19679
a1b64ce1
DE
19680 /* Note: We can't use init_cutu_and_read_dies_simple here,
19681 we need addr_base. */
58f0c718 19682 init_cutu_and_read_dies (per_cu, NULL, 0, 0, false,
a1b64ce1 19683 dwarf2_read_addr_index_reader, &aidata);
3019eac3
DE
19684 addr_base = aidata.addr_base;
19685 addr_size = aidata.addr_size;
19686 }
19687
ed2dc618
SM
19688 return read_addr_index_1 (dwarf2_per_objfile, addr_index, addr_base,
19689 addr_size);
3019eac3
DE
19690}
19691
57d63ce2
DE
19692/* Given a DW_FORM_GNU_str_index, fetch the string.
19693 This is only used by the Fission support. */
3019eac3 19694
d521ce57 19695static const char *
342587c4 19696read_str_index (const struct die_reader_specs *reader, ULONGEST str_index)
3019eac3 19697{
ed2dc618 19698 struct dwarf2_cu *cu = reader->cu;
518817b3
SM
19699 struct dwarf2_per_objfile *dwarf2_per_objfile
19700 = cu->per_cu->dwarf2_per_objfile;
3019eac3 19701 struct objfile *objfile = dwarf2_per_objfile->objfile;
c5164cbc 19702 const char *objf_name = objfile_name (objfile);
3019eac3 19703 bfd *abfd = objfile->obfd;
73869dc2
DE
19704 struct dwarf2_section_info *str_section = &reader->dwo_file->sections.str;
19705 struct dwarf2_section_info *str_offsets_section =
19706 &reader->dwo_file->sections.str_offsets;
d521ce57 19707 const gdb_byte *info_ptr;
3019eac3 19708 ULONGEST str_offset;
57d63ce2 19709 static const char form_name[] = "DW_FORM_GNU_str_index";
3019eac3 19710
73869dc2
DE
19711 dwarf2_read_section (objfile, str_section);
19712 dwarf2_read_section (objfile, str_offsets_section);
19713 if (str_section->buffer == NULL)
57d63ce2 19714 error (_("%s used without .debug_str.dwo section"
9d8780f0
SM
19715 " in CU at offset %s [in module %s]"),
19716 form_name, sect_offset_str (cu->header.sect_off), objf_name);
73869dc2 19717 if (str_offsets_section->buffer == NULL)
57d63ce2 19718 error (_("%s used without .debug_str_offsets.dwo section"
9d8780f0
SM
19719 " in CU at offset %s [in module %s]"),
19720 form_name, sect_offset_str (cu->header.sect_off), objf_name);
73869dc2 19721 if (str_index * cu->header.offset_size >= str_offsets_section->size)
57d63ce2 19722 error (_("%s pointing outside of .debug_str_offsets.dwo"
9d8780f0
SM
19723 " section in CU at offset %s [in module %s]"),
19724 form_name, sect_offset_str (cu->header.sect_off), objf_name);
73869dc2 19725 info_ptr = (str_offsets_section->buffer
3019eac3
DE
19726 + str_index * cu->header.offset_size);
19727 if (cu->header.offset_size == 4)
19728 str_offset = bfd_get_32 (abfd, info_ptr);
19729 else
19730 str_offset = bfd_get_64 (abfd, info_ptr);
73869dc2 19731 if (str_offset >= str_section->size)
57d63ce2 19732 error (_("Offset from %s pointing outside of"
9d8780f0
SM
19733 " .debug_str.dwo section in CU at offset %s [in module %s]"),
19734 form_name, sect_offset_str (cu->header.sect_off), objf_name);
73869dc2 19735 return (const char *) (str_section->buffer + str_offset);
3019eac3
DE
19736}
19737
3019eac3
DE
19738/* Return the length of an LEB128 number in BUF. */
19739
19740static int
19741leb128_size (const gdb_byte *buf)
19742{
19743 const gdb_byte *begin = buf;
19744 gdb_byte byte;
19745
19746 while (1)
19747 {
19748 byte = *buf++;
19749 if ((byte & 128) == 0)
19750 return buf - begin;
19751 }
19752}
19753
c906108c 19754static void
e142c38c 19755set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
c906108c
SS
19756{
19757 switch (lang)
19758 {
19759 case DW_LANG_C89:
76bee0cc 19760 case DW_LANG_C99:
0cfd832f 19761 case DW_LANG_C11:
c906108c 19762 case DW_LANG_C:
d1be3247 19763 case DW_LANG_UPC:
e142c38c 19764 cu->language = language_c;
c906108c 19765 break;
9c37b5ae 19766 case DW_LANG_Java:
c906108c 19767 case DW_LANG_C_plus_plus:
0cfd832f
MW
19768 case DW_LANG_C_plus_plus_11:
19769 case DW_LANG_C_plus_plus_14:
e142c38c 19770 cu->language = language_cplus;
c906108c 19771 break;
6aecb9c2
JB
19772 case DW_LANG_D:
19773 cu->language = language_d;
19774 break;
c906108c
SS
19775 case DW_LANG_Fortran77:
19776 case DW_LANG_Fortran90:
b21b22e0 19777 case DW_LANG_Fortran95:
f7de9aab
MW
19778 case DW_LANG_Fortran03:
19779 case DW_LANG_Fortran08:
e142c38c 19780 cu->language = language_fortran;
c906108c 19781 break;
a766d390
DE
19782 case DW_LANG_Go:
19783 cu->language = language_go;
19784 break;
c906108c 19785 case DW_LANG_Mips_Assembler:
e142c38c 19786 cu->language = language_asm;
c906108c
SS
19787 break;
19788 case DW_LANG_Ada83:
8aaf0b47 19789 case DW_LANG_Ada95:
bc5f45f8
JB
19790 cu->language = language_ada;
19791 break;
72019c9c
GM
19792 case DW_LANG_Modula2:
19793 cu->language = language_m2;
19794 break;
fe8e67fd
PM
19795 case DW_LANG_Pascal83:
19796 cu->language = language_pascal;
19797 break;
22566fbd
DJ
19798 case DW_LANG_ObjC:
19799 cu->language = language_objc;
19800 break;
c44af4eb
TT
19801 case DW_LANG_Rust:
19802 case DW_LANG_Rust_old:
19803 cu->language = language_rust;
19804 break;
c906108c
SS
19805 case DW_LANG_Cobol74:
19806 case DW_LANG_Cobol85:
c906108c 19807 default:
e142c38c 19808 cu->language = language_minimal;
c906108c
SS
19809 break;
19810 }
e142c38c 19811 cu->language_defn = language_def (cu->language);
c906108c
SS
19812}
19813
19814/* Return the named attribute or NULL if not there. */
19815
19816static struct attribute *
e142c38c 19817dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
c906108c 19818{
a48e046c 19819 for (;;)
c906108c 19820 {
a48e046c
TT
19821 unsigned int i;
19822 struct attribute *spec = NULL;
19823
19824 for (i = 0; i < die->num_attrs; ++i)
19825 {
19826 if (die->attrs[i].name == name)
19827 return &die->attrs[i];
19828 if (die->attrs[i].name == DW_AT_specification
19829 || die->attrs[i].name == DW_AT_abstract_origin)
19830 spec = &die->attrs[i];
19831 }
19832
19833 if (!spec)
19834 break;
c906108c 19835
f2f0e013 19836 die = follow_die_ref (die, spec, &cu);
f2f0e013 19837 }
c5aa993b 19838
c906108c
SS
19839 return NULL;
19840}
19841
348e048f
DE
19842/* Return the named attribute or NULL if not there,
19843 but do not follow DW_AT_specification, etc.
19844 This is for use in contexts where we're reading .debug_types dies.
19845 Following DW_AT_specification, DW_AT_abstract_origin will take us
19846 back up the chain, and we want to go down. */
19847
19848static struct attribute *
45e58e77 19849dwarf2_attr_no_follow (struct die_info *die, unsigned int name)
348e048f
DE
19850{
19851 unsigned int i;
19852
19853 for (i = 0; i < die->num_attrs; ++i)
19854 if (die->attrs[i].name == name)
19855 return &die->attrs[i];
19856
19857 return NULL;
19858}
19859
7d45c7c3
KB
19860/* Return the string associated with a string-typed attribute, or NULL if it
19861 is either not found or is of an incorrect type. */
19862
19863static const char *
19864dwarf2_string_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
19865{
19866 struct attribute *attr;
19867 const char *str = NULL;
19868
19869 attr = dwarf2_attr (die, name, cu);
19870
19871 if (attr != NULL)
19872 {
43988095 19873 if (attr->form == DW_FORM_strp || attr->form == DW_FORM_line_strp
b3340438
L
19874 || attr->form == DW_FORM_string
19875 || attr->form == DW_FORM_GNU_str_index
16eb6b2d 19876 || attr->form == DW_FORM_GNU_strp_alt)
7d45c7c3
KB
19877 str = DW_STRING (attr);
19878 else
19879 complaint (&symfile_complaints,
19880 _("string type expected for attribute %s for "
9d8780f0
SM
19881 "DIE at %s in module %s"),
19882 dwarf_attr_name (name), sect_offset_str (die->sect_off),
518817b3 19883 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
7d45c7c3
KB
19884 }
19885
19886 return str;
19887}
19888
05cf31d1
JB
19889/* Return non-zero iff the attribute NAME is defined for the given DIE,
19890 and holds a non-zero value. This function should only be used for
2dc7f7b3 19891 DW_FORM_flag or DW_FORM_flag_present attributes. */
05cf31d1
JB
19892
19893static int
19894dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
19895{
19896 struct attribute *attr = dwarf2_attr (die, name, cu);
19897
19898 return (attr && DW_UNSND (attr));
19899}
19900
3ca72b44 19901static int
e142c38c 19902die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
3ca72b44 19903{
05cf31d1
JB
19904 /* A DIE is a declaration if it has a DW_AT_declaration attribute
19905 which value is non-zero. However, we have to be careful with
19906 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
19907 (via dwarf2_flag_true_p) follows this attribute. So we may
19908 end up accidently finding a declaration attribute that belongs
19909 to a different DIE referenced by the specification attribute,
19910 even though the given DIE does not have a declaration attribute. */
19911 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
19912 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
3ca72b44
AC
19913}
19914
63d06c5c 19915/* Return the die giving the specification for DIE, if there is
f2f0e013 19916 one. *SPEC_CU is the CU containing DIE on input, and the CU
edb3359d
DJ
19917 containing the return value on output. If there is no
19918 specification, but there is an abstract origin, that is
19919 returned. */
63d06c5c
DC
19920
19921static struct die_info *
f2f0e013 19922die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
63d06c5c 19923{
f2f0e013
DJ
19924 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
19925 *spec_cu);
63d06c5c 19926
edb3359d
DJ
19927 if (spec_attr == NULL)
19928 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
19929
63d06c5c
DC
19930 if (spec_attr == NULL)
19931 return NULL;
19932 else
f2f0e013 19933 return follow_die_ref (die, spec_attr, spec_cu);
63d06c5c 19934}
c906108c 19935
527f3840
JK
19936/* Stub for free_line_header to match void * callback types. */
19937
19938static void
19939free_line_header_voidp (void *arg)
19940{
9a3c8263 19941 struct line_header *lh = (struct line_header *) arg;
527f3840 19942
fff8551c 19943 delete lh;
527f3840
JK
19944}
19945
fff8551c
PA
19946void
19947line_header::add_include_dir (const char *include_dir)
c906108c 19948{
27e0867f 19949 if (dwarf_line_debug >= 2)
fff8551c
PA
19950 fprintf_unfiltered (gdb_stdlog, "Adding dir %zu: %s\n",
19951 include_dirs.size () + 1, include_dir);
27e0867f 19952
fff8551c 19953 include_dirs.push_back (include_dir);
debd256d 19954}
6e70227d 19955
fff8551c
PA
19956void
19957line_header::add_file_name (const char *name,
ecfb656c 19958 dir_index d_index,
fff8551c
PA
19959 unsigned int mod_time,
19960 unsigned int length)
debd256d 19961{
27e0867f
DE
19962 if (dwarf_line_debug >= 2)
19963 fprintf_unfiltered (gdb_stdlog, "Adding file %u: %s\n",
fff8551c 19964 (unsigned) file_names.size () + 1, name);
27e0867f 19965
ecfb656c 19966 file_names.emplace_back (name, d_index, mod_time, length);
debd256d 19967}
6e70227d 19968
83769d0b 19969/* A convenience function to find the proper .debug_line section for a CU. */
36586728
TT
19970
19971static struct dwarf2_section_info *
19972get_debug_line_section (struct dwarf2_cu *cu)
19973{
19974 struct dwarf2_section_info *section;
518817b3
SM
19975 struct dwarf2_per_objfile *dwarf2_per_objfile
19976 = cu->per_cu->dwarf2_per_objfile;
36586728
TT
19977
19978 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
19979 DWO file. */
19980 if (cu->dwo_unit && cu->per_cu->is_debug_types)
19981 section = &cu->dwo_unit->dwo_file->sections.line;
19982 else if (cu->per_cu->is_dwz)
19983 {
ed2dc618 19984 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
36586728
TT
19985
19986 section = &dwz->line;
19987 }
19988 else
19989 section = &dwarf2_per_objfile->line;
19990
19991 return section;
19992}
19993
43988095
JK
19994/* Read directory or file name entry format, starting with byte of
19995 format count entries, ULEB128 pairs of entry formats, ULEB128 of
19996 entries count and the entries themselves in the described entry
19997 format. */
19998
19999static void
ed2dc618
SM
20000read_formatted_entries (struct dwarf2_per_objfile *dwarf2_per_objfile,
20001 bfd *abfd, const gdb_byte **bufp,
43988095
JK
20002 struct line_header *lh,
20003 const struct comp_unit_head *cu_header,
20004 void (*callback) (struct line_header *lh,
20005 const char *name,
ecfb656c 20006 dir_index d_index,
43988095
JK
20007 unsigned int mod_time,
20008 unsigned int length))
20009{
20010 gdb_byte format_count, formati;
20011 ULONGEST data_count, datai;
20012 const gdb_byte *buf = *bufp;
20013 const gdb_byte *format_header_data;
43988095
JK
20014 unsigned int bytes_read;
20015
20016 format_count = read_1_byte (abfd, buf);
20017 buf += 1;
20018 format_header_data = buf;
20019 for (formati = 0; formati < format_count; formati++)
20020 {
20021 read_unsigned_leb128 (abfd, buf, &bytes_read);
20022 buf += bytes_read;
20023 read_unsigned_leb128 (abfd, buf, &bytes_read);
20024 buf += bytes_read;
20025 }
20026
20027 data_count = read_unsigned_leb128 (abfd, buf, &bytes_read);
20028 buf += bytes_read;
20029 for (datai = 0; datai < data_count; datai++)
20030 {
20031 const gdb_byte *format = format_header_data;
20032 struct file_entry fe;
20033
43988095
JK
20034 for (formati = 0; formati < format_count; formati++)
20035 {
ecfb656c 20036 ULONGEST content_type = read_unsigned_leb128 (abfd, format, &bytes_read);
43988095 20037 format += bytes_read;
43988095 20038
ecfb656c 20039 ULONGEST form = read_unsigned_leb128 (abfd, format, &bytes_read);
43988095 20040 format += bytes_read;
ecfb656c
PA
20041
20042 gdb::optional<const char *> string;
20043 gdb::optional<unsigned int> uint;
20044
43988095
JK
20045 switch (form)
20046 {
20047 case DW_FORM_string:
ecfb656c 20048 string.emplace (read_direct_string (abfd, buf, &bytes_read));
43988095
JK
20049 buf += bytes_read;
20050 break;
20051
20052 case DW_FORM_line_strp:
ed2dc618
SM
20053 string.emplace (read_indirect_line_string (dwarf2_per_objfile,
20054 abfd, buf,
ecfb656c
PA
20055 cu_header,
20056 &bytes_read));
43988095
JK
20057 buf += bytes_read;
20058 break;
20059
20060 case DW_FORM_data1:
ecfb656c 20061 uint.emplace (read_1_byte (abfd, buf));
43988095
JK
20062 buf += 1;
20063 break;
20064
20065 case DW_FORM_data2:
ecfb656c 20066 uint.emplace (read_2_bytes (abfd, buf));
43988095
JK
20067 buf += 2;
20068 break;
20069
20070 case DW_FORM_data4:
ecfb656c 20071 uint.emplace (read_4_bytes (abfd, buf));
43988095
JK
20072 buf += 4;
20073 break;
20074
20075 case DW_FORM_data8:
ecfb656c 20076 uint.emplace (read_8_bytes (abfd, buf));
43988095
JK
20077 buf += 8;
20078 break;
20079
20080 case DW_FORM_udata:
ecfb656c 20081 uint.emplace (read_unsigned_leb128 (abfd, buf, &bytes_read));
43988095
JK
20082 buf += bytes_read;
20083 break;
20084
20085 case DW_FORM_block:
20086 /* It is valid only for DW_LNCT_timestamp which is ignored by
20087 current GDB. */
20088 break;
20089 }
ecfb656c
PA
20090
20091 switch (content_type)
20092 {
20093 case DW_LNCT_path:
20094 if (string.has_value ())
20095 fe.name = *string;
20096 break;
20097 case DW_LNCT_directory_index:
20098 if (uint.has_value ())
20099 fe.d_index = (dir_index) *uint;
20100 break;
20101 case DW_LNCT_timestamp:
20102 if (uint.has_value ())
20103 fe.mod_time = *uint;
20104 break;
20105 case DW_LNCT_size:
20106 if (uint.has_value ())
20107 fe.length = *uint;
20108 break;
20109 case DW_LNCT_MD5:
20110 break;
20111 default:
20112 complaint (&symfile_complaints,
20113 _("Unknown format content type %s"),
20114 pulongest (content_type));
20115 }
43988095
JK
20116 }
20117
ecfb656c 20118 callback (lh, fe.name, fe.d_index, fe.mod_time, fe.length);
43988095
JK
20119 }
20120
20121 *bufp = buf;
20122}
20123
debd256d 20124/* Read the statement program header starting at OFFSET in
3019eac3 20125 .debug_line, or .debug_line.dwo. Return a pointer
6502dd73 20126 to a struct line_header, allocated using xmalloc.
cd366ee8
DE
20127 Returns NULL if there is a problem reading the header, e.g., if it
20128 has a version we don't understand.
debd256d
JB
20129
20130 NOTE: the strings in the include directory and file name tables of
3019eac3
DE
20131 the returned object point into the dwarf line section buffer,
20132 and must not be freed. */
ae2de4f8 20133
fff8551c 20134static line_header_up
9c541725 20135dwarf_decode_line_header (sect_offset sect_off, struct dwarf2_cu *cu)
debd256d 20136{
d521ce57 20137 const gdb_byte *line_ptr;
c764a876 20138 unsigned int bytes_read, offset_size;
debd256d 20139 int i;
d521ce57 20140 const char *cur_dir, *cur_file;
3019eac3
DE
20141 struct dwarf2_section_info *section;
20142 bfd *abfd;
518817b3
SM
20143 struct dwarf2_per_objfile *dwarf2_per_objfile
20144 = cu->per_cu->dwarf2_per_objfile;
3019eac3 20145
36586728 20146 section = get_debug_line_section (cu);
3019eac3
DE
20147 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
20148 if (section->buffer == NULL)
debd256d 20149 {
3019eac3
DE
20150 if (cu->dwo_unit && cu->per_cu->is_debug_types)
20151 complaint (&symfile_complaints, _("missing .debug_line.dwo section"));
20152 else
20153 complaint (&symfile_complaints, _("missing .debug_line section"));
debd256d
JB
20154 return 0;
20155 }
20156
fceca515
DE
20157 /* We can't do this until we know the section is non-empty.
20158 Only then do we know we have such a section. */
a32a8923 20159 abfd = get_section_bfd_owner (section);
fceca515 20160
a738430d
MK
20161 /* Make sure that at least there's room for the total_length field.
20162 That could be 12 bytes long, but we're just going to fudge that. */
9c541725 20163 if (to_underlying (sect_off) + 4 >= section->size)
debd256d 20164 {
4d3c2250 20165 dwarf2_statement_list_fits_in_line_number_section_complaint ();
debd256d
JB
20166 return 0;
20167 }
20168
fff8551c 20169 line_header_up lh (new line_header ());
debd256d 20170
9c541725 20171 lh->sect_off = sect_off;
527f3840
JK
20172 lh->offset_in_dwz = cu->per_cu->is_dwz;
20173
9c541725 20174 line_ptr = section->buffer + to_underlying (sect_off);
debd256d 20175
a738430d 20176 /* Read in the header. */
6e70227d 20177 lh->total_length =
c764a876
DE
20178 read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
20179 &bytes_read, &offset_size);
debd256d 20180 line_ptr += bytes_read;
3019eac3 20181 if (line_ptr + lh->total_length > (section->buffer + section->size))
debd256d 20182 {
4d3c2250 20183 dwarf2_statement_list_fits_in_line_number_section_complaint ();
debd256d
JB
20184 return 0;
20185 }
20186 lh->statement_program_end = line_ptr + lh->total_length;
20187 lh->version = read_2_bytes (abfd, line_ptr);
20188 line_ptr += 2;
43988095 20189 if (lh->version > 5)
cd366ee8
DE
20190 {
20191 /* This is a version we don't understand. The format could have
20192 changed in ways we don't handle properly so just punt. */
20193 complaint (&symfile_complaints,
20194 _("unsupported version in .debug_line section"));
20195 return NULL;
20196 }
43988095
JK
20197 if (lh->version >= 5)
20198 {
20199 gdb_byte segment_selector_size;
20200
20201 /* Skip address size. */
20202 read_1_byte (abfd, line_ptr);
20203 line_ptr += 1;
20204
20205 segment_selector_size = read_1_byte (abfd, line_ptr);
20206 line_ptr += 1;
20207 if (segment_selector_size != 0)
20208 {
20209 complaint (&symfile_complaints,
20210 _("unsupported segment selector size %u "
20211 "in .debug_line section"),
20212 segment_selector_size);
20213 return NULL;
20214 }
20215 }
c764a876
DE
20216 lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
20217 line_ptr += offset_size;
debd256d
JB
20218 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
20219 line_ptr += 1;
2dc7f7b3
TT
20220 if (lh->version >= 4)
20221 {
20222 lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
20223 line_ptr += 1;
20224 }
20225 else
20226 lh->maximum_ops_per_instruction = 1;
20227
20228 if (lh->maximum_ops_per_instruction == 0)
20229 {
20230 lh->maximum_ops_per_instruction = 1;
20231 complaint (&symfile_complaints,
3e43a32a
MS
20232 _("invalid maximum_ops_per_instruction "
20233 "in `.debug_line' section"));
2dc7f7b3
TT
20234 }
20235
debd256d
JB
20236 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
20237 line_ptr += 1;
20238 lh->line_base = read_1_signed_byte (abfd, line_ptr);
20239 line_ptr += 1;
20240 lh->line_range = read_1_byte (abfd, line_ptr);
20241 line_ptr += 1;
20242 lh->opcode_base = read_1_byte (abfd, line_ptr);
20243 line_ptr += 1;
fff8551c 20244 lh->standard_opcode_lengths.reset (new unsigned char[lh->opcode_base]);
debd256d
JB
20245
20246 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
20247 for (i = 1; i < lh->opcode_base; ++i)
20248 {
20249 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
20250 line_ptr += 1;
20251 }
20252
43988095 20253 if (lh->version >= 5)
debd256d 20254 {
43988095 20255 /* Read directory table. */
ed2dc618
SM
20256 read_formatted_entries (dwarf2_per_objfile, abfd, &line_ptr, lh.get (),
20257 &cu->header,
fff8551c 20258 [] (struct line_header *lh, const char *name,
ecfb656c 20259 dir_index d_index, unsigned int mod_time,
fff8551c
PA
20260 unsigned int length)
20261 {
20262 lh->add_include_dir (name);
20263 });
debd256d 20264
43988095 20265 /* Read file name table. */
ed2dc618
SM
20266 read_formatted_entries (dwarf2_per_objfile, abfd, &line_ptr, lh.get (),
20267 &cu->header,
fff8551c 20268 [] (struct line_header *lh, const char *name,
ecfb656c 20269 dir_index d_index, unsigned int mod_time,
fff8551c
PA
20270 unsigned int length)
20271 {
ecfb656c 20272 lh->add_file_name (name, d_index, mod_time, length);
fff8551c 20273 });
43988095
JK
20274 }
20275 else
debd256d 20276 {
43988095
JK
20277 /* Read directory table. */
20278 while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
20279 {
20280 line_ptr += bytes_read;
fff8551c 20281 lh->add_include_dir (cur_dir);
43988095 20282 }
debd256d
JB
20283 line_ptr += bytes_read;
20284
43988095
JK
20285 /* Read file name table. */
20286 while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
20287 {
ecfb656c
PA
20288 unsigned int mod_time, length;
20289 dir_index d_index;
43988095
JK
20290
20291 line_ptr += bytes_read;
ecfb656c 20292 d_index = (dir_index) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
43988095
JK
20293 line_ptr += bytes_read;
20294 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20295 line_ptr += bytes_read;
20296 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20297 line_ptr += bytes_read;
20298
ecfb656c 20299 lh->add_file_name (cur_file, d_index, mod_time, length);
43988095
JK
20300 }
20301 line_ptr += bytes_read;
debd256d 20302 }
6e70227d 20303 lh->statement_program_start = line_ptr;
debd256d 20304
3019eac3 20305 if (line_ptr > (section->buffer + section->size))
4d3c2250 20306 complaint (&symfile_complaints,
3e43a32a
MS
20307 _("line number info header doesn't "
20308 "fit in `.debug_line' section"));
debd256d 20309
debd256d
JB
20310 return lh;
20311}
c906108c 20312
c6da4cef
DE
20313/* Subroutine of dwarf_decode_lines to simplify it.
20314 Return the file name of the psymtab for included file FILE_INDEX
20315 in line header LH of PST.
20316 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
c89b44cd
TT
20317 If space for the result is malloc'd, *NAME_HOLDER will be set.
20318 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
c6da4cef 20319
d521ce57 20320static const char *
c6da4cef
DE
20321psymtab_include_file_name (const struct line_header *lh, int file_index,
20322 const struct partial_symtab *pst,
c89b44cd
TT
20323 const char *comp_dir,
20324 gdb::unique_xmalloc_ptr<char> *name_holder)
c6da4cef 20325{
8c43009f 20326 const file_entry &fe = lh->file_names[file_index];
d521ce57
TT
20327 const char *include_name = fe.name;
20328 const char *include_name_to_compare = include_name;
72b9f47f 20329 const char *pst_filename;
c6da4cef
DE
20330 int file_is_pst;
20331
8c43009f 20332 const char *dir_name = fe.include_dir (lh);
c6da4cef 20333
c89b44cd 20334 gdb::unique_xmalloc_ptr<char> hold_compare;
c6da4cef
DE
20335 if (!IS_ABSOLUTE_PATH (include_name)
20336 && (dir_name != NULL || comp_dir != NULL))
20337 {
20338 /* Avoid creating a duplicate psymtab for PST.
20339 We do this by comparing INCLUDE_NAME and PST_FILENAME.
20340 Before we do the comparison, however, we need to account
20341 for DIR_NAME and COMP_DIR.
20342 First prepend dir_name (if non-NULL). If we still don't
20343 have an absolute path prepend comp_dir (if non-NULL).
20344 However, the directory we record in the include-file's
20345 psymtab does not contain COMP_DIR (to match the
20346 corresponding symtab(s)).
20347
20348 Example:
20349
20350 bash$ cd /tmp
20351 bash$ gcc -g ./hello.c
20352 include_name = "hello.c"
20353 dir_name = "."
20354 DW_AT_comp_dir = comp_dir = "/tmp"
5f52445b
YQ
20355 DW_AT_name = "./hello.c"
20356
20357 */
c6da4cef
DE
20358
20359 if (dir_name != NULL)
20360 {
c89b44cd
TT
20361 name_holder->reset (concat (dir_name, SLASH_STRING,
20362 include_name, (char *) NULL));
20363 include_name = name_holder->get ();
c6da4cef 20364 include_name_to_compare = include_name;
c6da4cef
DE
20365 }
20366 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
20367 {
c89b44cd
TT
20368 hold_compare.reset (concat (comp_dir, SLASH_STRING,
20369 include_name, (char *) NULL));
20370 include_name_to_compare = hold_compare.get ();
c6da4cef
DE
20371 }
20372 }
20373
20374 pst_filename = pst->filename;
c89b44cd 20375 gdb::unique_xmalloc_ptr<char> copied_name;
c6da4cef
DE
20376 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
20377 {
c89b44cd
TT
20378 copied_name.reset (concat (pst->dirname, SLASH_STRING,
20379 pst_filename, (char *) NULL));
20380 pst_filename = copied_name.get ();
c6da4cef
DE
20381 }
20382
1e3fad37 20383 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
c6da4cef 20384
c6da4cef
DE
20385 if (file_is_pst)
20386 return NULL;
20387 return include_name;
20388}
20389
d9b3de22
DE
20390/* State machine to track the state of the line number program. */
20391
6f77053d 20392class lnp_state_machine
d9b3de22 20393{
6f77053d
PA
20394public:
20395 /* Initialize a machine state for the start of a line number
20396 program. */
20397 lnp_state_machine (gdbarch *arch, line_header *lh, bool record_lines_p);
20398
8c43009f
PA
20399 file_entry *current_file ()
20400 {
20401 /* lh->file_names is 0-based, but the file name numbers in the
20402 statement program are 1-based. */
6f77053d
PA
20403 return m_line_header->file_name_at (m_file);
20404 }
20405
20406 /* Record the line in the state machine. END_SEQUENCE is true if
20407 we're processing the end of a sequence. */
20408 void record_line (bool end_sequence);
20409
20410 /* Check address and if invalid nop-out the rest of the lines in this
20411 sequence. */
20412 void check_line_address (struct dwarf2_cu *cu,
20413 const gdb_byte *line_ptr,
20414 CORE_ADDR lowpc, CORE_ADDR address);
20415
20416 void handle_set_discriminator (unsigned int discriminator)
20417 {
20418 m_discriminator = discriminator;
20419 m_line_has_non_zero_discriminator |= discriminator != 0;
20420 }
20421
20422 /* Handle DW_LNE_set_address. */
20423 void handle_set_address (CORE_ADDR baseaddr, CORE_ADDR address)
20424 {
20425 m_op_index = 0;
20426 address += baseaddr;
20427 m_address = gdbarch_adjust_dwarf2_line (m_gdbarch, address, false);
20428 }
20429
20430 /* Handle DW_LNS_advance_pc. */
20431 void handle_advance_pc (CORE_ADDR adjust);
20432
20433 /* Handle a special opcode. */
20434 void handle_special_opcode (unsigned char op_code);
20435
20436 /* Handle DW_LNS_advance_line. */
20437 void handle_advance_line (int line_delta)
20438 {
20439 advance_line (line_delta);
20440 }
20441
20442 /* Handle DW_LNS_set_file. */
20443 void handle_set_file (file_name_index file);
20444
20445 /* Handle DW_LNS_negate_stmt. */
20446 void handle_negate_stmt ()
20447 {
20448 m_is_stmt = !m_is_stmt;
20449 }
20450
20451 /* Handle DW_LNS_const_add_pc. */
20452 void handle_const_add_pc ();
20453
20454 /* Handle DW_LNS_fixed_advance_pc. */
20455 void handle_fixed_advance_pc (CORE_ADDR addr_adj)
20456 {
20457 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20458 m_op_index = 0;
20459 }
20460
20461 /* Handle DW_LNS_copy. */
20462 void handle_copy ()
20463 {
20464 record_line (false);
20465 m_discriminator = 0;
20466 }
20467
20468 /* Handle DW_LNE_end_sequence. */
20469 void handle_end_sequence ()
20470 {
20471 m_record_line_callback = ::record_line;
20472 }
20473
20474private:
20475 /* Advance the line by LINE_DELTA. */
20476 void advance_line (int line_delta)
20477 {
20478 m_line += line_delta;
20479
20480 if (line_delta != 0)
20481 m_line_has_non_zero_discriminator = m_discriminator != 0;
8c43009f
PA
20482 }
20483
6f77053d
PA
20484 gdbarch *m_gdbarch;
20485
20486 /* True if we're recording lines.
20487 Otherwise we're building partial symtabs and are just interested in
20488 finding include files mentioned by the line number program. */
20489 bool m_record_lines_p;
20490
8c43009f 20491 /* The line number header. */
6f77053d 20492 line_header *m_line_header;
8c43009f 20493
6f77053d
PA
20494 /* These are part of the standard DWARF line number state machine,
20495 and initialized according to the DWARF spec. */
d9b3de22 20496
6f77053d 20497 unsigned char m_op_index = 0;
8c43009f 20498 /* The line table index (1-based) of the current file. */
6f77053d
PA
20499 file_name_index m_file = (file_name_index) 1;
20500 unsigned int m_line = 1;
20501
20502 /* These are initialized in the constructor. */
20503
20504 CORE_ADDR m_address;
20505 bool m_is_stmt;
20506 unsigned int m_discriminator;
d9b3de22
DE
20507
20508 /* Additional bits of state we need to track. */
20509
20510 /* The last file that we called dwarf2_start_subfile for.
20511 This is only used for TLLs. */
6f77053d 20512 unsigned int m_last_file = 0;
d9b3de22 20513 /* The last file a line number was recorded for. */
6f77053d 20514 struct subfile *m_last_subfile = NULL;
d9b3de22
DE
20515
20516 /* The function to call to record a line. */
6f77053d 20517 record_line_ftype *m_record_line_callback = NULL;
d9b3de22
DE
20518
20519 /* The last line number that was recorded, used to coalesce
20520 consecutive entries for the same line. This can happen, for
20521 example, when discriminators are present. PR 17276. */
6f77053d
PA
20522 unsigned int m_last_line = 0;
20523 bool m_line_has_non_zero_discriminator = false;
8c43009f 20524};
d9b3de22 20525
6f77053d
PA
20526void
20527lnp_state_machine::handle_advance_pc (CORE_ADDR adjust)
20528{
20529 CORE_ADDR addr_adj = (((m_op_index + adjust)
20530 / m_line_header->maximum_ops_per_instruction)
20531 * m_line_header->minimum_instruction_length);
20532 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20533 m_op_index = ((m_op_index + adjust)
20534 % m_line_header->maximum_ops_per_instruction);
20535}
d9b3de22 20536
6f77053d
PA
20537void
20538lnp_state_machine::handle_special_opcode (unsigned char op_code)
d9b3de22 20539{
6f77053d
PA
20540 unsigned char adj_opcode = op_code - m_line_header->opcode_base;
20541 CORE_ADDR addr_adj = (((m_op_index
20542 + (adj_opcode / m_line_header->line_range))
20543 / m_line_header->maximum_ops_per_instruction)
20544 * m_line_header->minimum_instruction_length);
20545 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20546 m_op_index = ((m_op_index + (adj_opcode / m_line_header->line_range))
20547 % m_line_header->maximum_ops_per_instruction);
d9b3de22 20548
6f77053d
PA
20549 int line_delta = (m_line_header->line_base
20550 + (adj_opcode % m_line_header->line_range));
20551 advance_line (line_delta);
20552 record_line (false);
20553 m_discriminator = 0;
20554}
d9b3de22 20555
6f77053d
PA
20556void
20557lnp_state_machine::handle_set_file (file_name_index file)
20558{
20559 m_file = file;
20560
20561 const file_entry *fe = current_file ();
20562 if (fe == NULL)
20563 dwarf2_debug_line_missing_file_complaint ();
20564 else if (m_record_lines_p)
20565 {
20566 const char *dir = fe->include_dir (m_line_header);
20567
20568 m_last_subfile = current_subfile;
20569 m_line_has_non_zero_discriminator = m_discriminator != 0;
20570 dwarf2_start_subfile (fe->name, dir);
20571 }
20572}
20573
20574void
20575lnp_state_machine::handle_const_add_pc ()
20576{
20577 CORE_ADDR adjust
20578 = (255 - m_line_header->opcode_base) / m_line_header->line_range;
20579
20580 CORE_ADDR addr_adj
20581 = (((m_op_index + adjust)
20582 / m_line_header->maximum_ops_per_instruction)
20583 * m_line_header->minimum_instruction_length);
20584
20585 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20586 m_op_index = ((m_op_index + adjust)
20587 % m_line_header->maximum_ops_per_instruction);
20588}
d9b3de22 20589
c91513d8
PP
20590/* Ignore this record_line request. */
20591
20592static void
20593noop_record_line (struct subfile *subfile, int line, CORE_ADDR pc)
20594{
20595 return;
20596}
20597
a05a36a5
DE
20598/* Return non-zero if we should add LINE to the line number table.
20599 LINE is the line to add, LAST_LINE is the last line that was added,
20600 LAST_SUBFILE is the subfile for LAST_LINE.
20601 LINE_HAS_NON_ZERO_DISCRIMINATOR is non-zero if LINE has ever
20602 had a non-zero discriminator.
20603
20604 We have to be careful in the presence of discriminators.
20605 E.g., for this line:
20606
20607 for (i = 0; i < 100000; i++);
20608
20609 clang can emit four line number entries for that one line,
20610 each with a different discriminator.
20611 See gdb.dwarf2/dw2-single-line-discriminators.exp for an example.
20612
20613 However, we want gdb to coalesce all four entries into one.
20614 Otherwise the user could stepi into the middle of the line and
20615 gdb would get confused about whether the pc really was in the
20616 middle of the line.
20617
20618 Things are further complicated by the fact that two consecutive
20619 line number entries for the same line is a heuristic used by gcc
20620 to denote the end of the prologue. So we can't just discard duplicate
20621 entries, we have to be selective about it. The heuristic we use is
20622 that we only collapse consecutive entries for the same line if at least
20623 one of those entries has a non-zero discriminator. PR 17276.
20624
20625 Note: Addresses in the line number state machine can never go backwards
20626 within one sequence, thus this coalescing is ok. */
20627
20628static int
20629dwarf_record_line_p (unsigned int line, unsigned int last_line,
20630 int line_has_non_zero_discriminator,
20631 struct subfile *last_subfile)
20632{
20633 if (current_subfile != last_subfile)
20634 return 1;
20635 if (line != last_line)
20636 return 1;
20637 /* Same line for the same file that we've seen already.
20638 As a last check, for pr 17276, only record the line if the line
20639 has never had a non-zero discriminator. */
20640 if (!line_has_non_zero_discriminator)
20641 return 1;
20642 return 0;
20643}
20644
252a6764
DE
20645/* Use P_RECORD_LINE to record line number LINE beginning at address ADDRESS
20646 in the line table of subfile SUBFILE. */
20647
20648static void
d9b3de22
DE
20649dwarf_record_line_1 (struct gdbarch *gdbarch, struct subfile *subfile,
20650 unsigned int line, CORE_ADDR address,
20651 record_line_ftype p_record_line)
252a6764
DE
20652{
20653 CORE_ADDR addr = gdbarch_addr_bits_remove (gdbarch, address);
20654
27e0867f
DE
20655 if (dwarf_line_debug)
20656 {
20657 fprintf_unfiltered (gdb_stdlog,
20658 "Recording line %u, file %s, address %s\n",
20659 line, lbasename (subfile->name),
20660 paddress (gdbarch, address));
20661 }
20662
d5962de5 20663 (*p_record_line) (subfile, line, addr);
252a6764
DE
20664}
20665
20666/* Subroutine of dwarf_decode_lines_1 to simplify it.
20667 Mark the end of a set of line number records.
d9b3de22 20668 The arguments are the same as for dwarf_record_line_1.
252a6764
DE
20669 If SUBFILE is NULL the request is ignored. */
20670
20671static void
20672dwarf_finish_line (struct gdbarch *gdbarch, struct subfile *subfile,
20673 CORE_ADDR address, record_line_ftype p_record_line)
20674{
27e0867f
DE
20675 if (subfile == NULL)
20676 return;
20677
20678 if (dwarf_line_debug)
20679 {
20680 fprintf_unfiltered (gdb_stdlog,
20681 "Finishing current line, file %s, address %s\n",
20682 lbasename (subfile->name),
20683 paddress (gdbarch, address));
20684 }
20685
d9b3de22
DE
20686 dwarf_record_line_1 (gdbarch, subfile, 0, address, p_record_line);
20687}
20688
6f77053d
PA
20689void
20690lnp_state_machine::record_line (bool end_sequence)
d9b3de22 20691{
d9b3de22
DE
20692 if (dwarf_line_debug)
20693 {
20694 fprintf_unfiltered (gdb_stdlog,
20695 "Processing actual line %u: file %u,"
20696 " address %s, is_stmt %u, discrim %u\n",
6f77053d
PA
20697 m_line, to_underlying (m_file),
20698 paddress (m_gdbarch, m_address),
20699 m_is_stmt, m_discriminator);
d9b3de22
DE
20700 }
20701
6f77053d 20702 file_entry *fe = current_file ();
8c43009f
PA
20703
20704 if (fe == NULL)
d9b3de22
DE
20705 dwarf2_debug_line_missing_file_complaint ();
20706 /* For now we ignore lines not starting on an instruction boundary.
20707 But not when processing end_sequence for compatibility with the
20708 previous version of the code. */
6f77053d 20709 else if (m_op_index == 0 || end_sequence)
d9b3de22 20710 {
8c43009f 20711 fe->included_p = 1;
6f77053d 20712 if (m_record_lines_p && m_is_stmt)
d9b3de22 20713 {
6f77053d 20714 if (m_last_subfile != current_subfile || end_sequence)
d9b3de22 20715 {
6f77053d
PA
20716 dwarf_finish_line (m_gdbarch, m_last_subfile,
20717 m_address, m_record_line_callback);
d9b3de22
DE
20718 }
20719
20720 if (!end_sequence)
20721 {
6f77053d
PA
20722 if (dwarf_record_line_p (m_line, m_last_line,
20723 m_line_has_non_zero_discriminator,
20724 m_last_subfile))
d9b3de22 20725 {
6f77053d
PA
20726 dwarf_record_line_1 (m_gdbarch, current_subfile,
20727 m_line, m_address,
20728 m_record_line_callback);
d9b3de22 20729 }
6f77053d
PA
20730 m_last_subfile = current_subfile;
20731 m_last_line = m_line;
d9b3de22
DE
20732 }
20733 }
20734 }
20735}
20736
6f77053d
PA
20737lnp_state_machine::lnp_state_machine (gdbarch *arch, line_header *lh,
20738 bool record_lines_p)
d9b3de22 20739{
6f77053d
PA
20740 m_gdbarch = arch;
20741 m_record_lines_p = record_lines_p;
20742 m_line_header = lh;
d9b3de22 20743
6f77053d 20744 m_record_line_callback = ::record_line;
d9b3de22 20745
d9b3de22
DE
20746 /* Call `gdbarch_adjust_dwarf2_line' on the initial 0 address as if there
20747 was a line entry for it so that the backend has a chance to adjust it
20748 and also record it in case it needs it. This is currently used by MIPS
20749 code, cf. `mips_adjust_dwarf2_line'. */
6f77053d
PA
20750 m_address = gdbarch_adjust_dwarf2_line (arch, 0, 0);
20751 m_is_stmt = lh->default_is_stmt;
20752 m_discriminator = 0;
252a6764
DE
20753}
20754
6f77053d
PA
20755void
20756lnp_state_machine::check_line_address (struct dwarf2_cu *cu,
20757 const gdb_byte *line_ptr,
20758 CORE_ADDR lowpc, CORE_ADDR address)
924c2928
DE
20759{
20760 /* If address < lowpc then it's not a usable value, it's outside the
20761 pc range of the CU. However, we restrict the test to only address
20762 values of zero to preserve GDB's previous behaviour which is to
20763 handle the specific case of a function being GC'd by the linker. */
20764
20765 if (address == 0 && address < lowpc)
20766 {
20767 /* This line table is for a function which has been
20768 GCd by the linker. Ignore it. PR gdb/12528 */
20769
518817b3 20770 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
924c2928
DE
20771 long line_offset = line_ptr - get_debug_line_section (cu)->buffer;
20772
20773 complaint (&symfile_complaints,
20774 _(".debug_line address at offset 0x%lx is 0 [in module %s]"),
20775 line_offset, objfile_name (objfile));
6f77053d
PA
20776 m_record_line_callback = noop_record_line;
20777 /* Note: record_line_callback is left as noop_record_line until
20778 we see DW_LNE_end_sequence. */
924c2928
DE
20779 }
20780}
20781
f3f5162e 20782/* Subroutine of dwarf_decode_lines to simplify it.
d9b3de22
DE
20783 Process the line number information in LH.
20784 If DECODE_FOR_PST_P is non-zero, all we do is process the line number
20785 program in order to set included_p for every referenced header. */
debd256d 20786
c906108c 20787static void
43f3e411
DE
20788dwarf_decode_lines_1 (struct line_header *lh, struct dwarf2_cu *cu,
20789 const int decode_for_pst_p, CORE_ADDR lowpc)
c906108c 20790{
d521ce57
TT
20791 const gdb_byte *line_ptr, *extended_end;
20792 const gdb_byte *line_end;
a8c50c1f 20793 unsigned int bytes_read, extended_len;
699ca60a 20794 unsigned char op_code, extended_op;
e142c38c 20795 CORE_ADDR baseaddr;
518817b3 20796 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
f3f5162e 20797 bfd *abfd = objfile->obfd;
fbf65064 20798 struct gdbarch *gdbarch = get_objfile_arch (objfile);
6f77053d
PA
20799 /* True if we're recording line info (as opposed to building partial
20800 symtabs and just interested in finding include files mentioned by
20801 the line number program). */
20802 bool record_lines_p = !decode_for_pst_p;
e142c38c
DJ
20803
20804 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 20805
debd256d
JB
20806 line_ptr = lh->statement_program_start;
20807 line_end = lh->statement_program_end;
c906108c
SS
20808
20809 /* Read the statement sequences until there's nothing left. */
20810 while (line_ptr < line_end)
20811 {
6f77053d
PA
20812 /* The DWARF line number program state machine. Reset the state
20813 machine at the start of each sequence. */
20814 lnp_state_machine state_machine (gdbarch, lh, record_lines_p);
20815 bool end_sequence = false;
d9b3de22 20816
8c43009f 20817 if (record_lines_p)
c906108c 20818 {
8c43009f
PA
20819 /* Start a subfile for the current file of the state
20820 machine. */
20821 const file_entry *fe = state_machine.current_file ();
20822
20823 if (fe != NULL)
20824 dwarf2_start_subfile (fe->name, fe->include_dir (lh));
c906108c
SS
20825 }
20826
a738430d 20827 /* Decode the table. */
d9b3de22 20828 while (line_ptr < line_end && !end_sequence)
c906108c
SS
20829 {
20830 op_code = read_1_byte (abfd, line_ptr);
20831 line_ptr += 1;
9aa1fe7e 20832
debd256d 20833 if (op_code >= lh->opcode_base)
6e70227d 20834 {
8e07a239 20835 /* Special opcode. */
6f77053d 20836 state_machine.handle_special_opcode (op_code);
9aa1fe7e
GK
20837 }
20838 else switch (op_code)
c906108c
SS
20839 {
20840 case DW_LNS_extended_op:
3e43a32a
MS
20841 extended_len = read_unsigned_leb128 (abfd, line_ptr,
20842 &bytes_read);
473b7be6 20843 line_ptr += bytes_read;
a8c50c1f 20844 extended_end = line_ptr + extended_len;
c906108c
SS
20845 extended_op = read_1_byte (abfd, line_ptr);
20846 line_ptr += 1;
20847 switch (extended_op)
20848 {
20849 case DW_LNE_end_sequence:
6f77053d
PA
20850 state_machine.handle_end_sequence ();
20851 end_sequence = true;
c906108c
SS
20852 break;
20853 case DW_LNE_set_address:
d9b3de22
DE
20854 {
20855 CORE_ADDR address
20856 = read_address (abfd, line_ptr, cu, &bytes_read);
d9b3de22 20857 line_ptr += bytes_read;
6f77053d
PA
20858
20859 state_machine.check_line_address (cu, line_ptr,
20860 lowpc, address);
20861 state_machine.handle_set_address (baseaddr, address);
d9b3de22 20862 }
c906108c
SS
20863 break;
20864 case DW_LNE_define_file:
debd256d 20865 {
d521ce57 20866 const char *cur_file;
ecfb656c
PA
20867 unsigned int mod_time, length;
20868 dir_index dindex;
6e70227d 20869
3e43a32a
MS
20870 cur_file = read_direct_string (abfd, line_ptr,
20871 &bytes_read);
debd256d 20872 line_ptr += bytes_read;
ecfb656c 20873 dindex = (dir_index)
debd256d
JB
20874 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20875 line_ptr += bytes_read;
20876 mod_time =
20877 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20878 line_ptr += bytes_read;
20879 length =
20880 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20881 line_ptr += bytes_read;
ecfb656c 20882 lh->add_file_name (cur_file, dindex, mod_time, length);
debd256d 20883 }
c906108c 20884 break;
d0c6ba3d 20885 case DW_LNE_set_discriminator:
6f77053d
PA
20886 {
20887 /* The discriminator is not interesting to the
20888 debugger; just ignore it. We still need to
20889 check its value though:
20890 if there are consecutive entries for the same
20891 (non-prologue) line we want to coalesce them.
20892 PR 17276. */
20893 unsigned int discr
20894 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20895 line_ptr += bytes_read;
20896
20897 state_machine.handle_set_discriminator (discr);
20898 }
d0c6ba3d 20899 break;
c906108c 20900 default:
4d3c2250 20901 complaint (&symfile_complaints,
e2e0b3e5 20902 _("mangled .debug_line section"));
debd256d 20903 return;
c906108c 20904 }
a8c50c1f
DJ
20905 /* Make sure that we parsed the extended op correctly. If e.g.
20906 we expected a different address size than the producer used,
20907 we may have read the wrong number of bytes. */
20908 if (line_ptr != extended_end)
20909 {
20910 complaint (&symfile_complaints,
20911 _("mangled .debug_line section"));
20912 return;
20913 }
c906108c
SS
20914 break;
20915 case DW_LNS_copy:
6f77053d 20916 state_machine.handle_copy ();
c906108c
SS
20917 break;
20918 case DW_LNS_advance_pc:
2dc7f7b3
TT
20919 {
20920 CORE_ADDR adjust
20921 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
2dc7f7b3 20922 line_ptr += bytes_read;
6f77053d
PA
20923
20924 state_machine.handle_advance_pc (adjust);
2dc7f7b3 20925 }
c906108c
SS
20926 break;
20927 case DW_LNS_advance_line:
a05a36a5
DE
20928 {
20929 int line_delta
20930 = read_signed_leb128 (abfd, line_ptr, &bytes_read);
a05a36a5 20931 line_ptr += bytes_read;
6f77053d
PA
20932
20933 state_machine.handle_advance_line (line_delta);
a05a36a5 20934 }
c906108c
SS
20935 break;
20936 case DW_LNS_set_file:
d9b3de22 20937 {
6f77053d 20938 file_name_index file
ecfb656c
PA
20939 = (file_name_index) read_unsigned_leb128 (abfd, line_ptr,
20940 &bytes_read);
d9b3de22 20941 line_ptr += bytes_read;
8c43009f 20942
6f77053d 20943 state_machine.handle_set_file (file);
d9b3de22 20944 }
c906108c
SS
20945 break;
20946 case DW_LNS_set_column:
0ad93d4f 20947 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
c906108c
SS
20948 line_ptr += bytes_read;
20949 break;
20950 case DW_LNS_negate_stmt:
6f77053d 20951 state_machine.handle_negate_stmt ();
c906108c
SS
20952 break;
20953 case DW_LNS_set_basic_block:
c906108c 20954 break;
c2c6d25f
JM
20955 /* Add to the address register of the state machine the
20956 address increment value corresponding to special opcode
a738430d
MK
20957 255. I.e., this value is scaled by the minimum
20958 instruction length since special opcode 255 would have
b021a221 20959 scaled the increment. */
c906108c 20960 case DW_LNS_const_add_pc:
6f77053d 20961 state_machine.handle_const_add_pc ();
c906108c
SS
20962 break;
20963 case DW_LNS_fixed_advance_pc:
3e29f34a 20964 {
6f77053d 20965 CORE_ADDR addr_adj = read_2_bytes (abfd, line_ptr);
3e29f34a 20966 line_ptr += 2;
6f77053d
PA
20967
20968 state_machine.handle_fixed_advance_pc (addr_adj);
3e29f34a 20969 }
c906108c 20970 break;
9aa1fe7e 20971 default:
a738430d
MK
20972 {
20973 /* Unknown standard opcode, ignore it. */
9aa1fe7e 20974 int i;
a738430d 20975
debd256d 20976 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
9aa1fe7e
GK
20977 {
20978 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20979 line_ptr += bytes_read;
20980 }
20981 }
c906108c
SS
20982 }
20983 }
d9b3de22
DE
20984
20985 if (!end_sequence)
20986 dwarf2_debug_line_missing_end_sequence_complaint ();
20987
20988 /* We got a DW_LNE_end_sequence (or we ran off the end of the buffer,
20989 in which case we still finish recording the last line). */
6f77053d 20990 state_machine.record_line (true);
c906108c 20991 }
f3f5162e
DE
20992}
20993
20994/* Decode the Line Number Program (LNP) for the given line_header
20995 structure and CU. The actual information extracted and the type
20996 of structures created from the LNP depends on the value of PST.
20997
20998 1. If PST is NULL, then this procedure uses the data from the program
20999 to create all necessary symbol tables, and their linetables.
21000
21001 2. If PST is not NULL, this procedure reads the program to determine
21002 the list of files included by the unit represented by PST, and
21003 builds all the associated partial symbol tables.
21004
21005 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
21006 It is used for relative paths in the line table.
21007 NOTE: When processing partial symtabs (pst != NULL),
21008 comp_dir == pst->dirname.
21009
21010 NOTE: It is important that psymtabs have the same file name (via strcmp)
21011 as the corresponding symtab. Since COMP_DIR is not used in the name of the
21012 symtab we don't use it in the name of the psymtabs we create.
21013 E.g. expand_line_sal requires this when finding psymtabs to expand.
c3b7b696
YQ
21014 A good testcase for this is mb-inline.exp.
21015
527f3840
JK
21016 LOWPC is the lowest address in CU (or 0 if not known).
21017
21018 Boolean DECODE_MAPPING specifies we need to fully decode .debug_line
21019 for its PC<->lines mapping information. Otherwise only the filename
21020 table is read in. */
f3f5162e
DE
21021
21022static void
21023dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
c3b7b696 21024 struct dwarf2_cu *cu, struct partial_symtab *pst,
527f3840 21025 CORE_ADDR lowpc, int decode_mapping)
f3f5162e 21026{
518817b3 21027 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
f3f5162e 21028 const int decode_for_pst_p = (pst != NULL);
f3f5162e 21029
527f3840
JK
21030 if (decode_mapping)
21031 dwarf_decode_lines_1 (lh, cu, decode_for_pst_p, lowpc);
aaa75496
JB
21032
21033 if (decode_for_pst_p)
21034 {
21035 int file_index;
21036
21037 /* Now that we're done scanning the Line Header Program, we can
21038 create the psymtab of each included file. */
fff8551c 21039 for (file_index = 0; file_index < lh->file_names.size (); file_index++)
aaa75496
JB
21040 if (lh->file_names[file_index].included_p == 1)
21041 {
c89b44cd 21042 gdb::unique_xmalloc_ptr<char> name_holder;
d521ce57 21043 const char *include_name =
c89b44cd
TT
21044 psymtab_include_file_name (lh, file_index, pst, comp_dir,
21045 &name_holder);
c6da4cef 21046 if (include_name != NULL)
aaa75496
JB
21047 dwarf2_create_include_psymtab (include_name, pst, objfile);
21048 }
21049 }
cb1df416
DJ
21050 else
21051 {
21052 /* Make sure a symtab is created for every file, even files
21053 which contain only variables (i.e. no code with associated
21054 line numbers). */
43f3e411 21055 struct compunit_symtab *cust = buildsym_compunit_symtab ();
cb1df416 21056 int i;
cb1df416 21057
fff8551c 21058 for (i = 0; i < lh->file_names.size (); i++)
cb1df416 21059 {
8c43009f 21060 file_entry &fe = lh->file_names[i];
9a619af0 21061
8c43009f 21062 dwarf2_start_subfile (fe.name, fe.include_dir (lh));
cb1df416 21063
cb1df416 21064 if (current_subfile->symtab == NULL)
43f3e411
DE
21065 {
21066 current_subfile->symtab
21067 = allocate_symtab (cust, current_subfile->name);
21068 }
8c43009f 21069 fe.symtab = current_subfile->symtab;
cb1df416
DJ
21070 }
21071 }
c906108c
SS
21072}
21073
21074/* Start a subfile for DWARF. FILENAME is the name of the file and
21075 DIRNAME the name of the source directory which contains FILENAME
4d663531 21076 or NULL if not known.
c906108c
SS
21077 This routine tries to keep line numbers from identical absolute and
21078 relative file names in a common subfile.
21079
21080 Using the `list' example from the GDB testsuite, which resides in
21081 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
21082 of /srcdir/list0.c yields the following debugging information for list0.c:
21083
c5aa993b 21084 DW_AT_name: /srcdir/list0.c
4d663531 21085 DW_AT_comp_dir: /compdir
357e46e7 21086 files.files[0].name: list0.h
c5aa993b 21087 files.files[0].dir: /srcdir
357e46e7 21088 files.files[1].name: list0.c
c5aa993b 21089 files.files[1].dir: /srcdir
c906108c
SS
21090
21091 The line number information for list0.c has to end up in a single
4f1520fb
FR
21092 subfile, so that `break /srcdir/list0.c:1' works as expected.
21093 start_subfile will ensure that this happens provided that we pass the
21094 concatenation of files.files[1].dir and files.files[1].name as the
21095 subfile's name. */
c906108c
SS
21096
21097static void
4d663531 21098dwarf2_start_subfile (const char *filename, const char *dirname)
c906108c 21099{
d521ce57 21100 char *copy = NULL;
4f1520fb 21101
4d663531 21102 /* In order not to lose the line information directory,
4f1520fb
FR
21103 we concatenate it to the filename when it makes sense.
21104 Note that the Dwarf3 standard says (speaking of filenames in line
21105 information): ``The directory index is ignored for file names
21106 that represent full path names''. Thus ignoring dirname in the
21107 `else' branch below isn't an issue. */
c906108c 21108
d5166ae1 21109 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
d521ce57
TT
21110 {
21111 copy = concat (dirname, SLASH_STRING, filename, (char *)NULL);
21112 filename = copy;
21113 }
c906108c 21114
4d663531 21115 start_subfile (filename);
4f1520fb 21116
d521ce57
TT
21117 if (copy != NULL)
21118 xfree (copy);
c906108c
SS
21119}
21120
f4dc4d17
DE
21121/* Start a symtab for DWARF.
21122 NAME, COMP_DIR, LOW_PC are passed to start_symtab. */
21123
43f3e411 21124static struct compunit_symtab *
f4dc4d17 21125dwarf2_start_symtab (struct dwarf2_cu *cu,
15d034d0 21126 const char *name, const char *comp_dir, CORE_ADDR low_pc)
f4dc4d17 21127{
43f3e411 21128 struct compunit_symtab *cust
518817b3
SM
21129 = start_symtab (cu->per_cu->dwarf2_per_objfile->objfile, name, comp_dir,
21130 low_pc, cu->language);
43f3e411 21131
f4dc4d17
DE
21132 record_debugformat ("DWARF 2");
21133 record_producer (cu->producer);
21134
21135 /* We assume that we're processing GCC output. */
21136 processing_gcc_compilation = 2;
21137
4d4ec4e5 21138 cu->processing_has_namespace_info = 0;
43f3e411
DE
21139
21140 return cust;
f4dc4d17
DE
21141}
21142
4c2df51b
DJ
21143static void
21144var_decode_location (struct attribute *attr, struct symbol *sym,
e7c27a73 21145 struct dwarf2_cu *cu)
4c2df51b 21146{
518817b3 21147 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
e7c27a73
DJ
21148 struct comp_unit_head *cu_header = &cu->header;
21149
4c2df51b
DJ
21150 /* NOTE drow/2003-01-30: There used to be a comment and some special
21151 code here to turn a symbol with DW_AT_external and a
21152 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
21153 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
21154 with some versions of binutils) where shared libraries could have
21155 relocations against symbols in their debug information - the
21156 minimal symbol would have the right address, but the debug info
21157 would not. It's no longer necessary, because we will explicitly
21158 apply relocations when we read in the debug information now. */
21159
21160 /* A DW_AT_location attribute with no contents indicates that a
21161 variable has been optimized away. */
21162 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
21163 {
f1e6e072 21164 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
4c2df51b
DJ
21165 return;
21166 }
21167
21168 /* Handle one degenerate form of location expression specially, to
21169 preserve GDB's previous behavior when section offsets are
3019eac3
DE
21170 specified. If this is just a DW_OP_addr or DW_OP_GNU_addr_index
21171 then mark this symbol as LOC_STATIC. */
4c2df51b
DJ
21172
21173 if (attr_form_is_block (attr)
3019eac3
DE
21174 && ((DW_BLOCK (attr)->data[0] == DW_OP_addr
21175 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size)
21176 || (DW_BLOCK (attr)->data[0] == DW_OP_GNU_addr_index
21177 && (DW_BLOCK (attr)->size
21178 == 1 + leb128_size (&DW_BLOCK (attr)->data[1])))))
4c2df51b 21179 {
891d2f0b 21180 unsigned int dummy;
4c2df51b 21181
3019eac3
DE
21182 if (DW_BLOCK (attr)->data[0] == DW_OP_addr)
21183 SYMBOL_VALUE_ADDRESS (sym) =
21184 read_address (objfile->obfd, DW_BLOCK (attr)->data + 1, cu, &dummy);
21185 else
21186 SYMBOL_VALUE_ADDRESS (sym) =
21187 read_addr_index_from_leb128 (cu, DW_BLOCK (attr)->data + 1, &dummy);
f1e6e072 21188 SYMBOL_ACLASS_INDEX (sym) = LOC_STATIC;
4c2df51b
DJ
21189 fixup_symbol_section (sym, objfile);
21190 SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (objfile->section_offsets,
21191 SYMBOL_SECTION (sym));
4c2df51b
DJ
21192 return;
21193 }
21194
21195 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
21196 expression evaluator, and use LOC_COMPUTED only when necessary
21197 (i.e. when the value of a register or memory location is
21198 referenced, or a thread-local block, etc.). Then again, it might
21199 not be worthwhile. I'm assuming that it isn't unless performance
21200 or memory numbers show me otherwise. */
21201
f1e6e072 21202 dwarf2_symbol_mark_computed (attr, sym, cu, 0);
8be455d7 21203
f1e6e072 21204 if (SYMBOL_COMPUTED_OPS (sym)->location_has_loclist)
8be455d7 21205 cu->has_loclist = 1;
4c2df51b
DJ
21206}
21207
c906108c
SS
21208/* Given a pointer to a DWARF information entry, figure out if we need
21209 to make a symbol table entry for it, and if so, create a new entry
21210 and return a pointer to it.
21211 If TYPE is NULL, determine symbol type from the die, otherwise
34eaf542
TT
21212 used the passed type.
21213 If SPACE is not NULL, use it to hold the new symbol. If it is
21214 NULL, allocate a new symbol on the objfile's obstack. */
c906108c
SS
21215
21216static struct symbol *
5e2db402
TT
21217new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
21218 struct symbol *space)
c906108c 21219{
518817b3
SM
21220 struct dwarf2_per_objfile *dwarf2_per_objfile
21221 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 21222 struct objfile *objfile = dwarf2_per_objfile->objfile;
3e29f34a 21223 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c 21224 struct symbol *sym = NULL;
15d034d0 21225 const char *name;
c906108c
SS
21226 struct attribute *attr = NULL;
21227 struct attribute *attr2 = NULL;
e142c38c 21228 CORE_ADDR baseaddr;
e37fd15a
SW
21229 struct pending **list_to_add = NULL;
21230
edb3359d 21231 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
e142c38c
DJ
21232
21233 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 21234
94af9270 21235 name = dwarf2_name (die, cu);
c906108c
SS
21236 if (name)
21237 {
94af9270 21238 const char *linkagename;
34eaf542 21239 int suppress_add = 0;
94af9270 21240
34eaf542
TT
21241 if (space)
21242 sym = space;
21243 else
e623cf5d 21244 sym = allocate_symbol (objfile);
c906108c 21245 OBJSTAT (objfile, n_syms++);
2de7ced7
DJ
21246
21247 /* Cache this symbol's name and the name's demangled form (if any). */
f85f34ed 21248 SYMBOL_SET_LANGUAGE (sym, cu->language, &objfile->objfile_obstack);
94af9270
KS
21249 linkagename = dwarf2_physname (name, die, cu);
21250 SYMBOL_SET_NAMES (sym, linkagename, strlen (linkagename), 0, objfile);
c906108c 21251
f55ee35c
JK
21252 /* Fortran does not have mangling standard and the mangling does differ
21253 between gfortran, iFort etc. */
21254 if (cu->language == language_fortran
b250c185 21255 && symbol_get_demangled_name (&(sym->ginfo)) == NULL)
29df156d 21256 symbol_set_demangled_name (&(sym->ginfo),
cfc594ee 21257 dwarf2_full_name (name, die, cu),
29df156d 21258 NULL);
f55ee35c 21259
c906108c 21260 /* Default assumptions.
c5aa993b 21261 Use the passed type or decode it from the die. */
176620f1 21262 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
f1e6e072 21263 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
c906108c
SS
21264 if (type != NULL)
21265 SYMBOL_TYPE (sym) = type;
21266 else
e7c27a73 21267 SYMBOL_TYPE (sym) = die_type (die, cu);
edb3359d
DJ
21268 attr = dwarf2_attr (die,
21269 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
21270 cu);
c906108c
SS
21271 if (attr)
21272 {
21273 SYMBOL_LINE (sym) = DW_UNSND (attr);
21274 }
cb1df416 21275
edb3359d
DJ
21276 attr = dwarf2_attr (die,
21277 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
21278 cu);
cb1df416
DJ
21279 if (attr)
21280 {
ecfb656c 21281 file_name_index file_index = (file_name_index) DW_UNSND (attr);
8c43009f 21282 struct file_entry *fe;
9a619af0 21283
ecfb656c
PA
21284 if (cu->line_header != NULL)
21285 fe = cu->line_header->file_name_at (file_index);
8c43009f
PA
21286 else
21287 fe = NULL;
21288
21289 if (fe == NULL)
cb1df416
DJ
21290 complaint (&symfile_complaints,
21291 _("file index out of range"));
8c43009f
PA
21292 else
21293 symbol_set_symtab (sym, fe->symtab);
cb1df416
DJ
21294 }
21295
c906108c
SS
21296 switch (die->tag)
21297 {
21298 case DW_TAG_label:
e142c38c 21299 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
c906108c 21300 if (attr)
3e29f34a
MR
21301 {
21302 CORE_ADDR addr;
21303
21304 addr = attr_value_as_address (attr);
21305 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr + baseaddr);
21306 SYMBOL_VALUE_ADDRESS (sym) = addr;
21307 }
0f5238ed
TT
21308 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
21309 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
f1e6e072 21310 SYMBOL_ACLASS_INDEX (sym) = LOC_LABEL;
0f5238ed 21311 add_symbol_to_list (sym, cu->list_in_scope);
c906108c
SS
21312 break;
21313 case DW_TAG_subprogram:
21314 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
21315 finish_block. */
f1e6e072 21316 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
e142c38c 21317 attr2 = dwarf2_attr (die, DW_AT_external, cu);
2cfa0c8d
JB
21318 if ((attr2 && (DW_UNSND (attr2) != 0))
21319 || cu->language == language_ada)
c906108c 21320 {
2cfa0c8d
JB
21321 /* Subprograms marked external are stored as a global symbol.
21322 Ada subprograms, whether marked external or not, are always
21323 stored as a global symbol, because we want to be able to
21324 access them globally. For instance, we want to be able
21325 to break on a nested subprogram without having to
21326 specify the context. */
e37fd15a 21327 list_to_add = &global_symbols;
c906108c
SS
21328 }
21329 else
21330 {
e37fd15a 21331 list_to_add = cu->list_in_scope;
c906108c
SS
21332 }
21333 break;
edb3359d
DJ
21334 case DW_TAG_inlined_subroutine:
21335 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
21336 finish_block. */
f1e6e072 21337 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
edb3359d 21338 SYMBOL_INLINED (sym) = 1;
481860b3 21339 list_to_add = cu->list_in_scope;
edb3359d 21340 break;
34eaf542
TT
21341 case DW_TAG_template_value_param:
21342 suppress_add = 1;
21343 /* Fall through. */
72929c62 21344 case DW_TAG_constant:
c906108c 21345 case DW_TAG_variable:
254e6b9e 21346 case DW_TAG_member:
0963b4bd
MS
21347 /* Compilation with minimal debug info may result in
21348 variables with missing type entries. Change the
21349 misleading `void' type to something sensible. */
c906108c 21350 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
46a4882b 21351 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_int;
64c50499 21352
e142c38c 21353 attr = dwarf2_attr (die, DW_AT_const_value, cu);
254e6b9e
DE
21354 /* In the case of DW_TAG_member, we should only be called for
21355 static const members. */
21356 if (die->tag == DW_TAG_member)
21357 {
3863f96c
DE
21358 /* dwarf2_add_field uses die_is_declaration,
21359 so we do the same. */
254e6b9e
DE
21360 gdb_assert (die_is_declaration (die, cu));
21361 gdb_assert (attr);
21362 }
c906108c
SS
21363 if (attr)
21364 {
e7c27a73 21365 dwarf2_const_value (attr, sym, cu);
e142c38c 21366 attr2 = dwarf2_attr (die, DW_AT_external, cu);
e37fd15a 21367 if (!suppress_add)
34eaf542
TT
21368 {
21369 if (attr2 && (DW_UNSND (attr2) != 0))
e37fd15a 21370 list_to_add = &global_symbols;
34eaf542 21371 else
e37fd15a 21372 list_to_add = cu->list_in_scope;
34eaf542 21373 }
c906108c
SS
21374 break;
21375 }
e142c38c 21376 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
21377 if (attr)
21378 {
e7c27a73 21379 var_decode_location (attr, sym, cu);
e142c38c 21380 attr2 = dwarf2_attr (die, DW_AT_external, cu);
4357ac6c
TT
21381
21382 /* Fortran explicitly imports any global symbols to the local
21383 scope by DW_TAG_common_block. */
21384 if (cu->language == language_fortran && die->parent
21385 && die->parent->tag == DW_TAG_common_block)
21386 attr2 = NULL;
21387
caac4577
JG
21388 if (SYMBOL_CLASS (sym) == LOC_STATIC
21389 && SYMBOL_VALUE_ADDRESS (sym) == 0
21390 && !dwarf2_per_objfile->has_section_at_zero)
21391 {
21392 /* When a static variable is eliminated by the linker,
21393 the corresponding debug information is not stripped
21394 out, but the variable address is set to null;
21395 do not add such variables into symbol table. */
21396 }
21397 else if (attr2 && (DW_UNSND (attr2) != 0))
1c809c68 21398 {
f55ee35c
JK
21399 /* Workaround gfortran PR debug/40040 - it uses
21400 DW_AT_location for variables in -fPIC libraries which may
21401 get overriden by other libraries/executable and get
21402 a different address. Resolve it by the minimal symbol
21403 which may come from inferior's executable using copy
21404 relocation. Make this workaround only for gfortran as for
21405 other compilers GDB cannot guess the minimal symbol
21406 Fortran mangling kind. */
21407 if (cu->language == language_fortran && die->parent
21408 && die->parent->tag == DW_TAG_module
21409 && cu->producer
28586665 21410 && startswith (cu->producer, "GNU Fortran"))
f1e6e072 21411 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
f55ee35c 21412
1c809c68
TT
21413 /* A variable with DW_AT_external is never static,
21414 but it may be block-scoped. */
21415 list_to_add = (cu->list_in_scope == &file_symbols
21416 ? &global_symbols : cu->list_in_scope);
1c809c68 21417 }
c906108c 21418 else
e37fd15a 21419 list_to_add = cu->list_in_scope;
c906108c
SS
21420 }
21421 else
21422 {
21423 /* We do not know the address of this symbol.
c5aa993b
JM
21424 If it is an external symbol and we have type information
21425 for it, enter the symbol as a LOC_UNRESOLVED symbol.
21426 The address of the variable will then be determined from
21427 the minimal symbol table whenever the variable is
21428 referenced. */
e142c38c 21429 attr2 = dwarf2_attr (die, DW_AT_external, cu);
0971de02
TT
21430
21431 /* Fortran explicitly imports any global symbols to the local
21432 scope by DW_TAG_common_block. */
21433 if (cu->language == language_fortran && die->parent
21434 && die->parent->tag == DW_TAG_common_block)
21435 {
21436 /* SYMBOL_CLASS doesn't matter here because
21437 read_common_block is going to reset it. */
21438 if (!suppress_add)
21439 list_to_add = cu->list_in_scope;
21440 }
21441 else if (attr2 && (DW_UNSND (attr2) != 0)
21442 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
c906108c 21443 {
0fe7935b
DJ
21444 /* A variable with DW_AT_external is never static, but it
21445 may be block-scoped. */
21446 list_to_add = (cu->list_in_scope == &file_symbols
21447 ? &global_symbols : cu->list_in_scope);
21448
f1e6e072 21449 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
c906108c 21450 }
442ddf59
JK
21451 else if (!die_is_declaration (die, cu))
21452 {
21453 /* Use the default LOC_OPTIMIZED_OUT class. */
21454 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
e37fd15a
SW
21455 if (!suppress_add)
21456 list_to_add = cu->list_in_scope;
442ddf59 21457 }
c906108c
SS
21458 }
21459 break;
21460 case DW_TAG_formal_parameter:
edb3359d
DJ
21461 /* If we are inside a function, mark this as an argument. If
21462 not, we might be looking at an argument to an inlined function
21463 when we do not have enough information to show inlined frames;
21464 pretend it's a local variable in that case so that the user can
21465 still see it. */
21466 if (context_stack_depth > 0
21467 && context_stack[context_stack_depth - 1].name != NULL)
21468 SYMBOL_IS_ARGUMENT (sym) = 1;
e142c38c 21469 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
21470 if (attr)
21471 {
e7c27a73 21472 var_decode_location (attr, sym, cu);
c906108c 21473 }
e142c38c 21474 attr = dwarf2_attr (die, DW_AT_const_value, cu);
c906108c
SS
21475 if (attr)
21476 {
e7c27a73 21477 dwarf2_const_value (attr, sym, cu);
c906108c 21478 }
f346a30d 21479
e37fd15a 21480 list_to_add = cu->list_in_scope;
c906108c
SS
21481 break;
21482 case DW_TAG_unspecified_parameters:
21483 /* From varargs functions; gdb doesn't seem to have any
21484 interest in this information, so just ignore it for now.
21485 (FIXME?) */
21486 break;
34eaf542
TT
21487 case DW_TAG_template_type_param:
21488 suppress_add = 1;
21489 /* Fall through. */
c906108c 21490 case DW_TAG_class_type:
680b30c7 21491 case DW_TAG_interface_type:
c906108c
SS
21492 case DW_TAG_structure_type:
21493 case DW_TAG_union_type:
72019c9c 21494 case DW_TAG_set_type:
c906108c 21495 case DW_TAG_enumeration_type:
f1e6e072 21496 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
176620f1 21497 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
c906108c 21498
63d06c5c 21499 {
9c37b5ae 21500 /* NOTE: carlton/2003-11-10: C++ class symbols shouldn't
63d06c5c
DC
21501 really ever be static objects: otherwise, if you try
21502 to, say, break of a class's method and you're in a file
21503 which doesn't mention that class, it won't work unless
21504 the check for all static symbols in lookup_symbol_aux
21505 saves you. See the OtherFileClass tests in
21506 gdb.c++/namespace.exp. */
21507
e37fd15a 21508 if (!suppress_add)
34eaf542 21509 {
34eaf542 21510 list_to_add = (cu->list_in_scope == &file_symbols
9c37b5ae 21511 && cu->language == language_cplus
34eaf542 21512 ? &global_symbols : cu->list_in_scope);
63d06c5c 21513
64382290 21514 /* The semantics of C++ state that "struct foo {
9c37b5ae 21515 ... }" also defines a typedef for "foo". */
64382290 21516 if (cu->language == language_cplus
45280282 21517 || cu->language == language_ada
c44af4eb
TT
21518 || cu->language == language_d
21519 || cu->language == language_rust)
64382290
TT
21520 {
21521 /* The symbol's name is already allocated along
21522 with this objfile, so we don't need to
21523 duplicate it for the type. */
21524 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
21525 TYPE_NAME (SYMBOL_TYPE (sym)) = SYMBOL_SEARCH_NAME (sym);
21526 }
63d06c5c
DC
21527 }
21528 }
c906108c
SS
21529 break;
21530 case DW_TAG_typedef:
f1e6e072 21531 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
63d06c5c 21532 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 21533 list_to_add = cu->list_in_scope;
63d06c5c 21534 break;
c906108c 21535 case DW_TAG_base_type:
a02abb62 21536 case DW_TAG_subrange_type:
f1e6e072 21537 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
176620f1 21538 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 21539 list_to_add = cu->list_in_scope;
c906108c
SS
21540 break;
21541 case DW_TAG_enumerator:
e142c38c 21542 attr = dwarf2_attr (die, DW_AT_const_value, cu);
c906108c
SS
21543 if (attr)
21544 {
e7c27a73 21545 dwarf2_const_value (attr, sym, cu);
c906108c 21546 }
63d06c5c
DC
21547 {
21548 /* NOTE: carlton/2003-11-10: See comment above in the
21549 DW_TAG_class_type, etc. block. */
21550
e142c38c 21551 list_to_add = (cu->list_in_scope == &file_symbols
9c37b5ae 21552 && cu->language == language_cplus
e142c38c 21553 ? &global_symbols : cu->list_in_scope);
63d06c5c 21554 }
c906108c 21555 break;
74921315 21556 case DW_TAG_imported_declaration:
5c4e30ca 21557 case DW_TAG_namespace:
f1e6e072 21558 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
e37fd15a 21559 list_to_add = &global_symbols;
5c4e30ca 21560 break;
530e8392
KB
21561 case DW_TAG_module:
21562 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21563 SYMBOL_DOMAIN (sym) = MODULE_DOMAIN;
21564 list_to_add = &global_symbols;
21565 break;
4357ac6c 21566 case DW_TAG_common_block:
f1e6e072 21567 SYMBOL_ACLASS_INDEX (sym) = LOC_COMMON_BLOCK;
4357ac6c
TT
21568 SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
21569 add_symbol_to_list (sym, cu->list_in_scope);
21570 break;
c906108c
SS
21571 default:
21572 /* Not a tag we recognize. Hopefully we aren't processing
21573 trash data, but since we must specifically ignore things
21574 we don't recognize, there is nothing else we should do at
0963b4bd 21575 this point. */
e2e0b3e5 21576 complaint (&symfile_complaints, _("unsupported tag: '%s'"),
4d3c2250 21577 dwarf_tag_name (die->tag));
c906108c
SS
21578 break;
21579 }
df8a16a1 21580
e37fd15a
SW
21581 if (suppress_add)
21582 {
21583 sym->hash_next = objfile->template_symbols;
21584 objfile->template_symbols = sym;
21585 list_to_add = NULL;
21586 }
21587
21588 if (list_to_add != NULL)
21589 add_symbol_to_list (sym, list_to_add);
21590
df8a16a1
DJ
21591 /* For the benefit of old versions of GCC, check for anonymous
21592 namespaces based on the demangled name. */
4d4ec4e5 21593 if (!cu->processing_has_namespace_info
94af9270 21594 && cu->language == language_cplus)
a10964d1 21595 cp_scan_for_anonymous_namespaces (sym, objfile);
c906108c
SS
21596 }
21597 return (sym);
21598}
21599
98bfdba5
PA
21600/* Given an attr with a DW_FORM_dataN value in host byte order,
21601 zero-extend it as appropriate for the symbol's type. The DWARF
21602 standard (v4) is not entirely clear about the meaning of using
21603 DW_FORM_dataN for a constant with a signed type, where the type is
21604 wider than the data. The conclusion of a discussion on the DWARF
21605 list was that this is unspecified. We choose to always zero-extend
21606 because that is the interpretation long in use by GCC. */
c906108c 21607
98bfdba5 21608static gdb_byte *
ff39bb5e 21609dwarf2_const_value_data (const struct attribute *attr, struct obstack *obstack,
12df843f 21610 struct dwarf2_cu *cu, LONGEST *value, int bits)
c906108c 21611{
518817b3 21612 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
e17a4113
UW
21613 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
21614 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
98bfdba5
PA
21615 LONGEST l = DW_UNSND (attr);
21616
21617 if (bits < sizeof (*value) * 8)
21618 {
21619 l &= ((LONGEST) 1 << bits) - 1;
21620 *value = l;
21621 }
21622 else if (bits == sizeof (*value) * 8)
21623 *value = l;
21624 else
21625 {
224c3ddb 21626 gdb_byte *bytes = (gdb_byte *) obstack_alloc (obstack, bits / 8);
98bfdba5
PA
21627 store_unsigned_integer (bytes, bits / 8, byte_order, l);
21628 return bytes;
21629 }
21630
21631 return NULL;
21632}
21633
21634/* Read a constant value from an attribute. Either set *VALUE, or if
21635 the value does not fit in *VALUE, set *BYTES - either already
21636 allocated on the objfile obstack, or newly allocated on OBSTACK,
21637 or, set *BATON, if we translated the constant to a location
21638 expression. */
21639
21640static void
ff39bb5e 21641dwarf2_const_value_attr (const struct attribute *attr, struct type *type,
98bfdba5
PA
21642 const char *name, struct obstack *obstack,
21643 struct dwarf2_cu *cu,
d521ce57 21644 LONGEST *value, const gdb_byte **bytes,
98bfdba5
PA
21645 struct dwarf2_locexpr_baton **baton)
21646{
518817b3 21647 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
98bfdba5 21648 struct comp_unit_head *cu_header = &cu->header;
c906108c 21649 struct dwarf_block *blk;
98bfdba5
PA
21650 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
21651 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
21652
21653 *value = 0;
21654 *bytes = NULL;
21655 *baton = NULL;
c906108c
SS
21656
21657 switch (attr->form)
21658 {
21659 case DW_FORM_addr:
3019eac3 21660 case DW_FORM_GNU_addr_index:
ac56253d 21661 {
ac56253d
TT
21662 gdb_byte *data;
21663
98bfdba5
PA
21664 if (TYPE_LENGTH (type) != cu_header->addr_size)
21665 dwarf2_const_value_length_mismatch_complaint (name,
ac56253d 21666 cu_header->addr_size,
98bfdba5 21667 TYPE_LENGTH (type));
ac56253d
TT
21668 /* Symbols of this form are reasonably rare, so we just
21669 piggyback on the existing location code rather than writing
21670 a new implementation of symbol_computed_ops. */
8d749320 21671 *baton = XOBNEW (obstack, struct dwarf2_locexpr_baton);
98bfdba5
PA
21672 (*baton)->per_cu = cu->per_cu;
21673 gdb_assert ((*baton)->per_cu);
ac56253d 21674
98bfdba5 21675 (*baton)->size = 2 + cu_header->addr_size;
224c3ddb 21676 data = (gdb_byte *) obstack_alloc (obstack, (*baton)->size);
98bfdba5 21677 (*baton)->data = data;
ac56253d
TT
21678
21679 data[0] = DW_OP_addr;
21680 store_unsigned_integer (&data[1], cu_header->addr_size,
21681 byte_order, DW_ADDR (attr));
21682 data[cu_header->addr_size + 1] = DW_OP_stack_value;
ac56253d 21683 }
c906108c 21684 break;
4ac36638 21685 case DW_FORM_string:
93b5768b 21686 case DW_FORM_strp:
3019eac3 21687 case DW_FORM_GNU_str_index:
36586728 21688 case DW_FORM_GNU_strp_alt:
98bfdba5
PA
21689 /* DW_STRING is already allocated on the objfile obstack, point
21690 directly to it. */
d521ce57 21691 *bytes = (const gdb_byte *) DW_STRING (attr);
93b5768b 21692 break;
c906108c
SS
21693 case DW_FORM_block1:
21694 case DW_FORM_block2:
21695 case DW_FORM_block4:
21696 case DW_FORM_block:
2dc7f7b3 21697 case DW_FORM_exprloc:
0224619f 21698 case DW_FORM_data16:
c906108c 21699 blk = DW_BLOCK (attr);
98bfdba5
PA
21700 if (TYPE_LENGTH (type) != blk->size)
21701 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
21702 TYPE_LENGTH (type));
21703 *bytes = blk->data;
c906108c 21704 break;
2df3850c
JM
21705
21706 /* The DW_AT_const_value attributes are supposed to carry the
21707 symbol's value "represented as it would be on the target
21708 architecture." By the time we get here, it's already been
21709 converted to host endianness, so we just need to sign- or
21710 zero-extend it as appropriate. */
21711 case DW_FORM_data1:
3aef2284 21712 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 8);
2df3850c 21713 break;
c906108c 21714 case DW_FORM_data2:
3aef2284 21715 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 16);
2df3850c 21716 break;
c906108c 21717 case DW_FORM_data4:
3aef2284 21718 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 32);
2df3850c 21719 break;
c906108c 21720 case DW_FORM_data8:
3aef2284 21721 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 64);
2df3850c
JM
21722 break;
21723
c906108c 21724 case DW_FORM_sdata:
663c44ac 21725 case DW_FORM_implicit_const:
98bfdba5 21726 *value = DW_SND (attr);
2df3850c
JM
21727 break;
21728
c906108c 21729 case DW_FORM_udata:
98bfdba5 21730 *value = DW_UNSND (attr);
c906108c 21731 break;
2df3850c 21732
c906108c 21733 default:
4d3c2250 21734 complaint (&symfile_complaints,
e2e0b3e5 21735 _("unsupported const value attribute form: '%s'"),
4d3c2250 21736 dwarf_form_name (attr->form));
98bfdba5 21737 *value = 0;
c906108c
SS
21738 break;
21739 }
21740}
21741
2df3850c 21742
98bfdba5
PA
21743/* Copy constant value from an attribute to a symbol. */
21744
2df3850c 21745static void
ff39bb5e 21746dwarf2_const_value (const struct attribute *attr, struct symbol *sym,
98bfdba5 21747 struct dwarf2_cu *cu)
2df3850c 21748{
518817b3 21749 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
12df843f 21750 LONGEST value;
d521ce57 21751 const gdb_byte *bytes;
98bfdba5 21752 struct dwarf2_locexpr_baton *baton;
2df3850c 21753
98bfdba5
PA
21754 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
21755 SYMBOL_PRINT_NAME (sym),
21756 &objfile->objfile_obstack, cu,
21757 &value, &bytes, &baton);
2df3850c 21758
98bfdba5
PA
21759 if (baton != NULL)
21760 {
98bfdba5 21761 SYMBOL_LOCATION_BATON (sym) = baton;
f1e6e072 21762 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
98bfdba5
PA
21763 }
21764 else if (bytes != NULL)
21765 {
21766 SYMBOL_VALUE_BYTES (sym) = bytes;
f1e6e072 21767 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST_BYTES;
98bfdba5
PA
21768 }
21769 else
21770 {
21771 SYMBOL_VALUE (sym) = value;
f1e6e072 21772 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST;
98bfdba5 21773 }
2df3850c
JM
21774}
21775
c906108c
SS
21776/* Return the type of the die in question using its DW_AT_type attribute. */
21777
21778static struct type *
e7c27a73 21779die_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 21780{
c906108c 21781 struct attribute *type_attr;
c906108c 21782
e142c38c 21783 type_attr = dwarf2_attr (die, DW_AT_type, cu);
c906108c
SS
21784 if (!type_attr)
21785 {
518817b3 21786 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c 21787 /* A missing DW_AT_type represents a void type. */
518817b3 21788 return objfile_type (objfile)->builtin_void;
c906108c 21789 }
348e048f 21790
673bfd45 21791 return lookup_die_type (die, type_attr, cu);
c906108c
SS
21792}
21793
b4ba55a1
JB
21794/* True iff CU's producer generates GNAT Ada auxiliary information
21795 that allows to find parallel types through that information instead
21796 of having to do expensive parallel lookups by type name. */
21797
21798static int
21799need_gnat_info (struct dwarf2_cu *cu)
21800{
de4cb04a
JB
21801 /* Assume that the Ada compiler was GNAT, which always produces
21802 the auxiliary information. */
21803 return (cu->language == language_ada);
b4ba55a1
JB
21804}
21805
b4ba55a1
JB
21806/* Return the auxiliary type of the die in question using its
21807 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
21808 attribute is not present. */
21809
21810static struct type *
21811die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
21812{
b4ba55a1 21813 struct attribute *type_attr;
b4ba55a1
JB
21814
21815 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
21816 if (!type_attr)
21817 return NULL;
21818
673bfd45 21819 return lookup_die_type (die, type_attr, cu);
b4ba55a1
JB
21820}
21821
21822/* If DIE has a descriptive_type attribute, then set the TYPE's
21823 descriptive type accordingly. */
21824
21825static void
21826set_descriptive_type (struct type *type, struct die_info *die,
21827 struct dwarf2_cu *cu)
21828{
21829 struct type *descriptive_type = die_descriptive_type (die, cu);
21830
21831 if (descriptive_type)
21832 {
21833 ALLOCATE_GNAT_AUX_TYPE (type);
21834 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
21835 }
21836}
21837
c906108c
SS
21838/* Return the containing type of the die in question using its
21839 DW_AT_containing_type attribute. */
21840
21841static struct type *
e7c27a73 21842die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 21843{
c906108c 21844 struct attribute *type_attr;
518817b3 21845 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c 21846
e142c38c 21847 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
33ac96f0
JK
21848 if (!type_attr)
21849 error (_("Dwarf Error: Problem turning containing type into gdb type "
518817b3 21850 "[in module %s]"), objfile_name (objfile));
33ac96f0 21851
673bfd45 21852 return lookup_die_type (die, type_attr, cu);
c906108c
SS
21853}
21854
ac9ec31b
DE
21855/* Return an error marker type to use for the ill formed type in DIE/CU. */
21856
21857static struct type *
21858build_error_marker_type (struct dwarf2_cu *cu, struct die_info *die)
21859{
518817b3
SM
21860 struct dwarf2_per_objfile *dwarf2_per_objfile
21861 = cu->per_cu->dwarf2_per_objfile;
ac9ec31b
DE
21862 struct objfile *objfile = dwarf2_per_objfile->objfile;
21863 char *message, *saved;
21864
9d8780f0 21865 message = xstrprintf (_("<unknown type in %s, CU %s, DIE %s>"),
4262abfb 21866 objfile_name (objfile),
9d8780f0
SM
21867 sect_offset_str (cu->header.sect_off),
21868 sect_offset_str (die->sect_off));
224c3ddb
SM
21869 saved = (char *) obstack_copy0 (&objfile->objfile_obstack,
21870 message, strlen (message));
ac9ec31b
DE
21871 xfree (message);
21872
19f392bc 21873 return init_type (objfile, TYPE_CODE_ERROR, 0, saved);
ac9ec31b
DE
21874}
21875
673bfd45 21876/* Look up the type of DIE in CU using its type attribute ATTR.
ac9ec31b
DE
21877 ATTR must be one of: DW_AT_type, DW_AT_GNAT_descriptive_type,
21878 DW_AT_containing_type.
673bfd45
DE
21879 If there is no type substitute an error marker. */
21880
c906108c 21881static struct type *
ff39bb5e 21882lookup_die_type (struct die_info *die, const struct attribute *attr,
673bfd45 21883 struct dwarf2_cu *cu)
c906108c 21884{
518817b3
SM
21885 struct dwarf2_per_objfile *dwarf2_per_objfile
21886 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 21887 struct objfile *objfile = dwarf2_per_objfile->objfile;
f792889a
DJ
21888 struct type *this_type;
21889
ac9ec31b
DE
21890 gdb_assert (attr->name == DW_AT_type
21891 || attr->name == DW_AT_GNAT_descriptive_type
21892 || attr->name == DW_AT_containing_type);
21893
673bfd45
DE
21894 /* First see if we have it cached. */
21895
36586728
TT
21896 if (attr->form == DW_FORM_GNU_ref_alt)
21897 {
21898 struct dwarf2_per_cu_data *per_cu;
9c541725 21899 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
36586728 21900
ed2dc618
SM
21901 per_cu = dwarf2_find_containing_comp_unit (sect_off, 1,
21902 dwarf2_per_objfile);
9c541725 21903 this_type = get_die_type_at_offset (sect_off, per_cu);
36586728 21904 }
7771576e 21905 else if (attr_form_is_ref (attr))
673bfd45 21906 {
9c541725 21907 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
673bfd45 21908
9c541725 21909 this_type = get_die_type_at_offset (sect_off, cu->per_cu);
673bfd45 21910 }
55f1336d 21911 else if (attr->form == DW_FORM_ref_sig8)
673bfd45 21912 {
ac9ec31b 21913 ULONGEST signature = DW_SIGNATURE (attr);
673bfd45 21914
ac9ec31b 21915 return get_signatured_type (die, signature, cu);
673bfd45
DE
21916 }
21917 else
21918 {
ac9ec31b
DE
21919 complaint (&symfile_complaints,
21920 _("Dwarf Error: Bad type attribute %s in DIE"
9d8780f0
SM
21921 " at %s [in module %s]"),
21922 dwarf_attr_name (attr->name), sect_offset_str (die->sect_off),
4262abfb 21923 objfile_name (objfile));
ac9ec31b 21924 return build_error_marker_type (cu, die);
673bfd45
DE
21925 }
21926
21927 /* If not cached we need to read it in. */
21928
21929 if (this_type == NULL)
21930 {
ac9ec31b 21931 struct die_info *type_die = NULL;
673bfd45
DE
21932 struct dwarf2_cu *type_cu = cu;
21933
7771576e 21934 if (attr_form_is_ref (attr))
ac9ec31b
DE
21935 type_die = follow_die_ref (die, attr, &type_cu);
21936 if (type_die == NULL)
21937 return build_error_marker_type (cu, die);
21938 /* If we find the type now, it's probably because the type came
3019eac3
DE
21939 from an inter-CU reference and the type's CU got expanded before
21940 ours. */
ac9ec31b 21941 this_type = read_type_die (type_die, type_cu);
673bfd45
DE
21942 }
21943
21944 /* If we still don't have a type use an error marker. */
21945
21946 if (this_type == NULL)
ac9ec31b 21947 return build_error_marker_type (cu, die);
673bfd45 21948
f792889a 21949 return this_type;
c906108c
SS
21950}
21951
673bfd45
DE
21952/* Return the type in DIE, CU.
21953 Returns NULL for invalid types.
21954
02142a6c 21955 This first does a lookup in die_type_hash,
673bfd45
DE
21956 and only reads the die in if necessary.
21957
21958 NOTE: This can be called when reading in partial or full symbols. */
21959
f792889a 21960static struct type *
e7c27a73 21961read_type_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 21962{
f792889a
DJ
21963 struct type *this_type;
21964
21965 this_type = get_die_type (die, cu);
21966 if (this_type)
21967 return this_type;
21968
673bfd45
DE
21969 return read_type_die_1 (die, cu);
21970}
21971
21972/* Read the type in DIE, CU.
21973 Returns NULL for invalid types. */
21974
21975static struct type *
21976read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
21977{
21978 struct type *this_type = NULL;
21979
c906108c
SS
21980 switch (die->tag)
21981 {
21982 case DW_TAG_class_type:
680b30c7 21983 case DW_TAG_interface_type:
c906108c
SS
21984 case DW_TAG_structure_type:
21985 case DW_TAG_union_type:
f792889a 21986 this_type = read_structure_type (die, cu);
c906108c
SS
21987 break;
21988 case DW_TAG_enumeration_type:
f792889a 21989 this_type = read_enumeration_type (die, cu);
c906108c
SS
21990 break;
21991 case DW_TAG_subprogram:
21992 case DW_TAG_subroutine_type:
edb3359d 21993 case DW_TAG_inlined_subroutine:
f792889a 21994 this_type = read_subroutine_type (die, cu);
c906108c
SS
21995 break;
21996 case DW_TAG_array_type:
f792889a 21997 this_type = read_array_type (die, cu);
c906108c 21998 break;
72019c9c 21999 case DW_TAG_set_type:
f792889a 22000 this_type = read_set_type (die, cu);
72019c9c 22001 break;
c906108c 22002 case DW_TAG_pointer_type:
f792889a 22003 this_type = read_tag_pointer_type (die, cu);
c906108c
SS
22004 break;
22005 case DW_TAG_ptr_to_member_type:
f792889a 22006 this_type = read_tag_ptr_to_member_type (die, cu);
c906108c
SS
22007 break;
22008 case DW_TAG_reference_type:
4297a3f0
AV
22009 this_type = read_tag_reference_type (die, cu, TYPE_CODE_REF);
22010 break;
22011 case DW_TAG_rvalue_reference_type:
22012 this_type = read_tag_reference_type (die, cu, TYPE_CODE_RVALUE_REF);
c906108c
SS
22013 break;
22014 case DW_TAG_const_type:
f792889a 22015 this_type = read_tag_const_type (die, cu);
c906108c
SS
22016 break;
22017 case DW_TAG_volatile_type:
f792889a 22018 this_type = read_tag_volatile_type (die, cu);
c906108c 22019 break;
06d66ee9
TT
22020 case DW_TAG_restrict_type:
22021 this_type = read_tag_restrict_type (die, cu);
22022 break;
c906108c 22023 case DW_TAG_string_type:
f792889a 22024 this_type = read_tag_string_type (die, cu);
c906108c
SS
22025 break;
22026 case DW_TAG_typedef:
f792889a 22027 this_type = read_typedef (die, cu);
c906108c 22028 break;
a02abb62 22029 case DW_TAG_subrange_type:
f792889a 22030 this_type = read_subrange_type (die, cu);
a02abb62 22031 break;
c906108c 22032 case DW_TAG_base_type:
f792889a 22033 this_type = read_base_type (die, cu);
c906108c 22034 break;
81a17f79 22035 case DW_TAG_unspecified_type:
f792889a 22036 this_type = read_unspecified_type (die, cu);
81a17f79 22037 break;
0114d602
DJ
22038 case DW_TAG_namespace:
22039 this_type = read_namespace_type (die, cu);
22040 break;
f55ee35c
JK
22041 case DW_TAG_module:
22042 this_type = read_module_type (die, cu);
22043 break;
a2c2acaf
MW
22044 case DW_TAG_atomic_type:
22045 this_type = read_tag_atomic_type (die, cu);
22046 break;
c906108c 22047 default:
3e43a32a
MS
22048 complaint (&symfile_complaints,
22049 _("unexpected tag in read_type_die: '%s'"),
4d3c2250 22050 dwarf_tag_name (die->tag));
c906108c
SS
22051 break;
22052 }
63d06c5c 22053
f792889a 22054 return this_type;
63d06c5c
DC
22055}
22056
abc72ce4
DE
22057/* See if we can figure out if the class lives in a namespace. We do
22058 this by looking for a member function; its demangled name will
22059 contain namespace info, if there is any.
22060 Return the computed name or NULL.
22061 Space for the result is allocated on the objfile's obstack.
22062 This is the full-die version of guess_partial_die_structure_name.
22063 In this case we know DIE has no useful parent. */
22064
22065static char *
22066guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
22067{
22068 struct die_info *spec_die;
22069 struct dwarf2_cu *spec_cu;
22070 struct die_info *child;
518817b3 22071 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
abc72ce4
DE
22072
22073 spec_cu = cu;
22074 spec_die = die_specification (die, &spec_cu);
22075 if (spec_die != NULL)
22076 {
22077 die = spec_die;
22078 cu = spec_cu;
22079 }
22080
22081 for (child = die->child;
22082 child != NULL;
22083 child = child->sibling)
22084 {
22085 if (child->tag == DW_TAG_subprogram)
22086 {
73b9be8b 22087 const char *linkage_name = dw2_linkage_name (child, cu);
abc72ce4 22088
7d45c7c3 22089 if (linkage_name != NULL)
abc72ce4
DE
22090 {
22091 char *actual_name
22092 = language_class_name_from_physname (cu->language_defn,
7d45c7c3 22093 linkage_name);
abc72ce4
DE
22094 char *name = NULL;
22095
22096 if (actual_name != NULL)
22097 {
15d034d0 22098 const char *die_name = dwarf2_name (die, cu);
abc72ce4
DE
22099
22100 if (die_name != NULL
22101 && strcmp (die_name, actual_name) != 0)
22102 {
22103 /* Strip off the class name from the full name.
22104 We want the prefix. */
22105 int die_name_len = strlen (die_name);
22106 int actual_name_len = strlen (actual_name);
22107
22108 /* Test for '::' as a sanity check. */
22109 if (actual_name_len > die_name_len + 2
3e43a32a
MS
22110 && actual_name[actual_name_len
22111 - die_name_len - 1] == ':')
224c3ddb 22112 name = (char *) obstack_copy0 (
e3b94546 22113 &objfile->per_bfd->storage_obstack,
224c3ddb 22114 actual_name, actual_name_len - die_name_len - 2);
abc72ce4
DE
22115 }
22116 }
22117 xfree (actual_name);
22118 return name;
22119 }
22120 }
22121 }
22122
22123 return NULL;
22124}
22125
96408a79
SA
22126/* GCC might emit a nameless typedef that has a linkage name. Determine the
22127 prefix part in such case. See
22128 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
22129
a121b7c1 22130static const char *
96408a79
SA
22131anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
22132{
22133 struct attribute *attr;
e6a959d6 22134 const char *base;
96408a79
SA
22135
22136 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
22137 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
22138 return NULL;
22139
7d45c7c3 22140 if (dwarf2_string_attr (die, DW_AT_name, cu) != NULL)
96408a79
SA
22141 return NULL;
22142
73b9be8b 22143 attr = dw2_linkage_name_attr (die, cu);
96408a79
SA
22144 if (attr == NULL || DW_STRING (attr) == NULL)
22145 return NULL;
22146
22147 /* dwarf2_name had to be already called. */
22148 gdb_assert (DW_STRING_IS_CANONICAL (attr));
22149
22150 /* Strip the base name, keep any leading namespaces/classes. */
22151 base = strrchr (DW_STRING (attr), ':');
22152 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
22153 return "";
22154
518817b3 22155 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
e3b94546 22156 return (char *) obstack_copy0 (&objfile->per_bfd->storage_obstack,
224c3ddb
SM
22157 DW_STRING (attr),
22158 &base[-1] - DW_STRING (attr));
96408a79
SA
22159}
22160
fdde2d81 22161/* Return the name of the namespace/class that DIE is defined within,
0114d602 22162 or "" if we can't tell. The caller should not xfree the result.
fdde2d81 22163
0114d602
DJ
22164 For example, if we're within the method foo() in the following
22165 code:
22166
22167 namespace N {
22168 class C {
22169 void foo () {
22170 }
22171 };
22172 }
22173
22174 then determine_prefix on foo's die will return "N::C". */
fdde2d81 22175
0d5cff50 22176static const char *
e142c38c 22177determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
63d06c5c 22178{
518817b3
SM
22179 struct dwarf2_per_objfile *dwarf2_per_objfile
22180 = cu->per_cu->dwarf2_per_objfile;
0114d602
DJ
22181 struct die_info *parent, *spec_die;
22182 struct dwarf2_cu *spec_cu;
22183 struct type *parent_type;
a121b7c1 22184 const char *retval;
63d06c5c 22185
9c37b5ae 22186 if (cu->language != language_cplus
c44af4eb
TT
22187 && cu->language != language_fortran && cu->language != language_d
22188 && cu->language != language_rust)
0114d602
DJ
22189 return "";
22190
96408a79
SA
22191 retval = anonymous_struct_prefix (die, cu);
22192 if (retval)
22193 return retval;
22194
0114d602
DJ
22195 /* We have to be careful in the presence of DW_AT_specification.
22196 For example, with GCC 3.4, given the code
22197
22198 namespace N {
22199 void foo() {
22200 // Definition of N::foo.
22201 }
22202 }
22203
22204 then we'll have a tree of DIEs like this:
22205
22206 1: DW_TAG_compile_unit
22207 2: DW_TAG_namespace // N
22208 3: DW_TAG_subprogram // declaration of N::foo
22209 4: DW_TAG_subprogram // definition of N::foo
22210 DW_AT_specification // refers to die #3
22211
22212 Thus, when processing die #4, we have to pretend that we're in
22213 the context of its DW_AT_specification, namely the contex of die
22214 #3. */
22215 spec_cu = cu;
22216 spec_die = die_specification (die, &spec_cu);
22217 if (spec_die == NULL)
22218 parent = die->parent;
22219 else
63d06c5c 22220 {
0114d602
DJ
22221 parent = spec_die->parent;
22222 cu = spec_cu;
63d06c5c 22223 }
0114d602
DJ
22224
22225 if (parent == NULL)
22226 return "";
98bfdba5
PA
22227 else if (parent->building_fullname)
22228 {
22229 const char *name;
22230 const char *parent_name;
22231
22232 /* It has been seen on RealView 2.2 built binaries,
22233 DW_TAG_template_type_param types actually _defined_ as
22234 children of the parent class:
22235
22236 enum E {};
22237 template class <class Enum> Class{};
22238 Class<enum E> class_e;
22239
22240 1: DW_TAG_class_type (Class)
22241 2: DW_TAG_enumeration_type (E)
22242 3: DW_TAG_enumerator (enum1:0)
22243 3: DW_TAG_enumerator (enum2:1)
22244 ...
22245 2: DW_TAG_template_type_param
22246 DW_AT_type DW_FORM_ref_udata (E)
22247
22248 Besides being broken debug info, it can put GDB into an
22249 infinite loop. Consider:
22250
22251 When we're building the full name for Class<E>, we'll start
22252 at Class, and go look over its template type parameters,
22253 finding E. We'll then try to build the full name of E, and
22254 reach here. We're now trying to build the full name of E,
22255 and look over the parent DIE for containing scope. In the
22256 broken case, if we followed the parent DIE of E, we'd again
22257 find Class, and once again go look at its template type
22258 arguments, etc., etc. Simply don't consider such parent die
22259 as source-level parent of this die (it can't be, the language
22260 doesn't allow it), and break the loop here. */
22261 name = dwarf2_name (die, cu);
22262 parent_name = dwarf2_name (parent, cu);
22263 complaint (&symfile_complaints,
22264 _("template param type '%s' defined within parent '%s'"),
22265 name ? name : "<unknown>",
22266 parent_name ? parent_name : "<unknown>");
22267 return "";
22268 }
63d06c5c 22269 else
0114d602
DJ
22270 switch (parent->tag)
22271 {
63d06c5c 22272 case DW_TAG_namespace:
0114d602 22273 parent_type = read_type_die (parent, cu);
acebe513
UW
22274 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
22275 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
22276 Work around this problem here. */
22277 if (cu->language == language_cplus
22278 && strcmp (TYPE_TAG_NAME (parent_type), "::") == 0)
22279 return "";
0114d602
DJ
22280 /* We give a name to even anonymous namespaces. */
22281 return TYPE_TAG_NAME (parent_type);
63d06c5c 22282 case DW_TAG_class_type:
680b30c7 22283 case DW_TAG_interface_type:
63d06c5c 22284 case DW_TAG_structure_type:
0114d602 22285 case DW_TAG_union_type:
f55ee35c 22286 case DW_TAG_module:
0114d602
DJ
22287 parent_type = read_type_die (parent, cu);
22288 if (TYPE_TAG_NAME (parent_type) != NULL)
22289 return TYPE_TAG_NAME (parent_type);
22290 else
22291 /* An anonymous structure is only allowed non-static data
22292 members; no typedefs, no member functions, et cetera.
22293 So it does not need a prefix. */
22294 return "";
abc72ce4 22295 case DW_TAG_compile_unit:
95554aad 22296 case DW_TAG_partial_unit:
abc72ce4
DE
22297 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
22298 if (cu->language == language_cplus
8b70b953 22299 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
abc72ce4
DE
22300 && die->child != NULL
22301 && (die->tag == DW_TAG_class_type
22302 || die->tag == DW_TAG_structure_type
22303 || die->tag == DW_TAG_union_type))
22304 {
22305 char *name = guess_full_die_structure_name (die, cu);
22306 if (name != NULL)
22307 return name;
22308 }
22309 return "";
3d567982
TT
22310 case DW_TAG_enumeration_type:
22311 parent_type = read_type_die (parent, cu);
22312 if (TYPE_DECLARED_CLASS (parent_type))
22313 {
22314 if (TYPE_TAG_NAME (parent_type) != NULL)
22315 return TYPE_TAG_NAME (parent_type);
22316 return "";
22317 }
22318 /* Fall through. */
63d06c5c 22319 default:
8176b9b8 22320 return determine_prefix (parent, cu);
63d06c5c 22321 }
63d06c5c
DC
22322}
22323
3e43a32a
MS
22324/* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
22325 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
22326 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
22327 an obconcat, otherwise allocate storage for the result. The CU argument is
22328 used to determine the language and hence, the appropriate separator. */
987504bb 22329
f55ee35c 22330#define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
63d06c5c
DC
22331
22332static char *
f55ee35c
JK
22333typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
22334 int physname, struct dwarf2_cu *cu)
63d06c5c 22335{
f55ee35c 22336 const char *lead = "";
5c315b68 22337 const char *sep;
63d06c5c 22338
3e43a32a
MS
22339 if (suffix == NULL || suffix[0] == '\0'
22340 || prefix == NULL || prefix[0] == '\0')
987504bb 22341 sep = "";
45280282
IB
22342 else if (cu->language == language_d)
22343 {
22344 /* For D, the 'main' function could be defined in any module, but it
22345 should never be prefixed. */
22346 if (strcmp (suffix, "D main") == 0)
22347 {
22348 prefix = "";
22349 sep = "";
22350 }
22351 else
22352 sep = ".";
22353 }
f55ee35c
JK
22354 else if (cu->language == language_fortran && physname)
22355 {
22356 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
22357 DW_AT_MIPS_linkage_name is preferred and used instead. */
22358
22359 lead = "__";
22360 sep = "_MOD_";
22361 }
987504bb
JJ
22362 else
22363 sep = "::";
63d06c5c 22364
6dd47d34
DE
22365 if (prefix == NULL)
22366 prefix = "";
22367 if (suffix == NULL)
22368 suffix = "";
22369
987504bb
JJ
22370 if (obs == NULL)
22371 {
3e43a32a 22372 char *retval
224c3ddb
SM
22373 = ((char *)
22374 xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1));
9a619af0 22375
f55ee35c
JK
22376 strcpy (retval, lead);
22377 strcat (retval, prefix);
6dd47d34
DE
22378 strcat (retval, sep);
22379 strcat (retval, suffix);
63d06c5c
DC
22380 return retval;
22381 }
987504bb
JJ
22382 else
22383 {
22384 /* We have an obstack. */
f55ee35c 22385 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
987504bb 22386 }
63d06c5c
DC
22387}
22388
c906108c
SS
22389/* Return sibling of die, NULL if no sibling. */
22390
f9aca02d 22391static struct die_info *
fba45db2 22392sibling_die (struct die_info *die)
c906108c 22393{
639d11d3 22394 return die->sibling;
c906108c
SS
22395}
22396
71c25dea
TT
22397/* Get name of a die, return NULL if not found. */
22398
15d034d0
TT
22399static const char *
22400dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
71c25dea
TT
22401 struct obstack *obstack)
22402{
22403 if (name && cu->language == language_cplus)
22404 {
2f408ecb 22405 std::string canon_name = cp_canonicalize_string (name);
71c25dea 22406
2f408ecb 22407 if (!canon_name.empty ())
71c25dea 22408 {
2f408ecb
PA
22409 if (canon_name != name)
22410 name = (const char *) obstack_copy0 (obstack,
22411 canon_name.c_str (),
22412 canon_name.length ());
71c25dea
TT
22413 }
22414 }
22415
22416 return name;
c906108c
SS
22417}
22418
96553a0c
DE
22419/* Get name of a die, return NULL if not found.
22420 Anonymous namespaces are converted to their magic string. */
9219021c 22421
15d034d0 22422static const char *
e142c38c 22423dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
9219021c
DC
22424{
22425 struct attribute *attr;
518817b3 22426 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
9219021c 22427
e142c38c 22428 attr = dwarf2_attr (die, DW_AT_name, cu);
53832f31 22429 if ((!attr || !DW_STRING (attr))
96553a0c 22430 && die->tag != DW_TAG_namespace
53832f31
TT
22431 && die->tag != DW_TAG_class_type
22432 && die->tag != DW_TAG_interface_type
22433 && die->tag != DW_TAG_structure_type
22434 && die->tag != DW_TAG_union_type)
71c25dea
TT
22435 return NULL;
22436
22437 switch (die->tag)
22438 {
22439 case DW_TAG_compile_unit:
95554aad 22440 case DW_TAG_partial_unit:
71c25dea
TT
22441 /* Compilation units have a DW_AT_name that is a filename, not
22442 a source language identifier. */
22443 case DW_TAG_enumeration_type:
22444 case DW_TAG_enumerator:
22445 /* These tags always have simple identifiers already; no need
22446 to canonicalize them. */
22447 return DW_STRING (attr);
907af001 22448
96553a0c
DE
22449 case DW_TAG_namespace:
22450 if (attr != NULL && DW_STRING (attr) != NULL)
22451 return DW_STRING (attr);
22452 return CP_ANONYMOUS_NAMESPACE_STR;
22453
907af001
UW
22454 case DW_TAG_class_type:
22455 case DW_TAG_interface_type:
22456 case DW_TAG_structure_type:
22457 case DW_TAG_union_type:
22458 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
22459 structures or unions. These were of the form "._%d" in GCC 4.1,
22460 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
22461 and GCC 4.4. We work around this problem by ignoring these. */
53832f31 22462 if (attr && DW_STRING (attr)
61012eef
GB
22463 && (startswith (DW_STRING (attr), "._")
22464 || startswith (DW_STRING (attr), "<anonymous")))
907af001 22465 return NULL;
53832f31
TT
22466
22467 /* GCC might emit a nameless typedef that has a linkage name. See
22468 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
22469 if (!attr || DW_STRING (attr) == NULL)
22470 {
df5c6c50 22471 char *demangled = NULL;
53832f31 22472
73b9be8b 22473 attr = dw2_linkage_name_attr (die, cu);
53832f31
TT
22474 if (attr == NULL || DW_STRING (attr) == NULL)
22475 return NULL;
22476
df5c6c50
JK
22477 /* Avoid demangling DW_STRING (attr) the second time on a second
22478 call for the same DIE. */
22479 if (!DW_STRING_IS_CANONICAL (attr))
8de20a37 22480 demangled = gdb_demangle (DW_STRING (attr), DMGL_TYPES);
53832f31
TT
22481
22482 if (demangled)
22483 {
e6a959d6 22484 const char *base;
96408a79 22485
53832f31 22486 /* FIXME: we already did this for the partial symbol... */
34a68019 22487 DW_STRING (attr)
224c3ddb 22488 = ((const char *)
e3b94546 22489 obstack_copy0 (&objfile->per_bfd->storage_obstack,
224c3ddb 22490 demangled, strlen (demangled)));
53832f31
TT
22491 DW_STRING_IS_CANONICAL (attr) = 1;
22492 xfree (demangled);
96408a79
SA
22493
22494 /* Strip any leading namespaces/classes, keep only the base name.
22495 DW_AT_name for named DIEs does not contain the prefixes. */
22496 base = strrchr (DW_STRING (attr), ':');
22497 if (base && base > DW_STRING (attr) && base[-1] == ':')
22498 return &base[1];
22499 else
22500 return DW_STRING (attr);
53832f31
TT
22501 }
22502 }
907af001
UW
22503 break;
22504
71c25dea 22505 default:
907af001
UW
22506 break;
22507 }
22508
22509 if (!DW_STRING_IS_CANONICAL (attr))
22510 {
22511 DW_STRING (attr)
22512 = dwarf2_canonicalize_name (DW_STRING (attr), cu,
e3b94546 22513 &objfile->per_bfd->storage_obstack);
907af001 22514 DW_STRING_IS_CANONICAL (attr) = 1;
71c25dea 22515 }
907af001 22516 return DW_STRING (attr);
9219021c
DC
22517}
22518
22519/* Return the die that this die in an extension of, or NULL if there
f2f0e013
DJ
22520 is none. *EXT_CU is the CU containing DIE on input, and the CU
22521 containing the return value on output. */
9219021c
DC
22522
22523static struct die_info *
f2f0e013 22524dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
9219021c
DC
22525{
22526 struct attribute *attr;
9219021c 22527
f2f0e013 22528 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
9219021c
DC
22529 if (attr == NULL)
22530 return NULL;
22531
f2f0e013 22532 return follow_die_ref (die, attr, ext_cu);
9219021c
DC
22533}
22534
c906108c
SS
22535/* Convert a DIE tag into its string name. */
22536
f39c6ffd 22537static const char *
aa1ee363 22538dwarf_tag_name (unsigned tag)
c906108c 22539{
f39c6ffd
TT
22540 const char *name = get_DW_TAG_name (tag);
22541
22542 if (name == NULL)
22543 return "DW_TAG_<unknown>";
22544
22545 return name;
c906108c
SS
22546}
22547
22548/* Convert a DWARF attribute code into its string name. */
22549
f39c6ffd 22550static const char *
aa1ee363 22551dwarf_attr_name (unsigned attr)
c906108c 22552{
f39c6ffd
TT
22553 const char *name;
22554
c764a876 22555#ifdef MIPS /* collides with DW_AT_HP_block_index */
f39c6ffd
TT
22556 if (attr == DW_AT_MIPS_fde)
22557 return "DW_AT_MIPS_fde";
22558#else
22559 if (attr == DW_AT_HP_block_index)
22560 return "DW_AT_HP_block_index";
c764a876 22561#endif
f39c6ffd
TT
22562
22563 name = get_DW_AT_name (attr);
22564
22565 if (name == NULL)
22566 return "DW_AT_<unknown>";
22567
22568 return name;
c906108c
SS
22569}
22570
22571/* Convert a DWARF value form code into its string name. */
22572
f39c6ffd 22573static const char *
aa1ee363 22574dwarf_form_name (unsigned form)
c906108c 22575{
f39c6ffd
TT
22576 const char *name = get_DW_FORM_name (form);
22577
22578 if (name == NULL)
22579 return "DW_FORM_<unknown>";
22580
22581 return name;
c906108c
SS
22582}
22583
a121b7c1 22584static const char *
fba45db2 22585dwarf_bool_name (unsigned mybool)
c906108c
SS
22586{
22587 if (mybool)
22588 return "TRUE";
22589 else
22590 return "FALSE";
22591}
22592
22593/* Convert a DWARF type code into its string name. */
22594
f39c6ffd 22595static const char *
aa1ee363 22596dwarf_type_encoding_name (unsigned enc)
c906108c 22597{
f39c6ffd 22598 const char *name = get_DW_ATE_name (enc);
c906108c 22599
f39c6ffd
TT
22600 if (name == NULL)
22601 return "DW_ATE_<unknown>";
c906108c 22602
f39c6ffd 22603 return name;
c906108c 22604}
c906108c 22605
f9aca02d 22606static void
d97bc12b 22607dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
c906108c
SS
22608{
22609 unsigned int i;
22610
d97bc12b 22611 print_spaces (indent, f);
9d8780f0 22612 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset %s)\n",
9c541725 22613 dwarf_tag_name (die->tag), die->abbrev,
9d8780f0 22614 sect_offset_str (die->sect_off));
d97bc12b
DE
22615
22616 if (die->parent != NULL)
22617 {
22618 print_spaces (indent, f);
9d8780f0
SM
22619 fprintf_unfiltered (f, " parent at offset: %s\n",
22620 sect_offset_str (die->parent->sect_off));
d97bc12b
DE
22621 }
22622
22623 print_spaces (indent, f);
22624 fprintf_unfiltered (f, " has children: %s\n",
639d11d3 22625 dwarf_bool_name (die->child != NULL));
c906108c 22626
d97bc12b
DE
22627 print_spaces (indent, f);
22628 fprintf_unfiltered (f, " attributes:\n");
22629
c906108c
SS
22630 for (i = 0; i < die->num_attrs; ++i)
22631 {
d97bc12b
DE
22632 print_spaces (indent, f);
22633 fprintf_unfiltered (f, " %s (%s) ",
c906108c
SS
22634 dwarf_attr_name (die->attrs[i].name),
22635 dwarf_form_name (die->attrs[i].form));
d97bc12b 22636
c906108c
SS
22637 switch (die->attrs[i].form)
22638 {
c906108c 22639 case DW_FORM_addr:
3019eac3 22640 case DW_FORM_GNU_addr_index:
d97bc12b 22641 fprintf_unfiltered (f, "address: ");
5af949e3 22642 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
c906108c
SS
22643 break;
22644 case DW_FORM_block2:
22645 case DW_FORM_block4:
22646 case DW_FORM_block:
22647 case DW_FORM_block1:
56eb65bd
SP
22648 fprintf_unfiltered (f, "block: size %s",
22649 pulongest (DW_BLOCK (&die->attrs[i])->size));
c906108c 22650 break;
2dc7f7b3 22651 case DW_FORM_exprloc:
56eb65bd
SP
22652 fprintf_unfiltered (f, "expression: size %s",
22653 pulongest (DW_BLOCK (&die->attrs[i])->size));
2dc7f7b3 22654 break;
0224619f
JK
22655 case DW_FORM_data16:
22656 fprintf_unfiltered (f, "constant of 16 bytes");
22657 break;
4568ecf9
DE
22658 case DW_FORM_ref_addr:
22659 fprintf_unfiltered (f, "ref address: ");
22660 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
22661 break;
36586728
TT
22662 case DW_FORM_GNU_ref_alt:
22663 fprintf_unfiltered (f, "alt ref address: ");
22664 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
22665 break;
10b3939b
DJ
22666 case DW_FORM_ref1:
22667 case DW_FORM_ref2:
22668 case DW_FORM_ref4:
4568ecf9
DE
22669 case DW_FORM_ref8:
22670 case DW_FORM_ref_udata:
d97bc12b 22671 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
4568ecf9 22672 (long) (DW_UNSND (&die->attrs[i])));
10b3939b 22673 break;
c906108c
SS
22674 case DW_FORM_data1:
22675 case DW_FORM_data2:
22676 case DW_FORM_data4:
ce5d95e1 22677 case DW_FORM_data8:
c906108c
SS
22678 case DW_FORM_udata:
22679 case DW_FORM_sdata:
43bbcdc2
PH
22680 fprintf_unfiltered (f, "constant: %s",
22681 pulongest (DW_UNSND (&die->attrs[i])));
c906108c 22682 break;
2dc7f7b3
TT
22683 case DW_FORM_sec_offset:
22684 fprintf_unfiltered (f, "section offset: %s",
22685 pulongest (DW_UNSND (&die->attrs[i])));
22686 break;
55f1336d 22687 case DW_FORM_ref_sig8:
ac9ec31b
DE
22688 fprintf_unfiltered (f, "signature: %s",
22689 hex_string (DW_SIGNATURE (&die->attrs[i])));
348e048f 22690 break;
c906108c 22691 case DW_FORM_string:
4bdf3d34 22692 case DW_FORM_strp:
43988095 22693 case DW_FORM_line_strp:
3019eac3 22694 case DW_FORM_GNU_str_index:
36586728 22695 case DW_FORM_GNU_strp_alt:
8285870a 22696 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
c906108c 22697 DW_STRING (&die->attrs[i])
8285870a
JK
22698 ? DW_STRING (&die->attrs[i]) : "",
22699 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
c906108c
SS
22700 break;
22701 case DW_FORM_flag:
22702 if (DW_UNSND (&die->attrs[i]))
d97bc12b 22703 fprintf_unfiltered (f, "flag: TRUE");
c906108c 22704 else
d97bc12b 22705 fprintf_unfiltered (f, "flag: FALSE");
c906108c 22706 break;
2dc7f7b3
TT
22707 case DW_FORM_flag_present:
22708 fprintf_unfiltered (f, "flag: TRUE");
22709 break;
a8329558 22710 case DW_FORM_indirect:
0963b4bd
MS
22711 /* The reader will have reduced the indirect form to
22712 the "base form" so this form should not occur. */
3e43a32a
MS
22713 fprintf_unfiltered (f,
22714 "unexpected attribute form: DW_FORM_indirect");
a8329558 22715 break;
663c44ac
JK
22716 case DW_FORM_implicit_const:
22717 fprintf_unfiltered (f, "constant: %s",
22718 plongest (DW_SND (&die->attrs[i])));
22719 break;
c906108c 22720 default:
d97bc12b 22721 fprintf_unfiltered (f, "unsupported attribute form: %d.",
c5aa993b 22722 die->attrs[i].form);
d97bc12b 22723 break;
c906108c 22724 }
d97bc12b 22725 fprintf_unfiltered (f, "\n");
c906108c
SS
22726 }
22727}
22728
f9aca02d 22729static void
d97bc12b 22730dump_die_for_error (struct die_info *die)
c906108c 22731{
d97bc12b
DE
22732 dump_die_shallow (gdb_stderr, 0, die);
22733}
22734
22735static void
22736dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
22737{
22738 int indent = level * 4;
22739
22740 gdb_assert (die != NULL);
22741
22742 if (level >= max_level)
22743 return;
22744
22745 dump_die_shallow (f, indent, die);
22746
22747 if (die->child != NULL)
c906108c 22748 {
d97bc12b
DE
22749 print_spaces (indent, f);
22750 fprintf_unfiltered (f, " Children:");
22751 if (level + 1 < max_level)
22752 {
22753 fprintf_unfiltered (f, "\n");
22754 dump_die_1 (f, level + 1, max_level, die->child);
22755 }
22756 else
22757 {
3e43a32a
MS
22758 fprintf_unfiltered (f,
22759 " [not printed, max nesting level reached]\n");
d97bc12b
DE
22760 }
22761 }
22762
22763 if (die->sibling != NULL && level > 0)
22764 {
22765 dump_die_1 (f, level, max_level, die->sibling);
c906108c
SS
22766 }
22767}
22768
d97bc12b
DE
22769/* This is called from the pdie macro in gdbinit.in.
22770 It's not static so gcc will keep a copy callable from gdb. */
22771
22772void
22773dump_die (struct die_info *die, int max_level)
22774{
22775 dump_die_1 (gdb_stdlog, 0, max_level, die);
22776}
22777
f9aca02d 22778static void
51545339 22779store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
c906108c 22780{
51545339 22781 void **slot;
c906108c 22782
9c541725
PA
22783 slot = htab_find_slot_with_hash (cu->die_hash, die,
22784 to_underlying (die->sect_off),
b64f50a1 22785 INSERT);
51545339
DJ
22786
22787 *slot = die;
c906108c
SS
22788}
22789
b64f50a1
JK
22790/* Return DIE offset of ATTR. Return 0 with complaint if ATTR is not of the
22791 required kind. */
22792
22793static sect_offset
ff39bb5e 22794dwarf2_get_ref_die_offset (const struct attribute *attr)
93311388 22795{
7771576e 22796 if (attr_form_is_ref (attr))
9c541725 22797 return (sect_offset) DW_UNSND (attr);
93311388
DE
22798
22799 complaint (&symfile_complaints,
22800 _("unsupported die ref attribute form: '%s'"),
22801 dwarf_form_name (attr->form));
9c541725 22802 return {};
c906108c
SS
22803}
22804
43bbcdc2
PH
22805/* Return the constant value held by ATTR. Return DEFAULT_VALUE if
22806 * the value held by the attribute is not constant. */
a02abb62 22807
43bbcdc2 22808static LONGEST
ff39bb5e 22809dwarf2_get_attr_constant_value (const struct attribute *attr, int default_value)
a02abb62 22810{
663c44ac 22811 if (attr->form == DW_FORM_sdata || attr->form == DW_FORM_implicit_const)
a02abb62
JB
22812 return DW_SND (attr);
22813 else if (attr->form == DW_FORM_udata
22814 || attr->form == DW_FORM_data1
22815 || attr->form == DW_FORM_data2
22816 || attr->form == DW_FORM_data4
22817 || attr->form == DW_FORM_data8)
22818 return DW_UNSND (attr);
22819 else
22820 {
0224619f 22821 /* For DW_FORM_data16 see attr_form_is_constant. */
3e43a32a
MS
22822 complaint (&symfile_complaints,
22823 _("Attribute value is not a constant (%s)"),
a02abb62
JB
22824 dwarf_form_name (attr->form));
22825 return default_value;
22826 }
22827}
22828
348e048f
DE
22829/* Follow reference or signature attribute ATTR of SRC_DIE.
22830 On entry *REF_CU is the CU of SRC_DIE.
22831 On exit *REF_CU is the CU of the result. */
22832
22833static struct die_info *
ff39bb5e 22834follow_die_ref_or_sig (struct die_info *src_die, const struct attribute *attr,
348e048f
DE
22835 struct dwarf2_cu **ref_cu)
22836{
22837 struct die_info *die;
22838
7771576e 22839 if (attr_form_is_ref (attr))
348e048f 22840 die = follow_die_ref (src_die, attr, ref_cu);
55f1336d 22841 else if (attr->form == DW_FORM_ref_sig8)
348e048f
DE
22842 die = follow_die_sig (src_die, attr, ref_cu);
22843 else
22844 {
22845 dump_die_for_error (src_die);
22846 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
518817b3 22847 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
348e048f
DE
22848 }
22849
22850 return die;
03dd20cc
DJ
22851}
22852
5c631832 22853/* Follow reference OFFSET.
673bfd45
DE
22854 On entry *REF_CU is the CU of the source die referencing OFFSET.
22855 On exit *REF_CU is the CU of the result.
22856 Returns NULL if OFFSET is invalid. */
f504f079 22857
f9aca02d 22858static struct die_info *
9c541725 22859follow_die_offset (sect_offset sect_off, int offset_in_dwz,
36586728 22860 struct dwarf2_cu **ref_cu)
c906108c 22861{
10b3939b 22862 struct die_info temp_die;
f2f0e013 22863 struct dwarf2_cu *target_cu, *cu = *ref_cu;
518817b3
SM
22864 struct dwarf2_per_objfile *dwarf2_per_objfile
22865 = cu->per_cu->dwarf2_per_objfile;
10b3939b 22866
348e048f
DE
22867 gdb_assert (cu->per_cu != NULL);
22868
98bfdba5
PA
22869 target_cu = cu;
22870
3019eac3 22871 if (cu->per_cu->is_debug_types)
348e048f
DE
22872 {
22873 /* .debug_types CUs cannot reference anything outside their CU.
22874 If they need to, they have to reference a signatured type via
55f1336d 22875 DW_FORM_ref_sig8. */
9c541725 22876 if (!offset_in_cu_p (&cu->header, sect_off))
5c631832 22877 return NULL;
348e048f 22878 }
36586728 22879 else if (offset_in_dwz != cu->per_cu->is_dwz
9c541725 22880 || !offset_in_cu_p (&cu->header, sect_off))
10b3939b
DJ
22881 {
22882 struct dwarf2_per_cu_data *per_cu;
9a619af0 22883
9c541725 22884 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
ed2dc618 22885 dwarf2_per_objfile);
03dd20cc
DJ
22886
22887 /* If necessary, add it to the queue and load its DIEs. */
95554aad 22888 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
58f0c718 22889 load_full_comp_unit (per_cu, false, cu->language);
03dd20cc 22890
10b3939b
DJ
22891 target_cu = per_cu->cu;
22892 }
98bfdba5
PA
22893 else if (cu->dies == NULL)
22894 {
22895 /* We're loading full DIEs during partial symbol reading. */
22896 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
58f0c718 22897 load_full_comp_unit (cu->per_cu, false, language_minimal);
98bfdba5 22898 }
c906108c 22899
f2f0e013 22900 *ref_cu = target_cu;
9c541725 22901 temp_die.sect_off = sect_off;
9a3c8263 22902 return (struct die_info *) htab_find_with_hash (target_cu->die_hash,
9c541725
PA
22903 &temp_die,
22904 to_underlying (sect_off));
5c631832 22905}
10b3939b 22906
5c631832
JK
22907/* Follow reference attribute ATTR of SRC_DIE.
22908 On entry *REF_CU is the CU of SRC_DIE.
22909 On exit *REF_CU is the CU of the result. */
22910
22911static struct die_info *
ff39bb5e 22912follow_die_ref (struct die_info *src_die, const struct attribute *attr,
5c631832
JK
22913 struct dwarf2_cu **ref_cu)
22914{
9c541725 22915 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
5c631832
JK
22916 struct dwarf2_cu *cu = *ref_cu;
22917 struct die_info *die;
22918
9c541725 22919 die = follow_die_offset (sect_off,
36586728
TT
22920 (attr->form == DW_FORM_GNU_ref_alt
22921 || cu->per_cu->is_dwz),
22922 ref_cu);
5c631832 22923 if (!die)
9d8780f0
SM
22924 error (_("Dwarf Error: Cannot find DIE at %s referenced from DIE "
22925 "at %s [in module %s]"),
22926 sect_offset_str (sect_off), sect_offset_str (src_die->sect_off),
518817b3 22927 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
348e048f 22928
5c631832
JK
22929 return die;
22930}
22931
9c541725 22932/* Return DWARF block referenced by DW_AT_location of DIE at SECT_OFF at PER_CU.
d83e736b 22933 Returned value is intended for DW_OP_call*. Returned
e3b94546
SM
22934 dwarf2_locexpr_baton->data has lifetime of
22935 PER_CU->DWARF2_PER_OBJFILE->OBJFILE. */
5c631832
JK
22936
22937struct dwarf2_locexpr_baton
9c541725 22938dwarf2_fetch_die_loc_sect_off (sect_offset sect_off,
8b9737bf
TT
22939 struct dwarf2_per_cu_data *per_cu,
22940 CORE_ADDR (*get_frame_pc) (void *baton),
22941 void *baton)
5c631832 22942{
918dd910 22943 struct dwarf2_cu *cu;
5c631832
JK
22944 struct die_info *die;
22945 struct attribute *attr;
22946 struct dwarf2_locexpr_baton retval;
12359b5e
SM
22947 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
22948 struct objfile *objfile = dwarf2_per_objfile->objfile;
8cf6f0b1 22949
918dd910 22950 if (per_cu->cu == NULL)
58f0c718 22951 load_cu (per_cu, false);
918dd910 22952 cu = per_cu->cu;
cc12ce38
DE
22953 if (cu == NULL)
22954 {
22955 /* We shouldn't get here for a dummy CU, but don't crash on the user.
22956 Instead just throw an error, not much else we can do. */
9d8780f0
SM
22957 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
22958 sect_offset_str (sect_off), objfile_name (objfile));
cc12ce38 22959 }
918dd910 22960
9c541725 22961 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
5c631832 22962 if (!die)
9d8780f0
SM
22963 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
22964 sect_offset_str (sect_off), objfile_name (objfile));
5c631832
JK
22965
22966 attr = dwarf2_attr (die, DW_AT_location, cu);
22967 if (!attr)
22968 {
e103e986
JK
22969 /* DWARF: "If there is no such attribute, then there is no effect.".
22970 DATA is ignored if SIZE is 0. */
5c631832 22971
e103e986 22972 retval.data = NULL;
5c631832
JK
22973 retval.size = 0;
22974 }
8cf6f0b1
TT
22975 else if (attr_form_is_section_offset (attr))
22976 {
22977 struct dwarf2_loclist_baton loclist_baton;
22978 CORE_ADDR pc = (*get_frame_pc) (baton);
22979 size_t size;
22980
22981 fill_in_loclist_baton (cu, &loclist_baton, attr);
22982
22983 retval.data = dwarf2_find_location_expression (&loclist_baton,
22984 &size, pc);
22985 retval.size = size;
22986 }
5c631832
JK
22987 else
22988 {
22989 if (!attr_form_is_block (attr))
9d8780f0 22990 error (_("Dwarf Error: DIE at %s referenced in module %s "
5c631832 22991 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
9d8780f0 22992 sect_offset_str (sect_off), objfile_name (objfile));
5c631832
JK
22993
22994 retval.data = DW_BLOCK (attr)->data;
22995 retval.size = DW_BLOCK (attr)->size;
22996 }
22997 retval.per_cu = cu->per_cu;
918dd910 22998
ed2dc618 22999 age_cached_comp_units (dwarf2_per_objfile);
918dd910 23000
5c631832 23001 return retval;
348e048f
DE
23002}
23003
8b9737bf
TT
23004/* Like dwarf2_fetch_die_loc_sect_off, but take a CU
23005 offset. */
23006
23007struct dwarf2_locexpr_baton
23008dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
23009 struct dwarf2_per_cu_data *per_cu,
23010 CORE_ADDR (*get_frame_pc) (void *baton),
23011 void *baton)
23012{
9c541725 23013 sect_offset sect_off = per_cu->sect_off + to_underlying (offset_in_cu);
8b9737bf 23014
9c541725 23015 return dwarf2_fetch_die_loc_sect_off (sect_off, per_cu, get_frame_pc, baton);
8b9737bf
TT
23016}
23017
b6807d98
TT
23018/* Write a constant of a given type as target-ordered bytes into
23019 OBSTACK. */
23020
23021static const gdb_byte *
23022write_constant_as_bytes (struct obstack *obstack,
23023 enum bfd_endian byte_order,
23024 struct type *type,
23025 ULONGEST value,
23026 LONGEST *len)
23027{
23028 gdb_byte *result;
23029
23030 *len = TYPE_LENGTH (type);
224c3ddb 23031 result = (gdb_byte *) obstack_alloc (obstack, *len);
b6807d98
TT
23032 store_unsigned_integer (result, *len, byte_order, value);
23033
23034 return result;
23035}
23036
23037/* If the DIE at OFFSET in PER_CU has a DW_AT_const_value, return a
23038 pointer to the constant bytes and set LEN to the length of the
23039 data. If memory is needed, allocate it on OBSTACK. If the DIE
23040 does not have a DW_AT_const_value, return NULL. */
23041
23042const gdb_byte *
9c541725 23043dwarf2_fetch_constant_bytes (sect_offset sect_off,
b6807d98
TT
23044 struct dwarf2_per_cu_data *per_cu,
23045 struct obstack *obstack,
23046 LONGEST *len)
23047{
23048 struct dwarf2_cu *cu;
23049 struct die_info *die;
23050 struct attribute *attr;
23051 const gdb_byte *result = NULL;
23052 struct type *type;
23053 LONGEST value;
23054 enum bfd_endian byte_order;
e3b94546 23055 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
b6807d98 23056
b6807d98 23057 if (per_cu->cu == NULL)
58f0c718 23058 load_cu (per_cu, false);
b6807d98 23059 cu = per_cu->cu;
cc12ce38
DE
23060 if (cu == NULL)
23061 {
23062 /* We shouldn't get here for a dummy CU, but don't crash on the user.
23063 Instead just throw an error, not much else we can do. */
9d8780f0
SM
23064 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
23065 sect_offset_str (sect_off), objfile_name (objfile));
cc12ce38 23066 }
b6807d98 23067
9c541725 23068 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
b6807d98 23069 if (!die)
9d8780f0
SM
23070 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
23071 sect_offset_str (sect_off), objfile_name (objfile));
b6807d98
TT
23072
23073 attr = dwarf2_attr (die, DW_AT_const_value, cu);
23074 if (attr == NULL)
23075 return NULL;
23076
e3b94546 23077 byte_order = (bfd_big_endian (objfile->obfd)
b6807d98
TT
23078 ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
23079
23080 switch (attr->form)
23081 {
23082 case DW_FORM_addr:
23083 case DW_FORM_GNU_addr_index:
23084 {
23085 gdb_byte *tem;
23086
23087 *len = cu->header.addr_size;
224c3ddb 23088 tem = (gdb_byte *) obstack_alloc (obstack, *len);
b6807d98
TT
23089 store_unsigned_integer (tem, *len, byte_order, DW_ADDR (attr));
23090 result = tem;
23091 }
23092 break;
23093 case DW_FORM_string:
23094 case DW_FORM_strp:
23095 case DW_FORM_GNU_str_index:
23096 case DW_FORM_GNU_strp_alt:
23097 /* DW_STRING is already allocated on the objfile obstack, point
23098 directly to it. */
23099 result = (const gdb_byte *) DW_STRING (attr);
23100 *len = strlen (DW_STRING (attr));
23101 break;
23102 case DW_FORM_block1:
23103 case DW_FORM_block2:
23104 case DW_FORM_block4:
23105 case DW_FORM_block:
23106 case DW_FORM_exprloc:
0224619f 23107 case DW_FORM_data16:
b6807d98
TT
23108 result = DW_BLOCK (attr)->data;
23109 *len = DW_BLOCK (attr)->size;
23110 break;
23111
23112 /* The DW_AT_const_value attributes are supposed to carry the
23113 symbol's value "represented as it would be on the target
23114 architecture." By the time we get here, it's already been
23115 converted to host endianness, so we just need to sign- or
23116 zero-extend it as appropriate. */
23117 case DW_FORM_data1:
23118 type = die_type (die, cu);
23119 result = dwarf2_const_value_data (attr, obstack, cu, &value, 8);
23120 if (result == NULL)
23121 result = write_constant_as_bytes (obstack, byte_order,
23122 type, value, len);
23123 break;
23124 case DW_FORM_data2:
23125 type = die_type (die, cu);
23126 result = dwarf2_const_value_data (attr, obstack, cu, &value, 16);
23127 if (result == NULL)
23128 result = write_constant_as_bytes (obstack, byte_order,
23129 type, value, len);
23130 break;
23131 case DW_FORM_data4:
23132 type = die_type (die, cu);
23133 result = dwarf2_const_value_data (attr, obstack, cu, &value, 32);
23134 if (result == NULL)
23135 result = write_constant_as_bytes (obstack, byte_order,
23136 type, value, len);
23137 break;
23138 case DW_FORM_data8:
23139 type = die_type (die, cu);
23140 result = dwarf2_const_value_data (attr, obstack, cu, &value, 64);
23141 if (result == NULL)
23142 result = write_constant_as_bytes (obstack, byte_order,
23143 type, value, len);
23144 break;
23145
23146 case DW_FORM_sdata:
663c44ac 23147 case DW_FORM_implicit_const:
b6807d98
TT
23148 type = die_type (die, cu);
23149 result = write_constant_as_bytes (obstack, byte_order,
23150 type, DW_SND (attr), len);
23151 break;
23152
23153 case DW_FORM_udata:
23154 type = die_type (die, cu);
23155 result = write_constant_as_bytes (obstack, byte_order,
23156 type, DW_UNSND (attr), len);
23157 break;
23158
23159 default:
23160 complaint (&symfile_complaints,
23161 _("unsupported const value attribute form: '%s'"),
23162 dwarf_form_name (attr->form));
23163 break;
23164 }
23165
23166 return result;
23167}
23168
7942e96e
AA
23169/* Return the type of the die at OFFSET in PER_CU. Return NULL if no
23170 valid type for this die is found. */
23171
23172struct type *
9c541725 23173dwarf2_fetch_die_type_sect_off (sect_offset sect_off,
7942e96e
AA
23174 struct dwarf2_per_cu_data *per_cu)
23175{
23176 struct dwarf2_cu *cu;
23177 struct die_info *die;
23178
7942e96e 23179 if (per_cu->cu == NULL)
58f0c718 23180 load_cu (per_cu, false);
7942e96e
AA
23181 cu = per_cu->cu;
23182 if (!cu)
23183 return NULL;
23184
9c541725 23185 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
7942e96e
AA
23186 if (!die)
23187 return NULL;
23188
23189 return die_type (die, cu);
23190}
23191
8a9b8146
TT
23192/* Return the type of the DIE at DIE_OFFSET in the CU named by
23193 PER_CU. */
23194
23195struct type *
b64f50a1 23196dwarf2_get_die_type (cu_offset die_offset,
8a9b8146
TT
23197 struct dwarf2_per_cu_data *per_cu)
23198{
9c541725 23199 sect_offset die_offset_sect = per_cu->sect_off + to_underlying (die_offset);
b64f50a1 23200 return get_die_type_at_offset (die_offset_sect, per_cu);
8a9b8146
TT
23201}
23202
ac9ec31b 23203/* Follow type unit SIG_TYPE referenced by SRC_DIE.
348e048f 23204 On entry *REF_CU is the CU of SRC_DIE.
ac9ec31b
DE
23205 On exit *REF_CU is the CU of the result.
23206 Returns NULL if the referenced DIE isn't found. */
348e048f
DE
23207
23208static struct die_info *
ac9ec31b
DE
23209follow_die_sig_1 (struct die_info *src_die, struct signatured_type *sig_type,
23210 struct dwarf2_cu **ref_cu)
348e048f 23211{
348e048f 23212 struct die_info temp_die;
348e048f
DE
23213 struct dwarf2_cu *sig_cu;
23214 struct die_info *die;
23215
ac9ec31b
DE
23216 /* While it might be nice to assert sig_type->type == NULL here,
23217 we can get here for DW_AT_imported_declaration where we need
23218 the DIE not the type. */
348e048f
DE
23219
23220 /* If necessary, add it to the queue and load its DIEs. */
23221
95554aad 23222 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, language_minimal))
a0f42c21 23223 read_signatured_type (sig_type);
348e048f 23224
348e048f 23225 sig_cu = sig_type->per_cu.cu;
69d751e3 23226 gdb_assert (sig_cu != NULL);
9c541725
PA
23227 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
23228 temp_die.sect_off = sig_type->type_offset_in_section;
9a3c8263 23229 die = (struct die_info *) htab_find_with_hash (sig_cu->die_hash, &temp_die,
9c541725 23230 to_underlying (temp_die.sect_off));
348e048f
DE
23231 if (die)
23232 {
ed2dc618 23233 struct dwarf2_per_objfile *dwarf2_per_objfile
518817b3 23234 = (*ref_cu)->per_cu->dwarf2_per_objfile;
ed2dc618 23235
796a7ff8
DE
23236 /* For .gdb_index version 7 keep track of included TUs.
23237 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
23238 if (dwarf2_per_objfile->index_table != NULL
23239 && dwarf2_per_objfile->index_table->version <= 7)
23240 {
23241 VEC_safe_push (dwarf2_per_cu_ptr,
23242 (*ref_cu)->per_cu->imported_symtabs,
23243 sig_cu->per_cu);
23244 }
23245
348e048f
DE
23246 *ref_cu = sig_cu;
23247 return die;
23248 }
23249
ac9ec31b
DE
23250 return NULL;
23251}
23252
23253/* Follow signatured type referenced by ATTR in SRC_DIE.
23254 On entry *REF_CU is the CU of SRC_DIE.
23255 On exit *REF_CU is the CU of the result.
23256 The result is the DIE of the type.
23257 If the referenced type cannot be found an error is thrown. */
23258
23259static struct die_info *
ff39bb5e 23260follow_die_sig (struct die_info *src_die, const struct attribute *attr,
ac9ec31b
DE
23261 struct dwarf2_cu **ref_cu)
23262{
23263 ULONGEST signature = DW_SIGNATURE (attr);
23264 struct signatured_type *sig_type;
23265 struct die_info *die;
23266
23267 gdb_assert (attr->form == DW_FORM_ref_sig8);
23268
a2ce51a0 23269 sig_type = lookup_signatured_type (*ref_cu, signature);
ac9ec31b
DE
23270 /* sig_type will be NULL if the signatured type is missing from
23271 the debug info. */
23272 if (sig_type == NULL)
23273 {
23274 error (_("Dwarf Error: Cannot find signatured DIE %s referenced"
9d8780f0
SM
23275 " from DIE at %s [in module %s]"),
23276 hex_string (signature), sect_offset_str (src_die->sect_off),
518817b3 23277 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
ac9ec31b
DE
23278 }
23279
23280 die = follow_die_sig_1 (src_die, sig_type, ref_cu);
23281 if (die == NULL)
23282 {
23283 dump_die_for_error (src_die);
23284 error (_("Dwarf Error: Problem reading signatured DIE %s referenced"
9d8780f0
SM
23285 " from DIE at %s [in module %s]"),
23286 hex_string (signature), sect_offset_str (src_die->sect_off),
518817b3 23287 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
ac9ec31b
DE
23288 }
23289
23290 return die;
23291}
23292
23293/* Get the type specified by SIGNATURE referenced in DIE/CU,
23294 reading in and processing the type unit if necessary. */
23295
23296static struct type *
23297get_signatured_type (struct die_info *die, ULONGEST signature,
23298 struct dwarf2_cu *cu)
23299{
518817b3
SM
23300 struct dwarf2_per_objfile *dwarf2_per_objfile
23301 = cu->per_cu->dwarf2_per_objfile;
ac9ec31b
DE
23302 struct signatured_type *sig_type;
23303 struct dwarf2_cu *type_cu;
23304 struct die_info *type_die;
23305 struct type *type;
23306
a2ce51a0 23307 sig_type = lookup_signatured_type (cu, signature);
ac9ec31b
DE
23308 /* sig_type will be NULL if the signatured type is missing from
23309 the debug info. */
23310 if (sig_type == NULL)
23311 {
23312 complaint (&symfile_complaints,
23313 _("Dwarf Error: Cannot find signatured DIE %s referenced"
9d8780f0
SM
23314 " from DIE at %s [in module %s]"),
23315 hex_string (signature), sect_offset_str (die->sect_off),
4262abfb 23316 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
23317 return build_error_marker_type (cu, die);
23318 }
23319
23320 /* If we already know the type we're done. */
23321 if (sig_type->type != NULL)
23322 return sig_type->type;
23323
23324 type_cu = cu;
23325 type_die = follow_die_sig_1 (die, sig_type, &type_cu);
23326 if (type_die != NULL)
23327 {
23328 /* N.B. We need to call get_die_type to ensure only one type for this DIE
23329 is created. This is important, for example, because for c++ classes
23330 we need TYPE_NAME set which is only done by new_symbol. Blech. */
23331 type = read_type_die (type_die, type_cu);
23332 if (type == NULL)
23333 {
23334 complaint (&symfile_complaints,
23335 _("Dwarf Error: Cannot build signatured type %s"
9d8780f0
SM
23336 " referenced from DIE at %s [in module %s]"),
23337 hex_string (signature), sect_offset_str (die->sect_off),
4262abfb 23338 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
23339 type = build_error_marker_type (cu, die);
23340 }
23341 }
23342 else
23343 {
23344 complaint (&symfile_complaints,
23345 _("Dwarf Error: Problem reading signatured DIE %s referenced"
9d8780f0
SM
23346 " from DIE at %s [in module %s]"),
23347 hex_string (signature), sect_offset_str (die->sect_off),
4262abfb 23348 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
23349 type = build_error_marker_type (cu, die);
23350 }
23351 sig_type->type = type;
23352
23353 return type;
23354}
23355
23356/* Get the type specified by the DW_AT_signature ATTR in DIE/CU,
23357 reading in and processing the type unit if necessary. */
23358
23359static struct type *
ff39bb5e 23360get_DW_AT_signature_type (struct die_info *die, const struct attribute *attr,
b385a60d 23361 struct dwarf2_cu *cu) /* ARI: editCase function */
ac9ec31b
DE
23362{
23363 /* Yes, DW_AT_signature can use a non-ref_sig8 reference. */
7771576e 23364 if (attr_form_is_ref (attr))
ac9ec31b
DE
23365 {
23366 struct dwarf2_cu *type_cu = cu;
23367 struct die_info *type_die = follow_die_ref (die, attr, &type_cu);
23368
23369 return read_type_die (type_die, type_cu);
23370 }
23371 else if (attr->form == DW_FORM_ref_sig8)
23372 {
23373 return get_signatured_type (die, DW_SIGNATURE (attr), cu);
23374 }
23375 else
23376 {
518817b3
SM
23377 struct dwarf2_per_objfile *dwarf2_per_objfile
23378 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 23379
ac9ec31b
DE
23380 complaint (&symfile_complaints,
23381 _("Dwarf Error: DW_AT_signature has bad form %s in DIE"
9d8780f0
SM
23382 " at %s [in module %s]"),
23383 dwarf_form_name (attr->form), sect_offset_str (die->sect_off),
4262abfb 23384 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
23385 return build_error_marker_type (cu, die);
23386 }
348e048f
DE
23387}
23388
e5fe5e75 23389/* Load the DIEs associated with type unit PER_CU into memory. */
348e048f
DE
23390
23391static void
e5fe5e75 23392load_full_type_unit (struct dwarf2_per_cu_data *per_cu)
348e048f 23393{
52dc124a 23394 struct signatured_type *sig_type;
348e048f 23395
f4dc4d17
DE
23396 /* Caller is responsible for ensuring type_unit_groups don't get here. */
23397 gdb_assert (! IS_TYPE_UNIT_GROUP (per_cu));
23398
6721b2ec
DE
23399 /* We have the per_cu, but we need the signatured_type.
23400 Fortunately this is an easy translation. */
23401 gdb_assert (per_cu->is_debug_types);
23402 sig_type = (struct signatured_type *) per_cu;
348e048f 23403
6721b2ec 23404 gdb_assert (per_cu->cu == NULL);
348e048f 23405
52dc124a 23406 read_signatured_type (sig_type);
348e048f 23407
6721b2ec 23408 gdb_assert (per_cu->cu != NULL);
348e048f
DE
23409}
23410
dee91e82
DE
23411/* die_reader_func for read_signatured_type.
23412 This is identical to load_full_comp_unit_reader,
23413 but is kept separate for now. */
348e048f
DE
23414
23415static void
dee91e82 23416read_signatured_type_reader (const struct die_reader_specs *reader,
d521ce57 23417 const gdb_byte *info_ptr,
dee91e82
DE
23418 struct die_info *comp_unit_die,
23419 int has_children,
23420 void *data)
348e048f 23421{
dee91e82 23422 struct dwarf2_cu *cu = reader->cu;
348e048f 23423
dee91e82
DE
23424 gdb_assert (cu->die_hash == NULL);
23425 cu->die_hash =
23426 htab_create_alloc_ex (cu->header.length / 12,
23427 die_hash,
23428 die_eq,
23429 NULL,
23430 &cu->comp_unit_obstack,
23431 hashtab_obstack_allocate,
23432 dummy_obstack_deallocate);
348e048f 23433
dee91e82
DE
23434 if (has_children)
23435 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
23436 &info_ptr, comp_unit_die);
23437 cu->dies = comp_unit_die;
23438 /* comp_unit_die is not stored in die_hash, no need. */
348e048f
DE
23439
23440 /* We try not to read any attributes in this function, because not
9cdd5dbd 23441 all CUs needed for references have been loaded yet, and symbol
348e048f 23442 table processing isn't initialized. But we have to set the CU language,
dee91e82
DE
23443 or we won't be able to build types correctly.
23444 Similarly, if we do not read the producer, we can not apply
23445 producer-specific interpretation. */
95554aad 23446 prepare_one_comp_unit (cu, cu->dies, language_minimal);
dee91e82 23447}
348e048f 23448
3019eac3
DE
23449/* Read in a signatured type and build its CU and DIEs.
23450 If the type is a stub for the real type in a DWO file,
23451 read in the real type from the DWO file as well. */
dee91e82
DE
23452
23453static void
23454read_signatured_type (struct signatured_type *sig_type)
23455{
23456 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
348e048f 23457
3019eac3 23458 gdb_assert (per_cu->is_debug_types);
dee91e82 23459 gdb_assert (per_cu->cu == NULL);
348e048f 23460
58f0c718 23461 init_cutu_and_read_dies (per_cu, NULL, 0, 1, false,
f4dc4d17 23462 read_signatured_type_reader, NULL);
7ee85ab1 23463 sig_type->per_cu.tu_read = 1;
c906108c
SS
23464}
23465
c906108c
SS
23466/* Decode simple location descriptions.
23467 Given a pointer to a dwarf block that defines a location, compute
23468 the location and return the value.
23469
4cecd739
DJ
23470 NOTE drow/2003-11-18: This function is called in two situations
23471 now: for the address of static or global variables (partial symbols
23472 only) and for offsets into structures which are expected to be
23473 (more or less) constant. The partial symbol case should go away,
23474 and only the constant case should remain. That will let this
23475 function complain more accurately. A few special modes are allowed
23476 without complaint for global variables (for instance, global
23477 register values and thread-local values).
c906108c
SS
23478
23479 A location description containing no operations indicates that the
4cecd739 23480 object is optimized out. The return value is 0 for that case.
6b992462
DJ
23481 FIXME drow/2003-11-16: No callers check for this case any more; soon all
23482 callers will only want a very basic result and this can become a
21ae7a4d
JK
23483 complaint.
23484
23485 Note that stack[0] is unused except as a default error return. */
c906108c
SS
23486
23487static CORE_ADDR
e7c27a73 23488decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
c906108c 23489{
518817b3 23490 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
56eb65bd
SP
23491 size_t i;
23492 size_t size = blk->size;
d521ce57 23493 const gdb_byte *data = blk->data;
21ae7a4d
JK
23494 CORE_ADDR stack[64];
23495 int stacki;
23496 unsigned int bytes_read, unsnd;
23497 gdb_byte op;
c906108c 23498
21ae7a4d
JK
23499 i = 0;
23500 stacki = 0;
23501 stack[stacki] = 0;
23502 stack[++stacki] = 0;
23503
23504 while (i < size)
23505 {
23506 op = data[i++];
23507 switch (op)
23508 {
23509 case DW_OP_lit0:
23510 case DW_OP_lit1:
23511 case DW_OP_lit2:
23512 case DW_OP_lit3:
23513 case DW_OP_lit4:
23514 case DW_OP_lit5:
23515 case DW_OP_lit6:
23516 case DW_OP_lit7:
23517 case DW_OP_lit8:
23518 case DW_OP_lit9:
23519 case DW_OP_lit10:
23520 case DW_OP_lit11:
23521 case DW_OP_lit12:
23522 case DW_OP_lit13:
23523 case DW_OP_lit14:
23524 case DW_OP_lit15:
23525 case DW_OP_lit16:
23526 case DW_OP_lit17:
23527 case DW_OP_lit18:
23528 case DW_OP_lit19:
23529 case DW_OP_lit20:
23530 case DW_OP_lit21:
23531 case DW_OP_lit22:
23532 case DW_OP_lit23:
23533 case DW_OP_lit24:
23534 case DW_OP_lit25:
23535 case DW_OP_lit26:
23536 case DW_OP_lit27:
23537 case DW_OP_lit28:
23538 case DW_OP_lit29:
23539 case DW_OP_lit30:
23540 case DW_OP_lit31:
23541 stack[++stacki] = op - DW_OP_lit0;
23542 break;
f1bea926 23543
21ae7a4d
JK
23544 case DW_OP_reg0:
23545 case DW_OP_reg1:
23546 case DW_OP_reg2:
23547 case DW_OP_reg3:
23548 case DW_OP_reg4:
23549 case DW_OP_reg5:
23550 case DW_OP_reg6:
23551 case DW_OP_reg7:
23552 case DW_OP_reg8:
23553 case DW_OP_reg9:
23554 case DW_OP_reg10:
23555 case DW_OP_reg11:
23556 case DW_OP_reg12:
23557 case DW_OP_reg13:
23558 case DW_OP_reg14:
23559 case DW_OP_reg15:
23560 case DW_OP_reg16:
23561 case DW_OP_reg17:
23562 case DW_OP_reg18:
23563 case DW_OP_reg19:
23564 case DW_OP_reg20:
23565 case DW_OP_reg21:
23566 case DW_OP_reg22:
23567 case DW_OP_reg23:
23568 case DW_OP_reg24:
23569 case DW_OP_reg25:
23570 case DW_OP_reg26:
23571 case DW_OP_reg27:
23572 case DW_OP_reg28:
23573 case DW_OP_reg29:
23574 case DW_OP_reg30:
23575 case DW_OP_reg31:
23576 stack[++stacki] = op - DW_OP_reg0;
23577 if (i < size)
23578 dwarf2_complex_location_expr_complaint ();
23579 break;
c906108c 23580
21ae7a4d
JK
23581 case DW_OP_regx:
23582 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
23583 i += bytes_read;
23584 stack[++stacki] = unsnd;
23585 if (i < size)
23586 dwarf2_complex_location_expr_complaint ();
23587 break;
c906108c 23588
21ae7a4d
JK
23589 case DW_OP_addr:
23590 stack[++stacki] = read_address (objfile->obfd, &data[i],
23591 cu, &bytes_read);
23592 i += bytes_read;
23593 break;
d53d4ac5 23594
21ae7a4d
JK
23595 case DW_OP_const1u:
23596 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
23597 i += 1;
23598 break;
23599
23600 case DW_OP_const1s:
23601 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
23602 i += 1;
23603 break;
23604
23605 case DW_OP_const2u:
23606 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
23607 i += 2;
23608 break;
23609
23610 case DW_OP_const2s:
23611 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
23612 i += 2;
23613 break;
d53d4ac5 23614
21ae7a4d
JK
23615 case DW_OP_const4u:
23616 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
23617 i += 4;
23618 break;
23619
23620 case DW_OP_const4s:
23621 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
23622 i += 4;
23623 break;
23624
585861ea
JK
23625 case DW_OP_const8u:
23626 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
23627 i += 8;
23628 break;
23629
21ae7a4d
JK
23630 case DW_OP_constu:
23631 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
23632 &bytes_read);
23633 i += bytes_read;
23634 break;
23635
23636 case DW_OP_consts:
23637 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
23638 i += bytes_read;
23639 break;
23640
23641 case DW_OP_dup:
23642 stack[stacki + 1] = stack[stacki];
23643 stacki++;
23644 break;
23645
23646 case DW_OP_plus:
23647 stack[stacki - 1] += stack[stacki];
23648 stacki--;
23649 break;
23650
23651 case DW_OP_plus_uconst:
23652 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
23653 &bytes_read);
23654 i += bytes_read;
23655 break;
23656
23657 case DW_OP_minus:
23658 stack[stacki - 1] -= stack[stacki];
23659 stacki--;
23660 break;
23661
23662 case DW_OP_deref:
23663 /* If we're not the last op, then we definitely can't encode
23664 this using GDB's address_class enum. This is valid for partial
23665 global symbols, although the variable's address will be bogus
23666 in the psymtab. */
23667 if (i < size)
23668 dwarf2_complex_location_expr_complaint ();
23669 break;
23670
23671 case DW_OP_GNU_push_tls_address:
4aa4e28b 23672 case DW_OP_form_tls_address:
21ae7a4d
JK
23673 /* The top of the stack has the offset from the beginning
23674 of the thread control block at which the variable is located. */
23675 /* Nothing should follow this operator, so the top of stack would
23676 be returned. */
23677 /* This is valid for partial global symbols, but the variable's
585861ea
JK
23678 address will be bogus in the psymtab. Make it always at least
23679 non-zero to not look as a variable garbage collected by linker
23680 which have DW_OP_addr 0. */
21ae7a4d
JK
23681 if (i < size)
23682 dwarf2_complex_location_expr_complaint ();
585861ea 23683 stack[stacki]++;
21ae7a4d
JK
23684 break;
23685
23686 case DW_OP_GNU_uninit:
23687 break;
23688
3019eac3 23689 case DW_OP_GNU_addr_index:
49f6c839 23690 case DW_OP_GNU_const_index:
3019eac3
DE
23691 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
23692 &bytes_read);
23693 i += bytes_read;
23694 break;
23695
21ae7a4d
JK
23696 default:
23697 {
f39c6ffd 23698 const char *name = get_DW_OP_name (op);
21ae7a4d
JK
23699
23700 if (name)
23701 complaint (&symfile_complaints, _("unsupported stack op: '%s'"),
23702 name);
23703 else
23704 complaint (&symfile_complaints, _("unsupported stack op: '%02x'"),
23705 op);
23706 }
23707
23708 return (stack[stacki]);
d53d4ac5 23709 }
3c6e0cb3 23710
21ae7a4d
JK
23711 /* Enforce maximum stack depth of SIZE-1 to avoid writing
23712 outside of the allocated space. Also enforce minimum>0. */
23713 if (stacki >= ARRAY_SIZE (stack) - 1)
23714 {
23715 complaint (&symfile_complaints,
23716 _("location description stack overflow"));
23717 return 0;
23718 }
23719
23720 if (stacki <= 0)
23721 {
23722 complaint (&symfile_complaints,
23723 _("location description stack underflow"));
23724 return 0;
23725 }
23726 }
23727 return (stack[stacki]);
c906108c
SS
23728}
23729
23730/* memory allocation interface */
23731
c906108c 23732static struct dwarf_block *
7b5a2f43 23733dwarf_alloc_block (struct dwarf2_cu *cu)
c906108c 23734{
8d749320 23735 return XOBNEW (&cu->comp_unit_obstack, struct dwarf_block);
c906108c
SS
23736}
23737
c906108c 23738static struct die_info *
b60c80d6 23739dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
c906108c
SS
23740{
23741 struct die_info *die;
b60c80d6
DJ
23742 size_t size = sizeof (struct die_info);
23743
23744 if (num_attrs > 1)
23745 size += (num_attrs - 1) * sizeof (struct attribute);
c906108c 23746
b60c80d6 23747 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
c906108c
SS
23748 memset (die, 0, sizeof (struct die_info));
23749 return (die);
23750}
2e276125
JB
23751
23752\f
23753/* Macro support. */
23754
233d95b5
JK
23755/* Return file name relative to the compilation directory of file number I in
23756 *LH's file name table. The result is allocated using xmalloc; the caller is
2e276125 23757 responsible for freeing it. */
233d95b5 23758
2e276125 23759static char *
233d95b5 23760file_file_name (int file, struct line_header *lh)
2e276125 23761{
6a83a1e6
EZ
23762 /* Is the file number a valid index into the line header's file name
23763 table? Remember that file numbers start with one, not zero. */
fff8551c 23764 if (1 <= file && file <= lh->file_names.size ())
6a83a1e6 23765 {
8c43009f 23766 const file_entry &fe = lh->file_names[file - 1];
6e70227d 23767
8c43009f
PA
23768 if (!IS_ABSOLUTE_PATH (fe.name))
23769 {
23770 const char *dir = fe.include_dir (lh);
23771 if (dir != NULL)
23772 return concat (dir, SLASH_STRING, fe.name, (char *) NULL);
23773 }
23774 return xstrdup (fe.name);
6a83a1e6 23775 }
2e276125
JB
23776 else
23777 {
6a83a1e6
EZ
23778 /* The compiler produced a bogus file number. We can at least
23779 record the macro definitions made in the file, even if we
23780 won't be able to find the file by name. */
23781 char fake_name[80];
9a619af0 23782
8c042590
PM
23783 xsnprintf (fake_name, sizeof (fake_name),
23784 "<bad macro file number %d>", file);
2e276125 23785
6e70227d 23786 complaint (&symfile_complaints,
6a83a1e6
EZ
23787 _("bad file number in macro information (%d)"),
23788 file);
2e276125 23789
6a83a1e6 23790 return xstrdup (fake_name);
2e276125
JB
23791 }
23792}
23793
233d95b5
JK
23794/* Return the full name of file number I in *LH's file name table.
23795 Use COMP_DIR as the name of the current directory of the
23796 compilation. The result is allocated using xmalloc; the caller is
23797 responsible for freeing it. */
23798static char *
23799file_full_name (int file, struct line_header *lh, const char *comp_dir)
23800{
23801 /* Is the file number a valid index into the line header's file name
23802 table? Remember that file numbers start with one, not zero. */
fff8551c 23803 if (1 <= file && file <= lh->file_names.size ())
233d95b5
JK
23804 {
23805 char *relative = file_file_name (file, lh);
23806
23807 if (IS_ABSOLUTE_PATH (relative) || comp_dir == NULL)
23808 return relative;
b36cec19
PA
23809 return reconcat (relative, comp_dir, SLASH_STRING,
23810 relative, (char *) NULL);
233d95b5
JK
23811 }
23812 else
23813 return file_file_name (file, lh);
23814}
23815
2e276125
JB
23816
23817static struct macro_source_file *
23818macro_start_file (int file, int line,
23819 struct macro_source_file *current_file,
43f3e411 23820 struct line_header *lh)
2e276125 23821{
233d95b5
JK
23822 /* File name relative to the compilation directory of this source file. */
23823 char *file_name = file_file_name (file, lh);
2e276125 23824
2e276125 23825 if (! current_file)
abc9d0dc 23826 {
fc474241
DE
23827 /* Note: We don't create a macro table for this compilation unit
23828 at all until we actually get a filename. */
43f3e411 23829 struct macro_table *macro_table = get_macro_table ();
fc474241 23830
abc9d0dc
TT
23831 /* If we have no current file, then this must be the start_file
23832 directive for the compilation unit's main source file. */
fc474241
DE
23833 current_file = macro_set_main (macro_table, file_name);
23834 macro_define_special (macro_table);
abc9d0dc 23835 }
2e276125 23836 else
233d95b5 23837 current_file = macro_include (current_file, line, file_name);
2e276125 23838
233d95b5 23839 xfree (file_name);
6e70227d 23840
2e276125
JB
23841 return current_file;
23842}
23843
2e276125
JB
23844static const char *
23845consume_improper_spaces (const char *p, const char *body)
23846{
23847 if (*p == ' ')
23848 {
4d3c2250 23849 complaint (&symfile_complaints,
3e43a32a
MS
23850 _("macro definition contains spaces "
23851 "in formal argument list:\n`%s'"),
4d3c2250 23852 body);
2e276125
JB
23853
23854 while (*p == ' ')
23855 p++;
23856 }
23857
23858 return p;
23859}
23860
23861
23862static void
23863parse_macro_definition (struct macro_source_file *file, int line,
23864 const char *body)
23865{
23866 const char *p;
23867
23868 /* The body string takes one of two forms. For object-like macro
23869 definitions, it should be:
23870
23871 <macro name> " " <definition>
23872
23873 For function-like macro definitions, it should be:
23874
23875 <macro name> "() " <definition>
23876 or
23877 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
23878
23879 Spaces may appear only where explicitly indicated, and in the
23880 <definition>.
23881
23882 The Dwarf 2 spec says that an object-like macro's name is always
23883 followed by a space, but versions of GCC around March 2002 omit
6e70227d 23884 the space when the macro's definition is the empty string.
2e276125
JB
23885
23886 The Dwarf 2 spec says that there should be no spaces between the
23887 formal arguments in a function-like macro's formal argument list,
23888 but versions of GCC around March 2002 include spaces after the
23889 commas. */
23890
23891
23892 /* Find the extent of the macro name. The macro name is terminated
23893 by either a space or null character (for an object-like macro) or
23894 an opening paren (for a function-like macro). */
23895 for (p = body; *p; p++)
23896 if (*p == ' ' || *p == '(')
23897 break;
23898
23899 if (*p == ' ' || *p == '\0')
23900 {
23901 /* It's an object-like macro. */
23902 int name_len = p - body;
3f8a7804 23903 char *name = savestring (body, name_len);
2e276125
JB
23904 const char *replacement;
23905
23906 if (*p == ' ')
23907 replacement = body + name_len + 1;
23908 else
23909 {
4d3c2250 23910 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
23911 replacement = body + name_len;
23912 }
6e70227d 23913
2e276125
JB
23914 macro_define_object (file, line, name, replacement);
23915
23916 xfree (name);
23917 }
23918 else if (*p == '(')
23919 {
23920 /* It's a function-like macro. */
3f8a7804 23921 char *name = savestring (body, p - body);
2e276125
JB
23922 int argc = 0;
23923 int argv_size = 1;
8d749320 23924 char **argv = XNEWVEC (char *, argv_size);
2e276125
JB
23925
23926 p++;
23927
23928 p = consume_improper_spaces (p, body);
23929
23930 /* Parse the formal argument list. */
23931 while (*p && *p != ')')
23932 {
23933 /* Find the extent of the current argument name. */
23934 const char *arg_start = p;
23935
23936 while (*p && *p != ',' && *p != ')' && *p != ' ')
23937 p++;
23938
23939 if (! *p || p == arg_start)
4d3c2250 23940 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
23941 else
23942 {
23943 /* Make sure argv has room for the new argument. */
23944 if (argc >= argv_size)
23945 {
23946 argv_size *= 2;
224c3ddb 23947 argv = XRESIZEVEC (char *, argv, argv_size);
2e276125
JB
23948 }
23949
3f8a7804 23950 argv[argc++] = savestring (arg_start, p - arg_start);
2e276125
JB
23951 }
23952
23953 p = consume_improper_spaces (p, body);
23954
23955 /* Consume the comma, if present. */
23956 if (*p == ',')
23957 {
23958 p++;
23959
23960 p = consume_improper_spaces (p, body);
23961 }
23962 }
23963
23964 if (*p == ')')
23965 {
23966 p++;
23967
23968 if (*p == ' ')
23969 /* Perfectly formed definition, no complaints. */
23970 macro_define_function (file, line, name,
6e70227d 23971 argc, (const char **) argv,
2e276125
JB
23972 p + 1);
23973 else if (*p == '\0')
23974 {
23975 /* Complain, but do define it. */
4d3c2250 23976 dwarf2_macro_malformed_definition_complaint (body);
2e276125 23977 macro_define_function (file, line, name,
6e70227d 23978 argc, (const char **) argv,
2e276125
JB
23979 p);
23980 }
23981 else
23982 /* Just complain. */
4d3c2250 23983 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
23984 }
23985 else
23986 /* Just complain. */
4d3c2250 23987 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
23988
23989 xfree (name);
23990 {
23991 int i;
23992
23993 for (i = 0; i < argc; i++)
23994 xfree (argv[i]);
23995 }
23996 xfree (argv);
23997 }
23998 else
4d3c2250 23999 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
24000}
24001
cf2c3c16
TT
24002/* Skip some bytes from BYTES according to the form given in FORM.
24003 Returns the new pointer. */
2e276125 24004
d521ce57
TT
24005static const gdb_byte *
24006skip_form_bytes (bfd *abfd, const gdb_byte *bytes, const gdb_byte *buffer_end,
cf2c3c16
TT
24007 enum dwarf_form form,
24008 unsigned int offset_size,
24009 struct dwarf2_section_info *section)
2e276125 24010{
cf2c3c16 24011 unsigned int bytes_read;
2e276125 24012
cf2c3c16 24013 switch (form)
2e276125 24014 {
cf2c3c16
TT
24015 case DW_FORM_data1:
24016 case DW_FORM_flag:
24017 ++bytes;
24018 break;
24019
24020 case DW_FORM_data2:
24021 bytes += 2;
24022 break;
24023
24024 case DW_FORM_data4:
24025 bytes += 4;
24026 break;
24027
24028 case DW_FORM_data8:
24029 bytes += 8;
24030 break;
24031
0224619f
JK
24032 case DW_FORM_data16:
24033 bytes += 16;
24034 break;
24035
cf2c3c16
TT
24036 case DW_FORM_string:
24037 read_direct_string (abfd, bytes, &bytes_read);
24038 bytes += bytes_read;
24039 break;
24040
24041 case DW_FORM_sec_offset:
24042 case DW_FORM_strp:
36586728 24043 case DW_FORM_GNU_strp_alt:
cf2c3c16
TT
24044 bytes += offset_size;
24045 break;
24046
24047 case DW_FORM_block:
24048 bytes += read_unsigned_leb128 (abfd, bytes, &bytes_read);
24049 bytes += bytes_read;
24050 break;
24051
24052 case DW_FORM_block1:
24053 bytes += 1 + read_1_byte (abfd, bytes);
24054 break;
24055 case DW_FORM_block2:
24056 bytes += 2 + read_2_bytes (abfd, bytes);
24057 break;
24058 case DW_FORM_block4:
24059 bytes += 4 + read_4_bytes (abfd, bytes);
24060 break;
24061
24062 case DW_FORM_sdata:
24063 case DW_FORM_udata:
3019eac3
DE
24064 case DW_FORM_GNU_addr_index:
24065 case DW_FORM_GNU_str_index:
d521ce57 24066 bytes = gdb_skip_leb128 (bytes, buffer_end);
f664829e
DE
24067 if (bytes == NULL)
24068 {
24069 dwarf2_section_buffer_overflow_complaint (section);
24070 return NULL;
24071 }
cf2c3c16
TT
24072 break;
24073
663c44ac
JK
24074 case DW_FORM_implicit_const:
24075 break;
24076
cf2c3c16
TT
24077 default:
24078 {
cf2c3c16
TT
24079 complaint (&symfile_complaints,
24080 _("invalid form 0x%x in `%s'"),
a32a8923 24081 form, get_section_name (section));
cf2c3c16
TT
24082 return NULL;
24083 }
2e276125
JB
24084 }
24085
cf2c3c16
TT
24086 return bytes;
24087}
757a13d0 24088
cf2c3c16
TT
24089/* A helper for dwarf_decode_macros that handles skipping an unknown
24090 opcode. Returns an updated pointer to the macro data buffer; or,
24091 on error, issues a complaint and returns NULL. */
757a13d0 24092
d521ce57 24093static const gdb_byte *
cf2c3c16 24094skip_unknown_opcode (unsigned int opcode,
d521ce57
TT
24095 const gdb_byte **opcode_definitions,
24096 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
cf2c3c16
TT
24097 bfd *abfd,
24098 unsigned int offset_size,
24099 struct dwarf2_section_info *section)
24100{
24101 unsigned int bytes_read, i;
24102 unsigned long arg;
d521ce57 24103 const gdb_byte *defn;
2e276125 24104
cf2c3c16 24105 if (opcode_definitions[opcode] == NULL)
2e276125 24106 {
cf2c3c16
TT
24107 complaint (&symfile_complaints,
24108 _("unrecognized DW_MACFINO opcode 0x%x"),
24109 opcode);
24110 return NULL;
24111 }
2e276125 24112
cf2c3c16
TT
24113 defn = opcode_definitions[opcode];
24114 arg = read_unsigned_leb128 (abfd, defn, &bytes_read);
24115 defn += bytes_read;
2e276125 24116
cf2c3c16
TT
24117 for (i = 0; i < arg; ++i)
24118 {
aead7601
SM
24119 mac_ptr = skip_form_bytes (abfd, mac_ptr, mac_end,
24120 (enum dwarf_form) defn[i], offset_size,
f664829e 24121 section);
cf2c3c16
TT
24122 if (mac_ptr == NULL)
24123 {
24124 /* skip_form_bytes already issued the complaint. */
24125 return NULL;
24126 }
24127 }
757a13d0 24128
cf2c3c16
TT
24129 return mac_ptr;
24130}
757a13d0 24131
cf2c3c16
TT
24132/* A helper function which parses the header of a macro section.
24133 If the macro section is the extended (for now called "GNU") type,
24134 then this updates *OFFSET_SIZE. Returns a pointer to just after
24135 the header, or issues a complaint and returns NULL on error. */
757a13d0 24136
d521ce57
TT
24137static const gdb_byte *
24138dwarf_parse_macro_header (const gdb_byte **opcode_definitions,
cf2c3c16 24139 bfd *abfd,
d521ce57 24140 const gdb_byte *mac_ptr,
cf2c3c16
TT
24141 unsigned int *offset_size,
24142 int section_is_gnu)
24143{
24144 memset (opcode_definitions, 0, 256 * sizeof (gdb_byte *));
757a13d0 24145
cf2c3c16
TT
24146 if (section_is_gnu)
24147 {
24148 unsigned int version, flags;
757a13d0 24149
cf2c3c16 24150 version = read_2_bytes (abfd, mac_ptr);
0af92d60 24151 if (version != 4 && version != 5)
cf2c3c16
TT
24152 {
24153 complaint (&symfile_complaints,
24154 _("unrecognized version `%d' in .debug_macro section"),
24155 version);
24156 return NULL;
24157 }
24158 mac_ptr += 2;
757a13d0 24159
cf2c3c16
TT
24160 flags = read_1_byte (abfd, mac_ptr);
24161 ++mac_ptr;
24162 *offset_size = (flags & 1) ? 8 : 4;
757a13d0 24163
cf2c3c16
TT
24164 if ((flags & 2) != 0)
24165 /* We don't need the line table offset. */
24166 mac_ptr += *offset_size;
757a13d0 24167
cf2c3c16
TT
24168 /* Vendor opcode descriptions. */
24169 if ((flags & 4) != 0)
24170 {
24171 unsigned int i, count;
757a13d0 24172
cf2c3c16
TT
24173 count = read_1_byte (abfd, mac_ptr);
24174 ++mac_ptr;
24175 for (i = 0; i < count; ++i)
24176 {
24177 unsigned int opcode, bytes_read;
24178 unsigned long arg;
24179
24180 opcode = read_1_byte (abfd, mac_ptr);
24181 ++mac_ptr;
24182 opcode_definitions[opcode] = mac_ptr;
24183 arg = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24184 mac_ptr += bytes_read;
24185 mac_ptr += arg;
24186 }
757a13d0 24187 }
cf2c3c16 24188 }
757a13d0 24189
cf2c3c16
TT
24190 return mac_ptr;
24191}
757a13d0 24192
cf2c3c16 24193/* A helper for dwarf_decode_macros that handles the GNU extensions,
0af92d60 24194 including DW_MACRO_import. */
cf2c3c16
TT
24195
24196static void
ed2dc618
SM
24197dwarf_decode_macro_bytes (struct dwarf2_per_objfile *dwarf2_per_objfile,
24198 bfd *abfd,
d521ce57 24199 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
cf2c3c16 24200 struct macro_source_file *current_file,
43f3e411 24201 struct line_header *lh,
cf2c3c16 24202 struct dwarf2_section_info *section,
36586728 24203 int section_is_gnu, int section_is_dwz,
cf2c3c16 24204 unsigned int offset_size,
8fc3fc34 24205 htab_t include_hash)
cf2c3c16 24206{
4d663531 24207 struct objfile *objfile = dwarf2_per_objfile->objfile;
cf2c3c16
TT
24208 enum dwarf_macro_record_type macinfo_type;
24209 int at_commandline;
d521ce57 24210 const gdb_byte *opcode_definitions[256];
757a13d0 24211
cf2c3c16
TT
24212 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
24213 &offset_size, section_is_gnu);
24214 if (mac_ptr == NULL)
24215 {
24216 /* We already issued a complaint. */
24217 return;
24218 }
757a13d0
JK
24219
24220 /* Determines if GDB is still before first DW_MACINFO_start_file. If true
24221 GDB is still reading the definitions from command line. First
24222 DW_MACINFO_start_file will need to be ignored as it was already executed
24223 to create CURRENT_FILE for the main source holding also the command line
24224 definitions. On first met DW_MACINFO_start_file this flag is reset to
24225 normally execute all the remaining DW_MACINFO_start_file macinfos. */
24226
24227 at_commandline = 1;
24228
24229 do
24230 {
24231 /* Do we at least have room for a macinfo type byte? */
24232 if (mac_ptr >= mac_end)
24233 {
f664829e 24234 dwarf2_section_buffer_overflow_complaint (section);
757a13d0
JK
24235 break;
24236 }
24237
aead7601 24238 macinfo_type = (enum dwarf_macro_record_type) read_1_byte (abfd, mac_ptr);
757a13d0
JK
24239 mac_ptr++;
24240
cf2c3c16
TT
24241 /* Note that we rely on the fact that the corresponding GNU and
24242 DWARF constants are the same. */
132448f8
SM
24243 DIAGNOSTIC_PUSH
24244 DIAGNOSTIC_IGNORE_SWITCH_DIFFERENT_ENUM_TYPES
757a13d0
JK
24245 switch (macinfo_type)
24246 {
24247 /* A zero macinfo type indicates the end of the macro
24248 information. */
24249 case 0:
24250 break;
2e276125 24251
0af92d60
JK
24252 case DW_MACRO_define:
24253 case DW_MACRO_undef:
24254 case DW_MACRO_define_strp:
24255 case DW_MACRO_undef_strp:
24256 case DW_MACRO_define_sup:
24257 case DW_MACRO_undef_sup:
2e276125 24258 {
891d2f0b 24259 unsigned int bytes_read;
2e276125 24260 int line;
d521ce57 24261 const char *body;
cf2c3c16 24262 int is_define;
2e276125 24263
cf2c3c16
TT
24264 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24265 mac_ptr += bytes_read;
24266
0af92d60
JK
24267 if (macinfo_type == DW_MACRO_define
24268 || macinfo_type == DW_MACRO_undef)
cf2c3c16
TT
24269 {
24270 body = read_direct_string (abfd, mac_ptr, &bytes_read);
24271 mac_ptr += bytes_read;
24272 }
24273 else
24274 {
24275 LONGEST str_offset;
24276
24277 str_offset = read_offset_1 (abfd, mac_ptr, offset_size);
24278 mac_ptr += offset_size;
2e276125 24279
0af92d60
JK
24280 if (macinfo_type == DW_MACRO_define_sup
24281 || macinfo_type == DW_MACRO_undef_sup
f7a35f02 24282 || section_is_dwz)
36586728 24283 {
ed2dc618
SM
24284 struct dwz_file *dwz
24285 = dwarf2_get_dwz_file (dwarf2_per_objfile);
36586728 24286
ed2dc618
SM
24287 body = read_indirect_string_from_dwz (objfile,
24288 dwz, str_offset);
36586728
TT
24289 }
24290 else
ed2dc618
SM
24291 body = read_indirect_string_at_offset (dwarf2_per_objfile,
24292 abfd, str_offset);
cf2c3c16
TT
24293 }
24294
0af92d60
JK
24295 is_define = (macinfo_type == DW_MACRO_define
24296 || macinfo_type == DW_MACRO_define_strp
24297 || macinfo_type == DW_MACRO_define_sup);
2e276125 24298 if (! current_file)
757a13d0
JK
24299 {
24300 /* DWARF violation as no main source is present. */
24301 complaint (&symfile_complaints,
24302 _("debug info with no main source gives macro %s "
24303 "on line %d: %s"),
cf2c3c16
TT
24304 is_define ? _("definition") : _("undefinition"),
24305 line, body);
757a13d0
JK
24306 break;
24307 }
3e43a32a
MS
24308 if ((line == 0 && !at_commandline)
24309 || (line != 0 && at_commandline))
4d3c2250 24310 complaint (&symfile_complaints,
757a13d0
JK
24311 _("debug info gives %s macro %s with %s line %d: %s"),
24312 at_commandline ? _("command-line") : _("in-file"),
cf2c3c16 24313 is_define ? _("definition") : _("undefinition"),
757a13d0
JK
24314 line == 0 ? _("zero") : _("non-zero"), line, body);
24315
cf2c3c16 24316 if (is_define)
757a13d0 24317 parse_macro_definition (current_file, line, body);
cf2c3c16
TT
24318 else
24319 {
0af92d60
JK
24320 gdb_assert (macinfo_type == DW_MACRO_undef
24321 || macinfo_type == DW_MACRO_undef_strp
24322 || macinfo_type == DW_MACRO_undef_sup);
cf2c3c16
TT
24323 macro_undef (current_file, line, body);
24324 }
2e276125
JB
24325 }
24326 break;
24327
0af92d60 24328 case DW_MACRO_start_file:
2e276125 24329 {
891d2f0b 24330 unsigned int bytes_read;
2e276125
JB
24331 int line, file;
24332
24333 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24334 mac_ptr += bytes_read;
24335 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24336 mac_ptr += bytes_read;
24337
3e43a32a
MS
24338 if ((line == 0 && !at_commandline)
24339 || (line != 0 && at_commandline))
757a13d0
JK
24340 complaint (&symfile_complaints,
24341 _("debug info gives source %d included "
24342 "from %s at %s line %d"),
24343 file, at_commandline ? _("command-line") : _("file"),
24344 line == 0 ? _("zero") : _("non-zero"), line);
24345
24346 if (at_commandline)
24347 {
0af92d60 24348 /* This DW_MACRO_start_file was executed in the
cf2c3c16 24349 pass one. */
757a13d0
JK
24350 at_commandline = 0;
24351 }
24352 else
43f3e411 24353 current_file = macro_start_file (file, line, current_file, lh);
2e276125
JB
24354 }
24355 break;
24356
0af92d60 24357 case DW_MACRO_end_file:
2e276125 24358 if (! current_file)
4d3c2250 24359 complaint (&symfile_complaints,
3e43a32a
MS
24360 _("macro debug info has an unmatched "
24361 "`close_file' directive"));
2e276125
JB
24362 else
24363 {
24364 current_file = current_file->included_by;
24365 if (! current_file)
24366 {
cf2c3c16 24367 enum dwarf_macro_record_type next_type;
2e276125
JB
24368
24369 /* GCC circa March 2002 doesn't produce the zero
24370 type byte marking the end of the compilation
24371 unit. Complain if it's not there, but exit no
24372 matter what. */
24373
24374 /* Do we at least have room for a macinfo type byte? */
24375 if (mac_ptr >= mac_end)
24376 {
f664829e 24377 dwarf2_section_buffer_overflow_complaint (section);
2e276125
JB
24378 return;
24379 }
24380
24381 /* We don't increment mac_ptr here, so this is just
24382 a look-ahead. */
aead7601
SM
24383 next_type
24384 = (enum dwarf_macro_record_type) read_1_byte (abfd,
24385 mac_ptr);
2e276125 24386 if (next_type != 0)
4d3c2250 24387 complaint (&symfile_complaints,
3e43a32a
MS
24388 _("no terminating 0-type entry for "
24389 "macros in `.debug_macinfo' section"));
2e276125
JB
24390
24391 return;
24392 }
24393 }
24394 break;
24395
0af92d60
JK
24396 case DW_MACRO_import:
24397 case DW_MACRO_import_sup:
cf2c3c16
TT
24398 {
24399 LONGEST offset;
8fc3fc34 24400 void **slot;
a036ba48
TT
24401 bfd *include_bfd = abfd;
24402 struct dwarf2_section_info *include_section = section;
d521ce57 24403 const gdb_byte *include_mac_end = mac_end;
a036ba48 24404 int is_dwz = section_is_dwz;
d521ce57 24405 const gdb_byte *new_mac_ptr;
cf2c3c16
TT
24406
24407 offset = read_offset_1 (abfd, mac_ptr, offset_size);
24408 mac_ptr += offset_size;
24409
0af92d60 24410 if (macinfo_type == DW_MACRO_import_sup)
a036ba48 24411 {
ed2dc618 24412 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
a036ba48 24413
4d663531 24414 dwarf2_read_section (objfile, &dwz->macro);
a036ba48 24415
a036ba48 24416 include_section = &dwz->macro;
a32a8923 24417 include_bfd = get_section_bfd_owner (include_section);
a036ba48
TT
24418 include_mac_end = dwz->macro.buffer + dwz->macro.size;
24419 is_dwz = 1;
24420 }
24421
24422 new_mac_ptr = include_section->buffer + offset;
24423 slot = htab_find_slot (include_hash, new_mac_ptr, INSERT);
24424
8fc3fc34
TT
24425 if (*slot != NULL)
24426 {
24427 /* This has actually happened; see
24428 http://sourceware.org/bugzilla/show_bug.cgi?id=13568. */
24429 complaint (&symfile_complaints,
0af92d60 24430 _("recursive DW_MACRO_import in "
8fc3fc34
TT
24431 ".debug_macro section"));
24432 }
24433 else
24434 {
d521ce57 24435 *slot = (void *) new_mac_ptr;
36586728 24436
ed2dc618
SM
24437 dwarf_decode_macro_bytes (dwarf2_per_objfile,
24438 include_bfd, new_mac_ptr,
43f3e411 24439 include_mac_end, current_file, lh,
36586728 24440 section, section_is_gnu, is_dwz,
4d663531 24441 offset_size, include_hash);
8fc3fc34 24442
d521ce57 24443 htab_remove_elt (include_hash, (void *) new_mac_ptr);
8fc3fc34 24444 }
cf2c3c16
TT
24445 }
24446 break;
24447
2e276125 24448 case DW_MACINFO_vendor_ext:
cf2c3c16
TT
24449 if (!section_is_gnu)
24450 {
24451 unsigned int bytes_read;
2e276125 24452
ac298888
TT
24453 /* This reads the constant, but since we don't recognize
24454 any vendor extensions, we ignore it. */
24455 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
cf2c3c16
TT
24456 mac_ptr += bytes_read;
24457 read_direct_string (abfd, mac_ptr, &bytes_read);
24458 mac_ptr += bytes_read;
2e276125 24459
cf2c3c16
TT
24460 /* We don't recognize any vendor extensions. */
24461 break;
24462 }
24463 /* FALLTHROUGH */
24464
24465 default:
24466 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
f664829e 24467 mac_ptr, mac_end, abfd, offset_size,
cf2c3c16
TT
24468 section);
24469 if (mac_ptr == NULL)
24470 return;
24471 break;
2e276125 24472 }
132448f8 24473 DIAGNOSTIC_POP
757a13d0 24474 } while (macinfo_type != 0);
2e276125 24475}
8e19ed76 24476
cf2c3c16 24477static void
09262596 24478dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
43f3e411 24479 int section_is_gnu)
cf2c3c16 24480{
518817b3
SM
24481 struct dwarf2_per_objfile *dwarf2_per_objfile
24482 = cu->per_cu->dwarf2_per_objfile;
bb5ed363 24483 struct objfile *objfile = dwarf2_per_objfile->objfile;
09262596
DE
24484 struct line_header *lh = cu->line_header;
24485 bfd *abfd;
d521ce57 24486 const gdb_byte *mac_ptr, *mac_end;
cf2c3c16
TT
24487 struct macro_source_file *current_file = 0;
24488 enum dwarf_macro_record_type macinfo_type;
24489 unsigned int offset_size = cu->header.offset_size;
d521ce57 24490 const gdb_byte *opcode_definitions[256];
8fc3fc34 24491 void **slot;
09262596
DE
24492 struct dwarf2_section_info *section;
24493 const char *section_name;
24494
24495 if (cu->dwo_unit != NULL)
24496 {
24497 if (section_is_gnu)
24498 {
24499 section = &cu->dwo_unit->dwo_file->sections.macro;
24500 section_name = ".debug_macro.dwo";
24501 }
24502 else
24503 {
24504 section = &cu->dwo_unit->dwo_file->sections.macinfo;
24505 section_name = ".debug_macinfo.dwo";
24506 }
24507 }
24508 else
24509 {
24510 if (section_is_gnu)
24511 {
24512 section = &dwarf2_per_objfile->macro;
24513 section_name = ".debug_macro";
24514 }
24515 else
24516 {
24517 section = &dwarf2_per_objfile->macinfo;
24518 section_name = ".debug_macinfo";
24519 }
24520 }
cf2c3c16 24521
bb5ed363 24522 dwarf2_read_section (objfile, section);
cf2c3c16
TT
24523 if (section->buffer == NULL)
24524 {
fceca515 24525 complaint (&symfile_complaints, _("missing %s section"), section_name);
cf2c3c16
TT
24526 return;
24527 }
a32a8923 24528 abfd = get_section_bfd_owner (section);
cf2c3c16
TT
24529
24530 /* First pass: Find the name of the base filename.
24531 This filename is needed in order to process all macros whose definition
24532 (or undefinition) comes from the command line. These macros are defined
24533 before the first DW_MACINFO_start_file entry, and yet still need to be
24534 associated to the base file.
24535
24536 To determine the base file name, we scan the macro definitions until we
24537 reach the first DW_MACINFO_start_file entry. We then initialize
24538 CURRENT_FILE accordingly so that any macro definition found before the
24539 first DW_MACINFO_start_file can still be associated to the base file. */
24540
24541 mac_ptr = section->buffer + offset;
24542 mac_end = section->buffer + section->size;
24543
24544 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
24545 &offset_size, section_is_gnu);
24546 if (mac_ptr == NULL)
24547 {
24548 /* We already issued a complaint. */
24549 return;
24550 }
24551
24552 do
24553 {
24554 /* Do we at least have room for a macinfo type byte? */
24555 if (mac_ptr >= mac_end)
24556 {
24557 /* Complaint is printed during the second pass as GDB will probably
24558 stop the first pass earlier upon finding
24559 DW_MACINFO_start_file. */
24560 break;
24561 }
24562
aead7601 24563 macinfo_type = (enum dwarf_macro_record_type) read_1_byte (abfd, mac_ptr);
cf2c3c16
TT
24564 mac_ptr++;
24565
24566 /* Note that we rely on the fact that the corresponding GNU and
24567 DWARF constants are the same. */
132448f8
SM
24568 DIAGNOSTIC_PUSH
24569 DIAGNOSTIC_IGNORE_SWITCH_DIFFERENT_ENUM_TYPES
cf2c3c16
TT
24570 switch (macinfo_type)
24571 {
24572 /* A zero macinfo type indicates the end of the macro
24573 information. */
24574 case 0:
24575 break;
24576
0af92d60
JK
24577 case DW_MACRO_define:
24578 case DW_MACRO_undef:
cf2c3c16
TT
24579 /* Only skip the data by MAC_PTR. */
24580 {
24581 unsigned int bytes_read;
24582
24583 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24584 mac_ptr += bytes_read;
24585 read_direct_string (abfd, mac_ptr, &bytes_read);
24586 mac_ptr += bytes_read;
24587 }
24588 break;
24589
0af92d60 24590 case DW_MACRO_start_file:
cf2c3c16
TT
24591 {
24592 unsigned int bytes_read;
24593 int line, file;
24594
24595 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24596 mac_ptr += bytes_read;
24597 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24598 mac_ptr += bytes_read;
24599
43f3e411 24600 current_file = macro_start_file (file, line, current_file, lh);
cf2c3c16
TT
24601 }
24602 break;
24603
0af92d60 24604 case DW_MACRO_end_file:
cf2c3c16
TT
24605 /* No data to skip by MAC_PTR. */
24606 break;
24607
0af92d60
JK
24608 case DW_MACRO_define_strp:
24609 case DW_MACRO_undef_strp:
24610 case DW_MACRO_define_sup:
24611 case DW_MACRO_undef_sup:
cf2c3c16
TT
24612 {
24613 unsigned int bytes_read;
24614
24615 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24616 mac_ptr += bytes_read;
24617 mac_ptr += offset_size;
24618 }
24619 break;
24620
0af92d60
JK
24621 case DW_MACRO_import:
24622 case DW_MACRO_import_sup:
cf2c3c16 24623 /* Note that, according to the spec, a transparent include
0af92d60 24624 chain cannot call DW_MACRO_start_file. So, we can just
cf2c3c16
TT
24625 skip this opcode. */
24626 mac_ptr += offset_size;
24627 break;
24628
24629 case DW_MACINFO_vendor_ext:
24630 /* Only skip the data by MAC_PTR. */
24631 if (!section_is_gnu)
24632 {
24633 unsigned int bytes_read;
24634
24635 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24636 mac_ptr += bytes_read;
24637 read_direct_string (abfd, mac_ptr, &bytes_read);
24638 mac_ptr += bytes_read;
24639 }
24640 /* FALLTHROUGH */
24641
24642 default:
24643 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
f664829e 24644 mac_ptr, mac_end, abfd, offset_size,
cf2c3c16
TT
24645 section);
24646 if (mac_ptr == NULL)
24647 return;
24648 break;
24649 }
132448f8 24650 DIAGNOSTIC_POP
cf2c3c16
TT
24651 } while (macinfo_type != 0 && current_file == NULL);
24652
24653 /* Second pass: Process all entries.
24654
24655 Use the AT_COMMAND_LINE flag to determine whether we are still processing
24656 command-line macro definitions/undefinitions. This flag is unset when we
24657 reach the first DW_MACINFO_start_file entry. */
24658
fc4007c9
TT
24659 htab_up include_hash (htab_create_alloc (1, htab_hash_pointer,
24660 htab_eq_pointer,
24661 NULL, xcalloc, xfree));
8fc3fc34 24662 mac_ptr = section->buffer + offset;
fc4007c9 24663 slot = htab_find_slot (include_hash.get (), mac_ptr, INSERT);
d521ce57 24664 *slot = (void *) mac_ptr;
ed2dc618
SM
24665 dwarf_decode_macro_bytes (dwarf2_per_objfile,
24666 abfd, mac_ptr, mac_end,
43f3e411 24667 current_file, lh, section,
fc4007c9
TT
24668 section_is_gnu, 0, offset_size,
24669 include_hash.get ());
cf2c3c16
TT
24670}
24671
8e19ed76 24672/* Check if the attribute's form is a DW_FORM_block*
0963b4bd 24673 if so return true else false. */
380bca97 24674
8e19ed76 24675static int
6e5a29e1 24676attr_form_is_block (const struct attribute *attr)
8e19ed76
PS
24677{
24678 return (attr == NULL ? 0 :
24679 attr->form == DW_FORM_block1
24680 || attr->form == DW_FORM_block2
24681 || attr->form == DW_FORM_block4
2dc7f7b3
TT
24682 || attr->form == DW_FORM_block
24683 || attr->form == DW_FORM_exprloc);
8e19ed76 24684}
4c2df51b 24685
c6a0999f
JB
24686/* Return non-zero if ATTR's value is a section offset --- classes
24687 lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
24688 You may use DW_UNSND (attr) to retrieve such offsets.
24689
24690 Section 7.5.4, "Attribute Encodings", explains that no attribute
24691 may have a value that belongs to more than one of these classes; it
24692 would be ambiguous if we did, because we use the same forms for all
24693 of them. */
380bca97 24694
3690dd37 24695static int
6e5a29e1 24696attr_form_is_section_offset (const struct attribute *attr)
3690dd37
JB
24697{
24698 return (attr->form == DW_FORM_data4
2dc7f7b3
TT
24699 || attr->form == DW_FORM_data8
24700 || attr->form == DW_FORM_sec_offset);
3690dd37
JB
24701}
24702
3690dd37
JB
24703/* Return non-zero if ATTR's value falls in the 'constant' class, or
24704 zero otherwise. When this function returns true, you can apply
24705 dwarf2_get_attr_constant_value to it.
24706
24707 However, note that for some attributes you must check
24708 attr_form_is_section_offset before using this test. DW_FORM_data4
24709 and DW_FORM_data8 are members of both the constant class, and of
24710 the classes that contain offsets into other debug sections
24711 (lineptr, loclistptr, macptr or rangelistptr). The DWARF spec says
24712 that, if an attribute's can be either a constant or one of the
24713 section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
0224619f
JK
24714 taken as section offsets, not constants.
24715
24716 DW_FORM_data16 is not considered as dwarf2_get_attr_constant_value
24717 cannot handle that. */
380bca97 24718
3690dd37 24719static int
6e5a29e1 24720attr_form_is_constant (const struct attribute *attr)
3690dd37
JB
24721{
24722 switch (attr->form)
24723 {
24724 case DW_FORM_sdata:
24725 case DW_FORM_udata:
24726 case DW_FORM_data1:
24727 case DW_FORM_data2:
24728 case DW_FORM_data4:
24729 case DW_FORM_data8:
663c44ac 24730 case DW_FORM_implicit_const:
3690dd37
JB
24731 return 1;
24732 default:
24733 return 0;
24734 }
24735}
24736
7771576e
SA
24737
24738/* DW_ADDR is always stored already as sect_offset; despite for the forms
24739 besides DW_FORM_ref_addr it is stored as cu_offset in the DWARF file. */
24740
24741static int
6e5a29e1 24742attr_form_is_ref (const struct attribute *attr)
7771576e
SA
24743{
24744 switch (attr->form)
24745 {
24746 case DW_FORM_ref_addr:
24747 case DW_FORM_ref1:
24748 case DW_FORM_ref2:
24749 case DW_FORM_ref4:
24750 case DW_FORM_ref8:
24751 case DW_FORM_ref_udata:
24752 case DW_FORM_GNU_ref_alt:
24753 return 1;
24754 default:
24755 return 0;
24756 }
24757}
24758
3019eac3
DE
24759/* Return the .debug_loc section to use for CU.
24760 For DWO files use .debug_loc.dwo. */
24761
24762static struct dwarf2_section_info *
24763cu_debug_loc_section (struct dwarf2_cu *cu)
24764{
518817b3
SM
24765 struct dwarf2_per_objfile *dwarf2_per_objfile
24766 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 24767
3019eac3 24768 if (cu->dwo_unit)
43988095
JK
24769 {
24770 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
24771
24772 return cu->header.version >= 5 ? &sections->loclists : &sections->loc;
24773 }
24774 return (cu->header.version >= 5 ? &dwarf2_per_objfile->loclists
24775 : &dwarf2_per_objfile->loc);
3019eac3
DE
24776}
24777
8cf6f0b1
TT
24778/* A helper function that fills in a dwarf2_loclist_baton. */
24779
24780static void
24781fill_in_loclist_baton (struct dwarf2_cu *cu,
24782 struct dwarf2_loclist_baton *baton,
ff39bb5e 24783 const struct attribute *attr)
8cf6f0b1 24784{
518817b3
SM
24785 struct dwarf2_per_objfile *dwarf2_per_objfile
24786 = cu->per_cu->dwarf2_per_objfile;
3019eac3
DE
24787 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
24788
24789 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
8cf6f0b1
TT
24790
24791 baton->per_cu = cu->per_cu;
24792 gdb_assert (baton->per_cu);
24793 /* We don't know how long the location list is, but make sure we
24794 don't run off the edge of the section. */
3019eac3
DE
24795 baton->size = section->size - DW_UNSND (attr);
24796 baton->data = section->buffer + DW_UNSND (attr);
8cf6f0b1 24797 baton->base_address = cu->base_address;
f664829e 24798 baton->from_dwo = cu->dwo_unit != NULL;
8cf6f0b1
TT
24799}
24800
4c2df51b 24801static void
ff39bb5e 24802dwarf2_symbol_mark_computed (const struct attribute *attr, struct symbol *sym,
f1e6e072 24803 struct dwarf2_cu *cu, int is_block)
4c2df51b 24804{
518817b3
SM
24805 struct dwarf2_per_objfile *dwarf2_per_objfile
24806 = cu->per_cu->dwarf2_per_objfile;
bb5ed363 24807 struct objfile *objfile = dwarf2_per_objfile->objfile;
3019eac3 24808 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
bb5ed363 24809
3690dd37 24810 if (attr_form_is_section_offset (attr)
3019eac3 24811 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
99bcc461
DJ
24812 the section. If so, fall through to the complaint in the
24813 other branch. */
3019eac3 24814 && DW_UNSND (attr) < dwarf2_section_size (objfile, section))
4c2df51b 24815 {
0d53c4c4 24816 struct dwarf2_loclist_baton *baton;
4c2df51b 24817
8d749320 24818 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_loclist_baton);
4c2df51b 24819
8cf6f0b1 24820 fill_in_loclist_baton (cu, baton, attr);
be391dca 24821
d00adf39 24822 if (cu->base_known == 0)
0d53c4c4 24823 complaint (&symfile_complaints,
3e43a32a
MS
24824 _("Location list used without "
24825 "specifying the CU base address."));
4c2df51b 24826
f1e6e072
TT
24827 SYMBOL_ACLASS_INDEX (sym) = (is_block
24828 ? dwarf2_loclist_block_index
24829 : dwarf2_loclist_index);
0d53c4c4
DJ
24830 SYMBOL_LOCATION_BATON (sym) = baton;
24831 }
24832 else
24833 {
24834 struct dwarf2_locexpr_baton *baton;
24835
8d749320 24836 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
ae0d2f24
UW
24837 baton->per_cu = cu->per_cu;
24838 gdb_assert (baton->per_cu);
0d53c4c4
DJ
24839
24840 if (attr_form_is_block (attr))
24841 {
24842 /* Note that we're just copying the block's data pointer
24843 here, not the actual data. We're still pointing into the
6502dd73
DJ
24844 info_buffer for SYM's objfile; right now we never release
24845 that buffer, but when we do clean up properly this may
24846 need to change. */
0d53c4c4
DJ
24847 baton->size = DW_BLOCK (attr)->size;
24848 baton->data = DW_BLOCK (attr)->data;
24849 }
24850 else
24851 {
24852 dwarf2_invalid_attrib_class_complaint ("location description",
24853 SYMBOL_NATURAL_NAME (sym));
24854 baton->size = 0;
0d53c4c4 24855 }
6e70227d 24856
f1e6e072
TT
24857 SYMBOL_ACLASS_INDEX (sym) = (is_block
24858 ? dwarf2_locexpr_block_index
24859 : dwarf2_locexpr_index);
0d53c4c4
DJ
24860 SYMBOL_LOCATION_BATON (sym) = baton;
24861 }
4c2df51b 24862}
6502dd73 24863
9aa1f1e3
TT
24864/* Return the OBJFILE associated with the compilation unit CU. If CU
24865 came from a separate debuginfo file, then the master objfile is
24866 returned. */
ae0d2f24
UW
24867
24868struct objfile *
24869dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
24870{
e3b94546 24871 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
ae0d2f24
UW
24872
24873 /* Return the master objfile, so that we can report and look up the
24874 correct file containing this variable. */
24875 if (objfile->separate_debug_objfile_backlink)
24876 objfile = objfile->separate_debug_objfile_backlink;
24877
24878 return objfile;
24879}
24880
96408a79
SA
24881/* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
24882 (CU_HEADERP is unused in such case) or prepare a temporary copy at
24883 CU_HEADERP first. */
24884
24885static const struct comp_unit_head *
24886per_cu_header_read_in (struct comp_unit_head *cu_headerp,
24887 struct dwarf2_per_cu_data *per_cu)
24888{
d521ce57 24889 const gdb_byte *info_ptr;
96408a79
SA
24890
24891 if (per_cu->cu)
24892 return &per_cu->cu->header;
24893
9c541725 24894 info_ptr = per_cu->section->buffer + to_underlying (per_cu->sect_off);
96408a79
SA
24895
24896 memset (cu_headerp, 0, sizeof (*cu_headerp));
43988095
JK
24897 read_comp_unit_head (cu_headerp, info_ptr, per_cu->section,
24898 rcuh_kind::COMPILE);
96408a79
SA
24899
24900 return cu_headerp;
24901}
24902
ae0d2f24
UW
24903/* Return the address size given in the compilation unit header for CU. */
24904
98714339 24905int
ae0d2f24
UW
24906dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
24907{
96408a79
SA
24908 struct comp_unit_head cu_header_local;
24909 const struct comp_unit_head *cu_headerp;
c471e790 24910
96408a79
SA
24911 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
24912
24913 return cu_headerp->addr_size;
ae0d2f24
UW
24914}
24915
9eae7c52
TT
24916/* Return the offset size given in the compilation unit header for CU. */
24917
24918int
24919dwarf2_per_cu_offset_size (struct dwarf2_per_cu_data *per_cu)
24920{
96408a79
SA
24921 struct comp_unit_head cu_header_local;
24922 const struct comp_unit_head *cu_headerp;
9c6c53f7 24923
96408a79
SA
24924 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
24925
24926 return cu_headerp->offset_size;
24927}
24928
24929/* See its dwarf2loc.h declaration. */
24930
24931int
24932dwarf2_per_cu_ref_addr_size (struct dwarf2_per_cu_data *per_cu)
24933{
24934 struct comp_unit_head cu_header_local;
24935 const struct comp_unit_head *cu_headerp;
24936
24937 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
24938
24939 if (cu_headerp->version == 2)
24940 return cu_headerp->addr_size;
24941 else
24942 return cu_headerp->offset_size;
181cebd4
JK
24943}
24944
9aa1f1e3
TT
24945/* Return the text offset of the CU. The returned offset comes from
24946 this CU's objfile. If this objfile came from a separate debuginfo
24947 file, then the offset may be different from the corresponding
24948 offset in the parent objfile. */
24949
24950CORE_ADDR
24951dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
24952{
e3b94546 24953 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
9aa1f1e3
TT
24954
24955 return ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
24956}
24957
43988095
JK
24958/* Return DWARF version number of PER_CU. */
24959
24960short
24961dwarf2_version (struct dwarf2_per_cu_data *per_cu)
24962{
24963 return per_cu->dwarf_version;
24964}
24965
348e048f
DE
24966/* Locate the .debug_info compilation unit from CU's objfile which contains
24967 the DIE at OFFSET. Raises an error on failure. */
ae038cb0
DJ
24968
24969static struct dwarf2_per_cu_data *
9c541725 24970dwarf2_find_containing_comp_unit (sect_offset sect_off,
36586728 24971 unsigned int offset_in_dwz,
ed2dc618 24972 struct dwarf2_per_objfile *dwarf2_per_objfile)
ae038cb0
DJ
24973{
24974 struct dwarf2_per_cu_data *this_cu;
24975 int low, high;
36586728 24976 const sect_offset *cu_off;
ae038cb0 24977
ae038cb0 24978 low = 0;
b76e467d 24979 high = dwarf2_per_objfile->all_comp_units.size () - 1;
ae038cb0
DJ
24980 while (high > low)
24981 {
36586728 24982 struct dwarf2_per_cu_data *mid_cu;
ae038cb0 24983 int mid = low + (high - low) / 2;
9a619af0 24984
36586728 24985 mid_cu = dwarf2_per_objfile->all_comp_units[mid];
9c541725 24986 cu_off = &mid_cu->sect_off;
36586728 24987 if (mid_cu->is_dwz > offset_in_dwz
9c541725 24988 || (mid_cu->is_dwz == offset_in_dwz && *cu_off >= sect_off))
ae038cb0
DJ
24989 high = mid;
24990 else
24991 low = mid + 1;
24992 }
24993 gdb_assert (low == high);
36586728 24994 this_cu = dwarf2_per_objfile->all_comp_units[low];
9c541725
PA
24995 cu_off = &this_cu->sect_off;
24996 if (this_cu->is_dwz != offset_in_dwz || *cu_off > sect_off)
ae038cb0 24997 {
36586728 24998 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
8a3fe4f8 24999 error (_("Dwarf Error: could not find partial DIE containing "
9d8780f0
SM
25000 "offset %s [in module %s]"),
25001 sect_offset_str (sect_off),
ed2dc618 25002 bfd_get_filename (dwarf2_per_objfile->objfile->obfd));
10b3939b 25003
9c541725
PA
25004 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->sect_off
25005 <= sect_off);
ae038cb0
DJ
25006 return dwarf2_per_objfile->all_comp_units[low-1];
25007 }
25008 else
25009 {
25010 this_cu = dwarf2_per_objfile->all_comp_units[low];
b76e467d 25011 if (low == dwarf2_per_objfile->all_comp_units.size () - 1
9c541725 25012 && sect_off >= this_cu->sect_off + this_cu->length)
9d8780f0 25013 error (_("invalid dwarf2 offset %s"), sect_offset_str (sect_off));
9c541725 25014 gdb_assert (sect_off < this_cu->sect_off + this_cu->length);
ae038cb0
DJ
25015 return this_cu;
25016 }
25017}
25018
23745b47 25019/* Initialize dwarf2_cu CU, owned by PER_CU. */
93311388 25020
fcd3b13d
SM
25021dwarf2_cu::dwarf2_cu (struct dwarf2_per_cu_data *per_cu_)
25022 : per_cu (per_cu_),
25023 mark (0),
25024 has_loclist (0),
25025 checked_producer (0),
25026 producer_is_gxx_lt_4_6 (0),
25027 producer_is_gcc_lt_4_3 (0),
25028 producer_is_icc_lt_14 (0),
25029 processing_has_namespace_info (0)
93311388 25030{
fcd3b13d
SM
25031 per_cu->cu = this;
25032}
25033
25034/* Destroy a dwarf2_cu. */
25035
25036dwarf2_cu::~dwarf2_cu ()
25037{
25038 per_cu->cu = NULL;
9816fde3
JK
25039}
25040
25041/* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
25042
25043static void
95554aad
TT
25044prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
25045 enum language pretend_language)
9816fde3
JK
25046{
25047 struct attribute *attr;
25048
25049 /* Set the language we're debugging. */
25050 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
25051 if (attr)
25052 set_cu_language (DW_UNSND (attr), cu);
25053 else
9cded63f 25054 {
95554aad 25055 cu->language = pretend_language;
9cded63f
TT
25056 cu->language_defn = language_def (cu->language);
25057 }
dee91e82 25058
7d45c7c3 25059 cu->producer = dwarf2_string_attr (comp_unit_die, DW_AT_producer, cu);
93311388
DE
25060}
25061
ae038cb0
DJ
25062/* Increase the age counter on each cached compilation unit, and free
25063 any that are too old. */
25064
25065static void
ed2dc618 25066age_cached_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
ae038cb0
DJ
25067{
25068 struct dwarf2_per_cu_data *per_cu, **last_chain;
25069
25070 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
25071 per_cu = dwarf2_per_objfile->read_in_chain;
25072 while (per_cu != NULL)
25073 {
25074 per_cu->cu->last_used ++;
b4f54984 25075 if (per_cu->cu->last_used <= dwarf_max_cache_age)
ae038cb0
DJ
25076 dwarf2_mark (per_cu->cu);
25077 per_cu = per_cu->cu->read_in_chain;
25078 }
25079
25080 per_cu = dwarf2_per_objfile->read_in_chain;
25081 last_chain = &dwarf2_per_objfile->read_in_chain;
25082 while (per_cu != NULL)
25083 {
25084 struct dwarf2_per_cu_data *next_cu;
25085
25086 next_cu = per_cu->cu->read_in_chain;
25087
25088 if (!per_cu->cu->mark)
25089 {
fcd3b13d 25090 delete per_cu->cu;
ae038cb0
DJ
25091 *last_chain = next_cu;
25092 }
25093 else
25094 last_chain = &per_cu->cu->read_in_chain;
25095
25096 per_cu = next_cu;
25097 }
25098}
25099
25100/* Remove a single compilation unit from the cache. */
25101
25102static void
dee91e82 25103free_one_cached_comp_unit (struct dwarf2_per_cu_data *target_per_cu)
ae038cb0
DJ
25104{
25105 struct dwarf2_per_cu_data *per_cu, **last_chain;
ed2dc618
SM
25106 struct dwarf2_per_objfile *dwarf2_per_objfile
25107 = target_per_cu->dwarf2_per_objfile;
ae038cb0
DJ
25108
25109 per_cu = dwarf2_per_objfile->read_in_chain;
25110 last_chain = &dwarf2_per_objfile->read_in_chain;
25111 while (per_cu != NULL)
25112 {
25113 struct dwarf2_per_cu_data *next_cu;
25114
25115 next_cu = per_cu->cu->read_in_chain;
25116
dee91e82 25117 if (per_cu == target_per_cu)
ae038cb0 25118 {
fcd3b13d 25119 delete per_cu->cu;
dee91e82 25120 per_cu->cu = NULL;
ae038cb0
DJ
25121 *last_chain = next_cu;
25122 break;
25123 }
25124 else
25125 last_chain = &per_cu->cu->read_in_chain;
25126
25127 per_cu = next_cu;
25128 }
25129}
25130
fe3e1990
DJ
25131/* Release all extra memory associated with OBJFILE. */
25132
25133void
25134dwarf2_free_objfile (struct objfile *objfile)
25135{
ed2dc618
SM
25136 struct dwarf2_per_objfile *dwarf2_per_objfile
25137 = get_dwarf2_per_objfile (objfile);
fe3e1990 25138
fd90ace4 25139 delete dwarf2_per_objfile;
fe3e1990
DJ
25140}
25141
dee91e82
DE
25142/* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
25143 We store these in a hash table separate from the DIEs, and preserve them
25144 when the DIEs are flushed out of cache.
25145
25146 The CU "per_cu" pointer is needed because offset alone is not enough to
3019eac3 25147 uniquely identify the type. A file may have multiple .debug_types sections,
c88ee1f0
DE
25148 or the type may come from a DWO file. Furthermore, while it's more logical
25149 to use per_cu->section+offset, with Fission the section with the data is in
25150 the DWO file but we don't know that section at the point we need it.
25151 We have to use something in dwarf2_per_cu_data (or the pointer to it)
25152 because we can enter the lookup routine, get_die_type_at_offset, from
25153 outside this file, and thus won't necessarily have PER_CU->cu.
25154 Fortunately, PER_CU is stable for the life of the objfile. */
1c379e20 25155
dee91e82 25156struct dwarf2_per_cu_offset_and_type
1c379e20 25157{
dee91e82 25158 const struct dwarf2_per_cu_data *per_cu;
9c541725 25159 sect_offset sect_off;
1c379e20
DJ
25160 struct type *type;
25161};
25162
dee91e82 25163/* Hash function for a dwarf2_per_cu_offset_and_type. */
1c379e20
DJ
25164
25165static hashval_t
dee91e82 25166per_cu_offset_and_type_hash (const void *item)
1c379e20 25167{
9a3c8263
SM
25168 const struct dwarf2_per_cu_offset_and_type *ofs
25169 = (const struct dwarf2_per_cu_offset_and_type *) item;
9a619af0 25170
9c541725 25171 return (uintptr_t) ofs->per_cu + to_underlying (ofs->sect_off);
1c379e20
DJ
25172}
25173
dee91e82 25174/* Equality function for a dwarf2_per_cu_offset_and_type. */
1c379e20
DJ
25175
25176static int
dee91e82 25177per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
1c379e20 25178{
9a3c8263
SM
25179 const struct dwarf2_per_cu_offset_and_type *ofs_lhs
25180 = (const struct dwarf2_per_cu_offset_and_type *) item_lhs;
25181 const struct dwarf2_per_cu_offset_and_type *ofs_rhs
25182 = (const struct dwarf2_per_cu_offset_and_type *) item_rhs;
9a619af0 25183
dee91e82 25184 return (ofs_lhs->per_cu == ofs_rhs->per_cu
9c541725 25185 && ofs_lhs->sect_off == ofs_rhs->sect_off);
1c379e20
DJ
25186}
25187
25188/* Set the type associated with DIE to TYPE. Save it in CU's hash
7e314c57
JK
25189 table if necessary. For convenience, return TYPE.
25190
25191 The DIEs reading must have careful ordering to:
25192 * Not cause infite loops trying to read in DIEs as a prerequisite for
25193 reading current DIE.
25194 * Not trying to dereference contents of still incompletely read in types
25195 while reading in other DIEs.
25196 * Enable referencing still incompletely read in types just by a pointer to
25197 the type without accessing its fields.
25198
25199 Therefore caller should follow these rules:
25200 * Try to fetch any prerequisite types we may need to build this DIE type
25201 before building the type and calling set_die_type.
e71ec853 25202 * After building type call set_die_type for current DIE as soon as
7e314c57
JK
25203 possible before fetching more types to complete the current type.
25204 * Make the type as complete as possible before fetching more types. */
1c379e20 25205
f792889a 25206static struct type *
1c379e20
DJ
25207set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
25208{
518817b3
SM
25209 struct dwarf2_per_objfile *dwarf2_per_objfile
25210 = cu->per_cu->dwarf2_per_objfile;
dee91e82 25211 struct dwarf2_per_cu_offset_and_type **slot, ofs;
ed2dc618 25212 struct objfile *objfile = dwarf2_per_objfile->objfile;
3cdcd0ce
JB
25213 struct attribute *attr;
25214 struct dynamic_prop prop;
1c379e20 25215
b4ba55a1
JB
25216 /* For Ada types, make sure that the gnat-specific data is always
25217 initialized (if not already set). There are a few types where
25218 we should not be doing so, because the type-specific area is
25219 already used to hold some other piece of info (eg: TYPE_CODE_FLT
25220 where the type-specific area is used to store the floatformat).
25221 But this is not a problem, because the gnat-specific information
25222 is actually not needed for these types. */
25223 if (need_gnat_info (cu)
25224 && TYPE_CODE (type) != TYPE_CODE_FUNC
25225 && TYPE_CODE (type) != TYPE_CODE_FLT
09e2d7c7
DE
25226 && TYPE_CODE (type) != TYPE_CODE_METHODPTR
25227 && TYPE_CODE (type) != TYPE_CODE_MEMBERPTR
25228 && TYPE_CODE (type) != TYPE_CODE_METHOD
b4ba55a1
JB
25229 && !HAVE_GNAT_AUX_INFO (type))
25230 INIT_GNAT_SPECIFIC (type);
25231
3f2f83dd
KB
25232 /* Read DW_AT_allocated and set in type. */
25233 attr = dwarf2_attr (die, DW_AT_allocated, cu);
25234 if (attr_form_is_block (attr))
25235 {
25236 if (attr_to_dynamic_prop (attr, die, cu, &prop))
50a82047 25237 add_dyn_prop (DYN_PROP_ALLOCATED, prop, type);
3f2f83dd
KB
25238 }
25239 else if (attr != NULL)
25240 {
25241 complaint (&symfile_complaints,
9d8780f0 25242 _("DW_AT_allocated has the wrong form (%s) at DIE %s"),
9c541725 25243 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
9d8780f0 25244 sect_offset_str (die->sect_off));
3f2f83dd
KB
25245 }
25246
25247 /* Read DW_AT_associated and set in type. */
25248 attr = dwarf2_attr (die, DW_AT_associated, cu);
25249 if (attr_form_is_block (attr))
25250 {
25251 if (attr_to_dynamic_prop (attr, die, cu, &prop))
50a82047 25252 add_dyn_prop (DYN_PROP_ASSOCIATED, prop, type);
3f2f83dd
KB
25253 }
25254 else if (attr != NULL)
25255 {
25256 complaint (&symfile_complaints,
9d8780f0 25257 _("DW_AT_associated has the wrong form (%s) at DIE %s"),
9c541725 25258 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
9d8780f0 25259 sect_offset_str (die->sect_off));
3f2f83dd
KB
25260 }
25261
3cdcd0ce
JB
25262 /* Read DW_AT_data_location and set in type. */
25263 attr = dwarf2_attr (die, DW_AT_data_location, cu);
25264 if (attr_to_dynamic_prop (attr, die, cu, &prop))
50a82047 25265 add_dyn_prop (DYN_PROP_DATA_LOCATION, prop, type);
3cdcd0ce 25266
dee91e82 25267 if (dwarf2_per_objfile->die_type_hash == NULL)
f792889a 25268 {
dee91e82
DE
25269 dwarf2_per_objfile->die_type_hash =
25270 htab_create_alloc_ex (127,
25271 per_cu_offset_and_type_hash,
25272 per_cu_offset_and_type_eq,
25273 NULL,
25274 &objfile->objfile_obstack,
25275 hashtab_obstack_allocate,
25276 dummy_obstack_deallocate);
f792889a 25277 }
1c379e20 25278
dee91e82 25279 ofs.per_cu = cu->per_cu;
9c541725 25280 ofs.sect_off = die->sect_off;
1c379e20 25281 ofs.type = type;
dee91e82
DE
25282 slot = (struct dwarf2_per_cu_offset_and_type **)
25283 htab_find_slot (dwarf2_per_objfile->die_type_hash, &ofs, INSERT);
7e314c57
JK
25284 if (*slot)
25285 complaint (&symfile_complaints,
9d8780f0
SM
25286 _("A problem internal to GDB: DIE %s has type already set"),
25287 sect_offset_str (die->sect_off));
8d749320
SM
25288 *slot = XOBNEW (&objfile->objfile_obstack,
25289 struct dwarf2_per_cu_offset_and_type);
1c379e20 25290 **slot = ofs;
f792889a 25291 return type;
1c379e20
DJ
25292}
25293
9c541725 25294/* Look up the type for the die at SECT_OFF in PER_CU in die_type_hash,
02142a6c 25295 or return NULL if the die does not have a saved type. */
1c379e20
DJ
25296
25297static struct type *
9c541725 25298get_die_type_at_offset (sect_offset sect_off,
673bfd45 25299 struct dwarf2_per_cu_data *per_cu)
1c379e20 25300{
dee91e82 25301 struct dwarf2_per_cu_offset_and_type *slot, ofs;
ed2dc618 25302 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
f792889a 25303
dee91e82 25304 if (dwarf2_per_objfile->die_type_hash == NULL)
f792889a 25305 return NULL;
1c379e20 25306
dee91e82 25307 ofs.per_cu = per_cu;
9c541725 25308 ofs.sect_off = sect_off;
9a3c8263
SM
25309 slot = ((struct dwarf2_per_cu_offset_and_type *)
25310 htab_find (dwarf2_per_objfile->die_type_hash, &ofs));
1c379e20
DJ
25311 if (slot)
25312 return slot->type;
25313 else
25314 return NULL;
25315}
25316
02142a6c 25317/* Look up the type for DIE in CU in die_type_hash,
673bfd45
DE
25318 or return NULL if DIE does not have a saved type. */
25319
25320static struct type *
25321get_die_type (struct die_info *die, struct dwarf2_cu *cu)
25322{
9c541725 25323 return get_die_type_at_offset (die->sect_off, cu->per_cu);
673bfd45
DE
25324}
25325
10b3939b
DJ
25326/* Add a dependence relationship from CU to REF_PER_CU. */
25327
25328static void
25329dwarf2_add_dependence (struct dwarf2_cu *cu,
25330 struct dwarf2_per_cu_data *ref_per_cu)
25331{
25332 void **slot;
25333
25334 if (cu->dependencies == NULL)
25335 cu->dependencies
25336 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
25337 NULL, &cu->comp_unit_obstack,
25338 hashtab_obstack_allocate,
25339 dummy_obstack_deallocate);
25340
25341 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
25342 if (*slot == NULL)
25343 *slot = ref_per_cu;
25344}
1c379e20 25345
f504f079
DE
25346/* Subroutine of dwarf2_mark to pass to htab_traverse.
25347 Set the mark field in every compilation unit in the
ae038cb0
DJ
25348 cache that we must keep because we are keeping CU. */
25349
10b3939b
DJ
25350static int
25351dwarf2_mark_helper (void **slot, void *data)
25352{
25353 struct dwarf2_per_cu_data *per_cu;
25354
25355 per_cu = (struct dwarf2_per_cu_data *) *slot;
d07ed419
JK
25356
25357 /* cu->dependencies references may not yet have been ever read if QUIT aborts
25358 reading of the chain. As such dependencies remain valid it is not much
25359 useful to track and undo them during QUIT cleanups. */
25360 if (per_cu->cu == NULL)
25361 return 1;
25362
10b3939b
DJ
25363 if (per_cu->cu->mark)
25364 return 1;
25365 per_cu->cu->mark = 1;
25366
25367 if (per_cu->cu->dependencies != NULL)
25368 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
25369
25370 return 1;
25371}
25372
f504f079
DE
25373/* Set the mark field in CU and in every other compilation unit in the
25374 cache that we must keep because we are keeping CU. */
25375
ae038cb0
DJ
25376static void
25377dwarf2_mark (struct dwarf2_cu *cu)
25378{
25379 if (cu->mark)
25380 return;
25381 cu->mark = 1;
10b3939b
DJ
25382 if (cu->dependencies != NULL)
25383 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
ae038cb0
DJ
25384}
25385
25386static void
25387dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
25388{
25389 while (per_cu)
25390 {
25391 per_cu->cu->mark = 0;
25392 per_cu = per_cu->cu->read_in_chain;
25393 }
72bf9492
DJ
25394}
25395
72bf9492
DJ
25396/* Trivial hash function for partial_die_info: the hash value of a DIE
25397 is its offset in .debug_info for this objfile. */
25398
25399static hashval_t
25400partial_die_hash (const void *item)
25401{
9a3c8263
SM
25402 const struct partial_die_info *part_die
25403 = (const struct partial_die_info *) item;
9a619af0 25404
9c541725 25405 return to_underlying (part_die->sect_off);
72bf9492
DJ
25406}
25407
25408/* Trivial comparison function for partial_die_info structures: two DIEs
25409 are equal if they have the same offset. */
25410
25411static int
25412partial_die_eq (const void *item_lhs, const void *item_rhs)
25413{
9a3c8263
SM
25414 const struct partial_die_info *part_die_lhs
25415 = (const struct partial_die_info *) item_lhs;
25416 const struct partial_die_info *part_die_rhs
25417 = (const struct partial_die_info *) item_rhs;
9a619af0 25418
9c541725 25419 return part_die_lhs->sect_off == part_die_rhs->sect_off;
72bf9492
DJ
25420}
25421
b4f54984
DE
25422static struct cmd_list_element *set_dwarf_cmdlist;
25423static struct cmd_list_element *show_dwarf_cmdlist;
ae038cb0
DJ
25424
25425static void
981a3fb3 25426set_dwarf_cmd (const char *args, int from_tty)
ae038cb0 25427{
b4f54984 25428 help_list (set_dwarf_cmdlist, "maintenance set dwarf ", all_commands,
635c7e8a 25429 gdb_stdout);
ae038cb0
DJ
25430}
25431
25432static void
981a3fb3 25433show_dwarf_cmd (const char *args, int from_tty)
6e70227d 25434{
b4f54984 25435 cmd_show_list (show_dwarf_cmdlist, from_tty, "");
ae038cb0
DJ
25436}
25437
cd4fb1b2 25438int dwarf_always_disassemble;
437afbb8 25439
437afbb8 25440static void
cd4fb1b2
SM
25441show_dwarf_always_disassemble (struct ui_file *file, int from_tty,
25442 struct cmd_list_element *c, const char *value)
9291a0cd 25443{
cd4fb1b2
SM
25444 fprintf_filtered (file,
25445 _("Whether to always disassemble "
25446 "DWARF expressions is %s.\n"),
25447 value);
9291a0cd
TT
25448}
25449
9291a0cd 25450static void
cd4fb1b2
SM
25451show_check_physname (struct ui_file *file, int from_tty,
25452 struct cmd_list_element *c, const char *value)
9291a0cd 25453{
cd4fb1b2
SM
25454 fprintf_filtered (file,
25455 _("Whether to check \"physname\" is %s.\n"),
25456 value);
9291a0cd
TT
25457}
25458
cd4fb1b2
SM
25459void
25460_initialize_dwarf2_read (void)
9291a0cd 25461{
9291a0cd 25462
cd4fb1b2 25463 dwarf2_objfile_data_key = register_objfile_data ();
156942c7 25464
cd4fb1b2
SM
25465 add_prefix_cmd ("dwarf", class_maintenance, set_dwarf_cmd, _("\
25466Set DWARF specific variables.\n\
25467Configure DWARF variables such as the cache size"),
25468 &set_dwarf_cmdlist, "maintenance set dwarf ",
25469 0/*allow-unknown*/, &maintenance_set_cmdlist);
156942c7 25470
cd4fb1b2
SM
25471 add_prefix_cmd ("dwarf", class_maintenance, show_dwarf_cmd, _("\
25472Show DWARF specific variables\n\
25473Show DWARF variables such as the cache size"),
25474 &show_dwarf_cmdlist, "maintenance show dwarf ",
25475 0/*allow-unknown*/, &maintenance_show_cmdlist);
156942c7 25476
cd4fb1b2
SM
25477 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
25478 &dwarf_max_cache_age, _("\
25479Set the upper bound on the age of cached DWARF compilation units."), _("\
25480Show the upper bound on the age of cached DWARF compilation units."), _("\
25481A higher limit means that cached compilation units will be stored\n\
25482in memory longer, and more total memory will be used. Zero disables\n\
25483caching, which can slow down startup."),
25484 NULL,
25485 show_dwarf_max_cache_age,
25486 &set_dwarf_cmdlist,
25487 &show_dwarf_cmdlist);
156942c7 25488
cd4fb1b2
SM
25489 add_setshow_boolean_cmd ("always-disassemble", class_obscure,
25490 &dwarf_always_disassemble, _("\
25491Set whether `info address' always disassembles DWARF expressions."), _("\
25492Show whether `info address' always disassembles DWARF expressions."), _("\
25493When enabled, DWARF expressions are always printed in an assembly-like\n\
25494syntax. When disabled, expressions will be printed in a more\n\
25495conversational style, when possible."),
25496 NULL,
25497 show_dwarf_always_disassemble,
25498 &set_dwarf_cmdlist,
25499 &show_dwarf_cmdlist);
9291a0cd 25500
cd4fb1b2
SM
25501 add_setshow_zuinteger_cmd ("dwarf-read", no_class, &dwarf_read_debug, _("\
25502Set debugging of the DWARF reader."), _("\
25503Show debugging of the DWARF reader."), _("\
25504When enabled (non-zero), debugging messages are printed during DWARF\n\
25505reading and symtab expansion. A value of 1 (one) provides basic\n\
25506information. A value greater than 1 provides more verbose information."),
25507 NULL,
25508 NULL,
25509 &setdebuglist, &showdebuglist);
9291a0cd 25510
cd4fb1b2
SM
25511 add_setshow_zuinteger_cmd ("dwarf-die", no_class, &dwarf_die_debug, _("\
25512Set debugging of the DWARF DIE reader."), _("\
25513Show debugging of the DWARF DIE reader."), _("\
25514When enabled (non-zero), DIEs are dumped after they are read in.\n\
25515The value is the maximum depth to print."),
25516 NULL,
25517 NULL,
25518 &setdebuglist, &showdebuglist);
9291a0cd 25519
cd4fb1b2
SM
25520 add_setshow_zuinteger_cmd ("dwarf-line", no_class, &dwarf_line_debug, _("\
25521Set debugging of the dwarf line reader."), _("\
25522Show debugging of the dwarf line reader."), _("\
25523When enabled (non-zero), line number entries are dumped as they are read in.\n\
25524A value of 1 (one) provides basic information.\n\
25525A value greater than 1 provides more verbose information."),
25526 NULL,
25527 NULL,
25528 &setdebuglist, &showdebuglist);
437afbb8 25529
cd4fb1b2
SM
25530 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
25531Set cross-checking of \"physname\" code against demangler."), _("\
25532Show cross-checking of \"physname\" code against demangler."), _("\
25533When enabled, GDB's internal \"physname\" code is checked against\n\
25534the demangler."),
25535 NULL, show_check_physname,
25536 &setdebuglist, &showdebuglist);
900e11f9 25537
e615022a
DE
25538 add_setshow_boolean_cmd ("use-deprecated-index-sections",
25539 no_class, &use_deprecated_index_sections, _("\
25540Set whether to use deprecated gdb_index sections."), _("\
25541Show whether to use deprecated gdb_index sections."), _("\
25542When enabled, deprecated .gdb_index sections are used anyway.\n\
25543Normally they are ignored either because of a missing feature or\n\
25544performance issue.\n\
25545Warning: This option must be enabled before gdb reads the file."),
25546 NULL,
25547 NULL,
25548 &setlist, &showlist);
25549
f1e6e072
TT
25550 dwarf2_locexpr_index = register_symbol_computed_impl (LOC_COMPUTED,
25551 &dwarf2_locexpr_funcs);
25552 dwarf2_loclist_index = register_symbol_computed_impl (LOC_COMPUTED,
25553 &dwarf2_loclist_funcs);
25554
25555 dwarf2_locexpr_block_index = register_symbol_block_impl (LOC_BLOCK,
25556 &dwarf2_block_frame_base_locexpr_funcs);
25557 dwarf2_loclist_block_index = register_symbol_block_impl (LOC_BLOCK,
25558 &dwarf2_block_frame_base_loclist_funcs);
c62446b1
PA
25559
25560#if GDB_SELF_TEST
25561 selftests::register_test ("dw2_expand_symtabs_matching",
25562 selftests::dw2_expand_symtabs_matching::run_test);
25563#endif
6502dd73 25564}
This page took 4.585241 seconds and 4 git commands to generate.