Remove the "listhead" argument from finish_block
[deliverable/binutils-gdb.git] / gdb / dwarf2read.c
CommitLineData
c906108c 1/* DWARF 2 debugging format support for GDB.
917c78fc 2
e2882c85 3 Copyright (C) 1994-2018 Free Software Foundation, Inc.
c906108c
SS
4
5 Adapted by Gary Funck (gary@intrepid.com), Intrepid Technology,
6 Inc. with support from Florida State University (under contract
7 with the Ada Joint Program Office), and Silicon Graphics, Inc.
8 Initial contribution by Brent Benson, Harris Computer Systems, Inc.,
9 based on Fred Fish's (Cygnus Support) implementation of DWARF 1
7ce59000 10 support.
c906108c 11
c5aa993b 12 This file is part of GDB.
c906108c 13
c5aa993b
JM
14 This program is free software; you can redistribute it and/or modify
15 it under the terms of the GNU General Public License as published by
a9762ec7
JB
16 the Free Software Foundation; either version 3 of the License, or
17 (at your option) any later version.
c906108c 18
a9762ec7
JB
19 This program is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 GNU General Public License for more details.
c906108c 23
c5aa993b 24 You should have received a copy of the GNU General Public License
a9762ec7 25 along with this program. If not, see <http://www.gnu.org/licenses/>. */
c906108c 26
21b2bd31
DE
27/* FIXME: Various die-reading functions need to be more careful with
28 reading off the end of the section.
29 E.g., load_partial_dies, read_partial_die. */
30
c906108c 31#include "defs.h"
cd4fb1b2
SM
32#include "dwarf2read.h"
33#include "dwarf-index-common.h"
c906108c 34#include "bfd.h"
80626a55 35#include "elf-bfd.h"
c906108c
SS
36#include "symtab.h"
37#include "gdbtypes.h"
c906108c 38#include "objfiles.h"
fa8f86ff 39#include "dwarf2.h"
c906108c
SS
40#include "buildsym.h"
41#include "demangle.h"
50f182aa 42#include "gdb-demangle.h"
c906108c 43#include "expression.h"
d5166ae1 44#include "filenames.h" /* for DOSish file names */
2e276125 45#include "macrotab.h"
c906108c
SS
46#include "language.h"
47#include "complaints.h"
357e46e7 48#include "bcache.h"
4c2df51b
DJ
49#include "dwarf2expr.h"
50#include "dwarf2loc.h"
9219021c 51#include "cp-support.h"
72bf9492 52#include "hashtab.h"
ae038cb0
DJ
53#include "command.h"
54#include "gdbcmd.h"
edb3359d 55#include "block.h"
ff013f42 56#include "addrmap.h"
94af9270 57#include "typeprint.h"
ccefe4c4 58#include "psympriv.h"
53ce3c39 59#include <sys/stat.h>
96d19272 60#include "completer.h"
34eaf542 61#include "vec.h"
98bfdba5 62#include "c-lang.h"
a766d390 63#include "go-lang.h"
98bfdba5 64#include "valprint.h"
3019eac3 65#include "gdbcore.h" /* for gnutarget */
156942c7 66#include "gdb/gdb-index.h"
60d5a603 67#include <ctype.h>
cbb099e8 68#include "gdb_bfd.h"
4357ac6c 69#include "f-lang.h"
05cba821 70#include "source.h"
614c279d 71#include "filestuff.h"
dc294be5 72#include "build-id.h"
22cee43f 73#include "namespace.h"
bef155c3 74#include "common/gdb_unlinker.h"
14bc53a8 75#include "common/function-view.h"
ecfb656c
PA
76#include "common/gdb_optional.h"
77#include "common/underlying.h"
d5722aa2 78#include "common/byte-vector.h"
927aa2e7 79#include "common/hash_enum.h"
bbf2f4df 80#include "filename-seen-cache.h"
b32b108a 81#include "producer.h"
c906108c 82#include <fcntl.h>
c906108c 83#include <sys/types.h>
325fac50 84#include <algorithm>
bc8f2430
JK
85#include <unordered_set>
86#include <unordered_map>
c62446b1 87#include "selftest.h"
437afbb8
JK
88#include <cmath>
89#include <set>
90#include <forward_list>
c9317f21 91#include "rust-lang.h"
b4987c95 92#include "common/pathstuff.h"
437afbb8 93
73be47f5
DE
94/* When == 1, print basic high level tracing messages.
95 When > 1, be more verbose.
b4f54984
DE
96 This is in contrast to the low level DIE reading of dwarf_die_debug. */
97static unsigned int dwarf_read_debug = 0;
45cfd468 98
d97bc12b 99/* When non-zero, dump DIEs after they are read in. */
b4f54984 100static unsigned int dwarf_die_debug = 0;
d97bc12b 101
27e0867f
DE
102/* When non-zero, dump line number entries as they are read in. */
103static unsigned int dwarf_line_debug = 0;
104
900e11f9
JK
105/* When non-zero, cross-check physname against demangler. */
106static int check_physname = 0;
107
481860b3 108/* When non-zero, do not reject deprecated .gdb_index sections. */
e615022a 109static int use_deprecated_index_sections = 0;
481860b3 110
6502dd73
DJ
111static const struct objfile_data *dwarf2_objfile_data_key;
112
f1e6e072
TT
113/* The "aclass" indices for various kinds of computed DWARF symbols. */
114
115static int dwarf2_locexpr_index;
116static int dwarf2_loclist_index;
117static int dwarf2_locexpr_block_index;
118static int dwarf2_loclist_block_index;
119
3f563c84
PA
120/* An index into a (C++) symbol name component in a symbol name as
121 recorded in the mapped_index's symbol table. For each C++ symbol
122 in the symbol table, we record one entry for the start of each
123 component in the symbol in a table of name components, and then
124 sort the table, in order to be able to binary search symbol names,
125 ignoring leading namespaces, both completion and regular look up.
126 For example, for symbol "A::B::C", we'll have an entry that points
127 to "A::B::C", another that points to "B::C", and another for "C".
128 Note that function symbols in GDB index have no parameter
129 information, just the function/method names. You can convert a
130 name_component to a "const char *" using the
131 'mapped_index::symbol_name_at(offset_type)' method. */
132
133struct name_component
134{
135 /* Offset in the symbol name where the component starts. Stored as
136 a (32-bit) offset instead of a pointer to save memory and improve
137 locality on 64-bit architectures. */
138 offset_type name_offset;
139
140 /* The symbol's index in the symbol and constant pool tables of a
141 mapped_index. */
142 offset_type idx;
143};
144
44ed8f3e
PA
145/* Base class containing bits shared by both .gdb_index and
146 .debug_name indexes. */
147
148struct mapped_index_base
149{
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
e142c38c
DJ
438 /* The generic symbol table building routines have separate lists for
439 file scope symbols and all all other scopes (local scopes). So
440 we need to select the right one to pass to add_symbol_to_list().
441 We do it by keeping a pointer to the correct list in list_in_scope.
442
443 FIXME: The original dwarf code just treated the file scope as the
444 first local scope, and all other local scopes as nested local
445 scopes, and worked fine. Check to see if we really need to
446 distinguish these in buildsym.c. */
fcd3b13d 447 struct pending **list_in_scope = nullptr;
e142c38c 448
b64f50a1
JK
449 /* Hash table holding all the loaded partial DIEs
450 with partial_die->offset.SECT_OFF as hash. */
fcd3b13d 451 htab_t partial_dies = nullptr;
72bf9492
DJ
452
453 /* Storage for things with the same lifetime as this read-in compilation
454 unit, including partial DIEs. */
fcd3b13d 455 auto_obstack comp_unit_obstack;
72bf9492 456
ae038cb0
DJ
457 /* When multiple dwarf2_cu structures are living in memory, this field
458 chains them all together, so that they can be released efficiently.
459 We will probably also want a generation counter so that most-recently-used
460 compilation units are cached... */
fcd3b13d 461 struct dwarf2_per_cu_data *read_in_chain = nullptr;
ae038cb0 462
69d751e3 463 /* Backlink to our per_cu entry. */
ae038cb0
DJ
464 struct dwarf2_per_cu_data *per_cu;
465
466 /* How many compilation units ago was this CU last referenced? */
fcd3b13d 467 int last_used = 0;
ae038cb0 468
b64f50a1
JK
469 /* A hash table of DIE cu_offset for following references with
470 die_info->offset.sect_off as hash. */
fcd3b13d 471 htab_t die_hash = nullptr;
10b3939b
DJ
472
473 /* Full DIEs if read in. */
fcd3b13d 474 struct die_info *dies = nullptr;
10b3939b
DJ
475
476 /* A set of pointers to dwarf2_per_cu_data objects for compilation
477 units referenced by this one. Only set during full symbol processing;
478 partial symbol tables do not have dependencies. */
fcd3b13d 479 htab_t dependencies = nullptr;
10b3939b 480
cb1df416 481 /* Header data from the line table, during full symbol processing. */
fcd3b13d 482 struct line_header *line_header = nullptr;
4c8aa72d
PA
483 /* Non-NULL if LINE_HEADER is owned by this DWARF_CU. Otherwise,
484 it's owned by dwarf2_per_objfile::line_header_hash. If non-NULL,
485 this is the DW_TAG_compile_unit die for this CU. We'll hold on
486 to the line header as long as this DIE is being processed. See
487 process_die_scope. */
fcd3b13d 488 die_info *line_header_die_owner = nullptr;
cb1df416 489
3da10d80
KS
490 /* A list of methods which need to have physnames computed
491 after all type information has been read. */
c89b44cd 492 std::vector<delayed_method_info> method_list;
3da10d80 493
96408a79 494 /* To be copied to symtab->call_site_htab. */
fcd3b13d 495 htab_t call_site_htab = nullptr;
96408a79 496
034e5797
DE
497 /* Non-NULL if this CU came from a DWO file.
498 There is an invariant here that is important to remember:
499 Except for attributes copied from the top level DIE in the "main"
500 (or "stub") file in preparation for reading the DWO file
501 (e.g., DW_AT_GNU_addr_base), we KISS: there is only *one* CU.
502 Either there isn't a DWO file (in which case this is NULL and the point
503 is moot), or there is and either we're not going to read it (in which
504 case this is NULL) or there is and we are reading it (in which case this
505 is non-NULL). */
fcd3b13d 506 struct dwo_unit *dwo_unit = nullptr;
3019eac3
DE
507
508 /* The DW_AT_addr_base attribute if present, zero otherwise
509 (zero is a valid value though).
1dbab08b 510 Note this value comes from the Fission stub CU/TU's DIE. */
fcd3b13d 511 ULONGEST addr_base = 0;
3019eac3 512
2e3cf129
DE
513 /* The DW_AT_ranges_base attribute if present, zero otherwise
514 (zero is a valid value though).
1dbab08b 515 Note this value comes from the Fission stub CU/TU's DIE.
2e3cf129 516 Also note that the value is zero in the non-DWO case so this value can
ab435259
DE
517 be used without needing to know whether DWO files are in use or not.
518 N.B. This does not apply to DW_AT_ranges appearing in
519 DW_TAG_compile_unit dies. This is a bit of a wart, consider if ever
520 DW_AT_ranges appeared in the DW_TAG_compile_unit of DWO DIEs: then
521 DW_AT_ranges_base *would* have to be applied, and we'd have to care
522 whether the DW_AT_ranges attribute came from the skeleton or DWO. */
fcd3b13d 523 ULONGEST ranges_base = 0;
2e3cf129 524
c9317f21
TT
525 /* When reading debug info generated by older versions of rustc, we
526 have to rewrite some union types to be struct types with a
527 variant part. This rewriting must be done after the CU is fully
528 read in, because otherwise at the point of rewriting some struct
529 type might not have been fully processed. So, we keep a list of
530 all such types here and process them after expansion. */
531 std::vector<struct type *> rust_unions;
532
ae038cb0
DJ
533 /* Mark used when releasing cached dies. */
534 unsigned int mark : 1;
535
8be455d7
JK
536 /* This CU references .debug_loc. See the symtab->locations_valid field.
537 This test is imperfect as there may exist optimized debug code not using
538 any location list and still facing inlining issues if handled as
539 unoptimized code. For a future better test see GCC PR other/32998. */
8be455d7 540 unsigned int has_loclist : 1;
ba919b58 541
1b80a9fa
JK
542 /* These cache the results for producer_is_* fields. CHECKED_PRODUCER is set
543 if all the producer_is_* fields are valid. This information is cached
544 because profiling CU expansion showed excessive time spent in
545 producer_is_gxx_lt_4_6. */
ba919b58
TT
546 unsigned int checked_producer : 1;
547 unsigned int producer_is_gxx_lt_4_6 : 1;
1b80a9fa 548 unsigned int producer_is_gcc_lt_4_3 : 1;
5230b05a 549 unsigned int producer_is_icc_lt_14 : 1;
4d4ec4e5
TT
550
551 /* When set, the file that we're processing is known to have
552 debugging info for C++ namespaces. GCC 3.3.x did not produce
553 this information, but later versions do. */
554
555 unsigned int processing_has_namespace_info : 1;
d590ff25
YQ
556
557 struct partial_die_info *find_partial_die (sect_offset sect_off);
e7c27a73
DJ
558};
559
094b34ac
DE
560/* A struct that can be used as a hash key for tables based on DW_AT_stmt_list.
561 This includes type_unit_group and quick_file_names. */
562
563struct stmt_list_hash
564{
565 /* The DWO unit this table is from or NULL if there is none. */
566 struct dwo_unit *dwo_unit;
567
568 /* Offset in .debug_line or .debug_line.dwo. */
9c541725 569 sect_offset line_sect_off;
094b34ac
DE
570};
571
f4dc4d17
DE
572/* Each element of dwarf2_per_objfile->type_unit_groups is a pointer to
573 an object of this type. */
574
575struct type_unit_group
576{
0186c6a7 577 /* dwarf2read.c's main "handle" on a TU symtab.
f4dc4d17
DE
578 To simplify things we create an artificial CU that "includes" all the
579 type units using this stmt_list so that the rest of the code still has
580 a "per_cu" handle on the symtab.
581 This PER_CU is recognized by having no section. */
8a0459fd 582#define IS_TYPE_UNIT_GROUP(per_cu) ((per_cu)->section == NULL)
094b34ac
DE
583 struct dwarf2_per_cu_data per_cu;
584
0186c6a7
DE
585 /* The TUs that share this DW_AT_stmt_list entry.
586 This is added to while parsing type units to build partial symtabs,
587 and is deleted afterwards and not used again. */
588 VEC (sig_type_ptr) *tus;
f4dc4d17 589
43f3e411 590 /* The compunit symtab.
094b34ac 591 Type units in a group needn't all be defined in the same source file,
43f3e411
DE
592 so we create an essentially anonymous symtab as the compunit symtab. */
593 struct compunit_symtab *compunit_symtab;
f4dc4d17 594
094b34ac
DE
595 /* The data used to construct the hash key. */
596 struct stmt_list_hash hash;
f4dc4d17
DE
597
598 /* The number of symtabs from the line header.
599 The value here must match line_header.num_file_names. */
600 unsigned int num_symtabs;
601
602 /* The symbol tables for this TU (obtained from the files listed in
603 DW_AT_stmt_list).
604 WARNING: The order of entries here must match the order of entries
605 in the line header. After the first TU using this type_unit_group, the
606 line header for the subsequent TUs is recreated from this. This is done
607 because we need to use the same symtabs for each TU using the same
608 DW_AT_stmt_list value. Also note that symtabs may be repeated here,
609 there's no guarantee the line header doesn't have duplicate entries. */
610 struct symtab **symtabs;
611};
612
73869dc2 613/* These sections are what may appear in a (real or virtual) DWO file. */
3019eac3
DE
614
615struct dwo_sections
616{
617 struct dwarf2_section_info abbrev;
3019eac3
DE
618 struct dwarf2_section_info line;
619 struct dwarf2_section_info loc;
43988095 620 struct dwarf2_section_info loclists;
09262596
DE
621 struct dwarf2_section_info macinfo;
622 struct dwarf2_section_info macro;
3019eac3
DE
623 struct dwarf2_section_info str;
624 struct dwarf2_section_info str_offsets;
80626a55
DE
625 /* In the case of a virtual DWO file, these two are unused. */
626 struct dwarf2_section_info info;
3019eac3
DE
627 VEC (dwarf2_section_info_def) *types;
628};
629
c88ee1f0 630/* CUs/TUs in DWP/DWO files. */
3019eac3
DE
631
632struct dwo_unit
633{
634 /* Backlink to the containing struct dwo_file. */
635 struct dwo_file *dwo_file;
636
637 /* The "id" that distinguishes this CU/TU.
638 .debug_info calls this "dwo_id", .debug_types calls this "signature".
639 Since signatures came first, we stick with it for consistency. */
640 ULONGEST signature;
641
642 /* The section this CU/TU lives in, in the DWO file. */
8a0459fd 643 struct dwarf2_section_info *section;
3019eac3 644
9c541725
PA
645 /* Same as dwarf2_per_cu_data:{sect_off,length} but in the DWO section. */
646 sect_offset sect_off;
3019eac3
DE
647 unsigned int length;
648
649 /* For types, offset in the type's DIE of the type defined by this TU. */
650 cu_offset type_offset_in_tu;
651};
652
73869dc2
DE
653/* include/dwarf2.h defines the DWP section codes.
654 It defines a max value but it doesn't define a min value, which we
655 use for error checking, so provide one. */
656
657enum dwp_v2_section_ids
658{
659 DW_SECT_MIN = 1
660};
661
80626a55 662/* Data for one DWO file.
57d63ce2
DE
663
664 This includes virtual DWO files (a virtual DWO file is a DWO file as it
665 appears in a DWP file). DWP files don't really have DWO files per se -
666 comdat folding of types "loses" the DWO file they came from, and from
667 a high level view DWP files appear to contain a mass of random types.
668 However, to maintain consistency with the non-DWP case we pretend DWP
669 files contain virtual DWO files, and we assign each TU with one virtual
670 DWO file (generally based on the line and abbrev section offsets -
671 a heuristic that seems to work in practice). */
3019eac3
DE
672
673struct dwo_file
674{
0ac5b59e 675 /* The DW_AT_GNU_dwo_name attribute.
80626a55
DE
676 For virtual DWO files the name is constructed from the section offsets
677 of abbrev,line,loc,str_offsets so that we combine virtual DWO files
678 from related CU+TUs. */
0ac5b59e
DE
679 const char *dwo_name;
680
681 /* The DW_AT_comp_dir attribute. */
682 const char *comp_dir;
3019eac3 683
80626a55
DE
684 /* The bfd, when the file is open. Otherwise this is NULL.
685 This is unused(NULL) for virtual DWO files where we use dwp_file.dbfd. */
686 bfd *dbfd;
3019eac3 687
73869dc2
DE
688 /* The sections that make up this DWO file.
689 Remember that for virtual DWO files in DWP V2, these are virtual
690 sections (for lack of a better name). */
3019eac3
DE
691 struct dwo_sections sections;
692
33c5cd75
DB
693 /* The CUs in the file.
694 Each element is a struct dwo_unit. Multiple CUs per DWO are supported as
695 an extension to handle LLVM's Link Time Optimization output (where
696 multiple source files may be compiled into a single object/dwo pair). */
697 htab_t cus;
3019eac3
DE
698
699 /* Table of TUs in the file.
700 Each element is a struct dwo_unit. */
701 htab_t tus;
702};
703
80626a55
DE
704/* These sections are what may appear in a DWP file. */
705
706struct dwp_sections
707{
73869dc2 708 /* These are used by both DWP version 1 and 2. */
80626a55
DE
709 struct dwarf2_section_info str;
710 struct dwarf2_section_info cu_index;
711 struct dwarf2_section_info tu_index;
73869dc2
DE
712
713 /* These are only used by DWP version 2 files.
714 In DWP version 1 the .debug_info.dwo, .debug_types.dwo, and other
715 sections are referenced by section number, and are not recorded here.
716 In DWP version 2 there is at most one copy of all these sections, each
717 section being (effectively) comprised of the concatenation of all of the
718 individual sections that exist in the version 1 format.
719 To keep the code simple we treat each of these concatenated pieces as a
720 section itself (a virtual section?). */
721 struct dwarf2_section_info abbrev;
722 struct dwarf2_section_info info;
723 struct dwarf2_section_info line;
724 struct dwarf2_section_info loc;
725 struct dwarf2_section_info macinfo;
726 struct dwarf2_section_info macro;
727 struct dwarf2_section_info str_offsets;
728 struct dwarf2_section_info types;
80626a55
DE
729};
730
73869dc2
DE
731/* These sections are what may appear in a virtual DWO file in DWP version 1.
732 A virtual DWO file is a DWO file as it appears in a DWP file. */
80626a55 733
73869dc2 734struct virtual_v1_dwo_sections
80626a55
DE
735{
736 struct dwarf2_section_info abbrev;
737 struct dwarf2_section_info line;
738 struct dwarf2_section_info loc;
739 struct dwarf2_section_info macinfo;
740 struct dwarf2_section_info macro;
741 struct dwarf2_section_info str_offsets;
742 /* Each DWP hash table entry records one CU or one TU.
8a0459fd 743 That is recorded here, and copied to dwo_unit.section. */
80626a55
DE
744 struct dwarf2_section_info info_or_types;
745};
746
73869dc2
DE
747/* Similar to virtual_v1_dwo_sections, but for DWP version 2.
748 In version 2, the sections of the DWO files are concatenated together
749 and stored in one section of that name. Thus each ELF section contains
750 several "virtual" sections. */
751
752struct virtual_v2_dwo_sections
753{
754 bfd_size_type abbrev_offset;
755 bfd_size_type abbrev_size;
756
757 bfd_size_type line_offset;
758 bfd_size_type line_size;
759
760 bfd_size_type loc_offset;
761 bfd_size_type loc_size;
762
763 bfd_size_type macinfo_offset;
764 bfd_size_type macinfo_size;
765
766 bfd_size_type macro_offset;
767 bfd_size_type macro_size;
768
769 bfd_size_type str_offsets_offset;
770 bfd_size_type str_offsets_size;
771
772 /* Each DWP hash table entry records one CU or one TU.
773 That is recorded here, and copied to dwo_unit.section. */
774 bfd_size_type info_or_types_offset;
775 bfd_size_type info_or_types_size;
776};
777
80626a55
DE
778/* Contents of DWP hash tables. */
779
780struct dwp_hash_table
781{
73869dc2 782 uint32_t version, nr_columns;
80626a55 783 uint32_t nr_units, nr_slots;
73869dc2
DE
784 const gdb_byte *hash_table, *unit_table;
785 union
786 {
787 struct
788 {
789 const gdb_byte *indices;
790 } v1;
791 struct
792 {
793 /* This is indexed by column number and gives the id of the section
794 in that column. */
795#define MAX_NR_V2_DWO_SECTIONS \
796 (1 /* .debug_info or .debug_types */ \
797 + 1 /* .debug_abbrev */ \
798 + 1 /* .debug_line */ \
799 + 1 /* .debug_loc */ \
800 + 1 /* .debug_str_offsets */ \
801 + 1 /* .debug_macro or .debug_macinfo */)
802 int section_ids[MAX_NR_V2_DWO_SECTIONS];
803 const gdb_byte *offsets;
804 const gdb_byte *sizes;
805 } v2;
806 } section_pool;
80626a55
DE
807};
808
809/* Data for one DWP file. */
810
811struct dwp_file
812{
400174b1
TT
813 dwp_file (const char *name_, gdb_bfd_ref_ptr &&abfd)
814 : name (name_),
815 dbfd (std::move (abfd))
816 {
817 }
818
80626a55
DE
819 /* Name of the file. */
820 const char *name;
821
73869dc2 822 /* File format version. */
400174b1 823 int version = 0;
73869dc2 824
93417882 825 /* The bfd. */
400174b1 826 gdb_bfd_ref_ptr dbfd;
80626a55
DE
827
828 /* Section info for this file. */
400174b1 829 struct dwp_sections sections {};
80626a55 830
57d63ce2 831 /* Table of CUs in the file. */
400174b1 832 const struct dwp_hash_table *cus = nullptr;
80626a55
DE
833
834 /* Table of TUs in the file. */
400174b1 835 const struct dwp_hash_table *tus = nullptr;
80626a55 836
19ac8c2e 837 /* Tables of loaded CUs/TUs. Each entry is a struct dwo_unit *. */
400174b1
TT
838 htab_t loaded_cus {};
839 htab_t loaded_tus {};
80626a55 840
73869dc2
DE
841 /* Table to map ELF section numbers to their sections.
842 This is only needed for the DWP V1 file format. */
400174b1
TT
843 unsigned int num_sections = 0;
844 asection **elf_sections = nullptr;
80626a55
DE
845};
846
36586728
TT
847/* This represents a '.dwz' file. */
848
849struct dwz_file
850{
7ff8cb8c
TT
851 dwz_file (gdb_bfd_ref_ptr &&bfd)
852 : dwz_bfd (std::move (bfd))
853 {
854 }
855
36586728 856 /* A dwz file can only contain a few sections. */
7ff8cb8c
TT
857 struct dwarf2_section_info abbrev {};
858 struct dwarf2_section_info info {};
859 struct dwarf2_section_info str {};
860 struct dwarf2_section_info line {};
861 struct dwarf2_section_info macro {};
862 struct dwarf2_section_info gdb_index {};
863 struct dwarf2_section_info debug_names {};
36586728
TT
864
865 /* The dwz's BFD. */
7ff8cb8c 866 gdb_bfd_ref_ptr dwz_bfd;
36586728
TT
867};
868
0963b4bd
MS
869/* Struct used to pass misc. parameters to read_die_and_children, et
870 al. which are used for both .debug_info and .debug_types dies.
871 All parameters here are unchanging for the life of the call. This
dee91e82 872 struct exists to abstract away the constant parameters of die reading. */
93311388
DE
873
874struct die_reader_specs
875{
a32a8923 876 /* The bfd of die_section. */
93311388
DE
877 bfd* abfd;
878
879 /* The CU of the DIE we are parsing. */
880 struct dwarf2_cu *cu;
881
80626a55 882 /* Non-NULL if reading a DWO file (including one packaged into a DWP). */
3019eac3
DE
883 struct dwo_file *dwo_file;
884
dee91e82 885 /* The section the die comes from.
3019eac3 886 This is either .debug_info or .debug_types, or the .dwo variants. */
dee91e82
DE
887 struct dwarf2_section_info *die_section;
888
889 /* die_section->buffer. */
d521ce57 890 const gdb_byte *buffer;
f664829e
DE
891
892 /* The end of the buffer. */
893 const gdb_byte *buffer_end;
a2ce51a0
DE
894
895 /* The value of the DW_AT_comp_dir attribute. */
896 const char *comp_dir;
685af9cd
TT
897
898 /* The abbreviation table to use when reading the DIEs. */
899 struct abbrev_table *abbrev_table;
93311388
DE
900};
901
fd820528 902/* Type of function passed to init_cutu_and_read_dies, et.al. */
dee91e82 903typedef void (die_reader_func_ftype) (const struct die_reader_specs *reader,
d521ce57 904 const gdb_byte *info_ptr,
dee91e82
DE
905 struct die_info *comp_unit_die,
906 int has_children,
907 void *data);
908
ecfb656c
PA
909/* A 1-based directory index. This is a strong typedef to prevent
910 accidentally using a directory index as a 0-based index into an
911 array/vector. */
912enum class dir_index : unsigned int {};
913
914/* Likewise, a 1-based file name index. */
915enum class file_name_index : unsigned int {};
916
52059ffd
TT
917struct file_entry
918{
fff8551c
PA
919 file_entry () = default;
920
ecfb656c 921 file_entry (const char *name_, dir_index d_index_,
fff8551c
PA
922 unsigned int mod_time_, unsigned int length_)
923 : name (name_),
ecfb656c 924 d_index (d_index_),
fff8551c
PA
925 mod_time (mod_time_),
926 length (length_)
927 {}
928
ecfb656c
PA
929 /* Return the include directory at D_INDEX stored in LH. Returns
930 NULL if D_INDEX is out of bounds. */
8c43009f
PA
931 const char *include_dir (const line_header *lh) const;
932
fff8551c
PA
933 /* The file name. Note this is an observing pointer. The memory is
934 owned by debug_line_buffer. */
935 const char *name {};
936
8c43009f 937 /* The directory index (1-based). */
ecfb656c 938 dir_index d_index {};
fff8551c
PA
939
940 unsigned int mod_time {};
941
942 unsigned int length {};
943
944 /* True if referenced by the Line Number Program. */
945 bool included_p {};
946
83769d0b 947 /* The associated symbol table, if any. */
fff8551c 948 struct symtab *symtab {};
52059ffd
TT
949};
950
debd256d
JB
951/* The line number information for a compilation unit (found in the
952 .debug_line section) begins with a "statement program header",
953 which contains the following information. */
954struct line_header
955{
fff8551c
PA
956 line_header ()
957 : offset_in_dwz {}
958 {}
959
960 /* Add an entry to the include directory table. */
961 void add_include_dir (const char *include_dir);
962
963 /* Add an entry to the file name table. */
ecfb656c 964 void add_file_name (const char *name, dir_index d_index,
fff8551c
PA
965 unsigned int mod_time, unsigned int length);
966
ecfb656c 967 /* Return the include dir at INDEX (1-based). Returns NULL if INDEX
8c43009f 968 is out of bounds. */
ecfb656c 969 const char *include_dir_at (dir_index index) const
8c43009f 970 {
ecfb656c
PA
971 /* Convert directory index number (1-based) to vector index
972 (0-based). */
973 size_t vec_index = to_underlying (index) - 1;
974
975 if (vec_index >= include_dirs.size ())
8c43009f 976 return NULL;
ecfb656c 977 return include_dirs[vec_index];
8c43009f
PA
978 }
979
ecfb656c 980 /* Return the file name at INDEX (1-based). Returns NULL if INDEX
8c43009f 981 is out of bounds. */
ecfb656c 982 file_entry *file_name_at (file_name_index index)
8c43009f 983 {
ecfb656c
PA
984 /* Convert file name index number (1-based) to vector index
985 (0-based). */
986 size_t vec_index = to_underlying (index) - 1;
987
988 if (vec_index >= file_names.size ())
fff8551c 989 return NULL;
ecfb656c 990 return &file_names[vec_index];
fff8551c
PA
991 }
992
993 /* Const version of the above. */
994 const file_entry *file_name_at (unsigned int index) const
995 {
996 if (index >= file_names.size ())
8c43009f
PA
997 return NULL;
998 return &file_names[index];
999 }
1000
527f3840 1001 /* Offset of line number information in .debug_line section. */
9c541725 1002 sect_offset sect_off {};
527f3840
JK
1003
1004 /* OFFSET is for struct dwz_file associated with dwarf2_per_objfile. */
fff8551c
PA
1005 unsigned offset_in_dwz : 1; /* Can't initialize bitfields in-class. */
1006
1007 unsigned int total_length {};
1008 unsigned short version {};
1009 unsigned int header_length {};
1010 unsigned char minimum_instruction_length {};
1011 unsigned char maximum_ops_per_instruction {};
1012 unsigned char default_is_stmt {};
1013 int line_base {};
1014 unsigned char line_range {};
1015 unsigned char opcode_base {};
debd256d
JB
1016
1017 /* standard_opcode_lengths[i] is the number of operands for the
1018 standard opcode whose value is i. This means that
1019 standard_opcode_lengths[0] is unused, and the last meaningful
1020 element is standard_opcode_lengths[opcode_base - 1]. */
fff8551c 1021 std::unique_ptr<unsigned char[]> standard_opcode_lengths;
debd256d 1022
fff8551c
PA
1023 /* The include_directories table. Note these are observing
1024 pointers. The memory is owned by debug_line_buffer. */
1025 std::vector<const char *> include_dirs;
debd256d 1026
fff8551c
PA
1027 /* The file_names table. */
1028 std::vector<file_entry> file_names;
debd256d
JB
1029
1030 /* The start and end of the statement program following this
6502dd73 1031 header. These point into dwarf2_per_objfile->line_buffer. */
fff8551c 1032 const gdb_byte *statement_program_start {}, *statement_program_end {};
debd256d 1033};
c906108c 1034
fff8551c
PA
1035typedef std::unique_ptr<line_header> line_header_up;
1036
8c43009f
PA
1037const char *
1038file_entry::include_dir (const line_header *lh) const
1039{
ecfb656c 1040 return lh->include_dir_at (d_index);
8c43009f
PA
1041}
1042
c906108c 1043/* When we construct a partial symbol table entry we only
0963b4bd 1044 need this much information. */
6f06d47b 1045struct partial_die_info : public allocate_on_obstack
c906108c 1046 {
6f06d47b
YQ
1047 partial_die_info (sect_offset sect_off, struct abbrev_info *abbrev);
1048
1049 /* Disable assign but still keep copy ctor, which is needed
1050 load_partial_dies. */
1051 partial_die_info& operator=(const partial_die_info& rhs) = delete;
1052
52356b79
YQ
1053 /* Adjust the partial die before generating a symbol for it. This
1054 function may set the is_external flag or change the DIE's
1055 name. */
1056 void fixup (struct dwarf2_cu *cu);
1057
48fbe735
YQ
1058 /* Read a minimal amount of information into the minimal die
1059 structure. */
1060 const gdb_byte *read (const struct die_reader_specs *reader,
1061 const struct abbrev_info &abbrev,
1062 const gdb_byte *info_ptr);
1063
72bf9492 1064 /* Offset of this DIE. */
6f06d47b 1065 const sect_offset sect_off;
72bf9492
DJ
1066
1067 /* DWARF-2 tag for this DIE. */
6f06d47b 1068 const ENUM_BITFIELD(dwarf_tag) tag : 16;
72bf9492 1069
72bf9492 1070 /* Assorted flags describing the data found in this DIE. */
6f06d47b
YQ
1071 const unsigned int has_children : 1;
1072
72bf9492
DJ
1073 unsigned int is_external : 1;
1074 unsigned int is_declaration : 1;
1075 unsigned int has_type : 1;
1076 unsigned int has_specification : 1;
1077 unsigned int has_pc_info : 1;
481860b3 1078 unsigned int may_be_inlined : 1;
72bf9492 1079
0c1b455e
TT
1080 /* This DIE has been marked DW_AT_main_subprogram. */
1081 unsigned int main_subprogram : 1;
1082
72bf9492
DJ
1083 /* Flag set if the SCOPE field of this structure has been
1084 computed. */
1085 unsigned int scope_set : 1;
1086
fa4028e9
JB
1087 /* Flag set if the DIE has a byte_size attribute. */
1088 unsigned int has_byte_size : 1;
1089
ff908ebf
AW
1090 /* Flag set if the DIE has a DW_AT_const_value attribute. */
1091 unsigned int has_const_value : 1;
1092
98bfdba5
PA
1093 /* Flag set if any of the DIE's children are template arguments. */
1094 unsigned int has_template_arguments : 1;
1095
52356b79 1096 /* Flag set if fixup has been called on this die. */
abc72ce4
DE
1097 unsigned int fixup_called : 1;
1098
36586728
TT
1099 /* Flag set if DW_TAG_imported_unit uses DW_FORM_GNU_ref_alt. */
1100 unsigned int is_dwz : 1;
1101
1102 /* Flag set if spec_offset uses DW_FORM_GNU_ref_alt. */
1103 unsigned int spec_is_dwz : 1;
1104
72bf9492 1105 /* The name of this DIE. Normally the value of DW_AT_name, but
94af9270 1106 sometimes a default name for unnamed DIEs. */
6f06d47b 1107 const char *name = nullptr;
72bf9492 1108
abc72ce4 1109 /* The linkage name, if present. */
6f06d47b 1110 const char *linkage_name = nullptr;
abc72ce4 1111
72bf9492
DJ
1112 /* The scope to prepend to our children. This is generally
1113 allocated on the comp_unit_obstack, so will disappear
1114 when this compilation unit leaves the cache. */
6f06d47b 1115 const char *scope = nullptr;
72bf9492 1116
95554aad
TT
1117 /* Some data associated with the partial DIE. The tag determines
1118 which field is live. */
1119 union
1120 {
1121 /* The location description associated with this DIE, if any. */
1122 struct dwarf_block *locdesc;
1123 /* The offset of an import, for DW_TAG_imported_unit. */
9c541725 1124 sect_offset sect_off;
6f06d47b 1125 } d {};
72bf9492
DJ
1126
1127 /* If HAS_PC_INFO, the PC range associated with this DIE. */
6f06d47b
YQ
1128 CORE_ADDR lowpc = 0;
1129 CORE_ADDR highpc = 0;
72bf9492 1130
93311388 1131 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
72bf9492 1132 DW_AT_sibling, if any. */
48fbe735
YQ
1133 /* NOTE: This member isn't strictly necessary, partial_die_info::read
1134 could return DW_AT_sibling values to its caller load_partial_dies. */
6f06d47b 1135 const gdb_byte *sibling = nullptr;
72bf9492
DJ
1136
1137 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
1138 DW_AT_specification (or DW_AT_abstract_origin or
1139 DW_AT_extension). */
6f06d47b 1140 sect_offset spec_offset {};
72bf9492
DJ
1141
1142 /* Pointers to this DIE's parent, first child, and next sibling,
1143 if any. */
6f06d47b
YQ
1144 struct partial_die_info *die_parent = nullptr;
1145 struct partial_die_info *die_child = nullptr;
1146 struct partial_die_info *die_sibling = nullptr;
1147
1148 friend struct partial_die_info *
1149 dwarf2_cu::find_partial_die (sect_offset sect_off);
1150
1151 private:
1152 /* Only need to do look up in dwarf2_cu::find_partial_die. */
1153 partial_die_info (sect_offset sect_off)
1154 : partial_die_info (sect_off, DW_TAG_padding, 0)
1155 {
1156 }
1157
1158 partial_die_info (sect_offset sect_off_, enum dwarf_tag tag_,
1159 int has_children_)
1160 : sect_off (sect_off_), tag (tag_), has_children (has_children_)
1161 {
1162 is_external = 0;
1163 is_declaration = 0;
1164 has_type = 0;
1165 has_specification = 0;
1166 has_pc_info = 0;
1167 may_be_inlined = 0;
1168 main_subprogram = 0;
1169 scope_set = 0;
1170 has_byte_size = 0;
1171 has_const_value = 0;
1172 has_template_arguments = 0;
1173 fixup_called = 0;
1174 is_dwz = 0;
1175 spec_is_dwz = 0;
1176 }
c906108c
SS
1177 };
1178
0963b4bd 1179/* This data structure holds the information of an abbrev. */
c906108c
SS
1180struct abbrev_info
1181 {
1182 unsigned int number; /* number identifying abbrev */
1183 enum dwarf_tag tag; /* dwarf tag */
f3dd6933
DJ
1184 unsigned short has_children; /* boolean */
1185 unsigned short num_attrs; /* number of attributes */
c906108c
SS
1186 struct attr_abbrev *attrs; /* an array of attribute descriptions */
1187 struct abbrev_info *next; /* next in chain */
1188 };
1189
1190struct attr_abbrev
1191 {
9d25dd43
DE
1192 ENUM_BITFIELD(dwarf_attribute) name : 16;
1193 ENUM_BITFIELD(dwarf_form) form : 16;
43988095
JK
1194
1195 /* It is valid only if FORM is DW_FORM_implicit_const. */
1196 LONGEST implicit_const;
c906108c
SS
1197 };
1198
433df2d4
DE
1199/* Size of abbrev_table.abbrev_hash_table. */
1200#define ABBREV_HASH_SIZE 121
1201
1202/* Top level data structure to contain an abbreviation table. */
1203
1204struct abbrev_table
1205{
685af9cd
TT
1206 explicit abbrev_table (sect_offset off)
1207 : sect_off (off)
1208 {
4a17f768 1209 m_abbrevs =
685af9cd 1210 XOBNEWVEC (&abbrev_obstack, struct abbrev_info *, ABBREV_HASH_SIZE);
4a17f768 1211 memset (m_abbrevs, 0, ABBREV_HASH_SIZE * sizeof (struct abbrev_info *));
685af9cd
TT
1212 }
1213
1214 DISABLE_COPY_AND_ASSIGN (abbrev_table);
1215
1216 /* Allocate space for a struct abbrev_info object in
1217 ABBREV_TABLE. */
1218 struct abbrev_info *alloc_abbrev ();
1219
1220 /* Add an abbreviation to the table. */
1221 void add_abbrev (unsigned int abbrev_number, struct abbrev_info *abbrev);
1222
1223 /* Look up an abbrev in the table.
1224 Returns NULL if the abbrev is not found. */
1225
1226 struct abbrev_info *lookup_abbrev (unsigned int abbrev_number);
1227
1228
f4dc4d17
DE
1229 /* Where the abbrev table came from.
1230 This is used as a sanity check when the table is used. */
685af9cd 1231 const sect_offset sect_off;
433df2d4
DE
1232
1233 /* Storage for the abbrev table. */
685af9cd 1234 auto_obstack abbrev_obstack;
433df2d4 1235
4a17f768
YQ
1236private:
1237
433df2d4
DE
1238 /* Hash table of abbrevs.
1239 This is an array of size ABBREV_HASH_SIZE allocated in abbrev_obstack.
1240 It could be statically allocated, but the previous code didn't so we
1241 don't either. */
4a17f768 1242 struct abbrev_info **m_abbrevs;
433df2d4
DE
1243};
1244
685af9cd
TT
1245typedef std::unique_ptr<struct abbrev_table> abbrev_table_up;
1246
0963b4bd 1247/* Attributes have a name and a value. */
b60c80d6
DJ
1248struct attribute
1249 {
9d25dd43 1250 ENUM_BITFIELD(dwarf_attribute) name : 16;
8285870a
JK
1251 ENUM_BITFIELD(dwarf_form) form : 15;
1252
1253 /* Has DW_STRING already been updated by dwarf2_canonicalize_name? This
1254 field should be in u.str (existing only for DW_STRING) but it is kept
1255 here for better struct attribute alignment. */
1256 unsigned int string_is_canonical : 1;
1257
b60c80d6
DJ
1258 union
1259 {
15d034d0 1260 const char *str;
b60c80d6 1261 struct dwarf_block *blk;
43bbcdc2
PH
1262 ULONGEST unsnd;
1263 LONGEST snd;
b60c80d6 1264 CORE_ADDR addr;
ac9ec31b 1265 ULONGEST signature;
b60c80d6
DJ
1266 }
1267 u;
1268 };
1269
0963b4bd 1270/* This data structure holds a complete die structure. */
c906108c
SS
1271struct die_info
1272 {
76815b17
DE
1273 /* DWARF-2 tag for this DIE. */
1274 ENUM_BITFIELD(dwarf_tag) tag : 16;
1275
1276 /* Number of attributes */
98bfdba5
PA
1277 unsigned char num_attrs;
1278
1279 /* True if we're presently building the full type name for the
1280 type derived from this DIE. */
1281 unsigned char building_fullname : 1;
76815b17 1282
adde2bff
DE
1283 /* True if this die is in process. PR 16581. */
1284 unsigned char in_process : 1;
1285
76815b17
DE
1286 /* Abbrev number */
1287 unsigned int abbrev;
1288
93311388 1289 /* Offset in .debug_info or .debug_types section. */
9c541725 1290 sect_offset sect_off;
78ba4af6
JB
1291
1292 /* The dies in a compilation unit form an n-ary tree. PARENT
1293 points to this die's parent; CHILD points to the first child of
1294 this node; and all the children of a given node are chained
4950bc1c 1295 together via their SIBLING fields. */
639d11d3
DC
1296 struct die_info *child; /* Its first child, if any. */
1297 struct die_info *sibling; /* Its next sibling, if any. */
1298 struct die_info *parent; /* Its parent, if any. */
c906108c 1299
b60c80d6
DJ
1300 /* An array of attributes, with NUM_ATTRS elements. There may be
1301 zero, but it's not common and zero-sized arrays are not
1302 sufficiently portable C. */
1303 struct attribute attrs[1];
c906108c
SS
1304 };
1305
0963b4bd 1306/* Get at parts of an attribute structure. */
c906108c
SS
1307
1308#define DW_STRING(attr) ((attr)->u.str)
8285870a 1309#define DW_STRING_IS_CANONICAL(attr) ((attr)->string_is_canonical)
c906108c
SS
1310#define DW_UNSND(attr) ((attr)->u.unsnd)
1311#define DW_BLOCK(attr) ((attr)->u.blk)
1312#define DW_SND(attr) ((attr)->u.snd)
1313#define DW_ADDR(attr) ((attr)->u.addr)
ac9ec31b 1314#define DW_SIGNATURE(attr) ((attr)->u.signature)
c906108c 1315
0963b4bd 1316/* Blocks are a bunch of untyped bytes. */
c906108c
SS
1317struct dwarf_block
1318 {
56eb65bd 1319 size_t size;
1d6edc3c
JK
1320
1321 /* Valid only if SIZE is not zero. */
d521ce57 1322 const gdb_byte *data;
c906108c
SS
1323 };
1324
c906108c
SS
1325#ifndef ATTR_ALLOC_CHUNK
1326#define ATTR_ALLOC_CHUNK 4
1327#endif
1328
c906108c
SS
1329/* Allocate fields for structs, unions and enums in this size. */
1330#ifndef DW_FIELD_ALLOC_CHUNK
1331#define DW_FIELD_ALLOC_CHUNK 4
1332#endif
1333
c906108c
SS
1334/* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
1335 but this would require a corresponding change in unpack_field_as_long
1336 and friends. */
1337static int bits_per_byte = 8;
1338
2ddeaf8a
TT
1339/* When reading a variant or variant part, we track a bit more
1340 information about the field, and store it in an object of this
1341 type. */
1342
1343struct variant_field
1344{
1345 /* If we see a DW_TAG_variant, then this will be the discriminant
1346 value. */
1347 ULONGEST discriminant_value;
1348 /* If we see a DW_TAG_variant, then this will be set if this is the
1349 default branch. */
1350 bool default_branch;
1351 /* While reading a DW_TAG_variant_part, this will be set if this
1352 field is the discriminant. */
1353 bool is_discriminant;
1354};
1355
52059ffd
TT
1356struct nextfield
1357{
be2daae6
TT
1358 int accessibility = 0;
1359 int virtuality = 0;
2ddeaf8a 1360 /* Extra information to describe a variant or variant part. */
be2daae6
TT
1361 struct variant_field variant {};
1362 struct field field {};
52059ffd
TT
1363};
1364
1365struct fnfieldlist
1366{
be2daae6
TT
1367 const char *name = nullptr;
1368 std::vector<struct fn_field> fnfields;
52059ffd
TT
1369};
1370
c906108c
SS
1371/* The routines that read and process dies for a C struct or C++ class
1372 pass lists of data member fields and lists of member function fields
1373 in an instance of a field_info structure, as defined below. */
1374struct field_info
c5aa993b 1375 {
0963b4bd 1376 /* List of data member and baseclasses fields. */
be2daae6
TT
1377 std::vector<struct nextfield> fields;
1378 std::vector<struct nextfield> baseclasses;
c906108c 1379
7d0ccb61 1380 /* Number of fields (including baseclasses). */
be2daae6 1381 int nfields = 0;
c906108c 1382
c5aa993b 1383 /* Set if the accesibility of one of the fields is not public. */
be2daae6 1384 int non_public_fields = 0;
c906108c 1385
c5aa993b
JM
1386 /* Member function fieldlist array, contains name of possibly overloaded
1387 member function, number of overloaded member functions and a pointer
1388 to the head of the member function field chain. */
be2daae6 1389 std::vector<struct fnfieldlist> fnfieldlists;
98751a41
JK
1390
1391 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
1392 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
be2daae6 1393 std::vector<struct decl_field> typedef_field_list;
883fd55a
KS
1394
1395 /* Nested types defined by this class and the number of elements in this
1396 list. */
be2daae6 1397 std::vector<struct decl_field> nested_types_list;
c5aa993b 1398 };
c906108c 1399
10b3939b
DJ
1400/* One item on the queue of compilation units to read in full symbols
1401 for. */
1402struct dwarf2_queue_item
1403{
1404 struct dwarf2_per_cu_data *per_cu;
95554aad 1405 enum language pretend_language;
10b3939b
DJ
1406 struct dwarf2_queue_item *next;
1407};
1408
1409/* The current queue. */
1410static struct dwarf2_queue_item *dwarf2_queue, *dwarf2_queue_tail;
1411
ae038cb0
DJ
1412/* Loaded secondary compilation units are kept in memory until they
1413 have not been referenced for the processing of this many
1414 compilation units. Set this to zero to disable caching. Cache
1415 sizes of up to at least twenty will improve startup time for
1416 typical inter-CU-reference binaries, at an obvious memory cost. */
b4f54984 1417static int dwarf_max_cache_age = 5;
920d2a44 1418static void
b4f54984
DE
1419show_dwarf_max_cache_age (struct ui_file *file, int from_tty,
1420 struct cmd_list_element *c, const char *value)
920d2a44 1421{
3e43a32a 1422 fprintf_filtered (file, _("The upper bound on the age of cached "
b4f54984 1423 "DWARF compilation units is %s.\n"),
920d2a44
AC
1424 value);
1425}
4390d890 1426\f
c906108c
SS
1427/* local function prototypes */
1428
a32a8923
DE
1429static const char *get_section_name (const struct dwarf2_section_info *);
1430
1431static const char *get_section_file_name (const struct dwarf2_section_info *);
1432
918dd910
JK
1433static void dwarf2_find_base_address (struct die_info *die,
1434 struct dwarf2_cu *cu);
1435
0018ea6f
DE
1436static struct partial_symtab *create_partial_symtab
1437 (struct dwarf2_per_cu_data *per_cu, const char *name);
1438
f1902523
JK
1439static void build_type_psymtabs_reader (const struct die_reader_specs *reader,
1440 const gdb_byte *info_ptr,
1441 struct die_info *type_unit_die,
1442 int has_children, void *data);
1443
ed2dc618
SM
1444static void dwarf2_build_psymtabs_hard
1445 (struct dwarf2_per_objfile *dwarf2_per_objfile);
c906108c 1446
72bf9492
DJ
1447static void scan_partial_symbols (struct partial_die_info *,
1448 CORE_ADDR *, CORE_ADDR *,
5734ee8b 1449 int, struct dwarf2_cu *);
c906108c 1450
72bf9492
DJ
1451static void add_partial_symbol (struct partial_die_info *,
1452 struct dwarf2_cu *);
63d06c5c 1453
72bf9492
DJ
1454static void add_partial_namespace (struct partial_die_info *pdi,
1455 CORE_ADDR *lowpc, CORE_ADDR *highpc,
cdc07690 1456 int set_addrmap, struct dwarf2_cu *cu);
63d06c5c 1457
5d7cb8df 1458static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
cdc07690 1459 CORE_ADDR *highpc, int set_addrmap,
5d7cb8df
JK
1460 struct dwarf2_cu *cu);
1461
72bf9492
DJ
1462static void add_partial_enumeration (struct partial_die_info *enum_pdi,
1463 struct dwarf2_cu *cu);
91c24f0a 1464
bc30ff58
JB
1465static void add_partial_subprogram (struct partial_die_info *pdi,
1466 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 1467 int need_pc, struct dwarf2_cu *cu);
bc30ff58 1468
257e7a09
YQ
1469static void dwarf2_read_symtab (struct partial_symtab *,
1470 struct objfile *);
c906108c 1471
a14ed312 1472static void psymtab_to_symtab_1 (struct partial_symtab *);
c906108c 1473
685af9cd 1474static abbrev_table_up abbrev_table_read_table
ed2dc618
SM
1475 (struct dwarf2_per_objfile *dwarf2_per_objfile, struct dwarf2_section_info *,
1476 sect_offset);
433df2d4 1477
d521ce57 1478static unsigned int peek_abbrev_code (bfd *, const gdb_byte *);
6caca83c 1479
dee91e82 1480static struct partial_die_info *load_partial_dies
d521ce57 1481 (const struct die_reader_specs *, const gdb_byte *, int);
72bf9492 1482
36586728 1483static struct partial_die_info *find_partial_die (sect_offset, int,
10b3939b 1484 struct dwarf2_cu *);
72bf9492 1485
d521ce57
TT
1486static const gdb_byte *read_attribute (const struct die_reader_specs *,
1487 struct attribute *, struct attr_abbrev *,
1488 const gdb_byte *);
a8329558 1489
a1855c1d 1490static unsigned int read_1_byte (bfd *, const gdb_byte *);
c906108c 1491
a1855c1d 1492static int read_1_signed_byte (bfd *, const gdb_byte *);
c906108c 1493
a1855c1d 1494static unsigned int read_2_bytes (bfd *, const gdb_byte *);
c906108c 1495
a1855c1d 1496static unsigned int read_4_bytes (bfd *, const gdb_byte *);
c906108c 1497
a1855c1d 1498static ULONGEST read_8_bytes (bfd *, const gdb_byte *);
c906108c 1499
d521ce57 1500static CORE_ADDR read_address (bfd *, const gdb_byte *ptr, struct dwarf2_cu *,
891d2f0b 1501 unsigned int *);
c906108c 1502
d521ce57 1503static LONGEST read_initial_length (bfd *, const gdb_byte *, unsigned int *);
c764a876
DE
1504
1505static LONGEST read_checked_initial_length_and_offset
d521ce57 1506 (bfd *, const gdb_byte *, const struct comp_unit_head *,
c764a876 1507 unsigned int *, unsigned int *);
613e1657 1508
d521ce57
TT
1509static LONGEST read_offset (bfd *, const gdb_byte *,
1510 const struct comp_unit_head *,
c764a876
DE
1511 unsigned int *);
1512
d521ce57 1513static LONGEST read_offset_1 (bfd *, const gdb_byte *, unsigned int);
613e1657 1514
ed2dc618
SM
1515static sect_offset read_abbrev_offset
1516 (struct dwarf2_per_objfile *dwarf2_per_objfile,
1517 struct dwarf2_section_info *, sect_offset);
f4dc4d17 1518
d521ce57 1519static const gdb_byte *read_n_bytes (bfd *, const gdb_byte *, unsigned int);
c906108c 1520
d521ce57 1521static const char *read_direct_string (bfd *, const gdb_byte *, unsigned int *);
c906108c 1522
ed2dc618
SM
1523static const char *read_indirect_string
1524 (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *, const gdb_byte *,
1525 const struct comp_unit_head *, unsigned int *);
4bdf3d34 1526
ed2dc618
SM
1527static const char *read_indirect_line_string
1528 (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *, const gdb_byte *,
1529 const struct comp_unit_head *, unsigned int *);
36586728 1530
ed2dc618
SM
1531static const char *read_indirect_string_at_offset
1532 (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *abfd,
1533 LONGEST str_offset);
927aa2e7 1534
ed2dc618
SM
1535static const char *read_indirect_string_from_dwz
1536 (struct objfile *objfile, struct dwz_file *, LONGEST);
c906108c 1537
d521ce57 1538static LONGEST read_signed_leb128 (bfd *, const gdb_byte *, unsigned int *);
c906108c 1539
d521ce57
TT
1540static CORE_ADDR read_addr_index_from_leb128 (struct dwarf2_cu *,
1541 const gdb_byte *,
3019eac3
DE
1542 unsigned int *);
1543
d521ce57 1544static const char *read_str_index (const struct die_reader_specs *reader,
342587c4 1545 ULONGEST str_index);
3019eac3 1546
e142c38c 1547static void set_cu_language (unsigned int, struct dwarf2_cu *);
c906108c 1548
e142c38c
DJ
1549static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
1550 struct dwarf2_cu *);
c906108c 1551
348e048f 1552static struct attribute *dwarf2_attr_no_follow (struct die_info *,
45e58e77 1553 unsigned int);
348e048f 1554
7d45c7c3
KB
1555static const char *dwarf2_string_attr (struct die_info *die, unsigned int name,
1556 struct dwarf2_cu *cu);
1557
05cf31d1
JB
1558static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
1559 struct dwarf2_cu *cu);
1560
e142c38c 1561static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
3ca72b44 1562
e142c38c 1563static struct die_info *die_specification (struct die_info *die,
f2f0e013 1564 struct dwarf2_cu **);
63d06c5c 1565
9c541725 1566static line_header_up dwarf_decode_line_header (sect_offset sect_off,
fff8551c 1567 struct dwarf2_cu *cu);
debd256d 1568
f3f5162e 1569static void dwarf_decode_lines (struct line_header *, const char *,
c3b7b696 1570 struct dwarf2_cu *, struct partial_symtab *,
527f3840 1571 CORE_ADDR, int decode_mapping);
c906108c 1572
4d663531 1573static void dwarf2_start_subfile (const char *, const char *);
c906108c 1574
43f3e411
DE
1575static struct compunit_symtab *dwarf2_start_symtab (struct dwarf2_cu *,
1576 const char *, const char *,
1577 CORE_ADDR);
f4dc4d17 1578
a14ed312 1579static struct symbol *new_symbol (struct die_info *, struct type *,
5e2db402 1580 struct dwarf2_cu *, struct symbol * = NULL);
34eaf542 1581
ff39bb5e 1582static void dwarf2_const_value (const struct attribute *, struct symbol *,
e7c27a73 1583 struct dwarf2_cu *);
c906108c 1584
ff39bb5e 1585static void dwarf2_const_value_attr (const struct attribute *attr,
98bfdba5
PA
1586 struct type *type,
1587 const char *name,
1588 struct obstack *obstack,
12df843f 1589 struct dwarf2_cu *cu, LONGEST *value,
d521ce57 1590 const gdb_byte **bytes,
98bfdba5 1591 struct dwarf2_locexpr_baton **baton);
2df3850c 1592
e7c27a73 1593static struct type *die_type (struct die_info *, struct dwarf2_cu *);
c906108c 1594
b4ba55a1
JB
1595static int need_gnat_info (struct dwarf2_cu *);
1596
3e43a32a
MS
1597static struct type *die_descriptive_type (struct die_info *,
1598 struct dwarf2_cu *);
b4ba55a1
JB
1599
1600static void set_descriptive_type (struct type *, struct die_info *,
1601 struct dwarf2_cu *);
1602
e7c27a73
DJ
1603static struct type *die_containing_type (struct die_info *,
1604 struct dwarf2_cu *);
c906108c 1605
ff39bb5e 1606static struct type *lookup_die_type (struct die_info *, const struct attribute *,
673bfd45 1607 struct dwarf2_cu *);
c906108c 1608
f792889a 1609static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
c906108c 1610
673bfd45
DE
1611static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1612
0d5cff50 1613static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
63d06c5c 1614
6e70227d 1615static char *typename_concat (struct obstack *obs, const char *prefix,
f55ee35c
JK
1616 const char *suffix, int physname,
1617 struct dwarf2_cu *cu);
63d06c5c 1618
e7c27a73 1619static void read_file_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1620
348e048f
DE
1621static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1622
e7c27a73 1623static void read_func_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1624
e7c27a73 1625static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1626
96408a79
SA
1627static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1628
71a3c369
TT
1629static void read_variable (struct die_info *die, struct dwarf2_cu *cu);
1630
ff013f42
JK
1631static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
1632 struct dwarf2_cu *, struct partial_symtab *);
1633
3a2b436a 1634/* How dwarf2_get_pc_bounds constructed its *LOWPC and *HIGHPC return
e385593e 1635 values. Keep the items ordered with increasing constraints compliance. */
3a2b436a
JK
1636enum pc_bounds_kind
1637{
e385593e 1638 /* No attribute DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges was found. */
3a2b436a
JK
1639 PC_BOUNDS_NOT_PRESENT,
1640
e385593e
JK
1641 /* Some of the attributes DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges
1642 were present but they do not form a valid range of PC addresses. */
1643 PC_BOUNDS_INVALID,
1644
3a2b436a
JK
1645 /* Discontiguous range was found - that is DW_AT_ranges was found. */
1646 PC_BOUNDS_RANGES,
1647
1648 /* Contiguous range was found - DW_AT_low_pc and DW_AT_high_pc were found. */
1649 PC_BOUNDS_HIGH_LOW,
1650};
1651
1652static enum pc_bounds_kind dwarf2_get_pc_bounds (struct die_info *,
1653 CORE_ADDR *, CORE_ADDR *,
1654 struct dwarf2_cu *,
1655 struct partial_symtab *);
c906108c 1656
fae299cd
DC
1657static void get_scope_pc_bounds (struct die_info *,
1658 CORE_ADDR *, CORE_ADDR *,
1659 struct dwarf2_cu *);
1660
801e3a5b
JB
1661static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1662 CORE_ADDR, struct dwarf2_cu *);
1663
a14ed312 1664static void dwarf2_add_field (struct field_info *, struct die_info *,
e7c27a73 1665 struct dwarf2_cu *);
c906108c 1666
a14ed312 1667static void dwarf2_attach_fields_to_type (struct field_info *,
e7c27a73 1668 struct type *, struct dwarf2_cu *);
c906108c 1669
a14ed312 1670static void dwarf2_add_member_fn (struct field_info *,
e26fb1d7 1671 struct die_info *, struct type *,
e7c27a73 1672 struct dwarf2_cu *);
c906108c 1673
a14ed312 1674static void dwarf2_attach_fn_fields_to_type (struct field_info *,
3e43a32a
MS
1675 struct type *,
1676 struct dwarf2_cu *);
c906108c 1677
134d01f1 1678static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1679
e7c27a73 1680static void read_common_block (struct die_info *, struct dwarf2_cu *);
c906108c 1681
e7c27a73 1682static void read_namespace (struct die_info *die, struct dwarf2_cu *);
d9fa45fe 1683
5d7cb8df
JK
1684static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1685
22cee43f
PMR
1686static struct using_direct **using_directives (enum language);
1687
27aa8d6a
SW
1688static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1689
74921315
KS
1690static int read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu);
1691
f55ee35c
JK
1692static struct type *read_module_type (struct die_info *die,
1693 struct dwarf2_cu *cu);
1694
38d518c9 1695static const char *namespace_name (struct die_info *die,
e142c38c 1696 int *is_anonymous, struct dwarf2_cu *);
38d518c9 1697
134d01f1 1698static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1699
e7c27a73 1700static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *);
c906108c 1701
6e70227d 1702static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
7ca2d3a3
DL
1703 struct dwarf2_cu *);
1704
bf6af496 1705static struct die_info *read_die_and_siblings_1
d521ce57 1706 (const struct die_reader_specs *, const gdb_byte *, const gdb_byte **,
bf6af496 1707 struct die_info *);
639d11d3 1708
dee91e82 1709static struct die_info *read_die_and_siblings (const struct die_reader_specs *,
d521ce57
TT
1710 const gdb_byte *info_ptr,
1711 const gdb_byte **new_info_ptr,
639d11d3
DC
1712 struct die_info *parent);
1713
d521ce57
TT
1714static const gdb_byte *read_full_die_1 (const struct die_reader_specs *,
1715 struct die_info **, const gdb_byte *,
1716 int *, int);
3019eac3 1717
d521ce57
TT
1718static const gdb_byte *read_full_die (const struct die_reader_specs *,
1719 struct die_info **, const gdb_byte *,
1720 int *);
93311388 1721
e7c27a73 1722static void process_die (struct die_info *, struct dwarf2_cu *);
c906108c 1723
15d034d0
TT
1724static const char *dwarf2_canonicalize_name (const char *, struct dwarf2_cu *,
1725 struct obstack *);
71c25dea 1726
15d034d0 1727static const char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
9219021c 1728
15d034d0 1729static const char *dwarf2_full_name (const char *name,
98bfdba5
PA
1730 struct die_info *die,
1731 struct dwarf2_cu *cu);
1732
ca69b9e6
DE
1733static const char *dwarf2_physname (const char *name, struct die_info *die,
1734 struct dwarf2_cu *cu);
1735
e142c38c 1736static struct die_info *dwarf2_extension (struct die_info *die,
f2f0e013 1737 struct dwarf2_cu **);
9219021c 1738
f39c6ffd 1739static const char *dwarf_tag_name (unsigned int);
c906108c 1740
f39c6ffd 1741static const char *dwarf_attr_name (unsigned int);
c906108c 1742
f39c6ffd 1743static const char *dwarf_form_name (unsigned int);
c906108c 1744
a121b7c1 1745static const char *dwarf_bool_name (unsigned int);
c906108c 1746
f39c6ffd 1747static const char *dwarf_type_encoding_name (unsigned int);
c906108c 1748
f9aca02d 1749static struct die_info *sibling_die (struct die_info *);
c906108c 1750
d97bc12b
DE
1751static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1752
1753static void dump_die_for_error (struct die_info *);
1754
1755static void dump_die_1 (struct ui_file *, int level, int max_level,
1756 struct die_info *);
c906108c 1757
d97bc12b 1758/*static*/ void dump_die (struct die_info *, int max_level);
c906108c 1759
51545339 1760static void store_in_ref_table (struct die_info *,
10b3939b 1761 struct dwarf2_cu *);
c906108c 1762
ff39bb5e 1763static sect_offset dwarf2_get_ref_die_offset (const struct attribute *);
c906108c 1764
ff39bb5e 1765static LONGEST dwarf2_get_attr_constant_value (const struct attribute *, int);
a02abb62 1766
348e048f 1767static struct die_info *follow_die_ref_or_sig (struct die_info *,
ff39bb5e 1768 const struct attribute *,
348e048f
DE
1769 struct dwarf2_cu **);
1770
10b3939b 1771static struct die_info *follow_die_ref (struct die_info *,
ff39bb5e 1772 const struct attribute *,
f2f0e013 1773 struct dwarf2_cu **);
c906108c 1774
348e048f 1775static struct die_info *follow_die_sig (struct die_info *,
ff39bb5e 1776 const struct attribute *,
348e048f
DE
1777 struct dwarf2_cu **);
1778
ac9ec31b
DE
1779static struct type *get_signatured_type (struct die_info *, ULONGEST,
1780 struct dwarf2_cu *);
1781
1782static struct type *get_DW_AT_signature_type (struct die_info *,
ff39bb5e 1783 const struct attribute *,
ac9ec31b
DE
1784 struct dwarf2_cu *);
1785
e5fe5e75 1786static void load_full_type_unit (struct dwarf2_per_cu_data *per_cu);
348e048f 1787
52dc124a 1788static void read_signatured_type (struct signatured_type *);
348e048f 1789
63e43d3a
PMR
1790static int attr_to_dynamic_prop (const struct attribute *attr,
1791 struct die_info *die, struct dwarf2_cu *cu,
1792 struct dynamic_prop *prop);
1793
c906108c
SS
1794/* memory allocation interface */
1795
7b5a2f43 1796static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
c906108c 1797
b60c80d6 1798static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
c906108c 1799
43f3e411 1800static void dwarf_decode_macros (struct dwarf2_cu *, unsigned int, int);
2e276125 1801
6e5a29e1 1802static int attr_form_is_block (const struct attribute *);
8e19ed76 1803
6e5a29e1 1804static int attr_form_is_section_offset (const struct attribute *);
3690dd37 1805
6e5a29e1 1806static int attr_form_is_constant (const struct attribute *);
3690dd37 1807
6e5a29e1 1808static int attr_form_is_ref (const struct attribute *);
7771576e 1809
8cf6f0b1
TT
1810static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1811 struct dwarf2_loclist_baton *baton,
ff39bb5e 1812 const struct attribute *attr);
8cf6f0b1 1813
ff39bb5e 1814static void dwarf2_symbol_mark_computed (const struct attribute *attr,
93e7bd98 1815 struct symbol *sym,
f1e6e072
TT
1816 struct dwarf2_cu *cu,
1817 int is_block);
4c2df51b 1818
d521ce57
TT
1819static const gdb_byte *skip_one_die (const struct die_reader_specs *reader,
1820 const gdb_byte *info_ptr,
1821 struct abbrev_info *abbrev);
4bb7a0a7 1822
72bf9492
DJ
1823static hashval_t partial_die_hash (const void *item);
1824
1825static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1826
ae038cb0 1827static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
ed2dc618
SM
1828 (sect_offset sect_off, unsigned int offset_in_dwz,
1829 struct dwarf2_per_objfile *dwarf2_per_objfile);
ae038cb0 1830
9816fde3 1831static void prepare_one_comp_unit (struct dwarf2_cu *cu,
95554aad
TT
1832 struct die_info *comp_unit_die,
1833 enum language pretend_language);
93311388 1834
ed2dc618 1835static void age_cached_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
ae038cb0 1836
dee91e82 1837static void free_one_cached_comp_unit (struct dwarf2_per_cu_data *);
ae038cb0 1838
f792889a
DJ
1839static struct type *set_die_type (struct die_info *, struct type *,
1840 struct dwarf2_cu *);
1c379e20 1841
ed2dc618 1842static void create_all_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
ae038cb0 1843
ed2dc618 1844static int create_all_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
1fd400ff 1845
58f0c718 1846static void load_full_comp_unit (struct dwarf2_per_cu_data *, bool,
95554aad 1847 enum language);
10b3939b 1848
95554aad
TT
1849static void process_full_comp_unit (struct dwarf2_per_cu_data *,
1850 enum language);
10b3939b 1851
f4dc4d17
DE
1852static void process_full_type_unit (struct dwarf2_per_cu_data *,
1853 enum language);
1854
10b3939b
DJ
1855static void dwarf2_add_dependence (struct dwarf2_cu *,
1856 struct dwarf2_per_cu_data *);
1857
ae038cb0
DJ
1858static void dwarf2_mark (struct dwarf2_cu *);
1859
1860static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
1861
b64f50a1 1862static struct type *get_die_type_at_offset (sect_offset,
ac9ec31b 1863 struct dwarf2_per_cu_data *);
673bfd45 1864
f792889a 1865static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
72019c9c 1866
95554aad
TT
1867static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
1868 enum language pretend_language);
1869
ed2dc618 1870static void process_queue (struct dwarf2_per_objfile *dwarf2_per_objfile);
9291a0cd 1871
b303c6f6
AB
1872/* Class, the destructor of which frees all allocated queue entries. This
1873 will only have work to do if an error was thrown while processing the
1874 dwarf. If no error was thrown then the queue entries should have all
1875 been processed, and freed, as we went along. */
1876
1877class dwarf2_queue_guard
1878{
1879public:
1880 dwarf2_queue_guard () = default;
1881
1882 /* Free any entries remaining on the queue. There should only be
1883 entries left if we hit an error while processing the dwarf. */
1884 ~dwarf2_queue_guard ()
1885 {
1886 struct dwarf2_queue_item *item, *last;
1887
1888 item = dwarf2_queue;
1889 while (item)
1890 {
1891 /* Anything still marked queued is likely to be in an
1892 inconsistent state, so discard it. */
1893 if (item->per_cu->queued)
1894 {
1895 if (item->per_cu->cu != NULL)
1896 free_one_cached_comp_unit (item->per_cu);
1897 item->per_cu->queued = 0;
1898 }
1899
1900 last = item;
1901 item = item->next;
1902 xfree (last);
1903 }
1904
1905 dwarf2_queue = dwarf2_queue_tail = NULL;
1906 }
1907};
1908
d721ba37
PA
1909/* The return type of find_file_and_directory. Note, the enclosed
1910 string pointers are only valid while this object is valid. */
1911
1912struct file_and_directory
1913{
1914 /* The filename. This is never NULL. */
1915 const char *name;
1916
1917 /* The compilation directory. NULL if not known. If we needed to
1918 compute a new string, this points to COMP_DIR_STORAGE, otherwise,
1919 points directly to the DW_AT_comp_dir string attribute owned by
1920 the obstack that owns the DIE. */
1921 const char *comp_dir;
1922
1923 /* If we needed to build a new string for comp_dir, this is what
1924 owns the storage. */
1925 std::string comp_dir_storage;
1926};
1927
1928static file_and_directory find_file_and_directory (struct die_info *die,
1929 struct dwarf2_cu *cu);
9291a0cd
TT
1930
1931static char *file_full_name (int file, struct line_header *lh,
1932 const char *comp_dir);
1933
43988095
JK
1934/* Expected enum dwarf_unit_type for read_comp_unit_head. */
1935enum class rcuh_kind { COMPILE, TYPE };
1936
d521ce57 1937static const gdb_byte *read_and_check_comp_unit_head
ed2dc618
SM
1938 (struct dwarf2_per_objfile* dwarf2_per_objfile,
1939 struct comp_unit_head *header,
36586728 1940 struct dwarf2_section_info *section,
d521ce57 1941 struct dwarf2_section_info *abbrev_section, const gdb_byte *info_ptr,
43988095 1942 rcuh_kind section_kind);
36586728 1943
fd820528 1944static void init_cutu_and_read_dies
f4dc4d17 1945 (struct dwarf2_per_cu_data *this_cu, struct abbrev_table *abbrev_table,
58f0c718 1946 int use_existing_cu, int keep, bool skip_partial,
3019eac3
DE
1947 die_reader_func_ftype *die_reader_func, void *data);
1948
dee91e82
DE
1949static void init_cutu_and_read_dies_simple
1950 (struct dwarf2_per_cu_data *this_cu,
1951 die_reader_func_ftype *die_reader_func, void *data);
9291a0cd 1952
673bfd45 1953static htab_t allocate_signatured_type_table (struct objfile *objfile);
1fd400ff 1954
3019eac3
DE
1955static htab_t allocate_dwo_unit_table (struct objfile *objfile);
1956
57d63ce2 1957static struct dwo_unit *lookup_dwo_unit_in_dwp
ed2dc618
SM
1958 (struct dwarf2_per_objfile *dwarf2_per_objfile,
1959 struct dwp_file *dwp_file, const char *comp_dir,
57d63ce2 1960 ULONGEST signature, int is_debug_types);
a2ce51a0 1961
ed2dc618
SM
1962static struct dwp_file *get_dwp_file
1963 (struct dwarf2_per_objfile *dwarf2_per_objfile);
a2ce51a0 1964
3019eac3 1965static struct dwo_unit *lookup_dwo_comp_unit
a1855c1d 1966 (struct dwarf2_per_cu_data *, const char *, const char *, ULONGEST);
3019eac3
DE
1967
1968static struct dwo_unit *lookup_dwo_type_unit
a1855c1d 1969 (struct signatured_type *, const char *, const char *);
3019eac3 1970
89e63ee4
DE
1971static void queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *);
1972
263db9a1 1973static void free_dwo_file (struct dwo_file *);
3019eac3 1974
263db9a1
TT
1975/* A unique_ptr helper to free a dwo_file. */
1976
1977struct dwo_file_deleter
ed2dc618 1978{
263db9a1
TT
1979 void operator() (struct dwo_file *df) const
1980 {
1981 free_dwo_file (df);
1982 }
ed2dc618
SM
1983};
1984
263db9a1
TT
1985/* A unique pointer to a dwo_file. */
1986
1987typedef std::unique_ptr<struct dwo_file, dwo_file_deleter> dwo_file_up;
1988
ed2dc618 1989static void process_cu_includes (struct dwarf2_per_objfile *dwarf2_per_objfile);
95554aad 1990
1b80a9fa 1991static void check_producer (struct dwarf2_cu *cu);
527f3840
JK
1992
1993static void free_line_header_voidp (void *arg);
4390d890
DE
1994\f
1995/* Various complaints about symbol reading that don't abort the process. */
1996
1997static void
1998dwarf2_statement_list_fits_in_line_number_section_complaint (void)
1999{
b98664d3 2000 complaint (_("statement list doesn't fit in .debug_line section"));
4390d890
DE
2001}
2002
2003static void
2004dwarf2_debug_line_missing_file_complaint (void)
2005{
b98664d3 2006 complaint (_(".debug_line section has line data without a file"));
4390d890
DE
2007}
2008
2009static void
2010dwarf2_debug_line_missing_end_sequence_complaint (void)
2011{
b98664d3 2012 complaint (_(".debug_line section has line "
4390d890
DE
2013 "program sequence without an end"));
2014}
2015
2016static void
2017dwarf2_complex_location_expr_complaint (void)
2018{
b98664d3 2019 complaint (_("location expression too complex"));
4390d890
DE
2020}
2021
2022static void
2023dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
2024 int arg3)
2025{
b98664d3 2026 complaint (_("const value length mismatch for '%s', got %d, expected %d"),
4390d890
DE
2027 arg1, arg2, arg3);
2028}
2029
2030static void
2031dwarf2_section_buffer_overflow_complaint (struct dwarf2_section_info *section)
2032{
b98664d3 2033 complaint (_("debug info runs off end of %s section"
4390d890 2034 " [in module %s]"),
a32a8923
DE
2035 get_section_name (section),
2036 get_section_file_name (section));
4390d890 2037}
1b80a9fa 2038
4390d890
DE
2039static void
2040dwarf2_macro_malformed_definition_complaint (const char *arg1)
2041{
b98664d3 2042 complaint (_("macro debug info contains a "
4390d890
DE
2043 "malformed macro definition:\n`%s'"),
2044 arg1);
2045}
2046
2047static void
2048dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
2049{
b98664d3 2050 complaint (_("invalid attribute class or form for '%s' in '%s'"),
4390d890
DE
2051 arg1, arg2);
2052}
527f3840
JK
2053
2054/* Hash function for line_header_hash. */
2055
2056static hashval_t
2057line_header_hash (const struct line_header *ofs)
2058{
9c541725 2059 return to_underlying (ofs->sect_off) ^ ofs->offset_in_dwz;
527f3840
JK
2060}
2061
2062/* Hash function for htab_create_alloc_ex for line_header_hash. */
2063
2064static hashval_t
2065line_header_hash_voidp (const void *item)
2066{
9a3c8263 2067 const struct line_header *ofs = (const struct line_header *) item;
527f3840
JK
2068
2069 return line_header_hash (ofs);
2070}
2071
2072/* Equality function for line_header_hash. */
2073
2074static int
2075line_header_eq_voidp (const void *item_lhs, const void *item_rhs)
2076{
9a3c8263
SM
2077 const struct line_header *ofs_lhs = (const struct line_header *) item_lhs;
2078 const struct line_header *ofs_rhs = (const struct line_header *) item_rhs;
527f3840 2079
9c541725 2080 return (ofs_lhs->sect_off == ofs_rhs->sect_off
527f3840
JK
2081 && ofs_lhs->offset_in_dwz == ofs_rhs->offset_in_dwz);
2082}
2083
4390d890 2084\f
9291a0cd 2085
31aa7e4e
JB
2086/* Read the given attribute value as an address, taking the attribute's
2087 form into account. */
2088
2089static CORE_ADDR
2090attr_value_as_address (struct attribute *attr)
2091{
2092 CORE_ADDR addr;
2093
2094 if (attr->form != DW_FORM_addr && attr->form != DW_FORM_GNU_addr_index)
2095 {
2096 /* Aside from a few clearly defined exceptions, attributes that
2097 contain an address must always be in DW_FORM_addr form.
2098 Unfortunately, some compilers happen to be violating this
2099 requirement by encoding addresses using other forms, such
2100 as DW_FORM_data4 for example. For those broken compilers,
2101 we try to do our best, without any guarantee of success,
2102 to interpret the address correctly. It would also be nice
2103 to generate a complaint, but that would require us to maintain
2104 a list of legitimate cases where a non-address form is allowed,
2105 as well as update callers to pass in at least the CU's DWARF
2106 version. This is more overhead than what we're willing to
2107 expand for a pretty rare case. */
2108 addr = DW_UNSND (attr);
2109 }
2110 else
2111 addr = DW_ADDR (attr);
2112
2113 return addr;
2114}
2115
330cdd98
PA
2116/* See declaration. */
2117
2118dwarf2_per_objfile::dwarf2_per_objfile (struct objfile *objfile_,
2119 const dwarf2_debug_sections *names)
2120 : objfile (objfile_)
2121{
2122 if (names == NULL)
2123 names = &dwarf2_elf_names;
2124
2125 bfd *obfd = objfile->obfd;
2126
2127 for (asection *sec = obfd->sections; sec != NULL; sec = sec->next)
2128 locate_sections (obfd, sec, *names);
2129}
2130
fc8e7e75
SM
2131static void free_dwo_files (htab_t dwo_files, struct objfile *objfile);
2132
330cdd98
PA
2133dwarf2_per_objfile::~dwarf2_per_objfile ()
2134{
2135 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
2136 free_cached_comp_units ();
2137
2138 if (quick_file_names_table)
2139 htab_delete (quick_file_names_table);
2140
2141 if (line_header_hash)
2142 htab_delete (line_header_hash);
2143
b76e467d
SM
2144 for (dwarf2_per_cu_data *per_cu : all_comp_units)
2145 VEC_free (dwarf2_per_cu_ptr, per_cu->imported_symtabs);
fc8e7e75 2146
b2bdb8cf
SM
2147 for (signatured_type *sig_type : all_type_units)
2148 VEC_free (dwarf2_per_cu_ptr, sig_type->per_cu.imported_symtabs);
fc8e7e75
SM
2149
2150 VEC_free (dwarf2_section_info_def, types);
2151
2152 if (dwo_files != NULL)
2153 free_dwo_files (dwo_files, objfile);
fc8e7e75 2154
330cdd98
PA
2155 /* Everything else should be on the objfile obstack. */
2156}
2157
2158/* See declaration. */
2159
2160void
2161dwarf2_per_objfile::free_cached_comp_units ()
2162{
2163 dwarf2_per_cu_data *per_cu = read_in_chain;
2164 dwarf2_per_cu_data **last_chain = &read_in_chain;
2165 while (per_cu != NULL)
2166 {
2167 dwarf2_per_cu_data *next_cu = per_cu->cu->read_in_chain;
2168
fcd3b13d 2169 delete per_cu->cu;
330cdd98
PA
2170 *last_chain = next_cu;
2171 per_cu = next_cu;
2172 }
2173}
2174
11ed8cad
TT
2175/* A helper class that calls free_cached_comp_units on
2176 destruction. */
2177
2178class free_cached_comp_units
2179{
2180public:
2181
2182 explicit free_cached_comp_units (dwarf2_per_objfile *per_objfile)
2183 : m_per_objfile (per_objfile)
2184 {
2185 }
2186
2187 ~free_cached_comp_units ()
2188 {
2189 m_per_objfile->free_cached_comp_units ();
2190 }
2191
2192 DISABLE_COPY_AND_ASSIGN (free_cached_comp_units);
2193
2194private:
2195
2196 dwarf2_per_objfile *m_per_objfile;
2197};
2198
c906108c 2199/* Try to locate the sections we need for DWARF 2 debugging
251d32d9
TG
2200 information and return true if we have enough to do something.
2201 NAMES points to the dwarf2 section names, or is NULL if the standard
2202 ELF names are used. */
c906108c
SS
2203
2204int
251d32d9
TG
2205dwarf2_has_info (struct objfile *objfile,
2206 const struct dwarf2_debug_sections *names)
c906108c 2207{
97cbe998
SDJ
2208 if (objfile->flags & OBJF_READNEVER)
2209 return 0;
2210
ed2dc618
SM
2211 struct dwarf2_per_objfile *dwarf2_per_objfile
2212 = get_dwarf2_per_objfile (objfile);
2213
2214 if (dwarf2_per_objfile == NULL)
be391dca
TT
2215 {
2216 /* Initialize per-objfile state. */
fd90ace4
YQ
2217 dwarf2_per_objfile
2218 = new (&objfile->objfile_obstack) struct dwarf2_per_objfile (objfile,
2219 names);
ed2dc618 2220 set_dwarf2_per_objfile (objfile, dwarf2_per_objfile);
be391dca 2221 }
73869dc2 2222 return (!dwarf2_per_objfile->info.is_virtual
049412e3 2223 && dwarf2_per_objfile->info.s.section != NULL
73869dc2 2224 && !dwarf2_per_objfile->abbrev.is_virtual
049412e3 2225 && dwarf2_per_objfile->abbrev.s.section != NULL);
73869dc2
DE
2226}
2227
2228/* Return the containing section of virtual section SECTION. */
2229
2230static struct dwarf2_section_info *
2231get_containing_section (const struct dwarf2_section_info *section)
2232{
2233 gdb_assert (section->is_virtual);
2234 return section->s.containing_section;
c906108c
SS
2235}
2236
a32a8923
DE
2237/* Return the bfd owner of SECTION. */
2238
2239static struct bfd *
2240get_section_bfd_owner (const struct dwarf2_section_info *section)
2241{
73869dc2
DE
2242 if (section->is_virtual)
2243 {
2244 section = get_containing_section (section);
2245 gdb_assert (!section->is_virtual);
2246 }
049412e3 2247 return section->s.section->owner;
a32a8923
DE
2248}
2249
2250/* Return the bfd section of SECTION.
2251 Returns NULL if the section is not present. */
2252
2253static asection *
2254get_section_bfd_section (const struct dwarf2_section_info *section)
2255{
73869dc2
DE
2256 if (section->is_virtual)
2257 {
2258 section = get_containing_section (section);
2259 gdb_assert (!section->is_virtual);
2260 }
049412e3 2261 return section->s.section;
a32a8923
DE
2262}
2263
2264/* Return the name of SECTION. */
2265
2266static const char *
2267get_section_name (const struct dwarf2_section_info *section)
2268{
2269 asection *sectp = get_section_bfd_section (section);
2270
2271 gdb_assert (sectp != NULL);
2272 return bfd_section_name (get_section_bfd_owner (section), sectp);
2273}
2274
2275/* Return the name of the file SECTION is in. */
2276
2277static const char *
2278get_section_file_name (const struct dwarf2_section_info *section)
2279{
2280 bfd *abfd = get_section_bfd_owner (section);
2281
2282 return bfd_get_filename (abfd);
2283}
2284
2285/* Return the id of SECTION.
2286 Returns 0 if SECTION doesn't exist. */
2287
2288static int
2289get_section_id (const struct dwarf2_section_info *section)
2290{
2291 asection *sectp = get_section_bfd_section (section);
2292
2293 if (sectp == NULL)
2294 return 0;
2295 return sectp->id;
2296}
2297
2298/* Return the flags of SECTION.
73869dc2 2299 SECTION (or containing section if this is a virtual section) must exist. */
a32a8923
DE
2300
2301static int
2302get_section_flags (const struct dwarf2_section_info *section)
2303{
2304 asection *sectp = get_section_bfd_section (section);
2305
2306 gdb_assert (sectp != NULL);
2307 return bfd_get_section_flags (sectp->owner, sectp);
2308}
2309
251d32d9
TG
2310/* When loading sections, we look either for uncompressed section or for
2311 compressed section names. */
233a11ab
CS
2312
2313static int
251d32d9
TG
2314section_is_p (const char *section_name,
2315 const struct dwarf2_section_names *names)
233a11ab 2316{
251d32d9
TG
2317 if (names->normal != NULL
2318 && strcmp (section_name, names->normal) == 0)
2319 return 1;
2320 if (names->compressed != NULL
2321 && strcmp (section_name, names->compressed) == 0)
2322 return 1;
2323 return 0;
233a11ab
CS
2324}
2325
330cdd98 2326/* See declaration. */
c906108c 2327
330cdd98
PA
2328void
2329dwarf2_per_objfile::locate_sections (bfd *abfd, asection *sectp,
2330 const dwarf2_debug_sections &names)
c906108c 2331{
dc7650b8 2332 flagword aflag = bfd_get_section_flags (abfd, sectp);
251d32d9 2333
dc7650b8
JK
2334 if ((aflag & SEC_HAS_CONTENTS) == 0)
2335 {
2336 }
330cdd98 2337 else if (section_is_p (sectp->name, &names.info))
c906108c 2338 {
330cdd98
PA
2339 this->info.s.section = sectp;
2340 this->info.size = bfd_get_section_size (sectp);
c906108c 2341 }
330cdd98 2342 else if (section_is_p (sectp->name, &names.abbrev))
c906108c 2343 {
330cdd98
PA
2344 this->abbrev.s.section = sectp;
2345 this->abbrev.size = bfd_get_section_size (sectp);
c906108c 2346 }
330cdd98 2347 else if (section_is_p (sectp->name, &names.line))
c906108c 2348 {
330cdd98
PA
2349 this->line.s.section = sectp;
2350 this->line.size = bfd_get_section_size (sectp);
c906108c 2351 }
330cdd98 2352 else if (section_is_p (sectp->name, &names.loc))
c906108c 2353 {
330cdd98
PA
2354 this->loc.s.section = sectp;
2355 this->loc.size = bfd_get_section_size (sectp);
c906108c 2356 }
330cdd98 2357 else if (section_is_p (sectp->name, &names.loclists))
43988095 2358 {
330cdd98
PA
2359 this->loclists.s.section = sectp;
2360 this->loclists.size = bfd_get_section_size (sectp);
43988095 2361 }
330cdd98 2362 else if (section_is_p (sectp->name, &names.macinfo))
c906108c 2363 {
330cdd98
PA
2364 this->macinfo.s.section = sectp;
2365 this->macinfo.size = bfd_get_section_size (sectp);
c906108c 2366 }
330cdd98 2367 else if (section_is_p (sectp->name, &names.macro))
cf2c3c16 2368 {
330cdd98
PA
2369 this->macro.s.section = sectp;
2370 this->macro.size = bfd_get_section_size (sectp);
cf2c3c16 2371 }
330cdd98 2372 else if (section_is_p (sectp->name, &names.str))
c906108c 2373 {
330cdd98
PA
2374 this->str.s.section = sectp;
2375 this->str.size = bfd_get_section_size (sectp);
c906108c 2376 }
330cdd98 2377 else if (section_is_p (sectp->name, &names.line_str))
43988095 2378 {
330cdd98
PA
2379 this->line_str.s.section = sectp;
2380 this->line_str.size = bfd_get_section_size (sectp);
43988095 2381 }
330cdd98 2382 else if (section_is_p (sectp->name, &names.addr))
3019eac3 2383 {
330cdd98
PA
2384 this->addr.s.section = sectp;
2385 this->addr.size = bfd_get_section_size (sectp);
3019eac3 2386 }
330cdd98 2387 else if (section_is_p (sectp->name, &names.frame))
b6af0555 2388 {
330cdd98
PA
2389 this->frame.s.section = sectp;
2390 this->frame.size = bfd_get_section_size (sectp);
b6af0555 2391 }
330cdd98 2392 else if (section_is_p (sectp->name, &names.eh_frame))
b6af0555 2393 {
330cdd98
PA
2394 this->eh_frame.s.section = sectp;
2395 this->eh_frame.size = bfd_get_section_size (sectp);
b6af0555 2396 }
330cdd98 2397 else if (section_is_p (sectp->name, &names.ranges))
af34e669 2398 {
330cdd98
PA
2399 this->ranges.s.section = sectp;
2400 this->ranges.size = bfd_get_section_size (sectp);
af34e669 2401 }
330cdd98 2402 else if (section_is_p (sectp->name, &names.rnglists))
43988095 2403 {
330cdd98
PA
2404 this->rnglists.s.section = sectp;
2405 this->rnglists.size = bfd_get_section_size (sectp);
43988095 2406 }
330cdd98 2407 else if (section_is_p (sectp->name, &names.types))
348e048f 2408 {
8b70b953
TT
2409 struct dwarf2_section_info type_section;
2410
2411 memset (&type_section, 0, sizeof (type_section));
049412e3 2412 type_section.s.section = sectp;
8b70b953
TT
2413 type_section.size = bfd_get_section_size (sectp);
2414
330cdd98 2415 VEC_safe_push (dwarf2_section_info_def, this->types,
8b70b953 2416 &type_section);
348e048f 2417 }
330cdd98 2418 else if (section_is_p (sectp->name, &names.gdb_index))
9291a0cd 2419 {
330cdd98
PA
2420 this->gdb_index.s.section = sectp;
2421 this->gdb_index.size = bfd_get_section_size (sectp);
9291a0cd 2422 }
927aa2e7
JK
2423 else if (section_is_p (sectp->name, &names.debug_names))
2424 {
2425 this->debug_names.s.section = sectp;
2426 this->debug_names.size = bfd_get_section_size (sectp);
2427 }
2428 else if (section_is_p (sectp->name, &names.debug_aranges))
2429 {
2430 this->debug_aranges.s.section = sectp;
2431 this->debug_aranges.size = bfd_get_section_size (sectp);
2432 }
dce234bc 2433
b4e1fd61 2434 if ((bfd_get_section_flags (abfd, sectp) & (SEC_LOAD | SEC_ALLOC))
72dca2f5 2435 && bfd_section_vma (abfd, sectp) == 0)
330cdd98 2436 this->has_section_at_zero = true;
c906108c
SS
2437}
2438
fceca515
DE
2439/* A helper function that decides whether a section is empty,
2440 or not present. */
9e0ac564
TT
2441
2442static int
19ac8c2e 2443dwarf2_section_empty_p (const struct dwarf2_section_info *section)
9e0ac564 2444{
73869dc2
DE
2445 if (section->is_virtual)
2446 return section->size == 0;
049412e3 2447 return section->s.section == NULL || section->size == 0;
9e0ac564
TT
2448}
2449
cd4fb1b2 2450/* See dwarf2read.h. */
c906108c 2451
cd4fb1b2
SM
2452void
2453dwarf2_read_section (struct objfile *objfile, dwarf2_section_info *info)
c906108c 2454{
a32a8923 2455 asection *sectp;
3019eac3 2456 bfd *abfd;
dce234bc 2457 gdb_byte *buf, *retbuf;
c906108c 2458
be391dca
TT
2459 if (info->readin)
2460 return;
dce234bc 2461 info->buffer = NULL;
be391dca 2462 info->readin = 1;
188dd5d6 2463
9e0ac564 2464 if (dwarf2_section_empty_p (info))
dce234bc 2465 return;
c906108c 2466
a32a8923 2467 sectp = get_section_bfd_section (info);
3019eac3 2468
73869dc2
DE
2469 /* If this is a virtual section we need to read in the real one first. */
2470 if (info->is_virtual)
2471 {
2472 struct dwarf2_section_info *containing_section =
2473 get_containing_section (info);
2474
2475 gdb_assert (sectp != NULL);
2476 if ((sectp->flags & SEC_RELOC) != 0)
2477 {
2478 error (_("Dwarf Error: DWP format V2 with relocations is not"
2479 " supported in section %s [in module %s]"),
2480 get_section_name (info), get_section_file_name (info));
2481 }
2482 dwarf2_read_section (objfile, containing_section);
2483 /* Other code should have already caught virtual sections that don't
2484 fit. */
2485 gdb_assert (info->virtual_offset + info->size
2486 <= containing_section->size);
2487 /* If the real section is empty or there was a problem reading the
2488 section we shouldn't get here. */
2489 gdb_assert (containing_section->buffer != NULL);
2490 info->buffer = containing_section->buffer + info->virtual_offset;
2491 return;
2492 }
2493
4bf44c1c
TT
2494 /* If the section has relocations, we must read it ourselves.
2495 Otherwise we attach it to the BFD. */
2496 if ((sectp->flags & SEC_RELOC) == 0)
dce234bc 2497 {
d521ce57 2498 info->buffer = gdb_bfd_map_section (sectp, &info->size);
4bf44c1c 2499 return;
dce234bc 2500 }
dce234bc 2501
224c3ddb 2502 buf = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, info->size);
4bf44c1c 2503 info->buffer = buf;
dce234bc
PP
2504
2505 /* When debugging .o files, we may need to apply relocations; see
2506 http://sourceware.org/ml/gdb-patches/2002-04/msg00136.html .
2507 We never compress sections in .o files, so we only need to
2508 try this when the section is not compressed. */
ac8035ab 2509 retbuf = symfile_relocate_debug_section (objfile, sectp, buf);
dce234bc
PP
2510 if (retbuf != NULL)
2511 {
2512 info->buffer = retbuf;
2513 return;
2514 }
2515
a32a8923
DE
2516 abfd = get_section_bfd_owner (info);
2517 gdb_assert (abfd != NULL);
2518
dce234bc
PP
2519 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
2520 || bfd_bread (buf, info->size, abfd) != info->size)
19ac8c2e
DE
2521 {
2522 error (_("Dwarf Error: Can't read DWARF data"
2523 " in section %s [in module %s]"),
2524 bfd_section_name (abfd, sectp), bfd_get_filename (abfd));
2525 }
dce234bc
PP
2526}
2527
9e0ac564
TT
2528/* A helper function that returns the size of a section in a safe way.
2529 If you are positive that the section has been read before using the
2530 size, then it is safe to refer to the dwarf2_section_info object's
2531 "size" field directly. In other cases, you must call this
2532 function, because for compressed sections the size field is not set
2533 correctly until the section has been read. */
2534
2535static bfd_size_type
2536dwarf2_section_size (struct objfile *objfile,
2537 struct dwarf2_section_info *info)
2538{
2539 if (!info->readin)
2540 dwarf2_read_section (objfile, info);
2541 return info->size;
2542}
2543
dce234bc 2544/* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
0963b4bd 2545 SECTION_NAME. */
af34e669 2546
dce234bc 2547void
3017a003
TG
2548dwarf2_get_section_info (struct objfile *objfile,
2549 enum dwarf2_section_enum sect,
d521ce57 2550 asection **sectp, const gdb_byte **bufp,
dce234bc
PP
2551 bfd_size_type *sizep)
2552{
2553 struct dwarf2_per_objfile *data
9a3c8263
SM
2554 = (struct dwarf2_per_objfile *) objfile_data (objfile,
2555 dwarf2_objfile_data_key);
dce234bc 2556 struct dwarf2_section_info *info;
a3b2a86b
TT
2557
2558 /* We may see an objfile without any DWARF, in which case we just
2559 return nothing. */
2560 if (data == NULL)
2561 {
2562 *sectp = NULL;
2563 *bufp = NULL;
2564 *sizep = 0;
2565 return;
2566 }
3017a003
TG
2567 switch (sect)
2568 {
2569 case DWARF2_DEBUG_FRAME:
2570 info = &data->frame;
2571 break;
2572 case DWARF2_EH_FRAME:
2573 info = &data->eh_frame;
2574 break;
2575 default:
2576 gdb_assert_not_reached ("unexpected section");
2577 }
dce234bc 2578
9e0ac564 2579 dwarf2_read_section (objfile, info);
dce234bc 2580
a32a8923 2581 *sectp = get_section_bfd_section (info);
dce234bc
PP
2582 *bufp = info->buffer;
2583 *sizep = info->size;
2584}
2585
36586728
TT
2586/* A helper function to find the sections for a .dwz file. */
2587
2588static void
2589locate_dwz_sections (bfd *abfd, asection *sectp, void *arg)
2590{
9a3c8263 2591 struct dwz_file *dwz_file = (struct dwz_file *) arg;
36586728
TT
2592
2593 /* Note that we only support the standard ELF names, because .dwz
2594 is ELF-only (at the time of writing). */
2595 if (section_is_p (sectp->name, &dwarf2_elf_names.abbrev))
2596 {
049412e3 2597 dwz_file->abbrev.s.section = sectp;
36586728
TT
2598 dwz_file->abbrev.size = bfd_get_section_size (sectp);
2599 }
2600 else if (section_is_p (sectp->name, &dwarf2_elf_names.info))
2601 {
049412e3 2602 dwz_file->info.s.section = sectp;
36586728
TT
2603 dwz_file->info.size = bfd_get_section_size (sectp);
2604 }
2605 else if (section_is_p (sectp->name, &dwarf2_elf_names.str))
2606 {
049412e3 2607 dwz_file->str.s.section = sectp;
36586728
TT
2608 dwz_file->str.size = bfd_get_section_size (sectp);
2609 }
2610 else if (section_is_p (sectp->name, &dwarf2_elf_names.line))
2611 {
049412e3 2612 dwz_file->line.s.section = sectp;
36586728
TT
2613 dwz_file->line.size = bfd_get_section_size (sectp);
2614 }
2615 else if (section_is_p (sectp->name, &dwarf2_elf_names.macro))
2616 {
049412e3 2617 dwz_file->macro.s.section = sectp;
36586728
TT
2618 dwz_file->macro.size = bfd_get_section_size (sectp);
2619 }
2ec9a5e0
TT
2620 else if (section_is_p (sectp->name, &dwarf2_elf_names.gdb_index))
2621 {
049412e3 2622 dwz_file->gdb_index.s.section = sectp;
2ec9a5e0
TT
2623 dwz_file->gdb_index.size = bfd_get_section_size (sectp);
2624 }
927aa2e7
JK
2625 else if (section_is_p (sectp->name, &dwarf2_elf_names.debug_names))
2626 {
2627 dwz_file->debug_names.s.section = sectp;
2628 dwz_file->debug_names.size = bfd_get_section_size (sectp);
2629 }
36586728
TT
2630}
2631
4db1a1dc
TT
2632/* Open the separate '.dwz' debug file, if needed. Return NULL if
2633 there is no .gnu_debugaltlink section in the file. Error if there
2634 is such a section but the file cannot be found. */
36586728
TT
2635
2636static struct dwz_file *
ed2dc618 2637dwarf2_get_dwz_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
36586728 2638{
36586728 2639 const char *filename;
acd13123 2640 bfd_size_type buildid_len_arg;
dc294be5
TT
2641 size_t buildid_len;
2642 bfd_byte *buildid;
36586728
TT
2643
2644 if (dwarf2_per_objfile->dwz_file != NULL)
7ff8cb8c 2645 return dwarf2_per_objfile->dwz_file.get ();
36586728 2646
4db1a1dc 2647 bfd_set_error (bfd_error_no_error);
791afaa2
TT
2648 gdb::unique_xmalloc_ptr<char> data
2649 (bfd_get_alt_debug_link_info (dwarf2_per_objfile->objfile->obfd,
2650 &buildid_len_arg, &buildid));
4db1a1dc
TT
2651 if (data == NULL)
2652 {
2653 if (bfd_get_error () == bfd_error_no_error)
2654 return NULL;
2655 error (_("could not read '.gnu_debugaltlink' section: %s"),
2656 bfd_errmsg (bfd_get_error ()));
2657 }
791afaa2
TT
2658
2659 gdb::unique_xmalloc_ptr<bfd_byte> buildid_holder (buildid);
36586728 2660
acd13123
TT
2661 buildid_len = (size_t) buildid_len_arg;
2662
791afaa2 2663 filename = data.get ();
d721ba37
PA
2664
2665 std::string abs_storage;
36586728
TT
2666 if (!IS_ABSOLUTE_PATH (filename))
2667 {
14278e1f
TT
2668 gdb::unique_xmalloc_ptr<char> abs
2669 = gdb_realpath (objfile_name (dwarf2_per_objfile->objfile));
36586728 2670
14278e1f 2671 abs_storage = ldirname (abs.get ()) + SLASH_STRING + filename;
d721ba37 2672 filename = abs_storage.c_str ();
36586728
TT
2673 }
2674
dc294be5
TT
2675 /* First try the file name given in the section. If that doesn't
2676 work, try to use the build-id instead. */
192b62ce 2677 gdb_bfd_ref_ptr dwz_bfd (gdb_bfd_open (filename, gnutarget, -1));
dc294be5 2678 if (dwz_bfd != NULL)
36586728 2679 {
192b62ce
TT
2680 if (!build_id_verify (dwz_bfd.get (), buildid_len, buildid))
2681 dwz_bfd.release ();
36586728
TT
2682 }
2683
dc294be5
TT
2684 if (dwz_bfd == NULL)
2685 dwz_bfd = build_id_to_debug_bfd (buildid_len, buildid);
2686
2687 if (dwz_bfd == NULL)
2688 error (_("could not find '.gnu_debugaltlink' file for %s"),
2689 objfile_name (dwarf2_per_objfile->objfile));
2690
7ff8cb8c
TT
2691 std::unique_ptr<struct dwz_file> result
2692 (new struct dwz_file (std::move (dwz_bfd)));
36586728 2693
7ff8cb8c
TT
2694 bfd_map_over_sections (result->dwz_bfd.get (), locate_dwz_sections,
2695 result.get ());
36586728 2696
7ff8cb8c
TT
2697 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd,
2698 result->dwz_bfd.get ());
2699 dwarf2_per_objfile->dwz_file = std::move (result);
2700 return dwarf2_per_objfile->dwz_file.get ();
36586728 2701}
9291a0cd 2702\f
7b9f3c50
DE
2703/* DWARF quick_symbols_functions support. */
2704
2705/* TUs can share .debug_line entries, and there can be a lot more TUs than
2706 unique line tables, so we maintain a separate table of all .debug_line
2707 derived entries to support the sharing.
2708 All the quick functions need is the list of file names. We discard the
2709 line_header when we're done and don't need to record it here. */
2710struct quick_file_names
2711{
094b34ac
DE
2712 /* The data used to construct the hash key. */
2713 struct stmt_list_hash hash;
7b9f3c50
DE
2714
2715 /* The number of entries in file_names, real_names. */
2716 unsigned int num_file_names;
2717
2718 /* The file names from the line table, after being run through
2719 file_full_name. */
2720 const char **file_names;
2721
2722 /* The file names from the line table after being run through
2723 gdb_realpath. These are computed lazily. */
2724 const char **real_names;
2725};
2726
2727/* When using the index (and thus not using psymtabs), each CU has an
2728 object of this type. This is used to hold information needed by
2729 the various "quick" methods. */
2730struct dwarf2_per_cu_quick_data
2731{
2732 /* The file table. This can be NULL if there was no file table
2733 or it's currently not read in.
2734 NOTE: This points into dwarf2_per_objfile->quick_file_names_table. */
2735 struct quick_file_names *file_names;
2736
2737 /* The corresponding symbol table. This is NULL if symbols for this
2738 CU have not yet been read. */
43f3e411 2739 struct compunit_symtab *compunit_symtab;
7b9f3c50
DE
2740
2741 /* A temporary mark bit used when iterating over all CUs in
2742 expand_symtabs_matching. */
2743 unsigned int mark : 1;
2744
2745 /* True if we've tried to read the file table and found there isn't one.
2746 There will be no point in trying to read it again next time. */
2747 unsigned int no_file_data : 1;
2748};
2749
094b34ac
DE
2750/* Utility hash function for a stmt_list_hash. */
2751
2752static hashval_t
2753hash_stmt_list_entry (const struct stmt_list_hash *stmt_list_hash)
2754{
2755 hashval_t v = 0;
2756
2757 if (stmt_list_hash->dwo_unit != NULL)
2758 v += (uintptr_t) stmt_list_hash->dwo_unit->dwo_file;
9c541725 2759 v += to_underlying (stmt_list_hash->line_sect_off);
094b34ac
DE
2760 return v;
2761}
2762
2763/* Utility equality function for a stmt_list_hash. */
2764
2765static int
2766eq_stmt_list_entry (const struct stmt_list_hash *lhs,
2767 const struct stmt_list_hash *rhs)
2768{
2769 if ((lhs->dwo_unit != NULL) != (rhs->dwo_unit != NULL))
2770 return 0;
2771 if (lhs->dwo_unit != NULL
2772 && lhs->dwo_unit->dwo_file != rhs->dwo_unit->dwo_file)
2773 return 0;
2774
9c541725 2775 return lhs->line_sect_off == rhs->line_sect_off;
094b34ac
DE
2776}
2777
7b9f3c50
DE
2778/* Hash function for a quick_file_names. */
2779
2780static hashval_t
2781hash_file_name_entry (const void *e)
2782{
9a3c8263
SM
2783 const struct quick_file_names *file_data
2784 = (const struct quick_file_names *) e;
7b9f3c50 2785
094b34ac 2786 return hash_stmt_list_entry (&file_data->hash);
7b9f3c50
DE
2787}
2788
2789/* Equality function for a quick_file_names. */
2790
2791static int
2792eq_file_name_entry (const void *a, const void *b)
2793{
9a3c8263
SM
2794 const struct quick_file_names *ea = (const struct quick_file_names *) a;
2795 const struct quick_file_names *eb = (const struct quick_file_names *) b;
7b9f3c50 2796
094b34ac 2797 return eq_stmt_list_entry (&ea->hash, &eb->hash);
7b9f3c50
DE
2798}
2799
2800/* Delete function for a quick_file_names. */
2801
2802static void
2803delete_file_name_entry (void *e)
2804{
9a3c8263 2805 struct quick_file_names *file_data = (struct quick_file_names *) e;
7b9f3c50
DE
2806 int i;
2807
2808 for (i = 0; i < file_data->num_file_names; ++i)
2809 {
2810 xfree ((void*) file_data->file_names[i]);
2811 if (file_data->real_names)
2812 xfree ((void*) file_data->real_names[i]);
2813 }
2814
2815 /* The space for the struct itself lives on objfile_obstack,
2816 so we don't free it here. */
2817}
2818
2819/* Create a quick_file_names hash table. */
2820
2821static htab_t
2822create_quick_file_names_table (unsigned int nr_initial_entries)
2823{
2824 return htab_create_alloc (nr_initial_entries,
2825 hash_file_name_entry, eq_file_name_entry,
2826 delete_file_name_entry, xcalloc, xfree);
2827}
9291a0cd 2828
918dd910
JK
2829/* Read in PER_CU->CU. This function is unrelated to symtabs, symtab would
2830 have to be created afterwards. You should call age_cached_comp_units after
2831 processing PER_CU->CU. dw2_setup must have been already called. */
2832
2833static void
58f0c718 2834load_cu (struct dwarf2_per_cu_data *per_cu, bool skip_partial)
918dd910 2835{
3019eac3 2836 if (per_cu->is_debug_types)
e5fe5e75 2837 load_full_type_unit (per_cu);
918dd910 2838 else
58f0c718 2839 load_full_comp_unit (per_cu, skip_partial, language_minimal);
918dd910 2840
cc12ce38
DE
2841 if (per_cu->cu == NULL)
2842 return; /* Dummy CU. */
2dc860c0
DE
2843
2844 dwarf2_find_base_address (per_cu->cu->dies, per_cu->cu);
918dd910
JK
2845}
2846
a0f42c21 2847/* Read in the symbols for PER_CU. */
2fdf6df6 2848
9291a0cd 2849static void
58f0c718 2850dw2_do_instantiate_symtab (struct dwarf2_per_cu_data *per_cu, bool skip_partial)
9291a0cd 2851{
ed2dc618 2852 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
9291a0cd 2853
f4dc4d17
DE
2854 /* Skip type_unit_groups, reading the type units they contain
2855 is handled elsewhere. */
2856 if (IS_TYPE_UNIT_GROUP (per_cu))
2857 return;
2858
b303c6f6
AB
2859 /* The destructor of dwarf2_queue_guard frees any entries left on
2860 the queue. After this point we're guaranteed to leave this function
2861 with the dwarf queue empty. */
2862 dwarf2_queue_guard q_guard;
9291a0cd 2863
95554aad 2864 if (dwarf2_per_objfile->using_index
43f3e411 2865 ? per_cu->v.quick->compunit_symtab == NULL
95554aad
TT
2866 : (per_cu->v.psymtab == NULL || !per_cu->v.psymtab->readin))
2867 {
2868 queue_comp_unit (per_cu, language_minimal);
58f0c718 2869 load_cu (per_cu, skip_partial);
89e63ee4
DE
2870
2871 /* If we just loaded a CU from a DWO, and we're working with an index
2872 that may badly handle TUs, load all the TUs in that DWO as well.
2873 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
2874 if (!per_cu->is_debug_types
cc12ce38 2875 && per_cu->cu != NULL
89e63ee4
DE
2876 && per_cu->cu->dwo_unit != NULL
2877 && dwarf2_per_objfile->index_table != NULL
2878 && dwarf2_per_objfile->index_table->version <= 7
2879 /* DWP files aren't supported yet. */
ed2dc618 2880 && get_dwp_file (dwarf2_per_objfile) == NULL)
89e63ee4 2881 queue_and_load_all_dwo_tus (per_cu);
95554aad 2882 }
9291a0cd 2883
ed2dc618 2884 process_queue (dwarf2_per_objfile);
9291a0cd
TT
2885
2886 /* Age the cache, releasing compilation units that have not
2887 been used recently. */
ed2dc618 2888 age_cached_comp_units (dwarf2_per_objfile);
9291a0cd
TT
2889}
2890
2891/* Ensure that the symbols for PER_CU have been read in. OBJFILE is
2892 the objfile from which this CU came. Returns the resulting symbol
2893 table. */
2fdf6df6 2894
43f3e411 2895static struct compunit_symtab *
58f0c718 2896dw2_instantiate_symtab (struct dwarf2_per_cu_data *per_cu, bool skip_partial)
9291a0cd 2897{
ed2dc618
SM
2898 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
2899
95554aad 2900 gdb_assert (dwarf2_per_objfile->using_index);
43f3e411 2901 if (!per_cu->v.quick->compunit_symtab)
9291a0cd 2902 {
11ed8cad 2903 free_cached_comp_units freer (dwarf2_per_objfile);
c83dd867 2904 scoped_restore decrementer = increment_reading_symtab ();
58f0c718 2905 dw2_do_instantiate_symtab (per_cu, skip_partial);
ed2dc618 2906 process_cu_includes (dwarf2_per_objfile);
9291a0cd 2907 }
f194fefb 2908
43f3e411 2909 return per_cu->v.quick->compunit_symtab;
9291a0cd
TT
2910}
2911
ff4c9fec 2912/* See declaration. */
f4dc4d17 2913
ff4c9fec
SM
2914dwarf2_per_cu_data *
2915dwarf2_per_objfile::get_cutu (int index)
2916{
b76e467d 2917 if (index >= this->all_comp_units.size ())
ff4c9fec 2918 {
b76e467d 2919 index -= this->all_comp_units.size ();
b2bdb8cf 2920 gdb_assert (index < this->all_type_units.size ());
ff4c9fec
SM
2921 return &this->all_type_units[index]->per_cu;
2922 }
f4dc4d17 2923
ff4c9fec
SM
2924 return this->all_comp_units[index];
2925}
f4dc4d17 2926
ff4c9fec 2927/* See declaration. */
2fdf6df6 2928
ff4c9fec
SM
2929dwarf2_per_cu_data *
2930dwarf2_per_objfile::get_cu (int index)
1fd400ff 2931{
b76e467d 2932 gdb_assert (index >= 0 && index < this->all_comp_units.size ());
f4dc4d17 2933
ff4c9fec 2934 return this->all_comp_units[index];
f4dc4d17
DE
2935}
2936
ff4c9fec 2937/* See declaration. */
f4dc4d17 2938
ff4c9fec
SM
2939signatured_type *
2940dwarf2_per_objfile::get_tu (int index)
f4dc4d17 2941{
b2bdb8cf 2942 gdb_assert (index >= 0 && index < this->all_type_units.size ());
f4dc4d17 2943
ff4c9fec 2944 return this->all_type_units[index];
1fd400ff
TT
2945}
2946
4b514bc8
JK
2947/* Return a new dwarf2_per_cu_data allocated on OBJFILE's
2948 objfile_obstack, and constructed with the specified field
2949 values. */
2950
2951static dwarf2_per_cu_data *
ed2dc618 2952create_cu_from_index_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
4b514bc8
JK
2953 struct dwarf2_section_info *section,
2954 int is_dwz,
2955 sect_offset sect_off, ULONGEST length)
2956{
ed2dc618 2957 struct objfile *objfile = dwarf2_per_objfile->objfile;
4b514bc8
JK
2958 dwarf2_per_cu_data *the_cu
2959 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2960 struct dwarf2_per_cu_data);
2961 the_cu->sect_off = sect_off;
2962 the_cu->length = length;
e3b94546 2963 the_cu->dwarf2_per_objfile = dwarf2_per_objfile;
4b514bc8
JK
2964 the_cu->section = section;
2965 the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2966 struct dwarf2_per_cu_quick_data);
2967 the_cu->is_dwz = is_dwz;
2968 return the_cu;
2969}
2970
2ec9a5e0
TT
2971/* A helper for create_cus_from_index that handles a given list of
2972 CUs. */
2fdf6df6 2973
74a0d9f6 2974static void
12359b5e 2975create_cus_from_index_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
2ec9a5e0
TT
2976 const gdb_byte *cu_list, offset_type n_elements,
2977 struct dwarf2_section_info *section,
b76e467d 2978 int is_dwz)
9291a0cd 2979{
12359b5e 2980 for (offset_type i = 0; i < n_elements; i += 2)
9291a0cd 2981 {
74a0d9f6 2982 gdb_static_assert (sizeof (ULONGEST) >= 8);
9c541725
PA
2983
2984 sect_offset sect_off
2985 = (sect_offset) extract_unsigned_integer (cu_list, 8, BFD_ENDIAN_LITTLE);
2986 ULONGEST length = extract_unsigned_integer (cu_list + 8, 8, BFD_ENDIAN_LITTLE);
9291a0cd
TT
2987 cu_list += 2 * 8;
2988
b76e467d 2989 dwarf2_per_cu_data *per_cu
ed2dc618
SM
2990 = create_cu_from_index_list (dwarf2_per_objfile, section, is_dwz,
2991 sect_off, length);
b76e467d 2992 dwarf2_per_objfile->all_comp_units.push_back (per_cu);
9291a0cd 2993 }
9291a0cd
TT
2994}
2995
2ec9a5e0 2996/* Read the CU list from the mapped index, and use it to create all
74a0d9f6 2997 the CU objects for this objfile. */
2ec9a5e0 2998
74a0d9f6 2999static void
12359b5e 3000create_cus_from_index (struct dwarf2_per_objfile *dwarf2_per_objfile,
2ec9a5e0
TT
3001 const gdb_byte *cu_list, offset_type cu_list_elements,
3002 const gdb_byte *dwz_list, offset_type dwz_elements)
3003{
b76e467d
SM
3004 gdb_assert (dwarf2_per_objfile->all_comp_units.empty ());
3005 dwarf2_per_objfile->all_comp_units.reserve
3006 ((cu_list_elements + dwz_elements) / 2);
2ec9a5e0 3007
12359b5e 3008 create_cus_from_index_list (dwarf2_per_objfile, cu_list, cu_list_elements,
b76e467d 3009 &dwarf2_per_objfile->info, 0);
2ec9a5e0
TT
3010
3011 if (dwz_elements == 0)
74a0d9f6 3012 return;
2ec9a5e0 3013
12359b5e
SM
3014 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
3015 create_cus_from_index_list (dwarf2_per_objfile, dwz_list, dwz_elements,
b76e467d 3016 &dwz->info, 1);
2ec9a5e0
TT
3017}
3018
1fd400ff 3019/* Create the signatured type hash table from the index. */
673bfd45 3020
74a0d9f6 3021static void
12359b5e
SM
3022create_signatured_type_table_from_index
3023 (struct dwarf2_per_objfile *dwarf2_per_objfile,
3024 struct dwarf2_section_info *section,
3025 const gdb_byte *bytes,
3026 offset_type elements)
1fd400ff 3027{
12359b5e 3028 struct objfile *objfile = dwarf2_per_objfile->objfile;
1fd400ff 3029
b2bdb8cf
SM
3030 gdb_assert (dwarf2_per_objfile->all_type_units.empty ());
3031 dwarf2_per_objfile->all_type_units.reserve (elements / 3);
1fd400ff 3032
12359b5e 3033 htab_t sig_types_hash = allocate_signatured_type_table (objfile);
1fd400ff 3034
12359b5e 3035 for (offset_type i = 0; i < elements; i += 3)
1fd400ff 3036 {
52dc124a 3037 struct signatured_type *sig_type;
9c541725 3038 ULONGEST signature;
1fd400ff 3039 void **slot;
9c541725 3040 cu_offset type_offset_in_tu;
1fd400ff 3041
74a0d9f6 3042 gdb_static_assert (sizeof (ULONGEST) >= 8);
9c541725
PA
3043 sect_offset sect_off
3044 = (sect_offset) extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
3045 type_offset_in_tu
3046 = (cu_offset) extract_unsigned_integer (bytes + 8, 8,
3047 BFD_ENDIAN_LITTLE);
1fd400ff
TT
3048 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
3049 bytes += 3 * 8;
3050
52dc124a 3051 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1fd400ff 3052 struct signatured_type);
52dc124a 3053 sig_type->signature = signature;
9c541725 3054 sig_type->type_offset_in_tu = type_offset_in_tu;
3019eac3 3055 sig_type->per_cu.is_debug_types = 1;
8a0459fd 3056 sig_type->per_cu.section = section;
9c541725 3057 sig_type->per_cu.sect_off = sect_off;
e3b94546 3058 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
52dc124a 3059 sig_type->per_cu.v.quick
1fd400ff
TT
3060 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3061 struct dwarf2_per_cu_quick_data);
3062
52dc124a
DE
3063 slot = htab_find_slot (sig_types_hash, sig_type, INSERT);
3064 *slot = sig_type;
1fd400ff 3065
b2bdb8cf 3066 dwarf2_per_objfile->all_type_units.push_back (sig_type);
1fd400ff
TT
3067 }
3068
673bfd45 3069 dwarf2_per_objfile->signatured_types = sig_types_hash;
1fd400ff
TT
3070}
3071
927aa2e7
JK
3072/* Create the signatured type hash table from .debug_names. */
3073
3074static void
3075create_signatured_type_table_from_debug_names
ed2dc618 3076 (struct dwarf2_per_objfile *dwarf2_per_objfile,
927aa2e7
JK
3077 const mapped_debug_names &map,
3078 struct dwarf2_section_info *section,
3079 struct dwarf2_section_info *abbrev_section)
3080{
ed2dc618
SM
3081 struct objfile *objfile = dwarf2_per_objfile->objfile;
3082
927aa2e7
JK
3083 dwarf2_read_section (objfile, section);
3084 dwarf2_read_section (objfile, abbrev_section);
3085
b2bdb8cf
SM
3086 gdb_assert (dwarf2_per_objfile->all_type_units.empty ());
3087 dwarf2_per_objfile->all_type_units.reserve (map.tu_count);
927aa2e7
JK
3088
3089 htab_t sig_types_hash = allocate_signatured_type_table (objfile);
3090
3091 for (uint32_t i = 0; i < map.tu_count; ++i)
3092 {
3093 struct signatured_type *sig_type;
927aa2e7 3094 void **slot;
927aa2e7
JK
3095
3096 sect_offset sect_off
3097 = (sect_offset) (extract_unsigned_integer
3098 (map.tu_table_reordered + i * map.offset_size,
3099 map.offset_size,
3100 map.dwarf5_byte_order));
3101
3102 comp_unit_head cu_header;
ed2dc618
SM
3103 read_and_check_comp_unit_head (dwarf2_per_objfile, &cu_header, section,
3104 abbrev_section,
927aa2e7
JK
3105 section->buffer + to_underlying (sect_off),
3106 rcuh_kind::TYPE);
3107
3108 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3109 struct signatured_type);
3110 sig_type->signature = cu_header.signature;
3111 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
3112 sig_type->per_cu.is_debug_types = 1;
3113 sig_type->per_cu.section = section;
3114 sig_type->per_cu.sect_off = sect_off;
e3b94546 3115 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
927aa2e7
JK
3116 sig_type->per_cu.v.quick
3117 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3118 struct dwarf2_per_cu_quick_data);
3119
3120 slot = htab_find_slot (sig_types_hash, sig_type, INSERT);
3121 *slot = sig_type;
3122
b2bdb8cf 3123 dwarf2_per_objfile->all_type_units.push_back (sig_type);
927aa2e7
JK
3124 }
3125
3126 dwarf2_per_objfile->signatured_types = sig_types_hash;
3127}
3128
9291a0cd
TT
3129/* Read the address map data from the mapped index, and use it to
3130 populate the objfile's psymtabs_addrmap. */
2fdf6df6 3131
9291a0cd 3132static void
ed2dc618
SM
3133create_addrmap_from_index (struct dwarf2_per_objfile *dwarf2_per_objfile,
3134 struct mapped_index *index)
9291a0cd 3135{
ed2dc618 3136 struct objfile *objfile = dwarf2_per_objfile->objfile;
3e29f34a 3137 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9291a0cd 3138 const gdb_byte *iter, *end;
9291a0cd 3139 struct addrmap *mutable_map;
9291a0cd
TT
3140 CORE_ADDR baseaddr;
3141
8268c778
PA
3142 auto_obstack temp_obstack;
3143
9291a0cd
TT
3144 mutable_map = addrmap_create_mutable (&temp_obstack);
3145
f00a2de2
PA
3146 iter = index->address_table.data ();
3147 end = iter + index->address_table.size ();
9291a0cd
TT
3148
3149 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
3150
3151 while (iter < end)
3152 {
3153 ULONGEST hi, lo, cu_index;
3154 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
3155 iter += 8;
3156 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
3157 iter += 8;
3158 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
3159 iter += 4;
f652bce2 3160
24a55014 3161 if (lo > hi)
f652bce2 3162 {
b98664d3 3163 complaint (_(".gdb_index address table has invalid range (%s - %s)"),
c0cd8254 3164 hex_string (lo), hex_string (hi));
24a55014 3165 continue;
f652bce2 3166 }
24a55014 3167
b76e467d 3168 if (cu_index >= dwarf2_per_objfile->all_comp_units.size ())
f652bce2 3169 {
b98664d3 3170 complaint (_(".gdb_index address table has invalid CU number %u"),
f652bce2 3171 (unsigned) cu_index);
24a55014 3172 continue;
f652bce2 3173 }
24a55014 3174
3e29f34a
MR
3175 lo = gdbarch_adjust_dwarf2_addr (gdbarch, lo + baseaddr);
3176 hi = gdbarch_adjust_dwarf2_addr (gdbarch, hi + baseaddr);
ed2dc618 3177 addrmap_set_empty (mutable_map, lo, hi - 1,
ff4c9fec 3178 dwarf2_per_objfile->get_cu (cu_index));
9291a0cd
TT
3179 }
3180
3181 objfile->psymtabs_addrmap = addrmap_create_fixed (mutable_map,
3182 &objfile->objfile_obstack);
9291a0cd
TT
3183}
3184
927aa2e7
JK
3185/* Read the address map data from DWARF-5 .debug_aranges, and use it to
3186 populate the objfile's psymtabs_addrmap. */
3187
3188static void
ed2dc618 3189create_addrmap_from_aranges (struct dwarf2_per_objfile *dwarf2_per_objfile,
927aa2e7
JK
3190 struct dwarf2_section_info *section)
3191{
ed2dc618 3192 struct objfile *objfile = dwarf2_per_objfile->objfile;
927aa2e7
JK
3193 bfd *abfd = objfile->obfd;
3194 struct gdbarch *gdbarch = get_objfile_arch (objfile);
3195 const CORE_ADDR baseaddr = ANOFFSET (objfile->section_offsets,
3196 SECT_OFF_TEXT (objfile));
3197
3198 auto_obstack temp_obstack;
3199 addrmap *mutable_map = addrmap_create_mutable (&temp_obstack);
3200
3201 std::unordered_map<sect_offset,
3202 dwarf2_per_cu_data *,
3203 gdb::hash_enum<sect_offset>>
3204 debug_info_offset_to_per_cu;
b76e467d 3205 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
927aa2e7 3206 {
927aa2e7
JK
3207 const auto insertpair
3208 = debug_info_offset_to_per_cu.emplace (per_cu->sect_off, per_cu);
3209 if (!insertpair.second)
3210 {
3211 warning (_("Section .debug_aranges in %s has duplicate "
9d8780f0
SM
3212 "debug_info_offset %s, ignoring .debug_aranges."),
3213 objfile_name (objfile), sect_offset_str (per_cu->sect_off));
927aa2e7
JK
3214 return;
3215 }
3216 }
3217
3218 dwarf2_read_section (objfile, section);
3219
3220 const bfd_endian dwarf5_byte_order = gdbarch_byte_order (gdbarch);
3221
3222 const gdb_byte *addr = section->buffer;
3223
3224 while (addr < section->buffer + section->size)
3225 {
3226 const gdb_byte *const entry_addr = addr;
3227 unsigned int bytes_read;
3228
3229 const LONGEST entry_length = read_initial_length (abfd, addr,
3230 &bytes_read);
3231 addr += bytes_read;
3232
3233 const gdb_byte *const entry_end = addr + entry_length;
3234 const bool dwarf5_is_dwarf64 = bytes_read != 4;
3235 const uint8_t offset_size = dwarf5_is_dwarf64 ? 8 : 4;
3236 if (addr + entry_length > section->buffer + section->size)
3237 {
3238 warning (_("Section .debug_aranges in %s entry at offset %zu "
3239 "length %s exceeds section length %s, "
3240 "ignoring .debug_aranges."),
3241 objfile_name (objfile), entry_addr - section->buffer,
3242 plongest (bytes_read + entry_length),
3243 pulongest (section->size));
3244 return;
3245 }
3246
3247 /* The version number. */
3248 const uint16_t version = read_2_bytes (abfd, addr);
3249 addr += 2;
3250 if (version != 2)
3251 {
3252 warning (_("Section .debug_aranges in %s entry at offset %zu "
3253 "has unsupported version %d, ignoring .debug_aranges."),
3254 objfile_name (objfile), entry_addr - section->buffer,
3255 version);
3256 return;
3257 }
3258
3259 const uint64_t debug_info_offset
3260 = extract_unsigned_integer (addr, offset_size, dwarf5_byte_order);
3261 addr += offset_size;
3262 const auto per_cu_it
3263 = debug_info_offset_to_per_cu.find (sect_offset (debug_info_offset));
3264 if (per_cu_it == debug_info_offset_to_per_cu.cend ())
3265 {
3266 warning (_("Section .debug_aranges in %s entry at offset %zu "
3267 "debug_info_offset %s does not exists, "
3268 "ignoring .debug_aranges."),
3269 objfile_name (objfile), entry_addr - section->buffer,
3270 pulongest (debug_info_offset));
3271 return;
3272 }
3273 dwarf2_per_cu_data *const per_cu = per_cu_it->second;
3274
3275 const uint8_t address_size = *addr++;
3276 if (address_size < 1 || address_size > 8)
3277 {
3278 warning (_("Section .debug_aranges in %s entry at offset %zu "
3279 "address_size %u is invalid, ignoring .debug_aranges."),
3280 objfile_name (objfile), entry_addr - section->buffer,
3281 address_size);
3282 return;
3283 }
3284
3285 const uint8_t segment_selector_size = *addr++;
3286 if (segment_selector_size != 0)
3287 {
3288 warning (_("Section .debug_aranges in %s entry at offset %zu "
3289 "segment_selector_size %u is not supported, "
3290 "ignoring .debug_aranges."),
3291 objfile_name (objfile), entry_addr - section->buffer,
3292 segment_selector_size);
3293 return;
3294 }
3295
3296 /* Must pad to an alignment boundary that is twice the address
3297 size. It is undocumented by the DWARF standard but GCC does
3298 use it. */
3299 for (size_t padding = ((-(addr - section->buffer))
3300 & (2 * address_size - 1));
3301 padding > 0; padding--)
3302 if (*addr++ != 0)
3303 {
3304 warning (_("Section .debug_aranges in %s entry at offset %zu "
3305 "padding is not zero, ignoring .debug_aranges."),
3306 objfile_name (objfile), entry_addr - section->buffer);
3307 return;
3308 }
3309
3310 for (;;)
3311 {
3312 if (addr + 2 * address_size > entry_end)
3313 {
3314 warning (_("Section .debug_aranges in %s entry at offset %zu "
3315 "address list is not properly terminated, "
3316 "ignoring .debug_aranges."),
3317 objfile_name (objfile), entry_addr - section->buffer);
3318 return;
3319 }
3320 ULONGEST start = extract_unsigned_integer (addr, address_size,
3321 dwarf5_byte_order);
3322 addr += address_size;
3323 ULONGEST length = extract_unsigned_integer (addr, address_size,
3324 dwarf5_byte_order);
3325 addr += address_size;
3326 if (start == 0 && length == 0)
3327 break;
3328 if (start == 0 && !dwarf2_per_objfile->has_section_at_zero)
3329 {
3330 /* Symbol was eliminated due to a COMDAT group. */
3331 continue;
3332 }
3333 ULONGEST end = start + length;
3334 start = gdbarch_adjust_dwarf2_addr (gdbarch, start + baseaddr);
3335 end = gdbarch_adjust_dwarf2_addr (gdbarch, end + baseaddr);
3336 addrmap_set_empty (mutable_map, start, end - 1, per_cu);
3337 }
3338 }
3339
3340 objfile->psymtabs_addrmap = addrmap_create_fixed (mutable_map,
3341 &objfile->objfile_obstack);
3342}
3343
9291a0cd
TT
3344/* Find a slot in the mapped index INDEX for the object named NAME.
3345 If NAME is found, set *VEC_OUT to point to the CU vector in the
109483d9
PA
3346 constant pool and return true. If NAME cannot be found, return
3347 false. */
2fdf6df6 3348
109483d9 3349static bool
9291a0cd
TT
3350find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
3351 offset_type **vec_out)
3352{
0cf03b49 3353 offset_type hash;
9291a0cd 3354 offset_type slot, step;
559a7a62 3355 int (*cmp) (const char *, const char *);
9291a0cd 3356
791afaa2 3357 gdb::unique_xmalloc_ptr<char> without_params;
0cf03b49 3358 if (current_language->la_language == language_cplus
45280282
IB
3359 || current_language->la_language == language_fortran
3360 || current_language->la_language == language_d)
0cf03b49
JK
3361 {
3362 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
3363 not contain any. */
a8719064 3364
72998fb3 3365 if (strchr (name, '(') != NULL)
0cf03b49 3366 {
109483d9 3367 without_params = cp_remove_params (name);
0cf03b49 3368
72998fb3 3369 if (without_params != NULL)
791afaa2 3370 name = without_params.get ();
0cf03b49
JK
3371 }
3372 }
3373
559a7a62 3374 /* Index version 4 did not support case insensitive searches. But the
feea76c2 3375 indices for case insensitive languages are built in lowercase, therefore
559a7a62
JK
3376 simulate our NAME being searched is also lowercased. */
3377 hash = mapped_index_string_hash ((index->version == 4
3378 && case_sensitivity == case_sensitive_off
3379 ? 5 : index->version),
3380 name);
3381
f00a2de2
PA
3382 slot = hash & (index->symbol_table.size () - 1);
3383 step = ((hash * 17) & (index->symbol_table.size () - 1)) | 1;
559a7a62 3384 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
9291a0cd
TT
3385
3386 for (;;)
3387 {
9291a0cd 3388 const char *str;
f00a2de2
PA
3389
3390 const auto &bucket = index->symbol_table[slot];
3391 if (bucket.name == 0 && bucket.vec == 0)
109483d9 3392 return false;
9291a0cd 3393
f00a2de2 3394 str = index->constant_pool + MAYBE_SWAP (bucket.name);
559a7a62 3395 if (!cmp (name, str))
9291a0cd
TT
3396 {
3397 *vec_out = (offset_type *) (index->constant_pool
f00a2de2 3398 + MAYBE_SWAP (bucket.vec));
109483d9 3399 return true;
9291a0cd
TT
3400 }
3401
f00a2de2 3402 slot = (slot + step) & (index->symbol_table.size () - 1);
9291a0cd
TT
3403 }
3404}
3405
2ec9a5e0
TT
3406/* A helper function that reads the .gdb_index from SECTION and fills
3407 in MAP. FILENAME is the name of the file containing the section;
d33bc52e 3408 it is used for error reporting. DEPRECATED_OK is true if it is
2ec9a5e0
TT
3409 ok to use deprecated sections.
3410
3411 CU_LIST, CU_LIST_ELEMENTS, TYPES_LIST, and TYPES_LIST_ELEMENTS are
3412 out parameters that are filled in with information about the CU and
3413 TU lists in the section.
3414
3415 Returns 1 if all went well, 0 otherwise. */
2fdf6df6 3416
d33bc52e 3417static bool
7b23e087
SM
3418read_gdb_index_from_section (struct objfile *objfile,
3419 const char *filename,
3420 bool deprecated_ok,
3421 struct dwarf2_section_info *section,
3422 struct mapped_index *map,
3423 const gdb_byte **cu_list,
3424 offset_type *cu_list_elements,
3425 const gdb_byte **types_list,
3426 offset_type *types_list_elements)
9291a0cd 3427{
948f8e3d 3428 const gdb_byte *addr;
2ec9a5e0 3429 offset_type version;
b3b272e1 3430 offset_type *metadata;
1fd400ff 3431 int i;
9291a0cd 3432
2ec9a5e0 3433 if (dwarf2_section_empty_p (section))
9291a0cd 3434 return 0;
82430852
JK
3435
3436 /* Older elfutils strip versions could keep the section in the main
3437 executable while splitting it for the separate debug info file. */
a32a8923 3438 if ((get_section_flags (section) & SEC_HAS_CONTENTS) == 0)
82430852
JK
3439 return 0;
3440
2ec9a5e0 3441 dwarf2_read_section (objfile, section);
9291a0cd 3442
2ec9a5e0 3443 addr = section->buffer;
9291a0cd 3444 /* Version check. */
1fd400ff 3445 version = MAYBE_SWAP (*(offset_type *) addr);
987d643c 3446 /* Versions earlier than 3 emitted every copy of a psymbol. This
a6e293d1 3447 causes the index to behave very poorly for certain requests. Version 3
831adc1f 3448 contained incomplete addrmap. So, it seems better to just ignore such
481860b3 3449 indices. */
831adc1f 3450 if (version < 4)
481860b3
GB
3451 {
3452 static int warning_printed = 0;
3453 if (!warning_printed)
3454 {
3455 warning (_("Skipping obsolete .gdb_index section in %s."),
2ec9a5e0 3456 filename);
481860b3
GB
3457 warning_printed = 1;
3458 }
3459 return 0;
3460 }
3461 /* Index version 4 uses a different hash function than index version
3462 5 and later.
3463
3464 Versions earlier than 6 did not emit psymbols for inlined
3465 functions. Using these files will cause GDB not to be able to
3466 set breakpoints on inlined functions by name, so we ignore these
e615022a
DE
3467 indices unless the user has done
3468 "set use-deprecated-index-sections on". */
2ec9a5e0 3469 if (version < 6 && !deprecated_ok)
481860b3
GB
3470 {
3471 static int warning_printed = 0;
3472 if (!warning_printed)
3473 {
e615022a
DE
3474 warning (_("\
3475Skipping deprecated .gdb_index section in %s.\n\
3476Do \"set use-deprecated-index-sections on\" before the file is read\n\
3477to use the section anyway."),
2ec9a5e0 3478 filename);
481860b3
GB
3479 warning_printed = 1;
3480 }
3481 return 0;
3482 }
796a7ff8 3483 /* Version 7 indices generated by gold refer to the CU for a symbol instead
8943b874
DE
3484 of the TU (for symbols coming from TUs),
3485 http://sourceware.org/bugzilla/show_bug.cgi?id=15021.
3486 Plus gold-generated indices can have duplicate entries for global symbols,
3487 http://sourceware.org/bugzilla/show_bug.cgi?id=15646.
3488 These are just performance bugs, and we can't distinguish gdb-generated
3489 indices from gold-generated ones, so issue no warning here. */
796a7ff8 3490
481860b3 3491 /* Indexes with higher version than the one supported by GDB may be no
594e8718 3492 longer backward compatible. */
796a7ff8 3493 if (version > 8)
594e8718 3494 return 0;
9291a0cd 3495
559a7a62 3496 map->version = version;
9291a0cd
TT
3497
3498 metadata = (offset_type *) (addr + sizeof (offset_type));
1fd400ff
TT
3499
3500 i = 0;
2ec9a5e0
TT
3501 *cu_list = addr + MAYBE_SWAP (metadata[i]);
3502 *cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
3503 / 8);
1fd400ff
TT
3504 ++i;
3505
2ec9a5e0
TT
3506 *types_list = addr + MAYBE_SWAP (metadata[i]);
3507 *types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
3508 - MAYBE_SWAP (metadata[i]))
3509 / 8);
987d643c 3510 ++i;
1fd400ff 3511
f00a2de2
PA
3512 const gdb_byte *address_table = addr + MAYBE_SWAP (metadata[i]);
3513 const gdb_byte *address_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
3514 map->address_table
3515 = gdb::array_view<const gdb_byte> (address_table, address_table_end);
1fd400ff
TT
3516 ++i;
3517
f00a2de2
PA
3518 const gdb_byte *symbol_table = addr + MAYBE_SWAP (metadata[i]);
3519 const gdb_byte *symbol_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
3520 map->symbol_table
3521 = gdb::array_view<mapped_index::symbol_table_slot>
3522 ((mapped_index::symbol_table_slot *) symbol_table,
3523 (mapped_index::symbol_table_slot *) symbol_table_end);
9291a0cd 3524
f00a2de2 3525 ++i;
f9d83a0b 3526 map->constant_pool = (char *) (addr + MAYBE_SWAP (metadata[i]));
1fd400ff 3527
2ec9a5e0
TT
3528 return 1;
3529}
3530
927aa2e7 3531/* Read .gdb_index. If everything went ok, initialize the "quick"
2ec9a5e0
TT
3532 elements of all the CUs and return 1. Otherwise, return 0. */
3533
3534static int
7b23e087 3535dwarf2_read_gdb_index (struct dwarf2_per_objfile *dwarf2_per_objfile)
2ec9a5e0 3536{
2ec9a5e0
TT
3537 const gdb_byte *cu_list, *types_list, *dwz_list = NULL;
3538 offset_type cu_list_elements, types_list_elements, dwz_list_elements = 0;
4db1a1dc 3539 struct dwz_file *dwz;
12359b5e 3540 struct objfile *objfile = dwarf2_per_objfile->objfile;
2ec9a5e0 3541
3063847f 3542 std::unique_ptr<struct mapped_index> map (new struct mapped_index);
7b23e087
SM
3543 if (!read_gdb_index_from_section (objfile, objfile_name (objfile),
3544 use_deprecated_index_sections,
3545 &dwarf2_per_objfile->gdb_index, map.get (),
3546 &cu_list, &cu_list_elements,
3547 &types_list, &types_list_elements))
2ec9a5e0
TT
3548 return 0;
3549
0fefef59 3550 /* Don't use the index if it's empty. */
3063847f 3551 if (map->symbol_table.empty ())
0fefef59
DE
3552 return 0;
3553
2ec9a5e0
TT
3554 /* If there is a .dwz file, read it so we can get its CU list as
3555 well. */
ed2dc618 3556 dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
4db1a1dc 3557 if (dwz != NULL)
2ec9a5e0 3558 {
2ec9a5e0
TT
3559 struct mapped_index dwz_map;
3560 const gdb_byte *dwz_types_ignore;
3561 offset_type dwz_types_elements_ignore;
3562
7b23e087
SM
3563 if (!read_gdb_index_from_section (objfile,
3564 bfd_get_filename (dwz->dwz_bfd), 1,
3565 &dwz->gdb_index, &dwz_map,
3566 &dwz_list, &dwz_list_elements,
3567 &dwz_types_ignore,
3568 &dwz_types_elements_ignore))
2ec9a5e0
TT
3569 {
3570 warning (_("could not read '.gdb_index' section from %s; skipping"),
3571 bfd_get_filename (dwz->dwz_bfd));
3572 return 0;
3573 }
3574 }
3575
12359b5e
SM
3576 create_cus_from_index (dwarf2_per_objfile, cu_list, cu_list_elements,
3577 dwz_list, dwz_list_elements);
1fd400ff 3578
8b70b953
TT
3579 if (types_list_elements)
3580 {
3581 struct dwarf2_section_info *section;
3582
3583 /* We can only handle a single .debug_types when we have an
3584 index. */
3585 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) != 1)
3586 return 0;
3587
3588 section = VEC_index (dwarf2_section_info_def,
3589 dwarf2_per_objfile->types, 0);
3590
12359b5e
SM
3591 create_signatured_type_table_from_index (dwarf2_per_objfile, section,
3592 types_list, types_list_elements);
8b70b953 3593 }
9291a0cd 3594
3063847f 3595 create_addrmap_from_index (dwarf2_per_objfile, map.get ());
9291a0cd 3596
3063847f 3597 dwarf2_per_objfile->index_table = std::move (map);
9291a0cd 3598 dwarf2_per_objfile->using_index = 1;
7b9f3c50 3599 dwarf2_per_objfile->quick_file_names_table =
b76e467d 3600 create_quick_file_names_table (dwarf2_per_objfile->all_comp_units.size ());
9291a0cd
TT
3601
3602 return 1;
3603}
3604
dee91e82 3605/* die_reader_func for dw2_get_file_names. */
2fdf6df6 3606
dee91e82
DE
3607static void
3608dw2_get_file_names_reader (const struct die_reader_specs *reader,
d521ce57 3609 const gdb_byte *info_ptr,
dee91e82
DE
3610 struct die_info *comp_unit_die,
3611 int has_children,
3612 void *data)
9291a0cd 3613{
dee91e82 3614 struct dwarf2_cu *cu = reader->cu;
ed2dc618 3615 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
518817b3
SM
3616 struct dwarf2_per_objfile *dwarf2_per_objfile
3617 = cu->per_cu->dwarf2_per_objfile;
dee91e82 3618 struct objfile *objfile = dwarf2_per_objfile->objfile;
094b34ac 3619 struct dwarf2_per_cu_data *lh_cu;
9291a0cd 3620 struct attribute *attr;
dee91e82 3621 int i;
7b9f3c50
DE
3622 void **slot;
3623 struct quick_file_names *qfn;
9291a0cd 3624
0186c6a7
DE
3625 gdb_assert (! this_cu->is_debug_types);
3626
07261596
TT
3627 /* Our callers never want to match partial units -- instead they
3628 will match the enclosing full CU. */
3629 if (comp_unit_die->tag == DW_TAG_partial_unit)
3630 {
3631 this_cu->v.quick->no_file_data = 1;
3632 return;
3633 }
3634
0186c6a7 3635 lh_cu = this_cu;
7b9f3c50 3636 slot = NULL;
dee91e82 3637
fff8551c 3638 line_header_up lh;
9c541725 3639 sect_offset line_offset {};
fff8551c 3640
dee91e82 3641 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
9291a0cd
TT
3642 if (attr)
3643 {
7b9f3c50
DE
3644 struct quick_file_names find_entry;
3645
9c541725 3646 line_offset = (sect_offset) DW_UNSND (attr);
7b9f3c50
DE
3647
3648 /* We may have already read in this line header (TU line header sharing).
3649 If we have we're done. */
094b34ac 3650 find_entry.hash.dwo_unit = cu->dwo_unit;
9c541725 3651 find_entry.hash.line_sect_off = line_offset;
7b9f3c50
DE
3652 slot = htab_find_slot (dwarf2_per_objfile->quick_file_names_table,
3653 &find_entry, INSERT);
3654 if (*slot != NULL)
3655 {
9a3c8263 3656 lh_cu->v.quick->file_names = (struct quick_file_names *) *slot;
dee91e82 3657 return;
7b9f3c50
DE
3658 }
3659
3019eac3 3660 lh = dwarf_decode_line_header (line_offset, cu);
9291a0cd
TT
3661 }
3662 if (lh == NULL)
3663 {
094b34ac 3664 lh_cu->v.quick->no_file_data = 1;
dee91e82 3665 return;
9291a0cd
TT
3666 }
3667
8d749320 3668 qfn = XOBNEW (&objfile->objfile_obstack, struct quick_file_names);
094b34ac 3669 qfn->hash.dwo_unit = cu->dwo_unit;
9c541725 3670 qfn->hash.line_sect_off = line_offset;
7b9f3c50
DE
3671 gdb_assert (slot != NULL);
3672 *slot = qfn;
9291a0cd 3673
d721ba37 3674 file_and_directory fnd = find_file_and_directory (comp_unit_die, cu);
9291a0cd 3675
fff8551c 3676 qfn->num_file_names = lh->file_names.size ();
8d749320 3677 qfn->file_names =
fff8551c
PA
3678 XOBNEWVEC (&objfile->objfile_obstack, const char *, lh->file_names.size ());
3679 for (i = 0; i < lh->file_names.size (); ++i)
3680 qfn->file_names[i] = file_full_name (i + 1, lh.get (), fnd.comp_dir);
7b9f3c50 3681 qfn->real_names = NULL;
9291a0cd 3682
094b34ac 3683 lh_cu->v.quick->file_names = qfn;
dee91e82
DE
3684}
3685
3686/* A helper for the "quick" functions which attempts to read the line
3687 table for THIS_CU. */
3688
3689static struct quick_file_names *
e4a48d9d 3690dw2_get_file_names (struct dwarf2_per_cu_data *this_cu)
dee91e82 3691{
0186c6a7
DE
3692 /* This should never be called for TUs. */
3693 gdb_assert (! this_cu->is_debug_types);
3694 /* Nor type unit groups. */
3695 gdb_assert (! IS_TYPE_UNIT_GROUP (this_cu));
f4dc4d17 3696
dee91e82
DE
3697 if (this_cu->v.quick->file_names != NULL)
3698 return this_cu->v.quick->file_names;
3699 /* If we know there is no line data, no point in looking again. */
3700 if (this_cu->v.quick->no_file_data)
3701 return NULL;
3702
0186c6a7 3703 init_cutu_and_read_dies_simple (this_cu, dw2_get_file_names_reader, NULL);
dee91e82
DE
3704
3705 if (this_cu->v.quick->no_file_data)
3706 return NULL;
3707 return this_cu->v.quick->file_names;
9291a0cd
TT
3708}
3709
3710/* A helper for the "quick" functions which computes and caches the
7b9f3c50 3711 real path for a given file name from the line table. */
2fdf6df6 3712
9291a0cd 3713static const char *
7b9f3c50
DE
3714dw2_get_real_path (struct objfile *objfile,
3715 struct quick_file_names *qfn, int index)
9291a0cd 3716{
7b9f3c50
DE
3717 if (qfn->real_names == NULL)
3718 qfn->real_names = OBSTACK_CALLOC (&objfile->objfile_obstack,
26f2dc30 3719 qfn->num_file_names, const char *);
9291a0cd 3720
7b9f3c50 3721 if (qfn->real_names[index] == NULL)
14278e1f 3722 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]).release ();
9291a0cd 3723
7b9f3c50 3724 return qfn->real_names[index];
9291a0cd
TT
3725}
3726
3727static struct symtab *
3728dw2_find_last_source_symtab (struct objfile *objfile)
3729{
ed2dc618
SM
3730 struct dwarf2_per_objfile *dwarf2_per_objfile
3731 = get_dwarf2_per_objfile (objfile);
b76e467d 3732 dwarf2_per_cu_data *dwarf_cu = dwarf2_per_objfile->all_comp_units.back ();
58f0c718 3733 compunit_symtab *cust = dw2_instantiate_symtab (dwarf_cu, false);
ae2de4f8 3734
43f3e411
DE
3735 if (cust == NULL)
3736 return NULL;
ed2dc618 3737
43f3e411 3738 return compunit_primary_filetab (cust);
9291a0cd
TT
3739}
3740
7b9f3c50
DE
3741/* Traversal function for dw2_forget_cached_source_info. */
3742
3743static int
3744dw2_free_cached_file_names (void **slot, void *info)
9291a0cd 3745{
7b9f3c50 3746 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
9291a0cd 3747
7b9f3c50 3748 if (file_data->real_names)
9291a0cd 3749 {
7b9f3c50 3750 int i;
9291a0cd 3751
7b9f3c50 3752 for (i = 0; i < file_data->num_file_names; ++i)
9291a0cd 3753 {
7b9f3c50
DE
3754 xfree ((void*) file_data->real_names[i]);
3755 file_data->real_names[i] = NULL;
9291a0cd
TT
3756 }
3757 }
7b9f3c50
DE
3758
3759 return 1;
3760}
3761
3762static void
3763dw2_forget_cached_source_info (struct objfile *objfile)
3764{
ed2dc618
SM
3765 struct dwarf2_per_objfile *dwarf2_per_objfile
3766 = get_dwarf2_per_objfile (objfile);
7b9f3c50
DE
3767
3768 htab_traverse_noresize (dwarf2_per_objfile->quick_file_names_table,
3769 dw2_free_cached_file_names, NULL);
9291a0cd
TT
3770}
3771
f8eba3c6
TT
3772/* Helper function for dw2_map_symtabs_matching_filename that expands
3773 the symtabs and calls the iterator. */
3774
3775static int
3776dw2_map_expand_apply (struct objfile *objfile,
3777 struct dwarf2_per_cu_data *per_cu,
f5b95b50 3778 const char *name, const char *real_path,
14bc53a8 3779 gdb::function_view<bool (symtab *)> callback)
f8eba3c6 3780{
43f3e411 3781 struct compunit_symtab *last_made = objfile->compunit_symtabs;
f8eba3c6
TT
3782
3783 /* Don't visit already-expanded CUs. */
43f3e411 3784 if (per_cu->v.quick->compunit_symtab)
f8eba3c6
TT
3785 return 0;
3786
3787 /* This may expand more than one symtab, and we want to iterate over
3788 all of them. */
58f0c718 3789 dw2_instantiate_symtab (per_cu, false);
f8eba3c6 3790
14bc53a8
PA
3791 return iterate_over_some_symtabs (name, real_path, objfile->compunit_symtabs,
3792 last_made, callback);
f8eba3c6
TT
3793}
3794
3795/* Implementation of the map_symtabs_matching_filename method. */
3796
14bc53a8
PA
3797static bool
3798dw2_map_symtabs_matching_filename
3799 (struct objfile *objfile, const char *name, const char *real_path,
3800 gdb::function_view<bool (symtab *)> callback)
9291a0cd 3801{
c011a4f4 3802 const char *name_basename = lbasename (name);
ed2dc618
SM
3803 struct dwarf2_per_objfile *dwarf2_per_objfile
3804 = get_dwarf2_per_objfile (objfile);
ae2de4f8 3805
848e3e78
DE
3806 /* The rule is CUs specify all the files, including those used by
3807 any TU, so there's no need to scan TUs here. */
f4dc4d17 3808
b76e467d 3809 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
9291a0cd 3810 {
3d7bb9d9 3811 /* We only need to look at symtabs not already expanded. */
43f3e411 3812 if (per_cu->v.quick->compunit_symtab)
9291a0cd
TT
3813 continue;
3814
b76e467d 3815 quick_file_names *file_data = dw2_get_file_names (per_cu);
7b9f3c50 3816 if (file_data == NULL)
9291a0cd
TT
3817 continue;
3818
b76e467d 3819 for (int j = 0; j < file_data->num_file_names; ++j)
9291a0cd 3820 {
7b9f3c50 3821 const char *this_name = file_data->file_names[j];
da235a7c 3822 const char *this_real_name;
9291a0cd 3823
af529f8f 3824 if (compare_filenames_for_search (this_name, name))
9291a0cd 3825 {
f5b95b50 3826 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
14bc53a8
PA
3827 callback))
3828 return true;
288e77a7 3829 continue;
4aac40c8 3830 }
9291a0cd 3831
c011a4f4
DE
3832 /* Before we invoke realpath, which can get expensive when many
3833 files are involved, do a quick comparison of the basenames. */
3834 if (! basenames_may_differ
3835 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
3836 continue;
3837
da235a7c
JK
3838 this_real_name = dw2_get_real_path (objfile, file_data, j);
3839 if (compare_filenames_for_search (this_real_name, name))
9291a0cd 3840 {
da235a7c 3841 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
14bc53a8
PA
3842 callback))
3843 return true;
288e77a7 3844 continue;
da235a7c 3845 }
9291a0cd 3846
da235a7c
JK
3847 if (real_path != NULL)
3848 {
af529f8f
JK
3849 gdb_assert (IS_ABSOLUTE_PATH (real_path));
3850 gdb_assert (IS_ABSOLUTE_PATH (name));
7b9f3c50 3851 if (this_real_name != NULL
af529f8f 3852 && FILENAME_CMP (real_path, this_real_name) == 0)
9291a0cd 3853 {
f5b95b50 3854 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
14bc53a8
PA
3855 callback))
3856 return true;
288e77a7 3857 continue;
9291a0cd
TT
3858 }
3859 }
3860 }
3861 }
3862
14bc53a8 3863 return false;
9291a0cd
TT
3864}
3865
da51c347
DE
3866/* Struct used to manage iterating over all CUs looking for a symbol. */
3867
3868struct dw2_symtab_iterator
9291a0cd 3869{
ed2dc618
SM
3870 /* The dwarf2_per_objfile owning the CUs we are iterating on. */
3871 struct dwarf2_per_objfile *dwarf2_per_objfile;
da51c347
DE
3872 /* If non-zero, only look for symbols that match BLOCK_INDEX. */
3873 int want_specific_block;
3874 /* One of GLOBAL_BLOCK or STATIC_BLOCK.
3875 Unused if !WANT_SPECIFIC_BLOCK. */
3876 int block_index;
3877 /* The kind of symbol we're looking for. */
3878 domain_enum domain;
3879 /* The list of CUs from the index entry of the symbol,
3880 or NULL if not found. */
3881 offset_type *vec;
3882 /* The next element in VEC to look at. */
3883 int next;
3884 /* The number of elements in VEC, or zero if there is no match. */
3885 int length;
8943b874
DE
3886 /* Have we seen a global version of the symbol?
3887 If so we can ignore all further global instances.
3888 This is to work around gold/15646, inefficient gold-generated
3889 indices. */
3890 int global_seen;
da51c347 3891};
9291a0cd 3892
da51c347
DE
3893/* Initialize the index symtab iterator ITER.
3894 If WANT_SPECIFIC_BLOCK is non-zero, only look for symbols
3895 in block BLOCK_INDEX. Otherwise BLOCK_INDEX is ignored. */
2fdf6df6 3896
9291a0cd 3897static void
da51c347 3898dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
ed2dc618 3899 struct dwarf2_per_objfile *dwarf2_per_objfile,
da51c347
DE
3900 int want_specific_block,
3901 int block_index,
3902 domain_enum domain,
3903 const char *name)
3904{
ed2dc618 3905 iter->dwarf2_per_objfile = dwarf2_per_objfile;
da51c347
DE
3906 iter->want_specific_block = want_specific_block;
3907 iter->block_index = block_index;
3908 iter->domain = domain;
3909 iter->next = 0;
8943b874 3910 iter->global_seen = 0;
da51c347 3911
3063847f 3912 mapped_index *index = dwarf2_per_objfile->index_table.get ();
ed2dc618
SM
3913
3914 /* index is NULL if OBJF_READNOW. */
3915 if (index != NULL && find_slot_in_mapped_hash (index, name, &iter->vec))
da51c347
DE
3916 iter->length = MAYBE_SWAP (*iter->vec);
3917 else
3918 {
3919 iter->vec = NULL;
3920 iter->length = 0;
3921 }
3922}
3923
3924/* Return the next matching CU or NULL if there are no more. */
3925
3926static struct dwarf2_per_cu_data *
3927dw2_symtab_iter_next (struct dw2_symtab_iterator *iter)
3928{
ed2dc618
SM
3929 struct dwarf2_per_objfile *dwarf2_per_objfile = iter->dwarf2_per_objfile;
3930
da51c347
DE
3931 for ( ; iter->next < iter->length; ++iter->next)
3932 {
3933 offset_type cu_index_and_attrs =
3934 MAYBE_SWAP (iter->vec[iter->next + 1]);
3935 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
da51c347
DE
3936 int want_static = iter->block_index != GLOBAL_BLOCK;
3937 /* This value is only valid for index versions >= 7. */
3938 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
3939 gdb_index_symbol_kind symbol_kind =
3940 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3941 /* Only check the symbol attributes if they're present.
3942 Indices prior to version 7 don't record them,
3943 and indices >= 7 may elide them for certain symbols
3944 (gold does this). */
3945 int attrs_valid =
ed2dc618 3946 (dwarf2_per_objfile->index_table->version >= 7
da51c347
DE
3947 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
3948
3190f0c6 3949 /* Don't crash on bad data. */
b76e467d 3950 if (cu_index >= (dwarf2_per_objfile->all_comp_units.size ()
b2bdb8cf 3951 + dwarf2_per_objfile->all_type_units.size ()))
3190f0c6 3952 {
b98664d3 3953 complaint (_(".gdb_index entry has bad CU index"
4262abfb
JK
3954 " [in module %s]"),
3955 objfile_name (dwarf2_per_objfile->objfile));
3190f0c6
DE
3956 continue;
3957 }
3958
ff4c9fec 3959 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (cu_index);
3190f0c6 3960
da51c347 3961 /* Skip if already read in. */
43f3e411 3962 if (per_cu->v.quick->compunit_symtab)
da51c347
DE
3963 continue;
3964
8943b874
DE
3965 /* Check static vs global. */
3966 if (attrs_valid)
3967 {
3968 if (iter->want_specific_block
3969 && want_static != is_static)
3970 continue;
3971 /* Work around gold/15646. */
3972 if (!is_static && iter->global_seen)
3973 continue;
3974 if (!is_static)
3975 iter->global_seen = 1;
3976 }
da51c347
DE
3977
3978 /* Only check the symbol's kind if it has one. */
3979 if (attrs_valid)
3980 {
3981 switch (iter->domain)
3982 {
3983 case VAR_DOMAIN:
3984 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
3985 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
3986 /* Some types are also in VAR_DOMAIN. */
3987 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3988 continue;
3989 break;
3990 case STRUCT_DOMAIN:
3991 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3992 continue;
3993 break;
3994 case LABEL_DOMAIN:
3995 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3996 continue;
3997 break;
3998 default:
3999 break;
4000 }
4001 }
4002
4003 ++iter->next;
4004 return per_cu;
4005 }
4006
4007 return NULL;
4008}
4009
43f3e411 4010static struct compunit_symtab *
da51c347
DE
4011dw2_lookup_symbol (struct objfile *objfile, int block_index,
4012 const char *name, domain_enum domain)
9291a0cd 4013{
43f3e411 4014 struct compunit_symtab *stab_best = NULL;
ed2dc618
SM
4015 struct dwarf2_per_objfile *dwarf2_per_objfile
4016 = get_dwarf2_per_objfile (objfile);
9291a0cd 4017
b5ec771e
PA
4018 lookup_name_info lookup_name (name, symbol_name_match_type::FULL);
4019
ed2dc618
SM
4020 struct dw2_symtab_iterator iter;
4021 struct dwarf2_per_cu_data *per_cu;
da51c347 4022
ed2dc618 4023 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, 1, block_index, domain, name);
9291a0cd 4024
ed2dc618
SM
4025 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
4026 {
4027 struct symbol *sym, *with_opaque = NULL;
58f0c718 4028 struct compunit_symtab *stab = dw2_instantiate_symtab (per_cu, false);
ed2dc618
SM
4029 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
4030 struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
da51c347 4031
ed2dc618
SM
4032 sym = block_find_symbol (block, name, domain,
4033 block_find_non_opaque_type_preferred,
4034 &with_opaque);
b2e2f908 4035
ed2dc618
SM
4036 /* Some caution must be observed with overloaded functions
4037 and methods, since the index will not contain any overload
4038 information (but NAME might contain it). */
da51c347 4039
ed2dc618
SM
4040 if (sym != NULL
4041 && SYMBOL_MATCHES_SEARCH_NAME (sym, lookup_name))
4042 return stab;
4043 if (with_opaque != NULL
4044 && SYMBOL_MATCHES_SEARCH_NAME (with_opaque, lookup_name))
4045 stab_best = stab;
da51c347 4046
ed2dc618 4047 /* Keep looking through other CUs. */
9291a0cd 4048 }
9291a0cd 4049
da51c347 4050 return stab_best;
9291a0cd
TT
4051}
4052
4053static void
4054dw2_print_stats (struct objfile *objfile)
4055{
ed2dc618
SM
4056 struct dwarf2_per_objfile *dwarf2_per_objfile
4057 = get_dwarf2_per_objfile (objfile);
b76e467d 4058 int total = (dwarf2_per_objfile->all_comp_units.size ()
b2bdb8cf 4059 + dwarf2_per_objfile->all_type_units.size ());
ed2dc618 4060 int count = 0;
9291a0cd 4061
ed2dc618 4062 for (int i = 0; i < total; ++i)
9291a0cd 4063 {
ff4c9fec 4064 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (i);
9291a0cd 4065
43f3e411 4066 if (!per_cu->v.quick->compunit_symtab)
9291a0cd
TT
4067 ++count;
4068 }
e4a48d9d 4069 printf_filtered (_(" Number of read CUs: %d\n"), total - count);
9291a0cd
TT
4070 printf_filtered (_(" Number of unread CUs: %d\n"), count);
4071}
4072
779bd270
DE
4073/* This dumps minimal information about the index.
4074 It is called via "mt print objfiles".
4075 One use is to verify .gdb_index has been loaded by the
4076 gdb.dwarf2/gdb-index.exp testcase. */
4077
9291a0cd
TT
4078static void
4079dw2_dump (struct objfile *objfile)
4080{
ed2dc618
SM
4081 struct dwarf2_per_objfile *dwarf2_per_objfile
4082 = get_dwarf2_per_objfile (objfile);
4083
779bd270
DE
4084 gdb_assert (dwarf2_per_objfile->using_index);
4085 printf_filtered (".gdb_index:");
4086 if (dwarf2_per_objfile->index_table != NULL)
4087 {
4088 printf_filtered (" version %d\n",
4089 dwarf2_per_objfile->index_table->version);
4090 }
4091 else
4092 printf_filtered (" faked for \"readnow\"\n");
4093 printf_filtered ("\n");
9291a0cd
TT
4094}
4095
4096static void
3189cb12
DE
4097dw2_relocate (struct objfile *objfile,
4098 const struct section_offsets *new_offsets,
4099 const struct section_offsets *delta)
9291a0cd
TT
4100{
4101 /* There's nothing to relocate here. */
4102}
4103
4104static void
4105dw2_expand_symtabs_for_function (struct objfile *objfile,
4106 const char *func_name)
4107{
ed2dc618
SM
4108 struct dwarf2_per_objfile *dwarf2_per_objfile
4109 = get_dwarf2_per_objfile (objfile);
da51c347 4110
ed2dc618
SM
4111 struct dw2_symtab_iterator iter;
4112 struct dwarf2_per_cu_data *per_cu;
da51c347 4113
ed2dc618
SM
4114 /* Note: It doesn't matter what we pass for block_index here. */
4115 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, 0, GLOBAL_BLOCK, VAR_DOMAIN,
4116 func_name);
da51c347 4117
ed2dc618 4118 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
58f0c718 4119 dw2_instantiate_symtab (per_cu, false);
da51c347 4120
9291a0cd
TT
4121}
4122
4123static void
4124dw2_expand_all_symtabs (struct objfile *objfile)
4125{
ed2dc618
SM
4126 struct dwarf2_per_objfile *dwarf2_per_objfile
4127 = get_dwarf2_per_objfile (objfile);
b76e467d 4128 int total_units = (dwarf2_per_objfile->all_comp_units.size ()
b2bdb8cf 4129 + dwarf2_per_objfile->all_type_units.size ());
9291a0cd 4130
ed2dc618 4131 for (int i = 0; i < total_units; ++i)
9291a0cd 4132 {
ff4c9fec 4133 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (i);
9291a0cd 4134
58f0c718
TT
4135 /* We don't want to directly expand a partial CU, because if we
4136 read it with the wrong language, then assertion failures can
4137 be triggered later on. See PR symtab/23010. So, tell
4138 dw2_instantiate_symtab to skip partial CUs -- any important
4139 partial CU will be read via DW_TAG_imported_unit anyway. */
4140 dw2_instantiate_symtab (per_cu, true);
9291a0cd
TT
4141 }
4142}
4143
4144static void
652a8996
JK
4145dw2_expand_symtabs_with_fullname (struct objfile *objfile,
4146 const char *fullname)
9291a0cd 4147{
ed2dc618
SM
4148 struct dwarf2_per_objfile *dwarf2_per_objfile
4149 = get_dwarf2_per_objfile (objfile);
d4637a04
DE
4150
4151 /* We don't need to consider type units here.
4152 This is only called for examining code, e.g. expand_line_sal.
4153 There can be an order of magnitude (or more) more type units
4154 than comp units, and we avoid them if we can. */
4155
b76e467d 4156 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
9291a0cd 4157 {
3d7bb9d9 4158 /* We only need to look at symtabs not already expanded. */
43f3e411 4159 if (per_cu->v.quick->compunit_symtab)
9291a0cd
TT
4160 continue;
4161
b76e467d 4162 quick_file_names *file_data = dw2_get_file_names (per_cu);
7b9f3c50 4163 if (file_data == NULL)
9291a0cd
TT
4164 continue;
4165
b76e467d 4166 for (int j = 0; j < file_data->num_file_names; ++j)
9291a0cd 4167 {
652a8996
JK
4168 const char *this_fullname = file_data->file_names[j];
4169
4170 if (filename_cmp (this_fullname, fullname) == 0)
9291a0cd 4171 {
58f0c718 4172 dw2_instantiate_symtab (per_cu, false);
9291a0cd
TT
4173 break;
4174 }
4175 }
4176 }
4177}
4178
9291a0cd 4179static void
ade7ed9e 4180dw2_map_matching_symbols (struct objfile *objfile,
fe978cb0 4181 const char * name, domain_enum domain,
ade7ed9e 4182 int global,
40658b94
PH
4183 int (*callback) (struct block *,
4184 struct symbol *, void *),
b5ec771e 4185 void *data, symbol_name_match_type match,
2edb89d3 4186 symbol_compare_ftype *ordered_compare)
9291a0cd 4187{
40658b94 4188 /* Currently unimplemented; used for Ada. The function can be called if the
a9e6a4bb
JK
4189 current language is Ada for a non-Ada objfile using GNU index. As Ada
4190 does not look for non-Ada symbols this function should just return. */
9291a0cd
TT
4191}
4192
b5ec771e
PA
4193/* Symbol name matcher for .gdb_index names.
4194
4195 Symbol names in .gdb_index have a few particularities:
4196
4197 - There's no indication of which is the language of each symbol.
4198
4199 Since each language has its own symbol name matching algorithm,
4200 and we don't know which language is the right one, we must match
3f563c84
PA
4201 each symbol against all languages. This would be a potential
4202 performance problem if it were not mitigated by the
4203 mapped_index::name_components lookup table, which significantly
4204 reduces the number of times we need to call into this matcher,
4205 making it a non-issue.
b5ec771e
PA
4206
4207 - Symbol names in the index have no overload (parameter)
4208 information. I.e., in C++, "foo(int)" and "foo(long)" both
4209 appear as "foo" in the index, for example.
4210
4211 This means that the lookup names passed to the symbol name
4212 matcher functions must have no parameter information either
4213 because (e.g.) symbol search name "foo" does not match
4214 lookup-name "foo(int)" [while swapping search name for lookup
4215 name would match].
4216*/
4217class gdb_index_symbol_name_matcher
4218{
4219public:
4220 /* Prepares the vector of comparison functions for LOOKUP_NAME. */
4221 gdb_index_symbol_name_matcher (const lookup_name_info &lookup_name);
4222
4223 /* Walk all the matcher routines and match SYMBOL_NAME against them.
4224 Returns true if any matcher matches. */
4225 bool matches (const char *symbol_name);
4226
4227private:
4228 /* A reference to the lookup name we're matching against. */
4229 const lookup_name_info &m_lookup_name;
4230
4231 /* A vector holding all the different symbol name matchers, for all
4232 languages. */
4233 std::vector<symbol_name_matcher_ftype *> m_symbol_name_matcher_funcs;
4234};
4235
4236gdb_index_symbol_name_matcher::gdb_index_symbol_name_matcher
4237 (const lookup_name_info &lookup_name)
4238 : m_lookup_name (lookup_name)
4239{
4240 /* Prepare the vector of comparison functions upfront, to avoid
4241 doing the same work for each symbol. Care is taken to avoid
4242 matching with the same matcher more than once if/when multiple
4243 languages use the same matcher function. */
4244 auto &matchers = m_symbol_name_matcher_funcs;
4245 matchers.reserve (nr_languages);
4246
4247 matchers.push_back (default_symbol_name_matcher);
4248
4249 for (int i = 0; i < nr_languages; i++)
4250 {
4251 const language_defn *lang = language_def ((enum language) i);
c63d3e8d 4252 symbol_name_matcher_ftype *name_matcher
618daa93 4253 = get_symbol_name_matcher (lang, m_lookup_name);
c63d3e8d
PA
4254
4255 /* Don't insert the same comparison routine more than once.
4256 Note that we do this linear walk instead of a seemingly
4257 cheaper sorted insert, or use a std::set or something like
4258 that, because relative order of function addresses is not
4259 stable. This is not a problem in practice because the number
4260 of supported languages is low, and the cost here is tiny
4261 compared to the number of searches we'll do afterwards using
4262 this object. */
4263 if (name_matcher != default_symbol_name_matcher
4264 && (std::find (matchers.begin (), matchers.end (), name_matcher)
4265 == matchers.end ()))
4266 matchers.push_back (name_matcher);
b5ec771e
PA
4267 }
4268}
4269
4270bool
4271gdb_index_symbol_name_matcher::matches (const char *symbol_name)
4272{
4273 for (auto matches_name : m_symbol_name_matcher_funcs)
4274 if (matches_name (symbol_name, m_lookup_name, NULL))
4275 return true;
4276
4277 return false;
4278}
4279
e1ef7d7a
PA
4280/* Starting from a search name, return the string that finds the upper
4281 bound of all strings that start with SEARCH_NAME in a sorted name
4282 list. Returns the empty string to indicate that the upper bound is
4283 the end of the list. */
4284
4285static std::string
4286make_sort_after_prefix_name (const char *search_name)
4287{
4288 /* When looking to complete "func", we find the upper bound of all
4289 symbols that start with "func" by looking for where we'd insert
4290 the closest string that would follow "func" in lexicographical
4291 order. Usually, that's "func"-with-last-character-incremented,
4292 i.e. "fund". Mind non-ASCII characters, though. Usually those
4293 will be UTF-8 multi-byte sequences, but we can't be certain.
4294 Especially mind the 0xff character, which is a valid character in
4295 non-UTF-8 source character sets (e.g. Latin1 'ÿ'), and we can't
4296 rule out compilers allowing it in identifiers. Note that
4297 conveniently, strcmp/strcasecmp are specified to compare
4298 characters interpreted as unsigned char. So what we do is treat
4299 the whole string as a base 256 number composed of a sequence of
4300 base 256 "digits" and add 1 to it. I.e., adding 1 to 0xff wraps
4301 to 0, and carries 1 to the following more-significant position.
4302 If the very first character in SEARCH_NAME ends up incremented
4303 and carries/overflows, then the upper bound is the end of the
4304 list. The string after the empty string is also the empty
4305 string.
4306
4307 Some examples of this operation:
4308
4309 SEARCH_NAME => "+1" RESULT
4310
4311 "abc" => "abd"
4312 "ab\xff" => "ac"
4313 "\xff" "a" "\xff" => "\xff" "b"
4314 "\xff" => ""
4315 "\xff\xff" => ""
4316 "" => ""
4317
4318 Then, with these symbols for example:
4319
4320 func
4321 func1
4322 fund
4323
4324 completing "func" looks for symbols between "func" and
4325 "func"-with-last-character-incremented, i.e. "fund" (exclusive),
4326 which finds "func" and "func1", but not "fund".
4327
4328 And with:
4329
4330 funcÿ (Latin1 'ÿ' [0xff])
4331 funcÿ1
4332 fund
4333
4334 completing "funcÿ" looks for symbols between "funcÿ" and "fund"
4335 (exclusive), which finds "funcÿ" and "funcÿ1", but not "fund".
4336
4337 And with:
4338
4339 ÿÿ (Latin1 'ÿ' [0xff])
4340 ÿÿ1
4341
4342 completing "ÿ" or "ÿÿ" looks for symbols between between "ÿÿ" and
4343 the end of the list.
4344 */
4345 std::string after = search_name;
4346 while (!after.empty () && (unsigned char) after.back () == 0xff)
4347 after.pop_back ();
4348 if (!after.empty ())
4349 after.back () = (unsigned char) after.back () + 1;
4350 return after;
4351}
4352
5c58de74 4353/* See declaration. */
61d96d7e 4354
5c58de74
PA
4355std::pair<std::vector<name_component>::const_iterator,
4356 std::vector<name_component>::const_iterator>
44ed8f3e 4357mapped_index_base::find_name_components_bounds
5c58de74 4358 (const lookup_name_info &lookup_name_without_params) const
3f563c84 4359{
5c58de74
PA
4360 auto *name_cmp
4361 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
3f563c84
PA
4362
4363 const char *cplus
c62446b1 4364 = lookup_name_without_params.cplus ().lookup_name ().c_str ();
9291a0cd 4365
3f563c84
PA
4366 /* Comparison function object for lower_bound that matches against a
4367 given symbol name. */
4368 auto lookup_compare_lower = [&] (const name_component &elem,
4369 const char *name)
4370 {
5c58de74 4371 const char *elem_qualified = this->symbol_name_at (elem.idx);
3f563c84
PA
4372 const char *elem_name = elem_qualified + elem.name_offset;
4373 return name_cmp (elem_name, name) < 0;
4374 };
4375
4376 /* Comparison function object for upper_bound that matches against a
4377 given symbol name. */
4378 auto lookup_compare_upper = [&] (const char *name,
4379 const name_component &elem)
4380 {
5c58de74 4381 const char *elem_qualified = this->symbol_name_at (elem.idx);
3f563c84
PA
4382 const char *elem_name = elem_qualified + elem.name_offset;
4383 return name_cmp (name, elem_name) < 0;
4384 };
4385
5c58de74
PA
4386 auto begin = this->name_components.begin ();
4387 auto end = this->name_components.end ();
3f563c84
PA
4388
4389 /* Find the lower bound. */
4390 auto lower = [&] ()
4391 {
5c58de74 4392 if (lookup_name_without_params.completion_mode () && cplus[0] == '\0')
3f563c84
PA
4393 return begin;
4394 else
4395 return std::lower_bound (begin, end, cplus, lookup_compare_lower);
4396 } ();
4397
4398 /* Find the upper bound. */
4399 auto upper = [&] ()
4400 {
5c58de74 4401 if (lookup_name_without_params.completion_mode ())
3f563c84 4402 {
e1ef7d7a
PA
4403 /* In completion mode, we want UPPER to point past all
4404 symbols names that have the same prefix. I.e., with
4405 these symbols, and completing "func":
4406
4407 function << lower bound
4408 function1
4409 other_function << upper bound
4410
4411 We find the upper bound by looking for the insertion
4412 point of "func"-with-last-character-incremented,
4413 i.e. "fund". */
4414 std::string after = make_sort_after_prefix_name (cplus);
4415 if (after.empty ())
3f563c84 4416 return end;
e6b2f5ef
PA
4417 return std::lower_bound (lower, end, after.c_str (),
4418 lookup_compare_lower);
3f563c84
PA
4419 }
4420 else
4421 return std::upper_bound (lower, end, cplus, lookup_compare_upper);
4422 } ();
4423
5c58de74
PA
4424 return {lower, upper};
4425}
4426
4427/* See declaration. */
4428
4429void
44ed8f3e 4430mapped_index_base::build_name_components ()
5c58de74
PA
4431{
4432 if (!this->name_components.empty ())
4433 return;
4434
4435 this->name_components_casing = case_sensitivity;
4436 auto *name_cmp
4437 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
4438
4439 /* The code below only knows how to break apart components of C++
4440 symbol names (and other languages that use '::' as
4441 namespace/module separator). If we add support for wild matching
4442 to some language that uses some other operator (E.g., Ada, Go and
4443 D use '.'), then we'll need to try splitting the symbol name
4444 according to that language too. Note that Ada does support wild
4445 matching, but doesn't currently support .gdb_index. */
44ed8f3e
PA
4446 auto count = this->symbol_name_count ();
4447 for (offset_type idx = 0; idx < count; idx++)
5c58de74 4448 {
44ed8f3e 4449 if (this->symbol_name_slot_invalid (idx))
5c58de74
PA
4450 continue;
4451
4452 const char *name = this->symbol_name_at (idx);
4453
4454 /* Add each name component to the name component table. */
4455 unsigned int previous_len = 0;
4456 for (unsigned int current_len = cp_find_first_component (name);
4457 name[current_len] != '\0';
4458 current_len += cp_find_first_component (name + current_len))
4459 {
4460 gdb_assert (name[current_len] == ':');
4461 this->name_components.push_back ({previous_len, idx});
4462 /* Skip the '::'. */
4463 current_len += 2;
4464 previous_len = current_len;
4465 }
4466 this->name_components.push_back ({previous_len, idx});
4467 }
4468
4469 /* Sort name_components elements by name. */
4470 auto name_comp_compare = [&] (const name_component &left,
4471 const name_component &right)
4472 {
4473 const char *left_qualified = this->symbol_name_at (left.idx);
4474 const char *right_qualified = this->symbol_name_at (right.idx);
4475
4476 const char *left_name = left_qualified + left.name_offset;
4477 const char *right_name = right_qualified + right.name_offset;
4478
4479 return name_cmp (left_name, right_name) < 0;
4480 };
4481
4482 std::sort (this->name_components.begin (),
4483 this->name_components.end (),
4484 name_comp_compare);
4485}
4486
4487/* Helper for dw2_expand_symtabs_matching that works with a
44ed8f3e
PA
4488 mapped_index_base instead of the containing objfile. This is split
4489 to a separate function in order to be able to unit test the
4490 name_components matching using a mock mapped_index_base. For each
5c58de74 4491 symbol name that matches, calls MATCH_CALLBACK, passing it the
44ed8f3e 4492 symbol's index in the mapped_index_base symbol table. */
5c58de74
PA
4493
4494static void
4495dw2_expand_symtabs_matching_symbol
44ed8f3e 4496 (mapped_index_base &index,
5c58de74
PA
4497 const lookup_name_info &lookup_name_in,
4498 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4499 enum search_domain kind,
4500 gdb::function_view<void (offset_type)> match_callback)
4501{
4502 lookup_name_info lookup_name_without_params
4503 = lookup_name_in.make_ignore_params ();
4504 gdb_index_symbol_name_matcher lookup_name_matcher
4505 (lookup_name_without_params);
4506
4507 /* Build the symbol name component sorted vector, if we haven't
4508 yet. */
4509 index.build_name_components ();
4510
4511 auto bounds = index.find_name_components_bounds (lookup_name_without_params);
4512
3f563c84
PA
4513 /* Now for each symbol name in range, check to see if we have a name
4514 match, and if so, call the MATCH_CALLBACK callback. */
4515
4516 /* The same symbol may appear more than once in the range though.
4517 E.g., if we're looking for symbols that complete "w", and we have
4518 a symbol named "w1::w2", we'll find the two name components for
4519 that same symbol in the range. To be sure we only call the
4520 callback once per symbol, we first collect the symbol name
4521 indexes that matched in a temporary vector and ignore
4522 duplicates. */
4523 std::vector<offset_type> matches;
5c58de74 4524 matches.reserve (std::distance (bounds.first, bounds.second));
3f563c84 4525
5c58de74 4526 for (; bounds.first != bounds.second; ++bounds.first)
3f563c84 4527 {
5c58de74 4528 const char *qualified = index.symbol_name_at (bounds.first->idx);
3f563c84
PA
4529
4530 if (!lookup_name_matcher.matches (qualified)
4531 || (symbol_matcher != NULL && !symbol_matcher (qualified)))
9291a0cd
TT
4532 continue;
4533
5c58de74 4534 matches.push_back (bounds.first->idx);
3f563c84
PA
4535 }
4536
4537 std::sort (matches.begin (), matches.end ());
4538
4539 /* Finally call the callback, once per match. */
4540 ULONGEST prev = -1;
4541 for (offset_type idx : matches)
4542 {
4543 if (prev != idx)
4544 {
4545 match_callback (idx);
4546 prev = idx;
4547 }
4548 }
4549
4550 /* Above we use a type wider than idx's for 'prev', since 0 and
4551 (offset_type)-1 are both possible values. */
4552 static_assert (sizeof (prev) > sizeof (offset_type), "");
4553}
4554
c62446b1
PA
4555#if GDB_SELF_TEST
4556
4557namespace selftests { namespace dw2_expand_symtabs_matching {
4558
a3c5fafd
PA
4559/* A mock .gdb_index/.debug_names-like name index table, enough to
4560 exercise dw2_expand_symtabs_matching_symbol, which works with the
4561 mapped_index_base interface. Builds an index from the symbol list
4562 passed as parameter to the constructor. */
4563class mock_mapped_index : public mapped_index_base
c62446b1
PA
4564{
4565public:
a3c5fafd
PA
4566 mock_mapped_index (gdb::array_view<const char *> symbols)
4567 : m_symbol_table (symbols)
c62446b1
PA
4568 {}
4569
a3c5fafd 4570 DISABLE_COPY_AND_ASSIGN (mock_mapped_index);
c62446b1 4571
a3c5fafd 4572 /* Return the number of names in the symbol table. */
632e107b 4573 size_t symbol_name_count () const override
c62446b1 4574 {
a3c5fafd 4575 return m_symbol_table.size ();
c62446b1
PA
4576 }
4577
a3c5fafd 4578 /* Get the name of the symbol at IDX in the symbol table. */
632e107b 4579 const char *symbol_name_at (offset_type idx) const override
a3c5fafd
PA
4580 {
4581 return m_symbol_table[idx];
4582 }
c62446b1 4583
a3c5fafd
PA
4584private:
4585 gdb::array_view<const char *> m_symbol_table;
c62446b1
PA
4586};
4587
4588/* Convenience function that converts a NULL pointer to a "<null>"
4589 string, to pass to print routines. */
4590
4591static const char *
4592string_or_null (const char *str)
4593{
4594 return str != NULL ? str : "<null>";
4595}
4596
4597/* Check if a lookup_name_info built from
4598 NAME/MATCH_TYPE/COMPLETION_MODE matches the symbols in the mock
4599 index. EXPECTED_LIST is the list of expected matches, in expected
4600 matching order. If no match expected, then an empty list is
4601 specified. Returns true on success. On failure prints a warning
4602 indicating the file:line that failed, and returns false. */
4603
4604static bool
4605check_match (const char *file, int line,
4606 mock_mapped_index &mock_index,
4607 const char *name, symbol_name_match_type match_type,
4608 bool completion_mode,
4609 std::initializer_list<const char *> expected_list)
4610{
4611 lookup_name_info lookup_name (name, match_type, completion_mode);
4612
4613 bool matched = true;
4614
4615 auto mismatch = [&] (const char *expected_str,
4616 const char *got)
4617 {
4618 warning (_("%s:%d: match_type=%s, looking-for=\"%s\", "
4619 "expected=\"%s\", got=\"%s\"\n"),
4620 file, line,
4621 (match_type == symbol_name_match_type::FULL
4622 ? "FULL" : "WILD"),
4623 name, string_or_null (expected_str), string_or_null (got));
4624 matched = false;
4625 };
4626
4627 auto expected_it = expected_list.begin ();
4628 auto expected_end = expected_list.end ();
4629
a3c5fafd 4630 dw2_expand_symtabs_matching_symbol (mock_index, lookup_name,
c62446b1
PA
4631 NULL, ALL_DOMAIN,
4632 [&] (offset_type idx)
4633 {
a3c5fafd 4634 const char *matched_name = mock_index.symbol_name_at (idx);
c62446b1
PA
4635 const char *expected_str
4636 = expected_it == expected_end ? NULL : *expected_it++;
4637
4638 if (expected_str == NULL || strcmp (expected_str, matched_name) != 0)
4639 mismatch (expected_str, matched_name);
4640 });
4641
4642 const char *expected_str
4643 = expected_it == expected_end ? NULL : *expected_it++;
4644 if (expected_str != NULL)
4645 mismatch (expected_str, NULL);
4646
4647 return matched;
4648}
4649
4650/* The symbols added to the mock mapped_index for testing (in
4651 canonical form). */
4652static const char *test_symbols[] = {
4653 "function",
4654 "std::bar",
4655 "std::zfunction",
4656 "std::zfunction2",
4657 "w1::w2",
4658 "ns::foo<char*>",
4659 "ns::foo<int>",
4660 "ns::foo<long>",
a20714ff
PA
4661 "ns2::tmpl<int>::foo2",
4662 "(anonymous namespace)::A::B::C",
c62446b1 4663
e1ef7d7a
PA
4664 /* These are used to check that the increment-last-char in the
4665 matching algorithm for completion doesn't match "t1_fund" when
4666 completing "t1_func". */
4667 "t1_func",
4668 "t1_func1",
4669 "t1_fund",
4670 "t1_fund1",
4671
4672 /* A UTF-8 name with multi-byte sequences to make sure that
4673 cp-name-parser understands this as a single identifier ("função"
4674 is "function" in PT). */
4675 u8"u8função",
4676
4677 /* \377 (0xff) is Latin1 'ÿ'. */
4678 "yfunc\377",
4679
4680 /* \377 (0xff) is Latin1 'ÿ'. */
4681 "\377",
4682 "\377\377123",
4683
c62446b1
PA
4684 /* A name with all sorts of complications. Starts with "z" to make
4685 it easier for the completion tests below. */
4686#define Z_SYM_NAME \
4687 "z::std::tuple<(anonymous namespace)::ui*, std::bar<(anonymous namespace)::ui> >" \
4688 "::tuple<(anonymous namespace)::ui*, " \
4689 "std::default_delete<(anonymous namespace)::ui>, void>"
4690
4691 Z_SYM_NAME
4692};
4693
a3c5fafd
PA
4694/* Returns true if the mapped_index_base::find_name_component_bounds
4695 method finds EXPECTED_SYMS in INDEX when looking for SEARCH_NAME,
4696 in completion mode. */
5c58de74
PA
4697
4698static bool
a3c5fafd 4699check_find_bounds_finds (mapped_index_base &index,
5c58de74
PA
4700 const char *search_name,
4701 gdb::array_view<const char *> expected_syms)
4702{
4703 lookup_name_info lookup_name (search_name,
4704 symbol_name_match_type::FULL, true);
4705
4706 auto bounds = index.find_name_components_bounds (lookup_name);
4707
4708 size_t distance = std::distance (bounds.first, bounds.second);
4709 if (distance != expected_syms.size ())
4710 return false;
4711
4712 for (size_t exp_elem = 0; exp_elem < distance; exp_elem++)
4713 {
4714 auto nc_elem = bounds.first + exp_elem;
4715 const char *qualified = index.symbol_name_at (nc_elem->idx);
4716 if (strcmp (qualified, expected_syms[exp_elem]) != 0)
4717 return false;
4718 }
4719
4720 return true;
4721}
4722
4723/* Test the lower-level mapped_index::find_name_component_bounds
4724 method. */
4725
c62446b1 4726static void
5c58de74
PA
4727test_mapped_index_find_name_component_bounds ()
4728{
4729 mock_mapped_index mock_index (test_symbols);
4730
a3c5fafd 4731 mock_index.build_name_components ();
5c58de74
PA
4732
4733 /* Test the lower-level mapped_index::find_name_component_bounds
4734 method in completion mode. */
4735 {
4736 static const char *expected_syms[] = {
4737 "t1_func",
4738 "t1_func1",
5c58de74
PA
4739 };
4740
a3c5fafd 4741 SELF_CHECK (check_find_bounds_finds (mock_index,
5c58de74
PA
4742 "t1_func", expected_syms));
4743 }
4744
4745 /* Check that the increment-last-char in the name matching algorithm
4746 for completion doesn't get confused with Ansi1 'ÿ' / 0xff. */
4747 {
4748 static const char *expected_syms1[] = {
4749 "\377",
4750 "\377\377123",
4751 };
a3c5fafd 4752 SELF_CHECK (check_find_bounds_finds (mock_index,
5c58de74
PA
4753 "\377", expected_syms1));
4754
4755 static const char *expected_syms2[] = {
4756 "\377\377123",
4757 };
a3c5fafd 4758 SELF_CHECK (check_find_bounds_finds (mock_index,
5c58de74
PA
4759 "\377\377", expected_syms2));
4760 }
4761}
4762
4763/* Test dw2_expand_symtabs_matching_symbol. */
4764
4765static void
4766test_dw2_expand_symtabs_matching_symbol ()
c62446b1
PA
4767{
4768 mock_mapped_index mock_index (test_symbols);
4769
4770 /* We let all tests run until the end even if some fails, for debug
4771 convenience. */
4772 bool any_mismatch = false;
4773
4774 /* Create the expected symbols list (an initializer_list). Needed
4775 because lists have commas, and we need to pass them to CHECK,
4776 which is a macro. */
4777#define EXPECT(...) { __VA_ARGS__ }
4778
4779 /* Wrapper for check_match that passes down the current
4780 __FILE__/__LINE__. */
4781#define CHECK_MATCH(NAME, MATCH_TYPE, COMPLETION_MODE, EXPECTED_LIST) \
4782 any_mismatch |= !check_match (__FILE__, __LINE__, \
4783 mock_index, \
4784 NAME, MATCH_TYPE, COMPLETION_MODE, \
4785 EXPECTED_LIST)
4786
4787 /* Identity checks. */
4788 for (const char *sym : test_symbols)
4789 {
4790 /* Should be able to match all existing symbols. */
4791 CHECK_MATCH (sym, symbol_name_match_type::FULL, false,
4792 EXPECT (sym));
4793
4794 /* Should be able to match all existing symbols with
4795 parameters. */
4796 std::string with_params = std::string (sym) + "(int)";
4797 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4798 EXPECT (sym));
4799
4800 /* Should be able to match all existing symbols with
4801 parameters and qualifiers. */
4802 with_params = std::string (sym) + " ( int ) const";
4803 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4804 EXPECT (sym));
4805
4806 /* This should really find sym, but cp-name-parser.y doesn't
4807 know about lvalue/rvalue qualifiers yet. */
4808 with_params = std::string (sym) + " ( int ) &&";
4809 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4810 {});
4811 }
4812
e1ef7d7a
PA
4813 /* Check that the name matching algorithm for completion doesn't get
4814 confused with Latin1 'ÿ' / 0xff. */
4815 {
4816 static const char str[] = "\377";
4817 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4818 EXPECT ("\377", "\377\377123"));
4819 }
4820
4821 /* Check that the increment-last-char in the matching algorithm for
4822 completion doesn't match "t1_fund" when completing "t1_func". */
4823 {
4824 static const char str[] = "t1_func";
4825 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4826 EXPECT ("t1_func", "t1_func1"));
4827 }
4828
c62446b1
PA
4829 /* Check that completion mode works at each prefix of the expected
4830 symbol name. */
4831 {
4832 static const char str[] = "function(int)";
4833 size_t len = strlen (str);
4834 std::string lookup;
4835
4836 for (size_t i = 1; i < len; i++)
4837 {
4838 lookup.assign (str, i);
4839 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4840 EXPECT ("function"));
4841 }
4842 }
4843
4844 /* While "w" is a prefix of both components, the match function
4845 should still only be called once. */
4846 {
4847 CHECK_MATCH ("w", symbol_name_match_type::FULL, true,
4848 EXPECT ("w1::w2"));
a20714ff
PA
4849 CHECK_MATCH ("w", symbol_name_match_type::WILD, true,
4850 EXPECT ("w1::w2"));
c62446b1
PA
4851 }
4852
4853 /* Same, with a "complicated" symbol. */
4854 {
4855 static const char str[] = Z_SYM_NAME;
4856 size_t len = strlen (str);
4857 std::string lookup;
4858
4859 for (size_t i = 1; i < len; i++)
4860 {
4861 lookup.assign (str, i);
4862 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4863 EXPECT (Z_SYM_NAME));
4864 }
4865 }
4866
4867 /* In FULL mode, an incomplete symbol doesn't match. */
4868 {
4869 CHECK_MATCH ("std::zfunction(int", symbol_name_match_type::FULL, false,
4870 {});
4871 }
4872
4873 /* A complete symbol with parameters matches any overload, since the
4874 index has no overload info. */
4875 {
4876 CHECK_MATCH ("std::zfunction(int)", symbol_name_match_type::FULL, true,
4877 EXPECT ("std::zfunction", "std::zfunction2"));
a20714ff
PA
4878 CHECK_MATCH ("zfunction(int)", symbol_name_match_type::WILD, true,
4879 EXPECT ("std::zfunction", "std::zfunction2"));
4880 CHECK_MATCH ("zfunc", symbol_name_match_type::WILD, true,
4881 EXPECT ("std::zfunction", "std::zfunction2"));
c62446b1
PA
4882 }
4883
4884 /* Check that whitespace is ignored appropriately. A symbol with a
4885 template argument list. */
4886 {
4887 static const char expected[] = "ns::foo<int>";
4888 CHECK_MATCH ("ns :: foo < int > ", symbol_name_match_type::FULL, false,
4889 EXPECT (expected));
a20714ff
PA
4890 CHECK_MATCH ("foo < int > ", symbol_name_match_type::WILD, false,
4891 EXPECT (expected));
c62446b1
PA
4892 }
4893
4894 /* Check that whitespace is ignored appropriately. A symbol with a
4895 template argument list that includes a pointer. */
4896 {
4897 static const char expected[] = "ns::foo<char*>";
4898 /* Try both completion and non-completion modes. */
4899 static const bool completion_mode[2] = {false, true};
4900 for (size_t i = 0; i < 2; i++)
4901 {
4902 CHECK_MATCH ("ns :: foo < char * >", symbol_name_match_type::FULL,
4903 completion_mode[i], EXPECT (expected));
a20714ff
PA
4904 CHECK_MATCH ("foo < char * >", symbol_name_match_type::WILD,
4905 completion_mode[i], EXPECT (expected));
c62446b1
PA
4906
4907 CHECK_MATCH ("ns :: foo < char * > (int)", symbol_name_match_type::FULL,
4908 completion_mode[i], EXPECT (expected));
a20714ff
PA
4909 CHECK_MATCH ("foo < char * > (int)", symbol_name_match_type::WILD,
4910 completion_mode[i], EXPECT (expected));
c62446b1
PA
4911 }
4912 }
4913
4914 {
4915 /* Check method qualifiers are ignored. */
4916 static const char expected[] = "ns::foo<char*>";
4917 CHECK_MATCH ("ns :: foo < char * > ( int ) const",
4918 symbol_name_match_type::FULL, true, EXPECT (expected));
4919 CHECK_MATCH ("ns :: foo < char * > ( int ) &&",
4920 symbol_name_match_type::FULL, true, EXPECT (expected));
a20714ff
PA
4921 CHECK_MATCH ("foo < char * > ( int ) const",
4922 symbol_name_match_type::WILD, true, EXPECT (expected));
4923 CHECK_MATCH ("foo < char * > ( int ) &&",
4924 symbol_name_match_type::WILD, true, EXPECT (expected));
c62446b1
PA
4925 }
4926
4927 /* Test lookup names that don't match anything. */
4928 {
a20714ff
PA
4929 CHECK_MATCH ("bar2", symbol_name_match_type::WILD, false,
4930 {});
4931
c62446b1
PA
4932 CHECK_MATCH ("doesntexist", symbol_name_match_type::FULL, false,
4933 {});
4934 }
4935
a20714ff
PA
4936 /* Some wild matching tests, exercising "(anonymous namespace)",
4937 which should not be confused with a parameter list. */
4938 {
4939 static const char *syms[] = {
4940 "A::B::C",
4941 "B::C",
4942 "C",
4943 "A :: B :: C ( int )",
4944 "B :: C ( int )",
4945 "C ( int )",
4946 };
4947
4948 for (const char *s : syms)
4949 {
4950 CHECK_MATCH (s, symbol_name_match_type::WILD, false,
4951 EXPECT ("(anonymous namespace)::A::B::C"));
4952 }
4953 }
4954
4955 {
4956 static const char expected[] = "ns2::tmpl<int>::foo2";
4957 CHECK_MATCH ("tmp", symbol_name_match_type::WILD, true,
4958 EXPECT (expected));
4959 CHECK_MATCH ("tmpl<", symbol_name_match_type::WILD, true,
4960 EXPECT (expected));
4961 }
4962
c62446b1
PA
4963 SELF_CHECK (!any_mismatch);
4964
4965#undef EXPECT
4966#undef CHECK_MATCH
4967}
4968
5c58de74
PA
4969static void
4970run_test ()
4971{
4972 test_mapped_index_find_name_component_bounds ();
4973 test_dw2_expand_symtabs_matching_symbol ();
4974}
4975
c62446b1
PA
4976}} // namespace selftests::dw2_expand_symtabs_matching
4977
4978#endif /* GDB_SELF_TEST */
4979
4b514bc8
JK
4980/* If FILE_MATCHER is NULL or if PER_CU has
4981 dwarf2_per_cu_quick_data::MARK set (see
4982 dw_expand_symtabs_matching_file_matcher), expand the CU and call
4983 EXPANSION_NOTIFY on it. */
4984
4985static void
4986dw2_expand_symtabs_matching_one
4987 (struct dwarf2_per_cu_data *per_cu,
4988 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4989 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify)
4990{
4991 if (file_matcher == NULL || per_cu->v.quick->mark)
4992 {
4993 bool symtab_was_null
4994 = (per_cu->v.quick->compunit_symtab == NULL);
4995
58f0c718 4996 dw2_instantiate_symtab (per_cu, false);
4b514bc8
JK
4997
4998 if (expansion_notify != NULL
4999 && symtab_was_null
5000 && per_cu->v.quick->compunit_symtab != NULL)
5001 expansion_notify (per_cu->v.quick->compunit_symtab);
5002 }
5003}
5004
3f563c84
PA
5005/* Helper for dw2_expand_matching symtabs. Called on each symbol
5006 matched, to expand corresponding CUs that were marked. IDX is the
5007 index of the symbol name that matched. */
5008
5009static void
5010dw2_expand_marked_cus
ed2dc618 5011 (struct dwarf2_per_objfile *dwarf2_per_objfile, offset_type idx,
3f563c84
PA
5012 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
5013 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
5014 search_domain kind)
5015{
3f563c84
PA
5016 offset_type *vec, vec_len, vec_idx;
5017 bool global_seen = false;
ed2dc618 5018 mapped_index &index = *dwarf2_per_objfile->index_table;
3f563c84 5019
61920122 5020 vec = (offset_type *) (index.constant_pool
f00a2de2 5021 + MAYBE_SWAP (index.symbol_table[idx].vec));
61920122
PA
5022 vec_len = MAYBE_SWAP (vec[0]);
5023 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
5024 {
61920122
PA
5025 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[vec_idx + 1]);
5026 /* This value is only valid for index versions >= 7. */
5027 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
5028 gdb_index_symbol_kind symbol_kind =
5029 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
5030 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
5031 /* Only check the symbol attributes if they're present.
5032 Indices prior to version 7 don't record them,
5033 and indices >= 7 may elide them for certain symbols
5034 (gold does this). */
5035 int attrs_valid =
5036 (index.version >= 7
5037 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
5038
5039 /* Work around gold/15646. */
5040 if (attrs_valid)
9291a0cd 5041 {
61920122
PA
5042 if (!is_static && global_seen)
5043 continue;
5044 if (!is_static)
5045 global_seen = true;
5046 }
3190f0c6 5047
61920122
PA
5048 /* Only check the symbol's kind if it has one. */
5049 if (attrs_valid)
5050 {
5051 switch (kind)
8943b874 5052 {
61920122
PA
5053 case VARIABLES_DOMAIN:
5054 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
5055 continue;
5056 break;
5057 case FUNCTIONS_DOMAIN:
5058 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
8943b874 5059 continue;
61920122
PA
5060 break;
5061 case TYPES_DOMAIN:
5062 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
5063 continue;
5064 break;
5065 default:
5066 break;
8943b874 5067 }
61920122 5068 }
8943b874 5069
61920122 5070 /* Don't crash on bad data. */
b76e467d 5071 if (cu_index >= (dwarf2_per_objfile->all_comp_units.size ()
b2bdb8cf 5072 + dwarf2_per_objfile->all_type_units.size ()))
61920122 5073 {
b98664d3 5074 complaint (_(".gdb_index entry has bad CU index"
ed2dc618
SM
5075 " [in module %s]"),
5076 objfile_name (dwarf2_per_objfile->objfile));
61920122
PA
5077 continue;
5078 }
5079
ff4c9fec 5080 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (cu_index);
4b514bc8
JK
5081 dw2_expand_symtabs_matching_one (per_cu, file_matcher,
5082 expansion_notify);
61920122
PA
5083 }
5084}
5085
4b514bc8
JK
5086/* If FILE_MATCHER is non-NULL, set all the
5087 dwarf2_per_cu_quick_data::MARK of the current DWARF2_PER_OBJFILE
5088 that match FILE_MATCHER. */
5089
61920122 5090static void
4b514bc8 5091dw_expand_symtabs_matching_file_matcher
ed2dc618
SM
5092 (struct dwarf2_per_objfile *dwarf2_per_objfile,
5093 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher)
61920122 5094{
4b514bc8 5095 if (file_matcher == NULL)
61920122
PA
5096 return;
5097
4b514bc8
JK
5098 objfile *const objfile = dwarf2_per_objfile->objfile;
5099
5100 htab_up visited_found (htab_create_alloc (10, htab_hash_pointer,
5101 htab_eq_pointer,
5102 NULL, xcalloc, xfree));
5103 htab_up visited_not_found (htab_create_alloc (10, htab_hash_pointer,
61920122
PA
5104 htab_eq_pointer,
5105 NULL, xcalloc, xfree));
61920122 5106
4b514bc8
JK
5107 /* The rule is CUs specify all the files, including those used by
5108 any TU, so there's no need to scan TUs here. */
61920122 5109
b76e467d 5110 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
927aa2e7 5111 {
927aa2e7
JK
5112 QUIT;
5113
5114 per_cu->v.quick->mark = 0;
5115
5116 /* We only need to look at symtabs not already expanded. */
5117 if (per_cu->v.quick->compunit_symtab)
5118 continue;
5119
b76e467d 5120 quick_file_names *file_data = dw2_get_file_names (per_cu);
927aa2e7
JK
5121 if (file_data == NULL)
5122 continue;
5123
5124 if (htab_find (visited_not_found.get (), file_data) != NULL)
5125 continue;
5126 else if (htab_find (visited_found.get (), file_data) != NULL)
5127 {
5128 per_cu->v.quick->mark = 1;
5129 continue;
5130 }
5131
b76e467d 5132 for (int j = 0; j < file_data->num_file_names; ++j)
927aa2e7
JK
5133 {
5134 const char *this_real_name;
5135
5136 if (file_matcher (file_data->file_names[j], false))
5137 {
5138 per_cu->v.quick->mark = 1;
5139 break;
5140 }
5141
5142 /* Before we invoke realpath, which can get expensive when many
5143 files are involved, do a quick comparison of the basenames. */
5144 if (!basenames_may_differ
5145 && !file_matcher (lbasename (file_data->file_names[j]),
5146 true))
5147 continue;
5148
5149 this_real_name = dw2_get_real_path (objfile, file_data, j);
5150 if (file_matcher (this_real_name, false))
5151 {
5152 per_cu->v.quick->mark = 1;
5153 break;
5154 }
5155 }
5156
b76e467d
SM
5157 void **slot = htab_find_slot (per_cu->v.quick->mark
5158 ? visited_found.get ()
5159 : visited_not_found.get (),
5160 file_data, INSERT);
927aa2e7
JK
5161 *slot = file_data;
5162 }
5163}
5164
5165static void
5166dw2_expand_symtabs_matching
5167 (struct objfile *objfile,
5168 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
5169 const lookup_name_info &lookup_name,
5170 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
5171 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
5172 enum search_domain kind)
5173{
ed2dc618
SM
5174 struct dwarf2_per_objfile *dwarf2_per_objfile
5175 = get_dwarf2_per_objfile (objfile);
927aa2e7
JK
5176
5177 /* index_table is NULL if OBJF_READNOW. */
5178 if (!dwarf2_per_objfile->index_table)
5179 return;
5180
ed2dc618 5181 dw_expand_symtabs_matching_file_matcher (dwarf2_per_objfile, file_matcher);
927aa2e7
JK
5182
5183 mapped_index &index = *dwarf2_per_objfile->index_table;
5184
5185 dw2_expand_symtabs_matching_symbol (index, lookup_name,
5186 symbol_matcher,
5187 kind, [&] (offset_type idx)
5188 {
ed2dc618 5189 dw2_expand_marked_cus (dwarf2_per_objfile, idx, file_matcher,
927aa2e7
JK
5190 expansion_notify, kind);
5191 });
5192}
5193
5194/* A helper for dw2_find_pc_sect_compunit_symtab which finds the most specific
5195 symtab. */
5196
5197static struct compunit_symtab *
5198recursively_find_pc_sect_compunit_symtab (struct compunit_symtab *cust,
5199 CORE_ADDR pc)
5200{
5201 int i;
5202
5203 if (COMPUNIT_BLOCKVECTOR (cust) != NULL
5204 && blockvector_contains_pc (COMPUNIT_BLOCKVECTOR (cust), pc))
5205 return cust;
5206
5207 if (cust->includes == NULL)
5208 return NULL;
5209
5210 for (i = 0; cust->includes[i]; ++i)
5211 {
5212 struct compunit_symtab *s = cust->includes[i];
5213
5214 s = recursively_find_pc_sect_compunit_symtab (s, pc);
5215 if (s != NULL)
5216 return s;
5217 }
5218
5219 return NULL;
5220}
5221
5222static struct compunit_symtab *
5223dw2_find_pc_sect_compunit_symtab (struct objfile *objfile,
5224 struct bound_minimal_symbol msymbol,
5225 CORE_ADDR pc,
5226 struct obj_section *section,
5227 int warn_if_readin)
5228{
5229 struct dwarf2_per_cu_data *data;
5230 struct compunit_symtab *result;
5231
927aa2e7
JK
5232 if (!objfile->psymtabs_addrmap)
5233 return NULL;
5234
5235 data = (struct dwarf2_per_cu_data *) addrmap_find (objfile->psymtabs_addrmap,
5236 pc);
5237 if (!data)
5238 return NULL;
5239
5240 if (warn_if_readin && data->v.quick->compunit_symtab)
5241 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
5242 paddress (get_objfile_arch (objfile), pc));
5243
5244 result
58f0c718
TT
5245 = recursively_find_pc_sect_compunit_symtab (dw2_instantiate_symtab (data,
5246 false),
927aa2e7
JK
5247 pc);
5248 gdb_assert (result != NULL);
5249 return result;
5250}
5251
5252static void
5253dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
5254 void *data, int need_fullname)
5255{
ed2dc618
SM
5256 struct dwarf2_per_objfile *dwarf2_per_objfile
5257 = get_dwarf2_per_objfile (objfile);
927aa2e7
JK
5258
5259 if (!dwarf2_per_objfile->filenames_cache)
5260 {
5261 dwarf2_per_objfile->filenames_cache.emplace ();
5262
5263 htab_up visited (htab_create_alloc (10,
5264 htab_hash_pointer, htab_eq_pointer,
5265 NULL, xcalloc, xfree));
5266
5267 /* The rule is CUs specify all the files, including those used
5268 by any TU, so there's no need to scan TUs here. We can
5269 ignore file names coming from already-expanded CUs. */
5270
b76e467d 5271 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
927aa2e7 5272 {
927aa2e7
JK
5273 if (per_cu->v.quick->compunit_symtab)
5274 {
5275 void **slot = htab_find_slot (visited.get (),
5276 per_cu->v.quick->file_names,
5277 INSERT);
5278
5279 *slot = per_cu->v.quick->file_names;
5280 }
5281 }
5282
b76e467d 5283 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
927aa2e7 5284 {
927aa2e7
JK
5285 /* We only need to look at symtabs not already expanded. */
5286 if (per_cu->v.quick->compunit_symtab)
5287 continue;
5288
b76e467d 5289 quick_file_names *file_data = dw2_get_file_names (per_cu);
927aa2e7
JK
5290 if (file_data == NULL)
5291 continue;
5292
b76e467d 5293 void **slot = htab_find_slot (visited.get (), file_data, INSERT);
927aa2e7
JK
5294 if (*slot)
5295 {
5296 /* Already visited. */
5297 continue;
5298 }
5299 *slot = file_data;
5300
5301 for (int j = 0; j < file_data->num_file_names; ++j)
5302 {
5303 const char *filename = file_data->file_names[j];
5304 dwarf2_per_objfile->filenames_cache->seen (filename);
5305 }
5306 }
5307 }
5308
5309 dwarf2_per_objfile->filenames_cache->traverse ([&] (const char *filename)
5310 {
5311 gdb::unique_xmalloc_ptr<char> this_real_name;
5312
5313 if (need_fullname)
5314 this_real_name = gdb_realpath (filename);
5315 (*fun) (filename, this_real_name.get (), data);
5316 });
5317}
5318
5319static int
5320dw2_has_symbols (struct objfile *objfile)
5321{
5322 return 1;
5323}
5324
5325const struct quick_symbol_functions dwarf2_gdb_index_functions =
5326{
5327 dw2_has_symbols,
5328 dw2_find_last_source_symtab,
5329 dw2_forget_cached_source_info,
5330 dw2_map_symtabs_matching_filename,
5331 dw2_lookup_symbol,
5332 dw2_print_stats,
5333 dw2_dump,
5334 dw2_relocate,
5335 dw2_expand_symtabs_for_function,
5336 dw2_expand_all_symtabs,
5337 dw2_expand_symtabs_with_fullname,
5338 dw2_map_matching_symbols,
5339 dw2_expand_symtabs_matching,
5340 dw2_find_pc_sect_compunit_symtab,
5341 NULL,
5342 dw2_map_symbol_filenames
5343};
5344
5345/* DWARF-5 debug_names reader. */
5346
5347/* DWARF-5 augmentation string for GDB's DW_IDX_GNU_* extension. */
5348static const gdb_byte dwarf5_augmentation[] = { 'G', 'D', 'B', 0 };
5349
5350/* A helper function that reads the .debug_names section in SECTION
5351 and fills in MAP. FILENAME is the name of the file containing the
5352 section; it is used for error reporting.
5353
5354 Returns true if all went well, false otherwise. */
5355
5356static bool
5357read_debug_names_from_section (struct objfile *objfile,
5358 const char *filename,
5359 struct dwarf2_section_info *section,
5360 mapped_debug_names &map)
5361{
5362 if (dwarf2_section_empty_p (section))
5363 return false;
5364
5365 /* Older elfutils strip versions could keep the section in the main
5366 executable while splitting it for the separate debug info file. */
5367 if ((get_section_flags (section) & SEC_HAS_CONTENTS) == 0)
5368 return false;
5369
5370 dwarf2_read_section (objfile, section);
5371
5372 map.dwarf5_byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
5373
5374 const gdb_byte *addr = section->buffer;
5375
5376 bfd *const abfd = get_section_bfd_owner (section);
5377
5378 unsigned int bytes_read;
5379 LONGEST length = read_initial_length (abfd, addr, &bytes_read);
5380 addr += bytes_read;
5381
5382 map.dwarf5_is_dwarf64 = bytes_read != 4;
5383 map.offset_size = map.dwarf5_is_dwarf64 ? 8 : 4;
5384 if (bytes_read + length != section->size)
5385 {
5386 /* There may be multiple per-CU indices. */
5387 warning (_("Section .debug_names in %s length %s does not match "
5388 "section length %s, ignoring .debug_names."),
5389 filename, plongest (bytes_read + length),
5390 pulongest (section->size));
5391 return false;
5392 }
5393
5394 /* The version number. */
5395 uint16_t version = read_2_bytes (abfd, addr);
5396 addr += 2;
5397 if (version != 5)
5398 {
5399 warning (_("Section .debug_names in %s has unsupported version %d, "
5400 "ignoring .debug_names."),
5401 filename, version);
5402 return false;
5403 }
5404
5405 /* Padding. */
5406 uint16_t padding = read_2_bytes (abfd, addr);
5407 addr += 2;
5408 if (padding != 0)
5409 {
5410 warning (_("Section .debug_names in %s has unsupported padding %d, "
5411 "ignoring .debug_names."),
5412 filename, padding);
5413 return false;
5414 }
5415
5416 /* comp_unit_count - The number of CUs in the CU list. */
5417 map.cu_count = read_4_bytes (abfd, addr);
5418 addr += 4;
5419
5420 /* local_type_unit_count - The number of TUs in the local TU
5421 list. */
5422 map.tu_count = read_4_bytes (abfd, addr);
5423 addr += 4;
5424
5425 /* foreign_type_unit_count - The number of TUs in the foreign TU
5426 list. */
5427 uint32_t foreign_tu_count = read_4_bytes (abfd, addr);
5428 addr += 4;
5429 if (foreign_tu_count != 0)
5430 {
5431 warning (_("Section .debug_names in %s has unsupported %lu foreign TUs, "
5432 "ignoring .debug_names."),
5433 filename, static_cast<unsigned long> (foreign_tu_count));
5434 return false;
5435 }
5436
5437 /* bucket_count - The number of hash buckets in the hash lookup
5438 table. */
5439 map.bucket_count = read_4_bytes (abfd, addr);
5440 addr += 4;
5441
5442 /* name_count - The number of unique names in the index. */
5443 map.name_count = read_4_bytes (abfd, addr);
5444 addr += 4;
5445
5446 /* abbrev_table_size - The size in bytes of the abbreviations
5447 table. */
5448 uint32_t abbrev_table_size = read_4_bytes (abfd, addr);
5449 addr += 4;
5450
5451 /* augmentation_string_size - The size in bytes of the augmentation
5452 string. This value is rounded up to a multiple of 4. */
5453 uint32_t augmentation_string_size = read_4_bytes (abfd, addr);
5454 addr += 4;
5455 map.augmentation_is_gdb = ((augmentation_string_size
5456 == sizeof (dwarf5_augmentation))
5457 && memcmp (addr, dwarf5_augmentation,
5458 sizeof (dwarf5_augmentation)) == 0);
5459 augmentation_string_size += (-augmentation_string_size) & 3;
5460 addr += augmentation_string_size;
5461
5462 /* List of CUs */
5463 map.cu_table_reordered = addr;
5464 addr += map.cu_count * map.offset_size;
5465
5466 /* List of Local TUs */
5467 map.tu_table_reordered = addr;
5468 addr += map.tu_count * map.offset_size;
5469
5470 /* Hash Lookup Table */
5471 map.bucket_table_reordered = reinterpret_cast<const uint32_t *> (addr);
5472 addr += map.bucket_count * 4;
5473 map.hash_table_reordered = reinterpret_cast<const uint32_t *> (addr);
5474 addr += map.name_count * 4;
5475
5476 /* Name Table */
5477 map.name_table_string_offs_reordered = addr;
5478 addr += map.name_count * map.offset_size;
5479 map.name_table_entry_offs_reordered = addr;
5480 addr += map.name_count * map.offset_size;
5481
5482 const gdb_byte *abbrev_table_start = addr;
5483 for (;;)
5484 {
5485 unsigned int bytes_read;
5486 const ULONGEST index_num = read_unsigned_leb128 (abfd, addr, &bytes_read);
5487 addr += bytes_read;
5488 if (index_num == 0)
5489 break;
5490
5491 const auto insertpair
5492 = map.abbrev_map.emplace (index_num, mapped_debug_names::index_val ());
5493 if (!insertpair.second)
5494 {
5495 warning (_("Section .debug_names in %s has duplicate index %s, "
5496 "ignoring .debug_names."),
5497 filename, pulongest (index_num));
5498 return false;
5499 }
5500 mapped_debug_names::index_val &indexval = insertpair.first->second;
5501 indexval.dwarf_tag = read_unsigned_leb128 (abfd, addr, &bytes_read);
5502 addr += bytes_read;
5503
5504 for (;;)
5505 {
5506 mapped_debug_names::index_val::attr attr;
5507 attr.dw_idx = read_unsigned_leb128 (abfd, addr, &bytes_read);
5508 addr += bytes_read;
5509 attr.form = read_unsigned_leb128 (abfd, addr, &bytes_read);
5510 addr += bytes_read;
5511 if (attr.form == DW_FORM_implicit_const)
5512 {
5513 attr.implicit_const = read_signed_leb128 (abfd, addr,
5514 &bytes_read);
5515 addr += bytes_read;
5516 }
5517 if (attr.dw_idx == 0 && attr.form == 0)
5518 break;
5519 indexval.attr_vec.push_back (std::move (attr));
5520 }
5521 }
5522 if (addr != abbrev_table_start + abbrev_table_size)
5523 {
5524 warning (_("Section .debug_names in %s has abbreviation_table "
5525 "of size %zu vs. written as %u, ignoring .debug_names."),
5526 filename, addr - abbrev_table_start, abbrev_table_size);
5527 return false;
5528 }
5529 map.entry_pool = addr;
5530
5531 return true;
5532}
5533
5534/* A helper for create_cus_from_debug_names that handles the MAP's CU
5535 list. */
5536
5537static void
ed2dc618 5538create_cus_from_debug_names_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
927aa2e7
JK
5539 const mapped_debug_names &map,
5540 dwarf2_section_info &section,
b76e467d 5541 bool is_dwz)
927aa2e7
JK
5542{
5543 sect_offset sect_off_prev;
5544 for (uint32_t i = 0; i <= map.cu_count; ++i)
5545 {
5546 sect_offset sect_off_next;
5547 if (i < map.cu_count)
5548 {
5549 sect_off_next
5550 = (sect_offset) (extract_unsigned_integer
5551 (map.cu_table_reordered + i * map.offset_size,
5552 map.offset_size,
5553 map.dwarf5_byte_order));
5554 }
5555 else
5556 sect_off_next = (sect_offset) section.size;
5557 if (i >= 1)
5558 {
5559 const ULONGEST length = sect_off_next - sect_off_prev;
b76e467d 5560 dwarf2_per_cu_data *per_cu
ed2dc618 5561 = create_cu_from_index_list (dwarf2_per_objfile, &section, is_dwz,
927aa2e7 5562 sect_off_prev, length);
b76e467d 5563 dwarf2_per_objfile->all_comp_units.push_back (per_cu);
927aa2e7
JK
5564 }
5565 sect_off_prev = sect_off_next;
5566 }
5567}
5568
5569/* Read the CU list from the mapped index, and use it to create all
ed2dc618 5570 the CU objects for this dwarf2_per_objfile. */
927aa2e7
JK
5571
5572static void
ed2dc618 5573create_cus_from_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile,
927aa2e7
JK
5574 const mapped_debug_names &map,
5575 const mapped_debug_names &dwz_map)
5576{
b76e467d
SM
5577 gdb_assert (dwarf2_per_objfile->all_comp_units.empty ());
5578 dwarf2_per_objfile->all_comp_units.reserve (map.cu_count + dwz_map.cu_count);
927aa2e7 5579
ed2dc618
SM
5580 create_cus_from_debug_names_list (dwarf2_per_objfile, map,
5581 dwarf2_per_objfile->info,
b76e467d 5582 false /* is_dwz */);
927aa2e7
JK
5583
5584 if (dwz_map.cu_count == 0)
5585 return;
5586
ed2dc618
SM
5587 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
5588 create_cus_from_debug_names_list (dwarf2_per_objfile, dwz_map, dwz->info,
b76e467d 5589 true /* is_dwz */);
927aa2e7
JK
5590}
5591
5592/* Read .debug_names. If everything went ok, initialize the "quick"
5593 elements of all the CUs and return true. Otherwise, return false. */
5594
5595static bool
ed2dc618 5596dwarf2_read_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile)
927aa2e7 5597{
22ca247e
TT
5598 std::unique_ptr<mapped_debug_names> map
5599 (new mapped_debug_names (dwarf2_per_objfile));
ed2dc618
SM
5600 mapped_debug_names dwz_map (dwarf2_per_objfile);
5601 struct objfile *objfile = dwarf2_per_objfile->objfile;
927aa2e7
JK
5602
5603 if (!read_debug_names_from_section (objfile, objfile_name (objfile),
5604 &dwarf2_per_objfile->debug_names,
22ca247e 5605 *map))
927aa2e7
JK
5606 return false;
5607
5608 /* Don't use the index if it's empty. */
22ca247e 5609 if (map->name_count == 0)
927aa2e7
JK
5610 return false;
5611
5612 /* If there is a .dwz file, read it so we can get its CU list as
5613 well. */
ed2dc618 5614 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
927aa2e7
JK
5615 if (dwz != NULL)
5616 {
5617 if (!read_debug_names_from_section (objfile,
5618 bfd_get_filename (dwz->dwz_bfd),
5619 &dwz->debug_names, dwz_map))
5620 {
5621 warning (_("could not read '.debug_names' section from %s; skipping"),
5622 bfd_get_filename (dwz->dwz_bfd));
5623 return false;
5624 }
5625 }
5626
22ca247e 5627 create_cus_from_debug_names (dwarf2_per_objfile, *map, dwz_map);
927aa2e7 5628
22ca247e 5629 if (map->tu_count != 0)
927aa2e7
JK
5630 {
5631 /* We can only handle a single .debug_types when we have an
5632 index. */
5633 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) != 1)
5634 return false;
5635
5636 dwarf2_section_info *section = VEC_index (dwarf2_section_info_def,
5637 dwarf2_per_objfile->types, 0);
5638
5639 create_signatured_type_table_from_debug_names
22ca247e 5640 (dwarf2_per_objfile, *map, section, &dwarf2_per_objfile->abbrev);
927aa2e7
JK
5641 }
5642
ed2dc618
SM
5643 create_addrmap_from_aranges (dwarf2_per_objfile,
5644 &dwarf2_per_objfile->debug_aranges);
927aa2e7 5645
22ca247e 5646 dwarf2_per_objfile->debug_names_table = std::move (map);
927aa2e7
JK
5647 dwarf2_per_objfile->using_index = 1;
5648 dwarf2_per_objfile->quick_file_names_table =
b76e467d 5649 create_quick_file_names_table (dwarf2_per_objfile->all_comp_units.size ());
927aa2e7
JK
5650
5651 return true;
5652}
5653
927aa2e7
JK
5654/* Type used to manage iterating over all CUs looking for a symbol for
5655 .debug_names. */
5656
5657class dw2_debug_names_iterator
5658{
5659public:
5660 /* If WANT_SPECIFIC_BLOCK is true, only look for symbols in block
5661 BLOCK_INDEX. Otherwise BLOCK_INDEX is ignored. */
5662 dw2_debug_names_iterator (const mapped_debug_names &map,
5663 bool want_specific_block,
5664 block_enum block_index, domain_enum domain,
5665 const char *name)
5666 : m_map (map), m_want_specific_block (want_specific_block),
5667 m_block_index (block_index), m_domain (domain),
5668 m_addr (find_vec_in_debug_names (map, name))
5669 {}
5670
5671 dw2_debug_names_iterator (const mapped_debug_names &map,
5672 search_domain search, uint32_t namei)
5673 : m_map (map),
5674 m_search (search),
5675 m_addr (find_vec_in_debug_names (map, namei))
5676 {}
5677
5678 /* Return the next matching CU or NULL if there are no more. */
5679 dwarf2_per_cu_data *next ();
5680
5681private:
5682 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5683 const char *name);
5684 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5685 uint32_t namei);
5686
5687 /* The internalized form of .debug_names. */
5688 const mapped_debug_names &m_map;
5689
5690 /* If true, only look for symbols that match BLOCK_INDEX. */
5691 const bool m_want_specific_block = false;
5692
5693 /* One of GLOBAL_BLOCK or STATIC_BLOCK.
5694 Unused if !WANT_SPECIFIC_BLOCK - FIRST_LOCAL_BLOCK is an invalid
5695 value. */
5696 const block_enum m_block_index = FIRST_LOCAL_BLOCK;
5697
5698 /* The kind of symbol we're looking for. */
5699 const domain_enum m_domain = UNDEF_DOMAIN;
5700 const search_domain m_search = ALL_DOMAIN;
5701
5702 /* The list of CUs from the index entry of the symbol, or NULL if
5703 not found. */
5704 const gdb_byte *m_addr;
5705};
5706
5707const char *
5708mapped_debug_names::namei_to_name (uint32_t namei) const
5709{
5710 const ULONGEST namei_string_offs
5711 = extract_unsigned_integer ((name_table_string_offs_reordered
5712 + namei * offset_size),
5713 offset_size,
5714 dwarf5_byte_order);
5715 return read_indirect_string_at_offset
ed2dc618 5716 (dwarf2_per_objfile, dwarf2_per_objfile->objfile->obfd, namei_string_offs);
927aa2e7
JK
5717}
5718
5719/* Find a slot in .debug_names for the object named NAME. If NAME is
5720 found, return pointer to its pool data. If NAME cannot be found,
5721 return NULL. */
5722
5723const gdb_byte *
5724dw2_debug_names_iterator::find_vec_in_debug_names
5725 (const mapped_debug_names &map, const char *name)
5726{
5727 int (*cmp) (const char *, const char *);
5728
5729 if (current_language->la_language == language_cplus
5730 || current_language->la_language == language_fortran
5731 || current_language->la_language == language_d)
5732 {
5733 /* NAME is already canonical. Drop any qualifiers as
5734 .debug_names does not contain any. */
5735
5736 if (strchr (name, '(') != NULL)
5737 {
5738 gdb::unique_xmalloc_ptr<char> without_params
5739 = cp_remove_params (name);
5740
5741 if (without_params != NULL)
5742 {
5743 name = without_params.get();
5744 }
5745 }
5746 }
5747
5748 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
5749
5750 const uint32_t full_hash = dwarf5_djb_hash (name);
5751 uint32_t namei
5752 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5753 (map.bucket_table_reordered
5754 + (full_hash % map.bucket_count)), 4,
5755 map.dwarf5_byte_order);
5756 if (namei == 0)
5757 return NULL;
5758 --namei;
5759 if (namei >= map.name_count)
5760 {
b98664d3 5761 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
927aa2e7
JK
5762 "[in module %s]"),
5763 namei, map.name_count,
ed2dc618 5764 objfile_name (map.dwarf2_per_objfile->objfile));
927aa2e7
JK
5765 return NULL;
5766 }
5767
5768 for (;;)
5769 {
5770 const uint32_t namei_full_hash
5771 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5772 (map.hash_table_reordered + namei), 4,
5773 map.dwarf5_byte_order);
5774 if (full_hash % map.bucket_count != namei_full_hash % map.bucket_count)
5775 return NULL;
5776
5777 if (full_hash == namei_full_hash)
5778 {
5779 const char *const namei_string = map.namei_to_name (namei);
5780
5781#if 0 /* An expensive sanity check. */
5782 if (namei_full_hash != dwarf5_djb_hash (namei_string))
5783 {
b98664d3 5784 complaint (_("Wrong .debug_names hash for string at index %u "
927aa2e7
JK
5785 "[in module %s]"),
5786 namei, objfile_name (dwarf2_per_objfile->objfile));
5787 return NULL;
5788 }
5789#endif
5790
5791 if (cmp (namei_string, name) == 0)
5792 {
5793 const ULONGEST namei_entry_offs
5794 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5795 + namei * map.offset_size),
5796 map.offset_size, map.dwarf5_byte_order);
5797 return map.entry_pool + namei_entry_offs;
5798 }
5799 }
5800
5801 ++namei;
5802 if (namei >= map.name_count)
5803 return NULL;
5804 }
5805}
5806
5807const gdb_byte *
5808dw2_debug_names_iterator::find_vec_in_debug_names
5809 (const mapped_debug_names &map, uint32_t namei)
5810{
5811 if (namei >= map.name_count)
5812 {
b98664d3 5813 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
927aa2e7
JK
5814 "[in module %s]"),
5815 namei, map.name_count,
ed2dc618 5816 objfile_name (map.dwarf2_per_objfile->objfile));
927aa2e7
JK
5817 return NULL;
5818 }
5819
5820 const ULONGEST namei_entry_offs
5821 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5822 + namei * map.offset_size),
5823 map.offset_size, map.dwarf5_byte_order);
5824 return map.entry_pool + namei_entry_offs;
5825}
5826
5827/* See dw2_debug_names_iterator. */
5828
5829dwarf2_per_cu_data *
5830dw2_debug_names_iterator::next ()
5831{
5832 if (m_addr == NULL)
5833 return NULL;
5834
ed2dc618
SM
5835 struct dwarf2_per_objfile *dwarf2_per_objfile = m_map.dwarf2_per_objfile;
5836 struct objfile *objfile = dwarf2_per_objfile->objfile;
5837 bfd *const abfd = objfile->obfd;
927aa2e7
JK
5838
5839 again:
5840
5841 unsigned int bytes_read;
5842 const ULONGEST abbrev = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5843 m_addr += bytes_read;
5844 if (abbrev == 0)
5845 return NULL;
5846
5847 const auto indexval_it = m_map.abbrev_map.find (abbrev);
5848 if (indexval_it == m_map.abbrev_map.cend ())
5849 {
b98664d3 5850 complaint (_("Wrong .debug_names undefined abbrev code %s "
927aa2e7 5851 "[in module %s]"),
ed2dc618 5852 pulongest (abbrev), objfile_name (objfile));
927aa2e7
JK
5853 return NULL;
5854 }
5855 const mapped_debug_names::index_val &indexval = indexval_it->second;
5856 bool have_is_static = false;
5857 bool is_static;
5858 dwarf2_per_cu_data *per_cu = NULL;
5859 for (const mapped_debug_names::index_val::attr &attr : indexval.attr_vec)
5860 {
5861 ULONGEST ull;
5862 switch (attr.form)
5863 {
5864 case DW_FORM_implicit_const:
5865 ull = attr.implicit_const;
5866 break;
5867 case DW_FORM_flag_present:
5868 ull = 1;
5869 break;
5870 case DW_FORM_udata:
5871 ull = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5872 m_addr += bytes_read;
5873 break;
5874 default:
b98664d3 5875 complaint (_("Unsupported .debug_names form %s [in module %s]"),
927aa2e7 5876 dwarf_form_name (attr.form),
ed2dc618 5877 objfile_name (objfile));
927aa2e7
JK
5878 return NULL;
5879 }
5880 switch (attr.dw_idx)
5881 {
5882 case DW_IDX_compile_unit:
5883 /* Don't crash on bad data. */
b76e467d 5884 if (ull >= dwarf2_per_objfile->all_comp_units.size ())
927aa2e7 5885 {
b98664d3 5886 complaint (_(".debug_names entry has bad CU index %s"
927aa2e7
JK
5887 " [in module %s]"),
5888 pulongest (ull),
5889 objfile_name (dwarf2_per_objfile->objfile));
5890 continue;
5891 }
ff4c9fec 5892 per_cu = dwarf2_per_objfile->get_cutu (ull);
927aa2e7 5893 break;
8af5c486
JK
5894 case DW_IDX_type_unit:
5895 /* Don't crash on bad data. */
b2bdb8cf 5896 if (ull >= dwarf2_per_objfile->all_type_units.size ())
8af5c486 5897 {
b98664d3 5898 complaint (_(".debug_names entry has bad TU index %s"
8af5c486
JK
5899 " [in module %s]"),
5900 pulongest (ull),
5901 objfile_name (dwarf2_per_objfile->objfile));
5902 continue;
5903 }
ff4c9fec 5904 per_cu = &dwarf2_per_objfile->get_tu (ull)->per_cu;
8af5c486 5905 break;
927aa2e7
JK
5906 case DW_IDX_GNU_internal:
5907 if (!m_map.augmentation_is_gdb)
5908 break;
5909 have_is_static = true;
5910 is_static = true;
5911 break;
5912 case DW_IDX_GNU_external:
5913 if (!m_map.augmentation_is_gdb)
5914 break;
5915 have_is_static = true;
5916 is_static = false;
5917 break;
5918 }
5919 }
5920
5921 /* Skip if already read in. */
5922 if (per_cu->v.quick->compunit_symtab)
5923 goto again;
5924
5925 /* Check static vs global. */
5926 if (have_is_static)
5927 {
5928 const bool want_static = m_block_index != GLOBAL_BLOCK;
5929 if (m_want_specific_block && want_static != is_static)
5930 goto again;
5931 }
5932
5933 /* Match dw2_symtab_iter_next, symbol_kind
5934 and debug_names::psymbol_tag. */
5935 switch (m_domain)
5936 {
5937 case VAR_DOMAIN:
5938 switch (indexval.dwarf_tag)
5939 {
5940 case DW_TAG_variable:
5941 case DW_TAG_subprogram:
5942 /* Some types are also in VAR_DOMAIN. */
5943 case DW_TAG_typedef:
5944 case DW_TAG_structure_type:
5945 break;
5946 default:
5947 goto again;
5948 }
5949 break;
5950 case STRUCT_DOMAIN:
5951 switch (indexval.dwarf_tag)
5952 {
5953 case DW_TAG_typedef:
5954 case DW_TAG_structure_type:
5955 break;
5956 default:
5957 goto again;
5958 }
5959 break;
5960 case LABEL_DOMAIN:
5961 switch (indexval.dwarf_tag)
5962 {
5963 case 0:
5964 case DW_TAG_variable:
5965 break;
5966 default:
5967 goto again;
5968 }
5969 break;
5970 default:
5971 break;
5972 }
5973
5974 /* Match dw2_expand_symtabs_matching, symbol_kind and
5975 debug_names::psymbol_tag. */
5976 switch (m_search)
4b514bc8 5977 {
927aa2e7
JK
5978 case VARIABLES_DOMAIN:
5979 switch (indexval.dwarf_tag)
4b514bc8 5980 {
927aa2e7
JK
5981 case DW_TAG_variable:
5982 break;
5983 default:
5984 goto again;
4b514bc8 5985 }
927aa2e7
JK
5986 break;
5987 case FUNCTIONS_DOMAIN:
5988 switch (indexval.dwarf_tag)
4b514bc8 5989 {
927aa2e7
JK
5990 case DW_TAG_subprogram:
5991 break;
5992 default:
5993 goto again;
4b514bc8 5994 }
927aa2e7
JK
5995 break;
5996 case TYPES_DOMAIN:
5997 switch (indexval.dwarf_tag)
5998 {
5999 case DW_TAG_typedef:
6000 case DW_TAG_structure_type:
6001 break;
6002 default:
6003 goto again;
6004 }
6005 break;
6006 default:
6007 break;
4b514bc8 6008 }
927aa2e7
JK
6009
6010 return per_cu;
4b514bc8 6011}
61920122 6012
927aa2e7
JK
6013static struct compunit_symtab *
6014dw2_debug_names_lookup_symbol (struct objfile *objfile, int block_index_int,
6015 const char *name, domain_enum domain)
4b514bc8 6016{
927aa2e7 6017 const block_enum block_index = static_cast<block_enum> (block_index_int);
ed2dc618
SM
6018 struct dwarf2_per_objfile *dwarf2_per_objfile
6019 = get_dwarf2_per_objfile (objfile);
61920122 6020
927aa2e7
JK
6021 const auto &mapp = dwarf2_per_objfile->debug_names_table;
6022 if (!mapp)
61920122 6023 {
927aa2e7
JK
6024 /* index is NULL if OBJF_READNOW. */
6025 return NULL;
6026 }
6027 const auto &map = *mapp;
9291a0cd 6028
927aa2e7
JK
6029 dw2_debug_names_iterator iter (map, true /* want_specific_block */,
6030 block_index, domain, name);
9703b513 6031
927aa2e7
JK
6032 struct compunit_symtab *stab_best = NULL;
6033 struct dwarf2_per_cu_data *per_cu;
6034 while ((per_cu = iter.next ()) != NULL)
6035 {
6036 struct symbol *sym, *with_opaque = NULL;
58f0c718 6037 struct compunit_symtab *stab = dw2_instantiate_symtab (per_cu, false);
927aa2e7
JK
6038 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
6039 struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
9703b513 6040
927aa2e7
JK
6041 sym = block_find_symbol (block, name, domain,
6042 block_find_non_opaque_type_preferred,
6043 &with_opaque);
9703b513 6044
927aa2e7
JK
6045 /* Some caution must be observed with overloaded functions and
6046 methods, since the index will not contain any overload
6047 information (but NAME might contain it). */
a3ec0bb1 6048
927aa2e7
JK
6049 if (sym != NULL
6050 && strcmp_iw (SYMBOL_SEARCH_NAME (sym), name) == 0)
6051 return stab;
6052 if (with_opaque != NULL
6053 && strcmp_iw (SYMBOL_SEARCH_NAME (with_opaque), name) == 0)
6054 stab_best = stab;
9703b513 6055
927aa2e7 6056 /* Keep looking through other CUs. */
9703b513
TT
6057 }
6058
927aa2e7 6059 return stab_best;
9703b513
TT
6060}
6061
927aa2e7
JK
6062/* This dumps minimal information about .debug_names. It is called
6063 via "mt print objfiles". The gdb.dwarf2/gdb-index.exp testcase
6064 uses this to verify that .debug_names has been loaded. */
9291a0cd 6065
927aa2e7
JK
6066static void
6067dw2_debug_names_dump (struct objfile *objfile)
6068{
ed2dc618
SM
6069 struct dwarf2_per_objfile *dwarf2_per_objfile
6070 = get_dwarf2_per_objfile (objfile);
6071
927aa2e7
JK
6072 gdb_assert (dwarf2_per_objfile->using_index);
6073 printf_filtered (".debug_names:");
6074 if (dwarf2_per_objfile->debug_names_table)
6075 printf_filtered (" exists\n");
6076 else
6077 printf_filtered (" faked for \"readnow\"\n");
6078 printf_filtered ("\n");
9291a0cd
TT
6079}
6080
9291a0cd 6081static void
927aa2e7
JK
6082dw2_debug_names_expand_symtabs_for_function (struct objfile *objfile,
6083 const char *func_name)
9291a0cd 6084{
ed2dc618
SM
6085 struct dwarf2_per_objfile *dwarf2_per_objfile
6086 = get_dwarf2_per_objfile (objfile);
ae2de4f8 6087
927aa2e7
JK
6088 /* dwarf2_per_objfile->debug_names_table is NULL if OBJF_READNOW. */
6089 if (dwarf2_per_objfile->debug_names_table)
24c79950 6090 {
927aa2e7 6091 const mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
24c79950 6092
927aa2e7
JK
6093 /* Note: It doesn't matter what we pass for block_index here. */
6094 dw2_debug_names_iterator iter (map, false /* want_specific_block */,
6095 GLOBAL_BLOCK, VAR_DOMAIN, func_name);
24c79950 6096
927aa2e7
JK
6097 struct dwarf2_per_cu_data *per_cu;
6098 while ((per_cu = iter.next ()) != NULL)
58f0c718 6099 dw2_instantiate_symtab (per_cu, false);
927aa2e7
JK
6100 }
6101}
24c79950 6102
927aa2e7
JK
6103static void
6104dw2_debug_names_expand_symtabs_matching
6105 (struct objfile *objfile,
6106 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
6107 const lookup_name_info &lookup_name,
6108 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
6109 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
6110 enum search_domain kind)
6111{
ed2dc618
SM
6112 struct dwarf2_per_objfile *dwarf2_per_objfile
6113 = get_dwarf2_per_objfile (objfile);
9291a0cd 6114
927aa2e7
JK
6115 /* debug_names_table is NULL if OBJF_READNOW. */
6116 if (!dwarf2_per_objfile->debug_names_table)
6117 return;
9291a0cd 6118
ed2dc618 6119 dw_expand_symtabs_matching_file_matcher (dwarf2_per_objfile, file_matcher);
24c79950 6120
44ed8f3e 6121 mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
bbf2f4df 6122
44ed8f3e
PA
6123 dw2_expand_symtabs_matching_symbol (map, lookup_name,
6124 symbol_matcher,
6125 kind, [&] (offset_type namei)
927aa2e7 6126 {
927aa2e7
JK
6127 /* The name was matched, now expand corresponding CUs that were
6128 marked. */
6129 dw2_debug_names_iterator iter (map, kind, namei);
bbf2f4df 6130
927aa2e7
JK
6131 struct dwarf2_per_cu_data *per_cu;
6132 while ((per_cu = iter.next ()) != NULL)
6133 dw2_expand_symtabs_matching_one (per_cu, file_matcher,
6134 expansion_notify);
44ed8f3e 6135 });
9291a0cd
TT
6136}
6137
927aa2e7 6138const struct quick_symbol_functions dwarf2_debug_names_functions =
9291a0cd
TT
6139{
6140 dw2_has_symbols,
6141 dw2_find_last_source_symtab,
6142 dw2_forget_cached_source_info,
f8eba3c6 6143 dw2_map_symtabs_matching_filename,
927aa2e7 6144 dw2_debug_names_lookup_symbol,
9291a0cd 6145 dw2_print_stats,
927aa2e7 6146 dw2_debug_names_dump,
9291a0cd 6147 dw2_relocate,
927aa2e7 6148 dw2_debug_names_expand_symtabs_for_function,
9291a0cd 6149 dw2_expand_all_symtabs,
652a8996 6150 dw2_expand_symtabs_with_fullname,
40658b94 6151 dw2_map_matching_symbols,
927aa2e7 6152 dw2_debug_names_expand_symtabs_matching,
43f3e411 6153 dw2_find_pc_sect_compunit_symtab,
71a3c369 6154 NULL,
9291a0cd
TT
6155 dw2_map_symbol_filenames
6156};
6157
3c0aa29a 6158/* See symfile.h. */
9291a0cd 6159
3c0aa29a
PA
6160bool
6161dwarf2_initialize_objfile (struct objfile *objfile, dw_index_kind *index_kind)
9291a0cd 6162{
ed2dc618
SM
6163 struct dwarf2_per_objfile *dwarf2_per_objfile
6164 = get_dwarf2_per_objfile (objfile);
6165
9291a0cd
TT
6166 /* If we're about to read full symbols, don't bother with the
6167 indices. In this case we also don't care if some other debug
6168 format is making psymtabs, because they are all about to be
6169 expanded anyway. */
6170 if ((objfile->flags & OBJF_READNOW))
6171 {
9291a0cd 6172 dwarf2_per_objfile->using_index = 1;
ed2dc618
SM
6173 create_all_comp_units (dwarf2_per_objfile);
6174 create_all_type_units (dwarf2_per_objfile);
b76e467d
SM
6175 dwarf2_per_objfile->quick_file_names_table
6176 = create_quick_file_names_table
6177 (dwarf2_per_objfile->all_comp_units.size ());
9291a0cd 6178
b76e467d 6179 for (int i = 0; i < (dwarf2_per_objfile->all_comp_units.size ()
b2bdb8cf 6180 + dwarf2_per_objfile->all_type_units.size ()); ++i)
9291a0cd 6181 {
ff4c9fec 6182 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (i);
9291a0cd 6183
e254ef6a
DE
6184 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6185 struct dwarf2_per_cu_quick_data);
9291a0cd
TT
6186 }
6187
6188 /* Return 1 so that gdb sees the "quick" functions. However,
6189 these functions will be no-ops because we will have expanded
6190 all symtabs. */
3c0aa29a
PA
6191 *index_kind = dw_index_kind::GDB_INDEX;
6192 return true;
9291a0cd
TT
6193 }
6194
ed2dc618 6195 if (dwarf2_read_debug_names (dwarf2_per_objfile))
3c0aa29a
PA
6196 {
6197 *index_kind = dw_index_kind::DEBUG_NAMES;
6198 return true;
6199 }
927aa2e7 6200
7b23e087 6201 if (dwarf2_read_gdb_index (dwarf2_per_objfile))
3c0aa29a
PA
6202 {
6203 *index_kind = dw_index_kind::GDB_INDEX;
6204 return true;
6205 }
9291a0cd 6206
3c0aa29a 6207 return false;
9291a0cd
TT
6208}
6209
6210\f
6211
dce234bc
PP
6212/* Build a partial symbol table. */
6213
6214void
f29dff0a 6215dwarf2_build_psymtabs (struct objfile *objfile)
dce234bc 6216{
ed2dc618
SM
6217 struct dwarf2_per_objfile *dwarf2_per_objfile
6218 = get_dwarf2_per_objfile (objfile);
c9bf0622 6219
af5bf4ad
SM
6220 if (objfile->global_psymbols.capacity () == 0
6221 && objfile->static_psymbols.capacity () == 0)
6222 init_psymbol_list (objfile, 1024);
c906108c 6223
492d29ea 6224 TRY
c9bf0622
TT
6225 {
6226 /* This isn't really ideal: all the data we allocate on the
6227 objfile's obstack is still uselessly kept around. However,
6228 freeing it seems unsafe. */
906768f9 6229 psymtab_discarder psymtabs (objfile);
ed2dc618 6230 dwarf2_build_psymtabs_hard (dwarf2_per_objfile);
906768f9 6231 psymtabs.keep ();
c9bf0622 6232 }
492d29ea
PA
6233 CATCH (except, RETURN_MASK_ERROR)
6234 {
6235 exception_print (gdb_stderr, except);
6236 }
6237 END_CATCH
c906108c 6238}
c906108c 6239
1ce1cefd
DE
6240/* Return the total length of the CU described by HEADER. */
6241
6242static unsigned int
6243get_cu_length (const struct comp_unit_head *header)
6244{
6245 return header->initial_length_size + header->length;
6246}
6247
9c541725 6248/* Return TRUE if SECT_OFF is within CU_HEADER. */
45452591 6249
9c541725
PA
6250static inline bool
6251offset_in_cu_p (const comp_unit_head *cu_header, sect_offset sect_off)
45452591 6252{
9c541725
PA
6253 sect_offset bottom = cu_header->sect_off;
6254 sect_offset top = cu_header->sect_off + get_cu_length (cu_header);
9a619af0 6255
9c541725 6256 return sect_off >= bottom && sect_off < top;
45452591
DE
6257}
6258
3b80fe9b
DE
6259/* Find the base address of the compilation unit for range lists and
6260 location lists. It will normally be specified by DW_AT_low_pc.
6261 In DWARF-3 draft 4, the base address could be overridden by
6262 DW_AT_entry_pc. It's been removed, but GCC still uses this for
6263 compilation units with discontinuous ranges. */
6264
6265static void
6266dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
6267{
6268 struct attribute *attr;
6269
6270 cu->base_known = 0;
6271 cu->base_address = 0;
6272
6273 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
6274 if (attr)
6275 {
31aa7e4e 6276 cu->base_address = attr_value_as_address (attr);
3b80fe9b
DE
6277 cu->base_known = 1;
6278 }
6279 else
6280 {
6281 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
6282 if (attr)
6283 {
31aa7e4e 6284 cu->base_address = attr_value_as_address (attr);
3b80fe9b
DE
6285 cu->base_known = 1;
6286 }
6287 }
6288}
6289
93311388 6290/* Read in the comp unit header information from the debug_info at info_ptr.
43988095 6291 Use rcuh_kind::COMPILE as the default type if not known by the caller.
93311388
DE
6292 NOTE: This leaves members offset, first_die_offset to be filled in
6293 by the caller. */
107d2387 6294
d521ce57 6295static const gdb_byte *
107d2387 6296read_comp_unit_head (struct comp_unit_head *cu_header,
43988095
JK
6297 const gdb_byte *info_ptr,
6298 struct dwarf2_section_info *section,
6299 rcuh_kind section_kind)
107d2387
AC
6300{
6301 int signed_addr;
891d2f0b 6302 unsigned int bytes_read;
43988095
JK
6303 const char *filename = get_section_file_name (section);
6304 bfd *abfd = get_section_bfd_owner (section);
c764a876
DE
6305
6306 cu_header->length = read_initial_length (abfd, info_ptr, &bytes_read);
6307 cu_header->initial_length_size = bytes_read;
6308 cu_header->offset_size = (bytes_read == 4) ? 4 : 8;
613e1657 6309 info_ptr += bytes_read;
107d2387 6310 cu_header->version = read_2_bytes (abfd, info_ptr);
1ea5da02
TV
6311 if (cu_header->version < 2 || cu_header->version > 5)
6312 error (_("Dwarf Error: wrong version in compilation unit header "
6313 "(is %d, should be 2, 3, 4 or 5) [in module %s]"),
6314 cu_header->version, filename);
107d2387 6315 info_ptr += 2;
43988095
JK
6316 if (cu_header->version < 5)
6317 switch (section_kind)
6318 {
6319 case rcuh_kind::COMPILE:
6320 cu_header->unit_type = DW_UT_compile;
6321 break;
6322 case rcuh_kind::TYPE:
6323 cu_header->unit_type = DW_UT_type;
6324 break;
6325 default:
6326 internal_error (__FILE__, __LINE__,
6327 _("read_comp_unit_head: invalid section_kind"));
6328 }
6329 else
6330 {
6331 cu_header->unit_type = static_cast<enum dwarf_unit_type>
6332 (read_1_byte (abfd, info_ptr));
6333 info_ptr += 1;
6334 switch (cu_header->unit_type)
6335 {
6336 case DW_UT_compile:
6337 if (section_kind != rcuh_kind::COMPILE)
6338 error (_("Dwarf Error: wrong unit_type in compilation unit header "
6339 "(is DW_UT_compile, should be DW_UT_type) [in module %s]"),
6340 filename);
6341 break;
6342 case DW_UT_type:
6343 section_kind = rcuh_kind::TYPE;
6344 break;
6345 default:
6346 error (_("Dwarf Error: wrong unit_type in compilation unit header "
6347 "(is %d, should be %d or %d) [in module %s]"),
6348 cu_header->unit_type, DW_UT_compile, DW_UT_type, filename);
6349 }
6350
6351 cu_header->addr_size = read_1_byte (abfd, info_ptr);
6352 info_ptr += 1;
6353 }
9c541725
PA
6354 cu_header->abbrev_sect_off = (sect_offset) read_offset (abfd, info_ptr,
6355 cu_header,
6356 &bytes_read);
613e1657 6357 info_ptr += bytes_read;
43988095
JK
6358 if (cu_header->version < 5)
6359 {
6360 cu_header->addr_size = read_1_byte (abfd, info_ptr);
6361 info_ptr += 1;
6362 }
107d2387
AC
6363 signed_addr = bfd_get_sign_extend_vma (abfd);
6364 if (signed_addr < 0)
8e65ff28 6365 internal_error (__FILE__, __LINE__,
e2e0b3e5 6366 _("read_comp_unit_head: dwarf from non elf file"));
107d2387 6367 cu_header->signed_addr_p = signed_addr;
c764a876 6368
43988095
JK
6369 if (section_kind == rcuh_kind::TYPE)
6370 {
6371 LONGEST type_offset;
6372
6373 cu_header->signature = read_8_bytes (abfd, info_ptr);
6374 info_ptr += 8;
6375
6376 type_offset = read_offset (abfd, info_ptr, cu_header, &bytes_read);
6377 info_ptr += bytes_read;
9c541725
PA
6378 cu_header->type_cu_offset_in_tu = (cu_offset) type_offset;
6379 if (to_underlying (cu_header->type_cu_offset_in_tu) != type_offset)
43988095
JK
6380 error (_("Dwarf Error: Too big type_offset in compilation unit "
6381 "header (is %s) [in module %s]"), plongest (type_offset),
6382 filename);
6383 }
6384
107d2387
AC
6385 return info_ptr;
6386}
6387
36586728
TT
6388/* Helper function that returns the proper abbrev section for
6389 THIS_CU. */
6390
6391static struct dwarf2_section_info *
6392get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
6393{
6394 struct dwarf2_section_info *abbrev;
ed2dc618 6395 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
36586728
TT
6396
6397 if (this_cu->is_dwz)
ed2dc618 6398 abbrev = &dwarf2_get_dwz_file (dwarf2_per_objfile)->abbrev;
36586728
TT
6399 else
6400 abbrev = &dwarf2_per_objfile->abbrev;
6401
6402 return abbrev;
6403}
6404
9ff913ba
DE
6405/* Subroutine of read_and_check_comp_unit_head and
6406 read_and_check_type_unit_head to simplify them.
6407 Perform various error checking on the header. */
6408
6409static void
ed2dc618
SM
6410error_check_comp_unit_head (struct dwarf2_per_objfile *dwarf2_per_objfile,
6411 struct comp_unit_head *header,
4bdcc0c1
DE
6412 struct dwarf2_section_info *section,
6413 struct dwarf2_section_info *abbrev_section)
9ff913ba 6414{
a32a8923 6415 const char *filename = get_section_file_name (section);
9ff913ba 6416
9c541725 6417 if (to_underlying (header->abbrev_sect_off)
36586728 6418 >= dwarf2_section_size (dwarf2_per_objfile->objfile, abbrev_section))
9d8780f0
SM
6419 error (_("Dwarf Error: bad offset (%s) in compilation unit header "
6420 "(offset %s + 6) [in module %s]"),
6421 sect_offset_str (header->abbrev_sect_off),
6422 sect_offset_str (header->sect_off),
9ff913ba
DE
6423 filename);
6424
9c541725 6425 /* Cast to ULONGEST to use 64-bit arithmetic when possible to
9ff913ba 6426 avoid potential 32-bit overflow. */
9c541725 6427 if (((ULONGEST) header->sect_off + get_cu_length (header))
9ff913ba 6428 > section->size)
9c541725 6429 error (_("Dwarf Error: bad length (0x%x) in compilation unit header "
9d8780f0
SM
6430 "(offset %s + 0) [in module %s]"),
6431 header->length, sect_offset_str (header->sect_off),
9ff913ba
DE
6432 filename);
6433}
6434
6435/* Read in a CU/TU header and perform some basic error checking.
6436 The contents of the header are stored in HEADER.
6437 The result is a pointer to the start of the first DIE. */
adabb602 6438
d521ce57 6439static const gdb_byte *
ed2dc618
SM
6440read_and_check_comp_unit_head (struct dwarf2_per_objfile *dwarf2_per_objfile,
6441 struct comp_unit_head *header,
9ff913ba 6442 struct dwarf2_section_info *section,
4bdcc0c1 6443 struct dwarf2_section_info *abbrev_section,
d521ce57 6444 const gdb_byte *info_ptr,
43988095 6445 rcuh_kind section_kind)
72bf9492 6446{
d521ce57 6447 const gdb_byte *beg_of_comp_unit = info_ptr;
72bf9492 6448
9c541725 6449 header->sect_off = (sect_offset) (beg_of_comp_unit - section->buffer);
adabb602 6450
43988095 6451 info_ptr = read_comp_unit_head (header, info_ptr, section, section_kind);
9ff913ba 6452
9c541725 6453 header->first_die_cu_offset = (cu_offset) (info_ptr - beg_of_comp_unit);
348e048f 6454
ed2dc618
SM
6455 error_check_comp_unit_head (dwarf2_per_objfile, header, section,
6456 abbrev_section);
9ff913ba
DE
6457
6458 return info_ptr;
348e048f
DE
6459}
6460
f4dc4d17
DE
6461/* Fetch the abbreviation table offset from a comp or type unit header. */
6462
6463static sect_offset
ed2dc618
SM
6464read_abbrev_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
6465 struct dwarf2_section_info *section,
9c541725 6466 sect_offset sect_off)
f4dc4d17 6467{
a32a8923 6468 bfd *abfd = get_section_bfd_owner (section);
d521ce57 6469 const gdb_byte *info_ptr;
ac298888 6470 unsigned int initial_length_size, offset_size;
43988095 6471 uint16_t version;
f4dc4d17
DE
6472
6473 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
9c541725 6474 info_ptr = section->buffer + to_underlying (sect_off);
ac298888 6475 read_initial_length (abfd, info_ptr, &initial_length_size);
f4dc4d17 6476 offset_size = initial_length_size == 4 ? 4 : 8;
43988095
JK
6477 info_ptr += initial_length_size;
6478
6479 version = read_2_bytes (abfd, info_ptr);
6480 info_ptr += 2;
6481 if (version >= 5)
6482 {
6483 /* Skip unit type and address size. */
6484 info_ptr += 2;
6485 }
6486
9c541725 6487 return (sect_offset) read_offset_1 (abfd, info_ptr, offset_size);
f4dc4d17
DE
6488}
6489
aaa75496
JB
6490/* Allocate a new partial symtab for file named NAME and mark this new
6491 partial symtab as being an include of PST. */
6492
6493static void
d521ce57 6494dwarf2_create_include_psymtab (const char *name, struct partial_symtab *pst,
aaa75496
JB
6495 struct objfile *objfile)
6496{
6497 struct partial_symtab *subpst = allocate_psymtab (name, objfile);
6498
fbd9ab74
JK
6499 if (!IS_ABSOLUTE_PATH (subpst->filename))
6500 {
6501 /* It shares objfile->objfile_obstack. */
6502 subpst->dirname = pst->dirname;
6503 }
6504
aaa75496
JB
6505 subpst->textlow = 0;
6506 subpst->texthigh = 0;
6507
8d749320
SM
6508 subpst->dependencies
6509 = XOBNEW (&objfile->objfile_obstack, struct partial_symtab *);
aaa75496
JB
6510 subpst->dependencies[0] = pst;
6511 subpst->number_of_dependencies = 1;
6512
6513 subpst->globals_offset = 0;
6514 subpst->n_global_syms = 0;
6515 subpst->statics_offset = 0;
6516 subpst->n_static_syms = 0;
43f3e411 6517 subpst->compunit_symtab = NULL;
aaa75496
JB
6518 subpst->read_symtab = pst->read_symtab;
6519 subpst->readin = 0;
6520
6521 /* No private part is necessary for include psymtabs. This property
6522 can be used to differentiate between such include psymtabs and
10b3939b 6523 the regular ones. */
58a9656e 6524 subpst->read_symtab_private = NULL;
aaa75496
JB
6525}
6526
6527/* Read the Line Number Program data and extract the list of files
6528 included by the source file represented by PST. Build an include
d85a05f0 6529 partial symtab for each of these included files. */
aaa75496
JB
6530
6531static void
6532dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
dee91e82
DE
6533 struct die_info *die,
6534 struct partial_symtab *pst)
aaa75496 6535{
fff8551c 6536 line_header_up lh;
d85a05f0 6537 struct attribute *attr;
aaa75496 6538
d85a05f0
DJ
6539 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
6540 if (attr)
9c541725 6541 lh = dwarf_decode_line_header ((sect_offset) DW_UNSND (attr), cu);
aaa75496
JB
6542 if (lh == NULL)
6543 return; /* No linetable, so no includes. */
6544
c6da4cef 6545 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). */
fff8551c 6546 dwarf_decode_lines (lh.get (), pst->dirname, cu, pst, pst->textlow, 1);
aaa75496
JB
6547}
6548
348e048f 6549static hashval_t
52dc124a 6550hash_signatured_type (const void *item)
348e048f 6551{
9a3c8263
SM
6552 const struct signatured_type *sig_type
6553 = (const struct signatured_type *) item;
9a619af0 6554
348e048f 6555 /* This drops the top 32 bits of the signature, but is ok for a hash. */
52dc124a 6556 return sig_type->signature;
348e048f
DE
6557}
6558
6559static int
52dc124a 6560eq_signatured_type (const void *item_lhs, const void *item_rhs)
348e048f 6561{
9a3c8263
SM
6562 const struct signatured_type *lhs = (const struct signatured_type *) item_lhs;
6563 const struct signatured_type *rhs = (const struct signatured_type *) item_rhs;
9a619af0 6564
348e048f
DE
6565 return lhs->signature == rhs->signature;
6566}
6567
1fd400ff
TT
6568/* Allocate a hash table for signatured types. */
6569
6570static htab_t
673bfd45 6571allocate_signatured_type_table (struct objfile *objfile)
1fd400ff
TT
6572{
6573 return htab_create_alloc_ex (41,
52dc124a
DE
6574 hash_signatured_type,
6575 eq_signatured_type,
1fd400ff
TT
6576 NULL,
6577 &objfile->objfile_obstack,
6578 hashtab_obstack_allocate,
6579 dummy_obstack_deallocate);
6580}
6581
d467dd73 6582/* A helper function to add a signatured type CU to a table. */
1fd400ff
TT
6583
6584static int
d467dd73 6585add_signatured_type_cu_to_table (void **slot, void *datum)
1fd400ff 6586{
9a3c8263 6587 struct signatured_type *sigt = (struct signatured_type *) *slot;
b2bdb8cf
SM
6588 std::vector<signatured_type *> *all_type_units
6589 = (std::vector<signatured_type *> *) datum;
1fd400ff 6590
b2bdb8cf 6591 all_type_units->push_back (sigt);
1fd400ff
TT
6592
6593 return 1;
6594}
6595
78d4d2c5 6596/* A helper for create_debug_types_hash_table. Read types from SECTION
43988095
JK
6597 and fill them into TYPES_HTAB. It will process only type units,
6598 therefore DW_UT_type. */
c88ee1f0 6599
78d4d2c5 6600static void
ed2dc618
SM
6601create_debug_type_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
6602 struct dwo_file *dwo_file,
43988095
JK
6603 dwarf2_section_info *section, htab_t &types_htab,
6604 rcuh_kind section_kind)
348e048f 6605{
3019eac3 6606 struct objfile *objfile = dwarf2_per_objfile->objfile;
4bdcc0c1 6607 struct dwarf2_section_info *abbrev_section;
78d4d2c5
JK
6608 bfd *abfd;
6609 const gdb_byte *info_ptr, *end_ptr;
348e048f 6610
4bdcc0c1
DE
6611 abbrev_section = (dwo_file != NULL
6612 ? &dwo_file->sections.abbrev
6613 : &dwarf2_per_objfile->abbrev);
6614
b4f54984 6615 if (dwarf_read_debug)
43988095
JK
6616 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
6617 get_section_name (section),
a32a8923 6618 get_section_file_name (abbrev_section));
09406207 6619
78d4d2c5
JK
6620 dwarf2_read_section (objfile, section);
6621 info_ptr = section->buffer;
348e048f 6622
78d4d2c5
JK
6623 if (info_ptr == NULL)
6624 return;
348e048f 6625
78d4d2c5
JK
6626 /* We can't set abfd until now because the section may be empty or
6627 not present, in which case the bfd is unknown. */
6628 abfd = get_section_bfd_owner (section);
348e048f 6629
78d4d2c5
JK
6630 /* We don't use init_cutu_and_read_dies_simple, or some such, here
6631 because we don't need to read any dies: the signature is in the
6632 header. */
3019eac3 6633
78d4d2c5
JK
6634 end_ptr = info_ptr + section->size;
6635 while (info_ptr < end_ptr)
6636 {
78d4d2c5
JK
6637 struct signatured_type *sig_type;
6638 struct dwo_unit *dwo_tu;
6639 void **slot;
6640 const gdb_byte *ptr = info_ptr;
6641 struct comp_unit_head header;
6642 unsigned int length;
8b70b953 6643
9c541725 6644 sect_offset sect_off = (sect_offset) (ptr - section->buffer);
348e048f 6645
a49dd8dd
JK
6646 /* Initialize it due to a false compiler warning. */
6647 header.signature = -1;
9c541725 6648 header.type_cu_offset_in_tu = (cu_offset) -1;
a49dd8dd 6649
78d4d2c5
JK
6650 /* We need to read the type's signature in order to build the hash
6651 table, but we don't need anything else just yet. */
348e048f 6652
ed2dc618 6653 ptr = read_and_check_comp_unit_head (dwarf2_per_objfile, &header, section,
43988095 6654 abbrev_section, ptr, section_kind);
348e048f 6655
78d4d2c5 6656 length = get_cu_length (&header);
6caca83c 6657
78d4d2c5
JK
6658 /* Skip dummy type units. */
6659 if (ptr >= info_ptr + length
43988095
JK
6660 || peek_abbrev_code (abfd, ptr) == 0
6661 || header.unit_type != DW_UT_type)
78d4d2c5
JK
6662 {
6663 info_ptr += length;
6664 continue;
6665 }
dee91e82 6666
78d4d2c5
JK
6667 if (types_htab == NULL)
6668 {
6669 if (dwo_file)
6670 types_htab = allocate_dwo_unit_table (objfile);
6671 else
6672 types_htab = allocate_signatured_type_table (objfile);
6673 }
8b70b953 6674
78d4d2c5
JK
6675 if (dwo_file)
6676 {
6677 sig_type = NULL;
6678 dwo_tu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6679 struct dwo_unit);
6680 dwo_tu->dwo_file = dwo_file;
43988095 6681 dwo_tu->signature = header.signature;
9c541725 6682 dwo_tu->type_offset_in_tu = header.type_cu_offset_in_tu;
78d4d2c5 6683 dwo_tu->section = section;
9c541725 6684 dwo_tu->sect_off = sect_off;
78d4d2c5
JK
6685 dwo_tu->length = length;
6686 }
6687 else
6688 {
6689 /* N.B.: type_offset is not usable if this type uses a DWO file.
6690 The real type_offset is in the DWO file. */
6691 dwo_tu = NULL;
6692 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6693 struct signatured_type);
43988095 6694 sig_type->signature = header.signature;
9c541725 6695 sig_type->type_offset_in_tu = header.type_cu_offset_in_tu;
e3b94546 6696 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
78d4d2c5
JK
6697 sig_type->per_cu.is_debug_types = 1;
6698 sig_type->per_cu.section = section;
9c541725 6699 sig_type->per_cu.sect_off = sect_off;
78d4d2c5
JK
6700 sig_type->per_cu.length = length;
6701 }
6702
6703 slot = htab_find_slot (types_htab,
6704 dwo_file ? (void*) dwo_tu : (void *) sig_type,
6705 INSERT);
6706 gdb_assert (slot != NULL);
6707 if (*slot != NULL)
6708 {
9c541725 6709 sect_offset dup_sect_off;
0349ea22 6710
3019eac3
DE
6711 if (dwo_file)
6712 {
78d4d2c5
JK
6713 const struct dwo_unit *dup_tu
6714 = (const struct dwo_unit *) *slot;
6715
9c541725 6716 dup_sect_off = dup_tu->sect_off;
3019eac3
DE
6717 }
6718 else
6719 {
78d4d2c5
JK
6720 const struct signatured_type *dup_tu
6721 = (const struct signatured_type *) *slot;
6722
9c541725 6723 dup_sect_off = dup_tu->per_cu.sect_off;
3019eac3 6724 }
8b70b953 6725
b98664d3 6726 complaint (_("debug type entry at offset %s is duplicate to"
9d8780f0
SM
6727 " the entry at offset %s, signature %s"),
6728 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
43988095 6729 hex_string (header.signature));
78d4d2c5
JK
6730 }
6731 *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
3019eac3 6732
78d4d2c5 6733 if (dwarf_read_debug > 1)
9d8780f0
SM
6734 fprintf_unfiltered (gdb_stdlog, " offset %s, signature %s\n",
6735 sect_offset_str (sect_off),
43988095 6736 hex_string (header.signature));
3019eac3 6737
78d4d2c5
JK
6738 info_ptr += length;
6739 }
6740}
3019eac3 6741
78d4d2c5
JK
6742/* Create the hash table of all entries in the .debug_types
6743 (or .debug_types.dwo) section(s).
6744 If reading a DWO file, then DWO_FILE is a pointer to the DWO file object,
6745 otherwise it is NULL.
b3c8eb43 6746
78d4d2c5 6747 The result is a pointer to the hash table or NULL if there are no types.
348e048f 6748
78d4d2c5 6749 Note: This function processes DWO files only, not DWP files. */
348e048f 6750
78d4d2c5 6751static void
ed2dc618
SM
6752create_debug_types_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
6753 struct dwo_file *dwo_file,
78d4d2c5
JK
6754 VEC (dwarf2_section_info_def) *types,
6755 htab_t &types_htab)
6756{
6757 int ix;
6758 struct dwarf2_section_info *section;
6759
6760 if (VEC_empty (dwarf2_section_info_def, types))
6761 return;
348e048f 6762
78d4d2c5
JK
6763 for (ix = 0;
6764 VEC_iterate (dwarf2_section_info_def, types, ix, section);
6765 ++ix)
ed2dc618
SM
6766 create_debug_type_hash_table (dwarf2_per_objfile, dwo_file, section,
6767 types_htab, rcuh_kind::TYPE);
3019eac3
DE
6768}
6769
6770/* Create the hash table of all entries in the .debug_types section,
6771 and initialize all_type_units.
6772 The result is zero if there is an error (e.g. missing .debug_types section),
6773 otherwise non-zero. */
6774
6775static int
ed2dc618 6776create_all_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
3019eac3 6777{
78d4d2c5 6778 htab_t types_htab = NULL;
3019eac3 6779
ed2dc618
SM
6780 create_debug_type_hash_table (dwarf2_per_objfile, NULL,
6781 &dwarf2_per_objfile->info, types_htab,
43988095 6782 rcuh_kind::COMPILE);
ed2dc618
SM
6783 create_debug_types_hash_table (dwarf2_per_objfile, NULL,
6784 dwarf2_per_objfile->types, types_htab);
3019eac3
DE
6785 if (types_htab == NULL)
6786 {
6787 dwarf2_per_objfile->signatured_types = NULL;
6788 return 0;
6789 }
6790
348e048f
DE
6791 dwarf2_per_objfile->signatured_types = types_htab;
6792
b2bdb8cf
SM
6793 gdb_assert (dwarf2_per_objfile->all_type_units.empty ());
6794 dwarf2_per_objfile->all_type_units.reserve (htab_elements (types_htab));
6795
6796 htab_traverse_noresize (types_htab, add_signatured_type_cu_to_table,
6797 &dwarf2_per_objfile->all_type_units);
1fd400ff 6798
348e048f
DE
6799 return 1;
6800}
6801
6aa5f3a6
DE
6802/* Add an entry for signature SIG to dwarf2_per_objfile->signatured_types.
6803 If SLOT is non-NULL, it is the entry to use in the hash table.
6804 Otherwise we find one. */
6805
6806static struct signatured_type *
ed2dc618
SM
6807add_type_unit (struct dwarf2_per_objfile *dwarf2_per_objfile, ULONGEST sig,
6808 void **slot)
6aa5f3a6
DE
6809{
6810 struct objfile *objfile = dwarf2_per_objfile->objfile;
6aa5f3a6 6811
b2bdb8cf
SM
6812 if (dwarf2_per_objfile->all_type_units.size ()
6813 == dwarf2_per_objfile->all_type_units.capacity ())
6814 ++dwarf2_per_objfile->tu_stats.nr_all_type_units_reallocs;
6aa5f3a6 6815
b2bdb8cf
SM
6816 signatured_type *sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6817 struct signatured_type);
6818
6819 dwarf2_per_objfile->all_type_units.push_back (sig_type);
6aa5f3a6
DE
6820 sig_type->signature = sig;
6821 sig_type->per_cu.is_debug_types = 1;
6822 if (dwarf2_per_objfile->using_index)
6823 {
6824 sig_type->per_cu.v.quick =
6825 OBSTACK_ZALLOC (&objfile->objfile_obstack,
6826 struct dwarf2_per_cu_quick_data);
6827 }
6828
6829 if (slot == NULL)
6830 {
6831 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
6832 sig_type, INSERT);
6833 }
6834 gdb_assert (*slot == NULL);
6835 *slot = sig_type;
6836 /* The rest of sig_type must be filled in by the caller. */
6837 return sig_type;
6838}
6839
a2ce51a0
DE
6840/* Subroutine of lookup_dwo_signatured_type and lookup_dwp_signatured_type.
6841 Fill in SIG_ENTRY with DWO_ENTRY. */
6842
6843static void
ed2dc618 6844fill_in_sig_entry_from_dwo_entry (struct dwarf2_per_objfile *dwarf2_per_objfile,
a2ce51a0
DE
6845 struct signatured_type *sig_entry,
6846 struct dwo_unit *dwo_entry)
6847{
7ee85ab1 6848 /* Make sure we're not clobbering something we don't expect to. */
a2ce51a0
DE
6849 gdb_assert (! sig_entry->per_cu.queued);
6850 gdb_assert (sig_entry->per_cu.cu == NULL);
6aa5f3a6
DE
6851 if (dwarf2_per_objfile->using_index)
6852 {
6853 gdb_assert (sig_entry->per_cu.v.quick != NULL);
43f3e411 6854 gdb_assert (sig_entry->per_cu.v.quick->compunit_symtab == NULL);
6aa5f3a6
DE
6855 }
6856 else
6857 gdb_assert (sig_entry->per_cu.v.psymtab == NULL);
a2ce51a0 6858 gdb_assert (sig_entry->signature == dwo_entry->signature);
9c541725 6859 gdb_assert (to_underlying (sig_entry->type_offset_in_section) == 0);
a2ce51a0 6860 gdb_assert (sig_entry->type_unit_group == NULL);
7ee85ab1
DE
6861 gdb_assert (sig_entry->dwo_unit == NULL);
6862
6863 sig_entry->per_cu.section = dwo_entry->section;
9c541725 6864 sig_entry->per_cu.sect_off = dwo_entry->sect_off;
7ee85ab1
DE
6865 sig_entry->per_cu.length = dwo_entry->length;
6866 sig_entry->per_cu.reading_dwo_directly = 1;
e3b94546 6867 sig_entry->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
a2ce51a0
DE
6868 sig_entry->type_offset_in_tu = dwo_entry->type_offset_in_tu;
6869 sig_entry->dwo_unit = dwo_entry;
6870}
6871
6872/* Subroutine of lookup_signatured_type.
7ee85ab1
DE
6873 If we haven't read the TU yet, create the signatured_type data structure
6874 for a TU to be read in directly from a DWO file, bypassing the stub.
6875 This is the "Stay in DWO Optimization": When there is no DWP file and we're
6876 using .gdb_index, then when reading a CU we want to stay in the DWO file
6877 containing that CU. Otherwise we could end up reading several other DWO
6878 files (due to comdat folding) to process the transitive closure of all the
6879 mentioned TUs, and that can be slow. The current DWO file will have every
6880 type signature that it needs.
a2ce51a0
DE
6881 We only do this for .gdb_index because in the psymtab case we already have
6882 to read all the DWOs to build the type unit groups. */
6883
6884static struct signatured_type *
6885lookup_dwo_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6886{
518817b3
SM
6887 struct dwarf2_per_objfile *dwarf2_per_objfile
6888 = cu->per_cu->dwarf2_per_objfile;
a2ce51a0
DE
6889 struct objfile *objfile = dwarf2_per_objfile->objfile;
6890 struct dwo_file *dwo_file;
6891 struct dwo_unit find_dwo_entry, *dwo_entry;
6892 struct signatured_type find_sig_entry, *sig_entry;
6aa5f3a6 6893 void **slot;
a2ce51a0
DE
6894
6895 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
6896
6aa5f3a6
DE
6897 /* If TU skeletons have been removed then we may not have read in any
6898 TUs yet. */
6899 if (dwarf2_per_objfile->signatured_types == NULL)
6900 {
6901 dwarf2_per_objfile->signatured_types
6902 = allocate_signatured_type_table (objfile);
6903 }
a2ce51a0
DE
6904
6905 /* We only ever need to read in one copy of a signatured type.
6aa5f3a6
DE
6906 Use the global signatured_types array to do our own comdat-folding
6907 of types. If this is the first time we're reading this TU, and
6908 the TU has an entry in .gdb_index, replace the recorded data from
6909 .gdb_index with this TU. */
a2ce51a0 6910
a2ce51a0 6911 find_sig_entry.signature = sig;
6aa5f3a6
DE
6912 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
6913 &find_sig_entry, INSERT);
9a3c8263 6914 sig_entry = (struct signatured_type *) *slot;
7ee85ab1
DE
6915
6916 /* We can get here with the TU already read, *or* in the process of being
6aa5f3a6
DE
6917 read. Don't reassign the global entry to point to this DWO if that's
6918 the case. Also note that if the TU is already being read, it may not
6919 have come from a DWO, the program may be a mix of Fission-compiled
6920 code and non-Fission-compiled code. */
6921
6922 /* Have we already tried to read this TU?
6923 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
6924 needn't exist in the global table yet). */
6925 if (sig_entry != NULL && sig_entry->per_cu.tu_read)
a2ce51a0
DE
6926 return sig_entry;
6927
6aa5f3a6
DE
6928 /* Note: cu->dwo_unit is the dwo_unit that references this TU, not the
6929 dwo_unit of the TU itself. */
6930 dwo_file = cu->dwo_unit->dwo_file;
6931
a2ce51a0
DE
6932 /* Ok, this is the first time we're reading this TU. */
6933 if (dwo_file->tus == NULL)
6934 return NULL;
6935 find_dwo_entry.signature = sig;
9a3c8263 6936 dwo_entry = (struct dwo_unit *) htab_find (dwo_file->tus, &find_dwo_entry);
a2ce51a0
DE
6937 if (dwo_entry == NULL)
6938 return NULL;
6939
6aa5f3a6
DE
6940 /* If the global table doesn't have an entry for this TU, add one. */
6941 if (sig_entry == NULL)
ed2dc618 6942 sig_entry = add_type_unit (dwarf2_per_objfile, sig, slot);
6aa5f3a6 6943
ed2dc618 6944 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, sig_entry, dwo_entry);
89e63ee4 6945 sig_entry->per_cu.tu_read = 1;
a2ce51a0
DE
6946 return sig_entry;
6947}
6948
a2ce51a0
DE
6949/* Subroutine of lookup_signatured_type.
6950 Look up the type for signature SIG, and if we can't find SIG in .gdb_index
6aa5f3a6
DE
6951 then try the DWP file. If the TU stub (skeleton) has been removed then
6952 it won't be in .gdb_index. */
a2ce51a0
DE
6953
6954static struct signatured_type *
6955lookup_dwp_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6956{
518817b3
SM
6957 struct dwarf2_per_objfile *dwarf2_per_objfile
6958 = cu->per_cu->dwarf2_per_objfile;
a2ce51a0 6959 struct objfile *objfile = dwarf2_per_objfile->objfile;
ed2dc618 6960 struct dwp_file *dwp_file = get_dwp_file (dwarf2_per_objfile);
a2ce51a0
DE
6961 struct dwo_unit *dwo_entry;
6962 struct signatured_type find_sig_entry, *sig_entry;
6aa5f3a6 6963 void **slot;
a2ce51a0
DE
6964
6965 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
6966 gdb_assert (dwp_file != NULL);
6967
6aa5f3a6
DE
6968 /* If TU skeletons have been removed then we may not have read in any
6969 TUs yet. */
6970 if (dwarf2_per_objfile->signatured_types == NULL)
a2ce51a0 6971 {
6aa5f3a6
DE
6972 dwarf2_per_objfile->signatured_types
6973 = allocate_signatured_type_table (objfile);
a2ce51a0
DE
6974 }
6975
6aa5f3a6
DE
6976 find_sig_entry.signature = sig;
6977 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
6978 &find_sig_entry, INSERT);
9a3c8263 6979 sig_entry = (struct signatured_type *) *slot;
6aa5f3a6
DE
6980
6981 /* Have we already tried to read this TU?
6982 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
6983 needn't exist in the global table yet). */
6984 if (sig_entry != NULL)
6985 return sig_entry;
6986
a2ce51a0
DE
6987 if (dwp_file->tus == NULL)
6988 return NULL;
ed2dc618 6989 dwo_entry = lookup_dwo_unit_in_dwp (dwarf2_per_objfile, dwp_file, NULL,
57d63ce2 6990 sig, 1 /* is_debug_types */);
a2ce51a0
DE
6991 if (dwo_entry == NULL)
6992 return NULL;
6993
ed2dc618
SM
6994 sig_entry = add_type_unit (dwarf2_per_objfile, sig, slot);
6995 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, sig_entry, dwo_entry);
a2ce51a0 6996
a2ce51a0
DE
6997 return sig_entry;
6998}
6999
380bca97 7000/* Lookup a signature based type for DW_FORM_ref_sig8.
5a8b3f62
DE
7001 Returns NULL if signature SIG is not present in the table.
7002 It is up to the caller to complain about this. */
348e048f
DE
7003
7004static struct signatured_type *
a2ce51a0 7005lookup_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
348e048f 7006{
518817b3
SM
7007 struct dwarf2_per_objfile *dwarf2_per_objfile
7008 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 7009
a2ce51a0
DE
7010 if (cu->dwo_unit
7011 && dwarf2_per_objfile->using_index)
7012 {
7013 /* We're in a DWO/DWP file, and we're using .gdb_index.
7014 These cases require special processing. */
ed2dc618 7015 if (get_dwp_file (dwarf2_per_objfile) == NULL)
a2ce51a0
DE
7016 return lookup_dwo_signatured_type (cu, sig);
7017 else
7018 return lookup_dwp_signatured_type (cu, sig);
7019 }
7020 else
7021 {
7022 struct signatured_type find_entry, *entry;
348e048f 7023
a2ce51a0
DE
7024 if (dwarf2_per_objfile->signatured_types == NULL)
7025 return NULL;
7026 find_entry.signature = sig;
9a3c8263
SM
7027 entry = ((struct signatured_type *)
7028 htab_find (dwarf2_per_objfile->signatured_types, &find_entry));
a2ce51a0
DE
7029 return entry;
7030 }
348e048f 7031}
42e7ad6c
DE
7032\f
7033/* Low level DIE reading support. */
348e048f 7034
d85a05f0
DJ
7035/* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
7036
7037static void
7038init_cu_die_reader (struct die_reader_specs *reader,
dee91e82 7039 struct dwarf2_cu *cu,
3019eac3 7040 struct dwarf2_section_info *section,
685af9cd
TT
7041 struct dwo_file *dwo_file,
7042 struct abbrev_table *abbrev_table)
d85a05f0 7043{
fceca515 7044 gdb_assert (section->readin && section->buffer != NULL);
a32a8923 7045 reader->abfd = get_section_bfd_owner (section);
d85a05f0 7046 reader->cu = cu;
3019eac3 7047 reader->dwo_file = dwo_file;
dee91e82
DE
7048 reader->die_section = section;
7049 reader->buffer = section->buffer;
f664829e 7050 reader->buffer_end = section->buffer + section->size;
a2ce51a0 7051 reader->comp_dir = NULL;
685af9cd 7052 reader->abbrev_table = abbrev_table;
d85a05f0
DJ
7053}
7054
b0c7bfa9
DE
7055/* Subroutine of init_cutu_and_read_dies to simplify it.
7056 Read in the rest of a CU/TU top level DIE from DWO_UNIT.
7057 There's just a lot of work to do, and init_cutu_and_read_dies is big enough
7058 already.
7059
7060 STUB_COMP_UNIT_DIE is for the stub DIE, we copy over certain attributes
7061 from it to the DIE in the DWO. If NULL we are skipping the stub.
a2ce51a0
DE
7062 STUB_COMP_DIR is similar to STUB_COMP_UNIT_DIE: When reading a TU directly
7063 from the DWO file, bypassing the stub, it contains the DW_AT_comp_dir
c54a1dd8
DE
7064 attribute of the referencing CU. At most one of STUB_COMP_UNIT_DIE and
7065 STUB_COMP_DIR may be non-NULL.
b0c7bfa9
DE
7066 *RESULT_READER,*RESULT_INFO_PTR,*RESULT_COMP_UNIT_DIE,*RESULT_HAS_CHILDREN
7067 are filled in with the info of the DIE from the DWO file.
685af9cd
TT
7068 *RESULT_DWO_ABBREV_TABLE will be filled in with the abbrev table allocated
7069 from the dwo. Since *RESULT_READER references this abbrev table, it must be
7070 kept around for at least as long as *RESULT_READER.
7071
b0c7bfa9
DE
7072 The result is non-zero if a valid (non-dummy) DIE was found. */
7073
7074static int
7075read_cutu_die_from_dwo (struct dwarf2_per_cu_data *this_cu,
7076 struct dwo_unit *dwo_unit,
b0c7bfa9 7077 struct die_info *stub_comp_unit_die,
a2ce51a0 7078 const char *stub_comp_dir,
b0c7bfa9 7079 struct die_reader_specs *result_reader,
d521ce57 7080 const gdb_byte **result_info_ptr,
b0c7bfa9 7081 struct die_info **result_comp_unit_die,
685af9cd
TT
7082 int *result_has_children,
7083 abbrev_table_up *result_dwo_abbrev_table)
b0c7bfa9 7084{
ed2dc618 7085 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
b0c7bfa9
DE
7086 struct objfile *objfile = dwarf2_per_objfile->objfile;
7087 struct dwarf2_cu *cu = this_cu->cu;
b0c7bfa9 7088 bfd *abfd;
d521ce57 7089 const gdb_byte *begin_info_ptr, *info_ptr;
b0c7bfa9
DE
7090 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
7091 int i,num_extra_attrs;
7092 struct dwarf2_section_info *dwo_abbrev_section;
7093 struct attribute *attr;
7094 struct die_info *comp_unit_die;
7095
b0aeadb3
DE
7096 /* At most one of these may be provided. */
7097 gdb_assert ((stub_comp_unit_die != NULL) + (stub_comp_dir != NULL) <= 1);
a2ce51a0 7098
b0c7bfa9
DE
7099 /* These attributes aren't processed until later:
7100 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
0d60c288
DE
7101 DW_AT_comp_dir is used now, to find the DWO file, but it is also
7102 referenced later. However, these attributes are found in the stub
7103 which we won't have later. In order to not impose this complication
7104 on the rest of the code, we read them here and copy them to the
7105 DWO CU/TU die. */
b0c7bfa9
DE
7106
7107 stmt_list = NULL;
7108 low_pc = NULL;
7109 high_pc = NULL;
7110 ranges = NULL;
7111 comp_dir = NULL;
7112
7113 if (stub_comp_unit_die != NULL)
7114 {
7115 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
7116 DWO file. */
7117 if (! this_cu->is_debug_types)
7118 stmt_list = dwarf2_attr (stub_comp_unit_die, DW_AT_stmt_list, cu);
7119 low_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_low_pc, cu);
7120 high_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_high_pc, cu);
7121 ranges = dwarf2_attr (stub_comp_unit_die, DW_AT_ranges, cu);
7122 comp_dir = dwarf2_attr (stub_comp_unit_die, DW_AT_comp_dir, cu);
7123
7124 /* There should be a DW_AT_addr_base attribute here (if needed).
7125 We need the value before we can process DW_FORM_GNU_addr_index. */
7126 cu->addr_base = 0;
7127 attr = dwarf2_attr (stub_comp_unit_die, DW_AT_GNU_addr_base, cu);
7128 if (attr)
7129 cu->addr_base = DW_UNSND (attr);
7130
7131 /* There should be a DW_AT_ranges_base attribute here (if needed).
7132 We need the value before we can process DW_AT_ranges. */
7133 cu->ranges_base = 0;
7134 attr = dwarf2_attr (stub_comp_unit_die, DW_AT_GNU_ranges_base, cu);
7135 if (attr)
7136 cu->ranges_base = DW_UNSND (attr);
7137 }
a2ce51a0
DE
7138 else if (stub_comp_dir != NULL)
7139 {
7140 /* Reconstruct the comp_dir attribute to simplify the code below. */
8d749320 7141 comp_dir = XOBNEW (&cu->comp_unit_obstack, struct attribute);
a2ce51a0
DE
7142 comp_dir->name = DW_AT_comp_dir;
7143 comp_dir->form = DW_FORM_string;
7144 DW_STRING_IS_CANONICAL (comp_dir) = 0;
7145 DW_STRING (comp_dir) = stub_comp_dir;
7146 }
b0c7bfa9
DE
7147
7148 /* Set up for reading the DWO CU/TU. */
7149 cu->dwo_unit = dwo_unit;
685af9cd 7150 dwarf2_section_info *section = dwo_unit->section;
b0c7bfa9 7151 dwarf2_read_section (objfile, section);
a32a8923 7152 abfd = get_section_bfd_owner (section);
9c541725
PA
7153 begin_info_ptr = info_ptr = (section->buffer
7154 + to_underlying (dwo_unit->sect_off));
b0c7bfa9 7155 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
b0c7bfa9
DE
7156
7157 if (this_cu->is_debug_types)
7158 {
b0c7bfa9
DE
7159 struct signatured_type *sig_type = (struct signatured_type *) this_cu;
7160
ed2dc618
SM
7161 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7162 &cu->header, section,
b0c7bfa9 7163 dwo_abbrev_section,
43988095 7164 info_ptr, rcuh_kind::TYPE);
a2ce51a0 7165 /* This is not an assert because it can be caused by bad debug info. */
43988095 7166 if (sig_type->signature != cu->header.signature)
a2ce51a0
DE
7167 {
7168 error (_("Dwarf Error: signature mismatch %s vs %s while reading"
9d8780f0 7169 " TU at offset %s [in module %s]"),
a2ce51a0 7170 hex_string (sig_type->signature),
43988095 7171 hex_string (cu->header.signature),
9d8780f0 7172 sect_offset_str (dwo_unit->sect_off),
a2ce51a0
DE
7173 bfd_get_filename (abfd));
7174 }
9c541725 7175 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
b0c7bfa9
DE
7176 /* For DWOs coming from DWP files, we don't know the CU length
7177 nor the type's offset in the TU until now. */
7178 dwo_unit->length = get_cu_length (&cu->header);
9c541725 7179 dwo_unit->type_offset_in_tu = cu->header.type_cu_offset_in_tu;
b0c7bfa9
DE
7180
7181 /* Establish the type offset that can be used to lookup the type.
7182 For DWO files, we don't know it until now. */
9c541725
PA
7183 sig_type->type_offset_in_section
7184 = dwo_unit->sect_off + to_underlying (dwo_unit->type_offset_in_tu);
b0c7bfa9
DE
7185 }
7186 else
7187 {
ed2dc618
SM
7188 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7189 &cu->header, section,
b0c7bfa9 7190 dwo_abbrev_section,
43988095 7191 info_ptr, rcuh_kind::COMPILE);
9c541725 7192 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
b0c7bfa9
DE
7193 /* For DWOs coming from DWP files, we don't know the CU length
7194 until now. */
7195 dwo_unit->length = get_cu_length (&cu->header);
7196 }
7197
685af9cd
TT
7198 *result_dwo_abbrev_table
7199 = abbrev_table_read_table (dwarf2_per_objfile, dwo_abbrev_section,
7200 cu->header.abbrev_sect_off);
7201 init_cu_die_reader (result_reader, cu, section, dwo_unit->dwo_file,
7202 result_dwo_abbrev_table->get ());
b0c7bfa9
DE
7203
7204 /* Read in the die, but leave space to copy over the attributes
7205 from the stub. This has the benefit of simplifying the rest of
7206 the code - all the work to maintain the illusion of a single
7207 DW_TAG_{compile,type}_unit DIE is done here. */
7208 num_extra_attrs = ((stmt_list != NULL)
7209 + (low_pc != NULL)
7210 + (high_pc != NULL)
7211 + (ranges != NULL)
7212 + (comp_dir != NULL));
7213 info_ptr = read_full_die_1 (result_reader, result_comp_unit_die, info_ptr,
7214 result_has_children, num_extra_attrs);
7215
7216 /* Copy over the attributes from the stub to the DIE we just read in. */
7217 comp_unit_die = *result_comp_unit_die;
7218 i = comp_unit_die->num_attrs;
7219 if (stmt_list != NULL)
7220 comp_unit_die->attrs[i++] = *stmt_list;
7221 if (low_pc != NULL)
7222 comp_unit_die->attrs[i++] = *low_pc;
7223 if (high_pc != NULL)
7224 comp_unit_die->attrs[i++] = *high_pc;
7225 if (ranges != NULL)
7226 comp_unit_die->attrs[i++] = *ranges;
7227 if (comp_dir != NULL)
7228 comp_unit_die->attrs[i++] = *comp_dir;
7229 comp_unit_die->num_attrs += num_extra_attrs;
7230
b4f54984 7231 if (dwarf_die_debug)
bf6af496
DE
7232 {
7233 fprintf_unfiltered (gdb_stdlog,
7234 "Read die from %s@0x%x of %s:\n",
a32a8923 7235 get_section_name (section),
bf6af496
DE
7236 (unsigned) (begin_info_ptr - section->buffer),
7237 bfd_get_filename (abfd));
b4f54984 7238 dump_die (comp_unit_die, dwarf_die_debug);
bf6af496
DE
7239 }
7240
a2ce51a0
DE
7241 /* Save the comp_dir attribute. If there is no DWP file then we'll read
7242 TUs by skipping the stub and going directly to the entry in the DWO file.
7243 However, skipping the stub means we won't get DW_AT_comp_dir, so we have
7244 to get it via circuitous means. Blech. */
7245 if (comp_dir != NULL)
7246 result_reader->comp_dir = DW_STRING (comp_dir);
7247
b0c7bfa9
DE
7248 /* Skip dummy compilation units. */
7249 if (info_ptr >= begin_info_ptr + dwo_unit->length
7250 || peek_abbrev_code (abfd, info_ptr) == 0)
7251 return 0;
7252
7253 *result_info_ptr = info_ptr;
7254 return 1;
7255}
7256
7257/* Subroutine of init_cutu_and_read_dies to simplify it.
7258 Look up the DWO unit specified by COMP_UNIT_DIE of THIS_CU.
6a506a2d 7259 Returns NULL if the specified DWO unit cannot be found. */
b0c7bfa9
DE
7260
7261static struct dwo_unit *
7262lookup_dwo_unit (struct dwarf2_per_cu_data *this_cu,
7263 struct die_info *comp_unit_die)
7264{
7265 struct dwarf2_cu *cu = this_cu->cu;
b0c7bfa9
DE
7266 ULONGEST signature;
7267 struct dwo_unit *dwo_unit;
7268 const char *comp_dir, *dwo_name;
7269
a2ce51a0
DE
7270 gdb_assert (cu != NULL);
7271
b0c7bfa9 7272 /* Yeah, we look dwo_name up again, but it simplifies the code. */
7d45c7c3
KB
7273 dwo_name = dwarf2_string_attr (comp_unit_die, DW_AT_GNU_dwo_name, cu);
7274 comp_dir = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
b0c7bfa9
DE
7275
7276 if (this_cu->is_debug_types)
7277 {
7278 struct signatured_type *sig_type;
7279
7280 /* Since this_cu is the first member of struct signatured_type,
7281 we can go from a pointer to one to a pointer to the other. */
7282 sig_type = (struct signatured_type *) this_cu;
7283 signature = sig_type->signature;
7284 dwo_unit = lookup_dwo_type_unit (sig_type, dwo_name, comp_dir);
7285 }
7286 else
7287 {
7288 struct attribute *attr;
7289
7290 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
7291 if (! attr)
7292 error (_("Dwarf Error: missing dwo_id for dwo_name %s"
7293 " [in module %s]"),
e3b94546 7294 dwo_name, objfile_name (this_cu->dwarf2_per_objfile->objfile));
b0c7bfa9
DE
7295 signature = DW_UNSND (attr);
7296 dwo_unit = lookup_dwo_comp_unit (this_cu, dwo_name, comp_dir,
7297 signature);
7298 }
7299
b0c7bfa9
DE
7300 return dwo_unit;
7301}
7302
a2ce51a0 7303/* Subroutine of init_cutu_and_read_dies to simplify it.
6aa5f3a6 7304 See it for a description of the parameters.
fcd3b13d 7305 Read a TU directly from a DWO file, bypassing the stub. */
a2ce51a0
DE
7306
7307static void
6aa5f3a6
DE
7308init_tu_and_read_dwo_dies (struct dwarf2_per_cu_data *this_cu,
7309 int use_existing_cu, int keep,
a2ce51a0
DE
7310 die_reader_func_ftype *die_reader_func,
7311 void *data)
7312{
fcd3b13d 7313 std::unique_ptr<dwarf2_cu> new_cu;
a2ce51a0 7314 struct signatured_type *sig_type;
a2ce51a0
DE
7315 struct die_reader_specs reader;
7316 const gdb_byte *info_ptr;
7317 struct die_info *comp_unit_die;
7318 int has_children;
ed2dc618 7319 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
a2ce51a0
DE
7320
7321 /* Verify we can do the following downcast, and that we have the
7322 data we need. */
7323 gdb_assert (this_cu->is_debug_types && this_cu->reading_dwo_directly);
7324 sig_type = (struct signatured_type *) this_cu;
7325 gdb_assert (sig_type->dwo_unit != NULL);
7326
6aa5f3a6
DE
7327 if (use_existing_cu && this_cu->cu != NULL)
7328 {
7329 gdb_assert (this_cu->cu->dwo_unit == sig_type->dwo_unit);
6aa5f3a6
DE
7330 /* There's no need to do the rereading_dwo_cu handling that
7331 init_cutu_and_read_dies does since we don't read the stub. */
7332 }
7333 else
7334 {
7335 /* If !use_existing_cu, this_cu->cu must be NULL. */
7336 gdb_assert (this_cu->cu == NULL);
fcd3b13d 7337 new_cu.reset (new dwarf2_cu (this_cu));
6aa5f3a6
DE
7338 }
7339
7340 /* A future optimization, if needed, would be to use an existing
7341 abbrev table. When reading DWOs with skeletonless TUs, all the TUs
7342 could share abbrev tables. */
a2ce51a0 7343
685af9cd
TT
7344 /* The abbreviation table used by READER, this must live at least as long as
7345 READER. */
7346 abbrev_table_up dwo_abbrev_table;
7347
a2ce51a0 7348 if (read_cutu_die_from_dwo (this_cu, sig_type->dwo_unit,
a2ce51a0
DE
7349 NULL /* stub_comp_unit_die */,
7350 sig_type->dwo_unit->dwo_file->comp_dir,
7351 &reader, &info_ptr,
685af9cd
TT
7352 &comp_unit_die, &has_children,
7353 &dwo_abbrev_table) == 0)
a2ce51a0
DE
7354 {
7355 /* Dummy die. */
a2ce51a0
DE
7356 return;
7357 }
7358
7359 /* All the "real" work is done here. */
7360 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
7361
6aa5f3a6 7362 /* This duplicates the code in init_cutu_and_read_dies,
a2ce51a0
DE
7363 but the alternative is making the latter more complex.
7364 This function is only for the special case of using DWO files directly:
7365 no point in overly complicating the general case just to handle this. */
fcd3b13d 7366 if (new_cu != NULL && keep)
a2ce51a0 7367 {
fcd3b13d
SM
7368 /* Link this CU into read_in_chain. */
7369 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
7370 dwarf2_per_objfile->read_in_chain = this_cu;
7371 /* The chain owns it now. */
7372 new_cu.release ();
a2ce51a0 7373 }
a2ce51a0
DE
7374}
7375
fd820528 7376/* Initialize a CU (or TU) and read its DIEs.
3019eac3 7377 If the CU defers to a DWO file, read the DWO file as well.
dee91e82 7378
f4dc4d17
DE
7379 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
7380 Otherwise the table specified in the comp unit header is read in and used.
7381 This is an optimization for when we already have the abbrev table.
7382
dee91e82
DE
7383 If USE_EXISTING_CU is non-zero, and THIS_CU->cu is non-NULL, then use it.
7384 Otherwise, a new CU is allocated with xmalloc.
7385
7386 If KEEP is non-zero, then if we allocated a dwarf2_cu we add it to
7387 read_in_chain. Otherwise the dwarf2_cu data is freed at the end.
7388
7389 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
fd820528 7390 linker) then DIE_READER_FUNC will not get called. */
aaa75496 7391
70221824 7392static void
fd820528 7393init_cutu_and_read_dies (struct dwarf2_per_cu_data *this_cu,
f4dc4d17 7394 struct abbrev_table *abbrev_table,
fd820528 7395 int use_existing_cu, int keep,
58f0c718 7396 bool skip_partial,
fd820528
DE
7397 die_reader_func_ftype *die_reader_func,
7398 void *data)
c906108c 7399{
ed2dc618 7400 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
dee91e82 7401 struct objfile *objfile = dwarf2_per_objfile->objfile;
8a0459fd 7402 struct dwarf2_section_info *section = this_cu->section;
a32a8923 7403 bfd *abfd = get_section_bfd_owner (section);
dee91e82 7404 struct dwarf2_cu *cu;
d521ce57 7405 const gdb_byte *begin_info_ptr, *info_ptr;
dee91e82 7406 struct die_reader_specs reader;
d85a05f0 7407 struct die_info *comp_unit_die;
dee91e82 7408 int has_children;
d85a05f0 7409 struct attribute *attr;
dee91e82 7410 struct signatured_type *sig_type = NULL;
4bdcc0c1 7411 struct dwarf2_section_info *abbrev_section;
42e7ad6c
DE
7412 /* Non-zero if CU currently points to a DWO file and we need to
7413 reread it. When this happens we need to reread the skeleton die
a2ce51a0 7414 before we can reread the DWO file (this only applies to CUs, not TUs). */
42e7ad6c 7415 int rereading_dwo_cu = 0;
c906108c 7416
b4f54984 7417 if (dwarf_die_debug)
9d8780f0 7418 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
09406207 7419 this_cu->is_debug_types ? "type" : "comp",
9d8780f0 7420 sect_offset_str (this_cu->sect_off));
09406207 7421
dee91e82
DE
7422 if (use_existing_cu)
7423 gdb_assert (keep);
23745b47 7424
a2ce51a0
DE
7425 /* If we're reading a TU directly from a DWO file, including a virtual DWO
7426 file (instead of going through the stub), short-circuit all of this. */
7427 if (this_cu->reading_dwo_directly)
7428 {
7429 /* Narrow down the scope of possibilities to have to understand. */
7430 gdb_assert (this_cu->is_debug_types);
7431 gdb_assert (abbrev_table == NULL);
6aa5f3a6
DE
7432 init_tu_and_read_dwo_dies (this_cu, use_existing_cu, keep,
7433 die_reader_func, data);
a2ce51a0
DE
7434 return;
7435 }
7436
dee91e82
DE
7437 /* This is cheap if the section is already read in. */
7438 dwarf2_read_section (objfile, section);
7439
9c541725 7440 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
36586728
TT
7441
7442 abbrev_section = get_abbrev_section_for_cu (this_cu);
dee91e82 7443
fcd3b13d 7444 std::unique_ptr<dwarf2_cu> new_cu;
dee91e82
DE
7445 if (use_existing_cu && this_cu->cu != NULL)
7446 {
7447 cu = this_cu->cu;
42e7ad6c
DE
7448 /* If this CU is from a DWO file we need to start over, we need to
7449 refetch the attributes from the skeleton CU.
7450 This could be optimized by retrieving those attributes from when we
7451 were here the first time: the previous comp_unit_die was stored in
7452 comp_unit_obstack. But there's no data yet that we need this
7453 optimization. */
7454 if (cu->dwo_unit != NULL)
7455 rereading_dwo_cu = 1;
dee91e82
DE
7456 }
7457 else
7458 {
7459 /* If !use_existing_cu, this_cu->cu must be NULL. */
7460 gdb_assert (this_cu->cu == NULL);
fcd3b13d
SM
7461 new_cu.reset (new dwarf2_cu (this_cu));
7462 cu = new_cu.get ();
42e7ad6c 7463 }
dee91e82 7464
b0c7bfa9 7465 /* Get the header. */
9c541725 7466 if (to_underlying (cu->header.first_die_cu_offset) != 0 && !rereading_dwo_cu)
42e7ad6c
DE
7467 {
7468 /* We already have the header, there's no need to read it in again. */
9c541725 7469 info_ptr += to_underlying (cu->header.first_die_cu_offset);
42e7ad6c
DE
7470 }
7471 else
7472 {
3019eac3 7473 if (this_cu->is_debug_types)
dee91e82 7474 {
ed2dc618
SM
7475 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7476 &cu->header, section,
4bdcc0c1 7477 abbrev_section, info_ptr,
43988095 7478 rcuh_kind::TYPE);
dee91e82 7479
42e7ad6c
DE
7480 /* Since per_cu is the first member of struct signatured_type,
7481 we can go from a pointer to one to a pointer to the other. */
7482 sig_type = (struct signatured_type *) this_cu;
43988095 7483 gdb_assert (sig_type->signature == cu->header.signature);
9c541725
PA
7484 gdb_assert (sig_type->type_offset_in_tu
7485 == cu->header.type_cu_offset_in_tu);
7486 gdb_assert (this_cu->sect_off == cu->header.sect_off);
dee91e82 7487
42e7ad6c
DE
7488 /* LENGTH has not been set yet for type units if we're
7489 using .gdb_index. */
1ce1cefd 7490 this_cu->length = get_cu_length (&cu->header);
3019eac3
DE
7491
7492 /* Establish the type offset that can be used to lookup the type. */
9c541725
PA
7493 sig_type->type_offset_in_section =
7494 this_cu->sect_off + to_underlying (sig_type->type_offset_in_tu);
43988095
JK
7495
7496 this_cu->dwarf_version = cu->header.version;
dee91e82
DE
7497 }
7498 else
7499 {
ed2dc618
SM
7500 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7501 &cu->header, section,
4bdcc0c1 7502 abbrev_section,
43988095
JK
7503 info_ptr,
7504 rcuh_kind::COMPILE);
dee91e82 7505
9c541725 7506 gdb_assert (this_cu->sect_off == cu->header.sect_off);
1ce1cefd 7507 gdb_assert (this_cu->length == get_cu_length (&cu->header));
43988095 7508 this_cu->dwarf_version = cu->header.version;
dee91e82
DE
7509 }
7510 }
10b3939b 7511
6caca83c 7512 /* Skip dummy compilation units. */
dee91e82 7513 if (info_ptr >= begin_info_ptr + this_cu->length
6caca83c 7514 || peek_abbrev_code (abfd, info_ptr) == 0)
fcd3b13d 7515 return;
6caca83c 7516
433df2d4
DE
7517 /* If we don't have them yet, read the abbrevs for this compilation unit.
7518 And if we need to read them now, make sure they're freed when we're
685af9cd
TT
7519 done (own the table through ABBREV_TABLE_HOLDER). */
7520 abbrev_table_up abbrev_table_holder;
f4dc4d17 7521 if (abbrev_table != NULL)
685af9cd
TT
7522 gdb_assert (cu->header.abbrev_sect_off == abbrev_table->sect_off);
7523 else
f4dc4d17 7524 {
685af9cd
TT
7525 abbrev_table_holder
7526 = abbrev_table_read_table (dwarf2_per_objfile, abbrev_section,
7527 cu->header.abbrev_sect_off);
7528 abbrev_table = abbrev_table_holder.get ();
42e7ad6c 7529 }
af703f96 7530
dee91e82 7531 /* Read the top level CU/TU die. */
685af9cd 7532 init_cu_die_reader (&reader, cu, section, NULL, abbrev_table);
dee91e82 7533 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
93311388 7534
58f0c718
TT
7535 if (skip_partial && comp_unit_die->tag == DW_TAG_partial_unit)
7536 return;
7537
b0c7bfa9 7538 /* If we are in a DWO stub, process it and then read in the "real" CU/TU
685af9cd
TT
7539 from the DWO file. read_cutu_die_from_dwo will allocate the abbreviation
7540 table from the DWO file and pass the ownership over to us. It will be
7541 referenced from READER, so we must make sure to free it after we're done
7542 with READER.
7543
b0c7bfa9
DE
7544 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains a
7545 DWO CU, that this test will fail (the attribute will not be present). */
3019eac3 7546 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_name, cu);
685af9cd 7547 abbrev_table_up dwo_abbrev_table;
3019eac3
DE
7548 if (attr)
7549 {
3019eac3 7550 struct dwo_unit *dwo_unit;
b0c7bfa9 7551 struct die_info *dwo_comp_unit_die;
3019eac3
DE
7552
7553 if (has_children)
6a506a2d 7554 {
b98664d3 7555 complaint (_("compilation unit with DW_AT_GNU_dwo_name"
9d8780f0
SM
7556 " has children (offset %s) [in module %s]"),
7557 sect_offset_str (this_cu->sect_off),
7558 bfd_get_filename (abfd));
6a506a2d 7559 }
b0c7bfa9 7560 dwo_unit = lookup_dwo_unit (this_cu, comp_unit_die);
6a506a2d 7561 if (dwo_unit != NULL)
3019eac3 7562 {
6a506a2d 7563 if (read_cutu_die_from_dwo (this_cu, dwo_unit,
a2ce51a0 7564 comp_unit_die, NULL,
6a506a2d 7565 &reader, &info_ptr,
685af9cd
TT
7566 &dwo_comp_unit_die, &has_children,
7567 &dwo_abbrev_table) == 0)
6a506a2d
DE
7568 {
7569 /* Dummy die. */
6a506a2d
DE
7570 return;
7571 }
7572 comp_unit_die = dwo_comp_unit_die;
7573 }
7574 else
7575 {
7576 /* Yikes, we couldn't find the rest of the DIE, we only have
7577 the stub. A complaint has already been logged. There's
7578 not much more we can do except pass on the stub DIE to
7579 die_reader_func. We don't want to throw an error on bad
7580 debug info. */
3019eac3
DE
7581 }
7582 }
7583
b0c7bfa9 7584 /* All of the above is setup for this call. Yikes. */
dee91e82
DE
7585 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
7586
b0c7bfa9 7587 /* Done, clean up. */
fcd3b13d 7588 if (new_cu != NULL && keep)
348e048f 7589 {
fcd3b13d
SM
7590 /* Link this CU into read_in_chain. */
7591 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
7592 dwarf2_per_objfile->read_in_chain = this_cu;
7593 /* The chain owns it now. */
7594 new_cu.release ();
348e048f 7595 }
dee91e82
DE
7596}
7597
33e80786
DE
7598/* Read CU/TU THIS_CU but do not follow DW_AT_GNU_dwo_name if present.
7599 DWO_FILE, if non-NULL, is the DWO file to read (the caller is assumed
7600 to have already done the lookup to find the DWO file).
dee91e82
DE
7601
7602 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
3019eac3 7603 THIS_CU->is_debug_types, but nothing else.
dee91e82
DE
7604
7605 We fill in THIS_CU->length.
7606
7607 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
7608 linker) then DIE_READER_FUNC will not get called.
7609
7610 THIS_CU->cu is always freed when done.
3019eac3
DE
7611 This is done in order to not leave THIS_CU->cu in a state where we have
7612 to care whether it refers to the "main" CU or the DWO CU. */
dee91e82
DE
7613
7614static void
7615init_cutu_and_read_dies_no_follow (struct dwarf2_per_cu_data *this_cu,
3019eac3 7616 struct dwo_file *dwo_file,
dee91e82
DE
7617 die_reader_func_ftype *die_reader_func,
7618 void *data)
7619{
ed2dc618 7620 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
dee91e82 7621 struct objfile *objfile = dwarf2_per_objfile->objfile;
8a0459fd 7622 struct dwarf2_section_info *section = this_cu->section;
a32a8923 7623 bfd *abfd = get_section_bfd_owner (section);
33e80786 7624 struct dwarf2_section_info *abbrev_section;
d521ce57 7625 const gdb_byte *begin_info_ptr, *info_ptr;
dee91e82 7626 struct die_reader_specs reader;
dee91e82
DE
7627 struct die_info *comp_unit_die;
7628 int has_children;
7629
b4f54984 7630 if (dwarf_die_debug)
9d8780f0 7631 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
09406207 7632 this_cu->is_debug_types ? "type" : "comp",
9d8780f0 7633 sect_offset_str (this_cu->sect_off));
09406207 7634
dee91e82
DE
7635 gdb_assert (this_cu->cu == NULL);
7636
33e80786
DE
7637 abbrev_section = (dwo_file != NULL
7638 ? &dwo_file->sections.abbrev
7639 : get_abbrev_section_for_cu (this_cu));
7640
dee91e82
DE
7641 /* This is cheap if the section is already read in. */
7642 dwarf2_read_section (objfile, section);
7643
fcd3b13d 7644 struct dwarf2_cu cu (this_cu);
dee91e82 7645
9c541725 7646 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
ed2dc618
SM
7647 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7648 &cu.header, section,
4bdcc0c1 7649 abbrev_section, info_ptr,
43988095
JK
7650 (this_cu->is_debug_types
7651 ? rcuh_kind::TYPE
7652 : rcuh_kind::COMPILE));
dee91e82 7653
1ce1cefd 7654 this_cu->length = get_cu_length (&cu.header);
dee91e82
DE
7655
7656 /* Skip dummy compilation units. */
7657 if (info_ptr >= begin_info_ptr + this_cu->length
7658 || peek_abbrev_code (abfd, info_ptr) == 0)
fcd3b13d 7659 return;
72bf9492 7660
685af9cd
TT
7661 abbrev_table_up abbrev_table
7662 = abbrev_table_read_table (dwarf2_per_objfile, abbrev_section,
7663 cu.header.abbrev_sect_off);
dee91e82 7664
685af9cd 7665 init_cu_die_reader (&reader, &cu, section, dwo_file, abbrev_table.get ());
dee91e82
DE
7666 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
7667
7668 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
dee91e82
DE
7669}
7670
3019eac3
DE
7671/* Read a CU/TU, except that this does not look for DW_AT_GNU_dwo_name and
7672 does not lookup the specified DWO file.
7673 This cannot be used to read DWO files.
dee91e82
DE
7674
7675 THIS_CU->cu is always freed when done.
3019eac3
DE
7676 This is done in order to not leave THIS_CU->cu in a state where we have
7677 to care whether it refers to the "main" CU or the DWO CU.
7678 We can revisit this if the data shows there's a performance issue. */
dee91e82
DE
7679
7680static void
7681init_cutu_and_read_dies_simple (struct dwarf2_per_cu_data *this_cu,
7682 die_reader_func_ftype *die_reader_func,
7683 void *data)
7684{
33e80786 7685 init_cutu_and_read_dies_no_follow (this_cu, NULL, die_reader_func, data);
dee91e82 7686}
0018ea6f
DE
7687\f
7688/* Type Unit Groups.
dee91e82 7689
0018ea6f
DE
7690 Type Unit Groups are a way to collapse the set of all TUs (type units) into
7691 a more manageable set. The grouping is done by DW_AT_stmt_list entry
7692 so that all types coming from the same compilation (.o file) are grouped
7693 together. A future step could be to put the types in the same symtab as
7694 the CU the types ultimately came from. */
ff013f42 7695
f4dc4d17
DE
7696static hashval_t
7697hash_type_unit_group (const void *item)
7698{
9a3c8263
SM
7699 const struct type_unit_group *tu_group
7700 = (const struct type_unit_group *) item;
f4dc4d17 7701
094b34ac 7702 return hash_stmt_list_entry (&tu_group->hash);
f4dc4d17 7703}
348e048f
DE
7704
7705static int
f4dc4d17 7706eq_type_unit_group (const void *item_lhs, const void *item_rhs)
348e048f 7707{
9a3c8263
SM
7708 const struct type_unit_group *lhs = (const struct type_unit_group *) item_lhs;
7709 const struct type_unit_group *rhs = (const struct type_unit_group *) item_rhs;
348e048f 7710
094b34ac 7711 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
f4dc4d17 7712}
348e048f 7713
f4dc4d17
DE
7714/* Allocate a hash table for type unit groups. */
7715
7716static htab_t
ed2dc618 7717allocate_type_unit_groups_table (struct objfile *objfile)
f4dc4d17
DE
7718{
7719 return htab_create_alloc_ex (3,
7720 hash_type_unit_group,
7721 eq_type_unit_group,
7722 NULL,
ed2dc618 7723 &objfile->objfile_obstack,
f4dc4d17
DE
7724 hashtab_obstack_allocate,
7725 dummy_obstack_deallocate);
7726}
dee91e82 7727
f4dc4d17
DE
7728/* Type units that don't have DW_AT_stmt_list are grouped into their own
7729 partial symtabs. We combine several TUs per psymtab to not let the size
7730 of any one psymtab grow too big. */
7731#define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
7732#define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
dee91e82 7733
094b34ac 7734/* Helper routine for get_type_unit_group.
f4dc4d17
DE
7735 Create the type_unit_group object used to hold one or more TUs. */
7736
7737static struct type_unit_group *
094b34ac 7738create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
f4dc4d17 7739{
518817b3
SM
7740 struct dwarf2_per_objfile *dwarf2_per_objfile
7741 = cu->per_cu->dwarf2_per_objfile;
f4dc4d17 7742 struct objfile *objfile = dwarf2_per_objfile->objfile;
094b34ac 7743 struct dwarf2_per_cu_data *per_cu;
f4dc4d17 7744 struct type_unit_group *tu_group;
f4dc4d17
DE
7745
7746 tu_group = OBSTACK_ZALLOC (&objfile->objfile_obstack,
7747 struct type_unit_group);
094b34ac 7748 per_cu = &tu_group->per_cu;
518817b3 7749 per_cu->dwarf2_per_objfile = dwarf2_per_objfile;
f4dc4d17 7750
094b34ac
DE
7751 if (dwarf2_per_objfile->using_index)
7752 {
7753 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
7754 struct dwarf2_per_cu_quick_data);
094b34ac
DE
7755 }
7756 else
7757 {
9c541725 7758 unsigned int line_offset = to_underlying (line_offset_struct);
094b34ac
DE
7759 struct partial_symtab *pst;
7760 char *name;
7761
7762 /* Give the symtab a useful name for debug purposes. */
7763 if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
7764 name = xstrprintf ("<type_units_%d>",
7765 (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
7766 else
7767 name = xstrprintf ("<type_units_at_0x%x>", line_offset);
7768
7769 pst = create_partial_symtab (per_cu, name);
7770 pst->anonymous = 1;
f4dc4d17 7771
094b34ac
DE
7772 xfree (name);
7773 }
f4dc4d17 7774
094b34ac 7775 tu_group->hash.dwo_unit = cu->dwo_unit;
9c541725 7776 tu_group->hash.line_sect_off = line_offset_struct;
f4dc4d17
DE
7777
7778 return tu_group;
7779}
7780
094b34ac
DE
7781/* Look up the type_unit_group for type unit CU, and create it if necessary.
7782 STMT_LIST is a DW_AT_stmt_list attribute. */
f4dc4d17
DE
7783
7784static struct type_unit_group *
ff39bb5e 7785get_type_unit_group (struct dwarf2_cu *cu, const struct attribute *stmt_list)
f4dc4d17 7786{
518817b3
SM
7787 struct dwarf2_per_objfile *dwarf2_per_objfile
7788 = cu->per_cu->dwarf2_per_objfile;
f4dc4d17
DE
7789 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
7790 struct type_unit_group *tu_group;
7791 void **slot;
7792 unsigned int line_offset;
7793 struct type_unit_group type_unit_group_for_lookup;
7794
7795 if (dwarf2_per_objfile->type_unit_groups == NULL)
7796 {
7797 dwarf2_per_objfile->type_unit_groups =
ed2dc618 7798 allocate_type_unit_groups_table (dwarf2_per_objfile->objfile);
f4dc4d17
DE
7799 }
7800
7801 /* Do we need to create a new group, or can we use an existing one? */
7802
7803 if (stmt_list)
7804 {
7805 line_offset = DW_UNSND (stmt_list);
7806 ++tu_stats->nr_symtab_sharers;
7807 }
7808 else
7809 {
7810 /* Ugh, no stmt_list. Rare, but we have to handle it.
7811 We can do various things here like create one group per TU or
7812 spread them over multiple groups to split up the expansion work.
7813 To avoid worst case scenarios (too many groups or too large groups)
7814 we, umm, group them in bunches. */
7815 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
7816 | (tu_stats->nr_stmt_less_type_units
7817 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
7818 ++tu_stats->nr_stmt_less_type_units;
7819 }
7820
094b34ac 7821 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
9c541725 7822 type_unit_group_for_lookup.hash.line_sect_off = (sect_offset) line_offset;
f4dc4d17
DE
7823 slot = htab_find_slot (dwarf2_per_objfile->type_unit_groups,
7824 &type_unit_group_for_lookup, INSERT);
7825 if (*slot != NULL)
7826 {
9a3c8263 7827 tu_group = (struct type_unit_group *) *slot;
f4dc4d17
DE
7828 gdb_assert (tu_group != NULL);
7829 }
7830 else
7831 {
9c541725 7832 sect_offset line_offset_struct = (sect_offset) line_offset;
094b34ac 7833 tu_group = create_type_unit_group (cu, line_offset_struct);
f4dc4d17
DE
7834 *slot = tu_group;
7835 ++tu_stats->nr_symtabs;
7836 }
7837
7838 return tu_group;
7839}
0018ea6f
DE
7840\f
7841/* Partial symbol tables. */
7842
7843/* Create a psymtab named NAME and assign it to PER_CU.
7844
7845 The caller must fill in the following details:
7846 dirname, textlow, texthigh. */
7847
7848static struct partial_symtab *
7849create_partial_symtab (struct dwarf2_per_cu_data *per_cu, const char *name)
7850{
e3b94546 7851 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
0018ea6f
DE
7852 struct partial_symtab *pst;
7853
18a94d75 7854 pst = start_psymtab_common (objfile, name, 0,
af5bf4ad
SM
7855 objfile->global_psymbols,
7856 objfile->static_psymbols);
0018ea6f
DE
7857
7858 pst->psymtabs_addrmap_supported = 1;
7859
7860 /* This is the glue that links PST into GDB's symbol API. */
7861 pst->read_symtab_private = per_cu;
7862 pst->read_symtab = dwarf2_read_symtab;
7863 per_cu->v.psymtab = pst;
7864
7865 return pst;
7866}
7867
b93601f3
TT
7868/* The DATA object passed to process_psymtab_comp_unit_reader has this
7869 type. */
7870
7871struct process_psymtab_comp_unit_data
7872{
7873 /* True if we are reading a DW_TAG_partial_unit. */
7874
7875 int want_partial_unit;
7876
7877 /* The "pretend" language that is used if the CU doesn't declare a
7878 language. */
7879
7880 enum language pretend_language;
7881};
7882
0018ea6f
DE
7883/* die_reader_func for process_psymtab_comp_unit. */
7884
7885static void
7886process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
d521ce57 7887 const gdb_byte *info_ptr,
0018ea6f
DE
7888 struct die_info *comp_unit_die,
7889 int has_children,
7890 void *data)
7891{
7892 struct dwarf2_cu *cu = reader->cu;
518817b3 7893 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
3e29f34a 7894 struct gdbarch *gdbarch = get_objfile_arch (objfile);
0018ea6f 7895 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
0018ea6f
DE
7896 CORE_ADDR baseaddr;
7897 CORE_ADDR best_lowpc = 0, best_highpc = 0;
7898 struct partial_symtab *pst;
3a2b436a 7899 enum pc_bounds_kind cu_bounds_kind;
0018ea6f 7900 const char *filename;
9a3c8263
SM
7901 struct process_psymtab_comp_unit_data *info
7902 = (struct process_psymtab_comp_unit_data *) data;
0018ea6f 7903
b93601f3 7904 if (comp_unit_die->tag == DW_TAG_partial_unit && !info->want_partial_unit)
0018ea6f
DE
7905 return;
7906
7907 gdb_assert (! per_cu->is_debug_types);
7908
b93601f3 7909 prepare_one_comp_unit (cu, comp_unit_die, info->pretend_language);
0018ea6f
DE
7910
7911 cu->list_in_scope = &file_symbols;
7912
7913 /* Allocate a new partial symbol table structure. */
7d45c7c3
KB
7914 filename = dwarf2_string_attr (comp_unit_die, DW_AT_name, cu);
7915 if (filename == NULL)
0018ea6f 7916 filename = "";
0018ea6f
DE
7917
7918 pst = create_partial_symtab (per_cu, filename);
7919
7920 /* This must be done before calling dwarf2_build_include_psymtabs. */
7d45c7c3 7921 pst->dirname = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
0018ea6f
DE
7922
7923 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
7924
7925 dwarf2_find_base_address (comp_unit_die, cu);
7926
7927 /* Possibly set the default values of LOWPC and HIGHPC from
7928 `DW_AT_ranges'. */
3a2b436a
JK
7929 cu_bounds_kind = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
7930 &best_highpc, cu, pst);
7931 if (cu_bounds_kind == PC_BOUNDS_HIGH_LOW && best_lowpc < best_highpc)
0018ea6f
DE
7932 /* Store the contiguous range if it is not empty; it can be empty for
7933 CUs with no code. */
7934 addrmap_set_empty (objfile->psymtabs_addrmap,
3e29f34a
MR
7935 gdbarch_adjust_dwarf2_addr (gdbarch,
7936 best_lowpc + baseaddr),
7937 gdbarch_adjust_dwarf2_addr (gdbarch,
7938 best_highpc + baseaddr) - 1,
7939 pst);
0018ea6f
DE
7940
7941 /* Check if comp unit has_children.
7942 If so, read the rest of the partial symbols from this comp unit.
7943 If not, there's no more debug_info for this comp unit. */
7944 if (has_children)
7945 {
7946 struct partial_die_info *first_die;
7947 CORE_ADDR lowpc, highpc;
7948
7949 lowpc = ((CORE_ADDR) -1);
7950 highpc = ((CORE_ADDR) 0);
7951
7952 first_die = load_partial_dies (reader, info_ptr, 1);
7953
7954 scan_partial_symbols (first_die, &lowpc, &highpc,
e385593e 7955 cu_bounds_kind <= PC_BOUNDS_INVALID, cu);
0018ea6f
DE
7956
7957 /* If we didn't find a lowpc, set it to highpc to avoid
7958 complaints from `maint check'. */
7959 if (lowpc == ((CORE_ADDR) -1))
7960 lowpc = highpc;
7961
7962 /* If the compilation unit didn't have an explicit address range,
7963 then use the information extracted from its child dies. */
e385593e 7964 if (cu_bounds_kind <= PC_BOUNDS_INVALID)
0018ea6f
DE
7965 {
7966 best_lowpc = lowpc;
7967 best_highpc = highpc;
7968 }
7969 }
3e29f34a
MR
7970 pst->textlow = gdbarch_adjust_dwarf2_addr (gdbarch, best_lowpc + baseaddr);
7971 pst->texthigh = gdbarch_adjust_dwarf2_addr (gdbarch, best_highpc + baseaddr);
0018ea6f 7972
8763cede 7973 end_psymtab_common (objfile, pst);
0018ea6f
DE
7974
7975 if (!VEC_empty (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs))
7976 {
7977 int i;
7978 int len = VEC_length (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs);
7979 struct dwarf2_per_cu_data *iter;
7980
7981 /* Fill in 'dependencies' here; we fill in 'users' in a
7982 post-pass. */
7983 pst->number_of_dependencies = len;
8d749320
SM
7984 pst->dependencies =
7985 XOBNEWVEC (&objfile->objfile_obstack, struct partial_symtab *, len);
0018ea6f
DE
7986 for (i = 0;
7987 VEC_iterate (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs,
7988 i, iter);
7989 ++i)
7990 pst->dependencies[i] = iter->v.psymtab;
7991
7992 VEC_free (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs);
7993 }
7994
7995 /* Get the list of files included in the current compilation unit,
7996 and build a psymtab for each of them. */
7997 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
7998
b4f54984 7999 if (dwarf_read_debug)
0018ea6f
DE
8000 {
8001 struct gdbarch *gdbarch = get_objfile_arch (objfile);
8002
8003 fprintf_unfiltered (gdb_stdlog,
9d8780f0 8004 "Psymtab for %s unit @%s: %s - %s"
0018ea6f
DE
8005 ", %d global, %d static syms\n",
8006 per_cu->is_debug_types ? "type" : "comp",
9d8780f0 8007 sect_offset_str (per_cu->sect_off),
0018ea6f
DE
8008 paddress (gdbarch, pst->textlow),
8009 paddress (gdbarch, pst->texthigh),
8010 pst->n_global_syms, pst->n_static_syms);
8011 }
8012}
8013
8014/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
8015 Process compilation unit THIS_CU for a psymtab. */
8016
8017static void
8018process_psymtab_comp_unit (struct dwarf2_per_cu_data *this_cu,
b93601f3
TT
8019 int want_partial_unit,
8020 enum language pretend_language)
0018ea6f
DE
8021{
8022 /* If this compilation unit was already read in, free the
8023 cached copy in order to read it in again. This is
8024 necessary because we skipped some symbols when we first
8025 read in the compilation unit (see load_partial_dies).
8026 This problem could be avoided, but the benefit is unclear. */
8027 if (this_cu->cu != NULL)
8028 free_one_cached_comp_unit (this_cu);
8029
f1902523 8030 if (this_cu->is_debug_types)
58f0c718
TT
8031 init_cutu_and_read_dies (this_cu, NULL, 0, 0, false,
8032 build_type_psymtabs_reader, NULL);
f1902523
JK
8033 else
8034 {
8035 process_psymtab_comp_unit_data info;
8036 info.want_partial_unit = want_partial_unit;
8037 info.pretend_language = pretend_language;
58f0c718 8038 init_cutu_and_read_dies (this_cu, NULL, 0, 0, false,
f1902523
JK
8039 process_psymtab_comp_unit_reader, &info);
8040 }
0018ea6f
DE
8041
8042 /* Age out any secondary CUs. */
ed2dc618 8043 age_cached_comp_units (this_cu->dwarf2_per_objfile);
0018ea6f 8044}
f4dc4d17
DE
8045
8046/* Reader function for build_type_psymtabs. */
8047
8048static void
8049build_type_psymtabs_reader (const struct die_reader_specs *reader,
d521ce57 8050 const gdb_byte *info_ptr,
f4dc4d17
DE
8051 struct die_info *type_unit_die,
8052 int has_children,
8053 void *data)
8054{
ed2dc618 8055 struct dwarf2_per_objfile *dwarf2_per_objfile
518817b3 8056 = reader->cu->per_cu->dwarf2_per_objfile;
f4dc4d17
DE
8057 struct objfile *objfile = dwarf2_per_objfile->objfile;
8058 struct dwarf2_cu *cu = reader->cu;
8059 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
0186c6a7 8060 struct signatured_type *sig_type;
f4dc4d17
DE
8061 struct type_unit_group *tu_group;
8062 struct attribute *attr;
8063 struct partial_die_info *first_die;
8064 CORE_ADDR lowpc, highpc;
8065 struct partial_symtab *pst;
8066
8067 gdb_assert (data == NULL);
0186c6a7
DE
8068 gdb_assert (per_cu->is_debug_types);
8069 sig_type = (struct signatured_type *) per_cu;
f4dc4d17
DE
8070
8071 if (! has_children)
8072 return;
8073
8074 attr = dwarf2_attr_no_follow (type_unit_die, DW_AT_stmt_list);
094b34ac 8075 tu_group = get_type_unit_group (cu, attr);
f4dc4d17 8076
0186c6a7 8077 VEC_safe_push (sig_type_ptr, tu_group->tus, sig_type);
f4dc4d17
DE
8078
8079 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
8080 cu->list_in_scope = &file_symbols;
8081 pst = create_partial_symtab (per_cu, "");
8082 pst->anonymous = 1;
8083
8084 first_die = load_partial_dies (reader, info_ptr, 1);
8085
8086 lowpc = (CORE_ADDR) -1;
8087 highpc = (CORE_ADDR) 0;
8088 scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
8089
8763cede 8090 end_psymtab_common (objfile, pst);
f4dc4d17
DE
8091}
8092
73051182
DE
8093/* Struct used to sort TUs by their abbreviation table offset. */
8094
8095struct tu_abbrev_offset
8096{
b2bdb8cf
SM
8097 tu_abbrev_offset (signatured_type *sig_type_, sect_offset abbrev_offset_)
8098 : sig_type (sig_type_), abbrev_offset (abbrev_offset_)
8099 {}
8100
8101 signatured_type *sig_type;
73051182
DE
8102 sect_offset abbrev_offset;
8103};
8104
484cf504 8105/* Helper routine for build_type_psymtabs_1, passed to std::sort. */
73051182 8106
484cf504
TT
8107static bool
8108sort_tu_by_abbrev_offset (const struct tu_abbrev_offset &a,
8109 const struct tu_abbrev_offset &b)
73051182 8110{
484cf504 8111 return a.abbrev_offset < b.abbrev_offset;
73051182
DE
8112}
8113
8114/* Efficiently read all the type units.
8115 This does the bulk of the work for build_type_psymtabs.
8116
8117 The efficiency is because we sort TUs by the abbrev table they use and
8118 only read each abbrev table once. In one program there are 200K TUs
8119 sharing 8K abbrev tables.
8120
8121 The main purpose of this function is to support building the
8122 dwarf2_per_objfile->type_unit_groups table.
8123 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
8124 can collapse the search space by grouping them by stmt_list.
8125 The savings can be significant, in the same program from above the 200K TUs
8126 share 8K stmt_list tables.
8127
8128 FUNC is expected to call get_type_unit_group, which will create the
8129 struct type_unit_group if necessary and add it to
8130 dwarf2_per_objfile->type_unit_groups. */
8131
8132static void
ed2dc618 8133build_type_psymtabs_1 (struct dwarf2_per_objfile *dwarf2_per_objfile)
73051182 8134{
73051182 8135 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
685af9cd 8136 abbrev_table_up abbrev_table;
73051182 8137 sect_offset abbrev_offset;
73051182
DE
8138
8139 /* It's up to the caller to not call us multiple times. */
8140 gdb_assert (dwarf2_per_objfile->type_unit_groups == NULL);
8141
b2bdb8cf 8142 if (dwarf2_per_objfile->all_type_units.empty ())
73051182
DE
8143 return;
8144
8145 /* TUs typically share abbrev tables, and there can be way more TUs than
8146 abbrev tables. Sort by abbrev table to reduce the number of times we
8147 read each abbrev table in.
8148 Alternatives are to punt or to maintain a cache of abbrev tables.
8149 This is simpler and efficient enough for now.
8150
8151 Later we group TUs by their DW_AT_stmt_list value (as this defines the
8152 symtab to use). Typically TUs with the same abbrev offset have the same
8153 stmt_list value too so in practice this should work well.
8154
8155 The basic algorithm here is:
8156
8157 sort TUs by abbrev table
8158 for each TU with same abbrev table:
8159 read abbrev table if first user
8160 read TU top level DIE
8161 [IWBN if DWO skeletons had DW_AT_stmt_list]
8162 call FUNC */
8163
b4f54984 8164 if (dwarf_read_debug)
73051182
DE
8165 fprintf_unfiltered (gdb_stdlog, "Building type unit groups ...\n");
8166
8167 /* Sort in a separate table to maintain the order of all_type_units
8168 for .gdb_index: TU indices directly index all_type_units. */
b2bdb8cf
SM
8169 std::vector<tu_abbrev_offset> sorted_by_abbrev;
8170 sorted_by_abbrev.reserve (dwarf2_per_objfile->all_type_units.size ());
8171
8172 for (signatured_type *sig_type : dwarf2_per_objfile->all_type_units)
8173 sorted_by_abbrev.emplace_back
8174 (sig_type, read_abbrev_offset (dwarf2_per_objfile,
8175 sig_type->per_cu.section,
8176 sig_type->per_cu.sect_off));
73051182 8177
484cf504
TT
8178 std::sort (sorted_by_abbrev.begin (), sorted_by_abbrev.end (),
8179 sort_tu_by_abbrev_offset);
73051182 8180
9c541725 8181 abbrev_offset = (sect_offset) ~(unsigned) 0;
73051182 8182
b2bdb8cf 8183 for (const tu_abbrev_offset &tu : sorted_by_abbrev)
73051182 8184 {
73051182
DE
8185 /* Switch to the next abbrev table if necessary. */
8186 if (abbrev_table == NULL
b2bdb8cf 8187 || tu.abbrev_offset != abbrev_offset)
73051182 8188 {
b2bdb8cf 8189 abbrev_offset = tu.abbrev_offset;
73051182 8190 abbrev_table =
ed2dc618
SM
8191 abbrev_table_read_table (dwarf2_per_objfile,
8192 &dwarf2_per_objfile->abbrev,
73051182
DE
8193 abbrev_offset);
8194 ++tu_stats->nr_uniq_abbrev_tables;
8195 }
8196
b2bdb8cf 8197 init_cutu_and_read_dies (&tu.sig_type->per_cu, abbrev_table.get (),
58f0c718 8198 0, 0, false, build_type_psymtabs_reader, NULL);
73051182 8199 }
6aa5f3a6 8200}
73051182 8201
6aa5f3a6
DE
8202/* Print collected type unit statistics. */
8203
8204static void
ed2dc618 8205print_tu_stats (struct dwarf2_per_objfile *dwarf2_per_objfile)
6aa5f3a6
DE
8206{
8207 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
8208
8209 fprintf_unfiltered (gdb_stdlog, "Type unit statistics:\n");
b2bdb8cf
SM
8210 fprintf_unfiltered (gdb_stdlog, " %zu TUs\n",
8211 dwarf2_per_objfile->all_type_units.size ());
6aa5f3a6
DE
8212 fprintf_unfiltered (gdb_stdlog, " %d uniq abbrev tables\n",
8213 tu_stats->nr_uniq_abbrev_tables);
8214 fprintf_unfiltered (gdb_stdlog, " %d symtabs from stmt_list entries\n",
8215 tu_stats->nr_symtabs);
8216 fprintf_unfiltered (gdb_stdlog, " %d symtab sharers\n",
8217 tu_stats->nr_symtab_sharers);
8218 fprintf_unfiltered (gdb_stdlog, " %d type units without a stmt_list\n",
8219 tu_stats->nr_stmt_less_type_units);
8220 fprintf_unfiltered (gdb_stdlog, " %d all_type_units reallocs\n",
8221 tu_stats->nr_all_type_units_reallocs);
73051182
DE
8222}
8223
f4dc4d17
DE
8224/* Traversal function for build_type_psymtabs. */
8225
8226static int
8227build_type_psymtab_dependencies (void **slot, void *info)
8228{
ed2dc618
SM
8229 struct dwarf2_per_objfile *dwarf2_per_objfile
8230 = (struct dwarf2_per_objfile *) info;
f4dc4d17
DE
8231 struct objfile *objfile = dwarf2_per_objfile->objfile;
8232 struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
094b34ac 8233 struct dwarf2_per_cu_data *per_cu = &tu_group->per_cu;
f4dc4d17 8234 struct partial_symtab *pst = per_cu->v.psymtab;
0186c6a7
DE
8235 int len = VEC_length (sig_type_ptr, tu_group->tus);
8236 struct signatured_type *iter;
f4dc4d17
DE
8237 int i;
8238
8239 gdb_assert (len > 0);
0186c6a7 8240 gdb_assert (IS_TYPE_UNIT_GROUP (per_cu));
f4dc4d17
DE
8241
8242 pst->number_of_dependencies = len;
8d749320
SM
8243 pst->dependencies =
8244 XOBNEWVEC (&objfile->objfile_obstack, struct partial_symtab *, len);
f4dc4d17 8245 for (i = 0;
0186c6a7 8246 VEC_iterate (sig_type_ptr, tu_group->tus, i, iter);
f4dc4d17
DE
8247 ++i)
8248 {
0186c6a7
DE
8249 gdb_assert (iter->per_cu.is_debug_types);
8250 pst->dependencies[i] = iter->per_cu.v.psymtab;
796a7ff8 8251 iter->type_unit_group = tu_group;
f4dc4d17
DE
8252 }
8253
0186c6a7 8254 VEC_free (sig_type_ptr, tu_group->tus);
348e048f
DE
8255
8256 return 1;
8257}
8258
8259/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
8260 Build partial symbol tables for the .debug_types comp-units. */
8261
8262static void
ed2dc618 8263build_type_psymtabs (struct dwarf2_per_objfile *dwarf2_per_objfile)
348e048f 8264{
ed2dc618 8265 if (! create_all_type_units (dwarf2_per_objfile))
348e048f
DE
8266 return;
8267
ed2dc618 8268 build_type_psymtabs_1 (dwarf2_per_objfile);
6aa5f3a6 8269}
f4dc4d17 8270
6aa5f3a6
DE
8271/* Traversal function for process_skeletonless_type_unit.
8272 Read a TU in a DWO file and build partial symbols for it. */
8273
8274static int
8275process_skeletonless_type_unit (void **slot, void *info)
8276{
8277 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
ed2dc618
SM
8278 struct dwarf2_per_objfile *dwarf2_per_objfile
8279 = (struct dwarf2_per_objfile *) info;
6aa5f3a6
DE
8280 struct signatured_type find_entry, *entry;
8281
8282 /* If this TU doesn't exist in the global table, add it and read it in. */
8283
8284 if (dwarf2_per_objfile->signatured_types == NULL)
8285 {
8286 dwarf2_per_objfile->signatured_types
ed2dc618 8287 = allocate_signatured_type_table (dwarf2_per_objfile->objfile);
6aa5f3a6
DE
8288 }
8289
8290 find_entry.signature = dwo_unit->signature;
8291 slot = htab_find_slot (dwarf2_per_objfile->signatured_types, &find_entry,
8292 INSERT);
8293 /* If we've already seen this type there's nothing to do. What's happening
8294 is we're doing our own version of comdat-folding here. */
8295 if (*slot != NULL)
8296 return 1;
8297
8298 /* This does the job that create_all_type_units would have done for
8299 this TU. */
ed2dc618
SM
8300 entry = add_type_unit (dwarf2_per_objfile, dwo_unit->signature, slot);
8301 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, entry, dwo_unit);
6aa5f3a6
DE
8302 *slot = entry;
8303
8304 /* This does the job that build_type_psymtabs_1 would have done. */
58f0c718 8305 init_cutu_and_read_dies (&entry->per_cu, NULL, 0, 0, false,
6aa5f3a6
DE
8306 build_type_psymtabs_reader, NULL);
8307
8308 return 1;
8309}
8310
8311/* Traversal function for process_skeletonless_type_units. */
8312
8313static int
8314process_dwo_file_for_skeletonless_type_units (void **slot, void *info)
8315{
8316 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
8317
8318 if (dwo_file->tus != NULL)
8319 {
8320 htab_traverse_noresize (dwo_file->tus,
8321 process_skeletonless_type_unit, info);
8322 }
8323
8324 return 1;
8325}
8326
8327/* Scan all TUs of DWO files, verifying we've processed them.
8328 This is needed in case a TU was emitted without its skeleton.
8329 Note: This can't be done until we know what all the DWO files are. */
8330
8331static void
ed2dc618 8332process_skeletonless_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
6aa5f3a6
DE
8333{
8334 /* Skeletonless TUs in DWP files without .gdb_index is not supported yet. */
ed2dc618 8335 if (get_dwp_file (dwarf2_per_objfile) == NULL
6aa5f3a6
DE
8336 && dwarf2_per_objfile->dwo_files != NULL)
8337 {
8338 htab_traverse_noresize (dwarf2_per_objfile->dwo_files,
8339 process_dwo_file_for_skeletonless_type_units,
ed2dc618 8340 dwarf2_per_objfile);
6aa5f3a6 8341 }
348e048f
DE
8342}
8343
ed2dc618 8344/* Compute the 'user' field for each psymtab in DWARF2_PER_OBJFILE. */
95554aad
TT
8345
8346static void
ed2dc618 8347set_partial_user (struct dwarf2_per_objfile *dwarf2_per_objfile)
95554aad 8348{
b76e467d 8349 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
95554aad 8350 {
95554aad 8351 struct partial_symtab *pst = per_cu->v.psymtab;
95554aad 8352
36586728
TT
8353 if (pst == NULL)
8354 continue;
8355
b76e467d 8356 for (int j = 0; j < pst->number_of_dependencies; ++j)
95554aad
TT
8357 {
8358 /* Set the 'user' field only if it is not already set. */
8359 if (pst->dependencies[j]->user == NULL)
8360 pst->dependencies[j]->user = pst;
8361 }
8362 }
8363}
8364
93311388
DE
8365/* Build the partial symbol table by doing a quick pass through the
8366 .debug_info and .debug_abbrev sections. */
72bf9492 8367
93311388 8368static void
ed2dc618 8369dwarf2_build_psymtabs_hard (struct dwarf2_per_objfile *dwarf2_per_objfile)
93311388 8370{
ed2dc618 8371 struct objfile *objfile = dwarf2_per_objfile->objfile;
93311388 8372
b4f54984 8373 if (dwarf_read_debug)
45cfd468
DE
8374 {
8375 fprintf_unfiltered (gdb_stdlog, "Building psymtabs of objfile %s ...\n",
4262abfb 8376 objfile_name (objfile));
45cfd468
DE
8377 }
8378
98bfdba5
PA
8379 dwarf2_per_objfile->reading_partial_symbols = 1;
8380
be391dca 8381 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
91c24f0a 8382
93311388
DE
8383 /* Any cached compilation units will be linked by the per-objfile
8384 read_in_chain. Make sure to free them when we're done. */
11ed8cad 8385 free_cached_comp_units freer (dwarf2_per_objfile);
72bf9492 8386
ed2dc618 8387 build_type_psymtabs (dwarf2_per_objfile);
348e048f 8388
ed2dc618 8389 create_all_comp_units (dwarf2_per_objfile);
c906108c 8390
60606b2c
TT
8391 /* Create a temporary address map on a temporary obstack. We later
8392 copy this to the final obstack. */
8268c778 8393 auto_obstack temp_obstack;
791afaa2
TT
8394
8395 scoped_restore save_psymtabs_addrmap
8396 = make_scoped_restore (&objfile->psymtabs_addrmap,
8397 addrmap_create_mutable (&temp_obstack));
72bf9492 8398
b76e467d
SM
8399 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
8400 process_psymtab_comp_unit (per_cu, 0, language_minimal);
ff013f42 8401
6aa5f3a6 8402 /* This has to wait until we read the CUs, we need the list of DWOs. */
ed2dc618 8403 process_skeletonless_type_units (dwarf2_per_objfile);
6aa5f3a6
DE
8404
8405 /* Now that all TUs have been processed we can fill in the dependencies. */
8406 if (dwarf2_per_objfile->type_unit_groups != NULL)
8407 {
8408 htab_traverse_noresize (dwarf2_per_objfile->type_unit_groups,
ed2dc618 8409 build_type_psymtab_dependencies, dwarf2_per_objfile);
6aa5f3a6
DE
8410 }
8411
b4f54984 8412 if (dwarf_read_debug)
ed2dc618 8413 print_tu_stats (dwarf2_per_objfile);
6aa5f3a6 8414
ed2dc618 8415 set_partial_user (dwarf2_per_objfile);
95554aad 8416
ff013f42
JK
8417 objfile->psymtabs_addrmap = addrmap_create_fixed (objfile->psymtabs_addrmap,
8418 &objfile->objfile_obstack);
791afaa2
TT
8419 /* At this point we want to keep the address map. */
8420 save_psymtabs_addrmap.release ();
ff013f42 8421
b4f54984 8422 if (dwarf_read_debug)
45cfd468 8423 fprintf_unfiltered (gdb_stdlog, "Done building psymtabs of %s\n",
4262abfb 8424 objfile_name (objfile));
ae038cb0
DJ
8425}
8426
3019eac3 8427/* die_reader_func for load_partial_comp_unit. */
ae038cb0
DJ
8428
8429static void
dee91e82 8430load_partial_comp_unit_reader (const struct die_reader_specs *reader,
d521ce57 8431 const gdb_byte *info_ptr,
dee91e82
DE
8432 struct die_info *comp_unit_die,
8433 int has_children,
8434 void *data)
ae038cb0 8435{
dee91e82 8436 struct dwarf2_cu *cu = reader->cu;
ae038cb0 8437
95554aad 8438 prepare_one_comp_unit (cu, comp_unit_die, language_minimal);
ae038cb0 8439
ae038cb0
DJ
8440 /* Check if comp unit has_children.
8441 If so, read the rest of the partial symbols from this comp unit.
0963b4bd 8442 If not, there's no more debug_info for this comp unit. */
d85a05f0 8443 if (has_children)
dee91e82
DE
8444 load_partial_dies (reader, info_ptr, 0);
8445}
98bfdba5 8446
dee91e82
DE
8447/* Load the partial DIEs for a secondary CU into memory.
8448 This is also used when rereading a primary CU with load_all_dies. */
c5b7e1cb 8449
dee91e82
DE
8450static void
8451load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu)
8452{
58f0c718 8453 init_cutu_and_read_dies (this_cu, NULL, 1, 1, false,
f4dc4d17 8454 load_partial_comp_unit_reader, NULL);
ae038cb0
DJ
8455}
8456
ae038cb0 8457static void
ed2dc618 8458read_comp_units_from_section (struct dwarf2_per_objfile *dwarf2_per_objfile,
36586728 8459 struct dwarf2_section_info *section,
f1902523 8460 struct dwarf2_section_info *abbrev_section,
b76e467d 8461 unsigned int is_dwz)
ae038cb0 8462{
d521ce57 8463 const gdb_byte *info_ptr;
ed2dc618 8464 struct objfile *objfile = dwarf2_per_objfile->objfile;
be391dca 8465
b4f54984 8466 if (dwarf_read_debug)
bf6af496 8467 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s\n",
a32a8923
DE
8468 get_section_name (section),
8469 get_section_file_name (section));
bf6af496 8470
36586728 8471 dwarf2_read_section (objfile, section);
ae038cb0 8472
36586728 8473 info_ptr = section->buffer;
6e70227d 8474
36586728 8475 while (info_ptr < section->buffer + section->size)
ae038cb0 8476 {
ae038cb0 8477 struct dwarf2_per_cu_data *this_cu;
ae038cb0 8478
9c541725 8479 sect_offset sect_off = (sect_offset) (info_ptr - section->buffer);
ae038cb0 8480
f1902523 8481 comp_unit_head cu_header;
ed2dc618
SM
8482 read_and_check_comp_unit_head (dwarf2_per_objfile, &cu_header, section,
8483 abbrev_section, info_ptr,
8484 rcuh_kind::COMPILE);
ae038cb0
DJ
8485
8486 /* Save the compilation unit for later lookup. */
f1902523
JK
8487 if (cu_header.unit_type != DW_UT_type)
8488 {
8489 this_cu = XOBNEW (&objfile->objfile_obstack,
8490 struct dwarf2_per_cu_data);
8491 memset (this_cu, 0, sizeof (*this_cu));
8492 }
8493 else
8494 {
8495 auto sig_type = XOBNEW (&objfile->objfile_obstack,
8496 struct signatured_type);
8497 memset (sig_type, 0, sizeof (*sig_type));
8498 sig_type->signature = cu_header.signature;
8499 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
8500 this_cu = &sig_type->per_cu;
8501 }
8502 this_cu->is_debug_types = (cu_header.unit_type == DW_UT_type);
9c541725 8503 this_cu->sect_off = sect_off;
f1902523 8504 this_cu->length = cu_header.length + cu_header.initial_length_size;
36586728 8505 this_cu->is_dwz = is_dwz;
e3b94546 8506 this_cu->dwarf2_per_objfile = dwarf2_per_objfile;
8a0459fd 8507 this_cu->section = section;
ae038cb0 8508
b76e467d 8509 dwarf2_per_objfile->all_comp_units.push_back (this_cu);
ae038cb0
DJ
8510
8511 info_ptr = info_ptr + this_cu->length;
8512 }
36586728
TT
8513}
8514
8515/* Create a list of all compilation units in OBJFILE.
8516 This is only done for -readnow and building partial symtabs. */
8517
8518static void
ed2dc618 8519create_all_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
36586728 8520{
b76e467d 8521 gdb_assert (dwarf2_per_objfile->all_comp_units.empty ());
ed2dc618 8522 read_comp_units_from_section (dwarf2_per_objfile, &dwarf2_per_objfile->info,
b76e467d 8523 &dwarf2_per_objfile->abbrev, 0);
36586728 8524
b76e467d 8525 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
4db1a1dc 8526 if (dwz != NULL)
ed2dc618 8527 read_comp_units_from_section (dwarf2_per_objfile, &dwz->info, &dwz->abbrev,
b76e467d 8528 1);
c906108c
SS
8529}
8530
5734ee8b 8531/* Process all loaded DIEs for compilation unit CU, starting at
cdc07690 8532 FIRST_DIE. The caller should pass SET_ADDRMAP == 1 if the compilation
5734ee8b 8533 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
cdc07690
YQ
8534 DW_AT_ranges). See the comments of add_partial_subprogram on how
8535 SET_ADDRMAP is used and how *LOWPC and *HIGHPC are updated. */
c906108c 8536
72bf9492
DJ
8537static void
8538scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
cdc07690
YQ
8539 CORE_ADDR *highpc, int set_addrmap,
8540 struct dwarf2_cu *cu)
c906108c 8541{
72bf9492 8542 struct partial_die_info *pdi;
c906108c 8543
91c24f0a
DC
8544 /* Now, march along the PDI's, descending into ones which have
8545 interesting children but skipping the children of the other ones,
8546 until we reach the end of the compilation unit. */
c906108c 8547
72bf9492 8548 pdi = first_die;
91c24f0a 8549
72bf9492
DJ
8550 while (pdi != NULL)
8551 {
52356b79 8552 pdi->fixup (cu);
c906108c 8553
f55ee35c 8554 /* Anonymous namespaces or modules have no name but have interesting
91c24f0a
DC
8555 children, so we need to look at them. Ditto for anonymous
8556 enums. */
933c6fe4 8557
72bf9492 8558 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
95554aad 8559 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
b1dc1806
XR
8560 || pdi->tag == DW_TAG_imported_unit
8561 || pdi->tag == DW_TAG_inlined_subroutine)
c906108c 8562 {
72bf9492 8563 switch (pdi->tag)
c906108c
SS
8564 {
8565 case DW_TAG_subprogram:
b1dc1806 8566 case DW_TAG_inlined_subroutine:
cdc07690 8567 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
c906108c 8568 break;
72929c62 8569 case DW_TAG_constant:
c906108c
SS
8570 case DW_TAG_variable:
8571 case DW_TAG_typedef:
91c24f0a 8572 case DW_TAG_union_type:
72bf9492 8573 if (!pdi->is_declaration)
63d06c5c 8574 {
72bf9492 8575 add_partial_symbol (pdi, cu);
63d06c5c
DC
8576 }
8577 break;
c906108c 8578 case DW_TAG_class_type:
680b30c7 8579 case DW_TAG_interface_type:
c906108c 8580 case DW_TAG_structure_type:
72bf9492 8581 if (!pdi->is_declaration)
c906108c 8582 {
72bf9492 8583 add_partial_symbol (pdi, cu);
c906108c 8584 }
b7fee5a3
KS
8585 if ((cu->language == language_rust
8586 || cu->language == language_cplus) && pdi->has_children)
e98c9e7c
TT
8587 scan_partial_symbols (pdi->die_child, lowpc, highpc,
8588 set_addrmap, cu);
c906108c 8589 break;
91c24f0a 8590 case DW_TAG_enumeration_type:
72bf9492
DJ
8591 if (!pdi->is_declaration)
8592 add_partial_enumeration (pdi, cu);
c906108c
SS
8593 break;
8594 case DW_TAG_base_type:
a02abb62 8595 case DW_TAG_subrange_type:
c906108c 8596 /* File scope base type definitions are added to the partial
c5aa993b 8597 symbol table. */
72bf9492 8598 add_partial_symbol (pdi, cu);
c906108c 8599 break;
d9fa45fe 8600 case DW_TAG_namespace:
cdc07690 8601 add_partial_namespace (pdi, lowpc, highpc, set_addrmap, cu);
91c24f0a 8602 break;
5d7cb8df 8603 case DW_TAG_module:
cdc07690 8604 add_partial_module (pdi, lowpc, highpc, set_addrmap, cu);
5d7cb8df 8605 break;
95554aad
TT
8606 case DW_TAG_imported_unit:
8607 {
8608 struct dwarf2_per_cu_data *per_cu;
8609
f4dc4d17
DE
8610 /* For now we don't handle imported units in type units. */
8611 if (cu->per_cu->is_debug_types)
8612 {
8613 error (_("Dwarf Error: DW_TAG_imported_unit is not"
8614 " supported in type units [in module %s]"),
518817b3 8615 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
f4dc4d17
DE
8616 }
8617
e3b94546
SM
8618 per_cu = dwarf2_find_containing_comp_unit
8619 (pdi->d.sect_off, pdi->is_dwz,
518817b3 8620 cu->per_cu->dwarf2_per_objfile);
95554aad
TT
8621
8622 /* Go read the partial unit, if needed. */
8623 if (per_cu->v.psymtab == NULL)
b93601f3 8624 process_psymtab_comp_unit (per_cu, 1, cu->language);
95554aad 8625
f4dc4d17 8626 VEC_safe_push (dwarf2_per_cu_ptr,
796a7ff8 8627 cu->per_cu->imported_symtabs, per_cu);
95554aad
TT
8628 }
8629 break;
74921315
KS
8630 case DW_TAG_imported_declaration:
8631 add_partial_symbol (pdi, cu);
8632 break;
c906108c
SS
8633 default:
8634 break;
8635 }
8636 }
8637
72bf9492
DJ
8638 /* If the die has a sibling, skip to the sibling. */
8639
8640 pdi = pdi->die_sibling;
8641 }
8642}
8643
8644/* Functions used to compute the fully scoped name of a partial DIE.
91c24f0a 8645
72bf9492 8646 Normally, this is simple. For C++, the parent DIE's fully scoped
9c37b5ae 8647 name is concatenated with "::" and the partial DIE's name.
72bf9492
DJ
8648 Enumerators are an exception; they use the scope of their parent
8649 enumeration type, i.e. the name of the enumeration type is not
8650 prepended to the enumerator.
91c24f0a 8651
72bf9492
DJ
8652 There are two complexities. One is DW_AT_specification; in this
8653 case "parent" means the parent of the target of the specification,
8654 instead of the direct parent of the DIE. The other is compilers
8655 which do not emit DW_TAG_namespace; in this case we try to guess
8656 the fully qualified name of structure types from their members'
8657 linkage names. This must be done using the DIE's children rather
8658 than the children of any DW_AT_specification target. We only need
8659 to do this for structures at the top level, i.e. if the target of
8660 any DW_AT_specification (if any; otherwise the DIE itself) does not
8661 have a parent. */
8662
8663/* Compute the scope prefix associated with PDI's parent, in
8664 compilation unit CU. The result will be allocated on CU's
8665 comp_unit_obstack, or a copy of the already allocated PDI->NAME
8666 field. NULL is returned if no prefix is necessary. */
15d034d0 8667static const char *
72bf9492
DJ
8668partial_die_parent_scope (struct partial_die_info *pdi,
8669 struct dwarf2_cu *cu)
8670{
15d034d0 8671 const char *grandparent_scope;
72bf9492 8672 struct partial_die_info *parent, *real_pdi;
91c24f0a 8673
72bf9492
DJ
8674 /* We need to look at our parent DIE; if we have a DW_AT_specification,
8675 then this means the parent of the specification DIE. */
8676
8677 real_pdi = pdi;
72bf9492 8678 while (real_pdi->has_specification)
36586728
TT
8679 real_pdi = find_partial_die (real_pdi->spec_offset,
8680 real_pdi->spec_is_dwz, cu);
72bf9492
DJ
8681
8682 parent = real_pdi->die_parent;
8683 if (parent == NULL)
8684 return NULL;
8685
8686 if (parent->scope_set)
8687 return parent->scope;
8688
52356b79 8689 parent->fixup (cu);
72bf9492 8690
10b3939b 8691 grandparent_scope = partial_die_parent_scope (parent, cu);
72bf9492 8692
acebe513
UW
8693 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
8694 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
8695 Work around this problem here. */
8696 if (cu->language == language_cplus
6e70227d 8697 && parent->tag == DW_TAG_namespace
acebe513
UW
8698 && strcmp (parent->name, "::") == 0
8699 && grandparent_scope == NULL)
8700 {
8701 parent->scope = NULL;
8702 parent->scope_set = 1;
8703 return NULL;
8704 }
8705
9c6c53f7
SA
8706 if (pdi->tag == DW_TAG_enumerator)
8707 /* Enumerators should not get the name of the enumeration as a prefix. */
8708 parent->scope = grandparent_scope;
8709 else if (parent->tag == DW_TAG_namespace
f55ee35c 8710 || parent->tag == DW_TAG_module
72bf9492
DJ
8711 || parent->tag == DW_TAG_structure_type
8712 || parent->tag == DW_TAG_class_type
680b30c7 8713 || parent->tag == DW_TAG_interface_type
ceeb3d5a
TT
8714 || parent->tag == DW_TAG_union_type
8715 || parent->tag == DW_TAG_enumeration_type)
72bf9492
DJ
8716 {
8717 if (grandparent_scope == NULL)
8718 parent->scope = parent->name;
8719 else
3e43a32a
MS
8720 parent->scope = typename_concat (&cu->comp_unit_obstack,
8721 grandparent_scope,
f55ee35c 8722 parent->name, 0, cu);
72bf9492 8723 }
72bf9492
DJ
8724 else
8725 {
8726 /* FIXME drow/2004-04-01: What should we be doing with
8727 function-local names? For partial symbols, we should probably be
8728 ignoring them. */
b98664d3 8729 complaint (_("unhandled containing DIE tag %d for DIE at %s"),
9d8780f0 8730 parent->tag, sect_offset_str (pdi->sect_off));
72bf9492 8731 parent->scope = grandparent_scope;
c906108c
SS
8732 }
8733
72bf9492
DJ
8734 parent->scope_set = 1;
8735 return parent->scope;
8736}
8737
8738/* Return the fully scoped name associated with PDI, from compilation unit
8739 CU. The result will be allocated with malloc. */
4568ecf9 8740
72bf9492
DJ
8741static char *
8742partial_die_full_name (struct partial_die_info *pdi,
8743 struct dwarf2_cu *cu)
8744{
15d034d0 8745 const char *parent_scope;
72bf9492 8746
98bfdba5
PA
8747 /* If this is a template instantiation, we can not work out the
8748 template arguments from partial DIEs. So, unfortunately, we have
8749 to go through the full DIEs. At least any work we do building
8750 types here will be reused if full symbols are loaded later. */
8751 if (pdi->has_template_arguments)
8752 {
52356b79 8753 pdi->fixup (cu);
98bfdba5
PA
8754
8755 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
8756 {
8757 struct die_info *die;
8758 struct attribute attr;
8759 struct dwarf2_cu *ref_cu = cu;
8760
b64f50a1 8761 /* DW_FORM_ref_addr is using section offset. */
b4069958 8762 attr.name = (enum dwarf_attribute) 0;
98bfdba5 8763 attr.form = DW_FORM_ref_addr;
9c541725 8764 attr.u.unsnd = to_underlying (pdi->sect_off);
98bfdba5
PA
8765 die = follow_die_ref (NULL, &attr, &ref_cu);
8766
8767 return xstrdup (dwarf2_full_name (NULL, die, ref_cu));
8768 }
8769 }
8770
72bf9492
DJ
8771 parent_scope = partial_die_parent_scope (pdi, cu);
8772 if (parent_scope == NULL)
8773 return NULL;
8774 else
f55ee35c 8775 return typename_concat (NULL, parent_scope, pdi->name, 0, cu);
c906108c
SS
8776}
8777
8778static void
72bf9492 8779add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
c906108c 8780{
518817b3
SM
8781 struct dwarf2_per_objfile *dwarf2_per_objfile
8782 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 8783 struct objfile *objfile = dwarf2_per_objfile->objfile;
3e29f34a 8784 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c 8785 CORE_ADDR addr = 0;
15d034d0 8786 const char *actual_name = NULL;
e142c38c 8787 CORE_ADDR baseaddr;
15d034d0 8788 char *built_actual_name;
e142c38c
DJ
8789
8790 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 8791
15d034d0
TT
8792 built_actual_name = partial_die_full_name (pdi, cu);
8793 if (built_actual_name != NULL)
8794 actual_name = built_actual_name;
63d06c5c 8795
72bf9492
DJ
8796 if (actual_name == NULL)
8797 actual_name = pdi->name;
8798
c906108c
SS
8799 switch (pdi->tag)
8800 {
b1dc1806 8801 case DW_TAG_inlined_subroutine:
c906108c 8802 case DW_TAG_subprogram:
3e29f34a 8803 addr = gdbarch_adjust_dwarf2_addr (gdbarch, pdi->lowpc + baseaddr);
2cfa0c8d 8804 if (pdi->is_external || cu->language == language_ada)
c906108c 8805 {
2cfa0c8d
JB
8806 /* brobecker/2007-12-26: Normally, only "external" DIEs are part
8807 of the global scope. But in Ada, we want to be able to access
8808 nested procedures globally. So all Ada subprograms are stored
8809 in the global scope. */
f47fb265 8810 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 8811 built_actual_name != NULL,
f47fb265
MS
8812 VAR_DOMAIN, LOC_BLOCK,
8813 &objfile->global_psymbols,
1762568f 8814 addr, cu->language, objfile);
c906108c
SS
8815 }
8816 else
8817 {
f47fb265 8818 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 8819 built_actual_name != NULL,
f47fb265
MS
8820 VAR_DOMAIN, LOC_BLOCK,
8821 &objfile->static_psymbols,
1762568f 8822 addr, cu->language, objfile);
c906108c 8823 }
0c1b455e
TT
8824
8825 if (pdi->main_subprogram && actual_name != NULL)
8826 set_objfile_main_name (objfile, actual_name, cu->language);
c906108c 8827 break;
72929c62
JB
8828 case DW_TAG_constant:
8829 {
af5bf4ad 8830 std::vector<partial_symbol *> *list;
72929c62
JB
8831
8832 if (pdi->is_external)
8833 list = &objfile->global_psymbols;
8834 else
8835 list = &objfile->static_psymbols;
f47fb265 8836 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 8837 built_actual_name != NULL, VAR_DOMAIN, LOC_STATIC,
1762568f 8838 list, 0, cu->language, objfile);
72929c62
JB
8839 }
8840 break;
c906108c 8841 case DW_TAG_variable:
95554aad
TT
8842 if (pdi->d.locdesc)
8843 addr = decode_locdesc (pdi->d.locdesc, cu);
caac4577 8844
95554aad 8845 if (pdi->d.locdesc
caac4577
JG
8846 && addr == 0
8847 && !dwarf2_per_objfile->has_section_at_zero)
8848 {
8849 /* A global or static variable may also have been stripped
8850 out by the linker if unused, in which case its address
8851 will be nullified; do not add such variables into partial
8852 symbol table then. */
8853 }
8854 else if (pdi->is_external)
c906108c
SS
8855 {
8856 /* Global Variable.
8857 Don't enter into the minimal symbol tables as there is
8858 a minimal symbol table entry from the ELF symbols already.
8859 Enter into partial symbol table if it has a location
8860 descriptor or a type.
8861 If the location descriptor is missing, new_symbol will create
8862 a LOC_UNRESOLVED symbol, the address of the variable will then
8863 be determined from the minimal symbol table whenever the variable
8864 is referenced.
8865 The address for the partial symbol table entry is not
8866 used by GDB, but it comes in handy for debugging partial symbol
8867 table building. */
8868
95554aad 8869 if (pdi->d.locdesc || pdi->has_type)
f47fb265 8870 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 8871 built_actual_name != NULL,
f47fb265
MS
8872 VAR_DOMAIN, LOC_STATIC,
8873 &objfile->global_psymbols,
1762568f 8874 addr + baseaddr,
f47fb265 8875 cu->language, objfile);
c906108c
SS
8876 }
8877 else
8878 {
ff908ebf
AW
8879 int has_loc = pdi->d.locdesc != NULL;
8880
8881 /* Static Variable. Skip symbols whose value we cannot know (those
8882 without location descriptors or constant values). */
8883 if (!has_loc && !pdi->has_const_value)
decbce07 8884 {
15d034d0 8885 xfree (built_actual_name);
decbce07
MS
8886 return;
8887 }
ff908ebf 8888
f47fb265 8889 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 8890 built_actual_name != NULL,
f47fb265
MS
8891 VAR_DOMAIN, LOC_STATIC,
8892 &objfile->static_psymbols,
ff908ebf 8893 has_loc ? addr + baseaddr : (CORE_ADDR) 0,
f47fb265 8894 cu->language, objfile);
c906108c
SS
8895 }
8896 break;
8897 case DW_TAG_typedef:
8898 case DW_TAG_base_type:
a02abb62 8899 case DW_TAG_subrange_type:
38d518c9 8900 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 8901 built_actual_name != NULL,
176620f1 8902 VAR_DOMAIN, LOC_TYPEDEF,
c906108c 8903 &objfile->static_psymbols,
1762568f 8904 0, cu->language, objfile);
c906108c 8905 break;
74921315 8906 case DW_TAG_imported_declaration:
72bf9492
DJ
8907 case DW_TAG_namespace:
8908 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 8909 built_actual_name != NULL,
72bf9492
DJ
8910 VAR_DOMAIN, LOC_TYPEDEF,
8911 &objfile->global_psymbols,
1762568f 8912 0, cu->language, objfile);
72bf9492 8913 break;
530e8392
KB
8914 case DW_TAG_module:
8915 add_psymbol_to_list (actual_name, strlen (actual_name),
8916 built_actual_name != NULL,
8917 MODULE_DOMAIN, LOC_TYPEDEF,
8918 &objfile->global_psymbols,
1762568f 8919 0, cu->language, objfile);
530e8392 8920 break;
c906108c 8921 case DW_TAG_class_type:
680b30c7 8922 case DW_TAG_interface_type:
c906108c
SS
8923 case DW_TAG_structure_type:
8924 case DW_TAG_union_type:
8925 case DW_TAG_enumeration_type:
fa4028e9
JB
8926 /* Skip external references. The DWARF standard says in the section
8927 about "Structure, Union, and Class Type Entries": "An incomplete
8928 structure, union or class type is represented by a structure,
8929 union or class entry that does not have a byte size attribute
8930 and that has a DW_AT_declaration attribute." */
8931 if (!pdi->has_byte_size && pdi->is_declaration)
decbce07 8932 {
15d034d0 8933 xfree (built_actual_name);
decbce07
MS
8934 return;
8935 }
fa4028e9 8936
63d06c5c
DC
8937 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
8938 static vs. global. */
38d518c9 8939 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 8940 built_actual_name != NULL,
176620f1 8941 STRUCT_DOMAIN, LOC_TYPEDEF,
9c37b5ae 8942 cu->language == language_cplus
63d06c5c
DC
8943 ? &objfile->global_psymbols
8944 : &objfile->static_psymbols,
1762568f 8945 0, cu->language, objfile);
c906108c 8946
c906108c
SS
8947 break;
8948 case DW_TAG_enumerator:
38d518c9 8949 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 8950 built_actual_name != NULL,
176620f1 8951 VAR_DOMAIN, LOC_CONST,
9c37b5ae 8952 cu->language == language_cplus
f6fe98ef
DJ
8953 ? &objfile->global_psymbols
8954 : &objfile->static_psymbols,
1762568f 8955 0, cu->language, objfile);
c906108c
SS
8956 break;
8957 default:
8958 break;
8959 }
5c4e30ca 8960
15d034d0 8961 xfree (built_actual_name);
c906108c
SS
8962}
8963
5c4e30ca
DC
8964/* Read a partial die corresponding to a namespace; also, add a symbol
8965 corresponding to that namespace to the symbol table. NAMESPACE is
8966 the name of the enclosing namespace. */
91c24f0a 8967
72bf9492
DJ
8968static void
8969add_partial_namespace (struct partial_die_info *pdi,
91c24f0a 8970 CORE_ADDR *lowpc, CORE_ADDR *highpc,
cdc07690 8971 int set_addrmap, struct dwarf2_cu *cu)
91c24f0a 8972{
72bf9492 8973 /* Add a symbol for the namespace. */
e7c27a73 8974
72bf9492 8975 add_partial_symbol (pdi, cu);
5c4e30ca
DC
8976
8977 /* Now scan partial symbols in that namespace. */
8978
91c24f0a 8979 if (pdi->has_children)
cdc07690 8980 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
91c24f0a
DC
8981}
8982
5d7cb8df
JK
8983/* Read a partial die corresponding to a Fortran module. */
8984
8985static void
8986add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
cdc07690 8987 CORE_ADDR *highpc, int set_addrmap, struct dwarf2_cu *cu)
5d7cb8df 8988{
530e8392
KB
8989 /* Add a symbol for the namespace. */
8990
8991 add_partial_symbol (pdi, cu);
8992
f55ee35c 8993 /* Now scan partial symbols in that module. */
5d7cb8df
JK
8994
8995 if (pdi->has_children)
cdc07690 8996 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
5d7cb8df
JK
8997}
8998
b1dc1806
XR
8999/* Read a partial die corresponding to a subprogram or an inlined
9000 subprogram and create a partial symbol for that subprogram.
9001 When the CU language allows it, this routine also defines a partial
9002 symbol for each nested subprogram that this subprogram contains.
9003 If SET_ADDRMAP is true, record the covered ranges in the addrmap.
9004 Set *LOWPC and *HIGHPC to the lowest and highest PC values found in PDI.
6e70227d 9005
cdc07690
YQ
9006 PDI may also be a lexical block, in which case we simply search
9007 recursively for subprograms defined inside that lexical block.
bc30ff58
JB
9008 Again, this is only performed when the CU language allows this
9009 type of definitions. */
9010
9011static void
9012add_partial_subprogram (struct partial_die_info *pdi,
9013 CORE_ADDR *lowpc, CORE_ADDR *highpc,
cdc07690 9014 int set_addrmap, struct dwarf2_cu *cu)
bc30ff58 9015{
b1dc1806 9016 if (pdi->tag == DW_TAG_subprogram || pdi->tag == DW_TAG_inlined_subroutine)
bc30ff58
JB
9017 {
9018 if (pdi->has_pc_info)
9019 {
9020 if (pdi->lowpc < *lowpc)
9021 *lowpc = pdi->lowpc;
9022 if (pdi->highpc > *highpc)
9023 *highpc = pdi->highpc;
cdc07690 9024 if (set_addrmap)
5734ee8b 9025 {
518817b3 9026 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
3e29f34a
MR
9027 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9028 CORE_ADDR baseaddr;
9029 CORE_ADDR highpc;
9030 CORE_ADDR lowpc;
5734ee8b
DJ
9031
9032 baseaddr = ANOFFSET (objfile->section_offsets,
9033 SECT_OFF_TEXT (objfile));
3e29f34a
MR
9034 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch,
9035 pdi->lowpc + baseaddr);
9036 highpc = gdbarch_adjust_dwarf2_addr (gdbarch,
9037 pdi->highpc + baseaddr);
9038 addrmap_set_empty (objfile->psymtabs_addrmap, lowpc, highpc - 1,
9291a0cd 9039 cu->per_cu->v.psymtab);
5734ee8b 9040 }
481860b3
GB
9041 }
9042
9043 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
9044 {
bc30ff58 9045 if (!pdi->is_declaration)
e8d05480
JB
9046 /* Ignore subprogram DIEs that do not have a name, they are
9047 illegal. Do not emit a complaint at this point, we will
9048 do so when we convert this psymtab into a symtab. */
9049 if (pdi->name)
9050 add_partial_symbol (pdi, cu);
bc30ff58
JB
9051 }
9052 }
6e70227d 9053
bc30ff58
JB
9054 if (! pdi->has_children)
9055 return;
9056
9057 if (cu->language == language_ada)
9058 {
9059 pdi = pdi->die_child;
9060 while (pdi != NULL)
9061 {
52356b79 9062 pdi->fixup (cu);
bc30ff58 9063 if (pdi->tag == DW_TAG_subprogram
b1dc1806 9064 || pdi->tag == DW_TAG_inlined_subroutine
bc30ff58 9065 || pdi->tag == DW_TAG_lexical_block)
cdc07690 9066 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
bc30ff58
JB
9067 pdi = pdi->die_sibling;
9068 }
9069 }
9070}
9071
91c24f0a
DC
9072/* Read a partial die corresponding to an enumeration type. */
9073
72bf9492
DJ
9074static void
9075add_partial_enumeration (struct partial_die_info *enum_pdi,
9076 struct dwarf2_cu *cu)
91c24f0a 9077{
72bf9492 9078 struct partial_die_info *pdi;
91c24f0a
DC
9079
9080 if (enum_pdi->name != NULL)
72bf9492
DJ
9081 add_partial_symbol (enum_pdi, cu);
9082
9083 pdi = enum_pdi->die_child;
9084 while (pdi)
91c24f0a 9085 {
72bf9492 9086 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
b98664d3 9087 complaint (_("malformed enumerator DIE ignored"));
91c24f0a 9088 else
72bf9492
DJ
9089 add_partial_symbol (pdi, cu);
9090 pdi = pdi->die_sibling;
91c24f0a 9091 }
91c24f0a
DC
9092}
9093
6caca83c
CC
9094/* Return the initial uleb128 in the die at INFO_PTR. */
9095
9096static unsigned int
d521ce57 9097peek_abbrev_code (bfd *abfd, const gdb_byte *info_ptr)
6caca83c
CC
9098{
9099 unsigned int bytes_read;
9100
9101 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9102}
9103
685af9cd
TT
9104/* Read the initial uleb128 in the die at INFO_PTR in compilation unit
9105 READER::CU. Use READER::ABBREV_TABLE to lookup any abbreviation.
9106
4bb7a0a7
DJ
9107 Return the corresponding abbrev, or NULL if the number is zero (indicating
9108 an empty DIE). In either case *BYTES_READ will be set to the length of
9109 the initial number. */
9110
9111static struct abbrev_info *
685af9cd
TT
9112peek_die_abbrev (const die_reader_specs &reader,
9113 const gdb_byte *info_ptr, unsigned int *bytes_read)
4bb7a0a7 9114{
685af9cd 9115 dwarf2_cu *cu = reader.cu;
518817b3 9116 bfd *abfd = cu->per_cu->dwarf2_per_objfile->objfile->obfd;
685af9cd
TT
9117 unsigned int abbrev_number
9118 = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
4bb7a0a7
DJ
9119
9120 if (abbrev_number == 0)
9121 return NULL;
9122
685af9cd 9123 abbrev_info *abbrev = reader.abbrev_table->lookup_abbrev (abbrev_number);
4bb7a0a7
DJ
9124 if (!abbrev)
9125 {
422b9917 9126 error (_("Dwarf Error: Could not find abbrev number %d in %s"
9d8780f0 9127 " at offset %s [in module %s]"),
422b9917 9128 abbrev_number, cu->per_cu->is_debug_types ? "TU" : "CU",
9d8780f0 9129 sect_offset_str (cu->header.sect_off), bfd_get_filename (abfd));
4bb7a0a7
DJ
9130 }
9131
9132 return abbrev;
9133}
9134
93311388
DE
9135/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
9136 Returns a pointer to the end of a series of DIEs, terminated by an empty
4bb7a0a7
DJ
9137 DIE. Any children of the skipped DIEs will also be skipped. */
9138
d521ce57
TT
9139static const gdb_byte *
9140skip_children (const struct die_reader_specs *reader, const gdb_byte *info_ptr)
4bb7a0a7 9141{
4bb7a0a7
DJ
9142 while (1)
9143 {
685af9cd
TT
9144 unsigned int bytes_read;
9145 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
9146
4bb7a0a7
DJ
9147 if (abbrev == NULL)
9148 return info_ptr + bytes_read;
9149 else
dee91e82 9150 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
4bb7a0a7
DJ
9151 }
9152}
9153
93311388
DE
9154/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
9155 INFO_PTR should point just after the initial uleb128 of a DIE, and the
4bb7a0a7
DJ
9156 abbrev corresponding to that skipped uleb128 should be passed in
9157 ABBREV. Returns a pointer to this DIE's sibling, skipping any
9158 children. */
9159
d521ce57
TT
9160static const gdb_byte *
9161skip_one_die (const struct die_reader_specs *reader, const gdb_byte *info_ptr,
dee91e82 9162 struct abbrev_info *abbrev)
4bb7a0a7
DJ
9163{
9164 unsigned int bytes_read;
9165 struct attribute attr;
dee91e82
DE
9166 bfd *abfd = reader->abfd;
9167 struct dwarf2_cu *cu = reader->cu;
d521ce57 9168 const gdb_byte *buffer = reader->buffer;
f664829e 9169 const gdb_byte *buffer_end = reader->buffer_end;
4bb7a0a7
DJ
9170 unsigned int form, i;
9171
9172 for (i = 0; i < abbrev->num_attrs; i++)
9173 {
9174 /* The only abbrev we care about is DW_AT_sibling. */
9175 if (abbrev->attrs[i].name == DW_AT_sibling)
9176 {
dee91e82 9177 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
4bb7a0a7 9178 if (attr.form == DW_FORM_ref_addr)
b98664d3 9179 complaint (_("ignoring absolute DW_AT_sibling"));
4bb7a0a7 9180 else
b9502d3f 9181 {
9c541725
PA
9182 sect_offset off = dwarf2_get_ref_die_offset (&attr);
9183 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
b9502d3f
WN
9184
9185 if (sibling_ptr < info_ptr)
b98664d3 9186 complaint (_("DW_AT_sibling points backwards"));
22869d73
KS
9187 else if (sibling_ptr > reader->buffer_end)
9188 dwarf2_section_buffer_overflow_complaint (reader->die_section);
b9502d3f
WN
9189 else
9190 return sibling_ptr;
9191 }
4bb7a0a7
DJ
9192 }
9193
9194 /* If it isn't DW_AT_sibling, skip this attribute. */
9195 form = abbrev->attrs[i].form;
9196 skip_attribute:
9197 switch (form)
9198 {
4bb7a0a7 9199 case DW_FORM_ref_addr:
ae411497
TT
9200 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
9201 and later it is offset sized. */
9202 if (cu->header.version == 2)
9203 info_ptr += cu->header.addr_size;
9204 else
9205 info_ptr += cu->header.offset_size;
9206 break;
36586728
TT
9207 case DW_FORM_GNU_ref_alt:
9208 info_ptr += cu->header.offset_size;
9209 break;
ae411497 9210 case DW_FORM_addr:
4bb7a0a7
DJ
9211 info_ptr += cu->header.addr_size;
9212 break;
9213 case DW_FORM_data1:
9214 case DW_FORM_ref1:
9215 case DW_FORM_flag:
9216 info_ptr += 1;
9217 break;
2dc7f7b3 9218 case DW_FORM_flag_present:
43988095 9219 case DW_FORM_implicit_const:
2dc7f7b3 9220 break;
4bb7a0a7
DJ
9221 case DW_FORM_data2:
9222 case DW_FORM_ref2:
9223 info_ptr += 2;
9224 break;
9225 case DW_FORM_data4:
9226 case DW_FORM_ref4:
9227 info_ptr += 4;
9228 break;
9229 case DW_FORM_data8:
9230 case DW_FORM_ref8:
55f1336d 9231 case DW_FORM_ref_sig8:
4bb7a0a7
DJ
9232 info_ptr += 8;
9233 break;
0224619f
JK
9234 case DW_FORM_data16:
9235 info_ptr += 16;
9236 break;
4bb7a0a7 9237 case DW_FORM_string:
9b1c24c8 9238 read_direct_string (abfd, info_ptr, &bytes_read);
4bb7a0a7
DJ
9239 info_ptr += bytes_read;
9240 break;
2dc7f7b3 9241 case DW_FORM_sec_offset:
4bb7a0a7 9242 case DW_FORM_strp:
36586728 9243 case DW_FORM_GNU_strp_alt:
4bb7a0a7
DJ
9244 info_ptr += cu->header.offset_size;
9245 break;
2dc7f7b3 9246 case DW_FORM_exprloc:
4bb7a0a7
DJ
9247 case DW_FORM_block:
9248 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9249 info_ptr += bytes_read;
9250 break;
9251 case DW_FORM_block1:
9252 info_ptr += 1 + read_1_byte (abfd, info_ptr);
9253 break;
9254 case DW_FORM_block2:
9255 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
9256 break;
9257 case DW_FORM_block4:
9258 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
9259 break;
9260 case DW_FORM_sdata:
9261 case DW_FORM_udata:
9262 case DW_FORM_ref_udata:
3019eac3
DE
9263 case DW_FORM_GNU_addr_index:
9264 case DW_FORM_GNU_str_index:
d521ce57 9265 info_ptr = safe_skip_leb128 (info_ptr, buffer_end);
4bb7a0a7
DJ
9266 break;
9267 case DW_FORM_indirect:
9268 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9269 info_ptr += bytes_read;
9270 /* We need to continue parsing from here, so just go back to
9271 the top. */
9272 goto skip_attribute;
9273
9274 default:
3e43a32a
MS
9275 error (_("Dwarf Error: Cannot handle %s "
9276 "in DWARF reader [in module %s]"),
4bb7a0a7
DJ
9277 dwarf_form_name (form),
9278 bfd_get_filename (abfd));
9279 }
9280 }
9281
9282 if (abbrev->has_children)
dee91e82 9283 return skip_children (reader, info_ptr);
4bb7a0a7
DJ
9284 else
9285 return info_ptr;
9286}
9287
93311388 9288/* Locate ORIG_PDI's sibling.
dee91e82 9289 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
91c24f0a 9290
d521ce57 9291static const gdb_byte *
dee91e82
DE
9292locate_pdi_sibling (const struct die_reader_specs *reader,
9293 struct partial_die_info *orig_pdi,
d521ce57 9294 const gdb_byte *info_ptr)
91c24f0a
DC
9295{
9296 /* Do we know the sibling already? */
72bf9492 9297
91c24f0a
DC
9298 if (orig_pdi->sibling)
9299 return orig_pdi->sibling;
9300
9301 /* Are there any children to deal with? */
9302
9303 if (!orig_pdi->has_children)
9304 return info_ptr;
9305
4bb7a0a7 9306 /* Skip the children the long way. */
91c24f0a 9307
dee91e82 9308 return skip_children (reader, info_ptr);
91c24f0a
DC
9309}
9310
257e7a09 9311/* Expand this partial symbol table into a full symbol table. SELF is
442e4d9c 9312 not NULL. */
c906108c
SS
9313
9314static void
257e7a09
YQ
9315dwarf2_read_symtab (struct partial_symtab *self,
9316 struct objfile *objfile)
c906108c 9317{
ed2dc618
SM
9318 struct dwarf2_per_objfile *dwarf2_per_objfile
9319 = get_dwarf2_per_objfile (objfile);
9320
257e7a09 9321 if (self->readin)
c906108c 9322 {
442e4d9c 9323 warning (_("bug: psymtab for %s is already read in."),
257e7a09 9324 self->filename);
442e4d9c
YQ
9325 }
9326 else
9327 {
9328 if (info_verbose)
c906108c 9329 {
442e4d9c 9330 printf_filtered (_("Reading in symbols for %s..."),
257e7a09 9331 self->filename);
442e4d9c 9332 gdb_flush (gdb_stdout);
c906108c 9333 }
c906108c 9334
442e4d9c
YQ
9335 /* If this psymtab is constructed from a debug-only objfile, the
9336 has_section_at_zero flag will not necessarily be correct. We
9337 can get the correct value for this flag by looking at the data
9338 associated with the (presumably stripped) associated objfile. */
9339 if (objfile->separate_debug_objfile_backlink)
9340 {
9341 struct dwarf2_per_objfile *dpo_backlink
ed2dc618 9342 = get_dwarf2_per_objfile (objfile->separate_debug_objfile_backlink);
9a619af0 9343
442e4d9c
YQ
9344 dwarf2_per_objfile->has_section_at_zero
9345 = dpo_backlink->has_section_at_zero;
9346 }
b2ab525c 9347
442e4d9c 9348 dwarf2_per_objfile->reading_partial_symbols = 0;
98bfdba5 9349
257e7a09 9350 psymtab_to_symtab_1 (self);
c906108c 9351
442e4d9c
YQ
9352 /* Finish up the debug error message. */
9353 if (info_verbose)
9354 printf_filtered (_("done.\n"));
c906108c 9355 }
95554aad 9356
ed2dc618 9357 process_cu_includes (dwarf2_per_objfile);
c906108c 9358}
9cdd5dbd
DE
9359\f
9360/* Reading in full CUs. */
c906108c 9361
10b3939b
DJ
9362/* Add PER_CU to the queue. */
9363
9364static void
95554aad
TT
9365queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
9366 enum language pretend_language)
10b3939b
DJ
9367{
9368 struct dwarf2_queue_item *item;
9369
9370 per_cu->queued = 1;
8d749320 9371 item = XNEW (struct dwarf2_queue_item);
10b3939b 9372 item->per_cu = per_cu;
95554aad 9373 item->pretend_language = pretend_language;
10b3939b
DJ
9374 item->next = NULL;
9375
9376 if (dwarf2_queue == NULL)
9377 dwarf2_queue = item;
9378 else
9379 dwarf2_queue_tail->next = item;
9380
9381 dwarf2_queue_tail = item;
9382}
9383
89e63ee4
DE
9384/* If PER_CU is not yet queued, add it to the queue.
9385 If DEPENDENT_CU is non-NULL, it has a reference to PER_CU so add a
9386 dependency.
0907af0c 9387 The result is non-zero if PER_CU was queued, otherwise the result is zero
69d751e3
DE
9388 meaning either PER_CU is already queued or it is already loaded.
9389
9390 N.B. There is an invariant here that if a CU is queued then it is loaded.
9391 The caller is required to load PER_CU if we return non-zero. */
0907af0c
DE
9392
9393static int
89e63ee4 9394maybe_queue_comp_unit (struct dwarf2_cu *dependent_cu,
0907af0c
DE
9395 struct dwarf2_per_cu_data *per_cu,
9396 enum language pretend_language)
9397{
9398 /* We may arrive here during partial symbol reading, if we need full
9399 DIEs to process an unusual case (e.g. template arguments). Do
9400 not queue PER_CU, just tell our caller to load its DIEs. */
ed2dc618 9401 if (per_cu->dwarf2_per_objfile->reading_partial_symbols)
0907af0c
DE
9402 {
9403 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
9404 return 1;
9405 return 0;
9406 }
9407
9408 /* Mark the dependence relation so that we don't flush PER_CU
9409 too early. */
89e63ee4
DE
9410 if (dependent_cu != NULL)
9411 dwarf2_add_dependence (dependent_cu, per_cu);
0907af0c
DE
9412
9413 /* If it's already on the queue, we have nothing to do. */
9414 if (per_cu->queued)
9415 return 0;
9416
9417 /* If the compilation unit is already loaded, just mark it as
9418 used. */
9419 if (per_cu->cu != NULL)
9420 {
9421 per_cu->cu->last_used = 0;
9422 return 0;
9423 }
9424
9425 /* Add it to the queue. */
9426 queue_comp_unit (per_cu, pretend_language);
9427
9428 return 1;
9429}
9430
10b3939b
DJ
9431/* Process the queue. */
9432
9433static void
ed2dc618 9434process_queue (struct dwarf2_per_objfile *dwarf2_per_objfile)
10b3939b
DJ
9435{
9436 struct dwarf2_queue_item *item, *next_item;
9437
b4f54984 9438 if (dwarf_read_debug)
45cfd468
DE
9439 {
9440 fprintf_unfiltered (gdb_stdlog,
9441 "Expanding one or more symtabs of objfile %s ...\n",
4262abfb 9442 objfile_name (dwarf2_per_objfile->objfile));
45cfd468
DE
9443 }
9444
03dd20cc
DJ
9445 /* The queue starts out with one item, but following a DIE reference
9446 may load a new CU, adding it to the end of the queue. */
10b3939b
DJ
9447 for (item = dwarf2_queue; item != NULL; dwarf2_queue = item = next_item)
9448 {
cc12ce38
DE
9449 if ((dwarf2_per_objfile->using_index
9450 ? !item->per_cu->v.quick->compunit_symtab
9451 : (item->per_cu->v.psymtab && !item->per_cu->v.psymtab->readin))
9452 /* Skip dummy CUs. */
9453 && item->per_cu->cu != NULL)
f4dc4d17
DE
9454 {
9455 struct dwarf2_per_cu_data *per_cu = item->per_cu;
73be47f5 9456 unsigned int debug_print_threshold;
247f5c4f 9457 char buf[100];
f4dc4d17 9458
247f5c4f 9459 if (per_cu->is_debug_types)
f4dc4d17 9460 {
247f5c4f
DE
9461 struct signatured_type *sig_type =
9462 (struct signatured_type *) per_cu;
9463
9d8780f0 9464 sprintf (buf, "TU %s at offset %s",
73be47f5 9465 hex_string (sig_type->signature),
9d8780f0 9466 sect_offset_str (per_cu->sect_off));
73be47f5
DE
9467 /* There can be 100s of TUs.
9468 Only print them in verbose mode. */
9469 debug_print_threshold = 2;
f4dc4d17 9470 }
247f5c4f 9471 else
73be47f5 9472 {
9d8780f0
SM
9473 sprintf (buf, "CU at offset %s",
9474 sect_offset_str (per_cu->sect_off));
73be47f5
DE
9475 debug_print_threshold = 1;
9476 }
247f5c4f 9477
b4f54984 9478 if (dwarf_read_debug >= debug_print_threshold)
247f5c4f 9479 fprintf_unfiltered (gdb_stdlog, "Expanding symtab of %s\n", buf);
f4dc4d17
DE
9480
9481 if (per_cu->is_debug_types)
9482 process_full_type_unit (per_cu, item->pretend_language);
9483 else
9484 process_full_comp_unit (per_cu, item->pretend_language);
9485
b4f54984 9486 if (dwarf_read_debug >= debug_print_threshold)
247f5c4f 9487 fprintf_unfiltered (gdb_stdlog, "Done expanding %s\n", buf);
f4dc4d17 9488 }
10b3939b
DJ
9489
9490 item->per_cu->queued = 0;
9491 next_item = item->next;
9492 xfree (item);
9493 }
9494
9495 dwarf2_queue_tail = NULL;
45cfd468 9496
b4f54984 9497 if (dwarf_read_debug)
45cfd468
DE
9498 {
9499 fprintf_unfiltered (gdb_stdlog, "Done expanding symtabs of %s.\n",
4262abfb 9500 objfile_name (dwarf2_per_objfile->objfile));
45cfd468 9501 }
10b3939b
DJ
9502}
9503
10b3939b
DJ
9504/* Read in full symbols for PST, and anything it depends on. */
9505
c906108c 9506static void
fba45db2 9507psymtab_to_symtab_1 (struct partial_symtab *pst)
c906108c 9508{
10b3939b 9509 struct dwarf2_per_cu_data *per_cu;
aaa75496
JB
9510 int i;
9511
95554aad
TT
9512 if (pst->readin)
9513 return;
9514
aaa75496 9515 for (i = 0; i < pst->number_of_dependencies; i++)
95554aad
TT
9516 if (!pst->dependencies[i]->readin
9517 && pst->dependencies[i]->user == NULL)
aaa75496
JB
9518 {
9519 /* Inform about additional files that need to be read in. */
9520 if (info_verbose)
9521 {
a3f17187 9522 /* FIXME: i18n: Need to make this a single string. */
aaa75496
JB
9523 fputs_filtered (" ", gdb_stdout);
9524 wrap_here ("");
9525 fputs_filtered ("and ", gdb_stdout);
9526 wrap_here ("");
9527 printf_filtered ("%s...", pst->dependencies[i]->filename);
0963b4bd 9528 wrap_here (""); /* Flush output. */
aaa75496
JB
9529 gdb_flush (gdb_stdout);
9530 }
9531 psymtab_to_symtab_1 (pst->dependencies[i]);
9532 }
9533
9a3c8263 9534 per_cu = (struct dwarf2_per_cu_data *) pst->read_symtab_private;
10b3939b
DJ
9535
9536 if (per_cu == NULL)
aaa75496
JB
9537 {
9538 /* It's an include file, no symbols to read for it.
9539 Everything is in the parent symtab. */
9540 pst->readin = 1;
9541 return;
9542 }
c906108c 9543
58f0c718 9544 dw2_do_instantiate_symtab (per_cu, false);
10b3939b
DJ
9545}
9546
dee91e82
DE
9547/* Trivial hash function for die_info: the hash value of a DIE
9548 is its offset in .debug_info for this objfile. */
10b3939b 9549
dee91e82
DE
9550static hashval_t
9551die_hash (const void *item)
10b3939b 9552{
9a3c8263 9553 const struct die_info *die = (const struct die_info *) item;
6502dd73 9554
9c541725 9555 return to_underlying (die->sect_off);
dee91e82 9556}
63d06c5c 9557
dee91e82
DE
9558/* Trivial comparison function for die_info structures: two DIEs
9559 are equal if they have the same offset. */
98bfdba5 9560
dee91e82
DE
9561static int
9562die_eq (const void *item_lhs, const void *item_rhs)
9563{
9a3c8263
SM
9564 const struct die_info *die_lhs = (const struct die_info *) item_lhs;
9565 const struct die_info *die_rhs = (const struct die_info *) item_rhs;
c906108c 9566
9c541725 9567 return die_lhs->sect_off == die_rhs->sect_off;
dee91e82 9568}
c906108c 9569
dee91e82
DE
9570/* die_reader_func for load_full_comp_unit.
9571 This is identical to read_signatured_type_reader,
9572 but is kept separate for now. */
c906108c 9573
dee91e82
DE
9574static void
9575load_full_comp_unit_reader (const struct die_reader_specs *reader,
d521ce57 9576 const gdb_byte *info_ptr,
dee91e82
DE
9577 struct die_info *comp_unit_die,
9578 int has_children,
9579 void *data)
9580{
9581 struct dwarf2_cu *cu = reader->cu;
9a3c8263 9582 enum language *language_ptr = (enum language *) data;
6caca83c 9583
dee91e82
DE
9584 gdb_assert (cu->die_hash == NULL);
9585 cu->die_hash =
9586 htab_create_alloc_ex (cu->header.length / 12,
9587 die_hash,
9588 die_eq,
9589 NULL,
9590 &cu->comp_unit_obstack,
9591 hashtab_obstack_allocate,
9592 dummy_obstack_deallocate);
e142c38c 9593
dee91e82
DE
9594 if (has_children)
9595 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
9596 &info_ptr, comp_unit_die);
9597 cu->dies = comp_unit_die;
9598 /* comp_unit_die is not stored in die_hash, no need. */
10b3939b
DJ
9599
9600 /* We try not to read any attributes in this function, because not
9cdd5dbd 9601 all CUs needed for references have been loaded yet, and symbol
10b3939b 9602 table processing isn't initialized. But we have to set the CU language,
dee91e82
DE
9603 or we won't be able to build types correctly.
9604 Similarly, if we do not read the producer, we can not apply
9605 producer-specific interpretation. */
95554aad 9606 prepare_one_comp_unit (cu, cu->dies, *language_ptr);
dee91e82 9607}
10b3939b 9608
dee91e82 9609/* Load the DIEs associated with PER_CU into memory. */
a6c727b2 9610
dee91e82 9611static void
95554aad 9612load_full_comp_unit (struct dwarf2_per_cu_data *this_cu,
58f0c718 9613 bool skip_partial,
95554aad 9614 enum language pretend_language)
dee91e82 9615{
3019eac3 9616 gdb_assert (! this_cu->is_debug_types);
c5b7e1cb 9617
58f0c718 9618 init_cutu_and_read_dies (this_cu, NULL, 1, 1, skip_partial,
f4dc4d17 9619 load_full_comp_unit_reader, &pretend_language);
10b3939b
DJ
9620}
9621
3da10d80
KS
9622/* Add a DIE to the delayed physname list. */
9623
9624static void
9625add_to_method_list (struct type *type, int fnfield_index, int index,
9626 const char *name, struct die_info *die,
9627 struct dwarf2_cu *cu)
9628{
9629 struct delayed_method_info mi;
9630 mi.type = type;
9631 mi.fnfield_index = fnfield_index;
9632 mi.index = index;
9633 mi.name = name;
9634 mi.die = die;
c89b44cd 9635 cu->method_list.push_back (mi);
3da10d80
KS
9636}
9637
3693fdb3
PA
9638/* Check whether [PHYSNAME, PHYSNAME+LEN) ends with a modifier like
9639 "const" / "volatile". If so, decrements LEN by the length of the
9640 modifier and return true. Otherwise return false. */
9641
9642template<size_t N>
9643static bool
9644check_modifier (const char *physname, size_t &len, const char (&mod)[N])
9645{
9646 size_t mod_len = sizeof (mod) - 1;
9647 if (len > mod_len && startswith (physname + (len - mod_len), mod))
9648 {
9649 len -= mod_len;
9650 return true;
9651 }
9652 return false;
9653}
9654
3da10d80
KS
9655/* Compute the physnames of any methods on the CU's method list.
9656
9657 The computation of method physnames is delayed in order to avoid the
9658 (bad) condition that one of the method's formal parameters is of an as yet
9659 incomplete type. */
9660
9661static void
9662compute_delayed_physnames (struct dwarf2_cu *cu)
9663{
3693fdb3 9664 /* Only C++ delays computing physnames. */
c89b44cd 9665 if (cu->method_list.empty ())
3693fdb3
PA
9666 return;
9667 gdb_assert (cu->language == language_cplus);
9668
52941706 9669 for (const delayed_method_info &mi : cu->method_list)
3da10d80 9670 {
1d06ead6 9671 const char *physname;
3da10d80 9672 struct fn_fieldlist *fn_flp
c89b44cd
TT
9673 = &TYPE_FN_FIELDLIST (mi.type, mi.fnfield_index);
9674 physname = dwarf2_physname (mi.name, mi.die, cu);
9675 TYPE_FN_FIELD_PHYSNAME (fn_flp->fn_fields, mi.index)
005e54bb 9676 = physname ? physname : "";
3693fdb3
PA
9677
9678 /* Since there's no tag to indicate whether a method is a
9679 const/volatile overload, extract that information out of the
9680 demangled name. */
9681 if (physname != NULL)
9682 {
9683 size_t len = strlen (physname);
9684
9685 while (1)
9686 {
9687 if (physname[len] == ')') /* shortcut */
9688 break;
9689 else if (check_modifier (physname, len, " const"))
c89b44cd 9690 TYPE_FN_FIELD_CONST (fn_flp->fn_fields, mi.index) = 1;
3693fdb3 9691 else if (check_modifier (physname, len, " volatile"))
c89b44cd 9692 TYPE_FN_FIELD_VOLATILE (fn_flp->fn_fields, mi.index) = 1;
3693fdb3
PA
9693 else
9694 break;
9695 }
9696 }
3da10d80 9697 }
c89b44cd
TT
9698
9699 /* The list is no longer needed. */
9700 cu->method_list.clear ();
3da10d80
KS
9701}
9702
a766d390
DE
9703/* Go objects should be embedded in a DW_TAG_module DIE,
9704 and it's not clear if/how imported objects will appear.
9705 To keep Go support simple until that's worked out,
9706 go back through what we've read and create something usable.
9707 We could do this while processing each DIE, and feels kinda cleaner,
9708 but that way is more invasive.
9709 This is to, for example, allow the user to type "p var" or "b main"
9710 without having to specify the package name, and allow lookups
9711 of module.object to work in contexts that use the expression
9712 parser. */
9713
9714static void
9715fixup_go_packaging (struct dwarf2_cu *cu)
9716{
9717 char *package_name = NULL;
9718 struct pending *list;
9719 int i;
9720
9721 for (list = global_symbols; list != NULL; list = list->next)
9722 {
9723 for (i = 0; i < list->nsyms; ++i)
9724 {
9725 struct symbol *sym = list->symbol[i];
9726
9727 if (SYMBOL_LANGUAGE (sym) == language_go
9728 && SYMBOL_CLASS (sym) == LOC_BLOCK)
9729 {
9730 char *this_package_name = go_symbol_package_name (sym);
9731
9732 if (this_package_name == NULL)
9733 continue;
9734 if (package_name == NULL)
9735 package_name = this_package_name;
9736 else
9737 {
518817b3
SM
9738 struct objfile *objfile
9739 = cu->per_cu->dwarf2_per_objfile->objfile;
a766d390 9740 if (strcmp (package_name, this_package_name) != 0)
b98664d3 9741 complaint (_("Symtab %s has objects from two different Go packages: %s and %s"),
08be3fe3
DE
9742 (symbol_symtab (sym) != NULL
9743 ? symtab_to_filename_for_display
9744 (symbol_symtab (sym))
e3b94546 9745 : objfile_name (objfile)),
a766d390
DE
9746 this_package_name, package_name);
9747 xfree (this_package_name);
9748 }
9749 }
9750 }
9751 }
9752
9753 if (package_name != NULL)
9754 {
518817b3 9755 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
34a68019 9756 const char *saved_package_name
224c3ddb
SM
9757 = (const char *) obstack_copy0 (&objfile->per_bfd->storage_obstack,
9758 package_name,
9759 strlen (package_name));
19f392bc
UW
9760 struct type *type = init_type (objfile, TYPE_CODE_MODULE, 0,
9761 saved_package_name);
a766d390
DE
9762 struct symbol *sym;
9763
e623cf5d 9764 sym = allocate_symbol (objfile);
f85f34ed 9765 SYMBOL_SET_LANGUAGE (sym, language_go, &objfile->objfile_obstack);
86f62fd7
TT
9766 SYMBOL_SET_NAMES (sym, saved_package_name,
9767 strlen (saved_package_name), 0, objfile);
a766d390
DE
9768 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
9769 e.g., "main" finds the "main" module and not C's main(). */
9770 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
f1e6e072 9771 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
a766d390
DE
9772 SYMBOL_TYPE (sym) = type;
9773
9774 add_symbol_to_list (sym, &global_symbols);
9775
9776 xfree (package_name);
9777 }
9778}
9779
c9317f21
TT
9780/* Allocate a fully-qualified name consisting of the two parts on the
9781 obstack. */
9782
9783static const char *
9784rust_fully_qualify (struct obstack *obstack, const char *p1, const char *p2)
9785{
9786 return obconcat (obstack, p1, "::", p2, (char *) NULL);
9787}
9788
9789/* A helper that allocates a struct discriminant_info to attach to a
9790 union type. */
9791
9792static struct discriminant_info *
9793alloc_discriminant_info (struct type *type, int discriminant_index,
9794 int default_index)
9795{
9796 gdb_assert (TYPE_CODE (type) == TYPE_CODE_UNION);
c7b15a66
TT
9797 gdb_assert (discriminant_index == -1
9798 || (discriminant_index >= 0
9799 && discriminant_index < TYPE_NFIELDS (type)));
c9317f21 9800 gdb_assert (default_index == -1
c7b15a66 9801 || (default_index >= 0 && default_index < TYPE_NFIELDS (type)));
c9317f21
TT
9802
9803 TYPE_FLAG_DISCRIMINATED_UNION (type) = 1;
9804
9805 struct discriminant_info *disc
9806 = ((struct discriminant_info *)
9807 TYPE_ZALLOC (type,
9808 offsetof (struct discriminant_info, discriminants)
9809 + TYPE_NFIELDS (type) * sizeof (disc->discriminants[0])));
9810 disc->default_index = default_index;
9811 disc->discriminant_index = discriminant_index;
9812
9813 struct dynamic_prop prop;
9814 prop.kind = PROP_UNDEFINED;
9815 prop.data.baton = disc;
9816
9817 add_dyn_prop (DYN_PROP_DISCRIMINATED, prop, type);
9818
9819 return disc;
9820}
9821
9822/* Some versions of rustc emitted enums in an unusual way.
9823
9824 Ordinary enums were emitted as unions. The first element of each
9825 structure in the union was named "RUST$ENUM$DISR". This element
9826 held the discriminant.
9827
9828 These versions of Rust also implemented the "non-zero"
9829 optimization. When the enum had two values, and one is empty and
9830 the other holds a pointer that cannot be zero, the pointer is used
9831 as the discriminant, with a zero value meaning the empty variant.
9832 Here, the union's first member is of the form
9833 RUST$ENCODED$ENUM$<fieldno>$<fieldno>$...$<variantname>
9834 where the fieldnos are the indices of the fields that should be
9835 traversed in order to find the field (which may be several fields deep)
9836 and the variantname is the name of the variant of the case when the
9837 field is zero.
9838
9839 This function recognizes whether TYPE is of one of these forms,
9840 and, if so, smashes it to be a variant type. */
9841
9842static void
9843quirk_rust_enum (struct type *type, struct objfile *objfile)
9844{
9845 gdb_assert (TYPE_CODE (type) == TYPE_CODE_UNION);
9846
9847 /* We don't need to deal with empty enums. */
9848 if (TYPE_NFIELDS (type) == 0)
9849 return;
9850
9851#define RUST_ENUM_PREFIX "RUST$ENCODED$ENUM$"
9852 if (TYPE_NFIELDS (type) == 1
9853 && startswith (TYPE_FIELD_NAME (type, 0), RUST_ENUM_PREFIX))
9854 {
9855 const char *name = TYPE_FIELD_NAME (type, 0) + strlen (RUST_ENUM_PREFIX);
9856
9857 /* Decode the field name to find the offset of the
9858 discriminant. */
9859 ULONGEST bit_offset = 0;
9860 struct type *field_type = TYPE_FIELD_TYPE (type, 0);
9861 while (name[0] >= '0' && name[0] <= '9')
9862 {
9863 char *tail;
9864 unsigned long index = strtoul (name, &tail, 10);
9865 name = tail;
9866 if (*name != '$'
9867 || index >= TYPE_NFIELDS (field_type)
9868 || (TYPE_FIELD_LOC_KIND (field_type, index)
9869 != FIELD_LOC_KIND_BITPOS))
9870 {
b98664d3 9871 complaint (_("Could not parse Rust enum encoding string \"%s\""
c9317f21
TT
9872 "[in module %s]"),
9873 TYPE_FIELD_NAME (type, 0),
9874 objfile_name (objfile));
9875 return;
9876 }
9877 ++name;
9878
9879 bit_offset += TYPE_FIELD_BITPOS (field_type, index);
9880 field_type = TYPE_FIELD_TYPE (field_type, index);
9881 }
9882
9883 /* Make a union to hold the variants. */
9884 struct type *union_type = alloc_type (objfile);
9885 TYPE_CODE (union_type) = TYPE_CODE_UNION;
9886 TYPE_NFIELDS (union_type) = 3;
9887 TYPE_FIELDS (union_type)
9888 = (struct field *) TYPE_ZALLOC (type, 3 * sizeof (struct field));
9889 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
2b4424c3 9890 set_type_align (union_type, TYPE_RAW_ALIGN (type));
c9317f21
TT
9891
9892 /* Put the discriminant must at index 0. */
9893 TYPE_FIELD_TYPE (union_type, 0) = field_type;
9894 TYPE_FIELD_ARTIFICIAL (union_type, 0) = 1;
9895 TYPE_FIELD_NAME (union_type, 0) = "<<discriminant>>";
9896 SET_FIELD_BITPOS (TYPE_FIELD (union_type, 0), bit_offset);
9897
9898 /* The order of fields doesn't really matter, so put the real
9899 field at index 1 and the data-less field at index 2. */
9900 struct discriminant_info *disc
9901 = alloc_discriminant_info (union_type, 0, 1);
9902 TYPE_FIELD (union_type, 1) = TYPE_FIELD (type, 0);
9903 TYPE_FIELD_NAME (union_type, 1)
9904 = rust_last_path_segment (TYPE_NAME (TYPE_FIELD_TYPE (union_type, 1)));
9905 TYPE_NAME (TYPE_FIELD_TYPE (union_type, 1))
9906 = rust_fully_qualify (&objfile->objfile_obstack, TYPE_NAME (type),
9907 TYPE_FIELD_NAME (union_type, 1));
9908
9909 const char *dataless_name
9910 = rust_fully_qualify (&objfile->objfile_obstack, TYPE_NAME (type),
9911 name);
9912 struct type *dataless_type = init_type (objfile, TYPE_CODE_VOID, 0,
9913 dataless_name);
9914 TYPE_FIELD_TYPE (union_type, 2) = dataless_type;
9915 /* NAME points into the original discriminant name, which
9916 already has the correct lifetime. */
9917 TYPE_FIELD_NAME (union_type, 2) = name;
9918 SET_FIELD_BITPOS (TYPE_FIELD (union_type, 2), 0);
9919 disc->discriminants[2] = 0;
9920
9921 /* Smash this type to be a structure type. We have to do this
9922 because the type has already been recorded. */
9923 TYPE_CODE (type) = TYPE_CODE_STRUCT;
9924 TYPE_NFIELDS (type) = 1;
9925 TYPE_FIELDS (type)
9926 = (struct field *) TYPE_ZALLOC (type, sizeof (struct field));
9927
9928 /* Install the variant part. */
9929 TYPE_FIELD_TYPE (type, 0) = union_type;
9930 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
9931 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
9932 }
9933 else if (TYPE_NFIELDS (type) == 1)
9934 {
9935 /* We assume that a union with a single field is a univariant
9936 enum. */
9937 /* Smash this type to be a structure type. We have to do this
9938 because the type has already been recorded. */
9939 TYPE_CODE (type) = TYPE_CODE_STRUCT;
9940
9941 /* Make a union to hold the variants. */
9942 struct type *union_type = alloc_type (objfile);
9943 TYPE_CODE (union_type) = TYPE_CODE_UNION;
9944 TYPE_NFIELDS (union_type) = TYPE_NFIELDS (type);
9945 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
2b4424c3 9946 set_type_align (union_type, TYPE_RAW_ALIGN (type));
c9317f21
TT
9947 TYPE_FIELDS (union_type) = TYPE_FIELDS (type);
9948
9949 struct type *field_type = TYPE_FIELD_TYPE (union_type, 0);
9950 const char *variant_name
9951 = rust_last_path_segment (TYPE_NAME (field_type));
9952 TYPE_FIELD_NAME (union_type, 0) = variant_name;
9953 TYPE_NAME (field_type)
9954 = rust_fully_qualify (&objfile->objfile_obstack,
c7b15a66 9955 TYPE_NAME (type), variant_name);
c9317f21
TT
9956
9957 /* Install the union in the outer struct type. */
9958 TYPE_NFIELDS (type) = 1;
9959 TYPE_FIELDS (type)
9960 = (struct field *) TYPE_ZALLOC (union_type, sizeof (struct field));
9961 TYPE_FIELD_TYPE (type, 0) = union_type;
9962 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
9963 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
9964
9965 alloc_discriminant_info (union_type, -1, 0);
9966 }
9967 else
9968 {
9969 struct type *disr_type = nullptr;
9970 for (int i = 0; i < TYPE_NFIELDS (type); ++i)
9971 {
9972 disr_type = TYPE_FIELD_TYPE (type, i);
9973
a037790e
TT
9974 if (TYPE_CODE (disr_type) != TYPE_CODE_STRUCT)
9975 {
9976 /* All fields of a true enum will be structs. */
9977 return;
9978 }
9979 else if (TYPE_NFIELDS (disr_type) == 0)
c9317f21
TT
9980 {
9981 /* Could be data-less variant, so keep going. */
a037790e 9982 disr_type = nullptr;
c9317f21
TT
9983 }
9984 else if (strcmp (TYPE_FIELD_NAME (disr_type, 0),
9985 "RUST$ENUM$DISR") != 0)
9986 {
9987 /* Not a Rust enum. */
9988 return;
9989 }
9990 else
9991 {
9992 /* Found one. */
9993 break;
9994 }
9995 }
9996
9997 /* If we got here without a discriminant, then it's probably
9998 just a union. */
9999 if (disr_type == nullptr)
10000 return;
10001
10002 /* Smash this type to be a structure type. We have to do this
10003 because the type has already been recorded. */
10004 TYPE_CODE (type) = TYPE_CODE_STRUCT;
10005
10006 /* Make a union to hold the variants. */
10007 struct field *disr_field = &TYPE_FIELD (disr_type, 0);
10008 struct type *union_type = alloc_type (objfile);
10009 TYPE_CODE (union_type) = TYPE_CODE_UNION;
10010 TYPE_NFIELDS (union_type) = 1 + TYPE_NFIELDS (type);
10011 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
2b4424c3 10012 set_type_align (union_type, TYPE_RAW_ALIGN (type));
c9317f21
TT
10013 TYPE_FIELDS (union_type)
10014 = (struct field *) TYPE_ZALLOC (union_type,
10015 (TYPE_NFIELDS (union_type)
10016 * sizeof (struct field)));
10017
10018 memcpy (TYPE_FIELDS (union_type) + 1, TYPE_FIELDS (type),
10019 TYPE_NFIELDS (type) * sizeof (struct field));
10020
10021 /* Install the discriminant at index 0 in the union. */
10022 TYPE_FIELD (union_type, 0) = *disr_field;
10023 TYPE_FIELD_ARTIFICIAL (union_type, 0) = 1;
10024 TYPE_FIELD_NAME (union_type, 0) = "<<discriminant>>";
10025
10026 /* Install the union in the outer struct type. */
10027 TYPE_FIELD_TYPE (type, 0) = union_type;
10028 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
10029 TYPE_NFIELDS (type) = 1;
10030
10031 /* Set the size and offset of the union type. */
10032 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
10033
10034 /* We need a way to find the correct discriminant given a
10035 variant name. For convenience we build a map here. */
10036 struct type *enum_type = FIELD_TYPE (*disr_field);
10037 std::unordered_map<std::string, ULONGEST> discriminant_map;
10038 for (int i = 0; i < TYPE_NFIELDS (enum_type); ++i)
10039 {
10040 if (TYPE_FIELD_LOC_KIND (enum_type, i) == FIELD_LOC_KIND_ENUMVAL)
10041 {
10042 const char *name
10043 = rust_last_path_segment (TYPE_FIELD_NAME (enum_type, i));
10044 discriminant_map[name] = TYPE_FIELD_ENUMVAL (enum_type, i);
10045 }
10046 }
10047
10048 int n_fields = TYPE_NFIELDS (union_type);
10049 struct discriminant_info *disc
10050 = alloc_discriminant_info (union_type, 0, -1);
10051 /* Skip the discriminant here. */
10052 for (int i = 1; i < n_fields; ++i)
10053 {
10054 /* Find the final word in the name of this variant's type.
10055 That name can be used to look up the correct
10056 discriminant. */
10057 const char *variant_name
10058 = rust_last_path_segment (TYPE_NAME (TYPE_FIELD_TYPE (union_type,
10059 i)));
10060
10061 auto iter = discriminant_map.find (variant_name);
10062 if (iter != discriminant_map.end ())
10063 disc->discriminants[i] = iter->second;
10064
bedda9ac 10065 /* Remove the discriminant field, if it exists. */
c9317f21 10066 struct type *sub_type = TYPE_FIELD_TYPE (union_type, i);
bedda9ac
TT
10067 if (TYPE_NFIELDS (sub_type) > 0)
10068 {
10069 --TYPE_NFIELDS (sub_type);
10070 ++TYPE_FIELDS (sub_type);
10071 }
c9317f21
TT
10072 TYPE_FIELD_NAME (union_type, i) = variant_name;
10073 TYPE_NAME (sub_type)
10074 = rust_fully_qualify (&objfile->objfile_obstack,
10075 TYPE_NAME (type), variant_name);
10076 }
10077 }
10078}
10079
10080/* Rewrite some Rust unions to be structures with variants parts. */
10081
10082static void
10083rust_union_quirks (struct dwarf2_cu *cu)
10084{
10085 gdb_assert (cu->language == language_rust);
52941706
SM
10086 for (type *type_ : cu->rust_unions)
10087 quirk_rust_enum (type_, cu->per_cu->dwarf2_per_objfile->objfile);
2d79090e
TT
10088 /* We don't need this any more. */
10089 cu->rust_unions.clear ();
c9317f21
TT
10090}
10091
95554aad
TT
10092/* Return the symtab for PER_CU. This works properly regardless of
10093 whether we're using the index or psymtabs. */
10094
43f3e411
DE
10095static struct compunit_symtab *
10096get_compunit_symtab (struct dwarf2_per_cu_data *per_cu)
95554aad 10097{
ed2dc618 10098 return (per_cu->dwarf2_per_objfile->using_index
43f3e411
DE
10099 ? per_cu->v.quick->compunit_symtab
10100 : per_cu->v.psymtab->compunit_symtab);
95554aad
TT
10101}
10102
10103/* A helper function for computing the list of all symbol tables
10104 included by PER_CU. */
10105
10106static void
43f3e411 10107recursively_compute_inclusions (VEC (compunit_symtab_ptr) **result,
ec94af83 10108 htab_t all_children, htab_t all_type_symtabs,
f9125b6c 10109 struct dwarf2_per_cu_data *per_cu,
43f3e411 10110 struct compunit_symtab *immediate_parent)
95554aad
TT
10111{
10112 void **slot;
10113 int ix;
43f3e411 10114 struct compunit_symtab *cust;
95554aad
TT
10115 struct dwarf2_per_cu_data *iter;
10116
10117 slot = htab_find_slot (all_children, per_cu, INSERT);
10118 if (*slot != NULL)
10119 {
10120 /* This inclusion and its children have been processed. */
10121 return;
10122 }
10123
10124 *slot = per_cu;
10125 /* Only add a CU if it has a symbol table. */
43f3e411
DE
10126 cust = get_compunit_symtab (per_cu);
10127 if (cust != NULL)
ec94af83
DE
10128 {
10129 /* If this is a type unit only add its symbol table if we haven't
10130 seen it yet (type unit per_cu's can share symtabs). */
10131 if (per_cu->is_debug_types)
10132 {
43f3e411 10133 slot = htab_find_slot (all_type_symtabs, cust, INSERT);
ec94af83
DE
10134 if (*slot == NULL)
10135 {
43f3e411
DE
10136 *slot = cust;
10137 VEC_safe_push (compunit_symtab_ptr, *result, cust);
10138 if (cust->user == NULL)
10139 cust->user = immediate_parent;
ec94af83
DE
10140 }
10141 }
10142 else
f9125b6c 10143 {
43f3e411
DE
10144 VEC_safe_push (compunit_symtab_ptr, *result, cust);
10145 if (cust->user == NULL)
10146 cust->user = immediate_parent;
f9125b6c 10147 }
ec94af83 10148 }
95554aad
TT
10149
10150 for (ix = 0;
796a7ff8 10151 VEC_iterate (dwarf2_per_cu_ptr, per_cu->imported_symtabs, ix, iter);
95554aad 10152 ++ix)
ec94af83
DE
10153 {
10154 recursively_compute_inclusions (result, all_children,
43f3e411 10155 all_type_symtabs, iter, cust);
ec94af83 10156 }
95554aad
TT
10157}
10158
43f3e411 10159/* Compute the compunit_symtab 'includes' fields for the compunit_symtab of
95554aad
TT
10160 PER_CU. */
10161
10162static void
43f3e411 10163compute_compunit_symtab_includes (struct dwarf2_per_cu_data *per_cu)
95554aad 10164{
f4dc4d17
DE
10165 gdb_assert (! per_cu->is_debug_types);
10166
796a7ff8 10167 if (!VEC_empty (dwarf2_per_cu_ptr, per_cu->imported_symtabs))
95554aad
TT
10168 {
10169 int ix, len;
ec94af83 10170 struct dwarf2_per_cu_data *per_cu_iter;
43f3e411
DE
10171 struct compunit_symtab *compunit_symtab_iter;
10172 VEC (compunit_symtab_ptr) *result_symtabs = NULL;
ec94af83 10173 htab_t all_children, all_type_symtabs;
43f3e411 10174 struct compunit_symtab *cust = get_compunit_symtab (per_cu);
95554aad
TT
10175
10176 /* If we don't have a symtab, we can just skip this case. */
43f3e411 10177 if (cust == NULL)
95554aad
TT
10178 return;
10179
10180 all_children = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
10181 NULL, xcalloc, xfree);
ec94af83
DE
10182 all_type_symtabs = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
10183 NULL, xcalloc, xfree);
95554aad
TT
10184
10185 for (ix = 0;
796a7ff8 10186 VEC_iterate (dwarf2_per_cu_ptr, per_cu->imported_symtabs,
ec94af83 10187 ix, per_cu_iter);
95554aad 10188 ++ix)
ec94af83
DE
10189 {
10190 recursively_compute_inclusions (&result_symtabs, all_children,
f9125b6c 10191 all_type_symtabs, per_cu_iter,
43f3e411 10192 cust);
ec94af83 10193 }
95554aad 10194
ec94af83 10195 /* Now we have a transitive closure of all the included symtabs. */
43f3e411
DE
10196 len = VEC_length (compunit_symtab_ptr, result_symtabs);
10197 cust->includes
ed2dc618 10198 = XOBNEWVEC (&per_cu->dwarf2_per_objfile->objfile->objfile_obstack,
8d749320 10199 struct compunit_symtab *, len + 1);
95554aad 10200 for (ix = 0;
43f3e411
DE
10201 VEC_iterate (compunit_symtab_ptr, result_symtabs, ix,
10202 compunit_symtab_iter);
95554aad 10203 ++ix)
43f3e411
DE
10204 cust->includes[ix] = compunit_symtab_iter;
10205 cust->includes[len] = NULL;
95554aad 10206
43f3e411 10207 VEC_free (compunit_symtab_ptr, result_symtabs);
95554aad 10208 htab_delete (all_children);
ec94af83 10209 htab_delete (all_type_symtabs);
95554aad
TT
10210 }
10211}
10212
10213/* Compute the 'includes' field for the symtabs of all the CUs we just
10214 read. */
10215
10216static void
ed2dc618 10217process_cu_includes (struct dwarf2_per_objfile *dwarf2_per_objfile)
95554aad 10218{
71b73764 10219 for (dwarf2_per_cu_data *iter : dwarf2_per_objfile->just_read_cus)
f4dc4d17
DE
10220 {
10221 if (! iter->is_debug_types)
43f3e411 10222 compute_compunit_symtab_includes (iter);
f4dc4d17 10223 }
95554aad 10224
c5d0225d 10225 dwarf2_per_objfile->just_read_cus.clear ();
95554aad
TT
10226}
10227
9cdd5dbd 10228/* Generate full symbol information for PER_CU, whose DIEs have
10b3939b
DJ
10229 already been loaded into memory. */
10230
10231static void
95554aad
TT
10232process_full_comp_unit (struct dwarf2_per_cu_data *per_cu,
10233 enum language pretend_language)
10b3939b 10234{
10b3939b 10235 struct dwarf2_cu *cu = per_cu->cu;
ed2dc618
SM
10236 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
10237 struct objfile *objfile = dwarf2_per_objfile->objfile;
3e29f34a 10238 struct gdbarch *gdbarch = get_objfile_arch (objfile);
10b3939b 10239 CORE_ADDR lowpc, highpc;
43f3e411 10240 struct compunit_symtab *cust;
10b3939b 10241 CORE_ADDR baseaddr;
4359dff1 10242 struct block *static_block;
3e29f34a 10243 CORE_ADDR addr;
10b3939b
DJ
10244
10245 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
10246
10b3939b 10247 buildsym_init ();
33c7c59d 10248 scoped_free_pendings free_pending;
c89b44cd
TT
10249
10250 /* Clear the list here in case something was left over. */
10251 cu->method_list.clear ();
10b3939b
DJ
10252
10253 cu->list_in_scope = &file_symbols;
c906108c 10254
95554aad
TT
10255 cu->language = pretend_language;
10256 cu->language_defn = language_def (cu->language);
10257
c906108c 10258 /* Do line number decoding in read_file_scope () */
10b3939b 10259 process_die (cu->dies, cu);
c906108c 10260
a766d390
DE
10261 /* For now fudge the Go package. */
10262 if (cu->language == language_go)
10263 fixup_go_packaging (cu);
10264
3da10d80
KS
10265 /* Now that we have processed all the DIEs in the CU, all the types
10266 should be complete, and it should now be safe to compute all of the
10267 physnames. */
10268 compute_delayed_physnames (cu);
3da10d80 10269
c9317f21
TT
10270 if (cu->language == language_rust)
10271 rust_union_quirks (cu);
10272
fae299cd
DC
10273 /* Some compilers don't define a DW_AT_high_pc attribute for the
10274 compilation unit. If the DW_AT_high_pc is missing, synthesize
10275 it, by scanning the DIE's below the compilation unit. */
10b3939b 10276 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
c906108c 10277
3e29f34a
MR
10278 addr = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
10279 static_block = end_symtab_get_static_block (addr, 0, 1);
4359dff1
JK
10280
10281 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
10282 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
10283 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
10284 addrmap to help ensure it has an accurate map of pc values belonging to
10285 this comp unit. */
10286 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
10287
43f3e411
DE
10288 cust = end_symtab_from_static_block (static_block,
10289 SECT_OFF_TEXT (objfile), 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);
f4dc4d17 10334}
45cfd468 10335
f4dc4d17
DE
10336/* Generate full symbol information for type unit PER_CU, whose DIEs have
10337 already been loaded into memory. */
10338
10339static void
10340process_full_type_unit (struct dwarf2_per_cu_data *per_cu,
10341 enum language pretend_language)
10342{
10343 struct dwarf2_cu *cu = per_cu->cu;
ed2dc618
SM
10344 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
10345 struct objfile *objfile = dwarf2_per_objfile->objfile;
43f3e411 10346 struct compunit_symtab *cust;
0186c6a7
DE
10347 struct signatured_type *sig_type;
10348
10349 gdb_assert (per_cu->is_debug_types);
10350 sig_type = (struct signatured_type *) per_cu;
f4dc4d17
DE
10351
10352 buildsym_init ();
33c7c59d 10353 scoped_free_pendings free_pending;
c89b44cd
TT
10354
10355 /* Clear the list here in case something was left over. */
10356 cu->method_list.clear ();
f4dc4d17
DE
10357
10358 cu->list_in_scope = &file_symbols;
10359
10360 cu->language = pretend_language;
10361 cu->language_defn = language_def (cu->language);
10362
10363 /* The symbol tables are set up in read_type_unit_scope. */
10364 process_die (cu->dies, cu);
10365
10366 /* For now fudge the Go package. */
10367 if (cu->language == language_go)
10368 fixup_go_packaging (cu);
10369
10370 /* Now that we have processed all the DIEs in the CU, all the types
10371 should be complete, and it should now be safe to compute all of the
10372 physnames. */
10373 compute_delayed_physnames (cu);
f4dc4d17 10374
c9317f21
TT
10375 if (cu->language == language_rust)
10376 rust_union_quirks (cu);
10377
f4dc4d17
DE
10378 /* TUs share symbol tables.
10379 If this is the first TU to use this symtab, complete the construction
094b34ac
DE
10380 of it with end_expandable_symtab. Otherwise, complete the addition of
10381 this TU's symbols to the existing symtab. */
43f3e411 10382 if (sig_type->type_unit_group->compunit_symtab == NULL)
45cfd468 10383 {
43f3e411
DE
10384 cust = end_expandable_symtab (0, SECT_OFF_TEXT (objfile));
10385 sig_type->type_unit_group->compunit_symtab = cust;
f4dc4d17 10386
43f3e411 10387 if (cust != NULL)
f4dc4d17
DE
10388 {
10389 /* Set symtab language to language from DW_AT_language. If the
10390 compilation is from a C file generated by language preprocessors,
10391 do not set the language if it was already deduced by
10392 start_subfile. */
43f3e411
DE
10393 if (!(cu->language == language_c
10394 && COMPUNIT_FILETABS (cust)->language != language_c))
10395 COMPUNIT_FILETABS (cust)->language = cu->language;
f4dc4d17
DE
10396 }
10397 }
10398 else
10399 {
0ab9ce85 10400 augment_type_symtab ();
43f3e411 10401 cust = sig_type->type_unit_group->compunit_symtab;
f4dc4d17
DE
10402 }
10403
10404 if (dwarf2_per_objfile->using_index)
43f3e411 10405 per_cu->v.quick->compunit_symtab = cust;
f4dc4d17
DE
10406 else
10407 {
10408 struct partial_symtab *pst = per_cu->v.psymtab;
43f3e411 10409 pst->compunit_symtab = cust;
f4dc4d17 10410 pst->readin = 1;
45cfd468 10411 }
c906108c
SS
10412}
10413
95554aad
TT
10414/* Process an imported unit DIE. */
10415
10416static void
10417process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
10418{
10419 struct attribute *attr;
10420
f4dc4d17
DE
10421 /* For now we don't handle imported units in type units. */
10422 if (cu->per_cu->is_debug_types)
10423 {
10424 error (_("Dwarf Error: DW_TAG_imported_unit is not"
10425 " supported in type units [in module %s]"),
518817b3 10426 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
f4dc4d17
DE
10427 }
10428
95554aad
TT
10429 attr = dwarf2_attr (die, DW_AT_import, cu);
10430 if (attr != NULL)
10431 {
9c541725
PA
10432 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
10433 bool is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
10434 dwarf2_per_cu_data *per_cu
e3b94546 10435 = dwarf2_find_containing_comp_unit (sect_off, is_dwz,
518817b3 10436 cu->per_cu->dwarf2_per_objfile);
95554aad 10437
69d751e3 10438 /* If necessary, add it to the queue and load its DIEs. */
95554aad 10439 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
58f0c718 10440 load_full_comp_unit (per_cu, false, cu->language);
95554aad 10441
796a7ff8 10442 VEC_safe_push (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs,
95554aad
TT
10443 per_cu);
10444 }
10445}
10446
4c8aa72d
PA
10447/* RAII object that represents a process_die scope: i.e.,
10448 starts/finishes processing a DIE. */
10449class process_die_scope
adde2bff 10450{
4c8aa72d
PA
10451public:
10452 process_die_scope (die_info *die, dwarf2_cu *cu)
10453 : m_die (die), m_cu (cu)
10454 {
10455 /* We should only be processing DIEs not already in process. */
10456 gdb_assert (!m_die->in_process);
10457 m_die->in_process = true;
10458 }
8c3cb9fa 10459
4c8aa72d
PA
10460 ~process_die_scope ()
10461 {
10462 m_die->in_process = false;
10463
10464 /* If we're done processing the DIE for the CU that owns the line
10465 header, we don't need the line header anymore. */
10466 if (m_cu->line_header_die_owner == m_die)
10467 {
10468 delete m_cu->line_header;
10469 m_cu->line_header = NULL;
10470 m_cu->line_header_die_owner = NULL;
10471 }
10472 }
10473
10474private:
10475 die_info *m_die;
10476 dwarf2_cu *m_cu;
10477};
adde2bff 10478
c906108c
SS
10479/* Process a die and its children. */
10480
10481static void
e7c27a73 10482process_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 10483{
4c8aa72d 10484 process_die_scope scope (die, cu);
adde2bff 10485
c906108c
SS
10486 switch (die->tag)
10487 {
10488 case DW_TAG_padding:
10489 break;
10490 case DW_TAG_compile_unit:
95554aad 10491 case DW_TAG_partial_unit:
e7c27a73 10492 read_file_scope (die, cu);
c906108c 10493 break;
348e048f
DE
10494 case DW_TAG_type_unit:
10495 read_type_unit_scope (die, cu);
10496 break;
c906108c 10497 case DW_TAG_subprogram:
c906108c 10498 case DW_TAG_inlined_subroutine:
edb3359d 10499 read_func_scope (die, cu);
c906108c
SS
10500 break;
10501 case DW_TAG_lexical_block:
14898363
L
10502 case DW_TAG_try_block:
10503 case DW_TAG_catch_block:
e7c27a73 10504 read_lexical_block_scope (die, cu);
c906108c 10505 break;
216f72a1 10506 case DW_TAG_call_site:
96408a79
SA
10507 case DW_TAG_GNU_call_site:
10508 read_call_site_scope (die, cu);
10509 break;
c906108c 10510 case DW_TAG_class_type:
680b30c7 10511 case DW_TAG_interface_type:
c906108c
SS
10512 case DW_TAG_structure_type:
10513 case DW_TAG_union_type:
134d01f1 10514 process_structure_scope (die, cu);
c906108c
SS
10515 break;
10516 case DW_TAG_enumeration_type:
134d01f1 10517 process_enumeration_scope (die, cu);
c906108c 10518 break;
134d01f1 10519
f792889a
DJ
10520 /* These dies have a type, but processing them does not create
10521 a symbol or recurse to process the children. Therefore we can
10522 read them on-demand through read_type_die. */
c906108c 10523 case DW_TAG_subroutine_type:
72019c9c 10524 case DW_TAG_set_type:
c906108c 10525 case DW_TAG_array_type:
c906108c 10526 case DW_TAG_pointer_type:
c906108c 10527 case DW_TAG_ptr_to_member_type:
c906108c 10528 case DW_TAG_reference_type:
4297a3f0 10529 case DW_TAG_rvalue_reference_type:
c906108c 10530 case DW_TAG_string_type:
c906108c 10531 break;
134d01f1 10532
c906108c 10533 case DW_TAG_base_type:
a02abb62 10534 case DW_TAG_subrange_type:
cb249c71 10535 case DW_TAG_typedef:
134d01f1
DJ
10536 /* Add a typedef symbol for the type definition, if it has a
10537 DW_AT_name. */
f792889a 10538 new_symbol (die, read_type_die (die, cu), cu);
a02abb62 10539 break;
c906108c 10540 case DW_TAG_common_block:
e7c27a73 10541 read_common_block (die, cu);
c906108c
SS
10542 break;
10543 case DW_TAG_common_inclusion:
10544 break;
d9fa45fe 10545 case DW_TAG_namespace:
4d4ec4e5 10546 cu->processing_has_namespace_info = 1;
e7c27a73 10547 read_namespace (die, cu);
d9fa45fe 10548 break;
5d7cb8df 10549 case DW_TAG_module:
4d4ec4e5 10550 cu->processing_has_namespace_info = 1;
5d7cb8df
JK
10551 read_module (die, cu);
10552 break;
d9fa45fe 10553 case DW_TAG_imported_declaration:
74921315
KS
10554 cu->processing_has_namespace_info = 1;
10555 if (read_namespace_alias (die, cu))
10556 break;
86a73007
TT
10557 /* The declaration is not a global namespace alias. */
10558 /* Fall through. */
d9fa45fe 10559 case DW_TAG_imported_module:
4d4ec4e5 10560 cu->processing_has_namespace_info = 1;
27aa8d6a
SW
10561 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
10562 || cu->language != language_fortran))
b98664d3 10563 complaint (_("Tag '%s' has unexpected children"),
27aa8d6a
SW
10564 dwarf_tag_name (die->tag));
10565 read_import_statement (die, cu);
d9fa45fe 10566 break;
95554aad
TT
10567
10568 case DW_TAG_imported_unit:
10569 process_imported_unit_die (die, cu);
10570 break;
10571
71a3c369
TT
10572 case DW_TAG_variable:
10573 read_variable (die, cu);
10574 break;
10575
c906108c 10576 default:
e7c27a73 10577 new_symbol (die, NULL, cu);
c906108c
SS
10578 break;
10579 }
10580}
ca69b9e6
DE
10581\f
10582/* DWARF name computation. */
c906108c 10583
94af9270
KS
10584/* A helper function for dwarf2_compute_name which determines whether DIE
10585 needs to have the name of the scope prepended to the name listed in the
10586 die. */
10587
10588static int
10589die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
10590{
1c809c68
TT
10591 struct attribute *attr;
10592
94af9270
KS
10593 switch (die->tag)
10594 {
10595 case DW_TAG_namespace:
10596 case DW_TAG_typedef:
10597 case DW_TAG_class_type:
10598 case DW_TAG_interface_type:
10599 case DW_TAG_structure_type:
10600 case DW_TAG_union_type:
10601 case DW_TAG_enumeration_type:
10602 case DW_TAG_enumerator:
10603 case DW_TAG_subprogram:
08a76f8a 10604 case DW_TAG_inlined_subroutine:
94af9270 10605 case DW_TAG_member:
74921315 10606 case DW_TAG_imported_declaration:
94af9270
KS
10607 return 1;
10608
10609 case DW_TAG_variable:
c2b0a229 10610 case DW_TAG_constant:
94af9270
KS
10611 /* We only need to prefix "globally" visible variables. These include
10612 any variable marked with DW_AT_external or any variable that
10613 lives in a namespace. [Variables in anonymous namespaces
10614 require prefixing, but they are not DW_AT_external.] */
10615
10616 if (dwarf2_attr (die, DW_AT_specification, cu))
10617 {
10618 struct dwarf2_cu *spec_cu = cu;
9a619af0 10619
94af9270
KS
10620 return die_needs_namespace (die_specification (die, &spec_cu),
10621 spec_cu);
10622 }
10623
1c809c68 10624 attr = dwarf2_attr (die, DW_AT_external, cu);
f55ee35c
JK
10625 if (attr == NULL && die->parent->tag != DW_TAG_namespace
10626 && die->parent->tag != DW_TAG_module)
1c809c68
TT
10627 return 0;
10628 /* A variable in a lexical block of some kind does not need a
10629 namespace, even though in C++ such variables may be external
10630 and have a mangled name. */
10631 if (die->parent->tag == DW_TAG_lexical_block
10632 || die->parent->tag == DW_TAG_try_block
1054b214
TT
10633 || die->parent->tag == DW_TAG_catch_block
10634 || die->parent->tag == DW_TAG_subprogram)
1c809c68
TT
10635 return 0;
10636 return 1;
94af9270
KS
10637
10638 default:
10639 return 0;
10640 }
10641}
10642
73b9be8b
KS
10643/* Return the DIE's linkage name attribute, either DW_AT_linkage_name
10644 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10645 defined for the given DIE. */
10646
10647static struct attribute *
10648dw2_linkage_name_attr (struct die_info *die, struct dwarf2_cu *cu)
10649{
10650 struct attribute *attr;
10651
10652 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
10653 if (attr == NULL)
10654 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
10655
10656 return attr;
10657}
10658
10659/* Return the DIE's linkage name as a string, either DW_AT_linkage_name
10660 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10661 defined for the given DIE. */
10662
10663static const char *
10664dw2_linkage_name (struct die_info *die, struct dwarf2_cu *cu)
10665{
10666 const char *linkage_name;
10667
10668 linkage_name = dwarf2_string_attr (die, DW_AT_linkage_name, cu);
10669 if (linkage_name == NULL)
10670 linkage_name = dwarf2_string_attr (die, DW_AT_MIPS_linkage_name, cu);
10671
10672 return linkage_name;
10673}
10674
94af9270 10675/* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
a766d390 10676 compute the physname for the object, which include a method's:
9c37b5ae 10677 - formal parameters (C++),
a766d390 10678 - receiver type (Go),
a766d390
DE
10679
10680 The term "physname" is a bit confusing.
10681 For C++, for example, it is the demangled name.
10682 For Go, for example, it's the mangled name.
94af9270 10683
af6b7be1
JB
10684 For Ada, return the DIE's linkage name rather than the fully qualified
10685 name. PHYSNAME is ignored..
10686
94af9270
KS
10687 The result is allocated on the objfile_obstack and canonicalized. */
10688
10689static const char *
15d034d0
TT
10690dwarf2_compute_name (const char *name,
10691 struct die_info *die, struct dwarf2_cu *cu,
94af9270
KS
10692 int physname)
10693{
518817b3 10694 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
bb5ed363 10695
94af9270
KS
10696 if (name == NULL)
10697 name = dwarf2_name (die, cu);
10698
2ee7123e
DE
10699 /* For Fortran GDB prefers DW_AT_*linkage_name for the physname if present
10700 but otherwise compute it by typename_concat inside GDB.
10701 FIXME: Actually this is not really true, or at least not always true.
10702 It's all very confusing. SYMBOL_SET_NAMES doesn't try to demangle
5e2db402 10703 Fortran names because there is no mangling standard. So new_symbol
2ee7123e
DE
10704 will set the demangled name to the result of dwarf2_full_name, and it is
10705 the demangled name that GDB uses if it exists. */
f55ee35c
JK
10706 if (cu->language == language_ada
10707 || (cu->language == language_fortran && physname))
10708 {
10709 /* For Ada unit, we prefer the linkage name over the name, as
10710 the former contains the exported name, which the user expects
10711 to be able to reference. Ideally, we want the user to be able
10712 to reference this entity using either natural or linkage name,
10713 but we haven't started looking at this enhancement yet. */
73b9be8b 10714 const char *linkage_name = dw2_linkage_name (die, cu);
f55ee35c 10715
2ee7123e
DE
10716 if (linkage_name != NULL)
10717 return linkage_name;
f55ee35c
JK
10718 }
10719
94af9270
KS
10720 /* These are the only languages we know how to qualify names in. */
10721 if (name != NULL
9c37b5ae 10722 && (cu->language == language_cplus
c44af4eb
TT
10723 || cu->language == language_fortran || cu->language == language_d
10724 || cu->language == language_rust))
94af9270
KS
10725 {
10726 if (die_needs_namespace (die, cu))
10727 {
0d5cff50 10728 const char *prefix;
34a68019 10729 const char *canonical_name = NULL;
94af9270 10730
d7e74731
PA
10731 string_file buf;
10732
94af9270 10733 prefix = determine_prefix (die, cu);
94af9270
KS
10734 if (*prefix != '\0')
10735 {
f55ee35c
JK
10736 char *prefixed_name = typename_concat (NULL, prefix, name,
10737 physname, cu);
9a619af0 10738
d7e74731 10739 buf.puts (prefixed_name);
94af9270
KS
10740 xfree (prefixed_name);
10741 }
10742 else
d7e74731 10743 buf.puts (name);
94af9270 10744
98bfdba5
PA
10745 /* Template parameters may be specified in the DIE's DW_AT_name, or
10746 as children with DW_TAG_template_type_param or
10747 DW_TAG_value_type_param. If the latter, add them to the name
10748 here. If the name already has template parameters, then
10749 skip this step; some versions of GCC emit both, and
10750 it is more efficient to use the pre-computed name.
10751
10752 Something to keep in mind about this process: it is very
10753 unlikely, or in some cases downright impossible, to produce
10754 something that will match the mangled name of a function.
10755 If the definition of the function has the same debug info,
10756 we should be able to match up with it anyway. But fallbacks
10757 using the minimal symbol, for instance to find a method
10758 implemented in a stripped copy of libstdc++, will not work.
10759 If we do not have debug info for the definition, we will have to
10760 match them up some other way.
10761
10762 When we do name matching there is a related problem with function
10763 templates; two instantiated function templates are allowed to
10764 differ only by their return types, which we do not add here. */
10765
10766 if (cu->language == language_cplus && strchr (name, '<') == NULL)
10767 {
10768 struct attribute *attr;
10769 struct die_info *child;
10770 int first = 1;
10771
10772 die->building_fullname = 1;
10773
10774 for (child = die->child; child != NULL; child = child->sibling)
10775 {
10776 struct type *type;
12df843f 10777 LONGEST value;
d521ce57 10778 const gdb_byte *bytes;
98bfdba5
PA
10779 struct dwarf2_locexpr_baton *baton;
10780 struct value *v;
10781
10782 if (child->tag != DW_TAG_template_type_param
10783 && child->tag != DW_TAG_template_value_param)
10784 continue;
10785
10786 if (first)
10787 {
d7e74731 10788 buf.puts ("<");
98bfdba5
PA
10789 first = 0;
10790 }
10791 else
d7e74731 10792 buf.puts (", ");
98bfdba5
PA
10793
10794 attr = dwarf2_attr (child, DW_AT_type, cu);
10795 if (attr == NULL)
10796 {
b98664d3 10797 complaint (_("template parameter missing DW_AT_type"));
d7e74731 10798 buf.puts ("UNKNOWN_TYPE");
98bfdba5
PA
10799 continue;
10800 }
10801 type = die_type (child, cu);
10802
10803 if (child->tag == DW_TAG_template_type_param)
10804 {
c1ec8cea
TT
10805 c_print_type (type, "", &buf, -1, 0, cu->language,
10806 &type_print_raw_options);
98bfdba5
PA
10807 continue;
10808 }
10809
10810 attr = dwarf2_attr (child, DW_AT_const_value, cu);
10811 if (attr == NULL)
10812 {
b98664d3 10813 complaint (_("template parameter missing "
3e43a32a 10814 "DW_AT_const_value"));
d7e74731 10815 buf.puts ("UNKNOWN_VALUE");
98bfdba5
PA
10816 continue;
10817 }
10818
10819 dwarf2_const_value_attr (attr, type, name,
10820 &cu->comp_unit_obstack, cu,
10821 &value, &bytes, &baton);
10822
10823 if (TYPE_NOSIGN (type))
10824 /* GDB prints characters as NUMBER 'CHAR'. If that's
10825 changed, this can use value_print instead. */
d7e74731 10826 c_printchar (value, type, &buf);
98bfdba5
PA
10827 else
10828 {
10829 struct value_print_options opts;
10830
10831 if (baton != NULL)
10832 v = dwarf2_evaluate_loc_desc (type, NULL,
10833 baton->data,
10834 baton->size,
10835 baton->per_cu);
10836 else if (bytes != NULL)
10837 {
10838 v = allocate_value (type);
10839 memcpy (value_contents_writeable (v), bytes,
10840 TYPE_LENGTH (type));
10841 }
10842 else
10843 v = value_from_longest (type, value);
10844
3e43a32a
MS
10845 /* Specify decimal so that we do not depend on
10846 the radix. */
98bfdba5
PA
10847 get_formatted_print_options (&opts, 'd');
10848 opts.raw = 1;
d7e74731 10849 value_print (v, &buf, &opts);
98bfdba5 10850 release_value (v);
98bfdba5
PA
10851 }
10852 }
10853
10854 die->building_fullname = 0;
10855
10856 if (!first)
10857 {
10858 /* Close the argument list, with a space if necessary
10859 (nested templates). */
d7e74731
PA
10860 if (!buf.empty () && buf.string ().back () == '>')
10861 buf.puts (" >");
98bfdba5 10862 else
d7e74731 10863 buf.puts (">");
98bfdba5
PA
10864 }
10865 }
10866
9c37b5ae 10867 /* For C++ methods, append formal parameter type
94af9270 10868 information, if PHYSNAME. */
6e70227d 10869
94af9270 10870 if (physname && die->tag == DW_TAG_subprogram
9c37b5ae 10871 && cu->language == language_cplus)
94af9270
KS
10872 {
10873 struct type *type = read_type_die (die, cu);
10874
d7e74731 10875 c_type_print_args (type, &buf, 1, cu->language,
79d43c61 10876 &type_print_raw_options);
94af9270 10877
9c37b5ae 10878 if (cu->language == language_cplus)
94af9270 10879 {
60430eff
DJ
10880 /* Assume that an artificial first parameter is
10881 "this", but do not crash if it is not. RealView
10882 marks unnamed (and thus unused) parameters as
10883 artificial; there is no way to differentiate
10884 the two cases. */
94af9270
KS
10885 if (TYPE_NFIELDS (type) > 0
10886 && TYPE_FIELD_ARTIFICIAL (type, 0)
60430eff 10887 && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
3e43a32a
MS
10888 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
10889 0))))
d7e74731 10890 buf.puts (" const");
94af9270
KS
10891 }
10892 }
10893
d7e74731 10894 const std::string &intermediate_name = buf.string ();
94af9270
KS
10895
10896 if (cu->language == language_cplus)
34a68019 10897 canonical_name
322a8516 10898 = dwarf2_canonicalize_name (intermediate_name.c_str (), cu,
34a68019
TT
10899 &objfile->per_bfd->storage_obstack);
10900
10901 /* If we only computed INTERMEDIATE_NAME, or if
10902 INTERMEDIATE_NAME is already canonical, then we need to
10903 copy it to the appropriate obstack. */
322a8516 10904 if (canonical_name == NULL || canonical_name == intermediate_name.c_str ())
224c3ddb
SM
10905 name = ((const char *)
10906 obstack_copy0 (&objfile->per_bfd->storage_obstack,
322a8516
PA
10907 intermediate_name.c_str (),
10908 intermediate_name.length ()));
34a68019
TT
10909 else
10910 name = canonical_name;
94af9270
KS
10911 }
10912 }
10913
10914 return name;
10915}
10916
0114d602
DJ
10917/* Return the fully qualified name of DIE, based on its DW_AT_name.
10918 If scope qualifiers are appropriate they will be added. The result
34a68019 10919 will be allocated on the storage_obstack, or NULL if the DIE does
94af9270
KS
10920 not have a name. NAME may either be from a previous call to
10921 dwarf2_name or NULL.
10922
9c37b5ae 10923 The output string will be canonicalized (if C++). */
0114d602
DJ
10924
10925static const char *
15d034d0 10926dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
0114d602 10927{
94af9270
KS
10928 return dwarf2_compute_name (name, die, cu, 0);
10929}
0114d602 10930
94af9270
KS
10931/* Construct a physname for the given DIE in CU. NAME may either be
10932 from a previous call to dwarf2_name or NULL. The result will be
10933 allocated on the objfile_objstack or NULL if the DIE does not have a
10934 name.
0114d602 10935
9c37b5ae 10936 The output string will be canonicalized (if C++). */
0114d602 10937
94af9270 10938static const char *
15d034d0 10939dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
94af9270 10940{
518817b3 10941 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
900e11f9 10942 const char *retval, *mangled = NULL, *canon = NULL;
900e11f9
JK
10943 int need_copy = 1;
10944
10945 /* In this case dwarf2_compute_name is just a shortcut not building anything
10946 on its own. */
10947 if (!die_needs_namespace (die, cu))
10948 return dwarf2_compute_name (name, die, cu, 1);
10949
73b9be8b 10950 mangled = dw2_linkage_name (die, cu);
900e11f9 10951
e98c9e7c
TT
10952 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
10953 See https://github.com/rust-lang/rust/issues/32925. */
10954 if (cu->language == language_rust && mangled != NULL
10955 && strchr (mangled, '{') != NULL)
10956 mangled = NULL;
10957
900e11f9
JK
10958 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
10959 has computed. */
791afaa2 10960 gdb::unique_xmalloc_ptr<char> demangled;
7d45c7c3 10961 if (mangled != NULL)
900e11f9 10962 {
900e11f9 10963
59cc4834
JB
10964 if (language_def (cu->language)->la_store_sym_names_in_linkage_form_p)
10965 {
10966 /* Do nothing (do not demangle the symbol name). */
10967 }
10968 else if (cu->language == language_go)
a766d390 10969 {
5e2db402
TT
10970 /* This is a lie, but we already lie to the caller new_symbol.
10971 new_symbol assumes we return the mangled name.
a766d390 10972 This just undoes that lie until things are cleaned up. */
a766d390
DE
10973 }
10974 else
10975 {
0eb876f5
JB
10976 /* Use DMGL_RET_DROP for C++ template functions to suppress
10977 their return type. It is easier for GDB users to search
10978 for such functions as `name(params)' than `long name(params)'.
10979 In such case the minimal symbol names do not match the full
10980 symbol names but for template functions there is never a need
10981 to look up their definition from their declaration so
10982 the only disadvantage remains the minimal symbol variant
10983 `long name(params)' does not have the proper inferior type. */
791afaa2
TT
10984 demangled.reset (gdb_demangle (mangled,
10985 (DMGL_PARAMS | DMGL_ANSI
10986 | DMGL_RET_DROP)));
a766d390 10987 }
900e11f9 10988 if (demangled)
791afaa2 10989 canon = demangled.get ();
900e11f9
JK
10990 else
10991 {
10992 canon = mangled;
10993 need_copy = 0;
10994 }
10995 }
10996
10997 if (canon == NULL || check_physname)
10998 {
10999 const char *physname = dwarf2_compute_name (name, die, cu, 1);
11000
11001 if (canon != NULL && strcmp (physname, canon) != 0)
11002 {
11003 /* It may not mean a bug in GDB. The compiler could also
11004 compute DW_AT_linkage_name incorrectly. But in such case
11005 GDB would need to be bug-to-bug compatible. */
11006
b98664d3 11007 complaint (_("Computed physname <%s> does not match demangled <%s> "
9d8780f0
SM
11008 "(from linkage <%s>) - DIE at %s [in module %s]"),
11009 physname, canon, mangled, sect_offset_str (die->sect_off),
4262abfb 11010 objfile_name (objfile));
900e11f9
JK
11011
11012 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
11013 is available here - over computed PHYSNAME. It is safer
11014 against both buggy GDB and buggy compilers. */
11015
11016 retval = canon;
11017 }
11018 else
11019 {
11020 retval = physname;
11021 need_copy = 0;
11022 }
11023 }
11024 else
11025 retval = canon;
11026
11027 if (need_copy)
224c3ddb
SM
11028 retval = ((const char *)
11029 obstack_copy0 (&objfile->per_bfd->storage_obstack,
11030 retval, strlen (retval)));
900e11f9 11031
900e11f9 11032 return retval;
0114d602
DJ
11033}
11034
74921315
KS
11035/* Inspect DIE in CU for a namespace alias. If one exists, record
11036 a new symbol for it.
11037
11038 Returns 1 if a namespace alias was recorded, 0 otherwise. */
11039
11040static int
11041read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu)
11042{
11043 struct attribute *attr;
11044
11045 /* If the die does not have a name, this is not a namespace
11046 alias. */
11047 attr = dwarf2_attr (die, DW_AT_name, cu);
11048 if (attr != NULL)
11049 {
11050 int num;
11051 struct die_info *d = die;
11052 struct dwarf2_cu *imported_cu = cu;
11053
11054 /* If the compiler has nested DW_AT_imported_declaration DIEs,
11055 keep inspecting DIEs until we hit the underlying import. */
11056#define MAX_NESTED_IMPORTED_DECLARATIONS 100
11057 for (num = 0; num < MAX_NESTED_IMPORTED_DECLARATIONS; ++num)
11058 {
11059 attr = dwarf2_attr (d, DW_AT_import, cu);
11060 if (attr == NULL)
11061 break;
11062
11063 d = follow_die_ref (d, attr, &imported_cu);
11064 if (d->tag != DW_TAG_imported_declaration)
11065 break;
11066 }
11067
11068 if (num == MAX_NESTED_IMPORTED_DECLARATIONS)
11069 {
b98664d3 11070 complaint (_("DIE at %s has too many recursively imported "
9d8780f0 11071 "declarations"), sect_offset_str (d->sect_off));
74921315
KS
11072 return 0;
11073 }
11074
11075 if (attr != NULL)
11076 {
11077 struct type *type;
9c541725 11078 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
74921315 11079
9c541725 11080 type = get_die_type_at_offset (sect_off, cu->per_cu);
74921315
KS
11081 if (type != NULL && TYPE_CODE (type) == TYPE_CODE_NAMESPACE)
11082 {
11083 /* This declaration is a global namespace alias. Add
11084 a symbol for it whose type is the aliased namespace. */
11085 new_symbol (die, type, cu);
11086 return 1;
11087 }
11088 }
11089 }
11090
11091 return 0;
11092}
11093
22cee43f
PMR
11094/* Return the using directives repository (global or local?) to use in the
11095 current context for LANGUAGE.
11096
11097 For Ada, imported declarations can materialize renamings, which *may* be
11098 global. However it is impossible (for now?) in DWARF to distinguish
11099 "external" imported declarations and "static" ones. As all imported
11100 declarations seem to be static in all other languages, make them all CU-wide
11101 global only in Ada. */
11102
11103static struct using_direct **
11104using_directives (enum language language)
11105{
edb0470b 11106 if (language == language_ada && outermost_context_p ())
6cccc9a8 11107 return get_global_using_directives ();
22cee43f 11108 else
6cccc9a8 11109 return get_local_using_directives ();
22cee43f
PMR
11110}
11111
27aa8d6a
SW
11112/* Read the import statement specified by the given die and record it. */
11113
11114static void
11115read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
11116{
518817b3 11117 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
27aa8d6a 11118 struct attribute *import_attr;
32019081 11119 struct die_info *imported_die, *child_die;
de4affc9 11120 struct dwarf2_cu *imported_cu;
27aa8d6a 11121 const char *imported_name;
794684b6 11122 const char *imported_name_prefix;
13387711
SW
11123 const char *canonical_name;
11124 const char *import_alias;
11125 const char *imported_declaration = NULL;
794684b6 11126 const char *import_prefix;
eb1e02fd 11127 std::vector<const char *> excludes;
13387711 11128
27aa8d6a
SW
11129 import_attr = dwarf2_attr (die, DW_AT_import, cu);
11130 if (import_attr == NULL)
11131 {
b98664d3 11132 complaint (_("Tag '%s' has no DW_AT_import"),
27aa8d6a
SW
11133 dwarf_tag_name (die->tag));
11134 return;
11135 }
11136
de4affc9
CC
11137 imported_cu = cu;
11138 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
11139 imported_name = dwarf2_name (imported_die, imported_cu);
27aa8d6a
SW
11140 if (imported_name == NULL)
11141 {
11142 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
11143
11144 The import in the following code:
11145 namespace A
11146 {
11147 typedef int B;
11148 }
11149
11150 int main ()
11151 {
11152 using A::B;
11153 B b;
11154 return b;
11155 }
11156
11157 ...
11158 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
11159 <52> DW_AT_decl_file : 1
11160 <53> DW_AT_decl_line : 6
11161 <54> DW_AT_import : <0x75>
11162 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
11163 <59> DW_AT_name : B
11164 <5b> DW_AT_decl_file : 1
11165 <5c> DW_AT_decl_line : 2
11166 <5d> DW_AT_type : <0x6e>
11167 ...
11168 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
11169 <76> DW_AT_byte_size : 4
11170 <77> DW_AT_encoding : 5 (signed)
11171
11172 imports the wrong die ( 0x75 instead of 0x58 ).
11173 This case will be ignored until the gcc bug is fixed. */
11174 return;
11175 }
11176
82856980
SW
11177 /* Figure out the local name after import. */
11178 import_alias = dwarf2_name (die, cu);
27aa8d6a 11179
794684b6
SW
11180 /* Figure out where the statement is being imported to. */
11181 import_prefix = determine_prefix (die, cu);
11182
11183 /* Figure out what the scope of the imported die is and prepend it
11184 to the name of the imported die. */
de4affc9 11185 imported_name_prefix = determine_prefix (imported_die, imported_cu);
794684b6 11186
f55ee35c
JK
11187 if (imported_die->tag != DW_TAG_namespace
11188 && imported_die->tag != DW_TAG_module)
794684b6 11189 {
13387711
SW
11190 imported_declaration = imported_name;
11191 canonical_name = imported_name_prefix;
794684b6 11192 }
13387711 11193 else if (strlen (imported_name_prefix) > 0)
12aaed36 11194 canonical_name = obconcat (&objfile->objfile_obstack,
45280282
IB
11195 imported_name_prefix,
11196 (cu->language == language_d ? "." : "::"),
11197 imported_name, (char *) NULL);
13387711
SW
11198 else
11199 canonical_name = imported_name;
794684b6 11200
32019081
JK
11201 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
11202 for (child_die = die->child; child_die && child_die->tag;
11203 child_die = sibling_die (child_die))
11204 {
11205 /* DWARF-4: A Fortran use statement with a “rename list” may be
11206 represented by an imported module entry with an import attribute
11207 referring to the module and owned entries corresponding to those
11208 entities that are renamed as part of being imported. */
11209
11210 if (child_die->tag != DW_TAG_imported_declaration)
11211 {
b98664d3 11212 complaint (_("child DW_TAG_imported_declaration expected "
9d8780f0
SM
11213 "- DIE at %s [in module %s]"),
11214 sect_offset_str (child_die->sect_off),
11215 objfile_name (objfile));
32019081
JK
11216 continue;
11217 }
11218
11219 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
11220 if (import_attr == NULL)
11221 {
b98664d3 11222 complaint (_("Tag '%s' has no DW_AT_import"),
32019081
JK
11223 dwarf_tag_name (child_die->tag));
11224 continue;
11225 }
11226
11227 imported_cu = cu;
11228 imported_die = follow_die_ref_or_sig (child_die, import_attr,
11229 &imported_cu);
11230 imported_name = dwarf2_name (imported_die, imported_cu);
11231 if (imported_name == NULL)
11232 {
b98664d3 11233 complaint (_("child DW_TAG_imported_declaration has unknown "
9d8780f0
SM
11234 "imported name - DIE at %s [in module %s]"),
11235 sect_offset_str (child_die->sect_off),
11236 objfile_name (objfile));
32019081
JK
11237 continue;
11238 }
11239
eb1e02fd 11240 excludes.push_back (imported_name);
32019081
JK
11241
11242 process_die (child_die, cu);
11243 }
11244
22cee43f
PMR
11245 add_using_directive (using_directives (cu->language),
11246 import_prefix,
11247 canonical_name,
11248 import_alias,
11249 imported_declaration,
11250 excludes,
11251 0,
11252 &objfile->objfile_obstack);
27aa8d6a
SW
11253}
11254
5230b05a
WT
11255/* ICC<14 does not output the required DW_AT_declaration on incomplete
11256 types, but gives them a size of zero. Starting with version 14,
11257 ICC is compatible with GCC. */
11258
11259static int
11260producer_is_icc_lt_14 (struct dwarf2_cu *cu)
11261{
11262 if (!cu->checked_producer)
11263 check_producer (cu);
11264
11265 return cu->producer_is_icc_lt_14;
11266}
11267
1b80a9fa
JK
11268/* Check for possibly missing DW_AT_comp_dir with relative .debug_line
11269 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
11270 this, it was first present in GCC release 4.3.0. */
11271
11272static int
11273producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
11274{
11275 if (!cu->checked_producer)
11276 check_producer (cu);
11277
11278 return cu->producer_is_gcc_lt_4_3;
11279}
11280
d721ba37
PA
11281static file_and_directory
11282find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu)
9291a0cd 11283{
d721ba37
PA
11284 file_and_directory res;
11285
9291a0cd
TT
11286 /* Find the filename. Do not use dwarf2_name here, since the filename
11287 is not a source language identifier. */
d721ba37
PA
11288 res.name = dwarf2_string_attr (die, DW_AT_name, cu);
11289 res.comp_dir = dwarf2_string_attr (die, DW_AT_comp_dir, cu);
9291a0cd 11290
d721ba37
PA
11291 if (res.comp_dir == NULL
11292 && producer_is_gcc_lt_4_3 (cu) && res.name != NULL
11293 && IS_ABSOLUTE_PATH (res.name))
9291a0cd 11294 {
d721ba37
PA
11295 res.comp_dir_storage = ldirname (res.name);
11296 if (!res.comp_dir_storage.empty ())
11297 res.comp_dir = res.comp_dir_storage.c_str ();
9291a0cd 11298 }
d721ba37 11299 if (res.comp_dir != NULL)
9291a0cd
TT
11300 {
11301 /* Irix 6.2 native cc prepends <machine>.: to the compilation
11302 directory, get rid of it. */
d721ba37 11303 const char *cp = strchr (res.comp_dir, ':');
9291a0cd 11304
d721ba37
PA
11305 if (cp && cp != res.comp_dir && cp[-1] == '.' && cp[1] == '/')
11306 res.comp_dir = cp + 1;
9291a0cd
TT
11307 }
11308
d721ba37
PA
11309 if (res.name == NULL)
11310 res.name = "<unknown>";
11311
11312 return res;
9291a0cd
TT
11313}
11314
f4dc4d17
DE
11315/* Handle DW_AT_stmt_list for a compilation unit.
11316 DIE is the DW_TAG_compile_unit die for CU.
c3b7b696
YQ
11317 COMP_DIR is the compilation directory. LOWPC is passed to
11318 dwarf_decode_lines. See dwarf_decode_lines comments about it. */
2ab95328
TT
11319
11320static void
11321handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
c3b7b696 11322 const char *comp_dir, CORE_ADDR lowpc) /* ARI: editCase function */
2ab95328 11323{
518817b3
SM
11324 struct dwarf2_per_objfile *dwarf2_per_objfile
11325 = cu->per_cu->dwarf2_per_objfile;
527f3840 11326 struct objfile *objfile = dwarf2_per_objfile->objfile;
2ab95328 11327 struct attribute *attr;
527f3840
JK
11328 struct line_header line_header_local;
11329 hashval_t line_header_local_hash;
527f3840
JK
11330 void **slot;
11331 int decode_mapping;
2ab95328 11332
f4dc4d17
DE
11333 gdb_assert (! cu->per_cu->is_debug_types);
11334
2ab95328 11335 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
527f3840
JK
11336 if (attr == NULL)
11337 return;
11338
9c541725 11339 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
527f3840
JK
11340
11341 /* The line header hash table is only created if needed (it exists to
11342 prevent redundant reading of the line table for partial_units).
11343 If we're given a partial_unit, we'll need it. If we're given a
11344 compile_unit, then use the line header hash table if it's already
11345 created, but don't create one just yet. */
11346
11347 if (dwarf2_per_objfile->line_header_hash == NULL
11348 && die->tag == DW_TAG_partial_unit)
2ab95328 11349 {
527f3840
JK
11350 dwarf2_per_objfile->line_header_hash
11351 = htab_create_alloc_ex (127, line_header_hash_voidp,
11352 line_header_eq_voidp,
11353 free_line_header_voidp,
11354 &objfile->objfile_obstack,
11355 hashtab_obstack_allocate,
11356 dummy_obstack_deallocate);
11357 }
2ab95328 11358
9c541725 11359 line_header_local.sect_off = line_offset;
527f3840
JK
11360 line_header_local.offset_in_dwz = cu->per_cu->is_dwz;
11361 line_header_local_hash = line_header_hash (&line_header_local);
11362 if (dwarf2_per_objfile->line_header_hash != NULL)
11363 {
11364 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash,
11365 &line_header_local,
11366 line_header_local_hash, NO_INSERT);
11367
11368 /* For DW_TAG_compile_unit we need info like symtab::linetable which
11369 is not present in *SLOT (since if there is something in *SLOT then
11370 it will be for a partial_unit). */
11371 if (die->tag == DW_TAG_partial_unit && slot != NULL)
dee91e82 11372 {
527f3840 11373 gdb_assert (*slot != NULL);
9a3c8263 11374 cu->line_header = (struct line_header *) *slot;
527f3840 11375 return;
dee91e82 11376 }
2ab95328 11377 }
527f3840
JK
11378
11379 /* dwarf_decode_line_header does not yet provide sufficient information.
11380 We always have to call also dwarf_decode_lines for it. */
fff8551c
PA
11381 line_header_up lh = dwarf_decode_line_header (line_offset, cu);
11382 if (lh == NULL)
527f3840 11383 return;
4c8aa72d
PA
11384
11385 cu->line_header = lh.release ();
11386 cu->line_header_die_owner = die;
527f3840
JK
11387
11388 if (dwarf2_per_objfile->line_header_hash == NULL)
11389 slot = NULL;
11390 else
11391 {
11392 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash,
11393 &line_header_local,
11394 line_header_local_hash, INSERT);
11395 gdb_assert (slot != NULL);
11396 }
11397 if (slot != NULL && *slot == NULL)
11398 {
11399 /* This newly decoded line number information unit will be owned
11400 by line_header_hash hash table. */
11401 *slot = cu->line_header;
4c8aa72d 11402 cu->line_header_die_owner = NULL;
527f3840
JK
11403 }
11404 else
11405 {
11406 /* We cannot free any current entry in (*slot) as that struct line_header
11407 may be already used by multiple CUs. Create only temporary decoded
11408 line_header for this CU - it may happen at most once for each line
11409 number information unit. And if we're not using line_header_hash
11410 then this is what we want as well. */
11411 gdb_assert (die->tag != DW_TAG_partial_unit);
527f3840
JK
11412 }
11413 decode_mapping = (die->tag != DW_TAG_partial_unit);
11414 dwarf_decode_lines (cu->line_header, comp_dir, cu, NULL, lowpc,
11415 decode_mapping);
fff8551c 11416
2ab95328
TT
11417}
11418
95554aad 11419/* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
ae2de4f8 11420
c906108c 11421static void
e7c27a73 11422read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 11423{
518817b3
SM
11424 struct dwarf2_per_objfile *dwarf2_per_objfile
11425 = cu->per_cu->dwarf2_per_objfile;
dee91e82 11426 struct objfile *objfile = dwarf2_per_objfile->objfile;
3e29f34a 11427 struct gdbarch *gdbarch = get_objfile_arch (objfile);
2acceee2 11428 CORE_ADDR lowpc = ((CORE_ADDR) -1);
c906108c
SS
11429 CORE_ADDR highpc = ((CORE_ADDR) 0);
11430 struct attribute *attr;
c906108c 11431 struct die_info *child_die;
e142c38c 11432 CORE_ADDR baseaddr;
6e70227d 11433
e142c38c 11434 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 11435
fae299cd 11436 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
c906108c
SS
11437
11438 /* If we didn't find a lowpc, set it to highpc to avoid complaints
11439 from finish_block. */
2acceee2 11440 if (lowpc == ((CORE_ADDR) -1))
c906108c 11441 lowpc = highpc;
3e29f34a 11442 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
c906108c 11443
d721ba37 11444 file_and_directory fnd = find_file_and_directory (die, cu);
e1024ff1 11445
95554aad 11446 prepare_one_comp_unit (cu, die, cu->language);
303b6f5d 11447
f4b8a18d
KW
11448 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
11449 standardised yet. As a workaround for the language detection we fall
11450 back to the DW_AT_producer string. */
11451 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
11452 cu->language = language_opencl;
11453
3019eac3
DE
11454 /* Similar hack for Go. */
11455 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
11456 set_cu_language (DW_LANG_Go, cu);
11457
d721ba37 11458 dwarf2_start_symtab (cu, fnd.name, fnd.comp_dir, lowpc);
3019eac3
DE
11459
11460 /* Decode line number information if present. We do this before
11461 processing child DIEs, so that the line header table is available
11462 for DW_AT_decl_file. */
d721ba37 11463 handle_DW_AT_stmt_list (die, cu, fnd.comp_dir, lowpc);
3019eac3
DE
11464
11465 /* Process all dies in compilation unit. */
11466 if (die->child != NULL)
11467 {
11468 child_die = die->child;
11469 while (child_die && child_die->tag)
11470 {
11471 process_die (child_die, cu);
11472 child_die = sibling_die (child_die);
11473 }
11474 }
11475
11476 /* Decode macro information, if present. Dwarf 2 macro information
11477 refers to information in the line number info statement program
11478 header, so we can only read it if we've read the header
11479 successfully. */
0af92d60
JK
11480 attr = dwarf2_attr (die, DW_AT_macros, cu);
11481 if (attr == NULL)
11482 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
3019eac3
DE
11483 if (attr && cu->line_header)
11484 {
11485 if (dwarf2_attr (die, DW_AT_macro_info, cu))
b98664d3 11486 complaint (_("CU refers to both DW_AT_macros and DW_AT_macro_info"));
3019eac3 11487
43f3e411 11488 dwarf_decode_macros (cu, DW_UNSND (attr), 1);
3019eac3
DE
11489 }
11490 else
11491 {
11492 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
11493 if (attr && cu->line_header)
11494 {
11495 unsigned int macro_offset = DW_UNSND (attr);
11496
43f3e411 11497 dwarf_decode_macros (cu, macro_offset, 0);
3019eac3
DE
11498 }
11499 }
3019eac3
DE
11500}
11501
f4dc4d17
DE
11502/* TU version of handle_DW_AT_stmt_list for read_type_unit_scope.
11503 Create the set of symtabs used by this TU, or if this TU is sharing
11504 symtabs with another TU and the symtabs have already been created
11505 then restore those symtabs in the line header.
11506 We don't need the pc/line-number mapping for type units. */
3019eac3
DE
11507
11508static void
f4dc4d17 11509setup_type_unit_groups (struct die_info *die, struct dwarf2_cu *cu)
3019eac3 11510{
f4dc4d17
DE
11511 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
11512 struct type_unit_group *tu_group;
11513 int first_time;
3019eac3 11514 struct attribute *attr;
9c541725 11515 unsigned int i;
0186c6a7 11516 struct signatured_type *sig_type;
3019eac3 11517
f4dc4d17 11518 gdb_assert (per_cu->is_debug_types);
0186c6a7 11519 sig_type = (struct signatured_type *) per_cu;
3019eac3 11520
f4dc4d17 11521 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
3019eac3 11522
f4dc4d17 11523 /* If we're using .gdb_index (includes -readnow) then
74e04d1c 11524 per_cu->type_unit_group may not have been set up yet. */
0186c6a7
DE
11525 if (sig_type->type_unit_group == NULL)
11526 sig_type->type_unit_group = get_type_unit_group (cu, attr);
11527 tu_group = sig_type->type_unit_group;
f4dc4d17
DE
11528
11529 /* If we've already processed this stmt_list there's no real need to
11530 do it again, we could fake it and just recreate the part we need
11531 (file name,index -> symtab mapping). If data shows this optimization
11532 is useful we can do it then. */
43f3e411 11533 first_time = tu_group->compunit_symtab == NULL;
f4dc4d17
DE
11534
11535 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
11536 debug info. */
fff8551c 11537 line_header_up lh;
f4dc4d17 11538 if (attr != NULL)
3019eac3 11539 {
9c541725 11540 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
f4dc4d17
DE
11541 lh = dwarf_decode_line_header (line_offset, cu);
11542 }
11543 if (lh == NULL)
11544 {
11545 if (first_time)
11546 dwarf2_start_symtab (cu, "", NULL, 0);
11547 else
11548 {
11549 gdb_assert (tu_group->symtabs == NULL);
0ab9ce85 11550 restart_symtab (tu_group->compunit_symtab, "", 0);
f4dc4d17 11551 }
f4dc4d17 11552 return;
3019eac3
DE
11553 }
11554
4c8aa72d
PA
11555 cu->line_header = lh.release ();
11556 cu->line_header_die_owner = die;
3019eac3 11557
f4dc4d17
DE
11558 if (first_time)
11559 {
43f3e411 11560 struct compunit_symtab *cust = dwarf2_start_symtab (cu, "", NULL, 0);
3019eac3 11561
1fd60fc0
DE
11562 /* Note: We don't assign tu_group->compunit_symtab yet because we're
11563 still initializing it, and our caller (a few levels up)
11564 process_full_type_unit still needs to know if this is the first
11565 time. */
11566
4c8aa72d
PA
11567 tu_group->num_symtabs = cu->line_header->file_names.size ();
11568 tu_group->symtabs = XNEWVEC (struct symtab *,
11569 cu->line_header->file_names.size ());
3019eac3 11570
4c8aa72d 11571 for (i = 0; i < cu->line_header->file_names.size (); ++i)
f4dc4d17 11572 {
4c8aa72d 11573 file_entry &fe = cu->line_header->file_names[i];
3019eac3 11574
4c8aa72d 11575 dwarf2_start_subfile (fe.name, fe.include_dir (cu->line_header));
3019eac3 11576
3c65e5b3 11577 if (get_current_subfile ()->symtab == NULL)
f4dc4d17 11578 {
4c8aa72d
PA
11579 /* NOTE: start_subfile will recognize when it's been
11580 passed a file it has already seen. So we can't
11581 assume there's a simple mapping from
11582 cu->line_header->file_names to subfiles, plus
11583 cu->line_header->file_names may contain dups. */
3c65e5b3
TT
11584 get_current_subfile ()->symtab
11585 = allocate_symtab (cust, get_current_subfile ()->name);
f4dc4d17
DE
11586 }
11587
3c65e5b3 11588 fe.symtab = get_current_subfile ()->symtab;
8c43009f 11589 tu_group->symtabs[i] = fe.symtab;
f4dc4d17
DE
11590 }
11591 }
11592 else
3019eac3 11593 {
0ab9ce85 11594 restart_symtab (tu_group->compunit_symtab, "", 0);
f4dc4d17 11595
4c8aa72d 11596 for (i = 0; i < cu->line_header->file_names.size (); ++i)
f4dc4d17 11597 {
4c8aa72d 11598 file_entry &fe = cu->line_header->file_names[i];
f4dc4d17 11599
4c8aa72d 11600 fe.symtab = tu_group->symtabs[i];
f4dc4d17 11601 }
3019eac3
DE
11602 }
11603
f4dc4d17
DE
11604 /* The main symtab is allocated last. Type units don't have DW_AT_name
11605 so they don't have a "real" (so to speak) symtab anyway.
11606 There is later code that will assign the main symtab to all symbols
11607 that don't have one. We need to handle the case of a symbol with a
11608 missing symtab (DW_AT_decl_file) anyway. */
11609}
3019eac3 11610
f4dc4d17
DE
11611/* Process DW_TAG_type_unit.
11612 For TUs we want to skip the first top level sibling if it's not the
11613 actual type being defined by this TU. In this case the first top
11614 level sibling is there to provide context only. */
3019eac3 11615
f4dc4d17
DE
11616static void
11617read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
11618{
11619 struct die_info *child_die;
3019eac3 11620
f4dc4d17
DE
11621 prepare_one_comp_unit (cu, die, language_minimal);
11622
11623 /* Initialize (or reinitialize) the machinery for building symtabs.
11624 We do this before processing child DIEs, so that the line header table
11625 is available for DW_AT_decl_file. */
11626 setup_type_unit_groups (die, cu);
11627
11628 if (die->child != NULL)
11629 {
11630 child_die = die->child;
11631 while (child_die && child_die->tag)
11632 {
11633 process_die (child_die, cu);
11634 child_die = sibling_die (child_die);
11635 }
11636 }
3019eac3
DE
11637}
11638\f
80626a55
DE
11639/* DWO/DWP files.
11640
11641 http://gcc.gnu.org/wiki/DebugFission
11642 http://gcc.gnu.org/wiki/DebugFissionDWP
11643
11644 To simplify handling of both DWO files ("object" files with the DWARF info)
11645 and DWP files (a file with the DWOs packaged up into one file), we treat
11646 DWP files as having a collection of virtual DWO files. */
3019eac3
DE
11647
11648static hashval_t
11649hash_dwo_file (const void *item)
11650{
9a3c8263 11651 const struct dwo_file *dwo_file = (const struct dwo_file *) item;
a2ce51a0 11652 hashval_t hash;
3019eac3 11653
a2ce51a0
DE
11654 hash = htab_hash_string (dwo_file->dwo_name);
11655 if (dwo_file->comp_dir != NULL)
11656 hash += htab_hash_string (dwo_file->comp_dir);
11657 return hash;
3019eac3
DE
11658}
11659
11660static int
11661eq_dwo_file (const void *item_lhs, const void *item_rhs)
11662{
9a3c8263
SM
11663 const struct dwo_file *lhs = (const struct dwo_file *) item_lhs;
11664 const struct dwo_file *rhs = (const struct dwo_file *) item_rhs;
3019eac3 11665
a2ce51a0
DE
11666 if (strcmp (lhs->dwo_name, rhs->dwo_name) != 0)
11667 return 0;
11668 if (lhs->comp_dir == NULL || rhs->comp_dir == NULL)
11669 return lhs->comp_dir == rhs->comp_dir;
11670 return strcmp (lhs->comp_dir, rhs->comp_dir) == 0;
3019eac3
DE
11671}
11672
11673/* Allocate a hash table for DWO files. */
11674
11675static htab_t
ed2dc618 11676allocate_dwo_file_hash_table (struct objfile *objfile)
3019eac3 11677{
3019eac3
DE
11678 return htab_create_alloc_ex (41,
11679 hash_dwo_file,
11680 eq_dwo_file,
11681 NULL,
11682 &objfile->objfile_obstack,
11683 hashtab_obstack_allocate,
11684 dummy_obstack_deallocate);
11685}
11686
80626a55
DE
11687/* Lookup DWO file DWO_NAME. */
11688
11689static void **
ed2dc618
SM
11690lookup_dwo_file_slot (struct dwarf2_per_objfile *dwarf2_per_objfile,
11691 const char *dwo_name,
11692 const char *comp_dir)
80626a55
DE
11693{
11694 struct dwo_file find_entry;
11695 void **slot;
11696
11697 if (dwarf2_per_objfile->dwo_files == NULL)
ed2dc618
SM
11698 dwarf2_per_objfile->dwo_files
11699 = allocate_dwo_file_hash_table (dwarf2_per_objfile->objfile);
80626a55
DE
11700
11701 memset (&find_entry, 0, sizeof (find_entry));
0ac5b59e
DE
11702 find_entry.dwo_name = dwo_name;
11703 find_entry.comp_dir = comp_dir;
80626a55
DE
11704 slot = htab_find_slot (dwarf2_per_objfile->dwo_files, &find_entry, INSERT);
11705
11706 return slot;
11707}
11708
3019eac3
DE
11709static hashval_t
11710hash_dwo_unit (const void *item)
11711{
9a3c8263 11712 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
3019eac3
DE
11713
11714 /* This drops the top 32 bits of the id, but is ok for a hash. */
11715 return dwo_unit->signature;
11716}
11717
11718static int
11719eq_dwo_unit (const void *item_lhs, const void *item_rhs)
11720{
9a3c8263
SM
11721 const struct dwo_unit *lhs = (const struct dwo_unit *) item_lhs;
11722 const struct dwo_unit *rhs = (const struct dwo_unit *) item_rhs;
3019eac3
DE
11723
11724 /* The signature is assumed to be unique within the DWO file.
11725 So while object file CU dwo_id's always have the value zero,
11726 that's OK, assuming each object file DWO file has only one CU,
11727 and that's the rule for now. */
11728 return lhs->signature == rhs->signature;
11729}
11730
11731/* Allocate a hash table for DWO CUs,TUs.
11732 There is one of these tables for each of CUs,TUs for each DWO file. */
11733
11734static htab_t
11735allocate_dwo_unit_table (struct objfile *objfile)
11736{
11737 /* Start out with a pretty small number.
11738 Generally DWO files contain only one CU and maybe some TUs. */
11739 return htab_create_alloc_ex (3,
11740 hash_dwo_unit,
11741 eq_dwo_unit,
11742 NULL,
11743 &objfile->objfile_obstack,
11744 hashtab_obstack_allocate,
11745 dummy_obstack_deallocate);
11746}
11747
80626a55 11748/* Structure used to pass data to create_dwo_debug_info_hash_table_reader. */
3019eac3 11749
19c3d4c9 11750struct create_dwo_cu_data
3019eac3
DE
11751{
11752 struct dwo_file *dwo_file;
19c3d4c9 11753 struct dwo_unit dwo_unit;
3019eac3
DE
11754};
11755
19c3d4c9 11756/* die_reader_func for create_dwo_cu. */
3019eac3
DE
11757
11758static void
19c3d4c9
DE
11759create_dwo_cu_reader (const struct die_reader_specs *reader,
11760 const gdb_byte *info_ptr,
11761 struct die_info *comp_unit_die,
11762 int has_children,
11763 void *datap)
3019eac3
DE
11764{
11765 struct dwarf2_cu *cu = reader->cu;
9c541725 11766 sect_offset sect_off = cu->per_cu->sect_off;
8a0459fd 11767 struct dwarf2_section_info *section = cu->per_cu->section;
9a3c8263 11768 struct create_dwo_cu_data *data = (struct create_dwo_cu_data *) datap;
3019eac3 11769 struct dwo_file *dwo_file = data->dwo_file;
19c3d4c9 11770 struct dwo_unit *dwo_unit = &data->dwo_unit;
3019eac3 11771 struct attribute *attr;
3019eac3
DE
11772
11773 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
11774 if (attr == NULL)
11775 {
b98664d3 11776 complaint (_("Dwarf Error: debug entry at offset %s is missing"
19c3d4c9 11777 " its dwo_id [in module %s]"),
9d8780f0 11778 sect_offset_str (sect_off), dwo_file->dwo_name);
3019eac3
DE
11779 return;
11780 }
11781
3019eac3
DE
11782 dwo_unit->dwo_file = dwo_file;
11783 dwo_unit->signature = DW_UNSND (attr);
8a0459fd 11784 dwo_unit->section = section;
9c541725 11785 dwo_unit->sect_off = sect_off;
3019eac3
DE
11786 dwo_unit->length = cu->per_cu->length;
11787
b4f54984 11788 if (dwarf_read_debug)
9d8780f0
SM
11789 fprintf_unfiltered (gdb_stdlog, " offset %s, dwo_id %s\n",
11790 sect_offset_str (sect_off),
9c541725 11791 hex_string (dwo_unit->signature));
3019eac3
DE
11792}
11793
33c5cd75 11794/* Create the dwo_units for the CUs in a DWO_FILE.
19c3d4c9 11795 Note: This function processes DWO files only, not DWP files. */
3019eac3 11796
33c5cd75 11797static void
ed2dc618
SM
11798create_cus_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
11799 struct dwo_file &dwo_file, dwarf2_section_info &section,
33c5cd75 11800 htab_t &cus_htab)
3019eac3
DE
11801{
11802 struct objfile *objfile = dwarf2_per_objfile->objfile;
d521ce57 11803 const gdb_byte *info_ptr, *end_ptr;
3019eac3 11804
33c5cd75
DB
11805 dwarf2_read_section (objfile, &section);
11806 info_ptr = section.buffer;
3019eac3
DE
11807
11808 if (info_ptr == NULL)
33c5cd75 11809 return;
3019eac3 11810
b4f54984 11811 if (dwarf_read_debug)
19c3d4c9
DE
11812 {
11813 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
33c5cd75
DB
11814 get_section_name (&section),
11815 get_section_file_name (&section));
19c3d4c9 11816 }
3019eac3 11817
33c5cd75 11818 end_ptr = info_ptr + section.size;
3019eac3
DE
11819 while (info_ptr < end_ptr)
11820 {
11821 struct dwarf2_per_cu_data per_cu;
33c5cd75
DB
11822 struct create_dwo_cu_data create_dwo_cu_data;
11823 struct dwo_unit *dwo_unit;
11824 void **slot;
11825 sect_offset sect_off = (sect_offset) (info_ptr - section.buffer);
3019eac3 11826
19c3d4c9
DE
11827 memset (&create_dwo_cu_data.dwo_unit, 0,
11828 sizeof (create_dwo_cu_data.dwo_unit));
3019eac3 11829 memset (&per_cu, 0, sizeof (per_cu));
e3b94546 11830 per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
3019eac3 11831 per_cu.is_debug_types = 0;
33c5cd75
DB
11832 per_cu.sect_off = sect_offset (info_ptr - section.buffer);
11833 per_cu.section = &section;
c5ed0576 11834 create_dwo_cu_data.dwo_file = &dwo_file;
33c5cd75
DB
11835
11836 init_cutu_and_read_dies_no_follow (
11837 &per_cu, &dwo_file, create_dwo_cu_reader, &create_dwo_cu_data);
11838 info_ptr += per_cu.length;
11839
11840 // If the unit could not be parsed, skip it.
11841 if (create_dwo_cu_data.dwo_unit.dwo_file == NULL)
11842 continue;
3019eac3 11843
33c5cd75
DB
11844 if (cus_htab == NULL)
11845 cus_htab = allocate_dwo_unit_table (objfile);
19c3d4c9 11846
33c5cd75
DB
11847 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
11848 *dwo_unit = create_dwo_cu_data.dwo_unit;
11849 slot = htab_find_slot (cus_htab, dwo_unit, INSERT);
11850 gdb_assert (slot != NULL);
11851 if (*slot != NULL)
19c3d4c9 11852 {
33c5cd75
DB
11853 const struct dwo_unit *dup_cu = (const struct dwo_unit *)*slot;
11854 sect_offset dup_sect_off = dup_cu->sect_off;
19c3d4c9 11855
b98664d3 11856 complaint (_("debug cu entry at offset %s is duplicate to"
9d8780f0
SM
11857 " the entry at offset %s, signature %s"),
11858 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
33c5cd75 11859 hex_string (dwo_unit->signature));
19c3d4c9 11860 }
33c5cd75 11861 *slot = (void *)dwo_unit;
3019eac3 11862 }
3019eac3
DE
11863}
11864
80626a55
DE
11865/* DWP file .debug_{cu,tu}_index section format:
11866 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
11867
d2415c6c
DE
11868 DWP Version 1:
11869
80626a55
DE
11870 Both index sections have the same format, and serve to map a 64-bit
11871 signature to a set of section numbers. Each section begins with a header,
11872 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
11873 indexes, and a pool of 32-bit section numbers. The index sections will be
11874 aligned at 8-byte boundaries in the file.
11875
d2415c6c
DE
11876 The index section header consists of:
11877
11878 V, 32 bit version number
11879 -, 32 bits unused
11880 N, 32 bit number of compilation units or type units in the index
11881 M, 32 bit number of slots in the hash table
80626a55 11882
d2415c6c 11883 Numbers are recorded using the byte order of the application binary.
80626a55 11884
d2415c6c
DE
11885 The hash table begins at offset 16 in the section, and consists of an array
11886 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
11887 order of the application binary). Unused slots in the hash table are 0.
11888 (We rely on the extreme unlikeliness of a signature being exactly 0.)
80626a55 11889
d2415c6c
DE
11890 The parallel table begins immediately after the hash table
11891 (at offset 16 + 8 * M from the beginning of the section), and consists of an
11892 array of 32-bit indexes (using the byte order of the application binary),
11893 corresponding 1-1 with slots in the hash table. Each entry in the parallel
11894 table contains a 32-bit index into the pool of section numbers. For unused
11895 hash table slots, the corresponding entry in the parallel table will be 0.
80626a55 11896
73869dc2
DE
11897 The pool of section numbers begins immediately following the hash table
11898 (at offset 16 + 12 * M from the beginning of the section). The pool of
11899 section numbers consists of an array of 32-bit words (using the byte order
11900 of the application binary). Each item in the array is indexed starting
11901 from 0. The hash table entry provides the index of the first section
11902 number in the set. Additional section numbers in the set follow, and the
11903 set is terminated by a 0 entry (section number 0 is not used in ELF).
11904
11905 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
11906 section must be the first entry in the set, and the .debug_abbrev.dwo must
11907 be the second entry. Other members of the set may follow in any order.
11908
11909 ---
11910
11911 DWP Version 2:
11912
11913 DWP Version 2 combines all the .debug_info, etc. sections into one,
11914 and the entries in the index tables are now offsets into these sections.
11915 CU offsets begin at 0. TU offsets begin at the size of the .debug_info
11916 section.
11917
11918 Index Section Contents:
11919 Header
11920 Hash Table of Signatures dwp_hash_table.hash_table
11921 Parallel Table of Indices dwp_hash_table.unit_table
11922 Table of Section Offsets dwp_hash_table.v2.{section_ids,offsets}
11923 Table of Section Sizes dwp_hash_table.v2.sizes
11924
11925 The index section header consists of:
11926
11927 V, 32 bit version number
11928 L, 32 bit number of columns in the table of section offsets
11929 N, 32 bit number of compilation units or type units in the index
11930 M, 32 bit number of slots in the hash table
11931
11932 Numbers are recorded using the byte order of the application binary.
11933
11934 The hash table has the same format as version 1.
11935 The parallel table of indices has the same format as version 1,
11936 except that the entries are origin-1 indices into the table of sections
11937 offsets and the table of section sizes.
11938
11939 The table of offsets begins immediately following the parallel table
11940 (at offset 16 + 12 * M from the beginning of the section). The table is
11941 a two-dimensional array of 32-bit words (using the byte order of the
11942 application binary), with L columns and N+1 rows, in row-major order.
11943 Each row in the array is indexed starting from 0. The first row provides
11944 a key to the remaining rows: each column in this row provides an identifier
11945 for a debug section, and the offsets in the same column of subsequent rows
11946 refer to that section. The section identifiers are:
11947
11948 DW_SECT_INFO 1 .debug_info.dwo
11949 DW_SECT_TYPES 2 .debug_types.dwo
11950 DW_SECT_ABBREV 3 .debug_abbrev.dwo
11951 DW_SECT_LINE 4 .debug_line.dwo
11952 DW_SECT_LOC 5 .debug_loc.dwo
11953 DW_SECT_STR_OFFSETS 6 .debug_str_offsets.dwo
11954 DW_SECT_MACINFO 7 .debug_macinfo.dwo
11955 DW_SECT_MACRO 8 .debug_macro.dwo
11956
11957 The offsets provided by the CU and TU index sections are the base offsets
11958 for the contributions made by each CU or TU to the corresponding section
11959 in the package file. Each CU and TU header contains an abbrev_offset
11960 field, used to find the abbreviations table for that CU or TU within the
11961 contribution to the .debug_abbrev.dwo section for that CU or TU, and should
11962 be interpreted as relative to the base offset given in the index section.
11963 Likewise, offsets into .debug_line.dwo from DW_AT_stmt_list attributes
11964 should be interpreted as relative to the base offset for .debug_line.dwo,
11965 and offsets into other debug sections obtained from DWARF attributes should
11966 also be interpreted as relative to the corresponding base offset.
11967
11968 The table of sizes begins immediately following the table of offsets.
11969 Like the table of offsets, it is a two-dimensional array of 32-bit words,
11970 with L columns and N rows, in row-major order. Each row in the array is
11971 indexed starting from 1 (row 0 is shared by the two tables).
11972
11973 ---
11974
11975 Hash table lookup is handled the same in version 1 and 2:
11976
11977 We assume that N and M will not exceed 2^32 - 1.
11978 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
11979
d2415c6c
DE
11980 Given a 64-bit compilation unit signature or a type signature S, an entry
11981 in the hash table is located as follows:
80626a55 11982
d2415c6c
DE
11983 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
11984 the low-order k bits all set to 1.
80626a55 11985
d2415c6c 11986 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
80626a55 11987
d2415c6c
DE
11988 3) If the hash table entry at index H matches the signature, use that
11989 entry. If the hash table entry at index H is unused (all zeroes),
11990 terminate the search: the signature is not present in the table.
80626a55 11991
d2415c6c 11992 4) Let H = (H + H') modulo M. Repeat at Step 3.
80626a55 11993
d2415c6c 11994 Because M > N and H' and M are relatively prime, the search is guaranteed
73869dc2 11995 to stop at an unused slot or find the match. */
80626a55
DE
11996
11997/* Create a hash table to map DWO IDs to their CU/TU entry in
11998 .debug_{info,types}.dwo in DWP_FILE.
11999 Returns NULL if there isn't one.
12000 Note: This function processes DWP files only, not DWO files. */
12001
12002static struct dwp_hash_table *
ed2dc618
SM
12003create_dwp_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
12004 struct dwp_file *dwp_file, int is_debug_types)
80626a55
DE
12005{
12006 struct objfile *objfile = dwarf2_per_objfile->objfile;
400174b1 12007 bfd *dbfd = dwp_file->dbfd.get ();
948f8e3d 12008 const gdb_byte *index_ptr, *index_end;
80626a55 12009 struct dwarf2_section_info *index;
73869dc2 12010 uint32_t version, nr_columns, nr_units, nr_slots;
80626a55
DE
12011 struct dwp_hash_table *htab;
12012
12013 if (is_debug_types)
12014 index = &dwp_file->sections.tu_index;
12015 else
12016 index = &dwp_file->sections.cu_index;
12017
12018 if (dwarf2_section_empty_p (index))
12019 return NULL;
12020 dwarf2_read_section (objfile, index);
12021
12022 index_ptr = index->buffer;
12023 index_end = index_ptr + index->size;
12024
12025 version = read_4_bytes (dbfd, index_ptr);
73869dc2
DE
12026 index_ptr += 4;
12027 if (version == 2)
12028 nr_columns = read_4_bytes (dbfd, index_ptr);
12029 else
12030 nr_columns = 0;
12031 index_ptr += 4;
80626a55
DE
12032 nr_units = read_4_bytes (dbfd, index_ptr);
12033 index_ptr += 4;
12034 nr_slots = read_4_bytes (dbfd, index_ptr);
12035 index_ptr += 4;
12036
73869dc2 12037 if (version != 1 && version != 2)
80626a55 12038 {
21aa081e 12039 error (_("Dwarf Error: unsupported DWP file version (%s)"
80626a55 12040 " [in module %s]"),
21aa081e 12041 pulongest (version), dwp_file->name);
80626a55
DE
12042 }
12043 if (nr_slots != (nr_slots & -nr_slots))
12044 {
21aa081e 12045 error (_("Dwarf Error: number of slots in DWP hash table (%s)"
80626a55 12046 " is not power of 2 [in module %s]"),
21aa081e 12047 pulongest (nr_slots), dwp_file->name);
80626a55
DE
12048 }
12049
12050 htab = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_hash_table);
73869dc2
DE
12051 htab->version = version;
12052 htab->nr_columns = nr_columns;
80626a55
DE
12053 htab->nr_units = nr_units;
12054 htab->nr_slots = nr_slots;
12055 htab->hash_table = index_ptr;
12056 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
73869dc2
DE
12057
12058 /* Exit early if the table is empty. */
12059 if (nr_slots == 0 || nr_units == 0
12060 || (version == 2 && nr_columns == 0))
12061 {
12062 /* All must be zero. */
12063 if (nr_slots != 0 || nr_units != 0
12064 || (version == 2 && nr_columns != 0))
12065 {
b98664d3 12066 complaint (_("Empty DWP but nr_slots,nr_units,nr_columns not"
73869dc2
DE
12067 " all zero [in modules %s]"),
12068 dwp_file->name);
12069 }
12070 return htab;
12071 }
12072
12073 if (version == 1)
12074 {
12075 htab->section_pool.v1.indices =
12076 htab->unit_table + sizeof (uint32_t) * nr_slots;
12077 /* It's harder to decide whether the section is too small in v1.
12078 V1 is deprecated anyway so we punt. */
12079 }
12080 else
12081 {
12082 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
12083 int *ids = htab->section_pool.v2.section_ids;
12084 /* Reverse map for error checking. */
12085 int ids_seen[DW_SECT_MAX + 1];
12086 int i;
12087
12088 if (nr_columns < 2)
12089 {
12090 error (_("Dwarf Error: bad DWP hash table, too few columns"
12091 " in section table [in module %s]"),
12092 dwp_file->name);
12093 }
12094 if (nr_columns > MAX_NR_V2_DWO_SECTIONS)
12095 {
12096 error (_("Dwarf Error: bad DWP hash table, too many columns"
12097 " in section table [in module %s]"),
12098 dwp_file->name);
12099 }
12100 memset (ids, 255, (DW_SECT_MAX + 1) * sizeof (int32_t));
12101 memset (ids_seen, 255, (DW_SECT_MAX + 1) * sizeof (int32_t));
12102 for (i = 0; i < nr_columns; ++i)
12103 {
12104 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
12105
12106 if (id < DW_SECT_MIN || id > DW_SECT_MAX)
12107 {
12108 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
12109 " in section table [in module %s]"),
12110 id, dwp_file->name);
12111 }
12112 if (ids_seen[id] != -1)
12113 {
12114 error (_("Dwarf Error: bad DWP hash table, duplicate section"
12115 " id %d in section table [in module %s]"),
12116 id, dwp_file->name);
12117 }
12118 ids_seen[id] = i;
12119 ids[i] = id;
12120 }
12121 /* Must have exactly one info or types section. */
12122 if (((ids_seen[DW_SECT_INFO] != -1)
12123 + (ids_seen[DW_SECT_TYPES] != -1))
12124 != 1)
12125 {
12126 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
12127 " DWO info/types section [in module %s]"),
12128 dwp_file->name);
12129 }
12130 /* Must have an abbrev section. */
12131 if (ids_seen[DW_SECT_ABBREV] == -1)
12132 {
12133 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
12134 " section [in module %s]"),
12135 dwp_file->name);
12136 }
12137 htab->section_pool.v2.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
12138 htab->section_pool.v2.sizes =
12139 htab->section_pool.v2.offsets + (sizeof (uint32_t)
12140 * nr_units * nr_columns);
12141 if ((htab->section_pool.v2.sizes + (sizeof (uint32_t)
12142 * nr_units * nr_columns))
12143 > index_end)
12144 {
12145 error (_("Dwarf Error: DWP index section is corrupt (too small)"
12146 " [in module %s]"),
12147 dwp_file->name);
12148 }
12149 }
80626a55
DE
12150
12151 return htab;
12152}
12153
12154/* Update SECTIONS with the data from SECTP.
12155
12156 This function is like the other "locate" section routines that are
12157 passed to bfd_map_over_sections, but in this context the sections to
73869dc2 12158 read comes from the DWP V1 hash table, not the full ELF section table.
80626a55
DE
12159
12160 The result is non-zero for success, or zero if an error was found. */
12161
12162static int
73869dc2
DE
12163locate_v1_virtual_dwo_sections (asection *sectp,
12164 struct virtual_v1_dwo_sections *sections)
80626a55
DE
12165{
12166 const struct dwop_section_names *names = &dwop_section_names;
12167
12168 if (section_is_p (sectp->name, &names->abbrev_dwo))
12169 {
12170 /* There can be only one. */
049412e3 12171 if (sections->abbrev.s.section != NULL)
80626a55 12172 return 0;
049412e3 12173 sections->abbrev.s.section = sectp;
80626a55
DE
12174 sections->abbrev.size = bfd_get_section_size (sectp);
12175 }
12176 else if (section_is_p (sectp->name, &names->info_dwo)
12177 || section_is_p (sectp->name, &names->types_dwo))
12178 {
12179 /* There can be only one. */
049412e3 12180 if (sections->info_or_types.s.section != NULL)
80626a55 12181 return 0;
049412e3 12182 sections->info_or_types.s.section = sectp;
80626a55
DE
12183 sections->info_or_types.size = bfd_get_section_size (sectp);
12184 }
12185 else if (section_is_p (sectp->name, &names->line_dwo))
12186 {
12187 /* There can be only one. */
049412e3 12188 if (sections->line.s.section != NULL)
80626a55 12189 return 0;
049412e3 12190 sections->line.s.section = sectp;
80626a55
DE
12191 sections->line.size = bfd_get_section_size (sectp);
12192 }
12193 else if (section_is_p (sectp->name, &names->loc_dwo))
12194 {
12195 /* There can be only one. */
049412e3 12196 if (sections->loc.s.section != NULL)
80626a55 12197 return 0;
049412e3 12198 sections->loc.s.section = sectp;
80626a55
DE
12199 sections->loc.size = bfd_get_section_size (sectp);
12200 }
12201 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12202 {
12203 /* There can be only one. */
049412e3 12204 if (sections->macinfo.s.section != NULL)
80626a55 12205 return 0;
049412e3 12206 sections->macinfo.s.section = sectp;
80626a55
DE
12207 sections->macinfo.size = bfd_get_section_size (sectp);
12208 }
12209 else if (section_is_p (sectp->name, &names->macro_dwo))
12210 {
12211 /* There can be only one. */
049412e3 12212 if (sections->macro.s.section != NULL)
80626a55 12213 return 0;
049412e3 12214 sections->macro.s.section = sectp;
80626a55
DE
12215 sections->macro.size = bfd_get_section_size (sectp);
12216 }
12217 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12218 {
12219 /* There can be only one. */
049412e3 12220 if (sections->str_offsets.s.section != NULL)
80626a55 12221 return 0;
049412e3 12222 sections->str_offsets.s.section = sectp;
80626a55
DE
12223 sections->str_offsets.size = bfd_get_section_size (sectp);
12224 }
12225 else
12226 {
12227 /* No other kind of section is valid. */
12228 return 0;
12229 }
12230
12231 return 1;
12232}
12233
73869dc2
DE
12234/* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
12235 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
12236 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
12237 This is for DWP version 1 files. */
80626a55
DE
12238
12239static struct dwo_unit *
ed2dc618
SM
12240create_dwo_unit_in_dwp_v1 (struct dwarf2_per_objfile *dwarf2_per_objfile,
12241 struct dwp_file *dwp_file,
73869dc2
DE
12242 uint32_t unit_index,
12243 const char *comp_dir,
12244 ULONGEST signature, int is_debug_types)
80626a55
DE
12245{
12246 struct objfile *objfile = dwarf2_per_objfile->objfile;
73869dc2
DE
12247 const struct dwp_hash_table *dwp_htab =
12248 is_debug_types ? dwp_file->tus : dwp_file->cus;
400174b1 12249 bfd *dbfd = dwp_file->dbfd.get ();
80626a55
DE
12250 const char *kind = is_debug_types ? "TU" : "CU";
12251 struct dwo_file *dwo_file;
12252 struct dwo_unit *dwo_unit;
73869dc2 12253 struct virtual_v1_dwo_sections sections;
80626a55 12254 void **dwo_file_slot;
80626a55
DE
12255 int i;
12256
73869dc2
DE
12257 gdb_assert (dwp_file->version == 1);
12258
b4f54984 12259 if (dwarf_read_debug)
80626a55 12260 {
73869dc2 12261 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V1 file: %s\n",
80626a55 12262 kind,
73869dc2 12263 pulongest (unit_index), hex_string (signature),
80626a55
DE
12264 dwp_file->name);
12265 }
12266
19ac8c2e 12267 /* Fetch the sections of this DWO unit.
80626a55
DE
12268 Put a limit on the number of sections we look for so that bad data
12269 doesn't cause us to loop forever. */
12270
73869dc2 12271#define MAX_NR_V1_DWO_SECTIONS \
80626a55
DE
12272 (1 /* .debug_info or .debug_types */ \
12273 + 1 /* .debug_abbrev */ \
12274 + 1 /* .debug_line */ \
12275 + 1 /* .debug_loc */ \
12276 + 1 /* .debug_str_offsets */ \
19ac8c2e 12277 + 1 /* .debug_macro or .debug_macinfo */ \
80626a55
DE
12278 + 1 /* trailing zero */)
12279
12280 memset (&sections, 0, sizeof (sections));
80626a55 12281
73869dc2 12282 for (i = 0; i < MAX_NR_V1_DWO_SECTIONS; ++i)
80626a55
DE
12283 {
12284 asection *sectp;
12285 uint32_t section_nr =
12286 read_4_bytes (dbfd,
73869dc2
DE
12287 dwp_htab->section_pool.v1.indices
12288 + (unit_index + i) * sizeof (uint32_t));
80626a55
DE
12289
12290 if (section_nr == 0)
12291 break;
12292 if (section_nr >= dwp_file->num_sections)
12293 {
12294 error (_("Dwarf Error: bad DWP hash table, section number too large"
12295 " [in module %s]"),
12296 dwp_file->name);
12297 }
12298
12299 sectp = dwp_file->elf_sections[section_nr];
73869dc2 12300 if (! locate_v1_virtual_dwo_sections (sectp, &sections))
80626a55
DE
12301 {
12302 error (_("Dwarf Error: bad DWP hash table, invalid section found"
12303 " [in module %s]"),
12304 dwp_file->name);
12305 }
12306 }
12307
12308 if (i < 2
a32a8923
DE
12309 || dwarf2_section_empty_p (&sections.info_or_types)
12310 || dwarf2_section_empty_p (&sections.abbrev))
80626a55
DE
12311 {
12312 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
12313 " [in module %s]"),
12314 dwp_file->name);
12315 }
73869dc2 12316 if (i == MAX_NR_V1_DWO_SECTIONS)
80626a55
DE
12317 {
12318 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
12319 " [in module %s]"),
12320 dwp_file->name);
12321 }
12322
12323 /* It's easier for the rest of the code if we fake a struct dwo_file and
12324 have dwo_unit "live" in that. At least for now.
12325
12326 The DWP file can be made up of a random collection of CUs and TUs.
c766f7ec 12327 However, for each CU + set of TUs that came from the same original DWO
57d63ce2
DE
12328 file, we can combine them back into a virtual DWO file to save space
12329 (fewer struct dwo_file objects to allocate). Remember that for really
80626a55
DE
12330 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
12331
791afaa2
TT
12332 std::string virtual_dwo_name =
12333 string_printf ("virtual-dwo/%d-%d-%d-%d",
12334 get_section_id (&sections.abbrev),
12335 get_section_id (&sections.line),
12336 get_section_id (&sections.loc),
12337 get_section_id (&sections.str_offsets));
80626a55 12338 /* Can we use an existing virtual DWO file? */
ed2dc618
SM
12339 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
12340 virtual_dwo_name.c_str (),
12341 comp_dir);
80626a55
DE
12342 /* Create one if necessary. */
12343 if (*dwo_file_slot == NULL)
12344 {
b4f54984 12345 if (dwarf_read_debug)
80626a55
DE
12346 {
12347 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
791afaa2 12348 virtual_dwo_name.c_str ());
80626a55
DE
12349 }
12350 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
224c3ddb
SM
12351 dwo_file->dwo_name
12352 = (const char *) obstack_copy0 (&objfile->objfile_obstack,
791afaa2
TT
12353 virtual_dwo_name.c_str (),
12354 virtual_dwo_name.size ());
0ac5b59e 12355 dwo_file->comp_dir = comp_dir;
80626a55
DE
12356 dwo_file->sections.abbrev = sections.abbrev;
12357 dwo_file->sections.line = sections.line;
12358 dwo_file->sections.loc = sections.loc;
12359 dwo_file->sections.macinfo = sections.macinfo;
12360 dwo_file->sections.macro = sections.macro;
12361 dwo_file->sections.str_offsets = sections.str_offsets;
12362 /* The "str" section is global to the entire DWP file. */
12363 dwo_file->sections.str = dwp_file->sections.str;
57d63ce2 12364 /* The info or types section is assigned below to dwo_unit,
80626a55
DE
12365 there's no need to record it in dwo_file.
12366 Also, we can't simply record type sections in dwo_file because
12367 we record a pointer into the vector in dwo_unit. As we collect more
12368 types we'll grow the vector and eventually have to reallocate space
57d63ce2
DE
12369 for it, invalidating all copies of pointers into the previous
12370 contents. */
80626a55
DE
12371 *dwo_file_slot = dwo_file;
12372 }
12373 else
12374 {
b4f54984 12375 if (dwarf_read_debug)
80626a55
DE
12376 {
12377 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
791afaa2 12378 virtual_dwo_name.c_str ());
80626a55 12379 }
9a3c8263 12380 dwo_file = (struct dwo_file *) *dwo_file_slot;
80626a55 12381 }
80626a55
DE
12382
12383 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
12384 dwo_unit->dwo_file = dwo_file;
12385 dwo_unit->signature = signature;
8d749320
SM
12386 dwo_unit->section =
12387 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
8a0459fd 12388 *dwo_unit->section = sections.info_or_types;
57d63ce2 12389 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
80626a55
DE
12390
12391 return dwo_unit;
12392}
12393
73869dc2
DE
12394/* Subroutine of create_dwo_unit_in_dwp_v2 to simplify it.
12395 Given a pointer to the containing section SECTION, and OFFSET,SIZE of the
12396 piece within that section used by a TU/CU, return a virtual section
12397 of just that piece. */
12398
12399static struct dwarf2_section_info
ed2dc618
SM
12400create_dwp_v2_section (struct dwarf2_per_objfile *dwarf2_per_objfile,
12401 struct dwarf2_section_info *section,
73869dc2
DE
12402 bfd_size_type offset, bfd_size_type size)
12403{
12404 struct dwarf2_section_info result;
12405 asection *sectp;
12406
12407 gdb_assert (section != NULL);
12408 gdb_assert (!section->is_virtual);
12409
12410 memset (&result, 0, sizeof (result));
12411 result.s.containing_section = section;
12412 result.is_virtual = 1;
12413
12414 if (size == 0)
12415 return result;
12416
12417 sectp = get_section_bfd_section (section);
12418
12419 /* Flag an error if the piece denoted by OFFSET,SIZE is outside the
12420 bounds of the real section. This is a pretty-rare event, so just
12421 flag an error (easier) instead of a warning and trying to cope. */
12422 if (sectp == NULL
12423 || offset + size > bfd_get_section_size (sectp))
12424 {
73869dc2
DE
12425 error (_("Dwarf Error: Bad DWP V2 section info, doesn't fit"
12426 " in section %s [in module %s]"),
12427 sectp ? bfd_section_name (abfd, sectp) : "<unknown>",
12428 objfile_name (dwarf2_per_objfile->objfile));
12429 }
12430
12431 result.virtual_offset = offset;
12432 result.size = size;
12433 return result;
12434}
12435
12436/* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
12437 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
12438 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
12439 This is for DWP version 2 files. */
12440
12441static struct dwo_unit *
ed2dc618
SM
12442create_dwo_unit_in_dwp_v2 (struct dwarf2_per_objfile *dwarf2_per_objfile,
12443 struct dwp_file *dwp_file,
73869dc2
DE
12444 uint32_t unit_index,
12445 const char *comp_dir,
12446 ULONGEST signature, int is_debug_types)
12447{
12448 struct objfile *objfile = dwarf2_per_objfile->objfile;
12449 const struct dwp_hash_table *dwp_htab =
12450 is_debug_types ? dwp_file->tus : dwp_file->cus;
400174b1 12451 bfd *dbfd = dwp_file->dbfd.get ();
73869dc2
DE
12452 const char *kind = is_debug_types ? "TU" : "CU";
12453 struct dwo_file *dwo_file;
12454 struct dwo_unit *dwo_unit;
12455 struct virtual_v2_dwo_sections sections;
12456 void **dwo_file_slot;
73869dc2
DE
12457 int i;
12458
12459 gdb_assert (dwp_file->version == 2);
12460
b4f54984 12461 if (dwarf_read_debug)
73869dc2
DE
12462 {
12463 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V2 file: %s\n",
12464 kind,
12465 pulongest (unit_index), hex_string (signature),
12466 dwp_file->name);
12467 }
12468
12469 /* Fetch the section offsets of this DWO unit. */
12470
12471 memset (&sections, 0, sizeof (sections));
73869dc2
DE
12472
12473 for (i = 0; i < dwp_htab->nr_columns; ++i)
12474 {
12475 uint32_t offset = read_4_bytes (dbfd,
12476 dwp_htab->section_pool.v2.offsets
12477 + (((unit_index - 1) * dwp_htab->nr_columns
12478 + i)
12479 * sizeof (uint32_t)));
12480 uint32_t size = read_4_bytes (dbfd,
12481 dwp_htab->section_pool.v2.sizes
12482 + (((unit_index - 1) * dwp_htab->nr_columns
12483 + i)
12484 * sizeof (uint32_t)));
12485
12486 switch (dwp_htab->section_pool.v2.section_ids[i])
12487 {
12488 case DW_SECT_INFO:
12489 case DW_SECT_TYPES:
12490 sections.info_or_types_offset = offset;
12491 sections.info_or_types_size = size;
12492 break;
12493 case DW_SECT_ABBREV:
12494 sections.abbrev_offset = offset;
12495 sections.abbrev_size = size;
12496 break;
12497 case DW_SECT_LINE:
12498 sections.line_offset = offset;
12499 sections.line_size = size;
12500 break;
12501 case DW_SECT_LOC:
12502 sections.loc_offset = offset;
12503 sections.loc_size = size;
12504 break;
12505 case DW_SECT_STR_OFFSETS:
12506 sections.str_offsets_offset = offset;
12507 sections.str_offsets_size = size;
12508 break;
12509 case DW_SECT_MACINFO:
12510 sections.macinfo_offset = offset;
12511 sections.macinfo_size = size;
12512 break;
12513 case DW_SECT_MACRO:
12514 sections.macro_offset = offset;
12515 sections.macro_size = size;
12516 break;
12517 }
12518 }
12519
12520 /* It's easier for the rest of the code if we fake a struct dwo_file and
12521 have dwo_unit "live" in that. At least for now.
12522
12523 The DWP file can be made up of a random collection of CUs and TUs.
12524 However, for each CU + set of TUs that came from the same original DWO
12525 file, we can combine them back into a virtual DWO file to save space
12526 (fewer struct dwo_file objects to allocate). Remember that for really
12527 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
12528
791afaa2
TT
12529 std::string virtual_dwo_name =
12530 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld",
12531 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
12532 (long) (sections.line_size ? sections.line_offset : 0),
12533 (long) (sections.loc_size ? sections.loc_offset : 0),
12534 (long) (sections.str_offsets_size
12535 ? sections.str_offsets_offset : 0));
73869dc2 12536 /* Can we use an existing virtual DWO file? */
ed2dc618
SM
12537 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
12538 virtual_dwo_name.c_str (),
12539 comp_dir);
73869dc2
DE
12540 /* Create one if necessary. */
12541 if (*dwo_file_slot == NULL)
12542 {
b4f54984 12543 if (dwarf_read_debug)
73869dc2
DE
12544 {
12545 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
791afaa2 12546 virtual_dwo_name.c_str ());
73869dc2
DE
12547 }
12548 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
224c3ddb
SM
12549 dwo_file->dwo_name
12550 = (const char *) obstack_copy0 (&objfile->objfile_obstack,
791afaa2
TT
12551 virtual_dwo_name.c_str (),
12552 virtual_dwo_name.size ());
73869dc2
DE
12553 dwo_file->comp_dir = comp_dir;
12554 dwo_file->sections.abbrev =
ed2dc618 12555 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.abbrev,
73869dc2
DE
12556 sections.abbrev_offset, sections.abbrev_size);
12557 dwo_file->sections.line =
ed2dc618 12558 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.line,
73869dc2
DE
12559 sections.line_offset, sections.line_size);
12560 dwo_file->sections.loc =
ed2dc618 12561 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.loc,
73869dc2
DE
12562 sections.loc_offset, sections.loc_size);
12563 dwo_file->sections.macinfo =
ed2dc618 12564 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.macinfo,
73869dc2
DE
12565 sections.macinfo_offset, sections.macinfo_size);
12566 dwo_file->sections.macro =
ed2dc618 12567 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.macro,
73869dc2
DE
12568 sections.macro_offset, sections.macro_size);
12569 dwo_file->sections.str_offsets =
ed2dc618
SM
12570 create_dwp_v2_section (dwarf2_per_objfile,
12571 &dwp_file->sections.str_offsets,
73869dc2
DE
12572 sections.str_offsets_offset,
12573 sections.str_offsets_size);
12574 /* The "str" section is global to the entire DWP file. */
12575 dwo_file->sections.str = dwp_file->sections.str;
12576 /* The info or types section is assigned below to dwo_unit,
12577 there's no need to record it in dwo_file.
12578 Also, we can't simply record type sections in dwo_file because
12579 we record a pointer into the vector in dwo_unit. As we collect more
12580 types we'll grow the vector and eventually have to reallocate space
12581 for it, invalidating all copies of pointers into the previous
12582 contents. */
12583 *dwo_file_slot = dwo_file;
12584 }
12585 else
12586 {
b4f54984 12587 if (dwarf_read_debug)
73869dc2
DE
12588 {
12589 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
791afaa2 12590 virtual_dwo_name.c_str ());
73869dc2 12591 }
9a3c8263 12592 dwo_file = (struct dwo_file *) *dwo_file_slot;
73869dc2 12593 }
73869dc2
DE
12594
12595 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
12596 dwo_unit->dwo_file = dwo_file;
12597 dwo_unit->signature = signature;
8d749320
SM
12598 dwo_unit->section =
12599 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
ed2dc618
SM
12600 *dwo_unit->section = create_dwp_v2_section (dwarf2_per_objfile,
12601 is_debug_types
73869dc2
DE
12602 ? &dwp_file->sections.types
12603 : &dwp_file->sections.info,
12604 sections.info_or_types_offset,
12605 sections.info_or_types_size);
12606 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
12607
12608 return dwo_unit;
12609}
12610
57d63ce2
DE
12611/* Lookup the DWO unit with SIGNATURE in DWP_FILE.
12612 Returns NULL if the signature isn't found. */
80626a55
DE
12613
12614static struct dwo_unit *
ed2dc618
SM
12615lookup_dwo_unit_in_dwp (struct dwarf2_per_objfile *dwarf2_per_objfile,
12616 struct dwp_file *dwp_file, const char *comp_dir,
57d63ce2 12617 ULONGEST signature, int is_debug_types)
80626a55 12618{
57d63ce2
DE
12619 const struct dwp_hash_table *dwp_htab =
12620 is_debug_types ? dwp_file->tus : dwp_file->cus;
400174b1 12621 bfd *dbfd = dwp_file->dbfd.get ();
57d63ce2 12622 uint32_t mask = dwp_htab->nr_slots - 1;
80626a55
DE
12623 uint32_t hash = signature & mask;
12624 uint32_t hash2 = ((signature >> 32) & mask) | 1;
12625 unsigned int i;
12626 void **slot;
870f88f7 12627 struct dwo_unit find_dwo_cu;
80626a55
DE
12628
12629 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
12630 find_dwo_cu.signature = signature;
19ac8c2e
DE
12631 slot = htab_find_slot (is_debug_types
12632 ? dwp_file->loaded_tus
12633 : dwp_file->loaded_cus,
12634 &find_dwo_cu, INSERT);
80626a55
DE
12635
12636 if (*slot != NULL)
9a3c8263 12637 return (struct dwo_unit *) *slot;
80626a55
DE
12638
12639 /* Use a for loop so that we don't loop forever on bad debug info. */
57d63ce2 12640 for (i = 0; i < dwp_htab->nr_slots; ++i)
80626a55
DE
12641 {
12642 ULONGEST signature_in_table;
12643
12644 signature_in_table =
57d63ce2 12645 read_8_bytes (dbfd, dwp_htab->hash_table + hash * sizeof (uint64_t));
80626a55
DE
12646 if (signature_in_table == signature)
12647 {
57d63ce2
DE
12648 uint32_t unit_index =
12649 read_4_bytes (dbfd,
12650 dwp_htab->unit_table + hash * sizeof (uint32_t));
80626a55 12651
73869dc2
DE
12652 if (dwp_file->version == 1)
12653 {
ed2dc618
SM
12654 *slot = create_dwo_unit_in_dwp_v1 (dwarf2_per_objfile,
12655 dwp_file, unit_index,
73869dc2
DE
12656 comp_dir, signature,
12657 is_debug_types);
12658 }
12659 else
12660 {
ed2dc618
SM
12661 *slot = create_dwo_unit_in_dwp_v2 (dwarf2_per_objfile,
12662 dwp_file, unit_index,
73869dc2
DE
12663 comp_dir, signature,
12664 is_debug_types);
12665 }
9a3c8263 12666 return (struct dwo_unit *) *slot;
80626a55
DE
12667 }
12668 if (signature_in_table == 0)
12669 return NULL;
12670 hash = (hash + hash2) & mask;
12671 }
12672
12673 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
12674 " [in module %s]"),
12675 dwp_file->name);
12676}
12677
ab5088bf 12678/* Subroutine of open_dwo_file,open_dwp_file to simplify them.
3019eac3
DE
12679 Open the file specified by FILE_NAME and hand it off to BFD for
12680 preliminary analysis. Return a newly initialized bfd *, which
12681 includes a canonicalized copy of FILE_NAME.
80626a55 12682 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
6ac97d4c
DE
12683 SEARCH_CWD is true if the current directory is to be searched.
12684 It will be searched before debug-file-directory.
13aaf454
DE
12685 If successful, the file is added to the bfd include table of the
12686 objfile's bfd (see gdb_bfd_record_inclusion).
6ac97d4c 12687 If unable to find/open the file, return NULL.
3019eac3
DE
12688 NOTE: This function is derived from symfile_bfd_open. */
12689
192b62ce 12690static gdb_bfd_ref_ptr
ed2dc618
SM
12691try_open_dwop_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12692 const char *file_name, int is_dwp, int search_cwd)
3019eac3 12693{
24b9144d 12694 int desc;
9c02c129
DE
12695 /* Blech. OPF_TRY_CWD_FIRST also disables searching the path list if
12696 FILE_NAME contains a '/'. So we can't use it. Instead prepend "."
12697 to debug_file_directory. */
e0cc99a6 12698 const char *search_path;
9c02c129
DE
12699 static const char dirname_separator_string[] = { DIRNAME_SEPARATOR, '\0' };
12700
e0cc99a6 12701 gdb::unique_xmalloc_ptr<char> search_path_holder;
6ac97d4c
DE
12702 if (search_cwd)
12703 {
12704 if (*debug_file_directory != '\0')
e0cc99a6
TT
12705 {
12706 search_path_holder.reset (concat (".", dirname_separator_string,
12707 debug_file_directory,
12708 (char *) NULL));
12709 search_path = search_path_holder.get ();
12710 }
6ac97d4c 12711 else
e0cc99a6 12712 search_path = ".";
6ac97d4c 12713 }
9c02c129 12714 else
e0cc99a6 12715 search_path = debug_file_directory;
3019eac3 12716
24b9144d 12717 openp_flags flags = OPF_RETURN_REALPATH;
80626a55
DE
12718 if (is_dwp)
12719 flags |= OPF_SEARCH_IN_PATH;
e0cc99a6
TT
12720
12721 gdb::unique_xmalloc_ptr<char> absolute_name;
9c02c129 12722 desc = openp (search_path, flags, file_name,
3019eac3
DE
12723 O_RDONLY | O_BINARY, &absolute_name);
12724 if (desc < 0)
12725 return NULL;
12726
e0cc99a6
TT
12727 gdb_bfd_ref_ptr sym_bfd (gdb_bfd_open (absolute_name.get (),
12728 gnutarget, desc));
9c02c129
DE
12729 if (sym_bfd == NULL)
12730 return NULL;
192b62ce 12731 bfd_set_cacheable (sym_bfd.get (), 1);
3019eac3 12732
192b62ce
TT
12733 if (!bfd_check_format (sym_bfd.get (), bfd_object))
12734 return NULL;
3019eac3 12735
13aaf454
DE
12736 /* Success. Record the bfd as having been included by the objfile's bfd.
12737 This is important because things like demangled_names_hash lives in the
12738 objfile's per_bfd space and may have references to things like symbol
12739 names that live in the DWO/DWP file's per_bfd space. PR 16426. */
192b62ce 12740 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd, sym_bfd.get ());
13aaf454 12741
3019eac3
DE
12742 return sym_bfd;
12743}
12744
ab5088bf 12745/* Try to open DWO file FILE_NAME.
3019eac3
DE
12746 COMP_DIR is the DW_AT_comp_dir attribute.
12747 The result is the bfd handle of the file.
12748 If there is a problem finding or opening the file, return NULL.
12749 Upon success, the canonicalized path of the file is stored in the bfd,
12750 same as symfile_bfd_open. */
12751
192b62ce 12752static gdb_bfd_ref_ptr
ed2dc618
SM
12753open_dwo_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12754 const char *file_name, const char *comp_dir)
3019eac3 12755{
80626a55 12756 if (IS_ABSOLUTE_PATH (file_name))
ed2dc618
SM
12757 return try_open_dwop_file (dwarf2_per_objfile, file_name,
12758 0 /*is_dwp*/, 0 /*search_cwd*/);
3019eac3
DE
12759
12760 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
12761
12762 if (comp_dir != NULL)
12763 {
b36cec19
PA
12764 char *path_to_try = concat (comp_dir, SLASH_STRING,
12765 file_name, (char *) NULL);
3019eac3
DE
12766
12767 /* NOTE: If comp_dir is a relative path, this will also try the
12768 search path, which seems useful. */
ed2dc618
SM
12769 gdb_bfd_ref_ptr abfd (try_open_dwop_file (dwarf2_per_objfile,
12770 path_to_try,
12771 0 /*is_dwp*/,
192b62ce 12772 1 /*search_cwd*/));
3019eac3
DE
12773 xfree (path_to_try);
12774 if (abfd != NULL)
12775 return abfd;
12776 }
12777
12778 /* That didn't work, try debug-file-directory, which, despite its name,
12779 is a list of paths. */
12780
12781 if (*debug_file_directory == '\0')
12782 return NULL;
12783
ed2dc618
SM
12784 return try_open_dwop_file (dwarf2_per_objfile, file_name,
12785 0 /*is_dwp*/, 1 /*search_cwd*/);
3019eac3
DE
12786}
12787
80626a55
DE
12788/* This function is mapped across the sections and remembers the offset and
12789 size of each of the DWO debugging sections we are interested in. */
12790
12791static void
12792dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp, void *dwo_sections_ptr)
12793{
9a3c8263 12794 struct dwo_sections *dwo_sections = (struct dwo_sections *) dwo_sections_ptr;
80626a55
DE
12795 const struct dwop_section_names *names = &dwop_section_names;
12796
12797 if (section_is_p (sectp->name, &names->abbrev_dwo))
12798 {
049412e3 12799 dwo_sections->abbrev.s.section = sectp;
80626a55
DE
12800 dwo_sections->abbrev.size = bfd_get_section_size (sectp);
12801 }
12802 else if (section_is_p (sectp->name, &names->info_dwo))
12803 {
049412e3 12804 dwo_sections->info.s.section = sectp;
80626a55
DE
12805 dwo_sections->info.size = bfd_get_section_size (sectp);
12806 }
12807 else if (section_is_p (sectp->name, &names->line_dwo))
12808 {
049412e3 12809 dwo_sections->line.s.section = sectp;
80626a55
DE
12810 dwo_sections->line.size = bfd_get_section_size (sectp);
12811 }
12812 else if (section_is_p (sectp->name, &names->loc_dwo))
12813 {
049412e3 12814 dwo_sections->loc.s.section = sectp;
80626a55
DE
12815 dwo_sections->loc.size = bfd_get_section_size (sectp);
12816 }
12817 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12818 {
049412e3 12819 dwo_sections->macinfo.s.section = sectp;
80626a55
DE
12820 dwo_sections->macinfo.size = bfd_get_section_size (sectp);
12821 }
12822 else if (section_is_p (sectp->name, &names->macro_dwo))
12823 {
049412e3 12824 dwo_sections->macro.s.section = sectp;
80626a55
DE
12825 dwo_sections->macro.size = bfd_get_section_size (sectp);
12826 }
12827 else if (section_is_p (sectp->name, &names->str_dwo))
12828 {
049412e3 12829 dwo_sections->str.s.section = sectp;
80626a55
DE
12830 dwo_sections->str.size = bfd_get_section_size (sectp);
12831 }
12832 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12833 {
049412e3 12834 dwo_sections->str_offsets.s.section = sectp;
80626a55
DE
12835 dwo_sections->str_offsets.size = bfd_get_section_size (sectp);
12836 }
12837 else if (section_is_p (sectp->name, &names->types_dwo))
12838 {
12839 struct dwarf2_section_info type_section;
12840
12841 memset (&type_section, 0, sizeof (type_section));
049412e3 12842 type_section.s.section = sectp;
80626a55
DE
12843 type_section.size = bfd_get_section_size (sectp);
12844 VEC_safe_push (dwarf2_section_info_def, dwo_sections->types,
12845 &type_section);
12846 }
12847}
12848
ab5088bf 12849/* Initialize the use of the DWO file specified by DWO_NAME and referenced
19c3d4c9 12850 by PER_CU. This is for the non-DWP case.
80626a55 12851 The result is NULL if DWO_NAME can't be found. */
3019eac3
DE
12852
12853static struct dwo_file *
0ac5b59e
DE
12854open_and_init_dwo_file (struct dwarf2_per_cu_data *per_cu,
12855 const char *dwo_name, const char *comp_dir)
3019eac3 12856{
ed2dc618 12857 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
3019eac3 12858 struct objfile *objfile = dwarf2_per_objfile->objfile;
3019eac3 12859
ed2dc618 12860 gdb_bfd_ref_ptr dbfd (open_dwo_file (dwarf2_per_objfile, dwo_name, comp_dir));
80626a55
DE
12861 if (dbfd == NULL)
12862 {
b4f54984 12863 if (dwarf_read_debug)
80626a55
DE
12864 fprintf_unfiltered (gdb_stdlog, "DWO file not found: %s\n", dwo_name);
12865 return NULL;
12866 }
263db9a1
TT
12867
12868 /* We use a unique pointer here, despite the obstack allocation,
12869 because a dwo_file needs some cleanup if it is abandoned. */
12870 dwo_file_up dwo_file (OBSTACK_ZALLOC (&objfile->objfile_obstack,
12871 struct dwo_file));
0ac5b59e
DE
12872 dwo_file->dwo_name = dwo_name;
12873 dwo_file->comp_dir = comp_dir;
192b62ce 12874 dwo_file->dbfd = dbfd.release ();
3019eac3 12875
192b62ce
TT
12876 bfd_map_over_sections (dwo_file->dbfd, dwarf2_locate_dwo_sections,
12877 &dwo_file->sections);
3019eac3 12878
ed2dc618
SM
12879 create_cus_hash_table (dwarf2_per_objfile, *dwo_file, dwo_file->sections.info,
12880 dwo_file->cus);
3019eac3 12881
263db9a1 12882 create_debug_types_hash_table (dwarf2_per_objfile, dwo_file.get (),
ed2dc618 12883 dwo_file->sections.types, dwo_file->tus);
3019eac3 12884
b4f54984 12885 if (dwarf_read_debug)
80626a55
DE
12886 fprintf_unfiltered (gdb_stdlog, "DWO file found: %s\n", dwo_name);
12887
263db9a1 12888 return dwo_file.release ();
3019eac3
DE
12889}
12890
80626a55 12891/* This function is mapped across the sections and remembers the offset and
73869dc2
DE
12892 size of each of the DWP debugging sections common to version 1 and 2 that
12893 we are interested in. */
3019eac3 12894
80626a55 12895static void
73869dc2
DE
12896dwarf2_locate_common_dwp_sections (bfd *abfd, asection *sectp,
12897 void *dwp_file_ptr)
3019eac3 12898{
9a3c8263 12899 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
80626a55
DE
12900 const struct dwop_section_names *names = &dwop_section_names;
12901 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
3019eac3 12902
80626a55 12903 /* Record the ELF section number for later lookup: this is what the
73869dc2 12904 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
80626a55
DE
12905 gdb_assert (elf_section_nr < dwp_file->num_sections);
12906 dwp_file->elf_sections[elf_section_nr] = sectp;
3019eac3 12907
80626a55
DE
12908 /* Look for specific sections that we need. */
12909 if (section_is_p (sectp->name, &names->str_dwo))
12910 {
049412e3 12911 dwp_file->sections.str.s.section = sectp;
80626a55
DE
12912 dwp_file->sections.str.size = bfd_get_section_size (sectp);
12913 }
12914 else if (section_is_p (sectp->name, &names->cu_index))
12915 {
049412e3 12916 dwp_file->sections.cu_index.s.section = sectp;
80626a55
DE
12917 dwp_file->sections.cu_index.size = bfd_get_section_size (sectp);
12918 }
12919 else if (section_is_p (sectp->name, &names->tu_index))
12920 {
049412e3 12921 dwp_file->sections.tu_index.s.section = sectp;
80626a55
DE
12922 dwp_file->sections.tu_index.size = bfd_get_section_size (sectp);
12923 }
12924}
3019eac3 12925
73869dc2
DE
12926/* This function is mapped across the sections and remembers the offset and
12927 size of each of the DWP version 2 debugging sections that we are interested
12928 in. This is split into a separate function because we don't know if we
12929 have version 1 or 2 until we parse the cu_index/tu_index sections. */
12930
12931static void
12932dwarf2_locate_v2_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
12933{
9a3c8263 12934 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
73869dc2
DE
12935 const struct dwop_section_names *names = &dwop_section_names;
12936 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
12937
12938 /* Record the ELF section number for later lookup: this is what the
12939 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
12940 gdb_assert (elf_section_nr < dwp_file->num_sections);
12941 dwp_file->elf_sections[elf_section_nr] = sectp;
12942
12943 /* Look for specific sections that we need. */
12944 if (section_is_p (sectp->name, &names->abbrev_dwo))
12945 {
049412e3 12946 dwp_file->sections.abbrev.s.section = sectp;
73869dc2
DE
12947 dwp_file->sections.abbrev.size = bfd_get_section_size (sectp);
12948 }
12949 else if (section_is_p (sectp->name, &names->info_dwo))
12950 {
049412e3 12951 dwp_file->sections.info.s.section = sectp;
73869dc2
DE
12952 dwp_file->sections.info.size = bfd_get_section_size (sectp);
12953 }
12954 else if (section_is_p (sectp->name, &names->line_dwo))
12955 {
049412e3 12956 dwp_file->sections.line.s.section = sectp;
73869dc2
DE
12957 dwp_file->sections.line.size = bfd_get_section_size (sectp);
12958 }
12959 else if (section_is_p (sectp->name, &names->loc_dwo))
12960 {
049412e3 12961 dwp_file->sections.loc.s.section = sectp;
73869dc2
DE
12962 dwp_file->sections.loc.size = bfd_get_section_size (sectp);
12963 }
12964 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12965 {
049412e3 12966 dwp_file->sections.macinfo.s.section = sectp;
73869dc2
DE
12967 dwp_file->sections.macinfo.size = bfd_get_section_size (sectp);
12968 }
12969 else if (section_is_p (sectp->name, &names->macro_dwo))
12970 {
049412e3 12971 dwp_file->sections.macro.s.section = sectp;
73869dc2
DE
12972 dwp_file->sections.macro.size = bfd_get_section_size (sectp);
12973 }
12974 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12975 {
049412e3 12976 dwp_file->sections.str_offsets.s.section = sectp;
73869dc2
DE
12977 dwp_file->sections.str_offsets.size = bfd_get_section_size (sectp);
12978 }
12979 else if (section_is_p (sectp->name, &names->types_dwo))
12980 {
049412e3 12981 dwp_file->sections.types.s.section = sectp;
73869dc2
DE
12982 dwp_file->sections.types.size = bfd_get_section_size (sectp);
12983 }
12984}
12985
80626a55 12986/* Hash function for dwp_file loaded CUs/TUs. */
3019eac3 12987
80626a55
DE
12988static hashval_t
12989hash_dwp_loaded_cutus (const void *item)
12990{
9a3c8263 12991 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
3019eac3 12992
80626a55
DE
12993 /* This drops the top 32 bits of the signature, but is ok for a hash. */
12994 return dwo_unit->signature;
3019eac3
DE
12995}
12996
80626a55 12997/* Equality function for dwp_file loaded CUs/TUs. */
3019eac3 12998
80626a55
DE
12999static int
13000eq_dwp_loaded_cutus (const void *a, const void *b)
3019eac3 13001{
9a3c8263
SM
13002 const struct dwo_unit *dua = (const struct dwo_unit *) a;
13003 const struct dwo_unit *dub = (const struct dwo_unit *) b;
3019eac3 13004
80626a55
DE
13005 return dua->signature == dub->signature;
13006}
3019eac3 13007
80626a55 13008/* Allocate a hash table for dwp_file loaded CUs/TUs. */
3019eac3 13009
80626a55
DE
13010static htab_t
13011allocate_dwp_loaded_cutus_table (struct objfile *objfile)
13012{
13013 return htab_create_alloc_ex (3,
13014 hash_dwp_loaded_cutus,
13015 eq_dwp_loaded_cutus,
13016 NULL,
13017 &objfile->objfile_obstack,
13018 hashtab_obstack_allocate,
13019 dummy_obstack_deallocate);
13020}
3019eac3 13021
ab5088bf
DE
13022/* Try to open DWP file FILE_NAME.
13023 The result is the bfd handle of the file.
13024 If there is a problem finding or opening the file, return NULL.
13025 Upon success, the canonicalized path of the file is stored in the bfd,
13026 same as symfile_bfd_open. */
13027
192b62ce 13028static gdb_bfd_ref_ptr
ed2dc618
SM
13029open_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
13030 const char *file_name)
ab5088bf 13031{
ed2dc618
SM
13032 gdb_bfd_ref_ptr abfd (try_open_dwop_file (dwarf2_per_objfile, file_name,
13033 1 /*is_dwp*/,
192b62ce 13034 1 /*search_cwd*/));
6ac97d4c
DE
13035 if (abfd != NULL)
13036 return abfd;
13037
13038 /* Work around upstream bug 15652.
13039 http://sourceware.org/bugzilla/show_bug.cgi?id=15652
13040 [Whether that's a "bug" is debatable, but it is getting in our way.]
13041 We have no real idea where the dwp file is, because gdb's realpath-ing
13042 of the executable's path may have discarded the needed info.
13043 [IWBN if the dwp file name was recorded in the executable, akin to
13044 .gnu_debuglink, but that doesn't exist yet.]
13045 Strip the directory from FILE_NAME and search again. */
13046 if (*debug_file_directory != '\0')
13047 {
13048 /* Don't implicitly search the current directory here.
13049 If the user wants to search "." to handle this case,
13050 it must be added to debug-file-directory. */
ed2dc618
SM
13051 return try_open_dwop_file (dwarf2_per_objfile,
13052 lbasename (file_name), 1 /*is_dwp*/,
6ac97d4c
DE
13053 0 /*search_cwd*/);
13054 }
13055
13056 return NULL;
ab5088bf
DE
13057}
13058
80626a55
DE
13059/* Initialize the use of the DWP file for the current objfile.
13060 By convention the name of the DWP file is ${objfile}.dwp.
13061 The result is NULL if it can't be found. */
a766d390 13062
400174b1 13063static std::unique_ptr<struct dwp_file>
ed2dc618 13064open_and_init_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
80626a55
DE
13065{
13066 struct objfile *objfile = dwarf2_per_objfile->objfile;
80626a55 13067
82bf32bc
JK
13068 /* Try to find first .dwp for the binary file before any symbolic links
13069 resolving. */
6c447423
DE
13070
13071 /* If the objfile is a debug file, find the name of the real binary
13072 file and get the name of dwp file from there. */
d721ba37 13073 std::string dwp_name;
6c447423
DE
13074 if (objfile->separate_debug_objfile_backlink != NULL)
13075 {
13076 struct objfile *backlink = objfile->separate_debug_objfile_backlink;
13077 const char *backlink_basename = lbasename (backlink->original_name);
6c447423 13078
d721ba37 13079 dwp_name = ldirname (objfile->original_name) + SLASH_STRING + backlink_basename;
6c447423
DE
13080 }
13081 else
d721ba37
PA
13082 dwp_name = objfile->original_name;
13083
13084 dwp_name += ".dwp";
80626a55 13085
ed2dc618 13086 gdb_bfd_ref_ptr dbfd (open_dwp_file (dwarf2_per_objfile, dwp_name.c_str ()));
82bf32bc
JK
13087 if (dbfd == NULL
13088 && strcmp (objfile->original_name, objfile_name (objfile)) != 0)
13089 {
13090 /* Try to find .dwp for the binary file after gdb_realpath resolving. */
d721ba37
PA
13091 dwp_name = objfile_name (objfile);
13092 dwp_name += ".dwp";
ed2dc618 13093 dbfd = open_dwp_file (dwarf2_per_objfile, dwp_name.c_str ());
82bf32bc
JK
13094 }
13095
80626a55
DE
13096 if (dbfd == NULL)
13097 {
b4f54984 13098 if (dwarf_read_debug)
d721ba37 13099 fprintf_unfiltered (gdb_stdlog, "DWP file not found: %s\n", dwp_name.c_str ());
400174b1 13100 return std::unique_ptr<dwp_file> ();
3019eac3 13101 }
400174b1
TT
13102
13103 const char *name = bfd_get_filename (dbfd.get ());
13104 std::unique_ptr<struct dwp_file> dwp_file
13105 (new struct dwp_file (name, std::move (dbfd)));
c906108c 13106
80626a55 13107 /* +1: section 0 is unused */
192b62ce 13108 dwp_file->num_sections = bfd_count_sections (dwp_file->dbfd) + 1;
80626a55
DE
13109 dwp_file->elf_sections =
13110 OBSTACK_CALLOC (&objfile->objfile_obstack,
13111 dwp_file->num_sections, asection *);
13112
400174b1
TT
13113 bfd_map_over_sections (dwp_file->dbfd.get (),
13114 dwarf2_locate_common_dwp_sections,
13115 dwp_file.get ());
80626a55 13116
400174b1
TT
13117 dwp_file->cus = create_dwp_hash_table (dwarf2_per_objfile, dwp_file.get (),
13118 0);
80626a55 13119
400174b1
TT
13120 dwp_file->tus = create_dwp_hash_table (dwarf2_per_objfile, dwp_file.get (),
13121 1);
80626a55 13122
73869dc2 13123 /* The DWP file version is stored in the hash table. Oh well. */
08302ed2
DE
13124 if (dwp_file->cus && dwp_file->tus
13125 && dwp_file->cus->version != dwp_file->tus->version)
73869dc2
DE
13126 {
13127 /* Technically speaking, we should try to limp along, but this is
fbcbc3fd 13128 pretty bizarre. We use pulongest here because that's the established
4d65956b 13129 portability solution (e.g, we cannot use %u for uint32_t). */
fbcbc3fd
DE
13130 error (_("Dwarf Error: DWP file CU version %s doesn't match"
13131 " TU version %s [in DWP file %s]"),
13132 pulongest (dwp_file->cus->version),
d721ba37 13133 pulongest (dwp_file->tus->version), dwp_name.c_str ());
73869dc2 13134 }
08302ed2
DE
13135
13136 if (dwp_file->cus)
13137 dwp_file->version = dwp_file->cus->version;
13138 else if (dwp_file->tus)
13139 dwp_file->version = dwp_file->tus->version;
13140 else
13141 dwp_file->version = 2;
73869dc2
DE
13142
13143 if (dwp_file->version == 2)
400174b1
TT
13144 bfd_map_over_sections (dwp_file->dbfd.get (),
13145 dwarf2_locate_v2_dwp_sections,
13146 dwp_file.get ());
73869dc2 13147
19ac8c2e
DE
13148 dwp_file->loaded_cus = allocate_dwp_loaded_cutus_table (objfile);
13149 dwp_file->loaded_tus = allocate_dwp_loaded_cutus_table (objfile);
80626a55 13150
b4f54984 13151 if (dwarf_read_debug)
80626a55
DE
13152 {
13153 fprintf_unfiltered (gdb_stdlog, "DWP file found: %s\n", dwp_file->name);
13154 fprintf_unfiltered (gdb_stdlog,
21aa081e
PA
13155 " %s CUs, %s TUs\n",
13156 pulongest (dwp_file->cus ? dwp_file->cus->nr_units : 0),
13157 pulongest (dwp_file->tus ? dwp_file->tus->nr_units : 0));
80626a55
DE
13158 }
13159
13160 return dwp_file;
3019eac3 13161}
c906108c 13162
ab5088bf
DE
13163/* Wrapper around open_and_init_dwp_file, only open it once. */
13164
13165static struct dwp_file *
ed2dc618 13166get_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
ab5088bf
DE
13167{
13168 if (! dwarf2_per_objfile->dwp_checked)
13169 {
ed2dc618
SM
13170 dwarf2_per_objfile->dwp_file
13171 = open_and_init_dwp_file (dwarf2_per_objfile);
ab5088bf
DE
13172 dwarf2_per_objfile->dwp_checked = 1;
13173 }
400174b1 13174 return dwarf2_per_objfile->dwp_file.get ();
ab5088bf
DE
13175}
13176
80626a55
DE
13177/* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
13178 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
13179 or in the DWP file for the objfile, referenced by THIS_UNIT.
3019eac3 13180 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
80626a55
DE
13181 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
13182
13183 This is called, for example, when wanting to read a variable with a
13184 complex location. Therefore we don't want to do file i/o for every call.
13185 Therefore we don't want to look for a DWO file on every call.
13186 Therefore we first see if we've already seen SIGNATURE in a DWP file,
13187 then we check if we've already seen DWO_NAME, and only THEN do we check
13188 for a DWO file.
13189
1c658ad5 13190 The result is a pointer to the dwo_unit object or NULL if we didn't find it
80626a55 13191 (dwo_id mismatch or couldn't find the DWO/DWP file). */
debd256d 13192
3019eac3 13193static struct dwo_unit *
80626a55
DE
13194lookup_dwo_cutu (struct dwarf2_per_cu_data *this_unit,
13195 const char *dwo_name, const char *comp_dir,
13196 ULONGEST signature, int is_debug_types)
3019eac3 13197{
ed2dc618 13198 struct dwarf2_per_objfile *dwarf2_per_objfile = this_unit->dwarf2_per_objfile;
3019eac3 13199 struct objfile *objfile = dwarf2_per_objfile->objfile;
80626a55
DE
13200 const char *kind = is_debug_types ? "TU" : "CU";
13201 void **dwo_file_slot;
3019eac3 13202 struct dwo_file *dwo_file;
80626a55 13203 struct dwp_file *dwp_file;
cb1df416 13204
6a506a2d
DE
13205 /* First see if there's a DWP file.
13206 If we have a DWP file but didn't find the DWO inside it, don't
13207 look for the original DWO file. It makes gdb behave differently
13208 depending on whether one is debugging in the build tree. */
cf2c3c16 13209
ed2dc618 13210 dwp_file = get_dwp_file (dwarf2_per_objfile);
80626a55 13211 if (dwp_file != NULL)
cf2c3c16 13212 {
80626a55
DE
13213 const struct dwp_hash_table *dwp_htab =
13214 is_debug_types ? dwp_file->tus : dwp_file->cus;
13215
13216 if (dwp_htab != NULL)
13217 {
13218 struct dwo_unit *dwo_cutu =
ed2dc618 13219 lookup_dwo_unit_in_dwp (dwarf2_per_objfile, dwp_file, comp_dir,
57d63ce2 13220 signature, is_debug_types);
80626a55
DE
13221
13222 if (dwo_cutu != NULL)
13223 {
b4f54984 13224 if (dwarf_read_debug)
80626a55
DE
13225 {
13226 fprintf_unfiltered (gdb_stdlog,
13227 "Virtual DWO %s %s found: @%s\n",
13228 kind, hex_string (signature),
13229 host_address_to_string (dwo_cutu));
13230 }
13231 return dwo_cutu;
13232 }
13233 }
13234 }
6a506a2d 13235 else
80626a55 13236 {
6a506a2d 13237 /* No DWP file, look for the DWO file. */
80626a55 13238
ed2dc618
SM
13239 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
13240 dwo_name, comp_dir);
6a506a2d 13241 if (*dwo_file_slot == NULL)
80626a55 13242 {
6a506a2d
DE
13243 /* Read in the file and build a table of the CUs/TUs it contains. */
13244 *dwo_file_slot = open_and_init_dwo_file (this_unit, dwo_name, comp_dir);
19c3d4c9 13245 }
6a506a2d 13246 /* NOTE: This will be NULL if unable to open the file. */
9a3c8263 13247 dwo_file = (struct dwo_file *) *dwo_file_slot;
3019eac3 13248
6a506a2d 13249 if (dwo_file != NULL)
19c3d4c9 13250 {
6a506a2d
DE
13251 struct dwo_unit *dwo_cutu = NULL;
13252
13253 if (is_debug_types && dwo_file->tus)
13254 {
13255 struct dwo_unit find_dwo_cutu;
13256
13257 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
13258 find_dwo_cutu.signature = signature;
9a3c8263
SM
13259 dwo_cutu
13260 = (struct dwo_unit *) htab_find (dwo_file->tus, &find_dwo_cutu);
6a506a2d 13261 }
33c5cd75 13262 else if (!is_debug_types && dwo_file->cus)
80626a55 13263 {
33c5cd75
DB
13264 struct dwo_unit find_dwo_cutu;
13265
13266 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
13267 find_dwo_cutu.signature = signature;
13268 dwo_cutu = (struct dwo_unit *)htab_find (dwo_file->cus,
13269 &find_dwo_cutu);
6a506a2d
DE
13270 }
13271
13272 if (dwo_cutu != NULL)
13273 {
b4f54984 13274 if (dwarf_read_debug)
6a506a2d
DE
13275 {
13276 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) found: @%s\n",
13277 kind, dwo_name, hex_string (signature),
13278 host_address_to_string (dwo_cutu));
13279 }
13280 return dwo_cutu;
80626a55
DE
13281 }
13282 }
2e276125 13283 }
9cdd5dbd 13284
80626a55
DE
13285 /* We didn't find it. This could mean a dwo_id mismatch, or
13286 someone deleted the DWO/DWP file, or the search path isn't set up
13287 correctly to find the file. */
13288
b4f54984 13289 if (dwarf_read_debug)
80626a55
DE
13290 {
13291 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) not found\n",
13292 kind, dwo_name, hex_string (signature));
13293 }
3019eac3 13294
6656a72d
DE
13295 /* This is a warning and not a complaint because it can be caused by
13296 pilot error (e.g., user accidentally deleting the DWO). */
43942612
DE
13297 {
13298 /* Print the name of the DWP file if we looked there, helps the user
13299 better diagnose the problem. */
791afaa2 13300 std::string dwp_text;
43942612
DE
13301
13302 if (dwp_file != NULL)
791afaa2
TT
13303 dwp_text = string_printf (" [in DWP file %s]",
13304 lbasename (dwp_file->name));
43942612 13305
9d8780f0 13306 warning (_("Could not find DWO %s %s(%s)%s referenced by %s at offset %s"
43942612
DE
13307 " [in module %s]"),
13308 kind, dwo_name, hex_string (signature),
791afaa2 13309 dwp_text.c_str (),
43942612 13310 this_unit->is_debug_types ? "TU" : "CU",
9d8780f0 13311 sect_offset_str (this_unit->sect_off), objfile_name (objfile));
43942612 13312 }
3019eac3 13313 return NULL;
5fb290d7
DJ
13314}
13315
80626a55
DE
13316/* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
13317 See lookup_dwo_cutu_unit for details. */
13318
13319static struct dwo_unit *
13320lookup_dwo_comp_unit (struct dwarf2_per_cu_data *this_cu,
13321 const char *dwo_name, const char *comp_dir,
13322 ULONGEST signature)
13323{
13324 return lookup_dwo_cutu (this_cu, dwo_name, comp_dir, signature, 0);
13325}
13326
13327/* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
13328 See lookup_dwo_cutu_unit for details. */
13329
13330static struct dwo_unit *
13331lookup_dwo_type_unit (struct signatured_type *this_tu,
13332 const char *dwo_name, const char *comp_dir)
13333{
13334 return lookup_dwo_cutu (&this_tu->per_cu, dwo_name, comp_dir, this_tu->signature, 1);
13335}
13336
89e63ee4
DE
13337/* Traversal function for queue_and_load_all_dwo_tus. */
13338
13339static int
13340queue_and_load_dwo_tu (void **slot, void *info)
13341{
13342 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
13343 struct dwarf2_per_cu_data *per_cu = (struct dwarf2_per_cu_data *) info;
13344 ULONGEST signature = dwo_unit->signature;
13345 struct signatured_type *sig_type =
13346 lookup_dwo_signatured_type (per_cu->cu, signature);
13347
13348 if (sig_type != NULL)
13349 {
13350 struct dwarf2_per_cu_data *sig_cu = &sig_type->per_cu;
13351
13352 /* We pass NULL for DEPENDENT_CU because we don't yet know if there's
13353 a real dependency of PER_CU on SIG_TYPE. That is detected later
13354 while processing PER_CU. */
13355 if (maybe_queue_comp_unit (NULL, sig_cu, per_cu->cu->language))
13356 load_full_type_unit (sig_cu);
13357 VEC_safe_push (dwarf2_per_cu_ptr, per_cu->imported_symtabs, sig_cu);
13358 }
13359
13360 return 1;
13361}
13362
13363/* Queue all TUs contained in the DWO of PER_CU to be read in.
13364 The DWO may have the only definition of the type, though it may not be
13365 referenced anywhere in PER_CU. Thus we have to load *all* its TUs.
13366 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
13367
13368static void
13369queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *per_cu)
13370{
13371 struct dwo_unit *dwo_unit;
13372 struct dwo_file *dwo_file;
13373
13374 gdb_assert (!per_cu->is_debug_types);
ed2dc618 13375 gdb_assert (get_dwp_file (per_cu->dwarf2_per_objfile) == NULL);
89e63ee4
DE
13376 gdb_assert (per_cu->cu != NULL);
13377
13378 dwo_unit = per_cu->cu->dwo_unit;
13379 gdb_assert (dwo_unit != NULL);
13380
13381 dwo_file = dwo_unit->dwo_file;
13382 if (dwo_file->tus != NULL)
13383 htab_traverse_noresize (dwo_file->tus, queue_and_load_dwo_tu, per_cu);
13384}
13385
3019eac3 13386/* Free all resources associated with DWO_FILE.
5dafb3d1 13387 Close the DWO file and munmap the sections. */
348e048f
DE
13388
13389static void
5dafb3d1 13390free_dwo_file (struct dwo_file *dwo_file)
348e048f 13391{
5c6fa7ab 13392 /* Note: dbfd is NULL for virtual DWO files. */
80626a55 13393 gdb_bfd_unref (dwo_file->dbfd);
348e048f 13394
3019eac3
DE
13395 VEC_free (dwarf2_section_info_def, dwo_file->sections.types);
13396}
348e048f 13397
3019eac3 13398/* Traversal function for free_dwo_files. */
2ab95328 13399
3019eac3
DE
13400static int
13401free_dwo_file_from_slot (void **slot, void *info)
13402{
13403 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
348e048f 13404
5dafb3d1 13405 free_dwo_file (dwo_file);
348e048f 13406
3019eac3
DE
13407 return 1;
13408}
348e048f 13409
3019eac3 13410/* Free all resources associated with DWO_FILES. */
348e048f 13411
3019eac3
DE
13412static void
13413free_dwo_files (htab_t dwo_files, struct objfile *objfile)
13414{
13415 htab_traverse_noresize (dwo_files, free_dwo_file_from_slot, objfile);
348e048f 13416}
3019eac3
DE
13417\f
13418/* Read in various DIEs. */
348e048f 13419
d389af10 13420/* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
3e43a32a
MS
13421 Inherit only the children of the DW_AT_abstract_origin DIE not being
13422 already referenced by DW_AT_abstract_origin from the children of the
13423 current DIE. */
d389af10
JK
13424
13425static void
13426inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
13427{
13428 struct die_info *child_die;
791afaa2 13429 sect_offset *offsetp;
d389af10
JK
13430 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
13431 struct die_info *origin_die;
13432 /* Iterator of the ORIGIN_DIE children. */
13433 struct die_info *origin_child_die;
d389af10 13434 struct attribute *attr;
cd02d79d
PA
13435 struct dwarf2_cu *origin_cu;
13436 struct pending **origin_previous_list_in_scope;
d389af10
JK
13437
13438 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13439 if (!attr)
13440 return;
13441
cd02d79d
PA
13442 /* Note that following die references may follow to a die in a
13443 different cu. */
13444
13445 origin_cu = cu;
13446 origin_die = follow_die_ref (die, attr, &origin_cu);
13447
13448 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
13449 symbols in. */
13450 origin_previous_list_in_scope = origin_cu->list_in_scope;
13451 origin_cu->list_in_scope = cu->list_in_scope;
13452
edb3359d
DJ
13453 if (die->tag != origin_die->tag
13454 && !(die->tag == DW_TAG_inlined_subroutine
13455 && origin_die->tag == DW_TAG_subprogram))
b98664d3 13456 complaint (_("DIE %s and its abstract origin %s have different tags"),
9d8780f0
SM
13457 sect_offset_str (die->sect_off),
13458 sect_offset_str (origin_die->sect_off));
d389af10 13459
791afaa2 13460 std::vector<sect_offset> offsets;
d389af10 13461
3ea89b92
PMR
13462 for (child_die = die->child;
13463 child_die && child_die->tag;
13464 child_die = sibling_die (child_die))
13465 {
13466 struct die_info *child_origin_die;
13467 struct dwarf2_cu *child_origin_cu;
13468
13469 /* We are trying to process concrete instance entries:
216f72a1 13470 DW_TAG_call_site DIEs indeed have a DW_AT_abstract_origin tag, but
3ea89b92
PMR
13471 it's not relevant to our analysis here. i.e. detecting DIEs that are
13472 present in the abstract instance but not referenced in the concrete
13473 one. */
216f72a1
JK
13474 if (child_die->tag == DW_TAG_call_site
13475 || child_die->tag == DW_TAG_GNU_call_site)
3ea89b92
PMR
13476 continue;
13477
c38f313d
DJ
13478 /* For each CHILD_DIE, find the corresponding child of
13479 ORIGIN_DIE. If there is more than one layer of
13480 DW_AT_abstract_origin, follow them all; there shouldn't be,
13481 but GCC versions at least through 4.4 generate this (GCC PR
13482 40573). */
3ea89b92
PMR
13483 child_origin_die = child_die;
13484 child_origin_cu = cu;
c38f313d
DJ
13485 while (1)
13486 {
cd02d79d
PA
13487 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
13488 child_origin_cu);
c38f313d
DJ
13489 if (attr == NULL)
13490 break;
cd02d79d
PA
13491 child_origin_die = follow_die_ref (child_origin_die, attr,
13492 &child_origin_cu);
c38f313d
DJ
13493 }
13494
d389af10
JK
13495 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
13496 counterpart may exist. */
c38f313d 13497 if (child_origin_die != child_die)
d389af10 13498 {
edb3359d
DJ
13499 if (child_die->tag != child_origin_die->tag
13500 && !(child_die->tag == DW_TAG_inlined_subroutine
13501 && child_origin_die->tag == DW_TAG_subprogram))
b98664d3 13502 complaint (_("Child DIE %s and its abstract origin %s have "
9c541725 13503 "different tags"),
9d8780f0
SM
13504 sect_offset_str (child_die->sect_off),
13505 sect_offset_str (child_origin_die->sect_off));
c38f313d 13506 if (child_origin_die->parent != origin_die)
b98664d3 13507 complaint (_("Child DIE %s and its abstract origin %s have "
9c541725 13508 "different parents"),
9d8780f0
SM
13509 sect_offset_str (child_die->sect_off),
13510 sect_offset_str (child_origin_die->sect_off));
c38f313d 13511 else
791afaa2 13512 offsets.push_back (child_origin_die->sect_off);
d389af10 13513 }
d389af10 13514 }
791afaa2
TT
13515 std::sort (offsets.begin (), offsets.end ());
13516 sect_offset *offsets_end = offsets.data () + offsets.size ();
13517 for (offsetp = offsets.data () + 1; offsetp < offsets_end; offsetp++)
9c541725 13518 if (offsetp[-1] == *offsetp)
b98664d3 13519 complaint (_("Multiple children of DIE %s refer "
9d8780f0
SM
13520 "to DIE %s as their abstract origin"),
13521 sect_offset_str (die->sect_off), sect_offset_str (*offsetp));
d389af10 13522
791afaa2 13523 offsetp = offsets.data ();
d389af10
JK
13524 origin_child_die = origin_die->child;
13525 while (origin_child_die && origin_child_die->tag)
13526 {
13527 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
b64f50a1 13528 while (offsetp < offsets_end
9c541725 13529 && *offsetp < origin_child_die->sect_off)
d389af10 13530 offsetp++;
b64f50a1 13531 if (offsetp >= offsets_end
9c541725 13532 || *offsetp > origin_child_die->sect_off)
d389af10 13533 {
adde2bff
DE
13534 /* Found that ORIGIN_CHILD_DIE is really not referenced.
13535 Check whether we're already processing ORIGIN_CHILD_DIE.
13536 This can happen with mutually referenced abstract_origins.
13537 PR 16581. */
13538 if (!origin_child_die->in_process)
13539 process_die (origin_child_die, origin_cu);
d389af10
JK
13540 }
13541 origin_child_die = sibling_die (origin_child_die);
13542 }
cd02d79d 13543 origin_cu->list_in_scope = origin_previous_list_in_scope;
d389af10
JK
13544}
13545
c906108c 13546static void
e7c27a73 13547read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 13548{
518817b3 13549 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
3e29f34a 13550 struct gdbarch *gdbarch = get_objfile_arch (objfile);
fe978cb0 13551 struct context_stack *newobj;
c906108c
SS
13552 CORE_ADDR lowpc;
13553 CORE_ADDR highpc;
13554 struct die_info *child_die;
edb3359d 13555 struct attribute *attr, *call_line, *call_file;
15d034d0 13556 const char *name;
e142c38c 13557 CORE_ADDR baseaddr;
801e3a5b 13558 struct block *block;
edb3359d 13559 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
2f4732b0 13560 std::vector<struct symbol *> template_args;
34eaf542 13561 struct template_symbol *templ_func = NULL;
edb3359d
DJ
13562
13563 if (inlined_func)
13564 {
13565 /* If we do not have call site information, we can't show the
13566 caller of this inlined function. That's too confusing, so
13567 only use the scope for local variables. */
13568 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
13569 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
13570 if (call_line == NULL || call_file == NULL)
13571 {
13572 read_lexical_block_scope (die, cu);
13573 return;
13574 }
13575 }
c906108c 13576
e142c38c
DJ
13577 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
13578
94af9270 13579 name = dwarf2_name (die, cu);
c906108c 13580
e8d05480
JB
13581 /* Ignore functions with missing or empty names. These are actually
13582 illegal according to the DWARF standard. */
13583 if (name == NULL)
13584 {
b98664d3 13585 complaint (_("missing name for subprogram DIE at %s"),
9d8780f0 13586 sect_offset_str (die->sect_off));
e8d05480
JB
13587 return;
13588 }
13589
13590 /* Ignore functions with missing or invalid low and high pc attributes. */
3a2b436a 13591 if (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL)
e385593e 13592 <= PC_BOUNDS_INVALID)
e8d05480 13593 {
ae4d0c03
PM
13594 attr = dwarf2_attr (die, DW_AT_external, cu);
13595 if (!attr || !DW_UNSND (attr))
b98664d3 13596 complaint (_("cannot get low and high bounds "
9d8780f0
SM
13597 "for subprogram DIE at %s"),
13598 sect_offset_str (die->sect_off));
e8d05480
JB
13599 return;
13600 }
c906108c 13601
3e29f34a
MR
13602 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13603 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
c906108c 13604
34eaf542
TT
13605 /* If we have any template arguments, then we must allocate a
13606 different sort of symbol. */
13607 for (child_die = die->child; child_die; child_die = sibling_die (child_die))
13608 {
13609 if (child_die->tag == DW_TAG_template_type_param
13610 || child_die->tag == DW_TAG_template_value_param)
13611 {
e623cf5d 13612 templ_func = allocate_template_symbol (objfile);
cf724bc9 13613 templ_func->subclass = SYMBOL_TEMPLATE;
34eaf542
TT
13614 break;
13615 }
13616 }
13617
fe978cb0 13618 newobj = push_context (0, lowpc);
5e2db402
TT
13619 newobj->name = new_symbol (die, read_type_die (die, cu), cu,
13620 (struct symbol *) templ_func);
4c2df51b 13621
4cecd739
DJ
13622 /* If there is a location expression for DW_AT_frame_base, record
13623 it. */
e142c38c 13624 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
4c2df51b 13625 if (attr)
fe978cb0 13626 dwarf2_symbol_mark_computed (attr, newobj->name, cu, 1);
4c2df51b 13627
63e43d3a
PMR
13628 /* If there is a location for the static link, record it. */
13629 newobj->static_link = NULL;
13630 attr = dwarf2_attr (die, DW_AT_static_link, cu);
13631 if (attr)
13632 {
224c3ddb
SM
13633 newobj->static_link
13634 = XOBNEW (&objfile->objfile_obstack, struct dynamic_prop);
63e43d3a
PMR
13635 attr_to_dynamic_prop (attr, die, cu, newobj->static_link);
13636 }
13637
e142c38c 13638 cu->list_in_scope = &local_symbols;
c906108c 13639
639d11d3 13640 if (die->child != NULL)
c906108c 13641 {
639d11d3 13642 child_die = die->child;
c906108c
SS
13643 while (child_die && child_die->tag)
13644 {
34eaf542
TT
13645 if (child_die->tag == DW_TAG_template_type_param
13646 || child_die->tag == DW_TAG_template_value_param)
13647 {
13648 struct symbol *arg = new_symbol (child_die, NULL, cu);
13649
f1078f66 13650 if (arg != NULL)
2f4732b0 13651 template_args.push_back (arg);
34eaf542
TT
13652 }
13653 else
13654 process_die (child_die, cu);
c906108c
SS
13655 child_die = sibling_die (child_die);
13656 }
13657 }
13658
d389af10
JK
13659 inherit_abstract_dies (die, cu);
13660
4a811a97
UW
13661 /* If we have a DW_AT_specification, we might need to import using
13662 directives from the context of the specification DIE. See the
13663 comment in determine_prefix. */
13664 if (cu->language == language_cplus
13665 && dwarf2_attr (die, DW_AT_specification, cu))
13666 {
13667 struct dwarf2_cu *spec_cu = cu;
13668 struct die_info *spec_die = die_specification (die, &spec_cu);
13669
13670 while (spec_die)
13671 {
13672 child_die = spec_die->child;
13673 while (child_die && child_die->tag)
13674 {
13675 if (child_die->tag == DW_TAG_imported_module)
13676 process_die (child_die, spec_cu);
13677 child_die = sibling_die (child_die);
13678 }
13679
13680 /* In some cases, GCC generates specification DIEs that
13681 themselves contain DW_AT_specification attributes. */
13682 spec_die = die_specification (spec_die, &spec_cu);
13683 }
13684 }
13685
a60f3166 13686 struct context_stack cstk = pop_context ();
c906108c 13687 /* Make a block for the local symbols within. */
c233e9c6 13688 block = finish_block (cstk.name, cstk.old_blocks,
a60f3166 13689 cstk.static_link, lowpc, highpc);
801e3a5b 13690
df8a16a1 13691 /* For C++, set the block's scope. */
45280282
IB
13692 if ((cu->language == language_cplus
13693 || cu->language == language_fortran
c44af4eb
TT
13694 || cu->language == language_d
13695 || cu->language == language_rust)
4d4ec4e5 13696 && cu->processing_has_namespace_info)
195a3f6c
TT
13697 block_set_scope (block, determine_prefix (die, cu),
13698 &objfile->objfile_obstack);
df8a16a1 13699
801e3a5b
JB
13700 /* If we have address ranges, record them. */
13701 dwarf2_record_block_ranges (die, block, baseaddr, cu);
6e70227d 13702
a60f3166 13703 gdbarch_make_symbol_special (gdbarch, cstk.name, objfile);
3e29f34a 13704
34eaf542 13705 /* Attach template arguments to function. */
2f4732b0 13706 if (!template_args.empty ())
34eaf542
TT
13707 {
13708 gdb_assert (templ_func != NULL);
13709
2f4732b0 13710 templ_func->n_template_arguments = template_args.size ();
34eaf542 13711 templ_func->template_arguments
8d749320
SM
13712 = XOBNEWVEC (&objfile->objfile_obstack, struct symbol *,
13713 templ_func->n_template_arguments);
34eaf542 13714 memcpy (templ_func->template_arguments,
2f4732b0 13715 template_args.data (),
34eaf542 13716 (templ_func->n_template_arguments * sizeof (struct symbol *)));
34eaf542
TT
13717 }
13718
208d8187
JB
13719 /* In C++, we can have functions nested inside functions (e.g., when
13720 a function declares a class that has methods). This means that
13721 when we finish processing a function scope, we may need to go
13722 back to building a containing block's symbol lists. */
a60f3166
TT
13723 local_symbols = cstk.locals;
13724 set_local_using_directives (cstk.local_using_directives);
208d8187 13725
921e78cf
JB
13726 /* If we've finished processing a top-level function, subsequent
13727 symbols go in the file symbol list. */
13728 if (outermost_context_p ())
e142c38c 13729 cu->list_in_scope = &file_symbols;
c906108c
SS
13730}
13731
13732/* Process all the DIES contained within a lexical block scope. Start
13733 a new scope, process the dies, and then close the scope. */
13734
13735static void
e7c27a73 13736read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 13737{
518817b3 13738 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
3e29f34a 13739 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
13740 CORE_ADDR lowpc, highpc;
13741 struct die_info *child_die;
e142c38c
DJ
13742 CORE_ADDR baseaddr;
13743
13744 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c
SS
13745
13746 /* Ignore blocks with missing or invalid low and high pc attributes. */
af34e669
DJ
13747 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
13748 as multiple lexical blocks? Handling children in a sane way would
6e70227d 13749 be nasty. Might be easier to properly extend generic blocks to
af34e669 13750 describe ranges. */
e385593e
JK
13751 switch (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
13752 {
13753 case PC_BOUNDS_NOT_PRESENT:
13754 /* DW_TAG_lexical_block has no attributes, process its children as if
13755 there was no wrapping by that DW_TAG_lexical_block.
13756 GCC does no longer produces such DWARF since GCC r224161. */
13757 for (child_die = die->child;
13758 child_die != NULL && child_die->tag;
13759 child_die = sibling_die (child_die))
13760 process_die (child_die, cu);
13761 return;
13762 case PC_BOUNDS_INVALID:
13763 return;
13764 }
3e29f34a
MR
13765 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13766 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
c906108c
SS
13767
13768 push_context (0, lowpc);
639d11d3 13769 if (die->child != NULL)
c906108c 13770 {
639d11d3 13771 child_die = die->child;
c906108c
SS
13772 while (child_die && child_die->tag)
13773 {
e7c27a73 13774 process_die (child_die, cu);
c906108c
SS
13775 child_die = sibling_die (child_die);
13776 }
13777 }
3ea89b92 13778 inherit_abstract_dies (die, cu);
a60f3166 13779 struct context_stack cstk = pop_context ();
c906108c 13780
6cccc9a8 13781 if (local_symbols != NULL || (*get_local_using_directives ()) != NULL)
c906108c 13782 {
801e3a5b 13783 struct block *block
c233e9c6 13784 = finish_block (0, cstk.old_blocks, NULL,
a60f3166 13785 cstk.start_addr, highpc);
801e3a5b
JB
13786
13787 /* Note that recording ranges after traversing children, as we
13788 do here, means that recording a parent's ranges entails
13789 walking across all its children's ranges as they appear in
13790 the address map, which is quadratic behavior.
13791
13792 It would be nicer to record the parent's ranges before
13793 traversing its children, simply overriding whatever you find
13794 there. But since we don't even decide whether to create a
13795 block until after we've traversed its children, that's hard
13796 to do. */
13797 dwarf2_record_block_ranges (die, block, baseaddr, cu);
c906108c 13798 }
a60f3166
TT
13799 local_symbols = cstk.locals;
13800 set_local_using_directives (cstk.local_using_directives);
c906108c
SS
13801}
13802
216f72a1 13803/* Read in DW_TAG_call_site and insert it to CU->call_site_htab. */
96408a79
SA
13804
13805static void
13806read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
13807{
518817b3 13808 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
96408a79
SA
13809 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13810 CORE_ADDR pc, baseaddr;
13811 struct attribute *attr;
13812 struct call_site *call_site, call_site_local;
13813 void **slot;
13814 int nparams;
13815 struct die_info *child_die;
13816
13817 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
13818
216f72a1
JK
13819 attr = dwarf2_attr (die, DW_AT_call_return_pc, cu);
13820 if (attr == NULL)
13821 {
13822 /* This was a pre-DWARF-5 GNU extension alias
13823 for DW_AT_call_return_pc. */
13824 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
13825 }
96408a79
SA
13826 if (!attr)
13827 {
b98664d3 13828 complaint (_("missing DW_AT_call_return_pc for DW_TAG_call_site "
9d8780f0
SM
13829 "DIE %s [in module %s]"),
13830 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79
SA
13831 return;
13832 }
31aa7e4e 13833 pc = attr_value_as_address (attr) + baseaddr;
3e29f34a 13834 pc = gdbarch_adjust_dwarf2_addr (gdbarch, pc);
96408a79
SA
13835
13836 if (cu->call_site_htab == NULL)
13837 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
13838 NULL, &objfile->objfile_obstack,
13839 hashtab_obstack_allocate, NULL);
13840 call_site_local.pc = pc;
13841 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
13842 if (*slot != NULL)
13843 {
b98664d3 13844 complaint (_("Duplicate PC %s for DW_TAG_call_site "
9d8780f0
SM
13845 "DIE %s [in module %s]"),
13846 paddress (gdbarch, pc), sect_offset_str (die->sect_off),
4262abfb 13847 objfile_name (objfile));
96408a79
SA
13848 return;
13849 }
13850
13851 /* Count parameters at the caller. */
13852
13853 nparams = 0;
13854 for (child_die = die->child; child_die && child_die->tag;
13855 child_die = sibling_die (child_die))
13856 {
216f72a1
JK
13857 if (child_die->tag != DW_TAG_call_site_parameter
13858 && child_die->tag != DW_TAG_GNU_call_site_parameter)
96408a79 13859 {
b98664d3 13860 complaint (_("Tag %d is not DW_TAG_call_site_parameter in "
9d8780f0
SM
13861 "DW_TAG_call_site child DIE %s [in module %s]"),
13862 child_die->tag, sect_offset_str (child_die->sect_off),
4262abfb 13863 objfile_name (objfile));
96408a79
SA
13864 continue;
13865 }
13866
13867 nparams++;
13868 }
13869
224c3ddb
SM
13870 call_site
13871 = ((struct call_site *)
13872 obstack_alloc (&objfile->objfile_obstack,
13873 sizeof (*call_site)
13874 + (sizeof (*call_site->parameter) * (nparams - 1))));
96408a79
SA
13875 *slot = call_site;
13876 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
13877 call_site->pc = pc;
13878
216f72a1
JK
13879 if (dwarf2_flag_true_p (die, DW_AT_call_tail_call, cu)
13880 || dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
96408a79
SA
13881 {
13882 struct die_info *func_die;
13883
13884 /* Skip also over DW_TAG_inlined_subroutine. */
13885 for (func_die = die->parent;
13886 func_die && func_die->tag != DW_TAG_subprogram
13887 && func_die->tag != DW_TAG_subroutine_type;
13888 func_die = func_die->parent);
13889
216f72a1
JK
13890 /* DW_AT_call_all_calls is a superset
13891 of DW_AT_call_all_tail_calls. */
96408a79 13892 if (func_die
216f72a1 13893 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_calls, cu)
96408a79 13894 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
216f72a1 13895 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_tail_calls, cu)
96408a79
SA
13896 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
13897 {
13898 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
13899 not complete. But keep CALL_SITE for look ups via call_site_htab,
13900 both the initial caller containing the real return address PC and
13901 the final callee containing the current PC of a chain of tail
13902 calls do not need to have the tail call list complete. But any
13903 function candidate for a virtual tail call frame searched via
13904 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
13905 determined unambiguously. */
13906 }
13907 else
13908 {
13909 struct type *func_type = NULL;
13910
13911 if (func_die)
13912 func_type = get_die_type (func_die, cu);
13913 if (func_type != NULL)
13914 {
13915 gdb_assert (TYPE_CODE (func_type) == TYPE_CODE_FUNC);
13916
13917 /* Enlist this call site to the function. */
13918 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
13919 TYPE_TAIL_CALL_LIST (func_type) = call_site;
13920 }
13921 else
b98664d3 13922 complaint (_("Cannot find function owning DW_TAG_call_site "
9d8780f0
SM
13923 "DIE %s [in module %s]"),
13924 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79
SA
13925 }
13926 }
13927
216f72a1
JK
13928 attr = dwarf2_attr (die, DW_AT_call_target, cu);
13929 if (attr == NULL)
13930 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
13931 if (attr == NULL)
13932 attr = dwarf2_attr (die, DW_AT_call_origin, cu);
96408a79 13933 if (attr == NULL)
216f72a1
JK
13934 {
13935 /* This was a pre-DWARF-5 GNU extension alias for DW_AT_call_origin. */
13936 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13937 }
96408a79
SA
13938 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
13939 if (!attr || (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0))
13940 /* Keep NULL DWARF_BLOCK. */;
13941 else if (attr_form_is_block (attr))
13942 {
13943 struct dwarf2_locexpr_baton *dlbaton;
13944
8d749320 13945 dlbaton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
96408a79
SA
13946 dlbaton->data = DW_BLOCK (attr)->data;
13947 dlbaton->size = DW_BLOCK (attr)->size;
13948 dlbaton->per_cu = cu->per_cu;
13949
13950 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
13951 }
7771576e 13952 else if (attr_form_is_ref (attr))
96408a79 13953 {
96408a79
SA
13954 struct dwarf2_cu *target_cu = cu;
13955 struct die_info *target_die;
13956
ac9ec31b 13957 target_die = follow_die_ref (die, attr, &target_cu);
518817b3 13958 gdb_assert (target_cu->per_cu->dwarf2_per_objfile->objfile == objfile);
96408a79
SA
13959 if (die_is_declaration (target_die, target_cu))
13960 {
7d45c7c3 13961 const char *target_physname;
9112db09
JK
13962
13963 /* Prefer the mangled name; otherwise compute the demangled one. */
73b9be8b 13964 target_physname = dw2_linkage_name (target_die, target_cu);
7d45c7c3 13965 if (target_physname == NULL)
9112db09 13966 target_physname = dwarf2_physname (NULL, target_die, target_cu);
96408a79 13967 if (target_physname == NULL)
b98664d3 13968 complaint (_("DW_AT_call_target target DIE has invalid "
9d8780f0
SM
13969 "physname, for referencing DIE %s [in module %s]"),
13970 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79 13971 else
7d455152 13972 SET_FIELD_PHYSNAME (call_site->target, target_physname);
96408a79
SA
13973 }
13974 else
13975 {
13976 CORE_ADDR lowpc;
13977
13978 /* DW_AT_entry_pc should be preferred. */
3a2b436a 13979 if (dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL)
e385593e 13980 <= PC_BOUNDS_INVALID)
b98664d3 13981 complaint (_("DW_AT_call_target target DIE has invalid "
9d8780f0
SM
13982 "low pc, for referencing DIE %s [in module %s]"),
13983 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79 13984 else
3e29f34a
MR
13985 {
13986 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13987 SET_FIELD_PHYSADDR (call_site->target, lowpc);
13988 }
96408a79
SA
13989 }
13990 }
13991 else
b98664d3 13992 complaint (_("DW_TAG_call_site DW_AT_call_target is neither "
9d8780f0
SM
13993 "block nor reference, for DIE %s [in module %s]"),
13994 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79
SA
13995
13996 call_site->per_cu = cu->per_cu;
13997
13998 for (child_die = die->child;
13999 child_die && child_die->tag;
14000 child_die = sibling_die (child_die))
14001 {
96408a79 14002 struct call_site_parameter *parameter;
1788b2d3 14003 struct attribute *loc, *origin;
96408a79 14004
216f72a1
JK
14005 if (child_die->tag != DW_TAG_call_site_parameter
14006 && child_die->tag != DW_TAG_GNU_call_site_parameter)
96408a79
SA
14007 {
14008 /* Already printed the complaint above. */
14009 continue;
14010 }
14011
14012 gdb_assert (call_site->parameter_count < nparams);
14013 parameter = &call_site->parameter[call_site->parameter_count];
14014
1788b2d3
JK
14015 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
14016 specifies DW_TAG_formal_parameter. Value of the data assumed for the
216f72a1 14017 register is contained in DW_AT_call_value. */
96408a79 14018
24c5c679 14019 loc = dwarf2_attr (child_die, DW_AT_location, cu);
216f72a1
JK
14020 origin = dwarf2_attr (child_die, DW_AT_call_parameter, cu);
14021 if (origin == NULL)
14022 {
14023 /* This was a pre-DWARF-5 GNU extension alias
14024 for DW_AT_call_parameter. */
14025 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
14026 }
7771576e 14027 if (loc == NULL && origin != NULL && attr_form_is_ref (origin))
1788b2d3 14028 {
1788b2d3 14029 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
9c541725
PA
14030
14031 sect_offset sect_off
14032 = (sect_offset) dwarf2_get_ref_die_offset (origin);
14033 if (!offset_in_cu_p (&cu->header, sect_off))
d76b7dbc
JK
14034 {
14035 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
14036 binding can be done only inside one CU. Such referenced DIE
14037 therefore cannot be even moved to DW_TAG_partial_unit. */
b98664d3 14038 complaint (_("DW_AT_call_parameter offset is not in CU for "
9d8780f0
SM
14039 "DW_TAG_call_site child DIE %s [in module %s]"),
14040 sect_offset_str (child_die->sect_off),
9c541725 14041 objfile_name (objfile));
d76b7dbc
JK
14042 continue;
14043 }
9c541725
PA
14044 parameter->u.param_cu_off
14045 = (cu_offset) (sect_off - cu->header.sect_off);
1788b2d3
JK
14046 }
14047 else if (loc == NULL || origin != NULL || !attr_form_is_block (loc))
96408a79 14048 {
b98664d3 14049 complaint (_("No DW_FORM_block* DW_AT_location for "
9d8780f0
SM
14050 "DW_TAG_call_site child DIE %s [in module %s]"),
14051 sect_offset_str (child_die->sect_off), objfile_name (objfile));
96408a79
SA
14052 continue;
14053 }
24c5c679 14054 else
96408a79 14055 {
24c5c679
JK
14056 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
14057 (DW_BLOCK (loc)->data, &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size]);
14058 if (parameter->u.dwarf_reg != -1)
14059 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
14060 else if (dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (loc)->data,
14061 &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size],
14062 &parameter->u.fb_offset))
14063 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
14064 else
14065 {
b98664d3 14066 complaint (_("Only single DW_OP_reg or DW_OP_fbreg is supported "
24c5c679 14067 "for DW_FORM_block* DW_AT_location is supported for "
9d8780f0 14068 "DW_TAG_call_site child DIE %s "
24c5c679 14069 "[in module %s]"),
9d8780f0 14070 sect_offset_str (child_die->sect_off),
9c541725 14071 objfile_name (objfile));
24c5c679
JK
14072 continue;
14073 }
96408a79
SA
14074 }
14075
216f72a1
JK
14076 attr = dwarf2_attr (child_die, DW_AT_call_value, cu);
14077 if (attr == NULL)
14078 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
96408a79
SA
14079 if (!attr_form_is_block (attr))
14080 {
b98664d3 14081 complaint (_("No DW_FORM_block* DW_AT_call_value for "
9d8780f0
SM
14082 "DW_TAG_call_site child DIE %s [in module %s]"),
14083 sect_offset_str (child_die->sect_off),
9c541725 14084 objfile_name (objfile));
96408a79
SA
14085 continue;
14086 }
14087 parameter->value = DW_BLOCK (attr)->data;
14088 parameter->value_size = DW_BLOCK (attr)->size;
14089
14090 /* Parameters are not pre-cleared by memset above. */
14091 parameter->data_value = NULL;
14092 parameter->data_value_size = 0;
14093 call_site->parameter_count++;
14094
216f72a1
JK
14095 attr = dwarf2_attr (child_die, DW_AT_call_data_value, cu);
14096 if (attr == NULL)
14097 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
96408a79
SA
14098 if (attr)
14099 {
14100 if (!attr_form_is_block (attr))
b98664d3 14101 complaint (_("No DW_FORM_block* DW_AT_call_data_value for "
9d8780f0
SM
14102 "DW_TAG_call_site child DIE %s [in module %s]"),
14103 sect_offset_str (child_die->sect_off),
9c541725 14104 objfile_name (objfile));
96408a79
SA
14105 else
14106 {
14107 parameter->data_value = DW_BLOCK (attr)->data;
14108 parameter->data_value_size = DW_BLOCK (attr)->size;
14109 }
14110 }
14111 }
14112}
14113
71a3c369
TT
14114/* Helper function for read_variable. If DIE represents a virtual
14115 table, then return the type of the concrete object that is
14116 associated with the virtual table. Otherwise, return NULL. */
14117
14118static struct type *
14119rust_containing_type (struct die_info *die, struct dwarf2_cu *cu)
14120{
14121 struct attribute *attr = dwarf2_attr (die, DW_AT_type, cu);
14122 if (attr == NULL)
14123 return NULL;
14124
14125 /* Find the type DIE. */
14126 struct die_info *type_die = NULL;
14127 struct dwarf2_cu *type_cu = cu;
14128
14129 if (attr_form_is_ref (attr))
14130 type_die = follow_die_ref (die, attr, &type_cu);
14131 if (type_die == NULL)
14132 return NULL;
14133
14134 if (dwarf2_attr (type_die, DW_AT_containing_type, type_cu) == NULL)
14135 return NULL;
14136 return die_containing_type (type_die, type_cu);
14137}
14138
14139/* Read a variable (DW_TAG_variable) DIE and create a new symbol. */
14140
14141static void
14142read_variable (struct die_info *die, struct dwarf2_cu *cu)
14143{
14144 struct rust_vtable_symbol *storage = NULL;
14145
14146 if (cu->language == language_rust)
14147 {
14148 struct type *containing_type = rust_containing_type (die, cu);
14149
14150 if (containing_type != NULL)
14151 {
518817b3 14152 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
71a3c369
TT
14153
14154 storage = OBSTACK_ZALLOC (&objfile->objfile_obstack,
14155 struct rust_vtable_symbol);
14156 initialize_objfile_symbol (storage);
14157 storage->concrete_type = containing_type;
cf724bc9 14158 storage->subclass = SYMBOL_RUST_VTABLE;
71a3c369
TT
14159 }
14160 }
14161
5e2db402 14162 new_symbol (die, NULL, cu, storage);
71a3c369
TT
14163}
14164
43988095
JK
14165/* Call CALLBACK from DW_AT_ranges attribute value OFFSET
14166 reading .debug_rnglists.
14167 Callback's type should be:
14168 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
14169 Return true if the attributes are present and valid, otherwise,
14170 return false. */
14171
14172template <typename Callback>
14173static bool
14174dwarf2_rnglists_process (unsigned offset, struct dwarf2_cu *cu,
14175 Callback &&callback)
14176{
ed2dc618 14177 struct dwarf2_per_objfile *dwarf2_per_objfile
518817b3 14178 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 14179 struct objfile *objfile = dwarf2_per_objfile->objfile;
43988095 14180 bfd *obfd = objfile->obfd;
43988095
JK
14181 /* Base address selection entry. */
14182 CORE_ADDR base;
14183 int found_base;
43988095 14184 const gdb_byte *buffer;
43988095
JK
14185 CORE_ADDR baseaddr;
14186 bool overflow = false;
14187
14188 found_base = cu->base_known;
14189 base = cu->base_address;
14190
14191 dwarf2_read_section (objfile, &dwarf2_per_objfile->rnglists);
14192 if (offset >= dwarf2_per_objfile->rnglists.size)
14193 {
b98664d3 14194 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
43988095
JK
14195 offset);
14196 return false;
14197 }
14198 buffer = dwarf2_per_objfile->rnglists.buffer + offset;
14199
14200 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
14201
14202 while (1)
14203 {
7814882a
JK
14204 /* Initialize it due to a false compiler warning. */
14205 CORE_ADDR range_beginning = 0, range_end = 0;
43988095
JK
14206 const gdb_byte *buf_end = (dwarf2_per_objfile->rnglists.buffer
14207 + dwarf2_per_objfile->rnglists.size);
14208 unsigned int bytes_read;
14209
14210 if (buffer == buf_end)
14211 {
14212 overflow = true;
14213 break;
14214 }
14215 const auto rlet = static_cast<enum dwarf_range_list_entry>(*buffer++);
14216 switch (rlet)
14217 {
14218 case DW_RLE_end_of_list:
14219 break;
14220 case DW_RLE_base_address:
14221 if (buffer + cu->header.addr_size > buf_end)
14222 {
14223 overflow = true;
14224 break;
14225 }
14226 base = read_address (obfd, buffer, cu, &bytes_read);
14227 found_base = 1;
14228 buffer += bytes_read;
14229 break;
14230 case DW_RLE_start_length:
14231 if (buffer + cu->header.addr_size > buf_end)
14232 {
14233 overflow = true;
14234 break;
14235 }
14236 range_beginning = read_address (obfd, buffer, cu, &bytes_read);
14237 buffer += bytes_read;
14238 range_end = (range_beginning
14239 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
14240 buffer += bytes_read;
14241 if (buffer > buf_end)
14242 {
14243 overflow = true;
14244 break;
14245 }
14246 break;
14247 case DW_RLE_offset_pair:
14248 range_beginning = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14249 buffer += bytes_read;
14250 if (buffer > buf_end)
14251 {
14252 overflow = true;
14253 break;
14254 }
14255 range_end = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14256 buffer += bytes_read;
14257 if (buffer > buf_end)
14258 {
14259 overflow = true;
14260 break;
14261 }
14262 break;
14263 case DW_RLE_start_end:
14264 if (buffer + 2 * cu->header.addr_size > buf_end)
14265 {
14266 overflow = true;
14267 break;
14268 }
14269 range_beginning = read_address (obfd, buffer, cu, &bytes_read);
14270 buffer += bytes_read;
14271 range_end = read_address (obfd, buffer, cu, &bytes_read);
14272 buffer += bytes_read;
14273 break;
14274 default:
b98664d3 14275 complaint (_("Invalid .debug_rnglists data (no base address)"));
43988095
JK
14276 return false;
14277 }
14278 if (rlet == DW_RLE_end_of_list || overflow)
14279 break;
14280 if (rlet == DW_RLE_base_address)
14281 continue;
14282
14283 if (!found_base)
14284 {
14285 /* We have no valid base address for the ranges
14286 data. */
b98664d3 14287 complaint (_("Invalid .debug_rnglists data (no base address)"));
43988095
JK
14288 return false;
14289 }
14290
14291 if (range_beginning > range_end)
14292 {
14293 /* Inverted range entries are invalid. */
b98664d3 14294 complaint (_("Invalid .debug_rnglists data (inverted range)"));
43988095
JK
14295 return false;
14296 }
14297
14298 /* Empty range entries have no effect. */
14299 if (range_beginning == range_end)
14300 continue;
14301
14302 range_beginning += base;
14303 range_end += base;
14304
14305 /* A not-uncommon case of bad debug info.
14306 Don't pollute the addrmap with bad data. */
14307 if (range_beginning + baseaddr == 0
14308 && !dwarf2_per_objfile->has_section_at_zero)
14309 {
b98664d3 14310 complaint (_(".debug_rnglists entry has start address of zero"
43988095
JK
14311 " [in module %s]"), objfile_name (objfile));
14312 continue;
14313 }
14314
14315 callback (range_beginning, range_end);
14316 }
14317
14318 if (overflow)
14319 {
b98664d3 14320 complaint (_("Offset %d is not terminated "
43988095
JK
14321 "for DW_AT_ranges attribute"),
14322 offset);
14323 return false;
14324 }
14325
14326 return true;
14327}
14328
14329/* Call CALLBACK from DW_AT_ranges attribute value OFFSET reading .debug_ranges.
14330 Callback's type should be:
14331 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
5f46c5a5 14332 Return 1 if the attributes are present and valid, otherwise, return 0. */
43039443 14333
43988095 14334template <typename Callback>
43039443 14335static int
5f46c5a5 14336dwarf2_ranges_process (unsigned offset, struct dwarf2_cu *cu,
43988095 14337 Callback &&callback)
43039443 14338{
ed2dc618 14339 struct dwarf2_per_objfile *dwarf2_per_objfile
518817b3 14340 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 14341 struct objfile *objfile = dwarf2_per_objfile->objfile;
43039443
JK
14342 struct comp_unit_head *cu_header = &cu->header;
14343 bfd *obfd = objfile->obfd;
14344 unsigned int addr_size = cu_header->addr_size;
14345 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
14346 /* Base address selection entry. */
14347 CORE_ADDR base;
14348 int found_base;
14349 unsigned int dummy;
d521ce57 14350 const gdb_byte *buffer;
ff013f42 14351 CORE_ADDR baseaddr;
43039443 14352
43988095
JK
14353 if (cu_header->version >= 5)
14354 return dwarf2_rnglists_process (offset, cu, callback);
14355
d00adf39
DE
14356 found_base = cu->base_known;
14357 base = cu->base_address;
43039443 14358
be391dca 14359 dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
dce234bc 14360 if (offset >= dwarf2_per_objfile->ranges.size)
43039443 14361 {
b98664d3 14362 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
43039443
JK
14363 offset);
14364 return 0;
14365 }
dce234bc 14366 buffer = dwarf2_per_objfile->ranges.buffer + offset;
43039443 14367
e7030f15 14368 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
ff013f42 14369
43039443
JK
14370 while (1)
14371 {
14372 CORE_ADDR range_beginning, range_end;
14373
14374 range_beginning = read_address (obfd, buffer, cu, &dummy);
14375 buffer += addr_size;
14376 range_end = read_address (obfd, buffer, cu, &dummy);
14377 buffer += addr_size;
14378 offset += 2 * addr_size;
14379
14380 /* An end of list marker is a pair of zero addresses. */
14381 if (range_beginning == 0 && range_end == 0)
14382 /* Found the end of list entry. */
14383 break;
14384
14385 /* Each base address selection entry is a pair of 2 values.
14386 The first is the largest possible address, the second is
14387 the base address. Check for a base address here. */
14388 if ((range_beginning & mask) == mask)
14389 {
28d2bfb9
AB
14390 /* If we found the largest possible address, then we already
14391 have the base address in range_end. */
14392 base = range_end;
43039443
JK
14393 found_base = 1;
14394 continue;
14395 }
14396
14397 if (!found_base)
14398 {
14399 /* We have no valid base address for the ranges
14400 data. */
b98664d3 14401 complaint (_("Invalid .debug_ranges data (no base address)"));
43039443
JK
14402 return 0;
14403 }
14404
9277c30c
UW
14405 if (range_beginning > range_end)
14406 {
14407 /* Inverted range entries are invalid. */
b98664d3 14408 complaint (_("Invalid .debug_ranges data (inverted range)"));
9277c30c
UW
14409 return 0;
14410 }
14411
14412 /* Empty range entries have no effect. */
14413 if (range_beginning == range_end)
14414 continue;
14415
43039443
JK
14416 range_beginning += base;
14417 range_end += base;
14418
01093045
DE
14419 /* A not-uncommon case of bad debug info.
14420 Don't pollute the addrmap with bad data. */
14421 if (range_beginning + baseaddr == 0
14422 && !dwarf2_per_objfile->has_section_at_zero)
14423 {
b98664d3 14424 complaint (_(".debug_ranges entry has start address of zero"
4262abfb 14425 " [in module %s]"), objfile_name (objfile));
01093045
DE
14426 continue;
14427 }
14428
5f46c5a5
JK
14429 callback (range_beginning, range_end);
14430 }
14431
14432 return 1;
14433}
14434
14435/* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
14436 Return 1 if the attributes are present and valid, otherwise, return 0.
14437 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
14438
14439static int
14440dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
14441 CORE_ADDR *high_return, struct dwarf2_cu *cu,
14442 struct partial_symtab *ranges_pst)
14443{
518817b3 14444 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
5f46c5a5
JK
14445 struct gdbarch *gdbarch = get_objfile_arch (objfile);
14446 const CORE_ADDR baseaddr = ANOFFSET (objfile->section_offsets,
14447 SECT_OFF_TEXT (objfile));
14448 int low_set = 0;
14449 CORE_ADDR low = 0;
14450 CORE_ADDR high = 0;
14451 int retval;
14452
14453 retval = dwarf2_ranges_process (offset, cu,
14454 [&] (CORE_ADDR range_beginning, CORE_ADDR range_end)
14455 {
9277c30c 14456 if (ranges_pst != NULL)
3e29f34a
MR
14457 {
14458 CORE_ADDR lowpc;
14459 CORE_ADDR highpc;
14460
14461 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch,
14462 range_beginning + baseaddr);
14463 highpc = gdbarch_adjust_dwarf2_addr (gdbarch,
14464 range_end + baseaddr);
14465 addrmap_set_empty (objfile->psymtabs_addrmap, lowpc, highpc - 1,
14466 ranges_pst);
14467 }
ff013f42 14468
43039443
JK
14469 /* FIXME: This is recording everything as a low-high
14470 segment of consecutive addresses. We should have a
14471 data structure for discontiguous block ranges
14472 instead. */
14473 if (! low_set)
14474 {
14475 low = range_beginning;
14476 high = range_end;
14477 low_set = 1;
14478 }
14479 else
14480 {
14481 if (range_beginning < low)
14482 low = range_beginning;
14483 if (range_end > high)
14484 high = range_end;
14485 }
5f46c5a5
JK
14486 });
14487 if (!retval)
14488 return 0;
43039443
JK
14489
14490 if (! low_set)
14491 /* If the first entry is an end-of-list marker, the range
14492 describes an empty scope, i.e. no instructions. */
14493 return 0;
14494
14495 if (low_return)
14496 *low_return = low;
14497 if (high_return)
14498 *high_return = high;
14499 return 1;
14500}
14501
3a2b436a
JK
14502/* Get low and high pc attributes from a die. See enum pc_bounds_kind
14503 definition for the return value. *LOWPC and *HIGHPC are set iff
e385593e 14504 neither PC_BOUNDS_NOT_PRESENT nor PC_BOUNDS_INVALID are returned. */
380bca97 14505
3a2b436a 14506static enum pc_bounds_kind
af34e669 14507dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
d85a05f0
DJ
14508 CORE_ADDR *highpc, struct dwarf2_cu *cu,
14509 struct partial_symtab *pst)
c906108c 14510{
518817b3
SM
14511 struct dwarf2_per_objfile *dwarf2_per_objfile
14512 = cu->per_cu->dwarf2_per_objfile;
c906108c 14513 struct attribute *attr;
91da1414 14514 struct attribute *attr_high;
af34e669
DJ
14515 CORE_ADDR low = 0;
14516 CORE_ADDR high = 0;
e385593e 14517 enum pc_bounds_kind ret;
c906108c 14518
91da1414
MW
14519 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14520 if (attr_high)
af34e669 14521 {
e142c38c 14522 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
af34e669 14523 if (attr)
91da1414 14524 {
31aa7e4e
JB
14525 low = attr_value_as_address (attr);
14526 high = attr_value_as_address (attr_high);
14527 if (cu->header.version >= 4 && attr_form_is_constant (attr_high))
14528 high += low;
91da1414 14529 }
af34e669
DJ
14530 else
14531 /* Found high w/o low attribute. */
e385593e 14532 return PC_BOUNDS_INVALID;
af34e669
DJ
14533
14534 /* Found consecutive range of addresses. */
3a2b436a 14535 ret = PC_BOUNDS_HIGH_LOW;
af34e669 14536 }
c906108c 14537 else
af34e669 14538 {
e142c38c 14539 attr = dwarf2_attr (die, DW_AT_ranges, cu);
af34e669
DJ
14540 if (attr != NULL)
14541 {
ab435259
DE
14542 /* DW_AT_ranges_base does not apply to DIEs from the DWO skeleton.
14543 We take advantage of the fact that DW_AT_ranges does not appear
14544 in DW_TAG_compile_unit of DWO files. */
14545 int need_ranges_base = die->tag != DW_TAG_compile_unit;
14546 unsigned int ranges_offset = (DW_UNSND (attr)
14547 + (need_ranges_base
14548 ? cu->ranges_base
14549 : 0));
2e3cf129 14550
af34e669 14551 /* Value of the DW_AT_ranges attribute is the offset in the
a604369a 14552 .debug_ranges section. */
2e3cf129 14553 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst))
e385593e 14554 return PC_BOUNDS_INVALID;
43039443 14555 /* Found discontinuous range of addresses. */
3a2b436a 14556 ret = PC_BOUNDS_RANGES;
af34e669 14557 }
e385593e
JK
14558 else
14559 return PC_BOUNDS_NOT_PRESENT;
af34e669 14560 }
c906108c 14561
48fbe735 14562 /* partial_die_info::read has also the strict LOW < HIGH requirement. */
9373cf26 14563 if (high <= low)
e385593e 14564 return PC_BOUNDS_INVALID;
c906108c
SS
14565
14566 /* When using the GNU linker, .gnu.linkonce. sections are used to
14567 eliminate duplicate copies of functions and vtables and such.
14568 The linker will arbitrarily choose one and discard the others.
14569 The AT_*_pc values for such functions refer to local labels in
14570 these sections. If the section from that file was discarded, the
14571 labels are not in the output, so the relocs get a value of 0.
14572 If this is a discarded function, mark the pc bounds as invalid,
14573 so that GDB will ignore it. */
72dca2f5 14574 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
e385593e 14575 return PC_BOUNDS_INVALID;
c906108c
SS
14576
14577 *lowpc = low;
96408a79
SA
14578 if (highpc)
14579 *highpc = high;
af34e669 14580 return ret;
c906108c
SS
14581}
14582
b084d499
JB
14583/* Assuming that DIE represents a subprogram DIE or a lexical block, get
14584 its low and high PC addresses. Do nothing if these addresses could not
14585 be determined. Otherwise, set LOWPC to the low address if it is smaller,
14586 and HIGHPC to the high address if greater than HIGHPC. */
14587
14588static void
14589dwarf2_get_subprogram_pc_bounds (struct die_info *die,
14590 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14591 struct dwarf2_cu *cu)
14592{
14593 CORE_ADDR low, high;
14594 struct die_info *child = die->child;
14595
e385593e 14596 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL) >= PC_BOUNDS_RANGES)
b084d499 14597 {
325fac50
PA
14598 *lowpc = std::min (*lowpc, low);
14599 *highpc = std::max (*highpc, high);
b084d499
JB
14600 }
14601
14602 /* If the language does not allow nested subprograms (either inside
14603 subprograms or lexical blocks), we're done. */
14604 if (cu->language != language_ada)
14605 return;
6e70227d 14606
b084d499
JB
14607 /* Check all the children of the given DIE. If it contains nested
14608 subprograms, then check their pc bounds. Likewise, we need to
14609 check lexical blocks as well, as they may also contain subprogram
14610 definitions. */
14611 while (child && child->tag)
14612 {
14613 if (child->tag == DW_TAG_subprogram
14614 || child->tag == DW_TAG_lexical_block)
14615 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
14616 child = sibling_die (child);
14617 }
14618}
14619
fae299cd
DC
14620/* Get the low and high pc's represented by the scope DIE, and store
14621 them in *LOWPC and *HIGHPC. If the correct values can't be
14622 determined, set *LOWPC to -1 and *HIGHPC to 0. */
14623
14624static void
14625get_scope_pc_bounds (struct die_info *die,
14626 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14627 struct dwarf2_cu *cu)
14628{
14629 CORE_ADDR best_low = (CORE_ADDR) -1;
14630 CORE_ADDR best_high = (CORE_ADDR) 0;
14631 CORE_ADDR current_low, current_high;
14632
3a2b436a 14633 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL)
e385593e 14634 >= PC_BOUNDS_RANGES)
fae299cd
DC
14635 {
14636 best_low = current_low;
14637 best_high = current_high;
14638 }
14639 else
14640 {
14641 struct die_info *child = die->child;
14642
14643 while (child && child->tag)
14644 {
14645 switch (child->tag) {
14646 case DW_TAG_subprogram:
b084d499 14647 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
fae299cd
DC
14648 break;
14649 case DW_TAG_namespace:
f55ee35c 14650 case DW_TAG_module:
fae299cd
DC
14651 /* FIXME: carlton/2004-01-16: Should we do this for
14652 DW_TAG_class_type/DW_TAG_structure_type, too? I think
14653 that current GCC's always emit the DIEs corresponding
14654 to definitions of methods of classes as children of a
14655 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
14656 the DIEs giving the declarations, which could be
14657 anywhere). But I don't see any reason why the
14658 standards says that they have to be there. */
14659 get_scope_pc_bounds (child, &current_low, &current_high, cu);
14660
14661 if (current_low != ((CORE_ADDR) -1))
14662 {
325fac50
PA
14663 best_low = std::min (best_low, current_low);
14664 best_high = std::max (best_high, current_high);
fae299cd
DC
14665 }
14666 break;
14667 default:
0963b4bd 14668 /* Ignore. */
fae299cd
DC
14669 break;
14670 }
14671
14672 child = sibling_die (child);
14673 }
14674 }
14675
14676 *lowpc = best_low;
14677 *highpc = best_high;
14678}
14679
801e3a5b
JB
14680/* Record the address ranges for BLOCK, offset by BASEADDR, as given
14681 in DIE. */
380bca97 14682
801e3a5b
JB
14683static void
14684dwarf2_record_block_ranges (struct die_info *die, struct block *block,
14685 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
14686{
518817b3 14687 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
3e29f34a 14688 struct gdbarch *gdbarch = get_objfile_arch (objfile);
801e3a5b 14689 struct attribute *attr;
91da1414 14690 struct attribute *attr_high;
801e3a5b 14691
91da1414
MW
14692 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14693 if (attr_high)
801e3a5b 14694 {
801e3a5b
JB
14695 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
14696 if (attr)
14697 {
31aa7e4e
JB
14698 CORE_ADDR low = attr_value_as_address (attr);
14699 CORE_ADDR high = attr_value_as_address (attr_high);
14700
14701 if (cu->header.version >= 4 && attr_form_is_constant (attr_high))
14702 high += low;
9a619af0 14703
3e29f34a
MR
14704 low = gdbarch_adjust_dwarf2_addr (gdbarch, low + baseaddr);
14705 high = gdbarch_adjust_dwarf2_addr (gdbarch, high + baseaddr);
14706 record_block_range (block, low, high - 1);
801e3a5b
JB
14707 }
14708 }
14709
14710 attr = dwarf2_attr (die, DW_AT_ranges, cu);
14711 if (attr)
14712 {
ab435259
DE
14713 /* DW_AT_ranges_base does not apply to DIEs from the DWO skeleton.
14714 We take advantage of the fact that DW_AT_ranges does not appear
14715 in DW_TAG_compile_unit of DWO files. */
14716 int need_ranges_base = die->tag != DW_TAG_compile_unit;
801e3a5b
JB
14717
14718 /* The value of the DW_AT_ranges attribute is the offset of the
14719 address range list in the .debug_ranges section. */
ab435259
DE
14720 unsigned long offset = (DW_UNSND (attr)
14721 + (need_ranges_base ? cu->ranges_base : 0));
801e3a5b 14722
5f46c5a5
JK
14723 dwarf2_ranges_process (offset, cu,
14724 [&] (CORE_ADDR start, CORE_ADDR end)
14725 {
58fdfd2c
JK
14726 start += baseaddr;
14727 end += baseaddr;
5f46c5a5
JK
14728 start = gdbarch_adjust_dwarf2_addr (gdbarch, start);
14729 end = gdbarch_adjust_dwarf2_addr (gdbarch, end);
14730 record_block_range (block, start, end - 1);
14731 });
801e3a5b
JB
14732 }
14733}
14734
685b1105
JK
14735/* Check whether the producer field indicates either of GCC < 4.6, or the
14736 Intel C/C++ compiler, and cache the result in CU. */
60d5a603 14737
685b1105
JK
14738static void
14739check_producer (struct dwarf2_cu *cu)
60d5a603 14740{
38360086 14741 int major, minor;
60d5a603
JK
14742
14743 if (cu->producer == NULL)
14744 {
14745 /* For unknown compilers expect their behavior is DWARF version
14746 compliant.
14747
14748 GCC started to support .debug_types sections by -gdwarf-4 since
14749 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
14750 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
14751 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
14752 interpreted incorrectly by GDB now - GCC PR debug/48229. */
60d5a603 14753 }
b1ffba5a 14754 else if (producer_is_gcc (cu->producer, &major, &minor))
60d5a603 14755 {
38360086
MW
14756 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
14757 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
685b1105 14758 }
5230b05a
WT
14759 else if (producer_is_icc (cu->producer, &major, &minor))
14760 cu->producer_is_icc_lt_14 = major < 14;
685b1105
JK
14761 else
14762 {
14763 /* For other non-GCC compilers, expect their behavior is DWARF version
14764 compliant. */
60d5a603
JK
14765 }
14766
ba919b58 14767 cu->checked_producer = 1;
685b1105 14768}
ba919b58 14769
685b1105
JK
14770/* Check for GCC PR debug/45124 fix which is not present in any G++ version up
14771 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
14772 during 4.6.0 experimental. */
14773
14774static int
14775producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
14776{
14777 if (!cu->checked_producer)
14778 check_producer (cu);
14779
14780 return cu->producer_is_gxx_lt_4_6;
60d5a603
JK
14781}
14782
14783/* Return the default accessibility type if it is not overriden by
14784 DW_AT_accessibility. */
14785
14786static enum dwarf_access_attribute
14787dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
14788{
14789 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
14790 {
14791 /* The default DWARF 2 accessibility for members is public, the default
14792 accessibility for inheritance is private. */
14793
14794 if (die->tag != DW_TAG_inheritance)
14795 return DW_ACCESS_public;
14796 else
14797 return DW_ACCESS_private;
14798 }
14799 else
14800 {
14801 /* DWARF 3+ defines the default accessibility a different way. The same
14802 rules apply now for DW_TAG_inheritance as for the members and it only
14803 depends on the container kind. */
14804
14805 if (die->parent->tag == DW_TAG_class_type)
14806 return DW_ACCESS_private;
14807 else
14808 return DW_ACCESS_public;
14809 }
14810}
14811
74ac6d43
TT
14812/* Look for DW_AT_data_member_location. Set *OFFSET to the byte
14813 offset. If the attribute was not found return 0, otherwise return
14814 1. If it was found but could not properly be handled, set *OFFSET
14815 to 0. */
14816
14817static int
14818handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
14819 LONGEST *offset)
14820{
14821 struct attribute *attr;
14822
14823 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
14824 if (attr != NULL)
14825 {
14826 *offset = 0;
14827
14828 /* Note that we do not check for a section offset first here.
14829 This is because DW_AT_data_member_location is new in DWARF 4,
14830 so if we see it, we can assume that a constant form is really
14831 a constant and not a section offset. */
14832 if (attr_form_is_constant (attr))
14833 *offset = dwarf2_get_attr_constant_value (attr, 0);
14834 else if (attr_form_is_section_offset (attr))
14835 dwarf2_complex_location_expr_complaint ();
14836 else if (attr_form_is_block (attr))
14837 *offset = decode_locdesc (DW_BLOCK (attr), cu);
14838 else
14839 dwarf2_complex_location_expr_complaint ();
14840
14841 return 1;
14842 }
14843
14844 return 0;
14845}
14846
c906108c
SS
14847/* Add an aggregate field to the field list. */
14848
14849static void
107d2387 14850dwarf2_add_field (struct field_info *fip, struct die_info *die,
e7c27a73 14851 struct dwarf2_cu *cu)
6e70227d 14852{
518817b3 14853 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
5e2b427d 14854 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
14855 struct nextfield *new_field;
14856 struct attribute *attr;
14857 struct field *fp;
15d034d0 14858 const char *fieldname = "";
c906108c 14859
7d0ccb61
DJ
14860 if (die->tag == DW_TAG_inheritance)
14861 {
be2daae6
TT
14862 fip->baseclasses.emplace_back ();
14863 new_field = &fip->baseclasses.back ();
7d0ccb61
DJ
14864 }
14865 else
14866 {
be2daae6
TT
14867 fip->fields.emplace_back ();
14868 new_field = &fip->fields.back ();
7d0ccb61 14869 }
be2daae6 14870
c906108c
SS
14871 fip->nfields++;
14872
e142c38c 14873 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
c906108c
SS
14874 if (attr)
14875 new_field->accessibility = DW_UNSND (attr);
60d5a603
JK
14876 else
14877 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
c906108c
SS
14878 if (new_field->accessibility != DW_ACCESS_public)
14879 fip->non_public_fields = 1;
60d5a603 14880
e142c38c 14881 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
c906108c
SS
14882 if (attr)
14883 new_field->virtuality = DW_UNSND (attr);
60d5a603
JK
14884 else
14885 new_field->virtuality = DW_VIRTUALITY_none;
c906108c
SS
14886
14887 fp = &new_field->field;
a9a9bd0f 14888
e142c38c 14889 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
c906108c 14890 {
74ac6d43
TT
14891 LONGEST offset;
14892
a9a9bd0f 14893 /* Data member other than a C++ static data member. */
6e70227d 14894
c906108c 14895 /* Get type of field. */
e7c27a73 14896 fp->type = die_type (die, cu);
c906108c 14897
d6a843b5 14898 SET_FIELD_BITPOS (*fp, 0);
01ad7f36 14899
c906108c 14900 /* Get bit size of field (zero if none). */
e142c38c 14901 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
c906108c
SS
14902 if (attr)
14903 {
14904 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
14905 }
14906 else
14907 {
14908 FIELD_BITSIZE (*fp) = 0;
14909 }
14910
14911 /* Get bit offset of field. */
74ac6d43
TT
14912 if (handle_data_member_location (die, cu, &offset))
14913 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
e142c38c 14914 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
c906108c
SS
14915 if (attr)
14916 {
5e2b427d 14917 if (gdbarch_bits_big_endian (gdbarch))
c906108c
SS
14918 {
14919 /* For big endian bits, the DW_AT_bit_offset gives the
c5aa993b
JM
14920 additional bit offset from the MSB of the containing
14921 anonymous object to the MSB of the field. We don't
14922 have to do anything special since we don't need to
14923 know the size of the anonymous object. */
f41f5e61 14924 SET_FIELD_BITPOS (*fp, FIELD_BITPOS (*fp) + DW_UNSND (attr));
c906108c
SS
14925 }
14926 else
14927 {
14928 /* For little endian bits, compute the bit offset to the
c5aa993b
JM
14929 MSB of the anonymous object, subtract off the number of
14930 bits from the MSB of the field to the MSB of the
14931 object, and then subtract off the number of bits of
14932 the field itself. The result is the bit offset of
14933 the LSB of the field. */
c906108c
SS
14934 int anonymous_size;
14935 int bit_offset = DW_UNSND (attr);
14936
e142c38c 14937 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
14938 if (attr)
14939 {
14940 /* The size of the anonymous object containing
14941 the bit field is explicit, so use the
14942 indicated size (in bytes). */
14943 anonymous_size = DW_UNSND (attr);
14944 }
14945 else
14946 {
14947 /* The size of the anonymous object containing
14948 the bit field must be inferred from the type
14949 attribute of the data member containing the
14950 bit field. */
14951 anonymous_size = TYPE_LENGTH (fp->type);
14952 }
f41f5e61
PA
14953 SET_FIELD_BITPOS (*fp,
14954 (FIELD_BITPOS (*fp)
14955 + anonymous_size * bits_per_byte
14956 - bit_offset - FIELD_BITSIZE (*fp)));
c906108c
SS
14957 }
14958 }
da5b30da
AA
14959 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
14960 if (attr != NULL)
14961 SET_FIELD_BITPOS (*fp, (FIELD_BITPOS (*fp)
14962 + dwarf2_get_attr_constant_value (attr, 0)));
c906108c
SS
14963
14964 /* Get name of field. */
39cbfefa
DJ
14965 fieldname = dwarf2_name (die, cu);
14966 if (fieldname == NULL)
14967 fieldname = "";
d8151005
DJ
14968
14969 /* The name is already allocated along with this objfile, so we don't
14970 need to duplicate it for the type. */
14971 fp->name = fieldname;
c906108c
SS
14972
14973 /* Change accessibility for artificial fields (e.g. virtual table
c5aa993b 14974 pointer or virtual base class pointer) to private. */
e142c38c 14975 if (dwarf2_attr (die, DW_AT_artificial, cu))
c906108c 14976 {
d48cc9dd 14977 FIELD_ARTIFICIAL (*fp) = 1;
c906108c
SS
14978 new_field->accessibility = DW_ACCESS_private;
14979 fip->non_public_fields = 1;
14980 }
14981 }
a9a9bd0f 14982 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
c906108c 14983 {
a9a9bd0f
DC
14984 /* C++ static member. */
14985
14986 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
14987 is a declaration, but all versions of G++ as of this writing
14988 (so through at least 3.2.1) incorrectly generate
14989 DW_TAG_variable tags. */
6e70227d 14990
ff355380 14991 const char *physname;
c906108c 14992
a9a9bd0f 14993 /* Get name of field. */
39cbfefa
DJ
14994 fieldname = dwarf2_name (die, cu);
14995 if (fieldname == NULL)
c906108c
SS
14996 return;
14997
254e6b9e 14998 attr = dwarf2_attr (die, DW_AT_const_value, cu);
3863f96c
DE
14999 if (attr
15000 /* Only create a symbol if this is an external value.
15001 new_symbol checks this and puts the value in the global symbol
15002 table, which we want. If it is not external, new_symbol
15003 will try to put the value in cu->list_in_scope which is wrong. */
15004 && dwarf2_flag_true_p (die, DW_AT_external, cu))
254e6b9e
DE
15005 {
15006 /* A static const member, not much different than an enum as far as
15007 we're concerned, except that we can support more types. */
15008 new_symbol (die, NULL, cu);
15009 }
15010
2df3850c 15011 /* Get physical name. */
ff355380 15012 physname = dwarf2_physname (fieldname, die, cu);
c906108c 15013
d8151005
DJ
15014 /* The name is already allocated along with this objfile, so we don't
15015 need to duplicate it for the type. */
15016 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
e7c27a73 15017 FIELD_TYPE (*fp) = die_type (die, cu);
d8151005 15018 FIELD_NAME (*fp) = fieldname;
c906108c
SS
15019 }
15020 else if (die->tag == DW_TAG_inheritance)
15021 {
74ac6d43 15022 LONGEST offset;
d4b96c9a 15023
74ac6d43
TT
15024 /* C++ base class field. */
15025 if (handle_data_member_location (die, cu, &offset))
15026 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
c906108c 15027 FIELD_BITSIZE (*fp) = 0;
e7c27a73 15028 FIELD_TYPE (*fp) = die_type (die, cu);
a737d952 15029 FIELD_NAME (*fp) = TYPE_NAME (fp->type);
c906108c 15030 }
2ddeaf8a
TT
15031 else if (die->tag == DW_TAG_variant_part)
15032 {
15033 /* process_structure_scope will treat this DIE as a union. */
15034 process_structure_scope (die, cu);
15035
15036 /* The variant part is relative to the start of the enclosing
15037 structure. */
15038 SET_FIELD_BITPOS (*fp, 0);
15039 fp->type = get_die_type (die, cu);
15040 fp->artificial = 1;
15041 fp->name = "<<variant>>";
15042 }
15043 else
15044 gdb_assert_not_reached ("missing case in dwarf2_add_field");
c906108c
SS
15045}
15046
883fd55a
KS
15047/* Can the type given by DIE define another type? */
15048
15049static bool
15050type_can_define_types (const struct die_info *die)
15051{
15052 switch (die->tag)
15053 {
15054 case DW_TAG_typedef:
15055 case DW_TAG_class_type:
15056 case DW_TAG_structure_type:
15057 case DW_TAG_union_type:
15058 case DW_TAG_enumeration_type:
15059 return true;
15060
15061 default:
15062 return false;
15063 }
15064}
15065
15066/* Add a type definition defined in the scope of the FIP's class. */
98751a41
JK
15067
15068static void
883fd55a
KS
15069dwarf2_add_type_defn (struct field_info *fip, struct die_info *die,
15070 struct dwarf2_cu *cu)
6e70227d 15071{
be2daae6
TT
15072 struct decl_field fp;
15073 memset (&fp, 0, sizeof (fp));
98751a41 15074
883fd55a 15075 gdb_assert (type_can_define_types (die));
98751a41 15076
883fd55a 15077 /* Get name of field. NULL is okay here, meaning an anonymous type. */
be2daae6
TT
15078 fp.name = dwarf2_name (die, cu);
15079 fp.type = read_type_die (die, cu);
98751a41 15080
c191a687
KS
15081 /* Save accessibility. */
15082 enum dwarf_access_attribute accessibility;
15083 struct attribute *attr = dwarf2_attr (die, DW_AT_accessibility, cu);
15084 if (attr != NULL)
15085 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
15086 else
15087 accessibility = dwarf2_default_access_attribute (die, cu);
15088 switch (accessibility)
15089 {
15090 case DW_ACCESS_public:
15091 /* The assumed value if neither private nor protected. */
15092 break;
15093 case DW_ACCESS_private:
be2daae6 15094 fp.is_private = 1;
c191a687
KS
15095 break;
15096 case DW_ACCESS_protected:
be2daae6 15097 fp.is_protected = 1;
c191a687
KS
15098 break;
15099 default:
b98664d3 15100 complaint (_("Unhandled DW_AT_accessibility value (%x)"), accessibility);
c191a687
KS
15101 }
15102
883fd55a 15103 if (die->tag == DW_TAG_typedef)
be2daae6 15104 fip->typedef_field_list.push_back (fp);
883fd55a 15105 else
be2daae6 15106 fip->nested_types_list.push_back (fp);
98751a41
JK
15107}
15108
c906108c
SS
15109/* Create the vector of fields, and attach it to the type. */
15110
15111static void
fba45db2 15112dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 15113 struct dwarf2_cu *cu)
c906108c
SS
15114{
15115 int nfields = fip->nfields;
15116
15117 /* Record the field count, allocate space for the array of fields,
15118 and create blank accessibility bitfields if necessary. */
15119 TYPE_NFIELDS (type) = nfields;
15120 TYPE_FIELDS (type) = (struct field *)
be2daae6 15121 TYPE_ZALLOC (type, sizeof (struct field) * nfields);
c906108c 15122
b4ba55a1 15123 if (fip->non_public_fields && cu->language != language_ada)
c906108c
SS
15124 {
15125 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15126
15127 TYPE_FIELD_PRIVATE_BITS (type) =
15128 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15129 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
15130
15131 TYPE_FIELD_PROTECTED_BITS (type) =
15132 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15133 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
15134
774b6a14
TT
15135 TYPE_FIELD_IGNORE_BITS (type) =
15136 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15137 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
c906108c
SS
15138 }
15139
15140 /* If the type has baseclasses, allocate and clear a bit vector for
15141 TYPE_FIELD_VIRTUAL_BITS. */
be2daae6 15142 if (!fip->baseclasses.empty () && cu->language != language_ada)
c906108c 15143 {
be2daae6 15144 int num_bytes = B_BYTES (fip->baseclasses.size ());
fe1b8b76 15145 unsigned char *pointer;
c906108c
SS
15146
15147 ALLOCATE_CPLUS_STRUCT_TYPE (type);
224c3ddb 15148 pointer = (unsigned char *) TYPE_ALLOC (type, num_bytes);
fe1b8b76 15149 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
be2daae6
TT
15150 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->baseclasses.size ());
15151 TYPE_N_BASECLASSES (type) = fip->baseclasses.size ();
c906108c
SS
15152 }
15153
2ddeaf8a
TT
15154 if (TYPE_FLAG_DISCRIMINATED_UNION (type))
15155 {
15156 struct discriminant_info *di = alloc_discriminant_info (type, -1, -1);
15157
be2daae6 15158 for (int index = 0; index < nfields; ++index)
2ddeaf8a 15159 {
be2daae6
TT
15160 struct nextfield &field = fip->fields[index];
15161
15162 if (field.variant.is_discriminant)
2ddeaf8a 15163 di->discriminant_index = index;
be2daae6 15164 else if (field.variant.default_branch)
2ddeaf8a
TT
15165 di->default_index = index;
15166 else
be2daae6 15167 di->discriminants[index] = field.variant.discriminant_value;
2ddeaf8a
TT
15168 }
15169 }
15170
be2daae6
TT
15171 /* Copy the saved-up fields into the field vector. */
15172 for (int i = 0; i < nfields; ++i)
c906108c 15173 {
be2daae6
TT
15174 struct nextfield &field
15175 = ((i < fip->baseclasses.size ()) ? fip->baseclasses[i]
15176 : fip->fields[i - fip->baseclasses.size ()]);
7d0ccb61 15177
be2daae6
TT
15178 TYPE_FIELD (type, i) = field.field;
15179 switch (field.accessibility)
c906108c 15180 {
c5aa993b 15181 case DW_ACCESS_private:
b4ba55a1 15182 if (cu->language != language_ada)
be2daae6 15183 SET_TYPE_FIELD_PRIVATE (type, i);
c5aa993b 15184 break;
c906108c 15185
c5aa993b 15186 case DW_ACCESS_protected:
b4ba55a1 15187 if (cu->language != language_ada)
be2daae6 15188 SET_TYPE_FIELD_PROTECTED (type, i);
c5aa993b 15189 break;
c906108c 15190
c5aa993b
JM
15191 case DW_ACCESS_public:
15192 break;
c906108c 15193
c5aa993b
JM
15194 default:
15195 /* Unknown accessibility. Complain and treat it as public. */
15196 {
b98664d3 15197 complaint (_("unsupported accessibility %d"),
be2daae6 15198 field.accessibility);
c5aa993b
JM
15199 }
15200 break;
c906108c 15201 }
be2daae6 15202 if (i < fip->baseclasses.size ())
c906108c 15203 {
be2daae6 15204 switch (field.virtuality)
c906108c 15205 {
c5aa993b
JM
15206 case DW_VIRTUALITY_virtual:
15207 case DW_VIRTUALITY_pure_virtual:
b4ba55a1 15208 if (cu->language == language_ada)
a73c6dcd 15209 error (_("unexpected virtuality in component of Ada type"));
be2daae6 15210 SET_TYPE_FIELD_VIRTUAL (type, i);
c5aa993b 15211 break;
c906108c
SS
15212 }
15213 }
c906108c
SS
15214 }
15215}
15216
7d27a96d
TT
15217/* Return true if this member function is a constructor, false
15218 otherwise. */
15219
15220static int
15221dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
15222{
15223 const char *fieldname;
fe978cb0 15224 const char *type_name;
7d27a96d
TT
15225 int len;
15226
15227 if (die->parent == NULL)
15228 return 0;
15229
15230 if (die->parent->tag != DW_TAG_structure_type
15231 && die->parent->tag != DW_TAG_union_type
15232 && die->parent->tag != DW_TAG_class_type)
15233 return 0;
15234
15235 fieldname = dwarf2_name (die, cu);
fe978cb0
PA
15236 type_name = dwarf2_name (die->parent, cu);
15237 if (fieldname == NULL || type_name == NULL)
7d27a96d
TT
15238 return 0;
15239
15240 len = strlen (fieldname);
fe978cb0
PA
15241 return (strncmp (fieldname, type_name, len) == 0
15242 && (type_name[len] == '\0' || type_name[len] == '<'));
7d27a96d
TT
15243}
15244
c906108c
SS
15245/* Add a member function to the proper fieldlist. */
15246
15247static void
107d2387 15248dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
e7c27a73 15249 struct type *type, struct dwarf2_cu *cu)
c906108c 15250{
518817b3 15251 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c 15252 struct attribute *attr;
c906108c 15253 int i;
be2daae6 15254 struct fnfieldlist *flp = nullptr;
c906108c 15255 struct fn_field *fnp;
15d034d0 15256 const char *fieldname;
f792889a 15257 struct type *this_type;
60d5a603 15258 enum dwarf_access_attribute accessibility;
c906108c 15259
b4ba55a1 15260 if (cu->language == language_ada)
a73c6dcd 15261 error (_("unexpected member function in Ada type"));
b4ba55a1 15262
2df3850c 15263 /* Get name of member function. */
39cbfefa
DJ
15264 fieldname = dwarf2_name (die, cu);
15265 if (fieldname == NULL)
2df3850c 15266 return;
c906108c 15267
c906108c 15268 /* Look up member function name in fieldlist. */
be2daae6 15269 for (i = 0; i < fip->fnfieldlists.size (); i++)
c906108c 15270 {
27bfe10e 15271 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
be2daae6
TT
15272 {
15273 flp = &fip->fnfieldlists[i];
15274 break;
15275 }
c906108c
SS
15276 }
15277
be2daae6
TT
15278 /* Create a new fnfieldlist if necessary. */
15279 if (flp == nullptr)
c906108c 15280 {
be2daae6
TT
15281 fip->fnfieldlists.emplace_back ();
15282 flp = &fip->fnfieldlists.back ();
c906108c 15283 flp->name = fieldname;
be2daae6 15284 i = fip->fnfieldlists.size () - 1;
c906108c
SS
15285 }
15286
be2daae6
TT
15287 /* Create a new member function field and add it to the vector of
15288 fnfieldlists. */
15289 flp->fnfields.emplace_back ();
15290 fnp = &flp->fnfields.back ();
3da10d80
KS
15291
15292 /* Delay processing of the physname until later. */
9c37b5ae 15293 if (cu->language == language_cplus)
be2daae6
TT
15294 add_to_method_list (type, i, flp->fnfields.size () - 1, fieldname,
15295 die, cu);
3da10d80
KS
15296 else
15297 {
1d06ead6 15298 const char *physname = dwarf2_physname (fieldname, die, cu);
3da10d80
KS
15299 fnp->physname = physname ? physname : "";
15300 }
15301
c906108c 15302 fnp->type = alloc_type (objfile);
f792889a
DJ
15303 this_type = read_type_die (die, cu);
15304 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
c906108c 15305 {
f792889a 15306 int nparams = TYPE_NFIELDS (this_type);
c906108c 15307
f792889a 15308 /* TYPE is the domain of this method, and THIS_TYPE is the type
e26fb1d7
DC
15309 of the method itself (TYPE_CODE_METHOD). */
15310 smash_to_method_type (fnp->type, type,
f792889a
DJ
15311 TYPE_TARGET_TYPE (this_type),
15312 TYPE_FIELDS (this_type),
15313 TYPE_NFIELDS (this_type),
15314 TYPE_VARARGS (this_type));
c906108c
SS
15315
15316 /* Handle static member functions.
c5aa993b 15317 Dwarf2 has no clean way to discern C++ static and non-static
0963b4bd
MS
15318 member functions. G++ helps GDB by marking the first
15319 parameter for non-static member functions (which is the this
15320 pointer) as artificial. We obtain this information from
15321 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
f792889a 15322 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
c906108c
SS
15323 fnp->voffset = VOFFSET_STATIC;
15324 }
15325 else
b98664d3 15326 complaint (_("member function type missing for '%s'"),
3da10d80 15327 dwarf2_full_name (fieldname, die, cu));
c906108c
SS
15328
15329 /* Get fcontext from DW_AT_containing_type if present. */
e142c38c 15330 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
e7c27a73 15331 fnp->fcontext = die_containing_type (die, cu);
c906108c 15332
3e43a32a
MS
15333 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
15334 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
c906108c
SS
15335
15336 /* Get accessibility. */
e142c38c 15337 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
c906108c 15338 if (attr)
aead7601 15339 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
60d5a603
JK
15340 else
15341 accessibility = dwarf2_default_access_attribute (die, cu);
15342 switch (accessibility)
c906108c 15343 {
60d5a603
JK
15344 case DW_ACCESS_private:
15345 fnp->is_private = 1;
15346 break;
15347 case DW_ACCESS_protected:
15348 fnp->is_protected = 1;
15349 break;
c906108c
SS
15350 }
15351
b02dede2 15352 /* Check for artificial methods. */
e142c38c 15353 attr = dwarf2_attr (die, DW_AT_artificial, cu);
b02dede2
DJ
15354 if (attr && DW_UNSND (attr) != 0)
15355 fnp->is_artificial = 1;
15356
7d27a96d
TT
15357 fnp->is_constructor = dwarf2_is_constructor (die, cu);
15358
0d564a31 15359 /* Get index in virtual function table if it is a virtual member
aec5aa8b
TT
15360 function. For older versions of GCC, this is an offset in the
15361 appropriate virtual table, as specified by DW_AT_containing_type.
15362 For everyone else, it is an expression to be evaluated relative
0d564a31
DJ
15363 to the object address. */
15364
e142c38c 15365 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
aec5aa8b 15366 if (attr)
8e19ed76 15367 {
aec5aa8b 15368 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size > 0)
8e19ed76 15369 {
aec5aa8b
TT
15370 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
15371 {
15372 /* Old-style GCC. */
15373 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
15374 }
15375 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
15376 || (DW_BLOCK (attr)->size > 1
15377 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
15378 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
15379 {
aec5aa8b
TT
15380 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
15381 if ((fnp->voffset % cu->header.addr_size) != 0)
15382 dwarf2_complex_location_expr_complaint ();
15383 else
15384 fnp->voffset /= cu->header.addr_size;
15385 fnp->voffset += 2;
15386 }
15387 else
15388 dwarf2_complex_location_expr_complaint ();
15389
15390 if (!fnp->fcontext)
7e993ebf
KS
15391 {
15392 /* If there is no `this' field and no DW_AT_containing_type,
15393 we cannot actually find a base class context for the
15394 vtable! */
15395 if (TYPE_NFIELDS (this_type) == 0
15396 || !TYPE_FIELD_ARTIFICIAL (this_type, 0))
15397 {
b98664d3 15398 complaint (_("cannot determine context for virtual member "
9d8780f0
SM
15399 "function \"%s\" (offset %s)"),
15400 fieldname, sect_offset_str (die->sect_off));
7e993ebf
KS
15401 }
15402 else
15403 {
15404 fnp->fcontext
15405 = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
15406 }
15407 }
aec5aa8b 15408 }
3690dd37 15409 else if (attr_form_is_section_offset (attr))
8e19ed76 15410 {
4d3c2250 15411 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
15412 }
15413 else
15414 {
4d3c2250
KB
15415 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
15416 fieldname);
8e19ed76 15417 }
0d564a31 15418 }
d48cc9dd
DJ
15419 else
15420 {
15421 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
15422 if (attr && DW_UNSND (attr))
15423 {
15424 /* GCC does this, as of 2008-08-25; PR debug/37237. */
b98664d3 15425 complaint (_("Member function \"%s\" (offset %s) is virtual "
3e43a32a 15426 "but the vtable offset is not specified"),
9d8780f0 15427 fieldname, sect_offset_str (die->sect_off));
9655fd1a 15428 ALLOCATE_CPLUS_STRUCT_TYPE (type);
d48cc9dd
DJ
15429 TYPE_CPLUS_DYNAMIC (type) = 1;
15430 }
15431 }
c906108c
SS
15432}
15433
15434/* Create the vector of member function fields, and attach it to the type. */
15435
15436static void
fba45db2 15437dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 15438 struct dwarf2_cu *cu)
c906108c 15439{
b4ba55a1 15440 if (cu->language == language_ada)
a73c6dcd 15441 error (_("unexpected member functions in Ada type"));
b4ba55a1 15442
c906108c
SS
15443 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15444 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
be2daae6
TT
15445 TYPE_ALLOC (type,
15446 sizeof (struct fn_fieldlist) * fip->fnfieldlists.size ());
c906108c 15447
be2daae6 15448 for (int i = 0; i < fip->fnfieldlists.size (); i++)
c906108c 15449 {
be2daae6 15450 struct fnfieldlist &nf = fip->fnfieldlists[i];
c906108c 15451 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
c906108c 15452
be2daae6
TT
15453 TYPE_FN_FIELDLIST_NAME (type, i) = nf.name;
15454 TYPE_FN_FIELDLIST_LENGTH (type, i) = nf.fnfields.size ();
c906108c 15455 fn_flp->fn_fields = (struct fn_field *)
be2daae6
TT
15456 TYPE_ALLOC (type, sizeof (struct fn_field) * nf.fnfields.size ());
15457
15458 for (int k = 0; k < nf.fnfields.size (); ++k)
15459 fn_flp->fn_fields[k] = nf.fnfields[k];
c906108c
SS
15460 }
15461
be2daae6 15462 TYPE_NFN_FIELDS (type) = fip->fnfieldlists.size ();
c906108c
SS
15463}
15464
1168df01
JB
15465/* Returns non-zero if NAME is the name of a vtable member in CU's
15466 language, zero otherwise. */
15467static int
15468is_vtable_name (const char *name, struct dwarf2_cu *cu)
15469{
15470 static const char vptr[] = "_vptr";
15471
9c37b5ae
TT
15472 /* Look for the C++ form of the vtable. */
15473 if (startswith (name, vptr) && is_cplus_marker (name[sizeof (vptr) - 1]))
1168df01
JB
15474 return 1;
15475
15476 return 0;
15477}
15478
c0dd20ea 15479/* GCC outputs unnamed structures that are really pointers to member
0b92b5bb
TT
15480 functions, with the ABI-specified layout. If TYPE describes
15481 such a structure, smash it into a member function type.
61049d3b
DJ
15482
15483 GCC shouldn't do this; it should just output pointer to member DIEs.
15484 This is GCC PR debug/28767. */
c0dd20ea 15485
0b92b5bb
TT
15486static void
15487quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
c0dd20ea 15488{
09e2d7c7 15489 struct type *pfn_type, *self_type, *new_type;
c0dd20ea
DJ
15490
15491 /* Check for a structure with no name and two children. */
0b92b5bb
TT
15492 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
15493 return;
c0dd20ea
DJ
15494
15495 /* Check for __pfn and __delta members. */
0b92b5bb
TT
15496 if (TYPE_FIELD_NAME (type, 0) == NULL
15497 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
15498 || TYPE_FIELD_NAME (type, 1) == NULL
15499 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
15500 return;
c0dd20ea
DJ
15501
15502 /* Find the type of the method. */
0b92b5bb 15503 pfn_type = TYPE_FIELD_TYPE (type, 0);
c0dd20ea
DJ
15504 if (pfn_type == NULL
15505 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
15506 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
0b92b5bb 15507 return;
c0dd20ea
DJ
15508
15509 /* Look for the "this" argument. */
15510 pfn_type = TYPE_TARGET_TYPE (pfn_type);
15511 if (TYPE_NFIELDS (pfn_type) == 0
0b92b5bb 15512 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
c0dd20ea 15513 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
0b92b5bb 15514 return;
c0dd20ea 15515
09e2d7c7 15516 self_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
0b92b5bb 15517 new_type = alloc_type (objfile);
09e2d7c7 15518 smash_to_method_type (new_type, self_type, TYPE_TARGET_TYPE (pfn_type),
c0dd20ea
DJ
15519 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
15520 TYPE_VARARGS (pfn_type));
0b92b5bb 15521 smash_to_methodptr_type (type, new_type);
c0dd20ea 15522}
1168df01 15523
2b4424c3
TT
15524/* If the DIE has a DW_AT_alignment attribute, return its value, doing
15525 appropriate error checking and issuing complaints if there is a
15526 problem. */
15527
15528static ULONGEST
15529get_alignment (struct dwarf2_cu *cu, struct die_info *die)
15530{
15531 struct attribute *attr = dwarf2_attr (die, DW_AT_alignment, cu);
15532
15533 if (attr == nullptr)
15534 return 0;
15535
15536 if (!attr_form_is_constant (attr))
15537 {
b98664d3 15538 complaint (_("DW_AT_alignment must have constant form"
2b4424c3
TT
15539 " - DIE at %s [in module %s]"),
15540 sect_offset_str (die->sect_off),
15541 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15542 return 0;
15543 }
15544
15545 ULONGEST align;
15546 if (attr->form == DW_FORM_sdata)
15547 {
15548 LONGEST val = DW_SND (attr);
15549 if (val < 0)
15550 {
b98664d3 15551 complaint (_("DW_AT_alignment value must not be negative"
2b4424c3
TT
15552 " - DIE at %s [in module %s]"),
15553 sect_offset_str (die->sect_off),
15554 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15555 return 0;
15556 }
15557 align = val;
15558 }
15559 else
15560 align = DW_UNSND (attr);
15561
15562 if (align == 0)
15563 {
b98664d3 15564 complaint (_("DW_AT_alignment value must not be zero"
2b4424c3
TT
15565 " - DIE at %s [in module %s]"),
15566 sect_offset_str (die->sect_off),
15567 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15568 return 0;
15569 }
15570 if ((align & (align - 1)) != 0)
15571 {
b98664d3 15572 complaint (_("DW_AT_alignment value must be a power of 2"
2b4424c3
TT
15573 " - DIE at %s [in module %s]"),
15574 sect_offset_str (die->sect_off),
15575 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15576 return 0;
15577 }
15578
15579 return align;
15580}
15581
15582/* If the DIE has a DW_AT_alignment attribute, use its value to set
15583 the alignment for TYPE. */
15584
15585static void
15586maybe_set_alignment (struct dwarf2_cu *cu, struct die_info *die,
15587 struct type *type)
15588{
15589 if (!set_type_align (type, get_alignment (cu, die)))
b98664d3 15590 complaint (_("DW_AT_alignment value too large"
2b4424c3
TT
15591 " - DIE at %s [in module %s]"),
15592 sect_offset_str (die->sect_off),
15593 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15594}
685b1105 15595
c906108c 15596/* Called when we find the DIE that starts a structure or union scope
c767944b
DJ
15597 (definition) to create a type for the structure or union. Fill in
15598 the type's name and general properties; the members will not be
83655187
DE
15599 processed until process_structure_scope. A symbol table entry for
15600 the type will also not be done until process_structure_scope (assuming
15601 the type has a name).
c906108c 15602
c767944b
DJ
15603 NOTE: we need to call these functions regardless of whether or not the
15604 DIE has a DW_AT_name attribute, since it might be an anonymous
c906108c 15605 structure or union. This gets the type entered into our set of
83655187 15606 user defined types. */
c906108c 15607
f792889a 15608static struct type *
134d01f1 15609read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 15610{
518817b3 15611 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c
SS
15612 struct type *type;
15613 struct attribute *attr;
15d034d0 15614 const char *name;
c906108c 15615
348e048f
DE
15616 /* If the definition of this type lives in .debug_types, read that type.
15617 Don't follow DW_AT_specification though, that will take us back up
15618 the chain and we want to go down. */
45e58e77 15619 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
348e048f
DE
15620 if (attr)
15621 {
ac9ec31b 15622 type = get_DW_AT_signature_type (die, attr, cu);
9dc481d3 15623
ac9ec31b 15624 /* The type's CU may not be the same as CU.
02142a6c 15625 Ensure TYPE is recorded with CU in die_type_hash. */
348e048f
DE
15626 return set_die_type (die, type, cu);
15627 }
15628
c0dd20ea 15629 type = alloc_type (objfile);
c906108c 15630 INIT_CPLUS_SPECIFIC (type);
93311388 15631
39cbfefa
DJ
15632 name = dwarf2_name (die, cu);
15633 if (name != NULL)
c906108c 15634 {
987504bb 15635 if (cu->language == language_cplus
c44af4eb
TT
15636 || cu->language == language_d
15637 || cu->language == language_rust)
63d06c5c 15638 {
15d034d0 15639 const char *full_name = dwarf2_full_name (name, die, cu);
3da10d80
KS
15640
15641 /* dwarf2_full_name might have already finished building the DIE's
15642 type. If so, there is no need to continue. */
15643 if (get_die_type (die, cu) != NULL)
15644 return get_die_type (die, cu);
15645
e86ca25f 15646 TYPE_NAME (type) = full_name;
63d06c5c
DC
15647 }
15648 else
15649 {
d8151005
DJ
15650 /* The name is already allocated along with this objfile, so
15651 we don't need to duplicate it for the type. */
e86ca25f 15652 TYPE_NAME (type) = name;
63d06c5c 15653 }
c906108c
SS
15654 }
15655
15656 if (die->tag == DW_TAG_structure_type)
15657 {
15658 TYPE_CODE (type) = TYPE_CODE_STRUCT;
15659 }
15660 else if (die->tag == DW_TAG_union_type)
15661 {
15662 TYPE_CODE (type) = TYPE_CODE_UNION;
15663 }
2ddeaf8a
TT
15664 else if (die->tag == DW_TAG_variant_part)
15665 {
15666 TYPE_CODE (type) = TYPE_CODE_UNION;
15667 TYPE_FLAG_DISCRIMINATED_UNION (type) = 1;
15668 }
c906108c
SS
15669 else
15670 {
4753d33b 15671 TYPE_CODE (type) = TYPE_CODE_STRUCT;
c906108c
SS
15672 }
15673
0cc2414c
TT
15674 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
15675 TYPE_DECLARED_CLASS (type) = 1;
15676
e142c38c 15677 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
15678 if (attr)
15679 {
155bfbd3
JB
15680 if (attr_form_is_constant (attr))
15681 TYPE_LENGTH (type) = DW_UNSND (attr);
15682 else
15683 {
15684 /* For the moment, dynamic type sizes are not supported
15685 by GDB's struct type. The actual size is determined
15686 on-demand when resolving the type of a given object,
15687 so set the type's length to zero for now. Otherwise,
15688 we record an expression as the length, and that expression
15689 could lead to a very large value, which could eventually
15690 lead to us trying to allocate that much memory when creating
15691 a value of that type. */
15692 TYPE_LENGTH (type) = 0;
15693 }
c906108c
SS
15694 }
15695 else
15696 {
15697 TYPE_LENGTH (type) = 0;
15698 }
15699
2b4424c3
TT
15700 maybe_set_alignment (cu, die, type);
15701
5230b05a 15702 if (producer_is_icc_lt_14 (cu) && (TYPE_LENGTH (type) == 0))
685b1105 15703 {
5230b05a
WT
15704 /* ICC<14 does not output the required DW_AT_declaration on
15705 incomplete types, but gives them a size of zero. */
422b1cb0 15706 TYPE_STUB (type) = 1;
685b1105
JK
15707 }
15708 else
15709 TYPE_STUB_SUPPORTED (type) = 1;
15710
dc718098 15711 if (die_is_declaration (die, cu))
876cecd0 15712 TYPE_STUB (type) = 1;
a6c727b2
DJ
15713 else if (attr == NULL && die->child == NULL
15714 && producer_is_realview (cu->producer))
15715 /* RealView does not output the required DW_AT_declaration
15716 on incomplete types. */
15717 TYPE_STUB (type) = 1;
dc718098 15718
c906108c
SS
15719 /* We need to add the type field to the die immediately so we don't
15720 infinitely recurse when dealing with pointers to the structure
0963b4bd 15721 type within the structure itself. */
1c379e20 15722 set_die_type (die, type, cu);
c906108c 15723
7e314c57
JK
15724 /* set_die_type should be already done. */
15725 set_descriptive_type (type, die, cu);
15726
c767944b
DJ
15727 return type;
15728}
15729
2ddeaf8a
TT
15730/* A helper for process_structure_scope that handles a single member
15731 DIE. */
15732
15733static void
15734handle_struct_member_die (struct die_info *child_die, struct type *type,
15735 struct field_info *fi,
15736 std::vector<struct symbol *> *template_args,
15737 struct dwarf2_cu *cu)
15738{
15739 if (child_die->tag == DW_TAG_member
15740 || child_die->tag == DW_TAG_variable
15741 || child_die->tag == DW_TAG_variant_part)
15742 {
15743 /* NOTE: carlton/2002-11-05: A C++ static data member
15744 should be a DW_TAG_member that is a declaration, but
15745 all versions of G++ as of this writing (so through at
15746 least 3.2.1) incorrectly generate DW_TAG_variable
15747 tags for them instead. */
15748 dwarf2_add_field (fi, child_die, cu);
15749 }
15750 else if (child_die->tag == DW_TAG_subprogram)
15751 {
15752 /* Rust doesn't have member functions in the C++ sense.
15753 However, it does emit ordinary functions as children
15754 of a struct DIE. */
15755 if (cu->language == language_rust)
15756 read_func_scope (child_die, cu);
15757 else
15758 {
15759 /* C++ member function. */
15760 dwarf2_add_member_fn (fi, child_die, type, cu);
15761 }
15762 }
15763 else if (child_die->tag == DW_TAG_inheritance)
15764 {
15765 /* C++ base class field. */
15766 dwarf2_add_field (fi, child_die, cu);
15767 }
15768 else if (type_can_define_types (child_die))
15769 dwarf2_add_type_defn (fi, child_die, cu);
15770 else if (child_die->tag == DW_TAG_template_type_param
15771 || child_die->tag == DW_TAG_template_value_param)
15772 {
15773 struct symbol *arg = new_symbol (child_die, NULL, cu);
15774
15775 if (arg != NULL)
15776 template_args->push_back (arg);
15777 }
15778 else if (child_die->tag == DW_TAG_variant)
15779 {
15780 /* In a variant we want to get the discriminant and also add a
15781 field for our sole member child. */
15782 struct attribute *discr = dwarf2_attr (child_die, DW_AT_discr_value, cu);
15783
15784 for (struct die_info *variant_child = child_die->child;
15785 variant_child != NULL;
15786 variant_child = sibling_die (variant_child))
15787 {
15788 if (variant_child->tag == DW_TAG_member)
15789 {
15790 handle_struct_member_die (variant_child, type, fi,
15791 template_args, cu);
15792 /* Only handle the one. */
15793 break;
15794 }
15795 }
15796
15797 /* We don't handle this but we might as well report it if we see
15798 it. */
15799 if (dwarf2_attr (child_die, DW_AT_discr_list, cu) != nullptr)
b98664d3 15800 complaint (_("DW_AT_discr_list is not supported yet"
2ddeaf8a
TT
15801 " - DIE at %s [in module %s]"),
15802 sect_offset_str (child_die->sect_off),
15803 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15804
15805 /* The first field was just added, so we can stash the
15806 discriminant there. */
be2daae6 15807 gdb_assert (!fi->fields.empty ());
2ddeaf8a 15808 if (discr == NULL)
be2daae6 15809 fi->fields.back ().variant.default_branch = true;
2ddeaf8a 15810 else
be2daae6 15811 fi->fields.back ().variant.discriminant_value = DW_UNSND (discr);
2ddeaf8a
TT
15812 }
15813}
15814
c767944b
DJ
15815/* Finish creating a structure or union type, including filling in
15816 its members and creating a symbol for it. */
15817
15818static void
15819process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
15820{
518817b3 15821 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
ca040673 15822 struct die_info *child_die;
c767944b
DJ
15823 struct type *type;
15824
15825 type = get_die_type (die, cu);
15826 if (type == NULL)
15827 type = read_structure_type (die, cu);
15828
2ddeaf8a
TT
15829 /* When reading a DW_TAG_variant_part, we need to notice when we
15830 read the discriminant member, so we can record it later in the
15831 discriminant_info. */
15832 bool is_variant_part = TYPE_FLAG_DISCRIMINATED_UNION (type);
15833 sect_offset discr_offset;
15834
15835 if (is_variant_part)
15836 {
15837 struct attribute *discr = dwarf2_attr (die, DW_AT_discr, cu);
15838 if (discr == NULL)
15839 {
15840 /* Maybe it's a univariant form, an extension we support.
15841 In this case arrange not to check the offset. */
15842 is_variant_part = false;
15843 }
15844 else if (attr_form_is_ref (discr))
15845 {
15846 struct dwarf2_cu *target_cu = cu;
15847 struct die_info *target_die = follow_die_ref (die, discr, &target_cu);
15848
15849 discr_offset = target_die->sect_off;
15850 }
15851 else
15852 {
b98664d3 15853 complaint (_("DW_AT_discr does not have DIE reference form"
2ddeaf8a
TT
15854 " - DIE at %s [in module %s]"),
15855 sect_offset_str (die->sect_off),
15856 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15857 is_variant_part = false;
15858 }
15859 }
15860
e142c38c 15861 if (die->child != NULL && ! die_is_declaration (die, cu))
c906108c
SS
15862 {
15863 struct field_info fi;
2f4732b0 15864 std::vector<struct symbol *> template_args;
c906108c 15865
639d11d3 15866 child_die = die->child;
c906108c
SS
15867
15868 while (child_die && child_die->tag)
15869 {
2ddeaf8a 15870 handle_struct_member_die (child_die, type, &fi, &template_args, cu);
34eaf542 15871
2ddeaf8a 15872 if (is_variant_part && discr_offset == child_die->sect_off)
be2daae6 15873 fi.fields.back ().variant.is_discriminant = true;
34eaf542 15874
c906108c
SS
15875 child_die = sibling_die (child_die);
15876 }
15877
34eaf542 15878 /* Attach template arguments to type. */
2f4732b0 15879 if (!template_args.empty ())
34eaf542
TT
15880 {
15881 ALLOCATE_CPLUS_STRUCT_TYPE (type);
2f4732b0 15882 TYPE_N_TEMPLATE_ARGUMENTS (type) = template_args.size ();
34eaf542 15883 TYPE_TEMPLATE_ARGUMENTS (type)
8d749320
SM
15884 = XOBNEWVEC (&objfile->objfile_obstack,
15885 struct symbol *,
15886 TYPE_N_TEMPLATE_ARGUMENTS (type));
34eaf542 15887 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
2f4732b0 15888 template_args.data (),
34eaf542
TT
15889 (TYPE_N_TEMPLATE_ARGUMENTS (type)
15890 * sizeof (struct symbol *)));
34eaf542
TT
15891 }
15892
c906108c
SS
15893 /* Attach fields and member functions to the type. */
15894 if (fi.nfields)
e7c27a73 15895 dwarf2_attach_fields_to_type (&fi, type, cu);
be2daae6 15896 if (!fi.fnfieldlists.empty ())
c906108c 15897 {
e7c27a73 15898 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
c906108c 15899
c5aa993b 15900 /* Get the type which refers to the base class (possibly this
c906108c 15901 class itself) which contains the vtable pointer for the current
0d564a31
DJ
15902 class from the DW_AT_containing_type attribute. This use of
15903 DW_AT_containing_type is a GNU extension. */
c906108c 15904
e142c38c 15905 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
c906108c 15906 {
e7c27a73 15907 struct type *t = die_containing_type (die, cu);
c906108c 15908
ae6ae975 15909 set_type_vptr_basetype (type, t);
c906108c
SS
15910 if (type == t)
15911 {
c906108c
SS
15912 int i;
15913
15914 /* Our own class provides vtbl ptr. */
15915 for (i = TYPE_NFIELDS (t) - 1;
15916 i >= TYPE_N_BASECLASSES (t);
15917 --i)
15918 {
0d5cff50 15919 const char *fieldname = TYPE_FIELD_NAME (t, i);
c906108c 15920
1168df01 15921 if (is_vtable_name (fieldname, cu))
c906108c 15922 {
ae6ae975 15923 set_type_vptr_fieldno (type, i);
c906108c
SS
15924 break;
15925 }
15926 }
15927
15928 /* Complain if virtual function table field not found. */
15929 if (i < TYPE_N_BASECLASSES (t))
b98664d3 15930 complaint (_("virtual function table pointer "
3e43a32a 15931 "not found when defining class '%s'"),
e86ca25f 15932 TYPE_NAME (type) ? TYPE_NAME (type) : "");
c906108c
SS
15933 }
15934 else
15935 {
ae6ae975 15936 set_type_vptr_fieldno (type, TYPE_VPTR_FIELDNO (t));
c906108c
SS
15937 }
15938 }
f6235d4c 15939 else if (cu->producer
61012eef 15940 && startswith (cu->producer, "IBM(R) XL C/C++ Advanced Edition"))
f6235d4c
EZ
15941 {
15942 /* The IBM XLC compiler does not provide direct indication
15943 of the containing type, but the vtable pointer is
15944 always named __vfp. */
15945
15946 int i;
15947
15948 for (i = TYPE_NFIELDS (type) - 1;
15949 i >= TYPE_N_BASECLASSES (type);
15950 --i)
15951 {
15952 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
15953 {
ae6ae975
DE
15954 set_type_vptr_fieldno (type, i);
15955 set_type_vptr_basetype (type, type);
f6235d4c
EZ
15956 break;
15957 }
15958 }
15959 }
c906108c 15960 }
98751a41
JK
15961
15962 /* Copy fi.typedef_field_list linked list elements content into the
15963 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
be2daae6 15964 if (!fi.typedef_field_list.empty ())
98751a41 15965 {
be2daae6 15966 int count = fi.typedef_field_list.size ();
98751a41 15967
a0d7a4ff 15968 ALLOCATE_CPLUS_STRUCT_TYPE (type);
98751a41 15969 TYPE_TYPEDEF_FIELD_ARRAY (type)
883fd55a 15970 = ((struct decl_field *)
be2daae6
TT
15971 TYPE_ALLOC (type,
15972 sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * count));
15973 TYPE_TYPEDEF_FIELD_COUNT (type) = count;
6e70227d 15974
be2daae6
TT
15975 for (int i = 0; i < fi.typedef_field_list.size (); ++i)
15976 TYPE_TYPEDEF_FIELD (type, i) = fi.typedef_field_list[i];
98751a41 15977 }
c767944b 15978
883fd55a
KS
15979 /* Copy fi.nested_types_list linked list elements content into the
15980 allocated array TYPE_NESTED_TYPES_ARRAY (type). */
be2daae6 15981 if (!fi.nested_types_list.empty () && cu->language != language_ada)
883fd55a 15982 {
be2daae6 15983 int count = fi.nested_types_list.size ();
883fd55a
KS
15984
15985 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15986 TYPE_NESTED_TYPES_ARRAY (type)
15987 = ((struct decl_field *)
be2daae6
TT
15988 TYPE_ALLOC (type, sizeof (struct decl_field) * count));
15989 TYPE_NESTED_TYPES_COUNT (type) = count;
883fd55a 15990
be2daae6
TT
15991 for (int i = 0; i < fi.nested_types_list.size (); ++i)
15992 TYPE_NESTED_TYPES_FIELD (type, i) = fi.nested_types_list[i];
883fd55a 15993 }
c906108c 15994 }
63d06c5c 15995
bb5ed363 15996 quirk_gcc_member_function_pointer (type, objfile);
c9317f21
TT
15997 if (cu->language == language_rust && die->tag == DW_TAG_union_type)
15998 cu->rust_unions.push_back (type);
0b92b5bb 15999
90aeadfc
DC
16000 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
16001 snapshots) has been known to create a die giving a declaration
16002 for a class that has, as a child, a die giving a definition for a
16003 nested class. So we have to process our children even if the
16004 current die is a declaration. Normally, of course, a declaration
16005 won't have any children at all. */
134d01f1 16006
ca040673
DE
16007 child_die = die->child;
16008
90aeadfc
DC
16009 while (child_die != NULL && child_die->tag)
16010 {
16011 if (child_die->tag == DW_TAG_member
16012 || child_die->tag == DW_TAG_variable
34eaf542
TT
16013 || child_die->tag == DW_TAG_inheritance
16014 || child_die->tag == DW_TAG_template_value_param
16015 || child_die->tag == DW_TAG_template_type_param)
134d01f1 16016 {
90aeadfc 16017 /* Do nothing. */
134d01f1 16018 }
90aeadfc
DC
16019 else
16020 process_die (child_die, cu);
134d01f1 16021
90aeadfc 16022 child_die = sibling_die (child_die);
134d01f1
DJ
16023 }
16024
fa4028e9
JB
16025 /* Do not consider external references. According to the DWARF standard,
16026 these DIEs are identified by the fact that they have no byte_size
16027 attribute, and a declaration attribute. */
16028 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
16029 || !die_is_declaration (die, cu))
c767944b 16030 new_symbol (die, type, cu);
134d01f1
DJ
16031}
16032
55426c9d
JB
16033/* Assuming DIE is an enumeration type, and TYPE is its associated type,
16034 update TYPE using some information only available in DIE's children. */
16035
16036static void
16037update_enumeration_type_from_children (struct die_info *die,
16038 struct type *type,
16039 struct dwarf2_cu *cu)
16040{
60f7655a 16041 struct die_info *child_die;
55426c9d
JB
16042 int unsigned_enum = 1;
16043 int flag_enum = 1;
16044 ULONGEST mask = 0;
55426c9d 16045
8268c778 16046 auto_obstack obstack;
55426c9d 16047
60f7655a
DE
16048 for (child_die = die->child;
16049 child_die != NULL && child_die->tag;
16050 child_die = sibling_die (child_die))
55426c9d
JB
16051 {
16052 struct attribute *attr;
16053 LONGEST value;
16054 const gdb_byte *bytes;
16055 struct dwarf2_locexpr_baton *baton;
16056 const char *name;
60f7655a 16057
55426c9d
JB
16058 if (child_die->tag != DW_TAG_enumerator)
16059 continue;
16060
16061 attr = dwarf2_attr (child_die, DW_AT_const_value, cu);
16062 if (attr == NULL)
16063 continue;
16064
16065 name = dwarf2_name (child_die, cu);
16066 if (name == NULL)
16067 name = "<anonymous enumerator>";
16068
16069 dwarf2_const_value_attr (attr, type, name, &obstack, cu,
16070 &value, &bytes, &baton);
16071 if (value < 0)
16072 {
16073 unsigned_enum = 0;
16074 flag_enum = 0;
16075 }
16076 else if ((mask & value) != 0)
16077 flag_enum = 0;
16078 else
16079 mask |= value;
16080
16081 /* If we already know that the enum type is neither unsigned, nor
16082 a flag type, no need to look at the rest of the enumerates. */
16083 if (!unsigned_enum && !flag_enum)
16084 break;
55426c9d
JB
16085 }
16086
16087 if (unsigned_enum)
16088 TYPE_UNSIGNED (type) = 1;
16089 if (flag_enum)
16090 TYPE_FLAG_ENUM (type) = 1;
55426c9d
JB
16091}
16092
134d01f1
DJ
16093/* Given a DW_AT_enumeration_type die, set its type. We do not
16094 complete the type's fields yet, or create any symbols. */
c906108c 16095
f792889a 16096static struct type *
134d01f1 16097read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16098{
518817b3 16099 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c 16100 struct type *type;
c906108c 16101 struct attribute *attr;
0114d602 16102 const char *name;
134d01f1 16103
348e048f
DE
16104 /* If the definition of this type lives in .debug_types, read that type.
16105 Don't follow DW_AT_specification though, that will take us back up
16106 the chain and we want to go down. */
45e58e77 16107 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
348e048f
DE
16108 if (attr)
16109 {
ac9ec31b 16110 type = get_DW_AT_signature_type (die, attr, cu);
9dc481d3 16111
ac9ec31b 16112 /* The type's CU may not be the same as CU.
02142a6c 16113 Ensure TYPE is recorded with CU in die_type_hash. */
348e048f
DE
16114 return set_die_type (die, type, cu);
16115 }
16116
c906108c
SS
16117 type = alloc_type (objfile);
16118
16119 TYPE_CODE (type) = TYPE_CODE_ENUM;
94af9270 16120 name = dwarf2_full_name (NULL, die, cu);
39cbfefa 16121 if (name != NULL)
e86ca25f 16122 TYPE_NAME (type) = name;
c906108c 16123
0626fc76
TT
16124 attr = dwarf2_attr (die, DW_AT_type, cu);
16125 if (attr != NULL)
16126 {
16127 struct type *underlying_type = die_type (die, cu);
16128
16129 TYPE_TARGET_TYPE (type) = underlying_type;
16130 }
16131
e142c38c 16132 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
16133 if (attr)
16134 {
16135 TYPE_LENGTH (type) = DW_UNSND (attr);
16136 }
16137 else
16138 {
16139 TYPE_LENGTH (type) = 0;
16140 }
16141
2b4424c3
TT
16142 maybe_set_alignment (cu, die, type);
16143
137033e9
JB
16144 /* The enumeration DIE can be incomplete. In Ada, any type can be
16145 declared as private in the package spec, and then defined only
16146 inside the package body. Such types are known as Taft Amendment
16147 Types. When another package uses such a type, an incomplete DIE
16148 may be generated by the compiler. */
02eb380e 16149 if (die_is_declaration (die, cu))
876cecd0 16150 TYPE_STUB (type) = 1;
02eb380e 16151
0626fc76
TT
16152 /* Finish the creation of this type by using the enum's children.
16153 We must call this even when the underlying type has been provided
16154 so that we can determine if we're looking at a "flag" enum. */
55426c9d
JB
16155 update_enumeration_type_from_children (die, type, cu);
16156
0626fc76
TT
16157 /* If this type has an underlying type that is not a stub, then we
16158 may use its attributes. We always use the "unsigned" attribute
16159 in this situation, because ordinarily we guess whether the type
16160 is unsigned -- but the guess can be wrong and the underlying type
16161 can tell us the reality. However, we defer to a local size
16162 attribute if one exists, because this lets the compiler override
16163 the underlying type if needed. */
16164 if (TYPE_TARGET_TYPE (type) != NULL && !TYPE_STUB (TYPE_TARGET_TYPE (type)))
16165 {
16166 TYPE_UNSIGNED (type) = TYPE_UNSIGNED (TYPE_TARGET_TYPE (type));
16167 if (TYPE_LENGTH (type) == 0)
16168 TYPE_LENGTH (type) = TYPE_LENGTH (TYPE_TARGET_TYPE (type));
2b4424c3
TT
16169 if (TYPE_RAW_ALIGN (type) == 0
16170 && TYPE_RAW_ALIGN (TYPE_TARGET_TYPE (type)) != 0)
16171 set_type_align (type, TYPE_RAW_ALIGN (TYPE_TARGET_TYPE (type)));
0626fc76
TT
16172 }
16173
3d567982
TT
16174 TYPE_DECLARED_CLASS (type) = dwarf2_flag_true_p (die, DW_AT_enum_class, cu);
16175
f792889a 16176 return set_die_type (die, type, cu);
134d01f1
DJ
16177}
16178
16179/* Given a pointer to a die which begins an enumeration, process all
16180 the dies that define the members of the enumeration, and create the
16181 symbol for the enumeration type.
16182
16183 NOTE: We reverse the order of the element list. */
16184
16185static void
16186process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
16187{
f792889a 16188 struct type *this_type;
134d01f1 16189
f792889a
DJ
16190 this_type = get_die_type (die, cu);
16191 if (this_type == NULL)
16192 this_type = read_enumeration_type (die, cu);
9dc481d3 16193
639d11d3 16194 if (die->child != NULL)
c906108c 16195 {
9dc481d3
DE
16196 struct die_info *child_die;
16197 struct symbol *sym;
16198 struct field *fields = NULL;
16199 int num_fields = 0;
15d034d0 16200 const char *name;
9dc481d3 16201
639d11d3 16202 child_die = die->child;
c906108c
SS
16203 while (child_die && child_die->tag)
16204 {
16205 if (child_die->tag != DW_TAG_enumerator)
16206 {
e7c27a73 16207 process_die (child_die, cu);
c906108c
SS
16208 }
16209 else
16210 {
39cbfefa
DJ
16211 name = dwarf2_name (child_die, cu);
16212 if (name)
c906108c 16213 {
f792889a 16214 sym = new_symbol (child_die, this_type, cu);
c906108c
SS
16215
16216 if ((num_fields % DW_FIELD_ALLOC_CHUNK) == 0)
16217 {
16218 fields = (struct field *)
16219 xrealloc (fields,
16220 (num_fields + DW_FIELD_ALLOC_CHUNK)
c5aa993b 16221 * sizeof (struct field));
c906108c
SS
16222 }
16223
3567439c 16224 FIELD_NAME (fields[num_fields]) = SYMBOL_LINKAGE_NAME (sym);
c906108c 16225 FIELD_TYPE (fields[num_fields]) = NULL;
14e75d8e 16226 SET_FIELD_ENUMVAL (fields[num_fields], SYMBOL_VALUE (sym));
c906108c
SS
16227 FIELD_BITSIZE (fields[num_fields]) = 0;
16228
16229 num_fields++;
16230 }
16231 }
16232
16233 child_die = sibling_die (child_die);
16234 }
16235
16236 if (num_fields)
16237 {
f792889a
DJ
16238 TYPE_NFIELDS (this_type) = num_fields;
16239 TYPE_FIELDS (this_type) = (struct field *)
16240 TYPE_ALLOC (this_type, sizeof (struct field) * num_fields);
16241 memcpy (TYPE_FIELDS (this_type), fields,
c906108c 16242 sizeof (struct field) * num_fields);
b8c9b27d 16243 xfree (fields);
c906108c 16244 }
c906108c 16245 }
134d01f1 16246
6c83ed52
TT
16247 /* If we are reading an enum from a .debug_types unit, and the enum
16248 is a declaration, and the enum is not the signatured type in the
16249 unit, then we do not want to add a symbol for it. Adding a
16250 symbol would in some cases obscure the true definition of the
16251 enum, giving users an incomplete type when the definition is
16252 actually available. Note that we do not want to do this for all
16253 enums which are just declarations, because C++0x allows forward
16254 enum declarations. */
3019eac3 16255 if (cu->per_cu->is_debug_types
6c83ed52
TT
16256 && die_is_declaration (die, cu))
16257 {
52dc124a 16258 struct signatured_type *sig_type;
6c83ed52 16259
c0f78cd4 16260 sig_type = (struct signatured_type *) cu->per_cu;
9c541725
PA
16261 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
16262 if (sig_type->type_offset_in_section != die->sect_off)
6c83ed52
TT
16263 return;
16264 }
16265
f792889a 16266 new_symbol (die, this_type, cu);
c906108c
SS
16267}
16268
16269/* Extract all information from a DW_TAG_array_type DIE and put it in
16270 the DIE's type field. For now, this only handles one dimensional
16271 arrays. */
16272
f792889a 16273static struct type *
e7c27a73 16274read_array_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16275{
518817b3 16276 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c 16277 struct die_info *child_die;
7e314c57 16278 struct type *type;
c906108c 16279 struct type *element_type, *range_type, *index_type;
c906108c 16280 struct attribute *attr;
15d034d0 16281 const char *name;
a405673c 16282 struct dynamic_prop *byte_stride_prop = NULL;
dc53a7ad 16283 unsigned int bit_stride = 0;
c906108c 16284
e7c27a73 16285 element_type = die_type (die, cu);
c906108c 16286
7e314c57
JK
16287 /* The die_type call above may have already set the type for this DIE. */
16288 type = get_die_type (die, cu);
16289 if (type)
16290 return type;
16291
dc53a7ad
JB
16292 attr = dwarf2_attr (die, DW_AT_byte_stride, cu);
16293 if (attr != NULL)
a405673c
JB
16294 {
16295 int stride_ok;
16296
16297 byte_stride_prop
16298 = (struct dynamic_prop *) alloca (sizeof (struct dynamic_prop));
16299 stride_ok = attr_to_dynamic_prop (attr, die, cu, byte_stride_prop);
16300 if (!stride_ok)
16301 {
b98664d3 16302 complaint (_("unable to read array DW_AT_byte_stride "
9d8780f0
SM
16303 " - DIE at %s [in module %s]"),
16304 sect_offset_str (die->sect_off),
518817b3 16305 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
a405673c
JB
16306 /* Ignore this attribute. We will likely not be able to print
16307 arrays of this type correctly, but there is little we can do
16308 to help if we cannot read the attribute's value. */
16309 byte_stride_prop = NULL;
16310 }
16311 }
dc53a7ad
JB
16312
16313 attr = dwarf2_attr (die, DW_AT_bit_stride, cu);
16314 if (attr != NULL)
16315 bit_stride = DW_UNSND (attr);
16316
c906108c
SS
16317 /* Irix 6.2 native cc creates array types without children for
16318 arrays with unspecified length. */
639d11d3 16319 if (die->child == NULL)
c906108c 16320 {
46bf5051 16321 index_type = objfile_type (objfile)->builtin_int;
0c9c3474 16322 range_type = create_static_range_type (NULL, index_type, 0, -1);
dc53a7ad 16323 type = create_array_type_with_stride (NULL, element_type, range_type,
a405673c 16324 byte_stride_prop, bit_stride);
f792889a 16325 return set_die_type (die, type, cu);
c906108c
SS
16326 }
16327
791afaa2 16328 std::vector<struct type *> range_types;
639d11d3 16329 child_die = die->child;
c906108c
SS
16330 while (child_die && child_die->tag)
16331 {
16332 if (child_die->tag == DW_TAG_subrange_type)
16333 {
f792889a 16334 struct type *child_type = read_type_die (child_die, cu);
9a619af0 16335
f792889a 16336 if (child_type != NULL)
a02abb62 16337 {
0963b4bd
MS
16338 /* The range type was succesfully read. Save it for the
16339 array type creation. */
791afaa2 16340 range_types.push_back (child_type);
a02abb62 16341 }
c906108c
SS
16342 }
16343 child_die = sibling_die (child_die);
16344 }
16345
16346 /* Dwarf2 dimensions are output from left to right, create the
16347 necessary array types in backwards order. */
7ca2d3a3 16348
c906108c 16349 type = element_type;
7ca2d3a3
DL
16350
16351 if (read_array_order (die, cu) == DW_ORD_col_major)
16352 {
16353 int i = 0;
9a619af0 16354
791afaa2 16355 while (i < range_types.size ())
dc53a7ad 16356 type = create_array_type_with_stride (NULL, type, range_types[i++],
a405673c 16357 byte_stride_prop, bit_stride);
7ca2d3a3
DL
16358 }
16359 else
16360 {
791afaa2 16361 size_t ndim = range_types.size ();
7ca2d3a3 16362 while (ndim-- > 0)
dc53a7ad 16363 type = create_array_type_with_stride (NULL, type, range_types[ndim],
a405673c 16364 byte_stride_prop, bit_stride);
7ca2d3a3 16365 }
c906108c 16366
f5f8a009
EZ
16367 /* Understand Dwarf2 support for vector types (like they occur on
16368 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
16369 array type. This is not part of the Dwarf2/3 standard yet, but a
16370 custom vendor extension. The main difference between a regular
16371 array and the vector variant is that vectors are passed by value
16372 to functions. */
e142c38c 16373 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
f5f8a009 16374 if (attr)
ea37ba09 16375 make_vector_type (type);
f5f8a009 16376
dbc98a8b
KW
16377 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
16378 implementation may choose to implement triple vectors using this
16379 attribute. */
16380 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16381 if (attr)
16382 {
16383 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
16384 TYPE_LENGTH (type) = DW_UNSND (attr);
16385 else
b98664d3 16386 complaint (_("DW_AT_byte_size for array type smaller "
3e43a32a 16387 "than the total size of elements"));
dbc98a8b
KW
16388 }
16389
39cbfefa
DJ
16390 name = dwarf2_name (die, cu);
16391 if (name)
16392 TYPE_NAME (type) = name;
6e70227d 16393
2b4424c3
TT
16394 maybe_set_alignment (cu, die, type);
16395
0963b4bd 16396 /* Install the type in the die. */
7e314c57
JK
16397 set_die_type (die, type, cu);
16398
16399 /* set_die_type should be already done. */
b4ba55a1
JB
16400 set_descriptive_type (type, die, cu);
16401
7e314c57 16402 return type;
c906108c
SS
16403}
16404
7ca2d3a3 16405static enum dwarf_array_dim_ordering
6e70227d 16406read_array_order (struct die_info *die, struct dwarf2_cu *cu)
7ca2d3a3
DL
16407{
16408 struct attribute *attr;
16409
16410 attr = dwarf2_attr (die, DW_AT_ordering, cu);
16411
aead7601
SM
16412 if (attr)
16413 return (enum dwarf_array_dim_ordering) DW_SND (attr);
7ca2d3a3 16414
0963b4bd
MS
16415 /* GNU F77 is a special case, as at 08/2004 array type info is the
16416 opposite order to the dwarf2 specification, but data is still
16417 laid out as per normal fortran.
7ca2d3a3 16418
0963b4bd
MS
16419 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
16420 version checking. */
7ca2d3a3 16421
905e0470
PM
16422 if (cu->language == language_fortran
16423 && cu->producer && strstr (cu->producer, "GNU F77"))
7ca2d3a3
DL
16424 {
16425 return DW_ORD_row_major;
16426 }
16427
6e70227d 16428 switch (cu->language_defn->la_array_ordering)
7ca2d3a3
DL
16429 {
16430 case array_column_major:
16431 return DW_ORD_col_major;
16432 case array_row_major:
16433 default:
16434 return DW_ORD_row_major;
16435 };
16436}
16437
72019c9c 16438/* Extract all information from a DW_TAG_set_type DIE and put it in
0963b4bd 16439 the DIE's type field. */
72019c9c 16440
f792889a 16441static struct type *
72019c9c
GM
16442read_set_type (struct die_info *die, struct dwarf2_cu *cu)
16443{
7e314c57
JK
16444 struct type *domain_type, *set_type;
16445 struct attribute *attr;
f792889a 16446
7e314c57
JK
16447 domain_type = die_type (die, cu);
16448
16449 /* The die_type call above may have already set the type for this DIE. */
16450 set_type = get_die_type (die, cu);
16451 if (set_type)
16452 return set_type;
16453
16454 set_type = create_set_type (NULL, domain_type);
16455
16456 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
d09039dd
PM
16457 if (attr)
16458 TYPE_LENGTH (set_type) = DW_UNSND (attr);
7e314c57 16459
2b4424c3
TT
16460 maybe_set_alignment (cu, die, set_type);
16461
f792889a 16462 return set_die_type (die, set_type, cu);
72019c9c 16463}
7ca2d3a3 16464
0971de02
TT
16465/* A helper for read_common_block that creates a locexpr baton.
16466 SYM is the symbol which we are marking as computed.
16467 COMMON_DIE is the DIE for the common block.
16468 COMMON_LOC is the location expression attribute for the common
16469 block itself.
16470 MEMBER_LOC is the location expression attribute for the particular
16471 member of the common block that we are processing.
16472 CU is the CU from which the above come. */
16473
16474static void
16475mark_common_block_symbol_computed (struct symbol *sym,
16476 struct die_info *common_die,
16477 struct attribute *common_loc,
16478 struct attribute *member_loc,
16479 struct dwarf2_cu *cu)
16480{
518817b3
SM
16481 struct dwarf2_per_objfile *dwarf2_per_objfile
16482 = cu->per_cu->dwarf2_per_objfile;
0971de02
TT
16483 struct objfile *objfile = dwarf2_per_objfile->objfile;
16484 struct dwarf2_locexpr_baton *baton;
16485 gdb_byte *ptr;
16486 unsigned int cu_off;
16487 enum bfd_endian byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
16488 LONGEST offset = 0;
16489
16490 gdb_assert (common_loc && member_loc);
16491 gdb_assert (attr_form_is_block (common_loc));
16492 gdb_assert (attr_form_is_block (member_loc)
16493 || attr_form_is_constant (member_loc));
16494
8d749320 16495 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
0971de02
TT
16496 baton->per_cu = cu->per_cu;
16497 gdb_assert (baton->per_cu);
16498
16499 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
16500
16501 if (attr_form_is_constant (member_loc))
16502 {
16503 offset = dwarf2_get_attr_constant_value (member_loc, 0);
16504 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
16505 }
16506 else
16507 baton->size += DW_BLOCK (member_loc)->size;
16508
224c3ddb 16509 ptr = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, baton->size);
0971de02
TT
16510 baton->data = ptr;
16511
16512 *ptr++ = DW_OP_call4;
9c541725 16513 cu_off = common_die->sect_off - cu->per_cu->sect_off;
0971de02
TT
16514 store_unsigned_integer (ptr, 4, byte_order, cu_off);
16515 ptr += 4;
16516
16517 if (attr_form_is_constant (member_loc))
16518 {
16519 *ptr++ = DW_OP_addr;
16520 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
16521 ptr += cu->header.addr_size;
16522 }
16523 else
16524 {
16525 /* We have to copy the data here, because DW_OP_call4 will only
16526 use a DW_AT_location attribute. */
16527 memcpy (ptr, DW_BLOCK (member_loc)->data, DW_BLOCK (member_loc)->size);
16528 ptr += DW_BLOCK (member_loc)->size;
16529 }
16530
16531 *ptr++ = DW_OP_plus;
16532 gdb_assert (ptr - baton->data == baton->size);
16533
0971de02 16534 SYMBOL_LOCATION_BATON (sym) = baton;
f1e6e072 16535 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
0971de02
TT
16536}
16537
4357ac6c
TT
16538/* Create appropriate locally-scoped variables for all the
16539 DW_TAG_common_block entries. Also create a struct common_block
16540 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
16541 is used to sepate the common blocks name namespace from regular
16542 variable names. */
c906108c
SS
16543
16544static void
e7c27a73 16545read_common_block (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16546{
0971de02
TT
16547 struct attribute *attr;
16548
16549 attr = dwarf2_attr (die, DW_AT_location, cu);
16550 if (attr)
16551 {
16552 /* Support the .debug_loc offsets. */
16553 if (attr_form_is_block (attr))
16554 {
16555 /* Ok. */
16556 }
16557 else if (attr_form_is_section_offset (attr))
16558 {
16559 dwarf2_complex_location_expr_complaint ();
16560 attr = NULL;
16561 }
16562 else
16563 {
16564 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
16565 "common block member");
16566 attr = NULL;
16567 }
16568 }
16569
639d11d3 16570 if (die->child != NULL)
c906108c 16571 {
518817b3 16572 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
4357ac6c
TT
16573 struct die_info *child_die;
16574 size_t n_entries = 0, size;
16575 struct common_block *common_block;
16576 struct symbol *sym;
74ac6d43 16577
4357ac6c
TT
16578 for (child_die = die->child;
16579 child_die && child_die->tag;
16580 child_die = sibling_die (child_die))
16581 ++n_entries;
16582
16583 size = (sizeof (struct common_block)
16584 + (n_entries - 1) * sizeof (struct symbol *));
224c3ddb
SM
16585 common_block
16586 = (struct common_block *) obstack_alloc (&objfile->objfile_obstack,
16587 size);
4357ac6c
TT
16588 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
16589 common_block->n_entries = 0;
16590
16591 for (child_die = die->child;
16592 child_die && child_die->tag;
16593 child_die = sibling_die (child_die))
16594 {
16595 /* Create the symbol in the DW_TAG_common_block block in the current
16596 symbol scope. */
e7c27a73 16597 sym = new_symbol (child_die, NULL, cu);
0971de02
TT
16598 if (sym != NULL)
16599 {
16600 struct attribute *member_loc;
16601
16602 common_block->contents[common_block->n_entries++] = sym;
16603
16604 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
16605 cu);
16606 if (member_loc)
16607 {
16608 /* GDB has handled this for a long time, but it is
16609 not specified by DWARF. It seems to have been
16610 emitted by gfortran at least as recently as:
16611 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
b98664d3 16612 complaint (_("Variable in common block has "
0971de02 16613 "DW_AT_data_member_location "
9d8780f0
SM
16614 "- DIE at %s [in module %s]"),
16615 sect_offset_str (child_die->sect_off),
518817b3 16616 objfile_name (objfile));
0971de02
TT
16617
16618 if (attr_form_is_section_offset (member_loc))
16619 dwarf2_complex_location_expr_complaint ();
16620 else if (attr_form_is_constant (member_loc)
16621 || attr_form_is_block (member_loc))
16622 {
16623 if (attr)
16624 mark_common_block_symbol_computed (sym, die, attr,
16625 member_loc, cu);
16626 }
16627 else
16628 dwarf2_complex_location_expr_complaint ();
16629 }
16630 }
c906108c 16631 }
4357ac6c
TT
16632
16633 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
16634 SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
c906108c
SS
16635 }
16636}
16637
0114d602 16638/* Create a type for a C++ namespace. */
d9fa45fe 16639
0114d602
DJ
16640static struct type *
16641read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
d9fa45fe 16642{
518817b3 16643 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
0114d602 16644 const char *previous_prefix, *name;
9219021c 16645 int is_anonymous;
0114d602
DJ
16646 struct type *type;
16647
16648 /* For extensions, reuse the type of the original namespace. */
16649 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
16650 {
16651 struct die_info *ext_die;
16652 struct dwarf2_cu *ext_cu = cu;
9a619af0 16653
0114d602
DJ
16654 ext_die = dwarf2_extension (die, &ext_cu);
16655 type = read_type_die (ext_die, ext_cu);
9dc481d3
DE
16656
16657 /* EXT_CU may not be the same as CU.
02142a6c 16658 Ensure TYPE is recorded with CU in die_type_hash. */
0114d602
DJ
16659 return set_die_type (die, type, cu);
16660 }
9219021c 16661
e142c38c 16662 name = namespace_name (die, &is_anonymous, cu);
9219021c
DC
16663
16664 /* Now build the name of the current namespace. */
16665
0114d602
DJ
16666 previous_prefix = determine_prefix (die, cu);
16667 if (previous_prefix[0] != '\0')
16668 name = typename_concat (&objfile->objfile_obstack,
f55ee35c 16669 previous_prefix, name, 0, cu);
0114d602
DJ
16670
16671 /* Create the type. */
19f392bc 16672 type = init_type (objfile, TYPE_CODE_NAMESPACE, 0, name);
0114d602 16673
60531b24 16674 return set_die_type (die, type, cu);
0114d602
DJ
16675}
16676
22cee43f 16677/* Read a namespace scope. */
0114d602
DJ
16678
16679static void
16680read_namespace (struct die_info *die, struct dwarf2_cu *cu)
16681{
518817b3 16682 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
0114d602 16683 int is_anonymous;
9219021c 16684
5c4e30ca
DC
16685 /* Add a symbol associated to this if we haven't seen the namespace
16686 before. Also, add a using directive if it's an anonymous
16687 namespace. */
9219021c 16688
f2f0e013 16689 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
5c4e30ca
DC
16690 {
16691 struct type *type;
16692
0114d602 16693 type = read_type_die (die, cu);
e7c27a73 16694 new_symbol (die, type, cu);
5c4e30ca 16695
e8e80198 16696 namespace_name (die, &is_anonymous, cu);
5c4e30ca 16697 if (is_anonymous)
0114d602
DJ
16698 {
16699 const char *previous_prefix = determine_prefix (die, cu);
9a619af0 16700
eb1e02fd 16701 std::vector<const char *> excludes;
22cee43f
PMR
16702 add_using_directive (using_directives (cu->language),
16703 previous_prefix, TYPE_NAME (type), NULL,
eb1e02fd 16704 NULL, excludes, 0, &objfile->objfile_obstack);
0114d602 16705 }
5c4e30ca 16706 }
9219021c 16707
639d11d3 16708 if (die->child != NULL)
d9fa45fe 16709 {
639d11d3 16710 struct die_info *child_die = die->child;
6e70227d 16711
d9fa45fe
DC
16712 while (child_die && child_die->tag)
16713 {
e7c27a73 16714 process_die (child_die, cu);
d9fa45fe
DC
16715 child_die = sibling_die (child_die);
16716 }
16717 }
38d518c9
EZ
16718}
16719
f55ee35c
JK
16720/* Read a Fortran module as type. This DIE can be only a declaration used for
16721 imported module. Still we need that type as local Fortran "use ... only"
16722 declaration imports depend on the created type in determine_prefix. */
16723
16724static struct type *
16725read_module_type (struct die_info *die, struct dwarf2_cu *cu)
16726{
518817b3 16727 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15d034d0 16728 const char *module_name;
f55ee35c
JK
16729 struct type *type;
16730
16731 module_name = dwarf2_name (die, cu);
16732 if (!module_name)
b98664d3 16733 complaint (_("DW_TAG_module has no name, offset %s"),
9d8780f0 16734 sect_offset_str (die->sect_off));
19f392bc 16735 type = init_type (objfile, TYPE_CODE_MODULE, 0, module_name);
f55ee35c 16736
f55ee35c
JK
16737 return set_die_type (die, type, cu);
16738}
16739
5d7cb8df
JK
16740/* Read a Fortran module. */
16741
16742static void
16743read_module (struct die_info *die, struct dwarf2_cu *cu)
16744{
16745 struct die_info *child_die = die->child;
530e8392
KB
16746 struct type *type;
16747
16748 type = read_type_die (die, cu);
16749 new_symbol (die, type, cu);
5d7cb8df 16750
5d7cb8df
JK
16751 while (child_die && child_die->tag)
16752 {
16753 process_die (child_die, cu);
16754 child_die = sibling_die (child_die);
16755 }
16756}
16757
38d518c9
EZ
16758/* Return the name of the namespace represented by DIE. Set
16759 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
16760 namespace. */
16761
16762static const char *
e142c38c 16763namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
38d518c9
EZ
16764{
16765 struct die_info *current_die;
16766 const char *name = NULL;
16767
16768 /* Loop through the extensions until we find a name. */
16769
16770 for (current_die = die;
16771 current_die != NULL;
f2f0e013 16772 current_die = dwarf2_extension (die, &cu))
38d518c9 16773 {
96553a0c
DE
16774 /* We don't use dwarf2_name here so that we can detect the absence
16775 of a name -> anonymous namespace. */
7d45c7c3 16776 name = dwarf2_string_attr (die, DW_AT_name, cu);
96553a0c 16777
38d518c9
EZ
16778 if (name != NULL)
16779 break;
16780 }
16781
16782 /* Is it an anonymous namespace? */
16783
16784 *is_anonymous = (name == NULL);
16785 if (*is_anonymous)
2b1dbab0 16786 name = CP_ANONYMOUS_NAMESPACE_STR;
38d518c9
EZ
16787
16788 return name;
d9fa45fe
DC
16789}
16790
c906108c
SS
16791/* Extract all information from a DW_TAG_pointer_type DIE and add to
16792 the user defined type vector. */
16793
f792889a 16794static struct type *
e7c27a73 16795read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16796{
518817b3
SM
16797 struct gdbarch *gdbarch
16798 = get_objfile_arch (cu->per_cu->dwarf2_per_objfile->objfile);
e7c27a73 16799 struct comp_unit_head *cu_header = &cu->header;
c906108c 16800 struct type *type;
8b2dbe47
KB
16801 struct attribute *attr_byte_size;
16802 struct attribute *attr_address_class;
16803 int byte_size, addr_class;
7e314c57
JK
16804 struct type *target_type;
16805
16806 target_type = die_type (die, cu);
c906108c 16807
7e314c57
JK
16808 /* The die_type call above may have already set the type for this DIE. */
16809 type = get_die_type (die, cu);
16810 if (type)
16811 return type;
16812
16813 type = lookup_pointer_type (target_type);
8b2dbe47 16814
e142c38c 16815 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
8b2dbe47
KB
16816 if (attr_byte_size)
16817 byte_size = DW_UNSND (attr_byte_size);
c906108c 16818 else
8b2dbe47
KB
16819 byte_size = cu_header->addr_size;
16820
e142c38c 16821 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
8b2dbe47
KB
16822 if (attr_address_class)
16823 addr_class = DW_UNSND (attr_address_class);
16824 else
16825 addr_class = DW_ADDR_none;
16826
2b4424c3
TT
16827 ULONGEST alignment = get_alignment (cu, die);
16828
16829 /* If the pointer size, alignment, or address class is different
16830 than the default, create a type variant marked as such and set
16831 the length accordingly. */
16832 if (TYPE_LENGTH (type) != byte_size
16833 || (alignment != 0 && TYPE_RAW_ALIGN (type) != 0
16834 && alignment != TYPE_RAW_ALIGN (type))
16835 || addr_class != DW_ADDR_none)
c906108c 16836 {
5e2b427d 16837 if (gdbarch_address_class_type_flags_p (gdbarch))
8b2dbe47
KB
16838 {
16839 int type_flags;
16840
849957d9 16841 type_flags = gdbarch_address_class_type_flags
5e2b427d 16842 (gdbarch, byte_size, addr_class);
876cecd0
TT
16843 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
16844 == 0);
8b2dbe47
KB
16845 type = make_type_with_address_space (type, type_flags);
16846 }
16847 else if (TYPE_LENGTH (type) != byte_size)
16848 {
b98664d3 16849 complaint (_("invalid pointer size %d"), byte_size);
8b2dbe47 16850 }
2b4424c3
TT
16851 else if (TYPE_RAW_ALIGN (type) != alignment)
16852 {
b98664d3 16853 complaint (_("Invalid DW_AT_alignment"
2b4424c3
TT
16854 " - DIE at %s [in module %s]"),
16855 sect_offset_str (die->sect_off),
16856 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
16857 }
6e70227d 16858 else
9a619af0
MS
16859 {
16860 /* Should we also complain about unhandled address classes? */
16861 }
c906108c 16862 }
8b2dbe47
KB
16863
16864 TYPE_LENGTH (type) = byte_size;
2b4424c3 16865 set_type_align (type, alignment);
f792889a 16866 return set_die_type (die, type, cu);
c906108c
SS
16867}
16868
16869/* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
16870 the user defined type vector. */
16871
f792889a 16872static struct type *
e7c27a73 16873read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
16874{
16875 struct type *type;
16876 struct type *to_type;
16877 struct type *domain;
16878
e7c27a73
DJ
16879 to_type = die_type (die, cu);
16880 domain = die_containing_type (die, cu);
0d5de010 16881
7e314c57
JK
16882 /* The calls above may have already set the type for this DIE. */
16883 type = get_die_type (die, cu);
16884 if (type)
16885 return type;
16886
0d5de010
DJ
16887 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
16888 type = lookup_methodptr_type (to_type);
7078baeb
TT
16889 else if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_FUNC)
16890 {
518817b3
SM
16891 struct type *new_type
16892 = alloc_type (cu->per_cu->dwarf2_per_objfile->objfile);
7078baeb
TT
16893
16894 smash_to_method_type (new_type, domain, TYPE_TARGET_TYPE (to_type),
16895 TYPE_FIELDS (to_type), TYPE_NFIELDS (to_type),
16896 TYPE_VARARGS (to_type));
16897 type = lookup_methodptr_type (new_type);
16898 }
0d5de010
DJ
16899 else
16900 type = lookup_memberptr_type (to_type, domain);
c906108c 16901
f792889a 16902 return set_die_type (die, type, cu);
c906108c
SS
16903}
16904
4297a3f0 16905/* Extract all information from a DW_TAG_{rvalue_,}reference_type DIE and add to
c906108c
SS
16906 the user defined type vector. */
16907
f792889a 16908static struct type *
4297a3f0
AV
16909read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu,
16910 enum type_code refcode)
c906108c 16911{
e7c27a73 16912 struct comp_unit_head *cu_header = &cu->header;
7e314c57 16913 struct type *type, *target_type;
c906108c
SS
16914 struct attribute *attr;
16915
4297a3f0
AV
16916 gdb_assert (refcode == TYPE_CODE_REF || refcode == TYPE_CODE_RVALUE_REF);
16917
7e314c57
JK
16918 target_type = die_type (die, cu);
16919
16920 /* The die_type call above may have already set the type for this DIE. */
16921 type = get_die_type (die, cu);
16922 if (type)
16923 return type;
16924
4297a3f0 16925 type = lookup_reference_type (target_type, refcode);
e142c38c 16926 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
16927 if (attr)
16928 {
16929 TYPE_LENGTH (type) = DW_UNSND (attr);
16930 }
16931 else
16932 {
107d2387 16933 TYPE_LENGTH (type) = cu_header->addr_size;
c906108c 16934 }
2b4424c3 16935 maybe_set_alignment (cu, die, type);
f792889a 16936 return set_die_type (die, type, cu);
c906108c
SS
16937}
16938
cf363f18
MW
16939/* Add the given cv-qualifiers to the element type of the array. GCC
16940 outputs DWARF type qualifiers that apply to an array, not the
16941 element type. But GDB relies on the array element type to carry
16942 the cv-qualifiers. This mimics section 6.7.3 of the C99
16943 specification. */
16944
16945static struct type *
16946add_array_cv_type (struct die_info *die, struct dwarf2_cu *cu,
16947 struct type *base_type, int cnst, int voltl)
16948{
16949 struct type *el_type, *inner_array;
16950
16951 base_type = copy_type (base_type);
16952 inner_array = base_type;
16953
16954 while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
16955 {
16956 TYPE_TARGET_TYPE (inner_array) =
16957 copy_type (TYPE_TARGET_TYPE (inner_array));
16958 inner_array = TYPE_TARGET_TYPE (inner_array);
16959 }
16960
16961 el_type = TYPE_TARGET_TYPE (inner_array);
16962 cnst |= TYPE_CONST (el_type);
16963 voltl |= TYPE_VOLATILE (el_type);
16964 TYPE_TARGET_TYPE (inner_array) = make_cv_type (cnst, voltl, el_type, NULL);
16965
16966 return set_die_type (die, base_type, cu);
16967}
16968
f792889a 16969static struct type *
e7c27a73 16970read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16971{
f792889a 16972 struct type *base_type, *cv_type;
c906108c 16973
e7c27a73 16974 base_type = die_type (die, cu);
7e314c57
JK
16975
16976 /* The die_type call above may have already set the type for this DIE. */
16977 cv_type = get_die_type (die, cu);
16978 if (cv_type)
16979 return cv_type;
16980
2f608a3a
KW
16981 /* In case the const qualifier is applied to an array type, the element type
16982 is so qualified, not the array type (section 6.7.3 of C99). */
16983 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
cf363f18 16984 return add_array_cv_type (die, cu, base_type, 1, 0);
2f608a3a 16985
f792889a
DJ
16986 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
16987 return set_die_type (die, cv_type, cu);
c906108c
SS
16988}
16989
f792889a 16990static struct type *
e7c27a73 16991read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16992{
f792889a 16993 struct type *base_type, *cv_type;
c906108c 16994
e7c27a73 16995 base_type = die_type (die, cu);
7e314c57
JK
16996
16997 /* The die_type call above may have already set the type for this DIE. */
16998 cv_type = get_die_type (die, cu);
16999 if (cv_type)
17000 return cv_type;
17001
cf363f18
MW
17002 /* In case the volatile qualifier is applied to an array type, the
17003 element type is so qualified, not the array type (section 6.7.3
17004 of C99). */
17005 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
17006 return add_array_cv_type (die, cu, base_type, 0, 1);
17007
f792889a
DJ
17008 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
17009 return set_die_type (die, cv_type, cu);
c906108c
SS
17010}
17011
06d66ee9
TT
17012/* Handle DW_TAG_restrict_type. */
17013
17014static struct type *
17015read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
17016{
17017 struct type *base_type, *cv_type;
17018
17019 base_type = die_type (die, cu);
17020
17021 /* The die_type call above may have already set the type for this DIE. */
17022 cv_type = get_die_type (die, cu);
17023 if (cv_type)
17024 return cv_type;
17025
17026 cv_type = make_restrict_type (base_type);
17027 return set_die_type (die, cv_type, cu);
17028}
17029
a2c2acaf
MW
17030/* Handle DW_TAG_atomic_type. */
17031
17032static struct type *
17033read_tag_atomic_type (struct die_info *die, struct dwarf2_cu *cu)
17034{
17035 struct type *base_type, *cv_type;
17036
17037 base_type = die_type (die, cu);
17038
17039 /* The die_type call above may have already set the type for this DIE. */
17040 cv_type = get_die_type (die, cu);
17041 if (cv_type)
17042 return cv_type;
17043
17044 cv_type = make_atomic_type (base_type);
17045 return set_die_type (die, cv_type, cu);
17046}
17047
c906108c
SS
17048/* Extract all information from a DW_TAG_string_type DIE and add to
17049 the user defined type vector. It isn't really a user defined type,
17050 but it behaves like one, with other DIE's using an AT_user_def_type
17051 attribute to reference it. */
17052
f792889a 17053static struct type *
e7c27a73 17054read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17055{
518817b3 17056 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
3b7538c0 17057 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
17058 struct type *type, *range_type, *index_type, *char_type;
17059 struct attribute *attr;
17060 unsigned int length;
17061
e142c38c 17062 attr = dwarf2_attr (die, DW_AT_string_length, cu);
c906108c
SS
17063 if (attr)
17064 {
17065 length = DW_UNSND (attr);
17066 }
17067 else
17068 {
0963b4bd 17069 /* Check for the DW_AT_byte_size attribute. */
e142c38c 17070 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
b21b22e0
PS
17071 if (attr)
17072 {
17073 length = DW_UNSND (attr);
17074 }
17075 else
17076 {
17077 length = 1;
17078 }
c906108c 17079 }
6ccb9162 17080
46bf5051 17081 index_type = objfile_type (objfile)->builtin_int;
0c9c3474 17082 range_type = create_static_range_type (NULL, index_type, 1, length);
3b7538c0
UW
17083 char_type = language_string_char_type (cu->language_defn, gdbarch);
17084 type = create_string_type (NULL, char_type, range_type);
6ccb9162 17085
f792889a 17086 return set_die_type (die, type, cu);
c906108c
SS
17087}
17088
4d804846
JB
17089/* Assuming that DIE corresponds to a function, returns nonzero
17090 if the function is prototyped. */
17091
17092static int
17093prototyped_function_p (struct die_info *die, struct dwarf2_cu *cu)
17094{
17095 struct attribute *attr;
17096
17097 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
17098 if (attr && (DW_UNSND (attr) != 0))
17099 return 1;
17100
17101 /* The DWARF standard implies that the DW_AT_prototyped attribute
17102 is only meaninful for C, but the concept also extends to other
17103 languages that allow unprototyped functions (Eg: Objective C).
17104 For all other languages, assume that functions are always
17105 prototyped. */
17106 if (cu->language != language_c
17107 && cu->language != language_objc
17108 && cu->language != language_opencl)
17109 return 1;
17110
17111 /* RealView does not emit DW_AT_prototyped. We can not distinguish
17112 prototyped and unprototyped functions; default to prototyped,
17113 since that is more common in modern code (and RealView warns
17114 about unprototyped functions). */
17115 if (producer_is_realview (cu->producer))
17116 return 1;
17117
17118 return 0;
17119}
17120
c906108c
SS
17121/* Handle DIES due to C code like:
17122
17123 struct foo
c5aa993b
JM
17124 {
17125 int (*funcp)(int a, long l);
17126 int b;
17127 };
c906108c 17128
0963b4bd 17129 ('funcp' generates a DW_TAG_subroutine_type DIE). */
c906108c 17130
f792889a 17131static struct type *
e7c27a73 17132read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17133{
518817b3 17134 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
0963b4bd
MS
17135 struct type *type; /* Type that this function returns. */
17136 struct type *ftype; /* Function that returns above type. */
c906108c
SS
17137 struct attribute *attr;
17138
e7c27a73 17139 type = die_type (die, cu);
7e314c57
JK
17140
17141 /* The die_type call above may have already set the type for this DIE. */
17142 ftype = get_die_type (die, cu);
17143 if (ftype)
17144 return ftype;
17145
0c8b41f1 17146 ftype = lookup_function_type (type);
c906108c 17147
4d804846 17148 if (prototyped_function_p (die, cu))
a6c727b2 17149 TYPE_PROTOTYPED (ftype) = 1;
c906108c 17150
c055b101
CV
17151 /* Store the calling convention in the type if it's available in
17152 the subroutine die. Otherwise set the calling convention to
17153 the default value DW_CC_normal. */
17154 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
54fcddd0
UW
17155 if (attr)
17156 TYPE_CALLING_CONVENTION (ftype) = DW_UNSND (attr);
17157 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
17158 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
17159 else
17160 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
76c10ea2 17161
743649fd
MW
17162 /* Record whether the function returns normally to its caller or not
17163 if the DWARF producer set that information. */
17164 attr = dwarf2_attr (die, DW_AT_noreturn, cu);
17165 if (attr && (DW_UNSND (attr) != 0))
17166 TYPE_NO_RETURN (ftype) = 1;
17167
76c10ea2
GM
17168 /* We need to add the subroutine type to the die immediately so
17169 we don't infinitely recurse when dealing with parameters
0963b4bd 17170 declared as the same subroutine type. */
76c10ea2 17171 set_die_type (die, ftype, cu);
6e70227d 17172
639d11d3 17173 if (die->child != NULL)
c906108c 17174 {
bb5ed363 17175 struct type *void_type = objfile_type (objfile)->builtin_void;
c906108c 17176 struct die_info *child_die;
8072405b 17177 int nparams, iparams;
c906108c
SS
17178
17179 /* Count the number of parameters.
17180 FIXME: GDB currently ignores vararg functions, but knows about
17181 vararg member functions. */
8072405b 17182 nparams = 0;
639d11d3 17183 child_die = die->child;
c906108c
SS
17184 while (child_die && child_die->tag)
17185 {
17186 if (child_die->tag == DW_TAG_formal_parameter)
17187 nparams++;
17188 else if (child_die->tag == DW_TAG_unspecified_parameters)
876cecd0 17189 TYPE_VARARGS (ftype) = 1;
c906108c
SS
17190 child_die = sibling_die (child_die);
17191 }
17192
17193 /* Allocate storage for parameters and fill them in. */
17194 TYPE_NFIELDS (ftype) = nparams;
17195 TYPE_FIELDS (ftype) = (struct field *)
ae5a43e0 17196 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
c906108c 17197
8072405b
JK
17198 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
17199 even if we error out during the parameters reading below. */
17200 for (iparams = 0; iparams < nparams; iparams++)
17201 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
17202
17203 iparams = 0;
639d11d3 17204 child_die = die->child;
c906108c
SS
17205 while (child_die && child_die->tag)
17206 {
17207 if (child_die->tag == DW_TAG_formal_parameter)
17208 {
3ce3b1ba
PA
17209 struct type *arg_type;
17210
17211 /* DWARF version 2 has no clean way to discern C++
17212 static and non-static member functions. G++ helps
17213 GDB by marking the first parameter for non-static
17214 member functions (which is the this pointer) as
17215 artificial. We pass this information to
17216 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
17217
17218 DWARF version 3 added DW_AT_object_pointer, which GCC
17219 4.5 does not yet generate. */
e142c38c 17220 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
c906108c
SS
17221 if (attr)
17222 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
17223 else
9c37b5ae 17224 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
3ce3b1ba
PA
17225 arg_type = die_type (child_die, cu);
17226
17227 /* RealView does not mark THIS as const, which the testsuite
17228 expects. GCC marks THIS as const in method definitions,
17229 but not in the class specifications (GCC PR 43053). */
17230 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
17231 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
17232 {
17233 int is_this = 0;
17234 struct dwarf2_cu *arg_cu = cu;
17235 const char *name = dwarf2_name (child_die, cu);
17236
17237 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
17238 if (attr)
17239 {
17240 /* If the compiler emits this, use it. */
17241 if (follow_die_ref (die, attr, &arg_cu) == child_die)
17242 is_this = 1;
17243 }
17244 else if (name && strcmp (name, "this") == 0)
17245 /* Function definitions will have the argument names. */
17246 is_this = 1;
17247 else if (name == NULL && iparams == 0)
17248 /* Declarations may not have the names, so like
17249 elsewhere in GDB, assume an artificial first
17250 argument is "this". */
17251 is_this = 1;
17252
17253 if (is_this)
17254 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
17255 arg_type, 0);
17256 }
17257
17258 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
c906108c
SS
17259 iparams++;
17260 }
17261 child_die = sibling_die (child_die);
17262 }
17263 }
17264
76c10ea2 17265 return ftype;
c906108c
SS
17266}
17267
f792889a 17268static struct type *
e7c27a73 17269read_typedef (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17270{
518817b3 17271 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
0114d602 17272 const char *name = NULL;
3c8e0968 17273 struct type *this_type, *target_type;
c906108c 17274
94af9270 17275 name = dwarf2_full_name (NULL, die, cu);
19f392bc
UW
17276 this_type = init_type (objfile, TYPE_CODE_TYPEDEF, 0, name);
17277 TYPE_TARGET_STUB (this_type) = 1;
f792889a 17278 set_die_type (die, this_type, cu);
3c8e0968
DE
17279 target_type = die_type (die, cu);
17280 if (target_type != this_type)
17281 TYPE_TARGET_TYPE (this_type) = target_type;
17282 else
17283 {
17284 /* Self-referential typedefs are, it seems, not allowed by the DWARF
17285 spec and cause infinite loops in GDB. */
b98664d3 17286 complaint (_("Self-referential DW_TAG_typedef "
9d8780f0
SM
17287 "- DIE at %s [in module %s]"),
17288 sect_offset_str (die->sect_off), objfile_name (objfile));
3c8e0968
DE
17289 TYPE_TARGET_TYPE (this_type) = NULL;
17290 }
f792889a 17291 return this_type;
c906108c
SS
17292}
17293
9b790ce7
UW
17294/* Allocate a floating-point type of size BITS and name NAME. Pass NAME_HINT
17295 (which may be different from NAME) to the architecture back-end to allow
17296 it to guess the correct format if necessary. */
17297
17298static struct type *
17299dwarf2_init_float_type (struct objfile *objfile, int bits, const char *name,
17300 const char *name_hint)
17301{
17302 struct gdbarch *gdbarch = get_objfile_arch (objfile);
17303 const struct floatformat **format;
17304 struct type *type;
17305
17306 format = gdbarch_floatformat_for_type (gdbarch, name_hint, bits);
17307 if (format)
17308 type = init_float_type (objfile, bits, name, format);
17309 else
77b7c781 17310 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
9b790ce7
UW
17311
17312 return type;
17313}
17314
c906108c
SS
17315/* Find a representation of a given base type and install
17316 it in the TYPE field of the die. */
17317
f792889a 17318static struct type *
e7c27a73 17319read_base_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17320{
518817b3 17321 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c
SS
17322 struct type *type;
17323 struct attribute *attr;
19f392bc 17324 int encoding = 0, bits = 0;
15d034d0 17325 const char *name;
c906108c 17326
e142c38c 17327 attr = dwarf2_attr (die, DW_AT_encoding, cu);
c906108c
SS
17328 if (attr)
17329 {
17330 encoding = DW_UNSND (attr);
17331 }
e142c38c 17332 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
17333 if (attr)
17334 {
19f392bc 17335 bits = DW_UNSND (attr) * TARGET_CHAR_BIT;
c906108c 17336 }
39cbfefa 17337 name = dwarf2_name (die, cu);
6ccb9162 17338 if (!name)
c906108c 17339 {
b98664d3 17340 complaint (_("DW_AT_name missing from DW_TAG_base_type"));
c906108c 17341 }
6ccb9162
UW
17342
17343 switch (encoding)
c906108c 17344 {
6ccb9162
UW
17345 case DW_ATE_address:
17346 /* Turn DW_ATE_address into a void * pointer. */
77b7c781 17347 type = init_type (objfile, TYPE_CODE_VOID, TARGET_CHAR_BIT, NULL);
19f392bc 17348 type = init_pointer_type (objfile, bits, name, type);
6ccb9162
UW
17349 break;
17350 case DW_ATE_boolean:
19f392bc 17351 type = init_boolean_type (objfile, bits, 1, name);
6ccb9162
UW
17352 break;
17353 case DW_ATE_complex_float:
9b790ce7 17354 type = dwarf2_init_float_type (objfile, bits / 2, NULL, name);
19f392bc 17355 type = init_complex_type (objfile, name, type);
6ccb9162
UW
17356 break;
17357 case DW_ATE_decimal_float:
19f392bc 17358 type = init_decfloat_type (objfile, bits, name);
6ccb9162
UW
17359 break;
17360 case DW_ATE_float:
9b790ce7 17361 type = dwarf2_init_float_type (objfile, bits, name, name);
6ccb9162
UW
17362 break;
17363 case DW_ATE_signed:
19f392bc 17364 type = init_integer_type (objfile, bits, 0, name);
6ccb9162
UW
17365 break;
17366 case DW_ATE_unsigned:
3b2b8fea
TT
17367 if (cu->language == language_fortran
17368 && name
61012eef 17369 && startswith (name, "character("))
19f392bc
UW
17370 type = init_character_type (objfile, bits, 1, name);
17371 else
17372 type = init_integer_type (objfile, bits, 1, name);
6ccb9162
UW
17373 break;
17374 case DW_ATE_signed_char:
6e70227d 17375 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea
TT
17376 || cu->language == language_pascal
17377 || cu->language == language_fortran)
19f392bc
UW
17378 type = init_character_type (objfile, bits, 0, name);
17379 else
17380 type = init_integer_type (objfile, bits, 0, name);
6ccb9162
UW
17381 break;
17382 case DW_ATE_unsigned_char:
868a0084 17383 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea 17384 || cu->language == language_pascal
c44af4eb
TT
17385 || cu->language == language_fortran
17386 || cu->language == language_rust)
19f392bc
UW
17387 type = init_character_type (objfile, bits, 1, name);
17388 else
17389 type = init_integer_type (objfile, bits, 1, name);
6ccb9162 17390 break;
75079b2b 17391 case DW_ATE_UTF:
53e710ac
PA
17392 {
17393 gdbarch *arch = get_objfile_arch (objfile);
17394
17395 if (bits == 16)
17396 type = builtin_type (arch)->builtin_char16;
17397 else if (bits == 32)
17398 type = builtin_type (arch)->builtin_char32;
17399 else
17400 {
b98664d3 17401 complaint (_("unsupported DW_ATE_UTF bit size: '%d'"),
53e710ac
PA
17402 bits);
17403 type = init_integer_type (objfile, bits, 1, name);
17404 }
17405 return set_die_type (die, type, cu);
17406 }
75079b2b
TT
17407 break;
17408
6ccb9162 17409 default:
b98664d3 17410 complaint (_("unsupported DW_AT_encoding: '%s'"),
6ccb9162 17411 dwarf_type_encoding_name (encoding));
77b7c781 17412 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
6ccb9162 17413 break;
c906108c 17414 }
6ccb9162 17415
0114d602 17416 if (name && strcmp (name, "char") == 0)
876cecd0 17417 TYPE_NOSIGN (type) = 1;
0114d602 17418
2b4424c3
TT
17419 maybe_set_alignment (cu, die, type);
17420
f792889a 17421 return set_die_type (die, type, cu);
c906108c
SS
17422}
17423
80180f79
SA
17424/* Parse dwarf attribute if it's a block, reference or constant and put the
17425 resulting value of the attribute into struct bound_prop.
17426 Returns 1 if ATTR could be resolved into PROP, 0 otherwise. */
17427
17428static int
17429attr_to_dynamic_prop (const struct attribute *attr, struct die_info *die,
17430 struct dwarf2_cu *cu, struct dynamic_prop *prop)
17431{
17432 struct dwarf2_property_baton *baton;
518817b3
SM
17433 struct obstack *obstack
17434 = &cu->per_cu->dwarf2_per_objfile->objfile->objfile_obstack;
80180f79
SA
17435
17436 if (attr == NULL || prop == NULL)
17437 return 0;
17438
17439 if (attr_form_is_block (attr))
17440 {
8d749320 17441 baton = XOBNEW (obstack, struct dwarf2_property_baton);
80180f79
SA
17442 baton->referenced_type = NULL;
17443 baton->locexpr.per_cu = cu->per_cu;
17444 baton->locexpr.size = DW_BLOCK (attr)->size;
17445 baton->locexpr.data = DW_BLOCK (attr)->data;
17446 prop->data.baton = baton;
17447 prop->kind = PROP_LOCEXPR;
17448 gdb_assert (prop->data.baton != NULL);
17449 }
17450 else if (attr_form_is_ref (attr))
17451 {
17452 struct dwarf2_cu *target_cu = cu;
17453 struct die_info *target_die;
17454 struct attribute *target_attr;
17455
17456 target_die = follow_die_ref (die, attr, &target_cu);
17457 target_attr = dwarf2_attr (target_die, DW_AT_location, target_cu);
df25ebbd
JB
17458 if (target_attr == NULL)
17459 target_attr = dwarf2_attr (target_die, DW_AT_data_member_location,
17460 target_cu);
80180f79
SA
17461 if (target_attr == NULL)
17462 return 0;
17463
df25ebbd 17464 switch (target_attr->name)
80180f79 17465 {
df25ebbd
JB
17466 case DW_AT_location:
17467 if (attr_form_is_section_offset (target_attr))
17468 {
8d749320 17469 baton = XOBNEW (obstack, struct dwarf2_property_baton);
df25ebbd
JB
17470 baton->referenced_type = die_type (target_die, target_cu);
17471 fill_in_loclist_baton (cu, &baton->loclist, target_attr);
17472 prop->data.baton = baton;
17473 prop->kind = PROP_LOCLIST;
17474 gdb_assert (prop->data.baton != NULL);
17475 }
17476 else if (attr_form_is_block (target_attr))
17477 {
8d749320 17478 baton = XOBNEW (obstack, struct dwarf2_property_baton);
df25ebbd
JB
17479 baton->referenced_type = die_type (target_die, target_cu);
17480 baton->locexpr.per_cu = cu->per_cu;
17481 baton->locexpr.size = DW_BLOCK (target_attr)->size;
17482 baton->locexpr.data = DW_BLOCK (target_attr)->data;
17483 prop->data.baton = baton;
17484 prop->kind = PROP_LOCEXPR;
17485 gdb_assert (prop->data.baton != NULL);
17486 }
17487 else
17488 {
17489 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
17490 "dynamic property");
17491 return 0;
17492 }
17493 break;
17494 case DW_AT_data_member_location:
17495 {
17496 LONGEST offset;
17497
17498 if (!handle_data_member_location (target_die, target_cu,
17499 &offset))
17500 return 0;
17501
8d749320 17502 baton = XOBNEW (obstack, struct dwarf2_property_baton);
6ad395a7
JB
17503 baton->referenced_type = read_type_die (target_die->parent,
17504 target_cu);
df25ebbd
JB
17505 baton->offset_info.offset = offset;
17506 baton->offset_info.type = die_type (target_die, target_cu);
17507 prop->data.baton = baton;
17508 prop->kind = PROP_ADDR_OFFSET;
17509 break;
17510 }
80180f79
SA
17511 }
17512 }
17513 else if (attr_form_is_constant (attr))
17514 {
17515 prop->data.const_val = dwarf2_get_attr_constant_value (attr, 0);
17516 prop->kind = PROP_CONST;
17517 }
17518 else
17519 {
17520 dwarf2_invalid_attrib_class_complaint (dwarf_form_name (attr->form),
17521 dwarf2_name (die, cu));
17522 return 0;
17523 }
17524
17525 return 1;
17526}
17527
a02abb62
JB
17528/* Read the given DW_AT_subrange DIE. */
17529
f792889a 17530static struct type *
a02abb62
JB
17531read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
17532{
4c9ad8c2 17533 struct type *base_type, *orig_base_type;
a02abb62
JB
17534 struct type *range_type;
17535 struct attribute *attr;
729efb13 17536 struct dynamic_prop low, high;
4fae6e18 17537 int low_default_is_valid;
c451ebe5 17538 int high_bound_is_count = 0;
15d034d0 17539 const char *name;
43bbcdc2 17540 LONGEST negative_mask;
e77813c8 17541
4c9ad8c2
TT
17542 orig_base_type = die_type (die, cu);
17543 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
17544 whereas the real type might be. So, we use ORIG_BASE_TYPE when
17545 creating the range type, but we use the result of check_typedef
17546 when examining properties of the type. */
17547 base_type = check_typedef (orig_base_type);
a02abb62 17548
7e314c57
JK
17549 /* The die_type call above may have already set the type for this DIE. */
17550 range_type = get_die_type (die, cu);
17551 if (range_type)
17552 return range_type;
17553
729efb13
SA
17554 low.kind = PROP_CONST;
17555 high.kind = PROP_CONST;
17556 high.data.const_val = 0;
17557
4fae6e18
JK
17558 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
17559 omitting DW_AT_lower_bound. */
17560 switch (cu->language)
6e70227d 17561 {
4fae6e18
JK
17562 case language_c:
17563 case language_cplus:
729efb13 17564 low.data.const_val = 0;
4fae6e18
JK
17565 low_default_is_valid = 1;
17566 break;
17567 case language_fortran:
729efb13 17568 low.data.const_val = 1;
4fae6e18
JK
17569 low_default_is_valid = 1;
17570 break;
17571 case language_d:
4fae6e18 17572 case language_objc:
c44af4eb 17573 case language_rust:
729efb13 17574 low.data.const_val = 0;
4fae6e18
JK
17575 low_default_is_valid = (cu->header.version >= 4);
17576 break;
17577 case language_ada:
17578 case language_m2:
17579 case language_pascal:
729efb13 17580 low.data.const_val = 1;
4fae6e18
JK
17581 low_default_is_valid = (cu->header.version >= 4);
17582 break;
17583 default:
729efb13 17584 low.data.const_val = 0;
4fae6e18
JK
17585 low_default_is_valid = 0;
17586 break;
a02abb62
JB
17587 }
17588
e142c38c 17589 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
a02abb62 17590 if (attr)
11c1ba78 17591 attr_to_dynamic_prop (attr, die, cu, &low);
4fae6e18 17592 else if (!low_default_is_valid)
b98664d3 17593 complaint (_("Missing DW_AT_lower_bound "
9d8780f0
SM
17594 "- DIE at %s [in module %s]"),
17595 sect_offset_str (die->sect_off),
518817b3 17596 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
a02abb62 17597
e142c38c 17598 attr = dwarf2_attr (die, DW_AT_upper_bound, cu);
80180f79 17599 if (!attr_to_dynamic_prop (attr, die, cu, &high))
e77813c8
PM
17600 {
17601 attr = dwarf2_attr (die, DW_AT_count, cu);
c451ebe5 17602 if (attr_to_dynamic_prop (attr, die, cu, &high))
6b662e19 17603 {
c451ebe5
SA
17604 /* If bounds are constant do the final calculation here. */
17605 if (low.kind == PROP_CONST && high.kind == PROP_CONST)
17606 high.data.const_val = low.data.const_val + high.data.const_val - 1;
17607 else
17608 high_bound_is_count = 1;
c2ff108b 17609 }
e77813c8
PM
17610 }
17611
17612 /* Dwarf-2 specifications explicitly allows to create subrange types
17613 without specifying a base type.
17614 In that case, the base type must be set to the type of
17615 the lower bound, upper bound or count, in that order, if any of these
17616 three attributes references an object that has a type.
17617 If no base type is found, the Dwarf-2 specifications say that
17618 a signed integer type of size equal to the size of an address should
17619 be used.
17620 For the following C code: `extern char gdb_int [];'
17621 GCC produces an empty range DIE.
17622 FIXME: muller/2010-05-28: Possible references to object for low bound,
0963b4bd 17623 high bound or count are not yet handled by this code. */
e77813c8
PM
17624 if (TYPE_CODE (base_type) == TYPE_CODE_VOID)
17625 {
518817b3 17626 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
e77813c8
PM
17627 struct gdbarch *gdbarch = get_objfile_arch (objfile);
17628 int addr_size = gdbarch_addr_bit (gdbarch) /8;
17629 struct type *int_type = objfile_type (objfile)->builtin_int;
17630
17631 /* Test "int", "long int", and "long long int" objfile types,
17632 and select the first one having a size above or equal to the
17633 architecture address size. */
17634 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
17635 base_type = int_type;
17636 else
17637 {
17638 int_type = objfile_type (objfile)->builtin_long;
17639 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
17640 base_type = int_type;
17641 else
17642 {
17643 int_type = objfile_type (objfile)->builtin_long_long;
17644 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
17645 base_type = int_type;
17646 }
17647 }
17648 }
a02abb62 17649
dbb9c2b1
JB
17650 /* Normally, the DWARF producers are expected to use a signed
17651 constant form (Eg. DW_FORM_sdata) to express negative bounds.
17652 But this is unfortunately not always the case, as witnessed
17653 with GCC, for instance, where the ambiguous DW_FORM_dataN form
17654 is used instead. To work around that ambiguity, we treat
17655 the bounds as signed, and thus sign-extend their values, when
17656 the base type is signed. */
6e70227d 17657 negative_mask =
66c6502d 17658 -((LONGEST) 1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1));
729efb13
SA
17659 if (low.kind == PROP_CONST
17660 && !TYPE_UNSIGNED (base_type) && (low.data.const_val & negative_mask))
17661 low.data.const_val |= negative_mask;
17662 if (high.kind == PROP_CONST
17663 && !TYPE_UNSIGNED (base_type) && (high.data.const_val & negative_mask))
17664 high.data.const_val |= negative_mask;
43bbcdc2 17665
729efb13 17666 range_type = create_range_type (NULL, orig_base_type, &low, &high);
a02abb62 17667
c451ebe5
SA
17668 if (high_bound_is_count)
17669 TYPE_RANGE_DATA (range_type)->flag_upper_bound_is_count = 1;
17670
c2ff108b
JK
17671 /* Ada expects an empty array on no boundary attributes. */
17672 if (attr == NULL && cu->language != language_ada)
729efb13 17673 TYPE_HIGH_BOUND_KIND (range_type) = PROP_UNDEFINED;
c2ff108b 17674
39cbfefa
DJ
17675 name = dwarf2_name (die, cu);
17676 if (name)
17677 TYPE_NAME (range_type) = name;
6e70227d 17678
e142c38c 17679 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
a02abb62
JB
17680 if (attr)
17681 TYPE_LENGTH (range_type) = DW_UNSND (attr);
17682
2b4424c3
TT
17683 maybe_set_alignment (cu, die, range_type);
17684
7e314c57
JK
17685 set_die_type (die, range_type, cu);
17686
17687 /* set_die_type should be already done. */
b4ba55a1
JB
17688 set_descriptive_type (range_type, die, cu);
17689
7e314c57 17690 return range_type;
a02abb62 17691}
6e70227d 17692
f792889a 17693static struct type *
81a17f79
JB
17694read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
17695{
17696 struct type *type;
81a17f79 17697
518817b3
SM
17698 type = init_type (cu->per_cu->dwarf2_per_objfile->objfile, TYPE_CODE_VOID,0,
17699 NULL);
0114d602 17700 TYPE_NAME (type) = dwarf2_name (die, cu);
81a17f79 17701
74a2f8ff
JB
17702 /* In Ada, an unspecified type is typically used when the description
17703 of the type is defered to a different unit. When encountering
17704 such a type, we treat it as a stub, and try to resolve it later on,
17705 when needed. */
17706 if (cu->language == language_ada)
17707 TYPE_STUB (type) = 1;
17708
f792889a 17709 return set_die_type (die, type, cu);
81a17f79 17710}
a02abb62 17711
639d11d3
DC
17712/* Read a single die and all its descendents. Set the die's sibling
17713 field to NULL; set other fields in the die correctly, and set all
17714 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
17715 location of the info_ptr after reading all of those dies. PARENT
17716 is the parent of the die in question. */
17717
17718static struct die_info *
dee91e82 17719read_die_and_children (const struct die_reader_specs *reader,
d521ce57
TT
17720 const gdb_byte *info_ptr,
17721 const gdb_byte **new_info_ptr,
dee91e82 17722 struct die_info *parent)
639d11d3
DC
17723{
17724 struct die_info *die;
d521ce57 17725 const gdb_byte *cur_ptr;
639d11d3
DC
17726 int has_children;
17727
bf6af496 17728 cur_ptr = read_full_die_1 (reader, &die, info_ptr, &has_children, 0);
1d325ec1
DJ
17729 if (die == NULL)
17730 {
17731 *new_info_ptr = cur_ptr;
17732 return NULL;
17733 }
93311388 17734 store_in_ref_table (die, reader->cu);
639d11d3
DC
17735
17736 if (has_children)
bf6af496 17737 die->child = read_die_and_siblings_1 (reader, cur_ptr, new_info_ptr, die);
639d11d3
DC
17738 else
17739 {
17740 die->child = NULL;
17741 *new_info_ptr = cur_ptr;
17742 }
17743
17744 die->sibling = NULL;
17745 die->parent = parent;
17746 return die;
17747}
17748
17749/* Read a die, all of its descendents, and all of its siblings; set
17750 all of the fields of all of the dies correctly. Arguments are as
17751 in read_die_and_children. */
17752
17753static struct die_info *
bf6af496 17754read_die_and_siblings_1 (const struct die_reader_specs *reader,
d521ce57
TT
17755 const gdb_byte *info_ptr,
17756 const gdb_byte **new_info_ptr,
bf6af496 17757 struct die_info *parent)
639d11d3
DC
17758{
17759 struct die_info *first_die, *last_sibling;
d521ce57 17760 const gdb_byte *cur_ptr;
639d11d3 17761
c906108c 17762 cur_ptr = info_ptr;
639d11d3
DC
17763 first_die = last_sibling = NULL;
17764
17765 while (1)
c906108c 17766 {
639d11d3 17767 struct die_info *die
dee91e82 17768 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
639d11d3 17769
1d325ec1 17770 if (die == NULL)
c906108c 17771 {
639d11d3
DC
17772 *new_info_ptr = cur_ptr;
17773 return first_die;
c906108c 17774 }
1d325ec1
DJ
17775
17776 if (!first_die)
17777 first_die = die;
c906108c 17778 else
1d325ec1
DJ
17779 last_sibling->sibling = die;
17780
17781 last_sibling = die;
c906108c 17782 }
c906108c
SS
17783}
17784
bf6af496
DE
17785/* Read a die, all of its descendents, and all of its siblings; set
17786 all of the fields of all of the dies correctly. Arguments are as
17787 in read_die_and_children.
17788 This the main entry point for reading a DIE and all its children. */
17789
17790static struct die_info *
17791read_die_and_siblings (const struct die_reader_specs *reader,
d521ce57
TT
17792 const gdb_byte *info_ptr,
17793 const gdb_byte **new_info_ptr,
bf6af496
DE
17794 struct die_info *parent)
17795{
17796 struct die_info *die = read_die_and_siblings_1 (reader, info_ptr,
17797 new_info_ptr, parent);
17798
b4f54984 17799 if (dwarf_die_debug)
bf6af496
DE
17800 {
17801 fprintf_unfiltered (gdb_stdlog,
17802 "Read die from %s@0x%x of %s:\n",
a32a8923 17803 get_section_name (reader->die_section),
bf6af496
DE
17804 (unsigned) (info_ptr - reader->die_section->buffer),
17805 bfd_get_filename (reader->abfd));
b4f54984 17806 dump_die (die, dwarf_die_debug);
bf6af496
DE
17807 }
17808
17809 return die;
17810}
17811
3019eac3
DE
17812/* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
17813 attributes.
17814 The caller is responsible for filling in the extra attributes
17815 and updating (*DIEP)->num_attrs.
17816 Set DIEP to point to a newly allocated die with its information,
17817 except for its child, sibling, and parent fields.
17818 Set HAS_CHILDREN to tell whether the die has children or not. */
93311388 17819
d521ce57 17820static const gdb_byte *
3019eac3 17821read_full_die_1 (const struct die_reader_specs *reader,
d521ce57 17822 struct die_info **diep, const gdb_byte *info_ptr,
3019eac3 17823 int *has_children, int num_extra_attrs)
93311388 17824{
b64f50a1 17825 unsigned int abbrev_number, bytes_read, i;
93311388
DE
17826 struct abbrev_info *abbrev;
17827 struct die_info *die;
17828 struct dwarf2_cu *cu = reader->cu;
17829 bfd *abfd = reader->abfd;
17830
9c541725 17831 sect_offset sect_off = (sect_offset) (info_ptr - reader->buffer);
93311388
DE
17832 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
17833 info_ptr += bytes_read;
17834 if (!abbrev_number)
17835 {
17836 *diep = NULL;
17837 *has_children = 0;
17838 return info_ptr;
17839 }
17840
685af9cd 17841 abbrev = reader->abbrev_table->lookup_abbrev (abbrev_number);
93311388 17842 if (!abbrev)
348e048f
DE
17843 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
17844 abbrev_number,
17845 bfd_get_filename (abfd));
17846
3019eac3 17847 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
9c541725 17848 die->sect_off = sect_off;
93311388
DE
17849 die->tag = abbrev->tag;
17850 die->abbrev = abbrev_number;
17851
3019eac3
DE
17852 /* Make the result usable.
17853 The caller needs to update num_attrs after adding the extra
17854 attributes. */
93311388
DE
17855 die->num_attrs = abbrev->num_attrs;
17856
17857 for (i = 0; i < abbrev->num_attrs; ++i)
dee91e82
DE
17858 info_ptr = read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
17859 info_ptr);
93311388
DE
17860
17861 *diep = die;
17862 *has_children = abbrev->has_children;
17863 return info_ptr;
17864}
17865
3019eac3
DE
17866/* Read a die and all its attributes.
17867 Set DIEP to point to a newly allocated die with its information,
17868 except for its child, sibling, and parent fields.
17869 Set HAS_CHILDREN to tell whether the die has children or not. */
17870
d521ce57 17871static const gdb_byte *
3019eac3 17872read_full_die (const struct die_reader_specs *reader,
d521ce57 17873 struct die_info **diep, const gdb_byte *info_ptr,
3019eac3
DE
17874 int *has_children)
17875{
d521ce57 17876 const gdb_byte *result;
bf6af496
DE
17877
17878 result = read_full_die_1 (reader, diep, info_ptr, has_children, 0);
17879
b4f54984 17880 if (dwarf_die_debug)
bf6af496
DE
17881 {
17882 fprintf_unfiltered (gdb_stdlog,
17883 "Read die from %s@0x%x of %s:\n",
a32a8923 17884 get_section_name (reader->die_section),
bf6af496
DE
17885 (unsigned) (info_ptr - reader->die_section->buffer),
17886 bfd_get_filename (reader->abfd));
b4f54984 17887 dump_die (*diep, dwarf_die_debug);
bf6af496
DE
17888 }
17889
17890 return result;
3019eac3 17891}
433df2d4
DE
17892\f
17893/* Abbreviation tables.
3019eac3 17894
433df2d4 17895 In DWARF version 2, the description of the debugging information is
c906108c
SS
17896 stored in a separate .debug_abbrev section. Before we read any
17897 dies from a section we read in all abbreviations and install them
433df2d4
DE
17898 in a hash table. */
17899
17900/* Allocate space for a struct abbrev_info object in ABBREV_TABLE. */
17901
685af9cd
TT
17902struct abbrev_info *
17903abbrev_table::alloc_abbrev ()
433df2d4
DE
17904{
17905 struct abbrev_info *abbrev;
17906
685af9cd 17907 abbrev = XOBNEW (&abbrev_obstack, struct abbrev_info);
433df2d4 17908 memset (abbrev, 0, sizeof (struct abbrev_info));
8d749320 17909
433df2d4
DE
17910 return abbrev;
17911}
17912
17913/* Add an abbreviation to the table. */
c906108c 17914
685af9cd
TT
17915void
17916abbrev_table::add_abbrev (unsigned int abbrev_number,
17917 struct abbrev_info *abbrev)
433df2d4
DE
17918{
17919 unsigned int hash_number;
17920
17921 hash_number = abbrev_number % ABBREV_HASH_SIZE;
4a17f768
YQ
17922 abbrev->next = m_abbrevs[hash_number];
17923 m_abbrevs[hash_number] = abbrev;
433df2d4 17924}
dee91e82 17925
433df2d4
DE
17926/* Look up an abbrev in the table.
17927 Returns NULL if the abbrev is not found. */
17928
685af9cd
TT
17929struct abbrev_info *
17930abbrev_table::lookup_abbrev (unsigned int abbrev_number)
c906108c 17931{
433df2d4
DE
17932 unsigned int hash_number;
17933 struct abbrev_info *abbrev;
17934
17935 hash_number = abbrev_number % ABBREV_HASH_SIZE;
4a17f768 17936 abbrev = m_abbrevs[hash_number];
433df2d4
DE
17937
17938 while (abbrev)
17939 {
17940 if (abbrev->number == abbrev_number)
17941 return abbrev;
17942 abbrev = abbrev->next;
17943 }
17944 return NULL;
17945}
17946
17947/* Read in an abbrev table. */
17948
685af9cd 17949static abbrev_table_up
ed2dc618
SM
17950abbrev_table_read_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
17951 struct dwarf2_section_info *section,
9c541725 17952 sect_offset sect_off)
433df2d4
DE
17953{
17954 struct objfile *objfile = dwarf2_per_objfile->objfile;
a32a8923 17955 bfd *abfd = get_section_bfd_owner (section);
d521ce57 17956 const gdb_byte *abbrev_ptr;
c906108c
SS
17957 struct abbrev_info *cur_abbrev;
17958 unsigned int abbrev_number, bytes_read, abbrev_name;
433df2d4 17959 unsigned int abbrev_form;
f3dd6933
DJ
17960 struct attr_abbrev *cur_attrs;
17961 unsigned int allocated_attrs;
c906108c 17962
685af9cd 17963 abbrev_table_up abbrev_table (new struct abbrev_table (sect_off));
c906108c 17964
433df2d4 17965 dwarf2_read_section (objfile, section);
9c541725 17966 abbrev_ptr = section->buffer + to_underlying (sect_off);
c906108c
SS
17967 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
17968 abbrev_ptr += bytes_read;
17969
f3dd6933 17970 allocated_attrs = ATTR_ALLOC_CHUNK;
8d749320 17971 cur_attrs = XNEWVEC (struct attr_abbrev, allocated_attrs);
6e70227d 17972
0963b4bd 17973 /* Loop until we reach an abbrev number of 0. */
c906108c
SS
17974 while (abbrev_number)
17975 {
685af9cd 17976 cur_abbrev = abbrev_table->alloc_abbrev ();
c906108c
SS
17977
17978 /* read in abbrev header */
17979 cur_abbrev->number = abbrev_number;
aead7601
SM
17980 cur_abbrev->tag
17981 = (enum dwarf_tag) read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
c906108c
SS
17982 abbrev_ptr += bytes_read;
17983 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
17984 abbrev_ptr += 1;
17985
17986 /* now read in declarations */
22d2f3ab 17987 for (;;)
c906108c 17988 {
43988095
JK
17989 LONGEST implicit_const;
17990
22d2f3ab
JK
17991 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
17992 abbrev_ptr += bytes_read;
17993 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
17994 abbrev_ptr += bytes_read;
43988095
JK
17995 if (abbrev_form == DW_FORM_implicit_const)
17996 {
17997 implicit_const = read_signed_leb128 (abfd, abbrev_ptr,
17998 &bytes_read);
17999 abbrev_ptr += bytes_read;
18000 }
18001 else
18002 {
18003 /* Initialize it due to a false compiler warning. */
18004 implicit_const = -1;
18005 }
22d2f3ab
JK
18006
18007 if (abbrev_name == 0)
18008 break;
18009
f3dd6933 18010 if (cur_abbrev->num_attrs == allocated_attrs)
c906108c 18011 {
f3dd6933
DJ
18012 allocated_attrs += ATTR_ALLOC_CHUNK;
18013 cur_attrs
224c3ddb 18014 = XRESIZEVEC (struct attr_abbrev, cur_attrs, allocated_attrs);
c906108c 18015 }
ae038cb0 18016
aead7601
SM
18017 cur_attrs[cur_abbrev->num_attrs].name
18018 = (enum dwarf_attribute) abbrev_name;
22d2f3ab 18019 cur_attrs[cur_abbrev->num_attrs].form
aead7601 18020 = (enum dwarf_form) abbrev_form;
43988095 18021 cur_attrs[cur_abbrev->num_attrs].implicit_const = implicit_const;
22d2f3ab 18022 ++cur_abbrev->num_attrs;
c906108c
SS
18023 }
18024
8d749320
SM
18025 cur_abbrev->attrs =
18026 XOBNEWVEC (&abbrev_table->abbrev_obstack, struct attr_abbrev,
18027 cur_abbrev->num_attrs);
f3dd6933
DJ
18028 memcpy (cur_abbrev->attrs, cur_attrs,
18029 cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
18030
685af9cd 18031 abbrev_table->add_abbrev (abbrev_number, cur_abbrev);
c906108c
SS
18032
18033 /* Get next abbreviation.
18034 Under Irix6 the abbreviations for a compilation unit are not
c5aa993b
JM
18035 always properly terminated with an abbrev number of 0.
18036 Exit loop if we encounter an abbreviation which we have
18037 already read (which means we are about to read the abbreviations
18038 for the next compile unit) or if the end of the abbreviation
18039 table is reached. */
433df2d4 18040 if ((unsigned int) (abbrev_ptr - section->buffer) >= section->size)
c906108c
SS
18041 break;
18042 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18043 abbrev_ptr += bytes_read;
685af9cd 18044 if (abbrev_table->lookup_abbrev (abbrev_number) != NULL)
c906108c
SS
18045 break;
18046 }
f3dd6933
DJ
18047
18048 xfree (cur_attrs);
433df2d4 18049 return abbrev_table;
c906108c
SS
18050}
18051
72bf9492
DJ
18052/* Returns nonzero if TAG represents a type that we might generate a partial
18053 symbol for. */
18054
18055static int
18056is_type_tag_for_partial (int tag)
18057{
18058 switch (tag)
18059 {
18060#if 0
18061 /* Some types that would be reasonable to generate partial symbols for,
18062 that we don't at present. */
18063 case DW_TAG_array_type:
18064 case DW_TAG_file_type:
18065 case DW_TAG_ptr_to_member_type:
18066 case DW_TAG_set_type:
18067 case DW_TAG_string_type:
18068 case DW_TAG_subroutine_type:
18069#endif
18070 case DW_TAG_base_type:
18071 case DW_TAG_class_type:
680b30c7 18072 case DW_TAG_interface_type:
72bf9492
DJ
18073 case DW_TAG_enumeration_type:
18074 case DW_TAG_structure_type:
18075 case DW_TAG_subrange_type:
18076 case DW_TAG_typedef:
18077 case DW_TAG_union_type:
18078 return 1;
18079 default:
18080 return 0;
18081 }
18082}
18083
18084/* Load all DIEs that are interesting for partial symbols into memory. */
18085
18086static struct partial_die_info *
dee91e82 18087load_partial_dies (const struct die_reader_specs *reader,
d521ce57 18088 const gdb_byte *info_ptr, int building_psymtab)
72bf9492 18089{
dee91e82 18090 struct dwarf2_cu *cu = reader->cu;
518817b3 18091 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
72bf9492 18092 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
72bf9492 18093 unsigned int bytes_read;
5afb4e99 18094 unsigned int load_all = 0;
72bf9492
DJ
18095 int nesting_level = 1;
18096
18097 parent_die = NULL;
18098 last_die = NULL;
18099
7adf1e79
DE
18100 gdb_assert (cu->per_cu != NULL);
18101 if (cu->per_cu->load_all_dies)
5afb4e99
DJ
18102 load_all = 1;
18103
72bf9492
DJ
18104 cu->partial_dies
18105 = htab_create_alloc_ex (cu->header.length / 12,
18106 partial_die_hash,
18107 partial_die_eq,
18108 NULL,
18109 &cu->comp_unit_obstack,
18110 hashtab_obstack_allocate,
18111 dummy_obstack_deallocate);
18112
72bf9492
DJ
18113 while (1)
18114 {
685af9cd 18115 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
72bf9492
DJ
18116
18117 /* A NULL abbrev means the end of a series of children. */
18118 if (abbrev == NULL)
18119 {
18120 if (--nesting_level == 0)
cd9983dd
YQ
18121 return first_die;
18122
72bf9492
DJ
18123 info_ptr += bytes_read;
18124 last_die = parent_die;
18125 parent_die = parent_die->die_parent;
18126 continue;
18127 }
18128
98bfdba5
PA
18129 /* Check for template arguments. We never save these; if
18130 they're seen, we just mark the parent, and go on our way. */
18131 if (parent_die != NULL
18132 && cu->language == language_cplus
18133 && (abbrev->tag == DW_TAG_template_type_param
18134 || abbrev->tag == DW_TAG_template_value_param))
18135 {
18136 parent_die->has_template_arguments = 1;
18137
18138 if (!load_all)
18139 {
18140 /* We don't need a partial DIE for the template argument. */
dee91e82 18141 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
98bfdba5
PA
18142 continue;
18143 }
18144 }
18145
0d99eb77 18146 /* We only recurse into c++ subprograms looking for template arguments.
98bfdba5
PA
18147 Skip their other children. */
18148 if (!load_all
18149 && cu->language == language_cplus
18150 && parent_die != NULL
18151 && parent_die->tag == DW_TAG_subprogram)
18152 {
dee91e82 18153 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
98bfdba5
PA
18154 continue;
18155 }
18156
5afb4e99
DJ
18157 /* Check whether this DIE is interesting enough to save. Normally
18158 we would not be interested in members here, but there may be
18159 later variables referencing them via DW_AT_specification (for
18160 static members). */
18161 if (!load_all
18162 && !is_type_tag_for_partial (abbrev->tag)
72929c62 18163 && abbrev->tag != DW_TAG_constant
72bf9492
DJ
18164 && abbrev->tag != DW_TAG_enumerator
18165 && abbrev->tag != DW_TAG_subprogram
b1dc1806 18166 && abbrev->tag != DW_TAG_inlined_subroutine
bc30ff58 18167 && abbrev->tag != DW_TAG_lexical_block
72bf9492 18168 && abbrev->tag != DW_TAG_variable
5afb4e99 18169 && abbrev->tag != DW_TAG_namespace
f55ee35c 18170 && abbrev->tag != DW_TAG_module
95554aad 18171 && abbrev->tag != DW_TAG_member
74921315
KS
18172 && abbrev->tag != DW_TAG_imported_unit
18173 && abbrev->tag != DW_TAG_imported_declaration)
72bf9492
DJ
18174 {
18175 /* Otherwise we skip to the next sibling, if any. */
dee91e82 18176 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
72bf9492
DJ
18177 continue;
18178 }
18179
6f06d47b
YQ
18180 struct partial_die_info pdi ((sect_offset) (info_ptr - reader->buffer),
18181 abbrev);
cd9983dd 18182
48fbe735 18183 info_ptr = pdi.read (reader, *abbrev, info_ptr + bytes_read);
72bf9492
DJ
18184
18185 /* This two-pass algorithm for processing partial symbols has a
18186 high cost in cache pressure. Thus, handle some simple cases
18187 here which cover the majority of C partial symbols. DIEs
18188 which neither have specification tags in them, nor could have
18189 specification tags elsewhere pointing at them, can simply be
18190 processed and discarded.
18191
18192 This segment is also optional; scan_partial_symbols and
18193 add_partial_symbol will handle these DIEs if we chain
18194 them in normally. When compilers which do not emit large
18195 quantities of duplicate debug information are more common,
18196 this code can probably be removed. */
18197
18198 /* Any complete simple types at the top level (pretty much all
18199 of them, for a language without namespaces), can be processed
18200 directly. */
18201 if (parent_die == NULL
cd9983dd
YQ
18202 && pdi.has_specification == 0
18203 && pdi.is_declaration == 0
18204 && ((pdi.tag == DW_TAG_typedef && !pdi.has_children)
18205 || pdi.tag == DW_TAG_base_type
18206 || pdi.tag == DW_TAG_subrange_type))
72bf9492 18207 {
cd9983dd
YQ
18208 if (building_psymtab && pdi.name != NULL)
18209 add_psymbol_to_list (pdi.name, strlen (pdi.name), 0,
72bf9492 18210 VAR_DOMAIN, LOC_TYPEDEF,
bb5ed363 18211 &objfile->static_psymbols,
1762568f 18212 0, cu->language, objfile);
cd9983dd 18213 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
72bf9492
DJ
18214 continue;
18215 }
18216
d8228535
JK
18217 /* The exception for DW_TAG_typedef with has_children above is
18218 a workaround of GCC PR debug/47510. In the case of this complaint
a737d952 18219 type_name_or_error will error on such types later.
d8228535
JK
18220
18221 GDB skipped children of DW_TAG_typedef by the shortcut above and then
18222 it could not find the child DIEs referenced later, this is checked
18223 above. In correct DWARF DW_TAG_typedef should have no children. */
18224
cd9983dd 18225 if (pdi.tag == DW_TAG_typedef && pdi.has_children)
b98664d3 18226 complaint (_("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
9d8780f0 18227 "- DIE at %s [in module %s]"),
cd9983dd 18228 sect_offset_str (pdi.sect_off), objfile_name (objfile));
d8228535 18229
72bf9492
DJ
18230 /* If we're at the second level, and we're an enumerator, and
18231 our parent has no specification (meaning possibly lives in a
18232 namespace elsewhere), then we can add the partial symbol now
18233 instead of queueing it. */
cd9983dd 18234 if (pdi.tag == DW_TAG_enumerator
72bf9492
DJ
18235 && parent_die != NULL
18236 && parent_die->die_parent == NULL
18237 && parent_die->tag == DW_TAG_enumeration_type
18238 && parent_die->has_specification == 0)
18239 {
cd9983dd 18240 if (pdi.name == NULL)
b98664d3 18241 complaint (_("malformed enumerator DIE ignored"));
72bf9492 18242 else if (building_psymtab)
cd9983dd 18243 add_psymbol_to_list (pdi.name, strlen (pdi.name), 0,
72bf9492 18244 VAR_DOMAIN, LOC_CONST,
9c37b5ae 18245 cu->language == language_cplus
bb5ed363
DE
18246 ? &objfile->global_psymbols
18247 : &objfile->static_psymbols,
1762568f 18248 0, cu->language, objfile);
72bf9492 18249
cd9983dd 18250 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
72bf9492
DJ
18251 continue;
18252 }
18253
cd9983dd 18254 struct partial_die_info *part_die
6f06d47b 18255 = new (&cu->comp_unit_obstack) partial_die_info (pdi);
cd9983dd 18256
72bf9492
DJ
18257 /* We'll save this DIE so link it in. */
18258 part_die->die_parent = parent_die;
18259 part_die->die_sibling = NULL;
18260 part_die->die_child = NULL;
18261
18262 if (last_die && last_die == parent_die)
18263 last_die->die_child = part_die;
18264 else if (last_die)
18265 last_die->die_sibling = part_die;
18266
18267 last_die = part_die;
18268
18269 if (first_die == NULL)
18270 first_die = part_die;
18271
18272 /* Maybe add the DIE to the hash table. Not all DIEs that we
18273 find interesting need to be in the hash table, because we
18274 also have the parent/sibling/child chains; only those that we
18275 might refer to by offset later during partial symbol reading.
18276
18277 For now this means things that might have be the target of a
18278 DW_AT_specification, DW_AT_abstract_origin, or
18279 DW_AT_extension. DW_AT_extension will refer only to
18280 namespaces; DW_AT_abstract_origin refers to functions (and
18281 many things under the function DIE, but we do not recurse
18282 into function DIEs during partial symbol reading) and
18283 possibly variables as well; DW_AT_specification refers to
18284 declarations. Declarations ought to have the DW_AT_declaration
18285 flag. It happens that GCC forgets to put it in sometimes, but
18286 only for functions, not for types.
18287
18288 Adding more things than necessary to the hash table is harmless
18289 except for the performance cost. Adding too few will result in
5afb4e99
DJ
18290 wasted time in find_partial_die, when we reread the compilation
18291 unit with load_all_dies set. */
72bf9492 18292
5afb4e99 18293 if (load_all
72929c62 18294 || abbrev->tag == DW_TAG_constant
5afb4e99 18295 || abbrev->tag == DW_TAG_subprogram
72bf9492
DJ
18296 || abbrev->tag == DW_TAG_variable
18297 || abbrev->tag == DW_TAG_namespace
18298 || part_die->is_declaration)
18299 {
18300 void **slot;
18301
18302 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
9c541725
PA
18303 to_underlying (part_die->sect_off),
18304 INSERT);
72bf9492
DJ
18305 *slot = part_die;
18306 }
18307
72bf9492 18308 /* For some DIEs we want to follow their children (if any). For C
bc30ff58 18309 we have no reason to follow the children of structures; for other
98bfdba5
PA
18310 languages we have to, so that we can get at method physnames
18311 to infer fully qualified class names, for DW_AT_specification,
18312 and for C++ template arguments. For C++, we also look one level
18313 inside functions to find template arguments (if the name of the
18314 function does not already contain the template arguments).
bc30ff58
JB
18315
18316 For Ada, we need to scan the children of subprograms and lexical
18317 blocks as well because Ada allows the definition of nested
18318 entities that could be interesting for the debugger, such as
18319 nested subprograms for instance. */
72bf9492 18320 if (last_die->has_children
5afb4e99
DJ
18321 && (load_all
18322 || last_die->tag == DW_TAG_namespace
f55ee35c 18323 || last_die->tag == DW_TAG_module
72bf9492 18324 || last_die->tag == DW_TAG_enumeration_type
98bfdba5
PA
18325 || (cu->language == language_cplus
18326 && last_die->tag == DW_TAG_subprogram
18327 && (last_die->name == NULL
18328 || strchr (last_die->name, '<') == NULL))
72bf9492
DJ
18329 || (cu->language != language_c
18330 && (last_die->tag == DW_TAG_class_type
680b30c7 18331 || last_die->tag == DW_TAG_interface_type
72bf9492 18332 || last_die->tag == DW_TAG_structure_type
bc30ff58
JB
18333 || last_die->tag == DW_TAG_union_type))
18334 || (cu->language == language_ada
18335 && (last_die->tag == DW_TAG_subprogram
18336 || last_die->tag == DW_TAG_lexical_block))))
72bf9492
DJ
18337 {
18338 nesting_level++;
18339 parent_die = last_die;
18340 continue;
18341 }
18342
18343 /* Otherwise we skip to the next sibling, if any. */
dee91e82 18344 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
72bf9492
DJ
18345
18346 /* Back to the top, do it again. */
18347 }
18348}
18349
6f06d47b
YQ
18350partial_die_info::partial_die_info (sect_offset sect_off_,
18351 struct abbrev_info *abbrev)
18352 : partial_die_info (sect_off_, abbrev->tag, abbrev->has_children)
18353{
18354}
18355
35cc7ed7
YQ
18356/* Read a minimal amount of information into the minimal die structure.
18357 INFO_PTR should point just after the initial uleb128 of a DIE. */
c906108c 18358
48fbe735
YQ
18359const gdb_byte *
18360partial_die_info::read (const struct die_reader_specs *reader,
18361 const struct abbrev_info &abbrev, const gdb_byte *info_ptr)
c906108c 18362{
dee91e82 18363 struct dwarf2_cu *cu = reader->cu;
518817b3
SM
18364 struct dwarf2_per_objfile *dwarf2_per_objfile
18365 = cu->per_cu->dwarf2_per_objfile;
fa238c03 18366 unsigned int i;
c5aa993b 18367 int has_low_pc_attr = 0;
c906108c 18368 int has_high_pc_attr = 0;
91da1414 18369 int high_pc_relative = 0;
c906108c 18370
fd0a254f 18371 for (i = 0; i < abbrev.num_attrs; ++i)
c906108c 18372 {
48fbe735
YQ
18373 struct attribute attr;
18374
fd0a254f 18375 info_ptr = read_attribute (reader, &attr, &abbrev.attrs[i], info_ptr);
c906108c
SS
18376
18377 /* Store the data if it is of an attribute we want to keep in a
c5aa993b 18378 partial symbol table. */
c906108c
SS
18379 switch (attr.name)
18380 {
18381 case DW_AT_name:
48fbe735 18382 switch (tag)
71c25dea
TT
18383 {
18384 case DW_TAG_compile_unit:
95554aad 18385 case DW_TAG_partial_unit:
348e048f 18386 case DW_TAG_type_unit:
71c25dea
TT
18387 /* Compilation units have a DW_AT_name that is a filename, not
18388 a source language identifier. */
18389 case DW_TAG_enumeration_type:
18390 case DW_TAG_enumerator:
18391 /* These tags always have simple identifiers already; no need
18392 to canonicalize them. */
48fbe735 18393 name = DW_STRING (&attr);
71c25dea
TT
18394 break;
18395 default:
48fbe735
YQ
18396 {
18397 struct objfile *objfile = dwarf2_per_objfile->objfile;
18398
18399 name
18400 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
18401 &objfile->per_bfd->storage_obstack);
18402 }
71c25dea
TT
18403 break;
18404 }
c906108c 18405 break;
31ef98ae 18406 case DW_AT_linkage_name:
c906108c 18407 case DW_AT_MIPS_linkage_name:
31ef98ae
TT
18408 /* Note that both forms of linkage name might appear. We
18409 assume they will be the same, and we only store the last
18410 one we see. */
94af9270 18411 if (cu->language == language_ada)
48fbe735
YQ
18412 name = DW_STRING (&attr);
18413 linkage_name = DW_STRING (&attr);
c906108c
SS
18414 break;
18415 case DW_AT_low_pc:
18416 has_low_pc_attr = 1;
48fbe735 18417 lowpc = attr_value_as_address (&attr);
c906108c
SS
18418 break;
18419 case DW_AT_high_pc:
18420 has_high_pc_attr = 1;
48fbe735 18421 highpc = attr_value_as_address (&attr);
31aa7e4e
JB
18422 if (cu->header.version >= 4 && attr_form_is_constant (&attr))
18423 high_pc_relative = 1;
c906108c
SS
18424 break;
18425 case DW_AT_location:
0963b4bd 18426 /* Support the .debug_loc offsets. */
8e19ed76
PS
18427 if (attr_form_is_block (&attr))
18428 {
48fbe735 18429 d.locdesc = DW_BLOCK (&attr);
8e19ed76 18430 }
3690dd37 18431 else if (attr_form_is_section_offset (&attr))
8e19ed76 18432 {
4d3c2250 18433 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
18434 }
18435 else
18436 {
4d3c2250
KB
18437 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
18438 "partial symbol information");
8e19ed76 18439 }
c906108c 18440 break;
c906108c 18441 case DW_AT_external:
48fbe735 18442 is_external = DW_UNSND (&attr);
c906108c
SS
18443 break;
18444 case DW_AT_declaration:
48fbe735 18445 is_declaration = DW_UNSND (&attr);
c906108c
SS
18446 break;
18447 case DW_AT_type:
48fbe735 18448 has_type = 1;
c906108c
SS
18449 break;
18450 case DW_AT_abstract_origin:
18451 case DW_AT_specification:
72bf9492 18452 case DW_AT_extension:
48fbe735
YQ
18453 has_specification = 1;
18454 spec_offset = dwarf2_get_ref_die_offset (&attr);
18455 spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
36586728 18456 || cu->per_cu->is_dwz);
c906108c
SS
18457 break;
18458 case DW_AT_sibling:
18459 /* Ignore absolute siblings, they might point outside of
18460 the current compile unit. */
18461 if (attr.form == DW_FORM_ref_addr)
b98664d3 18462 complaint (_("ignoring absolute DW_AT_sibling"));
c906108c 18463 else
b9502d3f 18464 {
48fbe735 18465 const gdb_byte *buffer = reader->buffer;
9c541725
PA
18466 sect_offset off = dwarf2_get_ref_die_offset (&attr);
18467 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
b9502d3f
WN
18468
18469 if (sibling_ptr < info_ptr)
b98664d3 18470 complaint (_("DW_AT_sibling points backwards"));
22869d73
KS
18471 else if (sibling_ptr > reader->buffer_end)
18472 dwarf2_section_buffer_overflow_complaint (reader->die_section);
b9502d3f 18473 else
48fbe735 18474 sibling = sibling_ptr;
b9502d3f 18475 }
c906108c 18476 break;
fa4028e9 18477 case DW_AT_byte_size:
48fbe735 18478 has_byte_size = 1;
fa4028e9 18479 break;
ff908ebf 18480 case DW_AT_const_value:
48fbe735 18481 has_const_value = 1;
ff908ebf 18482 break;
68511cec
CES
18483 case DW_AT_calling_convention:
18484 /* DWARF doesn't provide a way to identify a program's source-level
18485 entry point. DW_AT_calling_convention attributes are only meant
18486 to describe functions' calling conventions.
18487
18488 However, because it's a necessary piece of information in
0c1b455e
TT
18489 Fortran, and before DWARF 4 DW_CC_program was the only
18490 piece of debugging information whose definition refers to
18491 a 'main program' at all, several compilers marked Fortran
18492 main programs with DW_CC_program --- even when those
18493 functions use the standard calling conventions.
18494
18495 Although DWARF now specifies a way to provide this
18496 information, we support this practice for backward
18497 compatibility. */
68511cec 18498 if (DW_UNSND (&attr) == DW_CC_program
0c1b455e 18499 && cu->language == language_fortran)
48fbe735 18500 main_subprogram = 1;
68511cec 18501 break;
481860b3
GB
18502 case DW_AT_inline:
18503 if (DW_UNSND (&attr) == DW_INL_inlined
18504 || DW_UNSND (&attr) == DW_INL_declared_inlined)
48fbe735 18505 may_be_inlined = 1;
481860b3 18506 break;
95554aad
TT
18507
18508 case DW_AT_import:
48fbe735 18509 if (tag == DW_TAG_imported_unit)
36586728 18510 {
48fbe735
YQ
18511 d.sect_off = dwarf2_get_ref_die_offset (&attr);
18512 is_dwz = (attr.form == DW_FORM_GNU_ref_alt
36586728
TT
18513 || cu->per_cu->is_dwz);
18514 }
95554aad
TT
18515 break;
18516
0c1b455e 18517 case DW_AT_main_subprogram:
48fbe735 18518 main_subprogram = DW_UNSND (&attr);
0c1b455e
TT
18519 break;
18520
c906108c
SS
18521 default:
18522 break;
18523 }
18524 }
18525
91da1414 18526 if (high_pc_relative)
48fbe735 18527 highpc += lowpc;
91da1414 18528
9373cf26
JK
18529 if (has_low_pc_attr && has_high_pc_attr)
18530 {
18531 /* When using the GNU linker, .gnu.linkonce. sections are used to
18532 eliminate duplicate copies of functions and vtables and such.
18533 The linker will arbitrarily choose one and discard the others.
18534 The AT_*_pc values for such functions refer to local labels in
18535 these sections. If the section from that file was discarded, the
18536 labels are not in the output, so the relocs get a value of 0.
18537 If this is a discarded function, mark the pc bounds as invalid,
18538 so that GDB will ignore it. */
48fbe735 18539 if (lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
9373cf26 18540 {
48fbe735 18541 struct objfile *objfile = dwarf2_per_objfile->objfile;
bb5ed363 18542 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9373cf26 18543
b98664d3 18544 complaint (_("DW_AT_low_pc %s is zero "
9d8780f0 18545 "for DIE at %s [in module %s]"),
48fbe735
YQ
18546 paddress (gdbarch, lowpc),
18547 sect_offset_str (sect_off),
9d8780f0 18548 objfile_name (objfile));
9373cf26
JK
18549 }
18550 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
48fbe735 18551 else if (lowpc >= highpc)
9373cf26 18552 {
48fbe735 18553 struct objfile *objfile = dwarf2_per_objfile->objfile;
bb5ed363 18554 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9373cf26 18555
b98664d3 18556 complaint (_("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
9d8780f0 18557 "for DIE at %s [in module %s]"),
48fbe735
YQ
18558 paddress (gdbarch, lowpc),
18559 paddress (gdbarch, highpc),
18560 sect_offset_str (sect_off),
9c541725 18561 objfile_name (objfile));
9373cf26
JK
18562 }
18563 else
48fbe735 18564 has_pc_info = 1;
9373cf26 18565 }
85cbf3d3 18566
c906108c
SS
18567 return info_ptr;
18568}
18569
72bf9492
DJ
18570/* Find a cached partial DIE at OFFSET in CU. */
18571
d590ff25
YQ
18572struct partial_die_info *
18573dwarf2_cu::find_partial_die (sect_offset sect_off)
72bf9492
DJ
18574{
18575 struct partial_die_info *lookup_die = NULL;
6f06d47b 18576 struct partial_die_info part_die (sect_off);
72bf9492 18577
9a3c8263 18578 lookup_die = ((struct partial_die_info *)
d590ff25 18579 htab_find_with_hash (partial_dies, &part_die,
9c541725 18580 to_underlying (sect_off)));
72bf9492 18581
72bf9492
DJ
18582 return lookup_die;
18583}
18584
348e048f
DE
18585/* Find a partial DIE at OFFSET, which may or may not be in CU,
18586 except in the case of .debug_types DIEs which do not reference
18587 outside their CU (they do however referencing other types via
55f1336d 18588 DW_FORM_ref_sig8). */
72bf9492
DJ
18589
18590static struct partial_die_info *
9c541725 18591find_partial_die (sect_offset sect_off, int offset_in_dwz, struct dwarf2_cu *cu)
72bf9492 18592{
518817b3
SM
18593 struct dwarf2_per_objfile *dwarf2_per_objfile
18594 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 18595 struct objfile *objfile = dwarf2_per_objfile->objfile;
5afb4e99
DJ
18596 struct dwarf2_per_cu_data *per_cu = NULL;
18597 struct partial_die_info *pd = NULL;
72bf9492 18598
36586728 18599 if (offset_in_dwz == cu->per_cu->is_dwz
9c541725 18600 && offset_in_cu_p (&cu->header, sect_off))
5afb4e99 18601 {
d590ff25 18602 pd = cu->find_partial_die (sect_off);
5afb4e99
DJ
18603 if (pd != NULL)
18604 return pd;
0d99eb77
DE
18605 /* We missed recording what we needed.
18606 Load all dies and try again. */
18607 per_cu = cu->per_cu;
5afb4e99 18608 }
0d99eb77
DE
18609 else
18610 {
18611 /* TUs don't reference other CUs/TUs (except via type signatures). */
3019eac3 18612 if (cu->per_cu->is_debug_types)
0d99eb77 18613 {
9d8780f0
SM
18614 error (_("Dwarf Error: Type Unit at offset %s contains"
18615 " external reference to offset %s [in module %s].\n"),
18616 sect_offset_str (cu->header.sect_off), sect_offset_str (sect_off),
0d99eb77
DE
18617 bfd_get_filename (objfile->obfd));
18618 }
9c541725 18619 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
ed2dc618 18620 dwarf2_per_objfile);
72bf9492 18621
0d99eb77
DE
18622 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
18623 load_partial_comp_unit (per_cu);
ae038cb0 18624
0d99eb77 18625 per_cu->cu->last_used = 0;
d590ff25 18626 pd = per_cu->cu->find_partial_die (sect_off);
0d99eb77 18627 }
5afb4e99 18628
dee91e82
DE
18629 /* If we didn't find it, and not all dies have been loaded,
18630 load them all and try again. */
18631
5afb4e99
DJ
18632 if (pd == NULL && per_cu->load_all_dies == 0)
18633 {
5afb4e99 18634 per_cu->load_all_dies = 1;
fd820528
DE
18635
18636 /* This is nasty. When we reread the DIEs, somewhere up the call chain
18637 THIS_CU->cu may already be in use. So we can't just free it and
18638 replace its DIEs with the ones we read in. Instead, we leave those
18639 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
18640 and clobber THIS_CU->cu->partial_dies with the hash table for the new
18641 set. */
dee91e82 18642 load_partial_comp_unit (per_cu);
5afb4e99 18643
d590ff25 18644 pd = per_cu->cu->find_partial_die (sect_off);
5afb4e99
DJ
18645 }
18646
18647 if (pd == NULL)
18648 internal_error (__FILE__, __LINE__,
9d8780f0 18649 _("could not find partial DIE %s "
3e43a32a 18650 "in cache [from module %s]\n"),
9d8780f0 18651 sect_offset_str (sect_off), bfd_get_filename (objfile->obfd));
5afb4e99 18652 return pd;
72bf9492
DJ
18653}
18654
abc72ce4
DE
18655/* See if we can figure out if the class lives in a namespace. We do
18656 this by looking for a member function; its demangled name will
18657 contain namespace info, if there is any. */
18658
18659static void
18660guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
18661 struct dwarf2_cu *cu)
18662{
18663 /* NOTE: carlton/2003-10-07: Getting the info this way changes
18664 what template types look like, because the demangler
18665 frequently doesn't give the same name as the debug info. We
18666 could fix this by only using the demangled name to get the
18667 prefix (but see comment in read_structure_type). */
18668
18669 struct partial_die_info *real_pdi;
18670 struct partial_die_info *child_pdi;
18671
18672 /* If this DIE (this DIE's specification, if any) has a parent, then
18673 we should not do this. We'll prepend the parent's fully qualified
18674 name when we create the partial symbol. */
18675
18676 real_pdi = struct_pdi;
18677 while (real_pdi->has_specification)
36586728
TT
18678 real_pdi = find_partial_die (real_pdi->spec_offset,
18679 real_pdi->spec_is_dwz, cu);
abc72ce4
DE
18680
18681 if (real_pdi->die_parent != NULL)
18682 return;
18683
18684 for (child_pdi = struct_pdi->die_child;
18685 child_pdi != NULL;
18686 child_pdi = child_pdi->die_sibling)
18687 {
18688 if (child_pdi->tag == DW_TAG_subprogram
18689 && child_pdi->linkage_name != NULL)
18690 {
18691 char *actual_class_name
18692 = language_class_name_from_physname (cu->language_defn,
18693 child_pdi->linkage_name);
18694 if (actual_class_name != NULL)
18695 {
518817b3 18696 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
abc72ce4 18697 struct_pdi->name
224c3ddb 18698 = ((const char *)
e3b94546 18699 obstack_copy0 (&objfile->per_bfd->storage_obstack,
224c3ddb
SM
18700 actual_class_name,
18701 strlen (actual_class_name)));
abc72ce4
DE
18702 xfree (actual_class_name);
18703 }
18704 break;
18705 }
18706 }
18707}
18708
52356b79
YQ
18709void
18710partial_die_info::fixup (struct dwarf2_cu *cu)
72bf9492 18711{
abc72ce4
DE
18712 /* Once we've fixed up a die, there's no point in doing so again.
18713 This also avoids a memory leak if we were to call
18714 guess_partial_die_structure_name multiple times. */
52356b79 18715 if (fixup_called)
abc72ce4
DE
18716 return;
18717
72bf9492
DJ
18718 /* If we found a reference attribute and the DIE has no name, try
18719 to find a name in the referred to DIE. */
18720
52356b79 18721 if (name == NULL && has_specification)
72bf9492
DJ
18722 {
18723 struct partial_die_info *spec_die;
72bf9492 18724
52356b79 18725 spec_die = find_partial_die (spec_offset, spec_is_dwz, cu);
72bf9492 18726
52356b79 18727 spec_die->fixup (cu);
72bf9492
DJ
18728
18729 if (spec_die->name)
18730 {
52356b79 18731 name = spec_die->name;
72bf9492
DJ
18732
18733 /* Copy DW_AT_external attribute if it is set. */
18734 if (spec_die->is_external)
52356b79 18735 is_external = spec_die->is_external;
72bf9492
DJ
18736 }
18737 }
18738
18739 /* Set default names for some unnamed DIEs. */
72bf9492 18740
52356b79
YQ
18741 if (name == NULL && tag == DW_TAG_namespace)
18742 name = CP_ANONYMOUS_NAMESPACE_STR;
72bf9492 18743
abc72ce4
DE
18744 /* If there is no parent die to provide a namespace, and there are
18745 children, see if we can determine the namespace from their linkage
122d1940 18746 name. */
abc72ce4 18747 if (cu->language == language_cplus
518817b3
SM
18748 && !VEC_empty (dwarf2_section_info_def,
18749 cu->per_cu->dwarf2_per_objfile->types)
52356b79
YQ
18750 && die_parent == NULL
18751 && has_children
18752 && (tag == DW_TAG_class_type
18753 || tag == DW_TAG_structure_type
18754 || tag == DW_TAG_union_type))
18755 guess_partial_die_structure_name (this, cu);
abc72ce4 18756
53832f31
TT
18757 /* GCC might emit a nameless struct or union that has a linkage
18758 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
52356b79
YQ
18759 if (name == NULL
18760 && (tag == DW_TAG_class_type
18761 || tag == DW_TAG_interface_type
18762 || tag == DW_TAG_structure_type
18763 || tag == DW_TAG_union_type)
18764 && linkage_name != NULL)
53832f31
TT
18765 {
18766 char *demangled;
18767
52356b79 18768 demangled = gdb_demangle (linkage_name, DMGL_TYPES);
53832f31
TT
18769 if (demangled)
18770 {
96408a79
SA
18771 const char *base;
18772
18773 /* Strip any leading namespaces/classes, keep only the base name.
18774 DW_AT_name for named DIEs does not contain the prefixes. */
18775 base = strrchr (demangled, ':');
18776 if (base && base > demangled && base[-1] == ':')
18777 base++;
18778 else
18779 base = demangled;
18780
518817b3 18781 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
52356b79 18782 name
224c3ddb 18783 = ((const char *)
e3b94546 18784 obstack_copy0 (&objfile->per_bfd->storage_obstack,
224c3ddb 18785 base, strlen (base)));
53832f31
TT
18786 xfree (demangled);
18787 }
18788 }
18789
52356b79 18790 fixup_called = 1;
72bf9492
DJ
18791}
18792
a8329558 18793/* Read an attribute value described by an attribute form. */
c906108c 18794
d521ce57 18795static const gdb_byte *
dee91e82
DE
18796read_attribute_value (const struct die_reader_specs *reader,
18797 struct attribute *attr, unsigned form,
43988095 18798 LONGEST implicit_const, const gdb_byte *info_ptr)
c906108c 18799{
dee91e82 18800 struct dwarf2_cu *cu = reader->cu;
518817b3
SM
18801 struct dwarf2_per_objfile *dwarf2_per_objfile
18802 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 18803 struct objfile *objfile = dwarf2_per_objfile->objfile;
3e29f34a 18804 struct gdbarch *gdbarch = get_objfile_arch (objfile);
dee91e82 18805 bfd *abfd = reader->abfd;
e7c27a73 18806 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
18807 unsigned int bytes_read;
18808 struct dwarf_block *blk;
18809
aead7601 18810 attr->form = (enum dwarf_form) form;
a8329558 18811 switch (form)
c906108c 18812 {
c906108c 18813 case DW_FORM_ref_addr:
ae411497 18814 if (cu->header.version == 2)
4568ecf9 18815 DW_UNSND (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
ae411497 18816 else
4568ecf9
DE
18817 DW_UNSND (attr) = read_offset (abfd, info_ptr,
18818 &cu->header, &bytes_read);
ae411497
TT
18819 info_ptr += bytes_read;
18820 break;
36586728
TT
18821 case DW_FORM_GNU_ref_alt:
18822 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
18823 info_ptr += bytes_read;
18824 break;
ae411497 18825 case DW_FORM_addr:
e7c27a73 18826 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
3e29f34a 18827 DW_ADDR (attr) = gdbarch_adjust_dwarf2_addr (gdbarch, DW_ADDR (attr));
107d2387 18828 info_ptr += bytes_read;
c906108c
SS
18829 break;
18830 case DW_FORM_block2:
7b5a2f43 18831 blk = dwarf_alloc_block (cu);
c906108c
SS
18832 blk->size = read_2_bytes (abfd, info_ptr);
18833 info_ptr += 2;
18834 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
18835 info_ptr += blk->size;
18836 DW_BLOCK (attr) = blk;
18837 break;
18838 case DW_FORM_block4:
7b5a2f43 18839 blk = dwarf_alloc_block (cu);
c906108c
SS
18840 blk->size = read_4_bytes (abfd, info_ptr);
18841 info_ptr += 4;
18842 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
18843 info_ptr += blk->size;
18844 DW_BLOCK (attr) = blk;
18845 break;
18846 case DW_FORM_data2:
18847 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
18848 info_ptr += 2;
18849 break;
18850 case DW_FORM_data4:
18851 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
18852 info_ptr += 4;
18853 break;
18854 case DW_FORM_data8:
18855 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
18856 info_ptr += 8;
18857 break;
0224619f
JK
18858 case DW_FORM_data16:
18859 blk = dwarf_alloc_block (cu);
18860 blk->size = 16;
18861 blk->data = read_n_bytes (abfd, info_ptr, 16);
18862 info_ptr += 16;
18863 DW_BLOCK (attr) = blk;
18864 break;
2dc7f7b3
TT
18865 case DW_FORM_sec_offset:
18866 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
18867 info_ptr += bytes_read;
18868 break;
c906108c 18869 case DW_FORM_string:
9b1c24c8 18870 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
8285870a 18871 DW_STRING_IS_CANONICAL (attr) = 0;
c906108c
SS
18872 info_ptr += bytes_read;
18873 break;
4bdf3d34 18874 case DW_FORM_strp:
36586728
TT
18875 if (!cu->per_cu->is_dwz)
18876 {
ed2dc618
SM
18877 DW_STRING (attr) = read_indirect_string (dwarf2_per_objfile,
18878 abfd, info_ptr, cu_header,
36586728
TT
18879 &bytes_read);
18880 DW_STRING_IS_CANONICAL (attr) = 0;
18881 info_ptr += bytes_read;
18882 break;
18883 }
18884 /* FALLTHROUGH */
43988095
JK
18885 case DW_FORM_line_strp:
18886 if (!cu->per_cu->is_dwz)
18887 {
ed2dc618
SM
18888 DW_STRING (attr) = read_indirect_line_string (dwarf2_per_objfile,
18889 abfd, info_ptr,
43988095
JK
18890 cu_header, &bytes_read);
18891 DW_STRING_IS_CANONICAL (attr) = 0;
18892 info_ptr += bytes_read;
18893 break;
18894 }
18895 /* FALLTHROUGH */
36586728
TT
18896 case DW_FORM_GNU_strp_alt:
18897 {
ed2dc618 18898 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
36586728
TT
18899 LONGEST str_offset = read_offset (abfd, info_ptr, cu_header,
18900 &bytes_read);
18901
ed2dc618
SM
18902 DW_STRING (attr) = read_indirect_string_from_dwz (objfile,
18903 dwz, str_offset);
36586728
TT
18904 DW_STRING_IS_CANONICAL (attr) = 0;
18905 info_ptr += bytes_read;
18906 }
4bdf3d34 18907 break;
2dc7f7b3 18908 case DW_FORM_exprloc:
c906108c 18909 case DW_FORM_block:
7b5a2f43 18910 blk = dwarf_alloc_block (cu);
c906108c
SS
18911 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18912 info_ptr += bytes_read;
18913 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
18914 info_ptr += blk->size;
18915 DW_BLOCK (attr) = blk;
18916 break;
18917 case DW_FORM_block1:
7b5a2f43 18918 blk = dwarf_alloc_block (cu);
c906108c
SS
18919 blk->size = read_1_byte (abfd, info_ptr);
18920 info_ptr += 1;
18921 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
18922 info_ptr += blk->size;
18923 DW_BLOCK (attr) = blk;
18924 break;
18925 case DW_FORM_data1:
18926 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
18927 info_ptr += 1;
18928 break;
18929 case DW_FORM_flag:
18930 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
18931 info_ptr += 1;
18932 break;
2dc7f7b3
TT
18933 case DW_FORM_flag_present:
18934 DW_UNSND (attr) = 1;
18935 break;
c906108c
SS
18936 case DW_FORM_sdata:
18937 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
18938 info_ptr += bytes_read;
18939 break;
18940 case DW_FORM_udata:
18941 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18942 info_ptr += bytes_read;
18943 break;
18944 case DW_FORM_ref1:
9c541725 18945 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 18946 + read_1_byte (abfd, info_ptr));
c906108c
SS
18947 info_ptr += 1;
18948 break;
18949 case DW_FORM_ref2:
9c541725 18950 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 18951 + read_2_bytes (abfd, info_ptr));
c906108c
SS
18952 info_ptr += 2;
18953 break;
18954 case DW_FORM_ref4:
9c541725 18955 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 18956 + read_4_bytes (abfd, info_ptr));
c906108c
SS
18957 info_ptr += 4;
18958 break;
613e1657 18959 case DW_FORM_ref8:
9c541725 18960 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 18961 + read_8_bytes (abfd, info_ptr));
613e1657
KB
18962 info_ptr += 8;
18963 break;
55f1336d 18964 case DW_FORM_ref_sig8:
ac9ec31b 18965 DW_SIGNATURE (attr) = read_8_bytes (abfd, info_ptr);
348e048f
DE
18966 info_ptr += 8;
18967 break;
c906108c 18968 case DW_FORM_ref_udata:
9c541725 18969 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 18970 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
c906108c
SS
18971 info_ptr += bytes_read;
18972 break;
c906108c 18973 case DW_FORM_indirect:
a8329558
KW
18974 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18975 info_ptr += bytes_read;
43988095
JK
18976 if (form == DW_FORM_implicit_const)
18977 {
18978 implicit_const = read_signed_leb128 (abfd, info_ptr, &bytes_read);
18979 info_ptr += bytes_read;
18980 }
18981 info_ptr = read_attribute_value (reader, attr, form, implicit_const,
18982 info_ptr);
18983 break;
18984 case DW_FORM_implicit_const:
18985 DW_SND (attr) = implicit_const;
a8329558 18986 break;
3019eac3
DE
18987 case DW_FORM_GNU_addr_index:
18988 if (reader->dwo_file == NULL)
18989 {
18990 /* For now flag a hard error.
18991 Later we can turn this into a complaint. */
18992 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
18993 dwarf_form_name (form),
18994 bfd_get_filename (abfd));
18995 }
18996 DW_ADDR (attr) = read_addr_index_from_leb128 (cu, info_ptr, &bytes_read);
18997 info_ptr += bytes_read;
18998 break;
18999 case DW_FORM_GNU_str_index:
19000 if (reader->dwo_file == NULL)
19001 {
19002 /* For now flag a hard error.
19003 Later we can turn this into a complaint if warranted. */
19004 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
19005 dwarf_form_name (form),
19006 bfd_get_filename (abfd));
19007 }
19008 {
19009 ULONGEST str_index =
19010 read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19011
342587c4 19012 DW_STRING (attr) = read_str_index (reader, str_index);
3019eac3
DE
19013 DW_STRING_IS_CANONICAL (attr) = 0;
19014 info_ptr += bytes_read;
19015 }
19016 break;
c906108c 19017 default:
8a3fe4f8 19018 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
659b0389
ML
19019 dwarf_form_name (form),
19020 bfd_get_filename (abfd));
c906108c 19021 }
28e94949 19022
36586728 19023 /* Super hack. */
7771576e 19024 if (cu->per_cu->is_dwz && attr_form_is_ref (attr))
36586728
TT
19025 attr->form = DW_FORM_GNU_ref_alt;
19026
28e94949
JB
19027 /* We have seen instances where the compiler tried to emit a byte
19028 size attribute of -1 which ended up being encoded as an unsigned
19029 0xffffffff. Although 0xffffffff is technically a valid size value,
19030 an object of this size seems pretty unlikely so we can relatively
19031 safely treat these cases as if the size attribute was invalid and
19032 treat them as zero by default. */
19033 if (attr->name == DW_AT_byte_size
19034 && form == DW_FORM_data4
19035 && DW_UNSND (attr) >= 0xffffffff)
01c66ae6
JB
19036 {
19037 complaint
b98664d3 19038 (_("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
43bbcdc2 19039 hex_string (DW_UNSND (attr)));
01c66ae6
JB
19040 DW_UNSND (attr) = 0;
19041 }
28e94949 19042
c906108c
SS
19043 return info_ptr;
19044}
19045
a8329558
KW
19046/* Read an attribute described by an abbreviated attribute. */
19047
d521ce57 19048static const gdb_byte *
dee91e82
DE
19049read_attribute (const struct die_reader_specs *reader,
19050 struct attribute *attr, struct attr_abbrev *abbrev,
d521ce57 19051 const gdb_byte *info_ptr)
a8329558
KW
19052{
19053 attr->name = abbrev->name;
43988095
JK
19054 return read_attribute_value (reader, attr, abbrev->form,
19055 abbrev->implicit_const, info_ptr);
a8329558
KW
19056}
19057
0963b4bd 19058/* Read dwarf information from a buffer. */
c906108c
SS
19059
19060static unsigned int
a1855c1d 19061read_1_byte (bfd *abfd, const gdb_byte *buf)
c906108c 19062{
fe1b8b76 19063 return bfd_get_8 (abfd, buf);
c906108c
SS
19064}
19065
19066static int
a1855c1d 19067read_1_signed_byte (bfd *abfd, const gdb_byte *buf)
c906108c 19068{
fe1b8b76 19069 return bfd_get_signed_8 (abfd, buf);
c906108c
SS
19070}
19071
19072static unsigned int
a1855c1d 19073read_2_bytes (bfd *abfd, const gdb_byte *buf)
c906108c 19074{
fe1b8b76 19075 return bfd_get_16 (abfd, buf);
c906108c
SS
19076}
19077
21ae7a4d 19078static int
a1855c1d 19079read_2_signed_bytes (bfd *abfd, const gdb_byte *buf)
21ae7a4d
JK
19080{
19081 return bfd_get_signed_16 (abfd, buf);
19082}
19083
c906108c 19084static unsigned int
a1855c1d 19085read_4_bytes (bfd *abfd, const gdb_byte *buf)
c906108c 19086{
fe1b8b76 19087 return bfd_get_32 (abfd, buf);
c906108c
SS
19088}
19089
21ae7a4d 19090static int
a1855c1d 19091read_4_signed_bytes (bfd *abfd, const gdb_byte *buf)
21ae7a4d
JK
19092{
19093 return bfd_get_signed_32 (abfd, buf);
19094}
19095
93311388 19096static ULONGEST
a1855c1d 19097read_8_bytes (bfd *abfd, const gdb_byte *buf)
c906108c 19098{
fe1b8b76 19099 return bfd_get_64 (abfd, buf);
c906108c
SS
19100}
19101
19102static CORE_ADDR
d521ce57 19103read_address (bfd *abfd, const gdb_byte *buf, struct dwarf2_cu *cu,
891d2f0b 19104 unsigned int *bytes_read)
c906108c 19105{
e7c27a73 19106 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
19107 CORE_ADDR retval = 0;
19108
107d2387 19109 if (cu_header->signed_addr_p)
c906108c 19110 {
107d2387
AC
19111 switch (cu_header->addr_size)
19112 {
19113 case 2:
fe1b8b76 19114 retval = bfd_get_signed_16 (abfd, buf);
107d2387
AC
19115 break;
19116 case 4:
fe1b8b76 19117 retval = bfd_get_signed_32 (abfd, buf);
107d2387
AC
19118 break;
19119 case 8:
fe1b8b76 19120 retval = bfd_get_signed_64 (abfd, buf);
107d2387
AC
19121 break;
19122 default:
8e65ff28 19123 internal_error (__FILE__, __LINE__,
e2e0b3e5 19124 _("read_address: bad switch, signed [in module %s]"),
659b0389 19125 bfd_get_filename (abfd));
107d2387
AC
19126 }
19127 }
19128 else
19129 {
19130 switch (cu_header->addr_size)
19131 {
19132 case 2:
fe1b8b76 19133 retval = bfd_get_16 (abfd, buf);
107d2387
AC
19134 break;
19135 case 4:
fe1b8b76 19136 retval = bfd_get_32 (abfd, buf);
107d2387
AC
19137 break;
19138 case 8:
fe1b8b76 19139 retval = bfd_get_64 (abfd, buf);
107d2387
AC
19140 break;
19141 default:
8e65ff28 19142 internal_error (__FILE__, __LINE__,
a73c6dcd
MS
19143 _("read_address: bad switch, "
19144 "unsigned [in module %s]"),
659b0389 19145 bfd_get_filename (abfd));
107d2387 19146 }
c906108c 19147 }
64367e0a 19148
107d2387
AC
19149 *bytes_read = cu_header->addr_size;
19150 return retval;
c906108c
SS
19151}
19152
f7ef9339 19153/* Read the initial length from a section. The (draft) DWARF 3
613e1657
KB
19154 specification allows the initial length to take up either 4 bytes
19155 or 12 bytes. If the first 4 bytes are 0xffffffff, then the next 8
19156 bytes describe the length and all offsets will be 8 bytes in length
19157 instead of 4.
19158
f7ef9339
KB
19159 An older, non-standard 64-bit format is also handled by this
19160 function. The older format in question stores the initial length
19161 as an 8-byte quantity without an escape value. Lengths greater
19162 than 2^32 aren't very common which means that the initial 4 bytes
19163 is almost always zero. Since a length value of zero doesn't make
19164 sense for the 32-bit format, this initial zero can be considered to
19165 be an escape value which indicates the presence of the older 64-bit
19166 format. As written, the code can't detect (old format) lengths
917c78fc
MK
19167 greater than 4GB. If it becomes necessary to handle lengths
19168 somewhat larger than 4GB, we could allow other small values (such
19169 as the non-sensical values of 1, 2, and 3) to also be used as
19170 escape values indicating the presence of the old format.
f7ef9339 19171
917c78fc
MK
19172 The value returned via bytes_read should be used to increment the
19173 relevant pointer after calling read_initial_length().
c764a876 19174
613e1657
KB
19175 [ Note: read_initial_length() and read_offset() are based on the
19176 document entitled "DWARF Debugging Information Format", revision
f7ef9339 19177 3, draft 8, dated November 19, 2001. This document was obtained
613e1657
KB
19178 from:
19179
f7ef9339 19180 http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
6e70227d 19181
613e1657
KB
19182 This document is only a draft and is subject to change. (So beware.)
19183
f7ef9339 19184 Details regarding the older, non-standard 64-bit format were
917c78fc
MK
19185 determined empirically by examining 64-bit ELF files produced by
19186 the SGI toolchain on an IRIX 6.5 machine.
f7ef9339
KB
19187
19188 - Kevin, July 16, 2002
613e1657
KB
19189 ] */
19190
19191static LONGEST
d521ce57 19192read_initial_length (bfd *abfd, const gdb_byte *buf, unsigned int *bytes_read)
613e1657 19193{
fe1b8b76 19194 LONGEST length = bfd_get_32 (abfd, buf);
613e1657 19195
dd373385 19196 if (length == 0xffffffff)
613e1657 19197 {
fe1b8b76 19198 length = bfd_get_64 (abfd, buf + 4);
613e1657 19199 *bytes_read = 12;
613e1657 19200 }
dd373385 19201 else if (length == 0)
f7ef9339 19202 {
dd373385 19203 /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX. */
fe1b8b76 19204 length = bfd_get_64 (abfd, buf);
f7ef9339 19205 *bytes_read = 8;
f7ef9339 19206 }
613e1657
KB
19207 else
19208 {
19209 *bytes_read = 4;
613e1657
KB
19210 }
19211
c764a876
DE
19212 return length;
19213}
dd373385 19214
c764a876
DE
19215/* Cover function for read_initial_length.
19216 Returns the length of the object at BUF, and stores the size of the
19217 initial length in *BYTES_READ and stores the size that offsets will be in
19218 *OFFSET_SIZE.
19219 If the initial length size is not equivalent to that specified in
19220 CU_HEADER then issue a complaint.
19221 This is useful when reading non-comp-unit headers. */
dd373385 19222
c764a876 19223static LONGEST
d521ce57 19224read_checked_initial_length_and_offset (bfd *abfd, const gdb_byte *buf,
c764a876
DE
19225 const struct comp_unit_head *cu_header,
19226 unsigned int *bytes_read,
19227 unsigned int *offset_size)
19228{
19229 LONGEST length = read_initial_length (abfd, buf, bytes_read);
19230
19231 gdb_assert (cu_header->initial_length_size == 4
19232 || cu_header->initial_length_size == 8
19233 || cu_header->initial_length_size == 12);
19234
19235 if (cu_header->initial_length_size != *bytes_read)
b98664d3 19236 complaint (_("intermixed 32-bit and 64-bit DWARF sections"));
dd373385 19237
c764a876 19238 *offset_size = (*bytes_read == 4) ? 4 : 8;
dd373385 19239 return length;
613e1657
KB
19240}
19241
19242/* Read an offset from the data stream. The size of the offset is
917c78fc 19243 given by cu_header->offset_size. */
613e1657
KB
19244
19245static LONGEST
d521ce57
TT
19246read_offset (bfd *abfd, const gdb_byte *buf,
19247 const struct comp_unit_head *cu_header,
891d2f0b 19248 unsigned int *bytes_read)
c764a876
DE
19249{
19250 LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
9a619af0 19251
c764a876
DE
19252 *bytes_read = cu_header->offset_size;
19253 return offset;
19254}
19255
19256/* Read an offset from the data stream. */
19257
19258static LONGEST
d521ce57 19259read_offset_1 (bfd *abfd, const gdb_byte *buf, unsigned int offset_size)
613e1657
KB
19260{
19261 LONGEST retval = 0;
19262
c764a876 19263 switch (offset_size)
613e1657
KB
19264 {
19265 case 4:
fe1b8b76 19266 retval = bfd_get_32 (abfd, buf);
613e1657
KB
19267 break;
19268 case 8:
fe1b8b76 19269 retval = bfd_get_64 (abfd, buf);
613e1657
KB
19270 break;
19271 default:
8e65ff28 19272 internal_error (__FILE__, __LINE__,
c764a876 19273 _("read_offset_1: bad switch [in module %s]"),
659b0389 19274 bfd_get_filename (abfd));
613e1657
KB
19275 }
19276
917c78fc 19277 return retval;
613e1657
KB
19278}
19279
d521ce57
TT
19280static const gdb_byte *
19281read_n_bytes (bfd *abfd, const gdb_byte *buf, unsigned int size)
c906108c
SS
19282{
19283 /* If the size of a host char is 8 bits, we can return a pointer
19284 to the buffer, otherwise we have to copy the data to a buffer
19285 allocated on the temporary obstack. */
4bdf3d34 19286 gdb_assert (HOST_CHAR_BIT == 8);
c906108c 19287 return buf;
c906108c
SS
19288}
19289
d521ce57
TT
19290static const char *
19291read_direct_string (bfd *abfd, const gdb_byte *buf,
19292 unsigned int *bytes_read_ptr)
c906108c
SS
19293{
19294 /* If the size of a host char is 8 bits, we can return a pointer
19295 to the string, otherwise we have to copy the string to a buffer
19296 allocated on the temporary obstack. */
4bdf3d34 19297 gdb_assert (HOST_CHAR_BIT == 8);
c906108c
SS
19298 if (*buf == '\0')
19299 {
19300 *bytes_read_ptr = 1;
19301 return NULL;
19302 }
d521ce57
TT
19303 *bytes_read_ptr = strlen ((const char *) buf) + 1;
19304 return (const char *) buf;
4bdf3d34
JJ
19305}
19306
43988095
JK
19307/* Return pointer to string at section SECT offset STR_OFFSET with error
19308 reporting strings FORM_NAME and SECT_NAME. */
19309
d521ce57 19310static const char *
ed2dc618
SM
19311read_indirect_string_at_offset_from (struct objfile *objfile,
19312 bfd *abfd, LONGEST str_offset,
43988095
JK
19313 struct dwarf2_section_info *sect,
19314 const char *form_name,
19315 const char *sect_name)
19316{
ed2dc618 19317 dwarf2_read_section (objfile, sect);
43988095
JK
19318 if (sect->buffer == NULL)
19319 error (_("%s used without %s section [in module %s]"),
19320 form_name, sect_name, bfd_get_filename (abfd));
19321 if (str_offset >= sect->size)
19322 error (_("%s pointing outside of %s section [in module %s]"),
19323 form_name, sect_name, bfd_get_filename (abfd));
4bdf3d34 19324 gdb_assert (HOST_CHAR_BIT == 8);
43988095 19325 if (sect->buffer[str_offset] == '\0')
4bdf3d34 19326 return NULL;
43988095
JK
19327 return (const char *) (sect->buffer + str_offset);
19328}
19329
19330/* Return pointer to string at .debug_str offset STR_OFFSET. */
19331
19332static const char *
ed2dc618
SM
19333read_indirect_string_at_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
19334 bfd *abfd, LONGEST str_offset)
43988095 19335{
ed2dc618
SM
19336 return read_indirect_string_at_offset_from (dwarf2_per_objfile->objfile,
19337 abfd, str_offset,
43988095
JK
19338 &dwarf2_per_objfile->str,
19339 "DW_FORM_strp", ".debug_str");
19340}
19341
19342/* Return pointer to string at .debug_line_str offset STR_OFFSET. */
19343
19344static const char *
ed2dc618
SM
19345read_indirect_line_string_at_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
19346 bfd *abfd, LONGEST str_offset)
43988095 19347{
ed2dc618
SM
19348 return read_indirect_string_at_offset_from (dwarf2_per_objfile->objfile,
19349 abfd, str_offset,
43988095
JK
19350 &dwarf2_per_objfile->line_str,
19351 "DW_FORM_line_strp",
19352 ".debug_line_str");
c906108c
SS
19353}
19354
36586728
TT
19355/* Read a string at offset STR_OFFSET in the .debug_str section from
19356 the .dwz file DWZ. Throw an error if the offset is too large. If
19357 the string consists of a single NUL byte, return NULL; otherwise
19358 return a pointer to the string. */
19359
d521ce57 19360static const char *
ed2dc618
SM
19361read_indirect_string_from_dwz (struct objfile *objfile, struct dwz_file *dwz,
19362 LONGEST str_offset)
36586728 19363{
ed2dc618 19364 dwarf2_read_section (objfile, &dwz->str);
36586728
TT
19365
19366 if (dwz->str.buffer == NULL)
19367 error (_("DW_FORM_GNU_strp_alt used without .debug_str "
19368 "section [in module %s]"),
19369 bfd_get_filename (dwz->dwz_bfd));
19370 if (str_offset >= dwz->str.size)
19371 error (_("DW_FORM_GNU_strp_alt pointing outside of "
19372 ".debug_str section [in module %s]"),
19373 bfd_get_filename (dwz->dwz_bfd));
19374 gdb_assert (HOST_CHAR_BIT == 8);
19375 if (dwz->str.buffer[str_offset] == '\0')
19376 return NULL;
d521ce57 19377 return (const char *) (dwz->str.buffer + str_offset);
36586728
TT
19378}
19379
43988095
JK
19380/* Return pointer to string at .debug_str offset as read from BUF.
19381 BUF is assumed to be in a compilation unit described by CU_HEADER.
19382 Return *BYTES_READ_PTR count of bytes read from BUF. */
19383
d521ce57 19384static const char *
ed2dc618
SM
19385read_indirect_string (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *abfd,
19386 const gdb_byte *buf,
cf2c3c16
TT
19387 const struct comp_unit_head *cu_header,
19388 unsigned int *bytes_read_ptr)
19389{
19390 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
19391
ed2dc618 19392 return read_indirect_string_at_offset (dwarf2_per_objfile, abfd, str_offset);
cf2c3c16
TT
19393}
19394
43988095
JK
19395/* Return pointer to string at .debug_line_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
19399static const char *
ed2dc618
SM
19400read_indirect_line_string (struct dwarf2_per_objfile *dwarf2_per_objfile,
19401 bfd *abfd, const gdb_byte *buf,
43988095
JK
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
SM
19407 return read_indirect_line_string_at_offset (dwarf2_per_objfile, abfd,
19408 str_offset);
43988095
JK
19409}
19410
19411ULONGEST
d521ce57 19412read_unsigned_leb128 (bfd *abfd, const gdb_byte *buf,
43988095 19413 unsigned int *bytes_read_ptr)
c906108c 19414{
12df843f 19415 ULONGEST result;
ce5d95e1 19416 unsigned int num_read;
870f88f7 19417 int shift;
c906108c
SS
19418 unsigned char byte;
19419
19420 result = 0;
19421 shift = 0;
19422 num_read = 0;
c906108c
SS
19423 while (1)
19424 {
fe1b8b76 19425 byte = bfd_get_8 (abfd, buf);
c906108c
SS
19426 buf++;
19427 num_read++;
12df843f 19428 result |= ((ULONGEST) (byte & 127) << shift);
c906108c
SS
19429 if ((byte & 128) == 0)
19430 {
19431 break;
19432 }
19433 shift += 7;
19434 }
19435 *bytes_read_ptr = num_read;
19436 return result;
19437}
19438
12df843f 19439static LONGEST
d521ce57
TT
19440read_signed_leb128 (bfd *abfd, const gdb_byte *buf,
19441 unsigned int *bytes_read_ptr)
c906108c 19442{
12df843f 19443 LONGEST result;
870f88f7 19444 int shift, num_read;
c906108c
SS
19445 unsigned char byte;
19446
19447 result = 0;
19448 shift = 0;
c906108c 19449 num_read = 0;
c906108c
SS
19450 while (1)
19451 {
fe1b8b76 19452 byte = bfd_get_8 (abfd, buf);
c906108c
SS
19453 buf++;
19454 num_read++;
12df843f 19455 result |= ((LONGEST) (byte & 127) << shift);
c906108c
SS
19456 shift += 7;
19457 if ((byte & 128) == 0)
19458 {
19459 break;
19460 }
19461 }
77e0b926 19462 if ((shift < 8 * sizeof (result)) && (byte & 0x40))
12df843f 19463 result |= -(((LONGEST) 1) << shift);
c906108c
SS
19464 *bytes_read_ptr = num_read;
19465 return result;
19466}
19467
3019eac3
DE
19468/* Given index ADDR_INDEX in .debug_addr, fetch the value.
19469 ADDR_BASE is the DW_AT_GNU_addr_base attribute or zero.
19470 ADDR_SIZE is the size of addresses from the CU header. */
19471
19472static CORE_ADDR
ed2dc618
SM
19473read_addr_index_1 (struct dwarf2_per_objfile *dwarf2_per_objfile,
19474 unsigned int addr_index, ULONGEST addr_base, int addr_size)
3019eac3
DE
19475{
19476 struct objfile *objfile = dwarf2_per_objfile->objfile;
19477 bfd *abfd = objfile->obfd;
19478 const gdb_byte *info_ptr;
19479
19480 dwarf2_read_section (objfile, &dwarf2_per_objfile->addr);
19481 if (dwarf2_per_objfile->addr.buffer == NULL)
19482 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
4262abfb 19483 objfile_name (objfile));
3019eac3
DE
19484 if (addr_base + addr_index * addr_size >= dwarf2_per_objfile->addr.size)
19485 error (_("DW_FORM_addr_index pointing outside of "
19486 ".debug_addr section [in module %s]"),
4262abfb 19487 objfile_name (objfile));
3019eac3
DE
19488 info_ptr = (dwarf2_per_objfile->addr.buffer
19489 + addr_base + addr_index * addr_size);
19490 if (addr_size == 4)
19491 return bfd_get_32 (abfd, info_ptr);
19492 else
19493 return bfd_get_64 (abfd, info_ptr);
19494}
19495
19496/* Given index ADDR_INDEX in .debug_addr, fetch the value. */
19497
19498static CORE_ADDR
19499read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
19500{
518817b3
SM
19501 return read_addr_index_1 (cu->per_cu->dwarf2_per_objfile, addr_index,
19502 cu->addr_base, cu->header.addr_size);
3019eac3
DE
19503}
19504
19505/* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
19506
19507static CORE_ADDR
d521ce57 19508read_addr_index_from_leb128 (struct dwarf2_cu *cu, const gdb_byte *info_ptr,
3019eac3
DE
19509 unsigned int *bytes_read)
19510{
518817b3 19511 bfd *abfd = cu->per_cu->dwarf2_per_objfile->objfile->obfd;
3019eac3
DE
19512 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
19513
19514 return read_addr_index (cu, addr_index);
19515}
19516
19517/* Data structure to pass results from dwarf2_read_addr_index_reader
19518 back to dwarf2_read_addr_index. */
19519
19520struct dwarf2_read_addr_index_data
19521{
19522 ULONGEST addr_base;
19523 int addr_size;
19524};
19525
19526/* die_reader_func for dwarf2_read_addr_index. */
19527
19528static void
19529dwarf2_read_addr_index_reader (const struct die_reader_specs *reader,
d521ce57 19530 const gdb_byte *info_ptr,
3019eac3
DE
19531 struct die_info *comp_unit_die,
19532 int has_children,
19533 void *data)
19534{
19535 struct dwarf2_cu *cu = reader->cu;
19536 struct dwarf2_read_addr_index_data *aidata =
19537 (struct dwarf2_read_addr_index_data *) data;
19538
19539 aidata->addr_base = cu->addr_base;
19540 aidata->addr_size = cu->header.addr_size;
19541}
19542
19543/* Given an index in .debug_addr, fetch the value.
19544 NOTE: This can be called during dwarf expression evaluation,
19545 long after the debug information has been read, and thus per_cu->cu
19546 may no longer exist. */
19547
19548CORE_ADDR
19549dwarf2_read_addr_index (struct dwarf2_per_cu_data *per_cu,
19550 unsigned int addr_index)
19551{
ed2dc618 19552 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
3019eac3
DE
19553 struct dwarf2_cu *cu = per_cu->cu;
19554 ULONGEST addr_base;
19555 int addr_size;
19556
3019eac3
DE
19557 /* We need addr_base and addr_size.
19558 If we don't have PER_CU->cu, we have to get it.
19559 Nasty, but the alternative is storing the needed info in PER_CU,
19560 which at this point doesn't seem justified: it's not clear how frequently
19561 it would get used and it would increase the size of every PER_CU.
19562 Entry points like dwarf2_per_cu_addr_size do a similar thing
19563 so we're not in uncharted territory here.
19564 Alas we need to be a bit more complicated as addr_base is contained
19565 in the DIE.
19566
19567 We don't need to read the entire CU(/TU).
19568 We just need the header and top level die.
a1b64ce1 19569
3019eac3 19570 IWBN to use the aging mechanism to let us lazily later discard the CU.
a1b64ce1 19571 For now we skip this optimization. */
3019eac3
DE
19572
19573 if (cu != NULL)
19574 {
19575 addr_base = cu->addr_base;
19576 addr_size = cu->header.addr_size;
19577 }
19578 else
19579 {
19580 struct dwarf2_read_addr_index_data aidata;
19581
a1b64ce1
DE
19582 /* Note: We can't use init_cutu_and_read_dies_simple here,
19583 we need addr_base. */
58f0c718 19584 init_cutu_and_read_dies (per_cu, NULL, 0, 0, false,
a1b64ce1 19585 dwarf2_read_addr_index_reader, &aidata);
3019eac3
DE
19586 addr_base = aidata.addr_base;
19587 addr_size = aidata.addr_size;
19588 }
19589
ed2dc618
SM
19590 return read_addr_index_1 (dwarf2_per_objfile, addr_index, addr_base,
19591 addr_size);
3019eac3
DE
19592}
19593
57d63ce2
DE
19594/* Given a DW_FORM_GNU_str_index, fetch the string.
19595 This is only used by the Fission support. */
3019eac3 19596
d521ce57 19597static const char *
342587c4 19598read_str_index (const struct die_reader_specs *reader, ULONGEST str_index)
3019eac3 19599{
ed2dc618 19600 struct dwarf2_cu *cu = reader->cu;
518817b3
SM
19601 struct dwarf2_per_objfile *dwarf2_per_objfile
19602 = cu->per_cu->dwarf2_per_objfile;
3019eac3 19603 struct objfile *objfile = dwarf2_per_objfile->objfile;
c5164cbc 19604 const char *objf_name = objfile_name (objfile);
3019eac3 19605 bfd *abfd = objfile->obfd;
73869dc2
DE
19606 struct dwarf2_section_info *str_section = &reader->dwo_file->sections.str;
19607 struct dwarf2_section_info *str_offsets_section =
19608 &reader->dwo_file->sections.str_offsets;
d521ce57 19609 const gdb_byte *info_ptr;
3019eac3 19610 ULONGEST str_offset;
57d63ce2 19611 static const char form_name[] = "DW_FORM_GNU_str_index";
3019eac3 19612
73869dc2
DE
19613 dwarf2_read_section (objfile, str_section);
19614 dwarf2_read_section (objfile, str_offsets_section);
19615 if (str_section->buffer == NULL)
57d63ce2 19616 error (_("%s used without .debug_str.dwo section"
9d8780f0
SM
19617 " in CU at offset %s [in module %s]"),
19618 form_name, sect_offset_str (cu->header.sect_off), objf_name);
73869dc2 19619 if (str_offsets_section->buffer == NULL)
57d63ce2 19620 error (_("%s used without .debug_str_offsets.dwo section"
9d8780f0
SM
19621 " in CU at offset %s [in module %s]"),
19622 form_name, sect_offset_str (cu->header.sect_off), objf_name);
73869dc2 19623 if (str_index * cu->header.offset_size >= str_offsets_section->size)
57d63ce2 19624 error (_("%s pointing outside of .debug_str_offsets.dwo"
9d8780f0
SM
19625 " section in CU at offset %s [in module %s]"),
19626 form_name, sect_offset_str (cu->header.sect_off), objf_name);
73869dc2 19627 info_ptr = (str_offsets_section->buffer
3019eac3
DE
19628 + str_index * cu->header.offset_size);
19629 if (cu->header.offset_size == 4)
19630 str_offset = bfd_get_32 (abfd, info_ptr);
19631 else
19632 str_offset = bfd_get_64 (abfd, info_ptr);
73869dc2 19633 if (str_offset >= str_section->size)
57d63ce2 19634 error (_("Offset from %s pointing outside of"
9d8780f0
SM
19635 " .debug_str.dwo section in CU at offset %s [in module %s]"),
19636 form_name, sect_offset_str (cu->header.sect_off), objf_name);
73869dc2 19637 return (const char *) (str_section->buffer + str_offset);
3019eac3
DE
19638}
19639
3019eac3
DE
19640/* Return the length of an LEB128 number in BUF. */
19641
19642static int
19643leb128_size (const gdb_byte *buf)
19644{
19645 const gdb_byte *begin = buf;
19646 gdb_byte byte;
19647
19648 while (1)
19649 {
19650 byte = *buf++;
19651 if ((byte & 128) == 0)
19652 return buf - begin;
19653 }
19654}
19655
c906108c 19656static void
e142c38c 19657set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
c906108c
SS
19658{
19659 switch (lang)
19660 {
19661 case DW_LANG_C89:
76bee0cc 19662 case DW_LANG_C99:
0cfd832f 19663 case DW_LANG_C11:
c906108c 19664 case DW_LANG_C:
d1be3247 19665 case DW_LANG_UPC:
e142c38c 19666 cu->language = language_c;
c906108c 19667 break;
9c37b5ae 19668 case DW_LANG_Java:
c906108c 19669 case DW_LANG_C_plus_plus:
0cfd832f
MW
19670 case DW_LANG_C_plus_plus_11:
19671 case DW_LANG_C_plus_plus_14:
e142c38c 19672 cu->language = language_cplus;
c906108c 19673 break;
6aecb9c2
JB
19674 case DW_LANG_D:
19675 cu->language = language_d;
19676 break;
c906108c
SS
19677 case DW_LANG_Fortran77:
19678 case DW_LANG_Fortran90:
b21b22e0 19679 case DW_LANG_Fortran95:
f7de9aab
MW
19680 case DW_LANG_Fortran03:
19681 case DW_LANG_Fortran08:
e142c38c 19682 cu->language = language_fortran;
c906108c 19683 break;
a766d390
DE
19684 case DW_LANG_Go:
19685 cu->language = language_go;
19686 break;
c906108c 19687 case DW_LANG_Mips_Assembler:
e142c38c 19688 cu->language = language_asm;
c906108c
SS
19689 break;
19690 case DW_LANG_Ada83:
8aaf0b47 19691 case DW_LANG_Ada95:
bc5f45f8
JB
19692 cu->language = language_ada;
19693 break;
72019c9c
GM
19694 case DW_LANG_Modula2:
19695 cu->language = language_m2;
19696 break;
fe8e67fd
PM
19697 case DW_LANG_Pascal83:
19698 cu->language = language_pascal;
19699 break;
22566fbd
DJ
19700 case DW_LANG_ObjC:
19701 cu->language = language_objc;
19702 break;
c44af4eb
TT
19703 case DW_LANG_Rust:
19704 case DW_LANG_Rust_old:
19705 cu->language = language_rust;
19706 break;
c906108c
SS
19707 case DW_LANG_Cobol74:
19708 case DW_LANG_Cobol85:
c906108c 19709 default:
e142c38c 19710 cu->language = language_minimal;
c906108c
SS
19711 break;
19712 }
e142c38c 19713 cu->language_defn = language_def (cu->language);
c906108c
SS
19714}
19715
19716/* Return the named attribute or NULL if not there. */
19717
19718static struct attribute *
e142c38c 19719dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
c906108c 19720{
a48e046c 19721 for (;;)
c906108c 19722 {
a48e046c
TT
19723 unsigned int i;
19724 struct attribute *spec = NULL;
19725
19726 for (i = 0; i < die->num_attrs; ++i)
19727 {
19728 if (die->attrs[i].name == name)
19729 return &die->attrs[i];
19730 if (die->attrs[i].name == DW_AT_specification
19731 || die->attrs[i].name == DW_AT_abstract_origin)
19732 spec = &die->attrs[i];
19733 }
19734
19735 if (!spec)
19736 break;
c906108c 19737
f2f0e013 19738 die = follow_die_ref (die, spec, &cu);
f2f0e013 19739 }
c5aa993b 19740
c906108c
SS
19741 return NULL;
19742}
19743
348e048f
DE
19744/* Return the named attribute or NULL if not there,
19745 but do not follow DW_AT_specification, etc.
19746 This is for use in contexts where we're reading .debug_types dies.
19747 Following DW_AT_specification, DW_AT_abstract_origin will take us
19748 back up the chain, and we want to go down. */
19749
19750static struct attribute *
45e58e77 19751dwarf2_attr_no_follow (struct die_info *die, unsigned int name)
348e048f
DE
19752{
19753 unsigned int i;
19754
19755 for (i = 0; i < die->num_attrs; ++i)
19756 if (die->attrs[i].name == name)
19757 return &die->attrs[i];
19758
19759 return NULL;
19760}
19761
7d45c7c3
KB
19762/* Return the string associated with a string-typed attribute, or NULL if it
19763 is either not found or is of an incorrect type. */
19764
19765static const char *
19766dwarf2_string_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
19767{
19768 struct attribute *attr;
19769 const char *str = NULL;
19770
19771 attr = dwarf2_attr (die, name, cu);
19772
19773 if (attr != NULL)
19774 {
43988095 19775 if (attr->form == DW_FORM_strp || attr->form == DW_FORM_line_strp
b3340438
L
19776 || attr->form == DW_FORM_string
19777 || attr->form == DW_FORM_GNU_str_index
16eb6b2d 19778 || attr->form == DW_FORM_GNU_strp_alt)
7d45c7c3
KB
19779 str = DW_STRING (attr);
19780 else
b98664d3 19781 complaint (_("string type expected for attribute %s for "
9d8780f0
SM
19782 "DIE at %s in module %s"),
19783 dwarf_attr_name (name), sect_offset_str (die->sect_off),
518817b3 19784 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
7d45c7c3
KB
19785 }
19786
19787 return str;
19788}
19789
05cf31d1
JB
19790/* Return non-zero iff the attribute NAME is defined for the given DIE,
19791 and holds a non-zero value. This function should only be used for
2dc7f7b3 19792 DW_FORM_flag or DW_FORM_flag_present attributes. */
05cf31d1
JB
19793
19794static int
19795dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
19796{
19797 struct attribute *attr = dwarf2_attr (die, name, cu);
19798
19799 return (attr && DW_UNSND (attr));
19800}
19801
3ca72b44 19802static int
e142c38c 19803die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
3ca72b44 19804{
05cf31d1
JB
19805 /* A DIE is a declaration if it has a DW_AT_declaration attribute
19806 which value is non-zero. However, we have to be careful with
19807 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
19808 (via dwarf2_flag_true_p) follows this attribute. So we may
19809 end up accidently finding a declaration attribute that belongs
19810 to a different DIE referenced by the specification attribute,
19811 even though the given DIE does not have a declaration attribute. */
19812 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
19813 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
3ca72b44
AC
19814}
19815
63d06c5c 19816/* Return the die giving the specification for DIE, if there is
f2f0e013 19817 one. *SPEC_CU is the CU containing DIE on input, and the CU
edb3359d
DJ
19818 containing the return value on output. If there is no
19819 specification, but there is an abstract origin, that is
19820 returned. */
63d06c5c
DC
19821
19822static struct die_info *
f2f0e013 19823die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
63d06c5c 19824{
f2f0e013
DJ
19825 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
19826 *spec_cu);
63d06c5c 19827
edb3359d
DJ
19828 if (spec_attr == NULL)
19829 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
19830
63d06c5c
DC
19831 if (spec_attr == NULL)
19832 return NULL;
19833 else
f2f0e013 19834 return follow_die_ref (die, spec_attr, spec_cu);
63d06c5c 19835}
c906108c 19836
527f3840
JK
19837/* Stub for free_line_header to match void * callback types. */
19838
19839static void
19840free_line_header_voidp (void *arg)
19841{
9a3c8263 19842 struct line_header *lh = (struct line_header *) arg;
527f3840 19843
fff8551c 19844 delete lh;
527f3840
JK
19845}
19846
fff8551c
PA
19847void
19848line_header::add_include_dir (const char *include_dir)
c906108c 19849{
27e0867f 19850 if (dwarf_line_debug >= 2)
fff8551c
PA
19851 fprintf_unfiltered (gdb_stdlog, "Adding dir %zu: %s\n",
19852 include_dirs.size () + 1, include_dir);
27e0867f 19853
fff8551c 19854 include_dirs.push_back (include_dir);
debd256d 19855}
6e70227d 19856
fff8551c
PA
19857void
19858line_header::add_file_name (const char *name,
ecfb656c 19859 dir_index d_index,
fff8551c
PA
19860 unsigned int mod_time,
19861 unsigned int length)
debd256d 19862{
27e0867f
DE
19863 if (dwarf_line_debug >= 2)
19864 fprintf_unfiltered (gdb_stdlog, "Adding file %u: %s\n",
fff8551c 19865 (unsigned) file_names.size () + 1, name);
27e0867f 19866
ecfb656c 19867 file_names.emplace_back (name, d_index, mod_time, length);
debd256d 19868}
6e70227d 19869
83769d0b 19870/* A convenience function to find the proper .debug_line section for a CU. */
36586728
TT
19871
19872static struct dwarf2_section_info *
19873get_debug_line_section (struct dwarf2_cu *cu)
19874{
19875 struct dwarf2_section_info *section;
518817b3
SM
19876 struct dwarf2_per_objfile *dwarf2_per_objfile
19877 = cu->per_cu->dwarf2_per_objfile;
36586728
TT
19878
19879 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
19880 DWO file. */
19881 if (cu->dwo_unit && cu->per_cu->is_debug_types)
19882 section = &cu->dwo_unit->dwo_file->sections.line;
19883 else if (cu->per_cu->is_dwz)
19884 {
ed2dc618 19885 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
36586728
TT
19886
19887 section = &dwz->line;
19888 }
19889 else
19890 section = &dwarf2_per_objfile->line;
19891
19892 return section;
19893}
19894
43988095
JK
19895/* Read directory or file name entry format, starting with byte of
19896 format count entries, ULEB128 pairs of entry formats, ULEB128 of
19897 entries count and the entries themselves in the described entry
19898 format. */
19899
19900static void
ed2dc618
SM
19901read_formatted_entries (struct dwarf2_per_objfile *dwarf2_per_objfile,
19902 bfd *abfd, const gdb_byte **bufp,
43988095
JK
19903 struct line_header *lh,
19904 const struct comp_unit_head *cu_header,
19905 void (*callback) (struct line_header *lh,
19906 const char *name,
ecfb656c 19907 dir_index d_index,
43988095
JK
19908 unsigned int mod_time,
19909 unsigned int length))
19910{
19911 gdb_byte format_count, formati;
19912 ULONGEST data_count, datai;
19913 const gdb_byte *buf = *bufp;
19914 const gdb_byte *format_header_data;
43988095
JK
19915 unsigned int bytes_read;
19916
19917 format_count = read_1_byte (abfd, buf);
19918 buf += 1;
19919 format_header_data = buf;
19920 for (formati = 0; formati < format_count; formati++)
19921 {
19922 read_unsigned_leb128 (abfd, buf, &bytes_read);
19923 buf += bytes_read;
19924 read_unsigned_leb128 (abfd, buf, &bytes_read);
19925 buf += bytes_read;
19926 }
19927
19928 data_count = read_unsigned_leb128 (abfd, buf, &bytes_read);
19929 buf += bytes_read;
19930 for (datai = 0; datai < data_count; datai++)
19931 {
19932 const gdb_byte *format = format_header_data;
19933 struct file_entry fe;
19934
43988095
JK
19935 for (formati = 0; formati < format_count; formati++)
19936 {
ecfb656c 19937 ULONGEST content_type = read_unsigned_leb128 (abfd, format, &bytes_read);
43988095 19938 format += bytes_read;
43988095 19939
ecfb656c 19940 ULONGEST form = read_unsigned_leb128 (abfd, format, &bytes_read);
43988095 19941 format += bytes_read;
ecfb656c
PA
19942
19943 gdb::optional<const char *> string;
19944 gdb::optional<unsigned int> uint;
19945
43988095
JK
19946 switch (form)
19947 {
19948 case DW_FORM_string:
ecfb656c 19949 string.emplace (read_direct_string (abfd, buf, &bytes_read));
43988095
JK
19950 buf += bytes_read;
19951 break;
19952
19953 case DW_FORM_line_strp:
ed2dc618
SM
19954 string.emplace (read_indirect_line_string (dwarf2_per_objfile,
19955 abfd, buf,
ecfb656c
PA
19956 cu_header,
19957 &bytes_read));
43988095
JK
19958 buf += bytes_read;
19959 break;
19960
19961 case DW_FORM_data1:
ecfb656c 19962 uint.emplace (read_1_byte (abfd, buf));
43988095
JK
19963 buf += 1;
19964 break;
19965
19966 case DW_FORM_data2:
ecfb656c 19967 uint.emplace (read_2_bytes (abfd, buf));
43988095
JK
19968 buf += 2;
19969 break;
19970
19971 case DW_FORM_data4:
ecfb656c 19972 uint.emplace (read_4_bytes (abfd, buf));
43988095
JK
19973 buf += 4;
19974 break;
19975
19976 case DW_FORM_data8:
ecfb656c 19977 uint.emplace (read_8_bytes (abfd, buf));
43988095
JK
19978 buf += 8;
19979 break;
19980
19981 case DW_FORM_udata:
ecfb656c 19982 uint.emplace (read_unsigned_leb128 (abfd, buf, &bytes_read));
43988095
JK
19983 buf += bytes_read;
19984 break;
19985
19986 case DW_FORM_block:
19987 /* It is valid only for DW_LNCT_timestamp which is ignored by
19988 current GDB. */
19989 break;
19990 }
ecfb656c
PA
19991
19992 switch (content_type)
19993 {
19994 case DW_LNCT_path:
19995 if (string.has_value ())
19996 fe.name = *string;
19997 break;
19998 case DW_LNCT_directory_index:
19999 if (uint.has_value ())
20000 fe.d_index = (dir_index) *uint;
20001 break;
20002 case DW_LNCT_timestamp:
20003 if (uint.has_value ())
20004 fe.mod_time = *uint;
20005 break;
20006 case DW_LNCT_size:
20007 if (uint.has_value ())
20008 fe.length = *uint;
20009 break;
20010 case DW_LNCT_MD5:
20011 break;
20012 default:
b98664d3 20013 complaint (_("Unknown format content type %s"),
ecfb656c
PA
20014 pulongest (content_type));
20015 }
43988095
JK
20016 }
20017
ecfb656c 20018 callback (lh, fe.name, fe.d_index, fe.mod_time, fe.length);
43988095
JK
20019 }
20020
20021 *bufp = buf;
20022}
20023
debd256d 20024/* Read the statement program header starting at OFFSET in
3019eac3 20025 .debug_line, or .debug_line.dwo. Return a pointer
6502dd73 20026 to a struct line_header, allocated using xmalloc.
cd366ee8
DE
20027 Returns NULL if there is a problem reading the header, e.g., if it
20028 has a version we don't understand.
debd256d
JB
20029
20030 NOTE: the strings in the include directory and file name tables of
3019eac3
DE
20031 the returned object point into the dwarf line section buffer,
20032 and must not be freed. */
ae2de4f8 20033
fff8551c 20034static line_header_up
9c541725 20035dwarf_decode_line_header (sect_offset sect_off, struct dwarf2_cu *cu)
debd256d 20036{
d521ce57 20037 const gdb_byte *line_ptr;
c764a876 20038 unsigned int bytes_read, offset_size;
debd256d 20039 int i;
d521ce57 20040 const char *cur_dir, *cur_file;
3019eac3
DE
20041 struct dwarf2_section_info *section;
20042 bfd *abfd;
518817b3
SM
20043 struct dwarf2_per_objfile *dwarf2_per_objfile
20044 = cu->per_cu->dwarf2_per_objfile;
3019eac3 20045
36586728 20046 section = get_debug_line_section (cu);
3019eac3
DE
20047 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
20048 if (section->buffer == NULL)
debd256d 20049 {
3019eac3 20050 if (cu->dwo_unit && cu->per_cu->is_debug_types)
b98664d3 20051 complaint (_("missing .debug_line.dwo section"));
3019eac3 20052 else
b98664d3 20053 complaint (_("missing .debug_line section"));
debd256d
JB
20054 return 0;
20055 }
20056
fceca515
DE
20057 /* We can't do this until we know the section is non-empty.
20058 Only then do we know we have such a section. */
a32a8923 20059 abfd = get_section_bfd_owner (section);
fceca515 20060
a738430d
MK
20061 /* Make sure that at least there's room for the total_length field.
20062 That could be 12 bytes long, but we're just going to fudge that. */
9c541725 20063 if (to_underlying (sect_off) + 4 >= section->size)
debd256d 20064 {
4d3c2250 20065 dwarf2_statement_list_fits_in_line_number_section_complaint ();
debd256d
JB
20066 return 0;
20067 }
20068
fff8551c 20069 line_header_up lh (new line_header ());
debd256d 20070
9c541725 20071 lh->sect_off = sect_off;
527f3840
JK
20072 lh->offset_in_dwz = cu->per_cu->is_dwz;
20073
9c541725 20074 line_ptr = section->buffer + to_underlying (sect_off);
debd256d 20075
a738430d 20076 /* Read in the header. */
6e70227d 20077 lh->total_length =
c764a876
DE
20078 read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
20079 &bytes_read, &offset_size);
debd256d 20080 line_ptr += bytes_read;
3019eac3 20081 if (line_ptr + lh->total_length > (section->buffer + section->size))
debd256d 20082 {
4d3c2250 20083 dwarf2_statement_list_fits_in_line_number_section_complaint ();
debd256d
JB
20084 return 0;
20085 }
20086 lh->statement_program_end = line_ptr + lh->total_length;
20087 lh->version = read_2_bytes (abfd, line_ptr);
20088 line_ptr += 2;
43988095 20089 if (lh->version > 5)
cd366ee8
DE
20090 {
20091 /* This is a version we don't understand. The format could have
20092 changed in ways we don't handle properly so just punt. */
b98664d3 20093 complaint (_("unsupported version in .debug_line section"));
cd366ee8
DE
20094 return NULL;
20095 }
43988095
JK
20096 if (lh->version >= 5)
20097 {
20098 gdb_byte segment_selector_size;
20099
20100 /* Skip address size. */
20101 read_1_byte (abfd, line_ptr);
20102 line_ptr += 1;
20103
20104 segment_selector_size = read_1_byte (abfd, line_ptr);
20105 line_ptr += 1;
20106 if (segment_selector_size != 0)
20107 {
b98664d3 20108 complaint (_("unsupported segment selector size %u "
43988095
JK
20109 "in .debug_line section"),
20110 segment_selector_size);
20111 return NULL;
20112 }
20113 }
c764a876
DE
20114 lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
20115 line_ptr += offset_size;
debd256d
JB
20116 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
20117 line_ptr += 1;
2dc7f7b3
TT
20118 if (lh->version >= 4)
20119 {
20120 lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
20121 line_ptr += 1;
20122 }
20123 else
20124 lh->maximum_ops_per_instruction = 1;
20125
20126 if (lh->maximum_ops_per_instruction == 0)
20127 {
20128 lh->maximum_ops_per_instruction = 1;
b98664d3 20129 complaint (_("invalid maximum_ops_per_instruction "
3e43a32a 20130 "in `.debug_line' section"));
2dc7f7b3
TT
20131 }
20132
debd256d
JB
20133 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
20134 line_ptr += 1;
20135 lh->line_base = read_1_signed_byte (abfd, line_ptr);
20136 line_ptr += 1;
20137 lh->line_range = read_1_byte (abfd, line_ptr);
20138 line_ptr += 1;
20139 lh->opcode_base = read_1_byte (abfd, line_ptr);
20140 line_ptr += 1;
fff8551c 20141 lh->standard_opcode_lengths.reset (new unsigned char[lh->opcode_base]);
debd256d
JB
20142
20143 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
20144 for (i = 1; i < lh->opcode_base; ++i)
20145 {
20146 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
20147 line_ptr += 1;
20148 }
20149
43988095 20150 if (lh->version >= 5)
debd256d 20151 {
43988095 20152 /* Read directory table. */
ed2dc618
SM
20153 read_formatted_entries (dwarf2_per_objfile, abfd, &line_ptr, lh.get (),
20154 &cu->header,
fff8551c 20155 [] (struct line_header *lh, const char *name,
ecfb656c 20156 dir_index d_index, unsigned int mod_time,
fff8551c
PA
20157 unsigned int length)
20158 {
20159 lh->add_include_dir (name);
20160 });
debd256d 20161
43988095 20162 /* Read file name table. */
ed2dc618
SM
20163 read_formatted_entries (dwarf2_per_objfile, abfd, &line_ptr, lh.get (),
20164 &cu->header,
fff8551c 20165 [] (struct line_header *lh, const char *name,
ecfb656c 20166 dir_index d_index, unsigned int mod_time,
fff8551c
PA
20167 unsigned int length)
20168 {
ecfb656c 20169 lh->add_file_name (name, d_index, mod_time, length);
fff8551c 20170 });
43988095
JK
20171 }
20172 else
debd256d 20173 {
43988095
JK
20174 /* Read directory table. */
20175 while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
20176 {
20177 line_ptr += bytes_read;
fff8551c 20178 lh->add_include_dir (cur_dir);
43988095 20179 }
debd256d
JB
20180 line_ptr += bytes_read;
20181
43988095
JK
20182 /* Read file name table. */
20183 while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
20184 {
ecfb656c
PA
20185 unsigned int mod_time, length;
20186 dir_index d_index;
43988095
JK
20187
20188 line_ptr += bytes_read;
ecfb656c 20189 d_index = (dir_index) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
43988095
JK
20190 line_ptr += bytes_read;
20191 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20192 line_ptr += bytes_read;
20193 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20194 line_ptr += bytes_read;
20195
ecfb656c 20196 lh->add_file_name (cur_file, d_index, mod_time, length);
43988095
JK
20197 }
20198 line_ptr += bytes_read;
debd256d 20199 }
6e70227d 20200 lh->statement_program_start = line_ptr;
debd256d 20201
3019eac3 20202 if (line_ptr > (section->buffer + section->size))
b98664d3 20203 complaint (_("line number info header doesn't "
3e43a32a 20204 "fit in `.debug_line' section"));
debd256d 20205
debd256d
JB
20206 return lh;
20207}
c906108c 20208
c6da4cef
DE
20209/* Subroutine of dwarf_decode_lines to simplify it.
20210 Return the file name of the psymtab for included file FILE_INDEX
20211 in line header LH of PST.
20212 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
c89b44cd
TT
20213 If space for the result is malloc'd, *NAME_HOLDER will be set.
20214 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
c6da4cef 20215
d521ce57 20216static const char *
c6da4cef
DE
20217psymtab_include_file_name (const struct line_header *lh, int file_index,
20218 const struct partial_symtab *pst,
c89b44cd
TT
20219 const char *comp_dir,
20220 gdb::unique_xmalloc_ptr<char> *name_holder)
c6da4cef 20221{
8c43009f 20222 const file_entry &fe = lh->file_names[file_index];
d521ce57
TT
20223 const char *include_name = fe.name;
20224 const char *include_name_to_compare = include_name;
72b9f47f 20225 const char *pst_filename;
c6da4cef
DE
20226 int file_is_pst;
20227
8c43009f 20228 const char *dir_name = fe.include_dir (lh);
c6da4cef 20229
c89b44cd 20230 gdb::unique_xmalloc_ptr<char> hold_compare;
c6da4cef
DE
20231 if (!IS_ABSOLUTE_PATH (include_name)
20232 && (dir_name != NULL || comp_dir != NULL))
20233 {
20234 /* Avoid creating a duplicate psymtab for PST.
20235 We do this by comparing INCLUDE_NAME and PST_FILENAME.
20236 Before we do the comparison, however, we need to account
20237 for DIR_NAME and COMP_DIR.
20238 First prepend dir_name (if non-NULL). If we still don't
20239 have an absolute path prepend comp_dir (if non-NULL).
20240 However, the directory we record in the include-file's
20241 psymtab does not contain COMP_DIR (to match the
20242 corresponding symtab(s)).
20243
20244 Example:
20245
20246 bash$ cd /tmp
20247 bash$ gcc -g ./hello.c
20248 include_name = "hello.c"
20249 dir_name = "."
20250 DW_AT_comp_dir = comp_dir = "/tmp"
5f52445b
YQ
20251 DW_AT_name = "./hello.c"
20252
20253 */
c6da4cef
DE
20254
20255 if (dir_name != NULL)
20256 {
c89b44cd
TT
20257 name_holder->reset (concat (dir_name, SLASH_STRING,
20258 include_name, (char *) NULL));
20259 include_name = name_holder->get ();
c6da4cef 20260 include_name_to_compare = include_name;
c6da4cef
DE
20261 }
20262 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
20263 {
c89b44cd
TT
20264 hold_compare.reset (concat (comp_dir, SLASH_STRING,
20265 include_name, (char *) NULL));
20266 include_name_to_compare = hold_compare.get ();
c6da4cef
DE
20267 }
20268 }
20269
20270 pst_filename = pst->filename;
c89b44cd 20271 gdb::unique_xmalloc_ptr<char> copied_name;
c6da4cef
DE
20272 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
20273 {
c89b44cd
TT
20274 copied_name.reset (concat (pst->dirname, SLASH_STRING,
20275 pst_filename, (char *) NULL));
20276 pst_filename = copied_name.get ();
c6da4cef
DE
20277 }
20278
1e3fad37 20279 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
c6da4cef 20280
c6da4cef
DE
20281 if (file_is_pst)
20282 return NULL;
20283 return include_name;
20284}
20285
d9b3de22
DE
20286/* State machine to track the state of the line number program. */
20287
6f77053d 20288class lnp_state_machine
d9b3de22 20289{
6f77053d
PA
20290public:
20291 /* Initialize a machine state for the start of a line number
20292 program. */
20293 lnp_state_machine (gdbarch *arch, line_header *lh, bool record_lines_p);
20294
8c43009f
PA
20295 file_entry *current_file ()
20296 {
20297 /* lh->file_names is 0-based, but the file name numbers in the
20298 statement program are 1-based. */
6f77053d
PA
20299 return m_line_header->file_name_at (m_file);
20300 }
20301
20302 /* Record the line in the state machine. END_SEQUENCE is true if
20303 we're processing the end of a sequence. */
20304 void record_line (bool end_sequence);
20305
7ab6656f
OJ
20306 /* Check ADDRESS is zero and less than UNRELOCATED_LOWPC and if true
20307 nop-out rest of the lines in this sequence. */
6f77053d
PA
20308 void check_line_address (struct dwarf2_cu *cu,
20309 const gdb_byte *line_ptr,
7ab6656f 20310 CORE_ADDR unrelocated_lowpc, CORE_ADDR address);
6f77053d
PA
20311
20312 void handle_set_discriminator (unsigned int discriminator)
20313 {
20314 m_discriminator = discriminator;
20315 m_line_has_non_zero_discriminator |= discriminator != 0;
20316 }
20317
20318 /* Handle DW_LNE_set_address. */
20319 void handle_set_address (CORE_ADDR baseaddr, CORE_ADDR address)
20320 {
20321 m_op_index = 0;
20322 address += baseaddr;
20323 m_address = gdbarch_adjust_dwarf2_line (m_gdbarch, address, false);
20324 }
20325
20326 /* Handle DW_LNS_advance_pc. */
20327 void handle_advance_pc (CORE_ADDR adjust);
20328
20329 /* Handle a special opcode. */
20330 void handle_special_opcode (unsigned char op_code);
20331
20332 /* Handle DW_LNS_advance_line. */
20333 void handle_advance_line (int line_delta)
20334 {
20335 advance_line (line_delta);
20336 }
20337
20338 /* Handle DW_LNS_set_file. */
20339 void handle_set_file (file_name_index file);
20340
20341 /* Handle DW_LNS_negate_stmt. */
20342 void handle_negate_stmt ()
20343 {
20344 m_is_stmt = !m_is_stmt;
20345 }
20346
20347 /* Handle DW_LNS_const_add_pc. */
20348 void handle_const_add_pc ();
20349
20350 /* Handle DW_LNS_fixed_advance_pc. */
20351 void handle_fixed_advance_pc (CORE_ADDR addr_adj)
20352 {
20353 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20354 m_op_index = 0;
20355 }
20356
20357 /* Handle DW_LNS_copy. */
20358 void handle_copy ()
20359 {
20360 record_line (false);
20361 m_discriminator = 0;
20362 }
20363
20364 /* Handle DW_LNE_end_sequence. */
20365 void handle_end_sequence ()
20366 {
20367 m_record_line_callback = ::record_line;
20368 }
20369
20370private:
20371 /* Advance the line by LINE_DELTA. */
20372 void advance_line (int line_delta)
20373 {
20374 m_line += line_delta;
20375
20376 if (line_delta != 0)
20377 m_line_has_non_zero_discriminator = m_discriminator != 0;
8c43009f
PA
20378 }
20379
6f77053d
PA
20380 gdbarch *m_gdbarch;
20381
20382 /* True if we're recording lines.
20383 Otherwise we're building partial symtabs and are just interested in
20384 finding include files mentioned by the line number program. */
20385 bool m_record_lines_p;
20386
8c43009f 20387 /* The line number header. */
6f77053d 20388 line_header *m_line_header;
8c43009f 20389
6f77053d
PA
20390 /* These are part of the standard DWARF line number state machine,
20391 and initialized according to the DWARF spec. */
d9b3de22 20392
6f77053d 20393 unsigned char m_op_index = 0;
8c43009f 20394 /* The line table index (1-based) of the current file. */
6f77053d
PA
20395 file_name_index m_file = (file_name_index) 1;
20396 unsigned int m_line = 1;
20397
20398 /* These are initialized in the constructor. */
20399
20400 CORE_ADDR m_address;
20401 bool m_is_stmt;
20402 unsigned int m_discriminator;
d9b3de22
DE
20403
20404 /* Additional bits of state we need to track. */
20405
20406 /* The last file that we called dwarf2_start_subfile for.
20407 This is only used for TLLs. */
6f77053d 20408 unsigned int m_last_file = 0;
d9b3de22 20409 /* The last file a line number was recorded for. */
6f77053d 20410 struct subfile *m_last_subfile = NULL;
d9b3de22
DE
20411
20412 /* The function to call to record a line. */
6f77053d 20413 record_line_ftype *m_record_line_callback = NULL;
d9b3de22
DE
20414
20415 /* The last line number that was recorded, used to coalesce
20416 consecutive entries for the same line. This can happen, for
20417 example, when discriminators are present. PR 17276. */
6f77053d
PA
20418 unsigned int m_last_line = 0;
20419 bool m_line_has_non_zero_discriminator = false;
8c43009f 20420};
d9b3de22 20421
6f77053d
PA
20422void
20423lnp_state_machine::handle_advance_pc (CORE_ADDR adjust)
20424{
20425 CORE_ADDR addr_adj = (((m_op_index + adjust)
20426 / m_line_header->maximum_ops_per_instruction)
20427 * m_line_header->minimum_instruction_length);
20428 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20429 m_op_index = ((m_op_index + adjust)
20430 % m_line_header->maximum_ops_per_instruction);
20431}
d9b3de22 20432
6f77053d
PA
20433void
20434lnp_state_machine::handle_special_opcode (unsigned char op_code)
d9b3de22 20435{
6f77053d
PA
20436 unsigned char adj_opcode = op_code - m_line_header->opcode_base;
20437 CORE_ADDR addr_adj = (((m_op_index
20438 + (adj_opcode / m_line_header->line_range))
20439 / m_line_header->maximum_ops_per_instruction)
20440 * m_line_header->minimum_instruction_length);
20441 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20442 m_op_index = ((m_op_index + (adj_opcode / m_line_header->line_range))
20443 % m_line_header->maximum_ops_per_instruction);
d9b3de22 20444
6f77053d
PA
20445 int line_delta = (m_line_header->line_base
20446 + (adj_opcode % m_line_header->line_range));
20447 advance_line (line_delta);
20448 record_line (false);
20449 m_discriminator = 0;
20450}
d9b3de22 20451
6f77053d
PA
20452void
20453lnp_state_machine::handle_set_file (file_name_index file)
20454{
20455 m_file = file;
20456
20457 const file_entry *fe = current_file ();
20458 if (fe == NULL)
20459 dwarf2_debug_line_missing_file_complaint ();
20460 else if (m_record_lines_p)
20461 {
20462 const char *dir = fe->include_dir (m_line_header);
20463
3c65e5b3 20464 m_last_subfile = get_current_subfile ();
6f77053d
PA
20465 m_line_has_non_zero_discriminator = m_discriminator != 0;
20466 dwarf2_start_subfile (fe->name, dir);
20467 }
20468}
20469
20470void
20471lnp_state_machine::handle_const_add_pc ()
20472{
20473 CORE_ADDR adjust
20474 = (255 - m_line_header->opcode_base) / m_line_header->line_range;
20475
20476 CORE_ADDR addr_adj
20477 = (((m_op_index + adjust)
20478 / m_line_header->maximum_ops_per_instruction)
20479 * m_line_header->minimum_instruction_length);
20480
20481 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20482 m_op_index = ((m_op_index + adjust)
20483 % m_line_header->maximum_ops_per_instruction);
20484}
d9b3de22 20485
c91513d8
PP
20486/* Ignore this record_line request. */
20487
20488static void
20489noop_record_line (struct subfile *subfile, int line, CORE_ADDR pc)
20490{
20491 return;
20492}
20493
a05a36a5
DE
20494/* Return non-zero if we should add LINE to the line number table.
20495 LINE is the line to add, LAST_LINE is the last line that was added,
20496 LAST_SUBFILE is the subfile for LAST_LINE.
20497 LINE_HAS_NON_ZERO_DISCRIMINATOR is non-zero if LINE has ever
20498 had a non-zero discriminator.
20499
20500 We have to be careful in the presence of discriminators.
20501 E.g., for this line:
20502
20503 for (i = 0; i < 100000; i++);
20504
20505 clang can emit four line number entries for that one line,
20506 each with a different discriminator.
20507 See gdb.dwarf2/dw2-single-line-discriminators.exp for an example.
20508
20509 However, we want gdb to coalesce all four entries into one.
20510 Otherwise the user could stepi into the middle of the line and
20511 gdb would get confused about whether the pc really was in the
20512 middle of the line.
20513
20514 Things are further complicated by the fact that two consecutive
20515 line number entries for the same line is a heuristic used by gcc
20516 to denote the end of the prologue. So we can't just discard duplicate
20517 entries, we have to be selective about it. The heuristic we use is
20518 that we only collapse consecutive entries for the same line if at least
20519 one of those entries has a non-zero discriminator. PR 17276.
20520
20521 Note: Addresses in the line number state machine can never go backwards
20522 within one sequence, thus this coalescing is ok. */
20523
20524static int
20525dwarf_record_line_p (unsigned int line, unsigned int last_line,
20526 int line_has_non_zero_discriminator,
20527 struct subfile *last_subfile)
20528{
3c65e5b3 20529 if (get_current_subfile () != last_subfile)
a05a36a5
DE
20530 return 1;
20531 if (line != last_line)
20532 return 1;
20533 /* Same line for the same file that we've seen already.
20534 As a last check, for pr 17276, only record the line if the line
20535 has never had a non-zero discriminator. */
20536 if (!line_has_non_zero_discriminator)
20537 return 1;
20538 return 0;
20539}
20540
252a6764
DE
20541/* Use P_RECORD_LINE to record line number LINE beginning at address ADDRESS
20542 in the line table of subfile SUBFILE. */
20543
20544static void
d9b3de22
DE
20545dwarf_record_line_1 (struct gdbarch *gdbarch, struct subfile *subfile,
20546 unsigned int line, CORE_ADDR address,
20547 record_line_ftype p_record_line)
252a6764
DE
20548{
20549 CORE_ADDR addr = gdbarch_addr_bits_remove (gdbarch, address);
20550
27e0867f
DE
20551 if (dwarf_line_debug)
20552 {
20553 fprintf_unfiltered (gdb_stdlog,
20554 "Recording line %u, file %s, address %s\n",
20555 line, lbasename (subfile->name),
20556 paddress (gdbarch, address));
20557 }
20558
d5962de5 20559 (*p_record_line) (subfile, line, addr);
252a6764
DE
20560}
20561
20562/* Subroutine of dwarf_decode_lines_1 to simplify it.
20563 Mark the end of a set of line number records.
d9b3de22 20564 The arguments are the same as for dwarf_record_line_1.
252a6764
DE
20565 If SUBFILE is NULL the request is ignored. */
20566
20567static void
20568dwarf_finish_line (struct gdbarch *gdbarch, struct subfile *subfile,
20569 CORE_ADDR address, record_line_ftype p_record_line)
20570{
27e0867f
DE
20571 if (subfile == NULL)
20572 return;
20573
20574 if (dwarf_line_debug)
20575 {
20576 fprintf_unfiltered (gdb_stdlog,
20577 "Finishing current line, file %s, address %s\n",
20578 lbasename (subfile->name),
20579 paddress (gdbarch, address));
20580 }
20581
d9b3de22
DE
20582 dwarf_record_line_1 (gdbarch, subfile, 0, address, p_record_line);
20583}
20584
6f77053d
PA
20585void
20586lnp_state_machine::record_line (bool end_sequence)
d9b3de22 20587{
d9b3de22
DE
20588 if (dwarf_line_debug)
20589 {
20590 fprintf_unfiltered (gdb_stdlog,
20591 "Processing actual line %u: file %u,"
20592 " address %s, is_stmt %u, discrim %u\n",
6f77053d
PA
20593 m_line, to_underlying (m_file),
20594 paddress (m_gdbarch, m_address),
20595 m_is_stmt, m_discriminator);
d9b3de22
DE
20596 }
20597
6f77053d 20598 file_entry *fe = current_file ();
8c43009f
PA
20599
20600 if (fe == NULL)
d9b3de22
DE
20601 dwarf2_debug_line_missing_file_complaint ();
20602 /* For now we ignore lines not starting on an instruction boundary.
20603 But not when processing end_sequence for compatibility with the
20604 previous version of the code. */
6f77053d 20605 else if (m_op_index == 0 || end_sequence)
d9b3de22 20606 {
8c43009f 20607 fe->included_p = 1;
6f77053d 20608 if (m_record_lines_p && m_is_stmt)
d9b3de22 20609 {
3c65e5b3 20610 if (m_last_subfile != get_current_subfile () || end_sequence)
d9b3de22 20611 {
6f77053d
PA
20612 dwarf_finish_line (m_gdbarch, m_last_subfile,
20613 m_address, m_record_line_callback);
d9b3de22
DE
20614 }
20615
20616 if (!end_sequence)
20617 {
6f77053d
PA
20618 if (dwarf_record_line_p (m_line, m_last_line,
20619 m_line_has_non_zero_discriminator,
20620 m_last_subfile))
d9b3de22 20621 {
3c65e5b3 20622 dwarf_record_line_1 (m_gdbarch, get_current_subfile (),
6f77053d
PA
20623 m_line, m_address,
20624 m_record_line_callback);
d9b3de22 20625 }
3c65e5b3 20626 m_last_subfile = get_current_subfile ();
6f77053d 20627 m_last_line = m_line;
d9b3de22
DE
20628 }
20629 }
20630 }
20631}
20632
6f77053d
PA
20633lnp_state_machine::lnp_state_machine (gdbarch *arch, line_header *lh,
20634 bool record_lines_p)
d9b3de22 20635{
6f77053d
PA
20636 m_gdbarch = arch;
20637 m_record_lines_p = record_lines_p;
20638 m_line_header = lh;
d9b3de22 20639
6f77053d 20640 m_record_line_callback = ::record_line;
d9b3de22 20641
d9b3de22
DE
20642 /* Call `gdbarch_adjust_dwarf2_line' on the initial 0 address as if there
20643 was a line entry for it so that the backend has a chance to adjust it
20644 and also record it in case it needs it. This is currently used by MIPS
20645 code, cf. `mips_adjust_dwarf2_line'. */
6f77053d
PA
20646 m_address = gdbarch_adjust_dwarf2_line (arch, 0, 0);
20647 m_is_stmt = lh->default_is_stmt;
20648 m_discriminator = 0;
252a6764
DE
20649}
20650
6f77053d
PA
20651void
20652lnp_state_machine::check_line_address (struct dwarf2_cu *cu,
20653 const gdb_byte *line_ptr,
7ab6656f 20654 CORE_ADDR unrelocated_lowpc, CORE_ADDR address)
924c2928 20655{
7ab6656f
OJ
20656 /* If ADDRESS < UNRELOCATED_LOWPC then it's not a usable value, it's outside
20657 the pc range of the CU. However, we restrict the test to only ADDRESS
20658 values of zero to preserve GDB's previous behaviour which is to handle
20659 the specific case of a function being GC'd by the linker. */
924c2928 20660
7ab6656f 20661 if (address == 0 && address < unrelocated_lowpc)
924c2928
DE
20662 {
20663 /* This line table is for a function which has been
20664 GCd by the linker. Ignore it. PR gdb/12528 */
20665
518817b3 20666 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
924c2928
DE
20667 long line_offset = line_ptr - get_debug_line_section (cu)->buffer;
20668
b98664d3 20669 complaint (_(".debug_line address at offset 0x%lx is 0 [in module %s]"),
924c2928 20670 line_offset, objfile_name (objfile));
6f77053d
PA
20671 m_record_line_callback = noop_record_line;
20672 /* Note: record_line_callback is left as noop_record_line until
20673 we see DW_LNE_end_sequence. */
924c2928
DE
20674 }
20675}
20676
f3f5162e 20677/* Subroutine of dwarf_decode_lines to simplify it.
d9b3de22
DE
20678 Process the line number information in LH.
20679 If DECODE_FOR_PST_P is non-zero, all we do is process the line number
20680 program in order to set included_p for every referenced header. */
debd256d 20681
c906108c 20682static void
43f3e411
DE
20683dwarf_decode_lines_1 (struct line_header *lh, struct dwarf2_cu *cu,
20684 const int decode_for_pst_p, CORE_ADDR lowpc)
c906108c 20685{
d521ce57
TT
20686 const gdb_byte *line_ptr, *extended_end;
20687 const gdb_byte *line_end;
a8c50c1f 20688 unsigned int bytes_read, extended_len;
699ca60a 20689 unsigned char op_code, extended_op;
e142c38c 20690 CORE_ADDR baseaddr;
518817b3 20691 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
f3f5162e 20692 bfd *abfd = objfile->obfd;
fbf65064 20693 struct gdbarch *gdbarch = get_objfile_arch (objfile);
6f77053d
PA
20694 /* True if we're recording line info (as opposed to building partial
20695 symtabs and just interested in finding include files mentioned by
20696 the line number program). */
20697 bool record_lines_p = !decode_for_pst_p;
e142c38c
DJ
20698
20699 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 20700
debd256d
JB
20701 line_ptr = lh->statement_program_start;
20702 line_end = lh->statement_program_end;
c906108c
SS
20703
20704 /* Read the statement sequences until there's nothing left. */
20705 while (line_ptr < line_end)
20706 {
6f77053d
PA
20707 /* The DWARF line number program state machine. Reset the state
20708 machine at the start of each sequence. */
20709 lnp_state_machine state_machine (gdbarch, lh, record_lines_p);
20710 bool end_sequence = false;
d9b3de22 20711
8c43009f 20712 if (record_lines_p)
c906108c 20713 {
8c43009f
PA
20714 /* Start a subfile for the current file of the state
20715 machine. */
20716 const file_entry *fe = state_machine.current_file ();
20717
20718 if (fe != NULL)
20719 dwarf2_start_subfile (fe->name, fe->include_dir (lh));
c906108c
SS
20720 }
20721
a738430d 20722 /* Decode the table. */
d9b3de22 20723 while (line_ptr < line_end && !end_sequence)
c906108c
SS
20724 {
20725 op_code = read_1_byte (abfd, line_ptr);
20726 line_ptr += 1;
9aa1fe7e 20727
debd256d 20728 if (op_code >= lh->opcode_base)
6e70227d 20729 {
8e07a239 20730 /* Special opcode. */
6f77053d 20731 state_machine.handle_special_opcode (op_code);
9aa1fe7e
GK
20732 }
20733 else switch (op_code)
c906108c
SS
20734 {
20735 case DW_LNS_extended_op:
3e43a32a
MS
20736 extended_len = read_unsigned_leb128 (abfd, line_ptr,
20737 &bytes_read);
473b7be6 20738 line_ptr += bytes_read;
a8c50c1f 20739 extended_end = line_ptr + extended_len;
c906108c
SS
20740 extended_op = read_1_byte (abfd, line_ptr);
20741 line_ptr += 1;
20742 switch (extended_op)
20743 {
20744 case DW_LNE_end_sequence:
6f77053d
PA
20745 state_machine.handle_end_sequence ();
20746 end_sequence = true;
c906108c
SS
20747 break;
20748 case DW_LNE_set_address:
d9b3de22
DE
20749 {
20750 CORE_ADDR address
20751 = read_address (abfd, line_ptr, cu, &bytes_read);
d9b3de22 20752 line_ptr += bytes_read;
6f77053d
PA
20753
20754 state_machine.check_line_address (cu, line_ptr,
7ab6656f 20755 lowpc - baseaddr, address);
6f77053d 20756 state_machine.handle_set_address (baseaddr, address);
d9b3de22 20757 }
c906108c
SS
20758 break;
20759 case DW_LNE_define_file:
debd256d 20760 {
d521ce57 20761 const char *cur_file;
ecfb656c
PA
20762 unsigned int mod_time, length;
20763 dir_index dindex;
6e70227d 20764
3e43a32a
MS
20765 cur_file = read_direct_string (abfd, line_ptr,
20766 &bytes_read);
debd256d 20767 line_ptr += bytes_read;
ecfb656c 20768 dindex = (dir_index)
debd256d
JB
20769 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20770 line_ptr += bytes_read;
20771 mod_time =
20772 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20773 line_ptr += bytes_read;
20774 length =
20775 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20776 line_ptr += bytes_read;
ecfb656c 20777 lh->add_file_name (cur_file, dindex, mod_time, length);
debd256d 20778 }
c906108c 20779 break;
d0c6ba3d 20780 case DW_LNE_set_discriminator:
6f77053d
PA
20781 {
20782 /* The discriminator is not interesting to the
20783 debugger; just ignore it. We still need to
20784 check its value though:
20785 if there are consecutive entries for the same
20786 (non-prologue) line we want to coalesce them.
20787 PR 17276. */
20788 unsigned int discr
20789 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20790 line_ptr += bytes_read;
20791
20792 state_machine.handle_set_discriminator (discr);
20793 }
d0c6ba3d 20794 break;
c906108c 20795 default:
b98664d3 20796 complaint (_("mangled .debug_line section"));
debd256d 20797 return;
c906108c 20798 }
a8c50c1f
DJ
20799 /* Make sure that we parsed the extended op correctly. If e.g.
20800 we expected a different address size than the producer used,
20801 we may have read the wrong number of bytes. */
20802 if (line_ptr != extended_end)
20803 {
b98664d3 20804 complaint (_("mangled .debug_line section"));
a8c50c1f
DJ
20805 return;
20806 }
c906108c
SS
20807 break;
20808 case DW_LNS_copy:
6f77053d 20809 state_machine.handle_copy ();
c906108c
SS
20810 break;
20811 case DW_LNS_advance_pc:
2dc7f7b3
TT
20812 {
20813 CORE_ADDR adjust
20814 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
2dc7f7b3 20815 line_ptr += bytes_read;
6f77053d
PA
20816
20817 state_machine.handle_advance_pc (adjust);
2dc7f7b3 20818 }
c906108c
SS
20819 break;
20820 case DW_LNS_advance_line:
a05a36a5
DE
20821 {
20822 int line_delta
20823 = read_signed_leb128 (abfd, line_ptr, &bytes_read);
a05a36a5 20824 line_ptr += bytes_read;
6f77053d
PA
20825
20826 state_machine.handle_advance_line (line_delta);
a05a36a5 20827 }
c906108c
SS
20828 break;
20829 case DW_LNS_set_file:
d9b3de22 20830 {
6f77053d 20831 file_name_index file
ecfb656c
PA
20832 = (file_name_index) read_unsigned_leb128 (abfd, line_ptr,
20833 &bytes_read);
d9b3de22 20834 line_ptr += bytes_read;
8c43009f 20835
6f77053d 20836 state_machine.handle_set_file (file);
d9b3de22 20837 }
c906108c
SS
20838 break;
20839 case DW_LNS_set_column:
0ad93d4f 20840 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
c906108c
SS
20841 line_ptr += bytes_read;
20842 break;
20843 case DW_LNS_negate_stmt:
6f77053d 20844 state_machine.handle_negate_stmt ();
c906108c
SS
20845 break;
20846 case DW_LNS_set_basic_block:
c906108c 20847 break;
c2c6d25f
JM
20848 /* Add to the address register of the state machine the
20849 address increment value corresponding to special opcode
a738430d
MK
20850 255. I.e., this value is scaled by the minimum
20851 instruction length since special opcode 255 would have
b021a221 20852 scaled the increment. */
c906108c 20853 case DW_LNS_const_add_pc:
6f77053d 20854 state_machine.handle_const_add_pc ();
c906108c
SS
20855 break;
20856 case DW_LNS_fixed_advance_pc:
3e29f34a 20857 {
6f77053d 20858 CORE_ADDR addr_adj = read_2_bytes (abfd, line_ptr);
3e29f34a 20859 line_ptr += 2;
6f77053d
PA
20860
20861 state_machine.handle_fixed_advance_pc (addr_adj);
3e29f34a 20862 }
c906108c 20863 break;
9aa1fe7e 20864 default:
a738430d
MK
20865 {
20866 /* Unknown standard opcode, ignore it. */
9aa1fe7e 20867 int i;
a738430d 20868
debd256d 20869 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
9aa1fe7e
GK
20870 {
20871 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20872 line_ptr += bytes_read;
20873 }
20874 }
c906108c
SS
20875 }
20876 }
d9b3de22
DE
20877
20878 if (!end_sequence)
20879 dwarf2_debug_line_missing_end_sequence_complaint ();
20880
20881 /* We got a DW_LNE_end_sequence (or we ran off the end of the buffer,
20882 in which case we still finish recording the last line). */
6f77053d 20883 state_machine.record_line (true);
c906108c 20884 }
f3f5162e
DE
20885}
20886
20887/* Decode the Line Number Program (LNP) for the given line_header
20888 structure and CU. The actual information extracted and the type
20889 of structures created from the LNP depends on the value of PST.
20890
20891 1. If PST is NULL, then this procedure uses the data from the program
20892 to create all necessary symbol tables, and their linetables.
20893
20894 2. If PST is not NULL, this procedure reads the program to determine
20895 the list of files included by the unit represented by PST, and
20896 builds all the associated partial symbol tables.
20897
20898 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
20899 It is used for relative paths in the line table.
20900 NOTE: When processing partial symtabs (pst != NULL),
20901 comp_dir == pst->dirname.
20902
20903 NOTE: It is important that psymtabs have the same file name (via strcmp)
20904 as the corresponding symtab. Since COMP_DIR is not used in the name of the
20905 symtab we don't use it in the name of the psymtabs we create.
20906 E.g. expand_line_sal requires this when finding psymtabs to expand.
c3b7b696
YQ
20907 A good testcase for this is mb-inline.exp.
20908
527f3840
JK
20909 LOWPC is the lowest address in CU (or 0 if not known).
20910
20911 Boolean DECODE_MAPPING specifies we need to fully decode .debug_line
20912 for its PC<->lines mapping information. Otherwise only the filename
20913 table is read in. */
f3f5162e
DE
20914
20915static void
20916dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
c3b7b696 20917 struct dwarf2_cu *cu, struct partial_symtab *pst,
527f3840 20918 CORE_ADDR lowpc, int decode_mapping)
f3f5162e 20919{
518817b3 20920 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
f3f5162e 20921 const int decode_for_pst_p = (pst != NULL);
f3f5162e 20922
527f3840
JK
20923 if (decode_mapping)
20924 dwarf_decode_lines_1 (lh, cu, decode_for_pst_p, lowpc);
aaa75496
JB
20925
20926 if (decode_for_pst_p)
20927 {
20928 int file_index;
20929
20930 /* Now that we're done scanning the Line Header Program, we can
20931 create the psymtab of each included file. */
fff8551c 20932 for (file_index = 0; file_index < lh->file_names.size (); file_index++)
aaa75496
JB
20933 if (lh->file_names[file_index].included_p == 1)
20934 {
c89b44cd 20935 gdb::unique_xmalloc_ptr<char> name_holder;
d521ce57 20936 const char *include_name =
c89b44cd
TT
20937 psymtab_include_file_name (lh, file_index, pst, comp_dir,
20938 &name_holder);
c6da4cef 20939 if (include_name != NULL)
aaa75496
JB
20940 dwarf2_create_include_psymtab (include_name, pst, objfile);
20941 }
20942 }
cb1df416
DJ
20943 else
20944 {
20945 /* Make sure a symtab is created for every file, even files
20946 which contain only variables (i.e. no code with associated
20947 line numbers). */
43f3e411 20948 struct compunit_symtab *cust = buildsym_compunit_symtab ();
cb1df416 20949 int i;
cb1df416 20950
fff8551c 20951 for (i = 0; i < lh->file_names.size (); i++)
cb1df416 20952 {
8c43009f 20953 file_entry &fe = lh->file_names[i];
9a619af0 20954
8c43009f 20955 dwarf2_start_subfile (fe.name, fe.include_dir (lh));
cb1df416 20956
3c65e5b3 20957 if (get_current_subfile ()->symtab == NULL)
43f3e411 20958 {
3c65e5b3
TT
20959 get_current_subfile ()->symtab
20960 = allocate_symtab (cust, get_current_subfile ()->name);
43f3e411 20961 }
3c65e5b3 20962 fe.symtab = get_current_subfile ()->symtab;
cb1df416
DJ
20963 }
20964 }
c906108c
SS
20965}
20966
20967/* Start a subfile for DWARF. FILENAME is the name of the file and
20968 DIRNAME the name of the source directory which contains FILENAME
4d663531 20969 or NULL if not known.
c906108c
SS
20970 This routine tries to keep line numbers from identical absolute and
20971 relative file names in a common subfile.
20972
20973 Using the `list' example from the GDB testsuite, which resides in
20974 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
20975 of /srcdir/list0.c yields the following debugging information for list0.c:
20976
c5aa993b 20977 DW_AT_name: /srcdir/list0.c
4d663531 20978 DW_AT_comp_dir: /compdir
357e46e7 20979 files.files[0].name: list0.h
c5aa993b 20980 files.files[0].dir: /srcdir
357e46e7 20981 files.files[1].name: list0.c
c5aa993b 20982 files.files[1].dir: /srcdir
c906108c
SS
20983
20984 The line number information for list0.c has to end up in a single
4f1520fb
FR
20985 subfile, so that `break /srcdir/list0.c:1' works as expected.
20986 start_subfile will ensure that this happens provided that we pass the
20987 concatenation of files.files[1].dir and files.files[1].name as the
20988 subfile's name. */
c906108c
SS
20989
20990static void
4d663531 20991dwarf2_start_subfile (const char *filename, const char *dirname)
c906108c 20992{
d521ce57 20993 char *copy = NULL;
4f1520fb 20994
4d663531 20995 /* In order not to lose the line information directory,
4f1520fb
FR
20996 we concatenate it to the filename when it makes sense.
20997 Note that the Dwarf3 standard says (speaking of filenames in line
20998 information): ``The directory index is ignored for file names
20999 that represent full path names''. Thus ignoring dirname in the
21000 `else' branch below isn't an issue. */
c906108c 21001
d5166ae1 21002 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
d521ce57
TT
21003 {
21004 copy = concat (dirname, SLASH_STRING, filename, (char *)NULL);
21005 filename = copy;
21006 }
c906108c 21007
4d663531 21008 start_subfile (filename);
4f1520fb 21009
d521ce57
TT
21010 if (copy != NULL)
21011 xfree (copy);
c906108c
SS
21012}
21013
f4dc4d17
DE
21014/* Start a symtab for DWARF.
21015 NAME, COMP_DIR, LOW_PC are passed to start_symtab. */
21016
43f3e411 21017static struct compunit_symtab *
f4dc4d17 21018dwarf2_start_symtab (struct dwarf2_cu *cu,
15d034d0 21019 const char *name, const char *comp_dir, CORE_ADDR low_pc)
f4dc4d17 21020{
43f3e411 21021 struct compunit_symtab *cust
518817b3
SM
21022 = start_symtab (cu->per_cu->dwarf2_per_objfile->objfile, name, comp_dir,
21023 low_pc, cu->language);
43f3e411 21024
f4dc4d17
DE
21025 record_debugformat ("DWARF 2");
21026 record_producer (cu->producer);
21027
4d4ec4e5 21028 cu->processing_has_namespace_info = 0;
43f3e411
DE
21029
21030 return cust;
f4dc4d17
DE
21031}
21032
4c2df51b
DJ
21033static void
21034var_decode_location (struct attribute *attr, struct symbol *sym,
e7c27a73 21035 struct dwarf2_cu *cu)
4c2df51b 21036{
518817b3 21037 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
e7c27a73
DJ
21038 struct comp_unit_head *cu_header = &cu->header;
21039
4c2df51b
DJ
21040 /* NOTE drow/2003-01-30: There used to be a comment and some special
21041 code here to turn a symbol with DW_AT_external and a
21042 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
21043 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
21044 with some versions of binutils) where shared libraries could have
21045 relocations against symbols in their debug information - the
21046 minimal symbol would have the right address, but the debug info
21047 would not. It's no longer necessary, because we will explicitly
21048 apply relocations when we read in the debug information now. */
21049
21050 /* A DW_AT_location attribute with no contents indicates that a
21051 variable has been optimized away. */
21052 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
21053 {
f1e6e072 21054 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
4c2df51b
DJ
21055 return;
21056 }
21057
21058 /* Handle one degenerate form of location expression specially, to
21059 preserve GDB's previous behavior when section offsets are
3019eac3
DE
21060 specified. If this is just a DW_OP_addr or DW_OP_GNU_addr_index
21061 then mark this symbol as LOC_STATIC. */
4c2df51b
DJ
21062
21063 if (attr_form_is_block (attr)
3019eac3
DE
21064 && ((DW_BLOCK (attr)->data[0] == DW_OP_addr
21065 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size)
21066 || (DW_BLOCK (attr)->data[0] == DW_OP_GNU_addr_index
21067 && (DW_BLOCK (attr)->size
21068 == 1 + leb128_size (&DW_BLOCK (attr)->data[1])))))
4c2df51b 21069 {
891d2f0b 21070 unsigned int dummy;
4c2df51b 21071
3019eac3
DE
21072 if (DW_BLOCK (attr)->data[0] == DW_OP_addr)
21073 SYMBOL_VALUE_ADDRESS (sym) =
21074 read_address (objfile->obfd, DW_BLOCK (attr)->data + 1, cu, &dummy);
21075 else
21076 SYMBOL_VALUE_ADDRESS (sym) =
21077 read_addr_index_from_leb128 (cu, DW_BLOCK (attr)->data + 1, &dummy);
f1e6e072 21078 SYMBOL_ACLASS_INDEX (sym) = LOC_STATIC;
4c2df51b
DJ
21079 fixup_symbol_section (sym, objfile);
21080 SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (objfile->section_offsets,
21081 SYMBOL_SECTION (sym));
4c2df51b
DJ
21082 return;
21083 }
21084
21085 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
21086 expression evaluator, and use LOC_COMPUTED only when necessary
21087 (i.e. when the value of a register or memory location is
21088 referenced, or a thread-local block, etc.). Then again, it might
21089 not be worthwhile. I'm assuming that it isn't unless performance
21090 or memory numbers show me otherwise. */
21091
f1e6e072 21092 dwarf2_symbol_mark_computed (attr, sym, cu, 0);
8be455d7 21093
f1e6e072 21094 if (SYMBOL_COMPUTED_OPS (sym)->location_has_loclist)
8be455d7 21095 cu->has_loclist = 1;
4c2df51b
DJ
21096}
21097
c906108c
SS
21098/* Given a pointer to a DWARF information entry, figure out if we need
21099 to make a symbol table entry for it, and if so, create a new entry
21100 and return a pointer to it.
21101 If TYPE is NULL, determine symbol type from the die, otherwise
34eaf542
TT
21102 used the passed type.
21103 If SPACE is not NULL, use it to hold the new symbol. If it is
21104 NULL, allocate a new symbol on the objfile's obstack. */
c906108c
SS
21105
21106static struct symbol *
5e2db402
TT
21107new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
21108 struct symbol *space)
c906108c 21109{
518817b3
SM
21110 struct dwarf2_per_objfile *dwarf2_per_objfile
21111 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 21112 struct objfile *objfile = dwarf2_per_objfile->objfile;
3e29f34a 21113 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c 21114 struct symbol *sym = NULL;
15d034d0 21115 const char *name;
c906108c
SS
21116 struct attribute *attr = NULL;
21117 struct attribute *attr2 = NULL;
e142c38c 21118 CORE_ADDR baseaddr;
e37fd15a
SW
21119 struct pending **list_to_add = NULL;
21120
edb3359d 21121 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
e142c38c
DJ
21122
21123 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 21124
94af9270 21125 name = dwarf2_name (die, cu);
c906108c
SS
21126 if (name)
21127 {
94af9270 21128 const char *linkagename;
34eaf542 21129 int suppress_add = 0;
94af9270 21130
34eaf542
TT
21131 if (space)
21132 sym = space;
21133 else
e623cf5d 21134 sym = allocate_symbol (objfile);
c906108c 21135 OBJSTAT (objfile, n_syms++);
2de7ced7
DJ
21136
21137 /* Cache this symbol's name and the name's demangled form (if any). */
f85f34ed 21138 SYMBOL_SET_LANGUAGE (sym, cu->language, &objfile->objfile_obstack);
94af9270
KS
21139 linkagename = dwarf2_physname (name, die, cu);
21140 SYMBOL_SET_NAMES (sym, linkagename, strlen (linkagename), 0, objfile);
c906108c 21141
f55ee35c
JK
21142 /* Fortran does not have mangling standard and the mangling does differ
21143 between gfortran, iFort etc. */
21144 if (cu->language == language_fortran
b250c185 21145 && symbol_get_demangled_name (&(sym->ginfo)) == NULL)
29df156d 21146 symbol_set_demangled_name (&(sym->ginfo),
cfc594ee 21147 dwarf2_full_name (name, die, cu),
29df156d 21148 NULL);
f55ee35c 21149
c906108c 21150 /* Default assumptions.
c5aa993b 21151 Use the passed type or decode it from the die. */
176620f1 21152 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
f1e6e072 21153 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
c906108c
SS
21154 if (type != NULL)
21155 SYMBOL_TYPE (sym) = type;
21156 else
e7c27a73 21157 SYMBOL_TYPE (sym) = die_type (die, cu);
edb3359d
DJ
21158 attr = dwarf2_attr (die,
21159 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
21160 cu);
c906108c
SS
21161 if (attr)
21162 {
21163 SYMBOL_LINE (sym) = DW_UNSND (attr);
21164 }
cb1df416 21165
edb3359d
DJ
21166 attr = dwarf2_attr (die,
21167 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
21168 cu);
cb1df416
DJ
21169 if (attr)
21170 {
ecfb656c 21171 file_name_index file_index = (file_name_index) DW_UNSND (attr);
8c43009f 21172 struct file_entry *fe;
9a619af0 21173
ecfb656c
PA
21174 if (cu->line_header != NULL)
21175 fe = cu->line_header->file_name_at (file_index);
8c43009f
PA
21176 else
21177 fe = NULL;
21178
21179 if (fe == NULL)
b98664d3 21180 complaint (_("file index out of range"));
8c43009f
PA
21181 else
21182 symbol_set_symtab (sym, fe->symtab);
cb1df416
DJ
21183 }
21184
c906108c
SS
21185 switch (die->tag)
21186 {
21187 case DW_TAG_label:
e142c38c 21188 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
c906108c 21189 if (attr)
3e29f34a
MR
21190 {
21191 CORE_ADDR addr;
21192
21193 addr = attr_value_as_address (attr);
21194 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr + baseaddr);
21195 SYMBOL_VALUE_ADDRESS (sym) = addr;
21196 }
0f5238ed
TT
21197 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
21198 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
f1e6e072 21199 SYMBOL_ACLASS_INDEX (sym) = LOC_LABEL;
0f5238ed 21200 add_symbol_to_list (sym, cu->list_in_scope);
c906108c
SS
21201 break;
21202 case DW_TAG_subprogram:
21203 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
21204 finish_block. */
f1e6e072 21205 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
e142c38c 21206 attr2 = dwarf2_attr (die, DW_AT_external, cu);
2cfa0c8d
JB
21207 if ((attr2 && (DW_UNSND (attr2) != 0))
21208 || cu->language == language_ada)
c906108c 21209 {
2cfa0c8d
JB
21210 /* Subprograms marked external are stored as a global symbol.
21211 Ada subprograms, whether marked external or not, are always
21212 stored as a global symbol, because we want to be able to
21213 access them globally. For instance, we want to be able
21214 to break on a nested subprogram without having to
21215 specify the context. */
e37fd15a 21216 list_to_add = &global_symbols;
c906108c
SS
21217 }
21218 else
21219 {
e37fd15a 21220 list_to_add = cu->list_in_scope;
c906108c
SS
21221 }
21222 break;
edb3359d
DJ
21223 case DW_TAG_inlined_subroutine:
21224 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
21225 finish_block. */
f1e6e072 21226 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
edb3359d 21227 SYMBOL_INLINED (sym) = 1;
481860b3 21228 list_to_add = cu->list_in_scope;
edb3359d 21229 break;
34eaf542
TT
21230 case DW_TAG_template_value_param:
21231 suppress_add = 1;
21232 /* Fall through. */
72929c62 21233 case DW_TAG_constant:
c906108c 21234 case DW_TAG_variable:
254e6b9e 21235 case DW_TAG_member:
0963b4bd
MS
21236 /* Compilation with minimal debug info may result in
21237 variables with missing type entries. Change the
21238 misleading `void' type to something sensible. */
c906108c 21239 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
46a4882b 21240 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_int;
64c50499 21241
e142c38c 21242 attr = dwarf2_attr (die, DW_AT_const_value, cu);
254e6b9e
DE
21243 /* In the case of DW_TAG_member, we should only be called for
21244 static const members. */
21245 if (die->tag == DW_TAG_member)
21246 {
3863f96c
DE
21247 /* dwarf2_add_field uses die_is_declaration,
21248 so we do the same. */
254e6b9e
DE
21249 gdb_assert (die_is_declaration (die, cu));
21250 gdb_assert (attr);
21251 }
c906108c
SS
21252 if (attr)
21253 {
e7c27a73 21254 dwarf2_const_value (attr, sym, cu);
e142c38c 21255 attr2 = dwarf2_attr (die, DW_AT_external, cu);
e37fd15a 21256 if (!suppress_add)
34eaf542
TT
21257 {
21258 if (attr2 && (DW_UNSND (attr2) != 0))
e37fd15a 21259 list_to_add = &global_symbols;
34eaf542 21260 else
e37fd15a 21261 list_to_add = cu->list_in_scope;
34eaf542 21262 }
c906108c
SS
21263 break;
21264 }
e142c38c 21265 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
21266 if (attr)
21267 {
e7c27a73 21268 var_decode_location (attr, sym, cu);
e142c38c 21269 attr2 = dwarf2_attr (die, DW_AT_external, cu);
4357ac6c
TT
21270
21271 /* Fortran explicitly imports any global symbols to the local
21272 scope by DW_TAG_common_block. */
21273 if (cu->language == language_fortran && die->parent
21274 && die->parent->tag == DW_TAG_common_block)
21275 attr2 = NULL;
21276
caac4577
JG
21277 if (SYMBOL_CLASS (sym) == LOC_STATIC
21278 && SYMBOL_VALUE_ADDRESS (sym) == 0
21279 && !dwarf2_per_objfile->has_section_at_zero)
21280 {
21281 /* When a static variable is eliminated by the linker,
21282 the corresponding debug information is not stripped
21283 out, but the variable address is set to null;
21284 do not add such variables into symbol table. */
21285 }
21286 else if (attr2 && (DW_UNSND (attr2) != 0))
1c809c68 21287 {
f55ee35c
JK
21288 /* Workaround gfortran PR debug/40040 - it uses
21289 DW_AT_location for variables in -fPIC libraries which may
21290 get overriden by other libraries/executable and get
21291 a different address. Resolve it by the minimal symbol
21292 which may come from inferior's executable using copy
21293 relocation. Make this workaround only for gfortran as for
21294 other compilers GDB cannot guess the minimal symbol
21295 Fortran mangling kind. */
21296 if (cu->language == language_fortran && die->parent
21297 && die->parent->tag == DW_TAG_module
21298 && cu->producer
28586665 21299 && startswith (cu->producer, "GNU Fortran"))
f1e6e072 21300 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
f55ee35c 21301
1c809c68
TT
21302 /* A variable with DW_AT_external is never static,
21303 but it may be block-scoped. */
21304 list_to_add = (cu->list_in_scope == &file_symbols
21305 ? &global_symbols : cu->list_in_scope);
1c809c68 21306 }
c906108c 21307 else
e37fd15a 21308 list_to_add = cu->list_in_scope;
c906108c
SS
21309 }
21310 else
21311 {
21312 /* We do not know the address of this symbol.
c5aa993b
JM
21313 If it is an external symbol and we have type information
21314 for it, enter the symbol as a LOC_UNRESOLVED symbol.
21315 The address of the variable will then be determined from
21316 the minimal symbol table whenever the variable is
21317 referenced. */
e142c38c 21318 attr2 = dwarf2_attr (die, DW_AT_external, cu);
0971de02
TT
21319
21320 /* Fortran explicitly imports any global symbols to the local
21321 scope by DW_TAG_common_block. */
21322 if (cu->language == language_fortran && die->parent
21323 && die->parent->tag == DW_TAG_common_block)
21324 {
21325 /* SYMBOL_CLASS doesn't matter here because
21326 read_common_block is going to reset it. */
21327 if (!suppress_add)
21328 list_to_add = cu->list_in_scope;
21329 }
21330 else if (attr2 && (DW_UNSND (attr2) != 0)
21331 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
c906108c 21332 {
0fe7935b
DJ
21333 /* A variable with DW_AT_external is never static, but it
21334 may be block-scoped. */
21335 list_to_add = (cu->list_in_scope == &file_symbols
21336 ? &global_symbols : cu->list_in_scope);
21337
f1e6e072 21338 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
c906108c 21339 }
442ddf59
JK
21340 else if (!die_is_declaration (die, cu))
21341 {
21342 /* Use the default LOC_OPTIMIZED_OUT class. */
21343 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
e37fd15a
SW
21344 if (!suppress_add)
21345 list_to_add = cu->list_in_scope;
442ddf59 21346 }
c906108c
SS
21347 }
21348 break;
21349 case DW_TAG_formal_parameter:
a60f3166
TT
21350 {
21351 /* If we are inside a function, mark this as an argument. If
21352 not, we might be looking at an argument to an inlined function
21353 when we do not have enough information to show inlined frames;
21354 pretend it's a local variable in that case so that the user can
21355 still see it. */
21356 struct context_stack *curr = get_current_context_stack ();
21357 if (curr != nullptr && curr->name != nullptr)
21358 SYMBOL_IS_ARGUMENT (sym) = 1;
21359 attr = dwarf2_attr (die, DW_AT_location, cu);
21360 if (attr)
21361 {
21362 var_decode_location (attr, sym, cu);
21363 }
21364 attr = dwarf2_attr (die, DW_AT_const_value, cu);
21365 if (attr)
21366 {
21367 dwarf2_const_value (attr, sym, cu);
21368 }
f346a30d 21369
a60f3166
TT
21370 list_to_add = cu->list_in_scope;
21371 }
c906108c
SS
21372 break;
21373 case DW_TAG_unspecified_parameters:
21374 /* From varargs functions; gdb doesn't seem to have any
21375 interest in this information, so just ignore it for now.
21376 (FIXME?) */
21377 break;
34eaf542
TT
21378 case DW_TAG_template_type_param:
21379 suppress_add = 1;
21380 /* Fall through. */
c906108c 21381 case DW_TAG_class_type:
680b30c7 21382 case DW_TAG_interface_type:
c906108c
SS
21383 case DW_TAG_structure_type:
21384 case DW_TAG_union_type:
72019c9c 21385 case DW_TAG_set_type:
c906108c 21386 case DW_TAG_enumeration_type:
f1e6e072 21387 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
176620f1 21388 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
c906108c 21389
63d06c5c 21390 {
9c37b5ae 21391 /* NOTE: carlton/2003-11-10: C++ class symbols shouldn't
63d06c5c
DC
21392 really ever be static objects: otherwise, if you try
21393 to, say, break of a class's method and you're in a file
21394 which doesn't mention that class, it won't work unless
21395 the check for all static symbols in lookup_symbol_aux
21396 saves you. See the OtherFileClass tests in
21397 gdb.c++/namespace.exp. */
21398
e37fd15a 21399 if (!suppress_add)
34eaf542 21400 {
34eaf542 21401 list_to_add = (cu->list_in_scope == &file_symbols
9c37b5ae 21402 && cu->language == language_cplus
34eaf542 21403 ? &global_symbols : cu->list_in_scope);
63d06c5c 21404
64382290 21405 /* The semantics of C++ state that "struct foo {
9c37b5ae 21406 ... }" also defines a typedef for "foo". */
64382290 21407 if (cu->language == language_cplus
45280282 21408 || cu->language == language_ada
c44af4eb
TT
21409 || cu->language == language_d
21410 || cu->language == language_rust)
64382290
TT
21411 {
21412 /* The symbol's name is already allocated along
21413 with this objfile, so we don't need to
21414 duplicate it for the type. */
21415 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
21416 TYPE_NAME (SYMBOL_TYPE (sym)) = SYMBOL_SEARCH_NAME (sym);
21417 }
63d06c5c
DC
21418 }
21419 }
c906108c
SS
21420 break;
21421 case DW_TAG_typedef:
f1e6e072 21422 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
63d06c5c 21423 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 21424 list_to_add = cu->list_in_scope;
63d06c5c 21425 break;
c906108c 21426 case DW_TAG_base_type:
a02abb62 21427 case DW_TAG_subrange_type:
f1e6e072 21428 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
176620f1 21429 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 21430 list_to_add = cu->list_in_scope;
c906108c
SS
21431 break;
21432 case DW_TAG_enumerator:
e142c38c 21433 attr = dwarf2_attr (die, DW_AT_const_value, cu);
c906108c
SS
21434 if (attr)
21435 {
e7c27a73 21436 dwarf2_const_value (attr, sym, cu);
c906108c 21437 }
63d06c5c
DC
21438 {
21439 /* NOTE: carlton/2003-11-10: See comment above in the
21440 DW_TAG_class_type, etc. block. */
21441
e142c38c 21442 list_to_add = (cu->list_in_scope == &file_symbols
9c37b5ae 21443 && cu->language == language_cplus
e142c38c 21444 ? &global_symbols : cu->list_in_scope);
63d06c5c 21445 }
c906108c 21446 break;
74921315 21447 case DW_TAG_imported_declaration:
5c4e30ca 21448 case DW_TAG_namespace:
f1e6e072 21449 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
e37fd15a 21450 list_to_add = &global_symbols;
5c4e30ca 21451 break;
530e8392
KB
21452 case DW_TAG_module:
21453 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21454 SYMBOL_DOMAIN (sym) = MODULE_DOMAIN;
21455 list_to_add = &global_symbols;
21456 break;
4357ac6c 21457 case DW_TAG_common_block:
f1e6e072 21458 SYMBOL_ACLASS_INDEX (sym) = LOC_COMMON_BLOCK;
4357ac6c
TT
21459 SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
21460 add_symbol_to_list (sym, cu->list_in_scope);
21461 break;
c906108c
SS
21462 default:
21463 /* Not a tag we recognize. Hopefully we aren't processing
21464 trash data, but since we must specifically ignore things
21465 we don't recognize, there is nothing else we should do at
0963b4bd 21466 this point. */
b98664d3 21467 complaint (_("unsupported tag: '%s'"),
4d3c2250 21468 dwarf_tag_name (die->tag));
c906108c
SS
21469 break;
21470 }
df8a16a1 21471
e37fd15a
SW
21472 if (suppress_add)
21473 {
21474 sym->hash_next = objfile->template_symbols;
21475 objfile->template_symbols = sym;
21476 list_to_add = NULL;
21477 }
21478
21479 if (list_to_add != NULL)
21480 add_symbol_to_list (sym, list_to_add);
21481
df8a16a1
DJ
21482 /* For the benefit of old versions of GCC, check for anonymous
21483 namespaces based on the demangled name. */
4d4ec4e5 21484 if (!cu->processing_has_namespace_info
94af9270 21485 && cu->language == language_cplus)
a10964d1 21486 cp_scan_for_anonymous_namespaces (sym, objfile);
c906108c
SS
21487 }
21488 return (sym);
21489}
21490
98bfdba5
PA
21491/* Given an attr with a DW_FORM_dataN value in host byte order,
21492 zero-extend it as appropriate for the symbol's type. The DWARF
21493 standard (v4) is not entirely clear about the meaning of using
21494 DW_FORM_dataN for a constant with a signed type, where the type is
21495 wider than the data. The conclusion of a discussion on the DWARF
21496 list was that this is unspecified. We choose to always zero-extend
21497 because that is the interpretation long in use by GCC. */
c906108c 21498
98bfdba5 21499static gdb_byte *
ff39bb5e 21500dwarf2_const_value_data (const struct attribute *attr, struct obstack *obstack,
12df843f 21501 struct dwarf2_cu *cu, LONGEST *value, int bits)
c906108c 21502{
518817b3 21503 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
e17a4113
UW
21504 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
21505 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
98bfdba5
PA
21506 LONGEST l = DW_UNSND (attr);
21507
21508 if (bits < sizeof (*value) * 8)
21509 {
21510 l &= ((LONGEST) 1 << bits) - 1;
21511 *value = l;
21512 }
21513 else if (bits == sizeof (*value) * 8)
21514 *value = l;
21515 else
21516 {
224c3ddb 21517 gdb_byte *bytes = (gdb_byte *) obstack_alloc (obstack, bits / 8);
98bfdba5
PA
21518 store_unsigned_integer (bytes, bits / 8, byte_order, l);
21519 return bytes;
21520 }
21521
21522 return NULL;
21523}
21524
21525/* Read a constant value from an attribute. Either set *VALUE, or if
21526 the value does not fit in *VALUE, set *BYTES - either already
21527 allocated on the objfile obstack, or newly allocated on OBSTACK,
21528 or, set *BATON, if we translated the constant to a location
21529 expression. */
21530
21531static void
ff39bb5e 21532dwarf2_const_value_attr (const struct attribute *attr, struct type *type,
98bfdba5
PA
21533 const char *name, struct obstack *obstack,
21534 struct dwarf2_cu *cu,
d521ce57 21535 LONGEST *value, const gdb_byte **bytes,
98bfdba5
PA
21536 struct dwarf2_locexpr_baton **baton)
21537{
518817b3 21538 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
98bfdba5 21539 struct comp_unit_head *cu_header = &cu->header;
c906108c 21540 struct dwarf_block *blk;
98bfdba5
PA
21541 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
21542 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
21543
21544 *value = 0;
21545 *bytes = NULL;
21546 *baton = NULL;
c906108c
SS
21547
21548 switch (attr->form)
21549 {
21550 case DW_FORM_addr:
3019eac3 21551 case DW_FORM_GNU_addr_index:
ac56253d 21552 {
ac56253d
TT
21553 gdb_byte *data;
21554
98bfdba5
PA
21555 if (TYPE_LENGTH (type) != cu_header->addr_size)
21556 dwarf2_const_value_length_mismatch_complaint (name,
ac56253d 21557 cu_header->addr_size,
98bfdba5 21558 TYPE_LENGTH (type));
ac56253d
TT
21559 /* Symbols of this form are reasonably rare, so we just
21560 piggyback on the existing location code rather than writing
21561 a new implementation of symbol_computed_ops. */
8d749320 21562 *baton = XOBNEW (obstack, struct dwarf2_locexpr_baton);
98bfdba5
PA
21563 (*baton)->per_cu = cu->per_cu;
21564 gdb_assert ((*baton)->per_cu);
ac56253d 21565
98bfdba5 21566 (*baton)->size = 2 + cu_header->addr_size;
224c3ddb 21567 data = (gdb_byte *) obstack_alloc (obstack, (*baton)->size);
98bfdba5 21568 (*baton)->data = data;
ac56253d
TT
21569
21570 data[0] = DW_OP_addr;
21571 store_unsigned_integer (&data[1], cu_header->addr_size,
21572 byte_order, DW_ADDR (attr));
21573 data[cu_header->addr_size + 1] = DW_OP_stack_value;
ac56253d 21574 }
c906108c 21575 break;
4ac36638 21576 case DW_FORM_string:
93b5768b 21577 case DW_FORM_strp:
3019eac3 21578 case DW_FORM_GNU_str_index:
36586728 21579 case DW_FORM_GNU_strp_alt:
98bfdba5
PA
21580 /* DW_STRING is already allocated on the objfile obstack, point
21581 directly to it. */
d521ce57 21582 *bytes = (const gdb_byte *) DW_STRING (attr);
93b5768b 21583 break;
c906108c
SS
21584 case DW_FORM_block1:
21585 case DW_FORM_block2:
21586 case DW_FORM_block4:
21587 case DW_FORM_block:
2dc7f7b3 21588 case DW_FORM_exprloc:
0224619f 21589 case DW_FORM_data16:
c906108c 21590 blk = DW_BLOCK (attr);
98bfdba5
PA
21591 if (TYPE_LENGTH (type) != blk->size)
21592 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
21593 TYPE_LENGTH (type));
21594 *bytes = blk->data;
c906108c 21595 break;
2df3850c
JM
21596
21597 /* The DW_AT_const_value attributes are supposed to carry the
21598 symbol's value "represented as it would be on the target
21599 architecture." By the time we get here, it's already been
21600 converted to host endianness, so we just need to sign- or
21601 zero-extend it as appropriate. */
21602 case DW_FORM_data1:
3aef2284 21603 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 8);
2df3850c 21604 break;
c906108c 21605 case DW_FORM_data2:
3aef2284 21606 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 16);
2df3850c 21607 break;
c906108c 21608 case DW_FORM_data4:
3aef2284 21609 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 32);
2df3850c 21610 break;
c906108c 21611 case DW_FORM_data8:
3aef2284 21612 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 64);
2df3850c
JM
21613 break;
21614
c906108c 21615 case DW_FORM_sdata:
663c44ac 21616 case DW_FORM_implicit_const:
98bfdba5 21617 *value = DW_SND (attr);
2df3850c
JM
21618 break;
21619
c906108c 21620 case DW_FORM_udata:
98bfdba5 21621 *value = DW_UNSND (attr);
c906108c 21622 break;
2df3850c 21623
c906108c 21624 default:
b98664d3 21625 complaint (_("unsupported const value attribute form: '%s'"),
4d3c2250 21626 dwarf_form_name (attr->form));
98bfdba5 21627 *value = 0;
c906108c
SS
21628 break;
21629 }
21630}
21631
2df3850c 21632
98bfdba5
PA
21633/* Copy constant value from an attribute to a symbol. */
21634
2df3850c 21635static void
ff39bb5e 21636dwarf2_const_value (const struct attribute *attr, struct symbol *sym,
98bfdba5 21637 struct dwarf2_cu *cu)
2df3850c 21638{
518817b3 21639 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
12df843f 21640 LONGEST value;
d521ce57 21641 const gdb_byte *bytes;
98bfdba5 21642 struct dwarf2_locexpr_baton *baton;
2df3850c 21643
98bfdba5
PA
21644 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
21645 SYMBOL_PRINT_NAME (sym),
21646 &objfile->objfile_obstack, cu,
21647 &value, &bytes, &baton);
2df3850c 21648
98bfdba5
PA
21649 if (baton != NULL)
21650 {
98bfdba5 21651 SYMBOL_LOCATION_BATON (sym) = baton;
f1e6e072 21652 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
98bfdba5
PA
21653 }
21654 else if (bytes != NULL)
21655 {
21656 SYMBOL_VALUE_BYTES (sym) = bytes;
f1e6e072 21657 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST_BYTES;
98bfdba5
PA
21658 }
21659 else
21660 {
21661 SYMBOL_VALUE (sym) = value;
f1e6e072 21662 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST;
98bfdba5 21663 }
2df3850c
JM
21664}
21665
c906108c
SS
21666/* Return the type of the die in question using its DW_AT_type attribute. */
21667
21668static struct type *
e7c27a73 21669die_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 21670{
c906108c 21671 struct attribute *type_attr;
c906108c 21672
e142c38c 21673 type_attr = dwarf2_attr (die, DW_AT_type, cu);
c906108c
SS
21674 if (!type_attr)
21675 {
518817b3 21676 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c 21677 /* A missing DW_AT_type represents a void type. */
518817b3 21678 return objfile_type (objfile)->builtin_void;
c906108c 21679 }
348e048f 21680
673bfd45 21681 return lookup_die_type (die, type_attr, cu);
c906108c
SS
21682}
21683
b4ba55a1
JB
21684/* True iff CU's producer generates GNAT Ada auxiliary information
21685 that allows to find parallel types through that information instead
21686 of having to do expensive parallel lookups by type name. */
21687
21688static int
21689need_gnat_info (struct dwarf2_cu *cu)
21690{
de4cb04a
JB
21691 /* Assume that the Ada compiler was GNAT, which always produces
21692 the auxiliary information. */
21693 return (cu->language == language_ada);
b4ba55a1
JB
21694}
21695
b4ba55a1
JB
21696/* Return the auxiliary type of the die in question using its
21697 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
21698 attribute is not present. */
21699
21700static struct type *
21701die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
21702{
b4ba55a1 21703 struct attribute *type_attr;
b4ba55a1
JB
21704
21705 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
21706 if (!type_attr)
21707 return NULL;
21708
673bfd45 21709 return lookup_die_type (die, type_attr, cu);
b4ba55a1
JB
21710}
21711
21712/* If DIE has a descriptive_type attribute, then set the TYPE's
21713 descriptive type accordingly. */
21714
21715static void
21716set_descriptive_type (struct type *type, struct die_info *die,
21717 struct dwarf2_cu *cu)
21718{
21719 struct type *descriptive_type = die_descriptive_type (die, cu);
21720
21721 if (descriptive_type)
21722 {
21723 ALLOCATE_GNAT_AUX_TYPE (type);
21724 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
21725 }
21726}
21727
c906108c
SS
21728/* Return the containing type of the die in question using its
21729 DW_AT_containing_type attribute. */
21730
21731static struct type *
e7c27a73 21732die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 21733{
c906108c 21734 struct attribute *type_attr;
518817b3 21735 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c 21736
e142c38c 21737 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
33ac96f0
JK
21738 if (!type_attr)
21739 error (_("Dwarf Error: Problem turning containing type into gdb type "
518817b3 21740 "[in module %s]"), objfile_name (objfile));
33ac96f0 21741
673bfd45 21742 return lookup_die_type (die, type_attr, cu);
c906108c
SS
21743}
21744
ac9ec31b
DE
21745/* Return an error marker type to use for the ill formed type in DIE/CU. */
21746
21747static struct type *
21748build_error_marker_type (struct dwarf2_cu *cu, struct die_info *die)
21749{
518817b3
SM
21750 struct dwarf2_per_objfile *dwarf2_per_objfile
21751 = cu->per_cu->dwarf2_per_objfile;
ac9ec31b
DE
21752 struct objfile *objfile = dwarf2_per_objfile->objfile;
21753 char *message, *saved;
21754
9d8780f0 21755 message = xstrprintf (_("<unknown type in %s, CU %s, DIE %s>"),
4262abfb 21756 objfile_name (objfile),
9d8780f0
SM
21757 sect_offset_str (cu->header.sect_off),
21758 sect_offset_str (die->sect_off));
224c3ddb
SM
21759 saved = (char *) obstack_copy0 (&objfile->objfile_obstack,
21760 message, strlen (message));
ac9ec31b
DE
21761 xfree (message);
21762
19f392bc 21763 return init_type (objfile, TYPE_CODE_ERROR, 0, saved);
ac9ec31b
DE
21764}
21765
673bfd45 21766/* Look up the type of DIE in CU using its type attribute ATTR.
ac9ec31b
DE
21767 ATTR must be one of: DW_AT_type, DW_AT_GNAT_descriptive_type,
21768 DW_AT_containing_type.
673bfd45
DE
21769 If there is no type substitute an error marker. */
21770
c906108c 21771static struct type *
ff39bb5e 21772lookup_die_type (struct die_info *die, const struct attribute *attr,
673bfd45 21773 struct dwarf2_cu *cu)
c906108c 21774{
518817b3
SM
21775 struct dwarf2_per_objfile *dwarf2_per_objfile
21776 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 21777 struct objfile *objfile = dwarf2_per_objfile->objfile;
f792889a
DJ
21778 struct type *this_type;
21779
ac9ec31b
DE
21780 gdb_assert (attr->name == DW_AT_type
21781 || attr->name == DW_AT_GNAT_descriptive_type
21782 || attr->name == DW_AT_containing_type);
21783
673bfd45
DE
21784 /* First see if we have it cached. */
21785
36586728
TT
21786 if (attr->form == DW_FORM_GNU_ref_alt)
21787 {
21788 struct dwarf2_per_cu_data *per_cu;
9c541725 21789 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
36586728 21790
ed2dc618
SM
21791 per_cu = dwarf2_find_containing_comp_unit (sect_off, 1,
21792 dwarf2_per_objfile);
9c541725 21793 this_type = get_die_type_at_offset (sect_off, per_cu);
36586728 21794 }
7771576e 21795 else if (attr_form_is_ref (attr))
673bfd45 21796 {
9c541725 21797 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
673bfd45 21798
9c541725 21799 this_type = get_die_type_at_offset (sect_off, cu->per_cu);
673bfd45 21800 }
55f1336d 21801 else if (attr->form == DW_FORM_ref_sig8)
673bfd45 21802 {
ac9ec31b 21803 ULONGEST signature = DW_SIGNATURE (attr);
673bfd45 21804
ac9ec31b 21805 return get_signatured_type (die, signature, cu);
673bfd45
DE
21806 }
21807 else
21808 {
b98664d3 21809 complaint (_("Dwarf Error: Bad type attribute %s in DIE"
9d8780f0
SM
21810 " at %s [in module %s]"),
21811 dwarf_attr_name (attr->name), sect_offset_str (die->sect_off),
4262abfb 21812 objfile_name (objfile));
ac9ec31b 21813 return build_error_marker_type (cu, die);
673bfd45
DE
21814 }
21815
21816 /* If not cached we need to read it in. */
21817
21818 if (this_type == NULL)
21819 {
ac9ec31b 21820 struct die_info *type_die = NULL;
673bfd45
DE
21821 struct dwarf2_cu *type_cu = cu;
21822
7771576e 21823 if (attr_form_is_ref (attr))
ac9ec31b
DE
21824 type_die = follow_die_ref (die, attr, &type_cu);
21825 if (type_die == NULL)
21826 return build_error_marker_type (cu, die);
21827 /* If we find the type now, it's probably because the type came
3019eac3
DE
21828 from an inter-CU reference and the type's CU got expanded before
21829 ours. */
ac9ec31b 21830 this_type = read_type_die (type_die, type_cu);
673bfd45
DE
21831 }
21832
21833 /* If we still don't have a type use an error marker. */
21834
21835 if (this_type == NULL)
ac9ec31b 21836 return build_error_marker_type (cu, die);
673bfd45 21837
f792889a 21838 return this_type;
c906108c
SS
21839}
21840
673bfd45
DE
21841/* Return the type in DIE, CU.
21842 Returns NULL for invalid types.
21843
02142a6c 21844 This first does a lookup in die_type_hash,
673bfd45
DE
21845 and only reads the die in if necessary.
21846
21847 NOTE: This can be called when reading in partial or full symbols. */
21848
f792889a 21849static struct type *
e7c27a73 21850read_type_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 21851{
f792889a
DJ
21852 struct type *this_type;
21853
21854 this_type = get_die_type (die, cu);
21855 if (this_type)
21856 return this_type;
21857
673bfd45
DE
21858 return read_type_die_1 (die, cu);
21859}
21860
21861/* Read the type in DIE, CU.
21862 Returns NULL for invalid types. */
21863
21864static struct type *
21865read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
21866{
21867 struct type *this_type = NULL;
21868
c906108c
SS
21869 switch (die->tag)
21870 {
21871 case DW_TAG_class_type:
680b30c7 21872 case DW_TAG_interface_type:
c906108c
SS
21873 case DW_TAG_structure_type:
21874 case DW_TAG_union_type:
f792889a 21875 this_type = read_structure_type (die, cu);
c906108c
SS
21876 break;
21877 case DW_TAG_enumeration_type:
f792889a 21878 this_type = read_enumeration_type (die, cu);
c906108c
SS
21879 break;
21880 case DW_TAG_subprogram:
21881 case DW_TAG_subroutine_type:
edb3359d 21882 case DW_TAG_inlined_subroutine:
f792889a 21883 this_type = read_subroutine_type (die, cu);
c906108c
SS
21884 break;
21885 case DW_TAG_array_type:
f792889a 21886 this_type = read_array_type (die, cu);
c906108c 21887 break;
72019c9c 21888 case DW_TAG_set_type:
f792889a 21889 this_type = read_set_type (die, cu);
72019c9c 21890 break;
c906108c 21891 case DW_TAG_pointer_type:
f792889a 21892 this_type = read_tag_pointer_type (die, cu);
c906108c
SS
21893 break;
21894 case DW_TAG_ptr_to_member_type:
f792889a 21895 this_type = read_tag_ptr_to_member_type (die, cu);
c906108c
SS
21896 break;
21897 case DW_TAG_reference_type:
4297a3f0
AV
21898 this_type = read_tag_reference_type (die, cu, TYPE_CODE_REF);
21899 break;
21900 case DW_TAG_rvalue_reference_type:
21901 this_type = read_tag_reference_type (die, cu, TYPE_CODE_RVALUE_REF);
c906108c
SS
21902 break;
21903 case DW_TAG_const_type:
f792889a 21904 this_type = read_tag_const_type (die, cu);
c906108c
SS
21905 break;
21906 case DW_TAG_volatile_type:
f792889a 21907 this_type = read_tag_volatile_type (die, cu);
c906108c 21908 break;
06d66ee9
TT
21909 case DW_TAG_restrict_type:
21910 this_type = read_tag_restrict_type (die, cu);
21911 break;
c906108c 21912 case DW_TAG_string_type:
f792889a 21913 this_type = read_tag_string_type (die, cu);
c906108c
SS
21914 break;
21915 case DW_TAG_typedef:
f792889a 21916 this_type = read_typedef (die, cu);
c906108c 21917 break;
a02abb62 21918 case DW_TAG_subrange_type:
f792889a 21919 this_type = read_subrange_type (die, cu);
a02abb62 21920 break;
c906108c 21921 case DW_TAG_base_type:
f792889a 21922 this_type = read_base_type (die, cu);
c906108c 21923 break;
81a17f79 21924 case DW_TAG_unspecified_type:
f792889a 21925 this_type = read_unspecified_type (die, cu);
81a17f79 21926 break;
0114d602
DJ
21927 case DW_TAG_namespace:
21928 this_type = read_namespace_type (die, cu);
21929 break;
f55ee35c
JK
21930 case DW_TAG_module:
21931 this_type = read_module_type (die, cu);
21932 break;
a2c2acaf
MW
21933 case DW_TAG_atomic_type:
21934 this_type = read_tag_atomic_type (die, cu);
21935 break;
c906108c 21936 default:
b98664d3 21937 complaint (_("unexpected tag in read_type_die: '%s'"),
4d3c2250 21938 dwarf_tag_name (die->tag));
c906108c
SS
21939 break;
21940 }
63d06c5c 21941
f792889a 21942 return this_type;
63d06c5c
DC
21943}
21944
abc72ce4
DE
21945/* See if we can figure out if the class lives in a namespace. We do
21946 this by looking for a member function; its demangled name will
21947 contain namespace info, if there is any.
21948 Return the computed name or NULL.
21949 Space for the result is allocated on the objfile's obstack.
21950 This is the full-die version of guess_partial_die_structure_name.
21951 In this case we know DIE has no useful parent. */
21952
21953static char *
21954guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
21955{
21956 struct die_info *spec_die;
21957 struct dwarf2_cu *spec_cu;
21958 struct die_info *child;
518817b3 21959 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
abc72ce4
DE
21960
21961 spec_cu = cu;
21962 spec_die = die_specification (die, &spec_cu);
21963 if (spec_die != NULL)
21964 {
21965 die = spec_die;
21966 cu = spec_cu;
21967 }
21968
21969 for (child = die->child;
21970 child != NULL;
21971 child = child->sibling)
21972 {
21973 if (child->tag == DW_TAG_subprogram)
21974 {
73b9be8b 21975 const char *linkage_name = dw2_linkage_name (child, cu);
abc72ce4 21976
7d45c7c3 21977 if (linkage_name != NULL)
abc72ce4
DE
21978 {
21979 char *actual_name
21980 = language_class_name_from_physname (cu->language_defn,
7d45c7c3 21981 linkage_name);
abc72ce4
DE
21982 char *name = NULL;
21983
21984 if (actual_name != NULL)
21985 {
15d034d0 21986 const char *die_name = dwarf2_name (die, cu);
abc72ce4
DE
21987
21988 if (die_name != NULL
21989 && strcmp (die_name, actual_name) != 0)
21990 {
21991 /* Strip off the class name from the full name.
21992 We want the prefix. */
21993 int die_name_len = strlen (die_name);
21994 int actual_name_len = strlen (actual_name);
21995
21996 /* Test for '::' as a sanity check. */
21997 if (actual_name_len > die_name_len + 2
3e43a32a
MS
21998 && actual_name[actual_name_len
21999 - die_name_len - 1] == ':')
224c3ddb 22000 name = (char *) obstack_copy0 (
e3b94546 22001 &objfile->per_bfd->storage_obstack,
224c3ddb 22002 actual_name, actual_name_len - die_name_len - 2);
abc72ce4
DE
22003 }
22004 }
22005 xfree (actual_name);
22006 return name;
22007 }
22008 }
22009 }
22010
22011 return NULL;
22012}
22013
96408a79
SA
22014/* GCC might emit a nameless typedef that has a linkage name. Determine the
22015 prefix part in such case. See
22016 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
22017
a121b7c1 22018static const char *
96408a79
SA
22019anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
22020{
22021 struct attribute *attr;
e6a959d6 22022 const char *base;
96408a79
SA
22023
22024 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
22025 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
22026 return NULL;
22027
7d45c7c3 22028 if (dwarf2_string_attr (die, DW_AT_name, cu) != NULL)
96408a79
SA
22029 return NULL;
22030
73b9be8b 22031 attr = dw2_linkage_name_attr (die, cu);
96408a79
SA
22032 if (attr == NULL || DW_STRING (attr) == NULL)
22033 return NULL;
22034
22035 /* dwarf2_name had to be already called. */
22036 gdb_assert (DW_STRING_IS_CANONICAL (attr));
22037
22038 /* Strip the base name, keep any leading namespaces/classes. */
22039 base = strrchr (DW_STRING (attr), ':');
22040 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
22041 return "";
22042
518817b3 22043 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
e3b94546 22044 return (char *) obstack_copy0 (&objfile->per_bfd->storage_obstack,
224c3ddb
SM
22045 DW_STRING (attr),
22046 &base[-1] - DW_STRING (attr));
96408a79
SA
22047}
22048
fdde2d81 22049/* Return the name of the namespace/class that DIE is defined within,
0114d602 22050 or "" if we can't tell. The caller should not xfree the result.
fdde2d81 22051
0114d602
DJ
22052 For example, if we're within the method foo() in the following
22053 code:
22054
22055 namespace N {
22056 class C {
22057 void foo () {
22058 }
22059 };
22060 }
22061
22062 then determine_prefix on foo's die will return "N::C". */
fdde2d81 22063
0d5cff50 22064static const char *
e142c38c 22065determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
63d06c5c 22066{
518817b3
SM
22067 struct dwarf2_per_objfile *dwarf2_per_objfile
22068 = cu->per_cu->dwarf2_per_objfile;
0114d602
DJ
22069 struct die_info *parent, *spec_die;
22070 struct dwarf2_cu *spec_cu;
22071 struct type *parent_type;
a121b7c1 22072 const char *retval;
63d06c5c 22073
9c37b5ae 22074 if (cu->language != language_cplus
c44af4eb
TT
22075 && cu->language != language_fortran && cu->language != language_d
22076 && cu->language != language_rust)
0114d602
DJ
22077 return "";
22078
96408a79
SA
22079 retval = anonymous_struct_prefix (die, cu);
22080 if (retval)
22081 return retval;
22082
0114d602
DJ
22083 /* We have to be careful in the presence of DW_AT_specification.
22084 For example, with GCC 3.4, given the code
22085
22086 namespace N {
22087 void foo() {
22088 // Definition of N::foo.
22089 }
22090 }
22091
22092 then we'll have a tree of DIEs like this:
22093
22094 1: DW_TAG_compile_unit
22095 2: DW_TAG_namespace // N
22096 3: DW_TAG_subprogram // declaration of N::foo
22097 4: DW_TAG_subprogram // definition of N::foo
22098 DW_AT_specification // refers to die #3
22099
22100 Thus, when processing die #4, we have to pretend that we're in
22101 the context of its DW_AT_specification, namely the contex of die
22102 #3. */
22103 spec_cu = cu;
22104 spec_die = die_specification (die, &spec_cu);
22105 if (spec_die == NULL)
22106 parent = die->parent;
22107 else
63d06c5c 22108 {
0114d602
DJ
22109 parent = spec_die->parent;
22110 cu = spec_cu;
63d06c5c 22111 }
0114d602
DJ
22112
22113 if (parent == NULL)
22114 return "";
98bfdba5
PA
22115 else if (parent->building_fullname)
22116 {
22117 const char *name;
22118 const char *parent_name;
22119
22120 /* It has been seen on RealView 2.2 built binaries,
22121 DW_TAG_template_type_param types actually _defined_ as
22122 children of the parent class:
22123
22124 enum E {};
22125 template class <class Enum> Class{};
22126 Class<enum E> class_e;
22127
22128 1: DW_TAG_class_type (Class)
22129 2: DW_TAG_enumeration_type (E)
22130 3: DW_TAG_enumerator (enum1:0)
22131 3: DW_TAG_enumerator (enum2:1)
22132 ...
22133 2: DW_TAG_template_type_param
22134 DW_AT_type DW_FORM_ref_udata (E)
22135
22136 Besides being broken debug info, it can put GDB into an
22137 infinite loop. Consider:
22138
22139 When we're building the full name for Class<E>, we'll start
22140 at Class, and go look over its template type parameters,
22141 finding E. We'll then try to build the full name of E, and
22142 reach here. We're now trying to build the full name of E,
22143 and look over the parent DIE for containing scope. In the
22144 broken case, if we followed the parent DIE of E, we'd again
22145 find Class, and once again go look at its template type
22146 arguments, etc., etc. Simply don't consider such parent die
22147 as source-level parent of this die (it can't be, the language
22148 doesn't allow it), and break the loop here. */
22149 name = dwarf2_name (die, cu);
22150 parent_name = dwarf2_name (parent, cu);
b98664d3 22151 complaint (_("template param type '%s' defined within parent '%s'"),
98bfdba5
PA
22152 name ? name : "<unknown>",
22153 parent_name ? parent_name : "<unknown>");
22154 return "";
22155 }
63d06c5c 22156 else
0114d602
DJ
22157 switch (parent->tag)
22158 {
63d06c5c 22159 case DW_TAG_namespace:
0114d602 22160 parent_type = read_type_die (parent, cu);
acebe513
UW
22161 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
22162 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
22163 Work around this problem here. */
22164 if (cu->language == language_cplus
e86ca25f 22165 && strcmp (TYPE_NAME (parent_type), "::") == 0)
acebe513 22166 return "";
0114d602 22167 /* We give a name to even anonymous namespaces. */
e86ca25f 22168 return TYPE_NAME (parent_type);
63d06c5c 22169 case DW_TAG_class_type:
680b30c7 22170 case DW_TAG_interface_type:
63d06c5c 22171 case DW_TAG_structure_type:
0114d602 22172 case DW_TAG_union_type:
f55ee35c 22173 case DW_TAG_module:
0114d602 22174 parent_type = read_type_die (parent, cu);
e86ca25f
TT
22175 if (TYPE_NAME (parent_type) != NULL)
22176 return TYPE_NAME (parent_type);
0114d602
DJ
22177 else
22178 /* An anonymous structure is only allowed non-static data
22179 members; no typedefs, no member functions, et cetera.
22180 So it does not need a prefix. */
22181 return "";
abc72ce4 22182 case DW_TAG_compile_unit:
95554aad 22183 case DW_TAG_partial_unit:
abc72ce4
DE
22184 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
22185 if (cu->language == language_cplus
8b70b953 22186 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
abc72ce4
DE
22187 && die->child != NULL
22188 && (die->tag == DW_TAG_class_type
22189 || die->tag == DW_TAG_structure_type
22190 || die->tag == DW_TAG_union_type))
22191 {
22192 char *name = guess_full_die_structure_name (die, cu);
22193 if (name != NULL)
22194 return name;
22195 }
22196 return "";
3d567982
TT
22197 case DW_TAG_enumeration_type:
22198 parent_type = read_type_die (parent, cu);
22199 if (TYPE_DECLARED_CLASS (parent_type))
22200 {
e86ca25f
TT
22201 if (TYPE_NAME (parent_type) != NULL)
22202 return TYPE_NAME (parent_type);
3d567982
TT
22203 return "";
22204 }
22205 /* Fall through. */
63d06c5c 22206 default:
8176b9b8 22207 return determine_prefix (parent, cu);
63d06c5c 22208 }
63d06c5c
DC
22209}
22210
3e43a32a
MS
22211/* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
22212 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
22213 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
22214 an obconcat, otherwise allocate storage for the result. The CU argument is
22215 used to determine the language and hence, the appropriate separator. */
987504bb 22216
f55ee35c 22217#define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
63d06c5c
DC
22218
22219static char *
f55ee35c
JK
22220typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
22221 int physname, struct dwarf2_cu *cu)
63d06c5c 22222{
f55ee35c 22223 const char *lead = "";
5c315b68 22224 const char *sep;
63d06c5c 22225
3e43a32a
MS
22226 if (suffix == NULL || suffix[0] == '\0'
22227 || prefix == NULL || prefix[0] == '\0')
987504bb 22228 sep = "";
45280282
IB
22229 else if (cu->language == language_d)
22230 {
22231 /* For D, the 'main' function could be defined in any module, but it
22232 should never be prefixed. */
22233 if (strcmp (suffix, "D main") == 0)
22234 {
22235 prefix = "";
22236 sep = "";
22237 }
22238 else
22239 sep = ".";
22240 }
f55ee35c
JK
22241 else if (cu->language == language_fortran && physname)
22242 {
22243 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
22244 DW_AT_MIPS_linkage_name is preferred and used instead. */
22245
22246 lead = "__";
22247 sep = "_MOD_";
22248 }
987504bb
JJ
22249 else
22250 sep = "::";
63d06c5c 22251
6dd47d34
DE
22252 if (prefix == NULL)
22253 prefix = "";
22254 if (suffix == NULL)
22255 suffix = "";
22256
987504bb
JJ
22257 if (obs == NULL)
22258 {
3e43a32a 22259 char *retval
224c3ddb
SM
22260 = ((char *)
22261 xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1));
9a619af0 22262
f55ee35c
JK
22263 strcpy (retval, lead);
22264 strcat (retval, prefix);
6dd47d34
DE
22265 strcat (retval, sep);
22266 strcat (retval, suffix);
63d06c5c
DC
22267 return retval;
22268 }
987504bb
JJ
22269 else
22270 {
22271 /* We have an obstack. */
f55ee35c 22272 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
987504bb 22273 }
63d06c5c
DC
22274}
22275
c906108c
SS
22276/* Return sibling of die, NULL if no sibling. */
22277
f9aca02d 22278static struct die_info *
fba45db2 22279sibling_die (struct die_info *die)
c906108c 22280{
639d11d3 22281 return die->sibling;
c906108c
SS
22282}
22283
71c25dea
TT
22284/* Get name of a die, return NULL if not found. */
22285
15d034d0
TT
22286static const char *
22287dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
71c25dea
TT
22288 struct obstack *obstack)
22289{
22290 if (name && cu->language == language_cplus)
22291 {
2f408ecb 22292 std::string canon_name = cp_canonicalize_string (name);
71c25dea 22293
2f408ecb 22294 if (!canon_name.empty ())
71c25dea 22295 {
2f408ecb
PA
22296 if (canon_name != name)
22297 name = (const char *) obstack_copy0 (obstack,
22298 canon_name.c_str (),
22299 canon_name.length ());
71c25dea
TT
22300 }
22301 }
22302
22303 return name;
c906108c
SS
22304}
22305
96553a0c
DE
22306/* Get name of a die, return NULL if not found.
22307 Anonymous namespaces are converted to their magic string. */
9219021c 22308
15d034d0 22309static const char *
e142c38c 22310dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
9219021c
DC
22311{
22312 struct attribute *attr;
518817b3 22313 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
9219021c 22314
e142c38c 22315 attr = dwarf2_attr (die, DW_AT_name, cu);
53832f31 22316 if ((!attr || !DW_STRING (attr))
96553a0c 22317 && die->tag != DW_TAG_namespace
53832f31
TT
22318 && die->tag != DW_TAG_class_type
22319 && die->tag != DW_TAG_interface_type
22320 && die->tag != DW_TAG_structure_type
22321 && die->tag != DW_TAG_union_type)
71c25dea
TT
22322 return NULL;
22323
22324 switch (die->tag)
22325 {
22326 case DW_TAG_compile_unit:
95554aad 22327 case DW_TAG_partial_unit:
71c25dea
TT
22328 /* Compilation units have a DW_AT_name that is a filename, not
22329 a source language identifier. */
22330 case DW_TAG_enumeration_type:
22331 case DW_TAG_enumerator:
22332 /* These tags always have simple identifiers already; no need
22333 to canonicalize them. */
22334 return DW_STRING (attr);
907af001 22335
96553a0c
DE
22336 case DW_TAG_namespace:
22337 if (attr != NULL && DW_STRING (attr) != NULL)
22338 return DW_STRING (attr);
22339 return CP_ANONYMOUS_NAMESPACE_STR;
22340
907af001
UW
22341 case DW_TAG_class_type:
22342 case DW_TAG_interface_type:
22343 case DW_TAG_structure_type:
22344 case DW_TAG_union_type:
22345 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
22346 structures or unions. These were of the form "._%d" in GCC 4.1,
22347 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
22348 and GCC 4.4. We work around this problem by ignoring these. */
53832f31 22349 if (attr && DW_STRING (attr)
61012eef
GB
22350 && (startswith (DW_STRING (attr), "._")
22351 || startswith (DW_STRING (attr), "<anonymous")))
907af001 22352 return NULL;
53832f31
TT
22353
22354 /* GCC might emit a nameless typedef that has a linkage name. See
22355 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
22356 if (!attr || DW_STRING (attr) == NULL)
22357 {
df5c6c50 22358 char *demangled = NULL;
53832f31 22359
73b9be8b 22360 attr = dw2_linkage_name_attr (die, cu);
53832f31
TT
22361 if (attr == NULL || DW_STRING (attr) == NULL)
22362 return NULL;
22363
df5c6c50
JK
22364 /* Avoid demangling DW_STRING (attr) the second time on a second
22365 call for the same DIE. */
22366 if (!DW_STRING_IS_CANONICAL (attr))
8de20a37 22367 demangled = gdb_demangle (DW_STRING (attr), DMGL_TYPES);
53832f31
TT
22368
22369 if (demangled)
22370 {
e6a959d6 22371 const char *base;
96408a79 22372
53832f31 22373 /* FIXME: we already did this for the partial symbol... */
34a68019 22374 DW_STRING (attr)
224c3ddb 22375 = ((const char *)
e3b94546 22376 obstack_copy0 (&objfile->per_bfd->storage_obstack,
224c3ddb 22377 demangled, strlen (demangled)));
53832f31
TT
22378 DW_STRING_IS_CANONICAL (attr) = 1;
22379 xfree (demangled);
96408a79
SA
22380
22381 /* Strip any leading namespaces/classes, keep only the base name.
22382 DW_AT_name for named DIEs does not contain the prefixes. */
22383 base = strrchr (DW_STRING (attr), ':');
22384 if (base && base > DW_STRING (attr) && base[-1] == ':')
22385 return &base[1];
22386 else
22387 return DW_STRING (attr);
53832f31
TT
22388 }
22389 }
907af001
UW
22390 break;
22391
71c25dea 22392 default:
907af001
UW
22393 break;
22394 }
22395
22396 if (!DW_STRING_IS_CANONICAL (attr))
22397 {
22398 DW_STRING (attr)
22399 = dwarf2_canonicalize_name (DW_STRING (attr), cu,
e3b94546 22400 &objfile->per_bfd->storage_obstack);
907af001 22401 DW_STRING_IS_CANONICAL (attr) = 1;
71c25dea 22402 }
907af001 22403 return DW_STRING (attr);
9219021c
DC
22404}
22405
22406/* Return the die that this die in an extension of, or NULL if there
f2f0e013
DJ
22407 is none. *EXT_CU is the CU containing DIE on input, and the CU
22408 containing the return value on output. */
9219021c
DC
22409
22410static struct die_info *
f2f0e013 22411dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
9219021c
DC
22412{
22413 struct attribute *attr;
9219021c 22414
f2f0e013 22415 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
9219021c
DC
22416 if (attr == NULL)
22417 return NULL;
22418
f2f0e013 22419 return follow_die_ref (die, attr, ext_cu);
9219021c
DC
22420}
22421
c906108c
SS
22422/* Convert a DIE tag into its string name. */
22423
f39c6ffd 22424static const char *
aa1ee363 22425dwarf_tag_name (unsigned tag)
c906108c 22426{
f39c6ffd
TT
22427 const char *name = get_DW_TAG_name (tag);
22428
22429 if (name == NULL)
22430 return "DW_TAG_<unknown>";
22431
22432 return name;
c906108c
SS
22433}
22434
22435/* Convert a DWARF attribute code into its string name. */
22436
f39c6ffd 22437static const char *
aa1ee363 22438dwarf_attr_name (unsigned attr)
c906108c 22439{
f39c6ffd
TT
22440 const char *name;
22441
c764a876 22442#ifdef MIPS /* collides with DW_AT_HP_block_index */
f39c6ffd
TT
22443 if (attr == DW_AT_MIPS_fde)
22444 return "DW_AT_MIPS_fde";
22445#else
22446 if (attr == DW_AT_HP_block_index)
22447 return "DW_AT_HP_block_index";
c764a876 22448#endif
f39c6ffd
TT
22449
22450 name = get_DW_AT_name (attr);
22451
22452 if (name == NULL)
22453 return "DW_AT_<unknown>";
22454
22455 return name;
c906108c
SS
22456}
22457
22458/* Convert a DWARF value form code into its string name. */
22459
f39c6ffd 22460static const char *
aa1ee363 22461dwarf_form_name (unsigned form)
c906108c 22462{
f39c6ffd
TT
22463 const char *name = get_DW_FORM_name (form);
22464
22465 if (name == NULL)
22466 return "DW_FORM_<unknown>";
22467
22468 return name;
c906108c
SS
22469}
22470
a121b7c1 22471static const char *
fba45db2 22472dwarf_bool_name (unsigned mybool)
c906108c
SS
22473{
22474 if (mybool)
22475 return "TRUE";
22476 else
22477 return "FALSE";
22478}
22479
22480/* Convert a DWARF type code into its string name. */
22481
f39c6ffd 22482static const char *
aa1ee363 22483dwarf_type_encoding_name (unsigned enc)
c906108c 22484{
f39c6ffd 22485 const char *name = get_DW_ATE_name (enc);
c906108c 22486
f39c6ffd
TT
22487 if (name == NULL)
22488 return "DW_ATE_<unknown>";
c906108c 22489
f39c6ffd 22490 return name;
c906108c 22491}
c906108c 22492
f9aca02d 22493static void
d97bc12b 22494dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
c906108c
SS
22495{
22496 unsigned int i;
22497
d97bc12b 22498 print_spaces (indent, f);
9d8780f0 22499 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset %s)\n",
9c541725 22500 dwarf_tag_name (die->tag), die->abbrev,
9d8780f0 22501 sect_offset_str (die->sect_off));
d97bc12b
DE
22502
22503 if (die->parent != NULL)
22504 {
22505 print_spaces (indent, f);
9d8780f0
SM
22506 fprintf_unfiltered (f, " parent at offset: %s\n",
22507 sect_offset_str (die->parent->sect_off));
d97bc12b
DE
22508 }
22509
22510 print_spaces (indent, f);
22511 fprintf_unfiltered (f, " has children: %s\n",
639d11d3 22512 dwarf_bool_name (die->child != NULL));
c906108c 22513
d97bc12b
DE
22514 print_spaces (indent, f);
22515 fprintf_unfiltered (f, " attributes:\n");
22516
c906108c
SS
22517 for (i = 0; i < die->num_attrs; ++i)
22518 {
d97bc12b
DE
22519 print_spaces (indent, f);
22520 fprintf_unfiltered (f, " %s (%s) ",
c906108c
SS
22521 dwarf_attr_name (die->attrs[i].name),
22522 dwarf_form_name (die->attrs[i].form));
d97bc12b 22523
c906108c
SS
22524 switch (die->attrs[i].form)
22525 {
c906108c 22526 case DW_FORM_addr:
3019eac3 22527 case DW_FORM_GNU_addr_index:
d97bc12b 22528 fprintf_unfiltered (f, "address: ");
5af949e3 22529 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
c906108c
SS
22530 break;
22531 case DW_FORM_block2:
22532 case DW_FORM_block4:
22533 case DW_FORM_block:
22534 case DW_FORM_block1:
56eb65bd
SP
22535 fprintf_unfiltered (f, "block: size %s",
22536 pulongest (DW_BLOCK (&die->attrs[i])->size));
c906108c 22537 break;
2dc7f7b3 22538 case DW_FORM_exprloc:
56eb65bd
SP
22539 fprintf_unfiltered (f, "expression: size %s",
22540 pulongest (DW_BLOCK (&die->attrs[i])->size));
2dc7f7b3 22541 break;
0224619f
JK
22542 case DW_FORM_data16:
22543 fprintf_unfiltered (f, "constant of 16 bytes");
22544 break;
4568ecf9
DE
22545 case DW_FORM_ref_addr:
22546 fprintf_unfiltered (f, "ref address: ");
22547 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
22548 break;
36586728
TT
22549 case DW_FORM_GNU_ref_alt:
22550 fprintf_unfiltered (f, "alt ref address: ");
22551 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
22552 break;
10b3939b
DJ
22553 case DW_FORM_ref1:
22554 case DW_FORM_ref2:
22555 case DW_FORM_ref4:
4568ecf9
DE
22556 case DW_FORM_ref8:
22557 case DW_FORM_ref_udata:
d97bc12b 22558 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
4568ecf9 22559 (long) (DW_UNSND (&die->attrs[i])));
10b3939b 22560 break;
c906108c
SS
22561 case DW_FORM_data1:
22562 case DW_FORM_data2:
22563 case DW_FORM_data4:
ce5d95e1 22564 case DW_FORM_data8:
c906108c
SS
22565 case DW_FORM_udata:
22566 case DW_FORM_sdata:
43bbcdc2
PH
22567 fprintf_unfiltered (f, "constant: %s",
22568 pulongest (DW_UNSND (&die->attrs[i])));
c906108c 22569 break;
2dc7f7b3
TT
22570 case DW_FORM_sec_offset:
22571 fprintf_unfiltered (f, "section offset: %s",
22572 pulongest (DW_UNSND (&die->attrs[i])));
22573 break;
55f1336d 22574 case DW_FORM_ref_sig8:
ac9ec31b
DE
22575 fprintf_unfiltered (f, "signature: %s",
22576 hex_string (DW_SIGNATURE (&die->attrs[i])));
348e048f 22577 break;
c906108c 22578 case DW_FORM_string:
4bdf3d34 22579 case DW_FORM_strp:
43988095 22580 case DW_FORM_line_strp:
3019eac3 22581 case DW_FORM_GNU_str_index:
36586728 22582 case DW_FORM_GNU_strp_alt:
8285870a 22583 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
c906108c 22584 DW_STRING (&die->attrs[i])
8285870a
JK
22585 ? DW_STRING (&die->attrs[i]) : "",
22586 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
c906108c
SS
22587 break;
22588 case DW_FORM_flag:
22589 if (DW_UNSND (&die->attrs[i]))
d97bc12b 22590 fprintf_unfiltered (f, "flag: TRUE");
c906108c 22591 else
d97bc12b 22592 fprintf_unfiltered (f, "flag: FALSE");
c906108c 22593 break;
2dc7f7b3
TT
22594 case DW_FORM_flag_present:
22595 fprintf_unfiltered (f, "flag: TRUE");
22596 break;
a8329558 22597 case DW_FORM_indirect:
0963b4bd
MS
22598 /* The reader will have reduced the indirect form to
22599 the "base form" so this form should not occur. */
3e43a32a
MS
22600 fprintf_unfiltered (f,
22601 "unexpected attribute form: DW_FORM_indirect");
a8329558 22602 break;
663c44ac
JK
22603 case DW_FORM_implicit_const:
22604 fprintf_unfiltered (f, "constant: %s",
22605 plongest (DW_SND (&die->attrs[i])));
22606 break;
c906108c 22607 default:
d97bc12b 22608 fprintf_unfiltered (f, "unsupported attribute form: %d.",
c5aa993b 22609 die->attrs[i].form);
d97bc12b 22610 break;
c906108c 22611 }
d97bc12b 22612 fprintf_unfiltered (f, "\n");
c906108c
SS
22613 }
22614}
22615
f9aca02d 22616static void
d97bc12b 22617dump_die_for_error (struct die_info *die)
c906108c 22618{
d97bc12b
DE
22619 dump_die_shallow (gdb_stderr, 0, die);
22620}
22621
22622static void
22623dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
22624{
22625 int indent = level * 4;
22626
22627 gdb_assert (die != NULL);
22628
22629 if (level >= max_level)
22630 return;
22631
22632 dump_die_shallow (f, indent, die);
22633
22634 if (die->child != NULL)
c906108c 22635 {
d97bc12b
DE
22636 print_spaces (indent, f);
22637 fprintf_unfiltered (f, " Children:");
22638 if (level + 1 < max_level)
22639 {
22640 fprintf_unfiltered (f, "\n");
22641 dump_die_1 (f, level + 1, max_level, die->child);
22642 }
22643 else
22644 {
3e43a32a
MS
22645 fprintf_unfiltered (f,
22646 " [not printed, max nesting level reached]\n");
d97bc12b
DE
22647 }
22648 }
22649
22650 if (die->sibling != NULL && level > 0)
22651 {
22652 dump_die_1 (f, level, max_level, die->sibling);
c906108c
SS
22653 }
22654}
22655
d97bc12b
DE
22656/* This is called from the pdie macro in gdbinit.in.
22657 It's not static so gcc will keep a copy callable from gdb. */
22658
22659void
22660dump_die (struct die_info *die, int max_level)
22661{
22662 dump_die_1 (gdb_stdlog, 0, max_level, die);
22663}
22664
f9aca02d 22665static void
51545339 22666store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
c906108c 22667{
51545339 22668 void **slot;
c906108c 22669
9c541725
PA
22670 slot = htab_find_slot_with_hash (cu->die_hash, die,
22671 to_underlying (die->sect_off),
b64f50a1 22672 INSERT);
51545339
DJ
22673
22674 *slot = die;
c906108c
SS
22675}
22676
b64f50a1
JK
22677/* Return DIE offset of ATTR. Return 0 with complaint if ATTR is not of the
22678 required kind. */
22679
22680static sect_offset
ff39bb5e 22681dwarf2_get_ref_die_offset (const struct attribute *attr)
93311388 22682{
7771576e 22683 if (attr_form_is_ref (attr))
9c541725 22684 return (sect_offset) DW_UNSND (attr);
93311388 22685
b98664d3 22686 complaint (_("unsupported die ref attribute form: '%s'"),
93311388 22687 dwarf_form_name (attr->form));
9c541725 22688 return {};
c906108c
SS
22689}
22690
43bbcdc2
PH
22691/* Return the constant value held by ATTR. Return DEFAULT_VALUE if
22692 * the value held by the attribute is not constant. */
a02abb62 22693
43bbcdc2 22694static LONGEST
ff39bb5e 22695dwarf2_get_attr_constant_value (const struct attribute *attr, int default_value)
a02abb62 22696{
663c44ac 22697 if (attr->form == DW_FORM_sdata || attr->form == DW_FORM_implicit_const)
a02abb62
JB
22698 return DW_SND (attr);
22699 else if (attr->form == DW_FORM_udata
22700 || attr->form == DW_FORM_data1
22701 || attr->form == DW_FORM_data2
22702 || attr->form == DW_FORM_data4
22703 || attr->form == DW_FORM_data8)
22704 return DW_UNSND (attr);
22705 else
22706 {
0224619f 22707 /* For DW_FORM_data16 see attr_form_is_constant. */
b98664d3 22708 complaint (_("Attribute value is not a constant (%s)"),
a02abb62
JB
22709 dwarf_form_name (attr->form));
22710 return default_value;
22711 }
22712}
22713
348e048f
DE
22714/* Follow reference or signature attribute ATTR of SRC_DIE.
22715 On entry *REF_CU is the CU of SRC_DIE.
22716 On exit *REF_CU is the CU of the result. */
22717
22718static struct die_info *
ff39bb5e 22719follow_die_ref_or_sig (struct die_info *src_die, const struct attribute *attr,
348e048f
DE
22720 struct dwarf2_cu **ref_cu)
22721{
22722 struct die_info *die;
22723
7771576e 22724 if (attr_form_is_ref (attr))
348e048f 22725 die = follow_die_ref (src_die, attr, ref_cu);
55f1336d 22726 else if (attr->form == DW_FORM_ref_sig8)
348e048f
DE
22727 die = follow_die_sig (src_die, attr, ref_cu);
22728 else
22729 {
22730 dump_die_for_error (src_die);
22731 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
518817b3 22732 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
348e048f
DE
22733 }
22734
22735 return die;
03dd20cc
DJ
22736}
22737
5c631832 22738/* Follow reference OFFSET.
673bfd45
DE
22739 On entry *REF_CU is the CU of the source die referencing OFFSET.
22740 On exit *REF_CU is the CU of the result.
22741 Returns NULL if OFFSET is invalid. */
f504f079 22742
f9aca02d 22743static struct die_info *
9c541725 22744follow_die_offset (sect_offset sect_off, int offset_in_dwz,
36586728 22745 struct dwarf2_cu **ref_cu)
c906108c 22746{
10b3939b 22747 struct die_info temp_die;
f2f0e013 22748 struct dwarf2_cu *target_cu, *cu = *ref_cu;
518817b3
SM
22749 struct dwarf2_per_objfile *dwarf2_per_objfile
22750 = cu->per_cu->dwarf2_per_objfile;
10b3939b 22751
348e048f
DE
22752 gdb_assert (cu->per_cu != NULL);
22753
98bfdba5
PA
22754 target_cu = cu;
22755
3019eac3 22756 if (cu->per_cu->is_debug_types)
348e048f
DE
22757 {
22758 /* .debug_types CUs cannot reference anything outside their CU.
22759 If they need to, they have to reference a signatured type via
55f1336d 22760 DW_FORM_ref_sig8. */
9c541725 22761 if (!offset_in_cu_p (&cu->header, sect_off))
5c631832 22762 return NULL;
348e048f 22763 }
36586728 22764 else if (offset_in_dwz != cu->per_cu->is_dwz
9c541725 22765 || !offset_in_cu_p (&cu->header, sect_off))
10b3939b
DJ
22766 {
22767 struct dwarf2_per_cu_data *per_cu;
9a619af0 22768
9c541725 22769 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
ed2dc618 22770 dwarf2_per_objfile);
03dd20cc
DJ
22771
22772 /* If necessary, add it to the queue and load its DIEs. */
95554aad 22773 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
58f0c718 22774 load_full_comp_unit (per_cu, false, cu->language);
03dd20cc 22775
10b3939b
DJ
22776 target_cu = per_cu->cu;
22777 }
98bfdba5
PA
22778 else if (cu->dies == NULL)
22779 {
22780 /* We're loading full DIEs during partial symbol reading. */
22781 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
58f0c718 22782 load_full_comp_unit (cu->per_cu, false, language_minimal);
98bfdba5 22783 }
c906108c 22784
f2f0e013 22785 *ref_cu = target_cu;
9c541725 22786 temp_die.sect_off = sect_off;
9a3c8263 22787 return (struct die_info *) htab_find_with_hash (target_cu->die_hash,
9c541725
PA
22788 &temp_die,
22789 to_underlying (sect_off));
5c631832 22790}
10b3939b 22791
5c631832
JK
22792/* Follow reference attribute ATTR of SRC_DIE.
22793 On entry *REF_CU is the CU of SRC_DIE.
22794 On exit *REF_CU is the CU of the result. */
22795
22796static struct die_info *
ff39bb5e 22797follow_die_ref (struct die_info *src_die, const struct attribute *attr,
5c631832
JK
22798 struct dwarf2_cu **ref_cu)
22799{
9c541725 22800 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
5c631832
JK
22801 struct dwarf2_cu *cu = *ref_cu;
22802 struct die_info *die;
22803
9c541725 22804 die = follow_die_offset (sect_off,
36586728
TT
22805 (attr->form == DW_FORM_GNU_ref_alt
22806 || cu->per_cu->is_dwz),
22807 ref_cu);
5c631832 22808 if (!die)
9d8780f0
SM
22809 error (_("Dwarf Error: Cannot find DIE at %s referenced from DIE "
22810 "at %s [in module %s]"),
22811 sect_offset_str (sect_off), sect_offset_str (src_die->sect_off),
518817b3 22812 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
348e048f 22813
5c631832
JK
22814 return die;
22815}
22816
9c541725 22817/* Return DWARF block referenced by DW_AT_location of DIE at SECT_OFF at PER_CU.
d83e736b 22818 Returned value is intended for DW_OP_call*. Returned
e3b94546
SM
22819 dwarf2_locexpr_baton->data has lifetime of
22820 PER_CU->DWARF2_PER_OBJFILE->OBJFILE. */
5c631832
JK
22821
22822struct dwarf2_locexpr_baton
9c541725 22823dwarf2_fetch_die_loc_sect_off (sect_offset sect_off,
8b9737bf
TT
22824 struct dwarf2_per_cu_data *per_cu,
22825 CORE_ADDR (*get_frame_pc) (void *baton),
22826 void *baton)
5c631832 22827{
918dd910 22828 struct dwarf2_cu *cu;
5c631832
JK
22829 struct die_info *die;
22830 struct attribute *attr;
22831 struct dwarf2_locexpr_baton retval;
12359b5e
SM
22832 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
22833 struct objfile *objfile = dwarf2_per_objfile->objfile;
8cf6f0b1 22834
918dd910 22835 if (per_cu->cu == NULL)
58f0c718 22836 load_cu (per_cu, false);
918dd910 22837 cu = per_cu->cu;
cc12ce38
DE
22838 if (cu == NULL)
22839 {
22840 /* We shouldn't get here for a dummy CU, but don't crash on the user.
22841 Instead just throw an error, not much else we can do. */
9d8780f0
SM
22842 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
22843 sect_offset_str (sect_off), objfile_name (objfile));
cc12ce38 22844 }
918dd910 22845
9c541725 22846 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
5c631832 22847 if (!die)
9d8780f0
SM
22848 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
22849 sect_offset_str (sect_off), objfile_name (objfile));
5c631832
JK
22850
22851 attr = dwarf2_attr (die, DW_AT_location, cu);
22852 if (!attr)
22853 {
e103e986
JK
22854 /* DWARF: "If there is no such attribute, then there is no effect.".
22855 DATA is ignored if SIZE is 0. */
5c631832 22856
e103e986 22857 retval.data = NULL;
5c631832
JK
22858 retval.size = 0;
22859 }
8cf6f0b1
TT
22860 else if (attr_form_is_section_offset (attr))
22861 {
22862 struct dwarf2_loclist_baton loclist_baton;
22863 CORE_ADDR pc = (*get_frame_pc) (baton);
22864 size_t size;
22865
22866 fill_in_loclist_baton (cu, &loclist_baton, attr);
22867
22868 retval.data = dwarf2_find_location_expression (&loclist_baton,
22869 &size, pc);
22870 retval.size = size;
22871 }
5c631832
JK
22872 else
22873 {
22874 if (!attr_form_is_block (attr))
9d8780f0 22875 error (_("Dwarf Error: DIE at %s referenced in module %s "
5c631832 22876 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
9d8780f0 22877 sect_offset_str (sect_off), objfile_name (objfile));
5c631832
JK
22878
22879 retval.data = DW_BLOCK (attr)->data;
22880 retval.size = DW_BLOCK (attr)->size;
22881 }
22882 retval.per_cu = cu->per_cu;
918dd910 22883
ed2dc618 22884 age_cached_comp_units (dwarf2_per_objfile);
918dd910 22885
5c631832 22886 return retval;
348e048f
DE
22887}
22888
8b9737bf
TT
22889/* Like dwarf2_fetch_die_loc_sect_off, but take a CU
22890 offset. */
22891
22892struct dwarf2_locexpr_baton
22893dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
22894 struct dwarf2_per_cu_data *per_cu,
22895 CORE_ADDR (*get_frame_pc) (void *baton),
22896 void *baton)
22897{
9c541725 22898 sect_offset sect_off = per_cu->sect_off + to_underlying (offset_in_cu);
8b9737bf 22899
9c541725 22900 return dwarf2_fetch_die_loc_sect_off (sect_off, per_cu, get_frame_pc, baton);
8b9737bf
TT
22901}
22902
b6807d98
TT
22903/* Write a constant of a given type as target-ordered bytes into
22904 OBSTACK. */
22905
22906static const gdb_byte *
22907write_constant_as_bytes (struct obstack *obstack,
22908 enum bfd_endian byte_order,
22909 struct type *type,
22910 ULONGEST value,
22911 LONGEST *len)
22912{
22913 gdb_byte *result;
22914
22915 *len = TYPE_LENGTH (type);
224c3ddb 22916 result = (gdb_byte *) obstack_alloc (obstack, *len);
b6807d98
TT
22917 store_unsigned_integer (result, *len, byte_order, value);
22918
22919 return result;
22920}
22921
22922/* If the DIE at OFFSET in PER_CU has a DW_AT_const_value, return a
22923 pointer to the constant bytes and set LEN to the length of the
22924 data. If memory is needed, allocate it on OBSTACK. If the DIE
22925 does not have a DW_AT_const_value, return NULL. */
22926
22927const gdb_byte *
9c541725 22928dwarf2_fetch_constant_bytes (sect_offset sect_off,
b6807d98
TT
22929 struct dwarf2_per_cu_data *per_cu,
22930 struct obstack *obstack,
22931 LONGEST *len)
22932{
22933 struct dwarf2_cu *cu;
22934 struct die_info *die;
22935 struct attribute *attr;
22936 const gdb_byte *result = NULL;
22937 struct type *type;
22938 LONGEST value;
22939 enum bfd_endian byte_order;
e3b94546 22940 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
b6807d98 22941
b6807d98 22942 if (per_cu->cu == NULL)
58f0c718 22943 load_cu (per_cu, false);
b6807d98 22944 cu = per_cu->cu;
cc12ce38
DE
22945 if (cu == NULL)
22946 {
22947 /* We shouldn't get here for a dummy CU, but don't crash on the user.
22948 Instead just throw an error, not much else we can do. */
9d8780f0
SM
22949 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
22950 sect_offset_str (sect_off), objfile_name (objfile));
cc12ce38 22951 }
b6807d98 22952
9c541725 22953 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
b6807d98 22954 if (!die)
9d8780f0
SM
22955 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
22956 sect_offset_str (sect_off), objfile_name (objfile));
b6807d98
TT
22957
22958 attr = dwarf2_attr (die, DW_AT_const_value, cu);
22959 if (attr == NULL)
22960 return NULL;
22961
e3b94546 22962 byte_order = (bfd_big_endian (objfile->obfd)
b6807d98
TT
22963 ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
22964
22965 switch (attr->form)
22966 {
22967 case DW_FORM_addr:
22968 case DW_FORM_GNU_addr_index:
22969 {
22970 gdb_byte *tem;
22971
22972 *len = cu->header.addr_size;
224c3ddb 22973 tem = (gdb_byte *) obstack_alloc (obstack, *len);
b6807d98
TT
22974 store_unsigned_integer (tem, *len, byte_order, DW_ADDR (attr));
22975 result = tem;
22976 }
22977 break;
22978 case DW_FORM_string:
22979 case DW_FORM_strp:
22980 case DW_FORM_GNU_str_index:
22981 case DW_FORM_GNU_strp_alt:
22982 /* DW_STRING is already allocated on the objfile obstack, point
22983 directly to it. */
22984 result = (const gdb_byte *) DW_STRING (attr);
22985 *len = strlen (DW_STRING (attr));
22986 break;
22987 case DW_FORM_block1:
22988 case DW_FORM_block2:
22989 case DW_FORM_block4:
22990 case DW_FORM_block:
22991 case DW_FORM_exprloc:
0224619f 22992 case DW_FORM_data16:
b6807d98
TT
22993 result = DW_BLOCK (attr)->data;
22994 *len = DW_BLOCK (attr)->size;
22995 break;
22996
22997 /* The DW_AT_const_value attributes are supposed to carry the
22998 symbol's value "represented as it would be on the target
22999 architecture." By the time we get here, it's already been
23000 converted to host endianness, so we just need to sign- or
23001 zero-extend it as appropriate. */
23002 case DW_FORM_data1:
23003 type = die_type (die, cu);
23004 result = dwarf2_const_value_data (attr, obstack, cu, &value, 8);
23005 if (result == NULL)
23006 result = write_constant_as_bytes (obstack, byte_order,
23007 type, value, len);
23008 break;
23009 case DW_FORM_data2:
23010 type = die_type (die, cu);
23011 result = dwarf2_const_value_data (attr, obstack, cu, &value, 16);
23012 if (result == NULL)
23013 result = write_constant_as_bytes (obstack, byte_order,
23014 type, value, len);
23015 break;
23016 case DW_FORM_data4:
23017 type = die_type (die, cu);
23018 result = dwarf2_const_value_data (attr, obstack, cu, &value, 32);
23019 if (result == NULL)
23020 result = write_constant_as_bytes (obstack, byte_order,
23021 type, value, len);
23022 break;
23023 case DW_FORM_data8:
23024 type = die_type (die, cu);
23025 result = dwarf2_const_value_data (attr, obstack, cu, &value, 64);
23026 if (result == NULL)
23027 result = write_constant_as_bytes (obstack, byte_order,
23028 type, value, len);
23029 break;
23030
23031 case DW_FORM_sdata:
663c44ac 23032 case DW_FORM_implicit_const:
b6807d98
TT
23033 type = die_type (die, cu);
23034 result = write_constant_as_bytes (obstack, byte_order,
23035 type, DW_SND (attr), len);
23036 break;
23037
23038 case DW_FORM_udata:
23039 type = die_type (die, cu);
23040 result = write_constant_as_bytes (obstack, byte_order,
23041 type, DW_UNSND (attr), len);
23042 break;
23043
23044 default:
b98664d3 23045 complaint (_("unsupported const value attribute form: '%s'"),
b6807d98
TT
23046 dwarf_form_name (attr->form));
23047 break;
23048 }
23049
23050 return result;
23051}
23052
7942e96e
AA
23053/* Return the type of the die at OFFSET in PER_CU. Return NULL if no
23054 valid type for this die is found. */
23055
23056struct type *
9c541725 23057dwarf2_fetch_die_type_sect_off (sect_offset sect_off,
7942e96e
AA
23058 struct dwarf2_per_cu_data *per_cu)
23059{
23060 struct dwarf2_cu *cu;
23061 struct die_info *die;
23062
7942e96e 23063 if (per_cu->cu == NULL)
58f0c718 23064 load_cu (per_cu, false);
7942e96e
AA
23065 cu = per_cu->cu;
23066 if (!cu)
23067 return NULL;
23068
9c541725 23069 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
7942e96e
AA
23070 if (!die)
23071 return NULL;
23072
23073 return die_type (die, cu);
23074}
23075
8a9b8146
TT
23076/* Return the type of the DIE at DIE_OFFSET in the CU named by
23077 PER_CU. */
23078
23079struct type *
b64f50a1 23080dwarf2_get_die_type (cu_offset die_offset,
8a9b8146
TT
23081 struct dwarf2_per_cu_data *per_cu)
23082{
9c541725 23083 sect_offset die_offset_sect = per_cu->sect_off + to_underlying (die_offset);
b64f50a1 23084 return get_die_type_at_offset (die_offset_sect, per_cu);
8a9b8146
TT
23085}
23086
ac9ec31b 23087/* Follow type unit SIG_TYPE referenced by SRC_DIE.
348e048f 23088 On entry *REF_CU is the CU of SRC_DIE.
ac9ec31b
DE
23089 On exit *REF_CU is the CU of the result.
23090 Returns NULL if the referenced DIE isn't found. */
348e048f
DE
23091
23092static struct die_info *
ac9ec31b
DE
23093follow_die_sig_1 (struct die_info *src_die, struct signatured_type *sig_type,
23094 struct dwarf2_cu **ref_cu)
348e048f 23095{
348e048f 23096 struct die_info temp_die;
348e048f
DE
23097 struct dwarf2_cu *sig_cu;
23098 struct die_info *die;
23099
ac9ec31b
DE
23100 /* While it might be nice to assert sig_type->type == NULL here,
23101 we can get here for DW_AT_imported_declaration where we need
23102 the DIE not the type. */
348e048f
DE
23103
23104 /* If necessary, add it to the queue and load its DIEs. */
23105
95554aad 23106 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, language_minimal))
a0f42c21 23107 read_signatured_type (sig_type);
348e048f 23108
348e048f 23109 sig_cu = sig_type->per_cu.cu;
69d751e3 23110 gdb_assert (sig_cu != NULL);
9c541725
PA
23111 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
23112 temp_die.sect_off = sig_type->type_offset_in_section;
9a3c8263 23113 die = (struct die_info *) htab_find_with_hash (sig_cu->die_hash, &temp_die,
9c541725 23114 to_underlying (temp_die.sect_off));
348e048f
DE
23115 if (die)
23116 {
ed2dc618 23117 struct dwarf2_per_objfile *dwarf2_per_objfile
518817b3 23118 = (*ref_cu)->per_cu->dwarf2_per_objfile;
ed2dc618 23119
796a7ff8
DE
23120 /* For .gdb_index version 7 keep track of included TUs.
23121 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
23122 if (dwarf2_per_objfile->index_table != NULL
23123 && dwarf2_per_objfile->index_table->version <= 7)
23124 {
23125 VEC_safe_push (dwarf2_per_cu_ptr,
23126 (*ref_cu)->per_cu->imported_symtabs,
23127 sig_cu->per_cu);
23128 }
23129
348e048f
DE
23130 *ref_cu = sig_cu;
23131 return die;
23132 }
23133
ac9ec31b
DE
23134 return NULL;
23135}
23136
23137/* Follow signatured type referenced by ATTR in SRC_DIE.
23138 On entry *REF_CU is the CU of SRC_DIE.
23139 On exit *REF_CU is the CU of the result.
23140 The result is the DIE of the type.
23141 If the referenced type cannot be found an error is thrown. */
23142
23143static struct die_info *
ff39bb5e 23144follow_die_sig (struct die_info *src_die, const struct attribute *attr,
ac9ec31b
DE
23145 struct dwarf2_cu **ref_cu)
23146{
23147 ULONGEST signature = DW_SIGNATURE (attr);
23148 struct signatured_type *sig_type;
23149 struct die_info *die;
23150
23151 gdb_assert (attr->form == DW_FORM_ref_sig8);
23152
a2ce51a0 23153 sig_type = lookup_signatured_type (*ref_cu, signature);
ac9ec31b
DE
23154 /* sig_type will be NULL if the signatured type is missing from
23155 the debug info. */
23156 if (sig_type == NULL)
23157 {
23158 error (_("Dwarf Error: Cannot find signatured DIE %s referenced"
9d8780f0
SM
23159 " from DIE at %s [in module %s]"),
23160 hex_string (signature), sect_offset_str (src_die->sect_off),
518817b3 23161 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
ac9ec31b
DE
23162 }
23163
23164 die = follow_die_sig_1 (src_die, sig_type, ref_cu);
23165 if (die == NULL)
23166 {
23167 dump_die_for_error (src_die);
23168 error (_("Dwarf Error: Problem reading signatured DIE %s referenced"
9d8780f0
SM
23169 " from DIE at %s [in module %s]"),
23170 hex_string (signature), sect_offset_str (src_die->sect_off),
518817b3 23171 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
ac9ec31b
DE
23172 }
23173
23174 return die;
23175}
23176
23177/* Get the type specified by SIGNATURE referenced in DIE/CU,
23178 reading in and processing the type unit if necessary. */
23179
23180static struct type *
23181get_signatured_type (struct die_info *die, ULONGEST signature,
23182 struct dwarf2_cu *cu)
23183{
518817b3
SM
23184 struct dwarf2_per_objfile *dwarf2_per_objfile
23185 = cu->per_cu->dwarf2_per_objfile;
ac9ec31b
DE
23186 struct signatured_type *sig_type;
23187 struct dwarf2_cu *type_cu;
23188 struct die_info *type_die;
23189 struct type *type;
23190
a2ce51a0 23191 sig_type = lookup_signatured_type (cu, signature);
ac9ec31b
DE
23192 /* sig_type will be NULL if the signatured type is missing from
23193 the debug info. */
23194 if (sig_type == NULL)
23195 {
b98664d3 23196 complaint (_("Dwarf Error: Cannot find signatured DIE %s referenced"
9d8780f0
SM
23197 " from DIE at %s [in module %s]"),
23198 hex_string (signature), sect_offset_str (die->sect_off),
4262abfb 23199 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
23200 return build_error_marker_type (cu, die);
23201 }
23202
23203 /* If we already know the type we're done. */
23204 if (sig_type->type != NULL)
23205 return sig_type->type;
23206
23207 type_cu = cu;
23208 type_die = follow_die_sig_1 (die, sig_type, &type_cu);
23209 if (type_die != NULL)
23210 {
23211 /* N.B. We need to call get_die_type to ensure only one type for this DIE
23212 is created. This is important, for example, because for c++ classes
23213 we need TYPE_NAME set which is only done by new_symbol. Blech. */
23214 type = read_type_die (type_die, type_cu);
23215 if (type == NULL)
23216 {
b98664d3 23217 complaint (_("Dwarf Error: Cannot build signatured type %s"
9d8780f0
SM
23218 " referenced from DIE at %s [in module %s]"),
23219 hex_string (signature), sect_offset_str (die->sect_off),
4262abfb 23220 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
23221 type = build_error_marker_type (cu, die);
23222 }
23223 }
23224 else
23225 {
b98664d3 23226 complaint (_("Dwarf Error: Problem reading 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 type = build_error_marker_type (cu, die);
23231 }
23232 sig_type->type = type;
23233
23234 return type;
23235}
23236
23237/* Get the type specified by the DW_AT_signature ATTR in DIE/CU,
23238 reading in and processing the type unit if necessary. */
23239
23240static struct type *
ff39bb5e 23241get_DW_AT_signature_type (struct die_info *die, const struct attribute *attr,
b385a60d 23242 struct dwarf2_cu *cu) /* ARI: editCase function */
ac9ec31b
DE
23243{
23244 /* Yes, DW_AT_signature can use a non-ref_sig8 reference. */
7771576e 23245 if (attr_form_is_ref (attr))
ac9ec31b
DE
23246 {
23247 struct dwarf2_cu *type_cu = cu;
23248 struct die_info *type_die = follow_die_ref (die, attr, &type_cu);
23249
23250 return read_type_die (type_die, type_cu);
23251 }
23252 else if (attr->form == DW_FORM_ref_sig8)
23253 {
23254 return get_signatured_type (die, DW_SIGNATURE (attr), cu);
23255 }
23256 else
23257 {
518817b3
SM
23258 struct dwarf2_per_objfile *dwarf2_per_objfile
23259 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 23260
b98664d3 23261 complaint (_("Dwarf Error: DW_AT_signature has bad form %s in DIE"
9d8780f0
SM
23262 " at %s [in module %s]"),
23263 dwarf_form_name (attr->form), sect_offset_str (die->sect_off),
4262abfb 23264 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
23265 return build_error_marker_type (cu, die);
23266 }
348e048f
DE
23267}
23268
e5fe5e75 23269/* Load the DIEs associated with type unit PER_CU into memory. */
348e048f
DE
23270
23271static void
e5fe5e75 23272load_full_type_unit (struct dwarf2_per_cu_data *per_cu)
348e048f 23273{
52dc124a 23274 struct signatured_type *sig_type;
348e048f 23275
f4dc4d17
DE
23276 /* Caller is responsible for ensuring type_unit_groups don't get here. */
23277 gdb_assert (! IS_TYPE_UNIT_GROUP (per_cu));
23278
6721b2ec
DE
23279 /* We have the per_cu, but we need the signatured_type.
23280 Fortunately this is an easy translation. */
23281 gdb_assert (per_cu->is_debug_types);
23282 sig_type = (struct signatured_type *) per_cu;
348e048f 23283
6721b2ec 23284 gdb_assert (per_cu->cu == NULL);
348e048f 23285
52dc124a 23286 read_signatured_type (sig_type);
348e048f 23287
6721b2ec 23288 gdb_assert (per_cu->cu != NULL);
348e048f
DE
23289}
23290
dee91e82
DE
23291/* die_reader_func for read_signatured_type.
23292 This is identical to load_full_comp_unit_reader,
23293 but is kept separate for now. */
348e048f
DE
23294
23295static void
dee91e82 23296read_signatured_type_reader (const struct die_reader_specs *reader,
d521ce57 23297 const gdb_byte *info_ptr,
dee91e82
DE
23298 struct die_info *comp_unit_die,
23299 int has_children,
23300 void *data)
348e048f 23301{
dee91e82 23302 struct dwarf2_cu *cu = reader->cu;
348e048f 23303
dee91e82
DE
23304 gdb_assert (cu->die_hash == NULL);
23305 cu->die_hash =
23306 htab_create_alloc_ex (cu->header.length / 12,
23307 die_hash,
23308 die_eq,
23309 NULL,
23310 &cu->comp_unit_obstack,
23311 hashtab_obstack_allocate,
23312 dummy_obstack_deallocate);
348e048f 23313
dee91e82
DE
23314 if (has_children)
23315 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
23316 &info_ptr, comp_unit_die);
23317 cu->dies = comp_unit_die;
23318 /* comp_unit_die is not stored in die_hash, no need. */
348e048f
DE
23319
23320 /* We try not to read any attributes in this function, because not
9cdd5dbd 23321 all CUs needed for references have been loaded yet, and symbol
348e048f 23322 table processing isn't initialized. But we have to set the CU language,
dee91e82
DE
23323 or we won't be able to build types correctly.
23324 Similarly, if we do not read the producer, we can not apply
23325 producer-specific interpretation. */
95554aad 23326 prepare_one_comp_unit (cu, cu->dies, language_minimal);
dee91e82 23327}
348e048f 23328
3019eac3
DE
23329/* Read in a signatured type and build its CU and DIEs.
23330 If the type is a stub for the real type in a DWO file,
23331 read in the real type from the DWO file as well. */
dee91e82
DE
23332
23333static void
23334read_signatured_type (struct signatured_type *sig_type)
23335{
23336 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
348e048f 23337
3019eac3 23338 gdb_assert (per_cu->is_debug_types);
dee91e82 23339 gdb_assert (per_cu->cu == NULL);
348e048f 23340
58f0c718 23341 init_cutu_and_read_dies (per_cu, NULL, 0, 1, false,
f4dc4d17 23342 read_signatured_type_reader, NULL);
7ee85ab1 23343 sig_type->per_cu.tu_read = 1;
c906108c
SS
23344}
23345
c906108c
SS
23346/* Decode simple location descriptions.
23347 Given a pointer to a dwarf block that defines a location, compute
23348 the location and return the value.
23349
4cecd739
DJ
23350 NOTE drow/2003-11-18: This function is called in two situations
23351 now: for the address of static or global variables (partial symbols
23352 only) and for offsets into structures which are expected to be
23353 (more or less) constant. The partial symbol case should go away,
23354 and only the constant case should remain. That will let this
23355 function complain more accurately. A few special modes are allowed
23356 without complaint for global variables (for instance, global
23357 register values and thread-local values).
c906108c
SS
23358
23359 A location description containing no operations indicates that the
4cecd739 23360 object is optimized out. The return value is 0 for that case.
6b992462
DJ
23361 FIXME drow/2003-11-16: No callers check for this case any more; soon all
23362 callers will only want a very basic result and this can become a
21ae7a4d
JK
23363 complaint.
23364
23365 Note that stack[0] is unused except as a default error return. */
c906108c
SS
23366
23367static CORE_ADDR
e7c27a73 23368decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
c906108c 23369{
518817b3 23370 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
56eb65bd
SP
23371 size_t i;
23372 size_t size = blk->size;
d521ce57 23373 const gdb_byte *data = blk->data;
21ae7a4d
JK
23374 CORE_ADDR stack[64];
23375 int stacki;
23376 unsigned int bytes_read, unsnd;
23377 gdb_byte op;
c906108c 23378
21ae7a4d
JK
23379 i = 0;
23380 stacki = 0;
23381 stack[stacki] = 0;
23382 stack[++stacki] = 0;
23383
23384 while (i < size)
23385 {
23386 op = data[i++];
23387 switch (op)
23388 {
23389 case DW_OP_lit0:
23390 case DW_OP_lit1:
23391 case DW_OP_lit2:
23392 case DW_OP_lit3:
23393 case DW_OP_lit4:
23394 case DW_OP_lit5:
23395 case DW_OP_lit6:
23396 case DW_OP_lit7:
23397 case DW_OP_lit8:
23398 case DW_OP_lit9:
23399 case DW_OP_lit10:
23400 case DW_OP_lit11:
23401 case DW_OP_lit12:
23402 case DW_OP_lit13:
23403 case DW_OP_lit14:
23404 case DW_OP_lit15:
23405 case DW_OP_lit16:
23406 case DW_OP_lit17:
23407 case DW_OP_lit18:
23408 case DW_OP_lit19:
23409 case DW_OP_lit20:
23410 case DW_OP_lit21:
23411 case DW_OP_lit22:
23412 case DW_OP_lit23:
23413 case DW_OP_lit24:
23414 case DW_OP_lit25:
23415 case DW_OP_lit26:
23416 case DW_OP_lit27:
23417 case DW_OP_lit28:
23418 case DW_OP_lit29:
23419 case DW_OP_lit30:
23420 case DW_OP_lit31:
23421 stack[++stacki] = op - DW_OP_lit0;
23422 break;
f1bea926 23423
21ae7a4d
JK
23424 case DW_OP_reg0:
23425 case DW_OP_reg1:
23426 case DW_OP_reg2:
23427 case DW_OP_reg3:
23428 case DW_OP_reg4:
23429 case DW_OP_reg5:
23430 case DW_OP_reg6:
23431 case DW_OP_reg7:
23432 case DW_OP_reg8:
23433 case DW_OP_reg9:
23434 case DW_OP_reg10:
23435 case DW_OP_reg11:
23436 case DW_OP_reg12:
23437 case DW_OP_reg13:
23438 case DW_OP_reg14:
23439 case DW_OP_reg15:
23440 case DW_OP_reg16:
23441 case DW_OP_reg17:
23442 case DW_OP_reg18:
23443 case DW_OP_reg19:
23444 case DW_OP_reg20:
23445 case DW_OP_reg21:
23446 case DW_OP_reg22:
23447 case DW_OP_reg23:
23448 case DW_OP_reg24:
23449 case DW_OP_reg25:
23450 case DW_OP_reg26:
23451 case DW_OP_reg27:
23452 case DW_OP_reg28:
23453 case DW_OP_reg29:
23454 case DW_OP_reg30:
23455 case DW_OP_reg31:
23456 stack[++stacki] = op - DW_OP_reg0;
23457 if (i < size)
23458 dwarf2_complex_location_expr_complaint ();
23459 break;
c906108c 23460
21ae7a4d
JK
23461 case DW_OP_regx:
23462 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
23463 i += bytes_read;
23464 stack[++stacki] = unsnd;
23465 if (i < size)
23466 dwarf2_complex_location_expr_complaint ();
23467 break;
c906108c 23468
21ae7a4d
JK
23469 case DW_OP_addr:
23470 stack[++stacki] = read_address (objfile->obfd, &data[i],
23471 cu, &bytes_read);
23472 i += bytes_read;
23473 break;
d53d4ac5 23474
21ae7a4d
JK
23475 case DW_OP_const1u:
23476 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
23477 i += 1;
23478 break;
23479
23480 case DW_OP_const1s:
23481 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
23482 i += 1;
23483 break;
23484
23485 case DW_OP_const2u:
23486 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
23487 i += 2;
23488 break;
23489
23490 case DW_OP_const2s:
23491 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
23492 i += 2;
23493 break;
d53d4ac5 23494
21ae7a4d
JK
23495 case DW_OP_const4u:
23496 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
23497 i += 4;
23498 break;
23499
23500 case DW_OP_const4s:
23501 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
23502 i += 4;
23503 break;
23504
585861ea
JK
23505 case DW_OP_const8u:
23506 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
23507 i += 8;
23508 break;
23509
21ae7a4d
JK
23510 case DW_OP_constu:
23511 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
23512 &bytes_read);
23513 i += bytes_read;
23514 break;
23515
23516 case DW_OP_consts:
23517 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
23518 i += bytes_read;
23519 break;
23520
23521 case DW_OP_dup:
23522 stack[stacki + 1] = stack[stacki];
23523 stacki++;
23524 break;
23525
23526 case DW_OP_plus:
23527 stack[stacki - 1] += stack[stacki];
23528 stacki--;
23529 break;
23530
23531 case DW_OP_plus_uconst:
23532 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
23533 &bytes_read);
23534 i += bytes_read;
23535 break;
23536
23537 case DW_OP_minus:
23538 stack[stacki - 1] -= stack[stacki];
23539 stacki--;
23540 break;
23541
23542 case DW_OP_deref:
23543 /* If we're not the last op, then we definitely can't encode
23544 this using GDB's address_class enum. This is valid for partial
23545 global symbols, although the variable's address will be bogus
23546 in the psymtab. */
23547 if (i < size)
23548 dwarf2_complex_location_expr_complaint ();
23549 break;
23550
23551 case DW_OP_GNU_push_tls_address:
4aa4e28b 23552 case DW_OP_form_tls_address:
21ae7a4d
JK
23553 /* The top of the stack has the offset from the beginning
23554 of the thread control block at which the variable is located. */
23555 /* Nothing should follow this operator, so the top of stack would
23556 be returned. */
23557 /* This is valid for partial global symbols, but the variable's
585861ea
JK
23558 address will be bogus in the psymtab. Make it always at least
23559 non-zero to not look as a variable garbage collected by linker
23560 which have DW_OP_addr 0. */
21ae7a4d
JK
23561 if (i < size)
23562 dwarf2_complex_location_expr_complaint ();
585861ea 23563 stack[stacki]++;
21ae7a4d
JK
23564 break;
23565
23566 case DW_OP_GNU_uninit:
23567 break;
23568
3019eac3 23569 case DW_OP_GNU_addr_index:
49f6c839 23570 case DW_OP_GNU_const_index:
3019eac3
DE
23571 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
23572 &bytes_read);
23573 i += bytes_read;
23574 break;
23575
21ae7a4d
JK
23576 default:
23577 {
f39c6ffd 23578 const char *name = get_DW_OP_name (op);
21ae7a4d
JK
23579
23580 if (name)
b98664d3 23581 complaint (_("unsupported stack op: '%s'"),
21ae7a4d
JK
23582 name);
23583 else
b98664d3 23584 complaint (_("unsupported stack op: '%02x'"),
21ae7a4d
JK
23585 op);
23586 }
23587
23588 return (stack[stacki]);
d53d4ac5 23589 }
3c6e0cb3 23590
21ae7a4d
JK
23591 /* Enforce maximum stack depth of SIZE-1 to avoid writing
23592 outside of the allocated space. Also enforce minimum>0. */
23593 if (stacki >= ARRAY_SIZE (stack) - 1)
23594 {
b98664d3 23595 complaint (_("location description stack overflow"));
21ae7a4d
JK
23596 return 0;
23597 }
23598
23599 if (stacki <= 0)
23600 {
b98664d3 23601 complaint (_("location description stack underflow"));
21ae7a4d
JK
23602 return 0;
23603 }
23604 }
23605 return (stack[stacki]);
c906108c
SS
23606}
23607
23608/* memory allocation interface */
23609
c906108c 23610static struct dwarf_block *
7b5a2f43 23611dwarf_alloc_block (struct dwarf2_cu *cu)
c906108c 23612{
8d749320 23613 return XOBNEW (&cu->comp_unit_obstack, struct dwarf_block);
c906108c
SS
23614}
23615
c906108c 23616static struct die_info *
b60c80d6 23617dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
c906108c
SS
23618{
23619 struct die_info *die;
b60c80d6
DJ
23620 size_t size = sizeof (struct die_info);
23621
23622 if (num_attrs > 1)
23623 size += (num_attrs - 1) * sizeof (struct attribute);
c906108c 23624
b60c80d6 23625 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
c906108c
SS
23626 memset (die, 0, sizeof (struct die_info));
23627 return (die);
23628}
2e276125
JB
23629
23630\f
23631/* Macro support. */
23632
233d95b5
JK
23633/* Return file name relative to the compilation directory of file number I in
23634 *LH's file name table. The result is allocated using xmalloc; the caller is
2e276125 23635 responsible for freeing it. */
233d95b5 23636
2e276125 23637static char *
233d95b5 23638file_file_name (int file, struct line_header *lh)
2e276125 23639{
6a83a1e6
EZ
23640 /* Is the file number a valid index into the line header's file name
23641 table? Remember that file numbers start with one, not zero. */
fff8551c 23642 if (1 <= file && file <= lh->file_names.size ())
6a83a1e6 23643 {
8c43009f 23644 const file_entry &fe = lh->file_names[file - 1];
6e70227d 23645
8c43009f
PA
23646 if (!IS_ABSOLUTE_PATH (fe.name))
23647 {
23648 const char *dir = fe.include_dir (lh);
23649 if (dir != NULL)
23650 return concat (dir, SLASH_STRING, fe.name, (char *) NULL);
23651 }
23652 return xstrdup (fe.name);
6a83a1e6 23653 }
2e276125
JB
23654 else
23655 {
6a83a1e6
EZ
23656 /* The compiler produced a bogus file number. We can at least
23657 record the macro definitions made in the file, even if we
23658 won't be able to find the file by name. */
23659 char fake_name[80];
9a619af0 23660
8c042590
PM
23661 xsnprintf (fake_name, sizeof (fake_name),
23662 "<bad macro file number %d>", file);
2e276125 23663
b98664d3 23664 complaint (_("bad file number in macro information (%d)"),
6a83a1e6 23665 file);
2e276125 23666
6a83a1e6 23667 return xstrdup (fake_name);
2e276125
JB
23668 }
23669}
23670
233d95b5
JK
23671/* Return the full name of file number I in *LH's file name table.
23672 Use COMP_DIR as the name of the current directory of the
23673 compilation. The result is allocated using xmalloc; the caller is
23674 responsible for freeing it. */
23675static char *
23676file_full_name (int file, struct line_header *lh, const char *comp_dir)
23677{
23678 /* Is the file number a valid index into the line header's file name
23679 table? Remember that file numbers start with one, not zero. */
fff8551c 23680 if (1 <= file && file <= lh->file_names.size ())
233d95b5
JK
23681 {
23682 char *relative = file_file_name (file, lh);
23683
23684 if (IS_ABSOLUTE_PATH (relative) || comp_dir == NULL)
23685 return relative;
b36cec19
PA
23686 return reconcat (relative, comp_dir, SLASH_STRING,
23687 relative, (char *) NULL);
233d95b5
JK
23688 }
23689 else
23690 return file_file_name (file, lh);
23691}
23692
2e276125
JB
23693
23694static struct macro_source_file *
23695macro_start_file (int file, int line,
23696 struct macro_source_file *current_file,
43f3e411 23697 struct line_header *lh)
2e276125 23698{
233d95b5
JK
23699 /* File name relative to the compilation directory of this source file. */
23700 char *file_name = file_file_name (file, lh);
2e276125 23701
2e276125 23702 if (! current_file)
abc9d0dc 23703 {
fc474241
DE
23704 /* Note: We don't create a macro table for this compilation unit
23705 at all until we actually get a filename. */
43f3e411 23706 struct macro_table *macro_table = get_macro_table ();
fc474241 23707
abc9d0dc
TT
23708 /* If we have no current file, then this must be the start_file
23709 directive for the compilation unit's main source file. */
fc474241
DE
23710 current_file = macro_set_main (macro_table, file_name);
23711 macro_define_special (macro_table);
abc9d0dc 23712 }
2e276125 23713 else
233d95b5 23714 current_file = macro_include (current_file, line, file_name);
2e276125 23715
233d95b5 23716 xfree (file_name);
6e70227d 23717
2e276125
JB
23718 return current_file;
23719}
23720
2e276125
JB
23721static const char *
23722consume_improper_spaces (const char *p, const char *body)
23723{
23724 if (*p == ' ')
23725 {
b98664d3 23726 complaint (_("macro definition contains spaces "
3e43a32a 23727 "in formal argument list:\n`%s'"),
4d3c2250 23728 body);
2e276125
JB
23729
23730 while (*p == ' ')
23731 p++;
23732 }
23733
23734 return p;
23735}
23736
23737
23738static void
23739parse_macro_definition (struct macro_source_file *file, int line,
23740 const char *body)
23741{
23742 const char *p;
23743
23744 /* The body string takes one of two forms. For object-like macro
23745 definitions, it should be:
23746
23747 <macro name> " " <definition>
23748
23749 For function-like macro definitions, it should be:
23750
23751 <macro name> "() " <definition>
23752 or
23753 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
23754
23755 Spaces may appear only where explicitly indicated, and in the
23756 <definition>.
23757
23758 The Dwarf 2 spec says that an object-like macro's name is always
23759 followed by a space, but versions of GCC around March 2002 omit
6e70227d 23760 the space when the macro's definition is the empty string.
2e276125
JB
23761
23762 The Dwarf 2 spec says that there should be no spaces between the
23763 formal arguments in a function-like macro's formal argument list,
23764 but versions of GCC around March 2002 include spaces after the
23765 commas. */
23766
23767
23768 /* Find the extent of the macro name. The macro name is terminated
23769 by either a space or null character (for an object-like macro) or
23770 an opening paren (for a function-like macro). */
23771 for (p = body; *p; p++)
23772 if (*p == ' ' || *p == '(')
23773 break;
23774
23775 if (*p == ' ' || *p == '\0')
23776 {
23777 /* It's an object-like macro. */
23778 int name_len = p - body;
3f8a7804 23779 char *name = savestring (body, name_len);
2e276125
JB
23780 const char *replacement;
23781
23782 if (*p == ' ')
23783 replacement = body + name_len + 1;
23784 else
23785 {
4d3c2250 23786 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
23787 replacement = body + name_len;
23788 }
6e70227d 23789
2e276125
JB
23790 macro_define_object (file, line, name, replacement);
23791
23792 xfree (name);
23793 }
23794 else if (*p == '(')
23795 {
23796 /* It's a function-like macro. */
3f8a7804 23797 char *name = savestring (body, p - body);
2e276125
JB
23798 int argc = 0;
23799 int argv_size = 1;
8d749320 23800 char **argv = XNEWVEC (char *, argv_size);
2e276125
JB
23801
23802 p++;
23803
23804 p = consume_improper_spaces (p, body);
23805
23806 /* Parse the formal argument list. */
23807 while (*p && *p != ')')
23808 {
23809 /* Find the extent of the current argument name. */
23810 const char *arg_start = p;
23811
23812 while (*p && *p != ',' && *p != ')' && *p != ' ')
23813 p++;
23814
23815 if (! *p || p == arg_start)
4d3c2250 23816 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
23817 else
23818 {
23819 /* Make sure argv has room for the new argument. */
23820 if (argc >= argv_size)
23821 {
23822 argv_size *= 2;
224c3ddb 23823 argv = XRESIZEVEC (char *, argv, argv_size);
2e276125
JB
23824 }
23825
3f8a7804 23826 argv[argc++] = savestring (arg_start, p - arg_start);
2e276125
JB
23827 }
23828
23829 p = consume_improper_spaces (p, body);
23830
23831 /* Consume the comma, if present. */
23832 if (*p == ',')
23833 {
23834 p++;
23835
23836 p = consume_improper_spaces (p, body);
23837 }
23838 }
23839
23840 if (*p == ')')
23841 {
23842 p++;
23843
23844 if (*p == ' ')
23845 /* Perfectly formed definition, no complaints. */
23846 macro_define_function (file, line, name,
6e70227d 23847 argc, (const char **) argv,
2e276125
JB
23848 p + 1);
23849 else if (*p == '\0')
23850 {
23851 /* Complain, but do define it. */
4d3c2250 23852 dwarf2_macro_malformed_definition_complaint (body);
2e276125 23853 macro_define_function (file, line, name,
6e70227d 23854 argc, (const char **) argv,
2e276125
JB
23855 p);
23856 }
23857 else
23858 /* Just complain. */
4d3c2250 23859 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
23860 }
23861 else
23862 /* Just complain. */
4d3c2250 23863 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
23864
23865 xfree (name);
23866 {
23867 int i;
23868
23869 for (i = 0; i < argc; i++)
23870 xfree (argv[i]);
23871 }
23872 xfree (argv);
23873 }
23874 else
4d3c2250 23875 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
23876}
23877
cf2c3c16
TT
23878/* Skip some bytes from BYTES according to the form given in FORM.
23879 Returns the new pointer. */
2e276125 23880
d521ce57
TT
23881static const gdb_byte *
23882skip_form_bytes (bfd *abfd, const gdb_byte *bytes, const gdb_byte *buffer_end,
cf2c3c16
TT
23883 enum dwarf_form form,
23884 unsigned int offset_size,
23885 struct dwarf2_section_info *section)
2e276125 23886{
cf2c3c16 23887 unsigned int bytes_read;
2e276125 23888
cf2c3c16 23889 switch (form)
2e276125 23890 {
cf2c3c16
TT
23891 case DW_FORM_data1:
23892 case DW_FORM_flag:
23893 ++bytes;
23894 break;
23895
23896 case DW_FORM_data2:
23897 bytes += 2;
23898 break;
23899
23900 case DW_FORM_data4:
23901 bytes += 4;
23902 break;
23903
23904 case DW_FORM_data8:
23905 bytes += 8;
23906 break;
23907
0224619f
JK
23908 case DW_FORM_data16:
23909 bytes += 16;
23910 break;
23911
cf2c3c16
TT
23912 case DW_FORM_string:
23913 read_direct_string (abfd, bytes, &bytes_read);
23914 bytes += bytes_read;
23915 break;
23916
23917 case DW_FORM_sec_offset:
23918 case DW_FORM_strp:
36586728 23919 case DW_FORM_GNU_strp_alt:
cf2c3c16
TT
23920 bytes += offset_size;
23921 break;
23922
23923 case DW_FORM_block:
23924 bytes += read_unsigned_leb128 (abfd, bytes, &bytes_read);
23925 bytes += bytes_read;
23926 break;
23927
23928 case DW_FORM_block1:
23929 bytes += 1 + read_1_byte (abfd, bytes);
23930 break;
23931 case DW_FORM_block2:
23932 bytes += 2 + read_2_bytes (abfd, bytes);
23933 break;
23934 case DW_FORM_block4:
23935 bytes += 4 + read_4_bytes (abfd, bytes);
23936 break;
23937
23938 case DW_FORM_sdata:
23939 case DW_FORM_udata:
3019eac3
DE
23940 case DW_FORM_GNU_addr_index:
23941 case DW_FORM_GNU_str_index:
d521ce57 23942 bytes = gdb_skip_leb128 (bytes, buffer_end);
f664829e
DE
23943 if (bytes == NULL)
23944 {
23945 dwarf2_section_buffer_overflow_complaint (section);
23946 return NULL;
23947 }
cf2c3c16
TT
23948 break;
23949
663c44ac
JK
23950 case DW_FORM_implicit_const:
23951 break;
23952
cf2c3c16
TT
23953 default:
23954 {
b98664d3 23955 complaint (_("invalid form 0x%x in `%s'"),
a32a8923 23956 form, get_section_name (section));
cf2c3c16
TT
23957 return NULL;
23958 }
2e276125
JB
23959 }
23960
cf2c3c16
TT
23961 return bytes;
23962}
757a13d0 23963
cf2c3c16
TT
23964/* A helper for dwarf_decode_macros that handles skipping an unknown
23965 opcode. Returns an updated pointer to the macro data buffer; or,
23966 on error, issues a complaint and returns NULL. */
757a13d0 23967
d521ce57 23968static const gdb_byte *
cf2c3c16 23969skip_unknown_opcode (unsigned int opcode,
d521ce57
TT
23970 const gdb_byte **opcode_definitions,
23971 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
cf2c3c16
TT
23972 bfd *abfd,
23973 unsigned int offset_size,
23974 struct dwarf2_section_info *section)
23975{
23976 unsigned int bytes_read, i;
23977 unsigned long arg;
d521ce57 23978 const gdb_byte *defn;
2e276125 23979
cf2c3c16 23980 if (opcode_definitions[opcode] == NULL)
2e276125 23981 {
b98664d3 23982 complaint (_("unrecognized DW_MACFINO opcode 0x%x"),
cf2c3c16
TT
23983 opcode);
23984 return NULL;
23985 }
2e276125 23986
cf2c3c16
TT
23987 defn = opcode_definitions[opcode];
23988 arg = read_unsigned_leb128 (abfd, defn, &bytes_read);
23989 defn += bytes_read;
2e276125 23990
cf2c3c16
TT
23991 for (i = 0; i < arg; ++i)
23992 {
aead7601
SM
23993 mac_ptr = skip_form_bytes (abfd, mac_ptr, mac_end,
23994 (enum dwarf_form) defn[i], offset_size,
f664829e 23995 section);
cf2c3c16
TT
23996 if (mac_ptr == NULL)
23997 {
23998 /* skip_form_bytes already issued the complaint. */
23999 return NULL;
24000 }
24001 }
757a13d0 24002
cf2c3c16
TT
24003 return mac_ptr;
24004}
757a13d0 24005
cf2c3c16
TT
24006/* A helper function which parses the header of a macro section.
24007 If the macro section is the extended (for now called "GNU") type,
24008 then this updates *OFFSET_SIZE. Returns a pointer to just after
24009 the header, or issues a complaint and returns NULL on error. */
757a13d0 24010
d521ce57
TT
24011static const gdb_byte *
24012dwarf_parse_macro_header (const gdb_byte **opcode_definitions,
cf2c3c16 24013 bfd *abfd,
d521ce57 24014 const gdb_byte *mac_ptr,
cf2c3c16
TT
24015 unsigned int *offset_size,
24016 int section_is_gnu)
24017{
24018 memset (opcode_definitions, 0, 256 * sizeof (gdb_byte *));
757a13d0 24019
cf2c3c16
TT
24020 if (section_is_gnu)
24021 {
24022 unsigned int version, flags;
757a13d0 24023
cf2c3c16 24024 version = read_2_bytes (abfd, mac_ptr);
0af92d60 24025 if (version != 4 && version != 5)
cf2c3c16 24026 {
b98664d3 24027 complaint (_("unrecognized version `%d' in .debug_macro section"),
cf2c3c16
TT
24028 version);
24029 return NULL;
24030 }
24031 mac_ptr += 2;
757a13d0 24032
cf2c3c16
TT
24033 flags = read_1_byte (abfd, mac_ptr);
24034 ++mac_ptr;
24035 *offset_size = (flags & 1) ? 8 : 4;
757a13d0 24036
cf2c3c16
TT
24037 if ((flags & 2) != 0)
24038 /* We don't need the line table offset. */
24039 mac_ptr += *offset_size;
757a13d0 24040
cf2c3c16
TT
24041 /* Vendor opcode descriptions. */
24042 if ((flags & 4) != 0)
24043 {
24044 unsigned int i, count;
757a13d0 24045
cf2c3c16
TT
24046 count = read_1_byte (abfd, mac_ptr);
24047 ++mac_ptr;
24048 for (i = 0; i < count; ++i)
24049 {
24050 unsigned int opcode, bytes_read;
24051 unsigned long arg;
24052
24053 opcode = read_1_byte (abfd, mac_ptr);
24054 ++mac_ptr;
24055 opcode_definitions[opcode] = mac_ptr;
24056 arg = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24057 mac_ptr += bytes_read;
24058 mac_ptr += arg;
24059 }
757a13d0 24060 }
cf2c3c16 24061 }
757a13d0 24062
cf2c3c16
TT
24063 return mac_ptr;
24064}
757a13d0 24065
cf2c3c16 24066/* A helper for dwarf_decode_macros that handles the GNU extensions,
0af92d60 24067 including DW_MACRO_import. */
cf2c3c16
TT
24068
24069static void
ed2dc618
SM
24070dwarf_decode_macro_bytes (struct dwarf2_per_objfile *dwarf2_per_objfile,
24071 bfd *abfd,
d521ce57 24072 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
cf2c3c16 24073 struct macro_source_file *current_file,
43f3e411 24074 struct line_header *lh,
cf2c3c16 24075 struct dwarf2_section_info *section,
36586728 24076 int section_is_gnu, int section_is_dwz,
cf2c3c16 24077 unsigned int offset_size,
8fc3fc34 24078 htab_t include_hash)
cf2c3c16 24079{
4d663531 24080 struct objfile *objfile = dwarf2_per_objfile->objfile;
cf2c3c16
TT
24081 enum dwarf_macro_record_type macinfo_type;
24082 int at_commandline;
d521ce57 24083 const gdb_byte *opcode_definitions[256];
757a13d0 24084
cf2c3c16
TT
24085 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
24086 &offset_size, section_is_gnu);
24087 if (mac_ptr == NULL)
24088 {
24089 /* We already issued a complaint. */
24090 return;
24091 }
757a13d0
JK
24092
24093 /* Determines if GDB is still before first DW_MACINFO_start_file. If true
24094 GDB is still reading the definitions from command line. First
24095 DW_MACINFO_start_file will need to be ignored as it was already executed
24096 to create CURRENT_FILE for the main source holding also the command line
24097 definitions. On first met DW_MACINFO_start_file this flag is reset to
24098 normally execute all the remaining DW_MACINFO_start_file macinfos. */
24099
24100 at_commandline = 1;
24101
24102 do
24103 {
24104 /* Do we at least have room for a macinfo type byte? */
24105 if (mac_ptr >= mac_end)
24106 {
f664829e 24107 dwarf2_section_buffer_overflow_complaint (section);
757a13d0
JK
24108 break;
24109 }
24110
aead7601 24111 macinfo_type = (enum dwarf_macro_record_type) read_1_byte (abfd, mac_ptr);
757a13d0
JK
24112 mac_ptr++;
24113
cf2c3c16
TT
24114 /* Note that we rely on the fact that the corresponding GNU and
24115 DWARF constants are the same. */
132448f8
SM
24116 DIAGNOSTIC_PUSH
24117 DIAGNOSTIC_IGNORE_SWITCH_DIFFERENT_ENUM_TYPES
757a13d0
JK
24118 switch (macinfo_type)
24119 {
24120 /* A zero macinfo type indicates the end of the macro
24121 information. */
24122 case 0:
24123 break;
2e276125 24124
0af92d60
JK
24125 case DW_MACRO_define:
24126 case DW_MACRO_undef:
24127 case DW_MACRO_define_strp:
24128 case DW_MACRO_undef_strp:
24129 case DW_MACRO_define_sup:
24130 case DW_MACRO_undef_sup:
2e276125 24131 {
891d2f0b 24132 unsigned int bytes_read;
2e276125 24133 int line;
d521ce57 24134 const char *body;
cf2c3c16 24135 int is_define;
2e276125 24136
cf2c3c16
TT
24137 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24138 mac_ptr += bytes_read;
24139
0af92d60
JK
24140 if (macinfo_type == DW_MACRO_define
24141 || macinfo_type == DW_MACRO_undef)
cf2c3c16
TT
24142 {
24143 body = read_direct_string (abfd, mac_ptr, &bytes_read);
24144 mac_ptr += bytes_read;
24145 }
24146 else
24147 {
24148 LONGEST str_offset;
24149
24150 str_offset = read_offset_1 (abfd, mac_ptr, offset_size);
24151 mac_ptr += offset_size;
2e276125 24152
0af92d60
JK
24153 if (macinfo_type == DW_MACRO_define_sup
24154 || macinfo_type == DW_MACRO_undef_sup
f7a35f02 24155 || section_is_dwz)
36586728 24156 {
ed2dc618
SM
24157 struct dwz_file *dwz
24158 = dwarf2_get_dwz_file (dwarf2_per_objfile);
36586728 24159
ed2dc618
SM
24160 body = read_indirect_string_from_dwz (objfile,
24161 dwz, str_offset);
36586728
TT
24162 }
24163 else
ed2dc618
SM
24164 body = read_indirect_string_at_offset (dwarf2_per_objfile,
24165 abfd, str_offset);
cf2c3c16
TT
24166 }
24167
0af92d60
JK
24168 is_define = (macinfo_type == DW_MACRO_define
24169 || macinfo_type == DW_MACRO_define_strp
24170 || macinfo_type == DW_MACRO_define_sup);
2e276125 24171 if (! current_file)
757a13d0
JK
24172 {
24173 /* DWARF violation as no main source is present. */
b98664d3 24174 complaint (_("debug info with no main source gives macro %s "
757a13d0 24175 "on line %d: %s"),
cf2c3c16
TT
24176 is_define ? _("definition") : _("undefinition"),
24177 line, body);
757a13d0
JK
24178 break;
24179 }
3e43a32a
MS
24180 if ((line == 0 && !at_commandline)
24181 || (line != 0 && at_commandline))
b98664d3 24182 complaint (_("debug info gives %s macro %s with %s line %d: %s"),
757a13d0 24183 at_commandline ? _("command-line") : _("in-file"),
cf2c3c16 24184 is_define ? _("definition") : _("undefinition"),
757a13d0
JK
24185 line == 0 ? _("zero") : _("non-zero"), line, body);
24186
cf2c3c16 24187 if (is_define)
757a13d0 24188 parse_macro_definition (current_file, line, body);
cf2c3c16
TT
24189 else
24190 {
0af92d60
JK
24191 gdb_assert (macinfo_type == DW_MACRO_undef
24192 || macinfo_type == DW_MACRO_undef_strp
24193 || macinfo_type == DW_MACRO_undef_sup);
cf2c3c16
TT
24194 macro_undef (current_file, line, body);
24195 }
2e276125
JB
24196 }
24197 break;
24198
0af92d60 24199 case DW_MACRO_start_file:
2e276125 24200 {
891d2f0b 24201 unsigned int bytes_read;
2e276125
JB
24202 int line, file;
24203
24204 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24205 mac_ptr += bytes_read;
24206 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24207 mac_ptr += bytes_read;
24208
3e43a32a
MS
24209 if ((line == 0 && !at_commandline)
24210 || (line != 0 && at_commandline))
b98664d3 24211 complaint (_("debug info gives source %d included "
757a13d0
JK
24212 "from %s at %s line %d"),
24213 file, at_commandline ? _("command-line") : _("file"),
24214 line == 0 ? _("zero") : _("non-zero"), line);
24215
24216 if (at_commandline)
24217 {
0af92d60 24218 /* This DW_MACRO_start_file was executed in the
cf2c3c16 24219 pass one. */
757a13d0
JK
24220 at_commandline = 0;
24221 }
24222 else
43f3e411 24223 current_file = macro_start_file (file, line, current_file, lh);
2e276125
JB
24224 }
24225 break;
24226
0af92d60 24227 case DW_MACRO_end_file:
2e276125 24228 if (! current_file)
b98664d3 24229 complaint (_("macro debug info has an unmatched "
3e43a32a 24230 "`close_file' directive"));
2e276125
JB
24231 else
24232 {
24233 current_file = current_file->included_by;
24234 if (! current_file)
24235 {
cf2c3c16 24236 enum dwarf_macro_record_type next_type;
2e276125
JB
24237
24238 /* GCC circa March 2002 doesn't produce the zero
24239 type byte marking the end of the compilation
24240 unit. Complain if it's not there, but exit no
24241 matter what. */
24242
24243 /* Do we at least have room for a macinfo type byte? */
24244 if (mac_ptr >= mac_end)
24245 {
f664829e 24246 dwarf2_section_buffer_overflow_complaint (section);
2e276125
JB
24247 return;
24248 }
24249
24250 /* We don't increment mac_ptr here, so this is just
24251 a look-ahead. */
aead7601
SM
24252 next_type
24253 = (enum dwarf_macro_record_type) read_1_byte (abfd,
24254 mac_ptr);
2e276125 24255 if (next_type != 0)
b98664d3 24256 complaint (_("no terminating 0-type entry for "
3e43a32a 24257 "macros in `.debug_macinfo' section"));
2e276125
JB
24258
24259 return;
24260 }
24261 }
24262 break;
24263
0af92d60
JK
24264 case DW_MACRO_import:
24265 case DW_MACRO_import_sup:
cf2c3c16
TT
24266 {
24267 LONGEST offset;
8fc3fc34 24268 void **slot;
a036ba48
TT
24269 bfd *include_bfd = abfd;
24270 struct dwarf2_section_info *include_section = section;
d521ce57 24271 const gdb_byte *include_mac_end = mac_end;
a036ba48 24272 int is_dwz = section_is_dwz;
d521ce57 24273 const gdb_byte *new_mac_ptr;
cf2c3c16
TT
24274
24275 offset = read_offset_1 (abfd, mac_ptr, offset_size);
24276 mac_ptr += offset_size;
24277
0af92d60 24278 if (macinfo_type == DW_MACRO_import_sup)
a036ba48 24279 {
ed2dc618 24280 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
a036ba48 24281
4d663531 24282 dwarf2_read_section (objfile, &dwz->macro);
a036ba48 24283
a036ba48 24284 include_section = &dwz->macro;
a32a8923 24285 include_bfd = get_section_bfd_owner (include_section);
a036ba48
TT
24286 include_mac_end = dwz->macro.buffer + dwz->macro.size;
24287 is_dwz = 1;
24288 }
24289
24290 new_mac_ptr = include_section->buffer + offset;
24291 slot = htab_find_slot (include_hash, new_mac_ptr, INSERT);
24292
8fc3fc34
TT
24293 if (*slot != NULL)
24294 {
24295 /* This has actually happened; see
24296 http://sourceware.org/bugzilla/show_bug.cgi?id=13568. */
b98664d3 24297 complaint (_("recursive DW_MACRO_import in "
8fc3fc34
TT
24298 ".debug_macro section"));
24299 }
24300 else
24301 {
d521ce57 24302 *slot = (void *) new_mac_ptr;
36586728 24303
ed2dc618
SM
24304 dwarf_decode_macro_bytes (dwarf2_per_objfile,
24305 include_bfd, new_mac_ptr,
43f3e411 24306 include_mac_end, current_file, lh,
36586728 24307 section, section_is_gnu, is_dwz,
4d663531 24308 offset_size, include_hash);
8fc3fc34 24309
d521ce57 24310 htab_remove_elt (include_hash, (void *) new_mac_ptr);
8fc3fc34 24311 }
cf2c3c16
TT
24312 }
24313 break;
24314
2e276125 24315 case DW_MACINFO_vendor_ext:
cf2c3c16
TT
24316 if (!section_is_gnu)
24317 {
24318 unsigned int bytes_read;
2e276125 24319
ac298888
TT
24320 /* This reads the constant, but since we don't recognize
24321 any vendor extensions, we ignore it. */
24322 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
cf2c3c16
TT
24323 mac_ptr += bytes_read;
24324 read_direct_string (abfd, mac_ptr, &bytes_read);
24325 mac_ptr += bytes_read;
2e276125 24326
cf2c3c16
TT
24327 /* We don't recognize any vendor extensions. */
24328 break;
24329 }
24330 /* FALLTHROUGH */
24331
24332 default:
24333 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
f664829e 24334 mac_ptr, mac_end, abfd, offset_size,
cf2c3c16
TT
24335 section);
24336 if (mac_ptr == NULL)
24337 return;
24338 break;
2e276125 24339 }
132448f8 24340 DIAGNOSTIC_POP
757a13d0 24341 } while (macinfo_type != 0);
2e276125 24342}
8e19ed76 24343
cf2c3c16 24344static void
09262596 24345dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
43f3e411 24346 int section_is_gnu)
cf2c3c16 24347{
518817b3
SM
24348 struct dwarf2_per_objfile *dwarf2_per_objfile
24349 = cu->per_cu->dwarf2_per_objfile;
bb5ed363 24350 struct objfile *objfile = dwarf2_per_objfile->objfile;
09262596
DE
24351 struct line_header *lh = cu->line_header;
24352 bfd *abfd;
d521ce57 24353 const gdb_byte *mac_ptr, *mac_end;
cf2c3c16
TT
24354 struct macro_source_file *current_file = 0;
24355 enum dwarf_macro_record_type macinfo_type;
24356 unsigned int offset_size = cu->header.offset_size;
d521ce57 24357 const gdb_byte *opcode_definitions[256];
8fc3fc34 24358 void **slot;
09262596
DE
24359 struct dwarf2_section_info *section;
24360 const char *section_name;
24361
24362 if (cu->dwo_unit != NULL)
24363 {
24364 if (section_is_gnu)
24365 {
24366 section = &cu->dwo_unit->dwo_file->sections.macro;
24367 section_name = ".debug_macro.dwo";
24368 }
24369 else
24370 {
24371 section = &cu->dwo_unit->dwo_file->sections.macinfo;
24372 section_name = ".debug_macinfo.dwo";
24373 }
24374 }
24375 else
24376 {
24377 if (section_is_gnu)
24378 {
24379 section = &dwarf2_per_objfile->macro;
24380 section_name = ".debug_macro";
24381 }
24382 else
24383 {
24384 section = &dwarf2_per_objfile->macinfo;
24385 section_name = ".debug_macinfo";
24386 }
24387 }
cf2c3c16 24388
bb5ed363 24389 dwarf2_read_section (objfile, section);
cf2c3c16
TT
24390 if (section->buffer == NULL)
24391 {
b98664d3 24392 complaint (_("missing %s section"), section_name);
cf2c3c16
TT
24393 return;
24394 }
a32a8923 24395 abfd = get_section_bfd_owner (section);
cf2c3c16
TT
24396
24397 /* First pass: Find the name of the base filename.
24398 This filename is needed in order to process all macros whose definition
24399 (or undefinition) comes from the command line. These macros are defined
24400 before the first DW_MACINFO_start_file entry, and yet still need to be
24401 associated to the base file.
24402
24403 To determine the base file name, we scan the macro definitions until we
24404 reach the first DW_MACINFO_start_file entry. We then initialize
24405 CURRENT_FILE accordingly so that any macro definition found before the
24406 first DW_MACINFO_start_file can still be associated to the base file. */
24407
24408 mac_ptr = section->buffer + offset;
24409 mac_end = section->buffer + section->size;
24410
24411 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
24412 &offset_size, section_is_gnu);
24413 if (mac_ptr == NULL)
24414 {
24415 /* We already issued a complaint. */
24416 return;
24417 }
24418
24419 do
24420 {
24421 /* Do we at least have room for a macinfo type byte? */
24422 if (mac_ptr >= mac_end)
24423 {
24424 /* Complaint is printed during the second pass as GDB will probably
24425 stop the first pass earlier upon finding
24426 DW_MACINFO_start_file. */
24427 break;
24428 }
24429
aead7601 24430 macinfo_type = (enum dwarf_macro_record_type) read_1_byte (abfd, mac_ptr);
cf2c3c16
TT
24431 mac_ptr++;
24432
24433 /* Note that we rely on the fact that the corresponding GNU and
24434 DWARF constants are the same. */
132448f8
SM
24435 DIAGNOSTIC_PUSH
24436 DIAGNOSTIC_IGNORE_SWITCH_DIFFERENT_ENUM_TYPES
cf2c3c16
TT
24437 switch (macinfo_type)
24438 {
24439 /* A zero macinfo type indicates the end of the macro
24440 information. */
24441 case 0:
24442 break;
24443
0af92d60
JK
24444 case DW_MACRO_define:
24445 case DW_MACRO_undef:
cf2c3c16
TT
24446 /* Only skip the data by MAC_PTR. */
24447 {
24448 unsigned int bytes_read;
24449
24450 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24451 mac_ptr += bytes_read;
24452 read_direct_string (abfd, mac_ptr, &bytes_read);
24453 mac_ptr += bytes_read;
24454 }
24455 break;
24456
0af92d60 24457 case DW_MACRO_start_file:
cf2c3c16
TT
24458 {
24459 unsigned int bytes_read;
24460 int line, file;
24461
24462 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24463 mac_ptr += bytes_read;
24464 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24465 mac_ptr += bytes_read;
24466
43f3e411 24467 current_file = macro_start_file (file, line, current_file, lh);
cf2c3c16
TT
24468 }
24469 break;
24470
0af92d60 24471 case DW_MACRO_end_file:
cf2c3c16
TT
24472 /* No data to skip by MAC_PTR. */
24473 break;
24474
0af92d60
JK
24475 case DW_MACRO_define_strp:
24476 case DW_MACRO_undef_strp:
24477 case DW_MACRO_define_sup:
24478 case DW_MACRO_undef_sup:
cf2c3c16
TT
24479 {
24480 unsigned int bytes_read;
24481
24482 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24483 mac_ptr += bytes_read;
24484 mac_ptr += offset_size;
24485 }
24486 break;
24487
0af92d60
JK
24488 case DW_MACRO_import:
24489 case DW_MACRO_import_sup:
cf2c3c16 24490 /* Note that, according to the spec, a transparent include
0af92d60 24491 chain cannot call DW_MACRO_start_file. So, we can just
cf2c3c16
TT
24492 skip this opcode. */
24493 mac_ptr += offset_size;
24494 break;
24495
24496 case DW_MACINFO_vendor_ext:
24497 /* Only skip the data by MAC_PTR. */
24498 if (!section_is_gnu)
24499 {
24500 unsigned int bytes_read;
24501
24502 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24503 mac_ptr += bytes_read;
24504 read_direct_string (abfd, mac_ptr, &bytes_read);
24505 mac_ptr += bytes_read;
24506 }
24507 /* FALLTHROUGH */
24508
24509 default:
24510 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
f664829e 24511 mac_ptr, mac_end, abfd, offset_size,
cf2c3c16
TT
24512 section);
24513 if (mac_ptr == NULL)
24514 return;
24515 break;
24516 }
132448f8 24517 DIAGNOSTIC_POP
cf2c3c16
TT
24518 } while (macinfo_type != 0 && current_file == NULL);
24519
24520 /* Second pass: Process all entries.
24521
24522 Use the AT_COMMAND_LINE flag to determine whether we are still processing
24523 command-line macro definitions/undefinitions. This flag is unset when we
24524 reach the first DW_MACINFO_start_file entry. */
24525
fc4007c9
TT
24526 htab_up include_hash (htab_create_alloc (1, htab_hash_pointer,
24527 htab_eq_pointer,
24528 NULL, xcalloc, xfree));
8fc3fc34 24529 mac_ptr = section->buffer + offset;
fc4007c9 24530 slot = htab_find_slot (include_hash.get (), mac_ptr, INSERT);
d521ce57 24531 *slot = (void *) mac_ptr;
ed2dc618
SM
24532 dwarf_decode_macro_bytes (dwarf2_per_objfile,
24533 abfd, mac_ptr, mac_end,
43f3e411 24534 current_file, lh, section,
fc4007c9
TT
24535 section_is_gnu, 0, offset_size,
24536 include_hash.get ());
cf2c3c16
TT
24537}
24538
8e19ed76 24539/* Check if the attribute's form is a DW_FORM_block*
0963b4bd 24540 if so return true else false. */
380bca97 24541
8e19ed76 24542static int
6e5a29e1 24543attr_form_is_block (const struct attribute *attr)
8e19ed76
PS
24544{
24545 return (attr == NULL ? 0 :
24546 attr->form == DW_FORM_block1
24547 || attr->form == DW_FORM_block2
24548 || attr->form == DW_FORM_block4
2dc7f7b3
TT
24549 || attr->form == DW_FORM_block
24550 || attr->form == DW_FORM_exprloc);
8e19ed76 24551}
4c2df51b 24552
c6a0999f
JB
24553/* Return non-zero if ATTR's value is a section offset --- classes
24554 lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
24555 You may use DW_UNSND (attr) to retrieve such offsets.
24556
24557 Section 7.5.4, "Attribute Encodings", explains that no attribute
24558 may have a value that belongs to more than one of these classes; it
24559 would be ambiguous if we did, because we use the same forms for all
24560 of them. */
380bca97 24561
3690dd37 24562static int
6e5a29e1 24563attr_form_is_section_offset (const struct attribute *attr)
3690dd37
JB
24564{
24565 return (attr->form == DW_FORM_data4
2dc7f7b3
TT
24566 || attr->form == DW_FORM_data8
24567 || attr->form == DW_FORM_sec_offset);
3690dd37
JB
24568}
24569
3690dd37
JB
24570/* Return non-zero if ATTR's value falls in the 'constant' class, or
24571 zero otherwise. When this function returns true, you can apply
24572 dwarf2_get_attr_constant_value to it.
24573
24574 However, note that for some attributes you must check
24575 attr_form_is_section_offset before using this test. DW_FORM_data4
24576 and DW_FORM_data8 are members of both the constant class, and of
24577 the classes that contain offsets into other debug sections
24578 (lineptr, loclistptr, macptr or rangelistptr). The DWARF spec says
24579 that, if an attribute's can be either a constant or one of the
24580 section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
0224619f
JK
24581 taken as section offsets, not constants.
24582
24583 DW_FORM_data16 is not considered as dwarf2_get_attr_constant_value
24584 cannot handle that. */
380bca97 24585
3690dd37 24586static int
6e5a29e1 24587attr_form_is_constant (const struct attribute *attr)
3690dd37
JB
24588{
24589 switch (attr->form)
24590 {
24591 case DW_FORM_sdata:
24592 case DW_FORM_udata:
24593 case DW_FORM_data1:
24594 case DW_FORM_data2:
24595 case DW_FORM_data4:
24596 case DW_FORM_data8:
663c44ac 24597 case DW_FORM_implicit_const:
3690dd37
JB
24598 return 1;
24599 default:
24600 return 0;
24601 }
24602}
24603
7771576e
SA
24604
24605/* DW_ADDR is always stored already as sect_offset; despite for the forms
24606 besides DW_FORM_ref_addr it is stored as cu_offset in the DWARF file. */
24607
24608static int
6e5a29e1 24609attr_form_is_ref (const struct attribute *attr)
7771576e
SA
24610{
24611 switch (attr->form)
24612 {
24613 case DW_FORM_ref_addr:
24614 case DW_FORM_ref1:
24615 case DW_FORM_ref2:
24616 case DW_FORM_ref4:
24617 case DW_FORM_ref8:
24618 case DW_FORM_ref_udata:
24619 case DW_FORM_GNU_ref_alt:
24620 return 1;
24621 default:
24622 return 0;
24623 }
24624}
24625
3019eac3
DE
24626/* Return the .debug_loc section to use for CU.
24627 For DWO files use .debug_loc.dwo. */
24628
24629static struct dwarf2_section_info *
24630cu_debug_loc_section (struct dwarf2_cu *cu)
24631{
518817b3
SM
24632 struct dwarf2_per_objfile *dwarf2_per_objfile
24633 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 24634
3019eac3 24635 if (cu->dwo_unit)
43988095
JK
24636 {
24637 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
24638
24639 return cu->header.version >= 5 ? &sections->loclists : &sections->loc;
24640 }
24641 return (cu->header.version >= 5 ? &dwarf2_per_objfile->loclists
24642 : &dwarf2_per_objfile->loc);
3019eac3
DE
24643}
24644
8cf6f0b1
TT
24645/* A helper function that fills in a dwarf2_loclist_baton. */
24646
24647static void
24648fill_in_loclist_baton (struct dwarf2_cu *cu,
24649 struct dwarf2_loclist_baton *baton,
ff39bb5e 24650 const struct attribute *attr)
8cf6f0b1 24651{
518817b3
SM
24652 struct dwarf2_per_objfile *dwarf2_per_objfile
24653 = cu->per_cu->dwarf2_per_objfile;
3019eac3
DE
24654 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
24655
24656 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
8cf6f0b1
TT
24657
24658 baton->per_cu = cu->per_cu;
24659 gdb_assert (baton->per_cu);
24660 /* We don't know how long the location list is, but make sure we
24661 don't run off the edge of the section. */
3019eac3
DE
24662 baton->size = section->size - DW_UNSND (attr);
24663 baton->data = section->buffer + DW_UNSND (attr);
8cf6f0b1 24664 baton->base_address = cu->base_address;
f664829e 24665 baton->from_dwo = cu->dwo_unit != NULL;
8cf6f0b1
TT
24666}
24667
4c2df51b 24668static void
ff39bb5e 24669dwarf2_symbol_mark_computed (const struct attribute *attr, struct symbol *sym,
f1e6e072 24670 struct dwarf2_cu *cu, int is_block)
4c2df51b 24671{
518817b3
SM
24672 struct dwarf2_per_objfile *dwarf2_per_objfile
24673 = cu->per_cu->dwarf2_per_objfile;
bb5ed363 24674 struct objfile *objfile = dwarf2_per_objfile->objfile;
3019eac3 24675 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
bb5ed363 24676
3690dd37 24677 if (attr_form_is_section_offset (attr)
3019eac3 24678 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
99bcc461
DJ
24679 the section. If so, fall through to the complaint in the
24680 other branch. */
3019eac3 24681 && DW_UNSND (attr) < dwarf2_section_size (objfile, section))
4c2df51b 24682 {
0d53c4c4 24683 struct dwarf2_loclist_baton *baton;
4c2df51b 24684
8d749320 24685 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_loclist_baton);
4c2df51b 24686
8cf6f0b1 24687 fill_in_loclist_baton (cu, baton, attr);
be391dca 24688
d00adf39 24689 if (cu->base_known == 0)
b98664d3 24690 complaint (_("Location list used without "
3e43a32a 24691 "specifying the CU base address."));
4c2df51b 24692
f1e6e072
TT
24693 SYMBOL_ACLASS_INDEX (sym) = (is_block
24694 ? dwarf2_loclist_block_index
24695 : dwarf2_loclist_index);
0d53c4c4
DJ
24696 SYMBOL_LOCATION_BATON (sym) = baton;
24697 }
24698 else
24699 {
24700 struct dwarf2_locexpr_baton *baton;
24701
8d749320 24702 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
ae0d2f24
UW
24703 baton->per_cu = cu->per_cu;
24704 gdb_assert (baton->per_cu);
0d53c4c4
DJ
24705
24706 if (attr_form_is_block (attr))
24707 {
24708 /* Note that we're just copying the block's data pointer
24709 here, not the actual data. We're still pointing into the
6502dd73
DJ
24710 info_buffer for SYM's objfile; right now we never release
24711 that buffer, but when we do clean up properly this may
24712 need to change. */
0d53c4c4
DJ
24713 baton->size = DW_BLOCK (attr)->size;
24714 baton->data = DW_BLOCK (attr)->data;
24715 }
24716 else
24717 {
24718 dwarf2_invalid_attrib_class_complaint ("location description",
24719 SYMBOL_NATURAL_NAME (sym));
24720 baton->size = 0;
0d53c4c4 24721 }
6e70227d 24722
f1e6e072
TT
24723 SYMBOL_ACLASS_INDEX (sym) = (is_block
24724 ? dwarf2_locexpr_block_index
24725 : dwarf2_locexpr_index);
0d53c4c4
DJ
24726 SYMBOL_LOCATION_BATON (sym) = baton;
24727 }
4c2df51b 24728}
6502dd73 24729
9aa1f1e3
TT
24730/* Return the OBJFILE associated with the compilation unit CU. If CU
24731 came from a separate debuginfo file, then the master objfile is
24732 returned. */
ae0d2f24
UW
24733
24734struct objfile *
24735dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
24736{
e3b94546 24737 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
ae0d2f24
UW
24738
24739 /* Return the master objfile, so that we can report and look up the
24740 correct file containing this variable. */
24741 if (objfile->separate_debug_objfile_backlink)
24742 objfile = objfile->separate_debug_objfile_backlink;
24743
24744 return objfile;
24745}
24746
96408a79
SA
24747/* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
24748 (CU_HEADERP is unused in such case) or prepare a temporary copy at
24749 CU_HEADERP first. */
24750
24751static const struct comp_unit_head *
24752per_cu_header_read_in (struct comp_unit_head *cu_headerp,
24753 struct dwarf2_per_cu_data *per_cu)
24754{
d521ce57 24755 const gdb_byte *info_ptr;
96408a79
SA
24756
24757 if (per_cu->cu)
24758 return &per_cu->cu->header;
24759
9c541725 24760 info_ptr = per_cu->section->buffer + to_underlying (per_cu->sect_off);
96408a79
SA
24761
24762 memset (cu_headerp, 0, sizeof (*cu_headerp));
43988095
JK
24763 read_comp_unit_head (cu_headerp, info_ptr, per_cu->section,
24764 rcuh_kind::COMPILE);
96408a79
SA
24765
24766 return cu_headerp;
24767}
24768
ae0d2f24
UW
24769/* Return the address size given in the compilation unit header for CU. */
24770
98714339 24771int
ae0d2f24
UW
24772dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
24773{
96408a79
SA
24774 struct comp_unit_head cu_header_local;
24775 const struct comp_unit_head *cu_headerp;
c471e790 24776
96408a79
SA
24777 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
24778
24779 return cu_headerp->addr_size;
ae0d2f24
UW
24780}
24781
9eae7c52
TT
24782/* Return the offset size given in the compilation unit header for CU. */
24783
24784int
24785dwarf2_per_cu_offset_size (struct dwarf2_per_cu_data *per_cu)
24786{
96408a79
SA
24787 struct comp_unit_head cu_header_local;
24788 const struct comp_unit_head *cu_headerp;
9c6c53f7 24789
96408a79
SA
24790 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
24791
24792 return cu_headerp->offset_size;
24793}
24794
24795/* See its dwarf2loc.h declaration. */
24796
24797int
24798dwarf2_per_cu_ref_addr_size (struct dwarf2_per_cu_data *per_cu)
24799{
24800 struct comp_unit_head cu_header_local;
24801 const struct comp_unit_head *cu_headerp;
24802
24803 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
24804
24805 if (cu_headerp->version == 2)
24806 return cu_headerp->addr_size;
24807 else
24808 return cu_headerp->offset_size;
181cebd4
JK
24809}
24810
9aa1f1e3
TT
24811/* Return the text offset of the CU. The returned offset comes from
24812 this CU's objfile. If this objfile came from a separate debuginfo
24813 file, then the offset may be different from the corresponding
24814 offset in the parent objfile. */
24815
24816CORE_ADDR
24817dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
24818{
e3b94546 24819 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
9aa1f1e3
TT
24820
24821 return ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
24822}
24823
43988095
JK
24824/* Return DWARF version number of PER_CU. */
24825
24826short
24827dwarf2_version (struct dwarf2_per_cu_data *per_cu)
24828{
24829 return per_cu->dwarf_version;
24830}
24831
348e048f
DE
24832/* Locate the .debug_info compilation unit from CU's objfile which contains
24833 the DIE at OFFSET. Raises an error on failure. */
ae038cb0
DJ
24834
24835static struct dwarf2_per_cu_data *
9c541725 24836dwarf2_find_containing_comp_unit (sect_offset sect_off,
36586728 24837 unsigned int offset_in_dwz,
ed2dc618 24838 struct dwarf2_per_objfile *dwarf2_per_objfile)
ae038cb0
DJ
24839{
24840 struct dwarf2_per_cu_data *this_cu;
24841 int low, high;
36586728 24842 const sect_offset *cu_off;
ae038cb0 24843
ae038cb0 24844 low = 0;
b76e467d 24845 high = dwarf2_per_objfile->all_comp_units.size () - 1;
ae038cb0
DJ
24846 while (high > low)
24847 {
36586728 24848 struct dwarf2_per_cu_data *mid_cu;
ae038cb0 24849 int mid = low + (high - low) / 2;
9a619af0 24850
36586728 24851 mid_cu = dwarf2_per_objfile->all_comp_units[mid];
9c541725 24852 cu_off = &mid_cu->sect_off;
36586728 24853 if (mid_cu->is_dwz > offset_in_dwz
9c541725 24854 || (mid_cu->is_dwz == offset_in_dwz && *cu_off >= sect_off))
ae038cb0
DJ
24855 high = mid;
24856 else
24857 low = mid + 1;
24858 }
24859 gdb_assert (low == high);
36586728 24860 this_cu = dwarf2_per_objfile->all_comp_units[low];
9c541725
PA
24861 cu_off = &this_cu->sect_off;
24862 if (this_cu->is_dwz != offset_in_dwz || *cu_off > sect_off)
ae038cb0 24863 {
36586728 24864 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
8a3fe4f8 24865 error (_("Dwarf Error: could not find partial DIE containing "
9d8780f0
SM
24866 "offset %s [in module %s]"),
24867 sect_offset_str (sect_off),
ed2dc618 24868 bfd_get_filename (dwarf2_per_objfile->objfile->obfd));
10b3939b 24869
9c541725
PA
24870 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->sect_off
24871 <= sect_off);
ae038cb0
DJ
24872 return dwarf2_per_objfile->all_comp_units[low-1];
24873 }
24874 else
24875 {
24876 this_cu = dwarf2_per_objfile->all_comp_units[low];
b76e467d 24877 if (low == dwarf2_per_objfile->all_comp_units.size () - 1
9c541725 24878 && sect_off >= this_cu->sect_off + this_cu->length)
9d8780f0 24879 error (_("invalid dwarf2 offset %s"), sect_offset_str (sect_off));
9c541725 24880 gdb_assert (sect_off < this_cu->sect_off + this_cu->length);
ae038cb0
DJ
24881 return this_cu;
24882 }
24883}
24884
23745b47 24885/* Initialize dwarf2_cu CU, owned by PER_CU. */
93311388 24886
fcd3b13d
SM
24887dwarf2_cu::dwarf2_cu (struct dwarf2_per_cu_data *per_cu_)
24888 : per_cu (per_cu_),
24889 mark (0),
24890 has_loclist (0),
24891 checked_producer (0),
24892 producer_is_gxx_lt_4_6 (0),
24893 producer_is_gcc_lt_4_3 (0),
24894 producer_is_icc_lt_14 (0),
24895 processing_has_namespace_info (0)
93311388 24896{
fcd3b13d
SM
24897 per_cu->cu = this;
24898}
24899
24900/* Destroy a dwarf2_cu. */
24901
24902dwarf2_cu::~dwarf2_cu ()
24903{
24904 per_cu->cu = NULL;
9816fde3
JK
24905}
24906
24907/* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
24908
24909static void
95554aad
TT
24910prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
24911 enum language pretend_language)
9816fde3
JK
24912{
24913 struct attribute *attr;
24914
24915 /* Set the language we're debugging. */
24916 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
24917 if (attr)
24918 set_cu_language (DW_UNSND (attr), cu);
24919 else
9cded63f 24920 {
95554aad 24921 cu->language = pretend_language;
9cded63f
TT
24922 cu->language_defn = language_def (cu->language);
24923 }
dee91e82 24924
7d45c7c3 24925 cu->producer = dwarf2_string_attr (comp_unit_die, DW_AT_producer, cu);
93311388
DE
24926}
24927
ae038cb0
DJ
24928/* Increase the age counter on each cached compilation unit, and free
24929 any that are too old. */
24930
24931static void
ed2dc618 24932age_cached_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
ae038cb0
DJ
24933{
24934 struct dwarf2_per_cu_data *per_cu, **last_chain;
24935
24936 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
24937 per_cu = dwarf2_per_objfile->read_in_chain;
24938 while (per_cu != NULL)
24939 {
24940 per_cu->cu->last_used ++;
b4f54984 24941 if (per_cu->cu->last_used <= dwarf_max_cache_age)
ae038cb0
DJ
24942 dwarf2_mark (per_cu->cu);
24943 per_cu = per_cu->cu->read_in_chain;
24944 }
24945
24946 per_cu = dwarf2_per_objfile->read_in_chain;
24947 last_chain = &dwarf2_per_objfile->read_in_chain;
24948 while (per_cu != NULL)
24949 {
24950 struct dwarf2_per_cu_data *next_cu;
24951
24952 next_cu = per_cu->cu->read_in_chain;
24953
24954 if (!per_cu->cu->mark)
24955 {
fcd3b13d 24956 delete per_cu->cu;
ae038cb0
DJ
24957 *last_chain = next_cu;
24958 }
24959 else
24960 last_chain = &per_cu->cu->read_in_chain;
24961
24962 per_cu = next_cu;
24963 }
24964}
24965
24966/* Remove a single compilation unit from the cache. */
24967
24968static void
dee91e82 24969free_one_cached_comp_unit (struct dwarf2_per_cu_data *target_per_cu)
ae038cb0
DJ
24970{
24971 struct dwarf2_per_cu_data *per_cu, **last_chain;
ed2dc618
SM
24972 struct dwarf2_per_objfile *dwarf2_per_objfile
24973 = target_per_cu->dwarf2_per_objfile;
ae038cb0
DJ
24974
24975 per_cu = dwarf2_per_objfile->read_in_chain;
24976 last_chain = &dwarf2_per_objfile->read_in_chain;
24977 while (per_cu != NULL)
24978 {
24979 struct dwarf2_per_cu_data *next_cu;
24980
24981 next_cu = per_cu->cu->read_in_chain;
24982
dee91e82 24983 if (per_cu == target_per_cu)
ae038cb0 24984 {
fcd3b13d 24985 delete per_cu->cu;
dee91e82 24986 per_cu->cu = NULL;
ae038cb0
DJ
24987 *last_chain = next_cu;
24988 break;
24989 }
24990 else
24991 last_chain = &per_cu->cu->read_in_chain;
24992
24993 per_cu = next_cu;
24994 }
24995}
24996
d95d3aef 24997/* Cleanup function for the dwarf2_per_objfile data. */
fe3e1990 24998
d95d3aef
TT
24999static void
25000dwarf2_free_objfile (struct objfile *objfile, void *datum)
fe3e1990 25001{
ed2dc618 25002 struct dwarf2_per_objfile *dwarf2_per_objfile
d95d3aef 25003 = static_cast<struct dwarf2_per_objfile *> (datum);
fe3e1990 25004
fd90ace4 25005 delete dwarf2_per_objfile;
fe3e1990
DJ
25006}
25007
dee91e82
DE
25008/* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
25009 We store these in a hash table separate from the DIEs, and preserve them
25010 when the DIEs are flushed out of cache.
25011
25012 The CU "per_cu" pointer is needed because offset alone is not enough to
3019eac3 25013 uniquely identify the type. A file may have multiple .debug_types sections,
c88ee1f0
DE
25014 or the type may come from a DWO file. Furthermore, while it's more logical
25015 to use per_cu->section+offset, with Fission the section with the data is in
25016 the DWO file but we don't know that section at the point we need it.
25017 We have to use something in dwarf2_per_cu_data (or the pointer to it)
25018 because we can enter the lookup routine, get_die_type_at_offset, from
25019 outside this file, and thus won't necessarily have PER_CU->cu.
25020 Fortunately, PER_CU is stable for the life of the objfile. */
1c379e20 25021
dee91e82 25022struct dwarf2_per_cu_offset_and_type
1c379e20 25023{
dee91e82 25024 const struct dwarf2_per_cu_data *per_cu;
9c541725 25025 sect_offset sect_off;
1c379e20
DJ
25026 struct type *type;
25027};
25028
dee91e82 25029/* Hash function for a dwarf2_per_cu_offset_and_type. */
1c379e20
DJ
25030
25031static hashval_t
dee91e82 25032per_cu_offset_and_type_hash (const void *item)
1c379e20 25033{
9a3c8263
SM
25034 const struct dwarf2_per_cu_offset_and_type *ofs
25035 = (const struct dwarf2_per_cu_offset_and_type *) item;
9a619af0 25036
9c541725 25037 return (uintptr_t) ofs->per_cu + to_underlying (ofs->sect_off);
1c379e20
DJ
25038}
25039
dee91e82 25040/* Equality function for a dwarf2_per_cu_offset_and_type. */
1c379e20
DJ
25041
25042static int
dee91e82 25043per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
1c379e20 25044{
9a3c8263
SM
25045 const struct dwarf2_per_cu_offset_and_type *ofs_lhs
25046 = (const struct dwarf2_per_cu_offset_and_type *) item_lhs;
25047 const struct dwarf2_per_cu_offset_and_type *ofs_rhs
25048 = (const struct dwarf2_per_cu_offset_and_type *) item_rhs;
9a619af0 25049
dee91e82 25050 return (ofs_lhs->per_cu == ofs_rhs->per_cu
9c541725 25051 && ofs_lhs->sect_off == ofs_rhs->sect_off);
1c379e20
DJ
25052}
25053
25054/* Set the type associated with DIE to TYPE. Save it in CU's hash
7e314c57
JK
25055 table if necessary. For convenience, return TYPE.
25056
25057 The DIEs reading must have careful ordering to:
25058 * Not cause infite loops trying to read in DIEs as a prerequisite for
25059 reading current DIE.
25060 * Not trying to dereference contents of still incompletely read in types
25061 while reading in other DIEs.
25062 * Enable referencing still incompletely read in types just by a pointer to
25063 the type without accessing its fields.
25064
25065 Therefore caller should follow these rules:
25066 * Try to fetch any prerequisite types we may need to build this DIE type
25067 before building the type and calling set_die_type.
e71ec853 25068 * After building type call set_die_type for current DIE as soon as
7e314c57
JK
25069 possible before fetching more types to complete the current type.
25070 * Make the type as complete as possible before fetching more types. */
1c379e20 25071
f792889a 25072static struct type *
1c379e20
DJ
25073set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
25074{
518817b3
SM
25075 struct dwarf2_per_objfile *dwarf2_per_objfile
25076 = cu->per_cu->dwarf2_per_objfile;
dee91e82 25077 struct dwarf2_per_cu_offset_and_type **slot, ofs;
ed2dc618 25078 struct objfile *objfile = dwarf2_per_objfile->objfile;
3cdcd0ce
JB
25079 struct attribute *attr;
25080 struct dynamic_prop prop;
1c379e20 25081
b4ba55a1
JB
25082 /* For Ada types, make sure that the gnat-specific data is always
25083 initialized (if not already set). There are a few types where
25084 we should not be doing so, because the type-specific area is
25085 already used to hold some other piece of info (eg: TYPE_CODE_FLT
25086 where the type-specific area is used to store the floatformat).
25087 But this is not a problem, because the gnat-specific information
25088 is actually not needed for these types. */
25089 if (need_gnat_info (cu)
25090 && TYPE_CODE (type) != TYPE_CODE_FUNC
25091 && TYPE_CODE (type) != TYPE_CODE_FLT
09e2d7c7
DE
25092 && TYPE_CODE (type) != TYPE_CODE_METHODPTR
25093 && TYPE_CODE (type) != TYPE_CODE_MEMBERPTR
25094 && TYPE_CODE (type) != TYPE_CODE_METHOD
b4ba55a1
JB
25095 && !HAVE_GNAT_AUX_INFO (type))
25096 INIT_GNAT_SPECIFIC (type);
25097
3f2f83dd
KB
25098 /* Read DW_AT_allocated and set in type. */
25099 attr = dwarf2_attr (die, DW_AT_allocated, cu);
25100 if (attr_form_is_block (attr))
25101 {
25102 if (attr_to_dynamic_prop (attr, die, cu, &prop))
50a82047 25103 add_dyn_prop (DYN_PROP_ALLOCATED, prop, type);
3f2f83dd
KB
25104 }
25105 else if (attr != NULL)
25106 {
b98664d3 25107 complaint (_("DW_AT_allocated has the wrong form (%s) at DIE %s"),
9c541725 25108 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
9d8780f0 25109 sect_offset_str (die->sect_off));
3f2f83dd
KB
25110 }
25111
25112 /* Read DW_AT_associated and set in type. */
25113 attr = dwarf2_attr (die, DW_AT_associated, cu);
25114 if (attr_form_is_block (attr))
25115 {
25116 if (attr_to_dynamic_prop (attr, die, cu, &prop))
50a82047 25117 add_dyn_prop (DYN_PROP_ASSOCIATED, prop, type);
3f2f83dd
KB
25118 }
25119 else if (attr != NULL)
25120 {
b98664d3 25121 complaint (_("DW_AT_associated has the wrong form (%s) at DIE %s"),
9c541725 25122 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
9d8780f0 25123 sect_offset_str (die->sect_off));
3f2f83dd
KB
25124 }
25125
3cdcd0ce
JB
25126 /* Read DW_AT_data_location and set in type. */
25127 attr = dwarf2_attr (die, DW_AT_data_location, cu);
25128 if (attr_to_dynamic_prop (attr, die, cu, &prop))
50a82047 25129 add_dyn_prop (DYN_PROP_DATA_LOCATION, prop, type);
3cdcd0ce 25130
dee91e82 25131 if (dwarf2_per_objfile->die_type_hash == NULL)
f792889a 25132 {
dee91e82
DE
25133 dwarf2_per_objfile->die_type_hash =
25134 htab_create_alloc_ex (127,
25135 per_cu_offset_and_type_hash,
25136 per_cu_offset_and_type_eq,
25137 NULL,
25138 &objfile->objfile_obstack,
25139 hashtab_obstack_allocate,
25140 dummy_obstack_deallocate);
f792889a 25141 }
1c379e20 25142
dee91e82 25143 ofs.per_cu = cu->per_cu;
9c541725 25144 ofs.sect_off = die->sect_off;
1c379e20 25145 ofs.type = type;
dee91e82
DE
25146 slot = (struct dwarf2_per_cu_offset_and_type **)
25147 htab_find_slot (dwarf2_per_objfile->die_type_hash, &ofs, INSERT);
7e314c57 25148 if (*slot)
b98664d3 25149 complaint (_("A problem internal to GDB: DIE %s has type already set"),
9d8780f0 25150 sect_offset_str (die->sect_off));
8d749320
SM
25151 *slot = XOBNEW (&objfile->objfile_obstack,
25152 struct dwarf2_per_cu_offset_and_type);
1c379e20 25153 **slot = ofs;
f792889a 25154 return type;
1c379e20
DJ
25155}
25156
9c541725 25157/* Look up the type for the die at SECT_OFF in PER_CU in die_type_hash,
02142a6c 25158 or return NULL if the die does not have a saved type. */
1c379e20
DJ
25159
25160static struct type *
9c541725 25161get_die_type_at_offset (sect_offset sect_off,
673bfd45 25162 struct dwarf2_per_cu_data *per_cu)
1c379e20 25163{
dee91e82 25164 struct dwarf2_per_cu_offset_and_type *slot, ofs;
ed2dc618 25165 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
f792889a 25166
dee91e82 25167 if (dwarf2_per_objfile->die_type_hash == NULL)
f792889a 25168 return NULL;
1c379e20 25169
dee91e82 25170 ofs.per_cu = per_cu;
9c541725 25171 ofs.sect_off = sect_off;
9a3c8263
SM
25172 slot = ((struct dwarf2_per_cu_offset_and_type *)
25173 htab_find (dwarf2_per_objfile->die_type_hash, &ofs));
1c379e20
DJ
25174 if (slot)
25175 return slot->type;
25176 else
25177 return NULL;
25178}
25179
02142a6c 25180/* Look up the type for DIE in CU in die_type_hash,
673bfd45
DE
25181 or return NULL if DIE does not have a saved type. */
25182
25183static struct type *
25184get_die_type (struct die_info *die, struct dwarf2_cu *cu)
25185{
9c541725 25186 return get_die_type_at_offset (die->sect_off, cu->per_cu);
673bfd45
DE
25187}
25188
10b3939b
DJ
25189/* Add a dependence relationship from CU to REF_PER_CU. */
25190
25191static void
25192dwarf2_add_dependence (struct dwarf2_cu *cu,
25193 struct dwarf2_per_cu_data *ref_per_cu)
25194{
25195 void **slot;
25196
25197 if (cu->dependencies == NULL)
25198 cu->dependencies
25199 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
25200 NULL, &cu->comp_unit_obstack,
25201 hashtab_obstack_allocate,
25202 dummy_obstack_deallocate);
25203
25204 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
25205 if (*slot == NULL)
25206 *slot = ref_per_cu;
25207}
1c379e20 25208
f504f079
DE
25209/* Subroutine of dwarf2_mark to pass to htab_traverse.
25210 Set the mark field in every compilation unit in the
ae038cb0
DJ
25211 cache that we must keep because we are keeping CU. */
25212
10b3939b
DJ
25213static int
25214dwarf2_mark_helper (void **slot, void *data)
25215{
25216 struct dwarf2_per_cu_data *per_cu;
25217
25218 per_cu = (struct dwarf2_per_cu_data *) *slot;
d07ed419
JK
25219
25220 /* cu->dependencies references may not yet have been ever read if QUIT aborts
25221 reading of the chain. As such dependencies remain valid it is not much
25222 useful to track and undo them during QUIT cleanups. */
25223 if (per_cu->cu == NULL)
25224 return 1;
25225
10b3939b
DJ
25226 if (per_cu->cu->mark)
25227 return 1;
25228 per_cu->cu->mark = 1;
25229
25230 if (per_cu->cu->dependencies != NULL)
25231 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
25232
25233 return 1;
25234}
25235
f504f079
DE
25236/* Set the mark field in CU and in every other compilation unit in the
25237 cache that we must keep because we are keeping CU. */
25238
ae038cb0
DJ
25239static void
25240dwarf2_mark (struct dwarf2_cu *cu)
25241{
25242 if (cu->mark)
25243 return;
25244 cu->mark = 1;
10b3939b
DJ
25245 if (cu->dependencies != NULL)
25246 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
ae038cb0
DJ
25247}
25248
25249static void
25250dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
25251{
25252 while (per_cu)
25253 {
25254 per_cu->cu->mark = 0;
25255 per_cu = per_cu->cu->read_in_chain;
25256 }
72bf9492
DJ
25257}
25258
72bf9492
DJ
25259/* Trivial hash function for partial_die_info: the hash value of a DIE
25260 is its offset in .debug_info for this objfile. */
25261
25262static hashval_t
25263partial_die_hash (const void *item)
25264{
9a3c8263
SM
25265 const struct partial_die_info *part_die
25266 = (const struct partial_die_info *) item;
9a619af0 25267
9c541725 25268 return to_underlying (part_die->sect_off);
72bf9492
DJ
25269}
25270
25271/* Trivial comparison function for partial_die_info structures: two DIEs
25272 are equal if they have the same offset. */
25273
25274static int
25275partial_die_eq (const void *item_lhs, const void *item_rhs)
25276{
9a3c8263
SM
25277 const struct partial_die_info *part_die_lhs
25278 = (const struct partial_die_info *) item_lhs;
25279 const struct partial_die_info *part_die_rhs
25280 = (const struct partial_die_info *) item_rhs;
9a619af0 25281
9c541725 25282 return part_die_lhs->sect_off == part_die_rhs->sect_off;
72bf9492
DJ
25283}
25284
b4f54984
DE
25285static struct cmd_list_element *set_dwarf_cmdlist;
25286static struct cmd_list_element *show_dwarf_cmdlist;
ae038cb0
DJ
25287
25288static void
981a3fb3 25289set_dwarf_cmd (const char *args, int from_tty)
ae038cb0 25290{
b4f54984 25291 help_list (set_dwarf_cmdlist, "maintenance set dwarf ", all_commands,
635c7e8a 25292 gdb_stdout);
ae038cb0
DJ
25293}
25294
25295static void
981a3fb3 25296show_dwarf_cmd (const char *args, int from_tty)
6e70227d 25297{
b4f54984 25298 cmd_show_list (show_dwarf_cmdlist, from_tty, "");
ae038cb0
DJ
25299}
25300
cd4fb1b2 25301int dwarf_always_disassemble;
437afbb8 25302
437afbb8 25303static void
cd4fb1b2
SM
25304show_dwarf_always_disassemble (struct ui_file *file, int from_tty,
25305 struct cmd_list_element *c, const char *value)
9291a0cd 25306{
cd4fb1b2
SM
25307 fprintf_filtered (file,
25308 _("Whether to always disassemble "
25309 "DWARF expressions is %s.\n"),
25310 value);
9291a0cd
TT
25311}
25312
9291a0cd 25313static void
cd4fb1b2
SM
25314show_check_physname (struct ui_file *file, int from_tty,
25315 struct cmd_list_element *c, const char *value)
9291a0cd 25316{
cd4fb1b2
SM
25317 fprintf_filtered (file,
25318 _("Whether to check \"physname\" is %s.\n"),
25319 value);
9291a0cd
TT
25320}
25321
cd4fb1b2
SM
25322void
25323_initialize_dwarf2_read (void)
9291a0cd 25324{
d95d3aef
TT
25325 dwarf2_objfile_data_key
25326 = register_objfile_data_with_cleanup (nullptr, dwarf2_free_objfile);
156942c7 25327
cd4fb1b2
SM
25328 add_prefix_cmd ("dwarf", class_maintenance, set_dwarf_cmd, _("\
25329Set DWARF specific variables.\n\
25330Configure DWARF variables such as the cache size"),
25331 &set_dwarf_cmdlist, "maintenance set dwarf ",
25332 0/*allow-unknown*/, &maintenance_set_cmdlist);
156942c7 25333
cd4fb1b2
SM
25334 add_prefix_cmd ("dwarf", class_maintenance, show_dwarf_cmd, _("\
25335Show DWARF specific variables\n\
25336Show DWARF variables such as the cache size"),
25337 &show_dwarf_cmdlist, "maintenance show dwarf ",
25338 0/*allow-unknown*/, &maintenance_show_cmdlist);
156942c7 25339
cd4fb1b2
SM
25340 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
25341 &dwarf_max_cache_age, _("\
25342Set the upper bound on the age of cached DWARF compilation units."), _("\
25343Show the upper bound on the age of cached DWARF compilation units."), _("\
25344A higher limit means that cached compilation units will be stored\n\
25345in memory longer, and more total memory will be used. Zero disables\n\
25346caching, which can slow down startup."),
25347 NULL,
25348 show_dwarf_max_cache_age,
25349 &set_dwarf_cmdlist,
25350 &show_dwarf_cmdlist);
156942c7 25351
cd4fb1b2
SM
25352 add_setshow_boolean_cmd ("always-disassemble", class_obscure,
25353 &dwarf_always_disassemble, _("\
25354Set whether `info address' always disassembles DWARF expressions."), _("\
25355Show whether `info address' always disassembles DWARF expressions."), _("\
25356When enabled, DWARF expressions are always printed in an assembly-like\n\
25357syntax. When disabled, expressions will be printed in a more\n\
25358conversational style, when possible."),
25359 NULL,
25360 show_dwarf_always_disassemble,
25361 &set_dwarf_cmdlist,
25362 &show_dwarf_cmdlist);
9291a0cd 25363
cd4fb1b2
SM
25364 add_setshow_zuinteger_cmd ("dwarf-read", no_class, &dwarf_read_debug, _("\
25365Set debugging of the DWARF reader."), _("\
25366Show debugging of the DWARF reader."), _("\
25367When enabled (non-zero), debugging messages are printed during DWARF\n\
25368reading and symtab expansion. A value of 1 (one) provides basic\n\
25369information. A value greater than 1 provides more verbose information."),
25370 NULL,
25371 NULL,
25372 &setdebuglist, &showdebuglist);
9291a0cd 25373
cd4fb1b2
SM
25374 add_setshow_zuinteger_cmd ("dwarf-die", no_class, &dwarf_die_debug, _("\
25375Set debugging of the DWARF DIE reader."), _("\
25376Show debugging of the DWARF DIE reader."), _("\
25377When enabled (non-zero), DIEs are dumped after they are read in.\n\
25378The value is the maximum depth to print."),
25379 NULL,
25380 NULL,
25381 &setdebuglist, &showdebuglist);
9291a0cd 25382
cd4fb1b2
SM
25383 add_setshow_zuinteger_cmd ("dwarf-line", no_class, &dwarf_line_debug, _("\
25384Set debugging of the dwarf line reader."), _("\
25385Show debugging of the dwarf line reader."), _("\
25386When enabled (non-zero), line number entries are dumped as they are read in.\n\
25387A value of 1 (one) provides basic information.\n\
25388A value greater than 1 provides more verbose information."),
25389 NULL,
25390 NULL,
25391 &setdebuglist, &showdebuglist);
437afbb8 25392
cd4fb1b2
SM
25393 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
25394Set cross-checking of \"physname\" code against demangler."), _("\
25395Show cross-checking of \"physname\" code against demangler."), _("\
25396When enabled, GDB's internal \"physname\" code is checked against\n\
25397the demangler."),
25398 NULL, show_check_physname,
25399 &setdebuglist, &showdebuglist);
900e11f9 25400
e615022a
DE
25401 add_setshow_boolean_cmd ("use-deprecated-index-sections",
25402 no_class, &use_deprecated_index_sections, _("\
25403Set whether to use deprecated gdb_index sections."), _("\
25404Show whether to use deprecated gdb_index sections."), _("\
25405When enabled, deprecated .gdb_index sections are used anyway.\n\
25406Normally they are ignored either because of a missing feature or\n\
25407performance issue.\n\
25408Warning: This option must be enabled before gdb reads the file."),
25409 NULL,
25410 NULL,
25411 &setlist, &showlist);
25412
f1e6e072
TT
25413 dwarf2_locexpr_index = register_symbol_computed_impl (LOC_COMPUTED,
25414 &dwarf2_locexpr_funcs);
25415 dwarf2_loclist_index = register_symbol_computed_impl (LOC_COMPUTED,
25416 &dwarf2_loclist_funcs);
25417
25418 dwarf2_locexpr_block_index = register_symbol_block_impl (LOC_BLOCK,
25419 &dwarf2_block_frame_base_locexpr_funcs);
25420 dwarf2_loclist_block_index = register_symbol_block_impl (LOC_BLOCK,
25421 &dwarf2_block_frame_base_loclist_funcs);
c62446b1
PA
25422
25423#if GDB_SELF_TEST
25424 selftests::register_test ("dw2_expand_symtabs_matching",
25425 selftests::dw2_expand_symtabs_matching::run_test);
25426#endif
6502dd73 25427}
This page took 6.090127 seconds and 4 git commands to generate.