Fix crash in quirk_rust_enum
[deliverable/binutils-gdb.git] / gdb / dwarf2read.c
CommitLineData
c906108c 1/* DWARF 2 debugging format support for GDB.
917c78fc 2
e2882c85 3 Copyright (C) 1994-2018 Free Software Foundation, Inc.
c906108c
SS
4
5 Adapted by Gary Funck (gary@intrepid.com), Intrepid Technology,
6 Inc. with support from Florida State University (under contract
7 with the Ada Joint Program Office), and Silicon Graphics, Inc.
8 Initial contribution by Brent Benson, Harris Computer Systems, Inc.,
9 based on Fred Fish's (Cygnus Support) implementation of DWARF 1
7ce59000 10 support.
c906108c 11
c5aa993b 12 This file is part of GDB.
c906108c 13
c5aa993b
JM
14 This program is free software; you can redistribute it and/or modify
15 it under the terms of the GNU General Public License as published by
a9762ec7
JB
16 the Free Software Foundation; either version 3 of the License, or
17 (at your option) any later version.
c906108c 18
a9762ec7
JB
19 This program is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 GNU General Public License for more details.
c906108c 23
c5aa993b 24 You should have received a copy of the GNU General Public License
a9762ec7 25 along with this program. If not, see <http://www.gnu.org/licenses/>. */
c906108c 26
21b2bd31
DE
27/* FIXME: Various die-reading functions need to be more careful with
28 reading off the end of the section.
29 E.g., load_partial_dies, read_partial_die. */
30
c906108c 31#include "defs.h"
cd4fb1b2
SM
32#include "dwarf2read.h"
33#include "dwarf-index-common.h"
c906108c 34#include "bfd.h"
80626a55 35#include "elf-bfd.h"
c906108c
SS
36#include "symtab.h"
37#include "gdbtypes.h"
c906108c 38#include "objfiles.h"
fa8f86ff 39#include "dwarf2.h"
c906108c
SS
40#include "buildsym.h"
41#include "demangle.h"
50f182aa 42#include "gdb-demangle.h"
c906108c 43#include "expression.h"
d5166ae1 44#include "filenames.h" /* for DOSish file names */
2e276125 45#include "macrotab.h"
c906108c
SS
46#include "language.h"
47#include "complaints.h"
357e46e7 48#include "bcache.h"
4c2df51b
DJ
49#include "dwarf2expr.h"
50#include "dwarf2loc.h"
9219021c 51#include "cp-support.h"
72bf9492 52#include "hashtab.h"
ae038cb0
DJ
53#include "command.h"
54#include "gdbcmd.h"
edb3359d 55#include "block.h"
ff013f42 56#include "addrmap.h"
94af9270 57#include "typeprint.h"
ccefe4c4 58#include "psympriv.h"
53ce3c39 59#include <sys/stat.h>
96d19272 60#include "completer.h"
34eaf542 61#include "vec.h"
98bfdba5 62#include "c-lang.h"
a766d390 63#include "go-lang.h"
98bfdba5 64#include "valprint.h"
3019eac3 65#include "gdbcore.h" /* for gnutarget */
156942c7 66#include "gdb/gdb-index.h"
60d5a603 67#include <ctype.h>
cbb099e8 68#include "gdb_bfd.h"
4357ac6c 69#include "f-lang.h"
05cba821 70#include "source.h"
614c279d 71#include "filestuff.h"
dc294be5 72#include "build-id.h"
22cee43f 73#include "namespace.h"
bef155c3 74#include "common/gdb_unlinker.h"
14bc53a8 75#include "common/function-view.h"
ecfb656c
PA
76#include "common/gdb_optional.h"
77#include "common/underlying.h"
d5722aa2 78#include "common/byte-vector.h"
927aa2e7 79#include "common/hash_enum.h"
bbf2f4df 80#include "filename-seen-cache.h"
b32b108a 81#include "producer.h"
c906108c 82#include <fcntl.h>
c906108c 83#include <sys/types.h>
325fac50 84#include <algorithm>
bc8f2430
JK
85#include <unordered_set>
86#include <unordered_map>
c62446b1 87#include "selftest.h"
437afbb8
JK
88#include <cmath>
89#include <set>
90#include <forward_list>
c9317f21 91#include "rust-lang.h"
b4987c95 92#include "common/pathstuff.h"
437afbb8 93
73be47f5
DE
94/* When == 1, print basic high level tracing messages.
95 When > 1, be more verbose.
b4f54984
DE
96 This is in contrast to the low level DIE reading of dwarf_die_debug. */
97static unsigned int dwarf_read_debug = 0;
45cfd468 98
d97bc12b 99/* When non-zero, dump DIEs after they are read in. */
b4f54984 100static unsigned int dwarf_die_debug = 0;
d97bc12b 101
27e0867f
DE
102/* When non-zero, dump line number entries as they are read in. */
103static unsigned int dwarf_line_debug = 0;
104
900e11f9
JK
105/* When non-zero, cross-check physname against demangler. */
106static int check_physname = 0;
107
481860b3 108/* When non-zero, do not reject deprecated .gdb_index sections. */
e615022a 109static int use_deprecated_index_sections = 0;
481860b3 110
6502dd73
DJ
111static const struct objfile_data *dwarf2_objfile_data_key;
112
f1e6e072
TT
113/* The "aclass" indices for various kinds of computed DWARF symbols. */
114
115static int dwarf2_locexpr_index;
116static int dwarf2_loclist_index;
117static int dwarf2_locexpr_block_index;
118static int dwarf2_loclist_block_index;
119
3f563c84
PA
120/* An index into a (C++) symbol name component in a symbol name as
121 recorded in the mapped_index's symbol table. For each C++ symbol
122 in the symbol table, we record one entry for the start of each
123 component in the symbol in a table of name components, and then
124 sort the table, in order to be able to binary search symbol names,
125 ignoring leading namespaces, both completion and regular look up.
126 For example, for symbol "A::B::C", we'll have an entry that points
127 to "A::B::C", another that points to "B::C", and another for "C".
128 Note that function symbols in GDB index have no parameter
129 information, just the function/method names. You can convert a
130 name_component to a "const char *" using the
131 'mapped_index::symbol_name_at(offset_type)' method. */
132
133struct name_component
134{
135 /* Offset in the symbol name where the component starts. Stored as
136 a (32-bit) offset instead of a pointer to save memory and improve
137 locality on 64-bit architectures. */
138 offset_type name_offset;
139
140 /* The symbol's index in the symbol and constant pool tables of a
141 mapped_index. */
142 offset_type idx;
143};
144
44ed8f3e
PA
145/* Base class containing bits shared by both .gdb_index and
146 .debug_name indexes. */
147
148struct mapped_index_base
149{
150 /* The name_component table (a sorted vector). See name_component's
151 description above. */
152 std::vector<name_component> name_components;
153
154 /* How NAME_COMPONENTS is sorted. */
155 enum case_sensitivity name_components_casing;
156
157 /* Return the number of names in the symbol table. */
158 virtual size_t symbol_name_count () const = 0;
159
160 /* Get the name of the symbol at IDX in the symbol table. */
161 virtual const char *symbol_name_at (offset_type idx) const = 0;
162
163 /* Return whether the name at IDX in the symbol table should be
164 ignored. */
165 virtual bool symbol_name_slot_invalid (offset_type idx) const
166 {
167 return false;
168 }
169
170 /* Build the symbol name component sorted vector, if we haven't
171 yet. */
172 void build_name_components ();
173
174 /* Returns the lower (inclusive) and upper (exclusive) bounds of the
175 possible matches for LN_NO_PARAMS in the name component
176 vector. */
177 std::pair<std::vector<name_component>::const_iterator,
178 std::vector<name_component>::const_iterator>
179 find_name_components_bounds (const lookup_name_info &ln_no_params) const;
180
181 /* Prevent deleting/destroying via a base class pointer. */
182protected:
183 ~mapped_index_base() = default;
184};
185
9291a0cd
TT
186/* A description of the mapped index. The file format is described in
187 a comment by the code that writes the index. */
fc898b42 188struct mapped_index final : public mapped_index_base
9291a0cd 189{
f00a2de2
PA
190 /* A slot/bucket in the symbol table hash. */
191 struct symbol_table_slot
192 {
193 const offset_type name;
194 const offset_type vec;
195 };
196
559a7a62
JK
197 /* Index data format version. */
198 int version;
199
9291a0cd
TT
200 /* The total length of the buffer. */
201 off_t total_size;
b11b1f88 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
TT
209 /* A pointer to the constant pool. */
210 const char *constant_pool;
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{
813 /* Name of the file. */
814 const char *name;
815
73869dc2
DE
816 /* File format version. */
817 int version;
818
93417882 819 /* The bfd. */
80626a55
DE
820 bfd *dbfd;
821
822 /* Section info for this file. */
823 struct dwp_sections sections;
824
57d63ce2 825 /* Table of CUs in the file. */
80626a55
DE
826 const struct dwp_hash_table *cus;
827
828 /* Table of TUs in the file. */
829 const struct dwp_hash_table *tus;
830
19ac8c2e
DE
831 /* Tables of loaded CUs/TUs. Each entry is a struct dwo_unit *. */
832 htab_t loaded_cus;
833 htab_t loaded_tus;
80626a55 834
73869dc2
DE
835 /* Table to map ELF section numbers to their sections.
836 This is only needed for the DWP V1 file format. */
80626a55
DE
837 unsigned int num_sections;
838 asection **elf_sections;
839};
840
36586728
TT
841/* This represents a '.dwz' file. */
842
843struct dwz_file
844{
845 /* A dwz file can only contain a few sections. */
846 struct dwarf2_section_info abbrev;
847 struct dwarf2_section_info info;
848 struct dwarf2_section_info str;
849 struct dwarf2_section_info line;
850 struct dwarf2_section_info macro;
2ec9a5e0 851 struct dwarf2_section_info gdb_index;
927aa2e7 852 struct dwarf2_section_info debug_names;
36586728
TT
853
854 /* The dwz's BFD. */
855 bfd *dwz_bfd;
856};
857
0963b4bd
MS
858/* Struct used to pass misc. parameters to read_die_and_children, et
859 al. which are used for both .debug_info and .debug_types dies.
860 All parameters here are unchanging for the life of the call. This
dee91e82 861 struct exists to abstract away the constant parameters of die reading. */
93311388
DE
862
863struct die_reader_specs
864{
a32a8923 865 /* The bfd of die_section. */
93311388
DE
866 bfd* abfd;
867
868 /* The CU of the DIE we are parsing. */
869 struct dwarf2_cu *cu;
870
80626a55 871 /* Non-NULL if reading a DWO file (including one packaged into a DWP). */
3019eac3
DE
872 struct dwo_file *dwo_file;
873
dee91e82 874 /* The section the die comes from.
3019eac3 875 This is either .debug_info or .debug_types, or the .dwo variants. */
dee91e82
DE
876 struct dwarf2_section_info *die_section;
877
878 /* die_section->buffer. */
d521ce57 879 const gdb_byte *buffer;
f664829e
DE
880
881 /* The end of the buffer. */
882 const gdb_byte *buffer_end;
a2ce51a0
DE
883
884 /* The value of the DW_AT_comp_dir attribute. */
885 const char *comp_dir;
685af9cd
TT
886
887 /* The abbreviation table to use when reading the DIEs. */
888 struct abbrev_table *abbrev_table;
93311388
DE
889};
890
fd820528 891/* Type of function passed to init_cutu_and_read_dies, et.al. */
dee91e82 892typedef void (die_reader_func_ftype) (const struct die_reader_specs *reader,
d521ce57 893 const gdb_byte *info_ptr,
dee91e82
DE
894 struct die_info *comp_unit_die,
895 int has_children,
896 void *data);
897
ecfb656c
PA
898/* A 1-based directory index. This is a strong typedef to prevent
899 accidentally using a directory index as a 0-based index into an
900 array/vector. */
901enum class dir_index : unsigned int {};
902
903/* Likewise, a 1-based file name index. */
904enum class file_name_index : unsigned int {};
905
52059ffd
TT
906struct file_entry
907{
fff8551c
PA
908 file_entry () = default;
909
ecfb656c 910 file_entry (const char *name_, dir_index d_index_,
fff8551c
PA
911 unsigned int mod_time_, unsigned int length_)
912 : name (name_),
ecfb656c 913 d_index (d_index_),
fff8551c
PA
914 mod_time (mod_time_),
915 length (length_)
916 {}
917
ecfb656c
PA
918 /* Return the include directory at D_INDEX stored in LH. Returns
919 NULL if D_INDEX is out of bounds. */
8c43009f
PA
920 const char *include_dir (const line_header *lh) const;
921
fff8551c
PA
922 /* The file name. Note this is an observing pointer. The memory is
923 owned by debug_line_buffer. */
924 const char *name {};
925
8c43009f 926 /* The directory index (1-based). */
ecfb656c 927 dir_index d_index {};
fff8551c
PA
928
929 unsigned int mod_time {};
930
931 unsigned int length {};
932
933 /* True if referenced by the Line Number Program. */
934 bool included_p {};
935
83769d0b 936 /* The associated symbol table, if any. */
fff8551c 937 struct symtab *symtab {};
52059ffd
TT
938};
939
debd256d
JB
940/* The line number information for a compilation unit (found in the
941 .debug_line section) begins with a "statement program header",
942 which contains the following information. */
943struct line_header
944{
fff8551c
PA
945 line_header ()
946 : offset_in_dwz {}
947 {}
948
949 /* Add an entry to the include directory table. */
950 void add_include_dir (const char *include_dir);
951
952 /* Add an entry to the file name table. */
ecfb656c 953 void add_file_name (const char *name, dir_index d_index,
fff8551c
PA
954 unsigned int mod_time, unsigned int length);
955
ecfb656c 956 /* Return the include dir at INDEX (1-based). Returns NULL if INDEX
8c43009f 957 is out of bounds. */
ecfb656c 958 const char *include_dir_at (dir_index index) const
8c43009f 959 {
ecfb656c
PA
960 /* Convert directory index number (1-based) to vector index
961 (0-based). */
962 size_t vec_index = to_underlying (index) - 1;
963
964 if (vec_index >= include_dirs.size ())
8c43009f 965 return NULL;
ecfb656c 966 return include_dirs[vec_index];
8c43009f
PA
967 }
968
ecfb656c 969 /* Return the file name at INDEX (1-based). Returns NULL if INDEX
8c43009f 970 is out of bounds. */
ecfb656c 971 file_entry *file_name_at (file_name_index index)
8c43009f 972 {
ecfb656c
PA
973 /* Convert file name index number (1-based) to vector index
974 (0-based). */
975 size_t vec_index = to_underlying (index) - 1;
976
977 if (vec_index >= file_names.size ())
fff8551c 978 return NULL;
ecfb656c 979 return &file_names[vec_index];
fff8551c
PA
980 }
981
982 /* Const version of the above. */
983 const file_entry *file_name_at (unsigned int index) const
984 {
985 if (index >= file_names.size ())
8c43009f
PA
986 return NULL;
987 return &file_names[index];
988 }
989
527f3840 990 /* Offset of line number information in .debug_line section. */
9c541725 991 sect_offset sect_off {};
527f3840
JK
992
993 /* OFFSET is for struct dwz_file associated with dwarf2_per_objfile. */
fff8551c
PA
994 unsigned offset_in_dwz : 1; /* Can't initialize bitfields in-class. */
995
996 unsigned int total_length {};
997 unsigned short version {};
998 unsigned int header_length {};
999 unsigned char minimum_instruction_length {};
1000 unsigned char maximum_ops_per_instruction {};
1001 unsigned char default_is_stmt {};
1002 int line_base {};
1003 unsigned char line_range {};
1004 unsigned char opcode_base {};
debd256d
JB
1005
1006 /* standard_opcode_lengths[i] is the number of operands for the
1007 standard opcode whose value is i. This means that
1008 standard_opcode_lengths[0] is unused, and the last meaningful
1009 element is standard_opcode_lengths[opcode_base - 1]. */
fff8551c 1010 std::unique_ptr<unsigned char[]> standard_opcode_lengths;
debd256d 1011
fff8551c
PA
1012 /* The include_directories table. Note these are observing
1013 pointers. The memory is owned by debug_line_buffer. */
1014 std::vector<const char *> include_dirs;
debd256d 1015
fff8551c
PA
1016 /* The file_names table. */
1017 std::vector<file_entry> file_names;
debd256d
JB
1018
1019 /* The start and end of the statement program following this
6502dd73 1020 header. These point into dwarf2_per_objfile->line_buffer. */
fff8551c 1021 const gdb_byte *statement_program_start {}, *statement_program_end {};
debd256d 1022};
c906108c 1023
fff8551c
PA
1024typedef std::unique_ptr<line_header> line_header_up;
1025
8c43009f
PA
1026const char *
1027file_entry::include_dir (const line_header *lh) const
1028{
ecfb656c 1029 return lh->include_dir_at (d_index);
8c43009f
PA
1030}
1031
c906108c 1032/* When we construct a partial symbol table entry we only
0963b4bd 1033 need this much information. */
6f06d47b 1034struct partial_die_info : public allocate_on_obstack
c906108c 1035 {
6f06d47b
YQ
1036 partial_die_info (sect_offset sect_off, struct abbrev_info *abbrev);
1037
1038 /* Disable assign but still keep copy ctor, which is needed
1039 load_partial_dies. */
1040 partial_die_info& operator=(const partial_die_info& rhs) = delete;
1041
52356b79
YQ
1042 /* Adjust the partial die before generating a symbol for it. This
1043 function may set the is_external flag or change the DIE's
1044 name. */
1045 void fixup (struct dwarf2_cu *cu);
1046
48fbe735
YQ
1047 /* Read a minimal amount of information into the minimal die
1048 structure. */
1049 const gdb_byte *read (const struct die_reader_specs *reader,
1050 const struct abbrev_info &abbrev,
1051 const gdb_byte *info_ptr);
1052
72bf9492 1053 /* Offset of this DIE. */
6f06d47b 1054 const sect_offset sect_off;
72bf9492
DJ
1055
1056 /* DWARF-2 tag for this DIE. */
6f06d47b 1057 const ENUM_BITFIELD(dwarf_tag) tag : 16;
72bf9492 1058
72bf9492 1059 /* Assorted flags describing the data found in this DIE. */
6f06d47b
YQ
1060 const unsigned int has_children : 1;
1061
72bf9492
DJ
1062 unsigned int is_external : 1;
1063 unsigned int is_declaration : 1;
1064 unsigned int has_type : 1;
1065 unsigned int has_specification : 1;
1066 unsigned int has_pc_info : 1;
481860b3 1067 unsigned int may_be_inlined : 1;
72bf9492 1068
0c1b455e
TT
1069 /* This DIE has been marked DW_AT_main_subprogram. */
1070 unsigned int main_subprogram : 1;
1071
72bf9492
DJ
1072 /* Flag set if the SCOPE field of this structure has been
1073 computed. */
1074 unsigned int scope_set : 1;
1075
fa4028e9
JB
1076 /* Flag set if the DIE has a byte_size attribute. */
1077 unsigned int has_byte_size : 1;
1078
ff908ebf
AW
1079 /* Flag set if the DIE has a DW_AT_const_value attribute. */
1080 unsigned int has_const_value : 1;
1081
98bfdba5
PA
1082 /* Flag set if any of the DIE's children are template arguments. */
1083 unsigned int has_template_arguments : 1;
1084
52356b79 1085 /* Flag set if fixup has been called on this die. */
abc72ce4
DE
1086 unsigned int fixup_called : 1;
1087
36586728
TT
1088 /* Flag set if DW_TAG_imported_unit uses DW_FORM_GNU_ref_alt. */
1089 unsigned int is_dwz : 1;
1090
1091 /* Flag set if spec_offset uses DW_FORM_GNU_ref_alt. */
1092 unsigned int spec_is_dwz : 1;
1093
72bf9492 1094 /* The name of this DIE. Normally the value of DW_AT_name, but
94af9270 1095 sometimes a default name for unnamed DIEs. */
6f06d47b 1096 const char *name = nullptr;
72bf9492 1097
abc72ce4 1098 /* The linkage name, if present. */
6f06d47b 1099 const char *linkage_name = nullptr;
abc72ce4 1100
72bf9492
DJ
1101 /* The scope to prepend to our children. This is generally
1102 allocated on the comp_unit_obstack, so will disappear
1103 when this compilation unit leaves the cache. */
6f06d47b 1104 const char *scope = nullptr;
72bf9492 1105
95554aad
TT
1106 /* Some data associated with the partial DIE. The tag determines
1107 which field is live. */
1108 union
1109 {
1110 /* The location description associated with this DIE, if any. */
1111 struct dwarf_block *locdesc;
1112 /* The offset of an import, for DW_TAG_imported_unit. */
9c541725 1113 sect_offset sect_off;
6f06d47b 1114 } d {};
72bf9492
DJ
1115
1116 /* If HAS_PC_INFO, the PC range associated with this DIE. */
6f06d47b
YQ
1117 CORE_ADDR lowpc = 0;
1118 CORE_ADDR highpc = 0;
72bf9492 1119
93311388 1120 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
72bf9492 1121 DW_AT_sibling, if any. */
48fbe735
YQ
1122 /* NOTE: This member isn't strictly necessary, partial_die_info::read
1123 could return DW_AT_sibling values to its caller load_partial_dies. */
6f06d47b 1124 const gdb_byte *sibling = nullptr;
72bf9492
DJ
1125
1126 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
1127 DW_AT_specification (or DW_AT_abstract_origin or
1128 DW_AT_extension). */
6f06d47b 1129 sect_offset spec_offset {};
72bf9492
DJ
1130
1131 /* Pointers to this DIE's parent, first child, and next sibling,
1132 if any. */
6f06d47b
YQ
1133 struct partial_die_info *die_parent = nullptr;
1134 struct partial_die_info *die_child = nullptr;
1135 struct partial_die_info *die_sibling = nullptr;
1136
1137 friend struct partial_die_info *
1138 dwarf2_cu::find_partial_die (sect_offset sect_off);
1139
1140 private:
1141 /* Only need to do look up in dwarf2_cu::find_partial_die. */
1142 partial_die_info (sect_offset sect_off)
1143 : partial_die_info (sect_off, DW_TAG_padding, 0)
1144 {
1145 }
1146
1147 partial_die_info (sect_offset sect_off_, enum dwarf_tag tag_,
1148 int has_children_)
1149 : sect_off (sect_off_), tag (tag_), has_children (has_children_)
1150 {
1151 is_external = 0;
1152 is_declaration = 0;
1153 has_type = 0;
1154 has_specification = 0;
1155 has_pc_info = 0;
1156 may_be_inlined = 0;
1157 main_subprogram = 0;
1158 scope_set = 0;
1159 has_byte_size = 0;
1160 has_const_value = 0;
1161 has_template_arguments = 0;
1162 fixup_called = 0;
1163 is_dwz = 0;
1164 spec_is_dwz = 0;
1165 }
c906108c
SS
1166 };
1167
0963b4bd 1168/* This data structure holds the information of an abbrev. */
c906108c
SS
1169struct abbrev_info
1170 {
1171 unsigned int number; /* number identifying abbrev */
1172 enum dwarf_tag tag; /* dwarf tag */
f3dd6933
DJ
1173 unsigned short has_children; /* boolean */
1174 unsigned short num_attrs; /* number of attributes */
c906108c
SS
1175 struct attr_abbrev *attrs; /* an array of attribute descriptions */
1176 struct abbrev_info *next; /* next in chain */
1177 };
1178
1179struct attr_abbrev
1180 {
9d25dd43
DE
1181 ENUM_BITFIELD(dwarf_attribute) name : 16;
1182 ENUM_BITFIELD(dwarf_form) form : 16;
43988095
JK
1183
1184 /* It is valid only if FORM is DW_FORM_implicit_const. */
1185 LONGEST implicit_const;
c906108c
SS
1186 };
1187
433df2d4
DE
1188/* Size of abbrev_table.abbrev_hash_table. */
1189#define ABBREV_HASH_SIZE 121
1190
1191/* Top level data structure to contain an abbreviation table. */
1192
1193struct abbrev_table
1194{
685af9cd
TT
1195 explicit abbrev_table (sect_offset off)
1196 : sect_off (off)
1197 {
4a17f768 1198 m_abbrevs =
685af9cd 1199 XOBNEWVEC (&abbrev_obstack, struct abbrev_info *, ABBREV_HASH_SIZE);
4a17f768 1200 memset (m_abbrevs, 0, ABBREV_HASH_SIZE * sizeof (struct abbrev_info *));
685af9cd
TT
1201 }
1202
1203 DISABLE_COPY_AND_ASSIGN (abbrev_table);
1204
1205 /* Allocate space for a struct abbrev_info object in
1206 ABBREV_TABLE. */
1207 struct abbrev_info *alloc_abbrev ();
1208
1209 /* Add an abbreviation to the table. */
1210 void add_abbrev (unsigned int abbrev_number, struct abbrev_info *abbrev);
1211
1212 /* Look up an abbrev in the table.
1213 Returns NULL if the abbrev is not found. */
1214
1215 struct abbrev_info *lookup_abbrev (unsigned int abbrev_number);
1216
1217
f4dc4d17
DE
1218 /* Where the abbrev table came from.
1219 This is used as a sanity check when the table is used. */
685af9cd 1220 const sect_offset sect_off;
433df2d4
DE
1221
1222 /* Storage for the abbrev table. */
685af9cd 1223 auto_obstack abbrev_obstack;
433df2d4 1224
4a17f768
YQ
1225private:
1226
433df2d4
DE
1227 /* Hash table of abbrevs.
1228 This is an array of size ABBREV_HASH_SIZE allocated in abbrev_obstack.
1229 It could be statically allocated, but the previous code didn't so we
1230 don't either. */
4a17f768 1231 struct abbrev_info **m_abbrevs;
433df2d4
DE
1232};
1233
685af9cd
TT
1234typedef std::unique_ptr<struct abbrev_table> abbrev_table_up;
1235
0963b4bd 1236/* Attributes have a name and a value. */
b60c80d6
DJ
1237struct attribute
1238 {
9d25dd43 1239 ENUM_BITFIELD(dwarf_attribute) name : 16;
8285870a
JK
1240 ENUM_BITFIELD(dwarf_form) form : 15;
1241
1242 /* Has DW_STRING already been updated by dwarf2_canonicalize_name? This
1243 field should be in u.str (existing only for DW_STRING) but it is kept
1244 here for better struct attribute alignment. */
1245 unsigned int string_is_canonical : 1;
1246
b60c80d6
DJ
1247 union
1248 {
15d034d0 1249 const char *str;
b60c80d6 1250 struct dwarf_block *blk;
43bbcdc2
PH
1251 ULONGEST unsnd;
1252 LONGEST snd;
b60c80d6 1253 CORE_ADDR addr;
ac9ec31b 1254 ULONGEST signature;
b60c80d6
DJ
1255 }
1256 u;
1257 };
1258
0963b4bd 1259/* This data structure holds a complete die structure. */
c906108c
SS
1260struct die_info
1261 {
76815b17
DE
1262 /* DWARF-2 tag for this DIE. */
1263 ENUM_BITFIELD(dwarf_tag) tag : 16;
1264
1265 /* Number of attributes */
98bfdba5
PA
1266 unsigned char num_attrs;
1267
1268 /* True if we're presently building the full type name for the
1269 type derived from this DIE. */
1270 unsigned char building_fullname : 1;
76815b17 1271
adde2bff
DE
1272 /* True if this die is in process. PR 16581. */
1273 unsigned char in_process : 1;
1274
76815b17
DE
1275 /* Abbrev number */
1276 unsigned int abbrev;
1277
93311388 1278 /* Offset in .debug_info or .debug_types section. */
9c541725 1279 sect_offset sect_off;
78ba4af6
JB
1280
1281 /* The dies in a compilation unit form an n-ary tree. PARENT
1282 points to this die's parent; CHILD points to the first child of
1283 this node; and all the children of a given node are chained
4950bc1c 1284 together via their SIBLING fields. */
639d11d3
DC
1285 struct die_info *child; /* Its first child, if any. */
1286 struct die_info *sibling; /* Its next sibling, if any. */
1287 struct die_info *parent; /* Its parent, if any. */
c906108c 1288
b60c80d6
DJ
1289 /* An array of attributes, with NUM_ATTRS elements. There may be
1290 zero, but it's not common and zero-sized arrays are not
1291 sufficiently portable C. */
1292 struct attribute attrs[1];
c906108c
SS
1293 };
1294
0963b4bd 1295/* Get at parts of an attribute structure. */
c906108c
SS
1296
1297#define DW_STRING(attr) ((attr)->u.str)
8285870a 1298#define DW_STRING_IS_CANONICAL(attr) ((attr)->string_is_canonical)
c906108c
SS
1299#define DW_UNSND(attr) ((attr)->u.unsnd)
1300#define DW_BLOCK(attr) ((attr)->u.blk)
1301#define DW_SND(attr) ((attr)->u.snd)
1302#define DW_ADDR(attr) ((attr)->u.addr)
ac9ec31b 1303#define DW_SIGNATURE(attr) ((attr)->u.signature)
c906108c 1304
0963b4bd 1305/* Blocks are a bunch of untyped bytes. */
c906108c
SS
1306struct dwarf_block
1307 {
56eb65bd 1308 size_t size;
1d6edc3c
JK
1309
1310 /* Valid only if SIZE is not zero. */
d521ce57 1311 const gdb_byte *data;
c906108c
SS
1312 };
1313
c906108c
SS
1314#ifndef ATTR_ALLOC_CHUNK
1315#define ATTR_ALLOC_CHUNK 4
1316#endif
1317
c906108c
SS
1318/* Allocate fields for structs, unions and enums in this size. */
1319#ifndef DW_FIELD_ALLOC_CHUNK
1320#define DW_FIELD_ALLOC_CHUNK 4
1321#endif
1322
c906108c
SS
1323/* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
1324 but this would require a corresponding change in unpack_field_as_long
1325 and friends. */
1326static int bits_per_byte = 8;
1327
2ddeaf8a
TT
1328/* When reading a variant or variant part, we track a bit more
1329 information about the field, and store it in an object of this
1330 type. */
1331
1332struct variant_field
1333{
1334 /* If we see a DW_TAG_variant, then this will be the discriminant
1335 value. */
1336 ULONGEST discriminant_value;
1337 /* If we see a DW_TAG_variant, then this will be set if this is the
1338 default branch. */
1339 bool default_branch;
1340 /* While reading a DW_TAG_variant_part, this will be set if this
1341 field is the discriminant. */
1342 bool is_discriminant;
1343};
1344
52059ffd
TT
1345struct nextfield
1346{
be2daae6
TT
1347 int accessibility = 0;
1348 int virtuality = 0;
2ddeaf8a 1349 /* Extra information to describe a variant or variant part. */
be2daae6
TT
1350 struct variant_field variant {};
1351 struct field field {};
52059ffd
TT
1352};
1353
1354struct fnfieldlist
1355{
be2daae6
TT
1356 const char *name = nullptr;
1357 std::vector<struct fn_field> fnfields;
52059ffd
TT
1358};
1359
c906108c
SS
1360/* The routines that read and process dies for a C struct or C++ class
1361 pass lists of data member fields and lists of member function fields
1362 in an instance of a field_info structure, as defined below. */
1363struct field_info
c5aa993b 1364 {
0963b4bd 1365 /* List of data member and baseclasses fields. */
be2daae6
TT
1366 std::vector<struct nextfield> fields;
1367 std::vector<struct nextfield> baseclasses;
c906108c 1368
7d0ccb61 1369 /* Number of fields (including baseclasses). */
be2daae6 1370 int nfields = 0;
c906108c 1371
c5aa993b 1372 /* Set if the accesibility of one of the fields is not public. */
be2daae6 1373 int non_public_fields = 0;
c906108c 1374
c5aa993b
JM
1375 /* Member function fieldlist array, contains name of possibly overloaded
1376 member function, number of overloaded member functions and a pointer
1377 to the head of the member function field chain. */
be2daae6 1378 std::vector<struct fnfieldlist> fnfieldlists;
98751a41
JK
1379
1380 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
1381 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
be2daae6 1382 std::vector<struct decl_field> typedef_field_list;
883fd55a
KS
1383
1384 /* Nested types defined by this class and the number of elements in this
1385 list. */
be2daae6 1386 std::vector<struct decl_field> nested_types_list;
c5aa993b 1387 };
c906108c 1388
10b3939b
DJ
1389/* One item on the queue of compilation units to read in full symbols
1390 for. */
1391struct dwarf2_queue_item
1392{
1393 struct dwarf2_per_cu_data *per_cu;
95554aad 1394 enum language pretend_language;
10b3939b
DJ
1395 struct dwarf2_queue_item *next;
1396};
1397
1398/* The current queue. */
1399static struct dwarf2_queue_item *dwarf2_queue, *dwarf2_queue_tail;
1400
ae038cb0
DJ
1401/* Loaded secondary compilation units are kept in memory until they
1402 have not been referenced for the processing of this many
1403 compilation units. Set this to zero to disable caching. Cache
1404 sizes of up to at least twenty will improve startup time for
1405 typical inter-CU-reference binaries, at an obvious memory cost. */
b4f54984 1406static int dwarf_max_cache_age = 5;
920d2a44 1407static void
b4f54984
DE
1408show_dwarf_max_cache_age (struct ui_file *file, int from_tty,
1409 struct cmd_list_element *c, const char *value)
920d2a44 1410{
3e43a32a 1411 fprintf_filtered (file, _("The upper bound on the age of cached "
b4f54984 1412 "DWARF compilation units is %s.\n"),
920d2a44
AC
1413 value);
1414}
4390d890 1415\f
c906108c
SS
1416/* local function prototypes */
1417
a32a8923
DE
1418static const char *get_section_name (const struct dwarf2_section_info *);
1419
1420static const char *get_section_file_name (const struct dwarf2_section_info *);
1421
918dd910
JK
1422static void dwarf2_find_base_address (struct die_info *die,
1423 struct dwarf2_cu *cu);
1424
0018ea6f
DE
1425static struct partial_symtab *create_partial_symtab
1426 (struct dwarf2_per_cu_data *per_cu, const char *name);
1427
f1902523
JK
1428static void build_type_psymtabs_reader (const struct die_reader_specs *reader,
1429 const gdb_byte *info_ptr,
1430 struct die_info *type_unit_die,
1431 int has_children, void *data);
1432
ed2dc618
SM
1433static void dwarf2_build_psymtabs_hard
1434 (struct dwarf2_per_objfile *dwarf2_per_objfile);
c906108c 1435
72bf9492
DJ
1436static void scan_partial_symbols (struct partial_die_info *,
1437 CORE_ADDR *, CORE_ADDR *,
5734ee8b 1438 int, struct dwarf2_cu *);
c906108c 1439
72bf9492
DJ
1440static void add_partial_symbol (struct partial_die_info *,
1441 struct dwarf2_cu *);
63d06c5c 1442
72bf9492
DJ
1443static void add_partial_namespace (struct partial_die_info *pdi,
1444 CORE_ADDR *lowpc, CORE_ADDR *highpc,
cdc07690 1445 int set_addrmap, struct dwarf2_cu *cu);
63d06c5c 1446
5d7cb8df 1447static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
cdc07690 1448 CORE_ADDR *highpc, int set_addrmap,
5d7cb8df
JK
1449 struct dwarf2_cu *cu);
1450
72bf9492
DJ
1451static void add_partial_enumeration (struct partial_die_info *enum_pdi,
1452 struct dwarf2_cu *cu);
91c24f0a 1453
bc30ff58
JB
1454static void add_partial_subprogram (struct partial_die_info *pdi,
1455 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 1456 int need_pc, struct dwarf2_cu *cu);
bc30ff58 1457
257e7a09
YQ
1458static void dwarf2_read_symtab (struct partial_symtab *,
1459 struct objfile *);
c906108c 1460
a14ed312 1461static void psymtab_to_symtab_1 (struct partial_symtab *);
c906108c 1462
685af9cd 1463static abbrev_table_up abbrev_table_read_table
ed2dc618
SM
1464 (struct dwarf2_per_objfile *dwarf2_per_objfile, struct dwarf2_section_info *,
1465 sect_offset);
433df2d4 1466
d521ce57 1467static unsigned int peek_abbrev_code (bfd *, const gdb_byte *);
6caca83c 1468
dee91e82 1469static struct partial_die_info *load_partial_dies
d521ce57 1470 (const struct die_reader_specs *, const gdb_byte *, int);
72bf9492 1471
36586728 1472static struct partial_die_info *find_partial_die (sect_offset, int,
10b3939b 1473 struct dwarf2_cu *);
72bf9492 1474
d521ce57
TT
1475static const gdb_byte *read_attribute (const struct die_reader_specs *,
1476 struct attribute *, struct attr_abbrev *,
1477 const gdb_byte *);
a8329558 1478
a1855c1d 1479static unsigned int read_1_byte (bfd *, const gdb_byte *);
c906108c 1480
a1855c1d 1481static int read_1_signed_byte (bfd *, const gdb_byte *);
c906108c 1482
a1855c1d 1483static unsigned int read_2_bytes (bfd *, const gdb_byte *);
c906108c 1484
a1855c1d 1485static unsigned int read_4_bytes (bfd *, const gdb_byte *);
c906108c 1486
a1855c1d 1487static ULONGEST read_8_bytes (bfd *, const gdb_byte *);
c906108c 1488
d521ce57 1489static CORE_ADDR read_address (bfd *, const gdb_byte *ptr, struct dwarf2_cu *,
891d2f0b 1490 unsigned int *);
c906108c 1491
d521ce57 1492static LONGEST read_initial_length (bfd *, const gdb_byte *, unsigned int *);
c764a876
DE
1493
1494static LONGEST read_checked_initial_length_and_offset
d521ce57 1495 (bfd *, const gdb_byte *, const struct comp_unit_head *,
c764a876 1496 unsigned int *, unsigned int *);
613e1657 1497
d521ce57
TT
1498static LONGEST read_offset (bfd *, const gdb_byte *,
1499 const struct comp_unit_head *,
c764a876
DE
1500 unsigned int *);
1501
d521ce57 1502static LONGEST read_offset_1 (bfd *, const gdb_byte *, unsigned int);
613e1657 1503
ed2dc618
SM
1504static sect_offset read_abbrev_offset
1505 (struct dwarf2_per_objfile *dwarf2_per_objfile,
1506 struct dwarf2_section_info *, sect_offset);
f4dc4d17 1507
d521ce57 1508static const gdb_byte *read_n_bytes (bfd *, const gdb_byte *, unsigned int);
c906108c 1509
d521ce57 1510static const char *read_direct_string (bfd *, const gdb_byte *, unsigned int *);
c906108c 1511
ed2dc618
SM
1512static const char *read_indirect_string
1513 (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *, const gdb_byte *,
1514 const struct comp_unit_head *, unsigned int *);
4bdf3d34 1515
ed2dc618
SM
1516static const char *read_indirect_line_string
1517 (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *, const gdb_byte *,
1518 const struct comp_unit_head *, unsigned int *);
36586728 1519
ed2dc618
SM
1520static const char *read_indirect_string_at_offset
1521 (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *abfd,
1522 LONGEST str_offset);
927aa2e7 1523
ed2dc618
SM
1524static const char *read_indirect_string_from_dwz
1525 (struct objfile *objfile, struct dwz_file *, LONGEST);
c906108c 1526
d521ce57 1527static LONGEST read_signed_leb128 (bfd *, const gdb_byte *, unsigned int *);
c906108c 1528
d521ce57
TT
1529static CORE_ADDR read_addr_index_from_leb128 (struct dwarf2_cu *,
1530 const gdb_byte *,
3019eac3
DE
1531 unsigned int *);
1532
d521ce57 1533static const char *read_str_index (const struct die_reader_specs *reader,
342587c4 1534 ULONGEST str_index);
3019eac3 1535
e142c38c 1536static void set_cu_language (unsigned int, struct dwarf2_cu *);
c906108c 1537
e142c38c
DJ
1538static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
1539 struct dwarf2_cu *);
c906108c 1540
348e048f 1541static struct attribute *dwarf2_attr_no_follow (struct die_info *,
45e58e77 1542 unsigned int);
348e048f 1543
7d45c7c3
KB
1544static const char *dwarf2_string_attr (struct die_info *die, unsigned int name,
1545 struct dwarf2_cu *cu);
1546
05cf31d1
JB
1547static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
1548 struct dwarf2_cu *cu);
1549
e142c38c 1550static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
3ca72b44 1551
e142c38c 1552static struct die_info *die_specification (struct die_info *die,
f2f0e013 1553 struct dwarf2_cu **);
63d06c5c 1554
9c541725 1555static line_header_up dwarf_decode_line_header (sect_offset sect_off,
fff8551c 1556 struct dwarf2_cu *cu);
debd256d 1557
f3f5162e 1558static void dwarf_decode_lines (struct line_header *, const char *,
c3b7b696 1559 struct dwarf2_cu *, struct partial_symtab *,
527f3840 1560 CORE_ADDR, int decode_mapping);
c906108c 1561
4d663531 1562static void dwarf2_start_subfile (const char *, const char *);
c906108c 1563
43f3e411
DE
1564static struct compunit_symtab *dwarf2_start_symtab (struct dwarf2_cu *,
1565 const char *, const char *,
1566 CORE_ADDR);
f4dc4d17 1567
a14ed312 1568static struct symbol *new_symbol (struct die_info *, struct type *,
5e2db402 1569 struct dwarf2_cu *, struct symbol * = NULL);
34eaf542 1570
ff39bb5e 1571static void dwarf2_const_value (const struct attribute *, struct symbol *,
e7c27a73 1572 struct dwarf2_cu *);
c906108c 1573
ff39bb5e 1574static void dwarf2_const_value_attr (const struct attribute *attr,
98bfdba5
PA
1575 struct type *type,
1576 const char *name,
1577 struct obstack *obstack,
12df843f 1578 struct dwarf2_cu *cu, LONGEST *value,
d521ce57 1579 const gdb_byte **bytes,
98bfdba5 1580 struct dwarf2_locexpr_baton **baton);
2df3850c 1581
e7c27a73 1582static struct type *die_type (struct die_info *, struct dwarf2_cu *);
c906108c 1583
b4ba55a1
JB
1584static int need_gnat_info (struct dwarf2_cu *);
1585
3e43a32a
MS
1586static struct type *die_descriptive_type (struct die_info *,
1587 struct dwarf2_cu *);
b4ba55a1
JB
1588
1589static void set_descriptive_type (struct type *, struct die_info *,
1590 struct dwarf2_cu *);
1591
e7c27a73
DJ
1592static struct type *die_containing_type (struct die_info *,
1593 struct dwarf2_cu *);
c906108c 1594
ff39bb5e 1595static struct type *lookup_die_type (struct die_info *, const struct attribute *,
673bfd45 1596 struct dwarf2_cu *);
c906108c 1597
f792889a 1598static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
c906108c 1599
673bfd45
DE
1600static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1601
0d5cff50 1602static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
63d06c5c 1603
6e70227d 1604static char *typename_concat (struct obstack *obs, const char *prefix,
f55ee35c
JK
1605 const char *suffix, int physname,
1606 struct dwarf2_cu *cu);
63d06c5c 1607
e7c27a73 1608static void read_file_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1609
348e048f
DE
1610static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1611
e7c27a73 1612static void read_func_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1613
e7c27a73 1614static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1615
96408a79
SA
1616static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1617
71a3c369
TT
1618static void read_variable (struct die_info *die, struct dwarf2_cu *cu);
1619
ff013f42
JK
1620static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
1621 struct dwarf2_cu *, struct partial_symtab *);
1622
3a2b436a 1623/* How dwarf2_get_pc_bounds constructed its *LOWPC and *HIGHPC return
e385593e 1624 values. Keep the items ordered with increasing constraints compliance. */
3a2b436a
JK
1625enum pc_bounds_kind
1626{
e385593e 1627 /* No attribute DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges was found. */
3a2b436a
JK
1628 PC_BOUNDS_NOT_PRESENT,
1629
e385593e
JK
1630 /* Some of the attributes DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges
1631 were present but they do not form a valid range of PC addresses. */
1632 PC_BOUNDS_INVALID,
1633
3a2b436a
JK
1634 /* Discontiguous range was found - that is DW_AT_ranges was found. */
1635 PC_BOUNDS_RANGES,
1636
1637 /* Contiguous range was found - DW_AT_low_pc and DW_AT_high_pc were found. */
1638 PC_BOUNDS_HIGH_LOW,
1639};
1640
1641static enum pc_bounds_kind dwarf2_get_pc_bounds (struct die_info *,
1642 CORE_ADDR *, CORE_ADDR *,
1643 struct dwarf2_cu *,
1644 struct partial_symtab *);
c906108c 1645
fae299cd
DC
1646static void get_scope_pc_bounds (struct die_info *,
1647 CORE_ADDR *, CORE_ADDR *,
1648 struct dwarf2_cu *);
1649
801e3a5b
JB
1650static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1651 CORE_ADDR, struct dwarf2_cu *);
1652
a14ed312 1653static void dwarf2_add_field (struct field_info *, struct die_info *,
e7c27a73 1654 struct dwarf2_cu *);
c906108c 1655
a14ed312 1656static void dwarf2_attach_fields_to_type (struct field_info *,
e7c27a73 1657 struct type *, struct dwarf2_cu *);
c906108c 1658
a14ed312 1659static void dwarf2_add_member_fn (struct field_info *,
e26fb1d7 1660 struct die_info *, struct type *,
e7c27a73 1661 struct dwarf2_cu *);
c906108c 1662
a14ed312 1663static void dwarf2_attach_fn_fields_to_type (struct field_info *,
3e43a32a
MS
1664 struct type *,
1665 struct dwarf2_cu *);
c906108c 1666
134d01f1 1667static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1668
e7c27a73 1669static void read_common_block (struct die_info *, struct dwarf2_cu *);
c906108c 1670
e7c27a73 1671static void read_namespace (struct die_info *die, struct dwarf2_cu *);
d9fa45fe 1672
5d7cb8df
JK
1673static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1674
22cee43f
PMR
1675static struct using_direct **using_directives (enum language);
1676
27aa8d6a
SW
1677static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1678
74921315
KS
1679static int read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu);
1680
f55ee35c
JK
1681static struct type *read_module_type (struct die_info *die,
1682 struct dwarf2_cu *cu);
1683
38d518c9 1684static const char *namespace_name (struct die_info *die,
e142c38c 1685 int *is_anonymous, struct dwarf2_cu *);
38d518c9 1686
134d01f1 1687static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1688
e7c27a73 1689static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *);
c906108c 1690
6e70227d 1691static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
7ca2d3a3
DL
1692 struct dwarf2_cu *);
1693
bf6af496 1694static struct die_info *read_die_and_siblings_1
d521ce57 1695 (const struct die_reader_specs *, const gdb_byte *, const gdb_byte **,
bf6af496 1696 struct die_info *);
639d11d3 1697
dee91e82 1698static struct die_info *read_die_and_siblings (const struct die_reader_specs *,
d521ce57
TT
1699 const gdb_byte *info_ptr,
1700 const gdb_byte **new_info_ptr,
639d11d3
DC
1701 struct die_info *parent);
1702
d521ce57
TT
1703static const gdb_byte *read_full_die_1 (const struct die_reader_specs *,
1704 struct die_info **, const gdb_byte *,
1705 int *, int);
3019eac3 1706
d521ce57
TT
1707static const gdb_byte *read_full_die (const struct die_reader_specs *,
1708 struct die_info **, const gdb_byte *,
1709 int *);
93311388 1710
e7c27a73 1711static void process_die (struct die_info *, struct dwarf2_cu *);
c906108c 1712
15d034d0
TT
1713static const char *dwarf2_canonicalize_name (const char *, struct dwarf2_cu *,
1714 struct obstack *);
71c25dea 1715
15d034d0 1716static const char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
9219021c 1717
15d034d0 1718static const char *dwarf2_full_name (const char *name,
98bfdba5
PA
1719 struct die_info *die,
1720 struct dwarf2_cu *cu);
1721
ca69b9e6
DE
1722static const char *dwarf2_physname (const char *name, struct die_info *die,
1723 struct dwarf2_cu *cu);
1724
e142c38c 1725static struct die_info *dwarf2_extension (struct die_info *die,
f2f0e013 1726 struct dwarf2_cu **);
9219021c 1727
f39c6ffd 1728static const char *dwarf_tag_name (unsigned int);
c906108c 1729
f39c6ffd 1730static const char *dwarf_attr_name (unsigned int);
c906108c 1731
f39c6ffd 1732static const char *dwarf_form_name (unsigned int);
c906108c 1733
a121b7c1 1734static const char *dwarf_bool_name (unsigned int);
c906108c 1735
f39c6ffd 1736static const char *dwarf_type_encoding_name (unsigned int);
c906108c 1737
f9aca02d 1738static struct die_info *sibling_die (struct die_info *);
c906108c 1739
d97bc12b
DE
1740static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1741
1742static void dump_die_for_error (struct die_info *);
1743
1744static void dump_die_1 (struct ui_file *, int level, int max_level,
1745 struct die_info *);
c906108c 1746
d97bc12b 1747/*static*/ void dump_die (struct die_info *, int max_level);
c906108c 1748
51545339 1749static void store_in_ref_table (struct die_info *,
10b3939b 1750 struct dwarf2_cu *);
c906108c 1751
ff39bb5e 1752static sect_offset dwarf2_get_ref_die_offset (const struct attribute *);
c906108c 1753
ff39bb5e 1754static LONGEST dwarf2_get_attr_constant_value (const struct attribute *, int);
a02abb62 1755
348e048f 1756static struct die_info *follow_die_ref_or_sig (struct die_info *,
ff39bb5e 1757 const struct attribute *,
348e048f
DE
1758 struct dwarf2_cu **);
1759
10b3939b 1760static struct die_info *follow_die_ref (struct die_info *,
ff39bb5e 1761 const struct attribute *,
f2f0e013 1762 struct dwarf2_cu **);
c906108c 1763
348e048f 1764static struct die_info *follow_die_sig (struct die_info *,
ff39bb5e 1765 const struct attribute *,
348e048f
DE
1766 struct dwarf2_cu **);
1767
ac9ec31b
DE
1768static struct type *get_signatured_type (struct die_info *, ULONGEST,
1769 struct dwarf2_cu *);
1770
1771static struct type *get_DW_AT_signature_type (struct die_info *,
ff39bb5e 1772 const struct attribute *,
ac9ec31b
DE
1773 struct dwarf2_cu *);
1774
e5fe5e75 1775static void load_full_type_unit (struct dwarf2_per_cu_data *per_cu);
348e048f 1776
52dc124a 1777static void read_signatured_type (struct signatured_type *);
348e048f 1778
63e43d3a
PMR
1779static int attr_to_dynamic_prop (const struct attribute *attr,
1780 struct die_info *die, struct dwarf2_cu *cu,
1781 struct dynamic_prop *prop);
1782
c906108c
SS
1783/* memory allocation interface */
1784
7b5a2f43 1785static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
c906108c 1786
b60c80d6 1787static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
c906108c 1788
43f3e411 1789static void dwarf_decode_macros (struct dwarf2_cu *, unsigned int, int);
2e276125 1790
6e5a29e1 1791static int attr_form_is_block (const struct attribute *);
8e19ed76 1792
6e5a29e1 1793static int attr_form_is_section_offset (const struct attribute *);
3690dd37 1794
6e5a29e1 1795static int attr_form_is_constant (const struct attribute *);
3690dd37 1796
6e5a29e1 1797static int attr_form_is_ref (const struct attribute *);
7771576e 1798
8cf6f0b1
TT
1799static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1800 struct dwarf2_loclist_baton *baton,
ff39bb5e 1801 const struct attribute *attr);
8cf6f0b1 1802
ff39bb5e 1803static void dwarf2_symbol_mark_computed (const struct attribute *attr,
93e7bd98 1804 struct symbol *sym,
f1e6e072
TT
1805 struct dwarf2_cu *cu,
1806 int is_block);
4c2df51b 1807
d521ce57
TT
1808static const gdb_byte *skip_one_die (const struct die_reader_specs *reader,
1809 const gdb_byte *info_ptr,
1810 struct abbrev_info *abbrev);
4bb7a0a7 1811
72bf9492
DJ
1812static hashval_t partial_die_hash (const void *item);
1813
1814static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1815
ae038cb0 1816static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
ed2dc618
SM
1817 (sect_offset sect_off, unsigned int offset_in_dwz,
1818 struct dwarf2_per_objfile *dwarf2_per_objfile);
ae038cb0 1819
9816fde3 1820static void prepare_one_comp_unit (struct dwarf2_cu *cu,
95554aad
TT
1821 struct die_info *comp_unit_die,
1822 enum language pretend_language);
93311388 1823
ed2dc618 1824static void age_cached_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
ae038cb0 1825
dee91e82 1826static void free_one_cached_comp_unit (struct dwarf2_per_cu_data *);
ae038cb0 1827
f792889a
DJ
1828static struct type *set_die_type (struct die_info *, struct type *,
1829 struct dwarf2_cu *);
1c379e20 1830
ed2dc618 1831static void create_all_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
ae038cb0 1832
ed2dc618 1833static int create_all_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
1fd400ff 1834
95554aad
TT
1835static void load_full_comp_unit (struct dwarf2_per_cu_data *,
1836 enum language);
10b3939b 1837
95554aad
TT
1838static void process_full_comp_unit (struct dwarf2_per_cu_data *,
1839 enum language);
10b3939b 1840
f4dc4d17
DE
1841static void process_full_type_unit (struct dwarf2_per_cu_data *,
1842 enum language);
1843
10b3939b
DJ
1844static void dwarf2_add_dependence (struct dwarf2_cu *,
1845 struct dwarf2_per_cu_data *);
1846
ae038cb0
DJ
1847static void dwarf2_mark (struct dwarf2_cu *);
1848
1849static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
1850
b64f50a1 1851static struct type *get_die_type_at_offset (sect_offset,
ac9ec31b 1852 struct dwarf2_per_cu_data *);
673bfd45 1853
f792889a 1854static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
72019c9c 1855
95554aad
TT
1856static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
1857 enum language pretend_language);
1858
ed2dc618 1859static void process_queue (struct dwarf2_per_objfile *dwarf2_per_objfile);
9291a0cd 1860
b303c6f6
AB
1861/* Class, the destructor of which frees all allocated queue entries. This
1862 will only have work to do if an error was thrown while processing the
1863 dwarf. If no error was thrown then the queue entries should have all
1864 been processed, and freed, as we went along. */
1865
1866class dwarf2_queue_guard
1867{
1868public:
1869 dwarf2_queue_guard () = default;
1870
1871 /* Free any entries remaining on the queue. There should only be
1872 entries left if we hit an error while processing the dwarf. */
1873 ~dwarf2_queue_guard ()
1874 {
1875 struct dwarf2_queue_item *item, *last;
1876
1877 item = dwarf2_queue;
1878 while (item)
1879 {
1880 /* Anything still marked queued is likely to be in an
1881 inconsistent state, so discard it. */
1882 if (item->per_cu->queued)
1883 {
1884 if (item->per_cu->cu != NULL)
1885 free_one_cached_comp_unit (item->per_cu);
1886 item->per_cu->queued = 0;
1887 }
1888
1889 last = item;
1890 item = item->next;
1891 xfree (last);
1892 }
1893
1894 dwarf2_queue = dwarf2_queue_tail = NULL;
1895 }
1896};
1897
d721ba37
PA
1898/* The return type of find_file_and_directory. Note, the enclosed
1899 string pointers are only valid while this object is valid. */
1900
1901struct file_and_directory
1902{
1903 /* The filename. This is never NULL. */
1904 const char *name;
1905
1906 /* The compilation directory. NULL if not known. If we needed to
1907 compute a new string, this points to COMP_DIR_STORAGE, otherwise,
1908 points directly to the DW_AT_comp_dir string attribute owned by
1909 the obstack that owns the DIE. */
1910 const char *comp_dir;
1911
1912 /* If we needed to build a new string for comp_dir, this is what
1913 owns the storage. */
1914 std::string comp_dir_storage;
1915};
1916
1917static file_and_directory find_file_and_directory (struct die_info *die,
1918 struct dwarf2_cu *cu);
9291a0cd
TT
1919
1920static char *file_full_name (int file, struct line_header *lh,
1921 const char *comp_dir);
1922
43988095
JK
1923/* Expected enum dwarf_unit_type for read_comp_unit_head. */
1924enum class rcuh_kind { COMPILE, TYPE };
1925
d521ce57 1926static const gdb_byte *read_and_check_comp_unit_head
ed2dc618
SM
1927 (struct dwarf2_per_objfile* dwarf2_per_objfile,
1928 struct comp_unit_head *header,
36586728 1929 struct dwarf2_section_info *section,
d521ce57 1930 struct dwarf2_section_info *abbrev_section, const gdb_byte *info_ptr,
43988095 1931 rcuh_kind section_kind);
36586728 1932
fd820528 1933static void init_cutu_and_read_dies
f4dc4d17
DE
1934 (struct dwarf2_per_cu_data *this_cu, struct abbrev_table *abbrev_table,
1935 int use_existing_cu, int keep,
3019eac3
DE
1936 die_reader_func_ftype *die_reader_func, void *data);
1937
dee91e82
DE
1938static void init_cutu_and_read_dies_simple
1939 (struct dwarf2_per_cu_data *this_cu,
1940 die_reader_func_ftype *die_reader_func, void *data);
9291a0cd 1941
673bfd45 1942static htab_t allocate_signatured_type_table (struct objfile *objfile);
1fd400ff 1943
3019eac3
DE
1944static htab_t allocate_dwo_unit_table (struct objfile *objfile);
1945
57d63ce2 1946static struct dwo_unit *lookup_dwo_unit_in_dwp
ed2dc618
SM
1947 (struct dwarf2_per_objfile *dwarf2_per_objfile,
1948 struct dwp_file *dwp_file, const char *comp_dir,
57d63ce2 1949 ULONGEST signature, int is_debug_types);
a2ce51a0 1950
ed2dc618
SM
1951static struct dwp_file *get_dwp_file
1952 (struct dwarf2_per_objfile *dwarf2_per_objfile);
a2ce51a0 1953
3019eac3 1954static struct dwo_unit *lookup_dwo_comp_unit
a1855c1d 1955 (struct dwarf2_per_cu_data *, const char *, const char *, ULONGEST);
3019eac3
DE
1956
1957static struct dwo_unit *lookup_dwo_type_unit
a1855c1d 1958 (struct signatured_type *, const char *, const char *);
3019eac3 1959
89e63ee4
DE
1960static void queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *);
1961
263db9a1 1962static void free_dwo_file (struct dwo_file *);
3019eac3 1963
263db9a1
TT
1964/* A unique_ptr helper to free a dwo_file. */
1965
1966struct dwo_file_deleter
ed2dc618 1967{
263db9a1
TT
1968 void operator() (struct dwo_file *df) const
1969 {
1970 free_dwo_file (df);
1971 }
ed2dc618
SM
1972};
1973
263db9a1
TT
1974/* A unique pointer to a dwo_file. */
1975
1976typedef std::unique_ptr<struct dwo_file, dwo_file_deleter> dwo_file_up;
1977
ed2dc618 1978static void process_cu_includes (struct dwarf2_per_objfile *dwarf2_per_objfile);
95554aad 1979
1b80a9fa 1980static void check_producer (struct dwarf2_cu *cu);
527f3840
JK
1981
1982static void free_line_header_voidp (void *arg);
4390d890
DE
1983\f
1984/* Various complaints about symbol reading that don't abort the process. */
1985
1986static void
1987dwarf2_statement_list_fits_in_line_number_section_complaint (void)
1988{
1989 complaint (&symfile_complaints,
1990 _("statement list doesn't fit in .debug_line section"));
1991}
1992
1993static void
1994dwarf2_debug_line_missing_file_complaint (void)
1995{
1996 complaint (&symfile_complaints,
1997 _(".debug_line section has line data without a file"));
1998}
1999
2000static void
2001dwarf2_debug_line_missing_end_sequence_complaint (void)
2002{
2003 complaint (&symfile_complaints,
2004 _(".debug_line section has line "
2005 "program sequence without an end"));
2006}
2007
2008static void
2009dwarf2_complex_location_expr_complaint (void)
2010{
2011 complaint (&symfile_complaints, _("location expression too complex"));
2012}
2013
2014static void
2015dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
2016 int arg3)
2017{
2018 complaint (&symfile_complaints,
2019 _("const value length mismatch for '%s', got %d, expected %d"),
2020 arg1, arg2, arg3);
2021}
2022
2023static void
2024dwarf2_section_buffer_overflow_complaint (struct dwarf2_section_info *section)
2025{
2026 complaint (&symfile_complaints,
2027 _("debug info runs off end of %s section"
2028 " [in module %s]"),
a32a8923
DE
2029 get_section_name (section),
2030 get_section_file_name (section));
4390d890 2031}
1b80a9fa 2032
4390d890
DE
2033static void
2034dwarf2_macro_malformed_definition_complaint (const char *arg1)
2035{
2036 complaint (&symfile_complaints,
2037 _("macro debug info contains a "
2038 "malformed macro definition:\n`%s'"),
2039 arg1);
2040}
2041
2042static void
2043dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
2044{
2045 complaint (&symfile_complaints,
2046 _("invalid attribute class or form for '%s' in '%s'"),
2047 arg1, arg2);
2048}
527f3840
JK
2049
2050/* Hash function for line_header_hash. */
2051
2052static hashval_t
2053line_header_hash (const struct line_header *ofs)
2054{
9c541725 2055 return to_underlying (ofs->sect_off) ^ ofs->offset_in_dwz;
527f3840
JK
2056}
2057
2058/* Hash function for htab_create_alloc_ex for line_header_hash. */
2059
2060static hashval_t
2061line_header_hash_voidp (const void *item)
2062{
9a3c8263 2063 const struct line_header *ofs = (const struct line_header *) item;
527f3840
JK
2064
2065 return line_header_hash (ofs);
2066}
2067
2068/* Equality function for line_header_hash. */
2069
2070static int
2071line_header_eq_voidp (const void *item_lhs, const void *item_rhs)
2072{
9a3c8263
SM
2073 const struct line_header *ofs_lhs = (const struct line_header *) item_lhs;
2074 const struct line_header *ofs_rhs = (const struct line_header *) item_rhs;
527f3840 2075
9c541725 2076 return (ofs_lhs->sect_off == ofs_rhs->sect_off
527f3840
JK
2077 && ofs_lhs->offset_in_dwz == ofs_rhs->offset_in_dwz);
2078}
2079
4390d890 2080\f
9291a0cd 2081
31aa7e4e
JB
2082/* Read the given attribute value as an address, taking the attribute's
2083 form into account. */
2084
2085static CORE_ADDR
2086attr_value_as_address (struct attribute *attr)
2087{
2088 CORE_ADDR addr;
2089
2090 if (attr->form != DW_FORM_addr && attr->form != DW_FORM_GNU_addr_index)
2091 {
2092 /* Aside from a few clearly defined exceptions, attributes that
2093 contain an address must always be in DW_FORM_addr form.
2094 Unfortunately, some compilers happen to be violating this
2095 requirement by encoding addresses using other forms, such
2096 as DW_FORM_data4 for example. For those broken compilers,
2097 we try to do our best, without any guarantee of success,
2098 to interpret the address correctly. It would also be nice
2099 to generate a complaint, but that would require us to maintain
2100 a list of legitimate cases where a non-address form is allowed,
2101 as well as update callers to pass in at least the CU's DWARF
2102 version. This is more overhead than what we're willing to
2103 expand for a pretty rare case. */
2104 addr = DW_UNSND (attr);
2105 }
2106 else
2107 addr = DW_ADDR (attr);
2108
2109 return addr;
2110}
2111
330cdd98
PA
2112/* See declaration. */
2113
2114dwarf2_per_objfile::dwarf2_per_objfile (struct objfile *objfile_,
2115 const dwarf2_debug_sections *names)
2116 : objfile (objfile_)
2117{
2118 if (names == NULL)
2119 names = &dwarf2_elf_names;
2120
2121 bfd *obfd = objfile->obfd;
2122
2123 for (asection *sec = obfd->sections; sec != NULL; sec = sec->next)
2124 locate_sections (obfd, sec, *names);
2125}
2126
fc8e7e75
SM
2127static void free_dwo_files (htab_t dwo_files, struct objfile *objfile);
2128
330cdd98
PA
2129dwarf2_per_objfile::~dwarf2_per_objfile ()
2130{
2131 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
2132 free_cached_comp_units ();
2133
2134 if (quick_file_names_table)
2135 htab_delete (quick_file_names_table);
2136
2137 if (line_header_hash)
2138 htab_delete (line_header_hash);
2139
b76e467d
SM
2140 for (dwarf2_per_cu_data *per_cu : all_comp_units)
2141 VEC_free (dwarf2_per_cu_ptr, per_cu->imported_symtabs);
fc8e7e75 2142
b2bdb8cf
SM
2143 for (signatured_type *sig_type : all_type_units)
2144 VEC_free (dwarf2_per_cu_ptr, sig_type->per_cu.imported_symtabs);
fc8e7e75
SM
2145
2146 VEC_free (dwarf2_section_info_def, types);
2147
2148 if (dwo_files != NULL)
2149 free_dwo_files (dwo_files, objfile);
2150 if (dwp_file != NULL)
2151 gdb_bfd_unref (dwp_file->dbfd);
2152
2153 if (dwz_file != NULL && dwz_file->dwz_bfd)
2154 gdb_bfd_unref (dwz_file->dwz_bfd);
2155
2156 if (index_table != NULL)
2157 index_table->~mapped_index ();
2158
330cdd98
PA
2159 /* Everything else should be on the objfile obstack. */
2160}
2161
2162/* See declaration. */
2163
2164void
2165dwarf2_per_objfile::free_cached_comp_units ()
2166{
2167 dwarf2_per_cu_data *per_cu = read_in_chain;
2168 dwarf2_per_cu_data **last_chain = &read_in_chain;
2169 while (per_cu != NULL)
2170 {
2171 dwarf2_per_cu_data *next_cu = per_cu->cu->read_in_chain;
2172
fcd3b13d 2173 delete per_cu->cu;
330cdd98
PA
2174 *last_chain = next_cu;
2175 per_cu = next_cu;
2176 }
2177}
2178
11ed8cad
TT
2179/* A helper class that calls free_cached_comp_units on
2180 destruction. */
2181
2182class free_cached_comp_units
2183{
2184public:
2185
2186 explicit free_cached_comp_units (dwarf2_per_objfile *per_objfile)
2187 : m_per_objfile (per_objfile)
2188 {
2189 }
2190
2191 ~free_cached_comp_units ()
2192 {
2193 m_per_objfile->free_cached_comp_units ();
2194 }
2195
2196 DISABLE_COPY_AND_ASSIGN (free_cached_comp_units);
2197
2198private:
2199
2200 dwarf2_per_objfile *m_per_objfile;
2201};
2202
c906108c 2203/* Try to locate the sections we need for DWARF 2 debugging
251d32d9
TG
2204 information and return true if we have enough to do something.
2205 NAMES points to the dwarf2 section names, or is NULL if the standard
2206 ELF names are used. */
c906108c
SS
2207
2208int
251d32d9
TG
2209dwarf2_has_info (struct objfile *objfile,
2210 const struct dwarf2_debug_sections *names)
c906108c 2211{
97cbe998
SDJ
2212 if (objfile->flags & OBJF_READNEVER)
2213 return 0;
2214
ed2dc618
SM
2215 struct dwarf2_per_objfile *dwarf2_per_objfile
2216 = get_dwarf2_per_objfile (objfile);
2217
2218 if (dwarf2_per_objfile == NULL)
be391dca
TT
2219 {
2220 /* Initialize per-objfile state. */
fd90ace4
YQ
2221 dwarf2_per_objfile
2222 = new (&objfile->objfile_obstack) struct dwarf2_per_objfile (objfile,
2223 names);
ed2dc618 2224 set_dwarf2_per_objfile (objfile, dwarf2_per_objfile);
be391dca 2225 }
73869dc2 2226 return (!dwarf2_per_objfile->info.is_virtual
049412e3 2227 && dwarf2_per_objfile->info.s.section != NULL
73869dc2 2228 && !dwarf2_per_objfile->abbrev.is_virtual
049412e3 2229 && dwarf2_per_objfile->abbrev.s.section != NULL);
73869dc2
DE
2230}
2231
2232/* Return the containing section of virtual section SECTION. */
2233
2234static struct dwarf2_section_info *
2235get_containing_section (const struct dwarf2_section_info *section)
2236{
2237 gdb_assert (section->is_virtual);
2238 return section->s.containing_section;
c906108c
SS
2239}
2240
a32a8923
DE
2241/* Return the bfd owner of SECTION. */
2242
2243static struct bfd *
2244get_section_bfd_owner (const struct dwarf2_section_info *section)
2245{
73869dc2
DE
2246 if (section->is_virtual)
2247 {
2248 section = get_containing_section (section);
2249 gdb_assert (!section->is_virtual);
2250 }
049412e3 2251 return section->s.section->owner;
a32a8923
DE
2252}
2253
2254/* Return the bfd section of SECTION.
2255 Returns NULL if the section is not present. */
2256
2257static asection *
2258get_section_bfd_section (const struct dwarf2_section_info *section)
2259{
73869dc2
DE
2260 if (section->is_virtual)
2261 {
2262 section = get_containing_section (section);
2263 gdb_assert (!section->is_virtual);
2264 }
049412e3 2265 return section->s.section;
a32a8923
DE
2266}
2267
2268/* Return the name of SECTION. */
2269
2270static const char *
2271get_section_name (const struct dwarf2_section_info *section)
2272{
2273 asection *sectp = get_section_bfd_section (section);
2274
2275 gdb_assert (sectp != NULL);
2276 return bfd_section_name (get_section_bfd_owner (section), sectp);
2277}
2278
2279/* Return the name of the file SECTION is in. */
2280
2281static const char *
2282get_section_file_name (const struct dwarf2_section_info *section)
2283{
2284 bfd *abfd = get_section_bfd_owner (section);
2285
2286 return bfd_get_filename (abfd);
2287}
2288
2289/* Return the id of SECTION.
2290 Returns 0 if SECTION doesn't exist. */
2291
2292static int
2293get_section_id (const struct dwarf2_section_info *section)
2294{
2295 asection *sectp = get_section_bfd_section (section);
2296
2297 if (sectp == NULL)
2298 return 0;
2299 return sectp->id;
2300}
2301
2302/* Return the flags of SECTION.
73869dc2 2303 SECTION (or containing section if this is a virtual section) must exist. */
a32a8923
DE
2304
2305static int
2306get_section_flags (const struct dwarf2_section_info *section)
2307{
2308 asection *sectp = get_section_bfd_section (section);
2309
2310 gdb_assert (sectp != NULL);
2311 return bfd_get_section_flags (sectp->owner, sectp);
2312}
2313
251d32d9
TG
2314/* When loading sections, we look either for uncompressed section or for
2315 compressed section names. */
233a11ab
CS
2316
2317static int
251d32d9
TG
2318section_is_p (const char *section_name,
2319 const struct dwarf2_section_names *names)
233a11ab 2320{
251d32d9
TG
2321 if (names->normal != NULL
2322 && strcmp (section_name, names->normal) == 0)
2323 return 1;
2324 if (names->compressed != NULL
2325 && strcmp (section_name, names->compressed) == 0)
2326 return 1;
2327 return 0;
233a11ab
CS
2328}
2329
330cdd98 2330/* See declaration. */
c906108c 2331
330cdd98
PA
2332void
2333dwarf2_per_objfile::locate_sections (bfd *abfd, asection *sectp,
2334 const dwarf2_debug_sections &names)
c906108c 2335{
dc7650b8 2336 flagword aflag = bfd_get_section_flags (abfd, sectp);
251d32d9 2337
dc7650b8
JK
2338 if ((aflag & SEC_HAS_CONTENTS) == 0)
2339 {
2340 }
330cdd98 2341 else if (section_is_p (sectp->name, &names.info))
c906108c 2342 {
330cdd98
PA
2343 this->info.s.section = sectp;
2344 this->info.size = bfd_get_section_size (sectp);
c906108c 2345 }
330cdd98 2346 else if (section_is_p (sectp->name, &names.abbrev))
c906108c 2347 {
330cdd98
PA
2348 this->abbrev.s.section = sectp;
2349 this->abbrev.size = bfd_get_section_size (sectp);
c906108c 2350 }
330cdd98 2351 else if (section_is_p (sectp->name, &names.line))
c906108c 2352 {
330cdd98
PA
2353 this->line.s.section = sectp;
2354 this->line.size = bfd_get_section_size (sectp);
c906108c 2355 }
330cdd98 2356 else if (section_is_p (sectp->name, &names.loc))
c906108c 2357 {
330cdd98
PA
2358 this->loc.s.section = sectp;
2359 this->loc.size = bfd_get_section_size (sectp);
c906108c 2360 }
330cdd98 2361 else if (section_is_p (sectp->name, &names.loclists))
43988095 2362 {
330cdd98
PA
2363 this->loclists.s.section = sectp;
2364 this->loclists.size = bfd_get_section_size (sectp);
43988095 2365 }
330cdd98 2366 else if (section_is_p (sectp->name, &names.macinfo))
c906108c 2367 {
330cdd98
PA
2368 this->macinfo.s.section = sectp;
2369 this->macinfo.size = bfd_get_section_size (sectp);
c906108c 2370 }
330cdd98 2371 else if (section_is_p (sectp->name, &names.macro))
cf2c3c16 2372 {
330cdd98
PA
2373 this->macro.s.section = sectp;
2374 this->macro.size = bfd_get_section_size (sectp);
cf2c3c16 2375 }
330cdd98 2376 else if (section_is_p (sectp->name, &names.str))
c906108c 2377 {
330cdd98
PA
2378 this->str.s.section = sectp;
2379 this->str.size = bfd_get_section_size (sectp);
c906108c 2380 }
330cdd98 2381 else if (section_is_p (sectp->name, &names.line_str))
43988095 2382 {
330cdd98
PA
2383 this->line_str.s.section = sectp;
2384 this->line_str.size = bfd_get_section_size (sectp);
43988095 2385 }
330cdd98 2386 else if (section_is_p (sectp->name, &names.addr))
3019eac3 2387 {
330cdd98
PA
2388 this->addr.s.section = sectp;
2389 this->addr.size = bfd_get_section_size (sectp);
3019eac3 2390 }
330cdd98 2391 else if (section_is_p (sectp->name, &names.frame))
b6af0555 2392 {
330cdd98
PA
2393 this->frame.s.section = sectp;
2394 this->frame.size = bfd_get_section_size (sectp);
b6af0555 2395 }
330cdd98 2396 else if (section_is_p (sectp->name, &names.eh_frame))
b6af0555 2397 {
330cdd98
PA
2398 this->eh_frame.s.section = sectp;
2399 this->eh_frame.size = bfd_get_section_size (sectp);
b6af0555 2400 }
330cdd98 2401 else if (section_is_p (sectp->name, &names.ranges))
af34e669 2402 {
330cdd98
PA
2403 this->ranges.s.section = sectp;
2404 this->ranges.size = bfd_get_section_size (sectp);
af34e669 2405 }
330cdd98 2406 else if (section_is_p (sectp->name, &names.rnglists))
43988095 2407 {
330cdd98
PA
2408 this->rnglists.s.section = sectp;
2409 this->rnglists.size = bfd_get_section_size (sectp);
43988095 2410 }
330cdd98 2411 else if (section_is_p (sectp->name, &names.types))
348e048f 2412 {
8b70b953
TT
2413 struct dwarf2_section_info type_section;
2414
2415 memset (&type_section, 0, sizeof (type_section));
049412e3 2416 type_section.s.section = sectp;
8b70b953
TT
2417 type_section.size = bfd_get_section_size (sectp);
2418
330cdd98 2419 VEC_safe_push (dwarf2_section_info_def, this->types,
8b70b953 2420 &type_section);
348e048f 2421 }
330cdd98 2422 else if (section_is_p (sectp->name, &names.gdb_index))
9291a0cd 2423 {
330cdd98
PA
2424 this->gdb_index.s.section = sectp;
2425 this->gdb_index.size = bfd_get_section_size (sectp);
9291a0cd 2426 }
927aa2e7
JK
2427 else if (section_is_p (sectp->name, &names.debug_names))
2428 {
2429 this->debug_names.s.section = sectp;
2430 this->debug_names.size = bfd_get_section_size (sectp);
2431 }
2432 else if (section_is_p (sectp->name, &names.debug_aranges))
2433 {
2434 this->debug_aranges.s.section = sectp;
2435 this->debug_aranges.size = bfd_get_section_size (sectp);
2436 }
dce234bc 2437
b4e1fd61 2438 if ((bfd_get_section_flags (abfd, sectp) & (SEC_LOAD | SEC_ALLOC))
72dca2f5 2439 && bfd_section_vma (abfd, sectp) == 0)
330cdd98 2440 this->has_section_at_zero = true;
c906108c
SS
2441}
2442
fceca515
DE
2443/* A helper function that decides whether a section is empty,
2444 or not present. */
9e0ac564
TT
2445
2446static int
19ac8c2e 2447dwarf2_section_empty_p (const struct dwarf2_section_info *section)
9e0ac564 2448{
73869dc2
DE
2449 if (section->is_virtual)
2450 return section->size == 0;
049412e3 2451 return section->s.section == NULL || section->size == 0;
9e0ac564
TT
2452}
2453
cd4fb1b2 2454/* See dwarf2read.h. */
c906108c 2455
cd4fb1b2
SM
2456void
2457dwarf2_read_section (struct objfile *objfile, dwarf2_section_info *info)
c906108c 2458{
a32a8923 2459 asection *sectp;
3019eac3 2460 bfd *abfd;
dce234bc 2461 gdb_byte *buf, *retbuf;
c906108c 2462
be391dca
TT
2463 if (info->readin)
2464 return;
dce234bc 2465 info->buffer = NULL;
be391dca 2466 info->readin = 1;
188dd5d6 2467
9e0ac564 2468 if (dwarf2_section_empty_p (info))
dce234bc 2469 return;
c906108c 2470
a32a8923 2471 sectp = get_section_bfd_section (info);
3019eac3 2472
73869dc2
DE
2473 /* If this is a virtual section we need to read in the real one first. */
2474 if (info->is_virtual)
2475 {
2476 struct dwarf2_section_info *containing_section =
2477 get_containing_section (info);
2478
2479 gdb_assert (sectp != NULL);
2480 if ((sectp->flags & SEC_RELOC) != 0)
2481 {
2482 error (_("Dwarf Error: DWP format V2 with relocations is not"
2483 " supported in section %s [in module %s]"),
2484 get_section_name (info), get_section_file_name (info));
2485 }
2486 dwarf2_read_section (objfile, containing_section);
2487 /* Other code should have already caught virtual sections that don't
2488 fit. */
2489 gdb_assert (info->virtual_offset + info->size
2490 <= containing_section->size);
2491 /* If the real section is empty or there was a problem reading the
2492 section we shouldn't get here. */
2493 gdb_assert (containing_section->buffer != NULL);
2494 info->buffer = containing_section->buffer + info->virtual_offset;
2495 return;
2496 }
2497
4bf44c1c
TT
2498 /* If the section has relocations, we must read it ourselves.
2499 Otherwise we attach it to the BFD. */
2500 if ((sectp->flags & SEC_RELOC) == 0)
dce234bc 2501 {
d521ce57 2502 info->buffer = gdb_bfd_map_section (sectp, &info->size);
4bf44c1c 2503 return;
dce234bc 2504 }
dce234bc 2505
224c3ddb 2506 buf = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, info->size);
4bf44c1c 2507 info->buffer = buf;
dce234bc
PP
2508
2509 /* When debugging .o files, we may need to apply relocations; see
2510 http://sourceware.org/ml/gdb-patches/2002-04/msg00136.html .
2511 We never compress sections in .o files, so we only need to
2512 try this when the section is not compressed. */
ac8035ab 2513 retbuf = symfile_relocate_debug_section (objfile, sectp, buf);
dce234bc
PP
2514 if (retbuf != NULL)
2515 {
2516 info->buffer = retbuf;
2517 return;
2518 }
2519
a32a8923
DE
2520 abfd = get_section_bfd_owner (info);
2521 gdb_assert (abfd != NULL);
2522
dce234bc
PP
2523 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
2524 || bfd_bread (buf, info->size, abfd) != info->size)
19ac8c2e
DE
2525 {
2526 error (_("Dwarf Error: Can't read DWARF data"
2527 " in section %s [in module %s]"),
2528 bfd_section_name (abfd, sectp), bfd_get_filename (abfd));
2529 }
dce234bc
PP
2530}
2531
9e0ac564
TT
2532/* A helper function that returns the size of a section in a safe way.
2533 If you are positive that the section has been read before using the
2534 size, then it is safe to refer to the dwarf2_section_info object's
2535 "size" field directly. In other cases, you must call this
2536 function, because for compressed sections the size field is not set
2537 correctly until the section has been read. */
2538
2539static bfd_size_type
2540dwarf2_section_size (struct objfile *objfile,
2541 struct dwarf2_section_info *info)
2542{
2543 if (!info->readin)
2544 dwarf2_read_section (objfile, info);
2545 return info->size;
2546}
2547
dce234bc 2548/* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
0963b4bd 2549 SECTION_NAME. */
af34e669 2550
dce234bc 2551void
3017a003
TG
2552dwarf2_get_section_info (struct objfile *objfile,
2553 enum dwarf2_section_enum sect,
d521ce57 2554 asection **sectp, const gdb_byte **bufp,
dce234bc
PP
2555 bfd_size_type *sizep)
2556{
2557 struct dwarf2_per_objfile *data
9a3c8263
SM
2558 = (struct dwarf2_per_objfile *) objfile_data (objfile,
2559 dwarf2_objfile_data_key);
dce234bc 2560 struct dwarf2_section_info *info;
a3b2a86b
TT
2561
2562 /* We may see an objfile without any DWARF, in which case we just
2563 return nothing. */
2564 if (data == NULL)
2565 {
2566 *sectp = NULL;
2567 *bufp = NULL;
2568 *sizep = 0;
2569 return;
2570 }
3017a003
TG
2571 switch (sect)
2572 {
2573 case DWARF2_DEBUG_FRAME:
2574 info = &data->frame;
2575 break;
2576 case DWARF2_EH_FRAME:
2577 info = &data->eh_frame;
2578 break;
2579 default:
2580 gdb_assert_not_reached ("unexpected section");
2581 }
dce234bc 2582
9e0ac564 2583 dwarf2_read_section (objfile, info);
dce234bc 2584
a32a8923 2585 *sectp = get_section_bfd_section (info);
dce234bc
PP
2586 *bufp = info->buffer;
2587 *sizep = info->size;
2588}
2589
36586728
TT
2590/* A helper function to find the sections for a .dwz file. */
2591
2592static void
2593locate_dwz_sections (bfd *abfd, asection *sectp, void *arg)
2594{
9a3c8263 2595 struct dwz_file *dwz_file = (struct dwz_file *) arg;
36586728
TT
2596
2597 /* Note that we only support the standard ELF names, because .dwz
2598 is ELF-only (at the time of writing). */
2599 if (section_is_p (sectp->name, &dwarf2_elf_names.abbrev))
2600 {
049412e3 2601 dwz_file->abbrev.s.section = sectp;
36586728
TT
2602 dwz_file->abbrev.size = bfd_get_section_size (sectp);
2603 }
2604 else if (section_is_p (sectp->name, &dwarf2_elf_names.info))
2605 {
049412e3 2606 dwz_file->info.s.section = sectp;
36586728
TT
2607 dwz_file->info.size = bfd_get_section_size (sectp);
2608 }
2609 else if (section_is_p (sectp->name, &dwarf2_elf_names.str))
2610 {
049412e3 2611 dwz_file->str.s.section = sectp;
36586728
TT
2612 dwz_file->str.size = bfd_get_section_size (sectp);
2613 }
2614 else if (section_is_p (sectp->name, &dwarf2_elf_names.line))
2615 {
049412e3 2616 dwz_file->line.s.section = sectp;
36586728
TT
2617 dwz_file->line.size = bfd_get_section_size (sectp);
2618 }
2619 else if (section_is_p (sectp->name, &dwarf2_elf_names.macro))
2620 {
049412e3 2621 dwz_file->macro.s.section = sectp;
36586728
TT
2622 dwz_file->macro.size = bfd_get_section_size (sectp);
2623 }
2ec9a5e0
TT
2624 else if (section_is_p (sectp->name, &dwarf2_elf_names.gdb_index))
2625 {
049412e3 2626 dwz_file->gdb_index.s.section = sectp;
2ec9a5e0
TT
2627 dwz_file->gdb_index.size = bfd_get_section_size (sectp);
2628 }
927aa2e7
JK
2629 else if (section_is_p (sectp->name, &dwarf2_elf_names.debug_names))
2630 {
2631 dwz_file->debug_names.s.section = sectp;
2632 dwz_file->debug_names.size = bfd_get_section_size (sectp);
2633 }
36586728
TT
2634}
2635
4db1a1dc
TT
2636/* Open the separate '.dwz' debug file, if needed. Return NULL if
2637 there is no .gnu_debugaltlink section in the file. Error if there
2638 is such a section but the file cannot be found. */
36586728
TT
2639
2640static struct dwz_file *
ed2dc618 2641dwarf2_get_dwz_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
36586728 2642{
36586728
TT
2643 const char *filename;
2644 struct dwz_file *result;
acd13123 2645 bfd_size_type buildid_len_arg;
dc294be5
TT
2646 size_t buildid_len;
2647 bfd_byte *buildid;
36586728
TT
2648
2649 if (dwarf2_per_objfile->dwz_file != NULL)
2650 return dwarf2_per_objfile->dwz_file;
2651
4db1a1dc 2652 bfd_set_error (bfd_error_no_error);
791afaa2
TT
2653 gdb::unique_xmalloc_ptr<char> data
2654 (bfd_get_alt_debug_link_info (dwarf2_per_objfile->objfile->obfd,
2655 &buildid_len_arg, &buildid));
4db1a1dc
TT
2656 if (data == NULL)
2657 {
2658 if (bfd_get_error () == bfd_error_no_error)
2659 return NULL;
2660 error (_("could not read '.gnu_debugaltlink' section: %s"),
2661 bfd_errmsg (bfd_get_error ()));
2662 }
791afaa2
TT
2663
2664 gdb::unique_xmalloc_ptr<bfd_byte> buildid_holder (buildid);
36586728 2665
acd13123
TT
2666 buildid_len = (size_t) buildid_len_arg;
2667
791afaa2 2668 filename = data.get ();
d721ba37
PA
2669
2670 std::string abs_storage;
36586728
TT
2671 if (!IS_ABSOLUTE_PATH (filename))
2672 {
14278e1f
TT
2673 gdb::unique_xmalloc_ptr<char> abs
2674 = gdb_realpath (objfile_name (dwarf2_per_objfile->objfile));
36586728 2675
14278e1f 2676 abs_storage = ldirname (abs.get ()) + SLASH_STRING + filename;
d721ba37 2677 filename = abs_storage.c_str ();
36586728
TT
2678 }
2679
dc294be5
TT
2680 /* First try the file name given in the section. If that doesn't
2681 work, try to use the build-id instead. */
192b62ce 2682 gdb_bfd_ref_ptr dwz_bfd (gdb_bfd_open (filename, gnutarget, -1));
dc294be5 2683 if (dwz_bfd != NULL)
36586728 2684 {
192b62ce
TT
2685 if (!build_id_verify (dwz_bfd.get (), buildid_len, buildid))
2686 dwz_bfd.release ();
36586728
TT
2687 }
2688
dc294be5
TT
2689 if (dwz_bfd == NULL)
2690 dwz_bfd = build_id_to_debug_bfd (buildid_len, buildid);
2691
2692 if (dwz_bfd == NULL)
2693 error (_("could not find '.gnu_debugaltlink' file for %s"),
2694 objfile_name (dwarf2_per_objfile->objfile));
2695
36586728
TT
2696 result = OBSTACK_ZALLOC (&dwarf2_per_objfile->objfile->objfile_obstack,
2697 struct dwz_file);
192b62ce 2698 result->dwz_bfd = dwz_bfd.release ();
36586728 2699
192b62ce 2700 bfd_map_over_sections (result->dwz_bfd, locate_dwz_sections, result);
36586728 2701
192b62ce 2702 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd, result->dwz_bfd);
8d2cc612 2703 dwarf2_per_objfile->dwz_file = result;
36586728
TT
2704 return result;
2705}
9291a0cd 2706\f
7b9f3c50
DE
2707/* DWARF quick_symbols_functions support. */
2708
2709/* TUs can share .debug_line entries, and there can be a lot more TUs than
2710 unique line tables, so we maintain a separate table of all .debug_line
2711 derived entries to support the sharing.
2712 All the quick functions need is the list of file names. We discard the
2713 line_header when we're done and don't need to record it here. */
2714struct quick_file_names
2715{
094b34ac
DE
2716 /* The data used to construct the hash key. */
2717 struct stmt_list_hash hash;
7b9f3c50
DE
2718
2719 /* The number of entries in file_names, real_names. */
2720 unsigned int num_file_names;
2721
2722 /* The file names from the line table, after being run through
2723 file_full_name. */
2724 const char **file_names;
2725
2726 /* The file names from the line table after being run through
2727 gdb_realpath. These are computed lazily. */
2728 const char **real_names;
2729};
2730
2731/* When using the index (and thus not using psymtabs), each CU has an
2732 object of this type. This is used to hold information needed by
2733 the various "quick" methods. */
2734struct dwarf2_per_cu_quick_data
2735{
2736 /* The file table. This can be NULL if there was no file table
2737 or it's currently not read in.
2738 NOTE: This points into dwarf2_per_objfile->quick_file_names_table. */
2739 struct quick_file_names *file_names;
2740
2741 /* The corresponding symbol table. This is NULL if symbols for this
2742 CU have not yet been read. */
43f3e411 2743 struct compunit_symtab *compunit_symtab;
7b9f3c50
DE
2744
2745 /* A temporary mark bit used when iterating over all CUs in
2746 expand_symtabs_matching. */
2747 unsigned int mark : 1;
2748
2749 /* True if we've tried to read the file table and found there isn't one.
2750 There will be no point in trying to read it again next time. */
2751 unsigned int no_file_data : 1;
2752};
2753
094b34ac
DE
2754/* Utility hash function for a stmt_list_hash. */
2755
2756static hashval_t
2757hash_stmt_list_entry (const struct stmt_list_hash *stmt_list_hash)
2758{
2759 hashval_t v = 0;
2760
2761 if (stmt_list_hash->dwo_unit != NULL)
2762 v += (uintptr_t) stmt_list_hash->dwo_unit->dwo_file;
9c541725 2763 v += to_underlying (stmt_list_hash->line_sect_off);
094b34ac
DE
2764 return v;
2765}
2766
2767/* Utility equality function for a stmt_list_hash. */
2768
2769static int
2770eq_stmt_list_entry (const struct stmt_list_hash *lhs,
2771 const struct stmt_list_hash *rhs)
2772{
2773 if ((lhs->dwo_unit != NULL) != (rhs->dwo_unit != NULL))
2774 return 0;
2775 if (lhs->dwo_unit != NULL
2776 && lhs->dwo_unit->dwo_file != rhs->dwo_unit->dwo_file)
2777 return 0;
2778
9c541725 2779 return lhs->line_sect_off == rhs->line_sect_off;
094b34ac
DE
2780}
2781
7b9f3c50
DE
2782/* Hash function for a quick_file_names. */
2783
2784static hashval_t
2785hash_file_name_entry (const void *e)
2786{
9a3c8263
SM
2787 const struct quick_file_names *file_data
2788 = (const struct quick_file_names *) e;
7b9f3c50 2789
094b34ac 2790 return hash_stmt_list_entry (&file_data->hash);
7b9f3c50
DE
2791}
2792
2793/* Equality function for a quick_file_names. */
2794
2795static int
2796eq_file_name_entry (const void *a, const void *b)
2797{
9a3c8263
SM
2798 const struct quick_file_names *ea = (const struct quick_file_names *) a;
2799 const struct quick_file_names *eb = (const struct quick_file_names *) b;
7b9f3c50 2800
094b34ac 2801 return eq_stmt_list_entry (&ea->hash, &eb->hash);
7b9f3c50
DE
2802}
2803
2804/* Delete function for a quick_file_names. */
2805
2806static void
2807delete_file_name_entry (void *e)
2808{
9a3c8263 2809 struct quick_file_names *file_data = (struct quick_file_names *) e;
7b9f3c50
DE
2810 int i;
2811
2812 for (i = 0; i < file_data->num_file_names; ++i)
2813 {
2814 xfree ((void*) file_data->file_names[i]);
2815 if (file_data->real_names)
2816 xfree ((void*) file_data->real_names[i]);
2817 }
2818
2819 /* The space for the struct itself lives on objfile_obstack,
2820 so we don't free it here. */
2821}
2822
2823/* Create a quick_file_names hash table. */
2824
2825static htab_t
2826create_quick_file_names_table (unsigned int nr_initial_entries)
2827{
2828 return htab_create_alloc (nr_initial_entries,
2829 hash_file_name_entry, eq_file_name_entry,
2830 delete_file_name_entry, xcalloc, xfree);
2831}
9291a0cd 2832
918dd910
JK
2833/* Read in PER_CU->CU. This function is unrelated to symtabs, symtab would
2834 have to be created afterwards. You should call age_cached_comp_units after
2835 processing PER_CU->CU. dw2_setup must have been already called. */
2836
2837static void
2838load_cu (struct dwarf2_per_cu_data *per_cu)
2839{
3019eac3 2840 if (per_cu->is_debug_types)
e5fe5e75 2841 load_full_type_unit (per_cu);
918dd910 2842 else
95554aad 2843 load_full_comp_unit (per_cu, language_minimal);
918dd910 2844
cc12ce38
DE
2845 if (per_cu->cu == NULL)
2846 return; /* Dummy CU. */
2dc860c0
DE
2847
2848 dwarf2_find_base_address (per_cu->cu->dies, per_cu->cu);
918dd910
JK
2849}
2850
a0f42c21 2851/* Read in the symbols for PER_CU. */
2fdf6df6 2852
9291a0cd 2853static void
a0f42c21 2854dw2_do_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
9291a0cd 2855{
ed2dc618 2856 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
9291a0cd 2857
f4dc4d17
DE
2858 /* Skip type_unit_groups, reading the type units they contain
2859 is handled elsewhere. */
2860 if (IS_TYPE_UNIT_GROUP (per_cu))
2861 return;
2862
b303c6f6
AB
2863 /* The destructor of dwarf2_queue_guard frees any entries left on
2864 the queue. After this point we're guaranteed to leave this function
2865 with the dwarf queue empty. */
2866 dwarf2_queue_guard q_guard;
9291a0cd 2867
95554aad 2868 if (dwarf2_per_objfile->using_index
43f3e411 2869 ? per_cu->v.quick->compunit_symtab == NULL
95554aad
TT
2870 : (per_cu->v.psymtab == NULL || !per_cu->v.psymtab->readin))
2871 {
2872 queue_comp_unit (per_cu, language_minimal);
2873 load_cu (per_cu);
89e63ee4
DE
2874
2875 /* If we just loaded a CU from a DWO, and we're working with an index
2876 that may badly handle TUs, load all the TUs in that DWO as well.
2877 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
2878 if (!per_cu->is_debug_types
cc12ce38 2879 && per_cu->cu != NULL
89e63ee4
DE
2880 && per_cu->cu->dwo_unit != NULL
2881 && dwarf2_per_objfile->index_table != NULL
2882 && dwarf2_per_objfile->index_table->version <= 7
2883 /* DWP files aren't supported yet. */
ed2dc618 2884 && get_dwp_file (dwarf2_per_objfile) == NULL)
89e63ee4 2885 queue_and_load_all_dwo_tus (per_cu);
95554aad 2886 }
9291a0cd 2887
ed2dc618 2888 process_queue (dwarf2_per_objfile);
9291a0cd
TT
2889
2890 /* Age the cache, releasing compilation units that have not
2891 been used recently. */
ed2dc618 2892 age_cached_comp_units (dwarf2_per_objfile);
9291a0cd
TT
2893}
2894
2895/* Ensure that the symbols for PER_CU have been read in. OBJFILE is
2896 the objfile from which this CU came. Returns the resulting symbol
2897 table. */
2fdf6df6 2898
43f3e411 2899static struct compunit_symtab *
a0f42c21 2900dw2_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
9291a0cd 2901{
ed2dc618
SM
2902 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
2903
95554aad 2904 gdb_assert (dwarf2_per_objfile->using_index);
43f3e411 2905 if (!per_cu->v.quick->compunit_symtab)
9291a0cd 2906 {
11ed8cad 2907 free_cached_comp_units freer (dwarf2_per_objfile);
c83dd867 2908 scoped_restore decrementer = increment_reading_symtab ();
a0f42c21 2909 dw2_do_instantiate_symtab (per_cu);
ed2dc618 2910 process_cu_includes (dwarf2_per_objfile);
9291a0cd 2911 }
f194fefb 2912
43f3e411 2913 return per_cu->v.quick->compunit_symtab;
9291a0cd
TT
2914}
2915
ff4c9fec 2916/* See declaration. */
f4dc4d17 2917
ff4c9fec
SM
2918dwarf2_per_cu_data *
2919dwarf2_per_objfile::get_cutu (int index)
2920{
b76e467d 2921 if (index >= this->all_comp_units.size ())
ff4c9fec 2922 {
b76e467d 2923 index -= this->all_comp_units.size ();
b2bdb8cf 2924 gdb_assert (index < this->all_type_units.size ());
ff4c9fec
SM
2925 return &this->all_type_units[index]->per_cu;
2926 }
f4dc4d17 2927
ff4c9fec
SM
2928 return this->all_comp_units[index];
2929}
f4dc4d17 2930
ff4c9fec 2931/* See declaration. */
2fdf6df6 2932
ff4c9fec
SM
2933dwarf2_per_cu_data *
2934dwarf2_per_objfile::get_cu (int index)
1fd400ff 2935{
b76e467d 2936 gdb_assert (index >= 0 && index < this->all_comp_units.size ());
f4dc4d17 2937
ff4c9fec 2938 return this->all_comp_units[index];
f4dc4d17
DE
2939}
2940
ff4c9fec 2941/* See declaration. */
f4dc4d17 2942
ff4c9fec
SM
2943signatured_type *
2944dwarf2_per_objfile::get_tu (int index)
f4dc4d17 2945{
b2bdb8cf 2946 gdb_assert (index >= 0 && index < this->all_type_units.size ());
f4dc4d17 2947
ff4c9fec 2948 return this->all_type_units[index];
1fd400ff
TT
2949}
2950
4b514bc8
JK
2951/* Return a new dwarf2_per_cu_data allocated on OBJFILE's
2952 objfile_obstack, and constructed with the specified field
2953 values. */
2954
2955static dwarf2_per_cu_data *
ed2dc618 2956create_cu_from_index_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
4b514bc8
JK
2957 struct dwarf2_section_info *section,
2958 int is_dwz,
2959 sect_offset sect_off, ULONGEST length)
2960{
ed2dc618 2961 struct objfile *objfile = dwarf2_per_objfile->objfile;
4b514bc8
JK
2962 dwarf2_per_cu_data *the_cu
2963 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2964 struct dwarf2_per_cu_data);
2965 the_cu->sect_off = sect_off;
2966 the_cu->length = length;
e3b94546 2967 the_cu->dwarf2_per_objfile = dwarf2_per_objfile;
4b514bc8
JK
2968 the_cu->section = section;
2969 the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2970 struct dwarf2_per_cu_quick_data);
2971 the_cu->is_dwz = is_dwz;
2972 return the_cu;
2973}
2974
2ec9a5e0
TT
2975/* A helper for create_cus_from_index that handles a given list of
2976 CUs. */
2fdf6df6 2977
74a0d9f6 2978static void
12359b5e 2979create_cus_from_index_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
2ec9a5e0
TT
2980 const gdb_byte *cu_list, offset_type n_elements,
2981 struct dwarf2_section_info *section,
b76e467d 2982 int is_dwz)
9291a0cd 2983{
12359b5e 2984 for (offset_type i = 0; i < n_elements; i += 2)
9291a0cd 2985 {
74a0d9f6 2986 gdb_static_assert (sizeof (ULONGEST) >= 8);
9c541725
PA
2987
2988 sect_offset sect_off
2989 = (sect_offset) extract_unsigned_integer (cu_list, 8, BFD_ENDIAN_LITTLE);
2990 ULONGEST length = extract_unsigned_integer (cu_list + 8, 8, BFD_ENDIAN_LITTLE);
9291a0cd
TT
2991 cu_list += 2 * 8;
2992
b76e467d 2993 dwarf2_per_cu_data *per_cu
ed2dc618
SM
2994 = create_cu_from_index_list (dwarf2_per_objfile, section, is_dwz,
2995 sect_off, length);
b76e467d 2996 dwarf2_per_objfile->all_comp_units.push_back (per_cu);
9291a0cd 2997 }
9291a0cd
TT
2998}
2999
2ec9a5e0 3000/* Read the CU list from the mapped index, and use it to create all
74a0d9f6 3001 the CU objects for this objfile. */
2ec9a5e0 3002
74a0d9f6 3003static void
12359b5e 3004create_cus_from_index (struct dwarf2_per_objfile *dwarf2_per_objfile,
2ec9a5e0
TT
3005 const gdb_byte *cu_list, offset_type cu_list_elements,
3006 const gdb_byte *dwz_list, offset_type dwz_elements)
3007{
b76e467d
SM
3008 gdb_assert (dwarf2_per_objfile->all_comp_units.empty ());
3009 dwarf2_per_objfile->all_comp_units.reserve
3010 ((cu_list_elements + dwz_elements) / 2);
2ec9a5e0 3011
12359b5e 3012 create_cus_from_index_list (dwarf2_per_objfile, cu_list, cu_list_elements,
b76e467d 3013 &dwarf2_per_objfile->info, 0);
2ec9a5e0
TT
3014
3015 if (dwz_elements == 0)
74a0d9f6 3016 return;
2ec9a5e0 3017
12359b5e
SM
3018 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
3019 create_cus_from_index_list (dwarf2_per_objfile, dwz_list, dwz_elements,
b76e467d 3020 &dwz->info, 1);
2ec9a5e0
TT
3021}
3022
1fd400ff 3023/* Create the signatured type hash table from the index. */
673bfd45 3024
74a0d9f6 3025static void
12359b5e
SM
3026create_signatured_type_table_from_index
3027 (struct dwarf2_per_objfile *dwarf2_per_objfile,
3028 struct dwarf2_section_info *section,
3029 const gdb_byte *bytes,
3030 offset_type elements)
1fd400ff 3031{
12359b5e 3032 struct objfile *objfile = dwarf2_per_objfile->objfile;
1fd400ff 3033
b2bdb8cf
SM
3034 gdb_assert (dwarf2_per_objfile->all_type_units.empty ());
3035 dwarf2_per_objfile->all_type_units.reserve (elements / 3);
1fd400ff 3036
12359b5e 3037 htab_t sig_types_hash = allocate_signatured_type_table (objfile);
1fd400ff 3038
12359b5e 3039 for (offset_type i = 0; i < elements; i += 3)
1fd400ff 3040 {
52dc124a 3041 struct signatured_type *sig_type;
9c541725 3042 ULONGEST signature;
1fd400ff 3043 void **slot;
9c541725 3044 cu_offset type_offset_in_tu;
1fd400ff 3045
74a0d9f6 3046 gdb_static_assert (sizeof (ULONGEST) >= 8);
9c541725
PA
3047 sect_offset sect_off
3048 = (sect_offset) extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
3049 type_offset_in_tu
3050 = (cu_offset) extract_unsigned_integer (bytes + 8, 8,
3051 BFD_ENDIAN_LITTLE);
1fd400ff
TT
3052 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
3053 bytes += 3 * 8;
3054
52dc124a 3055 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1fd400ff 3056 struct signatured_type);
52dc124a 3057 sig_type->signature = signature;
9c541725 3058 sig_type->type_offset_in_tu = type_offset_in_tu;
3019eac3 3059 sig_type->per_cu.is_debug_types = 1;
8a0459fd 3060 sig_type->per_cu.section = section;
9c541725 3061 sig_type->per_cu.sect_off = sect_off;
e3b94546 3062 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
52dc124a 3063 sig_type->per_cu.v.quick
1fd400ff
TT
3064 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3065 struct dwarf2_per_cu_quick_data);
3066
52dc124a
DE
3067 slot = htab_find_slot (sig_types_hash, sig_type, INSERT);
3068 *slot = sig_type;
1fd400ff 3069
b2bdb8cf 3070 dwarf2_per_objfile->all_type_units.push_back (sig_type);
1fd400ff
TT
3071 }
3072
673bfd45 3073 dwarf2_per_objfile->signatured_types = sig_types_hash;
1fd400ff
TT
3074}
3075
927aa2e7
JK
3076/* Create the signatured type hash table from .debug_names. */
3077
3078static void
3079create_signatured_type_table_from_debug_names
ed2dc618 3080 (struct dwarf2_per_objfile *dwarf2_per_objfile,
927aa2e7
JK
3081 const mapped_debug_names &map,
3082 struct dwarf2_section_info *section,
3083 struct dwarf2_section_info *abbrev_section)
3084{
ed2dc618
SM
3085 struct objfile *objfile = dwarf2_per_objfile->objfile;
3086
927aa2e7
JK
3087 dwarf2_read_section (objfile, section);
3088 dwarf2_read_section (objfile, abbrev_section);
3089
b2bdb8cf
SM
3090 gdb_assert (dwarf2_per_objfile->all_type_units.empty ());
3091 dwarf2_per_objfile->all_type_units.reserve (map.tu_count);
927aa2e7
JK
3092
3093 htab_t sig_types_hash = allocate_signatured_type_table (objfile);
3094
3095 for (uint32_t i = 0; i < map.tu_count; ++i)
3096 {
3097 struct signatured_type *sig_type;
927aa2e7 3098 void **slot;
927aa2e7
JK
3099
3100 sect_offset sect_off
3101 = (sect_offset) (extract_unsigned_integer
3102 (map.tu_table_reordered + i * map.offset_size,
3103 map.offset_size,
3104 map.dwarf5_byte_order));
3105
3106 comp_unit_head cu_header;
ed2dc618
SM
3107 read_and_check_comp_unit_head (dwarf2_per_objfile, &cu_header, section,
3108 abbrev_section,
927aa2e7
JK
3109 section->buffer + to_underlying (sect_off),
3110 rcuh_kind::TYPE);
3111
3112 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3113 struct signatured_type);
3114 sig_type->signature = cu_header.signature;
3115 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
3116 sig_type->per_cu.is_debug_types = 1;
3117 sig_type->per_cu.section = section;
3118 sig_type->per_cu.sect_off = sect_off;
e3b94546 3119 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
927aa2e7
JK
3120 sig_type->per_cu.v.quick
3121 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3122 struct dwarf2_per_cu_quick_data);
3123
3124 slot = htab_find_slot (sig_types_hash, sig_type, INSERT);
3125 *slot = sig_type;
3126
b2bdb8cf 3127 dwarf2_per_objfile->all_type_units.push_back (sig_type);
927aa2e7
JK
3128 }
3129
3130 dwarf2_per_objfile->signatured_types = sig_types_hash;
3131}
3132
9291a0cd
TT
3133/* Read the address map data from the mapped index, and use it to
3134 populate the objfile's psymtabs_addrmap. */
2fdf6df6 3135
9291a0cd 3136static void
ed2dc618
SM
3137create_addrmap_from_index (struct dwarf2_per_objfile *dwarf2_per_objfile,
3138 struct mapped_index *index)
9291a0cd 3139{
ed2dc618 3140 struct objfile *objfile = dwarf2_per_objfile->objfile;
3e29f34a 3141 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9291a0cd 3142 const gdb_byte *iter, *end;
9291a0cd 3143 struct addrmap *mutable_map;
9291a0cd
TT
3144 CORE_ADDR baseaddr;
3145
8268c778
PA
3146 auto_obstack temp_obstack;
3147
9291a0cd
TT
3148 mutable_map = addrmap_create_mutable (&temp_obstack);
3149
f00a2de2
PA
3150 iter = index->address_table.data ();
3151 end = iter + index->address_table.size ();
9291a0cd
TT
3152
3153 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
3154
3155 while (iter < end)
3156 {
3157 ULONGEST hi, lo, cu_index;
3158 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
3159 iter += 8;
3160 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
3161 iter += 8;
3162 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
3163 iter += 4;
f652bce2 3164
24a55014 3165 if (lo > hi)
f652bce2 3166 {
24a55014
DE
3167 complaint (&symfile_complaints,
3168 _(".gdb_index address table has invalid range (%s - %s)"),
c0cd8254 3169 hex_string (lo), hex_string (hi));
24a55014 3170 continue;
f652bce2 3171 }
24a55014 3172
b76e467d 3173 if (cu_index >= dwarf2_per_objfile->all_comp_units.size ())
f652bce2
DE
3174 {
3175 complaint (&symfile_complaints,
3176 _(".gdb_index address table has invalid CU number %u"),
3177 (unsigned) cu_index);
24a55014 3178 continue;
f652bce2 3179 }
24a55014 3180
3e29f34a
MR
3181 lo = gdbarch_adjust_dwarf2_addr (gdbarch, lo + baseaddr);
3182 hi = gdbarch_adjust_dwarf2_addr (gdbarch, hi + baseaddr);
ed2dc618 3183 addrmap_set_empty (mutable_map, lo, hi - 1,
ff4c9fec 3184 dwarf2_per_objfile->get_cu (cu_index));
9291a0cd
TT
3185 }
3186
3187 objfile->psymtabs_addrmap = addrmap_create_fixed (mutable_map,
3188 &objfile->objfile_obstack);
9291a0cd
TT
3189}
3190
927aa2e7
JK
3191/* Read the address map data from DWARF-5 .debug_aranges, and use it to
3192 populate the objfile's psymtabs_addrmap. */
3193
3194static void
ed2dc618 3195create_addrmap_from_aranges (struct dwarf2_per_objfile *dwarf2_per_objfile,
927aa2e7
JK
3196 struct dwarf2_section_info *section)
3197{
ed2dc618 3198 struct objfile *objfile = dwarf2_per_objfile->objfile;
927aa2e7
JK
3199 bfd *abfd = objfile->obfd;
3200 struct gdbarch *gdbarch = get_objfile_arch (objfile);
3201 const CORE_ADDR baseaddr = ANOFFSET (objfile->section_offsets,
3202 SECT_OFF_TEXT (objfile));
3203
3204 auto_obstack temp_obstack;
3205 addrmap *mutable_map = addrmap_create_mutable (&temp_obstack);
3206
3207 std::unordered_map<sect_offset,
3208 dwarf2_per_cu_data *,
3209 gdb::hash_enum<sect_offset>>
3210 debug_info_offset_to_per_cu;
b76e467d 3211 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
927aa2e7 3212 {
927aa2e7
JK
3213 const auto insertpair
3214 = debug_info_offset_to_per_cu.emplace (per_cu->sect_off, per_cu);
3215 if (!insertpair.second)
3216 {
3217 warning (_("Section .debug_aranges in %s has duplicate "
9d8780f0
SM
3218 "debug_info_offset %s, ignoring .debug_aranges."),
3219 objfile_name (objfile), sect_offset_str (per_cu->sect_off));
927aa2e7
JK
3220 return;
3221 }
3222 }
3223
3224 dwarf2_read_section (objfile, section);
3225
3226 const bfd_endian dwarf5_byte_order = gdbarch_byte_order (gdbarch);
3227
3228 const gdb_byte *addr = section->buffer;
3229
3230 while (addr < section->buffer + section->size)
3231 {
3232 const gdb_byte *const entry_addr = addr;
3233 unsigned int bytes_read;
3234
3235 const LONGEST entry_length = read_initial_length (abfd, addr,
3236 &bytes_read);
3237 addr += bytes_read;
3238
3239 const gdb_byte *const entry_end = addr + entry_length;
3240 const bool dwarf5_is_dwarf64 = bytes_read != 4;
3241 const uint8_t offset_size = dwarf5_is_dwarf64 ? 8 : 4;
3242 if (addr + entry_length > section->buffer + section->size)
3243 {
3244 warning (_("Section .debug_aranges in %s entry at offset %zu "
3245 "length %s exceeds section length %s, "
3246 "ignoring .debug_aranges."),
3247 objfile_name (objfile), entry_addr - section->buffer,
3248 plongest (bytes_read + entry_length),
3249 pulongest (section->size));
3250 return;
3251 }
3252
3253 /* The version number. */
3254 const uint16_t version = read_2_bytes (abfd, addr);
3255 addr += 2;
3256 if (version != 2)
3257 {
3258 warning (_("Section .debug_aranges in %s entry at offset %zu "
3259 "has unsupported version %d, ignoring .debug_aranges."),
3260 objfile_name (objfile), entry_addr - section->buffer,
3261 version);
3262 return;
3263 }
3264
3265 const uint64_t debug_info_offset
3266 = extract_unsigned_integer (addr, offset_size, dwarf5_byte_order);
3267 addr += offset_size;
3268 const auto per_cu_it
3269 = debug_info_offset_to_per_cu.find (sect_offset (debug_info_offset));
3270 if (per_cu_it == debug_info_offset_to_per_cu.cend ())
3271 {
3272 warning (_("Section .debug_aranges in %s entry at offset %zu "
3273 "debug_info_offset %s does not exists, "
3274 "ignoring .debug_aranges."),
3275 objfile_name (objfile), entry_addr - section->buffer,
3276 pulongest (debug_info_offset));
3277 return;
3278 }
3279 dwarf2_per_cu_data *const per_cu = per_cu_it->second;
3280
3281 const uint8_t address_size = *addr++;
3282 if (address_size < 1 || address_size > 8)
3283 {
3284 warning (_("Section .debug_aranges in %s entry at offset %zu "
3285 "address_size %u is invalid, ignoring .debug_aranges."),
3286 objfile_name (objfile), entry_addr - section->buffer,
3287 address_size);
3288 return;
3289 }
3290
3291 const uint8_t segment_selector_size = *addr++;
3292 if (segment_selector_size != 0)
3293 {
3294 warning (_("Section .debug_aranges in %s entry at offset %zu "
3295 "segment_selector_size %u is not supported, "
3296 "ignoring .debug_aranges."),
3297 objfile_name (objfile), entry_addr - section->buffer,
3298 segment_selector_size);
3299 return;
3300 }
3301
3302 /* Must pad to an alignment boundary that is twice the address
3303 size. It is undocumented by the DWARF standard but GCC does
3304 use it. */
3305 for (size_t padding = ((-(addr - section->buffer))
3306 & (2 * address_size - 1));
3307 padding > 0; padding--)
3308 if (*addr++ != 0)
3309 {
3310 warning (_("Section .debug_aranges in %s entry at offset %zu "
3311 "padding is not zero, ignoring .debug_aranges."),
3312 objfile_name (objfile), entry_addr - section->buffer);
3313 return;
3314 }
3315
3316 for (;;)
3317 {
3318 if (addr + 2 * address_size > entry_end)
3319 {
3320 warning (_("Section .debug_aranges in %s entry at offset %zu "
3321 "address list is not properly terminated, "
3322 "ignoring .debug_aranges."),
3323 objfile_name (objfile), entry_addr - section->buffer);
3324 return;
3325 }
3326 ULONGEST start = extract_unsigned_integer (addr, address_size,
3327 dwarf5_byte_order);
3328 addr += address_size;
3329 ULONGEST length = extract_unsigned_integer (addr, address_size,
3330 dwarf5_byte_order);
3331 addr += address_size;
3332 if (start == 0 && length == 0)
3333 break;
3334 if (start == 0 && !dwarf2_per_objfile->has_section_at_zero)
3335 {
3336 /* Symbol was eliminated due to a COMDAT group. */
3337 continue;
3338 }
3339 ULONGEST end = start + length;
3340 start = gdbarch_adjust_dwarf2_addr (gdbarch, start + baseaddr);
3341 end = gdbarch_adjust_dwarf2_addr (gdbarch, end + baseaddr);
3342 addrmap_set_empty (mutable_map, start, end - 1, per_cu);
3343 }
3344 }
3345
3346 objfile->psymtabs_addrmap = addrmap_create_fixed (mutable_map,
3347 &objfile->objfile_obstack);
3348}
3349
9291a0cd
TT
3350/* Find a slot in the mapped index INDEX for the object named NAME.
3351 If NAME is found, set *VEC_OUT to point to the CU vector in the
109483d9
PA
3352 constant pool and return true. If NAME cannot be found, return
3353 false. */
2fdf6df6 3354
109483d9 3355static bool
9291a0cd
TT
3356find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
3357 offset_type **vec_out)
3358{
0cf03b49 3359 offset_type hash;
9291a0cd 3360 offset_type slot, step;
559a7a62 3361 int (*cmp) (const char *, const char *);
9291a0cd 3362
791afaa2 3363 gdb::unique_xmalloc_ptr<char> without_params;
0cf03b49 3364 if (current_language->la_language == language_cplus
45280282
IB
3365 || current_language->la_language == language_fortran
3366 || current_language->la_language == language_d)
0cf03b49
JK
3367 {
3368 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
3369 not contain any. */
a8719064 3370
72998fb3 3371 if (strchr (name, '(') != NULL)
0cf03b49 3372 {
109483d9 3373 without_params = cp_remove_params (name);
0cf03b49 3374
72998fb3 3375 if (without_params != NULL)
791afaa2 3376 name = without_params.get ();
0cf03b49
JK
3377 }
3378 }
3379
559a7a62 3380 /* Index version 4 did not support case insensitive searches. But the
feea76c2 3381 indices for case insensitive languages are built in lowercase, therefore
559a7a62
JK
3382 simulate our NAME being searched is also lowercased. */
3383 hash = mapped_index_string_hash ((index->version == 4
3384 && case_sensitivity == case_sensitive_off
3385 ? 5 : index->version),
3386 name);
3387
f00a2de2
PA
3388 slot = hash & (index->symbol_table.size () - 1);
3389 step = ((hash * 17) & (index->symbol_table.size () - 1)) | 1;
559a7a62 3390 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
9291a0cd
TT
3391
3392 for (;;)
3393 {
9291a0cd 3394 const char *str;
f00a2de2
PA
3395
3396 const auto &bucket = index->symbol_table[slot];
3397 if (bucket.name == 0 && bucket.vec == 0)
109483d9 3398 return false;
9291a0cd 3399
f00a2de2 3400 str = index->constant_pool + MAYBE_SWAP (bucket.name);
559a7a62 3401 if (!cmp (name, str))
9291a0cd
TT
3402 {
3403 *vec_out = (offset_type *) (index->constant_pool
f00a2de2 3404 + MAYBE_SWAP (bucket.vec));
109483d9 3405 return true;
9291a0cd
TT
3406 }
3407
f00a2de2 3408 slot = (slot + step) & (index->symbol_table.size () - 1);
9291a0cd
TT
3409 }
3410}
3411
2ec9a5e0
TT
3412/* A helper function that reads the .gdb_index from SECTION and fills
3413 in MAP. FILENAME is the name of the file containing the section;
3414 it is used for error reporting. DEPRECATED_OK is nonzero if it is
3415 ok to use deprecated sections.
3416
3417 CU_LIST, CU_LIST_ELEMENTS, TYPES_LIST, and TYPES_LIST_ELEMENTS are
3418 out parameters that are filled in with information about the CU and
3419 TU lists in the section.
3420
3421 Returns 1 if all went well, 0 otherwise. */
2fdf6df6 3422
9291a0cd 3423static int
2ec9a5e0
TT
3424read_index_from_section (struct objfile *objfile,
3425 const char *filename,
3426 int deprecated_ok,
3427 struct dwarf2_section_info *section,
3428 struct mapped_index *map,
3429 const gdb_byte **cu_list,
3430 offset_type *cu_list_elements,
3431 const gdb_byte **types_list,
3432 offset_type *types_list_elements)
9291a0cd 3433{
948f8e3d 3434 const gdb_byte *addr;
2ec9a5e0 3435 offset_type version;
b3b272e1 3436 offset_type *metadata;
1fd400ff 3437 int i;
9291a0cd 3438
2ec9a5e0 3439 if (dwarf2_section_empty_p (section))
9291a0cd 3440 return 0;
82430852
JK
3441
3442 /* Older elfutils strip versions could keep the section in the main
3443 executable while splitting it for the separate debug info file. */
a32a8923 3444 if ((get_section_flags (section) & SEC_HAS_CONTENTS) == 0)
82430852
JK
3445 return 0;
3446
2ec9a5e0 3447 dwarf2_read_section (objfile, section);
9291a0cd 3448
2ec9a5e0 3449 addr = section->buffer;
9291a0cd 3450 /* Version check. */
1fd400ff 3451 version = MAYBE_SWAP (*(offset_type *) addr);
987d643c 3452 /* Versions earlier than 3 emitted every copy of a psymbol. This
a6e293d1 3453 causes the index to behave very poorly for certain requests. Version 3
831adc1f 3454 contained incomplete addrmap. So, it seems better to just ignore such
481860b3 3455 indices. */
831adc1f 3456 if (version < 4)
481860b3
GB
3457 {
3458 static int warning_printed = 0;
3459 if (!warning_printed)
3460 {
3461 warning (_("Skipping obsolete .gdb_index section in %s."),
2ec9a5e0 3462 filename);
481860b3
GB
3463 warning_printed = 1;
3464 }
3465 return 0;
3466 }
3467 /* Index version 4 uses a different hash function than index version
3468 5 and later.
3469
3470 Versions earlier than 6 did not emit psymbols for inlined
3471 functions. Using these files will cause GDB not to be able to
3472 set breakpoints on inlined functions by name, so we ignore these
e615022a
DE
3473 indices unless the user has done
3474 "set use-deprecated-index-sections on". */
2ec9a5e0 3475 if (version < 6 && !deprecated_ok)
481860b3
GB
3476 {
3477 static int warning_printed = 0;
3478 if (!warning_printed)
3479 {
e615022a
DE
3480 warning (_("\
3481Skipping deprecated .gdb_index section in %s.\n\
3482Do \"set use-deprecated-index-sections on\" before the file is read\n\
3483to use the section anyway."),
2ec9a5e0 3484 filename);
481860b3
GB
3485 warning_printed = 1;
3486 }
3487 return 0;
3488 }
796a7ff8 3489 /* Version 7 indices generated by gold refer to the CU for a symbol instead
8943b874
DE
3490 of the TU (for symbols coming from TUs),
3491 http://sourceware.org/bugzilla/show_bug.cgi?id=15021.
3492 Plus gold-generated indices can have duplicate entries for global symbols,
3493 http://sourceware.org/bugzilla/show_bug.cgi?id=15646.
3494 These are just performance bugs, and we can't distinguish gdb-generated
3495 indices from gold-generated ones, so issue no warning here. */
796a7ff8 3496
481860b3 3497 /* Indexes with higher version than the one supported by GDB may be no
594e8718 3498 longer backward compatible. */
796a7ff8 3499 if (version > 8)
594e8718 3500 return 0;
9291a0cd 3501
559a7a62 3502 map->version = version;
2ec9a5e0 3503 map->total_size = section->size;
9291a0cd
TT
3504
3505 metadata = (offset_type *) (addr + sizeof (offset_type));
1fd400ff
TT
3506
3507 i = 0;
2ec9a5e0
TT
3508 *cu_list = addr + MAYBE_SWAP (metadata[i]);
3509 *cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
3510 / 8);
1fd400ff
TT
3511 ++i;
3512
2ec9a5e0
TT
3513 *types_list = addr + MAYBE_SWAP (metadata[i]);
3514 *types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
3515 - MAYBE_SWAP (metadata[i]))
3516 / 8);
987d643c 3517 ++i;
1fd400ff 3518
f00a2de2
PA
3519 const gdb_byte *address_table = addr + MAYBE_SWAP (metadata[i]);
3520 const gdb_byte *address_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
3521 map->address_table
3522 = gdb::array_view<const gdb_byte> (address_table, address_table_end);
1fd400ff
TT
3523 ++i;
3524
f00a2de2
PA
3525 const gdb_byte *symbol_table = addr + MAYBE_SWAP (metadata[i]);
3526 const gdb_byte *symbol_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
3527 map->symbol_table
3528 = gdb::array_view<mapped_index::symbol_table_slot>
3529 ((mapped_index::symbol_table_slot *) symbol_table,
3530 (mapped_index::symbol_table_slot *) symbol_table_end);
9291a0cd 3531
f00a2de2 3532 ++i;
f9d83a0b 3533 map->constant_pool = (char *) (addr + MAYBE_SWAP (metadata[i]));
1fd400ff 3534
2ec9a5e0
TT
3535 return 1;
3536}
3537
927aa2e7 3538/* Read .gdb_index. If everything went ok, initialize the "quick"
2ec9a5e0
TT
3539 elements of all the CUs and return 1. Otherwise, return 0. */
3540
3541static int
12359b5e 3542dwarf2_read_index (struct dwarf2_per_objfile *dwarf2_per_objfile)
2ec9a5e0
TT
3543{
3544 struct mapped_index local_map, *map;
3545 const gdb_byte *cu_list, *types_list, *dwz_list = NULL;
3546 offset_type cu_list_elements, types_list_elements, dwz_list_elements = 0;
4db1a1dc 3547 struct dwz_file *dwz;
12359b5e 3548 struct objfile *objfile = dwarf2_per_objfile->objfile;
2ec9a5e0 3549
4262abfb 3550 if (!read_index_from_section (objfile, objfile_name (objfile),
2ec9a5e0
TT
3551 use_deprecated_index_sections,
3552 &dwarf2_per_objfile->gdb_index, &local_map,
3553 &cu_list, &cu_list_elements,
3554 &types_list, &types_list_elements))
3555 return 0;
3556
0fefef59 3557 /* Don't use the index if it's empty. */
f00a2de2 3558 if (local_map.symbol_table.empty ())
0fefef59
DE
3559 return 0;
3560
2ec9a5e0
TT
3561 /* If there is a .dwz file, read it so we can get its CU list as
3562 well. */
ed2dc618 3563 dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
4db1a1dc 3564 if (dwz != NULL)
2ec9a5e0 3565 {
2ec9a5e0
TT
3566 struct mapped_index dwz_map;
3567 const gdb_byte *dwz_types_ignore;
3568 offset_type dwz_types_elements_ignore;
3569
3570 if (!read_index_from_section (objfile, bfd_get_filename (dwz->dwz_bfd),
3571 1,
3572 &dwz->gdb_index, &dwz_map,
3573 &dwz_list, &dwz_list_elements,
3574 &dwz_types_ignore,
3575 &dwz_types_elements_ignore))
3576 {
3577 warning (_("could not read '.gdb_index' section from %s; skipping"),
3578 bfd_get_filename (dwz->dwz_bfd));
3579 return 0;
3580 }
3581 }
3582
12359b5e
SM
3583 create_cus_from_index (dwarf2_per_objfile, cu_list, cu_list_elements,
3584 dwz_list, dwz_list_elements);
1fd400ff 3585
8b70b953
TT
3586 if (types_list_elements)
3587 {
3588 struct dwarf2_section_info *section;
3589
3590 /* We can only handle a single .debug_types when we have an
3591 index. */
3592 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) != 1)
3593 return 0;
3594
3595 section = VEC_index (dwarf2_section_info_def,
3596 dwarf2_per_objfile->types, 0);
3597
12359b5e
SM
3598 create_signatured_type_table_from_index (dwarf2_per_objfile, section,
3599 types_list, types_list_elements);
8b70b953 3600 }
9291a0cd 3601
ed2dc618 3602 create_addrmap_from_index (dwarf2_per_objfile, &local_map);
2ec9a5e0 3603
8d749320 3604 map = XOBNEW (&objfile->objfile_obstack, struct mapped_index);
3f563c84 3605 map = new (map) mapped_index ();
2ec9a5e0 3606 *map = local_map;
9291a0cd
TT
3607
3608 dwarf2_per_objfile->index_table = map;
3609 dwarf2_per_objfile->using_index = 1;
7b9f3c50 3610 dwarf2_per_objfile->quick_file_names_table =
b76e467d 3611 create_quick_file_names_table (dwarf2_per_objfile->all_comp_units.size ());
9291a0cd
TT
3612
3613 return 1;
3614}
3615
dee91e82 3616/* die_reader_func for dw2_get_file_names. */
2fdf6df6 3617
dee91e82
DE
3618static void
3619dw2_get_file_names_reader (const struct die_reader_specs *reader,
d521ce57 3620 const gdb_byte *info_ptr,
dee91e82
DE
3621 struct die_info *comp_unit_die,
3622 int has_children,
3623 void *data)
9291a0cd 3624{
dee91e82 3625 struct dwarf2_cu *cu = reader->cu;
ed2dc618 3626 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
518817b3
SM
3627 struct dwarf2_per_objfile *dwarf2_per_objfile
3628 = cu->per_cu->dwarf2_per_objfile;
dee91e82 3629 struct objfile *objfile = dwarf2_per_objfile->objfile;
094b34ac 3630 struct dwarf2_per_cu_data *lh_cu;
9291a0cd 3631 struct attribute *attr;
dee91e82 3632 int i;
7b9f3c50
DE
3633 void **slot;
3634 struct quick_file_names *qfn;
9291a0cd 3635
0186c6a7
DE
3636 gdb_assert (! this_cu->is_debug_types);
3637
07261596
TT
3638 /* Our callers never want to match partial units -- instead they
3639 will match the enclosing full CU. */
3640 if (comp_unit_die->tag == DW_TAG_partial_unit)
3641 {
3642 this_cu->v.quick->no_file_data = 1;
3643 return;
3644 }
3645
0186c6a7 3646 lh_cu = this_cu;
7b9f3c50 3647 slot = NULL;
dee91e82 3648
fff8551c 3649 line_header_up lh;
9c541725 3650 sect_offset line_offset {};
fff8551c 3651
dee91e82 3652 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
9291a0cd
TT
3653 if (attr)
3654 {
7b9f3c50
DE
3655 struct quick_file_names find_entry;
3656
9c541725 3657 line_offset = (sect_offset) DW_UNSND (attr);
7b9f3c50
DE
3658
3659 /* We may have already read in this line header (TU line header sharing).
3660 If we have we're done. */
094b34ac 3661 find_entry.hash.dwo_unit = cu->dwo_unit;
9c541725 3662 find_entry.hash.line_sect_off = line_offset;
7b9f3c50
DE
3663 slot = htab_find_slot (dwarf2_per_objfile->quick_file_names_table,
3664 &find_entry, INSERT);
3665 if (*slot != NULL)
3666 {
9a3c8263 3667 lh_cu->v.quick->file_names = (struct quick_file_names *) *slot;
dee91e82 3668 return;
7b9f3c50
DE
3669 }
3670
3019eac3 3671 lh = dwarf_decode_line_header (line_offset, cu);
9291a0cd
TT
3672 }
3673 if (lh == NULL)
3674 {
094b34ac 3675 lh_cu->v.quick->no_file_data = 1;
dee91e82 3676 return;
9291a0cd
TT
3677 }
3678
8d749320 3679 qfn = XOBNEW (&objfile->objfile_obstack, struct quick_file_names);
094b34ac 3680 qfn->hash.dwo_unit = cu->dwo_unit;
9c541725 3681 qfn->hash.line_sect_off = line_offset;
7b9f3c50
DE
3682 gdb_assert (slot != NULL);
3683 *slot = qfn;
9291a0cd 3684
d721ba37 3685 file_and_directory fnd = find_file_and_directory (comp_unit_die, cu);
9291a0cd 3686
fff8551c 3687 qfn->num_file_names = lh->file_names.size ();
8d749320 3688 qfn->file_names =
fff8551c
PA
3689 XOBNEWVEC (&objfile->objfile_obstack, const char *, lh->file_names.size ());
3690 for (i = 0; i < lh->file_names.size (); ++i)
3691 qfn->file_names[i] = file_full_name (i + 1, lh.get (), fnd.comp_dir);
7b9f3c50 3692 qfn->real_names = NULL;
9291a0cd 3693
094b34ac 3694 lh_cu->v.quick->file_names = qfn;
dee91e82
DE
3695}
3696
3697/* A helper for the "quick" functions which attempts to read the line
3698 table for THIS_CU. */
3699
3700static struct quick_file_names *
e4a48d9d 3701dw2_get_file_names (struct dwarf2_per_cu_data *this_cu)
dee91e82 3702{
0186c6a7
DE
3703 /* This should never be called for TUs. */
3704 gdb_assert (! this_cu->is_debug_types);
3705 /* Nor type unit groups. */
3706 gdb_assert (! IS_TYPE_UNIT_GROUP (this_cu));
f4dc4d17 3707
dee91e82
DE
3708 if (this_cu->v.quick->file_names != NULL)
3709 return this_cu->v.quick->file_names;
3710 /* If we know there is no line data, no point in looking again. */
3711 if (this_cu->v.quick->no_file_data)
3712 return NULL;
3713
0186c6a7 3714 init_cutu_and_read_dies_simple (this_cu, dw2_get_file_names_reader, NULL);
dee91e82
DE
3715
3716 if (this_cu->v.quick->no_file_data)
3717 return NULL;
3718 return this_cu->v.quick->file_names;
9291a0cd
TT
3719}
3720
3721/* A helper for the "quick" functions which computes and caches the
7b9f3c50 3722 real path for a given file name from the line table. */
2fdf6df6 3723
9291a0cd 3724static const char *
7b9f3c50
DE
3725dw2_get_real_path (struct objfile *objfile,
3726 struct quick_file_names *qfn, int index)
9291a0cd 3727{
7b9f3c50
DE
3728 if (qfn->real_names == NULL)
3729 qfn->real_names = OBSTACK_CALLOC (&objfile->objfile_obstack,
26f2dc30 3730 qfn->num_file_names, const char *);
9291a0cd 3731
7b9f3c50 3732 if (qfn->real_names[index] == NULL)
14278e1f 3733 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]).release ();
9291a0cd 3734
7b9f3c50 3735 return qfn->real_names[index];
9291a0cd
TT
3736}
3737
3738static struct symtab *
3739dw2_find_last_source_symtab (struct objfile *objfile)
3740{
ed2dc618
SM
3741 struct dwarf2_per_objfile *dwarf2_per_objfile
3742 = get_dwarf2_per_objfile (objfile);
b76e467d 3743 dwarf2_per_cu_data *dwarf_cu = dwarf2_per_objfile->all_comp_units.back ();
ed2dc618 3744 compunit_symtab *cust = dw2_instantiate_symtab (dwarf_cu);
ae2de4f8 3745
43f3e411
DE
3746 if (cust == NULL)
3747 return NULL;
ed2dc618 3748
43f3e411 3749 return compunit_primary_filetab (cust);
9291a0cd
TT
3750}
3751
7b9f3c50
DE
3752/* Traversal function for dw2_forget_cached_source_info. */
3753
3754static int
3755dw2_free_cached_file_names (void **slot, void *info)
9291a0cd 3756{
7b9f3c50 3757 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
9291a0cd 3758
7b9f3c50 3759 if (file_data->real_names)
9291a0cd 3760 {
7b9f3c50 3761 int i;
9291a0cd 3762
7b9f3c50 3763 for (i = 0; i < file_data->num_file_names; ++i)
9291a0cd 3764 {
7b9f3c50
DE
3765 xfree ((void*) file_data->real_names[i]);
3766 file_data->real_names[i] = NULL;
9291a0cd
TT
3767 }
3768 }
7b9f3c50
DE
3769
3770 return 1;
3771}
3772
3773static void
3774dw2_forget_cached_source_info (struct objfile *objfile)
3775{
ed2dc618
SM
3776 struct dwarf2_per_objfile *dwarf2_per_objfile
3777 = get_dwarf2_per_objfile (objfile);
7b9f3c50
DE
3778
3779 htab_traverse_noresize (dwarf2_per_objfile->quick_file_names_table,
3780 dw2_free_cached_file_names, NULL);
9291a0cd
TT
3781}
3782
f8eba3c6
TT
3783/* Helper function for dw2_map_symtabs_matching_filename that expands
3784 the symtabs and calls the iterator. */
3785
3786static int
3787dw2_map_expand_apply (struct objfile *objfile,
3788 struct dwarf2_per_cu_data *per_cu,
f5b95b50 3789 const char *name, const char *real_path,
14bc53a8 3790 gdb::function_view<bool (symtab *)> callback)
f8eba3c6 3791{
43f3e411 3792 struct compunit_symtab *last_made = objfile->compunit_symtabs;
f8eba3c6
TT
3793
3794 /* Don't visit already-expanded CUs. */
43f3e411 3795 if (per_cu->v.quick->compunit_symtab)
f8eba3c6
TT
3796 return 0;
3797
3798 /* This may expand more than one symtab, and we want to iterate over
3799 all of them. */
a0f42c21 3800 dw2_instantiate_symtab (per_cu);
f8eba3c6 3801
14bc53a8
PA
3802 return iterate_over_some_symtabs (name, real_path, objfile->compunit_symtabs,
3803 last_made, callback);
f8eba3c6
TT
3804}
3805
3806/* Implementation of the map_symtabs_matching_filename method. */
3807
14bc53a8
PA
3808static bool
3809dw2_map_symtabs_matching_filename
3810 (struct objfile *objfile, const char *name, const char *real_path,
3811 gdb::function_view<bool (symtab *)> callback)
9291a0cd 3812{
c011a4f4 3813 const char *name_basename = lbasename (name);
ed2dc618
SM
3814 struct dwarf2_per_objfile *dwarf2_per_objfile
3815 = get_dwarf2_per_objfile (objfile);
ae2de4f8 3816
848e3e78
DE
3817 /* The rule is CUs specify all the files, including those used by
3818 any TU, so there's no need to scan TUs here. */
f4dc4d17 3819
b76e467d 3820 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
9291a0cd 3821 {
3d7bb9d9 3822 /* We only need to look at symtabs not already expanded. */
43f3e411 3823 if (per_cu->v.quick->compunit_symtab)
9291a0cd
TT
3824 continue;
3825
b76e467d 3826 quick_file_names *file_data = dw2_get_file_names (per_cu);
7b9f3c50 3827 if (file_data == NULL)
9291a0cd
TT
3828 continue;
3829
b76e467d 3830 for (int j = 0; j < file_data->num_file_names; ++j)
9291a0cd 3831 {
7b9f3c50 3832 const char *this_name = file_data->file_names[j];
da235a7c 3833 const char *this_real_name;
9291a0cd 3834
af529f8f 3835 if (compare_filenames_for_search (this_name, name))
9291a0cd 3836 {
f5b95b50 3837 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
14bc53a8
PA
3838 callback))
3839 return true;
288e77a7 3840 continue;
4aac40c8 3841 }
9291a0cd 3842
c011a4f4
DE
3843 /* Before we invoke realpath, which can get expensive when many
3844 files are involved, do a quick comparison of the basenames. */
3845 if (! basenames_may_differ
3846 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
3847 continue;
3848
da235a7c
JK
3849 this_real_name = dw2_get_real_path (objfile, file_data, j);
3850 if (compare_filenames_for_search (this_real_name, name))
9291a0cd 3851 {
da235a7c 3852 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
14bc53a8
PA
3853 callback))
3854 return true;
288e77a7 3855 continue;
da235a7c 3856 }
9291a0cd 3857
da235a7c
JK
3858 if (real_path != NULL)
3859 {
af529f8f
JK
3860 gdb_assert (IS_ABSOLUTE_PATH (real_path));
3861 gdb_assert (IS_ABSOLUTE_PATH (name));
7b9f3c50 3862 if (this_real_name != NULL
af529f8f 3863 && FILENAME_CMP (real_path, this_real_name) == 0)
9291a0cd 3864 {
f5b95b50 3865 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
14bc53a8
PA
3866 callback))
3867 return true;
288e77a7 3868 continue;
9291a0cd
TT
3869 }
3870 }
3871 }
3872 }
3873
14bc53a8 3874 return false;
9291a0cd
TT
3875}
3876
da51c347
DE
3877/* Struct used to manage iterating over all CUs looking for a symbol. */
3878
3879struct dw2_symtab_iterator
9291a0cd 3880{
ed2dc618
SM
3881 /* The dwarf2_per_objfile owning the CUs we are iterating on. */
3882 struct dwarf2_per_objfile *dwarf2_per_objfile;
da51c347
DE
3883 /* If non-zero, only look for symbols that match BLOCK_INDEX. */
3884 int want_specific_block;
3885 /* One of GLOBAL_BLOCK or STATIC_BLOCK.
3886 Unused if !WANT_SPECIFIC_BLOCK. */
3887 int block_index;
3888 /* The kind of symbol we're looking for. */
3889 domain_enum domain;
3890 /* The list of CUs from the index entry of the symbol,
3891 or NULL if not found. */
3892 offset_type *vec;
3893 /* The next element in VEC to look at. */
3894 int next;
3895 /* The number of elements in VEC, or zero if there is no match. */
3896 int length;
8943b874
DE
3897 /* Have we seen a global version of the symbol?
3898 If so we can ignore all further global instances.
3899 This is to work around gold/15646, inefficient gold-generated
3900 indices. */
3901 int global_seen;
da51c347 3902};
9291a0cd 3903
da51c347
DE
3904/* Initialize the index symtab iterator ITER.
3905 If WANT_SPECIFIC_BLOCK is non-zero, only look for symbols
3906 in block BLOCK_INDEX. Otherwise BLOCK_INDEX is ignored. */
2fdf6df6 3907
9291a0cd 3908static void
da51c347 3909dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
ed2dc618 3910 struct dwarf2_per_objfile *dwarf2_per_objfile,
da51c347
DE
3911 int want_specific_block,
3912 int block_index,
3913 domain_enum domain,
3914 const char *name)
3915{
ed2dc618 3916 iter->dwarf2_per_objfile = dwarf2_per_objfile;
da51c347
DE
3917 iter->want_specific_block = want_specific_block;
3918 iter->block_index = block_index;
3919 iter->domain = domain;
3920 iter->next = 0;
8943b874 3921 iter->global_seen = 0;
da51c347 3922
ed2dc618
SM
3923 mapped_index *index = dwarf2_per_objfile->index_table;
3924
3925 /* index is NULL if OBJF_READNOW. */
3926 if (index != NULL && find_slot_in_mapped_hash (index, name, &iter->vec))
da51c347
DE
3927 iter->length = MAYBE_SWAP (*iter->vec);
3928 else
3929 {
3930 iter->vec = NULL;
3931 iter->length = 0;
3932 }
3933}
3934
3935/* Return the next matching CU or NULL if there are no more. */
3936
3937static struct dwarf2_per_cu_data *
3938dw2_symtab_iter_next (struct dw2_symtab_iterator *iter)
3939{
ed2dc618
SM
3940 struct dwarf2_per_objfile *dwarf2_per_objfile = iter->dwarf2_per_objfile;
3941
da51c347
DE
3942 for ( ; iter->next < iter->length; ++iter->next)
3943 {
3944 offset_type cu_index_and_attrs =
3945 MAYBE_SWAP (iter->vec[iter->next + 1]);
3946 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
da51c347
DE
3947 int want_static = iter->block_index != GLOBAL_BLOCK;
3948 /* This value is only valid for index versions >= 7. */
3949 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
3950 gdb_index_symbol_kind symbol_kind =
3951 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3952 /* Only check the symbol attributes if they're present.
3953 Indices prior to version 7 don't record them,
3954 and indices >= 7 may elide them for certain symbols
3955 (gold does this). */
3956 int attrs_valid =
ed2dc618 3957 (dwarf2_per_objfile->index_table->version >= 7
da51c347
DE
3958 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
3959
3190f0c6 3960 /* Don't crash on bad data. */
b76e467d 3961 if (cu_index >= (dwarf2_per_objfile->all_comp_units.size ()
b2bdb8cf 3962 + dwarf2_per_objfile->all_type_units.size ()))
3190f0c6
DE
3963 {
3964 complaint (&symfile_complaints,
3965 _(".gdb_index entry has bad CU index"
4262abfb
JK
3966 " [in module %s]"),
3967 objfile_name (dwarf2_per_objfile->objfile));
3190f0c6
DE
3968 continue;
3969 }
3970
ff4c9fec 3971 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (cu_index);
3190f0c6 3972
da51c347 3973 /* Skip if already read in. */
43f3e411 3974 if (per_cu->v.quick->compunit_symtab)
da51c347
DE
3975 continue;
3976
8943b874
DE
3977 /* Check static vs global. */
3978 if (attrs_valid)
3979 {
3980 if (iter->want_specific_block
3981 && want_static != is_static)
3982 continue;
3983 /* Work around gold/15646. */
3984 if (!is_static && iter->global_seen)
3985 continue;
3986 if (!is_static)
3987 iter->global_seen = 1;
3988 }
da51c347
DE
3989
3990 /* Only check the symbol's kind if it has one. */
3991 if (attrs_valid)
3992 {
3993 switch (iter->domain)
3994 {
3995 case VAR_DOMAIN:
3996 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
3997 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
3998 /* Some types are also in VAR_DOMAIN. */
3999 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
4000 continue;
4001 break;
4002 case STRUCT_DOMAIN:
4003 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
4004 continue;
4005 break;
4006 case LABEL_DOMAIN:
4007 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
4008 continue;
4009 break;
4010 default:
4011 break;
4012 }
4013 }
4014
4015 ++iter->next;
4016 return per_cu;
4017 }
4018
4019 return NULL;
4020}
4021
43f3e411 4022static struct compunit_symtab *
da51c347
DE
4023dw2_lookup_symbol (struct objfile *objfile, int block_index,
4024 const char *name, domain_enum domain)
9291a0cd 4025{
43f3e411 4026 struct compunit_symtab *stab_best = NULL;
ed2dc618
SM
4027 struct dwarf2_per_objfile *dwarf2_per_objfile
4028 = get_dwarf2_per_objfile (objfile);
9291a0cd 4029
b5ec771e
PA
4030 lookup_name_info lookup_name (name, symbol_name_match_type::FULL);
4031
ed2dc618
SM
4032 struct dw2_symtab_iterator iter;
4033 struct dwarf2_per_cu_data *per_cu;
da51c347 4034
ed2dc618 4035 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, 1, block_index, domain, name);
9291a0cd 4036
ed2dc618
SM
4037 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
4038 {
4039 struct symbol *sym, *with_opaque = NULL;
4040 struct compunit_symtab *stab = dw2_instantiate_symtab (per_cu);
4041 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
4042 struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
da51c347 4043
ed2dc618
SM
4044 sym = block_find_symbol (block, name, domain,
4045 block_find_non_opaque_type_preferred,
4046 &with_opaque);
b2e2f908 4047
ed2dc618
SM
4048 /* Some caution must be observed with overloaded functions
4049 and methods, since the index will not contain any overload
4050 information (but NAME might contain it). */
da51c347 4051
ed2dc618
SM
4052 if (sym != NULL
4053 && SYMBOL_MATCHES_SEARCH_NAME (sym, lookup_name))
4054 return stab;
4055 if (with_opaque != NULL
4056 && SYMBOL_MATCHES_SEARCH_NAME (with_opaque, lookup_name))
4057 stab_best = stab;
da51c347 4058
ed2dc618 4059 /* Keep looking through other CUs. */
9291a0cd 4060 }
9291a0cd 4061
da51c347 4062 return stab_best;
9291a0cd
TT
4063}
4064
4065static void
4066dw2_print_stats (struct objfile *objfile)
4067{
ed2dc618
SM
4068 struct dwarf2_per_objfile *dwarf2_per_objfile
4069 = get_dwarf2_per_objfile (objfile);
b76e467d 4070 int total = (dwarf2_per_objfile->all_comp_units.size ()
b2bdb8cf 4071 + dwarf2_per_objfile->all_type_units.size ());
ed2dc618 4072 int count = 0;
9291a0cd 4073
ed2dc618 4074 for (int i = 0; i < total; ++i)
9291a0cd 4075 {
ff4c9fec 4076 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (i);
9291a0cd 4077
43f3e411 4078 if (!per_cu->v.quick->compunit_symtab)
9291a0cd
TT
4079 ++count;
4080 }
e4a48d9d 4081 printf_filtered (_(" Number of read CUs: %d\n"), total - count);
9291a0cd
TT
4082 printf_filtered (_(" Number of unread CUs: %d\n"), count);
4083}
4084
779bd270
DE
4085/* This dumps minimal information about the index.
4086 It is called via "mt print objfiles".
4087 One use is to verify .gdb_index has been loaded by the
4088 gdb.dwarf2/gdb-index.exp testcase. */
4089
9291a0cd
TT
4090static void
4091dw2_dump (struct objfile *objfile)
4092{
ed2dc618
SM
4093 struct dwarf2_per_objfile *dwarf2_per_objfile
4094 = get_dwarf2_per_objfile (objfile);
4095
779bd270
DE
4096 gdb_assert (dwarf2_per_objfile->using_index);
4097 printf_filtered (".gdb_index:");
4098 if (dwarf2_per_objfile->index_table != NULL)
4099 {
4100 printf_filtered (" version %d\n",
4101 dwarf2_per_objfile->index_table->version);
4102 }
4103 else
4104 printf_filtered (" faked for \"readnow\"\n");
4105 printf_filtered ("\n");
9291a0cd
TT
4106}
4107
4108static void
3189cb12
DE
4109dw2_relocate (struct objfile *objfile,
4110 const struct section_offsets *new_offsets,
4111 const struct section_offsets *delta)
9291a0cd
TT
4112{
4113 /* There's nothing to relocate here. */
4114}
4115
4116static void
4117dw2_expand_symtabs_for_function (struct objfile *objfile,
4118 const char *func_name)
4119{
ed2dc618
SM
4120 struct dwarf2_per_objfile *dwarf2_per_objfile
4121 = get_dwarf2_per_objfile (objfile);
da51c347 4122
ed2dc618
SM
4123 struct dw2_symtab_iterator iter;
4124 struct dwarf2_per_cu_data *per_cu;
da51c347 4125
ed2dc618
SM
4126 /* Note: It doesn't matter what we pass for block_index here. */
4127 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, 0, GLOBAL_BLOCK, VAR_DOMAIN,
4128 func_name);
da51c347 4129
ed2dc618
SM
4130 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
4131 dw2_instantiate_symtab (per_cu);
da51c347 4132
9291a0cd
TT
4133}
4134
4135static void
4136dw2_expand_all_symtabs (struct objfile *objfile)
4137{
ed2dc618
SM
4138 struct dwarf2_per_objfile *dwarf2_per_objfile
4139 = get_dwarf2_per_objfile (objfile);
b76e467d 4140 int total_units = (dwarf2_per_objfile->all_comp_units.size ()
b2bdb8cf 4141 + dwarf2_per_objfile->all_type_units.size ());
9291a0cd 4142
ed2dc618 4143 for (int i = 0; i < total_units; ++i)
9291a0cd 4144 {
ff4c9fec 4145 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (i);
9291a0cd 4146
a0f42c21 4147 dw2_instantiate_symtab (per_cu);
9291a0cd
TT
4148 }
4149}
4150
4151static void
652a8996
JK
4152dw2_expand_symtabs_with_fullname (struct objfile *objfile,
4153 const char *fullname)
9291a0cd 4154{
ed2dc618
SM
4155 struct dwarf2_per_objfile *dwarf2_per_objfile
4156 = get_dwarf2_per_objfile (objfile);
d4637a04
DE
4157
4158 /* We don't need to consider type units here.
4159 This is only called for examining code, e.g. expand_line_sal.
4160 There can be an order of magnitude (or more) more type units
4161 than comp units, and we avoid them if we can. */
4162
b76e467d 4163 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
9291a0cd 4164 {
3d7bb9d9 4165 /* We only need to look at symtabs not already expanded. */
43f3e411 4166 if (per_cu->v.quick->compunit_symtab)
9291a0cd
TT
4167 continue;
4168
b76e467d 4169 quick_file_names *file_data = dw2_get_file_names (per_cu);
7b9f3c50 4170 if (file_data == NULL)
9291a0cd
TT
4171 continue;
4172
b76e467d 4173 for (int j = 0; j < file_data->num_file_names; ++j)
9291a0cd 4174 {
652a8996
JK
4175 const char *this_fullname = file_data->file_names[j];
4176
4177 if (filename_cmp (this_fullname, fullname) == 0)
9291a0cd 4178 {
a0f42c21 4179 dw2_instantiate_symtab (per_cu);
9291a0cd
TT
4180 break;
4181 }
4182 }
4183 }
4184}
4185
9291a0cd 4186static void
ade7ed9e 4187dw2_map_matching_symbols (struct objfile *objfile,
fe978cb0 4188 const char * name, domain_enum domain,
ade7ed9e 4189 int global,
40658b94
PH
4190 int (*callback) (struct block *,
4191 struct symbol *, void *),
b5ec771e 4192 void *data, symbol_name_match_type match,
2edb89d3 4193 symbol_compare_ftype *ordered_compare)
9291a0cd 4194{
40658b94 4195 /* Currently unimplemented; used for Ada. The function can be called if the
a9e6a4bb
JK
4196 current language is Ada for a non-Ada objfile using GNU index. As Ada
4197 does not look for non-Ada symbols this function should just return. */
9291a0cd
TT
4198}
4199
b5ec771e
PA
4200/* Symbol name matcher for .gdb_index names.
4201
4202 Symbol names in .gdb_index have a few particularities:
4203
4204 - There's no indication of which is the language of each symbol.
4205
4206 Since each language has its own symbol name matching algorithm,
4207 and we don't know which language is the right one, we must match
3f563c84
PA
4208 each symbol against all languages. This would be a potential
4209 performance problem if it were not mitigated by the
4210 mapped_index::name_components lookup table, which significantly
4211 reduces the number of times we need to call into this matcher,
4212 making it a non-issue.
b5ec771e
PA
4213
4214 - Symbol names in the index have no overload (parameter)
4215 information. I.e., in C++, "foo(int)" and "foo(long)" both
4216 appear as "foo" in the index, for example.
4217
4218 This means that the lookup names passed to the symbol name
4219 matcher functions must have no parameter information either
4220 because (e.g.) symbol search name "foo" does not match
4221 lookup-name "foo(int)" [while swapping search name for lookup
4222 name would match].
4223*/
4224class gdb_index_symbol_name_matcher
4225{
4226public:
4227 /* Prepares the vector of comparison functions for LOOKUP_NAME. */
4228 gdb_index_symbol_name_matcher (const lookup_name_info &lookup_name);
4229
4230 /* Walk all the matcher routines and match SYMBOL_NAME against them.
4231 Returns true if any matcher matches. */
4232 bool matches (const char *symbol_name);
4233
4234private:
4235 /* A reference to the lookup name we're matching against. */
4236 const lookup_name_info &m_lookup_name;
4237
4238 /* A vector holding all the different symbol name matchers, for all
4239 languages. */
4240 std::vector<symbol_name_matcher_ftype *> m_symbol_name_matcher_funcs;
4241};
4242
4243gdb_index_symbol_name_matcher::gdb_index_symbol_name_matcher
4244 (const lookup_name_info &lookup_name)
4245 : m_lookup_name (lookup_name)
4246{
4247 /* Prepare the vector of comparison functions upfront, to avoid
4248 doing the same work for each symbol. Care is taken to avoid
4249 matching with the same matcher more than once if/when multiple
4250 languages use the same matcher function. */
4251 auto &matchers = m_symbol_name_matcher_funcs;
4252 matchers.reserve (nr_languages);
4253
4254 matchers.push_back (default_symbol_name_matcher);
4255
4256 for (int i = 0; i < nr_languages; i++)
4257 {
4258 const language_defn *lang = language_def ((enum language) i);
c63d3e8d 4259 symbol_name_matcher_ftype *name_matcher
618daa93 4260 = get_symbol_name_matcher (lang, m_lookup_name);
c63d3e8d
PA
4261
4262 /* Don't insert the same comparison routine more than once.
4263 Note that we do this linear walk instead of a seemingly
4264 cheaper sorted insert, or use a std::set or something like
4265 that, because relative order of function addresses is not
4266 stable. This is not a problem in practice because the number
4267 of supported languages is low, and the cost here is tiny
4268 compared to the number of searches we'll do afterwards using
4269 this object. */
4270 if (name_matcher != default_symbol_name_matcher
4271 && (std::find (matchers.begin (), matchers.end (), name_matcher)
4272 == matchers.end ()))
4273 matchers.push_back (name_matcher);
b5ec771e
PA
4274 }
4275}
4276
4277bool
4278gdb_index_symbol_name_matcher::matches (const char *symbol_name)
4279{
4280 for (auto matches_name : m_symbol_name_matcher_funcs)
4281 if (matches_name (symbol_name, m_lookup_name, NULL))
4282 return true;
4283
4284 return false;
4285}
4286
e1ef7d7a
PA
4287/* Starting from a search name, return the string that finds the upper
4288 bound of all strings that start with SEARCH_NAME in a sorted name
4289 list. Returns the empty string to indicate that the upper bound is
4290 the end of the list. */
4291
4292static std::string
4293make_sort_after_prefix_name (const char *search_name)
4294{
4295 /* When looking to complete "func", we find the upper bound of all
4296 symbols that start with "func" by looking for where we'd insert
4297 the closest string that would follow "func" in lexicographical
4298 order. Usually, that's "func"-with-last-character-incremented,
4299 i.e. "fund". Mind non-ASCII characters, though. Usually those
4300 will be UTF-8 multi-byte sequences, but we can't be certain.
4301 Especially mind the 0xff character, which is a valid character in
4302 non-UTF-8 source character sets (e.g. Latin1 'ÿ'), and we can't
4303 rule out compilers allowing it in identifiers. Note that
4304 conveniently, strcmp/strcasecmp are specified to compare
4305 characters interpreted as unsigned char. So what we do is treat
4306 the whole string as a base 256 number composed of a sequence of
4307 base 256 "digits" and add 1 to it. I.e., adding 1 to 0xff wraps
4308 to 0, and carries 1 to the following more-significant position.
4309 If the very first character in SEARCH_NAME ends up incremented
4310 and carries/overflows, then the upper bound is the end of the
4311 list. The string after the empty string is also the empty
4312 string.
4313
4314 Some examples of this operation:
4315
4316 SEARCH_NAME => "+1" RESULT
4317
4318 "abc" => "abd"
4319 "ab\xff" => "ac"
4320 "\xff" "a" "\xff" => "\xff" "b"
4321 "\xff" => ""
4322 "\xff\xff" => ""
4323 "" => ""
4324
4325 Then, with these symbols for example:
4326
4327 func
4328 func1
4329 fund
4330
4331 completing "func" looks for symbols between "func" and
4332 "func"-with-last-character-incremented, i.e. "fund" (exclusive),
4333 which finds "func" and "func1", but not "fund".
4334
4335 And with:
4336
4337 funcÿ (Latin1 'ÿ' [0xff])
4338 funcÿ1
4339 fund
4340
4341 completing "funcÿ" looks for symbols between "funcÿ" and "fund"
4342 (exclusive), which finds "funcÿ" and "funcÿ1", but not "fund".
4343
4344 And with:
4345
4346 ÿÿ (Latin1 'ÿ' [0xff])
4347 ÿÿ1
4348
4349 completing "ÿ" or "ÿÿ" looks for symbols between between "ÿÿ" and
4350 the end of the list.
4351 */
4352 std::string after = search_name;
4353 while (!after.empty () && (unsigned char) after.back () == 0xff)
4354 after.pop_back ();
4355 if (!after.empty ())
4356 after.back () = (unsigned char) after.back () + 1;
4357 return after;
4358}
4359
5c58de74 4360/* See declaration. */
61d96d7e 4361
5c58de74
PA
4362std::pair<std::vector<name_component>::const_iterator,
4363 std::vector<name_component>::const_iterator>
44ed8f3e 4364mapped_index_base::find_name_components_bounds
5c58de74 4365 (const lookup_name_info &lookup_name_without_params) const
3f563c84 4366{
5c58de74
PA
4367 auto *name_cmp
4368 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
3f563c84
PA
4369
4370 const char *cplus
c62446b1 4371 = lookup_name_without_params.cplus ().lookup_name ().c_str ();
9291a0cd 4372
3f563c84
PA
4373 /* Comparison function object for lower_bound that matches against a
4374 given symbol name. */
4375 auto lookup_compare_lower = [&] (const name_component &elem,
4376 const char *name)
4377 {
5c58de74 4378 const char *elem_qualified = this->symbol_name_at (elem.idx);
3f563c84
PA
4379 const char *elem_name = elem_qualified + elem.name_offset;
4380 return name_cmp (elem_name, name) < 0;
4381 };
4382
4383 /* Comparison function object for upper_bound that matches against a
4384 given symbol name. */
4385 auto lookup_compare_upper = [&] (const char *name,
4386 const name_component &elem)
4387 {
5c58de74 4388 const char *elem_qualified = this->symbol_name_at (elem.idx);
3f563c84
PA
4389 const char *elem_name = elem_qualified + elem.name_offset;
4390 return name_cmp (name, elem_name) < 0;
4391 };
4392
5c58de74
PA
4393 auto begin = this->name_components.begin ();
4394 auto end = this->name_components.end ();
3f563c84
PA
4395
4396 /* Find the lower bound. */
4397 auto lower = [&] ()
4398 {
5c58de74 4399 if (lookup_name_without_params.completion_mode () && cplus[0] == '\0')
3f563c84
PA
4400 return begin;
4401 else
4402 return std::lower_bound (begin, end, cplus, lookup_compare_lower);
4403 } ();
4404
4405 /* Find the upper bound. */
4406 auto upper = [&] ()
4407 {
5c58de74 4408 if (lookup_name_without_params.completion_mode ())
3f563c84 4409 {
e1ef7d7a
PA
4410 /* In completion mode, we want UPPER to point past all
4411 symbols names that have the same prefix. I.e., with
4412 these symbols, and completing "func":
4413
4414 function << lower bound
4415 function1
4416 other_function << upper bound
4417
4418 We find the upper bound by looking for the insertion
4419 point of "func"-with-last-character-incremented,
4420 i.e. "fund". */
4421 std::string after = make_sort_after_prefix_name (cplus);
4422 if (after.empty ())
3f563c84 4423 return end;
e6b2f5ef
PA
4424 return std::lower_bound (lower, end, after.c_str (),
4425 lookup_compare_lower);
3f563c84
PA
4426 }
4427 else
4428 return std::upper_bound (lower, end, cplus, lookup_compare_upper);
4429 } ();
4430
5c58de74
PA
4431 return {lower, upper};
4432}
4433
4434/* See declaration. */
4435
4436void
44ed8f3e 4437mapped_index_base::build_name_components ()
5c58de74
PA
4438{
4439 if (!this->name_components.empty ())
4440 return;
4441
4442 this->name_components_casing = case_sensitivity;
4443 auto *name_cmp
4444 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
4445
4446 /* The code below only knows how to break apart components of C++
4447 symbol names (and other languages that use '::' as
4448 namespace/module separator). If we add support for wild matching
4449 to some language that uses some other operator (E.g., Ada, Go and
4450 D use '.'), then we'll need to try splitting the symbol name
4451 according to that language too. Note that Ada does support wild
4452 matching, but doesn't currently support .gdb_index. */
44ed8f3e
PA
4453 auto count = this->symbol_name_count ();
4454 for (offset_type idx = 0; idx < count; idx++)
5c58de74 4455 {
44ed8f3e 4456 if (this->symbol_name_slot_invalid (idx))
5c58de74
PA
4457 continue;
4458
4459 const char *name = this->symbol_name_at (idx);
4460
4461 /* Add each name component to the name component table. */
4462 unsigned int previous_len = 0;
4463 for (unsigned int current_len = cp_find_first_component (name);
4464 name[current_len] != '\0';
4465 current_len += cp_find_first_component (name + current_len))
4466 {
4467 gdb_assert (name[current_len] == ':');
4468 this->name_components.push_back ({previous_len, idx});
4469 /* Skip the '::'. */
4470 current_len += 2;
4471 previous_len = current_len;
4472 }
4473 this->name_components.push_back ({previous_len, idx});
4474 }
4475
4476 /* Sort name_components elements by name. */
4477 auto name_comp_compare = [&] (const name_component &left,
4478 const name_component &right)
4479 {
4480 const char *left_qualified = this->symbol_name_at (left.idx);
4481 const char *right_qualified = this->symbol_name_at (right.idx);
4482
4483 const char *left_name = left_qualified + left.name_offset;
4484 const char *right_name = right_qualified + right.name_offset;
4485
4486 return name_cmp (left_name, right_name) < 0;
4487 };
4488
4489 std::sort (this->name_components.begin (),
4490 this->name_components.end (),
4491 name_comp_compare);
4492}
4493
4494/* Helper for dw2_expand_symtabs_matching that works with a
44ed8f3e
PA
4495 mapped_index_base instead of the containing objfile. This is split
4496 to a separate function in order to be able to unit test the
4497 name_components matching using a mock mapped_index_base. For each
5c58de74 4498 symbol name that matches, calls MATCH_CALLBACK, passing it the
44ed8f3e 4499 symbol's index in the mapped_index_base symbol table. */
5c58de74
PA
4500
4501static void
4502dw2_expand_symtabs_matching_symbol
44ed8f3e 4503 (mapped_index_base &index,
5c58de74
PA
4504 const lookup_name_info &lookup_name_in,
4505 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4506 enum search_domain kind,
4507 gdb::function_view<void (offset_type)> match_callback)
4508{
4509 lookup_name_info lookup_name_without_params
4510 = lookup_name_in.make_ignore_params ();
4511 gdb_index_symbol_name_matcher lookup_name_matcher
4512 (lookup_name_without_params);
4513
4514 /* Build the symbol name component sorted vector, if we haven't
4515 yet. */
4516 index.build_name_components ();
4517
4518 auto bounds = index.find_name_components_bounds (lookup_name_without_params);
4519
3f563c84
PA
4520 /* Now for each symbol name in range, check to see if we have a name
4521 match, and if so, call the MATCH_CALLBACK callback. */
4522
4523 /* The same symbol may appear more than once in the range though.
4524 E.g., if we're looking for symbols that complete "w", and we have
4525 a symbol named "w1::w2", we'll find the two name components for
4526 that same symbol in the range. To be sure we only call the
4527 callback once per symbol, we first collect the symbol name
4528 indexes that matched in a temporary vector and ignore
4529 duplicates. */
4530 std::vector<offset_type> matches;
5c58de74 4531 matches.reserve (std::distance (bounds.first, bounds.second));
3f563c84 4532
5c58de74 4533 for (; bounds.first != bounds.second; ++bounds.first)
3f563c84 4534 {
5c58de74 4535 const char *qualified = index.symbol_name_at (bounds.first->idx);
3f563c84
PA
4536
4537 if (!lookup_name_matcher.matches (qualified)
4538 || (symbol_matcher != NULL && !symbol_matcher (qualified)))
9291a0cd
TT
4539 continue;
4540
5c58de74 4541 matches.push_back (bounds.first->idx);
3f563c84
PA
4542 }
4543
4544 std::sort (matches.begin (), matches.end ());
4545
4546 /* Finally call the callback, once per match. */
4547 ULONGEST prev = -1;
4548 for (offset_type idx : matches)
4549 {
4550 if (prev != idx)
4551 {
4552 match_callback (idx);
4553 prev = idx;
4554 }
4555 }
4556
4557 /* Above we use a type wider than idx's for 'prev', since 0 and
4558 (offset_type)-1 are both possible values. */
4559 static_assert (sizeof (prev) > sizeof (offset_type), "");
4560}
4561
c62446b1
PA
4562#if GDB_SELF_TEST
4563
4564namespace selftests { namespace dw2_expand_symtabs_matching {
4565
a3c5fafd
PA
4566/* A mock .gdb_index/.debug_names-like name index table, enough to
4567 exercise dw2_expand_symtabs_matching_symbol, which works with the
4568 mapped_index_base interface. Builds an index from the symbol list
4569 passed as parameter to the constructor. */
4570class mock_mapped_index : public mapped_index_base
c62446b1
PA
4571{
4572public:
a3c5fafd
PA
4573 mock_mapped_index (gdb::array_view<const char *> symbols)
4574 : m_symbol_table (symbols)
c62446b1
PA
4575 {}
4576
a3c5fafd 4577 DISABLE_COPY_AND_ASSIGN (mock_mapped_index);
c62446b1 4578
a3c5fafd
PA
4579 /* Return the number of names in the symbol table. */
4580 virtual size_t symbol_name_count () const
c62446b1 4581 {
a3c5fafd 4582 return m_symbol_table.size ();
c62446b1
PA
4583 }
4584
a3c5fafd
PA
4585 /* Get the name of the symbol at IDX in the symbol table. */
4586 virtual const char *symbol_name_at (offset_type idx) const
4587 {
4588 return m_symbol_table[idx];
4589 }
c62446b1 4590
a3c5fafd
PA
4591private:
4592 gdb::array_view<const char *> m_symbol_table;
c62446b1
PA
4593};
4594
4595/* Convenience function that converts a NULL pointer to a "<null>"
4596 string, to pass to print routines. */
4597
4598static const char *
4599string_or_null (const char *str)
4600{
4601 return str != NULL ? str : "<null>";
4602}
4603
4604/* Check if a lookup_name_info built from
4605 NAME/MATCH_TYPE/COMPLETION_MODE matches the symbols in the mock
4606 index. EXPECTED_LIST is the list of expected matches, in expected
4607 matching order. If no match expected, then an empty list is
4608 specified. Returns true on success. On failure prints a warning
4609 indicating the file:line that failed, and returns false. */
4610
4611static bool
4612check_match (const char *file, int line,
4613 mock_mapped_index &mock_index,
4614 const char *name, symbol_name_match_type match_type,
4615 bool completion_mode,
4616 std::initializer_list<const char *> expected_list)
4617{
4618 lookup_name_info lookup_name (name, match_type, completion_mode);
4619
4620 bool matched = true;
4621
4622 auto mismatch = [&] (const char *expected_str,
4623 const char *got)
4624 {
4625 warning (_("%s:%d: match_type=%s, looking-for=\"%s\", "
4626 "expected=\"%s\", got=\"%s\"\n"),
4627 file, line,
4628 (match_type == symbol_name_match_type::FULL
4629 ? "FULL" : "WILD"),
4630 name, string_or_null (expected_str), string_or_null (got));
4631 matched = false;
4632 };
4633
4634 auto expected_it = expected_list.begin ();
4635 auto expected_end = expected_list.end ();
4636
a3c5fafd 4637 dw2_expand_symtabs_matching_symbol (mock_index, lookup_name,
c62446b1
PA
4638 NULL, ALL_DOMAIN,
4639 [&] (offset_type idx)
4640 {
a3c5fafd 4641 const char *matched_name = mock_index.symbol_name_at (idx);
c62446b1
PA
4642 const char *expected_str
4643 = expected_it == expected_end ? NULL : *expected_it++;
4644
4645 if (expected_str == NULL || strcmp (expected_str, matched_name) != 0)
4646 mismatch (expected_str, matched_name);
4647 });
4648
4649 const char *expected_str
4650 = expected_it == expected_end ? NULL : *expected_it++;
4651 if (expected_str != NULL)
4652 mismatch (expected_str, NULL);
4653
4654 return matched;
4655}
4656
4657/* The symbols added to the mock mapped_index for testing (in
4658 canonical form). */
4659static const char *test_symbols[] = {
4660 "function",
4661 "std::bar",
4662 "std::zfunction",
4663 "std::zfunction2",
4664 "w1::w2",
4665 "ns::foo<char*>",
4666 "ns::foo<int>",
4667 "ns::foo<long>",
a20714ff
PA
4668 "ns2::tmpl<int>::foo2",
4669 "(anonymous namespace)::A::B::C",
c62446b1 4670
e1ef7d7a
PA
4671 /* These are used to check that the increment-last-char in the
4672 matching algorithm for completion doesn't match "t1_fund" when
4673 completing "t1_func". */
4674 "t1_func",
4675 "t1_func1",
4676 "t1_fund",
4677 "t1_fund1",
4678
4679 /* A UTF-8 name with multi-byte sequences to make sure that
4680 cp-name-parser understands this as a single identifier ("função"
4681 is "function" in PT). */
4682 u8"u8função",
4683
4684 /* \377 (0xff) is Latin1 'ÿ'. */
4685 "yfunc\377",
4686
4687 /* \377 (0xff) is Latin1 'ÿ'. */
4688 "\377",
4689 "\377\377123",
4690
c62446b1
PA
4691 /* A name with all sorts of complications. Starts with "z" to make
4692 it easier for the completion tests below. */
4693#define Z_SYM_NAME \
4694 "z::std::tuple<(anonymous namespace)::ui*, std::bar<(anonymous namespace)::ui> >" \
4695 "::tuple<(anonymous namespace)::ui*, " \
4696 "std::default_delete<(anonymous namespace)::ui>, void>"
4697
4698 Z_SYM_NAME
4699};
4700
a3c5fafd
PA
4701/* Returns true if the mapped_index_base::find_name_component_bounds
4702 method finds EXPECTED_SYMS in INDEX when looking for SEARCH_NAME,
4703 in completion mode. */
5c58de74
PA
4704
4705static bool
a3c5fafd 4706check_find_bounds_finds (mapped_index_base &index,
5c58de74
PA
4707 const char *search_name,
4708 gdb::array_view<const char *> expected_syms)
4709{
4710 lookup_name_info lookup_name (search_name,
4711 symbol_name_match_type::FULL, true);
4712
4713 auto bounds = index.find_name_components_bounds (lookup_name);
4714
4715 size_t distance = std::distance (bounds.first, bounds.second);
4716 if (distance != expected_syms.size ())
4717 return false;
4718
4719 for (size_t exp_elem = 0; exp_elem < distance; exp_elem++)
4720 {
4721 auto nc_elem = bounds.first + exp_elem;
4722 const char *qualified = index.symbol_name_at (nc_elem->idx);
4723 if (strcmp (qualified, expected_syms[exp_elem]) != 0)
4724 return false;
4725 }
4726
4727 return true;
4728}
4729
4730/* Test the lower-level mapped_index::find_name_component_bounds
4731 method. */
4732
c62446b1 4733static void
5c58de74
PA
4734test_mapped_index_find_name_component_bounds ()
4735{
4736 mock_mapped_index mock_index (test_symbols);
4737
a3c5fafd 4738 mock_index.build_name_components ();
5c58de74
PA
4739
4740 /* Test the lower-level mapped_index::find_name_component_bounds
4741 method in completion mode. */
4742 {
4743 static const char *expected_syms[] = {
4744 "t1_func",
4745 "t1_func1",
5c58de74
PA
4746 };
4747
a3c5fafd 4748 SELF_CHECK (check_find_bounds_finds (mock_index,
5c58de74
PA
4749 "t1_func", expected_syms));
4750 }
4751
4752 /* Check that the increment-last-char in the name matching algorithm
4753 for completion doesn't get confused with Ansi1 'ÿ' / 0xff. */
4754 {
4755 static const char *expected_syms1[] = {
4756 "\377",
4757 "\377\377123",
4758 };
a3c5fafd 4759 SELF_CHECK (check_find_bounds_finds (mock_index,
5c58de74
PA
4760 "\377", expected_syms1));
4761
4762 static const char *expected_syms2[] = {
4763 "\377\377123",
4764 };
a3c5fafd 4765 SELF_CHECK (check_find_bounds_finds (mock_index,
5c58de74
PA
4766 "\377\377", expected_syms2));
4767 }
4768}
4769
4770/* Test dw2_expand_symtabs_matching_symbol. */
4771
4772static void
4773test_dw2_expand_symtabs_matching_symbol ()
c62446b1
PA
4774{
4775 mock_mapped_index mock_index (test_symbols);
4776
4777 /* We let all tests run until the end even if some fails, for debug
4778 convenience. */
4779 bool any_mismatch = false;
4780
4781 /* Create the expected symbols list (an initializer_list). Needed
4782 because lists have commas, and we need to pass them to CHECK,
4783 which is a macro. */
4784#define EXPECT(...) { __VA_ARGS__ }
4785
4786 /* Wrapper for check_match that passes down the current
4787 __FILE__/__LINE__. */
4788#define CHECK_MATCH(NAME, MATCH_TYPE, COMPLETION_MODE, EXPECTED_LIST) \
4789 any_mismatch |= !check_match (__FILE__, __LINE__, \
4790 mock_index, \
4791 NAME, MATCH_TYPE, COMPLETION_MODE, \
4792 EXPECTED_LIST)
4793
4794 /* Identity checks. */
4795 for (const char *sym : test_symbols)
4796 {
4797 /* Should be able to match all existing symbols. */
4798 CHECK_MATCH (sym, symbol_name_match_type::FULL, false,
4799 EXPECT (sym));
4800
4801 /* Should be able to match all existing symbols with
4802 parameters. */
4803 std::string with_params = std::string (sym) + "(int)";
4804 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4805 EXPECT (sym));
4806
4807 /* Should be able to match all existing symbols with
4808 parameters and qualifiers. */
4809 with_params = std::string (sym) + " ( int ) const";
4810 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4811 EXPECT (sym));
4812
4813 /* This should really find sym, but cp-name-parser.y doesn't
4814 know about lvalue/rvalue qualifiers yet. */
4815 with_params = std::string (sym) + " ( int ) &&";
4816 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4817 {});
4818 }
4819
e1ef7d7a
PA
4820 /* Check that the name matching algorithm for completion doesn't get
4821 confused with Latin1 'ÿ' / 0xff. */
4822 {
4823 static const char str[] = "\377";
4824 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4825 EXPECT ("\377", "\377\377123"));
4826 }
4827
4828 /* Check that the increment-last-char in the matching algorithm for
4829 completion doesn't match "t1_fund" when completing "t1_func". */
4830 {
4831 static const char str[] = "t1_func";
4832 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4833 EXPECT ("t1_func", "t1_func1"));
4834 }
4835
c62446b1
PA
4836 /* Check that completion mode works at each prefix of the expected
4837 symbol name. */
4838 {
4839 static const char str[] = "function(int)";
4840 size_t len = strlen (str);
4841 std::string lookup;
4842
4843 for (size_t i = 1; i < len; i++)
4844 {
4845 lookup.assign (str, i);
4846 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4847 EXPECT ("function"));
4848 }
4849 }
4850
4851 /* While "w" is a prefix of both components, the match function
4852 should still only be called once. */
4853 {
4854 CHECK_MATCH ("w", symbol_name_match_type::FULL, true,
4855 EXPECT ("w1::w2"));
a20714ff
PA
4856 CHECK_MATCH ("w", symbol_name_match_type::WILD, true,
4857 EXPECT ("w1::w2"));
c62446b1
PA
4858 }
4859
4860 /* Same, with a "complicated" symbol. */
4861 {
4862 static const char str[] = Z_SYM_NAME;
4863 size_t len = strlen (str);
4864 std::string lookup;
4865
4866 for (size_t i = 1; i < len; i++)
4867 {
4868 lookup.assign (str, i);
4869 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4870 EXPECT (Z_SYM_NAME));
4871 }
4872 }
4873
4874 /* In FULL mode, an incomplete symbol doesn't match. */
4875 {
4876 CHECK_MATCH ("std::zfunction(int", symbol_name_match_type::FULL, false,
4877 {});
4878 }
4879
4880 /* A complete symbol with parameters matches any overload, since the
4881 index has no overload info. */
4882 {
4883 CHECK_MATCH ("std::zfunction(int)", symbol_name_match_type::FULL, true,
4884 EXPECT ("std::zfunction", "std::zfunction2"));
a20714ff
PA
4885 CHECK_MATCH ("zfunction(int)", symbol_name_match_type::WILD, true,
4886 EXPECT ("std::zfunction", "std::zfunction2"));
4887 CHECK_MATCH ("zfunc", symbol_name_match_type::WILD, true,
4888 EXPECT ("std::zfunction", "std::zfunction2"));
c62446b1
PA
4889 }
4890
4891 /* Check that whitespace is ignored appropriately. A symbol with a
4892 template argument list. */
4893 {
4894 static const char expected[] = "ns::foo<int>";
4895 CHECK_MATCH ("ns :: foo < int > ", symbol_name_match_type::FULL, false,
4896 EXPECT (expected));
a20714ff
PA
4897 CHECK_MATCH ("foo < int > ", symbol_name_match_type::WILD, false,
4898 EXPECT (expected));
c62446b1
PA
4899 }
4900
4901 /* Check that whitespace is ignored appropriately. A symbol with a
4902 template argument list that includes a pointer. */
4903 {
4904 static const char expected[] = "ns::foo<char*>";
4905 /* Try both completion and non-completion modes. */
4906 static const bool completion_mode[2] = {false, true};
4907 for (size_t i = 0; i < 2; i++)
4908 {
4909 CHECK_MATCH ("ns :: foo < char * >", symbol_name_match_type::FULL,
4910 completion_mode[i], EXPECT (expected));
a20714ff
PA
4911 CHECK_MATCH ("foo < char * >", symbol_name_match_type::WILD,
4912 completion_mode[i], EXPECT (expected));
c62446b1
PA
4913
4914 CHECK_MATCH ("ns :: foo < char * > (int)", symbol_name_match_type::FULL,
4915 completion_mode[i], EXPECT (expected));
a20714ff
PA
4916 CHECK_MATCH ("foo < char * > (int)", symbol_name_match_type::WILD,
4917 completion_mode[i], EXPECT (expected));
c62446b1
PA
4918 }
4919 }
4920
4921 {
4922 /* Check method qualifiers are ignored. */
4923 static const char expected[] = "ns::foo<char*>";
4924 CHECK_MATCH ("ns :: foo < char * > ( int ) const",
4925 symbol_name_match_type::FULL, true, EXPECT (expected));
4926 CHECK_MATCH ("ns :: foo < char * > ( int ) &&",
4927 symbol_name_match_type::FULL, true, EXPECT (expected));
a20714ff
PA
4928 CHECK_MATCH ("foo < char * > ( int ) const",
4929 symbol_name_match_type::WILD, true, EXPECT (expected));
4930 CHECK_MATCH ("foo < char * > ( int ) &&",
4931 symbol_name_match_type::WILD, true, EXPECT (expected));
c62446b1
PA
4932 }
4933
4934 /* Test lookup names that don't match anything. */
4935 {
a20714ff
PA
4936 CHECK_MATCH ("bar2", symbol_name_match_type::WILD, false,
4937 {});
4938
c62446b1
PA
4939 CHECK_MATCH ("doesntexist", symbol_name_match_type::FULL, false,
4940 {});
4941 }
4942
a20714ff
PA
4943 /* Some wild matching tests, exercising "(anonymous namespace)",
4944 which should not be confused with a parameter list. */
4945 {
4946 static const char *syms[] = {
4947 "A::B::C",
4948 "B::C",
4949 "C",
4950 "A :: B :: C ( int )",
4951 "B :: C ( int )",
4952 "C ( int )",
4953 };
4954
4955 for (const char *s : syms)
4956 {
4957 CHECK_MATCH (s, symbol_name_match_type::WILD, false,
4958 EXPECT ("(anonymous namespace)::A::B::C"));
4959 }
4960 }
4961
4962 {
4963 static const char expected[] = "ns2::tmpl<int>::foo2";
4964 CHECK_MATCH ("tmp", symbol_name_match_type::WILD, true,
4965 EXPECT (expected));
4966 CHECK_MATCH ("tmpl<", symbol_name_match_type::WILD, true,
4967 EXPECT (expected));
4968 }
4969
c62446b1
PA
4970 SELF_CHECK (!any_mismatch);
4971
4972#undef EXPECT
4973#undef CHECK_MATCH
4974}
4975
5c58de74
PA
4976static void
4977run_test ()
4978{
4979 test_mapped_index_find_name_component_bounds ();
4980 test_dw2_expand_symtabs_matching_symbol ();
4981}
4982
c62446b1
PA
4983}} // namespace selftests::dw2_expand_symtabs_matching
4984
4985#endif /* GDB_SELF_TEST */
4986
4b514bc8
JK
4987/* If FILE_MATCHER is NULL or if PER_CU has
4988 dwarf2_per_cu_quick_data::MARK set (see
4989 dw_expand_symtabs_matching_file_matcher), expand the CU and call
4990 EXPANSION_NOTIFY on it. */
4991
4992static void
4993dw2_expand_symtabs_matching_one
4994 (struct dwarf2_per_cu_data *per_cu,
4995 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4996 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify)
4997{
4998 if (file_matcher == NULL || per_cu->v.quick->mark)
4999 {
5000 bool symtab_was_null
5001 = (per_cu->v.quick->compunit_symtab == NULL);
5002
5003 dw2_instantiate_symtab (per_cu);
5004
5005 if (expansion_notify != NULL
5006 && symtab_was_null
5007 && per_cu->v.quick->compunit_symtab != NULL)
5008 expansion_notify (per_cu->v.quick->compunit_symtab);
5009 }
5010}
5011
3f563c84
PA
5012/* Helper for dw2_expand_matching symtabs. Called on each symbol
5013 matched, to expand corresponding CUs that were marked. IDX is the
5014 index of the symbol name that matched. */
5015
5016static void
5017dw2_expand_marked_cus
ed2dc618 5018 (struct dwarf2_per_objfile *dwarf2_per_objfile, offset_type idx,
3f563c84
PA
5019 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
5020 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
5021 search_domain kind)
5022{
3f563c84
PA
5023 offset_type *vec, vec_len, vec_idx;
5024 bool global_seen = false;
ed2dc618 5025 mapped_index &index = *dwarf2_per_objfile->index_table;
3f563c84 5026
61920122 5027 vec = (offset_type *) (index.constant_pool
f00a2de2 5028 + MAYBE_SWAP (index.symbol_table[idx].vec));
61920122
PA
5029 vec_len = MAYBE_SWAP (vec[0]);
5030 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
5031 {
61920122
PA
5032 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[vec_idx + 1]);
5033 /* This value is only valid for index versions >= 7. */
5034 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
5035 gdb_index_symbol_kind symbol_kind =
5036 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
5037 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
5038 /* Only check the symbol attributes if they're present.
5039 Indices prior to version 7 don't record them,
5040 and indices >= 7 may elide them for certain symbols
5041 (gold does this). */
5042 int attrs_valid =
5043 (index.version >= 7
5044 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
5045
5046 /* Work around gold/15646. */
5047 if (attrs_valid)
9291a0cd 5048 {
61920122
PA
5049 if (!is_static && global_seen)
5050 continue;
5051 if (!is_static)
5052 global_seen = true;
5053 }
3190f0c6 5054
61920122
PA
5055 /* Only check the symbol's kind if it has one. */
5056 if (attrs_valid)
5057 {
5058 switch (kind)
8943b874 5059 {
61920122
PA
5060 case VARIABLES_DOMAIN:
5061 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
5062 continue;
5063 break;
5064 case FUNCTIONS_DOMAIN:
5065 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
8943b874 5066 continue;
61920122
PA
5067 break;
5068 case TYPES_DOMAIN:
5069 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
5070 continue;
5071 break;
5072 default:
5073 break;
8943b874 5074 }
61920122 5075 }
8943b874 5076
61920122 5077 /* Don't crash on bad data. */
b76e467d 5078 if (cu_index >= (dwarf2_per_objfile->all_comp_units.size ()
b2bdb8cf 5079 + dwarf2_per_objfile->all_type_units.size ()))
61920122
PA
5080 {
5081 complaint (&symfile_complaints,
5082 _(".gdb_index entry has bad CU index"
ed2dc618
SM
5083 " [in module %s]"),
5084 objfile_name (dwarf2_per_objfile->objfile));
61920122
PA
5085 continue;
5086 }
5087
ff4c9fec 5088 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (cu_index);
4b514bc8
JK
5089 dw2_expand_symtabs_matching_one (per_cu, file_matcher,
5090 expansion_notify);
61920122
PA
5091 }
5092}
5093
4b514bc8
JK
5094/* If FILE_MATCHER is non-NULL, set all the
5095 dwarf2_per_cu_quick_data::MARK of the current DWARF2_PER_OBJFILE
5096 that match FILE_MATCHER. */
5097
61920122 5098static void
4b514bc8 5099dw_expand_symtabs_matching_file_matcher
ed2dc618
SM
5100 (struct dwarf2_per_objfile *dwarf2_per_objfile,
5101 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher)
61920122 5102{
4b514bc8 5103 if (file_matcher == NULL)
61920122
PA
5104 return;
5105
4b514bc8
JK
5106 objfile *const objfile = dwarf2_per_objfile->objfile;
5107
5108 htab_up visited_found (htab_create_alloc (10, htab_hash_pointer,
5109 htab_eq_pointer,
5110 NULL, xcalloc, xfree));
5111 htab_up visited_not_found (htab_create_alloc (10, htab_hash_pointer,
61920122
PA
5112 htab_eq_pointer,
5113 NULL, xcalloc, xfree));
61920122 5114
4b514bc8
JK
5115 /* The rule is CUs specify all the files, including those used by
5116 any TU, so there's no need to scan TUs here. */
61920122 5117
b76e467d 5118 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
927aa2e7 5119 {
927aa2e7
JK
5120 QUIT;
5121
5122 per_cu->v.quick->mark = 0;
5123
5124 /* We only need to look at symtabs not already expanded. */
5125 if (per_cu->v.quick->compunit_symtab)
5126 continue;
5127
b76e467d 5128 quick_file_names *file_data = dw2_get_file_names (per_cu);
927aa2e7
JK
5129 if (file_data == NULL)
5130 continue;
5131
5132 if (htab_find (visited_not_found.get (), file_data) != NULL)
5133 continue;
5134 else if (htab_find (visited_found.get (), file_data) != NULL)
5135 {
5136 per_cu->v.quick->mark = 1;
5137 continue;
5138 }
5139
b76e467d 5140 for (int j = 0; j < file_data->num_file_names; ++j)
927aa2e7
JK
5141 {
5142 const char *this_real_name;
5143
5144 if (file_matcher (file_data->file_names[j], false))
5145 {
5146 per_cu->v.quick->mark = 1;
5147 break;
5148 }
5149
5150 /* Before we invoke realpath, which can get expensive when many
5151 files are involved, do a quick comparison of the basenames. */
5152 if (!basenames_may_differ
5153 && !file_matcher (lbasename (file_data->file_names[j]),
5154 true))
5155 continue;
5156
5157 this_real_name = dw2_get_real_path (objfile, file_data, j);
5158 if (file_matcher (this_real_name, false))
5159 {
5160 per_cu->v.quick->mark = 1;
5161 break;
5162 }
5163 }
5164
b76e467d
SM
5165 void **slot = htab_find_slot (per_cu->v.quick->mark
5166 ? visited_found.get ()
5167 : visited_not_found.get (),
5168 file_data, INSERT);
927aa2e7
JK
5169 *slot = file_data;
5170 }
5171}
5172
5173static void
5174dw2_expand_symtabs_matching
5175 (struct objfile *objfile,
5176 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
5177 const lookup_name_info &lookup_name,
5178 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
5179 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
5180 enum search_domain kind)
5181{
ed2dc618
SM
5182 struct dwarf2_per_objfile *dwarf2_per_objfile
5183 = get_dwarf2_per_objfile (objfile);
927aa2e7
JK
5184
5185 /* index_table is NULL if OBJF_READNOW. */
5186 if (!dwarf2_per_objfile->index_table)
5187 return;
5188
ed2dc618 5189 dw_expand_symtabs_matching_file_matcher (dwarf2_per_objfile, file_matcher);
927aa2e7
JK
5190
5191 mapped_index &index = *dwarf2_per_objfile->index_table;
5192
5193 dw2_expand_symtabs_matching_symbol (index, lookup_name,
5194 symbol_matcher,
5195 kind, [&] (offset_type idx)
5196 {
ed2dc618 5197 dw2_expand_marked_cus (dwarf2_per_objfile, idx, file_matcher,
927aa2e7
JK
5198 expansion_notify, kind);
5199 });
5200}
5201
5202/* A helper for dw2_find_pc_sect_compunit_symtab which finds the most specific
5203 symtab. */
5204
5205static struct compunit_symtab *
5206recursively_find_pc_sect_compunit_symtab (struct compunit_symtab *cust,
5207 CORE_ADDR pc)
5208{
5209 int i;
5210
5211 if (COMPUNIT_BLOCKVECTOR (cust) != NULL
5212 && blockvector_contains_pc (COMPUNIT_BLOCKVECTOR (cust), pc))
5213 return cust;
5214
5215 if (cust->includes == NULL)
5216 return NULL;
5217
5218 for (i = 0; cust->includes[i]; ++i)
5219 {
5220 struct compunit_symtab *s = cust->includes[i];
5221
5222 s = recursively_find_pc_sect_compunit_symtab (s, pc);
5223 if (s != NULL)
5224 return s;
5225 }
5226
5227 return NULL;
5228}
5229
5230static struct compunit_symtab *
5231dw2_find_pc_sect_compunit_symtab (struct objfile *objfile,
5232 struct bound_minimal_symbol msymbol,
5233 CORE_ADDR pc,
5234 struct obj_section *section,
5235 int warn_if_readin)
5236{
5237 struct dwarf2_per_cu_data *data;
5238 struct compunit_symtab *result;
5239
927aa2e7
JK
5240 if (!objfile->psymtabs_addrmap)
5241 return NULL;
5242
5243 data = (struct dwarf2_per_cu_data *) addrmap_find (objfile->psymtabs_addrmap,
5244 pc);
5245 if (!data)
5246 return NULL;
5247
5248 if (warn_if_readin && data->v.quick->compunit_symtab)
5249 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
5250 paddress (get_objfile_arch (objfile), pc));
5251
5252 result
5253 = recursively_find_pc_sect_compunit_symtab (dw2_instantiate_symtab (data),
5254 pc);
5255 gdb_assert (result != NULL);
5256 return result;
5257}
5258
5259static void
5260dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
5261 void *data, int need_fullname)
5262{
ed2dc618
SM
5263 struct dwarf2_per_objfile *dwarf2_per_objfile
5264 = get_dwarf2_per_objfile (objfile);
927aa2e7
JK
5265
5266 if (!dwarf2_per_objfile->filenames_cache)
5267 {
5268 dwarf2_per_objfile->filenames_cache.emplace ();
5269
5270 htab_up visited (htab_create_alloc (10,
5271 htab_hash_pointer, htab_eq_pointer,
5272 NULL, xcalloc, xfree));
5273
5274 /* The rule is CUs specify all the files, including those used
5275 by any TU, so there's no need to scan TUs here. We can
5276 ignore file names coming from already-expanded CUs. */
5277
b76e467d 5278 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
927aa2e7 5279 {
927aa2e7
JK
5280 if (per_cu->v.quick->compunit_symtab)
5281 {
5282 void **slot = htab_find_slot (visited.get (),
5283 per_cu->v.quick->file_names,
5284 INSERT);
5285
5286 *slot = per_cu->v.quick->file_names;
5287 }
5288 }
5289
b76e467d 5290 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
927aa2e7 5291 {
927aa2e7
JK
5292 /* We only need to look at symtabs not already expanded. */
5293 if (per_cu->v.quick->compunit_symtab)
5294 continue;
5295
b76e467d 5296 quick_file_names *file_data = dw2_get_file_names (per_cu);
927aa2e7
JK
5297 if (file_data == NULL)
5298 continue;
5299
b76e467d 5300 void **slot = htab_find_slot (visited.get (), file_data, INSERT);
927aa2e7
JK
5301 if (*slot)
5302 {
5303 /* Already visited. */
5304 continue;
5305 }
5306 *slot = file_data;
5307
5308 for (int j = 0; j < file_data->num_file_names; ++j)
5309 {
5310 const char *filename = file_data->file_names[j];
5311 dwarf2_per_objfile->filenames_cache->seen (filename);
5312 }
5313 }
5314 }
5315
5316 dwarf2_per_objfile->filenames_cache->traverse ([&] (const char *filename)
5317 {
5318 gdb::unique_xmalloc_ptr<char> this_real_name;
5319
5320 if (need_fullname)
5321 this_real_name = gdb_realpath (filename);
5322 (*fun) (filename, this_real_name.get (), data);
5323 });
5324}
5325
5326static int
5327dw2_has_symbols (struct objfile *objfile)
5328{
5329 return 1;
5330}
5331
5332const struct quick_symbol_functions dwarf2_gdb_index_functions =
5333{
5334 dw2_has_symbols,
5335 dw2_find_last_source_symtab,
5336 dw2_forget_cached_source_info,
5337 dw2_map_symtabs_matching_filename,
5338 dw2_lookup_symbol,
5339 dw2_print_stats,
5340 dw2_dump,
5341 dw2_relocate,
5342 dw2_expand_symtabs_for_function,
5343 dw2_expand_all_symtabs,
5344 dw2_expand_symtabs_with_fullname,
5345 dw2_map_matching_symbols,
5346 dw2_expand_symtabs_matching,
5347 dw2_find_pc_sect_compunit_symtab,
5348 NULL,
5349 dw2_map_symbol_filenames
5350};
5351
5352/* DWARF-5 debug_names reader. */
5353
5354/* DWARF-5 augmentation string for GDB's DW_IDX_GNU_* extension. */
5355static const gdb_byte dwarf5_augmentation[] = { 'G', 'D', 'B', 0 };
5356
5357/* A helper function that reads the .debug_names section in SECTION
5358 and fills in MAP. FILENAME is the name of the file containing the
5359 section; it is used for error reporting.
5360
5361 Returns true if all went well, false otherwise. */
5362
5363static bool
5364read_debug_names_from_section (struct objfile *objfile,
5365 const char *filename,
5366 struct dwarf2_section_info *section,
5367 mapped_debug_names &map)
5368{
5369 if (dwarf2_section_empty_p (section))
5370 return false;
5371
5372 /* Older elfutils strip versions could keep the section in the main
5373 executable while splitting it for the separate debug info file. */
5374 if ((get_section_flags (section) & SEC_HAS_CONTENTS) == 0)
5375 return false;
5376
5377 dwarf2_read_section (objfile, section);
5378
5379 map.dwarf5_byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
5380
5381 const gdb_byte *addr = section->buffer;
5382
5383 bfd *const abfd = get_section_bfd_owner (section);
5384
5385 unsigned int bytes_read;
5386 LONGEST length = read_initial_length (abfd, addr, &bytes_read);
5387 addr += bytes_read;
5388
5389 map.dwarf5_is_dwarf64 = bytes_read != 4;
5390 map.offset_size = map.dwarf5_is_dwarf64 ? 8 : 4;
5391 if (bytes_read + length != section->size)
5392 {
5393 /* There may be multiple per-CU indices. */
5394 warning (_("Section .debug_names in %s length %s does not match "
5395 "section length %s, ignoring .debug_names."),
5396 filename, plongest (bytes_read + length),
5397 pulongest (section->size));
5398 return false;
5399 }
5400
5401 /* The version number. */
5402 uint16_t version = read_2_bytes (abfd, addr);
5403 addr += 2;
5404 if (version != 5)
5405 {
5406 warning (_("Section .debug_names in %s has unsupported version %d, "
5407 "ignoring .debug_names."),
5408 filename, version);
5409 return false;
5410 }
5411
5412 /* Padding. */
5413 uint16_t padding = read_2_bytes (abfd, addr);
5414 addr += 2;
5415 if (padding != 0)
5416 {
5417 warning (_("Section .debug_names in %s has unsupported padding %d, "
5418 "ignoring .debug_names."),
5419 filename, padding);
5420 return false;
5421 }
5422
5423 /* comp_unit_count - The number of CUs in the CU list. */
5424 map.cu_count = read_4_bytes (abfd, addr);
5425 addr += 4;
5426
5427 /* local_type_unit_count - The number of TUs in the local TU
5428 list. */
5429 map.tu_count = read_4_bytes (abfd, addr);
5430 addr += 4;
5431
5432 /* foreign_type_unit_count - The number of TUs in the foreign TU
5433 list. */
5434 uint32_t foreign_tu_count = read_4_bytes (abfd, addr);
5435 addr += 4;
5436 if (foreign_tu_count != 0)
5437 {
5438 warning (_("Section .debug_names in %s has unsupported %lu foreign TUs, "
5439 "ignoring .debug_names."),
5440 filename, static_cast<unsigned long> (foreign_tu_count));
5441 return false;
5442 }
5443
5444 /* bucket_count - The number of hash buckets in the hash lookup
5445 table. */
5446 map.bucket_count = read_4_bytes (abfd, addr);
5447 addr += 4;
5448
5449 /* name_count - The number of unique names in the index. */
5450 map.name_count = read_4_bytes (abfd, addr);
5451 addr += 4;
5452
5453 /* abbrev_table_size - The size in bytes of the abbreviations
5454 table. */
5455 uint32_t abbrev_table_size = read_4_bytes (abfd, addr);
5456 addr += 4;
5457
5458 /* augmentation_string_size - The size in bytes of the augmentation
5459 string. This value is rounded up to a multiple of 4. */
5460 uint32_t augmentation_string_size = read_4_bytes (abfd, addr);
5461 addr += 4;
5462 map.augmentation_is_gdb = ((augmentation_string_size
5463 == sizeof (dwarf5_augmentation))
5464 && memcmp (addr, dwarf5_augmentation,
5465 sizeof (dwarf5_augmentation)) == 0);
5466 augmentation_string_size += (-augmentation_string_size) & 3;
5467 addr += augmentation_string_size;
5468
5469 /* List of CUs */
5470 map.cu_table_reordered = addr;
5471 addr += map.cu_count * map.offset_size;
5472
5473 /* List of Local TUs */
5474 map.tu_table_reordered = addr;
5475 addr += map.tu_count * map.offset_size;
5476
5477 /* Hash Lookup Table */
5478 map.bucket_table_reordered = reinterpret_cast<const uint32_t *> (addr);
5479 addr += map.bucket_count * 4;
5480 map.hash_table_reordered = reinterpret_cast<const uint32_t *> (addr);
5481 addr += map.name_count * 4;
5482
5483 /* Name Table */
5484 map.name_table_string_offs_reordered = addr;
5485 addr += map.name_count * map.offset_size;
5486 map.name_table_entry_offs_reordered = addr;
5487 addr += map.name_count * map.offset_size;
5488
5489 const gdb_byte *abbrev_table_start = addr;
5490 for (;;)
5491 {
5492 unsigned int bytes_read;
5493 const ULONGEST index_num = read_unsigned_leb128 (abfd, addr, &bytes_read);
5494 addr += bytes_read;
5495 if (index_num == 0)
5496 break;
5497
5498 const auto insertpair
5499 = map.abbrev_map.emplace (index_num, mapped_debug_names::index_val ());
5500 if (!insertpair.second)
5501 {
5502 warning (_("Section .debug_names in %s has duplicate index %s, "
5503 "ignoring .debug_names."),
5504 filename, pulongest (index_num));
5505 return false;
5506 }
5507 mapped_debug_names::index_val &indexval = insertpair.first->second;
5508 indexval.dwarf_tag = read_unsigned_leb128 (abfd, addr, &bytes_read);
5509 addr += bytes_read;
5510
5511 for (;;)
5512 {
5513 mapped_debug_names::index_val::attr attr;
5514 attr.dw_idx = read_unsigned_leb128 (abfd, addr, &bytes_read);
5515 addr += bytes_read;
5516 attr.form = read_unsigned_leb128 (abfd, addr, &bytes_read);
5517 addr += bytes_read;
5518 if (attr.form == DW_FORM_implicit_const)
5519 {
5520 attr.implicit_const = read_signed_leb128 (abfd, addr,
5521 &bytes_read);
5522 addr += bytes_read;
5523 }
5524 if (attr.dw_idx == 0 && attr.form == 0)
5525 break;
5526 indexval.attr_vec.push_back (std::move (attr));
5527 }
5528 }
5529 if (addr != abbrev_table_start + abbrev_table_size)
5530 {
5531 warning (_("Section .debug_names in %s has abbreviation_table "
5532 "of size %zu vs. written as %u, ignoring .debug_names."),
5533 filename, addr - abbrev_table_start, abbrev_table_size);
5534 return false;
5535 }
5536 map.entry_pool = addr;
5537
5538 return true;
5539}
5540
5541/* A helper for create_cus_from_debug_names that handles the MAP's CU
5542 list. */
5543
5544static void
ed2dc618 5545create_cus_from_debug_names_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
927aa2e7
JK
5546 const mapped_debug_names &map,
5547 dwarf2_section_info &section,
b76e467d 5548 bool is_dwz)
927aa2e7
JK
5549{
5550 sect_offset sect_off_prev;
5551 for (uint32_t i = 0; i <= map.cu_count; ++i)
5552 {
5553 sect_offset sect_off_next;
5554 if (i < map.cu_count)
5555 {
5556 sect_off_next
5557 = (sect_offset) (extract_unsigned_integer
5558 (map.cu_table_reordered + i * map.offset_size,
5559 map.offset_size,
5560 map.dwarf5_byte_order));
5561 }
5562 else
5563 sect_off_next = (sect_offset) section.size;
5564 if (i >= 1)
5565 {
5566 const ULONGEST length = sect_off_next - sect_off_prev;
b76e467d 5567 dwarf2_per_cu_data *per_cu
ed2dc618 5568 = create_cu_from_index_list (dwarf2_per_objfile, &section, is_dwz,
927aa2e7 5569 sect_off_prev, length);
b76e467d 5570 dwarf2_per_objfile->all_comp_units.push_back (per_cu);
927aa2e7
JK
5571 }
5572 sect_off_prev = sect_off_next;
5573 }
5574}
5575
5576/* Read the CU list from the mapped index, and use it to create all
ed2dc618 5577 the CU objects for this dwarf2_per_objfile. */
927aa2e7
JK
5578
5579static void
ed2dc618 5580create_cus_from_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile,
927aa2e7
JK
5581 const mapped_debug_names &map,
5582 const mapped_debug_names &dwz_map)
5583{
b76e467d
SM
5584 gdb_assert (dwarf2_per_objfile->all_comp_units.empty ());
5585 dwarf2_per_objfile->all_comp_units.reserve (map.cu_count + dwz_map.cu_count);
927aa2e7 5586
ed2dc618
SM
5587 create_cus_from_debug_names_list (dwarf2_per_objfile, map,
5588 dwarf2_per_objfile->info,
b76e467d 5589 false /* is_dwz */);
927aa2e7
JK
5590
5591 if (dwz_map.cu_count == 0)
5592 return;
5593
ed2dc618
SM
5594 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
5595 create_cus_from_debug_names_list (dwarf2_per_objfile, dwz_map, dwz->info,
b76e467d 5596 true /* is_dwz */);
927aa2e7
JK
5597}
5598
5599/* Read .debug_names. If everything went ok, initialize the "quick"
5600 elements of all the CUs and return true. Otherwise, return false. */
5601
5602static bool
ed2dc618 5603dwarf2_read_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile)
927aa2e7 5604{
ed2dc618
SM
5605 mapped_debug_names local_map (dwarf2_per_objfile);
5606 mapped_debug_names dwz_map (dwarf2_per_objfile);
5607 struct objfile *objfile = dwarf2_per_objfile->objfile;
927aa2e7
JK
5608
5609 if (!read_debug_names_from_section (objfile, objfile_name (objfile),
5610 &dwarf2_per_objfile->debug_names,
5611 local_map))
5612 return false;
5613
5614 /* Don't use the index if it's empty. */
5615 if (local_map.name_count == 0)
5616 return false;
5617
5618 /* If there is a .dwz file, read it so we can get its CU list as
5619 well. */
ed2dc618 5620 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
927aa2e7
JK
5621 if (dwz != NULL)
5622 {
5623 if (!read_debug_names_from_section (objfile,
5624 bfd_get_filename (dwz->dwz_bfd),
5625 &dwz->debug_names, dwz_map))
5626 {
5627 warning (_("could not read '.debug_names' section from %s; skipping"),
5628 bfd_get_filename (dwz->dwz_bfd));
5629 return false;
5630 }
5631 }
5632
ed2dc618 5633 create_cus_from_debug_names (dwarf2_per_objfile, local_map, dwz_map);
927aa2e7
JK
5634
5635 if (local_map.tu_count != 0)
5636 {
5637 /* We can only handle a single .debug_types when we have an
5638 index. */
5639 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) != 1)
5640 return false;
5641
5642 dwarf2_section_info *section = VEC_index (dwarf2_section_info_def,
5643 dwarf2_per_objfile->types, 0);
5644
5645 create_signatured_type_table_from_debug_names
ed2dc618 5646 (dwarf2_per_objfile, local_map, section, &dwarf2_per_objfile->abbrev);
927aa2e7
JK
5647 }
5648
ed2dc618
SM
5649 create_addrmap_from_aranges (dwarf2_per_objfile,
5650 &dwarf2_per_objfile->debug_aranges);
927aa2e7 5651
ed2dc618
SM
5652 dwarf2_per_objfile->debug_names_table.reset
5653 (new mapped_debug_names (dwarf2_per_objfile));
927aa2e7
JK
5654 *dwarf2_per_objfile->debug_names_table = std::move (local_map);
5655 dwarf2_per_objfile->using_index = 1;
5656 dwarf2_per_objfile->quick_file_names_table =
b76e467d 5657 create_quick_file_names_table (dwarf2_per_objfile->all_comp_units.size ());
927aa2e7
JK
5658
5659 return true;
5660}
5661
927aa2e7
JK
5662/* Type used to manage iterating over all CUs looking for a symbol for
5663 .debug_names. */
5664
5665class dw2_debug_names_iterator
5666{
5667public:
5668 /* If WANT_SPECIFIC_BLOCK is true, only look for symbols in block
5669 BLOCK_INDEX. Otherwise BLOCK_INDEX is ignored. */
5670 dw2_debug_names_iterator (const mapped_debug_names &map,
5671 bool want_specific_block,
5672 block_enum block_index, domain_enum domain,
5673 const char *name)
5674 : m_map (map), m_want_specific_block (want_specific_block),
5675 m_block_index (block_index), m_domain (domain),
5676 m_addr (find_vec_in_debug_names (map, name))
5677 {}
5678
5679 dw2_debug_names_iterator (const mapped_debug_names &map,
5680 search_domain search, uint32_t namei)
5681 : m_map (map),
5682 m_search (search),
5683 m_addr (find_vec_in_debug_names (map, namei))
5684 {}
5685
5686 /* Return the next matching CU or NULL if there are no more. */
5687 dwarf2_per_cu_data *next ();
5688
5689private:
5690 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5691 const char *name);
5692 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5693 uint32_t namei);
5694
5695 /* The internalized form of .debug_names. */
5696 const mapped_debug_names &m_map;
5697
5698 /* If true, only look for symbols that match BLOCK_INDEX. */
5699 const bool m_want_specific_block = false;
5700
5701 /* One of GLOBAL_BLOCK or STATIC_BLOCK.
5702 Unused if !WANT_SPECIFIC_BLOCK - FIRST_LOCAL_BLOCK is an invalid
5703 value. */
5704 const block_enum m_block_index = FIRST_LOCAL_BLOCK;
5705
5706 /* The kind of symbol we're looking for. */
5707 const domain_enum m_domain = UNDEF_DOMAIN;
5708 const search_domain m_search = ALL_DOMAIN;
5709
5710 /* The list of CUs from the index entry of the symbol, or NULL if
5711 not found. */
5712 const gdb_byte *m_addr;
5713};
5714
5715const char *
5716mapped_debug_names::namei_to_name (uint32_t namei) const
5717{
5718 const ULONGEST namei_string_offs
5719 = extract_unsigned_integer ((name_table_string_offs_reordered
5720 + namei * offset_size),
5721 offset_size,
5722 dwarf5_byte_order);
5723 return read_indirect_string_at_offset
ed2dc618 5724 (dwarf2_per_objfile, dwarf2_per_objfile->objfile->obfd, namei_string_offs);
927aa2e7
JK
5725}
5726
5727/* Find a slot in .debug_names for the object named NAME. If NAME is
5728 found, return pointer to its pool data. If NAME cannot be found,
5729 return NULL. */
5730
5731const gdb_byte *
5732dw2_debug_names_iterator::find_vec_in_debug_names
5733 (const mapped_debug_names &map, const char *name)
5734{
5735 int (*cmp) (const char *, const char *);
5736
5737 if (current_language->la_language == language_cplus
5738 || current_language->la_language == language_fortran
5739 || current_language->la_language == language_d)
5740 {
5741 /* NAME is already canonical. Drop any qualifiers as
5742 .debug_names does not contain any. */
5743
5744 if (strchr (name, '(') != NULL)
5745 {
5746 gdb::unique_xmalloc_ptr<char> without_params
5747 = cp_remove_params (name);
5748
5749 if (without_params != NULL)
5750 {
5751 name = without_params.get();
5752 }
5753 }
5754 }
5755
5756 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
5757
5758 const uint32_t full_hash = dwarf5_djb_hash (name);
5759 uint32_t namei
5760 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5761 (map.bucket_table_reordered
5762 + (full_hash % map.bucket_count)), 4,
5763 map.dwarf5_byte_order);
5764 if (namei == 0)
5765 return NULL;
5766 --namei;
5767 if (namei >= map.name_count)
5768 {
5769 complaint (&symfile_complaints,
5770 _("Wrong .debug_names with name index %u but name_count=%u "
5771 "[in module %s]"),
5772 namei, map.name_count,
ed2dc618 5773 objfile_name (map.dwarf2_per_objfile->objfile));
927aa2e7
JK
5774 return NULL;
5775 }
5776
5777 for (;;)
5778 {
5779 const uint32_t namei_full_hash
5780 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5781 (map.hash_table_reordered + namei), 4,
5782 map.dwarf5_byte_order);
5783 if (full_hash % map.bucket_count != namei_full_hash % map.bucket_count)
5784 return NULL;
5785
5786 if (full_hash == namei_full_hash)
5787 {
5788 const char *const namei_string = map.namei_to_name (namei);
5789
5790#if 0 /* An expensive sanity check. */
5791 if (namei_full_hash != dwarf5_djb_hash (namei_string))
5792 {
5793 complaint (&symfile_complaints,
5794 _("Wrong .debug_names hash for string at index %u "
5795 "[in module %s]"),
5796 namei, objfile_name (dwarf2_per_objfile->objfile));
5797 return NULL;
5798 }
5799#endif
5800
5801 if (cmp (namei_string, name) == 0)
5802 {
5803 const ULONGEST namei_entry_offs
5804 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5805 + namei * map.offset_size),
5806 map.offset_size, map.dwarf5_byte_order);
5807 return map.entry_pool + namei_entry_offs;
5808 }
5809 }
5810
5811 ++namei;
5812 if (namei >= map.name_count)
5813 return NULL;
5814 }
5815}
5816
5817const gdb_byte *
5818dw2_debug_names_iterator::find_vec_in_debug_names
5819 (const mapped_debug_names &map, uint32_t namei)
5820{
5821 if (namei >= map.name_count)
5822 {
5823 complaint (&symfile_complaints,
5824 _("Wrong .debug_names with name index %u but name_count=%u "
5825 "[in module %s]"),
5826 namei, map.name_count,
ed2dc618 5827 objfile_name (map.dwarf2_per_objfile->objfile));
927aa2e7
JK
5828 return NULL;
5829 }
5830
5831 const ULONGEST namei_entry_offs
5832 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5833 + namei * map.offset_size),
5834 map.offset_size, map.dwarf5_byte_order);
5835 return map.entry_pool + namei_entry_offs;
5836}
5837
5838/* See dw2_debug_names_iterator. */
5839
5840dwarf2_per_cu_data *
5841dw2_debug_names_iterator::next ()
5842{
5843 if (m_addr == NULL)
5844 return NULL;
5845
ed2dc618
SM
5846 struct dwarf2_per_objfile *dwarf2_per_objfile = m_map.dwarf2_per_objfile;
5847 struct objfile *objfile = dwarf2_per_objfile->objfile;
5848 bfd *const abfd = objfile->obfd;
927aa2e7
JK
5849
5850 again:
5851
5852 unsigned int bytes_read;
5853 const ULONGEST abbrev = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5854 m_addr += bytes_read;
5855 if (abbrev == 0)
5856 return NULL;
5857
5858 const auto indexval_it = m_map.abbrev_map.find (abbrev);
5859 if (indexval_it == m_map.abbrev_map.cend ())
5860 {
5861 complaint (&symfile_complaints,
5862 _("Wrong .debug_names undefined abbrev code %s "
5863 "[in module %s]"),
ed2dc618 5864 pulongest (abbrev), objfile_name (objfile));
927aa2e7
JK
5865 return NULL;
5866 }
5867 const mapped_debug_names::index_val &indexval = indexval_it->second;
5868 bool have_is_static = false;
5869 bool is_static;
5870 dwarf2_per_cu_data *per_cu = NULL;
5871 for (const mapped_debug_names::index_val::attr &attr : indexval.attr_vec)
5872 {
5873 ULONGEST ull;
5874 switch (attr.form)
5875 {
5876 case DW_FORM_implicit_const:
5877 ull = attr.implicit_const;
5878 break;
5879 case DW_FORM_flag_present:
5880 ull = 1;
5881 break;
5882 case DW_FORM_udata:
5883 ull = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5884 m_addr += bytes_read;
5885 break;
5886 default:
5887 complaint (&symfile_complaints,
5888 _("Unsupported .debug_names form %s [in module %s]"),
5889 dwarf_form_name (attr.form),
ed2dc618 5890 objfile_name (objfile));
927aa2e7
JK
5891 return NULL;
5892 }
5893 switch (attr.dw_idx)
5894 {
5895 case DW_IDX_compile_unit:
5896 /* Don't crash on bad data. */
b76e467d 5897 if (ull >= dwarf2_per_objfile->all_comp_units.size ())
927aa2e7
JK
5898 {
5899 complaint (&symfile_complaints,
5900 _(".debug_names entry has bad CU index %s"
5901 " [in module %s]"),
5902 pulongest (ull),
5903 objfile_name (dwarf2_per_objfile->objfile));
5904 continue;
5905 }
ff4c9fec 5906 per_cu = dwarf2_per_objfile->get_cutu (ull);
927aa2e7 5907 break;
8af5c486
JK
5908 case DW_IDX_type_unit:
5909 /* Don't crash on bad data. */
b2bdb8cf 5910 if (ull >= dwarf2_per_objfile->all_type_units.size ())
8af5c486
JK
5911 {
5912 complaint (&symfile_complaints,
5913 _(".debug_names entry has bad TU index %s"
5914 " [in module %s]"),
5915 pulongest (ull),
5916 objfile_name (dwarf2_per_objfile->objfile));
5917 continue;
5918 }
ff4c9fec 5919 per_cu = &dwarf2_per_objfile->get_tu (ull)->per_cu;
8af5c486 5920 break;
927aa2e7
JK
5921 case DW_IDX_GNU_internal:
5922 if (!m_map.augmentation_is_gdb)
5923 break;
5924 have_is_static = true;
5925 is_static = true;
5926 break;
5927 case DW_IDX_GNU_external:
5928 if (!m_map.augmentation_is_gdb)
5929 break;
5930 have_is_static = true;
5931 is_static = false;
5932 break;
5933 }
5934 }
5935
5936 /* Skip if already read in. */
5937 if (per_cu->v.quick->compunit_symtab)
5938 goto again;
5939
5940 /* Check static vs global. */
5941 if (have_is_static)
5942 {
5943 const bool want_static = m_block_index != GLOBAL_BLOCK;
5944 if (m_want_specific_block && want_static != is_static)
5945 goto again;
5946 }
5947
5948 /* Match dw2_symtab_iter_next, symbol_kind
5949 and debug_names::psymbol_tag. */
5950 switch (m_domain)
5951 {
5952 case VAR_DOMAIN:
5953 switch (indexval.dwarf_tag)
5954 {
5955 case DW_TAG_variable:
5956 case DW_TAG_subprogram:
5957 /* Some types are also in VAR_DOMAIN. */
5958 case DW_TAG_typedef:
5959 case DW_TAG_structure_type:
5960 break;
5961 default:
5962 goto again;
5963 }
5964 break;
5965 case STRUCT_DOMAIN:
5966 switch (indexval.dwarf_tag)
5967 {
5968 case DW_TAG_typedef:
5969 case DW_TAG_structure_type:
5970 break;
5971 default:
5972 goto again;
5973 }
5974 break;
5975 case LABEL_DOMAIN:
5976 switch (indexval.dwarf_tag)
5977 {
5978 case 0:
5979 case DW_TAG_variable:
5980 break;
5981 default:
5982 goto again;
5983 }
5984 break;
5985 default:
5986 break;
5987 }
5988
5989 /* Match dw2_expand_symtabs_matching, symbol_kind and
5990 debug_names::psymbol_tag. */
5991 switch (m_search)
4b514bc8 5992 {
927aa2e7
JK
5993 case VARIABLES_DOMAIN:
5994 switch (indexval.dwarf_tag)
4b514bc8 5995 {
927aa2e7
JK
5996 case DW_TAG_variable:
5997 break;
5998 default:
5999 goto again;
4b514bc8 6000 }
927aa2e7
JK
6001 break;
6002 case FUNCTIONS_DOMAIN:
6003 switch (indexval.dwarf_tag)
4b514bc8 6004 {
927aa2e7
JK
6005 case DW_TAG_subprogram:
6006 break;
6007 default:
6008 goto again;
4b514bc8 6009 }
927aa2e7
JK
6010 break;
6011 case TYPES_DOMAIN:
6012 switch (indexval.dwarf_tag)
6013 {
6014 case DW_TAG_typedef:
6015 case DW_TAG_structure_type:
6016 break;
6017 default:
6018 goto again;
6019 }
6020 break;
6021 default:
6022 break;
4b514bc8 6023 }
927aa2e7
JK
6024
6025 return per_cu;
4b514bc8 6026}
61920122 6027
927aa2e7
JK
6028static struct compunit_symtab *
6029dw2_debug_names_lookup_symbol (struct objfile *objfile, int block_index_int,
6030 const char *name, domain_enum domain)
4b514bc8 6031{
927aa2e7 6032 const block_enum block_index = static_cast<block_enum> (block_index_int);
ed2dc618
SM
6033 struct dwarf2_per_objfile *dwarf2_per_objfile
6034 = get_dwarf2_per_objfile (objfile);
61920122 6035
927aa2e7
JK
6036 const auto &mapp = dwarf2_per_objfile->debug_names_table;
6037 if (!mapp)
61920122 6038 {
927aa2e7
JK
6039 /* index is NULL if OBJF_READNOW. */
6040 return NULL;
6041 }
6042 const auto &map = *mapp;
9291a0cd 6043
927aa2e7
JK
6044 dw2_debug_names_iterator iter (map, true /* want_specific_block */,
6045 block_index, domain, name);
9703b513 6046
927aa2e7
JK
6047 struct compunit_symtab *stab_best = NULL;
6048 struct dwarf2_per_cu_data *per_cu;
6049 while ((per_cu = iter.next ()) != NULL)
6050 {
6051 struct symbol *sym, *with_opaque = NULL;
6052 struct compunit_symtab *stab = dw2_instantiate_symtab (per_cu);
6053 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
6054 struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
9703b513 6055
927aa2e7
JK
6056 sym = block_find_symbol (block, name, domain,
6057 block_find_non_opaque_type_preferred,
6058 &with_opaque);
9703b513 6059
927aa2e7
JK
6060 /* Some caution must be observed with overloaded functions and
6061 methods, since the index will not contain any overload
6062 information (but NAME might contain it). */
a3ec0bb1 6063
927aa2e7
JK
6064 if (sym != NULL
6065 && strcmp_iw (SYMBOL_SEARCH_NAME (sym), name) == 0)
6066 return stab;
6067 if (with_opaque != NULL
6068 && strcmp_iw (SYMBOL_SEARCH_NAME (with_opaque), name) == 0)
6069 stab_best = stab;
9703b513 6070
927aa2e7 6071 /* Keep looking through other CUs. */
9703b513
TT
6072 }
6073
927aa2e7 6074 return stab_best;
9703b513
TT
6075}
6076
927aa2e7
JK
6077/* This dumps minimal information about .debug_names. It is called
6078 via "mt print objfiles". The gdb.dwarf2/gdb-index.exp testcase
6079 uses this to verify that .debug_names has been loaded. */
9291a0cd 6080
927aa2e7
JK
6081static void
6082dw2_debug_names_dump (struct objfile *objfile)
6083{
ed2dc618
SM
6084 struct dwarf2_per_objfile *dwarf2_per_objfile
6085 = get_dwarf2_per_objfile (objfile);
6086
927aa2e7
JK
6087 gdb_assert (dwarf2_per_objfile->using_index);
6088 printf_filtered (".debug_names:");
6089 if (dwarf2_per_objfile->debug_names_table)
6090 printf_filtered (" exists\n");
6091 else
6092 printf_filtered (" faked for \"readnow\"\n");
6093 printf_filtered ("\n");
9291a0cd
TT
6094}
6095
9291a0cd 6096static void
927aa2e7
JK
6097dw2_debug_names_expand_symtabs_for_function (struct objfile *objfile,
6098 const char *func_name)
9291a0cd 6099{
ed2dc618
SM
6100 struct dwarf2_per_objfile *dwarf2_per_objfile
6101 = get_dwarf2_per_objfile (objfile);
ae2de4f8 6102
927aa2e7
JK
6103 /* dwarf2_per_objfile->debug_names_table is NULL if OBJF_READNOW. */
6104 if (dwarf2_per_objfile->debug_names_table)
24c79950 6105 {
927aa2e7 6106 const mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
24c79950 6107
927aa2e7
JK
6108 /* Note: It doesn't matter what we pass for block_index here. */
6109 dw2_debug_names_iterator iter (map, false /* want_specific_block */,
6110 GLOBAL_BLOCK, VAR_DOMAIN, func_name);
24c79950 6111
927aa2e7
JK
6112 struct dwarf2_per_cu_data *per_cu;
6113 while ((per_cu = iter.next ()) != NULL)
6114 dw2_instantiate_symtab (per_cu);
6115 }
6116}
24c79950 6117
927aa2e7
JK
6118static void
6119dw2_debug_names_expand_symtabs_matching
6120 (struct objfile *objfile,
6121 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
6122 const lookup_name_info &lookup_name,
6123 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
6124 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
6125 enum search_domain kind)
6126{
ed2dc618
SM
6127 struct dwarf2_per_objfile *dwarf2_per_objfile
6128 = get_dwarf2_per_objfile (objfile);
9291a0cd 6129
927aa2e7
JK
6130 /* debug_names_table is NULL if OBJF_READNOW. */
6131 if (!dwarf2_per_objfile->debug_names_table)
6132 return;
9291a0cd 6133
ed2dc618 6134 dw_expand_symtabs_matching_file_matcher (dwarf2_per_objfile, file_matcher);
24c79950 6135
44ed8f3e 6136 mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
bbf2f4df 6137
44ed8f3e
PA
6138 dw2_expand_symtabs_matching_symbol (map, lookup_name,
6139 symbol_matcher,
6140 kind, [&] (offset_type namei)
927aa2e7 6141 {
927aa2e7
JK
6142 /* The name was matched, now expand corresponding CUs that were
6143 marked. */
6144 dw2_debug_names_iterator iter (map, kind, namei);
bbf2f4df 6145
927aa2e7
JK
6146 struct dwarf2_per_cu_data *per_cu;
6147 while ((per_cu = iter.next ()) != NULL)
6148 dw2_expand_symtabs_matching_one (per_cu, file_matcher,
6149 expansion_notify);
44ed8f3e 6150 });
9291a0cd
TT
6151}
6152
927aa2e7 6153const struct quick_symbol_functions dwarf2_debug_names_functions =
9291a0cd
TT
6154{
6155 dw2_has_symbols,
6156 dw2_find_last_source_symtab,
6157 dw2_forget_cached_source_info,
f8eba3c6 6158 dw2_map_symtabs_matching_filename,
927aa2e7 6159 dw2_debug_names_lookup_symbol,
9291a0cd 6160 dw2_print_stats,
927aa2e7 6161 dw2_debug_names_dump,
9291a0cd 6162 dw2_relocate,
927aa2e7 6163 dw2_debug_names_expand_symtabs_for_function,
9291a0cd 6164 dw2_expand_all_symtabs,
652a8996 6165 dw2_expand_symtabs_with_fullname,
40658b94 6166 dw2_map_matching_symbols,
927aa2e7 6167 dw2_debug_names_expand_symtabs_matching,
43f3e411 6168 dw2_find_pc_sect_compunit_symtab,
71a3c369 6169 NULL,
9291a0cd
TT
6170 dw2_map_symbol_filenames
6171};
6172
3c0aa29a 6173/* See symfile.h. */
9291a0cd 6174
3c0aa29a
PA
6175bool
6176dwarf2_initialize_objfile (struct objfile *objfile, dw_index_kind *index_kind)
9291a0cd 6177{
ed2dc618
SM
6178 struct dwarf2_per_objfile *dwarf2_per_objfile
6179 = get_dwarf2_per_objfile (objfile);
6180
9291a0cd
TT
6181 /* If we're about to read full symbols, don't bother with the
6182 indices. In this case we also don't care if some other debug
6183 format is making psymtabs, because they are all about to be
6184 expanded anyway. */
6185 if ((objfile->flags & OBJF_READNOW))
6186 {
9291a0cd 6187 dwarf2_per_objfile->using_index = 1;
ed2dc618
SM
6188 create_all_comp_units (dwarf2_per_objfile);
6189 create_all_type_units (dwarf2_per_objfile);
b76e467d
SM
6190 dwarf2_per_objfile->quick_file_names_table
6191 = create_quick_file_names_table
6192 (dwarf2_per_objfile->all_comp_units.size ());
9291a0cd 6193
b76e467d 6194 for (int i = 0; i < (dwarf2_per_objfile->all_comp_units.size ()
b2bdb8cf 6195 + dwarf2_per_objfile->all_type_units.size ()); ++i)
9291a0cd 6196 {
ff4c9fec 6197 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (i);
9291a0cd 6198
e254ef6a
DE
6199 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6200 struct dwarf2_per_cu_quick_data);
9291a0cd
TT
6201 }
6202
6203 /* Return 1 so that gdb sees the "quick" functions. However,
6204 these functions will be no-ops because we will have expanded
6205 all symtabs. */
3c0aa29a
PA
6206 *index_kind = dw_index_kind::GDB_INDEX;
6207 return true;
9291a0cd
TT
6208 }
6209
ed2dc618 6210 if (dwarf2_read_debug_names (dwarf2_per_objfile))
3c0aa29a
PA
6211 {
6212 *index_kind = dw_index_kind::DEBUG_NAMES;
6213 return true;
6214 }
927aa2e7 6215
12359b5e 6216 if (dwarf2_read_index (dwarf2_per_objfile))
3c0aa29a
PA
6217 {
6218 *index_kind = dw_index_kind::GDB_INDEX;
6219 return true;
6220 }
9291a0cd 6221
3c0aa29a 6222 return false;
9291a0cd
TT
6223}
6224
6225\f
6226
dce234bc
PP
6227/* Build a partial symbol table. */
6228
6229void
f29dff0a 6230dwarf2_build_psymtabs (struct objfile *objfile)
dce234bc 6231{
ed2dc618
SM
6232 struct dwarf2_per_objfile *dwarf2_per_objfile
6233 = get_dwarf2_per_objfile (objfile);
c9bf0622 6234
af5bf4ad
SM
6235 if (objfile->global_psymbols.capacity () == 0
6236 && objfile->static_psymbols.capacity () == 0)
6237 init_psymbol_list (objfile, 1024);
c906108c 6238
492d29ea 6239 TRY
c9bf0622
TT
6240 {
6241 /* This isn't really ideal: all the data we allocate on the
6242 objfile's obstack is still uselessly kept around. However,
6243 freeing it seems unsafe. */
906768f9 6244 psymtab_discarder psymtabs (objfile);
ed2dc618 6245 dwarf2_build_psymtabs_hard (dwarf2_per_objfile);
906768f9 6246 psymtabs.keep ();
c9bf0622 6247 }
492d29ea
PA
6248 CATCH (except, RETURN_MASK_ERROR)
6249 {
6250 exception_print (gdb_stderr, except);
6251 }
6252 END_CATCH
c906108c 6253}
c906108c 6254
1ce1cefd
DE
6255/* Return the total length of the CU described by HEADER. */
6256
6257static unsigned int
6258get_cu_length (const struct comp_unit_head *header)
6259{
6260 return header->initial_length_size + header->length;
6261}
6262
9c541725 6263/* Return TRUE if SECT_OFF is within CU_HEADER. */
45452591 6264
9c541725
PA
6265static inline bool
6266offset_in_cu_p (const comp_unit_head *cu_header, sect_offset sect_off)
45452591 6267{
9c541725
PA
6268 sect_offset bottom = cu_header->sect_off;
6269 sect_offset top = cu_header->sect_off + get_cu_length (cu_header);
9a619af0 6270
9c541725 6271 return sect_off >= bottom && sect_off < top;
45452591
DE
6272}
6273
3b80fe9b
DE
6274/* Find the base address of the compilation unit for range lists and
6275 location lists. It will normally be specified by DW_AT_low_pc.
6276 In DWARF-3 draft 4, the base address could be overridden by
6277 DW_AT_entry_pc. It's been removed, but GCC still uses this for
6278 compilation units with discontinuous ranges. */
6279
6280static void
6281dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
6282{
6283 struct attribute *attr;
6284
6285 cu->base_known = 0;
6286 cu->base_address = 0;
6287
6288 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
6289 if (attr)
6290 {
31aa7e4e 6291 cu->base_address = attr_value_as_address (attr);
3b80fe9b
DE
6292 cu->base_known = 1;
6293 }
6294 else
6295 {
6296 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
6297 if (attr)
6298 {
31aa7e4e 6299 cu->base_address = attr_value_as_address (attr);
3b80fe9b
DE
6300 cu->base_known = 1;
6301 }
6302 }
6303}
6304
93311388 6305/* Read in the comp unit header information from the debug_info at info_ptr.
43988095 6306 Use rcuh_kind::COMPILE as the default type if not known by the caller.
93311388
DE
6307 NOTE: This leaves members offset, first_die_offset to be filled in
6308 by the caller. */
107d2387 6309
d521ce57 6310static const gdb_byte *
107d2387 6311read_comp_unit_head (struct comp_unit_head *cu_header,
43988095
JK
6312 const gdb_byte *info_ptr,
6313 struct dwarf2_section_info *section,
6314 rcuh_kind section_kind)
107d2387
AC
6315{
6316 int signed_addr;
891d2f0b 6317 unsigned int bytes_read;
43988095
JK
6318 const char *filename = get_section_file_name (section);
6319 bfd *abfd = get_section_bfd_owner (section);
c764a876
DE
6320
6321 cu_header->length = read_initial_length (abfd, info_ptr, &bytes_read);
6322 cu_header->initial_length_size = bytes_read;
6323 cu_header->offset_size = (bytes_read == 4) ? 4 : 8;
613e1657 6324 info_ptr += bytes_read;
107d2387
AC
6325 cu_header->version = read_2_bytes (abfd, info_ptr);
6326 info_ptr += 2;
43988095
JK
6327 if (cu_header->version < 5)
6328 switch (section_kind)
6329 {
6330 case rcuh_kind::COMPILE:
6331 cu_header->unit_type = DW_UT_compile;
6332 break;
6333 case rcuh_kind::TYPE:
6334 cu_header->unit_type = DW_UT_type;
6335 break;
6336 default:
6337 internal_error (__FILE__, __LINE__,
6338 _("read_comp_unit_head: invalid section_kind"));
6339 }
6340 else
6341 {
6342 cu_header->unit_type = static_cast<enum dwarf_unit_type>
6343 (read_1_byte (abfd, info_ptr));
6344 info_ptr += 1;
6345 switch (cu_header->unit_type)
6346 {
6347 case DW_UT_compile:
6348 if (section_kind != rcuh_kind::COMPILE)
6349 error (_("Dwarf Error: wrong unit_type in compilation unit header "
6350 "(is DW_UT_compile, should be DW_UT_type) [in module %s]"),
6351 filename);
6352 break;
6353 case DW_UT_type:
6354 section_kind = rcuh_kind::TYPE;
6355 break;
6356 default:
6357 error (_("Dwarf Error: wrong unit_type in compilation unit header "
6358 "(is %d, should be %d or %d) [in module %s]"),
6359 cu_header->unit_type, DW_UT_compile, DW_UT_type, filename);
6360 }
6361
6362 cu_header->addr_size = read_1_byte (abfd, info_ptr);
6363 info_ptr += 1;
6364 }
9c541725
PA
6365 cu_header->abbrev_sect_off = (sect_offset) read_offset (abfd, info_ptr,
6366 cu_header,
6367 &bytes_read);
613e1657 6368 info_ptr += bytes_read;
43988095
JK
6369 if (cu_header->version < 5)
6370 {
6371 cu_header->addr_size = read_1_byte (abfd, info_ptr);
6372 info_ptr += 1;
6373 }
107d2387
AC
6374 signed_addr = bfd_get_sign_extend_vma (abfd);
6375 if (signed_addr < 0)
8e65ff28 6376 internal_error (__FILE__, __LINE__,
e2e0b3e5 6377 _("read_comp_unit_head: dwarf from non elf file"));
107d2387 6378 cu_header->signed_addr_p = signed_addr;
c764a876 6379
43988095
JK
6380 if (section_kind == rcuh_kind::TYPE)
6381 {
6382 LONGEST type_offset;
6383
6384 cu_header->signature = read_8_bytes (abfd, info_ptr);
6385 info_ptr += 8;
6386
6387 type_offset = read_offset (abfd, info_ptr, cu_header, &bytes_read);
6388 info_ptr += bytes_read;
9c541725
PA
6389 cu_header->type_cu_offset_in_tu = (cu_offset) type_offset;
6390 if (to_underlying (cu_header->type_cu_offset_in_tu) != type_offset)
43988095
JK
6391 error (_("Dwarf Error: Too big type_offset in compilation unit "
6392 "header (is %s) [in module %s]"), plongest (type_offset),
6393 filename);
6394 }
6395
107d2387
AC
6396 return info_ptr;
6397}
6398
36586728
TT
6399/* Helper function that returns the proper abbrev section for
6400 THIS_CU. */
6401
6402static struct dwarf2_section_info *
6403get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
6404{
6405 struct dwarf2_section_info *abbrev;
ed2dc618 6406 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
36586728
TT
6407
6408 if (this_cu->is_dwz)
ed2dc618 6409 abbrev = &dwarf2_get_dwz_file (dwarf2_per_objfile)->abbrev;
36586728
TT
6410 else
6411 abbrev = &dwarf2_per_objfile->abbrev;
6412
6413 return abbrev;
6414}
6415
9ff913ba
DE
6416/* Subroutine of read_and_check_comp_unit_head and
6417 read_and_check_type_unit_head to simplify them.
6418 Perform various error checking on the header. */
6419
6420static void
ed2dc618
SM
6421error_check_comp_unit_head (struct dwarf2_per_objfile *dwarf2_per_objfile,
6422 struct comp_unit_head *header,
4bdcc0c1
DE
6423 struct dwarf2_section_info *section,
6424 struct dwarf2_section_info *abbrev_section)
9ff913ba 6425{
a32a8923 6426 const char *filename = get_section_file_name (section);
9ff913ba 6427
43988095 6428 if (header->version < 2 || header->version > 5)
9ff913ba 6429 error (_("Dwarf Error: wrong version in compilation unit header "
43988095 6430 "(is %d, should be 2, 3, 4 or 5) [in module %s]"), header->version,
9ff913ba
DE
6431 filename);
6432
9c541725 6433 if (to_underlying (header->abbrev_sect_off)
36586728 6434 >= dwarf2_section_size (dwarf2_per_objfile->objfile, abbrev_section))
9d8780f0
SM
6435 error (_("Dwarf Error: bad offset (%s) in compilation unit header "
6436 "(offset %s + 6) [in module %s]"),
6437 sect_offset_str (header->abbrev_sect_off),
6438 sect_offset_str (header->sect_off),
9ff913ba
DE
6439 filename);
6440
9c541725 6441 /* Cast to ULONGEST to use 64-bit arithmetic when possible to
9ff913ba 6442 avoid potential 32-bit overflow. */
9c541725 6443 if (((ULONGEST) header->sect_off + get_cu_length (header))
9ff913ba 6444 > section->size)
9c541725 6445 error (_("Dwarf Error: bad length (0x%x) in compilation unit header "
9d8780f0
SM
6446 "(offset %s + 0) [in module %s]"),
6447 header->length, sect_offset_str (header->sect_off),
9ff913ba
DE
6448 filename);
6449}
6450
6451/* Read in a CU/TU header and perform some basic error checking.
6452 The contents of the header are stored in HEADER.
6453 The result is a pointer to the start of the first DIE. */
adabb602 6454
d521ce57 6455static const gdb_byte *
ed2dc618
SM
6456read_and_check_comp_unit_head (struct dwarf2_per_objfile *dwarf2_per_objfile,
6457 struct comp_unit_head *header,
9ff913ba 6458 struct dwarf2_section_info *section,
4bdcc0c1 6459 struct dwarf2_section_info *abbrev_section,
d521ce57 6460 const gdb_byte *info_ptr,
43988095 6461 rcuh_kind section_kind)
72bf9492 6462{
d521ce57 6463 const gdb_byte *beg_of_comp_unit = info_ptr;
72bf9492 6464
9c541725 6465 header->sect_off = (sect_offset) (beg_of_comp_unit - section->buffer);
adabb602 6466
43988095 6467 info_ptr = read_comp_unit_head (header, info_ptr, section, section_kind);
9ff913ba 6468
9c541725 6469 header->first_die_cu_offset = (cu_offset) (info_ptr - beg_of_comp_unit);
348e048f 6470
ed2dc618
SM
6471 error_check_comp_unit_head (dwarf2_per_objfile, header, section,
6472 abbrev_section);
9ff913ba
DE
6473
6474 return info_ptr;
348e048f
DE
6475}
6476
f4dc4d17
DE
6477/* Fetch the abbreviation table offset from a comp or type unit header. */
6478
6479static sect_offset
ed2dc618
SM
6480read_abbrev_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
6481 struct dwarf2_section_info *section,
9c541725 6482 sect_offset sect_off)
f4dc4d17 6483{
a32a8923 6484 bfd *abfd = get_section_bfd_owner (section);
d521ce57 6485 const gdb_byte *info_ptr;
ac298888 6486 unsigned int initial_length_size, offset_size;
43988095 6487 uint16_t version;
f4dc4d17
DE
6488
6489 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
9c541725 6490 info_ptr = section->buffer + to_underlying (sect_off);
ac298888 6491 read_initial_length (abfd, info_ptr, &initial_length_size);
f4dc4d17 6492 offset_size = initial_length_size == 4 ? 4 : 8;
43988095
JK
6493 info_ptr += initial_length_size;
6494
6495 version = read_2_bytes (abfd, info_ptr);
6496 info_ptr += 2;
6497 if (version >= 5)
6498 {
6499 /* Skip unit type and address size. */
6500 info_ptr += 2;
6501 }
6502
9c541725 6503 return (sect_offset) read_offset_1 (abfd, info_ptr, offset_size);
f4dc4d17
DE
6504}
6505
aaa75496
JB
6506/* Allocate a new partial symtab for file named NAME and mark this new
6507 partial symtab as being an include of PST. */
6508
6509static void
d521ce57 6510dwarf2_create_include_psymtab (const char *name, struct partial_symtab *pst,
aaa75496
JB
6511 struct objfile *objfile)
6512{
6513 struct partial_symtab *subpst = allocate_psymtab (name, objfile);
6514
fbd9ab74
JK
6515 if (!IS_ABSOLUTE_PATH (subpst->filename))
6516 {
6517 /* It shares objfile->objfile_obstack. */
6518 subpst->dirname = pst->dirname;
6519 }
6520
aaa75496
JB
6521 subpst->textlow = 0;
6522 subpst->texthigh = 0;
6523
8d749320
SM
6524 subpst->dependencies
6525 = XOBNEW (&objfile->objfile_obstack, struct partial_symtab *);
aaa75496
JB
6526 subpst->dependencies[0] = pst;
6527 subpst->number_of_dependencies = 1;
6528
6529 subpst->globals_offset = 0;
6530 subpst->n_global_syms = 0;
6531 subpst->statics_offset = 0;
6532 subpst->n_static_syms = 0;
43f3e411 6533 subpst->compunit_symtab = NULL;
aaa75496
JB
6534 subpst->read_symtab = pst->read_symtab;
6535 subpst->readin = 0;
6536
6537 /* No private part is necessary for include psymtabs. This property
6538 can be used to differentiate between such include psymtabs and
10b3939b 6539 the regular ones. */
58a9656e 6540 subpst->read_symtab_private = NULL;
aaa75496
JB
6541}
6542
6543/* Read the Line Number Program data and extract the list of files
6544 included by the source file represented by PST. Build an include
d85a05f0 6545 partial symtab for each of these included files. */
aaa75496
JB
6546
6547static void
6548dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
dee91e82
DE
6549 struct die_info *die,
6550 struct partial_symtab *pst)
aaa75496 6551{
fff8551c 6552 line_header_up lh;
d85a05f0 6553 struct attribute *attr;
aaa75496 6554
d85a05f0
DJ
6555 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
6556 if (attr)
9c541725 6557 lh = dwarf_decode_line_header ((sect_offset) DW_UNSND (attr), cu);
aaa75496
JB
6558 if (lh == NULL)
6559 return; /* No linetable, so no includes. */
6560
c6da4cef 6561 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). */
fff8551c 6562 dwarf_decode_lines (lh.get (), pst->dirname, cu, pst, pst->textlow, 1);
aaa75496
JB
6563}
6564
348e048f 6565static hashval_t
52dc124a 6566hash_signatured_type (const void *item)
348e048f 6567{
9a3c8263
SM
6568 const struct signatured_type *sig_type
6569 = (const struct signatured_type *) item;
9a619af0 6570
348e048f 6571 /* This drops the top 32 bits of the signature, but is ok for a hash. */
52dc124a 6572 return sig_type->signature;
348e048f
DE
6573}
6574
6575static int
52dc124a 6576eq_signatured_type (const void *item_lhs, const void *item_rhs)
348e048f 6577{
9a3c8263
SM
6578 const struct signatured_type *lhs = (const struct signatured_type *) item_lhs;
6579 const struct signatured_type *rhs = (const struct signatured_type *) item_rhs;
9a619af0 6580
348e048f
DE
6581 return lhs->signature == rhs->signature;
6582}
6583
1fd400ff
TT
6584/* Allocate a hash table for signatured types. */
6585
6586static htab_t
673bfd45 6587allocate_signatured_type_table (struct objfile *objfile)
1fd400ff
TT
6588{
6589 return htab_create_alloc_ex (41,
52dc124a
DE
6590 hash_signatured_type,
6591 eq_signatured_type,
1fd400ff
TT
6592 NULL,
6593 &objfile->objfile_obstack,
6594 hashtab_obstack_allocate,
6595 dummy_obstack_deallocate);
6596}
6597
d467dd73 6598/* A helper function to add a signatured type CU to a table. */
1fd400ff
TT
6599
6600static int
d467dd73 6601add_signatured_type_cu_to_table (void **slot, void *datum)
1fd400ff 6602{
9a3c8263 6603 struct signatured_type *sigt = (struct signatured_type *) *slot;
b2bdb8cf
SM
6604 std::vector<signatured_type *> *all_type_units
6605 = (std::vector<signatured_type *> *) datum;
1fd400ff 6606
b2bdb8cf 6607 all_type_units->push_back (sigt);
1fd400ff
TT
6608
6609 return 1;
6610}
6611
78d4d2c5 6612/* A helper for create_debug_types_hash_table. Read types from SECTION
43988095
JK
6613 and fill them into TYPES_HTAB. It will process only type units,
6614 therefore DW_UT_type. */
c88ee1f0 6615
78d4d2c5 6616static void
ed2dc618
SM
6617create_debug_type_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
6618 struct dwo_file *dwo_file,
43988095
JK
6619 dwarf2_section_info *section, htab_t &types_htab,
6620 rcuh_kind section_kind)
348e048f 6621{
3019eac3 6622 struct objfile *objfile = dwarf2_per_objfile->objfile;
4bdcc0c1 6623 struct dwarf2_section_info *abbrev_section;
78d4d2c5
JK
6624 bfd *abfd;
6625 const gdb_byte *info_ptr, *end_ptr;
348e048f 6626
4bdcc0c1
DE
6627 abbrev_section = (dwo_file != NULL
6628 ? &dwo_file->sections.abbrev
6629 : &dwarf2_per_objfile->abbrev);
6630
b4f54984 6631 if (dwarf_read_debug)
43988095
JK
6632 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
6633 get_section_name (section),
a32a8923 6634 get_section_file_name (abbrev_section));
09406207 6635
78d4d2c5
JK
6636 dwarf2_read_section (objfile, section);
6637 info_ptr = section->buffer;
348e048f 6638
78d4d2c5
JK
6639 if (info_ptr == NULL)
6640 return;
348e048f 6641
78d4d2c5
JK
6642 /* We can't set abfd until now because the section may be empty or
6643 not present, in which case the bfd is unknown. */
6644 abfd = get_section_bfd_owner (section);
348e048f 6645
78d4d2c5
JK
6646 /* We don't use init_cutu_and_read_dies_simple, or some such, here
6647 because we don't need to read any dies: the signature is in the
6648 header. */
3019eac3 6649
78d4d2c5
JK
6650 end_ptr = info_ptr + section->size;
6651 while (info_ptr < end_ptr)
6652 {
78d4d2c5
JK
6653 struct signatured_type *sig_type;
6654 struct dwo_unit *dwo_tu;
6655 void **slot;
6656 const gdb_byte *ptr = info_ptr;
6657 struct comp_unit_head header;
6658 unsigned int length;
8b70b953 6659
9c541725 6660 sect_offset sect_off = (sect_offset) (ptr - section->buffer);
348e048f 6661
a49dd8dd
JK
6662 /* Initialize it due to a false compiler warning. */
6663 header.signature = -1;
9c541725 6664 header.type_cu_offset_in_tu = (cu_offset) -1;
a49dd8dd 6665
78d4d2c5
JK
6666 /* We need to read the type's signature in order to build the hash
6667 table, but we don't need anything else just yet. */
348e048f 6668
ed2dc618 6669 ptr = read_and_check_comp_unit_head (dwarf2_per_objfile, &header, section,
43988095 6670 abbrev_section, ptr, section_kind);
348e048f 6671
78d4d2c5 6672 length = get_cu_length (&header);
6caca83c 6673
78d4d2c5
JK
6674 /* Skip dummy type units. */
6675 if (ptr >= info_ptr + length
43988095
JK
6676 || peek_abbrev_code (abfd, ptr) == 0
6677 || header.unit_type != DW_UT_type)
78d4d2c5
JK
6678 {
6679 info_ptr += length;
6680 continue;
6681 }
dee91e82 6682
78d4d2c5
JK
6683 if (types_htab == NULL)
6684 {
6685 if (dwo_file)
6686 types_htab = allocate_dwo_unit_table (objfile);
6687 else
6688 types_htab = allocate_signatured_type_table (objfile);
6689 }
8b70b953 6690
78d4d2c5
JK
6691 if (dwo_file)
6692 {
6693 sig_type = NULL;
6694 dwo_tu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6695 struct dwo_unit);
6696 dwo_tu->dwo_file = dwo_file;
43988095 6697 dwo_tu->signature = header.signature;
9c541725 6698 dwo_tu->type_offset_in_tu = header.type_cu_offset_in_tu;
78d4d2c5 6699 dwo_tu->section = section;
9c541725 6700 dwo_tu->sect_off = sect_off;
78d4d2c5
JK
6701 dwo_tu->length = length;
6702 }
6703 else
6704 {
6705 /* N.B.: type_offset is not usable if this type uses a DWO file.
6706 The real type_offset is in the DWO file. */
6707 dwo_tu = NULL;
6708 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6709 struct signatured_type);
43988095 6710 sig_type->signature = header.signature;
9c541725 6711 sig_type->type_offset_in_tu = header.type_cu_offset_in_tu;
e3b94546 6712 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
78d4d2c5
JK
6713 sig_type->per_cu.is_debug_types = 1;
6714 sig_type->per_cu.section = section;
9c541725 6715 sig_type->per_cu.sect_off = sect_off;
78d4d2c5
JK
6716 sig_type->per_cu.length = length;
6717 }
6718
6719 slot = htab_find_slot (types_htab,
6720 dwo_file ? (void*) dwo_tu : (void *) sig_type,
6721 INSERT);
6722 gdb_assert (slot != NULL);
6723 if (*slot != NULL)
6724 {
9c541725 6725 sect_offset dup_sect_off;
0349ea22 6726
3019eac3
DE
6727 if (dwo_file)
6728 {
78d4d2c5
JK
6729 const struct dwo_unit *dup_tu
6730 = (const struct dwo_unit *) *slot;
6731
9c541725 6732 dup_sect_off = dup_tu->sect_off;
3019eac3
DE
6733 }
6734 else
6735 {
78d4d2c5
JK
6736 const struct signatured_type *dup_tu
6737 = (const struct signatured_type *) *slot;
6738
9c541725 6739 dup_sect_off = dup_tu->per_cu.sect_off;
3019eac3 6740 }
8b70b953 6741
78d4d2c5 6742 complaint (&symfile_complaints,
9d8780f0
SM
6743 _("debug type entry at offset %s is duplicate to"
6744 " the entry at offset %s, signature %s"),
6745 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
43988095 6746 hex_string (header.signature));
78d4d2c5
JK
6747 }
6748 *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
3019eac3 6749
78d4d2c5 6750 if (dwarf_read_debug > 1)
9d8780f0
SM
6751 fprintf_unfiltered (gdb_stdlog, " offset %s, signature %s\n",
6752 sect_offset_str (sect_off),
43988095 6753 hex_string (header.signature));
3019eac3 6754
78d4d2c5
JK
6755 info_ptr += length;
6756 }
6757}
3019eac3 6758
78d4d2c5
JK
6759/* Create the hash table of all entries in the .debug_types
6760 (or .debug_types.dwo) section(s).
6761 If reading a DWO file, then DWO_FILE is a pointer to the DWO file object,
6762 otherwise it is NULL.
b3c8eb43 6763
78d4d2c5 6764 The result is a pointer to the hash table or NULL if there are no types.
348e048f 6765
78d4d2c5 6766 Note: This function processes DWO files only, not DWP files. */
348e048f 6767
78d4d2c5 6768static void
ed2dc618
SM
6769create_debug_types_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
6770 struct dwo_file *dwo_file,
78d4d2c5
JK
6771 VEC (dwarf2_section_info_def) *types,
6772 htab_t &types_htab)
6773{
6774 int ix;
6775 struct dwarf2_section_info *section;
6776
6777 if (VEC_empty (dwarf2_section_info_def, types))
6778 return;
348e048f 6779
78d4d2c5
JK
6780 for (ix = 0;
6781 VEC_iterate (dwarf2_section_info_def, types, ix, section);
6782 ++ix)
ed2dc618
SM
6783 create_debug_type_hash_table (dwarf2_per_objfile, dwo_file, section,
6784 types_htab, rcuh_kind::TYPE);
3019eac3
DE
6785}
6786
6787/* Create the hash table of all entries in the .debug_types section,
6788 and initialize all_type_units.
6789 The result is zero if there is an error (e.g. missing .debug_types section),
6790 otherwise non-zero. */
6791
6792static int
ed2dc618 6793create_all_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
3019eac3 6794{
78d4d2c5 6795 htab_t types_htab = NULL;
3019eac3 6796
ed2dc618
SM
6797 create_debug_type_hash_table (dwarf2_per_objfile, NULL,
6798 &dwarf2_per_objfile->info, types_htab,
43988095 6799 rcuh_kind::COMPILE);
ed2dc618
SM
6800 create_debug_types_hash_table (dwarf2_per_objfile, NULL,
6801 dwarf2_per_objfile->types, types_htab);
3019eac3
DE
6802 if (types_htab == NULL)
6803 {
6804 dwarf2_per_objfile->signatured_types = NULL;
6805 return 0;
6806 }
6807
348e048f
DE
6808 dwarf2_per_objfile->signatured_types = types_htab;
6809
b2bdb8cf
SM
6810 gdb_assert (dwarf2_per_objfile->all_type_units.empty ());
6811 dwarf2_per_objfile->all_type_units.reserve (htab_elements (types_htab));
6812
6813 htab_traverse_noresize (types_htab, add_signatured_type_cu_to_table,
6814 &dwarf2_per_objfile->all_type_units);
1fd400ff 6815
348e048f
DE
6816 return 1;
6817}
6818
6aa5f3a6
DE
6819/* Add an entry for signature SIG to dwarf2_per_objfile->signatured_types.
6820 If SLOT is non-NULL, it is the entry to use in the hash table.
6821 Otherwise we find one. */
6822
6823static struct signatured_type *
ed2dc618
SM
6824add_type_unit (struct dwarf2_per_objfile *dwarf2_per_objfile, ULONGEST sig,
6825 void **slot)
6aa5f3a6
DE
6826{
6827 struct objfile *objfile = dwarf2_per_objfile->objfile;
6aa5f3a6 6828
b2bdb8cf
SM
6829 if (dwarf2_per_objfile->all_type_units.size ()
6830 == dwarf2_per_objfile->all_type_units.capacity ())
6831 ++dwarf2_per_objfile->tu_stats.nr_all_type_units_reallocs;
6aa5f3a6 6832
b2bdb8cf
SM
6833 signatured_type *sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6834 struct signatured_type);
6835
6836 dwarf2_per_objfile->all_type_units.push_back (sig_type);
6aa5f3a6
DE
6837 sig_type->signature = sig;
6838 sig_type->per_cu.is_debug_types = 1;
6839 if (dwarf2_per_objfile->using_index)
6840 {
6841 sig_type->per_cu.v.quick =
6842 OBSTACK_ZALLOC (&objfile->objfile_obstack,
6843 struct dwarf2_per_cu_quick_data);
6844 }
6845
6846 if (slot == NULL)
6847 {
6848 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
6849 sig_type, INSERT);
6850 }
6851 gdb_assert (*slot == NULL);
6852 *slot = sig_type;
6853 /* The rest of sig_type must be filled in by the caller. */
6854 return sig_type;
6855}
6856
a2ce51a0
DE
6857/* Subroutine of lookup_dwo_signatured_type and lookup_dwp_signatured_type.
6858 Fill in SIG_ENTRY with DWO_ENTRY. */
6859
6860static void
ed2dc618 6861fill_in_sig_entry_from_dwo_entry (struct dwarf2_per_objfile *dwarf2_per_objfile,
a2ce51a0
DE
6862 struct signatured_type *sig_entry,
6863 struct dwo_unit *dwo_entry)
6864{
7ee85ab1 6865 /* Make sure we're not clobbering something we don't expect to. */
a2ce51a0
DE
6866 gdb_assert (! sig_entry->per_cu.queued);
6867 gdb_assert (sig_entry->per_cu.cu == NULL);
6aa5f3a6
DE
6868 if (dwarf2_per_objfile->using_index)
6869 {
6870 gdb_assert (sig_entry->per_cu.v.quick != NULL);
43f3e411 6871 gdb_assert (sig_entry->per_cu.v.quick->compunit_symtab == NULL);
6aa5f3a6
DE
6872 }
6873 else
6874 gdb_assert (sig_entry->per_cu.v.psymtab == NULL);
a2ce51a0 6875 gdb_assert (sig_entry->signature == dwo_entry->signature);
9c541725 6876 gdb_assert (to_underlying (sig_entry->type_offset_in_section) == 0);
a2ce51a0 6877 gdb_assert (sig_entry->type_unit_group == NULL);
7ee85ab1
DE
6878 gdb_assert (sig_entry->dwo_unit == NULL);
6879
6880 sig_entry->per_cu.section = dwo_entry->section;
9c541725 6881 sig_entry->per_cu.sect_off = dwo_entry->sect_off;
7ee85ab1
DE
6882 sig_entry->per_cu.length = dwo_entry->length;
6883 sig_entry->per_cu.reading_dwo_directly = 1;
e3b94546 6884 sig_entry->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
a2ce51a0
DE
6885 sig_entry->type_offset_in_tu = dwo_entry->type_offset_in_tu;
6886 sig_entry->dwo_unit = dwo_entry;
6887}
6888
6889/* Subroutine of lookup_signatured_type.
7ee85ab1
DE
6890 If we haven't read the TU yet, create the signatured_type data structure
6891 for a TU to be read in directly from a DWO file, bypassing the stub.
6892 This is the "Stay in DWO Optimization": When there is no DWP file and we're
6893 using .gdb_index, then when reading a CU we want to stay in the DWO file
6894 containing that CU. Otherwise we could end up reading several other DWO
6895 files (due to comdat folding) to process the transitive closure of all the
6896 mentioned TUs, and that can be slow. The current DWO file will have every
6897 type signature that it needs.
a2ce51a0
DE
6898 We only do this for .gdb_index because in the psymtab case we already have
6899 to read all the DWOs to build the type unit groups. */
6900
6901static struct signatured_type *
6902lookup_dwo_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6903{
518817b3
SM
6904 struct dwarf2_per_objfile *dwarf2_per_objfile
6905 = cu->per_cu->dwarf2_per_objfile;
a2ce51a0
DE
6906 struct objfile *objfile = dwarf2_per_objfile->objfile;
6907 struct dwo_file *dwo_file;
6908 struct dwo_unit find_dwo_entry, *dwo_entry;
6909 struct signatured_type find_sig_entry, *sig_entry;
6aa5f3a6 6910 void **slot;
a2ce51a0
DE
6911
6912 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
6913
6aa5f3a6
DE
6914 /* If TU skeletons have been removed then we may not have read in any
6915 TUs yet. */
6916 if (dwarf2_per_objfile->signatured_types == NULL)
6917 {
6918 dwarf2_per_objfile->signatured_types
6919 = allocate_signatured_type_table (objfile);
6920 }
a2ce51a0
DE
6921
6922 /* We only ever need to read in one copy of a signatured type.
6aa5f3a6
DE
6923 Use the global signatured_types array to do our own comdat-folding
6924 of types. If this is the first time we're reading this TU, and
6925 the TU has an entry in .gdb_index, replace the recorded data from
6926 .gdb_index with this TU. */
a2ce51a0 6927
a2ce51a0 6928 find_sig_entry.signature = sig;
6aa5f3a6
DE
6929 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
6930 &find_sig_entry, INSERT);
9a3c8263 6931 sig_entry = (struct signatured_type *) *slot;
7ee85ab1
DE
6932
6933 /* We can get here with the TU already read, *or* in the process of being
6aa5f3a6
DE
6934 read. Don't reassign the global entry to point to this DWO if that's
6935 the case. Also note that if the TU is already being read, it may not
6936 have come from a DWO, the program may be a mix of Fission-compiled
6937 code and non-Fission-compiled code. */
6938
6939 /* Have we already tried to read this TU?
6940 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
6941 needn't exist in the global table yet). */
6942 if (sig_entry != NULL && sig_entry->per_cu.tu_read)
a2ce51a0
DE
6943 return sig_entry;
6944
6aa5f3a6
DE
6945 /* Note: cu->dwo_unit is the dwo_unit that references this TU, not the
6946 dwo_unit of the TU itself. */
6947 dwo_file = cu->dwo_unit->dwo_file;
6948
a2ce51a0
DE
6949 /* Ok, this is the first time we're reading this TU. */
6950 if (dwo_file->tus == NULL)
6951 return NULL;
6952 find_dwo_entry.signature = sig;
9a3c8263 6953 dwo_entry = (struct dwo_unit *) htab_find (dwo_file->tus, &find_dwo_entry);
a2ce51a0
DE
6954 if (dwo_entry == NULL)
6955 return NULL;
6956
6aa5f3a6
DE
6957 /* If the global table doesn't have an entry for this TU, add one. */
6958 if (sig_entry == NULL)
ed2dc618 6959 sig_entry = add_type_unit (dwarf2_per_objfile, sig, slot);
6aa5f3a6 6960
ed2dc618 6961 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, sig_entry, dwo_entry);
89e63ee4 6962 sig_entry->per_cu.tu_read = 1;
a2ce51a0
DE
6963 return sig_entry;
6964}
6965
a2ce51a0
DE
6966/* Subroutine of lookup_signatured_type.
6967 Look up the type for signature SIG, and if we can't find SIG in .gdb_index
6aa5f3a6
DE
6968 then try the DWP file. If the TU stub (skeleton) has been removed then
6969 it won't be in .gdb_index. */
a2ce51a0
DE
6970
6971static struct signatured_type *
6972lookup_dwp_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6973{
518817b3
SM
6974 struct dwarf2_per_objfile *dwarf2_per_objfile
6975 = cu->per_cu->dwarf2_per_objfile;
a2ce51a0 6976 struct objfile *objfile = dwarf2_per_objfile->objfile;
ed2dc618 6977 struct dwp_file *dwp_file = get_dwp_file (dwarf2_per_objfile);
a2ce51a0
DE
6978 struct dwo_unit *dwo_entry;
6979 struct signatured_type find_sig_entry, *sig_entry;
6aa5f3a6 6980 void **slot;
a2ce51a0
DE
6981
6982 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
6983 gdb_assert (dwp_file != NULL);
6984
6aa5f3a6
DE
6985 /* If TU skeletons have been removed then we may not have read in any
6986 TUs yet. */
6987 if (dwarf2_per_objfile->signatured_types == NULL)
a2ce51a0 6988 {
6aa5f3a6
DE
6989 dwarf2_per_objfile->signatured_types
6990 = allocate_signatured_type_table (objfile);
a2ce51a0
DE
6991 }
6992
6aa5f3a6
DE
6993 find_sig_entry.signature = sig;
6994 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
6995 &find_sig_entry, INSERT);
9a3c8263 6996 sig_entry = (struct signatured_type *) *slot;
6aa5f3a6
DE
6997
6998 /* Have we already tried to read this TU?
6999 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
7000 needn't exist in the global table yet). */
7001 if (sig_entry != NULL)
7002 return sig_entry;
7003
a2ce51a0
DE
7004 if (dwp_file->tus == NULL)
7005 return NULL;
ed2dc618 7006 dwo_entry = lookup_dwo_unit_in_dwp (dwarf2_per_objfile, dwp_file, NULL,
57d63ce2 7007 sig, 1 /* is_debug_types */);
a2ce51a0
DE
7008 if (dwo_entry == NULL)
7009 return NULL;
7010
ed2dc618
SM
7011 sig_entry = add_type_unit (dwarf2_per_objfile, sig, slot);
7012 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, sig_entry, dwo_entry);
a2ce51a0 7013
a2ce51a0
DE
7014 return sig_entry;
7015}
7016
380bca97 7017/* Lookup a signature based type for DW_FORM_ref_sig8.
5a8b3f62
DE
7018 Returns NULL if signature SIG is not present in the table.
7019 It is up to the caller to complain about this. */
348e048f
DE
7020
7021static struct signatured_type *
a2ce51a0 7022lookup_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
348e048f 7023{
518817b3
SM
7024 struct dwarf2_per_objfile *dwarf2_per_objfile
7025 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 7026
a2ce51a0
DE
7027 if (cu->dwo_unit
7028 && dwarf2_per_objfile->using_index)
7029 {
7030 /* We're in a DWO/DWP file, and we're using .gdb_index.
7031 These cases require special processing. */
ed2dc618 7032 if (get_dwp_file (dwarf2_per_objfile) == NULL)
a2ce51a0
DE
7033 return lookup_dwo_signatured_type (cu, sig);
7034 else
7035 return lookup_dwp_signatured_type (cu, sig);
7036 }
7037 else
7038 {
7039 struct signatured_type find_entry, *entry;
348e048f 7040
a2ce51a0
DE
7041 if (dwarf2_per_objfile->signatured_types == NULL)
7042 return NULL;
7043 find_entry.signature = sig;
9a3c8263
SM
7044 entry = ((struct signatured_type *)
7045 htab_find (dwarf2_per_objfile->signatured_types, &find_entry));
a2ce51a0
DE
7046 return entry;
7047 }
348e048f 7048}
42e7ad6c
DE
7049\f
7050/* Low level DIE reading support. */
348e048f 7051
d85a05f0
DJ
7052/* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
7053
7054static void
7055init_cu_die_reader (struct die_reader_specs *reader,
dee91e82 7056 struct dwarf2_cu *cu,
3019eac3 7057 struct dwarf2_section_info *section,
685af9cd
TT
7058 struct dwo_file *dwo_file,
7059 struct abbrev_table *abbrev_table)
d85a05f0 7060{
fceca515 7061 gdb_assert (section->readin && section->buffer != NULL);
a32a8923 7062 reader->abfd = get_section_bfd_owner (section);
d85a05f0 7063 reader->cu = cu;
3019eac3 7064 reader->dwo_file = dwo_file;
dee91e82
DE
7065 reader->die_section = section;
7066 reader->buffer = section->buffer;
f664829e 7067 reader->buffer_end = section->buffer + section->size;
a2ce51a0 7068 reader->comp_dir = NULL;
685af9cd 7069 reader->abbrev_table = abbrev_table;
d85a05f0
DJ
7070}
7071
b0c7bfa9
DE
7072/* Subroutine of init_cutu_and_read_dies to simplify it.
7073 Read in the rest of a CU/TU top level DIE from DWO_UNIT.
7074 There's just a lot of work to do, and init_cutu_and_read_dies is big enough
7075 already.
7076
7077 STUB_COMP_UNIT_DIE is for the stub DIE, we copy over certain attributes
7078 from it to the DIE in the DWO. If NULL we are skipping the stub.
a2ce51a0
DE
7079 STUB_COMP_DIR is similar to STUB_COMP_UNIT_DIE: When reading a TU directly
7080 from the DWO file, bypassing the stub, it contains the DW_AT_comp_dir
c54a1dd8
DE
7081 attribute of the referencing CU. At most one of STUB_COMP_UNIT_DIE and
7082 STUB_COMP_DIR may be non-NULL.
b0c7bfa9
DE
7083 *RESULT_READER,*RESULT_INFO_PTR,*RESULT_COMP_UNIT_DIE,*RESULT_HAS_CHILDREN
7084 are filled in with the info of the DIE from the DWO file.
685af9cd
TT
7085 *RESULT_DWO_ABBREV_TABLE will be filled in with the abbrev table allocated
7086 from the dwo. Since *RESULT_READER references this abbrev table, it must be
7087 kept around for at least as long as *RESULT_READER.
7088
b0c7bfa9
DE
7089 The result is non-zero if a valid (non-dummy) DIE was found. */
7090
7091static int
7092read_cutu_die_from_dwo (struct dwarf2_per_cu_data *this_cu,
7093 struct dwo_unit *dwo_unit,
b0c7bfa9 7094 struct die_info *stub_comp_unit_die,
a2ce51a0 7095 const char *stub_comp_dir,
b0c7bfa9 7096 struct die_reader_specs *result_reader,
d521ce57 7097 const gdb_byte **result_info_ptr,
b0c7bfa9 7098 struct die_info **result_comp_unit_die,
685af9cd
TT
7099 int *result_has_children,
7100 abbrev_table_up *result_dwo_abbrev_table)
b0c7bfa9 7101{
ed2dc618 7102 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
b0c7bfa9
DE
7103 struct objfile *objfile = dwarf2_per_objfile->objfile;
7104 struct dwarf2_cu *cu = this_cu->cu;
b0c7bfa9 7105 bfd *abfd;
d521ce57 7106 const gdb_byte *begin_info_ptr, *info_ptr;
b0c7bfa9
DE
7107 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
7108 int i,num_extra_attrs;
7109 struct dwarf2_section_info *dwo_abbrev_section;
7110 struct attribute *attr;
7111 struct die_info *comp_unit_die;
7112
b0aeadb3
DE
7113 /* At most one of these may be provided. */
7114 gdb_assert ((stub_comp_unit_die != NULL) + (stub_comp_dir != NULL) <= 1);
a2ce51a0 7115
b0c7bfa9
DE
7116 /* These attributes aren't processed until later:
7117 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
0d60c288
DE
7118 DW_AT_comp_dir is used now, to find the DWO file, but it is also
7119 referenced later. However, these attributes are found in the stub
7120 which we won't have later. In order to not impose this complication
7121 on the rest of the code, we read them here and copy them to the
7122 DWO CU/TU die. */
b0c7bfa9
DE
7123
7124 stmt_list = NULL;
7125 low_pc = NULL;
7126 high_pc = NULL;
7127 ranges = NULL;
7128 comp_dir = NULL;
7129
7130 if (stub_comp_unit_die != NULL)
7131 {
7132 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
7133 DWO file. */
7134 if (! this_cu->is_debug_types)
7135 stmt_list = dwarf2_attr (stub_comp_unit_die, DW_AT_stmt_list, cu);
7136 low_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_low_pc, cu);
7137 high_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_high_pc, cu);
7138 ranges = dwarf2_attr (stub_comp_unit_die, DW_AT_ranges, cu);
7139 comp_dir = dwarf2_attr (stub_comp_unit_die, DW_AT_comp_dir, cu);
7140
7141 /* There should be a DW_AT_addr_base attribute here (if needed).
7142 We need the value before we can process DW_FORM_GNU_addr_index. */
7143 cu->addr_base = 0;
7144 attr = dwarf2_attr (stub_comp_unit_die, DW_AT_GNU_addr_base, cu);
7145 if (attr)
7146 cu->addr_base = DW_UNSND (attr);
7147
7148 /* There should be a DW_AT_ranges_base attribute here (if needed).
7149 We need the value before we can process DW_AT_ranges. */
7150 cu->ranges_base = 0;
7151 attr = dwarf2_attr (stub_comp_unit_die, DW_AT_GNU_ranges_base, cu);
7152 if (attr)
7153 cu->ranges_base = DW_UNSND (attr);
7154 }
a2ce51a0
DE
7155 else if (stub_comp_dir != NULL)
7156 {
7157 /* Reconstruct the comp_dir attribute to simplify the code below. */
8d749320 7158 comp_dir = XOBNEW (&cu->comp_unit_obstack, struct attribute);
a2ce51a0
DE
7159 comp_dir->name = DW_AT_comp_dir;
7160 comp_dir->form = DW_FORM_string;
7161 DW_STRING_IS_CANONICAL (comp_dir) = 0;
7162 DW_STRING (comp_dir) = stub_comp_dir;
7163 }
b0c7bfa9
DE
7164
7165 /* Set up for reading the DWO CU/TU. */
7166 cu->dwo_unit = dwo_unit;
685af9cd 7167 dwarf2_section_info *section = dwo_unit->section;
b0c7bfa9 7168 dwarf2_read_section (objfile, section);
a32a8923 7169 abfd = get_section_bfd_owner (section);
9c541725
PA
7170 begin_info_ptr = info_ptr = (section->buffer
7171 + to_underlying (dwo_unit->sect_off));
b0c7bfa9 7172 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
b0c7bfa9
DE
7173
7174 if (this_cu->is_debug_types)
7175 {
b0c7bfa9
DE
7176 struct signatured_type *sig_type = (struct signatured_type *) this_cu;
7177
ed2dc618
SM
7178 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7179 &cu->header, section,
b0c7bfa9 7180 dwo_abbrev_section,
43988095 7181 info_ptr, rcuh_kind::TYPE);
a2ce51a0 7182 /* This is not an assert because it can be caused by bad debug info. */
43988095 7183 if (sig_type->signature != cu->header.signature)
a2ce51a0
DE
7184 {
7185 error (_("Dwarf Error: signature mismatch %s vs %s while reading"
9d8780f0 7186 " TU at offset %s [in module %s]"),
a2ce51a0 7187 hex_string (sig_type->signature),
43988095 7188 hex_string (cu->header.signature),
9d8780f0 7189 sect_offset_str (dwo_unit->sect_off),
a2ce51a0
DE
7190 bfd_get_filename (abfd));
7191 }
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 nor the type's offset in the TU until now. */
7195 dwo_unit->length = get_cu_length (&cu->header);
9c541725 7196 dwo_unit->type_offset_in_tu = cu->header.type_cu_offset_in_tu;
b0c7bfa9
DE
7197
7198 /* Establish the type offset that can be used to lookup the type.
7199 For DWO files, we don't know it until now. */
9c541725
PA
7200 sig_type->type_offset_in_section
7201 = dwo_unit->sect_off + to_underlying (dwo_unit->type_offset_in_tu);
b0c7bfa9
DE
7202 }
7203 else
7204 {
ed2dc618
SM
7205 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7206 &cu->header, section,
b0c7bfa9 7207 dwo_abbrev_section,
43988095 7208 info_ptr, rcuh_kind::COMPILE);
9c541725 7209 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
b0c7bfa9
DE
7210 /* For DWOs coming from DWP files, we don't know the CU length
7211 until now. */
7212 dwo_unit->length = get_cu_length (&cu->header);
7213 }
7214
685af9cd
TT
7215 *result_dwo_abbrev_table
7216 = abbrev_table_read_table (dwarf2_per_objfile, dwo_abbrev_section,
7217 cu->header.abbrev_sect_off);
7218 init_cu_die_reader (result_reader, cu, section, dwo_unit->dwo_file,
7219 result_dwo_abbrev_table->get ());
b0c7bfa9
DE
7220
7221 /* Read in the die, but leave space to copy over the attributes
7222 from the stub. This has the benefit of simplifying the rest of
7223 the code - all the work to maintain the illusion of a single
7224 DW_TAG_{compile,type}_unit DIE is done here. */
7225 num_extra_attrs = ((stmt_list != NULL)
7226 + (low_pc != NULL)
7227 + (high_pc != NULL)
7228 + (ranges != NULL)
7229 + (comp_dir != NULL));
7230 info_ptr = read_full_die_1 (result_reader, result_comp_unit_die, info_ptr,
7231 result_has_children, num_extra_attrs);
7232
7233 /* Copy over the attributes from the stub to the DIE we just read in. */
7234 comp_unit_die = *result_comp_unit_die;
7235 i = comp_unit_die->num_attrs;
7236 if (stmt_list != NULL)
7237 comp_unit_die->attrs[i++] = *stmt_list;
7238 if (low_pc != NULL)
7239 comp_unit_die->attrs[i++] = *low_pc;
7240 if (high_pc != NULL)
7241 comp_unit_die->attrs[i++] = *high_pc;
7242 if (ranges != NULL)
7243 comp_unit_die->attrs[i++] = *ranges;
7244 if (comp_dir != NULL)
7245 comp_unit_die->attrs[i++] = *comp_dir;
7246 comp_unit_die->num_attrs += num_extra_attrs;
7247
b4f54984 7248 if (dwarf_die_debug)
bf6af496
DE
7249 {
7250 fprintf_unfiltered (gdb_stdlog,
7251 "Read die from %s@0x%x of %s:\n",
a32a8923 7252 get_section_name (section),
bf6af496
DE
7253 (unsigned) (begin_info_ptr - section->buffer),
7254 bfd_get_filename (abfd));
b4f54984 7255 dump_die (comp_unit_die, dwarf_die_debug);
bf6af496
DE
7256 }
7257
a2ce51a0
DE
7258 /* Save the comp_dir attribute. If there is no DWP file then we'll read
7259 TUs by skipping the stub and going directly to the entry in the DWO file.
7260 However, skipping the stub means we won't get DW_AT_comp_dir, so we have
7261 to get it via circuitous means. Blech. */
7262 if (comp_dir != NULL)
7263 result_reader->comp_dir = DW_STRING (comp_dir);
7264
b0c7bfa9
DE
7265 /* Skip dummy compilation units. */
7266 if (info_ptr >= begin_info_ptr + dwo_unit->length
7267 || peek_abbrev_code (abfd, info_ptr) == 0)
7268 return 0;
7269
7270 *result_info_ptr = info_ptr;
7271 return 1;
7272}
7273
7274/* Subroutine of init_cutu_and_read_dies to simplify it.
7275 Look up the DWO unit specified by COMP_UNIT_DIE of THIS_CU.
6a506a2d 7276 Returns NULL if the specified DWO unit cannot be found. */
b0c7bfa9
DE
7277
7278static struct dwo_unit *
7279lookup_dwo_unit (struct dwarf2_per_cu_data *this_cu,
7280 struct die_info *comp_unit_die)
7281{
7282 struct dwarf2_cu *cu = this_cu->cu;
b0c7bfa9
DE
7283 ULONGEST signature;
7284 struct dwo_unit *dwo_unit;
7285 const char *comp_dir, *dwo_name;
7286
a2ce51a0
DE
7287 gdb_assert (cu != NULL);
7288
b0c7bfa9 7289 /* Yeah, we look dwo_name up again, but it simplifies the code. */
7d45c7c3
KB
7290 dwo_name = dwarf2_string_attr (comp_unit_die, DW_AT_GNU_dwo_name, cu);
7291 comp_dir = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
b0c7bfa9
DE
7292
7293 if (this_cu->is_debug_types)
7294 {
7295 struct signatured_type *sig_type;
7296
7297 /* Since this_cu is the first member of struct signatured_type,
7298 we can go from a pointer to one to a pointer to the other. */
7299 sig_type = (struct signatured_type *) this_cu;
7300 signature = sig_type->signature;
7301 dwo_unit = lookup_dwo_type_unit (sig_type, dwo_name, comp_dir);
7302 }
7303 else
7304 {
7305 struct attribute *attr;
7306
7307 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
7308 if (! attr)
7309 error (_("Dwarf Error: missing dwo_id for dwo_name %s"
7310 " [in module %s]"),
e3b94546 7311 dwo_name, objfile_name (this_cu->dwarf2_per_objfile->objfile));
b0c7bfa9
DE
7312 signature = DW_UNSND (attr);
7313 dwo_unit = lookup_dwo_comp_unit (this_cu, dwo_name, comp_dir,
7314 signature);
7315 }
7316
b0c7bfa9
DE
7317 return dwo_unit;
7318}
7319
a2ce51a0 7320/* Subroutine of init_cutu_and_read_dies to simplify it.
6aa5f3a6 7321 See it for a description of the parameters.
fcd3b13d 7322 Read a TU directly from a DWO file, bypassing the stub. */
a2ce51a0
DE
7323
7324static void
6aa5f3a6
DE
7325init_tu_and_read_dwo_dies (struct dwarf2_per_cu_data *this_cu,
7326 int use_existing_cu, int keep,
a2ce51a0
DE
7327 die_reader_func_ftype *die_reader_func,
7328 void *data)
7329{
fcd3b13d 7330 std::unique_ptr<dwarf2_cu> new_cu;
a2ce51a0 7331 struct signatured_type *sig_type;
a2ce51a0
DE
7332 struct die_reader_specs reader;
7333 const gdb_byte *info_ptr;
7334 struct die_info *comp_unit_die;
7335 int has_children;
ed2dc618 7336 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
a2ce51a0
DE
7337
7338 /* Verify we can do the following downcast, and that we have the
7339 data we need. */
7340 gdb_assert (this_cu->is_debug_types && this_cu->reading_dwo_directly);
7341 sig_type = (struct signatured_type *) this_cu;
7342 gdb_assert (sig_type->dwo_unit != NULL);
7343
6aa5f3a6
DE
7344 if (use_existing_cu && this_cu->cu != NULL)
7345 {
7346 gdb_assert (this_cu->cu->dwo_unit == sig_type->dwo_unit);
6aa5f3a6
DE
7347 /* There's no need to do the rereading_dwo_cu handling that
7348 init_cutu_and_read_dies does since we don't read the stub. */
7349 }
7350 else
7351 {
7352 /* If !use_existing_cu, this_cu->cu must be NULL. */
7353 gdb_assert (this_cu->cu == NULL);
fcd3b13d 7354 new_cu.reset (new dwarf2_cu (this_cu));
6aa5f3a6
DE
7355 }
7356
7357 /* A future optimization, if needed, would be to use an existing
7358 abbrev table. When reading DWOs with skeletonless TUs, all the TUs
7359 could share abbrev tables. */
a2ce51a0 7360
685af9cd
TT
7361 /* The abbreviation table used by READER, this must live at least as long as
7362 READER. */
7363 abbrev_table_up dwo_abbrev_table;
7364
a2ce51a0 7365 if (read_cutu_die_from_dwo (this_cu, sig_type->dwo_unit,
a2ce51a0
DE
7366 NULL /* stub_comp_unit_die */,
7367 sig_type->dwo_unit->dwo_file->comp_dir,
7368 &reader, &info_ptr,
685af9cd
TT
7369 &comp_unit_die, &has_children,
7370 &dwo_abbrev_table) == 0)
a2ce51a0
DE
7371 {
7372 /* Dummy die. */
a2ce51a0
DE
7373 return;
7374 }
7375
7376 /* All the "real" work is done here. */
7377 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
7378
6aa5f3a6 7379 /* This duplicates the code in init_cutu_and_read_dies,
a2ce51a0
DE
7380 but the alternative is making the latter more complex.
7381 This function is only for the special case of using DWO files directly:
7382 no point in overly complicating the general case just to handle this. */
fcd3b13d 7383 if (new_cu != NULL && keep)
a2ce51a0 7384 {
fcd3b13d
SM
7385 /* Link this CU into read_in_chain. */
7386 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
7387 dwarf2_per_objfile->read_in_chain = this_cu;
7388 /* The chain owns it now. */
7389 new_cu.release ();
a2ce51a0 7390 }
a2ce51a0
DE
7391}
7392
fd820528 7393/* Initialize a CU (or TU) and read its DIEs.
3019eac3 7394 If the CU defers to a DWO file, read the DWO file as well.
dee91e82 7395
f4dc4d17
DE
7396 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
7397 Otherwise the table specified in the comp unit header is read in and used.
7398 This is an optimization for when we already have the abbrev table.
7399
dee91e82
DE
7400 If USE_EXISTING_CU is non-zero, and THIS_CU->cu is non-NULL, then use it.
7401 Otherwise, a new CU is allocated with xmalloc.
7402
7403 If KEEP is non-zero, then if we allocated a dwarf2_cu we add it to
7404 read_in_chain. Otherwise the dwarf2_cu data is freed at the end.
7405
7406 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
fd820528 7407 linker) then DIE_READER_FUNC will not get called. */
aaa75496 7408
70221824 7409static void
fd820528 7410init_cutu_and_read_dies (struct dwarf2_per_cu_data *this_cu,
f4dc4d17 7411 struct abbrev_table *abbrev_table,
fd820528
DE
7412 int use_existing_cu, int keep,
7413 die_reader_func_ftype *die_reader_func,
7414 void *data)
c906108c 7415{
ed2dc618 7416 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
dee91e82 7417 struct objfile *objfile = dwarf2_per_objfile->objfile;
8a0459fd 7418 struct dwarf2_section_info *section = this_cu->section;
a32a8923 7419 bfd *abfd = get_section_bfd_owner (section);
dee91e82 7420 struct dwarf2_cu *cu;
d521ce57 7421 const gdb_byte *begin_info_ptr, *info_ptr;
dee91e82 7422 struct die_reader_specs reader;
d85a05f0 7423 struct die_info *comp_unit_die;
dee91e82 7424 int has_children;
d85a05f0 7425 struct attribute *attr;
dee91e82 7426 struct signatured_type *sig_type = NULL;
4bdcc0c1 7427 struct dwarf2_section_info *abbrev_section;
42e7ad6c
DE
7428 /* Non-zero if CU currently points to a DWO file and we need to
7429 reread it. When this happens we need to reread the skeleton die
a2ce51a0 7430 before we can reread the DWO file (this only applies to CUs, not TUs). */
42e7ad6c 7431 int rereading_dwo_cu = 0;
c906108c 7432
b4f54984 7433 if (dwarf_die_debug)
9d8780f0 7434 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
09406207 7435 this_cu->is_debug_types ? "type" : "comp",
9d8780f0 7436 sect_offset_str (this_cu->sect_off));
09406207 7437
dee91e82
DE
7438 if (use_existing_cu)
7439 gdb_assert (keep);
23745b47 7440
a2ce51a0
DE
7441 /* If we're reading a TU directly from a DWO file, including a virtual DWO
7442 file (instead of going through the stub), short-circuit all of this. */
7443 if (this_cu->reading_dwo_directly)
7444 {
7445 /* Narrow down the scope of possibilities to have to understand. */
7446 gdb_assert (this_cu->is_debug_types);
7447 gdb_assert (abbrev_table == NULL);
6aa5f3a6
DE
7448 init_tu_and_read_dwo_dies (this_cu, use_existing_cu, keep,
7449 die_reader_func, data);
a2ce51a0
DE
7450 return;
7451 }
7452
dee91e82
DE
7453 /* This is cheap if the section is already read in. */
7454 dwarf2_read_section (objfile, section);
7455
9c541725 7456 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
36586728
TT
7457
7458 abbrev_section = get_abbrev_section_for_cu (this_cu);
dee91e82 7459
fcd3b13d 7460 std::unique_ptr<dwarf2_cu> new_cu;
dee91e82
DE
7461 if (use_existing_cu && this_cu->cu != NULL)
7462 {
7463 cu = this_cu->cu;
42e7ad6c
DE
7464 /* If this CU is from a DWO file we need to start over, we need to
7465 refetch the attributes from the skeleton CU.
7466 This could be optimized by retrieving those attributes from when we
7467 were here the first time: the previous comp_unit_die was stored in
7468 comp_unit_obstack. But there's no data yet that we need this
7469 optimization. */
7470 if (cu->dwo_unit != NULL)
7471 rereading_dwo_cu = 1;
dee91e82
DE
7472 }
7473 else
7474 {
7475 /* If !use_existing_cu, this_cu->cu must be NULL. */
7476 gdb_assert (this_cu->cu == NULL);
fcd3b13d
SM
7477 new_cu.reset (new dwarf2_cu (this_cu));
7478 cu = new_cu.get ();
42e7ad6c 7479 }
dee91e82 7480
b0c7bfa9 7481 /* Get the header. */
9c541725 7482 if (to_underlying (cu->header.first_die_cu_offset) != 0 && !rereading_dwo_cu)
42e7ad6c
DE
7483 {
7484 /* We already have the header, there's no need to read it in again. */
9c541725 7485 info_ptr += to_underlying (cu->header.first_die_cu_offset);
42e7ad6c
DE
7486 }
7487 else
7488 {
3019eac3 7489 if (this_cu->is_debug_types)
dee91e82 7490 {
ed2dc618
SM
7491 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7492 &cu->header, section,
4bdcc0c1 7493 abbrev_section, info_ptr,
43988095 7494 rcuh_kind::TYPE);
dee91e82 7495
42e7ad6c
DE
7496 /* Since per_cu is the first member of struct signatured_type,
7497 we can go from a pointer to one to a pointer to the other. */
7498 sig_type = (struct signatured_type *) this_cu;
43988095 7499 gdb_assert (sig_type->signature == cu->header.signature);
9c541725
PA
7500 gdb_assert (sig_type->type_offset_in_tu
7501 == cu->header.type_cu_offset_in_tu);
7502 gdb_assert (this_cu->sect_off == cu->header.sect_off);
dee91e82 7503
42e7ad6c
DE
7504 /* LENGTH has not been set yet for type units if we're
7505 using .gdb_index. */
1ce1cefd 7506 this_cu->length = get_cu_length (&cu->header);
3019eac3
DE
7507
7508 /* Establish the type offset that can be used to lookup the type. */
9c541725
PA
7509 sig_type->type_offset_in_section =
7510 this_cu->sect_off + to_underlying (sig_type->type_offset_in_tu);
43988095
JK
7511
7512 this_cu->dwarf_version = cu->header.version;
dee91e82
DE
7513 }
7514 else
7515 {
ed2dc618
SM
7516 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7517 &cu->header, section,
4bdcc0c1 7518 abbrev_section,
43988095
JK
7519 info_ptr,
7520 rcuh_kind::COMPILE);
dee91e82 7521
9c541725 7522 gdb_assert (this_cu->sect_off == cu->header.sect_off);
1ce1cefd 7523 gdb_assert (this_cu->length == get_cu_length (&cu->header));
43988095 7524 this_cu->dwarf_version = cu->header.version;
dee91e82
DE
7525 }
7526 }
10b3939b 7527
6caca83c 7528 /* Skip dummy compilation units. */
dee91e82 7529 if (info_ptr >= begin_info_ptr + this_cu->length
6caca83c 7530 || peek_abbrev_code (abfd, info_ptr) == 0)
fcd3b13d 7531 return;
6caca83c 7532
433df2d4
DE
7533 /* If we don't have them yet, read the abbrevs for this compilation unit.
7534 And if we need to read them now, make sure they're freed when we're
685af9cd
TT
7535 done (own the table through ABBREV_TABLE_HOLDER). */
7536 abbrev_table_up abbrev_table_holder;
f4dc4d17 7537 if (abbrev_table != NULL)
685af9cd
TT
7538 gdb_assert (cu->header.abbrev_sect_off == abbrev_table->sect_off);
7539 else
f4dc4d17 7540 {
685af9cd
TT
7541 abbrev_table_holder
7542 = abbrev_table_read_table (dwarf2_per_objfile, abbrev_section,
7543 cu->header.abbrev_sect_off);
7544 abbrev_table = abbrev_table_holder.get ();
42e7ad6c 7545 }
af703f96 7546
dee91e82 7547 /* Read the top level CU/TU die. */
685af9cd 7548 init_cu_die_reader (&reader, cu, section, NULL, abbrev_table);
dee91e82 7549 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
93311388 7550
b0c7bfa9 7551 /* If we are in a DWO stub, process it and then read in the "real" CU/TU
685af9cd
TT
7552 from the DWO file. read_cutu_die_from_dwo will allocate the abbreviation
7553 table from the DWO file and pass the ownership over to us. It will be
7554 referenced from READER, so we must make sure to free it after we're done
7555 with READER.
7556
b0c7bfa9
DE
7557 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains a
7558 DWO CU, that this test will fail (the attribute will not be present). */
3019eac3 7559 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_name, cu);
685af9cd 7560 abbrev_table_up dwo_abbrev_table;
3019eac3
DE
7561 if (attr)
7562 {
3019eac3 7563 struct dwo_unit *dwo_unit;
b0c7bfa9 7564 struct die_info *dwo_comp_unit_die;
3019eac3
DE
7565
7566 if (has_children)
6a506a2d
DE
7567 {
7568 complaint (&symfile_complaints,
7569 _("compilation unit with DW_AT_GNU_dwo_name"
9d8780f0
SM
7570 " has children (offset %s) [in module %s]"),
7571 sect_offset_str (this_cu->sect_off),
7572 bfd_get_filename (abfd));
6a506a2d 7573 }
b0c7bfa9 7574 dwo_unit = lookup_dwo_unit (this_cu, comp_unit_die);
6a506a2d 7575 if (dwo_unit != NULL)
3019eac3 7576 {
6a506a2d 7577 if (read_cutu_die_from_dwo (this_cu, dwo_unit,
a2ce51a0 7578 comp_unit_die, NULL,
6a506a2d 7579 &reader, &info_ptr,
685af9cd
TT
7580 &dwo_comp_unit_die, &has_children,
7581 &dwo_abbrev_table) == 0)
6a506a2d
DE
7582 {
7583 /* Dummy die. */
6a506a2d
DE
7584 return;
7585 }
7586 comp_unit_die = dwo_comp_unit_die;
7587 }
7588 else
7589 {
7590 /* Yikes, we couldn't find the rest of the DIE, we only have
7591 the stub. A complaint has already been logged. There's
7592 not much more we can do except pass on the stub DIE to
7593 die_reader_func. We don't want to throw an error on bad
7594 debug info. */
3019eac3
DE
7595 }
7596 }
7597
b0c7bfa9 7598 /* All of the above is setup for this call. Yikes. */
dee91e82
DE
7599 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
7600
b0c7bfa9 7601 /* Done, clean up. */
fcd3b13d 7602 if (new_cu != NULL && keep)
348e048f 7603 {
fcd3b13d
SM
7604 /* Link this CU into read_in_chain. */
7605 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
7606 dwarf2_per_objfile->read_in_chain = this_cu;
7607 /* The chain owns it now. */
7608 new_cu.release ();
348e048f 7609 }
dee91e82
DE
7610}
7611
33e80786
DE
7612/* Read CU/TU THIS_CU but do not follow DW_AT_GNU_dwo_name if present.
7613 DWO_FILE, if non-NULL, is the DWO file to read (the caller is assumed
7614 to have already done the lookup to find the DWO file).
dee91e82
DE
7615
7616 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
3019eac3 7617 THIS_CU->is_debug_types, but nothing else.
dee91e82
DE
7618
7619 We fill in THIS_CU->length.
7620
7621 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
7622 linker) then DIE_READER_FUNC will not get called.
7623
7624 THIS_CU->cu is always freed when done.
3019eac3
DE
7625 This is done in order to not leave THIS_CU->cu in a state where we have
7626 to care whether it refers to the "main" CU or the DWO CU. */
dee91e82
DE
7627
7628static void
7629init_cutu_and_read_dies_no_follow (struct dwarf2_per_cu_data *this_cu,
3019eac3 7630 struct dwo_file *dwo_file,
dee91e82
DE
7631 die_reader_func_ftype *die_reader_func,
7632 void *data)
7633{
ed2dc618 7634 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
dee91e82 7635 struct objfile *objfile = dwarf2_per_objfile->objfile;
8a0459fd 7636 struct dwarf2_section_info *section = this_cu->section;
a32a8923 7637 bfd *abfd = get_section_bfd_owner (section);
33e80786 7638 struct dwarf2_section_info *abbrev_section;
d521ce57 7639 const gdb_byte *begin_info_ptr, *info_ptr;
dee91e82 7640 struct die_reader_specs reader;
dee91e82
DE
7641 struct die_info *comp_unit_die;
7642 int has_children;
7643
b4f54984 7644 if (dwarf_die_debug)
9d8780f0 7645 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
09406207 7646 this_cu->is_debug_types ? "type" : "comp",
9d8780f0 7647 sect_offset_str (this_cu->sect_off));
09406207 7648
dee91e82
DE
7649 gdb_assert (this_cu->cu == NULL);
7650
33e80786
DE
7651 abbrev_section = (dwo_file != NULL
7652 ? &dwo_file->sections.abbrev
7653 : get_abbrev_section_for_cu (this_cu));
7654
dee91e82
DE
7655 /* This is cheap if the section is already read in. */
7656 dwarf2_read_section (objfile, section);
7657
fcd3b13d 7658 struct dwarf2_cu cu (this_cu);
dee91e82 7659
9c541725 7660 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
ed2dc618
SM
7661 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7662 &cu.header, section,
4bdcc0c1 7663 abbrev_section, info_ptr,
43988095
JK
7664 (this_cu->is_debug_types
7665 ? rcuh_kind::TYPE
7666 : rcuh_kind::COMPILE));
dee91e82 7667
1ce1cefd 7668 this_cu->length = get_cu_length (&cu.header);
dee91e82
DE
7669
7670 /* Skip dummy compilation units. */
7671 if (info_ptr >= begin_info_ptr + this_cu->length
7672 || peek_abbrev_code (abfd, info_ptr) == 0)
fcd3b13d 7673 return;
72bf9492 7674
685af9cd
TT
7675 abbrev_table_up abbrev_table
7676 = abbrev_table_read_table (dwarf2_per_objfile, abbrev_section,
7677 cu.header.abbrev_sect_off);
dee91e82 7678
685af9cd 7679 init_cu_die_reader (&reader, &cu, section, dwo_file, abbrev_table.get ());
dee91e82
DE
7680 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
7681
7682 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
dee91e82
DE
7683}
7684
3019eac3
DE
7685/* Read a CU/TU, except that this does not look for DW_AT_GNU_dwo_name and
7686 does not lookup the specified DWO file.
7687 This cannot be used to read DWO files.
dee91e82
DE
7688
7689 THIS_CU->cu is always freed when done.
3019eac3
DE
7690 This is done in order to not leave THIS_CU->cu in a state where we have
7691 to care whether it refers to the "main" CU or the DWO CU.
7692 We can revisit this if the data shows there's a performance issue. */
dee91e82
DE
7693
7694static void
7695init_cutu_and_read_dies_simple (struct dwarf2_per_cu_data *this_cu,
7696 die_reader_func_ftype *die_reader_func,
7697 void *data)
7698{
33e80786 7699 init_cutu_and_read_dies_no_follow (this_cu, NULL, die_reader_func, data);
dee91e82 7700}
0018ea6f
DE
7701\f
7702/* Type Unit Groups.
dee91e82 7703
0018ea6f
DE
7704 Type Unit Groups are a way to collapse the set of all TUs (type units) into
7705 a more manageable set. The grouping is done by DW_AT_stmt_list entry
7706 so that all types coming from the same compilation (.o file) are grouped
7707 together. A future step could be to put the types in the same symtab as
7708 the CU the types ultimately came from. */
ff013f42 7709
f4dc4d17
DE
7710static hashval_t
7711hash_type_unit_group (const void *item)
7712{
9a3c8263
SM
7713 const struct type_unit_group *tu_group
7714 = (const struct type_unit_group *) item;
f4dc4d17 7715
094b34ac 7716 return hash_stmt_list_entry (&tu_group->hash);
f4dc4d17 7717}
348e048f
DE
7718
7719static int
f4dc4d17 7720eq_type_unit_group (const void *item_lhs, const void *item_rhs)
348e048f 7721{
9a3c8263
SM
7722 const struct type_unit_group *lhs = (const struct type_unit_group *) item_lhs;
7723 const struct type_unit_group *rhs = (const struct type_unit_group *) item_rhs;
348e048f 7724
094b34ac 7725 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
f4dc4d17 7726}
348e048f 7727
f4dc4d17
DE
7728/* Allocate a hash table for type unit groups. */
7729
7730static htab_t
ed2dc618 7731allocate_type_unit_groups_table (struct objfile *objfile)
f4dc4d17
DE
7732{
7733 return htab_create_alloc_ex (3,
7734 hash_type_unit_group,
7735 eq_type_unit_group,
7736 NULL,
ed2dc618 7737 &objfile->objfile_obstack,
f4dc4d17
DE
7738 hashtab_obstack_allocate,
7739 dummy_obstack_deallocate);
7740}
dee91e82 7741
f4dc4d17
DE
7742/* Type units that don't have DW_AT_stmt_list are grouped into their own
7743 partial symtabs. We combine several TUs per psymtab to not let the size
7744 of any one psymtab grow too big. */
7745#define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
7746#define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
dee91e82 7747
094b34ac 7748/* Helper routine for get_type_unit_group.
f4dc4d17
DE
7749 Create the type_unit_group object used to hold one or more TUs. */
7750
7751static struct type_unit_group *
094b34ac 7752create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
f4dc4d17 7753{
518817b3
SM
7754 struct dwarf2_per_objfile *dwarf2_per_objfile
7755 = cu->per_cu->dwarf2_per_objfile;
f4dc4d17 7756 struct objfile *objfile = dwarf2_per_objfile->objfile;
094b34ac 7757 struct dwarf2_per_cu_data *per_cu;
f4dc4d17 7758 struct type_unit_group *tu_group;
f4dc4d17
DE
7759
7760 tu_group = OBSTACK_ZALLOC (&objfile->objfile_obstack,
7761 struct type_unit_group);
094b34ac 7762 per_cu = &tu_group->per_cu;
518817b3 7763 per_cu->dwarf2_per_objfile = dwarf2_per_objfile;
f4dc4d17 7764
094b34ac
DE
7765 if (dwarf2_per_objfile->using_index)
7766 {
7767 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
7768 struct dwarf2_per_cu_quick_data);
094b34ac
DE
7769 }
7770 else
7771 {
9c541725 7772 unsigned int line_offset = to_underlying (line_offset_struct);
094b34ac
DE
7773 struct partial_symtab *pst;
7774 char *name;
7775
7776 /* Give the symtab a useful name for debug purposes. */
7777 if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
7778 name = xstrprintf ("<type_units_%d>",
7779 (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
7780 else
7781 name = xstrprintf ("<type_units_at_0x%x>", line_offset);
7782
7783 pst = create_partial_symtab (per_cu, name);
7784 pst->anonymous = 1;
f4dc4d17 7785
094b34ac
DE
7786 xfree (name);
7787 }
f4dc4d17 7788
094b34ac 7789 tu_group->hash.dwo_unit = cu->dwo_unit;
9c541725 7790 tu_group->hash.line_sect_off = line_offset_struct;
f4dc4d17
DE
7791
7792 return tu_group;
7793}
7794
094b34ac
DE
7795/* Look up the type_unit_group for type unit CU, and create it if necessary.
7796 STMT_LIST is a DW_AT_stmt_list attribute. */
f4dc4d17
DE
7797
7798static struct type_unit_group *
ff39bb5e 7799get_type_unit_group (struct dwarf2_cu *cu, const struct attribute *stmt_list)
f4dc4d17 7800{
518817b3
SM
7801 struct dwarf2_per_objfile *dwarf2_per_objfile
7802 = cu->per_cu->dwarf2_per_objfile;
f4dc4d17
DE
7803 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
7804 struct type_unit_group *tu_group;
7805 void **slot;
7806 unsigned int line_offset;
7807 struct type_unit_group type_unit_group_for_lookup;
7808
7809 if (dwarf2_per_objfile->type_unit_groups == NULL)
7810 {
7811 dwarf2_per_objfile->type_unit_groups =
ed2dc618 7812 allocate_type_unit_groups_table (dwarf2_per_objfile->objfile);
f4dc4d17
DE
7813 }
7814
7815 /* Do we need to create a new group, or can we use an existing one? */
7816
7817 if (stmt_list)
7818 {
7819 line_offset = DW_UNSND (stmt_list);
7820 ++tu_stats->nr_symtab_sharers;
7821 }
7822 else
7823 {
7824 /* Ugh, no stmt_list. Rare, but we have to handle it.
7825 We can do various things here like create one group per TU or
7826 spread them over multiple groups to split up the expansion work.
7827 To avoid worst case scenarios (too many groups or too large groups)
7828 we, umm, group them in bunches. */
7829 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
7830 | (tu_stats->nr_stmt_less_type_units
7831 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
7832 ++tu_stats->nr_stmt_less_type_units;
7833 }
7834
094b34ac 7835 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
9c541725 7836 type_unit_group_for_lookup.hash.line_sect_off = (sect_offset) line_offset;
f4dc4d17
DE
7837 slot = htab_find_slot (dwarf2_per_objfile->type_unit_groups,
7838 &type_unit_group_for_lookup, INSERT);
7839 if (*slot != NULL)
7840 {
9a3c8263 7841 tu_group = (struct type_unit_group *) *slot;
f4dc4d17
DE
7842 gdb_assert (tu_group != NULL);
7843 }
7844 else
7845 {
9c541725 7846 sect_offset line_offset_struct = (sect_offset) line_offset;
094b34ac 7847 tu_group = create_type_unit_group (cu, line_offset_struct);
f4dc4d17
DE
7848 *slot = tu_group;
7849 ++tu_stats->nr_symtabs;
7850 }
7851
7852 return tu_group;
7853}
0018ea6f
DE
7854\f
7855/* Partial symbol tables. */
7856
7857/* Create a psymtab named NAME and assign it to PER_CU.
7858
7859 The caller must fill in the following details:
7860 dirname, textlow, texthigh. */
7861
7862static struct partial_symtab *
7863create_partial_symtab (struct dwarf2_per_cu_data *per_cu, const char *name)
7864{
e3b94546 7865 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
0018ea6f
DE
7866 struct partial_symtab *pst;
7867
18a94d75 7868 pst = start_psymtab_common (objfile, name, 0,
af5bf4ad
SM
7869 objfile->global_psymbols,
7870 objfile->static_psymbols);
0018ea6f
DE
7871
7872 pst->psymtabs_addrmap_supported = 1;
7873
7874 /* This is the glue that links PST into GDB's symbol API. */
7875 pst->read_symtab_private = per_cu;
7876 pst->read_symtab = dwarf2_read_symtab;
7877 per_cu->v.psymtab = pst;
7878
7879 return pst;
7880}
7881
b93601f3
TT
7882/* The DATA object passed to process_psymtab_comp_unit_reader has this
7883 type. */
7884
7885struct process_psymtab_comp_unit_data
7886{
7887 /* True if we are reading a DW_TAG_partial_unit. */
7888
7889 int want_partial_unit;
7890
7891 /* The "pretend" language that is used if the CU doesn't declare a
7892 language. */
7893
7894 enum language pretend_language;
7895};
7896
0018ea6f
DE
7897/* die_reader_func for process_psymtab_comp_unit. */
7898
7899static void
7900process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
d521ce57 7901 const gdb_byte *info_ptr,
0018ea6f
DE
7902 struct die_info *comp_unit_die,
7903 int has_children,
7904 void *data)
7905{
7906 struct dwarf2_cu *cu = reader->cu;
518817b3 7907 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
3e29f34a 7908 struct gdbarch *gdbarch = get_objfile_arch (objfile);
0018ea6f 7909 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
0018ea6f
DE
7910 CORE_ADDR baseaddr;
7911 CORE_ADDR best_lowpc = 0, best_highpc = 0;
7912 struct partial_symtab *pst;
3a2b436a 7913 enum pc_bounds_kind cu_bounds_kind;
0018ea6f 7914 const char *filename;
9a3c8263
SM
7915 struct process_psymtab_comp_unit_data *info
7916 = (struct process_psymtab_comp_unit_data *) data;
0018ea6f 7917
b93601f3 7918 if (comp_unit_die->tag == DW_TAG_partial_unit && !info->want_partial_unit)
0018ea6f
DE
7919 return;
7920
7921 gdb_assert (! per_cu->is_debug_types);
7922
b93601f3 7923 prepare_one_comp_unit (cu, comp_unit_die, info->pretend_language);
0018ea6f
DE
7924
7925 cu->list_in_scope = &file_symbols;
7926
7927 /* Allocate a new partial symbol table structure. */
7d45c7c3
KB
7928 filename = dwarf2_string_attr (comp_unit_die, DW_AT_name, cu);
7929 if (filename == NULL)
0018ea6f 7930 filename = "";
0018ea6f
DE
7931
7932 pst = create_partial_symtab (per_cu, filename);
7933
7934 /* This must be done before calling dwarf2_build_include_psymtabs. */
7d45c7c3 7935 pst->dirname = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
0018ea6f
DE
7936
7937 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
7938
7939 dwarf2_find_base_address (comp_unit_die, cu);
7940
7941 /* Possibly set the default values of LOWPC and HIGHPC from
7942 `DW_AT_ranges'. */
3a2b436a
JK
7943 cu_bounds_kind = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
7944 &best_highpc, cu, pst);
7945 if (cu_bounds_kind == PC_BOUNDS_HIGH_LOW && best_lowpc < best_highpc)
0018ea6f
DE
7946 /* Store the contiguous range if it is not empty; it can be empty for
7947 CUs with no code. */
7948 addrmap_set_empty (objfile->psymtabs_addrmap,
3e29f34a
MR
7949 gdbarch_adjust_dwarf2_addr (gdbarch,
7950 best_lowpc + baseaddr),
7951 gdbarch_adjust_dwarf2_addr (gdbarch,
7952 best_highpc + baseaddr) - 1,
7953 pst);
0018ea6f
DE
7954
7955 /* Check if comp unit has_children.
7956 If so, read the rest of the partial symbols from this comp unit.
7957 If not, there's no more debug_info for this comp unit. */
7958 if (has_children)
7959 {
7960 struct partial_die_info *first_die;
7961 CORE_ADDR lowpc, highpc;
7962
7963 lowpc = ((CORE_ADDR) -1);
7964 highpc = ((CORE_ADDR) 0);
7965
7966 first_die = load_partial_dies (reader, info_ptr, 1);
7967
7968 scan_partial_symbols (first_die, &lowpc, &highpc,
e385593e 7969 cu_bounds_kind <= PC_BOUNDS_INVALID, cu);
0018ea6f
DE
7970
7971 /* If we didn't find a lowpc, set it to highpc to avoid
7972 complaints from `maint check'. */
7973 if (lowpc == ((CORE_ADDR) -1))
7974 lowpc = highpc;
7975
7976 /* If the compilation unit didn't have an explicit address range,
7977 then use the information extracted from its child dies. */
e385593e 7978 if (cu_bounds_kind <= PC_BOUNDS_INVALID)
0018ea6f
DE
7979 {
7980 best_lowpc = lowpc;
7981 best_highpc = highpc;
7982 }
7983 }
3e29f34a
MR
7984 pst->textlow = gdbarch_adjust_dwarf2_addr (gdbarch, best_lowpc + baseaddr);
7985 pst->texthigh = gdbarch_adjust_dwarf2_addr (gdbarch, best_highpc + baseaddr);
0018ea6f 7986
8763cede 7987 end_psymtab_common (objfile, pst);
0018ea6f
DE
7988
7989 if (!VEC_empty (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs))
7990 {
7991 int i;
7992 int len = VEC_length (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs);
7993 struct dwarf2_per_cu_data *iter;
7994
7995 /* Fill in 'dependencies' here; we fill in 'users' in a
7996 post-pass. */
7997 pst->number_of_dependencies = len;
8d749320
SM
7998 pst->dependencies =
7999 XOBNEWVEC (&objfile->objfile_obstack, struct partial_symtab *, len);
0018ea6f
DE
8000 for (i = 0;
8001 VEC_iterate (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs,
8002 i, iter);
8003 ++i)
8004 pst->dependencies[i] = iter->v.psymtab;
8005
8006 VEC_free (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs);
8007 }
8008
8009 /* Get the list of files included in the current compilation unit,
8010 and build a psymtab for each of them. */
8011 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
8012
b4f54984 8013 if (dwarf_read_debug)
0018ea6f
DE
8014 {
8015 struct gdbarch *gdbarch = get_objfile_arch (objfile);
8016
8017 fprintf_unfiltered (gdb_stdlog,
9d8780f0 8018 "Psymtab for %s unit @%s: %s - %s"
0018ea6f
DE
8019 ", %d global, %d static syms\n",
8020 per_cu->is_debug_types ? "type" : "comp",
9d8780f0 8021 sect_offset_str (per_cu->sect_off),
0018ea6f
DE
8022 paddress (gdbarch, pst->textlow),
8023 paddress (gdbarch, pst->texthigh),
8024 pst->n_global_syms, pst->n_static_syms);
8025 }
8026}
8027
8028/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
8029 Process compilation unit THIS_CU for a psymtab. */
8030
8031static void
8032process_psymtab_comp_unit (struct dwarf2_per_cu_data *this_cu,
b93601f3
TT
8033 int want_partial_unit,
8034 enum language pretend_language)
0018ea6f
DE
8035{
8036 /* If this compilation unit was already read in, free the
8037 cached copy in order to read it in again. This is
8038 necessary because we skipped some symbols when we first
8039 read in the compilation unit (see load_partial_dies).
8040 This problem could be avoided, but the benefit is unclear. */
8041 if (this_cu->cu != NULL)
8042 free_one_cached_comp_unit (this_cu);
8043
f1902523
JK
8044 if (this_cu->is_debug_types)
8045 init_cutu_and_read_dies (this_cu, NULL, 0, 0, build_type_psymtabs_reader,
8046 NULL);
8047 else
8048 {
8049 process_psymtab_comp_unit_data info;
8050 info.want_partial_unit = want_partial_unit;
8051 info.pretend_language = pretend_language;
8052 init_cutu_and_read_dies (this_cu, NULL, 0, 0,
8053 process_psymtab_comp_unit_reader, &info);
8054 }
0018ea6f
DE
8055
8056 /* Age out any secondary CUs. */
ed2dc618 8057 age_cached_comp_units (this_cu->dwarf2_per_objfile);
0018ea6f 8058}
f4dc4d17
DE
8059
8060/* Reader function for build_type_psymtabs. */
8061
8062static void
8063build_type_psymtabs_reader (const struct die_reader_specs *reader,
d521ce57 8064 const gdb_byte *info_ptr,
f4dc4d17
DE
8065 struct die_info *type_unit_die,
8066 int has_children,
8067 void *data)
8068{
ed2dc618 8069 struct dwarf2_per_objfile *dwarf2_per_objfile
518817b3 8070 = reader->cu->per_cu->dwarf2_per_objfile;
f4dc4d17
DE
8071 struct objfile *objfile = dwarf2_per_objfile->objfile;
8072 struct dwarf2_cu *cu = reader->cu;
8073 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
0186c6a7 8074 struct signatured_type *sig_type;
f4dc4d17
DE
8075 struct type_unit_group *tu_group;
8076 struct attribute *attr;
8077 struct partial_die_info *first_die;
8078 CORE_ADDR lowpc, highpc;
8079 struct partial_symtab *pst;
8080
8081 gdb_assert (data == NULL);
0186c6a7
DE
8082 gdb_assert (per_cu->is_debug_types);
8083 sig_type = (struct signatured_type *) per_cu;
f4dc4d17
DE
8084
8085 if (! has_children)
8086 return;
8087
8088 attr = dwarf2_attr_no_follow (type_unit_die, DW_AT_stmt_list);
094b34ac 8089 tu_group = get_type_unit_group (cu, attr);
f4dc4d17 8090
0186c6a7 8091 VEC_safe_push (sig_type_ptr, tu_group->tus, sig_type);
f4dc4d17
DE
8092
8093 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
8094 cu->list_in_scope = &file_symbols;
8095 pst = create_partial_symtab (per_cu, "");
8096 pst->anonymous = 1;
8097
8098 first_die = load_partial_dies (reader, info_ptr, 1);
8099
8100 lowpc = (CORE_ADDR) -1;
8101 highpc = (CORE_ADDR) 0;
8102 scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
8103
8763cede 8104 end_psymtab_common (objfile, pst);
f4dc4d17
DE
8105}
8106
73051182
DE
8107/* Struct used to sort TUs by their abbreviation table offset. */
8108
8109struct tu_abbrev_offset
8110{
b2bdb8cf
SM
8111 tu_abbrev_offset (signatured_type *sig_type_, sect_offset abbrev_offset_)
8112 : sig_type (sig_type_), abbrev_offset (abbrev_offset_)
8113 {}
8114
8115 signatured_type *sig_type;
73051182
DE
8116 sect_offset abbrev_offset;
8117};
8118
484cf504 8119/* Helper routine for build_type_psymtabs_1, passed to std::sort. */
73051182 8120
484cf504
TT
8121static bool
8122sort_tu_by_abbrev_offset (const struct tu_abbrev_offset &a,
8123 const struct tu_abbrev_offset &b)
73051182 8124{
484cf504 8125 return a.abbrev_offset < b.abbrev_offset;
73051182
DE
8126}
8127
8128/* Efficiently read all the type units.
8129 This does the bulk of the work for build_type_psymtabs.
8130
8131 The efficiency is because we sort TUs by the abbrev table they use and
8132 only read each abbrev table once. In one program there are 200K TUs
8133 sharing 8K abbrev tables.
8134
8135 The main purpose of this function is to support building the
8136 dwarf2_per_objfile->type_unit_groups table.
8137 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
8138 can collapse the search space by grouping them by stmt_list.
8139 The savings can be significant, in the same program from above the 200K TUs
8140 share 8K stmt_list tables.
8141
8142 FUNC is expected to call get_type_unit_group, which will create the
8143 struct type_unit_group if necessary and add it to
8144 dwarf2_per_objfile->type_unit_groups. */
8145
8146static void
ed2dc618 8147build_type_psymtabs_1 (struct dwarf2_per_objfile *dwarf2_per_objfile)
73051182 8148{
73051182 8149 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
685af9cd 8150 abbrev_table_up abbrev_table;
73051182 8151 sect_offset abbrev_offset;
73051182
DE
8152
8153 /* It's up to the caller to not call us multiple times. */
8154 gdb_assert (dwarf2_per_objfile->type_unit_groups == NULL);
8155
b2bdb8cf 8156 if (dwarf2_per_objfile->all_type_units.empty ())
73051182
DE
8157 return;
8158
8159 /* TUs typically share abbrev tables, and there can be way more TUs than
8160 abbrev tables. Sort by abbrev table to reduce the number of times we
8161 read each abbrev table in.
8162 Alternatives are to punt or to maintain a cache of abbrev tables.
8163 This is simpler and efficient enough for now.
8164
8165 Later we group TUs by their DW_AT_stmt_list value (as this defines the
8166 symtab to use). Typically TUs with the same abbrev offset have the same
8167 stmt_list value too so in practice this should work well.
8168
8169 The basic algorithm here is:
8170
8171 sort TUs by abbrev table
8172 for each TU with same abbrev table:
8173 read abbrev table if first user
8174 read TU top level DIE
8175 [IWBN if DWO skeletons had DW_AT_stmt_list]
8176 call FUNC */
8177
b4f54984 8178 if (dwarf_read_debug)
73051182
DE
8179 fprintf_unfiltered (gdb_stdlog, "Building type unit groups ...\n");
8180
8181 /* Sort in a separate table to maintain the order of all_type_units
8182 for .gdb_index: TU indices directly index all_type_units. */
b2bdb8cf
SM
8183 std::vector<tu_abbrev_offset> sorted_by_abbrev;
8184 sorted_by_abbrev.reserve (dwarf2_per_objfile->all_type_units.size ());
8185
8186 for (signatured_type *sig_type : dwarf2_per_objfile->all_type_units)
8187 sorted_by_abbrev.emplace_back
8188 (sig_type, read_abbrev_offset (dwarf2_per_objfile,
8189 sig_type->per_cu.section,
8190 sig_type->per_cu.sect_off));
73051182 8191
484cf504
TT
8192 std::sort (sorted_by_abbrev.begin (), sorted_by_abbrev.end (),
8193 sort_tu_by_abbrev_offset);
73051182 8194
9c541725 8195 abbrev_offset = (sect_offset) ~(unsigned) 0;
73051182 8196
b2bdb8cf 8197 for (const tu_abbrev_offset &tu : sorted_by_abbrev)
73051182 8198 {
73051182
DE
8199 /* Switch to the next abbrev table if necessary. */
8200 if (abbrev_table == NULL
b2bdb8cf 8201 || tu.abbrev_offset != abbrev_offset)
73051182 8202 {
b2bdb8cf 8203 abbrev_offset = tu.abbrev_offset;
73051182 8204 abbrev_table =
ed2dc618
SM
8205 abbrev_table_read_table (dwarf2_per_objfile,
8206 &dwarf2_per_objfile->abbrev,
73051182
DE
8207 abbrev_offset);
8208 ++tu_stats->nr_uniq_abbrev_tables;
8209 }
8210
b2bdb8cf 8211 init_cutu_and_read_dies (&tu.sig_type->per_cu, abbrev_table.get (),
685af9cd 8212 0, 0, build_type_psymtabs_reader, NULL);
73051182 8213 }
6aa5f3a6 8214}
73051182 8215
6aa5f3a6
DE
8216/* Print collected type unit statistics. */
8217
8218static void
ed2dc618 8219print_tu_stats (struct dwarf2_per_objfile *dwarf2_per_objfile)
6aa5f3a6
DE
8220{
8221 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
8222
8223 fprintf_unfiltered (gdb_stdlog, "Type unit statistics:\n");
b2bdb8cf
SM
8224 fprintf_unfiltered (gdb_stdlog, " %zu TUs\n",
8225 dwarf2_per_objfile->all_type_units.size ());
6aa5f3a6
DE
8226 fprintf_unfiltered (gdb_stdlog, " %d uniq abbrev tables\n",
8227 tu_stats->nr_uniq_abbrev_tables);
8228 fprintf_unfiltered (gdb_stdlog, " %d symtabs from stmt_list entries\n",
8229 tu_stats->nr_symtabs);
8230 fprintf_unfiltered (gdb_stdlog, " %d symtab sharers\n",
8231 tu_stats->nr_symtab_sharers);
8232 fprintf_unfiltered (gdb_stdlog, " %d type units without a stmt_list\n",
8233 tu_stats->nr_stmt_less_type_units);
8234 fprintf_unfiltered (gdb_stdlog, " %d all_type_units reallocs\n",
8235 tu_stats->nr_all_type_units_reallocs);
73051182
DE
8236}
8237
f4dc4d17
DE
8238/* Traversal function for build_type_psymtabs. */
8239
8240static int
8241build_type_psymtab_dependencies (void **slot, void *info)
8242{
ed2dc618
SM
8243 struct dwarf2_per_objfile *dwarf2_per_objfile
8244 = (struct dwarf2_per_objfile *) info;
f4dc4d17
DE
8245 struct objfile *objfile = dwarf2_per_objfile->objfile;
8246 struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
094b34ac 8247 struct dwarf2_per_cu_data *per_cu = &tu_group->per_cu;
f4dc4d17 8248 struct partial_symtab *pst = per_cu->v.psymtab;
0186c6a7
DE
8249 int len = VEC_length (sig_type_ptr, tu_group->tus);
8250 struct signatured_type *iter;
f4dc4d17
DE
8251 int i;
8252
8253 gdb_assert (len > 0);
0186c6a7 8254 gdb_assert (IS_TYPE_UNIT_GROUP (per_cu));
f4dc4d17
DE
8255
8256 pst->number_of_dependencies = len;
8d749320
SM
8257 pst->dependencies =
8258 XOBNEWVEC (&objfile->objfile_obstack, struct partial_symtab *, len);
f4dc4d17 8259 for (i = 0;
0186c6a7 8260 VEC_iterate (sig_type_ptr, tu_group->tus, i, iter);
f4dc4d17
DE
8261 ++i)
8262 {
0186c6a7
DE
8263 gdb_assert (iter->per_cu.is_debug_types);
8264 pst->dependencies[i] = iter->per_cu.v.psymtab;
796a7ff8 8265 iter->type_unit_group = tu_group;
f4dc4d17
DE
8266 }
8267
0186c6a7 8268 VEC_free (sig_type_ptr, tu_group->tus);
348e048f
DE
8269
8270 return 1;
8271}
8272
8273/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
8274 Build partial symbol tables for the .debug_types comp-units. */
8275
8276static void
ed2dc618 8277build_type_psymtabs (struct dwarf2_per_objfile *dwarf2_per_objfile)
348e048f 8278{
ed2dc618 8279 if (! create_all_type_units (dwarf2_per_objfile))
348e048f
DE
8280 return;
8281
ed2dc618 8282 build_type_psymtabs_1 (dwarf2_per_objfile);
6aa5f3a6 8283}
f4dc4d17 8284
6aa5f3a6
DE
8285/* Traversal function for process_skeletonless_type_unit.
8286 Read a TU in a DWO file and build partial symbols for it. */
8287
8288static int
8289process_skeletonless_type_unit (void **slot, void *info)
8290{
8291 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
ed2dc618
SM
8292 struct dwarf2_per_objfile *dwarf2_per_objfile
8293 = (struct dwarf2_per_objfile *) info;
6aa5f3a6
DE
8294 struct signatured_type find_entry, *entry;
8295
8296 /* If this TU doesn't exist in the global table, add it and read it in. */
8297
8298 if (dwarf2_per_objfile->signatured_types == NULL)
8299 {
8300 dwarf2_per_objfile->signatured_types
ed2dc618 8301 = allocate_signatured_type_table (dwarf2_per_objfile->objfile);
6aa5f3a6
DE
8302 }
8303
8304 find_entry.signature = dwo_unit->signature;
8305 slot = htab_find_slot (dwarf2_per_objfile->signatured_types, &find_entry,
8306 INSERT);
8307 /* If we've already seen this type there's nothing to do. What's happening
8308 is we're doing our own version of comdat-folding here. */
8309 if (*slot != NULL)
8310 return 1;
8311
8312 /* This does the job that create_all_type_units would have done for
8313 this TU. */
ed2dc618
SM
8314 entry = add_type_unit (dwarf2_per_objfile, dwo_unit->signature, slot);
8315 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, entry, dwo_unit);
6aa5f3a6
DE
8316 *slot = entry;
8317
8318 /* This does the job that build_type_psymtabs_1 would have done. */
8319 init_cutu_and_read_dies (&entry->per_cu, NULL, 0, 0,
8320 build_type_psymtabs_reader, NULL);
8321
8322 return 1;
8323}
8324
8325/* Traversal function for process_skeletonless_type_units. */
8326
8327static int
8328process_dwo_file_for_skeletonless_type_units (void **slot, void *info)
8329{
8330 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
8331
8332 if (dwo_file->tus != NULL)
8333 {
8334 htab_traverse_noresize (dwo_file->tus,
8335 process_skeletonless_type_unit, info);
8336 }
8337
8338 return 1;
8339}
8340
8341/* Scan all TUs of DWO files, verifying we've processed them.
8342 This is needed in case a TU was emitted without its skeleton.
8343 Note: This can't be done until we know what all the DWO files are. */
8344
8345static void
ed2dc618 8346process_skeletonless_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
6aa5f3a6
DE
8347{
8348 /* Skeletonless TUs in DWP files without .gdb_index is not supported yet. */
ed2dc618 8349 if (get_dwp_file (dwarf2_per_objfile) == NULL
6aa5f3a6
DE
8350 && dwarf2_per_objfile->dwo_files != NULL)
8351 {
8352 htab_traverse_noresize (dwarf2_per_objfile->dwo_files,
8353 process_dwo_file_for_skeletonless_type_units,
ed2dc618 8354 dwarf2_per_objfile);
6aa5f3a6 8355 }
348e048f
DE
8356}
8357
ed2dc618 8358/* Compute the 'user' field for each psymtab in DWARF2_PER_OBJFILE. */
95554aad
TT
8359
8360static void
ed2dc618 8361set_partial_user (struct dwarf2_per_objfile *dwarf2_per_objfile)
95554aad 8362{
b76e467d 8363 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
95554aad 8364 {
95554aad 8365 struct partial_symtab *pst = per_cu->v.psymtab;
95554aad 8366
36586728
TT
8367 if (pst == NULL)
8368 continue;
8369
b76e467d 8370 for (int j = 0; j < pst->number_of_dependencies; ++j)
95554aad
TT
8371 {
8372 /* Set the 'user' field only if it is not already set. */
8373 if (pst->dependencies[j]->user == NULL)
8374 pst->dependencies[j]->user = pst;
8375 }
8376 }
8377}
8378
93311388
DE
8379/* Build the partial symbol table by doing a quick pass through the
8380 .debug_info and .debug_abbrev sections. */
72bf9492 8381
93311388 8382static void
ed2dc618 8383dwarf2_build_psymtabs_hard (struct dwarf2_per_objfile *dwarf2_per_objfile)
93311388 8384{
ed2dc618 8385 struct objfile *objfile = dwarf2_per_objfile->objfile;
93311388 8386
b4f54984 8387 if (dwarf_read_debug)
45cfd468
DE
8388 {
8389 fprintf_unfiltered (gdb_stdlog, "Building psymtabs of objfile %s ...\n",
4262abfb 8390 objfile_name (objfile));
45cfd468
DE
8391 }
8392
98bfdba5
PA
8393 dwarf2_per_objfile->reading_partial_symbols = 1;
8394
be391dca 8395 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
91c24f0a 8396
93311388
DE
8397 /* Any cached compilation units will be linked by the per-objfile
8398 read_in_chain. Make sure to free them when we're done. */
11ed8cad 8399 free_cached_comp_units freer (dwarf2_per_objfile);
72bf9492 8400
ed2dc618 8401 build_type_psymtabs (dwarf2_per_objfile);
348e048f 8402
ed2dc618 8403 create_all_comp_units (dwarf2_per_objfile);
c906108c 8404
60606b2c
TT
8405 /* Create a temporary address map on a temporary obstack. We later
8406 copy this to the final obstack. */
8268c778 8407 auto_obstack temp_obstack;
791afaa2
TT
8408
8409 scoped_restore save_psymtabs_addrmap
8410 = make_scoped_restore (&objfile->psymtabs_addrmap,
8411 addrmap_create_mutable (&temp_obstack));
72bf9492 8412
b76e467d
SM
8413 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
8414 process_psymtab_comp_unit (per_cu, 0, language_minimal);
ff013f42 8415
6aa5f3a6 8416 /* This has to wait until we read the CUs, we need the list of DWOs. */
ed2dc618 8417 process_skeletonless_type_units (dwarf2_per_objfile);
6aa5f3a6
DE
8418
8419 /* Now that all TUs have been processed we can fill in the dependencies. */
8420 if (dwarf2_per_objfile->type_unit_groups != NULL)
8421 {
8422 htab_traverse_noresize (dwarf2_per_objfile->type_unit_groups,
ed2dc618 8423 build_type_psymtab_dependencies, dwarf2_per_objfile);
6aa5f3a6
DE
8424 }
8425
b4f54984 8426 if (dwarf_read_debug)
ed2dc618 8427 print_tu_stats (dwarf2_per_objfile);
6aa5f3a6 8428
ed2dc618 8429 set_partial_user (dwarf2_per_objfile);
95554aad 8430
ff013f42
JK
8431 objfile->psymtabs_addrmap = addrmap_create_fixed (objfile->psymtabs_addrmap,
8432 &objfile->objfile_obstack);
791afaa2
TT
8433 /* At this point we want to keep the address map. */
8434 save_psymtabs_addrmap.release ();
ff013f42 8435
b4f54984 8436 if (dwarf_read_debug)
45cfd468 8437 fprintf_unfiltered (gdb_stdlog, "Done building psymtabs of %s\n",
4262abfb 8438 objfile_name (objfile));
ae038cb0
DJ
8439}
8440
3019eac3 8441/* die_reader_func for load_partial_comp_unit. */
ae038cb0
DJ
8442
8443static void
dee91e82 8444load_partial_comp_unit_reader (const struct die_reader_specs *reader,
d521ce57 8445 const gdb_byte *info_ptr,
dee91e82
DE
8446 struct die_info *comp_unit_die,
8447 int has_children,
8448 void *data)
ae038cb0 8449{
dee91e82 8450 struct dwarf2_cu *cu = reader->cu;
ae038cb0 8451
95554aad 8452 prepare_one_comp_unit (cu, comp_unit_die, language_minimal);
ae038cb0 8453
ae038cb0
DJ
8454 /* Check if comp unit has_children.
8455 If so, read the rest of the partial symbols from this comp unit.
0963b4bd 8456 If not, there's no more debug_info for this comp unit. */
d85a05f0 8457 if (has_children)
dee91e82
DE
8458 load_partial_dies (reader, info_ptr, 0);
8459}
98bfdba5 8460
dee91e82
DE
8461/* Load the partial DIEs for a secondary CU into memory.
8462 This is also used when rereading a primary CU with load_all_dies. */
c5b7e1cb 8463
dee91e82
DE
8464static void
8465load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu)
8466{
f4dc4d17
DE
8467 init_cutu_and_read_dies (this_cu, NULL, 1, 1,
8468 load_partial_comp_unit_reader, NULL);
ae038cb0
DJ
8469}
8470
ae038cb0 8471static void
ed2dc618 8472read_comp_units_from_section (struct dwarf2_per_objfile *dwarf2_per_objfile,
36586728 8473 struct dwarf2_section_info *section,
f1902523 8474 struct dwarf2_section_info *abbrev_section,
b76e467d 8475 unsigned int is_dwz)
ae038cb0 8476{
d521ce57 8477 const gdb_byte *info_ptr;
ed2dc618 8478 struct objfile *objfile = dwarf2_per_objfile->objfile;
be391dca 8479
b4f54984 8480 if (dwarf_read_debug)
bf6af496 8481 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s\n",
a32a8923
DE
8482 get_section_name (section),
8483 get_section_file_name (section));
bf6af496 8484
36586728 8485 dwarf2_read_section (objfile, section);
ae038cb0 8486
36586728 8487 info_ptr = section->buffer;
6e70227d 8488
36586728 8489 while (info_ptr < section->buffer + section->size)
ae038cb0 8490 {
ae038cb0 8491 struct dwarf2_per_cu_data *this_cu;
ae038cb0 8492
9c541725 8493 sect_offset sect_off = (sect_offset) (info_ptr - section->buffer);
ae038cb0 8494
f1902523 8495 comp_unit_head cu_header;
ed2dc618
SM
8496 read_and_check_comp_unit_head (dwarf2_per_objfile, &cu_header, section,
8497 abbrev_section, info_ptr,
8498 rcuh_kind::COMPILE);
ae038cb0
DJ
8499
8500 /* Save the compilation unit for later lookup. */
f1902523
JK
8501 if (cu_header.unit_type != DW_UT_type)
8502 {
8503 this_cu = XOBNEW (&objfile->objfile_obstack,
8504 struct dwarf2_per_cu_data);
8505 memset (this_cu, 0, sizeof (*this_cu));
8506 }
8507 else
8508 {
8509 auto sig_type = XOBNEW (&objfile->objfile_obstack,
8510 struct signatured_type);
8511 memset (sig_type, 0, sizeof (*sig_type));
8512 sig_type->signature = cu_header.signature;
8513 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
8514 this_cu = &sig_type->per_cu;
8515 }
8516 this_cu->is_debug_types = (cu_header.unit_type == DW_UT_type);
9c541725 8517 this_cu->sect_off = sect_off;
f1902523 8518 this_cu->length = cu_header.length + cu_header.initial_length_size;
36586728 8519 this_cu->is_dwz = is_dwz;
e3b94546 8520 this_cu->dwarf2_per_objfile = dwarf2_per_objfile;
8a0459fd 8521 this_cu->section = section;
ae038cb0 8522
b76e467d 8523 dwarf2_per_objfile->all_comp_units.push_back (this_cu);
ae038cb0
DJ
8524
8525 info_ptr = info_ptr + this_cu->length;
8526 }
36586728
TT
8527}
8528
8529/* Create a list of all compilation units in OBJFILE.
8530 This is only done for -readnow and building partial symtabs. */
8531
8532static void
ed2dc618 8533create_all_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
36586728 8534{
b76e467d 8535 gdb_assert (dwarf2_per_objfile->all_comp_units.empty ());
ed2dc618 8536 read_comp_units_from_section (dwarf2_per_objfile, &dwarf2_per_objfile->info,
b76e467d 8537 &dwarf2_per_objfile->abbrev, 0);
36586728 8538
b76e467d 8539 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
4db1a1dc 8540 if (dwz != NULL)
ed2dc618 8541 read_comp_units_from_section (dwarf2_per_objfile, &dwz->info, &dwz->abbrev,
b76e467d 8542 1);
c906108c
SS
8543}
8544
5734ee8b 8545/* Process all loaded DIEs for compilation unit CU, starting at
cdc07690 8546 FIRST_DIE. The caller should pass SET_ADDRMAP == 1 if the compilation
5734ee8b 8547 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
cdc07690
YQ
8548 DW_AT_ranges). See the comments of add_partial_subprogram on how
8549 SET_ADDRMAP is used and how *LOWPC and *HIGHPC are updated. */
c906108c 8550
72bf9492
DJ
8551static void
8552scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
cdc07690
YQ
8553 CORE_ADDR *highpc, int set_addrmap,
8554 struct dwarf2_cu *cu)
c906108c 8555{
72bf9492 8556 struct partial_die_info *pdi;
c906108c 8557
91c24f0a
DC
8558 /* Now, march along the PDI's, descending into ones which have
8559 interesting children but skipping the children of the other ones,
8560 until we reach the end of the compilation unit. */
c906108c 8561
72bf9492 8562 pdi = first_die;
91c24f0a 8563
72bf9492
DJ
8564 while (pdi != NULL)
8565 {
52356b79 8566 pdi->fixup (cu);
c906108c 8567
f55ee35c 8568 /* Anonymous namespaces or modules have no name but have interesting
91c24f0a
DC
8569 children, so we need to look at them. Ditto for anonymous
8570 enums. */
933c6fe4 8571
72bf9492 8572 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
95554aad 8573 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
b1dc1806
XR
8574 || pdi->tag == DW_TAG_imported_unit
8575 || pdi->tag == DW_TAG_inlined_subroutine)
c906108c 8576 {
72bf9492 8577 switch (pdi->tag)
c906108c
SS
8578 {
8579 case DW_TAG_subprogram:
b1dc1806 8580 case DW_TAG_inlined_subroutine:
cdc07690 8581 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
c906108c 8582 break;
72929c62 8583 case DW_TAG_constant:
c906108c
SS
8584 case DW_TAG_variable:
8585 case DW_TAG_typedef:
91c24f0a 8586 case DW_TAG_union_type:
72bf9492 8587 if (!pdi->is_declaration)
63d06c5c 8588 {
72bf9492 8589 add_partial_symbol (pdi, cu);
63d06c5c
DC
8590 }
8591 break;
c906108c 8592 case DW_TAG_class_type:
680b30c7 8593 case DW_TAG_interface_type:
c906108c 8594 case DW_TAG_structure_type:
72bf9492 8595 if (!pdi->is_declaration)
c906108c 8596 {
72bf9492 8597 add_partial_symbol (pdi, cu);
c906108c 8598 }
b7fee5a3
KS
8599 if ((cu->language == language_rust
8600 || cu->language == language_cplus) && pdi->has_children)
e98c9e7c
TT
8601 scan_partial_symbols (pdi->die_child, lowpc, highpc,
8602 set_addrmap, cu);
c906108c 8603 break;
91c24f0a 8604 case DW_TAG_enumeration_type:
72bf9492
DJ
8605 if (!pdi->is_declaration)
8606 add_partial_enumeration (pdi, cu);
c906108c
SS
8607 break;
8608 case DW_TAG_base_type:
a02abb62 8609 case DW_TAG_subrange_type:
c906108c 8610 /* File scope base type definitions are added to the partial
c5aa993b 8611 symbol table. */
72bf9492 8612 add_partial_symbol (pdi, cu);
c906108c 8613 break;
d9fa45fe 8614 case DW_TAG_namespace:
cdc07690 8615 add_partial_namespace (pdi, lowpc, highpc, set_addrmap, cu);
91c24f0a 8616 break;
5d7cb8df 8617 case DW_TAG_module:
cdc07690 8618 add_partial_module (pdi, lowpc, highpc, set_addrmap, cu);
5d7cb8df 8619 break;
95554aad
TT
8620 case DW_TAG_imported_unit:
8621 {
8622 struct dwarf2_per_cu_data *per_cu;
8623
f4dc4d17
DE
8624 /* For now we don't handle imported units in type units. */
8625 if (cu->per_cu->is_debug_types)
8626 {
8627 error (_("Dwarf Error: DW_TAG_imported_unit is not"
8628 " supported in type units [in module %s]"),
518817b3 8629 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
f4dc4d17
DE
8630 }
8631
e3b94546
SM
8632 per_cu = dwarf2_find_containing_comp_unit
8633 (pdi->d.sect_off, pdi->is_dwz,
518817b3 8634 cu->per_cu->dwarf2_per_objfile);
95554aad
TT
8635
8636 /* Go read the partial unit, if needed. */
8637 if (per_cu->v.psymtab == NULL)
b93601f3 8638 process_psymtab_comp_unit (per_cu, 1, cu->language);
95554aad 8639
f4dc4d17 8640 VEC_safe_push (dwarf2_per_cu_ptr,
796a7ff8 8641 cu->per_cu->imported_symtabs, per_cu);
95554aad
TT
8642 }
8643 break;
74921315
KS
8644 case DW_TAG_imported_declaration:
8645 add_partial_symbol (pdi, cu);
8646 break;
c906108c
SS
8647 default:
8648 break;
8649 }
8650 }
8651
72bf9492
DJ
8652 /* If the die has a sibling, skip to the sibling. */
8653
8654 pdi = pdi->die_sibling;
8655 }
8656}
8657
8658/* Functions used to compute the fully scoped name of a partial DIE.
91c24f0a 8659
72bf9492 8660 Normally, this is simple. For C++, the parent DIE's fully scoped
9c37b5ae 8661 name is concatenated with "::" and the partial DIE's name.
72bf9492
DJ
8662 Enumerators are an exception; they use the scope of their parent
8663 enumeration type, i.e. the name of the enumeration type is not
8664 prepended to the enumerator.
91c24f0a 8665
72bf9492
DJ
8666 There are two complexities. One is DW_AT_specification; in this
8667 case "parent" means the parent of the target of the specification,
8668 instead of the direct parent of the DIE. The other is compilers
8669 which do not emit DW_TAG_namespace; in this case we try to guess
8670 the fully qualified name of structure types from their members'
8671 linkage names. This must be done using the DIE's children rather
8672 than the children of any DW_AT_specification target. We only need
8673 to do this for structures at the top level, i.e. if the target of
8674 any DW_AT_specification (if any; otherwise the DIE itself) does not
8675 have a parent. */
8676
8677/* Compute the scope prefix associated with PDI's parent, in
8678 compilation unit CU. The result will be allocated on CU's
8679 comp_unit_obstack, or a copy of the already allocated PDI->NAME
8680 field. NULL is returned if no prefix is necessary. */
15d034d0 8681static const char *
72bf9492
DJ
8682partial_die_parent_scope (struct partial_die_info *pdi,
8683 struct dwarf2_cu *cu)
8684{
15d034d0 8685 const char *grandparent_scope;
72bf9492 8686 struct partial_die_info *parent, *real_pdi;
91c24f0a 8687
72bf9492
DJ
8688 /* We need to look at our parent DIE; if we have a DW_AT_specification,
8689 then this means the parent of the specification DIE. */
8690
8691 real_pdi = pdi;
72bf9492 8692 while (real_pdi->has_specification)
36586728
TT
8693 real_pdi = find_partial_die (real_pdi->spec_offset,
8694 real_pdi->spec_is_dwz, cu);
72bf9492
DJ
8695
8696 parent = real_pdi->die_parent;
8697 if (parent == NULL)
8698 return NULL;
8699
8700 if (parent->scope_set)
8701 return parent->scope;
8702
52356b79 8703 parent->fixup (cu);
72bf9492 8704
10b3939b 8705 grandparent_scope = partial_die_parent_scope (parent, cu);
72bf9492 8706
acebe513
UW
8707 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
8708 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
8709 Work around this problem here. */
8710 if (cu->language == language_cplus
6e70227d 8711 && parent->tag == DW_TAG_namespace
acebe513
UW
8712 && strcmp (parent->name, "::") == 0
8713 && grandparent_scope == NULL)
8714 {
8715 parent->scope = NULL;
8716 parent->scope_set = 1;
8717 return NULL;
8718 }
8719
9c6c53f7
SA
8720 if (pdi->tag == DW_TAG_enumerator)
8721 /* Enumerators should not get the name of the enumeration as a prefix. */
8722 parent->scope = grandparent_scope;
8723 else if (parent->tag == DW_TAG_namespace
f55ee35c 8724 || parent->tag == DW_TAG_module
72bf9492
DJ
8725 || parent->tag == DW_TAG_structure_type
8726 || parent->tag == DW_TAG_class_type
680b30c7 8727 || parent->tag == DW_TAG_interface_type
ceeb3d5a
TT
8728 || parent->tag == DW_TAG_union_type
8729 || parent->tag == DW_TAG_enumeration_type)
72bf9492
DJ
8730 {
8731 if (grandparent_scope == NULL)
8732 parent->scope = parent->name;
8733 else
3e43a32a
MS
8734 parent->scope = typename_concat (&cu->comp_unit_obstack,
8735 grandparent_scope,
f55ee35c 8736 parent->name, 0, cu);
72bf9492 8737 }
72bf9492
DJ
8738 else
8739 {
8740 /* FIXME drow/2004-04-01: What should we be doing with
8741 function-local names? For partial symbols, we should probably be
8742 ignoring them. */
8743 complaint (&symfile_complaints,
9d8780f0
SM
8744 _("unhandled containing DIE tag %d for DIE at %s"),
8745 parent->tag, sect_offset_str (pdi->sect_off));
72bf9492 8746 parent->scope = grandparent_scope;
c906108c
SS
8747 }
8748
72bf9492
DJ
8749 parent->scope_set = 1;
8750 return parent->scope;
8751}
8752
8753/* Return the fully scoped name associated with PDI, from compilation unit
8754 CU. The result will be allocated with malloc. */
4568ecf9 8755
72bf9492
DJ
8756static char *
8757partial_die_full_name (struct partial_die_info *pdi,
8758 struct dwarf2_cu *cu)
8759{
15d034d0 8760 const char *parent_scope;
72bf9492 8761
98bfdba5
PA
8762 /* If this is a template instantiation, we can not work out the
8763 template arguments from partial DIEs. So, unfortunately, we have
8764 to go through the full DIEs. At least any work we do building
8765 types here will be reused if full symbols are loaded later. */
8766 if (pdi->has_template_arguments)
8767 {
52356b79 8768 pdi->fixup (cu);
98bfdba5
PA
8769
8770 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
8771 {
8772 struct die_info *die;
8773 struct attribute attr;
8774 struct dwarf2_cu *ref_cu = cu;
8775
b64f50a1 8776 /* DW_FORM_ref_addr is using section offset. */
b4069958 8777 attr.name = (enum dwarf_attribute) 0;
98bfdba5 8778 attr.form = DW_FORM_ref_addr;
9c541725 8779 attr.u.unsnd = to_underlying (pdi->sect_off);
98bfdba5
PA
8780 die = follow_die_ref (NULL, &attr, &ref_cu);
8781
8782 return xstrdup (dwarf2_full_name (NULL, die, ref_cu));
8783 }
8784 }
8785
72bf9492
DJ
8786 parent_scope = partial_die_parent_scope (pdi, cu);
8787 if (parent_scope == NULL)
8788 return NULL;
8789 else
f55ee35c 8790 return typename_concat (NULL, parent_scope, pdi->name, 0, cu);
c906108c
SS
8791}
8792
8793static void
72bf9492 8794add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
c906108c 8795{
518817b3
SM
8796 struct dwarf2_per_objfile *dwarf2_per_objfile
8797 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 8798 struct objfile *objfile = dwarf2_per_objfile->objfile;
3e29f34a 8799 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c 8800 CORE_ADDR addr = 0;
15d034d0 8801 const char *actual_name = NULL;
e142c38c 8802 CORE_ADDR baseaddr;
15d034d0 8803 char *built_actual_name;
e142c38c
DJ
8804
8805 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 8806
15d034d0
TT
8807 built_actual_name = partial_die_full_name (pdi, cu);
8808 if (built_actual_name != NULL)
8809 actual_name = built_actual_name;
63d06c5c 8810
72bf9492
DJ
8811 if (actual_name == NULL)
8812 actual_name = pdi->name;
8813
c906108c
SS
8814 switch (pdi->tag)
8815 {
b1dc1806 8816 case DW_TAG_inlined_subroutine:
c906108c 8817 case DW_TAG_subprogram:
3e29f34a 8818 addr = gdbarch_adjust_dwarf2_addr (gdbarch, pdi->lowpc + baseaddr);
2cfa0c8d 8819 if (pdi->is_external || cu->language == language_ada)
c906108c 8820 {
2cfa0c8d
JB
8821 /* brobecker/2007-12-26: Normally, only "external" DIEs are part
8822 of the global scope. But in Ada, we want to be able to access
8823 nested procedures globally. So all Ada subprograms are stored
8824 in the global scope. */
f47fb265 8825 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 8826 built_actual_name != NULL,
f47fb265
MS
8827 VAR_DOMAIN, LOC_BLOCK,
8828 &objfile->global_psymbols,
1762568f 8829 addr, cu->language, objfile);
c906108c
SS
8830 }
8831 else
8832 {
f47fb265 8833 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 8834 built_actual_name != NULL,
f47fb265
MS
8835 VAR_DOMAIN, LOC_BLOCK,
8836 &objfile->static_psymbols,
1762568f 8837 addr, cu->language, objfile);
c906108c 8838 }
0c1b455e
TT
8839
8840 if (pdi->main_subprogram && actual_name != NULL)
8841 set_objfile_main_name (objfile, actual_name, cu->language);
c906108c 8842 break;
72929c62
JB
8843 case DW_TAG_constant:
8844 {
af5bf4ad 8845 std::vector<partial_symbol *> *list;
72929c62
JB
8846
8847 if (pdi->is_external)
8848 list = &objfile->global_psymbols;
8849 else
8850 list = &objfile->static_psymbols;
f47fb265 8851 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 8852 built_actual_name != NULL, VAR_DOMAIN, LOC_STATIC,
1762568f 8853 list, 0, cu->language, objfile);
72929c62
JB
8854 }
8855 break;
c906108c 8856 case DW_TAG_variable:
95554aad
TT
8857 if (pdi->d.locdesc)
8858 addr = decode_locdesc (pdi->d.locdesc, cu);
caac4577 8859
95554aad 8860 if (pdi->d.locdesc
caac4577
JG
8861 && addr == 0
8862 && !dwarf2_per_objfile->has_section_at_zero)
8863 {
8864 /* A global or static variable may also have been stripped
8865 out by the linker if unused, in which case its address
8866 will be nullified; do not add such variables into partial
8867 symbol table then. */
8868 }
8869 else if (pdi->is_external)
c906108c
SS
8870 {
8871 /* Global Variable.
8872 Don't enter into the minimal symbol tables as there is
8873 a minimal symbol table entry from the ELF symbols already.
8874 Enter into partial symbol table if it has a location
8875 descriptor or a type.
8876 If the location descriptor is missing, new_symbol will create
8877 a LOC_UNRESOLVED symbol, the address of the variable will then
8878 be determined from the minimal symbol table whenever the variable
8879 is referenced.
8880 The address for the partial symbol table entry is not
8881 used by GDB, but it comes in handy for debugging partial symbol
8882 table building. */
8883
95554aad 8884 if (pdi->d.locdesc || pdi->has_type)
f47fb265 8885 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 8886 built_actual_name != NULL,
f47fb265
MS
8887 VAR_DOMAIN, LOC_STATIC,
8888 &objfile->global_psymbols,
1762568f 8889 addr + baseaddr,
f47fb265 8890 cu->language, objfile);
c906108c
SS
8891 }
8892 else
8893 {
ff908ebf
AW
8894 int has_loc = pdi->d.locdesc != NULL;
8895
8896 /* Static Variable. Skip symbols whose value we cannot know (those
8897 without location descriptors or constant values). */
8898 if (!has_loc && !pdi->has_const_value)
decbce07 8899 {
15d034d0 8900 xfree (built_actual_name);
decbce07
MS
8901 return;
8902 }
ff908ebf 8903
f47fb265 8904 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 8905 built_actual_name != NULL,
f47fb265
MS
8906 VAR_DOMAIN, LOC_STATIC,
8907 &objfile->static_psymbols,
ff908ebf 8908 has_loc ? addr + baseaddr : (CORE_ADDR) 0,
f47fb265 8909 cu->language, objfile);
c906108c
SS
8910 }
8911 break;
8912 case DW_TAG_typedef:
8913 case DW_TAG_base_type:
a02abb62 8914 case DW_TAG_subrange_type:
38d518c9 8915 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 8916 built_actual_name != NULL,
176620f1 8917 VAR_DOMAIN, LOC_TYPEDEF,
c906108c 8918 &objfile->static_psymbols,
1762568f 8919 0, cu->language, objfile);
c906108c 8920 break;
74921315 8921 case DW_TAG_imported_declaration:
72bf9492
DJ
8922 case DW_TAG_namespace:
8923 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 8924 built_actual_name != NULL,
72bf9492
DJ
8925 VAR_DOMAIN, LOC_TYPEDEF,
8926 &objfile->global_psymbols,
1762568f 8927 0, cu->language, objfile);
72bf9492 8928 break;
530e8392
KB
8929 case DW_TAG_module:
8930 add_psymbol_to_list (actual_name, strlen (actual_name),
8931 built_actual_name != NULL,
8932 MODULE_DOMAIN, LOC_TYPEDEF,
8933 &objfile->global_psymbols,
1762568f 8934 0, cu->language, objfile);
530e8392 8935 break;
c906108c 8936 case DW_TAG_class_type:
680b30c7 8937 case DW_TAG_interface_type:
c906108c
SS
8938 case DW_TAG_structure_type:
8939 case DW_TAG_union_type:
8940 case DW_TAG_enumeration_type:
fa4028e9
JB
8941 /* Skip external references. The DWARF standard says in the section
8942 about "Structure, Union, and Class Type Entries": "An incomplete
8943 structure, union or class type is represented by a structure,
8944 union or class entry that does not have a byte size attribute
8945 and that has a DW_AT_declaration attribute." */
8946 if (!pdi->has_byte_size && pdi->is_declaration)
decbce07 8947 {
15d034d0 8948 xfree (built_actual_name);
decbce07
MS
8949 return;
8950 }
fa4028e9 8951
63d06c5c
DC
8952 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
8953 static vs. global. */
38d518c9 8954 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 8955 built_actual_name != NULL,
176620f1 8956 STRUCT_DOMAIN, LOC_TYPEDEF,
9c37b5ae 8957 cu->language == language_cplus
63d06c5c
DC
8958 ? &objfile->global_psymbols
8959 : &objfile->static_psymbols,
1762568f 8960 0, cu->language, objfile);
c906108c 8961
c906108c
SS
8962 break;
8963 case DW_TAG_enumerator:
38d518c9 8964 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 8965 built_actual_name != NULL,
176620f1 8966 VAR_DOMAIN, LOC_CONST,
9c37b5ae 8967 cu->language == language_cplus
f6fe98ef
DJ
8968 ? &objfile->global_psymbols
8969 : &objfile->static_psymbols,
1762568f 8970 0, cu->language, objfile);
c906108c
SS
8971 break;
8972 default:
8973 break;
8974 }
5c4e30ca 8975
15d034d0 8976 xfree (built_actual_name);
c906108c
SS
8977}
8978
5c4e30ca
DC
8979/* Read a partial die corresponding to a namespace; also, add a symbol
8980 corresponding to that namespace to the symbol table. NAMESPACE is
8981 the name of the enclosing namespace. */
91c24f0a 8982
72bf9492
DJ
8983static void
8984add_partial_namespace (struct partial_die_info *pdi,
91c24f0a 8985 CORE_ADDR *lowpc, CORE_ADDR *highpc,
cdc07690 8986 int set_addrmap, struct dwarf2_cu *cu)
91c24f0a 8987{
72bf9492 8988 /* Add a symbol for the namespace. */
e7c27a73 8989
72bf9492 8990 add_partial_symbol (pdi, cu);
5c4e30ca
DC
8991
8992 /* Now scan partial symbols in that namespace. */
8993
91c24f0a 8994 if (pdi->has_children)
cdc07690 8995 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
91c24f0a
DC
8996}
8997
5d7cb8df
JK
8998/* Read a partial die corresponding to a Fortran module. */
8999
9000static void
9001add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
cdc07690 9002 CORE_ADDR *highpc, int set_addrmap, struct dwarf2_cu *cu)
5d7cb8df 9003{
530e8392
KB
9004 /* Add a symbol for the namespace. */
9005
9006 add_partial_symbol (pdi, cu);
9007
f55ee35c 9008 /* Now scan partial symbols in that module. */
5d7cb8df
JK
9009
9010 if (pdi->has_children)
cdc07690 9011 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
5d7cb8df
JK
9012}
9013
b1dc1806
XR
9014/* Read a partial die corresponding to a subprogram or an inlined
9015 subprogram and create a partial symbol for that subprogram.
9016 When the CU language allows it, this routine also defines a partial
9017 symbol for each nested subprogram that this subprogram contains.
9018 If SET_ADDRMAP is true, record the covered ranges in the addrmap.
9019 Set *LOWPC and *HIGHPC to the lowest and highest PC values found in PDI.
6e70227d 9020
cdc07690
YQ
9021 PDI may also be a lexical block, in which case we simply search
9022 recursively for subprograms defined inside that lexical block.
bc30ff58
JB
9023 Again, this is only performed when the CU language allows this
9024 type of definitions. */
9025
9026static void
9027add_partial_subprogram (struct partial_die_info *pdi,
9028 CORE_ADDR *lowpc, CORE_ADDR *highpc,
cdc07690 9029 int set_addrmap, struct dwarf2_cu *cu)
bc30ff58 9030{
b1dc1806 9031 if (pdi->tag == DW_TAG_subprogram || pdi->tag == DW_TAG_inlined_subroutine)
bc30ff58
JB
9032 {
9033 if (pdi->has_pc_info)
9034 {
9035 if (pdi->lowpc < *lowpc)
9036 *lowpc = pdi->lowpc;
9037 if (pdi->highpc > *highpc)
9038 *highpc = pdi->highpc;
cdc07690 9039 if (set_addrmap)
5734ee8b 9040 {
518817b3 9041 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
3e29f34a
MR
9042 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9043 CORE_ADDR baseaddr;
9044 CORE_ADDR highpc;
9045 CORE_ADDR lowpc;
5734ee8b
DJ
9046
9047 baseaddr = ANOFFSET (objfile->section_offsets,
9048 SECT_OFF_TEXT (objfile));
3e29f34a
MR
9049 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch,
9050 pdi->lowpc + baseaddr);
9051 highpc = gdbarch_adjust_dwarf2_addr (gdbarch,
9052 pdi->highpc + baseaddr);
9053 addrmap_set_empty (objfile->psymtabs_addrmap, lowpc, highpc - 1,
9291a0cd 9054 cu->per_cu->v.psymtab);
5734ee8b 9055 }
481860b3
GB
9056 }
9057
9058 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
9059 {
bc30ff58 9060 if (!pdi->is_declaration)
e8d05480
JB
9061 /* Ignore subprogram DIEs that do not have a name, they are
9062 illegal. Do not emit a complaint at this point, we will
9063 do so when we convert this psymtab into a symtab. */
9064 if (pdi->name)
9065 add_partial_symbol (pdi, cu);
bc30ff58
JB
9066 }
9067 }
6e70227d 9068
bc30ff58
JB
9069 if (! pdi->has_children)
9070 return;
9071
9072 if (cu->language == language_ada)
9073 {
9074 pdi = pdi->die_child;
9075 while (pdi != NULL)
9076 {
52356b79 9077 pdi->fixup (cu);
bc30ff58 9078 if (pdi->tag == DW_TAG_subprogram
b1dc1806 9079 || pdi->tag == DW_TAG_inlined_subroutine
bc30ff58 9080 || pdi->tag == DW_TAG_lexical_block)
cdc07690 9081 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
bc30ff58
JB
9082 pdi = pdi->die_sibling;
9083 }
9084 }
9085}
9086
91c24f0a
DC
9087/* Read a partial die corresponding to an enumeration type. */
9088
72bf9492
DJ
9089static void
9090add_partial_enumeration (struct partial_die_info *enum_pdi,
9091 struct dwarf2_cu *cu)
91c24f0a 9092{
72bf9492 9093 struct partial_die_info *pdi;
91c24f0a
DC
9094
9095 if (enum_pdi->name != NULL)
72bf9492
DJ
9096 add_partial_symbol (enum_pdi, cu);
9097
9098 pdi = enum_pdi->die_child;
9099 while (pdi)
91c24f0a 9100 {
72bf9492 9101 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
e2e0b3e5 9102 complaint (&symfile_complaints, _("malformed enumerator DIE ignored"));
91c24f0a 9103 else
72bf9492
DJ
9104 add_partial_symbol (pdi, cu);
9105 pdi = pdi->die_sibling;
91c24f0a 9106 }
91c24f0a
DC
9107}
9108
6caca83c
CC
9109/* Return the initial uleb128 in the die at INFO_PTR. */
9110
9111static unsigned int
d521ce57 9112peek_abbrev_code (bfd *abfd, const gdb_byte *info_ptr)
6caca83c
CC
9113{
9114 unsigned int bytes_read;
9115
9116 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9117}
9118
685af9cd
TT
9119/* Read the initial uleb128 in the die at INFO_PTR in compilation unit
9120 READER::CU. Use READER::ABBREV_TABLE to lookup any abbreviation.
9121
4bb7a0a7
DJ
9122 Return the corresponding abbrev, or NULL if the number is zero (indicating
9123 an empty DIE). In either case *BYTES_READ will be set to the length of
9124 the initial number. */
9125
9126static struct abbrev_info *
685af9cd
TT
9127peek_die_abbrev (const die_reader_specs &reader,
9128 const gdb_byte *info_ptr, unsigned int *bytes_read)
4bb7a0a7 9129{
685af9cd 9130 dwarf2_cu *cu = reader.cu;
518817b3 9131 bfd *abfd = cu->per_cu->dwarf2_per_objfile->objfile->obfd;
685af9cd
TT
9132 unsigned int abbrev_number
9133 = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
4bb7a0a7
DJ
9134
9135 if (abbrev_number == 0)
9136 return NULL;
9137
685af9cd 9138 abbrev_info *abbrev = reader.abbrev_table->lookup_abbrev (abbrev_number);
4bb7a0a7
DJ
9139 if (!abbrev)
9140 {
422b9917 9141 error (_("Dwarf Error: Could not find abbrev number %d in %s"
9d8780f0 9142 " at offset %s [in module %s]"),
422b9917 9143 abbrev_number, cu->per_cu->is_debug_types ? "TU" : "CU",
9d8780f0 9144 sect_offset_str (cu->header.sect_off), bfd_get_filename (abfd));
4bb7a0a7
DJ
9145 }
9146
9147 return abbrev;
9148}
9149
93311388
DE
9150/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
9151 Returns a pointer to the end of a series of DIEs, terminated by an empty
4bb7a0a7
DJ
9152 DIE. Any children of the skipped DIEs will also be skipped. */
9153
d521ce57
TT
9154static const gdb_byte *
9155skip_children (const struct die_reader_specs *reader, const gdb_byte *info_ptr)
4bb7a0a7 9156{
4bb7a0a7
DJ
9157 while (1)
9158 {
685af9cd
TT
9159 unsigned int bytes_read;
9160 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
9161
4bb7a0a7
DJ
9162 if (abbrev == NULL)
9163 return info_ptr + bytes_read;
9164 else
dee91e82 9165 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
4bb7a0a7
DJ
9166 }
9167}
9168
93311388
DE
9169/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
9170 INFO_PTR should point just after the initial uleb128 of a DIE, and the
4bb7a0a7
DJ
9171 abbrev corresponding to that skipped uleb128 should be passed in
9172 ABBREV. Returns a pointer to this DIE's sibling, skipping any
9173 children. */
9174
d521ce57
TT
9175static const gdb_byte *
9176skip_one_die (const struct die_reader_specs *reader, const gdb_byte *info_ptr,
dee91e82 9177 struct abbrev_info *abbrev)
4bb7a0a7
DJ
9178{
9179 unsigned int bytes_read;
9180 struct attribute attr;
dee91e82
DE
9181 bfd *abfd = reader->abfd;
9182 struct dwarf2_cu *cu = reader->cu;
d521ce57 9183 const gdb_byte *buffer = reader->buffer;
f664829e 9184 const gdb_byte *buffer_end = reader->buffer_end;
4bb7a0a7
DJ
9185 unsigned int form, i;
9186
9187 for (i = 0; i < abbrev->num_attrs; i++)
9188 {
9189 /* The only abbrev we care about is DW_AT_sibling. */
9190 if (abbrev->attrs[i].name == DW_AT_sibling)
9191 {
dee91e82 9192 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
4bb7a0a7 9193 if (attr.form == DW_FORM_ref_addr)
3e43a32a
MS
9194 complaint (&symfile_complaints,
9195 _("ignoring absolute DW_AT_sibling"));
4bb7a0a7 9196 else
b9502d3f 9197 {
9c541725
PA
9198 sect_offset off = dwarf2_get_ref_die_offset (&attr);
9199 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
b9502d3f
WN
9200
9201 if (sibling_ptr < info_ptr)
9202 complaint (&symfile_complaints,
9203 _("DW_AT_sibling points backwards"));
22869d73
KS
9204 else if (sibling_ptr > reader->buffer_end)
9205 dwarf2_section_buffer_overflow_complaint (reader->die_section);
b9502d3f
WN
9206 else
9207 return sibling_ptr;
9208 }
4bb7a0a7
DJ
9209 }
9210
9211 /* If it isn't DW_AT_sibling, skip this attribute. */
9212 form = abbrev->attrs[i].form;
9213 skip_attribute:
9214 switch (form)
9215 {
4bb7a0a7 9216 case DW_FORM_ref_addr:
ae411497
TT
9217 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
9218 and later it is offset sized. */
9219 if (cu->header.version == 2)
9220 info_ptr += cu->header.addr_size;
9221 else
9222 info_ptr += cu->header.offset_size;
9223 break;
36586728
TT
9224 case DW_FORM_GNU_ref_alt:
9225 info_ptr += cu->header.offset_size;
9226 break;
ae411497 9227 case DW_FORM_addr:
4bb7a0a7
DJ
9228 info_ptr += cu->header.addr_size;
9229 break;
9230 case DW_FORM_data1:
9231 case DW_FORM_ref1:
9232 case DW_FORM_flag:
9233 info_ptr += 1;
9234 break;
2dc7f7b3 9235 case DW_FORM_flag_present:
43988095 9236 case DW_FORM_implicit_const:
2dc7f7b3 9237 break;
4bb7a0a7
DJ
9238 case DW_FORM_data2:
9239 case DW_FORM_ref2:
9240 info_ptr += 2;
9241 break;
9242 case DW_FORM_data4:
9243 case DW_FORM_ref4:
9244 info_ptr += 4;
9245 break;
9246 case DW_FORM_data8:
9247 case DW_FORM_ref8:
55f1336d 9248 case DW_FORM_ref_sig8:
4bb7a0a7
DJ
9249 info_ptr += 8;
9250 break;
0224619f
JK
9251 case DW_FORM_data16:
9252 info_ptr += 16;
9253 break;
4bb7a0a7 9254 case DW_FORM_string:
9b1c24c8 9255 read_direct_string (abfd, info_ptr, &bytes_read);
4bb7a0a7
DJ
9256 info_ptr += bytes_read;
9257 break;
2dc7f7b3 9258 case DW_FORM_sec_offset:
4bb7a0a7 9259 case DW_FORM_strp:
36586728 9260 case DW_FORM_GNU_strp_alt:
4bb7a0a7
DJ
9261 info_ptr += cu->header.offset_size;
9262 break;
2dc7f7b3 9263 case DW_FORM_exprloc:
4bb7a0a7
DJ
9264 case DW_FORM_block:
9265 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9266 info_ptr += bytes_read;
9267 break;
9268 case DW_FORM_block1:
9269 info_ptr += 1 + read_1_byte (abfd, info_ptr);
9270 break;
9271 case DW_FORM_block2:
9272 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
9273 break;
9274 case DW_FORM_block4:
9275 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
9276 break;
9277 case DW_FORM_sdata:
9278 case DW_FORM_udata:
9279 case DW_FORM_ref_udata:
3019eac3
DE
9280 case DW_FORM_GNU_addr_index:
9281 case DW_FORM_GNU_str_index:
d521ce57 9282 info_ptr = safe_skip_leb128 (info_ptr, buffer_end);
4bb7a0a7
DJ
9283 break;
9284 case DW_FORM_indirect:
9285 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9286 info_ptr += bytes_read;
9287 /* We need to continue parsing from here, so just go back to
9288 the top. */
9289 goto skip_attribute;
9290
9291 default:
3e43a32a
MS
9292 error (_("Dwarf Error: Cannot handle %s "
9293 "in DWARF reader [in module %s]"),
4bb7a0a7
DJ
9294 dwarf_form_name (form),
9295 bfd_get_filename (abfd));
9296 }
9297 }
9298
9299 if (abbrev->has_children)
dee91e82 9300 return skip_children (reader, info_ptr);
4bb7a0a7
DJ
9301 else
9302 return info_ptr;
9303}
9304
93311388 9305/* Locate ORIG_PDI's sibling.
dee91e82 9306 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
91c24f0a 9307
d521ce57 9308static const gdb_byte *
dee91e82
DE
9309locate_pdi_sibling (const struct die_reader_specs *reader,
9310 struct partial_die_info *orig_pdi,
d521ce57 9311 const gdb_byte *info_ptr)
91c24f0a
DC
9312{
9313 /* Do we know the sibling already? */
72bf9492 9314
91c24f0a
DC
9315 if (orig_pdi->sibling)
9316 return orig_pdi->sibling;
9317
9318 /* Are there any children to deal with? */
9319
9320 if (!orig_pdi->has_children)
9321 return info_ptr;
9322
4bb7a0a7 9323 /* Skip the children the long way. */
91c24f0a 9324
dee91e82 9325 return skip_children (reader, info_ptr);
91c24f0a
DC
9326}
9327
257e7a09 9328/* Expand this partial symbol table into a full symbol table. SELF is
442e4d9c 9329 not NULL. */
c906108c
SS
9330
9331static void
257e7a09
YQ
9332dwarf2_read_symtab (struct partial_symtab *self,
9333 struct objfile *objfile)
c906108c 9334{
ed2dc618
SM
9335 struct dwarf2_per_objfile *dwarf2_per_objfile
9336 = get_dwarf2_per_objfile (objfile);
9337
257e7a09 9338 if (self->readin)
c906108c 9339 {
442e4d9c 9340 warning (_("bug: psymtab for %s is already read in."),
257e7a09 9341 self->filename);
442e4d9c
YQ
9342 }
9343 else
9344 {
9345 if (info_verbose)
c906108c 9346 {
442e4d9c 9347 printf_filtered (_("Reading in symbols for %s..."),
257e7a09 9348 self->filename);
442e4d9c 9349 gdb_flush (gdb_stdout);
c906108c 9350 }
c906108c 9351
442e4d9c
YQ
9352 /* If this psymtab is constructed from a debug-only objfile, the
9353 has_section_at_zero flag will not necessarily be correct. We
9354 can get the correct value for this flag by looking at the data
9355 associated with the (presumably stripped) associated objfile. */
9356 if (objfile->separate_debug_objfile_backlink)
9357 {
9358 struct dwarf2_per_objfile *dpo_backlink
ed2dc618 9359 = get_dwarf2_per_objfile (objfile->separate_debug_objfile_backlink);
9a619af0 9360
442e4d9c
YQ
9361 dwarf2_per_objfile->has_section_at_zero
9362 = dpo_backlink->has_section_at_zero;
9363 }
b2ab525c 9364
442e4d9c 9365 dwarf2_per_objfile->reading_partial_symbols = 0;
98bfdba5 9366
257e7a09 9367 psymtab_to_symtab_1 (self);
c906108c 9368
442e4d9c
YQ
9369 /* Finish up the debug error message. */
9370 if (info_verbose)
9371 printf_filtered (_("done.\n"));
c906108c 9372 }
95554aad 9373
ed2dc618 9374 process_cu_includes (dwarf2_per_objfile);
c906108c 9375}
9cdd5dbd
DE
9376\f
9377/* Reading in full CUs. */
c906108c 9378
10b3939b
DJ
9379/* Add PER_CU to the queue. */
9380
9381static void
95554aad
TT
9382queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
9383 enum language pretend_language)
10b3939b
DJ
9384{
9385 struct dwarf2_queue_item *item;
9386
9387 per_cu->queued = 1;
8d749320 9388 item = XNEW (struct dwarf2_queue_item);
10b3939b 9389 item->per_cu = per_cu;
95554aad 9390 item->pretend_language = pretend_language;
10b3939b
DJ
9391 item->next = NULL;
9392
9393 if (dwarf2_queue == NULL)
9394 dwarf2_queue = item;
9395 else
9396 dwarf2_queue_tail->next = item;
9397
9398 dwarf2_queue_tail = item;
9399}
9400
89e63ee4
DE
9401/* If PER_CU is not yet queued, add it to the queue.
9402 If DEPENDENT_CU is non-NULL, it has a reference to PER_CU so add a
9403 dependency.
0907af0c 9404 The result is non-zero if PER_CU was queued, otherwise the result is zero
69d751e3
DE
9405 meaning either PER_CU is already queued or it is already loaded.
9406
9407 N.B. There is an invariant here that if a CU is queued then it is loaded.
9408 The caller is required to load PER_CU if we return non-zero. */
0907af0c
DE
9409
9410static int
89e63ee4 9411maybe_queue_comp_unit (struct dwarf2_cu *dependent_cu,
0907af0c
DE
9412 struct dwarf2_per_cu_data *per_cu,
9413 enum language pretend_language)
9414{
9415 /* We may arrive here during partial symbol reading, if we need full
9416 DIEs to process an unusual case (e.g. template arguments). Do
9417 not queue PER_CU, just tell our caller to load its DIEs. */
ed2dc618 9418 if (per_cu->dwarf2_per_objfile->reading_partial_symbols)
0907af0c
DE
9419 {
9420 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
9421 return 1;
9422 return 0;
9423 }
9424
9425 /* Mark the dependence relation so that we don't flush PER_CU
9426 too early. */
89e63ee4
DE
9427 if (dependent_cu != NULL)
9428 dwarf2_add_dependence (dependent_cu, per_cu);
0907af0c
DE
9429
9430 /* If it's already on the queue, we have nothing to do. */
9431 if (per_cu->queued)
9432 return 0;
9433
9434 /* If the compilation unit is already loaded, just mark it as
9435 used. */
9436 if (per_cu->cu != NULL)
9437 {
9438 per_cu->cu->last_used = 0;
9439 return 0;
9440 }
9441
9442 /* Add it to the queue. */
9443 queue_comp_unit (per_cu, pretend_language);
9444
9445 return 1;
9446}
9447
10b3939b
DJ
9448/* Process the queue. */
9449
9450static void
ed2dc618 9451process_queue (struct dwarf2_per_objfile *dwarf2_per_objfile)
10b3939b
DJ
9452{
9453 struct dwarf2_queue_item *item, *next_item;
9454
b4f54984 9455 if (dwarf_read_debug)
45cfd468
DE
9456 {
9457 fprintf_unfiltered (gdb_stdlog,
9458 "Expanding one or more symtabs of objfile %s ...\n",
4262abfb 9459 objfile_name (dwarf2_per_objfile->objfile));
45cfd468
DE
9460 }
9461
03dd20cc
DJ
9462 /* The queue starts out with one item, but following a DIE reference
9463 may load a new CU, adding it to the end of the queue. */
10b3939b
DJ
9464 for (item = dwarf2_queue; item != NULL; dwarf2_queue = item = next_item)
9465 {
cc12ce38
DE
9466 if ((dwarf2_per_objfile->using_index
9467 ? !item->per_cu->v.quick->compunit_symtab
9468 : (item->per_cu->v.psymtab && !item->per_cu->v.psymtab->readin))
9469 /* Skip dummy CUs. */
9470 && item->per_cu->cu != NULL)
f4dc4d17
DE
9471 {
9472 struct dwarf2_per_cu_data *per_cu = item->per_cu;
73be47f5 9473 unsigned int debug_print_threshold;
247f5c4f 9474 char buf[100];
f4dc4d17 9475
247f5c4f 9476 if (per_cu->is_debug_types)
f4dc4d17 9477 {
247f5c4f
DE
9478 struct signatured_type *sig_type =
9479 (struct signatured_type *) per_cu;
9480
9d8780f0 9481 sprintf (buf, "TU %s at offset %s",
73be47f5 9482 hex_string (sig_type->signature),
9d8780f0 9483 sect_offset_str (per_cu->sect_off));
73be47f5
DE
9484 /* There can be 100s of TUs.
9485 Only print them in verbose mode. */
9486 debug_print_threshold = 2;
f4dc4d17 9487 }
247f5c4f 9488 else
73be47f5 9489 {
9d8780f0
SM
9490 sprintf (buf, "CU at offset %s",
9491 sect_offset_str (per_cu->sect_off));
73be47f5
DE
9492 debug_print_threshold = 1;
9493 }
247f5c4f 9494
b4f54984 9495 if (dwarf_read_debug >= debug_print_threshold)
247f5c4f 9496 fprintf_unfiltered (gdb_stdlog, "Expanding symtab of %s\n", buf);
f4dc4d17
DE
9497
9498 if (per_cu->is_debug_types)
9499 process_full_type_unit (per_cu, item->pretend_language);
9500 else
9501 process_full_comp_unit (per_cu, item->pretend_language);
9502
b4f54984 9503 if (dwarf_read_debug >= debug_print_threshold)
247f5c4f 9504 fprintf_unfiltered (gdb_stdlog, "Done expanding %s\n", buf);
f4dc4d17 9505 }
10b3939b
DJ
9506
9507 item->per_cu->queued = 0;
9508 next_item = item->next;
9509 xfree (item);
9510 }
9511
9512 dwarf2_queue_tail = NULL;
45cfd468 9513
b4f54984 9514 if (dwarf_read_debug)
45cfd468
DE
9515 {
9516 fprintf_unfiltered (gdb_stdlog, "Done expanding symtabs of %s.\n",
4262abfb 9517 objfile_name (dwarf2_per_objfile->objfile));
45cfd468 9518 }
10b3939b
DJ
9519}
9520
10b3939b
DJ
9521/* Read in full symbols for PST, and anything it depends on. */
9522
c906108c 9523static void
fba45db2 9524psymtab_to_symtab_1 (struct partial_symtab *pst)
c906108c 9525{
10b3939b 9526 struct dwarf2_per_cu_data *per_cu;
aaa75496
JB
9527 int i;
9528
95554aad
TT
9529 if (pst->readin)
9530 return;
9531
aaa75496 9532 for (i = 0; i < pst->number_of_dependencies; i++)
95554aad
TT
9533 if (!pst->dependencies[i]->readin
9534 && pst->dependencies[i]->user == NULL)
aaa75496
JB
9535 {
9536 /* Inform about additional files that need to be read in. */
9537 if (info_verbose)
9538 {
a3f17187 9539 /* FIXME: i18n: Need to make this a single string. */
aaa75496
JB
9540 fputs_filtered (" ", gdb_stdout);
9541 wrap_here ("");
9542 fputs_filtered ("and ", gdb_stdout);
9543 wrap_here ("");
9544 printf_filtered ("%s...", pst->dependencies[i]->filename);
0963b4bd 9545 wrap_here (""); /* Flush output. */
aaa75496
JB
9546 gdb_flush (gdb_stdout);
9547 }
9548 psymtab_to_symtab_1 (pst->dependencies[i]);
9549 }
9550
9a3c8263 9551 per_cu = (struct dwarf2_per_cu_data *) pst->read_symtab_private;
10b3939b
DJ
9552
9553 if (per_cu == NULL)
aaa75496
JB
9554 {
9555 /* It's an include file, no symbols to read for it.
9556 Everything is in the parent symtab. */
9557 pst->readin = 1;
9558 return;
9559 }
c906108c 9560
a0f42c21 9561 dw2_do_instantiate_symtab (per_cu);
10b3939b
DJ
9562}
9563
dee91e82
DE
9564/* Trivial hash function for die_info: the hash value of a DIE
9565 is its offset in .debug_info for this objfile. */
10b3939b 9566
dee91e82
DE
9567static hashval_t
9568die_hash (const void *item)
10b3939b 9569{
9a3c8263 9570 const struct die_info *die = (const struct die_info *) item;
6502dd73 9571
9c541725 9572 return to_underlying (die->sect_off);
dee91e82 9573}
63d06c5c 9574
dee91e82
DE
9575/* Trivial comparison function for die_info structures: two DIEs
9576 are equal if they have the same offset. */
98bfdba5 9577
dee91e82
DE
9578static int
9579die_eq (const void *item_lhs, const void *item_rhs)
9580{
9a3c8263
SM
9581 const struct die_info *die_lhs = (const struct die_info *) item_lhs;
9582 const struct die_info *die_rhs = (const struct die_info *) item_rhs;
c906108c 9583
9c541725 9584 return die_lhs->sect_off == die_rhs->sect_off;
dee91e82 9585}
c906108c 9586
dee91e82
DE
9587/* die_reader_func for load_full_comp_unit.
9588 This is identical to read_signatured_type_reader,
9589 but is kept separate for now. */
c906108c 9590
dee91e82
DE
9591static void
9592load_full_comp_unit_reader (const struct die_reader_specs *reader,
d521ce57 9593 const gdb_byte *info_ptr,
dee91e82
DE
9594 struct die_info *comp_unit_die,
9595 int has_children,
9596 void *data)
9597{
9598 struct dwarf2_cu *cu = reader->cu;
9a3c8263 9599 enum language *language_ptr = (enum language *) data;
6caca83c 9600
dee91e82
DE
9601 gdb_assert (cu->die_hash == NULL);
9602 cu->die_hash =
9603 htab_create_alloc_ex (cu->header.length / 12,
9604 die_hash,
9605 die_eq,
9606 NULL,
9607 &cu->comp_unit_obstack,
9608 hashtab_obstack_allocate,
9609 dummy_obstack_deallocate);
e142c38c 9610
dee91e82
DE
9611 if (has_children)
9612 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
9613 &info_ptr, comp_unit_die);
9614 cu->dies = comp_unit_die;
9615 /* comp_unit_die is not stored in die_hash, no need. */
10b3939b
DJ
9616
9617 /* We try not to read any attributes in this function, because not
9cdd5dbd 9618 all CUs needed for references have been loaded yet, and symbol
10b3939b 9619 table processing isn't initialized. But we have to set the CU language,
dee91e82
DE
9620 or we won't be able to build types correctly.
9621 Similarly, if we do not read the producer, we can not apply
9622 producer-specific interpretation. */
95554aad 9623 prepare_one_comp_unit (cu, cu->dies, *language_ptr);
dee91e82 9624}
10b3939b 9625
dee91e82 9626/* Load the DIEs associated with PER_CU into memory. */
a6c727b2 9627
dee91e82 9628static void
95554aad
TT
9629load_full_comp_unit (struct dwarf2_per_cu_data *this_cu,
9630 enum language pretend_language)
dee91e82 9631{
3019eac3 9632 gdb_assert (! this_cu->is_debug_types);
c5b7e1cb 9633
f4dc4d17
DE
9634 init_cutu_and_read_dies (this_cu, NULL, 1, 1,
9635 load_full_comp_unit_reader, &pretend_language);
10b3939b
DJ
9636}
9637
3da10d80
KS
9638/* Add a DIE to the delayed physname list. */
9639
9640static void
9641add_to_method_list (struct type *type, int fnfield_index, int index,
9642 const char *name, struct die_info *die,
9643 struct dwarf2_cu *cu)
9644{
9645 struct delayed_method_info mi;
9646 mi.type = type;
9647 mi.fnfield_index = fnfield_index;
9648 mi.index = index;
9649 mi.name = name;
9650 mi.die = die;
c89b44cd 9651 cu->method_list.push_back (mi);
3da10d80
KS
9652}
9653
3693fdb3
PA
9654/* Check whether [PHYSNAME, PHYSNAME+LEN) ends with a modifier like
9655 "const" / "volatile". If so, decrements LEN by the length of the
9656 modifier and return true. Otherwise return false. */
9657
9658template<size_t N>
9659static bool
9660check_modifier (const char *physname, size_t &len, const char (&mod)[N])
9661{
9662 size_t mod_len = sizeof (mod) - 1;
9663 if (len > mod_len && startswith (physname + (len - mod_len), mod))
9664 {
9665 len -= mod_len;
9666 return true;
9667 }
9668 return false;
9669}
9670
3da10d80
KS
9671/* Compute the physnames of any methods on the CU's method list.
9672
9673 The computation of method physnames is delayed in order to avoid the
9674 (bad) condition that one of the method's formal parameters is of an as yet
9675 incomplete type. */
9676
9677static void
9678compute_delayed_physnames (struct dwarf2_cu *cu)
9679{
3693fdb3 9680 /* Only C++ delays computing physnames. */
c89b44cd 9681 if (cu->method_list.empty ())
3693fdb3
PA
9682 return;
9683 gdb_assert (cu->language == language_cplus);
9684
c89b44cd 9685 for (struct delayed_method_info &mi : cu->method_list)
3da10d80 9686 {
1d06ead6 9687 const char *physname;
3da10d80 9688 struct fn_fieldlist *fn_flp
c89b44cd
TT
9689 = &TYPE_FN_FIELDLIST (mi.type, mi.fnfield_index);
9690 physname = dwarf2_physname (mi.name, mi.die, cu);
9691 TYPE_FN_FIELD_PHYSNAME (fn_flp->fn_fields, mi.index)
005e54bb 9692 = physname ? physname : "";
3693fdb3
PA
9693
9694 /* Since there's no tag to indicate whether a method is a
9695 const/volatile overload, extract that information out of the
9696 demangled name. */
9697 if (physname != NULL)
9698 {
9699 size_t len = strlen (physname);
9700
9701 while (1)
9702 {
9703 if (physname[len] == ')') /* shortcut */
9704 break;
9705 else if (check_modifier (physname, len, " const"))
c89b44cd 9706 TYPE_FN_FIELD_CONST (fn_flp->fn_fields, mi.index) = 1;
3693fdb3 9707 else if (check_modifier (physname, len, " volatile"))
c89b44cd 9708 TYPE_FN_FIELD_VOLATILE (fn_flp->fn_fields, mi.index) = 1;
3693fdb3
PA
9709 else
9710 break;
9711 }
9712 }
3da10d80 9713 }
c89b44cd
TT
9714
9715 /* The list is no longer needed. */
9716 cu->method_list.clear ();
3da10d80
KS
9717}
9718
a766d390
DE
9719/* Go objects should be embedded in a DW_TAG_module DIE,
9720 and it's not clear if/how imported objects will appear.
9721 To keep Go support simple until that's worked out,
9722 go back through what we've read and create something usable.
9723 We could do this while processing each DIE, and feels kinda cleaner,
9724 but that way is more invasive.
9725 This is to, for example, allow the user to type "p var" or "b main"
9726 without having to specify the package name, and allow lookups
9727 of module.object to work in contexts that use the expression
9728 parser. */
9729
9730static void
9731fixup_go_packaging (struct dwarf2_cu *cu)
9732{
9733 char *package_name = NULL;
9734 struct pending *list;
9735 int i;
9736
9737 for (list = global_symbols; list != NULL; list = list->next)
9738 {
9739 for (i = 0; i < list->nsyms; ++i)
9740 {
9741 struct symbol *sym = list->symbol[i];
9742
9743 if (SYMBOL_LANGUAGE (sym) == language_go
9744 && SYMBOL_CLASS (sym) == LOC_BLOCK)
9745 {
9746 char *this_package_name = go_symbol_package_name (sym);
9747
9748 if (this_package_name == NULL)
9749 continue;
9750 if (package_name == NULL)
9751 package_name = this_package_name;
9752 else
9753 {
518817b3
SM
9754 struct objfile *objfile
9755 = cu->per_cu->dwarf2_per_objfile->objfile;
a766d390
DE
9756 if (strcmp (package_name, this_package_name) != 0)
9757 complaint (&symfile_complaints,
9758 _("Symtab %s has objects from two different Go packages: %s and %s"),
08be3fe3
DE
9759 (symbol_symtab (sym) != NULL
9760 ? symtab_to_filename_for_display
9761 (symbol_symtab (sym))
e3b94546 9762 : objfile_name (objfile)),
a766d390
DE
9763 this_package_name, package_name);
9764 xfree (this_package_name);
9765 }
9766 }
9767 }
9768 }
9769
9770 if (package_name != NULL)
9771 {
518817b3 9772 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
34a68019 9773 const char *saved_package_name
224c3ddb
SM
9774 = (const char *) obstack_copy0 (&objfile->per_bfd->storage_obstack,
9775 package_name,
9776 strlen (package_name));
19f392bc
UW
9777 struct type *type = init_type (objfile, TYPE_CODE_MODULE, 0,
9778 saved_package_name);
a766d390
DE
9779 struct symbol *sym;
9780
9781 TYPE_TAG_NAME (type) = TYPE_NAME (type);
9782
e623cf5d 9783 sym = allocate_symbol (objfile);
f85f34ed 9784 SYMBOL_SET_LANGUAGE (sym, language_go, &objfile->objfile_obstack);
86f62fd7
TT
9785 SYMBOL_SET_NAMES (sym, saved_package_name,
9786 strlen (saved_package_name), 0, objfile);
a766d390
DE
9787 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
9788 e.g., "main" finds the "main" module and not C's main(). */
9789 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
f1e6e072 9790 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
a766d390
DE
9791 SYMBOL_TYPE (sym) = type;
9792
9793 add_symbol_to_list (sym, &global_symbols);
9794
9795 xfree (package_name);
9796 }
9797}
9798
c9317f21
TT
9799/* Allocate a fully-qualified name consisting of the two parts on the
9800 obstack. */
9801
9802static const char *
9803rust_fully_qualify (struct obstack *obstack, const char *p1, const char *p2)
9804{
9805 return obconcat (obstack, p1, "::", p2, (char *) NULL);
9806}
9807
9808/* A helper that allocates a struct discriminant_info to attach to a
9809 union type. */
9810
9811static struct discriminant_info *
9812alloc_discriminant_info (struct type *type, int discriminant_index,
9813 int default_index)
9814{
9815 gdb_assert (TYPE_CODE (type) == TYPE_CODE_UNION);
c7b15a66
TT
9816 gdb_assert (discriminant_index == -1
9817 || (discriminant_index >= 0
9818 && discriminant_index < TYPE_NFIELDS (type)));
c9317f21 9819 gdb_assert (default_index == -1
c7b15a66 9820 || (default_index >= 0 && default_index < TYPE_NFIELDS (type)));
c9317f21
TT
9821
9822 TYPE_FLAG_DISCRIMINATED_UNION (type) = 1;
9823
9824 struct discriminant_info *disc
9825 = ((struct discriminant_info *)
9826 TYPE_ZALLOC (type,
9827 offsetof (struct discriminant_info, discriminants)
9828 + TYPE_NFIELDS (type) * sizeof (disc->discriminants[0])));
9829 disc->default_index = default_index;
9830 disc->discriminant_index = discriminant_index;
9831
9832 struct dynamic_prop prop;
9833 prop.kind = PROP_UNDEFINED;
9834 prop.data.baton = disc;
9835
9836 add_dyn_prop (DYN_PROP_DISCRIMINATED, prop, type);
9837
9838 return disc;
9839}
9840
9841/* Some versions of rustc emitted enums in an unusual way.
9842
9843 Ordinary enums were emitted as unions. The first element of each
9844 structure in the union was named "RUST$ENUM$DISR". This element
9845 held the discriminant.
9846
9847 These versions of Rust also implemented the "non-zero"
9848 optimization. When the enum had two values, and one is empty and
9849 the other holds a pointer that cannot be zero, the pointer is used
9850 as the discriminant, with a zero value meaning the empty variant.
9851 Here, the union's first member is of the form
9852 RUST$ENCODED$ENUM$<fieldno>$<fieldno>$...$<variantname>
9853 where the fieldnos are the indices of the fields that should be
9854 traversed in order to find the field (which may be several fields deep)
9855 and the variantname is the name of the variant of the case when the
9856 field is zero.
9857
9858 This function recognizes whether TYPE is of one of these forms,
9859 and, if so, smashes it to be a variant type. */
9860
9861static void
9862quirk_rust_enum (struct type *type, struct objfile *objfile)
9863{
9864 gdb_assert (TYPE_CODE (type) == TYPE_CODE_UNION);
9865
9866 /* We don't need to deal with empty enums. */
9867 if (TYPE_NFIELDS (type) == 0)
9868 return;
9869
9870#define RUST_ENUM_PREFIX "RUST$ENCODED$ENUM$"
9871 if (TYPE_NFIELDS (type) == 1
9872 && startswith (TYPE_FIELD_NAME (type, 0), RUST_ENUM_PREFIX))
9873 {
9874 const char *name = TYPE_FIELD_NAME (type, 0) + strlen (RUST_ENUM_PREFIX);
9875
9876 /* Decode the field name to find the offset of the
9877 discriminant. */
9878 ULONGEST bit_offset = 0;
9879 struct type *field_type = TYPE_FIELD_TYPE (type, 0);
9880 while (name[0] >= '0' && name[0] <= '9')
9881 {
9882 char *tail;
9883 unsigned long index = strtoul (name, &tail, 10);
9884 name = tail;
9885 if (*name != '$'
9886 || index >= TYPE_NFIELDS (field_type)
9887 || (TYPE_FIELD_LOC_KIND (field_type, index)
9888 != FIELD_LOC_KIND_BITPOS))
9889 {
9890 complaint (&symfile_complaints,
9891 _("Could not parse Rust enum encoding string \"%s\""
9892 "[in module %s]"),
9893 TYPE_FIELD_NAME (type, 0),
9894 objfile_name (objfile));
9895 return;
9896 }
9897 ++name;
9898
9899 bit_offset += TYPE_FIELD_BITPOS (field_type, index);
9900 field_type = TYPE_FIELD_TYPE (field_type, index);
9901 }
9902
9903 /* Make a union to hold the variants. */
9904 struct type *union_type = alloc_type (objfile);
9905 TYPE_CODE (union_type) = TYPE_CODE_UNION;
9906 TYPE_NFIELDS (union_type) = 3;
9907 TYPE_FIELDS (union_type)
9908 = (struct field *) TYPE_ZALLOC (type, 3 * sizeof (struct field));
9909 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
9910
9911 /* Put the discriminant must at index 0. */
9912 TYPE_FIELD_TYPE (union_type, 0) = field_type;
9913 TYPE_FIELD_ARTIFICIAL (union_type, 0) = 1;
9914 TYPE_FIELD_NAME (union_type, 0) = "<<discriminant>>";
9915 SET_FIELD_BITPOS (TYPE_FIELD (union_type, 0), bit_offset);
9916
9917 /* The order of fields doesn't really matter, so put the real
9918 field at index 1 and the data-less field at index 2. */
9919 struct discriminant_info *disc
9920 = alloc_discriminant_info (union_type, 0, 1);
9921 TYPE_FIELD (union_type, 1) = TYPE_FIELD (type, 0);
9922 TYPE_FIELD_NAME (union_type, 1)
9923 = rust_last_path_segment (TYPE_NAME (TYPE_FIELD_TYPE (union_type, 1)));
9924 TYPE_NAME (TYPE_FIELD_TYPE (union_type, 1))
9925 = rust_fully_qualify (&objfile->objfile_obstack, TYPE_NAME (type),
9926 TYPE_FIELD_NAME (union_type, 1));
9927
9928 const char *dataless_name
9929 = rust_fully_qualify (&objfile->objfile_obstack, TYPE_NAME (type),
9930 name);
9931 struct type *dataless_type = init_type (objfile, TYPE_CODE_VOID, 0,
9932 dataless_name);
9933 TYPE_FIELD_TYPE (union_type, 2) = dataless_type;
9934 /* NAME points into the original discriminant name, which
9935 already has the correct lifetime. */
9936 TYPE_FIELD_NAME (union_type, 2) = name;
9937 SET_FIELD_BITPOS (TYPE_FIELD (union_type, 2), 0);
9938 disc->discriminants[2] = 0;
9939
9940 /* Smash this type to be a structure type. We have to do this
9941 because the type has already been recorded. */
9942 TYPE_CODE (type) = TYPE_CODE_STRUCT;
9943 TYPE_NFIELDS (type) = 1;
9944 TYPE_FIELDS (type)
9945 = (struct field *) TYPE_ZALLOC (type, sizeof (struct field));
9946
9947 /* Install the variant part. */
9948 TYPE_FIELD_TYPE (type, 0) = union_type;
9949 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
9950 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
9951 }
9952 else if (TYPE_NFIELDS (type) == 1)
9953 {
9954 /* We assume that a union with a single field is a univariant
9955 enum. */
9956 /* Smash this type to be a structure type. We have to do this
9957 because the type has already been recorded. */
9958 TYPE_CODE (type) = TYPE_CODE_STRUCT;
9959
9960 /* Make a union to hold the variants. */
9961 struct type *union_type = alloc_type (objfile);
9962 TYPE_CODE (union_type) = TYPE_CODE_UNION;
9963 TYPE_NFIELDS (union_type) = TYPE_NFIELDS (type);
9964 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
9965 TYPE_FIELDS (union_type) = TYPE_FIELDS (type);
9966
9967 struct type *field_type = TYPE_FIELD_TYPE (union_type, 0);
9968 const char *variant_name
9969 = rust_last_path_segment (TYPE_NAME (field_type));
9970 TYPE_FIELD_NAME (union_type, 0) = variant_name;
9971 TYPE_NAME (field_type)
9972 = rust_fully_qualify (&objfile->objfile_obstack,
c7b15a66 9973 TYPE_NAME (type), variant_name);
c9317f21
TT
9974
9975 /* Install the union in the outer struct type. */
9976 TYPE_NFIELDS (type) = 1;
9977 TYPE_FIELDS (type)
9978 = (struct field *) TYPE_ZALLOC (union_type, sizeof (struct field));
9979 TYPE_FIELD_TYPE (type, 0) = union_type;
9980 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
9981 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
9982
9983 alloc_discriminant_info (union_type, -1, 0);
9984 }
9985 else
9986 {
9987 struct type *disr_type = nullptr;
9988 for (int i = 0; i < TYPE_NFIELDS (type); ++i)
9989 {
9990 disr_type = TYPE_FIELD_TYPE (type, i);
9991
a037790e
TT
9992 if (TYPE_CODE (disr_type) != TYPE_CODE_STRUCT)
9993 {
9994 /* All fields of a true enum will be structs. */
9995 return;
9996 }
9997 else if (TYPE_NFIELDS (disr_type) == 0)
c9317f21
TT
9998 {
9999 /* Could be data-less variant, so keep going. */
a037790e 10000 disr_type = nullptr;
c9317f21
TT
10001 }
10002 else if (strcmp (TYPE_FIELD_NAME (disr_type, 0),
10003 "RUST$ENUM$DISR") != 0)
10004 {
10005 /* Not a Rust enum. */
10006 return;
10007 }
10008 else
10009 {
10010 /* Found one. */
10011 break;
10012 }
10013 }
10014
10015 /* If we got here without a discriminant, then it's probably
10016 just a union. */
10017 if (disr_type == nullptr)
10018 return;
10019
10020 /* Smash this type to be a structure type. We have to do this
10021 because the type has already been recorded. */
10022 TYPE_CODE (type) = TYPE_CODE_STRUCT;
10023
10024 /* Make a union to hold the variants. */
10025 struct field *disr_field = &TYPE_FIELD (disr_type, 0);
10026 struct type *union_type = alloc_type (objfile);
10027 TYPE_CODE (union_type) = TYPE_CODE_UNION;
10028 TYPE_NFIELDS (union_type) = 1 + TYPE_NFIELDS (type);
10029 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
10030 TYPE_FIELDS (union_type)
10031 = (struct field *) TYPE_ZALLOC (union_type,
10032 (TYPE_NFIELDS (union_type)
10033 * sizeof (struct field)));
10034
10035 memcpy (TYPE_FIELDS (union_type) + 1, TYPE_FIELDS (type),
10036 TYPE_NFIELDS (type) * sizeof (struct field));
10037
10038 /* Install the discriminant at index 0 in the union. */
10039 TYPE_FIELD (union_type, 0) = *disr_field;
10040 TYPE_FIELD_ARTIFICIAL (union_type, 0) = 1;
10041 TYPE_FIELD_NAME (union_type, 0) = "<<discriminant>>";
10042
10043 /* Install the union in the outer struct type. */
10044 TYPE_FIELD_TYPE (type, 0) = union_type;
10045 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
10046 TYPE_NFIELDS (type) = 1;
10047
10048 /* Set the size and offset of the union type. */
10049 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
10050
10051 /* We need a way to find the correct discriminant given a
10052 variant name. For convenience we build a map here. */
10053 struct type *enum_type = FIELD_TYPE (*disr_field);
10054 std::unordered_map<std::string, ULONGEST> discriminant_map;
10055 for (int i = 0; i < TYPE_NFIELDS (enum_type); ++i)
10056 {
10057 if (TYPE_FIELD_LOC_KIND (enum_type, i) == FIELD_LOC_KIND_ENUMVAL)
10058 {
10059 const char *name
10060 = rust_last_path_segment (TYPE_FIELD_NAME (enum_type, i));
10061 discriminant_map[name] = TYPE_FIELD_ENUMVAL (enum_type, i);
10062 }
10063 }
10064
10065 int n_fields = TYPE_NFIELDS (union_type);
10066 struct discriminant_info *disc
10067 = alloc_discriminant_info (union_type, 0, -1);
10068 /* Skip the discriminant here. */
10069 for (int i = 1; i < n_fields; ++i)
10070 {
10071 /* Find the final word in the name of this variant's type.
10072 That name can be used to look up the correct
10073 discriminant. */
10074 const char *variant_name
10075 = rust_last_path_segment (TYPE_NAME (TYPE_FIELD_TYPE (union_type,
10076 i)));
10077
10078 auto iter = discriminant_map.find (variant_name);
10079 if (iter != discriminant_map.end ())
10080 disc->discriminants[i] = iter->second;
10081
10082 /* Remove the discriminant field. */
10083 struct type *sub_type = TYPE_FIELD_TYPE (union_type, i);
10084 --TYPE_NFIELDS (sub_type);
10085 ++TYPE_FIELDS (sub_type);
10086 TYPE_FIELD_NAME (union_type, i) = variant_name;
10087 TYPE_NAME (sub_type)
10088 = rust_fully_qualify (&objfile->objfile_obstack,
10089 TYPE_NAME (type), variant_name);
10090 }
10091 }
10092}
10093
10094/* Rewrite some Rust unions to be structures with variants parts. */
10095
10096static void
10097rust_union_quirks (struct dwarf2_cu *cu)
10098{
10099 gdb_assert (cu->language == language_rust);
10100 for (struct type *type : cu->rust_unions)
10101 quirk_rust_enum (type, cu->per_cu->dwarf2_per_objfile->objfile);
10102}
10103
95554aad
TT
10104/* Return the symtab for PER_CU. This works properly regardless of
10105 whether we're using the index or psymtabs. */
10106
43f3e411
DE
10107static struct compunit_symtab *
10108get_compunit_symtab (struct dwarf2_per_cu_data *per_cu)
95554aad 10109{
ed2dc618 10110 return (per_cu->dwarf2_per_objfile->using_index
43f3e411
DE
10111 ? per_cu->v.quick->compunit_symtab
10112 : per_cu->v.psymtab->compunit_symtab);
95554aad
TT
10113}
10114
10115/* A helper function for computing the list of all symbol tables
10116 included by PER_CU. */
10117
10118static void
43f3e411 10119recursively_compute_inclusions (VEC (compunit_symtab_ptr) **result,
ec94af83 10120 htab_t all_children, htab_t all_type_symtabs,
f9125b6c 10121 struct dwarf2_per_cu_data *per_cu,
43f3e411 10122 struct compunit_symtab *immediate_parent)
95554aad
TT
10123{
10124 void **slot;
10125 int ix;
43f3e411 10126 struct compunit_symtab *cust;
95554aad
TT
10127 struct dwarf2_per_cu_data *iter;
10128
10129 slot = htab_find_slot (all_children, per_cu, INSERT);
10130 if (*slot != NULL)
10131 {
10132 /* This inclusion and its children have been processed. */
10133 return;
10134 }
10135
10136 *slot = per_cu;
10137 /* Only add a CU if it has a symbol table. */
43f3e411
DE
10138 cust = get_compunit_symtab (per_cu);
10139 if (cust != NULL)
ec94af83
DE
10140 {
10141 /* If this is a type unit only add its symbol table if we haven't
10142 seen it yet (type unit per_cu's can share symtabs). */
10143 if (per_cu->is_debug_types)
10144 {
43f3e411 10145 slot = htab_find_slot (all_type_symtabs, cust, INSERT);
ec94af83
DE
10146 if (*slot == NULL)
10147 {
43f3e411
DE
10148 *slot = cust;
10149 VEC_safe_push (compunit_symtab_ptr, *result, cust);
10150 if (cust->user == NULL)
10151 cust->user = immediate_parent;
ec94af83
DE
10152 }
10153 }
10154 else
f9125b6c 10155 {
43f3e411
DE
10156 VEC_safe_push (compunit_symtab_ptr, *result, cust);
10157 if (cust->user == NULL)
10158 cust->user = immediate_parent;
f9125b6c 10159 }
ec94af83 10160 }
95554aad
TT
10161
10162 for (ix = 0;
796a7ff8 10163 VEC_iterate (dwarf2_per_cu_ptr, per_cu->imported_symtabs, ix, iter);
95554aad 10164 ++ix)
ec94af83
DE
10165 {
10166 recursively_compute_inclusions (result, all_children,
43f3e411 10167 all_type_symtabs, iter, cust);
ec94af83 10168 }
95554aad
TT
10169}
10170
43f3e411 10171/* Compute the compunit_symtab 'includes' fields for the compunit_symtab of
95554aad
TT
10172 PER_CU. */
10173
10174static void
43f3e411 10175compute_compunit_symtab_includes (struct dwarf2_per_cu_data *per_cu)
95554aad 10176{
f4dc4d17
DE
10177 gdb_assert (! per_cu->is_debug_types);
10178
796a7ff8 10179 if (!VEC_empty (dwarf2_per_cu_ptr, per_cu->imported_symtabs))
95554aad
TT
10180 {
10181 int ix, len;
ec94af83 10182 struct dwarf2_per_cu_data *per_cu_iter;
43f3e411
DE
10183 struct compunit_symtab *compunit_symtab_iter;
10184 VEC (compunit_symtab_ptr) *result_symtabs = NULL;
ec94af83 10185 htab_t all_children, all_type_symtabs;
43f3e411 10186 struct compunit_symtab *cust = get_compunit_symtab (per_cu);
95554aad
TT
10187
10188 /* If we don't have a symtab, we can just skip this case. */
43f3e411 10189 if (cust == NULL)
95554aad
TT
10190 return;
10191
10192 all_children = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
10193 NULL, xcalloc, xfree);
ec94af83
DE
10194 all_type_symtabs = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
10195 NULL, xcalloc, xfree);
95554aad
TT
10196
10197 for (ix = 0;
796a7ff8 10198 VEC_iterate (dwarf2_per_cu_ptr, per_cu->imported_symtabs,
ec94af83 10199 ix, per_cu_iter);
95554aad 10200 ++ix)
ec94af83
DE
10201 {
10202 recursively_compute_inclusions (&result_symtabs, all_children,
f9125b6c 10203 all_type_symtabs, per_cu_iter,
43f3e411 10204 cust);
ec94af83 10205 }
95554aad 10206
ec94af83 10207 /* Now we have a transitive closure of all the included symtabs. */
43f3e411
DE
10208 len = VEC_length (compunit_symtab_ptr, result_symtabs);
10209 cust->includes
ed2dc618 10210 = XOBNEWVEC (&per_cu->dwarf2_per_objfile->objfile->objfile_obstack,
8d749320 10211 struct compunit_symtab *, len + 1);
95554aad 10212 for (ix = 0;
43f3e411
DE
10213 VEC_iterate (compunit_symtab_ptr, result_symtabs, ix,
10214 compunit_symtab_iter);
95554aad 10215 ++ix)
43f3e411
DE
10216 cust->includes[ix] = compunit_symtab_iter;
10217 cust->includes[len] = NULL;
95554aad 10218
43f3e411 10219 VEC_free (compunit_symtab_ptr, result_symtabs);
95554aad 10220 htab_delete (all_children);
ec94af83 10221 htab_delete (all_type_symtabs);
95554aad
TT
10222 }
10223}
10224
10225/* Compute the 'includes' field for the symtabs of all the CUs we just
10226 read. */
10227
10228static void
ed2dc618 10229process_cu_includes (struct dwarf2_per_objfile *dwarf2_per_objfile)
95554aad
TT
10230{
10231 int ix;
10232 struct dwarf2_per_cu_data *iter;
10233
10234 for (ix = 0;
10235 VEC_iterate (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus,
10236 ix, iter);
10237 ++ix)
f4dc4d17
DE
10238 {
10239 if (! iter->is_debug_types)
43f3e411 10240 compute_compunit_symtab_includes (iter);
f4dc4d17 10241 }
95554aad
TT
10242
10243 VEC_free (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus);
10244}
10245
9cdd5dbd 10246/* Generate full symbol information for PER_CU, whose DIEs have
10b3939b
DJ
10247 already been loaded into memory. */
10248
10249static void
95554aad
TT
10250process_full_comp_unit (struct dwarf2_per_cu_data *per_cu,
10251 enum language pretend_language)
10b3939b 10252{
10b3939b 10253 struct dwarf2_cu *cu = per_cu->cu;
ed2dc618
SM
10254 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
10255 struct objfile *objfile = dwarf2_per_objfile->objfile;
3e29f34a 10256 struct gdbarch *gdbarch = get_objfile_arch (objfile);
10b3939b 10257 CORE_ADDR lowpc, highpc;
43f3e411 10258 struct compunit_symtab *cust;
10b3939b 10259 CORE_ADDR baseaddr;
4359dff1 10260 struct block *static_block;
3e29f34a 10261 CORE_ADDR addr;
10b3939b
DJ
10262
10263 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
10264
10b3939b 10265 buildsym_init ();
33c7c59d 10266 scoped_free_pendings free_pending;
c89b44cd
TT
10267
10268 /* Clear the list here in case something was left over. */
10269 cu->method_list.clear ();
10b3939b
DJ
10270
10271 cu->list_in_scope = &file_symbols;
c906108c 10272
95554aad
TT
10273 cu->language = pretend_language;
10274 cu->language_defn = language_def (cu->language);
10275
c906108c 10276 /* Do line number decoding in read_file_scope () */
10b3939b 10277 process_die (cu->dies, cu);
c906108c 10278
a766d390
DE
10279 /* For now fudge the Go package. */
10280 if (cu->language == language_go)
10281 fixup_go_packaging (cu);
10282
3da10d80
KS
10283 /* Now that we have processed all the DIEs in the CU, all the types
10284 should be complete, and it should now be safe to compute all of the
10285 physnames. */
10286 compute_delayed_physnames (cu);
3da10d80 10287
c9317f21
TT
10288 if (cu->language == language_rust)
10289 rust_union_quirks (cu);
10290
fae299cd
DC
10291 /* Some compilers don't define a DW_AT_high_pc attribute for the
10292 compilation unit. If the DW_AT_high_pc is missing, synthesize
10293 it, by scanning the DIE's below the compilation unit. */
10b3939b 10294 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
c906108c 10295
3e29f34a
MR
10296 addr = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
10297 static_block = end_symtab_get_static_block (addr, 0, 1);
4359dff1
JK
10298
10299 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
10300 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
10301 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
10302 addrmap to help ensure it has an accurate map of pc values belonging to
10303 this comp unit. */
10304 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
10305
43f3e411
DE
10306 cust = end_symtab_from_static_block (static_block,
10307 SECT_OFF_TEXT (objfile), 0);
c906108c 10308
43f3e411 10309 if (cust != NULL)
c906108c 10310 {
df15bd07 10311 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
4632c0d0 10312
8be455d7
JK
10313 /* Set symtab language to language from DW_AT_language. If the
10314 compilation is from a C file generated by language preprocessors, do
10315 not set the language if it was already deduced by start_subfile. */
43f3e411 10316 if (!(cu->language == language_c
40e3ad0e 10317 && COMPUNIT_FILETABS (cust)->language != language_unknown))
43f3e411 10318 COMPUNIT_FILETABS (cust)->language = cu->language;
8be455d7
JK
10319
10320 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
10321 produce DW_AT_location with location lists but it can be possibly
ab260dad
JK
10322 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
10323 there were bugs in prologue debug info, fixed later in GCC-4.5
10324 by "unwind info for epilogues" patch (which is not directly related).
8be455d7
JK
10325
10326 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
10327 needed, it would be wrong due to missing DW_AT_producer there.
10328
10329 Still one can confuse GDB by using non-standard GCC compilation
10330 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
10331 */
ab260dad 10332 if (cu->has_loclist && gcc_4_minor >= 5)
43f3e411 10333 cust->locations_valid = 1;
e0d00bc7
JK
10334
10335 if (gcc_4_minor >= 5)
43f3e411 10336 cust->epilogue_unwind_valid = 1;
96408a79 10337
43f3e411 10338 cust->call_site_htab = cu->call_site_htab;
c906108c 10339 }
9291a0cd
TT
10340
10341 if (dwarf2_per_objfile->using_index)
43f3e411 10342 per_cu->v.quick->compunit_symtab = cust;
9291a0cd
TT
10343 else
10344 {
10345 struct partial_symtab *pst = per_cu->v.psymtab;
43f3e411 10346 pst->compunit_symtab = cust;
9291a0cd
TT
10347 pst->readin = 1;
10348 }
c906108c 10349
95554aad
TT
10350 /* Push it for inclusion processing later. */
10351 VEC_safe_push (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus, per_cu);
f4dc4d17 10352}
45cfd468 10353
f4dc4d17
DE
10354/* Generate full symbol information for type unit PER_CU, whose DIEs have
10355 already been loaded into memory. */
10356
10357static void
10358process_full_type_unit (struct dwarf2_per_cu_data *per_cu,
10359 enum language pretend_language)
10360{
10361 struct dwarf2_cu *cu = per_cu->cu;
ed2dc618
SM
10362 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
10363 struct objfile *objfile = dwarf2_per_objfile->objfile;
43f3e411 10364 struct compunit_symtab *cust;
0186c6a7
DE
10365 struct signatured_type *sig_type;
10366
10367 gdb_assert (per_cu->is_debug_types);
10368 sig_type = (struct signatured_type *) per_cu;
f4dc4d17
DE
10369
10370 buildsym_init ();
33c7c59d 10371 scoped_free_pendings free_pending;
c89b44cd
TT
10372
10373 /* Clear the list here in case something was left over. */
10374 cu->method_list.clear ();
f4dc4d17
DE
10375
10376 cu->list_in_scope = &file_symbols;
10377
10378 cu->language = pretend_language;
10379 cu->language_defn = language_def (cu->language);
10380
10381 /* The symbol tables are set up in read_type_unit_scope. */
10382 process_die (cu->dies, cu);
10383
10384 /* For now fudge the Go package. */
10385 if (cu->language == language_go)
10386 fixup_go_packaging (cu);
10387
10388 /* Now that we have processed all the DIEs in the CU, all the types
10389 should be complete, and it should now be safe to compute all of the
10390 physnames. */
10391 compute_delayed_physnames (cu);
f4dc4d17 10392
c9317f21
TT
10393 if (cu->language == language_rust)
10394 rust_union_quirks (cu);
10395
f4dc4d17
DE
10396 /* TUs share symbol tables.
10397 If this is the first TU to use this symtab, complete the construction
094b34ac
DE
10398 of it with end_expandable_symtab. Otherwise, complete the addition of
10399 this TU's symbols to the existing symtab. */
43f3e411 10400 if (sig_type->type_unit_group->compunit_symtab == NULL)
45cfd468 10401 {
43f3e411
DE
10402 cust = end_expandable_symtab (0, SECT_OFF_TEXT (objfile));
10403 sig_type->type_unit_group->compunit_symtab = cust;
f4dc4d17 10404
43f3e411 10405 if (cust != NULL)
f4dc4d17
DE
10406 {
10407 /* Set symtab language to language from DW_AT_language. If the
10408 compilation is from a C file generated by language preprocessors,
10409 do not set the language if it was already deduced by
10410 start_subfile. */
43f3e411
DE
10411 if (!(cu->language == language_c
10412 && COMPUNIT_FILETABS (cust)->language != language_c))
10413 COMPUNIT_FILETABS (cust)->language = cu->language;
f4dc4d17
DE
10414 }
10415 }
10416 else
10417 {
0ab9ce85 10418 augment_type_symtab ();
43f3e411 10419 cust = sig_type->type_unit_group->compunit_symtab;
f4dc4d17
DE
10420 }
10421
10422 if (dwarf2_per_objfile->using_index)
43f3e411 10423 per_cu->v.quick->compunit_symtab = cust;
f4dc4d17
DE
10424 else
10425 {
10426 struct partial_symtab *pst = per_cu->v.psymtab;
43f3e411 10427 pst->compunit_symtab = cust;
f4dc4d17 10428 pst->readin = 1;
45cfd468 10429 }
c906108c
SS
10430}
10431
95554aad
TT
10432/* Process an imported unit DIE. */
10433
10434static void
10435process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
10436{
10437 struct attribute *attr;
10438
f4dc4d17
DE
10439 /* For now we don't handle imported units in type units. */
10440 if (cu->per_cu->is_debug_types)
10441 {
10442 error (_("Dwarf Error: DW_TAG_imported_unit is not"
10443 " supported in type units [in module %s]"),
518817b3 10444 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
f4dc4d17
DE
10445 }
10446
95554aad
TT
10447 attr = dwarf2_attr (die, DW_AT_import, cu);
10448 if (attr != NULL)
10449 {
9c541725
PA
10450 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
10451 bool is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
10452 dwarf2_per_cu_data *per_cu
e3b94546 10453 = dwarf2_find_containing_comp_unit (sect_off, is_dwz,
518817b3 10454 cu->per_cu->dwarf2_per_objfile);
95554aad 10455
69d751e3 10456 /* If necessary, add it to the queue and load its DIEs. */
95554aad
TT
10457 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
10458 load_full_comp_unit (per_cu, cu->language);
10459
796a7ff8 10460 VEC_safe_push (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs,
95554aad
TT
10461 per_cu);
10462 }
10463}
10464
4c8aa72d
PA
10465/* RAII object that represents a process_die scope: i.e.,
10466 starts/finishes processing a DIE. */
10467class process_die_scope
adde2bff 10468{
4c8aa72d
PA
10469public:
10470 process_die_scope (die_info *die, dwarf2_cu *cu)
10471 : m_die (die), m_cu (cu)
10472 {
10473 /* We should only be processing DIEs not already in process. */
10474 gdb_assert (!m_die->in_process);
10475 m_die->in_process = true;
10476 }
8c3cb9fa 10477
4c8aa72d
PA
10478 ~process_die_scope ()
10479 {
10480 m_die->in_process = false;
10481
10482 /* If we're done processing the DIE for the CU that owns the line
10483 header, we don't need the line header anymore. */
10484 if (m_cu->line_header_die_owner == m_die)
10485 {
10486 delete m_cu->line_header;
10487 m_cu->line_header = NULL;
10488 m_cu->line_header_die_owner = NULL;
10489 }
10490 }
10491
10492private:
10493 die_info *m_die;
10494 dwarf2_cu *m_cu;
10495};
adde2bff 10496
c906108c
SS
10497/* Process a die and its children. */
10498
10499static void
e7c27a73 10500process_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 10501{
4c8aa72d 10502 process_die_scope scope (die, cu);
adde2bff 10503
c906108c
SS
10504 switch (die->tag)
10505 {
10506 case DW_TAG_padding:
10507 break;
10508 case DW_TAG_compile_unit:
95554aad 10509 case DW_TAG_partial_unit:
e7c27a73 10510 read_file_scope (die, cu);
c906108c 10511 break;
348e048f
DE
10512 case DW_TAG_type_unit:
10513 read_type_unit_scope (die, cu);
10514 break;
c906108c 10515 case DW_TAG_subprogram:
c906108c 10516 case DW_TAG_inlined_subroutine:
edb3359d 10517 read_func_scope (die, cu);
c906108c
SS
10518 break;
10519 case DW_TAG_lexical_block:
14898363
L
10520 case DW_TAG_try_block:
10521 case DW_TAG_catch_block:
e7c27a73 10522 read_lexical_block_scope (die, cu);
c906108c 10523 break;
216f72a1 10524 case DW_TAG_call_site:
96408a79
SA
10525 case DW_TAG_GNU_call_site:
10526 read_call_site_scope (die, cu);
10527 break;
c906108c 10528 case DW_TAG_class_type:
680b30c7 10529 case DW_TAG_interface_type:
c906108c
SS
10530 case DW_TAG_structure_type:
10531 case DW_TAG_union_type:
134d01f1 10532 process_structure_scope (die, cu);
c906108c
SS
10533 break;
10534 case DW_TAG_enumeration_type:
134d01f1 10535 process_enumeration_scope (die, cu);
c906108c 10536 break;
134d01f1 10537
f792889a
DJ
10538 /* These dies have a type, but processing them does not create
10539 a symbol or recurse to process the children. Therefore we can
10540 read them on-demand through read_type_die. */
c906108c 10541 case DW_TAG_subroutine_type:
72019c9c 10542 case DW_TAG_set_type:
c906108c 10543 case DW_TAG_array_type:
c906108c 10544 case DW_TAG_pointer_type:
c906108c 10545 case DW_TAG_ptr_to_member_type:
c906108c 10546 case DW_TAG_reference_type:
4297a3f0 10547 case DW_TAG_rvalue_reference_type:
c906108c 10548 case DW_TAG_string_type:
c906108c 10549 break;
134d01f1 10550
c906108c 10551 case DW_TAG_base_type:
a02abb62 10552 case DW_TAG_subrange_type:
cb249c71 10553 case DW_TAG_typedef:
134d01f1
DJ
10554 /* Add a typedef symbol for the type definition, if it has a
10555 DW_AT_name. */
f792889a 10556 new_symbol (die, read_type_die (die, cu), cu);
a02abb62 10557 break;
c906108c 10558 case DW_TAG_common_block:
e7c27a73 10559 read_common_block (die, cu);
c906108c
SS
10560 break;
10561 case DW_TAG_common_inclusion:
10562 break;
d9fa45fe 10563 case DW_TAG_namespace:
4d4ec4e5 10564 cu->processing_has_namespace_info = 1;
e7c27a73 10565 read_namespace (die, cu);
d9fa45fe 10566 break;
5d7cb8df 10567 case DW_TAG_module:
4d4ec4e5 10568 cu->processing_has_namespace_info = 1;
5d7cb8df
JK
10569 read_module (die, cu);
10570 break;
d9fa45fe 10571 case DW_TAG_imported_declaration:
74921315
KS
10572 cu->processing_has_namespace_info = 1;
10573 if (read_namespace_alias (die, cu))
10574 break;
10575 /* The declaration is not a global namespace alias: fall through. */
d9fa45fe 10576 case DW_TAG_imported_module:
4d4ec4e5 10577 cu->processing_has_namespace_info = 1;
27aa8d6a
SW
10578 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
10579 || cu->language != language_fortran))
10580 complaint (&symfile_complaints, _("Tag '%s' has unexpected children"),
10581 dwarf_tag_name (die->tag));
10582 read_import_statement (die, cu);
d9fa45fe 10583 break;
95554aad
TT
10584
10585 case DW_TAG_imported_unit:
10586 process_imported_unit_die (die, cu);
10587 break;
10588
71a3c369
TT
10589 case DW_TAG_variable:
10590 read_variable (die, cu);
10591 break;
10592
c906108c 10593 default:
e7c27a73 10594 new_symbol (die, NULL, cu);
c906108c
SS
10595 break;
10596 }
10597}
ca69b9e6
DE
10598\f
10599/* DWARF name computation. */
c906108c 10600
94af9270
KS
10601/* A helper function for dwarf2_compute_name which determines whether DIE
10602 needs to have the name of the scope prepended to the name listed in the
10603 die. */
10604
10605static int
10606die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
10607{
1c809c68
TT
10608 struct attribute *attr;
10609
94af9270
KS
10610 switch (die->tag)
10611 {
10612 case DW_TAG_namespace:
10613 case DW_TAG_typedef:
10614 case DW_TAG_class_type:
10615 case DW_TAG_interface_type:
10616 case DW_TAG_structure_type:
10617 case DW_TAG_union_type:
10618 case DW_TAG_enumeration_type:
10619 case DW_TAG_enumerator:
10620 case DW_TAG_subprogram:
08a76f8a 10621 case DW_TAG_inlined_subroutine:
94af9270 10622 case DW_TAG_member:
74921315 10623 case DW_TAG_imported_declaration:
94af9270
KS
10624 return 1;
10625
10626 case DW_TAG_variable:
c2b0a229 10627 case DW_TAG_constant:
94af9270
KS
10628 /* We only need to prefix "globally" visible variables. These include
10629 any variable marked with DW_AT_external or any variable that
10630 lives in a namespace. [Variables in anonymous namespaces
10631 require prefixing, but they are not DW_AT_external.] */
10632
10633 if (dwarf2_attr (die, DW_AT_specification, cu))
10634 {
10635 struct dwarf2_cu *spec_cu = cu;
9a619af0 10636
94af9270
KS
10637 return die_needs_namespace (die_specification (die, &spec_cu),
10638 spec_cu);
10639 }
10640
1c809c68 10641 attr = dwarf2_attr (die, DW_AT_external, cu);
f55ee35c
JK
10642 if (attr == NULL && die->parent->tag != DW_TAG_namespace
10643 && die->parent->tag != DW_TAG_module)
1c809c68
TT
10644 return 0;
10645 /* A variable in a lexical block of some kind does not need a
10646 namespace, even though in C++ such variables may be external
10647 and have a mangled name. */
10648 if (die->parent->tag == DW_TAG_lexical_block
10649 || die->parent->tag == DW_TAG_try_block
1054b214
TT
10650 || die->parent->tag == DW_TAG_catch_block
10651 || die->parent->tag == DW_TAG_subprogram)
1c809c68
TT
10652 return 0;
10653 return 1;
94af9270
KS
10654
10655 default:
10656 return 0;
10657 }
10658}
10659
73b9be8b
KS
10660/* Return the DIE's linkage name attribute, either DW_AT_linkage_name
10661 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10662 defined for the given DIE. */
10663
10664static struct attribute *
10665dw2_linkage_name_attr (struct die_info *die, struct dwarf2_cu *cu)
10666{
10667 struct attribute *attr;
10668
10669 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
10670 if (attr == NULL)
10671 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
10672
10673 return attr;
10674}
10675
10676/* Return the DIE's linkage name as a string, either DW_AT_linkage_name
10677 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10678 defined for the given DIE. */
10679
10680static const char *
10681dw2_linkage_name (struct die_info *die, struct dwarf2_cu *cu)
10682{
10683 const char *linkage_name;
10684
10685 linkage_name = dwarf2_string_attr (die, DW_AT_linkage_name, cu);
10686 if (linkage_name == NULL)
10687 linkage_name = dwarf2_string_attr (die, DW_AT_MIPS_linkage_name, cu);
10688
10689 return linkage_name;
10690}
10691
94af9270 10692/* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
a766d390 10693 compute the physname for the object, which include a method's:
9c37b5ae 10694 - formal parameters (C++),
a766d390 10695 - receiver type (Go),
a766d390
DE
10696
10697 The term "physname" is a bit confusing.
10698 For C++, for example, it is the demangled name.
10699 For Go, for example, it's the mangled name.
94af9270 10700
af6b7be1
JB
10701 For Ada, return the DIE's linkage name rather than the fully qualified
10702 name. PHYSNAME is ignored..
10703
94af9270
KS
10704 The result is allocated on the objfile_obstack and canonicalized. */
10705
10706static const char *
15d034d0
TT
10707dwarf2_compute_name (const char *name,
10708 struct die_info *die, struct dwarf2_cu *cu,
94af9270
KS
10709 int physname)
10710{
518817b3 10711 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
bb5ed363 10712
94af9270
KS
10713 if (name == NULL)
10714 name = dwarf2_name (die, cu);
10715
2ee7123e
DE
10716 /* For Fortran GDB prefers DW_AT_*linkage_name for the physname if present
10717 but otherwise compute it by typename_concat inside GDB.
10718 FIXME: Actually this is not really true, or at least not always true.
10719 It's all very confusing. SYMBOL_SET_NAMES doesn't try to demangle
5e2db402 10720 Fortran names because there is no mangling standard. So new_symbol
2ee7123e
DE
10721 will set the demangled name to the result of dwarf2_full_name, and it is
10722 the demangled name that GDB uses if it exists. */
f55ee35c
JK
10723 if (cu->language == language_ada
10724 || (cu->language == language_fortran && physname))
10725 {
10726 /* For Ada unit, we prefer the linkage name over the name, as
10727 the former contains the exported name, which the user expects
10728 to be able to reference. Ideally, we want the user to be able
10729 to reference this entity using either natural or linkage name,
10730 but we haven't started looking at this enhancement yet. */
73b9be8b 10731 const char *linkage_name = dw2_linkage_name (die, cu);
f55ee35c 10732
2ee7123e
DE
10733 if (linkage_name != NULL)
10734 return linkage_name;
f55ee35c
JK
10735 }
10736
94af9270
KS
10737 /* These are the only languages we know how to qualify names in. */
10738 if (name != NULL
9c37b5ae 10739 && (cu->language == language_cplus
c44af4eb
TT
10740 || cu->language == language_fortran || cu->language == language_d
10741 || cu->language == language_rust))
94af9270
KS
10742 {
10743 if (die_needs_namespace (die, cu))
10744 {
0d5cff50 10745 const char *prefix;
34a68019 10746 const char *canonical_name = NULL;
94af9270 10747
d7e74731
PA
10748 string_file buf;
10749
94af9270 10750 prefix = determine_prefix (die, cu);
94af9270
KS
10751 if (*prefix != '\0')
10752 {
f55ee35c
JK
10753 char *prefixed_name = typename_concat (NULL, prefix, name,
10754 physname, cu);
9a619af0 10755
d7e74731 10756 buf.puts (prefixed_name);
94af9270
KS
10757 xfree (prefixed_name);
10758 }
10759 else
d7e74731 10760 buf.puts (name);
94af9270 10761
98bfdba5
PA
10762 /* Template parameters may be specified in the DIE's DW_AT_name, or
10763 as children with DW_TAG_template_type_param or
10764 DW_TAG_value_type_param. If the latter, add them to the name
10765 here. If the name already has template parameters, then
10766 skip this step; some versions of GCC emit both, and
10767 it is more efficient to use the pre-computed name.
10768
10769 Something to keep in mind about this process: it is very
10770 unlikely, or in some cases downright impossible, to produce
10771 something that will match the mangled name of a function.
10772 If the definition of the function has the same debug info,
10773 we should be able to match up with it anyway. But fallbacks
10774 using the minimal symbol, for instance to find a method
10775 implemented in a stripped copy of libstdc++, will not work.
10776 If we do not have debug info for the definition, we will have to
10777 match them up some other way.
10778
10779 When we do name matching there is a related problem with function
10780 templates; two instantiated function templates are allowed to
10781 differ only by their return types, which we do not add here. */
10782
10783 if (cu->language == language_cplus && strchr (name, '<') == NULL)
10784 {
10785 struct attribute *attr;
10786 struct die_info *child;
10787 int first = 1;
10788
10789 die->building_fullname = 1;
10790
10791 for (child = die->child; child != NULL; child = child->sibling)
10792 {
10793 struct type *type;
12df843f 10794 LONGEST value;
d521ce57 10795 const gdb_byte *bytes;
98bfdba5
PA
10796 struct dwarf2_locexpr_baton *baton;
10797 struct value *v;
10798
10799 if (child->tag != DW_TAG_template_type_param
10800 && child->tag != DW_TAG_template_value_param)
10801 continue;
10802
10803 if (first)
10804 {
d7e74731 10805 buf.puts ("<");
98bfdba5
PA
10806 first = 0;
10807 }
10808 else
d7e74731 10809 buf.puts (", ");
98bfdba5
PA
10810
10811 attr = dwarf2_attr (child, DW_AT_type, cu);
10812 if (attr == NULL)
10813 {
10814 complaint (&symfile_complaints,
10815 _("template parameter missing DW_AT_type"));
d7e74731 10816 buf.puts ("UNKNOWN_TYPE");
98bfdba5
PA
10817 continue;
10818 }
10819 type = die_type (child, cu);
10820
10821 if (child->tag == DW_TAG_template_type_param)
10822 {
d7e74731 10823 c_print_type (type, "", &buf, -1, 0, &type_print_raw_options);
98bfdba5
PA
10824 continue;
10825 }
10826
10827 attr = dwarf2_attr (child, DW_AT_const_value, cu);
10828 if (attr == NULL)
10829 {
10830 complaint (&symfile_complaints,
3e43a32a
MS
10831 _("template parameter missing "
10832 "DW_AT_const_value"));
d7e74731 10833 buf.puts ("UNKNOWN_VALUE");
98bfdba5
PA
10834 continue;
10835 }
10836
10837 dwarf2_const_value_attr (attr, type, name,
10838 &cu->comp_unit_obstack, cu,
10839 &value, &bytes, &baton);
10840
10841 if (TYPE_NOSIGN (type))
10842 /* GDB prints characters as NUMBER 'CHAR'. If that's
10843 changed, this can use value_print instead. */
d7e74731 10844 c_printchar (value, type, &buf);
98bfdba5
PA
10845 else
10846 {
10847 struct value_print_options opts;
10848
10849 if (baton != NULL)
10850 v = dwarf2_evaluate_loc_desc (type, NULL,
10851 baton->data,
10852 baton->size,
10853 baton->per_cu);
10854 else if (bytes != NULL)
10855 {
10856 v = allocate_value (type);
10857 memcpy (value_contents_writeable (v), bytes,
10858 TYPE_LENGTH (type));
10859 }
10860 else
10861 v = value_from_longest (type, value);
10862
3e43a32a
MS
10863 /* Specify decimal so that we do not depend on
10864 the radix. */
98bfdba5
PA
10865 get_formatted_print_options (&opts, 'd');
10866 opts.raw = 1;
d7e74731 10867 value_print (v, &buf, &opts);
98bfdba5 10868 release_value (v);
98bfdba5
PA
10869 }
10870 }
10871
10872 die->building_fullname = 0;
10873
10874 if (!first)
10875 {
10876 /* Close the argument list, with a space if necessary
10877 (nested templates). */
d7e74731
PA
10878 if (!buf.empty () && buf.string ().back () == '>')
10879 buf.puts (" >");
98bfdba5 10880 else
d7e74731 10881 buf.puts (">");
98bfdba5
PA
10882 }
10883 }
10884
9c37b5ae 10885 /* For C++ methods, append formal parameter type
94af9270 10886 information, if PHYSNAME. */
6e70227d 10887
94af9270 10888 if (physname && die->tag == DW_TAG_subprogram
9c37b5ae 10889 && cu->language == language_cplus)
94af9270
KS
10890 {
10891 struct type *type = read_type_die (die, cu);
10892
d7e74731 10893 c_type_print_args (type, &buf, 1, cu->language,
79d43c61 10894 &type_print_raw_options);
94af9270 10895
9c37b5ae 10896 if (cu->language == language_cplus)
94af9270 10897 {
60430eff
DJ
10898 /* Assume that an artificial first parameter is
10899 "this", but do not crash if it is not. RealView
10900 marks unnamed (and thus unused) parameters as
10901 artificial; there is no way to differentiate
10902 the two cases. */
94af9270
KS
10903 if (TYPE_NFIELDS (type) > 0
10904 && TYPE_FIELD_ARTIFICIAL (type, 0)
60430eff 10905 && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
3e43a32a
MS
10906 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
10907 0))))
d7e74731 10908 buf.puts (" const");
94af9270
KS
10909 }
10910 }
10911
d7e74731 10912 const std::string &intermediate_name = buf.string ();
94af9270
KS
10913
10914 if (cu->language == language_cplus)
34a68019 10915 canonical_name
322a8516 10916 = dwarf2_canonicalize_name (intermediate_name.c_str (), cu,
34a68019
TT
10917 &objfile->per_bfd->storage_obstack);
10918
10919 /* If we only computed INTERMEDIATE_NAME, or if
10920 INTERMEDIATE_NAME is already canonical, then we need to
10921 copy it to the appropriate obstack. */
322a8516 10922 if (canonical_name == NULL || canonical_name == intermediate_name.c_str ())
224c3ddb
SM
10923 name = ((const char *)
10924 obstack_copy0 (&objfile->per_bfd->storage_obstack,
322a8516
PA
10925 intermediate_name.c_str (),
10926 intermediate_name.length ()));
34a68019
TT
10927 else
10928 name = canonical_name;
94af9270
KS
10929 }
10930 }
10931
10932 return name;
10933}
10934
0114d602
DJ
10935/* Return the fully qualified name of DIE, based on its DW_AT_name.
10936 If scope qualifiers are appropriate they will be added. The result
34a68019 10937 will be allocated on the storage_obstack, or NULL if the DIE does
94af9270
KS
10938 not have a name. NAME may either be from a previous call to
10939 dwarf2_name or NULL.
10940
9c37b5ae 10941 The output string will be canonicalized (if C++). */
0114d602
DJ
10942
10943static const char *
15d034d0 10944dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
0114d602 10945{
94af9270
KS
10946 return dwarf2_compute_name (name, die, cu, 0);
10947}
0114d602 10948
94af9270
KS
10949/* Construct a physname for the given DIE in CU. NAME may either be
10950 from a previous call to dwarf2_name or NULL. The result will be
10951 allocated on the objfile_objstack or NULL if the DIE does not have a
10952 name.
0114d602 10953
9c37b5ae 10954 The output string will be canonicalized (if C++). */
0114d602 10955
94af9270 10956static const char *
15d034d0 10957dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
94af9270 10958{
518817b3 10959 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
900e11f9 10960 const char *retval, *mangled = NULL, *canon = NULL;
900e11f9
JK
10961 int need_copy = 1;
10962
10963 /* In this case dwarf2_compute_name is just a shortcut not building anything
10964 on its own. */
10965 if (!die_needs_namespace (die, cu))
10966 return dwarf2_compute_name (name, die, cu, 1);
10967
73b9be8b 10968 mangled = dw2_linkage_name (die, cu);
900e11f9 10969
e98c9e7c
TT
10970 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
10971 See https://github.com/rust-lang/rust/issues/32925. */
10972 if (cu->language == language_rust && mangled != NULL
10973 && strchr (mangled, '{') != NULL)
10974 mangled = NULL;
10975
900e11f9
JK
10976 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
10977 has computed. */
791afaa2 10978 gdb::unique_xmalloc_ptr<char> demangled;
7d45c7c3 10979 if (mangled != NULL)
900e11f9 10980 {
900e11f9 10981
59cc4834
JB
10982 if (language_def (cu->language)->la_store_sym_names_in_linkage_form_p)
10983 {
10984 /* Do nothing (do not demangle the symbol name). */
10985 }
10986 else if (cu->language == language_go)
a766d390 10987 {
5e2db402
TT
10988 /* This is a lie, but we already lie to the caller new_symbol.
10989 new_symbol assumes we return the mangled name.
a766d390 10990 This just undoes that lie until things are cleaned up. */
a766d390
DE
10991 }
10992 else
10993 {
0eb876f5
JB
10994 /* Use DMGL_RET_DROP for C++ template functions to suppress
10995 their return type. It is easier for GDB users to search
10996 for such functions as `name(params)' than `long name(params)'.
10997 In such case the minimal symbol names do not match the full
10998 symbol names but for template functions there is never a need
10999 to look up their definition from their declaration so
11000 the only disadvantage remains the minimal symbol variant
11001 `long name(params)' does not have the proper inferior type. */
791afaa2
TT
11002 demangled.reset (gdb_demangle (mangled,
11003 (DMGL_PARAMS | DMGL_ANSI
11004 | DMGL_RET_DROP)));
a766d390 11005 }
900e11f9 11006 if (demangled)
791afaa2 11007 canon = demangled.get ();
900e11f9
JK
11008 else
11009 {
11010 canon = mangled;
11011 need_copy = 0;
11012 }
11013 }
11014
11015 if (canon == NULL || check_physname)
11016 {
11017 const char *physname = dwarf2_compute_name (name, die, cu, 1);
11018
11019 if (canon != NULL && strcmp (physname, canon) != 0)
11020 {
11021 /* It may not mean a bug in GDB. The compiler could also
11022 compute DW_AT_linkage_name incorrectly. But in such case
11023 GDB would need to be bug-to-bug compatible. */
11024
11025 complaint (&symfile_complaints,
11026 _("Computed physname <%s> does not match demangled <%s> "
9d8780f0
SM
11027 "(from linkage <%s>) - DIE at %s [in module %s]"),
11028 physname, canon, mangled, sect_offset_str (die->sect_off),
4262abfb 11029 objfile_name (objfile));
900e11f9
JK
11030
11031 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
11032 is available here - over computed PHYSNAME. It is safer
11033 against both buggy GDB and buggy compilers. */
11034
11035 retval = canon;
11036 }
11037 else
11038 {
11039 retval = physname;
11040 need_copy = 0;
11041 }
11042 }
11043 else
11044 retval = canon;
11045
11046 if (need_copy)
224c3ddb
SM
11047 retval = ((const char *)
11048 obstack_copy0 (&objfile->per_bfd->storage_obstack,
11049 retval, strlen (retval)));
900e11f9 11050
900e11f9 11051 return retval;
0114d602
DJ
11052}
11053
74921315
KS
11054/* Inspect DIE in CU for a namespace alias. If one exists, record
11055 a new symbol for it.
11056
11057 Returns 1 if a namespace alias was recorded, 0 otherwise. */
11058
11059static int
11060read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu)
11061{
11062 struct attribute *attr;
11063
11064 /* If the die does not have a name, this is not a namespace
11065 alias. */
11066 attr = dwarf2_attr (die, DW_AT_name, cu);
11067 if (attr != NULL)
11068 {
11069 int num;
11070 struct die_info *d = die;
11071 struct dwarf2_cu *imported_cu = cu;
11072
11073 /* If the compiler has nested DW_AT_imported_declaration DIEs,
11074 keep inspecting DIEs until we hit the underlying import. */
11075#define MAX_NESTED_IMPORTED_DECLARATIONS 100
11076 for (num = 0; num < MAX_NESTED_IMPORTED_DECLARATIONS; ++num)
11077 {
11078 attr = dwarf2_attr (d, DW_AT_import, cu);
11079 if (attr == NULL)
11080 break;
11081
11082 d = follow_die_ref (d, attr, &imported_cu);
11083 if (d->tag != DW_TAG_imported_declaration)
11084 break;
11085 }
11086
11087 if (num == MAX_NESTED_IMPORTED_DECLARATIONS)
11088 {
11089 complaint (&symfile_complaints,
9d8780f0
SM
11090 _("DIE at %s has too many recursively imported "
11091 "declarations"), sect_offset_str (d->sect_off));
74921315
KS
11092 return 0;
11093 }
11094
11095 if (attr != NULL)
11096 {
11097 struct type *type;
9c541725 11098 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
74921315 11099
9c541725 11100 type = get_die_type_at_offset (sect_off, cu->per_cu);
74921315
KS
11101 if (type != NULL && TYPE_CODE (type) == TYPE_CODE_NAMESPACE)
11102 {
11103 /* This declaration is a global namespace alias. Add
11104 a symbol for it whose type is the aliased namespace. */
11105 new_symbol (die, type, cu);
11106 return 1;
11107 }
11108 }
11109 }
11110
11111 return 0;
11112}
11113
22cee43f
PMR
11114/* Return the using directives repository (global or local?) to use in the
11115 current context for LANGUAGE.
11116
11117 For Ada, imported declarations can materialize renamings, which *may* be
11118 global. However it is impossible (for now?) in DWARF to distinguish
11119 "external" imported declarations and "static" ones. As all imported
11120 declarations seem to be static in all other languages, make them all CU-wide
11121 global only in Ada. */
11122
11123static struct using_direct **
11124using_directives (enum language language)
11125{
11126 if (language == language_ada && context_stack_depth == 0)
11127 return &global_using_directives;
11128 else
11129 return &local_using_directives;
11130}
11131
27aa8d6a
SW
11132/* Read the import statement specified by the given die and record it. */
11133
11134static void
11135read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
11136{
518817b3 11137 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
27aa8d6a 11138 struct attribute *import_attr;
32019081 11139 struct die_info *imported_die, *child_die;
de4affc9 11140 struct dwarf2_cu *imported_cu;
27aa8d6a 11141 const char *imported_name;
794684b6 11142 const char *imported_name_prefix;
13387711
SW
11143 const char *canonical_name;
11144 const char *import_alias;
11145 const char *imported_declaration = NULL;
794684b6 11146 const char *import_prefix;
eb1e02fd 11147 std::vector<const char *> excludes;
13387711 11148
27aa8d6a
SW
11149 import_attr = dwarf2_attr (die, DW_AT_import, cu);
11150 if (import_attr == NULL)
11151 {
11152 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
11153 dwarf_tag_name (die->tag));
11154 return;
11155 }
11156
de4affc9
CC
11157 imported_cu = cu;
11158 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
11159 imported_name = dwarf2_name (imported_die, imported_cu);
27aa8d6a
SW
11160 if (imported_name == NULL)
11161 {
11162 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
11163
11164 The import in the following code:
11165 namespace A
11166 {
11167 typedef int B;
11168 }
11169
11170 int main ()
11171 {
11172 using A::B;
11173 B b;
11174 return b;
11175 }
11176
11177 ...
11178 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
11179 <52> DW_AT_decl_file : 1
11180 <53> DW_AT_decl_line : 6
11181 <54> DW_AT_import : <0x75>
11182 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
11183 <59> DW_AT_name : B
11184 <5b> DW_AT_decl_file : 1
11185 <5c> DW_AT_decl_line : 2
11186 <5d> DW_AT_type : <0x6e>
11187 ...
11188 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
11189 <76> DW_AT_byte_size : 4
11190 <77> DW_AT_encoding : 5 (signed)
11191
11192 imports the wrong die ( 0x75 instead of 0x58 ).
11193 This case will be ignored until the gcc bug is fixed. */
11194 return;
11195 }
11196
82856980
SW
11197 /* Figure out the local name after import. */
11198 import_alias = dwarf2_name (die, cu);
27aa8d6a 11199
794684b6
SW
11200 /* Figure out where the statement is being imported to. */
11201 import_prefix = determine_prefix (die, cu);
11202
11203 /* Figure out what the scope of the imported die is and prepend it
11204 to the name of the imported die. */
de4affc9 11205 imported_name_prefix = determine_prefix (imported_die, imported_cu);
794684b6 11206
f55ee35c
JK
11207 if (imported_die->tag != DW_TAG_namespace
11208 && imported_die->tag != DW_TAG_module)
794684b6 11209 {
13387711
SW
11210 imported_declaration = imported_name;
11211 canonical_name = imported_name_prefix;
794684b6 11212 }
13387711 11213 else if (strlen (imported_name_prefix) > 0)
12aaed36 11214 canonical_name = obconcat (&objfile->objfile_obstack,
45280282
IB
11215 imported_name_prefix,
11216 (cu->language == language_d ? "." : "::"),
11217 imported_name, (char *) NULL);
13387711
SW
11218 else
11219 canonical_name = imported_name;
794684b6 11220
32019081
JK
11221 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
11222 for (child_die = die->child; child_die && child_die->tag;
11223 child_die = sibling_die (child_die))
11224 {
11225 /* DWARF-4: A Fortran use statement with a “rename list” may be
11226 represented by an imported module entry with an import attribute
11227 referring to the module and owned entries corresponding to those
11228 entities that are renamed as part of being imported. */
11229
11230 if (child_die->tag != DW_TAG_imported_declaration)
11231 {
11232 complaint (&symfile_complaints,
11233 _("child DW_TAG_imported_declaration expected "
9d8780f0
SM
11234 "- DIE at %s [in module %s]"),
11235 sect_offset_str (child_die->sect_off),
11236 objfile_name (objfile));
32019081
JK
11237 continue;
11238 }
11239
11240 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
11241 if (import_attr == NULL)
11242 {
11243 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
11244 dwarf_tag_name (child_die->tag));
11245 continue;
11246 }
11247
11248 imported_cu = cu;
11249 imported_die = follow_die_ref_or_sig (child_die, import_attr,
11250 &imported_cu);
11251 imported_name = dwarf2_name (imported_die, imported_cu);
11252 if (imported_name == NULL)
11253 {
11254 complaint (&symfile_complaints,
11255 _("child DW_TAG_imported_declaration has unknown "
9d8780f0
SM
11256 "imported name - DIE at %s [in module %s]"),
11257 sect_offset_str (child_die->sect_off),
11258 objfile_name (objfile));
32019081
JK
11259 continue;
11260 }
11261
eb1e02fd 11262 excludes.push_back (imported_name);
32019081
JK
11263
11264 process_die (child_die, cu);
11265 }
11266
22cee43f
PMR
11267 add_using_directive (using_directives (cu->language),
11268 import_prefix,
11269 canonical_name,
11270 import_alias,
11271 imported_declaration,
11272 excludes,
11273 0,
11274 &objfile->objfile_obstack);
27aa8d6a
SW
11275}
11276
5230b05a
WT
11277/* ICC<14 does not output the required DW_AT_declaration on incomplete
11278 types, but gives them a size of zero. Starting with version 14,
11279 ICC is compatible with GCC. */
11280
11281static int
11282producer_is_icc_lt_14 (struct dwarf2_cu *cu)
11283{
11284 if (!cu->checked_producer)
11285 check_producer (cu);
11286
11287 return cu->producer_is_icc_lt_14;
11288}
11289
1b80a9fa
JK
11290/* Check for possibly missing DW_AT_comp_dir with relative .debug_line
11291 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
11292 this, it was first present in GCC release 4.3.0. */
11293
11294static int
11295producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
11296{
11297 if (!cu->checked_producer)
11298 check_producer (cu);
11299
11300 return cu->producer_is_gcc_lt_4_3;
11301}
11302
d721ba37
PA
11303static file_and_directory
11304find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu)
9291a0cd 11305{
d721ba37
PA
11306 file_and_directory res;
11307
9291a0cd
TT
11308 /* Find the filename. Do not use dwarf2_name here, since the filename
11309 is not a source language identifier. */
d721ba37
PA
11310 res.name = dwarf2_string_attr (die, DW_AT_name, cu);
11311 res.comp_dir = dwarf2_string_attr (die, DW_AT_comp_dir, cu);
9291a0cd 11312
d721ba37
PA
11313 if (res.comp_dir == NULL
11314 && producer_is_gcc_lt_4_3 (cu) && res.name != NULL
11315 && IS_ABSOLUTE_PATH (res.name))
9291a0cd 11316 {
d721ba37
PA
11317 res.comp_dir_storage = ldirname (res.name);
11318 if (!res.comp_dir_storage.empty ())
11319 res.comp_dir = res.comp_dir_storage.c_str ();
9291a0cd 11320 }
d721ba37 11321 if (res.comp_dir != NULL)
9291a0cd
TT
11322 {
11323 /* Irix 6.2 native cc prepends <machine>.: to the compilation
11324 directory, get rid of it. */
d721ba37 11325 const char *cp = strchr (res.comp_dir, ':');
9291a0cd 11326
d721ba37
PA
11327 if (cp && cp != res.comp_dir && cp[-1] == '.' && cp[1] == '/')
11328 res.comp_dir = cp + 1;
9291a0cd
TT
11329 }
11330
d721ba37
PA
11331 if (res.name == NULL)
11332 res.name = "<unknown>";
11333
11334 return res;
9291a0cd
TT
11335}
11336
f4dc4d17
DE
11337/* Handle DW_AT_stmt_list for a compilation unit.
11338 DIE is the DW_TAG_compile_unit die for CU.
c3b7b696
YQ
11339 COMP_DIR is the compilation directory. LOWPC is passed to
11340 dwarf_decode_lines. See dwarf_decode_lines comments about it. */
2ab95328
TT
11341
11342static void
11343handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
c3b7b696 11344 const char *comp_dir, CORE_ADDR lowpc) /* ARI: editCase function */
2ab95328 11345{
518817b3
SM
11346 struct dwarf2_per_objfile *dwarf2_per_objfile
11347 = cu->per_cu->dwarf2_per_objfile;
527f3840 11348 struct objfile *objfile = dwarf2_per_objfile->objfile;
2ab95328 11349 struct attribute *attr;
527f3840
JK
11350 struct line_header line_header_local;
11351 hashval_t line_header_local_hash;
527f3840
JK
11352 void **slot;
11353 int decode_mapping;
2ab95328 11354
f4dc4d17
DE
11355 gdb_assert (! cu->per_cu->is_debug_types);
11356
2ab95328 11357 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
527f3840
JK
11358 if (attr == NULL)
11359 return;
11360
9c541725 11361 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
527f3840
JK
11362
11363 /* The line header hash table is only created if needed (it exists to
11364 prevent redundant reading of the line table for partial_units).
11365 If we're given a partial_unit, we'll need it. If we're given a
11366 compile_unit, then use the line header hash table if it's already
11367 created, but don't create one just yet. */
11368
11369 if (dwarf2_per_objfile->line_header_hash == NULL
11370 && die->tag == DW_TAG_partial_unit)
2ab95328 11371 {
527f3840
JK
11372 dwarf2_per_objfile->line_header_hash
11373 = htab_create_alloc_ex (127, line_header_hash_voidp,
11374 line_header_eq_voidp,
11375 free_line_header_voidp,
11376 &objfile->objfile_obstack,
11377 hashtab_obstack_allocate,
11378 dummy_obstack_deallocate);
11379 }
2ab95328 11380
9c541725 11381 line_header_local.sect_off = line_offset;
527f3840
JK
11382 line_header_local.offset_in_dwz = cu->per_cu->is_dwz;
11383 line_header_local_hash = line_header_hash (&line_header_local);
11384 if (dwarf2_per_objfile->line_header_hash != NULL)
11385 {
11386 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash,
11387 &line_header_local,
11388 line_header_local_hash, NO_INSERT);
11389
11390 /* For DW_TAG_compile_unit we need info like symtab::linetable which
11391 is not present in *SLOT (since if there is something in *SLOT then
11392 it will be for a partial_unit). */
11393 if (die->tag == DW_TAG_partial_unit && slot != NULL)
dee91e82 11394 {
527f3840 11395 gdb_assert (*slot != NULL);
9a3c8263 11396 cu->line_header = (struct line_header *) *slot;
527f3840 11397 return;
dee91e82 11398 }
2ab95328 11399 }
527f3840
JK
11400
11401 /* dwarf_decode_line_header does not yet provide sufficient information.
11402 We always have to call also dwarf_decode_lines for it. */
fff8551c
PA
11403 line_header_up lh = dwarf_decode_line_header (line_offset, cu);
11404 if (lh == NULL)
527f3840 11405 return;
4c8aa72d
PA
11406
11407 cu->line_header = lh.release ();
11408 cu->line_header_die_owner = die;
527f3840
JK
11409
11410 if (dwarf2_per_objfile->line_header_hash == NULL)
11411 slot = NULL;
11412 else
11413 {
11414 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash,
11415 &line_header_local,
11416 line_header_local_hash, INSERT);
11417 gdb_assert (slot != NULL);
11418 }
11419 if (slot != NULL && *slot == NULL)
11420 {
11421 /* This newly decoded line number information unit will be owned
11422 by line_header_hash hash table. */
11423 *slot = cu->line_header;
4c8aa72d 11424 cu->line_header_die_owner = NULL;
527f3840
JK
11425 }
11426 else
11427 {
11428 /* We cannot free any current entry in (*slot) as that struct line_header
11429 may be already used by multiple CUs. Create only temporary decoded
11430 line_header for this CU - it may happen at most once for each line
11431 number information unit. And if we're not using line_header_hash
11432 then this is what we want as well. */
11433 gdb_assert (die->tag != DW_TAG_partial_unit);
527f3840
JK
11434 }
11435 decode_mapping = (die->tag != DW_TAG_partial_unit);
11436 dwarf_decode_lines (cu->line_header, comp_dir, cu, NULL, lowpc,
11437 decode_mapping);
fff8551c 11438
2ab95328
TT
11439}
11440
95554aad 11441/* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
ae2de4f8 11442
c906108c 11443static void
e7c27a73 11444read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 11445{
518817b3
SM
11446 struct dwarf2_per_objfile *dwarf2_per_objfile
11447 = cu->per_cu->dwarf2_per_objfile;
dee91e82 11448 struct objfile *objfile = dwarf2_per_objfile->objfile;
3e29f34a 11449 struct gdbarch *gdbarch = get_objfile_arch (objfile);
2acceee2 11450 CORE_ADDR lowpc = ((CORE_ADDR) -1);
c906108c
SS
11451 CORE_ADDR highpc = ((CORE_ADDR) 0);
11452 struct attribute *attr;
c906108c 11453 struct die_info *child_die;
e142c38c 11454 CORE_ADDR baseaddr;
6e70227d 11455
e142c38c 11456 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 11457
fae299cd 11458 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
c906108c
SS
11459
11460 /* If we didn't find a lowpc, set it to highpc to avoid complaints
11461 from finish_block. */
2acceee2 11462 if (lowpc == ((CORE_ADDR) -1))
c906108c 11463 lowpc = highpc;
3e29f34a 11464 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
c906108c 11465
d721ba37 11466 file_and_directory fnd = find_file_and_directory (die, cu);
e1024ff1 11467
95554aad 11468 prepare_one_comp_unit (cu, die, cu->language);
303b6f5d 11469
f4b8a18d
KW
11470 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
11471 standardised yet. As a workaround for the language detection we fall
11472 back to the DW_AT_producer string. */
11473 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
11474 cu->language = language_opencl;
11475
3019eac3
DE
11476 /* Similar hack for Go. */
11477 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
11478 set_cu_language (DW_LANG_Go, cu);
11479
d721ba37 11480 dwarf2_start_symtab (cu, fnd.name, fnd.comp_dir, lowpc);
3019eac3
DE
11481
11482 /* Decode line number information if present. We do this before
11483 processing child DIEs, so that the line header table is available
11484 for DW_AT_decl_file. */
d721ba37 11485 handle_DW_AT_stmt_list (die, cu, fnd.comp_dir, lowpc);
3019eac3
DE
11486
11487 /* Process all dies in compilation unit. */
11488 if (die->child != NULL)
11489 {
11490 child_die = die->child;
11491 while (child_die && child_die->tag)
11492 {
11493 process_die (child_die, cu);
11494 child_die = sibling_die (child_die);
11495 }
11496 }
11497
11498 /* Decode macro information, if present. Dwarf 2 macro information
11499 refers to information in the line number info statement program
11500 header, so we can only read it if we've read the header
11501 successfully. */
0af92d60
JK
11502 attr = dwarf2_attr (die, DW_AT_macros, cu);
11503 if (attr == NULL)
11504 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
3019eac3
DE
11505 if (attr && cu->line_header)
11506 {
11507 if (dwarf2_attr (die, DW_AT_macro_info, cu))
11508 complaint (&symfile_complaints,
0af92d60 11509 _("CU refers to both DW_AT_macros and DW_AT_macro_info"));
3019eac3 11510
43f3e411 11511 dwarf_decode_macros (cu, DW_UNSND (attr), 1);
3019eac3
DE
11512 }
11513 else
11514 {
11515 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
11516 if (attr && cu->line_header)
11517 {
11518 unsigned int macro_offset = DW_UNSND (attr);
11519
43f3e411 11520 dwarf_decode_macros (cu, macro_offset, 0);
3019eac3
DE
11521 }
11522 }
3019eac3
DE
11523}
11524
f4dc4d17
DE
11525/* TU version of handle_DW_AT_stmt_list for read_type_unit_scope.
11526 Create the set of symtabs used by this TU, or if this TU is sharing
11527 symtabs with another TU and the symtabs have already been created
11528 then restore those symtabs in the line header.
11529 We don't need the pc/line-number mapping for type units. */
3019eac3
DE
11530
11531static void
f4dc4d17 11532setup_type_unit_groups (struct die_info *die, struct dwarf2_cu *cu)
3019eac3 11533{
f4dc4d17
DE
11534 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
11535 struct type_unit_group *tu_group;
11536 int first_time;
3019eac3 11537 struct attribute *attr;
9c541725 11538 unsigned int i;
0186c6a7 11539 struct signatured_type *sig_type;
3019eac3 11540
f4dc4d17 11541 gdb_assert (per_cu->is_debug_types);
0186c6a7 11542 sig_type = (struct signatured_type *) per_cu;
3019eac3 11543
f4dc4d17 11544 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
3019eac3 11545
f4dc4d17 11546 /* If we're using .gdb_index (includes -readnow) then
74e04d1c 11547 per_cu->type_unit_group may not have been set up yet. */
0186c6a7
DE
11548 if (sig_type->type_unit_group == NULL)
11549 sig_type->type_unit_group = get_type_unit_group (cu, attr);
11550 tu_group = sig_type->type_unit_group;
f4dc4d17
DE
11551
11552 /* If we've already processed this stmt_list there's no real need to
11553 do it again, we could fake it and just recreate the part we need
11554 (file name,index -> symtab mapping). If data shows this optimization
11555 is useful we can do it then. */
43f3e411 11556 first_time = tu_group->compunit_symtab == NULL;
f4dc4d17
DE
11557
11558 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
11559 debug info. */
fff8551c 11560 line_header_up lh;
f4dc4d17 11561 if (attr != NULL)
3019eac3 11562 {
9c541725 11563 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
f4dc4d17
DE
11564 lh = dwarf_decode_line_header (line_offset, cu);
11565 }
11566 if (lh == NULL)
11567 {
11568 if (first_time)
11569 dwarf2_start_symtab (cu, "", NULL, 0);
11570 else
11571 {
11572 gdb_assert (tu_group->symtabs == NULL);
0ab9ce85 11573 restart_symtab (tu_group->compunit_symtab, "", 0);
f4dc4d17 11574 }
f4dc4d17 11575 return;
3019eac3
DE
11576 }
11577
4c8aa72d
PA
11578 cu->line_header = lh.release ();
11579 cu->line_header_die_owner = die;
3019eac3 11580
f4dc4d17
DE
11581 if (first_time)
11582 {
43f3e411 11583 struct compunit_symtab *cust = dwarf2_start_symtab (cu, "", NULL, 0);
3019eac3 11584
1fd60fc0
DE
11585 /* Note: We don't assign tu_group->compunit_symtab yet because we're
11586 still initializing it, and our caller (a few levels up)
11587 process_full_type_unit still needs to know if this is the first
11588 time. */
11589
4c8aa72d
PA
11590 tu_group->num_symtabs = cu->line_header->file_names.size ();
11591 tu_group->symtabs = XNEWVEC (struct symtab *,
11592 cu->line_header->file_names.size ());
3019eac3 11593
4c8aa72d 11594 for (i = 0; i < cu->line_header->file_names.size (); ++i)
f4dc4d17 11595 {
4c8aa72d 11596 file_entry &fe = cu->line_header->file_names[i];
3019eac3 11597
4c8aa72d 11598 dwarf2_start_subfile (fe.name, fe.include_dir (cu->line_header));
3019eac3 11599
f4dc4d17
DE
11600 if (current_subfile->symtab == NULL)
11601 {
4c8aa72d
PA
11602 /* NOTE: start_subfile will recognize when it's been
11603 passed a file it has already seen. So we can't
11604 assume there's a simple mapping from
11605 cu->line_header->file_names to subfiles, plus
11606 cu->line_header->file_names may contain dups. */
43f3e411
DE
11607 current_subfile->symtab
11608 = allocate_symtab (cust, current_subfile->name);
f4dc4d17
DE
11609 }
11610
8c43009f
PA
11611 fe.symtab = current_subfile->symtab;
11612 tu_group->symtabs[i] = fe.symtab;
f4dc4d17
DE
11613 }
11614 }
11615 else
3019eac3 11616 {
0ab9ce85 11617 restart_symtab (tu_group->compunit_symtab, "", 0);
f4dc4d17 11618
4c8aa72d 11619 for (i = 0; i < cu->line_header->file_names.size (); ++i)
f4dc4d17 11620 {
4c8aa72d 11621 file_entry &fe = cu->line_header->file_names[i];
f4dc4d17 11622
4c8aa72d 11623 fe.symtab = tu_group->symtabs[i];
f4dc4d17 11624 }
3019eac3
DE
11625 }
11626
f4dc4d17
DE
11627 /* The main symtab is allocated last. Type units don't have DW_AT_name
11628 so they don't have a "real" (so to speak) symtab anyway.
11629 There is later code that will assign the main symtab to all symbols
11630 that don't have one. We need to handle the case of a symbol with a
11631 missing symtab (DW_AT_decl_file) anyway. */
11632}
3019eac3 11633
f4dc4d17
DE
11634/* Process DW_TAG_type_unit.
11635 For TUs we want to skip the first top level sibling if it's not the
11636 actual type being defined by this TU. In this case the first top
11637 level sibling is there to provide context only. */
3019eac3 11638
f4dc4d17
DE
11639static void
11640read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
11641{
11642 struct die_info *child_die;
3019eac3 11643
f4dc4d17
DE
11644 prepare_one_comp_unit (cu, die, language_minimal);
11645
11646 /* Initialize (or reinitialize) the machinery for building symtabs.
11647 We do this before processing child DIEs, so that the line header table
11648 is available for DW_AT_decl_file. */
11649 setup_type_unit_groups (die, cu);
11650
11651 if (die->child != NULL)
11652 {
11653 child_die = die->child;
11654 while (child_die && child_die->tag)
11655 {
11656 process_die (child_die, cu);
11657 child_die = sibling_die (child_die);
11658 }
11659 }
3019eac3
DE
11660}
11661\f
80626a55
DE
11662/* DWO/DWP files.
11663
11664 http://gcc.gnu.org/wiki/DebugFission
11665 http://gcc.gnu.org/wiki/DebugFissionDWP
11666
11667 To simplify handling of both DWO files ("object" files with the DWARF info)
11668 and DWP files (a file with the DWOs packaged up into one file), we treat
11669 DWP files as having a collection of virtual DWO files. */
3019eac3
DE
11670
11671static hashval_t
11672hash_dwo_file (const void *item)
11673{
9a3c8263 11674 const struct dwo_file *dwo_file = (const struct dwo_file *) item;
a2ce51a0 11675 hashval_t hash;
3019eac3 11676
a2ce51a0
DE
11677 hash = htab_hash_string (dwo_file->dwo_name);
11678 if (dwo_file->comp_dir != NULL)
11679 hash += htab_hash_string (dwo_file->comp_dir);
11680 return hash;
3019eac3
DE
11681}
11682
11683static int
11684eq_dwo_file (const void *item_lhs, const void *item_rhs)
11685{
9a3c8263
SM
11686 const struct dwo_file *lhs = (const struct dwo_file *) item_lhs;
11687 const struct dwo_file *rhs = (const struct dwo_file *) item_rhs;
3019eac3 11688
a2ce51a0
DE
11689 if (strcmp (lhs->dwo_name, rhs->dwo_name) != 0)
11690 return 0;
11691 if (lhs->comp_dir == NULL || rhs->comp_dir == NULL)
11692 return lhs->comp_dir == rhs->comp_dir;
11693 return strcmp (lhs->comp_dir, rhs->comp_dir) == 0;
3019eac3
DE
11694}
11695
11696/* Allocate a hash table for DWO files. */
11697
11698static htab_t
ed2dc618 11699allocate_dwo_file_hash_table (struct objfile *objfile)
3019eac3 11700{
3019eac3
DE
11701 return htab_create_alloc_ex (41,
11702 hash_dwo_file,
11703 eq_dwo_file,
11704 NULL,
11705 &objfile->objfile_obstack,
11706 hashtab_obstack_allocate,
11707 dummy_obstack_deallocate);
11708}
11709
80626a55
DE
11710/* Lookup DWO file DWO_NAME. */
11711
11712static void **
ed2dc618
SM
11713lookup_dwo_file_slot (struct dwarf2_per_objfile *dwarf2_per_objfile,
11714 const char *dwo_name,
11715 const char *comp_dir)
80626a55
DE
11716{
11717 struct dwo_file find_entry;
11718 void **slot;
11719
11720 if (dwarf2_per_objfile->dwo_files == NULL)
ed2dc618
SM
11721 dwarf2_per_objfile->dwo_files
11722 = allocate_dwo_file_hash_table (dwarf2_per_objfile->objfile);
80626a55
DE
11723
11724 memset (&find_entry, 0, sizeof (find_entry));
0ac5b59e
DE
11725 find_entry.dwo_name = dwo_name;
11726 find_entry.comp_dir = comp_dir;
80626a55
DE
11727 slot = htab_find_slot (dwarf2_per_objfile->dwo_files, &find_entry, INSERT);
11728
11729 return slot;
11730}
11731
3019eac3
DE
11732static hashval_t
11733hash_dwo_unit (const void *item)
11734{
9a3c8263 11735 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
3019eac3
DE
11736
11737 /* This drops the top 32 bits of the id, but is ok for a hash. */
11738 return dwo_unit->signature;
11739}
11740
11741static int
11742eq_dwo_unit (const void *item_lhs, const void *item_rhs)
11743{
9a3c8263
SM
11744 const struct dwo_unit *lhs = (const struct dwo_unit *) item_lhs;
11745 const struct dwo_unit *rhs = (const struct dwo_unit *) item_rhs;
3019eac3
DE
11746
11747 /* The signature is assumed to be unique within the DWO file.
11748 So while object file CU dwo_id's always have the value zero,
11749 that's OK, assuming each object file DWO file has only one CU,
11750 and that's the rule for now. */
11751 return lhs->signature == rhs->signature;
11752}
11753
11754/* Allocate a hash table for DWO CUs,TUs.
11755 There is one of these tables for each of CUs,TUs for each DWO file. */
11756
11757static htab_t
11758allocate_dwo_unit_table (struct objfile *objfile)
11759{
11760 /* Start out with a pretty small number.
11761 Generally DWO files contain only one CU and maybe some TUs. */
11762 return htab_create_alloc_ex (3,
11763 hash_dwo_unit,
11764 eq_dwo_unit,
11765 NULL,
11766 &objfile->objfile_obstack,
11767 hashtab_obstack_allocate,
11768 dummy_obstack_deallocate);
11769}
11770
80626a55 11771/* Structure used to pass data to create_dwo_debug_info_hash_table_reader. */
3019eac3 11772
19c3d4c9 11773struct create_dwo_cu_data
3019eac3
DE
11774{
11775 struct dwo_file *dwo_file;
19c3d4c9 11776 struct dwo_unit dwo_unit;
3019eac3
DE
11777};
11778
19c3d4c9 11779/* die_reader_func for create_dwo_cu. */
3019eac3
DE
11780
11781static void
19c3d4c9
DE
11782create_dwo_cu_reader (const struct die_reader_specs *reader,
11783 const gdb_byte *info_ptr,
11784 struct die_info *comp_unit_die,
11785 int has_children,
11786 void *datap)
3019eac3
DE
11787{
11788 struct dwarf2_cu *cu = reader->cu;
9c541725 11789 sect_offset sect_off = cu->per_cu->sect_off;
8a0459fd 11790 struct dwarf2_section_info *section = cu->per_cu->section;
9a3c8263 11791 struct create_dwo_cu_data *data = (struct create_dwo_cu_data *) datap;
3019eac3 11792 struct dwo_file *dwo_file = data->dwo_file;
19c3d4c9 11793 struct dwo_unit *dwo_unit = &data->dwo_unit;
3019eac3 11794 struct attribute *attr;
3019eac3
DE
11795
11796 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
11797 if (attr == NULL)
11798 {
19c3d4c9 11799 complaint (&symfile_complaints,
9d8780f0 11800 _("Dwarf Error: debug entry at offset %s is missing"
19c3d4c9 11801 " its dwo_id [in module %s]"),
9d8780f0 11802 sect_offset_str (sect_off), dwo_file->dwo_name);
3019eac3
DE
11803 return;
11804 }
11805
3019eac3
DE
11806 dwo_unit->dwo_file = dwo_file;
11807 dwo_unit->signature = DW_UNSND (attr);
8a0459fd 11808 dwo_unit->section = section;
9c541725 11809 dwo_unit->sect_off = sect_off;
3019eac3
DE
11810 dwo_unit->length = cu->per_cu->length;
11811
b4f54984 11812 if (dwarf_read_debug)
9d8780f0
SM
11813 fprintf_unfiltered (gdb_stdlog, " offset %s, dwo_id %s\n",
11814 sect_offset_str (sect_off),
9c541725 11815 hex_string (dwo_unit->signature));
3019eac3
DE
11816}
11817
33c5cd75 11818/* Create the dwo_units for the CUs in a DWO_FILE.
19c3d4c9 11819 Note: This function processes DWO files only, not DWP files. */
3019eac3 11820
33c5cd75 11821static void
ed2dc618
SM
11822create_cus_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
11823 struct dwo_file &dwo_file, dwarf2_section_info &section,
33c5cd75 11824 htab_t &cus_htab)
3019eac3
DE
11825{
11826 struct objfile *objfile = dwarf2_per_objfile->objfile;
d521ce57 11827 const gdb_byte *info_ptr, *end_ptr;
3019eac3 11828
33c5cd75
DB
11829 dwarf2_read_section (objfile, &section);
11830 info_ptr = section.buffer;
3019eac3
DE
11831
11832 if (info_ptr == NULL)
33c5cd75 11833 return;
3019eac3 11834
b4f54984 11835 if (dwarf_read_debug)
19c3d4c9
DE
11836 {
11837 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
33c5cd75
DB
11838 get_section_name (&section),
11839 get_section_file_name (&section));
19c3d4c9 11840 }
3019eac3 11841
33c5cd75 11842 end_ptr = info_ptr + section.size;
3019eac3
DE
11843 while (info_ptr < end_ptr)
11844 {
11845 struct dwarf2_per_cu_data per_cu;
33c5cd75
DB
11846 struct create_dwo_cu_data create_dwo_cu_data;
11847 struct dwo_unit *dwo_unit;
11848 void **slot;
11849 sect_offset sect_off = (sect_offset) (info_ptr - section.buffer);
3019eac3 11850
19c3d4c9
DE
11851 memset (&create_dwo_cu_data.dwo_unit, 0,
11852 sizeof (create_dwo_cu_data.dwo_unit));
3019eac3 11853 memset (&per_cu, 0, sizeof (per_cu));
e3b94546 11854 per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
3019eac3 11855 per_cu.is_debug_types = 0;
33c5cd75
DB
11856 per_cu.sect_off = sect_offset (info_ptr - section.buffer);
11857 per_cu.section = &section;
c5ed0576 11858 create_dwo_cu_data.dwo_file = &dwo_file;
33c5cd75
DB
11859
11860 init_cutu_and_read_dies_no_follow (
11861 &per_cu, &dwo_file, create_dwo_cu_reader, &create_dwo_cu_data);
11862 info_ptr += per_cu.length;
11863
11864 // If the unit could not be parsed, skip it.
11865 if (create_dwo_cu_data.dwo_unit.dwo_file == NULL)
11866 continue;
3019eac3 11867
33c5cd75
DB
11868 if (cus_htab == NULL)
11869 cus_htab = allocate_dwo_unit_table (objfile);
19c3d4c9 11870
33c5cd75
DB
11871 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
11872 *dwo_unit = create_dwo_cu_data.dwo_unit;
11873 slot = htab_find_slot (cus_htab, dwo_unit, INSERT);
11874 gdb_assert (slot != NULL);
11875 if (*slot != NULL)
19c3d4c9 11876 {
33c5cd75
DB
11877 const struct dwo_unit *dup_cu = (const struct dwo_unit *)*slot;
11878 sect_offset dup_sect_off = dup_cu->sect_off;
19c3d4c9 11879
33c5cd75 11880 complaint (&symfile_complaints,
9d8780f0
SM
11881 _("debug cu entry at offset %s is duplicate to"
11882 " the entry at offset %s, signature %s"),
11883 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
33c5cd75 11884 hex_string (dwo_unit->signature));
19c3d4c9 11885 }
33c5cd75 11886 *slot = (void *)dwo_unit;
3019eac3 11887 }
3019eac3
DE
11888}
11889
80626a55
DE
11890/* DWP file .debug_{cu,tu}_index section format:
11891 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
11892
d2415c6c
DE
11893 DWP Version 1:
11894
80626a55
DE
11895 Both index sections have the same format, and serve to map a 64-bit
11896 signature to a set of section numbers. Each section begins with a header,
11897 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
11898 indexes, and a pool of 32-bit section numbers. The index sections will be
11899 aligned at 8-byte boundaries in the file.
11900
d2415c6c
DE
11901 The index section header consists of:
11902
11903 V, 32 bit version number
11904 -, 32 bits unused
11905 N, 32 bit number of compilation units or type units in the index
11906 M, 32 bit number of slots in the hash table
80626a55 11907
d2415c6c 11908 Numbers are recorded using the byte order of the application binary.
80626a55 11909
d2415c6c
DE
11910 The hash table begins at offset 16 in the section, and consists of an array
11911 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
11912 order of the application binary). Unused slots in the hash table are 0.
11913 (We rely on the extreme unlikeliness of a signature being exactly 0.)
80626a55 11914
d2415c6c
DE
11915 The parallel table begins immediately after the hash table
11916 (at offset 16 + 8 * M from the beginning of the section), and consists of an
11917 array of 32-bit indexes (using the byte order of the application binary),
11918 corresponding 1-1 with slots in the hash table. Each entry in the parallel
11919 table contains a 32-bit index into the pool of section numbers. For unused
11920 hash table slots, the corresponding entry in the parallel table will be 0.
80626a55 11921
73869dc2
DE
11922 The pool of section numbers begins immediately following the hash table
11923 (at offset 16 + 12 * M from the beginning of the section). The pool of
11924 section numbers consists of an array of 32-bit words (using the byte order
11925 of the application binary). Each item in the array is indexed starting
11926 from 0. The hash table entry provides the index of the first section
11927 number in the set. Additional section numbers in the set follow, and the
11928 set is terminated by a 0 entry (section number 0 is not used in ELF).
11929
11930 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
11931 section must be the first entry in the set, and the .debug_abbrev.dwo must
11932 be the second entry. Other members of the set may follow in any order.
11933
11934 ---
11935
11936 DWP Version 2:
11937
11938 DWP Version 2 combines all the .debug_info, etc. sections into one,
11939 and the entries in the index tables are now offsets into these sections.
11940 CU offsets begin at 0. TU offsets begin at the size of the .debug_info
11941 section.
11942
11943 Index Section Contents:
11944 Header
11945 Hash Table of Signatures dwp_hash_table.hash_table
11946 Parallel Table of Indices dwp_hash_table.unit_table
11947 Table of Section Offsets dwp_hash_table.v2.{section_ids,offsets}
11948 Table of Section Sizes dwp_hash_table.v2.sizes
11949
11950 The index section header consists of:
11951
11952 V, 32 bit version number
11953 L, 32 bit number of columns in the table of section offsets
11954 N, 32 bit number of compilation units or type units in the index
11955 M, 32 bit number of slots in the hash table
11956
11957 Numbers are recorded using the byte order of the application binary.
11958
11959 The hash table has the same format as version 1.
11960 The parallel table of indices has the same format as version 1,
11961 except that the entries are origin-1 indices into the table of sections
11962 offsets and the table of section sizes.
11963
11964 The table of offsets begins immediately following the parallel table
11965 (at offset 16 + 12 * M from the beginning of the section). The table is
11966 a two-dimensional array of 32-bit words (using the byte order of the
11967 application binary), with L columns and N+1 rows, in row-major order.
11968 Each row in the array is indexed starting from 0. The first row provides
11969 a key to the remaining rows: each column in this row provides an identifier
11970 for a debug section, and the offsets in the same column of subsequent rows
11971 refer to that section. The section identifiers are:
11972
11973 DW_SECT_INFO 1 .debug_info.dwo
11974 DW_SECT_TYPES 2 .debug_types.dwo
11975 DW_SECT_ABBREV 3 .debug_abbrev.dwo
11976 DW_SECT_LINE 4 .debug_line.dwo
11977 DW_SECT_LOC 5 .debug_loc.dwo
11978 DW_SECT_STR_OFFSETS 6 .debug_str_offsets.dwo
11979 DW_SECT_MACINFO 7 .debug_macinfo.dwo
11980 DW_SECT_MACRO 8 .debug_macro.dwo
11981
11982 The offsets provided by the CU and TU index sections are the base offsets
11983 for the contributions made by each CU or TU to the corresponding section
11984 in the package file. Each CU and TU header contains an abbrev_offset
11985 field, used to find the abbreviations table for that CU or TU within the
11986 contribution to the .debug_abbrev.dwo section for that CU or TU, and should
11987 be interpreted as relative to the base offset given in the index section.
11988 Likewise, offsets into .debug_line.dwo from DW_AT_stmt_list attributes
11989 should be interpreted as relative to the base offset for .debug_line.dwo,
11990 and offsets into other debug sections obtained from DWARF attributes should
11991 also be interpreted as relative to the corresponding base offset.
11992
11993 The table of sizes begins immediately following the table of offsets.
11994 Like the table of offsets, it is a two-dimensional array of 32-bit words,
11995 with L columns and N rows, in row-major order. Each row in the array is
11996 indexed starting from 1 (row 0 is shared by the two tables).
11997
11998 ---
11999
12000 Hash table lookup is handled the same in version 1 and 2:
12001
12002 We assume that N and M will not exceed 2^32 - 1.
12003 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
12004
d2415c6c
DE
12005 Given a 64-bit compilation unit signature or a type signature S, an entry
12006 in the hash table is located as follows:
80626a55 12007
d2415c6c
DE
12008 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
12009 the low-order k bits all set to 1.
80626a55 12010
d2415c6c 12011 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
80626a55 12012
d2415c6c
DE
12013 3) If the hash table entry at index H matches the signature, use that
12014 entry. If the hash table entry at index H is unused (all zeroes),
12015 terminate the search: the signature is not present in the table.
80626a55 12016
d2415c6c 12017 4) Let H = (H + H') modulo M. Repeat at Step 3.
80626a55 12018
d2415c6c 12019 Because M > N and H' and M are relatively prime, the search is guaranteed
73869dc2 12020 to stop at an unused slot or find the match. */
80626a55
DE
12021
12022/* Create a hash table to map DWO IDs to their CU/TU entry in
12023 .debug_{info,types}.dwo in DWP_FILE.
12024 Returns NULL if there isn't one.
12025 Note: This function processes DWP files only, not DWO files. */
12026
12027static struct dwp_hash_table *
ed2dc618
SM
12028create_dwp_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
12029 struct dwp_file *dwp_file, int is_debug_types)
80626a55
DE
12030{
12031 struct objfile *objfile = dwarf2_per_objfile->objfile;
12032 bfd *dbfd = dwp_file->dbfd;
948f8e3d 12033 const gdb_byte *index_ptr, *index_end;
80626a55 12034 struct dwarf2_section_info *index;
73869dc2 12035 uint32_t version, nr_columns, nr_units, nr_slots;
80626a55
DE
12036 struct dwp_hash_table *htab;
12037
12038 if (is_debug_types)
12039 index = &dwp_file->sections.tu_index;
12040 else
12041 index = &dwp_file->sections.cu_index;
12042
12043 if (dwarf2_section_empty_p (index))
12044 return NULL;
12045 dwarf2_read_section (objfile, index);
12046
12047 index_ptr = index->buffer;
12048 index_end = index_ptr + index->size;
12049
12050 version = read_4_bytes (dbfd, index_ptr);
73869dc2
DE
12051 index_ptr += 4;
12052 if (version == 2)
12053 nr_columns = read_4_bytes (dbfd, index_ptr);
12054 else
12055 nr_columns = 0;
12056 index_ptr += 4;
80626a55
DE
12057 nr_units = read_4_bytes (dbfd, index_ptr);
12058 index_ptr += 4;
12059 nr_slots = read_4_bytes (dbfd, index_ptr);
12060 index_ptr += 4;
12061
73869dc2 12062 if (version != 1 && version != 2)
80626a55 12063 {
21aa081e 12064 error (_("Dwarf Error: unsupported DWP file version (%s)"
80626a55 12065 " [in module %s]"),
21aa081e 12066 pulongest (version), dwp_file->name);
80626a55
DE
12067 }
12068 if (nr_slots != (nr_slots & -nr_slots))
12069 {
21aa081e 12070 error (_("Dwarf Error: number of slots in DWP hash table (%s)"
80626a55 12071 " is not power of 2 [in module %s]"),
21aa081e 12072 pulongest (nr_slots), dwp_file->name);
80626a55
DE
12073 }
12074
12075 htab = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_hash_table);
73869dc2
DE
12076 htab->version = version;
12077 htab->nr_columns = nr_columns;
80626a55
DE
12078 htab->nr_units = nr_units;
12079 htab->nr_slots = nr_slots;
12080 htab->hash_table = index_ptr;
12081 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
73869dc2
DE
12082
12083 /* Exit early if the table is empty. */
12084 if (nr_slots == 0 || nr_units == 0
12085 || (version == 2 && nr_columns == 0))
12086 {
12087 /* All must be zero. */
12088 if (nr_slots != 0 || nr_units != 0
12089 || (version == 2 && nr_columns != 0))
12090 {
12091 complaint (&symfile_complaints,
12092 _("Empty DWP but nr_slots,nr_units,nr_columns not"
12093 " all zero [in modules %s]"),
12094 dwp_file->name);
12095 }
12096 return htab;
12097 }
12098
12099 if (version == 1)
12100 {
12101 htab->section_pool.v1.indices =
12102 htab->unit_table + sizeof (uint32_t) * nr_slots;
12103 /* It's harder to decide whether the section is too small in v1.
12104 V1 is deprecated anyway so we punt. */
12105 }
12106 else
12107 {
12108 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
12109 int *ids = htab->section_pool.v2.section_ids;
12110 /* Reverse map for error checking. */
12111 int ids_seen[DW_SECT_MAX + 1];
12112 int i;
12113
12114 if (nr_columns < 2)
12115 {
12116 error (_("Dwarf Error: bad DWP hash table, too few columns"
12117 " in section table [in module %s]"),
12118 dwp_file->name);
12119 }
12120 if (nr_columns > MAX_NR_V2_DWO_SECTIONS)
12121 {
12122 error (_("Dwarf Error: bad DWP hash table, too many columns"
12123 " in section table [in module %s]"),
12124 dwp_file->name);
12125 }
12126 memset (ids, 255, (DW_SECT_MAX + 1) * sizeof (int32_t));
12127 memset (ids_seen, 255, (DW_SECT_MAX + 1) * sizeof (int32_t));
12128 for (i = 0; i < nr_columns; ++i)
12129 {
12130 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
12131
12132 if (id < DW_SECT_MIN || id > DW_SECT_MAX)
12133 {
12134 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
12135 " in section table [in module %s]"),
12136 id, dwp_file->name);
12137 }
12138 if (ids_seen[id] != -1)
12139 {
12140 error (_("Dwarf Error: bad DWP hash table, duplicate section"
12141 " id %d in section table [in module %s]"),
12142 id, dwp_file->name);
12143 }
12144 ids_seen[id] = i;
12145 ids[i] = id;
12146 }
12147 /* Must have exactly one info or types section. */
12148 if (((ids_seen[DW_SECT_INFO] != -1)
12149 + (ids_seen[DW_SECT_TYPES] != -1))
12150 != 1)
12151 {
12152 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
12153 " DWO info/types section [in module %s]"),
12154 dwp_file->name);
12155 }
12156 /* Must have an abbrev section. */
12157 if (ids_seen[DW_SECT_ABBREV] == -1)
12158 {
12159 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
12160 " section [in module %s]"),
12161 dwp_file->name);
12162 }
12163 htab->section_pool.v2.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
12164 htab->section_pool.v2.sizes =
12165 htab->section_pool.v2.offsets + (sizeof (uint32_t)
12166 * nr_units * nr_columns);
12167 if ((htab->section_pool.v2.sizes + (sizeof (uint32_t)
12168 * nr_units * nr_columns))
12169 > index_end)
12170 {
12171 error (_("Dwarf Error: DWP index section is corrupt (too small)"
12172 " [in module %s]"),
12173 dwp_file->name);
12174 }
12175 }
80626a55
DE
12176
12177 return htab;
12178}
12179
12180/* Update SECTIONS with the data from SECTP.
12181
12182 This function is like the other "locate" section routines that are
12183 passed to bfd_map_over_sections, but in this context the sections to
73869dc2 12184 read comes from the DWP V1 hash table, not the full ELF section table.
80626a55
DE
12185
12186 The result is non-zero for success, or zero if an error was found. */
12187
12188static int
73869dc2
DE
12189locate_v1_virtual_dwo_sections (asection *sectp,
12190 struct virtual_v1_dwo_sections *sections)
80626a55
DE
12191{
12192 const struct dwop_section_names *names = &dwop_section_names;
12193
12194 if (section_is_p (sectp->name, &names->abbrev_dwo))
12195 {
12196 /* There can be only one. */
049412e3 12197 if (sections->abbrev.s.section != NULL)
80626a55 12198 return 0;
049412e3 12199 sections->abbrev.s.section = sectp;
80626a55
DE
12200 sections->abbrev.size = bfd_get_section_size (sectp);
12201 }
12202 else if (section_is_p (sectp->name, &names->info_dwo)
12203 || section_is_p (sectp->name, &names->types_dwo))
12204 {
12205 /* There can be only one. */
049412e3 12206 if (sections->info_or_types.s.section != NULL)
80626a55 12207 return 0;
049412e3 12208 sections->info_or_types.s.section = sectp;
80626a55
DE
12209 sections->info_or_types.size = bfd_get_section_size (sectp);
12210 }
12211 else if (section_is_p (sectp->name, &names->line_dwo))
12212 {
12213 /* There can be only one. */
049412e3 12214 if (sections->line.s.section != NULL)
80626a55 12215 return 0;
049412e3 12216 sections->line.s.section = sectp;
80626a55
DE
12217 sections->line.size = bfd_get_section_size (sectp);
12218 }
12219 else if (section_is_p (sectp->name, &names->loc_dwo))
12220 {
12221 /* There can be only one. */
049412e3 12222 if (sections->loc.s.section != NULL)
80626a55 12223 return 0;
049412e3 12224 sections->loc.s.section = sectp;
80626a55
DE
12225 sections->loc.size = bfd_get_section_size (sectp);
12226 }
12227 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12228 {
12229 /* There can be only one. */
049412e3 12230 if (sections->macinfo.s.section != NULL)
80626a55 12231 return 0;
049412e3 12232 sections->macinfo.s.section = sectp;
80626a55
DE
12233 sections->macinfo.size = bfd_get_section_size (sectp);
12234 }
12235 else if (section_is_p (sectp->name, &names->macro_dwo))
12236 {
12237 /* There can be only one. */
049412e3 12238 if (sections->macro.s.section != NULL)
80626a55 12239 return 0;
049412e3 12240 sections->macro.s.section = sectp;
80626a55
DE
12241 sections->macro.size = bfd_get_section_size (sectp);
12242 }
12243 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12244 {
12245 /* There can be only one. */
049412e3 12246 if (sections->str_offsets.s.section != NULL)
80626a55 12247 return 0;
049412e3 12248 sections->str_offsets.s.section = sectp;
80626a55
DE
12249 sections->str_offsets.size = bfd_get_section_size (sectp);
12250 }
12251 else
12252 {
12253 /* No other kind of section is valid. */
12254 return 0;
12255 }
12256
12257 return 1;
12258}
12259
73869dc2
DE
12260/* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
12261 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
12262 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
12263 This is for DWP version 1 files. */
80626a55
DE
12264
12265static struct dwo_unit *
ed2dc618
SM
12266create_dwo_unit_in_dwp_v1 (struct dwarf2_per_objfile *dwarf2_per_objfile,
12267 struct dwp_file *dwp_file,
73869dc2
DE
12268 uint32_t unit_index,
12269 const char *comp_dir,
12270 ULONGEST signature, int is_debug_types)
80626a55
DE
12271{
12272 struct objfile *objfile = dwarf2_per_objfile->objfile;
73869dc2
DE
12273 const struct dwp_hash_table *dwp_htab =
12274 is_debug_types ? dwp_file->tus : dwp_file->cus;
80626a55
DE
12275 bfd *dbfd = dwp_file->dbfd;
12276 const char *kind = is_debug_types ? "TU" : "CU";
12277 struct dwo_file *dwo_file;
12278 struct dwo_unit *dwo_unit;
73869dc2 12279 struct virtual_v1_dwo_sections sections;
80626a55 12280 void **dwo_file_slot;
80626a55
DE
12281 int i;
12282
73869dc2
DE
12283 gdb_assert (dwp_file->version == 1);
12284
b4f54984 12285 if (dwarf_read_debug)
80626a55 12286 {
73869dc2 12287 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V1 file: %s\n",
80626a55 12288 kind,
73869dc2 12289 pulongest (unit_index), hex_string (signature),
80626a55
DE
12290 dwp_file->name);
12291 }
12292
19ac8c2e 12293 /* Fetch the sections of this DWO unit.
80626a55
DE
12294 Put a limit on the number of sections we look for so that bad data
12295 doesn't cause us to loop forever. */
12296
73869dc2 12297#define MAX_NR_V1_DWO_SECTIONS \
80626a55
DE
12298 (1 /* .debug_info or .debug_types */ \
12299 + 1 /* .debug_abbrev */ \
12300 + 1 /* .debug_line */ \
12301 + 1 /* .debug_loc */ \
12302 + 1 /* .debug_str_offsets */ \
19ac8c2e 12303 + 1 /* .debug_macro or .debug_macinfo */ \
80626a55
DE
12304 + 1 /* trailing zero */)
12305
12306 memset (&sections, 0, sizeof (sections));
80626a55 12307
73869dc2 12308 for (i = 0; i < MAX_NR_V1_DWO_SECTIONS; ++i)
80626a55
DE
12309 {
12310 asection *sectp;
12311 uint32_t section_nr =
12312 read_4_bytes (dbfd,
73869dc2
DE
12313 dwp_htab->section_pool.v1.indices
12314 + (unit_index + i) * sizeof (uint32_t));
80626a55
DE
12315
12316 if (section_nr == 0)
12317 break;
12318 if (section_nr >= dwp_file->num_sections)
12319 {
12320 error (_("Dwarf Error: bad DWP hash table, section number too large"
12321 " [in module %s]"),
12322 dwp_file->name);
12323 }
12324
12325 sectp = dwp_file->elf_sections[section_nr];
73869dc2 12326 if (! locate_v1_virtual_dwo_sections (sectp, &sections))
80626a55
DE
12327 {
12328 error (_("Dwarf Error: bad DWP hash table, invalid section found"
12329 " [in module %s]"),
12330 dwp_file->name);
12331 }
12332 }
12333
12334 if (i < 2
a32a8923
DE
12335 || dwarf2_section_empty_p (&sections.info_or_types)
12336 || dwarf2_section_empty_p (&sections.abbrev))
80626a55
DE
12337 {
12338 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
12339 " [in module %s]"),
12340 dwp_file->name);
12341 }
73869dc2 12342 if (i == MAX_NR_V1_DWO_SECTIONS)
80626a55
DE
12343 {
12344 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
12345 " [in module %s]"),
12346 dwp_file->name);
12347 }
12348
12349 /* It's easier for the rest of the code if we fake a struct dwo_file and
12350 have dwo_unit "live" in that. At least for now.
12351
12352 The DWP file can be made up of a random collection of CUs and TUs.
c766f7ec 12353 However, for each CU + set of TUs that came from the same original DWO
57d63ce2
DE
12354 file, we can combine them back into a virtual DWO file to save space
12355 (fewer struct dwo_file objects to allocate). Remember that for really
80626a55
DE
12356 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
12357
791afaa2
TT
12358 std::string virtual_dwo_name =
12359 string_printf ("virtual-dwo/%d-%d-%d-%d",
12360 get_section_id (&sections.abbrev),
12361 get_section_id (&sections.line),
12362 get_section_id (&sections.loc),
12363 get_section_id (&sections.str_offsets));
80626a55 12364 /* Can we use an existing virtual DWO file? */
ed2dc618
SM
12365 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
12366 virtual_dwo_name.c_str (),
12367 comp_dir);
80626a55
DE
12368 /* Create one if necessary. */
12369 if (*dwo_file_slot == NULL)
12370 {
b4f54984 12371 if (dwarf_read_debug)
80626a55
DE
12372 {
12373 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
791afaa2 12374 virtual_dwo_name.c_str ());
80626a55
DE
12375 }
12376 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
224c3ddb
SM
12377 dwo_file->dwo_name
12378 = (const char *) obstack_copy0 (&objfile->objfile_obstack,
791afaa2
TT
12379 virtual_dwo_name.c_str (),
12380 virtual_dwo_name.size ());
0ac5b59e 12381 dwo_file->comp_dir = comp_dir;
80626a55
DE
12382 dwo_file->sections.abbrev = sections.abbrev;
12383 dwo_file->sections.line = sections.line;
12384 dwo_file->sections.loc = sections.loc;
12385 dwo_file->sections.macinfo = sections.macinfo;
12386 dwo_file->sections.macro = sections.macro;
12387 dwo_file->sections.str_offsets = sections.str_offsets;
12388 /* The "str" section is global to the entire DWP file. */
12389 dwo_file->sections.str = dwp_file->sections.str;
57d63ce2 12390 /* The info or types section is assigned below to dwo_unit,
80626a55
DE
12391 there's no need to record it in dwo_file.
12392 Also, we can't simply record type sections in dwo_file because
12393 we record a pointer into the vector in dwo_unit. As we collect more
12394 types we'll grow the vector and eventually have to reallocate space
57d63ce2
DE
12395 for it, invalidating all copies of pointers into the previous
12396 contents. */
80626a55
DE
12397 *dwo_file_slot = dwo_file;
12398 }
12399 else
12400 {
b4f54984 12401 if (dwarf_read_debug)
80626a55
DE
12402 {
12403 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
791afaa2 12404 virtual_dwo_name.c_str ());
80626a55 12405 }
9a3c8263 12406 dwo_file = (struct dwo_file *) *dwo_file_slot;
80626a55 12407 }
80626a55
DE
12408
12409 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
12410 dwo_unit->dwo_file = dwo_file;
12411 dwo_unit->signature = signature;
8d749320
SM
12412 dwo_unit->section =
12413 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
8a0459fd 12414 *dwo_unit->section = sections.info_or_types;
57d63ce2 12415 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
80626a55
DE
12416
12417 return dwo_unit;
12418}
12419
73869dc2
DE
12420/* Subroutine of create_dwo_unit_in_dwp_v2 to simplify it.
12421 Given a pointer to the containing section SECTION, and OFFSET,SIZE of the
12422 piece within that section used by a TU/CU, return a virtual section
12423 of just that piece. */
12424
12425static struct dwarf2_section_info
ed2dc618
SM
12426create_dwp_v2_section (struct dwarf2_per_objfile *dwarf2_per_objfile,
12427 struct dwarf2_section_info *section,
73869dc2
DE
12428 bfd_size_type offset, bfd_size_type size)
12429{
12430 struct dwarf2_section_info result;
12431 asection *sectp;
12432
12433 gdb_assert (section != NULL);
12434 gdb_assert (!section->is_virtual);
12435
12436 memset (&result, 0, sizeof (result));
12437 result.s.containing_section = section;
12438 result.is_virtual = 1;
12439
12440 if (size == 0)
12441 return result;
12442
12443 sectp = get_section_bfd_section (section);
12444
12445 /* Flag an error if the piece denoted by OFFSET,SIZE is outside the
12446 bounds of the real section. This is a pretty-rare event, so just
12447 flag an error (easier) instead of a warning and trying to cope. */
12448 if (sectp == NULL
12449 || offset + size > bfd_get_section_size (sectp))
12450 {
73869dc2
DE
12451 error (_("Dwarf Error: Bad DWP V2 section info, doesn't fit"
12452 " in section %s [in module %s]"),
12453 sectp ? bfd_section_name (abfd, sectp) : "<unknown>",
12454 objfile_name (dwarf2_per_objfile->objfile));
12455 }
12456
12457 result.virtual_offset = offset;
12458 result.size = size;
12459 return result;
12460}
12461
12462/* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
12463 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
12464 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
12465 This is for DWP version 2 files. */
12466
12467static struct dwo_unit *
ed2dc618
SM
12468create_dwo_unit_in_dwp_v2 (struct dwarf2_per_objfile *dwarf2_per_objfile,
12469 struct dwp_file *dwp_file,
73869dc2
DE
12470 uint32_t unit_index,
12471 const char *comp_dir,
12472 ULONGEST signature, int is_debug_types)
12473{
12474 struct objfile *objfile = dwarf2_per_objfile->objfile;
12475 const struct dwp_hash_table *dwp_htab =
12476 is_debug_types ? dwp_file->tus : dwp_file->cus;
12477 bfd *dbfd = dwp_file->dbfd;
12478 const char *kind = is_debug_types ? "TU" : "CU";
12479 struct dwo_file *dwo_file;
12480 struct dwo_unit *dwo_unit;
12481 struct virtual_v2_dwo_sections sections;
12482 void **dwo_file_slot;
73869dc2
DE
12483 int i;
12484
12485 gdb_assert (dwp_file->version == 2);
12486
b4f54984 12487 if (dwarf_read_debug)
73869dc2
DE
12488 {
12489 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V2 file: %s\n",
12490 kind,
12491 pulongest (unit_index), hex_string (signature),
12492 dwp_file->name);
12493 }
12494
12495 /* Fetch the section offsets of this DWO unit. */
12496
12497 memset (&sections, 0, sizeof (sections));
73869dc2
DE
12498
12499 for (i = 0; i < dwp_htab->nr_columns; ++i)
12500 {
12501 uint32_t offset = read_4_bytes (dbfd,
12502 dwp_htab->section_pool.v2.offsets
12503 + (((unit_index - 1) * dwp_htab->nr_columns
12504 + i)
12505 * sizeof (uint32_t)));
12506 uint32_t size = read_4_bytes (dbfd,
12507 dwp_htab->section_pool.v2.sizes
12508 + (((unit_index - 1) * dwp_htab->nr_columns
12509 + i)
12510 * sizeof (uint32_t)));
12511
12512 switch (dwp_htab->section_pool.v2.section_ids[i])
12513 {
12514 case DW_SECT_INFO:
12515 case DW_SECT_TYPES:
12516 sections.info_or_types_offset = offset;
12517 sections.info_or_types_size = size;
12518 break;
12519 case DW_SECT_ABBREV:
12520 sections.abbrev_offset = offset;
12521 sections.abbrev_size = size;
12522 break;
12523 case DW_SECT_LINE:
12524 sections.line_offset = offset;
12525 sections.line_size = size;
12526 break;
12527 case DW_SECT_LOC:
12528 sections.loc_offset = offset;
12529 sections.loc_size = size;
12530 break;
12531 case DW_SECT_STR_OFFSETS:
12532 sections.str_offsets_offset = offset;
12533 sections.str_offsets_size = size;
12534 break;
12535 case DW_SECT_MACINFO:
12536 sections.macinfo_offset = offset;
12537 sections.macinfo_size = size;
12538 break;
12539 case DW_SECT_MACRO:
12540 sections.macro_offset = offset;
12541 sections.macro_size = size;
12542 break;
12543 }
12544 }
12545
12546 /* It's easier for the rest of the code if we fake a struct dwo_file and
12547 have dwo_unit "live" in that. At least for now.
12548
12549 The DWP file can be made up of a random collection of CUs and TUs.
12550 However, for each CU + set of TUs that came from the same original DWO
12551 file, we can combine them back into a virtual DWO file to save space
12552 (fewer struct dwo_file objects to allocate). Remember that for really
12553 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
12554
791afaa2
TT
12555 std::string virtual_dwo_name =
12556 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld",
12557 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
12558 (long) (sections.line_size ? sections.line_offset : 0),
12559 (long) (sections.loc_size ? sections.loc_offset : 0),
12560 (long) (sections.str_offsets_size
12561 ? sections.str_offsets_offset : 0));
73869dc2 12562 /* Can we use an existing virtual DWO file? */
ed2dc618
SM
12563 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
12564 virtual_dwo_name.c_str (),
12565 comp_dir);
73869dc2
DE
12566 /* Create one if necessary. */
12567 if (*dwo_file_slot == NULL)
12568 {
b4f54984 12569 if (dwarf_read_debug)
73869dc2
DE
12570 {
12571 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
791afaa2 12572 virtual_dwo_name.c_str ());
73869dc2
DE
12573 }
12574 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
224c3ddb
SM
12575 dwo_file->dwo_name
12576 = (const char *) obstack_copy0 (&objfile->objfile_obstack,
791afaa2
TT
12577 virtual_dwo_name.c_str (),
12578 virtual_dwo_name.size ());
73869dc2
DE
12579 dwo_file->comp_dir = comp_dir;
12580 dwo_file->sections.abbrev =
ed2dc618 12581 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.abbrev,
73869dc2
DE
12582 sections.abbrev_offset, sections.abbrev_size);
12583 dwo_file->sections.line =
ed2dc618 12584 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.line,
73869dc2
DE
12585 sections.line_offset, sections.line_size);
12586 dwo_file->sections.loc =
ed2dc618 12587 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.loc,
73869dc2
DE
12588 sections.loc_offset, sections.loc_size);
12589 dwo_file->sections.macinfo =
ed2dc618 12590 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.macinfo,
73869dc2
DE
12591 sections.macinfo_offset, sections.macinfo_size);
12592 dwo_file->sections.macro =
ed2dc618 12593 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.macro,
73869dc2
DE
12594 sections.macro_offset, sections.macro_size);
12595 dwo_file->sections.str_offsets =
ed2dc618
SM
12596 create_dwp_v2_section (dwarf2_per_objfile,
12597 &dwp_file->sections.str_offsets,
73869dc2
DE
12598 sections.str_offsets_offset,
12599 sections.str_offsets_size);
12600 /* The "str" section is global to the entire DWP file. */
12601 dwo_file->sections.str = dwp_file->sections.str;
12602 /* The info or types section is assigned below to dwo_unit,
12603 there's no need to record it in dwo_file.
12604 Also, we can't simply record type sections in dwo_file because
12605 we record a pointer into the vector in dwo_unit. As we collect more
12606 types we'll grow the vector and eventually have to reallocate space
12607 for it, invalidating all copies of pointers into the previous
12608 contents. */
12609 *dwo_file_slot = dwo_file;
12610 }
12611 else
12612 {
b4f54984 12613 if (dwarf_read_debug)
73869dc2
DE
12614 {
12615 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
791afaa2 12616 virtual_dwo_name.c_str ());
73869dc2 12617 }
9a3c8263 12618 dwo_file = (struct dwo_file *) *dwo_file_slot;
73869dc2 12619 }
73869dc2
DE
12620
12621 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
12622 dwo_unit->dwo_file = dwo_file;
12623 dwo_unit->signature = signature;
8d749320
SM
12624 dwo_unit->section =
12625 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
ed2dc618
SM
12626 *dwo_unit->section = create_dwp_v2_section (dwarf2_per_objfile,
12627 is_debug_types
73869dc2
DE
12628 ? &dwp_file->sections.types
12629 : &dwp_file->sections.info,
12630 sections.info_or_types_offset,
12631 sections.info_or_types_size);
12632 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
12633
12634 return dwo_unit;
12635}
12636
57d63ce2
DE
12637/* Lookup the DWO unit with SIGNATURE in DWP_FILE.
12638 Returns NULL if the signature isn't found. */
80626a55
DE
12639
12640static struct dwo_unit *
ed2dc618
SM
12641lookup_dwo_unit_in_dwp (struct dwarf2_per_objfile *dwarf2_per_objfile,
12642 struct dwp_file *dwp_file, const char *comp_dir,
57d63ce2 12643 ULONGEST signature, int is_debug_types)
80626a55 12644{
57d63ce2
DE
12645 const struct dwp_hash_table *dwp_htab =
12646 is_debug_types ? dwp_file->tus : dwp_file->cus;
80626a55 12647 bfd *dbfd = dwp_file->dbfd;
57d63ce2 12648 uint32_t mask = dwp_htab->nr_slots - 1;
80626a55
DE
12649 uint32_t hash = signature & mask;
12650 uint32_t hash2 = ((signature >> 32) & mask) | 1;
12651 unsigned int i;
12652 void **slot;
870f88f7 12653 struct dwo_unit find_dwo_cu;
80626a55
DE
12654
12655 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
12656 find_dwo_cu.signature = signature;
19ac8c2e
DE
12657 slot = htab_find_slot (is_debug_types
12658 ? dwp_file->loaded_tus
12659 : dwp_file->loaded_cus,
12660 &find_dwo_cu, INSERT);
80626a55
DE
12661
12662 if (*slot != NULL)
9a3c8263 12663 return (struct dwo_unit *) *slot;
80626a55
DE
12664
12665 /* Use a for loop so that we don't loop forever on bad debug info. */
57d63ce2 12666 for (i = 0; i < dwp_htab->nr_slots; ++i)
80626a55
DE
12667 {
12668 ULONGEST signature_in_table;
12669
12670 signature_in_table =
57d63ce2 12671 read_8_bytes (dbfd, dwp_htab->hash_table + hash * sizeof (uint64_t));
80626a55
DE
12672 if (signature_in_table == signature)
12673 {
57d63ce2
DE
12674 uint32_t unit_index =
12675 read_4_bytes (dbfd,
12676 dwp_htab->unit_table + hash * sizeof (uint32_t));
80626a55 12677
73869dc2
DE
12678 if (dwp_file->version == 1)
12679 {
ed2dc618
SM
12680 *slot = create_dwo_unit_in_dwp_v1 (dwarf2_per_objfile,
12681 dwp_file, unit_index,
73869dc2
DE
12682 comp_dir, signature,
12683 is_debug_types);
12684 }
12685 else
12686 {
ed2dc618
SM
12687 *slot = create_dwo_unit_in_dwp_v2 (dwarf2_per_objfile,
12688 dwp_file, unit_index,
73869dc2
DE
12689 comp_dir, signature,
12690 is_debug_types);
12691 }
9a3c8263 12692 return (struct dwo_unit *) *slot;
80626a55
DE
12693 }
12694 if (signature_in_table == 0)
12695 return NULL;
12696 hash = (hash + hash2) & mask;
12697 }
12698
12699 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
12700 " [in module %s]"),
12701 dwp_file->name);
12702}
12703
ab5088bf 12704/* Subroutine of open_dwo_file,open_dwp_file to simplify them.
3019eac3
DE
12705 Open the file specified by FILE_NAME and hand it off to BFD for
12706 preliminary analysis. Return a newly initialized bfd *, which
12707 includes a canonicalized copy of FILE_NAME.
80626a55 12708 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
6ac97d4c
DE
12709 SEARCH_CWD is true if the current directory is to be searched.
12710 It will be searched before debug-file-directory.
13aaf454
DE
12711 If successful, the file is added to the bfd include table of the
12712 objfile's bfd (see gdb_bfd_record_inclusion).
6ac97d4c 12713 If unable to find/open the file, return NULL.
3019eac3
DE
12714 NOTE: This function is derived from symfile_bfd_open. */
12715
192b62ce 12716static gdb_bfd_ref_ptr
ed2dc618
SM
12717try_open_dwop_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12718 const char *file_name, int is_dwp, int search_cwd)
3019eac3 12719{
24b9144d 12720 int desc;
9c02c129
DE
12721 /* Blech. OPF_TRY_CWD_FIRST also disables searching the path list if
12722 FILE_NAME contains a '/'. So we can't use it. Instead prepend "."
12723 to debug_file_directory. */
e0cc99a6 12724 const char *search_path;
9c02c129
DE
12725 static const char dirname_separator_string[] = { DIRNAME_SEPARATOR, '\0' };
12726
e0cc99a6 12727 gdb::unique_xmalloc_ptr<char> search_path_holder;
6ac97d4c
DE
12728 if (search_cwd)
12729 {
12730 if (*debug_file_directory != '\0')
e0cc99a6
TT
12731 {
12732 search_path_holder.reset (concat (".", dirname_separator_string,
12733 debug_file_directory,
12734 (char *) NULL));
12735 search_path = search_path_holder.get ();
12736 }
6ac97d4c 12737 else
e0cc99a6 12738 search_path = ".";
6ac97d4c 12739 }
9c02c129 12740 else
e0cc99a6 12741 search_path = debug_file_directory;
3019eac3 12742
24b9144d 12743 openp_flags flags = OPF_RETURN_REALPATH;
80626a55
DE
12744 if (is_dwp)
12745 flags |= OPF_SEARCH_IN_PATH;
e0cc99a6
TT
12746
12747 gdb::unique_xmalloc_ptr<char> absolute_name;
9c02c129 12748 desc = openp (search_path, flags, file_name,
3019eac3
DE
12749 O_RDONLY | O_BINARY, &absolute_name);
12750 if (desc < 0)
12751 return NULL;
12752
e0cc99a6
TT
12753 gdb_bfd_ref_ptr sym_bfd (gdb_bfd_open (absolute_name.get (),
12754 gnutarget, desc));
9c02c129
DE
12755 if (sym_bfd == NULL)
12756 return NULL;
192b62ce 12757 bfd_set_cacheable (sym_bfd.get (), 1);
3019eac3 12758
192b62ce
TT
12759 if (!bfd_check_format (sym_bfd.get (), bfd_object))
12760 return NULL;
3019eac3 12761
13aaf454
DE
12762 /* Success. Record the bfd as having been included by the objfile's bfd.
12763 This is important because things like demangled_names_hash lives in the
12764 objfile's per_bfd space and may have references to things like symbol
12765 names that live in the DWO/DWP file's per_bfd space. PR 16426. */
192b62ce 12766 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd, sym_bfd.get ());
13aaf454 12767
3019eac3
DE
12768 return sym_bfd;
12769}
12770
ab5088bf 12771/* Try to open DWO file FILE_NAME.
3019eac3
DE
12772 COMP_DIR is the DW_AT_comp_dir attribute.
12773 The result is the bfd handle of the file.
12774 If there is a problem finding or opening the file, return NULL.
12775 Upon success, the canonicalized path of the file is stored in the bfd,
12776 same as symfile_bfd_open. */
12777
192b62ce 12778static gdb_bfd_ref_ptr
ed2dc618
SM
12779open_dwo_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12780 const char *file_name, const char *comp_dir)
3019eac3 12781{
80626a55 12782 if (IS_ABSOLUTE_PATH (file_name))
ed2dc618
SM
12783 return try_open_dwop_file (dwarf2_per_objfile, file_name,
12784 0 /*is_dwp*/, 0 /*search_cwd*/);
3019eac3
DE
12785
12786 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
12787
12788 if (comp_dir != NULL)
12789 {
b36cec19
PA
12790 char *path_to_try = concat (comp_dir, SLASH_STRING,
12791 file_name, (char *) NULL);
3019eac3
DE
12792
12793 /* NOTE: If comp_dir is a relative path, this will also try the
12794 search path, which seems useful. */
ed2dc618
SM
12795 gdb_bfd_ref_ptr abfd (try_open_dwop_file (dwarf2_per_objfile,
12796 path_to_try,
12797 0 /*is_dwp*/,
192b62ce 12798 1 /*search_cwd*/));
3019eac3
DE
12799 xfree (path_to_try);
12800 if (abfd != NULL)
12801 return abfd;
12802 }
12803
12804 /* That didn't work, try debug-file-directory, which, despite its name,
12805 is a list of paths. */
12806
12807 if (*debug_file_directory == '\0')
12808 return NULL;
12809
ed2dc618
SM
12810 return try_open_dwop_file (dwarf2_per_objfile, file_name,
12811 0 /*is_dwp*/, 1 /*search_cwd*/);
3019eac3
DE
12812}
12813
80626a55
DE
12814/* This function is mapped across the sections and remembers the offset and
12815 size of each of the DWO debugging sections we are interested in. */
12816
12817static void
12818dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp, void *dwo_sections_ptr)
12819{
9a3c8263 12820 struct dwo_sections *dwo_sections = (struct dwo_sections *) dwo_sections_ptr;
80626a55
DE
12821 const struct dwop_section_names *names = &dwop_section_names;
12822
12823 if (section_is_p (sectp->name, &names->abbrev_dwo))
12824 {
049412e3 12825 dwo_sections->abbrev.s.section = sectp;
80626a55
DE
12826 dwo_sections->abbrev.size = bfd_get_section_size (sectp);
12827 }
12828 else if (section_is_p (sectp->name, &names->info_dwo))
12829 {
049412e3 12830 dwo_sections->info.s.section = sectp;
80626a55
DE
12831 dwo_sections->info.size = bfd_get_section_size (sectp);
12832 }
12833 else if (section_is_p (sectp->name, &names->line_dwo))
12834 {
049412e3 12835 dwo_sections->line.s.section = sectp;
80626a55
DE
12836 dwo_sections->line.size = bfd_get_section_size (sectp);
12837 }
12838 else if (section_is_p (sectp->name, &names->loc_dwo))
12839 {
049412e3 12840 dwo_sections->loc.s.section = sectp;
80626a55
DE
12841 dwo_sections->loc.size = bfd_get_section_size (sectp);
12842 }
12843 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12844 {
049412e3 12845 dwo_sections->macinfo.s.section = sectp;
80626a55
DE
12846 dwo_sections->macinfo.size = bfd_get_section_size (sectp);
12847 }
12848 else if (section_is_p (sectp->name, &names->macro_dwo))
12849 {
049412e3 12850 dwo_sections->macro.s.section = sectp;
80626a55
DE
12851 dwo_sections->macro.size = bfd_get_section_size (sectp);
12852 }
12853 else if (section_is_p (sectp->name, &names->str_dwo))
12854 {
049412e3 12855 dwo_sections->str.s.section = sectp;
80626a55
DE
12856 dwo_sections->str.size = bfd_get_section_size (sectp);
12857 }
12858 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12859 {
049412e3 12860 dwo_sections->str_offsets.s.section = sectp;
80626a55
DE
12861 dwo_sections->str_offsets.size = bfd_get_section_size (sectp);
12862 }
12863 else if (section_is_p (sectp->name, &names->types_dwo))
12864 {
12865 struct dwarf2_section_info type_section;
12866
12867 memset (&type_section, 0, sizeof (type_section));
049412e3 12868 type_section.s.section = sectp;
80626a55
DE
12869 type_section.size = bfd_get_section_size (sectp);
12870 VEC_safe_push (dwarf2_section_info_def, dwo_sections->types,
12871 &type_section);
12872 }
12873}
12874
ab5088bf 12875/* Initialize the use of the DWO file specified by DWO_NAME and referenced
19c3d4c9 12876 by PER_CU. This is for the non-DWP case.
80626a55 12877 The result is NULL if DWO_NAME can't be found. */
3019eac3
DE
12878
12879static struct dwo_file *
0ac5b59e
DE
12880open_and_init_dwo_file (struct dwarf2_per_cu_data *per_cu,
12881 const char *dwo_name, const char *comp_dir)
3019eac3 12882{
ed2dc618 12883 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
3019eac3 12884 struct objfile *objfile = dwarf2_per_objfile->objfile;
3019eac3 12885
ed2dc618 12886 gdb_bfd_ref_ptr dbfd (open_dwo_file (dwarf2_per_objfile, dwo_name, comp_dir));
80626a55
DE
12887 if (dbfd == NULL)
12888 {
b4f54984 12889 if (dwarf_read_debug)
80626a55
DE
12890 fprintf_unfiltered (gdb_stdlog, "DWO file not found: %s\n", dwo_name);
12891 return NULL;
12892 }
263db9a1
TT
12893
12894 /* We use a unique pointer here, despite the obstack allocation,
12895 because a dwo_file needs some cleanup if it is abandoned. */
12896 dwo_file_up dwo_file (OBSTACK_ZALLOC (&objfile->objfile_obstack,
12897 struct dwo_file));
0ac5b59e
DE
12898 dwo_file->dwo_name = dwo_name;
12899 dwo_file->comp_dir = comp_dir;
192b62ce 12900 dwo_file->dbfd = dbfd.release ();
3019eac3 12901
192b62ce
TT
12902 bfd_map_over_sections (dwo_file->dbfd, dwarf2_locate_dwo_sections,
12903 &dwo_file->sections);
3019eac3 12904
ed2dc618
SM
12905 create_cus_hash_table (dwarf2_per_objfile, *dwo_file, dwo_file->sections.info,
12906 dwo_file->cus);
3019eac3 12907
263db9a1 12908 create_debug_types_hash_table (dwarf2_per_objfile, dwo_file.get (),
ed2dc618 12909 dwo_file->sections.types, dwo_file->tus);
3019eac3 12910
b4f54984 12911 if (dwarf_read_debug)
80626a55
DE
12912 fprintf_unfiltered (gdb_stdlog, "DWO file found: %s\n", dwo_name);
12913
263db9a1 12914 return dwo_file.release ();
3019eac3
DE
12915}
12916
80626a55 12917/* This function is mapped across the sections and remembers the offset and
73869dc2
DE
12918 size of each of the DWP debugging sections common to version 1 and 2 that
12919 we are interested in. */
3019eac3 12920
80626a55 12921static void
73869dc2
DE
12922dwarf2_locate_common_dwp_sections (bfd *abfd, asection *sectp,
12923 void *dwp_file_ptr)
3019eac3 12924{
9a3c8263 12925 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
80626a55
DE
12926 const struct dwop_section_names *names = &dwop_section_names;
12927 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
3019eac3 12928
80626a55 12929 /* Record the ELF section number for later lookup: this is what the
73869dc2 12930 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
80626a55
DE
12931 gdb_assert (elf_section_nr < dwp_file->num_sections);
12932 dwp_file->elf_sections[elf_section_nr] = sectp;
3019eac3 12933
80626a55
DE
12934 /* Look for specific sections that we need. */
12935 if (section_is_p (sectp->name, &names->str_dwo))
12936 {
049412e3 12937 dwp_file->sections.str.s.section = sectp;
80626a55
DE
12938 dwp_file->sections.str.size = bfd_get_section_size (sectp);
12939 }
12940 else if (section_is_p (sectp->name, &names->cu_index))
12941 {
049412e3 12942 dwp_file->sections.cu_index.s.section = sectp;
80626a55
DE
12943 dwp_file->sections.cu_index.size = bfd_get_section_size (sectp);
12944 }
12945 else if (section_is_p (sectp->name, &names->tu_index))
12946 {
049412e3 12947 dwp_file->sections.tu_index.s.section = sectp;
80626a55
DE
12948 dwp_file->sections.tu_index.size = bfd_get_section_size (sectp);
12949 }
12950}
3019eac3 12951
73869dc2
DE
12952/* This function is mapped across the sections and remembers the offset and
12953 size of each of the DWP version 2 debugging sections that we are interested
12954 in. This is split into a separate function because we don't know if we
12955 have version 1 or 2 until we parse the cu_index/tu_index sections. */
12956
12957static void
12958dwarf2_locate_v2_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
12959{
9a3c8263 12960 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
73869dc2
DE
12961 const struct dwop_section_names *names = &dwop_section_names;
12962 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
12963
12964 /* Record the ELF section number for later lookup: this is what the
12965 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
12966 gdb_assert (elf_section_nr < dwp_file->num_sections);
12967 dwp_file->elf_sections[elf_section_nr] = sectp;
12968
12969 /* Look for specific sections that we need. */
12970 if (section_is_p (sectp->name, &names->abbrev_dwo))
12971 {
049412e3 12972 dwp_file->sections.abbrev.s.section = sectp;
73869dc2
DE
12973 dwp_file->sections.abbrev.size = bfd_get_section_size (sectp);
12974 }
12975 else if (section_is_p (sectp->name, &names->info_dwo))
12976 {
049412e3 12977 dwp_file->sections.info.s.section = sectp;
73869dc2
DE
12978 dwp_file->sections.info.size = bfd_get_section_size (sectp);
12979 }
12980 else if (section_is_p (sectp->name, &names->line_dwo))
12981 {
049412e3 12982 dwp_file->sections.line.s.section = sectp;
73869dc2
DE
12983 dwp_file->sections.line.size = bfd_get_section_size (sectp);
12984 }
12985 else if (section_is_p (sectp->name, &names->loc_dwo))
12986 {
049412e3 12987 dwp_file->sections.loc.s.section = sectp;
73869dc2
DE
12988 dwp_file->sections.loc.size = bfd_get_section_size (sectp);
12989 }
12990 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12991 {
049412e3 12992 dwp_file->sections.macinfo.s.section = sectp;
73869dc2
DE
12993 dwp_file->sections.macinfo.size = bfd_get_section_size (sectp);
12994 }
12995 else if (section_is_p (sectp->name, &names->macro_dwo))
12996 {
049412e3 12997 dwp_file->sections.macro.s.section = sectp;
73869dc2
DE
12998 dwp_file->sections.macro.size = bfd_get_section_size (sectp);
12999 }
13000 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
13001 {
049412e3 13002 dwp_file->sections.str_offsets.s.section = sectp;
73869dc2
DE
13003 dwp_file->sections.str_offsets.size = bfd_get_section_size (sectp);
13004 }
13005 else if (section_is_p (sectp->name, &names->types_dwo))
13006 {
049412e3 13007 dwp_file->sections.types.s.section = sectp;
73869dc2
DE
13008 dwp_file->sections.types.size = bfd_get_section_size (sectp);
13009 }
13010}
13011
80626a55 13012/* Hash function for dwp_file loaded CUs/TUs. */
3019eac3 13013
80626a55
DE
13014static hashval_t
13015hash_dwp_loaded_cutus (const void *item)
13016{
9a3c8263 13017 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
3019eac3 13018
80626a55
DE
13019 /* This drops the top 32 bits of the signature, but is ok for a hash. */
13020 return dwo_unit->signature;
3019eac3
DE
13021}
13022
80626a55 13023/* Equality function for dwp_file loaded CUs/TUs. */
3019eac3 13024
80626a55
DE
13025static int
13026eq_dwp_loaded_cutus (const void *a, const void *b)
3019eac3 13027{
9a3c8263
SM
13028 const struct dwo_unit *dua = (const struct dwo_unit *) a;
13029 const struct dwo_unit *dub = (const struct dwo_unit *) b;
3019eac3 13030
80626a55
DE
13031 return dua->signature == dub->signature;
13032}
3019eac3 13033
80626a55 13034/* Allocate a hash table for dwp_file loaded CUs/TUs. */
3019eac3 13035
80626a55
DE
13036static htab_t
13037allocate_dwp_loaded_cutus_table (struct objfile *objfile)
13038{
13039 return htab_create_alloc_ex (3,
13040 hash_dwp_loaded_cutus,
13041 eq_dwp_loaded_cutus,
13042 NULL,
13043 &objfile->objfile_obstack,
13044 hashtab_obstack_allocate,
13045 dummy_obstack_deallocate);
13046}
3019eac3 13047
ab5088bf
DE
13048/* Try to open DWP file FILE_NAME.
13049 The result is the bfd handle of the file.
13050 If there is a problem finding or opening the file, return NULL.
13051 Upon success, the canonicalized path of the file is stored in the bfd,
13052 same as symfile_bfd_open. */
13053
192b62ce 13054static gdb_bfd_ref_ptr
ed2dc618
SM
13055open_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
13056 const char *file_name)
ab5088bf 13057{
ed2dc618
SM
13058 gdb_bfd_ref_ptr abfd (try_open_dwop_file (dwarf2_per_objfile, file_name,
13059 1 /*is_dwp*/,
192b62ce 13060 1 /*search_cwd*/));
6ac97d4c
DE
13061 if (abfd != NULL)
13062 return abfd;
13063
13064 /* Work around upstream bug 15652.
13065 http://sourceware.org/bugzilla/show_bug.cgi?id=15652
13066 [Whether that's a "bug" is debatable, but it is getting in our way.]
13067 We have no real idea where the dwp file is, because gdb's realpath-ing
13068 of the executable's path may have discarded the needed info.
13069 [IWBN if the dwp file name was recorded in the executable, akin to
13070 .gnu_debuglink, but that doesn't exist yet.]
13071 Strip the directory from FILE_NAME and search again. */
13072 if (*debug_file_directory != '\0')
13073 {
13074 /* Don't implicitly search the current directory here.
13075 If the user wants to search "." to handle this case,
13076 it must be added to debug-file-directory. */
ed2dc618
SM
13077 return try_open_dwop_file (dwarf2_per_objfile,
13078 lbasename (file_name), 1 /*is_dwp*/,
6ac97d4c
DE
13079 0 /*search_cwd*/);
13080 }
13081
13082 return NULL;
ab5088bf
DE
13083}
13084
80626a55
DE
13085/* Initialize the use of the DWP file for the current objfile.
13086 By convention the name of the DWP file is ${objfile}.dwp.
13087 The result is NULL if it can't be found. */
a766d390 13088
80626a55 13089static struct dwp_file *
ed2dc618 13090open_and_init_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
80626a55
DE
13091{
13092 struct objfile *objfile = dwarf2_per_objfile->objfile;
13093 struct dwp_file *dwp_file;
80626a55 13094
82bf32bc
JK
13095 /* Try to find first .dwp for the binary file before any symbolic links
13096 resolving. */
6c447423
DE
13097
13098 /* If the objfile is a debug file, find the name of the real binary
13099 file and get the name of dwp file from there. */
d721ba37 13100 std::string dwp_name;
6c447423
DE
13101 if (objfile->separate_debug_objfile_backlink != NULL)
13102 {
13103 struct objfile *backlink = objfile->separate_debug_objfile_backlink;
13104 const char *backlink_basename = lbasename (backlink->original_name);
6c447423 13105
d721ba37 13106 dwp_name = ldirname (objfile->original_name) + SLASH_STRING + backlink_basename;
6c447423
DE
13107 }
13108 else
d721ba37
PA
13109 dwp_name = objfile->original_name;
13110
13111 dwp_name += ".dwp";
80626a55 13112
ed2dc618 13113 gdb_bfd_ref_ptr dbfd (open_dwp_file (dwarf2_per_objfile, dwp_name.c_str ()));
82bf32bc
JK
13114 if (dbfd == NULL
13115 && strcmp (objfile->original_name, objfile_name (objfile)) != 0)
13116 {
13117 /* Try to find .dwp for the binary file after gdb_realpath resolving. */
d721ba37
PA
13118 dwp_name = objfile_name (objfile);
13119 dwp_name += ".dwp";
ed2dc618 13120 dbfd = open_dwp_file (dwarf2_per_objfile, dwp_name.c_str ());
82bf32bc
JK
13121 }
13122
80626a55
DE
13123 if (dbfd == NULL)
13124 {
b4f54984 13125 if (dwarf_read_debug)
d721ba37 13126 fprintf_unfiltered (gdb_stdlog, "DWP file not found: %s\n", dwp_name.c_str ());
80626a55 13127 return NULL;
3019eac3 13128 }
80626a55 13129 dwp_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_file);
192b62ce
TT
13130 dwp_file->name = bfd_get_filename (dbfd.get ());
13131 dwp_file->dbfd = dbfd.release ();
c906108c 13132
80626a55 13133 /* +1: section 0 is unused */
192b62ce 13134 dwp_file->num_sections = bfd_count_sections (dwp_file->dbfd) + 1;
80626a55
DE
13135 dwp_file->elf_sections =
13136 OBSTACK_CALLOC (&objfile->objfile_obstack,
13137 dwp_file->num_sections, asection *);
13138
192b62ce
TT
13139 bfd_map_over_sections (dwp_file->dbfd, dwarf2_locate_common_dwp_sections,
13140 dwp_file);
80626a55 13141
ed2dc618 13142 dwp_file->cus = create_dwp_hash_table (dwarf2_per_objfile, dwp_file, 0);
80626a55 13143
ed2dc618 13144 dwp_file->tus = create_dwp_hash_table (dwarf2_per_objfile, dwp_file, 1);
80626a55 13145
73869dc2 13146 /* The DWP file version is stored in the hash table. Oh well. */
08302ed2
DE
13147 if (dwp_file->cus && dwp_file->tus
13148 && dwp_file->cus->version != dwp_file->tus->version)
73869dc2
DE
13149 {
13150 /* Technically speaking, we should try to limp along, but this is
fbcbc3fd 13151 pretty bizarre. We use pulongest here because that's the established
4d65956b 13152 portability solution (e.g, we cannot use %u for uint32_t). */
fbcbc3fd
DE
13153 error (_("Dwarf Error: DWP file CU version %s doesn't match"
13154 " TU version %s [in DWP file %s]"),
13155 pulongest (dwp_file->cus->version),
d721ba37 13156 pulongest (dwp_file->tus->version), dwp_name.c_str ());
73869dc2 13157 }
08302ed2
DE
13158
13159 if (dwp_file->cus)
13160 dwp_file->version = dwp_file->cus->version;
13161 else if (dwp_file->tus)
13162 dwp_file->version = dwp_file->tus->version;
13163 else
13164 dwp_file->version = 2;
73869dc2
DE
13165
13166 if (dwp_file->version == 2)
192b62ce
TT
13167 bfd_map_over_sections (dwp_file->dbfd, dwarf2_locate_v2_dwp_sections,
13168 dwp_file);
73869dc2 13169
19ac8c2e
DE
13170 dwp_file->loaded_cus = allocate_dwp_loaded_cutus_table (objfile);
13171 dwp_file->loaded_tus = allocate_dwp_loaded_cutus_table (objfile);
80626a55 13172
b4f54984 13173 if (dwarf_read_debug)
80626a55
DE
13174 {
13175 fprintf_unfiltered (gdb_stdlog, "DWP file found: %s\n", dwp_file->name);
13176 fprintf_unfiltered (gdb_stdlog,
21aa081e
PA
13177 " %s CUs, %s TUs\n",
13178 pulongest (dwp_file->cus ? dwp_file->cus->nr_units : 0),
13179 pulongest (dwp_file->tus ? dwp_file->tus->nr_units : 0));
80626a55
DE
13180 }
13181
13182 return dwp_file;
3019eac3 13183}
c906108c 13184
ab5088bf
DE
13185/* Wrapper around open_and_init_dwp_file, only open it once. */
13186
13187static struct dwp_file *
ed2dc618 13188get_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
ab5088bf
DE
13189{
13190 if (! dwarf2_per_objfile->dwp_checked)
13191 {
ed2dc618
SM
13192 dwarf2_per_objfile->dwp_file
13193 = open_and_init_dwp_file (dwarf2_per_objfile);
ab5088bf
DE
13194 dwarf2_per_objfile->dwp_checked = 1;
13195 }
13196 return dwarf2_per_objfile->dwp_file;
13197}
13198
80626a55
DE
13199/* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
13200 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
13201 or in the DWP file for the objfile, referenced by THIS_UNIT.
3019eac3 13202 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
80626a55
DE
13203 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
13204
13205 This is called, for example, when wanting to read a variable with a
13206 complex location. Therefore we don't want to do file i/o for every call.
13207 Therefore we don't want to look for a DWO file on every call.
13208 Therefore we first see if we've already seen SIGNATURE in a DWP file,
13209 then we check if we've already seen DWO_NAME, and only THEN do we check
13210 for a DWO file.
13211
1c658ad5 13212 The result is a pointer to the dwo_unit object or NULL if we didn't find it
80626a55 13213 (dwo_id mismatch or couldn't find the DWO/DWP file). */
debd256d 13214
3019eac3 13215static struct dwo_unit *
80626a55
DE
13216lookup_dwo_cutu (struct dwarf2_per_cu_data *this_unit,
13217 const char *dwo_name, const char *comp_dir,
13218 ULONGEST signature, int is_debug_types)
3019eac3 13219{
ed2dc618 13220 struct dwarf2_per_objfile *dwarf2_per_objfile = this_unit->dwarf2_per_objfile;
3019eac3 13221 struct objfile *objfile = dwarf2_per_objfile->objfile;
80626a55
DE
13222 const char *kind = is_debug_types ? "TU" : "CU";
13223 void **dwo_file_slot;
3019eac3 13224 struct dwo_file *dwo_file;
80626a55 13225 struct dwp_file *dwp_file;
cb1df416 13226
6a506a2d
DE
13227 /* First see if there's a DWP file.
13228 If we have a DWP file but didn't find the DWO inside it, don't
13229 look for the original DWO file. It makes gdb behave differently
13230 depending on whether one is debugging in the build tree. */
cf2c3c16 13231
ed2dc618 13232 dwp_file = get_dwp_file (dwarf2_per_objfile);
80626a55 13233 if (dwp_file != NULL)
cf2c3c16 13234 {
80626a55
DE
13235 const struct dwp_hash_table *dwp_htab =
13236 is_debug_types ? dwp_file->tus : dwp_file->cus;
13237
13238 if (dwp_htab != NULL)
13239 {
13240 struct dwo_unit *dwo_cutu =
ed2dc618 13241 lookup_dwo_unit_in_dwp (dwarf2_per_objfile, dwp_file, comp_dir,
57d63ce2 13242 signature, is_debug_types);
80626a55
DE
13243
13244 if (dwo_cutu != NULL)
13245 {
b4f54984 13246 if (dwarf_read_debug)
80626a55
DE
13247 {
13248 fprintf_unfiltered (gdb_stdlog,
13249 "Virtual DWO %s %s found: @%s\n",
13250 kind, hex_string (signature),
13251 host_address_to_string (dwo_cutu));
13252 }
13253 return dwo_cutu;
13254 }
13255 }
13256 }
6a506a2d 13257 else
80626a55 13258 {
6a506a2d 13259 /* No DWP file, look for the DWO file. */
80626a55 13260
ed2dc618
SM
13261 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
13262 dwo_name, comp_dir);
6a506a2d 13263 if (*dwo_file_slot == NULL)
80626a55 13264 {
6a506a2d
DE
13265 /* Read in the file and build a table of the CUs/TUs it contains. */
13266 *dwo_file_slot = open_and_init_dwo_file (this_unit, dwo_name, comp_dir);
19c3d4c9 13267 }
6a506a2d 13268 /* NOTE: This will be NULL if unable to open the file. */
9a3c8263 13269 dwo_file = (struct dwo_file *) *dwo_file_slot;
3019eac3 13270
6a506a2d 13271 if (dwo_file != NULL)
19c3d4c9 13272 {
6a506a2d
DE
13273 struct dwo_unit *dwo_cutu = NULL;
13274
13275 if (is_debug_types && dwo_file->tus)
13276 {
13277 struct dwo_unit find_dwo_cutu;
13278
13279 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
13280 find_dwo_cutu.signature = signature;
9a3c8263
SM
13281 dwo_cutu
13282 = (struct dwo_unit *) htab_find (dwo_file->tus, &find_dwo_cutu);
6a506a2d 13283 }
33c5cd75 13284 else if (!is_debug_types && dwo_file->cus)
80626a55 13285 {
33c5cd75
DB
13286 struct dwo_unit find_dwo_cutu;
13287
13288 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
13289 find_dwo_cutu.signature = signature;
13290 dwo_cutu = (struct dwo_unit *)htab_find (dwo_file->cus,
13291 &find_dwo_cutu);
6a506a2d
DE
13292 }
13293
13294 if (dwo_cutu != NULL)
13295 {
b4f54984 13296 if (dwarf_read_debug)
6a506a2d
DE
13297 {
13298 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) found: @%s\n",
13299 kind, dwo_name, hex_string (signature),
13300 host_address_to_string (dwo_cutu));
13301 }
13302 return dwo_cutu;
80626a55
DE
13303 }
13304 }
2e276125 13305 }
9cdd5dbd 13306
80626a55
DE
13307 /* We didn't find it. This could mean a dwo_id mismatch, or
13308 someone deleted the DWO/DWP file, or the search path isn't set up
13309 correctly to find the file. */
13310
b4f54984 13311 if (dwarf_read_debug)
80626a55
DE
13312 {
13313 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) not found\n",
13314 kind, dwo_name, hex_string (signature));
13315 }
3019eac3 13316
6656a72d
DE
13317 /* This is a warning and not a complaint because it can be caused by
13318 pilot error (e.g., user accidentally deleting the DWO). */
43942612
DE
13319 {
13320 /* Print the name of the DWP file if we looked there, helps the user
13321 better diagnose the problem. */
791afaa2 13322 std::string dwp_text;
43942612
DE
13323
13324 if (dwp_file != NULL)
791afaa2
TT
13325 dwp_text = string_printf (" [in DWP file %s]",
13326 lbasename (dwp_file->name));
43942612 13327
9d8780f0 13328 warning (_("Could not find DWO %s %s(%s)%s referenced by %s at offset %s"
43942612
DE
13329 " [in module %s]"),
13330 kind, dwo_name, hex_string (signature),
791afaa2 13331 dwp_text.c_str (),
43942612 13332 this_unit->is_debug_types ? "TU" : "CU",
9d8780f0 13333 sect_offset_str (this_unit->sect_off), objfile_name (objfile));
43942612 13334 }
3019eac3 13335 return NULL;
5fb290d7
DJ
13336}
13337
80626a55
DE
13338/* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
13339 See lookup_dwo_cutu_unit for details. */
13340
13341static struct dwo_unit *
13342lookup_dwo_comp_unit (struct dwarf2_per_cu_data *this_cu,
13343 const char *dwo_name, const char *comp_dir,
13344 ULONGEST signature)
13345{
13346 return lookup_dwo_cutu (this_cu, dwo_name, comp_dir, signature, 0);
13347}
13348
13349/* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
13350 See lookup_dwo_cutu_unit for details. */
13351
13352static struct dwo_unit *
13353lookup_dwo_type_unit (struct signatured_type *this_tu,
13354 const char *dwo_name, const char *comp_dir)
13355{
13356 return lookup_dwo_cutu (&this_tu->per_cu, dwo_name, comp_dir, this_tu->signature, 1);
13357}
13358
89e63ee4
DE
13359/* Traversal function for queue_and_load_all_dwo_tus. */
13360
13361static int
13362queue_and_load_dwo_tu (void **slot, void *info)
13363{
13364 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
13365 struct dwarf2_per_cu_data *per_cu = (struct dwarf2_per_cu_data *) info;
13366 ULONGEST signature = dwo_unit->signature;
13367 struct signatured_type *sig_type =
13368 lookup_dwo_signatured_type (per_cu->cu, signature);
13369
13370 if (sig_type != NULL)
13371 {
13372 struct dwarf2_per_cu_data *sig_cu = &sig_type->per_cu;
13373
13374 /* We pass NULL for DEPENDENT_CU because we don't yet know if there's
13375 a real dependency of PER_CU on SIG_TYPE. That is detected later
13376 while processing PER_CU. */
13377 if (maybe_queue_comp_unit (NULL, sig_cu, per_cu->cu->language))
13378 load_full_type_unit (sig_cu);
13379 VEC_safe_push (dwarf2_per_cu_ptr, per_cu->imported_symtabs, sig_cu);
13380 }
13381
13382 return 1;
13383}
13384
13385/* Queue all TUs contained in the DWO of PER_CU to be read in.
13386 The DWO may have the only definition of the type, though it may not be
13387 referenced anywhere in PER_CU. Thus we have to load *all* its TUs.
13388 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
13389
13390static void
13391queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *per_cu)
13392{
13393 struct dwo_unit *dwo_unit;
13394 struct dwo_file *dwo_file;
13395
13396 gdb_assert (!per_cu->is_debug_types);
ed2dc618 13397 gdb_assert (get_dwp_file (per_cu->dwarf2_per_objfile) == NULL);
89e63ee4
DE
13398 gdb_assert (per_cu->cu != NULL);
13399
13400 dwo_unit = per_cu->cu->dwo_unit;
13401 gdb_assert (dwo_unit != NULL);
13402
13403 dwo_file = dwo_unit->dwo_file;
13404 if (dwo_file->tus != NULL)
13405 htab_traverse_noresize (dwo_file->tus, queue_and_load_dwo_tu, per_cu);
13406}
13407
3019eac3 13408/* Free all resources associated with DWO_FILE.
5dafb3d1 13409 Close the DWO file and munmap the sections. */
348e048f
DE
13410
13411static void
5dafb3d1 13412free_dwo_file (struct dwo_file *dwo_file)
348e048f 13413{
5c6fa7ab 13414 /* Note: dbfd is NULL for virtual DWO files. */
80626a55 13415 gdb_bfd_unref (dwo_file->dbfd);
348e048f 13416
3019eac3
DE
13417 VEC_free (dwarf2_section_info_def, dwo_file->sections.types);
13418}
348e048f 13419
3019eac3 13420/* Traversal function for free_dwo_files. */
2ab95328 13421
3019eac3
DE
13422static int
13423free_dwo_file_from_slot (void **slot, void *info)
13424{
13425 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
348e048f 13426
5dafb3d1 13427 free_dwo_file (dwo_file);
348e048f 13428
3019eac3
DE
13429 return 1;
13430}
348e048f 13431
3019eac3 13432/* Free all resources associated with DWO_FILES. */
348e048f 13433
3019eac3
DE
13434static void
13435free_dwo_files (htab_t dwo_files, struct objfile *objfile)
13436{
13437 htab_traverse_noresize (dwo_files, free_dwo_file_from_slot, objfile);
348e048f 13438}
3019eac3
DE
13439\f
13440/* Read in various DIEs. */
348e048f 13441
d389af10 13442/* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
3e43a32a
MS
13443 Inherit only the children of the DW_AT_abstract_origin DIE not being
13444 already referenced by DW_AT_abstract_origin from the children of the
13445 current DIE. */
d389af10
JK
13446
13447static void
13448inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
13449{
13450 struct die_info *child_die;
791afaa2 13451 sect_offset *offsetp;
d389af10
JK
13452 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
13453 struct die_info *origin_die;
13454 /* Iterator of the ORIGIN_DIE children. */
13455 struct die_info *origin_child_die;
d389af10 13456 struct attribute *attr;
cd02d79d
PA
13457 struct dwarf2_cu *origin_cu;
13458 struct pending **origin_previous_list_in_scope;
d389af10
JK
13459
13460 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13461 if (!attr)
13462 return;
13463
cd02d79d
PA
13464 /* Note that following die references may follow to a die in a
13465 different cu. */
13466
13467 origin_cu = cu;
13468 origin_die = follow_die_ref (die, attr, &origin_cu);
13469
13470 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
13471 symbols in. */
13472 origin_previous_list_in_scope = origin_cu->list_in_scope;
13473 origin_cu->list_in_scope = cu->list_in_scope;
13474
edb3359d
DJ
13475 if (die->tag != origin_die->tag
13476 && !(die->tag == DW_TAG_inlined_subroutine
13477 && origin_die->tag == DW_TAG_subprogram))
d389af10 13478 complaint (&symfile_complaints,
9d8780f0
SM
13479 _("DIE %s and its abstract origin %s have different tags"),
13480 sect_offset_str (die->sect_off),
13481 sect_offset_str (origin_die->sect_off));
d389af10 13482
791afaa2 13483 std::vector<sect_offset> offsets;
d389af10 13484
3ea89b92
PMR
13485 for (child_die = die->child;
13486 child_die && child_die->tag;
13487 child_die = sibling_die (child_die))
13488 {
13489 struct die_info *child_origin_die;
13490 struct dwarf2_cu *child_origin_cu;
13491
13492 /* We are trying to process concrete instance entries:
216f72a1 13493 DW_TAG_call_site DIEs indeed have a DW_AT_abstract_origin tag, but
3ea89b92
PMR
13494 it's not relevant to our analysis here. i.e. detecting DIEs that are
13495 present in the abstract instance but not referenced in the concrete
13496 one. */
216f72a1
JK
13497 if (child_die->tag == DW_TAG_call_site
13498 || child_die->tag == DW_TAG_GNU_call_site)
3ea89b92
PMR
13499 continue;
13500
c38f313d
DJ
13501 /* For each CHILD_DIE, find the corresponding child of
13502 ORIGIN_DIE. If there is more than one layer of
13503 DW_AT_abstract_origin, follow them all; there shouldn't be,
13504 but GCC versions at least through 4.4 generate this (GCC PR
13505 40573). */
3ea89b92
PMR
13506 child_origin_die = child_die;
13507 child_origin_cu = cu;
c38f313d
DJ
13508 while (1)
13509 {
cd02d79d
PA
13510 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
13511 child_origin_cu);
c38f313d
DJ
13512 if (attr == NULL)
13513 break;
cd02d79d
PA
13514 child_origin_die = follow_die_ref (child_origin_die, attr,
13515 &child_origin_cu);
c38f313d
DJ
13516 }
13517
d389af10
JK
13518 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
13519 counterpart may exist. */
c38f313d 13520 if (child_origin_die != child_die)
d389af10 13521 {
edb3359d
DJ
13522 if (child_die->tag != child_origin_die->tag
13523 && !(child_die->tag == DW_TAG_inlined_subroutine
13524 && child_origin_die->tag == DW_TAG_subprogram))
d389af10 13525 complaint (&symfile_complaints,
9d8780f0 13526 _("Child DIE %s and its abstract origin %s have "
9c541725 13527 "different tags"),
9d8780f0
SM
13528 sect_offset_str (child_die->sect_off),
13529 sect_offset_str (child_origin_die->sect_off));
c38f313d
DJ
13530 if (child_origin_die->parent != origin_die)
13531 complaint (&symfile_complaints,
9d8780f0 13532 _("Child DIE %s and its abstract origin %s have "
9c541725 13533 "different parents"),
9d8780f0
SM
13534 sect_offset_str (child_die->sect_off),
13535 sect_offset_str (child_origin_die->sect_off));
c38f313d 13536 else
791afaa2 13537 offsets.push_back (child_origin_die->sect_off);
d389af10 13538 }
d389af10 13539 }
791afaa2
TT
13540 std::sort (offsets.begin (), offsets.end ());
13541 sect_offset *offsets_end = offsets.data () + offsets.size ();
13542 for (offsetp = offsets.data () + 1; offsetp < offsets_end; offsetp++)
9c541725 13543 if (offsetp[-1] == *offsetp)
3e43a32a 13544 complaint (&symfile_complaints,
9d8780f0
SM
13545 _("Multiple children of DIE %s refer "
13546 "to DIE %s as their abstract origin"),
13547 sect_offset_str (die->sect_off), sect_offset_str (*offsetp));
d389af10 13548
791afaa2 13549 offsetp = offsets.data ();
d389af10
JK
13550 origin_child_die = origin_die->child;
13551 while (origin_child_die && origin_child_die->tag)
13552 {
13553 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
b64f50a1 13554 while (offsetp < offsets_end
9c541725 13555 && *offsetp < origin_child_die->sect_off)
d389af10 13556 offsetp++;
b64f50a1 13557 if (offsetp >= offsets_end
9c541725 13558 || *offsetp > origin_child_die->sect_off)
d389af10 13559 {
adde2bff
DE
13560 /* Found that ORIGIN_CHILD_DIE is really not referenced.
13561 Check whether we're already processing ORIGIN_CHILD_DIE.
13562 This can happen with mutually referenced abstract_origins.
13563 PR 16581. */
13564 if (!origin_child_die->in_process)
13565 process_die (origin_child_die, origin_cu);
d389af10
JK
13566 }
13567 origin_child_die = sibling_die (origin_child_die);
13568 }
cd02d79d 13569 origin_cu->list_in_scope = origin_previous_list_in_scope;
d389af10
JK
13570}
13571
c906108c 13572static void
e7c27a73 13573read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 13574{
518817b3 13575 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
3e29f34a 13576 struct gdbarch *gdbarch = get_objfile_arch (objfile);
fe978cb0 13577 struct context_stack *newobj;
c906108c
SS
13578 CORE_ADDR lowpc;
13579 CORE_ADDR highpc;
13580 struct die_info *child_die;
edb3359d 13581 struct attribute *attr, *call_line, *call_file;
15d034d0 13582 const char *name;
e142c38c 13583 CORE_ADDR baseaddr;
801e3a5b 13584 struct block *block;
edb3359d 13585 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
2f4732b0 13586 std::vector<struct symbol *> template_args;
34eaf542 13587 struct template_symbol *templ_func = NULL;
edb3359d
DJ
13588
13589 if (inlined_func)
13590 {
13591 /* If we do not have call site information, we can't show the
13592 caller of this inlined function. That's too confusing, so
13593 only use the scope for local variables. */
13594 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
13595 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
13596 if (call_line == NULL || call_file == NULL)
13597 {
13598 read_lexical_block_scope (die, cu);
13599 return;
13600 }
13601 }
c906108c 13602
e142c38c
DJ
13603 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
13604
94af9270 13605 name = dwarf2_name (die, cu);
c906108c 13606
e8d05480
JB
13607 /* Ignore functions with missing or empty names. These are actually
13608 illegal according to the DWARF standard. */
13609 if (name == NULL)
13610 {
13611 complaint (&symfile_complaints,
9d8780f0
SM
13612 _("missing name for subprogram DIE at %s"),
13613 sect_offset_str (die->sect_off));
e8d05480
JB
13614 return;
13615 }
13616
13617 /* Ignore functions with missing or invalid low and high pc attributes. */
3a2b436a 13618 if (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL)
e385593e 13619 <= PC_BOUNDS_INVALID)
e8d05480 13620 {
ae4d0c03
PM
13621 attr = dwarf2_attr (die, DW_AT_external, cu);
13622 if (!attr || !DW_UNSND (attr))
13623 complaint (&symfile_complaints,
3e43a32a 13624 _("cannot get low and high bounds "
9d8780f0
SM
13625 "for subprogram DIE at %s"),
13626 sect_offset_str (die->sect_off));
e8d05480
JB
13627 return;
13628 }
c906108c 13629
3e29f34a
MR
13630 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13631 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
c906108c 13632
34eaf542
TT
13633 /* If we have any template arguments, then we must allocate a
13634 different sort of symbol. */
13635 for (child_die = die->child; child_die; child_die = sibling_die (child_die))
13636 {
13637 if (child_die->tag == DW_TAG_template_type_param
13638 || child_die->tag == DW_TAG_template_value_param)
13639 {
e623cf5d 13640 templ_func = allocate_template_symbol (objfile);
cf724bc9 13641 templ_func->subclass = SYMBOL_TEMPLATE;
34eaf542
TT
13642 break;
13643 }
13644 }
13645
fe978cb0 13646 newobj = push_context (0, lowpc);
5e2db402
TT
13647 newobj->name = new_symbol (die, read_type_die (die, cu), cu,
13648 (struct symbol *) templ_func);
4c2df51b 13649
4cecd739
DJ
13650 /* If there is a location expression for DW_AT_frame_base, record
13651 it. */
e142c38c 13652 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
4c2df51b 13653 if (attr)
fe978cb0 13654 dwarf2_symbol_mark_computed (attr, newobj->name, cu, 1);
4c2df51b 13655
63e43d3a
PMR
13656 /* If there is a location for the static link, record it. */
13657 newobj->static_link = NULL;
13658 attr = dwarf2_attr (die, DW_AT_static_link, cu);
13659 if (attr)
13660 {
224c3ddb
SM
13661 newobj->static_link
13662 = XOBNEW (&objfile->objfile_obstack, struct dynamic_prop);
63e43d3a
PMR
13663 attr_to_dynamic_prop (attr, die, cu, newobj->static_link);
13664 }
13665
e142c38c 13666 cu->list_in_scope = &local_symbols;
c906108c 13667
639d11d3 13668 if (die->child != NULL)
c906108c 13669 {
639d11d3 13670 child_die = die->child;
c906108c
SS
13671 while (child_die && child_die->tag)
13672 {
34eaf542
TT
13673 if (child_die->tag == DW_TAG_template_type_param
13674 || child_die->tag == DW_TAG_template_value_param)
13675 {
13676 struct symbol *arg = new_symbol (child_die, NULL, cu);
13677
f1078f66 13678 if (arg != NULL)
2f4732b0 13679 template_args.push_back (arg);
34eaf542
TT
13680 }
13681 else
13682 process_die (child_die, cu);
c906108c
SS
13683 child_die = sibling_die (child_die);
13684 }
13685 }
13686
d389af10
JK
13687 inherit_abstract_dies (die, cu);
13688
4a811a97
UW
13689 /* If we have a DW_AT_specification, we might need to import using
13690 directives from the context of the specification DIE. See the
13691 comment in determine_prefix. */
13692 if (cu->language == language_cplus
13693 && dwarf2_attr (die, DW_AT_specification, cu))
13694 {
13695 struct dwarf2_cu *spec_cu = cu;
13696 struct die_info *spec_die = die_specification (die, &spec_cu);
13697
13698 while (spec_die)
13699 {
13700 child_die = spec_die->child;
13701 while (child_die && child_die->tag)
13702 {
13703 if (child_die->tag == DW_TAG_imported_module)
13704 process_die (child_die, spec_cu);
13705 child_die = sibling_die (child_die);
13706 }
13707
13708 /* In some cases, GCC generates specification DIEs that
13709 themselves contain DW_AT_specification attributes. */
13710 spec_die = die_specification (spec_die, &spec_cu);
13711 }
13712 }
13713
fe978cb0 13714 newobj = pop_context ();
c906108c 13715 /* Make a block for the local symbols within. */
fe978cb0 13716 block = finish_block (newobj->name, &local_symbols, newobj->old_blocks,
63e43d3a 13717 newobj->static_link, lowpc, highpc);
801e3a5b 13718
df8a16a1 13719 /* For C++, set the block's scope. */
45280282
IB
13720 if ((cu->language == language_cplus
13721 || cu->language == language_fortran
c44af4eb
TT
13722 || cu->language == language_d
13723 || cu->language == language_rust)
4d4ec4e5 13724 && cu->processing_has_namespace_info)
195a3f6c
TT
13725 block_set_scope (block, determine_prefix (die, cu),
13726 &objfile->objfile_obstack);
df8a16a1 13727
801e3a5b
JB
13728 /* If we have address ranges, record them. */
13729 dwarf2_record_block_ranges (die, block, baseaddr, cu);
6e70227d 13730
fe978cb0 13731 gdbarch_make_symbol_special (gdbarch, newobj->name, objfile);
3e29f34a 13732
34eaf542 13733 /* Attach template arguments to function. */
2f4732b0 13734 if (!template_args.empty ())
34eaf542
TT
13735 {
13736 gdb_assert (templ_func != NULL);
13737
2f4732b0 13738 templ_func->n_template_arguments = template_args.size ();
34eaf542 13739 templ_func->template_arguments
8d749320
SM
13740 = XOBNEWVEC (&objfile->objfile_obstack, struct symbol *,
13741 templ_func->n_template_arguments);
34eaf542 13742 memcpy (templ_func->template_arguments,
2f4732b0 13743 template_args.data (),
34eaf542 13744 (templ_func->n_template_arguments * sizeof (struct symbol *)));
34eaf542
TT
13745 }
13746
208d8187
JB
13747 /* In C++, we can have functions nested inside functions (e.g., when
13748 a function declares a class that has methods). This means that
13749 when we finish processing a function scope, we may need to go
13750 back to building a containing block's symbol lists. */
fe978cb0 13751 local_symbols = newobj->locals;
22cee43f 13752 local_using_directives = newobj->local_using_directives;
208d8187 13753
921e78cf
JB
13754 /* If we've finished processing a top-level function, subsequent
13755 symbols go in the file symbol list. */
13756 if (outermost_context_p ())
e142c38c 13757 cu->list_in_scope = &file_symbols;
c906108c
SS
13758}
13759
13760/* Process all the DIES contained within a lexical block scope. Start
13761 a new scope, process the dies, and then close the scope. */
13762
13763static void
e7c27a73 13764read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 13765{
518817b3 13766 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
3e29f34a 13767 struct gdbarch *gdbarch = get_objfile_arch (objfile);
fe978cb0 13768 struct context_stack *newobj;
c906108c
SS
13769 CORE_ADDR lowpc, highpc;
13770 struct die_info *child_die;
e142c38c
DJ
13771 CORE_ADDR baseaddr;
13772
13773 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c
SS
13774
13775 /* Ignore blocks with missing or invalid low and high pc attributes. */
af34e669
DJ
13776 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
13777 as multiple lexical blocks? Handling children in a sane way would
6e70227d 13778 be nasty. Might be easier to properly extend generic blocks to
af34e669 13779 describe ranges. */
e385593e
JK
13780 switch (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
13781 {
13782 case PC_BOUNDS_NOT_PRESENT:
13783 /* DW_TAG_lexical_block has no attributes, process its children as if
13784 there was no wrapping by that DW_TAG_lexical_block.
13785 GCC does no longer produces such DWARF since GCC r224161. */
13786 for (child_die = die->child;
13787 child_die != NULL && child_die->tag;
13788 child_die = sibling_die (child_die))
13789 process_die (child_die, cu);
13790 return;
13791 case PC_BOUNDS_INVALID:
13792 return;
13793 }
3e29f34a
MR
13794 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13795 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
c906108c
SS
13796
13797 push_context (0, lowpc);
639d11d3 13798 if (die->child != NULL)
c906108c 13799 {
639d11d3 13800 child_die = die->child;
c906108c
SS
13801 while (child_die && child_die->tag)
13802 {
e7c27a73 13803 process_die (child_die, cu);
c906108c
SS
13804 child_die = sibling_die (child_die);
13805 }
13806 }
3ea89b92 13807 inherit_abstract_dies (die, cu);
fe978cb0 13808 newobj = pop_context ();
c906108c 13809
22cee43f 13810 if (local_symbols != NULL || local_using_directives != NULL)
c906108c 13811 {
801e3a5b 13812 struct block *block
63e43d3a 13813 = finish_block (0, &local_symbols, newobj->old_blocks, NULL,
fe978cb0 13814 newobj->start_addr, highpc);
801e3a5b
JB
13815
13816 /* Note that recording ranges after traversing children, as we
13817 do here, means that recording a parent's ranges entails
13818 walking across all its children's ranges as they appear in
13819 the address map, which is quadratic behavior.
13820
13821 It would be nicer to record the parent's ranges before
13822 traversing its children, simply overriding whatever you find
13823 there. But since we don't even decide whether to create a
13824 block until after we've traversed its children, that's hard
13825 to do. */
13826 dwarf2_record_block_ranges (die, block, baseaddr, cu);
c906108c 13827 }
fe978cb0 13828 local_symbols = newobj->locals;
22cee43f 13829 local_using_directives = newobj->local_using_directives;
c906108c
SS
13830}
13831
216f72a1 13832/* Read in DW_TAG_call_site and insert it to CU->call_site_htab. */
96408a79
SA
13833
13834static void
13835read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
13836{
518817b3 13837 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
96408a79
SA
13838 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13839 CORE_ADDR pc, baseaddr;
13840 struct attribute *attr;
13841 struct call_site *call_site, call_site_local;
13842 void **slot;
13843 int nparams;
13844 struct die_info *child_die;
13845
13846 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
13847
216f72a1
JK
13848 attr = dwarf2_attr (die, DW_AT_call_return_pc, cu);
13849 if (attr == NULL)
13850 {
13851 /* This was a pre-DWARF-5 GNU extension alias
13852 for DW_AT_call_return_pc. */
13853 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
13854 }
96408a79
SA
13855 if (!attr)
13856 {
13857 complaint (&symfile_complaints,
216f72a1 13858 _("missing DW_AT_call_return_pc for DW_TAG_call_site "
9d8780f0
SM
13859 "DIE %s [in module %s]"),
13860 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79
SA
13861 return;
13862 }
31aa7e4e 13863 pc = attr_value_as_address (attr) + baseaddr;
3e29f34a 13864 pc = gdbarch_adjust_dwarf2_addr (gdbarch, pc);
96408a79
SA
13865
13866 if (cu->call_site_htab == NULL)
13867 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
13868 NULL, &objfile->objfile_obstack,
13869 hashtab_obstack_allocate, NULL);
13870 call_site_local.pc = pc;
13871 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
13872 if (*slot != NULL)
13873 {
13874 complaint (&symfile_complaints,
216f72a1 13875 _("Duplicate PC %s for DW_TAG_call_site "
9d8780f0
SM
13876 "DIE %s [in module %s]"),
13877 paddress (gdbarch, pc), sect_offset_str (die->sect_off),
4262abfb 13878 objfile_name (objfile));
96408a79
SA
13879 return;
13880 }
13881
13882 /* Count parameters at the caller. */
13883
13884 nparams = 0;
13885 for (child_die = die->child; child_die && child_die->tag;
13886 child_die = sibling_die (child_die))
13887 {
216f72a1
JK
13888 if (child_die->tag != DW_TAG_call_site_parameter
13889 && child_die->tag != DW_TAG_GNU_call_site_parameter)
96408a79
SA
13890 {
13891 complaint (&symfile_complaints,
216f72a1 13892 _("Tag %d is not DW_TAG_call_site_parameter in "
9d8780f0
SM
13893 "DW_TAG_call_site child DIE %s [in module %s]"),
13894 child_die->tag, sect_offset_str (child_die->sect_off),
4262abfb 13895 objfile_name (objfile));
96408a79
SA
13896 continue;
13897 }
13898
13899 nparams++;
13900 }
13901
224c3ddb
SM
13902 call_site
13903 = ((struct call_site *)
13904 obstack_alloc (&objfile->objfile_obstack,
13905 sizeof (*call_site)
13906 + (sizeof (*call_site->parameter) * (nparams - 1))));
96408a79
SA
13907 *slot = call_site;
13908 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
13909 call_site->pc = pc;
13910
216f72a1
JK
13911 if (dwarf2_flag_true_p (die, DW_AT_call_tail_call, cu)
13912 || dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
96408a79
SA
13913 {
13914 struct die_info *func_die;
13915
13916 /* Skip also over DW_TAG_inlined_subroutine. */
13917 for (func_die = die->parent;
13918 func_die && func_die->tag != DW_TAG_subprogram
13919 && func_die->tag != DW_TAG_subroutine_type;
13920 func_die = func_die->parent);
13921
216f72a1
JK
13922 /* DW_AT_call_all_calls is a superset
13923 of DW_AT_call_all_tail_calls. */
96408a79 13924 if (func_die
216f72a1 13925 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_calls, cu)
96408a79 13926 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
216f72a1 13927 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_tail_calls, cu)
96408a79
SA
13928 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
13929 {
13930 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
13931 not complete. But keep CALL_SITE for look ups via call_site_htab,
13932 both the initial caller containing the real return address PC and
13933 the final callee containing the current PC of a chain of tail
13934 calls do not need to have the tail call list complete. But any
13935 function candidate for a virtual tail call frame searched via
13936 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
13937 determined unambiguously. */
13938 }
13939 else
13940 {
13941 struct type *func_type = NULL;
13942
13943 if (func_die)
13944 func_type = get_die_type (func_die, cu);
13945 if (func_type != NULL)
13946 {
13947 gdb_assert (TYPE_CODE (func_type) == TYPE_CODE_FUNC);
13948
13949 /* Enlist this call site to the function. */
13950 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
13951 TYPE_TAIL_CALL_LIST (func_type) = call_site;
13952 }
13953 else
13954 complaint (&symfile_complaints,
216f72a1 13955 _("Cannot find function owning DW_TAG_call_site "
9d8780f0
SM
13956 "DIE %s [in module %s]"),
13957 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79
SA
13958 }
13959 }
13960
216f72a1
JK
13961 attr = dwarf2_attr (die, DW_AT_call_target, cu);
13962 if (attr == NULL)
13963 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
13964 if (attr == NULL)
13965 attr = dwarf2_attr (die, DW_AT_call_origin, cu);
96408a79 13966 if (attr == NULL)
216f72a1
JK
13967 {
13968 /* This was a pre-DWARF-5 GNU extension alias for DW_AT_call_origin. */
13969 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13970 }
96408a79
SA
13971 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
13972 if (!attr || (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0))
13973 /* Keep NULL DWARF_BLOCK. */;
13974 else if (attr_form_is_block (attr))
13975 {
13976 struct dwarf2_locexpr_baton *dlbaton;
13977
8d749320 13978 dlbaton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
96408a79
SA
13979 dlbaton->data = DW_BLOCK (attr)->data;
13980 dlbaton->size = DW_BLOCK (attr)->size;
13981 dlbaton->per_cu = cu->per_cu;
13982
13983 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
13984 }
7771576e 13985 else if (attr_form_is_ref (attr))
96408a79 13986 {
96408a79
SA
13987 struct dwarf2_cu *target_cu = cu;
13988 struct die_info *target_die;
13989
ac9ec31b 13990 target_die = follow_die_ref (die, attr, &target_cu);
518817b3 13991 gdb_assert (target_cu->per_cu->dwarf2_per_objfile->objfile == objfile);
96408a79
SA
13992 if (die_is_declaration (target_die, target_cu))
13993 {
7d45c7c3 13994 const char *target_physname;
9112db09
JK
13995
13996 /* Prefer the mangled name; otherwise compute the demangled one. */
73b9be8b 13997 target_physname = dw2_linkage_name (target_die, target_cu);
7d45c7c3 13998 if (target_physname == NULL)
9112db09 13999 target_physname = dwarf2_physname (NULL, target_die, target_cu);
96408a79
SA
14000 if (target_physname == NULL)
14001 complaint (&symfile_complaints,
216f72a1 14002 _("DW_AT_call_target target DIE has invalid "
9d8780f0
SM
14003 "physname, for referencing DIE %s [in module %s]"),
14004 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79 14005 else
7d455152 14006 SET_FIELD_PHYSNAME (call_site->target, target_physname);
96408a79
SA
14007 }
14008 else
14009 {
14010 CORE_ADDR lowpc;
14011
14012 /* DW_AT_entry_pc should be preferred. */
3a2b436a 14013 if (dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL)
e385593e 14014 <= PC_BOUNDS_INVALID)
96408a79 14015 complaint (&symfile_complaints,
216f72a1 14016 _("DW_AT_call_target target DIE has invalid "
9d8780f0
SM
14017 "low pc, for referencing DIE %s [in module %s]"),
14018 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79 14019 else
3e29f34a
MR
14020 {
14021 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
14022 SET_FIELD_PHYSADDR (call_site->target, lowpc);
14023 }
96408a79
SA
14024 }
14025 }
14026 else
14027 complaint (&symfile_complaints,
216f72a1 14028 _("DW_TAG_call_site DW_AT_call_target is neither "
9d8780f0
SM
14029 "block nor reference, for DIE %s [in module %s]"),
14030 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79
SA
14031
14032 call_site->per_cu = cu->per_cu;
14033
14034 for (child_die = die->child;
14035 child_die && child_die->tag;
14036 child_die = sibling_die (child_die))
14037 {
96408a79 14038 struct call_site_parameter *parameter;
1788b2d3 14039 struct attribute *loc, *origin;
96408a79 14040
216f72a1
JK
14041 if (child_die->tag != DW_TAG_call_site_parameter
14042 && child_die->tag != DW_TAG_GNU_call_site_parameter)
96408a79
SA
14043 {
14044 /* Already printed the complaint above. */
14045 continue;
14046 }
14047
14048 gdb_assert (call_site->parameter_count < nparams);
14049 parameter = &call_site->parameter[call_site->parameter_count];
14050
1788b2d3
JK
14051 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
14052 specifies DW_TAG_formal_parameter. Value of the data assumed for the
216f72a1 14053 register is contained in DW_AT_call_value. */
96408a79 14054
24c5c679 14055 loc = dwarf2_attr (child_die, DW_AT_location, cu);
216f72a1
JK
14056 origin = dwarf2_attr (child_die, DW_AT_call_parameter, cu);
14057 if (origin == NULL)
14058 {
14059 /* This was a pre-DWARF-5 GNU extension alias
14060 for DW_AT_call_parameter. */
14061 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
14062 }
7771576e 14063 if (loc == NULL && origin != NULL && attr_form_is_ref (origin))
1788b2d3 14064 {
1788b2d3 14065 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
9c541725
PA
14066
14067 sect_offset sect_off
14068 = (sect_offset) dwarf2_get_ref_die_offset (origin);
14069 if (!offset_in_cu_p (&cu->header, sect_off))
d76b7dbc
JK
14070 {
14071 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
14072 binding can be done only inside one CU. Such referenced DIE
14073 therefore cannot be even moved to DW_TAG_partial_unit. */
14074 complaint (&symfile_complaints,
216f72a1 14075 _("DW_AT_call_parameter offset is not in CU for "
9d8780f0
SM
14076 "DW_TAG_call_site child DIE %s [in module %s]"),
14077 sect_offset_str (child_die->sect_off),
9c541725 14078 objfile_name (objfile));
d76b7dbc
JK
14079 continue;
14080 }
9c541725
PA
14081 parameter->u.param_cu_off
14082 = (cu_offset) (sect_off - cu->header.sect_off);
1788b2d3
JK
14083 }
14084 else if (loc == NULL || origin != NULL || !attr_form_is_block (loc))
96408a79
SA
14085 {
14086 complaint (&symfile_complaints,
14087 _("No DW_FORM_block* DW_AT_location for "
9d8780f0
SM
14088 "DW_TAG_call_site child DIE %s [in module %s]"),
14089 sect_offset_str (child_die->sect_off), objfile_name (objfile));
96408a79
SA
14090 continue;
14091 }
24c5c679 14092 else
96408a79 14093 {
24c5c679
JK
14094 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
14095 (DW_BLOCK (loc)->data, &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size]);
14096 if (parameter->u.dwarf_reg != -1)
14097 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
14098 else if (dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (loc)->data,
14099 &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size],
14100 &parameter->u.fb_offset))
14101 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
14102 else
14103 {
14104 complaint (&symfile_complaints,
14105 _("Only single DW_OP_reg or DW_OP_fbreg is supported "
14106 "for DW_FORM_block* DW_AT_location is supported for "
9d8780f0 14107 "DW_TAG_call_site child DIE %s "
24c5c679 14108 "[in module %s]"),
9d8780f0 14109 sect_offset_str (child_die->sect_off),
9c541725 14110 objfile_name (objfile));
24c5c679
JK
14111 continue;
14112 }
96408a79
SA
14113 }
14114
216f72a1
JK
14115 attr = dwarf2_attr (child_die, DW_AT_call_value, cu);
14116 if (attr == NULL)
14117 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
96408a79
SA
14118 if (!attr_form_is_block (attr))
14119 {
14120 complaint (&symfile_complaints,
216f72a1 14121 _("No DW_FORM_block* DW_AT_call_value for "
9d8780f0
SM
14122 "DW_TAG_call_site child DIE %s [in module %s]"),
14123 sect_offset_str (child_die->sect_off),
9c541725 14124 objfile_name (objfile));
96408a79
SA
14125 continue;
14126 }
14127 parameter->value = DW_BLOCK (attr)->data;
14128 parameter->value_size = DW_BLOCK (attr)->size;
14129
14130 /* Parameters are not pre-cleared by memset above. */
14131 parameter->data_value = NULL;
14132 parameter->data_value_size = 0;
14133 call_site->parameter_count++;
14134
216f72a1
JK
14135 attr = dwarf2_attr (child_die, DW_AT_call_data_value, cu);
14136 if (attr == NULL)
14137 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
96408a79
SA
14138 if (attr)
14139 {
14140 if (!attr_form_is_block (attr))
14141 complaint (&symfile_complaints,
216f72a1 14142 _("No DW_FORM_block* DW_AT_call_data_value for "
9d8780f0
SM
14143 "DW_TAG_call_site child DIE %s [in module %s]"),
14144 sect_offset_str (child_die->sect_off),
9c541725 14145 objfile_name (objfile));
96408a79
SA
14146 else
14147 {
14148 parameter->data_value = DW_BLOCK (attr)->data;
14149 parameter->data_value_size = DW_BLOCK (attr)->size;
14150 }
14151 }
14152 }
14153}
14154
71a3c369
TT
14155/* Helper function for read_variable. If DIE represents a virtual
14156 table, then return the type of the concrete object that is
14157 associated with the virtual table. Otherwise, return NULL. */
14158
14159static struct type *
14160rust_containing_type (struct die_info *die, struct dwarf2_cu *cu)
14161{
14162 struct attribute *attr = dwarf2_attr (die, DW_AT_type, cu);
14163 if (attr == NULL)
14164 return NULL;
14165
14166 /* Find the type DIE. */
14167 struct die_info *type_die = NULL;
14168 struct dwarf2_cu *type_cu = cu;
14169
14170 if (attr_form_is_ref (attr))
14171 type_die = follow_die_ref (die, attr, &type_cu);
14172 if (type_die == NULL)
14173 return NULL;
14174
14175 if (dwarf2_attr (type_die, DW_AT_containing_type, type_cu) == NULL)
14176 return NULL;
14177 return die_containing_type (type_die, type_cu);
14178}
14179
14180/* Read a variable (DW_TAG_variable) DIE and create a new symbol. */
14181
14182static void
14183read_variable (struct die_info *die, struct dwarf2_cu *cu)
14184{
14185 struct rust_vtable_symbol *storage = NULL;
14186
14187 if (cu->language == language_rust)
14188 {
14189 struct type *containing_type = rust_containing_type (die, cu);
14190
14191 if (containing_type != NULL)
14192 {
518817b3 14193 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
71a3c369
TT
14194
14195 storage = OBSTACK_ZALLOC (&objfile->objfile_obstack,
14196 struct rust_vtable_symbol);
14197 initialize_objfile_symbol (storage);
14198 storage->concrete_type = containing_type;
cf724bc9 14199 storage->subclass = SYMBOL_RUST_VTABLE;
71a3c369
TT
14200 }
14201 }
14202
5e2db402 14203 new_symbol (die, NULL, cu, storage);
71a3c369
TT
14204}
14205
43988095
JK
14206/* Call CALLBACK from DW_AT_ranges attribute value OFFSET
14207 reading .debug_rnglists.
14208 Callback's type should be:
14209 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
14210 Return true if the attributes are present and valid, otherwise,
14211 return false. */
14212
14213template <typename Callback>
14214static bool
14215dwarf2_rnglists_process (unsigned offset, struct dwarf2_cu *cu,
14216 Callback &&callback)
14217{
ed2dc618 14218 struct dwarf2_per_objfile *dwarf2_per_objfile
518817b3 14219 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 14220 struct objfile *objfile = dwarf2_per_objfile->objfile;
43988095 14221 bfd *obfd = objfile->obfd;
43988095
JK
14222 /* Base address selection entry. */
14223 CORE_ADDR base;
14224 int found_base;
43988095 14225 const gdb_byte *buffer;
43988095
JK
14226 CORE_ADDR baseaddr;
14227 bool overflow = false;
14228
14229 found_base = cu->base_known;
14230 base = cu->base_address;
14231
14232 dwarf2_read_section (objfile, &dwarf2_per_objfile->rnglists);
14233 if (offset >= dwarf2_per_objfile->rnglists.size)
14234 {
14235 complaint (&symfile_complaints,
14236 _("Offset %d out of bounds for DW_AT_ranges attribute"),
14237 offset);
14238 return false;
14239 }
14240 buffer = dwarf2_per_objfile->rnglists.buffer + offset;
14241
14242 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
14243
14244 while (1)
14245 {
7814882a
JK
14246 /* Initialize it due to a false compiler warning. */
14247 CORE_ADDR range_beginning = 0, range_end = 0;
43988095
JK
14248 const gdb_byte *buf_end = (dwarf2_per_objfile->rnglists.buffer
14249 + dwarf2_per_objfile->rnglists.size);
14250 unsigned int bytes_read;
14251
14252 if (buffer == buf_end)
14253 {
14254 overflow = true;
14255 break;
14256 }
14257 const auto rlet = static_cast<enum dwarf_range_list_entry>(*buffer++);
14258 switch (rlet)
14259 {
14260 case DW_RLE_end_of_list:
14261 break;
14262 case DW_RLE_base_address:
14263 if (buffer + cu->header.addr_size > buf_end)
14264 {
14265 overflow = true;
14266 break;
14267 }
14268 base = read_address (obfd, buffer, cu, &bytes_read);
14269 found_base = 1;
14270 buffer += bytes_read;
14271 break;
14272 case DW_RLE_start_length:
14273 if (buffer + cu->header.addr_size > buf_end)
14274 {
14275 overflow = true;
14276 break;
14277 }
14278 range_beginning = read_address (obfd, buffer, cu, &bytes_read);
14279 buffer += bytes_read;
14280 range_end = (range_beginning
14281 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
14282 buffer += bytes_read;
14283 if (buffer > buf_end)
14284 {
14285 overflow = true;
14286 break;
14287 }
14288 break;
14289 case DW_RLE_offset_pair:
14290 range_beginning = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14291 buffer += bytes_read;
14292 if (buffer > buf_end)
14293 {
14294 overflow = true;
14295 break;
14296 }
14297 range_end = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14298 buffer += bytes_read;
14299 if (buffer > buf_end)
14300 {
14301 overflow = true;
14302 break;
14303 }
14304 break;
14305 case DW_RLE_start_end:
14306 if (buffer + 2 * cu->header.addr_size > buf_end)
14307 {
14308 overflow = true;
14309 break;
14310 }
14311 range_beginning = read_address (obfd, buffer, cu, &bytes_read);
14312 buffer += bytes_read;
14313 range_end = read_address (obfd, buffer, cu, &bytes_read);
14314 buffer += bytes_read;
14315 break;
14316 default:
14317 complaint (&symfile_complaints,
14318 _("Invalid .debug_rnglists data (no base address)"));
14319 return false;
14320 }
14321 if (rlet == DW_RLE_end_of_list || overflow)
14322 break;
14323 if (rlet == DW_RLE_base_address)
14324 continue;
14325
14326 if (!found_base)
14327 {
14328 /* We have no valid base address for the ranges
14329 data. */
14330 complaint (&symfile_complaints,
14331 _("Invalid .debug_rnglists data (no base address)"));
14332 return false;
14333 }
14334
14335 if (range_beginning > range_end)
14336 {
14337 /* Inverted range entries are invalid. */
14338 complaint (&symfile_complaints,
14339 _("Invalid .debug_rnglists data (inverted range)"));
14340 return false;
14341 }
14342
14343 /* Empty range entries have no effect. */
14344 if (range_beginning == range_end)
14345 continue;
14346
14347 range_beginning += base;
14348 range_end += base;
14349
14350 /* A not-uncommon case of bad debug info.
14351 Don't pollute the addrmap with bad data. */
14352 if (range_beginning + baseaddr == 0
14353 && !dwarf2_per_objfile->has_section_at_zero)
14354 {
14355 complaint (&symfile_complaints,
14356 _(".debug_rnglists entry has start address of zero"
14357 " [in module %s]"), objfile_name (objfile));
14358 continue;
14359 }
14360
14361 callback (range_beginning, range_end);
14362 }
14363
14364 if (overflow)
14365 {
14366 complaint (&symfile_complaints,
14367 _("Offset %d is not terminated "
14368 "for DW_AT_ranges attribute"),
14369 offset);
14370 return false;
14371 }
14372
14373 return true;
14374}
14375
14376/* Call CALLBACK from DW_AT_ranges attribute value OFFSET reading .debug_ranges.
14377 Callback's type should be:
14378 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
5f46c5a5 14379 Return 1 if the attributes are present and valid, otherwise, return 0. */
43039443 14380
43988095 14381template <typename Callback>
43039443 14382static int
5f46c5a5 14383dwarf2_ranges_process (unsigned offset, struct dwarf2_cu *cu,
43988095 14384 Callback &&callback)
43039443 14385{
ed2dc618 14386 struct dwarf2_per_objfile *dwarf2_per_objfile
518817b3 14387 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 14388 struct objfile *objfile = dwarf2_per_objfile->objfile;
43039443
JK
14389 struct comp_unit_head *cu_header = &cu->header;
14390 bfd *obfd = objfile->obfd;
14391 unsigned int addr_size = cu_header->addr_size;
14392 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
14393 /* Base address selection entry. */
14394 CORE_ADDR base;
14395 int found_base;
14396 unsigned int dummy;
d521ce57 14397 const gdb_byte *buffer;
ff013f42 14398 CORE_ADDR baseaddr;
43039443 14399
43988095
JK
14400 if (cu_header->version >= 5)
14401 return dwarf2_rnglists_process (offset, cu, callback);
14402
d00adf39
DE
14403 found_base = cu->base_known;
14404 base = cu->base_address;
43039443 14405
be391dca 14406 dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
dce234bc 14407 if (offset >= dwarf2_per_objfile->ranges.size)
43039443
JK
14408 {
14409 complaint (&symfile_complaints,
14410 _("Offset %d out of bounds for DW_AT_ranges attribute"),
14411 offset);
14412 return 0;
14413 }
dce234bc 14414 buffer = dwarf2_per_objfile->ranges.buffer + offset;
43039443 14415
e7030f15 14416 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
ff013f42 14417
43039443
JK
14418 while (1)
14419 {
14420 CORE_ADDR range_beginning, range_end;
14421
14422 range_beginning = read_address (obfd, buffer, cu, &dummy);
14423 buffer += addr_size;
14424 range_end = read_address (obfd, buffer, cu, &dummy);
14425 buffer += addr_size;
14426 offset += 2 * addr_size;
14427
14428 /* An end of list marker is a pair of zero addresses. */
14429 if (range_beginning == 0 && range_end == 0)
14430 /* Found the end of list entry. */
14431 break;
14432
14433 /* Each base address selection entry is a pair of 2 values.
14434 The first is the largest possible address, the second is
14435 the base address. Check for a base address here. */
14436 if ((range_beginning & mask) == mask)
14437 {
28d2bfb9
AB
14438 /* If we found the largest possible address, then we already
14439 have the base address in range_end. */
14440 base = range_end;
43039443
JK
14441 found_base = 1;
14442 continue;
14443 }
14444
14445 if (!found_base)
14446 {
14447 /* We have no valid base address for the ranges
14448 data. */
14449 complaint (&symfile_complaints,
14450 _("Invalid .debug_ranges data (no base address)"));
14451 return 0;
14452 }
14453
9277c30c
UW
14454 if (range_beginning > range_end)
14455 {
14456 /* Inverted range entries are invalid. */
14457 complaint (&symfile_complaints,
14458 _("Invalid .debug_ranges data (inverted range)"));
14459 return 0;
14460 }
14461
14462 /* Empty range entries have no effect. */
14463 if (range_beginning == range_end)
14464 continue;
14465
43039443
JK
14466 range_beginning += base;
14467 range_end += base;
14468
01093045
DE
14469 /* A not-uncommon case of bad debug info.
14470 Don't pollute the addrmap with bad data. */
14471 if (range_beginning + baseaddr == 0
14472 && !dwarf2_per_objfile->has_section_at_zero)
14473 {
14474 complaint (&symfile_complaints,
14475 _(".debug_ranges entry has start address of zero"
4262abfb 14476 " [in module %s]"), objfile_name (objfile));
01093045
DE
14477 continue;
14478 }
14479
5f46c5a5
JK
14480 callback (range_beginning, range_end);
14481 }
14482
14483 return 1;
14484}
14485
14486/* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
14487 Return 1 if the attributes are present and valid, otherwise, return 0.
14488 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
14489
14490static int
14491dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
14492 CORE_ADDR *high_return, struct dwarf2_cu *cu,
14493 struct partial_symtab *ranges_pst)
14494{
518817b3 14495 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
5f46c5a5
JK
14496 struct gdbarch *gdbarch = get_objfile_arch (objfile);
14497 const CORE_ADDR baseaddr = ANOFFSET (objfile->section_offsets,
14498 SECT_OFF_TEXT (objfile));
14499 int low_set = 0;
14500 CORE_ADDR low = 0;
14501 CORE_ADDR high = 0;
14502 int retval;
14503
14504 retval = dwarf2_ranges_process (offset, cu,
14505 [&] (CORE_ADDR range_beginning, CORE_ADDR range_end)
14506 {
9277c30c 14507 if (ranges_pst != NULL)
3e29f34a
MR
14508 {
14509 CORE_ADDR lowpc;
14510 CORE_ADDR highpc;
14511
14512 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch,
14513 range_beginning + baseaddr);
14514 highpc = gdbarch_adjust_dwarf2_addr (gdbarch,
14515 range_end + baseaddr);
14516 addrmap_set_empty (objfile->psymtabs_addrmap, lowpc, highpc - 1,
14517 ranges_pst);
14518 }
ff013f42 14519
43039443
JK
14520 /* FIXME: This is recording everything as a low-high
14521 segment of consecutive addresses. We should have a
14522 data structure for discontiguous block ranges
14523 instead. */
14524 if (! low_set)
14525 {
14526 low = range_beginning;
14527 high = range_end;
14528 low_set = 1;
14529 }
14530 else
14531 {
14532 if (range_beginning < low)
14533 low = range_beginning;
14534 if (range_end > high)
14535 high = range_end;
14536 }
5f46c5a5
JK
14537 });
14538 if (!retval)
14539 return 0;
43039443
JK
14540
14541 if (! low_set)
14542 /* If the first entry is an end-of-list marker, the range
14543 describes an empty scope, i.e. no instructions. */
14544 return 0;
14545
14546 if (low_return)
14547 *low_return = low;
14548 if (high_return)
14549 *high_return = high;
14550 return 1;
14551}
14552
3a2b436a
JK
14553/* Get low and high pc attributes from a die. See enum pc_bounds_kind
14554 definition for the return value. *LOWPC and *HIGHPC are set iff
e385593e 14555 neither PC_BOUNDS_NOT_PRESENT nor PC_BOUNDS_INVALID are returned. */
380bca97 14556
3a2b436a 14557static enum pc_bounds_kind
af34e669 14558dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
d85a05f0
DJ
14559 CORE_ADDR *highpc, struct dwarf2_cu *cu,
14560 struct partial_symtab *pst)
c906108c 14561{
518817b3
SM
14562 struct dwarf2_per_objfile *dwarf2_per_objfile
14563 = cu->per_cu->dwarf2_per_objfile;
c906108c 14564 struct attribute *attr;
91da1414 14565 struct attribute *attr_high;
af34e669
DJ
14566 CORE_ADDR low = 0;
14567 CORE_ADDR high = 0;
e385593e 14568 enum pc_bounds_kind ret;
c906108c 14569
91da1414
MW
14570 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14571 if (attr_high)
af34e669 14572 {
e142c38c 14573 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
af34e669 14574 if (attr)
91da1414 14575 {
31aa7e4e
JB
14576 low = attr_value_as_address (attr);
14577 high = attr_value_as_address (attr_high);
14578 if (cu->header.version >= 4 && attr_form_is_constant (attr_high))
14579 high += low;
91da1414 14580 }
af34e669
DJ
14581 else
14582 /* Found high w/o low attribute. */
e385593e 14583 return PC_BOUNDS_INVALID;
af34e669
DJ
14584
14585 /* Found consecutive range of addresses. */
3a2b436a 14586 ret = PC_BOUNDS_HIGH_LOW;
af34e669 14587 }
c906108c 14588 else
af34e669 14589 {
e142c38c 14590 attr = dwarf2_attr (die, DW_AT_ranges, cu);
af34e669
DJ
14591 if (attr != NULL)
14592 {
ab435259
DE
14593 /* DW_AT_ranges_base does not apply to DIEs from the DWO skeleton.
14594 We take advantage of the fact that DW_AT_ranges does not appear
14595 in DW_TAG_compile_unit of DWO files. */
14596 int need_ranges_base = die->tag != DW_TAG_compile_unit;
14597 unsigned int ranges_offset = (DW_UNSND (attr)
14598 + (need_ranges_base
14599 ? cu->ranges_base
14600 : 0));
2e3cf129 14601
af34e669 14602 /* Value of the DW_AT_ranges attribute is the offset in the
a604369a 14603 .debug_ranges section. */
2e3cf129 14604 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst))
e385593e 14605 return PC_BOUNDS_INVALID;
43039443 14606 /* Found discontinuous range of addresses. */
3a2b436a 14607 ret = PC_BOUNDS_RANGES;
af34e669 14608 }
e385593e
JK
14609 else
14610 return PC_BOUNDS_NOT_PRESENT;
af34e669 14611 }
c906108c 14612
48fbe735 14613 /* partial_die_info::read has also the strict LOW < HIGH requirement. */
9373cf26 14614 if (high <= low)
e385593e 14615 return PC_BOUNDS_INVALID;
c906108c
SS
14616
14617 /* When using the GNU linker, .gnu.linkonce. sections are used to
14618 eliminate duplicate copies of functions and vtables and such.
14619 The linker will arbitrarily choose one and discard the others.
14620 The AT_*_pc values for such functions refer to local labels in
14621 these sections. If the section from that file was discarded, the
14622 labels are not in the output, so the relocs get a value of 0.
14623 If this is a discarded function, mark the pc bounds as invalid,
14624 so that GDB will ignore it. */
72dca2f5 14625 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
e385593e 14626 return PC_BOUNDS_INVALID;
c906108c
SS
14627
14628 *lowpc = low;
96408a79
SA
14629 if (highpc)
14630 *highpc = high;
af34e669 14631 return ret;
c906108c
SS
14632}
14633
b084d499
JB
14634/* Assuming that DIE represents a subprogram DIE or a lexical block, get
14635 its low and high PC addresses. Do nothing if these addresses could not
14636 be determined. Otherwise, set LOWPC to the low address if it is smaller,
14637 and HIGHPC to the high address if greater than HIGHPC. */
14638
14639static void
14640dwarf2_get_subprogram_pc_bounds (struct die_info *die,
14641 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14642 struct dwarf2_cu *cu)
14643{
14644 CORE_ADDR low, high;
14645 struct die_info *child = die->child;
14646
e385593e 14647 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL) >= PC_BOUNDS_RANGES)
b084d499 14648 {
325fac50
PA
14649 *lowpc = std::min (*lowpc, low);
14650 *highpc = std::max (*highpc, high);
b084d499
JB
14651 }
14652
14653 /* If the language does not allow nested subprograms (either inside
14654 subprograms or lexical blocks), we're done. */
14655 if (cu->language != language_ada)
14656 return;
6e70227d 14657
b084d499
JB
14658 /* Check all the children of the given DIE. If it contains nested
14659 subprograms, then check their pc bounds. Likewise, we need to
14660 check lexical blocks as well, as they may also contain subprogram
14661 definitions. */
14662 while (child && child->tag)
14663 {
14664 if (child->tag == DW_TAG_subprogram
14665 || child->tag == DW_TAG_lexical_block)
14666 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
14667 child = sibling_die (child);
14668 }
14669}
14670
fae299cd
DC
14671/* Get the low and high pc's represented by the scope DIE, and store
14672 them in *LOWPC and *HIGHPC. If the correct values can't be
14673 determined, set *LOWPC to -1 and *HIGHPC to 0. */
14674
14675static void
14676get_scope_pc_bounds (struct die_info *die,
14677 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14678 struct dwarf2_cu *cu)
14679{
14680 CORE_ADDR best_low = (CORE_ADDR) -1;
14681 CORE_ADDR best_high = (CORE_ADDR) 0;
14682 CORE_ADDR current_low, current_high;
14683
3a2b436a 14684 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL)
e385593e 14685 >= PC_BOUNDS_RANGES)
fae299cd
DC
14686 {
14687 best_low = current_low;
14688 best_high = current_high;
14689 }
14690 else
14691 {
14692 struct die_info *child = die->child;
14693
14694 while (child && child->tag)
14695 {
14696 switch (child->tag) {
14697 case DW_TAG_subprogram:
b084d499 14698 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
fae299cd
DC
14699 break;
14700 case DW_TAG_namespace:
f55ee35c 14701 case DW_TAG_module:
fae299cd
DC
14702 /* FIXME: carlton/2004-01-16: Should we do this for
14703 DW_TAG_class_type/DW_TAG_structure_type, too? I think
14704 that current GCC's always emit the DIEs corresponding
14705 to definitions of methods of classes as children of a
14706 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
14707 the DIEs giving the declarations, which could be
14708 anywhere). But I don't see any reason why the
14709 standards says that they have to be there. */
14710 get_scope_pc_bounds (child, &current_low, &current_high, cu);
14711
14712 if (current_low != ((CORE_ADDR) -1))
14713 {
325fac50
PA
14714 best_low = std::min (best_low, current_low);
14715 best_high = std::max (best_high, current_high);
fae299cd
DC
14716 }
14717 break;
14718 default:
0963b4bd 14719 /* Ignore. */
fae299cd
DC
14720 break;
14721 }
14722
14723 child = sibling_die (child);
14724 }
14725 }
14726
14727 *lowpc = best_low;
14728 *highpc = best_high;
14729}
14730
801e3a5b
JB
14731/* Record the address ranges for BLOCK, offset by BASEADDR, as given
14732 in DIE. */
380bca97 14733
801e3a5b
JB
14734static void
14735dwarf2_record_block_ranges (struct die_info *die, struct block *block,
14736 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
14737{
518817b3 14738 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
3e29f34a 14739 struct gdbarch *gdbarch = get_objfile_arch (objfile);
801e3a5b 14740 struct attribute *attr;
91da1414 14741 struct attribute *attr_high;
801e3a5b 14742
91da1414
MW
14743 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14744 if (attr_high)
801e3a5b 14745 {
801e3a5b
JB
14746 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
14747 if (attr)
14748 {
31aa7e4e
JB
14749 CORE_ADDR low = attr_value_as_address (attr);
14750 CORE_ADDR high = attr_value_as_address (attr_high);
14751
14752 if (cu->header.version >= 4 && attr_form_is_constant (attr_high))
14753 high += low;
9a619af0 14754
3e29f34a
MR
14755 low = gdbarch_adjust_dwarf2_addr (gdbarch, low + baseaddr);
14756 high = gdbarch_adjust_dwarf2_addr (gdbarch, high + baseaddr);
14757 record_block_range (block, low, high - 1);
801e3a5b
JB
14758 }
14759 }
14760
14761 attr = dwarf2_attr (die, DW_AT_ranges, cu);
14762 if (attr)
14763 {
ab435259
DE
14764 /* DW_AT_ranges_base does not apply to DIEs from the DWO skeleton.
14765 We take advantage of the fact that DW_AT_ranges does not appear
14766 in DW_TAG_compile_unit of DWO files. */
14767 int need_ranges_base = die->tag != DW_TAG_compile_unit;
801e3a5b
JB
14768
14769 /* The value of the DW_AT_ranges attribute is the offset of the
14770 address range list in the .debug_ranges section. */
ab435259
DE
14771 unsigned long offset = (DW_UNSND (attr)
14772 + (need_ranges_base ? cu->ranges_base : 0));
801e3a5b 14773
5f46c5a5
JK
14774 dwarf2_ranges_process (offset, cu,
14775 [&] (CORE_ADDR start, CORE_ADDR end)
14776 {
58fdfd2c
JK
14777 start += baseaddr;
14778 end += baseaddr;
5f46c5a5
JK
14779 start = gdbarch_adjust_dwarf2_addr (gdbarch, start);
14780 end = gdbarch_adjust_dwarf2_addr (gdbarch, end);
14781 record_block_range (block, start, end - 1);
14782 });
801e3a5b
JB
14783 }
14784}
14785
685b1105
JK
14786/* Check whether the producer field indicates either of GCC < 4.6, or the
14787 Intel C/C++ compiler, and cache the result in CU. */
60d5a603 14788
685b1105
JK
14789static void
14790check_producer (struct dwarf2_cu *cu)
60d5a603 14791{
38360086 14792 int major, minor;
60d5a603
JK
14793
14794 if (cu->producer == NULL)
14795 {
14796 /* For unknown compilers expect their behavior is DWARF version
14797 compliant.
14798
14799 GCC started to support .debug_types sections by -gdwarf-4 since
14800 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
14801 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
14802 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
14803 interpreted incorrectly by GDB now - GCC PR debug/48229. */
60d5a603 14804 }
b1ffba5a 14805 else if (producer_is_gcc (cu->producer, &major, &minor))
60d5a603 14806 {
38360086
MW
14807 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
14808 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
685b1105 14809 }
5230b05a
WT
14810 else if (producer_is_icc (cu->producer, &major, &minor))
14811 cu->producer_is_icc_lt_14 = major < 14;
685b1105
JK
14812 else
14813 {
14814 /* For other non-GCC compilers, expect their behavior is DWARF version
14815 compliant. */
60d5a603
JK
14816 }
14817
ba919b58 14818 cu->checked_producer = 1;
685b1105 14819}
ba919b58 14820
685b1105
JK
14821/* Check for GCC PR debug/45124 fix which is not present in any G++ version up
14822 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
14823 during 4.6.0 experimental. */
14824
14825static int
14826producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
14827{
14828 if (!cu->checked_producer)
14829 check_producer (cu);
14830
14831 return cu->producer_is_gxx_lt_4_6;
60d5a603
JK
14832}
14833
14834/* Return the default accessibility type if it is not overriden by
14835 DW_AT_accessibility. */
14836
14837static enum dwarf_access_attribute
14838dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
14839{
14840 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
14841 {
14842 /* The default DWARF 2 accessibility for members is public, the default
14843 accessibility for inheritance is private. */
14844
14845 if (die->tag != DW_TAG_inheritance)
14846 return DW_ACCESS_public;
14847 else
14848 return DW_ACCESS_private;
14849 }
14850 else
14851 {
14852 /* DWARF 3+ defines the default accessibility a different way. The same
14853 rules apply now for DW_TAG_inheritance as for the members and it only
14854 depends on the container kind. */
14855
14856 if (die->parent->tag == DW_TAG_class_type)
14857 return DW_ACCESS_private;
14858 else
14859 return DW_ACCESS_public;
14860 }
14861}
14862
74ac6d43
TT
14863/* Look for DW_AT_data_member_location. Set *OFFSET to the byte
14864 offset. If the attribute was not found return 0, otherwise return
14865 1. If it was found but could not properly be handled, set *OFFSET
14866 to 0. */
14867
14868static int
14869handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
14870 LONGEST *offset)
14871{
14872 struct attribute *attr;
14873
14874 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
14875 if (attr != NULL)
14876 {
14877 *offset = 0;
14878
14879 /* Note that we do not check for a section offset first here.
14880 This is because DW_AT_data_member_location is new in DWARF 4,
14881 so if we see it, we can assume that a constant form is really
14882 a constant and not a section offset. */
14883 if (attr_form_is_constant (attr))
14884 *offset = dwarf2_get_attr_constant_value (attr, 0);
14885 else if (attr_form_is_section_offset (attr))
14886 dwarf2_complex_location_expr_complaint ();
14887 else if (attr_form_is_block (attr))
14888 *offset = decode_locdesc (DW_BLOCK (attr), cu);
14889 else
14890 dwarf2_complex_location_expr_complaint ();
14891
14892 return 1;
14893 }
14894
14895 return 0;
14896}
14897
c906108c
SS
14898/* Add an aggregate field to the field list. */
14899
14900static void
107d2387 14901dwarf2_add_field (struct field_info *fip, struct die_info *die,
e7c27a73 14902 struct dwarf2_cu *cu)
6e70227d 14903{
518817b3 14904 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
5e2b427d 14905 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
14906 struct nextfield *new_field;
14907 struct attribute *attr;
14908 struct field *fp;
15d034d0 14909 const char *fieldname = "";
c906108c 14910
7d0ccb61
DJ
14911 if (die->tag == DW_TAG_inheritance)
14912 {
be2daae6
TT
14913 fip->baseclasses.emplace_back ();
14914 new_field = &fip->baseclasses.back ();
7d0ccb61
DJ
14915 }
14916 else
14917 {
be2daae6
TT
14918 fip->fields.emplace_back ();
14919 new_field = &fip->fields.back ();
7d0ccb61 14920 }
be2daae6 14921
c906108c
SS
14922 fip->nfields++;
14923
e142c38c 14924 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
c906108c
SS
14925 if (attr)
14926 new_field->accessibility = DW_UNSND (attr);
60d5a603
JK
14927 else
14928 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
c906108c
SS
14929 if (new_field->accessibility != DW_ACCESS_public)
14930 fip->non_public_fields = 1;
60d5a603 14931
e142c38c 14932 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
c906108c
SS
14933 if (attr)
14934 new_field->virtuality = DW_UNSND (attr);
60d5a603
JK
14935 else
14936 new_field->virtuality = DW_VIRTUALITY_none;
c906108c
SS
14937
14938 fp = &new_field->field;
a9a9bd0f 14939
e142c38c 14940 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
c906108c 14941 {
74ac6d43
TT
14942 LONGEST offset;
14943
a9a9bd0f 14944 /* Data member other than a C++ static data member. */
6e70227d 14945
c906108c 14946 /* Get type of field. */
e7c27a73 14947 fp->type = die_type (die, cu);
c906108c 14948
d6a843b5 14949 SET_FIELD_BITPOS (*fp, 0);
01ad7f36 14950
c906108c 14951 /* Get bit size of field (zero if none). */
e142c38c 14952 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
c906108c
SS
14953 if (attr)
14954 {
14955 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
14956 }
14957 else
14958 {
14959 FIELD_BITSIZE (*fp) = 0;
14960 }
14961
14962 /* Get bit offset of field. */
74ac6d43
TT
14963 if (handle_data_member_location (die, cu, &offset))
14964 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
e142c38c 14965 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
c906108c
SS
14966 if (attr)
14967 {
5e2b427d 14968 if (gdbarch_bits_big_endian (gdbarch))
c906108c
SS
14969 {
14970 /* For big endian bits, the DW_AT_bit_offset gives the
c5aa993b
JM
14971 additional bit offset from the MSB of the containing
14972 anonymous object to the MSB of the field. We don't
14973 have to do anything special since we don't need to
14974 know the size of the anonymous object. */
f41f5e61 14975 SET_FIELD_BITPOS (*fp, FIELD_BITPOS (*fp) + DW_UNSND (attr));
c906108c
SS
14976 }
14977 else
14978 {
14979 /* For little endian bits, compute the bit offset to the
c5aa993b
JM
14980 MSB of the anonymous object, subtract off the number of
14981 bits from the MSB of the field to the MSB of the
14982 object, and then subtract off the number of bits of
14983 the field itself. The result is the bit offset of
14984 the LSB of the field. */
c906108c
SS
14985 int anonymous_size;
14986 int bit_offset = DW_UNSND (attr);
14987
e142c38c 14988 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
14989 if (attr)
14990 {
14991 /* The size of the anonymous object containing
14992 the bit field is explicit, so use the
14993 indicated size (in bytes). */
14994 anonymous_size = DW_UNSND (attr);
14995 }
14996 else
14997 {
14998 /* The size of the anonymous object containing
14999 the bit field must be inferred from the type
15000 attribute of the data member containing the
15001 bit field. */
15002 anonymous_size = TYPE_LENGTH (fp->type);
15003 }
f41f5e61
PA
15004 SET_FIELD_BITPOS (*fp,
15005 (FIELD_BITPOS (*fp)
15006 + anonymous_size * bits_per_byte
15007 - bit_offset - FIELD_BITSIZE (*fp)));
c906108c
SS
15008 }
15009 }
da5b30da
AA
15010 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
15011 if (attr != NULL)
15012 SET_FIELD_BITPOS (*fp, (FIELD_BITPOS (*fp)
15013 + dwarf2_get_attr_constant_value (attr, 0)));
c906108c
SS
15014
15015 /* Get name of field. */
39cbfefa
DJ
15016 fieldname = dwarf2_name (die, cu);
15017 if (fieldname == NULL)
15018 fieldname = "";
d8151005
DJ
15019
15020 /* The name is already allocated along with this objfile, so we don't
15021 need to duplicate it for the type. */
15022 fp->name = fieldname;
c906108c
SS
15023
15024 /* Change accessibility for artificial fields (e.g. virtual table
c5aa993b 15025 pointer or virtual base class pointer) to private. */
e142c38c 15026 if (dwarf2_attr (die, DW_AT_artificial, cu))
c906108c 15027 {
d48cc9dd 15028 FIELD_ARTIFICIAL (*fp) = 1;
c906108c
SS
15029 new_field->accessibility = DW_ACCESS_private;
15030 fip->non_public_fields = 1;
15031 }
15032 }
a9a9bd0f 15033 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
c906108c 15034 {
a9a9bd0f
DC
15035 /* C++ static member. */
15036
15037 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
15038 is a declaration, but all versions of G++ as of this writing
15039 (so through at least 3.2.1) incorrectly generate
15040 DW_TAG_variable tags. */
6e70227d 15041
ff355380 15042 const char *physname;
c906108c 15043
a9a9bd0f 15044 /* Get name of field. */
39cbfefa
DJ
15045 fieldname = dwarf2_name (die, cu);
15046 if (fieldname == NULL)
c906108c
SS
15047 return;
15048
254e6b9e 15049 attr = dwarf2_attr (die, DW_AT_const_value, cu);
3863f96c
DE
15050 if (attr
15051 /* Only create a symbol if this is an external value.
15052 new_symbol checks this and puts the value in the global symbol
15053 table, which we want. If it is not external, new_symbol
15054 will try to put the value in cu->list_in_scope which is wrong. */
15055 && dwarf2_flag_true_p (die, DW_AT_external, cu))
254e6b9e
DE
15056 {
15057 /* A static const member, not much different than an enum as far as
15058 we're concerned, except that we can support more types. */
15059 new_symbol (die, NULL, cu);
15060 }
15061
2df3850c 15062 /* Get physical name. */
ff355380 15063 physname = dwarf2_physname (fieldname, die, cu);
c906108c 15064
d8151005
DJ
15065 /* The name is already allocated along with this objfile, so we don't
15066 need to duplicate it for the type. */
15067 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
e7c27a73 15068 FIELD_TYPE (*fp) = die_type (die, cu);
d8151005 15069 FIELD_NAME (*fp) = fieldname;
c906108c
SS
15070 }
15071 else if (die->tag == DW_TAG_inheritance)
15072 {
74ac6d43 15073 LONGEST offset;
d4b96c9a 15074
74ac6d43
TT
15075 /* C++ base class field. */
15076 if (handle_data_member_location (die, cu, &offset))
15077 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
c906108c 15078 FIELD_BITSIZE (*fp) = 0;
e7c27a73 15079 FIELD_TYPE (*fp) = die_type (die, cu);
c906108c 15080 FIELD_NAME (*fp) = type_name_no_tag (fp->type);
c906108c 15081 }
2ddeaf8a
TT
15082 else if (die->tag == DW_TAG_variant_part)
15083 {
15084 /* process_structure_scope will treat this DIE as a union. */
15085 process_structure_scope (die, cu);
15086
15087 /* The variant part is relative to the start of the enclosing
15088 structure. */
15089 SET_FIELD_BITPOS (*fp, 0);
15090 fp->type = get_die_type (die, cu);
15091 fp->artificial = 1;
15092 fp->name = "<<variant>>";
15093 }
15094 else
15095 gdb_assert_not_reached ("missing case in dwarf2_add_field");
c906108c
SS
15096}
15097
883fd55a
KS
15098/* Can the type given by DIE define another type? */
15099
15100static bool
15101type_can_define_types (const struct die_info *die)
15102{
15103 switch (die->tag)
15104 {
15105 case DW_TAG_typedef:
15106 case DW_TAG_class_type:
15107 case DW_TAG_structure_type:
15108 case DW_TAG_union_type:
15109 case DW_TAG_enumeration_type:
15110 return true;
15111
15112 default:
15113 return false;
15114 }
15115}
15116
15117/* Add a type definition defined in the scope of the FIP's class. */
98751a41
JK
15118
15119static void
883fd55a
KS
15120dwarf2_add_type_defn (struct field_info *fip, struct die_info *die,
15121 struct dwarf2_cu *cu)
6e70227d 15122{
be2daae6
TT
15123 struct decl_field fp;
15124 memset (&fp, 0, sizeof (fp));
98751a41 15125
883fd55a 15126 gdb_assert (type_can_define_types (die));
98751a41 15127
883fd55a 15128 /* Get name of field. NULL is okay here, meaning an anonymous type. */
be2daae6
TT
15129 fp.name = dwarf2_name (die, cu);
15130 fp.type = read_type_die (die, cu);
98751a41 15131
c191a687
KS
15132 /* Save accessibility. */
15133 enum dwarf_access_attribute accessibility;
15134 struct attribute *attr = dwarf2_attr (die, DW_AT_accessibility, cu);
15135 if (attr != NULL)
15136 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
15137 else
15138 accessibility = dwarf2_default_access_attribute (die, cu);
15139 switch (accessibility)
15140 {
15141 case DW_ACCESS_public:
15142 /* The assumed value if neither private nor protected. */
15143 break;
15144 case DW_ACCESS_private:
be2daae6 15145 fp.is_private = 1;
c191a687
KS
15146 break;
15147 case DW_ACCESS_protected:
be2daae6 15148 fp.is_protected = 1;
c191a687
KS
15149 break;
15150 default:
37534686
KS
15151 complaint (&symfile_complaints,
15152 _("Unhandled DW_AT_accessibility value (%x)"), accessibility);
c191a687
KS
15153 }
15154
883fd55a 15155 if (die->tag == DW_TAG_typedef)
be2daae6 15156 fip->typedef_field_list.push_back (fp);
883fd55a 15157 else
be2daae6 15158 fip->nested_types_list.push_back (fp);
98751a41
JK
15159}
15160
c906108c
SS
15161/* Create the vector of fields, and attach it to the type. */
15162
15163static void
fba45db2 15164dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 15165 struct dwarf2_cu *cu)
c906108c
SS
15166{
15167 int nfields = fip->nfields;
15168
15169 /* Record the field count, allocate space for the array of fields,
15170 and create blank accessibility bitfields if necessary. */
15171 TYPE_NFIELDS (type) = nfields;
15172 TYPE_FIELDS (type) = (struct field *)
be2daae6 15173 TYPE_ZALLOC (type, sizeof (struct field) * nfields);
c906108c 15174
b4ba55a1 15175 if (fip->non_public_fields && cu->language != language_ada)
c906108c
SS
15176 {
15177 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15178
15179 TYPE_FIELD_PRIVATE_BITS (type) =
15180 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15181 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
15182
15183 TYPE_FIELD_PROTECTED_BITS (type) =
15184 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15185 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
15186
774b6a14
TT
15187 TYPE_FIELD_IGNORE_BITS (type) =
15188 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15189 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
c906108c
SS
15190 }
15191
15192 /* If the type has baseclasses, allocate and clear a bit vector for
15193 TYPE_FIELD_VIRTUAL_BITS. */
be2daae6 15194 if (!fip->baseclasses.empty () && cu->language != language_ada)
c906108c 15195 {
be2daae6 15196 int num_bytes = B_BYTES (fip->baseclasses.size ());
fe1b8b76 15197 unsigned char *pointer;
c906108c
SS
15198
15199 ALLOCATE_CPLUS_STRUCT_TYPE (type);
224c3ddb 15200 pointer = (unsigned char *) TYPE_ALLOC (type, num_bytes);
fe1b8b76 15201 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
be2daae6
TT
15202 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->baseclasses.size ());
15203 TYPE_N_BASECLASSES (type) = fip->baseclasses.size ();
c906108c
SS
15204 }
15205
2ddeaf8a
TT
15206 if (TYPE_FLAG_DISCRIMINATED_UNION (type))
15207 {
15208 struct discriminant_info *di = alloc_discriminant_info (type, -1, -1);
15209
be2daae6 15210 for (int index = 0; index < nfields; ++index)
2ddeaf8a 15211 {
be2daae6
TT
15212 struct nextfield &field = fip->fields[index];
15213
15214 if (field.variant.is_discriminant)
2ddeaf8a 15215 di->discriminant_index = index;
be2daae6 15216 else if (field.variant.default_branch)
2ddeaf8a
TT
15217 di->default_index = index;
15218 else
be2daae6 15219 di->discriminants[index] = field.variant.discriminant_value;
2ddeaf8a
TT
15220 }
15221 }
15222
be2daae6
TT
15223 /* Copy the saved-up fields into the field vector. */
15224 for (int i = 0; i < nfields; ++i)
c906108c 15225 {
be2daae6
TT
15226 struct nextfield &field
15227 = ((i < fip->baseclasses.size ()) ? fip->baseclasses[i]
15228 : fip->fields[i - fip->baseclasses.size ()]);
7d0ccb61 15229
be2daae6
TT
15230 TYPE_FIELD (type, i) = field.field;
15231 switch (field.accessibility)
c906108c 15232 {
c5aa993b 15233 case DW_ACCESS_private:
b4ba55a1 15234 if (cu->language != language_ada)
be2daae6 15235 SET_TYPE_FIELD_PRIVATE (type, i);
c5aa993b 15236 break;
c906108c 15237
c5aa993b 15238 case DW_ACCESS_protected:
b4ba55a1 15239 if (cu->language != language_ada)
be2daae6 15240 SET_TYPE_FIELD_PROTECTED (type, i);
c5aa993b 15241 break;
c906108c 15242
c5aa993b
JM
15243 case DW_ACCESS_public:
15244 break;
c906108c 15245
c5aa993b
JM
15246 default:
15247 /* Unknown accessibility. Complain and treat it as public. */
15248 {
e2e0b3e5 15249 complaint (&symfile_complaints, _("unsupported accessibility %d"),
be2daae6 15250 field.accessibility);
c5aa993b
JM
15251 }
15252 break;
c906108c 15253 }
be2daae6 15254 if (i < fip->baseclasses.size ())
c906108c 15255 {
be2daae6 15256 switch (field.virtuality)
c906108c 15257 {
c5aa993b
JM
15258 case DW_VIRTUALITY_virtual:
15259 case DW_VIRTUALITY_pure_virtual:
b4ba55a1 15260 if (cu->language == language_ada)
a73c6dcd 15261 error (_("unexpected virtuality in component of Ada type"));
be2daae6 15262 SET_TYPE_FIELD_VIRTUAL (type, i);
c5aa993b 15263 break;
c906108c
SS
15264 }
15265 }
c906108c
SS
15266 }
15267}
15268
7d27a96d
TT
15269/* Return true if this member function is a constructor, false
15270 otherwise. */
15271
15272static int
15273dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
15274{
15275 const char *fieldname;
fe978cb0 15276 const char *type_name;
7d27a96d
TT
15277 int len;
15278
15279 if (die->parent == NULL)
15280 return 0;
15281
15282 if (die->parent->tag != DW_TAG_structure_type
15283 && die->parent->tag != DW_TAG_union_type
15284 && die->parent->tag != DW_TAG_class_type)
15285 return 0;
15286
15287 fieldname = dwarf2_name (die, cu);
fe978cb0
PA
15288 type_name = dwarf2_name (die->parent, cu);
15289 if (fieldname == NULL || type_name == NULL)
7d27a96d
TT
15290 return 0;
15291
15292 len = strlen (fieldname);
fe978cb0
PA
15293 return (strncmp (fieldname, type_name, len) == 0
15294 && (type_name[len] == '\0' || type_name[len] == '<'));
7d27a96d
TT
15295}
15296
c906108c
SS
15297/* Add a member function to the proper fieldlist. */
15298
15299static void
107d2387 15300dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
e7c27a73 15301 struct type *type, struct dwarf2_cu *cu)
c906108c 15302{
518817b3 15303 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c 15304 struct attribute *attr;
c906108c 15305 int i;
be2daae6 15306 struct fnfieldlist *flp = nullptr;
c906108c 15307 struct fn_field *fnp;
15d034d0 15308 const char *fieldname;
f792889a 15309 struct type *this_type;
60d5a603 15310 enum dwarf_access_attribute accessibility;
c906108c 15311
b4ba55a1 15312 if (cu->language == language_ada)
a73c6dcd 15313 error (_("unexpected member function in Ada type"));
b4ba55a1 15314
2df3850c 15315 /* Get name of member function. */
39cbfefa
DJ
15316 fieldname = dwarf2_name (die, cu);
15317 if (fieldname == NULL)
2df3850c 15318 return;
c906108c 15319
c906108c 15320 /* Look up member function name in fieldlist. */
be2daae6 15321 for (i = 0; i < fip->fnfieldlists.size (); i++)
c906108c 15322 {
27bfe10e 15323 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
be2daae6
TT
15324 {
15325 flp = &fip->fnfieldlists[i];
15326 break;
15327 }
c906108c
SS
15328 }
15329
be2daae6
TT
15330 /* Create a new fnfieldlist if necessary. */
15331 if (flp == nullptr)
c906108c 15332 {
be2daae6
TT
15333 fip->fnfieldlists.emplace_back ();
15334 flp = &fip->fnfieldlists.back ();
c906108c 15335 flp->name = fieldname;
be2daae6 15336 i = fip->fnfieldlists.size () - 1;
c906108c
SS
15337 }
15338
be2daae6
TT
15339 /* Create a new member function field and add it to the vector of
15340 fnfieldlists. */
15341 flp->fnfields.emplace_back ();
15342 fnp = &flp->fnfields.back ();
3da10d80
KS
15343
15344 /* Delay processing of the physname until later. */
9c37b5ae 15345 if (cu->language == language_cplus)
be2daae6
TT
15346 add_to_method_list (type, i, flp->fnfields.size () - 1, fieldname,
15347 die, cu);
3da10d80
KS
15348 else
15349 {
1d06ead6 15350 const char *physname = dwarf2_physname (fieldname, die, cu);
3da10d80
KS
15351 fnp->physname = physname ? physname : "";
15352 }
15353
c906108c 15354 fnp->type = alloc_type (objfile);
f792889a
DJ
15355 this_type = read_type_die (die, cu);
15356 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
c906108c 15357 {
f792889a 15358 int nparams = TYPE_NFIELDS (this_type);
c906108c 15359
f792889a 15360 /* TYPE is the domain of this method, and THIS_TYPE is the type
e26fb1d7
DC
15361 of the method itself (TYPE_CODE_METHOD). */
15362 smash_to_method_type (fnp->type, type,
f792889a
DJ
15363 TYPE_TARGET_TYPE (this_type),
15364 TYPE_FIELDS (this_type),
15365 TYPE_NFIELDS (this_type),
15366 TYPE_VARARGS (this_type));
c906108c
SS
15367
15368 /* Handle static member functions.
c5aa993b 15369 Dwarf2 has no clean way to discern C++ static and non-static
0963b4bd
MS
15370 member functions. G++ helps GDB by marking the first
15371 parameter for non-static member functions (which is the this
15372 pointer) as artificial. We obtain this information from
15373 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
f792889a 15374 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
c906108c
SS
15375 fnp->voffset = VOFFSET_STATIC;
15376 }
15377 else
e2e0b3e5 15378 complaint (&symfile_complaints, _("member function type missing for '%s'"),
3da10d80 15379 dwarf2_full_name (fieldname, die, cu));
c906108c
SS
15380
15381 /* Get fcontext from DW_AT_containing_type if present. */
e142c38c 15382 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
e7c27a73 15383 fnp->fcontext = die_containing_type (die, cu);
c906108c 15384
3e43a32a
MS
15385 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
15386 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
c906108c
SS
15387
15388 /* Get accessibility. */
e142c38c 15389 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
c906108c 15390 if (attr)
aead7601 15391 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
60d5a603
JK
15392 else
15393 accessibility = dwarf2_default_access_attribute (die, cu);
15394 switch (accessibility)
c906108c 15395 {
60d5a603
JK
15396 case DW_ACCESS_private:
15397 fnp->is_private = 1;
15398 break;
15399 case DW_ACCESS_protected:
15400 fnp->is_protected = 1;
15401 break;
c906108c
SS
15402 }
15403
b02dede2 15404 /* Check for artificial methods. */
e142c38c 15405 attr = dwarf2_attr (die, DW_AT_artificial, cu);
b02dede2
DJ
15406 if (attr && DW_UNSND (attr) != 0)
15407 fnp->is_artificial = 1;
15408
7d27a96d
TT
15409 fnp->is_constructor = dwarf2_is_constructor (die, cu);
15410
0d564a31 15411 /* Get index in virtual function table if it is a virtual member
aec5aa8b
TT
15412 function. For older versions of GCC, this is an offset in the
15413 appropriate virtual table, as specified by DW_AT_containing_type.
15414 For everyone else, it is an expression to be evaluated relative
0d564a31
DJ
15415 to the object address. */
15416
e142c38c 15417 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
aec5aa8b 15418 if (attr)
8e19ed76 15419 {
aec5aa8b 15420 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size > 0)
8e19ed76 15421 {
aec5aa8b
TT
15422 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
15423 {
15424 /* Old-style GCC. */
15425 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
15426 }
15427 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
15428 || (DW_BLOCK (attr)->size > 1
15429 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
15430 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
15431 {
aec5aa8b
TT
15432 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
15433 if ((fnp->voffset % cu->header.addr_size) != 0)
15434 dwarf2_complex_location_expr_complaint ();
15435 else
15436 fnp->voffset /= cu->header.addr_size;
15437 fnp->voffset += 2;
15438 }
15439 else
15440 dwarf2_complex_location_expr_complaint ();
15441
15442 if (!fnp->fcontext)
7e993ebf
KS
15443 {
15444 /* If there is no `this' field and no DW_AT_containing_type,
15445 we cannot actually find a base class context for the
15446 vtable! */
15447 if (TYPE_NFIELDS (this_type) == 0
15448 || !TYPE_FIELD_ARTIFICIAL (this_type, 0))
15449 {
15450 complaint (&symfile_complaints,
15451 _("cannot determine context for virtual member "
9d8780f0
SM
15452 "function \"%s\" (offset %s)"),
15453 fieldname, sect_offset_str (die->sect_off));
7e993ebf
KS
15454 }
15455 else
15456 {
15457 fnp->fcontext
15458 = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
15459 }
15460 }
aec5aa8b 15461 }
3690dd37 15462 else if (attr_form_is_section_offset (attr))
8e19ed76 15463 {
4d3c2250 15464 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
15465 }
15466 else
15467 {
4d3c2250
KB
15468 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
15469 fieldname);
8e19ed76 15470 }
0d564a31 15471 }
d48cc9dd
DJ
15472 else
15473 {
15474 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
15475 if (attr && DW_UNSND (attr))
15476 {
15477 /* GCC does this, as of 2008-08-25; PR debug/37237. */
15478 complaint (&symfile_complaints,
9d8780f0 15479 _("Member function \"%s\" (offset %s) is virtual "
3e43a32a 15480 "but the vtable offset is not specified"),
9d8780f0 15481 fieldname, sect_offset_str (die->sect_off));
9655fd1a 15482 ALLOCATE_CPLUS_STRUCT_TYPE (type);
d48cc9dd
DJ
15483 TYPE_CPLUS_DYNAMIC (type) = 1;
15484 }
15485 }
c906108c
SS
15486}
15487
15488/* Create the vector of member function fields, and attach it to the type. */
15489
15490static void
fba45db2 15491dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 15492 struct dwarf2_cu *cu)
c906108c 15493{
b4ba55a1 15494 if (cu->language == language_ada)
a73c6dcd 15495 error (_("unexpected member functions in Ada type"));
b4ba55a1 15496
c906108c
SS
15497 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15498 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
be2daae6
TT
15499 TYPE_ALLOC (type,
15500 sizeof (struct fn_fieldlist) * fip->fnfieldlists.size ());
c906108c 15501
be2daae6 15502 for (int i = 0; i < fip->fnfieldlists.size (); i++)
c906108c 15503 {
be2daae6 15504 struct fnfieldlist &nf = fip->fnfieldlists[i];
c906108c 15505 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
c906108c 15506
be2daae6
TT
15507 TYPE_FN_FIELDLIST_NAME (type, i) = nf.name;
15508 TYPE_FN_FIELDLIST_LENGTH (type, i) = nf.fnfields.size ();
c906108c 15509 fn_flp->fn_fields = (struct fn_field *)
be2daae6
TT
15510 TYPE_ALLOC (type, sizeof (struct fn_field) * nf.fnfields.size ());
15511
15512 for (int k = 0; k < nf.fnfields.size (); ++k)
15513 fn_flp->fn_fields[k] = nf.fnfields[k];
c906108c
SS
15514 }
15515
be2daae6 15516 TYPE_NFN_FIELDS (type) = fip->fnfieldlists.size ();
c906108c
SS
15517}
15518
1168df01
JB
15519/* Returns non-zero if NAME is the name of a vtable member in CU's
15520 language, zero otherwise. */
15521static int
15522is_vtable_name (const char *name, struct dwarf2_cu *cu)
15523{
15524 static const char vptr[] = "_vptr";
15525
9c37b5ae
TT
15526 /* Look for the C++ form of the vtable. */
15527 if (startswith (name, vptr) && is_cplus_marker (name[sizeof (vptr) - 1]))
1168df01
JB
15528 return 1;
15529
15530 return 0;
15531}
15532
c0dd20ea 15533/* GCC outputs unnamed structures that are really pointers to member
0b92b5bb
TT
15534 functions, with the ABI-specified layout. If TYPE describes
15535 such a structure, smash it into a member function type.
61049d3b
DJ
15536
15537 GCC shouldn't do this; it should just output pointer to member DIEs.
15538 This is GCC PR debug/28767. */
c0dd20ea 15539
0b92b5bb
TT
15540static void
15541quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
c0dd20ea 15542{
09e2d7c7 15543 struct type *pfn_type, *self_type, *new_type;
c0dd20ea
DJ
15544
15545 /* Check for a structure with no name and two children. */
0b92b5bb
TT
15546 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
15547 return;
c0dd20ea
DJ
15548
15549 /* Check for __pfn and __delta members. */
0b92b5bb
TT
15550 if (TYPE_FIELD_NAME (type, 0) == NULL
15551 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
15552 || TYPE_FIELD_NAME (type, 1) == NULL
15553 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
15554 return;
c0dd20ea
DJ
15555
15556 /* Find the type of the method. */
0b92b5bb 15557 pfn_type = TYPE_FIELD_TYPE (type, 0);
c0dd20ea
DJ
15558 if (pfn_type == NULL
15559 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
15560 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
0b92b5bb 15561 return;
c0dd20ea
DJ
15562
15563 /* Look for the "this" argument. */
15564 pfn_type = TYPE_TARGET_TYPE (pfn_type);
15565 if (TYPE_NFIELDS (pfn_type) == 0
0b92b5bb 15566 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
c0dd20ea 15567 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
0b92b5bb 15568 return;
c0dd20ea 15569
09e2d7c7 15570 self_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
0b92b5bb 15571 new_type = alloc_type (objfile);
09e2d7c7 15572 smash_to_method_type (new_type, self_type, TYPE_TARGET_TYPE (pfn_type),
c0dd20ea
DJ
15573 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
15574 TYPE_VARARGS (pfn_type));
0b92b5bb 15575 smash_to_methodptr_type (type, new_type);
c0dd20ea 15576}
1168df01 15577
685b1105 15578
c906108c 15579/* Called when we find the DIE that starts a structure or union scope
c767944b
DJ
15580 (definition) to create a type for the structure or union. Fill in
15581 the type's name and general properties; the members will not be
83655187
DE
15582 processed until process_structure_scope. A symbol table entry for
15583 the type will also not be done until process_structure_scope (assuming
15584 the type has a name).
c906108c 15585
c767944b
DJ
15586 NOTE: we need to call these functions regardless of whether or not the
15587 DIE has a DW_AT_name attribute, since it might be an anonymous
c906108c 15588 structure or union. This gets the type entered into our set of
83655187 15589 user defined types. */
c906108c 15590
f792889a 15591static struct type *
134d01f1 15592read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 15593{
518817b3 15594 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c
SS
15595 struct type *type;
15596 struct attribute *attr;
15d034d0 15597 const char *name;
c906108c 15598
348e048f
DE
15599 /* If the definition of this type lives in .debug_types, read that type.
15600 Don't follow DW_AT_specification though, that will take us back up
15601 the chain and we want to go down. */
45e58e77 15602 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
348e048f
DE
15603 if (attr)
15604 {
ac9ec31b 15605 type = get_DW_AT_signature_type (die, attr, cu);
9dc481d3 15606
ac9ec31b 15607 /* The type's CU may not be the same as CU.
02142a6c 15608 Ensure TYPE is recorded with CU in die_type_hash. */
348e048f
DE
15609 return set_die_type (die, type, cu);
15610 }
15611
c0dd20ea 15612 type = alloc_type (objfile);
c906108c 15613 INIT_CPLUS_SPECIFIC (type);
93311388 15614
39cbfefa
DJ
15615 name = dwarf2_name (die, cu);
15616 if (name != NULL)
c906108c 15617 {
987504bb 15618 if (cu->language == language_cplus
c44af4eb
TT
15619 || cu->language == language_d
15620 || cu->language == language_rust)
63d06c5c 15621 {
15d034d0 15622 const char *full_name = dwarf2_full_name (name, die, cu);
3da10d80
KS
15623
15624 /* dwarf2_full_name might have already finished building the DIE's
15625 type. If so, there is no need to continue. */
15626 if (get_die_type (die, cu) != NULL)
15627 return get_die_type (die, cu);
15628
15629 TYPE_TAG_NAME (type) = full_name;
94af9270
KS
15630 if (die->tag == DW_TAG_structure_type
15631 || die->tag == DW_TAG_class_type)
15632 TYPE_NAME (type) = TYPE_TAG_NAME (type);
63d06c5c
DC
15633 }
15634 else
15635 {
d8151005
DJ
15636 /* The name is already allocated along with this objfile, so
15637 we don't need to duplicate it for the type. */
7d455152 15638 TYPE_TAG_NAME (type) = name;
94af9270
KS
15639 if (die->tag == DW_TAG_class_type)
15640 TYPE_NAME (type) = TYPE_TAG_NAME (type);
63d06c5c 15641 }
c906108c
SS
15642 }
15643
15644 if (die->tag == DW_TAG_structure_type)
15645 {
15646 TYPE_CODE (type) = TYPE_CODE_STRUCT;
15647 }
15648 else if (die->tag == DW_TAG_union_type)
15649 {
15650 TYPE_CODE (type) = TYPE_CODE_UNION;
15651 }
2ddeaf8a
TT
15652 else if (die->tag == DW_TAG_variant_part)
15653 {
15654 TYPE_CODE (type) = TYPE_CODE_UNION;
15655 TYPE_FLAG_DISCRIMINATED_UNION (type) = 1;
15656 }
c906108c
SS
15657 else
15658 {
4753d33b 15659 TYPE_CODE (type) = TYPE_CODE_STRUCT;
c906108c
SS
15660 }
15661
0cc2414c
TT
15662 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
15663 TYPE_DECLARED_CLASS (type) = 1;
15664
e142c38c 15665 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
15666 if (attr)
15667 {
155bfbd3
JB
15668 if (attr_form_is_constant (attr))
15669 TYPE_LENGTH (type) = DW_UNSND (attr);
15670 else
15671 {
15672 /* For the moment, dynamic type sizes are not supported
15673 by GDB's struct type. The actual size is determined
15674 on-demand when resolving the type of a given object,
15675 so set the type's length to zero for now. Otherwise,
15676 we record an expression as the length, and that expression
15677 could lead to a very large value, which could eventually
15678 lead to us trying to allocate that much memory when creating
15679 a value of that type. */
15680 TYPE_LENGTH (type) = 0;
15681 }
c906108c
SS
15682 }
15683 else
15684 {
15685 TYPE_LENGTH (type) = 0;
15686 }
15687
5230b05a 15688 if (producer_is_icc_lt_14 (cu) && (TYPE_LENGTH (type) == 0))
685b1105 15689 {
5230b05a
WT
15690 /* ICC<14 does not output the required DW_AT_declaration on
15691 incomplete types, but gives them a size of zero. */
422b1cb0 15692 TYPE_STUB (type) = 1;
685b1105
JK
15693 }
15694 else
15695 TYPE_STUB_SUPPORTED (type) = 1;
15696
dc718098 15697 if (die_is_declaration (die, cu))
876cecd0 15698 TYPE_STUB (type) = 1;
a6c727b2
DJ
15699 else if (attr == NULL && die->child == NULL
15700 && producer_is_realview (cu->producer))
15701 /* RealView does not output the required DW_AT_declaration
15702 on incomplete types. */
15703 TYPE_STUB (type) = 1;
dc718098 15704
c906108c
SS
15705 /* We need to add the type field to the die immediately so we don't
15706 infinitely recurse when dealing with pointers to the structure
0963b4bd 15707 type within the structure itself. */
1c379e20 15708 set_die_type (die, type, cu);
c906108c 15709
7e314c57
JK
15710 /* set_die_type should be already done. */
15711 set_descriptive_type (type, die, cu);
15712
c767944b
DJ
15713 return type;
15714}
15715
2ddeaf8a
TT
15716/* A helper for process_structure_scope that handles a single member
15717 DIE. */
15718
15719static void
15720handle_struct_member_die (struct die_info *child_die, struct type *type,
15721 struct field_info *fi,
15722 std::vector<struct symbol *> *template_args,
15723 struct dwarf2_cu *cu)
15724{
15725 if (child_die->tag == DW_TAG_member
15726 || child_die->tag == DW_TAG_variable
15727 || child_die->tag == DW_TAG_variant_part)
15728 {
15729 /* NOTE: carlton/2002-11-05: A C++ static data member
15730 should be a DW_TAG_member that is a declaration, but
15731 all versions of G++ as of this writing (so through at
15732 least 3.2.1) incorrectly generate DW_TAG_variable
15733 tags for them instead. */
15734 dwarf2_add_field (fi, child_die, cu);
15735 }
15736 else if (child_die->tag == DW_TAG_subprogram)
15737 {
15738 /* Rust doesn't have member functions in the C++ sense.
15739 However, it does emit ordinary functions as children
15740 of a struct DIE. */
15741 if (cu->language == language_rust)
15742 read_func_scope (child_die, cu);
15743 else
15744 {
15745 /* C++ member function. */
15746 dwarf2_add_member_fn (fi, child_die, type, cu);
15747 }
15748 }
15749 else if (child_die->tag == DW_TAG_inheritance)
15750 {
15751 /* C++ base class field. */
15752 dwarf2_add_field (fi, child_die, cu);
15753 }
15754 else if (type_can_define_types (child_die))
15755 dwarf2_add_type_defn (fi, child_die, cu);
15756 else if (child_die->tag == DW_TAG_template_type_param
15757 || child_die->tag == DW_TAG_template_value_param)
15758 {
15759 struct symbol *arg = new_symbol (child_die, NULL, cu);
15760
15761 if (arg != NULL)
15762 template_args->push_back (arg);
15763 }
15764 else if (child_die->tag == DW_TAG_variant)
15765 {
15766 /* In a variant we want to get the discriminant and also add a
15767 field for our sole member child. */
15768 struct attribute *discr = dwarf2_attr (child_die, DW_AT_discr_value, cu);
15769
15770 for (struct die_info *variant_child = child_die->child;
15771 variant_child != NULL;
15772 variant_child = sibling_die (variant_child))
15773 {
15774 if (variant_child->tag == DW_TAG_member)
15775 {
15776 handle_struct_member_die (variant_child, type, fi,
15777 template_args, cu);
15778 /* Only handle the one. */
15779 break;
15780 }
15781 }
15782
15783 /* We don't handle this but we might as well report it if we see
15784 it. */
15785 if (dwarf2_attr (child_die, DW_AT_discr_list, cu) != nullptr)
15786 complaint (&symfile_complaints,
15787 _("DW_AT_discr_list is not supported yet"
15788 " - DIE at %s [in module %s]"),
15789 sect_offset_str (child_die->sect_off),
15790 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15791
15792 /* The first field was just added, so we can stash the
15793 discriminant there. */
be2daae6 15794 gdb_assert (!fi->fields.empty ());
2ddeaf8a 15795 if (discr == NULL)
be2daae6 15796 fi->fields.back ().variant.default_branch = true;
2ddeaf8a 15797 else
be2daae6 15798 fi->fields.back ().variant.discriminant_value = DW_UNSND (discr);
2ddeaf8a
TT
15799 }
15800}
15801
c767944b
DJ
15802/* Finish creating a structure or union type, including filling in
15803 its members and creating a symbol for it. */
15804
15805static void
15806process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
15807{
518817b3 15808 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
ca040673 15809 struct die_info *child_die;
c767944b
DJ
15810 struct type *type;
15811
15812 type = get_die_type (die, cu);
15813 if (type == NULL)
15814 type = read_structure_type (die, cu);
15815
2ddeaf8a
TT
15816 /* When reading a DW_TAG_variant_part, we need to notice when we
15817 read the discriminant member, so we can record it later in the
15818 discriminant_info. */
15819 bool is_variant_part = TYPE_FLAG_DISCRIMINATED_UNION (type);
15820 sect_offset discr_offset;
15821
15822 if (is_variant_part)
15823 {
15824 struct attribute *discr = dwarf2_attr (die, DW_AT_discr, cu);
15825 if (discr == NULL)
15826 {
15827 /* Maybe it's a univariant form, an extension we support.
15828 In this case arrange not to check the offset. */
15829 is_variant_part = false;
15830 }
15831 else if (attr_form_is_ref (discr))
15832 {
15833 struct dwarf2_cu *target_cu = cu;
15834 struct die_info *target_die = follow_die_ref (die, discr, &target_cu);
15835
15836 discr_offset = target_die->sect_off;
15837 }
15838 else
15839 {
15840 complaint (&symfile_complaints,
15841 _("DW_AT_discr does not have DIE reference form"
15842 " - DIE at %s [in module %s]"),
15843 sect_offset_str (die->sect_off),
15844 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15845 is_variant_part = false;
15846 }
15847 }
15848
e142c38c 15849 if (die->child != NULL && ! die_is_declaration (die, cu))
c906108c
SS
15850 {
15851 struct field_info fi;
2f4732b0 15852 std::vector<struct symbol *> template_args;
c906108c 15853
639d11d3 15854 child_die = die->child;
c906108c
SS
15855
15856 while (child_die && child_die->tag)
15857 {
2ddeaf8a 15858 handle_struct_member_die (child_die, type, &fi, &template_args, cu);
34eaf542 15859
2ddeaf8a 15860 if (is_variant_part && discr_offset == child_die->sect_off)
be2daae6 15861 fi.fields.back ().variant.is_discriminant = true;
34eaf542 15862
c906108c
SS
15863 child_die = sibling_die (child_die);
15864 }
15865
34eaf542 15866 /* Attach template arguments to type. */
2f4732b0 15867 if (!template_args.empty ())
34eaf542
TT
15868 {
15869 ALLOCATE_CPLUS_STRUCT_TYPE (type);
2f4732b0 15870 TYPE_N_TEMPLATE_ARGUMENTS (type) = template_args.size ();
34eaf542 15871 TYPE_TEMPLATE_ARGUMENTS (type)
8d749320
SM
15872 = XOBNEWVEC (&objfile->objfile_obstack,
15873 struct symbol *,
15874 TYPE_N_TEMPLATE_ARGUMENTS (type));
34eaf542 15875 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
2f4732b0 15876 template_args.data (),
34eaf542
TT
15877 (TYPE_N_TEMPLATE_ARGUMENTS (type)
15878 * sizeof (struct symbol *)));
34eaf542
TT
15879 }
15880
c906108c
SS
15881 /* Attach fields and member functions to the type. */
15882 if (fi.nfields)
e7c27a73 15883 dwarf2_attach_fields_to_type (&fi, type, cu);
be2daae6 15884 if (!fi.fnfieldlists.empty ())
c906108c 15885 {
e7c27a73 15886 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
c906108c 15887
c5aa993b 15888 /* Get the type which refers to the base class (possibly this
c906108c 15889 class itself) which contains the vtable pointer for the current
0d564a31
DJ
15890 class from the DW_AT_containing_type attribute. This use of
15891 DW_AT_containing_type is a GNU extension. */
c906108c 15892
e142c38c 15893 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
c906108c 15894 {
e7c27a73 15895 struct type *t = die_containing_type (die, cu);
c906108c 15896
ae6ae975 15897 set_type_vptr_basetype (type, t);
c906108c
SS
15898 if (type == t)
15899 {
c906108c
SS
15900 int i;
15901
15902 /* Our own class provides vtbl ptr. */
15903 for (i = TYPE_NFIELDS (t) - 1;
15904 i >= TYPE_N_BASECLASSES (t);
15905 --i)
15906 {
0d5cff50 15907 const char *fieldname = TYPE_FIELD_NAME (t, i);
c906108c 15908
1168df01 15909 if (is_vtable_name (fieldname, cu))
c906108c 15910 {
ae6ae975 15911 set_type_vptr_fieldno (type, i);
c906108c
SS
15912 break;
15913 }
15914 }
15915
15916 /* Complain if virtual function table field not found. */
15917 if (i < TYPE_N_BASECLASSES (t))
4d3c2250 15918 complaint (&symfile_complaints,
3e43a32a
MS
15919 _("virtual function table pointer "
15920 "not found when defining class '%s'"),
4d3c2250
KB
15921 TYPE_TAG_NAME (type) ? TYPE_TAG_NAME (type) :
15922 "");
c906108c
SS
15923 }
15924 else
15925 {
ae6ae975 15926 set_type_vptr_fieldno (type, TYPE_VPTR_FIELDNO (t));
c906108c
SS
15927 }
15928 }
f6235d4c 15929 else if (cu->producer
61012eef 15930 && startswith (cu->producer, "IBM(R) XL C/C++ Advanced Edition"))
f6235d4c
EZ
15931 {
15932 /* The IBM XLC compiler does not provide direct indication
15933 of the containing type, but the vtable pointer is
15934 always named __vfp. */
15935
15936 int i;
15937
15938 for (i = TYPE_NFIELDS (type) - 1;
15939 i >= TYPE_N_BASECLASSES (type);
15940 --i)
15941 {
15942 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
15943 {
ae6ae975
DE
15944 set_type_vptr_fieldno (type, i);
15945 set_type_vptr_basetype (type, type);
f6235d4c
EZ
15946 break;
15947 }
15948 }
15949 }
c906108c 15950 }
98751a41
JK
15951
15952 /* Copy fi.typedef_field_list linked list elements content into the
15953 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
be2daae6 15954 if (!fi.typedef_field_list.empty ())
98751a41 15955 {
be2daae6 15956 int count = fi.typedef_field_list.size ();
98751a41 15957
a0d7a4ff 15958 ALLOCATE_CPLUS_STRUCT_TYPE (type);
98751a41 15959 TYPE_TYPEDEF_FIELD_ARRAY (type)
883fd55a 15960 = ((struct decl_field *)
be2daae6
TT
15961 TYPE_ALLOC (type,
15962 sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * count));
15963 TYPE_TYPEDEF_FIELD_COUNT (type) = count;
6e70227d 15964
be2daae6
TT
15965 for (int i = 0; i < fi.typedef_field_list.size (); ++i)
15966 TYPE_TYPEDEF_FIELD (type, i) = fi.typedef_field_list[i];
98751a41 15967 }
c767944b 15968
883fd55a
KS
15969 /* Copy fi.nested_types_list linked list elements content into the
15970 allocated array TYPE_NESTED_TYPES_ARRAY (type). */
be2daae6 15971 if (!fi.nested_types_list.empty () && cu->language != language_ada)
883fd55a 15972 {
be2daae6 15973 int count = fi.nested_types_list.size ();
883fd55a
KS
15974
15975 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15976 TYPE_NESTED_TYPES_ARRAY (type)
15977 = ((struct decl_field *)
be2daae6
TT
15978 TYPE_ALLOC (type, sizeof (struct decl_field) * count));
15979 TYPE_NESTED_TYPES_COUNT (type) = count;
883fd55a 15980
be2daae6
TT
15981 for (int i = 0; i < fi.nested_types_list.size (); ++i)
15982 TYPE_NESTED_TYPES_FIELD (type, i) = fi.nested_types_list[i];
883fd55a 15983 }
c906108c 15984 }
63d06c5c 15985
bb5ed363 15986 quirk_gcc_member_function_pointer (type, objfile);
c9317f21
TT
15987 if (cu->language == language_rust && die->tag == DW_TAG_union_type)
15988 cu->rust_unions.push_back (type);
0b92b5bb 15989
90aeadfc
DC
15990 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
15991 snapshots) has been known to create a die giving a declaration
15992 for a class that has, as a child, a die giving a definition for a
15993 nested class. So we have to process our children even if the
15994 current die is a declaration. Normally, of course, a declaration
15995 won't have any children at all. */
134d01f1 15996
ca040673
DE
15997 child_die = die->child;
15998
90aeadfc
DC
15999 while (child_die != NULL && child_die->tag)
16000 {
16001 if (child_die->tag == DW_TAG_member
16002 || child_die->tag == DW_TAG_variable
34eaf542
TT
16003 || child_die->tag == DW_TAG_inheritance
16004 || child_die->tag == DW_TAG_template_value_param
16005 || child_die->tag == DW_TAG_template_type_param)
134d01f1 16006 {
90aeadfc 16007 /* Do nothing. */
134d01f1 16008 }
90aeadfc
DC
16009 else
16010 process_die (child_die, cu);
134d01f1 16011
90aeadfc 16012 child_die = sibling_die (child_die);
134d01f1
DJ
16013 }
16014
fa4028e9
JB
16015 /* Do not consider external references. According to the DWARF standard,
16016 these DIEs are identified by the fact that they have no byte_size
16017 attribute, and a declaration attribute. */
16018 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
16019 || !die_is_declaration (die, cu))
c767944b 16020 new_symbol (die, type, cu);
134d01f1
DJ
16021}
16022
55426c9d
JB
16023/* Assuming DIE is an enumeration type, and TYPE is its associated type,
16024 update TYPE using some information only available in DIE's children. */
16025
16026static void
16027update_enumeration_type_from_children (struct die_info *die,
16028 struct type *type,
16029 struct dwarf2_cu *cu)
16030{
60f7655a 16031 struct die_info *child_die;
55426c9d
JB
16032 int unsigned_enum = 1;
16033 int flag_enum = 1;
16034 ULONGEST mask = 0;
55426c9d 16035
8268c778 16036 auto_obstack obstack;
55426c9d 16037
60f7655a
DE
16038 for (child_die = die->child;
16039 child_die != NULL && child_die->tag;
16040 child_die = sibling_die (child_die))
55426c9d
JB
16041 {
16042 struct attribute *attr;
16043 LONGEST value;
16044 const gdb_byte *bytes;
16045 struct dwarf2_locexpr_baton *baton;
16046 const char *name;
60f7655a 16047
55426c9d
JB
16048 if (child_die->tag != DW_TAG_enumerator)
16049 continue;
16050
16051 attr = dwarf2_attr (child_die, DW_AT_const_value, cu);
16052 if (attr == NULL)
16053 continue;
16054
16055 name = dwarf2_name (child_die, cu);
16056 if (name == NULL)
16057 name = "<anonymous enumerator>";
16058
16059 dwarf2_const_value_attr (attr, type, name, &obstack, cu,
16060 &value, &bytes, &baton);
16061 if (value < 0)
16062 {
16063 unsigned_enum = 0;
16064 flag_enum = 0;
16065 }
16066 else if ((mask & value) != 0)
16067 flag_enum = 0;
16068 else
16069 mask |= value;
16070
16071 /* If we already know that the enum type is neither unsigned, nor
16072 a flag type, no need to look at the rest of the enumerates. */
16073 if (!unsigned_enum && !flag_enum)
16074 break;
55426c9d
JB
16075 }
16076
16077 if (unsigned_enum)
16078 TYPE_UNSIGNED (type) = 1;
16079 if (flag_enum)
16080 TYPE_FLAG_ENUM (type) = 1;
55426c9d
JB
16081}
16082
134d01f1
DJ
16083/* Given a DW_AT_enumeration_type die, set its type. We do not
16084 complete the type's fields yet, or create any symbols. */
c906108c 16085
f792889a 16086static struct type *
134d01f1 16087read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16088{
518817b3 16089 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c 16090 struct type *type;
c906108c 16091 struct attribute *attr;
0114d602 16092 const char *name;
134d01f1 16093
348e048f
DE
16094 /* If the definition of this type lives in .debug_types, read that type.
16095 Don't follow DW_AT_specification though, that will take us back up
16096 the chain and we want to go down. */
45e58e77 16097 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
348e048f
DE
16098 if (attr)
16099 {
ac9ec31b 16100 type = get_DW_AT_signature_type (die, attr, cu);
9dc481d3 16101
ac9ec31b 16102 /* The type's CU may not be the same as CU.
02142a6c 16103 Ensure TYPE is recorded with CU in die_type_hash. */
348e048f
DE
16104 return set_die_type (die, type, cu);
16105 }
16106
c906108c
SS
16107 type = alloc_type (objfile);
16108
16109 TYPE_CODE (type) = TYPE_CODE_ENUM;
94af9270 16110 name = dwarf2_full_name (NULL, die, cu);
39cbfefa 16111 if (name != NULL)
7d455152 16112 TYPE_TAG_NAME (type) = name;
c906108c 16113
0626fc76
TT
16114 attr = dwarf2_attr (die, DW_AT_type, cu);
16115 if (attr != NULL)
16116 {
16117 struct type *underlying_type = die_type (die, cu);
16118
16119 TYPE_TARGET_TYPE (type) = underlying_type;
16120 }
16121
e142c38c 16122 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
16123 if (attr)
16124 {
16125 TYPE_LENGTH (type) = DW_UNSND (attr);
16126 }
16127 else
16128 {
16129 TYPE_LENGTH (type) = 0;
16130 }
16131
137033e9
JB
16132 /* The enumeration DIE can be incomplete. In Ada, any type can be
16133 declared as private in the package spec, and then defined only
16134 inside the package body. Such types are known as Taft Amendment
16135 Types. When another package uses such a type, an incomplete DIE
16136 may be generated by the compiler. */
02eb380e 16137 if (die_is_declaration (die, cu))
876cecd0 16138 TYPE_STUB (type) = 1;
02eb380e 16139
0626fc76
TT
16140 /* Finish the creation of this type by using the enum's children.
16141 We must call this even when the underlying type has been provided
16142 so that we can determine if we're looking at a "flag" enum. */
55426c9d
JB
16143 update_enumeration_type_from_children (die, type, cu);
16144
0626fc76
TT
16145 /* If this type has an underlying type that is not a stub, then we
16146 may use its attributes. We always use the "unsigned" attribute
16147 in this situation, because ordinarily we guess whether the type
16148 is unsigned -- but the guess can be wrong and the underlying type
16149 can tell us the reality. However, we defer to a local size
16150 attribute if one exists, because this lets the compiler override
16151 the underlying type if needed. */
16152 if (TYPE_TARGET_TYPE (type) != NULL && !TYPE_STUB (TYPE_TARGET_TYPE (type)))
16153 {
16154 TYPE_UNSIGNED (type) = TYPE_UNSIGNED (TYPE_TARGET_TYPE (type));
16155 if (TYPE_LENGTH (type) == 0)
16156 TYPE_LENGTH (type) = TYPE_LENGTH (TYPE_TARGET_TYPE (type));
16157 }
16158
3d567982
TT
16159 TYPE_DECLARED_CLASS (type) = dwarf2_flag_true_p (die, DW_AT_enum_class, cu);
16160
f792889a 16161 return set_die_type (die, type, cu);
134d01f1
DJ
16162}
16163
16164/* Given a pointer to a die which begins an enumeration, process all
16165 the dies that define the members of the enumeration, and create the
16166 symbol for the enumeration type.
16167
16168 NOTE: We reverse the order of the element list. */
16169
16170static void
16171process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
16172{
f792889a 16173 struct type *this_type;
134d01f1 16174
f792889a
DJ
16175 this_type = get_die_type (die, cu);
16176 if (this_type == NULL)
16177 this_type = read_enumeration_type (die, cu);
9dc481d3 16178
639d11d3 16179 if (die->child != NULL)
c906108c 16180 {
9dc481d3
DE
16181 struct die_info *child_die;
16182 struct symbol *sym;
16183 struct field *fields = NULL;
16184 int num_fields = 0;
15d034d0 16185 const char *name;
9dc481d3 16186
639d11d3 16187 child_die = die->child;
c906108c
SS
16188 while (child_die && child_die->tag)
16189 {
16190 if (child_die->tag != DW_TAG_enumerator)
16191 {
e7c27a73 16192 process_die (child_die, cu);
c906108c
SS
16193 }
16194 else
16195 {
39cbfefa
DJ
16196 name = dwarf2_name (child_die, cu);
16197 if (name)
c906108c 16198 {
f792889a 16199 sym = new_symbol (child_die, this_type, cu);
c906108c
SS
16200
16201 if ((num_fields % DW_FIELD_ALLOC_CHUNK) == 0)
16202 {
16203 fields = (struct field *)
16204 xrealloc (fields,
16205 (num_fields + DW_FIELD_ALLOC_CHUNK)
c5aa993b 16206 * sizeof (struct field));
c906108c
SS
16207 }
16208
3567439c 16209 FIELD_NAME (fields[num_fields]) = SYMBOL_LINKAGE_NAME (sym);
c906108c 16210 FIELD_TYPE (fields[num_fields]) = NULL;
14e75d8e 16211 SET_FIELD_ENUMVAL (fields[num_fields], SYMBOL_VALUE (sym));
c906108c
SS
16212 FIELD_BITSIZE (fields[num_fields]) = 0;
16213
16214 num_fields++;
16215 }
16216 }
16217
16218 child_die = sibling_die (child_die);
16219 }
16220
16221 if (num_fields)
16222 {
f792889a
DJ
16223 TYPE_NFIELDS (this_type) = num_fields;
16224 TYPE_FIELDS (this_type) = (struct field *)
16225 TYPE_ALLOC (this_type, sizeof (struct field) * num_fields);
16226 memcpy (TYPE_FIELDS (this_type), fields,
c906108c 16227 sizeof (struct field) * num_fields);
b8c9b27d 16228 xfree (fields);
c906108c 16229 }
c906108c 16230 }
134d01f1 16231
6c83ed52
TT
16232 /* If we are reading an enum from a .debug_types unit, and the enum
16233 is a declaration, and the enum is not the signatured type in the
16234 unit, then we do not want to add a symbol for it. Adding a
16235 symbol would in some cases obscure the true definition of the
16236 enum, giving users an incomplete type when the definition is
16237 actually available. Note that we do not want to do this for all
16238 enums which are just declarations, because C++0x allows forward
16239 enum declarations. */
3019eac3 16240 if (cu->per_cu->is_debug_types
6c83ed52
TT
16241 && die_is_declaration (die, cu))
16242 {
52dc124a 16243 struct signatured_type *sig_type;
6c83ed52 16244
c0f78cd4 16245 sig_type = (struct signatured_type *) cu->per_cu;
9c541725
PA
16246 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
16247 if (sig_type->type_offset_in_section != die->sect_off)
6c83ed52
TT
16248 return;
16249 }
16250
f792889a 16251 new_symbol (die, this_type, cu);
c906108c
SS
16252}
16253
16254/* Extract all information from a DW_TAG_array_type DIE and put it in
16255 the DIE's type field. For now, this only handles one dimensional
16256 arrays. */
16257
f792889a 16258static struct type *
e7c27a73 16259read_array_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16260{
518817b3 16261 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c 16262 struct die_info *child_die;
7e314c57 16263 struct type *type;
c906108c 16264 struct type *element_type, *range_type, *index_type;
c906108c 16265 struct attribute *attr;
15d034d0 16266 const char *name;
a405673c 16267 struct dynamic_prop *byte_stride_prop = NULL;
dc53a7ad 16268 unsigned int bit_stride = 0;
c906108c 16269
e7c27a73 16270 element_type = die_type (die, cu);
c906108c 16271
7e314c57
JK
16272 /* The die_type call above may have already set the type for this DIE. */
16273 type = get_die_type (die, cu);
16274 if (type)
16275 return type;
16276
dc53a7ad
JB
16277 attr = dwarf2_attr (die, DW_AT_byte_stride, cu);
16278 if (attr != NULL)
a405673c
JB
16279 {
16280 int stride_ok;
16281
16282 byte_stride_prop
16283 = (struct dynamic_prop *) alloca (sizeof (struct dynamic_prop));
16284 stride_ok = attr_to_dynamic_prop (attr, die, cu, byte_stride_prop);
16285 if (!stride_ok)
16286 {
16287 complaint (&symfile_complaints,
16288 _("unable to read array DW_AT_byte_stride "
9d8780f0
SM
16289 " - DIE at %s [in module %s]"),
16290 sect_offset_str (die->sect_off),
518817b3 16291 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
a405673c
JB
16292 /* Ignore this attribute. We will likely not be able to print
16293 arrays of this type correctly, but there is little we can do
16294 to help if we cannot read the attribute's value. */
16295 byte_stride_prop = NULL;
16296 }
16297 }
dc53a7ad
JB
16298
16299 attr = dwarf2_attr (die, DW_AT_bit_stride, cu);
16300 if (attr != NULL)
16301 bit_stride = DW_UNSND (attr);
16302
c906108c
SS
16303 /* Irix 6.2 native cc creates array types without children for
16304 arrays with unspecified length. */
639d11d3 16305 if (die->child == NULL)
c906108c 16306 {
46bf5051 16307 index_type = objfile_type (objfile)->builtin_int;
0c9c3474 16308 range_type = create_static_range_type (NULL, index_type, 0, -1);
dc53a7ad 16309 type = create_array_type_with_stride (NULL, element_type, range_type,
a405673c 16310 byte_stride_prop, bit_stride);
f792889a 16311 return set_die_type (die, type, cu);
c906108c
SS
16312 }
16313
791afaa2 16314 std::vector<struct type *> range_types;
639d11d3 16315 child_die = die->child;
c906108c
SS
16316 while (child_die && child_die->tag)
16317 {
16318 if (child_die->tag == DW_TAG_subrange_type)
16319 {
f792889a 16320 struct type *child_type = read_type_die (child_die, cu);
9a619af0 16321
f792889a 16322 if (child_type != NULL)
a02abb62 16323 {
0963b4bd
MS
16324 /* The range type was succesfully read. Save it for the
16325 array type creation. */
791afaa2 16326 range_types.push_back (child_type);
a02abb62 16327 }
c906108c
SS
16328 }
16329 child_die = sibling_die (child_die);
16330 }
16331
16332 /* Dwarf2 dimensions are output from left to right, create the
16333 necessary array types in backwards order. */
7ca2d3a3 16334
c906108c 16335 type = element_type;
7ca2d3a3
DL
16336
16337 if (read_array_order (die, cu) == DW_ORD_col_major)
16338 {
16339 int i = 0;
9a619af0 16340
791afaa2 16341 while (i < range_types.size ())
dc53a7ad 16342 type = create_array_type_with_stride (NULL, type, range_types[i++],
a405673c 16343 byte_stride_prop, bit_stride);
7ca2d3a3
DL
16344 }
16345 else
16346 {
791afaa2 16347 size_t ndim = range_types.size ();
7ca2d3a3 16348 while (ndim-- > 0)
dc53a7ad 16349 type = create_array_type_with_stride (NULL, type, range_types[ndim],
a405673c 16350 byte_stride_prop, bit_stride);
7ca2d3a3 16351 }
c906108c 16352
f5f8a009
EZ
16353 /* Understand Dwarf2 support for vector types (like they occur on
16354 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
16355 array type. This is not part of the Dwarf2/3 standard yet, but a
16356 custom vendor extension. The main difference between a regular
16357 array and the vector variant is that vectors are passed by value
16358 to functions. */
e142c38c 16359 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
f5f8a009 16360 if (attr)
ea37ba09 16361 make_vector_type (type);
f5f8a009 16362
dbc98a8b
KW
16363 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
16364 implementation may choose to implement triple vectors using this
16365 attribute. */
16366 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16367 if (attr)
16368 {
16369 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
16370 TYPE_LENGTH (type) = DW_UNSND (attr);
16371 else
3e43a32a
MS
16372 complaint (&symfile_complaints,
16373 _("DW_AT_byte_size for array type smaller "
16374 "than the total size of elements"));
dbc98a8b
KW
16375 }
16376
39cbfefa
DJ
16377 name = dwarf2_name (die, cu);
16378 if (name)
16379 TYPE_NAME (type) = name;
6e70227d 16380
0963b4bd 16381 /* Install the type in the die. */
7e314c57
JK
16382 set_die_type (die, type, cu);
16383
16384 /* set_die_type should be already done. */
b4ba55a1
JB
16385 set_descriptive_type (type, die, cu);
16386
7e314c57 16387 return type;
c906108c
SS
16388}
16389
7ca2d3a3 16390static enum dwarf_array_dim_ordering
6e70227d 16391read_array_order (struct die_info *die, struct dwarf2_cu *cu)
7ca2d3a3
DL
16392{
16393 struct attribute *attr;
16394
16395 attr = dwarf2_attr (die, DW_AT_ordering, cu);
16396
aead7601
SM
16397 if (attr)
16398 return (enum dwarf_array_dim_ordering) DW_SND (attr);
7ca2d3a3 16399
0963b4bd
MS
16400 /* GNU F77 is a special case, as at 08/2004 array type info is the
16401 opposite order to the dwarf2 specification, but data is still
16402 laid out as per normal fortran.
7ca2d3a3 16403
0963b4bd
MS
16404 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
16405 version checking. */
7ca2d3a3 16406
905e0470
PM
16407 if (cu->language == language_fortran
16408 && cu->producer && strstr (cu->producer, "GNU F77"))
7ca2d3a3
DL
16409 {
16410 return DW_ORD_row_major;
16411 }
16412
6e70227d 16413 switch (cu->language_defn->la_array_ordering)
7ca2d3a3
DL
16414 {
16415 case array_column_major:
16416 return DW_ORD_col_major;
16417 case array_row_major:
16418 default:
16419 return DW_ORD_row_major;
16420 };
16421}
16422
72019c9c 16423/* Extract all information from a DW_TAG_set_type DIE and put it in
0963b4bd 16424 the DIE's type field. */
72019c9c 16425
f792889a 16426static struct type *
72019c9c
GM
16427read_set_type (struct die_info *die, struct dwarf2_cu *cu)
16428{
7e314c57
JK
16429 struct type *domain_type, *set_type;
16430 struct attribute *attr;
f792889a 16431
7e314c57
JK
16432 domain_type = die_type (die, cu);
16433
16434 /* The die_type call above may have already set the type for this DIE. */
16435 set_type = get_die_type (die, cu);
16436 if (set_type)
16437 return set_type;
16438
16439 set_type = create_set_type (NULL, domain_type);
16440
16441 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
d09039dd
PM
16442 if (attr)
16443 TYPE_LENGTH (set_type) = DW_UNSND (attr);
7e314c57 16444
f792889a 16445 return set_die_type (die, set_type, cu);
72019c9c 16446}
7ca2d3a3 16447
0971de02
TT
16448/* A helper for read_common_block that creates a locexpr baton.
16449 SYM is the symbol which we are marking as computed.
16450 COMMON_DIE is the DIE for the common block.
16451 COMMON_LOC is the location expression attribute for the common
16452 block itself.
16453 MEMBER_LOC is the location expression attribute for the particular
16454 member of the common block that we are processing.
16455 CU is the CU from which the above come. */
16456
16457static void
16458mark_common_block_symbol_computed (struct symbol *sym,
16459 struct die_info *common_die,
16460 struct attribute *common_loc,
16461 struct attribute *member_loc,
16462 struct dwarf2_cu *cu)
16463{
518817b3
SM
16464 struct dwarf2_per_objfile *dwarf2_per_objfile
16465 = cu->per_cu->dwarf2_per_objfile;
0971de02
TT
16466 struct objfile *objfile = dwarf2_per_objfile->objfile;
16467 struct dwarf2_locexpr_baton *baton;
16468 gdb_byte *ptr;
16469 unsigned int cu_off;
16470 enum bfd_endian byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
16471 LONGEST offset = 0;
16472
16473 gdb_assert (common_loc && member_loc);
16474 gdb_assert (attr_form_is_block (common_loc));
16475 gdb_assert (attr_form_is_block (member_loc)
16476 || attr_form_is_constant (member_loc));
16477
8d749320 16478 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
0971de02
TT
16479 baton->per_cu = cu->per_cu;
16480 gdb_assert (baton->per_cu);
16481
16482 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
16483
16484 if (attr_form_is_constant (member_loc))
16485 {
16486 offset = dwarf2_get_attr_constant_value (member_loc, 0);
16487 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
16488 }
16489 else
16490 baton->size += DW_BLOCK (member_loc)->size;
16491
224c3ddb 16492 ptr = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, baton->size);
0971de02
TT
16493 baton->data = ptr;
16494
16495 *ptr++ = DW_OP_call4;
9c541725 16496 cu_off = common_die->sect_off - cu->per_cu->sect_off;
0971de02
TT
16497 store_unsigned_integer (ptr, 4, byte_order, cu_off);
16498 ptr += 4;
16499
16500 if (attr_form_is_constant (member_loc))
16501 {
16502 *ptr++ = DW_OP_addr;
16503 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
16504 ptr += cu->header.addr_size;
16505 }
16506 else
16507 {
16508 /* We have to copy the data here, because DW_OP_call4 will only
16509 use a DW_AT_location attribute. */
16510 memcpy (ptr, DW_BLOCK (member_loc)->data, DW_BLOCK (member_loc)->size);
16511 ptr += DW_BLOCK (member_loc)->size;
16512 }
16513
16514 *ptr++ = DW_OP_plus;
16515 gdb_assert (ptr - baton->data == baton->size);
16516
0971de02 16517 SYMBOL_LOCATION_BATON (sym) = baton;
f1e6e072 16518 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
0971de02
TT
16519}
16520
4357ac6c
TT
16521/* Create appropriate locally-scoped variables for all the
16522 DW_TAG_common_block entries. Also create a struct common_block
16523 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
16524 is used to sepate the common blocks name namespace from regular
16525 variable names. */
c906108c
SS
16526
16527static void
e7c27a73 16528read_common_block (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16529{
0971de02
TT
16530 struct attribute *attr;
16531
16532 attr = dwarf2_attr (die, DW_AT_location, cu);
16533 if (attr)
16534 {
16535 /* Support the .debug_loc offsets. */
16536 if (attr_form_is_block (attr))
16537 {
16538 /* Ok. */
16539 }
16540 else if (attr_form_is_section_offset (attr))
16541 {
16542 dwarf2_complex_location_expr_complaint ();
16543 attr = NULL;
16544 }
16545 else
16546 {
16547 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
16548 "common block member");
16549 attr = NULL;
16550 }
16551 }
16552
639d11d3 16553 if (die->child != NULL)
c906108c 16554 {
518817b3 16555 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
4357ac6c
TT
16556 struct die_info *child_die;
16557 size_t n_entries = 0, size;
16558 struct common_block *common_block;
16559 struct symbol *sym;
74ac6d43 16560
4357ac6c
TT
16561 for (child_die = die->child;
16562 child_die && child_die->tag;
16563 child_die = sibling_die (child_die))
16564 ++n_entries;
16565
16566 size = (sizeof (struct common_block)
16567 + (n_entries - 1) * sizeof (struct symbol *));
224c3ddb
SM
16568 common_block
16569 = (struct common_block *) obstack_alloc (&objfile->objfile_obstack,
16570 size);
4357ac6c
TT
16571 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
16572 common_block->n_entries = 0;
16573
16574 for (child_die = die->child;
16575 child_die && child_die->tag;
16576 child_die = sibling_die (child_die))
16577 {
16578 /* Create the symbol in the DW_TAG_common_block block in the current
16579 symbol scope. */
e7c27a73 16580 sym = new_symbol (child_die, NULL, cu);
0971de02
TT
16581 if (sym != NULL)
16582 {
16583 struct attribute *member_loc;
16584
16585 common_block->contents[common_block->n_entries++] = sym;
16586
16587 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
16588 cu);
16589 if (member_loc)
16590 {
16591 /* GDB has handled this for a long time, but it is
16592 not specified by DWARF. It seems to have been
16593 emitted by gfortran at least as recently as:
16594 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
16595 complaint (&symfile_complaints,
16596 _("Variable in common block has "
16597 "DW_AT_data_member_location "
9d8780f0
SM
16598 "- DIE at %s [in module %s]"),
16599 sect_offset_str (child_die->sect_off),
518817b3 16600 objfile_name (objfile));
0971de02
TT
16601
16602 if (attr_form_is_section_offset (member_loc))
16603 dwarf2_complex_location_expr_complaint ();
16604 else if (attr_form_is_constant (member_loc)
16605 || attr_form_is_block (member_loc))
16606 {
16607 if (attr)
16608 mark_common_block_symbol_computed (sym, die, attr,
16609 member_loc, cu);
16610 }
16611 else
16612 dwarf2_complex_location_expr_complaint ();
16613 }
16614 }
c906108c 16615 }
4357ac6c
TT
16616
16617 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
16618 SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
c906108c
SS
16619 }
16620}
16621
0114d602 16622/* Create a type for a C++ namespace. */
d9fa45fe 16623
0114d602
DJ
16624static struct type *
16625read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
d9fa45fe 16626{
518817b3 16627 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
0114d602 16628 const char *previous_prefix, *name;
9219021c 16629 int is_anonymous;
0114d602
DJ
16630 struct type *type;
16631
16632 /* For extensions, reuse the type of the original namespace. */
16633 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
16634 {
16635 struct die_info *ext_die;
16636 struct dwarf2_cu *ext_cu = cu;
9a619af0 16637
0114d602
DJ
16638 ext_die = dwarf2_extension (die, &ext_cu);
16639 type = read_type_die (ext_die, ext_cu);
9dc481d3
DE
16640
16641 /* EXT_CU may not be the same as CU.
02142a6c 16642 Ensure TYPE is recorded with CU in die_type_hash. */
0114d602
DJ
16643 return set_die_type (die, type, cu);
16644 }
9219021c 16645
e142c38c 16646 name = namespace_name (die, &is_anonymous, cu);
9219021c
DC
16647
16648 /* Now build the name of the current namespace. */
16649
0114d602
DJ
16650 previous_prefix = determine_prefix (die, cu);
16651 if (previous_prefix[0] != '\0')
16652 name = typename_concat (&objfile->objfile_obstack,
f55ee35c 16653 previous_prefix, name, 0, cu);
0114d602
DJ
16654
16655 /* Create the type. */
19f392bc 16656 type = init_type (objfile, TYPE_CODE_NAMESPACE, 0, name);
0114d602
DJ
16657 TYPE_TAG_NAME (type) = TYPE_NAME (type);
16658
60531b24 16659 return set_die_type (die, type, cu);
0114d602
DJ
16660}
16661
22cee43f 16662/* Read a namespace scope. */
0114d602
DJ
16663
16664static void
16665read_namespace (struct die_info *die, struct dwarf2_cu *cu)
16666{
518817b3 16667 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
0114d602 16668 int is_anonymous;
9219021c 16669
5c4e30ca
DC
16670 /* Add a symbol associated to this if we haven't seen the namespace
16671 before. Also, add a using directive if it's an anonymous
16672 namespace. */
9219021c 16673
f2f0e013 16674 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
5c4e30ca
DC
16675 {
16676 struct type *type;
16677
0114d602 16678 type = read_type_die (die, cu);
e7c27a73 16679 new_symbol (die, type, cu);
5c4e30ca 16680
e8e80198 16681 namespace_name (die, &is_anonymous, cu);
5c4e30ca 16682 if (is_anonymous)
0114d602
DJ
16683 {
16684 const char *previous_prefix = determine_prefix (die, cu);
9a619af0 16685
eb1e02fd 16686 std::vector<const char *> excludes;
22cee43f
PMR
16687 add_using_directive (using_directives (cu->language),
16688 previous_prefix, TYPE_NAME (type), NULL,
eb1e02fd 16689 NULL, excludes, 0, &objfile->objfile_obstack);
0114d602 16690 }
5c4e30ca 16691 }
9219021c 16692
639d11d3 16693 if (die->child != NULL)
d9fa45fe 16694 {
639d11d3 16695 struct die_info *child_die = die->child;
6e70227d 16696
d9fa45fe
DC
16697 while (child_die && child_die->tag)
16698 {
e7c27a73 16699 process_die (child_die, cu);
d9fa45fe
DC
16700 child_die = sibling_die (child_die);
16701 }
16702 }
38d518c9
EZ
16703}
16704
f55ee35c
JK
16705/* Read a Fortran module as type. This DIE can be only a declaration used for
16706 imported module. Still we need that type as local Fortran "use ... only"
16707 declaration imports depend on the created type in determine_prefix. */
16708
16709static struct type *
16710read_module_type (struct die_info *die, struct dwarf2_cu *cu)
16711{
518817b3 16712 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15d034d0 16713 const char *module_name;
f55ee35c
JK
16714 struct type *type;
16715
16716 module_name = dwarf2_name (die, cu);
16717 if (!module_name)
3e43a32a 16718 complaint (&symfile_complaints,
9d8780f0
SM
16719 _("DW_TAG_module has no name, offset %s"),
16720 sect_offset_str (die->sect_off));
19f392bc 16721 type = init_type (objfile, TYPE_CODE_MODULE, 0, module_name);
f55ee35c
JK
16722
16723 /* determine_prefix uses TYPE_TAG_NAME. */
16724 TYPE_TAG_NAME (type) = TYPE_NAME (type);
16725
16726 return set_die_type (die, type, cu);
16727}
16728
5d7cb8df
JK
16729/* Read a Fortran module. */
16730
16731static void
16732read_module (struct die_info *die, struct dwarf2_cu *cu)
16733{
16734 struct die_info *child_die = die->child;
530e8392
KB
16735 struct type *type;
16736
16737 type = read_type_die (die, cu);
16738 new_symbol (die, type, cu);
5d7cb8df 16739
5d7cb8df
JK
16740 while (child_die && child_die->tag)
16741 {
16742 process_die (child_die, cu);
16743 child_die = sibling_die (child_die);
16744 }
16745}
16746
38d518c9
EZ
16747/* Return the name of the namespace represented by DIE. Set
16748 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
16749 namespace. */
16750
16751static const char *
e142c38c 16752namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
38d518c9
EZ
16753{
16754 struct die_info *current_die;
16755 const char *name = NULL;
16756
16757 /* Loop through the extensions until we find a name. */
16758
16759 for (current_die = die;
16760 current_die != NULL;
f2f0e013 16761 current_die = dwarf2_extension (die, &cu))
38d518c9 16762 {
96553a0c
DE
16763 /* We don't use dwarf2_name here so that we can detect the absence
16764 of a name -> anonymous namespace. */
7d45c7c3 16765 name = dwarf2_string_attr (die, DW_AT_name, cu);
96553a0c 16766
38d518c9
EZ
16767 if (name != NULL)
16768 break;
16769 }
16770
16771 /* Is it an anonymous namespace? */
16772
16773 *is_anonymous = (name == NULL);
16774 if (*is_anonymous)
2b1dbab0 16775 name = CP_ANONYMOUS_NAMESPACE_STR;
38d518c9
EZ
16776
16777 return name;
d9fa45fe
DC
16778}
16779
c906108c
SS
16780/* Extract all information from a DW_TAG_pointer_type DIE and add to
16781 the user defined type vector. */
16782
f792889a 16783static struct type *
e7c27a73 16784read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16785{
518817b3
SM
16786 struct gdbarch *gdbarch
16787 = get_objfile_arch (cu->per_cu->dwarf2_per_objfile->objfile);
e7c27a73 16788 struct comp_unit_head *cu_header = &cu->header;
c906108c 16789 struct type *type;
8b2dbe47
KB
16790 struct attribute *attr_byte_size;
16791 struct attribute *attr_address_class;
16792 int byte_size, addr_class;
7e314c57
JK
16793 struct type *target_type;
16794
16795 target_type = die_type (die, cu);
c906108c 16796
7e314c57
JK
16797 /* The die_type call above may have already set the type for this DIE. */
16798 type = get_die_type (die, cu);
16799 if (type)
16800 return type;
16801
16802 type = lookup_pointer_type (target_type);
8b2dbe47 16803
e142c38c 16804 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
8b2dbe47
KB
16805 if (attr_byte_size)
16806 byte_size = DW_UNSND (attr_byte_size);
c906108c 16807 else
8b2dbe47
KB
16808 byte_size = cu_header->addr_size;
16809
e142c38c 16810 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
8b2dbe47
KB
16811 if (attr_address_class)
16812 addr_class = DW_UNSND (attr_address_class);
16813 else
16814 addr_class = DW_ADDR_none;
16815
16816 /* If the pointer size or address class is different than the
16817 default, create a type variant marked as such and set the
16818 length accordingly. */
16819 if (TYPE_LENGTH (type) != byte_size || addr_class != DW_ADDR_none)
c906108c 16820 {
5e2b427d 16821 if (gdbarch_address_class_type_flags_p (gdbarch))
8b2dbe47
KB
16822 {
16823 int type_flags;
16824
849957d9 16825 type_flags = gdbarch_address_class_type_flags
5e2b427d 16826 (gdbarch, byte_size, addr_class);
876cecd0
TT
16827 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
16828 == 0);
8b2dbe47
KB
16829 type = make_type_with_address_space (type, type_flags);
16830 }
16831 else if (TYPE_LENGTH (type) != byte_size)
16832 {
3e43a32a
MS
16833 complaint (&symfile_complaints,
16834 _("invalid pointer size %d"), byte_size);
8b2dbe47 16835 }
6e70227d 16836 else
9a619af0
MS
16837 {
16838 /* Should we also complain about unhandled address classes? */
16839 }
c906108c 16840 }
8b2dbe47
KB
16841
16842 TYPE_LENGTH (type) = byte_size;
f792889a 16843 return set_die_type (die, type, cu);
c906108c
SS
16844}
16845
16846/* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
16847 the user defined type vector. */
16848
f792889a 16849static struct type *
e7c27a73 16850read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
16851{
16852 struct type *type;
16853 struct type *to_type;
16854 struct type *domain;
16855
e7c27a73
DJ
16856 to_type = die_type (die, cu);
16857 domain = die_containing_type (die, cu);
0d5de010 16858
7e314c57
JK
16859 /* The calls above may have already set the type for this DIE. */
16860 type = get_die_type (die, cu);
16861 if (type)
16862 return type;
16863
0d5de010
DJ
16864 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
16865 type = lookup_methodptr_type (to_type);
7078baeb
TT
16866 else if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_FUNC)
16867 {
518817b3
SM
16868 struct type *new_type
16869 = alloc_type (cu->per_cu->dwarf2_per_objfile->objfile);
7078baeb
TT
16870
16871 smash_to_method_type (new_type, domain, TYPE_TARGET_TYPE (to_type),
16872 TYPE_FIELDS (to_type), TYPE_NFIELDS (to_type),
16873 TYPE_VARARGS (to_type));
16874 type = lookup_methodptr_type (new_type);
16875 }
0d5de010
DJ
16876 else
16877 type = lookup_memberptr_type (to_type, domain);
c906108c 16878
f792889a 16879 return set_die_type (die, type, cu);
c906108c
SS
16880}
16881
4297a3f0 16882/* Extract all information from a DW_TAG_{rvalue_,}reference_type DIE and add to
c906108c
SS
16883 the user defined type vector. */
16884
f792889a 16885static struct type *
4297a3f0
AV
16886read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu,
16887 enum type_code refcode)
c906108c 16888{
e7c27a73 16889 struct comp_unit_head *cu_header = &cu->header;
7e314c57 16890 struct type *type, *target_type;
c906108c
SS
16891 struct attribute *attr;
16892
4297a3f0
AV
16893 gdb_assert (refcode == TYPE_CODE_REF || refcode == TYPE_CODE_RVALUE_REF);
16894
7e314c57
JK
16895 target_type = die_type (die, cu);
16896
16897 /* The die_type call above may have already set the type for this DIE. */
16898 type = get_die_type (die, cu);
16899 if (type)
16900 return type;
16901
4297a3f0 16902 type = lookup_reference_type (target_type, refcode);
e142c38c 16903 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
16904 if (attr)
16905 {
16906 TYPE_LENGTH (type) = DW_UNSND (attr);
16907 }
16908 else
16909 {
107d2387 16910 TYPE_LENGTH (type) = cu_header->addr_size;
c906108c 16911 }
f792889a 16912 return set_die_type (die, type, cu);
c906108c
SS
16913}
16914
cf363f18
MW
16915/* Add the given cv-qualifiers to the element type of the array. GCC
16916 outputs DWARF type qualifiers that apply to an array, not the
16917 element type. But GDB relies on the array element type to carry
16918 the cv-qualifiers. This mimics section 6.7.3 of the C99
16919 specification. */
16920
16921static struct type *
16922add_array_cv_type (struct die_info *die, struct dwarf2_cu *cu,
16923 struct type *base_type, int cnst, int voltl)
16924{
16925 struct type *el_type, *inner_array;
16926
16927 base_type = copy_type (base_type);
16928 inner_array = base_type;
16929
16930 while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
16931 {
16932 TYPE_TARGET_TYPE (inner_array) =
16933 copy_type (TYPE_TARGET_TYPE (inner_array));
16934 inner_array = TYPE_TARGET_TYPE (inner_array);
16935 }
16936
16937 el_type = TYPE_TARGET_TYPE (inner_array);
16938 cnst |= TYPE_CONST (el_type);
16939 voltl |= TYPE_VOLATILE (el_type);
16940 TYPE_TARGET_TYPE (inner_array) = make_cv_type (cnst, voltl, el_type, NULL);
16941
16942 return set_die_type (die, base_type, cu);
16943}
16944
f792889a 16945static struct type *
e7c27a73 16946read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16947{
f792889a 16948 struct type *base_type, *cv_type;
c906108c 16949
e7c27a73 16950 base_type = die_type (die, cu);
7e314c57
JK
16951
16952 /* The die_type call above may have already set the type for this DIE. */
16953 cv_type = get_die_type (die, cu);
16954 if (cv_type)
16955 return cv_type;
16956
2f608a3a
KW
16957 /* In case the const qualifier is applied to an array type, the element type
16958 is so qualified, not the array type (section 6.7.3 of C99). */
16959 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
cf363f18 16960 return add_array_cv_type (die, cu, base_type, 1, 0);
2f608a3a 16961
f792889a
DJ
16962 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
16963 return set_die_type (die, cv_type, cu);
c906108c
SS
16964}
16965
f792889a 16966static struct type *
e7c27a73 16967read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16968{
f792889a 16969 struct type *base_type, *cv_type;
c906108c 16970
e7c27a73 16971 base_type = die_type (die, cu);
7e314c57
JK
16972
16973 /* The die_type call above may have already set the type for this DIE. */
16974 cv_type = get_die_type (die, cu);
16975 if (cv_type)
16976 return cv_type;
16977
cf363f18
MW
16978 /* In case the volatile qualifier is applied to an array type, the
16979 element type is so qualified, not the array type (section 6.7.3
16980 of C99). */
16981 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
16982 return add_array_cv_type (die, cu, base_type, 0, 1);
16983
f792889a
DJ
16984 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
16985 return set_die_type (die, cv_type, cu);
c906108c
SS
16986}
16987
06d66ee9
TT
16988/* Handle DW_TAG_restrict_type. */
16989
16990static struct type *
16991read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
16992{
16993 struct type *base_type, *cv_type;
16994
16995 base_type = die_type (die, cu);
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
17002 cv_type = make_restrict_type (base_type);
17003 return set_die_type (die, cv_type, cu);
17004}
17005
a2c2acaf
MW
17006/* Handle DW_TAG_atomic_type. */
17007
17008static struct type *
17009read_tag_atomic_type (struct die_info *die, struct dwarf2_cu *cu)
17010{
17011 struct type *base_type, *cv_type;
17012
17013 base_type = die_type (die, cu);
17014
17015 /* The die_type call above may have already set the type for this DIE. */
17016 cv_type = get_die_type (die, cu);
17017 if (cv_type)
17018 return cv_type;
17019
17020 cv_type = make_atomic_type (base_type);
17021 return set_die_type (die, cv_type, cu);
17022}
17023
c906108c
SS
17024/* Extract all information from a DW_TAG_string_type DIE and add to
17025 the user defined type vector. It isn't really a user defined type,
17026 but it behaves like one, with other DIE's using an AT_user_def_type
17027 attribute to reference it. */
17028
f792889a 17029static struct type *
e7c27a73 17030read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17031{
518817b3 17032 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
3b7538c0 17033 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
17034 struct type *type, *range_type, *index_type, *char_type;
17035 struct attribute *attr;
17036 unsigned int length;
17037
e142c38c 17038 attr = dwarf2_attr (die, DW_AT_string_length, cu);
c906108c
SS
17039 if (attr)
17040 {
17041 length = DW_UNSND (attr);
17042 }
17043 else
17044 {
0963b4bd 17045 /* Check for the DW_AT_byte_size attribute. */
e142c38c 17046 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
b21b22e0
PS
17047 if (attr)
17048 {
17049 length = DW_UNSND (attr);
17050 }
17051 else
17052 {
17053 length = 1;
17054 }
c906108c 17055 }
6ccb9162 17056
46bf5051 17057 index_type = objfile_type (objfile)->builtin_int;
0c9c3474 17058 range_type = create_static_range_type (NULL, index_type, 1, length);
3b7538c0
UW
17059 char_type = language_string_char_type (cu->language_defn, gdbarch);
17060 type = create_string_type (NULL, char_type, range_type);
6ccb9162 17061
f792889a 17062 return set_die_type (die, type, cu);
c906108c
SS
17063}
17064
4d804846
JB
17065/* Assuming that DIE corresponds to a function, returns nonzero
17066 if the function is prototyped. */
17067
17068static int
17069prototyped_function_p (struct die_info *die, struct dwarf2_cu *cu)
17070{
17071 struct attribute *attr;
17072
17073 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
17074 if (attr && (DW_UNSND (attr) != 0))
17075 return 1;
17076
17077 /* The DWARF standard implies that the DW_AT_prototyped attribute
17078 is only meaninful for C, but the concept also extends to other
17079 languages that allow unprototyped functions (Eg: Objective C).
17080 For all other languages, assume that functions are always
17081 prototyped. */
17082 if (cu->language != language_c
17083 && cu->language != language_objc
17084 && cu->language != language_opencl)
17085 return 1;
17086
17087 /* RealView does not emit DW_AT_prototyped. We can not distinguish
17088 prototyped and unprototyped functions; default to prototyped,
17089 since that is more common in modern code (and RealView warns
17090 about unprototyped functions). */
17091 if (producer_is_realview (cu->producer))
17092 return 1;
17093
17094 return 0;
17095}
17096
c906108c
SS
17097/* Handle DIES due to C code like:
17098
17099 struct foo
c5aa993b
JM
17100 {
17101 int (*funcp)(int a, long l);
17102 int b;
17103 };
c906108c 17104
0963b4bd 17105 ('funcp' generates a DW_TAG_subroutine_type DIE). */
c906108c 17106
f792889a 17107static struct type *
e7c27a73 17108read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17109{
518817b3 17110 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
0963b4bd
MS
17111 struct type *type; /* Type that this function returns. */
17112 struct type *ftype; /* Function that returns above type. */
c906108c
SS
17113 struct attribute *attr;
17114
e7c27a73 17115 type = die_type (die, cu);
7e314c57
JK
17116
17117 /* The die_type call above may have already set the type for this DIE. */
17118 ftype = get_die_type (die, cu);
17119 if (ftype)
17120 return ftype;
17121
0c8b41f1 17122 ftype = lookup_function_type (type);
c906108c 17123
4d804846 17124 if (prototyped_function_p (die, cu))
a6c727b2 17125 TYPE_PROTOTYPED (ftype) = 1;
c906108c 17126
c055b101
CV
17127 /* Store the calling convention in the type if it's available in
17128 the subroutine die. Otherwise set the calling convention to
17129 the default value DW_CC_normal. */
17130 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
54fcddd0
UW
17131 if (attr)
17132 TYPE_CALLING_CONVENTION (ftype) = DW_UNSND (attr);
17133 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
17134 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
17135 else
17136 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
76c10ea2 17137
743649fd
MW
17138 /* Record whether the function returns normally to its caller or not
17139 if the DWARF producer set that information. */
17140 attr = dwarf2_attr (die, DW_AT_noreturn, cu);
17141 if (attr && (DW_UNSND (attr) != 0))
17142 TYPE_NO_RETURN (ftype) = 1;
17143
76c10ea2
GM
17144 /* We need to add the subroutine type to the die immediately so
17145 we don't infinitely recurse when dealing with parameters
0963b4bd 17146 declared as the same subroutine type. */
76c10ea2 17147 set_die_type (die, ftype, cu);
6e70227d 17148
639d11d3 17149 if (die->child != NULL)
c906108c 17150 {
bb5ed363 17151 struct type *void_type = objfile_type (objfile)->builtin_void;
c906108c 17152 struct die_info *child_die;
8072405b 17153 int nparams, iparams;
c906108c
SS
17154
17155 /* Count the number of parameters.
17156 FIXME: GDB currently ignores vararg functions, but knows about
17157 vararg member functions. */
8072405b 17158 nparams = 0;
639d11d3 17159 child_die = die->child;
c906108c
SS
17160 while (child_die && child_die->tag)
17161 {
17162 if (child_die->tag == DW_TAG_formal_parameter)
17163 nparams++;
17164 else if (child_die->tag == DW_TAG_unspecified_parameters)
876cecd0 17165 TYPE_VARARGS (ftype) = 1;
c906108c
SS
17166 child_die = sibling_die (child_die);
17167 }
17168
17169 /* Allocate storage for parameters and fill them in. */
17170 TYPE_NFIELDS (ftype) = nparams;
17171 TYPE_FIELDS (ftype) = (struct field *)
ae5a43e0 17172 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
c906108c 17173
8072405b
JK
17174 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
17175 even if we error out during the parameters reading below. */
17176 for (iparams = 0; iparams < nparams; iparams++)
17177 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
17178
17179 iparams = 0;
639d11d3 17180 child_die = die->child;
c906108c
SS
17181 while (child_die && child_die->tag)
17182 {
17183 if (child_die->tag == DW_TAG_formal_parameter)
17184 {
3ce3b1ba
PA
17185 struct type *arg_type;
17186
17187 /* DWARF version 2 has no clean way to discern C++
17188 static and non-static member functions. G++ helps
17189 GDB by marking the first parameter for non-static
17190 member functions (which is the this pointer) as
17191 artificial. We pass this information to
17192 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
17193
17194 DWARF version 3 added DW_AT_object_pointer, which GCC
17195 4.5 does not yet generate. */
e142c38c 17196 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
c906108c
SS
17197 if (attr)
17198 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
17199 else
9c37b5ae 17200 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
3ce3b1ba
PA
17201 arg_type = die_type (child_die, cu);
17202
17203 /* RealView does not mark THIS as const, which the testsuite
17204 expects. GCC marks THIS as const in method definitions,
17205 but not in the class specifications (GCC PR 43053). */
17206 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
17207 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
17208 {
17209 int is_this = 0;
17210 struct dwarf2_cu *arg_cu = cu;
17211 const char *name = dwarf2_name (child_die, cu);
17212
17213 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
17214 if (attr)
17215 {
17216 /* If the compiler emits this, use it. */
17217 if (follow_die_ref (die, attr, &arg_cu) == child_die)
17218 is_this = 1;
17219 }
17220 else if (name && strcmp (name, "this") == 0)
17221 /* Function definitions will have the argument names. */
17222 is_this = 1;
17223 else if (name == NULL && iparams == 0)
17224 /* Declarations may not have the names, so like
17225 elsewhere in GDB, assume an artificial first
17226 argument is "this". */
17227 is_this = 1;
17228
17229 if (is_this)
17230 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
17231 arg_type, 0);
17232 }
17233
17234 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
c906108c
SS
17235 iparams++;
17236 }
17237 child_die = sibling_die (child_die);
17238 }
17239 }
17240
76c10ea2 17241 return ftype;
c906108c
SS
17242}
17243
f792889a 17244static struct type *
e7c27a73 17245read_typedef (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17246{
518817b3 17247 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
0114d602 17248 const char *name = NULL;
3c8e0968 17249 struct type *this_type, *target_type;
c906108c 17250
94af9270 17251 name = dwarf2_full_name (NULL, die, cu);
19f392bc
UW
17252 this_type = init_type (objfile, TYPE_CODE_TYPEDEF, 0, name);
17253 TYPE_TARGET_STUB (this_type) = 1;
f792889a 17254 set_die_type (die, this_type, cu);
3c8e0968
DE
17255 target_type = die_type (die, cu);
17256 if (target_type != this_type)
17257 TYPE_TARGET_TYPE (this_type) = target_type;
17258 else
17259 {
17260 /* Self-referential typedefs are, it seems, not allowed by the DWARF
17261 spec and cause infinite loops in GDB. */
17262 complaint (&symfile_complaints,
17263 _("Self-referential DW_TAG_typedef "
9d8780f0
SM
17264 "- DIE at %s [in module %s]"),
17265 sect_offset_str (die->sect_off), objfile_name (objfile));
3c8e0968
DE
17266 TYPE_TARGET_TYPE (this_type) = NULL;
17267 }
f792889a 17268 return this_type;
c906108c
SS
17269}
17270
9b790ce7
UW
17271/* Allocate a floating-point type of size BITS and name NAME. Pass NAME_HINT
17272 (which may be different from NAME) to the architecture back-end to allow
17273 it to guess the correct format if necessary. */
17274
17275static struct type *
17276dwarf2_init_float_type (struct objfile *objfile, int bits, const char *name,
17277 const char *name_hint)
17278{
17279 struct gdbarch *gdbarch = get_objfile_arch (objfile);
17280 const struct floatformat **format;
17281 struct type *type;
17282
17283 format = gdbarch_floatformat_for_type (gdbarch, name_hint, bits);
17284 if (format)
17285 type = init_float_type (objfile, bits, name, format);
17286 else
77b7c781 17287 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
9b790ce7
UW
17288
17289 return type;
17290}
17291
c906108c
SS
17292/* Find a representation of a given base type and install
17293 it in the TYPE field of the die. */
17294
f792889a 17295static struct type *
e7c27a73 17296read_base_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17297{
518817b3 17298 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c
SS
17299 struct type *type;
17300 struct attribute *attr;
19f392bc 17301 int encoding = 0, bits = 0;
15d034d0 17302 const char *name;
c906108c 17303
e142c38c 17304 attr = dwarf2_attr (die, DW_AT_encoding, cu);
c906108c
SS
17305 if (attr)
17306 {
17307 encoding = DW_UNSND (attr);
17308 }
e142c38c 17309 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
17310 if (attr)
17311 {
19f392bc 17312 bits = DW_UNSND (attr) * TARGET_CHAR_BIT;
c906108c 17313 }
39cbfefa 17314 name = dwarf2_name (die, cu);
6ccb9162 17315 if (!name)
c906108c 17316 {
6ccb9162
UW
17317 complaint (&symfile_complaints,
17318 _("DW_AT_name missing from DW_TAG_base_type"));
c906108c 17319 }
6ccb9162
UW
17320
17321 switch (encoding)
c906108c 17322 {
6ccb9162
UW
17323 case DW_ATE_address:
17324 /* Turn DW_ATE_address into a void * pointer. */
77b7c781 17325 type = init_type (objfile, TYPE_CODE_VOID, TARGET_CHAR_BIT, NULL);
19f392bc 17326 type = init_pointer_type (objfile, bits, name, type);
6ccb9162
UW
17327 break;
17328 case DW_ATE_boolean:
19f392bc 17329 type = init_boolean_type (objfile, bits, 1, name);
6ccb9162
UW
17330 break;
17331 case DW_ATE_complex_float:
9b790ce7 17332 type = dwarf2_init_float_type (objfile, bits / 2, NULL, name);
19f392bc 17333 type = init_complex_type (objfile, name, type);
6ccb9162
UW
17334 break;
17335 case DW_ATE_decimal_float:
19f392bc 17336 type = init_decfloat_type (objfile, bits, name);
6ccb9162
UW
17337 break;
17338 case DW_ATE_float:
9b790ce7 17339 type = dwarf2_init_float_type (objfile, bits, name, name);
6ccb9162
UW
17340 break;
17341 case DW_ATE_signed:
19f392bc 17342 type = init_integer_type (objfile, bits, 0, name);
6ccb9162
UW
17343 break;
17344 case DW_ATE_unsigned:
3b2b8fea
TT
17345 if (cu->language == language_fortran
17346 && name
61012eef 17347 && startswith (name, "character("))
19f392bc
UW
17348 type = init_character_type (objfile, bits, 1, name);
17349 else
17350 type = init_integer_type (objfile, bits, 1, name);
6ccb9162
UW
17351 break;
17352 case DW_ATE_signed_char:
6e70227d 17353 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea
TT
17354 || cu->language == language_pascal
17355 || cu->language == language_fortran)
19f392bc
UW
17356 type = init_character_type (objfile, bits, 0, name);
17357 else
17358 type = init_integer_type (objfile, bits, 0, name);
6ccb9162
UW
17359 break;
17360 case DW_ATE_unsigned_char:
868a0084 17361 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea 17362 || cu->language == language_pascal
c44af4eb
TT
17363 || cu->language == language_fortran
17364 || cu->language == language_rust)
19f392bc
UW
17365 type = init_character_type (objfile, bits, 1, name);
17366 else
17367 type = init_integer_type (objfile, bits, 1, name);
6ccb9162 17368 break;
75079b2b 17369 case DW_ATE_UTF:
53e710ac
PA
17370 {
17371 gdbarch *arch = get_objfile_arch (objfile);
17372
17373 if (bits == 16)
17374 type = builtin_type (arch)->builtin_char16;
17375 else if (bits == 32)
17376 type = builtin_type (arch)->builtin_char32;
17377 else
17378 {
17379 complaint (&symfile_complaints,
17380 _("unsupported DW_ATE_UTF bit size: '%d'"),
17381 bits);
17382 type = init_integer_type (objfile, bits, 1, name);
17383 }
17384 return set_die_type (die, type, cu);
17385 }
75079b2b
TT
17386 break;
17387
6ccb9162
UW
17388 default:
17389 complaint (&symfile_complaints, _("unsupported DW_AT_encoding: '%s'"),
17390 dwarf_type_encoding_name (encoding));
77b7c781 17391 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
6ccb9162 17392 break;
c906108c 17393 }
6ccb9162 17394
0114d602 17395 if (name && strcmp (name, "char") == 0)
876cecd0 17396 TYPE_NOSIGN (type) = 1;
0114d602 17397
f792889a 17398 return set_die_type (die, type, cu);
c906108c
SS
17399}
17400
80180f79
SA
17401/* Parse dwarf attribute if it's a block, reference or constant and put the
17402 resulting value of the attribute into struct bound_prop.
17403 Returns 1 if ATTR could be resolved into PROP, 0 otherwise. */
17404
17405static int
17406attr_to_dynamic_prop (const struct attribute *attr, struct die_info *die,
17407 struct dwarf2_cu *cu, struct dynamic_prop *prop)
17408{
17409 struct dwarf2_property_baton *baton;
518817b3
SM
17410 struct obstack *obstack
17411 = &cu->per_cu->dwarf2_per_objfile->objfile->objfile_obstack;
80180f79
SA
17412
17413 if (attr == NULL || prop == NULL)
17414 return 0;
17415
17416 if (attr_form_is_block (attr))
17417 {
8d749320 17418 baton = XOBNEW (obstack, struct dwarf2_property_baton);
80180f79
SA
17419 baton->referenced_type = NULL;
17420 baton->locexpr.per_cu = cu->per_cu;
17421 baton->locexpr.size = DW_BLOCK (attr)->size;
17422 baton->locexpr.data = DW_BLOCK (attr)->data;
17423 prop->data.baton = baton;
17424 prop->kind = PROP_LOCEXPR;
17425 gdb_assert (prop->data.baton != NULL);
17426 }
17427 else if (attr_form_is_ref (attr))
17428 {
17429 struct dwarf2_cu *target_cu = cu;
17430 struct die_info *target_die;
17431 struct attribute *target_attr;
17432
17433 target_die = follow_die_ref (die, attr, &target_cu);
17434 target_attr = dwarf2_attr (target_die, DW_AT_location, target_cu);
df25ebbd
JB
17435 if (target_attr == NULL)
17436 target_attr = dwarf2_attr (target_die, DW_AT_data_member_location,
17437 target_cu);
80180f79
SA
17438 if (target_attr == NULL)
17439 return 0;
17440
df25ebbd 17441 switch (target_attr->name)
80180f79 17442 {
df25ebbd
JB
17443 case DW_AT_location:
17444 if (attr_form_is_section_offset (target_attr))
17445 {
8d749320 17446 baton = XOBNEW (obstack, struct dwarf2_property_baton);
df25ebbd
JB
17447 baton->referenced_type = die_type (target_die, target_cu);
17448 fill_in_loclist_baton (cu, &baton->loclist, target_attr);
17449 prop->data.baton = baton;
17450 prop->kind = PROP_LOCLIST;
17451 gdb_assert (prop->data.baton != NULL);
17452 }
17453 else if (attr_form_is_block (target_attr))
17454 {
8d749320 17455 baton = XOBNEW (obstack, struct dwarf2_property_baton);
df25ebbd
JB
17456 baton->referenced_type = die_type (target_die, target_cu);
17457 baton->locexpr.per_cu = cu->per_cu;
17458 baton->locexpr.size = DW_BLOCK (target_attr)->size;
17459 baton->locexpr.data = DW_BLOCK (target_attr)->data;
17460 prop->data.baton = baton;
17461 prop->kind = PROP_LOCEXPR;
17462 gdb_assert (prop->data.baton != NULL);
17463 }
17464 else
17465 {
17466 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
17467 "dynamic property");
17468 return 0;
17469 }
17470 break;
17471 case DW_AT_data_member_location:
17472 {
17473 LONGEST offset;
17474
17475 if (!handle_data_member_location (target_die, target_cu,
17476 &offset))
17477 return 0;
17478
8d749320 17479 baton = XOBNEW (obstack, struct dwarf2_property_baton);
6ad395a7
JB
17480 baton->referenced_type = read_type_die (target_die->parent,
17481 target_cu);
df25ebbd
JB
17482 baton->offset_info.offset = offset;
17483 baton->offset_info.type = die_type (target_die, target_cu);
17484 prop->data.baton = baton;
17485 prop->kind = PROP_ADDR_OFFSET;
17486 break;
17487 }
80180f79
SA
17488 }
17489 }
17490 else if (attr_form_is_constant (attr))
17491 {
17492 prop->data.const_val = dwarf2_get_attr_constant_value (attr, 0);
17493 prop->kind = PROP_CONST;
17494 }
17495 else
17496 {
17497 dwarf2_invalid_attrib_class_complaint (dwarf_form_name (attr->form),
17498 dwarf2_name (die, cu));
17499 return 0;
17500 }
17501
17502 return 1;
17503}
17504
a02abb62
JB
17505/* Read the given DW_AT_subrange DIE. */
17506
f792889a 17507static struct type *
a02abb62
JB
17508read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
17509{
4c9ad8c2 17510 struct type *base_type, *orig_base_type;
a02abb62
JB
17511 struct type *range_type;
17512 struct attribute *attr;
729efb13 17513 struct dynamic_prop low, high;
4fae6e18 17514 int low_default_is_valid;
c451ebe5 17515 int high_bound_is_count = 0;
15d034d0 17516 const char *name;
43bbcdc2 17517 LONGEST negative_mask;
e77813c8 17518
4c9ad8c2
TT
17519 orig_base_type = die_type (die, cu);
17520 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
17521 whereas the real type might be. So, we use ORIG_BASE_TYPE when
17522 creating the range type, but we use the result of check_typedef
17523 when examining properties of the type. */
17524 base_type = check_typedef (orig_base_type);
a02abb62 17525
7e314c57
JK
17526 /* The die_type call above may have already set the type for this DIE. */
17527 range_type = get_die_type (die, cu);
17528 if (range_type)
17529 return range_type;
17530
729efb13
SA
17531 low.kind = PROP_CONST;
17532 high.kind = PROP_CONST;
17533 high.data.const_val = 0;
17534
4fae6e18
JK
17535 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
17536 omitting DW_AT_lower_bound. */
17537 switch (cu->language)
6e70227d 17538 {
4fae6e18
JK
17539 case language_c:
17540 case language_cplus:
729efb13 17541 low.data.const_val = 0;
4fae6e18
JK
17542 low_default_is_valid = 1;
17543 break;
17544 case language_fortran:
729efb13 17545 low.data.const_val = 1;
4fae6e18
JK
17546 low_default_is_valid = 1;
17547 break;
17548 case language_d:
4fae6e18 17549 case language_objc:
c44af4eb 17550 case language_rust:
729efb13 17551 low.data.const_val = 0;
4fae6e18
JK
17552 low_default_is_valid = (cu->header.version >= 4);
17553 break;
17554 case language_ada:
17555 case language_m2:
17556 case language_pascal:
729efb13 17557 low.data.const_val = 1;
4fae6e18
JK
17558 low_default_is_valid = (cu->header.version >= 4);
17559 break;
17560 default:
729efb13 17561 low.data.const_val = 0;
4fae6e18
JK
17562 low_default_is_valid = 0;
17563 break;
a02abb62
JB
17564 }
17565
e142c38c 17566 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
a02abb62 17567 if (attr)
11c1ba78 17568 attr_to_dynamic_prop (attr, die, cu, &low);
4fae6e18
JK
17569 else if (!low_default_is_valid)
17570 complaint (&symfile_complaints, _("Missing DW_AT_lower_bound "
9d8780f0
SM
17571 "- DIE at %s [in module %s]"),
17572 sect_offset_str (die->sect_off),
518817b3 17573 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
a02abb62 17574
e142c38c 17575 attr = dwarf2_attr (die, DW_AT_upper_bound, cu);
80180f79 17576 if (!attr_to_dynamic_prop (attr, die, cu, &high))
e77813c8
PM
17577 {
17578 attr = dwarf2_attr (die, DW_AT_count, cu);
c451ebe5 17579 if (attr_to_dynamic_prop (attr, die, cu, &high))
6b662e19 17580 {
c451ebe5
SA
17581 /* If bounds are constant do the final calculation here. */
17582 if (low.kind == PROP_CONST && high.kind == PROP_CONST)
17583 high.data.const_val = low.data.const_val + high.data.const_val - 1;
17584 else
17585 high_bound_is_count = 1;
c2ff108b 17586 }
e77813c8
PM
17587 }
17588
17589 /* Dwarf-2 specifications explicitly allows to create subrange types
17590 without specifying a base type.
17591 In that case, the base type must be set to the type of
17592 the lower bound, upper bound or count, in that order, if any of these
17593 three attributes references an object that has a type.
17594 If no base type is found, the Dwarf-2 specifications say that
17595 a signed integer type of size equal to the size of an address should
17596 be used.
17597 For the following C code: `extern char gdb_int [];'
17598 GCC produces an empty range DIE.
17599 FIXME: muller/2010-05-28: Possible references to object for low bound,
0963b4bd 17600 high bound or count are not yet handled by this code. */
e77813c8
PM
17601 if (TYPE_CODE (base_type) == TYPE_CODE_VOID)
17602 {
518817b3 17603 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
e77813c8
PM
17604 struct gdbarch *gdbarch = get_objfile_arch (objfile);
17605 int addr_size = gdbarch_addr_bit (gdbarch) /8;
17606 struct type *int_type = objfile_type (objfile)->builtin_int;
17607
17608 /* Test "int", "long int", and "long long int" objfile types,
17609 and select the first one having a size above or equal to the
17610 architecture address size. */
17611 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
17612 base_type = int_type;
17613 else
17614 {
17615 int_type = objfile_type (objfile)->builtin_long;
17616 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
17617 base_type = int_type;
17618 else
17619 {
17620 int_type = objfile_type (objfile)->builtin_long_long;
17621 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
17622 base_type = int_type;
17623 }
17624 }
17625 }
a02abb62 17626
dbb9c2b1
JB
17627 /* Normally, the DWARF producers are expected to use a signed
17628 constant form (Eg. DW_FORM_sdata) to express negative bounds.
17629 But this is unfortunately not always the case, as witnessed
17630 with GCC, for instance, where the ambiguous DW_FORM_dataN form
17631 is used instead. To work around that ambiguity, we treat
17632 the bounds as signed, and thus sign-extend their values, when
17633 the base type is signed. */
6e70227d 17634 negative_mask =
66c6502d 17635 -((LONGEST) 1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1));
729efb13
SA
17636 if (low.kind == PROP_CONST
17637 && !TYPE_UNSIGNED (base_type) && (low.data.const_val & negative_mask))
17638 low.data.const_val |= negative_mask;
17639 if (high.kind == PROP_CONST
17640 && !TYPE_UNSIGNED (base_type) && (high.data.const_val & negative_mask))
17641 high.data.const_val |= negative_mask;
43bbcdc2 17642
729efb13 17643 range_type = create_range_type (NULL, orig_base_type, &low, &high);
a02abb62 17644
c451ebe5
SA
17645 if (high_bound_is_count)
17646 TYPE_RANGE_DATA (range_type)->flag_upper_bound_is_count = 1;
17647
c2ff108b
JK
17648 /* Ada expects an empty array on no boundary attributes. */
17649 if (attr == NULL && cu->language != language_ada)
729efb13 17650 TYPE_HIGH_BOUND_KIND (range_type) = PROP_UNDEFINED;
c2ff108b 17651
39cbfefa
DJ
17652 name = dwarf2_name (die, cu);
17653 if (name)
17654 TYPE_NAME (range_type) = name;
6e70227d 17655
e142c38c 17656 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
a02abb62
JB
17657 if (attr)
17658 TYPE_LENGTH (range_type) = DW_UNSND (attr);
17659
7e314c57
JK
17660 set_die_type (die, range_type, cu);
17661
17662 /* set_die_type should be already done. */
b4ba55a1
JB
17663 set_descriptive_type (range_type, die, cu);
17664
7e314c57 17665 return range_type;
a02abb62 17666}
6e70227d 17667
f792889a 17668static struct type *
81a17f79
JB
17669read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
17670{
17671 struct type *type;
81a17f79 17672
518817b3
SM
17673 type = init_type (cu->per_cu->dwarf2_per_objfile->objfile, TYPE_CODE_VOID,0,
17674 NULL);
0114d602 17675 TYPE_NAME (type) = dwarf2_name (die, cu);
81a17f79 17676
74a2f8ff
JB
17677 /* In Ada, an unspecified type is typically used when the description
17678 of the type is defered to a different unit. When encountering
17679 such a type, we treat it as a stub, and try to resolve it later on,
17680 when needed. */
17681 if (cu->language == language_ada)
17682 TYPE_STUB (type) = 1;
17683
f792889a 17684 return set_die_type (die, type, cu);
81a17f79 17685}
a02abb62 17686
639d11d3
DC
17687/* Read a single die and all its descendents. Set the die's sibling
17688 field to NULL; set other fields in the die correctly, and set all
17689 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
17690 location of the info_ptr after reading all of those dies. PARENT
17691 is the parent of the die in question. */
17692
17693static struct die_info *
dee91e82 17694read_die_and_children (const struct die_reader_specs *reader,
d521ce57
TT
17695 const gdb_byte *info_ptr,
17696 const gdb_byte **new_info_ptr,
dee91e82 17697 struct die_info *parent)
639d11d3
DC
17698{
17699 struct die_info *die;
d521ce57 17700 const gdb_byte *cur_ptr;
639d11d3
DC
17701 int has_children;
17702
bf6af496 17703 cur_ptr = read_full_die_1 (reader, &die, info_ptr, &has_children, 0);
1d325ec1
DJ
17704 if (die == NULL)
17705 {
17706 *new_info_ptr = cur_ptr;
17707 return NULL;
17708 }
93311388 17709 store_in_ref_table (die, reader->cu);
639d11d3
DC
17710
17711 if (has_children)
bf6af496 17712 die->child = read_die_and_siblings_1 (reader, cur_ptr, new_info_ptr, die);
639d11d3
DC
17713 else
17714 {
17715 die->child = NULL;
17716 *new_info_ptr = cur_ptr;
17717 }
17718
17719 die->sibling = NULL;
17720 die->parent = parent;
17721 return die;
17722}
17723
17724/* Read a die, all of its descendents, and all of its siblings; set
17725 all of the fields of all of the dies correctly. Arguments are as
17726 in read_die_and_children. */
17727
17728static struct die_info *
bf6af496 17729read_die_and_siblings_1 (const struct die_reader_specs *reader,
d521ce57
TT
17730 const gdb_byte *info_ptr,
17731 const gdb_byte **new_info_ptr,
bf6af496 17732 struct die_info *parent)
639d11d3
DC
17733{
17734 struct die_info *first_die, *last_sibling;
d521ce57 17735 const gdb_byte *cur_ptr;
639d11d3 17736
c906108c 17737 cur_ptr = info_ptr;
639d11d3
DC
17738 first_die = last_sibling = NULL;
17739
17740 while (1)
c906108c 17741 {
639d11d3 17742 struct die_info *die
dee91e82 17743 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
639d11d3 17744
1d325ec1 17745 if (die == NULL)
c906108c 17746 {
639d11d3
DC
17747 *new_info_ptr = cur_ptr;
17748 return first_die;
c906108c 17749 }
1d325ec1
DJ
17750
17751 if (!first_die)
17752 first_die = die;
c906108c 17753 else
1d325ec1
DJ
17754 last_sibling->sibling = die;
17755
17756 last_sibling = die;
c906108c 17757 }
c906108c
SS
17758}
17759
bf6af496
DE
17760/* Read a die, all of its descendents, and all of its siblings; set
17761 all of the fields of all of the dies correctly. Arguments are as
17762 in read_die_and_children.
17763 This the main entry point for reading a DIE and all its children. */
17764
17765static struct die_info *
17766read_die_and_siblings (const struct die_reader_specs *reader,
d521ce57
TT
17767 const gdb_byte *info_ptr,
17768 const gdb_byte **new_info_ptr,
bf6af496
DE
17769 struct die_info *parent)
17770{
17771 struct die_info *die = read_die_and_siblings_1 (reader, info_ptr,
17772 new_info_ptr, parent);
17773
b4f54984 17774 if (dwarf_die_debug)
bf6af496
DE
17775 {
17776 fprintf_unfiltered (gdb_stdlog,
17777 "Read die from %s@0x%x of %s:\n",
a32a8923 17778 get_section_name (reader->die_section),
bf6af496
DE
17779 (unsigned) (info_ptr - reader->die_section->buffer),
17780 bfd_get_filename (reader->abfd));
b4f54984 17781 dump_die (die, dwarf_die_debug);
bf6af496
DE
17782 }
17783
17784 return die;
17785}
17786
3019eac3
DE
17787/* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
17788 attributes.
17789 The caller is responsible for filling in the extra attributes
17790 and updating (*DIEP)->num_attrs.
17791 Set DIEP to point to a newly allocated die with its information,
17792 except for its child, sibling, and parent fields.
17793 Set HAS_CHILDREN to tell whether the die has children or not. */
93311388 17794
d521ce57 17795static const gdb_byte *
3019eac3 17796read_full_die_1 (const struct die_reader_specs *reader,
d521ce57 17797 struct die_info **diep, const gdb_byte *info_ptr,
3019eac3 17798 int *has_children, int num_extra_attrs)
93311388 17799{
b64f50a1 17800 unsigned int abbrev_number, bytes_read, i;
93311388
DE
17801 struct abbrev_info *abbrev;
17802 struct die_info *die;
17803 struct dwarf2_cu *cu = reader->cu;
17804 bfd *abfd = reader->abfd;
17805
9c541725 17806 sect_offset sect_off = (sect_offset) (info_ptr - reader->buffer);
93311388
DE
17807 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
17808 info_ptr += bytes_read;
17809 if (!abbrev_number)
17810 {
17811 *diep = NULL;
17812 *has_children = 0;
17813 return info_ptr;
17814 }
17815
685af9cd 17816 abbrev = reader->abbrev_table->lookup_abbrev (abbrev_number);
93311388 17817 if (!abbrev)
348e048f
DE
17818 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
17819 abbrev_number,
17820 bfd_get_filename (abfd));
17821
3019eac3 17822 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
9c541725 17823 die->sect_off = sect_off;
93311388
DE
17824 die->tag = abbrev->tag;
17825 die->abbrev = abbrev_number;
17826
3019eac3
DE
17827 /* Make the result usable.
17828 The caller needs to update num_attrs after adding the extra
17829 attributes. */
93311388
DE
17830 die->num_attrs = abbrev->num_attrs;
17831
17832 for (i = 0; i < abbrev->num_attrs; ++i)
dee91e82
DE
17833 info_ptr = read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
17834 info_ptr);
93311388
DE
17835
17836 *diep = die;
17837 *has_children = abbrev->has_children;
17838 return info_ptr;
17839}
17840
3019eac3
DE
17841/* Read a die and all its attributes.
17842 Set DIEP to point to a newly allocated die with its information,
17843 except for its child, sibling, and parent fields.
17844 Set HAS_CHILDREN to tell whether the die has children or not. */
17845
d521ce57 17846static const gdb_byte *
3019eac3 17847read_full_die (const struct die_reader_specs *reader,
d521ce57 17848 struct die_info **diep, const gdb_byte *info_ptr,
3019eac3
DE
17849 int *has_children)
17850{
d521ce57 17851 const gdb_byte *result;
bf6af496
DE
17852
17853 result = read_full_die_1 (reader, diep, info_ptr, has_children, 0);
17854
b4f54984 17855 if (dwarf_die_debug)
bf6af496
DE
17856 {
17857 fprintf_unfiltered (gdb_stdlog,
17858 "Read die from %s@0x%x of %s:\n",
a32a8923 17859 get_section_name (reader->die_section),
bf6af496
DE
17860 (unsigned) (info_ptr - reader->die_section->buffer),
17861 bfd_get_filename (reader->abfd));
b4f54984 17862 dump_die (*diep, dwarf_die_debug);
bf6af496
DE
17863 }
17864
17865 return result;
3019eac3 17866}
433df2d4
DE
17867\f
17868/* Abbreviation tables.
3019eac3 17869
433df2d4 17870 In DWARF version 2, the description of the debugging information is
c906108c
SS
17871 stored in a separate .debug_abbrev section. Before we read any
17872 dies from a section we read in all abbreviations and install them
433df2d4
DE
17873 in a hash table. */
17874
17875/* Allocate space for a struct abbrev_info object in ABBREV_TABLE. */
17876
685af9cd
TT
17877struct abbrev_info *
17878abbrev_table::alloc_abbrev ()
433df2d4
DE
17879{
17880 struct abbrev_info *abbrev;
17881
685af9cd 17882 abbrev = XOBNEW (&abbrev_obstack, struct abbrev_info);
433df2d4 17883 memset (abbrev, 0, sizeof (struct abbrev_info));
8d749320 17884
433df2d4
DE
17885 return abbrev;
17886}
17887
17888/* Add an abbreviation to the table. */
c906108c 17889
685af9cd
TT
17890void
17891abbrev_table::add_abbrev (unsigned int abbrev_number,
17892 struct abbrev_info *abbrev)
433df2d4
DE
17893{
17894 unsigned int hash_number;
17895
17896 hash_number = abbrev_number % ABBREV_HASH_SIZE;
4a17f768
YQ
17897 abbrev->next = m_abbrevs[hash_number];
17898 m_abbrevs[hash_number] = abbrev;
433df2d4 17899}
dee91e82 17900
433df2d4
DE
17901/* Look up an abbrev in the table.
17902 Returns NULL if the abbrev is not found. */
17903
685af9cd
TT
17904struct abbrev_info *
17905abbrev_table::lookup_abbrev (unsigned int abbrev_number)
c906108c 17906{
433df2d4
DE
17907 unsigned int hash_number;
17908 struct abbrev_info *abbrev;
17909
17910 hash_number = abbrev_number % ABBREV_HASH_SIZE;
4a17f768 17911 abbrev = m_abbrevs[hash_number];
433df2d4
DE
17912
17913 while (abbrev)
17914 {
17915 if (abbrev->number == abbrev_number)
17916 return abbrev;
17917 abbrev = abbrev->next;
17918 }
17919 return NULL;
17920}
17921
17922/* Read in an abbrev table. */
17923
685af9cd 17924static abbrev_table_up
ed2dc618
SM
17925abbrev_table_read_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
17926 struct dwarf2_section_info *section,
9c541725 17927 sect_offset sect_off)
433df2d4
DE
17928{
17929 struct objfile *objfile = dwarf2_per_objfile->objfile;
a32a8923 17930 bfd *abfd = get_section_bfd_owner (section);
d521ce57 17931 const gdb_byte *abbrev_ptr;
c906108c
SS
17932 struct abbrev_info *cur_abbrev;
17933 unsigned int abbrev_number, bytes_read, abbrev_name;
433df2d4 17934 unsigned int abbrev_form;
f3dd6933
DJ
17935 struct attr_abbrev *cur_attrs;
17936 unsigned int allocated_attrs;
c906108c 17937
685af9cd 17938 abbrev_table_up abbrev_table (new struct abbrev_table (sect_off));
c906108c 17939
433df2d4 17940 dwarf2_read_section (objfile, section);
9c541725 17941 abbrev_ptr = section->buffer + to_underlying (sect_off);
c906108c
SS
17942 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
17943 abbrev_ptr += bytes_read;
17944
f3dd6933 17945 allocated_attrs = ATTR_ALLOC_CHUNK;
8d749320 17946 cur_attrs = XNEWVEC (struct attr_abbrev, allocated_attrs);
6e70227d 17947
0963b4bd 17948 /* Loop until we reach an abbrev number of 0. */
c906108c
SS
17949 while (abbrev_number)
17950 {
685af9cd 17951 cur_abbrev = abbrev_table->alloc_abbrev ();
c906108c
SS
17952
17953 /* read in abbrev header */
17954 cur_abbrev->number = abbrev_number;
aead7601
SM
17955 cur_abbrev->tag
17956 = (enum dwarf_tag) read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
c906108c
SS
17957 abbrev_ptr += bytes_read;
17958 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
17959 abbrev_ptr += 1;
17960
17961 /* now read in declarations */
22d2f3ab 17962 for (;;)
c906108c 17963 {
43988095
JK
17964 LONGEST implicit_const;
17965
22d2f3ab
JK
17966 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
17967 abbrev_ptr += bytes_read;
17968 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
17969 abbrev_ptr += bytes_read;
43988095
JK
17970 if (abbrev_form == DW_FORM_implicit_const)
17971 {
17972 implicit_const = read_signed_leb128 (abfd, abbrev_ptr,
17973 &bytes_read);
17974 abbrev_ptr += bytes_read;
17975 }
17976 else
17977 {
17978 /* Initialize it due to a false compiler warning. */
17979 implicit_const = -1;
17980 }
22d2f3ab
JK
17981
17982 if (abbrev_name == 0)
17983 break;
17984
f3dd6933 17985 if (cur_abbrev->num_attrs == allocated_attrs)
c906108c 17986 {
f3dd6933
DJ
17987 allocated_attrs += ATTR_ALLOC_CHUNK;
17988 cur_attrs
224c3ddb 17989 = XRESIZEVEC (struct attr_abbrev, cur_attrs, allocated_attrs);
c906108c 17990 }
ae038cb0 17991
aead7601
SM
17992 cur_attrs[cur_abbrev->num_attrs].name
17993 = (enum dwarf_attribute) abbrev_name;
22d2f3ab 17994 cur_attrs[cur_abbrev->num_attrs].form
aead7601 17995 = (enum dwarf_form) abbrev_form;
43988095 17996 cur_attrs[cur_abbrev->num_attrs].implicit_const = implicit_const;
22d2f3ab 17997 ++cur_abbrev->num_attrs;
c906108c
SS
17998 }
17999
8d749320
SM
18000 cur_abbrev->attrs =
18001 XOBNEWVEC (&abbrev_table->abbrev_obstack, struct attr_abbrev,
18002 cur_abbrev->num_attrs);
f3dd6933
DJ
18003 memcpy (cur_abbrev->attrs, cur_attrs,
18004 cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
18005
685af9cd 18006 abbrev_table->add_abbrev (abbrev_number, cur_abbrev);
c906108c
SS
18007
18008 /* Get next abbreviation.
18009 Under Irix6 the abbreviations for a compilation unit are not
c5aa993b
JM
18010 always properly terminated with an abbrev number of 0.
18011 Exit loop if we encounter an abbreviation which we have
18012 already read (which means we are about to read the abbreviations
18013 for the next compile unit) or if the end of the abbreviation
18014 table is reached. */
433df2d4 18015 if ((unsigned int) (abbrev_ptr - section->buffer) >= section->size)
c906108c
SS
18016 break;
18017 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18018 abbrev_ptr += bytes_read;
685af9cd 18019 if (abbrev_table->lookup_abbrev (abbrev_number) != NULL)
c906108c
SS
18020 break;
18021 }
f3dd6933
DJ
18022
18023 xfree (cur_attrs);
433df2d4 18024 return abbrev_table;
c906108c
SS
18025}
18026
72bf9492
DJ
18027/* Returns nonzero if TAG represents a type that we might generate a partial
18028 symbol for. */
18029
18030static int
18031is_type_tag_for_partial (int tag)
18032{
18033 switch (tag)
18034 {
18035#if 0
18036 /* Some types that would be reasonable to generate partial symbols for,
18037 that we don't at present. */
18038 case DW_TAG_array_type:
18039 case DW_TAG_file_type:
18040 case DW_TAG_ptr_to_member_type:
18041 case DW_TAG_set_type:
18042 case DW_TAG_string_type:
18043 case DW_TAG_subroutine_type:
18044#endif
18045 case DW_TAG_base_type:
18046 case DW_TAG_class_type:
680b30c7 18047 case DW_TAG_interface_type:
72bf9492
DJ
18048 case DW_TAG_enumeration_type:
18049 case DW_TAG_structure_type:
18050 case DW_TAG_subrange_type:
18051 case DW_TAG_typedef:
18052 case DW_TAG_union_type:
18053 return 1;
18054 default:
18055 return 0;
18056 }
18057}
18058
18059/* Load all DIEs that are interesting for partial symbols into memory. */
18060
18061static struct partial_die_info *
dee91e82 18062load_partial_dies (const struct die_reader_specs *reader,
d521ce57 18063 const gdb_byte *info_ptr, int building_psymtab)
72bf9492 18064{
dee91e82 18065 struct dwarf2_cu *cu = reader->cu;
518817b3 18066 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
72bf9492 18067 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
72bf9492 18068 unsigned int bytes_read;
5afb4e99 18069 unsigned int load_all = 0;
72bf9492
DJ
18070 int nesting_level = 1;
18071
18072 parent_die = NULL;
18073 last_die = NULL;
18074
7adf1e79
DE
18075 gdb_assert (cu->per_cu != NULL);
18076 if (cu->per_cu->load_all_dies)
5afb4e99
DJ
18077 load_all = 1;
18078
72bf9492
DJ
18079 cu->partial_dies
18080 = htab_create_alloc_ex (cu->header.length / 12,
18081 partial_die_hash,
18082 partial_die_eq,
18083 NULL,
18084 &cu->comp_unit_obstack,
18085 hashtab_obstack_allocate,
18086 dummy_obstack_deallocate);
18087
72bf9492
DJ
18088 while (1)
18089 {
685af9cd 18090 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
72bf9492
DJ
18091
18092 /* A NULL abbrev means the end of a series of children. */
18093 if (abbrev == NULL)
18094 {
18095 if (--nesting_level == 0)
cd9983dd
YQ
18096 return first_die;
18097
72bf9492
DJ
18098 info_ptr += bytes_read;
18099 last_die = parent_die;
18100 parent_die = parent_die->die_parent;
18101 continue;
18102 }
18103
98bfdba5
PA
18104 /* Check for template arguments. We never save these; if
18105 they're seen, we just mark the parent, and go on our way. */
18106 if (parent_die != NULL
18107 && cu->language == language_cplus
18108 && (abbrev->tag == DW_TAG_template_type_param
18109 || abbrev->tag == DW_TAG_template_value_param))
18110 {
18111 parent_die->has_template_arguments = 1;
18112
18113 if (!load_all)
18114 {
18115 /* We don't need a partial DIE for the template argument. */
dee91e82 18116 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
98bfdba5
PA
18117 continue;
18118 }
18119 }
18120
0d99eb77 18121 /* We only recurse into c++ subprograms looking for template arguments.
98bfdba5
PA
18122 Skip their other children. */
18123 if (!load_all
18124 && cu->language == language_cplus
18125 && parent_die != NULL
18126 && parent_die->tag == DW_TAG_subprogram)
18127 {
dee91e82 18128 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
98bfdba5
PA
18129 continue;
18130 }
18131
5afb4e99
DJ
18132 /* Check whether this DIE is interesting enough to save. Normally
18133 we would not be interested in members here, but there may be
18134 later variables referencing them via DW_AT_specification (for
18135 static members). */
18136 if (!load_all
18137 && !is_type_tag_for_partial (abbrev->tag)
72929c62 18138 && abbrev->tag != DW_TAG_constant
72bf9492
DJ
18139 && abbrev->tag != DW_TAG_enumerator
18140 && abbrev->tag != DW_TAG_subprogram
b1dc1806 18141 && abbrev->tag != DW_TAG_inlined_subroutine
bc30ff58 18142 && abbrev->tag != DW_TAG_lexical_block
72bf9492 18143 && abbrev->tag != DW_TAG_variable
5afb4e99 18144 && abbrev->tag != DW_TAG_namespace
f55ee35c 18145 && abbrev->tag != DW_TAG_module
95554aad 18146 && abbrev->tag != DW_TAG_member
74921315
KS
18147 && abbrev->tag != DW_TAG_imported_unit
18148 && abbrev->tag != DW_TAG_imported_declaration)
72bf9492
DJ
18149 {
18150 /* Otherwise we skip to the next sibling, if any. */
dee91e82 18151 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
72bf9492
DJ
18152 continue;
18153 }
18154
6f06d47b
YQ
18155 struct partial_die_info pdi ((sect_offset) (info_ptr - reader->buffer),
18156 abbrev);
cd9983dd 18157
48fbe735 18158 info_ptr = pdi.read (reader, *abbrev, info_ptr + bytes_read);
72bf9492
DJ
18159
18160 /* This two-pass algorithm for processing partial symbols has a
18161 high cost in cache pressure. Thus, handle some simple cases
18162 here which cover the majority of C partial symbols. DIEs
18163 which neither have specification tags in them, nor could have
18164 specification tags elsewhere pointing at them, can simply be
18165 processed and discarded.
18166
18167 This segment is also optional; scan_partial_symbols and
18168 add_partial_symbol will handle these DIEs if we chain
18169 them in normally. When compilers which do not emit large
18170 quantities of duplicate debug information are more common,
18171 this code can probably be removed. */
18172
18173 /* Any complete simple types at the top level (pretty much all
18174 of them, for a language without namespaces), can be processed
18175 directly. */
18176 if (parent_die == NULL
cd9983dd
YQ
18177 && pdi.has_specification == 0
18178 && pdi.is_declaration == 0
18179 && ((pdi.tag == DW_TAG_typedef && !pdi.has_children)
18180 || pdi.tag == DW_TAG_base_type
18181 || pdi.tag == DW_TAG_subrange_type))
72bf9492 18182 {
cd9983dd
YQ
18183 if (building_psymtab && pdi.name != NULL)
18184 add_psymbol_to_list (pdi.name, strlen (pdi.name), 0,
72bf9492 18185 VAR_DOMAIN, LOC_TYPEDEF,
bb5ed363 18186 &objfile->static_psymbols,
1762568f 18187 0, cu->language, objfile);
cd9983dd 18188 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
72bf9492
DJ
18189 continue;
18190 }
18191
d8228535
JK
18192 /* The exception for DW_TAG_typedef with has_children above is
18193 a workaround of GCC PR debug/47510. In the case of this complaint
18194 type_name_no_tag_or_error will error on such types later.
18195
18196 GDB skipped children of DW_TAG_typedef by the shortcut above and then
18197 it could not find the child DIEs referenced later, this is checked
18198 above. In correct DWARF DW_TAG_typedef should have no children. */
18199
cd9983dd 18200 if (pdi.tag == DW_TAG_typedef && pdi.has_children)
d8228535
JK
18201 complaint (&symfile_complaints,
18202 _("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
9d8780f0 18203 "- DIE at %s [in module %s]"),
cd9983dd 18204 sect_offset_str (pdi.sect_off), objfile_name (objfile));
d8228535 18205
72bf9492
DJ
18206 /* If we're at the second level, and we're an enumerator, and
18207 our parent has no specification (meaning possibly lives in a
18208 namespace elsewhere), then we can add the partial symbol now
18209 instead of queueing it. */
cd9983dd 18210 if (pdi.tag == DW_TAG_enumerator
72bf9492
DJ
18211 && parent_die != NULL
18212 && parent_die->die_parent == NULL
18213 && parent_die->tag == DW_TAG_enumeration_type
18214 && parent_die->has_specification == 0)
18215 {
cd9983dd 18216 if (pdi.name == NULL)
3e43a32a
MS
18217 complaint (&symfile_complaints,
18218 _("malformed enumerator DIE ignored"));
72bf9492 18219 else if (building_psymtab)
cd9983dd 18220 add_psymbol_to_list (pdi.name, strlen (pdi.name), 0,
72bf9492 18221 VAR_DOMAIN, LOC_CONST,
9c37b5ae 18222 cu->language == language_cplus
bb5ed363
DE
18223 ? &objfile->global_psymbols
18224 : &objfile->static_psymbols,
1762568f 18225 0, cu->language, objfile);
72bf9492 18226
cd9983dd 18227 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
72bf9492
DJ
18228 continue;
18229 }
18230
cd9983dd 18231 struct partial_die_info *part_die
6f06d47b 18232 = new (&cu->comp_unit_obstack) partial_die_info (pdi);
cd9983dd 18233
72bf9492
DJ
18234 /* We'll save this DIE so link it in. */
18235 part_die->die_parent = parent_die;
18236 part_die->die_sibling = NULL;
18237 part_die->die_child = NULL;
18238
18239 if (last_die && last_die == parent_die)
18240 last_die->die_child = part_die;
18241 else if (last_die)
18242 last_die->die_sibling = part_die;
18243
18244 last_die = part_die;
18245
18246 if (first_die == NULL)
18247 first_die = part_die;
18248
18249 /* Maybe add the DIE to the hash table. Not all DIEs that we
18250 find interesting need to be in the hash table, because we
18251 also have the parent/sibling/child chains; only those that we
18252 might refer to by offset later during partial symbol reading.
18253
18254 For now this means things that might have be the target of a
18255 DW_AT_specification, DW_AT_abstract_origin, or
18256 DW_AT_extension. DW_AT_extension will refer only to
18257 namespaces; DW_AT_abstract_origin refers to functions (and
18258 many things under the function DIE, but we do not recurse
18259 into function DIEs during partial symbol reading) and
18260 possibly variables as well; DW_AT_specification refers to
18261 declarations. Declarations ought to have the DW_AT_declaration
18262 flag. It happens that GCC forgets to put it in sometimes, but
18263 only for functions, not for types.
18264
18265 Adding more things than necessary to the hash table is harmless
18266 except for the performance cost. Adding too few will result in
5afb4e99
DJ
18267 wasted time in find_partial_die, when we reread the compilation
18268 unit with load_all_dies set. */
72bf9492 18269
5afb4e99 18270 if (load_all
72929c62 18271 || abbrev->tag == DW_TAG_constant
5afb4e99 18272 || abbrev->tag == DW_TAG_subprogram
72bf9492
DJ
18273 || abbrev->tag == DW_TAG_variable
18274 || abbrev->tag == DW_TAG_namespace
18275 || part_die->is_declaration)
18276 {
18277 void **slot;
18278
18279 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
9c541725
PA
18280 to_underlying (part_die->sect_off),
18281 INSERT);
72bf9492
DJ
18282 *slot = part_die;
18283 }
18284
72bf9492 18285 /* For some DIEs we want to follow their children (if any). For C
bc30ff58 18286 we have no reason to follow the children of structures; for other
98bfdba5
PA
18287 languages we have to, so that we can get at method physnames
18288 to infer fully qualified class names, for DW_AT_specification,
18289 and for C++ template arguments. For C++, we also look one level
18290 inside functions to find template arguments (if the name of the
18291 function does not already contain the template arguments).
bc30ff58
JB
18292
18293 For Ada, we need to scan the children of subprograms and lexical
18294 blocks as well because Ada allows the definition of nested
18295 entities that could be interesting for the debugger, such as
18296 nested subprograms for instance. */
72bf9492 18297 if (last_die->has_children
5afb4e99
DJ
18298 && (load_all
18299 || last_die->tag == DW_TAG_namespace
f55ee35c 18300 || last_die->tag == DW_TAG_module
72bf9492 18301 || last_die->tag == DW_TAG_enumeration_type
98bfdba5
PA
18302 || (cu->language == language_cplus
18303 && last_die->tag == DW_TAG_subprogram
18304 && (last_die->name == NULL
18305 || strchr (last_die->name, '<') == NULL))
72bf9492
DJ
18306 || (cu->language != language_c
18307 && (last_die->tag == DW_TAG_class_type
680b30c7 18308 || last_die->tag == DW_TAG_interface_type
72bf9492 18309 || last_die->tag == DW_TAG_structure_type
bc30ff58
JB
18310 || last_die->tag == DW_TAG_union_type))
18311 || (cu->language == language_ada
18312 && (last_die->tag == DW_TAG_subprogram
18313 || last_die->tag == DW_TAG_lexical_block))))
72bf9492
DJ
18314 {
18315 nesting_level++;
18316 parent_die = last_die;
18317 continue;
18318 }
18319
18320 /* Otherwise we skip to the next sibling, if any. */
dee91e82 18321 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
72bf9492
DJ
18322
18323 /* Back to the top, do it again. */
18324 }
18325}
18326
6f06d47b
YQ
18327partial_die_info::partial_die_info (sect_offset sect_off_,
18328 struct abbrev_info *abbrev)
18329 : partial_die_info (sect_off_, abbrev->tag, abbrev->has_children)
18330{
18331}
18332
35cc7ed7
YQ
18333/* Read a minimal amount of information into the minimal die structure.
18334 INFO_PTR should point just after the initial uleb128 of a DIE. */
c906108c 18335
48fbe735
YQ
18336const gdb_byte *
18337partial_die_info::read (const struct die_reader_specs *reader,
18338 const struct abbrev_info &abbrev, const gdb_byte *info_ptr)
c906108c 18339{
dee91e82 18340 struct dwarf2_cu *cu = reader->cu;
518817b3
SM
18341 struct dwarf2_per_objfile *dwarf2_per_objfile
18342 = cu->per_cu->dwarf2_per_objfile;
fa238c03 18343 unsigned int i;
c5aa993b 18344 int has_low_pc_attr = 0;
c906108c 18345 int has_high_pc_attr = 0;
91da1414 18346 int high_pc_relative = 0;
c906108c 18347
fd0a254f 18348 for (i = 0; i < abbrev.num_attrs; ++i)
c906108c 18349 {
48fbe735
YQ
18350 struct attribute attr;
18351
fd0a254f 18352 info_ptr = read_attribute (reader, &attr, &abbrev.attrs[i], info_ptr);
c906108c
SS
18353
18354 /* Store the data if it is of an attribute we want to keep in a
c5aa993b 18355 partial symbol table. */
c906108c
SS
18356 switch (attr.name)
18357 {
18358 case DW_AT_name:
48fbe735 18359 switch (tag)
71c25dea
TT
18360 {
18361 case DW_TAG_compile_unit:
95554aad 18362 case DW_TAG_partial_unit:
348e048f 18363 case DW_TAG_type_unit:
71c25dea
TT
18364 /* Compilation units have a DW_AT_name that is a filename, not
18365 a source language identifier. */
18366 case DW_TAG_enumeration_type:
18367 case DW_TAG_enumerator:
18368 /* These tags always have simple identifiers already; no need
18369 to canonicalize them. */
48fbe735 18370 name = DW_STRING (&attr);
71c25dea
TT
18371 break;
18372 default:
48fbe735
YQ
18373 {
18374 struct objfile *objfile = dwarf2_per_objfile->objfile;
18375
18376 name
18377 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
18378 &objfile->per_bfd->storage_obstack);
18379 }
71c25dea
TT
18380 break;
18381 }
c906108c 18382 break;
31ef98ae 18383 case DW_AT_linkage_name:
c906108c 18384 case DW_AT_MIPS_linkage_name:
31ef98ae
TT
18385 /* Note that both forms of linkage name might appear. We
18386 assume they will be the same, and we only store the last
18387 one we see. */
94af9270 18388 if (cu->language == language_ada)
48fbe735
YQ
18389 name = DW_STRING (&attr);
18390 linkage_name = DW_STRING (&attr);
c906108c
SS
18391 break;
18392 case DW_AT_low_pc:
18393 has_low_pc_attr = 1;
48fbe735 18394 lowpc = attr_value_as_address (&attr);
c906108c
SS
18395 break;
18396 case DW_AT_high_pc:
18397 has_high_pc_attr = 1;
48fbe735 18398 highpc = attr_value_as_address (&attr);
31aa7e4e
JB
18399 if (cu->header.version >= 4 && attr_form_is_constant (&attr))
18400 high_pc_relative = 1;
c906108c
SS
18401 break;
18402 case DW_AT_location:
0963b4bd 18403 /* Support the .debug_loc offsets. */
8e19ed76
PS
18404 if (attr_form_is_block (&attr))
18405 {
48fbe735 18406 d.locdesc = DW_BLOCK (&attr);
8e19ed76 18407 }
3690dd37 18408 else if (attr_form_is_section_offset (&attr))
8e19ed76 18409 {
4d3c2250 18410 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
18411 }
18412 else
18413 {
4d3c2250
KB
18414 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
18415 "partial symbol information");
8e19ed76 18416 }
c906108c 18417 break;
c906108c 18418 case DW_AT_external:
48fbe735 18419 is_external = DW_UNSND (&attr);
c906108c
SS
18420 break;
18421 case DW_AT_declaration:
48fbe735 18422 is_declaration = DW_UNSND (&attr);
c906108c
SS
18423 break;
18424 case DW_AT_type:
48fbe735 18425 has_type = 1;
c906108c
SS
18426 break;
18427 case DW_AT_abstract_origin:
18428 case DW_AT_specification:
72bf9492 18429 case DW_AT_extension:
48fbe735
YQ
18430 has_specification = 1;
18431 spec_offset = dwarf2_get_ref_die_offset (&attr);
18432 spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
36586728 18433 || cu->per_cu->is_dwz);
c906108c
SS
18434 break;
18435 case DW_AT_sibling:
18436 /* Ignore absolute siblings, they might point outside of
18437 the current compile unit. */
18438 if (attr.form == DW_FORM_ref_addr)
3e43a32a
MS
18439 complaint (&symfile_complaints,
18440 _("ignoring absolute DW_AT_sibling"));
c906108c 18441 else
b9502d3f 18442 {
48fbe735 18443 const gdb_byte *buffer = reader->buffer;
9c541725
PA
18444 sect_offset off = dwarf2_get_ref_die_offset (&attr);
18445 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
b9502d3f
WN
18446
18447 if (sibling_ptr < info_ptr)
18448 complaint (&symfile_complaints,
18449 _("DW_AT_sibling points backwards"));
22869d73
KS
18450 else if (sibling_ptr > reader->buffer_end)
18451 dwarf2_section_buffer_overflow_complaint (reader->die_section);
b9502d3f 18452 else
48fbe735 18453 sibling = sibling_ptr;
b9502d3f 18454 }
c906108c 18455 break;
fa4028e9 18456 case DW_AT_byte_size:
48fbe735 18457 has_byte_size = 1;
fa4028e9 18458 break;
ff908ebf 18459 case DW_AT_const_value:
48fbe735 18460 has_const_value = 1;
ff908ebf 18461 break;
68511cec
CES
18462 case DW_AT_calling_convention:
18463 /* DWARF doesn't provide a way to identify a program's source-level
18464 entry point. DW_AT_calling_convention attributes are only meant
18465 to describe functions' calling conventions.
18466
18467 However, because it's a necessary piece of information in
0c1b455e
TT
18468 Fortran, and before DWARF 4 DW_CC_program was the only
18469 piece of debugging information whose definition refers to
18470 a 'main program' at all, several compilers marked Fortran
18471 main programs with DW_CC_program --- even when those
18472 functions use the standard calling conventions.
18473
18474 Although DWARF now specifies a way to provide this
18475 information, we support this practice for backward
18476 compatibility. */
68511cec 18477 if (DW_UNSND (&attr) == DW_CC_program
0c1b455e 18478 && cu->language == language_fortran)
48fbe735 18479 main_subprogram = 1;
68511cec 18480 break;
481860b3
GB
18481 case DW_AT_inline:
18482 if (DW_UNSND (&attr) == DW_INL_inlined
18483 || DW_UNSND (&attr) == DW_INL_declared_inlined)
48fbe735 18484 may_be_inlined = 1;
481860b3 18485 break;
95554aad
TT
18486
18487 case DW_AT_import:
48fbe735 18488 if (tag == DW_TAG_imported_unit)
36586728 18489 {
48fbe735
YQ
18490 d.sect_off = dwarf2_get_ref_die_offset (&attr);
18491 is_dwz = (attr.form == DW_FORM_GNU_ref_alt
36586728
TT
18492 || cu->per_cu->is_dwz);
18493 }
95554aad
TT
18494 break;
18495
0c1b455e 18496 case DW_AT_main_subprogram:
48fbe735 18497 main_subprogram = DW_UNSND (&attr);
0c1b455e
TT
18498 break;
18499
c906108c
SS
18500 default:
18501 break;
18502 }
18503 }
18504
91da1414 18505 if (high_pc_relative)
48fbe735 18506 highpc += lowpc;
91da1414 18507
9373cf26
JK
18508 if (has_low_pc_attr && has_high_pc_attr)
18509 {
18510 /* When using the GNU linker, .gnu.linkonce. sections are used to
18511 eliminate duplicate copies of functions and vtables and such.
18512 The linker will arbitrarily choose one and discard the others.
18513 The AT_*_pc values for such functions refer to local labels in
18514 these sections. If the section from that file was discarded, the
18515 labels are not in the output, so the relocs get a value of 0.
18516 If this is a discarded function, mark the pc bounds as invalid,
18517 so that GDB will ignore it. */
48fbe735 18518 if (lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
9373cf26 18519 {
48fbe735 18520 struct objfile *objfile = dwarf2_per_objfile->objfile;
bb5ed363 18521 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9373cf26
JK
18522
18523 complaint (&symfile_complaints,
18524 _("DW_AT_low_pc %s is zero "
9d8780f0 18525 "for DIE at %s [in module %s]"),
48fbe735
YQ
18526 paddress (gdbarch, lowpc),
18527 sect_offset_str (sect_off),
9d8780f0 18528 objfile_name (objfile));
9373cf26
JK
18529 }
18530 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
48fbe735 18531 else if (lowpc >= highpc)
9373cf26 18532 {
48fbe735 18533 struct objfile *objfile = dwarf2_per_objfile->objfile;
bb5ed363 18534 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9373cf26
JK
18535
18536 complaint (&symfile_complaints,
18537 _("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
9d8780f0 18538 "for DIE at %s [in module %s]"),
48fbe735
YQ
18539 paddress (gdbarch, lowpc),
18540 paddress (gdbarch, highpc),
18541 sect_offset_str (sect_off),
9c541725 18542 objfile_name (objfile));
9373cf26
JK
18543 }
18544 else
48fbe735 18545 has_pc_info = 1;
9373cf26 18546 }
85cbf3d3 18547
c906108c
SS
18548 return info_ptr;
18549}
18550
72bf9492
DJ
18551/* Find a cached partial DIE at OFFSET in CU. */
18552
d590ff25
YQ
18553struct partial_die_info *
18554dwarf2_cu::find_partial_die (sect_offset sect_off)
72bf9492
DJ
18555{
18556 struct partial_die_info *lookup_die = NULL;
6f06d47b 18557 struct partial_die_info part_die (sect_off);
72bf9492 18558
9a3c8263 18559 lookup_die = ((struct partial_die_info *)
d590ff25 18560 htab_find_with_hash (partial_dies, &part_die,
9c541725 18561 to_underlying (sect_off)));
72bf9492 18562
72bf9492
DJ
18563 return lookup_die;
18564}
18565
348e048f
DE
18566/* Find a partial DIE at OFFSET, which may or may not be in CU,
18567 except in the case of .debug_types DIEs which do not reference
18568 outside their CU (they do however referencing other types via
55f1336d 18569 DW_FORM_ref_sig8). */
72bf9492
DJ
18570
18571static struct partial_die_info *
9c541725 18572find_partial_die (sect_offset sect_off, int offset_in_dwz, struct dwarf2_cu *cu)
72bf9492 18573{
518817b3
SM
18574 struct dwarf2_per_objfile *dwarf2_per_objfile
18575 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 18576 struct objfile *objfile = dwarf2_per_objfile->objfile;
5afb4e99
DJ
18577 struct dwarf2_per_cu_data *per_cu = NULL;
18578 struct partial_die_info *pd = NULL;
72bf9492 18579
36586728 18580 if (offset_in_dwz == cu->per_cu->is_dwz
9c541725 18581 && offset_in_cu_p (&cu->header, sect_off))
5afb4e99 18582 {
d590ff25 18583 pd = cu->find_partial_die (sect_off);
5afb4e99
DJ
18584 if (pd != NULL)
18585 return pd;
0d99eb77
DE
18586 /* We missed recording what we needed.
18587 Load all dies and try again. */
18588 per_cu = cu->per_cu;
5afb4e99 18589 }
0d99eb77
DE
18590 else
18591 {
18592 /* TUs don't reference other CUs/TUs (except via type signatures). */
3019eac3 18593 if (cu->per_cu->is_debug_types)
0d99eb77 18594 {
9d8780f0
SM
18595 error (_("Dwarf Error: Type Unit at offset %s contains"
18596 " external reference to offset %s [in module %s].\n"),
18597 sect_offset_str (cu->header.sect_off), sect_offset_str (sect_off),
0d99eb77
DE
18598 bfd_get_filename (objfile->obfd));
18599 }
9c541725 18600 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
ed2dc618 18601 dwarf2_per_objfile);
72bf9492 18602
0d99eb77
DE
18603 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
18604 load_partial_comp_unit (per_cu);
ae038cb0 18605
0d99eb77 18606 per_cu->cu->last_used = 0;
d590ff25 18607 pd = per_cu->cu->find_partial_die (sect_off);
0d99eb77 18608 }
5afb4e99 18609
dee91e82
DE
18610 /* If we didn't find it, and not all dies have been loaded,
18611 load them all and try again. */
18612
5afb4e99
DJ
18613 if (pd == NULL && per_cu->load_all_dies == 0)
18614 {
5afb4e99 18615 per_cu->load_all_dies = 1;
fd820528
DE
18616
18617 /* This is nasty. When we reread the DIEs, somewhere up the call chain
18618 THIS_CU->cu may already be in use. So we can't just free it and
18619 replace its DIEs with the ones we read in. Instead, we leave those
18620 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
18621 and clobber THIS_CU->cu->partial_dies with the hash table for the new
18622 set. */
dee91e82 18623 load_partial_comp_unit (per_cu);
5afb4e99 18624
d590ff25 18625 pd = per_cu->cu->find_partial_die (sect_off);
5afb4e99
DJ
18626 }
18627
18628 if (pd == NULL)
18629 internal_error (__FILE__, __LINE__,
9d8780f0 18630 _("could not find partial DIE %s "
3e43a32a 18631 "in cache [from module %s]\n"),
9d8780f0 18632 sect_offset_str (sect_off), bfd_get_filename (objfile->obfd));
5afb4e99 18633 return pd;
72bf9492
DJ
18634}
18635
abc72ce4
DE
18636/* See if we can figure out if the class lives in a namespace. We do
18637 this by looking for a member function; its demangled name will
18638 contain namespace info, if there is any. */
18639
18640static void
18641guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
18642 struct dwarf2_cu *cu)
18643{
18644 /* NOTE: carlton/2003-10-07: Getting the info this way changes
18645 what template types look like, because the demangler
18646 frequently doesn't give the same name as the debug info. We
18647 could fix this by only using the demangled name to get the
18648 prefix (but see comment in read_structure_type). */
18649
18650 struct partial_die_info *real_pdi;
18651 struct partial_die_info *child_pdi;
18652
18653 /* If this DIE (this DIE's specification, if any) has a parent, then
18654 we should not do this. We'll prepend the parent's fully qualified
18655 name when we create the partial symbol. */
18656
18657 real_pdi = struct_pdi;
18658 while (real_pdi->has_specification)
36586728
TT
18659 real_pdi = find_partial_die (real_pdi->spec_offset,
18660 real_pdi->spec_is_dwz, cu);
abc72ce4
DE
18661
18662 if (real_pdi->die_parent != NULL)
18663 return;
18664
18665 for (child_pdi = struct_pdi->die_child;
18666 child_pdi != NULL;
18667 child_pdi = child_pdi->die_sibling)
18668 {
18669 if (child_pdi->tag == DW_TAG_subprogram
18670 && child_pdi->linkage_name != NULL)
18671 {
18672 char *actual_class_name
18673 = language_class_name_from_physname (cu->language_defn,
18674 child_pdi->linkage_name);
18675 if (actual_class_name != NULL)
18676 {
518817b3 18677 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
abc72ce4 18678 struct_pdi->name
224c3ddb 18679 = ((const char *)
e3b94546 18680 obstack_copy0 (&objfile->per_bfd->storage_obstack,
224c3ddb
SM
18681 actual_class_name,
18682 strlen (actual_class_name)));
abc72ce4
DE
18683 xfree (actual_class_name);
18684 }
18685 break;
18686 }
18687 }
18688}
18689
52356b79
YQ
18690void
18691partial_die_info::fixup (struct dwarf2_cu *cu)
72bf9492 18692{
abc72ce4
DE
18693 /* Once we've fixed up a die, there's no point in doing so again.
18694 This also avoids a memory leak if we were to call
18695 guess_partial_die_structure_name multiple times. */
52356b79 18696 if (fixup_called)
abc72ce4
DE
18697 return;
18698
72bf9492
DJ
18699 /* If we found a reference attribute and the DIE has no name, try
18700 to find a name in the referred to DIE. */
18701
52356b79 18702 if (name == NULL && has_specification)
72bf9492
DJ
18703 {
18704 struct partial_die_info *spec_die;
72bf9492 18705
52356b79 18706 spec_die = find_partial_die (spec_offset, spec_is_dwz, cu);
72bf9492 18707
52356b79 18708 spec_die->fixup (cu);
72bf9492
DJ
18709
18710 if (spec_die->name)
18711 {
52356b79 18712 name = spec_die->name;
72bf9492
DJ
18713
18714 /* Copy DW_AT_external attribute if it is set. */
18715 if (spec_die->is_external)
52356b79 18716 is_external = spec_die->is_external;
72bf9492
DJ
18717 }
18718 }
18719
18720 /* Set default names for some unnamed DIEs. */
72bf9492 18721
52356b79
YQ
18722 if (name == NULL && tag == DW_TAG_namespace)
18723 name = CP_ANONYMOUS_NAMESPACE_STR;
72bf9492 18724
abc72ce4
DE
18725 /* If there is no parent die to provide a namespace, and there are
18726 children, see if we can determine the namespace from their linkage
122d1940 18727 name. */
abc72ce4 18728 if (cu->language == language_cplus
518817b3
SM
18729 && !VEC_empty (dwarf2_section_info_def,
18730 cu->per_cu->dwarf2_per_objfile->types)
52356b79
YQ
18731 && die_parent == NULL
18732 && has_children
18733 && (tag == DW_TAG_class_type
18734 || tag == DW_TAG_structure_type
18735 || tag == DW_TAG_union_type))
18736 guess_partial_die_structure_name (this, cu);
abc72ce4 18737
53832f31
TT
18738 /* GCC might emit a nameless struct or union that has a linkage
18739 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
52356b79
YQ
18740 if (name == NULL
18741 && (tag == DW_TAG_class_type
18742 || tag == DW_TAG_interface_type
18743 || tag == DW_TAG_structure_type
18744 || tag == DW_TAG_union_type)
18745 && linkage_name != NULL)
53832f31
TT
18746 {
18747 char *demangled;
18748
52356b79 18749 demangled = gdb_demangle (linkage_name, DMGL_TYPES);
53832f31
TT
18750 if (demangled)
18751 {
96408a79
SA
18752 const char *base;
18753
18754 /* Strip any leading namespaces/classes, keep only the base name.
18755 DW_AT_name for named DIEs does not contain the prefixes. */
18756 base = strrchr (demangled, ':');
18757 if (base && base > demangled && base[-1] == ':')
18758 base++;
18759 else
18760 base = demangled;
18761
518817b3 18762 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
52356b79 18763 name
224c3ddb 18764 = ((const char *)
e3b94546 18765 obstack_copy0 (&objfile->per_bfd->storage_obstack,
224c3ddb 18766 base, strlen (base)));
53832f31
TT
18767 xfree (demangled);
18768 }
18769 }
18770
52356b79 18771 fixup_called = 1;
72bf9492
DJ
18772}
18773
a8329558 18774/* Read an attribute value described by an attribute form. */
c906108c 18775
d521ce57 18776static const gdb_byte *
dee91e82
DE
18777read_attribute_value (const struct die_reader_specs *reader,
18778 struct attribute *attr, unsigned form,
43988095 18779 LONGEST implicit_const, const gdb_byte *info_ptr)
c906108c 18780{
dee91e82 18781 struct dwarf2_cu *cu = reader->cu;
518817b3
SM
18782 struct dwarf2_per_objfile *dwarf2_per_objfile
18783 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 18784 struct objfile *objfile = dwarf2_per_objfile->objfile;
3e29f34a 18785 struct gdbarch *gdbarch = get_objfile_arch (objfile);
dee91e82 18786 bfd *abfd = reader->abfd;
e7c27a73 18787 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
18788 unsigned int bytes_read;
18789 struct dwarf_block *blk;
18790
aead7601 18791 attr->form = (enum dwarf_form) form;
a8329558 18792 switch (form)
c906108c 18793 {
c906108c 18794 case DW_FORM_ref_addr:
ae411497 18795 if (cu->header.version == 2)
4568ecf9 18796 DW_UNSND (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
ae411497 18797 else
4568ecf9
DE
18798 DW_UNSND (attr) = read_offset (abfd, info_ptr,
18799 &cu->header, &bytes_read);
ae411497
TT
18800 info_ptr += bytes_read;
18801 break;
36586728
TT
18802 case DW_FORM_GNU_ref_alt:
18803 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
18804 info_ptr += bytes_read;
18805 break;
ae411497 18806 case DW_FORM_addr:
e7c27a73 18807 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
3e29f34a 18808 DW_ADDR (attr) = gdbarch_adjust_dwarf2_addr (gdbarch, DW_ADDR (attr));
107d2387 18809 info_ptr += bytes_read;
c906108c
SS
18810 break;
18811 case DW_FORM_block2:
7b5a2f43 18812 blk = dwarf_alloc_block (cu);
c906108c
SS
18813 blk->size = read_2_bytes (abfd, info_ptr);
18814 info_ptr += 2;
18815 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
18816 info_ptr += blk->size;
18817 DW_BLOCK (attr) = blk;
18818 break;
18819 case DW_FORM_block4:
7b5a2f43 18820 blk = dwarf_alloc_block (cu);
c906108c
SS
18821 blk->size = read_4_bytes (abfd, info_ptr);
18822 info_ptr += 4;
18823 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
18824 info_ptr += blk->size;
18825 DW_BLOCK (attr) = blk;
18826 break;
18827 case DW_FORM_data2:
18828 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
18829 info_ptr += 2;
18830 break;
18831 case DW_FORM_data4:
18832 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
18833 info_ptr += 4;
18834 break;
18835 case DW_FORM_data8:
18836 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
18837 info_ptr += 8;
18838 break;
0224619f
JK
18839 case DW_FORM_data16:
18840 blk = dwarf_alloc_block (cu);
18841 blk->size = 16;
18842 blk->data = read_n_bytes (abfd, info_ptr, 16);
18843 info_ptr += 16;
18844 DW_BLOCK (attr) = blk;
18845 break;
2dc7f7b3
TT
18846 case DW_FORM_sec_offset:
18847 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
18848 info_ptr += bytes_read;
18849 break;
c906108c 18850 case DW_FORM_string:
9b1c24c8 18851 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
8285870a 18852 DW_STRING_IS_CANONICAL (attr) = 0;
c906108c
SS
18853 info_ptr += bytes_read;
18854 break;
4bdf3d34 18855 case DW_FORM_strp:
36586728
TT
18856 if (!cu->per_cu->is_dwz)
18857 {
ed2dc618
SM
18858 DW_STRING (attr) = read_indirect_string (dwarf2_per_objfile,
18859 abfd, info_ptr, cu_header,
36586728
TT
18860 &bytes_read);
18861 DW_STRING_IS_CANONICAL (attr) = 0;
18862 info_ptr += bytes_read;
18863 break;
18864 }
18865 /* FALLTHROUGH */
43988095
JK
18866 case DW_FORM_line_strp:
18867 if (!cu->per_cu->is_dwz)
18868 {
ed2dc618
SM
18869 DW_STRING (attr) = read_indirect_line_string (dwarf2_per_objfile,
18870 abfd, info_ptr,
43988095
JK
18871 cu_header, &bytes_read);
18872 DW_STRING_IS_CANONICAL (attr) = 0;
18873 info_ptr += bytes_read;
18874 break;
18875 }
18876 /* FALLTHROUGH */
36586728
TT
18877 case DW_FORM_GNU_strp_alt:
18878 {
ed2dc618 18879 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
36586728
TT
18880 LONGEST str_offset = read_offset (abfd, info_ptr, cu_header,
18881 &bytes_read);
18882
ed2dc618
SM
18883 DW_STRING (attr) = read_indirect_string_from_dwz (objfile,
18884 dwz, str_offset);
36586728
TT
18885 DW_STRING_IS_CANONICAL (attr) = 0;
18886 info_ptr += bytes_read;
18887 }
4bdf3d34 18888 break;
2dc7f7b3 18889 case DW_FORM_exprloc:
c906108c 18890 case DW_FORM_block:
7b5a2f43 18891 blk = dwarf_alloc_block (cu);
c906108c
SS
18892 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18893 info_ptr += bytes_read;
18894 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
18895 info_ptr += blk->size;
18896 DW_BLOCK (attr) = blk;
18897 break;
18898 case DW_FORM_block1:
7b5a2f43 18899 blk = dwarf_alloc_block (cu);
c906108c
SS
18900 blk->size = read_1_byte (abfd, info_ptr);
18901 info_ptr += 1;
18902 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
18903 info_ptr += blk->size;
18904 DW_BLOCK (attr) = blk;
18905 break;
18906 case DW_FORM_data1:
18907 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
18908 info_ptr += 1;
18909 break;
18910 case DW_FORM_flag:
18911 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
18912 info_ptr += 1;
18913 break;
2dc7f7b3
TT
18914 case DW_FORM_flag_present:
18915 DW_UNSND (attr) = 1;
18916 break;
c906108c
SS
18917 case DW_FORM_sdata:
18918 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
18919 info_ptr += bytes_read;
18920 break;
18921 case DW_FORM_udata:
18922 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18923 info_ptr += bytes_read;
18924 break;
18925 case DW_FORM_ref1:
9c541725 18926 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 18927 + read_1_byte (abfd, info_ptr));
c906108c
SS
18928 info_ptr += 1;
18929 break;
18930 case DW_FORM_ref2:
9c541725 18931 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 18932 + read_2_bytes (abfd, info_ptr));
c906108c
SS
18933 info_ptr += 2;
18934 break;
18935 case DW_FORM_ref4:
9c541725 18936 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 18937 + read_4_bytes (abfd, info_ptr));
c906108c
SS
18938 info_ptr += 4;
18939 break;
613e1657 18940 case DW_FORM_ref8:
9c541725 18941 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 18942 + read_8_bytes (abfd, info_ptr));
613e1657
KB
18943 info_ptr += 8;
18944 break;
55f1336d 18945 case DW_FORM_ref_sig8:
ac9ec31b 18946 DW_SIGNATURE (attr) = read_8_bytes (abfd, info_ptr);
348e048f
DE
18947 info_ptr += 8;
18948 break;
c906108c 18949 case DW_FORM_ref_udata:
9c541725 18950 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 18951 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
c906108c
SS
18952 info_ptr += bytes_read;
18953 break;
c906108c 18954 case DW_FORM_indirect:
a8329558
KW
18955 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18956 info_ptr += bytes_read;
43988095
JK
18957 if (form == DW_FORM_implicit_const)
18958 {
18959 implicit_const = read_signed_leb128 (abfd, info_ptr, &bytes_read);
18960 info_ptr += bytes_read;
18961 }
18962 info_ptr = read_attribute_value (reader, attr, form, implicit_const,
18963 info_ptr);
18964 break;
18965 case DW_FORM_implicit_const:
18966 DW_SND (attr) = implicit_const;
a8329558 18967 break;
3019eac3
DE
18968 case DW_FORM_GNU_addr_index:
18969 if (reader->dwo_file == NULL)
18970 {
18971 /* For now flag a hard error.
18972 Later we can turn this into a complaint. */
18973 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
18974 dwarf_form_name (form),
18975 bfd_get_filename (abfd));
18976 }
18977 DW_ADDR (attr) = read_addr_index_from_leb128 (cu, info_ptr, &bytes_read);
18978 info_ptr += bytes_read;
18979 break;
18980 case DW_FORM_GNU_str_index:
18981 if (reader->dwo_file == NULL)
18982 {
18983 /* For now flag a hard error.
18984 Later we can turn this into a complaint if warranted. */
18985 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
18986 dwarf_form_name (form),
18987 bfd_get_filename (abfd));
18988 }
18989 {
18990 ULONGEST str_index =
18991 read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18992
342587c4 18993 DW_STRING (attr) = read_str_index (reader, str_index);
3019eac3
DE
18994 DW_STRING_IS_CANONICAL (attr) = 0;
18995 info_ptr += bytes_read;
18996 }
18997 break;
c906108c 18998 default:
8a3fe4f8 18999 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
659b0389
ML
19000 dwarf_form_name (form),
19001 bfd_get_filename (abfd));
c906108c 19002 }
28e94949 19003
36586728 19004 /* Super hack. */
7771576e 19005 if (cu->per_cu->is_dwz && attr_form_is_ref (attr))
36586728
TT
19006 attr->form = DW_FORM_GNU_ref_alt;
19007
28e94949
JB
19008 /* We have seen instances where the compiler tried to emit a byte
19009 size attribute of -1 which ended up being encoded as an unsigned
19010 0xffffffff. Although 0xffffffff is technically a valid size value,
19011 an object of this size seems pretty unlikely so we can relatively
19012 safely treat these cases as if the size attribute was invalid and
19013 treat them as zero by default. */
19014 if (attr->name == DW_AT_byte_size
19015 && form == DW_FORM_data4
19016 && DW_UNSND (attr) >= 0xffffffff)
01c66ae6
JB
19017 {
19018 complaint
19019 (&symfile_complaints,
43bbcdc2
PH
19020 _("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
19021 hex_string (DW_UNSND (attr)));
01c66ae6
JB
19022 DW_UNSND (attr) = 0;
19023 }
28e94949 19024
c906108c
SS
19025 return info_ptr;
19026}
19027
a8329558
KW
19028/* Read an attribute described by an abbreviated attribute. */
19029
d521ce57 19030static const gdb_byte *
dee91e82
DE
19031read_attribute (const struct die_reader_specs *reader,
19032 struct attribute *attr, struct attr_abbrev *abbrev,
d521ce57 19033 const gdb_byte *info_ptr)
a8329558
KW
19034{
19035 attr->name = abbrev->name;
43988095
JK
19036 return read_attribute_value (reader, attr, abbrev->form,
19037 abbrev->implicit_const, info_ptr);
a8329558
KW
19038}
19039
0963b4bd 19040/* Read dwarf information from a buffer. */
c906108c
SS
19041
19042static unsigned int
a1855c1d 19043read_1_byte (bfd *abfd, const gdb_byte *buf)
c906108c 19044{
fe1b8b76 19045 return bfd_get_8 (abfd, buf);
c906108c
SS
19046}
19047
19048static int
a1855c1d 19049read_1_signed_byte (bfd *abfd, const gdb_byte *buf)
c906108c 19050{
fe1b8b76 19051 return bfd_get_signed_8 (abfd, buf);
c906108c
SS
19052}
19053
19054static unsigned int
a1855c1d 19055read_2_bytes (bfd *abfd, const gdb_byte *buf)
c906108c 19056{
fe1b8b76 19057 return bfd_get_16 (abfd, buf);
c906108c
SS
19058}
19059
21ae7a4d 19060static int
a1855c1d 19061read_2_signed_bytes (bfd *abfd, const gdb_byte *buf)
21ae7a4d
JK
19062{
19063 return bfd_get_signed_16 (abfd, buf);
19064}
19065
c906108c 19066static unsigned int
a1855c1d 19067read_4_bytes (bfd *abfd, const gdb_byte *buf)
c906108c 19068{
fe1b8b76 19069 return bfd_get_32 (abfd, buf);
c906108c
SS
19070}
19071
21ae7a4d 19072static int
a1855c1d 19073read_4_signed_bytes (bfd *abfd, const gdb_byte *buf)
21ae7a4d
JK
19074{
19075 return bfd_get_signed_32 (abfd, buf);
19076}
19077
93311388 19078static ULONGEST
a1855c1d 19079read_8_bytes (bfd *abfd, const gdb_byte *buf)
c906108c 19080{
fe1b8b76 19081 return bfd_get_64 (abfd, buf);
c906108c
SS
19082}
19083
19084static CORE_ADDR
d521ce57 19085read_address (bfd *abfd, const gdb_byte *buf, struct dwarf2_cu *cu,
891d2f0b 19086 unsigned int *bytes_read)
c906108c 19087{
e7c27a73 19088 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
19089 CORE_ADDR retval = 0;
19090
107d2387 19091 if (cu_header->signed_addr_p)
c906108c 19092 {
107d2387
AC
19093 switch (cu_header->addr_size)
19094 {
19095 case 2:
fe1b8b76 19096 retval = bfd_get_signed_16 (abfd, buf);
107d2387
AC
19097 break;
19098 case 4:
fe1b8b76 19099 retval = bfd_get_signed_32 (abfd, buf);
107d2387
AC
19100 break;
19101 case 8:
fe1b8b76 19102 retval = bfd_get_signed_64 (abfd, buf);
107d2387
AC
19103 break;
19104 default:
8e65ff28 19105 internal_error (__FILE__, __LINE__,
e2e0b3e5 19106 _("read_address: bad switch, signed [in module %s]"),
659b0389 19107 bfd_get_filename (abfd));
107d2387
AC
19108 }
19109 }
19110 else
19111 {
19112 switch (cu_header->addr_size)
19113 {
19114 case 2:
fe1b8b76 19115 retval = bfd_get_16 (abfd, buf);
107d2387
AC
19116 break;
19117 case 4:
fe1b8b76 19118 retval = bfd_get_32 (abfd, buf);
107d2387
AC
19119 break;
19120 case 8:
fe1b8b76 19121 retval = bfd_get_64 (abfd, buf);
107d2387
AC
19122 break;
19123 default:
8e65ff28 19124 internal_error (__FILE__, __LINE__,
a73c6dcd
MS
19125 _("read_address: bad switch, "
19126 "unsigned [in module %s]"),
659b0389 19127 bfd_get_filename (abfd));
107d2387 19128 }
c906108c 19129 }
64367e0a 19130
107d2387
AC
19131 *bytes_read = cu_header->addr_size;
19132 return retval;
c906108c
SS
19133}
19134
f7ef9339 19135/* Read the initial length from a section. The (draft) DWARF 3
613e1657
KB
19136 specification allows the initial length to take up either 4 bytes
19137 or 12 bytes. If the first 4 bytes are 0xffffffff, then the next 8
19138 bytes describe the length and all offsets will be 8 bytes in length
19139 instead of 4.
19140
f7ef9339
KB
19141 An older, non-standard 64-bit format is also handled by this
19142 function. The older format in question stores the initial length
19143 as an 8-byte quantity without an escape value. Lengths greater
19144 than 2^32 aren't very common which means that the initial 4 bytes
19145 is almost always zero. Since a length value of zero doesn't make
19146 sense for the 32-bit format, this initial zero can be considered to
19147 be an escape value which indicates the presence of the older 64-bit
19148 format. As written, the code can't detect (old format) lengths
917c78fc
MK
19149 greater than 4GB. If it becomes necessary to handle lengths
19150 somewhat larger than 4GB, we could allow other small values (such
19151 as the non-sensical values of 1, 2, and 3) to also be used as
19152 escape values indicating the presence of the old format.
f7ef9339 19153
917c78fc
MK
19154 The value returned via bytes_read should be used to increment the
19155 relevant pointer after calling read_initial_length().
c764a876 19156
613e1657
KB
19157 [ Note: read_initial_length() and read_offset() are based on the
19158 document entitled "DWARF Debugging Information Format", revision
f7ef9339 19159 3, draft 8, dated November 19, 2001. This document was obtained
613e1657
KB
19160 from:
19161
f7ef9339 19162 http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
6e70227d 19163
613e1657
KB
19164 This document is only a draft and is subject to change. (So beware.)
19165
f7ef9339 19166 Details regarding the older, non-standard 64-bit format were
917c78fc
MK
19167 determined empirically by examining 64-bit ELF files produced by
19168 the SGI toolchain on an IRIX 6.5 machine.
f7ef9339
KB
19169
19170 - Kevin, July 16, 2002
613e1657
KB
19171 ] */
19172
19173static LONGEST
d521ce57 19174read_initial_length (bfd *abfd, const gdb_byte *buf, unsigned int *bytes_read)
613e1657 19175{
fe1b8b76 19176 LONGEST length = bfd_get_32 (abfd, buf);
613e1657 19177
dd373385 19178 if (length == 0xffffffff)
613e1657 19179 {
fe1b8b76 19180 length = bfd_get_64 (abfd, buf + 4);
613e1657 19181 *bytes_read = 12;
613e1657 19182 }
dd373385 19183 else if (length == 0)
f7ef9339 19184 {
dd373385 19185 /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX. */
fe1b8b76 19186 length = bfd_get_64 (abfd, buf);
f7ef9339 19187 *bytes_read = 8;
f7ef9339 19188 }
613e1657
KB
19189 else
19190 {
19191 *bytes_read = 4;
613e1657
KB
19192 }
19193
c764a876
DE
19194 return length;
19195}
dd373385 19196
c764a876
DE
19197/* Cover function for read_initial_length.
19198 Returns the length of the object at BUF, and stores the size of the
19199 initial length in *BYTES_READ and stores the size that offsets will be in
19200 *OFFSET_SIZE.
19201 If the initial length size is not equivalent to that specified in
19202 CU_HEADER then issue a complaint.
19203 This is useful when reading non-comp-unit headers. */
dd373385 19204
c764a876 19205static LONGEST
d521ce57 19206read_checked_initial_length_and_offset (bfd *abfd, const gdb_byte *buf,
c764a876
DE
19207 const struct comp_unit_head *cu_header,
19208 unsigned int *bytes_read,
19209 unsigned int *offset_size)
19210{
19211 LONGEST length = read_initial_length (abfd, buf, bytes_read);
19212
19213 gdb_assert (cu_header->initial_length_size == 4
19214 || cu_header->initial_length_size == 8
19215 || cu_header->initial_length_size == 12);
19216
19217 if (cu_header->initial_length_size != *bytes_read)
19218 complaint (&symfile_complaints,
19219 _("intermixed 32-bit and 64-bit DWARF sections"));
dd373385 19220
c764a876 19221 *offset_size = (*bytes_read == 4) ? 4 : 8;
dd373385 19222 return length;
613e1657
KB
19223}
19224
19225/* Read an offset from the data stream. The size of the offset is
917c78fc 19226 given by cu_header->offset_size. */
613e1657
KB
19227
19228static LONGEST
d521ce57
TT
19229read_offset (bfd *abfd, const gdb_byte *buf,
19230 const struct comp_unit_head *cu_header,
891d2f0b 19231 unsigned int *bytes_read)
c764a876
DE
19232{
19233 LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
9a619af0 19234
c764a876
DE
19235 *bytes_read = cu_header->offset_size;
19236 return offset;
19237}
19238
19239/* Read an offset from the data stream. */
19240
19241static LONGEST
d521ce57 19242read_offset_1 (bfd *abfd, const gdb_byte *buf, unsigned int offset_size)
613e1657
KB
19243{
19244 LONGEST retval = 0;
19245
c764a876 19246 switch (offset_size)
613e1657
KB
19247 {
19248 case 4:
fe1b8b76 19249 retval = bfd_get_32 (abfd, buf);
613e1657
KB
19250 break;
19251 case 8:
fe1b8b76 19252 retval = bfd_get_64 (abfd, buf);
613e1657
KB
19253 break;
19254 default:
8e65ff28 19255 internal_error (__FILE__, __LINE__,
c764a876 19256 _("read_offset_1: bad switch [in module %s]"),
659b0389 19257 bfd_get_filename (abfd));
613e1657
KB
19258 }
19259
917c78fc 19260 return retval;
613e1657
KB
19261}
19262
d521ce57
TT
19263static const gdb_byte *
19264read_n_bytes (bfd *abfd, const gdb_byte *buf, unsigned int size)
c906108c
SS
19265{
19266 /* If the size of a host char is 8 bits, we can return a pointer
19267 to the buffer, otherwise we have to copy the data to a buffer
19268 allocated on the temporary obstack. */
4bdf3d34 19269 gdb_assert (HOST_CHAR_BIT == 8);
c906108c 19270 return buf;
c906108c
SS
19271}
19272
d521ce57
TT
19273static const char *
19274read_direct_string (bfd *abfd, const gdb_byte *buf,
19275 unsigned int *bytes_read_ptr)
c906108c
SS
19276{
19277 /* If the size of a host char is 8 bits, we can return a pointer
19278 to the string, otherwise we have to copy the string to a buffer
19279 allocated on the temporary obstack. */
4bdf3d34 19280 gdb_assert (HOST_CHAR_BIT == 8);
c906108c
SS
19281 if (*buf == '\0')
19282 {
19283 *bytes_read_ptr = 1;
19284 return NULL;
19285 }
d521ce57
TT
19286 *bytes_read_ptr = strlen ((const char *) buf) + 1;
19287 return (const char *) buf;
4bdf3d34
JJ
19288}
19289
43988095
JK
19290/* Return pointer to string at section SECT offset STR_OFFSET with error
19291 reporting strings FORM_NAME and SECT_NAME. */
19292
d521ce57 19293static const char *
ed2dc618
SM
19294read_indirect_string_at_offset_from (struct objfile *objfile,
19295 bfd *abfd, LONGEST str_offset,
43988095
JK
19296 struct dwarf2_section_info *sect,
19297 const char *form_name,
19298 const char *sect_name)
19299{
ed2dc618 19300 dwarf2_read_section (objfile, sect);
43988095
JK
19301 if (sect->buffer == NULL)
19302 error (_("%s used without %s section [in module %s]"),
19303 form_name, sect_name, bfd_get_filename (abfd));
19304 if (str_offset >= sect->size)
19305 error (_("%s pointing outside of %s section [in module %s]"),
19306 form_name, sect_name, bfd_get_filename (abfd));
4bdf3d34 19307 gdb_assert (HOST_CHAR_BIT == 8);
43988095 19308 if (sect->buffer[str_offset] == '\0')
4bdf3d34 19309 return NULL;
43988095
JK
19310 return (const char *) (sect->buffer + str_offset);
19311}
19312
19313/* Return pointer to string at .debug_str offset STR_OFFSET. */
19314
19315static const char *
ed2dc618
SM
19316read_indirect_string_at_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
19317 bfd *abfd, LONGEST str_offset)
43988095 19318{
ed2dc618
SM
19319 return read_indirect_string_at_offset_from (dwarf2_per_objfile->objfile,
19320 abfd, str_offset,
43988095
JK
19321 &dwarf2_per_objfile->str,
19322 "DW_FORM_strp", ".debug_str");
19323}
19324
19325/* Return pointer to string at .debug_line_str offset STR_OFFSET. */
19326
19327static const char *
ed2dc618
SM
19328read_indirect_line_string_at_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
19329 bfd *abfd, LONGEST str_offset)
43988095 19330{
ed2dc618
SM
19331 return read_indirect_string_at_offset_from (dwarf2_per_objfile->objfile,
19332 abfd, str_offset,
43988095
JK
19333 &dwarf2_per_objfile->line_str,
19334 "DW_FORM_line_strp",
19335 ".debug_line_str");
c906108c
SS
19336}
19337
36586728
TT
19338/* Read a string at offset STR_OFFSET in the .debug_str section from
19339 the .dwz file DWZ. Throw an error if the offset is too large. If
19340 the string consists of a single NUL byte, return NULL; otherwise
19341 return a pointer to the string. */
19342
d521ce57 19343static const char *
ed2dc618
SM
19344read_indirect_string_from_dwz (struct objfile *objfile, struct dwz_file *dwz,
19345 LONGEST str_offset)
36586728 19346{
ed2dc618 19347 dwarf2_read_section (objfile, &dwz->str);
36586728
TT
19348
19349 if (dwz->str.buffer == NULL)
19350 error (_("DW_FORM_GNU_strp_alt used without .debug_str "
19351 "section [in module %s]"),
19352 bfd_get_filename (dwz->dwz_bfd));
19353 if (str_offset >= dwz->str.size)
19354 error (_("DW_FORM_GNU_strp_alt pointing outside of "
19355 ".debug_str section [in module %s]"),
19356 bfd_get_filename (dwz->dwz_bfd));
19357 gdb_assert (HOST_CHAR_BIT == 8);
19358 if (dwz->str.buffer[str_offset] == '\0')
19359 return NULL;
d521ce57 19360 return (const char *) (dwz->str.buffer + str_offset);
36586728
TT
19361}
19362
43988095
JK
19363/* Return pointer to string at .debug_str offset as read from BUF.
19364 BUF is assumed to be in a compilation unit described by CU_HEADER.
19365 Return *BYTES_READ_PTR count of bytes read from BUF. */
19366
d521ce57 19367static const char *
ed2dc618
SM
19368read_indirect_string (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *abfd,
19369 const gdb_byte *buf,
cf2c3c16
TT
19370 const struct comp_unit_head *cu_header,
19371 unsigned int *bytes_read_ptr)
19372{
19373 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
19374
ed2dc618 19375 return read_indirect_string_at_offset (dwarf2_per_objfile, abfd, str_offset);
cf2c3c16
TT
19376}
19377
43988095
JK
19378/* Return pointer to string at .debug_line_str offset as read from BUF.
19379 BUF is assumed to be in a compilation unit described by CU_HEADER.
19380 Return *BYTES_READ_PTR count of bytes read from BUF. */
19381
19382static const char *
ed2dc618
SM
19383read_indirect_line_string (struct dwarf2_per_objfile *dwarf2_per_objfile,
19384 bfd *abfd, const gdb_byte *buf,
43988095
JK
19385 const struct comp_unit_head *cu_header,
19386 unsigned int *bytes_read_ptr)
19387{
19388 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
19389
ed2dc618
SM
19390 return read_indirect_line_string_at_offset (dwarf2_per_objfile, abfd,
19391 str_offset);
43988095
JK
19392}
19393
19394ULONGEST
d521ce57 19395read_unsigned_leb128 (bfd *abfd, const gdb_byte *buf,
43988095 19396 unsigned int *bytes_read_ptr)
c906108c 19397{
12df843f 19398 ULONGEST result;
ce5d95e1 19399 unsigned int num_read;
870f88f7 19400 int shift;
c906108c
SS
19401 unsigned char byte;
19402
19403 result = 0;
19404 shift = 0;
19405 num_read = 0;
c906108c
SS
19406 while (1)
19407 {
fe1b8b76 19408 byte = bfd_get_8 (abfd, buf);
c906108c
SS
19409 buf++;
19410 num_read++;
12df843f 19411 result |= ((ULONGEST) (byte & 127) << shift);
c906108c
SS
19412 if ((byte & 128) == 0)
19413 {
19414 break;
19415 }
19416 shift += 7;
19417 }
19418 *bytes_read_ptr = num_read;
19419 return result;
19420}
19421
12df843f 19422static LONGEST
d521ce57
TT
19423read_signed_leb128 (bfd *abfd, const gdb_byte *buf,
19424 unsigned int *bytes_read_ptr)
c906108c 19425{
12df843f 19426 LONGEST result;
870f88f7 19427 int shift, num_read;
c906108c
SS
19428 unsigned char byte;
19429
19430 result = 0;
19431 shift = 0;
c906108c 19432 num_read = 0;
c906108c
SS
19433 while (1)
19434 {
fe1b8b76 19435 byte = bfd_get_8 (abfd, buf);
c906108c
SS
19436 buf++;
19437 num_read++;
12df843f 19438 result |= ((LONGEST) (byte & 127) << shift);
c906108c
SS
19439 shift += 7;
19440 if ((byte & 128) == 0)
19441 {
19442 break;
19443 }
19444 }
77e0b926 19445 if ((shift < 8 * sizeof (result)) && (byte & 0x40))
12df843f 19446 result |= -(((LONGEST) 1) << shift);
c906108c
SS
19447 *bytes_read_ptr = num_read;
19448 return result;
19449}
19450
3019eac3
DE
19451/* Given index ADDR_INDEX in .debug_addr, fetch the value.
19452 ADDR_BASE is the DW_AT_GNU_addr_base attribute or zero.
19453 ADDR_SIZE is the size of addresses from the CU header. */
19454
19455static CORE_ADDR
ed2dc618
SM
19456read_addr_index_1 (struct dwarf2_per_objfile *dwarf2_per_objfile,
19457 unsigned int addr_index, ULONGEST addr_base, int addr_size)
3019eac3
DE
19458{
19459 struct objfile *objfile = dwarf2_per_objfile->objfile;
19460 bfd *abfd = objfile->obfd;
19461 const gdb_byte *info_ptr;
19462
19463 dwarf2_read_section (objfile, &dwarf2_per_objfile->addr);
19464 if (dwarf2_per_objfile->addr.buffer == NULL)
19465 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
4262abfb 19466 objfile_name (objfile));
3019eac3
DE
19467 if (addr_base + addr_index * addr_size >= dwarf2_per_objfile->addr.size)
19468 error (_("DW_FORM_addr_index pointing outside of "
19469 ".debug_addr section [in module %s]"),
4262abfb 19470 objfile_name (objfile));
3019eac3
DE
19471 info_ptr = (dwarf2_per_objfile->addr.buffer
19472 + addr_base + addr_index * addr_size);
19473 if (addr_size == 4)
19474 return bfd_get_32 (abfd, info_ptr);
19475 else
19476 return bfd_get_64 (abfd, info_ptr);
19477}
19478
19479/* Given index ADDR_INDEX in .debug_addr, fetch the value. */
19480
19481static CORE_ADDR
19482read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
19483{
518817b3
SM
19484 return read_addr_index_1 (cu->per_cu->dwarf2_per_objfile, addr_index,
19485 cu->addr_base, cu->header.addr_size);
3019eac3
DE
19486}
19487
19488/* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
19489
19490static CORE_ADDR
d521ce57 19491read_addr_index_from_leb128 (struct dwarf2_cu *cu, const gdb_byte *info_ptr,
3019eac3
DE
19492 unsigned int *bytes_read)
19493{
518817b3 19494 bfd *abfd = cu->per_cu->dwarf2_per_objfile->objfile->obfd;
3019eac3
DE
19495 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
19496
19497 return read_addr_index (cu, addr_index);
19498}
19499
19500/* Data structure to pass results from dwarf2_read_addr_index_reader
19501 back to dwarf2_read_addr_index. */
19502
19503struct dwarf2_read_addr_index_data
19504{
19505 ULONGEST addr_base;
19506 int addr_size;
19507};
19508
19509/* die_reader_func for dwarf2_read_addr_index. */
19510
19511static void
19512dwarf2_read_addr_index_reader (const struct die_reader_specs *reader,
d521ce57 19513 const gdb_byte *info_ptr,
3019eac3
DE
19514 struct die_info *comp_unit_die,
19515 int has_children,
19516 void *data)
19517{
19518 struct dwarf2_cu *cu = reader->cu;
19519 struct dwarf2_read_addr_index_data *aidata =
19520 (struct dwarf2_read_addr_index_data *) data;
19521
19522 aidata->addr_base = cu->addr_base;
19523 aidata->addr_size = cu->header.addr_size;
19524}
19525
19526/* Given an index in .debug_addr, fetch the value.
19527 NOTE: This can be called during dwarf expression evaluation,
19528 long after the debug information has been read, and thus per_cu->cu
19529 may no longer exist. */
19530
19531CORE_ADDR
19532dwarf2_read_addr_index (struct dwarf2_per_cu_data *per_cu,
19533 unsigned int addr_index)
19534{
ed2dc618 19535 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
3019eac3
DE
19536 struct dwarf2_cu *cu = per_cu->cu;
19537 ULONGEST addr_base;
19538 int addr_size;
19539
3019eac3
DE
19540 /* We need addr_base and addr_size.
19541 If we don't have PER_CU->cu, we have to get it.
19542 Nasty, but the alternative is storing the needed info in PER_CU,
19543 which at this point doesn't seem justified: it's not clear how frequently
19544 it would get used and it would increase the size of every PER_CU.
19545 Entry points like dwarf2_per_cu_addr_size do a similar thing
19546 so we're not in uncharted territory here.
19547 Alas we need to be a bit more complicated as addr_base is contained
19548 in the DIE.
19549
19550 We don't need to read the entire CU(/TU).
19551 We just need the header and top level die.
a1b64ce1 19552
3019eac3 19553 IWBN to use the aging mechanism to let us lazily later discard the CU.
a1b64ce1 19554 For now we skip this optimization. */
3019eac3
DE
19555
19556 if (cu != NULL)
19557 {
19558 addr_base = cu->addr_base;
19559 addr_size = cu->header.addr_size;
19560 }
19561 else
19562 {
19563 struct dwarf2_read_addr_index_data aidata;
19564
a1b64ce1
DE
19565 /* Note: We can't use init_cutu_and_read_dies_simple here,
19566 we need addr_base. */
19567 init_cutu_and_read_dies (per_cu, NULL, 0, 0,
19568 dwarf2_read_addr_index_reader, &aidata);
3019eac3
DE
19569 addr_base = aidata.addr_base;
19570 addr_size = aidata.addr_size;
19571 }
19572
ed2dc618
SM
19573 return read_addr_index_1 (dwarf2_per_objfile, addr_index, addr_base,
19574 addr_size);
3019eac3
DE
19575}
19576
57d63ce2
DE
19577/* Given a DW_FORM_GNU_str_index, fetch the string.
19578 This is only used by the Fission support. */
3019eac3 19579
d521ce57 19580static const char *
342587c4 19581read_str_index (const struct die_reader_specs *reader, ULONGEST str_index)
3019eac3 19582{
ed2dc618 19583 struct dwarf2_cu *cu = reader->cu;
518817b3
SM
19584 struct dwarf2_per_objfile *dwarf2_per_objfile
19585 = cu->per_cu->dwarf2_per_objfile;
3019eac3 19586 struct objfile *objfile = dwarf2_per_objfile->objfile;
c5164cbc 19587 const char *objf_name = objfile_name (objfile);
3019eac3 19588 bfd *abfd = objfile->obfd;
73869dc2
DE
19589 struct dwarf2_section_info *str_section = &reader->dwo_file->sections.str;
19590 struct dwarf2_section_info *str_offsets_section =
19591 &reader->dwo_file->sections.str_offsets;
d521ce57 19592 const gdb_byte *info_ptr;
3019eac3 19593 ULONGEST str_offset;
57d63ce2 19594 static const char form_name[] = "DW_FORM_GNU_str_index";
3019eac3 19595
73869dc2
DE
19596 dwarf2_read_section (objfile, str_section);
19597 dwarf2_read_section (objfile, str_offsets_section);
19598 if (str_section->buffer == NULL)
57d63ce2 19599 error (_("%s used without .debug_str.dwo section"
9d8780f0
SM
19600 " in CU at offset %s [in module %s]"),
19601 form_name, sect_offset_str (cu->header.sect_off), objf_name);
73869dc2 19602 if (str_offsets_section->buffer == NULL)
57d63ce2 19603 error (_("%s used without .debug_str_offsets.dwo section"
9d8780f0
SM
19604 " in CU at offset %s [in module %s]"),
19605 form_name, sect_offset_str (cu->header.sect_off), objf_name);
73869dc2 19606 if (str_index * cu->header.offset_size >= str_offsets_section->size)
57d63ce2 19607 error (_("%s pointing outside of .debug_str_offsets.dwo"
9d8780f0
SM
19608 " section in CU at offset %s [in module %s]"),
19609 form_name, sect_offset_str (cu->header.sect_off), objf_name);
73869dc2 19610 info_ptr = (str_offsets_section->buffer
3019eac3
DE
19611 + str_index * cu->header.offset_size);
19612 if (cu->header.offset_size == 4)
19613 str_offset = bfd_get_32 (abfd, info_ptr);
19614 else
19615 str_offset = bfd_get_64 (abfd, info_ptr);
73869dc2 19616 if (str_offset >= str_section->size)
57d63ce2 19617 error (_("Offset from %s pointing outside of"
9d8780f0
SM
19618 " .debug_str.dwo section in CU at offset %s [in module %s]"),
19619 form_name, sect_offset_str (cu->header.sect_off), objf_name);
73869dc2 19620 return (const char *) (str_section->buffer + str_offset);
3019eac3
DE
19621}
19622
3019eac3
DE
19623/* Return the length of an LEB128 number in BUF. */
19624
19625static int
19626leb128_size (const gdb_byte *buf)
19627{
19628 const gdb_byte *begin = buf;
19629 gdb_byte byte;
19630
19631 while (1)
19632 {
19633 byte = *buf++;
19634 if ((byte & 128) == 0)
19635 return buf - begin;
19636 }
19637}
19638
c906108c 19639static void
e142c38c 19640set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
c906108c
SS
19641{
19642 switch (lang)
19643 {
19644 case DW_LANG_C89:
76bee0cc 19645 case DW_LANG_C99:
0cfd832f 19646 case DW_LANG_C11:
c906108c 19647 case DW_LANG_C:
d1be3247 19648 case DW_LANG_UPC:
e142c38c 19649 cu->language = language_c;
c906108c 19650 break;
9c37b5ae 19651 case DW_LANG_Java:
c906108c 19652 case DW_LANG_C_plus_plus:
0cfd832f
MW
19653 case DW_LANG_C_plus_plus_11:
19654 case DW_LANG_C_plus_plus_14:
e142c38c 19655 cu->language = language_cplus;
c906108c 19656 break;
6aecb9c2
JB
19657 case DW_LANG_D:
19658 cu->language = language_d;
19659 break;
c906108c
SS
19660 case DW_LANG_Fortran77:
19661 case DW_LANG_Fortran90:
b21b22e0 19662 case DW_LANG_Fortran95:
f7de9aab
MW
19663 case DW_LANG_Fortran03:
19664 case DW_LANG_Fortran08:
e142c38c 19665 cu->language = language_fortran;
c906108c 19666 break;
a766d390
DE
19667 case DW_LANG_Go:
19668 cu->language = language_go;
19669 break;
c906108c 19670 case DW_LANG_Mips_Assembler:
e142c38c 19671 cu->language = language_asm;
c906108c
SS
19672 break;
19673 case DW_LANG_Ada83:
8aaf0b47 19674 case DW_LANG_Ada95:
bc5f45f8
JB
19675 cu->language = language_ada;
19676 break;
72019c9c
GM
19677 case DW_LANG_Modula2:
19678 cu->language = language_m2;
19679 break;
fe8e67fd
PM
19680 case DW_LANG_Pascal83:
19681 cu->language = language_pascal;
19682 break;
22566fbd
DJ
19683 case DW_LANG_ObjC:
19684 cu->language = language_objc;
19685 break;
c44af4eb
TT
19686 case DW_LANG_Rust:
19687 case DW_LANG_Rust_old:
19688 cu->language = language_rust;
19689 break;
c906108c
SS
19690 case DW_LANG_Cobol74:
19691 case DW_LANG_Cobol85:
c906108c 19692 default:
e142c38c 19693 cu->language = language_minimal;
c906108c
SS
19694 break;
19695 }
e142c38c 19696 cu->language_defn = language_def (cu->language);
c906108c
SS
19697}
19698
19699/* Return the named attribute or NULL if not there. */
19700
19701static struct attribute *
e142c38c 19702dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
c906108c 19703{
a48e046c 19704 for (;;)
c906108c 19705 {
a48e046c
TT
19706 unsigned int i;
19707 struct attribute *spec = NULL;
19708
19709 for (i = 0; i < die->num_attrs; ++i)
19710 {
19711 if (die->attrs[i].name == name)
19712 return &die->attrs[i];
19713 if (die->attrs[i].name == DW_AT_specification
19714 || die->attrs[i].name == DW_AT_abstract_origin)
19715 spec = &die->attrs[i];
19716 }
19717
19718 if (!spec)
19719 break;
c906108c 19720
f2f0e013 19721 die = follow_die_ref (die, spec, &cu);
f2f0e013 19722 }
c5aa993b 19723
c906108c
SS
19724 return NULL;
19725}
19726
348e048f
DE
19727/* Return the named attribute or NULL if not there,
19728 but do not follow DW_AT_specification, etc.
19729 This is for use in contexts where we're reading .debug_types dies.
19730 Following DW_AT_specification, DW_AT_abstract_origin will take us
19731 back up the chain, and we want to go down. */
19732
19733static struct attribute *
45e58e77 19734dwarf2_attr_no_follow (struct die_info *die, unsigned int name)
348e048f
DE
19735{
19736 unsigned int i;
19737
19738 for (i = 0; i < die->num_attrs; ++i)
19739 if (die->attrs[i].name == name)
19740 return &die->attrs[i];
19741
19742 return NULL;
19743}
19744
7d45c7c3
KB
19745/* Return the string associated with a string-typed attribute, or NULL if it
19746 is either not found or is of an incorrect type. */
19747
19748static const char *
19749dwarf2_string_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
19750{
19751 struct attribute *attr;
19752 const char *str = NULL;
19753
19754 attr = dwarf2_attr (die, name, cu);
19755
19756 if (attr != NULL)
19757 {
43988095 19758 if (attr->form == DW_FORM_strp || attr->form == DW_FORM_line_strp
b3340438
L
19759 || attr->form == DW_FORM_string
19760 || attr->form == DW_FORM_GNU_str_index
16eb6b2d 19761 || attr->form == DW_FORM_GNU_strp_alt)
7d45c7c3
KB
19762 str = DW_STRING (attr);
19763 else
19764 complaint (&symfile_complaints,
19765 _("string type expected for attribute %s for "
9d8780f0
SM
19766 "DIE at %s in module %s"),
19767 dwarf_attr_name (name), sect_offset_str (die->sect_off),
518817b3 19768 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
7d45c7c3
KB
19769 }
19770
19771 return str;
19772}
19773
05cf31d1
JB
19774/* Return non-zero iff the attribute NAME is defined for the given DIE,
19775 and holds a non-zero value. This function should only be used for
2dc7f7b3 19776 DW_FORM_flag or DW_FORM_flag_present attributes. */
05cf31d1
JB
19777
19778static int
19779dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
19780{
19781 struct attribute *attr = dwarf2_attr (die, name, cu);
19782
19783 return (attr && DW_UNSND (attr));
19784}
19785
3ca72b44 19786static int
e142c38c 19787die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
3ca72b44 19788{
05cf31d1
JB
19789 /* A DIE is a declaration if it has a DW_AT_declaration attribute
19790 which value is non-zero. However, we have to be careful with
19791 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
19792 (via dwarf2_flag_true_p) follows this attribute. So we may
19793 end up accidently finding a declaration attribute that belongs
19794 to a different DIE referenced by the specification attribute,
19795 even though the given DIE does not have a declaration attribute. */
19796 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
19797 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
3ca72b44
AC
19798}
19799
63d06c5c 19800/* Return the die giving the specification for DIE, if there is
f2f0e013 19801 one. *SPEC_CU is the CU containing DIE on input, and the CU
edb3359d
DJ
19802 containing the return value on output. If there is no
19803 specification, but there is an abstract origin, that is
19804 returned. */
63d06c5c
DC
19805
19806static struct die_info *
f2f0e013 19807die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
63d06c5c 19808{
f2f0e013
DJ
19809 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
19810 *spec_cu);
63d06c5c 19811
edb3359d
DJ
19812 if (spec_attr == NULL)
19813 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
19814
63d06c5c
DC
19815 if (spec_attr == NULL)
19816 return NULL;
19817 else
f2f0e013 19818 return follow_die_ref (die, spec_attr, spec_cu);
63d06c5c 19819}
c906108c 19820
527f3840
JK
19821/* Stub for free_line_header to match void * callback types. */
19822
19823static void
19824free_line_header_voidp (void *arg)
19825{
9a3c8263 19826 struct line_header *lh = (struct line_header *) arg;
527f3840 19827
fff8551c 19828 delete lh;
527f3840
JK
19829}
19830
fff8551c
PA
19831void
19832line_header::add_include_dir (const char *include_dir)
c906108c 19833{
27e0867f 19834 if (dwarf_line_debug >= 2)
fff8551c
PA
19835 fprintf_unfiltered (gdb_stdlog, "Adding dir %zu: %s\n",
19836 include_dirs.size () + 1, include_dir);
27e0867f 19837
fff8551c 19838 include_dirs.push_back (include_dir);
debd256d 19839}
6e70227d 19840
fff8551c
PA
19841void
19842line_header::add_file_name (const char *name,
ecfb656c 19843 dir_index d_index,
fff8551c
PA
19844 unsigned int mod_time,
19845 unsigned int length)
debd256d 19846{
27e0867f
DE
19847 if (dwarf_line_debug >= 2)
19848 fprintf_unfiltered (gdb_stdlog, "Adding file %u: %s\n",
fff8551c 19849 (unsigned) file_names.size () + 1, name);
27e0867f 19850
ecfb656c 19851 file_names.emplace_back (name, d_index, mod_time, length);
debd256d 19852}
6e70227d 19853
83769d0b 19854/* A convenience function to find the proper .debug_line section for a CU. */
36586728
TT
19855
19856static struct dwarf2_section_info *
19857get_debug_line_section (struct dwarf2_cu *cu)
19858{
19859 struct dwarf2_section_info *section;
518817b3
SM
19860 struct dwarf2_per_objfile *dwarf2_per_objfile
19861 = cu->per_cu->dwarf2_per_objfile;
36586728
TT
19862
19863 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
19864 DWO file. */
19865 if (cu->dwo_unit && cu->per_cu->is_debug_types)
19866 section = &cu->dwo_unit->dwo_file->sections.line;
19867 else if (cu->per_cu->is_dwz)
19868 {
ed2dc618 19869 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
36586728
TT
19870
19871 section = &dwz->line;
19872 }
19873 else
19874 section = &dwarf2_per_objfile->line;
19875
19876 return section;
19877}
19878
43988095
JK
19879/* Read directory or file name entry format, starting with byte of
19880 format count entries, ULEB128 pairs of entry formats, ULEB128 of
19881 entries count and the entries themselves in the described entry
19882 format. */
19883
19884static void
ed2dc618
SM
19885read_formatted_entries (struct dwarf2_per_objfile *dwarf2_per_objfile,
19886 bfd *abfd, const gdb_byte **bufp,
43988095
JK
19887 struct line_header *lh,
19888 const struct comp_unit_head *cu_header,
19889 void (*callback) (struct line_header *lh,
19890 const char *name,
ecfb656c 19891 dir_index d_index,
43988095
JK
19892 unsigned int mod_time,
19893 unsigned int length))
19894{
19895 gdb_byte format_count, formati;
19896 ULONGEST data_count, datai;
19897 const gdb_byte *buf = *bufp;
19898 const gdb_byte *format_header_data;
43988095
JK
19899 unsigned int bytes_read;
19900
19901 format_count = read_1_byte (abfd, buf);
19902 buf += 1;
19903 format_header_data = buf;
19904 for (formati = 0; formati < format_count; formati++)
19905 {
19906 read_unsigned_leb128 (abfd, buf, &bytes_read);
19907 buf += bytes_read;
19908 read_unsigned_leb128 (abfd, buf, &bytes_read);
19909 buf += bytes_read;
19910 }
19911
19912 data_count = read_unsigned_leb128 (abfd, buf, &bytes_read);
19913 buf += bytes_read;
19914 for (datai = 0; datai < data_count; datai++)
19915 {
19916 const gdb_byte *format = format_header_data;
19917 struct file_entry fe;
19918
43988095
JK
19919 for (formati = 0; formati < format_count; formati++)
19920 {
ecfb656c 19921 ULONGEST content_type = read_unsigned_leb128 (abfd, format, &bytes_read);
43988095 19922 format += bytes_read;
43988095 19923
ecfb656c 19924 ULONGEST form = read_unsigned_leb128 (abfd, format, &bytes_read);
43988095 19925 format += bytes_read;
ecfb656c
PA
19926
19927 gdb::optional<const char *> string;
19928 gdb::optional<unsigned int> uint;
19929
43988095
JK
19930 switch (form)
19931 {
19932 case DW_FORM_string:
ecfb656c 19933 string.emplace (read_direct_string (abfd, buf, &bytes_read));
43988095
JK
19934 buf += bytes_read;
19935 break;
19936
19937 case DW_FORM_line_strp:
ed2dc618
SM
19938 string.emplace (read_indirect_line_string (dwarf2_per_objfile,
19939 abfd, buf,
ecfb656c
PA
19940 cu_header,
19941 &bytes_read));
43988095
JK
19942 buf += bytes_read;
19943 break;
19944
19945 case DW_FORM_data1:
ecfb656c 19946 uint.emplace (read_1_byte (abfd, buf));
43988095
JK
19947 buf += 1;
19948 break;
19949
19950 case DW_FORM_data2:
ecfb656c 19951 uint.emplace (read_2_bytes (abfd, buf));
43988095
JK
19952 buf += 2;
19953 break;
19954
19955 case DW_FORM_data4:
ecfb656c 19956 uint.emplace (read_4_bytes (abfd, buf));
43988095
JK
19957 buf += 4;
19958 break;
19959
19960 case DW_FORM_data8:
ecfb656c 19961 uint.emplace (read_8_bytes (abfd, buf));
43988095
JK
19962 buf += 8;
19963 break;
19964
19965 case DW_FORM_udata:
ecfb656c 19966 uint.emplace (read_unsigned_leb128 (abfd, buf, &bytes_read));
43988095
JK
19967 buf += bytes_read;
19968 break;
19969
19970 case DW_FORM_block:
19971 /* It is valid only for DW_LNCT_timestamp which is ignored by
19972 current GDB. */
19973 break;
19974 }
ecfb656c
PA
19975
19976 switch (content_type)
19977 {
19978 case DW_LNCT_path:
19979 if (string.has_value ())
19980 fe.name = *string;
19981 break;
19982 case DW_LNCT_directory_index:
19983 if (uint.has_value ())
19984 fe.d_index = (dir_index) *uint;
19985 break;
19986 case DW_LNCT_timestamp:
19987 if (uint.has_value ())
19988 fe.mod_time = *uint;
19989 break;
19990 case DW_LNCT_size:
19991 if (uint.has_value ())
19992 fe.length = *uint;
19993 break;
19994 case DW_LNCT_MD5:
19995 break;
19996 default:
19997 complaint (&symfile_complaints,
19998 _("Unknown format content type %s"),
19999 pulongest (content_type));
20000 }
43988095
JK
20001 }
20002
ecfb656c 20003 callback (lh, fe.name, fe.d_index, fe.mod_time, fe.length);
43988095
JK
20004 }
20005
20006 *bufp = buf;
20007}
20008
debd256d 20009/* Read the statement program header starting at OFFSET in
3019eac3 20010 .debug_line, or .debug_line.dwo. Return a pointer
6502dd73 20011 to a struct line_header, allocated using xmalloc.
cd366ee8
DE
20012 Returns NULL if there is a problem reading the header, e.g., if it
20013 has a version we don't understand.
debd256d
JB
20014
20015 NOTE: the strings in the include directory and file name tables of
3019eac3
DE
20016 the returned object point into the dwarf line section buffer,
20017 and must not be freed. */
ae2de4f8 20018
fff8551c 20019static line_header_up
9c541725 20020dwarf_decode_line_header (sect_offset sect_off, struct dwarf2_cu *cu)
debd256d 20021{
d521ce57 20022 const gdb_byte *line_ptr;
c764a876 20023 unsigned int bytes_read, offset_size;
debd256d 20024 int i;
d521ce57 20025 const char *cur_dir, *cur_file;
3019eac3
DE
20026 struct dwarf2_section_info *section;
20027 bfd *abfd;
518817b3
SM
20028 struct dwarf2_per_objfile *dwarf2_per_objfile
20029 = cu->per_cu->dwarf2_per_objfile;
3019eac3 20030
36586728 20031 section = get_debug_line_section (cu);
3019eac3
DE
20032 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
20033 if (section->buffer == NULL)
debd256d 20034 {
3019eac3
DE
20035 if (cu->dwo_unit && cu->per_cu->is_debug_types)
20036 complaint (&symfile_complaints, _("missing .debug_line.dwo section"));
20037 else
20038 complaint (&symfile_complaints, _("missing .debug_line section"));
debd256d
JB
20039 return 0;
20040 }
20041
fceca515
DE
20042 /* We can't do this until we know the section is non-empty.
20043 Only then do we know we have such a section. */
a32a8923 20044 abfd = get_section_bfd_owner (section);
fceca515 20045
a738430d
MK
20046 /* Make sure that at least there's room for the total_length field.
20047 That could be 12 bytes long, but we're just going to fudge that. */
9c541725 20048 if (to_underlying (sect_off) + 4 >= section->size)
debd256d 20049 {
4d3c2250 20050 dwarf2_statement_list_fits_in_line_number_section_complaint ();
debd256d
JB
20051 return 0;
20052 }
20053
fff8551c 20054 line_header_up lh (new line_header ());
debd256d 20055
9c541725 20056 lh->sect_off = sect_off;
527f3840
JK
20057 lh->offset_in_dwz = cu->per_cu->is_dwz;
20058
9c541725 20059 line_ptr = section->buffer + to_underlying (sect_off);
debd256d 20060
a738430d 20061 /* Read in the header. */
6e70227d 20062 lh->total_length =
c764a876
DE
20063 read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
20064 &bytes_read, &offset_size);
debd256d 20065 line_ptr += bytes_read;
3019eac3 20066 if (line_ptr + lh->total_length > (section->buffer + section->size))
debd256d 20067 {
4d3c2250 20068 dwarf2_statement_list_fits_in_line_number_section_complaint ();
debd256d
JB
20069 return 0;
20070 }
20071 lh->statement_program_end = line_ptr + lh->total_length;
20072 lh->version = read_2_bytes (abfd, line_ptr);
20073 line_ptr += 2;
43988095 20074 if (lh->version > 5)
cd366ee8
DE
20075 {
20076 /* This is a version we don't understand. The format could have
20077 changed in ways we don't handle properly so just punt. */
20078 complaint (&symfile_complaints,
20079 _("unsupported version in .debug_line section"));
20080 return NULL;
20081 }
43988095
JK
20082 if (lh->version >= 5)
20083 {
20084 gdb_byte segment_selector_size;
20085
20086 /* Skip address size. */
20087 read_1_byte (abfd, line_ptr);
20088 line_ptr += 1;
20089
20090 segment_selector_size = read_1_byte (abfd, line_ptr);
20091 line_ptr += 1;
20092 if (segment_selector_size != 0)
20093 {
20094 complaint (&symfile_complaints,
20095 _("unsupported segment selector size %u "
20096 "in .debug_line section"),
20097 segment_selector_size);
20098 return NULL;
20099 }
20100 }
c764a876
DE
20101 lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
20102 line_ptr += offset_size;
debd256d
JB
20103 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
20104 line_ptr += 1;
2dc7f7b3
TT
20105 if (lh->version >= 4)
20106 {
20107 lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
20108 line_ptr += 1;
20109 }
20110 else
20111 lh->maximum_ops_per_instruction = 1;
20112
20113 if (lh->maximum_ops_per_instruction == 0)
20114 {
20115 lh->maximum_ops_per_instruction = 1;
20116 complaint (&symfile_complaints,
3e43a32a
MS
20117 _("invalid maximum_ops_per_instruction "
20118 "in `.debug_line' section"));
2dc7f7b3
TT
20119 }
20120
debd256d
JB
20121 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
20122 line_ptr += 1;
20123 lh->line_base = read_1_signed_byte (abfd, line_ptr);
20124 line_ptr += 1;
20125 lh->line_range = read_1_byte (abfd, line_ptr);
20126 line_ptr += 1;
20127 lh->opcode_base = read_1_byte (abfd, line_ptr);
20128 line_ptr += 1;
fff8551c 20129 lh->standard_opcode_lengths.reset (new unsigned char[lh->opcode_base]);
debd256d
JB
20130
20131 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
20132 for (i = 1; i < lh->opcode_base; ++i)
20133 {
20134 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
20135 line_ptr += 1;
20136 }
20137
43988095 20138 if (lh->version >= 5)
debd256d 20139 {
43988095 20140 /* Read directory table. */
ed2dc618
SM
20141 read_formatted_entries (dwarf2_per_objfile, abfd, &line_ptr, lh.get (),
20142 &cu->header,
fff8551c 20143 [] (struct line_header *lh, const char *name,
ecfb656c 20144 dir_index d_index, unsigned int mod_time,
fff8551c
PA
20145 unsigned int length)
20146 {
20147 lh->add_include_dir (name);
20148 });
debd256d 20149
43988095 20150 /* Read file name table. */
ed2dc618
SM
20151 read_formatted_entries (dwarf2_per_objfile, abfd, &line_ptr, lh.get (),
20152 &cu->header,
fff8551c 20153 [] (struct line_header *lh, const char *name,
ecfb656c 20154 dir_index d_index, unsigned int mod_time,
fff8551c
PA
20155 unsigned int length)
20156 {
ecfb656c 20157 lh->add_file_name (name, d_index, mod_time, length);
fff8551c 20158 });
43988095
JK
20159 }
20160 else
debd256d 20161 {
43988095
JK
20162 /* Read directory table. */
20163 while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
20164 {
20165 line_ptr += bytes_read;
fff8551c 20166 lh->add_include_dir (cur_dir);
43988095 20167 }
debd256d
JB
20168 line_ptr += bytes_read;
20169
43988095
JK
20170 /* Read file name table. */
20171 while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
20172 {
ecfb656c
PA
20173 unsigned int mod_time, length;
20174 dir_index d_index;
43988095
JK
20175
20176 line_ptr += bytes_read;
ecfb656c 20177 d_index = (dir_index) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
43988095
JK
20178 line_ptr += bytes_read;
20179 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20180 line_ptr += bytes_read;
20181 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20182 line_ptr += bytes_read;
20183
ecfb656c 20184 lh->add_file_name (cur_file, d_index, mod_time, length);
43988095
JK
20185 }
20186 line_ptr += bytes_read;
debd256d 20187 }
6e70227d 20188 lh->statement_program_start = line_ptr;
debd256d 20189
3019eac3 20190 if (line_ptr > (section->buffer + section->size))
4d3c2250 20191 complaint (&symfile_complaints,
3e43a32a
MS
20192 _("line number info header doesn't "
20193 "fit in `.debug_line' section"));
debd256d 20194
debd256d
JB
20195 return lh;
20196}
c906108c 20197
c6da4cef
DE
20198/* Subroutine of dwarf_decode_lines to simplify it.
20199 Return the file name of the psymtab for included file FILE_INDEX
20200 in line header LH of PST.
20201 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
c89b44cd
TT
20202 If space for the result is malloc'd, *NAME_HOLDER will be set.
20203 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
c6da4cef 20204
d521ce57 20205static const char *
c6da4cef
DE
20206psymtab_include_file_name (const struct line_header *lh, int file_index,
20207 const struct partial_symtab *pst,
c89b44cd
TT
20208 const char *comp_dir,
20209 gdb::unique_xmalloc_ptr<char> *name_holder)
c6da4cef 20210{
8c43009f 20211 const file_entry &fe = lh->file_names[file_index];
d521ce57
TT
20212 const char *include_name = fe.name;
20213 const char *include_name_to_compare = include_name;
72b9f47f 20214 const char *pst_filename;
c6da4cef
DE
20215 int file_is_pst;
20216
8c43009f 20217 const char *dir_name = fe.include_dir (lh);
c6da4cef 20218
c89b44cd 20219 gdb::unique_xmalloc_ptr<char> hold_compare;
c6da4cef
DE
20220 if (!IS_ABSOLUTE_PATH (include_name)
20221 && (dir_name != NULL || comp_dir != NULL))
20222 {
20223 /* Avoid creating a duplicate psymtab for PST.
20224 We do this by comparing INCLUDE_NAME and PST_FILENAME.
20225 Before we do the comparison, however, we need to account
20226 for DIR_NAME and COMP_DIR.
20227 First prepend dir_name (if non-NULL). If we still don't
20228 have an absolute path prepend comp_dir (if non-NULL).
20229 However, the directory we record in the include-file's
20230 psymtab does not contain COMP_DIR (to match the
20231 corresponding symtab(s)).
20232
20233 Example:
20234
20235 bash$ cd /tmp
20236 bash$ gcc -g ./hello.c
20237 include_name = "hello.c"
20238 dir_name = "."
20239 DW_AT_comp_dir = comp_dir = "/tmp"
5f52445b
YQ
20240 DW_AT_name = "./hello.c"
20241
20242 */
c6da4cef
DE
20243
20244 if (dir_name != NULL)
20245 {
c89b44cd
TT
20246 name_holder->reset (concat (dir_name, SLASH_STRING,
20247 include_name, (char *) NULL));
20248 include_name = name_holder->get ();
c6da4cef 20249 include_name_to_compare = include_name;
c6da4cef
DE
20250 }
20251 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
20252 {
c89b44cd
TT
20253 hold_compare.reset (concat (comp_dir, SLASH_STRING,
20254 include_name, (char *) NULL));
20255 include_name_to_compare = hold_compare.get ();
c6da4cef
DE
20256 }
20257 }
20258
20259 pst_filename = pst->filename;
c89b44cd 20260 gdb::unique_xmalloc_ptr<char> copied_name;
c6da4cef
DE
20261 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
20262 {
c89b44cd
TT
20263 copied_name.reset (concat (pst->dirname, SLASH_STRING,
20264 pst_filename, (char *) NULL));
20265 pst_filename = copied_name.get ();
c6da4cef
DE
20266 }
20267
1e3fad37 20268 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
c6da4cef 20269
c6da4cef
DE
20270 if (file_is_pst)
20271 return NULL;
20272 return include_name;
20273}
20274
d9b3de22
DE
20275/* State machine to track the state of the line number program. */
20276
6f77053d 20277class lnp_state_machine
d9b3de22 20278{
6f77053d
PA
20279public:
20280 /* Initialize a machine state for the start of a line number
20281 program. */
20282 lnp_state_machine (gdbarch *arch, line_header *lh, bool record_lines_p);
20283
8c43009f
PA
20284 file_entry *current_file ()
20285 {
20286 /* lh->file_names is 0-based, but the file name numbers in the
20287 statement program are 1-based. */
6f77053d
PA
20288 return m_line_header->file_name_at (m_file);
20289 }
20290
20291 /* Record the line in the state machine. END_SEQUENCE is true if
20292 we're processing the end of a sequence. */
20293 void record_line (bool end_sequence);
20294
20295 /* Check address and if invalid nop-out the rest of the lines in this
20296 sequence. */
20297 void check_line_address (struct dwarf2_cu *cu,
20298 const gdb_byte *line_ptr,
20299 CORE_ADDR lowpc, CORE_ADDR address);
20300
20301 void handle_set_discriminator (unsigned int discriminator)
20302 {
20303 m_discriminator = discriminator;
20304 m_line_has_non_zero_discriminator |= discriminator != 0;
20305 }
20306
20307 /* Handle DW_LNE_set_address. */
20308 void handle_set_address (CORE_ADDR baseaddr, CORE_ADDR address)
20309 {
20310 m_op_index = 0;
20311 address += baseaddr;
20312 m_address = gdbarch_adjust_dwarf2_line (m_gdbarch, address, false);
20313 }
20314
20315 /* Handle DW_LNS_advance_pc. */
20316 void handle_advance_pc (CORE_ADDR adjust);
20317
20318 /* Handle a special opcode. */
20319 void handle_special_opcode (unsigned char op_code);
20320
20321 /* Handle DW_LNS_advance_line. */
20322 void handle_advance_line (int line_delta)
20323 {
20324 advance_line (line_delta);
20325 }
20326
20327 /* Handle DW_LNS_set_file. */
20328 void handle_set_file (file_name_index file);
20329
20330 /* Handle DW_LNS_negate_stmt. */
20331 void handle_negate_stmt ()
20332 {
20333 m_is_stmt = !m_is_stmt;
20334 }
20335
20336 /* Handle DW_LNS_const_add_pc. */
20337 void handle_const_add_pc ();
20338
20339 /* Handle DW_LNS_fixed_advance_pc. */
20340 void handle_fixed_advance_pc (CORE_ADDR addr_adj)
20341 {
20342 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20343 m_op_index = 0;
20344 }
20345
20346 /* Handle DW_LNS_copy. */
20347 void handle_copy ()
20348 {
20349 record_line (false);
20350 m_discriminator = 0;
20351 }
20352
20353 /* Handle DW_LNE_end_sequence. */
20354 void handle_end_sequence ()
20355 {
20356 m_record_line_callback = ::record_line;
20357 }
20358
20359private:
20360 /* Advance the line by LINE_DELTA. */
20361 void advance_line (int line_delta)
20362 {
20363 m_line += line_delta;
20364
20365 if (line_delta != 0)
20366 m_line_has_non_zero_discriminator = m_discriminator != 0;
8c43009f
PA
20367 }
20368
6f77053d
PA
20369 gdbarch *m_gdbarch;
20370
20371 /* True if we're recording lines.
20372 Otherwise we're building partial symtabs and are just interested in
20373 finding include files mentioned by the line number program. */
20374 bool m_record_lines_p;
20375
8c43009f 20376 /* The line number header. */
6f77053d 20377 line_header *m_line_header;
8c43009f 20378
6f77053d
PA
20379 /* These are part of the standard DWARF line number state machine,
20380 and initialized according to the DWARF spec. */
d9b3de22 20381
6f77053d 20382 unsigned char m_op_index = 0;
8c43009f 20383 /* The line table index (1-based) of the current file. */
6f77053d
PA
20384 file_name_index m_file = (file_name_index) 1;
20385 unsigned int m_line = 1;
20386
20387 /* These are initialized in the constructor. */
20388
20389 CORE_ADDR m_address;
20390 bool m_is_stmt;
20391 unsigned int m_discriminator;
d9b3de22
DE
20392
20393 /* Additional bits of state we need to track. */
20394
20395 /* The last file that we called dwarf2_start_subfile for.
20396 This is only used for TLLs. */
6f77053d 20397 unsigned int m_last_file = 0;
d9b3de22 20398 /* The last file a line number was recorded for. */
6f77053d 20399 struct subfile *m_last_subfile = NULL;
d9b3de22
DE
20400
20401 /* The function to call to record a line. */
6f77053d 20402 record_line_ftype *m_record_line_callback = NULL;
d9b3de22
DE
20403
20404 /* The last line number that was recorded, used to coalesce
20405 consecutive entries for the same line. This can happen, for
20406 example, when discriminators are present. PR 17276. */
6f77053d
PA
20407 unsigned int m_last_line = 0;
20408 bool m_line_has_non_zero_discriminator = false;
8c43009f 20409};
d9b3de22 20410
6f77053d
PA
20411void
20412lnp_state_machine::handle_advance_pc (CORE_ADDR adjust)
20413{
20414 CORE_ADDR addr_adj = (((m_op_index + adjust)
20415 / m_line_header->maximum_ops_per_instruction)
20416 * m_line_header->minimum_instruction_length);
20417 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20418 m_op_index = ((m_op_index + adjust)
20419 % m_line_header->maximum_ops_per_instruction);
20420}
d9b3de22 20421
6f77053d
PA
20422void
20423lnp_state_machine::handle_special_opcode (unsigned char op_code)
d9b3de22 20424{
6f77053d
PA
20425 unsigned char adj_opcode = op_code - m_line_header->opcode_base;
20426 CORE_ADDR addr_adj = (((m_op_index
20427 + (adj_opcode / m_line_header->line_range))
20428 / m_line_header->maximum_ops_per_instruction)
20429 * m_line_header->minimum_instruction_length);
20430 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20431 m_op_index = ((m_op_index + (adj_opcode / m_line_header->line_range))
20432 % m_line_header->maximum_ops_per_instruction);
d9b3de22 20433
6f77053d
PA
20434 int line_delta = (m_line_header->line_base
20435 + (adj_opcode % m_line_header->line_range));
20436 advance_line (line_delta);
20437 record_line (false);
20438 m_discriminator = 0;
20439}
d9b3de22 20440
6f77053d
PA
20441void
20442lnp_state_machine::handle_set_file (file_name_index file)
20443{
20444 m_file = file;
20445
20446 const file_entry *fe = current_file ();
20447 if (fe == NULL)
20448 dwarf2_debug_line_missing_file_complaint ();
20449 else if (m_record_lines_p)
20450 {
20451 const char *dir = fe->include_dir (m_line_header);
20452
20453 m_last_subfile = current_subfile;
20454 m_line_has_non_zero_discriminator = m_discriminator != 0;
20455 dwarf2_start_subfile (fe->name, dir);
20456 }
20457}
20458
20459void
20460lnp_state_machine::handle_const_add_pc ()
20461{
20462 CORE_ADDR adjust
20463 = (255 - m_line_header->opcode_base) / m_line_header->line_range;
20464
20465 CORE_ADDR addr_adj
20466 = (((m_op_index + adjust)
20467 / m_line_header->maximum_ops_per_instruction)
20468 * m_line_header->minimum_instruction_length);
20469
20470 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20471 m_op_index = ((m_op_index + adjust)
20472 % m_line_header->maximum_ops_per_instruction);
20473}
d9b3de22 20474
c91513d8
PP
20475/* Ignore this record_line request. */
20476
20477static void
20478noop_record_line (struct subfile *subfile, int line, CORE_ADDR pc)
20479{
20480 return;
20481}
20482
a05a36a5
DE
20483/* Return non-zero if we should add LINE to the line number table.
20484 LINE is the line to add, LAST_LINE is the last line that was added,
20485 LAST_SUBFILE is the subfile for LAST_LINE.
20486 LINE_HAS_NON_ZERO_DISCRIMINATOR is non-zero if LINE has ever
20487 had a non-zero discriminator.
20488
20489 We have to be careful in the presence of discriminators.
20490 E.g., for this line:
20491
20492 for (i = 0; i < 100000; i++);
20493
20494 clang can emit four line number entries for that one line,
20495 each with a different discriminator.
20496 See gdb.dwarf2/dw2-single-line-discriminators.exp for an example.
20497
20498 However, we want gdb to coalesce all four entries into one.
20499 Otherwise the user could stepi into the middle of the line and
20500 gdb would get confused about whether the pc really was in the
20501 middle of the line.
20502
20503 Things are further complicated by the fact that two consecutive
20504 line number entries for the same line is a heuristic used by gcc
20505 to denote the end of the prologue. So we can't just discard duplicate
20506 entries, we have to be selective about it. The heuristic we use is
20507 that we only collapse consecutive entries for the same line if at least
20508 one of those entries has a non-zero discriminator. PR 17276.
20509
20510 Note: Addresses in the line number state machine can never go backwards
20511 within one sequence, thus this coalescing is ok. */
20512
20513static int
20514dwarf_record_line_p (unsigned int line, unsigned int last_line,
20515 int line_has_non_zero_discriminator,
20516 struct subfile *last_subfile)
20517{
20518 if (current_subfile != last_subfile)
20519 return 1;
20520 if (line != last_line)
20521 return 1;
20522 /* Same line for the same file that we've seen already.
20523 As a last check, for pr 17276, only record the line if the line
20524 has never had a non-zero discriminator. */
20525 if (!line_has_non_zero_discriminator)
20526 return 1;
20527 return 0;
20528}
20529
252a6764
DE
20530/* Use P_RECORD_LINE to record line number LINE beginning at address ADDRESS
20531 in the line table of subfile SUBFILE. */
20532
20533static void
d9b3de22
DE
20534dwarf_record_line_1 (struct gdbarch *gdbarch, struct subfile *subfile,
20535 unsigned int line, CORE_ADDR address,
20536 record_line_ftype p_record_line)
252a6764
DE
20537{
20538 CORE_ADDR addr = gdbarch_addr_bits_remove (gdbarch, address);
20539
27e0867f
DE
20540 if (dwarf_line_debug)
20541 {
20542 fprintf_unfiltered (gdb_stdlog,
20543 "Recording line %u, file %s, address %s\n",
20544 line, lbasename (subfile->name),
20545 paddress (gdbarch, address));
20546 }
20547
d5962de5 20548 (*p_record_line) (subfile, line, addr);
252a6764
DE
20549}
20550
20551/* Subroutine of dwarf_decode_lines_1 to simplify it.
20552 Mark the end of a set of line number records.
d9b3de22 20553 The arguments are the same as for dwarf_record_line_1.
252a6764
DE
20554 If SUBFILE is NULL the request is ignored. */
20555
20556static void
20557dwarf_finish_line (struct gdbarch *gdbarch, struct subfile *subfile,
20558 CORE_ADDR address, record_line_ftype p_record_line)
20559{
27e0867f
DE
20560 if (subfile == NULL)
20561 return;
20562
20563 if (dwarf_line_debug)
20564 {
20565 fprintf_unfiltered (gdb_stdlog,
20566 "Finishing current line, file %s, address %s\n",
20567 lbasename (subfile->name),
20568 paddress (gdbarch, address));
20569 }
20570
d9b3de22
DE
20571 dwarf_record_line_1 (gdbarch, subfile, 0, address, p_record_line);
20572}
20573
6f77053d
PA
20574void
20575lnp_state_machine::record_line (bool end_sequence)
d9b3de22 20576{
d9b3de22
DE
20577 if (dwarf_line_debug)
20578 {
20579 fprintf_unfiltered (gdb_stdlog,
20580 "Processing actual line %u: file %u,"
20581 " address %s, is_stmt %u, discrim %u\n",
6f77053d
PA
20582 m_line, to_underlying (m_file),
20583 paddress (m_gdbarch, m_address),
20584 m_is_stmt, m_discriminator);
d9b3de22
DE
20585 }
20586
6f77053d 20587 file_entry *fe = current_file ();
8c43009f
PA
20588
20589 if (fe == NULL)
d9b3de22
DE
20590 dwarf2_debug_line_missing_file_complaint ();
20591 /* For now we ignore lines not starting on an instruction boundary.
20592 But not when processing end_sequence for compatibility with the
20593 previous version of the code. */
6f77053d 20594 else if (m_op_index == 0 || end_sequence)
d9b3de22 20595 {
8c43009f 20596 fe->included_p = 1;
6f77053d 20597 if (m_record_lines_p && m_is_stmt)
d9b3de22 20598 {
6f77053d 20599 if (m_last_subfile != current_subfile || end_sequence)
d9b3de22 20600 {
6f77053d
PA
20601 dwarf_finish_line (m_gdbarch, m_last_subfile,
20602 m_address, m_record_line_callback);
d9b3de22
DE
20603 }
20604
20605 if (!end_sequence)
20606 {
6f77053d
PA
20607 if (dwarf_record_line_p (m_line, m_last_line,
20608 m_line_has_non_zero_discriminator,
20609 m_last_subfile))
d9b3de22 20610 {
6f77053d
PA
20611 dwarf_record_line_1 (m_gdbarch, current_subfile,
20612 m_line, m_address,
20613 m_record_line_callback);
d9b3de22 20614 }
6f77053d
PA
20615 m_last_subfile = current_subfile;
20616 m_last_line = m_line;
d9b3de22
DE
20617 }
20618 }
20619 }
20620}
20621
6f77053d
PA
20622lnp_state_machine::lnp_state_machine (gdbarch *arch, line_header *lh,
20623 bool record_lines_p)
d9b3de22 20624{
6f77053d
PA
20625 m_gdbarch = arch;
20626 m_record_lines_p = record_lines_p;
20627 m_line_header = lh;
d9b3de22 20628
6f77053d 20629 m_record_line_callback = ::record_line;
d9b3de22 20630
d9b3de22
DE
20631 /* Call `gdbarch_adjust_dwarf2_line' on the initial 0 address as if there
20632 was a line entry for it so that the backend has a chance to adjust it
20633 and also record it in case it needs it. This is currently used by MIPS
20634 code, cf. `mips_adjust_dwarf2_line'. */
6f77053d
PA
20635 m_address = gdbarch_adjust_dwarf2_line (arch, 0, 0);
20636 m_is_stmt = lh->default_is_stmt;
20637 m_discriminator = 0;
252a6764
DE
20638}
20639
6f77053d
PA
20640void
20641lnp_state_machine::check_line_address (struct dwarf2_cu *cu,
20642 const gdb_byte *line_ptr,
20643 CORE_ADDR lowpc, CORE_ADDR address)
924c2928
DE
20644{
20645 /* If address < lowpc then it's not a usable value, it's outside the
20646 pc range of the CU. However, we restrict the test to only address
20647 values of zero to preserve GDB's previous behaviour which is to
20648 handle the specific case of a function being GC'd by the linker. */
20649
20650 if (address == 0 && address < lowpc)
20651 {
20652 /* This line table is for a function which has been
20653 GCd by the linker. Ignore it. PR gdb/12528 */
20654
518817b3 20655 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
924c2928
DE
20656 long line_offset = line_ptr - get_debug_line_section (cu)->buffer;
20657
20658 complaint (&symfile_complaints,
20659 _(".debug_line address at offset 0x%lx is 0 [in module %s]"),
20660 line_offset, objfile_name (objfile));
6f77053d
PA
20661 m_record_line_callback = noop_record_line;
20662 /* Note: record_line_callback is left as noop_record_line until
20663 we see DW_LNE_end_sequence. */
924c2928
DE
20664 }
20665}
20666
f3f5162e 20667/* Subroutine of dwarf_decode_lines to simplify it.
d9b3de22
DE
20668 Process the line number information in LH.
20669 If DECODE_FOR_PST_P is non-zero, all we do is process the line number
20670 program in order to set included_p for every referenced header. */
debd256d 20671
c906108c 20672static void
43f3e411
DE
20673dwarf_decode_lines_1 (struct line_header *lh, struct dwarf2_cu *cu,
20674 const int decode_for_pst_p, CORE_ADDR lowpc)
c906108c 20675{
d521ce57
TT
20676 const gdb_byte *line_ptr, *extended_end;
20677 const gdb_byte *line_end;
a8c50c1f 20678 unsigned int bytes_read, extended_len;
699ca60a 20679 unsigned char op_code, extended_op;
e142c38c 20680 CORE_ADDR baseaddr;
518817b3 20681 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
f3f5162e 20682 bfd *abfd = objfile->obfd;
fbf65064 20683 struct gdbarch *gdbarch = get_objfile_arch (objfile);
6f77053d
PA
20684 /* True if we're recording line info (as opposed to building partial
20685 symtabs and just interested in finding include files mentioned by
20686 the line number program). */
20687 bool record_lines_p = !decode_for_pst_p;
e142c38c
DJ
20688
20689 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 20690
debd256d
JB
20691 line_ptr = lh->statement_program_start;
20692 line_end = lh->statement_program_end;
c906108c
SS
20693
20694 /* Read the statement sequences until there's nothing left. */
20695 while (line_ptr < line_end)
20696 {
6f77053d
PA
20697 /* The DWARF line number program state machine. Reset the state
20698 machine at the start of each sequence. */
20699 lnp_state_machine state_machine (gdbarch, lh, record_lines_p);
20700 bool end_sequence = false;
d9b3de22 20701
8c43009f 20702 if (record_lines_p)
c906108c 20703 {
8c43009f
PA
20704 /* Start a subfile for the current file of the state
20705 machine. */
20706 const file_entry *fe = state_machine.current_file ();
20707
20708 if (fe != NULL)
20709 dwarf2_start_subfile (fe->name, fe->include_dir (lh));
c906108c
SS
20710 }
20711
a738430d 20712 /* Decode the table. */
d9b3de22 20713 while (line_ptr < line_end && !end_sequence)
c906108c
SS
20714 {
20715 op_code = read_1_byte (abfd, line_ptr);
20716 line_ptr += 1;
9aa1fe7e 20717
debd256d 20718 if (op_code >= lh->opcode_base)
6e70227d 20719 {
8e07a239 20720 /* Special opcode. */
6f77053d 20721 state_machine.handle_special_opcode (op_code);
9aa1fe7e
GK
20722 }
20723 else switch (op_code)
c906108c
SS
20724 {
20725 case DW_LNS_extended_op:
3e43a32a
MS
20726 extended_len = read_unsigned_leb128 (abfd, line_ptr,
20727 &bytes_read);
473b7be6 20728 line_ptr += bytes_read;
a8c50c1f 20729 extended_end = line_ptr + extended_len;
c906108c
SS
20730 extended_op = read_1_byte (abfd, line_ptr);
20731 line_ptr += 1;
20732 switch (extended_op)
20733 {
20734 case DW_LNE_end_sequence:
6f77053d
PA
20735 state_machine.handle_end_sequence ();
20736 end_sequence = true;
c906108c
SS
20737 break;
20738 case DW_LNE_set_address:
d9b3de22
DE
20739 {
20740 CORE_ADDR address
20741 = read_address (abfd, line_ptr, cu, &bytes_read);
d9b3de22 20742 line_ptr += bytes_read;
6f77053d
PA
20743
20744 state_machine.check_line_address (cu, line_ptr,
20745 lowpc, address);
20746 state_machine.handle_set_address (baseaddr, address);
d9b3de22 20747 }
c906108c
SS
20748 break;
20749 case DW_LNE_define_file:
debd256d 20750 {
d521ce57 20751 const char *cur_file;
ecfb656c
PA
20752 unsigned int mod_time, length;
20753 dir_index dindex;
6e70227d 20754
3e43a32a
MS
20755 cur_file = read_direct_string (abfd, line_ptr,
20756 &bytes_read);
debd256d 20757 line_ptr += bytes_read;
ecfb656c 20758 dindex = (dir_index)
debd256d
JB
20759 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20760 line_ptr += bytes_read;
20761 mod_time =
20762 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20763 line_ptr += bytes_read;
20764 length =
20765 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20766 line_ptr += bytes_read;
ecfb656c 20767 lh->add_file_name (cur_file, dindex, mod_time, length);
debd256d 20768 }
c906108c 20769 break;
d0c6ba3d 20770 case DW_LNE_set_discriminator:
6f77053d
PA
20771 {
20772 /* The discriminator is not interesting to the
20773 debugger; just ignore it. We still need to
20774 check its value though:
20775 if there are consecutive entries for the same
20776 (non-prologue) line we want to coalesce them.
20777 PR 17276. */
20778 unsigned int discr
20779 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20780 line_ptr += bytes_read;
20781
20782 state_machine.handle_set_discriminator (discr);
20783 }
d0c6ba3d 20784 break;
c906108c 20785 default:
4d3c2250 20786 complaint (&symfile_complaints,
e2e0b3e5 20787 _("mangled .debug_line section"));
debd256d 20788 return;
c906108c 20789 }
a8c50c1f
DJ
20790 /* Make sure that we parsed the extended op correctly. If e.g.
20791 we expected a different address size than the producer used,
20792 we may have read the wrong number of bytes. */
20793 if (line_ptr != extended_end)
20794 {
20795 complaint (&symfile_complaints,
20796 _("mangled .debug_line section"));
20797 return;
20798 }
c906108c
SS
20799 break;
20800 case DW_LNS_copy:
6f77053d 20801 state_machine.handle_copy ();
c906108c
SS
20802 break;
20803 case DW_LNS_advance_pc:
2dc7f7b3
TT
20804 {
20805 CORE_ADDR adjust
20806 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
2dc7f7b3 20807 line_ptr += bytes_read;
6f77053d
PA
20808
20809 state_machine.handle_advance_pc (adjust);
2dc7f7b3 20810 }
c906108c
SS
20811 break;
20812 case DW_LNS_advance_line:
a05a36a5
DE
20813 {
20814 int line_delta
20815 = read_signed_leb128 (abfd, line_ptr, &bytes_read);
a05a36a5 20816 line_ptr += bytes_read;
6f77053d
PA
20817
20818 state_machine.handle_advance_line (line_delta);
a05a36a5 20819 }
c906108c
SS
20820 break;
20821 case DW_LNS_set_file:
d9b3de22 20822 {
6f77053d 20823 file_name_index file
ecfb656c
PA
20824 = (file_name_index) read_unsigned_leb128 (abfd, line_ptr,
20825 &bytes_read);
d9b3de22 20826 line_ptr += bytes_read;
8c43009f 20827
6f77053d 20828 state_machine.handle_set_file (file);
d9b3de22 20829 }
c906108c
SS
20830 break;
20831 case DW_LNS_set_column:
0ad93d4f 20832 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
c906108c
SS
20833 line_ptr += bytes_read;
20834 break;
20835 case DW_LNS_negate_stmt:
6f77053d 20836 state_machine.handle_negate_stmt ();
c906108c
SS
20837 break;
20838 case DW_LNS_set_basic_block:
c906108c 20839 break;
c2c6d25f
JM
20840 /* Add to the address register of the state machine the
20841 address increment value corresponding to special opcode
a738430d
MK
20842 255. I.e., this value is scaled by the minimum
20843 instruction length since special opcode 255 would have
b021a221 20844 scaled the increment. */
c906108c 20845 case DW_LNS_const_add_pc:
6f77053d 20846 state_machine.handle_const_add_pc ();
c906108c
SS
20847 break;
20848 case DW_LNS_fixed_advance_pc:
3e29f34a 20849 {
6f77053d 20850 CORE_ADDR addr_adj = read_2_bytes (abfd, line_ptr);
3e29f34a 20851 line_ptr += 2;
6f77053d
PA
20852
20853 state_machine.handle_fixed_advance_pc (addr_adj);
3e29f34a 20854 }
c906108c 20855 break;
9aa1fe7e 20856 default:
a738430d
MK
20857 {
20858 /* Unknown standard opcode, ignore it. */
9aa1fe7e 20859 int i;
a738430d 20860
debd256d 20861 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
9aa1fe7e
GK
20862 {
20863 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20864 line_ptr += bytes_read;
20865 }
20866 }
c906108c
SS
20867 }
20868 }
d9b3de22
DE
20869
20870 if (!end_sequence)
20871 dwarf2_debug_line_missing_end_sequence_complaint ();
20872
20873 /* We got a DW_LNE_end_sequence (or we ran off the end of the buffer,
20874 in which case we still finish recording the last line). */
6f77053d 20875 state_machine.record_line (true);
c906108c 20876 }
f3f5162e
DE
20877}
20878
20879/* Decode the Line Number Program (LNP) for the given line_header
20880 structure and CU. The actual information extracted and the type
20881 of structures created from the LNP depends on the value of PST.
20882
20883 1. If PST is NULL, then this procedure uses the data from the program
20884 to create all necessary symbol tables, and their linetables.
20885
20886 2. If PST is not NULL, this procedure reads the program to determine
20887 the list of files included by the unit represented by PST, and
20888 builds all the associated partial symbol tables.
20889
20890 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
20891 It is used for relative paths in the line table.
20892 NOTE: When processing partial symtabs (pst != NULL),
20893 comp_dir == pst->dirname.
20894
20895 NOTE: It is important that psymtabs have the same file name (via strcmp)
20896 as the corresponding symtab. Since COMP_DIR is not used in the name of the
20897 symtab we don't use it in the name of the psymtabs we create.
20898 E.g. expand_line_sal requires this when finding psymtabs to expand.
c3b7b696
YQ
20899 A good testcase for this is mb-inline.exp.
20900
527f3840
JK
20901 LOWPC is the lowest address in CU (or 0 if not known).
20902
20903 Boolean DECODE_MAPPING specifies we need to fully decode .debug_line
20904 for its PC<->lines mapping information. Otherwise only the filename
20905 table is read in. */
f3f5162e
DE
20906
20907static void
20908dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
c3b7b696 20909 struct dwarf2_cu *cu, struct partial_symtab *pst,
527f3840 20910 CORE_ADDR lowpc, int decode_mapping)
f3f5162e 20911{
518817b3 20912 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
f3f5162e 20913 const int decode_for_pst_p = (pst != NULL);
f3f5162e 20914
527f3840
JK
20915 if (decode_mapping)
20916 dwarf_decode_lines_1 (lh, cu, decode_for_pst_p, lowpc);
aaa75496
JB
20917
20918 if (decode_for_pst_p)
20919 {
20920 int file_index;
20921
20922 /* Now that we're done scanning the Line Header Program, we can
20923 create the psymtab of each included file. */
fff8551c 20924 for (file_index = 0; file_index < lh->file_names.size (); file_index++)
aaa75496
JB
20925 if (lh->file_names[file_index].included_p == 1)
20926 {
c89b44cd 20927 gdb::unique_xmalloc_ptr<char> name_holder;
d521ce57 20928 const char *include_name =
c89b44cd
TT
20929 psymtab_include_file_name (lh, file_index, pst, comp_dir,
20930 &name_holder);
c6da4cef 20931 if (include_name != NULL)
aaa75496
JB
20932 dwarf2_create_include_psymtab (include_name, pst, objfile);
20933 }
20934 }
cb1df416
DJ
20935 else
20936 {
20937 /* Make sure a symtab is created for every file, even files
20938 which contain only variables (i.e. no code with associated
20939 line numbers). */
43f3e411 20940 struct compunit_symtab *cust = buildsym_compunit_symtab ();
cb1df416 20941 int i;
cb1df416 20942
fff8551c 20943 for (i = 0; i < lh->file_names.size (); i++)
cb1df416 20944 {
8c43009f 20945 file_entry &fe = lh->file_names[i];
9a619af0 20946
8c43009f 20947 dwarf2_start_subfile (fe.name, fe.include_dir (lh));
cb1df416 20948
cb1df416 20949 if (current_subfile->symtab == NULL)
43f3e411
DE
20950 {
20951 current_subfile->symtab
20952 = allocate_symtab (cust, current_subfile->name);
20953 }
8c43009f 20954 fe.symtab = current_subfile->symtab;
cb1df416
DJ
20955 }
20956 }
c906108c
SS
20957}
20958
20959/* Start a subfile for DWARF. FILENAME is the name of the file and
20960 DIRNAME the name of the source directory which contains FILENAME
4d663531 20961 or NULL if not known.
c906108c
SS
20962 This routine tries to keep line numbers from identical absolute and
20963 relative file names in a common subfile.
20964
20965 Using the `list' example from the GDB testsuite, which resides in
20966 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
20967 of /srcdir/list0.c yields the following debugging information for list0.c:
20968
c5aa993b 20969 DW_AT_name: /srcdir/list0.c
4d663531 20970 DW_AT_comp_dir: /compdir
357e46e7 20971 files.files[0].name: list0.h
c5aa993b 20972 files.files[0].dir: /srcdir
357e46e7 20973 files.files[1].name: list0.c
c5aa993b 20974 files.files[1].dir: /srcdir
c906108c
SS
20975
20976 The line number information for list0.c has to end up in a single
4f1520fb
FR
20977 subfile, so that `break /srcdir/list0.c:1' works as expected.
20978 start_subfile will ensure that this happens provided that we pass the
20979 concatenation of files.files[1].dir and files.files[1].name as the
20980 subfile's name. */
c906108c
SS
20981
20982static void
4d663531 20983dwarf2_start_subfile (const char *filename, const char *dirname)
c906108c 20984{
d521ce57 20985 char *copy = NULL;
4f1520fb 20986
4d663531 20987 /* In order not to lose the line information directory,
4f1520fb
FR
20988 we concatenate it to the filename when it makes sense.
20989 Note that the Dwarf3 standard says (speaking of filenames in line
20990 information): ``The directory index is ignored for file names
20991 that represent full path names''. Thus ignoring dirname in the
20992 `else' branch below isn't an issue. */
c906108c 20993
d5166ae1 20994 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
d521ce57
TT
20995 {
20996 copy = concat (dirname, SLASH_STRING, filename, (char *)NULL);
20997 filename = copy;
20998 }
c906108c 20999
4d663531 21000 start_subfile (filename);
4f1520fb 21001
d521ce57
TT
21002 if (copy != NULL)
21003 xfree (copy);
c906108c
SS
21004}
21005
f4dc4d17
DE
21006/* Start a symtab for DWARF.
21007 NAME, COMP_DIR, LOW_PC are passed to start_symtab. */
21008
43f3e411 21009static struct compunit_symtab *
f4dc4d17 21010dwarf2_start_symtab (struct dwarf2_cu *cu,
15d034d0 21011 const char *name, const char *comp_dir, CORE_ADDR low_pc)
f4dc4d17 21012{
43f3e411 21013 struct compunit_symtab *cust
518817b3
SM
21014 = start_symtab (cu->per_cu->dwarf2_per_objfile->objfile, name, comp_dir,
21015 low_pc, cu->language);
43f3e411 21016
f4dc4d17
DE
21017 record_debugformat ("DWARF 2");
21018 record_producer (cu->producer);
21019
21020 /* We assume that we're processing GCC output. */
21021 processing_gcc_compilation = 2;
21022
4d4ec4e5 21023 cu->processing_has_namespace_info = 0;
43f3e411
DE
21024
21025 return cust;
f4dc4d17
DE
21026}
21027
4c2df51b
DJ
21028static void
21029var_decode_location (struct attribute *attr, struct symbol *sym,
e7c27a73 21030 struct dwarf2_cu *cu)
4c2df51b 21031{
518817b3 21032 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
e7c27a73
DJ
21033 struct comp_unit_head *cu_header = &cu->header;
21034
4c2df51b
DJ
21035 /* NOTE drow/2003-01-30: There used to be a comment and some special
21036 code here to turn a symbol with DW_AT_external and a
21037 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
21038 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
21039 with some versions of binutils) where shared libraries could have
21040 relocations against symbols in their debug information - the
21041 minimal symbol would have the right address, but the debug info
21042 would not. It's no longer necessary, because we will explicitly
21043 apply relocations when we read in the debug information now. */
21044
21045 /* A DW_AT_location attribute with no contents indicates that a
21046 variable has been optimized away. */
21047 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
21048 {
f1e6e072 21049 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
4c2df51b
DJ
21050 return;
21051 }
21052
21053 /* Handle one degenerate form of location expression specially, to
21054 preserve GDB's previous behavior when section offsets are
3019eac3
DE
21055 specified. If this is just a DW_OP_addr or DW_OP_GNU_addr_index
21056 then mark this symbol as LOC_STATIC. */
4c2df51b
DJ
21057
21058 if (attr_form_is_block (attr)
3019eac3
DE
21059 && ((DW_BLOCK (attr)->data[0] == DW_OP_addr
21060 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size)
21061 || (DW_BLOCK (attr)->data[0] == DW_OP_GNU_addr_index
21062 && (DW_BLOCK (attr)->size
21063 == 1 + leb128_size (&DW_BLOCK (attr)->data[1])))))
4c2df51b 21064 {
891d2f0b 21065 unsigned int dummy;
4c2df51b 21066
3019eac3
DE
21067 if (DW_BLOCK (attr)->data[0] == DW_OP_addr)
21068 SYMBOL_VALUE_ADDRESS (sym) =
21069 read_address (objfile->obfd, DW_BLOCK (attr)->data + 1, cu, &dummy);
21070 else
21071 SYMBOL_VALUE_ADDRESS (sym) =
21072 read_addr_index_from_leb128 (cu, DW_BLOCK (attr)->data + 1, &dummy);
f1e6e072 21073 SYMBOL_ACLASS_INDEX (sym) = LOC_STATIC;
4c2df51b
DJ
21074 fixup_symbol_section (sym, objfile);
21075 SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (objfile->section_offsets,
21076 SYMBOL_SECTION (sym));
4c2df51b
DJ
21077 return;
21078 }
21079
21080 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
21081 expression evaluator, and use LOC_COMPUTED only when necessary
21082 (i.e. when the value of a register or memory location is
21083 referenced, or a thread-local block, etc.). Then again, it might
21084 not be worthwhile. I'm assuming that it isn't unless performance
21085 or memory numbers show me otherwise. */
21086
f1e6e072 21087 dwarf2_symbol_mark_computed (attr, sym, cu, 0);
8be455d7 21088
f1e6e072 21089 if (SYMBOL_COMPUTED_OPS (sym)->location_has_loclist)
8be455d7 21090 cu->has_loclist = 1;
4c2df51b
DJ
21091}
21092
c906108c
SS
21093/* Given a pointer to a DWARF information entry, figure out if we need
21094 to make a symbol table entry for it, and if so, create a new entry
21095 and return a pointer to it.
21096 If TYPE is NULL, determine symbol type from the die, otherwise
34eaf542
TT
21097 used the passed type.
21098 If SPACE is not NULL, use it to hold the new symbol. If it is
21099 NULL, allocate a new symbol on the objfile's obstack. */
c906108c
SS
21100
21101static struct symbol *
5e2db402
TT
21102new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
21103 struct symbol *space)
c906108c 21104{
518817b3
SM
21105 struct dwarf2_per_objfile *dwarf2_per_objfile
21106 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 21107 struct objfile *objfile = dwarf2_per_objfile->objfile;
3e29f34a 21108 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c 21109 struct symbol *sym = NULL;
15d034d0 21110 const char *name;
c906108c
SS
21111 struct attribute *attr = NULL;
21112 struct attribute *attr2 = NULL;
e142c38c 21113 CORE_ADDR baseaddr;
e37fd15a
SW
21114 struct pending **list_to_add = NULL;
21115
edb3359d 21116 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
e142c38c
DJ
21117
21118 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 21119
94af9270 21120 name = dwarf2_name (die, cu);
c906108c
SS
21121 if (name)
21122 {
94af9270 21123 const char *linkagename;
34eaf542 21124 int suppress_add = 0;
94af9270 21125
34eaf542
TT
21126 if (space)
21127 sym = space;
21128 else
e623cf5d 21129 sym = allocate_symbol (objfile);
c906108c 21130 OBJSTAT (objfile, n_syms++);
2de7ced7
DJ
21131
21132 /* Cache this symbol's name and the name's demangled form (if any). */
f85f34ed 21133 SYMBOL_SET_LANGUAGE (sym, cu->language, &objfile->objfile_obstack);
94af9270
KS
21134 linkagename = dwarf2_physname (name, die, cu);
21135 SYMBOL_SET_NAMES (sym, linkagename, strlen (linkagename), 0, objfile);
c906108c 21136
f55ee35c
JK
21137 /* Fortran does not have mangling standard and the mangling does differ
21138 between gfortran, iFort etc. */
21139 if (cu->language == language_fortran
b250c185 21140 && symbol_get_demangled_name (&(sym->ginfo)) == NULL)
29df156d 21141 symbol_set_demangled_name (&(sym->ginfo),
cfc594ee 21142 dwarf2_full_name (name, die, cu),
29df156d 21143 NULL);
f55ee35c 21144
c906108c 21145 /* Default assumptions.
c5aa993b 21146 Use the passed type or decode it from the die. */
176620f1 21147 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
f1e6e072 21148 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
c906108c
SS
21149 if (type != NULL)
21150 SYMBOL_TYPE (sym) = type;
21151 else
e7c27a73 21152 SYMBOL_TYPE (sym) = die_type (die, cu);
edb3359d
DJ
21153 attr = dwarf2_attr (die,
21154 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
21155 cu);
c906108c
SS
21156 if (attr)
21157 {
21158 SYMBOL_LINE (sym) = DW_UNSND (attr);
21159 }
cb1df416 21160
edb3359d
DJ
21161 attr = dwarf2_attr (die,
21162 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
21163 cu);
cb1df416
DJ
21164 if (attr)
21165 {
ecfb656c 21166 file_name_index file_index = (file_name_index) DW_UNSND (attr);
8c43009f 21167 struct file_entry *fe;
9a619af0 21168
ecfb656c
PA
21169 if (cu->line_header != NULL)
21170 fe = cu->line_header->file_name_at (file_index);
8c43009f
PA
21171 else
21172 fe = NULL;
21173
21174 if (fe == NULL)
cb1df416
DJ
21175 complaint (&symfile_complaints,
21176 _("file index out of range"));
8c43009f
PA
21177 else
21178 symbol_set_symtab (sym, fe->symtab);
cb1df416
DJ
21179 }
21180
c906108c
SS
21181 switch (die->tag)
21182 {
21183 case DW_TAG_label:
e142c38c 21184 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
c906108c 21185 if (attr)
3e29f34a
MR
21186 {
21187 CORE_ADDR addr;
21188
21189 addr = attr_value_as_address (attr);
21190 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr + baseaddr);
21191 SYMBOL_VALUE_ADDRESS (sym) = addr;
21192 }
0f5238ed
TT
21193 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
21194 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
f1e6e072 21195 SYMBOL_ACLASS_INDEX (sym) = LOC_LABEL;
0f5238ed 21196 add_symbol_to_list (sym, cu->list_in_scope);
c906108c
SS
21197 break;
21198 case DW_TAG_subprogram:
21199 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
21200 finish_block. */
f1e6e072 21201 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
e142c38c 21202 attr2 = dwarf2_attr (die, DW_AT_external, cu);
2cfa0c8d
JB
21203 if ((attr2 && (DW_UNSND (attr2) != 0))
21204 || cu->language == language_ada)
c906108c 21205 {
2cfa0c8d
JB
21206 /* Subprograms marked external are stored as a global symbol.
21207 Ada subprograms, whether marked external or not, are always
21208 stored as a global symbol, because we want to be able to
21209 access them globally. For instance, we want to be able
21210 to break on a nested subprogram without having to
21211 specify the context. */
e37fd15a 21212 list_to_add = &global_symbols;
c906108c
SS
21213 }
21214 else
21215 {
e37fd15a 21216 list_to_add = cu->list_in_scope;
c906108c
SS
21217 }
21218 break;
edb3359d
DJ
21219 case DW_TAG_inlined_subroutine:
21220 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
21221 finish_block. */
f1e6e072 21222 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
edb3359d 21223 SYMBOL_INLINED (sym) = 1;
481860b3 21224 list_to_add = cu->list_in_scope;
edb3359d 21225 break;
34eaf542
TT
21226 case DW_TAG_template_value_param:
21227 suppress_add = 1;
21228 /* Fall through. */
72929c62 21229 case DW_TAG_constant:
c906108c 21230 case DW_TAG_variable:
254e6b9e 21231 case DW_TAG_member:
0963b4bd
MS
21232 /* Compilation with minimal debug info may result in
21233 variables with missing type entries. Change the
21234 misleading `void' type to something sensible. */
c906108c 21235 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
46a4882b 21236 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_int;
64c50499 21237
e142c38c 21238 attr = dwarf2_attr (die, DW_AT_const_value, cu);
254e6b9e
DE
21239 /* In the case of DW_TAG_member, we should only be called for
21240 static const members. */
21241 if (die->tag == DW_TAG_member)
21242 {
3863f96c
DE
21243 /* dwarf2_add_field uses die_is_declaration,
21244 so we do the same. */
254e6b9e
DE
21245 gdb_assert (die_is_declaration (die, cu));
21246 gdb_assert (attr);
21247 }
c906108c
SS
21248 if (attr)
21249 {
e7c27a73 21250 dwarf2_const_value (attr, sym, cu);
e142c38c 21251 attr2 = dwarf2_attr (die, DW_AT_external, cu);
e37fd15a 21252 if (!suppress_add)
34eaf542
TT
21253 {
21254 if (attr2 && (DW_UNSND (attr2) != 0))
e37fd15a 21255 list_to_add = &global_symbols;
34eaf542 21256 else
e37fd15a 21257 list_to_add = cu->list_in_scope;
34eaf542 21258 }
c906108c
SS
21259 break;
21260 }
e142c38c 21261 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
21262 if (attr)
21263 {
e7c27a73 21264 var_decode_location (attr, sym, cu);
e142c38c 21265 attr2 = dwarf2_attr (die, DW_AT_external, cu);
4357ac6c
TT
21266
21267 /* Fortran explicitly imports any global symbols to the local
21268 scope by DW_TAG_common_block. */
21269 if (cu->language == language_fortran && die->parent
21270 && die->parent->tag == DW_TAG_common_block)
21271 attr2 = NULL;
21272
caac4577
JG
21273 if (SYMBOL_CLASS (sym) == LOC_STATIC
21274 && SYMBOL_VALUE_ADDRESS (sym) == 0
21275 && !dwarf2_per_objfile->has_section_at_zero)
21276 {
21277 /* When a static variable is eliminated by the linker,
21278 the corresponding debug information is not stripped
21279 out, but the variable address is set to null;
21280 do not add such variables into symbol table. */
21281 }
21282 else if (attr2 && (DW_UNSND (attr2) != 0))
1c809c68 21283 {
f55ee35c
JK
21284 /* Workaround gfortran PR debug/40040 - it uses
21285 DW_AT_location for variables in -fPIC libraries which may
21286 get overriden by other libraries/executable and get
21287 a different address. Resolve it by the minimal symbol
21288 which may come from inferior's executable using copy
21289 relocation. Make this workaround only for gfortran as for
21290 other compilers GDB cannot guess the minimal symbol
21291 Fortran mangling kind. */
21292 if (cu->language == language_fortran && die->parent
21293 && die->parent->tag == DW_TAG_module
21294 && cu->producer
28586665 21295 && startswith (cu->producer, "GNU Fortran"))
f1e6e072 21296 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
f55ee35c 21297
1c809c68
TT
21298 /* A variable with DW_AT_external is never static,
21299 but it may be block-scoped. */
21300 list_to_add = (cu->list_in_scope == &file_symbols
21301 ? &global_symbols : cu->list_in_scope);
1c809c68 21302 }
c906108c 21303 else
e37fd15a 21304 list_to_add = cu->list_in_scope;
c906108c
SS
21305 }
21306 else
21307 {
21308 /* We do not know the address of this symbol.
c5aa993b
JM
21309 If it is an external symbol and we have type information
21310 for it, enter the symbol as a LOC_UNRESOLVED symbol.
21311 The address of the variable will then be determined from
21312 the minimal symbol table whenever the variable is
21313 referenced. */
e142c38c 21314 attr2 = dwarf2_attr (die, DW_AT_external, cu);
0971de02
TT
21315
21316 /* Fortran explicitly imports any global symbols to the local
21317 scope by DW_TAG_common_block. */
21318 if (cu->language == language_fortran && die->parent
21319 && die->parent->tag == DW_TAG_common_block)
21320 {
21321 /* SYMBOL_CLASS doesn't matter here because
21322 read_common_block is going to reset it. */
21323 if (!suppress_add)
21324 list_to_add = cu->list_in_scope;
21325 }
21326 else if (attr2 && (DW_UNSND (attr2) != 0)
21327 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
c906108c 21328 {
0fe7935b
DJ
21329 /* A variable with DW_AT_external is never static, but it
21330 may be block-scoped. */
21331 list_to_add = (cu->list_in_scope == &file_symbols
21332 ? &global_symbols : cu->list_in_scope);
21333
f1e6e072 21334 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
c906108c 21335 }
442ddf59
JK
21336 else if (!die_is_declaration (die, cu))
21337 {
21338 /* Use the default LOC_OPTIMIZED_OUT class. */
21339 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
e37fd15a
SW
21340 if (!suppress_add)
21341 list_to_add = cu->list_in_scope;
442ddf59 21342 }
c906108c
SS
21343 }
21344 break;
21345 case DW_TAG_formal_parameter:
edb3359d
DJ
21346 /* If we are inside a function, mark this as an argument. If
21347 not, we might be looking at an argument to an inlined function
21348 when we do not have enough information to show inlined frames;
21349 pretend it's a local variable in that case so that the user can
21350 still see it. */
21351 if (context_stack_depth > 0
21352 && context_stack[context_stack_depth - 1].name != NULL)
21353 SYMBOL_IS_ARGUMENT (sym) = 1;
e142c38c 21354 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
21355 if (attr)
21356 {
e7c27a73 21357 var_decode_location (attr, sym, cu);
c906108c 21358 }
e142c38c 21359 attr = dwarf2_attr (die, DW_AT_const_value, cu);
c906108c
SS
21360 if (attr)
21361 {
e7c27a73 21362 dwarf2_const_value (attr, sym, cu);
c906108c 21363 }
f346a30d 21364
e37fd15a 21365 list_to_add = cu->list_in_scope;
c906108c
SS
21366 break;
21367 case DW_TAG_unspecified_parameters:
21368 /* From varargs functions; gdb doesn't seem to have any
21369 interest in this information, so just ignore it for now.
21370 (FIXME?) */
21371 break;
34eaf542
TT
21372 case DW_TAG_template_type_param:
21373 suppress_add = 1;
21374 /* Fall through. */
c906108c 21375 case DW_TAG_class_type:
680b30c7 21376 case DW_TAG_interface_type:
c906108c
SS
21377 case DW_TAG_structure_type:
21378 case DW_TAG_union_type:
72019c9c 21379 case DW_TAG_set_type:
c906108c 21380 case DW_TAG_enumeration_type:
f1e6e072 21381 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
176620f1 21382 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
c906108c 21383
63d06c5c 21384 {
9c37b5ae 21385 /* NOTE: carlton/2003-11-10: C++ class symbols shouldn't
63d06c5c
DC
21386 really ever be static objects: otherwise, if you try
21387 to, say, break of a class's method and you're in a file
21388 which doesn't mention that class, it won't work unless
21389 the check for all static symbols in lookup_symbol_aux
21390 saves you. See the OtherFileClass tests in
21391 gdb.c++/namespace.exp. */
21392
e37fd15a 21393 if (!suppress_add)
34eaf542 21394 {
34eaf542 21395 list_to_add = (cu->list_in_scope == &file_symbols
9c37b5ae 21396 && cu->language == language_cplus
34eaf542 21397 ? &global_symbols : cu->list_in_scope);
63d06c5c 21398
64382290 21399 /* The semantics of C++ state that "struct foo {
9c37b5ae 21400 ... }" also defines a typedef for "foo". */
64382290 21401 if (cu->language == language_cplus
45280282 21402 || cu->language == language_ada
c44af4eb
TT
21403 || cu->language == language_d
21404 || cu->language == language_rust)
64382290
TT
21405 {
21406 /* The symbol's name is already allocated along
21407 with this objfile, so we don't need to
21408 duplicate it for the type. */
21409 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
21410 TYPE_NAME (SYMBOL_TYPE (sym)) = SYMBOL_SEARCH_NAME (sym);
21411 }
63d06c5c
DC
21412 }
21413 }
c906108c
SS
21414 break;
21415 case DW_TAG_typedef:
f1e6e072 21416 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
63d06c5c 21417 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 21418 list_to_add = cu->list_in_scope;
63d06c5c 21419 break;
c906108c 21420 case DW_TAG_base_type:
a02abb62 21421 case DW_TAG_subrange_type:
f1e6e072 21422 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
176620f1 21423 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 21424 list_to_add = cu->list_in_scope;
c906108c
SS
21425 break;
21426 case DW_TAG_enumerator:
e142c38c 21427 attr = dwarf2_attr (die, DW_AT_const_value, cu);
c906108c
SS
21428 if (attr)
21429 {
e7c27a73 21430 dwarf2_const_value (attr, sym, cu);
c906108c 21431 }
63d06c5c
DC
21432 {
21433 /* NOTE: carlton/2003-11-10: See comment above in the
21434 DW_TAG_class_type, etc. block. */
21435
e142c38c 21436 list_to_add = (cu->list_in_scope == &file_symbols
9c37b5ae 21437 && cu->language == language_cplus
e142c38c 21438 ? &global_symbols : cu->list_in_scope);
63d06c5c 21439 }
c906108c 21440 break;
74921315 21441 case DW_TAG_imported_declaration:
5c4e30ca 21442 case DW_TAG_namespace:
f1e6e072 21443 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
e37fd15a 21444 list_to_add = &global_symbols;
5c4e30ca 21445 break;
530e8392
KB
21446 case DW_TAG_module:
21447 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21448 SYMBOL_DOMAIN (sym) = MODULE_DOMAIN;
21449 list_to_add = &global_symbols;
21450 break;
4357ac6c 21451 case DW_TAG_common_block:
f1e6e072 21452 SYMBOL_ACLASS_INDEX (sym) = LOC_COMMON_BLOCK;
4357ac6c
TT
21453 SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
21454 add_symbol_to_list (sym, cu->list_in_scope);
21455 break;
c906108c
SS
21456 default:
21457 /* Not a tag we recognize. Hopefully we aren't processing
21458 trash data, but since we must specifically ignore things
21459 we don't recognize, there is nothing else we should do at
0963b4bd 21460 this point. */
e2e0b3e5 21461 complaint (&symfile_complaints, _("unsupported tag: '%s'"),
4d3c2250 21462 dwarf_tag_name (die->tag));
c906108c
SS
21463 break;
21464 }
df8a16a1 21465
e37fd15a
SW
21466 if (suppress_add)
21467 {
21468 sym->hash_next = objfile->template_symbols;
21469 objfile->template_symbols = sym;
21470 list_to_add = NULL;
21471 }
21472
21473 if (list_to_add != NULL)
21474 add_symbol_to_list (sym, list_to_add);
21475
df8a16a1
DJ
21476 /* For the benefit of old versions of GCC, check for anonymous
21477 namespaces based on the demangled name. */
4d4ec4e5 21478 if (!cu->processing_has_namespace_info
94af9270 21479 && cu->language == language_cplus)
a10964d1 21480 cp_scan_for_anonymous_namespaces (sym, objfile);
c906108c
SS
21481 }
21482 return (sym);
21483}
21484
98bfdba5
PA
21485/* Given an attr with a DW_FORM_dataN value in host byte order,
21486 zero-extend it as appropriate for the symbol's type. The DWARF
21487 standard (v4) is not entirely clear about the meaning of using
21488 DW_FORM_dataN for a constant with a signed type, where the type is
21489 wider than the data. The conclusion of a discussion on the DWARF
21490 list was that this is unspecified. We choose to always zero-extend
21491 because that is the interpretation long in use by GCC. */
c906108c 21492
98bfdba5 21493static gdb_byte *
ff39bb5e 21494dwarf2_const_value_data (const struct attribute *attr, struct obstack *obstack,
12df843f 21495 struct dwarf2_cu *cu, LONGEST *value, int bits)
c906108c 21496{
518817b3 21497 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
e17a4113
UW
21498 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
21499 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
98bfdba5
PA
21500 LONGEST l = DW_UNSND (attr);
21501
21502 if (bits < sizeof (*value) * 8)
21503 {
21504 l &= ((LONGEST) 1 << bits) - 1;
21505 *value = l;
21506 }
21507 else if (bits == sizeof (*value) * 8)
21508 *value = l;
21509 else
21510 {
224c3ddb 21511 gdb_byte *bytes = (gdb_byte *) obstack_alloc (obstack, bits / 8);
98bfdba5
PA
21512 store_unsigned_integer (bytes, bits / 8, byte_order, l);
21513 return bytes;
21514 }
21515
21516 return NULL;
21517}
21518
21519/* Read a constant value from an attribute. Either set *VALUE, or if
21520 the value does not fit in *VALUE, set *BYTES - either already
21521 allocated on the objfile obstack, or newly allocated on OBSTACK,
21522 or, set *BATON, if we translated the constant to a location
21523 expression. */
21524
21525static void
ff39bb5e 21526dwarf2_const_value_attr (const struct attribute *attr, struct type *type,
98bfdba5
PA
21527 const char *name, struct obstack *obstack,
21528 struct dwarf2_cu *cu,
d521ce57 21529 LONGEST *value, const gdb_byte **bytes,
98bfdba5
PA
21530 struct dwarf2_locexpr_baton **baton)
21531{
518817b3 21532 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
98bfdba5 21533 struct comp_unit_head *cu_header = &cu->header;
c906108c 21534 struct dwarf_block *blk;
98bfdba5
PA
21535 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
21536 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
21537
21538 *value = 0;
21539 *bytes = NULL;
21540 *baton = NULL;
c906108c
SS
21541
21542 switch (attr->form)
21543 {
21544 case DW_FORM_addr:
3019eac3 21545 case DW_FORM_GNU_addr_index:
ac56253d 21546 {
ac56253d
TT
21547 gdb_byte *data;
21548
98bfdba5
PA
21549 if (TYPE_LENGTH (type) != cu_header->addr_size)
21550 dwarf2_const_value_length_mismatch_complaint (name,
ac56253d 21551 cu_header->addr_size,
98bfdba5 21552 TYPE_LENGTH (type));
ac56253d
TT
21553 /* Symbols of this form are reasonably rare, so we just
21554 piggyback on the existing location code rather than writing
21555 a new implementation of symbol_computed_ops. */
8d749320 21556 *baton = XOBNEW (obstack, struct dwarf2_locexpr_baton);
98bfdba5
PA
21557 (*baton)->per_cu = cu->per_cu;
21558 gdb_assert ((*baton)->per_cu);
ac56253d 21559
98bfdba5 21560 (*baton)->size = 2 + cu_header->addr_size;
224c3ddb 21561 data = (gdb_byte *) obstack_alloc (obstack, (*baton)->size);
98bfdba5 21562 (*baton)->data = data;
ac56253d
TT
21563
21564 data[0] = DW_OP_addr;
21565 store_unsigned_integer (&data[1], cu_header->addr_size,
21566 byte_order, DW_ADDR (attr));
21567 data[cu_header->addr_size + 1] = DW_OP_stack_value;
ac56253d 21568 }
c906108c 21569 break;
4ac36638 21570 case DW_FORM_string:
93b5768b 21571 case DW_FORM_strp:
3019eac3 21572 case DW_FORM_GNU_str_index:
36586728 21573 case DW_FORM_GNU_strp_alt:
98bfdba5
PA
21574 /* DW_STRING is already allocated on the objfile obstack, point
21575 directly to it. */
d521ce57 21576 *bytes = (const gdb_byte *) DW_STRING (attr);
93b5768b 21577 break;
c906108c
SS
21578 case DW_FORM_block1:
21579 case DW_FORM_block2:
21580 case DW_FORM_block4:
21581 case DW_FORM_block:
2dc7f7b3 21582 case DW_FORM_exprloc:
0224619f 21583 case DW_FORM_data16:
c906108c 21584 blk = DW_BLOCK (attr);
98bfdba5
PA
21585 if (TYPE_LENGTH (type) != blk->size)
21586 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
21587 TYPE_LENGTH (type));
21588 *bytes = blk->data;
c906108c 21589 break;
2df3850c
JM
21590
21591 /* The DW_AT_const_value attributes are supposed to carry the
21592 symbol's value "represented as it would be on the target
21593 architecture." By the time we get here, it's already been
21594 converted to host endianness, so we just need to sign- or
21595 zero-extend it as appropriate. */
21596 case DW_FORM_data1:
3aef2284 21597 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 8);
2df3850c 21598 break;
c906108c 21599 case DW_FORM_data2:
3aef2284 21600 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 16);
2df3850c 21601 break;
c906108c 21602 case DW_FORM_data4:
3aef2284 21603 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 32);
2df3850c 21604 break;
c906108c 21605 case DW_FORM_data8:
3aef2284 21606 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 64);
2df3850c
JM
21607 break;
21608
c906108c 21609 case DW_FORM_sdata:
663c44ac 21610 case DW_FORM_implicit_const:
98bfdba5 21611 *value = DW_SND (attr);
2df3850c
JM
21612 break;
21613
c906108c 21614 case DW_FORM_udata:
98bfdba5 21615 *value = DW_UNSND (attr);
c906108c 21616 break;
2df3850c 21617
c906108c 21618 default:
4d3c2250 21619 complaint (&symfile_complaints,
e2e0b3e5 21620 _("unsupported const value attribute form: '%s'"),
4d3c2250 21621 dwarf_form_name (attr->form));
98bfdba5 21622 *value = 0;
c906108c
SS
21623 break;
21624 }
21625}
21626
2df3850c 21627
98bfdba5
PA
21628/* Copy constant value from an attribute to a symbol. */
21629
2df3850c 21630static void
ff39bb5e 21631dwarf2_const_value (const struct attribute *attr, struct symbol *sym,
98bfdba5 21632 struct dwarf2_cu *cu)
2df3850c 21633{
518817b3 21634 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
12df843f 21635 LONGEST value;
d521ce57 21636 const gdb_byte *bytes;
98bfdba5 21637 struct dwarf2_locexpr_baton *baton;
2df3850c 21638
98bfdba5
PA
21639 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
21640 SYMBOL_PRINT_NAME (sym),
21641 &objfile->objfile_obstack, cu,
21642 &value, &bytes, &baton);
2df3850c 21643
98bfdba5
PA
21644 if (baton != NULL)
21645 {
98bfdba5 21646 SYMBOL_LOCATION_BATON (sym) = baton;
f1e6e072 21647 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
98bfdba5
PA
21648 }
21649 else if (bytes != NULL)
21650 {
21651 SYMBOL_VALUE_BYTES (sym) = bytes;
f1e6e072 21652 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST_BYTES;
98bfdba5
PA
21653 }
21654 else
21655 {
21656 SYMBOL_VALUE (sym) = value;
f1e6e072 21657 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST;
98bfdba5 21658 }
2df3850c
JM
21659}
21660
c906108c
SS
21661/* Return the type of the die in question using its DW_AT_type attribute. */
21662
21663static struct type *
e7c27a73 21664die_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 21665{
c906108c 21666 struct attribute *type_attr;
c906108c 21667
e142c38c 21668 type_attr = dwarf2_attr (die, DW_AT_type, cu);
c906108c
SS
21669 if (!type_attr)
21670 {
518817b3 21671 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c 21672 /* A missing DW_AT_type represents a void type. */
518817b3 21673 return objfile_type (objfile)->builtin_void;
c906108c 21674 }
348e048f 21675
673bfd45 21676 return lookup_die_type (die, type_attr, cu);
c906108c
SS
21677}
21678
b4ba55a1
JB
21679/* True iff CU's producer generates GNAT Ada auxiliary information
21680 that allows to find parallel types through that information instead
21681 of having to do expensive parallel lookups by type name. */
21682
21683static int
21684need_gnat_info (struct dwarf2_cu *cu)
21685{
de4cb04a
JB
21686 /* Assume that the Ada compiler was GNAT, which always produces
21687 the auxiliary information. */
21688 return (cu->language == language_ada);
b4ba55a1
JB
21689}
21690
b4ba55a1
JB
21691/* Return the auxiliary type of the die in question using its
21692 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
21693 attribute is not present. */
21694
21695static struct type *
21696die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
21697{
b4ba55a1 21698 struct attribute *type_attr;
b4ba55a1
JB
21699
21700 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
21701 if (!type_attr)
21702 return NULL;
21703
673bfd45 21704 return lookup_die_type (die, type_attr, cu);
b4ba55a1
JB
21705}
21706
21707/* If DIE has a descriptive_type attribute, then set the TYPE's
21708 descriptive type accordingly. */
21709
21710static void
21711set_descriptive_type (struct type *type, struct die_info *die,
21712 struct dwarf2_cu *cu)
21713{
21714 struct type *descriptive_type = die_descriptive_type (die, cu);
21715
21716 if (descriptive_type)
21717 {
21718 ALLOCATE_GNAT_AUX_TYPE (type);
21719 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
21720 }
21721}
21722
c906108c
SS
21723/* Return the containing type of the die in question using its
21724 DW_AT_containing_type attribute. */
21725
21726static struct type *
e7c27a73 21727die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 21728{
c906108c 21729 struct attribute *type_attr;
518817b3 21730 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c 21731
e142c38c 21732 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
33ac96f0
JK
21733 if (!type_attr)
21734 error (_("Dwarf Error: Problem turning containing type into gdb type "
518817b3 21735 "[in module %s]"), objfile_name (objfile));
33ac96f0 21736
673bfd45 21737 return lookup_die_type (die, type_attr, cu);
c906108c
SS
21738}
21739
ac9ec31b
DE
21740/* Return an error marker type to use for the ill formed type in DIE/CU. */
21741
21742static struct type *
21743build_error_marker_type (struct dwarf2_cu *cu, struct die_info *die)
21744{
518817b3
SM
21745 struct dwarf2_per_objfile *dwarf2_per_objfile
21746 = cu->per_cu->dwarf2_per_objfile;
ac9ec31b
DE
21747 struct objfile *objfile = dwarf2_per_objfile->objfile;
21748 char *message, *saved;
21749
9d8780f0 21750 message = xstrprintf (_("<unknown type in %s, CU %s, DIE %s>"),
4262abfb 21751 objfile_name (objfile),
9d8780f0
SM
21752 sect_offset_str (cu->header.sect_off),
21753 sect_offset_str (die->sect_off));
224c3ddb
SM
21754 saved = (char *) obstack_copy0 (&objfile->objfile_obstack,
21755 message, strlen (message));
ac9ec31b
DE
21756 xfree (message);
21757
19f392bc 21758 return init_type (objfile, TYPE_CODE_ERROR, 0, saved);
ac9ec31b
DE
21759}
21760
673bfd45 21761/* Look up the type of DIE in CU using its type attribute ATTR.
ac9ec31b
DE
21762 ATTR must be one of: DW_AT_type, DW_AT_GNAT_descriptive_type,
21763 DW_AT_containing_type.
673bfd45
DE
21764 If there is no type substitute an error marker. */
21765
c906108c 21766static struct type *
ff39bb5e 21767lookup_die_type (struct die_info *die, const struct attribute *attr,
673bfd45 21768 struct dwarf2_cu *cu)
c906108c 21769{
518817b3
SM
21770 struct dwarf2_per_objfile *dwarf2_per_objfile
21771 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 21772 struct objfile *objfile = dwarf2_per_objfile->objfile;
f792889a
DJ
21773 struct type *this_type;
21774
ac9ec31b
DE
21775 gdb_assert (attr->name == DW_AT_type
21776 || attr->name == DW_AT_GNAT_descriptive_type
21777 || attr->name == DW_AT_containing_type);
21778
673bfd45
DE
21779 /* First see if we have it cached. */
21780
36586728
TT
21781 if (attr->form == DW_FORM_GNU_ref_alt)
21782 {
21783 struct dwarf2_per_cu_data *per_cu;
9c541725 21784 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
36586728 21785
ed2dc618
SM
21786 per_cu = dwarf2_find_containing_comp_unit (sect_off, 1,
21787 dwarf2_per_objfile);
9c541725 21788 this_type = get_die_type_at_offset (sect_off, per_cu);
36586728 21789 }
7771576e 21790 else if (attr_form_is_ref (attr))
673bfd45 21791 {
9c541725 21792 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
673bfd45 21793
9c541725 21794 this_type = get_die_type_at_offset (sect_off, cu->per_cu);
673bfd45 21795 }
55f1336d 21796 else if (attr->form == DW_FORM_ref_sig8)
673bfd45 21797 {
ac9ec31b 21798 ULONGEST signature = DW_SIGNATURE (attr);
673bfd45 21799
ac9ec31b 21800 return get_signatured_type (die, signature, cu);
673bfd45
DE
21801 }
21802 else
21803 {
ac9ec31b
DE
21804 complaint (&symfile_complaints,
21805 _("Dwarf Error: Bad type attribute %s in DIE"
9d8780f0
SM
21806 " at %s [in module %s]"),
21807 dwarf_attr_name (attr->name), sect_offset_str (die->sect_off),
4262abfb 21808 objfile_name (objfile));
ac9ec31b 21809 return build_error_marker_type (cu, die);
673bfd45
DE
21810 }
21811
21812 /* If not cached we need to read it in. */
21813
21814 if (this_type == NULL)
21815 {
ac9ec31b 21816 struct die_info *type_die = NULL;
673bfd45
DE
21817 struct dwarf2_cu *type_cu = cu;
21818
7771576e 21819 if (attr_form_is_ref (attr))
ac9ec31b
DE
21820 type_die = follow_die_ref (die, attr, &type_cu);
21821 if (type_die == NULL)
21822 return build_error_marker_type (cu, die);
21823 /* If we find the type now, it's probably because the type came
3019eac3
DE
21824 from an inter-CU reference and the type's CU got expanded before
21825 ours. */
ac9ec31b 21826 this_type = read_type_die (type_die, type_cu);
673bfd45
DE
21827 }
21828
21829 /* If we still don't have a type use an error marker. */
21830
21831 if (this_type == NULL)
ac9ec31b 21832 return build_error_marker_type (cu, die);
673bfd45 21833
f792889a 21834 return this_type;
c906108c
SS
21835}
21836
673bfd45
DE
21837/* Return the type in DIE, CU.
21838 Returns NULL for invalid types.
21839
02142a6c 21840 This first does a lookup in die_type_hash,
673bfd45
DE
21841 and only reads the die in if necessary.
21842
21843 NOTE: This can be called when reading in partial or full symbols. */
21844
f792889a 21845static struct type *
e7c27a73 21846read_type_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 21847{
f792889a
DJ
21848 struct type *this_type;
21849
21850 this_type = get_die_type (die, cu);
21851 if (this_type)
21852 return this_type;
21853
673bfd45
DE
21854 return read_type_die_1 (die, cu);
21855}
21856
21857/* Read the type in DIE, CU.
21858 Returns NULL for invalid types. */
21859
21860static struct type *
21861read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
21862{
21863 struct type *this_type = NULL;
21864
c906108c
SS
21865 switch (die->tag)
21866 {
21867 case DW_TAG_class_type:
680b30c7 21868 case DW_TAG_interface_type:
c906108c
SS
21869 case DW_TAG_structure_type:
21870 case DW_TAG_union_type:
f792889a 21871 this_type = read_structure_type (die, cu);
c906108c
SS
21872 break;
21873 case DW_TAG_enumeration_type:
f792889a 21874 this_type = read_enumeration_type (die, cu);
c906108c
SS
21875 break;
21876 case DW_TAG_subprogram:
21877 case DW_TAG_subroutine_type:
edb3359d 21878 case DW_TAG_inlined_subroutine:
f792889a 21879 this_type = read_subroutine_type (die, cu);
c906108c
SS
21880 break;
21881 case DW_TAG_array_type:
f792889a 21882 this_type = read_array_type (die, cu);
c906108c 21883 break;
72019c9c 21884 case DW_TAG_set_type:
f792889a 21885 this_type = read_set_type (die, cu);
72019c9c 21886 break;
c906108c 21887 case DW_TAG_pointer_type:
f792889a 21888 this_type = read_tag_pointer_type (die, cu);
c906108c
SS
21889 break;
21890 case DW_TAG_ptr_to_member_type:
f792889a 21891 this_type = read_tag_ptr_to_member_type (die, cu);
c906108c
SS
21892 break;
21893 case DW_TAG_reference_type:
4297a3f0
AV
21894 this_type = read_tag_reference_type (die, cu, TYPE_CODE_REF);
21895 break;
21896 case DW_TAG_rvalue_reference_type:
21897 this_type = read_tag_reference_type (die, cu, TYPE_CODE_RVALUE_REF);
c906108c
SS
21898 break;
21899 case DW_TAG_const_type:
f792889a 21900 this_type = read_tag_const_type (die, cu);
c906108c
SS
21901 break;
21902 case DW_TAG_volatile_type:
f792889a 21903 this_type = read_tag_volatile_type (die, cu);
c906108c 21904 break;
06d66ee9
TT
21905 case DW_TAG_restrict_type:
21906 this_type = read_tag_restrict_type (die, cu);
21907 break;
c906108c 21908 case DW_TAG_string_type:
f792889a 21909 this_type = read_tag_string_type (die, cu);
c906108c
SS
21910 break;
21911 case DW_TAG_typedef:
f792889a 21912 this_type = read_typedef (die, cu);
c906108c 21913 break;
a02abb62 21914 case DW_TAG_subrange_type:
f792889a 21915 this_type = read_subrange_type (die, cu);
a02abb62 21916 break;
c906108c 21917 case DW_TAG_base_type:
f792889a 21918 this_type = read_base_type (die, cu);
c906108c 21919 break;
81a17f79 21920 case DW_TAG_unspecified_type:
f792889a 21921 this_type = read_unspecified_type (die, cu);
81a17f79 21922 break;
0114d602
DJ
21923 case DW_TAG_namespace:
21924 this_type = read_namespace_type (die, cu);
21925 break;
f55ee35c
JK
21926 case DW_TAG_module:
21927 this_type = read_module_type (die, cu);
21928 break;
a2c2acaf
MW
21929 case DW_TAG_atomic_type:
21930 this_type = read_tag_atomic_type (die, cu);
21931 break;
c906108c 21932 default:
3e43a32a
MS
21933 complaint (&symfile_complaints,
21934 _("unexpected tag in read_type_die: '%s'"),
4d3c2250 21935 dwarf_tag_name (die->tag));
c906108c
SS
21936 break;
21937 }
63d06c5c 21938
f792889a 21939 return this_type;
63d06c5c
DC
21940}
21941
abc72ce4
DE
21942/* See if we can figure out if the class lives in a namespace. We do
21943 this by looking for a member function; its demangled name will
21944 contain namespace info, if there is any.
21945 Return the computed name or NULL.
21946 Space for the result is allocated on the objfile's obstack.
21947 This is the full-die version of guess_partial_die_structure_name.
21948 In this case we know DIE has no useful parent. */
21949
21950static char *
21951guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
21952{
21953 struct die_info *spec_die;
21954 struct dwarf2_cu *spec_cu;
21955 struct die_info *child;
518817b3 21956 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
abc72ce4
DE
21957
21958 spec_cu = cu;
21959 spec_die = die_specification (die, &spec_cu);
21960 if (spec_die != NULL)
21961 {
21962 die = spec_die;
21963 cu = spec_cu;
21964 }
21965
21966 for (child = die->child;
21967 child != NULL;
21968 child = child->sibling)
21969 {
21970 if (child->tag == DW_TAG_subprogram)
21971 {
73b9be8b 21972 const char *linkage_name = dw2_linkage_name (child, cu);
abc72ce4 21973
7d45c7c3 21974 if (linkage_name != NULL)
abc72ce4
DE
21975 {
21976 char *actual_name
21977 = language_class_name_from_physname (cu->language_defn,
7d45c7c3 21978 linkage_name);
abc72ce4
DE
21979 char *name = NULL;
21980
21981 if (actual_name != NULL)
21982 {
15d034d0 21983 const char *die_name = dwarf2_name (die, cu);
abc72ce4
DE
21984
21985 if (die_name != NULL
21986 && strcmp (die_name, actual_name) != 0)
21987 {
21988 /* Strip off the class name from the full name.
21989 We want the prefix. */
21990 int die_name_len = strlen (die_name);
21991 int actual_name_len = strlen (actual_name);
21992
21993 /* Test for '::' as a sanity check. */
21994 if (actual_name_len > die_name_len + 2
3e43a32a
MS
21995 && actual_name[actual_name_len
21996 - die_name_len - 1] == ':')
224c3ddb 21997 name = (char *) obstack_copy0 (
e3b94546 21998 &objfile->per_bfd->storage_obstack,
224c3ddb 21999 actual_name, actual_name_len - die_name_len - 2);
abc72ce4
DE
22000 }
22001 }
22002 xfree (actual_name);
22003 return name;
22004 }
22005 }
22006 }
22007
22008 return NULL;
22009}
22010
96408a79
SA
22011/* GCC might emit a nameless typedef that has a linkage name. Determine the
22012 prefix part in such case. See
22013 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
22014
a121b7c1 22015static const char *
96408a79
SA
22016anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
22017{
22018 struct attribute *attr;
e6a959d6 22019 const char *base;
96408a79
SA
22020
22021 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
22022 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
22023 return NULL;
22024
7d45c7c3 22025 if (dwarf2_string_attr (die, DW_AT_name, cu) != NULL)
96408a79
SA
22026 return NULL;
22027
73b9be8b 22028 attr = dw2_linkage_name_attr (die, cu);
96408a79
SA
22029 if (attr == NULL || DW_STRING (attr) == NULL)
22030 return NULL;
22031
22032 /* dwarf2_name had to be already called. */
22033 gdb_assert (DW_STRING_IS_CANONICAL (attr));
22034
22035 /* Strip the base name, keep any leading namespaces/classes. */
22036 base = strrchr (DW_STRING (attr), ':');
22037 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
22038 return "";
22039
518817b3 22040 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
e3b94546 22041 return (char *) obstack_copy0 (&objfile->per_bfd->storage_obstack,
224c3ddb
SM
22042 DW_STRING (attr),
22043 &base[-1] - DW_STRING (attr));
96408a79
SA
22044}
22045
fdde2d81 22046/* Return the name of the namespace/class that DIE is defined within,
0114d602 22047 or "" if we can't tell. The caller should not xfree the result.
fdde2d81 22048
0114d602
DJ
22049 For example, if we're within the method foo() in the following
22050 code:
22051
22052 namespace N {
22053 class C {
22054 void foo () {
22055 }
22056 };
22057 }
22058
22059 then determine_prefix on foo's die will return "N::C". */
fdde2d81 22060
0d5cff50 22061static const char *
e142c38c 22062determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
63d06c5c 22063{
518817b3
SM
22064 struct dwarf2_per_objfile *dwarf2_per_objfile
22065 = cu->per_cu->dwarf2_per_objfile;
0114d602
DJ
22066 struct die_info *parent, *spec_die;
22067 struct dwarf2_cu *spec_cu;
22068 struct type *parent_type;
a121b7c1 22069 const char *retval;
63d06c5c 22070
9c37b5ae 22071 if (cu->language != language_cplus
c44af4eb
TT
22072 && cu->language != language_fortran && cu->language != language_d
22073 && cu->language != language_rust)
0114d602
DJ
22074 return "";
22075
96408a79
SA
22076 retval = anonymous_struct_prefix (die, cu);
22077 if (retval)
22078 return retval;
22079
0114d602
DJ
22080 /* We have to be careful in the presence of DW_AT_specification.
22081 For example, with GCC 3.4, given the code
22082
22083 namespace N {
22084 void foo() {
22085 // Definition of N::foo.
22086 }
22087 }
22088
22089 then we'll have a tree of DIEs like this:
22090
22091 1: DW_TAG_compile_unit
22092 2: DW_TAG_namespace // N
22093 3: DW_TAG_subprogram // declaration of N::foo
22094 4: DW_TAG_subprogram // definition of N::foo
22095 DW_AT_specification // refers to die #3
22096
22097 Thus, when processing die #4, we have to pretend that we're in
22098 the context of its DW_AT_specification, namely the contex of die
22099 #3. */
22100 spec_cu = cu;
22101 spec_die = die_specification (die, &spec_cu);
22102 if (spec_die == NULL)
22103 parent = die->parent;
22104 else
63d06c5c 22105 {
0114d602
DJ
22106 parent = spec_die->parent;
22107 cu = spec_cu;
63d06c5c 22108 }
0114d602
DJ
22109
22110 if (parent == NULL)
22111 return "";
98bfdba5
PA
22112 else if (parent->building_fullname)
22113 {
22114 const char *name;
22115 const char *parent_name;
22116
22117 /* It has been seen on RealView 2.2 built binaries,
22118 DW_TAG_template_type_param types actually _defined_ as
22119 children of the parent class:
22120
22121 enum E {};
22122 template class <class Enum> Class{};
22123 Class<enum E> class_e;
22124
22125 1: DW_TAG_class_type (Class)
22126 2: DW_TAG_enumeration_type (E)
22127 3: DW_TAG_enumerator (enum1:0)
22128 3: DW_TAG_enumerator (enum2:1)
22129 ...
22130 2: DW_TAG_template_type_param
22131 DW_AT_type DW_FORM_ref_udata (E)
22132
22133 Besides being broken debug info, it can put GDB into an
22134 infinite loop. Consider:
22135
22136 When we're building the full name for Class<E>, we'll start
22137 at Class, and go look over its template type parameters,
22138 finding E. We'll then try to build the full name of E, and
22139 reach here. We're now trying to build the full name of E,
22140 and look over the parent DIE for containing scope. In the
22141 broken case, if we followed the parent DIE of E, we'd again
22142 find Class, and once again go look at its template type
22143 arguments, etc., etc. Simply don't consider such parent die
22144 as source-level parent of this die (it can't be, the language
22145 doesn't allow it), and break the loop here. */
22146 name = dwarf2_name (die, cu);
22147 parent_name = dwarf2_name (parent, cu);
22148 complaint (&symfile_complaints,
22149 _("template param type '%s' defined within parent '%s'"),
22150 name ? name : "<unknown>",
22151 parent_name ? parent_name : "<unknown>");
22152 return "";
22153 }
63d06c5c 22154 else
0114d602
DJ
22155 switch (parent->tag)
22156 {
63d06c5c 22157 case DW_TAG_namespace:
0114d602 22158 parent_type = read_type_die (parent, cu);
acebe513
UW
22159 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
22160 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
22161 Work around this problem here. */
22162 if (cu->language == language_cplus
22163 && strcmp (TYPE_TAG_NAME (parent_type), "::") == 0)
22164 return "";
0114d602
DJ
22165 /* We give a name to even anonymous namespaces. */
22166 return TYPE_TAG_NAME (parent_type);
63d06c5c 22167 case DW_TAG_class_type:
680b30c7 22168 case DW_TAG_interface_type:
63d06c5c 22169 case DW_TAG_structure_type:
0114d602 22170 case DW_TAG_union_type:
f55ee35c 22171 case DW_TAG_module:
0114d602
DJ
22172 parent_type = read_type_die (parent, cu);
22173 if (TYPE_TAG_NAME (parent_type) != NULL)
22174 return TYPE_TAG_NAME (parent_type);
22175 else
22176 /* An anonymous structure is only allowed non-static data
22177 members; no typedefs, no member functions, et cetera.
22178 So it does not need a prefix. */
22179 return "";
abc72ce4 22180 case DW_TAG_compile_unit:
95554aad 22181 case DW_TAG_partial_unit:
abc72ce4
DE
22182 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
22183 if (cu->language == language_cplus
8b70b953 22184 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
abc72ce4
DE
22185 && die->child != NULL
22186 && (die->tag == DW_TAG_class_type
22187 || die->tag == DW_TAG_structure_type
22188 || die->tag == DW_TAG_union_type))
22189 {
22190 char *name = guess_full_die_structure_name (die, cu);
22191 if (name != NULL)
22192 return name;
22193 }
22194 return "";
3d567982
TT
22195 case DW_TAG_enumeration_type:
22196 parent_type = read_type_die (parent, cu);
22197 if (TYPE_DECLARED_CLASS (parent_type))
22198 {
22199 if (TYPE_TAG_NAME (parent_type) != NULL)
22200 return TYPE_TAG_NAME (parent_type);
22201 return "";
22202 }
22203 /* Fall through. */
63d06c5c 22204 default:
8176b9b8 22205 return determine_prefix (parent, cu);
63d06c5c 22206 }
63d06c5c
DC
22207}
22208
3e43a32a
MS
22209/* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
22210 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
22211 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
22212 an obconcat, otherwise allocate storage for the result. The CU argument is
22213 used to determine the language and hence, the appropriate separator. */
987504bb 22214
f55ee35c 22215#define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
63d06c5c
DC
22216
22217static char *
f55ee35c
JK
22218typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
22219 int physname, struct dwarf2_cu *cu)
63d06c5c 22220{
f55ee35c 22221 const char *lead = "";
5c315b68 22222 const char *sep;
63d06c5c 22223
3e43a32a
MS
22224 if (suffix == NULL || suffix[0] == '\0'
22225 || prefix == NULL || prefix[0] == '\0')
987504bb 22226 sep = "";
45280282
IB
22227 else if (cu->language == language_d)
22228 {
22229 /* For D, the 'main' function could be defined in any module, but it
22230 should never be prefixed. */
22231 if (strcmp (suffix, "D main") == 0)
22232 {
22233 prefix = "";
22234 sep = "";
22235 }
22236 else
22237 sep = ".";
22238 }
f55ee35c
JK
22239 else if (cu->language == language_fortran && physname)
22240 {
22241 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
22242 DW_AT_MIPS_linkage_name is preferred and used instead. */
22243
22244 lead = "__";
22245 sep = "_MOD_";
22246 }
987504bb
JJ
22247 else
22248 sep = "::";
63d06c5c 22249
6dd47d34
DE
22250 if (prefix == NULL)
22251 prefix = "";
22252 if (suffix == NULL)
22253 suffix = "";
22254
987504bb
JJ
22255 if (obs == NULL)
22256 {
3e43a32a 22257 char *retval
224c3ddb
SM
22258 = ((char *)
22259 xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1));
9a619af0 22260
f55ee35c
JK
22261 strcpy (retval, lead);
22262 strcat (retval, prefix);
6dd47d34
DE
22263 strcat (retval, sep);
22264 strcat (retval, suffix);
63d06c5c
DC
22265 return retval;
22266 }
987504bb
JJ
22267 else
22268 {
22269 /* We have an obstack. */
f55ee35c 22270 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
987504bb 22271 }
63d06c5c
DC
22272}
22273
c906108c
SS
22274/* Return sibling of die, NULL if no sibling. */
22275
f9aca02d 22276static struct die_info *
fba45db2 22277sibling_die (struct die_info *die)
c906108c 22278{
639d11d3 22279 return die->sibling;
c906108c
SS
22280}
22281
71c25dea
TT
22282/* Get name of a die, return NULL if not found. */
22283
15d034d0
TT
22284static const char *
22285dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
71c25dea
TT
22286 struct obstack *obstack)
22287{
22288 if (name && cu->language == language_cplus)
22289 {
2f408ecb 22290 std::string canon_name = cp_canonicalize_string (name);
71c25dea 22291
2f408ecb 22292 if (!canon_name.empty ())
71c25dea 22293 {
2f408ecb
PA
22294 if (canon_name != name)
22295 name = (const char *) obstack_copy0 (obstack,
22296 canon_name.c_str (),
22297 canon_name.length ());
71c25dea
TT
22298 }
22299 }
22300
22301 return name;
c906108c
SS
22302}
22303
96553a0c
DE
22304/* Get name of a die, return NULL if not found.
22305 Anonymous namespaces are converted to their magic string. */
9219021c 22306
15d034d0 22307static const char *
e142c38c 22308dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
9219021c
DC
22309{
22310 struct attribute *attr;
518817b3 22311 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
9219021c 22312
e142c38c 22313 attr = dwarf2_attr (die, DW_AT_name, cu);
53832f31 22314 if ((!attr || !DW_STRING (attr))
96553a0c 22315 && die->tag != DW_TAG_namespace
53832f31
TT
22316 && die->tag != DW_TAG_class_type
22317 && die->tag != DW_TAG_interface_type
22318 && die->tag != DW_TAG_structure_type
22319 && die->tag != DW_TAG_union_type)
71c25dea
TT
22320 return NULL;
22321
22322 switch (die->tag)
22323 {
22324 case DW_TAG_compile_unit:
95554aad 22325 case DW_TAG_partial_unit:
71c25dea
TT
22326 /* Compilation units have a DW_AT_name that is a filename, not
22327 a source language identifier. */
22328 case DW_TAG_enumeration_type:
22329 case DW_TAG_enumerator:
22330 /* These tags always have simple identifiers already; no need
22331 to canonicalize them. */
22332 return DW_STRING (attr);
907af001 22333
96553a0c
DE
22334 case DW_TAG_namespace:
22335 if (attr != NULL && DW_STRING (attr) != NULL)
22336 return DW_STRING (attr);
22337 return CP_ANONYMOUS_NAMESPACE_STR;
22338
907af001
UW
22339 case DW_TAG_class_type:
22340 case DW_TAG_interface_type:
22341 case DW_TAG_structure_type:
22342 case DW_TAG_union_type:
22343 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
22344 structures or unions. These were of the form "._%d" in GCC 4.1,
22345 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
22346 and GCC 4.4. We work around this problem by ignoring these. */
53832f31 22347 if (attr && DW_STRING (attr)
61012eef
GB
22348 && (startswith (DW_STRING (attr), "._")
22349 || startswith (DW_STRING (attr), "<anonymous")))
907af001 22350 return NULL;
53832f31
TT
22351
22352 /* GCC might emit a nameless typedef that has a linkage name. See
22353 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
22354 if (!attr || DW_STRING (attr) == NULL)
22355 {
df5c6c50 22356 char *demangled = NULL;
53832f31 22357
73b9be8b 22358 attr = dw2_linkage_name_attr (die, cu);
53832f31
TT
22359 if (attr == NULL || DW_STRING (attr) == NULL)
22360 return NULL;
22361
df5c6c50
JK
22362 /* Avoid demangling DW_STRING (attr) the second time on a second
22363 call for the same DIE. */
22364 if (!DW_STRING_IS_CANONICAL (attr))
8de20a37 22365 demangled = gdb_demangle (DW_STRING (attr), DMGL_TYPES);
53832f31
TT
22366
22367 if (demangled)
22368 {
e6a959d6 22369 const char *base;
96408a79 22370
53832f31 22371 /* FIXME: we already did this for the partial symbol... */
34a68019 22372 DW_STRING (attr)
224c3ddb 22373 = ((const char *)
e3b94546 22374 obstack_copy0 (&objfile->per_bfd->storage_obstack,
224c3ddb 22375 demangled, strlen (demangled)));
53832f31
TT
22376 DW_STRING_IS_CANONICAL (attr) = 1;
22377 xfree (demangled);
96408a79
SA
22378
22379 /* Strip any leading namespaces/classes, keep only the base name.
22380 DW_AT_name for named DIEs does not contain the prefixes. */
22381 base = strrchr (DW_STRING (attr), ':');
22382 if (base && base > DW_STRING (attr) && base[-1] == ':')
22383 return &base[1];
22384 else
22385 return DW_STRING (attr);
53832f31
TT
22386 }
22387 }
907af001
UW
22388 break;
22389
71c25dea 22390 default:
907af001
UW
22391 break;
22392 }
22393
22394 if (!DW_STRING_IS_CANONICAL (attr))
22395 {
22396 DW_STRING (attr)
22397 = dwarf2_canonicalize_name (DW_STRING (attr), cu,
e3b94546 22398 &objfile->per_bfd->storage_obstack);
907af001 22399 DW_STRING_IS_CANONICAL (attr) = 1;
71c25dea 22400 }
907af001 22401 return DW_STRING (attr);
9219021c
DC
22402}
22403
22404/* Return the die that this die in an extension of, or NULL if there
f2f0e013
DJ
22405 is none. *EXT_CU is the CU containing DIE on input, and the CU
22406 containing the return value on output. */
9219021c
DC
22407
22408static struct die_info *
f2f0e013 22409dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
9219021c
DC
22410{
22411 struct attribute *attr;
9219021c 22412
f2f0e013 22413 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
9219021c
DC
22414 if (attr == NULL)
22415 return NULL;
22416
f2f0e013 22417 return follow_die_ref (die, attr, ext_cu);
9219021c
DC
22418}
22419
c906108c
SS
22420/* Convert a DIE tag into its string name. */
22421
f39c6ffd 22422static const char *
aa1ee363 22423dwarf_tag_name (unsigned tag)
c906108c 22424{
f39c6ffd
TT
22425 const char *name = get_DW_TAG_name (tag);
22426
22427 if (name == NULL)
22428 return "DW_TAG_<unknown>";
22429
22430 return name;
c906108c
SS
22431}
22432
22433/* Convert a DWARF attribute code into its string name. */
22434
f39c6ffd 22435static const char *
aa1ee363 22436dwarf_attr_name (unsigned attr)
c906108c 22437{
f39c6ffd
TT
22438 const char *name;
22439
c764a876 22440#ifdef MIPS /* collides with DW_AT_HP_block_index */
f39c6ffd
TT
22441 if (attr == DW_AT_MIPS_fde)
22442 return "DW_AT_MIPS_fde";
22443#else
22444 if (attr == DW_AT_HP_block_index)
22445 return "DW_AT_HP_block_index";
c764a876 22446#endif
f39c6ffd
TT
22447
22448 name = get_DW_AT_name (attr);
22449
22450 if (name == NULL)
22451 return "DW_AT_<unknown>";
22452
22453 return name;
c906108c
SS
22454}
22455
22456/* Convert a DWARF value form code into its string name. */
22457
f39c6ffd 22458static const char *
aa1ee363 22459dwarf_form_name (unsigned form)
c906108c 22460{
f39c6ffd
TT
22461 const char *name = get_DW_FORM_name (form);
22462
22463 if (name == NULL)
22464 return "DW_FORM_<unknown>";
22465
22466 return name;
c906108c
SS
22467}
22468
a121b7c1 22469static const char *
fba45db2 22470dwarf_bool_name (unsigned mybool)
c906108c
SS
22471{
22472 if (mybool)
22473 return "TRUE";
22474 else
22475 return "FALSE";
22476}
22477
22478/* Convert a DWARF type code into its string name. */
22479
f39c6ffd 22480static const char *
aa1ee363 22481dwarf_type_encoding_name (unsigned enc)
c906108c 22482{
f39c6ffd 22483 const char *name = get_DW_ATE_name (enc);
c906108c 22484
f39c6ffd
TT
22485 if (name == NULL)
22486 return "DW_ATE_<unknown>";
c906108c 22487
f39c6ffd 22488 return name;
c906108c 22489}
c906108c 22490
f9aca02d 22491static void
d97bc12b 22492dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
c906108c
SS
22493{
22494 unsigned int i;
22495
d97bc12b 22496 print_spaces (indent, f);
9d8780f0 22497 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset %s)\n",
9c541725 22498 dwarf_tag_name (die->tag), die->abbrev,
9d8780f0 22499 sect_offset_str (die->sect_off));
d97bc12b
DE
22500
22501 if (die->parent != NULL)
22502 {
22503 print_spaces (indent, f);
9d8780f0
SM
22504 fprintf_unfiltered (f, " parent at offset: %s\n",
22505 sect_offset_str (die->parent->sect_off));
d97bc12b
DE
22506 }
22507
22508 print_spaces (indent, f);
22509 fprintf_unfiltered (f, " has children: %s\n",
639d11d3 22510 dwarf_bool_name (die->child != NULL));
c906108c 22511
d97bc12b
DE
22512 print_spaces (indent, f);
22513 fprintf_unfiltered (f, " attributes:\n");
22514
c906108c
SS
22515 for (i = 0; i < die->num_attrs; ++i)
22516 {
d97bc12b
DE
22517 print_spaces (indent, f);
22518 fprintf_unfiltered (f, " %s (%s) ",
c906108c
SS
22519 dwarf_attr_name (die->attrs[i].name),
22520 dwarf_form_name (die->attrs[i].form));
d97bc12b 22521
c906108c
SS
22522 switch (die->attrs[i].form)
22523 {
c906108c 22524 case DW_FORM_addr:
3019eac3 22525 case DW_FORM_GNU_addr_index:
d97bc12b 22526 fprintf_unfiltered (f, "address: ");
5af949e3 22527 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
c906108c
SS
22528 break;
22529 case DW_FORM_block2:
22530 case DW_FORM_block4:
22531 case DW_FORM_block:
22532 case DW_FORM_block1:
56eb65bd
SP
22533 fprintf_unfiltered (f, "block: size %s",
22534 pulongest (DW_BLOCK (&die->attrs[i])->size));
c906108c 22535 break;
2dc7f7b3 22536 case DW_FORM_exprloc:
56eb65bd
SP
22537 fprintf_unfiltered (f, "expression: size %s",
22538 pulongest (DW_BLOCK (&die->attrs[i])->size));
2dc7f7b3 22539 break;
0224619f
JK
22540 case DW_FORM_data16:
22541 fprintf_unfiltered (f, "constant of 16 bytes");
22542 break;
4568ecf9
DE
22543 case DW_FORM_ref_addr:
22544 fprintf_unfiltered (f, "ref address: ");
22545 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
22546 break;
36586728
TT
22547 case DW_FORM_GNU_ref_alt:
22548 fprintf_unfiltered (f, "alt ref address: ");
22549 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
22550 break;
10b3939b
DJ
22551 case DW_FORM_ref1:
22552 case DW_FORM_ref2:
22553 case DW_FORM_ref4:
4568ecf9
DE
22554 case DW_FORM_ref8:
22555 case DW_FORM_ref_udata:
d97bc12b 22556 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
4568ecf9 22557 (long) (DW_UNSND (&die->attrs[i])));
10b3939b 22558 break;
c906108c
SS
22559 case DW_FORM_data1:
22560 case DW_FORM_data2:
22561 case DW_FORM_data4:
ce5d95e1 22562 case DW_FORM_data8:
c906108c
SS
22563 case DW_FORM_udata:
22564 case DW_FORM_sdata:
43bbcdc2
PH
22565 fprintf_unfiltered (f, "constant: %s",
22566 pulongest (DW_UNSND (&die->attrs[i])));
c906108c 22567 break;
2dc7f7b3
TT
22568 case DW_FORM_sec_offset:
22569 fprintf_unfiltered (f, "section offset: %s",
22570 pulongest (DW_UNSND (&die->attrs[i])));
22571 break;
55f1336d 22572 case DW_FORM_ref_sig8:
ac9ec31b
DE
22573 fprintf_unfiltered (f, "signature: %s",
22574 hex_string (DW_SIGNATURE (&die->attrs[i])));
348e048f 22575 break;
c906108c 22576 case DW_FORM_string:
4bdf3d34 22577 case DW_FORM_strp:
43988095 22578 case DW_FORM_line_strp:
3019eac3 22579 case DW_FORM_GNU_str_index:
36586728 22580 case DW_FORM_GNU_strp_alt:
8285870a 22581 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
c906108c 22582 DW_STRING (&die->attrs[i])
8285870a
JK
22583 ? DW_STRING (&die->attrs[i]) : "",
22584 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
c906108c
SS
22585 break;
22586 case DW_FORM_flag:
22587 if (DW_UNSND (&die->attrs[i]))
d97bc12b 22588 fprintf_unfiltered (f, "flag: TRUE");
c906108c 22589 else
d97bc12b 22590 fprintf_unfiltered (f, "flag: FALSE");
c906108c 22591 break;
2dc7f7b3
TT
22592 case DW_FORM_flag_present:
22593 fprintf_unfiltered (f, "flag: TRUE");
22594 break;
a8329558 22595 case DW_FORM_indirect:
0963b4bd
MS
22596 /* The reader will have reduced the indirect form to
22597 the "base form" so this form should not occur. */
3e43a32a
MS
22598 fprintf_unfiltered (f,
22599 "unexpected attribute form: DW_FORM_indirect");
a8329558 22600 break;
663c44ac
JK
22601 case DW_FORM_implicit_const:
22602 fprintf_unfiltered (f, "constant: %s",
22603 plongest (DW_SND (&die->attrs[i])));
22604 break;
c906108c 22605 default:
d97bc12b 22606 fprintf_unfiltered (f, "unsupported attribute form: %d.",
c5aa993b 22607 die->attrs[i].form);
d97bc12b 22608 break;
c906108c 22609 }
d97bc12b 22610 fprintf_unfiltered (f, "\n");
c906108c
SS
22611 }
22612}
22613
f9aca02d 22614static void
d97bc12b 22615dump_die_for_error (struct die_info *die)
c906108c 22616{
d97bc12b
DE
22617 dump_die_shallow (gdb_stderr, 0, die);
22618}
22619
22620static void
22621dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
22622{
22623 int indent = level * 4;
22624
22625 gdb_assert (die != NULL);
22626
22627 if (level >= max_level)
22628 return;
22629
22630 dump_die_shallow (f, indent, die);
22631
22632 if (die->child != NULL)
c906108c 22633 {
d97bc12b
DE
22634 print_spaces (indent, f);
22635 fprintf_unfiltered (f, " Children:");
22636 if (level + 1 < max_level)
22637 {
22638 fprintf_unfiltered (f, "\n");
22639 dump_die_1 (f, level + 1, max_level, die->child);
22640 }
22641 else
22642 {
3e43a32a
MS
22643 fprintf_unfiltered (f,
22644 " [not printed, max nesting level reached]\n");
d97bc12b
DE
22645 }
22646 }
22647
22648 if (die->sibling != NULL && level > 0)
22649 {
22650 dump_die_1 (f, level, max_level, die->sibling);
c906108c
SS
22651 }
22652}
22653
d97bc12b
DE
22654/* This is called from the pdie macro in gdbinit.in.
22655 It's not static so gcc will keep a copy callable from gdb. */
22656
22657void
22658dump_die (struct die_info *die, int max_level)
22659{
22660 dump_die_1 (gdb_stdlog, 0, max_level, die);
22661}
22662
f9aca02d 22663static void
51545339 22664store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
c906108c 22665{
51545339 22666 void **slot;
c906108c 22667
9c541725
PA
22668 slot = htab_find_slot_with_hash (cu->die_hash, die,
22669 to_underlying (die->sect_off),
b64f50a1 22670 INSERT);
51545339
DJ
22671
22672 *slot = die;
c906108c
SS
22673}
22674
b64f50a1
JK
22675/* Return DIE offset of ATTR. Return 0 with complaint if ATTR is not of the
22676 required kind. */
22677
22678static sect_offset
ff39bb5e 22679dwarf2_get_ref_die_offset (const struct attribute *attr)
93311388 22680{
7771576e 22681 if (attr_form_is_ref (attr))
9c541725 22682 return (sect_offset) DW_UNSND (attr);
93311388
DE
22683
22684 complaint (&symfile_complaints,
22685 _("unsupported die ref attribute form: '%s'"),
22686 dwarf_form_name (attr->form));
9c541725 22687 return {};
c906108c
SS
22688}
22689
43bbcdc2
PH
22690/* Return the constant value held by ATTR. Return DEFAULT_VALUE if
22691 * the value held by the attribute is not constant. */
a02abb62 22692
43bbcdc2 22693static LONGEST
ff39bb5e 22694dwarf2_get_attr_constant_value (const struct attribute *attr, int default_value)
a02abb62 22695{
663c44ac 22696 if (attr->form == DW_FORM_sdata || attr->form == DW_FORM_implicit_const)
a02abb62
JB
22697 return DW_SND (attr);
22698 else if (attr->form == DW_FORM_udata
22699 || attr->form == DW_FORM_data1
22700 || attr->form == DW_FORM_data2
22701 || attr->form == DW_FORM_data4
22702 || attr->form == DW_FORM_data8)
22703 return DW_UNSND (attr);
22704 else
22705 {
0224619f 22706 /* For DW_FORM_data16 see attr_form_is_constant. */
3e43a32a
MS
22707 complaint (&symfile_complaints,
22708 _("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
TT
22773 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
22774 load_full_comp_unit (per_cu, 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);
95554aad 22782 load_full_comp_unit (cu->per_cu, 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
JK
22835 if (per_cu->cu == NULL)
22836 load_cu (per_cu);
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
TT
22942 if (per_cu->cu == NULL)
22943 load_cu (per_cu);
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:
23045 complaint (&symfile_complaints,
23046 _("unsupported const value attribute form: '%s'"),
23047 dwarf_form_name (attr->form));
23048 break;
23049 }
23050
23051 return result;
23052}
23053
7942e96e
AA
23054/* Return the type of the die at OFFSET in PER_CU. Return NULL if no
23055 valid type for this die is found. */
23056
23057struct type *
9c541725 23058dwarf2_fetch_die_type_sect_off (sect_offset sect_off,
7942e96e
AA
23059 struct dwarf2_per_cu_data *per_cu)
23060{
23061 struct dwarf2_cu *cu;
23062 struct die_info *die;
23063
7942e96e
AA
23064 if (per_cu->cu == NULL)
23065 load_cu (per_cu);
23066 cu = per_cu->cu;
23067 if (!cu)
23068 return NULL;
23069
9c541725 23070 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
7942e96e
AA
23071 if (!die)
23072 return NULL;
23073
23074 return die_type (die, cu);
23075}
23076
8a9b8146
TT
23077/* Return the type of the DIE at DIE_OFFSET in the CU named by
23078 PER_CU. */
23079
23080struct type *
b64f50a1 23081dwarf2_get_die_type (cu_offset die_offset,
8a9b8146
TT
23082 struct dwarf2_per_cu_data *per_cu)
23083{
9c541725 23084 sect_offset die_offset_sect = per_cu->sect_off + to_underlying (die_offset);
b64f50a1 23085 return get_die_type_at_offset (die_offset_sect, per_cu);
8a9b8146
TT
23086}
23087
ac9ec31b 23088/* Follow type unit SIG_TYPE referenced by SRC_DIE.
348e048f 23089 On entry *REF_CU is the CU of SRC_DIE.
ac9ec31b
DE
23090 On exit *REF_CU is the CU of the result.
23091 Returns NULL if the referenced DIE isn't found. */
348e048f
DE
23092
23093static struct die_info *
ac9ec31b
DE
23094follow_die_sig_1 (struct die_info *src_die, struct signatured_type *sig_type,
23095 struct dwarf2_cu **ref_cu)
348e048f 23096{
348e048f 23097 struct die_info temp_die;
348e048f
DE
23098 struct dwarf2_cu *sig_cu;
23099 struct die_info *die;
23100
ac9ec31b
DE
23101 /* While it might be nice to assert sig_type->type == NULL here,
23102 we can get here for DW_AT_imported_declaration where we need
23103 the DIE not the type. */
348e048f
DE
23104
23105 /* If necessary, add it to the queue and load its DIEs. */
23106
95554aad 23107 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, language_minimal))
a0f42c21 23108 read_signatured_type (sig_type);
348e048f 23109
348e048f 23110 sig_cu = sig_type->per_cu.cu;
69d751e3 23111 gdb_assert (sig_cu != NULL);
9c541725
PA
23112 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
23113 temp_die.sect_off = sig_type->type_offset_in_section;
9a3c8263 23114 die = (struct die_info *) htab_find_with_hash (sig_cu->die_hash, &temp_die,
9c541725 23115 to_underlying (temp_die.sect_off));
348e048f
DE
23116 if (die)
23117 {
ed2dc618 23118 struct dwarf2_per_objfile *dwarf2_per_objfile
518817b3 23119 = (*ref_cu)->per_cu->dwarf2_per_objfile;
ed2dc618 23120
796a7ff8
DE
23121 /* For .gdb_index version 7 keep track of included TUs.
23122 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
23123 if (dwarf2_per_objfile->index_table != NULL
23124 && dwarf2_per_objfile->index_table->version <= 7)
23125 {
23126 VEC_safe_push (dwarf2_per_cu_ptr,
23127 (*ref_cu)->per_cu->imported_symtabs,
23128 sig_cu->per_cu);
23129 }
23130
348e048f
DE
23131 *ref_cu = sig_cu;
23132 return die;
23133 }
23134
ac9ec31b
DE
23135 return NULL;
23136}
23137
23138/* Follow signatured type referenced by ATTR in SRC_DIE.
23139 On entry *REF_CU is the CU of SRC_DIE.
23140 On exit *REF_CU is the CU of the result.
23141 The result is the DIE of the type.
23142 If the referenced type cannot be found an error is thrown. */
23143
23144static struct die_info *
ff39bb5e 23145follow_die_sig (struct die_info *src_die, const struct attribute *attr,
ac9ec31b
DE
23146 struct dwarf2_cu **ref_cu)
23147{
23148 ULONGEST signature = DW_SIGNATURE (attr);
23149 struct signatured_type *sig_type;
23150 struct die_info *die;
23151
23152 gdb_assert (attr->form == DW_FORM_ref_sig8);
23153
a2ce51a0 23154 sig_type = lookup_signatured_type (*ref_cu, signature);
ac9ec31b
DE
23155 /* sig_type will be NULL if the signatured type is missing from
23156 the debug info. */
23157 if (sig_type == NULL)
23158 {
23159 error (_("Dwarf Error: Cannot find signatured DIE %s referenced"
9d8780f0
SM
23160 " from DIE at %s [in module %s]"),
23161 hex_string (signature), sect_offset_str (src_die->sect_off),
518817b3 23162 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
ac9ec31b
DE
23163 }
23164
23165 die = follow_die_sig_1 (src_die, sig_type, ref_cu);
23166 if (die == NULL)
23167 {
23168 dump_die_for_error (src_die);
23169 error (_("Dwarf Error: Problem reading signatured DIE %s referenced"
9d8780f0
SM
23170 " from DIE at %s [in module %s]"),
23171 hex_string (signature), sect_offset_str (src_die->sect_off),
518817b3 23172 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
ac9ec31b
DE
23173 }
23174
23175 return die;
23176}
23177
23178/* Get the type specified by SIGNATURE referenced in DIE/CU,
23179 reading in and processing the type unit if necessary. */
23180
23181static struct type *
23182get_signatured_type (struct die_info *die, ULONGEST signature,
23183 struct dwarf2_cu *cu)
23184{
518817b3
SM
23185 struct dwarf2_per_objfile *dwarf2_per_objfile
23186 = cu->per_cu->dwarf2_per_objfile;
ac9ec31b
DE
23187 struct signatured_type *sig_type;
23188 struct dwarf2_cu *type_cu;
23189 struct die_info *type_die;
23190 struct type *type;
23191
a2ce51a0 23192 sig_type = lookup_signatured_type (cu, signature);
ac9ec31b
DE
23193 /* sig_type will be NULL if the signatured type is missing from
23194 the debug info. */
23195 if (sig_type == NULL)
23196 {
23197 complaint (&symfile_complaints,
23198 _("Dwarf Error: Cannot find signatured DIE %s referenced"
9d8780f0
SM
23199 " from DIE at %s [in module %s]"),
23200 hex_string (signature), sect_offset_str (die->sect_off),
4262abfb 23201 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
23202 return build_error_marker_type (cu, die);
23203 }
23204
23205 /* If we already know the type we're done. */
23206 if (sig_type->type != NULL)
23207 return sig_type->type;
23208
23209 type_cu = cu;
23210 type_die = follow_die_sig_1 (die, sig_type, &type_cu);
23211 if (type_die != NULL)
23212 {
23213 /* N.B. We need to call get_die_type to ensure only one type for this DIE
23214 is created. This is important, for example, because for c++ classes
23215 we need TYPE_NAME set which is only done by new_symbol. Blech. */
23216 type = read_type_die (type_die, type_cu);
23217 if (type == NULL)
23218 {
23219 complaint (&symfile_complaints,
23220 _("Dwarf Error: Cannot build signatured type %s"
9d8780f0
SM
23221 " referenced from DIE at %s [in module %s]"),
23222 hex_string (signature), sect_offset_str (die->sect_off),
4262abfb 23223 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
23224 type = build_error_marker_type (cu, die);
23225 }
23226 }
23227 else
23228 {
23229 complaint (&symfile_complaints,
23230 _("Dwarf Error: Problem reading signatured DIE %s referenced"
9d8780f0
SM
23231 " from DIE at %s [in module %s]"),
23232 hex_string (signature), sect_offset_str (die->sect_off),
4262abfb 23233 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
23234 type = build_error_marker_type (cu, die);
23235 }
23236 sig_type->type = type;
23237
23238 return type;
23239}
23240
23241/* Get the type specified by the DW_AT_signature ATTR in DIE/CU,
23242 reading in and processing the type unit if necessary. */
23243
23244static struct type *
ff39bb5e 23245get_DW_AT_signature_type (struct die_info *die, const struct attribute *attr,
b385a60d 23246 struct dwarf2_cu *cu) /* ARI: editCase function */
ac9ec31b
DE
23247{
23248 /* Yes, DW_AT_signature can use a non-ref_sig8 reference. */
7771576e 23249 if (attr_form_is_ref (attr))
ac9ec31b
DE
23250 {
23251 struct dwarf2_cu *type_cu = cu;
23252 struct die_info *type_die = follow_die_ref (die, attr, &type_cu);
23253
23254 return read_type_die (type_die, type_cu);
23255 }
23256 else if (attr->form == DW_FORM_ref_sig8)
23257 {
23258 return get_signatured_type (die, DW_SIGNATURE (attr), cu);
23259 }
23260 else
23261 {
518817b3
SM
23262 struct dwarf2_per_objfile *dwarf2_per_objfile
23263 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 23264
ac9ec31b
DE
23265 complaint (&symfile_complaints,
23266 _("Dwarf Error: DW_AT_signature has bad form %s in DIE"
9d8780f0
SM
23267 " at %s [in module %s]"),
23268 dwarf_form_name (attr->form), sect_offset_str (die->sect_off),
4262abfb 23269 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
23270 return build_error_marker_type (cu, die);
23271 }
348e048f
DE
23272}
23273
e5fe5e75 23274/* Load the DIEs associated with type unit PER_CU into memory. */
348e048f
DE
23275
23276static void
e5fe5e75 23277load_full_type_unit (struct dwarf2_per_cu_data *per_cu)
348e048f 23278{
52dc124a 23279 struct signatured_type *sig_type;
348e048f 23280
f4dc4d17
DE
23281 /* Caller is responsible for ensuring type_unit_groups don't get here. */
23282 gdb_assert (! IS_TYPE_UNIT_GROUP (per_cu));
23283
6721b2ec
DE
23284 /* We have the per_cu, but we need the signatured_type.
23285 Fortunately this is an easy translation. */
23286 gdb_assert (per_cu->is_debug_types);
23287 sig_type = (struct signatured_type *) per_cu;
348e048f 23288
6721b2ec 23289 gdb_assert (per_cu->cu == NULL);
348e048f 23290
52dc124a 23291 read_signatured_type (sig_type);
348e048f 23292
6721b2ec 23293 gdb_assert (per_cu->cu != NULL);
348e048f
DE
23294}
23295
dee91e82
DE
23296/* die_reader_func for read_signatured_type.
23297 This is identical to load_full_comp_unit_reader,
23298 but is kept separate for now. */
348e048f
DE
23299
23300static void
dee91e82 23301read_signatured_type_reader (const struct die_reader_specs *reader,
d521ce57 23302 const gdb_byte *info_ptr,
dee91e82
DE
23303 struct die_info *comp_unit_die,
23304 int has_children,
23305 void *data)
348e048f 23306{
dee91e82 23307 struct dwarf2_cu *cu = reader->cu;
348e048f 23308
dee91e82
DE
23309 gdb_assert (cu->die_hash == NULL);
23310 cu->die_hash =
23311 htab_create_alloc_ex (cu->header.length / 12,
23312 die_hash,
23313 die_eq,
23314 NULL,
23315 &cu->comp_unit_obstack,
23316 hashtab_obstack_allocate,
23317 dummy_obstack_deallocate);
348e048f 23318
dee91e82
DE
23319 if (has_children)
23320 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
23321 &info_ptr, comp_unit_die);
23322 cu->dies = comp_unit_die;
23323 /* comp_unit_die is not stored in die_hash, no need. */
348e048f
DE
23324
23325 /* We try not to read any attributes in this function, because not
9cdd5dbd 23326 all CUs needed for references have been loaded yet, and symbol
348e048f 23327 table processing isn't initialized. But we have to set the CU language,
dee91e82
DE
23328 or we won't be able to build types correctly.
23329 Similarly, if we do not read the producer, we can not apply
23330 producer-specific interpretation. */
95554aad 23331 prepare_one_comp_unit (cu, cu->dies, language_minimal);
dee91e82 23332}
348e048f 23333
3019eac3
DE
23334/* Read in a signatured type and build its CU and DIEs.
23335 If the type is a stub for the real type in a DWO file,
23336 read in the real type from the DWO file as well. */
dee91e82
DE
23337
23338static void
23339read_signatured_type (struct signatured_type *sig_type)
23340{
23341 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
348e048f 23342
3019eac3 23343 gdb_assert (per_cu->is_debug_types);
dee91e82 23344 gdb_assert (per_cu->cu == NULL);
348e048f 23345
f4dc4d17
DE
23346 init_cutu_and_read_dies (per_cu, NULL, 0, 1,
23347 read_signatured_type_reader, NULL);
7ee85ab1 23348 sig_type->per_cu.tu_read = 1;
c906108c
SS
23349}
23350
c906108c
SS
23351/* Decode simple location descriptions.
23352 Given a pointer to a dwarf block that defines a location, compute
23353 the location and return the value.
23354
4cecd739
DJ
23355 NOTE drow/2003-11-18: This function is called in two situations
23356 now: for the address of static or global variables (partial symbols
23357 only) and for offsets into structures which are expected to be
23358 (more or less) constant. The partial symbol case should go away,
23359 and only the constant case should remain. That will let this
23360 function complain more accurately. A few special modes are allowed
23361 without complaint for global variables (for instance, global
23362 register values and thread-local values).
c906108c
SS
23363
23364 A location description containing no operations indicates that the
4cecd739 23365 object is optimized out. The return value is 0 for that case.
6b992462
DJ
23366 FIXME drow/2003-11-16: No callers check for this case any more; soon all
23367 callers will only want a very basic result and this can become a
21ae7a4d
JK
23368 complaint.
23369
23370 Note that stack[0] is unused except as a default error return. */
c906108c
SS
23371
23372static CORE_ADDR
e7c27a73 23373decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
c906108c 23374{
518817b3 23375 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
56eb65bd
SP
23376 size_t i;
23377 size_t size = blk->size;
d521ce57 23378 const gdb_byte *data = blk->data;
21ae7a4d
JK
23379 CORE_ADDR stack[64];
23380 int stacki;
23381 unsigned int bytes_read, unsnd;
23382 gdb_byte op;
c906108c 23383
21ae7a4d
JK
23384 i = 0;
23385 stacki = 0;
23386 stack[stacki] = 0;
23387 stack[++stacki] = 0;
23388
23389 while (i < size)
23390 {
23391 op = data[i++];
23392 switch (op)
23393 {
23394 case DW_OP_lit0:
23395 case DW_OP_lit1:
23396 case DW_OP_lit2:
23397 case DW_OP_lit3:
23398 case DW_OP_lit4:
23399 case DW_OP_lit5:
23400 case DW_OP_lit6:
23401 case DW_OP_lit7:
23402 case DW_OP_lit8:
23403 case DW_OP_lit9:
23404 case DW_OP_lit10:
23405 case DW_OP_lit11:
23406 case DW_OP_lit12:
23407 case DW_OP_lit13:
23408 case DW_OP_lit14:
23409 case DW_OP_lit15:
23410 case DW_OP_lit16:
23411 case DW_OP_lit17:
23412 case DW_OP_lit18:
23413 case DW_OP_lit19:
23414 case DW_OP_lit20:
23415 case DW_OP_lit21:
23416 case DW_OP_lit22:
23417 case DW_OP_lit23:
23418 case DW_OP_lit24:
23419 case DW_OP_lit25:
23420 case DW_OP_lit26:
23421 case DW_OP_lit27:
23422 case DW_OP_lit28:
23423 case DW_OP_lit29:
23424 case DW_OP_lit30:
23425 case DW_OP_lit31:
23426 stack[++stacki] = op - DW_OP_lit0;
23427 break;
f1bea926 23428
21ae7a4d
JK
23429 case DW_OP_reg0:
23430 case DW_OP_reg1:
23431 case DW_OP_reg2:
23432 case DW_OP_reg3:
23433 case DW_OP_reg4:
23434 case DW_OP_reg5:
23435 case DW_OP_reg6:
23436 case DW_OP_reg7:
23437 case DW_OP_reg8:
23438 case DW_OP_reg9:
23439 case DW_OP_reg10:
23440 case DW_OP_reg11:
23441 case DW_OP_reg12:
23442 case DW_OP_reg13:
23443 case DW_OP_reg14:
23444 case DW_OP_reg15:
23445 case DW_OP_reg16:
23446 case DW_OP_reg17:
23447 case DW_OP_reg18:
23448 case DW_OP_reg19:
23449 case DW_OP_reg20:
23450 case DW_OP_reg21:
23451 case DW_OP_reg22:
23452 case DW_OP_reg23:
23453 case DW_OP_reg24:
23454 case DW_OP_reg25:
23455 case DW_OP_reg26:
23456 case DW_OP_reg27:
23457 case DW_OP_reg28:
23458 case DW_OP_reg29:
23459 case DW_OP_reg30:
23460 case DW_OP_reg31:
23461 stack[++stacki] = op - DW_OP_reg0;
23462 if (i < size)
23463 dwarf2_complex_location_expr_complaint ();
23464 break;
c906108c 23465
21ae7a4d
JK
23466 case DW_OP_regx:
23467 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
23468 i += bytes_read;
23469 stack[++stacki] = unsnd;
23470 if (i < size)
23471 dwarf2_complex_location_expr_complaint ();
23472 break;
c906108c 23473
21ae7a4d
JK
23474 case DW_OP_addr:
23475 stack[++stacki] = read_address (objfile->obfd, &data[i],
23476 cu, &bytes_read);
23477 i += bytes_read;
23478 break;
d53d4ac5 23479
21ae7a4d
JK
23480 case DW_OP_const1u:
23481 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
23482 i += 1;
23483 break;
23484
23485 case DW_OP_const1s:
23486 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
23487 i += 1;
23488 break;
23489
23490 case DW_OP_const2u:
23491 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
23492 i += 2;
23493 break;
23494
23495 case DW_OP_const2s:
23496 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
23497 i += 2;
23498 break;
d53d4ac5 23499
21ae7a4d
JK
23500 case DW_OP_const4u:
23501 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
23502 i += 4;
23503 break;
23504
23505 case DW_OP_const4s:
23506 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
23507 i += 4;
23508 break;
23509
585861ea
JK
23510 case DW_OP_const8u:
23511 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
23512 i += 8;
23513 break;
23514
21ae7a4d
JK
23515 case DW_OP_constu:
23516 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
23517 &bytes_read);
23518 i += bytes_read;
23519 break;
23520
23521 case DW_OP_consts:
23522 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
23523 i += bytes_read;
23524 break;
23525
23526 case DW_OP_dup:
23527 stack[stacki + 1] = stack[stacki];
23528 stacki++;
23529 break;
23530
23531 case DW_OP_plus:
23532 stack[stacki - 1] += stack[stacki];
23533 stacki--;
23534 break;
23535
23536 case DW_OP_plus_uconst:
23537 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
23538 &bytes_read);
23539 i += bytes_read;
23540 break;
23541
23542 case DW_OP_minus:
23543 stack[stacki - 1] -= stack[stacki];
23544 stacki--;
23545 break;
23546
23547 case DW_OP_deref:
23548 /* If we're not the last op, then we definitely can't encode
23549 this using GDB's address_class enum. This is valid for partial
23550 global symbols, although the variable's address will be bogus
23551 in the psymtab. */
23552 if (i < size)
23553 dwarf2_complex_location_expr_complaint ();
23554 break;
23555
23556 case DW_OP_GNU_push_tls_address:
4aa4e28b 23557 case DW_OP_form_tls_address:
21ae7a4d
JK
23558 /* The top of the stack has the offset from the beginning
23559 of the thread control block at which the variable is located. */
23560 /* Nothing should follow this operator, so the top of stack would
23561 be returned. */
23562 /* This is valid for partial global symbols, but the variable's
585861ea
JK
23563 address will be bogus in the psymtab. Make it always at least
23564 non-zero to not look as a variable garbage collected by linker
23565 which have DW_OP_addr 0. */
21ae7a4d
JK
23566 if (i < size)
23567 dwarf2_complex_location_expr_complaint ();
585861ea 23568 stack[stacki]++;
21ae7a4d
JK
23569 break;
23570
23571 case DW_OP_GNU_uninit:
23572 break;
23573
3019eac3 23574 case DW_OP_GNU_addr_index:
49f6c839 23575 case DW_OP_GNU_const_index:
3019eac3
DE
23576 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
23577 &bytes_read);
23578 i += bytes_read;
23579 break;
23580
21ae7a4d
JK
23581 default:
23582 {
f39c6ffd 23583 const char *name = get_DW_OP_name (op);
21ae7a4d
JK
23584
23585 if (name)
23586 complaint (&symfile_complaints, _("unsupported stack op: '%s'"),
23587 name);
23588 else
23589 complaint (&symfile_complaints, _("unsupported stack op: '%02x'"),
23590 op);
23591 }
23592
23593 return (stack[stacki]);
d53d4ac5 23594 }
3c6e0cb3 23595
21ae7a4d
JK
23596 /* Enforce maximum stack depth of SIZE-1 to avoid writing
23597 outside of the allocated space. Also enforce minimum>0. */
23598 if (stacki >= ARRAY_SIZE (stack) - 1)
23599 {
23600 complaint (&symfile_complaints,
23601 _("location description stack overflow"));
23602 return 0;
23603 }
23604
23605 if (stacki <= 0)
23606 {
23607 complaint (&symfile_complaints,
23608 _("location description stack underflow"));
23609 return 0;
23610 }
23611 }
23612 return (stack[stacki]);
c906108c
SS
23613}
23614
23615/* memory allocation interface */
23616
c906108c 23617static struct dwarf_block *
7b5a2f43 23618dwarf_alloc_block (struct dwarf2_cu *cu)
c906108c 23619{
8d749320 23620 return XOBNEW (&cu->comp_unit_obstack, struct dwarf_block);
c906108c
SS
23621}
23622
c906108c 23623static struct die_info *
b60c80d6 23624dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
c906108c
SS
23625{
23626 struct die_info *die;
b60c80d6
DJ
23627 size_t size = sizeof (struct die_info);
23628
23629 if (num_attrs > 1)
23630 size += (num_attrs - 1) * sizeof (struct attribute);
c906108c 23631
b60c80d6 23632 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
c906108c
SS
23633 memset (die, 0, sizeof (struct die_info));
23634 return (die);
23635}
2e276125
JB
23636
23637\f
23638/* Macro support. */
23639
233d95b5
JK
23640/* Return file name relative to the compilation directory of file number I in
23641 *LH's file name table. The result is allocated using xmalloc; the caller is
2e276125 23642 responsible for freeing it. */
233d95b5 23643
2e276125 23644static char *
233d95b5 23645file_file_name (int file, struct line_header *lh)
2e276125 23646{
6a83a1e6
EZ
23647 /* Is the file number a valid index into the line header's file name
23648 table? Remember that file numbers start with one, not zero. */
fff8551c 23649 if (1 <= file && file <= lh->file_names.size ())
6a83a1e6 23650 {
8c43009f 23651 const file_entry &fe = lh->file_names[file - 1];
6e70227d 23652
8c43009f
PA
23653 if (!IS_ABSOLUTE_PATH (fe.name))
23654 {
23655 const char *dir = fe.include_dir (lh);
23656 if (dir != NULL)
23657 return concat (dir, SLASH_STRING, fe.name, (char *) NULL);
23658 }
23659 return xstrdup (fe.name);
6a83a1e6 23660 }
2e276125
JB
23661 else
23662 {
6a83a1e6
EZ
23663 /* The compiler produced a bogus file number. We can at least
23664 record the macro definitions made in the file, even if we
23665 won't be able to find the file by name. */
23666 char fake_name[80];
9a619af0 23667
8c042590
PM
23668 xsnprintf (fake_name, sizeof (fake_name),
23669 "<bad macro file number %d>", file);
2e276125 23670
6e70227d 23671 complaint (&symfile_complaints,
6a83a1e6
EZ
23672 _("bad file number in macro information (%d)"),
23673 file);
2e276125 23674
6a83a1e6 23675 return xstrdup (fake_name);
2e276125
JB
23676 }
23677}
23678
233d95b5
JK
23679/* Return the full name of file number I in *LH's file name table.
23680 Use COMP_DIR as the name of the current directory of the
23681 compilation. The result is allocated using xmalloc; the caller is
23682 responsible for freeing it. */
23683static char *
23684file_full_name (int file, struct line_header *lh, const char *comp_dir)
23685{
23686 /* Is the file number a valid index into the line header's file name
23687 table? Remember that file numbers start with one, not zero. */
fff8551c 23688 if (1 <= file && file <= lh->file_names.size ())
233d95b5
JK
23689 {
23690 char *relative = file_file_name (file, lh);
23691
23692 if (IS_ABSOLUTE_PATH (relative) || comp_dir == NULL)
23693 return relative;
b36cec19
PA
23694 return reconcat (relative, comp_dir, SLASH_STRING,
23695 relative, (char *) NULL);
233d95b5
JK
23696 }
23697 else
23698 return file_file_name (file, lh);
23699}
23700
2e276125
JB
23701
23702static struct macro_source_file *
23703macro_start_file (int file, int line,
23704 struct macro_source_file *current_file,
43f3e411 23705 struct line_header *lh)
2e276125 23706{
233d95b5
JK
23707 /* File name relative to the compilation directory of this source file. */
23708 char *file_name = file_file_name (file, lh);
2e276125 23709
2e276125 23710 if (! current_file)
abc9d0dc 23711 {
fc474241
DE
23712 /* Note: We don't create a macro table for this compilation unit
23713 at all until we actually get a filename. */
43f3e411 23714 struct macro_table *macro_table = get_macro_table ();
fc474241 23715
abc9d0dc
TT
23716 /* If we have no current file, then this must be the start_file
23717 directive for the compilation unit's main source file. */
fc474241
DE
23718 current_file = macro_set_main (macro_table, file_name);
23719 macro_define_special (macro_table);
abc9d0dc 23720 }
2e276125 23721 else
233d95b5 23722 current_file = macro_include (current_file, line, file_name);
2e276125 23723
233d95b5 23724 xfree (file_name);
6e70227d 23725
2e276125
JB
23726 return current_file;
23727}
23728
2e276125
JB
23729static const char *
23730consume_improper_spaces (const char *p, const char *body)
23731{
23732 if (*p == ' ')
23733 {
4d3c2250 23734 complaint (&symfile_complaints,
3e43a32a
MS
23735 _("macro definition contains spaces "
23736 "in formal argument list:\n`%s'"),
4d3c2250 23737 body);
2e276125
JB
23738
23739 while (*p == ' ')
23740 p++;
23741 }
23742
23743 return p;
23744}
23745
23746
23747static void
23748parse_macro_definition (struct macro_source_file *file, int line,
23749 const char *body)
23750{
23751 const char *p;
23752
23753 /* The body string takes one of two forms. For object-like macro
23754 definitions, it should be:
23755
23756 <macro name> " " <definition>
23757
23758 For function-like macro definitions, it should be:
23759
23760 <macro name> "() " <definition>
23761 or
23762 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
23763
23764 Spaces may appear only where explicitly indicated, and in the
23765 <definition>.
23766
23767 The Dwarf 2 spec says that an object-like macro's name is always
23768 followed by a space, but versions of GCC around March 2002 omit
6e70227d 23769 the space when the macro's definition is the empty string.
2e276125
JB
23770
23771 The Dwarf 2 spec says that there should be no spaces between the
23772 formal arguments in a function-like macro's formal argument list,
23773 but versions of GCC around March 2002 include spaces after the
23774 commas. */
23775
23776
23777 /* Find the extent of the macro name. The macro name is terminated
23778 by either a space or null character (for an object-like macro) or
23779 an opening paren (for a function-like macro). */
23780 for (p = body; *p; p++)
23781 if (*p == ' ' || *p == '(')
23782 break;
23783
23784 if (*p == ' ' || *p == '\0')
23785 {
23786 /* It's an object-like macro. */
23787 int name_len = p - body;
3f8a7804 23788 char *name = savestring (body, name_len);
2e276125
JB
23789 const char *replacement;
23790
23791 if (*p == ' ')
23792 replacement = body + name_len + 1;
23793 else
23794 {
4d3c2250 23795 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
23796 replacement = body + name_len;
23797 }
6e70227d 23798
2e276125
JB
23799 macro_define_object (file, line, name, replacement);
23800
23801 xfree (name);
23802 }
23803 else if (*p == '(')
23804 {
23805 /* It's a function-like macro. */
3f8a7804 23806 char *name = savestring (body, p - body);
2e276125
JB
23807 int argc = 0;
23808 int argv_size = 1;
8d749320 23809 char **argv = XNEWVEC (char *, argv_size);
2e276125
JB
23810
23811 p++;
23812
23813 p = consume_improper_spaces (p, body);
23814
23815 /* Parse the formal argument list. */
23816 while (*p && *p != ')')
23817 {
23818 /* Find the extent of the current argument name. */
23819 const char *arg_start = p;
23820
23821 while (*p && *p != ',' && *p != ')' && *p != ' ')
23822 p++;
23823
23824 if (! *p || p == arg_start)
4d3c2250 23825 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
23826 else
23827 {
23828 /* Make sure argv has room for the new argument. */
23829 if (argc >= argv_size)
23830 {
23831 argv_size *= 2;
224c3ddb 23832 argv = XRESIZEVEC (char *, argv, argv_size);
2e276125
JB
23833 }
23834
3f8a7804 23835 argv[argc++] = savestring (arg_start, p - arg_start);
2e276125
JB
23836 }
23837
23838 p = consume_improper_spaces (p, body);
23839
23840 /* Consume the comma, if present. */
23841 if (*p == ',')
23842 {
23843 p++;
23844
23845 p = consume_improper_spaces (p, body);
23846 }
23847 }
23848
23849 if (*p == ')')
23850 {
23851 p++;
23852
23853 if (*p == ' ')
23854 /* Perfectly formed definition, no complaints. */
23855 macro_define_function (file, line, name,
6e70227d 23856 argc, (const char **) argv,
2e276125
JB
23857 p + 1);
23858 else if (*p == '\0')
23859 {
23860 /* Complain, but do define it. */
4d3c2250 23861 dwarf2_macro_malformed_definition_complaint (body);
2e276125 23862 macro_define_function (file, line, name,
6e70227d 23863 argc, (const char **) argv,
2e276125
JB
23864 p);
23865 }
23866 else
23867 /* Just complain. */
4d3c2250 23868 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
23869 }
23870 else
23871 /* Just complain. */
4d3c2250 23872 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
23873
23874 xfree (name);
23875 {
23876 int i;
23877
23878 for (i = 0; i < argc; i++)
23879 xfree (argv[i]);
23880 }
23881 xfree (argv);
23882 }
23883 else
4d3c2250 23884 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
23885}
23886
cf2c3c16
TT
23887/* Skip some bytes from BYTES according to the form given in FORM.
23888 Returns the new pointer. */
2e276125 23889
d521ce57
TT
23890static const gdb_byte *
23891skip_form_bytes (bfd *abfd, const gdb_byte *bytes, const gdb_byte *buffer_end,
cf2c3c16
TT
23892 enum dwarf_form form,
23893 unsigned int offset_size,
23894 struct dwarf2_section_info *section)
2e276125 23895{
cf2c3c16 23896 unsigned int bytes_read;
2e276125 23897
cf2c3c16 23898 switch (form)
2e276125 23899 {
cf2c3c16
TT
23900 case DW_FORM_data1:
23901 case DW_FORM_flag:
23902 ++bytes;
23903 break;
23904
23905 case DW_FORM_data2:
23906 bytes += 2;
23907 break;
23908
23909 case DW_FORM_data4:
23910 bytes += 4;
23911 break;
23912
23913 case DW_FORM_data8:
23914 bytes += 8;
23915 break;
23916
0224619f
JK
23917 case DW_FORM_data16:
23918 bytes += 16;
23919 break;
23920
cf2c3c16
TT
23921 case DW_FORM_string:
23922 read_direct_string (abfd, bytes, &bytes_read);
23923 bytes += bytes_read;
23924 break;
23925
23926 case DW_FORM_sec_offset:
23927 case DW_FORM_strp:
36586728 23928 case DW_FORM_GNU_strp_alt:
cf2c3c16
TT
23929 bytes += offset_size;
23930 break;
23931
23932 case DW_FORM_block:
23933 bytes += read_unsigned_leb128 (abfd, bytes, &bytes_read);
23934 bytes += bytes_read;
23935 break;
23936
23937 case DW_FORM_block1:
23938 bytes += 1 + read_1_byte (abfd, bytes);
23939 break;
23940 case DW_FORM_block2:
23941 bytes += 2 + read_2_bytes (abfd, bytes);
23942 break;
23943 case DW_FORM_block4:
23944 bytes += 4 + read_4_bytes (abfd, bytes);
23945 break;
23946
23947 case DW_FORM_sdata:
23948 case DW_FORM_udata:
3019eac3
DE
23949 case DW_FORM_GNU_addr_index:
23950 case DW_FORM_GNU_str_index:
d521ce57 23951 bytes = gdb_skip_leb128 (bytes, buffer_end);
f664829e
DE
23952 if (bytes == NULL)
23953 {
23954 dwarf2_section_buffer_overflow_complaint (section);
23955 return NULL;
23956 }
cf2c3c16
TT
23957 break;
23958
663c44ac
JK
23959 case DW_FORM_implicit_const:
23960 break;
23961
cf2c3c16
TT
23962 default:
23963 {
cf2c3c16
TT
23964 complaint (&symfile_complaints,
23965 _("invalid form 0x%x in `%s'"),
a32a8923 23966 form, get_section_name (section));
cf2c3c16
TT
23967 return NULL;
23968 }
2e276125
JB
23969 }
23970
cf2c3c16
TT
23971 return bytes;
23972}
757a13d0 23973
cf2c3c16
TT
23974/* A helper for dwarf_decode_macros that handles skipping an unknown
23975 opcode. Returns an updated pointer to the macro data buffer; or,
23976 on error, issues a complaint and returns NULL. */
757a13d0 23977
d521ce57 23978static const gdb_byte *
cf2c3c16 23979skip_unknown_opcode (unsigned int opcode,
d521ce57
TT
23980 const gdb_byte **opcode_definitions,
23981 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
cf2c3c16
TT
23982 bfd *abfd,
23983 unsigned int offset_size,
23984 struct dwarf2_section_info *section)
23985{
23986 unsigned int bytes_read, i;
23987 unsigned long arg;
d521ce57 23988 const gdb_byte *defn;
2e276125 23989
cf2c3c16 23990 if (opcode_definitions[opcode] == NULL)
2e276125 23991 {
cf2c3c16
TT
23992 complaint (&symfile_complaints,
23993 _("unrecognized DW_MACFINO opcode 0x%x"),
23994 opcode);
23995 return NULL;
23996 }
2e276125 23997
cf2c3c16
TT
23998 defn = opcode_definitions[opcode];
23999 arg = read_unsigned_leb128 (abfd, defn, &bytes_read);
24000 defn += bytes_read;
2e276125 24001
cf2c3c16
TT
24002 for (i = 0; i < arg; ++i)
24003 {
aead7601
SM
24004 mac_ptr = skip_form_bytes (abfd, mac_ptr, mac_end,
24005 (enum dwarf_form) defn[i], offset_size,
f664829e 24006 section);
cf2c3c16
TT
24007 if (mac_ptr == NULL)
24008 {
24009 /* skip_form_bytes already issued the complaint. */
24010 return NULL;
24011 }
24012 }
757a13d0 24013
cf2c3c16
TT
24014 return mac_ptr;
24015}
757a13d0 24016
cf2c3c16
TT
24017/* A helper function which parses the header of a macro section.
24018 If the macro section is the extended (for now called "GNU") type,
24019 then this updates *OFFSET_SIZE. Returns a pointer to just after
24020 the header, or issues a complaint and returns NULL on error. */
757a13d0 24021
d521ce57
TT
24022static const gdb_byte *
24023dwarf_parse_macro_header (const gdb_byte **opcode_definitions,
cf2c3c16 24024 bfd *abfd,
d521ce57 24025 const gdb_byte *mac_ptr,
cf2c3c16
TT
24026 unsigned int *offset_size,
24027 int section_is_gnu)
24028{
24029 memset (opcode_definitions, 0, 256 * sizeof (gdb_byte *));
757a13d0 24030
cf2c3c16
TT
24031 if (section_is_gnu)
24032 {
24033 unsigned int version, flags;
757a13d0 24034
cf2c3c16 24035 version = read_2_bytes (abfd, mac_ptr);
0af92d60 24036 if (version != 4 && version != 5)
cf2c3c16
TT
24037 {
24038 complaint (&symfile_complaints,
24039 _("unrecognized version `%d' in .debug_macro section"),
24040 version);
24041 return NULL;
24042 }
24043 mac_ptr += 2;
757a13d0 24044
cf2c3c16
TT
24045 flags = read_1_byte (abfd, mac_ptr);
24046 ++mac_ptr;
24047 *offset_size = (flags & 1) ? 8 : 4;
757a13d0 24048
cf2c3c16
TT
24049 if ((flags & 2) != 0)
24050 /* We don't need the line table offset. */
24051 mac_ptr += *offset_size;
757a13d0 24052
cf2c3c16
TT
24053 /* Vendor opcode descriptions. */
24054 if ((flags & 4) != 0)
24055 {
24056 unsigned int i, count;
757a13d0 24057
cf2c3c16
TT
24058 count = read_1_byte (abfd, mac_ptr);
24059 ++mac_ptr;
24060 for (i = 0; i < count; ++i)
24061 {
24062 unsigned int opcode, bytes_read;
24063 unsigned long arg;
24064
24065 opcode = read_1_byte (abfd, mac_ptr);
24066 ++mac_ptr;
24067 opcode_definitions[opcode] = mac_ptr;
24068 arg = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24069 mac_ptr += bytes_read;
24070 mac_ptr += arg;
24071 }
757a13d0 24072 }
cf2c3c16 24073 }
757a13d0 24074
cf2c3c16
TT
24075 return mac_ptr;
24076}
757a13d0 24077
cf2c3c16 24078/* A helper for dwarf_decode_macros that handles the GNU extensions,
0af92d60 24079 including DW_MACRO_import. */
cf2c3c16
TT
24080
24081static void
ed2dc618
SM
24082dwarf_decode_macro_bytes (struct dwarf2_per_objfile *dwarf2_per_objfile,
24083 bfd *abfd,
d521ce57 24084 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
cf2c3c16 24085 struct macro_source_file *current_file,
43f3e411 24086 struct line_header *lh,
cf2c3c16 24087 struct dwarf2_section_info *section,
36586728 24088 int section_is_gnu, int section_is_dwz,
cf2c3c16 24089 unsigned int offset_size,
8fc3fc34 24090 htab_t include_hash)
cf2c3c16 24091{
4d663531 24092 struct objfile *objfile = dwarf2_per_objfile->objfile;
cf2c3c16
TT
24093 enum dwarf_macro_record_type macinfo_type;
24094 int at_commandline;
d521ce57 24095 const gdb_byte *opcode_definitions[256];
757a13d0 24096
cf2c3c16
TT
24097 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
24098 &offset_size, section_is_gnu);
24099 if (mac_ptr == NULL)
24100 {
24101 /* We already issued a complaint. */
24102 return;
24103 }
757a13d0
JK
24104
24105 /* Determines if GDB is still before first DW_MACINFO_start_file. If true
24106 GDB is still reading the definitions from command line. First
24107 DW_MACINFO_start_file will need to be ignored as it was already executed
24108 to create CURRENT_FILE for the main source holding also the command line
24109 definitions. On first met DW_MACINFO_start_file this flag is reset to
24110 normally execute all the remaining DW_MACINFO_start_file macinfos. */
24111
24112 at_commandline = 1;
24113
24114 do
24115 {
24116 /* Do we at least have room for a macinfo type byte? */
24117 if (mac_ptr >= mac_end)
24118 {
f664829e 24119 dwarf2_section_buffer_overflow_complaint (section);
757a13d0
JK
24120 break;
24121 }
24122
aead7601 24123 macinfo_type = (enum dwarf_macro_record_type) read_1_byte (abfd, mac_ptr);
757a13d0
JK
24124 mac_ptr++;
24125
cf2c3c16
TT
24126 /* Note that we rely on the fact that the corresponding GNU and
24127 DWARF constants are the same. */
132448f8
SM
24128 DIAGNOSTIC_PUSH
24129 DIAGNOSTIC_IGNORE_SWITCH_DIFFERENT_ENUM_TYPES
757a13d0
JK
24130 switch (macinfo_type)
24131 {
24132 /* A zero macinfo type indicates the end of the macro
24133 information. */
24134 case 0:
24135 break;
2e276125 24136
0af92d60
JK
24137 case DW_MACRO_define:
24138 case DW_MACRO_undef:
24139 case DW_MACRO_define_strp:
24140 case DW_MACRO_undef_strp:
24141 case DW_MACRO_define_sup:
24142 case DW_MACRO_undef_sup:
2e276125 24143 {
891d2f0b 24144 unsigned int bytes_read;
2e276125 24145 int line;
d521ce57 24146 const char *body;
cf2c3c16 24147 int is_define;
2e276125 24148
cf2c3c16
TT
24149 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24150 mac_ptr += bytes_read;
24151
0af92d60
JK
24152 if (macinfo_type == DW_MACRO_define
24153 || macinfo_type == DW_MACRO_undef)
cf2c3c16
TT
24154 {
24155 body = read_direct_string (abfd, mac_ptr, &bytes_read);
24156 mac_ptr += bytes_read;
24157 }
24158 else
24159 {
24160 LONGEST str_offset;
24161
24162 str_offset = read_offset_1 (abfd, mac_ptr, offset_size);
24163 mac_ptr += offset_size;
2e276125 24164
0af92d60
JK
24165 if (macinfo_type == DW_MACRO_define_sup
24166 || macinfo_type == DW_MACRO_undef_sup
f7a35f02 24167 || section_is_dwz)
36586728 24168 {
ed2dc618
SM
24169 struct dwz_file *dwz
24170 = dwarf2_get_dwz_file (dwarf2_per_objfile);
36586728 24171
ed2dc618
SM
24172 body = read_indirect_string_from_dwz (objfile,
24173 dwz, str_offset);
36586728
TT
24174 }
24175 else
ed2dc618
SM
24176 body = read_indirect_string_at_offset (dwarf2_per_objfile,
24177 abfd, str_offset);
cf2c3c16
TT
24178 }
24179
0af92d60
JK
24180 is_define = (macinfo_type == DW_MACRO_define
24181 || macinfo_type == DW_MACRO_define_strp
24182 || macinfo_type == DW_MACRO_define_sup);
2e276125 24183 if (! current_file)
757a13d0
JK
24184 {
24185 /* DWARF violation as no main source is present. */
24186 complaint (&symfile_complaints,
24187 _("debug info with no main source gives macro %s "
24188 "on line %d: %s"),
cf2c3c16
TT
24189 is_define ? _("definition") : _("undefinition"),
24190 line, body);
757a13d0
JK
24191 break;
24192 }
3e43a32a
MS
24193 if ((line == 0 && !at_commandline)
24194 || (line != 0 && at_commandline))
4d3c2250 24195 complaint (&symfile_complaints,
757a13d0
JK
24196 _("debug info gives %s macro %s with %s line %d: %s"),
24197 at_commandline ? _("command-line") : _("in-file"),
cf2c3c16 24198 is_define ? _("definition") : _("undefinition"),
757a13d0
JK
24199 line == 0 ? _("zero") : _("non-zero"), line, body);
24200
cf2c3c16 24201 if (is_define)
757a13d0 24202 parse_macro_definition (current_file, line, body);
cf2c3c16
TT
24203 else
24204 {
0af92d60
JK
24205 gdb_assert (macinfo_type == DW_MACRO_undef
24206 || macinfo_type == DW_MACRO_undef_strp
24207 || macinfo_type == DW_MACRO_undef_sup);
cf2c3c16
TT
24208 macro_undef (current_file, line, body);
24209 }
2e276125
JB
24210 }
24211 break;
24212
0af92d60 24213 case DW_MACRO_start_file:
2e276125 24214 {
891d2f0b 24215 unsigned int bytes_read;
2e276125
JB
24216 int line, file;
24217
24218 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24219 mac_ptr += bytes_read;
24220 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24221 mac_ptr += bytes_read;
24222
3e43a32a
MS
24223 if ((line == 0 && !at_commandline)
24224 || (line != 0 && at_commandline))
757a13d0
JK
24225 complaint (&symfile_complaints,
24226 _("debug info gives source %d included "
24227 "from %s at %s line %d"),
24228 file, at_commandline ? _("command-line") : _("file"),
24229 line == 0 ? _("zero") : _("non-zero"), line);
24230
24231 if (at_commandline)
24232 {
0af92d60 24233 /* This DW_MACRO_start_file was executed in the
cf2c3c16 24234 pass one. */
757a13d0
JK
24235 at_commandline = 0;
24236 }
24237 else
43f3e411 24238 current_file = macro_start_file (file, line, current_file, lh);
2e276125
JB
24239 }
24240 break;
24241
0af92d60 24242 case DW_MACRO_end_file:
2e276125 24243 if (! current_file)
4d3c2250 24244 complaint (&symfile_complaints,
3e43a32a
MS
24245 _("macro debug info has an unmatched "
24246 "`close_file' directive"));
2e276125
JB
24247 else
24248 {
24249 current_file = current_file->included_by;
24250 if (! current_file)
24251 {
cf2c3c16 24252 enum dwarf_macro_record_type next_type;
2e276125
JB
24253
24254 /* GCC circa March 2002 doesn't produce the zero
24255 type byte marking the end of the compilation
24256 unit. Complain if it's not there, but exit no
24257 matter what. */
24258
24259 /* Do we at least have room for a macinfo type byte? */
24260 if (mac_ptr >= mac_end)
24261 {
f664829e 24262 dwarf2_section_buffer_overflow_complaint (section);
2e276125
JB
24263 return;
24264 }
24265
24266 /* We don't increment mac_ptr here, so this is just
24267 a look-ahead. */
aead7601
SM
24268 next_type
24269 = (enum dwarf_macro_record_type) read_1_byte (abfd,
24270 mac_ptr);
2e276125 24271 if (next_type != 0)
4d3c2250 24272 complaint (&symfile_complaints,
3e43a32a
MS
24273 _("no terminating 0-type entry for "
24274 "macros in `.debug_macinfo' section"));
2e276125
JB
24275
24276 return;
24277 }
24278 }
24279 break;
24280
0af92d60
JK
24281 case DW_MACRO_import:
24282 case DW_MACRO_import_sup:
cf2c3c16
TT
24283 {
24284 LONGEST offset;
8fc3fc34 24285 void **slot;
a036ba48
TT
24286 bfd *include_bfd = abfd;
24287 struct dwarf2_section_info *include_section = section;
d521ce57 24288 const gdb_byte *include_mac_end = mac_end;
a036ba48 24289 int is_dwz = section_is_dwz;
d521ce57 24290 const gdb_byte *new_mac_ptr;
cf2c3c16
TT
24291
24292 offset = read_offset_1 (abfd, mac_ptr, offset_size);
24293 mac_ptr += offset_size;
24294
0af92d60 24295 if (macinfo_type == DW_MACRO_import_sup)
a036ba48 24296 {
ed2dc618 24297 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
a036ba48 24298
4d663531 24299 dwarf2_read_section (objfile, &dwz->macro);
a036ba48 24300
a036ba48 24301 include_section = &dwz->macro;
a32a8923 24302 include_bfd = get_section_bfd_owner (include_section);
a036ba48
TT
24303 include_mac_end = dwz->macro.buffer + dwz->macro.size;
24304 is_dwz = 1;
24305 }
24306
24307 new_mac_ptr = include_section->buffer + offset;
24308 slot = htab_find_slot (include_hash, new_mac_ptr, INSERT);
24309
8fc3fc34
TT
24310 if (*slot != NULL)
24311 {
24312 /* This has actually happened; see
24313 http://sourceware.org/bugzilla/show_bug.cgi?id=13568. */
24314 complaint (&symfile_complaints,
0af92d60 24315 _("recursive DW_MACRO_import in "
8fc3fc34
TT
24316 ".debug_macro section"));
24317 }
24318 else
24319 {
d521ce57 24320 *slot = (void *) new_mac_ptr;
36586728 24321
ed2dc618
SM
24322 dwarf_decode_macro_bytes (dwarf2_per_objfile,
24323 include_bfd, new_mac_ptr,
43f3e411 24324 include_mac_end, current_file, lh,
36586728 24325 section, section_is_gnu, is_dwz,
4d663531 24326 offset_size, include_hash);
8fc3fc34 24327
d521ce57 24328 htab_remove_elt (include_hash, (void *) new_mac_ptr);
8fc3fc34 24329 }
cf2c3c16
TT
24330 }
24331 break;
24332
2e276125 24333 case DW_MACINFO_vendor_ext:
cf2c3c16
TT
24334 if (!section_is_gnu)
24335 {
24336 unsigned int bytes_read;
2e276125 24337
ac298888
TT
24338 /* This reads the constant, but since we don't recognize
24339 any vendor extensions, we ignore it. */
24340 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
cf2c3c16
TT
24341 mac_ptr += bytes_read;
24342 read_direct_string (abfd, mac_ptr, &bytes_read);
24343 mac_ptr += bytes_read;
2e276125 24344
cf2c3c16
TT
24345 /* We don't recognize any vendor extensions. */
24346 break;
24347 }
24348 /* FALLTHROUGH */
24349
24350 default:
24351 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
f664829e 24352 mac_ptr, mac_end, abfd, offset_size,
cf2c3c16
TT
24353 section);
24354 if (mac_ptr == NULL)
24355 return;
24356 break;
2e276125 24357 }
132448f8 24358 DIAGNOSTIC_POP
757a13d0 24359 } while (macinfo_type != 0);
2e276125 24360}
8e19ed76 24361
cf2c3c16 24362static void
09262596 24363dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
43f3e411 24364 int section_is_gnu)
cf2c3c16 24365{
518817b3
SM
24366 struct dwarf2_per_objfile *dwarf2_per_objfile
24367 = cu->per_cu->dwarf2_per_objfile;
bb5ed363 24368 struct objfile *objfile = dwarf2_per_objfile->objfile;
09262596
DE
24369 struct line_header *lh = cu->line_header;
24370 bfd *abfd;
d521ce57 24371 const gdb_byte *mac_ptr, *mac_end;
cf2c3c16
TT
24372 struct macro_source_file *current_file = 0;
24373 enum dwarf_macro_record_type macinfo_type;
24374 unsigned int offset_size = cu->header.offset_size;
d521ce57 24375 const gdb_byte *opcode_definitions[256];
8fc3fc34 24376 void **slot;
09262596
DE
24377 struct dwarf2_section_info *section;
24378 const char *section_name;
24379
24380 if (cu->dwo_unit != NULL)
24381 {
24382 if (section_is_gnu)
24383 {
24384 section = &cu->dwo_unit->dwo_file->sections.macro;
24385 section_name = ".debug_macro.dwo";
24386 }
24387 else
24388 {
24389 section = &cu->dwo_unit->dwo_file->sections.macinfo;
24390 section_name = ".debug_macinfo.dwo";
24391 }
24392 }
24393 else
24394 {
24395 if (section_is_gnu)
24396 {
24397 section = &dwarf2_per_objfile->macro;
24398 section_name = ".debug_macro";
24399 }
24400 else
24401 {
24402 section = &dwarf2_per_objfile->macinfo;
24403 section_name = ".debug_macinfo";
24404 }
24405 }
cf2c3c16 24406
bb5ed363 24407 dwarf2_read_section (objfile, section);
cf2c3c16
TT
24408 if (section->buffer == NULL)
24409 {
fceca515 24410 complaint (&symfile_complaints, _("missing %s section"), section_name);
cf2c3c16
TT
24411 return;
24412 }
a32a8923 24413 abfd = get_section_bfd_owner (section);
cf2c3c16
TT
24414
24415 /* First pass: Find the name of the base filename.
24416 This filename is needed in order to process all macros whose definition
24417 (or undefinition) comes from the command line. These macros are defined
24418 before the first DW_MACINFO_start_file entry, and yet still need to be
24419 associated to the base file.
24420
24421 To determine the base file name, we scan the macro definitions until we
24422 reach the first DW_MACINFO_start_file entry. We then initialize
24423 CURRENT_FILE accordingly so that any macro definition found before the
24424 first DW_MACINFO_start_file can still be associated to the base file. */
24425
24426 mac_ptr = section->buffer + offset;
24427 mac_end = section->buffer + section->size;
24428
24429 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
24430 &offset_size, section_is_gnu);
24431 if (mac_ptr == NULL)
24432 {
24433 /* We already issued a complaint. */
24434 return;
24435 }
24436
24437 do
24438 {
24439 /* Do we at least have room for a macinfo type byte? */
24440 if (mac_ptr >= mac_end)
24441 {
24442 /* Complaint is printed during the second pass as GDB will probably
24443 stop the first pass earlier upon finding
24444 DW_MACINFO_start_file. */
24445 break;
24446 }
24447
aead7601 24448 macinfo_type = (enum dwarf_macro_record_type) read_1_byte (abfd, mac_ptr);
cf2c3c16
TT
24449 mac_ptr++;
24450
24451 /* Note that we rely on the fact that the corresponding GNU and
24452 DWARF constants are the same. */
132448f8
SM
24453 DIAGNOSTIC_PUSH
24454 DIAGNOSTIC_IGNORE_SWITCH_DIFFERENT_ENUM_TYPES
cf2c3c16
TT
24455 switch (macinfo_type)
24456 {
24457 /* A zero macinfo type indicates the end of the macro
24458 information. */
24459 case 0:
24460 break;
24461
0af92d60
JK
24462 case DW_MACRO_define:
24463 case DW_MACRO_undef:
cf2c3c16
TT
24464 /* Only skip the data by MAC_PTR. */
24465 {
24466 unsigned int bytes_read;
24467
24468 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24469 mac_ptr += bytes_read;
24470 read_direct_string (abfd, mac_ptr, &bytes_read);
24471 mac_ptr += bytes_read;
24472 }
24473 break;
24474
0af92d60 24475 case DW_MACRO_start_file:
cf2c3c16
TT
24476 {
24477 unsigned int bytes_read;
24478 int line, file;
24479
24480 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24481 mac_ptr += bytes_read;
24482 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24483 mac_ptr += bytes_read;
24484
43f3e411 24485 current_file = macro_start_file (file, line, current_file, lh);
cf2c3c16
TT
24486 }
24487 break;
24488
0af92d60 24489 case DW_MACRO_end_file:
cf2c3c16
TT
24490 /* No data to skip by MAC_PTR. */
24491 break;
24492
0af92d60
JK
24493 case DW_MACRO_define_strp:
24494 case DW_MACRO_undef_strp:
24495 case DW_MACRO_define_sup:
24496 case DW_MACRO_undef_sup:
cf2c3c16
TT
24497 {
24498 unsigned int bytes_read;
24499
24500 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24501 mac_ptr += bytes_read;
24502 mac_ptr += offset_size;
24503 }
24504 break;
24505
0af92d60
JK
24506 case DW_MACRO_import:
24507 case DW_MACRO_import_sup:
cf2c3c16 24508 /* Note that, according to the spec, a transparent include
0af92d60 24509 chain cannot call DW_MACRO_start_file. So, we can just
cf2c3c16
TT
24510 skip this opcode. */
24511 mac_ptr += offset_size;
24512 break;
24513
24514 case DW_MACINFO_vendor_ext:
24515 /* Only skip the data by MAC_PTR. */
24516 if (!section_is_gnu)
24517 {
24518 unsigned int bytes_read;
24519
24520 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24521 mac_ptr += bytes_read;
24522 read_direct_string (abfd, mac_ptr, &bytes_read);
24523 mac_ptr += bytes_read;
24524 }
24525 /* FALLTHROUGH */
24526
24527 default:
24528 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
f664829e 24529 mac_ptr, mac_end, abfd, offset_size,
cf2c3c16
TT
24530 section);
24531 if (mac_ptr == NULL)
24532 return;
24533 break;
24534 }
132448f8 24535 DIAGNOSTIC_POP
cf2c3c16
TT
24536 } while (macinfo_type != 0 && current_file == NULL);
24537
24538 /* Second pass: Process all entries.
24539
24540 Use the AT_COMMAND_LINE flag to determine whether we are still processing
24541 command-line macro definitions/undefinitions. This flag is unset when we
24542 reach the first DW_MACINFO_start_file entry. */
24543
fc4007c9
TT
24544 htab_up include_hash (htab_create_alloc (1, htab_hash_pointer,
24545 htab_eq_pointer,
24546 NULL, xcalloc, xfree));
8fc3fc34 24547 mac_ptr = section->buffer + offset;
fc4007c9 24548 slot = htab_find_slot (include_hash.get (), mac_ptr, INSERT);
d521ce57 24549 *slot = (void *) mac_ptr;
ed2dc618
SM
24550 dwarf_decode_macro_bytes (dwarf2_per_objfile,
24551 abfd, mac_ptr, mac_end,
43f3e411 24552 current_file, lh, section,
fc4007c9
TT
24553 section_is_gnu, 0, offset_size,
24554 include_hash.get ());
cf2c3c16
TT
24555}
24556
8e19ed76 24557/* Check if the attribute's form is a DW_FORM_block*
0963b4bd 24558 if so return true else false. */
380bca97 24559
8e19ed76 24560static int
6e5a29e1 24561attr_form_is_block (const struct attribute *attr)
8e19ed76
PS
24562{
24563 return (attr == NULL ? 0 :
24564 attr->form == DW_FORM_block1
24565 || attr->form == DW_FORM_block2
24566 || attr->form == DW_FORM_block4
2dc7f7b3
TT
24567 || attr->form == DW_FORM_block
24568 || attr->form == DW_FORM_exprloc);
8e19ed76 24569}
4c2df51b 24570
c6a0999f
JB
24571/* Return non-zero if ATTR's value is a section offset --- classes
24572 lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
24573 You may use DW_UNSND (attr) to retrieve such offsets.
24574
24575 Section 7.5.4, "Attribute Encodings", explains that no attribute
24576 may have a value that belongs to more than one of these classes; it
24577 would be ambiguous if we did, because we use the same forms for all
24578 of them. */
380bca97 24579
3690dd37 24580static int
6e5a29e1 24581attr_form_is_section_offset (const struct attribute *attr)
3690dd37
JB
24582{
24583 return (attr->form == DW_FORM_data4
2dc7f7b3
TT
24584 || attr->form == DW_FORM_data8
24585 || attr->form == DW_FORM_sec_offset);
3690dd37
JB
24586}
24587
3690dd37
JB
24588/* Return non-zero if ATTR's value falls in the 'constant' class, or
24589 zero otherwise. When this function returns true, you can apply
24590 dwarf2_get_attr_constant_value to it.
24591
24592 However, note that for some attributes you must check
24593 attr_form_is_section_offset before using this test. DW_FORM_data4
24594 and DW_FORM_data8 are members of both the constant class, and of
24595 the classes that contain offsets into other debug sections
24596 (lineptr, loclistptr, macptr or rangelistptr). The DWARF spec says
24597 that, if an attribute's can be either a constant or one of the
24598 section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
0224619f
JK
24599 taken as section offsets, not constants.
24600
24601 DW_FORM_data16 is not considered as dwarf2_get_attr_constant_value
24602 cannot handle that. */
380bca97 24603
3690dd37 24604static int
6e5a29e1 24605attr_form_is_constant (const struct attribute *attr)
3690dd37
JB
24606{
24607 switch (attr->form)
24608 {
24609 case DW_FORM_sdata:
24610 case DW_FORM_udata:
24611 case DW_FORM_data1:
24612 case DW_FORM_data2:
24613 case DW_FORM_data4:
24614 case DW_FORM_data8:
663c44ac 24615 case DW_FORM_implicit_const:
3690dd37
JB
24616 return 1;
24617 default:
24618 return 0;
24619 }
24620}
24621
7771576e
SA
24622
24623/* DW_ADDR is always stored already as sect_offset; despite for the forms
24624 besides DW_FORM_ref_addr it is stored as cu_offset in the DWARF file. */
24625
24626static int
6e5a29e1 24627attr_form_is_ref (const struct attribute *attr)
7771576e
SA
24628{
24629 switch (attr->form)
24630 {
24631 case DW_FORM_ref_addr:
24632 case DW_FORM_ref1:
24633 case DW_FORM_ref2:
24634 case DW_FORM_ref4:
24635 case DW_FORM_ref8:
24636 case DW_FORM_ref_udata:
24637 case DW_FORM_GNU_ref_alt:
24638 return 1;
24639 default:
24640 return 0;
24641 }
24642}
24643
3019eac3
DE
24644/* Return the .debug_loc section to use for CU.
24645 For DWO files use .debug_loc.dwo. */
24646
24647static struct dwarf2_section_info *
24648cu_debug_loc_section (struct dwarf2_cu *cu)
24649{
518817b3
SM
24650 struct dwarf2_per_objfile *dwarf2_per_objfile
24651 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 24652
3019eac3 24653 if (cu->dwo_unit)
43988095
JK
24654 {
24655 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
24656
24657 return cu->header.version >= 5 ? &sections->loclists : &sections->loc;
24658 }
24659 return (cu->header.version >= 5 ? &dwarf2_per_objfile->loclists
24660 : &dwarf2_per_objfile->loc);
3019eac3
DE
24661}
24662
8cf6f0b1
TT
24663/* A helper function that fills in a dwarf2_loclist_baton. */
24664
24665static void
24666fill_in_loclist_baton (struct dwarf2_cu *cu,
24667 struct dwarf2_loclist_baton *baton,
ff39bb5e 24668 const struct attribute *attr)
8cf6f0b1 24669{
518817b3
SM
24670 struct dwarf2_per_objfile *dwarf2_per_objfile
24671 = cu->per_cu->dwarf2_per_objfile;
3019eac3
DE
24672 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
24673
24674 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
8cf6f0b1
TT
24675
24676 baton->per_cu = cu->per_cu;
24677 gdb_assert (baton->per_cu);
24678 /* We don't know how long the location list is, but make sure we
24679 don't run off the edge of the section. */
3019eac3
DE
24680 baton->size = section->size - DW_UNSND (attr);
24681 baton->data = section->buffer + DW_UNSND (attr);
8cf6f0b1 24682 baton->base_address = cu->base_address;
f664829e 24683 baton->from_dwo = cu->dwo_unit != NULL;
8cf6f0b1
TT
24684}
24685
4c2df51b 24686static void
ff39bb5e 24687dwarf2_symbol_mark_computed (const struct attribute *attr, struct symbol *sym,
f1e6e072 24688 struct dwarf2_cu *cu, int is_block)
4c2df51b 24689{
518817b3
SM
24690 struct dwarf2_per_objfile *dwarf2_per_objfile
24691 = cu->per_cu->dwarf2_per_objfile;
bb5ed363 24692 struct objfile *objfile = dwarf2_per_objfile->objfile;
3019eac3 24693 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
bb5ed363 24694
3690dd37 24695 if (attr_form_is_section_offset (attr)
3019eac3 24696 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
99bcc461
DJ
24697 the section. If so, fall through to the complaint in the
24698 other branch. */
3019eac3 24699 && DW_UNSND (attr) < dwarf2_section_size (objfile, section))
4c2df51b 24700 {
0d53c4c4 24701 struct dwarf2_loclist_baton *baton;
4c2df51b 24702
8d749320 24703 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_loclist_baton);
4c2df51b 24704
8cf6f0b1 24705 fill_in_loclist_baton (cu, baton, attr);
be391dca 24706
d00adf39 24707 if (cu->base_known == 0)
0d53c4c4 24708 complaint (&symfile_complaints,
3e43a32a
MS
24709 _("Location list used without "
24710 "specifying the CU base address."));
4c2df51b 24711
f1e6e072
TT
24712 SYMBOL_ACLASS_INDEX (sym) = (is_block
24713 ? dwarf2_loclist_block_index
24714 : dwarf2_loclist_index);
0d53c4c4
DJ
24715 SYMBOL_LOCATION_BATON (sym) = baton;
24716 }
24717 else
24718 {
24719 struct dwarf2_locexpr_baton *baton;
24720
8d749320 24721 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
ae0d2f24
UW
24722 baton->per_cu = cu->per_cu;
24723 gdb_assert (baton->per_cu);
0d53c4c4
DJ
24724
24725 if (attr_form_is_block (attr))
24726 {
24727 /* Note that we're just copying the block's data pointer
24728 here, not the actual data. We're still pointing into the
6502dd73
DJ
24729 info_buffer for SYM's objfile; right now we never release
24730 that buffer, but when we do clean up properly this may
24731 need to change. */
0d53c4c4
DJ
24732 baton->size = DW_BLOCK (attr)->size;
24733 baton->data = DW_BLOCK (attr)->data;
24734 }
24735 else
24736 {
24737 dwarf2_invalid_attrib_class_complaint ("location description",
24738 SYMBOL_NATURAL_NAME (sym));
24739 baton->size = 0;
0d53c4c4 24740 }
6e70227d 24741
f1e6e072
TT
24742 SYMBOL_ACLASS_INDEX (sym) = (is_block
24743 ? dwarf2_locexpr_block_index
24744 : dwarf2_locexpr_index);
0d53c4c4
DJ
24745 SYMBOL_LOCATION_BATON (sym) = baton;
24746 }
4c2df51b 24747}
6502dd73 24748
9aa1f1e3
TT
24749/* Return the OBJFILE associated with the compilation unit CU. If CU
24750 came from a separate debuginfo file, then the master objfile is
24751 returned. */
ae0d2f24
UW
24752
24753struct objfile *
24754dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
24755{
e3b94546 24756 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
ae0d2f24
UW
24757
24758 /* Return the master objfile, so that we can report and look up the
24759 correct file containing this variable. */
24760 if (objfile->separate_debug_objfile_backlink)
24761 objfile = objfile->separate_debug_objfile_backlink;
24762
24763 return objfile;
24764}
24765
96408a79
SA
24766/* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
24767 (CU_HEADERP is unused in such case) or prepare a temporary copy at
24768 CU_HEADERP first. */
24769
24770static const struct comp_unit_head *
24771per_cu_header_read_in (struct comp_unit_head *cu_headerp,
24772 struct dwarf2_per_cu_data *per_cu)
24773{
d521ce57 24774 const gdb_byte *info_ptr;
96408a79
SA
24775
24776 if (per_cu->cu)
24777 return &per_cu->cu->header;
24778
9c541725 24779 info_ptr = per_cu->section->buffer + to_underlying (per_cu->sect_off);
96408a79
SA
24780
24781 memset (cu_headerp, 0, sizeof (*cu_headerp));
43988095
JK
24782 read_comp_unit_head (cu_headerp, info_ptr, per_cu->section,
24783 rcuh_kind::COMPILE);
96408a79
SA
24784
24785 return cu_headerp;
24786}
24787
ae0d2f24
UW
24788/* Return the address size given in the compilation unit header for CU. */
24789
98714339 24790int
ae0d2f24
UW
24791dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
24792{
96408a79
SA
24793 struct comp_unit_head cu_header_local;
24794 const struct comp_unit_head *cu_headerp;
c471e790 24795
96408a79
SA
24796 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
24797
24798 return cu_headerp->addr_size;
ae0d2f24
UW
24799}
24800
9eae7c52
TT
24801/* Return the offset size given in the compilation unit header for CU. */
24802
24803int
24804dwarf2_per_cu_offset_size (struct dwarf2_per_cu_data *per_cu)
24805{
96408a79
SA
24806 struct comp_unit_head cu_header_local;
24807 const struct comp_unit_head *cu_headerp;
9c6c53f7 24808
96408a79
SA
24809 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
24810
24811 return cu_headerp->offset_size;
24812}
24813
24814/* See its dwarf2loc.h declaration. */
24815
24816int
24817dwarf2_per_cu_ref_addr_size (struct dwarf2_per_cu_data *per_cu)
24818{
24819 struct comp_unit_head cu_header_local;
24820 const struct comp_unit_head *cu_headerp;
24821
24822 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
24823
24824 if (cu_headerp->version == 2)
24825 return cu_headerp->addr_size;
24826 else
24827 return cu_headerp->offset_size;
181cebd4
JK
24828}
24829
9aa1f1e3
TT
24830/* Return the text offset of the CU. The returned offset comes from
24831 this CU's objfile. If this objfile came from a separate debuginfo
24832 file, then the offset may be different from the corresponding
24833 offset in the parent objfile. */
24834
24835CORE_ADDR
24836dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
24837{
e3b94546 24838 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
9aa1f1e3
TT
24839
24840 return ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
24841}
24842
43988095
JK
24843/* Return DWARF version number of PER_CU. */
24844
24845short
24846dwarf2_version (struct dwarf2_per_cu_data *per_cu)
24847{
24848 return per_cu->dwarf_version;
24849}
24850
348e048f
DE
24851/* Locate the .debug_info compilation unit from CU's objfile which contains
24852 the DIE at OFFSET. Raises an error on failure. */
ae038cb0
DJ
24853
24854static struct dwarf2_per_cu_data *
9c541725 24855dwarf2_find_containing_comp_unit (sect_offset sect_off,
36586728 24856 unsigned int offset_in_dwz,
ed2dc618 24857 struct dwarf2_per_objfile *dwarf2_per_objfile)
ae038cb0
DJ
24858{
24859 struct dwarf2_per_cu_data *this_cu;
24860 int low, high;
36586728 24861 const sect_offset *cu_off;
ae038cb0 24862
ae038cb0 24863 low = 0;
b76e467d 24864 high = dwarf2_per_objfile->all_comp_units.size () - 1;
ae038cb0
DJ
24865 while (high > low)
24866 {
36586728 24867 struct dwarf2_per_cu_data *mid_cu;
ae038cb0 24868 int mid = low + (high - low) / 2;
9a619af0 24869
36586728 24870 mid_cu = dwarf2_per_objfile->all_comp_units[mid];
9c541725 24871 cu_off = &mid_cu->sect_off;
36586728 24872 if (mid_cu->is_dwz > offset_in_dwz
9c541725 24873 || (mid_cu->is_dwz == offset_in_dwz && *cu_off >= sect_off))
ae038cb0
DJ
24874 high = mid;
24875 else
24876 low = mid + 1;
24877 }
24878 gdb_assert (low == high);
36586728 24879 this_cu = dwarf2_per_objfile->all_comp_units[low];
9c541725
PA
24880 cu_off = &this_cu->sect_off;
24881 if (this_cu->is_dwz != offset_in_dwz || *cu_off > sect_off)
ae038cb0 24882 {
36586728 24883 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
8a3fe4f8 24884 error (_("Dwarf Error: could not find partial DIE containing "
9d8780f0
SM
24885 "offset %s [in module %s]"),
24886 sect_offset_str (sect_off),
ed2dc618 24887 bfd_get_filename (dwarf2_per_objfile->objfile->obfd));
10b3939b 24888
9c541725
PA
24889 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->sect_off
24890 <= sect_off);
ae038cb0
DJ
24891 return dwarf2_per_objfile->all_comp_units[low-1];
24892 }
24893 else
24894 {
24895 this_cu = dwarf2_per_objfile->all_comp_units[low];
b76e467d 24896 if (low == dwarf2_per_objfile->all_comp_units.size () - 1
9c541725 24897 && sect_off >= this_cu->sect_off + this_cu->length)
9d8780f0 24898 error (_("invalid dwarf2 offset %s"), sect_offset_str (sect_off));
9c541725 24899 gdb_assert (sect_off < this_cu->sect_off + this_cu->length);
ae038cb0
DJ
24900 return this_cu;
24901 }
24902}
24903
23745b47 24904/* Initialize dwarf2_cu CU, owned by PER_CU. */
93311388 24905
fcd3b13d
SM
24906dwarf2_cu::dwarf2_cu (struct dwarf2_per_cu_data *per_cu_)
24907 : per_cu (per_cu_),
24908 mark (0),
24909 has_loclist (0),
24910 checked_producer (0),
24911 producer_is_gxx_lt_4_6 (0),
24912 producer_is_gcc_lt_4_3 (0),
24913 producer_is_icc_lt_14 (0),
24914 processing_has_namespace_info (0)
93311388 24915{
fcd3b13d
SM
24916 per_cu->cu = this;
24917}
24918
24919/* Destroy a dwarf2_cu. */
24920
24921dwarf2_cu::~dwarf2_cu ()
24922{
24923 per_cu->cu = NULL;
9816fde3
JK
24924}
24925
24926/* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
24927
24928static void
95554aad
TT
24929prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
24930 enum language pretend_language)
9816fde3
JK
24931{
24932 struct attribute *attr;
24933
24934 /* Set the language we're debugging. */
24935 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
24936 if (attr)
24937 set_cu_language (DW_UNSND (attr), cu);
24938 else
9cded63f 24939 {
95554aad 24940 cu->language = pretend_language;
9cded63f
TT
24941 cu->language_defn = language_def (cu->language);
24942 }
dee91e82 24943
7d45c7c3 24944 cu->producer = dwarf2_string_attr (comp_unit_die, DW_AT_producer, cu);
93311388
DE
24945}
24946
ae038cb0
DJ
24947/* Increase the age counter on each cached compilation unit, and free
24948 any that are too old. */
24949
24950static void
ed2dc618 24951age_cached_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
ae038cb0
DJ
24952{
24953 struct dwarf2_per_cu_data *per_cu, **last_chain;
24954
24955 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
24956 per_cu = dwarf2_per_objfile->read_in_chain;
24957 while (per_cu != NULL)
24958 {
24959 per_cu->cu->last_used ++;
b4f54984 24960 if (per_cu->cu->last_used <= dwarf_max_cache_age)
ae038cb0
DJ
24961 dwarf2_mark (per_cu->cu);
24962 per_cu = per_cu->cu->read_in_chain;
24963 }
24964
24965 per_cu = dwarf2_per_objfile->read_in_chain;
24966 last_chain = &dwarf2_per_objfile->read_in_chain;
24967 while (per_cu != NULL)
24968 {
24969 struct dwarf2_per_cu_data *next_cu;
24970
24971 next_cu = per_cu->cu->read_in_chain;
24972
24973 if (!per_cu->cu->mark)
24974 {
fcd3b13d 24975 delete per_cu->cu;
ae038cb0
DJ
24976 *last_chain = next_cu;
24977 }
24978 else
24979 last_chain = &per_cu->cu->read_in_chain;
24980
24981 per_cu = next_cu;
24982 }
24983}
24984
24985/* Remove a single compilation unit from the cache. */
24986
24987static void
dee91e82 24988free_one_cached_comp_unit (struct dwarf2_per_cu_data *target_per_cu)
ae038cb0
DJ
24989{
24990 struct dwarf2_per_cu_data *per_cu, **last_chain;
ed2dc618
SM
24991 struct dwarf2_per_objfile *dwarf2_per_objfile
24992 = target_per_cu->dwarf2_per_objfile;
ae038cb0
DJ
24993
24994 per_cu = dwarf2_per_objfile->read_in_chain;
24995 last_chain = &dwarf2_per_objfile->read_in_chain;
24996 while (per_cu != NULL)
24997 {
24998 struct dwarf2_per_cu_data *next_cu;
24999
25000 next_cu = per_cu->cu->read_in_chain;
25001
dee91e82 25002 if (per_cu == target_per_cu)
ae038cb0 25003 {
fcd3b13d 25004 delete per_cu->cu;
dee91e82 25005 per_cu->cu = NULL;
ae038cb0
DJ
25006 *last_chain = next_cu;
25007 break;
25008 }
25009 else
25010 last_chain = &per_cu->cu->read_in_chain;
25011
25012 per_cu = next_cu;
25013 }
25014}
25015
fe3e1990
DJ
25016/* Release all extra memory associated with OBJFILE. */
25017
25018void
25019dwarf2_free_objfile (struct objfile *objfile)
25020{
ed2dc618
SM
25021 struct dwarf2_per_objfile *dwarf2_per_objfile
25022 = get_dwarf2_per_objfile (objfile);
fe3e1990 25023
fd90ace4 25024 delete dwarf2_per_objfile;
fe3e1990
DJ
25025}
25026
dee91e82
DE
25027/* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
25028 We store these in a hash table separate from the DIEs, and preserve them
25029 when the DIEs are flushed out of cache.
25030
25031 The CU "per_cu" pointer is needed because offset alone is not enough to
3019eac3 25032 uniquely identify the type. A file may have multiple .debug_types sections,
c88ee1f0
DE
25033 or the type may come from a DWO file. Furthermore, while it's more logical
25034 to use per_cu->section+offset, with Fission the section with the data is in
25035 the DWO file but we don't know that section at the point we need it.
25036 We have to use something in dwarf2_per_cu_data (or the pointer to it)
25037 because we can enter the lookup routine, get_die_type_at_offset, from
25038 outside this file, and thus won't necessarily have PER_CU->cu.
25039 Fortunately, PER_CU is stable for the life of the objfile. */
1c379e20 25040
dee91e82 25041struct dwarf2_per_cu_offset_and_type
1c379e20 25042{
dee91e82 25043 const struct dwarf2_per_cu_data *per_cu;
9c541725 25044 sect_offset sect_off;
1c379e20
DJ
25045 struct type *type;
25046};
25047
dee91e82 25048/* Hash function for a dwarf2_per_cu_offset_and_type. */
1c379e20
DJ
25049
25050static hashval_t
dee91e82 25051per_cu_offset_and_type_hash (const void *item)
1c379e20 25052{
9a3c8263
SM
25053 const struct dwarf2_per_cu_offset_and_type *ofs
25054 = (const struct dwarf2_per_cu_offset_and_type *) item;
9a619af0 25055
9c541725 25056 return (uintptr_t) ofs->per_cu + to_underlying (ofs->sect_off);
1c379e20
DJ
25057}
25058
dee91e82 25059/* Equality function for a dwarf2_per_cu_offset_and_type. */
1c379e20
DJ
25060
25061static int
dee91e82 25062per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
1c379e20 25063{
9a3c8263
SM
25064 const struct dwarf2_per_cu_offset_and_type *ofs_lhs
25065 = (const struct dwarf2_per_cu_offset_and_type *) item_lhs;
25066 const struct dwarf2_per_cu_offset_and_type *ofs_rhs
25067 = (const struct dwarf2_per_cu_offset_and_type *) item_rhs;
9a619af0 25068
dee91e82 25069 return (ofs_lhs->per_cu == ofs_rhs->per_cu
9c541725 25070 && ofs_lhs->sect_off == ofs_rhs->sect_off);
1c379e20
DJ
25071}
25072
25073/* Set the type associated with DIE to TYPE. Save it in CU's hash
7e314c57
JK
25074 table if necessary. For convenience, return TYPE.
25075
25076 The DIEs reading must have careful ordering to:
25077 * Not cause infite loops trying to read in DIEs as a prerequisite for
25078 reading current DIE.
25079 * Not trying to dereference contents of still incompletely read in types
25080 while reading in other DIEs.
25081 * Enable referencing still incompletely read in types just by a pointer to
25082 the type without accessing its fields.
25083
25084 Therefore caller should follow these rules:
25085 * Try to fetch any prerequisite types we may need to build this DIE type
25086 before building the type and calling set_die_type.
e71ec853 25087 * After building type call set_die_type for current DIE as soon as
7e314c57
JK
25088 possible before fetching more types to complete the current type.
25089 * Make the type as complete as possible before fetching more types. */
1c379e20 25090
f792889a 25091static struct type *
1c379e20
DJ
25092set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
25093{
518817b3
SM
25094 struct dwarf2_per_objfile *dwarf2_per_objfile
25095 = cu->per_cu->dwarf2_per_objfile;
dee91e82 25096 struct dwarf2_per_cu_offset_and_type **slot, ofs;
ed2dc618 25097 struct objfile *objfile = dwarf2_per_objfile->objfile;
3cdcd0ce
JB
25098 struct attribute *attr;
25099 struct dynamic_prop prop;
1c379e20 25100
b4ba55a1
JB
25101 /* For Ada types, make sure that the gnat-specific data is always
25102 initialized (if not already set). There are a few types where
25103 we should not be doing so, because the type-specific area is
25104 already used to hold some other piece of info (eg: TYPE_CODE_FLT
25105 where the type-specific area is used to store the floatformat).
25106 But this is not a problem, because the gnat-specific information
25107 is actually not needed for these types. */
25108 if (need_gnat_info (cu)
25109 && TYPE_CODE (type) != TYPE_CODE_FUNC
25110 && TYPE_CODE (type) != TYPE_CODE_FLT
09e2d7c7
DE
25111 && TYPE_CODE (type) != TYPE_CODE_METHODPTR
25112 && TYPE_CODE (type) != TYPE_CODE_MEMBERPTR
25113 && TYPE_CODE (type) != TYPE_CODE_METHOD
b4ba55a1
JB
25114 && !HAVE_GNAT_AUX_INFO (type))
25115 INIT_GNAT_SPECIFIC (type);
25116
3f2f83dd
KB
25117 /* Read DW_AT_allocated and set in type. */
25118 attr = dwarf2_attr (die, DW_AT_allocated, cu);
25119 if (attr_form_is_block (attr))
25120 {
25121 if (attr_to_dynamic_prop (attr, die, cu, &prop))
50a82047 25122 add_dyn_prop (DYN_PROP_ALLOCATED, prop, type);
3f2f83dd
KB
25123 }
25124 else if (attr != NULL)
25125 {
25126 complaint (&symfile_complaints,
9d8780f0 25127 _("DW_AT_allocated has the wrong form (%s) at DIE %s"),
9c541725 25128 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
9d8780f0 25129 sect_offset_str (die->sect_off));
3f2f83dd
KB
25130 }
25131
25132 /* Read DW_AT_associated and set in type. */
25133 attr = dwarf2_attr (die, DW_AT_associated, cu);
25134 if (attr_form_is_block (attr))
25135 {
25136 if (attr_to_dynamic_prop (attr, die, cu, &prop))
50a82047 25137 add_dyn_prop (DYN_PROP_ASSOCIATED, prop, type);
3f2f83dd
KB
25138 }
25139 else if (attr != NULL)
25140 {
25141 complaint (&symfile_complaints,
9d8780f0 25142 _("DW_AT_associated has the wrong form (%s) at DIE %s"),
9c541725 25143 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
9d8780f0 25144 sect_offset_str (die->sect_off));
3f2f83dd
KB
25145 }
25146
3cdcd0ce
JB
25147 /* Read DW_AT_data_location and set in type. */
25148 attr = dwarf2_attr (die, DW_AT_data_location, cu);
25149 if (attr_to_dynamic_prop (attr, die, cu, &prop))
50a82047 25150 add_dyn_prop (DYN_PROP_DATA_LOCATION, prop, type);
3cdcd0ce 25151
dee91e82 25152 if (dwarf2_per_objfile->die_type_hash == NULL)
f792889a 25153 {
dee91e82
DE
25154 dwarf2_per_objfile->die_type_hash =
25155 htab_create_alloc_ex (127,
25156 per_cu_offset_and_type_hash,
25157 per_cu_offset_and_type_eq,
25158 NULL,
25159 &objfile->objfile_obstack,
25160 hashtab_obstack_allocate,
25161 dummy_obstack_deallocate);
f792889a 25162 }
1c379e20 25163
dee91e82 25164 ofs.per_cu = cu->per_cu;
9c541725 25165 ofs.sect_off = die->sect_off;
1c379e20 25166 ofs.type = type;
dee91e82
DE
25167 slot = (struct dwarf2_per_cu_offset_and_type **)
25168 htab_find_slot (dwarf2_per_objfile->die_type_hash, &ofs, INSERT);
7e314c57
JK
25169 if (*slot)
25170 complaint (&symfile_complaints,
9d8780f0
SM
25171 _("A problem internal to GDB: DIE %s has type already set"),
25172 sect_offset_str (die->sect_off));
8d749320
SM
25173 *slot = XOBNEW (&objfile->objfile_obstack,
25174 struct dwarf2_per_cu_offset_and_type);
1c379e20 25175 **slot = ofs;
f792889a 25176 return type;
1c379e20
DJ
25177}
25178
9c541725 25179/* Look up the type for the die at SECT_OFF in PER_CU in die_type_hash,
02142a6c 25180 or return NULL if the die does not have a saved type. */
1c379e20
DJ
25181
25182static struct type *
9c541725 25183get_die_type_at_offset (sect_offset sect_off,
673bfd45 25184 struct dwarf2_per_cu_data *per_cu)
1c379e20 25185{
dee91e82 25186 struct dwarf2_per_cu_offset_and_type *slot, ofs;
ed2dc618 25187 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
f792889a 25188
dee91e82 25189 if (dwarf2_per_objfile->die_type_hash == NULL)
f792889a 25190 return NULL;
1c379e20 25191
dee91e82 25192 ofs.per_cu = per_cu;
9c541725 25193 ofs.sect_off = sect_off;
9a3c8263
SM
25194 slot = ((struct dwarf2_per_cu_offset_and_type *)
25195 htab_find (dwarf2_per_objfile->die_type_hash, &ofs));
1c379e20
DJ
25196 if (slot)
25197 return slot->type;
25198 else
25199 return NULL;
25200}
25201
02142a6c 25202/* Look up the type for DIE in CU in die_type_hash,
673bfd45
DE
25203 or return NULL if DIE does not have a saved type. */
25204
25205static struct type *
25206get_die_type (struct die_info *die, struct dwarf2_cu *cu)
25207{
9c541725 25208 return get_die_type_at_offset (die->sect_off, cu->per_cu);
673bfd45
DE
25209}
25210
10b3939b
DJ
25211/* Add a dependence relationship from CU to REF_PER_CU. */
25212
25213static void
25214dwarf2_add_dependence (struct dwarf2_cu *cu,
25215 struct dwarf2_per_cu_data *ref_per_cu)
25216{
25217 void **slot;
25218
25219 if (cu->dependencies == NULL)
25220 cu->dependencies
25221 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
25222 NULL, &cu->comp_unit_obstack,
25223 hashtab_obstack_allocate,
25224 dummy_obstack_deallocate);
25225
25226 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
25227 if (*slot == NULL)
25228 *slot = ref_per_cu;
25229}
1c379e20 25230
f504f079
DE
25231/* Subroutine of dwarf2_mark to pass to htab_traverse.
25232 Set the mark field in every compilation unit in the
ae038cb0
DJ
25233 cache that we must keep because we are keeping CU. */
25234
10b3939b
DJ
25235static int
25236dwarf2_mark_helper (void **slot, void *data)
25237{
25238 struct dwarf2_per_cu_data *per_cu;
25239
25240 per_cu = (struct dwarf2_per_cu_data *) *slot;
d07ed419
JK
25241
25242 /* cu->dependencies references may not yet have been ever read if QUIT aborts
25243 reading of the chain. As such dependencies remain valid it is not much
25244 useful to track and undo them during QUIT cleanups. */
25245 if (per_cu->cu == NULL)
25246 return 1;
25247
10b3939b
DJ
25248 if (per_cu->cu->mark)
25249 return 1;
25250 per_cu->cu->mark = 1;
25251
25252 if (per_cu->cu->dependencies != NULL)
25253 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
25254
25255 return 1;
25256}
25257
f504f079
DE
25258/* Set the mark field in CU and in every other compilation unit in the
25259 cache that we must keep because we are keeping CU. */
25260
ae038cb0
DJ
25261static void
25262dwarf2_mark (struct dwarf2_cu *cu)
25263{
25264 if (cu->mark)
25265 return;
25266 cu->mark = 1;
10b3939b
DJ
25267 if (cu->dependencies != NULL)
25268 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
ae038cb0
DJ
25269}
25270
25271static void
25272dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
25273{
25274 while (per_cu)
25275 {
25276 per_cu->cu->mark = 0;
25277 per_cu = per_cu->cu->read_in_chain;
25278 }
72bf9492
DJ
25279}
25280
72bf9492
DJ
25281/* Trivial hash function for partial_die_info: the hash value of a DIE
25282 is its offset in .debug_info for this objfile. */
25283
25284static hashval_t
25285partial_die_hash (const void *item)
25286{
9a3c8263
SM
25287 const struct partial_die_info *part_die
25288 = (const struct partial_die_info *) item;
9a619af0 25289
9c541725 25290 return to_underlying (part_die->sect_off);
72bf9492
DJ
25291}
25292
25293/* Trivial comparison function for partial_die_info structures: two DIEs
25294 are equal if they have the same offset. */
25295
25296static int
25297partial_die_eq (const void *item_lhs, const void *item_rhs)
25298{
9a3c8263
SM
25299 const struct partial_die_info *part_die_lhs
25300 = (const struct partial_die_info *) item_lhs;
25301 const struct partial_die_info *part_die_rhs
25302 = (const struct partial_die_info *) item_rhs;
9a619af0 25303
9c541725 25304 return part_die_lhs->sect_off == part_die_rhs->sect_off;
72bf9492
DJ
25305}
25306
b4f54984
DE
25307static struct cmd_list_element *set_dwarf_cmdlist;
25308static struct cmd_list_element *show_dwarf_cmdlist;
ae038cb0
DJ
25309
25310static void
981a3fb3 25311set_dwarf_cmd (const char *args, int from_tty)
ae038cb0 25312{
b4f54984 25313 help_list (set_dwarf_cmdlist, "maintenance set dwarf ", all_commands,
635c7e8a 25314 gdb_stdout);
ae038cb0
DJ
25315}
25316
25317static void
981a3fb3 25318show_dwarf_cmd (const char *args, int from_tty)
6e70227d 25319{
b4f54984 25320 cmd_show_list (show_dwarf_cmdlist, from_tty, "");
ae038cb0
DJ
25321}
25322
cd4fb1b2 25323int dwarf_always_disassemble;
437afbb8 25324
437afbb8 25325static void
cd4fb1b2
SM
25326show_dwarf_always_disassemble (struct ui_file *file, int from_tty,
25327 struct cmd_list_element *c, const char *value)
9291a0cd 25328{
cd4fb1b2
SM
25329 fprintf_filtered (file,
25330 _("Whether to always disassemble "
25331 "DWARF expressions is %s.\n"),
25332 value);
9291a0cd
TT
25333}
25334
9291a0cd 25335static void
cd4fb1b2
SM
25336show_check_physname (struct ui_file *file, int from_tty,
25337 struct cmd_list_element *c, const char *value)
9291a0cd 25338{
cd4fb1b2
SM
25339 fprintf_filtered (file,
25340 _("Whether to check \"physname\" is %s.\n"),
25341 value);
9291a0cd
TT
25342}
25343
cd4fb1b2
SM
25344void
25345_initialize_dwarf2_read (void)
9291a0cd 25346{
9291a0cd 25347
cd4fb1b2 25348 dwarf2_objfile_data_key = register_objfile_data ();
156942c7 25349
cd4fb1b2
SM
25350 add_prefix_cmd ("dwarf", class_maintenance, set_dwarf_cmd, _("\
25351Set DWARF specific variables.\n\
25352Configure DWARF variables such as the cache size"),
25353 &set_dwarf_cmdlist, "maintenance set dwarf ",
25354 0/*allow-unknown*/, &maintenance_set_cmdlist);
156942c7 25355
cd4fb1b2
SM
25356 add_prefix_cmd ("dwarf", class_maintenance, show_dwarf_cmd, _("\
25357Show DWARF specific variables\n\
25358Show DWARF variables such as the cache size"),
25359 &show_dwarf_cmdlist, "maintenance show dwarf ",
25360 0/*allow-unknown*/, &maintenance_show_cmdlist);
156942c7 25361
cd4fb1b2
SM
25362 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
25363 &dwarf_max_cache_age, _("\
25364Set the upper bound on the age of cached DWARF compilation units."), _("\
25365Show the upper bound on the age of cached DWARF compilation units."), _("\
25366A higher limit means that cached compilation units will be stored\n\
25367in memory longer, and more total memory will be used. Zero disables\n\
25368caching, which can slow down startup."),
25369 NULL,
25370 show_dwarf_max_cache_age,
25371 &set_dwarf_cmdlist,
25372 &show_dwarf_cmdlist);
156942c7 25373
cd4fb1b2
SM
25374 add_setshow_boolean_cmd ("always-disassemble", class_obscure,
25375 &dwarf_always_disassemble, _("\
25376Set whether `info address' always disassembles DWARF expressions."), _("\
25377Show whether `info address' always disassembles DWARF expressions."), _("\
25378When enabled, DWARF expressions are always printed in an assembly-like\n\
25379syntax. When disabled, expressions will be printed in a more\n\
25380conversational style, when possible."),
25381 NULL,
25382 show_dwarf_always_disassemble,
25383 &set_dwarf_cmdlist,
25384 &show_dwarf_cmdlist);
9291a0cd 25385
cd4fb1b2
SM
25386 add_setshow_zuinteger_cmd ("dwarf-read", no_class, &dwarf_read_debug, _("\
25387Set debugging of the DWARF reader."), _("\
25388Show debugging of the DWARF reader."), _("\
25389When enabled (non-zero), debugging messages are printed during DWARF\n\
25390reading and symtab expansion. A value of 1 (one) provides basic\n\
25391information. A value greater than 1 provides more verbose information."),
25392 NULL,
25393 NULL,
25394 &setdebuglist, &showdebuglist);
9291a0cd 25395
cd4fb1b2
SM
25396 add_setshow_zuinteger_cmd ("dwarf-die", no_class, &dwarf_die_debug, _("\
25397Set debugging of the DWARF DIE reader."), _("\
25398Show debugging of the DWARF DIE reader."), _("\
25399When enabled (non-zero), DIEs are dumped after they are read in.\n\
25400The value is the maximum depth to print."),
25401 NULL,
25402 NULL,
25403 &setdebuglist, &showdebuglist);
9291a0cd 25404
cd4fb1b2
SM
25405 add_setshow_zuinteger_cmd ("dwarf-line", no_class, &dwarf_line_debug, _("\
25406Set debugging of the dwarf line reader."), _("\
25407Show debugging of the dwarf line reader."), _("\
25408When enabled (non-zero), line number entries are dumped as they are read in.\n\
25409A value of 1 (one) provides basic information.\n\
25410A value greater than 1 provides more verbose information."),
25411 NULL,
25412 NULL,
25413 &setdebuglist, &showdebuglist);
437afbb8 25414
cd4fb1b2
SM
25415 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
25416Set cross-checking of \"physname\" code against demangler."), _("\
25417Show cross-checking of \"physname\" code against demangler."), _("\
25418When enabled, GDB's internal \"physname\" code is checked against\n\
25419the demangler."),
25420 NULL, show_check_physname,
25421 &setdebuglist, &showdebuglist);
900e11f9 25422
e615022a
DE
25423 add_setshow_boolean_cmd ("use-deprecated-index-sections",
25424 no_class, &use_deprecated_index_sections, _("\
25425Set whether to use deprecated gdb_index sections."), _("\
25426Show whether to use deprecated gdb_index sections."), _("\
25427When enabled, deprecated .gdb_index sections are used anyway.\n\
25428Normally they are ignored either because of a missing feature or\n\
25429performance issue.\n\
25430Warning: This option must be enabled before gdb reads the file."),
25431 NULL,
25432 NULL,
25433 &setlist, &showlist);
25434
f1e6e072
TT
25435 dwarf2_locexpr_index = register_symbol_computed_impl (LOC_COMPUTED,
25436 &dwarf2_locexpr_funcs);
25437 dwarf2_loclist_index = register_symbol_computed_impl (LOC_COMPUTED,
25438 &dwarf2_loclist_funcs);
25439
25440 dwarf2_locexpr_block_index = register_symbol_block_impl (LOC_BLOCK,
25441 &dwarf2_block_frame_base_locexpr_funcs);
25442 dwarf2_loclist_block_index = register_symbol_block_impl (LOC_BLOCK,
25443 &dwarf2_block_frame_base_loclist_funcs);
c62446b1
PA
25444
25445#if GDB_SELF_TEST
25446 selftests::register_test ("dw2_expand_symtabs_matching",
25447 selftests::dw2_expand_symtabs_matching::run_test);
25448#endif
6502dd73 25449}
This page took 4.585958 seconds and 4 git commands to generate.