Convert the DWARF reader to new-style buildysm
[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"
804d2729 40#include "buildsym.h"
c906108c 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{
22ca247e
TT
150 mapped_index_base () = default;
151 DISABLE_COPY_AND_ASSIGN (mapped_index_base);
152
44ed8f3e
PA
153 /* The name_component table (a sorted vector). See name_component's
154 description above. */
155 std::vector<name_component> name_components;
156
157 /* How NAME_COMPONENTS is sorted. */
158 enum case_sensitivity name_components_casing;
159
160 /* Return the number of names in the symbol table. */
161 virtual size_t symbol_name_count () const = 0;
162
163 /* Get the name of the symbol at IDX in the symbol table. */
164 virtual const char *symbol_name_at (offset_type idx) const = 0;
165
166 /* Return whether the name at IDX in the symbol table should be
167 ignored. */
168 virtual bool symbol_name_slot_invalid (offset_type idx) const
169 {
170 return false;
171 }
172
173 /* Build the symbol name component sorted vector, if we haven't
174 yet. */
175 void build_name_components ();
176
177 /* Returns the lower (inclusive) and upper (exclusive) bounds of the
178 possible matches for LN_NO_PARAMS in the name component
179 vector. */
180 std::pair<std::vector<name_component>::const_iterator,
181 std::vector<name_component>::const_iterator>
182 find_name_components_bounds (const lookup_name_info &ln_no_params) const;
183
184 /* Prevent deleting/destroying via a base class pointer. */
185protected:
186 ~mapped_index_base() = default;
187};
188
9291a0cd
TT
189/* A description of the mapped index. The file format is described in
190 a comment by the code that writes the index. */
fc898b42 191struct mapped_index final : public mapped_index_base
9291a0cd 192{
f00a2de2
PA
193 /* A slot/bucket in the symbol table hash. */
194 struct symbol_table_slot
195 {
196 const offset_type name;
197 const offset_type vec;
198 };
199
559a7a62 200 /* Index data format version. */
3063847f 201 int version = 0;
559a7a62 202
f00a2de2
PA
203 /* The address table data. */
204 gdb::array_view<const gdb_byte> address_table;
b11b1f88 205
3876f04e 206 /* The symbol table, implemented as a hash table. */
f00a2de2 207 gdb::array_view<symbol_table_slot> symbol_table;
b11b1f88 208
9291a0cd 209 /* A pointer to the constant pool. */
3063847f 210 const char *constant_pool = nullptr;
3f563c84 211
44ed8f3e
PA
212 bool symbol_name_slot_invalid (offset_type idx) const override
213 {
214 const auto &bucket = this->symbol_table[idx];
215 return bucket.name == 0 && bucket.vec;
216 }
5c58de74 217
3f563c84
PA
218 /* Convenience method to get at the name of the symbol at IDX in the
219 symbol table. */
44ed8f3e 220 const char *symbol_name_at (offset_type idx) const override
f00a2de2 221 { return this->constant_pool + MAYBE_SWAP (this->symbol_table[idx].name); }
5c58de74 222
44ed8f3e
PA
223 size_t symbol_name_count () const override
224 { return this->symbol_table.size (); }
9291a0cd
TT
225};
226
927aa2e7
JK
227/* A description of the mapped .debug_names.
228 Uninitialized map has CU_COUNT 0. */
fc898b42 229struct mapped_debug_names final : public mapped_index_base
927aa2e7 230{
ed2dc618
SM
231 mapped_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile_)
232 : dwarf2_per_objfile (dwarf2_per_objfile_)
233 {}
234
235 struct dwarf2_per_objfile *dwarf2_per_objfile;
927aa2e7
JK
236 bfd_endian dwarf5_byte_order;
237 bool dwarf5_is_dwarf64;
238 bool augmentation_is_gdb;
239 uint8_t offset_size;
240 uint32_t cu_count = 0;
241 uint32_t tu_count, bucket_count, name_count;
242 const gdb_byte *cu_table_reordered, *tu_table_reordered;
243 const uint32_t *bucket_table_reordered, *hash_table_reordered;
244 const gdb_byte *name_table_string_offs_reordered;
245 const gdb_byte *name_table_entry_offs_reordered;
246 const gdb_byte *entry_pool;
247
248 struct index_val
249 {
250 ULONGEST dwarf_tag;
251 struct attr
252 {
253 /* Attribute name DW_IDX_*. */
254 ULONGEST dw_idx;
255
256 /* Attribute form DW_FORM_*. */
257 ULONGEST form;
258
259 /* Value if FORM is DW_FORM_implicit_const. */
260 LONGEST implicit_const;
261 };
262 std::vector<attr> attr_vec;
263 };
264
265 std::unordered_map<ULONGEST, index_val> abbrev_map;
266
267 const char *namei_to_name (uint32_t namei) const;
44ed8f3e
PA
268
269 /* Implementation of the mapped_index_base virtual interface, for
270 the name_components cache. */
271
272 const char *symbol_name_at (offset_type idx) const override
273 { return namei_to_name (idx); }
274
275 size_t symbol_name_count () const override
276 { return this->name_count; }
927aa2e7
JK
277};
278
cd4fb1b2 279/* See dwarf2read.h. */
ed2dc618 280
cd4fb1b2 281dwarf2_per_objfile *
ed2dc618
SM
282get_dwarf2_per_objfile (struct objfile *objfile)
283{
284 return ((struct dwarf2_per_objfile *)
285 objfile_data (objfile, dwarf2_objfile_data_key));
286}
287
288/* Set the dwarf2_per_objfile associated to OBJFILE. */
289
290void
291set_dwarf2_per_objfile (struct objfile *objfile,
292 struct dwarf2_per_objfile *dwarf2_per_objfile)
293{
294 gdb_assert (get_dwarf2_per_objfile (objfile) == NULL);
295 set_objfile_data (objfile, dwarf2_objfile_data_key, dwarf2_per_objfile);
296}
c906108c 297
251d32d9 298/* Default names of the debugging sections. */
c906108c 299
233a11ab
CS
300/* Note that if the debugging section has been compressed, it might
301 have a name like .zdebug_info. */
302
9cdd5dbd
DE
303static const struct dwarf2_debug_sections dwarf2_elf_names =
304{
251d32d9
TG
305 { ".debug_info", ".zdebug_info" },
306 { ".debug_abbrev", ".zdebug_abbrev" },
307 { ".debug_line", ".zdebug_line" },
308 { ".debug_loc", ".zdebug_loc" },
43988095 309 { ".debug_loclists", ".zdebug_loclists" },
251d32d9 310 { ".debug_macinfo", ".zdebug_macinfo" },
cf2c3c16 311 { ".debug_macro", ".zdebug_macro" },
251d32d9 312 { ".debug_str", ".zdebug_str" },
43988095 313 { ".debug_line_str", ".zdebug_line_str" },
251d32d9 314 { ".debug_ranges", ".zdebug_ranges" },
43988095 315 { ".debug_rnglists", ".zdebug_rnglists" },
251d32d9 316 { ".debug_types", ".zdebug_types" },
3019eac3 317 { ".debug_addr", ".zdebug_addr" },
251d32d9
TG
318 { ".debug_frame", ".zdebug_frame" },
319 { ".eh_frame", NULL },
24d3216f 320 { ".gdb_index", ".zgdb_index" },
927aa2e7
JK
321 { ".debug_names", ".zdebug_names" },
322 { ".debug_aranges", ".zdebug_aranges" },
24d3216f 323 23
251d32d9 324};
c906108c 325
80626a55 326/* List of DWO/DWP sections. */
3019eac3 327
80626a55 328static const struct dwop_section_names
3019eac3
DE
329{
330 struct dwarf2_section_names abbrev_dwo;
331 struct dwarf2_section_names info_dwo;
332 struct dwarf2_section_names line_dwo;
333 struct dwarf2_section_names loc_dwo;
43988095 334 struct dwarf2_section_names loclists_dwo;
09262596
DE
335 struct dwarf2_section_names macinfo_dwo;
336 struct dwarf2_section_names macro_dwo;
3019eac3
DE
337 struct dwarf2_section_names str_dwo;
338 struct dwarf2_section_names str_offsets_dwo;
339 struct dwarf2_section_names types_dwo;
80626a55
DE
340 struct dwarf2_section_names cu_index;
341 struct dwarf2_section_names tu_index;
3019eac3 342}
80626a55 343dwop_section_names =
3019eac3
DE
344{
345 { ".debug_abbrev.dwo", ".zdebug_abbrev.dwo" },
346 { ".debug_info.dwo", ".zdebug_info.dwo" },
347 { ".debug_line.dwo", ".zdebug_line.dwo" },
348 { ".debug_loc.dwo", ".zdebug_loc.dwo" },
43988095 349 { ".debug_loclists.dwo", ".zdebug_loclists.dwo" },
09262596
DE
350 { ".debug_macinfo.dwo", ".zdebug_macinfo.dwo" },
351 { ".debug_macro.dwo", ".zdebug_macro.dwo" },
3019eac3
DE
352 { ".debug_str.dwo", ".zdebug_str.dwo" },
353 { ".debug_str_offsets.dwo", ".zdebug_str_offsets.dwo" },
354 { ".debug_types.dwo", ".zdebug_types.dwo" },
80626a55
DE
355 { ".debug_cu_index", ".zdebug_cu_index" },
356 { ".debug_tu_index", ".zdebug_tu_index" },
3019eac3
DE
357};
358
c906108c
SS
359/* local data types */
360
107d2387
AC
361/* The data in a compilation unit header, after target2host
362 translation, looks like this. */
c906108c 363struct comp_unit_head
a738430d 364{
c764a876 365 unsigned int length;
a738430d 366 short version;
a738430d
MK
367 unsigned char addr_size;
368 unsigned char signed_addr_p;
9c541725 369 sect_offset abbrev_sect_off;
57349743 370
a738430d
MK
371 /* Size of file offsets; either 4 or 8. */
372 unsigned int offset_size;
57349743 373
a738430d
MK
374 /* Size of the length field; either 4 or 12. */
375 unsigned int initial_length_size;
57349743 376
43988095
JK
377 enum dwarf_unit_type unit_type;
378
a738430d
MK
379 /* Offset to the first byte of this compilation unit header in the
380 .debug_info section, for resolving relative reference dies. */
9c541725 381 sect_offset sect_off;
57349743 382
d00adf39
DE
383 /* Offset to first die in this cu from the start of the cu.
384 This will be the first byte following the compilation unit header. */
9c541725 385 cu_offset first_die_cu_offset;
43988095
JK
386
387 /* 64-bit signature of this type unit - it is valid only for
388 UNIT_TYPE DW_UT_type. */
389 ULONGEST signature;
390
391 /* For types, offset in the type's DIE of the type defined by this TU. */
9c541725 392 cu_offset type_cu_offset_in_tu;
a738430d 393};
c906108c 394
3da10d80
KS
395/* Type used for delaying computation of method physnames.
396 See comments for compute_delayed_physnames. */
397struct delayed_method_info
398{
399 /* The type to which the method is attached, i.e., its parent class. */
400 struct type *type;
401
402 /* The index of the method in the type's function fieldlists. */
403 int fnfield_index;
404
405 /* The index of the method in the fieldlist. */
406 int index;
407
408 /* The name of the DIE. */
409 const char *name;
410
411 /* The DIE associated with this method. */
412 struct die_info *die;
413};
414
e7c27a73
DJ
415/* Internal state when decoding a particular compilation unit. */
416struct dwarf2_cu
417{
fcd3b13d
SM
418 explicit dwarf2_cu (struct dwarf2_per_cu_data *per_cu);
419 ~dwarf2_cu ();
420
421 DISABLE_COPY_AND_ASSIGN (dwarf2_cu);
422
d00adf39 423 /* The header of the compilation unit. */
fcd3b13d 424 struct comp_unit_head header {};
e142c38c 425
d00adf39 426 /* Base address of this compilation unit. */
fcd3b13d 427 CORE_ADDR base_address = 0;
d00adf39
DE
428
429 /* Non-zero if base_address has been set. */
fcd3b13d 430 int base_known = 0;
d00adf39 431
e142c38c 432 /* The language we are debugging. */
fcd3b13d
SM
433 enum language language = language_unknown;
434 const struct language_defn *language_defn = nullptr;
e142c38c 435
fcd3b13d 436 const char *producer = nullptr;
b0f35d58 437
804d2729
TT
438 /* The symtab builder for this CU. This is only non-NULL when full
439 symbols are being read. */
440 std::unique_ptr<buildsym_compunit> builder;
441
e142c38c
DJ
442 /* The generic symbol table building routines have separate lists for
443 file scope symbols and all all other scopes (local scopes). So
444 we need to select the right one to pass to add_symbol_to_list().
445 We do it by keeping a pointer to the correct list in list_in_scope.
446
447 FIXME: The original dwarf code just treated the file scope as the
448 first local scope, and all other local scopes as nested local
449 scopes, and worked fine. Check to see if we really need to
450 distinguish these in buildsym.c. */
fcd3b13d 451 struct pending **list_in_scope = nullptr;
e142c38c 452
b64f50a1
JK
453 /* Hash table holding all the loaded partial DIEs
454 with partial_die->offset.SECT_OFF as hash. */
fcd3b13d 455 htab_t partial_dies = nullptr;
72bf9492
DJ
456
457 /* Storage for things with the same lifetime as this read-in compilation
458 unit, including partial DIEs. */
fcd3b13d 459 auto_obstack comp_unit_obstack;
72bf9492 460
ae038cb0
DJ
461 /* When multiple dwarf2_cu structures are living in memory, this field
462 chains them all together, so that they can be released efficiently.
463 We will probably also want a generation counter so that most-recently-used
464 compilation units are cached... */
fcd3b13d 465 struct dwarf2_per_cu_data *read_in_chain = nullptr;
ae038cb0 466
69d751e3 467 /* Backlink to our per_cu entry. */
ae038cb0
DJ
468 struct dwarf2_per_cu_data *per_cu;
469
470 /* How many compilation units ago was this CU last referenced? */
fcd3b13d 471 int last_used = 0;
ae038cb0 472
b64f50a1
JK
473 /* A hash table of DIE cu_offset for following references with
474 die_info->offset.sect_off as hash. */
fcd3b13d 475 htab_t die_hash = nullptr;
10b3939b
DJ
476
477 /* Full DIEs if read in. */
fcd3b13d 478 struct die_info *dies = nullptr;
10b3939b
DJ
479
480 /* A set of pointers to dwarf2_per_cu_data objects for compilation
481 units referenced by this one. Only set during full symbol processing;
482 partial symbol tables do not have dependencies. */
fcd3b13d 483 htab_t dependencies = nullptr;
10b3939b 484
cb1df416 485 /* Header data from the line table, during full symbol processing. */
fcd3b13d 486 struct line_header *line_header = nullptr;
4c8aa72d
PA
487 /* Non-NULL if LINE_HEADER is owned by this DWARF_CU. Otherwise,
488 it's owned by dwarf2_per_objfile::line_header_hash. If non-NULL,
489 this is the DW_TAG_compile_unit die for this CU. We'll hold on
490 to the line header as long as this DIE is being processed. See
491 process_die_scope. */
fcd3b13d 492 die_info *line_header_die_owner = nullptr;
cb1df416 493
3da10d80
KS
494 /* A list of methods which need to have physnames computed
495 after all type information has been read. */
c89b44cd 496 std::vector<delayed_method_info> method_list;
3da10d80 497
96408a79 498 /* To be copied to symtab->call_site_htab. */
fcd3b13d 499 htab_t call_site_htab = nullptr;
96408a79 500
034e5797
DE
501 /* Non-NULL if this CU came from a DWO file.
502 There is an invariant here that is important to remember:
503 Except for attributes copied from the top level DIE in the "main"
504 (or "stub") file in preparation for reading the DWO file
505 (e.g., DW_AT_GNU_addr_base), we KISS: there is only *one* CU.
506 Either there isn't a DWO file (in which case this is NULL and the point
507 is moot), or there is and either we're not going to read it (in which
508 case this is NULL) or there is and we are reading it (in which case this
509 is non-NULL). */
fcd3b13d 510 struct dwo_unit *dwo_unit = nullptr;
3019eac3
DE
511
512 /* The DW_AT_addr_base attribute if present, zero otherwise
513 (zero is a valid value though).
1dbab08b 514 Note this value comes from the Fission stub CU/TU's DIE. */
fcd3b13d 515 ULONGEST addr_base = 0;
3019eac3 516
2e3cf129
DE
517 /* The DW_AT_ranges_base attribute if present, zero otherwise
518 (zero is a valid value though).
1dbab08b 519 Note this value comes from the Fission stub CU/TU's DIE.
2e3cf129 520 Also note that the value is zero in the non-DWO case so this value can
ab435259
DE
521 be used without needing to know whether DWO files are in use or not.
522 N.B. This does not apply to DW_AT_ranges appearing in
523 DW_TAG_compile_unit dies. This is a bit of a wart, consider if ever
524 DW_AT_ranges appeared in the DW_TAG_compile_unit of DWO DIEs: then
525 DW_AT_ranges_base *would* have to be applied, and we'd have to care
526 whether the DW_AT_ranges attribute came from the skeleton or DWO. */
fcd3b13d 527 ULONGEST ranges_base = 0;
2e3cf129 528
c9317f21
TT
529 /* When reading debug info generated by older versions of rustc, we
530 have to rewrite some union types to be struct types with a
531 variant part. This rewriting must be done after the CU is fully
532 read in, because otherwise at the point of rewriting some struct
533 type might not have been fully processed. So, we keep a list of
534 all such types here and process them after expansion. */
535 std::vector<struct type *> rust_unions;
536
ae038cb0
DJ
537 /* Mark used when releasing cached dies. */
538 unsigned int mark : 1;
539
8be455d7
JK
540 /* This CU references .debug_loc. See the symtab->locations_valid field.
541 This test is imperfect as there may exist optimized debug code not using
542 any location list and still facing inlining issues if handled as
543 unoptimized code. For a future better test see GCC PR other/32998. */
8be455d7 544 unsigned int has_loclist : 1;
ba919b58 545
1b80a9fa
JK
546 /* These cache the results for producer_is_* fields. CHECKED_PRODUCER is set
547 if all the producer_is_* fields are valid. This information is cached
548 because profiling CU expansion showed excessive time spent in
549 producer_is_gxx_lt_4_6. */
ba919b58
TT
550 unsigned int checked_producer : 1;
551 unsigned int producer_is_gxx_lt_4_6 : 1;
1b80a9fa 552 unsigned int producer_is_gcc_lt_4_3 : 1;
5230b05a 553 unsigned int producer_is_icc_lt_14 : 1;
4d4ec4e5
TT
554
555 /* When set, the file that we're processing is known to have
556 debugging info for C++ namespaces. GCC 3.3.x did not produce
557 this information, but later versions do. */
558
559 unsigned int processing_has_namespace_info : 1;
d590ff25
YQ
560
561 struct partial_die_info *find_partial_die (sect_offset sect_off);
e7c27a73
DJ
562};
563
094b34ac
DE
564/* A struct that can be used as a hash key for tables based on DW_AT_stmt_list.
565 This includes type_unit_group and quick_file_names. */
566
567struct stmt_list_hash
568{
569 /* The DWO unit this table is from or NULL if there is none. */
570 struct dwo_unit *dwo_unit;
571
572 /* Offset in .debug_line or .debug_line.dwo. */
9c541725 573 sect_offset line_sect_off;
094b34ac
DE
574};
575
f4dc4d17
DE
576/* Each element of dwarf2_per_objfile->type_unit_groups is a pointer to
577 an object of this type. */
578
579struct type_unit_group
580{
0186c6a7 581 /* dwarf2read.c's main "handle" on a TU symtab.
f4dc4d17
DE
582 To simplify things we create an artificial CU that "includes" all the
583 type units using this stmt_list so that the rest of the code still has
584 a "per_cu" handle on the symtab.
585 This PER_CU is recognized by having no section. */
8a0459fd 586#define IS_TYPE_UNIT_GROUP(per_cu) ((per_cu)->section == NULL)
094b34ac
DE
587 struct dwarf2_per_cu_data per_cu;
588
0186c6a7
DE
589 /* The TUs that share this DW_AT_stmt_list entry.
590 This is added to while parsing type units to build partial symtabs,
591 and is deleted afterwards and not used again. */
592 VEC (sig_type_ptr) *tus;
f4dc4d17 593
43f3e411 594 /* The compunit symtab.
094b34ac 595 Type units in a group needn't all be defined in the same source file,
43f3e411
DE
596 so we create an essentially anonymous symtab as the compunit symtab. */
597 struct compunit_symtab *compunit_symtab;
f4dc4d17 598
094b34ac
DE
599 /* The data used to construct the hash key. */
600 struct stmt_list_hash hash;
f4dc4d17
DE
601
602 /* The number of symtabs from the line header.
603 The value here must match line_header.num_file_names. */
604 unsigned int num_symtabs;
605
606 /* The symbol tables for this TU (obtained from the files listed in
607 DW_AT_stmt_list).
608 WARNING: The order of entries here must match the order of entries
609 in the line header. After the first TU using this type_unit_group, the
610 line header for the subsequent TUs is recreated from this. This is done
611 because we need to use the same symtabs for each TU using the same
612 DW_AT_stmt_list value. Also note that symtabs may be repeated here,
613 there's no guarantee the line header doesn't have duplicate entries. */
614 struct symtab **symtabs;
615};
616
73869dc2 617/* These sections are what may appear in a (real or virtual) DWO file. */
3019eac3
DE
618
619struct dwo_sections
620{
621 struct dwarf2_section_info abbrev;
3019eac3
DE
622 struct dwarf2_section_info line;
623 struct dwarf2_section_info loc;
43988095 624 struct dwarf2_section_info loclists;
09262596
DE
625 struct dwarf2_section_info macinfo;
626 struct dwarf2_section_info macro;
3019eac3
DE
627 struct dwarf2_section_info str;
628 struct dwarf2_section_info str_offsets;
80626a55
DE
629 /* In the case of a virtual DWO file, these two are unused. */
630 struct dwarf2_section_info info;
3019eac3
DE
631 VEC (dwarf2_section_info_def) *types;
632};
633
c88ee1f0 634/* CUs/TUs in DWP/DWO files. */
3019eac3
DE
635
636struct dwo_unit
637{
638 /* Backlink to the containing struct dwo_file. */
639 struct dwo_file *dwo_file;
640
641 /* The "id" that distinguishes this CU/TU.
642 .debug_info calls this "dwo_id", .debug_types calls this "signature".
643 Since signatures came first, we stick with it for consistency. */
644 ULONGEST signature;
645
646 /* The section this CU/TU lives in, in the DWO file. */
8a0459fd 647 struct dwarf2_section_info *section;
3019eac3 648
9c541725
PA
649 /* Same as dwarf2_per_cu_data:{sect_off,length} but in the DWO section. */
650 sect_offset sect_off;
3019eac3
DE
651 unsigned int length;
652
653 /* For types, offset in the type's DIE of the type defined by this TU. */
654 cu_offset type_offset_in_tu;
655};
656
73869dc2
DE
657/* include/dwarf2.h defines the DWP section codes.
658 It defines a max value but it doesn't define a min value, which we
659 use for error checking, so provide one. */
660
661enum dwp_v2_section_ids
662{
663 DW_SECT_MIN = 1
664};
665
80626a55 666/* Data for one DWO file.
57d63ce2
DE
667
668 This includes virtual DWO files (a virtual DWO file is a DWO file as it
669 appears in a DWP file). DWP files don't really have DWO files per se -
670 comdat folding of types "loses" the DWO file they came from, and from
671 a high level view DWP files appear to contain a mass of random types.
672 However, to maintain consistency with the non-DWP case we pretend DWP
673 files contain virtual DWO files, and we assign each TU with one virtual
674 DWO file (generally based on the line and abbrev section offsets -
675 a heuristic that seems to work in practice). */
3019eac3
DE
676
677struct dwo_file
678{
0ac5b59e 679 /* The DW_AT_GNU_dwo_name attribute.
80626a55
DE
680 For virtual DWO files the name is constructed from the section offsets
681 of abbrev,line,loc,str_offsets so that we combine virtual DWO files
682 from related CU+TUs. */
0ac5b59e
DE
683 const char *dwo_name;
684
685 /* The DW_AT_comp_dir attribute. */
686 const char *comp_dir;
3019eac3 687
80626a55
DE
688 /* The bfd, when the file is open. Otherwise this is NULL.
689 This is unused(NULL) for virtual DWO files where we use dwp_file.dbfd. */
690 bfd *dbfd;
3019eac3 691
73869dc2
DE
692 /* The sections that make up this DWO file.
693 Remember that for virtual DWO files in DWP V2, these are virtual
694 sections (for lack of a better name). */
3019eac3
DE
695 struct dwo_sections sections;
696
33c5cd75
DB
697 /* The CUs in the file.
698 Each element is a struct dwo_unit. Multiple CUs per DWO are supported as
699 an extension to handle LLVM's Link Time Optimization output (where
700 multiple source files may be compiled into a single object/dwo pair). */
701 htab_t cus;
3019eac3
DE
702
703 /* Table of TUs in the file.
704 Each element is a struct dwo_unit. */
705 htab_t tus;
706};
707
80626a55
DE
708/* These sections are what may appear in a DWP file. */
709
710struct dwp_sections
711{
73869dc2 712 /* These are used by both DWP version 1 and 2. */
80626a55
DE
713 struct dwarf2_section_info str;
714 struct dwarf2_section_info cu_index;
715 struct dwarf2_section_info tu_index;
73869dc2
DE
716
717 /* These are only used by DWP version 2 files.
718 In DWP version 1 the .debug_info.dwo, .debug_types.dwo, and other
719 sections are referenced by section number, and are not recorded here.
720 In DWP version 2 there is at most one copy of all these sections, each
721 section being (effectively) comprised of the concatenation of all of the
722 individual sections that exist in the version 1 format.
723 To keep the code simple we treat each of these concatenated pieces as a
724 section itself (a virtual section?). */
725 struct dwarf2_section_info abbrev;
726 struct dwarf2_section_info info;
727 struct dwarf2_section_info line;
728 struct dwarf2_section_info loc;
729 struct dwarf2_section_info macinfo;
730 struct dwarf2_section_info macro;
731 struct dwarf2_section_info str_offsets;
732 struct dwarf2_section_info types;
80626a55
DE
733};
734
73869dc2
DE
735/* These sections are what may appear in a virtual DWO file in DWP version 1.
736 A virtual DWO file is a DWO file as it appears in a DWP file. */
80626a55 737
73869dc2 738struct virtual_v1_dwo_sections
80626a55
DE
739{
740 struct dwarf2_section_info abbrev;
741 struct dwarf2_section_info line;
742 struct dwarf2_section_info loc;
743 struct dwarf2_section_info macinfo;
744 struct dwarf2_section_info macro;
745 struct dwarf2_section_info str_offsets;
746 /* Each DWP hash table entry records one CU or one TU.
8a0459fd 747 That is recorded here, and copied to dwo_unit.section. */
80626a55
DE
748 struct dwarf2_section_info info_or_types;
749};
750
73869dc2
DE
751/* Similar to virtual_v1_dwo_sections, but for DWP version 2.
752 In version 2, the sections of the DWO files are concatenated together
753 and stored in one section of that name. Thus each ELF section contains
754 several "virtual" sections. */
755
756struct virtual_v2_dwo_sections
757{
758 bfd_size_type abbrev_offset;
759 bfd_size_type abbrev_size;
760
761 bfd_size_type line_offset;
762 bfd_size_type line_size;
763
764 bfd_size_type loc_offset;
765 bfd_size_type loc_size;
766
767 bfd_size_type macinfo_offset;
768 bfd_size_type macinfo_size;
769
770 bfd_size_type macro_offset;
771 bfd_size_type macro_size;
772
773 bfd_size_type str_offsets_offset;
774 bfd_size_type str_offsets_size;
775
776 /* Each DWP hash table entry records one CU or one TU.
777 That is recorded here, and copied to dwo_unit.section. */
778 bfd_size_type info_or_types_offset;
779 bfd_size_type info_or_types_size;
780};
781
80626a55
DE
782/* Contents of DWP hash tables. */
783
784struct dwp_hash_table
785{
73869dc2 786 uint32_t version, nr_columns;
80626a55 787 uint32_t nr_units, nr_slots;
73869dc2
DE
788 const gdb_byte *hash_table, *unit_table;
789 union
790 {
791 struct
792 {
793 const gdb_byte *indices;
794 } v1;
795 struct
796 {
797 /* This is indexed by column number and gives the id of the section
798 in that column. */
799#define MAX_NR_V2_DWO_SECTIONS \
800 (1 /* .debug_info or .debug_types */ \
801 + 1 /* .debug_abbrev */ \
802 + 1 /* .debug_line */ \
803 + 1 /* .debug_loc */ \
804 + 1 /* .debug_str_offsets */ \
805 + 1 /* .debug_macro or .debug_macinfo */)
806 int section_ids[MAX_NR_V2_DWO_SECTIONS];
807 const gdb_byte *offsets;
808 const gdb_byte *sizes;
809 } v2;
810 } section_pool;
80626a55
DE
811};
812
813/* Data for one DWP file. */
814
815struct dwp_file
816{
400174b1
TT
817 dwp_file (const char *name_, gdb_bfd_ref_ptr &&abfd)
818 : name (name_),
819 dbfd (std::move (abfd))
820 {
821 }
822
80626a55
DE
823 /* Name of the file. */
824 const char *name;
825
73869dc2 826 /* File format version. */
400174b1 827 int version = 0;
73869dc2 828
93417882 829 /* The bfd. */
400174b1 830 gdb_bfd_ref_ptr dbfd;
80626a55
DE
831
832 /* Section info for this file. */
400174b1 833 struct dwp_sections sections {};
80626a55 834
57d63ce2 835 /* Table of CUs in the file. */
400174b1 836 const struct dwp_hash_table *cus = nullptr;
80626a55
DE
837
838 /* Table of TUs in the file. */
400174b1 839 const struct dwp_hash_table *tus = nullptr;
80626a55 840
19ac8c2e 841 /* Tables of loaded CUs/TUs. Each entry is a struct dwo_unit *. */
400174b1
TT
842 htab_t loaded_cus {};
843 htab_t loaded_tus {};
80626a55 844
73869dc2
DE
845 /* Table to map ELF section numbers to their sections.
846 This is only needed for the DWP V1 file format. */
400174b1
TT
847 unsigned int num_sections = 0;
848 asection **elf_sections = nullptr;
80626a55
DE
849};
850
36586728
TT
851/* This represents a '.dwz' file. */
852
853struct dwz_file
854{
7ff8cb8c
TT
855 dwz_file (gdb_bfd_ref_ptr &&bfd)
856 : dwz_bfd (std::move (bfd))
857 {
858 }
859
36586728 860 /* A dwz file can only contain a few sections. */
7ff8cb8c
TT
861 struct dwarf2_section_info abbrev {};
862 struct dwarf2_section_info info {};
863 struct dwarf2_section_info str {};
864 struct dwarf2_section_info line {};
865 struct dwarf2_section_info macro {};
866 struct dwarf2_section_info gdb_index {};
867 struct dwarf2_section_info debug_names {};
36586728
TT
868
869 /* The dwz's BFD. */
7ff8cb8c 870 gdb_bfd_ref_ptr dwz_bfd;
36586728
TT
871};
872
0963b4bd
MS
873/* Struct used to pass misc. parameters to read_die_and_children, et
874 al. which are used for both .debug_info and .debug_types dies.
875 All parameters here are unchanging for the life of the call. This
dee91e82 876 struct exists to abstract away the constant parameters of die reading. */
93311388
DE
877
878struct die_reader_specs
879{
a32a8923 880 /* The bfd of die_section. */
93311388
DE
881 bfd* abfd;
882
883 /* The CU of the DIE we are parsing. */
884 struct dwarf2_cu *cu;
885
80626a55 886 /* Non-NULL if reading a DWO file (including one packaged into a DWP). */
3019eac3
DE
887 struct dwo_file *dwo_file;
888
dee91e82 889 /* The section the die comes from.
3019eac3 890 This is either .debug_info or .debug_types, or the .dwo variants. */
dee91e82
DE
891 struct dwarf2_section_info *die_section;
892
893 /* die_section->buffer. */
d521ce57 894 const gdb_byte *buffer;
f664829e
DE
895
896 /* The end of the buffer. */
897 const gdb_byte *buffer_end;
a2ce51a0
DE
898
899 /* The value of the DW_AT_comp_dir attribute. */
900 const char *comp_dir;
685af9cd
TT
901
902 /* The abbreviation table to use when reading the DIEs. */
903 struct abbrev_table *abbrev_table;
93311388
DE
904};
905
fd820528 906/* Type of function passed to init_cutu_and_read_dies, et.al. */
dee91e82 907typedef void (die_reader_func_ftype) (const struct die_reader_specs *reader,
d521ce57 908 const gdb_byte *info_ptr,
dee91e82
DE
909 struct die_info *comp_unit_die,
910 int has_children,
911 void *data);
912
ecfb656c
PA
913/* A 1-based directory index. This is a strong typedef to prevent
914 accidentally using a directory index as a 0-based index into an
915 array/vector. */
916enum class dir_index : unsigned int {};
917
918/* Likewise, a 1-based file name index. */
919enum class file_name_index : unsigned int {};
920
52059ffd
TT
921struct file_entry
922{
fff8551c
PA
923 file_entry () = default;
924
ecfb656c 925 file_entry (const char *name_, dir_index d_index_,
fff8551c
PA
926 unsigned int mod_time_, unsigned int length_)
927 : name (name_),
ecfb656c 928 d_index (d_index_),
fff8551c
PA
929 mod_time (mod_time_),
930 length (length_)
931 {}
932
ecfb656c
PA
933 /* Return the include directory at D_INDEX stored in LH. Returns
934 NULL if D_INDEX is out of bounds. */
8c43009f
PA
935 const char *include_dir (const line_header *lh) const;
936
fff8551c
PA
937 /* The file name. Note this is an observing pointer. The memory is
938 owned by debug_line_buffer. */
939 const char *name {};
940
8c43009f 941 /* The directory index (1-based). */
ecfb656c 942 dir_index d_index {};
fff8551c
PA
943
944 unsigned int mod_time {};
945
946 unsigned int length {};
947
948 /* True if referenced by the Line Number Program. */
949 bool included_p {};
950
83769d0b 951 /* The associated symbol table, if any. */
fff8551c 952 struct symtab *symtab {};
52059ffd
TT
953};
954
debd256d
JB
955/* The line number information for a compilation unit (found in the
956 .debug_line section) begins with a "statement program header",
957 which contains the following information. */
958struct line_header
959{
fff8551c
PA
960 line_header ()
961 : offset_in_dwz {}
962 {}
963
964 /* Add an entry to the include directory table. */
965 void add_include_dir (const char *include_dir);
966
967 /* Add an entry to the file name table. */
ecfb656c 968 void add_file_name (const char *name, dir_index d_index,
fff8551c
PA
969 unsigned int mod_time, unsigned int length);
970
ecfb656c 971 /* Return the include dir at INDEX (1-based). Returns NULL if INDEX
8c43009f 972 is out of bounds. */
ecfb656c 973 const char *include_dir_at (dir_index index) const
8c43009f 974 {
ecfb656c
PA
975 /* Convert directory index number (1-based) to vector index
976 (0-based). */
977 size_t vec_index = to_underlying (index) - 1;
978
979 if (vec_index >= include_dirs.size ())
8c43009f 980 return NULL;
ecfb656c 981 return include_dirs[vec_index];
8c43009f
PA
982 }
983
ecfb656c 984 /* Return the file name at INDEX (1-based). Returns NULL if INDEX
8c43009f 985 is out of bounds. */
ecfb656c 986 file_entry *file_name_at (file_name_index index)
8c43009f 987 {
ecfb656c
PA
988 /* Convert file name index number (1-based) to vector index
989 (0-based). */
990 size_t vec_index = to_underlying (index) - 1;
991
992 if (vec_index >= file_names.size ())
fff8551c 993 return NULL;
ecfb656c 994 return &file_names[vec_index];
fff8551c
PA
995 }
996
997 /* Const version of the above. */
998 const file_entry *file_name_at (unsigned int index) const
999 {
1000 if (index >= file_names.size ())
8c43009f
PA
1001 return NULL;
1002 return &file_names[index];
1003 }
1004
527f3840 1005 /* Offset of line number information in .debug_line section. */
9c541725 1006 sect_offset sect_off {};
527f3840
JK
1007
1008 /* OFFSET is for struct dwz_file associated with dwarf2_per_objfile. */
fff8551c
PA
1009 unsigned offset_in_dwz : 1; /* Can't initialize bitfields in-class. */
1010
1011 unsigned int total_length {};
1012 unsigned short version {};
1013 unsigned int header_length {};
1014 unsigned char minimum_instruction_length {};
1015 unsigned char maximum_ops_per_instruction {};
1016 unsigned char default_is_stmt {};
1017 int line_base {};
1018 unsigned char line_range {};
1019 unsigned char opcode_base {};
debd256d
JB
1020
1021 /* standard_opcode_lengths[i] is the number of operands for the
1022 standard opcode whose value is i. This means that
1023 standard_opcode_lengths[0] is unused, and the last meaningful
1024 element is standard_opcode_lengths[opcode_base - 1]. */
fff8551c 1025 std::unique_ptr<unsigned char[]> standard_opcode_lengths;
debd256d 1026
fff8551c
PA
1027 /* The include_directories table. Note these are observing
1028 pointers. The memory is owned by debug_line_buffer. */
1029 std::vector<const char *> include_dirs;
debd256d 1030
fff8551c
PA
1031 /* The file_names table. */
1032 std::vector<file_entry> file_names;
debd256d
JB
1033
1034 /* The start and end of the statement program following this
6502dd73 1035 header. These point into dwarf2_per_objfile->line_buffer. */
fff8551c 1036 const gdb_byte *statement_program_start {}, *statement_program_end {};
debd256d 1037};
c906108c 1038
fff8551c
PA
1039typedef std::unique_ptr<line_header> line_header_up;
1040
8c43009f
PA
1041const char *
1042file_entry::include_dir (const line_header *lh) const
1043{
ecfb656c 1044 return lh->include_dir_at (d_index);
8c43009f
PA
1045}
1046
c906108c 1047/* When we construct a partial symbol table entry we only
0963b4bd 1048 need this much information. */
6f06d47b 1049struct partial_die_info : public allocate_on_obstack
c906108c 1050 {
6f06d47b
YQ
1051 partial_die_info (sect_offset sect_off, struct abbrev_info *abbrev);
1052
1053 /* Disable assign but still keep copy ctor, which is needed
1054 load_partial_dies. */
1055 partial_die_info& operator=(const partial_die_info& rhs) = delete;
1056
52356b79
YQ
1057 /* Adjust the partial die before generating a symbol for it. This
1058 function may set the is_external flag or change the DIE's
1059 name. */
1060 void fixup (struct dwarf2_cu *cu);
1061
48fbe735
YQ
1062 /* Read a minimal amount of information into the minimal die
1063 structure. */
1064 const gdb_byte *read (const struct die_reader_specs *reader,
1065 const struct abbrev_info &abbrev,
1066 const gdb_byte *info_ptr);
1067
72bf9492 1068 /* Offset of this DIE. */
6f06d47b 1069 const sect_offset sect_off;
72bf9492
DJ
1070
1071 /* DWARF-2 tag for this DIE. */
6f06d47b 1072 const ENUM_BITFIELD(dwarf_tag) tag : 16;
72bf9492 1073
72bf9492 1074 /* Assorted flags describing the data found in this DIE. */
6f06d47b
YQ
1075 const unsigned int has_children : 1;
1076
72bf9492
DJ
1077 unsigned int is_external : 1;
1078 unsigned int is_declaration : 1;
1079 unsigned int has_type : 1;
1080 unsigned int has_specification : 1;
1081 unsigned int has_pc_info : 1;
481860b3 1082 unsigned int may_be_inlined : 1;
72bf9492 1083
0c1b455e
TT
1084 /* This DIE has been marked DW_AT_main_subprogram. */
1085 unsigned int main_subprogram : 1;
1086
72bf9492
DJ
1087 /* Flag set if the SCOPE field of this structure has been
1088 computed. */
1089 unsigned int scope_set : 1;
1090
fa4028e9
JB
1091 /* Flag set if the DIE has a byte_size attribute. */
1092 unsigned int has_byte_size : 1;
1093
ff908ebf
AW
1094 /* Flag set if the DIE has a DW_AT_const_value attribute. */
1095 unsigned int has_const_value : 1;
1096
98bfdba5
PA
1097 /* Flag set if any of the DIE's children are template arguments. */
1098 unsigned int has_template_arguments : 1;
1099
52356b79 1100 /* Flag set if fixup has been called on this die. */
abc72ce4
DE
1101 unsigned int fixup_called : 1;
1102
36586728
TT
1103 /* Flag set if DW_TAG_imported_unit uses DW_FORM_GNU_ref_alt. */
1104 unsigned int is_dwz : 1;
1105
1106 /* Flag set if spec_offset uses DW_FORM_GNU_ref_alt. */
1107 unsigned int spec_is_dwz : 1;
1108
72bf9492 1109 /* The name of this DIE. Normally the value of DW_AT_name, but
94af9270 1110 sometimes a default name for unnamed DIEs. */
6f06d47b 1111 const char *name = nullptr;
72bf9492 1112
abc72ce4 1113 /* The linkage name, if present. */
6f06d47b 1114 const char *linkage_name = nullptr;
abc72ce4 1115
72bf9492
DJ
1116 /* The scope to prepend to our children. This is generally
1117 allocated on the comp_unit_obstack, so will disappear
1118 when this compilation unit leaves the cache. */
6f06d47b 1119 const char *scope = nullptr;
72bf9492 1120
95554aad
TT
1121 /* Some data associated with the partial DIE. The tag determines
1122 which field is live. */
1123 union
1124 {
1125 /* The location description associated with this DIE, if any. */
1126 struct dwarf_block *locdesc;
1127 /* The offset of an import, for DW_TAG_imported_unit. */
9c541725 1128 sect_offset sect_off;
6f06d47b 1129 } d {};
72bf9492
DJ
1130
1131 /* If HAS_PC_INFO, the PC range associated with this DIE. */
6f06d47b
YQ
1132 CORE_ADDR lowpc = 0;
1133 CORE_ADDR highpc = 0;
72bf9492 1134
93311388 1135 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
72bf9492 1136 DW_AT_sibling, if any. */
48fbe735
YQ
1137 /* NOTE: This member isn't strictly necessary, partial_die_info::read
1138 could return DW_AT_sibling values to its caller load_partial_dies. */
6f06d47b 1139 const gdb_byte *sibling = nullptr;
72bf9492
DJ
1140
1141 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
1142 DW_AT_specification (or DW_AT_abstract_origin or
1143 DW_AT_extension). */
6f06d47b 1144 sect_offset spec_offset {};
72bf9492
DJ
1145
1146 /* Pointers to this DIE's parent, first child, and next sibling,
1147 if any. */
6f06d47b
YQ
1148 struct partial_die_info *die_parent = nullptr;
1149 struct partial_die_info *die_child = nullptr;
1150 struct partial_die_info *die_sibling = nullptr;
1151
1152 friend struct partial_die_info *
1153 dwarf2_cu::find_partial_die (sect_offset sect_off);
1154
1155 private:
1156 /* Only need to do look up in dwarf2_cu::find_partial_die. */
1157 partial_die_info (sect_offset sect_off)
1158 : partial_die_info (sect_off, DW_TAG_padding, 0)
1159 {
1160 }
1161
1162 partial_die_info (sect_offset sect_off_, enum dwarf_tag tag_,
1163 int has_children_)
1164 : sect_off (sect_off_), tag (tag_), has_children (has_children_)
1165 {
1166 is_external = 0;
1167 is_declaration = 0;
1168 has_type = 0;
1169 has_specification = 0;
1170 has_pc_info = 0;
1171 may_be_inlined = 0;
1172 main_subprogram = 0;
1173 scope_set = 0;
1174 has_byte_size = 0;
1175 has_const_value = 0;
1176 has_template_arguments = 0;
1177 fixup_called = 0;
1178 is_dwz = 0;
1179 spec_is_dwz = 0;
1180 }
c906108c
SS
1181 };
1182
0963b4bd 1183/* This data structure holds the information of an abbrev. */
c906108c
SS
1184struct abbrev_info
1185 {
1186 unsigned int number; /* number identifying abbrev */
1187 enum dwarf_tag tag; /* dwarf tag */
f3dd6933
DJ
1188 unsigned short has_children; /* boolean */
1189 unsigned short num_attrs; /* number of attributes */
c906108c
SS
1190 struct attr_abbrev *attrs; /* an array of attribute descriptions */
1191 struct abbrev_info *next; /* next in chain */
1192 };
1193
1194struct attr_abbrev
1195 {
9d25dd43
DE
1196 ENUM_BITFIELD(dwarf_attribute) name : 16;
1197 ENUM_BITFIELD(dwarf_form) form : 16;
43988095
JK
1198
1199 /* It is valid only if FORM is DW_FORM_implicit_const. */
1200 LONGEST implicit_const;
c906108c
SS
1201 };
1202
433df2d4
DE
1203/* Size of abbrev_table.abbrev_hash_table. */
1204#define ABBREV_HASH_SIZE 121
1205
1206/* Top level data structure to contain an abbreviation table. */
1207
1208struct abbrev_table
1209{
685af9cd
TT
1210 explicit abbrev_table (sect_offset off)
1211 : sect_off (off)
1212 {
4a17f768 1213 m_abbrevs =
685af9cd 1214 XOBNEWVEC (&abbrev_obstack, struct abbrev_info *, ABBREV_HASH_SIZE);
4a17f768 1215 memset (m_abbrevs, 0, ABBREV_HASH_SIZE * sizeof (struct abbrev_info *));
685af9cd
TT
1216 }
1217
1218 DISABLE_COPY_AND_ASSIGN (abbrev_table);
1219
1220 /* Allocate space for a struct abbrev_info object in
1221 ABBREV_TABLE. */
1222 struct abbrev_info *alloc_abbrev ();
1223
1224 /* Add an abbreviation to the table. */
1225 void add_abbrev (unsigned int abbrev_number, struct abbrev_info *abbrev);
1226
1227 /* Look up an abbrev in the table.
1228 Returns NULL if the abbrev is not found. */
1229
1230 struct abbrev_info *lookup_abbrev (unsigned int abbrev_number);
1231
1232
f4dc4d17
DE
1233 /* Where the abbrev table came from.
1234 This is used as a sanity check when the table is used. */
685af9cd 1235 const sect_offset sect_off;
433df2d4
DE
1236
1237 /* Storage for the abbrev table. */
685af9cd 1238 auto_obstack abbrev_obstack;
433df2d4 1239
4a17f768
YQ
1240private:
1241
433df2d4
DE
1242 /* Hash table of abbrevs.
1243 This is an array of size ABBREV_HASH_SIZE allocated in abbrev_obstack.
1244 It could be statically allocated, but the previous code didn't so we
1245 don't either. */
4a17f768 1246 struct abbrev_info **m_abbrevs;
433df2d4
DE
1247};
1248
685af9cd
TT
1249typedef std::unique_ptr<struct abbrev_table> abbrev_table_up;
1250
0963b4bd 1251/* Attributes have a name and a value. */
b60c80d6
DJ
1252struct attribute
1253 {
9d25dd43 1254 ENUM_BITFIELD(dwarf_attribute) name : 16;
8285870a
JK
1255 ENUM_BITFIELD(dwarf_form) form : 15;
1256
1257 /* Has DW_STRING already been updated by dwarf2_canonicalize_name? This
1258 field should be in u.str (existing only for DW_STRING) but it is kept
1259 here for better struct attribute alignment. */
1260 unsigned int string_is_canonical : 1;
1261
b60c80d6
DJ
1262 union
1263 {
15d034d0 1264 const char *str;
b60c80d6 1265 struct dwarf_block *blk;
43bbcdc2
PH
1266 ULONGEST unsnd;
1267 LONGEST snd;
b60c80d6 1268 CORE_ADDR addr;
ac9ec31b 1269 ULONGEST signature;
b60c80d6
DJ
1270 }
1271 u;
1272 };
1273
0963b4bd 1274/* This data structure holds a complete die structure. */
c906108c
SS
1275struct die_info
1276 {
76815b17
DE
1277 /* DWARF-2 tag for this DIE. */
1278 ENUM_BITFIELD(dwarf_tag) tag : 16;
1279
1280 /* Number of attributes */
98bfdba5
PA
1281 unsigned char num_attrs;
1282
1283 /* True if we're presently building the full type name for the
1284 type derived from this DIE. */
1285 unsigned char building_fullname : 1;
76815b17 1286
adde2bff
DE
1287 /* True if this die is in process. PR 16581. */
1288 unsigned char in_process : 1;
1289
76815b17
DE
1290 /* Abbrev number */
1291 unsigned int abbrev;
1292
93311388 1293 /* Offset in .debug_info or .debug_types section. */
9c541725 1294 sect_offset sect_off;
78ba4af6
JB
1295
1296 /* The dies in a compilation unit form an n-ary tree. PARENT
1297 points to this die's parent; CHILD points to the first child of
1298 this node; and all the children of a given node are chained
4950bc1c 1299 together via their SIBLING fields. */
639d11d3
DC
1300 struct die_info *child; /* Its first child, if any. */
1301 struct die_info *sibling; /* Its next sibling, if any. */
1302 struct die_info *parent; /* Its parent, if any. */
c906108c 1303
b60c80d6
DJ
1304 /* An array of attributes, with NUM_ATTRS elements. There may be
1305 zero, but it's not common and zero-sized arrays are not
1306 sufficiently portable C. */
1307 struct attribute attrs[1];
c906108c
SS
1308 };
1309
0963b4bd 1310/* Get at parts of an attribute structure. */
c906108c
SS
1311
1312#define DW_STRING(attr) ((attr)->u.str)
8285870a 1313#define DW_STRING_IS_CANONICAL(attr) ((attr)->string_is_canonical)
c906108c
SS
1314#define DW_UNSND(attr) ((attr)->u.unsnd)
1315#define DW_BLOCK(attr) ((attr)->u.blk)
1316#define DW_SND(attr) ((attr)->u.snd)
1317#define DW_ADDR(attr) ((attr)->u.addr)
ac9ec31b 1318#define DW_SIGNATURE(attr) ((attr)->u.signature)
c906108c 1319
0963b4bd 1320/* Blocks are a bunch of untyped bytes. */
c906108c
SS
1321struct dwarf_block
1322 {
56eb65bd 1323 size_t size;
1d6edc3c
JK
1324
1325 /* Valid only if SIZE is not zero. */
d521ce57 1326 const gdb_byte *data;
c906108c
SS
1327 };
1328
c906108c
SS
1329#ifndef ATTR_ALLOC_CHUNK
1330#define ATTR_ALLOC_CHUNK 4
1331#endif
1332
c906108c
SS
1333/* Allocate fields for structs, unions and enums in this size. */
1334#ifndef DW_FIELD_ALLOC_CHUNK
1335#define DW_FIELD_ALLOC_CHUNK 4
1336#endif
1337
c906108c
SS
1338/* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
1339 but this would require a corresponding change in unpack_field_as_long
1340 and friends. */
1341static int bits_per_byte = 8;
1342
2ddeaf8a
TT
1343/* When reading a variant or variant part, we track a bit more
1344 information about the field, and store it in an object of this
1345 type. */
1346
1347struct variant_field
1348{
1349 /* If we see a DW_TAG_variant, then this will be the discriminant
1350 value. */
1351 ULONGEST discriminant_value;
1352 /* If we see a DW_TAG_variant, then this will be set if this is the
1353 default branch. */
1354 bool default_branch;
1355 /* While reading a DW_TAG_variant_part, this will be set if this
1356 field is the discriminant. */
1357 bool is_discriminant;
1358};
1359
52059ffd
TT
1360struct nextfield
1361{
be2daae6
TT
1362 int accessibility = 0;
1363 int virtuality = 0;
2ddeaf8a 1364 /* Extra information to describe a variant or variant part. */
be2daae6
TT
1365 struct variant_field variant {};
1366 struct field field {};
52059ffd
TT
1367};
1368
1369struct fnfieldlist
1370{
be2daae6
TT
1371 const char *name = nullptr;
1372 std::vector<struct fn_field> fnfields;
52059ffd
TT
1373};
1374
c906108c
SS
1375/* The routines that read and process dies for a C struct or C++ class
1376 pass lists of data member fields and lists of member function fields
1377 in an instance of a field_info structure, as defined below. */
1378struct field_info
c5aa993b 1379 {
0963b4bd 1380 /* List of data member and baseclasses fields. */
be2daae6
TT
1381 std::vector<struct nextfield> fields;
1382 std::vector<struct nextfield> baseclasses;
c906108c 1383
7d0ccb61 1384 /* Number of fields (including baseclasses). */
be2daae6 1385 int nfields = 0;
c906108c 1386
c5aa993b 1387 /* Set if the accesibility of one of the fields is not public. */
be2daae6 1388 int non_public_fields = 0;
c906108c 1389
c5aa993b
JM
1390 /* Member function fieldlist array, contains name of possibly overloaded
1391 member function, number of overloaded member functions and a pointer
1392 to the head of the member function field chain. */
be2daae6 1393 std::vector<struct fnfieldlist> fnfieldlists;
98751a41
JK
1394
1395 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
1396 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
be2daae6 1397 std::vector<struct decl_field> typedef_field_list;
883fd55a
KS
1398
1399 /* Nested types defined by this class and the number of elements in this
1400 list. */
be2daae6 1401 std::vector<struct decl_field> nested_types_list;
c5aa993b 1402 };
c906108c 1403
10b3939b
DJ
1404/* One item on the queue of compilation units to read in full symbols
1405 for. */
1406struct dwarf2_queue_item
1407{
1408 struct dwarf2_per_cu_data *per_cu;
95554aad 1409 enum language pretend_language;
10b3939b
DJ
1410 struct dwarf2_queue_item *next;
1411};
1412
1413/* The current queue. */
1414static struct dwarf2_queue_item *dwarf2_queue, *dwarf2_queue_tail;
1415
ae038cb0
DJ
1416/* Loaded secondary compilation units are kept in memory until they
1417 have not been referenced for the processing of this many
1418 compilation units. Set this to zero to disable caching. Cache
1419 sizes of up to at least twenty will improve startup time for
1420 typical inter-CU-reference binaries, at an obvious memory cost. */
b4f54984 1421static int dwarf_max_cache_age = 5;
920d2a44 1422static void
b4f54984
DE
1423show_dwarf_max_cache_age (struct ui_file *file, int from_tty,
1424 struct cmd_list_element *c, const char *value)
920d2a44 1425{
3e43a32a 1426 fprintf_filtered (file, _("The upper bound on the age of cached "
b4f54984 1427 "DWARF compilation units is %s.\n"),
920d2a44
AC
1428 value);
1429}
4390d890 1430\f
c906108c
SS
1431/* local function prototypes */
1432
a32a8923
DE
1433static const char *get_section_name (const struct dwarf2_section_info *);
1434
1435static const char *get_section_file_name (const struct dwarf2_section_info *);
1436
918dd910
JK
1437static void dwarf2_find_base_address (struct die_info *die,
1438 struct dwarf2_cu *cu);
1439
0018ea6f
DE
1440static struct partial_symtab *create_partial_symtab
1441 (struct dwarf2_per_cu_data *per_cu, const char *name);
1442
f1902523
JK
1443static void build_type_psymtabs_reader (const struct die_reader_specs *reader,
1444 const gdb_byte *info_ptr,
1445 struct die_info *type_unit_die,
1446 int has_children, void *data);
1447
ed2dc618
SM
1448static void dwarf2_build_psymtabs_hard
1449 (struct dwarf2_per_objfile *dwarf2_per_objfile);
c906108c 1450
72bf9492
DJ
1451static void scan_partial_symbols (struct partial_die_info *,
1452 CORE_ADDR *, CORE_ADDR *,
5734ee8b 1453 int, struct dwarf2_cu *);
c906108c 1454
72bf9492
DJ
1455static void add_partial_symbol (struct partial_die_info *,
1456 struct dwarf2_cu *);
63d06c5c 1457
72bf9492
DJ
1458static void add_partial_namespace (struct partial_die_info *pdi,
1459 CORE_ADDR *lowpc, CORE_ADDR *highpc,
cdc07690 1460 int set_addrmap, struct dwarf2_cu *cu);
63d06c5c 1461
5d7cb8df 1462static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
cdc07690 1463 CORE_ADDR *highpc, int set_addrmap,
5d7cb8df
JK
1464 struct dwarf2_cu *cu);
1465
72bf9492
DJ
1466static void add_partial_enumeration (struct partial_die_info *enum_pdi,
1467 struct dwarf2_cu *cu);
91c24f0a 1468
bc30ff58
JB
1469static void add_partial_subprogram (struct partial_die_info *pdi,
1470 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 1471 int need_pc, struct dwarf2_cu *cu);
bc30ff58 1472
257e7a09
YQ
1473static void dwarf2_read_symtab (struct partial_symtab *,
1474 struct objfile *);
c906108c 1475
a14ed312 1476static void psymtab_to_symtab_1 (struct partial_symtab *);
c906108c 1477
685af9cd 1478static abbrev_table_up abbrev_table_read_table
ed2dc618
SM
1479 (struct dwarf2_per_objfile *dwarf2_per_objfile, struct dwarf2_section_info *,
1480 sect_offset);
433df2d4 1481
d521ce57 1482static unsigned int peek_abbrev_code (bfd *, const gdb_byte *);
6caca83c 1483
dee91e82 1484static struct partial_die_info *load_partial_dies
d521ce57 1485 (const struct die_reader_specs *, const gdb_byte *, int);
72bf9492 1486
36586728 1487static struct partial_die_info *find_partial_die (sect_offset, int,
10b3939b 1488 struct dwarf2_cu *);
72bf9492 1489
d521ce57
TT
1490static const gdb_byte *read_attribute (const struct die_reader_specs *,
1491 struct attribute *, struct attr_abbrev *,
1492 const gdb_byte *);
a8329558 1493
a1855c1d 1494static unsigned int read_1_byte (bfd *, const gdb_byte *);
c906108c 1495
a1855c1d 1496static int read_1_signed_byte (bfd *, const gdb_byte *);
c906108c 1497
a1855c1d 1498static unsigned int read_2_bytes (bfd *, const gdb_byte *);
c906108c 1499
a1855c1d 1500static unsigned int read_4_bytes (bfd *, const gdb_byte *);
c906108c 1501
a1855c1d 1502static ULONGEST read_8_bytes (bfd *, const gdb_byte *);
c906108c 1503
d521ce57 1504static CORE_ADDR read_address (bfd *, const gdb_byte *ptr, struct dwarf2_cu *,
891d2f0b 1505 unsigned int *);
c906108c 1506
d521ce57 1507static LONGEST read_initial_length (bfd *, const gdb_byte *, unsigned int *);
c764a876
DE
1508
1509static LONGEST read_checked_initial_length_and_offset
d521ce57 1510 (bfd *, const gdb_byte *, const struct comp_unit_head *,
c764a876 1511 unsigned int *, unsigned int *);
613e1657 1512
d521ce57
TT
1513static LONGEST read_offset (bfd *, const gdb_byte *,
1514 const struct comp_unit_head *,
c764a876
DE
1515 unsigned int *);
1516
d521ce57 1517static LONGEST read_offset_1 (bfd *, const gdb_byte *, unsigned int);
613e1657 1518
ed2dc618
SM
1519static sect_offset read_abbrev_offset
1520 (struct dwarf2_per_objfile *dwarf2_per_objfile,
1521 struct dwarf2_section_info *, sect_offset);
f4dc4d17 1522
d521ce57 1523static const gdb_byte *read_n_bytes (bfd *, const gdb_byte *, unsigned int);
c906108c 1524
d521ce57 1525static const char *read_direct_string (bfd *, const gdb_byte *, unsigned int *);
c906108c 1526
ed2dc618
SM
1527static const char *read_indirect_string
1528 (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *, const gdb_byte *,
1529 const struct comp_unit_head *, unsigned int *);
4bdf3d34 1530
ed2dc618
SM
1531static const char *read_indirect_line_string
1532 (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *, const gdb_byte *,
1533 const struct comp_unit_head *, unsigned int *);
36586728 1534
ed2dc618
SM
1535static const char *read_indirect_string_at_offset
1536 (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *abfd,
1537 LONGEST str_offset);
927aa2e7 1538
ed2dc618
SM
1539static const char *read_indirect_string_from_dwz
1540 (struct objfile *objfile, struct dwz_file *, LONGEST);
c906108c 1541
d521ce57 1542static LONGEST read_signed_leb128 (bfd *, const gdb_byte *, unsigned int *);
c906108c 1543
d521ce57
TT
1544static CORE_ADDR read_addr_index_from_leb128 (struct dwarf2_cu *,
1545 const gdb_byte *,
3019eac3
DE
1546 unsigned int *);
1547
d521ce57 1548static const char *read_str_index (const struct die_reader_specs *reader,
342587c4 1549 ULONGEST str_index);
3019eac3 1550
e142c38c 1551static void set_cu_language (unsigned int, struct dwarf2_cu *);
c906108c 1552
e142c38c
DJ
1553static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
1554 struct dwarf2_cu *);
c906108c 1555
348e048f 1556static struct attribute *dwarf2_attr_no_follow (struct die_info *,
45e58e77 1557 unsigned int);
348e048f 1558
7d45c7c3
KB
1559static const char *dwarf2_string_attr (struct die_info *die, unsigned int name,
1560 struct dwarf2_cu *cu);
1561
05cf31d1
JB
1562static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
1563 struct dwarf2_cu *cu);
1564
e142c38c 1565static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
3ca72b44 1566
e142c38c 1567static struct die_info *die_specification (struct die_info *die,
f2f0e013 1568 struct dwarf2_cu **);
63d06c5c 1569
9c541725 1570static line_header_up dwarf_decode_line_header (sect_offset sect_off,
fff8551c 1571 struct dwarf2_cu *cu);
debd256d 1572
f3f5162e 1573static void dwarf_decode_lines (struct line_header *, const char *,
c3b7b696 1574 struct dwarf2_cu *, struct partial_symtab *,
527f3840 1575 CORE_ADDR, int decode_mapping);
c906108c 1576
804d2729
TT
1577static void dwarf2_start_subfile (struct dwarf2_cu *, const char *,
1578 const char *);
c906108c 1579
43f3e411
DE
1580static struct compunit_symtab *dwarf2_start_symtab (struct dwarf2_cu *,
1581 const char *, const char *,
1582 CORE_ADDR);
f4dc4d17 1583
a14ed312 1584static struct symbol *new_symbol (struct die_info *, struct type *,
5e2db402 1585 struct dwarf2_cu *, struct symbol * = NULL);
34eaf542 1586
ff39bb5e 1587static void dwarf2_const_value (const struct attribute *, struct symbol *,
e7c27a73 1588 struct dwarf2_cu *);
c906108c 1589
ff39bb5e 1590static void dwarf2_const_value_attr (const struct attribute *attr,
98bfdba5
PA
1591 struct type *type,
1592 const char *name,
1593 struct obstack *obstack,
12df843f 1594 struct dwarf2_cu *cu, LONGEST *value,
d521ce57 1595 const gdb_byte **bytes,
98bfdba5 1596 struct dwarf2_locexpr_baton **baton);
2df3850c 1597
e7c27a73 1598static struct type *die_type (struct die_info *, struct dwarf2_cu *);
c906108c 1599
b4ba55a1
JB
1600static int need_gnat_info (struct dwarf2_cu *);
1601
3e43a32a
MS
1602static struct type *die_descriptive_type (struct die_info *,
1603 struct dwarf2_cu *);
b4ba55a1
JB
1604
1605static void set_descriptive_type (struct type *, struct die_info *,
1606 struct dwarf2_cu *);
1607
e7c27a73
DJ
1608static struct type *die_containing_type (struct die_info *,
1609 struct dwarf2_cu *);
c906108c 1610
ff39bb5e 1611static struct type *lookup_die_type (struct die_info *, const struct attribute *,
673bfd45 1612 struct dwarf2_cu *);
c906108c 1613
f792889a 1614static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
c906108c 1615
673bfd45
DE
1616static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1617
0d5cff50 1618static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
63d06c5c 1619
6e70227d 1620static char *typename_concat (struct obstack *obs, const char *prefix,
f55ee35c
JK
1621 const char *suffix, int physname,
1622 struct dwarf2_cu *cu);
63d06c5c 1623
e7c27a73 1624static void read_file_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1625
348e048f
DE
1626static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1627
e7c27a73 1628static void read_func_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1629
e7c27a73 1630static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1631
96408a79
SA
1632static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1633
71a3c369
TT
1634static void read_variable (struct die_info *die, struct dwarf2_cu *cu);
1635
ff013f42
JK
1636static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
1637 struct dwarf2_cu *, struct partial_symtab *);
1638
3a2b436a 1639/* How dwarf2_get_pc_bounds constructed its *LOWPC and *HIGHPC return
e385593e 1640 values. Keep the items ordered with increasing constraints compliance. */
3a2b436a
JK
1641enum pc_bounds_kind
1642{
e385593e 1643 /* No attribute DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges was found. */
3a2b436a
JK
1644 PC_BOUNDS_NOT_PRESENT,
1645
e385593e
JK
1646 /* Some of the attributes DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges
1647 were present but they do not form a valid range of PC addresses. */
1648 PC_BOUNDS_INVALID,
1649
3a2b436a
JK
1650 /* Discontiguous range was found - that is DW_AT_ranges was found. */
1651 PC_BOUNDS_RANGES,
1652
1653 /* Contiguous range was found - DW_AT_low_pc and DW_AT_high_pc were found. */
1654 PC_BOUNDS_HIGH_LOW,
1655};
1656
1657static enum pc_bounds_kind dwarf2_get_pc_bounds (struct die_info *,
1658 CORE_ADDR *, CORE_ADDR *,
1659 struct dwarf2_cu *,
1660 struct partial_symtab *);
c906108c 1661
fae299cd
DC
1662static void get_scope_pc_bounds (struct die_info *,
1663 CORE_ADDR *, CORE_ADDR *,
1664 struct dwarf2_cu *);
1665
801e3a5b
JB
1666static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1667 CORE_ADDR, struct dwarf2_cu *);
1668
a14ed312 1669static void dwarf2_add_field (struct field_info *, struct die_info *,
e7c27a73 1670 struct dwarf2_cu *);
c906108c 1671
a14ed312 1672static void dwarf2_attach_fields_to_type (struct field_info *,
e7c27a73 1673 struct type *, struct dwarf2_cu *);
c906108c 1674
a14ed312 1675static void dwarf2_add_member_fn (struct field_info *,
e26fb1d7 1676 struct die_info *, struct type *,
e7c27a73 1677 struct dwarf2_cu *);
c906108c 1678
a14ed312 1679static void dwarf2_attach_fn_fields_to_type (struct field_info *,
3e43a32a
MS
1680 struct type *,
1681 struct dwarf2_cu *);
c906108c 1682
134d01f1 1683static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1684
e7c27a73 1685static void read_common_block (struct die_info *, struct dwarf2_cu *);
c906108c 1686
e7c27a73 1687static void read_namespace (struct die_info *die, struct dwarf2_cu *);
d9fa45fe 1688
5d7cb8df
JK
1689static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1690
804d2729 1691static struct using_direct **using_directives (struct dwarf2_cu *cu);
22cee43f 1692
27aa8d6a
SW
1693static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1694
74921315
KS
1695static int read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu);
1696
f55ee35c
JK
1697static struct type *read_module_type (struct die_info *die,
1698 struct dwarf2_cu *cu);
1699
38d518c9 1700static const char *namespace_name (struct die_info *die,
e142c38c 1701 int *is_anonymous, struct dwarf2_cu *);
38d518c9 1702
134d01f1 1703static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1704
e7c27a73 1705static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *);
c906108c 1706
6e70227d 1707static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
7ca2d3a3
DL
1708 struct dwarf2_cu *);
1709
bf6af496 1710static struct die_info *read_die_and_siblings_1
d521ce57 1711 (const struct die_reader_specs *, const gdb_byte *, const gdb_byte **,
bf6af496 1712 struct die_info *);
639d11d3 1713
dee91e82 1714static struct die_info *read_die_and_siblings (const struct die_reader_specs *,
d521ce57
TT
1715 const gdb_byte *info_ptr,
1716 const gdb_byte **new_info_ptr,
639d11d3
DC
1717 struct die_info *parent);
1718
d521ce57
TT
1719static const gdb_byte *read_full_die_1 (const struct die_reader_specs *,
1720 struct die_info **, const gdb_byte *,
1721 int *, int);
3019eac3 1722
d521ce57
TT
1723static const gdb_byte *read_full_die (const struct die_reader_specs *,
1724 struct die_info **, const gdb_byte *,
1725 int *);
93311388 1726
e7c27a73 1727static void process_die (struct die_info *, struct dwarf2_cu *);
c906108c 1728
15d034d0
TT
1729static const char *dwarf2_canonicalize_name (const char *, struct dwarf2_cu *,
1730 struct obstack *);
71c25dea 1731
15d034d0 1732static const char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
9219021c 1733
15d034d0 1734static const char *dwarf2_full_name (const char *name,
98bfdba5
PA
1735 struct die_info *die,
1736 struct dwarf2_cu *cu);
1737
ca69b9e6
DE
1738static const char *dwarf2_physname (const char *name, struct die_info *die,
1739 struct dwarf2_cu *cu);
1740
e142c38c 1741static struct die_info *dwarf2_extension (struct die_info *die,
f2f0e013 1742 struct dwarf2_cu **);
9219021c 1743
f39c6ffd 1744static const char *dwarf_tag_name (unsigned int);
c906108c 1745
f39c6ffd 1746static const char *dwarf_attr_name (unsigned int);
c906108c 1747
f39c6ffd 1748static const char *dwarf_form_name (unsigned int);
c906108c 1749
a121b7c1 1750static const char *dwarf_bool_name (unsigned int);
c906108c 1751
f39c6ffd 1752static const char *dwarf_type_encoding_name (unsigned int);
c906108c 1753
f9aca02d 1754static struct die_info *sibling_die (struct die_info *);
c906108c 1755
d97bc12b
DE
1756static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1757
1758static void dump_die_for_error (struct die_info *);
1759
1760static void dump_die_1 (struct ui_file *, int level, int max_level,
1761 struct die_info *);
c906108c 1762
d97bc12b 1763/*static*/ void dump_die (struct die_info *, int max_level);
c906108c 1764
51545339 1765static void store_in_ref_table (struct die_info *,
10b3939b 1766 struct dwarf2_cu *);
c906108c 1767
ff39bb5e 1768static sect_offset dwarf2_get_ref_die_offset (const struct attribute *);
c906108c 1769
ff39bb5e 1770static LONGEST dwarf2_get_attr_constant_value (const struct attribute *, int);
a02abb62 1771
348e048f 1772static struct die_info *follow_die_ref_or_sig (struct die_info *,
ff39bb5e 1773 const struct attribute *,
348e048f
DE
1774 struct dwarf2_cu **);
1775
10b3939b 1776static struct die_info *follow_die_ref (struct die_info *,
ff39bb5e 1777 const struct attribute *,
f2f0e013 1778 struct dwarf2_cu **);
c906108c 1779
348e048f 1780static struct die_info *follow_die_sig (struct die_info *,
ff39bb5e 1781 const struct attribute *,
348e048f
DE
1782 struct dwarf2_cu **);
1783
ac9ec31b
DE
1784static struct type *get_signatured_type (struct die_info *, ULONGEST,
1785 struct dwarf2_cu *);
1786
1787static struct type *get_DW_AT_signature_type (struct die_info *,
ff39bb5e 1788 const struct attribute *,
ac9ec31b
DE
1789 struct dwarf2_cu *);
1790
e5fe5e75 1791static void load_full_type_unit (struct dwarf2_per_cu_data *per_cu);
348e048f 1792
52dc124a 1793static void read_signatured_type (struct signatured_type *);
348e048f 1794
63e43d3a
PMR
1795static int attr_to_dynamic_prop (const struct attribute *attr,
1796 struct die_info *die, struct dwarf2_cu *cu,
1797 struct dynamic_prop *prop);
1798
c906108c
SS
1799/* memory allocation interface */
1800
7b5a2f43 1801static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
c906108c 1802
b60c80d6 1803static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
c906108c 1804
43f3e411 1805static void dwarf_decode_macros (struct dwarf2_cu *, unsigned int, int);
2e276125 1806
6e5a29e1 1807static int attr_form_is_block (const struct attribute *);
8e19ed76 1808
6e5a29e1 1809static int attr_form_is_section_offset (const struct attribute *);
3690dd37 1810
6e5a29e1 1811static int attr_form_is_constant (const struct attribute *);
3690dd37 1812
6e5a29e1 1813static int attr_form_is_ref (const struct attribute *);
7771576e 1814
8cf6f0b1
TT
1815static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1816 struct dwarf2_loclist_baton *baton,
ff39bb5e 1817 const struct attribute *attr);
8cf6f0b1 1818
ff39bb5e 1819static void dwarf2_symbol_mark_computed (const struct attribute *attr,
93e7bd98 1820 struct symbol *sym,
f1e6e072
TT
1821 struct dwarf2_cu *cu,
1822 int is_block);
4c2df51b 1823
d521ce57
TT
1824static const gdb_byte *skip_one_die (const struct die_reader_specs *reader,
1825 const gdb_byte *info_ptr,
1826 struct abbrev_info *abbrev);
4bb7a0a7 1827
72bf9492
DJ
1828static hashval_t partial_die_hash (const void *item);
1829
1830static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1831
ae038cb0 1832static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
ed2dc618
SM
1833 (sect_offset sect_off, unsigned int offset_in_dwz,
1834 struct dwarf2_per_objfile *dwarf2_per_objfile);
ae038cb0 1835
9816fde3 1836static void prepare_one_comp_unit (struct dwarf2_cu *cu,
95554aad
TT
1837 struct die_info *comp_unit_die,
1838 enum language pretend_language);
93311388 1839
ed2dc618 1840static void age_cached_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
ae038cb0 1841
dee91e82 1842static void free_one_cached_comp_unit (struct dwarf2_per_cu_data *);
ae038cb0 1843
f792889a
DJ
1844static struct type *set_die_type (struct die_info *, struct type *,
1845 struct dwarf2_cu *);
1c379e20 1846
ed2dc618 1847static void create_all_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
ae038cb0 1848
ed2dc618 1849static int create_all_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
1fd400ff 1850
58f0c718 1851static void load_full_comp_unit (struct dwarf2_per_cu_data *, bool,
95554aad 1852 enum language);
10b3939b 1853
95554aad
TT
1854static void process_full_comp_unit (struct dwarf2_per_cu_data *,
1855 enum language);
10b3939b 1856
f4dc4d17
DE
1857static void process_full_type_unit (struct dwarf2_per_cu_data *,
1858 enum language);
1859
10b3939b
DJ
1860static void dwarf2_add_dependence (struct dwarf2_cu *,
1861 struct dwarf2_per_cu_data *);
1862
ae038cb0
DJ
1863static void dwarf2_mark (struct dwarf2_cu *);
1864
1865static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
1866
b64f50a1 1867static struct type *get_die_type_at_offset (sect_offset,
ac9ec31b 1868 struct dwarf2_per_cu_data *);
673bfd45 1869
f792889a 1870static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
72019c9c 1871
95554aad
TT
1872static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
1873 enum language pretend_language);
1874
ed2dc618 1875static void process_queue (struct dwarf2_per_objfile *dwarf2_per_objfile);
9291a0cd 1876
b303c6f6
AB
1877/* Class, the destructor of which frees all allocated queue entries. This
1878 will only have work to do if an error was thrown while processing the
1879 dwarf. If no error was thrown then the queue entries should have all
1880 been processed, and freed, as we went along. */
1881
1882class dwarf2_queue_guard
1883{
1884public:
1885 dwarf2_queue_guard () = default;
1886
1887 /* Free any entries remaining on the queue. There should only be
1888 entries left if we hit an error while processing the dwarf. */
1889 ~dwarf2_queue_guard ()
1890 {
1891 struct dwarf2_queue_item *item, *last;
1892
1893 item = dwarf2_queue;
1894 while (item)
1895 {
1896 /* Anything still marked queued is likely to be in an
1897 inconsistent state, so discard it. */
1898 if (item->per_cu->queued)
1899 {
1900 if (item->per_cu->cu != NULL)
1901 free_one_cached_comp_unit (item->per_cu);
1902 item->per_cu->queued = 0;
1903 }
1904
1905 last = item;
1906 item = item->next;
1907 xfree (last);
1908 }
1909
1910 dwarf2_queue = dwarf2_queue_tail = NULL;
1911 }
1912};
1913
d721ba37
PA
1914/* The return type of find_file_and_directory. Note, the enclosed
1915 string pointers are only valid while this object is valid. */
1916
1917struct file_and_directory
1918{
1919 /* The filename. This is never NULL. */
1920 const char *name;
1921
1922 /* The compilation directory. NULL if not known. If we needed to
1923 compute a new string, this points to COMP_DIR_STORAGE, otherwise,
1924 points directly to the DW_AT_comp_dir string attribute owned by
1925 the obstack that owns the DIE. */
1926 const char *comp_dir;
1927
1928 /* If we needed to build a new string for comp_dir, this is what
1929 owns the storage. */
1930 std::string comp_dir_storage;
1931};
1932
1933static file_and_directory find_file_and_directory (struct die_info *die,
1934 struct dwarf2_cu *cu);
9291a0cd
TT
1935
1936static char *file_full_name (int file, struct line_header *lh,
1937 const char *comp_dir);
1938
43988095
JK
1939/* Expected enum dwarf_unit_type for read_comp_unit_head. */
1940enum class rcuh_kind { COMPILE, TYPE };
1941
d521ce57 1942static const gdb_byte *read_and_check_comp_unit_head
ed2dc618
SM
1943 (struct dwarf2_per_objfile* dwarf2_per_objfile,
1944 struct comp_unit_head *header,
36586728 1945 struct dwarf2_section_info *section,
d521ce57 1946 struct dwarf2_section_info *abbrev_section, const gdb_byte *info_ptr,
43988095 1947 rcuh_kind section_kind);
36586728 1948
fd820528 1949static void init_cutu_and_read_dies
f4dc4d17 1950 (struct dwarf2_per_cu_data *this_cu, struct abbrev_table *abbrev_table,
58f0c718 1951 int use_existing_cu, int keep, bool skip_partial,
3019eac3
DE
1952 die_reader_func_ftype *die_reader_func, void *data);
1953
dee91e82
DE
1954static void init_cutu_and_read_dies_simple
1955 (struct dwarf2_per_cu_data *this_cu,
1956 die_reader_func_ftype *die_reader_func, void *data);
9291a0cd 1957
673bfd45 1958static htab_t allocate_signatured_type_table (struct objfile *objfile);
1fd400ff 1959
3019eac3
DE
1960static htab_t allocate_dwo_unit_table (struct objfile *objfile);
1961
57d63ce2 1962static struct dwo_unit *lookup_dwo_unit_in_dwp
ed2dc618
SM
1963 (struct dwarf2_per_objfile *dwarf2_per_objfile,
1964 struct dwp_file *dwp_file, const char *comp_dir,
57d63ce2 1965 ULONGEST signature, int is_debug_types);
a2ce51a0 1966
ed2dc618
SM
1967static struct dwp_file *get_dwp_file
1968 (struct dwarf2_per_objfile *dwarf2_per_objfile);
a2ce51a0 1969
3019eac3 1970static struct dwo_unit *lookup_dwo_comp_unit
a1855c1d 1971 (struct dwarf2_per_cu_data *, const char *, const char *, ULONGEST);
3019eac3
DE
1972
1973static struct dwo_unit *lookup_dwo_type_unit
a1855c1d 1974 (struct signatured_type *, const char *, const char *);
3019eac3 1975
89e63ee4
DE
1976static void queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *);
1977
263db9a1 1978static void free_dwo_file (struct dwo_file *);
3019eac3 1979
263db9a1
TT
1980/* A unique_ptr helper to free a dwo_file. */
1981
1982struct dwo_file_deleter
ed2dc618 1983{
263db9a1
TT
1984 void operator() (struct dwo_file *df) const
1985 {
1986 free_dwo_file (df);
1987 }
ed2dc618
SM
1988};
1989
263db9a1
TT
1990/* A unique pointer to a dwo_file. */
1991
1992typedef std::unique_ptr<struct dwo_file, dwo_file_deleter> dwo_file_up;
1993
ed2dc618 1994static void process_cu_includes (struct dwarf2_per_objfile *dwarf2_per_objfile);
95554aad 1995
1b80a9fa 1996static void check_producer (struct dwarf2_cu *cu);
527f3840
JK
1997
1998static void free_line_header_voidp (void *arg);
4390d890
DE
1999\f
2000/* Various complaints about symbol reading that don't abort the process. */
2001
2002static void
2003dwarf2_statement_list_fits_in_line_number_section_complaint (void)
2004{
b98664d3 2005 complaint (_("statement list doesn't fit in .debug_line section"));
4390d890
DE
2006}
2007
2008static void
2009dwarf2_debug_line_missing_file_complaint (void)
2010{
b98664d3 2011 complaint (_(".debug_line section has line data without a file"));
4390d890
DE
2012}
2013
2014static void
2015dwarf2_debug_line_missing_end_sequence_complaint (void)
2016{
b98664d3 2017 complaint (_(".debug_line section has line "
4390d890
DE
2018 "program sequence without an end"));
2019}
2020
2021static void
2022dwarf2_complex_location_expr_complaint (void)
2023{
b98664d3 2024 complaint (_("location expression too complex"));
4390d890
DE
2025}
2026
2027static void
2028dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
2029 int arg3)
2030{
b98664d3 2031 complaint (_("const value length mismatch for '%s', got %d, expected %d"),
4390d890
DE
2032 arg1, arg2, arg3);
2033}
2034
2035static void
2036dwarf2_section_buffer_overflow_complaint (struct dwarf2_section_info *section)
2037{
b98664d3 2038 complaint (_("debug info runs off end of %s section"
4390d890 2039 " [in module %s]"),
a32a8923
DE
2040 get_section_name (section),
2041 get_section_file_name (section));
4390d890 2042}
1b80a9fa 2043
4390d890
DE
2044static void
2045dwarf2_macro_malformed_definition_complaint (const char *arg1)
2046{
b98664d3 2047 complaint (_("macro debug info contains a "
4390d890
DE
2048 "malformed macro definition:\n`%s'"),
2049 arg1);
2050}
2051
2052static void
2053dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
2054{
b98664d3 2055 complaint (_("invalid attribute class or form for '%s' in '%s'"),
4390d890
DE
2056 arg1, arg2);
2057}
527f3840
JK
2058
2059/* Hash function for line_header_hash. */
2060
2061static hashval_t
2062line_header_hash (const struct line_header *ofs)
2063{
9c541725 2064 return to_underlying (ofs->sect_off) ^ ofs->offset_in_dwz;
527f3840
JK
2065}
2066
2067/* Hash function for htab_create_alloc_ex for line_header_hash. */
2068
2069static hashval_t
2070line_header_hash_voidp (const void *item)
2071{
9a3c8263 2072 const struct line_header *ofs = (const struct line_header *) item;
527f3840
JK
2073
2074 return line_header_hash (ofs);
2075}
2076
2077/* Equality function for line_header_hash. */
2078
2079static int
2080line_header_eq_voidp (const void *item_lhs, const void *item_rhs)
2081{
9a3c8263
SM
2082 const struct line_header *ofs_lhs = (const struct line_header *) item_lhs;
2083 const struct line_header *ofs_rhs = (const struct line_header *) item_rhs;
527f3840 2084
9c541725 2085 return (ofs_lhs->sect_off == ofs_rhs->sect_off
527f3840
JK
2086 && ofs_lhs->offset_in_dwz == ofs_rhs->offset_in_dwz);
2087}
2088
4390d890 2089\f
9291a0cd 2090
31aa7e4e
JB
2091/* Read the given attribute value as an address, taking the attribute's
2092 form into account. */
2093
2094static CORE_ADDR
2095attr_value_as_address (struct attribute *attr)
2096{
2097 CORE_ADDR addr;
2098
2099 if (attr->form != DW_FORM_addr && attr->form != DW_FORM_GNU_addr_index)
2100 {
2101 /* Aside from a few clearly defined exceptions, attributes that
2102 contain an address must always be in DW_FORM_addr form.
2103 Unfortunately, some compilers happen to be violating this
2104 requirement by encoding addresses using other forms, such
2105 as DW_FORM_data4 for example. For those broken compilers,
2106 we try to do our best, without any guarantee of success,
2107 to interpret the address correctly. It would also be nice
2108 to generate a complaint, but that would require us to maintain
2109 a list of legitimate cases where a non-address form is allowed,
2110 as well as update callers to pass in at least the CU's DWARF
2111 version. This is more overhead than what we're willing to
2112 expand for a pretty rare case. */
2113 addr = DW_UNSND (attr);
2114 }
2115 else
2116 addr = DW_ADDR (attr);
2117
2118 return addr;
2119}
2120
330cdd98
PA
2121/* See declaration. */
2122
2123dwarf2_per_objfile::dwarf2_per_objfile (struct objfile *objfile_,
2124 const dwarf2_debug_sections *names)
2125 : objfile (objfile_)
2126{
2127 if (names == NULL)
2128 names = &dwarf2_elf_names;
2129
2130 bfd *obfd = objfile->obfd;
2131
2132 for (asection *sec = obfd->sections; sec != NULL; sec = sec->next)
2133 locate_sections (obfd, sec, *names);
2134}
2135
fc8e7e75
SM
2136static void free_dwo_files (htab_t dwo_files, struct objfile *objfile);
2137
330cdd98
PA
2138dwarf2_per_objfile::~dwarf2_per_objfile ()
2139{
2140 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
2141 free_cached_comp_units ();
2142
2143 if (quick_file_names_table)
2144 htab_delete (quick_file_names_table);
2145
2146 if (line_header_hash)
2147 htab_delete (line_header_hash);
2148
b76e467d
SM
2149 for (dwarf2_per_cu_data *per_cu : all_comp_units)
2150 VEC_free (dwarf2_per_cu_ptr, per_cu->imported_symtabs);
fc8e7e75 2151
b2bdb8cf
SM
2152 for (signatured_type *sig_type : all_type_units)
2153 VEC_free (dwarf2_per_cu_ptr, sig_type->per_cu.imported_symtabs);
fc8e7e75
SM
2154
2155 VEC_free (dwarf2_section_info_def, types);
2156
2157 if (dwo_files != NULL)
2158 free_dwo_files (dwo_files, objfile);
fc8e7e75 2159
330cdd98
PA
2160 /* Everything else should be on the objfile obstack. */
2161}
2162
2163/* See declaration. */
2164
2165void
2166dwarf2_per_objfile::free_cached_comp_units ()
2167{
2168 dwarf2_per_cu_data *per_cu = read_in_chain;
2169 dwarf2_per_cu_data **last_chain = &read_in_chain;
2170 while (per_cu != NULL)
2171 {
2172 dwarf2_per_cu_data *next_cu = per_cu->cu->read_in_chain;
2173
fcd3b13d 2174 delete per_cu->cu;
330cdd98
PA
2175 *last_chain = next_cu;
2176 per_cu = next_cu;
2177 }
2178}
2179
11ed8cad
TT
2180/* A helper class that calls free_cached_comp_units on
2181 destruction. */
2182
2183class free_cached_comp_units
2184{
2185public:
2186
2187 explicit free_cached_comp_units (dwarf2_per_objfile *per_objfile)
2188 : m_per_objfile (per_objfile)
2189 {
2190 }
2191
2192 ~free_cached_comp_units ()
2193 {
2194 m_per_objfile->free_cached_comp_units ();
2195 }
2196
2197 DISABLE_COPY_AND_ASSIGN (free_cached_comp_units);
2198
2199private:
2200
2201 dwarf2_per_objfile *m_per_objfile;
2202};
2203
c906108c 2204/* Try to locate the sections we need for DWARF 2 debugging
251d32d9
TG
2205 information and return true if we have enough to do something.
2206 NAMES points to the dwarf2 section names, or is NULL if the standard
2207 ELF names are used. */
c906108c
SS
2208
2209int
251d32d9
TG
2210dwarf2_has_info (struct objfile *objfile,
2211 const struct dwarf2_debug_sections *names)
c906108c 2212{
97cbe998
SDJ
2213 if (objfile->flags & OBJF_READNEVER)
2214 return 0;
2215
ed2dc618
SM
2216 struct dwarf2_per_objfile *dwarf2_per_objfile
2217 = get_dwarf2_per_objfile (objfile);
2218
2219 if (dwarf2_per_objfile == NULL)
be391dca
TT
2220 {
2221 /* Initialize per-objfile state. */
fd90ace4
YQ
2222 dwarf2_per_objfile
2223 = new (&objfile->objfile_obstack) struct dwarf2_per_objfile (objfile,
2224 names);
ed2dc618 2225 set_dwarf2_per_objfile (objfile, dwarf2_per_objfile);
be391dca 2226 }
73869dc2 2227 return (!dwarf2_per_objfile->info.is_virtual
049412e3 2228 && dwarf2_per_objfile->info.s.section != NULL
73869dc2 2229 && !dwarf2_per_objfile->abbrev.is_virtual
049412e3 2230 && dwarf2_per_objfile->abbrev.s.section != NULL);
73869dc2
DE
2231}
2232
2233/* Return the containing section of virtual section SECTION. */
2234
2235static struct dwarf2_section_info *
2236get_containing_section (const struct dwarf2_section_info *section)
2237{
2238 gdb_assert (section->is_virtual);
2239 return section->s.containing_section;
c906108c
SS
2240}
2241
a32a8923
DE
2242/* Return the bfd owner of SECTION. */
2243
2244static struct bfd *
2245get_section_bfd_owner (const struct dwarf2_section_info *section)
2246{
73869dc2
DE
2247 if (section->is_virtual)
2248 {
2249 section = get_containing_section (section);
2250 gdb_assert (!section->is_virtual);
2251 }
049412e3 2252 return section->s.section->owner;
a32a8923
DE
2253}
2254
2255/* Return the bfd section of SECTION.
2256 Returns NULL if the section is not present. */
2257
2258static asection *
2259get_section_bfd_section (const struct dwarf2_section_info *section)
2260{
73869dc2
DE
2261 if (section->is_virtual)
2262 {
2263 section = get_containing_section (section);
2264 gdb_assert (!section->is_virtual);
2265 }
049412e3 2266 return section->s.section;
a32a8923
DE
2267}
2268
2269/* Return the name of SECTION. */
2270
2271static const char *
2272get_section_name (const struct dwarf2_section_info *section)
2273{
2274 asection *sectp = get_section_bfd_section (section);
2275
2276 gdb_assert (sectp != NULL);
2277 return bfd_section_name (get_section_bfd_owner (section), sectp);
2278}
2279
2280/* Return the name of the file SECTION is in. */
2281
2282static const char *
2283get_section_file_name (const struct dwarf2_section_info *section)
2284{
2285 bfd *abfd = get_section_bfd_owner (section);
2286
2287 return bfd_get_filename (abfd);
2288}
2289
2290/* Return the id of SECTION.
2291 Returns 0 if SECTION doesn't exist. */
2292
2293static int
2294get_section_id (const struct dwarf2_section_info *section)
2295{
2296 asection *sectp = get_section_bfd_section (section);
2297
2298 if (sectp == NULL)
2299 return 0;
2300 return sectp->id;
2301}
2302
2303/* Return the flags of SECTION.
73869dc2 2304 SECTION (or containing section if this is a virtual section) must exist. */
a32a8923
DE
2305
2306static int
2307get_section_flags (const struct dwarf2_section_info *section)
2308{
2309 asection *sectp = get_section_bfd_section (section);
2310
2311 gdb_assert (sectp != NULL);
2312 return bfd_get_section_flags (sectp->owner, sectp);
2313}
2314
251d32d9
TG
2315/* When loading sections, we look either for uncompressed section or for
2316 compressed section names. */
233a11ab
CS
2317
2318static int
251d32d9
TG
2319section_is_p (const char *section_name,
2320 const struct dwarf2_section_names *names)
233a11ab 2321{
251d32d9
TG
2322 if (names->normal != NULL
2323 && strcmp (section_name, names->normal) == 0)
2324 return 1;
2325 if (names->compressed != NULL
2326 && strcmp (section_name, names->compressed) == 0)
2327 return 1;
2328 return 0;
233a11ab
CS
2329}
2330
330cdd98 2331/* See declaration. */
c906108c 2332
330cdd98
PA
2333void
2334dwarf2_per_objfile::locate_sections (bfd *abfd, asection *sectp,
2335 const dwarf2_debug_sections &names)
c906108c 2336{
dc7650b8 2337 flagword aflag = bfd_get_section_flags (abfd, sectp);
251d32d9 2338
dc7650b8
JK
2339 if ((aflag & SEC_HAS_CONTENTS) == 0)
2340 {
2341 }
330cdd98 2342 else if (section_is_p (sectp->name, &names.info))
c906108c 2343 {
330cdd98
PA
2344 this->info.s.section = sectp;
2345 this->info.size = bfd_get_section_size (sectp);
c906108c 2346 }
330cdd98 2347 else if (section_is_p (sectp->name, &names.abbrev))
c906108c 2348 {
330cdd98
PA
2349 this->abbrev.s.section = sectp;
2350 this->abbrev.size = bfd_get_section_size (sectp);
c906108c 2351 }
330cdd98 2352 else if (section_is_p (sectp->name, &names.line))
c906108c 2353 {
330cdd98
PA
2354 this->line.s.section = sectp;
2355 this->line.size = bfd_get_section_size (sectp);
c906108c 2356 }
330cdd98 2357 else if (section_is_p (sectp->name, &names.loc))
c906108c 2358 {
330cdd98
PA
2359 this->loc.s.section = sectp;
2360 this->loc.size = bfd_get_section_size (sectp);
c906108c 2361 }
330cdd98 2362 else if (section_is_p (sectp->name, &names.loclists))
43988095 2363 {
330cdd98
PA
2364 this->loclists.s.section = sectp;
2365 this->loclists.size = bfd_get_section_size (sectp);
43988095 2366 }
330cdd98 2367 else if (section_is_p (sectp->name, &names.macinfo))
c906108c 2368 {
330cdd98
PA
2369 this->macinfo.s.section = sectp;
2370 this->macinfo.size = bfd_get_section_size (sectp);
c906108c 2371 }
330cdd98 2372 else if (section_is_p (sectp->name, &names.macro))
cf2c3c16 2373 {
330cdd98
PA
2374 this->macro.s.section = sectp;
2375 this->macro.size = bfd_get_section_size (sectp);
cf2c3c16 2376 }
330cdd98 2377 else if (section_is_p (sectp->name, &names.str))
c906108c 2378 {
330cdd98
PA
2379 this->str.s.section = sectp;
2380 this->str.size = bfd_get_section_size (sectp);
c906108c 2381 }
330cdd98 2382 else if (section_is_p (sectp->name, &names.line_str))
43988095 2383 {
330cdd98
PA
2384 this->line_str.s.section = sectp;
2385 this->line_str.size = bfd_get_section_size (sectp);
43988095 2386 }
330cdd98 2387 else if (section_is_p (sectp->name, &names.addr))
3019eac3 2388 {
330cdd98
PA
2389 this->addr.s.section = sectp;
2390 this->addr.size = bfd_get_section_size (sectp);
3019eac3 2391 }
330cdd98 2392 else if (section_is_p (sectp->name, &names.frame))
b6af0555 2393 {
330cdd98
PA
2394 this->frame.s.section = sectp;
2395 this->frame.size = bfd_get_section_size (sectp);
b6af0555 2396 }
330cdd98 2397 else if (section_is_p (sectp->name, &names.eh_frame))
b6af0555 2398 {
330cdd98
PA
2399 this->eh_frame.s.section = sectp;
2400 this->eh_frame.size = bfd_get_section_size (sectp);
b6af0555 2401 }
330cdd98 2402 else if (section_is_p (sectp->name, &names.ranges))
af34e669 2403 {
330cdd98
PA
2404 this->ranges.s.section = sectp;
2405 this->ranges.size = bfd_get_section_size (sectp);
af34e669 2406 }
330cdd98 2407 else if (section_is_p (sectp->name, &names.rnglists))
43988095 2408 {
330cdd98
PA
2409 this->rnglists.s.section = sectp;
2410 this->rnglists.size = bfd_get_section_size (sectp);
43988095 2411 }
330cdd98 2412 else if (section_is_p (sectp->name, &names.types))
348e048f 2413 {
8b70b953
TT
2414 struct dwarf2_section_info type_section;
2415
2416 memset (&type_section, 0, sizeof (type_section));
049412e3 2417 type_section.s.section = sectp;
8b70b953
TT
2418 type_section.size = bfd_get_section_size (sectp);
2419
330cdd98 2420 VEC_safe_push (dwarf2_section_info_def, this->types,
8b70b953 2421 &type_section);
348e048f 2422 }
330cdd98 2423 else if (section_is_p (sectp->name, &names.gdb_index))
9291a0cd 2424 {
330cdd98
PA
2425 this->gdb_index.s.section = sectp;
2426 this->gdb_index.size = bfd_get_section_size (sectp);
9291a0cd 2427 }
927aa2e7
JK
2428 else if (section_is_p (sectp->name, &names.debug_names))
2429 {
2430 this->debug_names.s.section = sectp;
2431 this->debug_names.size = bfd_get_section_size (sectp);
2432 }
2433 else if (section_is_p (sectp->name, &names.debug_aranges))
2434 {
2435 this->debug_aranges.s.section = sectp;
2436 this->debug_aranges.size = bfd_get_section_size (sectp);
2437 }
dce234bc 2438
b4e1fd61 2439 if ((bfd_get_section_flags (abfd, sectp) & (SEC_LOAD | SEC_ALLOC))
72dca2f5 2440 && bfd_section_vma (abfd, sectp) == 0)
330cdd98 2441 this->has_section_at_zero = true;
c906108c
SS
2442}
2443
fceca515
DE
2444/* A helper function that decides whether a section is empty,
2445 or not present. */
9e0ac564
TT
2446
2447static int
19ac8c2e 2448dwarf2_section_empty_p (const struct dwarf2_section_info *section)
9e0ac564 2449{
73869dc2
DE
2450 if (section->is_virtual)
2451 return section->size == 0;
049412e3 2452 return section->s.section == NULL || section->size == 0;
9e0ac564
TT
2453}
2454
cd4fb1b2 2455/* See dwarf2read.h. */
c906108c 2456
cd4fb1b2
SM
2457void
2458dwarf2_read_section (struct objfile *objfile, dwarf2_section_info *info)
c906108c 2459{
a32a8923 2460 asection *sectp;
3019eac3 2461 bfd *abfd;
dce234bc 2462 gdb_byte *buf, *retbuf;
c906108c 2463
be391dca
TT
2464 if (info->readin)
2465 return;
dce234bc 2466 info->buffer = NULL;
be391dca 2467 info->readin = 1;
188dd5d6 2468
9e0ac564 2469 if (dwarf2_section_empty_p (info))
dce234bc 2470 return;
c906108c 2471
a32a8923 2472 sectp = get_section_bfd_section (info);
3019eac3 2473
73869dc2
DE
2474 /* If this is a virtual section we need to read in the real one first. */
2475 if (info->is_virtual)
2476 {
2477 struct dwarf2_section_info *containing_section =
2478 get_containing_section (info);
2479
2480 gdb_assert (sectp != NULL);
2481 if ((sectp->flags & SEC_RELOC) != 0)
2482 {
2483 error (_("Dwarf Error: DWP format V2 with relocations is not"
2484 " supported in section %s [in module %s]"),
2485 get_section_name (info), get_section_file_name (info));
2486 }
2487 dwarf2_read_section (objfile, containing_section);
2488 /* Other code should have already caught virtual sections that don't
2489 fit. */
2490 gdb_assert (info->virtual_offset + info->size
2491 <= containing_section->size);
2492 /* If the real section is empty or there was a problem reading the
2493 section we shouldn't get here. */
2494 gdb_assert (containing_section->buffer != NULL);
2495 info->buffer = containing_section->buffer + info->virtual_offset;
2496 return;
2497 }
2498
4bf44c1c
TT
2499 /* If the section has relocations, we must read it ourselves.
2500 Otherwise we attach it to the BFD. */
2501 if ((sectp->flags & SEC_RELOC) == 0)
dce234bc 2502 {
d521ce57 2503 info->buffer = gdb_bfd_map_section (sectp, &info->size);
4bf44c1c 2504 return;
dce234bc 2505 }
dce234bc 2506
224c3ddb 2507 buf = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, info->size);
4bf44c1c 2508 info->buffer = buf;
dce234bc
PP
2509
2510 /* When debugging .o files, we may need to apply relocations; see
2511 http://sourceware.org/ml/gdb-patches/2002-04/msg00136.html .
2512 We never compress sections in .o files, so we only need to
2513 try this when the section is not compressed. */
ac8035ab 2514 retbuf = symfile_relocate_debug_section (objfile, sectp, buf);
dce234bc
PP
2515 if (retbuf != NULL)
2516 {
2517 info->buffer = retbuf;
2518 return;
2519 }
2520
a32a8923
DE
2521 abfd = get_section_bfd_owner (info);
2522 gdb_assert (abfd != NULL);
2523
dce234bc
PP
2524 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
2525 || bfd_bread (buf, info->size, abfd) != info->size)
19ac8c2e
DE
2526 {
2527 error (_("Dwarf Error: Can't read DWARF data"
2528 " in section %s [in module %s]"),
2529 bfd_section_name (abfd, sectp), bfd_get_filename (abfd));
2530 }
dce234bc
PP
2531}
2532
9e0ac564
TT
2533/* A helper function that returns the size of a section in a safe way.
2534 If you are positive that the section has been read before using the
2535 size, then it is safe to refer to the dwarf2_section_info object's
2536 "size" field directly. In other cases, you must call this
2537 function, because for compressed sections the size field is not set
2538 correctly until the section has been read. */
2539
2540static bfd_size_type
2541dwarf2_section_size (struct objfile *objfile,
2542 struct dwarf2_section_info *info)
2543{
2544 if (!info->readin)
2545 dwarf2_read_section (objfile, info);
2546 return info->size;
2547}
2548
dce234bc 2549/* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
0963b4bd 2550 SECTION_NAME. */
af34e669 2551
dce234bc 2552void
3017a003
TG
2553dwarf2_get_section_info (struct objfile *objfile,
2554 enum dwarf2_section_enum sect,
d521ce57 2555 asection **sectp, const gdb_byte **bufp,
dce234bc
PP
2556 bfd_size_type *sizep)
2557{
2558 struct dwarf2_per_objfile *data
9a3c8263
SM
2559 = (struct dwarf2_per_objfile *) objfile_data (objfile,
2560 dwarf2_objfile_data_key);
dce234bc 2561 struct dwarf2_section_info *info;
a3b2a86b
TT
2562
2563 /* We may see an objfile without any DWARF, in which case we just
2564 return nothing. */
2565 if (data == NULL)
2566 {
2567 *sectp = NULL;
2568 *bufp = NULL;
2569 *sizep = 0;
2570 return;
2571 }
3017a003
TG
2572 switch (sect)
2573 {
2574 case DWARF2_DEBUG_FRAME:
2575 info = &data->frame;
2576 break;
2577 case DWARF2_EH_FRAME:
2578 info = &data->eh_frame;
2579 break;
2580 default:
2581 gdb_assert_not_reached ("unexpected section");
2582 }
dce234bc 2583
9e0ac564 2584 dwarf2_read_section (objfile, info);
dce234bc 2585
a32a8923 2586 *sectp = get_section_bfd_section (info);
dce234bc
PP
2587 *bufp = info->buffer;
2588 *sizep = info->size;
2589}
2590
36586728
TT
2591/* A helper function to find the sections for a .dwz file. */
2592
2593static void
2594locate_dwz_sections (bfd *abfd, asection *sectp, void *arg)
2595{
9a3c8263 2596 struct dwz_file *dwz_file = (struct dwz_file *) arg;
36586728
TT
2597
2598 /* Note that we only support the standard ELF names, because .dwz
2599 is ELF-only (at the time of writing). */
2600 if (section_is_p (sectp->name, &dwarf2_elf_names.abbrev))
2601 {
049412e3 2602 dwz_file->abbrev.s.section = sectp;
36586728
TT
2603 dwz_file->abbrev.size = bfd_get_section_size (sectp);
2604 }
2605 else if (section_is_p (sectp->name, &dwarf2_elf_names.info))
2606 {
049412e3 2607 dwz_file->info.s.section = sectp;
36586728
TT
2608 dwz_file->info.size = bfd_get_section_size (sectp);
2609 }
2610 else if (section_is_p (sectp->name, &dwarf2_elf_names.str))
2611 {
049412e3 2612 dwz_file->str.s.section = sectp;
36586728
TT
2613 dwz_file->str.size = bfd_get_section_size (sectp);
2614 }
2615 else if (section_is_p (sectp->name, &dwarf2_elf_names.line))
2616 {
049412e3 2617 dwz_file->line.s.section = sectp;
36586728
TT
2618 dwz_file->line.size = bfd_get_section_size (sectp);
2619 }
2620 else if (section_is_p (sectp->name, &dwarf2_elf_names.macro))
2621 {
049412e3 2622 dwz_file->macro.s.section = sectp;
36586728
TT
2623 dwz_file->macro.size = bfd_get_section_size (sectp);
2624 }
2ec9a5e0
TT
2625 else if (section_is_p (sectp->name, &dwarf2_elf_names.gdb_index))
2626 {
049412e3 2627 dwz_file->gdb_index.s.section = sectp;
2ec9a5e0
TT
2628 dwz_file->gdb_index.size = bfd_get_section_size (sectp);
2629 }
927aa2e7
JK
2630 else if (section_is_p (sectp->name, &dwarf2_elf_names.debug_names))
2631 {
2632 dwz_file->debug_names.s.section = sectp;
2633 dwz_file->debug_names.size = bfd_get_section_size (sectp);
2634 }
36586728
TT
2635}
2636
4db1a1dc
TT
2637/* Open the separate '.dwz' debug file, if needed. Return NULL if
2638 there is no .gnu_debugaltlink section in the file. Error if there
2639 is such a section but the file cannot be found. */
36586728
TT
2640
2641static struct dwz_file *
ed2dc618 2642dwarf2_get_dwz_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
36586728 2643{
36586728 2644 const char *filename;
acd13123 2645 bfd_size_type buildid_len_arg;
dc294be5
TT
2646 size_t buildid_len;
2647 bfd_byte *buildid;
36586728
TT
2648
2649 if (dwarf2_per_objfile->dwz_file != NULL)
7ff8cb8c 2650 return dwarf2_per_objfile->dwz_file.get ();
36586728 2651
4db1a1dc 2652 bfd_set_error (bfd_error_no_error);
791afaa2
TT
2653 gdb::unique_xmalloc_ptr<char> data
2654 (bfd_get_alt_debug_link_info (dwarf2_per_objfile->objfile->obfd,
2655 &buildid_len_arg, &buildid));
4db1a1dc
TT
2656 if (data == NULL)
2657 {
2658 if (bfd_get_error () == bfd_error_no_error)
2659 return NULL;
2660 error (_("could not read '.gnu_debugaltlink' section: %s"),
2661 bfd_errmsg (bfd_get_error ()));
2662 }
791afaa2
TT
2663
2664 gdb::unique_xmalloc_ptr<bfd_byte> buildid_holder (buildid);
36586728 2665
acd13123
TT
2666 buildid_len = (size_t) buildid_len_arg;
2667
791afaa2 2668 filename = data.get ();
d721ba37
PA
2669
2670 std::string abs_storage;
36586728
TT
2671 if (!IS_ABSOLUTE_PATH (filename))
2672 {
14278e1f
TT
2673 gdb::unique_xmalloc_ptr<char> abs
2674 = gdb_realpath (objfile_name (dwarf2_per_objfile->objfile));
36586728 2675
14278e1f 2676 abs_storage = ldirname (abs.get ()) + SLASH_STRING + filename;
d721ba37 2677 filename = abs_storage.c_str ();
36586728
TT
2678 }
2679
dc294be5
TT
2680 /* First try the file name given in the section. If that doesn't
2681 work, try to use the build-id instead. */
192b62ce 2682 gdb_bfd_ref_ptr dwz_bfd (gdb_bfd_open (filename, gnutarget, -1));
dc294be5 2683 if (dwz_bfd != NULL)
36586728 2684 {
192b62ce
TT
2685 if (!build_id_verify (dwz_bfd.get (), buildid_len, buildid))
2686 dwz_bfd.release ();
36586728
TT
2687 }
2688
dc294be5
TT
2689 if (dwz_bfd == NULL)
2690 dwz_bfd = build_id_to_debug_bfd (buildid_len, buildid);
2691
2692 if (dwz_bfd == NULL)
2693 error (_("could not find '.gnu_debugaltlink' file for %s"),
2694 objfile_name (dwarf2_per_objfile->objfile));
2695
7ff8cb8c
TT
2696 std::unique_ptr<struct dwz_file> result
2697 (new struct dwz_file (std::move (dwz_bfd)));
36586728 2698
7ff8cb8c
TT
2699 bfd_map_over_sections (result->dwz_bfd.get (), locate_dwz_sections,
2700 result.get ());
36586728 2701
7ff8cb8c
TT
2702 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd,
2703 result->dwz_bfd.get ());
2704 dwarf2_per_objfile->dwz_file = std::move (result);
2705 return dwarf2_per_objfile->dwz_file.get ();
36586728 2706}
9291a0cd 2707\f
7b9f3c50
DE
2708/* DWARF quick_symbols_functions support. */
2709
2710/* TUs can share .debug_line entries, and there can be a lot more TUs than
2711 unique line tables, so we maintain a separate table of all .debug_line
2712 derived entries to support the sharing.
2713 All the quick functions need is the list of file names. We discard the
2714 line_header when we're done and don't need to record it here. */
2715struct quick_file_names
2716{
094b34ac
DE
2717 /* The data used to construct the hash key. */
2718 struct stmt_list_hash hash;
7b9f3c50
DE
2719
2720 /* The number of entries in file_names, real_names. */
2721 unsigned int num_file_names;
2722
2723 /* The file names from the line table, after being run through
2724 file_full_name. */
2725 const char **file_names;
2726
2727 /* The file names from the line table after being run through
2728 gdb_realpath. These are computed lazily. */
2729 const char **real_names;
2730};
2731
2732/* When using the index (and thus not using psymtabs), each CU has an
2733 object of this type. This is used to hold information needed by
2734 the various "quick" methods. */
2735struct dwarf2_per_cu_quick_data
2736{
2737 /* The file table. This can be NULL if there was no file table
2738 or it's currently not read in.
2739 NOTE: This points into dwarf2_per_objfile->quick_file_names_table. */
2740 struct quick_file_names *file_names;
2741
2742 /* The corresponding symbol table. This is NULL if symbols for this
2743 CU have not yet been read. */
43f3e411 2744 struct compunit_symtab *compunit_symtab;
7b9f3c50
DE
2745
2746 /* A temporary mark bit used when iterating over all CUs in
2747 expand_symtabs_matching. */
2748 unsigned int mark : 1;
2749
2750 /* True if we've tried to read the file table and found there isn't one.
2751 There will be no point in trying to read it again next time. */
2752 unsigned int no_file_data : 1;
2753};
2754
094b34ac
DE
2755/* Utility hash function for a stmt_list_hash. */
2756
2757static hashval_t
2758hash_stmt_list_entry (const struct stmt_list_hash *stmt_list_hash)
2759{
2760 hashval_t v = 0;
2761
2762 if (stmt_list_hash->dwo_unit != NULL)
2763 v += (uintptr_t) stmt_list_hash->dwo_unit->dwo_file;
9c541725 2764 v += to_underlying (stmt_list_hash->line_sect_off);
094b34ac
DE
2765 return v;
2766}
2767
2768/* Utility equality function for a stmt_list_hash. */
2769
2770static int
2771eq_stmt_list_entry (const struct stmt_list_hash *lhs,
2772 const struct stmt_list_hash *rhs)
2773{
2774 if ((lhs->dwo_unit != NULL) != (rhs->dwo_unit != NULL))
2775 return 0;
2776 if (lhs->dwo_unit != NULL
2777 && lhs->dwo_unit->dwo_file != rhs->dwo_unit->dwo_file)
2778 return 0;
2779
9c541725 2780 return lhs->line_sect_off == rhs->line_sect_off;
094b34ac
DE
2781}
2782
7b9f3c50
DE
2783/* Hash function for a quick_file_names. */
2784
2785static hashval_t
2786hash_file_name_entry (const void *e)
2787{
9a3c8263
SM
2788 const struct quick_file_names *file_data
2789 = (const struct quick_file_names *) e;
7b9f3c50 2790
094b34ac 2791 return hash_stmt_list_entry (&file_data->hash);
7b9f3c50
DE
2792}
2793
2794/* Equality function for a quick_file_names. */
2795
2796static int
2797eq_file_name_entry (const void *a, const void *b)
2798{
9a3c8263
SM
2799 const struct quick_file_names *ea = (const struct quick_file_names *) a;
2800 const struct quick_file_names *eb = (const struct quick_file_names *) b;
7b9f3c50 2801
094b34ac 2802 return eq_stmt_list_entry (&ea->hash, &eb->hash);
7b9f3c50
DE
2803}
2804
2805/* Delete function for a quick_file_names. */
2806
2807static void
2808delete_file_name_entry (void *e)
2809{
9a3c8263 2810 struct quick_file_names *file_data = (struct quick_file_names *) e;
7b9f3c50
DE
2811 int i;
2812
2813 for (i = 0; i < file_data->num_file_names; ++i)
2814 {
2815 xfree ((void*) file_data->file_names[i]);
2816 if (file_data->real_names)
2817 xfree ((void*) file_data->real_names[i]);
2818 }
2819
2820 /* The space for the struct itself lives on objfile_obstack,
2821 so we don't free it here. */
2822}
2823
2824/* Create a quick_file_names hash table. */
2825
2826static htab_t
2827create_quick_file_names_table (unsigned int nr_initial_entries)
2828{
2829 return htab_create_alloc (nr_initial_entries,
2830 hash_file_name_entry, eq_file_name_entry,
2831 delete_file_name_entry, xcalloc, xfree);
2832}
9291a0cd 2833
918dd910
JK
2834/* Read in PER_CU->CU. This function is unrelated to symtabs, symtab would
2835 have to be created afterwards. You should call age_cached_comp_units after
2836 processing PER_CU->CU. dw2_setup must have been already called. */
2837
2838static void
58f0c718 2839load_cu (struct dwarf2_per_cu_data *per_cu, bool skip_partial)
918dd910 2840{
3019eac3 2841 if (per_cu->is_debug_types)
e5fe5e75 2842 load_full_type_unit (per_cu);
918dd910 2843 else
58f0c718 2844 load_full_comp_unit (per_cu, skip_partial, language_minimal);
918dd910 2845
cc12ce38
DE
2846 if (per_cu->cu == NULL)
2847 return; /* Dummy CU. */
2dc860c0
DE
2848
2849 dwarf2_find_base_address (per_cu->cu->dies, per_cu->cu);
918dd910
JK
2850}
2851
a0f42c21 2852/* Read in the symbols for PER_CU. */
2fdf6df6 2853
9291a0cd 2854static void
58f0c718 2855dw2_do_instantiate_symtab (struct dwarf2_per_cu_data *per_cu, bool skip_partial)
9291a0cd 2856{
ed2dc618 2857 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
9291a0cd 2858
f4dc4d17
DE
2859 /* Skip type_unit_groups, reading the type units they contain
2860 is handled elsewhere. */
2861 if (IS_TYPE_UNIT_GROUP (per_cu))
2862 return;
2863
b303c6f6
AB
2864 /* The destructor of dwarf2_queue_guard frees any entries left on
2865 the queue. After this point we're guaranteed to leave this function
2866 with the dwarf queue empty. */
2867 dwarf2_queue_guard q_guard;
9291a0cd 2868
95554aad 2869 if (dwarf2_per_objfile->using_index
43f3e411 2870 ? per_cu->v.quick->compunit_symtab == NULL
95554aad
TT
2871 : (per_cu->v.psymtab == NULL || !per_cu->v.psymtab->readin))
2872 {
2873 queue_comp_unit (per_cu, language_minimal);
58f0c718 2874 load_cu (per_cu, skip_partial);
89e63ee4
DE
2875
2876 /* If we just loaded a CU from a DWO, and we're working with an index
2877 that may badly handle TUs, load all the TUs in that DWO as well.
2878 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
2879 if (!per_cu->is_debug_types
cc12ce38 2880 && per_cu->cu != NULL
89e63ee4
DE
2881 && per_cu->cu->dwo_unit != NULL
2882 && dwarf2_per_objfile->index_table != NULL
2883 && dwarf2_per_objfile->index_table->version <= 7
2884 /* DWP files aren't supported yet. */
ed2dc618 2885 && get_dwp_file (dwarf2_per_objfile) == NULL)
89e63ee4 2886 queue_and_load_all_dwo_tus (per_cu);
95554aad 2887 }
9291a0cd 2888
ed2dc618 2889 process_queue (dwarf2_per_objfile);
9291a0cd
TT
2890
2891 /* Age the cache, releasing compilation units that have not
2892 been used recently. */
ed2dc618 2893 age_cached_comp_units (dwarf2_per_objfile);
9291a0cd
TT
2894}
2895
2896/* Ensure that the symbols for PER_CU have been read in. OBJFILE is
2897 the objfile from which this CU came. Returns the resulting symbol
2898 table. */
2fdf6df6 2899
43f3e411 2900static struct compunit_symtab *
58f0c718 2901dw2_instantiate_symtab (struct dwarf2_per_cu_data *per_cu, bool skip_partial)
9291a0cd 2902{
ed2dc618
SM
2903 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
2904
95554aad 2905 gdb_assert (dwarf2_per_objfile->using_index);
43f3e411 2906 if (!per_cu->v.quick->compunit_symtab)
9291a0cd 2907 {
11ed8cad 2908 free_cached_comp_units freer (dwarf2_per_objfile);
c83dd867 2909 scoped_restore decrementer = increment_reading_symtab ();
58f0c718 2910 dw2_do_instantiate_symtab (per_cu, skip_partial);
ed2dc618 2911 process_cu_includes (dwarf2_per_objfile);
9291a0cd 2912 }
f194fefb 2913
43f3e411 2914 return per_cu->v.quick->compunit_symtab;
9291a0cd
TT
2915}
2916
ff4c9fec 2917/* See declaration. */
f4dc4d17 2918
ff4c9fec
SM
2919dwarf2_per_cu_data *
2920dwarf2_per_objfile::get_cutu (int index)
2921{
b76e467d 2922 if (index >= this->all_comp_units.size ())
ff4c9fec 2923 {
b76e467d 2924 index -= this->all_comp_units.size ();
b2bdb8cf 2925 gdb_assert (index < this->all_type_units.size ());
ff4c9fec
SM
2926 return &this->all_type_units[index]->per_cu;
2927 }
f4dc4d17 2928
ff4c9fec
SM
2929 return this->all_comp_units[index];
2930}
f4dc4d17 2931
ff4c9fec 2932/* See declaration. */
2fdf6df6 2933
ff4c9fec
SM
2934dwarf2_per_cu_data *
2935dwarf2_per_objfile::get_cu (int index)
1fd400ff 2936{
b76e467d 2937 gdb_assert (index >= 0 && index < this->all_comp_units.size ());
f4dc4d17 2938
ff4c9fec 2939 return this->all_comp_units[index];
f4dc4d17
DE
2940}
2941
ff4c9fec 2942/* See declaration. */
f4dc4d17 2943
ff4c9fec
SM
2944signatured_type *
2945dwarf2_per_objfile::get_tu (int index)
f4dc4d17 2946{
b2bdb8cf 2947 gdb_assert (index >= 0 && index < this->all_type_units.size ());
f4dc4d17 2948
ff4c9fec 2949 return this->all_type_units[index];
1fd400ff
TT
2950}
2951
4b514bc8
JK
2952/* Return a new dwarf2_per_cu_data allocated on OBJFILE's
2953 objfile_obstack, and constructed with the specified field
2954 values. */
2955
2956static dwarf2_per_cu_data *
ed2dc618 2957create_cu_from_index_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
4b514bc8
JK
2958 struct dwarf2_section_info *section,
2959 int is_dwz,
2960 sect_offset sect_off, ULONGEST length)
2961{
ed2dc618 2962 struct objfile *objfile = dwarf2_per_objfile->objfile;
4b514bc8
JK
2963 dwarf2_per_cu_data *the_cu
2964 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2965 struct dwarf2_per_cu_data);
2966 the_cu->sect_off = sect_off;
2967 the_cu->length = length;
e3b94546 2968 the_cu->dwarf2_per_objfile = dwarf2_per_objfile;
4b514bc8
JK
2969 the_cu->section = section;
2970 the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2971 struct dwarf2_per_cu_quick_data);
2972 the_cu->is_dwz = is_dwz;
2973 return the_cu;
2974}
2975
2ec9a5e0
TT
2976/* A helper for create_cus_from_index that handles a given list of
2977 CUs. */
2fdf6df6 2978
74a0d9f6 2979static void
12359b5e 2980create_cus_from_index_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
2ec9a5e0
TT
2981 const gdb_byte *cu_list, offset_type n_elements,
2982 struct dwarf2_section_info *section,
b76e467d 2983 int is_dwz)
9291a0cd 2984{
12359b5e 2985 for (offset_type i = 0; i < n_elements; i += 2)
9291a0cd 2986 {
74a0d9f6 2987 gdb_static_assert (sizeof (ULONGEST) >= 8);
9c541725
PA
2988
2989 sect_offset sect_off
2990 = (sect_offset) extract_unsigned_integer (cu_list, 8, BFD_ENDIAN_LITTLE);
2991 ULONGEST length = extract_unsigned_integer (cu_list + 8, 8, BFD_ENDIAN_LITTLE);
9291a0cd
TT
2992 cu_list += 2 * 8;
2993
b76e467d 2994 dwarf2_per_cu_data *per_cu
ed2dc618
SM
2995 = create_cu_from_index_list (dwarf2_per_objfile, section, is_dwz,
2996 sect_off, length);
b76e467d 2997 dwarf2_per_objfile->all_comp_units.push_back (per_cu);
9291a0cd 2998 }
9291a0cd
TT
2999}
3000
2ec9a5e0 3001/* Read the CU list from the mapped index, and use it to create all
74a0d9f6 3002 the CU objects for this objfile. */
2ec9a5e0 3003
74a0d9f6 3004static void
12359b5e 3005create_cus_from_index (struct dwarf2_per_objfile *dwarf2_per_objfile,
2ec9a5e0
TT
3006 const gdb_byte *cu_list, offset_type cu_list_elements,
3007 const gdb_byte *dwz_list, offset_type dwz_elements)
3008{
b76e467d
SM
3009 gdb_assert (dwarf2_per_objfile->all_comp_units.empty ());
3010 dwarf2_per_objfile->all_comp_units.reserve
3011 ((cu_list_elements + dwz_elements) / 2);
2ec9a5e0 3012
12359b5e 3013 create_cus_from_index_list (dwarf2_per_objfile, cu_list, cu_list_elements,
b76e467d 3014 &dwarf2_per_objfile->info, 0);
2ec9a5e0
TT
3015
3016 if (dwz_elements == 0)
74a0d9f6 3017 return;
2ec9a5e0 3018
12359b5e
SM
3019 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
3020 create_cus_from_index_list (dwarf2_per_objfile, dwz_list, dwz_elements,
b76e467d 3021 &dwz->info, 1);
2ec9a5e0
TT
3022}
3023
1fd400ff 3024/* Create the signatured type hash table from the index. */
673bfd45 3025
74a0d9f6 3026static void
12359b5e
SM
3027create_signatured_type_table_from_index
3028 (struct dwarf2_per_objfile *dwarf2_per_objfile,
3029 struct dwarf2_section_info *section,
3030 const gdb_byte *bytes,
3031 offset_type elements)
1fd400ff 3032{
12359b5e 3033 struct objfile *objfile = dwarf2_per_objfile->objfile;
1fd400ff 3034
b2bdb8cf
SM
3035 gdb_assert (dwarf2_per_objfile->all_type_units.empty ());
3036 dwarf2_per_objfile->all_type_units.reserve (elements / 3);
1fd400ff 3037
12359b5e 3038 htab_t sig_types_hash = allocate_signatured_type_table (objfile);
1fd400ff 3039
12359b5e 3040 for (offset_type i = 0; i < elements; i += 3)
1fd400ff 3041 {
52dc124a 3042 struct signatured_type *sig_type;
9c541725 3043 ULONGEST signature;
1fd400ff 3044 void **slot;
9c541725 3045 cu_offset type_offset_in_tu;
1fd400ff 3046
74a0d9f6 3047 gdb_static_assert (sizeof (ULONGEST) >= 8);
9c541725
PA
3048 sect_offset sect_off
3049 = (sect_offset) extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
3050 type_offset_in_tu
3051 = (cu_offset) extract_unsigned_integer (bytes + 8, 8,
3052 BFD_ENDIAN_LITTLE);
1fd400ff
TT
3053 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
3054 bytes += 3 * 8;
3055
52dc124a 3056 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1fd400ff 3057 struct signatured_type);
52dc124a 3058 sig_type->signature = signature;
9c541725 3059 sig_type->type_offset_in_tu = type_offset_in_tu;
3019eac3 3060 sig_type->per_cu.is_debug_types = 1;
8a0459fd 3061 sig_type->per_cu.section = section;
9c541725 3062 sig_type->per_cu.sect_off = sect_off;
e3b94546 3063 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
52dc124a 3064 sig_type->per_cu.v.quick
1fd400ff
TT
3065 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3066 struct dwarf2_per_cu_quick_data);
3067
52dc124a
DE
3068 slot = htab_find_slot (sig_types_hash, sig_type, INSERT);
3069 *slot = sig_type;
1fd400ff 3070
b2bdb8cf 3071 dwarf2_per_objfile->all_type_units.push_back (sig_type);
1fd400ff
TT
3072 }
3073
673bfd45 3074 dwarf2_per_objfile->signatured_types = sig_types_hash;
1fd400ff
TT
3075}
3076
927aa2e7
JK
3077/* Create the signatured type hash table from .debug_names. */
3078
3079static void
3080create_signatured_type_table_from_debug_names
ed2dc618 3081 (struct dwarf2_per_objfile *dwarf2_per_objfile,
927aa2e7
JK
3082 const mapped_debug_names &map,
3083 struct dwarf2_section_info *section,
3084 struct dwarf2_section_info *abbrev_section)
3085{
ed2dc618
SM
3086 struct objfile *objfile = dwarf2_per_objfile->objfile;
3087
927aa2e7
JK
3088 dwarf2_read_section (objfile, section);
3089 dwarf2_read_section (objfile, abbrev_section);
3090
b2bdb8cf
SM
3091 gdb_assert (dwarf2_per_objfile->all_type_units.empty ());
3092 dwarf2_per_objfile->all_type_units.reserve (map.tu_count);
927aa2e7
JK
3093
3094 htab_t sig_types_hash = allocate_signatured_type_table (objfile);
3095
3096 for (uint32_t i = 0; i < map.tu_count; ++i)
3097 {
3098 struct signatured_type *sig_type;
927aa2e7 3099 void **slot;
927aa2e7
JK
3100
3101 sect_offset sect_off
3102 = (sect_offset) (extract_unsigned_integer
3103 (map.tu_table_reordered + i * map.offset_size,
3104 map.offset_size,
3105 map.dwarf5_byte_order));
3106
3107 comp_unit_head cu_header;
ed2dc618
SM
3108 read_and_check_comp_unit_head (dwarf2_per_objfile, &cu_header, section,
3109 abbrev_section,
927aa2e7
JK
3110 section->buffer + to_underlying (sect_off),
3111 rcuh_kind::TYPE);
3112
3113 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3114 struct signatured_type);
3115 sig_type->signature = cu_header.signature;
3116 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
3117 sig_type->per_cu.is_debug_types = 1;
3118 sig_type->per_cu.section = section;
3119 sig_type->per_cu.sect_off = sect_off;
e3b94546 3120 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
927aa2e7
JK
3121 sig_type->per_cu.v.quick
3122 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3123 struct dwarf2_per_cu_quick_data);
3124
3125 slot = htab_find_slot (sig_types_hash, sig_type, INSERT);
3126 *slot = sig_type;
3127
b2bdb8cf 3128 dwarf2_per_objfile->all_type_units.push_back (sig_type);
927aa2e7
JK
3129 }
3130
3131 dwarf2_per_objfile->signatured_types = sig_types_hash;
3132}
3133
9291a0cd
TT
3134/* Read the address map data from the mapped index, and use it to
3135 populate the objfile's psymtabs_addrmap. */
2fdf6df6 3136
9291a0cd 3137static void
ed2dc618
SM
3138create_addrmap_from_index (struct dwarf2_per_objfile *dwarf2_per_objfile,
3139 struct mapped_index *index)
9291a0cd 3140{
ed2dc618 3141 struct objfile *objfile = dwarf2_per_objfile->objfile;
3e29f34a 3142 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9291a0cd 3143 const gdb_byte *iter, *end;
9291a0cd 3144 struct addrmap *mutable_map;
9291a0cd
TT
3145 CORE_ADDR baseaddr;
3146
8268c778
PA
3147 auto_obstack temp_obstack;
3148
9291a0cd
TT
3149 mutable_map = addrmap_create_mutable (&temp_obstack);
3150
f00a2de2
PA
3151 iter = index->address_table.data ();
3152 end = iter + index->address_table.size ();
9291a0cd
TT
3153
3154 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
3155
3156 while (iter < end)
3157 {
3158 ULONGEST hi, lo, cu_index;
3159 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
3160 iter += 8;
3161 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
3162 iter += 8;
3163 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
3164 iter += 4;
f652bce2 3165
24a55014 3166 if (lo > hi)
f652bce2 3167 {
b98664d3 3168 complaint (_(".gdb_index address table has invalid range (%s - %s)"),
c0cd8254 3169 hex_string (lo), hex_string (hi));
24a55014 3170 continue;
f652bce2 3171 }
24a55014 3172
b76e467d 3173 if (cu_index >= dwarf2_per_objfile->all_comp_units.size ())
f652bce2 3174 {
b98664d3 3175 complaint (_(".gdb_index address table has invalid CU number %u"),
f652bce2 3176 (unsigned) cu_index);
24a55014 3177 continue;
f652bce2 3178 }
24a55014 3179
3e29f34a
MR
3180 lo = gdbarch_adjust_dwarf2_addr (gdbarch, lo + baseaddr);
3181 hi = gdbarch_adjust_dwarf2_addr (gdbarch, hi + baseaddr);
ed2dc618 3182 addrmap_set_empty (mutable_map, lo, hi - 1,
ff4c9fec 3183 dwarf2_per_objfile->get_cu (cu_index));
9291a0cd
TT
3184 }
3185
3186 objfile->psymtabs_addrmap = addrmap_create_fixed (mutable_map,
3187 &objfile->objfile_obstack);
9291a0cd
TT
3188}
3189
927aa2e7
JK
3190/* Read the address map data from DWARF-5 .debug_aranges, and use it to
3191 populate the objfile's psymtabs_addrmap. */
3192
3193static void
ed2dc618 3194create_addrmap_from_aranges (struct dwarf2_per_objfile *dwarf2_per_objfile,
927aa2e7
JK
3195 struct dwarf2_section_info *section)
3196{
ed2dc618 3197 struct objfile *objfile = dwarf2_per_objfile->objfile;
927aa2e7
JK
3198 bfd *abfd = objfile->obfd;
3199 struct gdbarch *gdbarch = get_objfile_arch (objfile);
3200 const CORE_ADDR baseaddr = ANOFFSET (objfile->section_offsets,
3201 SECT_OFF_TEXT (objfile));
3202
3203 auto_obstack temp_obstack;
3204 addrmap *mutable_map = addrmap_create_mutable (&temp_obstack);
3205
3206 std::unordered_map<sect_offset,
3207 dwarf2_per_cu_data *,
3208 gdb::hash_enum<sect_offset>>
3209 debug_info_offset_to_per_cu;
b76e467d 3210 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
927aa2e7 3211 {
927aa2e7
JK
3212 const auto insertpair
3213 = debug_info_offset_to_per_cu.emplace (per_cu->sect_off, per_cu);
3214 if (!insertpair.second)
3215 {
3216 warning (_("Section .debug_aranges in %s has duplicate "
9d8780f0
SM
3217 "debug_info_offset %s, ignoring .debug_aranges."),
3218 objfile_name (objfile), sect_offset_str (per_cu->sect_off));
927aa2e7
JK
3219 return;
3220 }
3221 }
3222
3223 dwarf2_read_section (objfile, section);
3224
3225 const bfd_endian dwarf5_byte_order = gdbarch_byte_order (gdbarch);
3226
3227 const gdb_byte *addr = section->buffer;
3228
3229 while (addr < section->buffer + section->size)
3230 {
3231 const gdb_byte *const entry_addr = addr;
3232 unsigned int bytes_read;
3233
3234 const LONGEST entry_length = read_initial_length (abfd, addr,
3235 &bytes_read);
3236 addr += bytes_read;
3237
3238 const gdb_byte *const entry_end = addr + entry_length;
3239 const bool dwarf5_is_dwarf64 = bytes_read != 4;
3240 const uint8_t offset_size = dwarf5_is_dwarf64 ? 8 : 4;
3241 if (addr + entry_length > section->buffer + section->size)
3242 {
3243 warning (_("Section .debug_aranges in %s entry at offset %zu "
3244 "length %s exceeds section length %s, "
3245 "ignoring .debug_aranges."),
3246 objfile_name (objfile), entry_addr - section->buffer,
3247 plongest (bytes_read + entry_length),
3248 pulongest (section->size));
3249 return;
3250 }
3251
3252 /* The version number. */
3253 const uint16_t version = read_2_bytes (abfd, addr);
3254 addr += 2;
3255 if (version != 2)
3256 {
3257 warning (_("Section .debug_aranges in %s entry at offset %zu "
3258 "has unsupported version %d, ignoring .debug_aranges."),
3259 objfile_name (objfile), entry_addr - section->buffer,
3260 version);
3261 return;
3262 }
3263
3264 const uint64_t debug_info_offset
3265 = extract_unsigned_integer (addr, offset_size, dwarf5_byte_order);
3266 addr += offset_size;
3267 const auto per_cu_it
3268 = debug_info_offset_to_per_cu.find (sect_offset (debug_info_offset));
3269 if (per_cu_it == debug_info_offset_to_per_cu.cend ())
3270 {
3271 warning (_("Section .debug_aranges in %s entry at offset %zu "
3272 "debug_info_offset %s does not exists, "
3273 "ignoring .debug_aranges."),
3274 objfile_name (objfile), entry_addr - section->buffer,
3275 pulongest (debug_info_offset));
3276 return;
3277 }
3278 dwarf2_per_cu_data *const per_cu = per_cu_it->second;
3279
3280 const uint8_t address_size = *addr++;
3281 if (address_size < 1 || address_size > 8)
3282 {
3283 warning (_("Section .debug_aranges in %s entry at offset %zu "
3284 "address_size %u is invalid, ignoring .debug_aranges."),
3285 objfile_name (objfile), entry_addr - section->buffer,
3286 address_size);
3287 return;
3288 }
3289
3290 const uint8_t segment_selector_size = *addr++;
3291 if (segment_selector_size != 0)
3292 {
3293 warning (_("Section .debug_aranges in %s entry at offset %zu "
3294 "segment_selector_size %u is not supported, "
3295 "ignoring .debug_aranges."),
3296 objfile_name (objfile), entry_addr - section->buffer,
3297 segment_selector_size);
3298 return;
3299 }
3300
3301 /* Must pad to an alignment boundary that is twice the address
3302 size. It is undocumented by the DWARF standard but GCC does
3303 use it. */
3304 for (size_t padding = ((-(addr - section->buffer))
3305 & (2 * address_size - 1));
3306 padding > 0; padding--)
3307 if (*addr++ != 0)
3308 {
3309 warning (_("Section .debug_aranges in %s entry at offset %zu "
3310 "padding is not zero, ignoring .debug_aranges."),
3311 objfile_name (objfile), entry_addr - section->buffer);
3312 return;
3313 }
3314
3315 for (;;)
3316 {
3317 if (addr + 2 * address_size > entry_end)
3318 {
3319 warning (_("Section .debug_aranges in %s entry at offset %zu "
3320 "address list is not properly terminated, "
3321 "ignoring .debug_aranges."),
3322 objfile_name (objfile), entry_addr - section->buffer);
3323 return;
3324 }
3325 ULONGEST start = extract_unsigned_integer (addr, address_size,
3326 dwarf5_byte_order);
3327 addr += address_size;
3328 ULONGEST length = extract_unsigned_integer (addr, address_size,
3329 dwarf5_byte_order);
3330 addr += address_size;
3331 if (start == 0 && length == 0)
3332 break;
3333 if (start == 0 && !dwarf2_per_objfile->has_section_at_zero)
3334 {
3335 /* Symbol was eliminated due to a COMDAT group. */
3336 continue;
3337 }
3338 ULONGEST end = start + length;
3339 start = gdbarch_adjust_dwarf2_addr (gdbarch, start + baseaddr);
3340 end = gdbarch_adjust_dwarf2_addr (gdbarch, end + baseaddr);
3341 addrmap_set_empty (mutable_map, start, end - 1, per_cu);
3342 }
3343 }
3344
3345 objfile->psymtabs_addrmap = addrmap_create_fixed (mutable_map,
3346 &objfile->objfile_obstack);
3347}
3348
9291a0cd
TT
3349/* Find a slot in the mapped index INDEX for the object named NAME.
3350 If NAME is found, set *VEC_OUT to point to the CU vector in the
109483d9
PA
3351 constant pool and return true. If NAME cannot be found, return
3352 false. */
2fdf6df6 3353
109483d9 3354static bool
9291a0cd
TT
3355find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
3356 offset_type **vec_out)
3357{
0cf03b49 3358 offset_type hash;
9291a0cd 3359 offset_type slot, step;
559a7a62 3360 int (*cmp) (const char *, const char *);
9291a0cd 3361
791afaa2 3362 gdb::unique_xmalloc_ptr<char> without_params;
0cf03b49 3363 if (current_language->la_language == language_cplus
45280282
IB
3364 || current_language->la_language == language_fortran
3365 || current_language->la_language == language_d)
0cf03b49
JK
3366 {
3367 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
3368 not contain any. */
a8719064 3369
72998fb3 3370 if (strchr (name, '(') != NULL)
0cf03b49 3371 {
109483d9 3372 without_params = cp_remove_params (name);
0cf03b49 3373
72998fb3 3374 if (without_params != NULL)
791afaa2 3375 name = without_params.get ();
0cf03b49
JK
3376 }
3377 }
3378
559a7a62 3379 /* Index version 4 did not support case insensitive searches. But the
feea76c2 3380 indices for case insensitive languages are built in lowercase, therefore
559a7a62
JK
3381 simulate our NAME being searched is also lowercased. */
3382 hash = mapped_index_string_hash ((index->version == 4
3383 && case_sensitivity == case_sensitive_off
3384 ? 5 : index->version),
3385 name);
3386
f00a2de2
PA
3387 slot = hash & (index->symbol_table.size () - 1);
3388 step = ((hash * 17) & (index->symbol_table.size () - 1)) | 1;
559a7a62 3389 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
9291a0cd
TT
3390
3391 for (;;)
3392 {
9291a0cd 3393 const char *str;
f00a2de2
PA
3394
3395 const auto &bucket = index->symbol_table[slot];
3396 if (bucket.name == 0 && bucket.vec == 0)
109483d9 3397 return false;
9291a0cd 3398
f00a2de2 3399 str = index->constant_pool + MAYBE_SWAP (bucket.name);
559a7a62 3400 if (!cmp (name, str))
9291a0cd
TT
3401 {
3402 *vec_out = (offset_type *) (index->constant_pool
f00a2de2 3403 + MAYBE_SWAP (bucket.vec));
109483d9 3404 return true;
9291a0cd
TT
3405 }
3406
f00a2de2 3407 slot = (slot + step) & (index->symbol_table.size () - 1);
9291a0cd
TT
3408 }
3409}
3410
2ec9a5e0
TT
3411/* A helper function that reads the .gdb_index from SECTION and fills
3412 in MAP. FILENAME is the name of the file containing the section;
d33bc52e 3413 it is used for error reporting. DEPRECATED_OK is true if it is
2ec9a5e0
TT
3414 ok to use deprecated sections.
3415
3416 CU_LIST, CU_LIST_ELEMENTS, TYPES_LIST, and TYPES_LIST_ELEMENTS are
3417 out parameters that are filled in with information about the CU and
3418 TU lists in the section.
3419
3420 Returns 1 if all went well, 0 otherwise. */
2fdf6df6 3421
d33bc52e 3422static bool
7b23e087
SM
3423read_gdb_index_from_section (struct objfile *objfile,
3424 const char *filename,
3425 bool deprecated_ok,
3426 struct dwarf2_section_info *section,
3427 struct mapped_index *map,
3428 const gdb_byte **cu_list,
3429 offset_type *cu_list_elements,
3430 const gdb_byte **types_list,
3431 offset_type *types_list_elements)
9291a0cd 3432{
948f8e3d 3433 const gdb_byte *addr;
2ec9a5e0 3434 offset_type version;
b3b272e1 3435 offset_type *metadata;
1fd400ff 3436 int i;
9291a0cd 3437
2ec9a5e0 3438 if (dwarf2_section_empty_p (section))
9291a0cd 3439 return 0;
82430852
JK
3440
3441 /* Older elfutils strip versions could keep the section in the main
3442 executable while splitting it for the separate debug info file. */
a32a8923 3443 if ((get_section_flags (section) & SEC_HAS_CONTENTS) == 0)
82430852
JK
3444 return 0;
3445
2ec9a5e0 3446 dwarf2_read_section (objfile, section);
9291a0cd 3447
2ec9a5e0 3448 addr = section->buffer;
9291a0cd 3449 /* Version check. */
1fd400ff 3450 version = MAYBE_SWAP (*(offset_type *) addr);
987d643c 3451 /* Versions earlier than 3 emitted every copy of a psymbol. This
a6e293d1 3452 causes the index to behave very poorly for certain requests. Version 3
831adc1f 3453 contained incomplete addrmap. So, it seems better to just ignore such
481860b3 3454 indices. */
831adc1f 3455 if (version < 4)
481860b3
GB
3456 {
3457 static int warning_printed = 0;
3458 if (!warning_printed)
3459 {
3460 warning (_("Skipping obsolete .gdb_index section in %s."),
2ec9a5e0 3461 filename);
481860b3
GB
3462 warning_printed = 1;
3463 }
3464 return 0;
3465 }
3466 /* Index version 4 uses a different hash function than index version
3467 5 and later.
3468
3469 Versions earlier than 6 did not emit psymbols for inlined
3470 functions. Using these files will cause GDB not to be able to
3471 set breakpoints on inlined functions by name, so we ignore these
e615022a
DE
3472 indices unless the user has done
3473 "set use-deprecated-index-sections on". */
2ec9a5e0 3474 if (version < 6 && !deprecated_ok)
481860b3
GB
3475 {
3476 static int warning_printed = 0;
3477 if (!warning_printed)
3478 {
e615022a
DE
3479 warning (_("\
3480Skipping deprecated .gdb_index section in %s.\n\
3481Do \"set use-deprecated-index-sections on\" before the file is read\n\
3482to use the section anyway."),
2ec9a5e0 3483 filename);
481860b3
GB
3484 warning_printed = 1;
3485 }
3486 return 0;
3487 }
796a7ff8 3488 /* Version 7 indices generated by gold refer to the CU for a symbol instead
8943b874
DE
3489 of the TU (for symbols coming from TUs),
3490 http://sourceware.org/bugzilla/show_bug.cgi?id=15021.
3491 Plus gold-generated indices can have duplicate entries for global symbols,
3492 http://sourceware.org/bugzilla/show_bug.cgi?id=15646.
3493 These are just performance bugs, and we can't distinguish gdb-generated
3494 indices from gold-generated ones, so issue no warning here. */
796a7ff8 3495
481860b3 3496 /* Indexes with higher version than the one supported by GDB may be no
594e8718 3497 longer backward compatible. */
796a7ff8 3498 if (version > 8)
594e8718 3499 return 0;
9291a0cd 3500
559a7a62 3501 map->version = version;
9291a0cd
TT
3502
3503 metadata = (offset_type *) (addr + sizeof (offset_type));
1fd400ff
TT
3504
3505 i = 0;
2ec9a5e0
TT
3506 *cu_list = addr + MAYBE_SWAP (metadata[i]);
3507 *cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
3508 / 8);
1fd400ff
TT
3509 ++i;
3510
2ec9a5e0
TT
3511 *types_list = addr + MAYBE_SWAP (metadata[i]);
3512 *types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
3513 - MAYBE_SWAP (metadata[i]))
3514 / 8);
987d643c 3515 ++i;
1fd400ff 3516
f00a2de2
PA
3517 const gdb_byte *address_table = addr + MAYBE_SWAP (metadata[i]);
3518 const gdb_byte *address_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
3519 map->address_table
3520 = gdb::array_view<const gdb_byte> (address_table, address_table_end);
1fd400ff
TT
3521 ++i;
3522
f00a2de2
PA
3523 const gdb_byte *symbol_table = addr + MAYBE_SWAP (metadata[i]);
3524 const gdb_byte *symbol_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
3525 map->symbol_table
3526 = gdb::array_view<mapped_index::symbol_table_slot>
3527 ((mapped_index::symbol_table_slot *) symbol_table,
3528 (mapped_index::symbol_table_slot *) symbol_table_end);
9291a0cd 3529
f00a2de2 3530 ++i;
f9d83a0b 3531 map->constant_pool = (char *) (addr + MAYBE_SWAP (metadata[i]));
1fd400ff 3532
2ec9a5e0
TT
3533 return 1;
3534}
3535
927aa2e7 3536/* Read .gdb_index. If everything went ok, initialize the "quick"
2ec9a5e0
TT
3537 elements of all the CUs and return 1. Otherwise, return 0. */
3538
3539static int
7b23e087 3540dwarf2_read_gdb_index (struct dwarf2_per_objfile *dwarf2_per_objfile)
2ec9a5e0 3541{
2ec9a5e0
TT
3542 const gdb_byte *cu_list, *types_list, *dwz_list = NULL;
3543 offset_type cu_list_elements, types_list_elements, dwz_list_elements = 0;
4db1a1dc 3544 struct dwz_file *dwz;
12359b5e 3545 struct objfile *objfile = dwarf2_per_objfile->objfile;
2ec9a5e0 3546
3063847f 3547 std::unique_ptr<struct mapped_index> map (new struct mapped_index);
7b23e087
SM
3548 if (!read_gdb_index_from_section (objfile, objfile_name (objfile),
3549 use_deprecated_index_sections,
3550 &dwarf2_per_objfile->gdb_index, map.get (),
3551 &cu_list, &cu_list_elements,
3552 &types_list, &types_list_elements))
2ec9a5e0
TT
3553 return 0;
3554
0fefef59 3555 /* Don't use the index if it's empty. */
3063847f 3556 if (map->symbol_table.empty ())
0fefef59
DE
3557 return 0;
3558
2ec9a5e0
TT
3559 /* If there is a .dwz file, read it so we can get its CU list as
3560 well. */
ed2dc618 3561 dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
4db1a1dc 3562 if (dwz != NULL)
2ec9a5e0 3563 {
2ec9a5e0
TT
3564 struct mapped_index dwz_map;
3565 const gdb_byte *dwz_types_ignore;
3566 offset_type dwz_types_elements_ignore;
3567
7b23e087
SM
3568 if (!read_gdb_index_from_section (objfile,
3569 bfd_get_filename (dwz->dwz_bfd), 1,
3570 &dwz->gdb_index, &dwz_map,
3571 &dwz_list, &dwz_list_elements,
3572 &dwz_types_ignore,
3573 &dwz_types_elements_ignore))
2ec9a5e0
TT
3574 {
3575 warning (_("could not read '.gdb_index' section from %s; skipping"),
3576 bfd_get_filename (dwz->dwz_bfd));
3577 return 0;
3578 }
3579 }
3580
12359b5e
SM
3581 create_cus_from_index (dwarf2_per_objfile, cu_list, cu_list_elements,
3582 dwz_list, dwz_list_elements);
1fd400ff 3583
8b70b953
TT
3584 if (types_list_elements)
3585 {
3586 struct dwarf2_section_info *section;
3587
3588 /* We can only handle a single .debug_types when we have an
3589 index. */
3590 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) != 1)
3591 return 0;
3592
3593 section = VEC_index (dwarf2_section_info_def,
3594 dwarf2_per_objfile->types, 0);
3595
12359b5e
SM
3596 create_signatured_type_table_from_index (dwarf2_per_objfile, section,
3597 types_list, types_list_elements);
8b70b953 3598 }
9291a0cd 3599
3063847f 3600 create_addrmap_from_index (dwarf2_per_objfile, map.get ());
9291a0cd 3601
3063847f 3602 dwarf2_per_objfile->index_table = std::move (map);
9291a0cd 3603 dwarf2_per_objfile->using_index = 1;
7b9f3c50 3604 dwarf2_per_objfile->quick_file_names_table =
b76e467d 3605 create_quick_file_names_table (dwarf2_per_objfile->all_comp_units.size ());
9291a0cd
TT
3606
3607 return 1;
3608}
3609
dee91e82 3610/* die_reader_func for dw2_get_file_names. */
2fdf6df6 3611
dee91e82
DE
3612static void
3613dw2_get_file_names_reader (const struct die_reader_specs *reader,
d521ce57 3614 const gdb_byte *info_ptr,
dee91e82
DE
3615 struct die_info *comp_unit_die,
3616 int has_children,
3617 void *data)
9291a0cd 3618{
dee91e82 3619 struct dwarf2_cu *cu = reader->cu;
ed2dc618 3620 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
518817b3
SM
3621 struct dwarf2_per_objfile *dwarf2_per_objfile
3622 = cu->per_cu->dwarf2_per_objfile;
dee91e82 3623 struct objfile *objfile = dwarf2_per_objfile->objfile;
094b34ac 3624 struct dwarf2_per_cu_data *lh_cu;
9291a0cd 3625 struct attribute *attr;
dee91e82 3626 int i;
7b9f3c50
DE
3627 void **slot;
3628 struct quick_file_names *qfn;
9291a0cd 3629
0186c6a7
DE
3630 gdb_assert (! this_cu->is_debug_types);
3631
07261596
TT
3632 /* Our callers never want to match partial units -- instead they
3633 will match the enclosing full CU. */
3634 if (comp_unit_die->tag == DW_TAG_partial_unit)
3635 {
3636 this_cu->v.quick->no_file_data = 1;
3637 return;
3638 }
3639
0186c6a7 3640 lh_cu = this_cu;
7b9f3c50 3641 slot = NULL;
dee91e82 3642
fff8551c 3643 line_header_up lh;
9c541725 3644 sect_offset line_offset {};
fff8551c 3645
dee91e82 3646 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
9291a0cd
TT
3647 if (attr)
3648 {
7b9f3c50
DE
3649 struct quick_file_names find_entry;
3650
9c541725 3651 line_offset = (sect_offset) DW_UNSND (attr);
7b9f3c50
DE
3652
3653 /* We may have already read in this line header (TU line header sharing).
3654 If we have we're done. */
094b34ac 3655 find_entry.hash.dwo_unit = cu->dwo_unit;
9c541725 3656 find_entry.hash.line_sect_off = line_offset;
7b9f3c50
DE
3657 slot = htab_find_slot (dwarf2_per_objfile->quick_file_names_table,
3658 &find_entry, INSERT);
3659 if (*slot != NULL)
3660 {
9a3c8263 3661 lh_cu->v.quick->file_names = (struct quick_file_names *) *slot;
dee91e82 3662 return;
7b9f3c50
DE
3663 }
3664
3019eac3 3665 lh = dwarf_decode_line_header (line_offset, cu);
9291a0cd
TT
3666 }
3667 if (lh == NULL)
3668 {
094b34ac 3669 lh_cu->v.quick->no_file_data = 1;
dee91e82 3670 return;
9291a0cd
TT
3671 }
3672
8d749320 3673 qfn = XOBNEW (&objfile->objfile_obstack, struct quick_file_names);
094b34ac 3674 qfn->hash.dwo_unit = cu->dwo_unit;
9c541725 3675 qfn->hash.line_sect_off = line_offset;
7b9f3c50
DE
3676 gdb_assert (slot != NULL);
3677 *slot = qfn;
9291a0cd 3678
d721ba37 3679 file_and_directory fnd = find_file_and_directory (comp_unit_die, cu);
9291a0cd 3680
fff8551c 3681 qfn->num_file_names = lh->file_names.size ();
8d749320 3682 qfn->file_names =
fff8551c
PA
3683 XOBNEWVEC (&objfile->objfile_obstack, const char *, lh->file_names.size ());
3684 for (i = 0; i < lh->file_names.size (); ++i)
3685 qfn->file_names[i] = file_full_name (i + 1, lh.get (), fnd.comp_dir);
7b9f3c50 3686 qfn->real_names = NULL;
9291a0cd 3687
094b34ac 3688 lh_cu->v.quick->file_names = qfn;
dee91e82
DE
3689}
3690
3691/* A helper for the "quick" functions which attempts to read the line
3692 table for THIS_CU. */
3693
3694static struct quick_file_names *
e4a48d9d 3695dw2_get_file_names (struct dwarf2_per_cu_data *this_cu)
dee91e82 3696{
0186c6a7
DE
3697 /* This should never be called for TUs. */
3698 gdb_assert (! this_cu->is_debug_types);
3699 /* Nor type unit groups. */
3700 gdb_assert (! IS_TYPE_UNIT_GROUP (this_cu));
f4dc4d17 3701
dee91e82
DE
3702 if (this_cu->v.quick->file_names != NULL)
3703 return this_cu->v.quick->file_names;
3704 /* If we know there is no line data, no point in looking again. */
3705 if (this_cu->v.quick->no_file_data)
3706 return NULL;
3707
0186c6a7 3708 init_cutu_and_read_dies_simple (this_cu, dw2_get_file_names_reader, NULL);
dee91e82
DE
3709
3710 if (this_cu->v.quick->no_file_data)
3711 return NULL;
3712 return this_cu->v.quick->file_names;
9291a0cd
TT
3713}
3714
3715/* A helper for the "quick" functions which computes and caches the
7b9f3c50 3716 real path for a given file name from the line table. */
2fdf6df6 3717
9291a0cd 3718static const char *
7b9f3c50
DE
3719dw2_get_real_path (struct objfile *objfile,
3720 struct quick_file_names *qfn, int index)
9291a0cd 3721{
7b9f3c50
DE
3722 if (qfn->real_names == NULL)
3723 qfn->real_names = OBSTACK_CALLOC (&objfile->objfile_obstack,
26f2dc30 3724 qfn->num_file_names, const char *);
9291a0cd 3725
7b9f3c50 3726 if (qfn->real_names[index] == NULL)
14278e1f 3727 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]).release ();
9291a0cd 3728
7b9f3c50 3729 return qfn->real_names[index];
9291a0cd
TT
3730}
3731
3732static struct symtab *
3733dw2_find_last_source_symtab (struct objfile *objfile)
3734{
ed2dc618
SM
3735 struct dwarf2_per_objfile *dwarf2_per_objfile
3736 = get_dwarf2_per_objfile (objfile);
b76e467d 3737 dwarf2_per_cu_data *dwarf_cu = dwarf2_per_objfile->all_comp_units.back ();
58f0c718 3738 compunit_symtab *cust = dw2_instantiate_symtab (dwarf_cu, false);
ae2de4f8 3739
43f3e411
DE
3740 if (cust == NULL)
3741 return NULL;
ed2dc618 3742
43f3e411 3743 return compunit_primary_filetab (cust);
9291a0cd
TT
3744}
3745
7b9f3c50
DE
3746/* Traversal function for dw2_forget_cached_source_info. */
3747
3748static int
3749dw2_free_cached_file_names (void **slot, void *info)
9291a0cd 3750{
7b9f3c50 3751 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
9291a0cd 3752
7b9f3c50 3753 if (file_data->real_names)
9291a0cd 3754 {
7b9f3c50 3755 int i;
9291a0cd 3756
7b9f3c50 3757 for (i = 0; i < file_data->num_file_names; ++i)
9291a0cd 3758 {
7b9f3c50
DE
3759 xfree ((void*) file_data->real_names[i]);
3760 file_data->real_names[i] = NULL;
9291a0cd
TT
3761 }
3762 }
7b9f3c50
DE
3763
3764 return 1;
3765}
3766
3767static void
3768dw2_forget_cached_source_info (struct objfile *objfile)
3769{
ed2dc618
SM
3770 struct dwarf2_per_objfile *dwarf2_per_objfile
3771 = get_dwarf2_per_objfile (objfile);
7b9f3c50
DE
3772
3773 htab_traverse_noresize (dwarf2_per_objfile->quick_file_names_table,
3774 dw2_free_cached_file_names, NULL);
9291a0cd
TT
3775}
3776
f8eba3c6
TT
3777/* Helper function for dw2_map_symtabs_matching_filename that expands
3778 the symtabs and calls the iterator. */
3779
3780static int
3781dw2_map_expand_apply (struct objfile *objfile,
3782 struct dwarf2_per_cu_data *per_cu,
f5b95b50 3783 const char *name, const char *real_path,
14bc53a8 3784 gdb::function_view<bool (symtab *)> callback)
f8eba3c6 3785{
43f3e411 3786 struct compunit_symtab *last_made = objfile->compunit_symtabs;
f8eba3c6
TT
3787
3788 /* Don't visit already-expanded CUs. */
43f3e411 3789 if (per_cu->v.quick->compunit_symtab)
f8eba3c6
TT
3790 return 0;
3791
3792 /* This may expand more than one symtab, and we want to iterate over
3793 all of them. */
58f0c718 3794 dw2_instantiate_symtab (per_cu, false);
f8eba3c6 3795
14bc53a8
PA
3796 return iterate_over_some_symtabs (name, real_path, objfile->compunit_symtabs,
3797 last_made, callback);
f8eba3c6
TT
3798}
3799
3800/* Implementation of the map_symtabs_matching_filename method. */
3801
14bc53a8
PA
3802static bool
3803dw2_map_symtabs_matching_filename
3804 (struct objfile *objfile, const char *name, const char *real_path,
3805 gdb::function_view<bool (symtab *)> callback)
9291a0cd 3806{
c011a4f4 3807 const char *name_basename = lbasename (name);
ed2dc618
SM
3808 struct dwarf2_per_objfile *dwarf2_per_objfile
3809 = get_dwarf2_per_objfile (objfile);
ae2de4f8 3810
848e3e78
DE
3811 /* The rule is CUs specify all the files, including those used by
3812 any TU, so there's no need to scan TUs here. */
f4dc4d17 3813
b76e467d 3814 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
9291a0cd 3815 {
3d7bb9d9 3816 /* We only need to look at symtabs not already expanded. */
43f3e411 3817 if (per_cu->v.quick->compunit_symtab)
9291a0cd
TT
3818 continue;
3819
b76e467d 3820 quick_file_names *file_data = dw2_get_file_names (per_cu);
7b9f3c50 3821 if (file_data == NULL)
9291a0cd
TT
3822 continue;
3823
b76e467d 3824 for (int j = 0; j < file_data->num_file_names; ++j)
9291a0cd 3825 {
7b9f3c50 3826 const char *this_name = file_data->file_names[j];
da235a7c 3827 const char *this_real_name;
9291a0cd 3828
af529f8f 3829 if (compare_filenames_for_search (this_name, name))
9291a0cd 3830 {
f5b95b50 3831 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
14bc53a8
PA
3832 callback))
3833 return true;
288e77a7 3834 continue;
4aac40c8 3835 }
9291a0cd 3836
c011a4f4
DE
3837 /* Before we invoke realpath, which can get expensive when many
3838 files are involved, do a quick comparison of the basenames. */
3839 if (! basenames_may_differ
3840 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
3841 continue;
3842
da235a7c
JK
3843 this_real_name = dw2_get_real_path (objfile, file_data, j);
3844 if (compare_filenames_for_search (this_real_name, name))
9291a0cd 3845 {
da235a7c 3846 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
14bc53a8
PA
3847 callback))
3848 return true;
288e77a7 3849 continue;
da235a7c 3850 }
9291a0cd 3851
da235a7c
JK
3852 if (real_path != NULL)
3853 {
af529f8f
JK
3854 gdb_assert (IS_ABSOLUTE_PATH (real_path));
3855 gdb_assert (IS_ABSOLUTE_PATH (name));
7b9f3c50 3856 if (this_real_name != NULL
af529f8f 3857 && FILENAME_CMP (real_path, this_real_name) == 0)
9291a0cd 3858 {
f5b95b50 3859 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
14bc53a8
PA
3860 callback))
3861 return true;
288e77a7 3862 continue;
9291a0cd
TT
3863 }
3864 }
3865 }
3866 }
3867
14bc53a8 3868 return false;
9291a0cd
TT
3869}
3870
da51c347
DE
3871/* Struct used to manage iterating over all CUs looking for a symbol. */
3872
3873struct dw2_symtab_iterator
9291a0cd 3874{
ed2dc618
SM
3875 /* The dwarf2_per_objfile owning the CUs we are iterating on. */
3876 struct dwarf2_per_objfile *dwarf2_per_objfile;
da51c347
DE
3877 /* If non-zero, only look for symbols that match BLOCK_INDEX. */
3878 int want_specific_block;
3879 /* One of GLOBAL_BLOCK or STATIC_BLOCK.
3880 Unused if !WANT_SPECIFIC_BLOCK. */
3881 int block_index;
3882 /* The kind of symbol we're looking for. */
3883 domain_enum domain;
3884 /* The list of CUs from the index entry of the symbol,
3885 or NULL if not found. */
3886 offset_type *vec;
3887 /* The next element in VEC to look at. */
3888 int next;
3889 /* The number of elements in VEC, or zero if there is no match. */
3890 int length;
8943b874
DE
3891 /* Have we seen a global version of the symbol?
3892 If so we can ignore all further global instances.
3893 This is to work around gold/15646, inefficient gold-generated
3894 indices. */
3895 int global_seen;
da51c347 3896};
9291a0cd 3897
da51c347
DE
3898/* Initialize the index symtab iterator ITER.
3899 If WANT_SPECIFIC_BLOCK is non-zero, only look for symbols
3900 in block BLOCK_INDEX. Otherwise BLOCK_INDEX is ignored. */
2fdf6df6 3901
9291a0cd 3902static void
da51c347 3903dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
ed2dc618 3904 struct dwarf2_per_objfile *dwarf2_per_objfile,
da51c347
DE
3905 int want_specific_block,
3906 int block_index,
3907 domain_enum domain,
3908 const char *name)
3909{
ed2dc618 3910 iter->dwarf2_per_objfile = dwarf2_per_objfile;
da51c347
DE
3911 iter->want_specific_block = want_specific_block;
3912 iter->block_index = block_index;
3913 iter->domain = domain;
3914 iter->next = 0;
8943b874 3915 iter->global_seen = 0;
da51c347 3916
3063847f 3917 mapped_index *index = dwarf2_per_objfile->index_table.get ();
ed2dc618
SM
3918
3919 /* index is NULL if OBJF_READNOW. */
3920 if (index != NULL && find_slot_in_mapped_hash (index, name, &iter->vec))
da51c347
DE
3921 iter->length = MAYBE_SWAP (*iter->vec);
3922 else
3923 {
3924 iter->vec = NULL;
3925 iter->length = 0;
3926 }
3927}
3928
3929/* Return the next matching CU or NULL if there are no more. */
3930
3931static struct dwarf2_per_cu_data *
3932dw2_symtab_iter_next (struct dw2_symtab_iterator *iter)
3933{
ed2dc618
SM
3934 struct dwarf2_per_objfile *dwarf2_per_objfile = iter->dwarf2_per_objfile;
3935
da51c347
DE
3936 for ( ; iter->next < iter->length; ++iter->next)
3937 {
3938 offset_type cu_index_and_attrs =
3939 MAYBE_SWAP (iter->vec[iter->next + 1]);
3940 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
da51c347
DE
3941 int want_static = iter->block_index != GLOBAL_BLOCK;
3942 /* This value is only valid for index versions >= 7. */
3943 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
3944 gdb_index_symbol_kind symbol_kind =
3945 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3946 /* Only check the symbol attributes if they're present.
3947 Indices prior to version 7 don't record them,
3948 and indices >= 7 may elide them for certain symbols
3949 (gold does this). */
3950 int attrs_valid =
ed2dc618 3951 (dwarf2_per_objfile->index_table->version >= 7
da51c347
DE
3952 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
3953
3190f0c6 3954 /* Don't crash on bad data. */
b76e467d 3955 if (cu_index >= (dwarf2_per_objfile->all_comp_units.size ()
b2bdb8cf 3956 + dwarf2_per_objfile->all_type_units.size ()))
3190f0c6 3957 {
b98664d3 3958 complaint (_(".gdb_index entry has bad CU index"
4262abfb
JK
3959 " [in module %s]"),
3960 objfile_name (dwarf2_per_objfile->objfile));
3190f0c6
DE
3961 continue;
3962 }
3963
ff4c9fec 3964 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (cu_index);
3190f0c6 3965
da51c347 3966 /* Skip if already read in. */
43f3e411 3967 if (per_cu->v.quick->compunit_symtab)
da51c347
DE
3968 continue;
3969
8943b874
DE
3970 /* Check static vs global. */
3971 if (attrs_valid)
3972 {
3973 if (iter->want_specific_block
3974 && want_static != is_static)
3975 continue;
3976 /* Work around gold/15646. */
3977 if (!is_static && iter->global_seen)
3978 continue;
3979 if (!is_static)
3980 iter->global_seen = 1;
3981 }
da51c347
DE
3982
3983 /* Only check the symbol's kind if it has one. */
3984 if (attrs_valid)
3985 {
3986 switch (iter->domain)
3987 {
3988 case VAR_DOMAIN:
3989 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
3990 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
3991 /* Some types are also in VAR_DOMAIN. */
3992 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3993 continue;
3994 break;
3995 case STRUCT_DOMAIN:
3996 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3997 continue;
3998 break;
3999 case LABEL_DOMAIN:
4000 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
4001 continue;
4002 break;
4003 default:
4004 break;
4005 }
4006 }
4007
4008 ++iter->next;
4009 return per_cu;
4010 }
4011
4012 return NULL;
4013}
4014
43f3e411 4015static struct compunit_symtab *
da51c347
DE
4016dw2_lookup_symbol (struct objfile *objfile, int block_index,
4017 const char *name, domain_enum domain)
9291a0cd 4018{
43f3e411 4019 struct compunit_symtab *stab_best = NULL;
ed2dc618
SM
4020 struct dwarf2_per_objfile *dwarf2_per_objfile
4021 = get_dwarf2_per_objfile (objfile);
9291a0cd 4022
b5ec771e
PA
4023 lookup_name_info lookup_name (name, symbol_name_match_type::FULL);
4024
ed2dc618
SM
4025 struct dw2_symtab_iterator iter;
4026 struct dwarf2_per_cu_data *per_cu;
da51c347 4027
ed2dc618 4028 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, 1, block_index, domain, name);
9291a0cd 4029
ed2dc618
SM
4030 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
4031 {
4032 struct symbol *sym, *with_opaque = NULL;
58f0c718 4033 struct compunit_symtab *stab = dw2_instantiate_symtab (per_cu, false);
ed2dc618
SM
4034 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
4035 struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
da51c347 4036
ed2dc618
SM
4037 sym = block_find_symbol (block, name, domain,
4038 block_find_non_opaque_type_preferred,
4039 &with_opaque);
b2e2f908 4040
ed2dc618
SM
4041 /* Some caution must be observed with overloaded functions
4042 and methods, since the index will not contain any overload
4043 information (but NAME might contain it). */
da51c347 4044
ed2dc618
SM
4045 if (sym != NULL
4046 && SYMBOL_MATCHES_SEARCH_NAME (sym, lookup_name))
4047 return stab;
4048 if (with_opaque != NULL
4049 && SYMBOL_MATCHES_SEARCH_NAME (with_opaque, lookup_name))
4050 stab_best = stab;
da51c347 4051
ed2dc618 4052 /* Keep looking through other CUs. */
9291a0cd 4053 }
9291a0cd 4054
da51c347 4055 return stab_best;
9291a0cd
TT
4056}
4057
4058static void
4059dw2_print_stats (struct objfile *objfile)
4060{
ed2dc618
SM
4061 struct dwarf2_per_objfile *dwarf2_per_objfile
4062 = get_dwarf2_per_objfile (objfile);
b76e467d 4063 int total = (dwarf2_per_objfile->all_comp_units.size ()
b2bdb8cf 4064 + dwarf2_per_objfile->all_type_units.size ());
ed2dc618 4065 int count = 0;
9291a0cd 4066
ed2dc618 4067 for (int i = 0; i < total; ++i)
9291a0cd 4068 {
ff4c9fec 4069 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (i);
9291a0cd 4070
43f3e411 4071 if (!per_cu->v.quick->compunit_symtab)
9291a0cd
TT
4072 ++count;
4073 }
e4a48d9d 4074 printf_filtered (_(" Number of read CUs: %d\n"), total - count);
9291a0cd
TT
4075 printf_filtered (_(" Number of unread CUs: %d\n"), count);
4076}
4077
779bd270
DE
4078/* This dumps minimal information about the index.
4079 It is called via "mt print objfiles".
4080 One use is to verify .gdb_index has been loaded by the
4081 gdb.dwarf2/gdb-index.exp testcase. */
4082
9291a0cd
TT
4083static void
4084dw2_dump (struct objfile *objfile)
4085{
ed2dc618
SM
4086 struct dwarf2_per_objfile *dwarf2_per_objfile
4087 = get_dwarf2_per_objfile (objfile);
4088
779bd270
DE
4089 gdb_assert (dwarf2_per_objfile->using_index);
4090 printf_filtered (".gdb_index:");
4091 if (dwarf2_per_objfile->index_table != NULL)
4092 {
4093 printf_filtered (" version %d\n",
4094 dwarf2_per_objfile->index_table->version);
4095 }
4096 else
4097 printf_filtered (" faked for \"readnow\"\n");
4098 printf_filtered ("\n");
9291a0cd
TT
4099}
4100
4101static void
3189cb12
DE
4102dw2_relocate (struct objfile *objfile,
4103 const struct section_offsets *new_offsets,
4104 const struct section_offsets *delta)
9291a0cd
TT
4105{
4106 /* There's nothing to relocate here. */
4107}
4108
4109static void
4110dw2_expand_symtabs_for_function (struct objfile *objfile,
4111 const char *func_name)
4112{
ed2dc618
SM
4113 struct dwarf2_per_objfile *dwarf2_per_objfile
4114 = get_dwarf2_per_objfile (objfile);
da51c347 4115
ed2dc618
SM
4116 struct dw2_symtab_iterator iter;
4117 struct dwarf2_per_cu_data *per_cu;
da51c347 4118
ed2dc618
SM
4119 /* Note: It doesn't matter what we pass for block_index here. */
4120 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, 0, GLOBAL_BLOCK, VAR_DOMAIN,
4121 func_name);
da51c347 4122
ed2dc618 4123 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
58f0c718 4124 dw2_instantiate_symtab (per_cu, false);
da51c347 4125
9291a0cd
TT
4126}
4127
4128static void
4129dw2_expand_all_symtabs (struct objfile *objfile)
4130{
ed2dc618
SM
4131 struct dwarf2_per_objfile *dwarf2_per_objfile
4132 = get_dwarf2_per_objfile (objfile);
b76e467d 4133 int total_units = (dwarf2_per_objfile->all_comp_units.size ()
b2bdb8cf 4134 + dwarf2_per_objfile->all_type_units.size ());
9291a0cd 4135
ed2dc618 4136 for (int i = 0; i < total_units; ++i)
9291a0cd 4137 {
ff4c9fec 4138 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (i);
9291a0cd 4139
58f0c718
TT
4140 /* We don't want to directly expand a partial CU, because if we
4141 read it with the wrong language, then assertion failures can
4142 be triggered later on. See PR symtab/23010. So, tell
4143 dw2_instantiate_symtab to skip partial CUs -- any important
4144 partial CU will be read via DW_TAG_imported_unit anyway. */
4145 dw2_instantiate_symtab (per_cu, true);
9291a0cd
TT
4146 }
4147}
4148
4149static void
652a8996
JK
4150dw2_expand_symtabs_with_fullname (struct objfile *objfile,
4151 const char *fullname)
9291a0cd 4152{
ed2dc618
SM
4153 struct dwarf2_per_objfile *dwarf2_per_objfile
4154 = get_dwarf2_per_objfile (objfile);
d4637a04
DE
4155
4156 /* We don't need to consider type units here.
4157 This is only called for examining code, e.g. expand_line_sal.
4158 There can be an order of magnitude (or more) more type units
4159 than comp units, and we avoid them if we can. */
4160
b76e467d 4161 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
9291a0cd 4162 {
3d7bb9d9 4163 /* We only need to look at symtabs not already expanded. */
43f3e411 4164 if (per_cu->v.quick->compunit_symtab)
9291a0cd
TT
4165 continue;
4166
b76e467d 4167 quick_file_names *file_data = dw2_get_file_names (per_cu);
7b9f3c50 4168 if (file_data == NULL)
9291a0cd
TT
4169 continue;
4170
b76e467d 4171 for (int j = 0; j < file_data->num_file_names; ++j)
9291a0cd 4172 {
652a8996
JK
4173 const char *this_fullname = file_data->file_names[j];
4174
4175 if (filename_cmp (this_fullname, fullname) == 0)
9291a0cd 4176 {
58f0c718 4177 dw2_instantiate_symtab (per_cu, false);
9291a0cd
TT
4178 break;
4179 }
4180 }
4181 }
4182}
4183
9291a0cd 4184static void
ade7ed9e 4185dw2_map_matching_symbols (struct objfile *objfile,
fe978cb0 4186 const char * name, domain_enum domain,
ade7ed9e 4187 int global,
40658b94
PH
4188 int (*callback) (struct block *,
4189 struct symbol *, void *),
b5ec771e 4190 void *data, symbol_name_match_type match,
2edb89d3 4191 symbol_compare_ftype *ordered_compare)
9291a0cd 4192{
40658b94 4193 /* Currently unimplemented; used for Ada. The function can be called if the
a9e6a4bb
JK
4194 current language is Ada for a non-Ada objfile using GNU index. As Ada
4195 does not look for non-Ada symbols this function should just return. */
9291a0cd
TT
4196}
4197
b5ec771e
PA
4198/* Symbol name matcher for .gdb_index names.
4199
4200 Symbol names in .gdb_index have a few particularities:
4201
4202 - There's no indication of which is the language of each symbol.
4203
4204 Since each language has its own symbol name matching algorithm,
4205 and we don't know which language is the right one, we must match
3f563c84
PA
4206 each symbol against all languages. This would be a potential
4207 performance problem if it were not mitigated by the
4208 mapped_index::name_components lookup table, which significantly
4209 reduces the number of times we need to call into this matcher,
4210 making it a non-issue.
b5ec771e
PA
4211
4212 - Symbol names in the index have no overload (parameter)
4213 information. I.e., in C++, "foo(int)" and "foo(long)" both
4214 appear as "foo" in the index, for example.
4215
4216 This means that the lookup names passed to the symbol name
4217 matcher functions must have no parameter information either
4218 because (e.g.) symbol search name "foo" does not match
4219 lookup-name "foo(int)" [while swapping search name for lookup
4220 name would match].
4221*/
4222class gdb_index_symbol_name_matcher
4223{
4224public:
4225 /* Prepares the vector of comparison functions for LOOKUP_NAME. */
4226 gdb_index_symbol_name_matcher (const lookup_name_info &lookup_name);
4227
4228 /* Walk all the matcher routines and match SYMBOL_NAME against them.
4229 Returns true if any matcher matches. */
4230 bool matches (const char *symbol_name);
4231
4232private:
4233 /* A reference to the lookup name we're matching against. */
4234 const lookup_name_info &m_lookup_name;
4235
4236 /* A vector holding all the different symbol name matchers, for all
4237 languages. */
4238 std::vector<symbol_name_matcher_ftype *> m_symbol_name_matcher_funcs;
4239};
4240
4241gdb_index_symbol_name_matcher::gdb_index_symbol_name_matcher
4242 (const lookup_name_info &lookup_name)
4243 : m_lookup_name (lookup_name)
4244{
4245 /* Prepare the vector of comparison functions upfront, to avoid
4246 doing the same work for each symbol. Care is taken to avoid
4247 matching with the same matcher more than once if/when multiple
4248 languages use the same matcher function. */
4249 auto &matchers = m_symbol_name_matcher_funcs;
4250 matchers.reserve (nr_languages);
4251
4252 matchers.push_back (default_symbol_name_matcher);
4253
4254 for (int i = 0; i < nr_languages; i++)
4255 {
4256 const language_defn *lang = language_def ((enum language) i);
c63d3e8d 4257 symbol_name_matcher_ftype *name_matcher
618daa93 4258 = get_symbol_name_matcher (lang, m_lookup_name);
c63d3e8d
PA
4259
4260 /* Don't insert the same comparison routine more than once.
4261 Note that we do this linear walk instead of a seemingly
4262 cheaper sorted insert, or use a std::set or something like
4263 that, because relative order of function addresses is not
4264 stable. This is not a problem in practice because the number
4265 of supported languages is low, and the cost here is tiny
4266 compared to the number of searches we'll do afterwards using
4267 this object. */
4268 if (name_matcher != default_symbol_name_matcher
4269 && (std::find (matchers.begin (), matchers.end (), name_matcher)
4270 == matchers.end ()))
4271 matchers.push_back (name_matcher);
b5ec771e
PA
4272 }
4273}
4274
4275bool
4276gdb_index_symbol_name_matcher::matches (const char *symbol_name)
4277{
4278 for (auto matches_name : m_symbol_name_matcher_funcs)
4279 if (matches_name (symbol_name, m_lookup_name, NULL))
4280 return true;
4281
4282 return false;
4283}
4284
e1ef7d7a
PA
4285/* Starting from a search name, return the string that finds the upper
4286 bound of all strings that start with SEARCH_NAME in a sorted name
4287 list. Returns the empty string to indicate that the upper bound is
4288 the end of the list. */
4289
4290static std::string
4291make_sort_after_prefix_name (const char *search_name)
4292{
4293 /* When looking to complete "func", we find the upper bound of all
4294 symbols that start with "func" by looking for where we'd insert
4295 the closest string that would follow "func" in lexicographical
4296 order. Usually, that's "func"-with-last-character-incremented,
4297 i.e. "fund". Mind non-ASCII characters, though. Usually those
4298 will be UTF-8 multi-byte sequences, but we can't be certain.
4299 Especially mind the 0xff character, which is a valid character in
4300 non-UTF-8 source character sets (e.g. Latin1 'ÿ'), and we can't
4301 rule out compilers allowing it in identifiers. Note that
4302 conveniently, strcmp/strcasecmp are specified to compare
4303 characters interpreted as unsigned char. So what we do is treat
4304 the whole string as a base 256 number composed of a sequence of
4305 base 256 "digits" and add 1 to it. I.e., adding 1 to 0xff wraps
4306 to 0, and carries 1 to the following more-significant position.
4307 If the very first character in SEARCH_NAME ends up incremented
4308 and carries/overflows, then the upper bound is the end of the
4309 list. The string after the empty string is also the empty
4310 string.
4311
4312 Some examples of this operation:
4313
4314 SEARCH_NAME => "+1" RESULT
4315
4316 "abc" => "abd"
4317 "ab\xff" => "ac"
4318 "\xff" "a" "\xff" => "\xff" "b"
4319 "\xff" => ""
4320 "\xff\xff" => ""
4321 "" => ""
4322
4323 Then, with these symbols for example:
4324
4325 func
4326 func1
4327 fund
4328
4329 completing "func" looks for symbols between "func" and
4330 "func"-with-last-character-incremented, i.e. "fund" (exclusive),
4331 which finds "func" and "func1", but not "fund".
4332
4333 And with:
4334
4335 funcÿ (Latin1 'ÿ' [0xff])
4336 funcÿ1
4337 fund
4338
4339 completing "funcÿ" looks for symbols between "funcÿ" and "fund"
4340 (exclusive), which finds "funcÿ" and "funcÿ1", but not "fund".
4341
4342 And with:
4343
4344 ÿÿ (Latin1 'ÿ' [0xff])
4345 ÿÿ1
4346
4347 completing "ÿ" or "ÿÿ" looks for symbols between between "ÿÿ" and
4348 the end of the list.
4349 */
4350 std::string after = search_name;
4351 while (!after.empty () && (unsigned char) after.back () == 0xff)
4352 after.pop_back ();
4353 if (!after.empty ())
4354 after.back () = (unsigned char) after.back () + 1;
4355 return after;
4356}
4357
5c58de74 4358/* See declaration. */
61d96d7e 4359
5c58de74
PA
4360std::pair<std::vector<name_component>::const_iterator,
4361 std::vector<name_component>::const_iterator>
44ed8f3e 4362mapped_index_base::find_name_components_bounds
5c58de74 4363 (const lookup_name_info &lookup_name_without_params) const
3f563c84 4364{
5c58de74
PA
4365 auto *name_cmp
4366 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
3f563c84
PA
4367
4368 const char *cplus
c62446b1 4369 = lookup_name_without_params.cplus ().lookup_name ().c_str ();
9291a0cd 4370
3f563c84
PA
4371 /* Comparison function object for lower_bound that matches against a
4372 given symbol name. */
4373 auto lookup_compare_lower = [&] (const name_component &elem,
4374 const char *name)
4375 {
5c58de74 4376 const char *elem_qualified = this->symbol_name_at (elem.idx);
3f563c84
PA
4377 const char *elem_name = elem_qualified + elem.name_offset;
4378 return name_cmp (elem_name, name) < 0;
4379 };
4380
4381 /* Comparison function object for upper_bound that matches against a
4382 given symbol name. */
4383 auto lookup_compare_upper = [&] (const char *name,
4384 const name_component &elem)
4385 {
5c58de74 4386 const char *elem_qualified = this->symbol_name_at (elem.idx);
3f563c84
PA
4387 const char *elem_name = elem_qualified + elem.name_offset;
4388 return name_cmp (name, elem_name) < 0;
4389 };
4390
5c58de74
PA
4391 auto begin = this->name_components.begin ();
4392 auto end = this->name_components.end ();
3f563c84
PA
4393
4394 /* Find the lower bound. */
4395 auto lower = [&] ()
4396 {
5c58de74 4397 if (lookup_name_without_params.completion_mode () && cplus[0] == '\0')
3f563c84
PA
4398 return begin;
4399 else
4400 return std::lower_bound (begin, end, cplus, lookup_compare_lower);
4401 } ();
4402
4403 /* Find the upper bound. */
4404 auto upper = [&] ()
4405 {
5c58de74 4406 if (lookup_name_without_params.completion_mode ())
3f563c84 4407 {
e1ef7d7a
PA
4408 /* In completion mode, we want UPPER to point past all
4409 symbols names that have the same prefix. I.e., with
4410 these symbols, and completing "func":
4411
4412 function << lower bound
4413 function1
4414 other_function << upper bound
4415
4416 We find the upper bound by looking for the insertion
4417 point of "func"-with-last-character-incremented,
4418 i.e. "fund". */
4419 std::string after = make_sort_after_prefix_name (cplus);
4420 if (after.empty ())
3f563c84 4421 return end;
e6b2f5ef
PA
4422 return std::lower_bound (lower, end, after.c_str (),
4423 lookup_compare_lower);
3f563c84
PA
4424 }
4425 else
4426 return std::upper_bound (lower, end, cplus, lookup_compare_upper);
4427 } ();
4428
5c58de74
PA
4429 return {lower, upper};
4430}
4431
4432/* See declaration. */
4433
4434void
44ed8f3e 4435mapped_index_base::build_name_components ()
5c58de74
PA
4436{
4437 if (!this->name_components.empty ())
4438 return;
4439
4440 this->name_components_casing = case_sensitivity;
4441 auto *name_cmp
4442 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
4443
4444 /* The code below only knows how to break apart components of C++
4445 symbol names (and other languages that use '::' as
4446 namespace/module separator). If we add support for wild matching
4447 to some language that uses some other operator (E.g., Ada, Go and
4448 D use '.'), then we'll need to try splitting the symbol name
4449 according to that language too. Note that Ada does support wild
4450 matching, but doesn't currently support .gdb_index. */
44ed8f3e
PA
4451 auto count = this->symbol_name_count ();
4452 for (offset_type idx = 0; idx < count; idx++)
5c58de74 4453 {
44ed8f3e 4454 if (this->symbol_name_slot_invalid (idx))
5c58de74
PA
4455 continue;
4456
4457 const char *name = this->symbol_name_at (idx);
4458
4459 /* Add each name component to the name component table. */
4460 unsigned int previous_len = 0;
4461 for (unsigned int current_len = cp_find_first_component (name);
4462 name[current_len] != '\0';
4463 current_len += cp_find_first_component (name + current_len))
4464 {
4465 gdb_assert (name[current_len] == ':');
4466 this->name_components.push_back ({previous_len, idx});
4467 /* Skip the '::'. */
4468 current_len += 2;
4469 previous_len = current_len;
4470 }
4471 this->name_components.push_back ({previous_len, idx});
4472 }
4473
4474 /* Sort name_components elements by name. */
4475 auto name_comp_compare = [&] (const name_component &left,
4476 const name_component &right)
4477 {
4478 const char *left_qualified = this->symbol_name_at (left.idx);
4479 const char *right_qualified = this->symbol_name_at (right.idx);
4480
4481 const char *left_name = left_qualified + left.name_offset;
4482 const char *right_name = right_qualified + right.name_offset;
4483
4484 return name_cmp (left_name, right_name) < 0;
4485 };
4486
4487 std::sort (this->name_components.begin (),
4488 this->name_components.end (),
4489 name_comp_compare);
4490}
4491
4492/* Helper for dw2_expand_symtabs_matching that works with a
44ed8f3e
PA
4493 mapped_index_base instead of the containing objfile. This is split
4494 to a separate function in order to be able to unit test the
4495 name_components matching using a mock mapped_index_base. For each
5c58de74 4496 symbol name that matches, calls MATCH_CALLBACK, passing it the
44ed8f3e 4497 symbol's index in the mapped_index_base symbol table. */
5c58de74
PA
4498
4499static void
4500dw2_expand_symtabs_matching_symbol
44ed8f3e 4501 (mapped_index_base &index,
5c58de74
PA
4502 const lookup_name_info &lookup_name_in,
4503 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4504 enum search_domain kind,
4505 gdb::function_view<void (offset_type)> match_callback)
4506{
4507 lookup_name_info lookup_name_without_params
4508 = lookup_name_in.make_ignore_params ();
4509 gdb_index_symbol_name_matcher lookup_name_matcher
4510 (lookup_name_without_params);
4511
4512 /* Build the symbol name component sorted vector, if we haven't
4513 yet. */
4514 index.build_name_components ();
4515
4516 auto bounds = index.find_name_components_bounds (lookup_name_without_params);
4517
3f563c84
PA
4518 /* Now for each symbol name in range, check to see if we have a name
4519 match, and if so, call the MATCH_CALLBACK callback. */
4520
4521 /* The same symbol may appear more than once in the range though.
4522 E.g., if we're looking for symbols that complete "w", and we have
4523 a symbol named "w1::w2", we'll find the two name components for
4524 that same symbol in the range. To be sure we only call the
4525 callback once per symbol, we first collect the symbol name
4526 indexes that matched in a temporary vector and ignore
4527 duplicates. */
4528 std::vector<offset_type> matches;
5c58de74 4529 matches.reserve (std::distance (bounds.first, bounds.second));
3f563c84 4530
5c58de74 4531 for (; bounds.first != bounds.second; ++bounds.first)
3f563c84 4532 {
5c58de74 4533 const char *qualified = index.symbol_name_at (bounds.first->idx);
3f563c84
PA
4534
4535 if (!lookup_name_matcher.matches (qualified)
4536 || (symbol_matcher != NULL && !symbol_matcher (qualified)))
9291a0cd
TT
4537 continue;
4538
5c58de74 4539 matches.push_back (bounds.first->idx);
3f563c84
PA
4540 }
4541
4542 std::sort (matches.begin (), matches.end ());
4543
4544 /* Finally call the callback, once per match. */
4545 ULONGEST prev = -1;
4546 for (offset_type idx : matches)
4547 {
4548 if (prev != idx)
4549 {
4550 match_callback (idx);
4551 prev = idx;
4552 }
4553 }
4554
4555 /* Above we use a type wider than idx's for 'prev', since 0 and
4556 (offset_type)-1 are both possible values. */
4557 static_assert (sizeof (prev) > sizeof (offset_type), "");
4558}
4559
c62446b1
PA
4560#if GDB_SELF_TEST
4561
4562namespace selftests { namespace dw2_expand_symtabs_matching {
4563
a3c5fafd
PA
4564/* A mock .gdb_index/.debug_names-like name index table, enough to
4565 exercise dw2_expand_symtabs_matching_symbol, which works with the
4566 mapped_index_base interface. Builds an index from the symbol list
4567 passed as parameter to the constructor. */
4568class mock_mapped_index : public mapped_index_base
c62446b1
PA
4569{
4570public:
a3c5fafd
PA
4571 mock_mapped_index (gdb::array_view<const char *> symbols)
4572 : m_symbol_table (symbols)
c62446b1
PA
4573 {}
4574
a3c5fafd 4575 DISABLE_COPY_AND_ASSIGN (mock_mapped_index);
c62446b1 4576
a3c5fafd 4577 /* Return the number of names in the symbol table. */
632e107b 4578 size_t symbol_name_count () const override
c62446b1 4579 {
a3c5fafd 4580 return m_symbol_table.size ();
c62446b1
PA
4581 }
4582
a3c5fafd 4583 /* Get the name of the symbol at IDX in the symbol table. */
632e107b 4584 const char *symbol_name_at (offset_type idx) const override
a3c5fafd
PA
4585 {
4586 return m_symbol_table[idx];
4587 }
c62446b1 4588
a3c5fafd
PA
4589private:
4590 gdb::array_view<const char *> m_symbol_table;
c62446b1
PA
4591};
4592
4593/* Convenience function that converts a NULL pointer to a "<null>"
4594 string, to pass to print routines. */
4595
4596static const char *
4597string_or_null (const char *str)
4598{
4599 return str != NULL ? str : "<null>";
4600}
4601
4602/* Check if a lookup_name_info built from
4603 NAME/MATCH_TYPE/COMPLETION_MODE matches the symbols in the mock
4604 index. EXPECTED_LIST is the list of expected matches, in expected
4605 matching order. If no match expected, then an empty list is
4606 specified. Returns true on success. On failure prints a warning
4607 indicating the file:line that failed, and returns false. */
4608
4609static bool
4610check_match (const char *file, int line,
4611 mock_mapped_index &mock_index,
4612 const char *name, symbol_name_match_type match_type,
4613 bool completion_mode,
4614 std::initializer_list<const char *> expected_list)
4615{
4616 lookup_name_info lookup_name (name, match_type, completion_mode);
4617
4618 bool matched = true;
4619
4620 auto mismatch = [&] (const char *expected_str,
4621 const char *got)
4622 {
4623 warning (_("%s:%d: match_type=%s, looking-for=\"%s\", "
4624 "expected=\"%s\", got=\"%s\"\n"),
4625 file, line,
4626 (match_type == symbol_name_match_type::FULL
4627 ? "FULL" : "WILD"),
4628 name, string_or_null (expected_str), string_or_null (got));
4629 matched = false;
4630 };
4631
4632 auto expected_it = expected_list.begin ();
4633 auto expected_end = expected_list.end ();
4634
a3c5fafd 4635 dw2_expand_symtabs_matching_symbol (mock_index, lookup_name,
c62446b1
PA
4636 NULL, ALL_DOMAIN,
4637 [&] (offset_type idx)
4638 {
a3c5fafd 4639 const char *matched_name = mock_index.symbol_name_at (idx);
c62446b1
PA
4640 const char *expected_str
4641 = expected_it == expected_end ? NULL : *expected_it++;
4642
4643 if (expected_str == NULL || strcmp (expected_str, matched_name) != 0)
4644 mismatch (expected_str, matched_name);
4645 });
4646
4647 const char *expected_str
4648 = expected_it == expected_end ? NULL : *expected_it++;
4649 if (expected_str != NULL)
4650 mismatch (expected_str, NULL);
4651
4652 return matched;
4653}
4654
4655/* The symbols added to the mock mapped_index for testing (in
4656 canonical form). */
4657static const char *test_symbols[] = {
4658 "function",
4659 "std::bar",
4660 "std::zfunction",
4661 "std::zfunction2",
4662 "w1::w2",
4663 "ns::foo<char*>",
4664 "ns::foo<int>",
4665 "ns::foo<long>",
a20714ff
PA
4666 "ns2::tmpl<int>::foo2",
4667 "(anonymous namespace)::A::B::C",
c62446b1 4668
e1ef7d7a
PA
4669 /* These are used to check that the increment-last-char in the
4670 matching algorithm for completion doesn't match "t1_fund" when
4671 completing "t1_func". */
4672 "t1_func",
4673 "t1_func1",
4674 "t1_fund",
4675 "t1_fund1",
4676
4677 /* A UTF-8 name with multi-byte sequences to make sure that
4678 cp-name-parser understands this as a single identifier ("função"
4679 is "function" in PT). */
4680 u8"u8função",
4681
4682 /* \377 (0xff) is Latin1 'ÿ'. */
4683 "yfunc\377",
4684
4685 /* \377 (0xff) is Latin1 'ÿ'. */
4686 "\377",
4687 "\377\377123",
4688
c62446b1
PA
4689 /* A name with all sorts of complications. Starts with "z" to make
4690 it easier for the completion tests below. */
4691#define Z_SYM_NAME \
4692 "z::std::tuple<(anonymous namespace)::ui*, std::bar<(anonymous namespace)::ui> >" \
4693 "::tuple<(anonymous namespace)::ui*, " \
4694 "std::default_delete<(anonymous namespace)::ui>, void>"
4695
4696 Z_SYM_NAME
4697};
4698
a3c5fafd
PA
4699/* Returns true if the mapped_index_base::find_name_component_bounds
4700 method finds EXPECTED_SYMS in INDEX when looking for SEARCH_NAME,
4701 in completion mode. */
5c58de74
PA
4702
4703static bool
a3c5fafd 4704check_find_bounds_finds (mapped_index_base &index,
5c58de74
PA
4705 const char *search_name,
4706 gdb::array_view<const char *> expected_syms)
4707{
4708 lookup_name_info lookup_name (search_name,
4709 symbol_name_match_type::FULL, true);
4710
4711 auto bounds = index.find_name_components_bounds (lookup_name);
4712
4713 size_t distance = std::distance (bounds.first, bounds.second);
4714 if (distance != expected_syms.size ())
4715 return false;
4716
4717 for (size_t exp_elem = 0; exp_elem < distance; exp_elem++)
4718 {
4719 auto nc_elem = bounds.first + exp_elem;
4720 const char *qualified = index.symbol_name_at (nc_elem->idx);
4721 if (strcmp (qualified, expected_syms[exp_elem]) != 0)
4722 return false;
4723 }
4724
4725 return true;
4726}
4727
4728/* Test the lower-level mapped_index::find_name_component_bounds
4729 method. */
4730
c62446b1 4731static void
5c58de74
PA
4732test_mapped_index_find_name_component_bounds ()
4733{
4734 mock_mapped_index mock_index (test_symbols);
4735
a3c5fafd 4736 mock_index.build_name_components ();
5c58de74
PA
4737
4738 /* Test the lower-level mapped_index::find_name_component_bounds
4739 method in completion mode. */
4740 {
4741 static const char *expected_syms[] = {
4742 "t1_func",
4743 "t1_func1",
5c58de74
PA
4744 };
4745
a3c5fafd 4746 SELF_CHECK (check_find_bounds_finds (mock_index,
5c58de74
PA
4747 "t1_func", expected_syms));
4748 }
4749
4750 /* Check that the increment-last-char in the name matching algorithm
4751 for completion doesn't get confused with Ansi1 'ÿ' / 0xff. */
4752 {
4753 static const char *expected_syms1[] = {
4754 "\377",
4755 "\377\377123",
4756 };
a3c5fafd 4757 SELF_CHECK (check_find_bounds_finds (mock_index,
5c58de74
PA
4758 "\377", expected_syms1));
4759
4760 static const char *expected_syms2[] = {
4761 "\377\377123",
4762 };
a3c5fafd 4763 SELF_CHECK (check_find_bounds_finds (mock_index,
5c58de74
PA
4764 "\377\377", expected_syms2));
4765 }
4766}
4767
4768/* Test dw2_expand_symtabs_matching_symbol. */
4769
4770static void
4771test_dw2_expand_symtabs_matching_symbol ()
c62446b1
PA
4772{
4773 mock_mapped_index mock_index (test_symbols);
4774
4775 /* We let all tests run until the end even if some fails, for debug
4776 convenience. */
4777 bool any_mismatch = false;
4778
4779 /* Create the expected symbols list (an initializer_list). Needed
4780 because lists have commas, and we need to pass them to CHECK,
4781 which is a macro. */
4782#define EXPECT(...) { __VA_ARGS__ }
4783
4784 /* Wrapper for check_match that passes down the current
4785 __FILE__/__LINE__. */
4786#define CHECK_MATCH(NAME, MATCH_TYPE, COMPLETION_MODE, EXPECTED_LIST) \
4787 any_mismatch |= !check_match (__FILE__, __LINE__, \
4788 mock_index, \
4789 NAME, MATCH_TYPE, COMPLETION_MODE, \
4790 EXPECTED_LIST)
4791
4792 /* Identity checks. */
4793 for (const char *sym : test_symbols)
4794 {
4795 /* Should be able to match all existing symbols. */
4796 CHECK_MATCH (sym, symbol_name_match_type::FULL, false,
4797 EXPECT (sym));
4798
4799 /* Should be able to match all existing symbols with
4800 parameters. */
4801 std::string with_params = std::string (sym) + "(int)";
4802 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4803 EXPECT (sym));
4804
4805 /* Should be able to match all existing symbols with
4806 parameters and qualifiers. */
4807 with_params = std::string (sym) + " ( int ) const";
4808 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4809 EXPECT (sym));
4810
4811 /* This should really find sym, but cp-name-parser.y doesn't
4812 know about lvalue/rvalue qualifiers yet. */
4813 with_params = std::string (sym) + " ( int ) &&";
4814 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4815 {});
4816 }
4817
e1ef7d7a
PA
4818 /* Check that the name matching algorithm for completion doesn't get
4819 confused with Latin1 'ÿ' / 0xff. */
4820 {
4821 static const char str[] = "\377";
4822 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4823 EXPECT ("\377", "\377\377123"));
4824 }
4825
4826 /* Check that the increment-last-char in the matching algorithm for
4827 completion doesn't match "t1_fund" when completing "t1_func". */
4828 {
4829 static const char str[] = "t1_func";
4830 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4831 EXPECT ("t1_func", "t1_func1"));
4832 }
4833
c62446b1
PA
4834 /* Check that completion mode works at each prefix of the expected
4835 symbol name. */
4836 {
4837 static const char str[] = "function(int)";
4838 size_t len = strlen (str);
4839 std::string lookup;
4840
4841 for (size_t i = 1; i < len; i++)
4842 {
4843 lookup.assign (str, i);
4844 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4845 EXPECT ("function"));
4846 }
4847 }
4848
4849 /* While "w" is a prefix of both components, the match function
4850 should still only be called once. */
4851 {
4852 CHECK_MATCH ("w", symbol_name_match_type::FULL, true,
4853 EXPECT ("w1::w2"));
a20714ff
PA
4854 CHECK_MATCH ("w", symbol_name_match_type::WILD, true,
4855 EXPECT ("w1::w2"));
c62446b1
PA
4856 }
4857
4858 /* Same, with a "complicated" symbol. */
4859 {
4860 static const char str[] = Z_SYM_NAME;
4861 size_t len = strlen (str);
4862 std::string lookup;
4863
4864 for (size_t i = 1; i < len; i++)
4865 {
4866 lookup.assign (str, i);
4867 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4868 EXPECT (Z_SYM_NAME));
4869 }
4870 }
4871
4872 /* In FULL mode, an incomplete symbol doesn't match. */
4873 {
4874 CHECK_MATCH ("std::zfunction(int", symbol_name_match_type::FULL, false,
4875 {});
4876 }
4877
4878 /* A complete symbol with parameters matches any overload, since the
4879 index has no overload info. */
4880 {
4881 CHECK_MATCH ("std::zfunction(int)", symbol_name_match_type::FULL, true,
4882 EXPECT ("std::zfunction", "std::zfunction2"));
a20714ff
PA
4883 CHECK_MATCH ("zfunction(int)", symbol_name_match_type::WILD, true,
4884 EXPECT ("std::zfunction", "std::zfunction2"));
4885 CHECK_MATCH ("zfunc", symbol_name_match_type::WILD, true,
4886 EXPECT ("std::zfunction", "std::zfunction2"));
c62446b1
PA
4887 }
4888
4889 /* Check that whitespace is ignored appropriately. A symbol with a
4890 template argument list. */
4891 {
4892 static const char expected[] = "ns::foo<int>";
4893 CHECK_MATCH ("ns :: foo < int > ", symbol_name_match_type::FULL, false,
4894 EXPECT (expected));
a20714ff
PA
4895 CHECK_MATCH ("foo < int > ", symbol_name_match_type::WILD, false,
4896 EXPECT (expected));
c62446b1
PA
4897 }
4898
4899 /* Check that whitespace is ignored appropriately. A symbol with a
4900 template argument list that includes a pointer. */
4901 {
4902 static const char expected[] = "ns::foo<char*>";
4903 /* Try both completion and non-completion modes. */
4904 static const bool completion_mode[2] = {false, true};
4905 for (size_t i = 0; i < 2; i++)
4906 {
4907 CHECK_MATCH ("ns :: foo < char * >", symbol_name_match_type::FULL,
4908 completion_mode[i], EXPECT (expected));
a20714ff
PA
4909 CHECK_MATCH ("foo < char * >", symbol_name_match_type::WILD,
4910 completion_mode[i], EXPECT (expected));
c62446b1
PA
4911
4912 CHECK_MATCH ("ns :: foo < char * > (int)", symbol_name_match_type::FULL,
4913 completion_mode[i], EXPECT (expected));
a20714ff
PA
4914 CHECK_MATCH ("foo < char * > (int)", symbol_name_match_type::WILD,
4915 completion_mode[i], EXPECT (expected));
c62446b1
PA
4916 }
4917 }
4918
4919 {
4920 /* Check method qualifiers are ignored. */
4921 static const char expected[] = "ns::foo<char*>";
4922 CHECK_MATCH ("ns :: foo < char * > ( int ) const",
4923 symbol_name_match_type::FULL, true, EXPECT (expected));
4924 CHECK_MATCH ("ns :: foo < char * > ( int ) &&",
4925 symbol_name_match_type::FULL, true, EXPECT (expected));
a20714ff
PA
4926 CHECK_MATCH ("foo < char * > ( int ) const",
4927 symbol_name_match_type::WILD, true, EXPECT (expected));
4928 CHECK_MATCH ("foo < char * > ( int ) &&",
4929 symbol_name_match_type::WILD, true, EXPECT (expected));
c62446b1
PA
4930 }
4931
4932 /* Test lookup names that don't match anything. */
4933 {
a20714ff
PA
4934 CHECK_MATCH ("bar2", symbol_name_match_type::WILD, false,
4935 {});
4936
c62446b1
PA
4937 CHECK_MATCH ("doesntexist", symbol_name_match_type::FULL, false,
4938 {});
4939 }
4940
a20714ff
PA
4941 /* Some wild matching tests, exercising "(anonymous namespace)",
4942 which should not be confused with a parameter list. */
4943 {
4944 static const char *syms[] = {
4945 "A::B::C",
4946 "B::C",
4947 "C",
4948 "A :: B :: C ( int )",
4949 "B :: C ( int )",
4950 "C ( int )",
4951 };
4952
4953 for (const char *s : syms)
4954 {
4955 CHECK_MATCH (s, symbol_name_match_type::WILD, false,
4956 EXPECT ("(anonymous namespace)::A::B::C"));
4957 }
4958 }
4959
4960 {
4961 static const char expected[] = "ns2::tmpl<int>::foo2";
4962 CHECK_MATCH ("tmp", symbol_name_match_type::WILD, true,
4963 EXPECT (expected));
4964 CHECK_MATCH ("tmpl<", symbol_name_match_type::WILD, true,
4965 EXPECT (expected));
4966 }
4967
c62446b1
PA
4968 SELF_CHECK (!any_mismatch);
4969
4970#undef EXPECT
4971#undef CHECK_MATCH
4972}
4973
5c58de74
PA
4974static void
4975run_test ()
4976{
4977 test_mapped_index_find_name_component_bounds ();
4978 test_dw2_expand_symtabs_matching_symbol ();
4979}
4980
c62446b1
PA
4981}} // namespace selftests::dw2_expand_symtabs_matching
4982
4983#endif /* GDB_SELF_TEST */
4984
4b514bc8
JK
4985/* If FILE_MATCHER is NULL or if PER_CU has
4986 dwarf2_per_cu_quick_data::MARK set (see
4987 dw_expand_symtabs_matching_file_matcher), expand the CU and call
4988 EXPANSION_NOTIFY on it. */
4989
4990static void
4991dw2_expand_symtabs_matching_one
4992 (struct dwarf2_per_cu_data *per_cu,
4993 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4994 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify)
4995{
4996 if (file_matcher == NULL || per_cu->v.quick->mark)
4997 {
4998 bool symtab_was_null
4999 = (per_cu->v.quick->compunit_symtab == NULL);
5000
58f0c718 5001 dw2_instantiate_symtab (per_cu, false);
4b514bc8
JK
5002
5003 if (expansion_notify != NULL
5004 && symtab_was_null
5005 && per_cu->v.quick->compunit_symtab != NULL)
5006 expansion_notify (per_cu->v.quick->compunit_symtab);
5007 }
5008}
5009
3f563c84
PA
5010/* Helper for dw2_expand_matching symtabs. Called on each symbol
5011 matched, to expand corresponding CUs that were marked. IDX is the
5012 index of the symbol name that matched. */
5013
5014static void
5015dw2_expand_marked_cus
ed2dc618 5016 (struct dwarf2_per_objfile *dwarf2_per_objfile, offset_type idx,
3f563c84
PA
5017 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
5018 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
5019 search_domain kind)
5020{
3f563c84
PA
5021 offset_type *vec, vec_len, vec_idx;
5022 bool global_seen = false;
ed2dc618 5023 mapped_index &index = *dwarf2_per_objfile->index_table;
3f563c84 5024
61920122 5025 vec = (offset_type *) (index.constant_pool
f00a2de2 5026 + MAYBE_SWAP (index.symbol_table[idx].vec));
61920122
PA
5027 vec_len = MAYBE_SWAP (vec[0]);
5028 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
5029 {
61920122
PA
5030 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[vec_idx + 1]);
5031 /* This value is only valid for index versions >= 7. */
5032 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
5033 gdb_index_symbol_kind symbol_kind =
5034 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
5035 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
5036 /* Only check the symbol attributes if they're present.
5037 Indices prior to version 7 don't record them,
5038 and indices >= 7 may elide them for certain symbols
5039 (gold does this). */
5040 int attrs_valid =
5041 (index.version >= 7
5042 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
5043
5044 /* Work around gold/15646. */
5045 if (attrs_valid)
9291a0cd 5046 {
61920122
PA
5047 if (!is_static && global_seen)
5048 continue;
5049 if (!is_static)
5050 global_seen = true;
5051 }
3190f0c6 5052
61920122
PA
5053 /* Only check the symbol's kind if it has one. */
5054 if (attrs_valid)
5055 {
5056 switch (kind)
8943b874 5057 {
61920122
PA
5058 case VARIABLES_DOMAIN:
5059 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
5060 continue;
5061 break;
5062 case FUNCTIONS_DOMAIN:
5063 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
8943b874 5064 continue;
61920122
PA
5065 break;
5066 case TYPES_DOMAIN:
5067 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
5068 continue;
5069 break;
5070 default:
5071 break;
8943b874 5072 }
61920122 5073 }
8943b874 5074
61920122 5075 /* Don't crash on bad data. */
b76e467d 5076 if (cu_index >= (dwarf2_per_objfile->all_comp_units.size ()
b2bdb8cf 5077 + dwarf2_per_objfile->all_type_units.size ()))
61920122 5078 {
b98664d3 5079 complaint (_(".gdb_index entry has bad CU index"
ed2dc618
SM
5080 " [in module %s]"),
5081 objfile_name (dwarf2_per_objfile->objfile));
61920122
PA
5082 continue;
5083 }
5084
ff4c9fec 5085 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (cu_index);
4b514bc8
JK
5086 dw2_expand_symtabs_matching_one (per_cu, file_matcher,
5087 expansion_notify);
61920122
PA
5088 }
5089}
5090
4b514bc8
JK
5091/* If FILE_MATCHER is non-NULL, set all the
5092 dwarf2_per_cu_quick_data::MARK of the current DWARF2_PER_OBJFILE
5093 that match FILE_MATCHER. */
5094
61920122 5095static void
4b514bc8 5096dw_expand_symtabs_matching_file_matcher
ed2dc618
SM
5097 (struct dwarf2_per_objfile *dwarf2_per_objfile,
5098 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher)
61920122 5099{
4b514bc8 5100 if (file_matcher == NULL)
61920122
PA
5101 return;
5102
4b514bc8
JK
5103 objfile *const objfile = dwarf2_per_objfile->objfile;
5104
5105 htab_up visited_found (htab_create_alloc (10, htab_hash_pointer,
5106 htab_eq_pointer,
5107 NULL, xcalloc, xfree));
5108 htab_up visited_not_found (htab_create_alloc (10, htab_hash_pointer,
61920122
PA
5109 htab_eq_pointer,
5110 NULL, xcalloc, xfree));
61920122 5111
4b514bc8
JK
5112 /* The rule is CUs specify all the files, including those used by
5113 any TU, so there's no need to scan TUs here. */
61920122 5114
b76e467d 5115 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
927aa2e7 5116 {
927aa2e7
JK
5117 QUIT;
5118
5119 per_cu->v.quick->mark = 0;
5120
5121 /* We only need to look at symtabs not already expanded. */
5122 if (per_cu->v.quick->compunit_symtab)
5123 continue;
5124
b76e467d 5125 quick_file_names *file_data = dw2_get_file_names (per_cu);
927aa2e7
JK
5126 if (file_data == NULL)
5127 continue;
5128
5129 if (htab_find (visited_not_found.get (), file_data) != NULL)
5130 continue;
5131 else if (htab_find (visited_found.get (), file_data) != NULL)
5132 {
5133 per_cu->v.quick->mark = 1;
5134 continue;
5135 }
5136
b76e467d 5137 for (int j = 0; j < file_data->num_file_names; ++j)
927aa2e7
JK
5138 {
5139 const char *this_real_name;
5140
5141 if (file_matcher (file_data->file_names[j], false))
5142 {
5143 per_cu->v.quick->mark = 1;
5144 break;
5145 }
5146
5147 /* Before we invoke realpath, which can get expensive when many
5148 files are involved, do a quick comparison of the basenames. */
5149 if (!basenames_may_differ
5150 && !file_matcher (lbasename (file_data->file_names[j]),
5151 true))
5152 continue;
5153
5154 this_real_name = dw2_get_real_path (objfile, file_data, j);
5155 if (file_matcher (this_real_name, false))
5156 {
5157 per_cu->v.quick->mark = 1;
5158 break;
5159 }
5160 }
5161
b76e467d
SM
5162 void **slot = htab_find_slot (per_cu->v.quick->mark
5163 ? visited_found.get ()
5164 : visited_not_found.get (),
5165 file_data, INSERT);
927aa2e7
JK
5166 *slot = file_data;
5167 }
5168}
5169
5170static void
5171dw2_expand_symtabs_matching
5172 (struct objfile *objfile,
5173 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
5174 const lookup_name_info &lookup_name,
5175 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
5176 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
5177 enum search_domain kind)
5178{
ed2dc618
SM
5179 struct dwarf2_per_objfile *dwarf2_per_objfile
5180 = get_dwarf2_per_objfile (objfile);
927aa2e7
JK
5181
5182 /* index_table is NULL if OBJF_READNOW. */
5183 if (!dwarf2_per_objfile->index_table)
5184 return;
5185
ed2dc618 5186 dw_expand_symtabs_matching_file_matcher (dwarf2_per_objfile, file_matcher);
927aa2e7
JK
5187
5188 mapped_index &index = *dwarf2_per_objfile->index_table;
5189
5190 dw2_expand_symtabs_matching_symbol (index, lookup_name,
5191 symbol_matcher,
5192 kind, [&] (offset_type idx)
5193 {
ed2dc618 5194 dw2_expand_marked_cus (dwarf2_per_objfile, idx, file_matcher,
927aa2e7
JK
5195 expansion_notify, kind);
5196 });
5197}
5198
5199/* A helper for dw2_find_pc_sect_compunit_symtab which finds the most specific
5200 symtab. */
5201
5202static struct compunit_symtab *
5203recursively_find_pc_sect_compunit_symtab (struct compunit_symtab *cust,
5204 CORE_ADDR pc)
5205{
5206 int i;
5207
5208 if (COMPUNIT_BLOCKVECTOR (cust) != NULL
5209 && blockvector_contains_pc (COMPUNIT_BLOCKVECTOR (cust), pc))
5210 return cust;
5211
5212 if (cust->includes == NULL)
5213 return NULL;
5214
5215 for (i = 0; cust->includes[i]; ++i)
5216 {
5217 struct compunit_symtab *s = cust->includes[i];
5218
5219 s = recursively_find_pc_sect_compunit_symtab (s, pc);
5220 if (s != NULL)
5221 return s;
5222 }
5223
5224 return NULL;
5225}
5226
5227static struct compunit_symtab *
5228dw2_find_pc_sect_compunit_symtab (struct objfile *objfile,
5229 struct bound_minimal_symbol msymbol,
5230 CORE_ADDR pc,
5231 struct obj_section *section,
5232 int warn_if_readin)
5233{
5234 struct dwarf2_per_cu_data *data;
5235 struct compunit_symtab *result;
5236
927aa2e7
JK
5237 if (!objfile->psymtabs_addrmap)
5238 return NULL;
5239
5240 data = (struct dwarf2_per_cu_data *) addrmap_find (objfile->psymtabs_addrmap,
5241 pc);
5242 if (!data)
5243 return NULL;
5244
5245 if (warn_if_readin && data->v.quick->compunit_symtab)
5246 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
5247 paddress (get_objfile_arch (objfile), pc));
5248
5249 result
58f0c718
TT
5250 = recursively_find_pc_sect_compunit_symtab (dw2_instantiate_symtab (data,
5251 false),
927aa2e7
JK
5252 pc);
5253 gdb_assert (result != NULL);
5254 return result;
5255}
5256
5257static void
5258dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
5259 void *data, int need_fullname)
5260{
ed2dc618
SM
5261 struct dwarf2_per_objfile *dwarf2_per_objfile
5262 = get_dwarf2_per_objfile (objfile);
927aa2e7
JK
5263
5264 if (!dwarf2_per_objfile->filenames_cache)
5265 {
5266 dwarf2_per_objfile->filenames_cache.emplace ();
5267
5268 htab_up visited (htab_create_alloc (10,
5269 htab_hash_pointer, htab_eq_pointer,
5270 NULL, xcalloc, xfree));
5271
5272 /* The rule is CUs specify all the files, including those used
5273 by any TU, so there's no need to scan TUs here. We can
5274 ignore file names coming from already-expanded CUs. */
5275
b76e467d 5276 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
927aa2e7 5277 {
927aa2e7
JK
5278 if (per_cu->v.quick->compunit_symtab)
5279 {
5280 void **slot = htab_find_slot (visited.get (),
5281 per_cu->v.quick->file_names,
5282 INSERT);
5283
5284 *slot = per_cu->v.quick->file_names;
5285 }
5286 }
5287
b76e467d 5288 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
927aa2e7 5289 {
927aa2e7
JK
5290 /* We only need to look at symtabs not already expanded. */
5291 if (per_cu->v.quick->compunit_symtab)
5292 continue;
5293
b76e467d 5294 quick_file_names *file_data = dw2_get_file_names (per_cu);
927aa2e7
JK
5295 if (file_data == NULL)
5296 continue;
5297
b76e467d 5298 void **slot = htab_find_slot (visited.get (), file_data, INSERT);
927aa2e7
JK
5299 if (*slot)
5300 {
5301 /* Already visited. */
5302 continue;
5303 }
5304 *slot = file_data;
5305
5306 for (int j = 0; j < file_data->num_file_names; ++j)
5307 {
5308 const char *filename = file_data->file_names[j];
5309 dwarf2_per_objfile->filenames_cache->seen (filename);
5310 }
5311 }
5312 }
5313
5314 dwarf2_per_objfile->filenames_cache->traverse ([&] (const char *filename)
5315 {
5316 gdb::unique_xmalloc_ptr<char> this_real_name;
5317
5318 if (need_fullname)
5319 this_real_name = gdb_realpath (filename);
5320 (*fun) (filename, this_real_name.get (), data);
5321 });
5322}
5323
5324static int
5325dw2_has_symbols (struct objfile *objfile)
5326{
5327 return 1;
5328}
5329
5330const struct quick_symbol_functions dwarf2_gdb_index_functions =
5331{
5332 dw2_has_symbols,
5333 dw2_find_last_source_symtab,
5334 dw2_forget_cached_source_info,
5335 dw2_map_symtabs_matching_filename,
5336 dw2_lookup_symbol,
5337 dw2_print_stats,
5338 dw2_dump,
5339 dw2_relocate,
5340 dw2_expand_symtabs_for_function,
5341 dw2_expand_all_symtabs,
5342 dw2_expand_symtabs_with_fullname,
5343 dw2_map_matching_symbols,
5344 dw2_expand_symtabs_matching,
5345 dw2_find_pc_sect_compunit_symtab,
5346 NULL,
5347 dw2_map_symbol_filenames
5348};
5349
5350/* DWARF-5 debug_names reader. */
5351
5352/* DWARF-5 augmentation string for GDB's DW_IDX_GNU_* extension. */
5353static const gdb_byte dwarf5_augmentation[] = { 'G', 'D', 'B', 0 };
5354
5355/* A helper function that reads the .debug_names section in SECTION
5356 and fills in MAP. FILENAME is the name of the file containing the
5357 section; it is used for error reporting.
5358
5359 Returns true if all went well, false otherwise. */
5360
5361static bool
5362read_debug_names_from_section (struct objfile *objfile,
5363 const char *filename,
5364 struct dwarf2_section_info *section,
5365 mapped_debug_names &map)
5366{
5367 if (dwarf2_section_empty_p (section))
5368 return false;
5369
5370 /* Older elfutils strip versions could keep the section in the main
5371 executable while splitting it for the separate debug info file. */
5372 if ((get_section_flags (section) & SEC_HAS_CONTENTS) == 0)
5373 return false;
5374
5375 dwarf2_read_section (objfile, section);
5376
5377 map.dwarf5_byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
5378
5379 const gdb_byte *addr = section->buffer;
5380
5381 bfd *const abfd = get_section_bfd_owner (section);
5382
5383 unsigned int bytes_read;
5384 LONGEST length = read_initial_length (abfd, addr, &bytes_read);
5385 addr += bytes_read;
5386
5387 map.dwarf5_is_dwarf64 = bytes_read != 4;
5388 map.offset_size = map.dwarf5_is_dwarf64 ? 8 : 4;
5389 if (bytes_read + length != section->size)
5390 {
5391 /* There may be multiple per-CU indices. */
5392 warning (_("Section .debug_names in %s length %s does not match "
5393 "section length %s, ignoring .debug_names."),
5394 filename, plongest (bytes_read + length),
5395 pulongest (section->size));
5396 return false;
5397 }
5398
5399 /* The version number. */
5400 uint16_t version = read_2_bytes (abfd, addr);
5401 addr += 2;
5402 if (version != 5)
5403 {
5404 warning (_("Section .debug_names in %s has unsupported version %d, "
5405 "ignoring .debug_names."),
5406 filename, version);
5407 return false;
5408 }
5409
5410 /* Padding. */
5411 uint16_t padding = read_2_bytes (abfd, addr);
5412 addr += 2;
5413 if (padding != 0)
5414 {
5415 warning (_("Section .debug_names in %s has unsupported padding %d, "
5416 "ignoring .debug_names."),
5417 filename, padding);
5418 return false;
5419 }
5420
5421 /* comp_unit_count - The number of CUs in the CU list. */
5422 map.cu_count = read_4_bytes (abfd, addr);
5423 addr += 4;
5424
5425 /* local_type_unit_count - The number of TUs in the local TU
5426 list. */
5427 map.tu_count = read_4_bytes (abfd, addr);
5428 addr += 4;
5429
5430 /* foreign_type_unit_count - The number of TUs in the foreign TU
5431 list. */
5432 uint32_t foreign_tu_count = read_4_bytes (abfd, addr);
5433 addr += 4;
5434 if (foreign_tu_count != 0)
5435 {
5436 warning (_("Section .debug_names in %s has unsupported %lu foreign TUs, "
5437 "ignoring .debug_names."),
5438 filename, static_cast<unsigned long> (foreign_tu_count));
5439 return false;
5440 }
5441
5442 /* bucket_count - The number of hash buckets in the hash lookup
5443 table. */
5444 map.bucket_count = read_4_bytes (abfd, addr);
5445 addr += 4;
5446
5447 /* name_count - The number of unique names in the index. */
5448 map.name_count = read_4_bytes (abfd, addr);
5449 addr += 4;
5450
5451 /* abbrev_table_size - The size in bytes of the abbreviations
5452 table. */
5453 uint32_t abbrev_table_size = read_4_bytes (abfd, addr);
5454 addr += 4;
5455
5456 /* augmentation_string_size - The size in bytes of the augmentation
5457 string. This value is rounded up to a multiple of 4. */
5458 uint32_t augmentation_string_size = read_4_bytes (abfd, addr);
5459 addr += 4;
5460 map.augmentation_is_gdb = ((augmentation_string_size
5461 == sizeof (dwarf5_augmentation))
5462 && memcmp (addr, dwarf5_augmentation,
5463 sizeof (dwarf5_augmentation)) == 0);
5464 augmentation_string_size += (-augmentation_string_size) & 3;
5465 addr += augmentation_string_size;
5466
5467 /* List of CUs */
5468 map.cu_table_reordered = addr;
5469 addr += map.cu_count * map.offset_size;
5470
5471 /* List of Local TUs */
5472 map.tu_table_reordered = addr;
5473 addr += map.tu_count * map.offset_size;
5474
5475 /* Hash Lookup Table */
5476 map.bucket_table_reordered = reinterpret_cast<const uint32_t *> (addr);
5477 addr += map.bucket_count * 4;
5478 map.hash_table_reordered = reinterpret_cast<const uint32_t *> (addr);
5479 addr += map.name_count * 4;
5480
5481 /* Name Table */
5482 map.name_table_string_offs_reordered = addr;
5483 addr += map.name_count * map.offset_size;
5484 map.name_table_entry_offs_reordered = addr;
5485 addr += map.name_count * map.offset_size;
5486
5487 const gdb_byte *abbrev_table_start = addr;
5488 for (;;)
5489 {
5490 unsigned int bytes_read;
5491 const ULONGEST index_num = read_unsigned_leb128 (abfd, addr, &bytes_read);
5492 addr += bytes_read;
5493 if (index_num == 0)
5494 break;
5495
5496 const auto insertpair
5497 = map.abbrev_map.emplace (index_num, mapped_debug_names::index_val ());
5498 if (!insertpair.second)
5499 {
5500 warning (_("Section .debug_names in %s has duplicate index %s, "
5501 "ignoring .debug_names."),
5502 filename, pulongest (index_num));
5503 return false;
5504 }
5505 mapped_debug_names::index_val &indexval = insertpair.first->second;
5506 indexval.dwarf_tag = read_unsigned_leb128 (abfd, addr, &bytes_read);
5507 addr += bytes_read;
5508
5509 for (;;)
5510 {
5511 mapped_debug_names::index_val::attr attr;
5512 attr.dw_idx = read_unsigned_leb128 (abfd, addr, &bytes_read);
5513 addr += bytes_read;
5514 attr.form = read_unsigned_leb128 (abfd, addr, &bytes_read);
5515 addr += bytes_read;
5516 if (attr.form == DW_FORM_implicit_const)
5517 {
5518 attr.implicit_const = read_signed_leb128 (abfd, addr,
5519 &bytes_read);
5520 addr += bytes_read;
5521 }
5522 if (attr.dw_idx == 0 && attr.form == 0)
5523 break;
5524 indexval.attr_vec.push_back (std::move (attr));
5525 }
5526 }
5527 if (addr != abbrev_table_start + abbrev_table_size)
5528 {
5529 warning (_("Section .debug_names in %s has abbreviation_table "
5530 "of size %zu vs. written as %u, ignoring .debug_names."),
5531 filename, addr - abbrev_table_start, abbrev_table_size);
5532 return false;
5533 }
5534 map.entry_pool = addr;
5535
5536 return true;
5537}
5538
5539/* A helper for create_cus_from_debug_names that handles the MAP's CU
5540 list. */
5541
5542static void
ed2dc618 5543create_cus_from_debug_names_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
927aa2e7
JK
5544 const mapped_debug_names &map,
5545 dwarf2_section_info &section,
b76e467d 5546 bool is_dwz)
927aa2e7
JK
5547{
5548 sect_offset sect_off_prev;
5549 for (uint32_t i = 0; i <= map.cu_count; ++i)
5550 {
5551 sect_offset sect_off_next;
5552 if (i < map.cu_count)
5553 {
5554 sect_off_next
5555 = (sect_offset) (extract_unsigned_integer
5556 (map.cu_table_reordered + i * map.offset_size,
5557 map.offset_size,
5558 map.dwarf5_byte_order));
5559 }
5560 else
5561 sect_off_next = (sect_offset) section.size;
5562 if (i >= 1)
5563 {
5564 const ULONGEST length = sect_off_next - sect_off_prev;
b76e467d 5565 dwarf2_per_cu_data *per_cu
ed2dc618 5566 = create_cu_from_index_list (dwarf2_per_objfile, &section, is_dwz,
927aa2e7 5567 sect_off_prev, length);
b76e467d 5568 dwarf2_per_objfile->all_comp_units.push_back (per_cu);
927aa2e7
JK
5569 }
5570 sect_off_prev = sect_off_next;
5571 }
5572}
5573
5574/* Read the CU list from the mapped index, and use it to create all
ed2dc618 5575 the CU objects for this dwarf2_per_objfile. */
927aa2e7
JK
5576
5577static void
ed2dc618 5578create_cus_from_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile,
927aa2e7
JK
5579 const mapped_debug_names &map,
5580 const mapped_debug_names &dwz_map)
5581{
b76e467d
SM
5582 gdb_assert (dwarf2_per_objfile->all_comp_units.empty ());
5583 dwarf2_per_objfile->all_comp_units.reserve (map.cu_count + dwz_map.cu_count);
927aa2e7 5584
ed2dc618
SM
5585 create_cus_from_debug_names_list (dwarf2_per_objfile, map,
5586 dwarf2_per_objfile->info,
b76e467d 5587 false /* is_dwz */);
927aa2e7
JK
5588
5589 if (dwz_map.cu_count == 0)
5590 return;
5591
ed2dc618
SM
5592 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
5593 create_cus_from_debug_names_list (dwarf2_per_objfile, dwz_map, dwz->info,
b76e467d 5594 true /* is_dwz */);
927aa2e7
JK
5595}
5596
5597/* Read .debug_names. If everything went ok, initialize the "quick"
5598 elements of all the CUs and return true. Otherwise, return false. */
5599
5600static bool
ed2dc618 5601dwarf2_read_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile)
927aa2e7 5602{
22ca247e
TT
5603 std::unique_ptr<mapped_debug_names> map
5604 (new mapped_debug_names (dwarf2_per_objfile));
ed2dc618
SM
5605 mapped_debug_names dwz_map (dwarf2_per_objfile);
5606 struct objfile *objfile = dwarf2_per_objfile->objfile;
927aa2e7
JK
5607
5608 if (!read_debug_names_from_section (objfile, objfile_name (objfile),
5609 &dwarf2_per_objfile->debug_names,
22ca247e 5610 *map))
927aa2e7
JK
5611 return false;
5612
5613 /* Don't use the index if it's empty. */
22ca247e 5614 if (map->name_count == 0)
927aa2e7
JK
5615 return false;
5616
5617 /* If there is a .dwz file, read it so we can get its CU list as
5618 well. */
ed2dc618 5619 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
927aa2e7
JK
5620 if (dwz != NULL)
5621 {
5622 if (!read_debug_names_from_section (objfile,
5623 bfd_get_filename (dwz->dwz_bfd),
5624 &dwz->debug_names, dwz_map))
5625 {
5626 warning (_("could not read '.debug_names' section from %s; skipping"),
5627 bfd_get_filename (dwz->dwz_bfd));
5628 return false;
5629 }
5630 }
5631
22ca247e 5632 create_cus_from_debug_names (dwarf2_per_objfile, *map, dwz_map);
927aa2e7 5633
22ca247e 5634 if (map->tu_count != 0)
927aa2e7
JK
5635 {
5636 /* We can only handle a single .debug_types when we have an
5637 index. */
5638 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) != 1)
5639 return false;
5640
5641 dwarf2_section_info *section = VEC_index (dwarf2_section_info_def,
5642 dwarf2_per_objfile->types, 0);
5643
5644 create_signatured_type_table_from_debug_names
22ca247e 5645 (dwarf2_per_objfile, *map, section, &dwarf2_per_objfile->abbrev);
927aa2e7
JK
5646 }
5647
ed2dc618
SM
5648 create_addrmap_from_aranges (dwarf2_per_objfile,
5649 &dwarf2_per_objfile->debug_aranges);
927aa2e7 5650
22ca247e 5651 dwarf2_per_objfile->debug_names_table = std::move (map);
927aa2e7
JK
5652 dwarf2_per_objfile->using_index = 1;
5653 dwarf2_per_objfile->quick_file_names_table =
b76e467d 5654 create_quick_file_names_table (dwarf2_per_objfile->all_comp_units.size ());
927aa2e7
JK
5655
5656 return true;
5657}
5658
927aa2e7
JK
5659/* Type used to manage iterating over all CUs looking for a symbol for
5660 .debug_names. */
5661
5662class dw2_debug_names_iterator
5663{
5664public:
5665 /* If WANT_SPECIFIC_BLOCK is true, only look for symbols in block
5666 BLOCK_INDEX. Otherwise BLOCK_INDEX is ignored. */
5667 dw2_debug_names_iterator (const mapped_debug_names &map,
5668 bool want_specific_block,
5669 block_enum block_index, domain_enum domain,
5670 const char *name)
5671 : m_map (map), m_want_specific_block (want_specific_block),
5672 m_block_index (block_index), m_domain (domain),
5673 m_addr (find_vec_in_debug_names (map, name))
5674 {}
5675
5676 dw2_debug_names_iterator (const mapped_debug_names &map,
5677 search_domain search, uint32_t namei)
5678 : m_map (map),
5679 m_search (search),
5680 m_addr (find_vec_in_debug_names (map, namei))
5681 {}
5682
5683 /* Return the next matching CU or NULL if there are no more. */
5684 dwarf2_per_cu_data *next ();
5685
5686private:
5687 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5688 const char *name);
5689 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5690 uint32_t namei);
5691
5692 /* The internalized form of .debug_names. */
5693 const mapped_debug_names &m_map;
5694
5695 /* If true, only look for symbols that match BLOCK_INDEX. */
5696 const bool m_want_specific_block = false;
5697
5698 /* One of GLOBAL_BLOCK or STATIC_BLOCK.
5699 Unused if !WANT_SPECIFIC_BLOCK - FIRST_LOCAL_BLOCK is an invalid
5700 value. */
5701 const block_enum m_block_index = FIRST_LOCAL_BLOCK;
5702
5703 /* The kind of symbol we're looking for. */
5704 const domain_enum m_domain = UNDEF_DOMAIN;
5705 const search_domain m_search = ALL_DOMAIN;
5706
5707 /* The list of CUs from the index entry of the symbol, or NULL if
5708 not found. */
5709 const gdb_byte *m_addr;
5710};
5711
5712const char *
5713mapped_debug_names::namei_to_name (uint32_t namei) const
5714{
5715 const ULONGEST namei_string_offs
5716 = extract_unsigned_integer ((name_table_string_offs_reordered
5717 + namei * offset_size),
5718 offset_size,
5719 dwarf5_byte_order);
5720 return read_indirect_string_at_offset
ed2dc618 5721 (dwarf2_per_objfile, dwarf2_per_objfile->objfile->obfd, namei_string_offs);
927aa2e7
JK
5722}
5723
5724/* Find a slot in .debug_names for the object named NAME. If NAME is
5725 found, return pointer to its pool data. If NAME cannot be found,
5726 return NULL. */
5727
5728const gdb_byte *
5729dw2_debug_names_iterator::find_vec_in_debug_names
5730 (const mapped_debug_names &map, const char *name)
5731{
5732 int (*cmp) (const char *, const char *);
5733
5734 if (current_language->la_language == language_cplus
5735 || current_language->la_language == language_fortran
5736 || current_language->la_language == language_d)
5737 {
5738 /* NAME is already canonical. Drop any qualifiers as
5739 .debug_names does not contain any. */
5740
5741 if (strchr (name, '(') != NULL)
5742 {
5743 gdb::unique_xmalloc_ptr<char> without_params
5744 = cp_remove_params (name);
5745
5746 if (without_params != NULL)
5747 {
5748 name = without_params.get();
5749 }
5750 }
5751 }
5752
5753 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
5754
5755 const uint32_t full_hash = dwarf5_djb_hash (name);
5756 uint32_t namei
5757 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5758 (map.bucket_table_reordered
5759 + (full_hash % map.bucket_count)), 4,
5760 map.dwarf5_byte_order);
5761 if (namei == 0)
5762 return NULL;
5763 --namei;
5764 if (namei >= map.name_count)
5765 {
b98664d3 5766 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
927aa2e7
JK
5767 "[in module %s]"),
5768 namei, map.name_count,
ed2dc618 5769 objfile_name (map.dwarf2_per_objfile->objfile));
927aa2e7
JK
5770 return NULL;
5771 }
5772
5773 for (;;)
5774 {
5775 const uint32_t namei_full_hash
5776 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5777 (map.hash_table_reordered + namei), 4,
5778 map.dwarf5_byte_order);
5779 if (full_hash % map.bucket_count != namei_full_hash % map.bucket_count)
5780 return NULL;
5781
5782 if (full_hash == namei_full_hash)
5783 {
5784 const char *const namei_string = map.namei_to_name (namei);
5785
5786#if 0 /* An expensive sanity check. */
5787 if (namei_full_hash != dwarf5_djb_hash (namei_string))
5788 {
b98664d3 5789 complaint (_("Wrong .debug_names hash for string at index %u "
927aa2e7
JK
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 {
b98664d3 5818 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
927aa2e7
JK
5819 "[in module %s]"),
5820 namei, map.name_count,
ed2dc618 5821 objfile_name (map.dwarf2_per_objfile->objfile));
927aa2e7
JK
5822 return NULL;
5823 }
5824
5825 const ULONGEST namei_entry_offs
5826 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5827 + namei * map.offset_size),
5828 map.offset_size, map.dwarf5_byte_order);
5829 return map.entry_pool + namei_entry_offs;
5830}
5831
5832/* See dw2_debug_names_iterator. */
5833
5834dwarf2_per_cu_data *
5835dw2_debug_names_iterator::next ()
5836{
5837 if (m_addr == NULL)
5838 return NULL;
5839
ed2dc618
SM
5840 struct dwarf2_per_objfile *dwarf2_per_objfile = m_map.dwarf2_per_objfile;
5841 struct objfile *objfile = dwarf2_per_objfile->objfile;
5842 bfd *const abfd = objfile->obfd;
927aa2e7
JK
5843
5844 again:
5845
5846 unsigned int bytes_read;
5847 const ULONGEST abbrev = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5848 m_addr += bytes_read;
5849 if (abbrev == 0)
5850 return NULL;
5851
5852 const auto indexval_it = m_map.abbrev_map.find (abbrev);
5853 if (indexval_it == m_map.abbrev_map.cend ())
5854 {
b98664d3 5855 complaint (_("Wrong .debug_names undefined abbrev code %s "
927aa2e7 5856 "[in module %s]"),
ed2dc618 5857 pulongest (abbrev), objfile_name (objfile));
927aa2e7
JK
5858 return NULL;
5859 }
5860 const mapped_debug_names::index_val &indexval = indexval_it->second;
5861 bool have_is_static = false;
5862 bool is_static;
5863 dwarf2_per_cu_data *per_cu = NULL;
5864 for (const mapped_debug_names::index_val::attr &attr : indexval.attr_vec)
5865 {
5866 ULONGEST ull;
5867 switch (attr.form)
5868 {
5869 case DW_FORM_implicit_const:
5870 ull = attr.implicit_const;
5871 break;
5872 case DW_FORM_flag_present:
5873 ull = 1;
5874 break;
5875 case DW_FORM_udata:
5876 ull = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5877 m_addr += bytes_read;
5878 break;
5879 default:
b98664d3 5880 complaint (_("Unsupported .debug_names form %s [in module %s]"),
927aa2e7 5881 dwarf_form_name (attr.form),
ed2dc618 5882 objfile_name (objfile));
927aa2e7
JK
5883 return NULL;
5884 }
5885 switch (attr.dw_idx)
5886 {
5887 case DW_IDX_compile_unit:
5888 /* Don't crash on bad data. */
b76e467d 5889 if (ull >= dwarf2_per_objfile->all_comp_units.size ())
927aa2e7 5890 {
b98664d3 5891 complaint (_(".debug_names entry has bad CU index %s"
927aa2e7
JK
5892 " [in module %s]"),
5893 pulongest (ull),
5894 objfile_name (dwarf2_per_objfile->objfile));
5895 continue;
5896 }
ff4c9fec 5897 per_cu = dwarf2_per_objfile->get_cutu (ull);
927aa2e7 5898 break;
8af5c486
JK
5899 case DW_IDX_type_unit:
5900 /* Don't crash on bad data. */
b2bdb8cf 5901 if (ull >= dwarf2_per_objfile->all_type_units.size ())
8af5c486 5902 {
b98664d3 5903 complaint (_(".debug_names entry has bad TU index %s"
8af5c486
JK
5904 " [in module %s]"),
5905 pulongest (ull),
5906 objfile_name (dwarf2_per_objfile->objfile));
5907 continue;
5908 }
ff4c9fec 5909 per_cu = &dwarf2_per_objfile->get_tu (ull)->per_cu;
8af5c486 5910 break;
927aa2e7
JK
5911 case DW_IDX_GNU_internal:
5912 if (!m_map.augmentation_is_gdb)
5913 break;
5914 have_is_static = true;
5915 is_static = true;
5916 break;
5917 case DW_IDX_GNU_external:
5918 if (!m_map.augmentation_is_gdb)
5919 break;
5920 have_is_static = true;
5921 is_static = false;
5922 break;
5923 }
5924 }
5925
5926 /* Skip if already read in. */
5927 if (per_cu->v.quick->compunit_symtab)
5928 goto again;
5929
5930 /* Check static vs global. */
5931 if (have_is_static)
5932 {
5933 const bool want_static = m_block_index != GLOBAL_BLOCK;
5934 if (m_want_specific_block && want_static != is_static)
5935 goto again;
5936 }
5937
5938 /* Match dw2_symtab_iter_next, symbol_kind
5939 and debug_names::psymbol_tag. */
5940 switch (m_domain)
5941 {
5942 case VAR_DOMAIN:
5943 switch (indexval.dwarf_tag)
5944 {
5945 case DW_TAG_variable:
5946 case DW_TAG_subprogram:
5947 /* Some types are also in VAR_DOMAIN. */
5948 case DW_TAG_typedef:
5949 case DW_TAG_structure_type:
5950 break;
5951 default:
5952 goto again;
5953 }
5954 break;
5955 case STRUCT_DOMAIN:
5956 switch (indexval.dwarf_tag)
5957 {
5958 case DW_TAG_typedef:
5959 case DW_TAG_structure_type:
5960 break;
5961 default:
5962 goto again;
5963 }
5964 break;
5965 case LABEL_DOMAIN:
5966 switch (indexval.dwarf_tag)
5967 {
5968 case 0:
5969 case DW_TAG_variable:
5970 break;
5971 default:
5972 goto again;
5973 }
5974 break;
5975 default:
5976 break;
5977 }
5978
5979 /* Match dw2_expand_symtabs_matching, symbol_kind and
5980 debug_names::psymbol_tag. */
5981 switch (m_search)
4b514bc8 5982 {
927aa2e7
JK
5983 case VARIABLES_DOMAIN:
5984 switch (indexval.dwarf_tag)
4b514bc8 5985 {
927aa2e7
JK
5986 case DW_TAG_variable:
5987 break;
5988 default:
5989 goto again;
4b514bc8 5990 }
927aa2e7
JK
5991 break;
5992 case FUNCTIONS_DOMAIN:
5993 switch (indexval.dwarf_tag)
4b514bc8 5994 {
927aa2e7
JK
5995 case DW_TAG_subprogram:
5996 break;
5997 default:
5998 goto again;
4b514bc8 5999 }
927aa2e7
JK
6000 break;
6001 case TYPES_DOMAIN:
6002 switch (indexval.dwarf_tag)
6003 {
6004 case DW_TAG_typedef:
6005 case DW_TAG_structure_type:
6006 break;
6007 default:
6008 goto again;
6009 }
6010 break;
6011 default:
6012 break;
4b514bc8 6013 }
927aa2e7
JK
6014
6015 return per_cu;
4b514bc8 6016}
61920122 6017
927aa2e7
JK
6018static struct compunit_symtab *
6019dw2_debug_names_lookup_symbol (struct objfile *objfile, int block_index_int,
6020 const char *name, domain_enum domain)
4b514bc8 6021{
927aa2e7 6022 const block_enum block_index = static_cast<block_enum> (block_index_int);
ed2dc618
SM
6023 struct dwarf2_per_objfile *dwarf2_per_objfile
6024 = get_dwarf2_per_objfile (objfile);
61920122 6025
927aa2e7
JK
6026 const auto &mapp = dwarf2_per_objfile->debug_names_table;
6027 if (!mapp)
61920122 6028 {
927aa2e7
JK
6029 /* index is NULL if OBJF_READNOW. */
6030 return NULL;
6031 }
6032 const auto &map = *mapp;
9291a0cd 6033
927aa2e7
JK
6034 dw2_debug_names_iterator iter (map, true /* want_specific_block */,
6035 block_index, domain, name);
9703b513 6036
927aa2e7
JK
6037 struct compunit_symtab *stab_best = NULL;
6038 struct dwarf2_per_cu_data *per_cu;
6039 while ((per_cu = iter.next ()) != NULL)
6040 {
6041 struct symbol *sym, *with_opaque = NULL;
58f0c718 6042 struct compunit_symtab *stab = dw2_instantiate_symtab (per_cu, false);
927aa2e7
JK
6043 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
6044 struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
9703b513 6045
927aa2e7
JK
6046 sym = block_find_symbol (block, name, domain,
6047 block_find_non_opaque_type_preferred,
6048 &with_opaque);
9703b513 6049
927aa2e7
JK
6050 /* Some caution must be observed with overloaded functions and
6051 methods, since the index will not contain any overload
6052 information (but NAME might contain it). */
a3ec0bb1 6053
927aa2e7
JK
6054 if (sym != NULL
6055 && strcmp_iw (SYMBOL_SEARCH_NAME (sym), name) == 0)
6056 return stab;
6057 if (with_opaque != NULL
6058 && strcmp_iw (SYMBOL_SEARCH_NAME (with_opaque), name) == 0)
6059 stab_best = stab;
9703b513 6060
927aa2e7 6061 /* Keep looking through other CUs. */
9703b513
TT
6062 }
6063
927aa2e7 6064 return stab_best;
9703b513
TT
6065}
6066
927aa2e7
JK
6067/* This dumps minimal information about .debug_names. It is called
6068 via "mt print objfiles". The gdb.dwarf2/gdb-index.exp testcase
6069 uses this to verify that .debug_names has been loaded. */
9291a0cd 6070
927aa2e7
JK
6071static void
6072dw2_debug_names_dump (struct objfile *objfile)
6073{
ed2dc618
SM
6074 struct dwarf2_per_objfile *dwarf2_per_objfile
6075 = get_dwarf2_per_objfile (objfile);
6076
927aa2e7
JK
6077 gdb_assert (dwarf2_per_objfile->using_index);
6078 printf_filtered (".debug_names:");
6079 if (dwarf2_per_objfile->debug_names_table)
6080 printf_filtered (" exists\n");
6081 else
6082 printf_filtered (" faked for \"readnow\"\n");
6083 printf_filtered ("\n");
9291a0cd
TT
6084}
6085
9291a0cd 6086static void
927aa2e7
JK
6087dw2_debug_names_expand_symtabs_for_function (struct objfile *objfile,
6088 const char *func_name)
9291a0cd 6089{
ed2dc618
SM
6090 struct dwarf2_per_objfile *dwarf2_per_objfile
6091 = get_dwarf2_per_objfile (objfile);
ae2de4f8 6092
927aa2e7
JK
6093 /* dwarf2_per_objfile->debug_names_table is NULL if OBJF_READNOW. */
6094 if (dwarf2_per_objfile->debug_names_table)
24c79950 6095 {
927aa2e7 6096 const mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
24c79950 6097
927aa2e7
JK
6098 /* Note: It doesn't matter what we pass for block_index here. */
6099 dw2_debug_names_iterator iter (map, false /* want_specific_block */,
6100 GLOBAL_BLOCK, VAR_DOMAIN, func_name);
24c79950 6101
927aa2e7
JK
6102 struct dwarf2_per_cu_data *per_cu;
6103 while ((per_cu = iter.next ()) != NULL)
58f0c718 6104 dw2_instantiate_symtab (per_cu, false);
927aa2e7
JK
6105 }
6106}
24c79950 6107
927aa2e7
JK
6108static void
6109dw2_debug_names_expand_symtabs_matching
6110 (struct objfile *objfile,
6111 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
6112 const lookup_name_info &lookup_name,
6113 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
6114 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
6115 enum search_domain kind)
6116{
ed2dc618
SM
6117 struct dwarf2_per_objfile *dwarf2_per_objfile
6118 = get_dwarf2_per_objfile (objfile);
9291a0cd 6119
927aa2e7
JK
6120 /* debug_names_table is NULL if OBJF_READNOW. */
6121 if (!dwarf2_per_objfile->debug_names_table)
6122 return;
9291a0cd 6123
ed2dc618 6124 dw_expand_symtabs_matching_file_matcher (dwarf2_per_objfile, file_matcher);
24c79950 6125
44ed8f3e 6126 mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
bbf2f4df 6127
44ed8f3e
PA
6128 dw2_expand_symtabs_matching_symbol (map, lookup_name,
6129 symbol_matcher,
6130 kind, [&] (offset_type namei)
927aa2e7 6131 {
927aa2e7
JK
6132 /* The name was matched, now expand corresponding CUs that were
6133 marked. */
6134 dw2_debug_names_iterator iter (map, kind, namei);
bbf2f4df 6135
927aa2e7
JK
6136 struct dwarf2_per_cu_data *per_cu;
6137 while ((per_cu = iter.next ()) != NULL)
6138 dw2_expand_symtabs_matching_one (per_cu, file_matcher,
6139 expansion_notify);
44ed8f3e 6140 });
9291a0cd
TT
6141}
6142
927aa2e7 6143const struct quick_symbol_functions dwarf2_debug_names_functions =
9291a0cd
TT
6144{
6145 dw2_has_symbols,
6146 dw2_find_last_source_symtab,
6147 dw2_forget_cached_source_info,
f8eba3c6 6148 dw2_map_symtabs_matching_filename,
927aa2e7 6149 dw2_debug_names_lookup_symbol,
9291a0cd 6150 dw2_print_stats,
927aa2e7 6151 dw2_debug_names_dump,
9291a0cd 6152 dw2_relocate,
927aa2e7 6153 dw2_debug_names_expand_symtabs_for_function,
9291a0cd 6154 dw2_expand_all_symtabs,
652a8996 6155 dw2_expand_symtabs_with_fullname,
40658b94 6156 dw2_map_matching_symbols,
927aa2e7 6157 dw2_debug_names_expand_symtabs_matching,
43f3e411 6158 dw2_find_pc_sect_compunit_symtab,
71a3c369 6159 NULL,
9291a0cd
TT
6160 dw2_map_symbol_filenames
6161};
6162
3c0aa29a 6163/* See symfile.h. */
9291a0cd 6164
3c0aa29a
PA
6165bool
6166dwarf2_initialize_objfile (struct objfile *objfile, dw_index_kind *index_kind)
9291a0cd 6167{
ed2dc618
SM
6168 struct dwarf2_per_objfile *dwarf2_per_objfile
6169 = get_dwarf2_per_objfile (objfile);
6170
9291a0cd
TT
6171 /* If we're about to read full symbols, don't bother with the
6172 indices. In this case we also don't care if some other debug
6173 format is making psymtabs, because they are all about to be
6174 expanded anyway. */
6175 if ((objfile->flags & OBJF_READNOW))
6176 {
9291a0cd 6177 dwarf2_per_objfile->using_index = 1;
ed2dc618
SM
6178 create_all_comp_units (dwarf2_per_objfile);
6179 create_all_type_units (dwarf2_per_objfile);
b76e467d
SM
6180 dwarf2_per_objfile->quick_file_names_table
6181 = create_quick_file_names_table
6182 (dwarf2_per_objfile->all_comp_units.size ());
9291a0cd 6183
b76e467d 6184 for (int i = 0; i < (dwarf2_per_objfile->all_comp_units.size ()
b2bdb8cf 6185 + dwarf2_per_objfile->all_type_units.size ()); ++i)
9291a0cd 6186 {
ff4c9fec 6187 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (i);
9291a0cd 6188
e254ef6a
DE
6189 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6190 struct dwarf2_per_cu_quick_data);
9291a0cd
TT
6191 }
6192
6193 /* Return 1 so that gdb sees the "quick" functions. However,
6194 these functions will be no-ops because we will have expanded
6195 all symtabs. */
3c0aa29a
PA
6196 *index_kind = dw_index_kind::GDB_INDEX;
6197 return true;
9291a0cd
TT
6198 }
6199
ed2dc618 6200 if (dwarf2_read_debug_names (dwarf2_per_objfile))
3c0aa29a
PA
6201 {
6202 *index_kind = dw_index_kind::DEBUG_NAMES;
6203 return true;
6204 }
927aa2e7 6205
7b23e087 6206 if (dwarf2_read_gdb_index (dwarf2_per_objfile))
3c0aa29a
PA
6207 {
6208 *index_kind = dw_index_kind::GDB_INDEX;
6209 return true;
6210 }
9291a0cd 6211
3c0aa29a 6212 return false;
9291a0cd
TT
6213}
6214
6215\f
6216
dce234bc
PP
6217/* Build a partial symbol table. */
6218
6219void
f29dff0a 6220dwarf2_build_psymtabs (struct objfile *objfile)
dce234bc 6221{
ed2dc618
SM
6222 struct dwarf2_per_objfile *dwarf2_per_objfile
6223 = get_dwarf2_per_objfile (objfile);
c9bf0622 6224
af5bf4ad
SM
6225 if (objfile->global_psymbols.capacity () == 0
6226 && objfile->static_psymbols.capacity () == 0)
6227 init_psymbol_list (objfile, 1024);
c906108c 6228
492d29ea 6229 TRY
c9bf0622
TT
6230 {
6231 /* This isn't really ideal: all the data we allocate on the
6232 objfile's obstack is still uselessly kept around. However,
6233 freeing it seems unsafe. */
906768f9 6234 psymtab_discarder psymtabs (objfile);
ed2dc618 6235 dwarf2_build_psymtabs_hard (dwarf2_per_objfile);
906768f9 6236 psymtabs.keep ();
c9bf0622 6237 }
492d29ea
PA
6238 CATCH (except, RETURN_MASK_ERROR)
6239 {
6240 exception_print (gdb_stderr, except);
6241 }
6242 END_CATCH
c906108c 6243}
c906108c 6244
1ce1cefd
DE
6245/* Return the total length of the CU described by HEADER. */
6246
6247static unsigned int
6248get_cu_length (const struct comp_unit_head *header)
6249{
6250 return header->initial_length_size + header->length;
6251}
6252
9c541725 6253/* Return TRUE if SECT_OFF is within CU_HEADER. */
45452591 6254
9c541725
PA
6255static inline bool
6256offset_in_cu_p (const comp_unit_head *cu_header, sect_offset sect_off)
45452591 6257{
9c541725
PA
6258 sect_offset bottom = cu_header->sect_off;
6259 sect_offset top = cu_header->sect_off + get_cu_length (cu_header);
9a619af0 6260
9c541725 6261 return sect_off >= bottom && sect_off < top;
45452591
DE
6262}
6263
3b80fe9b
DE
6264/* Find the base address of the compilation unit for range lists and
6265 location lists. It will normally be specified by DW_AT_low_pc.
6266 In DWARF-3 draft 4, the base address could be overridden by
6267 DW_AT_entry_pc. It's been removed, but GCC still uses this for
6268 compilation units with discontinuous ranges. */
6269
6270static void
6271dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
6272{
6273 struct attribute *attr;
6274
6275 cu->base_known = 0;
6276 cu->base_address = 0;
6277
6278 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
6279 if (attr)
6280 {
31aa7e4e 6281 cu->base_address = attr_value_as_address (attr);
3b80fe9b
DE
6282 cu->base_known = 1;
6283 }
6284 else
6285 {
6286 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
6287 if (attr)
6288 {
31aa7e4e 6289 cu->base_address = attr_value_as_address (attr);
3b80fe9b
DE
6290 cu->base_known = 1;
6291 }
6292 }
6293}
6294
93311388 6295/* Read in the comp unit header information from the debug_info at info_ptr.
43988095 6296 Use rcuh_kind::COMPILE as the default type if not known by the caller.
93311388
DE
6297 NOTE: This leaves members offset, first_die_offset to be filled in
6298 by the caller. */
107d2387 6299
d521ce57 6300static const gdb_byte *
107d2387 6301read_comp_unit_head (struct comp_unit_head *cu_header,
43988095
JK
6302 const gdb_byte *info_ptr,
6303 struct dwarf2_section_info *section,
6304 rcuh_kind section_kind)
107d2387
AC
6305{
6306 int signed_addr;
891d2f0b 6307 unsigned int bytes_read;
43988095
JK
6308 const char *filename = get_section_file_name (section);
6309 bfd *abfd = get_section_bfd_owner (section);
c764a876
DE
6310
6311 cu_header->length = read_initial_length (abfd, info_ptr, &bytes_read);
6312 cu_header->initial_length_size = bytes_read;
6313 cu_header->offset_size = (bytes_read == 4) ? 4 : 8;
613e1657 6314 info_ptr += bytes_read;
107d2387 6315 cu_header->version = read_2_bytes (abfd, info_ptr);
1ea5da02
TV
6316 if (cu_header->version < 2 || cu_header->version > 5)
6317 error (_("Dwarf Error: wrong version in compilation unit header "
6318 "(is %d, should be 2, 3, 4 or 5) [in module %s]"),
6319 cu_header->version, filename);
107d2387 6320 info_ptr += 2;
43988095
JK
6321 if (cu_header->version < 5)
6322 switch (section_kind)
6323 {
6324 case rcuh_kind::COMPILE:
6325 cu_header->unit_type = DW_UT_compile;
6326 break;
6327 case rcuh_kind::TYPE:
6328 cu_header->unit_type = DW_UT_type;
6329 break;
6330 default:
6331 internal_error (__FILE__, __LINE__,
6332 _("read_comp_unit_head: invalid section_kind"));
6333 }
6334 else
6335 {
6336 cu_header->unit_type = static_cast<enum dwarf_unit_type>
6337 (read_1_byte (abfd, info_ptr));
6338 info_ptr += 1;
6339 switch (cu_header->unit_type)
6340 {
6341 case DW_UT_compile:
6342 if (section_kind != rcuh_kind::COMPILE)
6343 error (_("Dwarf Error: wrong unit_type in compilation unit header "
6344 "(is DW_UT_compile, should be DW_UT_type) [in module %s]"),
6345 filename);
6346 break;
6347 case DW_UT_type:
6348 section_kind = rcuh_kind::TYPE;
6349 break;
6350 default:
6351 error (_("Dwarf Error: wrong unit_type in compilation unit header "
6352 "(is %d, should be %d or %d) [in module %s]"),
6353 cu_header->unit_type, DW_UT_compile, DW_UT_type, filename);
6354 }
6355
6356 cu_header->addr_size = read_1_byte (abfd, info_ptr);
6357 info_ptr += 1;
6358 }
9c541725
PA
6359 cu_header->abbrev_sect_off = (sect_offset) read_offset (abfd, info_ptr,
6360 cu_header,
6361 &bytes_read);
613e1657 6362 info_ptr += bytes_read;
43988095
JK
6363 if (cu_header->version < 5)
6364 {
6365 cu_header->addr_size = read_1_byte (abfd, info_ptr);
6366 info_ptr += 1;
6367 }
107d2387
AC
6368 signed_addr = bfd_get_sign_extend_vma (abfd);
6369 if (signed_addr < 0)
8e65ff28 6370 internal_error (__FILE__, __LINE__,
e2e0b3e5 6371 _("read_comp_unit_head: dwarf from non elf file"));
107d2387 6372 cu_header->signed_addr_p = signed_addr;
c764a876 6373
43988095
JK
6374 if (section_kind == rcuh_kind::TYPE)
6375 {
6376 LONGEST type_offset;
6377
6378 cu_header->signature = read_8_bytes (abfd, info_ptr);
6379 info_ptr += 8;
6380
6381 type_offset = read_offset (abfd, info_ptr, cu_header, &bytes_read);
6382 info_ptr += bytes_read;
9c541725
PA
6383 cu_header->type_cu_offset_in_tu = (cu_offset) type_offset;
6384 if (to_underlying (cu_header->type_cu_offset_in_tu) != type_offset)
43988095
JK
6385 error (_("Dwarf Error: Too big type_offset in compilation unit "
6386 "header (is %s) [in module %s]"), plongest (type_offset),
6387 filename);
6388 }
6389
107d2387
AC
6390 return info_ptr;
6391}
6392
36586728
TT
6393/* Helper function that returns the proper abbrev section for
6394 THIS_CU. */
6395
6396static struct dwarf2_section_info *
6397get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
6398{
6399 struct dwarf2_section_info *abbrev;
ed2dc618 6400 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
36586728
TT
6401
6402 if (this_cu->is_dwz)
ed2dc618 6403 abbrev = &dwarf2_get_dwz_file (dwarf2_per_objfile)->abbrev;
36586728
TT
6404 else
6405 abbrev = &dwarf2_per_objfile->abbrev;
6406
6407 return abbrev;
6408}
6409
9ff913ba
DE
6410/* Subroutine of read_and_check_comp_unit_head and
6411 read_and_check_type_unit_head to simplify them.
6412 Perform various error checking on the header. */
6413
6414static void
ed2dc618
SM
6415error_check_comp_unit_head (struct dwarf2_per_objfile *dwarf2_per_objfile,
6416 struct comp_unit_head *header,
4bdcc0c1
DE
6417 struct dwarf2_section_info *section,
6418 struct dwarf2_section_info *abbrev_section)
9ff913ba 6419{
a32a8923 6420 const char *filename = get_section_file_name (section);
9ff913ba 6421
9c541725 6422 if (to_underlying (header->abbrev_sect_off)
36586728 6423 >= dwarf2_section_size (dwarf2_per_objfile->objfile, abbrev_section))
9d8780f0
SM
6424 error (_("Dwarf Error: bad offset (%s) in compilation unit header "
6425 "(offset %s + 6) [in module %s]"),
6426 sect_offset_str (header->abbrev_sect_off),
6427 sect_offset_str (header->sect_off),
9ff913ba
DE
6428 filename);
6429
9c541725 6430 /* Cast to ULONGEST to use 64-bit arithmetic when possible to
9ff913ba 6431 avoid potential 32-bit overflow. */
9c541725 6432 if (((ULONGEST) header->sect_off + get_cu_length (header))
9ff913ba 6433 > section->size)
9c541725 6434 error (_("Dwarf Error: bad length (0x%x) in compilation unit header "
9d8780f0
SM
6435 "(offset %s + 0) [in module %s]"),
6436 header->length, sect_offset_str (header->sect_off),
9ff913ba
DE
6437 filename);
6438}
6439
6440/* Read in a CU/TU header and perform some basic error checking.
6441 The contents of the header are stored in HEADER.
6442 The result is a pointer to the start of the first DIE. */
adabb602 6443
d521ce57 6444static const gdb_byte *
ed2dc618
SM
6445read_and_check_comp_unit_head (struct dwarf2_per_objfile *dwarf2_per_objfile,
6446 struct comp_unit_head *header,
9ff913ba 6447 struct dwarf2_section_info *section,
4bdcc0c1 6448 struct dwarf2_section_info *abbrev_section,
d521ce57 6449 const gdb_byte *info_ptr,
43988095 6450 rcuh_kind section_kind)
72bf9492 6451{
d521ce57 6452 const gdb_byte *beg_of_comp_unit = info_ptr;
72bf9492 6453
9c541725 6454 header->sect_off = (sect_offset) (beg_of_comp_unit - section->buffer);
adabb602 6455
43988095 6456 info_ptr = read_comp_unit_head (header, info_ptr, section, section_kind);
9ff913ba 6457
9c541725 6458 header->first_die_cu_offset = (cu_offset) (info_ptr - beg_of_comp_unit);
348e048f 6459
ed2dc618
SM
6460 error_check_comp_unit_head (dwarf2_per_objfile, header, section,
6461 abbrev_section);
9ff913ba
DE
6462
6463 return info_ptr;
348e048f
DE
6464}
6465
f4dc4d17
DE
6466/* Fetch the abbreviation table offset from a comp or type unit header. */
6467
6468static sect_offset
ed2dc618
SM
6469read_abbrev_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
6470 struct dwarf2_section_info *section,
9c541725 6471 sect_offset sect_off)
f4dc4d17 6472{
a32a8923 6473 bfd *abfd = get_section_bfd_owner (section);
d521ce57 6474 const gdb_byte *info_ptr;
ac298888 6475 unsigned int initial_length_size, offset_size;
43988095 6476 uint16_t version;
f4dc4d17
DE
6477
6478 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
9c541725 6479 info_ptr = section->buffer + to_underlying (sect_off);
ac298888 6480 read_initial_length (abfd, info_ptr, &initial_length_size);
f4dc4d17 6481 offset_size = initial_length_size == 4 ? 4 : 8;
43988095
JK
6482 info_ptr += initial_length_size;
6483
6484 version = read_2_bytes (abfd, info_ptr);
6485 info_ptr += 2;
6486 if (version >= 5)
6487 {
6488 /* Skip unit type and address size. */
6489 info_ptr += 2;
6490 }
6491
9c541725 6492 return (sect_offset) read_offset_1 (abfd, info_ptr, offset_size);
f4dc4d17
DE
6493}
6494
aaa75496
JB
6495/* Allocate a new partial symtab for file named NAME and mark this new
6496 partial symtab as being an include of PST. */
6497
6498static void
d521ce57 6499dwarf2_create_include_psymtab (const char *name, struct partial_symtab *pst,
aaa75496
JB
6500 struct objfile *objfile)
6501{
6502 struct partial_symtab *subpst = allocate_psymtab (name, objfile);
6503
fbd9ab74
JK
6504 if (!IS_ABSOLUTE_PATH (subpst->filename))
6505 {
6506 /* It shares objfile->objfile_obstack. */
6507 subpst->dirname = pst->dirname;
6508 }
6509
aaa75496
JB
6510 subpst->textlow = 0;
6511 subpst->texthigh = 0;
6512
8d749320
SM
6513 subpst->dependencies
6514 = XOBNEW (&objfile->objfile_obstack, struct partial_symtab *);
aaa75496
JB
6515 subpst->dependencies[0] = pst;
6516 subpst->number_of_dependencies = 1;
6517
6518 subpst->globals_offset = 0;
6519 subpst->n_global_syms = 0;
6520 subpst->statics_offset = 0;
6521 subpst->n_static_syms = 0;
43f3e411 6522 subpst->compunit_symtab = NULL;
aaa75496
JB
6523 subpst->read_symtab = pst->read_symtab;
6524 subpst->readin = 0;
6525
6526 /* No private part is necessary for include psymtabs. This property
6527 can be used to differentiate between such include psymtabs and
10b3939b 6528 the regular ones. */
58a9656e 6529 subpst->read_symtab_private = NULL;
aaa75496
JB
6530}
6531
6532/* Read the Line Number Program data and extract the list of files
6533 included by the source file represented by PST. Build an include
d85a05f0 6534 partial symtab for each of these included files. */
aaa75496
JB
6535
6536static void
6537dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
dee91e82
DE
6538 struct die_info *die,
6539 struct partial_symtab *pst)
aaa75496 6540{
fff8551c 6541 line_header_up lh;
d85a05f0 6542 struct attribute *attr;
aaa75496 6543
d85a05f0
DJ
6544 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
6545 if (attr)
9c541725 6546 lh = dwarf_decode_line_header ((sect_offset) DW_UNSND (attr), cu);
aaa75496
JB
6547 if (lh == NULL)
6548 return; /* No linetable, so no includes. */
6549
c6da4cef 6550 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). */
fff8551c 6551 dwarf_decode_lines (lh.get (), pst->dirname, cu, pst, pst->textlow, 1);
aaa75496
JB
6552}
6553
348e048f 6554static hashval_t
52dc124a 6555hash_signatured_type (const void *item)
348e048f 6556{
9a3c8263
SM
6557 const struct signatured_type *sig_type
6558 = (const struct signatured_type *) item;
9a619af0 6559
348e048f 6560 /* This drops the top 32 bits of the signature, but is ok for a hash. */
52dc124a 6561 return sig_type->signature;
348e048f
DE
6562}
6563
6564static int
52dc124a 6565eq_signatured_type (const void *item_lhs, const void *item_rhs)
348e048f 6566{
9a3c8263
SM
6567 const struct signatured_type *lhs = (const struct signatured_type *) item_lhs;
6568 const struct signatured_type *rhs = (const struct signatured_type *) item_rhs;
9a619af0 6569
348e048f
DE
6570 return lhs->signature == rhs->signature;
6571}
6572
1fd400ff
TT
6573/* Allocate a hash table for signatured types. */
6574
6575static htab_t
673bfd45 6576allocate_signatured_type_table (struct objfile *objfile)
1fd400ff
TT
6577{
6578 return htab_create_alloc_ex (41,
52dc124a
DE
6579 hash_signatured_type,
6580 eq_signatured_type,
1fd400ff
TT
6581 NULL,
6582 &objfile->objfile_obstack,
6583 hashtab_obstack_allocate,
6584 dummy_obstack_deallocate);
6585}
6586
d467dd73 6587/* A helper function to add a signatured type CU to a table. */
1fd400ff
TT
6588
6589static int
d467dd73 6590add_signatured_type_cu_to_table (void **slot, void *datum)
1fd400ff 6591{
9a3c8263 6592 struct signatured_type *sigt = (struct signatured_type *) *slot;
b2bdb8cf
SM
6593 std::vector<signatured_type *> *all_type_units
6594 = (std::vector<signatured_type *> *) datum;
1fd400ff 6595
b2bdb8cf 6596 all_type_units->push_back (sigt);
1fd400ff
TT
6597
6598 return 1;
6599}
6600
78d4d2c5 6601/* A helper for create_debug_types_hash_table. Read types from SECTION
43988095
JK
6602 and fill them into TYPES_HTAB. It will process only type units,
6603 therefore DW_UT_type. */
c88ee1f0 6604
78d4d2c5 6605static void
ed2dc618
SM
6606create_debug_type_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
6607 struct dwo_file *dwo_file,
43988095
JK
6608 dwarf2_section_info *section, htab_t &types_htab,
6609 rcuh_kind section_kind)
348e048f 6610{
3019eac3 6611 struct objfile *objfile = dwarf2_per_objfile->objfile;
4bdcc0c1 6612 struct dwarf2_section_info *abbrev_section;
78d4d2c5
JK
6613 bfd *abfd;
6614 const gdb_byte *info_ptr, *end_ptr;
348e048f 6615
4bdcc0c1
DE
6616 abbrev_section = (dwo_file != NULL
6617 ? &dwo_file->sections.abbrev
6618 : &dwarf2_per_objfile->abbrev);
6619
b4f54984 6620 if (dwarf_read_debug)
43988095
JK
6621 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
6622 get_section_name (section),
a32a8923 6623 get_section_file_name (abbrev_section));
09406207 6624
78d4d2c5
JK
6625 dwarf2_read_section (objfile, section);
6626 info_ptr = section->buffer;
348e048f 6627
78d4d2c5
JK
6628 if (info_ptr == NULL)
6629 return;
348e048f 6630
78d4d2c5
JK
6631 /* We can't set abfd until now because the section may be empty or
6632 not present, in which case the bfd is unknown. */
6633 abfd = get_section_bfd_owner (section);
348e048f 6634
78d4d2c5
JK
6635 /* We don't use init_cutu_and_read_dies_simple, or some such, here
6636 because we don't need to read any dies: the signature is in the
6637 header. */
3019eac3 6638
78d4d2c5
JK
6639 end_ptr = info_ptr + section->size;
6640 while (info_ptr < end_ptr)
6641 {
78d4d2c5
JK
6642 struct signatured_type *sig_type;
6643 struct dwo_unit *dwo_tu;
6644 void **slot;
6645 const gdb_byte *ptr = info_ptr;
6646 struct comp_unit_head header;
6647 unsigned int length;
8b70b953 6648
9c541725 6649 sect_offset sect_off = (sect_offset) (ptr - section->buffer);
348e048f 6650
a49dd8dd
JK
6651 /* Initialize it due to a false compiler warning. */
6652 header.signature = -1;
9c541725 6653 header.type_cu_offset_in_tu = (cu_offset) -1;
a49dd8dd 6654
78d4d2c5
JK
6655 /* We need to read the type's signature in order to build the hash
6656 table, but we don't need anything else just yet. */
348e048f 6657
ed2dc618 6658 ptr = read_and_check_comp_unit_head (dwarf2_per_objfile, &header, section,
43988095 6659 abbrev_section, ptr, section_kind);
348e048f 6660
78d4d2c5 6661 length = get_cu_length (&header);
6caca83c 6662
78d4d2c5
JK
6663 /* Skip dummy type units. */
6664 if (ptr >= info_ptr + length
43988095
JK
6665 || peek_abbrev_code (abfd, ptr) == 0
6666 || header.unit_type != DW_UT_type)
78d4d2c5
JK
6667 {
6668 info_ptr += length;
6669 continue;
6670 }
dee91e82 6671
78d4d2c5
JK
6672 if (types_htab == NULL)
6673 {
6674 if (dwo_file)
6675 types_htab = allocate_dwo_unit_table (objfile);
6676 else
6677 types_htab = allocate_signatured_type_table (objfile);
6678 }
8b70b953 6679
78d4d2c5
JK
6680 if (dwo_file)
6681 {
6682 sig_type = NULL;
6683 dwo_tu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6684 struct dwo_unit);
6685 dwo_tu->dwo_file = dwo_file;
43988095 6686 dwo_tu->signature = header.signature;
9c541725 6687 dwo_tu->type_offset_in_tu = header.type_cu_offset_in_tu;
78d4d2c5 6688 dwo_tu->section = section;
9c541725 6689 dwo_tu->sect_off = sect_off;
78d4d2c5
JK
6690 dwo_tu->length = length;
6691 }
6692 else
6693 {
6694 /* N.B.: type_offset is not usable if this type uses a DWO file.
6695 The real type_offset is in the DWO file. */
6696 dwo_tu = NULL;
6697 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6698 struct signatured_type);
43988095 6699 sig_type->signature = header.signature;
9c541725 6700 sig_type->type_offset_in_tu = header.type_cu_offset_in_tu;
e3b94546 6701 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
78d4d2c5
JK
6702 sig_type->per_cu.is_debug_types = 1;
6703 sig_type->per_cu.section = section;
9c541725 6704 sig_type->per_cu.sect_off = sect_off;
78d4d2c5
JK
6705 sig_type->per_cu.length = length;
6706 }
6707
6708 slot = htab_find_slot (types_htab,
6709 dwo_file ? (void*) dwo_tu : (void *) sig_type,
6710 INSERT);
6711 gdb_assert (slot != NULL);
6712 if (*slot != NULL)
6713 {
9c541725 6714 sect_offset dup_sect_off;
0349ea22 6715
3019eac3
DE
6716 if (dwo_file)
6717 {
78d4d2c5
JK
6718 const struct dwo_unit *dup_tu
6719 = (const struct dwo_unit *) *slot;
6720
9c541725 6721 dup_sect_off = dup_tu->sect_off;
3019eac3
DE
6722 }
6723 else
6724 {
78d4d2c5
JK
6725 const struct signatured_type *dup_tu
6726 = (const struct signatured_type *) *slot;
6727
9c541725 6728 dup_sect_off = dup_tu->per_cu.sect_off;
3019eac3 6729 }
8b70b953 6730
b98664d3 6731 complaint (_("debug type entry at offset %s is duplicate to"
9d8780f0
SM
6732 " the entry at offset %s, signature %s"),
6733 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
43988095 6734 hex_string (header.signature));
78d4d2c5
JK
6735 }
6736 *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
3019eac3 6737
78d4d2c5 6738 if (dwarf_read_debug > 1)
9d8780f0
SM
6739 fprintf_unfiltered (gdb_stdlog, " offset %s, signature %s\n",
6740 sect_offset_str (sect_off),
43988095 6741 hex_string (header.signature));
3019eac3 6742
78d4d2c5
JK
6743 info_ptr += length;
6744 }
6745}
3019eac3 6746
78d4d2c5
JK
6747/* Create the hash table of all entries in the .debug_types
6748 (or .debug_types.dwo) section(s).
6749 If reading a DWO file, then DWO_FILE is a pointer to the DWO file object,
6750 otherwise it is NULL.
b3c8eb43 6751
78d4d2c5 6752 The result is a pointer to the hash table or NULL if there are no types.
348e048f 6753
78d4d2c5 6754 Note: This function processes DWO files only, not DWP files. */
348e048f 6755
78d4d2c5 6756static void
ed2dc618
SM
6757create_debug_types_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
6758 struct dwo_file *dwo_file,
78d4d2c5
JK
6759 VEC (dwarf2_section_info_def) *types,
6760 htab_t &types_htab)
6761{
6762 int ix;
6763 struct dwarf2_section_info *section;
6764
6765 if (VEC_empty (dwarf2_section_info_def, types))
6766 return;
348e048f 6767
78d4d2c5
JK
6768 for (ix = 0;
6769 VEC_iterate (dwarf2_section_info_def, types, ix, section);
6770 ++ix)
ed2dc618
SM
6771 create_debug_type_hash_table (dwarf2_per_objfile, dwo_file, section,
6772 types_htab, rcuh_kind::TYPE);
3019eac3
DE
6773}
6774
6775/* Create the hash table of all entries in the .debug_types section,
6776 and initialize all_type_units.
6777 The result is zero if there is an error (e.g. missing .debug_types section),
6778 otherwise non-zero. */
6779
6780static int
ed2dc618 6781create_all_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
3019eac3 6782{
78d4d2c5 6783 htab_t types_htab = NULL;
3019eac3 6784
ed2dc618
SM
6785 create_debug_type_hash_table (dwarf2_per_objfile, NULL,
6786 &dwarf2_per_objfile->info, types_htab,
43988095 6787 rcuh_kind::COMPILE);
ed2dc618
SM
6788 create_debug_types_hash_table (dwarf2_per_objfile, NULL,
6789 dwarf2_per_objfile->types, types_htab);
3019eac3
DE
6790 if (types_htab == NULL)
6791 {
6792 dwarf2_per_objfile->signatured_types = NULL;
6793 return 0;
6794 }
6795
348e048f
DE
6796 dwarf2_per_objfile->signatured_types = types_htab;
6797
b2bdb8cf
SM
6798 gdb_assert (dwarf2_per_objfile->all_type_units.empty ());
6799 dwarf2_per_objfile->all_type_units.reserve (htab_elements (types_htab));
6800
6801 htab_traverse_noresize (types_htab, add_signatured_type_cu_to_table,
6802 &dwarf2_per_objfile->all_type_units);
1fd400ff 6803
348e048f
DE
6804 return 1;
6805}
6806
6aa5f3a6
DE
6807/* Add an entry for signature SIG to dwarf2_per_objfile->signatured_types.
6808 If SLOT is non-NULL, it is the entry to use in the hash table.
6809 Otherwise we find one. */
6810
6811static struct signatured_type *
ed2dc618
SM
6812add_type_unit (struct dwarf2_per_objfile *dwarf2_per_objfile, ULONGEST sig,
6813 void **slot)
6aa5f3a6
DE
6814{
6815 struct objfile *objfile = dwarf2_per_objfile->objfile;
6aa5f3a6 6816
b2bdb8cf
SM
6817 if (dwarf2_per_objfile->all_type_units.size ()
6818 == dwarf2_per_objfile->all_type_units.capacity ())
6819 ++dwarf2_per_objfile->tu_stats.nr_all_type_units_reallocs;
6aa5f3a6 6820
b2bdb8cf
SM
6821 signatured_type *sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6822 struct signatured_type);
6823
6824 dwarf2_per_objfile->all_type_units.push_back (sig_type);
6aa5f3a6
DE
6825 sig_type->signature = sig;
6826 sig_type->per_cu.is_debug_types = 1;
6827 if (dwarf2_per_objfile->using_index)
6828 {
6829 sig_type->per_cu.v.quick =
6830 OBSTACK_ZALLOC (&objfile->objfile_obstack,
6831 struct dwarf2_per_cu_quick_data);
6832 }
6833
6834 if (slot == NULL)
6835 {
6836 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
6837 sig_type, INSERT);
6838 }
6839 gdb_assert (*slot == NULL);
6840 *slot = sig_type;
6841 /* The rest of sig_type must be filled in by the caller. */
6842 return sig_type;
6843}
6844
a2ce51a0
DE
6845/* Subroutine of lookup_dwo_signatured_type and lookup_dwp_signatured_type.
6846 Fill in SIG_ENTRY with DWO_ENTRY. */
6847
6848static void
ed2dc618 6849fill_in_sig_entry_from_dwo_entry (struct dwarf2_per_objfile *dwarf2_per_objfile,
a2ce51a0
DE
6850 struct signatured_type *sig_entry,
6851 struct dwo_unit *dwo_entry)
6852{
7ee85ab1 6853 /* Make sure we're not clobbering something we don't expect to. */
a2ce51a0
DE
6854 gdb_assert (! sig_entry->per_cu.queued);
6855 gdb_assert (sig_entry->per_cu.cu == NULL);
6aa5f3a6
DE
6856 if (dwarf2_per_objfile->using_index)
6857 {
6858 gdb_assert (sig_entry->per_cu.v.quick != NULL);
43f3e411 6859 gdb_assert (sig_entry->per_cu.v.quick->compunit_symtab == NULL);
6aa5f3a6
DE
6860 }
6861 else
6862 gdb_assert (sig_entry->per_cu.v.psymtab == NULL);
a2ce51a0 6863 gdb_assert (sig_entry->signature == dwo_entry->signature);
9c541725 6864 gdb_assert (to_underlying (sig_entry->type_offset_in_section) == 0);
a2ce51a0 6865 gdb_assert (sig_entry->type_unit_group == NULL);
7ee85ab1
DE
6866 gdb_assert (sig_entry->dwo_unit == NULL);
6867
6868 sig_entry->per_cu.section = dwo_entry->section;
9c541725 6869 sig_entry->per_cu.sect_off = dwo_entry->sect_off;
7ee85ab1
DE
6870 sig_entry->per_cu.length = dwo_entry->length;
6871 sig_entry->per_cu.reading_dwo_directly = 1;
e3b94546 6872 sig_entry->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
a2ce51a0
DE
6873 sig_entry->type_offset_in_tu = dwo_entry->type_offset_in_tu;
6874 sig_entry->dwo_unit = dwo_entry;
6875}
6876
6877/* Subroutine of lookup_signatured_type.
7ee85ab1
DE
6878 If we haven't read the TU yet, create the signatured_type data structure
6879 for a TU to be read in directly from a DWO file, bypassing the stub.
6880 This is the "Stay in DWO Optimization": When there is no DWP file and we're
6881 using .gdb_index, then when reading a CU we want to stay in the DWO file
6882 containing that CU. Otherwise we could end up reading several other DWO
6883 files (due to comdat folding) to process the transitive closure of all the
6884 mentioned TUs, and that can be slow. The current DWO file will have every
6885 type signature that it needs.
a2ce51a0
DE
6886 We only do this for .gdb_index because in the psymtab case we already have
6887 to read all the DWOs to build the type unit groups. */
6888
6889static struct signatured_type *
6890lookup_dwo_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6891{
518817b3
SM
6892 struct dwarf2_per_objfile *dwarf2_per_objfile
6893 = cu->per_cu->dwarf2_per_objfile;
a2ce51a0
DE
6894 struct objfile *objfile = dwarf2_per_objfile->objfile;
6895 struct dwo_file *dwo_file;
6896 struct dwo_unit find_dwo_entry, *dwo_entry;
6897 struct signatured_type find_sig_entry, *sig_entry;
6aa5f3a6 6898 void **slot;
a2ce51a0
DE
6899
6900 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
6901
6aa5f3a6
DE
6902 /* If TU skeletons have been removed then we may not have read in any
6903 TUs yet. */
6904 if (dwarf2_per_objfile->signatured_types == NULL)
6905 {
6906 dwarf2_per_objfile->signatured_types
6907 = allocate_signatured_type_table (objfile);
6908 }
a2ce51a0
DE
6909
6910 /* We only ever need to read in one copy of a signatured type.
6aa5f3a6
DE
6911 Use the global signatured_types array to do our own comdat-folding
6912 of types. If this is the first time we're reading this TU, and
6913 the TU has an entry in .gdb_index, replace the recorded data from
6914 .gdb_index with this TU. */
a2ce51a0 6915
a2ce51a0 6916 find_sig_entry.signature = sig;
6aa5f3a6
DE
6917 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
6918 &find_sig_entry, INSERT);
9a3c8263 6919 sig_entry = (struct signatured_type *) *slot;
7ee85ab1
DE
6920
6921 /* We can get here with the TU already read, *or* in the process of being
6aa5f3a6
DE
6922 read. Don't reassign the global entry to point to this DWO if that's
6923 the case. Also note that if the TU is already being read, it may not
6924 have come from a DWO, the program may be a mix of Fission-compiled
6925 code and non-Fission-compiled code. */
6926
6927 /* Have we already tried to read this TU?
6928 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
6929 needn't exist in the global table yet). */
6930 if (sig_entry != NULL && sig_entry->per_cu.tu_read)
a2ce51a0
DE
6931 return sig_entry;
6932
6aa5f3a6
DE
6933 /* Note: cu->dwo_unit is the dwo_unit that references this TU, not the
6934 dwo_unit of the TU itself. */
6935 dwo_file = cu->dwo_unit->dwo_file;
6936
a2ce51a0
DE
6937 /* Ok, this is the first time we're reading this TU. */
6938 if (dwo_file->tus == NULL)
6939 return NULL;
6940 find_dwo_entry.signature = sig;
9a3c8263 6941 dwo_entry = (struct dwo_unit *) htab_find (dwo_file->tus, &find_dwo_entry);
a2ce51a0
DE
6942 if (dwo_entry == NULL)
6943 return NULL;
6944
6aa5f3a6
DE
6945 /* If the global table doesn't have an entry for this TU, add one. */
6946 if (sig_entry == NULL)
ed2dc618 6947 sig_entry = add_type_unit (dwarf2_per_objfile, sig, slot);
6aa5f3a6 6948
ed2dc618 6949 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, sig_entry, dwo_entry);
89e63ee4 6950 sig_entry->per_cu.tu_read = 1;
a2ce51a0
DE
6951 return sig_entry;
6952}
6953
a2ce51a0
DE
6954/* Subroutine of lookup_signatured_type.
6955 Look up the type for signature SIG, and if we can't find SIG in .gdb_index
6aa5f3a6
DE
6956 then try the DWP file. If the TU stub (skeleton) has been removed then
6957 it won't be in .gdb_index. */
a2ce51a0
DE
6958
6959static struct signatured_type *
6960lookup_dwp_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6961{
518817b3
SM
6962 struct dwarf2_per_objfile *dwarf2_per_objfile
6963 = cu->per_cu->dwarf2_per_objfile;
a2ce51a0 6964 struct objfile *objfile = dwarf2_per_objfile->objfile;
ed2dc618 6965 struct dwp_file *dwp_file = get_dwp_file (dwarf2_per_objfile);
a2ce51a0
DE
6966 struct dwo_unit *dwo_entry;
6967 struct signatured_type find_sig_entry, *sig_entry;
6aa5f3a6 6968 void **slot;
a2ce51a0
DE
6969
6970 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
6971 gdb_assert (dwp_file != NULL);
6972
6aa5f3a6
DE
6973 /* If TU skeletons have been removed then we may not have read in any
6974 TUs yet. */
6975 if (dwarf2_per_objfile->signatured_types == NULL)
a2ce51a0 6976 {
6aa5f3a6
DE
6977 dwarf2_per_objfile->signatured_types
6978 = allocate_signatured_type_table (objfile);
a2ce51a0
DE
6979 }
6980
6aa5f3a6
DE
6981 find_sig_entry.signature = sig;
6982 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
6983 &find_sig_entry, INSERT);
9a3c8263 6984 sig_entry = (struct signatured_type *) *slot;
6aa5f3a6
DE
6985
6986 /* Have we already tried to read this TU?
6987 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
6988 needn't exist in the global table yet). */
6989 if (sig_entry != NULL)
6990 return sig_entry;
6991
a2ce51a0
DE
6992 if (dwp_file->tus == NULL)
6993 return NULL;
ed2dc618 6994 dwo_entry = lookup_dwo_unit_in_dwp (dwarf2_per_objfile, dwp_file, NULL,
57d63ce2 6995 sig, 1 /* is_debug_types */);
a2ce51a0
DE
6996 if (dwo_entry == NULL)
6997 return NULL;
6998
ed2dc618
SM
6999 sig_entry = add_type_unit (dwarf2_per_objfile, sig, slot);
7000 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, sig_entry, dwo_entry);
a2ce51a0 7001
a2ce51a0
DE
7002 return sig_entry;
7003}
7004
380bca97 7005/* Lookup a signature based type for DW_FORM_ref_sig8.
5a8b3f62
DE
7006 Returns NULL if signature SIG is not present in the table.
7007 It is up to the caller to complain about this. */
348e048f
DE
7008
7009static struct signatured_type *
a2ce51a0 7010lookup_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
348e048f 7011{
518817b3
SM
7012 struct dwarf2_per_objfile *dwarf2_per_objfile
7013 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 7014
a2ce51a0
DE
7015 if (cu->dwo_unit
7016 && dwarf2_per_objfile->using_index)
7017 {
7018 /* We're in a DWO/DWP file, and we're using .gdb_index.
7019 These cases require special processing. */
ed2dc618 7020 if (get_dwp_file (dwarf2_per_objfile) == NULL)
a2ce51a0
DE
7021 return lookup_dwo_signatured_type (cu, sig);
7022 else
7023 return lookup_dwp_signatured_type (cu, sig);
7024 }
7025 else
7026 {
7027 struct signatured_type find_entry, *entry;
348e048f 7028
a2ce51a0
DE
7029 if (dwarf2_per_objfile->signatured_types == NULL)
7030 return NULL;
7031 find_entry.signature = sig;
9a3c8263
SM
7032 entry = ((struct signatured_type *)
7033 htab_find (dwarf2_per_objfile->signatured_types, &find_entry));
a2ce51a0
DE
7034 return entry;
7035 }
348e048f 7036}
42e7ad6c
DE
7037\f
7038/* Low level DIE reading support. */
348e048f 7039
d85a05f0
DJ
7040/* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
7041
7042static void
7043init_cu_die_reader (struct die_reader_specs *reader,
dee91e82 7044 struct dwarf2_cu *cu,
3019eac3 7045 struct dwarf2_section_info *section,
685af9cd
TT
7046 struct dwo_file *dwo_file,
7047 struct abbrev_table *abbrev_table)
d85a05f0 7048{
fceca515 7049 gdb_assert (section->readin && section->buffer != NULL);
a32a8923 7050 reader->abfd = get_section_bfd_owner (section);
d85a05f0 7051 reader->cu = cu;
3019eac3 7052 reader->dwo_file = dwo_file;
dee91e82
DE
7053 reader->die_section = section;
7054 reader->buffer = section->buffer;
f664829e 7055 reader->buffer_end = section->buffer + section->size;
a2ce51a0 7056 reader->comp_dir = NULL;
685af9cd 7057 reader->abbrev_table = abbrev_table;
d85a05f0
DJ
7058}
7059
b0c7bfa9
DE
7060/* Subroutine of init_cutu_and_read_dies to simplify it.
7061 Read in the rest of a CU/TU top level DIE from DWO_UNIT.
7062 There's just a lot of work to do, and init_cutu_and_read_dies is big enough
7063 already.
7064
7065 STUB_COMP_UNIT_DIE is for the stub DIE, we copy over certain attributes
7066 from it to the DIE in the DWO. If NULL we are skipping the stub.
a2ce51a0
DE
7067 STUB_COMP_DIR is similar to STUB_COMP_UNIT_DIE: When reading a TU directly
7068 from the DWO file, bypassing the stub, it contains the DW_AT_comp_dir
c54a1dd8
DE
7069 attribute of the referencing CU. At most one of STUB_COMP_UNIT_DIE and
7070 STUB_COMP_DIR may be non-NULL.
b0c7bfa9
DE
7071 *RESULT_READER,*RESULT_INFO_PTR,*RESULT_COMP_UNIT_DIE,*RESULT_HAS_CHILDREN
7072 are filled in with the info of the DIE from the DWO file.
685af9cd
TT
7073 *RESULT_DWO_ABBREV_TABLE will be filled in with the abbrev table allocated
7074 from the dwo. Since *RESULT_READER references this abbrev table, it must be
7075 kept around for at least as long as *RESULT_READER.
7076
b0c7bfa9
DE
7077 The result is non-zero if a valid (non-dummy) DIE was found. */
7078
7079static int
7080read_cutu_die_from_dwo (struct dwarf2_per_cu_data *this_cu,
7081 struct dwo_unit *dwo_unit,
b0c7bfa9 7082 struct die_info *stub_comp_unit_die,
a2ce51a0 7083 const char *stub_comp_dir,
b0c7bfa9 7084 struct die_reader_specs *result_reader,
d521ce57 7085 const gdb_byte **result_info_ptr,
b0c7bfa9 7086 struct die_info **result_comp_unit_die,
685af9cd
TT
7087 int *result_has_children,
7088 abbrev_table_up *result_dwo_abbrev_table)
b0c7bfa9 7089{
ed2dc618 7090 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
b0c7bfa9
DE
7091 struct objfile *objfile = dwarf2_per_objfile->objfile;
7092 struct dwarf2_cu *cu = this_cu->cu;
b0c7bfa9 7093 bfd *abfd;
d521ce57 7094 const gdb_byte *begin_info_ptr, *info_ptr;
b0c7bfa9
DE
7095 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
7096 int i,num_extra_attrs;
7097 struct dwarf2_section_info *dwo_abbrev_section;
7098 struct attribute *attr;
7099 struct die_info *comp_unit_die;
7100
b0aeadb3
DE
7101 /* At most one of these may be provided. */
7102 gdb_assert ((stub_comp_unit_die != NULL) + (stub_comp_dir != NULL) <= 1);
a2ce51a0 7103
b0c7bfa9
DE
7104 /* These attributes aren't processed until later:
7105 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
0d60c288
DE
7106 DW_AT_comp_dir is used now, to find the DWO file, but it is also
7107 referenced later. However, these attributes are found in the stub
7108 which we won't have later. In order to not impose this complication
7109 on the rest of the code, we read them here and copy them to the
7110 DWO CU/TU die. */
b0c7bfa9
DE
7111
7112 stmt_list = NULL;
7113 low_pc = NULL;
7114 high_pc = NULL;
7115 ranges = NULL;
7116 comp_dir = NULL;
7117
7118 if (stub_comp_unit_die != NULL)
7119 {
7120 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
7121 DWO file. */
7122 if (! this_cu->is_debug_types)
7123 stmt_list = dwarf2_attr (stub_comp_unit_die, DW_AT_stmt_list, cu);
7124 low_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_low_pc, cu);
7125 high_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_high_pc, cu);
7126 ranges = dwarf2_attr (stub_comp_unit_die, DW_AT_ranges, cu);
7127 comp_dir = dwarf2_attr (stub_comp_unit_die, DW_AT_comp_dir, cu);
7128
7129 /* There should be a DW_AT_addr_base attribute here (if needed).
7130 We need the value before we can process DW_FORM_GNU_addr_index. */
7131 cu->addr_base = 0;
7132 attr = dwarf2_attr (stub_comp_unit_die, DW_AT_GNU_addr_base, cu);
7133 if (attr)
7134 cu->addr_base = DW_UNSND (attr);
7135
7136 /* There should be a DW_AT_ranges_base attribute here (if needed).
7137 We need the value before we can process DW_AT_ranges. */
7138 cu->ranges_base = 0;
7139 attr = dwarf2_attr (stub_comp_unit_die, DW_AT_GNU_ranges_base, cu);
7140 if (attr)
7141 cu->ranges_base = DW_UNSND (attr);
7142 }
a2ce51a0
DE
7143 else if (stub_comp_dir != NULL)
7144 {
7145 /* Reconstruct the comp_dir attribute to simplify the code below. */
8d749320 7146 comp_dir = XOBNEW (&cu->comp_unit_obstack, struct attribute);
a2ce51a0
DE
7147 comp_dir->name = DW_AT_comp_dir;
7148 comp_dir->form = DW_FORM_string;
7149 DW_STRING_IS_CANONICAL (comp_dir) = 0;
7150 DW_STRING (comp_dir) = stub_comp_dir;
7151 }
b0c7bfa9
DE
7152
7153 /* Set up for reading the DWO CU/TU. */
7154 cu->dwo_unit = dwo_unit;
685af9cd 7155 dwarf2_section_info *section = dwo_unit->section;
b0c7bfa9 7156 dwarf2_read_section (objfile, section);
a32a8923 7157 abfd = get_section_bfd_owner (section);
9c541725
PA
7158 begin_info_ptr = info_ptr = (section->buffer
7159 + to_underlying (dwo_unit->sect_off));
b0c7bfa9 7160 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
b0c7bfa9
DE
7161
7162 if (this_cu->is_debug_types)
7163 {
b0c7bfa9
DE
7164 struct signatured_type *sig_type = (struct signatured_type *) this_cu;
7165
ed2dc618
SM
7166 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7167 &cu->header, section,
b0c7bfa9 7168 dwo_abbrev_section,
43988095 7169 info_ptr, rcuh_kind::TYPE);
a2ce51a0 7170 /* This is not an assert because it can be caused by bad debug info. */
43988095 7171 if (sig_type->signature != cu->header.signature)
a2ce51a0
DE
7172 {
7173 error (_("Dwarf Error: signature mismatch %s vs %s while reading"
9d8780f0 7174 " TU at offset %s [in module %s]"),
a2ce51a0 7175 hex_string (sig_type->signature),
43988095 7176 hex_string (cu->header.signature),
9d8780f0 7177 sect_offset_str (dwo_unit->sect_off),
a2ce51a0
DE
7178 bfd_get_filename (abfd));
7179 }
9c541725 7180 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
b0c7bfa9
DE
7181 /* For DWOs coming from DWP files, we don't know the CU length
7182 nor the type's offset in the TU until now. */
7183 dwo_unit->length = get_cu_length (&cu->header);
9c541725 7184 dwo_unit->type_offset_in_tu = cu->header.type_cu_offset_in_tu;
b0c7bfa9
DE
7185
7186 /* Establish the type offset that can be used to lookup the type.
7187 For DWO files, we don't know it until now. */
9c541725
PA
7188 sig_type->type_offset_in_section
7189 = dwo_unit->sect_off + to_underlying (dwo_unit->type_offset_in_tu);
b0c7bfa9
DE
7190 }
7191 else
7192 {
ed2dc618
SM
7193 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7194 &cu->header, section,
b0c7bfa9 7195 dwo_abbrev_section,
43988095 7196 info_ptr, rcuh_kind::COMPILE);
9c541725 7197 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
b0c7bfa9
DE
7198 /* For DWOs coming from DWP files, we don't know the CU length
7199 until now. */
7200 dwo_unit->length = get_cu_length (&cu->header);
7201 }
7202
685af9cd
TT
7203 *result_dwo_abbrev_table
7204 = abbrev_table_read_table (dwarf2_per_objfile, dwo_abbrev_section,
7205 cu->header.abbrev_sect_off);
7206 init_cu_die_reader (result_reader, cu, section, dwo_unit->dwo_file,
7207 result_dwo_abbrev_table->get ());
b0c7bfa9
DE
7208
7209 /* Read in the die, but leave space to copy over the attributes
7210 from the stub. This has the benefit of simplifying the rest of
7211 the code - all the work to maintain the illusion of a single
7212 DW_TAG_{compile,type}_unit DIE is done here. */
7213 num_extra_attrs = ((stmt_list != NULL)
7214 + (low_pc != NULL)
7215 + (high_pc != NULL)
7216 + (ranges != NULL)
7217 + (comp_dir != NULL));
7218 info_ptr = read_full_die_1 (result_reader, result_comp_unit_die, info_ptr,
7219 result_has_children, num_extra_attrs);
7220
7221 /* Copy over the attributes from the stub to the DIE we just read in. */
7222 comp_unit_die = *result_comp_unit_die;
7223 i = comp_unit_die->num_attrs;
7224 if (stmt_list != NULL)
7225 comp_unit_die->attrs[i++] = *stmt_list;
7226 if (low_pc != NULL)
7227 comp_unit_die->attrs[i++] = *low_pc;
7228 if (high_pc != NULL)
7229 comp_unit_die->attrs[i++] = *high_pc;
7230 if (ranges != NULL)
7231 comp_unit_die->attrs[i++] = *ranges;
7232 if (comp_dir != NULL)
7233 comp_unit_die->attrs[i++] = *comp_dir;
7234 comp_unit_die->num_attrs += num_extra_attrs;
7235
b4f54984 7236 if (dwarf_die_debug)
bf6af496
DE
7237 {
7238 fprintf_unfiltered (gdb_stdlog,
7239 "Read die from %s@0x%x of %s:\n",
a32a8923 7240 get_section_name (section),
bf6af496
DE
7241 (unsigned) (begin_info_ptr - section->buffer),
7242 bfd_get_filename (abfd));
b4f54984 7243 dump_die (comp_unit_die, dwarf_die_debug);
bf6af496
DE
7244 }
7245
a2ce51a0
DE
7246 /* Save the comp_dir attribute. If there is no DWP file then we'll read
7247 TUs by skipping the stub and going directly to the entry in the DWO file.
7248 However, skipping the stub means we won't get DW_AT_comp_dir, so we have
7249 to get it via circuitous means. Blech. */
7250 if (comp_dir != NULL)
7251 result_reader->comp_dir = DW_STRING (comp_dir);
7252
b0c7bfa9
DE
7253 /* Skip dummy compilation units. */
7254 if (info_ptr >= begin_info_ptr + dwo_unit->length
7255 || peek_abbrev_code (abfd, info_ptr) == 0)
7256 return 0;
7257
7258 *result_info_ptr = info_ptr;
7259 return 1;
7260}
7261
7262/* Subroutine of init_cutu_and_read_dies to simplify it.
7263 Look up the DWO unit specified by COMP_UNIT_DIE of THIS_CU.
6a506a2d 7264 Returns NULL if the specified DWO unit cannot be found. */
b0c7bfa9
DE
7265
7266static struct dwo_unit *
7267lookup_dwo_unit (struct dwarf2_per_cu_data *this_cu,
7268 struct die_info *comp_unit_die)
7269{
7270 struct dwarf2_cu *cu = this_cu->cu;
b0c7bfa9
DE
7271 ULONGEST signature;
7272 struct dwo_unit *dwo_unit;
7273 const char *comp_dir, *dwo_name;
7274
a2ce51a0
DE
7275 gdb_assert (cu != NULL);
7276
b0c7bfa9 7277 /* Yeah, we look dwo_name up again, but it simplifies the code. */
7d45c7c3
KB
7278 dwo_name = dwarf2_string_attr (comp_unit_die, DW_AT_GNU_dwo_name, cu);
7279 comp_dir = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
b0c7bfa9
DE
7280
7281 if (this_cu->is_debug_types)
7282 {
7283 struct signatured_type *sig_type;
7284
7285 /* Since this_cu is the first member of struct signatured_type,
7286 we can go from a pointer to one to a pointer to the other. */
7287 sig_type = (struct signatured_type *) this_cu;
7288 signature = sig_type->signature;
7289 dwo_unit = lookup_dwo_type_unit (sig_type, dwo_name, comp_dir);
7290 }
7291 else
7292 {
7293 struct attribute *attr;
7294
7295 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
7296 if (! attr)
7297 error (_("Dwarf Error: missing dwo_id for dwo_name %s"
7298 " [in module %s]"),
e3b94546 7299 dwo_name, objfile_name (this_cu->dwarf2_per_objfile->objfile));
b0c7bfa9
DE
7300 signature = DW_UNSND (attr);
7301 dwo_unit = lookup_dwo_comp_unit (this_cu, dwo_name, comp_dir,
7302 signature);
7303 }
7304
b0c7bfa9
DE
7305 return dwo_unit;
7306}
7307
a2ce51a0 7308/* Subroutine of init_cutu_and_read_dies to simplify it.
6aa5f3a6 7309 See it for a description of the parameters.
fcd3b13d 7310 Read a TU directly from a DWO file, bypassing the stub. */
a2ce51a0
DE
7311
7312static void
6aa5f3a6
DE
7313init_tu_and_read_dwo_dies (struct dwarf2_per_cu_data *this_cu,
7314 int use_existing_cu, int keep,
a2ce51a0
DE
7315 die_reader_func_ftype *die_reader_func,
7316 void *data)
7317{
fcd3b13d 7318 std::unique_ptr<dwarf2_cu> new_cu;
a2ce51a0 7319 struct signatured_type *sig_type;
a2ce51a0
DE
7320 struct die_reader_specs reader;
7321 const gdb_byte *info_ptr;
7322 struct die_info *comp_unit_die;
7323 int has_children;
ed2dc618 7324 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
a2ce51a0
DE
7325
7326 /* Verify we can do the following downcast, and that we have the
7327 data we need. */
7328 gdb_assert (this_cu->is_debug_types && this_cu->reading_dwo_directly);
7329 sig_type = (struct signatured_type *) this_cu;
7330 gdb_assert (sig_type->dwo_unit != NULL);
7331
6aa5f3a6
DE
7332 if (use_existing_cu && this_cu->cu != NULL)
7333 {
7334 gdb_assert (this_cu->cu->dwo_unit == sig_type->dwo_unit);
6aa5f3a6
DE
7335 /* There's no need to do the rereading_dwo_cu handling that
7336 init_cutu_and_read_dies does since we don't read the stub. */
7337 }
7338 else
7339 {
7340 /* If !use_existing_cu, this_cu->cu must be NULL. */
7341 gdb_assert (this_cu->cu == NULL);
fcd3b13d 7342 new_cu.reset (new dwarf2_cu (this_cu));
6aa5f3a6
DE
7343 }
7344
7345 /* A future optimization, if needed, would be to use an existing
7346 abbrev table. When reading DWOs with skeletonless TUs, all the TUs
7347 could share abbrev tables. */
a2ce51a0 7348
685af9cd
TT
7349 /* The abbreviation table used by READER, this must live at least as long as
7350 READER. */
7351 abbrev_table_up dwo_abbrev_table;
7352
a2ce51a0 7353 if (read_cutu_die_from_dwo (this_cu, sig_type->dwo_unit,
a2ce51a0
DE
7354 NULL /* stub_comp_unit_die */,
7355 sig_type->dwo_unit->dwo_file->comp_dir,
7356 &reader, &info_ptr,
685af9cd
TT
7357 &comp_unit_die, &has_children,
7358 &dwo_abbrev_table) == 0)
a2ce51a0
DE
7359 {
7360 /* Dummy die. */
a2ce51a0
DE
7361 return;
7362 }
7363
7364 /* All the "real" work is done here. */
7365 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
7366
6aa5f3a6 7367 /* This duplicates the code in init_cutu_and_read_dies,
a2ce51a0
DE
7368 but the alternative is making the latter more complex.
7369 This function is only for the special case of using DWO files directly:
7370 no point in overly complicating the general case just to handle this. */
fcd3b13d 7371 if (new_cu != NULL && keep)
a2ce51a0 7372 {
fcd3b13d
SM
7373 /* Link this CU into read_in_chain. */
7374 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
7375 dwarf2_per_objfile->read_in_chain = this_cu;
7376 /* The chain owns it now. */
7377 new_cu.release ();
a2ce51a0 7378 }
a2ce51a0
DE
7379}
7380
fd820528 7381/* Initialize a CU (or TU) and read its DIEs.
3019eac3 7382 If the CU defers to a DWO file, read the DWO file as well.
dee91e82 7383
f4dc4d17
DE
7384 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
7385 Otherwise the table specified in the comp unit header is read in and used.
7386 This is an optimization for when we already have the abbrev table.
7387
dee91e82
DE
7388 If USE_EXISTING_CU is non-zero, and THIS_CU->cu is non-NULL, then use it.
7389 Otherwise, a new CU is allocated with xmalloc.
7390
7391 If KEEP is non-zero, then if we allocated a dwarf2_cu we add it to
7392 read_in_chain. Otherwise the dwarf2_cu data is freed at the end.
7393
7394 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
fd820528 7395 linker) then DIE_READER_FUNC will not get called. */
aaa75496 7396
70221824 7397static void
fd820528 7398init_cutu_and_read_dies (struct dwarf2_per_cu_data *this_cu,
f4dc4d17 7399 struct abbrev_table *abbrev_table,
fd820528 7400 int use_existing_cu, int keep,
58f0c718 7401 bool skip_partial,
fd820528
DE
7402 die_reader_func_ftype *die_reader_func,
7403 void *data)
c906108c 7404{
ed2dc618 7405 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
dee91e82 7406 struct objfile *objfile = dwarf2_per_objfile->objfile;
8a0459fd 7407 struct dwarf2_section_info *section = this_cu->section;
a32a8923 7408 bfd *abfd = get_section_bfd_owner (section);
dee91e82 7409 struct dwarf2_cu *cu;
d521ce57 7410 const gdb_byte *begin_info_ptr, *info_ptr;
dee91e82 7411 struct die_reader_specs reader;
d85a05f0 7412 struct die_info *comp_unit_die;
dee91e82 7413 int has_children;
d85a05f0 7414 struct attribute *attr;
dee91e82 7415 struct signatured_type *sig_type = NULL;
4bdcc0c1 7416 struct dwarf2_section_info *abbrev_section;
42e7ad6c
DE
7417 /* Non-zero if CU currently points to a DWO file and we need to
7418 reread it. When this happens we need to reread the skeleton die
a2ce51a0 7419 before we can reread the DWO file (this only applies to CUs, not TUs). */
42e7ad6c 7420 int rereading_dwo_cu = 0;
c906108c 7421
b4f54984 7422 if (dwarf_die_debug)
9d8780f0 7423 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
09406207 7424 this_cu->is_debug_types ? "type" : "comp",
9d8780f0 7425 sect_offset_str (this_cu->sect_off));
09406207 7426
dee91e82
DE
7427 if (use_existing_cu)
7428 gdb_assert (keep);
23745b47 7429
a2ce51a0
DE
7430 /* If we're reading a TU directly from a DWO file, including a virtual DWO
7431 file (instead of going through the stub), short-circuit all of this. */
7432 if (this_cu->reading_dwo_directly)
7433 {
7434 /* Narrow down the scope of possibilities to have to understand. */
7435 gdb_assert (this_cu->is_debug_types);
7436 gdb_assert (abbrev_table == NULL);
6aa5f3a6
DE
7437 init_tu_and_read_dwo_dies (this_cu, use_existing_cu, keep,
7438 die_reader_func, data);
a2ce51a0
DE
7439 return;
7440 }
7441
dee91e82
DE
7442 /* This is cheap if the section is already read in. */
7443 dwarf2_read_section (objfile, section);
7444
9c541725 7445 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
36586728
TT
7446
7447 abbrev_section = get_abbrev_section_for_cu (this_cu);
dee91e82 7448
fcd3b13d 7449 std::unique_ptr<dwarf2_cu> new_cu;
dee91e82
DE
7450 if (use_existing_cu && this_cu->cu != NULL)
7451 {
7452 cu = this_cu->cu;
42e7ad6c
DE
7453 /* If this CU is from a DWO file we need to start over, we need to
7454 refetch the attributes from the skeleton CU.
7455 This could be optimized by retrieving those attributes from when we
7456 were here the first time: the previous comp_unit_die was stored in
7457 comp_unit_obstack. But there's no data yet that we need this
7458 optimization. */
7459 if (cu->dwo_unit != NULL)
7460 rereading_dwo_cu = 1;
dee91e82
DE
7461 }
7462 else
7463 {
7464 /* If !use_existing_cu, this_cu->cu must be NULL. */
7465 gdb_assert (this_cu->cu == NULL);
fcd3b13d
SM
7466 new_cu.reset (new dwarf2_cu (this_cu));
7467 cu = new_cu.get ();
42e7ad6c 7468 }
dee91e82 7469
b0c7bfa9 7470 /* Get the header. */
9c541725 7471 if (to_underlying (cu->header.first_die_cu_offset) != 0 && !rereading_dwo_cu)
42e7ad6c
DE
7472 {
7473 /* We already have the header, there's no need to read it in again. */
9c541725 7474 info_ptr += to_underlying (cu->header.first_die_cu_offset);
42e7ad6c
DE
7475 }
7476 else
7477 {
3019eac3 7478 if (this_cu->is_debug_types)
dee91e82 7479 {
ed2dc618
SM
7480 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7481 &cu->header, section,
4bdcc0c1 7482 abbrev_section, info_ptr,
43988095 7483 rcuh_kind::TYPE);
dee91e82 7484
42e7ad6c
DE
7485 /* Since per_cu is the first member of struct signatured_type,
7486 we can go from a pointer to one to a pointer to the other. */
7487 sig_type = (struct signatured_type *) this_cu;
43988095 7488 gdb_assert (sig_type->signature == cu->header.signature);
9c541725
PA
7489 gdb_assert (sig_type->type_offset_in_tu
7490 == cu->header.type_cu_offset_in_tu);
7491 gdb_assert (this_cu->sect_off == cu->header.sect_off);
dee91e82 7492
42e7ad6c
DE
7493 /* LENGTH has not been set yet for type units if we're
7494 using .gdb_index. */
1ce1cefd 7495 this_cu->length = get_cu_length (&cu->header);
3019eac3
DE
7496
7497 /* Establish the type offset that can be used to lookup the type. */
9c541725
PA
7498 sig_type->type_offset_in_section =
7499 this_cu->sect_off + to_underlying (sig_type->type_offset_in_tu);
43988095
JK
7500
7501 this_cu->dwarf_version = cu->header.version;
dee91e82
DE
7502 }
7503 else
7504 {
ed2dc618
SM
7505 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7506 &cu->header, section,
4bdcc0c1 7507 abbrev_section,
43988095
JK
7508 info_ptr,
7509 rcuh_kind::COMPILE);
dee91e82 7510
9c541725 7511 gdb_assert (this_cu->sect_off == cu->header.sect_off);
1ce1cefd 7512 gdb_assert (this_cu->length == get_cu_length (&cu->header));
43988095 7513 this_cu->dwarf_version = cu->header.version;
dee91e82
DE
7514 }
7515 }
10b3939b 7516
6caca83c 7517 /* Skip dummy compilation units. */
dee91e82 7518 if (info_ptr >= begin_info_ptr + this_cu->length
6caca83c 7519 || peek_abbrev_code (abfd, info_ptr) == 0)
fcd3b13d 7520 return;
6caca83c 7521
433df2d4
DE
7522 /* If we don't have them yet, read the abbrevs for this compilation unit.
7523 And if we need to read them now, make sure they're freed when we're
685af9cd
TT
7524 done (own the table through ABBREV_TABLE_HOLDER). */
7525 abbrev_table_up abbrev_table_holder;
f4dc4d17 7526 if (abbrev_table != NULL)
685af9cd
TT
7527 gdb_assert (cu->header.abbrev_sect_off == abbrev_table->sect_off);
7528 else
f4dc4d17 7529 {
685af9cd
TT
7530 abbrev_table_holder
7531 = abbrev_table_read_table (dwarf2_per_objfile, abbrev_section,
7532 cu->header.abbrev_sect_off);
7533 abbrev_table = abbrev_table_holder.get ();
42e7ad6c 7534 }
af703f96 7535
dee91e82 7536 /* Read the top level CU/TU die. */
685af9cd 7537 init_cu_die_reader (&reader, cu, section, NULL, abbrev_table);
dee91e82 7538 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
93311388 7539
58f0c718
TT
7540 if (skip_partial && comp_unit_die->tag == DW_TAG_partial_unit)
7541 return;
7542
b0c7bfa9 7543 /* If we are in a DWO stub, process it and then read in the "real" CU/TU
685af9cd
TT
7544 from the DWO file. read_cutu_die_from_dwo will allocate the abbreviation
7545 table from the DWO file and pass the ownership over to us. It will be
7546 referenced from READER, so we must make sure to free it after we're done
7547 with READER.
7548
b0c7bfa9
DE
7549 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains a
7550 DWO CU, that this test will fail (the attribute will not be present). */
3019eac3 7551 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_name, cu);
685af9cd 7552 abbrev_table_up dwo_abbrev_table;
3019eac3
DE
7553 if (attr)
7554 {
3019eac3 7555 struct dwo_unit *dwo_unit;
b0c7bfa9 7556 struct die_info *dwo_comp_unit_die;
3019eac3
DE
7557
7558 if (has_children)
6a506a2d 7559 {
b98664d3 7560 complaint (_("compilation unit with DW_AT_GNU_dwo_name"
9d8780f0
SM
7561 " has children (offset %s) [in module %s]"),
7562 sect_offset_str (this_cu->sect_off),
7563 bfd_get_filename (abfd));
6a506a2d 7564 }
b0c7bfa9 7565 dwo_unit = lookup_dwo_unit (this_cu, comp_unit_die);
6a506a2d 7566 if (dwo_unit != NULL)
3019eac3 7567 {
6a506a2d 7568 if (read_cutu_die_from_dwo (this_cu, dwo_unit,
a2ce51a0 7569 comp_unit_die, NULL,
6a506a2d 7570 &reader, &info_ptr,
685af9cd
TT
7571 &dwo_comp_unit_die, &has_children,
7572 &dwo_abbrev_table) == 0)
6a506a2d
DE
7573 {
7574 /* Dummy die. */
6a506a2d
DE
7575 return;
7576 }
7577 comp_unit_die = dwo_comp_unit_die;
7578 }
7579 else
7580 {
7581 /* Yikes, we couldn't find the rest of the DIE, we only have
7582 the stub. A complaint has already been logged. There's
7583 not much more we can do except pass on the stub DIE to
7584 die_reader_func. We don't want to throw an error on bad
7585 debug info. */
3019eac3
DE
7586 }
7587 }
7588
b0c7bfa9 7589 /* All of the above is setup for this call. Yikes. */
dee91e82
DE
7590 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
7591
b0c7bfa9 7592 /* Done, clean up. */
fcd3b13d 7593 if (new_cu != NULL && keep)
348e048f 7594 {
fcd3b13d
SM
7595 /* Link this CU into read_in_chain. */
7596 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
7597 dwarf2_per_objfile->read_in_chain = this_cu;
7598 /* The chain owns it now. */
7599 new_cu.release ();
348e048f 7600 }
dee91e82
DE
7601}
7602
33e80786
DE
7603/* Read CU/TU THIS_CU but do not follow DW_AT_GNU_dwo_name if present.
7604 DWO_FILE, if non-NULL, is the DWO file to read (the caller is assumed
7605 to have already done the lookup to find the DWO file).
dee91e82
DE
7606
7607 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
3019eac3 7608 THIS_CU->is_debug_types, but nothing else.
dee91e82
DE
7609
7610 We fill in THIS_CU->length.
7611
7612 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
7613 linker) then DIE_READER_FUNC will not get called.
7614
7615 THIS_CU->cu is always freed when done.
3019eac3
DE
7616 This is done in order to not leave THIS_CU->cu in a state where we have
7617 to care whether it refers to the "main" CU or the DWO CU. */
dee91e82
DE
7618
7619static void
7620init_cutu_and_read_dies_no_follow (struct dwarf2_per_cu_data *this_cu,
3019eac3 7621 struct dwo_file *dwo_file,
dee91e82
DE
7622 die_reader_func_ftype *die_reader_func,
7623 void *data)
7624{
ed2dc618 7625 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
dee91e82 7626 struct objfile *objfile = dwarf2_per_objfile->objfile;
8a0459fd 7627 struct dwarf2_section_info *section = this_cu->section;
a32a8923 7628 bfd *abfd = get_section_bfd_owner (section);
33e80786 7629 struct dwarf2_section_info *abbrev_section;
d521ce57 7630 const gdb_byte *begin_info_ptr, *info_ptr;
dee91e82 7631 struct die_reader_specs reader;
dee91e82
DE
7632 struct die_info *comp_unit_die;
7633 int has_children;
7634
b4f54984 7635 if (dwarf_die_debug)
9d8780f0 7636 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
09406207 7637 this_cu->is_debug_types ? "type" : "comp",
9d8780f0 7638 sect_offset_str (this_cu->sect_off));
09406207 7639
dee91e82
DE
7640 gdb_assert (this_cu->cu == NULL);
7641
33e80786
DE
7642 abbrev_section = (dwo_file != NULL
7643 ? &dwo_file->sections.abbrev
7644 : get_abbrev_section_for_cu (this_cu));
7645
dee91e82
DE
7646 /* This is cheap if the section is already read in. */
7647 dwarf2_read_section (objfile, section);
7648
fcd3b13d 7649 struct dwarf2_cu cu (this_cu);
dee91e82 7650
9c541725 7651 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
ed2dc618
SM
7652 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7653 &cu.header, section,
4bdcc0c1 7654 abbrev_section, info_ptr,
43988095
JK
7655 (this_cu->is_debug_types
7656 ? rcuh_kind::TYPE
7657 : rcuh_kind::COMPILE));
dee91e82 7658
1ce1cefd 7659 this_cu->length = get_cu_length (&cu.header);
dee91e82
DE
7660
7661 /* Skip dummy compilation units. */
7662 if (info_ptr >= begin_info_ptr + this_cu->length
7663 || peek_abbrev_code (abfd, info_ptr) == 0)
fcd3b13d 7664 return;
72bf9492 7665
685af9cd
TT
7666 abbrev_table_up abbrev_table
7667 = abbrev_table_read_table (dwarf2_per_objfile, abbrev_section,
7668 cu.header.abbrev_sect_off);
dee91e82 7669
685af9cd 7670 init_cu_die_reader (&reader, &cu, section, dwo_file, abbrev_table.get ());
dee91e82
DE
7671 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
7672
7673 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
dee91e82
DE
7674}
7675
3019eac3
DE
7676/* Read a CU/TU, except that this does not look for DW_AT_GNU_dwo_name and
7677 does not lookup the specified DWO file.
7678 This cannot be used to read DWO files.
dee91e82
DE
7679
7680 THIS_CU->cu is always freed when done.
3019eac3
DE
7681 This is done in order to not leave THIS_CU->cu in a state where we have
7682 to care whether it refers to the "main" CU or the DWO CU.
7683 We can revisit this if the data shows there's a performance issue. */
dee91e82
DE
7684
7685static void
7686init_cutu_and_read_dies_simple (struct dwarf2_per_cu_data *this_cu,
7687 die_reader_func_ftype *die_reader_func,
7688 void *data)
7689{
33e80786 7690 init_cutu_and_read_dies_no_follow (this_cu, NULL, die_reader_func, data);
dee91e82 7691}
0018ea6f
DE
7692\f
7693/* Type Unit Groups.
dee91e82 7694
0018ea6f
DE
7695 Type Unit Groups are a way to collapse the set of all TUs (type units) into
7696 a more manageable set. The grouping is done by DW_AT_stmt_list entry
7697 so that all types coming from the same compilation (.o file) are grouped
7698 together. A future step could be to put the types in the same symtab as
7699 the CU the types ultimately came from. */
ff013f42 7700
f4dc4d17
DE
7701static hashval_t
7702hash_type_unit_group (const void *item)
7703{
9a3c8263
SM
7704 const struct type_unit_group *tu_group
7705 = (const struct type_unit_group *) item;
f4dc4d17 7706
094b34ac 7707 return hash_stmt_list_entry (&tu_group->hash);
f4dc4d17 7708}
348e048f
DE
7709
7710static int
f4dc4d17 7711eq_type_unit_group (const void *item_lhs, const void *item_rhs)
348e048f 7712{
9a3c8263
SM
7713 const struct type_unit_group *lhs = (const struct type_unit_group *) item_lhs;
7714 const struct type_unit_group *rhs = (const struct type_unit_group *) item_rhs;
348e048f 7715
094b34ac 7716 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
f4dc4d17 7717}
348e048f 7718
f4dc4d17
DE
7719/* Allocate a hash table for type unit groups. */
7720
7721static htab_t
ed2dc618 7722allocate_type_unit_groups_table (struct objfile *objfile)
f4dc4d17
DE
7723{
7724 return htab_create_alloc_ex (3,
7725 hash_type_unit_group,
7726 eq_type_unit_group,
7727 NULL,
ed2dc618 7728 &objfile->objfile_obstack,
f4dc4d17
DE
7729 hashtab_obstack_allocate,
7730 dummy_obstack_deallocate);
7731}
dee91e82 7732
f4dc4d17
DE
7733/* Type units that don't have DW_AT_stmt_list are grouped into their own
7734 partial symtabs. We combine several TUs per psymtab to not let the size
7735 of any one psymtab grow too big. */
7736#define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
7737#define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
dee91e82 7738
094b34ac 7739/* Helper routine for get_type_unit_group.
f4dc4d17
DE
7740 Create the type_unit_group object used to hold one or more TUs. */
7741
7742static struct type_unit_group *
094b34ac 7743create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
f4dc4d17 7744{
518817b3
SM
7745 struct dwarf2_per_objfile *dwarf2_per_objfile
7746 = cu->per_cu->dwarf2_per_objfile;
f4dc4d17 7747 struct objfile *objfile = dwarf2_per_objfile->objfile;
094b34ac 7748 struct dwarf2_per_cu_data *per_cu;
f4dc4d17 7749 struct type_unit_group *tu_group;
f4dc4d17
DE
7750
7751 tu_group = OBSTACK_ZALLOC (&objfile->objfile_obstack,
7752 struct type_unit_group);
094b34ac 7753 per_cu = &tu_group->per_cu;
518817b3 7754 per_cu->dwarf2_per_objfile = dwarf2_per_objfile;
f4dc4d17 7755
094b34ac
DE
7756 if (dwarf2_per_objfile->using_index)
7757 {
7758 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
7759 struct dwarf2_per_cu_quick_data);
094b34ac
DE
7760 }
7761 else
7762 {
9c541725 7763 unsigned int line_offset = to_underlying (line_offset_struct);
094b34ac
DE
7764 struct partial_symtab *pst;
7765 char *name;
7766
7767 /* Give the symtab a useful name for debug purposes. */
7768 if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
7769 name = xstrprintf ("<type_units_%d>",
7770 (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
7771 else
7772 name = xstrprintf ("<type_units_at_0x%x>", line_offset);
7773
7774 pst = create_partial_symtab (per_cu, name);
7775 pst->anonymous = 1;
f4dc4d17 7776
094b34ac
DE
7777 xfree (name);
7778 }
f4dc4d17 7779
094b34ac 7780 tu_group->hash.dwo_unit = cu->dwo_unit;
9c541725 7781 tu_group->hash.line_sect_off = line_offset_struct;
f4dc4d17
DE
7782
7783 return tu_group;
7784}
7785
094b34ac
DE
7786/* Look up the type_unit_group for type unit CU, and create it if necessary.
7787 STMT_LIST is a DW_AT_stmt_list attribute. */
f4dc4d17
DE
7788
7789static struct type_unit_group *
ff39bb5e 7790get_type_unit_group (struct dwarf2_cu *cu, const struct attribute *stmt_list)
f4dc4d17 7791{
518817b3
SM
7792 struct dwarf2_per_objfile *dwarf2_per_objfile
7793 = cu->per_cu->dwarf2_per_objfile;
f4dc4d17
DE
7794 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
7795 struct type_unit_group *tu_group;
7796 void **slot;
7797 unsigned int line_offset;
7798 struct type_unit_group type_unit_group_for_lookup;
7799
7800 if (dwarf2_per_objfile->type_unit_groups == NULL)
7801 {
7802 dwarf2_per_objfile->type_unit_groups =
ed2dc618 7803 allocate_type_unit_groups_table (dwarf2_per_objfile->objfile);
f4dc4d17
DE
7804 }
7805
7806 /* Do we need to create a new group, or can we use an existing one? */
7807
7808 if (stmt_list)
7809 {
7810 line_offset = DW_UNSND (stmt_list);
7811 ++tu_stats->nr_symtab_sharers;
7812 }
7813 else
7814 {
7815 /* Ugh, no stmt_list. Rare, but we have to handle it.
7816 We can do various things here like create one group per TU or
7817 spread them over multiple groups to split up the expansion work.
7818 To avoid worst case scenarios (too many groups or too large groups)
7819 we, umm, group them in bunches. */
7820 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
7821 | (tu_stats->nr_stmt_less_type_units
7822 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
7823 ++tu_stats->nr_stmt_less_type_units;
7824 }
7825
094b34ac 7826 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
9c541725 7827 type_unit_group_for_lookup.hash.line_sect_off = (sect_offset) line_offset;
f4dc4d17
DE
7828 slot = htab_find_slot (dwarf2_per_objfile->type_unit_groups,
7829 &type_unit_group_for_lookup, INSERT);
7830 if (*slot != NULL)
7831 {
9a3c8263 7832 tu_group = (struct type_unit_group *) *slot;
f4dc4d17
DE
7833 gdb_assert (tu_group != NULL);
7834 }
7835 else
7836 {
9c541725 7837 sect_offset line_offset_struct = (sect_offset) line_offset;
094b34ac 7838 tu_group = create_type_unit_group (cu, line_offset_struct);
f4dc4d17
DE
7839 *slot = tu_group;
7840 ++tu_stats->nr_symtabs;
7841 }
7842
7843 return tu_group;
7844}
0018ea6f
DE
7845\f
7846/* Partial symbol tables. */
7847
7848/* Create a psymtab named NAME and assign it to PER_CU.
7849
7850 The caller must fill in the following details:
7851 dirname, textlow, texthigh. */
7852
7853static struct partial_symtab *
7854create_partial_symtab (struct dwarf2_per_cu_data *per_cu, const char *name)
7855{
e3b94546 7856 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
0018ea6f
DE
7857 struct partial_symtab *pst;
7858
18a94d75 7859 pst = start_psymtab_common (objfile, name, 0,
af5bf4ad
SM
7860 objfile->global_psymbols,
7861 objfile->static_psymbols);
0018ea6f
DE
7862
7863 pst->psymtabs_addrmap_supported = 1;
7864
7865 /* This is the glue that links PST into GDB's symbol API. */
7866 pst->read_symtab_private = per_cu;
7867 pst->read_symtab = dwarf2_read_symtab;
7868 per_cu->v.psymtab = pst;
7869
7870 return pst;
7871}
7872
b93601f3
TT
7873/* The DATA object passed to process_psymtab_comp_unit_reader has this
7874 type. */
7875
7876struct process_psymtab_comp_unit_data
7877{
7878 /* True if we are reading a DW_TAG_partial_unit. */
7879
7880 int want_partial_unit;
7881
7882 /* The "pretend" language that is used if the CU doesn't declare a
7883 language. */
7884
7885 enum language pretend_language;
7886};
7887
0018ea6f
DE
7888/* die_reader_func for process_psymtab_comp_unit. */
7889
7890static void
7891process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
d521ce57 7892 const gdb_byte *info_ptr,
0018ea6f
DE
7893 struct die_info *comp_unit_die,
7894 int has_children,
7895 void *data)
7896{
7897 struct dwarf2_cu *cu = reader->cu;
518817b3 7898 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
3e29f34a 7899 struct gdbarch *gdbarch = get_objfile_arch (objfile);
0018ea6f 7900 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
0018ea6f
DE
7901 CORE_ADDR baseaddr;
7902 CORE_ADDR best_lowpc = 0, best_highpc = 0;
7903 struct partial_symtab *pst;
3a2b436a 7904 enum pc_bounds_kind cu_bounds_kind;
0018ea6f 7905 const char *filename;
9a3c8263
SM
7906 struct process_psymtab_comp_unit_data *info
7907 = (struct process_psymtab_comp_unit_data *) data;
0018ea6f 7908
b93601f3 7909 if (comp_unit_die->tag == DW_TAG_partial_unit && !info->want_partial_unit)
0018ea6f
DE
7910 return;
7911
7912 gdb_assert (! per_cu->is_debug_types);
7913
b93601f3 7914 prepare_one_comp_unit (cu, comp_unit_die, info->pretend_language);
0018ea6f 7915
0018ea6f 7916 /* Allocate a new partial symbol table structure. */
7d45c7c3
KB
7917 filename = dwarf2_string_attr (comp_unit_die, DW_AT_name, cu);
7918 if (filename == NULL)
0018ea6f 7919 filename = "";
0018ea6f
DE
7920
7921 pst = create_partial_symtab (per_cu, filename);
7922
7923 /* This must be done before calling dwarf2_build_include_psymtabs. */
7d45c7c3 7924 pst->dirname = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
0018ea6f
DE
7925
7926 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
7927
7928 dwarf2_find_base_address (comp_unit_die, cu);
7929
7930 /* Possibly set the default values of LOWPC and HIGHPC from
7931 `DW_AT_ranges'. */
3a2b436a
JK
7932 cu_bounds_kind = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
7933 &best_highpc, cu, pst);
7934 if (cu_bounds_kind == PC_BOUNDS_HIGH_LOW && best_lowpc < best_highpc)
0018ea6f
DE
7935 /* Store the contiguous range if it is not empty; it can be empty for
7936 CUs with no code. */
7937 addrmap_set_empty (objfile->psymtabs_addrmap,
3e29f34a
MR
7938 gdbarch_adjust_dwarf2_addr (gdbarch,
7939 best_lowpc + baseaddr),
7940 gdbarch_adjust_dwarf2_addr (gdbarch,
7941 best_highpc + baseaddr) - 1,
7942 pst);
0018ea6f
DE
7943
7944 /* Check if comp unit has_children.
7945 If so, read the rest of the partial symbols from this comp unit.
7946 If not, there's no more debug_info for this comp unit. */
7947 if (has_children)
7948 {
7949 struct partial_die_info *first_die;
7950 CORE_ADDR lowpc, highpc;
7951
7952 lowpc = ((CORE_ADDR) -1);
7953 highpc = ((CORE_ADDR) 0);
7954
7955 first_die = load_partial_dies (reader, info_ptr, 1);
7956
7957 scan_partial_symbols (first_die, &lowpc, &highpc,
e385593e 7958 cu_bounds_kind <= PC_BOUNDS_INVALID, cu);
0018ea6f
DE
7959
7960 /* If we didn't find a lowpc, set it to highpc to avoid
7961 complaints from `maint check'. */
7962 if (lowpc == ((CORE_ADDR) -1))
7963 lowpc = highpc;
7964
7965 /* If the compilation unit didn't have an explicit address range,
7966 then use the information extracted from its child dies. */
e385593e 7967 if (cu_bounds_kind <= PC_BOUNDS_INVALID)
0018ea6f
DE
7968 {
7969 best_lowpc = lowpc;
7970 best_highpc = highpc;
7971 }
7972 }
3e29f34a
MR
7973 pst->textlow = gdbarch_adjust_dwarf2_addr (gdbarch, best_lowpc + baseaddr);
7974 pst->texthigh = gdbarch_adjust_dwarf2_addr (gdbarch, best_highpc + baseaddr);
0018ea6f 7975
8763cede 7976 end_psymtab_common (objfile, pst);
0018ea6f
DE
7977
7978 if (!VEC_empty (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs))
7979 {
7980 int i;
7981 int len = VEC_length (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs);
7982 struct dwarf2_per_cu_data *iter;
7983
7984 /* Fill in 'dependencies' here; we fill in 'users' in a
7985 post-pass. */
7986 pst->number_of_dependencies = len;
8d749320
SM
7987 pst->dependencies =
7988 XOBNEWVEC (&objfile->objfile_obstack, struct partial_symtab *, len);
0018ea6f
DE
7989 for (i = 0;
7990 VEC_iterate (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs,
7991 i, iter);
7992 ++i)
7993 pst->dependencies[i] = iter->v.psymtab;
7994
7995 VEC_free (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs);
7996 }
7997
7998 /* Get the list of files included in the current compilation unit,
7999 and build a psymtab for each of them. */
8000 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
8001
b4f54984 8002 if (dwarf_read_debug)
0018ea6f
DE
8003 {
8004 struct gdbarch *gdbarch = get_objfile_arch (objfile);
8005
8006 fprintf_unfiltered (gdb_stdlog,
9d8780f0 8007 "Psymtab for %s unit @%s: %s - %s"
0018ea6f
DE
8008 ", %d global, %d static syms\n",
8009 per_cu->is_debug_types ? "type" : "comp",
9d8780f0 8010 sect_offset_str (per_cu->sect_off),
0018ea6f
DE
8011 paddress (gdbarch, pst->textlow),
8012 paddress (gdbarch, pst->texthigh),
8013 pst->n_global_syms, pst->n_static_syms);
8014 }
8015}
8016
8017/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
8018 Process compilation unit THIS_CU for a psymtab. */
8019
8020static void
8021process_psymtab_comp_unit (struct dwarf2_per_cu_data *this_cu,
b93601f3
TT
8022 int want_partial_unit,
8023 enum language pretend_language)
0018ea6f
DE
8024{
8025 /* If this compilation unit was already read in, free the
8026 cached copy in order to read it in again. This is
8027 necessary because we skipped some symbols when we first
8028 read in the compilation unit (see load_partial_dies).
8029 This problem could be avoided, but the benefit is unclear. */
8030 if (this_cu->cu != NULL)
8031 free_one_cached_comp_unit (this_cu);
8032
f1902523 8033 if (this_cu->is_debug_types)
58f0c718
TT
8034 init_cutu_and_read_dies (this_cu, NULL, 0, 0, false,
8035 build_type_psymtabs_reader, NULL);
f1902523
JK
8036 else
8037 {
8038 process_psymtab_comp_unit_data info;
8039 info.want_partial_unit = want_partial_unit;
8040 info.pretend_language = pretend_language;
58f0c718 8041 init_cutu_and_read_dies (this_cu, NULL, 0, 0, false,
f1902523
JK
8042 process_psymtab_comp_unit_reader, &info);
8043 }
0018ea6f
DE
8044
8045 /* Age out any secondary CUs. */
ed2dc618 8046 age_cached_comp_units (this_cu->dwarf2_per_objfile);
0018ea6f 8047}
f4dc4d17
DE
8048
8049/* Reader function for build_type_psymtabs. */
8050
8051static void
8052build_type_psymtabs_reader (const struct die_reader_specs *reader,
d521ce57 8053 const gdb_byte *info_ptr,
f4dc4d17
DE
8054 struct die_info *type_unit_die,
8055 int has_children,
8056 void *data)
8057{
ed2dc618 8058 struct dwarf2_per_objfile *dwarf2_per_objfile
518817b3 8059 = reader->cu->per_cu->dwarf2_per_objfile;
f4dc4d17
DE
8060 struct objfile *objfile = dwarf2_per_objfile->objfile;
8061 struct dwarf2_cu *cu = reader->cu;
8062 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
0186c6a7 8063 struct signatured_type *sig_type;
f4dc4d17
DE
8064 struct type_unit_group *tu_group;
8065 struct attribute *attr;
8066 struct partial_die_info *first_die;
8067 CORE_ADDR lowpc, highpc;
8068 struct partial_symtab *pst;
8069
8070 gdb_assert (data == NULL);
0186c6a7
DE
8071 gdb_assert (per_cu->is_debug_types);
8072 sig_type = (struct signatured_type *) per_cu;
f4dc4d17
DE
8073
8074 if (! has_children)
8075 return;
8076
8077 attr = dwarf2_attr_no_follow (type_unit_die, DW_AT_stmt_list);
094b34ac 8078 tu_group = get_type_unit_group (cu, attr);
f4dc4d17 8079
0186c6a7 8080 VEC_safe_push (sig_type_ptr, tu_group->tus, sig_type);
f4dc4d17
DE
8081
8082 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
f4dc4d17
DE
8083 pst = create_partial_symtab (per_cu, "");
8084 pst->anonymous = 1;
8085
8086 first_die = load_partial_dies (reader, info_ptr, 1);
8087
8088 lowpc = (CORE_ADDR) -1;
8089 highpc = (CORE_ADDR) 0;
8090 scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
8091
8763cede 8092 end_psymtab_common (objfile, pst);
f4dc4d17
DE
8093}
8094
73051182
DE
8095/* Struct used to sort TUs by their abbreviation table offset. */
8096
8097struct tu_abbrev_offset
8098{
b2bdb8cf
SM
8099 tu_abbrev_offset (signatured_type *sig_type_, sect_offset abbrev_offset_)
8100 : sig_type (sig_type_), abbrev_offset (abbrev_offset_)
8101 {}
8102
8103 signatured_type *sig_type;
73051182
DE
8104 sect_offset abbrev_offset;
8105};
8106
484cf504 8107/* Helper routine for build_type_psymtabs_1, passed to std::sort. */
73051182 8108
484cf504
TT
8109static bool
8110sort_tu_by_abbrev_offset (const struct tu_abbrev_offset &a,
8111 const struct tu_abbrev_offset &b)
73051182 8112{
484cf504 8113 return a.abbrev_offset < b.abbrev_offset;
73051182
DE
8114}
8115
8116/* Efficiently read all the type units.
8117 This does the bulk of the work for build_type_psymtabs.
8118
8119 The efficiency is because we sort TUs by the abbrev table they use and
8120 only read each abbrev table once. In one program there are 200K TUs
8121 sharing 8K abbrev tables.
8122
8123 The main purpose of this function is to support building the
8124 dwarf2_per_objfile->type_unit_groups table.
8125 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
8126 can collapse the search space by grouping them by stmt_list.
8127 The savings can be significant, in the same program from above the 200K TUs
8128 share 8K stmt_list tables.
8129
8130 FUNC is expected to call get_type_unit_group, which will create the
8131 struct type_unit_group if necessary and add it to
8132 dwarf2_per_objfile->type_unit_groups. */
8133
8134static void
ed2dc618 8135build_type_psymtabs_1 (struct dwarf2_per_objfile *dwarf2_per_objfile)
73051182 8136{
73051182 8137 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
685af9cd 8138 abbrev_table_up abbrev_table;
73051182 8139 sect_offset abbrev_offset;
73051182
DE
8140
8141 /* It's up to the caller to not call us multiple times. */
8142 gdb_assert (dwarf2_per_objfile->type_unit_groups == NULL);
8143
b2bdb8cf 8144 if (dwarf2_per_objfile->all_type_units.empty ())
73051182
DE
8145 return;
8146
8147 /* TUs typically share abbrev tables, and there can be way more TUs than
8148 abbrev tables. Sort by abbrev table to reduce the number of times we
8149 read each abbrev table in.
8150 Alternatives are to punt or to maintain a cache of abbrev tables.
8151 This is simpler and efficient enough for now.
8152
8153 Later we group TUs by their DW_AT_stmt_list value (as this defines the
8154 symtab to use). Typically TUs with the same abbrev offset have the same
8155 stmt_list value too so in practice this should work well.
8156
8157 The basic algorithm here is:
8158
8159 sort TUs by abbrev table
8160 for each TU with same abbrev table:
8161 read abbrev table if first user
8162 read TU top level DIE
8163 [IWBN if DWO skeletons had DW_AT_stmt_list]
8164 call FUNC */
8165
b4f54984 8166 if (dwarf_read_debug)
73051182
DE
8167 fprintf_unfiltered (gdb_stdlog, "Building type unit groups ...\n");
8168
8169 /* Sort in a separate table to maintain the order of all_type_units
8170 for .gdb_index: TU indices directly index all_type_units. */
b2bdb8cf
SM
8171 std::vector<tu_abbrev_offset> sorted_by_abbrev;
8172 sorted_by_abbrev.reserve (dwarf2_per_objfile->all_type_units.size ());
8173
8174 for (signatured_type *sig_type : dwarf2_per_objfile->all_type_units)
8175 sorted_by_abbrev.emplace_back
8176 (sig_type, read_abbrev_offset (dwarf2_per_objfile,
8177 sig_type->per_cu.section,
8178 sig_type->per_cu.sect_off));
73051182 8179
484cf504
TT
8180 std::sort (sorted_by_abbrev.begin (), sorted_by_abbrev.end (),
8181 sort_tu_by_abbrev_offset);
73051182 8182
9c541725 8183 abbrev_offset = (sect_offset) ~(unsigned) 0;
73051182 8184
b2bdb8cf 8185 for (const tu_abbrev_offset &tu : sorted_by_abbrev)
73051182 8186 {
73051182
DE
8187 /* Switch to the next abbrev table if necessary. */
8188 if (abbrev_table == NULL
b2bdb8cf 8189 || tu.abbrev_offset != abbrev_offset)
73051182 8190 {
b2bdb8cf 8191 abbrev_offset = tu.abbrev_offset;
73051182 8192 abbrev_table =
ed2dc618
SM
8193 abbrev_table_read_table (dwarf2_per_objfile,
8194 &dwarf2_per_objfile->abbrev,
73051182
DE
8195 abbrev_offset);
8196 ++tu_stats->nr_uniq_abbrev_tables;
8197 }
8198
b2bdb8cf 8199 init_cutu_and_read_dies (&tu.sig_type->per_cu, abbrev_table.get (),
58f0c718 8200 0, 0, false, build_type_psymtabs_reader, NULL);
73051182 8201 }
6aa5f3a6 8202}
73051182 8203
6aa5f3a6
DE
8204/* Print collected type unit statistics. */
8205
8206static void
ed2dc618 8207print_tu_stats (struct dwarf2_per_objfile *dwarf2_per_objfile)
6aa5f3a6
DE
8208{
8209 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
8210
8211 fprintf_unfiltered (gdb_stdlog, "Type unit statistics:\n");
b2bdb8cf
SM
8212 fprintf_unfiltered (gdb_stdlog, " %zu TUs\n",
8213 dwarf2_per_objfile->all_type_units.size ());
6aa5f3a6
DE
8214 fprintf_unfiltered (gdb_stdlog, " %d uniq abbrev tables\n",
8215 tu_stats->nr_uniq_abbrev_tables);
8216 fprintf_unfiltered (gdb_stdlog, " %d symtabs from stmt_list entries\n",
8217 tu_stats->nr_symtabs);
8218 fprintf_unfiltered (gdb_stdlog, " %d symtab sharers\n",
8219 tu_stats->nr_symtab_sharers);
8220 fprintf_unfiltered (gdb_stdlog, " %d type units without a stmt_list\n",
8221 tu_stats->nr_stmt_less_type_units);
8222 fprintf_unfiltered (gdb_stdlog, " %d all_type_units reallocs\n",
8223 tu_stats->nr_all_type_units_reallocs);
73051182
DE
8224}
8225
f4dc4d17
DE
8226/* Traversal function for build_type_psymtabs. */
8227
8228static int
8229build_type_psymtab_dependencies (void **slot, void *info)
8230{
ed2dc618
SM
8231 struct dwarf2_per_objfile *dwarf2_per_objfile
8232 = (struct dwarf2_per_objfile *) info;
f4dc4d17
DE
8233 struct objfile *objfile = dwarf2_per_objfile->objfile;
8234 struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
094b34ac 8235 struct dwarf2_per_cu_data *per_cu = &tu_group->per_cu;
f4dc4d17 8236 struct partial_symtab *pst = per_cu->v.psymtab;
0186c6a7
DE
8237 int len = VEC_length (sig_type_ptr, tu_group->tus);
8238 struct signatured_type *iter;
f4dc4d17
DE
8239 int i;
8240
8241 gdb_assert (len > 0);
0186c6a7 8242 gdb_assert (IS_TYPE_UNIT_GROUP (per_cu));
f4dc4d17
DE
8243
8244 pst->number_of_dependencies = len;
8d749320
SM
8245 pst->dependencies =
8246 XOBNEWVEC (&objfile->objfile_obstack, struct partial_symtab *, len);
f4dc4d17 8247 for (i = 0;
0186c6a7 8248 VEC_iterate (sig_type_ptr, tu_group->tus, i, iter);
f4dc4d17
DE
8249 ++i)
8250 {
0186c6a7
DE
8251 gdb_assert (iter->per_cu.is_debug_types);
8252 pst->dependencies[i] = iter->per_cu.v.psymtab;
796a7ff8 8253 iter->type_unit_group = tu_group;
f4dc4d17
DE
8254 }
8255
0186c6a7 8256 VEC_free (sig_type_ptr, tu_group->tus);
348e048f
DE
8257
8258 return 1;
8259}
8260
8261/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
8262 Build partial symbol tables for the .debug_types comp-units. */
8263
8264static void
ed2dc618 8265build_type_psymtabs (struct dwarf2_per_objfile *dwarf2_per_objfile)
348e048f 8266{
ed2dc618 8267 if (! create_all_type_units (dwarf2_per_objfile))
348e048f
DE
8268 return;
8269
ed2dc618 8270 build_type_psymtabs_1 (dwarf2_per_objfile);
6aa5f3a6 8271}
f4dc4d17 8272
6aa5f3a6
DE
8273/* Traversal function for process_skeletonless_type_unit.
8274 Read a TU in a DWO file and build partial symbols for it. */
8275
8276static int
8277process_skeletonless_type_unit (void **slot, void *info)
8278{
8279 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
ed2dc618
SM
8280 struct dwarf2_per_objfile *dwarf2_per_objfile
8281 = (struct dwarf2_per_objfile *) info;
6aa5f3a6
DE
8282 struct signatured_type find_entry, *entry;
8283
8284 /* If this TU doesn't exist in the global table, add it and read it in. */
8285
8286 if (dwarf2_per_objfile->signatured_types == NULL)
8287 {
8288 dwarf2_per_objfile->signatured_types
ed2dc618 8289 = allocate_signatured_type_table (dwarf2_per_objfile->objfile);
6aa5f3a6
DE
8290 }
8291
8292 find_entry.signature = dwo_unit->signature;
8293 slot = htab_find_slot (dwarf2_per_objfile->signatured_types, &find_entry,
8294 INSERT);
8295 /* If we've already seen this type there's nothing to do. What's happening
8296 is we're doing our own version of comdat-folding here. */
8297 if (*slot != NULL)
8298 return 1;
8299
8300 /* This does the job that create_all_type_units would have done for
8301 this TU. */
ed2dc618
SM
8302 entry = add_type_unit (dwarf2_per_objfile, dwo_unit->signature, slot);
8303 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, entry, dwo_unit);
6aa5f3a6
DE
8304 *slot = entry;
8305
8306 /* This does the job that build_type_psymtabs_1 would have done. */
58f0c718 8307 init_cutu_and_read_dies (&entry->per_cu, NULL, 0, 0, false,
6aa5f3a6
DE
8308 build_type_psymtabs_reader, NULL);
8309
8310 return 1;
8311}
8312
8313/* Traversal function for process_skeletonless_type_units. */
8314
8315static int
8316process_dwo_file_for_skeletonless_type_units (void **slot, void *info)
8317{
8318 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
8319
8320 if (dwo_file->tus != NULL)
8321 {
8322 htab_traverse_noresize (dwo_file->tus,
8323 process_skeletonless_type_unit, info);
8324 }
8325
8326 return 1;
8327}
8328
8329/* Scan all TUs of DWO files, verifying we've processed them.
8330 This is needed in case a TU was emitted without its skeleton.
8331 Note: This can't be done until we know what all the DWO files are. */
8332
8333static void
ed2dc618 8334process_skeletonless_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
6aa5f3a6
DE
8335{
8336 /* Skeletonless TUs in DWP files without .gdb_index is not supported yet. */
ed2dc618 8337 if (get_dwp_file (dwarf2_per_objfile) == NULL
6aa5f3a6
DE
8338 && dwarf2_per_objfile->dwo_files != NULL)
8339 {
8340 htab_traverse_noresize (dwarf2_per_objfile->dwo_files,
8341 process_dwo_file_for_skeletonless_type_units,
ed2dc618 8342 dwarf2_per_objfile);
6aa5f3a6 8343 }
348e048f
DE
8344}
8345
ed2dc618 8346/* Compute the 'user' field for each psymtab in DWARF2_PER_OBJFILE. */
95554aad
TT
8347
8348static void
ed2dc618 8349set_partial_user (struct dwarf2_per_objfile *dwarf2_per_objfile)
95554aad 8350{
b76e467d 8351 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
95554aad 8352 {
95554aad 8353 struct partial_symtab *pst = per_cu->v.psymtab;
95554aad 8354
36586728
TT
8355 if (pst == NULL)
8356 continue;
8357
b76e467d 8358 for (int j = 0; j < pst->number_of_dependencies; ++j)
95554aad
TT
8359 {
8360 /* Set the 'user' field only if it is not already set. */
8361 if (pst->dependencies[j]->user == NULL)
8362 pst->dependencies[j]->user = pst;
8363 }
8364 }
8365}
8366
93311388
DE
8367/* Build the partial symbol table by doing a quick pass through the
8368 .debug_info and .debug_abbrev sections. */
72bf9492 8369
93311388 8370static void
ed2dc618 8371dwarf2_build_psymtabs_hard (struct dwarf2_per_objfile *dwarf2_per_objfile)
93311388 8372{
ed2dc618 8373 struct objfile *objfile = dwarf2_per_objfile->objfile;
93311388 8374
b4f54984 8375 if (dwarf_read_debug)
45cfd468
DE
8376 {
8377 fprintf_unfiltered (gdb_stdlog, "Building psymtabs of objfile %s ...\n",
4262abfb 8378 objfile_name (objfile));
45cfd468
DE
8379 }
8380
98bfdba5
PA
8381 dwarf2_per_objfile->reading_partial_symbols = 1;
8382
be391dca 8383 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
91c24f0a 8384
93311388
DE
8385 /* Any cached compilation units will be linked by the per-objfile
8386 read_in_chain. Make sure to free them when we're done. */
11ed8cad 8387 free_cached_comp_units freer (dwarf2_per_objfile);
72bf9492 8388
ed2dc618 8389 build_type_psymtabs (dwarf2_per_objfile);
348e048f 8390
ed2dc618 8391 create_all_comp_units (dwarf2_per_objfile);
c906108c 8392
60606b2c
TT
8393 /* Create a temporary address map on a temporary obstack. We later
8394 copy this to the final obstack. */
8268c778 8395 auto_obstack temp_obstack;
791afaa2
TT
8396
8397 scoped_restore save_psymtabs_addrmap
8398 = make_scoped_restore (&objfile->psymtabs_addrmap,
8399 addrmap_create_mutable (&temp_obstack));
72bf9492 8400
b76e467d
SM
8401 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
8402 process_psymtab_comp_unit (per_cu, 0, language_minimal);
ff013f42 8403
6aa5f3a6 8404 /* This has to wait until we read the CUs, we need the list of DWOs. */
ed2dc618 8405 process_skeletonless_type_units (dwarf2_per_objfile);
6aa5f3a6
DE
8406
8407 /* Now that all TUs have been processed we can fill in the dependencies. */
8408 if (dwarf2_per_objfile->type_unit_groups != NULL)
8409 {
8410 htab_traverse_noresize (dwarf2_per_objfile->type_unit_groups,
ed2dc618 8411 build_type_psymtab_dependencies, dwarf2_per_objfile);
6aa5f3a6
DE
8412 }
8413
b4f54984 8414 if (dwarf_read_debug)
ed2dc618 8415 print_tu_stats (dwarf2_per_objfile);
6aa5f3a6 8416
ed2dc618 8417 set_partial_user (dwarf2_per_objfile);
95554aad 8418
ff013f42
JK
8419 objfile->psymtabs_addrmap = addrmap_create_fixed (objfile->psymtabs_addrmap,
8420 &objfile->objfile_obstack);
791afaa2
TT
8421 /* At this point we want to keep the address map. */
8422 save_psymtabs_addrmap.release ();
ff013f42 8423
b4f54984 8424 if (dwarf_read_debug)
45cfd468 8425 fprintf_unfiltered (gdb_stdlog, "Done building psymtabs of %s\n",
4262abfb 8426 objfile_name (objfile));
ae038cb0
DJ
8427}
8428
3019eac3 8429/* die_reader_func for load_partial_comp_unit. */
ae038cb0
DJ
8430
8431static void
dee91e82 8432load_partial_comp_unit_reader (const struct die_reader_specs *reader,
d521ce57 8433 const gdb_byte *info_ptr,
dee91e82
DE
8434 struct die_info *comp_unit_die,
8435 int has_children,
8436 void *data)
ae038cb0 8437{
dee91e82 8438 struct dwarf2_cu *cu = reader->cu;
ae038cb0 8439
95554aad 8440 prepare_one_comp_unit (cu, comp_unit_die, language_minimal);
ae038cb0 8441
ae038cb0
DJ
8442 /* Check if comp unit has_children.
8443 If so, read the rest of the partial symbols from this comp unit.
0963b4bd 8444 If not, there's no more debug_info for this comp unit. */
d85a05f0 8445 if (has_children)
dee91e82
DE
8446 load_partial_dies (reader, info_ptr, 0);
8447}
98bfdba5 8448
dee91e82
DE
8449/* Load the partial DIEs for a secondary CU into memory.
8450 This is also used when rereading a primary CU with load_all_dies. */
c5b7e1cb 8451
dee91e82
DE
8452static void
8453load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu)
8454{
58f0c718 8455 init_cutu_and_read_dies (this_cu, NULL, 1, 1, false,
f4dc4d17 8456 load_partial_comp_unit_reader, NULL);
ae038cb0
DJ
8457}
8458
ae038cb0 8459static void
ed2dc618 8460read_comp_units_from_section (struct dwarf2_per_objfile *dwarf2_per_objfile,
36586728 8461 struct dwarf2_section_info *section,
f1902523 8462 struct dwarf2_section_info *abbrev_section,
b76e467d 8463 unsigned int is_dwz)
ae038cb0 8464{
d521ce57 8465 const gdb_byte *info_ptr;
ed2dc618 8466 struct objfile *objfile = dwarf2_per_objfile->objfile;
be391dca 8467
b4f54984 8468 if (dwarf_read_debug)
bf6af496 8469 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s\n",
a32a8923
DE
8470 get_section_name (section),
8471 get_section_file_name (section));
bf6af496 8472
36586728 8473 dwarf2_read_section (objfile, section);
ae038cb0 8474
36586728 8475 info_ptr = section->buffer;
6e70227d 8476
36586728 8477 while (info_ptr < section->buffer + section->size)
ae038cb0 8478 {
ae038cb0 8479 struct dwarf2_per_cu_data *this_cu;
ae038cb0 8480
9c541725 8481 sect_offset sect_off = (sect_offset) (info_ptr - section->buffer);
ae038cb0 8482
f1902523 8483 comp_unit_head cu_header;
ed2dc618
SM
8484 read_and_check_comp_unit_head (dwarf2_per_objfile, &cu_header, section,
8485 abbrev_section, info_ptr,
8486 rcuh_kind::COMPILE);
ae038cb0
DJ
8487
8488 /* Save the compilation unit for later lookup. */
f1902523
JK
8489 if (cu_header.unit_type != DW_UT_type)
8490 {
8491 this_cu = XOBNEW (&objfile->objfile_obstack,
8492 struct dwarf2_per_cu_data);
8493 memset (this_cu, 0, sizeof (*this_cu));
8494 }
8495 else
8496 {
8497 auto sig_type = XOBNEW (&objfile->objfile_obstack,
8498 struct signatured_type);
8499 memset (sig_type, 0, sizeof (*sig_type));
8500 sig_type->signature = cu_header.signature;
8501 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
8502 this_cu = &sig_type->per_cu;
8503 }
8504 this_cu->is_debug_types = (cu_header.unit_type == DW_UT_type);
9c541725 8505 this_cu->sect_off = sect_off;
f1902523 8506 this_cu->length = cu_header.length + cu_header.initial_length_size;
36586728 8507 this_cu->is_dwz = is_dwz;
e3b94546 8508 this_cu->dwarf2_per_objfile = dwarf2_per_objfile;
8a0459fd 8509 this_cu->section = section;
ae038cb0 8510
b76e467d 8511 dwarf2_per_objfile->all_comp_units.push_back (this_cu);
ae038cb0
DJ
8512
8513 info_ptr = info_ptr + this_cu->length;
8514 }
36586728
TT
8515}
8516
8517/* Create a list of all compilation units in OBJFILE.
8518 This is only done for -readnow and building partial symtabs. */
8519
8520static void
ed2dc618 8521create_all_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
36586728 8522{
b76e467d 8523 gdb_assert (dwarf2_per_objfile->all_comp_units.empty ());
ed2dc618 8524 read_comp_units_from_section (dwarf2_per_objfile, &dwarf2_per_objfile->info,
b76e467d 8525 &dwarf2_per_objfile->abbrev, 0);
36586728 8526
b76e467d 8527 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
4db1a1dc 8528 if (dwz != NULL)
ed2dc618 8529 read_comp_units_from_section (dwarf2_per_objfile, &dwz->info, &dwz->abbrev,
b76e467d 8530 1);
c906108c
SS
8531}
8532
5734ee8b 8533/* Process all loaded DIEs for compilation unit CU, starting at
cdc07690 8534 FIRST_DIE. The caller should pass SET_ADDRMAP == 1 if the compilation
5734ee8b 8535 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
cdc07690
YQ
8536 DW_AT_ranges). See the comments of add_partial_subprogram on how
8537 SET_ADDRMAP is used and how *LOWPC and *HIGHPC are updated. */
c906108c 8538
72bf9492
DJ
8539static void
8540scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
cdc07690
YQ
8541 CORE_ADDR *highpc, int set_addrmap,
8542 struct dwarf2_cu *cu)
c906108c 8543{
72bf9492 8544 struct partial_die_info *pdi;
c906108c 8545
91c24f0a
DC
8546 /* Now, march along the PDI's, descending into ones which have
8547 interesting children but skipping the children of the other ones,
8548 until we reach the end of the compilation unit. */
c906108c 8549
72bf9492 8550 pdi = first_die;
91c24f0a 8551
72bf9492
DJ
8552 while (pdi != NULL)
8553 {
52356b79 8554 pdi->fixup (cu);
c906108c 8555
f55ee35c 8556 /* Anonymous namespaces or modules have no name but have interesting
91c24f0a
DC
8557 children, so we need to look at them. Ditto for anonymous
8558 enums. */
933c6fe4 8559
72bf9492 8560 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
95554aad 8561 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
b1dc1806
XR
8562 || pdi->tag == DW_TAG_imported_unit
8563 || pdi->tag == DW_TAG_inlined_subroutine)
c906108c 8564 {
72bf9492 8565 switch (pdi->tag)
c906108c
SS
8566 {
8567 case DW_TAG_subprogram:
b1dc1806 8568 case DW_TAG_inlined_subroutine:
cdc07690 8569 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
c906108c 8570 break;
72929c62 8571 case DW_TAG_constant:
c906108c
SS
8572 case DW_TAG_variable:
8573 case DW_TAG_typedef:
91c24f0a 8574 case DW_TAG_union_type:
72bf9492 8575 if (!pdi->is_declaration)
63d06c5c 8576 {
72bf9492 8577 add_partial_symbol (pdi, cu);
63d06c5c
DC
8578 }
8579 break;
c906108c 8580 case DW_TAG_class_type:
680b30c7 8581 case DW_TAG_interface_type:
c906108c 8582 case DW_TAG_structure_type:
72bf9492 8583 if (!pdi->is_declaration)
c906108c 8584 {
72bf9492 8585 add_partial_symbol (pdi, cu);
c906108c 8586 }
b7fee5a3
KS
8587 if ((cu->language == language_rust
8588 || cu->language == language_cplus) && pdi->has_children)
e98c9e7c
TT
8589 scan_partial_symbols (pdi->die_child, lowpc, highpc,
8590 set_addrmap, cu);
c906108c 8591 break;
91c24f0a 8592 case DW_TAG_enumeration_type:
72bf9492
DJ
8593 if (!pdi->is_declaration)
8594 add_partial_enumeration (pdi, cu);
c906108c
SS
8595 break;
8596 case DW_TAG_base_type:
a02abb62 8597 case DW_TAG_subrange_type:
c906108c 8598 /* File scope base type definitions are added to the partial
c5aa993b 8599 symbol table. */
72bf9492 8600 add_partial_symbol (pdi, cu);
c906108c 8601 break;
d9fa45fe 8602 case DW_TAG_namespace:
cdc07690 8603 add_partial_namespace (pdi, lowpc, highpc, set_addrmap, cu);
91c24f0a 8604 break;
5d7cb8df 8605 case DW_TAG_module:
cdc07690 8606 add_partial_module (pdi, lowpc, highpc, set_addrmap, cu);
5d7cb8df 8607 break;
95554aad
TT
8608 case DW_TAG_imported_unit:
8609 {
8610 struct dwarf2_per_cu_data *per_cu;
8611
f4dc4d17
DE
8612 /* For now we don't handle imported units in type units. */
8613 if (cu->per_cu->is_debug_types)
8614 {
8615 error (_("Dwarf Error: DW_TAG_imported_unit is not"
8616 " supported in type units [in module %s]"),
518817b3 8617 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
f4dc4d17
DE
8618 }
8619
e3b94546
SM
8620 per_cu = dwarf2_find_containing_comp_unit
8621 (pdi->d.sect_off, pdi->is_dwz,
518817b3 8622 cu->per_cu->dwarf2_per_objfile);
95554aad
TT
8623
8624 /* Go read the partial unit, if needed. */
8625 if (per_cu->v.psymtab == NULL)
b93601f3 8626 process_psymtab_comp_unit (per_cu, 1, cu->language);
95554aad 8627
f4dc4d17 8628 VEC_safe_push (dwarf2_per_cu_ptr,
796a7ff8 8629 cu->per_cu->imported_symtabs, per_cu);
95554aad
TT
8630 }
8631 break;
74921315
KS
8632 case DW_TAG_imported_declaration:
8633 add_partial_symbol (pdi, cu);
8634 break;
c906108c
SS
8635 default:
8636 break;
8637 }
8638 }
8639
72bf9492
DJ
8640 /* If the die has a sibling, skip to the sibling. */
8641
8642 pdi = pdi->die_sibling;
8643 }
8644}
8645
8646/* Functions used to compute the fully scoped name of a partial DIE.
91c24f0a 8647
72bf9492 8648 Normally, this is simple. For C++, the parent DIE's fully scoped
9c37b5ae 8649 name is concatenated with "::" and the partial DIE's name.
72bf9492
DJ
8650 Enumerators are an exception; they use the scope of their parent
8651 enumeration type, i.e. the name of the enumeration type is not
8652 prepended to the enumerator.
91c24f0a 8653
72bf9492
DJ
8654 There are two complexities. One is DW_AT_specification; in this
8655 case "parent" means the parent of the target of the specification,
8656 instead of the direct parent of the DIE. The other is compilers
8657 which do not emit DW_TAG_namespace; in this case we try to guess
8658 the fully qualified name of structure types from their members'
8659 linkage names. This must be done using the DIE's children rather
8660 than the children of any DW_AT_specification target. We only need
8661 to do this for structures at the top level, i.e. if the target of
8662 any DW_AT_specification (if any; otherwise the DIE itself) does not
8663 have a parent. */
8664
8665/* Compute the scope prefix associated with PDI's parent, in
8666 compilation unit CU. The result will be allocated on CU's
8667 comp_unit_obstack, or a copy of the already allocated PDI->NAME
8668 field. NULL is returned if no prefix is necessary. */
15d034d0 8669static const char *
72bf9492
DJ
8670partial_die_parent_scope (struct partial_die_info *pdi,
8671 struct dwarf2_cu *cu)
8672{
15d034d0 8673 const char *grandparent_scope;
72bf9492 8674 struct partial_die_info *parent, *real_pdi;
91c24f0a 8675
72bf9492
DJ
8676 /* We need to look at our parent DIE; if we have a DW_AT_specification,
8677 then this means the parent of the specification DIE. */
8678
8679 real_pdi = pdi;
72bf9492 8680 while (real_pdi->has_specification)
36586728
TT
8681 real_pdi = find_partial_die (real_pdi->spec_offset,
8682 real_pdi->spec_is_dwz, cu);
72bf9492
DJ
8683
8684 parent = real_pdi->die_parent;
8685 if (parent == NULL)
8686 return NULL;
8687
8688 if (parent->scope_set)
8689 return parent->scope;
8690
52356b79 8691 parent->fixup (cu);
72bf9492 8692
10b3939b 8693 grandparent_scope = partial_die_parent_scope (parent, cu);
72bf9492 8694
acebe513
UW
8695 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
8696 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
8697 Work around this problem here. */
8698 if (cu->language == language_cplus
6e70227d 8699 && parent->tag == DW_TAG_namespace
acebe513
UW
8700 && strcmp (parent->name, "::") == 0
8701 && grandparent_scope == NULL)
8702 {
8703 parent->scope = NULL;
8704 parent->scope_set = 1;
8705 return NULL;
8706 }
8707
9c6c53f7
SA
8708 if (pdi->tag == DW_TAG_enumerator)
8709 /* Enumerators should not get the name of the enumeration as a prefix. */
8710 parent->scope = grandparent_scope;
8711 else if (parent->tag == DW_TAG_namespace
f55ee35c 8712 || parent->tag == DW_TAG_module
72bf9492
DJ
8713 || parent->tag == DW_TAG_structure_type
8714 || parent->tag == DW_TAG_class_type
680b30c7 8715 || parent->tag == DW_TAG_interface_type
ceeb3d5a
TT
8716 || parent->tag == DW_TAG_union_type
8717 || parent->tag == DW_TAG_enumeration_type)
72bf9492
DJ
8718 {
8719 if (grandparent_scope == NULL)
8720 parent->scope = parent->name;
8721 else
3e43a32a
MS
8722 parent->scope = typename_concat (&cu->comp_unit_obstack,
8723 grandparent_scope,
f55ee35c 8724 parent->name, 0, cu);
72bf9492 8725 }
72bf9492
DJ
8726 else
8727 {
8728 /* FIXME drow/2004-04-01: What should we be doing with
8729 function-local names? For partial symbols, we should probably be
8730 ignoring them. */
b98664d3 8731 complaint (_("unhandled containing DIE tag %d for DIE at %s"),
9d8780f0 8732 parent->tag, sect_offset_str (pdi->sect_off));
72bf9492 8733 parent->scope = grandparent_scope;
c906108c
SS
8734 }
8735
72bf9492
DJ
8736 parent->scope_set = 1;
8737 return parent->scope;
8738}
8739
8740/* Return the fully scoped name associated with PDI, from compilation unit
8741 CU. The result will be allocated with malloc. */
4568ecf9 8742
72bf9492
DJ
8743static char *
8744partial_die_full_name (struct partial_die_info *pdi,
8745 struct dwarf2_cu *cu)
8746{
15d034d0 8747 const char *parent_scope;
72bf9492 8748
98bfdba5
PA
8749 /* If this is a template instantiation, we can not work out the
8750 template arguments from partial DIEs. So, unfortunately, we have
8751 to go through the full DIEs. At least any work we do building
8752 types here will be reused if full symbols are loaded later. */
8753 if (pdi->has_template_arguments)
8754 {
52356b79 8755 pdi->fixup (cu);
98bfdba5
PA
8756
8757 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
8758 {
8759 struct die_info *die;
8760 struct attribute attr;
8761 struct dwarf2_cu *ref_cu = cu;
8762
b64f50a1 8763 /* DW_FORM_ref_addr is using section offset. */
b4069958 8764 attr.name = (enum dwarf_attribute) 0;
98bfdba5 8765 attr.form = DW_FORM_ref_addr;
9c541725 8766 attr.u.unsnd = to_underlying (pdi->sect_off);
98bfdba5
PA
8767 die = follow_die_ref (NULL, &attr, &ref_cu);
8768
8769 return xstrdup (dwarf2_full_name (NULL, die, ref_cu));
8770 }
8771 }
8772
72bf9492
DJ
8773 parent_scope = partial_die_parent_scope (pdi, cu);
8774 if (parent_scope == NULL)
8775 return NULL;
8776 else
f55ee35c 8777 return typename_concat (NULL, parent_scope, pdi->name, 0, cu);
c906108c
SS
8778}
8779
8780static void
72bf9492 8781add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
c906108c 8782{
518817b3
SM
8783 struct dwarf2_per_objfile *dwarf2_per_objfile
8784 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 8785 struct objfile *objfile = dwarf2_per_objfile->objfile;
3e29f34a 8786 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c 8787 CORE_ADDR addr = 0;
15d034d0 8788 const char *actual_name = NULL;
e142c38c 8789 CORE_ADDR baseaddr;
15d034d0 8790 char *built_actual_name;
e142c38c
DJ
8791
8792 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 8793
15d034d0
TT
8794 built_actual_name = partial_die_full_name (pdi, cu);
8795 if (built_actual_name != NULL)
8796 actual_name = built_actual_name;
63d06c5c 8797
72bf9492
DJ
8798 if (actual_name == NULL)
8799 actual_name = pdi->name;
8800
c906108c
SS
8801 switch (pdi->tag)
8802 {
b1dc1806 8803 case DW_TAG_inlined_subroutine:
c906108c 8804 case DW_TAG_subprogram:
3e29f34a 8805 addr = gdbarch_adjust_dwarf2_addr (gdbarch, pdi->lowpc + baseaddr);
2cfa0c8d 8806 if (pdi->is_external || cu->language == language_ada)
c906108c 8807 {
2cfa0c8d
JB
8808 /* brobecker/2007-12-26: Normally, only "external" DIEs are part
8809 of the global scope. But in Ada, we want to be able to access
8810 nested procedures globally. So all Ada subprograms are stored
8811 in the global scope. */
f47fb265 8812 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 8813 built_actual_name != NULL,
f47fb265
MS
8814 VAR_DOMAIN, LOC_BLOCK,
8815 &objfile->global_psymbols,
1762568f 8816 addr, cu->language, objfile);
c906108c
SS
8817 }
8818 else
8819 {
f47fb265 8820 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 8821 built_actual_name != NULL,
f47fb265
MS
8822 VAR_DOMAIN, LOC_BLOCK,
8823 &objfile->static_psymbols,
1762568f 8824 addr, cu->language, objfile);
c906108c 8825 }
0c1b455e
TT
8826
8827 if (pdi->main_subprogram && actual_name != NULL)
8828 set_objfile_main_name (objfile, actual_name, cu->language);
c906108c 8829 break;
72929c62
JB
8830 case DW_TAG_constant:
8831 {
af5bf4ad 8832 std::vector<partial_symbol *> *list;
72929c62
JB
8833
8834 if (pdi->is_external)
8835 list = &objfile->global_psymbols;
8836 else
8837 list = &objfile->static_psymbols;
f47fb265 8838 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 8839 built_actual_name != NULL, VAR_DOMAIN, LOC_STATIC,
1762568f 8840 list, 0, cu->language, objfile);
72929c62
JB
8841 }
8842 break;
c906108c 8843 case DW_TAG_variable:
95554aad
TT
8844 if (pdi->d.locdesc)
8845 addr = decode_locdesc (pdi->d.locdesc, cu);
caac4577 8846
95554aad 8847 if (pdi->d.locdesc
caac4577
JG
8848 && addr == 0
8849 && !dwarf2_per_objfile->has_section_at_zero)
8850 {
8851 /* A global or static variable may also have been stripped
8852 out by the linker if unused, in which case its address
8853 will be nullified; do not add such variables into partial
8854 symbol table then. */
8855 }
8856 else if (pdi->is_external)
c906108c
SS
8857 {
8858 /* Global Variable.
8859 Don't enter into the minimal symbol tables as there is
8860 a minimal symbol table entry from the ELF symbols already.
8861 Enter into partial symbol table if it has a location
8862 descriptor or a type.
8863 If the location descriptor is missing, new_symbol will create
8864 a LOC_UNRESOLVED symbol, the address of the variable will then
8865 be determined from the minimal symbol table whenever the variable
8866 is referenced.
8867 The address for the partial symbol table entry is not
8868 used by GDB, but it comes in handy for debugging partial symbol
8869 table building. */
8870
95554aad 8871 if (pdi->d.locdesc || pdi->has_type)
f47fb265 8872 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 8873 built_actual_name != NULL,
f47fb265
MS
8874 VAR_DOMAIN, LOC_STATIC,
8875 &objfile->global_psymbols,
1762568f 8876 addr + baseaddr,
f47fb265 8877 cu->language, objfile);
c906108c
SS
8878 }
8879 else
8880 {
ff908ebf
AW
8881 int has_loc = pdi->d.locdesc != NULL;
8882
8883 /* Static Variable. Skip symbols whose value we cannot know (those
8884 without location descriptors or constant values). */
8885 if (!has_loc && !pdi->has_const_value)
decbce07 8886 {
15d034d0 8887 xfree (built_actual_name);
decbce07
MS
8888 return;
8889 }
ff908ebf 8890
f47fb265 8891 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 8892 built_actual_name != NULL,
f47fb265
MS
8893 VAR_DOMAIN, LOC_STATIC,
8894 &objfile->static_psymbols,
ff908ebf 8895 has_loc ? addr + baseaddr : (CORE_ADDR) 0,
f47fb265 8896 cu->language, objfile);
c906108c
SS
8897 }
8898 break;
8899 case DW_TAG_typedef:
8900 case DW_TAG_base_type:
a02abb62 8901 case DW_TAG_subrange_type:
38d518c9 8902 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 8903 built_actual_name != NULL,
176620f1 8904 VAR_DOMAIN, LOC_TYPEDEF,
c906108c 8905 &objfile->static_psymbols,
1762568f 8906 0, cu->language, objfile);
c906108c 8907 break;
74921315 8908 case DW_TAG_imported_declaration:
72bf9492
DJ
8909 case DW_TAG_namespace:
8910 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 8911 built_actual_name != NULL,
72bf9492
DJ
8912 VAR_DOMAIN, LOC_TYPEDEF,
8913 &objfile->global_psymbols,
1762568f 8914 0, cu->language, objfile);
72bf9492 8915 break;
530e8392
KB
8916 case DW_TAG_module:
8917 add_psymbol_to_list (actual_name, strlen (actual_name),
8918 built_actual_name != NULL,
8919 MODULE_DOMAIN, LOC_TYPEDEF,
8920 &objfile->global_psymbols,
1762568f 8921 0, cu->language, objfile);
530e8392 8922 break;
c906108c 8923 case DW_TAG_class_type:
680b30c7 8924 case DW_TAG_interface_type:
c906108c
SS
8925 case DW_TAG_structure_type:
8926 case DW_TAG_union_type:
8927 case DW_TAG_enumeration_type:
fa4028e9
JB
8928 /* Skip external references. The DWARF standard says in the section
8929 about "Structure, Union, and Class Type Entries": "An incomplete
8930 structure, union or class type is represented by a structure,
8931 union or class entry that does not have a byte size attribute
8932 and that has a DW_AT_declaration attribute." */
8933 if (!pdi->has_byte_size && pdi->is_declaration)
decbce07 8934 {
15d034d0 8935 xfree (built_actual_name);
decbce07
MS
8936 return;
8937 }
fa4028e9 8938
63d06c5c
DC
8939 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
8940 static vs. global. */
38d518c9 8941 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 8942 built_actual_name != NULL,
176620f1 8943 STRUCT_DOMAIN, LOC_TYPEDEF,
9c37b5ae 8944 cu->language == language_cplus
63d06c5c
DC
8945 ? &objfile->global_psymbols
8946 : &objfile->static_psymbols,
1762568f 8947 0, cu->language, objfile);
c906108c 8948
c906108c
SS
8949 break;
8950 case DW_TAG_enumerator:
38d518c9 8951 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 8952 built_actual_name != NULL,
176620f1 8953 VAR_DOMAIN, LOC_CONST,
9c37b5ae 8954 cu->language == language_cplus
f6fe98ef
DJ
8955 ? &objfile->global_psymbols
8956 : &objfile->static_psymbols,
1762568f 8957 0, cu->language, objfile);
c906108c
SS
8958 break;
8959 default:
8960 break;
8961 }
5c4e30ca 8962
15d034d0 8963 xfree (built_actual_name);
c906108c
SS
8964}
8965
5c4e30ca
DC
8966/* Read a partial die corresponding to a namespace; also, add a symbol
8967 corresponding to that namespace to the symbol table. NAMESPACE is
8968 the name of the enclosing namespace. */
91c24f0a 8969
72bf9492
DJ
8970static void
8971add_partial_namespace (struct partial_die_info *pdi,
91c24f0a 8972 CORE_ADDR *lowpc, CORE_ADDR *highpc,
cdc07690 8973 int set_addrmap, struct dwarf2_cu *cu)
91c24f0a 8974{
72bf9492 8975 /* Add a symbol for the namespace. */
e7c27a73 8976
72bf9492 8977 add_partial_symbol (pdi, cu);
5c4e30ca
DC
8978
8979 /* Now scan partial symbols in that namespace. */
8980
91c24f0a 8981 if (pdi->has_children)
cdc07690 8982 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
91c24f0a
DC
8983}
8984
5d7cb8df
JK
8985/* Read a partial die corresponding to a Fortran module. */
8986
8987static void
8988add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
cdc07690 8989 CORE_ADDR *highpc, int set_addrmap, struct dwarf2_cu *cu)
5d7cb8df 8990{
530e8392
KB
8991 /* Add a symbol for the namespace. */
8992
8993 add_partial_symbol (pdi, cu);
8994
f55ee35c 8995 /* Now scan partial symbols in that module. */
5d7cb8df
JK
8996
8997 if (pdi->has_children)
cdc07690 8998 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
5d7cb8df
JK
8999}
9000
b1dc1806
XR
9001/* Read a partial die corresponding to a subprogram or an inlined
9002 subprogram and create a partial symbol for that subprogram.
9003 When the CU language allows it, this routine also defines a partial
9004 symbol for each nested subprogram that this subprogram contains.
9005 If SET_ADDRMAP is true, record the covered ranges in the addrmap.
9006 Set *LOWPC and *HIGHPC to the lowest and highest PC values found in PDI.
6e70227d 9007
cdc07690
YQ
9008 PDI may also be a lexical block, in which case we simply search
9009 recursively for subprograms defined inside that lexical block.
bc30ff58
JB
9010 Again, this is only performed when the CU language allows this
9011 type of definitions. */
9012
9013static void
9014add_partial_subprogram (struct partial_die_info *pdi,
9015 CORE_ADDR *lowpc, CORE_ADDR *highpc,
cdc07690 9016 int set_addrmap, struct dwarf2_cu *cu)
bc30ff58 9017{
b1dc1806 9018 if (pdi->tag == DW_TAG_subprogram || pdi->tag == DW_TAG_inlined_subroutine)
bc30ff58
JB
9019 {
9020 if (pdi->has_pc_info)
9021 {
9022 if (pdi->lowpc < *lowpc)
9023 *lowpc = pdi->lowpc;
9024 if (pdi->highpc > *highpc)
9025 *highpc = pdi->highpc;
cdc07690 9026 if (set_addrmap)
5734ee8b 9027 {
518817b3 9028 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
3e29f34a
MR
9029 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9030 CORE_ADDR baseaddr;
9031 CORE_ADDR highpc;
9032 CORE_ADDR lowpc;
5734ee8b
DJ
9033
9034 baseaddr = ANOFFSET (objfile->section_offsets,
9035 SECT_OFF_TEXT (objfile));
3e29f34a
MR
9036 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch,
9037 pdi->lowpc + baseaddr);
9038 highpc = gdbarch_adjust_dwarf2_addr (gdbarch,
9039 pdi->highpc + baseaddr);
9040 addrmap_set_empty (objfile->psymtabs_addrmap, lowpc, highpc - 1,
9291a0cd 9041 cu->per_cu->v.psymtab);
5734ee8b 9042 }
481860b3
GB
9043 }
9044
9045 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
9046 {
bc30ff58 9047 if (!pdi->is_declaration)
e8d05480
JB
9048 /* Ignore subprogram DIEs that do not have a name, they are
9049 illegal. Do not emit a complaint at this point, we will
9050 do so when we convert this psymtab into a symtab. */
9051 if (pdi->name)
9052 add_partial_symbol (pdi, cu);
bc30ff58
JB
9053 }
9054 }
6e70227d 9055
bc30ff58
JB
9056 if (! pdi->has_children)
9057 return;
9058
9059 if (cu->language == language_ada)
9060 {
9061 pdi = pdi->die_child;
9062 while (pdi != NULL)
9063 {
52356b79 9064 pdi->fixup (cu);
bc30ff58 9065 if (pdi->tag == DW_TAG_subprogram
b1dc1806 9066 || pdi->tag == DW_TAG_inlined_subroutine
bc30ff58 9067 || pdi->tag == DW_TAG_lexical_block)
cdc07690 9068 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
bc30ff58
JB
9069 pdi = pdi->die_sibling;
9070 }
9071 }
9072}
9073
91c24f0a
DC
9074/* Read a partial die corresponding to an enumeration type. */
9075
72bf9492
DJ
9076static void
9077add_partial_enumeration (struct partial_die_info *enum_pdi,
9078 struct dwarf2_cu *cu)
91c24f0a 9079{
72bf9492 9080 struct partial_die_info *pdi;
91c24f0a
DC
9081
9082 if (enum_pdi->name != NULL)
72bf9492
DJ
9083 add_partial_symbol (enum_pdi, cu);
9084
9085 pdi = enum_pdi->die_child;
9086 while (pdi)
91c24f0a 9087 {
72bf9492 9088 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
b98664d3 9089 complaint (_("malformed enumerator DIE ignored"));
91c24f0a 9090 else
72bf9492
DJ
9091 add_partial_symbol (pdi, cu);
9092 pdi = pdi->die_sibling;
91c24f0a 9093 }
91c24f0a
DC
9094}
9095
6caca83c
CC
9096/* Return the initial uleb128 in the die at INFO_PTR. */
9097
9098static unsigned int
d521ce57 9099peek_abbrev_code (bfd *abfd, const gdb_byte *info_ptr)
6caca83c
CC
9100{
9101 unsigned int bytes_read;
9102
9103 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9104}
9105
685af9cd
TT
9106/* Read the initial uleb128 in the die at INFO_PTR in compilation unit
9107 READER::CU. Use READER::ABBREV_TABLE to lookup any abbreviation.
9108
4bb7a0a7
DJ
9109 Return the corresponding abbrev, or NULL if the number is zero (indicating
9110 an empty DIE). In either case *BYTES_READ will be set to the length of
9111 the initial number. */
9112
9113static struct abbrev_info *
685af9cd
TT
9114peek_die_abbrev (const die_reader_specs &reader,
9115 const gdb_byte *info_ptr, unsigned int *bytes_read)
4bb7a0a7 9116{
685af9cd 9117 dwarf2_cu *cu = reader.cu;
518817b3 9118 bfd *abfd = cu->per_cu->dwarf2_per_objfile->objfile->obfd;
685af9cd
TT
9119 unsigned int abbrev_number
9120 = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
4bb7a0a7
DJ
9121
9122 if (abbrev_number == 0)
9123 return NULL;
9124
685af9cd 9125 abbrev_info *abbrev = reader.abbrev_table->lookup_abbrev (abbrev_number);
4bb7a0a7
DJ
9126 if (!abbrev)
9127 {
422b9917 9128 error (_("Dwarf Error: Could not find abbrev number %d in %s"
9d8780f0 9129 " at offset %s [in module %s]"),
422b9917 9130 abbrev_number, cu->per_cu->is_debug_types ? "TU" : "CU",
9d8780f0 9131 sect_offset_str (cu->header.sect_off), bfd_get_filename (abfd));
4bb7a0a7
DJ
9132 }
9133
9134 return abbrev;
9135}
9136
93311388
DE
9137/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
9138 Returns a pointer to the end of a series of DIEs, terminated by an empty
4bb7a0a7
DJ
9139 DIE. Any children of the skipped DIEs will also be skipped. */
9140
d521ce57
TT
9141static const gdb_byte *
9142skip_children (const struct die_reader_specs *reader, const gdb_byte *info_ptr)
4bb7a0a7 9143{
4bb7a0a7
DJ
9144 while (1)
9145 {
685af9cd
TT
9146 unsigned int bytes_read;
9147 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
9148
4bb7a0a7
DJ
9149 if (abbrev == NULL)
9150 return info_ptr + bytes_read;
9151 else
dee91e82 9152 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
4bb7a0a7
DJ
9153 }
9154}
9155
93311388
DE
9156/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
9157 INFO_PTR should point just after the initial uleb128 of a DIE, and the
4bb7a0a7
DJ
9158 abbrev corresponding to that skipped uleb128 should be passed in
9159 ABBREV. Returns a pointer to this DIE's sibling, skipping any
9160 children. */
9161
d521ce57
TT
9162static const gdb_byte *
9163skip_one_die (const struct die_reader_specs *reader, const gdb_byte *info_ptr,
dee91e82 9164 struct abbrev_info *abbrev)
4bb7a0a7
DJ
9165{
9166 unsigned int bytes_read;
9167 struct attribute attr;
dee91e82
DE
9168 bfd *abfd = reader->abfd;
9169 struct dwarf2_cu *cu = reader->cu;
d521ce57 9170 const gdb_byte *buffer = reader->buffer;
f664829e 9171 const gdb_byte *buffer_end = reader->buffer_end;
4bb7a0a7
DJ
9172 unsigned int form, i;
9173
9174 for (i = 0; i < abbrev->num_attrs; i++)
9175 {
9176 /* The only abbrev we care about is DW_AT_sibling. */
9177 if (abbrev->attrs[i].name == DW_AT_sibling)
9178 {
dee91e82 9179 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
4bb7a0a7 9180 if (attr.form == DW_FORM_ref_addr)
b98664d3 9181 complaint (_("ignoring absolute DW_AT_sibling"));
4bb7a0a7 9182 else
b9502d3f 9183 {
9c541725
PA
9184 sect_offset off = dwarf2_get_ref_die_offset (&attr);
9185 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
b9502d3f
WN
9186
9187 if (sibling_ptr < info_ptr)
b98664d3 9188 complaint (_("DW_AT_sibling points backwards"));
22869d73
KS
9189 else if (sibling_ptr > reader->buffer_end)
9190 dwarf2_section_buffer_overflow_complaint (reader->die_section);
b9502d3f
WN
9191 else
9192 return sibling_ptr;
9193 }
4bb7a0a7
DJ
9194 }
9195
9196 /* If it isn't DW_AT_sibling, skip this attribute. */
9197 form = abbrev->attrs[i].form;
9198 skip_attribute:
9199 switch (form)
9200 {
4bb7a0a7 9201 case DW_FORM_ref_addr:
ae411497
TT
9202 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
9203 and later it is offset sized. */
9204 if (cu->header.version == 2)
9205 info_ptr += cu->header.addr_size;
9206 else
9207 info_ptr += cu->header.offset_size;
9208 break;
36586728
TT
9209 case DW_FORM_GNU_ref_alt:
9210 info_ptr += cu->header.offset_size;
9211 break;
ae411497 9212 case DW_FORM_addr:
4bb7a0a7
DJ
9213 info_ptr += cu->header.addr_size;
9214 break;
9215 case DW_FORM_data1:
9216 case DW_FORM_ref1:
9217 case DW_FORM_flag:
9218 info_ptr += 1;
9219 break;
2dc7f7b3 9220 case DW_FORM_flag_present:
43988095 9221 case DW_FORM_implicit_const:
2dc7f7b3 9222 break;
4bb7a0a7
DJ
9223 case DW_FORM_data2:
9224 case DW_FORM_ref2:
9225 info_ptr += 2;
9226 break;
9227 case DW_FORM_data4:
9228 case DW_FORM_ref4:
9229 info_ptr += 4;
9230 break;
9231 case DW_FORM_data8:
9232 case DW_FORM_ref8:
55f1336d 9233 case DW_FORM_ref_sig8:
4bb7a0a7
DJ
9234 info_ptr += 8;
9235 break;
0224619f
JK
9236 case DW_FORM_data16:
9237 info_ptr += 16;
9238 break;
4bb7a0a7 9239 case DW_FORM_string:
9b1c24c8 9240 read_direct_string (abfd, info_ptr, &bytes_read);
4bb7a0a7
DJ
9241 info_ptr += bytes_read;
9242 break;
2dc7f7b3 9243 case DW_FORM_sec_offset:
4bb7a0a7 9244 case DW_FORM_strp:
36586728 9245 case DW_FORM_GNU_strp_alt:
4bb7a0a7
DJ
9246 info_ptr += cu->header.offset_size;
9247 break;
2dc7f7b3 9248 case DW_FORM_exprloc:
4bb7a0a7
DJ
9249 case DW_FORM_block:
9250 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9251 info_ptr += bytes_read;
9252 break;
9253 case DW_FORM_block1:
9254 info_ptr += 1 + read_1_byte (abfd, info_ptr);
9255 break;
9256 case DW_FORM_block2:
9257 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
9258 break;
9259 case DW_FORM_block4:
9260 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
9261 break;
9262 case DW_FORM_sdata:
9263 case DW_FORM_udata:
9264 case DW_FORM_ref_udata:
3019eac3
DE
9265 case DW_FORM_GNU_addr_index:
9266 case DW_FORM_GNU_str_index:
d521ce57 9267 info_ptr = safe_skip_leb128 (info_ptr, buffer_end);
4bb7a0a7
DJ
9268 break;
9269 case DW_FORM_indirect:
9270 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9271 info_ptr += bytes_read;
9272 /* We need to continue parsing from here, so just go back to
9273 the top. */
9274 goto skip_attribute;
9275
9276 default:
3e43a32a
MS
9277 error (_("Dwarf Error: Cannot handle %s "
9278 "in DWARF reader [in module %s]"),
4bb7a0a7
DJ
9279 dwarf_form_name (form),
9280 bfd_get_filename (abfd));
9281 }
9282 }
9283
9284 if (abbrev->has_children)
dee91e82 9285 return skip_children (reader, info_ptr);
4bb7a0a7
DJ
9286 else
9287 return info_ptr;
9288}
9289
93311388 9290/* Locate ORIG_PDI's sibling.
dee91e82 9291 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
91c24f0a 9292
d521ce57 9293static const gdb_byte *
dee91e82
DE
9294locate_pdi_sibling (const struct die_reader_specs *reader,
9295 struct partial_die_info *orig_pdi,
d521ce57 9296 const gdb_byte *info_ptr)
91c24f0a
DC
9297{
9298 /* Do we know the sibling already? */
72bf9492 9299
91c24f0a
DC
9300 if (orig_pdi->sibling)
9301 return orig_pdi->sibling;
9302
9303 /* Are there any children to deal with? */
9304
9305 if (!orig_pdi->has_children)
9306 return info_ptr;
9307
4bb7a0a7 9308 /* Skip the children the long way. */
91c24f0a 9309
dee91e82 9310 return skip_children (reader, info_ptr);
91c24f0a
DC
9311}
9312
257e7a09 9313/* Expand this partial symbol table into a full symbol table. SELF is
442e4d9c 9314 not NULL. */
c906108c
SS
9315
9316static void
257e7a09
YQ
9317dwarf2_read_symtab (struct partial_symtab *self,
9318 struct objfile *objfile)
c906108c 9319{
ed2dc618
SM
9320 struct dwarf2_per_objfile *dwarf2_per_objfile
9321 = get_dwarf2_per_objfile (objfile);
9322
257e7a09 9323 if (self->readin)
c906108c 9324 {
442e4d9c 9325 warning (_("bug: psymtab for %s is already read in."),
257e7a09 9326 self->filename);
442e4d9c
YQ
9327 }
9328 else
9329 {
9330 if (info_verbose)
c906108c 9331 {
442e4d9c 9332 printf_filtered (_("Reading in symbols for %s..."),
257e7a09 9333 self->filename);
442e4d9c 9334 gdb_flush (gdb_stdout);
c906108c 9335 }
c906108c 9336
442e4d9c
YQ
9337 /* If this psymtab is constructed from a debug-only objfile, the
9338 has_section_at_zero flag will not necessarily be correct. We
9339 can get the correct value for this flag by looking at the data
9340 associated with the (presumably stripped) associated objfile. */
9341 if (objfile->separate_debug_objfile_backlink)
9342 {
9343 struct dwarf2_per_objfile *dpo_backlink
ed2dc618 9344 = get_dwarf2_per_objfile (objfile->separate_debug_objfile_backlink);
9a619af0 9345
442e4d9c
YQ
9346 dwarf2_per_objfile->has_section_at_zero
9347 = dpo_backlink->has_section_at_zero;
9348 }
b2ab525c 9349
442e4d9c 9350 dwarf2_per_objfile->reading_partial_symbols = 0;
98bfdba5 9351
257e7a09 9352 psymtab_to_symtab_1 (self);
c906108c 9353
442e4d9c
YQ
9354 /* Finish up the debug error message. */
9355 if (info_verbose)
9356 printf_filtered (_("done.\n"));
c906108c 9357 }
95554aad 9358
ed2dc618 9359 process_cu_includes (dwarf2_per_objfile);
c906108c 9360}
9cdd5dbd
DE
9361\f
9362/* Reading in full CUs. */
c906108c 9363
10b3939b
DJ
9364/* Add PER_CU to the queue. */
9365
9366static void
95554aad
TT
9367queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
9368 enum language pretend_language)
10b3939b
DJ
9369{
9370 struct dwarf2_queue_item *item;
9371
9372 per_cu->queued = 1;
8d749320 9373 item = XNEW (struct dwarf2_queue_item);
10b3939b 9374 item->per_cu = per_cu;
95554aad 9375 item->pretend_language = pretend_language;
10b3939b
DJ
9376 item->next = NULL;
9377
9378 if (dwarf2_queue == NULL)
9379 dwarf2_queue = item;
9380 else
9381 dwarf2_queue_tail->next = item;
9382
9383 dwarf2_queue_tail = item;
9384}
9385
89e63ee4
DE
9386/* If PER_CU is not yet queued, add it to the queue.
9387 If DEPENDENT_CU is non-NULL, it has a reference to PER_CU so add a
9388 dependency.
0907af0c 9389 The result is non-zero if PER_CU was queued, otherwise the result is zero
69d751e3
DE
9390 meaning either PER_CU is already queued or it is already loaded.
9391
9392 N.B. There is an invariant here that if a CU is queued then it is loaded.
9393 The caller is required to load PER_CU if we return non-zero. */
0907af0c
DE
9394
9395static int
89e63ee4 9396maybe_queue_comp_unit (struct dwarf2_cu *dependent_cu,
0907af0c
DE
9397 struct dwarf2_per_cu_data *per_cu,
9398 enum language pretend_language)
9399{
9400 /* We may arrive here during partial symbol reading, if we need full
9401 DIEs to process an unusual case (e.g. template arguments). Do
9402 not queue PER_CU, just tell our caller to load its DIEs. */
ed2dc618 9403 if (per_cu->dwarf2_per_objfile->reading_partial_symbols)
0907af0c
DE
9404 {
9405 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
9406 return 1;
9407 return 0;
9408 }
9409
9410 /* Mark the dependence relation so that we don't flush PER_CU
9411 too early. */
89e63ee4
DE
9412 if (dependent_cu != NULL)
9413 dwarf2_add_dependence (dependent_cu, per_cu);
0907af0c
DE
9414
9415 /* If it's already on the queue, we have nothing to do. */
9416 if (per_cu->queued)
9417 return 0;
9418
9419 /* If the compilation unit is already loaded, just mark it as
9420 used. */
9421 if (per_cu->cu != NULL)
9422 {
9423 per_cu->cu->last_used = 0;
9424 return 0;
9425 }
9426
9427 /* Add it to the queue. */
9428 queue_comp_unit (per_cu, pretend_language);
9429
9430 return 1;
9431}
9432
10b3939b
DJ
9433/* Process the queue. */
9434
9435static void
ed2dc618 9436process_queue (struct dwarf2_per_objfile *dwarf2_per_objfile)
10b3939b
DJ
9437{
9438 struct dwarf2_queue_item *item, *next_item;
9439
b4f54984 9440 if (dwarf_read_debug)
45cfd468
DE
9441 {
9442 fprintf_unfiltered (gdb_stdlog,
9443 "Expanding one or more symtabs of objfile %s ...\n",
4262abfb 9444 objfile_name (dwarf2_per_objfile->objfile));
45cfd468
DE
9445 }
9446
03dd20cc
DJ
9447 /* The queue starts out with one item, but following a DIE reference
9448 may load a new CU, adding it to the end of the queue. */
10b3939b
DJ
9449 for (item = dwarf2_queue; item != NULL; dwarf2_queue = item = next_item)
9450 {
cc12ce38
DE
9451 if ((dwarf2_per_objfile->using_index
9452 ? !item->per_cu->v.quick->compunit_symtab
9453 : (item->per_cu->v.psymtab && !item->per_cu->v.psymtab->readin))
9454 /* Skip dummy CUs. */
9455 && item->per_cu->cu != NULL)
f4dc4d17
DE
9456 {
9457 struct dwarf2_per_cu_data *per_cu = item->per_cu;
73be47f5 9458 unsigned int debug_print_threshold;
247f5c4f 9459 char buf[100];
f4dc4d17 9460
247f5c4f 9461 if (per_cu->is_debug_types)
f4dc4d17 9462 {
247f5c4f
DE
9463 struct signatured_type *sig_type =
9464 (struct signatured_type *) per_cu;
9465
9d8780f0 9466 sprintf (buf, "TU %s at offset %s",
73be47f5 9467 hex_string (sig_type->signature),
9d8780f0 9468 sect_offset_str (per_cu->sect_off));
73be47f5
DE
9469 /* There can be 100s of TUs.
9470 Only print them in verbose mode. */
9471 debug_print_threshold = 2;
f4dc4d17 9472 }
247f5c4f 9473 else
73be47f5 9474 {
9d8780f0
SM
9475 sprintf (buf, "CU at offset %s",
9476 sect_offset_str (per_cu->sect_off));
73be47f5
DE
9477 debug_print_threshold = 1;
9478 }
247f5c4f 9479
b4f54984 9480 if (dwarf_read_debug >= debug_print_threshold)
247f5c4f 9481 fprintf_unfiltered (gdb_stdlog, "Expanding symtab of %s\n", buf);
f4dc4d17
DE
9482
9483 if (per_cu->is_debug_types)
9484 process_full_type_unit (per_cu, item->pretend_language);
9485 else
9486 process_full_comp_unit (per_cu, item->pretend_language);
9487
b4f54984 9488 if (dwarf_read_debug >= debug_print_threshold)
247f5c4f 9489 fprintf_unfiltered (gdb_stdlog, "Done expanding %s\n", buf);
f4dc4d17 9490 }
10b3939b
DJ
9491
9492 item->per_cu->queued = 0;
9493 next_item = item->next;
9494 xfree (item);
9495 }
9496
9497 dwarf2_queue_tail = NULL;
45cfd468 9498
b4f54984 9499 if (dwarf_read_debug)
45cfd468
DE
9500 {
9501 fprintf_unfiltered (gdb_stdlog, "Done expanding symtabs of %s.\n",
4262abfb 9502 objfile_name (dwarf2_per_objfile->objfile));
45cfd468 9503 }
10b3939b
DJ
9504}
9505
10b3939b
DJ
9506/* Read in full symbols for PST, and anything it depends on. */
9507
c906108c 9508static void
fba45db2 9509psymtab_to_symtab_1 (struct partial_symtab *pst)
c906108c 9510{
10b3939b 9511 struct dwarf2_per_cu_data *per_cu;
aaa75496
JB
9512 int i;
9513
95554aad
TT
9514 if (pst->readin)
9515 return;
9516
aaa75496 9517 for (i = 0; i < pst->number_of_dependencies; i++)
95554aad
TT
9518 if (!pst->dependencies[i]->readin
9519 && pst->dependencies[i]->user == NULL)
aaa75496
JB
9520 {
9521 /* Inform about additional files that need to be read in. */
9522 if (info_verbose)
9523 {
a3f17187 9524 /* FIXME: i18n: Need to make this a single string. */
aaa75496
JB
9525 fputs_filtered (" ", gdb_stdout);
9526 wrap_here ("");
9527 fputs_filtered ("and ", gdb_stdout);
9528 wrap_here ("");
9529 printf_filtered ("%s...", pst->dependencies[i]->filename);
0963b4bd 9530 wrap_here (""); /* Flush output. */
aaa75496
JB
9531 gdb_flush (gdb_stdout);
9532 }
9533 psymtab_to_symtab_1 (pst->dependencies[i]);
9534 }
9535
9a3c8263 9536 per_cu = (struct dwarf2_per_cu_data *) pst->read_symtab_private;
10b3939b
DJ
9537
9538 if (per_cu == NULL)
aaa75496
JB
9539 {
9540 /* It's an include file, no symbols to read for it.
9541 Everything is in the parent symtab. */
9542 pst->readin = 1;
9543 return;
9544 }
c906108c 9545
58f0c718 9546 dw2_do_instantiate_symtab (per_cu, false);
10b3939b
DJ
9547}
9548
dee91e82
DE
9549/* Trivial hash function for die_info: the hash value of a DIE
9550 is its offset in .debug_info for this objfile. */
10b3939b 9551
dee91e82
DE
9552static hashval_t
9553die_hash (const void *item)
10b3939b 9554{
9a3c8263 9555 const struct die_info *die = (const struct die_info *) item;
6502dd73 9556
9c541725 9557 return to_underlying (die->sect_off);
dee91e82 9558}
63d06c5c 9559
dee91e82
DE
9560/* Trivial comparison function for die_info structures: two DIEs
9561 are equal if they have the same offset. */
98bfdba5 9562
dee91e82
DE
9563static int
9564die_eq (const void *item_lhs, const void *item_rhs)
9565{
9a3c8263
SM
9566 const struct die_info *die_lhs = (const struct die_info *) item_lhs;
9567 const struct die_info *die_rhs = (const struct die_info *) item_rhs;
c906108c 9568
9c541725 9569 return die_lhs->sect_off == die_rhs->sect_off;
dee91e82 9570}
c906108c 9571
dee91e82
DE
9572/* die_reader_func for load_full_comp_unit.
9573 This is identical to read_signatured_type_reader,
9574 but is kept separate for now. */
c906108c 9575
dee91e82
DE
9576static void
9577load_full_comp_unit_reader (const struct die_reader_specs *reader,
d521ce57 9578 const gdb_byte *info_ptr,
dee91e82
DE
9579 struct die_info *comp_unit_die,
9580 int has_children,
9581 void *data)
9582{
9583 struct dwarf2_cu *cu = reader->cu;
9a3c8263 9584 enum language *language_ptr = (enum language *) data;
6caca83c 9585
dee91e82
DE
9586 gdb_assert (cu->die_hash == NULL);
9587 cu->die_hash =
9588 htab_create_alloc_ex (cu->header.length / 12,
9589 die_hash,
9590 die_eq,
9591 NULL,
9592 &cu->comp_unit_obstack,
9593 hashtab_obstack_allocate,
9594 dummy_obstack_deallocate);
e142c38c 9595
dee91e82
DE
9596 if (has_children)
9597 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
9598 &info_ptr, comp_unit_die);
9599 cu->dies = comp_unit_die;
9600 /* comp_unit_die is not stored in die_hash, no need. */
10b3939b
DJ
9601
9602 /* We try not to read any attributes in this function, because not
9cdd5dbd 9603 all CUs needed for references have been loaded yet, and symbol
10b3939b 9604 table processing isn't initialized. But we have to set the CU language,
dee91e82
DE
9605 or we won't be able to build types correctly.
9606 Similarly, if we do not read the producer, we can not apply
9607 producer-specific interpretation. */
95554aad 9608 prepare_one_comp_unit (cu, cu->dies, *language_ptr);
dee91e82 9609}
10b3939b 9610
dee91e82 9611/* Load the DIEs associated with PER_CU into memory. */
a6c727b2 9612
dee91e82 9613static void
95554aad 9614load_full_comp_unit (struct dwarf2_per_cu_data *this_cu,
58f0c718 9615 bool skip_partial,
95554aad 9616 enum language pretend_language)
dee91e82 9617{
3019eac3 9618 gdb_assert (! this_cu->is_debug_types);
c5b7e1cb 9619
58f0c718 9620 init_cutu_and_read_dies (this_cu, NULL, 1, 1, skip_partial,
f4dc4d17 9621 load_full_comp_unit_reader, &pretend_language);
10b3939b
DJ
9622}
9623
3da10d80
KS
9624/* Add a DIE to the delayed physname list. */
9625
9626static void
9627add_to_method_list (struct type *type, int fnfield_index, int index,
9628 const char *name, struct die_info *die,
9629 struct dwarf2_cu *cu)
9630{
9631 struct delayed_method_info mi;
9632 mi.type = type;
9633 mi.fnfield_index = fnfield_index;
9634 mi.index = index;
9635 mi.name = name;
9636 mi.die = die;
c89b44cd 9637 cu->method_list.push_back (mi);
3da10d80
KS
9638}
9639
3693fdb3
PA
9640/* Check whether [PHYSNAME, PHYSNAME+LEN) ends with a modifier like
9641 "const" / "volatile". If so, decrements LEN by the length of the
9642 modifier and return true. Otherwise return false. */
9643
9644template<size_t N>
9645static bool
9646check_modifier (const char *physname, size_t &len, const char (&mod)[N])
9647{
9648 size_t mod_len = sizeof (mod) - 1;
9649 if (len > mod_len && startswith (physname + (len - mod_len), mod))
9650 {
9651 len -= mod_len;
9652 return true;
9653 }
9654 return false;
9655}
9656
3da10d80
KS
9657/* Compute the physnames of any methods on the CU's method list.
9658
9659 The computation of method physnames is delayed in order to avoid the
9660 (bad) condition that one of the method's formal parameters is of an as yet
9661 incomplete type. */
9662
9663static void
9664compute_delayed_physnames (struct dwarf2_cu *cu)
9665{
3693fdb3 9666 /* Only C++ delays computing physnames. */
c89b44cd 9667 if (cu->method_list.empty ())
3693fdb3
PA
9668 return;
9669 gdb_assert (cu->language == language_cplus);
9670
52941706 9671 for (const delayed_method_info &mi : cu->method_list)
3da10d80 9672 {
1d06ead6 9673 const char *physname;
3da10d80 9674 struct fn_fieldlist *fn_flp
c89b44cd
TT
9675 = &TYPE_FN_FIELDLIST (mi.type, mi.fnfield_index);
9676 physname = dwarf2_physname (mi.name, mi.die, cu);
9677 TYPE_FN_FIELD_PHYSNAME (fn_flp->fn_fields, mi.index)
005e54bb 9678 = physname ? physname : "";
3693fdb3
PA
9679
9680 /* Since there's no tag to indicate whether a method is a
9681 const/volatile overload, extract that information out of the
9682 demangled name. */
9683 if (physname != NULL)
9684 {
9685 size_t len = strlen (physname);
9686
9687 while (1)
9688 {
9689 if (physname[len] == ')') /* shortcut */
9690 break;
9691 else if (check_modifier (physname, len, " const"))
c89b44cd 9692 TYPE_FN_FIELD_CONST (fn_flp->fn_fields, mi.index) = 1;
3693fdb3 9693 else if (check_modifier (physname, len, " volatile"))
c89b44cd 9694 TYPE_FN_FIELD_VOLATILE (fn_flp->fn_fields, mi.index) = 1;
3693fdb3
PA
9695 else
9696 break;
9697 }
9698 }
3da10d80 9699 }
c89b44cd
TT
9700
9701 /* The list is no longer needed. */
9702 cu->method_list.clear ();
3da10d80
KS
9703}
9704
a766d390
DE
9705/* Go objects should be embedded in a DW_TAG_module DIE,
9706 and it's not clear if/how imported objects will appear.
9707 To keep Go support simple until that's worked out,
9708 go back through what we've read and create something usable.
9709 We could do this while processing each DIE, and feels kinda cleaner,
9710 but that way is more invasive.
9711 This is to, for example, allow the user to type "p var" or "b main"
9712 without having to specify the package name, and allow lookups
9713 of module.object to work in contexts that use the expression
9714 parser. */
9715
9716static void
9717fixup_go_packaging (struct dwarf2_cu *cu)
9718{
9719 char *package_name = NULL;
9720 struct pending *list;
9721 int i;
9722
804d2729
TT
9723 for (list = *cu->builder->get_global_symbols ();
9724 list != NULL;
9725 list = list->next)
a766d390
DE
9726 {
9727 for (i = 0; i < list->nsyms; ++i)
9728 {
9729 struct symbol *sym = list->symbol[i];
9730
9731 if (SYMBOL_LANGUAGE (sym) == language_go
9732 && SYMBOL_CLASS (sym) == LOC_BLOCK)
9733 {
9734 char *this_package_name = go_symbol_package_name (sym);
9735
9736 if (this_package_name == NULL)
9737 continue;
9738 if (package_name == NULL)
9739 package_name = this_package_name;
9740 else
9741 {
518817b3
SM
9742 struct objfile *objfile
9743 = cu->per_cu->dwarf2_per_objfile->objfile;
a766d390 9744 if (strcmp (package_name, this_package_name) != 0)
b98664d3 9745 complaint (_("Symtab %s has objects from two different Go packages: %s and %s"),
08be3fe3
DE
9746 (symbol_symtab (sym) != NULL
9747 ? symtab_to_filename_for_display
9748 (symbol_symtab (sym))
e3b94546 9749 : objfile_name (objfile)),
a766d390
DE
9750 this_package_name, package_name);
9751 xfree (this_package_name);
9752 }
9753 }
9754 }
9755 }
9756
9757 if (package_name != NULL)
9758 {
518817b3 9759 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
34a68019 9760 const char *saved_package_name
224c3ddb
SM
9761 = (const char *) obstack_copy0 (&objfile->per_bfd->storage_obstack,
9762 package_name,
9763 strlen (package_name));
19f392bc
UW
9764 struct type *type = init_type (objfile, TYPE_CODE_MODULE, 0,
9765 saved_package_name);
a766d390
DE
9766 struct symbol *sym;
9767
e623cf5d 9768 sym = allocate_symbol (objfile);
f85f34ed 9769 SYMBOL_SET_LANGUAGE (sym, language_go, &objfile->objfile_obstack);
86f62fd7
TT
9770 SYMBOL_SET_NAMES (sym, saved_package_name,
9771 strlen (saved_package_name), 0, objfile);
a766d390
DE
9772 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
9773 e.g., "main" finds the "main" module and not C's main(). */
9774 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
f1e6e072 9775 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
a766d390
DE
9776 SYMBOL_TYPE (sym) = type;
9777
804d2729 9778 add_symbol_to_list (sym, cu->builder->get_global_symbols ());
a766d390
DE
9779
9780 xfree (package_name);
9781 }
9782}
9783
c9317f21
TT
9784/* Allocate a fully-qualified name consisting of the two parts on the
9785 obstack. */
9786
9787static const char *
9788rust_fully_qualify (struct obstack *obstack, const char *p1, const char *p2)
9789{
9790 return obconcat (obstack, p1, "::", p2, (char *) NULL);
9791}
9792
9793/* A helper that allocates a struct discriminant_info to attach to a
9794 union type. */
9795
9796static struct discriminant_info *
9797alloc_discriminant_info (struct type *type, int discriminant_index,
9798 int default_index)
9799{
9800 gdb_assert (TYPE_CODE (type) == TYPE_CODE_UNION);
c7b15a66
TT
9801 gdb_assert (discriminant_index == -1
9802 || (discriminant_index >= 0
9803 && discriminant_index < TYPE_NFIELDS (type)));
c9317f21 9804 gdb_assert (default_index == -1
c7b15a66 9805 || (default_index >= 0 && default_index < TYPE_NFIELDS (type)));
c9317f21
TT
9806
9807 TYPE_FLAG_DISCRIMINATED_UNION (type) = 1;
9808
9809 struct discriminant_info *disc
9810 = ((struct discriminant_info *)
9811 TYPE_ZALLOC (type,
9812 offsetof (struct discriminant_info, discriminants)
9813 + TYPE_NFIELDS (type) * sizeof (disc->discriminants[0])));
9814 disc->default_index = default_index;
9815 disc->discriminant_index = discriminant_index;
9816
9817 struct dynamic_prop prop;
9818 prop.kind = PROP_UNDEFINED;
9819 prop.data.baton = disc;
9820
9821 add_dyn_prop (DYN_PROP_DISCRIMINATED, prop, type);
9822
9823 return disc;
9824}
9825
9826/* Some versions of rustc emitted enums in an unusual way.
9827
9828 Ordinary enums were emitted as unions. The first element of each
9829 structure in the union was named "RUST$ENUM$DISR". This element
9830 held the discriminant.
9831
9832 These versions of Rust also implemented the "non-zero"
9833 optimization. When the enum had two values, and one is empty and
9834 the other holds a pointer that cannot be zero, the pointer is used
9835 as the discriminant, with a zero value meaning the empty variant.
9836 Here, the union's first member is of the form
9837 RUST$ENCODED$ENUM$<fieldno>$<fieldno>$...$<variantname>
9838 where the fieldnos are the indices of the fields that should be
9839 traversed in order to find the field (which may be several fields deep)
9840 and the variantname is the name of the variant of the case when the
9841 field is zero.
9842
9843 This function recognizes whether TYPE is of one of these forms,
9844 and, if so, smashes it to be a variant type. */
9845
9846static void
9847quirk_rust_enum (struct type *type, struct objfile *objfile)
9848{
9849 gdb_assert (TYPE_CODE (type) == TYPE_CODE_UNION);
9850
9851 /* We don't need to deal with empty enums. */
9852 if (TYPE_NFIELDS (type) == 0)
9853 return;
9854
9855#define RUST_ENUM_PREFIX "RUST$ENCODED$ENUM$"
9856 if (TYPE_NFIELDS (type) == 1
9857 && startswith (TYPE_FIELD_NAME (type, 0), RUST_ENUM_PREFIX))
9858 {
9859 const char *name = TYPE_FIELD_NAME (type, 0) + strlen (RUST_ENUM_PREFIX);
9860
9861 /* Decode the field name to find the offset of the
9862 discriminant. */
9863 ULONGEST bit_offset = 0;
9864 struct type *field_type = TYPE_FIELD_TYPE (type, 0);
9865 while (name[0] >= '0' && name[0] <= '9')
9866 {
9867 char *tail;
9868 unsigned long index = strtoul (name, &tail, 10);
9869 name = tail;
9870 if (*name != '$'
9871 || index >= TYPE_NFIELDS (field_type)
9872 || (TYPE_FIELD_LOC_KIND (field_type, index)
9873 != FIELD_LOC_KIND_BITPOS))
9874 {
b98664d3 9875 complaint (_("Could not parse Rust enum encoding string \"%s\""
c9317f21
TT
9876 "[in module %s]"),
9877 TYPE_FIELD_NAME (type, 0),
9878 objfile_name (objfile));
9879 return;
9880 }
9881 ++name;
9882
9883 bit_offset += TYPE_FIELD_BITPOS (field_type, index);
9884 field_type = TYPE_FIELD_TYPE (field_type, index);
9885 }
9886
9887 /* Make a union to hold the variants. */
9888 struct type *union_type = alloc_type (objfile);
9889 TYPE_CODE (union_type) = TYPE_CODE_UNION;
9890 TYPE_NFIELDS (union_type) = 3;
9891 TYPE_FIELDS (union_type)
9892 = (struct field *) TYPE_ZALLOC (type, 3 * sizeof (struct field));
9893 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
2b4424c3 9894 set_type_align (union_type, TYPE_RAW_ALIGN (type));
c9317f21
TT
9895
9896 /* Put the discriminant must at index 0. */
9897 TYPE_FIELD_TYPE (union_type, 0) = field_type;
9898 TYPE_FIELD_ARTIFICIAL (union_type, 0) = 1;
9899 TYPE_FIELD_NAME (union_type, 0) = "<<discriminant>>";
9900 SET_FIELD_BITPOS (TYPE_FIELD (union_type, 0), bit_offset);
9901
9902 /* The order of fields doesn't really matter, so put the real
9903 field at index 1 and the data-less field at index 2. */
9904 struct discriminant_info *disc
9905 = alloc_discriminant_info (union_type, 0, 1);
9906 TYPE_FIELD (union_type, 1) = TYPE_FIELD (type, 0);
9907 TYPE_FIELD_NAME (union_type, 1)
9908 = rust_last_path_segment (TYPE_NAME (TYPE_FIELD_TYPE (union_type, 1)));
9909 TYPE_NAME (TYPE_FIELD_TYPE (union_type, 1))
9910 = rust_fully_qualify (&objfile->objfile_obstack, TYPE_NAME (type),
9911 TYPE_FIELD_NAME (union_type, 1));
9912
9913 const char *dataless_name
9914 = rust_fully_qualify (&objfile->objfile_obstack, TYPE_NAME (type),
9915 name);
9916 struct type *dataless_type = init_type (objfile, TYPE_CODE_VOID, 0,
9917 dataless_name);
9918 TYPE_FIELD_TYPE (union_type, 2) = dataless_type;
9919 /* NAME points into the original discriminant name, which
9920 already has the correct lifetime. */
9921 TYPE_FIELD_NAME (union_type, 2) = name;
9922 SET_FIELD_BITPOS (TYPE_FIELD (union_type, 2), 0);
9923 disc->discriminants[2] = 0;
9924
9925 /* Smash this type to be a structure type. We have to do this
9926 because the type has already been recorded. */
9927 TYPE_CODE (type) = TYPE_CODE_STRUCT;
9928 TYPE_NFIELDS (type) = 1;
9929 TYPE_FIELDS (type)
9930 = (struct field *) TYPE_ZALLOC (type, sizeof (struct field));
9931
9932 /* Install the variant part. */
9933 TYPE_FIELD_TYPE (type, 0) = union_type;
9934 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
9935 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
9936 }
9937 else if (TYPE_NFIELDS (type) == 1)
9938 {
9939 /* We assume that a union with a single field is a univariant
9940 enum. */
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
9945 /* Make a union to hold the variants. */
9946 struct type *union_type = alloc_type (objfile);
9947 TYPE_CODE (union_type) = TYPE_CODE_UNION;
9948 TYPE_NFIELDS (union_type) = TYPE_NFIELDS (type);
9949 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
2b4424c3 9950 set_type_align (union_type, TYPE_RAW_ALIGN (type));
c9317f21
TT
9951 TYPE_FIELDS (union_type) = TYPE_FIELDS (type);
9952
9953 struct type *field_type = TYPE_FIELD_TYPE (union_type, 0);
9954 const char *variant_name
9955 = rust_last_path_segment (TYPE_NAME (field_type));
9956 TYPE_FIELD_NAME (union_type, 0) = variant_name;
9957 TYPE_NAME (field_type)
9958 = rust_fully_qualify (&objfile->objfile_obstack,
c7b15a66 9959 TYPE_NAME (type), variant_name);
c9317f21
TT
9960
9961 /* Install the union in the outer struct type. */
9962 TYPE_NFIELDS (type) = 1;
9963 TYPE_FIELDS (type)
9964 = (struct field *) TYPE_ZALLOC (union_type, sizeof (struct field));
9965 TYPE_FIELD_TYPE (type, 0) = union_type;
9966 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
9967 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
9968
9969 alloc_discriminant_info (union_type, -1, 0);
9970 }
9971 else
9972 {
9973 struct type *disr_type = nullptr;
9974 for (int i = 0; i < TYPE_NFIELDS (type); ++i)
9975 {
9976 disr_type = TYPE_FIELD_TYPE (type, i);
9977
a037790e
TT
9978 if (TYPE_CODE (disr_type) != TYPE_CODE_STRUCT)
9979 {
9980 /* All fields of a true enum will be structs. */
9981 return;
9982 }
9983 else if (TYPE_NFIELDS (disr_type) == 0)
c9317f21
TT
9984 {
9985 /* Could be data-less variant, so keep going. */
a037790e 9986 disr_type = nullptr;
c9317f21
TT
9987 }
9988 else if (strcmp (TYPE_FIELD_NAME (disr_type, 0),
9989 "RUST$ENUM$DISR") != 0)
9990 {
9991 /* Not a Rust enum. */
9992 return;
9993 }
9994 else
9995 {
9996 /* Found one. */
9997 break;
9998 }
9999 }
10000
10001 /* If we got here without a discriminant, then it's probably
10002 just a union. */
10003 if (disr_type == nullptr)
10004 return;
10005
10006 /* Smash this type to be a structure type. We have to do this
10007 because the type has already been recorded. */
10008 TYPE_CODE (type) = TYPE_CODE_STRUCT;
10009
10010 /* Make a union to hold the variants. */
10011 struct field *disr_field = &TYPE_FIELD (disr_type, 0);
10012 struct type *union_type = alloc_type (objfile);
10013 TYPE_CODE (union_type) = TYPE_CODE_UNION;
10014 TYPE_NFIELDS (union_type) = 1 + TYPE_NFIELDS (type);
10015 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
2b4424c3 10016 set_type_align (union_type, TYPE_RAW_ALIGN (type));
c9317f21
TT
10017 TYPE_FIELDS (union_type)
10018 = (struct field *) TYPE_ZALLOC (union_type,
10019 (TYPE_NFIELDS (union_type)
10020 * sizeof (struct field)));
10021
10022 memcpy (TYPE_FIELDS (union_type) + 1, TYPE_FIELDS (type),
10023 TYPE_NFIELDS (type) * sizeof (struct field));
10024
10025 /* Install the discriminant at index 0 in the union. */
10026 TYPE_FIELD (union_type, 0) = *disr_field;
10027 TYPE_FIELD_ARTIFICIAL (union_type, 0) = 1;
10028 TYPE_FIELD_NAME (union_type, 0) = "<<discriminant>>";
10029
10030 /* Install the union in the outer struct type. */
10031 TYPE_FIELD_TYPE (type, 0) = union_type;
10032 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
10033 TYPE_NFIELDS (type) = 1;
10034
10035 /* Set the size and offset of the union type. */
10036 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
10037
10038 /* We need a way to find the correct discriminant given a
10039 variant name. For convenience we build a map here. */
10040 struct type *enum_type = FIELD_TYPE (*disr_field);
10041 std::unordered_map<std::string, ULONGEST> discriminant_map;
10042 for (int i = 0; i < TYPE_NFIELDS (enum_type); ++i)
10043 {
10044 if (TYPE_FIELD_LOC_KIND (enum_type, i) == FIELD_LOC_KIND_ENUMVAL)
10045 {
10046 const char *name
10047 = rust_last_path_segment (TYPE_FIELD_NAME (enum_type, i));
10048 discriminant_map[name] = TYPE_FIELD_ENUMVAL (enum_type, i);
10049 }
10050 }
10051
10052 int n_fields = TYPE_NFIELDS (union_type);
10053 struct discriminant_info *disc
10054 = alloc_discriminant_info (union_type, 0, -1);
10055 /* Skip the discriminant here. */
10056 for (int i = 1; i < n_fields; ++i)
10057 {
10058 /* Find the final word in the name of this variant's type.
10059 That name can be used to look up the correct
10060 discriminant. */
10061 const char *variant_name
10062 = rust_last_path_segment (TYPE_NAME (TYPE_FIELD_TYPE (union_type,
10063 i)));
10064
10065 auto iter = discriminant_map.find (variant_name);
10066 if (iter != discriminant_map.end ())
10067 disc->discriminants[i] = iter->second;
10068
bedda9ac 10069 /* Remove the discriminant field, if it exists. */
c9317f21 10070 struct type *sub_type = TYPE_FIELD_TYPE (union_type, i);
bedda9ac
TT
10071 if (TYPE_NFIELDS (sub_type) > 0)
10072 {
10073 --TYPE_NFIELDS (sub_type);
10074 ++TYPE_FIELDS (sub_type);
10075 }
c9317f21
TT
10076 TYPE_FIELD_NAME (union_type, i) = variant_name;
10077 TYPE_NAME (sub_type)
10078 = rust_fully_qualify (&objfile->objfile_obstack,
10079 TYPE_NAME (type), variant_name);
10080 }
10081 }
10082}
10083
10084/* Rewrite some Rust unions to be structures with variants parts. */
10085
10086static void
10087rust_union_quirks (struct dwarf2_cu *cu)
10088{
10089 gdb_assert (cu->language == language_rust);
52941706
SM
10090 for (type *type_ : cu->rust_unions)
10091 quirk_rust_enum (type_, cu->per_cu->dwarf2_per_objfile->objfile);
2d79090e
TT
10092 /* We don't need this any more. */
10093 cu->rust_unions.clear ();
c9317f21
TT
10094}
10095
95554aad
TT
10096/* Return the symtab for PER_CU. This works properly regardless of
10097 whether we're using the index or psymtabs. */
10098
43f3e411
DE
10099static struct compunit_symtab *
10100get_compunit_symtab (struct dwarf2_per_cu_data *per_cu)
95554aad 10101{
ed2dc618 10102 return (per_cu->dwarf2_per_objfile->using_index
43f3e411
DE
10103 ? per_cu->v.quick->compunit_symtab
10104 : per_cu->v.psymtab->compunit_symtab);
95554aad
TT
10105}
10106
10107/* A helper function for computing the list of all symbol tables
10108 included by PER_CU. */
10109
10110static void
43f3e411 10111recursively_compute_inclusions (VEC (compunit_symtab_ptr) **result,
ec94af83 10112 htab_t all_children, htab_t all_type_symtabs,
f9125b6c 10113 struct dwarf2_per_cu_data *per_cu,
43f3e411 10114 struct compunit_symtab *immediate_parent)
95554aad
TT
10115{
10116 void **slot;
10117 int ix;
43f3e411 10118 struct compunit_symtab *cust;
95554aad
TT
10119 struct dwarf2_per_cu_data *iter;
10120
10121 slot = htab_find_slot (all_children, per_cu, INSERT);
10122 if (*slot != NULL)
10123 {
10124 /* This inclusion and its children have been processed. */
10125 return;
10126 }
10127
10128 *slot = per_cu;
10129 /* Only add a CU if it has a symbol table. */
43f3e411
DE
10130 cust = get_compunit_symtab (per_cu);
10131 if (cust != NULL)
ec94af83
DE
10132 {
10133 /* If this is a type unit only add its symbol table if we haven't
10134 seen it yet (type unit per_cu's can share symtabs). */
10135 if (per_cu->is_debug_types)
10136 {
43f3e411 10137 slot = htab_find_slot (all_type_symtabs, cust, INSERT);
ec94af83
DE
10138 if (*slot == NULL)
10139 {
43f3e411
DE
10140 *slot = cust;
10141 VEC_safe_push (compunit_symtab_ptr, *result, cust);
10142 if (cust->user == NULL)
10143 cust->user = immediate_parent;
ec94af83
DE
10144 }
10145 }
10146 else
f9125b6c 10147 {
43f3e411
DE
10148 VEC_safe_push (compunit_symtab_ptr, *result, cust);
10149 if (cust->user == NULL)
10150 cust->user = immediate_parent;
f9125b6c 10151 }
ec94af83 10152 }
95554aad
TT
10153
10154 for (ix = 0;
796a7ff8 10155 VEC_iterate (dwarf2_per_cu_ptr, per_cu->imported_symtabs, ix, iter);
95554aad 10156 ++ix)
ec94af83
DE
10157 {
10158 recursively_compute_inclusions (result, all_children,
43f3e411 10159 all_type_symtabs, iter, cust);
ec94af83 10160 }
95554aad
TT
10161}
10162
43f3e411 10163/* Compute the compunit_symtab 'includes' fields for the compunit_symtab of
95554aad
TT
10164 PER_CU. */
10165
10166static void
43f3e411 10167compute_compunit_symtab_includes (struct dwarf2_per_cu_data *per_cu)
95554aad 10168{
f4dc4d17
DE
10169 gdb_assert (! per_cu->is_debug_types);
10170
796a7ff8 10171 if (!VEC_empty (dwarf2_per_cu_ptr, per_cu->imported_symtabs))
95554aad
TT
10172 {
10173 int ix, len;
ec94af83 10174 struct dwarf2_per_cu_data *per_cu_iter;
43f3e411
DE
10175 struct compunit_symtab *compunit_symtab_iter;
10176 VEC (compunit_symtab_ptr) *result_symtabs = NULL;
ec94af83 10177 htab_t all_children, all_type_symtabs;
43f3e411 10178 struct compunit_symtab *cust = get_compunit_symtab (per_cu);
95554aad
TT
10179
10180 /* If we don't have a symtab, we can just skip this case. */
43f3e411 10181 if (cust == NULL)
95554aad
TT
10182 return;
10183
10184 all_children = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
10185 NULL, xcalloc, xfree);
ec94af83
DE
10186 all_type_symtabs = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
10187 NULL, xcalloc, xfree);
95554aad
TT
10188
10189 for (ix = 0;
796a7ff8 10190 VEC_iterate (dwarf2_per_cu_ptr, per_cu->imported_symtabs,
ec94af83 10191 ix, per_cu_iter);
95554aad 10192 ++ix)
ec94af83
DE
10193 {
10194 recursively_compute_inclusions (&result_symtabs, all_children,
f9125b6c 10195 all_type_symtabs, per_cu_iter,
43f3e411 10196 cust);
ec94af83 10197 }
95554aad 10198
ec94af83 10199 /* Now we have a transitive closure of all the included symtabs. */
43f3e411
DE
10200 len = VEC_length (compunit_symtab_ptr, result_symtabs);
10201 cust->includes
ed2dc618 10202 = XOBNEWVEC (&per_cu->dwarf2_per_objfile->objfile->objfile_obstack,
8d749320 10203 struct compunit_symtab *, len + 1);
95554aad 10204 for (ix = 0;
43f3e411
DE
10205 VEC_iterate (compunit_symtab_ptr, result_symtabs, ix,
10206 compunit_symtab_iter);
95554aad 10207 ++ix)
43f3e411
DE
10208 cust->includes[ix] = compunit_symtab_iter;
10209 cust->includes[len] = NULL;
95554aad 10210
43f3e411 10211 VEC_free (compunit_symtab_ptr, result_symtabs);
95554aad 10212 htab_delete (all_children);
ec94af83 10213 htab_delete (all_type_symtabs);
95554aad
TT
10214 }
10215}
10216
10217/* Compute the 'includes' field for the symtabs of all the CUs we just
10218 read. */
10219
10220static void
ed2dc618 10221process_cu_includes (struct dwarf2_per_objfile *dwarf2_per_objfile)
95554aad 10222{
71b73764 10223 for (dwarf2_per_cu_data *iter : dwarf2_per_objfile->just_read_cus)
f4dc4d17
DE
10224 {
10225 if (! iter->is_debug_types)
43f3e411 10226 compute_compunit_symtab_includes (iter);
f4dc4d17 10227 }
95554aad 10228
c5d0225d 10229 dwarf2_per_objfile->just_read_cus.clear ();
95554aad
TT
10230}
10231
9cdd5dbd 10232/* Generate full symbol information for PER_CU, whose DIEs have
10b3939b
DJ
10233 already been loaded into memory. */
10234
10235static void
95554aad
TT
10236process_full_comp_unit (struct dwarf2_per_cu_data *per_cu,
10237 enum language pretend_language)
10b3939b 10238{
10b3939b 10239 struct dwarf2_cu *cu = per_cu->cu;
ed2dc618
SM
10240 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
10241 struct objfile *objfile = dwarf2_per_objfile->objfile;
3e29f34a 10242 struct gdbarch *gdbarch = get_objfile_arch (objfile);
10b3939b 10243 CORE_ADDR lowpc, highpc;
43f3e411 10244 struct compunit_symtab *cust;
10b3939b 10245 CORE_ADDR baseaddr;
4359dff1 10246 struct block *static_block;
3e29f34a 10247 CORE_ADDR addr;
10b3939b
DJ
10248
10249 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
10250
c89b44cd
TT
10251 /* Clear the list here in case something was left over. */
10252 cu->method_list.clear ();
10b3939b 10253
95554aad
TT
10254 cu->language = pretend_language;
10255 cu->language_defn = language_def (cu->language);
10256
c906108c 10257 /* Do line number decoding in read_file_scope () */
10b3939b 10258 process_die (cu->dies, cu);
c906108c 10259
a766d390
DE
10260 /* For now fudge the Go package. */
10261 if (cu->language == language_go)
10262 fixup_go_packaging (cu);
10263
3da10d80
KS
10264 /* Now that we have processed all the DIEs in the CU, all the types
10265 should be complete, and it should now be safe to compute all of the
10266 physnames. */
10267 compute_delayed_physnames (cu);
3da10d80 10268
c9317f21
TT
10269 if (cu->language == language_rust)
10270 rust_union_quirks (cu);
10271
fae299cd
DC
10272 /* Some compilers don't define a DW_AT_high_pc attribute for the
10273 compilation unit. If the DW_AT_high_pc is missing, synthesize
10274 it, by scanning the DIE's below the compilation unit. */
10b3939b 10275 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
c906108c 10276
3e29f34a 10277 addr = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
804d2729 10278 static_block = cu->builder->end_symtab_get_static_block (addr, 0, 1);
4359dff1
JK
10279
10280 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
10281 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
10282 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
10283 addrmap to help ensure it has an accurate map of pc values belonging to
10284 this comp unit. */
10285 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
10286
804d2729
TT
10287 cust = cu->builder->end_symtab_from_static_block (static_block,
10288 SECT_OFF_TEXT (objfile),
10289 0);
c906108c 10290
43f3e411 10291 if (cust != NULL)
c906108c 10292 {
df15bd07 10293 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
4632c0d0 10294
8be455d7
JK
10295 /* Set symtab language to language from DW_AT_language. If the
10296 compilation is from a C file generated by language preprocessors, do
10297 not set the language if it was already deduced by start_subfile. */
43f3e411 10298 if (!(cu->language == language_c
40e3ad0e 10299 && COMPUNIT_FILETABS (cust)->language != language_unknown))
43f3e411 10300 COMPUNIT_FILETABS (cust)->language = cu->language;
8be455d7
JK
10301
10302 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
10303 produce DW_AT_location with location lists but it can be possibly
ab260dad
JK
10304 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
10305 there were bugs in prologue debug info, fixed later in GCC-4.5
10306 by "unwind info for epilogues" patch (which is not directly related).
8be455d7
JK
10307
10308 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
10309 needed, it would be wrong due to missing DW_AT_producer there.
10310
10311 Still one can confuse GDB by using non-standard GCC compilation
10312 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
10313 */
ab260dad 10314 if (cu->has_loclist && gcc_4_minor >= 5)
43f3e411 10315 cust->locations_valid = 1;
e0d00bc7
JK
10316
10317 if (gcc_4_minor >= 5)
43f3e411 10318 cust->epilogue_unwind_valid = 1;
96408a79 10319
43f3e411 10320 cust->call_site_htab = cu->call_site_htab;
c906108c 10321 }
9291a0cd
TT
10322
10323 if (dwarf2_per_objfile->using_index)
43f3e411 10324 per_cu->v.quick->compunit_symtab = cust;
9291a0cd
TT
10325 else
10326 {
10327 struct partial_symtab *pst = per_cu->v.psymtab;
43f3e411 10328 pst->compunit_symtab = cust;
9291a0cd
TT
10329 pst->readin = 1;
10330 }
c906108c 10331
95554aad 10332 /* Push it for inclusion processing later. */
c5d0225d 10333 dwarf2_per_objfile->just_read_cus.push_back (per_cu);
804d2729
TT
10334
10335 /* Not needed any more. */
10336 cu->builder.reset ();
f4dc4d17 10337}
45cfd468 10338
f4dc4d17
DE
10339/* Generate full symbol information for type unit PER_CU, whose DIEs have
10340 already been loaded into memory. */
10341
10342static void
10343process_full_type_unit (struct dwarf2_per_cu_data *per_cu,
10344 enum language pretend_language)
10345{
10346 struct dwarf2_cu *cu = per_cu->cu;
ed2dc618
SM
10347 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
10348 struct objfile *objfile = dwarf2_per_objfile->objfile;
43f3e411 10349 struct compunit_symtab *cust;
0186c6a7
DE
10350 struct signatured_type *sig_type;
10351
10352 gdb_assert (per_cu->is_debug_types);
10353 sig_type = (struct signatured_type *) per_cu;
f4dc4d17 10354
c89b44cd
TT
10355 /* Clear the list here in case something was left over. */
10356 cu->method_list.clear ();
f4dc4d17 10357
f4dc4d17
DE
10358 cu->language = pretend_language;
10359 cu->language_defn = language_def (cu->language);
10360
10361 /* The symbol tables are set up in read_type_unit_scope. */
10362 process_die (cu->dies, cu);
10363
10364 /* For now fudge the Go package. */
10365 if (cu->language == language_go)
10366 fixup_go_packaging (cu);
10367
10368 /* Now that we have processed all the DIEs in the CU, all the types
10369 should be complete, and it should now be safe to compute all of the
10370 physnames. */
10371 compute_delayed_physnames (cu);
f4dc4d17 10372
c9317f21
TT
10373 if (cu->language == language_rust)
10374 rust_union_quirks (cu);
10375
f4dc4d17
DE
10376 /* TUs share symbol tables.
10377 If this is the first TU to use this symtab, complete the construction
094b34ac
DE
10378 of it with end_expandable_symtab. Otherwise, complete the addition of
10379 this TU's symbols to the existing symtab. */
43f3e411 10380 if (sig_type->type_unit_group->compunit_symtab == NULL)
45cfd468 10381 {
804d2729 10382 cust = cu->builder->end_expandable_symtab (0, SECT_OFF_TEXT (objfile));
43f3e411 10383 sig_type->type_unit_group->compunit_symtab = cust;
f4dc4d17 10384
43f3e411 10385 if (cust != NULL)
f4dc4d17
DE
10386 {
10387 /* Set symtab language to language from DW_AT_language. If the
10388 compilation is from a C file generated by language preprocessors,
10389 do not set the language if it was already deduced by
10390 start_subfile. */
43f3e411
DE
10391 if (!(cu->language == language_c
10392 && COMPUNIT_FILETABS (cust)->language != language_c))
10393 COMPUNIT_FILETABS (cust)->language = cu->language;
f4dc4d17
DE
10394 }
10395 }
10396 else
10397 {
804d2729 10398 cu->builder->augment_type_symtab ();
43f3e411 10399 cust = sig_type->type_unit_group->compunit_symtab;
f4dc4d17
DE
10400 }
10401
10402 if (dwarf2_per_objfile->using_index)
43f3e411 10403 per_cu->v.quick->compunit_symtab = cust;
f4dc4d17
DE
10404 else
10405 {
10406 struct partial_symtab *pst = per_cu->v.psymtab;
43f3e411 10407 pst->compunit_symtab = cust;
f4dc4d17 10408 pst->readin = 1;
45cfd468 10409 }
804d2729
TT
10410
10411 /* Not needed any more. */
10412 cu->builder.reset ();
c906108c
SS
10413}
10414
95554aad
TT
10415/* Process an imported unit DIE. */
10416
10417static void
10418process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
10419{
10420 struct attribute *attr;
10421
f4dc4d17
DE
10422 /* For now we don't handle imported units in type units. */
10423 if (cu->per_cu->is_debug_types)
10424 {
10425 error (_("Dwarf Error: DW_TAG_imported_unit is not"
10426 " supported in type units [in module %s]"),
518817b3 10427 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
f4dc4d17
DE
10428 }
10429
95554aad
TT
10430 attr = dwarf2_attr (die, DW_AT_import, cu);
10431 if (attr != NULL)
10432 {
9c541725
PA
10433 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
10434 bool is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
10435 dwarf2_per_cu_data *per_cu
e3b94546 10436 = dwarf2_find_containing_comp_unit (sect_off, is_dwz,
518817b3 10437 cu->per_cu->dwarf2_per_objfile);
95554aad 10438
69d751e3 10439 /* If necessary, add it to the queue and load its DIEs. */
95554aad 10440 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
58f0c718 10441 load_full_comp_unit (per_cu, false, cu->language);
95554aad 10442
796a7ff8 10443 VEC_safe_push (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs,
95554aad
TT
10444 per_cu);
10445 }
10446}
10447
4c8aa72d
PA
10448/* RAII object that represents a process_die scope: i.e.,
10449 starts/finishes processing a DIE. */
10450class process_die_scope
adde2bff 10451{
4c8aa72d
PA
10452public:
10453 process_die_scope (die_info *die, dwarf2_cu *cu)
10454 : m_die (die), m_cu (cu)
10455 {
10456 /* We should only be processing DIEs not already in process. */
10457 gdb_assert (!m_die->in_process);
10458 m_die->in_process = true;
10459 }
8c3cb9fa 10460
4c8aa72d
PA
10461 ~process_die_scope ()
10462 {
10463 m_die->in_process = false;
10464
10465 /* If we're done processing the DIE for the CU that owns the line
10466 header, we don't need the line header anymore. */
10467 if (m_cu->line_header_die_owner == m_die)
10468 {
10469 delete m_cu->line_header;
10470 m_cu->line_header = NULL;
10471 m_cu->line_header_die_owner = NULL;
10472 }
10473 }
10474
10475private:
10476 die_info *m_die;
10477 dwarf2_cu *m_cu;
10478};
adde2bff 10479
c906108c
SS
10480/* Process a die and its children. */
10481
10482static void
e7c27a73 10483process_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 10484{
4c8aa72d 10485 process_die_scope scope (die, cu);
adde2bff 10486
c906108c
SS
10487 switch (die->tag)
10488 {
10489 case DW_TAG_padding:
10490 break;
10491 case DW_TAG_compile_unit:
95554aad 10492 case DW_TAG_partial_unit:
e7c27a73 10493 read_file_scope (die, cu);
c906108c 10494 break;
348e048f
DE
10495 case DW_TAG_type_unit:
10496 read_type_unit_scope (die, cu);
10497 break;
c906108c 10498 case DW_TAG_subprogram:
c906108c 10499 case DW_TAG_inlined_subroutine:
edb3359d 10500 read_func_scope (die, cu);
c906108c
SS
10501 break;
10502 case DW_TAG_lexical_block:
14898363
L
10503 case DW_TAG_try_block:
10504 case DW_TAG_catch_block:
e7c27a73 10505 read_lexical_block_scope (die, cu);
c906108c 10506 break;
216f72a1 10507 case DW_TAG_call_site:
96408a79
SA
10508 case DW_TAG_GNU_call_site:
10509 read_call_site_scope (die, cu);
10510 break;
c906108c 10511 case DW_TAG_class_type:
680b30c7 10512 case DW_TAG_interface_type:
c906108c
SS
10513 case DW_TAG_structure_type:
10514 case DW_TAG_union_type:
134d01f1 10515 process_structure_scope (die, cu);
c906108c
SS
10516 break;
10517 case DW_TAG_enumeration_type:
134d01f1 10518 process_enumeration_scope (die, cu);
c906108c 10519 break;
134d01f1 10520
f792889a
DJ
10521 /* These dies have a type, but processing them does not create
10522 a symbol or recurse to process the children. Therefore we can
10523 read them on-demand through read_type_die. */
c906108c 10524 case DW_TAG_subroutine_type:
72019c9c 10525 case DW_TAG_set_type:
c906108c 10526 case DW_TAG_array_type:
c906108c 10527 case DW_TAG_pointer_type:
c906108c 10528 case DW_TAG_ptr_to_member_type:
c906108c 10529 case DW_TAG_reference_type:
4297a3f0 10530 case DW_TAG_rvalue_reference_type:
c906108c 10531 case DW_TAG_string_type:
c906108c 10532 break;
134d01f1 10533
c906108c 10534 case DW_TAG_base_type:
a02abb62 10535 case DW_TAG_subrange_type:
cb249c71 10536 case DW_TAG_typedef:
134d01f1
DJ
10537 /* Add a typedef symbol for the type definition, if it has a
10538 DW_AT_name. */
f792889a 10539 new_symbol (die, read_type_die (die, cu), cu);
a02abb62 10540 break;
c906108c 10541 case DW_TAG_common_block:
e7c27a73 10542 read_common_block (die, cu);
c906108c
SS
10543 break;
10544 case DW_TAG_common_inclusion:
10545 break;
d9fa45fe 10546 case DW_TAG_namespace:
4d4ec4e5 10547 cu->processing_has_namespace_info = 1;
e7c27a73 10548 read_namespace (die, cu);
d9fa45fe 10549 break;
5d7cb8df 10550 case DW_TAG_module:
4d4ec4e5 10551 cu->processing_has_namespace_info = 1;
5d7cb8df
JK
10552 read_module (die, cu);
10553 break;
d9fa45fe 10554 case DW_TAG_imported_declaration:
74921315
KS
10555 cu->processing_has_namespace_info = 1;
10556 if (read_namespace_alias (die, cu))
10557 break;
86a73007
TT
10558 /* The declaration is not a global namespace alias. */
10559 /* Fall through. */
d9fa45fe 10560 case DW_TAG_imported_module:
4d4ec4e5 10561 cu->processing_has_namespace_info = 1;
27aa8d6a
SW
10562 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
10563 || cu->language != language_fortran))
b98664d3 10564 complaint (_("Tag '%s' has unexpected children"),
27aa8d6a
SW
10565 dwarf_tag_name (die->tag));
10566 read_import_statement (die, cu);
d9fa45fe 10567 break;
95554aad
TT
10568
10569 case DW_TAG_imported_unit:
10570 process_imported_unit_die (die, cu);
10571 break;
10572
71a3c369
TT
10573 case DW_TAG_variable:
10574 read_variable (die, cu);
10575 break;
10576
c906108c 10577 default:
e7c27a73 10578 new_symbol (die, NULL, cu);
c906108c
SS
10579 break;
10580 }
10581}
ca69b9e6
DE
10582\f
10583/* DWARF name computation. */
c906108c 10584
94af9270
KS
10585/* A helper function for dwarf2_compute_name which determines whether DIE
10586 needs to have the name of the scope prepended to the name listed in the
10587 die. */
10588
10589static int
10590die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
10591{
1c809c68
TT
10592 struct attribute *attr;
10593
94af9270
KS
10594 switch (die->tag)
10595 {
10596 case DW_TAG_namespace:
10597 case DW_TAG_typedef:
10598 case DW_TAG_class_type:
10599 case DW_TAG_interface_type:
10600 case DW_TAG_structure_type:
10601 case DW_TAG_union_type:
10602 case DW_TAG_enumeration_type:
10603 case DW_TAG_enumerator:
10604 case DW_TAG_subprogram:
08a76f8a 10605 case DW_TAG_inlined_subroutine:
94af9270 10606 case DW_TAG_member:
74921315 10607 case DW_TAG_imported_declaration:
94af9270
KS
10608 return 1;
10609
10610 case DW_TAG_variable:
c2b0a229 10611 case DW_TAG_constant:
94af9270
KS
10612 /* We only need to prefix "globally" visible variables. These include
10613 any variable marked with DW_AT_external or any variable that
10614 lives in a namespace. [Variables in anonymous namespaces
10615 require prefixing, but they are not DW_AT_external.] */
10616
10617 if (dwarf2_attr (die, DW_AT_specification, cu))
10618 {
10619 struct dwarf2_cu *spec_cu = cu;
9a619af0 10620
94af9270
KS
10621 return die_needs_namespace (die_specification (die, &spec_cu),
10622 spec_cu);
10623 }
10624
1c809c68 10625 attr = dwarf2_attr (die, DW_AT_external, cu);
f55ee35c
JK
10626 if (attr == NULL && die->parent->tag != DW_TAG_namespace
10627 && die->parent->tag != DW_TAG_module)
1c809c68
TT
10628 return 0;
10629 /* A variable in a lexical block of some kind does not need a
10630 namespace, even though in C++ such variables may be external
10631 and have a mangled name. */
10632 if (die->parent->tag == DW_TAG_lexical_block
10633 || die->parent->tag == DW_TAG_try_block
1054b214
TT
10634 || die->parent->tag == DW_TAG_catch_block
10635 || die->parent->tag == DW_TAG_subprogram)
1c809c68
TT
10636 return 0;
10637 return 1;
94af9270
KS
10638
10639 default:
10640 return 0;
10641 }
10642}
10643
73b9be8b
KS
10644/* Return the DIE's linkage name attribute, either DW_AT_linkage_name
10645 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10646 defined for the given DIE. */
10647
10648static struct attribute *
10649dw2_linkage_name_attr (struct die_info *die, struct dwarf2_cu *cu)
10650{
10651 struct attribute *attr;
10652
10653 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
10654 if (attr == NULL)
10655 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
10656
10657 return attr;
10658}
10659
10660/* Return the DIE's linkage name as a string, either DW_AT_linkage_name
10661 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10662 defined for the given DIE. */
10663
10664static const char *
10665dw2_linkage_name (struct die_info *die, struct dwarf2_cu *cu)
10666{
10667 const char *linkage_name;
10668
10669 linkage_name = dwarf2_string_attr (die, DW_AT_linkage_name, cu);
10670 if (linkage_name == NULL)
10671 linkage_name = dwarf2_string_attr (die, DW_AT_MIPS_linkage_name, cu);
10672
10673 return linkage_name;
10674}
10675
94af9270 10676/* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
a766d390 10677 compute the physname for the object, which include a method's:
9c37b5ae 10678 - formal parameters (C++),
a766d390 10679 - receiver type (Go),
a766d390
DE
10680
10681 The term "physname" is a bit confusing.
10682 For C++, for example, it is the demangled name.
10683 For Go, for example, it's the mangled name.
94af9270 10684
af6b7be1
JB
10685 For Ada, return the DIE's linkage name rather than the fully qualified
10686 name. PHYSNAME is ignored..
10687
94af9270
KS
10688 The result is allocated on the objfile_obstack and canonicalized. */
10689
10690static const char *
15d034d0
TT
10691dwarf2_compute_name (const char *name,
10692 struct die_info *die, struct dwarf2_cu *cu,
94af9270
KS
10693 int physname)
10694{
518817b3 10695 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
bb5ed363 10696
94af9270
KS
10697 if (name == NULL)
10698 name = dwarf2_name (die, cu);
10699
2ee7123e
DE
10700 /* For Fortran GDB prefers DW_AT_*linkage_name for the physname if present
10701 but otherwise compute it by typename_concat inside GDB.
10702 FIXME: Actually this is not really true, or at least not always true.
10703 It's all very confusing. SYMBOL_SET_NAMES doesn't try to demangle
5e2db402 10704 Fortran names because there is no mangling standard. So new_symbol
2ee7123e
DE
10705 will set the demangled name to the result of dwarf2_full_name, and it is
10706 the demangled name that GDB uses if it exists. */
f55ee35c
JK
10707 if (cu->language == language_ada
10708 || (cu->language == language_fortran && physname))
10709 {
10710 /* For Ada unit, we prefer the linkage name over the name, as
10711 the former contains the exported name, which the user expects
10712 to be able to reference. Ideally, we want the user to be able
10713 to reference this entity using either natural or linkage name,
10714 but we haven't started looking at this enhancement yet. */
73b9be8b 10715 const char *linkage_name = dw2_linkage_name (die, cu);
f55ee35c 10716
2ee7123e
DE
10717 if (linkage_name != NULL)
10718 return linkage_name;
f55ee35c
JK
10719 }
10720
94af9270
KS
10721 /* These are the only languages we know how to qualify names in. */
10722 if (name != NULL
9c37b5ae 10723 && (cu->language == language_cplus
c44af4eb
TT
10724 || cu->language == language_fortran || cu->language == language_d
10725 || cu->language == language_rust))
94af9270
KS
10726 {
10727 if (die_needs_namespace (die, cu))
10728 {
0d5cff50 10729 const char *prefix;
34a68019 10730 const char *canonical_name = NULL;
94af9270 10731
d7e74731
PA
10732 string_file buf;
10733
94af9270 10734 prefix = determine_prefix (die, cu);
94af9270
KS
10735 if (*prefix != '\0')
10736 {
f55ee35c
JK
10737 char *prefixed_name = typename_concat (NULL, prefix, name,
10738 physname, cu);
9a619af0 10739
d7e74731 10740 buf.puts (prefixed_name);
94af9270
KS
10741 xfree (prefixed_name);
10742 }
10743 else
d7e74731 10744 buf.puts (name);
94af9270 10745
98bfdba5
PA
10746 /* Template parameters may be specified in the DIE's DW_AT_name, or
10747 as children with DW_TAG_template_type_param or
10748 DW_TAG_value_type_param. If the latter, add them to the name
10749 here. If the name already has template parameters, then
10750 skip this step; some versions of GCC emit both, and
10751 it is more efficient to use the pre-computed name.
10752
10753 Something to keep in mind about this process: it is very
10754 unlikely, or in some cases downright impossible, to produce
10755 something that will match the mangled name of a function.
10756 If the definition of the function has the same debug info,
10757 we should be able to match up with it anyway. But fallbacks
10758 using the minimal symbol, for instance to find a method
10759 implemented in a stripped copy of libstdc++, will not work.
10760 If we do not have debug info for the definition, we will have to
10761 match them up some other way.
10762
10763 When we do name matching there is a related problem with function
10764 templates; two instantiated function templates are allowed to
10765 differ only by their return types, which we do not add here. */
10766
10767 if (cu->language == language_cplus && strchr (name, '<') == NULL)
10768 {
10769 struct attribute *attr;
10770 struct die_info *child;
10771 int first = 1;
10772
10773 die->building_fullname = 1;
10774
10775 for (child = die->child; child != NULL; child = child->sibling)
10776 {
10777 struct type *type;
12df843f 10778 LONGEST value;
d521ce57 10779 const gdb_byte *bytes;
98bfdba5
PA
10780 struct dwarf2_locexpr_baton *baton;
10781 struct value *v;
10782
10783 if (child->tag != DW_TAG_template_type_param
10784 && child->tag != DW_TAG_template_value_param)
10785 continue;
10786
10787 if (first)
10788 {
d7e74731 10789 buf.puts ("<");
98bfdba5
PA
10790 first = 0;
10791 }
10792 else
d7e74731 10793 buf.puts (", ");
98bfdba5
PA
10794
10795 attr = dwarf2_attr (child, DW_AT_type, cu);
10796 if (attr == NULL)
10797 {
b98664d3 10798 complaint (_("template parameter missing DW_AT_type"));
d7e74731 10799 buf.puts ("UNKNOWN_TYPE");
98bfdba5
PA
10800 continue;
10801 }
10802 type = die_type (child, cu);
10803
10804 if (child->tag == DW_TAG_template_type_param)
10805 {
c1ec8cea
TT
10806 c_print_type (type, "", &buf, -1, 0, cu->language,
10807 &type_print_raw_options);
98bfdba5
PA
10808 continue;
10809 }
10810
10811 attr = dwarf2_attr (child, DW_AT_const_value, cu);
10812 if (attr == NULL)
10813 {
b98664d3 10814 complaint (_("template parameter missing "
3e43a32a 10815 "DW_AT_const_value"));
d7e74731 10816 buf.puts ("UNKNOWN_VALUE");
98bfdba5
PA
10817 continue;
10818 }
10819
10820 dwarf2_const_value_attr (attr, type, name,
10821 &cu->comp_unit_obstack, cu,
10822 &value, &bytes, &baton);
10823
10824 if (TYPE_NOSIGN (type))
10825 /* GDB prints characters as NUMBER 'CHAR'. If that's
10826 changed, this can use value_print instead. */
d7e74731 10827 c_printchar (value, type, &buf);
98bfdba5
PA
10828 else
10829 {
10830 struct value_print_options opts;
10831
10832 if (baton != NULL)
10833 v = dwarf2_evaluate_loc_desc (type, NULL,
10834 baton->data,
10835 baton->size,
10836 baton->per_cu);
10837 else if (bytes != NULL)
10838 {
10839 v = allocate_value (type);
10840 memcpy (value_contents_writeable (v), bytes,
10841 TYPE_LENGTH (type));
10842 }
10843 else
10844 v = value_from_longest (type, value);
10845
3e43a32a
MS
10846 /* Specify decimal so that we do not depend on
10847 the radix. */
98bfdba5
PA
10848 get_formatted_print_options (&opts, 'd');
10849 opts.raw = 1;
d7e74731 10850 value_print (v, &buf, &opts);
98bfdba5 10851 release_value (v);
98bfdba5
PA
10852 }
10853 }
10854
10855 die->building_fullname = 0;
10856
10857 if (!first)
10858 {
10859 /* Close the argument list, with a space if necessary
10860 (nested templates). */
d7e74731
PA
10861 if (!buf.empty () && buf.string ().back () == '>')
10862 buf.puts (" >");
98bfdba5 10863 else
d7e74731 10864 buf.puts (">");
98bfdba5
PA
10865 }
10866 }
10867
9c37b5ae 10868 /* For C++ methods, append formal parameter type
94af9270 10869 information, if PHYSNAME. */
6e70227d 10870
94af9270 10871 if (physname && die->tag == DW_TAG_subprogram
9c37b5ae 10872 && cu->language == language_cplus)
94af9270
KS
10873 {
10874 struct type *type = read_type_die (die, cu);
10875
d7e74731 10876 c_type_print_args (type, &buf, 1, cu->language,
79d43c61 10877 &type_print_raw_options);
94af9270 10878
9c37b5ae 10879 if (cu->language == language_cplus)
94af9270 10880 {
60430eff
DJ
10881 /* Assume that an artificial first parameter is
10882 "this", but do not crash if it is not. RealView
10883 marks unnamed (and thus unused) parameters as
10884 artificial; there is no way to differentiate
10885 the two cases. */
94af9270
KS
10886 if (TYPE_NFIELDS (type) > 0
10887 && TYPE_FIELD_ARTIFICIAL (type, 0)
60430eff 10888 && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
3e43a32a
MS
10889 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
10890 0))))
d7e74731 10891 buf.puts (" const");
94af9270
KS
10892 }
10893 }
10894
d7e74731 10895 const std::string &intermediate_name = buf.string ();
94af9270
KS
10896
10897 if (cu->language == language_cplus)
34a68019 10898 canonical_name
322a8516 10899 = dwarf2_canonicalize_name (intermediate_name.c_str (), cu,
34a68019
TT
10900 &objfile->per_bfd->storage_obstack);
10901
10902 /* If we only computed INTERMEDIATE_NAME, or if
10903 INTERMEDIATE_NAME is already canonical, then we need to
10904 copy it to the appropriate obstack. */
322a8516 10905 if (canonical_name == NULL || canonical_name == intermediate_name.c_str ())
224c3ddb
SM
10906 name = ((const char *)
10907 obstack_copy0 (&objfile->per_bfd->storage_obstack,
322a8516
PA
10908 intermediate_name.c_str (),
10909 intermediate_name.length ()));
34a68019
TT
10910 else
10911 name = canonical_name;
94af9270
KS
10912 }
10913 }
10914
10915 return name;
10916}
10917
0114d602
DJ
10918/* Return the fully qualified name of DIE, based on its DW_AT_name.
10919 If scope qualifiers are appropriate they will be added. The result
34a68019 10920 will be allocated on the storage_obstack, or NULL if the DIE does
94af9270
KS
10921 not have a name. NAME may either be from a previous call to
10922 dwarf2_name or NULL.
10923
9c37b5ae 10924 The output string will be canonicalized (if C++). */
0114d602
DJ
10925
10926static const char *
15d034d0 10927dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
0114d602 10928{
94af9270
KS
10929 return dwarf2_compute_name (name, die, cu, 0);
10930}
0114d602 10931
94af9270
KS
10932/* Construct a physname for the given DIE in CU. NAME may either be
10933 from a previous call to dwarf2_name or NULL. The result will be
10934 allocated on the objfile_objstack or NULL if the DIE does not have a
10935 name.
0114d602 10936
9c37b5ae 10937 The output string will be canonicalized (if C++). */
0114d602 10938
94af9270 10939static const char *
15d034d0 10940dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
94af9270 10941{
518817b3 10942 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
900e11f9 10943 const char *retval, *mangled = NULL, *canon = NULL;
900e11f9
JK
10944 int need_copy = 1;
10945
10946 /* In this case dwarf2_compute_name is just a shortcut not building anything
10947 on its own. */
10948 if (!die_needs_namespace (die, cu))
10949 return dwarf2_compute_name (name, die, cu, 1);
10950
73b9be8b 10951 mangled = dw2_linkage_name (die, cu);
900e11f9 10952
e98c9e7c
TT
10953 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
10954 See https://github.com/rust-lang/rust/issues/32925. */
10955 if (cu->language == language_rust && mangled != NULL
10956 && strchr (mangled, '{') != NULL)
10957 mangled = NULL;
10958
900e11f9
JK
10959 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
10960 has computed. */
791afaa2 10961 gdb::unique_xmalloc_ptr<char> demangled;
7d45c7c3 10962 if (mangled != NULL)
900e11f9 10963 {
900e11f9 10964
59cc4834
JB
10965 if (language_def (cu->language)->la_store_sym_names_in_linkage_form_p)
10966 {
10967 /* Do nothing (do not demangle the symbol name). */
10968 }
10969 else if (cu->language == language_go)
a766d390 10970 {
5e2db402
TT
10971 /* This is a lie, but we already lie to the caller new_symbol.
10972 new_symbol assumes we return the mangled name.
a766d390 10973 This just undoes that lie until things are cleaned up. */
a766d390
DE
10974 }
10975 else
10976 {
0eb876f5
JB
10977 /* Use DMGL_RET_DROP for C++ template functions to suppress
10978 their return type. It is easier for GDB users to search
10979 for such functions as `name(params)' than `long name(params)'.
10980 In such case the minimal symbol names do not match the full
10981 symbol names but for template functions there is never a need
10982 to look up their definition from their declaration so
10983 the only disadvantage remains the minimal symbol variant
10984 `long name(params)' does not have the proper inferior type. */
791afaa2
TT
10985 demangled.reset (gdb_demangle (mangled,
10986 (DMGL_PARAMS | DMGL_ANSI
10987 | DMGL_RET_DROP)));
a766d390 10988 }
900e11f9 10989 if (demangled)
791afaa2 10990 canon = demangled.get ();
900e11f9
JK
10991 else
10992 {
10993 canon = mangled;
10994 need_copy = 0;
10995 }
10996 }
10997
10998 if (canon == NULL || check_physname)
10999 {
11000 const char *physname = dwarf2_compute_name (name, die, cu, 1);
11001
11002 if (canon != NULL && strcmp (physname, canon) != 0)
11003 {
11004 /* It may not mean a bug in GDB. The compiler could also
11005 compute DW_AT_linkage_name incorrectly. But in such case
11006 GDB would need to be bug-to-bug compatible. */
11007
b98664d3 11008 complaint (_("Computed physname <%s> does not match demangled <%s> "
9d8780f0
SM
11009 "(from linkage <%s>) - DIE at %s [in module %s]"),
11010 physname, canon, mangled, sect_offset_str (die->sect_off),
4262abfb 11011 objfile_name (objfile));
900e11f9
JK
11012
11013 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
11014 is available here - over computed PHYSNAME. It is safer
11015 against both buggy GDB and buggy compilers. */
11016
11017 retval = canon;
11018 }
11019 else
11020 {
11021 retval = physname;
11022 need_copy = 0;
11023 }
11024 }
11025 else
11026 retval = canon;
11027
11028 if (need_copy)
224c3ddb
SM
11029 retval = ((const char *)
11030 obstack_copy0 (&objfile->per_bfd->storage_obstack,
11031 retval, strlen (retval)));
900e11f9 11032
900e11f9 11033 return retval;
0114d602
DJ
11034}
11035
74921315
KS
11036/* Inspect DIE in CU for a namespace alias. If one exists, record
11037 a new symbol for it.
11038
11039 Returns 1 if a namespace alias was recorded, 0 otherwise. */
11040
11041static int
11042read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu)
11043{
11044 struct attribute *attr;
11045
11046 /* If the die does not have a name, this is not a namespace
11047 alias. */
11048 attr = dwarf2_attr (die, DW_AT_name, cu);
11049 if (attr != NULL)
11050 {
11051 int num;
11052 struct die_info *d = die;
11053 struct dwarf2_cu *imported_cu = cu;
11054
11055 /* If the compiler has nested DW_AT_imported_declaration DIEs,
11056 keep inspecting DIEs until we hit the underlying import. */
11057#define MAX_NESTED_IMPORTED_DECLARATIONS 100
11058 for (num = 0; num < MAX_NESTED_IMPORTED_DECLARATIONS; ++num)
11059 {
11060 attr = dwarf2_attr (d, DW_AT_import, cu);
11061 if (attr == NULL)
11062 break;
11063
11064 d = follow_die_ref (d, attr, &imported_cu);
11065 if (d->tag != DW_TAG_imported_declaration)
11066 break;
11067 }
11068
11069 if (num == MAX_NESTED_IMPORTED_DECLARATIONS)
11070 {
b98664d3 11071 complaint (_("DIE at %s has too many recursively imported "
9d8780f0 11072 "declarations"), sect_offset_str (d->sect_off));
74921315
KS
11073 return 0;
11074 }
11075
11076 if (attr != NULL)
11077 {
11078 struct type *type;
9c541725 11079 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
74921315 11080
9c541725 11081 type = get_die_type_at_offset (sect_off, cu->per_cu);
74921315
KS
11082 if (type != NULL && TYPE_CODE (type) == TYPE_CODE_NAMESPACE)
11083 {
11084 /* This declaration is a global namespace alias. Add
11085 a symbol for it whose type is the aliased namespace. */
11086 new_symbol (die, type, cu);
11087 return 1;
11088 }
11089 }
11090 }
11091
11092 return 0;
11093}
11094
22cee43f 11095/* Return the using directives repository (global or local?) to use in the
804d2729 11096 current context for CU.
22cee43f
PMR
11097
11098 For Ada, imported declarations can materialize renamings, which *may* be
11099 global. However it is impossible (for now?) in DWARF to distinguish
11100 "external" imported declarations and "static" ones. As all imported
11101 declarations seem to be static in all other languages, make them all CU-wide
11102 global only in Ada. */
11103
11104static struct using_direct **
804d2729 11105using_directives (struct dwarf2_cu *cu)
22cee43f 11106{
804d2729
TT
11107 if (cu->language == language_ada && cu->builder->outermost_context_p ())
11108 return cu->builder->get_global_using_directives ();
22cee43f 11109 else
804d2729 11110 return cu->builder->get_local_using_directives ();
22cee43f
PMR
11111}
11112
27aa8d6a
SW
11113/* Read the import statement specified by the given die and record it. */
11114
11115static void
11116read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
11117{
518817b3 11118 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
27aa8d6a 11119 struct attribute *import_attr;
32019081 11120 struct die_info *imported_die, *child_die;
de4affc9 11121 struct dwarf2_cu *imported_cu;
27aa8d6a 11122 const char *imported_name;
794684b6 11123 const char *imported_name_prefix;
13387711
SW
11124 const char *canonical_name;
11125 const char *import_alias;
11126 const char *imported_declaration = NULL;
794684b6 11127 const char *import_prefix;
eb1e02fd 11128 std::vector<const char *> excludes;
13387711 11129
27aa8d6a
SW
11130 import_attr = dwarf2_attr (die, DW_AT_import, cu);
11131 if (import_attr == NULL)
11132 {
b98664d3 11133 complaint (_("Tag '%s' has no DW_AT_import"),
27aa8d6a
SW
11134 dwarf_tag_name (die->tag));
11135 return;
11136 }
11137
de4affc9
CC
11138 imported_cu = cu;
11139 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
11140 imported_name = dwarf2_name (imported_die, imported_cu);
27aa8d6a
SW
11141 if (imported_name == NULL)
11142 {
11143 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
11144
11145 The import in the following code:
11146 namespace A
11147 {
11148 typedef int B;
11149 }
11150
11151 int main ()
11152 {
11153 using A::B;
11154 B b;
11155 return b;
11156 }
11157
11158 ...
11159 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
11160 <52> DW_AT_decl_file : 1
11161 <53> DW_AT_decl_line : 6
11162 <54> DW_AT_import : <0x75>
11163 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
11164 <59> DW_AT_name : B
11165 <5b> DW_AT_decl_file : 1
11166 <5c> DW_AT_decl_line : 2
11167 <5d> DW_AT_type : <0x6e>
11168 ...
11169 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
11170 <76> DW_AT_byte_size : 4
11171 <77> DW_AT_encoding : 5 (signed)
11172
11173 imports the wrong die ( 0x75 instead of 0x58 ).
11174 This case will be ignored until the gcc bug is fixed. */
11175 return;
11176 }
11177
82856980
SW
11178 /* Figure out the local name after import. */
11179 import_alias = dwarf2_name (die, cu);
27aa8d6a 11180
794684b6
SW
11181 /* Figure out where the statement is being imported to. */
11182 import_prefix = determine_prefix (die, cu);
11183
11184 /* Figure out what the scope of the imported die is and prepend it
11185 to the name of the imported die. */
de4affc9 11186 imported_name_prefix = determine_prefix (imported_die, imported_cu);
794684b6 11187
f55ee35c
JK
11188 if (imported_die->tag != DW_TAG_namespace
11189 && imported_die->tag != DW_TAG_module)
794684b6 11190 {
13387711
SW
11191 imported_declaration = imported_name;
11192 canonical_name = imported_name_prefix;
794684b6 11193 }
13387711 11194 else if (strlen (imported_name_prefix) > 0)
12aaed36 11195 canonical_name = obconcat (&objfile->objfile_obstack,
45280282
IB
11196 imported_name_prefix,
11197 (cu->language == language_d ? "." : "::"),
11198 imported_name, (char *) NULL);
13387711
SW
11199 else
11200 canonical_name = imported_name;
794684b6 11201
32019081
JK
11202 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
11203 for (child_die = die->child; child_die && child_die->tag;
11204 child_die = sibling_die (child_die))
11205 {
11206 /* DWARF-4: A Fortran use statement with a “rename list” may be
11207 represented by an imported module entry with an import attribute
11208 referring to the module and owned entries corresponding to those
11209 entities that are renamed as part of being imported. */
11210
11211 if (child_die->tag != DW_TAG_imported_declaration)
11212 {
b98664d3 11213 complaint (_("child DW_TAG_imported_declaration expected "
9d8780f0
SM
11214 "- DIE at %s [in module %s]"),
11215 sect_offset_str (child_die->sect_off),
11216 objfile_name (objfile));
32019081
JK
11217 continue;
11218 }
11219
11220 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
11221 if (import_attr == NULL)
11222 {
b98664d3 11223 complaint (_("Tag '%s' has no DW_AT_import"),
32019081
JK
11224 dwarf_tag_name (child_die->tag));
11225 continue;
11226 }
11227
11228 imported_cu = cu;
11229 imported_die = follow_die_ref_or_sig (child_die, import_attr,
11230 &imported_cu);
11231 imported_name = dwarf2_name (imported_die, imported_cu);
11232 if (imported_name == NULL)
11233 {
b98664d3 11234 complaint (_("child DW_TAG_imported_declaration has unknown "
9d8780f0
SM
11235 "imported name - DIE at %s [in module %s]"),
11236 sect_offset_str (child_die->sect_off),
11237 objfile_name (objfile));
32019081
JK
11238 continue;
11239 }
11240
eb1e02fd 11241 excludes.push_back (imported_name);
32019081
JK
11242
11243 process_die (child_die, cu);
11244 }
11245
804d2729 11246 add_using_directive (using_directives (cu),
22cee43f
PMR
11247 import_prefix,
11248 canonical_name,
11249 import_alias,
11250 imported_declaration,
11251 excludes,
11252 0,
11253 &objfile->objfile_obstack);
27aa8d6a
SW
11254}
11255
5230b05a
WT
11256/* ICC<14 does not output the required DW_AT_declaration on incomplete
11257 types, but gives them a size of zero. Starting with version 14,
11258 ICC is compatible with GCC. */
11259
11260static int
11261producer_is_icc_lt_14 (struct dwarf2_cu *cu)
11262{
11263 if (!cu->checked_producer)
11264 check_producer (cu);
11265
11266 return cu->producer_is_icc_lt_14;
11267}
11268
1b80a9fa
JK
11269/* Check for possibly missing DW_AT_comp_dir with relative .debug_line
11270 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
11271 this, it was first present in GCC release 4.3.0. */
11272
11273static int
11274producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
11275{
11276 if (!cu->checked_producer)
11277 check_producer (cu);
11278
11279 return cu->producer_is_gcc_lt_4_3;
11280}
11281
d721ba37
PA
11282static file_and_directory
11283find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu)
9291a0cd 11284{
d721ba37
PA
11285 file_and_directory res;
11286
9291a0cd
TT
11287 /* Find the filename. Do not use dwarf2_name here, since the filename
11288 is not a source language identifier. */
d721ba37
PA
11289 res.name = dwarf2_string_attr (die, DW_AT_name, cu);
11290 res.comp_dir = dwarf2_string_attr (die, DW_AT_comp_dir, cu);
9291a0cd 11291
d721ba37
PA
11292 if (res.comp_dir == NULL
11293 && producer_is_gcc_lt_4_3 (cu) && res.name != NULL
11294 && IS_ABSOLUTE_PATH (res.name))
9291a0cd 11295 {
d721ba37
PA
11296 res.comp_dir_storage = ldirname (res.name);
11297 if (!res.comp_dir_storage.empty ())
11298 res.comp_dir = res.comp_dir_storage.c_str ();
9291a0cd 11299 }
d721ba37 11300 if (res.comp_dir != NULL)
9291a0cd
TT
11301 {
11302 /* Irix 6.2 native cc prepends <machine>.: to the compilation
11303 directory, get rid of it. */
d721ba37 11304 const char *cp = strchr (res.comp_dir, ':');
9291a0cd 11305
d721ba37
PA
11306 if (cp && cp != res.comp_dir && cp[-1] == '.' && cp[1] == '/')
11307 res.comp_dir = cp + 1;
9291a0cd
TT
11308 }
11309
d721ba37
PA
11310 if (res.name == NULL)
11311 res.name = "<unknown>";
11312
11313 return res;
9291a0cd
TT
11314}
11315
f4dc4d17
DE
11316/* Handle DW_AT_stmt_list for a compilation unit.
11317 DIE is the DW_TAG_compile_unit die for CU.
c3b7b696
YQ
11318 COMP_DIR is the compilation directory. LOWPC is passed to
11319 dwarf_decode_lines. See dwarf_decode_lines comments about it. */
2ab95328
TT
11320
11321static void
11322handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
c3b7b696 11323 const char *comp_dir, CORE_ADDR lowpc) /* ARI: editCase function */
2ab95328 11324{
518817b3
SM
11325 struct dwarf2_per_objfile *dwarf2_per_objfile
11326 = cu->per_cu->dwarf2_per_objfile;
527f3840 11327 struct objfile *objfile = dwarf2_per_objfile->objfile;
2ab95328 11328 struct attribute *attr;
527f3840
JK
11329 struct line_header line_header_local;
11330 hashval_t line_header_local_hash;
527f3840
JK
11331 void **slot;
11332 int decode_mapping;
2ab95328 11333
f4dc4d17
DE
11334 gdb_assert (! cu->per_cu->is_debug_types);
11335
2ab95328 11336 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
527f3840
JK
11337 if (attr == NULL)
11338 return;
11339
9c541725 11340 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
527f3840
JK
11341
11342 /* The line header hash table is only created if needed (it exists to
11343 prevent redundant reading of the line table for partial_units).
11344 If we're given a partial_unit, we'll need it. If we're given a
11345 compile_unit, then use the line header hash table if it's already
11346 created, but don't create one just yet. */
11347
11348 if (dwarf2_per_objfile->line_header_hash == NULL
11349 && die->tag == DW_TAG_partial_unit)
2ab95328 11350 {
527f3840
JK
11351 dwarf2_per_objfile->line_header_hash
11352 = htab_create_alloc_ex (127, line_header_hash_voidp,
11353 line_header_eq_voidp,
11354 free_line_header_voidp,
11355 &objfile->objfile_obstack,
11356 hashtab_obstack_allocate,
11357 dummy_obstack_deallocate);
11358 }
2ab95328 11359
9c541725 11360 line_header_local.sect_off = line_offset;
527f3840
JK
11361 line_header_local.offset_in_dwz = cu->per_cu->is_dwz;
11362 line_header_local_hash = line_header_hash (&line_header_local);
11363 if (dwarf2_per_objfile->line_header_hash != NULL)
11364 {
11365 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash,
11366 &line_header_local,
11367 line_header_local_hash, NO_INSERT);
11368
11369 /* For DW_TAG_compile_unit we need info like symtab::linetable which
11370 is not present in *SLOT (since if there is something in *SLOT then
11371 it will be for a partial_unit). */
11372 if (die->tag == DW_TAG_partial_unit && slot != NULL)
dee91e82 11373 {
527f3840 11374 gdb_assert (*slot != NULL);
9a3c8263 11375 cu->line_header = (struct line_header *) *slot;
527f3840 11376 return;
dee91e82 11377 }
2ab95328 11378 }
527f3840
JK
11379
11380 /* dwarf_decode_line_header does not yet provide sufficient information.
11381 We always have to call also dwarf_decode_lines for it. */
fff8551c
PA
11382 line_header_up lh = dwarf_decode_line_header (line_offset, cu);
11383 if (lh == NULL)
527f3840 11384 return;
4c8aa72d
PA
11385
11386 cu->line_header = lh.release ();
11387 cu->line_header_die_owner = die;
527f3840
JK
11388
11389 if (dwarf2_per_objfile->line_header_hash == NULL)
11390 slot = NULL;
11391 else
11392 {
11393 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash,
11394 &line_header_local,
11395 line_header_local_hash, INSERT);
11396 gdb_assert (slot != NULL);
11397 }
11398 if (slot != NULL && *slot == NULL)
11399 {
11400 /* This newly decoded line number information unit will be owned
11401 by line_header_hash hash table. */
11402 *slot = cu->line_header;
4c8aa72d 11403 cu->line_header_die_owner = NULL;
527f3840
JK
11404 }
11405 else
11406 {
11407 /* We cannot free any current entry in (*slot) as that struct line_header
11408 may be already used by multiple CUs. Create only temporary decoded
11409 line_header for this CU - it may happen at most once for each line
11410 number information unit. And if we're not using line_header_hash
11411 then this is what we want as well. */
11412 gdb_assert (die->tag != DW_TAG_partial_unit);
527f3840
JK
11413 }
11414 decode_mapping = (die->tag != DW_TAG_partial_unit);
11415 dwarf_decode_lines (cu->line_header, comp_dir, cu, NULL, lowpc,
11416 decode_mapping);
fff8551c 11417
2ab95328
TT
11418}
11419
95554aad 11420/* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
ae2de4f8 11421
c906108c 11422static void
e7c27a73 11423read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 11424{
518817b3
SM
11425 struct dwarf2_per_objfile *dwarf2_per_objfile
11426 = cu->per_cu->dwarf2_per_objfile;
dee91e82 11427 struct objfile *objfile = dwarf2_per_objfile->objfile;
3e29f34a 11428 struct gdbarch *gdbarch = get_objfile_arch (objfile);
2acceee2 11429 CORE_ADDR lowpc = ((CORE_ADDR) -1);
c906108c
SS
11430 CORE_ADDR highpc = ((CORE_ADDR) 0);
11431 struct attribute *attr;
c906108c 11432 struct die_info *child_die;
e142c38c 11433 CORE_ADDR baseaddr;
6e70227d 11434
e142c38c 11435 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 11436
fae299cd 11437 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
c906108c
SS
11438
11439 /* If we didn't find a lowpc, set it to highpc to avoid complaints
11440 from finish_block. */
2acceee2 11441 if (lowpc == ((CORE_ADDR) -1))
c906108c 11442 lowpc = highpc;
3e29f34a 11443 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
c906108c 11444
d721ba37 11445 file_and_directory fnd = find_file_and_directory (die, cu);
e1024ff1 11446
95554aad 11447 prepare_one_comp_unit (cu, die, cu->language);
303b6f5d 11448
f4b8a18d
KW
11449 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
11450 standardised yet. As a workaround for the language detection we fall
11451 back to the DW_AT_producer string. */
11452 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
11453 cu->language = language_opencl;
11454
3019eac3
DE
11455 /* Similar hack for Go. */
11456 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
11457 set_cu_language (DW_LANG_Go, cu);
11458
d721ba37 11459 dwarf2_start_symtab (cu, fnd.name, fnd.comp_dir, lowpc);
3019eac3
DE
11460
11461 /* Decode line number information if present. We do this before
11462 processing child DIEs, so that the line header table is available
11463 for DW_AT_decl_file. */
d721ba37 11464 handle_DW_AT_stmt_list (die, cu, fnd.comp_dir, lowpc);
3019eac3
DE
11465
11466 /* Process all dies in compilation unit. */
11467 if (die->child != NULL)
11468 {
11469 child_die = die->child;
11470 while (child_die && child_die->tag)
11471 {
11472 process_die (child_die, cu);
11473 child_die = sibling_die (child_die);
11474 }
11475 }
11476
11477 /* Decode macro information, if present. Dwarf 2 macro information
11478 refers to information in the line number info statement program
11479 header, so we can only read it if we've read the header
11480 successfully. */
0af92d60
JK
11481 attr = dwarf2_attr (die, DW_AT_macros, cu);
11482 if (attr == NULL)
11483 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
3019eac3
DE
11484 if (attr && cu->line_header)
11485 {
11486 if (dwarf2_attr (die, DW_AT_macro_info, cu))
b98664d3 11487 complaint (_("CU refers to both DW_AT_macros and DW_AT_macro_info"));
3019eac3 11488
43f3e411 11489 dwarf_decode_macros (cu, DW_UNSND (attr), 1);
3019eac3
DE
11490 }
11491 else
11492 {
11493 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
11494 if (attr && cu->line_header)
11495 {
11496 unsigned int macro_offset = DW_UNSND (attr);
11497
43f3e411 11498 dwarf_decode_macros (cu, macro_offset, 0);
3019eac3
DE
11499 }
11500 }
3019eac3
DE
11501}
11502
f4dc4d17
DE
11503/* TU version of handle_DW_AT_stmt_list for read_type_unit_scope.
11504 Create the set of symtabs used by this TU, or if this TU is sharing
11505 symtabs with another TU and the symtabs have already been created
11506 then restore those symtabs in the line header.
11507 We don't need the pc/line-number mapping for type units. */
3019eac3
DE
11508
11509static void
f4dc4d17 11510setup_type_unit_groups (struct die_info *die, struct dwarf2_cu *cu)
3019eac3 11511{
f4dc4d17
DE
11512 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
11513 struct type_unit_group *tu_group;
11514 int first_time;
3019eac3 11515 struct attribute *attr;
9c541725 11516 unsigned int i;
0186c6a7 11517 struct signatured_type *sig_type;
3019eac3 11518
f4dc4d17 11519 gdb_assert (per_cu->is_debug_types);
0186c6a7 11520 sig_type = (struct signatured_type *) per_cu;
3019eac3 11521
f4dc4d17 11522 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
3019eac3 11523
f4dc4d17 11524 /* If we're using .gdb_index (includes -readnow) then
74e04d1c 11525 per_cu->type_unit_group may not have been set up yet. */
0186c6a7
DE
11526 if (sig_type->type_unit_group == NULL)
11527 sig_type->type_unit_group = get_type_unit_group (cu, attr);
11528 tu_group = sig_type->type_unit_group;
f4dc4d17
DE
11529
11530 /* If we've already processed this stmt_list there's no real need to
11531 do it again, we could fake it and just recreate the part we need
11532 (file name,index -> symtab mapping). If data shows this optimization
11533 is useful we can do it then. */
43f3e411 11534 first_time = tu_group->compunit_symtab == NULL;
f4dc4d17
DE
11535
11536 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
11537 debug info. */
fff8551c 11538 line_header_up lh;
f4dc4d17 11539 if (attr != NULL)
3019eac3 11540 {
9c541725 11541 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
f4dc4d17
DE
11542 lh = dwarf_decode_line_header (line_offset, cu);
11543 }
11544 if (lh == NULL)
11545 {
11546 if (first_time)
11547 dwarf2_start_symtab (cu, "", NULL, 0);
11548 else
11549 {
11550 gdb_assert (tu_group->symtabs == NULL);
804d2729
TT
11551 gdb_assert (cu->builder == nullptr);
11552 struct compunit_symtab *cust = tu_group->compunit_symtab;
11553 cu->builder.reset (new struct buildsym_compunit
11554 (COMPUNIT_OBJFILE (cust), "",
11555 COMPUNIT_DIRNAME (cust),
11556 compunit_language (cust),
11557 0, cust));
f4dc4d17 11558 }
f4dc4d17 11559 return;
3019eac3
DE
11560 }
11561
4c8aa72d
PA
11562 cu->line_header = lh.release ();
11563 cu->line_header_die_owner = die;
3019eac3 11564
f4dc4d17
DE
11565 if (first_time)
11566 {
43f3e411 11567 struct compunit_symtab *cust = dwarf2_start_symtab (cu, "", NULL, 0);
3019eac3 11568
1fd60fc0
DE
11569 /* Note: We don't assign tu_group->compunit_symtab yet because we're
11570 still initializing it, and our caller (a few levels up)
11571 process_full_type_unit still needs to know if this is the first
11572 time. */
11573
4c8aa72d
PA
11574 tu_group->num_symtabs = cu->line_header->file_names.size ();
11575 tu_group->symtabs = XNEWVEC (struct symtab *,
11576 cu->line_header->file_names.size ());
3019eac3 11577
4c8aa72d 11578 for (i = 0; i < cu->line_header->file_names.size (); ++i)
f4dc4d17 11579 {
4c8aa72d 11580 file_entry &fe = cu->line_header->file_names[i];
3019eac3 11581
804d2729 11582 dwarf2_start_subfile (cu, fe.name, fe.include_dir (cu->line_header));
3019eac3 11583
804d2729 11584 if (cu->builder->get_current_subfile ()->symtab == NULL)
f4dc4d17 11585 {
4c8aa72d
PA
11586 /* NOTE: start_subfile will recognize when it's been
11587 passed a file it has already seen. So we can't
11588 assume there's a simple mapping from
11589 cu->line_header->file_names to subfiles, plus
11590 cu->line_header->file_names may contain dups. */
804d2729
TT
11591 cu->builder->get_current_subfile ()->symtab
11592 = allocate_symtab (cust,
11593 cu->builder->get_current_subfile ()->name);
f4dc4d17
DE
11594 }
11595
804d2729 11596 fe.symtab = cu->builder->get_current_subfile ()->symtab;
8c43009f 11597 tu_group->symtabs[i] = fe.symtab;
f4dc4d17
DE
11598 }
11599 }
11600 else
3019eac3 11601 {
804d2729
TT
11602 gdb_assert (cu->builder == nullptr);
11603 struct compunit_symtab *cust = tu_group->compunit_symtab;
11604 cu->builder.reset (new struct buildsym_compunit
11605 (COMPUNIT_OBJFILE (cust), "",
11606 COMPUNIT_DIRNAME (cust),
11607 compunit_language (cust),
11608 0, cust));
f4dc4d17 11609
4c8aa72d 11610 for (i = 0; i < cu->line_header->file_names.size (); ++i)
f4dc4d17 11611 {
4c8aa72d 11612 file_entry &fe = cu->line_header->file_names[i];
f4dc4d17 11613
4c8aa72d 11614 fe.symtab = tu_group->symtabs[i];
f4dc4d17 11615 }
3019eac3
DE
11616 }
11617
f4dc4d17
DE
11618 /* The main symtab is allocated last. Type units don't have DW_AT_name
11619 so they don't have a "real" (so to speak) symtab anyway.
11620 There is later code that will assign the main symtab to all symbols
11621 that don't have one. We need to handle the case of a symbol with a
11622 missing symtab (DW_AT_decl_file) anyway. */
11623}
3019eac3 11624
f4dc4d17
DE
11625/* Process DW_TAG_type_unit.
11626 For TUs we want to skip the first top level sibling if it's not the
11627 actual type being defined by this TU. In this case the first top
11628 level sibling is there to provide context only. */
3019eac3 11629
f4dc4d17
DE
11630static void
11631read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
11632{
11633 struct die_info *child_die;
3019eac3 11634
f4dc4d17
DE
11635 prepare_one_comp_unit (cu, die, language_minimal);
11636
11637 /* Initialize (or reinitialize) the machinery for building symtabs.
11638 We do this before processing child DIEs, so that the line header table
11639 is available for DW_AT_decl_file. */
11640 setup_type_unit_groups (die, cu);
11641
11642 if (die->child != NULL)
11643 {
11644 child_die = die->child;
11645 while (child_die && child_die->tag)
11646 {
11647 process_die (child_die, cu);
11648 child_die = sibling_die (child_die);
11649 }
11650 }
3019eac3
DE
11651}
11652\f
80626a55
DE
11653/* DWO/DWP files.
11654
11655 http://gcc.gnu.org/wiki/DebugFission
11656 http://gcc.gnu.org/wiki/DebugFissionDWP
11657
11658 To simplify handling of both DWO files ("object" files with the DWARF info)
11659 and DWP files (a file with the DWOs packaged up into one file), we treat
11660 DWP files as having a collection of virtual DWO files. */
3019eac3
DE
11661
11662static hashval_t
11663hash_dwo_file (const void *item)
11664{
9a3c8263 11665 const struct dwo_file *dwo_file = (const struct dwo_file *) item;
a2ce51a0 11666 hashval_t hash;
3019eac3 11667
a2ce51a0
DE
11668 hash = htab_hash_string (dwo_file->dwo_name);
11669 if (dwo_file->comp_dir != NULL)
11670 hash += htab_hash_string (dwo_file->comp_dir);
11671 return hash;
3019eac3
DE
11672}
11673
11674static int
11675eq_dwo_file (const void *item_lhs, const void *item_rhs)
11676{
9a3c8263
SM
11677 const struct dwo_file *lhs = (const struct dwo_file *) item_lhs;
11678 const struct dwo_file *rhs = (const struct dwo_file *) item_rhs;
3019eac3 11679
a2ce51a0
DE
11680 if (strcmp (lhs->dwo_name, rhs->dwo_name) != 0)
11681 return 0;
11682 if (lhs->comp_dir == NULL || rhs->comp_dir == NULL)
11683 return lhs->comp_dir == rhs->comp_dir;
11684 return strcmp (lhs->comp_dir, rhs->comp_dir) == 0;
3019eac3
DE
11685}
11686
11687/* Allocate a hash table for DWO files. */
11688
11689static htab_t
ed2dc618 11690allocate_dwo_file_hash_table (struct objfile *objfile)
3019eac3 11691{
3019eac3
DE
11692 return htab_create_alloc_ex (41,
11693 hash_dwo_file,
11694 eq_dwo_file,
11695 NULL,
11696 &objfile->objfile_obstack,
11697 hashtab_obstack_allocate,
11698 dummy_obstack_deallocate);
11699}
11700
80626a55
DE
11701/* Lookup DWO file DWO_NAME. */
11702
11703static void **
ed2dc618
SM
11704lookup_dwo_file_slot (struct dwarf2_per_objfile *dwarf2_per_objfile,
11705 const char *dwo_name,
11706 const char *comp_dir)
80626a55
DE
11707{
11708 struct dwo_file find_entry;
11709 void **slot;
11710
11711 if (dwarf2_per_objfile->dwo_files == NULL)
ed2dc618
SM
11712 dwarf2_per_objfile->dwo_files
11713 = allocate_dwo_file_hash_table (dwarf2_per_objfile->objfile);
80626a55
DE
11714
11715 memset (&find_entry, 0, sizeof (find_entry));
0ac5b59e
DE
11716 find_entry.dwo_name = dwo_name;
11717 find_entry.comp_dir = comp_dir;
80626a55
DE
11718 slot = htab_find_slot (dwarf2_per_objfile->dwo_files, &find_entry, INSERT);
11719
11720 return slot;
11721}
11722
3019eac3
DE
11723static hashval_t
11724hash_dwo_unit (const void *item)
11725{
9a3c8263 11726 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
3019eac3
DE
11727
11728 /* This drops the top 32 bits of the id, but is ok for a hash. */
11729 return dwo_unit->signature;
11730}
11731
11732static int
11733eq_dwo_unit (const void *item_lhs, const void *item_rhs)
11734{
9a3c8263
SM
11735 const struct dwo_unit *lhs = (const struct dwo_unit *) item_lhs;
11736 const struct dwo_unit *rhs = (const struct dwo_unit *) item_rhs;
3019eac3
DE
11737
11738 /* The signature is assumed to be unique within the DWO file.
11739 So while object file CU dwo_id's always have the value zero,
11740 that's OK, assuming each object file DWO file has only one CU,
11741 and that's the rule for now. */
11742 return lhs->signature == rhs->signature;
11743}
11744
11745/* Allocate a hash table for DWO CUs,TUs.
11746 There is one of these tables for each of CUs,TUs for each DWO file. */
11747
11748static htab_t
11749allocate_dwo_unit_table (struct objfile *objfile)
11750{
11751 /* Start out with a pretty small number.
11752 Generally DWO files contain only one CU and maybe some TUs. */
11753 return htab_create_alloc_ex (3,
11754 hash_dwo_unit,
11755 eq_dwo_unit,
11756 NULL,
11757 &objfile->objfile_obstack,
11758 hashtab_obstack_allocate,
11759 dummy_obstack_deallocate);
11760}
11761
80626a55 11762/* Structure used to pass data to create_dwo_debug_info_hash_table_reader. */
3019eac3 11763
19c3d4c9 11764struct create_dwo_cu_data
3019eac3
DE
11765{
11766 struct dwo_file *dwo_file;
19c3d4c9 11767 struct dwo_unit dwo_unit;
3019eac3
DE
11768};
11769
19c3d4c9 11770/* die_reader_func for create_dwo_cu. */
3019eac3
DE
11771
11772static void
19c3d4c9
DE
11773create_dwo_cu_reader (const struct die_reader_specs *reader,
11774 const gdb_byte *info_ptr,
11775 struct die_info *comp_unit_die,
11776 int has_children,
11777 void *datap)
3019eac3
DE
11778{
11779 struct dwarf2_cu *cu = reader->cu;
9c541725 11780 sect_offset sect_off = cu->per_cu->sect_off;
8a0459fd 11781 struct dwarf2_section_info *section = cu->per_cu->section;
9a3c8263 11782 struct create_dwo_cu_data *data = (struct create_dwo_cu_data *) datap;
3019eac3 11783 struct dwo_file *dwo_file = data->dwo_file;
19c3d4c9 11784 struct dwo_unit *dwo_unit = &data->dwo_unit;
3019eac3 11785 struct attribute *attr;
3019eac3
DE
11786
11787 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
11788 if (attr == NULL)
11789 {
b98664d3 11790 complaint (_("Dwarf Error: debug entry at offset %s is missing"
19c3d4c9 11791 " its dwo_id [in module %s]"),
9d8780f0 11792 sect_offset_str (sect_off), dwo_file->dwo_name);
3019eac3
DE
11793 return;
11794 }
11795
3019eac3
DE
11796 dwo_unit->dwo_file = dwo_file;
11797 dwo_unit->signature = DW_UNSND (attr);
8a0459fd 11798 dwo_unit->section = section;
9c541725 11799 dwo_unit->sect_off = sect_off;
3019eac3
DE
11800 dwo_unit->length = cu->per_cu->length;
11801
b4f54984 11802 if (dwarf_read_debug)
9d8780f0
SM
11803 fprintf_unfiltered (gdb_stdlog, " offset %s, dwo_id %s\n",
11804 sect_offset_str (sect_off),
9c541725 11805 hex_string (dwo_unit->signature));
3019eac3
DE
11806}
11807
33c5cd75 11808/* Create the dwo_units for the CUs in a DWO_FILE.
19c3d4c9 11809 Note: This function processes DWO files only, not DWP files. */
3019eac3 11810
33c5cd75 11811static void
ed2dc618
SM
11812create_cus_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
11813 struct dwo_file &dwo_file, dwarf2_section_info &section,
33c5cd75 11814 htab_t &cus_htab)
3019eac3
DE
11815{
11816 struct objfile *objfile = dwarf2_per_objfile->objfile;
d521ce57 11817 const gdb_byte *info_ptr, *end_ptr;
3019eac3 11818
33c5cd75
DB
11819 dwarf2_read_section (objfile, &section);
11820 info_ptr = section.buffer;
3019eac3
DE
11821
11822 if (info_ptr == NULL)
33c5cd75 11823 return;
3019eac3 11824
b4f54984 11825 if (dwarf_read_debug)
19c3d4c9
DE
11826 {
11827 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
33c5cd75
DB
11828 get_section_name (&section),
11829 get_section_file_name (&section));
19c3d4c9 11830 }
3019eac3 11831
33c5cd75 11832 end_ptr = info_ptr + section.size;
3019eac3
DE
11833 while (info_ptr < end_ptr)
11834 {
11835 struct dwarf2_per_cu_data per_cu;
33c5cd75
DB
11836 struct create_dwo_cu_data create_dwo_cu_data;
11837 struct dwo_unit *dwo_unit;
11838 void **slot;
11839 sect_offset sect_off = (sect_offset) (info_ptr - section.buffer);
3019eac3 11840
19c3d4c9
DE
11841 memset (&create_dwo_cu_data.dwo_unit, 0,
11842 sizeof (create_dwo_cu_data.dwo_unit));
3019eac3 11843 memset (&per_cu, 0, sizeof (per_cu));
e3b94546 11844 per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
3019eac3 11845 per_cu.is_debug_types = 0;
33c5cd75
DB
11846 per_cu.sect_off = sect_offset (info_ptr - section.buffer);
11847 per_cu.section = &section;
c5ed0576 11848 create_dwo_cu_data.dwo_file = &dwo_file;
33c5cd75
DB
11849
11850 init_cutu_and_read_dies_no_follow (
11851 &per_cu, &dwo_file, create_dwo_cu_reader, &create_dwo_cu_data);
11852 info_ptr += per_cu.length;
11853
11854 // If the unit could not be parsed, skip it.
11855 if (create_dwo_cu_data.dwo_unit.dwo_file == NULL)
11856 continue;
3019eac3 11857
33c5cd75
DB
11858 if (cus_htab == NULL)
11859 cus_htab = allocate_dwo_unit_table (objfile);
19c3d4c9 11860
33c5cd75
DB
11861 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
11862 *dwo_unit = create_dwo_cu_data.dwo_unit;
11863 slot = htab_find_slot (cus_htab, dwo_unit, INSERT);
11864 gdb_assert (slot != NULL);
11865 if (*slot != NULL)
19c3d4c9 11866 {
33c5cd75
DB
11867 const struct dwo_unit *dup_cu = (const struct dwo_unit *)*slot;
11868 sect_offset dup_sect_off = dup_cu->sect_off;
19c3d4c9 11869
b98664d3 11870 complaint (_("debug cu entry at offset %s is duplicate to"
9d8780f0
SM
11871 " the entry at offset %s, signature %s"),
11872 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
33c5cd75 11873 hex_string (dwo_unit->signature));
19c3d4c9 11874 }
33c5cd75 11875 *slot = (void *)dwo_unit;
3019eac3 11876 }
3019eac3
DE
11877}
11878
80626a55
DE
11879/* DWP file .debug_{cu,tu}_index section format:
11880 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
11881
d2415c6c
DE
11882 DWP Version 1:
11883
80626a55
DE
11884 Both index sections have the same format, and serve to map a 64-bit
11885 signature to a set of section numbers. Each section begins with a header,
11886 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
11887 indexes, and a pool of 32-bit section numbers. The index sections will be
11888 aligned at 8-byte boundaries in the file.
11889
d2415c6c
DE
11890 The index section header consists of:
11891
11892 V, 32 bit version number
11893 -, 32 bits unused
11894 N, 32 bit number of compilation units or type units in the index
11895 M, 32 bit number of slots in the hash table
80626a55 11896
d2415c6c 11897 Numbers are recorded using the byte order of the application binary.
80626a55 11898
d2415c6c
DE
11899 The hash table begins at offset 16 in the section, and consists of an array
11900 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
11901 order of the application binary). Unused slots in the hash table are 0.
11902 (We rely on the extreme unlikeliness of a signature being exactly 0.)
80626a55 11903
d2415c6c
DE
11904 The parallel table begins immediately after the hash table
11905 (at offset 16 + 8 * M from the beginning of the section), and consists of an
11906 array of 32-bit indexes (using the byte order of the application binary),
11907 corresponding 1-1 with slots in the hash table. Each entry in the parallel
11908 table contains a 32-bit index into the pool of section numbers. For unused
11909 hash table slots, the corresponding entry in the parallel table will be 0.
80626a55 11910
73869dc2
DE
11911 The pool of section numbers begins immediately following the hash table
11912 (at offset 16 + 12 * M from the beginning of the section). The pool of
11913 section numbers consists of an array of 32-bit words (using the byte order
11914 of the application binary). Each item in the array is indexed starting
11915 from 0. The hash table entry provides the index of the first section
11916 number in the set. Additional section numbers in the set follow, and the
11917 set is terminated by a 0 entry (section number 0 is not used in ELF).
11918
11919 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
11920 section must be the first entry in the set, and the .debug_abbrev.dwo must
11921 be the second entry. Other members of the set may follow in any order.
11922
11923 ---
11924
11925 DWP Version 2:
11926
11927 DWP Version 2 combines all the .debug_info, etc. sections into one,
11928 and the entries in the index tables are now offsets into these sections.
11929 CU offsets begin at 0. TU offsets begin at the size of the .debug_info
11930 section.
11931
11932 Index Section Contents:
11933 Header
11934 Hash Table of Signatures dwp_hash_table.hash_table
11935 Parallel Table of Indices dwp_hash_table.unit_table
11936 Table of Section Offsets dwp_hash_table.v2.{section_ids,offsets}
11937 Table of Section Sizes dwp_hash_table.v2.sizes
11938
11939 The index section header consists of:
11940
11941 V, 32 bit version number
11942 L, 32 bit number of columns in the table of section offsets
11943 N, 32 bit number of compilation units or type units in the index
11944 M, 32 bit number of slots in the hash table
11945
11946 Numbers are recorded using the byte order of the application binary.
11947
11948 The hash table has the same format as version 1.
11949 The parallel table of indices has the same format as version 1,
11950 except that the entries are origin-1 indices into the table of sections
11951 offsets and the table of section sizes.
11952
11953 The table of offsets begins immediately following the parallel table
11954 (at offset 16 + 12 * M from the beginning of the section). The table is
11955 a two-dimensional array of 32-bit words (using the byte order of the
11956 application binary), with L columns and N+1 rows, in row-major order.
11957 Each row in the array is indexed starting from 0. The first row provides
11958 a key to the remaining rows: each column in this row provides an identifier
11959 for a debug section, and the offsets in the same column of subsequent rows
11960 refer to that section. The section identifiers are:
11961
11962 DW_SECT_INFO 1 .debug_info.dwo
11963 DW_SECT_TYPES 2 .debug_types.dwo
11964 DW_SECT_ABBREV 3 .debug_abbrev.dwo
11965 DW_SECT_LINE 4 .debug_line.dwo
11966 DW_SECT_LOC 5 .debug_loc.dwo
11967 DW_SECT_STR_OFFSETS 6 .debug_str_offsets.dwo
11968 DW_SECT_MACINFO 7 .debug_macinfo.dwo
11969 DW_SECT_MACRO 8 .debug_macro.dwo
11970
11971 The offsets provided by the CU and TU index sections are the base offsets
11972 for the contributions made by each CU or TU to the corresponding section
11973 in the package file. Each CU and TU header contains an abbrev_offset
11974 field, used to find the abbreviations table for that CU or TU within the
11975 contribution to the .debug_abbrev.dwo section for that CU or TU, and should
11976 be interpreted as relative to the base offset given in the index section.
11977 Likewise, offsets into .debug_line.dwo from DW_AT_stmt_list attributes
11978 should be interpreted as relative to the base offset for .debug_line.dwo,
11979 and offsets into other debug sections obtained from DWARF attributes should
11980 also be interpreted as relative to the corresponding base offset.
11981
11982 The table of sizes begins immediately following the table of offsets.
11983 Like the table of offsets, it is a two-dimensional array of 32-bit words,
11984 with L columns and N rows, in row-major order. Each row in the array is
11985 indexed starting from 1 (row 0 is shared by the two tables).
11986
11987 ---
11988
11989 Hash table lookup is handled the same in version 1 and 2:
11990
11991 We assume that N and M will not exceed 2^32 - 1.
11992 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
11993
d2415c6c
DE
11994 Given a 64-bit compilation unit signature or a type signature S, an entry
11995 in the hash table is located as follows:
80626a55 11996
d2415c6c
DE
11997 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
11998 the low-order k bits all set to 1.
80626a55 11999
d2415c6c 12000 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
80626a55 12001
d2415c6c
DE
12002 3) If the hash table entry at index H matches the signature, use that
12003 entry. If the hash table entry at index H is unused (all zeroes),
12004 terminate the search: the signature is not present in the table.
80626a55 12005
d2415c6c 12006 4) Let H = (H + H') modulo M. Repeat at Step 3.
80626a55 12007
d2415c6c 12008 Because M > N and H' and M are relatively prime, the search is guaranteed
73869dc2 12009 to stop at an unused slot or find the match. */
80626a55
DE
12010
12011/* Create a hash table to map DWO IDs to their CU/TU entry in
12012 .debug_{info,types}.dwo in DWP_FILE.
12013 Returns NULL if there isn't one.
12014 Note: This function processes DWP files only, not DWO files. */
12015
12016static struct dwp_hash_table *
ed2dc618
SM
12017create_dwp_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
12018 struct dwp_file *dwp_file, int is_debug_types)
80626a55
DE
12019{
12020 struct objfile *objfile = dwarf2_per_objfile->objfile;
400174b1 12021 bfd *dbfd = dwp_file->dbfd.get ();
948f8e3d 12022 const gdb_byte *index_ptr, *index_end;
80626a55 12023 struct dwarf2_section_info *index;
73869dc2 12024 uint32_t version, nr_columns, nr_units, nr_slots;
80626a55
DE
12025 struct dwp_hash_table *htab;
12026
12027 if (is_debug_types)
12028 index = &dwp_file->sections.tu_index;
12029 else
12030 index = &dwp_file->sections.cu_index;
12031
12032 if (dwarf2_section_empty_p (index))
12033 return NULL;
12034 dwarf2_read_section (objfile, index);
12035
12036 index_ptr = index->buffer;
12037 index_end = index_ptr + index->size;
12038
12039 version = read_4_bytes (dbfd, index_ptr);
73869dc2
DE
12040 index_ptr += 4;
12041 if (version == 2)
12042 nr_columns = read_4_bytes (dbfd, index_ptr);
12043 else
12044 nr_columns = 0;
12045 index_ptr += 4;
80626a55
DE
12046 nr_units = read_4_bytes (dbfd, index_ptr);
12047 index_ptr += 4;
12048 nr_slots = read_4_bytes (dbfd, index_ptr);
12049 index_ptr += 4;
12050
73869dc2 12051 if (version != 1 && version != 2)
80626a55 12052 {
21aa081e 12053 error (_("Dwarf Error: unsupported DWP file version (%s)"
80626a55 12054 " [in module %s]"),
21aa081e 12055 pulongest (version), dwp_file->name);
80626a55
DE
12056 }
12057 if (nr_slots != (nr_slots & -nr_slots))
12058 {
21aa081e 12059 error (_("Dwarf Error: number of slots in DWP hash table (%s)"
80626a55 12060 " is not power of 2 [in module %s]"),
21aa081e 12061 pulongest (nr_slots), dwp_file->name);
80626a55
DE
12062 }
12063
12064 htab = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_hash_table);
73869dc2
DE
12065 htab->version = version;
12066 htab->nr_columns = nr_columns;
80626a55
DE
12067 htab->nr_units = nr_units;
12068 htab->nr_slots = nr_slots;
12069 htab->hash_table = index_ptr;
12070 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
73869dc2
DE
12071
12072 /* Exit early if the table is empty. */
12073 if (nr_slots == 0 || nr_units == 0
12074 || (version == 2 && nr_columns == 0))
12075 {
12076 /* All must be zero. */
12077 if (nr_slots != 0 || nr_units != 0
12078 || (version == 2 && nr_columns != 0))
12079 {
b98664d3 12080 complaint (_("Empty DWP but nr_slots,nr_units,nr_columns not"
73869dc2
DE
12081 " all zero [in modules %s]"),
12082 dwp_file->name);
12083 }
12084 return htab;
12085 }
12086
12087 if (version == 1)
12088 {
12089 htab->section_pool.v1.indices =
12090 htab->unit_table + sizeof (uint32_t) * nr_slots;
12091 /* It's harder to decide whether the section is too small in v1.
12092 V1 is deprecated anyway so we punt. */
12093 }
12094 else
12095 {
12096 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
12097 int *ids = htab->section_pool.v2.section_ids;
12098 /* Reverse map for error checking. */
12099 int ids_seen[DW_SECT_MAX + 1];
12100 int i;
12101
12102 if (nr_columns < 2)
12103 {
12104 error (_("Dwarf Error: bad DWP hash table, too few columns"
12105 " in section table [in module %s]"),
12106 dwp_file->name);
12107 }
12108 if (nr_columns > MAX_NR_V2_DWO_SECTIONS)
12109 {
12110 error (_("Dwarf Error: bad DWP hash table, too many columns"
12111 " in section table [in module %s]"),
12112 dwp_file->name);
12113 }
12114 memset (ids, 255, (DW_SECT_MAX + 1) * sizeof (int32_t));
12115 memset (ids_seen, 255, (DW_SECT_MAX + 1) * sizeof (int32_t));
12116 for (i = 0; i < nr_columns; ++i)
12117 {
12118 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
12119
12120 if (id < DW_SECT_MIN || id > DW_SECT_MAX)
12121 {
12122 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
12123 " in section table [in module %s]"),
12124 id, dwp_file->name);
12125 }
12126 if (ids_seen[id] != -1)
12127 {
12128 error (_("Dwarf Error: bad DWP hash table, duplicate section"
12129 " id %d in section table [in module %s]"),
12130 id, dwp_file->name);
12131 }
12132 ids_seen[id] = i;
12133 ids[i] = id;
12134 }
12135 /* Must have exactly one info or types section. */
12136 if (((ids_seen[DW_SECT_INFO] != -1)
12137 + (ids_seen[DW_SECT_TYPES] != -1))
12138 != 1)
12139 {
12140 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
12141 " DWO info/types section [in module %s]"),
12142 dwp_file->name);
12143 }
12144 /* Must have an abbrev section. */
12145 if (ids_seen[DW_SECT_ABBREV] == -1)
12146 {
12147 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
12148 " section [in module %s]"),
12149 dwp_file->name);
12150 }
12151 htab->section_pool.v2.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
12152 htab->section_pool.v2.sizes =
12153 htab->section_pool.v2.offsets + (sizeof (uint32_t)
12154 * nr_units * nr_columns);
12155 if ((htab->section_pool.v2.sizes + (sizeof (uint32_t)
12156 * nr_units * nr_columns))
12157 > index_end)
12158 {
12159 error (_("Dwarf Error: DWP index section is corrupt (too small)"
12160 " [in module %s]"),
12161 dwp_file->name);
12162 }
12163 }
80626a55
DE
12164
12165 return htab;
12166}
12167
12168/* Update SECTIONS with the data from SECTP.
12169
12170 This function is like the other "locate" section routines that are
12171 passed to bfd_map_over_sections, but in this context the sections to
73869dc2 12172 read comes from the DWP V1 hash table, not the full ELF section table.
80626a55
DE
12173
12174 The result is non-zero for success, or zero if an error was found. */
12175
12176static int
73869dc2
DE
12177locate_v1_virtual_dwo_sections (asection *sectp,
12178 struct virtual_v1_dwo_sections *sections)
80626a55
DE
12179{
12180 const struct dwop_section_names *names = &dwop_section_names;
12181
12182 if (section_is_p (sectp->name, &names->abbrev_dwo))
12183 {
12184 /* There can be only one. */
049412e3 12185 if (sections->abbrev.s.section != NULL)
80626a55 12186 return 0;
049412e3 12187 sections->abbrev.s.section = sectp;
80626a55
DE
12188 sections->abbrev.size = bfd_get_section_size (sectp);
12189 }
12190 else if (section_is_p (sectp->name, &names->info_dwo)
12191 || section_is_p (sectp->name, &names->types_dwo))
12192 {
12193 /* There can be only one. */
049412e3 12194 if (sections->info_or_types.s.section != NULL)
80626a55 12195 return 0;
049412e3 12196 sections->info_or_types.s.section = sectp;
80626a55
DE
12197 sections->info_or_types.size = bfd_get_section_size (sectp);
12198 }
12199 else if (section_is_p (sectp->name, &names->line_dwo))
12200 {
12201 /* There can be only one. */
049412e3 12202 if (sections->line.s.section != NULL)
80626a55 12203 return 0;
049412e3 12204 sections->line.s.section = sectp;
80626a55
DE
12205 sections->line.size = bfd_get_section_size (sectp);
12206 }
12207 else if (section_is_p (sectp->name, &names->loc_dwo))
12208 {
12209 /* There can be only one. */
049412e3 12210 if (sections->loc.s.section != NULL)
80626a55 12211 return 0;
049412e3 12212 sections->loc.s.section = sectp;
80626a55
DE
12213 sections->loc.size = bfd_get_section_size (sectp);
12214 }
12215 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12216 {
12217 /* There can be only one. */
049412e3 12218 if (sections->macinfo.s.section != NULL)
80626a55 12219 return 0;
049412e3 12220 sections->macinfo.s.section = sectp;
80626a55
DE
12221 sections->macinfo.size = bfd_get_section_size (sectp);
12222 }
12223 else if (section_is_p (sectp->name, &names->macro_dwo))
12224 {
12225 /* There can be only one. */
049412e3 12226 if (sections->macro.s.section != NULL)
80626a55 12227 return 0;
049412e3 12228 sections->macro.s.section = sectp;
80626a55
DE
12229 sections->macro.size = bfd_get_section_size (sectp);
12230 }
12231 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12232 {
12233 /* There can be only one. */
049412e3 12234 if (sections->str_offsets.s.section != NULL)
80626a55 12235 return 0;
049412e3 12236 sections->str_offsets.s.section = sectp;
80626a55
DE
12237 sections->str_offsets.size = bfd_get_section_size (sectp);
12238 }
12239 else
12240 {
12241 /* No other kind of section is valid. */
12242 return 0;
12243 }
12244
12245 return 1;
12246}
12247
73869dc2
DE
12248/* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
12249 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
12250 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
12251 This is for DWP version 1 files. */
80626a55
DE
12252
12253static struct dwo_unit *
ed2dc618
SM
12254create_dwo_unit_in_dwp_v1 (struct dwarf2_per_objfile *dwarf2_per_objfile,
12255 struct dwp_file *dwp_file,
73869dc2
DE
12256 uint32_t unit_index,
12257 const char *comp_dir,
12258 ULONGEST signature, int is_debug_types)
80626a55
DE
12259{
12260 struct objfile *objfile = dwarf2_per_objfile->objfile;
73869dc2
DE
12261 const struct dwp_hash_table *dwp_htab =
12262 is_debug_types ? dwp_file->tus : dwp_file->cus;
400174b1 12263 bfd *dbfd = dwp_file->dbfd.get ();
80626a55
DE
12264 const char *kind = is_debug_types ? "TU" : "CU";
12265 struct dwo_file *dwo_file;
12266 struct dwo_unit *dwo_unit;
73869dc2 12267 struct virtual_v1_dwo_sections sections;
80626a55 12268 void **dwo_file_slot;
80626a55
DE
12269 int i;
12270
73869dc2
DE
12271 gdb_assert (dwp_file->version == 1);
12272
b4f54984 12273 if (dwarf_read_debug)
80626a55 12274 {
73869dc2 12275 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V1 file: %s\n",
80626a55 12276 kind,
73869dc2 12277 pulongest (unit_index), hex_string (signature),
80626a55
DE
12278 dwp_file->name);
12279 }
12280
19ac8c2e 12281 /* Fetch the sections of this DWO unit.
80626a55
DE
12282 Put a limit on the number of sections we look for so that bad data
12283 doesn't cause us to loop forever. */
12284
73869dc2 12285#define MAX_NR_V1_DWO_SECTIONS \
80626a55
DE
12286 (1 /* .debug_info or .debug_types */ \
12287 + 1 /* .debug_abbrev */ \
12288 + 1 /* .debug_line */ \
12289 + 1 /* .debug_loc */ \
12290 + 1 /* .debug_str_offsets */ \
19ac8c2e 12291 + 1 /* .debug_macro or .debug_macinfo */ \
80626a55
DE
12292 + 1 /* trailing zero */)
12293
12294 memset (&sections, 0, sizeof (sections));
80626a55 12295
73869dc2 12296 for (i = 0; i < MAX_NR_V1_DWO_SECTIONS; ++i)
80626a55
DE
12297 {
12298 asection *sectp;
12299 uint32_t section_nr =
12300 read_4_bytes (dbfd,
73869dc2
DE
12301 dwp_htab->section_pool.v1.indices
12302 + (unit_index + i) * sizeof (uint32_t));
80626a55
DE
12303
12304 if (section_nr == 0)
12305 break;
12306 if (section_nr >= dwp_file->num_sections)
12307 {
12308 error (_("Dwarf Error: bad DWP hash table, section number too large"
12309 " [in module %s]"),
12310 dwp_file->name);
12311 }
12312
12313 sectp = dwp_file->elf_sections[section_nr];
73869dc2 12314 if (! locate_v1_virtual_dwo_sections (sectp, &sections))
80626a55
DE
12315 {
12316 error (_("Dwarf Error: bad DWP hash table, invalid section found"
12317 " [in module %s]"),
12318 dwp_file->name);
12319 }
12320 }
12321
12322 if (i < 2
a32a8923
DE
12323 || dwarf2_section_empty_p (&sections.info_or_types)
12324 || dwarf2_section_empty_p (&sections.abbrev))
80626a55
DE
12325 {
12326 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
12327 " [in module %s]"),
12328 dwp_file->name);
12329 }
73869dc2 12330 if (i == MAX_NR_V1_DWO_SECTIONS)
80626a55
DE
12331 {
12332 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
12333 " [in module %s]"),
12334 dwp_file->name);
12335 }
12336
12337 /* It's easier for the rest of the code if we fake a struct dwo_file and
12338 have dwo_unit "live" in that. At least for now.
12339
12340 The DWP file can be made up of a random collection of CUs and TUs.
c766f7ec 12341 However, for each CU + set of TUs that came from the same original DWO
57d63ce2
DE
12342 file, we can combine them back into a virtual DWO file to save space
12343 (fewer struct dwo_file objects to allocate). Remember that for really
80626a55
DE
12344 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
12345
791afaa2
TT
12346 std::string virtual_dwo_name =
12347 string_printf ("virtual-dwo/%d-%d-%d-%d",
12348 get_section_id (&sections.abbrev),
12349 get_section_id (&sections.line),
12350 get_section_id (&sections.loc),
12351 get_section_id (&sections.str_offsets));
80626a55 12352 /* Can we use an existing virtual DWO file? */
ed2dc618
SM
12353 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
12354 virtual_dwo_name.c_str (),
12355 comp_dir);
80626a55
DE
12356 /* Create one if necessary. */
12357 if (*dwo_file_slot == NULL)
12358 {
b4f54984 12359 if (dwarf_read_debug)
80626a55
DE
12360 {
12361 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
791afaa2 12362 virtual_dwo_name.c_str ());
80626a55
DE
12363 }
12364 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
224c3ddb
SM
12365 dwo_file->dwo_name
12366 = (const char *) obstack_copy0 (&objfile->objfile_obstack,
791afaa2
TT
12367 virtual_dwo_name.c_str (),
12368 virtual_dwo_name.size ());
0ac5b59e 12369 dwo_file->comp_dir = comp_dir;
80626a55
DE
12370 dwo_file->sections.abbrev = sections.abbrev;
12371 dwo_file->sections.line = sections.line;
12372 dwo_file->sections.loc = sections.loc;
12373 dwo_file->sections.macinfo = sections.macinfo;
12374 dwo_file->sections.macro = sections.macro;
12375 dwo_file->sections.str_offsets = sections.str_offsets;
12376 /* The "str" section is global to the entire DWP file. */
12377 dwo_file->sections.str = dwp_file->sections.str;
57d63ce2 12378 /* The info or types section is assigned below to dwo_unit,
80626a55
DE
12379 there's no need to record it in dwo_file.
12380 Also, we can't simply record type sections in dwo_file because
12381 we record a pointer into the vector in dwo_unit. As we collect more
12382 types we'll grow the vector and eventually have to reallocate space
57d63ce2
DE
12383 for it, invalidating all copies of pointers into the previous
12384 contents. */
80626a55
DE
12385 *dwo_file_slot = dwo_file;
12386 }
12387 else
12388 {
b4f54984 12389 if (dwarf_read_debug)
80626a55
DE
12390 {
12391 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
791afaa2 12392 virtual_dwo_name.c_str ());
80626a55 12393 }
9a3c8263 12394 dwo_file = (struct dwo_file *) *dwo_file_slot;
80626a55 12395 }
80626a55
DE
12396
12397 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
12398 dwo_unit->dwo_file = dwo_file;
12399 dwo_unit->signature = signature;
8d749320
SM
12400 dwo_unit->section =
12401 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
8a0459fd 12402 *dwo_unit->section = sections.info_or_types;
57d63ce2 12403 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
80626a55
DE
12404
12405 return dwo_unit;
12406}
12407
73869dc2
DE
12408/* Subroutine of create_dwo_unit_in_dwp_v2 to simplify it.
12409 Given a pointer to the containing section SECTION, and OFFSET,SIZE of the
12410 piece within that section used by a TU/CU, return a virtual section
12411 of just that piece. */
12412
12413static struct dwarf2_section_info
ed2dc618
SM
12414create_dwp_v2_section (struct dwarf2_per_objfile *dwarf2_per_objfile,
12415 struct dwarf2_section_info *section,
73869dc2
DE
12416 bfd_size_type offset, bfd_size_type size)
12417{
12418 struct dwarf2_section_info result;
12419 asection *sectp;
12420
12421 gdb_assert (section != NULL);
12422 gdb_assert (!section->is_virtual);
12423
12424 memset (&result, 0, sizeof (result));
12425 result.s.containing_section = section;
12426 result.is_virtual = 1;
12427
12428 if (size == 0)
12429 return result;
12430
12431 sectp = get_section_bfd_section (section);
12432
12433 /* Flag an error if the piece denoted by OFFSET,SIZE is outside the
12434 bounds of the real section. This is a pretty-rare event, so just
12435 flag an error (easier) instead of a warning and trying to cope. */
12436 if (sectp == NULL
12437 || offset + size > bfd_get_section_size (sectp))
12438 {
73869dc2
DE
12439 error (_("Dwarf Error: Bad DWP V2 section info, doesn't fit"
12440 " in section %s [in module %s]"),
12441 sectp ? bfd_section_name (abfd, sectp) : "<unknown>",
12442 objfile_name (dwarf2_per_objfile->objfile));
12443 }
12444
12445 result.virtual_offset = offset;
12446 result.size = size;
12447 return result;
12448}
12449
12450/* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
12451 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
12452 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
12453 This is for DWP version 2 files. */
12454
12455static struct dwo_unit *
ed2dc618
SM
12456create_dwo_unit_in_dwp_v2 (struct dwarf2_per_objfile *dwarf2_per_objfile,
12457 struct dwp_file *dwp_file,
73869dc2
DE
12458 uint32_t unit_index,
12459 const char *comp_dir,
12460 ULONGEST signature, int is_debug_types)
12461{
12462 struct objfile *objfile = dwarf2_per_objfile->objfile;
12463 const struct dwp_hash_table *dwp_htab =
12464 is_debug_types ? dwp_file->tus : dwp_file->cus;
400174b1 12465 bfd *dbfd = dwp_file->dbfd.get ();
73869dc2
DE
12466 const char *kind = is_debug_types ? "TU" : "CU";
12467 struct dwo_file *dwo_file;
12468 struct dwo_unit *dwo_unit;
12469 struct virtual_v2_dwo_sections sections;
12470 void **dwo_file_slot;
73869dc2
DE
12471 int i;
12472
12473 gdb_assert (dwp_file->version == 2);
12474
b4f54984 12475 if (dwarf_read_debug)
73869dc2
DE
12476 {
12477 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V2 file: %s\n",
12478 kind,
12479 pulongest (unit_index), hex_string (signature),
12480 dwp_file->name);
12481 }
12482
12483 /* Fetch the section offsets of this DWO unit. */
12484
12485 memset (&sections, 0, sizeof (sections));
73869dc2
DE
12486
12487 for (i = 0; i < dwp_htab->nr_columns; ++i)
12488 {
12489 uint32_t offset = read_4_bytes (dbfd,
12490 dwp_htab->section_pool.v2.offsets
12491 + (((unit_index - 1) * dwp_htab->nr_columns
12492 + i)
12493 * sizeof (uint32_t)));
12494 uint32_t size = read_4_bytes (dbfd,
12495 dwp_htab->section_pool.v2.sizes
12496 + (((unit_index - 1) * dwp_htab->nr_columns
12497 + i)
12498 * sizeof (uint32_t)));
12499
12500 switch (dwp_htab->section_pool.v2.section_ids[i])
12501 {
12502 case DW_SECT_INFO:
12503 case DW_SECT_TYPES:
12504 sections.info_or_types_offset = offset;
12505 sections.info_or_types_size = size;
12506 break;
12507 case DW_SECT_ABBREV:
12508 sections.abbrev_offset = offset;
12509 sections.abbrev_size = size;
12510 break;
12511 case DW_SECT_LINE:
12512 sections.line_offset = offset;
12513 sections.line_size = size;
12514 break;
12515 case DW_SECT_LOC:
12516 sections.loc_offset = offset;
12517 sections.loc_size = size;
12518 break;
12519 case DW_SECT_STR_OFFSETS:
12520 sections.str_offsets_offset = offset;
12521 sections.str_offsets_size = size;
12522 break;
12523 case DW_SECT_MACINFO:
12524 sections.macinfo_offset = offset;
12525 sections.macinfo_size = size;
12526 break;
12527 case DW_SECT_MACRO:
12528 sections.macro_offset = offset;
12529 sections.macro_size = size;
12530 break;
12531 }
12532 }
12533
12534 /* It's easier for the rest of the code if we fake a struct dwo_file and
12535 have dwo_unit "live" in that. At least for now.
12536
12537 The DWP file can be made up of a random collection of CUs and TUs.
12538 However, for each CU + set of TUs that came from the same original DWO
12539 file, we can combine them back into a virtual DWO file to save space
12540 (fewer struct dwo_file objects to allocate). Remember that for really
12541 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
12542
791afaa2
TT
12543 std::string virtual_dwo_name =
12544 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld",
12545 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
12546 (long) (sections.line_size ? sections.line_offset : 0),
12547 (long) (sections.loc_size ? sections.loc_offset : 0),
12548 (long) (sections.str_offsets_size
12549 ? sections.str_offsets_offset : 0));
73869dc2 12550 /* Can we use an existing virtual DWO file? */
ed2dc618
SM
12551 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
12552 virtual_dwo_name.c_str (),
12553 comp_dir);
73869dc2
DE
12554 /* Create one if necessary. */
12555 if (*dwo_file_slot == NULL)
12556 {
b4f54984 12557 if (dwarf_read_debug)
73869dc2
DE
12558 {
12559 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
791afaa2 12560 virtual_dwo_name.c_str ());
73869dc2
DE
12561 }
12562 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
224c3ddb
SM
12563 dwo_file->dwo_name
12564 = (const char *) obstack_copy0 (&objfile->objfile_obstack,
791afaa2
TT
12565 virtual_dwo_name.c_str (),
12566 virtual_dwo_name.size ());
73869dc2
DE
12567 dwo_file->comp_dir = comp_dir;
12568 dwo_file->sections.abbrev =
ed2dc618 12569 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.abbrev,
73869dc2
DE
12570 sections.abbrev_offset, sections.abbrev_size);
12571 dwo_file->sections.line =
ed2dc618 12572 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.line,
73869dc2
DE
12573 sections.line_offset, sections.line_size);
12574 dwo_file->sections.loc =
ed2dc618 12575 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.loc,
73869dc2
DE
12576 sections.loc_offset, sections.loc_size);
12577 dwo_file->sections.macinfo =
ed2dc618 12578 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.macinfo,
73869dc2
DE
12579 sections.macinfo_offset, sections.macinfo_size);
12580 dwo_file->sections.macro =
ed2dc618 12581 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.macro,
73869dc2
DE
12582 sections.macro_offset, sections.macro_size);
12583 dwo_file->sections.str_offsets =
ed2dc618
SM
12584 create_dwp_v2_section (dwarf2_per_objfile,
12585 &dwp_file->sections.str_offsets,
73869dc2
DE
12586 sections.str_offsets_offset,
12587 sections.str_offsets_size);
12588 /* The "str" section is global to the entire DWP file. */
12589 dwo_file->sections.str = dwp_file->sections.str;
12590 /* The info or types section is assigned below to dwo_unit,
12591 there's no need to record it in dwo_file.
12592 Also, we can't simply record type sections in dwo_file because
12593 we record a pointer into the vector in dwo_unit. As we collect more
12594 types we'll grow the vector and eventually have to reallocate space
12595 for it, invalidating all copies of pointers into the previous
12596 contents. */
12597 *dwo_file_slot = dwo_file;
12598 }
12599 else
12600 {
b4f54984 12601 if (dwarf_read_debug)
73869dc2
DE
12602 {
12603 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
791afaa2 12604 virtual_dwo_name.c_str ());
73869dc2 12605 }
9a3c8263 12606 dwo_file = (struct dwo_file *) *dwo_file_slot;
73869dc2 12607 }
73869dc2
DE
12608
12609 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
12610 dwo_unit->dwo_file = dwo_file;
12611 dwo_unit->signature = signature;
8d749320
SM
12612 dwo_unit->section =
12613 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
ed2dc618
SM
12614 *dwo_unit->section = create_dwp_v2_section (dwarf2_per_objfile,
12615 is_debug_types
73869dc2
DE
12616 ? &dwp_file->sections.types
12617 : &dwp_file->sections.info,
12618 sections.info_or_types_offset,
12619 sections.info_or_types_size);
12620 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
12621
12622 return dwo_unit;
12623}
12624
57d63ce2
DE
12625/* Lookup the DWO unit with SIGNATURE in DWP_FILE.
12626 Returns NULL if the signature isn't found. */
80626a55
DE
12627
12628static struct dwo_unit *
ed2dc618
SM
12629lookup_dwo_unit_in_dwp (struct dwarf2_per_objfile *dwarf2_per_objfile,
12630 struct dwp_file *dwp_file, const char *comp_dir,
57d63ce2 12631 ULONGEST signature, int is_debug_types)
80626a55 12632{
57d63ce2
DE
12633 const struct dwp_hash_table *dwp_htab =
12634 is_debug_types ? dwp_file->tus : dwp_file->cus;
400174b1 12635 bfd *dbfd = dwp_file->dbfd.get ();
57d63ce2 12636 uint32_t mask = dwp_htab->nr_slots - 1;
80626a55
DE
12637 uint32_t hash = signature & mask;
12638 uint32_t hash2 = ((signature >> 32) & mask) | 1;
12639 unsigned int i;
12640 void **slot;
870f88f7 12641 struct dwo_unit find_dwo_cu;
80626a55
DE
12642
12643 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
12644 find_dwo_cu.signature = signature;
19ac8c2e
DE
12645 slot = htab_find_slot (is_debug_types
12646 ? dwp_file->loaded_tus
12647 : dwp_file->loaded_cus,
12648 &find_dwo_cu, INSERT);
80626a55
DE
12649
12650 if (*slot != NULL)
9a3c8263 12651 return (struct dwo_unit *) *slot;
80626a55
DE
12652
12653 /* Use a for loop so that we don't loop forever on bad debug info. */
57d63ce2 12654 for (i = 0; i < dwp_htab->nr_slots; ++i)
80626a55
DE
12655 {
12656 ULONGEST signature_in_table;
12657
12658 signature_in_table =
57d63ce2 12659 read_8_bytes (dbfd, dwp_htab->hash_table + hash * sizeof (uint64_t));
80626a55
DE
12660 if (signature_in_table == signature)
12661 {
57d63ce2
DE
12662 uint32_t unit_index =
12663 read_4_bytes (dbfd,
12664 dwp_htab->unit_table + hash * sizeof (uint32_t));
80626a55 12665
73869dc2
DE
12666 if (dwp_file->version == 1)
12667 {
ed2dc618
SM
12668 *slot = create_dwo_unit_in_dwp_v1 (dwarf2_per_objfile,
12669 dwp_file, unit_index,
73869dc2
DE
12670 comp_dir, signature,
12671 is_debug_types);
12672 }
12673 else
12674 {
ed2dc618
SM
12675 *slot = create_dwo_unit_in_dwp_v2 (dwarf2_per_objfile,
12676 dwp_file, unit_index,
73869dc2
DE
12677 comp_dir, signature,
12678 is_debug_types);
12679 }
9a3c8263 12680 return (struct dwo_unit *) *slot;
80626a55
DE
12681 }
12682 if (signature_in_table == 0)
12683 return NULL;
12684 hash = (hash + hash2) & mask;
12685 }
12686
12687 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
12688 " [in module %s]"),
12689 dwp_file->name);
12690}
12691
ab5088bf 12692/* Subroutine of open_dwo_file,open_dwp_file to simplify them.
3019eac3
DE
12693 Open the file specified by FILE_NAME and hand it off to BFD for
12694 preliminary analysis. Return a newly initialized bfd *, which
12695 includes a canonicalized copy of FILE_NAME.
80626a55 12696 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
6ac97d4c
DE
12697 SEARCH_CWD is true if the current directory is to be searched.
12698 It will be searched before debug-file-directory.
13aaf454
DE
12699 If successful, the file is added to the bfd include table of the
12700 objfile's bfd (see gdb_bfd_record_inclusion).
6ac97d4c 12701 If unable to find/open the file, return NULL.
3019eac3
DE
12702 NOTE: This function is derived from symfile_bfd_open. */
12703
192b62ce 12704static gdb_bfd_ref_ptr
ed2dc618
SM
12705try_open_dwop_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12706 const char *file_name, int is_dwp, int search_cwd)
3019eac3 12707{
24b9144d 12708 int desc;
9c02c129
DE
12709 /* Blech. OPF_TRY_CWD_FIRST also disables searching the path list if
12710 FILE_NAME contains a '/'. So we can't use it. Instead prepend "."
12711 to debug_file_directory. */
e0cc99a6 12712 const char *search_path;
9c02c129
DE
12713 static const char dirname_separator_string[] = { DIRNAME_SEPARATOR, '\0' };
12714
e0cc99a6 12715 gdb::unique_xmalloc_ptr<char> search_path_holder;
6ac97d4c
DE
12716 if (search_cwd)
12717 {
12718 if (*debug_file_directory != '\0')
e0cc99a6
TT
12719 {
12720 search_path_holder.reset (concat (".", dirname_separator_string,
12721 debug_file_directory,
12722 (char *) NULL));
12723 search_path = search_path_holder.get ();
12724 }
6ac97d4c 12725 else
e0cc99a6 12726 search_path = ".";
6ac97d4c 12727 }
9c02c129 12728 else
e0cc99a6 12729 search_path = debug_file_directory;
3019eac3 12730
24b9144d 12731 openp_flags flags = OPF_RETURN_REALPATH;
80626a55
DE
12732 if (is_dwp)
12733 flags |= OPF_SEARCH_IN_PATH;
e0cc99a6
TT
12734
12735 gdb::unique_xmalloc_ptr<char> absolute_name;
9c02c129 12736 desc = openp (search_path, flags, file_name,
3019eac3
DE
12737 O_RDONLY | O_BINARY, &absolute_name);
12738 if (desc < 0)
12739 return NULL;
12740
e0cc99a6
TT
12741 gdb_bfd_ref_ptr sym_bfd (gdb_bfd_open (absolute_name.get (),
12742 gnutarget, desc));
9c02c129
DE
12743 if (sym_bfd == NULL)
12744 return NULL;
192b62ce 12745 bfd_set_cacheable (sym_bfd.get (), 1);
3019eac3 12746
192b62ce
TT
12747 if (!bfd_check_format (sym_bfd.get (), bfd_object))
12748 return NULL;
3019eac3 12749
13aaf454
DE
12750 /* Success. Record the bfd as having been included by the objfile's bfd.
12751 This is important because things like demangled_names_hash lives in the
12752 objfile's per_bfd space and may have references to things like symbol
12753 names that live in the DWO/DWP file's per_bfd space. PR 16426. */
192b62ce 12754 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd, sym_bfd.get ());
13aaf454 12755
3019eac3
DE
12756 return sym_bfd;
12757}
12758
ab5088bf 12759/* Try to open DWO file FILE_NAME.
3019eac3
DE
12760 COMP_DIR is the DW_AT_comp_dir attribute.
12761 The result is the bfd handle of the file.
12762 If there is a problem finding or opening the file, return NULL.
12763 Upon success, the canonicalized path of the file is stored in the bfd,
12764 same as symfile_bfd_open. */
12765
192b62ce 12766static gdb_bfd_ref_ptr
ed2dc618
SM
12767open_dwo_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12768 const char *file_name, const char *comp_dir)
3019eac3 12769{
80626a55 12770 if (IS_ABSOLUTE_PATH (file_name))
ed2dc618
SM
12771 return try_open_dwop_file (dwarf2_per_objfile, file_name,
12772 0 /*is_dwp*/, 0 /*search_cwd*/);
3019eac3
DE
12773
12774 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
12775
12776 if (comp_dir != NULL)
12777 {
b36cec19
PA
12778 char *path_to_try = concat (comp_dir, SLASH_STRING,
12779 file_name, (char *) NULL);
3019eac3
DE
12780
12781 /* NOTE: If comp_dir is a relative path, this will also try the
12782 search path, which seems useful. */
ed2dc618
SM
12783 gdb_bfd_ref_ptr abfd (try_open_dwop_file (dwarf2_per_objfile,
12784 path_to_try,
12785 0 /*is_dwp*/,
192b62ce 12786 1 /*search_cwd*/));
3019eac3
DE
12787 xfree (path_to_try);
12788 if (abfd != NULL)
12789 return abfd;
12790 }
12791
12792 /* That didn't work, try debug-file-directory, which, despite its name,
12793 is a list of paths. */
12794
12795 if (*debug_file_directory == '\0')
12796 return NULL;
12797
ed2dc618
SM
12798 return try_open_dwop_file (dwarf2_per_objfile, file_name,
12799 0 /*is_dwp*/, 1 /*search_cwd*/);
3019eac3
DE
12800}
12801
80626a55
DE
12802/* This function is mapped across the sections and remembers the offset and
12803 size of each of the DWO debugging sections we are interested in. */
12804
12805static void
12806dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp, void *dwo_sections_ptr)
12807{
9a3c8263 12808 struct dwo_sections *dwo_sections = (struct dwo_sections *) dwo_sections_ptr;
80626a55
DE
12809 const struct dwop_section_names *names = &dwop_section_names;
12810
12811 if (section_is_p (sectp->name, &names->abbrev_dwo))
12812 {
049412e3 12813 dwo_sections->abbrev.s.section = sectp;
80626a55
DE
12814 dwo_sections->abbrev.size = bfd_get_section_size (sectp);
12815 }
12816 else if (section_is_p (sectp->name, &names->info_dwo))
12817 {
049412e3 12818 dwo_sections->info.s.section = sectp;
80626a55
DE
12819 dwo_sections->info.size = bfd_get_section_size (sectp);
12820 }
12821 else if (section_is_p (sectp->name, &names->line_dwo))
12822 {
049412e3 12823 dwo_sections->line.s.section = sectp;
80626a55
DE
12824 dwo_sections->line.size = bfd_get_section_size (sectp);
12825 }
12826 else if (section_is_p (sectp->name, &names->loc_dwo))
12827 {
049412e3 12828 dwo_sections->loc.s.section = sectp;
80626a55
DE
12829 dwo_sections->loc.size = bfd_get_section_size (sectp);
12830 }
12831 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12832 {
049412e3 12833 dwo_sections->macinfo.s.section = sectp;
80626a55
DE
12834 dwo_sections->macinfo.size = bfd_get_section_size (sectp);
12835 }
12836 else if (section_is_p (sectp->name, &names->macro_dwo))
12837 {
049412e3 12838 dwo_sections->macro.s.section = sectp;
80626a55
DE
12839 dwo_sections->macro.size = bfd_get_section_size (sectp);
12840 }
12841 else if (section_is_p (sectp->name, &names->str_dwo))
12842 {
049412e3 12843 dwo_sections->str.s.section = sectp;
80626a55
DE
12844 dwo_sections->str.size = bfd_get_section_size (sectp);
12845 }
12846 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12847 {
049412e3 12848 dwo_sections->str_offsets.s.section = sectp;
80626a55
DE
12849 dwo_sections->str_offsets.size = bfd_get_section_size (sectp);
12850 }
12851 else if (section_is_p (sectp->name, &names->types_dwo))
12852 {
12853 struct dwarf2_section_info type_section;
12854
12855 memset (&type_section, 0, sizeof (type_section));
049412e3 12856 type_section.s.section = sectp;
80626a55
DE
12857 type_section.size = bfd_get_section_size (sectp);
12858 VEC_safe_push (dwarf2_section_info_def, dwo_sections->types,
12859 &type_section);
12860 }
12861}
12862
ab5088bf 12863/* Initialize the use of the DWO file specified by DWO_NAME and referenced
19c3d4c9 12864 by PER_CU. This is for the non-DWP case.
80626a55 12865 The result is NULL if DWO_NAME can't be found. */
3019eac3
DE
12866
12867static struct dwo_file *
0ac5b59e
DE
12868open_and_init_dwo_file (struct dwarf2_per_cu_data *per_cu,
12869 const char *dwo_name, const char *comp_dir)
3019eac3 12870{
ed2dc618 12871 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
3019eac3 12872 struct objfile *objfile = dwarf2_per_objfile->objfile;
3019eac3 12873
ed2dc618 12874 gdb_bfd_ref_ptr dbfd (open_dwo_file (dwarf2_per_objfile, dwo_name, comp_dir));
80626a55
DE
12875 if (dbfd == NULL)
12876 {
b4f54984 12877 if (dwarf_read_debug)
80626a55
DE
12878 fprintf_unfiltered (gdb_stdlog, "DWO file not found: %s\n", dwo_name);
12879 return NULL;
12880 }
263db9a1
TT
12881
12882 /* We use a unique pointer here, despite the obstack allocation,
12883 because a dwo_file needs some cleanup if it is abandoned. */
12884 dwo_file_up dwo_file (OBSTACK_ZALLOC (&objfile->objfile_obstack,
12885 struct dwo_file));
0ac5b59e
DE
12886 dwo_file->dwo_name = dwo_name;
12887 dwo_file->comp_dir = comp_dir;
192b62ce 12888 dwo_file->dbfd = dbfd.release ();
3019eac3 12889
192b62ce
TT
12890 bfd_map_over_sections (dwo_file->dbfd, dwarf2_locate_dwo_sections,
12891 &dwo_file->sections);
3019eac3 12892
ed2dc618
SM
12893 create_cus_hash_table (dwarf2_per_objfile, *dwo_file, dwo_file->sections.info,
12894 dwo_file->cus);
3019eac3 12895
263db9a1 12896 create_debug_types_hash_table (dwarf2_per_objfile, dwo_file.get (),
ed2dc618 12897 dwo_file->sections.types, dwo_file->tus);
3019eac3 12898
b4f54984 12899 if (dwarf_read_debug)
80626a55
DE
12900 fprintf_unfiltered (gdb_stdlog, "DWO file found: %s\n", dwo_name);
12901
263db9a1 12902 return dwo_file.release ();
3019eac3
DE
12903}
12904
80626a55 12905/* This function is mapped across the sections and remembers the offset and
73869dc2
DE
12906 size of each of the DWP debugging sections common to version 1 and 2 that
12907 we are interested in. */
3019eac3 12908
80626a55 12909static void
73869dc2
DE
12910dwarf2_locate_common_dwp_sections (bfd *abfd, asection *sectp,
12911 void *dwp_file_ptr)
3019eac3 12912{
9a3c8263 12913 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
80626a55
DE
12914 const struct dwop_section_names *names = &dwop_section_names;
12915 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
3019eac3 12916
80626a55 12917 /* Record the ELF section number for later lookup: this is what the
73869dc2 12918 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
80626a55
DE
12919 gdb_assert (elf_section_nr < dwp_file->num_sections);
12920 dwp_file->elf_sections[elf_section_nr] = sectp;
3019eac3 12921
80626a55
DE
12922 /* Look for specific sections that we need. */
12923 if (section_is_p (sectp->name, &names->str_dwo))
12924 {
049412e3 12925 dwp_file->sections.str.s.section = sectp;
80626a55
DE
12926 dwp_file->sections.str.size = bfd_get_section_size (sectp);
12927 }
12928 else if (section_is_p (sectp->name, &names->cu_index))
12929 {
049412e3 12930 dwp_file->sections.cu_index.s.section = sectp;
80626a55
DE
12931 dwp_file->sections.cu_index.size = bfd_get_section_size (sectp);
12932 }
12933 else if (section_is_p (sectp->name, &names->tu_index))
12934 {
049412e3 12935 dwp_file->sections.tu_index.s.section = sectp;
80626a55
DE
12936 dwp_file->sections.tu_index.size = bfd_get_section_size (sectp);
12937 }
12938}
3019eac3 12939
73869dc2
DE
12940/* This function is mapped across the sections and remembers the offset and
12941 size of each of the DWP version 2 debugging sections that we are interested
12942 in. This is split into a separate function because we don't know if we
12943 have version 1 or 2 until we parse the cu_index/tu_index sections. */
12944
12945static void
12946dwarf2_locate_v2_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
12947{
9a3c8263 12948 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
73869dc2
DE
12949 const struct dwop_section_names *names = &dwop_section_names;
12950 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
12951
12952 /* Record the ELF section number for later lookup: this is what the
12953 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
12954 gdb_assert (elf_section_nr < dwp_file->num_sections);
12955 dwp_file->elf_sections[elf_section_nr] = sectp;
12956
12957 /* Look for specific sections that we need. */
12958 if (section_is_p (sectp->name, &names->abbrev_dwo))
12959 {
049412e3 12960 dwp_file->sections.abbrev.s.section = sectp;
73869dc2
DE
12961 dwp_file->sections.abbrev.size = bfd_get_section_size (sectp);
12962 }
12963 else if (section_is_p (sectp->name, &names->info_dwo))
12964 {
049412e3 12965 dwp_file->sections.info.s.section = sectp;
73869dc2
DE
12966 dwp_file->sections.info.size = bfd_get_section_size (sectp);
12967 }
12968 else if (section_is_p (sectp->name, &names->line_dwo))
12969 {
049412e3 12970 dwp_file->sections.line.s.section = sectp;
73869dc2
DE
12971 dwp_file->sections.line.size = bfd_get_section_size (sectp);
12972 }
12973 else if (section_is_p (sectp->name, &names->loc_dwo))
12974 {
049412e3 12975 dwp_file->sections.loc.s.section = sectp;
73869dc2
DE
12976 dwp_file->sections.loc.size = bfd_get_section_size (sectp);
12977 }
12978 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12979 {
049412e3 12980 dwp_file->sections.macinfo.s.section = sectp;
73869dc2
DE
12981 dwp_file->sections.macinfo.size = bfd_get_section_size (sectp);
12982 }
12983 else if (section_is_p (sectp->name, &names->macro_dwo))
12984 {
049412e3 12985 dwp_file->sections.macro.s.section = sectp;
73869dc2
DE
12986 dwp_file->sections.macro.size = bfd_get_section_size (sectp);
12987 }
12988 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12989 {
049412e3 12990 dwp_file->sections.str_offsets.s.section = sectp;
73869dc2
DE
12991 dwp_file->sections.str_offsets.size = bfd_get_section_size (sectp);
12992 }
12993 else if (section_is_p (sectp->name, &names->types_dwo))
12994 {
049412e3 12995 dwp_file->sections.types.s.section = sectp;
73869dc2
DE
12996 dwp_file->sections.types.size = bfd_get_section_size (sectp);
12997 }
12998}
12999
80626a55 13000/* Hash function for dwp_file loaded CUs/TUs. */
3019eac3 13001
80626a55
DE
13002static hashval_t
13003hash_dwp_loaded_cutus (const void *item)
13004{
9a3c8263 13005 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
3019eac3 13006
80626a55
DE
13007 /* This drops the top 32 bits of the signature, but is ok for a hash. */
13008 return dwo_unit->signature;
3019eac3
DE
13009}
13010
80626a55 13011/* Equality function for dwp_file loaded CUs/TUs. */
3019eac3 13012
80626a55
DE
13013static int
13014eq_dwp_loaded_cutus (const void *a, const void *b)
3019eac3 13015{
9a3c8263
SM
13016 const struct dwo_unit *dua = (const struct dwo_unit *) a;
13017 const struct dwo_unit *dub = (const struct dwo_unit *) b;
3019eac3 13018
80626a55
DE
13019 return dua->signature == dub->signature;
13020}
3019eac3 13021
80626a55 13022/* Allocate a hash table for dwp_file loaded CUs/TUs. */
3019eac3 13023
80626a55
DE
13024static htab_t
13025allocate_dwp_loaded_cutus_table (struct objfile *objfile)
13026{
13027 return htab_create_alloc_ex (3,
13028 hash_dwp_loaded_cutus,
13029 eq_dwp_loaded_cutus,
13030 NULL,
13031 &objfile->objfile_obstack,
13032 hashtab_obstack_allocate,
13033 dummy_obstack_deallocate);
13034}
3019eac3 13035
ab5088bf
DE
13036/* Try to open DWP file FILE_NAME.
13037 The result is the bfd handle of the file.
13038 If there is a problem finding or opening the file, return NULL.
13039 Upon success, the canonicalized path of the file is stored in the bfd,
13040 same as symfile_bfd_open. */
13041
192b62ce 13042static gdb_bfd_ref_ptr
ed2dc618
SM
13043open_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
13044 const char *file_name)
ab5088bf 13045{
ed2dc618
SM
13046 gdb_bfd_ref_ptr abfd (try_open_dwop_file (dwarf2_per_objfile, file_name,
13047 1 /*is_dwp*/,
192b62ce 13048 1 /*search_cwd*/));
6ac97d4c
DE
13049 if (abfd != NULL)
13050 return abfd;
13051
13052 /* Work around upstream bug 15652.
13053 http://sourceware.org/bugzilla/show_bug.cgi?id=15652
13054 [Whether that's a "bug" is debatable, but it is getting in our way.]
13055 We have no real idea where the dwp file is, because gdb's realpath-ing
13056 of the executable's path may have discarded the needed info.
13057 [IWBN if the dwp file name was recorded in the executable, akin to
13058 .gnu_debuglink, but that doesn't exist yet.]
13059 Strip the directory from FILE_NAME and search again. */
13060 if (*debug_file_directory != '\0')
13061 {
13062 /* Don't implicitly search the current directory here.
13063 If the user wants to search "." to handle this case,
13064 it must be added to debug-file-directory. */
ed2dc618
SM
13065 return try_open_dwop_file (dwarf2_per_objfile,
13066 lbasename (file_name), 1 /*is_dwp*/,
6ac97d4c
DE
13067 0 /*search_cwd*/);
13068 }
13069
13070 return NULL;
ab5088bf
DE
13071}
13072
80626a55
DE
13073/* Initialize the use of the DWP file for the current objfile.
13074 By convention the name of the DWP file is ${objfile}.dwp.
13075 The result is NULL if it can't be found. */
a766d390 13076
400174b1 13077static std::unique_ptr<struct dwp_file>
ed2dc618 13078open_and_init_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
80626a55
DE
13079{
13080 struct objfile *objfile = dwarf2_per_objfile->objfile;
80626a55 13081
82bf32bc
JK
13082 /* Try to find first .dwp for the binary file before any symbolic links
13083 resolving. */
6c447423
DE
13084
13085 /* If the objfile is a debug file, find the name of the real binary
13086 file and get the name of dwp file from there. */
d721ba37 13087 std::string dwp_name;
6c447423
DE
13088 if (objfile->separate_debug_objfile_backlink != NULL)
13089 {
13090 struct objfile *backlink = objfile->separate_debug_objfile_backlink;
13091 const char *backlink_basename = lbasename (backlink->original_name);
6c447423 13092
d721ba37 13093 dwp_name = ldirname (objfile->original_name) + SLASH_STRING + backlink_basename;
6c447423
DE
13094 }
13095 else
d721ba37
PA
13096 dwp_name = objfile->original_name;
13097
13098 dwp_name += ".dwp";
80626a55 13099
ed2dc618 13100 gdb_bfd_ref_ptr dbfd (open_dwp_file (dwarf2_per_objfile, dwp_name.c_str ()));
82bf32bc
JK
13101 if (dbfd == NULL
13102 && strcmp (objfile->original_name, objfile_name (objfile)) != 0)
13103 {
13104 /* Try to find .dwp for the binary file after gdb_realpath resolving. */
d721ba37
PA
13105 dwp_name = objfile_name (objfile);
13106 dwp_name += ".dwp";
ed2dc618 13107 dbfd = open_dwp_file (dwarf2_per_objfile, dwp_name.c_str ());
82bf32bc
JK
13108 }
13109
80626a55
DE
13110 if (dbfd == NULL)
13111 {
b4f54984 13112 if (dwarf_read_debug)
d721ba37 13113 fprintf_unfiltered (gdb_stdlog, "DWP file not found: %s\n", dwp_name.c_str ());
400174b1 13114 return std::unique_ptr<dwp_file> ();
3019eac3 13115 }
400174b1
TT
13116
13117 const char *name = bfd_get_filename (dbfd.get ());
13118 std::unique_ptr<struct dwp_file> dwp_file
13119 (new struct dwp_file (name, std::move (dbfd)));
c906108c 13120
80626a55 13121 /* +1: section 0 is unused */
192b62ce 13122 dwp_file->num_sections = bfd_count_sections (dwp_file->dbfd) + 1;
80626a55
DE
13123 dwp_file->elf_sections =
13124 OBSTACK_CALLOC (&objfile->objfile_obstack,
13125 dwp_file->num_sections, asection *);
13126
400174b1
TT
13127 bfd_map_over_sections (dwp_file->dbfd.get (),
13128 dwarf2_locate_common_dwp_sections,
13129 dwp_file.get ());
80626a55 13130
400174b1
TT
13131 dwp_file->cus = create_dwp_hash_table (dwarf2_per_objfile, dwp_file.get (),
13132 0);
80626a55 13133
400174b1
TT
13134 dwp_file->tus = create_dwp_hash_table (dwarf2_per_objfile, dwp_file.get (),
13135 1);
80626a55 13136
73869dc2 13137 /* The DWP file version is stored in the hash table. Oh well. */
08302ed2
DE
13138 if (dwp_file->cus && dwp_file->tus
13139 && dwp_file->cus->version != dwp_file->tus->version)
73869dc2
DE
13140 {
13141 /* Technically speaking, we should try to limp along, but this is
fbcbc3fd 13142 pretty bizarre. We use pulongest here because that's the established
4d65956b 13143 portability solution (e.g, we cannot use %u for uint32_t). */
fbcbc3fd
DE
13144 error (_("Dwarf Error: DWP file CU version %s doesn't match"
13145 " TU version %s [in DWP file %s]"),
13146 pulongest (dwp_file->cus->version),
d721ba37 13147 pulongest (dwp_file->tus->version), dwp_name.c_str ());
73869dc2 13148 }
08302ed2
DE
13149
13150 if (dwp_file->cus)
13151 dwp_file->version = dwp_file->cus->version;
13152 else if (dwp_file->tus)
13153 dwp_file->version = dwp_file->tus->version;
13154 else
13155 dwp_file->version = 2;
73869dc2
DE
13156
13157 if (dwp_file->version == 2)
400174b1
TT
13158 bfd_map_over_sections (dwp_file->dbfd.get (),
13159 dwarf2_locate_v2_dwp_sections,
13160 dwp_file.get ());
73869dc2 13161
19ac8c2e
DE
13162 dwp_file->loaded_cus = allocate_dwp_loaded_cutus_table (objfile);
13163 dwp_file->loaded_tus = allocate_dwp_loaded_cutus_table (objfile);
80626a55 13164
b4f54984 13165 if (dwarf_read_debug)
80626a55
DE
13166 {
13167 fprintf_unfiltered (gdb_stdlog, "DWP file found: %s\n", dwp_file->name);
13168 fprintf_unfiltered (gdb_stdlog,
21aa081e
PA
13169 " %s CUs, %s TUs\n",
13170 pulongest (dwp_file->cus ? dwp_file->cus->nr_units : 0),
13171 pulongest (dwp_file->tus ? dwp_file->tus->nr_units : 0));
80626a55
DE
13172 }
13173
13174 return dwp_file;
3019eac3 13175}
c906108c 13176
ab5088bf
DE
13177/* Wrapper around open_and_init_dwp_file, only open it once. */
13178
13179static struct dwp_file *
ed2dc618 13180get_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
ab5088bf
DE
13181{
13182 if (! dwarf2_per_objfile->dwp_checked)
13183 {
ed2dc618
SM
13184 dwarf2_per_objfile->dwp_file
13185 = open_and_init_dwp_file (dwarf2_per_objfile);
ab5088bf
DE
13186 dwarf2_per_objfile->dwp_checked = 1;
13187 }
400174b1 13188 return dwarf2_per_objfile->dwp_file.get ();
ab5088bf
DE
13189}
13190
80626a55
DE
13191/* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
13192 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
13193 or in the DWP file for the objfile, referenced by THIS_UNIT.
3019eac3 13194 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
80626a55
DE
13195 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
13196
13197 This is called, for example, when wanting to read a variable with a
13198 complex location. Therefore we don't want to do file i/o for every call.
13199 Therefore we don't want to look for a DWO file on every call.
13200 Therefore we first see if we've already seen SIGNATURE in a DWP file,
13201 then we check if we've already seen DWO_NAME, and only THEN do we check
13202 for a DWO file.
13203
1c658ad5 13204 The result is a pointer to the dwo_unit object or NULL if we didn't find it
80626a55 13205 (dwo_id mismatch or couldn't find the DWO/DWP file). */
debd256d 13206
3019eac3 13207static struct dwo_unit *
80626a55
DE
13208lookup_dwo_cutu (struct dwarf2_per_cu_data *this_unit,
13209 const char *dwo_name, const char *comp_dir,
13210 ULONGEST signature, int is_debug_types)
3019eac3 13211{
ed2dc618 13212 struct dwarf2_per_objfile *dwarf2_per_objfile = this_unit->dwarf2_per_objfile;
3019eac3 13213 struct objfile *objfile = dwarf2_per_objfile->objfile;
80626a55
DE
13214 const char *kind = is_debug_types ? "TU" : "CU";
13215 void **dwo_file_slot;
3019eac3 13216 struct dwo_file *dwo_file;
80626a55 13217 struct dwp_file *dwp_file;
cb1df416 13218
6a506a2d
DE
13219 /* First see if there's a DWP file.
13220 If we have a DWP file but didn't find the DWO inside it, don't
13221 look for the original DWO file. It makes gdb behave differently
13222 depending on whether one is debugging in the build tree. */
cf2c3c16 13223
ed2dc618 13224 dwp_file = get_dwp_file (dwarf2_per_objfile);
80626a55 13225 if (dwp_file != NULL)
cf2c3c16 13226 {
80626a55
DE
13227 const struct dwp_hash_table *dwp_htab =
13228 is_debug_types ? dwp_file->tus : dwp_file->cus;
13229
13230 if (dwp_htab != NULL)
13231 {
13232 struct dwo_unit *dwo_cutu =
ed2dc618 13233 lookup_dwo_unit_in_dwp (dwarf2_per_objfile, dwp_file, comp_dir,
57d63ce2 13234 signature, is_debug_types);
80626a55
DE
13235
13236 if (dwo_cutu != NULL)
13237 {
b4f54984 13238 if (dwarf_read_debug)
80626a55
DE
13239 {
13240 fprintf_unfiltered (gdb_stdlog,
13241 "Virtual DWO %s %s found: @%s\n",
13242 kind, hex_string (signature),
13243 host_address_to_string (dwo_cutu));
13244 }
13245 return dwo_cutu;
13246 }
13247 }
13248 }
6a506a2d 13249 else
80626a55 13250 {
6a506a2d 13251 /* No DWP file, look for the DWO file. */
80626a55 13252
ed2dc618
SM
13253 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
13254 dwo_name, comp_dir);
6a506a2d 13255 if (*dwo_file_slot == NULL)
80626a55 13256 {
6a506a2d
DE
13257 /* Read in the file and build a table of the CUs/TUs it contains. */
13258 *dwo_file_slot = open_and_init_dwo_file (this_unit, dwo_name, comp_dir);
19c3d4c9 13259 }
6a506a2d 13260 /* NOTE: This will be NULL if unable to open the file. */
9a3c8263 13261 dwo_file = (struct dwo_file *) *dwo_file_slot;
3019eac3 13262
6a506a2d 13263 if (dwo_file != NULL)
19c3d4c9 13264 {
6a506a2d
DE
13265 struct dwo_unit *dwo_cutu = NULL;
13266
13267 if (is_debug_types && dwo_file->tus)
13268 {
13269 struct dwo_unit find_dwo_cutu;
13270
13271 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
13272 find_dwo_cutu.signature = signature;
9a3c8263
SM
13273 dwo_cutu
13274 = (struct dwo_unit *) htab_find (dwo_file->tus, &find_dwo_cutu);
6a506a2d 13275 }
33c5cd75 13276 else if (!is_debug_types && dwo_file->cus)
80626a55 13277 {
33c5cd75
DB
13278 struct dwo_unit find_dwo_cutu;
13279
13280 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
13281 find_dwo_cutu.signature = signature;
13282 dwo_cutu = (struct dwo_unit *)htab_find (dwo_file->cus,
13283 &find_dwo_cutu);
6a506a2d
DE
13284 }
13285
13286 if (dwo_cutu != NULL)
13287 {
b4f54984 13288 if (dwarf_read_debug)
6a506a2d
DE
13289 {
13290 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) found: @%s\n",
13291 kind, dwo_name, hex_string (signature),
13292 host_address_to_string (dwo_cutu));
13293 }
13294 return dwo_cutu;
80626a55
DE
13295 }
13296 }
2e276125 13297 }
9cdd5dbd 13298
80626a55
DE
13299 /* We didn't find it. This could mean a dwo_id mismatch, or
13300 someone deleted the DWO/DWP file, or the search path isn't set up
13301 correctly to find the file. */
13302
b4f54984 13303 if (dwarf_read_debug)
80626a55
DE
13304 {
13305 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) not found\n",
13306 kind, dwo_name, hex_string (signature));
13307 }
3019eac3 13308
6656a72d
DE
13309 /* This is a warning and not a complaint because it can be caused by
13310 pilot error (e.g., user accidentally deleting the DWO). */
43942612
DE
13311 {
13312 /* Print the name of the DWP file if we looked there, helps the user
13313 better diagnose the problem. */
791afaa2 13314 std::string dwp_text;
43942612
DE
13315
13316 if (dwp_file != NULL)
791afaa2
TT
13317 dwp_text = string_printf (" [in DWP file %s]",
13318 lbasename (dwp_file->name));
43942612 13319
9d8780f0 13320 warning (_("Could not find DWO %s %s(%s)%s referenced by %s at offset %s"
43942612
DE
13321 " [in module %s]"),
13322 kind, dwo_name, hex_string (signature),
791afaa2 13323 dwp_text.c_str (),
43942612 13324 this_unit->is_debug_types ? "TU" : "CU",
9d8780f0 13325 sect_offset_str (this_unit->sect_off), objfile_name (objfile));
43942612 13326 }
3019eac3 13327 return NULL;
5fb290d7
DJ
13328}
13329
80626a55
DE
13330/* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
13331 See lookup_dwo_cutu_unit for details. */
13332
13333static struct dwo_unit *
13334lookup_dwo_comp_unit (struct dwarf2_per_cu_data *this_cu,
13335 const char *dwo_name, const char *comp_dir,
13336 ULONGEST signature)
13337{
13338 return lookup_dwo_cutu (this_cu, dwo_name, comp_dir, signature, 0);
13339}
13340
13341/* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
13342 See lookup_dwo_cutu_unit for details. */
13343
13344static struct dwo_unit *
13345lookup_dwo_type_unit (struct signatured_type *this_tu,
13346 const char *dwo_name, const char *comp_dir)
13347{
13348 return lookup_dwo_cutu (&this_tu->per_cu, dwo_name, comp_dir, this_tu->signature, 1);
13349}
13350
89e63ee4
DE
13351/* Traversal function for queue_and_load_all_dwo_tus. */
13352
13353static int
13354queue_and_load_dwo_tu (void **slot, void *info)
13355{
13356 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
13357 struct dwarf2_per_cu_data *per_cu = (struct dwarf2_per_cu_data *) info;
13358 ULONGEST signature = dwo_unit->signature;
13359 struct signatured_type *sig_type =
13360 lookup_dwo_signatured_type (per_cu->cu, signature);
13361
13362 if (sig_type != NULL)
13363 {
13364 struct dwarf2_per_cu_data *sig_cu = &sig_type->per_cu;
13365
13366 /* We pass NULL for DEPENDENT_CU because we don't yet know if there's
13367 a real dependency of PER_CU on SIG_TYPE. That is detected later
13368 while processing PER_CU. */
13369 if (maybe_queue_comp_unit (NULL, sig_cu, per_cu->cu->language))
13370 load_full_type_unit (sig_cu);
13371 VEC_safe_push (dwarf2_per_cu_ptr, per_cu->imported_symtabs, sig_cu);
13372 }
13373
13374 return 1;
13375}
13376
13377/* Queue all TUs contained in the DWO of PER_CU to be read in.
13378 The DWO may have the only definition of the type, though it may not be
13379 referenced anywhere in PER_CU. Thus we have to load *all* its TUs.
13380 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
13381
13382static void
13383queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *per_cu)
13384{
13385 struct dwo_unit *dwo_unit;
13386 struct dwo_file *dwo_file;
13387
13388 gdb_assert (!per_cu->is_debug_types);
ed2dc618 13389 gdb_assert (get_dwp_file (per_cu->dwarf2_per_objfile) == NULL);
89e63ee4
DE
13390 gdb_assert (per_cu->cu != NULL);
13391
13392 dwo_unit = per_cu->cu->dwo_unit;
13393 gdb_assert (dwo_unit != NULL);
13394
13395 dwo_file = dwo_unit->dwo_file;
13396 if (dwo_file->tus != NULL)
13397 htab_traverse_noresize (dwo_file->tus, queue_and_load_dwo_tu, per_cu);
13398}
13399
3019eac3 13400/* Free all resources associated with DWO_FILE.
5dafb3d1 13401 Close the DWO file and munmap the sections. */
348e048f
DE
13402
13403static void
5dafb3d1 13404free_dwo_file (struct dwo_file *dwo_file)
348e048f 13405{
5c6fa7ab 13406 /* Note: dbfd is NULL for virtual DWO files. */
80626a55 13407 gdb_bfd_unref (dwo_file->dbfd);
348e048f 13408
3019eac3
DE
13409 VEC_free (dwarf2_section_info_def, dwo_file->sections.types);
13410}
348e048f 13411
3019eac3 13412/* Traversal function for free_dwo_files. */
2ab95328 13413
3019eac3
DE
13414static int
13415free_dwo_file_from_slot (void **slot, void *info)
13416{
13417 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
348e048f 13418
5dafb3d1 13419 free_dwo_file (dwo_file);
348e048f 13420
3019eac3
DE
13421 return 1;
13422}
348e048f 13423
3019eac3 13424/* Free all resources associated with DWO_FILES. */
348e048f 13425
3019eac3
DE
13426static void
13427free_dwo_files (htab_t dwo_files, struct objfile *objfile)
13428{
13429 htab_traverse_noresize (dwo_files, free_dwo_file_from_slot, objfile);
348e048f 13430}
3019eac3
DE
13431\f
13432/* Read in various DIEs. */
348e048f 13433
d389af10 13434/* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
3e43a32a
MS
13435 Inherit only the children of the DW_AT_abstract_origin DIE not being
13436 already referenced by DW_AT_abstract_origin from the children of the
13437 current DIE. */
d389af10
JK
13438
13439static void
13440inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
13441{
13442 struct die_info *child_die;
791afaa2 13443 sect_offset *offsetp;
d389af10
JK
13444 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
13445 struct die_info *origin_die;
13446 /* Iterator of the ORIGIN_DIE children. */
13447 struct die_info *origin_child_die;
d389af10 13448 struct attribute *attr;
cd02d79d
PA
13449 struct dwarf2_cu *origin_cu;
13450 struct pending **origin_previous_list_in_scope;
d389af10
JK
13451
13452 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13453 if (!attr)
13454 return;
13455
cd02d79d
PA
13456 /* Note that following die references may follow to a die in a
13457 different cu. */
13458
13459 origin_cu = cu;
13460 origin_die = follow_die_ref (die, attr, &origin_cu);
13461
13462 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
13463 symbols in. */
13464 origin_previous_list_in_scope = origin_cu->list_in_scope;
13465 origin_cu->list_in_scope = cu->list_in_scope;
13466
edb3359d
DJ
13467 if (die->tag != origin_die->tag
13468 && !(die->tag == DW_TAG_inlined_subroutine
13469 && origin_die->tag == DW_TAG_subprogram))
b98664d3 13470 complaint (_("DIE %s and its abstract origin %s have different tags"),
9d8780f0
SM
13471 sect_offset_str (die->sect_off),
13472 sect_offset_str (origin_die->sect_off));
d389af10 13473
791afaa2 13474 std::vector<sect_offset> offsets;
d389af10 13475
3ea89b92
PMR
13476 for (child_die = die->child;
13477 child_die && child_die->tag;
13478 child_die = sibling_die (child_die))
13479 {
13480 struct die_info *child_origin_die;
13481 struct dwarf2_cu *child_origin_cu;
13482
13483 /* We are trying to process concrete instance entries:
216f72a1 13484 DW_TAG_call_site DIEs indeed have a DW_AT_abstract_origin tag, but
3ea89b92
PMR
13485 it's not relevant to our analysis here. i.e. detecting DIEs that are
13486 present in the abstract instance but not referenced in the concrete
13487 one. */
216f72a1
JK
13488 if (child_die->tag == DW_TAG_call_site
13489 || child_die->tag == DW_TAG_GNU_call_site)
3ea89b92
PMR
13490 continue;
13491
c38f313d
DJ
13492 /* For each CHILD_DIE, find the corresponding child of
13493 ORIGIN_DIE. If there is more than one layer of
13494 DW_AT_abstract_origin, follow them all; there shouldn't be,
13495 but GCC versions at least through 4.4 generate this (GCC PR
13496 40573). */
3ea89b92
PMR
13497 child_origin_die = child_die;
13498 child_origin_cu = cu;
c38f313d
DJ
13499 while (1)
13500 {
cd02d79d
PA
13501 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
13502 child_origin_cu);
c38f313d
DJ
13503 if (attr == NULL)
13504 break;
cd02d79d
PA
13505 child_origin_die = follow_die_ref (child_origin_die, attr,
13506 &child_origin_cu);
c38f313d
DJ
13507 }
13508
d389af10
JK
13509 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
13510 counterpart may exist. */
c38f313d 13511 if (child_origin_die != child_die)
d389af10 13512 {
edb3359d
DJ
13513 if (child_die->tag != child_origin_die->tag
13514 && !(child_die->tag == DW_TAG_inlined_subroutine
13515 && child_origin_die->tag == DW_TAG_subprogram))
b98664d3 13516 complaint (_("Child DIE %s and its abstract origin %s have "
9c541725 13517 "different tags"),
9d8780f0
SM
13518 sect_offset_str (child_die->sect_off),
13519 sect_offset_str (child_origin_die->sect_off));
c38f313d 13520 if (child_origin_die->parent != origin_die)
b98664d3 13521 complaint (_("Child DIE %s and its abstract origin %s have "
9c541725 13522 "different parents"),
9d8780f0
SM
13523 sect_offset_str (child_die->sect_off),
13524 sect_offset_str (child_origin_die->sect_off));
c38f313d 13525 else
791afaa2 13526 offsets.push_back (child_origin_die->sect_off);
d389af10 13527 }
d389af10 13528 }
791afaa2
TT
13529 std::sort (offsets.begin (), offsets.end ());
13530 sect_offset *offsets_end = offsets.data () + offsets.size ();
13531 for (offsetp = offsets.data () + 1; offsetp < offsets_end; offsetp++)
9c541725 13532 if (offsetp[-1] == *offsetp)
b98664d3 13533 complaint (_("Multiple children of DIE %s refer "
9d8780f0
SM
13534 "to DIE %s as their abstract origin"),
13535 sect_offset_str (die->sect_off), sect_offset_str (*offsetp));
d389af10 13536
791afaa2 13537 offsetp = offsets.data ();
d389af10
JK
13538 origin_child_die = origin_die->child;
13539 while (origin_child_die && origin_child_die->tag)
13540 {
13541 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
b64f50a1 13542 while (offsetp < offsets_end
9c541725 13543 && *offsetp < origin_child_die->sect_off)
d389af10 13544 offsetp++;
b64f50a1 13545 if (offsetp >= offsets_end
9c541725 13546 || *offsetp > origin_child_die->sect_off)
d389af10 13547 {
adde2bff
DE
13548 /* Found that ORIGIN_CHILD_DIE is really not referenced.
13549 Check whether we're already processing ORIGIN_CHILD_DIE.
13550 This can happen with mutually referenced abstract_origins.
13551 PR 16581. */
13552 if (!origin_child_die->in_process)
13553 process_die (origin_child_die, origin_cu);
d389af10
JK
13554 }
13555 origin_child_die = sibling_die (origin_child_die);
13556 }
cd02d79d 13557 origin_cu->list_in_scope = origin_previous_list_in_scope;
d389af10
JK
13558}
13559
c906108c 13560static void
e7c27a73 13561read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 13562{
518817b3 13563 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
3e29f34a 13564 struct gdbarch *gdbarch = get_objfile_arch (objfile);
fe978cb0 13565 struct context_stack *newobj;
c906108c
SS
13566 CORE_ADDR lowpc;
13567 CORE_ADDR highpc;
13568 struct die_info *child_die;
edb3359d 13569 struct attribute *attr, *call_line, *call_file;
15d034d0 13570 const char *name;
e142c38c 13571 CORE_ADDR baseaddr;
801e3a5b 13572 struct block *block;
edb3359d 13573 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
2f4732b0 13574 std::vector<struct symbol *> template_args;
34eaf542 13575 struct template_symbol *templ_func = NULL;
edb3359d
DJ
13576
13577 if (inlined_func)
13578 {
13579 /* If we do not have call site information, we can't show the
13580 caller of this inlined function. That's too confusing, so
13581 only use the scope for local variables. */
13582 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
13583 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
13584 if (call_line == NULL || call_file == NULL)
13585 {
13586 read_lexical_block_scope (die, cu);
13587 return;
13588 }
13589 }
c906108c 13590
e142c38c
DJ
13591 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
13592
94af9270 13593 name = dwarf2_name (die, cu);
c906108c 13594
e8d05480
JB
13595 /* Ignore functions with missing or empty names. These are actually
13596 illegal according to the DWARF standard. */
13597 if (name == NULL)
13598 {
b98664d3 13599 complaint (_("missing name for subprogram DIE at %s"),
9d8780f0 13600 sect_offset_str (die->sect_off));
e8d05480
JB
13601 return;
13602 }
13603
13604 /* Ignore functions with missing or invalid low and high pc attributes. */
3a2b436a 13605 if (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL)
e385593e 13606 <= PC_BOUNDS_INVALID)
e8d05480 13607 {
ae4d0c03
PM
13608 attr = dwarf2_attr (die, DW_AT_external, cu);
13609 if (!attr || !DW_UNSND (attr))
b98664d3 13610 complaint (_("cannot get low and high bounds "
9d8780f0
SM
13611 "for subprogram DIE at %s"),
13612 sect_offset_str (die->sect_off));
e8d05480
JB
13613 return;
13614 }
c906108c 13615
3e29f34a
MR
13616 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13617 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
c906108c 13618
34eaf542
TT
13619 /* If we have any template arguments, then we must allocate a
13620 different sort of symbol. */
13621 for (child_die = die->child; child_die; child_die = sibling_die (child_die))
13622 {
13623 if (child_die->tag == DW_TAG_template_type_param
13624 || child_die->tag == DW_TAG_template_value_param)
13625 {
e623cf5d 13626 templ_func = allocate_template_symbol (objfile);
cf724bc9 13627 templ_func->subclass = SYMBOL_TEMPLATE;
34eaf542
TT
13628 break;
13629 }
13630 }
13631
804d2729 13632 newobj = cu->builder->push_context (0, lowpc);
5e2db402
TT
13633 newobj->name = new_symbol (die, read_type_die (die, cu), cu,
13634 (struct symbol *) templ_func);
4c2df51b 13635
4cecd739
DJ
13636 /* If there is a location expression for DW_AT_frame_base, record
13637 it. */
e142c38c 13638 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
4c2df51b 13639 if (attr)
fe978cb0 13640 dwarf2_symbol_mark_computed (attr, newobj->name, cu, 1);
4c2df51b 13641
63e43d3a
PMR
13642 /* If there is a location for the static link, record it. */
13643 newobj->static_link = NULL;
13644 attr = dwarf2_attr (die, DW_AT_static_link, cu);
13645 if (attr)
13646 {
224c3ddb
SM
13647 newobj->static_link
13648 = XOBNEW (&objfile->objfile_obstack, struct dynamic_prop);
63e43d3a
PMR
13649 attr_to_dynamic_prop (attr, die, cu, newobj->static_link);
13650 }
13651
804d2729 13652 cu->list_in_scope = cu->builder->get_local_symbols ();
c906108c 13653
639d11d3 13654 if (die->child != NULL)
c906108c 13655 {
639d11d3 13656 child_die = die->child;
c906108c
SS
13657 while (child_die && child_die->tag)
13658 {
34eaf542
TT
13659 if (child_die->tag == DW_TAG_template_type_param
13660 || child_die->tag == DW_TAG_template_value_param)
13661 {
13662 struct symbol *arg = new_symbol (child_die, NULL, cu);
13663
f1078f66 13664 if (arg != NULL)
2f4732b0 13665 template_args.push_back (arg);
34eaf542
TT
13666 }
13667 else
13668 process_die (child_die, cu);
c906108c
SS
13669 child_die = sibling_die (child_die);
13670 }
13671 }
13672
d389af10
JK
13673 inherit_abstract_dies (die, cu);
13674
4a811a97
UW
13675 /* If we have a DW_AT_specification, we might need to import using
13676 directives from the context of the specification DIE. See the
13677 comment in determine_prefix. */
13678 if (cu->language == language_cplus
13679 && dwarf2_attr (die, DW_AT_specification, cu))
13680 {
13681 struct dwarf2_cu *spec_cu = cu;
13682 struct die_info *spec_die = die_specification (die, &spec_cu);
13683
13684 while (spec_die)
13685 {
13686 child_die = spec_die->child;
13687 while (child_die && child_die->tag)
13688 {
13689 if (child_die->tag == DW_TAG_imported_module)
13690 process_die (child_die, spec_cu);
13691 child_die = sibling_die (child_die);
13692 }
13693
13694 /* In some cases, GCC generates specification DIEs that
13695 themselves contain DW_AT_specification attributes. */
13696 spec_die = die_specification (spec_die, &spec_cu);
13697 }
13698 }
13699
804d2729 13700 struct context_stack cstk = cu->builder->pop_context ();
c906108c 13701 /* Make a block for the local symbols within. */
804d2729
TT
13702 block = cu->builder->finish_block (cstk.name, cstk.old_blocks,
13703 cstk.static_link, lowpc, highpc);
801e3a5b 13704
df8a16a1 13705 /* For C++, set the block's scope. */
45280282
IB
13706 if ((cu->language == language_cplus
13707 || cu->language == language_fortran
c44af4eb
TT
13708 || cu->language == language_d
13709 || cu->language == language_rust)
4d4ec4e5 13710 && cu->processing_has_namespace_info)
195a3f6c
TT
13711 block_set_scope (block, determine_prefix (die, cu),
13712 &objfile->objfile_obstack);
df8a16a1 13713
801e3a5b
JB
13714 /* If we have address ranges, record them. */
13715 dwarf2_record_block_ranges (die, block, baseaddr, cu);
6e70227d 13716
a60f3166 13717 gdbarch_make_symbol_special (gdbarch, cstk.name, objfile);
3e29f34a 13718
34eaf542 13719 /* Attach template arguments to function. */
2f4732b0 13720 if (!template_args.empty ())
34eaf542
TT
13721 {
13722 gdb_assert (templ_func != NULL);
13723
2f4732b0 13724 templ_func->n_template_arguments = template_args.size ();
34eaf542 13725 templ_func->template_arguments
8d749320
SM
13726 = XOBNEWVEC (&objfile->objfile_obstack, struct symbol *,
13727 templ_func->n_template_arguments);
34eaf542 13728 memcpy (templ_func->template_arguments,
2f4732b0 13729 template_args.data (),
34eaf542 13730 (templ_func->n_template_arguments * sizeof (struct symbol *)));
34eaf542
TT
13731 }
13732
208d8187
JB
13733 /* In C++, we can have functions nested inside functions (e.g., when
13734 a function declares a class that has methods). This means that
13735 when we finish processing a function scope, we may need to go
13736 back to building a containing block's symbol lists. */
804d2729
TT
13737 *cu->builder->get_local_symbols () = cstk.locals;
13738 cu->builder->set_local_using_directives (cstk.local_using_directives);
208d8187 13739
921e78cf
JB
13740 /* If we've finished processing a top-level function, subsequent
13741 symbols go in the file symbol list. */
804d2729
TT
13742 if (cu->builder->outermost_context_p ())
13743 cu->list_in_scope = cu->builder->get_file_symbols ();
c906108c
SS
13744}
13745
13746/* Process all the DIES contained within a lexical block scope. Start
13747 a new scope, process the dies, and then close the scope. */
13748
13749static void
e7c27a73 13750read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 13751{
518817b3 13752 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
3e29f34a 13753 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
13754 CORE_ADDR lowpc, highpc;
13755 struct die_info *child_die;
e142c38c
DJ
13756 CORE_ADDR baseaddr;
13757
13758 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c
SS
13759
13760 /* Ignore blocks with missing or invalid low and high pc attributes. */
af34e669
DJ
13761 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
13762 as multiple lexical blocks? Handling children in a sane way would
6e70227d 13763 be nasty. Might be easier to properly extend generic blocks to
af34e669 13764 describe ranges. */
e385593e
JK
13765 switch (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
13766 {
13767 case PC_BOUNDS_NOT_PRESENT:
13768 /* DW_TAG_lexical_block has no attributes, process its children as if
13769 there was no wrapping by that DW_TAG_lexical_block.
13770 GCC does no longer produces such DWARF since GCC r224161. */
13771 for (child_die = die->child;
13772 child_die != NULL && child_die->tag;
13773 child_die = sibling_die (child_die))
13774 process_die (child_die, cu);
13775 return;
13776 case PC_BOUNDS_INVALID:
13777 return;
13778 }
3e29f34a
MR
13779 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13780 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
c906108c 13781
804d2729 13782 cu->builder->push_context (0, lowpc);
639d11d3 13783 if (die->child != NULL)
c906108c 13784 {
639d11d3 13785 child_die = die->child;
c906108c
SS
13786 while (child_die && child_die->tag)
13787 {
e7c27a73 13788 process_die (child_die, cu);
c906108c
SS
13789 child_die = sibling_die (child_die);
13790 }
13791 }
3ea89b92 13792 inherit_abstract_dies (die, cu);
804d2729 13793 struct context_stack cstk = cu->builder->pop_context ();
c906108c 13794
804d2729
TT
13795 if (*cu->builder->get_local_symbols () != NULL
13796 || (*cu->builder->get_local_using_directives ()) != NULL)
c906108c 13797 {
801e3a5b 13798 struct block *block
804d2729
TT
13799 = cu->builder->finish_block (0, cstk.old_blocks, NULL,
13800 cstk.start_addr, highpc);
801e3a5b
JB
13801
13802 /* Note that recording ranges after traversing children, as we
13803 do here, means that recording a parent's ranges entails
13804 walking across all its children's ranges as they appear in
13805 the address map, which is quadratic behavior.
13806
13807 It would be nicer to record the parent's ranges before
13808 traversing its children, simply overriding whatever you find
13809 there. But since we don't even decide whether to create a
13810 block until after we've traversed its children, that's hard
13811 to do. */
13812 dwarf2_record_block_ranges (die, block, baseaddr, cu);
c906108c 13813 }
804d2729
TT
13814 *cu->builder->get_local_symbols () = cstk.locals;
13815 cu->builder->set_local_using_directives (cstk.local_using_directives);
c906108c
SS
13816}
13817
216f72a1 13818/* Read in DW_TAG_call_site and insert it to CU->call_site_htab. */
96408a79
SA
13819
13820static void
13821read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
13822{
518817b3 13823 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
96408a79
SA
13824 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13825 CORE_ADDR pc, baseaddr;
13826 struct attribute *attr;
13827 struct call_site *call_site, call_site_local;
13828 void **slot;
13829 int nparams;
13830 struct die_info *child_die;
13831
13832 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
13833
216f72a1
JK
13834 attr = dwarf2_attr (die, DW_AT_call_return_pc, cu);
13835 if (attr == NULL)
13836 {
13837 /* This was a pre-DWARF-5 GNU extension alias
13838 for DW_AT_call_return_pc. */
13839 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
13840 }
96408a79
SA
13841 if (!attr)
13842 {
b98664d3 13843 complaint (_("missing DW_AT_call_return_pc for DW_TAG_call_site "
9d8780f0
SM
13844 "DIE %s [in module %s]"),
13845 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79
SA
13846 return;
13847 }
31aa7e4e 13848 pc = attr_value_as_address (attr) + baseaddr;
3e29f34a 13849 pc = gdbarch_adjust_dwarf2_addr (gdbarch, pc);
96408a79
SA
13850
13851 if (cu->call_site_htab == NULL)
13852 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
13853 NULL, &objfile->objfile_obstack,
13854 hashtab_obstack_allocate, NULL);
13855 call_site_local.pc = pc;
13856 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
13857 if (*slot != NULL)
13858 {
b98664d3 13859 complaint (_("Duplicate PC %s for DW_TAG_call_site "
9d8780f0
SM
13860 "DIE %s [in module %s]"),
13861 paddress (gdbarch, pc), sect_offset_str (die->sect_off),
4262abfb 13862 objfile_name (objfile));
96408a79
SA
13863 return;
13864 }
13865
13866 /* Count parameters at the caller. */
13867
13868 nparams = 0;
13869 for (child_die = die->child; child_die && child_die->tag;
13870 child_die = sibling_die (child_die))
13871 {
216f72a1
JK
13872 if (child_die->tag != DW_TAG_call_site_parameter
13873 && child_die->tag != DW_TAG_GNU_call_site_parameter)
96408a79 13874 {
b98664d3 13875 complaint (_("Tag %d is not DW_TAG_call_site_parameter in "
9d8780f0
SM
13876 "DW_TAG_call_site child DIE %s [in module %s]"),
13877 child_die->tag, sect_offset_str (child_die->sect_off),
4262abfb 13878 objfile_name (objfile));
96408a79
SA
13879 continue;
13880 }
13881
13882 nparams++;
13883 }
13884
224c3ddb
SM
13885 call_site
13886 = ((struct call_site *)
13887 obstack_alloc (&objfile->objfile_obstack,
13888 sizeof (*call_site)
13889 + (sizeof (*call_site->parameter) * (nparams - 1))));
96408a79
SA
13890 *slot = call_site;
13891 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
13892 call_site->pc = pc;
13893
216f72a1
JK
13894 if (dwarf2_flag_true_p (die, DW_AT_call_tail_call, cu)
13895 || dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
96408a79
SA
13896 {
13897 struct die_info *func_die;
13898
13899 /* Skip also over DW_TAG_inlined_subroutine. */
13900 for (func_die = die->parent;
13901 func_die && func_die->tag != DW_TAG_subprogram
13902 && func_die->tag != DW_TAG_subroutine_type;
13903 func_die = func_die->parent);
13904
216f72a1
JK
13905 /* DW_AT_call_all_calls is a superset
13906 of DW_AT_call_all_tail_calls. */
96408a79 13907 if (func_die
216f72a1 13908 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_calls, cu)
96408a79 13909 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
216f72a1 13910 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_tail_calls, cu)
96408a79
SA
13911 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
13912 {
13913 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
13914 not complete. But keep CALL_SITE for look ups via call_site_htab,
13915 both the initial caller containing the real return address PC and
13916 the final callee containing the current PC of a chain of tail
13917 calls do not need to have the tail call list complete. But any
13918 function candidate for a virtual tail call frame searched via
13919 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
13920 determined unambiguously. */
13921 }
13922 else
13923 {
13924 struct type *func_type = NULL;
13925
13926 if (func_die)
13927 func_type = get_die_type (func_die, cu);
13928 if (func_type != NULL)
13929 {
13930 gdb_assert (TYPE_CODE (func_type) == TYPE_CODE_FUNC);
13931
13932 /* Enlist this call site to the function. */
13933 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
13934 TYPE_TAIL_CALL_LIST (func_type) = call_site;
13935 }
13936 else
b98664d3 13937 complaint (_("Cannot find function owning DW_TAG_call_site "
9d8780f0
SM
13938 "DIE %s [in module %s]"),
13939 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79
SA
13940 }
13941 }
13942
216f72a1
JK
13943 attr = dwarf2_attr (die, DW_AT_call_target, cu);
13944 if (attr == NULL)
13945 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
13946 if (attr == NULL)
13947 attr = dwarf2_attr (die, DW_AT_call_origin, cu);
96408a79 13948 if (attr == NULL)
216f72a1
JK
13949 {
13950 /* This was a pre-DWARF-5 GNU extension alias for DW_AT_call_origin. */
13951 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13952 }
96408a79
SA
13953 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
13954 if (!attr || (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0))
13955 /* Keep NULL DWARF_BLOCK. */;
13956 else if (attr_form_is_block (attr))
13957 {
13958 struct dwarf2_locexpr_baton *dlbaton;
13959
8d749320 13960 dlbaton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
96408a79
SA
13961 dlbaton->data = DW_BLOCK (attr)->data;
13962 dlbaton->size = DW_BLOCK (attr)->size;
13963 dlbaton->per_cu = cu->per_cu;
13964
13965 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
13966 }
7771576e 13967 else if (attr_form_is_ref (attr))
96408a79 13968 {
96408a79
SA
13969 struct dwarf2_cu *target_cu = cu;
13970 struct die_info *target_die;
13971
ac9ec31b 13972 target_die = follow_die_ref (die, attr, &target_cu);
518817b3 13973 gdb_assert (target_cu->per_cu->dwarf2_per_objfile->objfile == objfile);
96408a79
SA
13974 if (die_is_declaration (target_die, target_cu))
13975 {
7d45c7c3 13976 const char *target_physname;
9112db09
JK
13977
13978 /* Prefer the mangled name; otherwise compute the demangled one. */
73b9be8b 13979 target_physname = dw2_linkage_name (target_die, target_cu);
7d45c7c3 13980 if (target_physname == NULL)
9112db09 13981 target_physname = dwarf2_physname (NULL, target_die, target_cu);
96408a79 13982 if (target_physname == NULL)
b98664d3 13983 complaint (_("DW_AT_call_target target DIE has invalid "
9d8780f0
SM
13984 "physname, for referencing DIE %s [in module %s]"),
13985 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79 13986 else
7d455152 13987 SET_FIELD_PHYSNAME (call_site->target, target_physname);
96408a79
SA
13988 }
13989 else
13990 {
13991 CORE_ADDR lowpc;
13992
13993 /* DW_AT_entry_pc should be preferred. */
3a2b436a 13994 if (dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL)
e385593e 13995 <= PC_BOUNDS_INVALID)
b98664d3 13996 complaint (_("DW_AT_call_target target DIE has invalid "
9d8780f0
SM
13997 "low pc, for referencing DIE %s [in module %s]"),
13998 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79 13999 else
3e29f34a
MR
14000 {
14001 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
14002 SET_FIELD_PHYSADDR (call_site->target, lowpc);
14003 }
96408a79
SA
14004 }
14005 }
14006 else
b98664d3 14007 complaint (_("DW_TAG_call_site DW_AT_call_target is neither "
9d8780f0
SM
14008 "block nor reference, for DIE %s [in module %s]"),
14009 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79
SA
14010
14011 call_site->per_cu = cu->per_cu;
14012
14013 for (child_die = die->child;
14014 child_die && child_die->tag;
14015 child_die = sibling_die (child_die))
14016 {
96408a79 14017 struct call_site_parameter *parameter;
1788b2d3 14018 struct attribute *loc, *origin;
96408a79 14019
216f72a1
JK
14020 if (child_die->tag != DW_TAG_call_site_parameter
14021 && child_die->tag != DW_TAG_GNU_call_site_parameter)
96408a79
SA
14022 {
14023 /* Already printed the complaint above. */
14024 continue;
14025 }
14026
14027 gdb_assert (call_site->parameter_count < nparams);
14028 parameter = &call_site->parameter[call_site->parameter_count];
14029
1788b2d3
JK
14030 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
14031 specifies DW_TAG_formal_parameter. Value of the data assumed for the
216f72a1 14032 register is contained in DW_AT_call_value. */
96408a79 14033
24c5c679 14034 loc = dwarf2_attr (child_die, DW_AT_location, cu);
216f72a1
JK
14035 origin = dwarf2_attr (child_die, DW_AT_call_parameter, cu);
14036 if (origin == NULL)
14037 {
14038 /* This was a pre-DWARF-5 GNU extension alias
14039 for DW_AT_call_parameter. */
14040 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
14041 }
7771576e 14042 if (loc == NULL && origin != NULL && attr_form_is_ref (origin))
1788b2d3 14043 {
1788b2d3 14044 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
9c541725
PA
14045
14046 sect_offset sect_off
14047 = (sect_offset) dwarf2_get_ref_die_offset (origin);
14048 if (!offset_in_cu_p (&cu->header, sect_off))
d76b7dbc
JK
14049 {
14050 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
14051 binding can be done only inside one CU. Such referenced DIE
14052 therefore cannot be even moved to DW_TAG_partial_unit. */
b98664d3 14053 complaint (_("DW_AT_call_parameter offset is not in CU for "
9d8780f0
SM
14054 "DW_TAG_call_site child DIE %s [in module %s]"),
14055 sect_offset_str (child_die->sect_off),
9c541725 14056 objfile_name (objfile));
d76b7dbc
JK
14057 continue;
14058 }
9c541725
PA
14059 parameter->u.param_cu_off
14060 = (cu_offset) (sect_off - cu->header.sect_off);
1788b2d3
JK
14061 }
14062 else if (loc == NULL || origin != NULL || !attr_form_is_block (loc))
96408a79 14063 {
b98664d3 14064 complaint (_("No DW_FORM_block* DW_AT_location for "
9d8780f0
SM
14065 "DW_TAG_call_site child DIE %s [in module %s]"),
14066 sect_offset_str (child_die->sect_off), objfile_name (objfile));
96408a79
SA
14067 continue;
14068 }
24c5c679 14069 else
96408a79 14070 {
24c5c679
JK
14071 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
14072 (DW_BLOCK (loc)->data, &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size]);
14073 if (parameter->u.dwarf_reg != -1)
14074 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
14075 else if (dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (loc)->data,
14076 &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size],
14077 &parameter->u.fb_offset))
14078 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
14079 else
14080 {
b98664d3 14081 complaint (_("Only single DW_OP_reg or DW_OP_fbreg is supported "
24c5c679 14082 "for DW_FORM_block* DW_AT_location is supported for "
9d8780f0 14083 "DW_TAG_call_site child DIE %s "
24c5c679 14084 "[in module %s]"),
9d8780f0 14085 sect_offset_str (child_die->sect_off),
9c541725 14086 objfile_name (objfile));
24c5c679
JK
14087 continue;
14088 }
96408a79
SA
14089 }
14090
216f72a1
JK
14091 attr = dwarf2_attr (child_die, DW_AT_call_value, cu);
14092 if (attr == NULL)
14093 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
96408a79
SA
14094 if (!attr_form_is_block (attr))
14095 {
b98664d3 14096 complaint (_("No DW_FORM_block* DW_AT_call_value for "
9d8780f0
SM
14097 "DW_TAG_call_site child DIE %s [in module %s]"),
14098 sect_offset_str (child_die->sect_off),
9c541725 14099 objfile_name (objfile));
96408a79
SA
14100 continue;
14101 }
14102 parameter->value = DW_BLOCK (attr)->data;
14103 parameter->value_size = DW_BLOCK (attr)->size;
14104
14105 /* Parameters are not pre-cleared by memset above. */
14106 parameter->data_value = NULL;
14107 parameter->data_value_size = 0;
14108 call_site->parameter_count++;
14109
216f72a1
JK
14110 attr = dwarf2_attr (child_die, DW_AT_call_data_value, cu);
14111 if (attr == NULL)
14112 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
96408a79
SA
14113 if (attr)
14114 {
14115 if (!attr_form_is_block (attr))
b98664d3 14116 complaint (_("No DW_FORM_block* DW_AT_call_data_value for "
9d8780f0
SM
14117 "DW_TAG_call_site child DIE %s [in module %s]"),
14118 sect_offset_str (child_die->sect_off),
9c541725 14119 objfile_name (objfile));
96408a79
SA
14120 else
14121 {
14122 parameter->data_value = DW_BLOCK (attr)->data;
14123 parameter->data_value_size = DW_BLOCK (attr)->size;
14124 }
14125 }
14126 }
14127}
14128
71a3c369
TT
14129/* Helper function for read_variable. If DIE represents a virtual
14130 table, then return the type of the concrete object that is
14131 associated with the virtual table. Otherwise, return NULL. */
14132
14133static struct type *
14134rust_containing_type (struct die_info *die, struct dwarf2_cu *cu)
14135{
14136 struct attribute *attr = dwarf2_attr (die, DW_AT_type, cu);
14137 if (attr == NULL)
14138 return NULL;
14139
14140 /* Find the type DIE. */
14141 struct die_info *type_die = NULL;
14142 struct dwarf2_cu *type_cu = cu;
14143
14144 if (attr_form_is_ref (attr))
14145 type_die = follow_die_ref (die, attr, &type_cu);
14146 if (type_die == NULL)
14147 return NULL;
14148
14149 if (dwarf2_attr (type_die, DW_AT_containing_type, type_cu) == NULL)
14150 return NULL;
14151 return die_containing_type (type_die, type_cu);
14152}
14153
14154/* Read a variable (DW_TAG_variable) DIE and create a new symbol. */
14155
14156static void
14157read_variable (struct die_info *die, struct dwarf2_cu *cu)
14158{
14159 struct rust_vtable_symbol *storage = NULL;
14160
14161 if (cu->language == language_rust)
14162 {
14163 struct type *containing_type = rust_containing_type (die, cu);
14164
14165 if (containing_type != NULL)
14166 {
518817b3 14167 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
71a3c369
TT
14168
14169 storage = OBSTACK_ZALLOC (&objfile->objfile_obstack,
14170 struct rust_vtable_symbol);
14171 initialize_objfile_symbol (storage);
14172 storage->concrete_type = containing_type;
cf724bc9 14173 storage->subclass = SYMBOL_RUST_VTABLE;
71a3c369
TT
14174 }
14175 }
14176
5e2db402 14177 new_symbol (die, NULL, cu, storage);
71a3c369
TT
14178}
14179
43988095
JK
14180/* Call CALLBACK from DW_AT_ranges attribute value OFFSET
14181 reading .debug_rnglists.
14182 Callback's type should be:
14183 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
14184 Return true if the attributes are present and valid, otherwise,
14185 return false. */
14186
14187template <typename Callback>
14188static bool
14189dwarf2_rnglists_process (unsigned offset, struct dwarf2_cu *cu,
14190 Callback &&callback)
14191{
ed2dc618 14192 struct dwarf2_per_objfile *dwarf2_per_objfile
518817b3 14193 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 14194 struct objfile *objfile = dwarf2_per_objfile->objfile;
43988095 14195 bfd *obfd = objfile->obfd;
43988095
JK
14196 /* Base address selection entry. */
14197 CORE_ADDR base;
14198 int found_base;
43988095 14199 const gdb_byte *buffer;
43988095
JK
14200 CORE_ADDR baseaddr;
14201 bool overflow = false;
14202
14203 found_base = cu->base_known;
14204 base = cu->base_address;
14205
14206 dwarf2_read_section (objfile, &dwarf2_per_objfile->rnglists);
14207 if (offset >= dwarf2_per_objfile->rnglists.size)
14208 {
b98664d3 14209 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
43988095
JK
14210 offset);
14211 return false;
14212 }
14213 buffer = dwarf2_per_objfile->rnglists.buffer + offset;
14214
14215 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
14216
14217 while (1)
14218 {
7814882a
JK
14219 /* Initialize it due to a false compiler warning. */
14220 CORE_ADDR range_beginning = 0, range_end = 0;
43988095
JK
14221 const gdb_byte *buf_end = (dwarf2_per_objfile->rnglists.buffer
14222 + dwarf2_per_objfile->rnglists.size);
14223 unsigned int bytes_read;
14224
14225 if (buffer == buf_end)
14226 {
14227 overflow = true;
14228 break;
14229 }
14230 const auto rlet = static_cast<enum dwarf_range_list_entry>(*buffer++);
14231 switch (rlet)
14232 {
14233 case DW_RLE_end_of_list:
14234 break;
14235 case DW_RLE_base_address:
14236 if (buffer + cu->header.addr_size > buf_end)
14237 {
14238 overflow = true;
14239 break;
14240 }
14241 base = read_address (obfd, buffer, cu, &bytes_read);
14242 found_base = 1;
14243 buffer += bytes_read;
14244 break;
14245 case DW_RLE_start_length:
14246 if (buffer + cu->header.addr_size > buf_end)
14247 {
14248 overflow = true;
14249 break;
14250 }
14251 range_beginning = read_address (obfd, buffer, cu, &bytes_read);
14252 buffer += bytes_read;
14253 range_end = (range_beginning
14254 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
14255 buffer += bytes_read;
14256 if (buffer > buf_end)
14257 {
14258 overflow = true;
14259 break;
14260 }
14261 break;
14262 case DW_RLE_offset_pair:
14263 range_beginning = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14264 buffer += bytes_read;
14265 if (buffer > buf_end)
14266 {
14267 overflow = true;
14268 break;
14269 }
14270 range_end = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14271 buffer += bytes_read;
14272 if (buffer > buf_end)
14273 {
14274 overflow = true;
14275 break;
14276 }
14277 break;
14278 case DW_RLE_start_end:
14279 if (buffer + 2 * cu->header.addr_size > buf_end)
14280 {
14281 overflow = true;
14282 break;
14283 }
14284 range_beginning = read_address (obfd, buffer, cu, &bytes_read);
14285 buffer += bytes_read;
14286 range_end = read_address (obfd, buffer, cu, &bytes_read);
14287 buffer += bytes_read;
14288 break;
14289 default:
b98664d3 14290 complaint (_("Invalid .debug_rnglists data (no base address)"));
43988095
JK
14291 return false;
14292 }
14293 if (rlet == DW_RLE_end_of_list || overflow)
14294 break;
14295 if (rlet == DW_RLE_base_address)
14296 continue;
14297
14298 if (!found_base)
14299 {
14300 /* We have no valid base address for the ranges
14301 data. */
b98664d3 14302 complaint (_("Invalid .debug_rnglists data (no base address)"));
43988095
JK
14303 return false;
14304 }
14305
14306 if (range_beginning > range_end)
14307 {
14308 /* Inverted range entries are invalid. */
b98664d3 14309 complaint (_("Invalid .debug_rnglists data (inverted range)"));
43988095
JK
14310 return false;
14311 }
14312
14313 /* Empty range entries have no effect. */
14314 if (range_beginning == range_end)
14315 continue;
14316
14317 range_beginning += base;
14318 range_end += base;
14319
14320 /* A not-uncommon case of bad debug info.
14321 Don't pollute the addrmap with bad data. */
14322 if (range_beginning + baseaddr == 0
14323 && !dwarf2_per_objfile->has_section_at_zero)
14324 {
b98664d3 14325 complaint (_(".debug_rnglists entry has start address of zero"
43988095
JK
14326 " [in module %s]"), objfile_name (objfile));
14327 continue;
14328 }
14329
14330 callback (range_beginning, range_end);
14331 }
14332
14333 if (overflow)
14334 {
b98664d3 14335 complaint (_("Offset %d is not terminated "
43988095
JK
14336 "for DW_AT_ranges attribute"),
14337 offset);
14338 return false;
14339 }
14340
14341 return true;
14342}
14343
14344/* Call CALLBACK from DW_AT_ranges attribute value OFFSET reading .debug_ranges.
14345 Callback's type should be:
14346 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
5f46c5a5 14347 Return 1 if the attributes are present and valid, otherwise, return 0. */
43039443 14348
43988095 14349template <typename Callback>
43039443 14350static int
5f46c5a5 14351dwarf2_ranges_process (unsigned offset, struct dwarf2_cu *cu,
43988095 14352 Callback &&callback)
43039443 14353{
ed2dc618 14354 struct dwarf2_per_objfile *dwarf2_per_objfile
518817b3 14355 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 14356 struct objfile *objfile = dwarf2_per_objfile->objfile;
43039443
JK
14357 struct comp_unit_head *cu_header = &cu->header;
14358 bfd *obfd = objfile->obfd;
14359 unsigned int addr_size = cu_header->addr_size;
14360 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
14361 /* Base address selection entry. */
14362 CORE_ADDR base;
14363 int found_base;
14364 unsigned int dummy;
d521ce57 14365 const gdb_byte *buffer;
ff013f42 14366 CORE_ADDR baseaddr;
43039443 14367
43988095
JK
14368 if (cu_header->version >= 5)
14369 return dwarf2_rnglists_process (offset, cu, callback);
14370
d00adf39
DE
14371 found_base = cu->base_known;
14372 base = cu->base_address;
43039443 14373
be391dca 14374 dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
dce234bc 14375 if (offset >= dwarf2_per_objfile->ranges.size)
43039443 14376 {
b98664d3 14377 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
43039443
JK
14378 offset);
14379 return 0;
14380 }
dce234bc 14381 buffer = dwarf2_per_objfile->ranges.buffer + offset;
43039443 14382
e7030f15 14383 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
ff013f42 14384
43039443
JK
14385 while (1)
14386 {
14387 CORE_ADDR range_beginning, range_end;
14388
14389 range_beginning = read_address (obfd, buffer, cu, &dummy);
14390 buffer += addr_size;
14391 range_end = read_address (obfd, buffer, cu, &dummy);
14392 buffer += addr_size;
14393 offset += 2 * addr_size;
14394
14395 /* An end of list marker is a pair of zero addresses. */
14396 if (range_beginning == 0 && range_end == 0)
14397 /* Found the end of list entry. */
14398 break;
14399
14400 /* Each base address selection entry is a pair of 2 values.
14401 The first is the largest possible address, the second is
14402 the base address. Check for a base address here. */
14403 if ((range_beginning & mask) == mask)
14404 {
28d2bfb9
AB
14405 /* If we found the largest possible address, then we already
14406 have the base address in range_end. */
14407 base = range_end;
43039443
JK
14408 found_base = 1;
14409 continue;
14410 }
14411
14412 if (!found_base)
14413 {
14414 /* We have no valid base address for the ranges
14415 data. */
b98664d3 14416 complaint (_("Invalid .debug_ranges data (no base address)"));
43039443
JK
14417 return 0;
14418 }
14419
9277c30c
UW
14420 if (range_beginning > range_end)
14421 {
14422 /* Inverted range entries are invalid. */
b98664d3 14423 complaint (_("Invalid .debug_ranges data (inverted range)"));
9277c30c
UW
14424 return 0;
14425 }
14426
14427 /* Empty range entries have no effect. */
14428 if (range_beginning == range_end)
14429 continue;
14430
43039443
JK
14431 range_beginning += base;
14432 range_end += base;
14433
01093045
DE
14434 /* A not-uncommon case of bad debug info.
14435 Don't pollute the addrmap with bad data. */
14436 if (range_beginning + baseaddr == 0
14437 && !dwarf2_per_objfile->has_section_at_zero)
14438 {
b98664d3 14439 complaint (_(".debug_ranges entry has start address of zero"
4262abfb 14440 " [in module %s]"), objfile_name (objfile));
01093045
DE
14441 continue;
14442 }
14443
5f46c5a5
JK
14444 callback (range_beginning, range_end);
14445 }
14446
14447 return 1;
14448}
14449
14450/* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
14451 Return 1 if the attributes are present and valid, otherwise, return 0.
14452 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
14453
14454static int
14455dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
14456 CORE_ADDR *high_return, struct dwarf2_cu *cu,
14457 struct partial_symtab *ranges_pst)
14458{
518817b3 14459 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
5f46c5a5
JK
14460 struct gdbarch *gdbarch = get_objfile_arch (objfile);
14461 const CORE_ADDR baseaddr = ANOFFSET (objfile->section_offsets,
14462 SECT_OFF_TEXT (objfile));
14463 int low_set = 0;
14464 CORE_ADDR low = 0;
14465 CORE_ADDR high = 0;
14466 int retval;
14467
14468 retval = dwarf2_ranges_process (offset, cu,
14469 [&] (CORE_ADDR range_beginning, CORE_ADDR range_end)
14470 {
9277c30c 14471 if (ranges_pst != NULL)
3e29f34a
MR
14472 {
14473 CORE_ADDR lowpc;
14474 CORE_ADDR highpc;
14475
14476 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch,
14477 range_beginning + baseaddr);
14478 highpc = gdbarch_adjust_dwarf2_addr (gdbarch,
14479 range_end + baseaddr);
14480 addrmap_set_empty (objfile->psymtabs_addrmap, lowpc, highpc - 1,
14481 ranges_pst);
14482 }
ff013f42 14483
43039443
JK
14484 /* FIXME: This is recording everything as a low-high
14485 segment of consecutive addresses. We should have a
14486 data structure for discontiguous block ranges
14487 instead. */
14488 if (! low_set)
14489 {
14490 low = range_beginning;
14491 high = range_end;
14492 low_set = 1;
14493 }
14494 else
14495 {
14496 if (range_beginning < low)
14497 low = range_beginning;
14498 if (range_end > high)
14499 high = range_end;
14500 }
5f46c5a5
JK
14501 });
14502 if (!retval)
14503 return 0;
43039443
JK
14504
14505 if (! low_set)
14506 /* If the first entry is an end-of-list marker, the range
14507 describes an empty scope, i.e. no instructions. */
14508 return 0;
14509
14510 if (low_return)
14511 *low_return = low;
14512 if (high_return)
14513 *high_return = high;
14514 return 1;
14515}
14516
3a2b436a
JK
14517/* Get low and high pc attributes from a die. See enum pc_bounds_kind
14518 definition for the return value. *LOWPC and *HIGHPC are set iff
e385593e 14519 neither PC_BOUNDS_NOT_PRESENT nor PC_BOUNDS_INVALID are returned. */
380bca97 14520
3a2b436a 14521static enum pc_bounds_kind
af34e669 14522dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
d85a05f0
DJ
14523 CORE_ADDR *highpc, struct dwarf2_cu *cu,
14524 struct partial_symtab *pst)
c906108c 14525{
518817b3
SM
14526 struct dwarf2_per_objfile *dwarf2_per_objfile
14527 = cu->per_cu->dwarf2_per_objfile;
c906108c 14528 struct attribute *attr;
91da1414 14529 struct attribute *attr_high;
af34e669
DJ
14530 CORE_ADDR low = 0;
14531 CORE_ADDR high = 0;
e385593e 14532 enum pc_bounds_kind ret;
c906108c 14533
91da1414
MW
14534 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14535 if (attr_high)
af34e669 14536 {
e142c38c 14537 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
af34e669 14538 if (attr)
91da1414 14539 {
31aa7e4e
JB
14540 low = attr_value_as_address (attr);
14541 high = attr_value_as_address (attr_high);
14542 if (cu->header.version >= 4 && attr_form_is_constant (attr_high))
14543 high += low;
91da1414 14544 }
af34e669
DJ
14545 else
14546 /* Found high w/o low attribute. */
e385593e 14547 return PC_BOUNDS_INVALID;
af34e669
DJ
14548
14549 /* Found consecutive range of addresses. */
3a2b436a 14550 ret = PC_BOUNDS_HIGH_LOW;
af34e669 14551 }
c906108c 14552 else
af34e669 14553 {
e142c38c 14554 attr = dwarf2_attr (die, DW_AT_ranges, cu);
af34e669
DJ
14555 if (attr != NULL)
14556 {
ab435259
DE
14557 /* DW_AT_ranges_base does not apply to DIEs from the DWO skeleton.
14558 We take advantage of the fact that DW_AT_ranges does not appear
14559 in DW_TAG_compile_unit of DWO files. */
14560 int need_ranges_base = die->tag != DW_TAG_compile_unit;
14561 unsigned int ranges_offset = (DW_UNSND (attr)
14562 + (need_ranges_base
14563 ? cu->ranges_base
14564 : 0));
2e3cf129 14565
af34e669 14566 /* Value of the DW_AT_ranges attribute is the offset in the
a604369a 14567 .debug_ranges section. */
2e3cf129 14568 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst))
e385593e 14569 return PC_BOUNDS_INVALID;
43039443 14570 /* Found discontinuous range of addresses. */
3a2b436a 14571 ret = PC_BOUNDS_RANGES;
af34e669 14572 }
e385593e
JK
14573 else
14574 return PC_BOUNDS_NOT_PRESENT;
af34e669 14575 }
c906108c 14576
48fbe735 14577 /* partial_die_info::read has also the strict LOW < HIGH requirement. */
9373cf26 14578 if (high <= low)
e385593e 14579 return PC_BOUNDS_INVALID;
c906108c
SS
14580
14581 /* When using the GNU linker, .gnu.linkonce. sections are used to
14582 eliminate duplicate copies of functions and vtables and such.
14583 The linker will arbitrarily choose one and discard the others.
14584 The AT_*_pc values for such functions refer to local labels in
14585 these sections. If the section from that file was discarded, the
14586 labels are not in the output, so the relocs get a value of 0.
14587 If this is a discarded function, mark the pc bounds as invalid,
14588 so that GDB will ignore it. */
72dca2f5 14589 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
e385593e 14590 return PC_BOUNDS_INVALID;
c906108c
SS
14591
14592 *lowpc = low;
96408a79
SA
14593 if (highpc)
14594 *highpc = high;
af34e669 14595 return ret;
c906108c
SS
14596}
14597
b084d499
JB
14598/* Assuming that DIE represents a subprogram DIE or a lexical block, get
14599 its low and high PC addresses. Do nothing if these addresses could not
14600 be determined. Otherwise, set LOWPC to the low address if it is smaller,
14601 and HIGHPC to the high address if greater than HIGHPC. */
14602
14603static void
14604dwarf2_get_subprogram_pc_bounds (struct die_info *die,
14605 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14606 struct dwarf2_cu *cu)
14607{
14608 CORE_ADDR low, high;
14609 struct die_info *child = die->child;
14610
e385593e 14611 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL) >= PC_BOUNDS_RANGES)
b084d499 14612 {
325fac50
PA
14613 *lowpc = std::min (*lowpc, low);
14614 *highpc = std::max (*highpc, high);
b084d499
JB
14615 }
14616
14617 /* If the language does not allow nested subprograms (either inside
14618 subprograms or lexical blocks), we're done. */
14619 if (cu->language != language_ada)
14620 return;
6e70227d 14621
b084d499
JB
14622 /* Check all the children of the given DIE. If it contains nested
14623 subprograms, then check their pc bounds. Likewise, we need to
14624 check lexical blocks as well, as they may also contain subprogram
14625 definitions. */
14626 while (child && child->tag)
14627 {
14628 if (child->tag == DW_TAG_subprogram
14629 || child->tag == DW_TAG_lexical_block)
14630 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
14631 child = sibling_die (child);
14632 }
14633}
14634
fae299cd
DC
14635/* Get the low and high pc's represented by the scope DIE, and store
14636 them in *LOWPC and *HIGHPC. If the correct values can't be
14637 determined, set *LOWPC to -1 and *HIGHPC to 0. */
14638
14639static void
14640get_scope_pc_bounds (struct die_info *die,
14641 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14642 struct dwarf2_cu *cu)
14643{
14644 CORE_ADDR best_low = (CORE_ADDR) -1;
14645 CORE_ADDR best_high = (CORE_ADDR) 0;
14646 CORE_ADDR current_low, current_high;
14647
3a2b436a 14648 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL)
e385593e 14649 >= PC_BOUNDS_RANGES)
fae299cd
DC
14650 {
14651 best_low = current_low;
14652 best_high = current_high;
14653 }
14654 else
14655 {
14656 struct die_info *child = die->child;
14657
14658 while (child && child->tag)
14659 {
14660 switch (child->tag) {
14661 case DW_TAG_subprogram:
b084d499 14662 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
fae299cd
DC
14663 break;
14664 case DW_TAG_namespace:
f55ee35c 14665 case DW_TAG_module:
fae299cd
DC
14666 /* FIXME: carlton/2004-01-16: Should we do this for
14667 DW_TAG_class_type/DW_TAG_structure_type, too? I think
14668 that current GCC's always emit the DIEs corresponding
14669 to definitions of methods of classes as children of a
14670 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
14671 the DIEs giving the declarations, which could be
14672 anywhere). But I don't see any reason why the
14673 standards says that they have to be there. */
14674 get_scope_pc_bounds (child, &current_low, &current_high, cu);
14675
14676 if (current_low != ((CORE_ADDR) -1))
14677 {
325fac50
PA
14678 best_low = std::min (best_low, current_low);
14679 best_high = std::max (best_high, current_high);
fae299cd
DC
14680 }
14681 break;
14682 default:
0963b4bd 14683 /* Ignore. */
fae299cd
DC
14684 break;
14685 }
14686
14687 child = sibling_die (child);
14688 }
14689 }
14690
14691 *lowpc = best_low;
14692 *highpc = best_high;
14693}
14694
801e3a5b
JB
14695/* Record the address ranges for BLOCK, offset by BASEADDR, as given
14696 in DIE. */
380bca97 14697
801e3a5b
JB
14698static void
14699dwarf2_record_block_ranges (struct die_info *die, struct block *block,
14700 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
14701{
518817b3 14702 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
3e29f34a 14703 struct gdbarch *gdbarch = get_objfile_arch (objfile);
801e3a5b 14704 struct attribute *attr;
91da1414 14705 struct attribute *attr_high;
801e3a5b 14706
91da1414
MW
14707 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14708 if (attr_high)
801e3a5b 14709 {
801e3a5b
JB
14710 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
14711 if (attr)
14712 {
31aa7e4e
JB
14713 CORE_ADDR low = attr_value_as_address (attr);
14714 CORE_ADDR high = attr_value_as_address (attr_high);
14715
14716 if (cu->header.version >= 4 && attr_form_is_constant (attr_high))
14717 high += low;
9a619af0 14718
3e29f34a
MR
14719 low = gdbarch_adjust_dwarf2_addr (gdbarch, low + baseaddr);
14720 high = gdbarch_adjust_dwarf2_addr (gdbarch, high + baseaddr);
804d2729 14721 cu->builder->record_block_range (block, low, high - 1);
801e3a5b
JB
14722 }
14723 }
14724
14725 attr = dwarf2_attr (die, DW_AT_ranges, cu);
14726 if (attr)
14727 {
ab435259
DE
14728 /* DW_AT_ranges_base does not apply to DIEs from the DWO skeleton.
14729 We take advantage of the fact that DW_AT_ranges does not appear
14730 in DW_TAG_compile_unit of DWO files. */
14731 int need_ranges_base = die->tag != DW_TAG_compile_unit;
801e3a5b
JB
14732
14733 /* The value of the DW_AT_ranges attribute is the offset of the
14734 address range list in the .debug_ranges section. */
ab435259
DE
14735 unsigned long offset = (DW_UNSND (attr)
14736 + (need_ranges_base ? cu->ranges_base : 0));
801e3a5b 14737
5f46c5a5
JK
14738 dwarf2_ranges_process (offset, cu,
14739 [&] (CORE_ADDR start, CORE_ADDR end)
14740 {
58fdfd2c
JK
14741 start += baseaddr;
14742 end += baseaddr;
5f46c5a5
JK
14743 start = gdbarch_adjust_dwarf2_addr (gdbarch, start);
14744 end = gdbarch_adjust_dwarf2_addr (gdbarch, end);
804d2729 14745 cu->builder->record_block_range (block, start, end - 1);
5f46c5a5 14746 });
801e3a5b
JB
14747 }
14748}
14749
685b1105
JK
14750/* Check whether the producer field indicates either of GCC < 4.6, or the
14751 Intel C/C++ compiler, and cache the result in CU. */
60d5a603 14752
685b1105
JK
14753static void
14754check_producer (struct dwarf2_cu *cu)
60d5a603 14755{
38360086 14756 int major, minor;
60d5a603
JK
14757
14758 if (cu->producer == NULL)
14759 {
14760 /* For unknown compilers expect their behavior is DWARF version
14761 compliant.
14762
14763 GCC started to support .debug_types sections by -gdwarf-4 since
14764 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
14765 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
14766 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
14767 interpreted incorrectly by GDB now - GCC PR debug/48229. */
60d5a603 14768 }
b1ffba5a 14769 else if (producer_is_gcc (cu->producer, &major, &minor))
60d5a603 14770 {
38360086
MW
14771 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
14772 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
685b1105 14773 }
5230b05a
WT
14774 else if (producer_is_icc (cu->producer, &major, &minor))
14775 cu->producer_is_icc_lt_14 = major < 14;
685b1105
JK
14776 else
14777 {
14778 /* For other non-GCC compilers, expect their behavior is DWARF version
14779 compliant. */
60d5a603
JK
14780 }
14781
ba919b58 14782 cu->checked_producer = 1;
685b1105 14783}
ba919b58 14784
685b1105
JK
14785/* Check for GCC PR debug/45124 fix which is not present in any G++ version up
14786 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
14787 during 4.6.0 experimental. */
14788
14789static int
14790producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
14791{
14792 if (!cu->checked_producer)
14793 check_producer (cu);
14794
14795 return cu->producer_is_gxx_lt_4_6;
60d5a603
JK
14796}
14797
14798/* Return the default accessibility type if it is not overriden by
14799 DW_AT_accessibility. */
14800
14801static enum dwarf_access_attribute
14802dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
14803{
14804 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
14805 {
14806 /* The default DWARF 2 accessibility for members is public, the default
14807 accessibility for inheritance is private. */
14808
14809 if (die->tag != DW_TAG_inheritance)
14810 return DW_ACCESS_public;
14811 else
14812 return DW_ACCESS_private;
14813 }
14814 else
14815 {
14816 /* DWARF 3+ defines the default accessibility a different way. The same
14817 rules apply now for DW_TAG_inheritance as for the members and it only
14818 depends on the container kind. */
14819
14820 if (die->parent->tag == DW_TAG_class_type)
14821 return DW_ACCESS_private;
14822 else
14823 return DW_ACCESS_public;
14824 }
14825}
14826
74ac6d43
TT
14827/* Look for DW_AT_data_member_location. Set *OFFSET to the byte
14828 offset. If the attribute was not found return 0, otherwise return
14829 1. If it was found but could not properly be handled, set *OFFSET
14830 to 0. */
14831
14832static int
14833handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
14834 LONGEST *offset)
14835{
14836 struct attribute *attr;
14837
14838 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
14839 if (attr != NULL)
14840 {
14841 *offset = 0;
14842
14843 /* Note that we do not check for a section offset first here.
14844 This is because DW_AT_data_member_location is new in DWARF 4,
14845 so if we see it, we can assume that a constant form is really
14846 a constant and not a section offset. */
14847 if (attr_form_is_constant (attr))
14848 *offset = dwarf2_get_attr_constant_value (attr, 0);
14849 else if (attr_form_is_section_offset (attr))
14850 dwarf2_complex_location_expr_complaint ();
14851 else if (attr_form_is_block (attr))
14852 *offset = decode_locdesc (DW_BLOCK (attr), cu);
14853 else
14854 dwarf2_complex_location_expr_complaint ();
14855
14856 return 1;
14857 }
14858
14859 return 0;
14860}
14861
c906108c
SS
14862/* Add an aggregate field to the field list. */
14863
14864static void
107d2387 14865dwarf2_add_field (struct field_info *fip, struct die_info *die,
e7c27a73 14866 struct dwarf2_cu *cu)
6e70227d 14867{
518817b3 14868 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
5e2b427d 14869 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
14870 struct nextfield *new_field;
14871 struct attribute *attr;
14872 struct field *fp;
15d034d0 14873 const char *fieldname = "";
c906108c 14874
7d0ccb61
DJ
14875 if (die->tag == DW_TAG_inheritance)
14876 {
be2daae6
TT
14877 fip->baseclasses.emplace_back ();
14878 new_field = &fip->baseclasses.back ();
7d0ccb61
DJ
14879 }
14880 else
14881 {
be2daae6
TT
14882 fip->fields.emplace_back ();
14883 new_field = &fip->fields.back ();
7d0ccb61 14884 }
be2daae6 14885
c906108c
SS
14886 fip->nfields++;
14887
e142c38c 14888 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
c906108c
SS
14889 if (attr)
14890 new_field->accessibility = DW_UNSND (attr);
60d5a603
JK
14891 else
14892 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
c906108c
SS
14893 if (new_field->accessibility != DW_ACCESS_public)
14894 fip->non_public_fields = 1;
60d5a603 14895
e142c38c 14896 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
c906108c
SS
14897 if (attr)
14898 new_field->virtuality = DW_UNSND (attr);
60d5a603
JK
14899 else
14900 new_field->virtuality = DW_VIRTUALITY_none;
c906108c
SS
14901
14902 fp = &new_field->field;
a9a9bd0f 14903
e142c38c 14904 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
c906108c 14905 {
74ac6d43
TT
14906 LONGEST offset;
14907
a9a9bd0f 14908 /* Data member other than a C++ static data member. */
6e70227d 14909
c906108c 14910 /* Get type of field. */
e7c27a73 14911 fp->type = die_type (die, cu);
c906108c 14912
d6a843b5 14913 SET_FIELD_BITPOS (*fp, 0);
01ad7f36 14914
c906108c 14915 /* Get bit size of field (zero if none). */
e142c38c 14916 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
c906108c
SS
14917 if (attr)
14918 {
14919 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
14920 }
14921 else
14922 {
14923 FIELD_BITSIZE (*fp) = 0;
14924 }
14925
14926 /* Get bit offset of field. */
74ac6d43
TT
14927 if (handle_data_member_location (die, cu, &offset))
14928 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
e142c38c 14929 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
c906108c
SS
14930 if (attr)
14931 {
5e2b427d 14932 if (gdbarch_bits_big_endian (gdbarch))
c906108c
SS
14933 {
14934 /* For big endian bits, the DW_AT_bit_offset gives the
c5aa993b
JM
14935 additional bit offset from the MSB of the containing
14936 anonymous object to the MSB of the field. We don't
14937 have to do anything special since we don't need to
14938 know the size of the anonymous object. */
f41f5e61 14939 SET_FIELD_BITPOS (*fp, FIELD_BITPOS (*fp) + DW_UNSND (attr));
c906108c
SS
14940 }
14941 else
14942 {
14943 /* For little endian bits, compute the bit offset to the
c5aa993b
JM
14944 MSB of the anonymous object, subtract off the number of
14945 bits from the MSB of the field to the MSB of the
14946 object, and then subtract off the number of bits of
14947 the field itself. The result is the bit offset of
14948 the LSB of the field. */
c906108c
SS
14949 int anonymous_size;
14950 int bit_offset = DW_UNSND (attr);
14951
e142c38c 14952 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
14953 if (attr)
14954 {
14955 /* The size of the anonymous object containing
14956 the bit field is explicit, so use the
14957 indicated size (in bytes). */
14958 anonymous_size = DW_UNSND (attr);
14959 }
14960 else
14961 {
14962 /* The size of the anonymous object containing
14963 the bit field must be inferred from the type
14964 attribute of the data member containing the
14965 bit field. */
14966 anonymous_size = TYPE_LENGTH (fp->type);
14967 }
f41f5e61
PA
14968 SET_FIELD_BITPOS (*fp,
14969 (FIELD_BITPOS (*fp)
14970 + anonymous_size * bits_per_byte
14971 - bit_offset - FIELD_BITSIZE (*fp)));
c906108c
SS
14972 }
14973 }
da5b30da
AA
14974 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
14975 if (attr != NULL)
14976 SET_FIELD_BITPOS (*fp, (FIELD_BITPOS (*fp)
14977 + dwarf2_get_attr_constant_value (attr, 0)));
c906108c
SS
14978
14979 /* Get name of field. */
39cbfefa
DJ
14980 fieldname = dwarf2_name (die, cu);
14981 if (fieldname == NULL)
14982 fieldname = "";
d8151005
DJ
14983
14984 /* The name is already allocated along with this objfile, so we don't
14985 need to duplicate it for the type. */
14986 fp->name = fieldname;
c906108c
SS
14987
14988 /* Change accessibility for artificial fields (e.g. virtual table
c5aa993b 14989 pointer or virtual base class pointer) to private. */
e142c38c 14990 if (dwarf2_attr (die, DW_AT_artificial, cu))
c906108c 14991 {
d48cc9dd 14992 FIELD_ARTIFICIAL (*fp) = 1;
c906108c
SS
14993 new_field->accessibility = DW_ACCESS_private;
14994 fip->non_public_fields = 1;
14995 }
14996 }
a9a9bd0f 14997 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
c906108c 14998 {
a9a9bd0f
DC
14999 /* C++ static member. */
15000
15001 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
15002 is a declaration, but all versions of G++ as of this writing
15003 (so through at least 3.2.1) incorrectly generate
15004 DW_TAG_variable tags. */
6e70227d 15005
ff355380 15006 const char *physname;
c906108c 15007
a9a9bd0f 15008 /* Get name of field. */
39cbfefa
DJ
15009 fieldname = dwarf2_name (die, cu);
15010 if (fieldname == NULL)
c906108c
SS
15011 return;
15012
254e6b9e 15013 attr = dwarf2_attr (die, DW_AT_const_value, cu);
3863f96c
DE
15014 if (attr
15015 /* Only create a symbol if this is an external value.
15016 new_symbol checks this and puts the value in the global symbol
15017 table, which we want. If it is not external, new_symbol
15018 will try to put the value in cu->list_in_scope which is wrong. */
15019 && dwarf2_flag_true_p (die, DW_AT_external, cu))
254e6b9e
DE
15020 {
15021 /* A static const member, not much different than an enum as far as
15022 we're concerned, except that we can support more types. */
15023 new_symbol (die, NULL, cu);
15024 }
15025
2df3850c 15026 /* Get physical name. */
ff355380 15027 physname = dwarf2_physname (fieldname, die, cu);
c906108c 15028
d8151005
DJ
15029 /* The name is already allocated along with this objfile, so we don't
15030 need to duplicate it for the type. */
15031 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
e7c27a73 15032 FIELD_TYPE (*fp) = die_type (die, cu);
d8151005 15033 FIELD_NAME (*fp) = fieldname;
c906108c
SS
15034 }
15035 else if (die->tag == DW_TAG_inheritance)
15036 {
74ac6d43 15037 LONGEST offset;
d4b96c9a 15038
74ac6d43
TT
15039 /* C++ base class field. */
15040 if (handle_data_member_location (die, cu, &offset))
15041 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
c906108c 15042 FIELD_BITSIZE (*fp) = 0;
e7c27a73 15043 FIELD_TYPE (*fp) = die_type (die, cu);
a737d952 15044 FIELD_NAME (*fp) = TYPE_NAME (fp->type);
c906108c 15045 }
2ddeaf8a
TT
15046 else if (die->tag == DW_TAG_variant_part)
15047 {
15048 /* process_structure_scope will treat this DIE as a union. */
15049 process_structure_scope (die, cu);
15050
15051 /* The variant part is relative to the start of the enclosing
15052 structure. */
15053 SET_FIELD_BITPOS (*fp, 0);
15054 fp->type = get_die_type (die, cu);
15055 fp->artificial = 1;
15056 fp->name = "<<variant>>";
15057 }
15058 else
15059 gdb_assert_not_reached ("missing case in dwarf2_add_field");
c906108c
SS
15060}
15061
883fd55a
KS
15062/* Can the type given by DIE define another type? */
15063
15064static bool
15065type_can_define_types (const struct die_info *die)
15066{
15067 switch (die->tag)
15068 {
15069 case DW_TAG_typedef:
15070 case DW_TAG_class_type:
15071 case DW_TAG_structure_type:
15072 case DW_TAG_union_type:
15073 case DW_TAG_enumeration_type:
15074 return true;
15075
15076 default:
15077 return false;
15078 }
15079}
15080
15081/* Add a type definition defined in the scope of the FIP's class. */
98751a41
JK
15082
15083static void
883fd55a
KS
15084dwarf2_add_type_defn (struct field_info *fip, struct die_info *die,
15085 struct dwarf2_cu *cu)
6e70227d 15086{
be2daae6
TT
15087 struct decl_field fp;
15088 memset (&fp, 0, sizeof (fp));
98751a41 15089
883fd55a 15090 gdb_assert (type_can_define_types (die));
98751a41 15091
883fd55a 15092 /* Get name of field. NULL is okay here, meaning an anonymous type. */
be2daae6
TT
15093 fp.name = dwarf2_name (die, cu);
15094 fp.type = read_type_die (die, cu);
98751a41 15095
c191a687
KS
15096 /* Save accessibility. */
15097 enum dwarf_access_attribute accessibility;
15098 struct attribute *attr = dwarf2_attr (die, DW_AT_accessibility, cu);
15099 if (attr != NULL)
15100 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
15101 else
15102 accessibility = dwarf2_default_access_attribute (die, cu);
15103 switch (accessibility)
15104 {
15105 case DW_ACCESS_public:
15106 /* The assumed value if neither private nor protected. */
15107 break;
15108 case DW_ACCESS_private:
be2daae6 15109 fp.is_private = 1;
c191a687
KS
15110 break;
15111 case DW_ACCESS_protected:
be2daae6 15112 fp.is_protected = 1;
c191a687
KS
15113 break;
15114 default:
b98664d3 15115 complaint (_("Unhandled DW_AT_accessibility value (%x)"), accessibility);
c191a687
KS
15116 }
15117
883fd55a 15118 if (die->tag == DW_TAG_typedef)
be2daae6 15119 fip->typedef_field_list.push_back (fp);
883fd55a 15120 else
be2daae6 15121 fip->nested_types_list.push_back (fp);
98751a41
JK
15122}
15123
c906108c
SS
15124/* Create the vector of fields, and attach it to the type. */
15125
15126static void
fba45db2 15127dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 15128 struct dwarf2_cu *cu)
c906108c
SS
15129{
15130 int nfields = fip->nfields;
15131
15132 /* Record the field count, allocate space for the array of fields,
15133 and create blank accessibility bitfields if necessary. */
15134 TYPE_NFIELDS (type) = nfields;
15135 TYPE_FIELDS (type) = (struct field *)
be2daae6 15136 TYPE_ZALLOC (type, sizeof (struct field) * nfields);
c906108c 15137
b4ba55a1 15138 if (fip->non_public_fields && cu->language != language_ada)
c906108c
SS
15139 {
15140 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15141
15142 TYPE_FIELD_PRIVATE_BITS (type) =
15143 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15144 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
15145
15146 TYPE_FIELD_PROTECTED_BITS (type) =
15147 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15148 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
15149
774b6a14
TT
15150 TYPE_FIELD_IGNORE_BITS (type) =
15151 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15152 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
c906108c
SS
15153 }
15154
15155 /* If the type has baseclasses, allocate and clear a bit vector for
15156 TYPE_FIELD_VIRTUAL_BITS. */
be2daae6 15157 if (!fip->baseclasses.empty () && cu->language != language_ada)
c906108c 15158 {
be2daae6 15159 int num_bytes = B_BYTES (fip->baseclasses.size ());
fe1b8b76 15160 unsigned char *pointer;
c906108c
SS
15161
15162 ALLOCATE_CPLUS_STRUCT_TYPE (type);
224c3ddb 15163 pointer = (unsigned char *) TYPE_ALLOC (type, num_bytes);
fe1b8b76 15164 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
be2daae6
TT
15165 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->baseclasses.size ());
15166 TYPE_N_BASECLASSES (type) = fip->baseclasses.size ();
c906108c
SS
15167 }
15168
2ddeaf8a
TT
15169 if (TYPE_FLAG_DISCRIMINATED_UNION (type))
15170 {
15171 struct discriminant_info *di = alloc_discriminant_info (type, -1, -1);
15172
be2daae6 15173 for (int index = 0; index < nfields; ++index)
2ddeaf8a 15174 {
be2daae6
TT
15175 struct nextfield &field = fip->fields[index];
15176
15177 if (field.variant.is_discriminant)
2ddeaf8a 15178 di->discriminant_index = index;
be2daae6 15179 else if (field.variant.default_branch)
2ddeaf8a
TT
15180 di->default_index = index;
15181 else
be2daae6 15182 di->discriminants[index] = field.variant.discriminant_value;
2ddeaf8a
TT
15183 }
15184 }
15185
be2daae6
TT
15186 /* Copy the saved-up fields into the field vector. */
15187 for (int i = 0; i < nfields; ++i)
c906108c 15188 {
be2daae6
TT
15189 struct nextfield &field
15190 = ((i < fip->baseclasses.size ()) ? fip->baseclasses[i]
15191 : fip->fields[i - fip->baseclasses.size ()]);
7d0ccb61 15192
be2daae6
TT
15193 TYPE_FIELD (type, i) = field.field;
15194 switch (field.accessibility)
c906108c 15195 {
c5aa993b 15196 case DW_ACCESS_private:
b4ba55a1 15197 if (cu->language != language_ada)
be2daae6 15198 SET_TYPE_FIELD_PRIVATE (type, i);
c5aa993b 15199 break;
c906108c 15200
c5aa993b 15201 case DW_ACCESS_protected:
b4ba55a1 15202 if (cu->language != language_ada)
be2daae6 15203 SET_TYPE_FIELD_PROTECTED (type, i);
c5aa993b 15204 break;
c906108c 15205
c5aa993b
JM
15206 case DW_ACCESS_public:
15207 break;
c906108c 15208
c5aa993b
JM
15209 default:
15210 /* Unknown accessibility. Complain and treat it as public. */
15211 {
b98664d3 15212 complaint (_("unsupported accessibility %d"),
be2daae6 15213 field.accessibility);
c5aa993b
JM
15214 }
15215 break;
c906108c 15216 }
be2daae6 15217 if (i < fip->baseclasses.size ())
c906108c 15218 {
be2daae6 15219 switch (field.virtuality)
c906108c 15220 {
c5aa993b
JM
15221 case DW_VIRTUALITY_virtual:
15222 case DW_VIRTUALITY_pure_virtual:
b4ba55a1 15223 if (cu->language == language_ada)
a73c6dcd 15224 error (_("unexpected virtuality in component of Ada type"));
be2daae6 15225 SET_TYPE_FIELD_VIRTUAL (type, i);
c5aa993b 15226 break;
c906108c
SS
15227 }
15228 }
c906108c
SS
15229 }
15230}
15231
7d27a96d
TT
15232/* Return true if this member function is a constructor, false
15233 otherwise. */
15234
15235static int
15236dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
15237{
15238 const char *fieldname;
fe978cb0 15239 const char *type_name;
7d27a96d
TT
15240 int len;
15241
15242 if (die->parent == NULL)
15243 return 0;
15244
15245 if (die->parent->tag != DW_TAG_structure_type
15246 && die->parent->tag != DW_TAG_union_type
15247 && die->parent->tag != DW_TAG_class_type)
15248 return 0;
15249
15250 fieldname = dwarf2_name (die, cu);
fe978cb0
PA
15251 type_name = dwarf2_name (die->parent, cu);
15252 if (fieldname == NULL || type_name == NULL)
7d27a96d
TT
15253 return 0;
15254
15255 len = strlen (fieldname);
fe978cb0
PA
15256 return (strncmp (fieldname, type_name, len) == 0
15257 && (type_name[len] == '\0' || type_name[len] == '<'));
7d27a96d
TT
15258}
15259
c906108c
SS
15260/* Add a member function to the proper fieldlist. */
15261
15262static void
107d2387 15263dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
e7c27a73 15264 struct type *type, struct dwarf2_cu *cu)
c906108c 15265{
518817b3 15266 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c 15267 struct attribute *attr;
c906108c 15268 int i;
be2daae6 15269 struct fnfieldlist *flp = nullptr;
c906108c 15270 struct fn_field *fnp;
15d034d0 15271 const char *fieldname;
f792889a 15272 struct type *this_type;
60d5a603 15273 enum dwarf_access_attribute accessibility;
c906108c 15274
b4ba55a1 15275 if (cu->language == language_ada)
a73c6dcd 15276 error (_("unexpected member function in Ada type"));
b4ba55a1 15277
2df3850c 15278 /* Get name of member function. */
39cbfefa
DJ
15279 fieldname = dwarf2_name (die, cu);
15280 if (fieldname == NULL)
2df3850c 15281 return;
c906108c 15282
c906108c 15283 /* Look up member function name in fieldlist. */
be2daae6 15284 for (i = 0; i < fip->fnfieldlists.size (); i++)
c906108c 15285 {
27bfe10e 15286 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
be2daae6
TT
15287 {
15288 flp = &fip->fnfieldlists[i];
15289 break;
15290 }
c906108c
SS
15291 }
15292
be2daae6
TT
15293 /* Create a new fnfieldlist if necessary. */
15294 if (flp == nullptr)
c906108c 15295 {
be2daae6
TT
15296 fip->fnfieldlists.emplace_back ();
15297 flp = &fip->fnfieldlists.back ();
c906108c 15298 flp->name = fieldname;
be2daae6 15299 i = fip->fnfieldlists.size () - 1;
c906108c
SS
15300 }
15301
be2daae6
TT
15302 /* Create a new member function field and add it to the vector of
15303 fnfieldlists. */
15304 flp->fnfields.emplace_back ();
15305 fnp = &flp->fnfields.back ();
3da10d80
KS
15306
15307 /* Delay processing of the physname until later. */
9c37b5ae 15308 if (cu->language == language_cplus)
be2daae6
TT
15309 add_to_method_list (type, i, flp->fnfields.size () - 1, fieldname,
15310 die, cu);
3da10d80
KS
15311 else
15312 {
1d06ead6 15313 const char *physname = dwarf2_physname (fieldname, die, cu);
3da10d80
KS
15314 fnp->physname = physname ? physname : "";
15315 }
15316
c906108c 15317 fnp->type = alloc_type (objfile);
f792889a
DJ
15318 this_type = read_type_die (die, cu);
15319 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
c906108c 15320 {
f792889a 15321 int nparams = TYPE_NFIELDS (this_type);
c906108c 15322
f792889a 15323 /* TYPE is the domain of this method, and THIS_TYPE is the type
e26fb1d7
DC
15324 of the method itself (TYPE_CODE_METHOD). */
15325 smash_to_method_type (fnp->type, type,
f792889a
DJ
15326 TYPE_TARGET_TYPE (this_type),
15327 TYPE_FIELDS (this_type),
15328 TYPE_NFIELDS (this_type),
15329 TYPE_VARARGS (this_type));
c906108c
SS
15330
15331 /* Handle static member functions.
c5aa993b 15332 Dwarf2 has no clean way to discern C++ static and non-static
0963b4bd
MS
15333 member functions. G++ helps GDB by marking the first
15334 parameter for non-static member functions (which is the this
15335 pointer) as artificial. We obtain this information from
15336 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
f792889a 15337 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
c906108c
SS
15338 fnp->voffset = VOFFSET_STATIC;
15339 }
15340 else
b98664d3 15341 complaint (_("member function type missing for '%s'"),
3da10d80 15342 dwarf2_full_name (fieldname, die, cu));
c906108c
SS
15343
15344 /* Get fcontext from DW_AT_containing_type if present. */
e142c38c 15345 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
e7c27a73 15346 fnp->fcontext = die_containing_type (die, cu);
c906108c 15347
3e43a32a
MS
15348 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
15349 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
c906108c
SS
15350
15351 /* Get accessibility. */
e142c38c 15352 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
c906108c 15353 if (attr)
aead7601 15354 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
60d5a603
JK
15355 else
15356 accessibility = dwarf2_default_access_attribute (die, cu);
15357 switch (accessibility)
c906108c 15358 {
60d5a603
JK
15359 case DW_ACCESS_private:
15360 fnp->is_private = 1;
15361 break;
15362 case DW_ACCESS_protected:
15363 fnp->is_protected = 1;
15364 break;
c906108c
SS
15365 }
15366
b02dede2 15367 /* Check for artificial methods. */
e142c38c 15368 attr = dwarf2_attr (die, DW_AT_artificial, cu);
b02dede2
DJ
15369 if (attr && DW_UNSND (attr) != 0)
15370 fnp->is_artificial = 1;
15371
7d27a96d
TT
15372 fnp->is_constructor = dwarf2_is_constructor (die, cu);
15373
0d564a31 15374 /* Get index in virtual function table if it is a virtual member
aec5aa8b
TT
15375 function. For older versions of GCC, this is an offset in the
15376 appropriate virtual table, as specified by DW_AT_containing_type.
15377 For everyone else, it is an expression to be evaluated relative
0d564a31
DJ
15378 to the object address. */
15379
e142c38c 15380 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
aec5aa8b 15381 if (attr)
8e19ed76 15382 {
aec5aa8b 15383 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size > 0)
8e19ed76 15384 {
aec5aa8b
TT
15385 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
15386 {
15387 /* Old-style GCC. */
15388 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
15389 }
15390 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
15391 || (DW_BLOCK (attr)->size > 1
15392 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
15393 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
15394 {
aec5aa8b
TT
15395 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
15396 if ((fnp->voffset % cu->header.addr_size) != 0)
15397 dwarf2_complex_location_expr_complaint ();
15398 else
15399 fnp->voffset /= cu->header.addr_size;
15400 fnp->voffset += 2;
15401 }
15402 else
15403 dwarf2_complex_location_expr_complaint ();
15404
15405 if (!fnp->fcontext)
7e993ebf
KS
15406 {
15407 /* If there is no `this' field and no DW_AT_containing_type,
15408 we cannot actually find a base class context for the
15409 vtable! */
15410 if (TYPE_NFIELDS (this_type) == 0
15411 || !TYPE_FIELD_ARTIFICIAL (this_type, 0))
15412 {
b98664d3 15413 complaint (_("cannot determine context for virtual member "
9d8780f0
SM
15414 "function \"%s\" (offset %s)"),
15415 fieldname, sect_offset_str (die->sect_off));
7e993ebf
KS
15416 }
15417 else
15418 {
15419 fnp->fcontext
15420 = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
15421 }
15422 }
aec5aa8b 15423 }
3690dd37 15424 else if (attr_form_is_section_offset (attr))
8e19ed76 15425 {
4d3c2250 15426 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
15427 }
15428 else
15429 {
4d3c2250
KB
15430 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
15431 fieldname);
8e19ed76 15432 }
0d564a31 15433 }
d48cc9dd
DJ
15434 else
15435 {
15436 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
15437 if (attr && DW_UNSND (attr))
15438 {
15439 /* GCC does this, as of 2008-08-25; PR debug/37237. */
b98664d3 15440 complaint (_("Member function \"%s\" (offset %s) is virtual "
3e43a32a 15441 "but the vtable offset is not specified"),
9d8780f0 15442 fieldname, sect_offset_str (die->sect_off));
9655fd1a 15443 ALLOCATE_CPLUS_STRUCT_TYPE (type);
d48cc9dd
DJ
15444 TYPE_CPLUS_DYNAMIC (type) = 1;
15445 }
15446 }
c906108c
SS
15447}
15448
15449/* Create the vector of member function fields, and attach it to the type. */
15450
15451static void
fba45db2 15452dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 15453 struct dwarf2_cu *cu)
c906108c 15454{
b4ba55a1 15455 if (cu->language == language_ada)
a73c6dcd 15456 error (_("unexpected member functions in Ada type"));
b4ba55a1 15457
c906108c
SS
15458 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15459 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
be2daae6
TT
15460 TYPE_ALLOC (type,
15461 sizeof (struct fn_fieldlist) * fip->fnfieldlists.size ());
c906108c 15462
be2daae6 15463 for (int i = 0; i < fip->fnfieldlists.size (); i++)
c906108c 15464 {
be2daae6 15465 struct fnfieldlist &nf = fip->fnfieldlists[i];
c906108c 15466 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
c906108c 15467
be2daae6
TT
15468 TYPE_FN_FIELDLIST_NAME (type, i) = nf.name;
15469 TYPE_FN_FIELDLIST_LENGTH (type, i) = nf.fnfields.size ();
c906108c 15470 fn_flp->fn_fields = (struct fn_field *)
be2daae6
TT
15471 TYPE_ALLOC (type, sizeof (struct fn_field) * nf.fnfields.size ());
15472
15473 for (int k = 0; k < nf.fnfields.size (); ++k)
15474 fn_flp->fn_fields[k] = nf.fnfields[k];
c906108c
SS
15475 }
15476
be2daae6 15477 TYPE_NFN_FIELDS (type) = fip->fnfieldlists.size ();
c906108c
SS
15478}
15479
1168df01
JB
15480/* Returns non-zero if NAME is the name of a vtable member in CU's
15481 language, zero otherwise. */
15482static int
15483is_vtable_name (const char *name, struct dwarf2_cu *cu)
15484{
15485 static const char vptr[] = "_vptr";
15486
9c37b5ae
TT
15487 /* Look for the C++ form of the vtable. */
15488 if (startswith (name, vptr) && is_cplus_marker (name[sizeof (vptr) - 1]))
1168df01
JB
15489 return 1;
15490
15491 return 0;
15492}
15493
c0dd20ea 15494/* GCC outputs unnamed structures that are really pointers to member
0b92b5bb
TT
15495 functions, with the ABI-specified layout. If TYPE describes
15496 such a structure, smash it into a member function type.
61049d3b
DJ
15497
15498 GCC shouldn't do this; it should just output pointer to member DIEs.
15499 This is GCC PR debug/28767. */
c0dd20ea 15500
0b92b5bb
TT
15501static void
15502quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
c0dd20ea 15503{
09e2d7c7 15504 struct type *pfn_type, *self_type, *new_type;
c0dd20ea
DJ
15505
15506 /* Check for a structure with no name and two children. */
0b92b5bb
TT
15507 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
15508 return;
c0dd20ea
DJ
15509
15510 /* Check for __pfn and __delta members. */
0b92b5bb
TT
15511 if (TYPE_FIELD_NAME (type, 0) == NULL
15512 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
15513 || TYPE_FIELD_NAME (type, 1) == NULL
15514 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
15515 return;
c0dd20ea
DJ
15516
15517 /* Find the type of the method. */
0b92b5bb 15518 pfn_type = TYPE_FIELD_TYPE (type, 0);
c0dd20ea
DJ
15519 if (pfn_type == NULL
15520 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
15521 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
0b92b5bb 15522 return;
c0dd20ea
DJ
15523
15524 /* Look for the "this" argument. */
15525 pfn_type = TYPE_TARGET_TYPE (pfn_type);
15526 if (TYPE_NFIELDS (pfn_type) == 0
0b92b5bb 15527 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
c0dd20ea 15528 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
0b92b5bb 15529 return;
c0dd20ea 15530
09e2d7c7 15531 self_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
0b92b5bb 15532 new_type = alloc_type (objfile);
09e2d7c7 15533 smash_to_method_type (new_type, self_type, TYPE_TARGET_TYPE (pfn_type),
c0dd20ea
DJ
15534 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
15535 TYPE_VARARGS (pfn_type));
0b92b5bb 15536 smash_to_methodptr_type (type, new_type);
c0dd20ea 15537}
1168df01 15538
2b4424c3
TT
15539/* If the DIE has a DW_AT_alignment attribute, return its value, doing
15540 appropriate error checking and issuing complaints if there is a
15541 problem. */
15542
15543static ULONGEST
15544get_alignment (struct dwarf2_cu *cu, struct die_info *die)
15545{
15546 struct attribute *attr = dwarf2_attr (die, DW_AT_alignment, cu);
15547
15548 if (attr == nullptr)
15549 return 0;
15550
15551 if (!attr_form_is_constant (attr))
15552 {
b98664d3 15553 complaint (_("DW_AT_alignment must have constant form"
2b4424c3
TT
15554 " - DIE at %s [in module %s]"),
15555 sect_offset_str (die->sect_off),
15556 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15557 return 0;
15558 }
15559
15560 ULONGEST align;
15561 if (attr->form == DW_FORM_sdata)
15562 {
15563 LONGEST val = DW_SND (attr);
15564 if (val < 0)
15565 {
b98664d3 15566 complaint (_("DW_AT_alignment value must not be negative"
2b4424c3
TT
15567 " - DIE at %s [in module %s]"),
15568 sect_offset_str (die->sect_off),
15569 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15570 return 0;
15571 }
15572 align = val;
15573 }
15574 else
15575 align = DW_UNSND (attr);
15576
15577 if (align == 0)
15578 {
b98664d3 15579 complaint (_("DW_AT_alignment value must not be zero"
2b4424c3
TT
15580 " - DIE at %s [in module %s]"),
15581 sect_offset_str (die->sect_off),
15582 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15583 return 0;
15584 }
15585 if ((align & (align - 1)) != 0)
15586 {
b98664d3 15587 complaint (_("DW_AT_alignment value must be a power of 2"
2b4424c3
TT
15588 " - DIE at %s [in module %s]"),
15589 sect_offset_str (die->sect_off),
15590 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15591 return 0;
15592 }
15593
15594 return align;
15595}
15596
15597/* If the DIE has a DW_AT_alignment attribute, use its value to set
15598 the alignment for TYPE. */
15599
15600static void
15601maybe_set_alignment (struct dwarf2_cu *cu, struct die_info *die,
15602 struct type *type)
15603{
15604 if (!set_type_align (type, get_alignment (cu, die)))
b98664d3 15605 complaint (_("DW_AT_alignment value too large"
2b4424c3
TT
15606 " - DIE at %s [in module %s]"),
15607 sect_offset_str (die->sect_off),
15608 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15609}
685b1105 15610
c906108c 15611/* Called when we find the DIE that starts a structure or union scope
c767944b
DJ
15612 (definition) to create a type for the structure or union. Fill in
15613 the type's name and general properties; the members will not be
83655187
DE
15614 processed until process_structure_scope. A symbol table entry for
15615 the type will also not be done until process_structure_scope (assuming
15616 the type has a name).
c906108c 15617
c767944b
DJ
15618 NOTE: we need to call these functions regardless of whether or not the
15619 DIE has a DW_AT_name attribute, since it might be an anonymous
c906108c 15620 structure or union. This gets the type entered into our set of
83655187 15621 user defined types. */
c906108c 15622
f792889a 15623static struct type *
134d01f1 15624read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 15625{
518817b3 15626 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c
SS
15627 struct type *type;
15628 struct attribute *attr;
15d034d0 15629 const char *name;
c906108c 15630
348e048f
DE
15631 /* If the definition of this type lives in .debug_types, read that type.
15632 Don't follow DW_AT_specification though, that will take us back up
15633 the chain and we want to go down. */
45e58e77 15634 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
348e048f
DE
15635 if (attr)
15636 {
ac9ec31b 15637 type = get_DW_AT_signature_type (die, attr, cu);
9dc481d3 15638
ac9ec31b 15639 /* The type's CU may not be the same as CU.
02142a6c 15640 Ensure TYPE is recorded with CU in die_type_hash. */
348e048f
DE
15641 return set_die_type (die, type, cu);
15642 }
15643
c0dd20ea 15644 type = alloc_type (objfile);
c906108c 15645 INIT_CPLUS_SPECIFIC (type);
93311388 15646
39cbfefa
DJ
15647 name = dwarf2_name (die, cu);
15648 if (name != NULL)
c906108c 15649 {
987504bb 15650 if (cu->language == language_cplus
c44af4eb
TT
15651 || cu->language == language_d
15652 || cu->language == language_rust)
63d06c5c 15653 {
15d034d0 15654 const char *full_name = dwarf2_full_name (name, die, cu);
3da10d80
KS
15655
15656 /* dwarf2_full_name might have already finished building the DIE's
15657 type. If so, there is no need to continue. */
15658 if (get_die_type (die, cu) != NULL)
15659 return get_die_type (die, cu);
15660
e86ca25f 15661 TYPE_NAME (type) = full_name;
63d06c5c
DC
15662 }
15663 else
15664 {
d8151005
DJ
15665 /* The name is already allocated along with this objfile, so
15666 we don't need to duplicate it for the type. */
e86ca25f 15667 TYPE_NAME (type) = name;
63d06c5c 15668 }
c906108c
SS
15669 }
15670
15671 if (die->tag == DW_TAG_structure_type)
15672 {
15673 TYPE_CODE (type) = TYPE_CODE_STRUCT;
15674 }
15675 else if (die->tag == DW_TAG_union_type)
15676 {
15677 TYPE_CODE (type) = TYPE_CODE_UNION;
15678 }
2ddeaf8a
TT
15679 else if (die->tag == DW_TAG_variant_part)
15680 {
15681 TYPE_CODE (type) = TYPE_CODE_UNION;
15682 TYPE_FLAG_DISCRIMINATED_UNION (type) = 1;
15683 }
c906108c
SS
15684 else
15685 {
4753d33b 15686 TYPE_CODE (type) = TYPE_CODE_STRUCT;
c906108c
SS
15687 }
15688
0cc2414c
TT
15689 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
15690 TYPE_DECLARED_CLASS (type) = 1;
15691
e142c38c 15692 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
15693 if (attr)
15694 {
155bfbd3
JB
15695 if (attr_form_is_constant (attr))
15696 TYPE_LENGTH (type) = DW_UNSND (attr);
15697 else
15698 {
15699 /* For the moment, dynamic type sizes are not supported
15700 by GDB's struct type. The actual size is determined
15701 on-demand when resolving the type of a given object,
15702 so set the type's length to zero for now. Otherwise,
15703 we record an expression as the length, and that expression
15704 could lead to a very large value, which could eventually
15705 lead to us trying to allocate that much memory when creating
15706 a value of that type. */
15707 TYPE_LENGTH (type) = 0;
15708 }
c906108c
SS
15709 }
15710 else
15711 {
15712 TYPE_LENGTH (type) = 0;
15713 }
15714
2b4424c3
TT
15715 maybe_set_alignment (cu, die, type);
15716
5230b05a 15717 if (producer_is_icc_lt_14 (cu) && (TYPE_LENGTH (type) == 0))
685b1105 15718 {
5230b05a
WT
15719 /* ICC<14 does not output the required DW_AT_declaration on
15720 incomplete types, but gives them a size of zero. */
422b1cb0 15721 TYPE_STUB (type) = 1;
685b1105
JK
15722 }
15723 else
15724 TYPE_STUB_SUPPORTED (type) = 1;
15725
dc718098 15726 if (die_is_declaration (die, cu))
876cecd0 15727 TYPE_STUB (type) = 1;
a6c727b2
DJ
15728 else if (attr == NULL && die->child == NULL
15729 && producer_is_realview (cu->producer))
15730 /* RealView does not output the required DW_AT_declaration
15731 on incomplete types. */
15732 TYPE_STUB (type) = 1;
dc718098 15733
c906108c
SS
15734 /* We need to add the type field to the die immediately so we don't
15735 infinitely recurse when dealing with pointers to the structure
0963b4bd 15736 type within the structure itself. */
1c379e20 15737 set_die_type (die, type, cu);
c906108c 15738
7e314c57
JK
15739 /* set_die_type should be already done. */
15740 set_descriptive_type (type, die, cu);
15741
c767944b
DJ
15742 return type;
15743}
15744
2ddeaf8a
TT
15745/* A helper for process_structure_scope that handles a single member
15746 DIE. */
15747
15748static void
15749handle_struct_member_die (struct die_info *child_die, struct type *type,
15750 struct field_info *fi,
15751 std::vector<struct symbol *> *template_args,
15752 struct dwarf2_cu *cu)
15753{
15754 if (child_die->tag == DW_TAG_member
15755 || child_die->tag == DW_TAG_variable
15756 || child_die->tag == DW_TAG_variant_part)
15757 {
15758 /* NOTE: carlton/2002-11-05: A C++ static data member
15759 should be a DW_TAG_member that is a declaration, but
15760 all versions of G++ as of this writing (so through at
15761 least 3.2.1) incorrectly generate DW_TAG_variable
15762 tags for them instead. */
15763 dwarf2_add_field (fi, child_die, cu);
15764 }
15765 else if (child_die->tag == DW_TAG_subprogram)
15766 {
15767 /* Rust doesn't have member functions in the C++ sense.
15768 However, it does emit ordinary functions as children
15769 of a struct DIE. */
15770 if (cu->language == language_rust)
15771 read_func_scope (child_die, cu);
15772 else
15773 {
15774 /* C++ member function. */
15775 dwarf2_add_member_fn (fi, child_die, type, cu);
15776 }
15777 }
15778 else if (child_die->tag == DW_TAG_inheritance)
15779 {
15780 /* C++ base class field. */
15781 dwarf2_add_field (fi, child_die, cu);
15782 }
15783 else if (type_can_define_types (child_die))
15784 dwarf2_add_type_defn (fi, child_die, cu);
15785 else if (child_die->tag == DW_TAG_template_type_param
15786 || child_die->tag == DW_TAG_template_value_param)
15787 {
15788 struct symbol *arg = new_symbol (child_die, NULL, cu);
15789
15790 if (arg != NULL)
15791 template_args->push_back (arg);
15792 }
15793 else if (child_die->tag == DW_TAG_variant)
15794 {
15795 /* In a variant we want to get the discriminant and also add a
15796 field for our sole member child. */
15797 struct attribute *discr = dwarf2_attr (child_die, DW_AT_discr_value, cu);
15798
15799 for (struct die_info *variant_child = child_die->child;
15800 variant_child != NULL;
15801 variant_child = sibling_die (variant_child))
15802 {
15803 if (variant_child->tag == DW_TAG_member)
15804 {
15805 handle_struct_member_die (variant_child, type, fi,
15806 template_args, cu);
15807 /* Only handle the one. */
15808 break;
15809 }
15810 }
15811
15812 /* We don't handle this but we might as well report it if we see
15813 it. */
15814 if (dwarf2_attr (child_die, DW_AT_discr_list, cu) != nullptr)
b98664d3 15815 complaint (_("DW_AT_discr_list is not supported yet"
2ddeaf8a
TT
15816 " - DIE at %s [in module %s]"),
15817 sect_offset_str (child_die->sect_off),
15818 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15819
15820 /* The first field was just added, so we can stash the
15821 discriminant there. */
be2daae6 15822 gdb_assert (!fi->fields.empty ());
2ddeaf8a 15823 if (discr == NULL)
be2daae6 15824 fi->fields.back ().variant.default_branch = true;
2ddeaf8a 15825 else
be2daae6 15826 fi->fields.back ().variant.discriminant_value = DW_UNSND (discr);
2ddeaf8a
TT
15827 }
15828}
15829
c767944b
DJ
15830/* Finish creating a structure or union type, including filling in
15831 its members and creating a symbol for it. */
15832
15833static void
15834process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
15835{
518817b3 15836 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
ca040673 15837 struct die_info *child_die;
c767944b
DJ
15838 struct type *type;
15839
15840 type = get_die_type (die, cu);
15841 if (type == NULL)
15842 type = read_structure_type (die, cu);
15843
2ddeaf8a
TT
15844 /* When reading a DW_TAG_variant_part, we need to notice when we
15845 read the discriminant member, so we can record it later in the
15846 discriminant_info. */
15847 bool is_variant_part = TYPE_FLAG_DISCRIMINATED_UNION (type);
15848 sect_offset discr_offset;
15849
15850 if (is_variant_part)
15851 {
15852 struct attribute *discr = dwarf2_attr (die, DW_AT_discr, cu);
15853 if (discr == NULL)
15854 {
15855 /* Maybe it's a univariant form, an extension we support.
15856 In this case arrange not to check the offset. */
15857 is_variant_part = false;
15858 }
15859 else if (attr_form_is_ref (discr))
15860 {
15861 struct dwarf2_cu *target_cu = cu;
15862 struct die_info *target_die = follow_die_ref (die, discr, &target_cu);
15863
15864 discr_offset = target_die->sect_off;
15865 }
15866 else
15867 {
b98664d3 15868 complaint (_("DW_AT_discr does not have DIE reference form"
2ddeaf8a
TT
15869 " - DIE at %s [in module %s]"),
15870 sect_offset_str (die->sect_off),
15871 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15872 is_variant_part = false;
15873 }
15874 }
15875
e142c38c 15876 if (die->child != NULL && ! die_is_declaration (die, cu))
c906108c
SS
15877 {
15878 struct field_info fi;
2f4732b0 15879 std::vector<struct symbol *> template_args;
c906108c 15880
639d11d3 15881 child_die = die->child;
c906108c
SS
15882
15883 while (child_die && child_die->tag)
15884 {
2ddeaf8a 15885 handle_struct_member_die (child_die, type, &fi, &template_args, cu);
34eaf542 15886
2ddeaf8a 15887 if (is_variant_part && discr_offset == child_die->sect_off)
be2daae6 15888 fi.fields.back ().variant.is_discriminant = true;
34eaf542 15889
c906108c
SS
15890 child_die = sibling_die (child_die);
15891 }
15892
34eaf542 15893 /* Attach template arguments to type. */
2f4732b0 15894 if (!template_args.empty ())
34eaf542
TT
15895 {
15896 ALLOCATE_CPLUS_STRUCT_TYPE (type);
2f4732b0 15897 TYPE_N_TEMPLATE_ARGUMENTS (type) = template_args.size ();
34eaf542 15898 TYPE_TEMPLATE_ARGUMENTS (type)
8d749320
SM
15899 = XOBNEWVEC (&objfile->objfile_obstack,
15900 struct symbol *,
15901 TYPE_N_TEMPLATE_ARGUMENTS (type));
34eaf542 15902 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
2f4732b0 15903 template_args.data (),
34eaf542
TT
15904 (TYPE_N_TEMPLATE_ARGUMENTS (type)
15905 * sizeof (struct symbol *)));
34eaf542
TT
15906 }
15907
c906108c
SS
15908 /* Attach fields and member functions to the type. */
15909 if (fi.nfields)
e7c27a73 15910 dwarf2_attach_fields_to_type (&fi, type, cu);
be2daae6 15911 if (!fi.fnfieldlists.empty ())
c906108c 15912 {
e7c27a73 15913 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
c906108c 15914
c5aa993b 15915 /* Get the type which refers to the base class (possibly this
c906108c 15916 class itself) which contains the vtable pointer for the current
0d564a31
DJ
15917 class from the DW_AT_containing_type attribute. This use of
15918 DW_AT_containing_type is a GNU extension. */
c906108c 15919
e142c38c 15920 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
c906108c 15921 {
e7c27a73 15922 struct type *t = die_containing_type (die, cu);
c906108c 15923
ae6ae975 15924 set_type_vptr_basetype (type, t);
c906108c
SS
15925 if (type == t)
15926 {
c906108c
SS
15927 int i;
15928
15929 /* Our own class provides vtbl ptr. */
15930 for (i = TYPE_NFIELDS (t) - 1;
15931 i >= TYPE_N_BASECLASSES (t);
15932 --i)
15933 {
0d5cff50 15934 const char *fieldname = TYPE_FIELD_NAME (t, i);
c906108c 15935
1168df01 15936 if (is_vtable_name (fieldname, cu))
c906108c 15937 {
ae6ae975 15938 set_type_vptr_fieldno (type, i);
c906108c
SS
15939 break;
15940 }
15941 }
15942
15943 /* Complain if virtual function table field not found. */
15944 if (i < TYPE_N_BASECLASSES (t))
b98664d3 15945 complaint (_("virtual function table pointer "
3e43a32a 15946 "not found when defining class '%s'"),
e86ca25f 15947 TYPE_NAME (type) ? TYPE_NAME (type) : "");
c906108c
SS
15948 }
15949 else
15950 {
ae6ae975 15951 set_type_vptr_fieldno (type, TYPE_VPTR_FIELDNO (t));
c906108c
SS
15952 }
15953 }
f6235d4c 15954 else if (cu->producer
61012eef 15955 && startswith (cu->producer, "IBM(R) XL C/C++ Advanced Edition"))
f6235d4c
EZ
15956 {
15957 /* The IBM XLC compiler does not provide direct indication
15958 of the containing type, but the vtable pointer is
15959 always named __vfp. */
15960
15961 int i;
15962
15963 for (i = TYPE_NFIELDS (type) - 1;
15964 i >= TYPE_N_BASECLASSES (type);
15965 --i)
15966 {
15967 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
15968 {
ae6ae975
DE
15969 set_type_vptr_fieldno (type, i);
15970 set_type_vptr_basetype (type, type);
f6235d4c
EZ
15971 break;
15972 }
15973 }
15974 }
c906108c 15975 }
98751a41
JK
15976
15977 /* Copy fi.typedef_field_list linked list elements content into the
15978 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
be2daae6 15979 if (!fi.typedef_field_list.empty ())
98751a41 15980 {
be2daae6 15981 int count = fi.typedef_field_list.size ();
98751a41 15982
a0d7a4ff 15983 ALLOCATE_CPLUS_STRUCT_TYPE (type);
98751a41 15984 TYPE_TYPEDEF_FIELD_ARRAY (type)
883fd55a 15985 = ((struct decl_field *)
be2daae6
TT
15986 TYPE_ALLOC (type,
15987 sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * count));
15988 TYPE_TYPEDEF_FIELD_COUNT (type) = count;
6e70227d 15989
be2daae6
TT
15990 for (int i = 0; i < fi.typedef_field_list.size (); ++i)
15991 TYPE_TYPEDEF_FIELD (type, i) = fi.typedef_field_list[i];
98751a41 15992 }
c767944b 15993
883fd55a
KS
15994 /* Copy fi.nested_types_list linked list elements content into the
15995 allocated array TYPE_NESTED_TYPES_ARRAY (type). */
be2daae6 15996 if (!fi.nested_types_list.empty () && cu->language != language_ada)
883fd55a 15997 {
be2daae6 15998 int count = fi.nested_types_list.size ();
883fd55a
KS
15999
16000 ALLOCATE_CPLUS_STRUCT_TYPE (type);
16001 TYPE_NESTED_TYPES_ARRAY (type)
16002 = ((struct decl_field *)
be2daae6
TT
16003 TYPE_ALLOC (type, sizeof (struct decl_field) * count));
16004 TYPE_NESTED_TYPES_COUNT (type) = count;
883fd55a 16005
be2daae6
TT
16006 for (int i = 0; i < fi.nested_types_list.size (); ++i)
16007 TYPE_NESTED_TYPES_FIELD (type, i) = fi.nested_types_list[i];
883fd55a 16008 }
c906108c 16009 }
63d06c5c 16010
bb5ed363 16011 quirk_gcc_member_function_pointer (type, objfile);
c9317f21
TT
16012 if (cu->language == language_rust && die->tag == DW_TAG_union_type)
16013 cu->rust_unions.push_back (type);
0b92b5bb 16014
90aeadfc
DC
16015 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
16016 snapshots) has been known to create a die giving a declaration
16017 for a class that has, as a child, a die giving a definition for a
16018 nested class. So we have to process our children even if the
16019 current die is a declaration. Normally, of course, a declaration
16020 won't have any children at all. */
134d01f1 16021
ca040673
DE
16022 child_die = die->child;
16023
90aeadfc
DC
16024 while (child_die != NULL && child_die->tag)
16025 {
16026 if (child_die->tag == DW_TAG_member
16027 || child_die->tag == DW_TAG_variable
34eaf542
TT
16028 || child_die->tag == DW_TAG_inheritance
16029 || child_die->tag == DW_TAG_template_value_param
16030 || child_die->tag == DW_TAG_template_type_param)
134d01f1 16031 {
90aeadfc 16032 /* Do nothing. */
134d01f1 16033 }
90aeadfc
DC
16034 else
16035 process_die (child_die, cu);
134d01f1 16036
90aeadfc 16037 child_die = sibling_die (child_die);
134d01f1
DJ
16038 }
16039
fa4028e9
JB
16040 /* Do not consider external references. According to the DWARF standard,
16041 these DIEs are identified by the fact that they have no byte_size
16042 attribute, and a declaration attribute. */
16043 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
16044 || !die_is_declaration (die, cu))
c767944b 16045 new_symbol (die, type, cu);
134d01f1
DJ
16046}
16047
55426c9d
JB
16048/* Assuming DIE is an enumeration type, and TYPE is its associated type,
16049 update TYPE using some information only available in DIE's children. */
16050
16051static void
16052update_enumeration_type_from_children (struct die_info *die,
16053 struct type *type,
16054 struct dwarf2_cu *cu)
16055{
60f7655a 16056 struct die_info *child_die;
55426c9d
JB
16057 int unsigned_enum = 1;
16058 int flag_enum = 1;
16059 ULONGEST mask = 0;
55426c9d 16060
8268c778 16061 auto_obstack obstack;
55426c9d 16062
60f7655a
DE
16063 for (child_die = die->child;
16064 child_die != NULL && child_die->tag;
16065 child_die = sibling_die (child_die))
55426c9d
JB
16066 {
16067 struct attribute *attr;
16068 LONGEST value;
16069 const gdb_byte *bytes;
16070 struct dwarf2_locexpr_baton *baton;
16071 const char *name;
60f7655a 16072
55426c9d
JB
16073 if (child_die->tag != DW_TAG_enumerator)
16074 continue;
16075
16076 attr = dwarf2_attr (child_die, DW_AT_const_value, cu);
16077 if (attr == NULL)
16078 continue;
16079
16080 name = dwarf2_name (child_die, cu);
16081 if (name == NULL)
16082 name = "<anonymous enumerator>";
16083
16084 dwarf2_const_value_attr (attr, type, name, &obstack, cu,
16085 &value, &bytes, &baton);
16086 if (value < 0)
16087 {
16088 unsigned_enum = 0;
16089 flag_enum = 0;
16090 }
16091 else if ((mask & value) != 0)
16092 flag_enum = 0;
16093 else
16094 mask |= value;
16095
16096 /* If we already know that the enum type is neither unsigned, nor
16097 a flag type, no need to look at the rest of the enumerates. */
16098 if (!unsigned_enum && !flag_enum)
16099 break;
55426c9d
JB
16100 }
16101
16102 if (unsigned_enum)
16103 TYPE_UNSIGNED (type) = 1;
16104 if (flag_enum)
16105 TYPE_FLAG_ENUM (type) = 1;
55426c9d
JB
16106}
16107
134d01f1
DJ
16108/* Given a DW_AT_enumeration_type die, set its type. We do not
16109 complete the type's fields yet, or create any symbols. */
c906108c 16110
f792889a 16111static struct type *
134d01f1 16112read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16113{
518817b3 16114 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c 16115 struct type *type;
c906108c 16116 struct attribute *attr;
0114d602 16117 const char *name;
134d01f1 16118
348e048f
DE
16119 /* If the definition of this type lives in .debug_types, read that type.
16120 Don't follow DW_AT_specification though, that will take us back up
16121 the chain and we want to go down. */
45e58e77 16122 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
348e048f
DE
16123 if (attr)
16124 {
ac9ec31b 16125 type = get_DW_AT_signature_type (die, attr, cu);
9dc481d3 16126
ac9ec31b 16127 /* The type's CU may not be the same as CU.
02142a6c 16128 Ensure TYPE is recorded with CU in die_type_hash. */
348e048f
DE
16129 return set_die_type (die, type, cu);
16130 }
16131
c906108c
SS
16132 type = alloc_type (objfile);
16133
16134 TYPE_CODE (type) = TYPE_CODE_ENUM;
94af9270 16135 name = dwarf2_full_name (NULL, die, cu);
39cbfefa 16136 if (name != NULL)
e86ca25f 16137 TYPE_NAME (type) = name;
c906108c 16138
0626fc76
TT
16139 attr = dwarf2_attr (die, DW_AT_type, cu);
16140 if (attr != NULL)
16141 {
16142 struct type *underlying_type = die_type (die, cu);
16143
16144 TYPE_TARGET_TYPE (type) = underlying_type;
16145 }
16146
e142c38c 16147 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
16148 if (attr)
16149 {
16150 TYPE_LENGTH (type) = DW_UNSND (attr);
16151 }
16152 else
16153 {
16154 TYPE_LENGTH (type) = 0;
16155 }
16156
2b4424c3
TT
16157 maybe_set_alignment (cu, die, type);
16158
137033e9
JB
16159 /* The enumeration DIE can be incomplete. In Ada, any type can be
16160 declared as private in the package spec, and then defined only
16161 inside the package body. Such types are known as Taft Amendment
16162 Types. When another package uses such a type, an incomplete DIE
16163 may be generated by the compiler. */
02eb380e 16164 if (die_is_declaration (die, cu))
876cecd0 16165 TYPE_STUB (type) = 1;
02eb380e 16166
0626fc76
TT
16167 /* Finish the creation of this type by using the enum's children.
16168 We must call this even when the underlying type has been provided
16169 so that we can determine if we're looking at a "flag" enum. */
55426c9d
JB
16170 update_enumeration_type_from_children (die, type, cu);
16171
0626fc76
TT
16172 /* If this type has an underlying type that is not a stub, then we
16173 may use its attributes. We always use the "unsigned" attribute
16174 in this situation, because ordinarily we guess whether the type
16175 is unsigned -- but the guess can be wrong and the underlying type
16176 can tell us the reality. However, we defer to a local size
16177 attribute if one exists, because this lets the compiler override
16178 the underlying type if needed. */
16179 if (TYPE_TARGET_TYPE (type) != NULL && !TYPE_STUB (TYPE_TARGET_TYPE (type)))
16180 {
16181 TYPE_UNSIGNED (type) = TYPE_UNSIGNED (TYPE_TARGET_TYPE (type));
16182 if (TYPE_LENGTH (type) == 0)
16183 TYPE_LENGTH (type) = TYPE_LENGTH (TYPE_TARGET_TYPE (type));
2b4424c3
TT
16184 if (TYPE_RAW_ALIGN (type) == 0
16185 && TYPE_RAW_ALIGN (TYPE_TARGET_TYPE (type)) != 0)
16186 set_type_align (type, TYPE_RAW_ALIGN (TYPE_TARGET_TYPE (type)));
0626fc76
TT
16187 }
16188
3d567982
TT
16189 TYPE_DECLARED_CLASS (type) = dwarf2_flag_true_p (die, DW_AT_enum_class, cu);
16190
f792889a 16191 return set_die_type (die, type, cu);
134d01f1
DJ
16192}
16193
16194/* Given a pointer to a die which begins an enumeration, process all
16195 the dies that define the members of the enumeration, and create the
16196 symbol for the enumeration type.
16197
16198 NOTE: We reverse the order of the element list. */
16199
16200static void
16201process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
16202{
f792889a 16203 struct type *this_type;
134d01f1 16204
f792889a
DJ
16205 this_type = get_die_type (die, cu);
16206 if (this_type == NULL)
16207 this_type = read_enumeration_type (die, cu);
9dc481d3 16208
639d11d3 16209 if (die->child != NULL)
c906108c 16210 {
9dc481d3
DE
16211 struct die_info *child_die;
16212 struct symbol *sym;
16213 struct field *fields = NULL;
16214 int num_fields = 0;
15d034d0 16215 const char *name;
9dc481d3 16216
639d11d3 16217 child_die = die->child;
c906108c
SS
16218 while (child_die && child_die->tag)
16219 {
16220 if (child_die->tag != DW_TAG_enumerator)
16221 {
e7c27a73 16222 process_die (child_die, cu);
c906108c
SS
16223 }
16224 else
16225 {
39cbfefa
DJ
16226 name = dwarf2_name (child_die, cu);
16227 if (name)
c906108c 16228 {
f792889a 16229 sym = new_symbol (child_die, this_type, cu);
c906108c
SS
16230
16231 if ((num_fields % DW_FIELD_ALLOC_CHUNK) == 0)
16232 {
16233 fields = (struct field *)
16234 xrealloc (fields,
16235 (num_fields + DW_FIELD_ALLOC_CHUNK)
c5aa993b 16236 * sizeof (struct field));
c906108c
SS
16237 }
16238
3567439c 16239 FIELD_NAME (fields[num_fields]) = SYMBOL_LINKAGE_NAME (sym);
c906108c 16240 FIELD_TYPE (fields[num_fields]) = NULL;
14e75d8e 16241 SET_FIELD_ENUMVAL (fields[num_fields], SYMBOL_VALUE (sym));
c906108c
SS
16242 FIELD_BITSIZE (fields[num_fields]) = 0;
16243
16244 num_fields++;
16245 }
16246 }
16247
16248 child_die = sibling_die (child_die);
16249 }
16250
16251 if (num_fields)
16252 {
f792889a
DJ
16253 TYPE_NFIELDS (this_type) = num_fields;
16254 TYPE_FIELDS (this_type) = (struct field *)
16255 TYPE_ALLOC (this_type, sizeof (struct field) * num_fields);
16256 memcpy (TYPE_FIELDS (this_type), fields,
c906108c 16257 sizeof (struct field) * num_fields);
b8c9b27d 16258 xfree (fields);
c906108c 16259 }
c906108c 16260 }
134d01f1 16261
6c83ed52
TT
16262 /* If we are reading an enum from a .debug_types unit, and the enum
16263 is a declaration, and the enum is not the signatured type in the
16264 unit, then we do not want to add a symbol for it. Adding a
16265 symbol would in some cases obscure the true definition of the
16266 enum, giving users an incomplete type when the definition is
16267 actually available. Note that we do not want to do this for all
16268 enums which are just declarations, because C++0x allows forward
16269 enum declarations. */
3019eac3 16270 if (cu->per_cu->is_debug_types
6c83ed52
TT
16271 && die_is_declaration (die, cu))
16272 {
52dc124a 16273 struct signatured_type *sig_type;
6c83ed52 16274
c0f78cd4 16275 sig_type = (struct signatured_type *) cu->per_cu;
9c541725
PA
16276 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
16277 if (sig_type->type_offset_in_section != die->sect_off)
6c83ed52
TT
16278 return;
16279 }
16280
f792889a 16281 new_symbol (die, this_type, cu);
c906108c
SS
16282}
16283
16284/* Extract all information from a DW_TAG_array_type DIE and put it in
16285 the DIE's type field. For now, this only handles one dimensional
16286 arrays. */
16287
f792889a 16288static struct type *
e7c27a73 16289read_array_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16290{
518817b3 16291 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c 16292 struct die_info *child_die;
7e314c57 16293 struct type *type;
c906108c 16294 struct type *element_type, *range_type, *index_type;
c906108c 16295 struct attribute *attr;
15d034d0 16296 const char *name;
a405673c 16297 struct dynamic_prop *byte_stride_prop = NULL;
dc53a7ad 16298 unsigned int bit_stride = 0;
c906108c 16299
e7c27a73 16300 element_type = die_type (die, cu);
c906108c 16301
7e314c57
JK
16302 /* The die_type call above may have already set the type for this DIE. */
16303 type = get_die_type (die, cu);
16304 if (type)
16305 return type;
16306
dc53a7ad
JB
16307 attr = dwarf2_attr (die, DW_AT_byte_stride, cu);
16308 if (attr != NULL)
a405673c
JB
16309 {
16310 int stride_ok;
16311
16312 byte_stride_prop
16313 = (struct dynamic_prop *) alloca (sizeof (struct dynamic_prop));
16314 stride_ok = attr_to_dynamic_prop (attr, die, cu, byte_stride_prop);
16315 if (!stride_ok)
16316 {
b98664d3 16317 complaint (_("unable to read array DW_AT_byte_stride "
9d8780f0
SM
16318 " - DIE at %s [in module %s]"),
16319 sect_offset_str (die->sect_off),
518817b3 16320 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
a405673c
JB
16321 /* Ignore this attribute. We will likely not be able to print
16322 arrays of this type correctly, but there is little we can do
16323 to help if we cannot read the attribute's value. */
16324 byte_stride_prop = NULL;
16325 }
16326 }
dc53a7ad
JB
16327
16328 attr = dwarf2_attr (die, DW_AT_bit_stride, cu);
16329 if (attr != NULL)
16330 bit_stride = DW_UNSND (attr);
16331
c906108c
SS
16332 /* Irix 6.2 native cc creates array types without children for
16333 arrays with unspecified length. */
639d11d3 16334 if (die->child == NULL)
c906108c 16335 {
46bf5051 16336 index_type = objfile_type (objfile)->builtin_int;
0c9c3474 16337 range_type = create_static_range_type (NULL, index_type, 0, -1);
dc53a7ad 16338 type = create_array_type_with_stride (NULL, element_type, range_type,
a405673c 16339 byte_stride_prop, bit_stride);
f792889a 16340 return set_die_type (die, type, cu);
c906108c
SS
16341 }
16342
791afaa2 16343 std::vector<struct type *> range_types;
639d11d3 16344 child_die = die->child;
c906108c
SS
16345 while (child_die && child_die->tag)
16346 {
16347 if (child_die->tag == DW_TAG_subrange_type)
16348 {
f792889a 16349 struct type *child_type = read_type_die (child_die, cu);
9a619af0 16350
f792889a 16351 if (child_type != NULL)
a02abb62 16352 {
0963b4bd
MS
16353 /* The range type was succesfully read. Save it for the
16354 array type creation. */
791afaa2 16355 range_types.push_back (child_type);
a02abb62 16356 }
c906108c
SS
16357 }
16358 child_die = sibling_die (child_die);
16359 }
16360
16361 /* Dwarf2 dimensions are output from left to right, create the
16362 necessary array types in backwards order. */
7ca2d3a3 16363
c906108c 16364 type = element_type;
7ca2d3a3
DL
16365
16366 if (read_array_order (die, cu) == DW_ORD_col_major)
16367 {
16368 int i = 0;
9a619af0 16369
791afaa2 16370 while (i < range_types.size ())
dc53a7ad 16371 type = create_array_type_with_stride (NULL, type, range_types[i++],
a405673c 16372 byte_stride_prop, bit_stride);
7ca2d3a3
DL
16373 }
16374 else
16375 {
791afaa2 16376 size_t ndim = range_types.size ();
7ca2d3a3 16377 while (ndim-- > 0)
dc53a7ad 16378 type = create_array_type_with_stride (NULL, type, range_types[ndim],
a405673c 16379 byte_stride_prop, bit_stride);
7ca2d3a3 16380 }
c906108c 16381
f5f8a009
EZ
16382 /* Understand Dwarf2 support for vector types (like they occur on
16383 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
16384 array type. This is not part of the Dwarf2/3 standard yet, but a
16385 custom vendor extension. The main difference between a regular
16386 array and the vector variant is that vectors are passed by value
16387 to functions. */
e142c38c 16388 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
f5f8a009 16389 if (attr)
ea37ba09 16390 make_vector_type (type);
f5f8a009 16391
dbc98a8b
KW
16392 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
16393 implementation may choose to implement triple vectors using this
16394 attribute. */
16395 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16396 if (attr)
16397 {
16398 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
16399 TYPE_LENGTH (type) = DW_UNSND (attr);
16400 else
b98664d3 16401 complaint (_("DW_AT_byte_size for array type smaller "
3e43a32a 16402 "than the total size of elements"));
dbc98a8b
KW
16403 }
16404
39cbfefa
DJ
16405 name = dwarf2_name (die, cu);
16406 if (name)
16407 TYPE_NAME (type) = name;
6e70227d 16408
2b4424c3
TT
16409 maybe_set_alignment (cu, die, type);
16410
0963b4bd 16411 /* Install the type in the die. */
7e314c57
JK
16412 set_die_type (die, type, cu);
16413
16414 /* set_die_type should be already done. */
b4ba55a1
JB
16415 set_descriptive_type (type, die, cu);
16416
7e314c57 16417 return type;
c906108c
SS
16418}
16419
7ca2d3a3 16420static enum dwarf_array_dim_ordering
6e70227d 16421read_array_order (struct die_info *die, struct dwarf2_cu *cu)
7ca2d3a3
DL
16422{
16423 struct attribute *attr;
16424
16425 attr = dwarf2_attr (die, DW_AT_ordering, cu);
16426
aead7601
SM
16427 if (attr)
16428 return (enum dwarf_array_dim_ordering) DW_SND (attr);
7ca2d3a3 16429
0963b4bd
MS
16430 /* GNU F77 is a special case, as at 08/2004 array type info is the
16431 opposite order to the dwarf2 specification, but data is still
16432 laid out as per normal fortran.
7ca2d3a3 16433
0963b4bd
MS
16434 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
16435 version checking. */
7ca2d3a3 16436
905e0470
PM
16437 if (cu->language == language_fortran
16438 && cu->producer && strstr (cu->producer, "GNU F77"))
7ca2d3a3
DL
16439 {
16440 return DW_ORD_row_major;
16441 }
16442
6e70227d 16443 switch (cu->language_defn->la_array_ordering)
7ca2d3a3
DL
16444 {
16445 case array_column_major:
16446 return DW_ORD_col_major;
16447 case array_row_major:
16448 default:
16449 return DW_ORD_row_major;
16450 };
16451}
16452
72019c9c 16453/* Extract all information from a DW_TAG_set_type DIE and put it in
0963b4bd 16454 the DIE's type field. */
72019c9c 16455
f792889a 16456static struct type *
72019c9c
GM
16457read_set_type (struct die_info *die, struct dwarf2_cu *cu)
16458{
7e314c57
JK
16459 struct type *domain_type, *set_type;
16460 struct attribute *attr;
f792889a 16461
7e314c57
JK
16462 domain_type = die_type (die, cu);
16463
16464 /* The die_type call above may have already set the type for this DIE. */
16465 set_type = get_die_type (die, cu);
16466 if (set_type)
16467 return set_type;
16468
16469 set_type = create_set_type (NULL, domain_type);
16470
16471 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
d09039dd
PM
16472 if (attr)
16473 TYPE_LENGTH (set_type) = DW_UNSND (attr);
7e314c57 16474
2b4424c3
TT
16475 maybe_set_alignment (cu, die, set_type);
16476
f792889a 16477 return set_die_type (die, set_type, cu);
72019c9c 16478}
7ca2d3a3 16479
0971de02
TT
16480/* A helper for read_common_block that creates a locexpr baton.
16481 SYM is the symbol which we are marking as computed.
16482 COMMON_DIE is the DIE for the common block.
16483 COMMON_LOC is the location expression attribute for the common
16484 block itself.
16485 MEMBER_LOC is the location expression attribute for the particular
16486 member of the common block that we are processing.
16487 CU is the CU from which the above come. */
16488
16489static void
16490mark_common_block_symbol_computed (struct symbol *sym,
16491 struct die_info *common_die,
16492 struct attribute *common_loc,
16493 struct attribute *member_loc,
16494 struct dwarf2_cu *cu)
16495{
518817b3
SM
16496 struct dwarf2_per_objfile *dwarf2_per_objfile
16497 = cu->per_cu->dwarf2_per_objfile;
0971de02
TT
16498 struct objfile *objfile = dwarf2_per_objfile->objfile;
16499 struct dwarf2_locexpr_baton *baton;
16500 gdb_byte *ptr;
16501 unsigned int cu_off;
16502 enum bfd_endian byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
16503 LONGEST offset = 0;
16504
16505 gdb_assert (common_loc && member_loc);
16506 gdb_assert (attr_form_is_block (common_loc));
16507 gdb_assert (attr_form_is_block (member_loc)
16508 || attr_form_is_constant (member_loc));
16509
8d749320 16510 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
0971de02
TT
16511 baton->per_cu = cu->per_cu;
16512 gdb_assert (baton->per_cu);
16513
16514 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
16515
16516 if (attr_form_is_constant (member_loc))
16517 {
16518 offset = dwarf2_get_attr_constant_value (member_loc, 0);
16519 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
16520 }
16521 else
16522 baton->size += DW_BLOCK (member_loc)->size;
16523
224c3ddb 16524 ptr = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, baton->size);
0971de02
TT
16525 baton->data = ptr;
16526
16527 *ptr++ = DW_OP_call4;
9c541725 16528 cu_off = common_die->sect_off - cu->per_cu->sect_off;
0971de02
TT
16529 store_unsigned_integer (ptr, 4, byte_order, cu_off);
16530 ptr += 4;
16531
16532 if (attr_form_is_constant (member_loc))
16533 {
16534 *ptr++ = DW_OP_addr;
16535 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
16536 ptr += cu->header.addr_size;
16537 }
16538 else
16539 {
16540 /* We have to copy the data here, because DW_OP_call4 will only
16541 use a DW_AT_location attribute. */
16542 memcpy (ptr, DW_BLOCK (member_loc)->data, DW_BLOCK (member_loc)->size);
16543 ptr += DW_BLOCK (member_loc)->size;
16544 }
16545
16546 *ptr++ = DW_OP_plus;
16547 gdb_assert (ptr - baton->data == baton->size);
16548
0971de02 16549 SYMBOL_LOCATION_BATON (sym) = baton;
f1e6e072 16550 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
0971de02
TT
16551}
16552
4357ac6c
TT
16553/* Create appropriate locally-scoped variables for all the
16554 DW_TAG_common_block entries. Also create a struct common_block
16555 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
16556 is used to sepate the common blocks name namespace from regular
16557 variable names. */
c906108c
SS
16558
16559static void
e7c27a73 16560read_common_block (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16561{
0971de02
TT
16562 struct attribute *attr;
16563
16564 attr = dwarf2_attr (die, DW_AT_location, cu);
16565 if (attr)
16566 {
16567 /* Support the .debug_loc offsets. */
16568 if (attr_form_is_block (attr))
16569 {
16570 /* Ok. */
16571 }
16572 else if (attr_form_is_section_offset (attr))
16573 {
16574 dwarf2_complex_location_expr_complaint ();
16575 attr = NULL;
16576 }
16577 else
16578 {
16579 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
16580 "common block member");
16581 attr = NULL;
16582 }
16583 }
16584
639d11d3 16585 if (die->child != NULL)
c906108c 16586 {
518817b3 16587 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
4357ac6c
TT
16588 struct die_info *child_die;
16589 size_t n_entries = 0, size;
16590 struct common_block *common_block;
16591 struct symbol *sym;
74ac6d43 16592
4357ac6c
TT
16593 for (child_die = die->child;
16594 child_die && child_die->tag;
16595 child_die = sibling_die (child_die))
16596 ++n_entries;
16597
16598 size = (sizeof (struct common_block)
16599 + (n_entries - 1) * sizeof (struct symbol *));
224c3ddb
SM
16600 common_block
16601 = (struct common_block *) obstack_alloc (&objfile->objfile_obstack,
16602 size);
4357ac6c
TT
16603 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
16604 common_block->n_entries = 0;
16605
16606 for (child_die = die->child;
16607 child_die && child_die->tag;
16608 child_die = sibling_die (child_die))
16609 {
16610 /* Create the symbol in the DW_TAG_common_block block in the current
16611 symbol scope. */
e7c27a73 16612 sym = new_symbol (child_die, NULL, cu);
0971de02
TT
16613 if (sym != NULL)
16614 {
16615 struct attribute *member_loc;
16616
16617 common_block->contents[common_block->n_entries++] = sym;
16618
16619 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
16620 cu);
16621 if (member_loc)
16622 {
16623 /* GDB has handled this for a long time, but it is
16624 not specified by DWARF. It seems to have been
16625 emitted by gfortran at least as recently as:
16626 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
b98664d3 16627 complaint (_("Variable in common block has "
0971de02 16628 "DW_AT_data_member_location "
9d8780f0
SM
16629 "- DIE at %s [in module %s]"),
16630 sect_offset_str (child_die->sect_off),
518817b3 16631 objfile_name (objfile));
0971de02
TT
16632
16633 if (attr_form_is_section_offset (member_loc))
16634 dwarf2_complex_location_expr_complaint ();
16635 else if (attr_form_is_constant (member_loc)
16636 || attr_form_is_block (member_loc))
16637 {
16638 if (attr)
16639 mark_common_block_symbol_computed (sym, die, attr,
16640 member_loc, cu);
16641 }
16642 else
16643 dwarf2_complex_location_expr_complaint ();
16644 }
16645 }
c906108c 16646 }
4357ac6c
TT
16647
16648 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
16649 SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
c906108c
SS
16650 }
16651}
16652
0114d602 16653/* Create a type for a C++ namespace. */
d9fa45fe 16654
0114d602
DJ
16655static struct type *
16656read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
d9fa45fe 16657{
518817b3 16658 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
0114d602 16659 const char *previous_prefix, *name;
9219021c 16660 int is_anonymous;
0114d602
DJ
16661 struct type *type;
16662
16663 /* For extensions, reuse the type of the original namespace. */
16664 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
16665 {
16666 struct die_info *ext_die;
16667 struct dwarf2_cu *ext_cu = cu;
9a619af0 16668
0114d602
DJ
16669 ext_die = dwarf2_extension (die, &ext_cu);
16670 type = read_type_die (ext_die, ext_cu);
9dc481d3
DE
16671
16672 /* EXT_CU may not be the same as CU.
02142a6c 16673 Ensure TYPE is recorded with CU in die_type_hash. */
0114d602
DJ
16674 return set_die_type (die, type, cu);
16675 }
9219021c 16676
e142c38c 16677 name = namespace_name (die, &is_anonymous, cu);
9219021c
DC
16678
16679 /* Now build the name of the current namespace. */
16680
0114d602
DJ
16681 previous_prefix = determine_prefix (die, cu);
16682 if (previous_prefix[0] != '\0')
16683 name = typename_concat (&objfile->objfile_obstack,
f55ee35c 16684 previous_prefix, name, 0, cu);
0114d602
DJ
16685
16686 /* Create the type. */
19f392bc 16687 type = init_type (objfile, TYPE_CODE_NAMESPACE, 0, name);
0114d602 16688
60531b24 16689 return set_die_type (die, type, cu);
0114d602
DJ
16690}
16691
22cee43f 16692/* Read a namespace scope. */
0114d602
DJ
16693
16694static void
16695read_namespace (struct die_info *die, struct dwarf2_cu *cu)
16696{
518817b3 16697 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
0114d602 16698 int is_anonymous;
9219021c 16699
5c4e30ca
DC
16700 /* Add a symbol associated to this if we haven't seen the namespace
16701 before. Also, add a using directive if it's an anonymous
16702 namespace. */
9219021c 16703
f2f0e013 16704 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
5c4e30ca
DC
16705 {
16706 struct type *type;
16707
0114d602 16708 type = read_type_die (die, cu);
e7c27a73 16709 new_symbol (die, type, cu);
5c4e30ca 16710
e8e80198 16711 namespace_name (die, &is_anonymous, cu);
5c4e30ca 16712 if (is_anonymous)
0114d602
DJ
16713 {
16714 const char *previous_prefix = determine_prefix (die, cu);
9a619af0 16715
eb1e02fd 16716 std::vector<const char *> excludes;
804d2729 16717 add_using_directive (using_directives (cu),
22cee43f 16718 previous_prefix, TYPE_NAME (type), NULL,
eb1e02fd 16719 NULL, excludes, 0, &objfile->objfile_obstack);
0114d602 16720 }
5c4e30ca 16721 }
9219021c 16722
639d11d3 16723 if (die->child != NULL)
d9fa45fe 16724 {
639d11d3 16725 struct die_info *child_die = die->child;
6e70227d 16726
d9fa45fe
DC
16727 while (child_die && child_die->tag)
16728 {
e7c27a73 16729 process_die (child_die, cu);
d9fa45fe
DC
16730 child_die = sibling_die (child_die);
16731 }
16732 }
38d518c9
EZ
16733}
16734
f55ee35c
JK
16735/* Read a Fortran module as type. This DIE can be only a declaration used for
16736 imported module. Still we need that type as local Fortran "use ... only"
16737 declaration imports depend on the created type in determine_prefix. */
16738
16739static struct type *
16740read_module_type (struct die_info *die, struct dwarf2_cu *cu)
16741{
518817b3 16742 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15d034d0 16743 const char *module_name;
f55ee35c
JK
16744 struct type *type;
16745
16746 module_name = dwarf2_name (die, cu);
16747 if (!module_name)
b98664d3 16748 complaint (_("DW_TAG_module has no name, offset %s"),
9d8780f0 16749 sect_offset_str (die->sect_off));
19f392bc 16750 type = init_type (objfile, TYPE_CODE_MODULE, 0, module_name);
f55ee35c 16751
f55ee35c
JK
16752 return set_die_type (die, type, cu);
16753}
16754
5d7cb8df
JK
16755/* Read a Fortran module. */
16756
16757static void
16758read_module (struct die_info *die, struct dwarf2_cu *cu)
16759{
16760 struct die_info *child_die = die->child;
530e8392
KB
16761 struct type *type;
16762
16763 type = read_type_die (die, cu);
16764 new_symbol (die, type, cu);
5d7cb8df 16765
5d7cb8df
JK
16766 while (child_die && child_die->tag)
16767 {
16768 process_die (child_die, cu);
16769 child_die = sibling_die (child_die);
16770 }
16771}
16772
38d518c9
EZ
16773/* Return the name of the namespace represented by DIE. Set
16774 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
16775 namespace. */
16776
16777static const char *
e142c38c 16778namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
38d518c9
EZ
16779{
16780 struct die_info *current_die;
16781 const char *name = NULL;
16782
16783 /* Loop through the extensions until we find a name. */
16784
16785 for (current_die = die;
16786 current_die != NULL;
f2f0e013 16787 current_die = dwarf2_extension (die, &cu))
38d518c9 16788 {
96553a0c
DE
16789 /* We don't use dwarf2_name here so that we can detect the absence
16790 of a name -> anonymous namespace. */
7d45c7c3 16791 name = dwarf2_string_attr (die, DW_AT_name, cu);
96553a0c 16792
38d518c9
EZ
16793 if (name != NULL)
16794 break;
16795 }
16796
16797 /* Is it an anonymous namespace? */
16798
16799 *is_anonymous = (name == NULL);
16800 if (*is_anonymous)
2b1dbab0 16801 name = CP_ANONYMOUS_NAMESPACE_STR;
38d518c9
EZ
16802
16803 return name;
d9fa45fe
DC
16804}
16805
c906108c
SS
16806/* Extract all information from a DW_TAG_pointer_type DIE and add to
16807 the user defined type vector. */
16808
f792889a 16809static struct type *
e7c27a73 16810read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16811{
518817b3
SM
16812 struct gdbarch *gdbarch
16813 = get_objfile_arch (cu->per_cu->dwarf2_per_objfile->objfile);
e7c27a73 16814 struct comp_unit_head *cu_header = &cu->header;
c906108c 16815 struct type *type;
8b2dbe47
KB
16816 struct attribute *attr_byte_size;
16817 struct attribute *attr_address_class;
16818 int byte_size, addr_class;
7e314c57
JK
16819 struct type *target_type;
16820
16821 target_type = die_type (die, cu);
c906108c 16822
7e314c57
JK
16823 /* The die_type call above may have already set the type for this DIE. */
16824 type = get_die_type (die, cu);
16825 if (type)
16826 return type;
16827
16828 type = lookup_pointer_type (target_type);
8b2dbe47 16829
e142c38c 16830 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
8b2dbe47
KB
16831 if (attr_byte_size)
16832 byte_size = DW_UNSND (attr_byte_size);
c906108c 16833 else
8b2dbe47
KB
16834 byte_size = cu_header->addr_size;
16835
e142c38c 16836 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
8b2dbe47
KB
16837 if (attr_address_class)
16838 addr_class = DW_UNSND (attr_address_class);
16839 else
16840 addr_class = DW_ADDR_none;
16841
2b4424c3
TT
16842 ULONGEST alignment = get_alignment (cu, die);
16843
16844 /* If the pointer size, alignment, or address class is different
16845 than the default, create a type variant marked as such and set
16846 the length accordingly. */
16847 if (TYPE_LENGTH (type) != byte_size
16848 || (alignment != 0 && TYPE_RAW_ALIGN (type) != 0
16849 && alignment != TYPE_RAW_ALIGN (type))
16850 || addr_class != DW_ADDR_none)
c906108c 16851 {
5e2b427d 16852 if (gdbarch_address_class_type_flags_p (gdbarch))
8b2dbe47
KB
16853 {
16854 int type_flags;
16855
849957d9 16856 type_flags = gdbarch_address_class_type_flags
5e2b427d 16857 (gdbarch, byte_size, addr_class);
876cecd0
TT
16858 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
16859 == 0);
8b2dbe47
KB
16860 type = make_type_with_address_space (type, type_flags);
16861 }
16862 else if (TYPE_LENGTH (type) != byte_size)
16863 {
b98664d3 16864 complaint (_("invalid pointer size %d"), byte_size);
8b2dbe47 16865 }
2b4424c3
TT
16866 else if (TYPE_RAW_ALIGN (type) != alignment)
16867 {
b98664d3 16868 complaint (_("Invalid DW_AT_alignment"
2b4424c3
TT
16869 " - DIE at %s [in module %s]"),
16870 sect_offset_str (die->sect_off),
16871 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
16872 }
6e70227d 16873 else
9a619af0
MS
16874 {
16875 /* Should we also complain about unhandled address classes? */
16876 }
c906108c 16877 }
8b2dbe47
KB
16878
16879 TYPE_LENGTH (type) = byte_size;
2b4424c3 16880 set_type_align (type, alignment);
f792889a 16881 return set_die_type (die, type, cu);
c906108c
SS
16882}
16883
16884/* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
16885 the user defined type vector. */
16886
f792889a 16887static struct type *
e7c27a73 16888read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
16889{
16890 struct type *type;
16891 struct type *to_type;
16892 struct type *domain;
16893
e7c27a73
DJ
16894 to_type = die_type (die, cu);
16895 domain = die_containing_type (die, cu);
0d5de010 16896
7e314c57
JK
16897 /* The calls above may have already set the type for this DIE. */
16898 type = get_die_type (die, cu);
16899 if (type)
16900 return type;
16901
0d5de010
DJ
16902 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
16903 type = lookup_methodptr_type (to_type);
7078baeb
TT
16904 else if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_FUNC)
16905 {
518817b3
SM
16906 struct type *new_type
16907 = alloc_type (cu->per_cu->dwarf2_per_objfile->objfile);
7078baeb
TT
16908
16909 smash_to_method_type (new_type, domain, TYPE_TARGET_TYPE (to_type),
16910 TYPE_FIELDS (to_type), TYPE_NFIELDS (to_type),
16911 TYPE_VARARGS (to_type));
16912 type = lookup_methodptr_type (new_type);
16913 }
0d5de010
DJ
16914 else
16915 type = lookup_memberptr_type (to_type, domain);
c906108c 16916
f792889a 16917 return set_die_type (die, type, cu);
c906108c
SS
16918}
16919
4297a3f0 16920/* Extract all information from a DW_TAG_{rvalue_,}reference_type DIE and add to
c906108c
SS
16921 the user defined type vector. */
16922
f792889a 16923static struct type *
4297a3f0
AV
16924read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu,
16925 enum type_code refcode)
c906108c 16926{
e7c27a73 16927 struct comp_unit_head *cu_header = &cu->header;
7e314c57 16928 struct type *type, *target_type;
c906108c
SS
16929 struct attribute *attr;
16930
4297a3f0
AV
16931 gdb_assert (refcode == TYPE_CODE_REF || refcode == TYPE_CODE_RVALUE_REF);
16932
7e314c57
JK
16933 target_type = die_type (die, cu);
16934
16935 /* The die_type call above may have already set the type for this DIE. */
16936 type = get_die_type (die, cu);
16937 if (type)
16938 return type;
16939
4297a3f0 16940 type = lookup_reference_type (target_type, refcode);
e142c38c 16941 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
16942 if (attr)
16943 {
16944 TYPE_LENGTH (type) = DW_UNSND (attr);
16945 }
16946 else
16947 {
107d2387 16948 TYPE_LENGTH (type) = cu_header->addr_size;
c906108c 16949 }
2b4424c3 16950 maybe_set_alignment (cu, die, type);
f792889a 16951 return set_die_type (die, type, cu);
c906108c
SS
16952}
16953
cf363f18
MW
16954/* Add the given cv-qualifiers to the element type of the array. GCC
16955 outputs DWARF type qualifiers that apply to an array, not the
16956 element type. But GDB relies on the array element type to carry
16957 the cv-qualifiers. This mimics section 6.7.3 of the C99
16958 specification. */
16959
16960static struct type *
16961add_array_cv_type (struct die_info *die, struct dwarf2_cu *cu,
16962 struct type *base_type, int cnst, int voltl)
16963{
16964 struct type *el_type, *inner_array;
16965
16966 base_type = copy_type (base_type);
16967 inner_array = base_type;
16968
16969 while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
16970 {
16971 TYPE_TARGET_TYPE (inner_array) =
16972 copy_type (TYPE_TARGET_TYPE (inner_array));
16973 inner_array = TYPE_TARGET_TYPE (inner_array);
16974 }
16975
16976 el_type = TYPE_TARGET_TYPE (inner_array);
16977 cnst |= TYPE_CONST (el_type);
16978 voltl |= TYPE_VOLATILE (el_type);
16979 TYPE_TARGET_TYPE (inner_array) = make_cv_type (cnst, voltl, el_type, NULL);
16980
16981 return set_die_type (die, base_type, cu);
16982}
16983
f792889a 16984static struct type *
e7c27a73 16985read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16986{
f792889a 16987 struct type *base_type, *cv_type;
c906108c 16988
e7c27a73 16989 base_type = die_type (die, cu);
7e314c57
JK
16990
16991 /* The die_type call above may have already set the type for this DIE. */
16992 cv_type = get_die_type (die, cu);
16993 if (cv_type)
16994 return cv_type;
16995
2f608a3a
KW
16996 /* In case the const qualifier is applied to an array type, the element type
16997 is so qualified, not the array type (section 6.7.3 of C99). */
16998 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
cf363f18 16999 return add_array_cv_type (die, cu, base_type, 1, 0);
2f608a3a 17000
f792889a
DJ
17001 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
17002 return set_die_type (die, cv_type, cu);
c906108c
SS
17003}
17004
f792889a 17005static struct type *
e7c27a73 17006read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17007{
f792889a 17008 struct type *base_type, *cv_type;
c906108c 17009
e7c27a73 17010 base_type = die_type (die, cu);
7e314c57
JK
17011
17012 /* The die_type call above may have already set the type for this DIE. */
17013 cv_type = get_die_type (die, cu);
17014 if (cv_type)
17015 return cv_type;
17016
cf363f18
MW
17017 /* In case the volatile qualifier is applied to an array type, the
17018 element type is so qualified, not the array type (section 6.7.3
17019 of C99). */
17020 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
17021 return add_array_cv_type (die, cu, base_type, 0, 1);
17022
f792889a
DJ
17023 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
17024 return set_die_type (die, cv_type, cu);
c906108c
SS
17025}
17026
06d66ee9
TT
17027/* Handle DW_TAG_restrict_type. */
17028
17029static struct type *
17030read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
17031{
17032 struct type *base_type, *cv_type;
17033
17034 base_type = die_type (die, cu);
17035
17036 /* The die_type call above may have already set the type for this DIE. */
17037 cv_type = get_die_type (die, cu);
17038 if (cv_type)
17039 return cv_type;
17040
17041 cv_type = make_restrict_type (base_type);
17042 return set_die_type (die, cv_type, cu);
17043}
17044
a2c2acaf
MW
17045/* Handle DW_TAG_atomic_type. */
17046
17047static struct type *
17048read_tag_atomic_type (struct die_info *die, struct dwarf2_cu *cu)
17049{
17050 struct type *base_type, *cv_type;
17051
17052 base_type = die_type (die, cu);
17053
17054 /* The die_type call above may have already set the type for this DIE. */
17055 cv_type = get_die_type (die, cu);
17056 if (cv_type)
17057 return cv_type;
17058
17059 cv_type = make_atomic_type (base_type);
17060 return set_die_type (die, cv_type, cu);
17061}
17062
c906108c
SS
17063/* Extract all information from a DW_TAG_string_type DIE and add to
17064 the user defined type vector. It isn't really a user defined type,
17065 but it behaves like one, with other DIE's using an AT_user_def_type
17066 attribute to reference it. */
17067
f792889a 17068static struct type *
e7c27a73 17069read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17070{
518817b3 17071 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
3b7538c0 17072 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
17073 struct type *type, *range_type, *index_type, *char_type;
17074 struct attribute *attr;
17075 unsigned int length;
17076
e142c38c 17077 attr = dwarf2_attr (die, DW_AT_string_length, cu);
c906108c
SS
17078 if (attr)
17079 {
17080 length = DW_UNSND (attr);
17081 }
17082 else
17083 {
0963b4bd 17084 /* Check for the DW_AT_byte_size attribute. */
e142c38c 17085 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
b21b22e0
PS
17086 if (attr)
17087 {
17088 length = DW_UNSND (attr);
17089 }
17090 else
17091 {
17092 length = 1;
17093 }
c906108c 17094 }
6ccb9162 17095
46bf5051 17096 index_type = objfile_type (objfile)->builtin_int;
0c9c3474 17097 range_type = create_static_range_type (NULL, index_type, 1, length);
3b7538c0
UW
17098 char_type = language_string_char_type (cu->language_defn, gdbarch);
17099 type = create_string_type (NULL, char_type, range_type);
6ccb9162 17100
f792889a 17101 return set_die_type (die, type, cu);
c906108c
SS
17102}
17103
4d804846
JB
17104/* Assuming that DIE corresponds to a function, returns nonzero
17105 if the function is prototyped. */
17106
17107static int
17108prototyped_function_p (struct die_info *die, struct dwarf2_cu *cu)
17109{
17110 struct attribute *attr;
17111
17112 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
17113 if (attr && (DW_UNSND (attr) != 0))
17114 return 1;
17115
17116 /* The DWARF standard implies that the DW_AT_prototyped attribute
17117 is only meaninful for C, but the concept also extends to other
17118 languages that allow unprototyped functions (Eg: Objective C).
17119 For all other languages, assume that functions are always
17120 prototyped. */
17121 if (cu->language != language_c
17122 && cu->language != language_objc
17123 && cu->language != language_opencl)
17124 return 1;
17125
17126 /* RealView does not emit DW_AT_prototyped. We can not distinguish
17127 prototyped and unprototyped functions; default to prototyped,
17128 since that is more common in modern code (and RealView warns
17129 about unprototyped functions). */
17130 if (producer_is_realview (cu->producer))
17131 return 1;
17132
17133 return 0;
17134}
17135
c906108c
SS
17136/* Handle DIES due to C code like:
17137
17138 struct foo
c5aa993b
JM
17139 {
17140 int (*funcp)(int a, long l);
17141 int b;
17142 };
c906108c 17143
0963b4bd 17144 ('funcp' generates a DW_TAG_subroutine_type DIE). */
c906108c 17145
f792889a 17146static struct type *
e7c27a73 17147read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17148{
518817b3 17149 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
0963b4bd
MS
17150 struct type *type; /* Type that this function returns. */
17151 struct type *ftype; /* Function that returns above type. */
c906108c
SS
17152 struct attribute *attr;
17153
e7c27a73 17154 type = die_type (die, cu);
7e314c57
JK
17155
17156 /* The die_type call above may have already set the type for this DIE. */
17157 ftype = get_die_type (die, cu);
17158 if (ftype)
17159 return ftype;
17160
0c8b41f1 17161 ftype = lookup_function_type (type);
c906108c 17162
4d804846 17163 if (prototyped_function_p (die, cu))
a6c727b2 17164 TYPE_PROTOTYPED (ftype) = 1;
c906108c 17165
c055b101
CV
17166 /* Store the calling convention in the type if it's available in
17167 the subroutine die. Otherwise set the calling convention to
17168 the default value DW_CC_normal. */
17169 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
54fcddd0
UW
17170 if (attr)
17171 TYPE_CALLING_CONVENTION (ftype) = DW_UNSND (attr);
17172 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
17173 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
17174 else
17175 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
76c10ea2 17176
743649fd
MW
17177 /* Record whether the function returns normally to its caller or not
17178 if the DWARF producer set that information. */
17179 attr = dwarf2_attr (die, DW_AT_noreturn, cu);
17180 if (attr && (DW_UNSND (attr) != 0))
17181 TYPE_NO_RETURN (ftype) = 1;
17182
76c10ea2
GM
17183 /* We need to add the subroutine type to the die immediately so
17184 we don't infinitely recurse when dealing with parameters
0963b4bd 17185 declared as the same subroutine type. */
76c10ea2 17186 set_die_type (die, ftype, cu);
6e70227d 17187
639d11d3 17188 if (die->child != NULL)
c906108c 17189 {
bb5ed363 17190 struct type *void_type = objfile_type (objfile)->builtin_void;
c906108c 17191 struct die_info *child_die;
8072405b 17192 int nparams, iparams;
c906108c
SS
17193
17194 /* Count the number of parameters.
17195 FIXME: GDB currently ignores vararg functions, but knows about
17196 vararg member functions. */
8072405b 17197 nparams = 0;
639d11d3 17198 child_die = die->child;
c906108c
SS
17199 while (child_die && child_die->tag)
17200 {
17201 if (child_die->tag == DW_TAG_formal_parameter)
17202 nparams++;
17203 else if (child_die->tag == DW_TAG_unspecified_parameters)
876cecd0 17204 TYPE_VARARGS (ftype) = 1;
c906108c
SS
17205 child_die = sibling_die (child_die);
17206 }
17207
17208 /* Allocate storage for parameters and fill them in. */
17209 TYPE_NFIELDS (ftype) = nparams;
17210 TYPE_FIELDS (ftype) = (struct field *)
ae5a43e0 17211 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
c906108c 17212
8072405b
JK
17213 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
17214 even if we error out during the parameters reading below. */
17215 for (iparams = 0; iparams < nparams; iparams++)
17216 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
17217
17218 iparams = 0;
639d11d3 17219 child_die = die->child;
c906108c
SS
17220 while (child_die && child_die->tag)
17221 {
17222 if (child_die->tag == DW_TAG_formal_parameter)
17223 {
3ce3b1ba
PA
17224 struct type *arg_type;
17225
17226 /* DWARF version 2 has no clean way to discern C++
17227 static and non-static member functions. G++ helps
17228 GDB by marking the first parameter for non-static
17229 member functions (which is the this pointer) as
17230 artificial. We pass this information to
17231 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
17232
17233 DWARF version 3 added DW_AT_object_pointer, which GCC
17234 4.5 does not yet generate. */
e142c38c 17235 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
c906108c
SS
17236 if (attr)
17237 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
17238 else
9c37b5ae 17239 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
3ce3b1ba
PA
17240 arg_type = die_type (child_die, cu);
17241
17242 /* RealView does not mark THIS as const, which the testsuite
17243 expects. GCC marks THIS as const in method definitions,
17244 but not in the class specifications (GCC PR 43053). */
17245 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
17246 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
17247 {
17248 int is_this = 0;
17249 struct dwarf2_cu *arg_cu = cu;
17250 const char *name = dwarf2_name (child_die, cu);
17251
17252 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
17253 if (attr)
17254 {
17255 /* If the compiler emits this, use it. */
17256 if (follow_die_ref (die, attr, &arg_cu) == child_die)
17257 is_this = 1;
17258 }
17259 else if (name && strcmp (name, "this") == 0)
17260 /* Function definitions will have the argument names. */
17261 is_this = 1;
17262 else if (name == NULL && iparams == 0)
17263 /* Declarations may not have the names, so like
17264 elsewhere in GDB, assume an artificial first
17265 argument is "this". */
17266 is_this = 1;
17267
17268 if (is_this)
17269 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
17270 arg_type, 0);
17271 }
17272
17273 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
c906108c
SS
17274 iparams++;
17275 }
17276 child_die = sibling_die (child_die);
17277 }
17278 }
17279
76c10ea2 17280 return ftype;
c906108c
SS
17281}
17282
f792889a 17283static struct type *
e7c27a73 17284read_typedef (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17285{
518817b3 17286 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
0114d602 17287 const char *name = NULL;
3c8e0968 17288 struct type *this_type, *target_type;
c906108c 17289
94af9270 17290 name = dwarf2_full_name (NULL, die, cu);
19f392bc
UW
17291 this_type = init_type (objfile, TYPE_CODE_TYPEDEF, 0, name);
17292 TYPE_TARGET_STUB (this_type) = 1;
f792889a 17293 set_die_type (die, this_type, cu);
3c8e0968
DE
17294 target_type = die_type (die, cu);
17295 if (target_type != this_type)
17296 TYPE_TARGET_TYPE (this_type) = target_type;
17297 else
17298 {
17299 /* Self-referential typedefs are, it seems, not allowed by the DWARF
17300 spec and cause infinite loops in GDB. */
b98664d3 17301 complaint (_("Self-referential DW_TAG_typedef "
9d8780f0
SM
17302 "- DIE at %s [in module %s]"),
17303 sect_offset_str (die->sect_off), objfile_name (objfile));
3c8e0968
DE
17304 TYPE_TARGET_TYPE (this_type) = NULL;
17305 }
f792889a 17306 return this_type;
c906108c
SS
17307}
17308
9b790ce7
UW
17309/* Allocate a floating-point type of size BITS and name NAME. Pass NAME_HINT
17310 (which may be different from NAME) to the architecture back-end to allow
17311 it to guess the correct format if necessary. */
17312
17313static struct type *
17314dwarf2_init_float_type (struct objfile *objfile, int bits, const char *name,
17315 const char *name_hint)
17316{
17317 struct gdbarch *gdbarch = get_objfile_arch (objfile);
17318 const struct floatformat **format;
17319 struct type *type;
17320
17321 format = gdbarch_floatformat_for_type (gdbarch, name_hint, bits);
17322 if (format)
17323 type = init_float_type (objfile, bits, name, format);
17324 else
77b7c781 17325 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
9b790ce7
UW
17326
17327 return type;
17328}
17329
c906108c
SS
17330/* Find a representation of a given base type and install
17331 it in the TYPE field of the die. */
17332
f792889a 17333static struct type *
e7c27a73 17334read_base_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17335{
518817b3 17336 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c
SS
17337 struct type *type;
17338 struct attribute *attr;
19f392bc 17339 int encoding = 0, bits = 0;
15d034d0 17340 const char *name;
c906108c 17341
e142c38c 17342 attr = dwarf2_attr (die, DW_AT_encoding, cu);
c906108c
SS
17343 if (attr)
17344 {
17345 encoding = DW_UNSND (attr);
17346 }
e142c38c 17347 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
17348 if (attr)
17349 {
19f392bc 17350 bits = DW_UNSND (attr) * TARGET_CHAR_BIT;
c906108c 17351 }
39cbfefa 17352 name = dwarf2_name (die, cu);
6ccb9162 17353 if (!name)
c906108c 17354 {
b98664d3 17355 complaint (_("DW_AT_name missing from DW_TAG_base_type"));
c906108c 17356 }
6ccb9162
UW
17357
17358 switch (encoding)
c906108c 17359 {
6ccb9162
UW
17360 case DW_ATE_address:
17361 /* Turn DW_ATE_address into a void * pointer. */
77b7c781 17362 type = init_type (objfile, TYPE_CODE_VOID, TARGET_CHAR_BIT, NULL);
19f392bc 17363 type = init_pointer_type (objfile, bits, name, type);
6ccb9162
UW
17364 break;
17365 case DW_ATE_boolean:
19f392bc 17366 type = init_boolean_type (objfile, bits, 1, name);
6ccb9162
UW
17367 break;
17368 case DW_ATE_complex_float:
9b790ce7 17369 type = dwarf2_init_float_type (objfile, bits / 2, NULL, name);
19f392bc 17370 type = init_complex_type (objfile, name, type);
6ccb9162
UW
17371 break;
17372 case DW_ATE_decimal_float:
19f392bc 17373 type = init_decfloat_type (objfile, bits, name);
6ccb9162
UW
17374 break;
17375 case DW_ATE_float:
9b790ce7 17376 type = dwarf2_init_float_type (objfile, bits, name, name);
6ccb9162
UW
17377 break;
17378 case DW_ATE_signed:
19f392bc 17379 type = init_integer_type (objfile, bits, 0, name);
6ccb9162
UW
17380 break;
17381 case DW_ATE_unsigned:
3b2b8fea
TT
17382 if (cu->language == language_fortran
17383 && name
61012eef 17384 && startswith (name, "character("))
19f392bc
UW
17385 type = init_character_type (objfile, bits, 1, name);
17386 else
17387 type = init_integer_type (objfile, bits, 1, name);
6ccb9162
UW
17388 break;
17389 case DW_ATE_signed_char:
6e70227d 17390 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea
TT
17391 || cu->language == language_pascal
17392 || cu->language == language_fortran)
19f392bc
UW
17393 type = init_character_type (objfile, bits, 0, name);
17394 else
17395 type = init_integer_type (objfile, bits, 0, name);
6ccb9162
UW
17396 break;
17397 case DW_ATE_unsigned_char:
868a0084 17398 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea 17399 || cu->language == language_pascal
c44af4eb
TT
17400 || cu->language == language_fortran
17401 || cu->language == language_rust)
19f392bc
UW
17402 type = init_character_type (objfile, bits, 1, name);
17403 else
17404 type = init_integer_type (objfile, bits, 1, name);
6ccb9162 17405 break;
75079b2b 17406 case DW_ATE_UTF:
53e710ac
PA
17407 {
17408 gdbarch *arch = get_objfile_arch (objfile);
17409
17410 if (bits == 16)
17411 type = builtin_type (arch)->builtin_char16;
17412 else if (bits == 32)
17413 type = builtin_type (arch)->builtin_char32;
17414 else
17415 {
b98664d3 17416 complaint (_("unsupported DW_ATE_UTF bit size: '%d'"),
53e710ac
PA
17417 bits);
17418 type = init_integer_type (objfile, bits, 1, name);
17419 }
17420 return set_die_type (die, type, cu);
17421 }
75079b2b
TT
17422 break;
17423
6ccb9162 17424 default:
b98664d3 17425 complaint (_("unsupported DW_AT_encoding: '%s'"),
6ccb9162 17426 dwarf_type_encoding_name (encoding));
77b7c781 17427 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
6ccb9162 17428 break;
c906108c 17429 }
6ccb9162 17430
0114d602 17431 if (name && strcmp (name, "char") == 0)
876cecd0 17432 TYPE_NOSIGN (type) = 1;
0114d602 17433
2b4424c3
TT
17434 maybe_set_alignment (cu, die, type);
17435
f792889a 17436 return set_die_type (die, type, cu);
c906108c
SS
17437}
17438
80180f79
SA
17439/* Parse dwarf attribute if it's a block, reference or constant and put the
17440 resulting value of the attribute into struct bound_prop.
17441 Returns 1 if ATTR could be resolved into PROP, 0 otherwise. */
17442
17443static int
17444attr_to_dynamic_prop (const struct attribute *attr, struct die_info *die,
17445 struct dwarf2_cu *cu, struct dynamic_prop *prop)
17446{
17447 struct dwarf2_property_baton *baton;
518817b3
SM
17448 struct obstack *obstack
17449 = &cu->per_cu->dwarf2_per_objfile->objfile->objfile_obstack;
80180f79
SA
17450
17451 if (attr == NULL || prop == NULL)
17452 return 0;
17453
17454 if (attr_form_is_block (attr))
17455 {
8d749320 17456 baton = XOBNEW (obstack, struct dwarf2_property_baton);
80180f79
SA
17457 baton->referenced_type = NULL;
17458 baton->locexpr.per_cu = cu->per_cu;
17459 baton->locexpr.size = DW_BLOCK (attr)->size;
17460 baton->locexpr.data = DW_BLOCK (attr)->data;
17461 prop->data.baton = baton;
17462 prop->kind = PROP_LOCEXPR;
17463 gdb_assert (prop->data.baton != NULL);
17464 }
17465 else if (attr_form_is_ref (attr))
17466 {
17467 struct dwarf2_cu *target_cu = cu;
17468 struct die_info *target_die;
17469 struct attribute *target_attr;
17470
17471 target_die = follow_die_ref (die, attr, &target_cu);
17472 target_attr = dwarf2_attr (target_die, DW_AT_location, target_cu);
df25ebbd
JB
17473 if (target_attr == NULL)
17474 target_attr = dwarf2_attr (target_die, DW_AT_data_member_location,
17475 target_cu);
80180f79
SA
17476 if (target_attr == NULL)
17477 return 0;
17478
df25ebbd 17479 switch (target_attr->name)
80180f79 17480 {
df25ebbd
JB
17481 case DW_AT_location:
17482 if (attr_form_is_section_offset (target_attr))
17483 {
8d749320 17484 baton = XOBNEW (obstack, struct dwarf2_property_baton);
df25ebbd
JB
17485 baton->referenced_type = die_type (target_die, target_cu);
17486 fill_in_loclist_baton (cu, &baton->loclist, target_attr);
17487 prop->data.baton = baton;
17488 prop->kind = PROP_LOCLIST;
17489 gdb_assert (prop->data.baton != NULL);
17490 }
17491 else if (attr_form_is_block (target_attr))
17492 {
8d749320 17493 baton = XOBNEW (obstack, struct dwarf2_property_baton);
df25ebbd
JB
17494 baton->referenced_type = die_type (target_die, target_cu);
17495 baton->locexpr.per_cu = cu->per_cu;
17496 baton->locexpr.size = DW_BLOCK (target_attr)->size;
17497 baton->locexpr.data = DW_BLOCK (target_attr)->data;
17498 prop->data.baton = baton;
17499 prop->kind = PROP_LOCEXPR;
17500 gdb_assert (prop->data.baton != NULL);
17501 }
17502 else
17503 {
17504 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
17505 "dynamic property");
17506 return 0;
17507 }
17508 break;
17509 case DW_AT_data_member_location:
17510 {
17511 LONGEST offset;
17512
17513 if (!handle_data_member_location (target_die, target_cu,
17514 &offset))
17515 return 0;
17516
8d749320 17517 baton = XOBNEW (obstack, struct dwarf2_property_baton);
6ad395a7
JB
17518 baton->referenced_type = read_type_die (target_die->parent,
17519 target_cu);
df25ebbd
JB
17520 baton->offset_info.offset = offset;
17521 baton->offset_info.type = die_type (target_die, target_cu);
17522 prop->data.baton = baton;
17523 prop->kind = PROP_ADDR_OFFSET;
17524 break;
17525 }
80180f79
SA
17526 }
17527 }
17528 else if (attr_form_is_constant (attr))
17529 {
17530 prop->data.const_val = dwarf2_get_attr_constant_value (attr, 0);
17531 prop->kind = PROP_CONST;
17532 }
17533 else
17534 {
17535 dwarf2_invalid_attrib_class_complaint (dwarf_form_name (attr->form),
17536 dwarf2_name (die, cu));
17537 return 0;
17538 }
17539
17540 return 1;
17541}
17542
a02abb62
JB
17543/* Read the given DW_AT_subrange DIE. */
17544
f792889a 17545static struct type *
a02abb62
JB
17546read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
17547{
4c9ad8c2 17548 struct type *base_type, *orig_base_type;
a02abb62
JB
17549 struct type *range_type;
17550 struct attribute *attr;
729efb13 17551 struct dynamic_prop low, high;
4fae6e18 17552 int low_default_is_valid;
c451ebe5 17553 int high_bound_is_count = 0;
15d034d0 17554 const char *name;
43bbcdc2 17555 LONGEST negative_mask;
e77813c8 17556
4c9ad8c2
TT
17557 orig_base_type = die_type (die, cu);
17558 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
17559 whereas the real type might be. So, we use ORIG_BASE_TYPE when
17560 creating the range type, but we use the result of check_typedef
17561 when examining properties of the type. */
17562 base_type = check_typedef (orig_base_type);
a02abb62 17563
7e314c57
JK
17564 /* The die_type call above may have already set the type for this DIE. */
17565 range_type = get_die_type (die, cu);
17566 if (range_type)
17567 return range_type;
17568
729efb13
SA
17569 low.kind = PROP_CONST;
17570 high.kind = PROP_CONST;
17571 high.data.const_val = 0;
17572
4fae6e18
JK
17573 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
17574 omitting DW_AT_lower_bound. */
17575 switch (cu->language)
6e70227d 17576 {
4fae6e18
JK
17577 case language_c:
17578 case language_cplus:
729efb13 17579 low.data.const_val = 0;
4fae6e18
JK
17580 low_default_is_valid = 1;
17581 break;
17582 case language_fortran:
729efb13 17583 low.data.const_val = 1;
4fae6e18
JK
17584 low_default_is_valid = 1;
17585 break;
17586 case language_d:
4fae6e18 17587 case language_objc:
c44af4eb 17588 case language_rust:
729efb13 17589 low.data.const_val = 0;
4fae6e18
JK
17590 low_default_is_valid = (cu->header.version >= 4);
17591 break;
17592 case language_ada:
17593 case language_m2:
17594 case language_pascal:
729efb13 17595 low.data.const_val = 1;
4fae6e18
JK
17596 low_default_is_valid = (cu->header.version >= 4);
17597 break;
17598 default:
729efb13 17599 low.data.const_val = 0;
4fae6e18
JK
17600 low_default_is_valid = 0;
17601 break;
a02abb62
JB
17602 }
17603
e142c38c 17604 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
a02abb62 17605 if (attr)
11c1ba78 17606 attr_to_dynamic_prop (attr, die, cu, &low);
4fae6e18 17607 else if (!low_default_is_valid)
b98664d3 17608 complaint (_("Missing DW_AT_lower_bound "
9d8780f0
SM
17609 "- DIE at %s [in module %s]"),
17610 sect_offset_str (die->sect_off),
518817b3 17611 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
a02abb62 17612
e142c38c 17613 attr = dwarf2_attr (die, DW_AT_upper_bound, cu);
80180f79 17614 if (!attr_to_dynamic_prop (attr, die, cu, &high))
e77813c8
PM
17615 {
17616 attr = dwarf2_attr (die, DW_AT_count, cu);
c451ebe5 17617 if (attr_to_dynamic_prop (attr, die, cu, &high))
6b662e19 17618 {
c451ebe5
SA
17619 /* If bounds are constant do the final calculation here. */
17620 if (low.kind == PROP_CONST && high.kind == PROP_CONST)
17621 high.data.const_val = low.data.const_val + high.data.const_val - 1;
17622 else
17623 high_bound_is_count = 1;
c2ff108b 17624 }
e77813c8
PM
17625 }
17626
17627 /* Dwarf-2 specifications explicitly allows to create subrange types
17628 without specifying a base type.
17629 In that case, the base type must be set to the type of
17630 the lower bound, upper bound or count, in that order, if any of these
17631 three attributes references an object that has a type.
17632 If no base type is found, the Dwarf-2 specifications say that
17633 a signed integer type of size equal to the size of an address should
17634 be used.
17635 For the following C code: `extern char gdb_int [];'
17636 GCC produces an empty range DIE.
17637 FIXME: muller/2010-05-28: Possible references to object for low bound,
0963b4bd 17638 high bound or count are not yet handled by this code. */
e77813c8
PM
17639 if (TYPE_CODE (base_type) == TYPE_CODE_VOID)
17640 {
518817b3 17641 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
e77813c8
PM
17642 struct gdbarch *gdbarch = get_objfile_arch (objfile);
17643 int addr_size = gdbarch_addr_bit (gdbarch) /8;
17644 struct type *int_type = objfile_type (objfile)->builtin_int;
17645
17646 /* Test "int", "long int", and "long long int" objfile types,
17647 and select the first one having a size above or equal to the
17648 architecture address size. */
17649 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
17650 base_type = int_type;
17651 else
17652 {
17653 int_type = objfile_type (objfile)->builtin_long;
17654 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
17655 base_type = int_type;
17656 else
17657 {
17658 int_type = objfile_type (objfile)->builtin_long_long;
17659 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
17660 base_type = int_type;
17661 }
17662 }
17663 }
a02abb62 17664
dbb9c2b1
JB
17665 /* Normally, the DWARF producers are expected to use a signed
17666 constant form (Eg. DW_FORM_sdata) to express negative bounds.
17667 But this is unfortunately not always the case, as witnessed
17668 with GCC, for instance, where the ambiguous DW_FORM_dataN form
17669 is used instead. To work around that ambiguity, we treat
17670 the bounds as signed, and thus sign-extend their values, when
17671 the base type is signed. */
6e70227d 17672 negative_mask =
66c6502d 17673 -((LONGEST) 1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1));
729efb13
SA
17674 if (low.kind == PROP_CONST
17675 && !TYPE_UNSIGNED (base_type) && (low.data.const_val & negative_mask))
17676 low.data.const_val |= negative_mask;
17677 if (high.kind == PROP_CONST
17678 && !TYPE_UNSIGNED (base_type) && (high.data.const_val & negative_mask))
17679 high.data.const_val |= negative_mask;
43bbcdc2 17680
729efb13 17681 range_type = create_range_type (NULL, orig_base_type, &low, &high);
a02abb62 17682
c451ebe5
SA
17683 if (high_bound_is_count)
17684 TYPE_RANGE_DATA (range_type)->flag_upper_bound_is_count = 1;
17685
c2ff108b
JK
17686 /* Ada expects an empty array on no boundary attributes. */
17687 if (attr == NULL && cu->language != language_ada)
729efb13 17688 TYPE_HIGH_BOUND_KIND (range_type) = PROP_UNDEFINED;
c2ff108b 17689
39cbfefa
DJ
17690 name = dwarf2_name (die, cu);
17691 if (name)
17692 TYPE_NAME (range_type) = name;
6e70227d 17693
e142c38c 17694 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
a02abb62
JB
17695 if (attr)
17696 TYPE_LENGTH (range_type) = DW_UNSND (attr);
17697
2b4424c3
TT
17698 maybe_set_alignment (cu, die, range_type);
17699
7e314c57
JK
17700 set_die_type (die, range_type, cu);
17701
17702 /* set_die_type should be already done. */
b4ba55a1
JB
17703 set_descriptive_type (range_type, die, cu);
17704
7e314c57 17705 return range_type;
a02abb62 17706}
6e70227d 17707
f792889a 17708static struct type *
81a17f79
JB
17709read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
17710{
17711 struct type *type;
81a17f79 17712
518817b3
SM
17713 type = init_type (cu->per_cu->dwarf2_per_objfile->objfile, TYPE_CODE_VOID,0,
17714 NULL);
0114d602 17715 TYPE_NAME (type) = dwarf2_name (die, cu);
81a17f79 17716
74a2f8ff
JB
17717 /* In Ada, an unspecified type is typically used when the description
17718 of the type is defered to a different unit. When encountering
17719 such a type, we treat it as a stub, and try to resolve it later on,
17720 when needed. */
17721 if (cu->language == language_ada)
17722 TYPE_STUB (type) = 1;
17723
f792889a 17724 return set_die_type (die, type, cu);
81a17f79 17725}
a02abb62 17726
639d11d3
DC
17727/* Read a single die and all its descendents. Set the die's sibling
17728 field to NULL; set other fields in the die correctly, and set all
17729 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
17730 location of the info_ptr after reading all of those dies. PARENT
17731 is the parent of the die in question. */
17732
17733static struct die_info *
dee91e82 17734read_die_and_children (const struct die_reader_specs *reader,
d521ce57
TT
17735 const gdb_byte *info_ptr,
17736 const gdb_byte **new_info_ptr,
dee91e82 17737 struct die_info *parent)
639d11d3
DC
17738{
17739 struct die_info *die;
d521ce57 17740 const gdb_byte *cur_ptr;
639d11d3
DC
17741 int has_children;
17742
bf6af496 17743 cur_ptr = read_full_die_1 (reader, &die, info_ptr, &has_children, 0);
1d325ec1
DJ
17744 if (die == NULL)
17745 {
17746 *new_info_ptr = cur_ptr;
17747 return NULL;
17748 }
93311388 17749 store_in_ref_table (die, reader->cu);
639d11d3
DC
17750
17751 if (has_children)
bf6af496 17752 die->child = read_die_and_siblings_1 (reader, cur_ptr, new_info_ptr, die);
639d11d3
DC
17753 else
17754 {
17755 die->child = NULL;
17756 *new_info_ptr = cur_ptr;
17757 }
17758
17759 die->sibling = NULL;
17760 die->parent = parent;
17761 return die;
17762}
17763
17764/* Read a die, all of its descendents, and all of its siblings; set
17765 all of the fields of all of the dies correctly. Arguments are as
17766 in read_die_and_children. */
17767
17768static struct die_info *
bf6af496 17769read_die_and_siblings_1 (const struct die_reader_specs *reader,
d521ce57
TT
17770 const gdb_byte *info_ptr,
17771 const gdb_byte **new_info_ptr,
bf6af496 17772 struct die_info *parent)
639d11d3
DC
17773{
17774 struct die_info *first_die, *last_sibling;
d521ce57 17775 const gdb_byte *cur_ptr;
639d11d3 17776
c906108c 17777 cur_ptr = info_ptr;
639d11d3
DC
17778 first_die = last_sibling = NULL;
17779
17780 while (1)
c906108c 17781 {
639d11d3 17782 struct die_info *die
dee91e82 17783 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
639d11d3 17784
1d325ec1 17785 if (die == NULL)
c906108c 17786 {
639d11d3
DC
17787 *new_info_ptr = cur_ptr;
17788 return first_die;
c906108c 17789 }
1d325ec1
DJ
17790
17791 if (!first_die)
17792 first_die = die;
c906108c 17793 else
1d325ec1
DJ
17794 last_sibling->sibling = die;
17795
17796 last_sibling = die;
c906108c 17797 }
c906108c
SS
17798}
17799
bf6af496
DE
17800/* Read a die, all of its descendents, and all of its siblings; set
17801 all of the fields of all of the dies correctly. Arguments are as
17802 in read_die_and_children.
17803 This the main entry point for reading a DIE and all its children. */
17804
17805static struct die_info *
17806read_die_and_siblings (const struct die_reader_specs *reader,
d521ce57
TT
17807 const gdb_byte *info_ptr,
17808 const gdb_byte **new_info_ptr,
bf6af496
DE
17809 struct die_info *parent)
17810{
17811 struct die_info *die = read_die_and_siblings_1 (reader, info_ptr,
17812 new_info_ptr, parent);
17813
b4f54984 17814 if (dwarf_die_debug)
bf6af496
DE
17815 {
17816 fprintf_unfiltered (gdb_stdlog,
17817 "Read die from %s@0x%x of %s:\n",
a32a8923 17818 get_section_name (reader->die_section),
bf6af496
DE
17819 (unsigned) (info_ptr - reader->die_section->buffer),
17820 bfd_get_filename (reader->abfd));
b4f54984 17821 dump_die (die, dwarf_die_debug);
bf6af496
DE
17822 }
17823
17824 return die;
17825}
17826
3019eac3
DE
17827/* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
17828 attributes.
17829 The caller is responsible for filling in the extra attributes
17830 and updating (*DIEP)->num_attrs.
17831 Set DIEP to point to a newly allocated die with its information,
17832 except for its child, sibling, and parent fields.
17833 Set HAS_CHILDREN to tell whether the die has children or not. */
93311388 17834
d521ce57 17835static const gdb_byte *
3019eac3 17836read_full_die_1 (const struct die_reader_specs *reader,
d521ce57 17837 struct die_info **diep, const gdb_byte *info_ptr,
3019eac3 17838 int *has_children, int num_extra_attrs)
93311388 17839{
b64f50a1 17840 unsigned int abbrev_number, bytes_read, i;
93311388
DE
17841 struct abbrev_info *abbrev;
17842 struct die_info *die;
17843 struct dwarf2_cu *cu = reader->cu;
17844 bfd *abfd = reader->abfd;
17845
9c541725 17846 sect_offset sect_off = (sect_offset) (info_ptr - reader->buffer);
93311388
DE
17847 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
17848 info_ptr += bytes_read;
17849 if (!abbrev_number)
17850 {
17851 *diep = NULL;
17852 *has_children = 0;
17853 return info_ptr;
17854 }
17855
685af9cd 17856 abbrev = reader->abbrev_table->lookup_abbrev (abbrev_number);
93311388 17857 if (!abbrev)
348e048f
DE
17858 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
17859 abbrev_number,
17860 bfd_get_filename (abfd));
17861
3019eac3 17862 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
9c541725 17863 die->sect_off = sect_off;
93311388
DE
17864 die->tag = abbrev->tag;
17865 die->abbrev = abbrev_number;
17866
3019eac3
DE
17867 /* Make the result usable.
17868 The caller needs to update num_attrs after adding the extra
17869 attributes. */
93311388
DE
17870 die->num_attrs = abbrev->num_attrs;
17871
17872 for (i = 0; i < abbrev->num_attrs; ++i)
dee91e82
DE
17873 info_ptr = read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
17874 info_ptr);
93311388
DE
17875
17876 *diep = die;
17877 *has_children = abbrev->has_children;
17878 return info_ptr;
17879}
17880
3019eac3
DE
17881/* Read a die and all its attributes.
17882 Set DIEP to point to a newly allocated die with its information,
17883 except for its child, sibling, and parent fields.
17884 Set HAS_CHILDREN to tell whether the die has children or not. */
17885
d521ce57 17886static const gdb_byte *
3019eac3 17887read_full_die (const struct die_reader_specs *reader,
d521ce57 17888 struct die_info **diep, const gdb_byte *info_ptr,
3019eac3
DE
17889 int *has_children)
17890{
d521ce57 17891 const gdb_byte *result;
bf6af496
DE
17892
17893 result = read_full_die_1 (reader, diep, info_ptr, has_children, 0);
17894
b4f54984 17895 if (dwarf_die_debug)
bf6af496
DE
17896 {
17897 fprintf_unfiltered (gdb_stdlog,
17898 "Read die from %s@0x%x of %s:\n",
a32a8923 17899 get_section_name (reader->die_section),
bf6af496
DE
17900 (unsigned) (info_ptr - reader->die_section->buffer),
17901 bfd_get_filename (reader->abfd));
b4f54984 17902 dump_die (*diep, dwarf_die_debug);
bf6af496
DE
17903 }
17904
17905 return result;
3019eac3 17906}
433df2d4
DE
17907\f
17908/* Abbreviation tables.
3019eac3 17909
433df2d4 17910 In DWARF version 2, the description of the debugging information is
c906108c
SS
17911 stored in a separate .debug_abbrev section. Before we read any
17912 dies from a section we read in all abbreviations and install them
433df2d4
DE
17913 in a hash table. */
17914
17915/* Allocate space for a struct abbrev_info object in ABBREV_TABLE. */
17916
685af9cd
TT
17917struct abbrev_info *
17918abbrev_table::alloc_abbrev ()
433df2d4
DE
17919{
17920 struct abbrev_info *abbrev;
17921
685af9cd 17922 abbrev = XOBNEW (&abbrev_obstack, struct abbrev_info);
433df2d4 17923 memset (abbrev, 0, sizeof (struct abbrev_info));
8d749320 17924
433df2d4
DE
17925 return abbrev;
17926}
17927
17928/* Add an abbreviation to the table. */
c906108c 17929
685af9cd
TT
17930void
17931abbrev_table::add_abbrev (unsigned int abbrev_number,
17932 struct abbrev_info *abbrev)
433df2d4
DE
17933{
17934 unsigned int hash_number;
17935
17936 hash_number = abbrev_number % ABBREV_HASH_SIZE;
4a17f768
YQ
17937 abbrev->next = m_abbrevs[hash_number];
17938 m_abbrevs[hash_number] = abbrev;
433df2d4 17939}
dee91e82 17940
433df2d4
DE
17941/* Look up an abbrev in the table.
17942 Returns NULL if the abbrev is not found. */
17943
685af9cd
TT
17944struct abbrev_info *
17945abbrev_table::lookup_abbrev (unsigned int abbrev_number)
c906108c 17946{
433df2d4
DE
17947 unsigned int hash_number;
17948 struct abbrev_info *abbrev;
17949
17950 hash_number = abbrev_number % ABBREV_HASH_SIZE;
4a17f768 17951 abbrev = m_abbrevs[hash_number];
433df2d4
DE
17952
17953 while (abbrev)
17954 {
17955 if (abbrev->number == abbrev_number)
17956 return abbrev;
17957 abbrev = abbrev->next;
17958 }
17959 return NULL;
17960}
17961
17962/* Read in an abbrev table. */
17963
685af9cd 17964static abbrev_table_up
ed2dc618
SM
17965abbrev_table_read_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
17966 struct dwarf2_section_info *section,
9c541725 17967 sect_offset sect_off)
433df2d4
DE
17968{
17969 struct objfile *objfile = dwarf2_per_objfile->objfile;
a32a8923 17970 bfd *abfd = get_section_bfd_owner (section);
d521ce57 17971 const gdb_byte *abbrev_ptr;
c906108c
SS
17972 struct abbrev_info *cur_abbrev;
17973 unsigned int abbrev_number, bytes_read, abbrev_name;
433df2d4 17974 unsigned int abbrev_form;
f3dd6933
DJ
17975 struct attr_abbrev *cur_attrs;
17976 unsigned int allocated_attrs;
c906108c 17977
685af9cd 17978 abbrev_table_up abbrev_table (new struct abbrev_table (sect_off));
c906108c 17979
433df2d4 17980 dwarf2_read_section (objfile, section);
9c541725 17981 abbrev_ptr = section->buffer + to_underlying (sect_off);
c906108c
SS
17982 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
17983 abbrev_ptr += bytes_read;
17984
f3dd6933 17985 allocated_attrs = ATTR_ALLOC_CHUNK;
8d749320 17986 cur_attrs = XNEWVEC (struct attr_abbrev, allocated_attrs);
6e70227d 17987
0963b4bd 17988 /* Loop until we reach an abbrev number of 0. */
c906108c
SS
17989 while (abbrev_number)
17990 {
685af9cd 17991 cur_abbrev = abbrev_table->alloc_abbrev ();
c906108c
SS
17992
17993 /* read in abbrev header */
17994 cur_abbrev->number = abbrev_number;
aead7601
SM
17995 cur_abbrev->tag
17996 = (enum dwarf_tag) read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
c906108c
SS
17997 abbrev_ptr += bytes_read;
17998 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
17999 abbrev_ptr += 1;
18000
18001 /* now read in declarations */
22d2f3ab 18002 for (;;)
c906108c 18003 {
43988095
JK
18004 LONGEST implicit_const;
18005
22d2f3ab
JK
18006 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18007 abbrev_ptr += bytes_read;
18008 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18009 abbrev_ptr += bytes_read;
43988095
JK
18010 if (abbrev_form == DW_FORM_implicit_const)
18011 {
18012 implicit_const = read_signed_leb128 (abfd, abbrev_ptr,
18013 &bytes_read);
18014 abbrev_ptr += bytes_read;
18015 }
18016 else
18017 {
18018 /* Initialize it due to a false compiler warning. */
18019 implicit_const = -1;
18020 }
22d2f3ab
JK
18021
18022 if (abbrev_name == 0)
18023 break;
18024
f3dd6933 18025 if (cur_abbrev->num_attrs == allocated_attrs)
c906108c 18026 {
f3dd6933
DJ
18027 allocated_attrs += ATTR_ALLOC_CHUNK;
18028 cur_attrs
224c3ddb 18029 = XRESIZEVEC (struct attr_abbrev, cur_attrs, allocated_attrs);
c906108c 18030 }
ae038cb0 18031
aead7601
SM
18032 cur_attrs[cur_abbrev->num_attrs].name
18033 = (enum dwarf_attribute) abbrev_name;
22d2f3ab 18034 cur_attrs[cur_abbrev->num_attrs].form
aead7601 18035 = (enum dwarf_form) abbrev_form;
43988095 18036 cur_attrs[cur_abbrev->num_attrs].implicit_const = implicit_const;
22d2f3ab 18037 ++cur_abbrev->num_attrs;
c906108c
SS
18038 }
18039
8d749320
SM
18040 cur_abbrev->attrs =
18041 XOBNEWVEC (&abbrev_table->abbrev_obstack, struct attr_abbrev,
18042 cur_abbrev->num_attrs);
f3dd6933
DJ
18043 memcpy (cur_abbrev->attrs, cur_attrs,
18044 cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
18045
685af9cd 18046 abbrev_table->add_abbrev (abbrev_number, cur_abbrev);
c906108c
SS
18047
18048 /* Get next abbreviation.
18049 Under Irix6 the abbreviations for a compilation unit are not
c5aa993b
JM
18050 always properly terminated with an abbrev number of 0.
18051 Exit loop if we encounter an abbreviation which we have
18052 already read (which means we are about to read the abbreviations
18053 for the next compile unit) or if the end of the abbreviation
18054 table is reached. */
433df2d4 18055 if ((unsigned int) (abbrev_ptr - section->buffer) >= section->size)
c906108c
SS
18056 break;
18057 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18058 abbrev_ptr += bytes_read;
685af9cd 18059 if (abbrev_table->lookup_abbrev (abbrev_number) != NULL)
c906108c
SS
18060 break;
18061 }
f3dd6933
DJ
18062
18063 xfree (cur_attrs);
433df2d4 18064 return abbrev_table;
c906108c
SS
18065}
18066
72bf9492
DJ
18067/* Returns nonzero if TAG represents a type that we might generate a partial
18068 symbol for. */
18069
18070static int
18071is_type_tag_for_partial (int tag)
18072{
18073 switch (tag)
18074 {
18075#if 0
18076 /* Some types that would be reasonable to generate partial symbols for,
18077 that we don't at present. */
18078 case DW_TAG_array_type:
18079 case DW_TAG_file_type:
18080 case DW_TAG_ptr_to_member_type:
18081 case DW_TAG_set_type:
18082 case DW_TAG_string_type:
18083 case DW_TAG_subroutine_type:
18084#endif
18085 case DW_TAG_base_type:
18086 case DW_TAG_class_type:
680b30c7 18087 case DW_TAG_interface_type:
72bf9492
DJ
18088 case DW_TAG_enumeration_type:
18089 case DW_TAG_structure_type:
18090 case DW_TAG_subrange_type:
18091 case DW_TAG_typedef:
18092 case DW_TAG_union_type:
18093 return 1;
18094 default:
18095 return 0;
18096 }
18097}
18098
18099/* Load all DIEs that are interesting for partial symbols into memory. */
18100
18101static struct partial_die_info *
dee91e82 18102load_partial_dies (const struct die_reader_specs *reader,
d521ce57 18103 const gdb_byte *info_ptr, int building_psymtab)
72bf9492 18104{
dee91e82 18105 struct dwarf2_cu *cu = reader->cu;
518817b3 18106 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
72bf9492 18107 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
72bf9492 18108 unsigned int bytes_read;
5afb4e99 18109 unsigned int load_all = 0;
72bf9492
DJ
18110 int nesting_level = 1;
18111
18112 parent_die = NULL;
18113 last_die = NULL;
18114
7adf1e79
DE
18115 gdb_assert (cu->per_cu != NULL);
18116 if (cu->per_cu->load_all_dies)
5afb4e99
DJ
18117 load_all = 1;
18118
72bf9492
DJ
18119 cu->partial_dies
18120 = htab_create_alloc_ex (cu->header.length / 12,
18121 partial_die_hash,
18122 partial_die_eq,
18123 NULL,
18124 &cu->comp_unit_obstack,
18125 hashtab_obstack_allocate,
18126 dummy_obstack_deallocate);
18127
72bf9492
DJ
18128 while (1)
18129 {
685af9cd 18130 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
72bf9492
DJ
18131
18132 /* A NULL abbrev means the end of a series of children. */
18133 if (abbrev == NULL)
18134 {
18135 if (--nesting_level == 0)
cd9983dd
YQ
18136 return first_die;
18137
72bf9492
DJ
18138 info_ptr += bytes_read;
18139 last_die = parent_die;
18140 parent_die = parent_die->die_parent;
18141 continue;
18142 }
18143
98bfdba5
PA
18144 /* Check for template arguments. We never save these; if
18145 they're seen, we just mark the parent, and go on our way. */
18146 if (parent_die != NULL
18147 && cu->language == language_cplus
18148 && (abbrev->tag == DW_TAG_template_type_param
18149 || abbrev->tag == DW_TAG_template_value_param))
18150 {
18151 parent_die->has_template_arguments = 1;
18152
18153 if (!load_all)
18154 {
18155 /* We don't need a partial DIE for the template argument. */
dee91e82 18156 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
98bfdba5
PA
18157 continue;
18158 }
18159 }
18160
0d99eb77 18161 /* We only recurse into c++ subprograms looking for template arguments.
98bfdba5
PA
18162 Skip their other children. */
18163 if (!load_all
18164 && cu->language == language_cplus
18165 && parent_die != NULL
18166 && parent_die->tag == DW_TAG_subprogram)
18167 {
dee91e82 18168 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
98bfdba5
PA
18169 continue;
18170 }
18171
5afb4e99
DJ
18172 /* Check whether this DIE is interesting enough to save. Normally
18173 we would not be interested in members here, but there may be
18174 later variables referencing them via DW_AT_specification (for
18175 static members). */
18176 if (!load_all
18177 && !is_type_tag_for_partial (abbrev->tag)
72929c62 18178 && abbrev->tag != DW_TAG_constant
72bf9492
DJ
18179 && abbrev->tag != DW_TAG_enumerator
18180 && abbrev->tag != DW_TAG_subprogram
b1dc1806 18181 && abbrev->tag != DW_TAG_inlined_subroutine
bc30ff58 18182 && abbrev->tag != DW_TAG_lexical_block
72bf9492 18183 && abbrev->tag != DW_TAG_variable
5afb4e99 18184 && abbrev->tag != DW_TAG_namespace
f55ee35c 18185 && abbrev->tag != DW_TAG_module
95554aad 18186 && abbrev->tag != DW_TAG_member
74921315
KS
18187 && abbrev->tag != DW_TAG_imported_unit
18188 && abbrev->tag != DW_TAG_imported_declaration)
72bf9492
DJ
18189 {
18190 /* Otherwise we skip to the next sibling, if any. */
dee91e82 18191 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
72bf9492
DJ
18192 continue;
18193 }
18194
6f06d47b
YQ
18195 struct partial_die_info pdi ((sect_offset) (info_ptr - reader->buffer),
18196 abbrev);
cd9983dd 18197
48fbe735 18198 info_ptr = pdi.read (reader, *abbrev, info_ptr + bytes_read);
72bf9492
DJ
18199
18200 /* This two-pass algorithm for processing partial symbols has a
18201 high cost in cache pressure. Thus, handle some simple cases
18202 here which cover the majority of C partial symbols. DIEs
18203 which neither have specification tags in them, nor could have
18204 specification tags elsewhere pointing at them, can simply be
18205 processed and discarded.
18206
18207 This segment is also optional; scan_partial_symbols and
18208 add_partial_symbol will handle these DIEs if we chain
18209 them in normally. When compilers which do not emit large
18210 quantities of duplicate debug information are more common,
18211 this code can probably be removed. */
18212
18213 /* Any complete simple types at the top level (pretty much all
18214 of them, for a language without namespaces), can be processed
18215 directly. */
18216 if (parent_die == NULL
cd9983dd
YQ
18217 && pdi.has_specification == 0
18218 && pdi.is_declaration == 0
18219 && ((pdi.tag == DW_TAG_typedef && !pdi.has_children)
18220 || pdi.tag == DW_TAG_base_type
18221 || pdi.tag == DW_TAG_subrange_type))
72bf9492 18222 {
cd9983dd
YQ
18223 if (building_psymtab && pdi.name != NULL)
18224 add_psymbol_to_list (pdi.name, strlen (pdi.name), 0,
72bf9492 18225 VAR_DOMAIN, LOC_TYPEDEF,
bb5ed363 18226 &objfile->static_psymbols,
1762568f 18227 0, cu->language, objfile);
cd9983dd 18228 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
72bf9492
DJ
18229 continue;
18230 }
18231
d8228535
JK
18232 /* The exception for DW_TAG_typedef with has_children above is
18233 a workaround of GCC PR debug/47510. In the case of this complaint
a737d952 18234 type_name_or_error will error on such types later.
d8228535
JK
18235
18236 GDB skipped children of DW_TAG_typedef by the shortcut above and then
18237 it could not find the child DIEs referenced later, this is checked
18238 above. In correct DWARF DW_TAG_typedef should have no children. */
18239
cd9983dd 18240 if (pdi.tag == DW_TAG_typedef && pdi.has_children)
b98664d3 18241 complaint (_("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
9d8780f0 18242 "- DIE at %s [in module %s]"),
cd9983dd 18243 sect_offset_str (pdi.sect_off), objfile_name (objfile));
d8228535 18244
72bf9492
DJ
18245 /* If we're at the second level, and we're an enumerator, and
18246 our parent has no specification (meaning possibly lives in a
18247 namespace elsewhere), then we can add the partial symbol now
18248 instead of queueing it. */
cd9983dd 18249 if (pdi.tag == DW_TAG_enumerator
72bf9492
DJ
18250 && parent_die != NULL
18251 && parent_die->die_parent == NULL
18252 && parent_die->tag == DW_TAG_enumeration_type
18253 && parent_die->has_specification == 0)
18254 {
cd9983dd 18255 if (pdi.name == NULL)
b98664d3 18256 complaint (_("malformed enumerator DIE ignored"));
72bf9492 18257 else if (building_psymtab)
cd9983dd 18258 add_psymbol_to_list (pdi.name, strlen (pdi.name), 0,
72bf9492 18259 VAR_DOMAIN, LOC_CONST,
9c37b5ae 18260 cu->language == language_cplus
bb5ed363
DE
18261 ? &objfile->global_psymbols
18262 : &objfile->static_psymbols,
1762568f 18263 0, cu->language, objfile);
72bf9492 18264
cd9983dd 18265 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
72bf9492
DJ
18266 continue;
18267 }
18268
cd9983dd 18269 struct partial_die_info *part_die
6f06d47b 18270 = new (&cu->comp_unit_obstack) partial_die_info (pdi);
cd9983dd 18271
72bf9492
DJ
18272 /* We'll save this DIE so link it in. */
18273 part_die->die_parent = parent_die;
18274 part_die->die_sibling = NULL;
18275 part_die->die_child = NULL;
18276
18277 if (last_die && last_die == parent_die)
18278 last_die->die_child = part_die;
18279 else if (last_die)
18280 last_die->die_sibling = part_die;
18281
18282 last_die = part_die;
18283
18284 if (first_die == NULL)
18285 first_die = part_die;
18286
18287 /* Maybe add the DIE to the hash table. Not all DIEs that we
18288 find interesting need to be in the hash table, because we
18289 also have the parent/sibling/child chains; only those that we
18290 might refer to by offset later during partial symbol reading.
18291
18292 For now this means things that might have be the target of a
18293 DW_AT_specification, DW_AT_abstract_origin, or
18294 DW_AT_extension. DW_AT_extension will refer only to
18295 namespaces; DW_AT_abstract_origin refers to functions (and
18296 many things under the function DIE, but we do not recurse
18297 into function DIEs during partial symbol reading) and
18298 possibly variables as well; DW_AT_specification refers to
18299 declarations. Declarations ought to have the DW_AT_declaration
18300 flag. It happens that GCC forgets to put it in sometimes, but
18301 only for functions, not for types.
18302
18303 Adding more things than necessary to the hash table is harmless
18304 except for the performance cost. Adding too few will result in
5afb4e99
DJ
18305 wasted time in find_partial_die, when we reread the compilation
18306 unit with load_all_dies set. */
72bf9492 18307
5afb4e99 18308 if (load_all
72929c62 18309 || abbrev->tag == DW_TAG_constant
5afb4e99 18310 || abbrev->tag == DW_TAG_subprogram
72bf9492
DJ
18311 || abbrev->tag == DW_TAG_variable
18312 || abbrev->tag == DW_TAG_namespace
18313 || part_die->is_declaration)
18314 {
18315 void **slot;
18316
18317 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
9c541725
PA
18318 to_underlying (part_die->sect_off),
18319 INSERT);
72bf9492
DJ
18320 *slot = part_die;
18321 }
18322
72bf9492 18323 /* For some DIEs we want to follow their children (if any). For C
bc30ff58 18324 we have no reason to follow the children of structures; for other
98bfdba5
PA
18325 languages we have to, so that we can get at method physnames
18326 to infer fully qualified class names, for DW_AT_specification,
18327 and for C++ template arguments. For C++, we also look one level
18328 inside functions to find template arguments (if the name of the
18329 function does not already contain the template arguments).
bc30ff58
JB
18330
18331 For Ada, we need to scan the children of subprograms and lexical
18332 blocks as well because Ada allows the definition of nested
18333 entities that could be interesting for the debugger, such as
18334 nested subprograms for instance. */
72bf9492 18335 if (last_die->has_children
5afb4e99
DJ
18336 && (load_all
18337 || last_die->tag == DW_TAG_namespace
f55ee35c 18338 || last_die->tag == DW_TAG_module
72bf9492 18339 || last_die->tag == DW_TAG_enumeration_type
98bfdba5
PA
18340 || (cu->language == language_cplus
18341 && last_die->tag == DW_TAG_subprogram
18342 && (last_die->name == NULL
18343 || strchr (last_die->name, '<') == NULL))
72bf9492
DJ
18344 || (cu->language != language_c
18345 && (last_die->tag == DW_TAG_class_type
680b30c7 18346 || last_die->tag == DW_TAG_interface_type
72bf9492 18347 || last_die->tag == DW_TAG_structure_type
bc30ff58
JB
18348 || last_die->tag == DW_TAG_union_type))
18349 || (cu->language == language_ada
18350 && (last_die->tag == DW_TAG_subprogram
18351 || last_die->tag == DW_TAG_lexical_block))))
72bf9492
DJ
18352 {
18353 nesting_level++;
18354 parent_die = last_die;
18355 continue;
18356 }
18357
18358 /* Otherwise we skip to the next sibling, if any. */
dee91e82 18359 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
72bf9492
DJ
18360
18361 /* Back to the top, do it again. */
18362 }
18363}
18364
6f06d47b
YQ
18365partial_die_info::partial_die_info (sect_offset sect_off_,
18366 struct abbrev_info *abbrev)
18367 : partial_die_info (sect_off_, abbrev->tag, abbrev->has_children)
18368{
18369}
18370
35cc7ed7
YQ
18371/* Read a minimal amount of information into the minimal die structure.
18372 INFO_PTR should point just after the initial uleb128 of a DIE. */
c906108c 18373
48fbe735
YQ
18374const gdb_byte *
18375partial_die_info::read (const struct die_reader_specs *reader,
18376 const struct abbrev_info &abbrev, const gdb_byte *info_ptr)
c906108c 18377{
dee91e82 18378 struct dwarf2_cu *cu = reader->cu;
518817b3
SM
18379 struct dwarf2_per_objfile *dwarf2_per_objfile
18380 = cu->per_cu->dwarf2_per_objfile;
fa238c03 18381 unsigned int i;
c5aa993b 18382 int has_low_pc_attr = 0;
c906108c 18383 int has_high_pc_attr = 0;
91da1414 18384 int high_pc_relative = 0;
c906108c 18385
fd0a254f 18386 for (i = 0; i < abbrev.num_attrs; ++i)
c906108c 18387 {
48fbe735
YQ
18388 struct attribute attr;
18389
fd0a254f 18390 info_ptr = read_attribute (reader, &attr, &abbrev.attrs[i], info_ptr);
c906108c
SS
18391
18392 /* Store the data if it is of an attribute we want to keep in a
c5aa993b 18393 partial symbol table. */
c906108c
SS
18394 switch (attr.name)
18395 {
18396 case DW_AT_name:
48fbe735 18397 switch (tag)
71c25dea
TT
18398 {
18399 case DW_TAG_compile_unit:
95554aad 18400 case DW_TAG_partial_unit:
348e048f 18401 case DW_TAG_type_unit:
71c25dea
TT
18402 /* Compilation units have a DW_AT_name that is a filename, not
18403 a source language identifier. */
18404 case DW_TAG_enumeration_type:
18405 case DW_TAG_enumerator:
18406 /* These tags always have simple identifiers already; no need
18407 to canonicalize them. */
48fbe735 18408 name = DW_STRING (&attr);
71c25dea
TT
18409 break;
18410 default:
48fbe735
YQ
18411 {
18412 struct objfile *objfile = dwarf2_per_objfile->objfile;
18413
18414 name
18415 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
18416 &objfile->per_bfd->storage_obstack);
18417 }
71c25dea
TT
18418 break;
18419 }
c906108c 18420 break;
31ef98ae 18421 case DW_AT_linkage_name:
c906108c 18422 case DW_AT_MIPS_linkage_name:
31ef98ae
TT
18423 /* Note that both forms of linkage name might appear. We
18424 assume they will be the same, and we only store the last
18425 one we see. */
94af9270 18426 if (cu->language == language_ada)
48fbe735
YQ
18427 name = DW_STRING (&attr);
18428 linkage_name = DW_STRING (&attr);
c906108c
SS
18429 break;
18430 case DW_AT_low_pc:
18431 has_low_pc_attr = 1;
48fbe735 18432 lowpc = attr_value_as_address (&attr);
c906108c
SS
18433 break;
18434 case DW_AT_high_pc:
18435 has_high_pc_attr = 1;
48fbe735 18436 highpc = attr_value_as_address (&attr);
31aa7e4e
JB
18437 if (cu->header.version >= 4 && attr_form_is_constant (&attr))
18438 high_pc_relative = 1;
c906108c
SS
18439 break;
18440 case DW_AT_location:
0963b4bd 18441 /* Support the .debug_loc offsets. */
8e19ed76
PS
18442 if (attr_form_is_block (&attr))
18443 {
48fbe735 18444 d.locdesc = DW_BLOCK (&attr);
8e19ed76 18445 }
3690dd37 18446 else if (attr_form_is_section_offset (&attr))
8e19ed76 18447 {
4d3c2250 18448 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
18449 }
18450 else
18451 {
4d3c2250
KB
18452 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
18453 "partial symbol information");
8e19ed76 18454 }
c906108c 18455 break;
c906108c 18456 case DW_AT_external:
48fbe735 18457 is_external = DW_UNSND (&attr);
c906108c
SS
18458 break;
18459 case DW_AT_declaration:
48fbe735 18460 is_declaration = DW_UNSND (&attr);
c906108c
SS
18461 break;
18462 case DW_AT_type:
48fbe735 18463 has_type = 1;
c906108c
SS
18464 break;
18465 case DW_AT_abstract_origin:
18466 case DW_AT_specification:
72bf9492 18467 case DW_AT_extension:
48fbe735
YQ
18468 has_specification = 1;
18469 spec_offset = dwarf2_get_ref_die_offset (&attr);
18470 spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
36586728 18471 || cu->per_cu->is_dwz);
c906108c
SS
18472 break;
18473 case DW_AT_sibling:
18474 /* Ignore absolute siblings, they might point outside of
18475 the current compile unit. */
18476 if (attr.form == DW_FORM_ref_addr)
b98664d3 18477 complaint (_("ignoring absolute DW_AT_sibling"));
c906108c 18478 else
b9502d3f 18479 {
48fbe735 18480 const gdb_byte *buffer = reader->buffer;
9c541725
PA
18481 sect_offset off = dwarf2_get_ref_die_offset (&attr);
18482 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
b9502d3f
WN
18483
18484 if (sibling_ptr < info_ptr)
b98664d3 18485 complaint (_("DW_AT_sibling points backwards"));
22869d73
KS
18486 else if (sibling_ptr > reader->buffer_end)
18487 dwarf2_section_buffer_overflow_complaint (reader->die_section);
b9502d3f 18488 else
48fbe735 18489 sibling = sibling_ptr;
b9502d3f 18490 }
c906108c 18491 break;
fa4028e9 18492 case DW_AT_byte_size:
48fbe735 18493 has_byte_size = 1;
fa4028e9 18494 break;
ff908ebf 18495 case DW_AT_const_value:
48fbe735 18496 has_const_value = 1;
ff908ebf 18497 break;
68511cec
CES
18498 case DW_AT_calling_convention:
18499 /* DWARF doesn't provide a way to identify a program's source-level
18500 entry point. DW_AT_calling_convention attributes are only meant
18501 to describe functions' calling conventions.
18502
18503 However, because it's a necessary piece of information in
0c1b455e
TT
18504 Fortran, and before DWARF 4 DW_CC_program was the only
18505 piece of debugging information whose definition refers to
18506 a 'main program' at all, several compilers marked Fortran
18507 main programs with DW_CC_program --- even when those
18508 functions use the standard calling conventions.
18509
18510 Although DWARF now specifies a way to provide this
18511 information, we support this practice for backward
18512 compatibility. */
68511cec 18513 if (DW_UNSND (&attr) == DW_CC_program
0c1b455e 18514 && cu->language == language_fortran)
48fbe735 18515 main_subprogram = 1;
68511cec 18516 break;
481860b3
GB
18517 case DW_AT_inline:
18518 if (DW_UNSND (&attr) == DW_INL_inlined
18519 || DW_UNSND (&attr) == DW_INL_declared_inlined)
48fbe735 18520 may_be_inlined = 1;
481860b3 18521 break;
95554aad
TT
18522
18523 case DW_AT_import:
48fbe735 18524 if (tag == DW_TAG_imported_unit)
36586728 18525 {
48fbe735
YQ
18526 d.sect_off = dwarf2_get_ref_die_offset (&attr);
18527 is_dwz = (attr.form == DW_FORM_GNU_ref_alt
36586728
TT
18528 || cu->per_cu->is_dwz);
18529 }
95554aad
TT
18530 break;
18531
0c1b455e 18532 case DW_AT_main_subprogram:
48fbe735 18533 main_subprogram = DW_UNSND (&attr);
0c1b455e
TT
18534 break;
18535
c906108c
SS
18536 default:
18537 break;
18538 }
18539 }
18540
91da1414 18541 if (high_pc_relative)
48fbe735 18542 highpc += lowpc;
91da1414 18543
9373cf26
JK
18544 if (has_low_pc_attr && has_high_pc_attr)
18545 {
18546 /* When using the GNU linker, .gnu.linkonce. sections are used to
18547 eliminate duplicate copies of functions and vtables and such.
18548 The linker will arbitrarily choose one and discard the others.
18549 The AT_*_pc values for such functions refer to local labels in
18550 these sections. If the section from that file was discarded, the
18551 labels are not in the output, so the relocs get a value of 0.
18552 If this is a discarded function, mark the pc bounds as invalid,
18553 so that GDB will ignore it. */
48fbe735 18554 if (lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
9373cf26 18555 {
48fbe735 18556 struct objfile *objfile = dwarf2_per_objfile->objfile;
bb5ed363 18557 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9373cf26 18558
b98664d3 18559 complaint (_("DW_AT_low_pc %s is zero "
9d8780f0 18560 "for DIE at %s [in module %s]"),
48fbe735
YQ
18561 paddress (gdbarch, lowpc),
18562 sect_offset_str (sect_off),
9d8780f0 18563 objfile_name (objfile));
9373cf26
JK
18564 }
18565 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
48fbe735 18566 else if (lowpc >= highpc)
9373cf26 18567 {
48fbe735 18568 struct objfile *objfile = dwarf2_per_objfile->objfile;
bb5ed363 18569 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9373cf26 18570
b98664d3 18571 complaint (_("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
9d8780f0 18572 "for DIE at %s [in module %s]"),
48fbe735
YQ
18573 paddress (gdbarch, lowpc),
18574 paddress (gdbarch, highpc),
18575 sect_offset_str (sect_off),
9c541725 18576 objfile_name (objfile));
9373cf26
JK
18577 }
18578 else
48fbe735 18579 has_pc_info = 1;
9373cf26 18580 }
85cbf3d3 18581
c906108c
SS
18582 return info_ptr;
18583}
18584
72bf9492
DJ
18585/* Find a cached partial DIE at OFFSET in CU. */
18586
d590ff25
YQ
18587struct partial_die_info *
18588dwarf2_cu::find_partial_die (sect_offset sect_off)
72bf9492
DJ
18589{
18590 struct partial_die_info *lookup_die = NULL;
6f06d47b 18591 struct partial_die_info part_die (sect_off);
72bf9492 18592
9a3c8263 18593 lookup_die = ((struct partial_die_info *)
d590ff25 18594 htab_find_with_hash (partial_dies, &part_die,
9c541725 18595 to_underlying (sect_off)));
72bf9492 18596
72bf9492
DJ
18597 return lookup_die;
18598}
18599
348e048f
DE
18600/* Find a partial DIE at OFFSET, which may or may not be in CU,
18601 except in the case of .debug_types DIEs which do not reference
18602 outside their CU (they do however referencing other types via
55f1336d 18603 DW_FORM_ref_sig8). */
72bf9492
DJ
18604
18605static struct partial_die_info *
9c541725 18606find_partial_die (sect_offset sect_off, int offset_in_dwz, struct dwarf2_cu *cu)
72bf9492 18607{
518817b3
SM
18608 struct dwarf2_per_objfile *dwarf2_per_objfile
18609 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 18610 struct objfile *objfile = dwarf2_per_objfile->objfile;
5afb4e99
DJ
18611 struct dwarf2_per_cu_data *per_cu = NULL;
18612 struct partial_die_info *pd = NULL;
72bf9492 18613
36586728 18614 if (offset_in_dwz == cu->per_cu->is_dwz
9c541725 18615 && offset_in_cu_p (&cu->header, sect_off))
5afb4e99 18616 {
d590ff25 18617 pd = cu->find_partial_die (sect_off);
5afb4e99
DJ
18618 if (pd != NULL)
18619 return pd;
0d99eb77
DE
18620 /* We missed recording what we needed.
18621 Load all dies and try again. */
18622 per_cu = cu->per_cu;
5afb4e99 18623 }
0d99eb77
DE
18624 else
18625 {
18626 /* TUs don't reference other CUs/TUs (except via type signatures). */
3019eac3 18627 if (cu->per_cu->is_debug_types)
0d99eb77 18628 {
9d8780f0
SM
18629 error (_("Dwarf Error: Type Unit at offset %s contains"
18630 " external reference to offset %s [in module %s].\n"),
18631 sect_offset_str (cu->header.sect_off), sect_offset_str (sect_off),
0d99eb77
DE
18632 bfd_get_filename (objfile->obfd));
18633 }
9c541725 18634 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
ed2dc618 18635 dwarf2_per_objfile);
72bf9492 18636
0d99eb77
DE
18637 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
18638 load_partial_comp_unit (per_cu);
ae038cb0 18639
0d99eb77 18640 per_cu->cu->last_used = 0;
d590ff25 18641 pd = per_cu->cu->find_partial_die (sect_off);
0d99eb77 18642 }
5afb4e99 18643
dee91e82
DE
18644 /* If we didn't find it, and not all dies have been loaded,
18645 load them all and try again. */
18646
5afb4e99
DJ
18647 if (pd == NULL && per_cu->load_all_dies == 0)
18648 {
5afb4e99 18649 per_cu->load_all_dies = 1;
fd820528
DE
18650
18651 /* This is nasty. When we reread the DIEs, somewhere up the call chain
18652 THIS_CU->cu may already be in use. So we can't just free it and
18653 replace its DIEs with the ones we read in. Instead, we leave those
18654 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
18655 and clobber THIS_CU->cu->partial_dies with the hash table for the new
18656 set. */
dee91e82 18657 load_partial_comp_unit (per_cu);
5afb4e99 18658
d590ff25 18659 pd = per_cu->cu->find_partial_die (sect_off);
5afb4e99
DJ
18660 }
18661
18662 if (pd == NULL)
18663 internal_error (__FILE__, __LINE__,
9d8780f0 18664 _("could not find partial DIE %s "
3e43a32a 18665 "in cache [from module %s]\n"),
9d8780f0 18666 sect_offset_str (sect_off), bfd_get_filename (objfile->obfd));
5afb4e99 18667 return pd;
72bf9492
DJ
18668}
18669
abc72ce4
DE
18670/* See if we can figure out if the class lives in a namespace. We do
18671 this by looking for a member function; its demangled name will
18672 contain namespace info, if there is any. */
18673
18674static void
18675guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
18676 struct dwarf2_cu *cu)
18677{
18678 /* NOTE: carlton/2003-10-07: Getting the info this way changes
18679 what template types look like, because the demangler
18680 frequently doesn't give the same name as the debug info. We
18681 could fix this by only using the demangled name to get the
18682 prefix (but see comment in read_structure_type). */
18683
18684 struct partial_die_info *real_pdi;
18685 struct partial_die_info *child_pdi;
18686
18687 /* If this DIE (this DIE's specification, if any) has a parent, then
18688 we should not do this. We'll prepend the parent's fully qualified
18689 name when we create the partial symbol. */
18690
18691 real_pdi = struct_pdi;
18692 while (real_pdi->has_specification)
36586728
TT
18693 real_pdi = find_partial_die (real_pdi->spec_offset,
18694 real_pdi->spec_is_dwz, cu);
abc72ce4
DE
18695
18696 if (real_pdi->die_parent != NULL)
18697 return;
18698
18699 for (child_pdi = struct_pdi->die_child;
18700 child_pdi != NULL;
18701 child_pdi = child_pdi->die_sibling)
18702 {
18703 if (child_pdi->tag == DW_TAG_subprogram
18704 && child_pdi->linkage_name != NULL)
18705 {
18706 char *actual_class_name
18707 = language_class_name_from_physname (cu->language_defn,
18708 child_pdi->linkage_name);
18709 if (actual_class_name != NULL)
18710 {
518817b3 18711 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
abc72ce4 18712 struct_pdi->name
224c3ddb 18713 = ((const char *)
e3b94546 18714 obstack_copy0 (&objfile->per_bfd->storage_obstack,
224c3ddb
SM
18715 actual_class_name,
18716 strlen (actual_class_name)));
abc72ce4
DE
18717 xfree (actual_class_name);
18718 }
18719 break;
18720 }
18721 }
18722}
18723
52356b79
YQ
18724void
18725partial_die_info::fixup (struct dwarf2_cu *cu)
72bf9492 18726{
abc72ce4
DE
18727 /* Once we've fixed up a die, there's no point in doing so again.
18728 This also avoids a memory leak if we were to call
18729 guess_partial_die_structure_name multiple times. */
52356b79 18730 if (fixup_called)
abc72ce4
DE
18731 return;
18732
72bf9492
DJ
18733 /* If we found a reference attribute and the DIE has no name, try
18734 to find a name in the referred to DIE. */
18735
52356b79 18736 if (name == NULL && has_specification)
72bf9492
DJ
18737 {
18738 struct partial_die_info *spec_die;
72bf9492 18739
52356b79 18740 spec_die = find_partial_die (spec_offset, spec_is_dwz, cu);
72bf9492 18741
52356b79 18742 spec_die->fixup (cu);
72bf9492
DJ
18743
18744 if (spec_die->name)
18745 {
52356b79 18746 name = spec_die->name;
72bf9492
DJ
18747
18748 /* Copy DW_AT_external attribute if it is set. */
18749 if (spec_die->is_external)
52356b79 18750 is_external = spec_die->is_external;
72bf9492
DJ
18751 }
18752 }
18753
18754 /* Set default names for some unnamed DIEs. */
72bf9492 18755
52356b79
YQ
18756 if (name == NULL && tag == DW_TAG_namespace)
18757 name = CP_ANONYMOUS_NAMESPACE_STR;
72bf9492 18758
abc72ce4
DE
18759 /* If there is no parent die to provide a namespace, and there are
18760 children, see if we can determine the namespace from their linkage
122d1940 18761 name. */
abc72ce4 18762 if (cu->language == language_cplus
518817b3
SM
18763 && !VEC_empty (dwarf2_section_info_def,
18764 cu->per_cu->dwarf2_per_objfile->types)
52356b79
YQ
18765 && die_parent == NULL
18766 && has_children
18767 && (tag == DW_TAG_class_type
18768 || tag == DW_TAG_structure_type
18769 || tag == DW_TAG_union_type))
18770 guess_partial_die_structure_name (this, cu);
abc72ce4 18771
53832f31
TT
18772 /* GCC might emit a nameless struct or union that has a linkage
18773 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
52356b79
YQ
18774 if (name == NULL
18775 && (tag == DW_TAG_class_type
18776 || tag == DW_TAG_interface_type
18777 || tag == DW_TAG_structure_type
18778 || tag == DW_TAG_union_type)
18779 && linkage_name != NULL)
53832f31
TT
18780 {
18781 char *demangled;
18782
52356b79 18783 demangled = gdb_demangle (linkage_name, DMGL_TYPES);
53832f31
TT
18784 if (demangled)
18785 {
96408a79
SA
18786 const char *base;
18787
18788 /* Strip any leading namespaces/classes, keep only the base name.
18789 DW_AT_name for named DIEs does not contain the prefixes. */
18790 base = strrchr (demangled, ':');
18791 if (base && base > demangled && base[-1] == ':')
18792 base++;
18793 else
18794 base = demangled;
18795
518817b3 18796 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
52356b79 18797 name
224c3ddb 18798 = ((const char *)
e3b94546 18799 obstack_copy0 (&objfile->per_bfd->storage_obstack,
224c3ddb 18800 base, strlen (base)));
53832f31
TT
18801 xfree (demangled);
18802 }
18803 }
18804
52356b79 18805 fixup_called = 1;
72bf9492
DJ
18806}
18807
a8329558 18808/* Read an attribute value described by an attribute form. */
c906108c 18809
d521ce57 18810static const gdb_byte *
dee91e82
DE
18811read_attribute_value (const struct die_reader_specs *reader,
18812 struct attribute *attr, unsigned form,
43988095 18813 LONGEST implicit_const, const gdb_byte *info_ptr)
c906108c 18814{
dee91e82 18815 struct dwarf2_cu *cu = reader->cu;
518817b3
SM
18816 struct dwarf2_per_objfile *dwarf2_per_objfile
18817 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 18818 struct objfile *objfile = dwarf2_per_objfile->objfile;
3e29f34a 18819 struct gdbarch *gdbarch = get_objfile_arch (objfile);
dee91e82 18820 bfd *abfd = reader->abfd;
e7c27a73 18821 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
18822 unsigned int bytes_read;
18823 struct dwarf_block *blk;
18824
aead7601 18825 attr->form = (enum dwarf_form) form;
a8329558 18826 switch (form)
c906108c 18827 {
c906108c 18828 case DW_FORM_ref_addr:
ae411497 18829 if (cu->header.version == 2)
4568ecf9 18830 DW_UNSND (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
ae411497 18831 else
4568ecf9
DE
18832 DW_UNSND (attr) = read_offset (abfd, info_ptr,
18833 &cu->header, &bytes_read);
ae411497
TT
18834 info_ptr += bytes_read;
18835 break;
36586728
TT
18836 case DW_FORM_GNU_ref_alt:
18837 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
18838 info_ptr += bytes_read;
18839 break;
ae411497 18840 case DW_FORM_addr:
e7c27a73 18841 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
3e29f34a 18842 DW_ADDR (attr) = gdbarch_adjust_dwarf2_addr (gdbarch, DW_ADDR (attr));
107d2387 18843 info_ptr += bytes_read;
c906108c
SS
18844 break;
18845 case DW_FORM_block2:
7b5a2f43 18846 blk = dwarf_alloc_block (cu);
c906108c
SS
18847 blk->size = read_2_bytes (abfd, info_ptr);
18848 info_ptr += 2;
18849 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
18850 info_ptr += blk->size;
18851 DW_BLOCK (attr) = blk;
18852 break;
18853 case DW_FORM_block4:
7b5a2f43 18854 blk = dwarf_alloc_block (cu);
c906108c
SS
18855 blk->size = read_4_bytes (abfd, info_ptr);
18856 info_ptr += 4;
18857 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
18858 info_ptr += blk->size;
18859 DW_BLOCK (attr) = blk;
18860 break;
18861 case DW_FORM_data2:
18862 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
18863 info_ptr += 2;
18864 break;
18865 case DW_FORM_data4:
18866 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
18867 info_ptr += 4;
18868 break;
18869 case DW_FORM_data8:
18870 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
18871 info_ptr += 8;
18872 break;
0224619f
JK
18873 case DW_FORM_data16:
18874 blk = dwarf_alloc_block (cu);
18875 blk->size = 16;
18876 blk->data = read_n_bytes (abfd, info_ptr, 16);
18877 info_ptr += 16;
18878 DW_BLOCK (attr) = blk;
18879 break;
2dc7f7b3
TT
18880 case DW_FORM_sec_offset:
18881 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
18882 info_ptr += bytes_read;
18883 break;
c906108c 18884 case DW_FORM_string:
9b1c24c8 18885 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
8285870a 18886 DW_STRING_IS_CANONICAL (attr) = 0;
c906108c
SS
18887 info_ptr += bytes_read;
18888 break;
4bdf3d34 18889 case DW_FORM_strp:
36586728
TT
18890 if (!cu->per_cu->is_dwz)
18891 {
ed2dc618
SM
18892 DW_STRING (attr) = read_indirect_string (dwarf2_per_objfile,
18893 abfd, info_ptr, cu_header,
36586728
TT
18894 &bytes_read);
18895 DW_STRING_IS_CANONICAL (attr) = 0;
18896 info_ptr += bytes_read;
18897 break;
18898 }
18899 /* FALLTHROUGH */
43988095
JK
18900 case DW_FORM_line_strp:
18901 if (!cu->per_cu->is_dwz)
18902 {
ed2dc618
SM
18903 DW_STRING (attr) = read_indirect_line_string (dwarf2_per_objfile,
18904 abfd, info_ptr,
43988095
JK
18905 cu_header, &bytes_read);
18906 DW_STRING_IS_CANONICAL (attr) = 0;
18907 info_ptr += bytes_read;
18908 break;
18909 }
18910 /* FALLTHROUGH */
36586728
TT
18911 case DW_FORM_GNU_strp_alt:
18912 {
ed2dc618 18913 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
36586728
TT
18914 LONGEST str_offset = read_offset (abfd, info_ptr, cu_header,
18915 &bytes_read);
18916
ed2dc618
SM
18917 DW_STRING (attr) = read_indirect_string_from_dwz (objfile,
18918 dwz, str_offset);
36586728
TT
18919 DW_STRING_IS_CANONICAL (attr) = 0;
18920 info_ptr += bytes_read;
18921 }
4bdf3d34 18922 break;
2dc7f7b3 18923 case DW_FORM_exprloc:
c906108c 18924 case DW_FORM_block:
7b5a2f43 18925 blk = dwarf_alloc_block (cu);
c906108c
SS
18926 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18927 info_ptr += bytes_read;
18928 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
18929 info_ptr += blk->size;
18930 DW_BLOCK (attr) = blk;
18931 break;
18932 case DW_FORM_block1:
7b5a2f43 18933 blk = dwarf_alloc_block (cu);
c906108c
SS
18934 blk->size = read_1_byte (abfd, info_ptr);
18935 info_ptr += 1;
18936 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
18937 info_ptr += blk->size;
18938 DW_BLOCK (attr) = blk;
18939 break;
18940 case DW_FORM_data1:
18941 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
18942 info_ptr += 1;
18943 break;
18944 case DW_FORM_flag:
18945 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
18946 info_ptr += 1;
18947 break;
2dc7f7b3
TT
18948 case DW_FORM_flag_present:
18949 DW_UNSND (attr) = 1;
18950 break;
c906108c
SS
18951 case DW_FORM_sdata:
18952 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
18953 info_ptr += bytes_read;
18954 break;
18955 case DW_FORM_udata:
18956 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18957 info_ptr += bytes_read;
18958 break;
18959 case DW_FORM_ref1:
9c541725 18960 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 18961 + read_1_byte (abfd, info_ptr));
c906108c
SS
18962 info_ptr += 1;
18963 break;
18964 case DW_FORM_ref2:
9c541725 18965 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 18966 + read_2_bytes (abfd, info_ptr));
c906108c
SS
18967 info_ptr += 2;
18968 break;
18969 case DW_FORM_ref4:
9c541725 18970 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 18971 + read_4_bytes (abfd, info_ptr));
c906108c
SS
18972 info_ptr += 4;
18973 break;
613e1657 18974 case DW_FORM_ref8:
9c541725 18975 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 18976 + read_8_bytes (abfd, info_ptr));
613e1657
KB
18977 info_ptr += 8;
18978 break;
55f1336d 18979 case DW_FORM_ref_sig8:
ac9ec31b 18980 DW_SIGNATURE (attr) = read_8_bytes (abfd, info_ptr);
348e048f
DE
18981 info_ptr += 8;
18982 break;
c906108c 18983 case DW_FORM_ref_udata:
9c541725 18984 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 18985 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
c906108c
SS
18986 info_ptr += bytes_read;
18987 break;
c906108c 18988 case DW_FORM_indirect:
a8329558
KW
18989 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18990 info_ptr += bytes_read;
43988095
JK
18991 if (form == DW_FORM_implicit_const)
18992 {
18993 implicit_const = read_signed_leb128 (abfd, info_ptr, &bytes_read);
18994 info_ptr += bytes_read;
18995 }
18996 info_ptr = read_attribute_value (reader, attr, form, implicit_const,
18997 info_ptr);
18998 break;
18999 case DW_FORM_implicit_const:
19000 DW_SND (attr) = implicit_const;
a8329558 19001 break;
3019eac3
DE
19002 case DW_FORM_GNU_addr_index:
19003 if (reader->dwo_file == NULL)
19004 {
19005 /* For now flag a hard error.
19006 Later we can turn this into a complaint. */
19007 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
19008 dwarf_form_name (form),
19009 bfd_get_filename (abfd));
19010 }
19011 DW_ADDR (attr) = read_addr_index_from_leb128 (cu, info_ptr, &bytes_read);
19012 info_ptr += bytes_read;
19013 break;
19014 case DW_FORM_GNU_str_index:
19015 if (reader->dwo_file == NULL)
19016 {
19017 /* For now flag a hard error.
19018 Later we can turn this into a complaint if warranted. */
19019 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
19020 dwarf_form_name (form),
19021 bfd_get_filename (abfd));
19022 }
19023 {
19024 ULONGEST str_index =
19025 read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19026
342587c4 19027 DW_STRING (attr) = read_str_index (reader, str_index);
3019eac3
DE
19028 DW_STRING_IS_CANONICAL (attr) = 0;
19029 info_ptr += bytes_read;
19030 }
19031 break;
c906108c 19032 default:
8a3fe4f8 19033 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
659b0389
ML
19034 dwarf_form_name (form),
19035 bfd_get_filename (abfd));
c906108c 19036 }
28e94949 19037
36586728 19038 /* Super hack. */
7771576e 19039 if (cu->per_cu->is_dwz && attr_form_is_ref (attr))
36586728
TT
19040 attr->form = DW_FORM_GNU_ref_alt;
19041
28e94949
JB
19042 /* We have seen instances where the compiler tried to emit a byte
19043 size attribute of -1 which ended up being encoded as an unsigned
19044 0xffffffff. Although 0xffffffff is technically a valid size value,
19045 an object of this size seems pretty unlikely so we can relatively
19046 safely treat these cases as if the size attribute was invalid and
19047 treat them as zero by default. */
19048 if (attr->name == DW_AT_byte_size
19049 && form == DW_FORM_data4
19050 && DW_UNSND (attr) >= 0xffffffff)
01c66ae6
JB
19051 {
19052 complaint
b98664d3 19053 (_("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
43bbcdc2 19054 hex_string (DW_UNSND (attr)));
01c66ae6
JB
19055 DW_UNSND (attr) = 0;
19056 }
28e94949 19057
c906108c
SS
19058 return info_ptr;
19059}
19060
a8329558
KW
19061/* Read an attribute described by an abbreviated attribute. */
19062
d521ce57 19063static const gdb_byte *
dee91e82
DE
19064read_attribute (const struct die_reader_specs *reader,
19065 struct attribute *attr, struct attr_abbrev *abbrev,
d521ce57 19066 const gdb_byte *info_ptr)
a8329558
KW
19067{
19068 attr->name = abbrev->name;
43988095
JK
19069 return read_attribute_value (reader, attr, abbrev->form,
19070 abbrev->implicit_const, info_ptr);
a8329558
KW
19071}
19072
0963b4bd 19073/* Read dwarf information from a buffer. */
c906108c
SS
19074
19075static unsigned int
a1855c1d 19076read_1_byte (bfd *abfd, const gdb_byte *buf)
c906108c 19077{
fe1b8b76 19078 return bfd_get_8 (abfd, buf);
c906108c
SS
19079}
19080
19081static int
a1855c1d 19082read_1_signed_byte (bfd *abfd, const gdb_byte *buf)
c906108c 19083{
fe1b8b76 19084 return bfd_get_signed_8 (abfd, buf);
c906108c
SS
19085}
19086
19087static unsigned int
a1855c1d 19088read_2_bytes (bfd *abfd, const gdb_byte *buf)
c906108c 19089{
fe1b8b76 19090 return bfd_get_16 (abfd, buf);
c906108c
SS
19091}
19092
21ae7a4d 19093static int
a1855c1d 19094read_2_signed_bytes (bfd *abfd, const gdb_byte *buf)
21ae7a4d
JK
19095{
19096 return bfd_get_signed_16 (abfd, buf);
19097}
19098
c906108c 19099static unsigned int
a1855c1d 19100read_4_bytes (bfd *abfd, const gdb_byte *buf)
c906108c 19101{
fe1b8b76 19102 return bfd_get_32 (abfd, buf);
c906108c
SS
19103}
19104
21ae7a4d 19105static int
a1855c1d 19106read_4_signed_bytes (bfd *abfd, const gdb_byte *buf)
21ae7a4d
JK
19107{
19108 return bfd_get_signed_32 (abfd, buf);
19109}
19110
93311388 19111static ULONGEST
a1855c1d 19112read_8_bytes (bfd *abfd, const gdb_byte *buf)
c906108c 19113{
fe1b8b76 19114 return bfd_get_64 (abfd, buf);
c906108c
SS
19115}
19116
19117static CORE_ADDR
d521ce57 19118read_address (bfd *abfd, const gdb_byte *buf, struct dwarf2_cu *cu,
891d2f0b 19119 unsigned int *bytes_read)
c906108c 19120{
e7c27a73 19121 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
19122 CORE_ADDR retval = 0;
19123
107d2387 19124 if (cu_header->signed_addr_p)
c906108c 19125 {
107d2387
AC
19126 switch (cu_header->addr_size)
19127 {
19128 case 2:
fe1b8b76 19129 retval = bfd_get_signed_16 (abfd, buf);
107d2387
AC
19130 break;
19131 case 4:
fe1b8b76 19132 retval = bfd_get_signed_32 (abfd, buf);
107d2387
AC
19133 break;
19134 case 8:
fe1b8b76 19135 retval = bfd_get_signed_64 (abfd, buf);
107d2387
AC
19136 break;
19137 default:
8e65ff28 19138 internal_error (__FILE__, __LINE__,
e2e0b3e5 19139 _("read_address: bad switch, signed [in module %s]"),
659b0389 19140 bfd_get_filename (abfd));
107d2387
AC
19141 }
19142 }
19143 else
19144 {
19145 switch (cu_header->addr_size)
19146 {
19147 case 2:
fe1b8b76 19148 retval = bfd_get_16 (abfd, buf);
107d2387
AC
19149 break;
19150 case 4:
fe1b8b76 19151 retval = bfd_get_32 (abfd, buf);
107d2387
AC
19152 break;
19153 case 8:
fe1b8b76 19154 retval = bfd_get_64 (abfd, buf);
107d2387
AC
19155 break;
19156 default:
8e65ff28 19157 internal_error (__FILE__, __LINE__,
a73c6dcd
MS
19158 _("read_address: bad switch, "
19159 "unsigned [in module %s]"),
659b0389 19160 bfd_get_filename (abfd));
107d2387 19161 }
c906108c 19162 }
64367e0a 19163
107d2387
AC
19164 *bytes_read = cu_header->addr_size;
19165 return retval;
c906108c
SS
19166}
19167
f7ef9339 19168/* Read the initial length from a section. The (draft) DWARF 3
613e1657
KB
19169 specification allows the initial length to take up either 4 bytes
19170 or 12 bytes. If the first 4 bytes are 0xffffffff, then the next 8
19171 bytes describe the length and all offsets will be 8 bytes in length
19172 instead of 4.
19173
f7ef9339
KB
19174 An older, non-standard 64-bit format is also handled by this
19175 function. The older format in question stores the initial length
19176 as an 8-byte quantity without an escape value. Lengths greater
19177 than 2^32 aren't very common which means that the initial 4 bytes
19178 is almost always zero. Since a length value of zero doesn't make
19179 sense for the 32-bit format, this initial zero can be considered to
19180 be an escape value which indicates the presence of the older 64-bit
19181 format. As written, the code can't detect (old format) lengths
917c78fc
MK
19182 greater than 4GB. If it becomes necessary to handle lengths
19183 somewhat larger than 4GB, we could allow other small values (such
19184 as the non-sensical values of 1, 2, and 3) to also be used as
19185 escape values indicating the presence of the old format.
f7ef9339 19186
917c78fc
MK
19187 The value returned via bytes_read should be used to increment the
19188 relevant pointer after calling read_initial_length().
c764a876 19189
613e1657
KB
19190 [ Note: read_initial_length() and read_offset() are based on the
19191 document entitled "DWARF Debugging Information Format", revision
f7ef9339 19192 3, draft 8, dated November 19, 2001. This document was obtained
613e1657
KB
19193 from:
19194
f7ef9339 19195 http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
6e70227d 19196
613e1657
KB
19197 This document is only a draft and is subject to change. (So beware.)
19198
f7ef9339 19199 Details regarding the older, non-standard 64-bit format were
917c78fc
MK
19200 determined empirically by examining 64-bit ELF files produced by
19201 the SGI toolchain on an IRIX 6.5 machine.
f7ef9339
KB
19202
19203 - Kevin, July 16, 2002
613e1657
KB
19204 ] */
19205
19206static LONGEST
d521ce57 19207read_initial_length (bfd *abfd, const gdb_byte *buf, unsigned int *bytes_read)
613e1657 19208{
fe1b8b76 19209 LONGEST length = bfd_get_32 (abfd, buf);
613e1657 19210
dd373385 19211 if (length == 0xffffffff)
613e1657 19212 {
fe1b8b76 19213 length = bfd_get_64 (abfd, buf + 4);
613e1657 19214 *bytes_read = 12;
613e1657 19215 }
dd373385 19216 else if (length == 0)
f7ef9339 19217 {
dd373385 19218 /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX. */
fe1b8b76 19219 length = bfd_get_64 (abfd, buf);
f7ef9339 19220 *bytes_read = 8;
f7ef9339 19221 }
613e1657
KB
19222 else
19223 {
19224 *bytes_read = 4;
613e1657
KB
19225 }
19226
c764a876
DE
19227 return length;
19228}
dd373385 19229
c764a876
DE
19230/* Cover function for read_initial_length.
19231 Returns the length of the object at BUF, and stores the size of the
19232 initial length in *BYTES_READ and stores the size that offsets will be in
19233 *OFFSET_SIZE.
19234 If the initial length size is not equivalent to that specified in
19235 CU_HEADER then issue a complaint.
19236 This is useful when reading non-comp-unit headers. */
dd373385 19237
c764a876 19238static LONGEST
d521ce57 19239read_checked_initial_length_and_offset (bfd *abfd, const gdb_byte *buf,
c764a876
DE
19240 const struct comp_unit_head *cu_header,
19241 unsigned int *bytes_read,
19242 unsigned int *offset_size)
19243{
19244 LONGEST length = read_initial_length (abfd, buf, bytes_read);
19245
19246 gdb_assert (cu_header->initial_length_size == 4
19247 || cu_header->initial_length_size == 8
19248 || cu_header->initial_length_size == 12);
19249
19250 if (cu_header->initial_length_size != *bytes_read)
b98664d3 19251 complaint (_("intermixed 32-bit and 64-bit DWARF sections"));
dd373385 19252
c764a876 19253 *offset_size = (*bytes_read == 4) ? 4 : 8;
dd373385 19254 return length;
613e1657
KB
19255}
19256
19257/* Read an offset from the data stream. The size of the offset is
917c78fc 19258 given by cu_header->offset_size. */
613e1657
KB
19259
19260static LONGEST
d521ce57
TT
19261read_offset (bfd *abfd, const gdb_byte *buf,
19262 const struct comp_unit_head *cu_header,
891d2f0b 19263 unsigned int *bytes_read)
c764a876
DE
19264{
19265 LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
9a619af0 19266
c764a876
DE
19267 *bytes_read = cu_header->offset_size;
19268 return offset;
19269}
19270
19271/* Read an offset from the data stream. */
19272
19273static LONGEST
d521ce57 19274read_offset_1 (bfd *abfd, const gdb_byte *buf, unsigned int offset_size)
613e1657
KB
19275{
19276 LONGEST retval = 0;
19277
c764a876 19278 switch (offset_size)
613e1657
KB
19279 {
19280 case 4:
fe1b8b76 19281 retval = bfd_get_32 (abfd, buf);
613e1657
KB
19282 break;
19283 case 8:
fe1b8b76 19284 retval = bfd_get_64 (abfd, buf);
613e1657
KB
19285 break;
19286 default:
8e65ff28 19287 internal_error (__FILE__, __LINE__,
c764a876 19288 _("read_offset_1: bad switch [in module %s]"),
659b0389 19289 bfd_get_filename (abfd));
613e1657
KB
19290 }
19291
917c78fc 19292 return retval;
613e1657
KB
19293}
19294
d521ce57
TT
19295static const gdb_byte *
19296read_n_bytes (bfd *abfd, const gdb_byte *buf, unsigned int size)
c906108c
SS
19297{
19298 /* If the size of a host char is 8 bits, we can return a pointer
19299 to the buffer, otherwise we have to copy the data to a buffer
19300 allocated on the temporary obstack. */
4bdf3d34 19301 gdb_assert (HOST_CHAR_BIT == 8);
c906108c 19302 return buf;
c906108c
SS
19303}
19304
d521ce57
TT
19305static const char *
19306read_direct_string (bfd *abfd, const gdb_byte *buf,
19307 unsigned int *bytes_read_ptr)
c906108c
SS
19308{
19309 /* If the size of a host char is 8 bits, we can return a pointer
19310 to the string, otherwise we have to copy the string to a buffer
19311 allocated on the temporary obstack. */
4bdf3d34 19312 gdb_assert (HOST_CHAR_BIT == 8);
c906108c
SS
19313 if (*buf == '\0')
19314 {
19315 *bytes_read_ptr = 1;
19316 return NULL;
19317 }
d521ce57
TT
19318 *bytes_read_ptr = strlen ((const char *) buf) + 1;
19319 return (const char *) buf;
4bdf3d34
JJ
19320}
19321
43988095
JK
19322/* Return pointer to string at section SECT offset STR_OFFSET with error
19323 reporting strings FORM_NAME and SECT_NAME. */
19324
d521ce57 19325static const char *
ed2dc618
SM
19326read_indirect_string_at_offset_from (struct objfile *objfile,
19327 bfd *abfd, LONGEST str_offset,
43988095
JK
19328 struct dwarf2_section_info *sect,
19329 const char *form_name,
19330 const char *sect_name)
19331{
ed2dc618 19332 dwarf2_read_section (objfile, sect);
43988095
JK
19333 if (sect->buffer == NULL)
19334 error (_("%s used without %s section [in module %s]"),
19335 form_name, sect_name, bfd_get_filename (abfd));
19336 if (str_offset >= sect->size)
19337 error (_("%s pointing outside of %s section [in module %s]"),
19338 form_name, sect_name, bfd_get_filename (abfd));
4bdf3d34 19339 gdb_assert (HOST_CHAR_BIT == 8);
43988095 19340 if (sect->buffer[str_offset] == '\0')
4bdf3d34 19341 return NULL;
43988095
JK
19342 return (const char *) (sect->buffer + str_offset);
19343}
19344
19345/* Return pointer to string at .debug_str offset STR_OFFSET. */
19346
19347static const char *
ed2dc618
SM
19348read_indirect_string_at_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
19349 bfd *abfd, LONGEST str_offset)
43988095 19350{
ed2dc618
SM
19351 return read_indirect_string_at_offset_from (dwarf2_per_objfile->objfile,
19352 abfd, str_offset,
43988095
JK
19353 &dwarf2_per_objfile->str,
19354 "DW_FORM_strp", ".debug_str");
19355}
19356
19357/* Return pointer to string at .debug_line_str offset STR_OFFSET. */
19358
19359static const char *
ed2dc618
SM
19360read_indirect_line_string_at_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
19361 bfd *abfd, LONGEST str_offset)
43988095 19362{
ed2dc618
SM
19363 return read_indirect_string_at_offset_from (dwarf2_per_objfile->objfile,
19364 abfd, str_offset,
43988095
JK
19365 &dwarf2_per_objfile->line_str,
19366 "DW_FORM_line_strp",
19367 ".debug_line_str");
c906108c
SS
19368}
19369
36586728
TT
19370/* Read a string at offset STR_OFFSET in the .debug_str section from
19371 the .dwz file DWZ. Throw an error if the offset is too large. If
19372 the string consists of a single NUL byte, return NULL; otherwise
19373 return a pointer to the string. */
19374
d521ce57 19375static const char *
ed2dc618
SM
19376read_indirect_string_from_dwz (struct objfile *objfile, struct dwz_file *dwz,
19377 LONGEST str_offset)
36586728 19378{
ed2dc618 19379 dwarf2_read_section (objfile, &dwz->str);
36586728
TT
19380
19381 if (dwz->str.buffer == NULL)
19382 error (_("DW_FORM_GNU_strp_alt used without .debug_str "
19383 "section [in module %s]"),
19384 bfd_get_filename (dwz->dwz_bfd));
19385 if (str_offset >= dwz->str.size)
19386 error (_("DW_FORM_GNU_strp_alt pointing outside of "
19387 ".debug_str section [in module %s]"),
19388 bfd_get_filename (dwz->dwz_bfd));
19389 gdb_assert (HOST_CHAR_BIT == 8);
19390 if (dwz->str.buffer[str_offset] == '\0')
19391 return NULL;
d521ce57 19392 return (const char *) (dwz->str.buffer + str_offset);
36586728
TT
19393}
19394
43988095
JK
19395/* Return pointer to string at .debug_str offset as read from BUF.
19396 BUF is assumed to be in a compilation unit described by CU_HEADER.
19397 Return *BYTES_READ_PTR count of bytes read from BUF. */
19398
d521ce57 19399static const char *
ed2dc618
SM
19400read_indirect_string (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *abfd,
19401 const gdb_byte *buf,
cf2c3c16
TT
19402 const struct comp_unit_head *cu_header,
19403 unsigned int *bytes_read_ptr)
19404{
19405 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
19406
ed2dc618 19407 return read_indirect_string_at_offset (dwarf2_per_objfile, abfd, str_offset);
cf2c3c16
TT
19408}
19409
43988095
JK
19410/* Return pointer to string at .debug_line_str offset as read from BUF.
19411 BUF is assumed to be in a compilation unit described by CU_HEADER.
19412 Return *BYTES_READ_PTR count of bytes read from BUF. */
19413
19414static const char *
ed2dc618
SM
19415read_indirect_line_string (struct dwarf2_per_objfile *dwarf2_per_objfile,
19416 bfd *abfd, const gdb_byte *buf,
43988095
JK
19417 const struct comp_unit_head *cu_header,
19418 unsigned int *bytes_read_ptr)
19419{
19420 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
19421
ed2dc618
SM
19422 return read_indirect_line_string_at_offset (dwarf2_per_objfile, abfd,
19423 str_offset);
43988095
JK
19424}
19425
19426ULONGEST
d521ce57 19427read_unsigned_leb128 (bfd *abfd, const gdb_byte *buf,
43988095 19428 unsigned int *bytes_read_ptr)
c906108c 19429{
12df843f 19430 ULONGEST result;
ce5d95e1 19431 unsigned int num_read;
870f88f7 19432 int shift;
c906108c
SS
19433 unsigned char byte;
19434
19435 result = 0;
19436 shift = 0;
19437 num_read = 0;
c906108c
SS
19438 while (1)
19439 {
fe1b8b76 19440 byte = bfd_get_8 (abfd, buf);
c906108c
SS
19441 buf++;
19442 num_read++;
12df843f 19443 result |= ((ULONGEST) (byte & 127) << shift);
c906108c
SS
19444 if ((byte & 128) == 0)
19445 {
19446 break;
19447 }
19448 shift += 7;
19449 }
19450 *bytes_read_ptr = num_read;
19451 return result;
19452}
19453
12df843f 19454static LONGEST
d521ce57
TT
19455read_signed_leb128 (bfd *abfd, const gdb_byte *buf,
19456 unsigned int *bytes_read_ptr)
c906108c 19457{
12df843f 19458 LONGEST result;
870f88f7 19459 int shift, num_read;
c906108c
SS
19460 unsigned char byte;
19461
19462 result = 0;
19463 shift = 0;
c906108c 19464 num_read = 0;
c906108c
SS
19465 while (1)
19466 {
fe1b8b76 19467 byte = bfd_get_8 (abfd, buf);
c906108c
SS
19468 buf++;
19469 num_read++;
12df843f 19470 result |= ((LONGEST) (byte & 127) << shift);
c906108c
SS
19471 shift += 7;
19472 if ((byte & 128) == 0)
19473 {
19474 break;
19475 }
19476 }
77e0b926 19477 if ((shift < 8 * sizeof (result)) && (byte & 0x40))
12df843f 19478 result |= -(((LONGEST) 1) << shift);
c906108c
SS
19479 *bytes_read_ptr = num_read;
19480 return result;
19481}
19482
3019eac3
DE
19483/* Given index ADDR_INDEX in .debug_addr, fetch the value.
19484 ADDR_BASE is the DW_AT_GNU_addr_base attribute or zero.
19485 ADDR_SIZE is the size of addresses from the CU header. */
19486
19487static CORE_ADDR
ed2dc618
SM
19488read_addr_index_1 (struct dwarf2_per_objfile *dwarf2_per_objfile,
19489 unsigned int addr_index, ULONGEST addr_base, int addr_size)
3019eac3
DE
19490{
19491 struct objfile *objfile = dwarf2_per_objfile->objfile;
19492 bfd *abfd = objfile->obfd;
19493 const gdb_byte *info_ptr;
19494
19495 dwarf2_read_section (objfile, &dwarf2_per_objfile->addr);
19496 if (dwarf2_per_objfile->addr.buffer == NULL)
19497 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
4262abfb 19498 objfile_name (objfile));
3019eac3
DE
19499 if (addr_base + addr_index * addr_size >= dwarf2_per_objfile->addr.size)
19500 error (_("DW_FORM_addr_index pointing outside of "
19501 ".debug_addr section [in module %s]"),
4262abfb 19502 objfile_name (objfile));
3019eac3
DE
19503 info_ptr = (dwarf2_per_objfile->addr.buffer
19504 + addr_base + addr_index * addr_size);
19505 if (addr_size == 4)
19506 return bfd_get_32 (abfd, info_ptr);
19507 else
19508 return bfd_get_64 (abfd, info_ptr);
19509}
19510
19511/* Given index ADDR_INDEX in .debug_addr, fetch the value. */
19512
19513static CORE_ADDR
19514read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
19515{
518817b3
SM
19516 return read_addr_index_1 (cu->per_cu->dwarf2_per_objfile, addr_index,
19517 cu->addr_base, cu->header.addr_size);
3019eac3
DE
19518}
19519
19520/* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
19521
19522static CORE_ADDR
d521ce57 19523read_addr_index_from_leb128 (struct dwarf2_cu *cu, const gdb_byte *info_ptr,
3019eac3
DE
19524 unsigned int *bytes_read)
19525{
518817b3 19526 bfd *abfd = cu->per_cu->dwarf2_per_objfile->objfile->obfd;
3019eac3
DE
19527 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
19528
19529 return read_addr_index (cu, addr_index);
19530}
19531
19532/* Data structure to pass results from dwarf2_read_addr_index_reader
19533 back to dwarf2_read_addr_index. */
19534
19535struct dwarf2_read_addr_index_data
19536{
19537 ULONGEST addr_base;
19538 int addr_size;
19539};
19540
19541/* die_reader_func for dwarf2_read_addr_index. */
19542
19543static void
19544dwarf2_read_addr_index_reader (const struct die_reader_specs *reader,
d521ce57 19545 const gdb_byte *info_ptr,
3019eac3
DE
19546 struct die_info *comp_unit_die,
19547 int has_children,
19548 void *data)
19549{
19550 struct dwarf2_cu *cu = reader->cu;
19551 struct dwarf2_read_addr_index_data *aidata =
19552 (struct dwarf2_read_addr_index_data *) data;
19553
19554 aidata->addr_base = cu->addr_base;
19555 aidata->addr_size = cu->header.addr_size;
19556}
19557
19558/* Given an index in .debug_addr, fetch the value.
19559 NOTE: This can be called during dwarf expression evaluation,
19560 long after the debug information has been read, and thus per_cu->cu
19561 may no longer exist. */
19562
19563CORE_ADDR
19564dwarf2_read_addr_index (struct dwarf2_per_cu_data *per_cu,
19565 unsigned int addr_index)
19566{
ed2dc618 19567 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
3019eac3
DE
19568 struct dwarf2_cu *cu = per_cu->cu;
19569 ULONGEST addr_base;
19570 int addr_size;
19571
3019eac3
DE
19572 /* We need addr_base and addr_size.
19573 If we don't have PER_CU->cu, we have to get it.
19574 Nasty, but the alternative is storing the needed info in PER_CU,
19575 which at this point doesn't seem justified: it's not clear how frequently
19576 it would get used and it would increase the size of every PER_CU.
19577 Entry points like dwarf2_per_cu_addr_size do a similar thing
19578 so we're not in uncharted territory here.
19579 Alas we need to be a bit more complicated as addr_base is contained
19580 in the DIE.
19581
19582 We don't need to read the entire CU(/TU).
19583 We just need the header and top level die.
a1b64ce1 19584
3019eac3 19585 IWBN to use the aging mechanism to let us lazily later discard the CU.
a1b64ce1 19586 For now we skip this optimization. */
3019eac3
DE
19587
19588 if (cu != NULL)
19589 {
19590 addr_base = cu->addr_base;
19591 addr_size = cu->header.addr_size;
19592 }
19593 else
19594 {
19595 struct dwarf2_read_addr_index_data aidata;
19596
a1b64ce1
DE
19597 /* Note: We can't use init_cutu_and_read_dies_simple here,
19598 we need addr_base. */
58f0c718 19599 init_cutu_and_read_dies (per_cu, NULL, 0, 0, false,
a1b64ce1 19600 dwarf2_read_addr_index_reader, &aidata);
3019eac3
DE
19601 addr_base = aidata.addr_base;
19602 addr_size = aidata.addr_size;
19603 }
19604
ed2dc618
SM
19605 return read_addr_index_1 (dwarf2_per_objfile, addr_index, addr_base,
19606 addr_size);
3019eac3
DE
19607}
19608
57d63ce2
DE
19609/* Given a DW_FORM_GNU_str_index, fetch the string.
19610 This is only used by the Fission support. */
3019eac3 19611
d521ce57 19612static const char *
342587c4 19613read_str_index (const struct die_reader_specs *reader, ULONGEST str_index)
3019eac3 19614{
ed2dc618 19615 struct dwarf2_cu *cu = reader->cu;
518817b3
SM
19616 struct dwarf2_per_objfile *dwarf2_per_objfile
19617 = cu->per_cu->dwarf2_per_objfile;
3019eac3 19618 struct objfile *objfile = dwarf2_per_objfile->objfile;
c5164cbc 19619 const char *objf_name = objfile_name (objfile);
3019eac3 19620 bfd *abfd = objfile->obfd;
73869dc2
DE
19621 struct dwarf2_section_info *str_section = &reader->dwo_file->sections.str;
19622 struct dwarf2_section_info *str_offsets_section =
19623 &reader->dwo_file->sections.str_offsets;
d521ce57 19624 const gdb_byte *info_ptr;
3019eac3 19625 ULONGEST str_offset;
57d63ce2 19626 static const char form_name[] = "DW_FORM_GNU_str_index";
3019eac3 19627
73869dc2
DE
19628 dwarf2_read_section (objfile, str_section);
19629 dwarf2_read_section (objfile, str_offsets_section);
19630 if (str_section->buffer == NULL)
57d63ce2 19631 error (_("%s used without .debug_str.dwo section"
9d8780f0
SM
19632 " in CU at offset %s [in module %s]"),
19633 form_name, sect_offset_str (cu->header.sect_off), objf_name);
73869dc2 19634 if (str_offsets_section->buffer == NULL)
57d63ce2 19635 error (_("%s used without .debug_str_offsets.dwo section"
9d8780f0
SM
19636 " in CU at offset %s [in module %s]"),
19637 form_name, sect_offset_str (cu->header.sect_off), objf_name);
73869dc2 19638 if (str_index * cu->header.offset_size >= str_offsets_section->size)
57d63ce2 19639 error (_("%s pointing outside of .debug_str_offsets.dwo"
9d8780f0
SM
19640 " section in CU at offset %s [in module %s]"),
19641 form_name, sect_offset_str (cu->header.sect_off), objf_name);
73869dc2 19642 info_ptr = (str_offsets_section->buffer
3019eac3
DE
19643 + str_index * cu->header.offset_size);
19644 if (cu->header.offset_size == 4)
19645 str_offset = bfd_get_32 (abfd, info_ptr);
19646 else
19647 str_offset = bfd_get_64 (abfd, info_ptr);
73869dc2 19648 if (str_offset >= str_section->size)
57d63ce2 19649 error (_("Offset from %s pointing outside of"
9d8780f0
SM
19650 " .debug_str.dwo section in CU at offset %s [in module %s]"),
19651 form_name, sect_offset_str (cu->header.sect_off), objf_name);
73869dc2 19652 return (const char *) (str_section->buffer + str_offset);
3019eac3
DE
19653}
19654
3019eac3
DE
19655/* Return the length of an LEB128 number in BUF. */
19656
19657static int
19658leb128_size (const gdb_byte *buf)
19659{
19660 const gdb_byte *begin = buf;
19661 gdb_byte byte;
19662
19663 while (1)
19664 {
19665 byte = *buf++;
19666 if ((byte & 128) == 0)
19667 return buf - begin;
19668 }
19669}
19670
c906108c 19671static void
e142c38c 19672set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
c906108c
SS
19673{
19674 switch (lang)
19675 {
19676 case DW_LANG_C89:
76bee0cc 19677 case DW_LANG_C99:
0cfd832f 19678 case DW_LANG_C11:
c906108c 19679 case DW_LANG_C:
d1be3247 19680 case DW_LANG_UPC:
e142c38c 19681 cu->language = language_c;
c906108c 19682 break;
9c37b5ae 19683 case DW_LANG_Java:
c906108c 19684 case DW_LANG_C_plus_plus:
0cfd832f
MW
19685 case DW_LANG_C_plus_plus_11:
19686 case DW_LANG_C_plus_plus_14:
e142c38c 19687 cu->language = language_cplus;
c906108c 19688 break;
6aecb9c2
JB
19689 case DW_LANG_D:
19690 cu->language = language_d;
19691 break;
c906108c
SS
19692 case DW_LANG_Fortran77:
19693 case DW_LANG_Fortran90:
b21b22e0 19694 case DW_LANG_Fortran95:
f7de9aab
MW
19695 case DW_LANG_Fortran03:
19696 case DW_LANG_Fortran08:
e142c38c 19697 cu->language = language_fortran;
c906108c 19698 break;
a766d390
DE
19699 case DW_LANG_Go:
19700 cu->language = language_go;
19701 break;
c906108c 19702 case DW_LANG_Mips_Assembler:
e142c38c 19703 cu->language = language_asm;
c906108c
SS
19704 break;
19705 case DW_LANG_Ada83:
8aaf0b47 19706 case DW_LANG_Ada95:
bc5f45f8
JB
19707 cu->language = language_ada;
19708 break;
72019c9c
GM
19709 case DW_LANG_Modula2:
19710 cu->language = language_m2;
19711 break;
fe8e67fd
PM
19712 case DW_LANG_Pascal83:
19713 cu->language = language_pascal;
19714 break;
22566fbd
DJ
19715 case DW_LANG_ObjC:
19716 cu->language = language_objc;
19717 break;
c44af4eb
TT
19718 case DW_LANG_Rust:
19719 case DW_LANG_Rust_old:
19720 cu->language = language_rust;
19721 break;
c906108c
SS
19722 case DW_LANG_Cobol74:
19723 case DW_LANG_Cobol85:
c906108c 19724 default:
e142c38c 19725 cu->language = language_minimal;
c906108c
SS
19726 break;
19727 }
e142c38c 19728 cu->language_defn = language_def (cu->language);
c906108c
SS
19729}
19730
19731/* Return the named attribute or NULL if not there. */
19732
19733static struct attribute *
e142c38c 19734dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
c906108c 19735{
a48e046c 19736 for (;;)
c906108c 19737 {
a48e046c
TT
19738 unsigned int i;
19739 struct attribute *spec = NULL;
19740
19741 for (i = 0; i < die->num_attrs; ++i)
19742 {
19743 if (die->attrs[i].name == name)
19744 return &die->attrs[i];
19745 if (die->attrs[i].name == DW_AT_specification
19746 || die->attrs[i].name == DW_AT_abstract_origin)
19747 spec = &die->attrs[i];
19748 }
19749
19750 if (!spec)
19751 break;
c906108c 19752
f2f0e013 19753 die = follow_die_ref (die, spec, &cu);
f2f0e013 19754 }
c5aa993b 19755
c906108c
SS
19756 return NULL;
19757}
19758
348e048f
DE
19759/* Return the named attribute or NULL if not there,
19760 but do not follow DW_AT_specification, etc.
19761 This is for use in contexts where we're reading .debug_types dies.
19762 Following DW_AT_specification, DW_AT_abstract_origin will take us
19763 back up the chain, and we want to go down. */
19764
19765static struct attribute *
45e58e77 19766dwarf2_attr_no_follow (struct die_info *die, unsigned int name)
348e048f
DE
19767{
19768 unsigned int i;
19769
19770 for (i = 0; i < die->num_attrs; ++i)
19771 if (die->attrs[i].name == name)
19772 return &die->attrs[i];
19773
19774 return NULL;
19775}
19776
7d45c7c3
KB
19777/* Return the string associated with a string-typed attribute, or NULL if it
19778 is either not found or is of an incorrect type. */
19779
19780static const char *
19781dwarf2_string_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
19782{
19783 struct attribute *attr;
19784 const char *str = NULL;
19785
19786 attr = dwarf2_attr (die, name, cu);
19787
19788 if (attr != NULL)
19789 {
43988095 19790 if (attr->form == DW_FORM_strp || attr->form == DW_FORM_line_strp
b3340438
L
19791 || attr->form == DW_FORM_string
19792 || attr->form == DW_FORM_GNU_str_index
16eb6b2d 19793 || attr->form == DW_FORM_GNU_strp_alt)
7d45c7c3
KB
19794 str = DW_STRING (attr);
19795 else
b98664d3 19796 complaint (_("string type expected for attribute %s for "
9d8780f0
SM
19797 "DIE at %s in module %s"),
19798 dwarf_attr_name (name), sect_offset_str (die->sect_off),
518817b3 19799 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
7d45c7c3
KB
19800 }
19801
19802 return str;
19803}
19804
05cf31d1
JB
19805/* Return non-zero iff the attribute NAME is defined for the given DIE,
19806 and holds a non-zero value. This function should only be used for
2dc7f7b3 19807 DW_FORM_flag or DW_FORM_flag_present attributes. */
05cf31d1
JB
19808
19809static int
19810dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
19811{
19812 struct attribute *attr = dwarf2_attr (die, name, cu);
19813
19814 return (attr && DW_UNSND (attr));
19815}
19816
3ca72b44 19817static int
e142c38c 19818die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
3ca72b44 19819{
05cf31d1
JB
19820 /* A DIE is a declaration if it has a DW_AT_declaration attribute
19821 which value is non-zero. However, we have to be careful with
19822 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
19823 (via dwarf2_flag_true_p) follows this attribute. So we may
19824 end up accidently finding a declaration attribute that belongs
19825 to a different DIE referenced by the specification attribute,
19826 even though the given DIE does not have a declaration attribute. */
19827 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
19828 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
3ca72b44
AC
19829}
19830
63d06c5c 19831/* Return the die giving the specification for DIE, if there is
f2f0e013 19832 one. *SPEC_CU is the CU containing DIE on input, and the CU
edb3359d
DJ
19833 containing the return value on output. If there is no
19834 specification, but there is an abstract origin, that is
19835 returned. */
63d06c5c
DC
19836
19837static struct die_info *
f2f0e013 19838die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
63d06c5c 19839{
f2f0e013
DJ
19840 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
19841 *spec_cu);
63d06c5c 19842
edb3359d
DJ
19843 if (spec_attr == NULL)
19844 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
19845
63d06c5c
DC
19846 if (spec_attr == NULL)
19847 return NULL;
19848 else
f2f0e013 19849 return follow_die_ref (die, spec_attr, spec_cu);
63d06c5c 19850}
c906108c 19851
527f3840
JK
19852/* Stub for free_line_header to match void * callback types. */
19853
19854static void
19855free_line_header_voidp (void *arg)
19856{
9a3c8263 19857 struct line_header *lh = (struct line_header *) arg;
527f3840 19858
fff8551c 19859 delete lh;
527f3840
JK
19860}
19861
fff8551c
PA
19862void
19863line_header::add_include_dir (const char *include_dir)
c906108c 19864{
27e0867f 19865 if (dwarf_line_debug >= 2)
fff8551c
PA
19866 fprintf_unfiltered (gdb_stdlog, "Adding dir %zu: %s\n",
19867 include_dirs.size () + 1, include_dir);
27e0867f 19868
fff8551c 19869 include_dirs.push_back (include_dir);
debd256d 19870}
6e70227d 19871
fff8551c
PA
19872void
19873line_header::add_file_name (const char *name,
ecfb656c 19874 dir_index d_index,
fff8551c
PA
19875 unsigned int mod_time,
19876 unsigned int length)
debd256d 19877{
27e0867f
DE
19878 if (dwarf_line_debug >= 2)
19879 fprintf_unfiltered (gdb_stdlog, "Adding file %u: %s\n",
fff8551c 19880 (unsigned) file_names.size () + 1, name);
27e0867f 19881
ecfb656c 19882 file_names.emplace_back (name, d_index, mod_time, length);
debd256d 19883}
6e70227d 19884
83769d0b 19885/* A convenience function to find the proper .debug_line section for a CU. */
36586728
TT
19886
19887static struct dwarf2_section_info *
19888get_debug_line_section (struct dwarf2_cu *cu)
19889{
19890 struct dwarf2_section_info *section;
518817b3
SM
19891 struct dwarf2_per_objfile *dwarf2_per_objfile
19892 = cu->per_cu->dwarf2_per_objfile;
36586728
TT
19893
19894 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
19895 DWO file. */
19896 if (cu->dwo_unit && cu->per_cu->is_debug_types)
19897 section = &cu->dwo_unit->dwo_file->sections.line;
19898 else if (cu->per_cu->is_dwz)
19899 {
ed2dc618 19900 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
36586728
TT
19901
19902 section = &dwz->line;
19903 }
19904 else
19905 section = &dwarf2_per_objfile->line;
19906
19907 return section;
19908}
19909
43988095
JK
19910/* Read directory or file name entry format, starting with byte of
19911 format count entries, ULEB128 pairs of entry formats, ULEB128 of
19912 entries count and the entries themselves in the described entry
19913 format. */
19914
19915static void
ed2dc618
SM
19916read_formatted_entries (struct dwarf2_per_objfile *dwarf2_per_objfile,
19917 bfd *abfd, const gdb_byte **bufp,
43988095
JK
19918 struct line_header *lh,
19919 const struct comp_unit_head *cu_header,
19920 void (*callback) (struct line_header *lh,
19921 const char *name,
ecfb656c 19922 dir_index d_index,
43988095
JK
19923 unsigned int mod_time,
19924 unsigned int length))
19925{
19926 gdb_byte format_count, formati;
19927 ULONGEST data_count, datai;
19928 const gdb_byte *buf = *bufp;
19929 const gdb_byte *format_header_data;
43988095
JK
19930 unsigned int bytes_read;
19931
19932 format_count = read_1_byte (abfd, buf);
19933 buf += 1;
19934 format_header_data = buf;
19935 for (formati = 0; formati < format_count; formati++)
19936 {
19937 read_unsigned_leb128 (abfd, buf, &bytes_read);
19938 buf += bytes_read;
19939 read_unsigned_leb128 (abfd, buf, &bytes_read);
19940 buf += bytes_read;
19941 }
19942
19943 data_count = read_unsigned_leb128 (abfd, buf, &bytes_read);
19944 buf += bytes_read;
19945 for (datai = 0; datai < data_count; datai++)
19946 {
19947 const gdb_byte *format = format_header_data;
19948 struct file_entry fe;
19949
43988095
JK
19950 for (formati = 0; formati < format_count; formati++)
19951 {
ecfb656c 19952 ULONGEST content_type = read_unsigned_leb128 (abfd, format, &bytes_read);
43988095 19953 format += bytes_read;
43988095 19954
ecfb656c 19955 ULONGEST form = read_unsigned_leb128 (abfd, format, &bytes_read);
43988095 19956 format += bytes_read;
ecfb656c
PA
19957
19958 gdb::optional<const char *> string;
19959 gdb::optional<unsigned int> uint;
19960
43988095
JK
19961 switch (form)
19962 {
19963 case DW_FORM_string:
ecfb656c 19964 string.emplace (read_direct_string (abfd, buf, &bytes_read));
43988095
JK
19965 buf += bytes_read;
19966 break;
19967
19968 case DW_FORM_line_strp:
ed2dc618
SM
19969 string.emplace (read_indirect_line_string (dwarf2_per_objfile,
19970 abfd, buf,
ecfb656c
PA
19971 cu_header,
19972 &bytes_read));
43988095
JK
19973 buf += bytes_read;
19974 break;
19975
19976 case DW_FORM_data1:
ecfb656c 19977 uint.emplace (read_1_byte (abfd, buf));
43988095
JK
19978 buf += 1;
19979 break;
19980
19981 case DW_FORM_data2:
ecfb656c 19982 uint.emplace (read_2_bytes (abfd, buf));
43988095
JK
19983 buf += 2;
19984 break;
19985
19986 case DW_FORM_data4:
ecfb656c 19987 uint.emplace (read_4_bytes (abfd, buf));
43988095
JK
19988 buf += 4;
19989 break;
19990
19991 case DW_FORM_data8:
ecfb656c 19992 uint.emplace (read_8_bytes (abfd, buf));
43988095
JK
19993 buf += 8;
19994 break;
19995
19996 case DW_FORM_udata:
ecfb656c 19997 uint.emplace (read_unsigned_leb128 (abfd, buf, &bytes_read));
43988095
JK
19998 buf += bytes_read;
19999 break;
20000
20001 case DW_FORM_block:
20002 /* It is valid only for DW_LNCT_timestamp which is ignored by
20003 current GDB. */
20004 break;
20005 }
ecfb656c
PA
20006
20007 switch (content_type)
20008 {
20009 case DW_LNCT_path:
20010 if (string.has_value ())
20011 fe.name = *string;
20012 break;
20013 case DW_LNCT_directory_index:
20014 if (uint.has_value ())
20015 fe.d_index = (dir_index) *uint;
20016 break;
20017 case DW_LNCT_timestamp:
20018 if (uint.has_value ())
20019 fe.mod_time = *uint;
20020 break;
20021 case DW_LNCT_size:
20022 if (uint.has_value ())
20023 fe.length = *uint;
20024 break;
20025 case DW_LNCT_MD5:
20026 break;
20027 default:
b98664d3 20028 complaint (_("Unknown format content type %s"),
ecfb656c
PA
20029 pulongest (content_type));
20030 }
43988095
JK
20031 }
20032
ecfb656c 20033 callback (lh, fe.name, fe.d_index, fe.mod_time, fe.length);
43988095
JK
20034 }
20035
20036 *bufp = buf;
20037}
20038
debd256d 20039/* Read the statement program header starting at OFFSET in
3019eac3 20040 .debug_line, or .debug_line.dwo. Return a pointer
6502dd73 20041 to a struct line_header, allocated using xmalloc.
cd366ee8
DE
20042 Returns NULL if there is a problem reading the header, e.g., if it
20043 has a version we don't understand.
debd256d
JB
20044
20045 NOTE: the strings in the include directory and file name tables of
3019eac3
DE
20046 the returned object point into the dwarf line section buffer,
20047 and must not be freed. */
ae2de4f8 20048
fff8551c 20049static line_header_up
9c541725 20050dwarf_decode_line_header (sect_offset sect_off, struct dwarf2_cu *cu)
debd256d 20051{
d521ce57 20052 const gdb_byte *line_ptr;
c764a876 20053 unsigned int bytes_read, offset_size;
debd256d 20054 int i;
d521ce57 20055 const char *cur_dir, *cur_file;
3019eac3
DE
20056 struct dwarf2_section_info *section;
20057 bfd *abfd;
518817b3
SM
20058 struct dwarf2_per_objfile *dwarf2_per_objfile
20059 = cu->per_cu->dwarf2_per_objfile;
3019eac3 20060
36586728 20061 section = get_debug_line_section (cu);
3019eac3
DE
20062 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
20063 if (section->buffer == NULL)
debd256d 20064 {
3019eac3 20065 if (cu->dwo_unit && cu->per_cu->is_debug_types)
b98664d3 20066 complaint (_("missing .debug_line.dwo section"));
3019eac3 20067 else
b98664d3 20068 complaint (_("missing .debug_line section"));
debd256d
JB
20069 return 0;
20070 }
20071
fceca515
DE
20072 /* We can't do this until we know the section is non-empty.
20073 Only then do we know we have such a section. */
a32a8923 20074 abfd = get_section_bfd_owner (section);
fceca515 20075
a738430d
MK
20076 /* Make sure that at least there's room for the total_length field.
20077 That could be 12 bytes long, but we're just going to fudge that. */
9c541725 20078 if (to_underlying (sect_off) + 4 >= section->size)
debd256d 20079 {
4d3c2250 20080 dwarf2_statement_list_fits_in_line_number_section_complaint ();
debd256d
JB
20081 return 0;
20082 }
20083
fff8551c 20084 line_header_up lh (new line_header ());
debd256d 20085
9c541725 20086 lh->sect_off = sect_off;
527f3840
JK
20087 lh->offset_in_dwz = cu->per_cu->is_dwz;
20088
9c541725 20089 line_ptr = section->buffer + to_underlying (sect_off);
debd256d 20090
a738430d 20091 /* Read in the header. */
6e70227d 20092 lh->total_length =
c764a876
DE
20093 read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
20094 &bytes_read, &offset_size);
debd256d 20095 line_ptr += bytes_read;
3019eac3 20096 if (line_ptr + lh->total_length > (section->buffer + section->size))
debd256d 20097 {
4d3c2250 20098 dwarf2_statement_list_fits_in_line_number_section_complaint ();
debd256d
JB
20099 return 0;
20100 }
20101 lh->statement_program_end = line_ptr + lh->total_length;
20102 lh->version = read_2_bytes (abfd, line_ptr);
20103 line_ptr += 2;
43988095 20104 if (lh->version > 5)
cd366ee8
DE
20105 {
20106 /* This is a version we don't understand. The format could have
20107 changed in ways we don't handle properly so just punt. */
b98664d3 20108 complaint (_("unsupported version in .debug_line section"));
cd366ee8
DE
20109 return NULL;
20110 }
43988095
JK
20111 if (lh->version >= 5)
20112 {
20113 gdb_byte segment_selector_size;
20114
20115 /* Skip address size. */
20116 read_1_byte (abfd, line_ptr);
20117 line_ptr += 1;
20118
20119 segment_selector_size = read_1_byte (abfd, line_ptr);
20120 line_ptr += 1;
20121 if (segment_selector_size != 0)
20122 {
b98664d3 20123 complaint (_("unsupported segment selector size %u "
43988095
JK
20124 "in .debug_line section"),
20125 segment_selector_size);
20126 return NULL;
20127 }
20128 }
c764a876
DE
20129 lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
20130 line_ptr += offset_size;
debd256d
JB
20131 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
20132 line_ptr += 1;
2dc7f7b3
TT
20133 if (lh->version >= 4)
20134 {
20135 lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
20136 line_ptr += 1;
20137 }
20138 else
20139 lh->maximum_ops_per_instruction = 1;
20140
20141 if (lh->maximum_ops_per_instruction == 0)
20142 {
20143 lh->maximum_ops_per_instruction = 1;
b98664d3 20144 complaint (_("invalid maximum_ops_per_instruction "
3e43a32a 20145 "in `.debug_line' section"));
2dc7f7b3
TT
20146 }
20147
debd256d
JB
20148 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
20149 line_ptr += 1;
20150 lh->line_base = read_1_signed_byte (abfd, line_ptr);
20151 line_ptr += 1;
20152 lh->line_range = read_1_byte (abfd, line_ptr);
20153 line_ptr += 1;
20154 lh->opcode_base = read_1_byte (abfd, line_ptr);
20155 line_ptr += 1;
fff8551c 20156 lh->standard_opcode_lengths.reset (new unsigned char[lh->opcode_base]);
debd256d
JB
20157
20158 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
20159 for (i = 1; i < lh->opcode_base; ++i)
20160 {
20161 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
20162 line_ptr += 1;
20163 }
20164
43988095 20165 if (lh->version >= 5)
debd256d 20166 {
43988095 20167 /* Read directory table. */
ed2dc618
SM
20168 read_formatted_entries (dwarf2_per_objfile, abfd, &line_ptr, lh.get (),
20169 &cu->header,
fff8551c 20170 [] (struct line_header *lh, const char *name,
ecfb656c 20171 dir_index d_index, unsigned int mod_time,
fff8551c
PA
20172 unsigned int length)
20173 {
20174 lh->add_include_dir (name);
20175 });
debd256d 20176
43988095 20177 /* Read file name table. */
ed2dc618
SM
20178 read_formatted_entries (dwarf2_per_objfile, abfd, &line_ptr, lh.get (),
20179 &cu->header,
fff8551c 20180 [] (struct line_header *lh, const char *name,
ecfb656c 20181 dir_index d_index, unsigned int mod_time,
fff8551c
PA
20182 unsigned int length)
20183 {
ecfb656c 20184 lh->add_file_name (name, d_index, mod_time, length);
fff8551c 20185 });
43988095
JK
20186 }
20187 else
debd256d 20188 {
43988095
JK
20189 /* Read directory table. */
20190 while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
20191 {
20192 line_ptr += bytes_read;
fff8551c 20193 lh->add_include_dir (cur_dir);
43988095 20194 }
debd256d
JB
20195 line_ptr += bytes_read;
20196
43988095
JK
20197 /* Read file name table. */
20198 while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
20199 {
ecfb656c
PA
20200 unsigned int mod_time, length;
20201 dir_index d_index;
43988095
JK
20202
20203 line_ptr += bytes_read;
ecfb656c 20204 d_index = (dir_index) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
43988095
JK
20205 line_ptr += bytes_read;
20206 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20207 line_ptr += bytes_read;
20208 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20209 line_ptr += bytes_read;
20210
ecfb656c 20211 lh->add_file_name (cur_file, d_index, mod_time, length);
43988095
JK
20212 }
20213 line_ptr += bytes_read;
debd256d 20214 }
6e70227d 20215 lh->statement_program_start = line_ptr;
debd256d 20216
3019eac3 20217 if (line_ptr > (section->buffer + section->size))
b98664d3 20218 complaint (_("line number info header doesn't "
3e43a32a 20219 "fit in `.debug_line' section"));
debd256d 20220
debd256d
JB
20221 return lh;
20222}
c906108c 20223
c6da4cef
DE
20224/* Subroutine of dwarf_decode_lines to simplify it.
20225 Return the file name of the psymtab for included file FILE_INDEX
20226 in line header LH of PST.
20227 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
c89b44cd
TT
20228 If space for the result is malloc'd, *NAME_HOLDER will be set.
20229 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
c6da4cef 20230
d521ce57 20231static const char *
c6da4cef
DE
20232psymtab_include_file_name (const struct line_header *lh, int file_index,
20233 const struct partial_symtab *pst,
c89b44cd
TT
20234 const char *comp_dir,
20235 gdb::unique_xmalloc_ptr<char> *name_holder)
c6da4cef 20236{
8c43009f 20237 const file_entry &fe = lh->file_names[file_index];
d521ce57
TT
20238 const char *include_name = fe.name;
20239 const char *include_name_to_compare = include_name;
72b9f47f 20240 const char *pst_filename;
c6da4cef
DE
20241 int file_is_pst;
20242
8c43009f 20243 const char *dir_name = fe.include_dir (lh);
c6da4cef 20244
c89b44cd 20245 gdb::unique_xmalloc_ptr<char> hold_compare;
c6da4cef
DE
20246 if (!IS_ABSOLUTE_PATH (include_name)
20247 && (dir_name != NULL || comp_dir != NULL))
20248 {
20249 /* Avoid creating a duplicate psymtab for PST.
20250 We do this by comparing INCLUDE_NAME and PST_FILENAME.
20251 Before we do the comparison, however, we need to account
20252 for DIR_NAME and COMP_DIR.
20253 First prepend dir_name (if non-NULL). If we still don't
20254 have an absolute path prepend comp_dir (if non-NULL).
20255 However, the directory we record in the include-file's
20256 psymtab does not contain COMP_DIR (to match the
20257 corresponding symtab(s)).
20258
20259 Example:
20260
20261 bash$ cd /tmp
20262 bash$ gcc -g ./hello.c
20263 include_name = "hello.c"
20264 dir_name = "."
20265 DW_AT_comp_dir = comp_dir = "/tmp"
5f52445b
YQ
20266 DW_AT_name = "./hello.c"
20267
20268 */
c6da4cef
DE
20269
20270 if (dir_name != NULL)
20271 {
c89b44cd
TT
20272 name_holder->reset (concat (dir_name, SLASH_STRING,
20273 include_name, (char *) NULL));
20274 include_name = name_holder->get ();
c6da4cef 20275 include_name_to_compare = include_name;
c6da4cef
DE
20276 }
20277 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
20278 {
c89b44cd
TT
20279 hold_compare.reset (concat (comp_dir, SLASH_STRING,
20280 include_name, (char *) NULL));
20281 include_name_to_compare = hold_compare.get ();
c6da4cef
DE
20282 }
20283 }
20284
20285 pst_filename = pst->filename;
c89b44cd 20286 gdb::unique_xmalloc_ptr<char> copied_name;
c6da4cef
DE
20287 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
20288 {
c89b44cd
TT
20289 copied_name.reset (concat (pst->dirname, SLASH_STRING,
20290 pst_filename, (char *) NULL));
20291 pst_filename = copied_name.get ();
c6da4cef
DE
20292 }
20293
1e3fad37 20294 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
c6da4cef 20295
c6da4cef
DE
20296 if (file_is_pst)
20297 return NULL;
20298 return include_name;
20299}
20300
d9b3de22
DE
20301/* State machine to track the state of the line number program. */
20302
6f77053d 20303class lnp_state_machine
d9b3de22 20304{
6f77053d
PA
20305public:
20306 /* Initialize a machine state for the start of a line number
20307 program. */
804d2729
TT
20308 lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch, line_header *lh,
20309 bool record_lines_p);
6f77053d 20310
8c43009f
PA
20311 file_entry *current_file ()
20312 {
20313 /* lh->file_names is 0-based, but the file name numbers in the
20314 statement program are 1-based. */
6f77053d
PA
20315 return m_line_header->file_name_at (m_file);
20316 }
20317
20318 /* Record the line in the state machine. END_SEQUENCE is true if
20319 we're processing the end of a sequence. */
20320 void record_line (bool end_sequence);
20321
7ab6656f
OJ
20322 /* Check ADDRESS is zero and less than UNRELOCATED_LOWPC and if true
20323 nop-out rest of the lines in this sequence. */
6f77053d
PA
20324 void check_line_address (struct dwarf2_cu *cu,
20325 const gdb_byte *line_ptr,
7ab6656f 20326 CORE_ADDR unrelocated_lowpc, CORE_ADDR address);
6f77053d
PA
20327
20328 void handle_set_discriminator (unsigned int discriminator)
20329 {
20330 m_discriminator = discriminator;
20331 m_line_has_non_zero_discriminator |= discriminator != 0;
20332 }
20333
20334 /* Handle DW_LNE_set_address. */
20335 void handle_set_address (CORE_ADDR baseaddr, CORE_ADDR address)
20336 {
20337 m_op_index = 0;
20338 address += baseaddr;
20339 m_address = gdbarch_adjust_dwarf2_line (m_gdbarch, address, false);
20340 }
20341
20342 /* Handle DW_LNS_advance_pc. */
20343 void handle_advance_pc (CORE_ADDR adjust);
20344
20345 /* Handle a special opcode. */
20346 void handle_special_opcode (unsigned char op_code);
20347
20348 /* Handle DW_LNS_advance_line. */
20349 void handle_advance_line (int line_delta)
20350 {
20351 advance_line (line_delta);
20352 }
20353
20354 /* Handle DW_LNS_set_file. */
20355 void handle_set_file (file_name_index file);
20356
20357 /* Handle DW_LNS_negate_stmt. */
20358 void handle_negate_stmt ()
20359 {
20360 m_is_stmt = !m_is_stmt;
20361 }
20362
20363 /* Handle DW_LNS_const_add_pc. */
20364 void handle_const_add_pc ();
20365
20366 /* Handle DW_LNS_fixed_advance_pc. */
20367 void handle_fixed_advance_pc (CORE_ADDR addr_adj)
20368 {
20369 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20370 m_op_index = 0;
20371 }
20372
20373 /* Handle DW_LNS_copy. */
20374 void handle_copy ()
20375 {
20376 record_line (false);
20377 m_discriminator = 0;
20378 }
20379
20380 /* Handle DW_LNE_end_sequence. */
20381 void handle_end_sequence ()
20382 {
804d2729 20383 m_currently_recording_lines = true;
6f77053d
PA
20384 }
20385
20386private:
20387 /* Advance the line by LINE_DELTA. */
20388 void advance_line (int line_delta)
20389 {
20390 m_line += line_delta;
20391
20392 if (line_delta != 0)
20393 m_line_has_non_zero_discriminator = m_discriminator != 0;
8c43009f
PA
20394 }
20395
804d2729
TT
20396 struct dwarf2_cu *m_cu;
20397
6f77053d
PA
20398 gdbarch *m_gdbarch;
20399
20400 /* True if we're recording lines.
20401 Otherwise we're building partial symtabs and are just interested in
20402 finding include files mentioned by the line number program. */
20403 bool m_record_lines_p;
20404
8c43009f 20405 /* The line number header. */
6f77053d 20406 line_header *m_line_header;
8c43009f 20407
6f77053d
PA
20408 /* These are part of the standard DWARF line number state machine,
20409 and initialized according to the DWARF spec. */
d9b3de22 20410
6f77053d 20411 unsigned char m_op_index = 0;
8c43009f 20412 /* The line table index (1-based) of the current file. */
6f77053d
PA
20413 file_name_index m_file = (file_name_index) 1;
20414 unsigned int m_line = 1;
20415
20416 /* These are initialized in the constructor. */
20417
20418 CORE_ADDR m_address;
20419 bool m_is_stmt;
20420 unsigned int m_discriminator;
d9b3de22
DE
20421
20422 /* Additional bits of state we need to track. */
20423
20424 /* The last file that we called dwarf2_start_subfile for.
20425 This is only used for TLLs. */
6f77053d 20426 unsigned int m_last_file = 0;
d9b3de22 20427 /* The last file a line number was recorded for. */
6f77053d 20428 struct subfile *m_last_subfile = NULL;
d9b3de22 20429
804d2729
TT
20430 /* When true, record the lines we decode. */
20431 bool m_currently_recording_lines = false;
d9b3de22
DE
20432
20433 /* The last line number that was recorded, used to coalesce
20434 consecutive entries for the same line. This can happen, for
20435 example, when discriminators are present. PR 17276. */
6f77053d
PA
20436 unsigned int m_last_line = 0;
20437 bool m_line_has_non_zero_discriminator = false;
8c43009f 20438};
d9b3de22 20439
6f77053d
PA
20440void
20441lnp_state_machine::handle_advance_pc (CORE_ADDR adjust)
20442{
20443 CORE_ADDR addr_adj = (((m_op_index + adjust)
20444 / m_line_header->maximum_ops_per_instruction)
20445 * m_line_header->minimum_instruction_length);
20446 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20447 m_op_index = ((m_op_index + adjust)
20448 % m_line_header->maximum_ops_per_instruction);
20449}
d9b3de22 20450
6f77053d
PA
20451void
20452lnp_state_machine::handle_special_opcode (unsigned char op_code)
d9b3de22 20453{
6f77053d
PA
20454 unsigned char adj_opcode = op_code - m_line_header->opcode_base;
20455 CORE_ADDR addr_adj = (((m_op_index
20456 + (adj_opcode / m_line_header->line_range))
20457 / m_line_header->maximum_ops_per_instruction)
20458 * m_line_header->minimum_instruction_length);
20459 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20460 m_op_index = ((m_op_index + (adj_opcode / m_line_header->line_range))
20461 % m_line_header->maximum_ops_per_instruction);
d9b3de22 20462
6f77053d
PA
20463 int line_delta = (m_line_header->line_base
20464 + (adj_opcode % m_line_header->line_range));
20465 advance_line (line_delta);
20466 record_line (false);
20467 m_discriminator = 0;
20468}
d9b3de22 20469
6f77053d
PA
20470void
20471lnp_state_machine::handle_set_file (file_name_index file)
20472{
20473 m_file = file;
20474
20475 const file_entry *fe = current_file ();
20476 if (fe == NULL)
20477 dwarf2_debug_line_missing_file_complaint ();
20478 else if (m_record_lines_p)
20479 {
20480 const char *dir = fe->include_dir (m_line_header);
20481
804d2729 20482 m_last_subfile = m_cu->builder->get_current_subfile ();
6f77053d 20483 m_line_has_non_zero_discriminator = m_discriminator != 0;
804d2729 20484 dwarf2_start_subfile (m_cu, fe->name, dir);
6f77053d
PA
20485 }
20486}
20487
20488void
20489lnp_state_machine::handle_const_add_pc ()
20490{
20491 CORE_ADDR adjust
20492 = (255 - m_line_header->opcode_base) / m_line_header->line_range;
20493
20494 CORE_ADDR addr_adj
20495 = (((m_op_index + adjust)
20496 / m_line_header->maximum_ops_per_instruction)
20497 * m_line_header->minimum_instruction_length);
20498
20499 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20500 m_op_index = ((m_op_index + adjust)
20501 % m_line_header->maximum_ops_per_instruction);
20502}
d9b3de22 20503
a05a36a5
DE
20504/* Return non-zero if we should add LINE to the line number table.
20505 LINE is the line to add, LAST_LINE is the last line that was added,
20506 LAST_SUBFILE is the subfile for LAST_LINE.
20507 LINE_HAS_NON_ZERO_DISCRIMINATOR is non-zero if LINE has ever
20508 had a non-zero discriminator.
20509
20510 We have to be careful in the presence of discriminators.
20511 E.g., for this line:
20512
20513 for (i = 0; i < 100000; i++);
20514
20515 clang can emit four line number entries for that one line,
20516 each with a different discriminator.
20517 See gdb.dwarf2/dw2-single-line-discriminators.exp for an example.
20518
20519 However, we want gdb to coalesce all four entries into one.
20520 Otherwise the user could stepi into the middle of the line and
20521 gdb would get confused about whether the pc really was in the
20522 middle of the line.
20523
20524 Things are further complicated by the fact that two consecutive
20525 line number entries for the same line is a heuristic used by gcc
20526 to denote the end of the prologue. So we can't just discard duplicate
20527 entries, we have to be selective about it. The heuristic we use is
20528 that we only collapse consecutive entries for the same line if at least
20529 one of those entries has a non-zero discriminator. PR 17276.
20530
20531 Note: Addresses in the line number state machine can never go backwards
20532 within one sequence, thus this coalescing is ok. */
20533
20534static int
804d2729
TT
20535dwarf_record_line_p (struct dwarf2_cu *cu,
20536 unsigned int line, unsigned int last_line,
a05a36a5
DE
20537 int line_has_non_zero_discriminator,
20538 struct subfile *last_subfile)
20539{
804d2729 20540 if (cu->builder->get_current_subfile () != last_subfile)
a05a36a5
DE
20541 return 1;
20542 if (line != last_line)
20543 return 1;
20544 /* Same line for the same file that we've seen already.
20545 As a last check, for pr 17276, only record the line if the line
20546 has never had a non-zero discriminator. */
20547 if (!line_has_non_zero_discriminator)
20548 return 1;
20549 return 0;
20550}
20551
804d2729
TT
20552/* Use the CU's builder to record line number LINE beginning at
20553 address ADDRESS in the line table of subfile SUBFILE. */
252a6764
DE
20554
20555static void
d9b3de22
DE
20556dwarf_record_line_1 (struct gdbarch *gdbarch, struct subfile *subfile,
20557 unsigned int line, CORE_ADDR address,
804d2729 20558 struct dwarf2_cu *cu)
252a6764
DE
20559{
20560 CORE_ADDR addr = gdbarch_addr_bits_remove (gdbarch, address);
20561
27e0867f
DE
20562 if (dwarf_line_debug)
20563 {
20564 fprintf_unfiltered (gdb_stdlog,
20565 "Recording line %u, file %s, address %s\n",
20566 line, lbasename (subfile->name),
20567 paddress (gdbarch, address));
20568 }
20569
804d2729
TT
20570 if (cu != nullptr)
20571 cu->builder->record_line (subfile, line, addr);
252a6764
DE
20572}
20573
20574/* Subroutine of dwarf_decode_lines_1 to simplify it.
20575 Mark the end of a set of line number records.
d9b3de22 20576 The arguments are the same as for dwarf_record_line_1.
252a6764
DE
20577 If SUBFILE is NULL the request is ignored. */
20578
20579static void
20580dwarf_finish_line (struct gdbarch *gdbarch, struct subfile *subfile,
804d2729 20581 CORE_ADDR address, struct dwarf2_cu *cu)
252a6764 20582{
27e0867f
DE
20583 if (subfile == NULL)
20584 return;
20585
20586 if (dwarf_line_debug)
20587 {
20588 fprintf_unfiltered (gdb_stdlog,
20589 "Finishing current line, file %s, address %s\n",
20590 lbasename (subfile->name),
20591 paddress (gdbarch, address));
20592 }
20593
804d2729 20594 dwarf_record_line_1 (gdbarch, subfile, 0, address, cu);
d9b3de22
DE
20595}
20596
6f77053d
PA
20597void
20598lnp_state_machine::record_line (bool end_sequence)
d9b3de22 20599{
d9b3de22
DE
20600 if (dwarf_line_debug)
20601 {
20602 fprintf_unfiltered (gdb_stdlog,
20603 "Processing actual line %u: file %u,"
20604 " address %s, is_stmt %u, discrim %u\n",
6f77053d
PA
20605 m_line, to_underlying (m_file),
20606 paddress (m_gdbarch, m_address),
20607 m_is_stmt, m_discriminator);
d9b3de22
DE
20608 }
20609
6f77053d 20610 file_entry *fe = current_file ();
8c43009f
PA
20611
20612 if (fe == NULL)
d9b3de22
DE
20613 dwarf2_debug_line_missing_file_complaint ();
20614 /* For now we ignore lines not starting on an instruction boundary.
20615 But not when processing end_sequence for compatibility with the
20616 previous version of the code. */
6f77053d 20617 else if (m_op_index == 0 || end_sequence)
d9b3de22 20618 {
8c43009f 20619 fe->included_p = 1;
6f77053d 20620 if (m_record_lines_p && m_is_stmt)
d9b3de22 20621 {
804d2729
TT
20622 if (m_last_subfile != m_cu->builder->get_current_subfile ()
20623 || end_sequence)
d9b3de22 20624 {
804d2729
TT
20625 dwarf_finish_line (m_gdbarch, m_last_subfile, m_address,
20626 m_currently_recording_lines ? m_cu : nullptr);
d9b3de22
DE
20627 }
20628
20629 if (!end_sequence)
20630 {
804d2729 20631 if (dwarf_record_line_p (m_cu, m_line, m_last_line,
6f77053d
PA
20632 m_line_has_non_zero_discriminator,
20633 m_last_subfile))
d9b3de22 20634 {
804d2729
TT
20635 dwarf_record_line_1 (m_gdbarch,
20636 m_cu->builder->get_current_subfile (),
6f77053d 20637 m_line, m_address,
804d2729 20638 m_currently_recording_lines ? m_cu : nullptr);
d9b3de22 20639 }
804d2729 20640 m_last_subfile = m_cu->builder->get_current_subfile ();
6f77053d 20641 m_last_line = m_line;
d9b3de22
DE
20642 }
20643 }
20644 }
20645}
20646
804d2729
TT
20647lnp_state_machine::lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch,
20648 line_header *lh, bool record_lines_p)
d9b3de22 20649{
804d2729 20650 m_cu = cu;
6f77053d
PA
20651 m_gdbarch = arch;
20652 m_record_lines_p = record_lines_p;
20653 m_line_header = lh;
d9b3de22 20654
804d2729 20655 m_currently_recording_lines = true;
d9b3de22 20656
d9b3de22
DE
20657 /* Call `gdbarch_adjust_dwarf2_line' on the initial 0 address as if there
20658 was a line entry for it so that the backend has a chance to adjust it
20659 and also record it in case it needs it. This is currently used by MIPS
20660 code, cf. `mips_adjust_dwarf2_line'. */
6f77053d
PA
20661 m_address = gdbarch_adjust_dwarf2_line (arch, 0, 0);
20662 m_is_stmt = lh->default_is_stmt;
20663 m_discriminator = 0;
252a6764
DE
20664}
20665
6f77053d
PA
20666void
20667lnp_state_machine::check_line_address (struct dwarf2_cu *cu,
20668 const gdb_byte *line_ptr,
7ab6656f 20669 CORE_ADDR unrelocated_lowpc, CORE_ADDR address)
924c2928 20670{
7ab6656f
OJ
20671 /* If ADDRESS < UNRELOCATED_LOWPC then it's not a usable value, it's outside
20672 the pc range of the CU. However, we restrict the test to only ADDRESS
20673 values of zero to preserve GDB's previous behaviour which is to handle
20674 the specific case of a function being GC'd by the linker. */
924c2928 20675
7ab6656f 20676 if (address == 0 && address < unrelocated_lowpc)
924c2928
DE
20677 {
20678 /* This line table is for a function which has been
20679 GCd by the linker. Ignore it. PR gdb/12528 */
20680
518817b3 20681 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
924c2928
DE
20682 long line_offset = line_ptr - get_debug_line_section (cu)->buffer;
20683
b98664d3 20684 complaint (_(".debug_line address at offset 0x%lx is 0 [in module %s]"),
924c2928 20685 line_offset, objfile_name (objfile));
804d2729
TT
20686 m_currently_recording_lines = false;
20687 /* Note: m_currently_recording_lines is left as false until we see
20688 DW_LNE_end_sequence. */
924c2928
DE
20689 }
20690}
20691
f3f5162e 20692/* Subroutine of dwarf_decode_lines to simplify it.
d9b3de22
DE
20693 Process the line number information in LH.
20694 If DECODE_FOR_PST_P is non-zero, all we do is process the line number
20695 program in order to set included_p for every referenced header. */
debd256d 20696
c906108c 20697static void
43f3e411
DE
20698dwarf_decode_lines_1 (struct line_header *lh, struct dwarf2_cu *cu,
20699 const int decode_for_pst_p, CORE_ADDR lowpc)
c906108c 20700{
d521ce57
TT
20701 const gdb_byte *line_ptr, *extended_end;
20702 const gdb_byte *line_end;
a8c50c1f 20703 unsigned int bytes_read, extended_len;
699ca60a 20704 unsigned char op_code, extended_op;
e142c38c 20705 CORE_ADDR baseaddr;
518817b3 20706 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
f3f5162e 20707 bfd *abfd = objfile->obfd;
fbf65064 20708 struct gdbarch *gdbarch = get_objfile_arch (objfile);
6f77053d
PA
20709 /* True if we're recording line info (as opposed to building partial
20710 symtabs and just interested in finding include files mentioned by
20711 the line number program). */
20712 bool record_lines_p = !decode_for_pst_p;
e142c38c
DJ
20713
20714 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 20715
debd256d
JB
20716 line_ptr = lh->statement_program_start;
20717 line_end = lh->statement_program_end;
c906108c
SS
20718
20719 /* Read the statement sequences until there's nothing left. */
20720 while (line_ptr < line_end)
20721 {
6f77053d
PA
20722 /* The DWARF line number program state machine. Reset the state
20723 machine at the start of each sequence. */
804d2729 20724 lnp_state_machine state_machine (cu, gdbarch, lh, record_lines_p);
6f77053d 20725 bool end_sequence = false;
d9b3de22 20726
8c43009f 20727 if (record_lines_p)
c906108c 20728 {
8c43009f
PA
20729 /* Start a subfile for the current file of the state
20730 machine. */
20731 const file_entry *fe = state_machine.current_file ();
20732
20733 if (fe != NULL)
804d2729 20734 dwarf2_start_subfile (cu, fe->name, fe->include_dir (lh));
c906108c
SS
20735 }
20736
a738430d 20737 /* Decode the table. */
d9b3de22 20738 while (line_ptr < line_end && !end_sequence)
c906108c
SS
20739 {
20740 op_code = read_1_byte (abfd, line_ptr);
20741 line_ptr += 1;
9aa1fe7e 20742
debd256d 20743 if (op_code >= lh->opcode_base)
6e70227d 20744 {
8e07a239 20745 /* Special opcode. */
6f77053d 20746 state_machine.handle_special_opcode (op_code);
9aa1fe7e
GK
20747 }
20748 else switch (op_code)
c906108c
SS
20749 {
20750 case DW_LNS_extended_op:
3e43a32a
MS
20751 extended_len = read_unsigned_leb128 (abfd, line_ptr,
20752 &bytes_read);
473b7be6 20753 line_ptr += bytes_read;
a8c50c1f 20754 extended_end = line_ptr + extended_len;
c906108c
SS
20755 extended_op = read_1_byte (abfd, line_ptr);
20756 line_ptr += 1;
20757 switch (extended_op)
20758 {
20759 case DW_LNE_end_sequence:
6f77053d
PA
20760 state_machine.handle_end_sequence ();
20761 end_sequence = true;
c906108c
SS
20762 break;
20763 case DW_LNE_set_address:
d9b3de22
DE
20764 {
20765 CORE_ADDR address
20766 = read_address (abfd, line_ptr, cu, &bytes_read);
d9b3de22 20767 line_ptr += bytes_read;
6f77053d
PA
20768
20769 state_machine.check_line_address (cu, line_ptr,
7ab6656f 20770 lowpc - baseaddr, address);
6f77053d 20771 state_machine.handle_set_address (baseaddr, address);
d9b3de22 20772 }
c906108c
SS
20773 break;
20774 case DW_LNE_define_file:
debd256d 20775 {
d521ce57 20776 const char *cur_file;
ecfb656c
PA
20777 unsigned int mod_time, length;
20778 dir_index dindex;
6e70227d 20779
3e43a32a
MS
20780 cur_file = read_direct_string (abfd, line_ptr,
20781 &bytes_read);
debd256d 20782 line_ptr += bytes_read;
ecfb656c 20783 dindex = (dir_index)
debd256d
JB
20784 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20785 line_ptr += bytes_read;
20786 mod_time =
20787 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20788 line_ptr += bytes_read;
20789 length =
20790 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20791 line_ptr += bytes_read;
ecfb656c 20792 lh->add_file_name (cur_file, dindex, mod_time, length);
debd256d 20793 }
c906108c 20794 break;
d0c6ba3d 20795 case DW_LNE_set_discriminator:
6f77053d
PA
20796 {
20797 /* The discriminator is not interesting to the
20798 debugger; just ignore it. We still need to
20799 check its value though:
20800 if there are consecutive entries for the same
20801 (non-prologue) line we want to coalesce them.
20802 PR 17276. */
20803 unsigned int discr
20804 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20805 line_ptr += bytes_read;
20806
20807 state_machine.handle_set_discriminator (discr);
20808 }
d0c6ba3d 20809 break;
c906108c 20810 default:
b98664d3 20811 complaint (_("mangled .debug_line section"));
debd256d 20812 return;
c906108c 20813 }
a8c50c1f
DJ
20814 /* Make sure that we parsed the extended op correctly. If e.g.
20815 we expected a different address size than the producer used,
20816 we may have read the wrong number of bytes. */
20817 if (line_ptr != extended_end)
20818 {
b98664d3 20819 complaint (_("mangled .debug_line section"));
a8c50c1f
DJ
20820 return;
20821 }
c906108c
SS
20822 break;
20823 case DW_LNS_copy:
6f77053d 20824 state_machine.handle_copy ();
c906108c
SS
20825 break;
20826 case DW_LNS_advance_pc:
2dc7f7b3
TT
20827 {
20828 CORE_ADDR adjust
20829 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
2dc7f7b3 20830 line_ptr += bytes_read;
6f77053d
PA
20831
20832 state_machine.handle_advance_pc (adjust);
2dc7f7b3 20833 }
c906108c
SS
20834 break;
20835 case DW_LNS_advance_line:
a05a36a5
DE
20836 {
20837 int line_delta
20838 = read_signed_leb128 (abfd, line_ptr, &bytes_read);
a05a36a5 20839 line_ptr += bytes_read;
6f77053d
PA
20840
20841 state_machine.handle_advance_line (line_delta);
a05a36a5 20842 }
c906108c
SS
20843 break;
20844 case DW_LNS_set_file:
d9b3de22 20845 {
6f77053d 20846 file_name_index file
ecfb656c
PA
20847 = (file_name_index) read_unsigned_leb128 (abfd, line_ptr,
20848 &bytes_read);
d9b3de22 20849 line_ptr += bytes_read;
8c43009f 20850
6f77053d 20851 state_machine.handle_set_file (file);
d9b3de22 20852 }
c906108c
SS
20853 break;
20854 case DW_LNS_set_column:
0ad93d4f 20855 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
c906108c
SS
20856 line_ptr += bytes_read;
20857 break;
20858 case DW_LNS_negate_stmt:
6f77053d 20859 state_machine.handle_negate_stmt ();
c906108c
SS
20860 break;
20861 case DW_LNS_set_basic_block:
c906108c 20862 break;
c2c6d25f
JM
20863 /* Add to the address register of the state machine the
20864 address increment value corresponding to special opcode
a738430d
MK
20865 255. I.e., this value is scaled by the minimum
20866 instruction length since special opcode 255 would have
b021a221 20867 scaled the increment. */
c906108c 20868 case DW_LNS_const_add_pc:
6f77053d 20869 state_machine.handle_const_add_pc ();
c906108c
SS
20870 break;
20871 case DW_LNS_fixed_advance_pc:
3e29f34a 20872 {
6f77053d 20873 CORE_ADDR addr_adj = read_2_bytes (abfd, line_ptr);
3e29f34a 20874 line_ptr += 2;
6f77053d
PA
20875
20876 state_machine.handle_fixed_advance_pc (addr_adj);
3e29f34a 20877 }
c906108c 20878 break;
9aa1fe7e 20879 default:
a738430d
MK
20880 {
20881 /* Unknown standard opcode, ignore it. */
9aa1fe7e 20882 int i;
a738430d 20883
debd256d 20884 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
9aa1fe7e
GK
20885 {
20886 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20887 line_ptr += bytes_read;
20888 }
20889 }
c906108c
SS
20890 }
20891 }
d9b3de22
DE
20892
20893 if (!end_sequence)
20894 dwarf2_debug_line_missing_end_sequence_complaint ();
20895
20896 /* We got a DW_LNE_end_sequence (or we ran off the end of the buffer,
20897 in which case we still finish recording the last line). */
6f77053d 20898 state_machine.record_line (true);
c906108c 20899 }
f3f5162e
DE
20900}
20901
20902/* Decode the Line Number Program (LNP) for the given line_header
20903 structure and CU. The actual information extracted and the type
20904 of structures created from the LNP depends on the value of PST.
20905
20906 1. If PST is NULL, then this procedure uses the data from the program
20907 to create all necessary symbol tables, and their linetables.
20908
20909 2. If PST is not NULL, this procedure reads the program to determine
20910 the list of files included by the unit represented by PST, and
20911 builds all the associated partial symbol tables.
20912
20913 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
20914 It is used for relative paths in the line table.
20915 NOTE: When processing partial symtabs (pst != NULL),
20916 comp_dir == pst->dirname.
20917
20918 NOTE: It is important that psymtabs have the same file name (via strcmp)
20919 as the corresponding symtab. Since COMP_DIR is not used in the name of the
20920 symtab we don't use it in the name of the psymtabs we create.
20921 E.g. expand_line_sal requires this when finding psymtabs to expand.
c3b7b696
YQ
20922 A good testcase for this is mb-inline.exp.
20923
527f3840
JK
20924 LOWPC is the lowest address in CU (or 0 if not known).
20925
20926 Boolean DECODE_MAPPING specifies we need to fully decode .debug_line
20927 for its PC<->lines mapping information. Otherwise only the filename
20928 table is read in. */
f3f5162e
DE
20929
20930static void
20931dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
c3b7b696 20932 struct dwarf2_cu *cu, struct partial_symtab *pst,
527f3840 20933 CORE_ADDR lowpc, int decode_mapping)
f3f5162e 20934{
518817b3 20935 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
f3f5162e 20936 const int decode_for_pst_p = (pst != NULL);
f3f5162e 20937
527f3840
JK
20938 if (decode_mapping)
20939 dwarf_decode_lines_1 (lh, cu, decode_for_pst_p, lowpc);
aaa75496
JB
20940
20941 if (decode_for_pst_p)
20942 {
20943 int file_index;
20944
20945 /* Now that we're done scanning the Line Header Program, we can
20946 create the psymtab of each included file. */
fff8551c 20947 for (file_index = 0; file_index < lh->file_names.size (); file_index++)
aaa75496
JB
20948 if (lh->file_names[file_index].included_p == 1)
20949 {
c89b44cd 20950 gdb::unique_xmalloc_ptr<char> name_holder;
d521ce57 20951 const char *include_name =
c89b44cd
TT
20952 psymtab_include_file_name (lh, file_index, pst, comp_dir,
20953 &name_holder);
c6da4cef 20954 if (include_name != NULL)
aaa75496
JB
20955 dwarf2_create_include_psymtab (include_name, pst, objfile);
20956 }
20957 }
cb1df416
DJ
20958 else
20959 {
20960 /* Make sure a symtab is created for every file, even files
20961 which contain only variables (i.e. no code with associated
20962 line numbers). */
804d2729 20963 struct compunit_symtab *cust = cu->builder->get_compunit_symtab ();
cb1df416 20964 int i;
cb1df416 20965
fff8551c 20966 for (i = 0; i < lh->file_names.size (); i++)
cb1df416 20967 {
8c43009f 20968 file_entry &fe = lh->file_names[i];
9a619af0 20969
804d2729 20970 dwarf2_start_subfile (cu, fe.name, fe.include_dir (lh));
cb1df416 20971
804d2729 20972 if (cu->builder->get_current_subfile ()->symtab == NULL)
43f3e411 20973 {
804d2729
TT
20974 cu->builder->get_current_subfile ()->symtab
20975 = allocate_symtab (cust,
20976 cu->builder->get_current_subfile ()->name);
43f3e411 20977 }
804d2729 20978 fe.symtab = cu->builder->get_current_subfile ()->symtab;
cb1df416
DJ
20979 }
20980 }
c906108c
SS
20981}
20982
20983/* Start a subfile for DWARF. FILENAME is the name of the file and
20984 DIRNAME the name of the source directory which contains FILENAME
4d663531 20985 or NULL if not known.
c906108c
SS
20986 This routine tries to keep line numbers from identical absolute and
20987 relative file names in a common subfile.
20988
20989 Using the `list' example from the GDB testsuite, which resides in
20990 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
20991 of /srcdir/list0.c yields the following debugging information for list0.c:
20992
c5aa993b 20993 DW_AT_name: /srcdir/list0.c
4d663531 20994 DW_AT_comp_dir: /compdir
357e46e7 20995 files.files[0].name: list0.h
c5aa993b 20996 files.files[0].dir: /srcdir
357e46e7 20997 files.files[1].name: list0.c
c5aa993b 20998 files.files[1].dir: /srcdir
c906108c
SS
20999
21000 The line number information for list0.c has to end up in a single
4f1520fb
FR
21001 subfile, so that `break /srcdir/list0.c:1' works as expected.
21002 start_subfile will ensure that this happens provided that we pass the
21003 concatenation of files.files[1].dir and files.files[1].name as the
21004 subfile's name. */
c906108c
SS
21005
21006static void
804d2729
TT
21007dwarf2_start_subfile (struct dwarf2_cu *cu, const char *filename,
21008 const char *dirname)
c906108c 21009{
d521ce57 21010 char *copy = NULL;
4f1520fb 21011
4d663531 21012 /* In order not to lose the line information directory,
4f1520fb
FR
21013 we concatenate it to the filename when it makes sense.
21014 Note that the Dwarf3 standard says (speaking of filenames in line
21015 information): ``The directory index is ignored for file names
21016 that represent full path names''. Thus ignoring dirname in the
21017 `else' branch below isn't an issue. */
c906108c 21018
d5166ae1 21019 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
d521ce57
TT
21020 {
21021 copy = concat (dirname, SLASH_STRING, filename, (char *)NULL);
21022 filename = copy;
21023 }
c906108c 21024
804d2729 21025 cu->builder->start_subfile (filename);
4f1520fb 21026
d521ce57
TT
21027 if (copy != NULL)
21028 xfree (copy);
c906108c
SS
21029}
21030
804d2729
TT
21031/* Start a symtab for DWARF. NAME, COMP_DIR, LOW_PC are passed to the
21032 buildsym_compunit constructor. */
f4dc4d17 21033
43f3e411 21034static struct compunit_symtab *
f4dc4d17 21035dwarf2_start_symtab (struct dwarf2_cu *cu,
15d034d0 21036 const char *name, const char *comp_dir, CORE_ADDR low_pc)
f4dc4d17 21037{
804d2729 21038 gdb_assert (cu->builder == nullptr);
43f3e411 21039
804d2729
TT
21040 cu->builder.reset (new struct buildsym_compunit
21041 (cu->per_cu->dwarf2_per_objfile->objfile,
21042 name, comp_dir, cu->language, low_pc));
93b8bea4 21043
804d2729
TT
21044 cu->list_in_scope = cu->builder->get_file_symbols ();
21045
21046 cu->builder->record_debugformat ("DWARF 2");
21047 cu->builder->record_producer (cu->producer);
f4dc4d17 21048
4d4ec4e5 21049 cu->processing_has_namespace_info = 0;
43f3e411 21050
804d2729 21051 return cu->builder->get_compunit_symtab ();
f4dc4d17
DE
21052}
21053
4c2df51b
DJ
21054static void
21055var_decode_location (struct attribute *attr, struct symbol *sym,
e7c27a73 21056 struct dwarf2_cu *cu)
4c2df51b 21057{
518817b3 21058 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
e7c27a73
DJ
21059 struct comp_unit_head *cu_header = &cu->header;
21060
4c2df51b
DJ
21061 /* NOTE drow/2003-01-30: There used to be a comment and some special
21062 code here to turn a symbol with DW_AT_external and a
21063 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
21064 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
21065 with some versions of binutils) where shared libraries could have
21066 relocations against symbols in their debug information - the
21067 minimal symbol would have the right address, but the debug info
21068 would not. It's no longer necessary, because we will explicitly
21069 apply relocations when we read in the debug information now. */
21070
21071 /* A DW_AT_location attribute with no contents indicates that a
21072 variable has been optimized away. */
21073 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
21074 {
f1e6e072 21075 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
4c2df51b
DJ
21076 return;
21077 }
21078
21079 /* Handle one degenerate form of location expression specially, to
21080 preserve GDB's previous behavior when section offsets are
3019eac3
DE
21081 specified. If this is just a DW_OP_addr or DW_OP_GNU_addr_index
21082 then mark this symbol as LOC_STATIC. */
4c2df51b
DJ
21083
21084 if (attr_form_is_block (attr)
3019eac3
DE
21085 && ((DW_BLOCK (attr)->data[0] == DW_OP_addr
21086 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size)
21087 || (DW_BLOCK (attr)->data[0] == DW_OP_GNU_addr_index
21088 && (DW_BLOCK (attr)->size
21089 == 1 + leb128_size (&DW_BLOCK (attr)->data[1])))))
4c2df51b 21090 {
891d2f0b 21091 unsigned int dummy;
4c2df51b 21092
3019eac3
DE
21093 if (DW_BLOCK (attr)->data[0] == DW_OP_addr)
21094 SYMBOL_VALUE_ADDRESS (sym) =
21095 read_address (objfile->obfd, DW_BLOCK (attr)->data + 1, cu, &dummy);
21096 else
21097 SYMBOL_VALUE_ADDRESS (sym) =
21098 read_addr_index_from_leb128 (cu, DW_BLOCK (attr)->data + 1, &dummy);
f1e6e072 21099 SYMBOL_ACLASS_INDEX (sym) = LOC_STATIC;
4c2df51b
DJ
21100 fixup_symbol_section (sym, objfile);
21101 SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (objfile->section_offsets,
21102 SYMBOL_SECTION (sym));
4c2df51b
DJ
21103 return;
21104 }
21105
21106 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
21107 expression evaluator, and use LOC_COMPUTED only when necessary
21108 (i.e. when the value of a register or memory location is
21109 referenced, or a thread-local block, etc.). Then again, it might
21110 not be worthwhile. I'm assuming that it isn't unless performance
21111 or memory numbers show me otherwise. */
21112
f1e6e072 21113 dwarf2_symbol_mark_computed (attr, sym, cu, 0);
8be455d7 21114
f1e6e072 21115 if (SYMBOL_COMPUTED_OPS (sym)->location_has_loclist)
8be455d7 21116 cu->has_loclist = 1;
4c2df51b
DJ
21117}
21118
c906108c
SS
21119/* Given a pointer to a DWARF information entry, figure out if we need
21120 to make a symbol table entry for it, and if so, create a new entry
21121 and return a pointer to it.
21122 If TYPE is NULL, determine symbol type from the die, otherwise
34eaf542
TT
21123 used the passed type.
21124 If SPACE is not NULL, use it to hold the new symbol. If it is
21125 NULL, allocate a new symbol on the objfile's obstack. */
c906108c
SS
21126
21127static struct symbol *
5e2db402
TT
21128new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
21129 struct symbol *space)
c906108c 21130{
518817b3
SM
21131 struct dwarf2_per_objfile *dwarf2_per_objfile
21132 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 21133 struct objfile *objfile = dwarf2_per_objfile->objfile;
3e29f34a 21134 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c 21135 struct symbol *sym = NULL;
15d034d0 21136 const char *name;
c906108c
SS
21137 struct attribute *attr = NULL;
21138 struct attribute *attr2 = NULL;
e142c38c 21139 CORE_ADDR baseaddr;
e37fd15a
SW
21140 struct pending **list_to_add = NULL;
21141
edb3359d 21142 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
e142c38c
DJ
21143
21144 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 21145
94af9270 21146 name = dwarf2_name (die, cu);
c906108c
SS
21147 if (name)
21148 {
94af9270 21149 const char *linkagename;
34eaf542 21150 int suppress_add = 0;
94af9270 21151
34eaf542
TT
21152 if (space)
21153 sym = space;
21154 else
e623cf5d 21155 sym = allocate_symbol (objfile);
c906108c 21156 OBJSTAT (objfile, n_syms++);
2de7ced7
DJ
21157
21158 /* Cache this symbol's name and the name's demangled form (if any). */
f85f34ed 21159 SYMBOL_SET_LANGUAGE (sym, cu->language, &objfile->objfile_obstack);
94af9270
KS
21160 linkagename = dwarf2_physname (name, die, cu);
21161 SYMBOL_SET_NAMES (sym, linkagename, strlen (linkagename), 0, objfile);
c906108c 21162
f55ee35c
JK
21163 /* Fortran does not have mangling standard and the mangling does differ
21164 between gfortran, iFort etc. */
21165 if (cu->language == language_fortran
b250c185 21166 && symbol_get_demangled_name (&(sym->ginfo)) == NULL)
29df156d 21167 symbol_set_demangled_name (&(sym->ginfo),
cfc594ee 21168 dwarf2_full_name (name, die, cu),
29df156d 21169 NULL);
f55ee35c 21170
c906108c 21171 /* Default assumptions.
c5aa993b 21172 Use the passed type or decode it from the die. */
176620f1 21173 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
f1e6e072 21174 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
c906108c
SS
21175 if (type != NULL)
21176 SYMBOL_TYPE (sym) = type;
21177 else
e7c27a73 21178 SYMBOL_TYPE (sym) = die_type (die, cu);
edb3359d
DJ
21179 attr = dwarf2_attr (die,
21180 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
21181 cu);
c906108c
SS
21182 if (attr)
21183 {
21184 SYMBOL_LINE (sym) = DW_UNSND (attr);
21185 }
cb1df416 21186
edb3359d
DJ
21187 attr = dwarf2_attr (die,
21188 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
21189 cu);
cb1df416
DJ
21190 if (attr)
21191 {
ecfb656c 21192 file_name_index file_index = (file_name_index) DW_UNSND (attr);
8c43009f 21193 struct file_entry *fe;
9a619af0 21194
ecfb656c
PA
21195 if (cu->line_header != NULL)
21196 fe = cu->line_header->file_name_at (file_index);
8c43009f
PA
21197 else
21198 fe = NULL;
21199
21200 if (fe == NULL)
b98664d3 21201 complaint (_("file index out of range"));
8c43009f
PA
21202 else
21203 symbol_set_symtab (sym, fe->symtab);
cb1df416
DJ
21204 }
21205
c906108c
SS
21206 switch (die->tag)
21207 {
21208 case DW_TAG_label:
e142c38c 21209 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
c906108c 21210 if (attr)
3e29f34a
MR
21211 {
21212 CORE_ADDR addr;
21213
21214 addr = attr_value_as_address (attr);
21215 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr + baseaddr);
21216 SYMBOL_VALUE_ADDRESS (sym) = addr;
21217 }
0f5238ed
TT
21218 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
21219 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
f1e6e072 21220 SYMBOL_ACLASS_INDEX (sym) = LOC_LABEL;
0f5238ed 21221 add_symbol_to_list (sym, cu->list_in_scope);
c906108c
SS
21222 break;
21223 case DW_TAG_subprogram:
21224 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
21225 finish_block. */
f1e6e072 21226 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
e142c38c 21227 attr2 = dwarf2_attr (die, DW_AT_external, cu);
2cfa0c8d
JB
21228 if ((attr2 && (DW_UNSND (attr2) != 0))
21229 || cu->language == language_ada)
c906108c 21230 {
2cfa0c8d
JB
21231 /* Subprograms marked external are stored as a global symbol.
21232 Ada subprograms, whether marked external or not, are always
21233 stored as a global symbol, because we want to be able to
21234 access them globally. For instance, we want to be able
21235 to break on a nested subprogram without having to
21236 specify the context. */
804d2729 21237 list_to_add = cu->builder->get_global_symbols ();
c906108c
SS
21238 }
21239 else
21240 {
e37fd15a 21241 list_to_add = cu->list_in_scope;
c906108c
SS
21242 }
21243 break;
edb3359d
DJ
21244 case DW_TAG_inlined_subroutine:
21245 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
21246 finish_block. */
f1e6e072 21247 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
edb3359d 21248 SYMBOL_INLINED (sym) = 1;
481860b3 21249 list_to_add = cu->list_in_scope;
edb3359d 21250 break;
34eaf542
TT
21251 case DW_TAG_template_value_param:
21252 suppress_add = 1;
21253 /* Fall through. */
72929c62 21254 case DW_TAG_constant:
c906108c 21255 case DW_TAG_variable:
254e6b9e 21256 case DW_TAG_member:
0963b4bd
MS
21257 /* Compilation with minimal debug info may result in
21258 variables with missing type entries. Change the
21259 misleading `void' type to something sensible. */
c906108c 21260 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
46a4882b 21261 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_int;
64c50499 21262
e142c38c 21263 attr = dwarf2_attr (die, DW_AT_const_value, cu);
254e6b9e
DE
21264 /* In the case of DW_TAG_member, we should only be called for
21265 static const members. */
21266 if (die->tag == DW_TAG_member)
21267 {
3863f96c
DE
21268 /* dwarf2_add_field uses die_is_declaration,
21269 so we do the same. */
254e6b9e
DE
21270 gdb_assert (die_is_declaration (die, cu));
21271 gdb_assert (attr);
21272 }
c906108c
SS
21273 if (attr)
21274 {
e7c27a73 21275 dwarf2_const_value (attr, sym, cu);
e142c38c 21276 attr2 = dwarf2_attr (die, DW_AT_external, cu);
e37fd15a 21277 if (!suppress_add)
34eaf542
TT
21278 {
21279 if (attr2 && (DW_UNSND (attr2) != 0))
804d2729 21280 list_to_add = cu->builder->get_global_symbols ();
34eaf542 21281 else
e37fd15a 21282 list_to_add = cu->list_in_scope;
34eaf542 21283 }
c906108c
SS
21284 break;
21285 }
e142c38c 21286 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
21287 if (attr)
21288 {
e7c27a73 21289 var_decode_location (attr, sym, cu);
e142c38c 21290 attr2 = dwarf2_attr (die, DW_AT_external, cu);
4357ac6c
TT
21291
21292 /* Fortran explicitly imports any global symbols to the local
21293 scope by DW_TAG_common_block. */
21294 if (cu->language == language_fortran && die->parent
21295 && die->parent->tag == DW_TAG_common_block)
21296 attr2 = NULL;
21297
caac4577
JG
21298 if (SYMBOL_CLASS (sym) == LOC_STATIC
21299 && SYMBOL_VALUE_ADDRESS (sym) == 0
21300 && !dwarf2_per_objfile->has_section_at_zero)
21301 {
21302 /* When a static variable is eliminated by the linker,
21303 the corresponding debug information is not stripped
21304 out, but the variable address is set to null;
21305 do not add such variables into symbol table. */
21306 }
21307 else if (attr2 && (DW_UNSND (attr2) != 0))
1c809c68 21308 {
f55ee35c
JK
21309 /* Workaround gfortran PR debug/40040 - it uses
21310 DW_AT_location for variables in -fPIC libraries which may
21311 get overriden by other libraries/executable and get
21312 a different address. Resolve it by the minimal symbol
21313 which may come from inferior's executable using copy
21314 relocation. Make this workaround only for gfortran as for
21315 other compilers GDB cannot guess the minimal symbol
21316 Fortran mangling kind. */
21317 if (cu->language == language_fortran && die->parent
21318 && die->parent->tag == DW_TAG_module
21319 && cu->producer
28586665 21320 && startswith (cu->producer, "GNU Fortran"))
f1e6e072 21321 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
f55ee35c 21322
1c809c68
TT
21323 /* A variable with DW_AT_external is never static,
21324 but it may be block-scoped. */
804d2729
TT
21325 list_to_add
21326 = (cu->list_in_scope == cu->builder->get_file_symbols ()
21327 ? cu->builder->get_global_symbols ()
21328 : cu->list_in_scope);
1c809c68 21329 }
c906108c 21330 else
e37fd15a 21331 list_to_add = cu->list_in_scope;
c906108c
SS
21332 }
21333 else
21334 {
21335 /* We do not know the address of this symbol.
c5aa993b
JM
21336 If it is an external symbol and we have type information
21337 for it, enter the symbol as a LOC_UNRESOLVED symbol.
21338 The address of the variable will then be determined from
21339 the minimal symbol table whenever the variable is
21340 referenced. */
e142c38c 21341 attr2 = dwarf2_attr (die, DW_AT_external, cu);
0971de02
TT
21342
21343 /* Fortran explicitly imports any global symbols to the local
21344 scope by DW_TAG_common_block. */
21345 if (cu->language == language_fortran && die->parent
21346 && die->parent->tag == DW_TAG_common_block)
21347 {
21348 /* SYMBOL_CLASS doesn't matter here because
21349 read_common_block is going to reset it. */
21350 if (!suppress_add)
21351 list_to_add = cu->list_in_scope;
21352 }
21353 else if (attr2 && (DW_UNSND (attr2) != 0)
21354 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
c906108c 21355 {
0fe7935b
DJ
21356 /* A variable with DW_AT_external is never static, but it
21357 may be block-scoped. */
804d2729
TT
21358 list_to_add
21359 = (cu->list_in_scope == cu->builder->get_file_symbols ()
21360 ? cu->builder->get_global_symbols ()
21361 : cu->list_in_scope);
0fe7935b 21362
f1e6e072 21363 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
c906108c 21364 }
442ddf59
JK
21365 else if (!die_is_declaration (die, cu))
21366 {
21367 /* Use the default LOC_OPTIMIZED_OUT class. */
21368 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
e37fd15a
SW
21369 if (!suppress_add)
21370 list_to_add = cu->list_in_scope;
442ddf59 21371 }
c906108c
SS
21372 }
21373 break;
21374 case DW_TAG_formal_parameter:
a60f3166
TT
21375 {
21376 /* If we are inside a function, mark this as an argument. If
21377 not, we might be looking at an argument to an inlined function
21378 when we do not have enough information to show inlined frames;
21379 pretend it's a local variable in that case so that the user can
21380 still see it. */
804d2729
TT
21381 struct context_stack *curr
21382 = cu->builder->get_current_context_stack ();
a60f3166
TT
21383 if (curr != nullptr && curr->name != nullptr)
21384 SYMBOL_IS_ARGUMENT (sym) = 1;
21385 attr = dwarf2_attr (die, DW_AT_location, cu);
21386 if (attr)
21387 {
21388 var_decode_location (attr, sym, cu);
21389 }
21390 attr = dwarf2_attr (die, DW_AT_const_value, cu);
21391 if (attr)
21392 {
21393 dwarf2_const_value (attr, sym, cu);
21394 }
f346a30d 21395
a60f3166
TT
21396 list_to_add = cu->list_in_scope;
21397 }
c906108c
SS
21398 break;
21399 case DW_TAG_unspecified_parameters:
21400 /* From varargs functions; gdb doesn't seem to have any
21401 interest in this information, so just ignore it for now.
21402 (FIXME?) */
21403 break;
34eaf542
TT
21404 case DW_TAG_template_type_param:
21405 suppress_add = 1;
21406 /* Fall through. */
c906108c 21407 case DW_TAG_class_type:
680b30c7 21408 case DW_TAG_interface_type:
c906108c
SS
21409 case DW_TAG_structure_type:
21410 case DW_TAG_union_type:
72019c9c 21411 case DW_TAG_set_type:
c906108c 21412 case DW_TAG_enumeration_type:
f1e6e072 21413 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
176620f1 21414 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
c906108c 21415
63d06c5c 21416 {
9c37b5ae 21417 /* NOTE: carlton/2003-11-10: C++ class symbols shouldn't
63d06c5c
DC
21418 really ever be static objects: otherwise, if you try
21419 to, say, break of a class's method and you're in a file
21420 which doesn't mention that class, it won't work unless
21421 the check for all static symbols in lookup_symbol_aux
21422 saves you. See the OtherFileClass tests in
21423 gdb.c++/namespace.exp. */
21424
e37fd15a 21425 if (!suppress_add)
34eaf542 21426 {
804d2729
TT
21427 list_to_add
21428 = (cu->list_in_scope == cu->builder->get_file_symbols ()
21429 && cu->language == language_cplus
21430 ? cu->builder->get_global_symbols ()
21431 : cu->list_in_scope);
63d06c5c 21432
64382290 21433 /* The semantics of C++ state that "struct foo {
9c37b5ae 21434 ... }" also defines a typedef for "foo". */
64382290 21435 if (cu->language == language_cplus
45280282 21436 || cu->language == language_ada
c44af4eb
TT
21437 || cu->language == language_d
21438 || cu->language == language_rust)
64382290
TT
21439 {
21440 /* The symbol's name is already allocated along
21441 with this objfile, so we don't need to
21442 duplicate it for the type. */
21443 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
21444 TYPE_NAME (SYMBOL_TYPE (sym)) = SYMBOL_SEARCH_NAME (sym);
21445 }
63d06c5c
DC
21446 }
21447 }
c906108c
SS
21448 break;
21449 case DW_TAG_typedef:
f1e6e072 21450 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
63d06c5c 21451 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 21452 list_to_add = cu->list_in_scope;
63d06c5c 21453 break;
c906108c 21454 case DW_TAG_base_type:
a02abb62 21455 case DW_TAG_subrange_type:
f1e6e072 21456 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
176620f1 21457 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 21458 list_to_add = cu->list_in_scope;
c906108c
SS
21459 break;
21460 case DW_TAG_enumerator:
e142c38c 21461 attr = dwarf2_attr (die, DW_AT_const_value, cu);
c906108c
SS
21462 if (attr)
21463 {
e7c27a73 21464 dwarf2_const_value (attr, sym, cu);
c906108c 21465 }
63d06c5c
DC
21466 {
21467 /* NOTE: carlton/2003-11-10: See comment above in the
21468 DW_TAG_class_type, etc. block. */
21469
804d2729
TT
21470 list_to_add
21471 = (cu->list_in_scope == cu->builder->get_file_symbols ()
21472 && cu->language == language_cplus
21473 ? cu->builder->get_global_symbols ()
21474 : cu->list_in_scope);
63d06c5c 21475 }
c906108c 21476 break;
74921315 21477 case DW_TAG_imported_declaration:
5c4e30ca 21478 case DW_TAG_namespace:
f1e6e072 21479 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
804d2729 21480 list_to_add = cu->builder->get_global_symbols ();
5c4e30ca 21481 break;
530e8392
KB
21482 case DW_TAG_module:
21483 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21484 SYMBOL_DOMAIN (sym) = MODULE_DOMAIN;
804d2729 21485 list_to_add = cu->builder->get_global_symbols ();
530e8392 21486 break;
4357ac6c 21487 case DW_TAG_common_block:
f1e6e072 21488 SYMBOL_ACLASS_INDEX (sym) = LOC_COMMON_BLOCK;
4357ac6c
TT
21489 SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
21490 add_symbol_to_list (sym, cu->list_in_scope);
21491 break;
c906108c
SS
21492 default:
21493 /* Not a tag we recognize. Hopefully we aren't processing
21494 trash data, but since we must specifically ignore things
21495 we don't recognize, there is nothing else we should do at
0963b4bd 21496 this point. */
b98664d3 21497 complaint (_("unsupported tag: '%s'"),
4d3c2250 21498 dwarf_tag_name (die->tag));
c906108c
SS
21499 break;
21500 }
df8a16a1 21501
e37fd15a
SW
21502 if (suppress_add)
21503 {
21504 sym->hash_next = objfile->template_symbols;
21505 objfile->template_symbols = sym;
21506 list_to_add = NULL;
21507 }
21508
21509 if (list_to_add != NULL)
21510 add_symbol_to_list (sym, list_to_add);
21511
df8a16a1
DJ
21512 /* For the benefit of old versions of GCC, check for anonymous
21513 namespaces based on the demangled name. */
4d4ec4e5 21514 if (!cu->processing_has_namespace_info
94af9270 21515 && cu->language == language_cplus)
804d2729 21516 cp_scan_for_anonymous_namespaces (cu->builder.get (), sym, objfile);
c906108c
SS
21517 }
21518 return (sym);
21519}
21520
98bfdba5
PA
21521/* Given an attr with a DW_FORM_dataN value in host byte order,
21522 zero-extend it as appropriate for the symbol's type. The DWARF
21523 standard (v4) is not entirely clear about the meaning of using
21524 DW_FORM_dataN for a constant with a signed type, where the type is
21525 wider than the data. The conclusion of a discussion on the DWARF
21526 list was that this is unspecified. We choose to always zero-extend
21527 because that is the interpretation long in use by GCC. */
c906108c 21528
98bfdba5 21529static gdb_byte *
ff39bb5e 21530dwarf2_const_value_data (const struct attribute *attr, struct obstack *obstack,
12df843f 21531 struct dwarf2_cu *cu, LONGEST *value, int bits)
c906108c 21532{
518817b3 21533 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
e17a4113
UW
21534 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
21535 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
98bfdba5
PA
21536 LONGEST l = DW_UNSND (attr);
21537
21538 if (bits < sizeof (*value) * 8)
21539 {
21540 l &= ((LONGEST) 1 << bits) - 1;
21541 *value = l;
21542 }
21543 else if (bits == sizeof (*value) * 8)
21544 *value = l;
21545 else
21546 {
224c3ddb 21547 gdb_byte *bytes = (gdb_byte *) obstack_alloc (obstack, bits / 8);
98bfdba5
PA
21548 store_unsigned_integer (bytes, bits / 8, byte_order, l);
21549 return bytes;
21550 }
21551
21552 return NULL;
21553}
21554
21555/* Read a constant value from an attribute. Either set *VALUE, or if
21556 the value does not fit in *VALUE, set *BYTES - either already
21557 allocated on the objfile obstack, or newly allocated on OBSTACK,
21558 or, set *BATON, if we translated the constant to a location
21559 expression. */
21560
21561static void
ff39bb5e 21562dwarf2_const_value_attr (const struct attribute *attr, struct type *type,
98bfdba5
PA
21563 const char *name, struct obstack *obstack,
21564 struct dwarf2_cu *cu,
d521ce57 21565 LONGEST *value, const gdb_byte **bytes,
98bfdba5
PA
21566 struct dwarf2_locexpr_baton **baton)
21567{
518817b3 21568 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
98bfdba5 21569 struct comp_unit_head *cu_header = &cu->header;
c906108c 21570 struct dwarf_block *blk;
98bfdba5
PA
21571 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
21572 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
21573
21574 *value = 0;
21575 *bytes = NULL;
21576 *baton = NULL;
c906108c
SS
21577
21578 switch (attr->form)
21579 {
21580 case DW_FORM_addr:
3019eac3 21581 case DW_FORM_GNU_addr_index:
ac56253d 21582 {
ac56253d
TT
21583 gdb_byte *data;
21584
98bfdba5
PA
21585 if (TYPE_LENGTH (type) != cu_header->addr_size)
21586 dwarf2_const_value_length_mismatch_complaint (name,
ac56253d 21587 cu_header->addr_size,
98bfdba5 21588 TYPE_LENGTH (type));
ac56253d
TT
21589 /* Symbols of this form are reasonably rare, so we just
21590 piggyback on the existing location code rather than writing
21591 a new implementation of symbol_computed_ops. */
8d749320 21592 *baton = XOBNEW (obstack, struct dwarf2_locexpr_baton);
98bfdba5
PA
21593 (*baton)->per_cu = cu->per_cu;
21594 gdb_assert ((*baton)->per_cu);
ac56253d 21595
98bfdba5 21596 (*baton)->size = 2 + cu_header->addr_size;
224c3ddb 21597 data = (gdb_byte *) obstack_alloc (obstack, (*baton)->size);
98bfdba5 21598 (*baton)->data = data;
ac56253d
TT
21599
21600 data[0] = DW_OP_addr;
21601 store_unsigned_integer (&data[1], cu_header->addr_size,
21602 byte_order, DW_ADDR (attr));
21603 data[cu_header->addr_size + 1] = DW_OP_stack_value;
ac56253d 21604 }
c906108c 21605 break;
4ac36638 21606 case DW_FORM_string:
93b5768b 21607 case DW_FORM_strp:
3019eac3 21608 case DW_FORM_GNU_str_index:
36586728 21609 case DW_FORM_GNU_strp_alt:
98bfdba5
PA
21610 /* DW_STRING is already allocated on the objfile obstack, point
21611 directly to it. */
d521ce57 21612 *bytes = (const gdb_byte *) DW_STRING (attr);
93b5768b 21613 break;
c906108c
SS
21614 case DW_FORM_block1:
21615 case DW_FORM_block2:
21616 case DW_FORM_block4:
21617 case DW_FORM_block:
2dc7f7b3 21618 case DW_FORM_exprloc:
0224619f 21619 case DW_FORM_data16:
c906108c 21620 blk = DW_BLOCK (attr);
98bfdba5
PA
21621 if (TYPE_LENGTH (type) != blk->size)
21622 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
21623 TYPE_LENGTH (type));
21624 *bytes = blk->data;
c906108c 21625 break;
2df3850c
JM
21626
21627 /* The DW_AT_const_value attributes are supposed to carry the
21628 symbol's value "represented as it would be on the target
21629 architecture." By the time we get here, it's already been
21630 converted to host endianness, so we just need to sign- or
21631 zero-extend it as appropriate. */
21632 case DW_FORM_data1:
3aef2284 21633 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 8);
2df3850c 21634 break;
c906108c 21635 case DW_FORM_data2:
3aef2284 21636 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 16);
2df3850c 21637 break;
c906108c 21638 case DW_FORM_data4:
3aef2284 21639 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 32);
2df3850c 21640 break;
c906108c 21641 case DW_FORM_data8:
3aef2284 21642 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 64);
2df3850c
JM
21643 break;
21644
c906108c 21645 case DW_FORM_sdata:
663c44ac 21646 case DW_FORM_implicit_const:
98bfdba5 21647 *value = DW_SND (attr);
2df3850c
JM
21648 break;
21649
c906108c 21650 case DW_FORM_udata:
98bfdba5 21651 *value = DW_UNSND (attr);
c906108c 21652 break;
2df3850c 21653
c906108c 21654 default:
b98664d3 21655 complaint (_("unsupported const value attribute form: '%s'"),
4d3c2250 21656 dwarf_form_name (attr->form));
98bfdba5 21657 *value = 0;
c906108c
SS
21658 break;
21659 }
21660}
21661
2df3850c 21662
98bfdba5
PA
21663/* Copy constant value from an attribute to a symbol. */
21664
2df3850c 21665static void
ff39bb5e 21666dwarf2_const_value (const struct attribute *attr, struct symbol *sym,
98bfdba5 21667 struct dwarf2_cu *cu)
2df3850c 21668{
518817b3 21669 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
12df843f 21670 LONGEST value;
d521ce57 21671 const gdb_byte *bytes;
98bfdba5 21672 struct dwarf2_locexpr_baton *baton;
2df3850c 21673
98bfdba5
PA
21674 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
21675 SYMBOL_PRINT_NAME (sym),
21676 &objfile->objfile_obstack, cu,
21677 &value, &bytes, &baton);
2df3850c 21678
98bfdba5
PA
21679 if (baton != NULL)
21680 {
98bfdba5 21681 SYMBOL_LOCATION_BATON (sym) = baton;
f1e6e072 21682 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
98bfdba5
PA
21683 }
21684 else if (bytes != NULL)
21685 {
21686 SYMBOL_VALUE_BYTES (sym) = bytes;
f1e6e072 21687 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST_BYTES;
98bfdba5
PA
21688 }
21689 else
21690 {
21691 SYMBOL_VALUE (sym) = value;
f1e6e072 21692 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST;
98bfdba5 21693 }
2df3850c
JM
21694}
21695
c906108c
SS
21696/* Return the type of the die in question using its DW_AT_type attribute. */
21697
21698static struct type *
e7c27a73 21699die_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 21700{
c906108c 21701 struct attribute *type_attr;
c906108c 21702
e142c38c 21703 type_attr = dwarf2_attr (die, DW_AT_type, cu);
c906108c
SS
21704 if (!type_attr)
21705 {
518817b3 21706 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c 21707 /* A missing DW_AT_type represents a void type. */
518817b3 21708 return objfile_type (objfile)->builtin_void;
c906108c 21709 }
348e048f 21710
673bfd45 21711 return lookup_die_type (die, type_attr, cu);
c906108c
SS
21712}
21713
b4ba55a1
JB
21714/* True iff CU's producer generates GNAT Ada auxiliary information
21715 that allows to find parallel types through that information instead
21716 of having to do expensive parallel lookups by type name. */
21717
21718static int
21719need_gnat_info (struct dwarf2_cu *cu)
21720{
de4cb04a
JB
21721 /* Assume that the Ada compiler was GNAT, which always produces
21722 the auxiliary information. */
21723 return (cu->language == language_ada);
b4ba55a1
JB
21724}
21725
b4ba55a1
JB
21726/* Return the auxiliary type of the die in question using its
21727 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
21728 attribute is not present. */
21729
21730static struct type *
21731die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
21732{
b4ba55a1 21733 struct attribute *type_attr;
b4ba55a1
JB
21734
21735 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
21736 if (!type_attr)
21737 return NULL;
21738
673bfd45 21739 return lookup_die_type (die, type_attr, cu);
b4ba55a1
JB
21740}
21741
21742/* If DIE has a descriptive_type attribute, then set the TYPE's
21743 descriptive type accordingly. */
21744
21745static void
21746set_descriptive_type (struct type *type, struct die_info *die,
21747 struct dwarf2_cu *cu)
21748{
21749 struct type *descriptive_type = die_descriptive_type (die, cu);
21750
21751 if (descriptive_type)
21752 {
21753 ALLOCATE_GNAT_AUX_TYPE (type);
21754 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
21755 }
21756}
21757
c906108c
SS
21758/* Return the containing type of the die in question using its
21759 DW_AT_containing_type attribute. */
21760
21761static struct type *
e7c27a73 21762die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 21763{
c906108c 21764 struct attribute *type_attr;
518817b3 21765 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c 21766
e142c38c 21767 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
33ac96f0
JK
21768 if (!type_attr)
21769 error (_("Dwarf Error: Problem turning containing type into gdb type "
518817b3 21770 "[in module %s]"), objfile_name (objfile));
33ac96f0 21771
673bfd45 21772 return lookup_die_type (die, type_attr, cu);
c906108c
SS
21773}
21774
ac9ec31b
DE
21775/* Return an error marker type to use for the ill formed type in DIE/CU. */
21776
21777static struct type *
21778build_error_marker_type (struct dwarf2_cu *cu, struct die_info *die)
21779{
518817b3
SM
21780 struct dwarf2_per_objfile *dwarf2_per_objfile
21781 = cu->per_cu->dwarf2_per_objfile;
ac9ec31b
DE
21782 struct objfile *objfile = dwarf2_per_objfile->objfile;
21783 char *message, *saved;
21784
9d8780f0 21785 message = xstrprintf (_("<unknown type in %s, CU %s, DIE %s>"),
4262abfb 21786 objfile_name (objfile),
9d8780f0
SM
21787 sect_offset_str (cu->header.sect_off),
21788 sect_offset_str (die->sect_off));
224c3ddb
SM
21789 saved = (char *) obstack_copy0 (&objfile->objfile_obstack,
21790 message, strlen (message));
ac9ec31b
DE
21791 xfree (message);
21792
19f392bc 21793 return init_type (objfile, TYPE_CODE_ERROR, 0, saved);
ac9ec31b
DE
21794}
21795
673bfd45 21796/* Look up the type of DIE in CU using its type attribute ATTR.
ac9ec31b
DE
21797 ATTR must be one of: DW_AT_type, DW_AT_GNAT_descriptive_type,
21798 DW_AT_containing_type.
673bfd45
DE
21799 If there is no type substitute an error marker. */
21800
c906108c 21801static struct type *
ff39bb5e 21802lookup_die_type (struct die_info *die, const struct attribute *attr,
673bfd45 21803 struct dwarf2_cu *cu)
c906108c 21804{
518817b3
SM
21805 struct dwarf2_per_objfile *dwarf2_per_objfile
21806 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 21807 struct objfile *objfile = dwarf2_per_objfile->objfile;
f792889a
DJ
21808 struct type *this_type;
21809
ac9ec31b
DE
21810 gdb_assert (attr->name == DW_AT_type
21811 || attr->name == DW_AT_GNAT_descriptive_type
21812 || attr->name == DW_AT_containing_type);
21813
673bfd45
DE
21814 /* First see if we have it cached. */
21815
36586728
TT
21816 if (attr->form == DW_FORM_GNU_ref_alt)
21817 {
21818 struct dwarf2_per_cu_data *per_cu;
9c541725 21819 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
36586728 21820
ed2dc618
SM
21821 per_cu = dwarf2_find_containing_comp_unit (sect_off, 1,
21822 dwarf2_per_objfile);
9c541725 21823 this_type = get_die_type_at_offset (sect_off, per_cu);
36586728 21824 }
7771576e 21825 else if (attr_form_is_ref (attr))
673bfd45 21826 {
9c541725 21827 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
673bfd45 21828
9c541725 21829 this_type = get_die_type_at_offset (sect_off, cu->per_cu);
673bfd45 21830 }
55f1336d 21831 else if (attr->form == DW_FORM_ref_sig8)
673bfd45 21832 {
ac9ec31b 21833 ULONGEST signature = DW_SIGNATURE (attr);
673bfd45 21834
ac9ec31b 21835 return get_signatured_type (die, signature, cu);
673bfd45
DE
21836 }
21837 else
21838 {
b98664d3 21839 complaint (_("Dwarf Error: Bad type attribute %s in DIE"
9d8780f0
SM
21840 " at %s [in module %s]"),
21841 dwarf_attr_name (attr->name), sect_offset_str (die->sect_off),
4262abfb 21842 objfile_name (objfile));
ac9ec31b 21843 return build_error_marker_type (cu, die);
673bfd45
DE
21844 }
21845
21846 /* If not cached we need to read it in. */
21847
21848 if (this_type == NULL)
21849 {
ac9ec31b 21850 struct die_info *type_die = NULL;
673bfd45
DE
21851 struct dwarf2_cu *type_cu = cu;
21852
7771576e 21853 if (attr_form_is_ref (attr))
ac9ec31b
DE
21854 type_die = follow_die_ref (die, attr, &type_cu);
21855 if (type_die == NULL)
21856 return build_error_marker_type (cu, die);
21857 /* If we find the type now, it's probably because the type came
3019eac3
DE
21858 from an inter-CU reference and the type's CU got expanded before
21859 ours. */
ac9ec31b 21860 this_type = read_type_die (type_die, type_cu);
673bfd45
DE
21861 }
21862
21863 /* If we still don't have a type use an error marker. */
21864
21865 if (this_type == NULL)
ac9ec31b 21866 return build_error_marker_type (cu, die);
673bfd45 21867
f792889a 21868 return this_type;
c906108c
SS
21869}
21870
673bfd45
DE
21871/* Return the type in DIE, CU.
21872 Returns NULL for invalid types.
21873
02142a6c 21874 This first does a lookup in die_type_hash,
673bfd45
DE
21875 and only reads the die in if necessary.
21876
21877 NOTE: This can be called when reading in partial or full symbols. */
21878
f792889a 21879static struct type *
e7c27a73 21880read_type_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 21881{
f792889a
DJ
21882 struct type *this_type;
21883
21884 this_type = get_die_type (die, cu);
21885 if (this_type)
21886 return this_type;
21887
673bfd45
DE
21888 return read_type_die_1 (die, cu);
21889}
21890
21891/* Read the type in DIE, CU.
21892 Returns NULL for invalid types. */
21893
21894static struct type *
21895read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
21896{
21897 struct type *this_type = NULL;
21898
c906108c
SS
21899 switch (die->tag)
21900 {
21901 case DW_TAG_class_type:
680b30c7 21902 case DW_TAG_interface_type:
c906108c
SS
21903 case DW_TAG_structure_type:
21904 case DW_TAG_union_type:
f792889a 21905 this_type = read_structure_type (die, cu);
c906108c
SS
21906 break;
21907 case DW_TAG_enumeration_type:
f792889a 21908 this_type = read_enumeration_type (die, cu);
c906108c
SS
21909 break;
21910 case DW_TAG_subprogram:
21911 case DW_TAG_subroutine_type:
edb3359d 21912 case DW_TAG_inlined_subroutine:
f792889a 21913 this_type = read_subroutine_type (die, cu);
c906108c
SS
21914 break;
21915 case DW_TAG_array_type:
f792889a 21916 this_type = read_array_type (die, cu);
c906108c 21917 break;
72019c9c 21918 case DW_TAG_set_type:
f792889a 21919 this_type = read_set_type (die, cu);
72019c9c 21920 break;
c906108c 21921 case DW_TAG_pointer_type:
f792889a 21922 this_type = read_tag_pointer_type (die, cu);
c906108c
SS
21923 break;
21924 case DW_TAG_ptr_to_member_type:
f792889a 21925 this_type = read_tag_ptr_to_member_type (die, cu);
c906108c
SS
21926 break;
21927 case DW_TAG_reference_type:
4297a3f0
AV
21928 this_type = read_tag_reference_type (die, cu, TYPE_CODE_REF);
21929 break;
21930 case DW_TAG_rvalue_reference_type:
21931 this_type = read_tag_reference_type (die, cu, TYPE_CODE_RVALUE_REF);
c906108c
SS
21932 break;
21933 case DW_TAG_const_type:
f792889a 21934 this_type = read_tag_const_type (die, cu);
c906108c
SS
21935 break;
21936 case DW_TAG_volatile_type:
f792889a 21937 this_type = read_tag_volatile_type (die, cu);
c906108c 21938 break;
06d66ee9
TT
21939 case DW_TAG_restrict_type:
21940 this_type = read_tag_restrict_type (die, cu);
21941 break;
c906108c 21942 case DW_TAG_string_type:
f792889a 21943 this_type = read_tag_string_type (die, cu);
c906108c
SS
21944 break;
21945 case DW_TAG_typedef:
f792889a 21946 this_type = read_typedef (die, cu);
c906108c 21947 break;
a02abb62 21948 case DW_TAG_subrange_type:
f792889a 21949 this_type = read_subrange_type (die, cu);
a02abb62 21950 break;
c906108c 21951 case DW_TAG_base_type:
f792889a 21952 this_type = read_base_type (die, cu);
c906108c 21953 break;
81a17f79 21954 case DW_TAG_unspecified_type:
f792889a 21955 this_type = read_unspecified_type (die, cu);
81a17f79 21956 break;
0114d602
DJ
21957 case DW_TAG_namespace:
21958 this_type = read_namespace_type (die, cu);
21959 break;
f55ee35c
JK
21960 case DW_TAG_module:
21961 this_type = read_module_type (die, cu);
21962 break;
a2c2acaf
MW
21963 case DW_TAG_atomic_type:
21964 this_type = read_tag_atomic_type (die, cu);
21965 break;
c906108c 21966 default:
b98664d3 21967 complaint (_("unexpected tag in read_type_die: '%s'"),
4d3c2250 21968 dwarf_tag_name (die->tag));
c906108c
SS
21969 break;
21970 }
63d06c5c 21971
f792889a 21972 return this_type;
63d06c5c
DC
21973}
21974
abc72ce4
DE
21975/* See if we can figure out if the class lives in a namespace. We do
21976 this by looking for a member function; its demangled name will
21977 contain namespace info, if there is any.
21978 Return the computed name or NULL.
21979 Space for the result is allocated on the objfile's obstack.
21980 This is the full-die version of guess_partial_die_structure_name.
21981 In this case we know DIE has no useful parent. */
21982
21983static char *
21984guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
21985{
21986 struct die_info *spec_die;
21987 struct dwarf2_cu *spec_cu;
21988 struct die_info *child;
518817b3 21989 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
abc72ce4
DE
21990
21991 spec_cu = cu;
21992 spec_die = die_specification (die, &spec_cu);
21993 if (spec_die != NULL)
21994 {
21995 die = spec_die;
21996 cu = spec_cu;
21997 }
21998
21999 for (child = die->child;
22000 child != NULL;
22001 child = child->sibling)
22002 {
22003 if (child->tag == DW_TAG_subprogram)
22004 {
73b9be8b 22005 const char *linkage_name = dw2_linkage_name (child, cu);
abc72ce4 22006
7d45c7c3 22007 if (linkage_name != NULL)
abc72ce4
DE
22008 {
22009 char *actual_name
22010 = language_class_name_from_physname (cu->language_defn,
7d45c7c3 22011 linkage_name);
abc72ce4
DE
22012 char *name = NULL;
22013
22014 if (actual_name != NULL)
22015 {
15d034d0 22016 const char *die_name = dwarf2_name (die, cu);
abc72ce4
DE
22017
22018 if (die_name != NULL
22019 && strcmp (die_name, actual_name) != 0)
22020 {
22021 /* Strip off the class name from the full name.
22022 We want the prefix. */
22023 int die_name_len = strlen (die_name);
22024 int actual_name_len = strlen (actual_name);
22025
22026 /* Test for '::' as a sanity check. */
22027 if (actual_name_len > die_name_len + 2
3e43a32a
MS
22028 && actual_name[actual_name_len
22029 - die_name_len - 1] == ':')
224c3ddb 22030 name = (char *) obstack_copy0 (
e3b94546 22031 &objfile->per_bfd->storage_obstack,
224c3ddb 22032 actual_name, actual_name_len - die_name_len - 2);
abc72ce4
DE
22033 }
22034 }
22035 xfree (actual_name);
22036 return name;
22037 }
22038 }
22039 }
22040
22041 return NULL;
22042}
22043
96408a79
SA
22044/* GCC might emit a nameless typedef that has a linkage name. Determine the
22045 prefix part in such case. See
22046 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
22047
a121b7c1 22048static const char *
96408a79
SA
22049anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
22050{
22051 struct attribute *attr;
e6a959d6 22052 const char *base;
96408a79
SA
22053
22054 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
22055 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
22056 return NULL;
22057
7d45c7c3 22058 if (dwarf2_string_attr (die, DW_AT_name, cu) != NULL)
96408a79
SA
22059 return NULL;
22060
73b9be8b 22061 attr = dw2_linkage_name_attr (die, cu);
96408a79
SA
22062 if (attr == NULL || DW_STRING (attr) == NULL)
22063 return NULL;
22064
22065 /* dwarf2_name had to be already called. */
22066 gdb_assert (DW_STRING_IS_CANONICAL (attr));
22067
22068 /* Strip the base name, keep any leading namespaces/classes. */
22069 base = strrchr (DW_STRING (attr), ':');
22070 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
22071 return "";
22072
518817b3 22073 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
e3b94546 22074 return (char *) obstack_copy0 (&objfile->per_bfd->storage_obstack,
224c3ddb
SM
22075 DW_STRING (attr),
22076 &base[-1] - DW_STRING (attr));
96408a79
SA
22077}
22078
fdde2d81 22079/* Return the name of the namespace/class that DIE is defined within,
0114d602 22080 or "" if we can't tell. The caller should not xfree the result.
fdde2d81 22081
0114d602
DJ
22082 For example, if we're within the method foo() in the following
22083 code:
22084
22085 namespace N {
22086 class C {
22087 void foo () {
22088 }
22089 };
22090 }
22091
22092 then determine_prefix on foo's die will return "N::C". */
fdde2d81 22093
0d5cff50 22094static const char *
e142c38c 22095determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
63d06c5c 22096{
518817b3
SM
22097 struct dwarf2_per_objfile *dwarf2_per_objfile
22098 = cu->per_cu->dwarf2_per_objfile;
0114d602
DJ
22099 struct die_info *parent, *spec_die;
22100 struct dwarf2_cu *spec_cu;
22101 struct type *parent_type;
a121b7c1 22102 const char *retval;
63d06c5c 22103
9c37b5ae 22104 if (cu->language != language_cplus
c44af4eb
TT
22105 && cu->language != language_fortran && cu->language != language_d
22106 && cu->language != language_rust)
0114d602
DJ
22107 return "";
22108
96408a79
SA
22109 retval = anonymous_struct_prefix (die, cu);
22110 if (retval)
22111 return retval;
22112
0114d602
DJ
22113 /* We have to be careful in the presence of DW_AT_specification.
22114 For example, with GCC 3.4, given the code
22115
22116 namespace N {
22117 void foo() {
22118 // Definition of N::foo.
22119 }
22120 }
22121
22122 then we'll have a tree of DIEs like this:
22123
22124 1: DW_TAG_compile_unit
22125 2: DW_TAG_namespace // N
22126 3: DW_TAG_subprogram // declaration of N::foo
22127 4: DW_TAG_subprogram // definition of N::foo
22128 DW_AT_specification // refers to die #3
22129
22130 Thus, when processing die #4, we have to pretend that we're in
22131 the context of its DW_AT_specification, namely the contex of die
22132 #3. */
22133 spec_cu = cu;
22134 spec_die = die_specification (die, &spec_cu);
22135 if (spec_die == NULL)
22136 parent = die->parent;
22137 else
63d06c5c 22138 {
0114d602
DJ
22139 parent = spec_die->parent;
22140 cu = spec_cu;
63d06c5c 22141 }
0114d602
DJ
22142
22143 if (parent == NULL)
22144 return "";
98bfdba5
PA
22145 else if (parent->building_fullname)
22146 {
22147 const char *name;
22148 const char *parent_name;
22149
22150 /* It has been seen on RealView 2.2 built binaries,
22151 DW_TAG_template_type_param types actually _defined_ as
22152 children of the parent class:
22153
22154 enum E {};
22155 template class <class Enum> Class{};
22156 Class<enum E> class_e;
22157
22158 1: DW_TAG_class_type (Class)
22159 2: DW_TAG_enumeration_type (E)
22160 3: DW_TAG_enumerator (enum1:0)
22161 3: DW_TAG_enumerator (enum2:1)
22162 ...
22163 2: DW_TAG_template_type_param
22164 DW_AT_type DW_FORM_ref_udata (E)
22165
22166 Besides being broken debug info, it can put GDB into an
22167 infinite loop. Consider:
22168
22169 When we're building the full name for Class<E>, we'll start
22170 at Class, and go look over its template type parameters,
22171 finding E. We'll then try to build the full name of E, and
22172 reach here. We're now trying to build the full name of E,
22173 and look over the parent DIE for containing scope. In the
22174 broken case, if we followed the parent DIE of E, we'd again
22175 find Class, and once again go look at its template type
22176 arguments, etc., etc. Simply don't consider such parent die
22177 as source-level parent of this die (it can't be, the language
22178 doesn't allow it), and break the loop here. */
22179 name = dwarf2_name (die, cu);
22180 parent_name = dwarf2_name (parent, cu);
b98664d3 22181 complaint (_("template param type '%s' defined within parent '%s'"),
98bfdba5
PA
22182 name ? name : "<unknown>",
22183 parent_name ? parent_name : "<unknown>");
22184 return "";
22185 }
63d06c5c 22186 else
0114d602
DJ
22187 switch (parent->tag)
22188 {
63d06c5c 22189 case DW_TAG_namespace:
0114d602 22190 parent_type = read_type_die (parent, cu);
acebe513
UW
22191 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
22192 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
22193 Work around this problem here. */
22194 if (cu->language == language_cplus
e86ca25f 22195 && strcmp (TYPE_NAME (parent_type), "::") == 0)
acebe513 22196 return "";
0114d602 22197 /* We give a name to even anonymous namespaces. */
e86ca25f 22198 return TYPE_NAME (parent_type);
63d06c5c 22199 case DW_TAG_class_type:
680b30c7 22200 case DW_TAG_interface_type:
63d06c5c 22201 case DW_TAG_structure_type:
0114d602 22202 case DW_TAG_union_type:
f55ee35c 22203 case DW_TAG_module:
0114d602 22204 parent_type = read_type_die (parent, cu);
e86ca25f
TT
22205 if (TYPE_NAME (parent_type) != NULL)
22206 return TYPE_NAME (parent_type);
0114d602
DJ
22207 else
22208 /* An anonymous structure is only allowed non-static data
22209 members; no typedefs, no member functions, et cetera.
22210 So it does not need a prefix. */
22211 return "";
abc72ce4 22212 case DW_TAG_compile_unit:
95554aad 22213 case DW_TAG_partial_unit:
abc72ce4
DE
22214 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
22215 if (cu->language == language_cplus
8b70b953 22216 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
abc72ce4
DE
22217 && die->child != NULL
22218 && (die->tag == DW_TAG_class_type
22219 || die->tag == DW_TAG_structure_type
22220 || die->tag == DW_TAG_union_type))
22221 {
22222 char *name = guess_full_die_structure_name (die, cu);
22223 if (name != NULL)
22224 return name;
22225 }
22226 return "";
3d567982
TT
22227 case DW_TAG_enumeration_type:
22228 parent_type = read_type_die (parent, cu);
22229 if (TYPE_DECLARED_CLASS (parent_type))
22230 {
e86ca25f
TT
22231 if (TYPE_NAME (parent_type) != NULL)
22232 return TYPE_NAME (parent_type);
3d567982
TT
22233 return "";
22234 }
22235 /* Fall through. */
63d06c5c 22236 default:
8176b9b8 22237 return determine_prefix (parent, cu);
63d06c5c 22238 }
63d06c5c
DC
22239}
22240
3e43a32a
MS
22241/* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
22242 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
22243 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
22244 an obconcat, otherwise allocate storage for the result. The CU argument is
22245 used to determine the language and hence, the appropriate separator. */
987504bb 22246
f55ee35c 22247#define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
63d06c5c
DC
22248
22249static char *
f55ee35c
JK
22250typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
22251 int physname, struct dwarf2_cu *cu)
63d06c5c 22252{
f55ee35c 22253 const char *lead = "";
5c315b68 22254 const char *sep;
63d06c5c 22255
3e43a32a
MS
22256 if (suffix == NULL || suffix[0] == '\0'
22257 || prefix == NULL || prefix[0] == '\0')
987504bb 22258 sep = "";
45280282
IB
22259 else if (cu->language == language_d)
22260 {
22261 /* For D, the 'main' function could be defined in any module, but it
22262 should never be prefixed. */
22263 if (strcmp (suffix, "D main") == 0)
22264 {
22265 prefix = "";
22266 sep = "";
22267 }
22268 else
22269 sep = ".";
22270 }
f55ee35c
JK
22271 else if (cu->language == language_fortran && physname)
22272 {
22273 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
22274 DW_AT_MIPS_linkage_name is preferred and used instead. */
22275
22276 lead = "__";
22277 sep = "_MOD_";
22278 }
987504bb
JJ
22279 else
22280 sep = "::";
63d06c5c 22281
6dd47d34
DE
22282 if (prefix == NULL)
22283 prefix = "";
22284 if (suffix == NULL)
22285 suffix = "";
22286
987504bb
JJ
22287 if (obs == NULL)
22288 {
3e43a32a 22289 char *retval
224c3ddb
SM
22290 = ((char *)
22291 xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1));
9a619af0 22292
f55ee35c
JK
22293 strcpy (retval, lead);
22294 strcat (retval, prefix);
6dd47d34
DE
22295 strcat (retval, sep);
22296 strcat (retval, suffix);
63d06c5c
DC
22297 return retval;
22298 }
987504bb
JJ
22299 else
22300 {
22301 /* We have an obstack. */
f55ee35c 22302 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
987504bb 22303 }
63d06c5c
DC
22304}
22305
c906108c
SS
22306/* Return sibling of die, NULL if no sibling. */
22307
f9aca02d 22308static struct die_info *
fba45db2 22309sibling_die (struct die_info *die)
c906108c 22310{
639d11d3 22311 return die->sibling;
c906108c
SS
22312}
22313
71c25dea
TT
22314/* Get name of a die, return NULL if not found. */
22315
15d034d0
TT
22316static const char *
22317dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
71c25dea
TT
22318 struct obstack *obstack)
22319{
22320 if (name && cu->language == language_cplus)
22321 {
2f408ecb 22322 std::string canon_name = cp_canonicalize_string (name);
71c25dea 22323
2f408ecb 22324 if (!canon_name.empty ())
71c25dea 22325 {
2f408ecb
PA
22326 if (canon_name != name)
22327 name = (const char *) obstack_copy0 (obstack,
22328 canon_name.c_str (),
22329 canon_name.length ());
71c25dea
TT
22330 }
22331 }
22332
22333 return name;
c906108c
SS
22334}
22335
96553a0c
DE
22336/* Get name of a die, return NULL if not found.
22337 Anonymous namespaces are converted to their magic string. */
9219021c 22338
15d034d0 22339static const char *
e142c38c 22340dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
9219021c
DC
22341{
22342 struct attribute *attr;
518817b3 22343 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
9219021c 22344
e142c38c 22345 attr = dwarf2_attr (die, DW_AT_name, cu);
53832f31 22346 if ((!attr || !DW_STRING (attr))
96553a0c 22347 && die->tag != DW_TAG_namespace
53832f31
TT
22348 && die->tag != DW_TAG_class_type
22349 && die->tag != DW_TAG_interface_type
22350 && die->tag != DW_TAG_structure_type
22351 && die->tag != DW_TAG_union_type)
71c25dea
TT
22352 return NULL;
22353
22354 switch (die->tag)
22355 {
22356 case DW_TAG_compile_unit:
95554aad 22357 case DW_TAG_partial_unit:
71c25dea
TT
22358 /* Compilation units have a DW_AT_name that is a filename, not
22359 a source language identifier. */
22360 case DW_TAG_enumeration_type:
22361 case DW_TAG_enumerator:
22362 /* These tags always have simple identifiers already; no need
22363 to canonicalize them. */
22364 return DW_STRING (attr);
907af001 22365
96553a0c
DE
22366 case DW_TAG_namespace:
22367 if (attr != NULL && DW_STRING (attr) != NULL)
22368 return DW_STRING (attr);
22369 return CP_ANONYMOUS_NAMESPACE_STR;
22370
907af001
UW
22371 case DW_TAG_class_type:
22372 case DW_TAG_interface_type:
22373 case DW_TAG_structure_type:
22374 case DW_TAG_union_type:
22375 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
22376 structures or unions. These were of the form "._%d" in GCC 4.1,
22377 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
22378 and GCC 4.4. We work around this problem by ignoring these. */
53832f31 22379 if (attr && DW_STRING (attr)
61012eef
GB
22380 && (startswith (DW_STRING (attr), "._")
22381 || startswith (DW_STRING (attr), "<anonymous")))
907af001 22382 return NULL;
53832f31
TT
22383
22384 /* GCC might emit a nameless typedef that has a linkage name. See
22385 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
22386 if (!attr || DW_STRING (attr) == NULL)
22387 {
df5c6c50 22388 char *demangled = NULL;
53832f31 22389
73b9be8b 22390 attr = dw2_linkage_name_attr (die, cu);
53832f31
TT
22391 if (attr == NULL || DW_STRING (attr) == NULL)
22392 return NULL;
22393
df5c6c50
JK
22394 /* Avoid demangling DW_STRING (attr) the second time on a second
22395 call for the same DIE. */
22396 if (!DW_STRING_IS_CANONICAL (attr))
8de20a37 22397 demangled = gdb_demangle (DW_STRING (attr), DMGL_TYPES);
53832f31
TT
22398
22399 if (demangled)
22400 {
e6a959d6 22401 const char *base;
96408a79 22402
53832f31 22403 /* FIXME: we already did this for the partial symbol... */
34a68019 22404 DW_STRING (attr)
224c3ddb 22405 = ((const char *)
e3b94546 22406 obstack_copy0 (&objfile->per_bfd->storage_obstack,
224c3ddb 22407 demangled, strlen (demangled)));
53832f31
TT
22408 DW_STRING_IS_CANONICAL (attr) = 1;
22409 xfree (demangled);
96408a79
SA
22410
22411 /* Strip any leading namespaces/classes, keep only the base name.
22412 DW_AT_name for named DIEs does not contain the prefixes. */
22413 base = strrchr (DW_STRING (attr), ':');
22414 if (base && base > DW_STRING (attr) && base[-1] == ':')
22415 return &base[1];
22416 else
22417 return DW_STRING (attr);
53832f31
TT
22418 }
22419 }
907af001
UW
22420 break;
22421
71c25dea 22422 default:
907af001
UW
22423 break;
22424 }
22425
22426 if (!DW_STRING_IS_CANONICAL (attr))
22427 {
22428 DW_STRING (attr)
22429 = dwarf2_canonicalize_name (DW_STRING (attr), cu,
e3b94546 22430 &objfile->per_bfd->storage_obstack);
907af001 22431 DW_STRING_IS_CANONICAL (attr) = 1;
71c25dea 22432 }
907af001 22433 return DW_STRING (attr);
9219021c
DC
22434}
22435
22436/* Return the die that this die in an extension of, or NULL if there
f2f0e013
DJ
22437 is none. *EXT_CU is the CU containing DIE on input, and the CU
22438 containing the return value on output. */
9219021c
DC
22439
22440static struct die_info *
f2f0e013 22441dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
9219021c
DC
22442{
22443 struct attribute *attr;
9219021c 22444
f2f0e013 22445 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
9219021c
DC
22446 if (attr == NULL)
22447 return NULL;
22448
f2f0e013 22449 return follow_die_ref (die, attr, ext_cu);
9219021c
DC
22450}
22451
c906108c
SS
22452/* Convert a DIE tag into its string name. */
22453
f39c6ffd 22454static const char *
aa1ee363 22455dwarf_tag_name (unsigned tag)
c906108c 22456{
f39c6ffd
TT
22457 const char *name = get_DW_TAG_name (tag);
22458
22459 if (name == NULL)
22460 return "DW_TAG_<unknown>";
22461
22462 return name;
c906108c
SS
22463}
22464
22465/* Convert a DWARF attribute code into its string name. */
22466
f39c6ffd 22467static const char *
aa1ee363 22468dwarf_attr_name (unsigned attr)
c906108c 22469{
f39c6ffd
TT
22470 const char *name;
22471
c764a876 22472#ifdef MIPS /* collides with DW_AT_HP_block_index */
f39c6ffd
TT
22473 if (attr == DW_AT_MIPS_fde)
22474 return "DW_AT_MIPS_fde";
22475#else
22476 if (attr == DW_AT_HP_block_index)
22477 return "DW_AT_HP_block_index";
c764a876 22478#endif
f39c6ffd
TT
22479
22480 name = get_DW_AT_name (attr);
22481
22482 if (name == NULL)
22483 return "DW_AT_<unknown>";
22484
22485 return name;
c906108c
SS
22486}
22487
22488/* Convert a DWARF value form code into its string name. */
22489
f39c6ffd 22490static const char *
aa1ee363 22491dwarf_form_name (unsigned form)
c906108c 22492{
f39c6ffd
TT
22493 const char *name = get_DW_FORM_name (form);
22494
22495 if (name == NULL)
22496 return "DW_FORM_<unknown>";
22497
22498 return name;
c906108c
SS
22499}
22500
a121b7c1 22501static const char *
fba45db2 22502dwarf_bool_name (unsigned mybool)
c906108c
SS
22503{
22504 if (mybool)
22505 return "TRUE";
22506 else
22507 return "FALSE";
22508}
22509
22510/* Convert a DWARF type code into its string name. */
22511
f39c6ffd 22512static const char *
aa1ee363 22513dwarf_type_encoding_name (unsigned enc)
c906108c 22514{
f39c6ffd 22515 const char *name = get_DW_ATE_name (enc);
c906108c 22516
f39c6ffd
TT
22517 if (name == NULL)
22518 return "DW_ATE_<unknown>";
c906108c 22519
f39c6ffd 22520 return name;
c906108c 22521}
c906108c 22522
f9aca02d 22523static void
d97bc12b 22524dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
c906108c
SS
22525{
22526 unsigned int i;
22527
d97bc12b 22528 print_spaces (indent, f);
9d8780f0 22529 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset %s)\n",
9c541725 22530 dwarf_tag_name (die->tag), die->abbrev,
9d8780f0 22531 sect_offset_str (die->sect_off));
d97bc12b
DE
22532
22533 if (die->parent != NULL)
22534 {
22535 print_spaces (indent, f);
9d8780f0
SM
22536 fprintf_unfiltered (f, " parent at offset: %s\n",
22537 sect_offset_str (die->parent->sect_off));
d97bc12b
DE
22538 }
22539
22540 print_spaces (indent, f);
22541 fprintf_unfiltered (f, " has children: %s\n",
639d11d3 22542 dwarf_bool_name (die->child != NULL));
c906108c 22543
d97bc12b
DE
22544 print_spaces (indent, f);
22545 fprintf_unfiltered (f, " attributes:\n");
22546
c906108c
SS
22547 for (i = 0; i < die->num_attrs; ++i)
22548 {
d97bc12b
DE
22549 print_spaces (indent, f);
22550 fprintf_unfiltered (f, " %s (%s) ",
c906108c
SS
22551 dwarf_attr_name (die->attrs[i].name),
22552 dwarf_form_name (die->attrs[i].form));
d97bc12b 22553
c906108c
SS
22554 switch (die->attrs[i].form)
22555 {
c906108c 22556 case DW_FORM_addr:
3019eac3 22557 case DW_FORM_GNU_addr_index:
d97bc12b 22558 fprintf_unfiltered (f, "address: ");
5af949e3 22559 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
c906108c
SS
22560 break;
22561 case DW_FORM_block2:
22562 case DW_FORM_block4:
22563 case DW_FORM_block:
22564 case DW_FORM_block1:
56eb65bd
SP
22565 fprintf_unfiltered (f, "block: size %s",
22566 pulongest (DW_BLOCK (&die->attrs[i])->size));
c906108c 22567 break;
2dc7f7b3 22568 case DW_FORM_exprloc:
56eb65bd
SP
22569 fprintf_unfiltered (f, "expression: size %s",
22570 pulongest (DW_BLOCK (&die->attrs[i])->size));
2dc7f7b3 22571 break;
0224619f
JK
22572 case DW_FORM_data16:
22573 fprintf_unfiltered (f, "constant of 16 bytes");
22574 break;
4568ecf9
DE
22575 case DW_FORM_ref_addr:
22576 fprintf_unfiltered (f, "ref address: ");
22577 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
22578 break;
36586728
TT
22579 case DW_FORM_GNU_ref_alt:
22580 fprintf_unfiltered (f, "alt ref address: ");
22581 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
22582 break;
10b3939b
DJ
22583 case DW_FORM_ref1:
22584 case DW_FORM_ref2:
22585 case DW_FORM_ref4:
4568ecf9
DE
22586 case DW_FORM_ref8:
22587 case DW_FORM_ref_udata:
d97bc12b 22588 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
4568ecf9 22589 (long) (DW_UNSND (&die->attrs[i])));
10b3939b 22590 break;
c906108c
SS
22591 case DW_FORM_data1:
22592 case DW_FORM_data2:
22593 case DW_FORM_data4:
ce5d95e1 22594 case DW_FORM_data8:
c906108c
SS
22595 case DW_FORM_udata:
22596 case DW_FORM_sdata:
43bbcdc2
PH
22597 fprintf_unfiltered (f, "constant: %s",
22598 pulongest (DW_UNSND (&die->attrs[i])));
c906108c 22599 break;
2dc7f7b3
TT
22600 case DW_FORM_sec_offset:
22601 fprintf_unfiltered (f, "section offset: %s",
22602 pulongest (DW_UNSND (&die->attrs[i])));
22603 break;
55f1336d 22604 case DW_FORM_ref_sig8:
ac9ec31b
DE
22605 fprintf_unfiltered (f, "signature: %s",
22606 hex_string (DW_SIGNATURE (&die->attrs[i])));
348e048f 22607 break;
c906108c 22608 case DW_FORM_string:
4bdf3d34 22609 case DW_FORM_strp:
43988095 22610 case DW_FORM_line_strp:
3019eac3 22611 case DW_FORM_GNU_str_index:
36586728 22612 case DW_FORM_GNU_strp_alt:
8285870a 22613 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
c906108c 22614 DW_STRING (&die->attrs[i])
8285870a
JK
22615 ? DW_STRING (&die->attrs[i]) : "",
22616 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
c906108c
SS
22617 break;
22618 case DW_FORM_flag:
22619 if (DW_UNSND (&die->attrs[i]))
d97bc12b 22620 fprintf_unfiltered (f, "flag: TRUE");
c906108c 22621 else
d97bc12b 22622 fprintf_unfiltered (f, "flag: FALSE");
c906108c 22623 break;
2dc7f7b3
TT
22624 case DW_FORM_flag_present:
22625 fprintf_unfiltered (f, "flag: TRUE");
22626 break;
a8329558 22627 case DW_FORM_indirect:
0963b4bd
MS
22628 /* The reader will have reduced the indirect form to
22629 the "base form" so this form should not occur. */
3e43a32a
MS
22630 fprintf_unfiltered (f,
22631 "unexpected attribute form: DW_FORM_indirect");
a8329558 22632 break;
663c44ac
JK
22633 case DW_FORM_implicit_const:
22634 fprintf_unfiltered (f, "constant: %s",
22635 plongest (DW_SND (&die->attrs[i])));
22636 break;
c906108c 22637 default:
d97bc12b 22638 fprintf_unfiltered (f, "unsupported attribute form: %d.",
c5aa993b 22639 die->attrs[i].form);
d97bc12b 22640 break;
c906108c 22641 }
d97bc12b 22642 fprintf_unfiltered (f, "\n");
c906108c
SS
22643 }
22644}
22645
f9aca02d 22646static void
d97bc12b 22647dump_die_for_error (struct die_info *die)
c906108c 22648{
d97bc12b
DE
22649 dump_die_shallow (gdb_stderr, 0, die);
22650}
22651
22652static void
22653dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
22654{
22655 int indent = level * 4;
22656
22657 gdb_assert (die != NULL);
22658
22659 if (level >= max_level)
22660 return;
22661
22662 dump_die_shallow (f, indent, die);
22663
22664 if (die->child != NULL)
c906108c 22665 {
d97bc12b
DE
22666 print_spaces (indent, f);
22667 fprintf_unfiltered (f, " Children:");
22668 if (level + 1 < max_level)
22669 {
22670 fprintf_unfiltered (f, "\n");
22671 dump_die_1 (f, level + 1, max_level, die->child);
22672 }
22673 else
22674 {
3e43a32a
MS
22675 fprintf_unfiltered (f,
22676 " [not printed, max nesting level reached]\n");
d97bc12b
DE
22677 }
22678 }
22679
22680 if (die->sibling != NULL && level > 0)
22681 {
22682 dump_die_1 (f, level, max_level, die->sibling);
c906108c
SS
22683 }
22684}
22685
d97bc12b
DE
22686/* This is called from the pdie macro in gdbinit.in.
22687 It's not static so gcc will keep a copy callable from gdb. */
22688
22689void
22690dump_die (struct die_info *die, int max_level)
22691{
22692 dump_die_1 (gdb_stdlog, 0, max_level, die);
22693}
22694
f9aca02d 22695static void
51545339 22696store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
c906108c 22697{
51545339 22698 void **slot;
c906108c 22699
9c541725
PA
22700 slot = htab_find_slot_with_hash (cu->die_hash, die,
22701 to_underlying (die->sect_off),
b64f50a1 22702 INSERT);
51545339
DJ
22703
22704 *slot = die;
c906108c
SS
22705}
22706
b64f50a1
JK
22707/* Return DIE offset of ATTR. Return 0 with complaint if ATTR is not of the
22708 required kind. */
22709
22710static sect_offset
ff39bb5e 22711dwarf2_get_ref_die_offset (const struct attribute *attr)
93311388 22712{
7771576e 22713 if (attr_form_is_ref (attr))
9c541725 22714 return (sect_offset) DW_UNSND (attr);
93311388 22715
b98664d3 22716 complaint (_("unsupported die ref attribute form: '%s'"),
93311388 22717 dwarf_form_name (attr->form));
9c541725 22718 return {};
c906108c
SS
22719}
22720
43bbcdc2
PH
22721/* Return the constant value held by ATTR. Return DEFAULT_VALUE if
22722 * the value held by the attribute is not constant. */
a02abb62 22723
43bbcdc2 22724static LONGEST
ff39bb5e 22725dwarf2_get_attr_constant_value (const struct attribute *attr, int default_value)
a02abb62 22726{
663c44ac 22727 if (attr->form == DW_FORM_sdata || attr->form == DW_FORM_implicit_const)
a02abb62
JB
22728 return DW_SND (attr);
22729 else if (attr->form == DW_FORM_udata
22730 || attr->form == DW_FORM_data1
22731 || attr->form == DW_FORM_data2
22732 || attr->form == DW_FORM_data4
22733 || attr->form == DW_FORM_data8)
22734 return DW_UNSND (attr);
22735 else
22736 {
0224619f 22737 /* For DW_FORM_data16 see attr_form_is_constant. */
b98664d3 22738 complaint (_("Attribute value is not a constant (%s)"),
a02abb62
JB
22739 dwarf_form_name (attr->form));
22740 return default_value;
22741 }
22742}
22743
348e048f
DE
22744/* Follow reference or signature attribute ATTR of SRC_DIE.
22745 On entry *REF_CU is the CU of SRC_DIE.
22746 On exit *REF_CU is the CU of the result. */
22747
22748static struct die_info *
ff39bb5e 22749follow_die_ref_or_sig (struct die_info *src_die, const struct attribute *attr,
348e048f
DE
22750 struct dwarf2_cu **ref_cu)
22751{
22752 struct die_info *die;
22753
7771576e 22754 if (attr_form_is_ref (attr))
348e048f 22755 die = follow_die_ref (src_die, attr, ref_cu);
55f1336d 22756 else if (attr->form == DW_FORM_ref_sig8)
348e048f
DE
22757 die = follow_die_sig (src_die, attr, ref_cu);
22758 else
22759 {
22760 dump_die_for_error (src_die);
22761 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
518817b3 22762 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
348e048f
DE
22763 }
22764
22765 return die;
03dd20cc
DJ
22766}
22767
5c631832 22768/* Follow reference OFFSET.
673bfd45
DE
22769 On entry *REF_CU is the CU of the source die referencing OFFSET.
22770 On exit *REF_CU is the CU of the result.
22771 Returns NULL if OFFSET is invalid. */
f504f079 22772
f9aca02d 22773static struct die_info *
9c541725 22774follow_die_offset (sect_offset sect_off, int offset_in_dwz,
36586728 22775 struct dwarf2_cu **ref_cu)
c906108c 22776{
10b3939b 22777 struct die_info temp_die;
f2f0e013 22778 struct dwarf2_cu *target_cu, *cu = *ref_cu;
518817b3
SM
22779 struct dwarf2_per_objfile *dwarf2_per_objfile
22780 = cu->per_cu->dwarf2_per_objfile;
10b3939b 22781
348e048f
DE
22782 gdb_assert (cu->per_cu != NULL);
22783
98bfdba5
PA
22784 target_cu = cu;
22785
3019eac3 22786 if (cu->per_cu->is_debug_types)
348e048f
DE
22787 {
22788 /* .debug_types CUs cannot reference anything outside their CU.
22789 If they need to, they have to reference a signatured type via
55f1336d 22790 DW_FORM_ref_sig8. */
9c541725 22791 if (!offset_in_cu_p (&cu->header, sect_off))
5c631832 22792 return NULL;
348e048f 22793 }
36586728 22794 else if (offset_in_dwz != cu->per_cu->is_dwz
9c541725 22795 || !offset_in_cu_p (&cu->header, sect_off))
10b3939b
DJ
22796 {
22797 struct dwarf2_per_cu_data *per_cu;
9a619af0 22798
9c541725 22799 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
ed2dc618 22800 dwarf2_per_objfile);
03dd20cc
DJ
22801
22802 /* If necessary, add it to the queue and load its DIEs. */
95554aad 22803 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
58f0c718 22804 load_full_comp_unit (per_cu, false, cu->language);
03dd20cc 22805
10b3939b
DJ
22806 target_cu = per_cu->cu;
22807 }
98bfdba5
PA
22808 else if (cu->dies == NULL)
22809 {
22810 /* We're loading full DIEs during partial symbol reading. */
22811 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
58f0c718 22812 load_full_comp_unit (cu->per_cu, false, language_minimal);
98bfdba5 22813 }
c906108c 22814
f2f0e013 22815 *ref_cu = target_cu;
9c541725 22816 temp_die.sect_off = sect_off;
9a3c8263 22817 return (struct die_info *) htab_find_with_hash (target_cu->die_hash,
9c541725
PA
22818 &temp_die,
22819 to_underlying (sect_off));
5c631832 22820}
10b3939b 22821
5c631832
JK
22822/* Follow reference attribute ATTR of SRC_DIE.
22823 On entry *REF_CU is the CU of SRC_DIE.
22824 On exit *REF_CU is the CU of the result. */
22825
22826static struct die_info *
ff39bb5e 22827follow_die_ref (struct die_info *src_die, const struct attribute *attr,
5c631832
JK
22828 struct dwarf2_cu **ref_cu)
22829{
9c541725 22830 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
5c631832
JK
22831 struct dwarf2_cu *cu = *ref_cu;
22832 struct die_info *die;
22833
9c541725 22834 die = follow_die_offset (sect_off,
36586728
TT
22835 (attr->form == DW_FORM_GNU_ref_alt
22836 || cu->per_cu->is_dwz),
22837 ref_cu);
5c631832 22838 if (!die)
9d8780f0
SM
22839 error (_("Dwarf Error: Cannot find DIE at %s referenced from DIE "
22840 "at %s [in module %s]"),
22841 sect_offset_str (sect_off), sect_offset_str (src_die->sect_off),
518817b3 22842 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
348e048f 22843
5c631832
JK
22844 return die;
22845}
22846
9c541725 22847/* Return DWARF block referenced by DW_AT_location of DIE at SECT_OFF at PER_CU.
d83e736b 22848 Returned value is intended for DW_OP_call*. Returned
e3b94546
SM
22849 dwarf2_locexpr_baton->data has lifetime of
22850 PER_CU->DWARF2_PER_OBJFILE->OBJFILE. */
5c631832
JK
22851
22852struct dwarf2_locexpr_baton
9c541725 22853dwarf2_fetch_die_loc_sect_off (sect_offset sect_off,
8b9737bf
TT
22854 struct dwarf2_per_cu_data *per_cu,
22855 CORE_ADDR (*get_frame_pc) (void *baton),
22856 void *baton)
5c631832 22857{
918dd910 22858 struct dwarf2_cu *cu;
5c631832
JK
22859 struct die_info *die;
22860 struct attribute *attr;
22861 struct dwarf2_locexpr_baton retval;
12359b5e
SM
22862 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
22863 struct objfile *objfile = dwarf2_per_objfile->objfile;
8cf6f0b1 22864
918dd910 22865 if (per_cu->cu == NULL)
58f0c718 22866 load_cu (per_cu, false);
918dd910 22867 cu = per_cu->cu;
cc12ce38
DE
22868 if (cu == NULL)
22869 {
22870 /* We shouldn't get here for a dummy CU, but don't crash on the user.
22871 Instead just throw an error, not much else we can do. */
9d8780f0
SM
22872 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
22873 sect_offset_str (sect_off), objfile_name (objfile));
cc12ce38 22874 }
918dd910 22875
9c541725 22876 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
5c631832 22877 if (!die)
9d8780f0
SM
22878 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
22879 sect_offset_str (sect_off), objfile_name (objfile));
5c631832
JK
22880
22881 attr = dwarf2_attr (die, DW_AT_location, cu);
22882 if (!attr)
22883 {
e103e986
JK
22884 /* DWARF: "If there is no such attribute, then there is no effect.".
22885 DATA is ignored if SIZE is 0. */
5c631832 22886
e103e986 22887 retval.data = NULL;
5c631832
JK
22888 retval.size = 0;
22889 }
8cf6f0b1
TT
22890 else if (attr_form_is_section_offset (attr))
22891 {
22892 struct dwarf2_loclist_baton loclist_baton;
22893 CORE_ADDR pc = (*get_frame_pc) (baton);
22894 size_t size;
22895
22896 fill_in_loclist_baton (cu, &loclist_baton, attr);
22897
22898 retval.data = dwarf2_find_location_expression (&loclist_baton,
22899 &size, pc);
22900 retval.size = size;
22901 }
5c631832
JK
22902 else
22903 {
22904 if (!attr_form_is_block (attr))
9d8780f0 22905 error (_("Dwarf Error: DIE at %s referenced in module %s "
5c631832 22906 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
9d8780f0 22907 sect_offset_str (sect_off), objfile_name (objfile));
5c631832
JK
22908
22909 retval.data = DW_BLOCK (attr)->data;
22910 retval.size = DW_BLOCK (attr)->size;
22911 }
22912 retval.per_cu = cu->per_cu;
918dd910 22913
ed2dc618 22914 age_cached_comp_units (dwarf2_per_objfile);
918dd910 22915
5c631832 22916 return retval;
348e048f
DE
22917}
22918
8b9737bf
TT
22919/* Like dwarf2_fetch_die_loc_sect_off, but take a CU
22920 offset. */
22921
22922struct dwarf2_locexpr_baton
22923dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
22924 struct dwarf2_per_cu_data *per_cu,
22925 CORE_ADDR (*get_frame_pc) (void *baton),
22926 void *baton)
22927{
9c541725 22928 sect_offset sect_off = per_cu->sect_off + to_underlying (offset_in_cu);
8b9737bf 22929
9c541725 22930 return dwarf2_fetch_die_loc_sect_off (sect_off, per_cu, get_frame_pc, baton);
8b9737bf
TT
22931}
22932
b6807d98
TT
22933/* Write a constant of a given type as target-ordered bytes into
22934 OBSTACK. */
22935
22936static const gdb_byte *
22937write_constant_as_bytes (struct obstack *obstack,
22938 enum bfd_endian byte_order,
22939 struct type *type,
22940 ULONGEST value,
22941 LONGEST *len)
22942{
22943 gdb_byte *result;
22944
22945 *len = TYPE_LENGTH (type);
224c3ddb 22946 result = (gdb_byte *) obstack_alloc (obstack, *len);
b6807d98
TT
22947 store_unsigned_integer (result, *len, byte_order, value);
22948
22949 return result;
22950}
22951
22952/* If the DIE at OFFSET in PER_CU has a DW_AT_const_value, return a
22953 pointer to the constant bytes and set LEN to the length of the
22954 data. If memory is needed, allocate it on OBSTACK. If the DIE
22955 does not have a DW_AT_const_value, return NULL. */
22956
22957const gdb_byte *
9c541725 22958dwarf2_fetch_constant_bytes (sect_offset sect_off,
b6807d98
TT
22959 struct dwarf2_per_cu_data *per_cu,
22960 struct obstack *obstack,
22961 LONGEST *len)
22962{
22963 struct dwarf2_cu *cu;
22964 struct die_info *die;
22965 struct attribute *attr;
22966 const gdb_byte *result = NULL;
22967 struct type *type;
22968 LONGEST value;
22969 enum bfd_endian byte_order;
e3b94546 22970 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
b6807d98 22971
b6807d98 22972 if (per_cu->cu == NULL)
58f0c718 22973 load_cu (per_cu, false);
b6807d98 22974 cu = per_cu->cu;
cc12ce38
DE
22975 if (cu == NULL)
22976 {
22977 /* We shouldn't get here for a dummy CU, but don't crash on the user.
22978 Instead just throw an error, not much else we can do. */
9d8780f0
SM
22979 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
22980 sect_offset_str (sect_off), objfile_name (objfile));
cc12ce38 22981 }
b6807d98 22982
9c541725 22983 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
b6807d98 22984 if (!die)
9d8780f0
SM
22985 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
22986 sect_offset_str (sect_off), objfile_name (objfile));
b6807d98
TT
22987
22988 attr = dwarf2_attr (die, DW_AT_const_value, cu);
22989 if (attr == NULL)
22990 return NULL;
22991
e3b94546 22992 byte_order = (bfd_big_endian (objfile->obfd)
b6807d98
TT
22993 ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
22994
22995 switch (attr->form)
22996 {
22997 case DW_FORM_addr:
22998 case DW_FORM_GNU_addr_index:
22999 {
23000 gdb_byte *tem;
23001
23002 *len = cu->header.addr_size;
224c3ddb 23003 tem = (gdb_byte *) obstack_alloc (obstack, *len);
b6807d98
TT
23004 store_unsigned_integer (tem, *len, byte_order, DW_ADDR (attr));
23005 result = tem;
23006 }
23007 break;
23008 case DW_FORM_string:
23009 case DW_FORM_strp:
23010 case DW_FORM_GNU_str_index:
23011 case DW_FORM_GNU_strp_alt:
23012 /* DW_STRING is already allocated on the objfile obstack, point
23013 directly to it. */
23014 result = (const gdb_byte *) DW_STRING (attr);
23015 *len = strlen (DW_STRING (attr));
23016 break;
23017 case DW_FORM_block1:
23018 case DW_FORM_block2:
23019 case DW_FORM_block4:
23020 case DW_FORM_block:
23021 case DW_FORM_exprloc:
0224619f 23022 case DW_FORM_data16:
b6807d98
TT
23023 result = DW_BLOCK (attr)->data;
23024 *len = DW_BLOCK (attr)->size;
23025 break;
23026
23027 /* The DW_AT_const_value attributes are supposed to carry the
23028 symbol's value "represented as it would be on the target
23029 architecture." By the time we get here, it's already been
23030 converted to host endianness, so we just need to sign- or
23031 zero-extend it as appropriate. */
23032 case DW_FORM_data1:
23033 type = die_type (die, cu);
23034 result = dwarf2_const_value_data (attr, obstack, cu, &value, 8);
23035 if (result == NULL)
23036 result = write_constant_as_bytes (obstack, byte_order,
23037 type, value, len);
23038 break;
23039 case DW_FORM_data2:
23040 type = die_type (die, cu);
23041 result = dwarf2_const_value_data (attr, obstack, cu, &value, 16);
23042 if (result == NULL)
23043 result = write_constant_as_bytes (obstack, byte_order,
23044 type, value, len);
23045 break;
23046 case DW_FORM_data4:
23047 type = die_type (die, cu);
23048 result = dwarf2_const_value_data (attr, obstack, cu, &value, 32);
23049 if (result == NULL)
23050 result = write_constant_as_bytes (obstack, byte_order,
23051 type, value, len);
23052 break;
23053 case DW_FORM_data8:
23054 type = die_type (die, cu);
23055 result = dwarf2_const_value_data (attr, obstack, cu, &value, 64);
23056 if (result == NULL)
23057 result = write_constant_as_bytes (obstack, byte_order,
23058 type, value, len);
23059 break;
23060
23061 case DW_FORM_sdata:
663c44ac 23062 case DW_FORM_implicit_const:
b6807d98
TT
23063 type = die_type (die, cu);
23064 result = write_constant_as_bytes (obstack, byte_order,
23065 type, DW_SND (attr), len);
23066 break;
23067
23068 case DW_FORM_udata:
23069 type = die_type (die, cu);
23070 result = write_constant_as_bytes (obstack, byte_order,
23071 type, DW_UNSND (attr), len);
23072 break;
23073
23074 default:
b98664d3 23075 complaint (_("unsupported const value attribute form: '%s'"),
b6807d98
TT
23076 dwarf_form_name (attr->form));
23077 break;
23078 }
23079
23080 return result;
23081}
23082
7942e96e
AA
23083/* Return the type of the die at OFFSET in PER_CU. Return NULL if no
23084 valid type for this die is found. */
23085
23086struct type *
9c541725 23087dwarf2_fetch_die_type_sect_off (sect_offset sect_off,
7942e96e
AA
23088 struct dwarf2_per_cu_data *per_cu)
23089{
23090 struct dwarf2_cu *cu;
23091 struct die_info *die;
23092
7942e96e 23093 if (per_cu->cu == NULL)
58f0c718 23094 load_cu (per_cu, false);
7942e96e
AA
23095 cu = per_cu->cu;
23096 if (!cu)
23097 return NULL;
23098
9c541725 23099 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
7942e96e
AA
23100 if (!die)
23101 return NULL;
23102
23103 return die_type (die, cu);
23104}
23105
8a9b8146
TT
23106/* Return the type of the DIE at DIE_OFFSET in the CU named by
23107 PER_CU. */
23108
23109struct type *
b64f50a1 23110dwarf2_get_die_type (cu_offset die_offset,
8a9b8146
TT
23111 struct dwarf2_per_cu_data *per_cu)
23112{
9c541725 23113 sect_offset die_offset_sect = per_cu->sect_off + to_underlying (die_offset);
b64f50a1 23114 return get_die_type_at_offset (die_offset_sect, per_cu);
8a9b8146
TT
23115}
23116
ac9ec31b 23117/* Follow type unit SIG_TYPE referenced by SRC_DIE.
348e048f 23118 On entry *REF_CU is the CU of SRC_DIE.
ac9ec31b
DE
23119 On exit *REF_CU is the CU of the result.
23120 Returns NULL if the referenced DIE isn't found. */
348e048f
DE
23121
23122static struct die_info *
ac9ec31b
DE
23123follow_die_sig_1 (struct die_info *src_die, struct signatured_type *sig_type,
23124 struct dwarf2_cu **ref_cu)
348e048f 23125{
348e048f 23126 struct die_info temp_die;
348e048f
DE
23127 struct dwarf2_cu *sig_cu;
23128 struct die_info *die;
23129
ac9ec31b
DE
23130 /* While it might be nice to assert sig_type->type == NULL here,
23131 we can get here for DW_AT_imported_declaration where we need
23132 the DIE not the type. */
348e048f
DE
23133
23134 /* If necessary, add it to the queue and load its DIEs. */
23135
95554aad 23136 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, language_minimal))
a0f42c21 23137 read_signatured_type (sig_type);
348e048f 23138
348e048f 23139 sig_cu = sig_type->per_cu.cu;
69d751e3 23140 gdb_assert (sig_cu != NULL);
9c541725
PA
23141 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
23142 temp_die.sect_off = sig_type->type_offset_in_section;
9a3c8263 23143 die = (struct die_info *) htab_find_with_hash (sig_cu->die_hash, &temp_die,
9c541725 23144 to_underlying (temp_die.sect_off));
348e048f
DE
23145 if (die)
23146 {
ed2dc618 23147 struct dwarf2_per_objfile *dwarf2_per_objfile
518817b3 23148 = (*ref_cu)->per_cu->dwarf2_per_objfile;
ed2dc618 23149
796a7ff8
DE
23150 /* For .gdb_index version 7 keep track of included TUs.
23151 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
23152 if (dwarf2_per_objfile->index_table != NULL
23153 && dwarf2_per_objfile->index_table->version <= 7)
23154 {
23155 VEC_safe_push (dwarf2_per_cu_ptr,
23156 (*ref_cu)->per_cu->imported_symtabs,
23157 sig_cu->per_cu);
23158 }
23159
348e048f
DE
23160 *ref_cu = sig_cu;
23161 return die;
23162 }
23163
ac9ec31b
DE
23164 return NULL;
23165}
23166
23167/* Follow signatured type referenced by ATTR in SRC_DIE.
23168 On entry *REF_CU is the CU of SRC_DIE.
23169 On exit *REF_CU is the CU of the result.
23170 The result is the DIE of the type.
23171 If the referenced type cannot be found an error is thrown. */
23172
23173static struct die_info *
ff39bb5e 23174follow_die_sig (struct die_info *src_die, const struct attribute *attr,
ac9ec31b
DE
23175 struct dwarf2_cu **ref_cu)
23176{
23177 ULONGEST signature = DW_SIGNATURE (attr);
23178 struct signatured_type *sig_type;
23179 struct die_info *die;
23180
23181 gdb_assert (attr->form == DW_FORM_ref_sig8);
23182
a2ce51a0 23183 sig_type = lookup_signatured_type (*ref_cu, signature);
ac9ec31b
DE
23184 /* sig_type will be NULL if the signatured type is missing from
23185 the debug info. */
23186 if (sig_type == NULL)
23187 {
23188 error (_("Dwarf Error: Cannot find signatured DIE %s referenced"
9d8780f0
SM
23189 " from DIE at %s [in module %s]"),
23190 hex_string (signature), sect_offset_str (src_die->sect_off),
518817b3 23191 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
ac9ec31b
DE
23192 }
23193
23194 die = follow_die_sig_1 (src_die, sig_type, ref_cu);
23195 if (die == NULL)
23196 {
23197 dump_die_for_error (src_die);
23198 error (_("Dwarf Error: Problem reading signatured DIE %s referenced"
9d8780f0
SM
23199 " from DIE at %s [in module %s]"),
23200 hex_string (signature), sect_offset_str (src_die->sect_off),
518817b3 23201 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
ac9ec31b
DE
23202 }
23203
23204 return die;
23205}
23206
23207/* Get the type specified by SIGNATURE referenced in DIE/CU,
23208 reading in and processing the type unit if necessary. */
23209
23210static struct type *
23211get_signatured_type (struct die_info *die, ULONGEST signature,
23212 struct dwarf2_cu *cu)
23213{
518817b3
SM
23214 struct dwarf2_per_objfile *dwarf2_per_objfile
23215 = cu->per_cu->dwarf2_per_objfile;
ac9ec31b
DE
23216 struct signatured_type *sig_type;
23217 struct dwarf2_cu *type_cu;
23218 struct die_info *type_die;
23219 struct type *type;
23220
a2ce51a0 23221 sig_type = lookup_signatured_type (cu, signature);
ac9ec31b
DE
23222 /* sig_type will be NULL if the signatured type is missing from
23223 the debug info. */
23224 if (sig_type == NULL)
23225 {
b98664d3 23226 complaint (_("Dwarf Error: Cannot find signatured DIE %s referenced"
9d8780f0
SM
23227 " from DIE at %s [in module %s]"),
23228 hex_string (signature), sect_offset_str (die->sect_off),
4262abfb 23229 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
23230 return build_error_marker_type (cu, die);
23231 }
23232
23233 /* If we already know the type we're done. */
23234 if (sig_type->type != NULL)
23235 return sig_type->type;
23236
23237 type_cu = cu;
23238 type_die = follow_die_sig_1 (die, sig_type, &type_cu);
23239 if (type_die != NULL)
23240 {
23241 /* N.B. We need to call get_die_type to ensure only one type for this DIE
23242 is created. This is important, for example, because for c++ classes
23243 we need TYPE_NAME set which is only done by new_symbol. Blech. */
23244 type = read_type_die (type_die, type_cu);
23245 if (type == NULL)
23246 {
b98664d3 23247 complaint (_("Dwarf Error: Cannot build signatured type %s"
9d8780f0
SM
23248 " referenced from DIE at %s [in module %s]"),
23249 hex_string (signature), sect_offset_str (die->sect_off),
4262abfb 23250 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
23251 type = build_error_marker_type (cu, die);
23252 }
23253 }
23254 else
23255 {
b98664d3 23256 complaint (_("Dwarf Error: Problem reading signatured DIE %s referenced"
9d8780f0
SM
23257 " from DIE at %s [in module %s]"),
23258 hex_string (signature), sect_offset_str (die->sect_off),
4262abfb 23259 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
23260 type = build_error_marker_type (cu, die);
23261 }
23262 sig_type->type = type;
23263
23264 return type;
23265}
23266
23267/* Get the type specified by the DW_AT_signature ATTR in DIE/CU,
23268 reading in and processing the type unit if necessary. */
23269
23270static struct type *
ff39bb5e 23271get_DW_AT_signature_type (struct die_info *die, const struct attribute *attr,
b385a60d 23272 struct dwarf2_cu *cu) /* ARI: editCase function */
ac9ec31b
DE
23273{
23274 /* Yes, DW_AT_signature can use a non-ref_sig8 reference. */
7771576e 23275 if (attr_form_is_ref (attr))
ac9ec31b
DE
23276 {
23277 struct dwarf2_cu *type_cu = cu;
23278 struct die_info *type_die = follow_die_ref (die, attr, &type_cu);
23279
23280 return read_type_die (type_die, type_cu);
23281 }
23282 else if (attr->form == DW_FORM_ref_sig8)
23283 {
23284 return get_signatured_type (die, DW_SIGNATURE (attr), cu);
23285 }
23286 else
23287 {
518817b3
SM
23288 struct dwarf2_per_objfile *dwarf2_per_objfile
23289 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 23290
b98664d3 23291 complaint (_("Dwarf Error: DW_AT_signature has bad form %s in DIE"
9d8780f0
SM
23292 " at %s [in module %s]"),
23293 dwarf_form_name (attr->form), sect_offset_str (die->sect_off),
4262abfb 23294 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
23295 return build_error_marker_type (cu, die);
23296 }
348e048f
DE
23297}
23298
e5fe5e75 23299/* Load the DIEs associated with type unit PER_CU into memory. */
348e048f
DE
23300
23301static void
e5fe5e75 23302load_full_type_unit (struct dwarf2_per_cu_data *per_cu)
348e048f 23303{
52dc124a 23304 struct signatured_type *sig_type;
348e048f 23305
f4dc4d17
DE
23306 /* Caller is responsible for ensuring type_unit_groups don't get here. */
23307 gdb_assert (! IS_TYPE_UNIT_GROUP (per_cu));
23308
6721b2ec
DE
23309 /* We have the per_cu, but we need the signatured_type.
23310 Fortunately this is an easy translation. */
23311 gdb_assert (per_cu->is_debug_types);
23312 sig_type = (struct signatured_type *) per_cu;
348e048f 23313
6721b2ec 23314 gdb_assert (per_cu->cu == NULL);
348e048f 23315
52dc124a 23316 read_signatured_type (sig_type);
348e048f 23317
6721b2ec 23318 gdb_assert (per_cu->cu != NULL);
348e048f
DE
23319}
23320
dee91e82
DE
23321/* die_reader_func for read_signatured_type.
23322 This is identical to load_full_comp_unit_reader,
23323 but is kept separate for now. */
348e048f
DE
23324
23325static void
dee91e82 23326read_signatured_type_reader (const struct die_reader_specs *reader,
d521ce57 23327 const gdb_byte *info_ptr,
dee91e82
DE
23328 struct die_info *comp_unit_die,
23329 int has_children,
23330 void *data)
348e048f 23331{
dee91e82 23332 struct dwarf2_cu *cu = reader->cu;
348e048f 23333
dee91e82
DE
23334 gdb_assert (cu->die_hash == NULL);
23335 cu->die_hash =
23336 htab_create_alloc_ex (cu->header.length / 12,
23337 die_hash,
23338 die_eq,
23339 NULL,
23340 &cu->comp_unit_obstack,
23341 hashtab_obstack_allocate,
23342 dummy_obstack_deallocate);
348e048f 23343
dee91e82
DE
23344 if (has_children)
23345 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
23346 &info_ptr, comp_unit_die);
23347 cu->dies = comp_unit_die;
23348 /* comp_unit_die is not stored in die_hash, no need. */
348e048f
DE
23349
23350 /* We try not to read any attributes in this function, because not
9cdd5dbd 23351 all CUs needed for references have been loaded yet, and symbol
348e048f 23352 table processing isn't initialized. But we have to set the CU language,
dee91e82
DE
23353 or we won't be able to build types correctly.
23354 Similarly, if we do not read the producer, we can not apply
23355 producer-specific interpretation. */
95554aad 23356 prepare_one_comp_unit (cu, cu->dies, language_minimal);
dee91e82 23357}
348e048f 23358
3019eac3
DE
23359/* Read in a signatured type and build its CU and DIEs.
23360 If the type is a stub for the real type in a DWO file,
23361 read in the real type from the DWO file as well. */
dee91e82
DE
23362
23363static void
23364read_signatured_type (struct signatured_type *sig_type)
23365{
23366 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
348e048f 23367
3019eac3 23368 gdb_assert (per_cu->is_debug_types);
dee91e82 23369 gdb_assert (per_cu->cu == NULL);
348e048f 23370
58f0c718 23371 init_cutu_and_read_dies (per_cu, NULL, 0, 1, false,
f4dc4d17 23372 read_signatured_type_reader, NULL);
7ee85ab1 23373 sig_type->per_cu.tu_read = 1;
c906108c
SS
23374}
23375
c906108c
SS
23376/* Decode simple location descriptions.
23377 Given a pointer to a dwarf block that defines a location, compute
23378 the location and return the value.
23379
4cecd739
DJ
23380 NOTE drow/2003-11-18: This function is called in two situations
23381 now: for the address of static or global variables (partial symbols
23382 only) and for offsets into structures which are expected to be
23383 (more or less) constant. The partial symbol case should go away,
23384 and only the constant case should remain. That will let this
23385 function complain more accurately. A few special modes are allowed
23386 without complaint for global variables (for instance, global
23387 register values and thread-local values).
c906108c
SS
23388
23389 A location description containing no operations indicates that the
4cecd739 23390 object is optimized out. The return value is 0 for that case.
6b992462
DJ
23391 FIXME drow/2003-11-16: No callers check for this case any more; soon all
23392 callers will only want a very basic result and this can become a
21ae7a4d
JK
23393 complaint.
23394
23395 Note that stack[0] is unused except as a default error return. */
c906108c
SS
23396
23397static CORE_ADDR
e7c27a73 23398decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
c906108c 23399{
518817b3 23400 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
56eb65bd
SP
23401 size_t i;
23402 size_t size = blk->size;
d521ce57 23403 const gdb_byte *data = blk->data;
21ae7a4d
JK
23404 CORE_ADDR stack[64];
23405 int stacki;
23406 unsigned int bytes_read, unsnd;
23407 gdb_byte op;
c906108c 23408
21ae7a4d
JK
23409 i = 0;
23410 stacki = 0;
23411 stack[stacki] = 0;
23412 stack[++stacki] = 0;
23413
23414 while (i < size)
23415 {
23416 op = data[i++];
23417 switch (op)
23418 {
23419 case DW_OP_lit0:
23420 case DW_OP_lit1:
23421 case DW_OP_lit2:
23422 case DW_OP_lit3:
23423 case DW_OP_lit4:
23424 case DW_OP_lit5:
23425 case DW_OP_lit6:
23426 case DW_OP_lit7:
23427 case DW_OP_lit8:
23428 case DW_OP_lit9:
23429 case DW_OP_lit10:
23430 case DW_OP_lit11:
23431 case DW_OP_lit12:
23432 case DW_OP_lit13:
23433 case DW_OP_lit14:
23434 case DW_OP_lit15:
23435 case DW_OP_lit16:
23436 case DW_OP_lit17:
23437 case DW_OP_lit18:
23438 case DW_OP_lit19:
23439 case DW_OP_lit20:
23440 case DW_OP_lit21:
23441 case DW_OP_lit22:
23442 case DW_OP_lit23:
23443 case DW_OP_lit24:
23444 case DW_OP_lit25:
23445 case DW_OP_lit26:
23446 case DW_OP_lit27:
23447 case DW_OP_lit28:
23448 case DW_OP_lit29:
23449 case DW_OP_lit30:
23450 case DW_OP_lit31:
23451 stack[++stacki] = op - DW_OP_lit0;
23452 break;
f1bea926 23453
21ae7a4d
JK
23454 case DW_OP_reg0:
23455 case DW_OP_reg1:
23456 case DW_OP_reg2:
23457 case DW_OP_reg3:
23458 case DW_OP_reg4:
23459 case DW_OP_reg5:
23460 case DW_OP_reg6:
23461 case DW_OP_reg7:
23462 case DW_OP_reg8:
23463 case DW_OP_reg9:
23464 case DW_OP_reg10:
23465 case DW_OP_reg11:
23466 case DW_OP_reg12:
23467 case DW_OP_reg13:
23468 case DW_OP_reg14:
23469 case DW_OP_reg15:
23470 case DW_OP_reg16:
23471 case DW_OP_reg17:
23472 case DW_OP_reg18:
23473 case DW_OP_reg19:
23474 case DW_OP_reg20:
23475 case DW_OP_reg21:
23476 case DW_OP_reg22:
23477 case DW_OP_reg23:
23478 case DW_OP_reg24:
23479 case DW_OP_reg25:
23480 case DW_OP_reg26:
23481 case DW_OP_reg27:
23482 case DW_OP_reg28:
23483 case DW_OP_reg29:
23484 case DW_OP_reg30:
23485 case DW_OP_reg31:
23486 stack[++stacki] = op - DW_OP_reg0;
23487 if (i < size)
23488 dwarf2_complex_location_expr_complaint ();
23489 break;
c906108c 23490
21ae7a4d
JK
23491 case DW_OP_regx:
23492 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
23493 i += bytes_read;
23494 stack[++stacki] = unsnd;
23495 if (i < size)
23496 dwarf2_complex_location_expr_complaint ();
23497 break;
c906108c 23498
21ae7a4d
JK
23499 case DW_OP_addr:
23500 stack[++stacki] = read_address (objfile->obfd, &data[i],
23501 cu, &bytes_read);
23502 i += bytes_read;
23503 break;
d53d4ac5 23504
21ae7a4d
JK
23505 case DW_OP_const1u:
23506 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
23507 i += 1;
23508 break;
23509
23510 case DW_OP_const1s:
23511 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
23512 i += 1;
23513 break;
23514
23515 case DW_OP_const2u:
23516 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
23517 i += 2;
23518 break;
23519
23520 case DW_OP_const2s:
23521 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
23522 i += 2;
23523 break;
d53d4ac5 23524
21ae7a4d
JK
23525 case DW_OP_const4u:
23526 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
23527 i += 4;
23528 break;
23529
23530 case DW_OP_const4s:
23531 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
23532 i += 4;
23533 break;
23534
585861ea
JK
23535 case DW_OP_const8u:
23536 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
23537 i += 8;
23538 break;
23539
21ae7a4d
JK
23540 case DW_OP_constu:
23541 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
23542 &bytes_read);
23543 i += bytes_read;
23544 break;
23545
23546 case DW_OP_consts:
23547 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
23548 i += bytes_read;
23549 break;
23550
23551 case DW_OP_dup:
23552 stack[stacki + 1] = stack[stacki];
23553 stacki++;
23554 break;
23555
23556 case DW_OP_plus:
23557 stack[stacki - 1] += stack[stacki];
23558 stacki--;
23559 break;
23560
23561 case DW_OP_plus_uconst:
23562 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
23563 &bytes_read);
23564 i += bytes_read;
23565 break;
23566
23567 case DW_OP_minus:
23568 stack[stacki - 1] -= stack[stacki];
23569 stacki--;
23570 break;
23571
23572 case DW_OP_deref:
23573 /* If we're not the last op, then we definitely can't encode
23574 this using GDB's address_class enum. This is valid for partial
23575 global symbols, although the variable's address will be bogus
23576 in the psymtab. */
23577 if (i < size)
23578 dwarf2_complex_location_expr_complaint ();
23579 break;
23580
23581 case DW_OP_GNU_push_tls_address:
4aa4e28b 23582 case DW_OP_form_tls_address:
21ae7a4d
JK
23583 /* The top of the stack has the offset from the beginning
23584 of the thread control block at which the variable is located. */
23585 /* Nothing should follow this operator, so the top of stack would
23586 be returned. */
23587 /* This is valid for partial global symbols, but the variable's
585861ea
JK
23588 address will be bogus in the psymtab. Make it always at least
23589 non-zero to not look as a variable garbage collected by linker
23590 which have DW_OP_addr 0. */
21ae7a4d
JK
23591 if (i < size)
23592 dwarf2_complex_location_expr_complaint ();
585861ea 23593 stack[stacki]++;
21ae7a4d
JK
23594 break;
23595
23596 case DW_OP_GNU_uninit:
23597 break;
23598
3019eac3 23599 case DW_OP_GNU_addr_index:
49f6c839 23600 case DW_OP_GNU_const_index:
3019eac3
DE
23601 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
23602 &bytes_read);
23603 i += bytes_read;
23604 break;
23605
21ae7a4d
JK
23606 default:
23607 {
f39c6ffd 23608 const char *name = get_DW_OP_name (op);
21ae7a4d
JK
23609
23610 if (name)
b98664d3 23611 complaint (_("unsupported stack op: '%s'"),
21ae7a4d
JK
23612 name);
23613 else
b98664d3 23614 complaint (_("unsupported stack op: '%02x'"),
21ae7a4d
JK
23615 op);
23616 }
23617
23618 return (stack[stacki]);
d53d4ac5 23619 }
3c6e0cb3 23620
21ae7a4d
JK
23621 /* Enforce maximum stack depth of SIZE-1 to avoid writing
23622 outside of the allocated space. Also enforce minimum>0. */
23623 if (stacki >= ARRAY_SIZE (stack) - 1)
23624 {
b98664d3 23625 complaint (_("location description stack overflow"));
21ae7a4d
JK
23626 return 0;
23627 }
23628
23629 if (stacki <= 0)
23630 {
b98664d3 23631 complaint (_("location description stack underflow"));
21ae7a4d
JK
23632 return 0;
23633 }
23634 }
23635 return (stack[stacki]);
c906108c
SS
23636}
23637
23638/* memory allocation interface */
23639
c906108c 23640static struct dwarf_block *
7b5a2f43 23641dwarf_alloc_block (struct dwarf2_cu *cu)
c906108c 23642{
8d749320 23643 return XOBNEW (&cu->comp_unit_obstack, struct dwarf_block);
c906108c
SS
23644}
23645
c906108c 23646static struct die_info *
b60c80d6 23647dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
c906108c
SS
23648{
23649 struct die_info *die;
b60c80d6
DJ
23650 size_t size = sizeof (struct die_info);
23651
23652 if (num_attrs > 1)
23653 size += (num_attrs - 1) * sizeof (struct attribute);
c906108c 23654
b60c80d6 23655 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
c906108c
SS
23656 memset (die, 0, sizeof (struct die_info));
23657 return (die);
23658}
2e276125
JB
23659
23660\f
23661/* Macro support. */
23662
233d95b5
JK
23663/* Return file name relative to the compilation directory of file number I in
23664 *LH's file name table. The result is allocated using xmalloc; the caller is
2e276125 23665 responsible for freeing it. */
233d95b5 23666
2e276125 23667static char *
233d95b5 23668file_file_name (int file, struct line_header *lh)
2e276125 23669{
6a83a1e6
EZ
23670 /* Is the file number a valid index into the line header's file name
23671 table? Remember that file numbers start with one, not zero. */
fff8551c 23672 if (1 <= file && file <= lh->file_names.size ())
6a83a1e6 23673 {
8c43009f 23674 const file_entry &fe = lh->file_names[file - 1];
6e70227d 23675
8c43009f
PA
23676 if (!IS_ABSOLUTE_PATH (fe.name))
23677 {
23678 const char *dir = fe.include_dir (lh);
23679 if (dir != NULL)
23680 return concat (dir, SLASH_STRING, fe.name, (char *) NULL);
23681 }
23682 return xstrdup (fe.name);
6a83a1e6 23683 }
2e276125
JB
23684 else
23685 {
6a83a1e6
EZ
23686 /* The compiler produced a bogus file number. We can at least
23687 record the macro definitions made in the file, even if we
23688 won't be able to find the file by name. */
23689 char fake_name[80];
9a619af0 23690
8c042590
PM
23691 xsnprintf (fake_name, sizeof (fake_name),
23692 "<bad macro file number %d>", file);
2e276125 23693
b98664d3 23694 complaint (_("bad file number in macro information (%d)"),
6a83a1e6 23695 file);
2e276125 23696
6a83a1e6 23697 return xstrdup (fake_name);
2e276125
JB
23698 }
23699}
23700
233d95b5
JK
23701/* Return the full name of file number I in *LH's file name table.
23702 Use COMP_DIR as the name of the current directory of the
23703 compilation. The result is allocated using xmalloc; the caller is
23704 responsible for freeing it. */
23705static char *
23706file_full_name (int file, struct line_header *lh, const char *comp_dir)
23707{
23708 /* Is the file number a valid index into the line header's file name
23709 table? Remember that file numbers start with one, not zero. */
fff8551c 23710 if (1 <= file && file <= lh->file_names.size ())
233d95b5
JK
23711 {
23712 char *relative = file_file_name (file, lh);
23713
23714 if (IS_ABSOLUTE_PATH (relative) || comp_dir == NULL)
23715 return relative;
b36cec19
PA
23716 return reconcat (relative, comp_dir, SLASH_STRING,
23717 relative, (char *) NULL);
233d95b5
JK
23718 }
23719 else
23720 return file_file_name (file, lh);
23721}
23722
2e276125
JB
23723
23724static struct macro_source_file *
804d2729
TT
23725macro_start_file (struct dwarf2_cu *cu,
23726 int file, int line,
2e276125 23727 struct macro_source_file *current_file,
43f3e411 23728 struct line_header *lh)
2e276125 23729{
233d95b5
JK
23730 /* File name relative to the compilation directory of this source file. */
23731 char *file_name = file_file_name (file, lh);
2e276125 23732
2e276125 23733 if (! current_file)
abc9d0dc 23734 {
fc474241
DE
23735 /* Note: We don't create a macro table for this compilation unit
23736 at all until we actually get a filename. */
804d2729 23737 struct macro_table *macro_table = cu->builder->get_macro_table ();
fc474241 23738
abc9d0dc
TT
23739 /* If we have no current file, then this must be the start_file
23740 directive for the compilation unit's main source file. */
fc474241
DE
23741 current_file = macro_set_main (macro_table, file_name);
23742 macro_define_special (macro_table);
abc9d0dc 23743 }
2e276125 23744 else
233d95b5 23745 current_file = macro_include (current_file, line, file_name);
2e276125 23746
233d95b5 23747 xfree (file_name);
6e70227d 23748
2e276125
JB
23749 return current_file;
23750}
23751
2e276125
JB
23752static const char *
23753consume_improper_spaces (const char *p, const char *body)
23754{
23755 if (*p == ' ')
23756 {
b98664d3 23757 complaint (_("macro definition contains spaces "
3e43a32a 23758 "in formal argument list:\n`%s'"),
4d3c2250 23759 body);
2e276125
JB
23760
23761 while (*p == ' ')
23762 p++;
23763 }
23764
23765 return p;
23766}
23767
23768
23769static void
23770parse_macro_definition (struct macro_source_file *file, int line,
23771 const char *body)
23772{
23773 const char *p;
23774
23775 /* The body string takes one of two forms. For object-like macro
23776 definitions, it should be:
23777
23778 <macro name> " " <definition>
23779
23780 For function-like macro definitions, it should be:
23781
23782 <macro name> "() " <definition>
23783 or
23784 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
23785
23786 Spaces may appear only where explicitly indicated, and in the
23787 <definition>.
23788
23789 The Dwarf 2 spec says that an object-like macro's name is always
23790 followed by a space, but versions of GCC around March 2002 omit
6e70227d 23791 the space when the macro's definition is the empty string.
2e276125
JB
23792
23793 The Dwarf 2 spec says that there should be no spaces between the
23794 formal arguments in a function-like macro's formal argument list,
23795 but versions of GCC around March 2002 include spaces after the
23796 commas. */
23797
23798
23799 /* Find the extent of the macro name. The macro name is terminated
23800 by either a space or null character (for an object-like macro) or
23801 an opening paren (for a function-like macro). */
23802 for (p = body; *p; p++)
23803 if (*p == ' ' || *p == '(')
23804 break;
23805
23806 if (*p == ' ' || *p == '\0')
23807 {
23808 /* It's an object-like macro. */
23809 int name_len = p - body;
3f8a7804 23810 char *name = savestring (body, name_len);
2e276125
JB
23811 const char *replacement;
23812
23813 if (*p == ' ')
23814 replacement = body + name_len + 1;
23815 else
23816 {
4d3c2250 23817 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
23818 replacement = body + name_len;
23819 }
6e70227d 23820
2e276125
JB
23821 macro_define_object (file, line, name, replacement);
23822
23823 xfree (name);
23824 }
23825 else if (*p == '(')
23826 {
23827 /* It's a function-like macro. */
3f8a7804 23828 char *name = savestring (body, p - body);
2e276125
JB
23829 int argc = 0;
23830 int argv_size = 1;
8d749320 23831 char **argv = XNEWVEC (char *, argv_size);
2e276125
JB
23832
23833 p++;
23834
23835 p = consume_improper_spaces (p, body);
23836
23837 /* Parse the formal argument list. */
23838 while (*p && *p != ')')
23839 {
23840 /* Find the extent of the current argument name. */
23841 const char *arg_start = p;
23842
23843 while (*p && *p != ',' && *p != ')' && *p != ' ')
23844 p++;
23845
23846 if (! *p || p == arg_start)
4d3c2250 23847 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
23848 else
23849 {
23850 /* Make sure argv has room for the new argument. */
23851 if (argc >= argv_size)
23852 {
23853 argv_size *= 2;
224c3ddb 23854 argv = XRESIZEVEC (char *, argv, argv_size);
2e276125
JB
23855 }
23856
3f8a7804 23857 argv[argc++] = savestring (arg_start, p - arg_start);
2e276125
JB
23858 }
23859
23860 p = consume_improper_spaces (p, body);
23861
23862 /* Consume the comma, if present. */
23863 if (*p == ',')
23864 {
23865 p++;
23866
23867 p = consume_improper_spaces (p, body);
23868 }
23869 }
23870
23871 if (*p == ')')
23872 {
23873 p++;
23874
23875 if (*p == ' ')
23876 /* Perfectly formed definition, no complaints. */
23877 macro_define_function (file, line, name,
6e70227d 23878 argc, (const char **) argv,
2e276125
JB
23879 p + 1);
23880 else if (*p == '\0')
23881 {
23882 /* Complain, but do define it. */
4d3c2250 23883 dwarf2_macro_malformed_definition_complaint (body);
2e276125 23884 macro_define_function (file, line, name,
6e70227d 23885 argc, (const char **) argv,
2e276125
JB
23886 p);
23887 }
23888 else
23889 /* Just complain. */
4d3c2250 23890 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
23891 }
23892 else
23893 /* Just complain. */
4d3c2250 23894 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
23895
23896 xfree (name);
23897 {
23898 int i;
23899
23900 for (i = 0; i < argc; i++)
23901 xfree (argv[i]);
23902 }
23903 xfree (argv);
23904 }
23905 else
4d3c2250 23906 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
23907}
23908
cf2c3c16
TT
23909/* Skip some bytes from BYTES according to the form given in FORM.
23910 Returns the new pointer. */
2e276125 23911
d521ce57
TT
23912static const gdb_byte *
23913skip_form_bytes (bfd *abfd, const gdb_byte *bytes, const gdb_byte *buffer_end,
cf2c3c16
TT
23914 enum dwarf_form form,
23915 unsigned int offset_size,
23916 struct dwarf2_section_info *section)
2e276125 23917{
cf2c3c16 23918 unsigned int bytes_read;
2e276125 23919
cf2c3c16 23920 switch (form)
2e276125 23921 {
cf2c3c16
TT
23922 case DW_FORM_data1:
23923 case DW_FORM_flag:
23924 ++bytes;
23925 break;
23926
23927 case DW_FORM_data2:
23928 bytes += 2;
23929 break;
23930
23931 case DW_FORM_data4:
23932 bytes += 4;
23933 break;
23934
23935 case DW_FORM_data8:
23936 bytes += 8;
23937 break;
23938
0224619f
JK
23939 case DW_FORM_data16:
23940 bytes += 16;
23941 break;
23942
cf2c3c16
TT
23943 case DW_FORM_string:
23944 read_direct_string (abfd, bytes, &bytes_read);
23945 bytes += bytes_read;
23946 break;
23947
23948 case DW_FORM_sec_offset:
23949 case DW_FORM_strp:
36586728 23950 case DW_FORM_GNU_strp_alt:
cf2c3c16
TT
23951 bytes += offset_size;
23952 break;
23953
23954 case DW_FORM_block:
23955 bytes += read_unsigned_leb128 (abfd, bytes, &bytes_read);
23956 bytes += bytes_read;
23957 break;
23958
23959 case DW_FORM_block1:
23960 bytes += 1 + read_1_byte (abfd, bytes);
23961 break;
23962 case DW_FORM_block2:
23963 bytes += 2 + read_2_bytes (abfd, bytes);
23964 break;
23965 case DW_FORM_block4:
23966 bytes += 4 + read_4_bytes (abfd, bytes);
23967 break;
23968
23969 case DW_FORM_sdata:
23970 case DW_FORM_udata:
3019eac3
DE
23971 case DW_FORM_GNU_addr_index:
23972 case DW_FORM_GNU_str_index:
d521ce57 23973 bytes = gdb_skip_leb128 (bytes, buffer_end);
f664829e
DE
23974 if (bytes == NULL)
23975 {
23976 dwarf2_section_buffer_overflow_complaint (section);
23977 return NULL;
23978 }
cf2c3c16
TT
23979 break;
23980
663c44ac
JK
23981 case DW_FORM_implicit_const:
23982 break;
23983
cf2c3c16
TT
23984 default:
23985 {
b98664d3 23986 complaint (_("invalid form 0x%x in `%s'"),
a32a8923 23987 form, get_section_name (section));
cf2c3c16
TT
23988 return NULL;
23989 }
2e276125
JB
23990 }
23991
cf2c3c16
TT
23992 return bytes;
23993}
757a13d0 23994
cf2c3c16
TT
23995/* A helper for dwarf_decode_macros that handles skipping an unknown
23996 opcode. Returns an updated pointer to the macro data buffer; or,
23997 on error, issues a complaint and returns NULL. */
757a13d0 23998
d521ce57 23999static const gdb_byte *
cf2c3c16 24000skip_unknown_opcode (unsigned int opcode,
d521ce57
TT
24001 const gdb_byte **opcode_definitions,
24002 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
cf2c3c16
TT
24003 bfd *abfd,
24004 unsigned int offset_size,
24005 struct dwarf2_section_info *section)
24006{
24007 unsigned int bytes_read, i;
24008 unsigned long arg;
d521ce57 24009 const gdb_byte *defn;
2e276125 24010
cf2c3c16 24011 if (opcode_definitions[opcode] == NULL)
2e276125 24012 {
b98664d3 24013 complaint (_("unrecognized DW_MACFINO opcode 0x%x"),
cf2c3c16
TT
24014 opcode);
24015 return NULL;
24016 }
2e276125 24017
cf2c3c16
TT
24018 defn = opcode_definitions[opcode];
24019 arg = read_unsigned_leb128 (abfd, defn, &bytes_read);
24020 defn += bytes_read;
2e276125 24021
cf2c3c16
TT
24022 for (i = 0; i < arg; ++i)
24023 {
aead7601
SM
24024 mac_ptr = skip_form_bytes (abfd, mac_ptr, mac_end,
24025 (enum dwarf_form) defn[i], offset_size,
f664829e 24026 section);
cf2c3c16
TT
24027 if (mac_ptr == NULL)
24028 {
24029 /* skip_form_bytes already issued the complaint. */
24030 return NULL;
24031 }
24032 }
757a13d0 24033
cf2c3c16
TT
24034 return mac_ptr;
24035}
757a13d0 24036
cf2c3c16
TT
24037/* A helper function which parses the header of a macro section.
24038 If the macro section is the extended (for now called "GNU") type,
24039 then this updates *OFFSET_SIZE. Returns a pointer to just after
24040 the header, or issues a complaint and returns NULL on error. */
757a13d0 24041
d521ce57
TT
24042static const gdb_byte *
24043dwarf_parse_macro_header (const gdb_byte **opcode_definitions,
cf2c3c16 24044 bfd *abfd,
d521ce57 24045 const gdb_byte *mac_ptr,
cf2c3c16
TT
24046 unsigned int *offset_size,
24047 int section_is_gnu)
24048{
24049 memset (opcode_definitions, 0, 256 * sizeof (gdb_byte *));
757a13d0 24050
cf2c3c16
TT
24051 if (section_is_gnu)
24052 {
24053 unsigned int version, flags;
757a13d0 24054
cf2c3c16 24055 version = read_2_bytes (abfd, mac_ptr);
0af92d60 24056 if (version != 4 && version != 5)
cf2c3c16 24057 {
b98664d3 24058 complaint (_("unrecognized version `%d' in .debug_macro section"),
cf2c3c16
TT
24059 version);
24060 return NULL;
24061 }
24062 mac_ptr += 2;
757a13d0 24063
cf2c3c16
TT
24064 flags = read_1_byte (abfd, mac_ptr);
24065 ++mac_ptr;
24066 *offset_size = (flags & 1) ? 8 : 4;
757a13d0 24067
cf2c3c16
TT
24068 if ((flags & 2) != 0)
24069 /* We don't need the line table offset. */
24070 mac_ptr += *offset_size;
757a13d0 24071
cf2c3c16
TT
24072 /* Vendor opcode descriptions. */
24073 if ((flags & 4) != 0)
24074 {
24075 unsigned int i, count;
757a13d0 24076
cf2c3c16
TT
24077 count = read_1_byte (abfd, mac_ptr);
24078 ++mac_ptr;
24079 for (i = 0; i < count; ++i)
24080 {
24081 unsigned int opcode, bytes_read;
24082 unsigned long arg;
24083
24084 opcode = read_1_byte (abfd, mac_ptr);
24085 ++mac_ptr;
24086 opcode_definitions[opcode] = mac_ptr;
24087 arg = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24088 mac_ptr += bytes_read;
24089 mac_ptr += arg;
24090 }
757a13d0 24091 }
cf2c3c16 24092 }
757a13d0 24093
cf2c3c16
TT
24094 return mac_ptr;
24095}
757a13d0 24096
cf2c3c16 24097/* A helper for dwarf_decode_macros that handles the GNU extensions,
0af92d60 24098 including DW_MACRO_import. */
cf2c3c16
TT
24099
24100static void
804d2729 24101dwarf_decode_macro_bytes (struct dwarf2_cu *cu,
ed2dc618 24102 bfd *abfd,
d521ce57 24103 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
cf2c3c16 24104 struct macro_source_file *current_file,
43f3e411 24105 struct line_header *lh,
cf2c3c16 24106 struct dwarf2_section_info *section,
36586728 24107 int section_is_gnu, int section_is_dwz,
cf2c3c16 24108 unsigned int offset_size,
8fc3fc34 24109 htab_t include_hash)
cf2c3c16 24110{
804d2729
TT
24111 struct dwarf2_per_objfile *dwarf2_per_objfile
24112 = cu->per_cu->dwarf2_per_objfile;
4d663531 24113 struct objfile *objfile = dwarf2_per_objfile->objfile;
cf2c3c16
TT
24114 enum dwarf_macro_record_type macinfo_type;
24115 int at_commandline;
d521ce57 24116 const gdb_byte *opcode_definitions[256];
757a13d0 24117
cf2c3c16
TT
24118 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
24119 &offset_size, section_is_gnu);
24120 if (mac_ptr == NULL)
24121 {
24122 /* We already issued a complaint. */
24123 return;
24124 }
757a13d0
JK
24125
24126 /* Determines if GDB is still before first DW_MACINFO_start_file. If true
24127 GDB is still reading the definitions from command line. First
24128 DW_MACINFO_start_file will need to be ignored as it was already executed
24129 to create CURRENT_FILE for the main source holding also the command line
24130 definitions. On first met DW_MACINFO_start_file this flag is reset to
24131 normally execute all the remaining DW_MACINFO_start_file macinfos. */
24132
24133 at_commandline = 1;
24134
24135 do
24136 {
24137 /* Do we at least have room for a macinfo type byte? */
24138 if (mac_ptr >= mac_end)
24139 {
f664829e 24140 dwarf2_section_buffer_overflow_complaint (section);
757a13d0
JK
24141 break;
24142 }
24143
aead7601 24144 macinfo_type = (enum dwarf_macro_record_type) read_1_byte (abfd, mac_ptr);
757a13d0
JK
24145 mac_ptr++;
24146
cf2c3c16
TT
24147 /* Note that we rely on the fact that the corresponding GNU and
24148 DWARF constants are the same. */
132448f8
SM
24149 DIAGNOSTIC_PUSH
24150 DIAGNOSTIC_IGNORE_SWITCH_DIFFERENT_ENUM_TYPES
757a13d0
JK
24151 switch (macinfo_type)
24152 {
24153 /* A zero macinfo type indicates the end of the macro
24154 information. */
24155 case 0:
24156 break;
2e276125 24157
0af92d60
JK
24158 case DW_MACRO_define:
24159 case DW_MACRO_undef:
24160 case DW_MACRO_define_strp:
24161 case DW_MACRO_undef_strp:
24162 case DW_MACRO_define_sup:
24163 case DW_MACRO_undef_sup:
2e276125 24164 {
891d2f0b 24165 unsigned int bytes_read;
2e276125 24166 int line;
d521ce57 24167 const char *body;
cf2c3c16 24168 int is_define;
2e276125 24169
cf2c3c16
TT
24170 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24171 mac_ptr += bytes_read;
24172
0af92d60
JK
24173 if (macinfo_type == DW_MACRO_define
24174 || macinfo_type == DW_MACRO_undef)
cf2c3c16
TT
24175 {
24176 body = read_direct_string (abfd, mac_ptr, &bytes_read);
24177 mac_ptr += bytes_read;
24178 }
24179 else
24180 {
24181 LONGEST str_offset;
24182
24183 str_offset = read_offset_1 (abfd, mac_ptr, offset_size);
24184 mac_ptr += offset_size;
2e276125 24185
0af92d60
JK
24186 if (macinfo_type == DW_MACRO_define_sup
24187 || macinfo_type == DW_MACRO_undef_sup
f7a35f02 24188 || section_is_dwz)
36586728 24189 {
ed2dc618
SM
24190 struct dwz_file *dwz
24191 = dwarf2_get_dwz_file (dwarf2_per_objfile);
36586728 24192
ed2dc618
SM
24193 body = read_indirect_string_from_dwz (objfile,
24194 dwz, str_offset);
36586728
TT
24195 }
24196 else
ed2dc618
SM
24197 body = read_indirect_string_at_offset (dwarf2_per_objfile,
24198 abfd, str_offset);
cf2c3c16
TT
24199 }
24200
0af92d60
JK
24201 is_define = (macinfo_type == DW_MACRO_define
24202 || macinfo_type == DW_MACRO_define_strp
24203 || macinfo_type == DW_MACRO_define_sup);
2e276125 24204 if (! current_file)
757a13d0
JK
24205 {
24206 /* DWARF violation as no main source is present. */
b98664d3 24207 complaint (_("debug info with no main source gives macro %s "
757a13d0 24208 "on line %d: %s"),
cf2c3c16
TT
24209 is_define ? _("definition") : _("undefinition"),
24210 line, body);
757a13d0
JK
24211 break;
24212 }
3e43a32a
MS
24213 if ((line == 0 && !at_commandline)
24214 || (line != 0 && at_commandline))
b98664d3 24215 complaint (_("debug info gives %s macro %s with %s line %d: %s"),
757a13d0 24216 at_commandline ? _("command-line") : _("in-file"),
cf2c3c16 24217 is_define ? _("definition") : _("undefinition"),
757a13d0
JK
24218 line == 0 ? _("zero") : _("non-zero"), line, body);
24219
cf2c3c16 24220 if (is_define)
757a13d0 24221 parse_macro_definition (current_file, line, body);
cf2c3c16
TT
24222 else
24223 {
0af92d60
JK
24224 gdb_assert (macinfo_type == DW_MACRO_undef
24225 || macinfo_type == DW_MACRO_undef_strp
24226 || macinfo_type == DW_MACRO_undef_sup);
cf2c3c16
TT
24227 macro_undef (current_file, line, body);
24228 }
2e276125
JB
24229 }
24230 break;
24231
0af92d60 24232 case DW_MACRO_start_file:
2e276125 24233 {
891d2f0b 24234 unsigned int bytes_read;
2e276125
JB
24235 int line, file;
24236
24237 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24238 mac_ptr += bytes_read;
24239 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24240 mac_ptr += bytes_read;
24241
3e43a32a
MS
24242 if ((line == 0 && !at_commandline)
24243 || (line != 0 && at_commandline))
b98664d3 24244 complaint (_("debug info gives source %d included "
757a13d0
JK
24245 "from %s at %s line %d"),
24246 file, at_commandline ? _("command-line") : _("file"),
24247 line == 0 ? _("zero") : _("non-zero"), line);
24248
24249 if (at_commandline)
24250 {
0af92d60 24251 /* This DW_MACRO_start_file was executed in the
cf2c3c16 24252 pass one. */
757a13d0
JK
24253 at_commandline = 0;
24254 }
24255 else
804d2729
TT
24256 current_file = macro_start_file (cu, file, line, current_file,
24257 lh);
2e276125
JB
24258 }
24259 break;
24260
0af92d60 24261 case DW_MACRO_end_file:
2e276125 24262 if (! current_file)
b98664d3 24263 complaint (_("macro debug info has an unmatched "
3e43a32a 24264 "`close_file' directive"));
2e276125
JB
24265 else
24266 {
24267 current_file = current_file->included_by;
24268 if (! current_file)
24269 {
cf2c3c16 24270 enum dwarf_macro_record_type next_type;
2e276125
JB
24271
24272 /* GCC circa March 2002 doesn't produce the zero
24273 type byte marking the end of the compilation
24274 unit. Complain if it's not there, but exit no
24275 matter what. */
24276
24277 /* Do we at least have room for a macinfo type byte? */
24278 if (mac_ptr >= mac_end)
24279 {
f664829e 24280 dwarf2_section_buffer_overflow_complaint (section);
2e276125
JB
24281 return;
24282 }
24283
24284 /* We don't increment mac_ptr here, so this is just
24285 a look-ahead. */
aead7601
SM
24286 next_type
24287 = (enum dwarf_macro_record_type) read_1_byte (abfd,
24288 mac_ptr);
2e276125 24289 if (next_type != 0)
b98664d3 24290 complaint (_("no terminating 0-type entry for "
3e43a32a 24291 "macros in `.debug_macinfo' section"));
2e276125
JB
24292
24293 return;
24294 }
24295 }
24296 break;
24297
0af92d60
JK
24298 case DW_MACRO_import:
24299 case DW_MACRO_import_sup:
cf2c3c16
TT
24300 {
24301 LONGEST offset;
8fc3fc34 24302 void **slot;
a036ba48
TT
24303 bfd *include_bfd = abfd;
24304 struct dwarf2_section_info *include_section = section;
d521ce57 24305 const gdb_byte *include_mac_end = mac_end;
a036ba48 24306 int is_dwz = section_is_dwz;
d521ce57 24307 const gdb_byte *new_mac_ptr;
cf2c3c16
TT
24308
24309 offset = read_offset_1 (abfd, mac_ptr, offset_size);
24310 mac_ptr += offset_size;
24311
0af92d60 24312 if (macinfo_type == DW_MACRO_import_sup)
a036ba48 24313 {
ed2dc618 24314 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
a036ba48 24315
4d663531 24316 dwarf2_read_section (objfile, &dwz->macro);
a036ba48 24317
a036ba48 24318 include_section = &dwz->macro;
a32a8923 24319 include_bfd = get_section_bfd_owner (include_section);
a036ba48
TT
24320 include_mac_end = dwz->macro.buffer + dwz->macro.size;
24321 is_dwz = 1;
24322 }
24323
24324 new_mac_ptr = include_section->buffer + offset;
24325 slot = htab_find_slot (include_hash, new_mac_ptr, INSERT);
24326
8fc3fc34
TT
24327 if (*slot != NULL)
24328 {
24329 /* This has actually happened; see
24330 http://sourceware.org/bugzilla/show_bug.cgi?id=13568. */
b98664d3 24331 complaint (_("recursive DW_MACRO_import in "
8fc3fc34
TT
24332 ".debug_macro section"));
24333 }
24334 else
24335 {
d521ce57 24336 *slot = (void *) new_mac_ptr;
36586728 24337
804d2729 24338 dwarf_decode_macro_bytes (cu, include_bfd, new_mac_ptr,
43f3e411 24339 include_mac_end, current_file, lh,
36586728 24340 section, section_is_gnu, is_dwz,
4d663531 24341 offset_size, include_hash);
8fc3fc34 24342
d521ce57 24343 htab_remove_elt (include_hash, (void *) new_mac_ptr);
8fc3fc34 24344 }
cf2c3c16
TT
24345 }
24346 break;
24347
2e276125 24348 case DW_MACINFO_vendor_ext:
cf2c3c16
TT
24349 if (!section_is_gnu)
24350 {
24351 unsigned int bytes_read;
2e276125 24352
ac298888
TT
24353 /* This reads the constant, but since we don't recognize
24354 any vendor extensions, we ignore it. */
24355 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
cf2c3c16
TT
24356 mac_ptr += bytes_read;
24357 read_direct_string (abfd, mac_ptr, &bytes_read);
24358 mac_ptr += bytes_read;
2e276125 24359
cf2c3c16
TT
24360 /* We don't recognize any vendor extensions. */
24361 break;
24362 }
24363 /* FALLTHROUGH */
24364
24365 default:
24366 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
f664829e 24367 mac_ptr, mac_end, abfd, offset_size,
cf2c3c16
TT
24368 section);
24369 if (mac_ptr == NULL)
24370 return;
24371 break;
2e276125 24372 }
132448f8 24373 DIAGNOSTIC_POP
757a13d0 24374 } while (macinfo_type != 0);
2e276125 24375}
8e19ed76 24376
cf2c3c16 24377static void
09262596 24378dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
43f3e411 24379 int section_is_gnu)
cf2c3c16 24380{
518817b3
SM
24381 struct dwarf2_per_objfile *dwarf2_per_objfile
24382 = cu->per_cu->dwarf2_per_objfile;
bb5ed363 24383 struct objfile *objfile = dwarf2_per_objfile->objfile;
09262596
DE
24384 struct line_header *lh = cu->line_header;
24385 bfd *abfd;
d521ce57 24386 const gdb_byte *mac_ptr, *mac_end;
cf2c3c16
TT
24387 struct macro_source_file *current_file = 0;
24388 enum dwarf_macro_record_type macinfo_type;
24389 unsigned int offset_size = cu->header.offset_size;
d521ce57 24390 const gdb_byte *opcode_definitions[256];
8fc3fc34 24391 void **slot;
09262596
DE
24392 struct dwarf2_section_info *section;
24393 const char *section_name;
24394
24395 if (cu->dwo_unit != NULL)
24396 {
24397 if (section_is_gnu)
24398 {
24399 section = &cu->dwo_unit->dwo_file->sections.macro;
24400 section_name = ".debug_macro.dwo";
24401 }
24402 else
24403 {
24404 section = &cu->dwo_unit->dwo_file->sections.macinfo;
24405 section_name = ".debug_macinfo.dwo";
24406 }
24407 }
24408 else
24409 {
24410 if (section_is_gnu)
24411 {
24412 section = &dwarf2_per_objfile->macro;
24413 section_name = ".debug_macro";
24414 }
24415 else
24416 {
24417 section = &dwarf2_per_objfile->macinfo;
24418 section_name = ".debug_macinfo";
24419 }
24420 }
cf2c3c16 24421
bb5ed363 24422 dwarf2_read_section (objfile, section);
cf2c3c16
TT
24423 if (section->buffer == NULL)
24424 {
b98664d3 24425 complaint (_("missing %s section"), section_name);
cf2c3c16
TT
24426 return;
24427 }
a32a8923 24428 abfd = get_section_bfd_owner (section);
cf2c3c16
TT
24429
24430 /* First pass: Find the name of the base filename.
24431 This filename is needed in order to process all macros whose definition
24432 (or undefinition) comes from the command line. These macros are defined
24433 before the first DW_MACINFO_start_file entry, and yet still need to be
24434 associated to the base file.
24435
24436 To determine the base file name, we scan the macro definitions until we
24437 reach the first DW_MACINFO_start_file entry. We then initialize
24438 CURRENT_FILE accordingly so that any macro definition found before the
24439 first DW_MACINFO_start_file can still be associated to the base file. */
24440
24441 mac_ptr = section->buffer + offset;
24442 mac_end = section->buffer + section->size;
24443
24444 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
24445 &offset_size, section_is_gnu);
24446 if (mac_ptr == NULL)
24447 {
24448 /* We already issued a complaint. */
24449 return;
24450 }
24451
24452 do
24453 {
24454 /* Do we at least have room for a macinfo type byte? */
24455 if (mac_ptr >= mac_end)
24456 {
24457 /* Complaint is printed during the second pass as GDB will probably
24458 stop the first pass earlier upon finding
24459 DW_MACINFO_start_file. */
24460 break;
24461 }
24462
aead7601 24463 macinfo_type = (enum dwarf_macro_record_type) read_1_byte (abfd, mac_ptr);
cf2c3c16
TT
24464 mac_ptr++;
24465
24466 /* Note that we rely on the fact that the corresponding GNU and
24467 DWARF constants are the same. */
132448f8
SM
24468 DIAGNOSTIC_PUSH
24469 DIAGNOSTIC_IGNORE_SWITCH_DIFFERENT_ENUM_TYPES
cf2c3c16
TT
24470 switch (macinfo_type)
24471 {
24472 /* A zero macinfo type indicates the end of the macro
24473 information. */
24474 case 0:
24475 break;
24476
0af92d60
JK
24477 case DW_MACRO_define:
24478 case DW_MACRO_undef:
cf2c3c16
TT
24479 /* Only skip the data by MAC_PTR. */
24480 {
24481 unsigned int bytes_read;
24482
24483 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24484 mac_ptr += bytes_read;
24485 read_direct_string (abfd, mac_ptr, &bytes_read);
24486 mac_ptr += bytes_read;
24487 }
24488 break;
24489
0af92d60 24490 case DW_MACRO_start_file:
cf2c3c16
TT
24491 {
24492 unsigned int bytes_read;
24493 int line, file;
24494
24495 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24496 mac_ptr += bytes_read;
24497 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24498 mac_ptr += bytes_read;
24499
804d2729 24500 current_file = macro_start_file (cu, file, line, current_file, lh);
cf2c3c16
TT
24501 }
24502 break;
24503
0af92d60 24504 case DW_MACRO_end_file:
cf2c3c16
TT
24505 /* No data to skip by MAC_PTR. */
24506 break;
24507
0af92d60
JK
24508 case DW_MACRO_define_strp:
24509 case DW_MACRO_undef_strp:
24510 case DW_MACRO_define_sup:
24511 case DW_MACRO_undef_sup:
cf2c3c16
TT
24512 {
24513 unsigned int bytes_read;
24514
24515 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24516 mac_ptr += bytes_read;
24517 mac_ptr += offset_size;
24518 }
24519 break;
24520
0af92d60
JK
24521 case DW_MACRO_import:
24522 case DW_MACRO_import_sup:
cf2c3c16 24523 /* Note that, according to the spec, a transparent include
0af92d60 24524 chain cannot call DW_MACRO_start_file. So, we can just
cf2c3c16
TT
24525 skip this opcode. */
24526 mac_ptr += offset_size;
24527 break;
24528
24529 case DW_MACINFO_vendor_ext:
24530 /* Only skip the data by MAC_PTR. */
24531 if (!section_is_gnu)
24532 {
24533 unsigned int bytes_read;
24534
24535 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24536 mac_ptr += bytes_read;
24537 read_direct_string (abfd, mac_ptr, &bytes_read);
24538 mac_ptr += bytes_read;
24539 }
24540 /* FALLTHROUGH */
24541
24542 default:
24543 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
f664829e 24544 mac_ptr, mac_end, abfd, offset_size,
cf2c3c16
TT
24545 section);
24546 if (mac_ptr == NULL)
24547 return;
24548 break;
24549 }
132448f8 24550 DIAGNOSTIC_POP
cf2c3c16
TT
24551 } while (macinfo_type != 0 && current_file == NULL);
24552
24553 /* Second pass: Process all entries.
24554
24555 Use the AT_COMMAND_LINE flag to determine whether we are still processing
24556 command-line macro definitions/undefinitions. This flag is unset when we
24557 reach the first DW_MACINFO_start_file entry. */
24558
fc4007c9
TT
24559 htab_up include_hash (htab_create_alloc (1, htab_hash_pointer,
24560 htab_eq_pointer,
24561 NULL, xcalloc, xfree));
8fc3fc34 24562 mac_ptr = section->buffer + offset;
fc4007c9 24563 slot = htab_find_slot (include_hash.get (), mac_ptr, INSERT);
d521ce57 24564 *slot = (void *) mac_ptr;
804d2729 24565 dwarf_decode_macro_bytes (cu, abfd, mac_ptr, mac_end,
43f3e411 24566 current_file, lh, section,
fc4007c9
TT
24567 section_is_gnu, 0, offset_size,
24568 include_hash.get ());
cf2c3c16
TT
24569}
24570
8e19ed76 24571/* Check if the attribute's form is a DW_FORM_block*
0963b4bd 24572 if so return true else false. */
380bca97 24573
8e19ed76 24574static int
6e5a29e1 24575attr_form_is_block (const struct attribute *attr)
8e19ed76
PS
24576{
24577 return (attr == NULL ? 0 :
24578 attr->form == DW_FORM_block1
24579 || attr->form == DW_FORM_block2
24580 || attr->form == DW_FORM_block4
2dc7f7b3
TT
24581 || attr->form == DW_FORM_block
24582 || attr->form == DW_FORM_exprloc);
8e19ed76 24583}
4c2df51b 24584
c6a0999f
JB
24585/* Return non-zero if ATTR's value is a section offset --- classes
24586 lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
24587 You may use DW_UNSND (attr) to retrieve such offsets.
24588
24589 Section 7.5.4, "Attribute Encodings", explains that no attribute
24590 may have a value that belongs to more than one of these classes; it
24591 would be ambiguous if we did, because we use the same forms for all
24592 of them. */
380bca97 24593
3690dd37 24594static int
6e5a29e1 24595attr_form_is_section_offset (const struct attribute *attr)
3690dd37
JB
24596{
24597 return (attr->form == DW_FORM_data4
2dc7f7b3
TT
24598 || attr->form == DW_FORM_data8
24599 || attr->form == DW_FORM_sec_offset);
3690dd37
JB
24600}
24601
3690dd37
JB
24602/* Return non-zero if ATTR's value falls in the 'constant' class, or
24603 zero otherwise. When this function returns true, you can apply
24604 dwarf2_get_attr_constant_value to it.
24605
24606 However, note that for some attributes you must check
24607 attr_form_is_section_offset before using this test. DW_FORM_data4
24608 and DW_FORM_data8 are members of both the constant class, and of
24609 the classes that contain offsets into other debug sections
24610 (lineptr, loclistptr, macptr or rangelistptr). The DWARF spec says
24611 that, if an attribute's can be either a constant or one of the
24612 section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
0224619f
JK
24613 taken as section offsets, not constants.
24614
24615 DW_FORM_data16 is not considered as dwarf2_get_attr_constant_value
24616 cannot handle that. */
380bca97 24617
3690dd37 24618static int
6e5a29e1 24619attr_form_is_constant (const struct attribute *attr)
3690dd37
JB
24620{
24621 switch (attr->form)
24622 {
24623 case DW_FORM_sdata:
24624 case DW_FORM_udata:
24625 case DW_FORM_data1:
24626 case DW_FORM_data2:
24627 case DW_FORM_data4:
24628 case DW_FORM_data8:
663c44ac 24629 case DW_FORM_implicit_const:
3690dd37
JB
24630 return 1;
24631 default:
24632 return 0;
24633 }
24634}
24635
7771576e
SA
24636
24637/* DW_ADDR is always stored already as sect_offset; despite for the forms
24638 besides DW_FORM_ref_addr it is stored as cu_offset in the DWARF file. */
24639
24640static int
6e5a29e1 24641attr_form_is_ref (const struct attribute *attr)
7771576e
SA
24642{
24643 switch (attr->form)
24644 {
24645 case DW_FORM_ref_addr:
24646 case DW_FORM_ref1:
24647 case DW_FORM_ref2:
24648 case DW_FORM_ref4:
24649 case DW_FORM_ref8:
24650 case DW_FORM_ref_udata:
24651 case DW_FORM_GNU_ref_alt:
24652 return 1;
24653 default:
24654 return 0;
24655 }
24656}
24657
3019eac3
DE
24658/* Return the .debug_loc section to use for CU.
24659 For DWO files use .debug_loc.dwo. */
24660
24661static struct dwarf2_section_info *
24662cu_debug_loc_section (struct dwarf2_cu *cu)
24663{
518817b3
SM
24664 struct dwarf2_per_objfile *dwarf2_per_objfile
24665 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 24666
3019eac3 24667 if (cu->dwo_unit)
43988095
JK
24668 {
24669 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
24670
24671 return cu->header.version >= 5 ? &sections->loclists : &sections->loc;
24672 }
24673 return (cu->header.version >= 5 ? &dwarf2_per_objfile->loclists
24674 : &dwarf2_per_objfile->loc);
3019eac3
DE
24675}
24676
8cf6f0b1
TT
24677/* A helper function that fills in a dwarf2_loclist_baton. */
24678
24679static void
24680fill_in_loclist_baton (struct dwarf2_cu *cu,
24681 struct dwarf2_loclist_baton *baton,
ff39bb5e 24682 const struct attribute *attr)
8cf6f0b1 24683{
518817b3
SM
24684 struct dwarf2_per_objfile *dwarf2_per_objfile
24685 = cu->per_cu->dwarf2_per_objfile;
3019eac3
DE
24686 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
24687
24688 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
8cf6f0b1
TT
24689
24690 baton->per_cu = cu->per_cu;
24691 gdb_assert (baton->per_cu);
24692 /* We don't know how long the location list is, but make sure we
24693 don't run off the edge of the section. */
3019eac3
DE
24694 baton->size = section->size - DW_UNSND (attr);
24695 baton->data = section->buffer + DW_UNSND (attr);
8cf6f0b1 24696 baton->base_address = cu->base_address;
f664829e 24697 baton->from_dwo = cu->dwo_unit != NULL;
8cf6f0b1
TT
24698}
24699
4c2df51b 24700static void
ff39bb5e 24701dwarf2_symbol_mark_computed (const struct attribute *attr, struct symbol *sym,
f1e6e072 24702 struct dwarf2_cu *cu, int is_block)
4c2df51b 24703{
518817b3
SM
24704 struct dwarf2_per_objfile *dwarf2_per_objfile
24705 = cu->per_cu->dwarf2_per_objfile;
bb5ed363 24706 struct objfile *objfile = dwarf2_per_objfile->objfile;
3019eac3 24707 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
bb5ed363 24708
3690dd37 24709 if (attr_form_is_section_offset (attr)
3019eac3 24710 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
99bcc461
DJ
24711 the section. If so, fall through to the complaint in the
24712 other branch. */
3019eac3 24713 && DW_UNSND (attr) < dwarf2_section_size (objfile, section))
4c2df51b 24714 {
0d53c4c4 24715 struct dwarf2_loclist_baton *baton;
4c2df51b 24716
8d749320 24717 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_loclist_baton);
4c2df51b 24718
8cf6f0b1 24719 fill_in_loclist_baton (cu, baton, attr);
be391dca 24720
d00adf39 24721 if (cu->base_known == 0)
b98664d3 24722 complaint (_("Location list used without "
3e43a32a 24723 "specifying the CU base address."));
4c2df51b 24724
f1e6e072
TT
24725 SYMBOL_ACLASS_INDEX (sym) = (is_block
24726 ? dwarf2_loclist_block_index
24727 : dwarf2_loclist_index);
0d53c4c4
DJ
24728 SYMBOL_LOCATION_BATON (sym) = baton;
24729 }
24730 else
24731 {
24732 struct dwarf2_locexpr_baton *baton;
24733
8d749320 24734 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
ae0d2f24
UW
24735 baton->per_cu = cu->per_cu;
24736 gdb_assert (baton->per_cu);
0d53c4c4
DJ
24737
24738 if (attr_form_is_block (attr))
24739 {
24740 /* Note that we're just copying the block's data pointer
24741 here, not the actual data. We're still pointing into the
6502dd73
DJ
24742 info_buffer for SYM's objfile; right now we never release
24743 that buffer, but when we do clean up properly this may
24744 need to change. */
0d53c4c4
DJ
24745 baton->size = DW_BLOCK (attr)->size;
24746 baton->data = DW_BLOCK (attr)->data;
24747 }
24748 else
24749 {
24750 dwarf2_invalid_attrib_class_complaint ("location description",
24751 SYMBOL_NATURAL_NAME (sym));
24752 baton->size = 0;
0d53c4c4 24753 }
6e70227d 24754
f1e6e072
TT
24755 SYMBOL_ACLASS_INDEX (sym) = (is_block
24756 ? dwarf2_locexpr_block_index
24757 : dwarf2_locexpr_index);
0d53c4c4
DJ
24758 SYMBOL_LOCATION_BATON (sym) = baton;
24759 }
4c2df51b 24760}
6502dd73 24761
9aa1f1e3
TT
24762/* Return the OBJFILE associated with the compilation unit CU. If CU
24763 came from a separate debuginfo file, then the master objfile is
24764 returned. */
ae0d2f24
UW
24765
24766struct objfile *
24767dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
24768{
e3b94546 24769 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
ae0d2f24
UW
24770
24771 /* Return the master objfile, so that we can report and look up the
24772 correct file containing this variable. */
24773 if (objfile->separate_debug_objfile_backlink)
24774 objfile = objfile->separate_debug_objfile_backlink;
24775
24776 return objfile;
24777}
24778
96408a79
SA
24779/* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
24780 (CU_HEADERP is unused in such case) or prepare a temporary copy at
24781 CU_HEADERP first. */
24782
24783static const struct comp_unit_head *
24784per_cu_header_read_in (struct comp_unit_head *cu_headerp,
24785 struct dwarf2_per_cu_data *per_cu)
24786{
d521ce57 24787 const gdb_byte *info_ptr;
96408a79
SA
24788
24789 if (per_cu->cu)
24790 return &per_cu->cu->header;
24791
9c541725 24792 info_ptr = per_cu->section->buffer + to_underlying (per_cu->sect_off);
96408a79
SA
24793
24794 memset (cu_headerp, 0, sizeof (*cu_headerp));
43988095
JK
24795 read_comp_unit_head (cu_headerp, info_ptr, per_cu->section,
24796 rcuh_kind::COMPILE);
96408a79
SA
24797
24798 return cu_headerp;
24799}
24800
ae0d2f24
UW
24801/* Return the address size given in the compilation unit header for CU. */
24802
98714339 24803int
ae0d2f24
UW
24804dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
24805{
96408a79
SA
24806 struct comp_unit_head cu_header_local;
24807 const struct comp_unit_head *cu_headerp;
c471e790 24808
96408a79
SA
24809 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
24810
24811 return cu_headerp->addr_size;
ae0d2f24
UW
24812}
24813
9eae7c52
TT
24814/* Return the offset size given in the compilation unit header for CU. */
24815
24816int
24817dwarf2_per_cu_offset_size (struct dwarf2_per_cu_data *per_cu)
24818{
96408a79
SA
24819 struct comp_unit_head cu_header_local;
24820 const struct comp_unit_head *cu_headerp;
9c6c53f7 24821
96408a79
SA
24822 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
24823
24824 return cu_headerp->offset_size;
24825}
24826
24827/* See its dwarf2loc.h declaration. */
24828
24829int
24830dwarf2_per_cu_ref_addr_size (struct dwarf2_per_cu_data *per_cu)
24831{
24832 struct comp_unit_head cu_header_local;
24833 const struct comp_unit_head *cu_headerp;
24834
24835 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
24836
24837 if (cu_headerp->version == 2)
24838 return cu_headerp->addr_size;
24839 else
24840 return cu_headerp->offset_size;
181cebd4
JK
24841}
24842
9aa1f1e3
TT
24843/* Return the text offset of the CU. The returned offset comes from
24844 this CU's objfile. If this objfile came from a separate debuginfo
24845 file, then the offset may be different from the corresponding
24846 offset in the parent objfile. */
24847
24848CORE_ADDR
24849dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
24850{
e3b94546 24851 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
9aa1f1e3
TT
24852
24853 return ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
24854}
24855
43988095
JK
24856/* Return DWARF version number of PER_CU. */
24857
24858short
24859dwarf2_version (struct dwarf2_per_cu_data *per_cu)
24860{
24861 return per_cu->dwarf_version;
24862}
24863
348e048f
DE
24864/* Locate the .debug_info compilation unit from CU's objfile which contains
24865 the DIE at OFFSET. Raises an error on failure. */
ae038cb0
DJ
24866
24867static struct dwarf2_per_cu_data *
9c541725 24868dwarf2_find_containing_comp_unit (sect_offset sect_off,
36586728 24869 unsigned int offset_in_dwz,
ed2dc618 24870 struct dwarf2_per_objfile *dwarf2_per_objfile)
ae038cb0
DJ
24871{
24872 struct dwarf2_per_cu_data *this_cu;
24873 int low, high;
36586728 24874 const sect_offset *cu_off;
ae038cb0 24875
ae038cb0 24876 low = 0;
b76e467d 24877 high = dwarf2_per_objfile->all_comp_units.size () - 1;
ae038cb0
DJ
24878 while (high > low)
24879 {
36586728 24880 struct dwarf2_per_cu_data *mid_cu;
ae038cb0 24881 int mid = low + (high - low) / 2;
9a619af0 24882
36586728 24883 mid_cu = dwarf2_per_objfile->all_comp_units[mid];
9c541725 24884 cu_off = &mid_cu->sect_off;
36586728 24885 if (mid_cu->is_dwz > offset_in_dwz
9c541725 24886 || (mid_cu->is_dwz == offset_in_dwz && *cu_off >= sect_off))
ae038cb0
DJ
24887 high = mid;
24888 else
24889 low = mid + 1;
24890 }
24891 gdb_assert (low == high);
36586728 24892 this_cu = dwarf2_per_objfile->all_comp_units[low];
9c541725
PA
24893 cu_off = &this_cu->sect_off;
24894 if (this_cu->is_dwz != offset_in_dwz || *cu_off > sect_off)
ae038cb0 24895 {
36586728 24896 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
8a3fe4f8 24897 error (_("Dwarf Error: could not find partial DIE containing "
9d8780f0
SM
24898 "offset %s [in module %s]"),
24899 sect_offset_str (sect_off),
ed2dc618 24900 bfd_get_filename (dwarf2_per_objfile->objfile->obfd));
10b3939b 24901
9c541725
PA
24902 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->sect_off
24903 <= sect_off);
ae038cb0
DJ
24904 return dwarf2_per_objfile->all_comp_units[low-1];
24905 }
24906 else
24907 {
24908 this_cu = dwarf2_per_objfile->all_comp_units[low];
b76e467d 24909 if (low == dwarf2_per_objfile->all_comp_units.size () - 1
9c541725 24910 && sect_off >= this_cu->sect_off + this_cu->length)
9d8780f0 24911 error (_("invalid dwarf2 offset %s"), sect_offset_str (sect_off));
9c541725 24912 gdb_assert (sect_off < this_cu->sect_off + this_cu->length);
ae038cb0
DJ
24913 return this_cu;
24914 }
24915}
24916
23745b47 24917/* Initialize dwarf2_cu CU, owned by PER_CU. */
93311388 24918
fcd3b13d
SM
24919dwarf2_cu::dwarf2_cu (struct dwarf2_per_cu_data *per_cu_)
24920 : per_cu (per_cu_),
24921 mark (0),
24922 has_loclist (0),
24923 checked_producer (0),
24924 producer_is_gxx_lt_4_6 (0),
24925 producer_is_gcc_lt_4_3 (0),
24926 producer_is_icc_lt_14 (0),
24927 processing_has_namespace_info (0)
93311388 24928{
fcd3b13d
SM
24929 per_cu->cu = this;
24930}
24931
24932/* Destroy a dwarf2_cu. */
24933
24934dwarf2_cu::~dwarf2_cu ()
24935{
24936 per_cu->cu = NULL;
9816fde3
JK
24937}
24938
24939/* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
24940
24941static void
95554aad
TT
24942prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
24943 enum language pretend_language)
9816fde3
JK
24944{
24945 struct attribute *attr;
24946
24947 /* Set the language we're debugging. */
24948 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
24949 if (attr)
24950 set_cu_language (DW_UNSND (attr), cu);
24951 else
9cded63f 24952 {
95554aad 24953 cu->language = pretend_language;
9cded63f
TT
24954 cu->language_defn = language_def (cu->language);
24955 }
dee91e82 24956
7d45c7c3 24957 cu->producer = dwarf2_string_attr (comp_unit_die, DW_AT_producer, cu);
93311388
DE
24958}
24959
ae038cb0
DJ
24960/* Increase the age counter on each cached compilation unit, and free
24961 any that are too old. */
24962
24963static void
ed2dc618 24964age_cached_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
ae038cb0
DJ
24965{
24966 struct dwarf2_per_cu_data *per_cu, **last_chain;
24967
24968 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
24969 per_cu = dwarf2_per_objfile->read_in_chain;
24970 while (per_cu != NULL)
24971 {
24972 per_cu->cu->last_used ++;
b4f54984 24973 if (per_cu->cu->last_used <= dwarf_max_cache_age)
ae038cb0
DJ
24974 dwarf2_mark (per_cu->cu);
24975 per_cu = per_cu->cu->read_in_chain;
24976 }
24977
24978 per_cu = dwarf2_per_objfile->read_in_chain;
24979 last_chain = &dwarf2_per_objfile->read_in_chain;
24980 while (per_cu != NULL)
24981 {
24982 struct dwarf2_per_cu_data *next_cu;
24983
24984 next_cu = per_cu->cu->read_in_chain;
24985
24986 if (!per_cu->cu->mark)
24987 {
fcd3b13d 24988 delete per_cu->cu;
ae038cb0
DJ
24989 *last_chain = next_cu;
24990 }
24991 else
24992 last_chain = &per_cu->cu->read_in_chain;
24993
24994 per_cu = next_cu;
24995 }
24996}
24997
24998/* Remove a single compilation unit from the cache. */
24999
25000static void
dee91e82 25001free_one_cached_comp_unit (struct dwarf2_per_cu_data *target_per_cu)
ae038cb0
DJ
25002{
25003 struct dwarf2_per_cu_data *per_cu, **last_chain;
ed2dc618
SM
25004 struct dwarf2_per_objfile *dwarf2_per_objfile
25005 = target_per_cu->dwarf2_per_objfile;
ae038cb0
DJ
25006
25007 per_cu = dwarf2_per_objfile->read_in_chain;
25008 last_chain = &dwarf2_per_objfile->read_in_chain;
25009 while (per_cu != NULL)
25010 {
25011 struct dwarf2_per_cu_data *next_cu;
25012
25013 next_cu = per_cu->cu->read_in_chain;
25014
dee91e82 25015 if (per_cu == target_per_cu)
ae038cb0 25016 {
fcd3b13d 25017 delete per_cu->cu;
dee91e82 25018 per_cu->cu = NULL;
ae038cb0
DJ
25019 *last_chain = next_cu;
25020 break;
25021 }
25022 else
25023 last_chain = &per_cu->cu->read_in_chain;
25024
25025 per_cu = next_cu;
25026 }
25027}
25028
d95d3aef 25029/* Cleanup function for the dwarf2_per_objfile data. */
fe3e1990 25030
d95d3aef
TT
25031static void
25032dwarf2_free_objfile (struct objfile *objfile, void *datum)
fe3e1990 25033{
ed2dc618 25034 struct dwarf2_per_objfile *dwarf2_per_objfile
d95d3aef 25035 = static_cast<struct dwarf2_per_objfile *> (datum);
fe3e1990 25036
fd90ace4 25037 delete dwarf2_per_objfile;
fe3e1990
DJ
25038}
25039
dee91e82
DE
25040/* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
25041 We store these in a hash table separate from the DIEs, and preserve them
25042 when the DIEs are flushed out of cache.
25043
25044 The CU "per_cu" pointer is needed because offset alone is not enough to
3019eac3 25045 uniquely identify the type. A file may have multiple .debug_types sections,
c88ee1f0
DE
25046 or the type may come from a DWO file. Furthermore, while it's more logical
25047 to use per_cu->section+offset, with Fission the section with the data is in
25048 the DWO file but we don't know that section at the point we need it.
25049 We have to use something in dwarf2_per_cu_data (or the pointer to it)
25050 because we can enter the lookup routine, get_die_type_at_offset, from
25051 outside this file, and thus won't necessarily have PER_CU->cu.
25052 Fortunately, PER_CU is stable for the life of the objfile. */
1c379e20 25053
dee91e82 25054struct dwarf2_per_cu_offset_and_type
1c379e20 25055{
dee91e82 25056 const struct dwarf2_per_cu_data *per_cu;
9c541725 25057 sect_offset sect_off;
1c379e20
DJ
25058 struct type *type;
25059};
25060
dee91e82 25061/* Hash function for a dwarf2_per_cu_offset_and_type. */
1c379e20
DJ
25062
25063static hashval_t
dee91e82 25064per_cu_offset_and_type_hash (const void *item)
1c379e20 25065{
9a3c8263
SM
25066 const struct dwarf2_per_cu_offset_and_type *ofs
25067 = (const struct dwarf2_per_cu_offset_and_type *) item;
9a619af0 25068
9c541725 25069 return (uintptr_t) ofs->per_cu + to_underlying (ofs->sect_off);
1c379e20
DJ
25070}
25071
dee91e82 25072/* Equality function for a dwarf2_per_cu_offset_and_type. */
1c379e20
DJ
25073
25074static int
dee91e82 25075per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
1c379e20 25076{
9a3c8263
SM
25077 const struct dwarf2_per_cu_offset_and_type *ofs_lhs
25078 = (const struct dwarf2_per_cu_offset_and_type *) item_lhs;
25079 const struct dwarf2_per_cu_offset_and_type *ofs_rhs
25080 = (const struct dwarf2_per_cu_offset_and_type *) item_rhs;
9a619af0 25081
dee91e82 25082 return (ofs_lhs->per_cu == ofs_rhs->per_cu
9c541725 25083 && ofs_lhs->sect_off == ofs_rhs->sect_off);
1c379e20
DJ
25084}
25085
25086/* Set the type associated with DIE to TYPE. Save it in CU's hash
7e314c57
JK
25087 table if necessary. For convenience, return TYPE.
25088
25089 The DIEs reading must have careful ordering to:
25090 * Not cause infite loops trying to read in DIEs as a prerequisite for
25091 reading current DIE.
25092 * Not trying to dereference contents of still incompletely read in types
25093 while reading in other DIEs.
25094 * Enable referencing still incompletely read in types just by a pointer to
25095 the type without accessing its fields.
25096
25097 Therefore caller should follow these rules:
25098 * Try to fetch any prerequisite types we may need to build this DIE type
25099 before building the type and calling set_die_type.
e71ec853 25100 * After building type call set_die_type for current DIE as soon as
7e314c57
JK
25101 possible before fetching more types to complete the current type.
25102 * Make the type as complete as possible before fetching more types. */
1c379e20 25103
f792889a 25104static struct type *
1c379e20
DJ
25105set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
25106{
518817b3
SM
25107 struct dwarf2_per_objfile *dwarf2_per_objfile
25108 = cu->per_cu->dwarf2_per_objfile;
dee91e82 25109 struct dwarf2_per_cu_offset_and_type **slot, ofs;
ed2dc618 25110 struct objfile *objfile = dwarf2_per_objfile->objfile;
3cdcd0ce
JB
25111 struct attribute *attr;
25112 struct dynamic_prop prop;
1c379e20 25113
b4ba55a1
JB
25114 /* For Ada types, make sure that the gnat-specific data is always
25115 initialized (if not already set). There are a few types where
25116 we should not be doing so, because the type-specific area is
25117 already used to hold some other piece of info (eg: TYPE_CODE_FLT
25118 where the type-specific area is used to store the floatformat).
25119 But this is not a problem, because the gnat-specific information
25120 is actually not needed for these types. */
25121 if (need_gnat_info (cu)
25122 && TYPE_CODE (type) != TYPE_CODE_FUNC
25123 && TYPE_CODE (type) != TYPE_CODE_FLT
09e2d7c7
DE
25124 && TYPE_CODE (type) != TYPE_CODE_METHODPTR
25125 && TYPE_CODE (type) != TYPE_CODE_MEMBERPTR
25126 && TYPE_CODE (type) != TYPE_CODE_METHOD
b4ba55a1
JB
25127 && !HAVE_GNAT_AUX_INFO (type))
25128 INIT_GNAT_SPECIFIC (type);
25129
3f2f83dd
KB
25130 /* Read DW_AT_allocated and set in type. */
25131 attr = dwarf2_attr (die, DW_AT_allocated, cu);
25132 if (attr_form_is_block (attr))
25133 {
25134 if (attr_to_dynamic_prop (attr, die, cu, &prop))
50a82047 25135 add_dyn_prop (DYN_PROP_ALLOCATED, prop, type);
3f2f83dd
KB
25136 }
25137 else if (attr != NULL)
25138 {
b98664d3 25139 complaint (_("DW_AT_allocated has the wrong form (%s) at DIE %s"),
9c541725 25140 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
9d8780f0 25141 sect_offset_str (die->sect_off));
3f2f83dd
KB
25142 }
25143
25144 /* Read DW_AT_associated and set in type. */
25145 attr = dwarf2_attr (die, DW_AT_associated, cu);
25146 if (attr_form_is_block (attr))
25147 {
25148 if (attr_to_dynamic_prop (attr, die, cu, &prop))
50a82047 25149 add_dyn_prop (DYN_PROP_ASSOCIATED, prop, type);
3f2f83dd
KB
25150 }
25151 else if (attr != NULL)
25152 {
b98664d3 25153 complaint (_("DW_AT_associated has the wrong form (%s) at DIE %s"),
9c541725 25154 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
9d8780f0 25155 sect_offset_str (die->sect_off));
3f2f83dd
KB
25156 }
25157
3cdcd0ce
JB
25158 /* Read DW_AT_data_location and set in type. */
25159 attr = dwarf2_attr (die, DW_AT_data_location, cu);
25160 if (attr_to_dynamic_prop (attr, die, cu, &prop))
50a82047 25161 add_dyn_prop (DYN_PROP_DATA_LOCATION, prop, type);
3cdcd0ce 25162
dee91e82 25163 if (dwarf2_per_objfile->die_type_hash == NULL)
f792889a 25164 {
dee91e82
DE
25165 dwarf2_per_objfile->die_type_hash =
25166 htab_create_alloc_ex (127,
25167 per_cu_offset_and_type_hash,
25168 per_cu_offset_and_type_eq,
25169 NULL,
25170 &objfile->objfile_obstack,
25171 hashtab_obstack_allocate,
25172 dummy_obstack_deallocate);
f792889a 25173 }
1c379e20 25174
dee91e82 25175 ofs.per_cu = cu->per_cu;
9c541725 25176 ofs.sect_off = die->sect_off;
1c379e20 25177 ofs.type = type;
dee91e82
DE
25178 slot = (struct dwarf2_per_cu_offset_and_type **)
25179 htab_find_slot (dwarf2_per_objfile->die_type_hash, &ofs, INSERT);
7e314c57 25180 if (*slot)
b98664d3 25181 complaint (_("A problem internal to GDB: DIE %s has type already set"),
9d8780f0 25182 sect_offset_str (die->sect_off));
8d749320
SM
25183 *slot = XOBNEW (&objfile->objfile_obstack,
25184 struct dwarf2_per_cu_offset_and_type);
1c379e20 25185 **slot = ofs;
f792889a 25186 return type;
1c379e20
DJ
25187}
25188
9c541725 25189/* Look up the type for the die at SECT_OFF in PER_CU in die_type_hash,
02142a6c 25190 or return NULL if the die does not have a saved type. */
1c379e20
DJ
25191
25192static struct type *
9c541725 25193get_die_type_at_offset (sect_offset sect_off,
673bfd45 25194 struct dwarf2_per_cu_data *per_cu)
1c379e20 25195{
dee91e82 25196 struct dwarf2_per_cu_offset_and_type *slot, ofs;
ed2dc618 25197 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
f792889a 25198
dee91e82 25199 if (dwarf2_per_objfile->die_type_hash == NULL)
f792889a 25200 return NULL;
1c379e20 25201
dee91e82 25202 ofs.per_cu = per_cu;
9c541725 25203 ofs.sect_off = sect_off;
9a3c8263
SM
25204 slot = ((struct dwarf2_per_cu_offset_and_type *)
25205 htab_find (dwarf2_per_objfile->die_type_hash, &ofs));
1c379e20
DJ
25206 if (slot)
25207 return slot->type;
25208 else
25209 return NULL;
25210}
25211
02142a6c 25212/* Look up the type for DIE in CU in die_type_hash,
673bfd45
DE
25213 or return NULL if DIE does not have a saved type. */
25214
25215static struct type *
25216get_die_type (struct die_info *die, struct dwarf2_cu *cu)
25217{
9c541725 25218 return get_die_type_at_offset (die->sect_off, cu->per_cu);
673bfd45
DE
25219}
25220
10b3939b
DJ
25221/* Add a dependence relationship from CU to REF_PER_CU. */
25222
25223static void
25224dwarf2_add_dependence (struct dwarf2_cu *cu,
25225 struct dwarf2_per_cu_data *ref_per_cu)
25226{
25227 void **slot;
25228
25229 if (cu->dependencies == NULL)
25230 cu->dependencies
25231 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
25232 NULL, &cu->comp_unit_obstack,
25233 hashtab_obstack_allocate,
25234 dummy_obstack_deallocate);
25235
25236 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
25237 if (*slot == NULL)
25238 *slot = ref_per_cu;
25239}
1c379e20 25240
f504f079
DE
25241/* Subroutine of dwarf2_mark to pass to htab_traverse.
25242 Set the mark field in every compilation unit in the
ae038cb0
DJ
25243 cache that we must keep because we are keeping CU. */
25244
10b3939b
DJ
25245static int
25246dwarf2_mark_helper (void **slot, void *data)
25247{
25248 struct dwarf2_per_cu_data *per_cu;
25249
25250 per_cu = (struct dwarf2_per_cu_data *) *slot;
d07ed419
JK
25251
25252 /* cu->dependencies references may not yet have been ever read if QUIT aborts
25253 reading of the chain. As such dependencies remain valid it is not much
25254 useful to track and undo them during QUIT cleanups. */
25255 if (per_cu->cu == NULL)
25256 return 1;
25257
10b3939b
DJ
25258 if (per_cu->cu->mark)
25259 return 1;
25260 per_cu->cu->mark = 1;
25261
25262 if (per_cu->cu->dependencies != NULL)
25263 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
25264
25265 return 1;
25266}
25267
f504f079
DE
25268/* Set the mark field in CU and in every other compilation unit in the
25269 cache that we must keep because we are keeping CU. */
25270
ae038cb0
DJ
25271static void
25272dwarf2_mark (struct dwarf2_cu *cu)
25273{
25274 if (cu->mark)
25275 return;
25276 cu->mark = 1;
10b3939b
DJ
25277 if (cu->dependencies != NULL)
25278 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
ae038cb0
DJ
25279}
25280
25281static void
25282dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
25283{
25284 while (per_cu)
25285 {
25286 per_cu->cu->mark = 0;
25287 per_cu = per_cu->cu->read_in_chain;
25288 }
72bf9492
DJ
25289}
25290
72bf9492
DJ
25291/* Trivial hash function for partial_die_info: the hash value of a DIE
25292 is its offset in .debug_info for this objfile. */
25293
25294static hashval_t
25295partial_die_hash (const void *item)
25296{
9a3c8263
SM
25297 const struct partial_die_info *part_die
25298 = (const struct partial_die_info *) item;
9a619af0 25299
9c541725 25300 return to_underlying (part_die->sect_off);
72bf9492
DJ
25301}
25302
25303/* Trivial comparison function for partial_die_info structures: two DIEs
25304 are equal if they have the same offset. */
25305
25306static int
25307partial_die_eq (const void *item_lhs, const void *item_rhs)
25308{
9a3c8263
SM
25309 const struct partial_die_info *part_die_lhs
25310 = (const struct partial_die_info *) item_lhs;
25311 const struct partial_die_info *part_die_rhs
25312 = (const struct partial_die_info *) item_rhs;
9a619af0 25313
9c541725 25314 return part_die_lhs->sect_off == part_die_rhs->sect_off;
72bf9492
DJ
25315}
25316
b4f54984
DE
25317static struct cmd_list_element *set_dwarf_cmdlist;
25318static struct cmd_list_element *show_dwarf_cmdlist;
ae038cb0
DJ
25319
25320static void
981a3fb3 25321set_dwarf_cmd (const char *args, int from_tty)
ae038cb0 25322{
b4f54984 25323 help_list (set_dwarf_cmdlist, "maintenance set dwarf ", all_commands,
635c7e8a 25324 gdb_stdout);
ae038cb0
DJ
25325}
25326
25327static void
981a3fb3 25328show_dwarf_cmd (const char *args, int from_tty)
6e70227d 25329{
b4f54984 25330 cmd_show_list (show_dwarf_cmdlist, from_tty, "");
ae038cb0
DJ
25331}
25332
cd4fb1b2 25333int dwarf_always_disassemble;
437afbb8 25334
437afbb8 25335static void
cd4fb1b2
SM
25336show_dwarf_always_disassemble (struct ui_file *file, int from_tty,
25337 struct cmd_list_element *c, const char *value)
9291a0cd 25338{
cd4fb1b2
SM
25339 fprintf_filtered (file,
25340 _("Whether to always disassemble "
25341 "DWARF expressions is %s.\n"),
25342 value);
9291a0cd
TT
25343}
25344
9291a0cd 25345static void
cd4fb1b2
SM
25346show_check_physname (struct ui_file *file, int from_tty,
25347 struct cmd_list_element *c, const char *value)
9291a0cd 25348{
cd4fb1b2
SM
25349 fprintf_filtered (file,
25350 _("Whether to check \"physname\" is %s.\n"),
25351 value);
9291a0cd
TT
25352}
25353
cd4fb1b2
SM
25354void
25355_initialize_dwarf2_read (void)
9291a0cd 25356{
d95d3aef
TT
25357 dwarf2_objfile_data_key
25358 = register_objfile_data_with_cleanup (nullptr, dwarf2_free_objfile);
156942c7 25359
cd4fb1b2
SM
25360 add_prefix_cmd ("dwarf", class_maintenance, set_dwarf_cmd, _("\
25361Set DWARF specific variables.\n\
25362Configure DWARF variables such as the cache size"),
25363 &set_dwarf_cmdlist, "maintenance set dwarf ",
25364 0/*allow-unknown*/, &maintenance_set_cmdlist);
156942c7 25365
cd4fb1b2
SM
25366 add_prefix_cmd ("dwarf", class_maintenance, show_dwarf_cmd, _("\
25367Show DWARF specific variables\n\
25368Show DWARF variables such as the cache size"),
25369 &show_dwarf_cmdlist, "maintenance show dwarf ",
25370 0/*allow-unknown*/, &maintenance_show_cmdlist);
156942c7 25371
cd4fb1b2
SM
25372 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
25373 &dwarf_max_cache_age, _("\
25374Set the upper bound on the age of cached DWARF compilation units."), _("\
25375Show the upper bound on the age of cached DWARF compilation units."), _("\
25376A higher limit means that cached compilation units will be stored\n\
25377in memory longer, and more total memory will be used. Zero disables\n\
25378caching, which can slow down startup."),
25379 NULL,
25380 show_dwarf_max_cache_age,
25381 &set_dwarf_cmdlist,
25382 &show_dwarf_cmdlist);
156942c7 25383
cd4fb1b2
SM
25384 add_setshow_boolean_cmd ("always-disassemble", class_obscure,
25385 &dwarf_always_disassemble, _("\
25386Set whether `info address' always disassembles DWARF expressions."), _("\
25387Show whether `info address' always disassembles DWARF expressions."), _("\
25388When enabled, DWARF expressions are always printed in an assembly-like\n\
25389syntax. When disabled, expressions will be printed in a more\n\
25390conversational style, when possible."),
25391 NULL,
25392 show_dwarf_always_disassemble,
25393 &set_dwarf_cmdlist,
25394 &show_dwarf_cmdlist);
9291a0cd 25395
cd4fb1b2
SM
25396 add_setshow_zuinteger_cmd ("dwarf-read", no_class, &dwarf_read_debug, _("\
25397Set debugging of the DWARF reader."), _("\
25398Show debugging of the DWARF reader."), _("\
25399When enabled (non-zero), debugging messages are printed during DWARF\n\
25400reading and symtab expansion. A value of 1 (one) provides basic\n\
25401information. A value greater than 1 provides more verbose information."),
25402 NULL,
25403 NULL,
25404 &setdebuglist, &showdebuglist);
9291a0cd 25405
cd4fb1b2
SM
25406 add_setshow_zuinteger_cmd ("dwarf-die", no_class, &dwarf_die_debug, _("\
25407Set debugging of the DWARF DIE reader."), _("\
25408Show debugging of the DWARF DIE reader."), _("\
25409When enabled (non-zero), DIEs are dumped after they are read in.\n\
25410The value is the maximum depth to print."),
25411 NULL,
25412 NULL,
25413 &setdebuglist, &showdebuglist);
9291a0cd 25414
cd4fb1b2
SM
25415 add_setshow_zuinteger_cmd ("dwarf-line", no_class, &dwarf_line_debug, _("\
25416Set debugging of the dwarf line reader."), _("\
25417Show debugging of the dwarf line reader."), _("\
25418When enabled (non-zero), line number entries are dumped as they are read in.\n\
25419A value of 1 (one) provides basic information.\n\
25420A value greater than 1 provides more verbose information."),
25421 NULL,
25422 NULL,
25423 &setdebuglist, &showdebuglist);
437afbb8 25424
cd4fb1b2
SM
25425 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
25426Set cross-checking of \"physname\" code against demangler."), _("\
25427Show cross-checking of \"physname\" code against demangler."), _("\
25428When enabled, GDB's internal \"physname\" code is checked against\n\
25429the demangler."),
25430 NULL, show_check_physname,
25431 &setdebuglist, &showdebuglist);
900e11f9 25432
e615022a
DE
25433 add_setshow_boolean_cmd ("use-deprecated-index-sections",
25434 no_class, &use_deprecated_index_sections, _("\
25435Set whether to use deprecated gdb_index sections."), _("\
25436Show whether to use deprecated gdb_index sections."), _("\
25437When enabled, deprecated .gdb_index sections are used anyway.\n\
25438Normally they are ignored either because of a missing feature or\n\
25439performance issue.\n\
25440Warning: This option must be enabled before gdb reads the file."),
25441 NULL,
25442 NULL,
25443 &setlist, &showlist);
25444
f1e6e072
TT
25445 dwarf2_locexpr_index = register_symbol_computed_impl (LOC_COMPUTED,
25446 &dwarf2_locexpr_funcs);
25447 dwarf2_loclist_index = register_symbol_computed_impl (LOC_COMPUTED,
25448 &dwarf2_loclist_funcs);
25449
25450 dwarf2_locexpr_block_index = register_symbol_block_impl (LOC_BLOCK,
25451 &dwarf2_block_frame_base_locexpr_funcs);
25452 dwarf2_loclist_block_index = register_symbol_block_impl (LOC_BLOCK,
25453 &dwarf2_block_frame_base_loclist_funcs);
c62446b1
PA
25454
25455#if GDB_SELF_TEST
25456 selftests::register_test ("dw2_expand_symtabs_matching",
25457 selftests::dw2_expand_symtabs_matching::run_test);
25458#endif
6502dd73 25459}
This page took 4.980604 seconds and 4 git commands to generate.