Remove free_cached_comp_units cleanups
[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
3019eac3
DE
1962static void free_dwo_file_cleanup (void *);
1963
ed2dc618
SM
1964struct free_dwo_file_cleanup_data
1965{
1966 struct dwo_file *dwo_file;
1967 struct dwarf2_per_objfile *dwarf2_per_objfile;
1968};
1969
1970static void process_cu_includes (struct dwarf2_per_objfile *dwarf2_per_objfile);
95554aad 1971
1b80a9fa 1972static void check_producer (struct dwarf2_cu *cu);
527f3840
JK
1973
1974static void free_line_header_voidp (void *arg);
4390d890
DE
1975\f
1976/* Various complaints about symbol reading that don't abort the process. */
1977
1978static void
1979dwarf2_statement_list_fits_in_line_number_section_complaint (void)
1980{
1981 complaint (&symfile_complaints,
1982 _("statement list doesn't fit in .debug_line section"));
1983}
1984
1985static void
1986dwarf2_debug_line_missing_file_complaint (void)
1987{
1988 complaint (&symfile_complaints,
1989 _(".debug_line section has line data without a file"));
1990}
1991
1992static void
1993dwarf2_debug_line_missing_end_sequence_complaint (void)
1994{
1995 complaint (&symfile_complaints,
1996 _(".debug_line section has line "
1997 "program sequence without an end"));
1998}
1999
2000static void
2001dwarf2_complex_location_expr_complaint (void)
2002{
2003 complaint (&symfile_complaints, _("location expression too complex"));
2004}
2005
2006static void
2007dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
2008 int arg3)
2009{
2010 complaint (&symfile_complaints,
2011 _("const value length mismatch for '%s', got %d, expected %d"),
2012 arg1, arg2, arg3);
2013}
2014
2015static void
2016dwarf2_section_buffer_overflow_complaint (struct dwarf2_section_info *section)
2017{
2018 complaint (&symfile_complaints,
2019 _("debug info runs off end of %s section"
2020 " [in module %s]"),
a32a8923
DE
2021 get_section_name (section),
2022 get_section_file_name (section));
4390d890 2023}
1b80a9fa 2024
4390d890
DE
2025static void
2026dwarf2_macro_malformed_definition_complaint (const char *arg1)
2027{
2028 complaint (&symfile_complaints,
2029 _("macro debug info contains a "
2030 "malformed macro definition:\n`%s'"),
2031 arg1);
2032}
2033
2034static void
2035dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
2036{
2037 complaint (&symfile_complaints,
2038 _("invalid attribute class or form for '%s' in '%s'"),
2039 arg1, arg2);
2040}
527f3840
JK
2041
2042/* Hash function for line_header_hash. */
2043
2044static hashval_t
2045line_header_hash (const struct line_header *ofs)
2046{
9c541725 2047 return to_underlying (ofs->sect_off) ^ ofs->offset_in_dwz;
527f3840
JK
2048}
2049
2050/* Hash function for htab_create_alloc_ex for line_header_hash. */
2051
2052static hashval_t
2053line_header_hash_voidp (const void *item)
2054{
9a3c8263 2055 const struct line_header *ofs = (const struct line_header *) item;
527f3840
JK
2056
2057 return line_header_hash (ofs);
2058}
2059
2060/* Equality function for line_header_hash. */
2061
2062static int
2063line_header_eq_voidp (const void *item_lhs, const void *item_rhs)
2064{
9a3c8263
SM
2065 const struct line_header *ofs_lhs = (const struct line_header *) item_lhs;
2066 const struct line_header *ofs_rhs = (const struct line_header *) item_rhs;
527f3840 2067
9c541725 2068 return (ofs_lhs->sect_off == ofs_rhs->sect_off
527f3840
JK
2069 && ofs_lhs->offset_in_dwz == ofs_rhs->offset_in_dwz);
2070}
2071
4390d890 2072\f
9291a0cd 2073
31aa7e4e
JB
2074/* Read the given attribute value as an address, taking the attribute's
2075 form into account. */
2076
2077static CORE_ADDR
2078attr_value_as_address (struct attribute *attr)
2079{
2080 CORE_ADDR addr;
2081
2082 if (attr->form != DW_FORM_addr && attr->form != DW_FORM_GNU_addr_index)
2083 {
2084 /* Aside from a few clearly defined exceptions, attributes that
2085 contain an address must always be in DW_FORM_addr form.
2086 Unfortunately, some compilers happen to be violating this
2087 requirement by encoding addresses using other forms, such
2088 as DW_FORM_data4 for example. For those broken compilers,
2089 we try to do our best, without any guarantee of success,
2090 to interpret the address correctly. It would also be nice
2091 to generate a complaint, but that would require us to maintain
2092 a list of legitimate cases where a non-address form is allowed,
2093 as well as update callers to pass in at least the CU's DWARF
2094 version. This is more overhead than what we're willing to
2095 expand for a pretty rare case. */
2096 addr = DW_UNSND (attr);
2097 }
2098 else
2099 addr = DW_ADDR (attr);
2100
2101 return addr;
2102}
2103
330cdd98
PA
2104/* See declaration. */
2105
2106dwarf2_per_objfile::dwarf2_per_objfile (struct objfile *objfile_,
2107 const dwarf2_debug_sections *names)
2108 : objfile (objfile_)
2109{
2110 if (names == NULL)
2111 names = &dwarf2_elf_names;
2112
2113 bfd *obfd = objfile->obfd;
2114
2115 for (asection *sec = obfd->sections; sec != NULL; sec = sec->next)
2116 locate_sections (obfd, sec, *names);
2117}
2118
fc8e7e75
SM
2119static void free_dwo_files (htab_t dwo_files, struct objfile *objfile);
2120
330cdd98
PA
2121dwarf2_per_objfile::~dwarf2_per_objfile ()
2122{
2123 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
2124 free_cached_comp_units ();
2125
2126 if (quick_file_names_table)
2127 htab_delete (quick_file_names_table);
2128
2129 if (line_header_hash)
2130 htab_delete (line_header_hash);
2131
fc8e7e75
SM
2132 for (int ix = 0; ix < n_comp_units; ++ix)
2133 VEC_free (dwarf2_per_cu_ptr, all_comp_units[ix]->imported_symtabs);
2134
2135 for (int ix = 0; ix < n_type_units; ++ix)
2136 VEC_free (dwarf2_per_cu_ptr,
2137 all_type_units[ix]->per_cu.imported_symtabs);
2138 xfree (all_type_units);
2139
2140 VEC_free (dwarf2_section_info_def, types);
2141
2142 if (dwo_files != NULL)
2143 free_dwo_files (dwo_files, objfile);
2144 if (dwp_file != NULL)
2145 gdb_bfd_unref (dwp_file->dbfd);
2146
2147 if (dwz_file != NULL && dwz_file->dwz_bfd)
2148 gdb_bfd_unref (dwz_file->dwz_bfd);
2149
2150 if (index_table != NULL)
2151 index_table->~mapped_index ();
2152
330cdd98
PA
2153 /* Everything else should be on the objfile obstack. */
2154}
2155
2156/* See declaration. */
2157
2158void
2159dwarf2_per_objfile::free_cached_comp_units ()
2160{
2161 dwarf2_per_cu_data *per_cu = read_in_chain;
2162 dwarf2_per_cu_data **last_chain = &read_in_chain;
2163 while (per_cu != NULL)
2164 {
2165 dwarf2_per_cu_data *next_cu = per_cu->cu->read_in_chain;
2166
fcd3b13d 2167 delete per_cu->cu;
330cdd98
PA
2168 *last_chain = next_cu;
2169 per_cu = next_cu;
2170 }
2171}
2172
11ed8cad
TT
2173/* A helper class that calls free_cached_comp_units on
2174 destruction. */
2175
2176class free_cached_comp_units
2177{
2178public:
2179
2180 explicit free_cached_comp_units (dwarf2_per_objfile *per_objfile)
2181 : m_per_objfile (per_objfile)
2182 {
2183 }
2184
2185 ~free_cached_comp_units ()
2186 {
2187 m_per_objfile->free_cached_comp_units ();
2188 }
2189
2190 DISABLE_COPY_AND_ASSIGN (free_cached_comp_units);
2191
2192private:
2193
2194 dwarf2_per_objfile *m_per_objfile;
2195};
2196
c906108c 2197/* Try to locate the sections we need for DWARF 2 debugging
251d32d9
TG
2198 information and return true if we have enough to do something.
2199 NAMES points to the dwarf2 section names, or is NULL if the standard
2200 ELF names are used. */
c906108c
SS
2201
2202int
251d32d9
TG
2203dwarf2_has_info (struct objfile *objfile,
2204 const struct dwarf2_debug_sections *names)
c906108c 2205{
97cbe998
SDJ
2206 if (objfile->flags & OBJF_READNEVER)
2207 return 0;
2208
ed2dc618
SM
2209 struct dwarf2_per_objfile *dwarf2_per_objfile
2210 = get_dwarf2_per_objfile (objfile);
2211
2212 if (dwarf2_per_objfile == NULL)
be391dca
TT
2213 {
2214 /* Initialize per-objfile state. */
fd90ace4
YQ
2215 dwarf2_per_objfile
2216 = new (&objfile->objfile_obstack) struct dwarf2_per_objfile (objfile,
2217 names);
ed2dc618 2218 set_dwarf2_per_objfile (objfile, dwarf2_per_objfile);
be391dca 2219 }
73869dc2 2220 return (!dwarf2_per_objfile->info.is_virtual
049412e3 2221 && dwarf2_per_objfile->info.s.section != NULL
73869dc2 2222 && !dwarf2_per_objfile->abbrev.is_virtual
049412e3 2223 && dwarf2_per_objfile->abbrev.s.section != NULL);
73869dc2
DE
2224}
2225
2226/* Return the containing section of virtual section SECTION. */
2227
2228static struct dwarf2_section_info *
2229get_containing_section (const struct dwarf2_section_info *section)
2230{
2231 gdb_assert (section->is_virtual);
2232 return section->s.containing_section;
c906108c
SS
2233}
2234
a32a8923
DE
2235/* Return the bfd owner of SECTION. */
2236
2237static struct bfd *
2238get_section_bfd_owner (const struct dwarf2_section_info *section)
2239{
73869dc2
DE
2240 if (section->is_virtual)
2241 {
2242 section = get_containing_section (section);
2243 gdb_assert (!section->is_virtual);
2244 }
049412e3 2245 return section->s.section->owner;
a32a8923
DE
2246}
2247
2248/* Return the bfd section of SECTION.
2249 Returns NULL if the section is not present. */
2250
2251static asection *
2252get_section_bfd_section (const struct dwarf2_section_info *section)
2253{
73869dc2
DE
2254 if (section->is_virtual)
2255 {
2256 section = get_containing_section (section);
2257 gdb_assert (!section->is_virtual);
2258 }
049412e3 2259 return section->s.section;
a32a8923
DE
2260}
2261
2262/* Return the name of SECTION. */
2263
2264static const char *
2265get_section_name (const struct dwarf2_section_info *section)
2266{
2267 asection *sectp = get_section_bfd_section (section);
2268
2269 gdb_assert (sectp != NULL);
2270 return bfd_section_name (get_section_bfd_owner (section), sectp);
2271}
2272
2273/* Return the name of the file SECTION is in. */
2274
2275static const char *
2276get_section_file_name (const struct dwarf2_section_info *section)
2277{
2278 bfd *abfd = get_section_bfd_owner (section);
2279
2280 return bfd_get_filename (abfd);
2281}
2282
2283/* Return the id of SECTION.
2284 Returns 0 if SECTION doesn't exist. */
2285
2286static int
2287get_section_id (const struct dwarf2_section_info *section)
2288{
2289 asection *sectp = get_section_bfd_section (section);
2290
2291 if (sectp == NULL)
2292 return 0;
2293 return sectp->id;
2294}
2295
2296/* Return the flags of SECTION.
73869dc2 2297 SECTION (or containing section if this is a virtual section) must exist. */
a32a8923
DE
2298
2299static int
2300get_section_flags (const struct dwarf2_section_info *section)
2301{
2302 asection *sectp = get_section_bfd_section (section);
2303
2304 gdb_assert (sectp != NULL);
2305 return bfd_get_section_flags (sectp->owner, sectp);
2306}
2307
251d32d9
TG
2308/* When loading sections, we look either for uncompressed section or for
2309 compressed section names. */
233a11ab
CS
2310
2311static int
251d32d9
TG
2312section_is_p (const char *section_name,
2313 const struct dwarf2_section_names *names)
233a11ab 2314{
251d32d9
TG
2315 if (names->normal != NULL
2316 && strcmp (section_name, names->normal) == 0)
2317 return 1;
2318 if (names->compressed != NULL
2319 && strcmp (section_name, names->compressed) == 0)
2320 return 1;
2321 return 0;
233a11ab
CS
2322}
2323
330cdd98 2324/* See declaration. */
c906108c 2325
330cdd98
PA
2326void
2327dwarf2_per_objfile::locate_sections (bfd *abfd, asection *sectp,
2328 const dwarf2_debug_sections &names)
c906108c 2329{
dc7650b8 2330 flagword aflag = bfd_get_section_flags (abfd, sectp);
251d32d9 2331
dc7650b8
JK
2332 if ((aflag & SEC_HAS_CONTENTS) == 0)
2333 {
2334 }
330cdd98 2335 else if (section_is_p (sectp->name, &names.info))
c906108c 2336 {
330cdd98
PA
2337 this->info.s.section = sectp;
2338 this->info.size = bfd_get_section_size (sectp);
c906108c 2339 }
330cdd98 2340 else if (section_is_p (sectp->name, &names.abbrev))
c906108c 2341 {
330cdd98
PA
2342 this->abbrev.s.section = sectp;
2343 this->abbrev.size = bfd_get_section_size (sectp);
c906108c 2344 }
330cdd98 2345 else if (section_is_p (sectp->name, &names.line))
c906108c 2346 {
330cdd98
PA
2347 this->line.s.section = sectp;
2348 this->line.size = bfd_get_section_size (sectp);
c906108c 2349 }
330cdd98 2350 else if (section_is_p (sectp->name, &names.loc))
c906108c 2351 {
330cdd98
PA
2352 this->loc.s.section = sectp;
2353 this->loc.size = bfd_get_section_size (sectp);
c906108c 2354 }
330cdd98 2355 else if (section_is_p (sectp->name, &names.loclists))
43988095 2356 {
330cdd98
PA
2357 this->loclists.s.section = sectp;
2358 this->loclists.size = bfd_get_section_size (sectp);
43988095 2359 }
330cdd98 2360 else if (section_is_p (sectp->name, &names.macinfo))
c906108c 2361 {
330cdd98
PA
2362 this->macinfo.s.section = sectp;
2363 this->macinfo.size = bfd_get_section_size (sectp);
c906108c 2364 }
330cdd98 2365 else if (section_is_p (sectp->name, &names.macro))
cf2c3c16 2366 {
330cdd98
PA
2367 this->macro.s.section = sectp;
2368 this->macro.size = bfd_get_section_size (sectp);
cf2c3c16 2369 }
330cdd98 2370 else if (section_is_p (sectp->name, &names.str))
c906108c 2371 {
330cdd98
PA
2372 this->str.s.section = sectp;
2373 this->str.size = bfd_get_section_size (sectp);
c906108c 2374 }
330cdd98 2375 else if (section_is_p (sectp->name, &names.line_str))
43988095 2376 {
330cdd98
PA
2377 this->line_str.s.section = sectp;
2378 this->line_str.size = bfd_get_section_size (sectp);
43988095 2379 }
330cdd98 2380 else if (section_is_p (sectp->name, &names.addr))
3019eac3 2381 {
330cdd98
PA
2382 this->addr.s.section = sectp;
2383 this->addr.size = bfd_get_section_size (sectp);
3019eac3 2384 }
330cdd98 2385 else if (section_is_p (sectp->name, &names.frame))
b6af0555 2386 {
330cdd98
PA
2387 this->frame.s.section = sectp;
2388 this->frame.size = bfd_get_section_size (sectp);
b6af0555 2389 }
330cdd98 2390 else if (section_is_p (sectp->name, &names.eh_frame))
b6af0555 2391 {
330cdd98
PA
2392 this->eh_frame.s.section = sectp;
2393 this->eh_frame.size = bfd_get_section_size (sectp);
b6af0555 2394 }
330cdd98 2395 else if (section_is_p (sectp->name, &names.ranges))
af34e669 2396 {
330cdd98
PA
2397 this->ranges.s.section = sectp;
2398 this->ranges.size = bfd_get_section_size (sectp);
af34e669 2399 }
330cdd98 2400 else if (section_is_p (sectp->name, &names.rnglists))
43988095 2401 {
330cdd98
PA
2402 this->rnglists.s.section = sectp;
2403 this->rnglists.size = bfd_get_section_size (sectp);
43988095 2404 }
330cdd98 2405 else if (section_is_p (sectp->name, &names.types))
348e048f 2406 {
8b70b953
TT
2407 struct dwarf2_section_info type_section;
2408
2409 memset (&type_section, 0, sizeof (type_section));
049412e3 2410 type_section.s.section = sectp;
8b70b953
TT
2411 type_section.size = bfd_get_section_size (sectp);
2412
330cdd98 2413 VEC_safe_push (dwarf2_section_info_def, this->types,
8b70b953 2414 &type_section);
348e048f 2415 }
330cdd98 2416 else if (section_is_p (sectp->name, &names.gdb_index))
9291a0cd 2417 {
330cdd98
PA
2418 this->gdb_index.s.section = sectp;
2419 this->gdb_index.size = bfd_get_section_size (sectp);
9291a0cd 2420 }
927aa2e7
JK
2421 else if (section_is_p (sectp->name, &names.debug_names))
2422 {
2423 this->debug_names.s.section = sectp;
2424 this->debug_names.size = bfd_get_section_size (sectp);
2425 }
2426 else if (section_is_p (sectp->name, &names.debug_aranges))
2427 {
2428 this->debug_aranges.s.section = sectp;
2429 this->debug_aranges.size = bfd_get_section_size (sectp);
2430 }
dce234bc 2431
b4e1fd61 2432 if ((bfd_get_section_flags (abfd, sectp) & (SEC_LOAD | SEC_ALLOC))
72dca2f5 2433 && bfd_section_vma (abfd, sectp) == 0)
330cdd98 2434 this->has_section_at_zero = true;
c906108c
SS
2435}
2436
fceca515
DE
2437/* A helper function that decides whether a section is empty,
2438 or not present. */
9e0ac564
TT
2439
2440static int
19ac8c2e 2441dwarf2_section_empty_p (const struct dwarf2_section_info *section)
9e0ac564 2442{
73869dc2
DE
2443 if (section->is_virtual)
2444 return section->size == 0;
049412e3 2445 return section->s.section == NULL || section->size == 0;
9e0ac564
TT
2446}
2447
cd4fb1b2 2448/* See dwarf2read.h. */
c906108c 2449
cd4fb1b2
SM
2450void
2451dwarf2_read_section (struct objfile *objfile, dwarf2_section_info *info)
c906108c 2452{
a32a8923 2453 asection *sectp;
3019eac3 2454 bfd *abfd;
dce234bc 2455 gdb_byte *buf, *retbuf;
c906108c 2456
be391dca
TT
2457 if (info->readin)
2458 return;
dce234bc 2459 info->buffer = NULL;
be391dca 2460 info->readin = 1;
188dd5d6 2461
9e0ac564 2462 if (dwarf2_section_empty_p (info))
dce234bc 2463 return;
c906108c 2464
a32a8923 2465 sectp = get_section_bfd_section (info);
3019eac3 2466
73869dc2
DE
2467 /* If this is a virtual section we need to read in the real one first. */
2468 if (info->is_virtual)
2469 {
2470 struct dwarf2_section_info *containing_section =
2471 get_containing_section (info);
2472
2473 gdb_assert (sectp != NULL);
2474 if ((sectp->flags & SEC_RELOC) != 0)
2475 {
2476 error (_("Dwarf Error: DWP format V2 with relocations is not"
2477 " supported in section %s [in module %s]"),
2478 get_section_name (info), get_section_file_name (info));
2479 }
2480 dwarf2_read_section (objfile, containing_section);
2481 /* Other code should have already caught virtual sections that don't
2482 fit. */
2483 gdb_assert (info->virtual_offset + info->size
2484 <= containing_section->size);
2485 /* If the real section is empty or there was a problem reading the
2486 section we shouldn't get here. */
2487 gdb_assert (containing_section->buffer != NULL);
2488 info->buffer = containing_section->buffer + info->virtual_offset;
2489 return;
2490 }
2491
4bf44c1c
TT
2492 /* If the section has relocations, we must read it ourselves.
2493 Otherwise we attach it to the BFD. */
2494 if ((sectp->flags & SEC_RELOC) == 0)
dce234bc 2495 {
d521ce57 2496 info->buffer = gdb_bfd_map_section (sectp, &info->size);
4bf44c1c 2497 return;
dce234bc 2498 }
dce234bc 2499
224c3ddb 2500 buf = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, info->size);
4bf44c1c 2501 info->buffer = buf;
dce234bc
PP
2502
2503 /* When debugging .o files, we may need to apply relocations; see
2504 http://sourceware.org/ml/gdb-patches/2002-04/msg00136.html .
2505 We never compress sections in .o files, so we only need to
2506 try this when the section is not compressed. */
ac8035ab 2507 retbuf = symfile_relocate_debug_section (objfile, sectp, buf);
dce234bc
PP
2508 if (retbuf != NULL)
2509 {
2510 info->buffer = retbuf;
2511 return;
2512 }
2513
a32a8923
DE
2514 abfd = get_section_bfd_owner (info);
2515 gdb_assert (abfd != NULL);
2516
dce234bc
PP
2517 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
2518 || bfd_bread (buf, info->size, abfd) != info->size)
19ac8c2e
DE
2519 {
2520 error (_("Dwarf Error: Can't read DWARF data"
2521 " in section %s [in module %s]"),
2522 bfd_section_name (abfd, sectp), bfd_get_filename (abfd));
2523 }
dce234bc
PP
2524}
2525
9e0ac564
TT
2526/* A helper function that returns the size of a section in a safe way.
2527 If you are positive that the section has been read before using the
2528 size, then it is safe to refer to the dwarf2_section_info object's
2529 "size" field directly. In other cases, you must call this
2530 function, because for compressed sections the size field is not set
2531 correctly until the section has been read. */
2532
2533static bfd_size_type
2534dwarf2_section_size (struct objfile *objfile,
2535 struct dwarf2_section_info *info)
2536{
2537 if (!info->readin)
2538 dwarf2_read_section (objfile, info);
2539 return info->size;
2540}
2541
dce234bc 2542/* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
0963b4bd 2543 SECTION_NAME. */
af34e669 2544
dce234bc 2545void
3017a003
TG
2546dwarf2_get_section_info (struct objfile *objfile,
2547 enum dwarf2_section_enum sect,
d521ce57 2548 asection **sectp, const gdb_byte **bufp,
dce234bc
PP
2549 bfd_size_type *sizep)
2550{
2551 struct dwarf2_per_objfile *data
9a3c8263
SM
2552 = (struct dwarf2_per_objfile *) objfile_data (objfile,
2553 dwarf2_objfile_data_key);
dce234bc 2554 struct dwarf2_section_info *info;
a3b2a86b
TT
2555
2556 /* We may see an objfile without any DWARF, in which case we just
2557 return nothing. */
2558 if (data == NULL)
2559 {
2560 *sectp = NULL;
2561 *bufp = NULL;
2562 *sizep = 0;
2563 return;
2564 }
3017a003
TG
2565 switch (sect)
2566 {
2567 case DWARF2_DEBUG_FRAME:
2568 info = &data->frame;
2569 break;
2570 case DWARF2_EH_FRAME:
2571 info = &data->eh_frame;
2572 break;
2573 default:
2574 gdb_assert_not_reached ("unexpected section");
2575 }
dce234bc 2576
9e0ac564 2577 dwarf2_read_section (objfile, info);
dce234bc 2578
a32a8923 2579 *sectp = get_section_bfd_section (info);
dce234bc
PP
2580 *bufp = info->buffer;
2581 *sizep = info->size;
2582}
2583
36586728
TT
2584/* A helper function to find the sections for a .dwz file. */
2585
2586static void
2587locate_dwz_sections (bfd *abfd, asection *sectp, void *arg)
2588{
9a3c8263 2589 struct dwz_file *dwz_file = (struct dwz_file *) arg;
36586728
TT
2590
2591 /* Note that we only support the standard ELF names, because .dwz
2592 is ELF-only (at the time of writing). */
2593 if (section_is_p (sectp->name, &dwarf2_elf_names.abbrev))
2594 {
049412e3 2595 dwz_file->abbrev.s.section = sectp;
36586728
TT
2596 dwz_file->abbrev.size = bfd_get_section_size (sectp);
2597 }
2598 else if (section_is_p (sectp->name, &dwarf2_elf_names.info))
2599 {
049412e3 2600 dwz_file->info.s.section = sectp;
36586728
TT
2601 dwz_file->info.size = bfd_get_section_size (sectp);
2602 }
2603 else if (section_is_p (sectp->name, &dwarf2_elf_names.str))
2604 {
049412e3 2605 dwz_file->str.s.section = sectp;
36586728
TT
2606 dwz_file->str.size = bfd_get_section_size (sectp);
2607 }
2608 else if (section_is_p (sectp->name, &dwarf2_elf_names.line))
2609 {
049412e3 2610 dwz_file->line.s.section = sectp;
36586728
TT
2611 dwz_file->line.size = bfd_get_section_size (sectp);
2612 }
2613 else if (section_is_p (sectp->name, &dwarf2_elf_names.macro))
2614 {
049412e3 2615 dwz_file->macro.s.section = sectp;
36586728
TT
2616 dwz_file->macro.size = bfd_get_section_size (sectp);
2617 }
2ec9a5e0
TT
2618 else if (section_is_p (sectp->name, &dwarf2_elf_names.gdb_index))
2619 {
049412e3 2620 dwz_file->gdb_index.s.section = sectp;
2ec9a5e0
TT
2621 dwz_file->gdb_index.size = bfd_get_section_size (sectp);
2622 }
927aa2e7
JK
2623 else if (section_is_p (sectp->name, &dwarf2_elf_names.debug_names))
2624 {
2625 dwz_file->debug_names.s.section = sectp;
2626 dwz_file->debug_names.size = bfd_get_section_size (sectp);
2627 }
36586728
TT
2628}
2629
4db1a1dc
TT
2630/* Open the separate '.dwz' debug file, if needed. Return NULL if
2631 there is no .gnu_debugaltlink section in the file. Error if there
2632 is such a section but the file cannot be found. */
36586728
TT
2633
2634static struct dwz_file *
ed2dc618 2635dwarf2_get_dwz_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
36586728 2636{
36586728
TT
2637 const char *filename;
2638 struct dwz_file *result;
acd13123 2639 bfd_size_type buildid_len_arg;
dc294be5
TT
2640 size_t buildid_len;
2641 bfd_byte *buildid;
36586728
TT
2642
2643 if (dwarf2_per_objfile->dwz_file != NULL)
2644 return dwarf2_per_objfile->dwz_file;
2645
4db1a1dc 2646 bfd_set_error (bfd_error_no_error);
791afaa2
TT
2647 gdb::unique_xmalloc_ptr<char> data
2648 (bfd_get_alt_debug_link_info (dwarf2_per_objfile->objfile->obfd,
2649 &buildid_len_arg, &buildid));
4db1a1dc
TT
2650 if (data == NULL)
2651 {
2652 if (bfd_get_error () == bfd_error_no_error)
2653 return NULL;
2654 error (_("could not read '.gnu_debugaltlink' section: %s"),
2655 bfd_errmsg (bfd_get_error ()));
2656 }
791afaa2
TT
2657
2658 gdb::unique_xmalloc_ptr<bfd_byte> buildid_holder (buildid);
36586728 2659
acd13123
TT
2660 buildid_len = (size_t) buildid_len_arg;
2661
791afaa2 2662 filename = data.get ();
d721ba37
PA
2663
2664 std::string abs_storage;
36586728
TT
2665 if (!IS_ABSOLUTE_PATH (filename))
2666 {
14278e1f
TT
2667 gdb::unique_xmalloc_ptr<char> abs
2668 = gdb_realpath (objfile_name (dwarf2_per_objfile->objfile));
36586728 2669
14278e1f 2670 abs_storage = ldirname (abs.get ()) + SLASH_STRING + filename;
d721ba37 2671 filename = abs_storage.c_str ();
36586728
TT
2672 }
2673
dc294be5
TT
2674 /* First try the file name given in the section. If that doesn't
2675 work, try to use the build-id instead. */
192b62ce 2676 gdb_bfd_ref_ptr dwz_bfd (gdb_bfd_open (filename, gnutarget, -1));
dc294be5 2677 if (dwz_bfd != NULL)
36586728 2678 {
192b62ce
TT
2679 if (!build_id_verify (dwz_bfd.get (), buildid_len, buildid))
2680 dwz_bfd.release ();
36586728
TT
2681 }
2682
dc294be5
TT
2683 if (dwz_bfd == NULL)
2684 dwz_bfd = build_id_to_debug_bfd (buildid_len, buildid);
2685
2686 if (dwz_bfd == NULL)
2687 error (_("could not find '.gnu_debugaltlink' file for %s"),
2688 objfile_name (dwarf2_per_objfile->objfile));
2689
36586728
TT
2690 result = OBSTACK_ZALLOC (&dwarf2_per_objfile->objfile->objfile_obstack,
2691 struct dwz_file);
192b62ce 2692 result->dwz_bfd = dwz_bfd.release ();
36586728 2693
192b62ce 2694 bfd_map_over_sections (result->dwz_bfd, locate_dwz_sections, result);
36586728 2695
192b62ce 2696 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd, result->dwz_bfd);
8d2cc612 2697 dwarf2_per_objfile->dwz_file = result;
36586728
TT
2698 return result;
2699}
9291a0cd 2700\f
7b9f3c50
DE
2701/* DWARF quick_symbols_functions support. */
2702
2703/* TUs can share .debug_line entries, and there can be a lot more TUs than
2704 unique line tables, so we maintain a separate table of all .debug_line
2705 derived entries to support the sharing.
2706 All the quick functions need is the list of file names. We discard the
2707 line_header when we're done and don't need to record it here. */
2708struct quick_file_names
2709{
094b34ac
DE
2710 /* The data used to construct the hash key. */
2711 struct stmt_list_hash hash;
7b9f3c50
DE
2712
2713 /* The number of entries in file_names, real_names. */
2714 unsigned int num_file_names;
2715
2716 /* The file names from the line table, after being run through
2717 file_full_name. */
2718 const char **file_names;
2719
2720 /* The file names from the line table after being run through
2721 gdb_realpath. These are computed lazily. */
2722 const char **real_names;
2723};
2724
2725/* When using the index (and thus not using psymtabs), each CU has an
2726 object of this type. This is used to hold information needed by
2727 the various "quick" methods. */
2728struct dwarf2_per_cu_quick_data
2729{
2730 /* The file table. This can be NULL if there was no file table
2731 or it's currently not read in.
2732 NOTE: This points into dwarf2_per_objfile->quick_file_names_table. */
2733 struct quick_file_names *file_names;
2734
2735 /* The corresponding symbol table. This is NULL if symbols for this
2736 CU have not yet been read. */
43f3e411 2737 struct compunit_symtab *compunit_symtab;
7b9f3c50
DE
2738
2739 /* A temporary mark bit used when iterating over all CUs in
2740 expand_symtabs_matching. */
2741 unsigned int mark : 1;
2742
2743 /* True if we've tried to read the file table and found there isn't one.
2744 There will be no point in trying to read it again next time. */
2745 unsigned int no_file_data : 1;
2746};
2747
094b34ac
DE
2748/* Utility hash function for a stmt_list_hash. */
2749
2750static hashval_t
2751hash_stmt_list_entry (const struct stmt_list_hash *stmt_list_hash)
2752{
2753 hashval_t v = 0;
2754
2755 if (stmt_list_hash->dwo_unit != NULL)
2756 v += (uintptr_t) stmt_list_hash->dwo_unit->dwo_file;
9c541725 2757 v += to_underlying (stmt_list_hash->line_sect_off);
094b34ac
DE
2758 return v;
2759}
2760
2761/* Utility equality function for a stmt_list_hash. */
2762
2763static int
2764eq_stmt_list_entry (const struct stmt_list_hash *lhs,
2765 const struct stmt_list_hash *rhs)
2766{
2767 if ((lhs->dwo_unit != NULL) != (rhs->dwo_unit != NULL))
2768 return 0;
2769 if (lhs->dwo_unit != NULL
2770 && lhs->dwo_unit->dwo_file != rhs->dwo_unit->dwo_file)
2771 return 0;
2772
9c541725 2773 return lhs->line_sect_off == rhs->line_sect_off;
094b34ac
DE
2774}
2775
7b9f3c50
DE
2776/* Hash function for a quick_file_names. */
2777
2778static hashval_t
2779hash_file_name_entry (const void *e)
2780{
9a3c8263
SM
2781 const struct quick_file_names *file_data
2782 = (const struct quick_file_names *) e;
7b9f3c50 2783
094b34ac 2784 return hash_stmt_list_entry (&file_data->hash);
7b9f3c50
DE
2785}
2786
2787/* Equality function for a quick_file_names. */
2788
2789static int
2790eq_file_name_entry (const void *a, const void *b)
2791{
9a3c8263
SM
2792 const struct quick_file_names *ea = (const struct quick_file_names *) a;
2793 const struct quick_file_names *eb = (const struct quick_file_names *) b;
7b9f3c50 2794
094b34ac 2795 return eq_stmt_list_entry (&ea->hash, &eb->hash);
7b9f3c50
DE
2796}
2797
2798/* Delete function for a quick_file_names. */
2799
2800static void
2801delete_file_name_entry (void *e)
2802{
9a3c8263 2803 struct quick_file_names *file_data = (struct quick_file_names *) e;
7b9f3c50
DE
2804 int i;
2805
2806 for (i = 0; i < file_data->num_file_names; ++i)
2807 {
2808 xfree ((void*) file_data->file_names[i]);
2809 if (file_data->real_names)
2810 xfree ((void*) file_data->real_names[i]);
2811 }
2812
2813 /* The space for the struct itself lives on objfile_obstack,
2814 so we don't free it here. */
2815}
2816
2817/* Create a quick_file_names hash table. */
2818
2819static htab_t
2820create_quick_file_names_table (unsigned int nr_initial_entries)
2821{
2822 return htab_create_alloc (nr_initial_entries,
2823 hash_file_name_entry, eq_file_name_entry,
2824 delete_file_name_entry, xcalloc, xfree);
2825}
9291a0cd 2826
918dd910
JK
2827/* Read in PER_CU->CU. This function is unrelated to symtabs, symtab would
2828 have to be created afterwards. You should call age_cached_comp_units after
2829 processing PER_CU->CU. dw2_setup must have been already called. */
2830
2831static void
2832load_cu (struct dwarf2_per_cu_data *per_cu)
2833{
3019eac3 2834 if (per_cu->is_debug_types)
e5fe5e75 2835 load_full_type_unit (per_cu);
918dd910 2836 else
95554aad 2837 load_full_comp_unit (per_cu, language_minimal);
918dd910 2838
cc12ce38
DE
2839 if (per_cu->cu == NULL)
2840 return; /* Dummy CU. */
2dc860c0
DE
2841
2842 dwarf2_find_base_address (per_cu->cu->dies, per_cu->cu);
918dd910
JK
2843}
2844
a0f42c21 2845/* Read in the symbols for PER_CU. */
2fdf6df6 2846
9291a0cd 2847static void
a0f42c21 2848dw2_do_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
9291a0cd 2849{
ed2dc618 2850 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
9291a0cd 2851
f4dc4d17
DE
2852 /* Skip type_unit_groups, reading the type units they contain
2853 is handled elsewhere. */
2854 if (IS_TYPE_UNIT_GROUP (per_cu))
2855 return;
2856
b303c6f6
AB
2857 /* The destructor of dwarf2_queue_guard frees any entries left on
2858 the queue. After this point we're guaranteed to leave this function
2859 with the dwarf queue empty. */
2860 dwarf2_queue_guard q_guard;
9291a0cd 2861
95554aad 2862 if (dwarf2_per_objfile->using_index
43f3e411 2863 ? per_cu->v.quick->compunit_symtab == NULL
95554aad
TT
2864 : (per_cu->v.psymtab == NULL || !per_cu->v.psymtab->readin))
2865 {
2866 queue_comp_unit (per_cu, language_minimal);
2867 load_cu (per_cu);
89e63ee4
DE
2868
2869 /* If we just loaded a CU from a DWO, and we're working with an index
2870 that may badly handle TUs, load all the TUs in that DWO as well.
2871 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
2872 if (!per_cu->is_debug_types
cc12ce38 2873 && per_cu->cu != NULL
89e63ee4
DE
2874 && per_cu->cu->dwo_unit != NULL
2875 && dwarf2_per_objfile->index_table != NULL
2876 && dwarf2_per_objfile->index_table->version <= 7
2877 /* DWP files aren't supported yet. */
ed2dc618 2878 && get_dwp_file (dwarf2_per_objfile) == NULL)
89e63ee4 2879 queue_and_load_all_dwo_tus (per_cu);
95554aad 2880 }
9291a0cd 2881
ed2dc618 2882 process_queue (dwarf2_per_objfile);
9291a0cd
TT
2883
2884 /* Age the cache, releasing compilation units that have not
2885 been used recently. */
ed2dc618 2886 age_cached_comp_units (dwarf2_per_objfile);
9291a0cd
TT
2887}
2888
2889/* Ensure that the symbols for PER_CU have been read in. OBJFILE is
2890 the objfile from which this CU came. Returns the resulting symbol
2891 table. */
2fdf6df6 2892
43f3e411 2893static struct compunit_symtab *
a0f42c21 2894dw2_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
9291a0cd 2895{
ed2dc618
SM
2896 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
2897
95554aad 2898 gdb_assert (dwarf2_per_objfile->using_index);
43f3e411 2899 if (!per_cu->v.quick->compunit_symtab)
9291a0cd 2900 {
11ed8cad 2901 free_cached_comp_units freer (dwarf2_per_objfile);
c83dd867 2902 scoped_restore decrementer = increment_reading_symtab ();
a0f42c21 2903 dw2_do_instantiate_symtab (per_cu);
ed2dc618 2904 process_cu_includes (dwarf2_per_objfile);
9291a0cd 2905 }
f194fefb 2906
43f3e411 2907 return per_cu->v.quick->compunit_symtab;
9291a0cd
TT
2908}
2909
8832e7e3 2910/* Return the CU/TU given its index.
f4dc4d17
DE
2911
2912 This is intended for loops like:
2913
2914 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2915 + dwarf2_per_objfile->n_type_units); ++i)
2916 {
8832e7e3 2917 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
f4dc4d17
DE
2918
2919 ...;
2920 }
2921*/
2fdf6df6 2922
1fd400ff 2923static struct dwarf2_per_cu_data *
ed2dc618
SM
2924dw2_get_cutu (struct dwarf2_per_objfile *dwarf2_per_objfile,
2925 int index)
1fd400ff
TT
2926{
2927 if (index >= dwarf2_per_objfile->n_comp_units)
2928 {
f4dc4d17 2929 index -= dwarf2_per_objfile->n_comp_units;
094b34ac
DE
2930 gdb_assert (index < dwarf2_per_objfile->n_type_units);
2931 return &dwarf2_per_objfile->all_type_units[index]->per_cu;
f4dc4d17
DE
2932 }
2933
2934 return dwarf2_per_objfile->all_comp_units[index];
2935}
2936
8832e7e3
DE
2937/* Return the CU given its index.
2938 This differs from dw2_get_cutu in that it's for when you know INDEX
2939 refers to a CU. */
f4dc4d17
DE
2940
2941static struct dwarf2_per_cu_data *
ed2dc618 2942dw2_get_cu (struct dwarf2_per_objfile *dwarf2_per_objfile, int index)
f4dc4d17 2943{
8832e7e3 2944 gdb_assert (index >= 0 && index < dwarf2_per_objfile->n_comp_units);
f4dc4d17 2945
1fd400ff
TT
2946 return dwarf2_per_objfile->all_comp_units[index];
2947}
2948
4b514bc8
JK
2949/* Return a new dwarf2_per_cu_data allocated on OBJFILE's
2950 objfile_obstack, and constructed with the specified field
2951 values. */
2952
2953static dwarf2_per_cu_data *
ed2dc618 2954create_cu_from_index_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
4b514bc8
JK
2955 struct dwarf2_section_info *section,
2956 int is_dwz,
2957 sect_offset sect_off, ULONGEST length)
2958{
ed2dc618 2959 struct objfile *objfile = dwarf2_per_objfile->objfile;
4b514bc8
JK
2960 dwarf2_per_cu_data *the_cu
2961 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2962 struct dwarf2_per_cu_data);
2963 the_cu->sect_off = sect_off;
2964 the_cu->length = length;
e3b94546 2965 the_cu->dwarf2_per_objfile = dwarf2_per_objfile;
4b514bc8
JK
2966 the_cu->section = section;
2967 the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2968 struct dwarf2_per_cu_quick_data);
2969 the_cu->is_dwz = is_dwz;
2970 return the_cu;
2971}
2972
2ec9a5e0
TT
2973/* A helper for create_cus_from_index that handles a given list of
2974 CUs. */
2fdf6df6 2975
74a0d9f6 2976static void
2ec9a5e0
TT
2977create_cus_from_index_list (struct objfile *objfile,
2978 const gdb_byte *cu_list, offset_type n_elements,
2979 struct dwarf2_section_info *section,
2980 int is_dwz,
2981 int base_offset)
9291a0cd
TT
2982{
2983 offset_type i;
ed2dc618
SM
2984 struct dwarf2_per_objfile *dwarf2_per_objfile
2985 = get_dwarf2_per_objfile (objfile);
9291a0cd 2986
2ec9a5e0 2987 for (i = 0; i < n_elements; i += 2)
9291a0cd 2988 {
74a0d9f6 2989 gdb_static_assert (sizeof (ULONGEST) >= 8);
9c541725
PA
2990
2991 sect_offset sect_off
2992 = (sect_offset) extract_unsigned_integer (cu_list, 8, BFD_ENDIAN_LITTLE);
2993 ULONGEST length = extract_unsigned_integer (cu_list + 8, 8, BFD_ENDIAN_LITTLE);
9291a0cd
TT
2994 cu_list += 2 * 8;
2995
4b514bc8 2996 dwarf2_per_objfile->all_comp_units[base_offset + i / 2]
ed2dc618
SM
2997 = create_cu_from_index_list (dwarf2_per_objfile, section, is_dwz,
2998 sect_off, length);
9291a0cd 2999 }
9291a0cd
TT
3000}
3001
2ec9a5e0 3002/* Read the CU list from the mapped index, and use it to create all
74a0d9f6 3003 the CU objects for this objfile. */
2ec9a5e0 3004
74a0d9f6 3005static void
2ec9a5e0
TT
3006create_cus_from_index (struct objfile *objfile,
3007 const gdb_byte *cu_list, offset_type cu_list_elements,
3008 const gdb_byte *dwz_list, offset_type dwz_elements)
3009{
3010 struct dwz_file *dwz;
ed2dc618
SM
3011 struct dwarf2_per_objfile *dwarf2_per_objfile
3012 = get_dwarf2_per_objfile (objfile);
2ec9a5e0
TT
3013
3014 dwarf2_per_objfile->n_comp_units = (cu_list_elements + dwz_elements) / 2;
8d749320
SM
3015 dwarf2_per_objfile->all_comp_units =
3016 XOBNEWVEC (&objfile->objfile_obstack, struct dwarf2_per_cu_data *,
3017 dwarf2_per_objfile->n_comp_units);
2ec9a5e0 3018
74a0d9f6
JK
3019 create_cus_from_index_list (objfile, cu_list, cu_list_elements,
3020 &dwarf2_per_objfile->info, 0, 0);
2ec9a5e0
TT
3021
3022 if (dwz_elements == 0)
74a0d9f6 3023 return;
2ec9a5e0 3024
ed2dc618 3025 dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
74a0d9f6
JK
3026 create_cus_from_index_list (objfile, dwz_list, dwz_elements, &dwz->info, 1,
3027 cu_list_elements / 2);
2ec9a5e0
TT
3028}
3029
1fd400ff 3030/* Create the signatured type hash table from the index. */
673bfd45 3031
74a0d9f6 3032static void
673bfd45 3033create_signatured_type_table_from_index (struct objfile *objfile,
8b70b953 3034 struct dwarf2_section_info *section,
673bfd45
DE
3035 const gdb_byte *bytes,
3036 offset_type elements)
1fd400ff
TT
3037{
3038 offset_type i;
673bfd45 3039 htab_t sig_types_hash;
ed2dc618
SM
3040 struct dwarf2_per_objfile *dwarf2_per_objfile
3041 = get_dwarf2_per_objfile (objfile);
1fd400ff 3042
6aa5f3a6
DE
3043 dwarf2_per_objfile->n_type_units
3044 = dwarf2_per_objfile->n_allocated_type_units
3045 = elements / 3;
8d749320
SM
3046 dwarf2_per_objfile->all_type_units =
3047 XNEWVEC (struct signatured_type *, dwarf2_per_objfile->n_type_units);
1fd400ff 3048
673bfd45 3049 sig_types_hash = allocate_signatured_type_table (objfile);
1fd400ff
TT
3050
3051 for (i = 0; i < elements; i += 3)
3052 {
52dc124a 3053 struct signatured_type *sig_type;
9c541725 3054 ULONGEST signature;
1fd400ff 3055 void **slot;
9c541725 3056 cu_offset type_offset_in_tu;
1fd400ff 3057
74a0d9f6 3058 gdb_static_assert (sizeof (ULONGEST) >= 8);
9c541725
PA
3059 sect_offset sect_off
3060 = (sect_offset) extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
3061 type_offset_in_tu
3062 = (cu_offset) extract_unsigned_integer (bytes + 8, 8,
3063 BFD_ENDIAN_LITTLE);
1fd400ff
TT
3064 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
3065 bytes += 3 * 8;
3066
52dc124a 3067 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1fd400ff 3068 struct signatured_type);
52dc124a 3069 sig_type->signature = signature;
9c541725 3070 sig_type->type_offset_in_tu = type_offset_in_tu;
3019eac3 3071 sig_type->per_cu.is_debug_types = 1;
8a0459fd 3072 sig_type->per_cu.section = section;
9c541725 3073 sig_type->per_cu.sect_off = sect_off;
e3b94546 3074 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
52dc124a 3075 sig_type->per_cu.v.quick
1fd400ff
TT
3076 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3077 struct dwarf2_per_cu_quick_data);
3078
52dc124a
DE
3079 slot = htab_find_slot (sig_types_hash, sig_type, INSERT);
3080 *slot = sig_type;
1fd400ff 3081
b4dd5633 3082 dwarf2_per_objfile->all_type_units[i / 3] = sig_type;
1fd400ff
TT
3083 }
3084
673bfd45 3085 dwarf2_per_objfile->signatured_types = sig_types_hash;
1fd400ff
TT
3086}
3087
927aa2e7
JK
3088/* Create the signatured type hash table from .debug_names. */
3089
3090static void
3091create_signatured_type_table_from_debug_names
ed2dc618 3092 (struct dwarf2_per_objfile *dwarf2_per_objfile,
927aa2e7
JK
3093 const mapped_debug_names &map,
3094 struct dwarf2_section_info *section,
3095 struct dwarf2_section_info *abbrev_section)
3096{
ed2dc618
SM
3097 struct objfile *objfile = dwarf2_per_objfile->objfile;
3098
927aa2e7
JK
3099 dwarf2_read_section (objfile, section);
3100 dwarf2_read_section (objfile, abbrev_section);
3101
3102 dwarf2_per_objfile->n_type_units
3103 = dwarf2_per_objfile->n_allocated_type_units
3104 = map.tu_count;
3105 dwarf2_per_objfile->all_type_units
3106 = XNEWVEC (struct signatured_type *, dwarf2_per_objfile->n_type_units);
3107
3108 htab_t sig_types_hash = allocate_signatured_type_table (objfile);
3109
3110 for (uint32_t i = 0; i < map.tu_count; ++i)
3111 {
3112 struct signatured_type *sig_type;
3113 ULONGEST signature;
3114 void **slot;
3115 cu_offset type_offset_in_tu;
3116
3117 sect_offset sect_off
3118 = (sect_offset) (extract_unsigned_integer
3119 (map.tu_table_reordered + i * map.offset_size,
3120 map.offset_size,
3121 map.dwarf5_byte_order));
3122
3123 comp_unit_head cu_header;
ed2dc618
SM
3124 read_and_check_comp_unit_head (dwarf2_per_objfile, &cu_header, section,
3125 abbrev_section,
927aa2e7
JK
3126 section->buffer + to_underlying (sect_off),
3127 rcuh_kind::TYPE);
3128
3129 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3130 struct signatured_type);
3131 sig_type->signature = cu_header.signature;
3132 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
3133 sig_type->per_cu.is_debug_types = 1;
3134 sig_type->per_cu.section = section;
3135 sig_type->per_cu.sect_off = sect_off;
e3b94546 3136 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
927aa2e7
JK
3137 sig_type->per_cu.v.quick
3138 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3139 struct dwarf2_per_cu_quick_data);
3140
3141 slot = htab_find_slot (sig_types_hash, sig_type, INSERT);
3142 *slot = sig_type;
3143
3144 dwarf2_per_objfile->all_type_units[i] = sig_type;
3145 }
3146
3147 dwarf2_per_objfile->signatured_types = sig_types_hash;
3148}
3149
9291a0cd
TT
3150/* Read the address map data from the mapped index, and use it to
3151 populate the objfile's psymtabs_addrmap. */
2fdf6df6 3152
9291a0cd 3153static void
ed2dc618
SM
3154create_addrmap_from_index (struct dwarf2_per_objfile *dwarf2_per_objfile,
3155 struct mapped_index *index)
9291a0cd 3156{
ed2dc618 3157 struct objfile *objfile = dwarf2_per_objfile->objfile;
3e29f34a 3158 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9291a0cd 3159 const gdb_byte *iter, *end;
9291a0cd 3160 struct addrmap *mutable_map;
9291a0cd
TT
3161 CORE_ADDR baseaddr;
3162
8268c778
PA
3163 auto_obstack temp_obstack;
3164
9291a0cd
TT
3165 mutable_map = addrmap_create_mutable (&temp_obstack);
3166
f00a2de2
PA
3167 iter = index->address_table.data ();
3168 end = iter + index->address_table.size ();
9291a0cd
TT
3169
3170 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
3171
3172 while (iter < end)
3173 {
3174 ULONGEST hi, lo, cu_index;
3175 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
3176 iter += 8;
3177 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
3178 iter += 8;
3179 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
3180 iter += 4;
f652bce2 3181
24a55014 3182 if (lo > hi)
f652bce2 3183 {
24a55014
DE
3184 complaint (&symfile_complaints,
3185 _(".gdb_index address table has invalid range (%s - %s)"),
c0cd8254 3186 hex_string (lo), hex_string (hi));
24a55014 3187 continue;
f652bce2 3188 }
24a55014
DE
3189
3190 if (cu_index >= dwarf2_per_objfile->n_comp_units)
f652bce2
DE
3191 {
3192 complaint (&symfile_complaints,
3193 _(".gdb_index address table has invalid CU number %u"),
3194 (unsigned) cu_index);
24a55014 3195 continue;
f652bce2 3196 }
24a55014 3197
3e29f34a
MR
3198 lo = gdbarch_adjust_dwarf2_addr (gdbarch, lo + baseaddr);
3199 hi = gdbarch_adjust_dwarf2_addr (gdbarch, hi + baseaddr);
ed2dc618
SM
3200 addrmap_set_empty (mutable_map, lo, hi - 1,
3201 dw2_get_cutu (dwarf2_per_objfile, cu_index));
9291a0cd
TT
3202 }
3203
3204 objfile->psymtabs_addrmap = addrmap_create_fixed (mutable_map,
3205 &objfile->objfile_obstack);
9291a0cd
TT
3206}
3207
927aa2e7
JK
3208/* Read the address map data from DWARF-5 .debug_aranges, and use it to
3209 populate the objfile's psymtabs_addrmap. */
3210
3211static void
ed2dc618 3212create_addrmap_from_aranges (struct dwarf2_per_objfile *dwarf2_per_objfile,
927aa2e7
JK
3213 struct dwarf2_section_info *section)
3214{
ed2dc618 3215 struct objfile *objfile = dwarf2_per_objfile->objfile;
927aa2e7
JK
3216 bfd *abfd = objfile->obfd;
3217 struct gdbarch *gdbarch = get_objfile_arch (objfile);
3218 const CORE_ADDR baseaddr = ANOFFSET (objfile->section_offsets,
3219 SECT_OFF_TEXT (objfile));
3220
3221 auto_obstack temp_obstack;
3222 addrmap *mutable_map = addrmap_create_mutable (&temp_obstack);
3223
3224 std::unordered_map<sect_offset,
3225 dwarf2_per_cu_data *,
3226 gdb::hash_enum<sect_offset>>
3227 debug_info_offset_to_per_cu;
3228 for (int cui = 0; cui < dwarf2_per_objfile->n_comp_units; ++cui)
3229 {
ed2dc618 3230 dwarf2_per_cu_data *per_cu = dw2_get_cutu (dwarf2_per_objfile, cui);
927aa2e7
JK
3231 const auto insertpair
3232 = debug_info_offset_to_per_cu.emplace (per_cu->sect_off, per_cu);
3233 if (!insertpair.second)
3234 {
3235 warning (_("Section .debug_aranges in %s has duplicate "
9d8780f0
SM
3236 "debug_info_offset %s, ignoring .debug_aranges."),
3237 objfile_name (objfile), sect_offset_str (per_cu->sect_off));
927aa2e7
JK
3238 return;
3239 }
3240 }
3241
3242 dwarf2_read_section (objfile, section);
3243
3244 const bfd_endian dwarf5_byte_order = gdbarch_byte_order (gdbarch);
3245
3246 const gdb_byte *addr = section->buffer;
3247
3248 while (addr < section->buffer + section->size)
3249 {
3250 const gdb_byte *const entry_addr = addr;
3251 unsigned int bytes_read;
3252
3253 const LONGEST entry_length = read_initial_length (abfd, addr,
3254 &bytes_read);
3255 addr += bytes_read;
3256
3257 const gdb_byte *const entry_end = addr + entry_length;
3258 const bool dwarf5_is_dwarf64 = bytes_read != 4;
3259 const uint8_t offset_size = dwarf5_is_dwarf64 ? 8 : 4;
3260 if (addr + entry_length > section->buffer + section->size)
3261 {
3262 warning (_("Section .debug_aranges in %s entry at offset %zu "
3263 "length %s exceeds section length %s, "
3264 "ignoring .debug_aranges."),
3265 objfile_name (objfile), entry_addr - section->buffer,
3266 plongest (bytes_read + entry_length),
3267 pulongest (section->size));
3268 return;
3269 }
3270
3271 /* The version number. */
3272 const uint16_t version = read_2_bytes (abfd, addr);
3273 addr += 2;
3274 if (version != 2)
3275 {
3276 warning (_("Section .debug_aranges in %s entry at offset %zu "
3277 "has unsupported version %d, ignoring .debug_aranges."),
3278 objfile_name (objfile), entry_addr - section->buffer,
3279 version);
3280 return;
3281 }
3282
3283 const uint64_t debug_info_offset
3284 = extract_unsigned_integer (addr, offset_size, dwarf5_byte_order);
3285 addr += offset_size;
3286 const auto per_cu_it
3287 = debug_info_offset_to_per_cu.find (sect_offset (debug_info_offset));
3288 if (per_cu_it == debug_info_offset_to_per_cu.cend ())
3289 {
3290 warning (_("Section .debug_aranges in %s entry at offset %zu "
3291 "debug_info_offset %s does not exists, "
3292 "ignoring .debug_aranges."),
3293 objfile_name (objfile), entry_addr - section->buffer,
3294 pulongest (debug_info_offset));
3295 return;
3296 }
3297 dwarf2_per_cu_data *const per_cu = per_cu_it->second;
3298
3299 const uint8_t address_size = *addr++;
3300 if (address_size < 1 || address_size > 8)
3301 {
3302 warning (_("Section .debug_aranges in %s entry at offset %zu "
3303 "address_size %u is invalid, ignoring .debug_aranges."),
3304 objfile_name (objfile), entry_addr - section->buffer,
3305 address_size);
3306 return;
3307 }
3308
3309 const uint8_t segment_selector_size = *addr++;
3310 if (segment_selector_size != 0)
3311 {
3312 warning (_("Section .debug_aranges in %s entry at offset %zu "
3313 "segment_selector_size %u is not supported, "
3314 "ignoring .debug_aranges."),
3315 objfile_name (objfile), entry_addr - section->buffer,
3316 segment_selector_size);
3317 return;
3318 }
3319
3320 /* Must pad to an alignment boundary that is twice the address
3321 size. It is undocumented by the DWARF standard but GCC does
3322 use it. */
3323 for (size_t padding = ((-(addr - section->buffer))
3324 & (2 * address_size - 1));
3325 padding > 0; padding--)
3326 if (*addr++ != 0)
3327 {
3328 warning (_("Section .debug_aranges in %s entry at offset %zu "
3329 "padding is not zero, ignoring .debug_aranges."),
3330 objfile_name (objfile), entry_addr - section->buffer);
3331 return;
3332 }
3333
3334 for (;;)
3335 {
3336 if (addr + 2 * address_size > entry_end)
3337 {
3338 warning (_("Section .debug_aranges in %s entry at offset %zu "
3339 "address list is not properly terminated, "
3340 "ignoring .debug_aranges."),
3341 objfile_name (objfile), entry_addr - section->buffer);
3342 return;
3343 }
3344 ULONGEST start = extract_unsigned_integer (addr, address_size,
3345 dwarf5_byte_order);
3346 addr += address_size;
3347 ULONGEST length = extract_unsigned_integer (addr, address_size,
3348 dwarf5_byte_order);
3349 addr += address_size;
3350 if (start == 0 && length == 0)
3351 break;
3352 if (start == 0 && !dwarf2_per_objfile->has_section_at_zero)
3353 {
3354 /* Symbol was eliminated due to a COMDAT group. */
3355 continue;
3356 }
3357 ULONGEST end = start + length;
3358 start = gdbarch_adjust_dwarf2_addr (gdbarch, start + baseaddr);
3359 end = gdbarch_adjust_dwarf2_addr (gdbarch, end + baseaddr);
3360 addrmap_set_empty (mutable_map, start, end - 1, per_cu);
3361 }
3362 }
3363
3364 objfile->psymtabs_addrmap = addrmap_create_fixed (mutable_map,
3365 &objfile->objfile_obstack);
3366}
3367
9291a0cd
TT
3368/* Find a slot in the mapped index INDEX for the object named NAME.
3369 If NAME is found, set *VEC_OUT to point to the CU vector in the
109483d9
PA
3370 constant pool and return true. If NAME cannot be found, return
3371 false. */
2fdf6df6 3372
109483d9 3373static bool
9291a0cd
TT
3374find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
3375 offset_type **vec_out)
3376{
0cf03b49 3377 offset_type hash;
9291a0cd 3378 offset_type slot, step;
559a7a62 3379 int (*cmp) (const char *, const char *);
9291a0cd 3380
791afaa2 3381 gdb::unique_xmalloc_ptr<char> without_params;
0cf03b49 3382 if (current_language->la_language == language_cplus
45280282
IB
3383 || current_language->la_language == language_fortran
3384 || current_language->la_language == language_d)
0cf03b49
JK
3385 {
3386 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
3387 not contain any. */
a8719064 3388
72998fb3 3389 if (strchr (name, '(') != NULL)
0cf03b49 3390 {
109483d9 3391 without_params = cp_remove_params (name);
0cf03b49 3392
72998fb3 3393 if (without_params != NULL)
791afaa2 3394 name = without_params.get ();
0cf03b49
JK
3395 }
3396 }
3397
559a7a62 3398 /* Index version 4 did not support case insensitive searches. But the
feea76c2 3399 indices for case insensitive languages are built in lowercase, therefore
559a7a62
JK
3400 simulate our NAME being searched is also lowercased. */
3401 hash = mapped_index_string_hash ((index->version == 4
3402 && case_sensitivity == case_sensitive_off
3403 ? 5 : index->version),
3404 name);
3405
f00a2de2
PA
3406 slot = hash & (index->symbol_table.size () - 1);
3407 step = ((hash * 17) & (index->symbol_table.size () - 1)) | 1;
559a7a62 3408 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
9291a0cd
TT
3409
3410 for (;;)
3411 {
9291a0cd 3412 const char *str;
f00a2de2
PA
3413
3414 const auto &bucket = index->symbol_table[slot];
3415 if (bucket.name == 0 && bucket.vec == 0)
109483d9 3416 return false;
9291a0cd 3417
f00a2de2 3418 str = index->constant_pool + MAYBE_SWAP (bucket.name);
559a7a62 3419 if (!cmp (name, str))
9291a0cd
TT
3420 {
3421 *vec_out = (offset_type *) (index->constant_pool
f00a2de2 3422 + MAYBE_SWAP (bucket.vec));
109483d9 3423 return true;
9291a0cd
TT
3424 }
3425
f00a2de2 3426 slot = (slot + step) & (index->symbol_table.size () - 1);
9291a0cd
TT
3427 }
3428}
3429
2ec9a5e0
TT
3430/* A helper function that reads the .gdb_index from SECTION and fills
3431 in MAP. FILENAME is the name of the file containing the section;
3432 it is used for error reporting. DEPRECATED_OK is nonzero if it is
3433 ok to use deprecated sections.
3434
3435 CU_LIST, CU_LIST_ELEMENTS, TYPES_LIST, and TYPES_LIST_ELEMENTS are
3436 out parameters that are filled in with information about the CU and
3437 TU lists in the section.
3438
3439 Returns 1 if all went well, 0 otherwise. */
2fdf6df6 3440
9291a0cd 3441static int
2ec9a5e0
TT
3442read_index_from_section (struct objfile *objfile,
3443 const char *filename,
3444 int deprecated_ok,
3445 struct dwarf2_section_info *section,
3446 struct mapped_index *map,
3447 const gdb_byte **cu_list,
3448 offset_type *cu_list_elements,
3449 const gdb_byte **types_list,
3450 offset_type *types_list_elements)
9291a0cd 3451{
948f8e3d 3452 const gdb_byte *addr;
2ec9a5e0 3453 offset_type version;
b3b272e1 3454 offset_type *metadata;
1fd400ff 3455 int i;
9291a0cd 3456
2ec9a5e0 3457 if (dwarf2_section_empty_p (section))
9291a0cd 3458 return 0;
82430852
JK
3459
3460 /* Older elfutils strip versions could keep the section in the main
3461 executable while splitting it for the separate debug info file. */
a32a8923 3462 if ((get_section_flags (section) & SEC_HAS_CONTENTS) == 0)
82430852
JK
3463 return 0;
3464
2ec9a5e0 3465 dwarf2_read_section (objfile, section);
9291a0cd 3466
2ec9a5e0 3467 addr = section->buffer;
9291a0cd 3468 /* Version check. */
1fd400ff 3469 version = MAYBE_SWAP (*(offset_type *) addr);
987d643c 3470 /* Versions earlier than 3 emitted every copy of a psymbol. This
a6e293d1 3471 causes the index to behave very poorly for certain requests. Version 3
831adc1f 3472 contained incomplete addrmap. So, it seems better to just ignore such
481860b3 3473 indices. */
831adc1f 3474 if (version < 4)
481860b3
GB
3475 {
3476 static int warning_printed = 0;
3477 if (!warning_printed)
3478 {
3479 warning (_("Skipping obsolete .gdb_index section in %s."),
2ec9a5e0 3480 filename);
481860b3
GB
3481 warning_printed = 1;
3482 }
3483 return 0;
3484 }
3485 /* Index version 4 uses a different hash function than index version
3486 5 and later.
3487
3488 Versions earlier than 6 did not emit psymbols for inlined
3489 functions. Using these files will cause GDB not to be able to
3490 set breakpoints on inlined functions by name, so we ignore these
e615022a
DE
3491 indices unless the user has done
3492 "set use-deprecated-index-sections on". */
2ec9a5e0 3493 if (version < 6 && !deprecated_ok)
481860b3
GB
3494 {
3495 static int warning_printed = 0;
3496 if (!warning_printed)
3497 {
e615022a
DE
3498 warning (_("\
3499Skipping deprecated .gdb_index section in %s.\n\
3500Do \"set use-deprecated-index-sections on\" before the file is read\n\
3501to use the section anyway."),
2ec9a5e0 3502 filename);
481860b3
GB
3503 warning_printed = 1;
3504 }
3505 return 0;
3506 }
796a7ff8 3507 /* Version 7 indices generated by gold refer to the CU for a symbol instead
8943b874
DE
3508 of the TU (for symbols coming from TUs),
3509 http://sourceware.org/bugzilla/show_bug.cgi?id=15021.
3510 Plus gold-generated indices can have duplicate entries for global symbols,
3511 http://sourceware.org/bugzilla/show_bug.cgi?id=15646.
3512 These are just performance bugs, and we can't distinguish gdb-generated
3513 indices from gold-generated ones, so issue no warning here. */
796a7ff8 3514
481860b3 3515 /* Indexes with higher version than the one supported by GDB may be no
594e8718 3516 longer backward compatible. */
796a7ff8 3517 if (version > 8)
594e8718 3518 return 0;
9291a0cd 3519
559a7a62 3520 map->version = version;
2ec9a5e0 3521 map->total_size = section->size;
9291a0cd
TT
3522
3523 metadata = (offset_type *) (addr + sizeof (offset_type));
1fd400ff
TT
3524
3525 i = 0;
2ec9a5e0
TT
3526 *cu_list = addr + MAYBE_SWAP (metadata[i]);
3527 *cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
3528 / 8);
1fd400ff
TT
3529 ++i;
3530
2ec9a5e0
TT
3531 *types_list = addr + MAYBE_SWAP (metadata[i]);
3532 *types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
3533 - MAYBE_SWAP (metadata[i]))
3534 / 8);
987d643c 3535 ++i;
1fd400ff 3536
f00a2de2
PA
3537 const gdb_byte *address_table = addr + MAYBE_SWAP (metadata[i]);
3538 const gdb_byte *address_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
3539 map->address_table
3540 = gdb::array_view<const gdb_byte> (address_table, address_table_end);
1fd400ff
TT
3541 ++i;
3542
f00a2de2
PA
3543 const gdb_byte *symbol_table = addr + MAYBE_SWAP (metadata[i]);
3544 const gdb_byte *symbol_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
3545 map->symbol_table
3546 = gdb::array_view<mapped_index::symbol_table_slot>
3547 ((mapped_index::symbol_table_slot *) symbol_table,
3548 (mapped_index::symbol_table_slot *) symbol_table_end);
9291a0cd 3549
f00a2de2 3550 ++i;
f9d83a0b 3551 map->constant_pool = (char *) (addr + MAYBE_SWAP (metadata[i]));
1fd400ff 3552
2ec9a5e0
TT
3553 return 1;
3554}
3555
927aa2e7 3556/* Read .gdb_index. If everything went ok, initialize the "quick"
2ec9a5e0
TT
3557 elements of all the CUs and return 1. Otherwise, return 0. */
3558
3559static int
3560dwarf2_read_index (struct objfile *objfile)
3561{
3562 struct mapped_index local_map, *map;
3563 const gdb_byte *cu_list, *types_list, *dwz_list = NULL;
3564 offset_type cu_list_elements, types_list_elements, dwz_list_elements = 0;
4db1a1dc 3565 struct dwz_file *dwz;
ed2dc618
SM
3566 struct dwarf2_per_objfile *dwarf2_per_objfile
3567 = get_dwarf2_per_objfile (objfile);
2ec9a5e0 3568
4262abfb 3569 if (!read_index_from_section (objfile, objfile_name (objfile),
2ec9a5e0
TT
3570 use_deprecated_index_sections,
3571 &dwarf2_per_objfile->gdb_index, &local_map,
3572 &cu_list, &cu_list_elements,
3573 &types_list, &types_list_elements))
3574 return 0;
3575
0fefef59 3576 /* Don't use the index if it's empty. */
f00a2de2 3577 if (local_map.symbol_table.empty ())
0fefef59
DE
3578 return 0;
3579
2ec9a5e0
TT
3580 /* If there is a .dwz file, read it so we can get its CU list as
3581 well. */
ed2dc618 3582 dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
4db1a1dc 3583 if (dwz != NULL)
2ec9a5e0 3584 {
2ec9a5e0
TT
3585 struct mapped_index dwz_map;
3586 const gdb_byte *dwz_types_ignore;
3587 offset_type dwz_types_elements_ignore;
3588
3589 if (!read_index_from_section (objfile, bfd_get_filename (dwz->dwz_bfd),
3590 1,
3591 &dwz->gdb_index, &dwz_map,
3592 &dwz_list, &dwz_list_elements,
3593 &dwz_types_ignore,
3594 &dwz_types_elements_ignore))
3595 {
3596 warning (_("could not read '.gdb_index' section from %s; skipping"),
3597 bfd_get_filename (dwz->dwz_bfd));
3598 return 0;
3599 }
3600 }
3601
74a0d9f6
JK
3602 create_cus_from_index (objfile, cu_list, cu_list_elements, dwz_list,
3603 dwz_list_elements);
1fd400ff 3604
8b70b953
TT
3605 if (types_list_elements)
3606 {
3607 struct dwarf2_section_info *section;
3608
3609 /* We can only handle a single .debug_types when we have an
3610 index. */
3611 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) != 1)
3612 return 0;
3613
3614 section = VEC_index (dwarf2_section_info_def,
3615 dwarf2_per_objfile->types, 0);
3616
74a0d9f6
JK
3617 create_signatured_type_table_from_index (objfile, section, types_list,
3618 types_list_elements);
8b70b953 3619 }
9291a0cd 3620
ed2dc618 3621 create_addrmap_from_index (dwarf2_per_objfile, &local_map);
2ec9a5e0 3622
8d749320 3623 map = XOBNEW (&objfile->objfile_obstack, struct mapped_index);
3f563c84 3624 map = new (map) mapped_index ();
2ec9a5e0 3625 *map = local_map;
9291a0cd
TT
3626
3627 dwarf2_per_objfile->index_table = map;
3628 dwarf2_per_objfile->using_index = 1;
7b9f3c50
DE
3629 dwarf2_per_objfile->quick_file_names_table =
3630 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
9291a0cd
TT
3631
3632 return 1;
3633}
3634
dee91e82 3635/* die_reader_func for dw2_get_file_names. */
2fdf6df6 3636
dee91e82
DE
3637static void
3638dw2_get_file_names_reader (const struct die_reader_specs *reader,
d521ce57 3639 const gdb_byte *info_ptr,
dee91e82
DE
3640 struct die_info *comp_unit_die,
3641 int has_children,
3642 void *data)
9291a0cd 3643{
dee91e82 3644 struct dwarf2_cu *cu = reader->cu;
ed2dc618 3645 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
518817b3
SM
3646 struct dwarf2_per_objfile *dwarf2_per_objfile
3647 = cu->per_cu->dwarf2_per_objfile;
dee91e82 3648 struct objfile *objfile = dwarf2_per_objfile->objfile;
094b34ac 3649 struct dwarf2_per_cu_data *lh_cu;
9291a0cd 3650 struct attribute *attr;
dee91e82 3651 int i;
7b9f3c50
DE
3652 void **slot;
3653 struct quick_file_names *qfn;
9291a0cd 3654
0186c6a7
DE
3655 gdb_assert (! this_cu->is_debug_types);
3656
07261596
TT
3657 /* Our callers never want to match partial units -- instead they
3658 will match the enclosing full CU. */
3659 if (comp_unit_die->tag == DW_TAG_partial_unit)
3660 {
3661 this_cu->v.quick->no_file_data = 1;
3662 return;
3663 }
3664
0186c6a7 3665 lh_cu = this_cu;
7b9f3c50 3666 slot = NULL;
dee91e82 3667
fff8551c 3668 line_header_up lh;
9c541725 3669 sect_offset line_offset {};
fff8551c 3670
dee91e82 3671 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
9291a0cd
TT
3672 if (attr)
3673 {
7b9f3c50
DE
3674 struct quick_file_names find_entry;
3675
9c541725 3676 line_offset = (sect_offset) DW_UNSND (attr);
7b9f3c50
DE
3677
3678 /* We may have already read in this line header (TU line header sharing).
3679 If we have we're done. */
094b34ac 3680 find_entry.hash.dwo_unit = cu->dwo_unit;
9c541725 3681 find_entry.hash.line_sect_off = line_offset;
7b9f3c50
DE
3682 slot = htab_find_slot (dwarf2_per_objfile->quick_file_names_table,
3683 &find_entry, INSERT);
3684 if (*slot != NULL)
3685 {
9a3c8263 3686 lh_cu->v.quick->file_names = (struct quick_file_names *) *slot;
dee91e82 3687 return;
7b9f3c50
DE
3688 }
3689
3019eac3 3690 lh = dwarf_decode_line_header (line_offset, cu);
9291a0cd
TT
3691 }
3692 if (lh == NULL)
3693 {
094b34ac 3694 lh_cu->v.quick->no_file_data = 1;
dee91e82 3695 return;
9291a0cd
TT
3696 }
3697
8d749320 3698 qfn = XOBNEW (&objfile->objfile_obstack, struct quick_file_names);
094b34ac 3699 qfn->hash.dwo_unit = cu->dwo_unit;
9c541725 3700 qfn->hash.line_sect_off = line_offset;
7b9f3c50
DE
3701 gdb_assert (slot != NULL);
3702 *slot = qfn;
9291a0cd 3703
d721ba37 3704 file_and_directory fnd = find_file_and_directory (comp_unit_die, cu);
9291a0cd 3705
fff8551c 3706 qfn->num_file_names = lh->file_names.size ();
8d749320 3707 qfn->file_names =
fff8551c
PA
3708 XOBNEWVEC (&objfile->objfile_obstack, const char *, lh->file_names.size ());
3709 for (i = 0; i < lh->file_names.size (); ++i)
3710 qfn->file_names[i] = file_full_name (i + 1, lh.get (), fnd.comp_dir);
7b9f3c50 3711 qfn->real_names = NULL;
9291a0cd 3712
094b34ac 3713 lh_cu->v.quick->file_names = qfn;
dee91e82
DE
3714}
3715
3716/* A helper for the "quick" functions which attempts to read the line
3717 table for THIS_CU. */
3718
3719static struct quick_file_names *
e4a48d9d 3720dw2_get_file_names (struct dwarf2_per_cu_data *this_cu)
dee91e82 3721{
0186c6a7
DE
3722 /* This should never be called for TUs. */
3723 gdb_assert (! this_cu->is_debug_types);
3724 /* Nor type unit groups. */
3725 gdb_assert (! IS_TYPE_UNIT_GROUP (this_cu));
f4dc4d17 3726
dee91e82
DE
3727 if (this_cu->v.quick->file_names != NULL)
3728 return this_cu->v.quick->file_names;
3729 /* If we know there is no line data, no point in looking again. */
3730 if (this_cu->v.quick->no_file_data)
3731 return NULL;
3732
0186c6a7 3733 init_cutu_and_read_dies_simple (this_cu, dw2_get_file_names_reader, NULL);
dee91e82
DE
3734
3735 if (this_cu->v.quick->no_file_data)
3736 return NULL;
3737 return this_cu->v.quick->file_names;
9291a0cd
TT
3738}
3739
3740/* A helper for the "quick" functions which computes and caches the
7b9f3c50 3741 real path for a given file name from the line table. */
2fdf6df6 3742
9291a0cd 3743static const char *
7b9f3c50
DE
3744dw2_get_real_path (struct objfile *objfile,
3745 struct quick_file_names *qfn, int index)
9291a0cd 3746{
7b9f3c50
DE
3747 if (qfn->real_names == NULL)
3748 qfn->real_names = OBSTACK_CALLOC (&objfile->objfile_obstack,
26f2dc30 3749 qfn->num_file_names, const char *);
9291a0cd 3750
7b9f3c50 3751 if (qfn->real_names[index] == NULL)
14278e1f 3752 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]).release ();
9291a0cd 3753
7b9f3c50 3754 return qfn->real_names[index];
9291a0cd
TT
3755}
3756
3757static struct symtab *
3758dw2_find_last_source_symtab (struct objfile *objfile)
3759{
ed2dc618
SM
3760 struct dwarf2_per_objfile *dwarf2_per_objfile
3761 = get_dwarf2_per_objfile (objfile);
3762 int index = dwarf2_per_objfile->n_comp_units - 1;
3763 dwarf2_per_cu_data *dwarf_cu = dw2_get_cutu (dwarf2_per_objfile, index);
3764 compunit_symtab *cust = dw2_instantiate_symtab (dwarf_cu);
ae2de4f8 3765
43f3e411
DE
3766 if (cust == NULL)
3767 return NULL;
ed2dc618 3768
43f3e411 3769 return compunit_primary_filetab (cust);
9291a0cd
TT
3770}
3771
7b9f3c50
DE
3772/* Traversal function for dw2_forget_cached_source_info. */
3773
3774static int
3775dw2_free_cached_file_names (void **slot, void *info)
9291a0cd 3776{
7b9f3c50 3777 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
9291a0cd 3778
7b9f3c50 3779 if (file_data->real_names)
9291a0cd 3780 {
7b9f3c50 3781 int i;
9291a0cd 3782
7b9f3c50 3783 for (i = 0; i < file_data->num_file_names; ++i)
9291a0cd 3784 {
7b9f3c50
DE
3785 xfree ((void*) file_data->real_names[i]);
3786 file_data->real_names[i] = NULL;
9291a0cd
TT
3787 }
3788 }
7b9f3c50
DE
3789
3790 return 1;
3791}
3792
3793static void
3794dw2_forget_cached_source_info (struct objfile *objfile)
3795{
ed2dc618
SM
3796 struct dwarf2_per_objfile *dwarf2_per_objfile
3797 = get_dwarf2_per_objfile (objfile);
7b9f3c50
DE
3798
3799 htab_traverse_noresize (dwarf2_per_objfile->quick_file_names_table,
3800 dw2_free_cached_file_names, NULL);
9291a0cd
TT
3801}
3802
f8eba3c6
TT
3803/* Helper function for dw2_map_symtabs_matching_filename that expands
3804 the symtabs and calls the iterator. */
3805
3806static int
3807dw2_map_expand_apply (struct objfile *objfile,
3808 struct dwarf2_per_cu_data *per_cu,
f5b95b50 3809 const char *name, const char *real_path,
14bc53a8 3810 gdb::function_view<bool (symtab *)> callback)
f8eba3c6 3811{
43f3e411 3812 struct compunit_symtab *last_made = objfile->compunit_symtabs;
f8eba3c6
TT
3813
3814 /* Don't visit already-expanded CUs. */
43f3e411 3815 if (per_cu->v.quick->compunit_symtab)
f8eba3c6
TT
3816 return 0;
3817
3818 /* This may expand more than one symtab, and we want to iterate over
3819 all of them. */
a0f42c21 3820 dw2_instantiate_symtab (per_cu);
f8eba3c6 3821
14bc53a8
PA
3822 return iterate_over_some_symtabs (name, real_path, objfile->compunit_symtabs,
3823 last_made, callback);
f8eba3c6
TT
3824}
3825
3826/* Implementation of the map_symtabs_matching_filename method. */
3827
14bc53a8
PA
3828static bool
3829dw2_map_symtabs_matching_filename
3830 (struct objfile *objfile, const char *name, const char *real_path,
3831 gdb::function_view<bool (symtab *)> callback)
9291a0cd
TT
3832{
3833 int i;
c011a4f4 3834 const char *name_basename = lbasename (name);
ed2dc618
SM
3835 struct dwarf2_per_objfile *dwarf2_per_objfile
3836 = get_dwarf2_per_objfile (objfile);
ae2de4f8 3837
848e3e78
DE
3838 /* The rule is CUs specify all the files, including those used by
3839 any TU, so there's no need to scan TUs here. */
f4dc4d17 3840
ed2dc618 3841 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
9291a0cd
TT
3842 {
3843 int j;
ed2dc618 3844 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (dwarf2_per_objfile, i);
7b9f3c50 3845 struct quick_file_names *file_data;
9291a0cd 3846
3d7bb9d9 3847 /* We only need to look at symtabs not already expanded. */
43f3e411 3848 if (per_cu->v.quick->compunit_symtab)
9291a0cd
TT
3849 continue;
3850
e4a48d9d 3851 file_data = dw2_get_file_names (per_cu);
7b9f3c50 3852 if (file_data == NULL)
9291a0cd
TT
3853 continue;
3854
7b9f3c50 3855 for (j = 0; j < file_data->num_file_names; ++j)
9291a0cd 3856 {
7b9f3c50 3857 const char *this_name = file_data->file_names[j];
da235a7c 3858 const char *this_real_name;
9291a0cd 3859
af529f8f 3860 if (compare_filenames_for_search (this_name, name))
9291a0cd 3861 {
f5b95b50 3862 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
14bc53a8
PA
3863 callback))
3864 return true;
288e77a7 3865 continue;
4aac40c8 3866 }
9291a0cd 3867
c011a4f4
DE
3868 /* Before we invoke realpath, which can get expensive when many
3869 files are involved, do a quick comparison of the basenames. */
3870 if (! basenames_may_differ
3871 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
3872 continue;
3873
da235a7c
JK
3874 this_real_name = dw2_get_real_path (objfile, file_data, j);
3875 if (compare_filenames_for_search (this_real_name, name))
9291a0cd 3876 {
da235a7c 3877 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
14bc53a8
PA
3878 callback))
3879 return true;
288e77a7 3880 continue;
da235a7c 3881 }
9291a0cd 3882
da235a7c
JK
3883 if (real_path != NULL)
3884 {
af529f8f
JK
3885 gdb_assert (IS_ABSOLUTE_PATH (real_path));
3886 gdb_assert (IS_ABSOLUTE_PATH (name));
7b9f3c50 3887 if (this_real_name != NULL
af529f8f 3888 && FILENAME_CMP (real_path, this_real_name) == 0)
9291a0cd 3889 {
f5b95b50 3890 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
14bc53a8
PA
3891 callback))
3892 return true;
288e77a7 3893 continue;
9291a0cd
TT
3894 }
3895 }
3896 }
3897 }
3898
14bc53a8 3899 return false;
9291a0cd
TT
3900}
3901
da51c347
DE
3902/* Struct used to manage iterating over all CUs looking for a symbol. */
3903
3904struct dw2_symtab_iterator
9291a0cd 3905{
ed2dc618
SM
3906 /* The dwarf2_per_objfile owning the CUs we are iterating on. */
3907 struct dwarf2_per_objfile *dwarf2_per_objfile;
da51c347
DE
3908 /* If non-zero, only look for symbols that match BLOCK_INDEX. */
3909 int want_specific_block;
3910 /* One of GLOBAL_BLOCK or STATIC_BLOCK.
3911 Unused if !WANT_SPECIFIC_BLOCK. */
3912 int block_index;
3913 /* The kind of symbol we're looking for. */
3914 domain_enum domain;
3915 /* The list of CUs from the index entry of the symbol,
3916 or NULL if not found. */
3917 offset_type *vec;
3918 /* The next element in VEC to look at. */
3919 int next;
3920 /* The number of elements in VEC, or zero if there is no match. */
3921 int length;
8943b874
DE
3922 /* Have we seen a global version of the symbol?
3923 If so we can ignore all further global instances.
3924 This is to work around gold/15646, inefficient gold-generated
3925 indices. */
3926 int global_seen;
da51c347 3927};
9291a0cd 3928
da51c347
DE
3929/* Initialize the index symtab iterator ITER.
3930 If WANT_SPECIFIC_BLOCK is non-zero, only look for symbols
3931 in block BLOCK_INDEX. Otherwise BLOCK_INDEX is ignored. */
2fdf6df6 3932
9291a0cd 3933static void
da51c347 3934dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
ed2dc618 3935 struct dwarf2_per_objfile *dwarf2_per_objfile,
da51c347
DE
3936 int want_specific_block,
3937 int block_index,
3938 domain_enum domain,
3939 const char *name)
3940{
ed2dc618 3941 iter->dwarf2_per_objfile = dwarf2_per_objfile;
da51c347
DE
3942 iter->want_specific_block = want_specific_block;
3943 iter->block_index = block_index;
3944 iter->domain = domain;
3945 iter->next = 0;
8943b874 3946 iter->global_seen = 0;
da51c347 3947
ed2dc618
SM
3948 mapped_index *index = dwarf2_per_objfile->index_table;
3949
3950 /* index is NULL if OBJF_READNOW. */
3951 if (index != NULL && find_slot_in_mapped_hash (index, name, &iter->vec))
da51c347
DE
3952 iter->length = MAYBE_SWAP (*iter->vec);
3953 else
3954 {
3955 iter->vec = NULL;
3956 iter->length = 0;
3957 }
3958}
3959
3960/* Return the next matching CU or NULL if there are no more. */
3961
3962static struct dwarf2_per_cu_data *
3963dw2_symtab_iter_next (struct dw2_symtab_iterator *iter)
3964{
ed2dc618
SM
3965 struct dwarf2_per_objfile *dwarf2_per_objfile = iter->dwarf2_per_objfile;
3966
da51c347
DE
3967 for ( ; iter->next < iter->length; ++iter->next)
3968 {
3969 offset_type cu_index_and_attrs =
3970 MAYBE_SWAP (iter->vec[iter->next + 1]);
3971 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
3190f0c6 3972 struct dwarf2_per_cu_data *per_cu;
da51c347
DE
3973 int want_static = iter->block_index != GLOBAL_BLOCK;
3974 /* This value is only valid for index versions >= 7. */
3975 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
3976 gdb_index_symbol_kind symbol_kind =
3977 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3978 /* Only check the symbol attributes if they're present.
3979 Indices prior to version 7 don't record them,
3980 and indices >= 7 may elide them for certain symbols
3981 (gold does this). */
3982 int attrs_valid =
ed2dc618 3983 (dwarf2_per_objfile->index_table->version >= 7
da51c347
DE
3984 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
3985
3190f0c6
DE
3986 /* Don't crash on bad data. */
3987 if (cu_index >= (dwarf2_per_objfile->n_comp_units
3988 + dwarf2_per_objfile->n_type_units))
3989 {
3990 complaint (&symfile_complaints,
3991 _(".gdb_index entry has bad CU index"
4262abfb
JK
3992 " [in module %s]"),
3993 objfile_name (dwarf2_per_objfile->objfile));
3190f0c6
DE
3994 continue;
3995 }
3996
ed2dc618 3997 per_cu = dw2_get_cutu (dwarf2_per_objfile, cu_index);
3190f0c6 3998
da51c347 3999 /* Skip if already read in. */
43f3e411 4000 if (per_cu->v.quick->compunit_symtab)
da51c347
DE
4001 continue;
4002
8943b874
DE
4003 /* Check static vs global. */
4004 if (attrs_valid)
4005 {
4006 if (iter->want_specific_block
4007 && want_static != is_static)
4008 continue;
4009 /* Work around gold/15646. */
4010 if (!is_static && iter->global_seen)
4011 continue;
4012 if (!is_static)
4013 iter->global_seen = 1;
4014 }
da51c347
DE
4015
4016 /* Only check the symbol's kind if it has one. */
4017 if (attrs_valid)
4018 {
4019 switch (iter->domain)
4020 {
4021 case VAR_DOMAIN:
4022 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
4023 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
4024 /* Some types are also in VAR_DOMAIN. */
4025 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
4026 continue;
4027 break;
4028 case STRUCT_DOMAIN:
4029 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
4030 continue;
4031 break;
4032 case LABEL_DOMAIN:
4033 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
4034 continue;
4035 break;
4036 default:
4037 break;
4038 }
4039 }
4040
4041 ++iter->next;
4042 return per_cu;
4043 }
4044
4045 return NULL;
4046}
4047
43f3e411 4048static struct compunit_symtab *
da51c347
DE
4049dw2_lookup_symbol (struct objfile *objfile, int block_index,
4050 const char *name, domain_enum domain)
9291a0cd 4051{
43f3e411 4052 struct compunit_symtab *stab_best = NULL;
ed2dc618
SM
4053 struct dwarf2_per_objfile *dwarf2_per_objfile
4054 = get_dwarf2_per_objfile (objfile);
9291a0cd 4055
b5ec771e
PA
4056 lookup_name_info lookup_name (name, symbol_name_match_type::FULL);
4057
ed2dc618
SM
4058 struct dw2_symtab_iterator iter;
4059 struct dwarf2_per_cu_data *per_cu;
da51c347 4060
ed2dc618 4061 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, 1, block_index, domain, name);
9291a0cd 4062
ed2dc618
SM
4063 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
4064 {
4065 struct symbol *sym, *with_opaque = NULL;
4066 struct compunit_symtab *stab = dw2_instantiate_symtab (per_cu);
4067 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
4068 struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
da51c347 4069
ed2dc618
SM
4070 sym = block_find_symbol (block, name, domain,
4071 block_find_non_opaque_type_preferred,
4072 &with_opaque);
b2e2f908 4073
ed2dc618
SM
4074 /* Some caution must be observed with overloaded functions
4075 and methods, since the index will not contain any overload
4076 information (but NAME might contain it). */
da51c347 4077
ed2dc618
SM
4078 if (sym != NULL
4079 && SYMBOL_MATCHES_SEARCH_NAME (sym, lookup_name))
4080 return stab;
4081 if (with_opaque != NULL
4082 && SYMBOL_MATCHES_SEARCH_NAME (with_opaque, lookup_name))
4083 stab_best = stab;
da51c347 4084
ed2dc618 4085 /* Keep looking through other CUs. */
9291a0cd 4086 }
9291a0cd 4087
da51c347 4088 return stab_best;
9291a0cd
TT
4089}
4090
4091static void
4092dw2_print_stats (struct objfile *objfile)
4093{
ed2dc618
SM
4094 struct dwarf2_per_objfile *dwarf2_per_objfile
4095 = get_dwarf2_per_objfile (objfile);
4096 int total = dwarf2_per_objfile->n_comp_units + dwarf2_per_objfile->n_type_units;
4097 int count = 0;
9291a0cd 4098
ed2dc618 4099 for (int i = 0; i < total; ++i)
9291a0cd 4100 {
ed2dc618 4101 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (dwarf2_per_objfile, i);
9291a0cd 4102
43f3e411 4103 if (!per_cu->v.quick->compunit_symtab)
9291a0cd
TT
4104 ++count;
4105 }
e4a48d9d 4106 printf_filtered (_(" Number of read CUs: %d\n"), total - count);
9291a0cd
TT
4107 printf_filtered (_(" Number of unread CUs: %d\n"), count);
4108}
4109
779bd270
DE
4110/* This dumps minimal information about the index.
4111 It is called via "mt print objfiles".
4112 One use is to verify .gdb_index has been loaded by the
4113 gdb.dwarf2/gdb-index.exp testcase. */
4114
9291a0cd
TT
4115static void
4116dw2_dump (struct objfile *objfile)
4117{
ed2dc618
SM
4118 struct dwarf2_per_objfile *dwarf2_per_objfile
4119 = get_dwarf2_per_objfile (objfile);
4120
779bd270
DE
4121 gdb_assert (dwarf2_per_objfile->using_index);
4122 printf_filtered (".gdb_index:");
4123 if (dwarf2_per_objfile->index_table != NULL)
4124 {
4125 printf_filtered (" version %d\n",
4126 dwarf2_per_objfile->index_table->version);
4127 }
4128 else
4129 printf_filtered (" faked for \"readnow\"\n");
4130 printf_filtered ("\n");
9291a0cd
TT
4131}
4132
4133static void
3189cb12
DE
4134dw2_relocate (struct objfile *objfile,
4135 const struct section_offsets *new_offsets,
4136 const struct section_offsets *delta)
9291a0cd
TT
4137{
4138 /* There's nothing to relocate here. */
4139}
4140
4141static void
4142dw2_expand_symtabs_for_function (struct objfile *objfile,
4143 const char *func_name)
4144{
ed2dc618
SM
4145 struct dwarf2_per_objfile *dwarf2_per_objfile
4146 = get_dwarf2_per_objfile (objfile);
da51c347 4147
ed2dc618
SM
4148 struct dw2_symtab_iterator iter;
4149 struct dwarf2_per_cu_data *per_cu;
da51c347 4150
ed2dc618
SM
4151 /* Note: It doesn't matter what we pass for block_index here. */
4152 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, 0, GLOBAL_BLOCK, VAR_DOMAIN,
4153 func_name);
da51c347 4154
ed2dc618
SM
4155 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
4156 dw2_instantiate_symtab (per_cu);
da51c347 4157
9291a0cd
TT
4158}
4159
4160static void
4161dw2_expand_all_symtabs (struct objfile *objfile)
4162{
ed2dc618
SM
4163 struct dwarf2_per_objfile *dwarf2_per_objfile
4164 = get_dwarf2_per_objfile (objfile);
4165 int total_units = (dwarf2_per_objfile->n_comp_units
4166 + dwarf2_per_objfile->n_type_units);
9291a0cd 4167
ed2dc618 4168 for (int i = 0; i < total_units; ++i)
9291a0cd 4169 {
ed2dc618
SM
4170 struct dwarf2_per_cu_data *per_cu
4171 = dw2_get_cutu (dwarf2_per_objfile, i);
9291a0cd 4172
a0f42c21 4173 dw2_instantiate_symtab (per_cu);
9291a0cd
TT
4174 }
4175}
4176
4177static void
652a8996
JK
4178dw2_expand_symtabs_with_fullname (struct objfile *objfile,
4179 const char *fullname)
9291a0cd 4180{
ed2dc618
SM
4181 struct dwarf2_per_objfile *dwarf2_per_objfile
4182 = get_dwarf2_per_objfile (objfile);
d4637a04
DE
4183
4184 /* We don't need to consider type units here.
4185 This is only called for examining code, e.g. expand_line_sal.
4186 There can be an order of magnitude (or more) more type units
4187 than comp units, and we avoid them if we can. */
4188
ed2dc618 4189 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
9291a0cd
TT
4190 {
4191 int j;
ed2dc618 4192 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (dwarf2_per_objfile, i);
7b9f3c50 4193 struct quick_file_names *file_data;
9291a0cd 4194
3d7bb9d9 4195 /* We only need to look at symtabs not already expanded. */
43f3e411 4196 if (per_cu->v.quick->compunit_symtab)
9291a0cd
TT
4197 continue;
4198
e4a48d9d 4199 file_data = dw2_get_file_names (per_cu);
7b9f3c50 4200 if (file_data == NULL)
9291a0cd
TT
4201 continue;
4202
7b9f3c50 4203 for (j = 0; j < file_data->num_file_names; ++j)
9291a0cd 4204 {
652a8996
JK
4205 const char *this_fullname = file_data->file_names[j];
4206
4207 if (filename_cmp (this_fullname, fullname) == 0)
9291a0cd 4208 {
a0f42c21 4209 dw2_instantiate_symtab (per_cu);
9291a0cd
TT
4210 break;
4211 }
4212 }
4213 }
4214}
4215
9291a0cd 4216static void
ade7ed9e 4217dw2_map_matching_symbols (struct objfile *objfile,
fe978cb0 4218 const char * name, domain_enum domain,
ade7ed9e 4219 int global,
40658b94
PH
4220 int (*callback) (struct block *,
4221 struct symbol *, void *),
b5ec771e 4222 void *data, symbol_name_match_type match,
2edb89d3 4223 symbol_compare_ftype *ordered_compare)
9291a0cd 4224{
40658b94 4225 /* Currently unimplemented; used for Ada. The function can be called if the
a9e6a4bb
JK
4226 current language is Ada for a non-Ada objfile using GNU index. As Ada
4227 does not look for non-Ada symbols this function should just return. */
9291a0cd
TT
4228}
4229
b5ec771e
PA
4230/* Symbol name matcher for .gdb_index names.
4231
4232 Symbol names in .gdb_index have a few particularities:
4233
4234 - There's no indication of which is the language of each symbol.
4235
4236 Since each language has its own symbol name matching algorithm,
4237 and we don't know which language is the right one, we must match
3f563c84
PA
4238 each symbol against all languages. This would be a potential
4239 performance problem if it were not mitigated by the
4240 mapped_index::name_components lookup table, which significantly
4241 reduces the number of times we need to call into this matcher,
4242 making it a non-issue.
b5ec771e
PA
4243
4244 - Symbol names in the index have no overload (parameter)
4245 information. I.e., in C++, "foo(int)" and "foo(long)" both
4246 appear as "foo" in the index, for example.
4247
4248 This means that the lookup names passed to the symbol name
4249 matcher functions must have no parameter information either
4250 because (e.g.) symbol search name "foo" does not match
4251 lookup-name "foo(int)" [while swapping search name for lookup
4252 name would match].
4253*/
4254class gdb_index_symbol_name_matcher
4255{
4256public:
4257 /* Prepares the vector of comparison functions for LOOKUP_NAME. */
4258 gdb_index_symbol_name_matcher (const lookup_name_info &lookup_name);
4259
4260 /* Walk all the matcher routines and match SYMBOL_NAME against them.
4261 Returns true if any matcher matches. */
4262 bool matches (const char *symbol_name);
4263
4264private:
4265 /* A reference to the lookup name we're matching against. */
4266 const lookup_name_info &m_lookup_name;
4267
4268 /* A vector holding all the different symbol name matchers, for all
4269 languages. */
4270 std::vector<symbol_name_matcher_ftype *> m_symbol_name_matcher_funcs;
4271};
4272
4273gdb_index_symbol_name_matcher::gdb_index_symbol_name_matcher
4274 (const lookup_name_info &lookup_name)
4275 : m_lookup_name (lookup_name)
4276{
4277 /* Prepare the vector of comparison functions upfront, to avoid
4278 doing the same work for each symbol. Care is taken to avoid
4279 matching with the same matcher more than once if/when multiple
4280 languages use the same matcher function. */
4281 auto &matchers = m_symbol_name_matcher_funcs;
4282 matchers.reserve (nr_languages);
4283
4284 matchers.push_back (default_symbol_name_matcher);
4285
4286 for (int i = 0; i < nr_languages; i++)
4287 {
4288 const language_defn *lang = language_def ((enum language) i);
c63d3e8d 4289 symbol_name_matcher_ftype *name_matcher
618daa93 4290 = get_symbol_name_matcher (lang, m_lookup_name);
c63d3e8d
PA
4291
4292 /* Don't insert the same comparison routine more than once.
4293 Note that we do this linear walk instead of a seemingly
4294 cheaper sorted insert, or use a std::set or something like
4295 that, because relative order of function addresses is not
4296 stable. This is not a problem in practice because the number
4297 of supported languages is low, and the cost here is tiny
4298 compared to the number of searches we'll do afterwards using
4299 this object. */
4300 if (name_matcher != default_symbol_name_matcher
4301 && (std::find (matchers.begin (), matchers.end (), name_matcher)
4302 == matchers.end ()))
4303 matchers.push_back (name_matcher);
b5ec771e
PA
4304 }
4305}
4306
4307bool
4308gdb_index_symbol_name_matcher::matches (const char *symbol_name)
4309{
4310 for (auto matches_name : m_symbol_name_matcher_funcs)
4311 if (matches_name (symbol_name, m_lookup_name, NULL))
4312 return true;
4313
4314 return false;
4315}
4316
e1ef7d7a
PA
4317/* Starting from a search name, return the string that finds the upper
4318 bound of all strings that start with SEARCH_NAME in a sorted name
4319 list. Returns the empty string to indicate that the upper bound is
4320 the end of the list. */
4321
4322static std::string
4323make_sort_after_prefix_name (const char *search_name)
4324{
4325 /* When looking to complete "func", we find the upper bound of all
4326 symbols that start with "func" by looking for where we'd insert
4327 the closest string that would follow "func" in lexicographical
4328 order. Usually, that's "func"-with-last-character-incremented,
4329 i.e. "fund". Mind non-ASCII characters, though. Usually those
4330 will be UTF-8 multi-byte sequences, but we can't be certain.
4331 Especially mind the 0xff character, which is a valid character in
4332 non-UTF-8 source character sets (e.g. Latin1 'ÿ'), and we can't
4333 rule out compilers allowing it in identifiers. Note that
4334 conveniently, strcmp/strcasecmp are specified to compare
4335 characters interpreted as unsigned char. So what we do is treat
4336 the whole string as a base 256 number composed of a sequence of
4337 base 256 "digits" and add 1 to it. I.e., adding 1 to 0xff wraps
4338 to 0, and carries 1 to the following more-significant position.
4339 If the very first character in SEARCH_NAME ends up incremented
4340 and carries/overflows, then the upper bound is the end of the
4341 list. The string after the empty string is also the empty
4342 string.
4343
4344 Some examples of this operation:
4345
4346 SEARCH_NAME => "+1" RESULT
4347
4348 "abc" => "abd"
4349 "ab\xff" => "ac"
4350 "\xff" "a" "\xff" => "\xff" "b"
4351 "\xff" => ""
4352 "\xff\xff" => ""
4353 "" => ""
4354
4355 Then, with these symbols for example:
4356
4357 func
4358 func1
4359 fund
4360
4361 completing "func" looks for symbols between "func" and
4362 "func"-with-last-character-incremented, i.e. "fund" (exclusive),
4363 which finds "func" and "func1", but not "fund".
4364
4365 And with:
4366
4367 funcÿ (Latin1 'ÿ' [0xff])
4368 funcÿ1
4369 fund
4370
4371 completing "funcÿ" looks for symbols between "funcÿ" and "fund"
4372 (exclusive), which finds "funcÿ" and "funcÿ1", but not "fund".
4373
4374 And with:
4375
4376 ÿÿ (Latin1 'ÿ' [0xff])
4377 ÿÿ1
4378
4379 completing "ÿ" or "ÿÿ" looks for symbols between between "ÿÿ" and
4380 the end of the list.
4381 */
4382 std::string after = search_name;
4383 while (!after.empty () && (unsigned char) after.back () == 0xff)
4384 after.pop_back ();
4385 if (!after.empty ())
4386 after.back () = (unsigned char) after.back () + 1;
4387 return after;
4388}
4389
5c58de74 4390/* See declaration. */
61d96d7e 4391
5c58de74
PA
4392std::pair<std::vector<name_component>::const_iterator,
4393 std::vector<name_component>::const_iterator>
44ed8f3e 4394mapped_index_base::find_name_components_bounds
5c58de74 4395 (const lookup_name_info &lookup_name_without_params) const
3f563c84 4396{
5c58de74
PA
4397 auto *name_cmp
4398 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
3f563c84
PA
4399
4400 const char *cplus
c62446b1 4401 = lookup_name_without_params.cplus ().lookup_name ().c_str ();
9291a0cd 4402
3f563c84
PA
4403 /* Comparison function object for lower_bound that matches against a
4404 given symbol name. */
4405 auto lookup_compare_lower = [&] (const name_component &elem,
4406 const char *name)
4407 {
5c58de74 4408 const char *elem_qualified = this->symbol_name_at (elem.idx);
3f563c84
PA
4409 const char *elem_name = elem_qualified + elem.name_offset;
4410 return name_cmp (elem_name, name) < 0;
4411 };
4412
4413 /* Comparison function object for upper_bound that matches against a
4414 given symbol name. */
4415 auto lookup_compare_upper = [&] (const char *name,
4416 const name_component &elem)
4417 {
5c58de74 4418 const char *elem_qualified = this->symbol_name_at (elem.idx);
3f563c84
PA
4419 const char *elem_name = elem_qualified + elem.name_offset;
4420 return name_cmp (name, elem_name) < 0;
4421 };
4422
5c58de74
PA
4423 auto begin = this->name_components.begin ();
4424 auto end = this->name_components.end ();
3f563c84
PA
4425
4426 /* Find the lower bound. */
4427 auto lower = [&] ()
4428 {
5c58de74 4429 if (lookup_name_without_params.completion_mode () && cplus[0] == '\0')
3f563c84
PA
4430 return begin;
4431 else
4432 return std::lower_bound (begin, end, cplus, lookup_compare_lower);
4433 } ();
4434
4435 /* Find the upper bound. */
4436 auto upper = [&] ()
4437 {
5c58de74 4438 if (lookup_name_without_params.completion_mode ())
3f563c84 4439 {
e1ef7d7a
PA
4440 /* In completion mode, we want UPPER to point past all
4441 symbols names that have the same prefix. I.e., with
4442 these symbols, and completing "func":
4443
4444 function << lower bound
4445 function1
4446 other_function << upper bound
4447
4448 We find the upper bound by looking for the insertion
4449 point of "func"-with-last-character-incremented,
4450 i.e. "fund". */
4451 std::string after = make_sort_after_prefix_name (cplus);
4452 if (after.empty ())
3f563c84 4453 return end;
e6b2f5ef
PA
4454 return std::lower_bound (lower, end, after.c_str (),
4455 lookup_compare_lower);
3f563c84
PA
4456 }
4457 else
4458 return std::upper_bound (lower, end, cplus, lookup_compare_upper);
4459 } ();
4460
5c58de74
PA
4461 return {lower, upper};
4462}
4463
4464/* See declaration. */
4465
4466void
44ed8f3e 4467mapped_index_base::build_name_components ()
5c58de74
PA
4468{
4469 if (!this->name_components.empty ())
4470 return;
4471
4472 this->name_components_casing = case_sensitivity;
4473 auto *name_cmp
4474 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
4475
4476 /* The code below only knows how to break apart components of C++
4477 symbol names (and other languages that use '::' as
4478 namespace/module separator). If we add support for wild matching
4479 to some language that uses some other operator (E.g., Ada, Go and
4480 D use '.'), then we'll need to try splitting the symbol name
4481 according to that language too. Note that Ada does support wild
4482 matching, but doesn't currently support .gdb_index. */
44ed8f3e
PA
4483 auto count = this->symbol_name_count ();
4484 for (offset_type idx = 0; idx < count; idx++)
5c58de74 4485 {
44ed8f3e 4486 if (this->symbol_name_slot_invalid (idx))
5c58de74
PA
4487 continue;
4488
4489 const char *name = this->symbol_name_at (idx);
4490
4491 /* Add each name component to the name component table. */
4492 unsigned int previous_len = 0;
4493 for (unsigned int current_len = cp_find_first_component (name);
4494 name[current_len] != '\0';
4495 current_len += cp_find_first_component (name + current_len))
4496 {
4497 gdb_assert (name[current_len] == ':');
4498 this->name_components.push_back ({previous_len, idx});
4499 /* Skip the '::'. */
4500 current_len += 2;
4501 previous_len = current_len;
4502 }
4503 this->name_components.push_back ({previous_len, idx});
4504 }
4505
4506 /* Sort name_components elements by name. */
4507 auto name_comp_compare = [&] (const name_component &left,
4508 const name_component &right)
4509 {
4510 const char *left_qualified = this->symbol_name_at (left.idx);
4511 const char *right_qualified = this->symbol_name_at (right.idx);
4512
4513 const char *left_name = left_qualified + left.name_offset;
4514 const char *right_name = right_qualified + right.name_offset;
4515
4516 return name_cmp (left_name, right_name) < 0;
4517 };
4518
4519 std::sort (this->name_components.begin (),
4520 this->name_components.end (),
4521 name_comp_compare);
4522}
4523
4524/* Helper for dw2_expand_symtabs_matching that works with a
44ed8f3e
PA
4525 mapped_index_base instead of the containing objfile. This is split
4526 to a separate function in order to be able to unit test the
4527 name_components matching using a mock mapped_index_base. For each
5c58de74 4528 symbol name that matches, calls MATCH_CALLBACK, passing it the
44ed8f3e 4529 symbol's index in the mapped_index_base symbol table. */
5c58de74
PA
4530
4531static void
4532dw2_expand_symtabs_matching_symbol
44ed8f3e 4533 (mapped_index_base &index,
5c58de74
PA
4534 const lookup_name_info &lookup_name_in,
4535 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4536 enum search_domain kind,
4537 gdb::function_view<void (offset_type)> match_callback)
4538{
4539 lookup_name_info lookup_name_without_params
4540 = lookup_name_in.make_ignore_params ();
4541 gdb_index_symbol_name_matcher lookup_name_matcher
4542 (lookup_name_without_params);
4543
4544 /* Build the symbol name component sorted vector, if we haven't
4545 yet. */
4546 index.build_name_components ();
4547
4548 auto bounds = index.find_name_components_bounds (lookup_name_without_params);
4549
3f563c84
PA
4550 /* Now for each symbol name in range, check to see if we have a name
4551 match, and if so, call the MATCH_CALLBACK callback. */
4552
4553 /* The same symbol may appear more than once in the range though.
4554 E.g., if we're looking for symbols that complete "w", and we have
4555 a symbol named "w1::w2", we'll find the two name components for
4556 that same symbol in the range. To be sure we only call the
4557 callback once per symbol, we first collect the symbol name
4558 indexes that matched in a temporary vector and ignore
4559 duplicates. */
4560 std::vector<offset_type> matches;
5c58de74 4561 matches.reserve (std::distance (bounds.first, bounds.second));
3f563c84 4562
5c58de74 4563 for (; bounds.first != bounds.second; ++bounds.first)
3f563c84 4564 {
5c58de74 4565 const char *qualified = index.symbol_name_at (bounds.first->idx);
3f563c84
PA
4566
4567 if (!lookup_name_matcher.matches (qualified)
4568 || (symbol_matcher != NULL && !symbol_matcher (qualified)))
9291a0cd
TT
4569 continue;
4570
5c58de74 4571 matches.push_back (bounds.first->idx);
3f563c84
PA
4572 }
4573
4574 std::sort (matches.begin (), matches.end ());
4575
4576 /* Finally call the callback, once per match. */
4577 ULONGEST prev = -1;
4578 for (offset_type idx : matches)
4579 {
4580 if (prev != idx)
4581 {
4582 match_callback (idx);
4583 prev = idx;
4584 }
4585 }
4586
4587 /* Above we use a type wider than idx's for 'prev', since 0 and
4588 (offset_type)-1 are both possible values. */
4589 static_assert (sizeof (prev) > sizeof (offset_type), "");
4590}
4591
c62446b1
PA
4592#if GDB_SELF_TEST
4593
4594namespace selftests { namespace dw2_expand_symtabs_matching {
4595
a3c5fafd
PA
4596/* A mock .gdb_index/.debug_names-like name index table, enough to
4597 exercise dw2_expand_symtabs_matching_symbol, which works with the
4598 mapped_index_base interface. Builds an index from the symbol list
4599 passed as parameter to the constructor. */
4600class mock_mapped_index : public mapped_index_base
c62446b1
PA
4601{
4602public:
a3c5fafd
PA
4603 mock_mapped_index (gdb::array_view<const char *> symbols)
4604 : m_symbol_table (symbols)
c62446b1
PA
4605 {}
4606
a3c5fafd 4607 DISABLE_COPY_AND_ASSIGN (mock_mapped_index);
c62446b1 4608
a3c5fafd
PA
4609 /* Return the number of names in the symbol table. */
4610 virtual size_t symbol_name_count () const
c62446b1 4611 {
a3c5fafd 4612 return m_symbol_table.size ();
c62446b1
PA
4613 }
4614
a3c5fafd
PA
4615 /* Get the name of the symbol at IDX in the symbol table. */
4616 virtual const char *symbol_name_at (offset_type idx) const
4617 {
4618 return m_symbol_table[idx];
4619 }
c62446b1 4620
a3c5fafd
PA
4621private:
4622 gdb::array_view<const char *> m_symbol_table;
c62446b1
PA
4623};
4624
4625/* Convenience function that converts a NULL pointer to a "<null>"
4626 string, to pass to print routines. */
4627
4628static const char *
4629string_or_null (const char *str)
4630{
4631 return str != NULL ? str : "<null>";
4632}
4633
4634/* Check if a lookup_name_info built from
4635 NAME/MATCH_TYPE/COMPLETION_MODE matches the symbols in the mock
4636 index. EXPECTED_LIST is the list of expected matches, in expected
4637 matching order. If no match expected, then an empty list is
4638 specified. Returns true on success. On failure prints a warning
4639 indicating the file:line that failed, and returns false. */
4640
4641static bool
4642check_match (const char *file, int line,
4643 mock_mapped_index &mock_index,
4644 const char *name, symbol_name_match_type match_type,
4645 bool completion_mode,
4646 std::initializer_list<const char *> expected_list)
4647{
4648 lookup_name_info lookup_name (name, match_type, completion_mode);
4649
4650 bool matched = true;
4651
4652 auto mismatch = [&] (const char *expected_str,
4653 const char *got)
4654 {
4655 warning (_("%s:%d: match_type=%s, looking-for=\"%s\", "
4656 "expected=\"%s\", got=\"%s\"\n"),
4657 file, line,
4658 (match_type == symbol_name_match_type::FULL
4659 ? "FULL" : "WILD"),
4660 name, string_or_null (expected_str), string_or_null (got));
4661 matched = false;
4662 };
4663
4664 auto expected_it = expected_list.begin ();
4665 auto expected_end = expected_list.end ();
4666
a3c5fafd 4667 dw2_expand_symtabs_matching_symbol (mock_index, lookup_name,
c62446b1
PA
4668 NULL, ALL_DOMAIN,
4669 [&] (offset_type idx)
4670 {
a3c5fafd 4671 const char *matched_name = mock_index.symbol_name_at (idx);
c62446b1
PA
4672 const char *expected_str
4673 = expected_it == expected_end ? NULL : *expected_it++;
4674
4675 if (expected_str == NULL || strcmp (expected_str, matched_name) != 0)
4676 mismatch (expected_str, matched_name);
4677 });
4678
4679 const char *expected_str
4680 = expected_it == expected_end ? NULL : *expected_it++;
4681 if (expected_str != NULL)
4682 mismatch (expected_str, NULL);
4683
4684 return matched;
4685}
4686
4687/* The symbols added to the mock mapped_index for testing (in
4688 canonical form). */
4689static const char *test_symbols[] = {
4690 "function",
4691 "std::bar",
4692 "std::zfunction",
4693 "std::zfunction2",
4694 "w1::w2",
4695 "ns::foo<char*>",
4696 "ns::foo<int>",
4697 "ns::foo<long>",
a20714ff
PA
4698 "ns2::tmpl<int>::foo2",
4699 "(anonymous namespace)::A::B::C",
c62446b1 4700
e1ef7d7a
PA
4701 /* These are used to check that the increment-last-char in the
4702 matching algorithm for completion doesn't match "t1_fund" when
4703 completing "t1_func". */
4704 "t1_func",
4705 "t1_func1",
4706 "t1_fund",
4707 "t1_fund1",
4708
4709 /* A UTF-8 name with multi-byte sequences to make sure that
4710 cp-name-parser understands this as a single identifier ("função"
4711 is "function" in PT). */
4712 u8"u8função",
4713
4714 /* \377 (0xff) is Latin1 'ÿ'. */
4715 "yfunc\377",
4716
4717 /* \377 (0xff) is Latin1 'ÿ'. */
4718 "\377",
4719 "\377\377123",
4720
c62446b1
PA
4721 /* A name with all sorts of complications. Starts with "z" to make
4722 it easier for the completion tests below. */
4723#define Z_SYM_NAME \
4724 "z::std::tuple<(anonymous namespace)::ui*, std::bar<(anonymous namespace)::ui> >" \
4725 "::tuple<(anonymous namespace)::ui*, " \
4726 "std::default_delete<(anonymous namespace)::ui>, void>"
4727
4728 Z_SYM_NAME
4729};
4730
a3c5fafd
PA
4731/* Returns true if the mapped_index_base::find_name_component_bounds
4732 method finds EXPECTED_SYMS in INDEX when looking for SEARCH_NAME,
4733 in completion mode. */
5c58de74
PA
4734
4735static bool
a3c5fafd 4736check_find_bounds_finds (mapped_index_base &index,
5c58de74
PA
4737 const char *search_name,
4738 gdb::array_view<const char *> expected_syms)
4739{
4740 lookup_name_info lookup_name (search_name,
4741 symbol_name_match_type::FULL, true);
4742
4743 auto bounds = index.find_name_components_bounds (lookup_name);
4744
4745 size_t distance = std::distance (bounds.first, bounds.second);
4746 if (distance != expected_syms.size ())
4747 return false;
4748
4749 for (size_t exp_elem = 0; exp_elem < distance; exp_elem++)
4750 {
4751 auto nc_elem = bounds.first + exp_elem;
4752 const char *qualified = index.symbol_name_at (nc_elem->idx);
4753 if (strcmp (qualified, expected_syms[exp_elem]) != 0)
4754 return false;
4755 }
4756
4757 return true;
4758}
4759
4760/* Test the lower-level mapped_index::find_name_component_bounds
4761 method. */
4762
c62446b1 4763static void
5c58de74
PA
4764test_mapped_index_find_name_component_bounds ()
4765{
4766 mock_mapped_index mock_index (test_symbols);
4767
a3c5fafd 4768 mock_index.build_name_components ();
5c58de74
PA
4769
4770 /* Test the lower-level mapped_index::find_name_component_bounds
4771 method in completion mode. */
4772 {
4773 static const char *expected_syms[] = {
4774 "t1_func",
4775 "t1_func1",
5c58de74
PA
4776 };
4777
a3c5fafd 4778 SELF_CHECK (check_find_bounds_finds (mock_index,
5c58de74
PA
4779 "t1_func", expected_syms));
4780 }
4781
4782 /* Check that the increment-last-char in the name matching algorithm
4783 for completion doesn't get confused with Ansi1 'ÿ' / 0xff. */
4784 {
4785 static const char *expected_syms1[] = {
4786 "\377",
4787 "\377\377123",
4788 };
a3c5fafd 4789 SELF_CHECK (check_find_bounds_finds (mock_index,
5c58de74
PA
4790 "\377", expected_syms1));
4791
4792 static const char *expected_syms2[] = {
4793 "\377\377123",
4794 };
a3c5fafd 4795 SELF_CHECK (check_find_bounds_finds (mock_index,
5c58de74
PA
4796 "\377\377", expected_syms2));
4797 }
4798}
4799
4800/* Test dw2_expand_symtabs_matching_symbol. */
4801
4802static void
4803test_dw2_expand_symtabs_matching_symbol ()
c62446b1
PA
4804{
4805 mock_mapped_index mock_index (test_symbols);
4806
4807 /* We let all tests run until the end even if some fails, for debug
4808 convenience. */
4809 bool any_mismatch = false;
4810
4811 /* Create the expected symbols list (an initializer_list). Needed
4812 because lists have commas, and we need to pass them to CHECK,
4813 which is a macro. */
4814#define EXPECT(...) { __VA_ARGS__ }
4815
4816 /* Wrapper for check_match that passes down the current
4817 __FILE__/__LINE__. */
4818#define CHECK_MATCH(NAME, MATCH_TYPE, COMPLETION_MODE, EXPECTED_LIST) \
4819 any_mismatch |= !check_match (__FILE__, __LINE__, \
4820 mock_index, \
4821 NAME, MATCH_TYPE, COMPLETION_MODE, \
4822 EXPECTED_LIST)
4823
4824 /* Identity checks. */
4825 for (const char *sym : test_symbols)
4826 {
4827 /* Should be able to match all existing symbols. */
4828 CHECK_MATCH (sym, symbol_name_match_type::FULL, false,
4829 EXPECT (sym));
4830
4831 /* Should be able to match all existing symbols with
4832 parameters. */
4833 std::string with_params = std::string (sym) + "(int)";
4834 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4835 EXPECT (sym));
4836
4837 /* Should be able to match all existing symbols with
4838 parameters and qualifiers. */
4839 with_params = std::string (sym) + " ( int ) const";
4840 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4841 EXPECT (sym));
4842
4843 /* This should really find sym, but cp-name-parser.y doesn't
4844 know about lvalue/rvalue qualifiers yet. */
4845 with_params = std::string (sym) + " ( int ) &&";
4846 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4847 {});
4848 }
4849
e1ef7d7a
PA
4850 /* Check that the name matching algorithm for completion doesn't get
4851 confused with Latin1 'ÿ' / 0xff. */
4852 {
4853 static const char str[] = "\377";
4854 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4855 EXPECT ("\377", "\377\377123"));
4856 }
4857
4858 /* Check that the increment-last-char in the matching algorithm for
4859 completion doesn't match "t1_fund" when completing "t1_func". */
4860 {
4861 static const char str[] = "t1_func";
4862 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4863 EXPECT ("t1_func", "t1_func1"));
4864 }
4865
c62446b1
PA
4866 /* Check that completion mode works at each prefix of the expected
4867 symbol name. */
4868 {
4869 static const char str[] = "function(int)";
4870 size_t len = strlen (str);
4871 std::string lookup;
4872
4873 for (size_t i = 1; i < len; i++)
4874 {
4875 lookup.assign (str, i);
4876 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4877 EXPECT ("function"));
4878 }
4879 }
4880
4881 /* While "w" is a prefix of both components, the match function
4882 should still only be called once. */
4883 {
4884 CHECK_MATCH ("w", symbol_name_match_type::FULL, true,
4885 EXPECT ("w1::w2"));
a20714ff
PA
4886 CHECK_MATCH ("w", symbol_name_match_type::WILD, true,
4887 EXPECT ("w1::w2"));
c62446b1
PA
4888 }
4889
4890 /* Same, with a "complicated" symbol. */
4891 {
4892 static const char str[] = Z_SYM_NAME;
4893 size_t len = strlen (str);
4894 std::string lookup;
4895
4896 for (size_t i = 1; i < len; i++)
4897 {
4898 lookup.assign (str, i);
4899 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4900 EXPECT (Z_SYM_NAME));
4901 }
4902 }
4903
4904 /* In FULL mode, an incomplete symbol doesn't match. */
4905 {
4906 CHECK_MATCH ("std::zfunction(int", symbol_name_match_type::FULL, false,
4907 {});
4908 }
4909
4910 /* A complete symbol with parameters matches any overload, since the
4911 index has no overload info. */
4912 {
4913 CHECK_MATCH ("std::zfunction(int)", symbol_name_match_type::FULL, true,
4914 EXPECT ("std::zfunction", "std::zfunction2"));
a20714ff
PA
4915 CHECK_MATCH ("zfunction(int)", symbol_name_match_type::WILD, true,
4916 EXPECT ("std::zfunction", "std::zfunction2"));
4917 CHECK_MATCH ("zfunc", symbol_name_match_type::WILD, true,
4918 EXPECT ("std::zfunction", "std::zfunction2"));
c62446b1
PA
4919 }
4920
4921 /* Check that whitespace is ignored appropriately. A symbol with a
4922 template argument list. */
4923 {
4924 static const char expected[] = "ns::foo<int>";
4925 CHECK_MATCH ("ns :: foo < int > ", symbol_name_match_type::FULL, false,
4926 EXPECT (expected));
a20714ff
PA
4927 CHECK_MATCH ("foo < int > ", symbol_name_match_type::WILD, false,
4928 EXPECT (expected));
c62446b1
PA
4929 }
4930
4931 /* Check that whitespace is ignored appropriately. A symbol with a
4932 template argument list that includes a pointer. */
4933 {
4934 static const char expected[] = "ns::foo<char*>";
4935 /* Try both completion and non-completion modes. */
4936 static const bool completion_mode[2] = {false, true};
4937 for (size_t i = 0; i < 2; i++)
4938 {
4939 CHECK_MATCH ("ns :: foo < char * >", symbol_name_match_type::FULL,
4940 completion_mode[i], EXPECT (expected));
a20714ff
PA
4941 CHECK_MATCH ("foo < char * >", symbol_name_match_type::WILD,
4942 completion_mode[i], EXPECT (expected));
c62446b1
PA
4943
4944 CHECK_MATCH ("ns :: foo < char * > (int)", symbol_name_match_type::FULL,
4945 completion_mode[i], EXPECT (expected));
a20714ff
PA
4946 CHECK_MATCH ("foo < char * > (int)", symbol_name_match_type::WILD,
4947 completion_mode[i], EXPECT (expected));
c62446b1
PA
4948 }
4949 }
4950
4951 {
4952 /* Check method qualifiers are ignored. */
4953 static const char expected[] = "ns::foo<char*>";
4954 CHECK_MATCH ("ns :: foo < char * > ( int ) const",
4955 symbol_name_match_type::FULL, true, EXPECT (expected));
4956 CHECK_MATCH ("ns :: foo < char * > ( int ) &&",
4957 symbol_name_match_type::FULL, true, EXPECT (expected));
a20714ff
PA
4958 CHECK_MATCH ("foo < char * > ( int ) const",
4959 symbol_name_match_type::WILD, true, EXPECT (expected));
4960 CHECK_MATCH ("foo < char * > ( int ) &&",
4961 symbol_name_match_type::WILD, true, EXPECT (expected));
c62446b1
PA
4962 }
4963
4964 /* Test lookup names that don't match anything. */
4965 {
a20714ff
PA
4966 CHECK_MATCH ("bar2", symbol_name_match_type::WILD, false,
4967 {});
4968
c62446b1
PA
4969 CHECK_MATCH ("doesntexist", symbol_name_match_type::FULL, false,
4970 {});
4971 }
4972
a20714ff
PA
4973 /* Some wild matching tests, exercising "(anonymous namespace)",
4974 which should not be confused with a parameter list. */
4975 {
4976 static const char *syms[] = {
4977 "A::B::C",
4978 "B::C",
4979 "C",
4980 "A :: B :: C ( int )",
4981 "B :: C ( int )",
4982 "C ( int )",
4983 };
4984
4985 for (const char *s : syms)
4986 {
4987 CHECK_MATCH (s, symbol_name_match_type::WILD, false,
4988 EXPECT ("(anonymous namespace)::A::B::C"));
4989 }
4990 }
4991
4992 {
4993 static const char expected[] = "ns2::tmpl<int>::foo2";
4994 CHECK_MATCH ("tmp", symbol_name_match_type::WILD, true,
4995 EXPECT (expected));
4996 CHECK_MATCH ("tmpl<", symbol_name_match_type::WILD, true,
4997 EXPECT (expected));
4998 }
4999
c62446b1
PA
5000 SELF_CHECK (!any_mismatch);
5001
5002#undef EXPECT
5003#undef CHECK_MATCH
5004}
5005
5c58de74
PA
5006static void
5007run_test ()
5008{
5009 test_mapped_index_find_name_component_bounds ();
5010 test_dw2_expand_symtabs_matching_symbol ();
5011}
5012
c62446b1
PA
5013}} // namespace selftests::dw2_expand_symtabs_matching
5014
5015#endif /* GDB_SELF_TEST */
5016
4b514bc8
JK
5017/* If FILE_MATCHER is NULL or if PER_CU has
5018 dwarf2_per_cu_quick_data::MARK set (see
5019 dw_expand_symtabs_matching_file_matcher), expand the CU and call
5020 EXPANSION_NOTIFY on it. */
5021
5022static void
5023dw2_expand_symtabs_matching_one
5024 (struct dwarf2_per_cu_data *per_cu,
5025 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
5026 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify)
5027{
5028 if (file_matcher == NULL || per_cu->v.quick->mark)
5029 {
5030 bool symtab_was_null
5031 = (per_cu->v.quick->compunit_symtab == NULL);
5032
5033 dw2_instantiate_symtab (per_cu);
5034
5035 if (expansion_notify != NULL
5036 && symtab_was_null
5037 && per_cu->v.quick->compunit_symtab != NULL)
5038 expansion_notify (per_cu->v.quick->compunit_symtab);
5039 }
5040}
5041
3f563c84
PA
5042/* Helper for dw2_expand_matching symtabs. Called on each symbol
5043 matched, to expand corresponding CUs that were marked. IDX is the
5044 index of the symbol name that matched. */
5045
5046static void
5047dw2_expand_marked_cus
ed2dc618 5048 (struct dwarf2_per_objfile *dwarf2_per_objfile, offset_type idx,
3f563c84
PA
5049 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
5050 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
5051 search_domain kind)
5052{
3f563c84
PA
5053 offset_type *vec, vec_len, vec_idx;
5054 bool global_seen = false;
ed2dc618 5055 mapped_index &index = *dwarf2_per_objfile->index_table;
3f563c84 5056
61920122 5057 vec = (offset_type *) (index.constant_pool
f00a2de2 5058 + MAYBE_SWAP (index.symbol_table[idx].vec));
61920122
PA
5059 vec_len = MAYBE_SWAP (vec[0]);
5060 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
5061 {
5062 struct dwarf2_per_cu_data *per_cu;
5063 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[vec_idx + 1]);
5064 /* This value is only valid for index versions >= 7. */
5065 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
5066 gdb_index_symbol_kind symbol_kind =
5067 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
5068 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
5069 /* Only check the symbol attributes if they're present.
5070 Indices prior to version 7 don't record them,
5071 and indices >= 7 may elide them for certain symbols
5072 (gold does this). */
5073 int attrs_valid =
5074 (index.version >= 7
5075 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
5076
5077 /* Work around gold/15646. */
5078 if (attrs_valid)
9291a0cd 5079 {
61920122
PA
5080 if (!is_static && global_seen)
5081 continue;
5082 if (!is_static)
5083 global_seen = true;
5084 }
3190f0c6 5085
61920122
PA
5086 /* Only check the symbol's kind if it has one. */
5087 if (attrs_valid)
5088 {
5089 switch (kind)
8943b874 5090 {
61920122
PA
5091 case VARIABLES_DOMAIN:
5092 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
5093 continue;
5094 break;
5095 case FUNCTIONS_DOMAIN:
5096 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
8943b874 5097 continue;
61920122
PA
5098 break;
5099 case TYPES_DOMAIN:
5100 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
5101 continue;
5102 break;
5103 default:
5104 break;
8943b874 5105 }
61920122 5106 }
8943b874 5107
61920122
PA
5108 /* Don't crash on bad data. */
5109 if (cu_index >= (dwarf2_per_objfile->n_comp_units
5110 + dwarf2_per_objfile->n_type_units))
5111 {
5112 complaint (&symfile_complaints,
5113 _(".gdb_index entry has bad CU index"
ed2dc618
SM
5114 " [in module %s]"),
5115 objfile_name (dwarf2_per_objfile->objfile));
61920122
PA
5116 continue;
5117 }
5118
ed2dc618 5119 per_cu = dw2_get_cutu (dwarf2_per_objfile, cu_index);
4b514bc8
JK
5120 dw2_expand_symtabs_matching_one (per_cu, file_matcher,
5121 expansion_notify);
61920122
PA
5122 }
5123}
5124
4b514bc8
JK
5125/* If FILE_MATCHER is non-NULL, set all the
5126 dwarf2_per_cu_quick_data::MARK of the current DWARF2_PER_OBJFILE
5127 that match FILE_MATCHER. */
5128
61920122 5129static void
4b514bc8 5130dw_expand_symtabs_matching_file_matcher
ed2dc618
SM
5131 (struct dwarf2_per_objfile *dwarf2_per_objfile,
5132 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher)
61920122 5133{
4b514bc8 5134 if (file_matcher == NULL)
61920122
PA
5135 return;
5136
4b514bc8
JK
5137 objfile *const objfile = dwarf2_per_objfile->objfile;
5138
5139 htab_up visited_found (htab_create_alloc (10, htab_hash_pointer,
5140 htab_eq_pointer,
5141 NULL, xcalloc, xfree));
5142 htab_up visited_not_found (htab_create_alloc (10, htab_hash_pointer,
61920122
PA
5143 htab_eq_pointer,
5144 NULL, xcalloc, xfree));
61920122 5145
4b514bc8
JK
5146 /* The rule is CUs specify all the files, including those used by
5147 any TU, so there's no need to scan TUs here. */
61920122 5148
927aa2e7
JK
5149 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
5150 {
5151 int j;
ed2dc618 5152 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (dwarf2_per_objfile, i);
927aa2e7
JK
5153 struct quick_file_names *file_data;
5154 void **slot;
5155
5156 QUIT;
5157
5158 per_cu->v.quick->mark = 0;
5159
5160 /* We only need to look at symtabs not already expanded. */
5161 if (per_cu->v.quick->compunit_symtab)
5162 continue;
5163
5164 file_data = dw2_get_file_names (per_cu);
5165 if (file_data == NULL)
5166 continue;
5167
5168 if (htab_find (visited_not_found.get (), file_data) != NULL)
5169 continue;
5170 else if (htab_find (visited_found.get (), file_data) != NULL)
5171 {
5172 per_cu->v.quick->mark = 1;
5173 continue;
5174 }
5175
5176 for (j = 0; j < file_data->num_file_names; ++j)
5177 {
5178 const char *this_real_name;
5179
5180 if (file_matcher (file_data->file_names[j], false))
5181 {
5182 per_cu->v.quick->mark = 1;
5183 break;
5184 }
5185
5186 /* Before we invoke realpath, which can get expensive when many
5187 files are involved, do a quick comparison of the basenames. */
5188 if (!basenames_may_differ
5189 && !file_matcher (lbasename (file_data->file_names[j]),
5190 true))
5191 continue;
5192
5193 this_real_name = dw2_get_real_path (objfile, file_data, j);
5194 if (file_matcher (this_real_name, false))
5195 {
5196 per_cu->v.quick->mark = 1;
5197 break;
5198 }
5199 }
5200
5201 slot = htab_find_slot (per_cu->v.quick->mark
5202 ? visited_found.get ()
5203 : visited_not_found.get (),
5204 file_data, INSERT);
5205 *slot = file_data;
5206 }
5207}
5208
5209static void
5210dw2_expand_symtabs_matching
5211 (struct objfile *objfile,
5212 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
5213 const lookup_name_info &lookup_name,
5214 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
5215 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
5216 enum search_domain kind)
5217{
ed2dc618
SM
5218 struct dwarf2_per_objfile *dwarf2_per_objfile
5219 = get_dwarf2_per_objfile (objfile);
927aa2e7
JK
5220
5221 /* index_table is NULL if OBJF_READNOW. */
5222 if (!dwarf2_per_objfile->index_table)
5223 return;
5224
ed2dc618 5225 dw_expand_symtabs_matching_file_matcher (dwarf2_per_objfile, file_matcher);
927aa2e7
JK
5226
5227 mapped_index &index = *dwarf2_per_objfile->index_table;
5228
5229 dw2_expand_symtabs_matching_symbol (index, lookup_name,
5230 symbol_matcher,
5231 kind, [&] (offset_type idx)
5232 {
ed2dc618 5233 dw2_expand_marked_cus (dwarf2_per_objfile, idx, file_matcher,
927aa2e7
JK
5234 expansion_notify, kind);
5235 });
5236}
5237
5238/* A helper for dw2_find_pc_sect_compunit_symtab which finds the most specific
5239 symtab. */
5240
5241static struct compunit_symtab *
5242recursively_find_pc_sect_compunit_symtab (struct compunit_symtab *cust,
5243 CORE_ADDR pc)
5244{
5245 int i;
5246
5247 if (COMPUNIT_BLOCKVECTOR (cust) != NULL
5248 && blockvector_contains_pc (COMPUNIT_BLOCKVECTOR (cust), pc))
5249 return cust;
5250
5251 if (cust->includes == NULL)
5252 return NULL;
5253
5254 for (i = 0; cust->includes[i]; ++i)
5255 {
5256 struct compunit_symtab *s = cust->includes[i];
5257
5258 s = recursively_find_pc_sect_compunit_symtab (s, pc);
5259 if (s != NULL)
5260 return s;
5261 }
5262
5263 return NULL;
5264}
5265
5266static struct compunit_symtab *
5267dw2_find_pc_sect_compunit_symtab (struct objfile *objfile,
5268 struct bound_minimal_symbol msymbol,
5269 CORE_ADDR pc,
5270 struct obj_section *section,
5271 int warn_if_readin)
5272{
5273 struct dwarf2_per_cu_data *data;
5274 struct compunit_symtab *result;
5275
927aa2e7
JK
5276 if (!objfile->psymtabs_addrmap)
5277 return NULL;
5278
5279 data = (struct dwarf2_per_cu_data *) addrmap_find (objfile->psymtabs_addrmap,
5280 pc);
5281 if (!data)
5282 return NULL;
5283
5284 if (warn_if_readin && data->v.quick->compunit_symtab)
5285 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
5286 paddress (get_objfile_arch (objfile), pc));
5287
5288 result
5289 = recursively_find_pc_sect_compunit_symtab (dw2_instantiate_symtab (data),
5290 pc);
5291 gdb_assert (result != NULL);
5292 return result;
5293}
5294
5295static void
5296dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
5297 void *data, int need_fullname)
5298{
ed2dc618
SM
5299 struct dwarf2_per_objfile *dwarf2_per_objfile
5300 = get_dwarf2_per_objfile (objfile);
927aa2e7
JK
5301
5302 if (!dwarf2_per_objfile->filenames_cache)
5303 {
5304 dwarf2_per_objfile->filenames_cache.emplace ();
5305
5306 htab_up visited (htab_create_alloc (10,
5307 htab_hash_pointer, htab_eq_pointer,
5308 NULL, xcalloc, xfree));
5309
5310 /* The rule is CUs specify all the files, including those used
5311 by any TU, so there's no need to scan TUs here. We can
5312 ignore file names coming from already-expanded CUs. */
5313
5314 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
5315 {
ed2dc618 5316 dwarf2_per_cu_data *per_cu = dw2_get_cutu (dwarf2_per_objfile, i);
927aa2e7
JK
5317
5318 if (per_cu->v.quick->compunit_symtab)
5319 {
5320 void **slot = htab_find_slot (visited.get (),
5321 per_cu->v.quick->file_names,
5322 INSERT);
5323
5324 *slot = per_cu->v.quick->file_names;
5325 }
5326 }
5327
5328 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
5329 {
ed2dc618 5330 dwarf2_per_cu_data *per_cu = dw2_get_cu (dwarf2_per_objfile, i);
927aa2e7
JK
5331 struct quick_file_names *file_data;
5332 void **slot;
5333
5334 /* We only need to look at symtabs not already expanded. */
5335 if (per_cu->v.quick->compunit_symtab)
5336 continue;
5337
5338 file_data = dw2_get_file_names (per_cu);
5339 if (file_data == NULL)
5340 continue;
5341
5342 slot = htab_find_slot (visited.get (), file_data, INSERT);
5343 if (*slot)
5344 {
5345 /* Already visited. */
5346 continue;
5347 }
5348 *slot = file_data;
5349
5350 for (int j = 0; j < file_data->num_file_names; ++j)
5351 {
5352 const char *filename = file_data->file_names[j];
5353 dwarf2_per_objfile->filenames_cache->seen (filename);
5354 }
5355 }
5356 }
5357
5358 dwarf2_per_objfile->filenames_cache->traverse ([&] (const char *filename)
5359 {
5360 gdb::unique_xmalloc_ptr<char> this_real_name;
5361
5362 if (need_fullname)
5363 this_real_name = gdb_realpath (filename);
5364 (*fun) (filename, this_real_name.get (), data);
5365 });
5366}
5367
5368static int
5369dw2_has_symbols (struct objfile *objfile)
5370{
5371 return 1;
5372}
5373
5374const struct quick_symbol_functions dwarf2_gdb_index_functions =
5375{
5376 dw2_has_symbols,
5377 dw2_find_last_source_symtab,
5378 dw2_forget_cached_source_info,
5379 dw2_map_symtabs_matching_filename,
5380 dw2_lookup_symbol,
5381 dw2_print_stats,
5382 dw2_dump,
5383 dw2_relocate,
5384 dw2_expand_symtabs_for_function,
5385 dw2_expand_all_symtabs,
5386 dw2_expand_symtabs_with_fullname,
5387 dw2_map_matching_symbols,
5388 dw2_expand_symtabs_matching,
5389 dw2_find_pc_sect_compunit_symtab,
5390 NULL,
5391 dw2_map_symbol_filenames
5392};
5393
5394/* DWARF-5 debug_names reader. */
5395
5396/* DWARF-5 augmentation string for GDB's DW_IDX_GNU_* extension. */
5397static const gdb_byte dwarf5_augmentation[] = { 'G', 'D', 'B', 0 };
5398
5399/* A helper function that reads the .debug_names section in SECTION
5400 and fills in MAP. FILENAME is the name of the file containing the
5401 section; it is used for error reporting.
5402
5403 Returns true if all went well, false otherwise. */
5404
5405static bool
5406read_debug_names_from_section (struct objfile *objfile,
5407 const char *filename,
5408 struct dwarf2_section_info *section,
5409 mapped_debug_names &map)
5410{
5411 if (dwarf2_section_empty_p (section))
5412 return false;
5413
5414 /* Older elfutils strip versions could keep the section in the main
5415 executable while splitting it for the separate debug info file. */
5416 if ((get_section_flags (section) & SEC_HAS_CONTENTS) == 0)
5417 return false;
5418
5419 dwarf2_read_section (objfile, section);
5420
5421 map.dwarf5_byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
5422
5423 const gdb_byte *addr = section->buffer;
5424
5425 bfd *const abfd = get_section_bfd_owner (section);
5426
5427 unsigned int bytes_read;
5428 LONGEST length = read_initial_length (abfd, addr, &bytes_read);
5429 addr += bytes_read;
5430
5431 map.dwarf5_is_dwarf64 = bytes_read != 4;
5432 map.offset_size = map.dwarf5_is_dwarf64 ? 8 : 4;
5433 if (bytes_read + length != section->size)
5434 {
5435 /* There may be multiple per-CU indices. */
5436 warning (_("Section .debug_names in %s length %s does not match "
5437 "section length %s, ignoring .debug_names."),
5438 filename, plongest (bytes_read + length),
5439 pulongest (section->size));
5440 return false;
5441 }
5442
5443 /* The version number. */
5444 uint16_t version = read_2_bytes (abfd, addr);
5445 addr += 2;
5446 if (version != 5)
5447 {
5448 warning (_("Section .debug_names in %s has unsupported version %d, "
5449 "ignoring .debug_names."),
5450 filename, version);
5451 return false;
5452 }
5453
5454 /* Padding. */
5455 uint16_t padding = read_2_bytes (abfd, addr);
5456 addr += 2;
5457 if (padding != 0)
5458 {
5459 warning (_("Section .debug_names in %s has unsupported padding %d, "
5460 "ignoring .debug_names."),
5461 filename, padding);
5462 return false;
5463 }
5464
5465 /* comp_unit_count - The number of CUs in the CU list. */
5466 map.cu_count = read_4_bytes (abfd, addr);
5467 addr += 4;
5468
5469 /* local_type_unit_count - The number of TUs in the local TU
5470 list. */
5471 map.tu_count = read_4_bytes (abfd, addr);
5472 addr += 4;
5473
5474 /* foreign_type_unit_count - The number of TUs in the foreign TU
5475 list. */
5476 uint32_t foreign_tu_count = read_4_bytes (abfd, addr);
5477 addr += 4;
5478 if (foreign_tu_count != 0)
5479 {
5480 warning (_("Section .debug_names in %s has unsupported %lu foreign TUs, "
5481 "ignoring .debug_names."),
5482 filename, static_cast<unsigned long> (foreign_tu_count));
5483 return false;
5484 }
5485
5486 /* bucket_count - The number of hash buckets in the hash lookup
5487 table. */
5488 map.bucket_count = read_4_bytes (abfd, addr);
5489 addr += 4;
5490
5491 /* name_count - The number of unique names in the index. */
5492 map.name_count = read_4_bytes (abfd, addr);
5493 addr += 4;
5494
5495 /* abbrev_table_size - The size in bytes of the abbreviations
5496 table. */
5497 uint32_t abbrev_table_size = read_4_bytes (abfd, addr);
5498 addr += 4;
5499
5500 /* augmentation_string_size - The size in bytes of the augmentation
5501 string. This value is rounded up to a multiple of 4. */
5502 uint32_t augmentation_string_size = read_4_bytes (abfd, addr);
5503 addr += 4;
5504 map.augmentation_is_gdb = ((augmentation_string_size
5505 == sizeof (dwarf5_augmentation))
5506 && memcmp (addr, dwarf5_augmentation,
5507 sizeof (dwarf5_augmentation)) == 0);
5508 augmentation_string_size += (-augmentation_string_size) & 3;
5509 addr += augmentation_string_size;
5510
5511 /* List of CUs */
5512 map.cu_table_reordered = addr;
5513 addr += map.cu_count * map.offset_size;
5514
5515 /* List of Local TUs */
5516 map.tu_table_reordered = addr;
5517 addr += map.tu_count * map.offset_size;
5518
5519 /* Hash Lookup Table */
5520 map.bucket_table_reordered = reinterpret_cast<const uint32_t *> (addr);
5521 addr += map.bucket_count * 4;
5522 map.hash_table_reordered = reinterpret_cast<const uint32_t *> (addr);
5523 addr += map.name_count * 4;
5524
5525 /* Name Table */
5526 map.name_table_string_offs_reordered = addr;
5527 addr += map.name_count * map.offset_size;
5528 map.name_table_entry_offs_reordered = addr;
5529 addr += map.name_count * map.offset_size;
5530
5531 const gdb_byte *abbrev_table_start = addr;
5532 for (;;)
5533 {
5534 unsigned int bytes_read;
5535 const ULONGEST index_num = read_unsigned_leb128 (abfd, addr, &bytes_read);
5536 addr += bytes_read;
5537 if (index_num == 0)
5538 break;
5539
5540 const auto insertpair
5541 = map.abbrev_map.emplace (index_num, mapped_debug_names::index_val ());
5542 if (!insertpair.second)
5543 {
5544 warning (_("Section .debug_names in %s has duplicate index %s, "
5545 "ignoring .debug_names."),
5546 filename, pulongest (index_num));
5547 return false;
5548 }
5549 mapped_debug_names::index_val &indexval = insertpair.first->second;
5550 indexval.dwarf_tag = read_unsigned_leb128 (abfd, addr, &bytes_read);
5551 addr += bytes_read;
5552
5553 for (;;)
5554 {
5555 mapped_debug_names::index_val::attr attr;
5556 attr.dw_idx = read_unsigned_leb128 (abfd, addr, &bytes_read);
5557 addr += bytes_read;
5558 attr.form = read_unsigned_leb128 (abfd, addr, &bytes_read);
5559 addr += bytes_read;
5560 if (attr.form == DW_FORM_implicit_const)
5561 {
5562 attr.implicit_const = read_signed_leb128 (abfd, addr,
5563 &bytes_read);
5564 addr += bytes_read;
5565 }
5566 if (attr.dw_idx == 0 && attr.form == 0)
5567 break;
5568 indexval.attr_vec.push_back (std::move (attr));
5569 }
5570 }
5571 if (addr != abbrev_table_start + abbrev_table_size)
5572 {
5573 warning (_("Section .debug_names in %s has abbreviation_table "
5574 "of size %zu vs. written as %u, ignoring .debug_names."),
5575 filename, addr - abbrev_table_start, abbrev_table_size);
5576 return false;
5577 }
5578 map.entry_pool = addr;
5579
5580 return true;
5581}
5582
5583/* A helper for create_cus_from_debug_names that handles the MAP's CU
5584 list. */
5585
5586static void
ed2dc618 5587create_cus_from_debug_names_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
927aa2e7
JK
5588 const mapped_debug_names &map,
5589 dwarf2_section_info &section,
5590 bool is_dwz, int base_offset)
5591{
5592 sect_offset sect_off_prev;
5593 for (uint32_t i = 0; i <= map.cu_count; ++i)
5594 {
5595 sect_offset sect_off_next;
5596 if (i < map.cu_count)
5597 {
5598 sect_off_next
5599 = (sect_offset) (extract_unsigned_integer
5600 (map.cu_table_reordered + i * map.offset_size,
5601 map.offset_size,
5602 map.dwarf5_byte_order));
5603 }
5604 else
5605 sect_off_next = (sect_offset) section.size;
5606 if (i >= 1)
5607 {
5608 const ULONGEST length = sect_off_next - sect_off_prev;
5609 dwarf2_per_objfile->all_comp_units[base_offset + (i - 1)]
ed2dc618 5610 = create_cu_from_index_list (dwarf2_per_objfile, &section, is_dwz,
927aa2e7
JK
5611 sect_off_prev, length);
5612 }
5613 sect_off_prev = sect_off_next;
5614 }
5615}
5616
5617/* Read the CU list from the mapped index, and use it to create all
ed2dc618 5618 the CU objects for this dwarf2_per_objfile. */
927aa2e7
JK
5619
5620static void
ed2dc618 5621create_cus_from_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile,
927aa2e7
JK
5622 const mapped_debug_names &map,
5623 const mapped_debug_names &dwz_map)
5624{
ed2dc618 5625 struct objfile *objfile = dwarf2_per_objfile->objfile;
927aa2e7
JK
5626
5627 dwarf2_per_objfile->n_comp_units = map.cu_count + dwz_map.cu_count;
5628 dwarf2_per_objfile->all_comp_units
5629 = XOBNEWVEC (&objfile->objfile_obstack, struct dwarf2_per_cu_data *,
5630 dwarf2_per_objfile->n_comp_units);
5631
ed2dc618
SM
5632 create_cus_from_debug_names_list (dwarf2_per_objfile, map,
5633 dwarf2_per_objfile->info,
927aa2e7
JK
5634 false /* is_dwz */,
5635 0 /* base_offset */);
5636
5637 if (dwz_map.cu_count == 0)
5638 return;
5639
ed2dc618
SM
5640 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
5641 create_cus_from_debug_names_list (dwarf2_per_objfile, dwz_map, dwz->info,
927aa2e7
JK
5642 true /* is_dwz */,
5643 map.cu_count /* base_offset */);
5644}
5645
5646/* Read .debug_names. If everything went ok, initialize the "quick"
5647 elements of all the CUs and return true. Otherwise, return false. */
5648
5649static bool
ed2dc618 5650dwarf2_read_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile)
927aa2e7 5651{
ed2dc618
SM
5652 mapped_debug_names local_map (dwarf2_per_objfile);
5653 mapped_debug_names dwz_map (dwarf2_per_objfile);
5654 struct objfile *objfile = dwarf2_per_objfile->objfile;
927aa2e7
JK
5655
5656 if (!read_debug_names_from_section (objfile, objfile_name (objfile),
5657 &dwarf2_per_objfile->debug_names,
5658 local_map))
5659 return false;
5660
5661 /* Don't use the index if it's empty. */
5662 if (local_map.name_count == 0)
5663 return false;
5664
5665 /* If there is a .dwz file, read it so we can get its CU list as
5666 well. */
ed2dc618 5667 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
927aa2e7
JK
5668 if (dwz != NULL)
5669 {
5670 if (!read_debug_names_from_section (objfile,
5671 bfd_get_filename (dwz->dwz_bfd),
5672 &dwz->debug_names, dwz_map))
5673 {
5674 warning (_("could not read '.debug_names' section from %s; skipping"),
5675 bfd_get_filename (dwz->dwz_bfd));
5676 return false;
5677 }
5678 }
5679
ed2dc618 5680 create_cus_from_debug_names (dwarf2_per_objfile, local_map, dwz_map);
927aa2e7
JK
5681
5682 if (local_map.tu_count != 0)
5683 {
5684 /* We can only handle a single .debug_types when we have an
5685 index. */
5686 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) != 1)
5687 return false;
5688
5689 dwarf2_section_info *section = VEC_index (dwarf2_section_info_def,
5690 dwarf2_per_objfile->types, 0);
5691
5692 create_signatured_type_table_from_debug_names
ed2dc618 5693 (dwarf2_per_objfile, local_map, section, &dwarf2_per_objfile->abbrev);
927aa2e7
JK
5694 }
5695
ed2dc618
SM
5696 create_addrmap_from_aranges (dwarf2_per_objfile,
5697 &dwarf2_per_objfile->debug_aranges);
927aa2e7 5698
ed2dc618
SM
5699 dwarf2_per_objfile->debug_names_table.reset
5700 (new mapped_debug_names (dwarf2_per_objfile));
927aa2e7
JK
5701 *dwarf2_per_objfile->debug_names_table = std::move (local_map);
5702 dwarf2_per_objfile->using_index = 1;
5703 dwarf2_per_objfile->quick_file_names_table =
5704 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
5705
5706 return true;
5707}
5708
927aa2e7
JK
5709/* Type used to manage iterating over all CUs looking for a symbol for
5710 .debug_names. */
5711
5712class dw2_debug_names_iterator
5713{
5714public:
5715 /* If WANT_SPECIFIC_BLOCK is true, only look for symbols in block
5716 BLOCK_INDEX. Otherwise BLOCK_INDEX is ignored. */
5717 dw2_debug_names_iterator (const mapped_debug_names &map,
5718 bool want_specific_block,
5719 block_enum block_index, domain_enum domain,
5720 const char *name)
5721 : m_map (map), m_want_specific_block (want_specific_block),
5722 m_block_index (block_index), m_domain (domain),
5723 m_addr (find_vec_in_debug_names (map, name))
5724 {}
5725
5726 dw2_debug_names_iterator (const mapped_debug_names &map,
5727 search_domain search, uint32_t namei)
5728 : m_map (map),
5729 m_search (search),
5730 m_addr (find_vec_in_debug_names (map, namei))
5731 {}
5732
5733 /* Return the next matching CU or NULL if there are no more. */
5734 dwarf2_per_cu_data *next ();
5735
5736private:
5737 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5738 const char *name);
5739 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5740 uint32_t namei);
5741
5742 /* The internalized form of .debug_names. */
5743 const mapped_debug_names &m_map;
5744
5745 /* If true, only look for symbols that match BLOCK_INDEX. */
5746 const bool m_want_specific_block = false;
5747
5748 /* One of GLOBAL_BLOCK or STATIC_BLOCK.
5749 Unused if !WANT_SPECIFIC_BLOCK - FIRST_LOCAL_BLOCK is an invalid
5750 value. */
5751 const block_enum m_block_index = FIRST_LOCAL_BLOCK;
5752
5753 /* The kind of symbol we're looking for. */
5754 const domain_enum m_domain = UNDEF_DOMAIN;
5755 const search_domain m_search = ALL_DOMAIN;
5756
5757 /* The list of CUs from the index entry of the symbol, or NULL if
5758 not found. */
5759 const gdb_byte *m_addr;
5760};
5761
5762const char *
5763mapped_debug_names::namei_to_name (uint32_t namei) const
5764{
5765 const ULONGEST namei_string_offs
5766 = extract_unsigned_integer ((name_table_string_offs_reordered
5767 + namei * offset_size),
5768 offset_size,
5769 dwarf5_byte_order);
5770 return read_indirect_string_at_offset
ed2dc618 5771 (dwarf2_per_objfile, dwarf2_per_objfile->objfile->obfd, namei_string_offs);
927aa2e7
JK
5772}
5773
5774/* Find a slot in .debug_names for the object named NAME. If NAME is
5775 found, return pointer to its pool data. If NAME cannot be found,
5776 return NULL. */
5777
5778const gdb_byte *
5779dw2_debug_names_iterator::find_vec_in_debug_names
5780 (const mapped_debug_names &map, const char *name)
5781{
5782 int (*cmp) (const char *, const char *);
5783
5784 if (current_language->la_language == language_cplus
5785 || current_language->la_language == language_fortran
5786 || current_language->la_language == language_d)
5787 {
5788 /* NAME is already canonical. Drop any qualifiers as
5789 .debug_names does not contain any. */
5790
5791 if (strchr (name, '(') != NULL)
5792 {
5793 gdb::unique_xmalloc_ptr<char> without_params
5794 = cp_remove_params (name);
5795
5796 if (without_params != NULL)
5797 {
5798 name = without_params.get();
5799 }
5800 }
5801 }
5802
5803 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
5804
5805 const uint32_t full_hash = dwarf5_djb_hash (name);
5806 uint32_t namei
5807 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5808 (map.bucket_table_reordered
5809 + (full_hash % map.bucket_count)), 4,
5810 map.dwarf5_byte_order);
5811 if (namei == 0)
5812 return NULL;
5813 --namei;
5814 if (namei >= map.name_count)
5815 {
5816 complaint (&symfile_complaints,
5817 _("Wrong .debug_names with name index %u but name_count=%u "
5818 "[in module %s]"),
5819 namei, map.name_count,
ed2dc618 5820 objfile_name (map.dwarf2_per_objfile->objfile));
927aa2e7
JK
5821 return NULL;
5822 }
5823
5824 for (;;)
5825 {
5826 const uint32_t namei_full_hash
5827 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5828 (map.hash_table_reordered + namei), 4,
5829 map.dwarf5_byte_order);
5830 if (full_hash % map.bucket_count != namei_full_hash % map.bucket_count)
5831 return NULL;
5832
5833 if (full_hash == namei_full_hash)
5834 {
5835 const char *const namei_string = map.namei_to_name (namei);
5836
5837#if 0 /* An expensive sanity check. */
5838 if (namei_full_hash != dwarf5_djb_hash (namei_string))
5839 {
5840 complaint (&symfile_complaints,
5841 _("Wrong .debug_names hash for string at index %u "
5842 "[in module %s]"),
5843 namei, objfile_name (dwarf2_per_objfile->objfile));
5844 return NULL;
5845 }
5846#endif
5847
5848 if (cmp (namei_string, name) == 0)
5849 {
5850 const ULONGEST namei_entry_offs
5851 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5852 + namei * map.offset_size),
5853 map.offset_size, map.dwarf5_byte_order);
5854 return map.entry_pool + namei_entry_offs;
5855 }
5856 }
5857
5858 ++namei;
5859 if (namei >= map.name_count)
5860 return NULL;
5861 }
5862}
5863
5864const gdb_byte *
5865dw2_debug_names_iterator::find_vec_in_debug_names
5866 (const mapped_debug_names &map, uint32_t namei)
5867{
5868 if (namei >= map.name_count)
5869 {
5870 complaint (&symfile_complaints,
5871 _("Wrong .debug_names with name index %u but name_count=%u "
5872 "[in module %s]"),
5873 namei, map.name_count,
ed2dc618 5874 objfile_name (map.dwarf2_per_objfile->objfile));
927aa2e7
JK
5875 return NULL;
5876 }
5877
5878 const ULONGEST namei_entry_offs
5879 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5880 + namei * map.offset_size),
5881 map.offset_size, map.dwarf5_byte_order);
5882 return map.entry_pool + namei_entry_offs;
5883}
5884
5885/* See dw2_debug_names_iterator. */
5886
5887dwarf2_per_cu_data *
5888dw2_debug_names_iterator::next ()
5889{
5890 if (m_addr == NULL)
5891 return NULL;
5892
ed2dc618
SM
5893 struct dwarf2_per_objfile *dwarf2_per_objfile = m_map.dwarf2_per_objfile;
5894 struct objfile *objfile = dwarf2_per_objfile->objfile;
5895 bfd *const abfd = objfile->obfd;
927aa2e7
JK
5896
5897 again:
5898
5899 unsigned int bytes_read;
5900 const ULONGEST abbrev = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5901 m_addr += bytes_read;
5902 if (abbrev == 0)
5903 return NULL;
5904
5905 const auto indexval_it = m_map.abbrev_map.find (abbrev);
5906 if (indexval_it == m_map.abbrev_map.cend ())
5907 {
5908 complaint (&symfile_complaints,
5909 _("Wrong .debug_names undefined abbrev code %s "
5910 "[in module %s]"),
ed2dc618 5911 pulongest (abbrev), objfile_name (objfile));
927aa2e7
JK
5912 return NULL;
5913 }
5914 const mapped_debug_names::index_val &indexval = indexval_it->second;
5915 bool have_is_static = false;
5916 bool is_static;
5917 dwarf2_per_cu_data *per_cu = NULL;
5918 for (const mapped_debug_names::index_val::attr &attr : indexval.attr_vec)
5919 {
5920 ULONGEST ull;
5921 switch (attr.form)
5922 {
5923 case DW_FORM_implicit_const:
5924 ull = attr.implicit_const;
5925 break;
5926 case DW_FORM_flag_present:
5927 ull = 1;
5928 break;
5929 case DW_FORM_udata:
5930 ull = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5931 m_addr += bytes_read;
5932 break;
5933 default:
5934 complaint (&symfile_complaints,
5935 _("Unsupported .debug_names form %s [in module %s]"),
5936 dwarf_form_name (attr.form),
ed2dc618 5937 objfile_name (objfile));
927aa2e7
JK
5938 return NULL;
5939 }
5940 switch (attr.dw_idx)
5941 {
5942 case DW_IDX_compile_unit:
5943 /* Don't crash on bad data. */
8af5c486 5944 if (ull >= dwarf2_per_objfile->n_comp_units)
927aa2e7
JK
5945 {
5946 complaint (&symfile_complaints,
5947 _(".debug_names entry has bad CU index %s"
5948 " [in module %s]"),
5949 pulongest (ull),
5950 objfile_name (dwarf2_per_objfile->objfile));
5951 continue;
5952 }
ed2dc618 5953 per_cu = dw2_get_cutu (dwarf2_per_objfile, ull);
927aa2e7 5954 break;
8af5c486
JK
5955 case DW_IDX_type_unit:
5956 /* Don't crash on bad data. */
5957 if (ull >= dwarf2_per_objfile->n_type_units)
5958 {
5959 complaint (&symfile_complaints,
5960 _(".debug_names entry has bad TU index %s"
5961 " [in module %s]"),
5962 pulongest (ull),
5963 objfile_name (dwarf2_per_objfile->objfile));
5964 continue;
5965 }
ed2dc618
SM
5966 per_cu = dw2_get_cutu (dwarf2_per_objfile,
5967 dwarf2_per_objfile->n_comp_units + ull);
8af5c486 5968 break;
927aa2e7
JK
5969 case DW_IDX_GNU_internal:
5970 if (!m_map.augmentation_is_gdb)
5971 break;
5972 have_is_static = true;
5973 is_static = true;
5974 break;
5975 case DW_IDX_GNU_external:
5976 if (!m_map.augmentation_is_gdb)
5977 break;
5978 have_is_static = true;
5979 is_static = false;
5980 break;
5981 }
5982 }
5983
5984 /* Skip if already read in. */
5985 if (per_cu->v.quick->compunit_symtab)
5986 goto again;
5987
5988 /* Check static vs global. */
5989 if (have_is_static)
5990 {
5991 const bool want_static = m_block_index != GLOBAL_BLOCK;
5992 if (m_want_specific_block && want_static != is_static)
5993 goto again;
5994 }
5995
5996 /* Match dw2_symtab_iter_next, symbol_kind
5997 and debug_names::psymbol_tag. */
5998 switch (m_domain)
5999 {
6000 case VAR_DOMAIN:
6001 switch (indexval.dwarf_tag)
6002 {
6003 case DW_TAG_variable:
6004 case DW_TAG_subprogram:
6005 /* Some types are also in VAR_DOMAIN. */
6006 case DW_TAG_typedef:
6007 case DW_TAG_structure_type:
6008 break;
6009 default:
6010 goto again;
6011 }
6012 break;
6013 case STRUCT_DOMAIN:
6014 switch (indexval.dwarf_tag)
6015 {
6016 case DW_TAG_typedef:
6017 case DW_TAG_structure_type:
6018 break;
6019 default:
6020 goto again;
6021 }
6022 break;
6023 case LABEL_DOMAIN:
6024 switch (indexval.dwarf_tag)
6025 {
6026 case 0:
6027 case DW_TAG_variable:
6028 break;
6029 default:
6030 goto again;
6031 }
6032 break;
6033 default:
6034 break;
6035 }
6036
6037 /* Match dw2_expand_symtabs_matching, symbol_kind and
6038 debug_names::psymbol_tag. */
6039 switch (m_search)
4b514bc8 6040 {
927aa2e7
JK
6041 case VARIABLES_DOMAIN:
6042 switch (indexval.dwarf_tag)
4b514bc8 6043 {
927aa2e7
JK
6044 case DW_TAG_variable:
6045 break;
6046 default:
6047 goto again;
4b514bc8 6048 }
927aa2e7
JK
6049 break;
6050 case FUNCTIONS_DOMAIN:
6051 switch (indexval.dwarf_tag)
4b514bc8 6052 {
927aa2e7
JK
6053 case DW_TAG_subprogram:
6054 break;
6055 default:
6056 goto again;
4b514bc8 6057 }
927aa2e7
JK
6058 break;
6059 case TYPES_DOMAIN:
6060 switch (indexval.dwarf_tag)
6061 {
6062 case DW_TAG_typedef:
6063 case DW_TAG_structure_type:
6064 break;
6065 default:
6066 goto again;
6067 }
6068 break;
6069 default:
6070 break;
4b514bc8 6071 }
927aa2e7
JK
6072
6073 return per_cu;
4b514bc8 6074}
61920122 6075
927aa2e7
JK
6076static struct compunit_symtab *
6077dw2_debug_names_lookup_symbol (struct objfile *objfile, int block_index_int,
6078 const char *name, domain_enum domain)
4b514bc8 6079{
927aa2e7 6080 const block_enum block_index = static_cast<block_enum> (block_index_int);
ed2dc618
SM
6081 struct dwarf2_per_objfile *dwarf2_per_objfile
6082 = get_dwarf2_per_objfile (objfile);
61920122 6083
927aa2e7
JK
6084 const auto &mapp = dwarf2_per_objfile->debug_names_table;
6085 if (!mapp)
61920122 6086 {
927aa2e7
JK
6087 /* index is NULL if OBJF_READNOW. */
6088 return NULL;
6089 }
6090 const auto &map = *mapp;
9291a0cd 6091
927aa2e7
JK
6092 dw2_debug_names_iterator iter (map, true /* want_specific_block */,
6093 block_index, domain, name);
9703b513 6094
927aa2e7
JK
6095 struct compunit_symtab *stab_best = NULL;
6096 struct dwarf2_per_cu_data *per_cu;
6097 while ((per_cu = iter.next ()) != NULL)
6098 {
6099 struct symbol *sym, *with_opaque = NULL;
6100 struct compunit_symtab *stab = dw2_instantiate_symtab (per_cu);
6101 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
6102 struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
9703b513 6103
927aa2e7
JK
6104 sym = block_find_symbol (block, name, domain,
6105 block_find_non_opaque_type_preferred,
6106 &with_opaque);
9703b513 6107
927aa2e7
JK
6108 /* Some caution must be observed with overloaded functions and
6109 methods, since the index will not contain any overload
6110 information (but NAME might contain it). */
a3ec0bb1 6111
927aa2e7
JK
6112 if (sym != NULL
6113 && strcmp_iw (SYMBOL_SEARCH_NAME (sym), name) == 0)
6114 return stab;
6115 if (with_opaque != NULL
6116 && strcmp_iw (SYMBOL_SEARCH_NAME (with_opaque), name) == 0)
6117 stab_best = stab;
9703b513 6118
927aa2e7 6119 /* Keep looking through other CUs. */
9703b513
TT
6120 }
6121
927aa2e7 6122 return stab_best;
9703b513
TT
6123}
6124
927aa2e7
JK
6125/* This dumps minimal information about .debug_names. It is called
6126 via "mt print objfiles". The gdb.dwarf2/gdb-index.exp testcase
6127 uses this to verify that .debug_names has been loaded. */
9291a0cd 6128
927aa2e7
JK
6129static void
6130dw2_debug_names_dump (struct objfile *objfile)
6131{
ed2dc618
SM
6132 struct dwarf2_per_objfile *dwarf2_per_objfile
6133 = get_dwarf2_per_objfile (objfile);
6134
927aa2e7
JK
6135 gdb_assert (dwarf2_per_objfile->using_index);
6136 printf_filtered (".debug_names:");
6137 if (dwarf2_per_objfile->debug_names_table)
6138 printf_filtered (" exists\n");
6139 else
6140 printf_filtered (" faked for \"readnow\"\n");
6141 printf_filtered ("\n");
9291a0cd
TT
6142}
6143
9291a0cd 6144static void
927aa2e7
JK
6145dw2_debug_names_expand_symtabs_for_function (struct objfile *objfile,
6146 const char *func_name)
9291a0cd 6147{
ed2dc618
SM
6148 struct dwarf2_per_objfile *dwarf2_per_objfile
6149 = get_dwarf2_per_objfile (objfile);
ae2de4f8 6150
927aa2e7
JK
6151 /* dwarf2_per_objfile->debug_names_table is NULL if OBJF_READNOW. */
6152 if (dwarf2_per_objfile->debug_names_table)
24c79950 6153 {
927aa2e7 6154 const mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
24c79950 6155
927aa2e7
JK
6156 /* Note: It doesn't matter what we pass for block_index here. */
6157 dw2_debug_names_iterator iter (map, false /* want_specific_block */,
6158 GLOBAL_BLOCK, VAR_DOMAIN, func_name);
24c79950 6159
927aa2e7
JK
6160 struct dwarf2_per_cu_data *per_cu;
6161 while ((per_cu = iter.next ()) != NULL)
6162 dw2_instantiate_symtab (per_cu);
6163 }
6164}
24c79950 6165
927aa2e7
JK
6166static void
6167dw2_debug_names_expand_symtabs_matching
6168 (struct objfile *objfile,
6169 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
6170 const lookup_name_info &lookup_name,
6171 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
6172 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
6173 enum search_domain kind)
6174{
ed2dc618
SM
6175 struct dwarf2_per_objfile *dwarf2_per_objfile
6176 = get_dwarf2_per_objfile (objfile);
9291a0cd 6177
927aa2e7
JK
6178 /* debug_names_table is NULL if OBJF_READNOW. */
6179 if (!dwarf2_per_objfile->debug_names_table)
6180 return;
9291a0cd 6181
ed2dc618 6182 dw_expand_symtabs_matching_file_matcher (dwarf2_per_objfile, file_matcher);
24c79950 6183
44ed8f3e 6184 mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
bbf2f4df 6185
44ed8f3e
PA
6186 dw2_expand_symtabs_matching_symbol (map, lookup_name,
6187 symbol_matcher,
6188 kind, [&] (offset_type namei)
927aa2e7 6189 {
927aa2e7
JK
6190 /* The name was matched, now expand corresponding CUs that were
6191 marked. */
6192 dw2_debug_names_iterator iter (map, kind, namei);
bbf2f4df 6193
927aa2e7
JK
6194 struct dwarf2_per_cu_data *per_cu;
6195 while ((per_cu = iter.next ()) != NULL)
6196 dw2_expand_symtabs_matching_one (per_cu, file_matcher,
6197 expansion_notify);
44ed8f3e 6198 });
9291a0cd
TT
6199}
6200
927aa2e7 6201const struct quick_symbol_functions dwarf2_debug_names_functions =
9291a0cd
TT
6202{
6203 dw2_has_symbols,
6204 dw2_find_last_source_symtab,
6205 dw2_forget_cached_source_info,
f8eba3c6 6206 dw2_map_symtabs_matching_filename,
927aa2e7 6207 dw2_debug_names_lookup_symbol,
9291a0cd 6208 dw2_print_stats,
927aa2e7 6209 dw2_debug_names_dump,
9291a0cd 6210 dw2_relocate,
927aa2e7 6211 dw2_debug_names_expand_symtabs_for_function,
9291a0cd 6212 dw2_expand_all_symtabs,
652a8996 6213 dw2_expand_symtabs_with_fullname,
40658b94 6214 dw2_map_matching_symbols,
927aa2e7 6215 dw2_debug_names_expand_symtabs_matching,
43f3e411 6216 dw2_find_pc_sect_compunit_symtab,
71a3c369 6217 NULL,
9291a0cd
TT
6218 dw2_map_symbol_filenames
6219};
6220
3c0aa29a 6221/* See symfile.h. */
9291a0cd 6222
3c0aa29a
PA
6223bool
6224dwarf2_initialize_objfile (struct objfile *objfile, dw_index_kind *index_kind)
9291a0cd 6225{
ed2dc618
SM
6226 struct dwarf2_per_objfile *dwarf2_per_objfile
6227 = get_dwarf2_per_objfile (objfile);
6228
9291a0cd
TT
6229 /* If we're about to read full symbols, don't bother with the
6230 indices. In this case we also don't care if some other debug
6231 format is making psymtabs, because they are all about to be
6232 expanded anyway. */
6233 if ((objfile->flags & OBJF_READNOW))
6234 {
6235 int i;
6236
6237 dwarf2_per_objfile->using_index = 1;
ed2dc618
SM
6238 create_all_comp_units (dwarf2_per_objfile);
6239 create_all_type_units (dwarf2_per_objfile);
7b9f3c50
DE
6240 dwarf2_per_objfile->quick_file_names_table =
6241 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
9291a0cd 6242
1fd400ff 6243 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
d467dd73 6244 + dwarf2_per_objfile->n_type_units); ++i)
9291a0cd 6245 {
ed2dc618 6246 dwarf2_per_cu_data *per_cu = dw2_get_cutu (dwarf2_per_objfile, i);
9291a0cd 6247
e254ef6a
DE
6248 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6249 struct dwarf2_per_cu_quick_data);
9291a0cd
TT
6250 }
6251
6252 /* Return 1 so that gdb sees the "quick" functions. However,
6253 these functions will be no-ops because we will have expanded
6254 all symtabs. */
3c0aa29a
PA
6255 *index_kind = dw_index_kind::GDB_INDEX;
6256 return true;
9291a0cd
TT
6257 }
6258
ed2dc618 6259 if (dwarf2_read_debug_names (dwarf2_per_objfile))
3c0aa29a
PA
6260 {
6261 *index_kind = dw_index_kind::DEBUG_NAMES;
6262 return true;
6263 }
927aa2e7 6264
9291a0cd 6265 if (dwarf2_read_index (objfile))
3c0aa29a
PA
6266 {
6267 *index_kind = dw_index_kind::GDB_INDEX;
6268 return true;
6269 }
9291a0cd 6270
3c0aa29a 6271 return false;
9291a0cd
TT
6272}
6273
6274\f
6275
dce234bc
PP
6276/* Build a partial symbol table. */
6277
6278void
f29dff0a 6279dwarf2_build_psymtabs (struct objfile *objfile)
dce234bc 6280{
ed2dc618
SM
6281 struct dwarf2_per_objfile *dwarf2_per_objfile
6282 = get_dwarf2_per_objfile (objfile);
c9bf0622 6283
af5bf4ad
SM
6284 if (objfile->global_psymbols.capacity () == 0
6285 && objfile->static_psymbols.capacity () == 0)
6286 init_psymbol_list (objfile, 1024);
c906108c 6287
492d29ea 6288 TRY
c9bf0622
TT
6289 {
6290 /* This isn't really ideal: all the data we allocate on the
6291 objfile's obstack is still uselessly kept around. However,
6292 freeing it seems unsafe. */
906768f9 6293 psymtab_discarder psymtabs (objfile);
ed2dc618 6294 dwarf2_build_psymtabs_hard (dwarf2_per_objfile);
906768f9 6295 psymtabs.keep ();
c9bf0622 6296 }
492d29ea
PA
6297 CATCH (except, RETURN_MASK_ERROR)
6298 {
6299 exception_print (gdb_stderr, except);
6300 }
6301 END_CATCH
c906108c 6302}
c906108c 6303
1ce1cefd
DE
6304/* Return the total length of the CU described by HEADER. */
6305
6306static unsigned int
6307get_cu_length (const struct comp_unit_head *header)
6308{
6309 return header->initial_length_size + header->length;
6310}
6311
9c541725 6312/* Return TRUE if SECT_OFF is within CU_HEADER. */
45452591 6313
9c541725
PA
6314static inline bool
6315offset_in_cu_p (const comp_unit_head *cu_header, sect_offset sect_off)
45452591 6316{
9c541725
PA
6317 sect_offset bottom = cu_header->sect_off;
6318 sect_offset top = cu_header->sect_off + get_cu_length (cu_header);
9a619af0 6319
9c541725 6320 return sect_off >= bottom && sect_off < top;
45452591
DE
6321}
6322
3b80fe9b
DE
6323/* Find the base address of the compilation unit for range lists and
6324 location lists. It will normally be specified by DW_AT_low_pc.
6325 In DWARF-3 draft 4, the base address could be overridden by
6326 DW_AT_entry_pc. It's been removed, but GCC still uses this for
6327 compilation units with discontinuous ranges. */
6328
6329static void
6330dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
6331{
6332 struct attribute *attr;
6333
6334 cu->base_known = 0;
6335 cu->base_address = 0;
6336
6337 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
6338 if (attr)
6339 {
31aa7e4e 6340 cu->base_address = attr_value_as_address (attr);
3b80fe9b
DE
6341 cu->base_known = 1;
6342 }
6343 else
6344 {
6345 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
6346 if (attr)
6347 {
31aa7e4e 6348 cu->base_address = attr_value_as_address (attr);
3b80fe9b
DE
6349 cu->base_known = 1;
6350 }
6351 }
6352}
6353
93311388 6354/* Read in the comp unit header information from the debug_info at info_ptr.
43988095 6355 Use rcuh_kind::COMPILE as the default type if not known by the caller.
93311388
DE
6356 NOTE: This leaves members offset, first_die_offset to be filled in
6357 by the caller. */
107d2387 6358
d521ce57 6359static const gdb_byte *
107d2387 6360read_comp_unit_head (struct comp_unit_head *cu_header,
43988095
JK
6361 const gdb_byte *info_ptr,
6362 struct dwarf2_section_info *section,
6363 rcuh_kind section_kind)
107d2387
AC
6364{
6365 int signed_addr;
891d2f0b 6366 unsigned int bytes_read;
43988095
JK
6367 const char *filename = get_section_file_name (section);
6368 bfd *abfd = get_section_bfd_owner (section);
c764a876
DE
6369
6370 cu_header->length = read_initial_length (abfd, info_ptr, &bytes_read);
6371 cu_header->initial_length_size = bytes_read;
6372 cu_header->offset_size = (bytes_read == 4) ? 4 : 8;
613e1657 6373 info_ptr += bytes_read;
107d2387
AC
6374 cu_header->version = read_2_bytes (abfd, info_ptr);
6375 info_ptr += 2;
43988095
JK
6376 if (cu_header->version < 5)
6377 switch (section_kind)
6378 {
6379 case rcuh_kind::COMPILE:
6380 cu_header->unit_type = DW_UT_compile;
6381 break;
6382 case rcuh_kind::TYPE:
6383 cu_header->unit_type = DW_UT_type;
6384 break;
6385 default:
6386 internal_error (__FILE__, __LINE__,
6387 _("read_comp_unit_head: invalid section_kind"));
6388 }
6389 else
6390 {
6391 cu_header->unit_type = static_cast<enum dwarf_unit_type>
6392 (read_1_byte (abfd, info_ptr));
6393 info_ptr += 1;
6394 switch (cu_header->unit_type)
6395 {
6396 case DW_UT_compile:
6397 if (section_kind != rcuh_kind::COMPILE)
6398 error (_("Dwarf Error: wrong unit_type in compilation unit header "
6399 "(is DW_UT_compile, should be DW_UT_type) [in module %s]"),
6400 filename);
6401 break;
6402 case DW_UT_type:
6403 section_kind = rcuh_kind::TYPE;
6404 break;
6405 default:
6406 error (_("Dwarf Error: wrong unit_type in compilation unit header "
6407 "(is %d, should be %d or %d) [in module %s]"),
6408 cu_header->unit_type, DW_UT_compile, DW_UT_type, filename);
6409 }
6410
6411 cu_header->addr_size = read_1_byte (abfd, info_ptr);
6412 info_ptr += 1;
6413 }
9c541725
PA
6414 cu_header->abbrev_sect_off = (sect_offset) read_offset (abfd, info_ptr,
6415 cu_header,
6416 &bytes_read);
613e1657 6417 info_ptr += bytes_read;
43988095
JK
6418 if (cu_header->version < 5)
6419 {
6420 cu_header->addr_size = read_1_byte (abfd, info_ptr);
6421 info_ptr += 1;
6422 }
107d2387
AC
6423 signed_addr = bfd_get_sign_extend_vma (abfd);
6424 if (signed_addr < 0)
8e65ff28 6425 internal_error (__FILE__, __LINE__,
e2e0b3e5 6426 _("read_comp_unit_head: dwarf from non elf file"));
107d2387 6427 cu_header->signed_addr_p = signed_addr;
c764a876 6428
43988095
JK
6429 if (section_kind == rcuh_kind::TYPE)
6430 {
6431 LONGEST type_offset;
6432
6433 cu_header->signature = read_8_bytes (abfd, info_ptr);
6434 info_ptr += 8;
6435
6436 type_offset = read_offset (abfd, info_ptr, cu_header, &bytes_read);
6437 info_ptr += bytes_read;
9c541725
PA
6438 cu_header->type_cu_offset_in_tu = (cu_offset) type_offset;
6439 if (to_underlying (cu_header->type_cu_offset_in_tu) != type_offset)
43988095
JK
6440 error (_("Dwarf Error: Too big type_offset in compilation unit "
6441 "header (is %s) [in module %s]"), plongest (type_offset),
6442 filename);
6443 }
6444
107d2387
AC
6445 return info_ptr;
6446}
6447
36586728
TT
6448/* Helper function that returns the proper abbrev section for
6449 THIS_CU. */
6450
6451static struct dwarf2_section_info *
6452get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
6453{
6454 struct dwarf2_section_info *abbrev;
ed2dc618 6455 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
36586728
TT
6456
6457 if (this_cu->is_dwz)
ed2dc618 6458 abbrev = &dwarf2_get_dwz_file (dwarf2_per_objfile)->abbrev;
36586728
TT
6459 else
6460 abbrev = &dwarf2_per_objfile->abbrev;
6461
6462 return abbrev;
6463}
6464
9ff913ba
DE
6465/* Subroutine of read_and_check_comp_unit_head and
6466 read_and_check_type_unit_head to simplify them.
6467 Perform various error checking on the header. */
6468
6469static void
ed2dc618
SM
6470error_check_comp_unit_head (struct dwarf2_per_objfile *dwarf2_per_objfile,
6471 struct comp_unit_head *header,
4bdcc0c1
DE
6472 struct dwarf2_section_info *section,
6473 struct dwarf2_section_info *abbrev_section)
9ff913ba 6474{
a32a8923 6475 const char *filename = get_section_file_name (section);
9ff913ba 6476
43988095 6477 if (header->version < 2 || header->version > 5)
9ff913ba 6478 error (_("Dwarf Error: wrong version in compilation unit header "
43988095 6479 "(is %d, should be 2, 3, 4 or 5) [in module %s]"), header->version,
9ff913ba
DE
6480 filename);
6481
9c541725 6482 if (to_underlying (header->abbrev_sect_off)
36586728 6483 >= dwarf2_section_size (dwarf2_per_objfile->objfile, abbrev_section))
9d8780f0
SM
6484 error (_("Dwarf Error: bad offset (%s) in compilation unit header "
6485 "(offset %s + 6) [in module %s]"),
6486 sect_offset_str (header->abbrev_sect_off),
6487 sect_offset_str (header->sect_off),
9ff913ba
DE
6488 filename);
6489
9c541725 6490 /* Cast to ULONGEST to use 64-bit arithmetic when possible to
9ff913ba 6491 avoid potential 32-bit overflow. */
9c541725 6492 if (((ULONGEST) header->sect_off + get_cu_length (header))
9ff913ba 6493 > section->size)
9c541725 6494 error (_("Dwarf Error: bad length (0x%x) in compilation unit header "
9d8780f0
SM
6495 "(offset %s + 0) [in module %s]"),
6496 header->length, sect_offset_str (header->sect_off),
9ff913ba
DE
6497 filename);
6498}
6499
6500/* Read in a CU/TU header and perform some basic error checking.
6501 The contents of the header are stored in HEADER.
6502 The result is a pointer to the start of the first DIE. */
adabb602 6503
d521ce57 6504static const gdb_byte *
ed2dc618
SM
6505read_and_check_comp_unit_head (struct dwarf2_per_objfile *dwarf2_per_objfile,
6506 struct comp_unit_head *header,
9ff913ba 6507 struct dwarf2_section_info *section,
4bdcc0c1 6508 struct dwarf2_section_info *abbrev_section,
d521ce57 6509 const gdb_byte *info_ptr,
43988095 6510 rcuh_kind section_kind)
72bf9492 6511{
d521ce57 6512 const gdb_byte *beg_of_comp_unit = info_ptr;
72bf9492 6513
9c541725 6514 header->sect_off = (sect_offset) (beg_of_comp_unit - section->buffer);
adabb602 6515
43988095 6516 info_ptr = read_comp_unit_head (header, info_ptr, section, section_kind);
9ff913ba 6517
9c541725 6518 header->first_die_cu_offset = (cu_offset) (info_ptr - beg_of_comp_unit);
348e048f 6519
ed2dc618
SM
6520 error_check_comp_unit_head (dwarf2_per_objfile, header, section,
6521 abbrev_section);
9ff913ba
DE
6522
6523 return info_ptr;
348e048f
DE
6524}
6525
f4dc4d17
DE
6526/* Fetch the abbreviation table offset from a comp or type unit header. */
6527
6528static sect_offset
ed2dc618
SM
6529read_abbrev_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
6530 struct dwarf2_section_info *section,
9c541725 6531 sect_offset sect_off)
f4dc4d17 6532{
a32a8923 6533 bfd *abfd = get_section_bfd_owner (section);
d521ce57 6534 const gdb_byte *info_ptr;
ac298888 6535 unsigned int initial_length_size, offset_size;
43988095 6536 uint16_t version;
f4dc4d17
DE
6537
6538 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
9c541725 6539 info_ptr = section->buffer + to_underlying (sect_off);
ac298888 6540 read_initial_length (abfd, info_ptr, &initial_length_size);
f4dc4d17 6541 offset_size = initial_length_size == 4 ? 4 : 8;
43988095
JK
6542 info_ptr += initial_length_size;
6543
6544 version = read_2_bytes (abfd, info_ptr);
6545 info_ptr += 2;
6546 if (version >= 5)
6547 {
6548 /* Skip unit type and address size. */
6549 info_ptr += 2;
6550 }
6551
9c541725 6552 return (sect_offset) read_offset_1 (abfd, info_ptr, offset_size);
f4dc4d17
DE
6553}
6554
aaa75496
JB
6555/* Allocate a new partial symtab for file named NAME and mark this new
6556 partial symtab as being an include of PST. */
6557
6558static void
d521ce57 6559dwarf2_create_include_psymtab (const char *name, struct partial_symtab *pst,
aaa75496
JB
6560 struct objfile *objfile)
6561{
6562 struct partial_symtab *subpst = allocate_psymtab (name, objfile);
6563
fbd9ab74
JK
6564 if (!IS_ABSOLUTE_PATH (subpst->filename))
6565 {
6566 /* It shares objfile->objfile_obstack. */
6567 subpst->dirname = pst->dirname;
6568 }
6569
aaa75496
JB
6570 subpst->textlow = 0;
6571 subpst->texthigh = 0;
6572
8d749320
SM
6573 subpst->dependencies
6574 = XOBNEW (&objfile->objfile_obstack, struct partial_symtab *);
aaa75496
JB
6575 subpst->dependencies[0] = pst;
6576 subpst->number_of_dependencies = 1;
6577
6578 subpst->globals_offset = 0;
6579 subpst->n_global_syms = 0;
6580 subpst->statics_offset = 0;
6581 subpst->n_static_syms = 0;
43f3e411 6582 subpst->compunit_symtab = NULL;
aaa75496
JB
6583 subpst->read_symtab = pst->read_symtab;
6584 subpst->readin = 0;
6585
6586 /* No private part is necessary for include psymtabs. This property
6587 can be used to differentiate between such include psymtabs and
10b3939b 6588 the regular ones. */
58a9656e 6589 subpst->read_symtab_private = NULL;
aaa75496
JB
6590}
6591
6592/* Read the Line Number Program data and extract the list of files
6593 included by the source file represented by PST. Build an include
d85a05f0 6594 partial symtab for each of these included files. */
aaa75496
JB
6595
6596static void
6597dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
dee91e82
DE
6598 struct die_info *die,
6599 struct partial_symtab *pst)
aaa75496 6600{
fff8551c 6601 line_header_up lh;
d85a05f0 6602 struct attribute *attr;
aaa75496 6603
d85a05f0
DJ
6604 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
6605 if (attr)
9c541725 6606 lh = dwarf_decode_line_header ((sect_offset) DW_UNSND (attr), cu);
aaa75496
JB
6607 if (lh == NULL)
6608 return; /* No linetable, so no includes. */
6609
c6da4cef 6610 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). */
fff8551c 6611 dwarf_decode_lines (lh.get (), pst->dirname, cu, pst, pst->textlow, 1);
aaa75496
JB
6612}
6613
348e048f 6614static hashval_t
52dc124a 6615hash_signatured_type (const void *item)
348e048f 6616{
9a3c8263
SM
6617 const struct signatured_type *sig_type
6618 = (const struct signatured_type *) item;
9a619af0 6619
348e048f 6620 /* This drops the top 32 bits of the signature, but is ok for a hash. */
52dc124a 6621 return sig_type->signature;
348e048f
DE
6622}
6623
6624static int
52dc124a 6625eq_signatured_type (const void *item_lhs, const void *item_rhs)
348e048f 6626{
9a3c8263
SM
6627 const struct signatured_type *lhs = (const struct signatured_type *) item_lhs;
6628 const struct signatured_type *rhs = (const struct signatured_type *) item_rhs;
9a619af0 6629
348e048f
DE
6630 return lhs->signature == rhs->signature;
6631}
6632
1fd400ff
TT
6633/* Allocate a hash table for signatured types. */
6634
6635static htab_t
673bfd45 6636allocate_signatured_type_table (struct objfile *objfile)
1fd400ff
TT
6637{
6638 return htab_create_alloc_ex (41,
52dc124a
DE
6639 hash_signatured_type,
6640 eq_signatured_type,
1fd400ff
TT
6641 NULL,
6642 &objfile->objfile_obstack,
6643 hashtab_obstack_allocate,
6644 dummy_obstack_deallocate);
6645}
6646
d467dd73 6647/* A helper function to add a signatured type CU to a table. */
1fd400ff
TT
6648
6649static int
d467dd73 6650add_signatured_type_cu_to_table (void **slot, void *datum)
1fd400ff 6651{
9a3c8263
SM
6652 struct signatured_type *sigt = (struct signatured_type *) *slot;
6653 struct signatured_type ***datap = (struct signatured_type ***) datum;
1fd400ff 6654
b4dd5633 6655 **datap = sigt;
1fd400ff
TT
6656 ++*datap;
6657
6658 return 1;
6659}
6660
78d4d2c5 6661/* A helper for create_debug_types_hash_table. Read types from SECTION
43988095
JK
6662 and fill them into TYPES_HTAB. It will process only type units,
6663 therefore DW_UT_type. */
c88ee1f0 6664
78d4d2c5 6665static void
ed2dc618
SM
6666create_debug_type_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
6667 struct dwo_file *dwo_file,
43988095
JK
6668 dwarf2_section_info *section, htab_t &types_htab,
6669 rcuh_kind section_kind)
348e048f 6670{
3019eac3 6671 struct objfile *objfile = dwarf2_per_objfile->objfile;
4bdcc0c1 6672 struct dwarf2_section_info *abbrev_section;
78d4d2c5
JK
6673 bfd *abfd;
6674 const gdb_byte *info_ptr, *end_ptr;
348e048f 6675
4bdcc0c1
DE
6676 abbrev_section = (dwo_file != NULL
6677 ? &dwo_file->sections.abbrev
6678 : &dwarf2_per_objfile->abbrev);
6679
b4f54984 6680 if (dwarf_read_debug)
43988095
JK
6681 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
6682 get_section_name (section),
a32a8923 6683 get_section_file_name (abbrev_section));
09406207 6684
78d4d2c5
JK
6685 dwarf2_read_section (objfile, section);
6686 info_ptr = section->buffer;
348e048f 6687
78d4d2c5
JK
6688 if (info_ptr == NULL)
6689 return;
348e048f 6690
78d4d2c5
JK
6691 /* We can't set abfd until now because the section may be empty or
6692 not present, in which case the bfd is unknown. */
6693 abfd = get_section_bfd_owner (section);
348e048f 6694
78d4d2c5
JK
6695 /* We don't use init_cutu_and_read_dies_simple, or some such, here
6696 because we don't need to read any dies: the signature is in the
6697 header. */
3019eac3 6698
78d4d2c5
JK
6699 end_ptr = info_ptr + section->size;
6700 while (info_ptr < end_ptr)
6701 {
78d4d2c5
JK
6702 struct signatured_type *sig_type;
6703 struct dwo_unit *dwo_tu;
6704 void **slot;
6705 const gdb_byte *ptr = info_ptr;
6706 struct comp_unit_head header;
6707 unsigned int length;
8b70b953 6708
9c541725 6709 sect_offset sect_off = (sect_offset) (ptr - section->buffer);
348e048f 6710
a49dd8dd
JK
6711 /* Initialize it due to a false compiler warning. */
6712 header.signature = -1;
9c541725 6713 header.type_cu_offset_in_tu = (cu_offset) -1;
a49dd8dd 6714
78d4d2c5
JK
6715 /* We need to read the type's signature in order to build the hash
6716 table, but we don't need anything else just yet. */
348e048f 6717
ed2dc618 6718 ptr = read_and_check_comp_unit_head (dwarf2_per_objfile, &header, section,
43988095 6719 abbrev_section, ptr, section_kind);
348e048f 6720
78d4d2c5 6721 length = get_cu_length (&header);
6caca83c 6722
78d4d2c5
JK
6723 /* Skip dummy type units. */
6724 if (ptr >= info_ptr + length
43988095
JK
6725 || peek_abbrev_code (abfd, ptr) == 0
6726 || header.unit_type != DW_UT_type)
78d4d2c5
JK
6727 {
6728 info_ptr += length;
6729 continue;
6730 }
dee91e82 6731
78d4d2c5
JK
6732 if (types_htab == NULL)
6733 {
6734 if (dwo_file)
6735 types_htab = allocate_dwo_unit_table (objfile);
6736 else
6737 types_htab = allocate_signatured_type_table (objfile);
6738 }
8b70b953 6739
78d4d2c5
JK
6740 if (dwo_file)
6741 {
6742 sig_type = NULL;
6743 dwo_tu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6744 struct dwo_unit);
6745 dwo_tu->dwo_file = dwo_file;
43988095 6746 dwo_tu->signature = header.signature;
9c541725 6747 dwo_tu->type_offset_in_tu = header.type_cu_offset_in_tu;
78d4d2c5 6748 dwo_tu->section = section;
9c541725 6749 dwo_tu->sect_off = sect_off;
78d4d2c5
JK
6750 dwo_tu->length = length;
6751 }
6752 else
6753 {
6754 /* N.B.: type_offset is not usable if this type uses a DWO file.
6755 The real type_offset is in the DWO file. */
6756 dwo_tu = NULL;
6757 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6758 struct signatured_type);
43988095 6759 sig_type->signature = header.signature;
9c541725 6760 sig_type->type_offset_in_tu = header.type_cu_offset_in_tu;
e3b94546 6761 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
78d4d2c5
JK
6762 sig_type->per_cu.is_debug_types = 1;
6763 sig_type->per_cu.section = section;
9c541725 6764 sig_type->per_cu.sect_off = sect_off;
78d4d2c5
JK
6765 sig_type->per_cu.length = length;
6766 }
6767
6768 slot = htab_find_slot (types_htab,
6769 dwo_file ? (void*) dwo_tu : (void *) sig_type,
6770 INSERT);
6771 gdb_assert (slot != NULL);
6772 if (*slot != NULL)
6773 {
9c541725 6774 sect_offset dup_sect_off;
0349ea22 6775
3019eac3
DE
6776 if (dwo_file)
6777 {
78d4d2c5
JK
6778 const struct dwo_unit *dup_tu
6779 = (const struct dwo_unit *) *slot;
6780
9c541725 6781 dup_sect_off = dup_tu->sect_off;
3019eac3
DE
6782 }
6783 else
6784 {
78d4d2c5
JK
6785 const struct signatured_type *dup_tu
6786 = (const struct signatured_type *) *slot;
6787
9c541725 6788 dup_sect_off = dup_tu->per_cu.sect_off;
3019eac3 6789 }
8b70b953 6790
78d4d2c5 6791 complaint (&symfile_complaints,
9d8780f0
SM
6792 _("debug type entry at offset %s is duplicate to"
6793 " the entry at offset %s, signature %s"),
6794 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
43988095 6795 hex_string (header.signature));
78d4d2c5
JK
6796 }
6797 *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
3019eac3 6798
78d4d2c5 6799 if (dwarf_read_debug > 1)
9d8780f0
SM
6800 fprintf_unfiltered (gdb_stdlog, " offset %s, signature %s\n",
6801 sect_offset_str (sect_off),
43988095 6802 hex_string (header.signature));
3019eac3 6803
78d4d2c5
JK
6804 info_ptr += length;
6805 }
6806}
3019eac3 6807
78d4d2c5
JK
6808/* Create the hash table of all entries in the .debug_types
6809 (or .debug_types.dwo) section(s).
6810 If reading a DWO file, then DWO_FILE is a pointer to the DWO file object,
6811 otherwise it is NULL.
b3c8eb43 6812
78d4d2c5 6813 The result is a pointer to the hash table or NULL if there are no types.
348e048f 6814
78d4d2c5 6815 Note: This function processes DWO files only, not DWP files. */
348e048f 6816
78d4d2c5 6817static void
ed2dc618
SM
6818create_debug_types_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
6819 struct dwo_file *dwo_file,
78d4d2c5
JK
6820 VEC (dwarf2_section_info_def) *types,
6821 htab_t &types_htab)
6822{
6823 int ix;
6824 struct dwarf2_section_info *section;
6825
6826 if (VEC_empty (dwarf2_section_info_def, types))
6827 return;
348e048f 6828
78d4d2c5
JK
6829 for (ix = 0;
6830 VEC_iterate (dwarf2_section_info_def, types, ix, section);
6831 ++ix)
ed2dc618
SM
6832 create_debug_type_hash_table (dwarf2_per_objfile, dwo_file, section,
6833 types_htab, rcuh_kind::TYPE);
3019eac3
DE
6834}
6835
6836/* Create the hash table of all entries in the .debug_types section,
6837 and initialize all_type_units.
6838 The result is zero if there is an error (e.g. missing .debug_types section),
6839 otherwise non-zero. */
6840
6841static int
ed2dc618 6842create_all_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
3019eac3 6843{
78d4d2c5 6844 htab_t types_htab = NULL;
b4dd5633 6845 struct signatured_type **iter;
3019eac3 6846
ed2dc618
SM
6847 create_debug_type_hash_table (dwarf2_per_objfile, NULL,
6848 &dwarf2_per_objfile->info, types_htab,
43988095 6849 rcuh_kind::COMPILE);
ed2dc618
SM
6850 create_debug_types_hash_table (dwarf2_per_objfile, NULL,
6851 dwarf2_per_objfile->types, types_htab);
3019eac3
DE
6852 if (types_htab == NULL)
6853 {
6854 dwarf2_per_objfile->signatured_types = NULL;
6855 return 0;
6856 }
6857
348e048f
DE
6858 dwarf2_per_objfile->signatured_types = types_htab;
6859
6aa5f3a6
DE
6860 dwarf2_per_objfile->n_type_units
6861 = dwarf2_per_objfile->n_allocated_type_units
6862 = htab_elements (types_htab);
8d749320
SM
6863 dwarf2_per_objfile->all_type_units =
6864 XNEWVEC (struct signatured_type *, dwarf2_per_objfile->n_type_units);
d467dd73
DE
6865 iter = &dwarf2_per_objfile->all_type_units[0];
6866 htab_traverse_noresize (types_htab, add_signatured_type_cu_to_table, &iter);
6867 gdb_assert (iter - &dwarf2_per_objfile->all_type_units[0]
6868 == dwarf2_per_objfile->n_type_units);
1fd400ff 6869
348e048f
DE
6870 return 1;
6871}
6872
6aa5f3a6
DE
6873/* Add an entry for signature SIG to dwarf2_per_objfile->signatured_types.
6874 If SLOT is non-NULL, it is the entry to use in the hash table.
6875 Otherwise we find one. */
6876
6877static struct signatured_type *
ed2dc618
SM
6878add_type_unit (struct dwarf2_per_objfile *dwarf2_per_objfile, ULONGEST sig,
6879 void **slot)
6aa5f3a6
DE
6880{
6881 struct objfile *objfile = dwarf2_per_objfile->objfile;
6882 int n_type_units = dwarf2_per_objfile->n_type_units;
6883 struct signatured_type *sig_type;
6884
6885 gdb_assert (n_type_units <= dwarf2_per_objfile->n_allocated_type_units);
6886 ++n_type_units;
6887 if (n_type_units > dwarf2_per_objfile->n_allocated_type_units)
6888 {
6889 if (dwarf2_per_objfile->n_allocated_type_units == 0)
6890 dwarf2_per_objfile->n_allocated_type_units = 1;
6891 dwarf2_per_objfile->n_allocated_type_units *= 2;
6892 dwarf2_per_objfile->all_type_units
224c3ddb
SM
6893 = XRESIZEVEC (struct signatured_type *,
6894 dwarf2_per_objfile->all_type_units,
6895 dwarf2_per_objfile->n_allocated_type_units);
6aa5f3a6
DE
6896 ++dwarf2_per_objfile->tu_stats.nr_all_type_units_reallocs;
6897 }
6898 dwarf2_per_objfile->n_type_units = n_type_units;
6899
6900 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6901 struct signatured_type);
6902 dwarf2_per_objfile->all_type_units[n_type_units - 1] = sig_type;
6903 sig_type->signature = sig;
6904 sig_type->per_cu.is_debug_types = 1;
6905 if (dwarf2_per_objfile->using_index)
6906 {
6907 sig_type->per_cu.v.quick =
6908 OBSTACK_ZALLOC (&objfile->objfile_obstack,
6909 struct dwarf2_per_cu_quick_data);
6910 }
6911
6912 if (slot == NULL)
6913 {
6914 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
6915 sig_type, INSERT);
6916 }
6917 gdb_assert (*slot == NULL);
6918 *slot = sig_type;
6919 /* The rest of sig_type must be filled in by the caller. */
6920 return sig_type;
6921}
6922
a2ce51a0
DE
6923/* Subroutine of lookup_dwo_signatured_type and lookup_dwp_signatured_type.
6924 Fill in SIG_ENTRY with DWO_ENTRY. */
6925
6926static void
ed2dc618 6927fill_in_sig_entry_from_dwo_entry (struct dwarf2_per_objfile *dwarf2_per_objfile,
a2ce51a0
DE
6928 struct signatured_type *sig_entry,
6929 struct dwo_unit *dwo_entry)
6930{
7ee85ab1 6931 /* Make sure we're not clobbering something we don't expect to. */
a2ce51a0
DE
6932 gdb_assert (! sig_entry->per_cu.queued);
6933 gdb_assert (sig_entry->per_cu.cu == NULL);
6aa5f3a6
DE
6934 if (dwarf2_per_objfile->using_index)
6935 {
6936 gdb_assert (sig_entry->per_cu.v.quick != NULL);
43f3e411 6937 gdb_assert (sig_entry->per_cu.v.quick->compunit_symtab == NULL);
6aa5f3a6
DE
6938 }
6939 else
6940 gdb_assert (sig_entry->per_cu.v.psymtab == NULL);
a2ce51a0 6941 gdb_assert (sig_entry->signature == dwo_entry->signature);
9c541725 6942 gdb_assert (to_underlying (sig_entry->type_offset_in_section) == 0);
a2ce51a0 6943 gdb_assert (sig_entry->type_unit_group == NULL);
7ee85ab1
DE
6944 gdb_assert (sig_entry->dwo_unit == NULL);
6945
6946 sig_entry->per_cu.section = dwo_entry->section;
9c541725 6947 sig_entry->per_cu.sect_off = dwo_entry->sect_off;
7ee85ab1
DE
6948 sig_entry->per_cu.length = dwo_entry->length;
6949 sig_entry->per_cu.reading_dwo_directly = 1;
e3b94546 6950 sig_entry->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
a2ce51a0
DE
6951 sig_entry->type_offset_in_tu = dwo_entry->type_offset_in_tu;
6952 sig_entry->dwo_unit = dwo_entry;
6953}
6954
6955/* Subroutine of lookup_signatured_type.
7ee85ab1
DE
6956 If we haven't read the TU yet, create the signatured_type data structure
6957 for a TU to be read in directly from a DWO file, bypassing the stub.
6958 This is the "Stay in DWO Optimization": When there is no DWP file and we're
6959 using .gdb_index, then when reading a CU we want to stay in the DWO file
6960 containing that CU. Otherwise we could end up reading several other DWO
6961 files (due to comdat folding) to process the transitive closure of all the
6962 mentioned TUs, and that can be slow. The current DWO file will have every
6963 type signature that it needs.
a2ce51a0
DE
6964 We only do this for .gdb_index because in the psymtab case we already have
6965 to read all the DWOs to build the type unit groups. */
6966
6967static struct signatured_type *
6968lookup_dwo_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6969{
518817b3
SM
6970 struct dwarf2_per_objfile *dwarf2_per_objfile
6971 = cu->per_cu->dwarf2_per_objfile;
a2ce51a0
DE
6972 struct objfile *objfile = dwarf2_per_objfile->objfile;
6973 struct dwo_file *dwo_file;
6974 struct dwo_unit find_dwo_entry, *dwo_entry;
6975 struct signatured_type find_sig_entry, *sig_entry;
6aa5f3a6 6976 void **slot;
a2ce51a0
DE
6977
6978 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
6979
6aa5f3a6
DE
6980 /* If TU skeletons have been removed then we may not have read in any
6981 TUs yet. */
6982 if (dwarf2_per_objfile->signatured_types == NULL)
6983 {
6984 dwarf2_per_objfile->signatured_types
6985 = allocate_signatured_type_table (objfile);
6986 }
a2ce51a0
DE
6987
6988 /* We only ever need to read in one copy of a signatured type.
6aa5f3a6
DE
6989 Use the global signatured_types array to do our own comdat-folding
6990 of types. If this is the first time we're reading this TU, and
6991 the TU has an entry in .gdb_index, replace the recorded data from
6992 .gdb_index with this TU. */
a2ce51a0 6993
a2ce51a0 6994 find_sig_entry.signature = sig;
6aa5f3a6
DE
6995 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
6996 &find_sig_entry, INSERT);
9a3c8263 6997 sig_entry = (struct signatured_type *) *slot;
7ee85ab1
DE
6998
6999 /* We can get here with the TU already read, *or* in the process of being
6aa5f3a6
DE
7000 read. Don't reassign the global entry to point to this DWO if that's
7001 the case. Also note that if the TU is already being read, it may not
7002 have come from a DWO, the program may be a mix of Fission-compiled
7003 code and non-Fission-compiled code. */
7004
7005 /* Have we already tried to read this TU?
7006 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
7007 needn't exist in the global table yet). */
7008 if (sig_entry != NULL && sig_entry->per_cu.tu_read)
a2ce51a0
DE
7009 return sig_entry;
7010
6aa5f3a6
DE
7011 /* Note: cu->dwo_unit is the dwo_unit that references this TU, not the
7012 dwo_unit of the TU itself. */
7013 dwo_file = cu->dwo_unit->dwo_file;
7014
a2ce51a0
DE
7015 /* Ok, this is the first time we're reading this TU. */
7016 if (dwo_file->tus == NULL)
7017 return NULL;
7018 find_dwo_entry.signature = sig;
9a3c8263 7019 dwo_entry = (struct dwo_unit *) htab_find (dwo_file->tus, &find_dwo_entry);
a2ce51a0
DE
7020 if (dwo_entry == NULL)
7021 return NULL;
7022
6aa5f3a6
DE
7023 /* If the global table doesn't have an entry for this TU, add one. */
7024 if (sig_entry == NULL)
ed2dc618 7025 sig_entry = add_type_unit (dwarf2_per_objfile, sig, slot);
6aa5f3a6 7026
ed2dc618 7027 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, sig_entry, dwo_entry);
89e63ee4 7028 sig_entry->per_cu.tu_read = 1;
a2ce51a0
DE
7029 return sig_entry;
7030}
7031
a2ce51a0
DE
7032/* Subroutine of lookup_signatured_type.
7033 Look up the type for signature SIG, and if we can't find SIG in .gdb_index
6aa5f3a6
DE
7034 then try the DWP file. If the TU stub (skeleton) has been removed then
7035 it won't be in .gdb_index. */
a2ce51a0
DE
7036
7037static struct signatured_type *
7038lookup_dwp_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
7039{
518817b3
SM
7040 struct dwarf2_per_objfile *dwarf2_per_objfile
7041 = cu->per_cu->dwarf2_per_objfile;
a2ce51a0 7042 struct objfile *objfile = dwarf2_per_objfile->objfile;
ed2dc618 7043 struct dwp_file *dwp_file = get_dwp_file (dwarf2_per_objfile);
a2ce51a0
DE
7044 struct dwo_unit *dwo_entry;
7045 struct signatured_type find_sig_entry, *sig_entry;
6aa5f3a6 7046 void **slot;
a2ce51a0
DE
7047
7048 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
7049 gdb_assert (dwp_file != NULL);
7050
6aa5f3a6
DE
7051 /* If TU skeletons have been removed then we may not have read in any
7052 TUs yet. */
7053 if (dwarf2_per_objfile->signatured_types == NULL)
a2ce51a0 7054 {
6aa5f3a6
DE
7055 dwarf2_per_objfile->signatured_types
7056 = allocate_signatured_type_table (objfile);
a2ce51a0
DE
7057 }
7058
6aa5f3a6
DE
7059 find_sig_entry.signature = sig;
7060 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
7061 &find_sig_entry, INSERT);
9a3c8263 7062 sig_entry = (struct signatured_type *) *slot;
6aa5f3a6
DE
7063
7064 /* Have we already tried to read this TU?
7065 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
7066 needn't exist in the global table yet). */
7067 if (sig_entry != NULL)
7068 return sig_entry;
7069
a2ce51a0
DE
7070 if (dwp_file->tus == NULL)
7071 return NULL;
ed2dc618 7072 dwo_entry = lookup_dwo_unit_in_dwp (dwarf2_per_objfile, dwp_file, NULL,
57d63ce2 7073 sig, 1 /* is_debug_types */);
a2ce51a0
DE
7074 if (dwo_entry == NULL)
7075 return NULL;
7076
ed2dc618
SM
7077 sig_entry = add_type_unit (dwarf2_per_objfile, sig, slot);
7078 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, sig_entry, dwo_entry);
a2ce51a0 7079
a2ce51a0
DE
7080 return sig_entry;
7081}
7082
380bca97 7083/* Lookup a signature based type for DW_FORM_ref_sig8.
5a8b3f62
DE
7084 Returns NULL if signature SIG is not present in the table.
7085 It is up to the caller to complain about this. */
348e048f
DE
7086
7087static struct signatured_type *
a2ce51a0 7088lookup_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
348e048f 7089{
518817b3
SM
7090 struct dwarf2_per_objfile *dwarf2_per_objfile
7091 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 7092
a2ce51a0
DE
7093 if (cu->dwo_unit
7094 && dwarf2_per_objfile->using_index)
7095 {
7096 /* We're in a DWO/DWP file, and we're using .gdb_index.
7097 These cases require special processing. */
ed2dc618 7098 if (get_dwp_file (dwarf2_per_objfile) == NULL)
a2ce51a0
DE
7099 return lookup_dwo_signatured_type (cu, sig);
7100 else
7101 return lookup_dwp_signatured_type (cu, sig);
7102 }
7103 else
7104 {
7105 struct signatured_type find_entry, *entry;
348e048f 7106
a2ce51a0
DE
7107 if (dwarf2_per_objfile->signatured_types == NULL)
7108 return NULL;
7109 find_entry.signature = sig;
9a3c8263
SM
7110 entry = ((struct signatured_type *)
7111 htab_find (dwarf2_per_objfile->signatured_types, &find_entry));
a2ce51a0
DE
7112 return entry;
7113 }
348e048f 7114}
42e7ad6c
DE
7115\f
7116/* Low level DIE reading support. */
348e048f 7117
d85a05f0
DJ
7118/* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
7119
7120static void
7121init_cu_die_reader (struct die_reader_specs *reader,
dee91e82 7122 struct dwarf2_cu *cu,
3019eac3 7123 struct dwarf2_section_info *section,
685af9cd
TT
7124 struct dwo_file *dwo_file,
7125 struct abbrev_table *abbrev_table)
d85a05f0 7126{
fceca515 7127 gdb_assert (section->readin && section->buffer != NULL);
a32a8923 7128 reader->abfd = get_section_bfd_owner (section);
d85a05f0 7129 reader->cu = cu;
3019eac3 7130 reader->dwo_file = dwo_file;
dee91e82
DE
7131 reader->die_section = section;
7132 reader->buffer = section->buffer;
f664829e 7133 reader->buffer_end = section->buffer + section->size;
a2ce51a0 7134 reader->comp_dir = NULL;
685af9cd 7135 reader->abbrev_table = abbrev_table;
d85a05f0
DJ
7136}
7137
b0c7bfa9
DE
7138/* Subroutine of init_cutu_and_read_dies to simplify it.
7139 Read in the rest of a CU/TU top level DIE from DWO_UNIT.
7140 There's just a lot of work to do, and init_cutu_and_read_dies is big enough
7141 already.
7142
7143 STUB_COMP_UNIT_DIE is for the stub DIE, we copy over certain attributes
7144 from it to the DIE in the DWO. If NULL we are skipping the stub.
a2ce51a0
DE
7145 STUB_COMP_DIR is similar to STUB_COMP_UNIT_DIE: When reading a TU directly
7146 from the DWO file, bypassing the stub, it contains the DW_AT_comp_dir
c54a1dd8
DE
7147 attribute of the referencing CU. At most one of STUB_COMP_UNIT_DIE and
7148 STUB_COMP_DIR may be non-NULL.
b0c7bfa9
DE
7149 *RESULT_READER,*RESULT_INFO_PTR,*RESULT_COMP_UNIT_DIE,*RESULT_HAS_CHILDREN
7150 are filled in with the info of the DIE from the DWO file.
685af9cd
TT
7151 *RESULT_DWO_ABBREV_TABLE will be filled in with the abbrev table allocated
7152 from the dwo. Since *RESULT_READER references this abbrev table, it must be
7153 kept around for at least as long as *RESULT_READER.
7154
b0c7bfa9
DE
7155 The result is non-zero if a valid (non-dummy) DIE was found. */
7156
7157static int
7158read_cutu_die_from_dwo (struct dwarf2_per_cu_data *this_cu,
7159 struct dwo_unit *dwo_unit,
b0c7bfa9 7160 struct die_info *stub_comp_unit_die,
a2ce51a0 7161 const char *stub_comp_dir,
b0c7bfa9 7162 struct die_reader_specs *result_reader,
d521ce57 7163 const gdb_byte **result_info_ptr,
b0c7bfa9 7164 struct die_info **result_comp_unit_die,
685af9cd
TT
7165 int *result_has_children,
7166 abbrev_table_up *result_dwo_abbrev_table)
b0c7bfa9 7167{
ed2dc618 7168 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
b0c7bfa9
DE
7169 struct objfile *objfile = dwarf2_per_objfile->objfile;
7170 struct dwarf2_cu *cu = this_cu->cu;
b0c7bfa9 7171 bfd *abfd;
d521ce57 7172 const gdb_byte *begin_info_ptr, *info_ptr;
b0c7bfa9
DE
7173 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
7174 int i,num_extra_attrs;
7175 struct dwarf2_section_info *dwo_abbrev_section;
7176 struct attribute *attr;
7177 struct die_info *comp_unit_die;
7178
b0aeadb3
DE
7179 /* At most one of these may be provided. */
7180 gdb_assert ((stub_comp_unit_die != NULL) + (stub_comp_dir != NULL) <= 1);
a2ce51a0 7181
b0c7bfa9
DE
7182 /* These attributes aren't processed until later:
7183 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
0d60c288
DE
7184 DW_AT_comp_dir is used now, to find the DWO file, but it is also
7185 referenced later. However, these attributes are found in the stub
7186 which we won't have later. In order to not impose this complication
7187 on the rest of the code, we read them here and copy them to the
7188 DWO CU/TU die. */
b0c7bfa9
DE
7189
7190 stmt_list = NULL;
7191 low_pc = NULL;
7192 high_pc = NULL;
7193 ranges = NULL;
7194 comp_dir = NULL;
7195
7196 if (stub_comp_unit_die != NULL)
7197 {
7198 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
7199 DWO file. */
7200 if (! this_cu->is_debug_types)
7201 stmt_list = dwarf2_attr (stub_comp_unit_die, DW_AT_stmt_list, cu);
7202 low_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_low_pc, cu);
7203 high_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_high_pc, cu);
7204 ranges = dwarf2_attr (stub_comp_unit_die, DW_AT_ranges, cu);
7205 comp_dir = dwarf2_attr (stub_comp_unit_die, DW_AT_comp_dir, cu);
7206
7207 /* There should be a DW_AT_addr_base attribute here (if needed).
7208 We need the value before we can process DW_FORM_GNU_addr_index. */
7209 cu->addr_base = 0;
7210 attr = dwarf2_attr (stub_comp_unit_die, DW_AT_GNU_addr_base, cu);
7211 if (attr)
7212 cu->addr_base = DW_UNSND (attr);
7213
7214 /* There should be a DW_AT_ranges_base attribute here (if needed).
7215 We need the value before we can process DW_AT_ranges. */
7216 cu->ranges_base = 0;
7217 attr = dwarf2_attr (stub_comp_unit_die, DW_AT_GNU_ranges_base, cu);
7218 if (attr)
7219 cu->ranges_base = DW_UNSND (attr);
7220 }
a2ce51a0
DE
7221 else if (stub_comp_dir != NULL)
7222 {
7223 /* Reconstruct the comp_dir attribute to simplify the code below. */
8d749320 7224 comp_dir = XOBNEW (&cu->comp_unit_obstack, struct attribute);
a2ce51a0
DE
7225 comp_dir->name = DW_AT_comp_dir;
7226 comp_dir->form = DW_FORM_string;
7227 DW_STRING_IS_CANONICAL (comp_dir) = 0;
7228 DW_STRING (comp_dir) = stub_comp_dir;
7229 }
b0c7bfa9
DE
7230
7231 /* Set up for reading the DWO CU/TU. */
7232 cu->dwo_unit = dwo_unit;
685af9cd 7233 dwarf2_section_info *section = dwo_unit->section;
b0c7bfa9 7234 dwarf2_read_section (objfile, section);
a32a8923 7235 abfd = get_section_bfd_owner (section);
9c541725
PA
7236 begin_info_ptr = info_ptr = (section->buffer
7237 + to_underlying (dwo_unit->sect_off));
b0c7bfa9 7238 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
b0c7bfa9
DE
7239
7240 if (this_cu->is_debug_types)
7241 {
b0c7bfa9
DE
7242 struct signatured_type *sig_type = (struct signatured_type *) this_cu;
7243
ed2dc618
SM
7244 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7245 &cu->header, section,
b0c7bfa9 7246 dwo_abbrev_section,
43988095 7247 info_ptr, rcuh_kind::TYPE);
a2ce51a0 7248 /* This is not an assert because it can be caused by bad debug info. */
43988095 7249 if (sig_type->signature != cu->header.signature)
a2ce51a0
DE
7250 {
7251 error (_("Dwarf Error: signature mismatch %s vs %s while reading"
9d8780f0 7252 " TU at offset %s [in module %s]"),
a2ce51a0 7253 hex_string (sig_type->signature),
43988095 7254 hex_string (cu->header.signature),
9d8780f0 7255 sect_offset_str (dwo_unit->sect_off),
a2ce51a0
DE
7256 bfd_get_filename (abfd));
7257 }
9c541725 7258 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
b0c7bfa9
DE
7259 /* For DWOs coming from DWP files, we don't know the CU length
7260 nor the type's offset in the TU until now. */
7261 dwo_unit->length = get_cu_length (&cu->header);
9c541725 7262 dwo_unit->type_offset_in_tu = cu->header.type_cu_offset_in_tu;
b0c7bfa9
DE
7263
7264 /* Establish the type offset that can be used to lookup the type.
7265 For DWO files, we don't know it until now. */
9c541725
PA
7266 sig_type->type_offset_in_section
7267 = dwo_unit->sect_off + to_underlying (dwo_unit->type_offset_in_tu);
b0c7bfa9
DE
7268 }
7269 else
7270 {
ed2dc618
SM
7271 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7272 &cu->header, section,
b0c7bfa9 7273 dwo_abbrev_section,
43988095 7274 info_ptr, rcuh_kind::COMPILE);
9c541725 7275 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
b0c7bfa9
DE
7276 /* For DWOs coming from DWP files, we don't know the CU length
7277 until now. */
7278 dwo_unit->length = get_cu_length (&cu->header);
7279 }
7280
685af9cd
TT
7281 *result_dwo_abbrev_table
7282 = abbrev_table_read_table (dwarf2_per_objfile, dwo_abbrev_section,
7283 cu->header.abbrev_sect_off);
7284 init_cu_die_reader (result_reader, cu, section, dwo_unit->dwo_file,
7285 result_dwo_abbrev_table->get ());
b0c7bfa9
DE
7286
7287 /* Read in the die, but leave space to copy over the attributes
7288 from the stub. This has the benefit of simplifying the rest of
7289 the code - all the work to maintain the illusion of a single
7290 DW_TAG_{compile,type}_unit DIE is done here. */
7291 num_extra_attrs = ((stmt_list != NULL)
7292 + (low_pc != NULL)
7293 + (high_pc != NULL)
7294 + (ranges != NULL)
7295 + (comp_dir != NULL));
7296 info_ptr = read_full_die_1 (result_reader, result_comp_unit_die, info_ptr,
7297 result_has_children, num_extra_attrs);
7298
7299 /* Copy over the attributes from the stub to the DIE we just read in. */
7300 comp_unit_die = *result_comp_unit_die;
7301 i = comp_unit_die->num_attrs;
7302 if (stmt_list != NULL)
7303 comp_unit_die->attrs[i++] = *stmt_list;
7304 if (low_pc != NULL)
7305 comp_unit_die->attrs[i++] = *low_pc;
7306 if (high_pc != NULL)
7307 comp_unit_die->attrs[i++] = *high_pc;
7308 if (ranges != NULL)
7309 comp_unit_die->attrs[i++] = *ranges;
7310 if (comp_dir != NULL)
7311 comp_unit_die->attrs[i++] = *comp_dir;
7312 comp_unit_die->num_attrs += num_extra_attrs;
7313
b4f54984 7314 if (dwarf_die_debug)
bf6af496
DE
7315 {
7316 fprintf_unfiltered (gdb_stdlog,
7317 "Read die from %s@0x%x of %s:\n",
a32a8923 7318 get_section_name (section),
bf6af496
DE
7319 (unsigned) (begin_info_ptr - section->buffer),
7320 bfd_get_filename (abfd));
b4f54984 7321 dump_die (comp_unit_die, dwarf_die_debug);
bf6af496
DE
7322 }
7323
a2ce51a0
DE
7324 /* Save the comp_dir attribute. If there is no DWP file then we'll read
7325 TUs by skipping the stub and going directly to the entry in the DWO file.
7326 However, skipping the stub means we won't get DW_AT_comp_dir, so we have
7327 to get it via circuitous means. Blech. */
7328 if (comp_dir != NULL)
7329 result_reader->comp_dir = DW_STRING (comp_dir);
7330
b0c7bfa9
DE
7331 /* Skip dummy compilation units. */
7332 if (info_ptr >= begin_info_ptr + dwo_unit->length
7333 || peek_abbrev_code (abfd, info_ptr) == 0)
7334 return 0;
7335
7336 *result_info_ptr = info_ptr;
7337 return 1;
7338}
7339
7340/* Subroutine of init_cutu_and_read_dies to simplify it.
7341 Look up the DWO unit specified by COMP_UNIT_DIE of THIS_CU.
6a506a2d 7342 Returns NULL if the specified DWO unit cannot be found. */
b0c7bfa9
DE
7343
7344static struct dwo_unit *
7345lookup_dwo_unit (struct dwarf2_per_cu_data *this_cu,
7346 struct die_info *comp_unit_die)
7347{
7348 struct dwarf2_cu *cu = this_cu->cu;
b0c7bfa9
DE
7349 ULONGEST signature;
7350 struct dwo_unit *dwo_unit;
7351 const char *comp_dir, *dwo_name;
7352
a2ce51a0
DE
7353 gdb_assert (cu != NULL);
7354
b0c7bfa9 7355 /* Yeah, we look dwo_name up again, but it simplifies the code. */
7d45c7c3
KB
7356 dwo_name = dwarf2_string_attr (comp_unit_die, DW_AT_GNU_dwo_name, cu);
7357 comp_dir = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
b0c7bfa9
DE
7358
7359 if (this_cu->is_debug_types)
7360 {
7361 struct signatured_type *sig_type;
7362
7363 /* Since this_cu is the first member of struct signatured_type,
7364 we can go from a pointer to one to a pointer to the other. */
7365 sig_type = (struct signatured_type *) this_cu;
7366 signature = sig_type->signature;
7367 dwo_unit = lookup_dwo_type_unit (sig_type, dwo_name, comp_dir);
7368 }
7369 else
7370 {
7371 struct attribute *attr;
7372
7373 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
7374 if (! attr)
7375 error (_("Dwarf Error: missing dwo_id for dwo_name %s"
7376 " [in module %s]"),
e3b94546 7377 dwo_name, objfile_name (this_cu->dwarf2_per_objfile->objfile));
b0c7bfa9
DE
7378 signature = DW_UNSND (attr);
7379 dwo_unit = lookup_dwo_comp_unit (this_cu, dwo_name, comp_dir,
7380 signature);
7381 }
7382
b0c7bfa9
DE
7383 return dwo_unit;
7384}
7385
a2ce51a0 7386/* Subroutine of init_cutu_and_read_dies to simplify it.
6aa5f3a6 7387 See it for a description of the parameters.
fcd3b13d 7388 Read a TU directly from a DWO file, bypassing the stub. */
a2ce51a0
DE
7389
7390static void
6aa5f3a6
DE
7391init_tu_and_read_dwo_dies (struct dwarf2_per_cu_data *this_cu,
7392 int use_existing_cu, int keep,
a2ce51a0
DE
7393 die_reader_func_ftype *die_reader_func,
7394 void *data)
7395{
fcd3b13d 7396 std::unique_ptr<dwarf2_cu> new_cu;
a2ce51a0 7397 struct signatured_type *sig_type;
a2ce51a0
DE
7398 struct die_reader_specs reader;
7399 const gdb_byte *info_ptr;
7400 struct die_info *comp_unit_die;
7401 int has_children;
ed2dc618 7402 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
a2ce51a0
DE
7403
7404 /* Verify we can do the following downcast, and that we have the
7405 data we need. */
7406 gdb_assert (this_cu->is_debug_types && this_cu->reading_dwo_directly);
7407 sig_type = (struct signatured_type *) this_cu;
7408 gdb_assert (sig_type->dwo_unit != NULL);
7409
6aa5f3a6
DE
7410 if (use_existing_cu && this_cu->cu != NULL)
7411 {
7412 gdb_assert (this_cu->cu->dwo_unit == sig_type->dwo_unit);
6aa5f3a6
DE
7413 /* There's no need to do the rereading_dwo_cu handling that
7414 init_cutu_and_read_dies does since we don't read the stub. */
7415 }
7416 else
7417 {
7418 /* If !use_existing_cu, this_cu->cu must be NULL. */
7419 gdb_assert (this_cu->cu == NULL);
fcd3b13d 7420 new_cu.reset (new dwarf2_cu (this_cu));
6aa5f3a6
DE
7421 }
7422
7423 /* A future optimization, if needed, would be to use an existing
7424 abbrev table. When reading DWOs with skeletonless TUs, all the TUs
7425 could share abbrev tables. */
a2ce51a0 7426
685af9cd
TT
7427 /* The abbreviation table used by READER, this must live at least as long as
7428 READER. */
7429 abbrev_table_up dwo_abbrev_table;
7430
a2ce51a0 7431 if (read_cutu_die_from_dwo (this_cu, sig_type->dwo_unit,
a2ce51a0
DE
7432 NULL /* stub_comp_unit_die */,
7433 sig_type->dwo_unit->dwo_file->comp_dir,
7434 &reader, &info_ptr,
685af9cd
TT
7435 &comp_unit_die, &has_children,
7436 &dwo_abbrev_table) == 0)
a2ce51a0
DE
7437 {
7438 /* Dummy die. */
a2ce51a0
DE
7439 return;
7440 }
7441
7442 /* All the "real" work is done here. */
7443 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
7444
6aa5f3a6 7445 /* This duplicates the code in init_cutu_and_read_dies,
a2ce51a0
DE
7446 but the alternative is making the latter more complex.
7447 This function is only for the special case of using DWO files directly:
7448 no point in overly complicating the general case just to handle this. */
fcd3b13d 7449 if (new_cu != NULL && keep)
a2ce51a0 7450 {
fcd3b13d
SM
7451 /* Link this CU into read_in_chain. */
7452 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
7453 dwarf2_per_objfile->read_in_chain = this_cu;
7454 /* The chain owns it now. */
7455 new_cu.release ();
a2ce51a0 7456 }
a2ce51a0
DE
7457}
7458
fd820528 7459/* Initialize a CU (or TU) and read its DIEs.
3019eac3 7460 If the CU defers to a DWO file, read the DWO file as well.
dee91e82 7461
f4dc4d17
DE
7462 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
7463 Otherwise the table specified in the comp unit header is read in and used.
7464 This is an optimization for when we already have the abbrev table.
7465
dee91e82
DE
7466 If USE_EXISTING_CU is non-zero, and THIS_CU->cu is non-NULL, then use it.
7467 Otherwise, a new CU is allocated with xmalloc.
7468
7469 If KEEP is non-zero, then if we allocated a dwarf2_cu we add it to
7470 read_in_chain. Otherwise the dwarf2_cu data is freed at the end.
7471
7472 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
fd820528 7473 linker) then DIE_READER_FUNC will not get called. */
aaa75496 7474
70221824 7475static void
fd820528 7476init_cutu_and_read_dies (struct dwarf2_per_cu_data *this_cu,
f4dc4d17 7477 struct abbrev_table *abbrev_table,
fd820528
DE
7478 int use_existing_cu, int keep,
7479 die_reader_func_ftype *die_reader_func,
7480 void *data)
c906108c 7481{
ed2dc618 7482 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
dee91e82 7483 struct objfile *objfile = dwarf2_per_objfile->objfile;
8a0459fd 7484 struct dwarf2_section_info *section = this_cu->section;
a32a8923 7485 bfd *abfd = get_section_bfd_owner (section);
dee91e82 7486 struct dwarf2_cu *cu;
d521ce57 7487 const gdb_byte *begin_info_ptr, *info_ptr;
dee91e82 7488 struct die_reader_specs reader;
d85a05f0 7489 struct die_info *comp_unit_die;
dee91e82 7490 int has_children;
d85a05f0 7491 struct attribute *attr;
dee91e82 7492 struct signatured_type *sig_type = NULL;
4bdcc0c1 7493 struct dwarf2_section_info *abbrev_section;
42e7ad6c
DE
7494 /* Non-zero if CU currently points to a DWO file and we need to
7495 reread it. When this happens we need to reread the skeleton die
a2ce51a0 7496 before we can reread the DWO file (this only applies to CUs, not TUs). */
42e7ad6c 7497 int rereading_dwo_cu = 0;
c906108c 7498
b4f54984 7499 if (dwarf_die_debug)
9d8780f0 7500 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
09406207 7501 this_cu->is_debug_types ? "type" : "comp",
9d8780f0 7502 sect_offset_str (this_cu->sect_off));
09406207 7503
dee91e82
DE
7504 if (use_existing_cu)
7505 gdb_assert (keep);
23745b47 7506
a2ce51a0
DE
7507 /* If we're reading a TU directly from a DWO file, including a virtual DWO
7508 file (instead of going through the stub), short-circuit all of this. */
7509 if (this_cu->reading_dwo_directly)
7510 {
7511 /* Narrow down the scope of possibilities to have to understand. */
7512 gdb_assert (this_cu->is_debug_types);
7513 gdb_assert (abbrev_table == NULL);
6aa5f3a6
DE
7514 init_tu_and_read_dwo_dies (this_cu, use_existing_cu, keep,
7515 die_reader_func, data);
a2ce51a0
DE
7516 return;
7517 }
7518
dee91e82
DE
7519 /* This is cheap if the section is already read in. */
7520 dwarf2_read_section (objfile, section);
7521
9c541725 7522 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
36586728
TT
7523
7524 abbrev_section = get_abbrev_section_for_cu (this_cu);
dee91e82 7525
fcd3b13d 7526 std::unique_ptr<dwarf2_cu> new_cu;
dee91e82
DE
7527 if (use_existing_cu && this_cu->cu != NULL)
7528 {
7529 cu = this_cu->cu;
42e7ad6c
DE
7530 /* If this CU is from a DWO file we need to start over, we need to
7531 refetch the attributes from the skeleton CU.
7532 This could be optimized by retrieving those attributes from when we
7533 were here the first time: the previous comp_unit_die was stored in
7534 comp_unit_obstack. But there's no data yet that we need this
7535 optimization. */
7536 if (cu->dwo_unit != NULL)
7537 rereading_dwo_cu = 1;
dee91e82
DE
7538 }
7539 else
7540 {
7541 /* If !use_existing_cu, this_cu->cu must be NULL. */
7542 gdb_assert (this_cu->cu == NULL);
fcd3b13d
SM
7543 new_cu.reset (new dwarf2_cu (this_cu));
7544 cu = new_cu.get ();
42e7ad6c 7545 }
dee91e82 7546
b0c7bfa9 7547 /* Get the header. */
9c541725 7548 if (to_underlying (cu->header.first_die_cu_offset) != 0 && !rereading_dwo_cu)
42e7ad6c
DE
7549 {
7550 /* We already have the header, there's no need to read it in again. */
9c541725 7551 info_ptr += to_underlying (cu->header.first_die_cu_offset);
42e7ad6c
DE
7552 }
7553 else
7554 {
3019eac3 7555 if (this_cu->is_debug_types)
dee91e82 7556 {
ed2dc618
SM
7557 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7558 &cu->header, section,
4bdcc0c1 7559 abbrev_section, info_ptr,
43988095 7560 rcuh_kind::TYPE);
dee91e82 7561
42e7ad6c
DE
7562 /* Since per_cu is the first member of struct signatured_type,
7563 we can go from a pointer to one to a pointer to the other. */
7564 sig_type = (struct signatured_type *) this_cu;
43988095 7565 gdb_assert (sig_type->signature == cu->header.signature);
9c541725
PA
7566 gdb_assert (sig_type->type_offset_in_tu
7567 == cu->header.type_cu_offset_in_tu);
7568 gdb_assert (this_cu->sect_off == cu->header.sect_off);
dee91e82 7569
42e7ad6c
DE
7570 /* LENGTH has not been set yet for type units if we're
7571 using .gdb_index. */
1ce1cefd 7572 this_cu->length = get_cu_length (&cu->header);
3019eac3
DE
7573
7574 /* Establish the type offset that can be used to lookup the type. */
9c541725
PA
7575 sig_type->type_offset_in_section =
7576 this_cu->sect_off + to_underlying (sig_type->type_offset_in_tu);
43988095
JK
7577
7578 this_cu->dwarf_version = cu->header.version;
dee91e82
DE
7579 }
7580 else
7581 {
ed2dc618
SM
7582 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7583 &cu->header, section,
4bdcc0c1 7584 abbrev_section,
43988095
JK
7585 info_ptr,
7586 rcuh_kind::COMPILE);
dee91e82 7587
9c541725 7588 gdb_assert (this_cu->sect_off == cu->header.sect_off);
1ce1cefd 7589 gdb_assert (this_cu->length == get_cu_length (&cu->header));
43988095 7590 this_cu->dwarf_version = cu->header.version;
dee91e82
DE
7591 }
7592 }
10b3939b 7593
6caca83c 7594 /* Skip dummy compilation units. */
dee91e82 7595 if (info_ptr >= begin_info_ptr + this_cu->length
6caca83c 7596 || peek_abbrev_code (abfd, info_ptr) == 0)
fcd3b13d 7597 return;
6caca83c 7598
433df2d4
DE
7599 /* If we don't have them yet, read the abbrevs for this compilation unit.
7600 And if we need to read them now, make sure they're freed when we're
685af9cd
TT
7601 done (own the table through ABBREV_TABLE_HOLDER). */
7602 abbrev_table_up abbrev_table_holder;
f4dc4d17 7603 if (abbrev_table != NULL)
685af9cd
TT
7604 gdb_assert (cu->header.abbrev_sect_off == abbrev_table->sect_off);
7605 else
f4dc4d17 7606 {
685af9cd
TT
7607 abbrev_table_holder
7608 = abbrev_table_read_table (dwarf2_per_objfile, abbrev_section,
7609 cu->header.abbrev_sect_off);
7610 abbrev_table = abbrev_table_holder.get ();
42e7ad6c 7611 }
af703f96 7612
dee91e82 7613 /* Read the top level CU/TU die. */
685af9cd 7614 init_cu_die_reader (&reader, cu, section, NULL, abbrev_table);
dee91e82 7615 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
93311388 7616
b0c7bfa9 7617 /* If we are in a DWO stub, process it and then read in the "real" CU/TU
685af9cd
TT
7618 from the DWO file. read_cutu_die_from_dwo will allocate the abbreviation
7619 table from the DWO file and pass the ownership over to us. It will be
7620 referenced from READER, so we must make sure to free it after we're done
7621 with READER.
7622
b0c7bfa9
DE
7623 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains a
7624 DWO CU, that this test will fail (the attribute will not be present). */
3019eac3 7625 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_name, cu);
685af9cd 7626 abbrev_table_up dwo_abbrev_table;
3019eac3
DE
7627 if (attr)
7628 {
3019eac3 7629 struct dwo_unit *dwo_unit;
b0c7bfa9 7630 struct die_info *dwo_comp_unit_die;
3019eac3
DE
7631
7632 if (has_children)
6a506a2d
DE
7633 {
7634 complaint (&symfile_complaints,
7635 _("compilation unit with DW_AT_GNU_dwo_name"
9d8780f0
SM
7636 " has children (offset %s) [in module %s]"),
7637 sect_offset_str (this_cu->sect_off),
7638 bfd_get_filename (abfd));
6a506a2d 7639 }
b0c7bfa9 7640 dwo_unit = lookup_dwo_unit (this_cu, comp_unit_die);
6a506a2d 7641 if (dwo_unit != NULL)
3019eac3 7642 {
6a506a2d 7643 if (read_cutu_die_from_dwo (this_cu, dwo_unit,
a2ce51a0 7644 comp_unit_die, NULL,
6a506a2d 7645 &reader, &info_ptr,
685af9cd
TT
7646 &dwo_comp_unit_die, &has_children,
7647 &dwo_abbrev_table) == 0)
6a506a2d
DE
7648 {
7649 /* Dummy die. */
6a506a2d
DE
7650 return;
7651 }
7652 comp_unit_die = dwo_comp_unit_die;
7653 }
7654 else
7655 {
7656 /* Yikes, we couldn't find the rest of the DIE, we only have
7657 the stub. A complaint has already been logged. There's
7658 not much more we can do except pass on the stub DIE to
7659 die_reader_func. We don't want to throw an error on bad
7660 debug info. */
3019eac3
DE
7661 }
7662 }
7663
b0c7bfa9 7664 /* All of the above is setup for this call. Yikes. */
dee91e82
DE
7665 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
7666
b0c7bfa9 7667 /* Done, clean up. */
fcd3b13d 7668 if (new_cu != NULL && keep)
348e048f 7669 {
fcd3b13d
SM
7670 /* Link this CU into read_in_chain. */
7671 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
7672 dwarf2_per_objfile->read_in_chain = this_cu;
7673 /* The chain owns it now. */
7674 new_cu.release ();
348e048f 7675 }
dee91e82
DE
7676}
7677
33e80786
DE
7678/* Read CU/TU THIS_CU but do not follow DW_AT_GNU_dwo_name if present.
7679 DWO_FILE, if non-NULL, is the DWO file to read (the caller is assumed
7680 to have already done the lookup to find the DWO file).
dee91e82
DE
7681
7682 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
3019eac3 7683 THIS_CU->is_debug_types, but nothing else.
dee91e82
DE
7684
7685 We fill in THIS_CU->length.
7686
7687 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
7688 linker) then DIE_READER_FUNC will not get called.
7689
7690 THIS_CU->cu is always freed when done.
3019eac3
DE
7691 This is done in order to not leave THIS_CU->cu in a state where we have
7692 to care whether it refers to the "main" CU or the DWO CU. */
dee91e82
DE
7693
7694static void
7695init_cutu_and_read_dies_no_follow (struct dwarf2_per_cu_data *this_cu,
3019eac3 7696 struct dwo_file *dwo_file,
dee91e82
DE
7697 die_reader_func_ftype *die_reader_func,
7698 void *data)
7699{
ed2dc618 7700 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
dee91e82 7701 struct objfile *objfile = dwarf2_per_objfile->objfile;
8a0459fd 7702 struct dwarf2_section_info *section = this_cu->section;
a32a8923 7703 bfd *abfd = get_section_bfd_owner (section);
33e80786 7704 struct dwarf2_section_info *abbrev_section;
d521ce57 7705 const gdb_byte *begin_info_ptr, *info_ptr;
dee91e82 7706 struct die_reader_specs reader;
dee91e82
DE
7707 struct die_info *comp_unit_die;
7708 int has_children;
7709
b4f54984 7710 if (dwarf_die_debug)
9d8780f0 7711 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
09406207 7712 this_cu->is_debug_types ? "type" : "comp",
9d8780f0 7713 sect_offset_str (this_cu->sect_off));
09406207 7714
dee91e82
DE
7715 gdb_assert (this_cu->cu == NULL);
7716
33e80786
DE
7717 abbrev_section = (dwo_file != NULL
7718 ? &dwo_file->sections.abbrev
7719 : get_abbrev_section_for_cu (this_cu));
7720
dee91e82
DE
7721 /* This is cheap if the section is already read in. */
7722 dwarf2_read_section (objfile, section);
7723
fcd3b13d 7724 struct dwarf2_cu cu (this_cu);
dee91e82 7725
9c541725 7726 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
ed2dc618
SM
7727 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7728 &cu.header, section,
4bdcc0c1 7729 abbrev_section, info_ptr,
43988095
JK
7730 (this_cu->is_debug_types
7731 ? rcuh_kind::TYPE
7732 : rcuh_kind::COMPILE));
dee91e82 7733
1ce1cefd 7734 this_cu->length = get_cu_length (&cu.header);
dee91e82
DE
7735
7736 /* Skip dummy compilation units. */
7737 if (info_ptr >= begin_info_ptr + this_cu->length
7738 || peek_abbrev_code (abfd, info_ptr) == 0)
fcd3b13d 7739 return;
72bf9492 7740
685af9cd
TT
7741 abbrev_table_up abbrev_table
7742 = abbrev_table_read_table (dwarf2_per_objfile, abbrev_section,
7743 cu.header.abbrev_sect_off);
dee91e82 7744
685af9cd 7745 init_cu_die_reader (&reader, &cu, section, dwo_file, abbrev_table.get ());
dee91e82
DE
7746 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
7747
7748 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
dee91e82
DE
7749}
7750
3019eac3
DE
7751/* Read a CU/TU, except that this does not look for DW_AT_GNU_dwo_name and
7752 does not lookup the specified DWO file.
7753 This cannot be used to read DWO files.
dee91e82
DE
7754
7755 THIS_CU->cu is always freed when done.
3019eac3
DE
7756 This is done in order to not leave THIS_CU->cu in a state where we have
7757 to care whether it refers to the "main" CU or the DWO CU.
7758 We can revisit this if the data shows there's a performance issue. */
dee91e82
DE
7759
7760static void
7761init_cutu_and_read_dies_simple (struct dwarf2_per_cu_data *this_cu,
7762 die_reader_func_ftype *die_reader_func,
7763 void *data)
7764{
33e80786 7765 init_cutu_and_read_dies_no_follow (this_cu, NULL, die_reader_func, data);
dee91e82 7766}
0018ea6f
DE
7767\f
7768/* Type Unit Groups.
dee91e82 7769
0018ea6f
DE
7770 Type Unit Groups are a way to collapse the set of all TUs (type units) into
7771 a more manageable set. The grouping is done by DW_AT_stmt_list entry
7772 so that all types coming from the same compilation (.o file) are grouped
7773 together. A future step could be to put the types in the same symtab as
7774 the CU the types ultimately came from. */
ff013f42 7775
f4dc4d17
DE
7776static hashval_t
7777hash_type_unit_group (const void *item)
7778{
9a3c8263
SM
7779 const struct type_unit_group *tu_group
7780 = (const struct type_unit_group *) item;
f4dc4d17 7781
094b34ac 7782 return hash_stmt_list_entry (&tu_group->hash);
f4dc4d17 7783}
348e048f
DE
7784
7785static int
f4dc4d17 7786eq_type_unit_group (const void *item_lhs, const void *item_rhs)
348e048f 7787{
9a3c8263
SM
7788 const struct type_unit_group *lhs = (const struct type_unit_group *) item_lhs;
7789 const struct type_unit_group *rhs = (const struct type_unit_group *) item_rhs;
348e048f 7790
094b34ac 7791 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
f4dc4d17 7792}
348e048f 7793
f4dc4d17
DE
7794/* Allocate a hash table for type unit groups. */
7795
7796static htab_t
ed2dc618 7797allocate_type_unit_groups_table (struct objfile *objfile)
f4dc4d17
DE
7798{
7799 return htab_create_alloc_ex (3,
7800 hash_type_unit_group,
7801 eq_type_unit_group,
7802 NULL,
ed2dc618 7803 &objfile->objfile_obstack,
f4dc4d17
DE
7804 hashtab_obstack_allocate,
7805 dummy_obstack_deallocate);
7806}
dee91e82 7807
f4dc4d17
DE
7808/* Type units that don't have DW_AT_stmt_list are grouped into their own
7809 partial symtabs. We combine several TUs per psymtab to not let the size
7810 of any one psymtab grow too big. */
7811#define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
7812#define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
dee91e82 7813
094b34ac 7814/* Helper routine for get_type_unit_group.
f4dc4d17
DE
7815 Create the type_unit_group object used to hold one or more TUs. */
7816
7817static struct type_unit_group *
094b34ac 7818create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
f4dc4d17 7819{
518817b3
SM
7820 struct dwarf2_per_objfile *dwarf2_per_objfile
7821 = cu->per_cu->dwarf2_per_objfile;
f4dc4d17 7822 struct objfile *objfile = dwarf2_per_objfile->objfile;
094b34ac 7823 struct dwarf2_per_cu_data *per_cu;
f4dc4d17 7824 struct type_unit_group *tu_group;
f4dc4d17
DE
7825
7826 tu_group = OBSTACK_ZALLOC (&objfile->objfile_obstack,
7827 struct type_unit_group);
094b34ac 7828 per_cu = &tu_group->per_cu;
518817b3 7829 per_cu->dwarf2_per_objfile = dwarf2_per_objfile;
f4dc4d17 7830
094b34ac
DE
7831 if (dwarf2_per_objfile->using_index)
7832 {
7833 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
7834 struct dwarf2_per_cu_quick_data);
094b34ac
DE
7835 }
7836 else
7837 {
9c541725 7838 unsigned int line_offset = to_underlying (line_offset_struct);
094b34ac
DE
7839 struct partial_symtab *pst;
7840 char *name;
7841
7842 /* Give the symtab a useful name for debug purposes. */
7843 if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
7844 name = xstrprintf ("<type_units_%d>",
7845 (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
7846 else
7847 name = xstrprintf ("<type_units_at_0x%x>", line_offset);
7848
7849 pst = create_partial_symtab (per_cu, name);
7850 pst->anonymous = 1;
f4dc4d17 7851
094b34ac
DE
7852 xfree (name);
7853 }
f4dc4d17 7854
094b34ac 7855 tu_group->hash.dwo_unit = cu->dwo_unit;
9c541725 7856 tu_group->hash.line_sect_off = line_offset_struct;
f4dc4d17
DE
7857
7858 return tu_group;
7859}
7860
094b34ac
DE
7861/* Look up the type_unit_group for type unit CU, and create it if necessary.
7862 STMT_LIST is a DW_AT_stmt_list attribute. */
f4dc4d17
DE
7863
7864static struct type_unit_group *
ff39bb5e 7865get_type_unit_group (struct dwarf2_cu *cu, const struct attribute *stmt_list)
f4dc4d17 7866{
518817b3
SM
7867 struct dwarf2_per_objfile *dwarf2_per_objfile
7868 = cu->per_cu->dwarf2_per_objfile;
f4dc4d17
DE
7869 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
7870 struct type_unit_group *tu_group;
7871 void **slot;
7872 unsigned int line_offset;
7873 struct type_unit_group type_unit_group_for_lookup;
7874
7875 if (dwarf2_per_objfile->type_unit_groups == NULL)
7876 {
7877 dwarf2_per_objfile->type_unit_groups =
ed2dc618 7878 allocate_type_unit_groups_table (dwarf2_per_objfile->objfile);
f4dc4d17
DE
7879 }
7880
7881 /* Do we need to create a new group, or can we use an existing one? */
7882
7883 if (stmt_list)
7884 {
7885 line_offset = DW_UNSND (stmt_list);
7886 ++tu_stats->nr_symtab_sharers;
7887 }
7888 else
7889 {
7890 /* Ugh, no stmt_list. Rare, but we have to handle it.
7891 We can do various things here like create one group per TU or
7892 spread them over multiple groups to split up the expansion work.
7893 To avoid worst case scenarios (too many groups or too large groups)
7894 we, umm, group them in bunches. */
7895 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
7896 | (tu_stats->nr_stmt_less_type_units
7897 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
7898 ++tu_stats->nr_stmt_less_type_units;
7899 }
7900
094b34ac 7901 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
9c541725 7902 type_unit_group_for_lookup.hash.line_sect_off = (sect_offset) line_offset;
f4dc4d17
DE
7903 slot = htab_find_slot (dwarf2_per_objfile->type_unit_groups,
7904 &type_unit_group_for_lookup, INSERT);
7905 if (*slot != NULL)
7906 {
9a3c8263 7907 tu_group = (struct type_unit_group *) *slot;
f4dc4d17
DE
7908 gdb_assert (tu_group != NULL);
7909 }
7910 else
7911 {
9c541725 7912 sect_offset line_offset_struct = (sect_offset) line_offset;
094b34ac 7913 tu_group = create_type_unit_group (cu, line_offset_struct);
f4dc4d17
DE
7914 *slot = tu_group;
7915 ++tu_stats->nr_symtabs;
7916 }
7917
7918 return tu_group;
7919}
0018ea6f
DE
7920\f
7921/* Partial symbol tables. */
7922
7923/* Create a psymtab named NAME and assign it to PER_CU.
7924
7925 The caller must fill in the following details:
7926 dirname, textlow, texthigh. */
7927
7928static struct partial_symtab *
7929create_partial_symtab (struct dwarf2_per_cu_data *per_cu, const char *name)
7930{
e3b94546 7931 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
0018ea6f
DE
7932 struct partial_symtab *pst;
7933
18a94d75 7934 pst = start_psymtab_common (objfile, name, 0,
af5bf4ad
SM
7935 objfile->global_psymbols,
7936 objfile->static_psymbols);
0018ea6f
DE
7937
7938 pst->psymtabs_addrmap_supported = 1;
7939
7940 /* This is the glue that links PST into GDB's symbol API. */
7941 pst->read_symtab_private = per_cu;
7942 pst->read_symtab = dwarf2_read_symtab;
7943 per_cu->v.psymtab = pst;
7944
7945 return pst;
7946}
7947
b93601f3
TT
7948/* The DATA object passed to process_psymtab_comp_unit_reader has this
7949 type. */
7950
7951struct process_psymtab_comp_unit_data
7952{
7953 /* True if we are reading a DW_TAG_partial_unit. */
7954
7955 int want_partial_unit;
7956
7957 /* The "pretend" language that is used if the CU doesn't declare a
7958 language. */
7959
7960 enum language pretend_language;
7961};
7962
0018ea6f
DE
7963/* die_reader_func for process_psymtab_comp_unit. */
7964
7965static void
7966process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
d521ce57 7967 const gdb_byte *info_ptr,
0018ea6f
DE
7968 struct die_info *comp_unit_die,
7969 int has_children,
7970 void *data)
7971{
7972 struct dwarf2_cu *cu = reader->cu;
518817b3 7973 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
3e29f34a 7974 struct gdbarch *gdbarch = get_objfile_arch (objfile);
0018ea6f 7975 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
0018ea6f
DE
7976 CORE_ADDR baseaddr;
7977 CORE_ADDR best_lowpc = 0, best_highpc = 0;
7978 struct partial_symtab *pst;
3a2b436a 7979 enum pc_bounds_kind cu_bounds_kind;
0018ea6f 7980 const char *filename;
9a3c8263
SM
7981 struct process_psymtab_comp_unit_data *info
7982 = (struct process_psymtab_comp_unit_data *) data;
0018ea6f 7983
b93601f3 7984 if (comp_unit_die->tag == DW_TAG_partial_unit && !info->want_partial_unit)
0018ea6f
DE
7985 return;
7986
7987 gdb_assert (! per_cu->is_debug_types);
7988
b93601f3 7989 prepare_one_comp_unit (cu, comp_unit_die, info->pretend_language);
0018ea6f
DE
7990
7991 cu->list_in_scope = &file_symbols;
7992
7993 /* Allocate a new partial symbol table structure. */
7d45c7c3
KB
7994 filename = dwarf2_string_attr (comp_unit_die, DW_AT_name, cu);
7995 if (filename == NULL)
0018ea6f 7996 filename = "";
0018ea6f
DE
7997
7998 pst = create_partial_symtab (per_cu, filename);
7999
8000 /* This must be done before calling dwarf2_build_include_psymtabs. */
7d45c7c3 8001 pst->dirname = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
0018ea6f
DE
8002
8003 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
8004
8005 dwarf2_find_base_address (comp_unit_die, cu);
8006
8007 /* Possibly set the default values of LOWPC and HIGHPC from
8008 `DW_AT_ranges'. */
3a2b436a
JK
8009 cu_bounds_kind = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
8010 &best_highpc, cu, pst);
8011 if (cu_bounds_kind == PC_BOUNDS_HIGH_LOW && best_lowpc < best_highpc)
0018ea6f
DE
8012 /* Store the contiguous range if it is not empty; it can be empty for
8013 CUs with no code. */
8014 addrmap_set_empty (objfile->psymtabs_addrmap,
3e29f34a
MR
8015 gdbarch_adjust_dwarf2_addr (gdbarch,
8016 best_lowpc + baseaddr),
8017 gdbarch_adjust_dwarf2_addr (gdbarch,
8018 best_highpc + baseaddr) - 1,
8019 pst);
0018ea6f
DE
8020
8021 /* Check if comp unit has_children.
8022 If so, read the rest of the partial symbols from this comp unit.
8023 If not, there's no more debug_info for this comp unit. */
8024 if (has_children)
8025 {
8026 struct partial_die_info *first_die;
8027 CORE_ADDR lowpc, highpc;
8028
8029 lowpc = ((CORE_ADDR) -1);
8030 highpc = ((CORE_ADDR) 0);
8031
8032 first_die = load_partial_dies (reader, info_ptr, 1);
8033
8034 scan_partial_symbols (first_die, &lowpc, &highpc,
e385593e 8035 cu_bounds_kind <= PC_BOUNDS_INVALID, cu);
0018ea6f
DE
8036
8037 /* If we didn't find a lowpc, set it to highpc to avoid
8038 complaints from `maint check'. */
8039 if (lowpc == ((CORE_ADDR) -1))
8040 lowpc = highpc;
8041
8042 /* If the compilation unit didn't have an explicit address range,
8043 then use the information extracted from its child dies. */
e385593e 8044 if (cu_bounds_kind <= PC_BOUNDS_INVALID)
0018ea6f
DE
8045 {
8046 best_lowpc = lowpc;
8047 best_highpc = highpc;
8048 }
8049 }
3e29f34a
MR
8050 pst->textlow = gdbarch_adjust_dwarf2_addr (gdbarch, best_lowpc + baseaddr);
8051 pst->texthigh = gdbarch_adjust_dwarf2_addr (gdbarch, best_highpc + baseaddr);
0018ea6f 8052
8763cede 8053 end_psymtab_common (objfile, pst);
0018ea6f
DE
8054
8055 if (!VEC_empty (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs))
8056 {
8057 int i;
8058 int len = VEC_length (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs);
8059 struct dwarf2_per_cu_data *iter;
8060
8061 /* Fill in 'dependencies' here; we fill in 'users' in a
8062 post-pass. */
8063 pst->number_of_dependencies = len;
8d749320
SM
8064 pst->dependencies =
8065 XOBNEWVEC (&objfile->objfile_obstack, struct partial_symtab *, len);
0018ea6f
DE
8066 for (i = 0;
8067 VEC_iterate (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs,
8068 i, iter);
8069 ++i)
8070 pst->dependencies[i] = iter->v.psymtab;
8071
8072 VEC_free (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs);
8073 }
8074
8075 /* Get the list of files included in the current compilation unit,
8076 and build a psymtab for each of them. */
8077 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
8078
b4f54984 8079 if (dwarf_read_debug)
0018ea6f
DE
8080 {
8081 struct gdbarch *gdbarch = get_objfile_arch (objfile);
8082
8083 fprintf_unfiltered (gdb_stdlog,
9d8780f0 8084 "Psymtab for %s unit @%s: %s - %s"
0018ea6f
DE
8085 ", %d global, %d static syms\n",
8086 per_cu->is_debug_types ? "type" : "comp",
9d8780f0 8087 sect_offset_str (per_cu->sect_off),
0018ea6f
DE
8088 paddress (gdbarch, pst->textlow),
8089 paddress (gdbarch, pst->texthigh),
8090 pst->n_global_syms, pst->n_static_syms);
8091 }
8092}
8093
8094/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
8095 Process compilation unit THIS_CU for a psymtab. */
8096
8097static void
8098process_psymtab_comp_unit (struct dwarf2_per_cu_data *this_cu,
b93601f3
TT
8099 int want_partial_unit,
8100 enum language pretend_language)
0018ea6f
DE
8101{
8102 /* If this compilation unit was already read in, free the
8103 cached copy in order to read it in again. This is
8104 necessary because we skipped some symbols when we first
8105 read in the compilation unit (see load_partial_dies).
8106 This problem could be avoided, but the benefit is unclear. */
8107 if (this_cu->cu != NULL)
8108 free_one_cached_comp_unit (this_cu);
8109
f1902523
JK
8110 if (this_cu->is_debug_types)
8111 init_cutu_and_read_dies (this_cu, NULL, 0, 0, build_type_psymtabs_reader,
8112 NULL);
8113 else
8114 {
8115 process_psymtab_comp_unit_data info;
8116 info.want_partial_unit = want_partial_unit;
8117 info.pretend_language = pretend_language;
8118 init_cutu_and_read_dies (this_cu, NULL, 0, 0,
8119 process_psymtab_comp_unit_reader, &info);
8120 }
0018ea6f
DE
8121
8122 /* Age out any secondary CUs. */
ed2dc618 8123 age_cached_comp_units (this_cu->dwarf2_per_objfile);
0018ea6f 8124}
f4dc4d17
DE
8125
8126/* Reader function for build_type_psymtabs. */
8127
8128static void
8129build_type_psymtabs_reader (const struct die_reader_specs *reader,
d521ce57 8130 const gdb_byte *info_ptr,
f4dc4d17
DE
8131 struct die_info *type_unit_die,
8132 int has_children,
8133 void *data)
8134{
ed2dc618 8135 struct dwarf2_per_objfile *dwarf2_per_objfile
518817b3 8136 = reader->cu->per_cu->dwarf2_per_objfile;
f4dc4d17
DE
8137 struct objfile *objfile = dwarf2_per_objfile->objfile;
8138 struct dwarf2_cu *cu = reader->cu;
8139 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
0186c6a7 8140 struct signatured_type *sig_type;
f4dc4d17
DE
8141 struct type_unit_group *tu_group;
8142 struct attribute *attr;
8143 struct partial_die_info *first_die;
8144 CORE_ADDR lowpc, highpc;
8145 struct partial_symtab *pst;
8146
8147 gdb_assert (data == NULL);
0186c6a7
DE
8148 gdb_assert (per_cu->is_debug_types);
8149 sig_type = (struct signatured_type *) per_cu;
f4dc4d17
DE
8150
8151 if (! has_children)
8152 return;
8153
8154 attr = dwarf2_attr_no_follow (type_unit_die, DW_AT_stmt_list);
094b34ac 8155 tu_group = get_type_unit_group (cu, attr);
f4dc4d17 8156
0186c6a7 8157 VEC_safe_push (sig_type_ptr, tu_group->tus, sig_type);
f4dc4d17
DE
8158
8159 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
8160 cu->list_in_scope = &file_symbols;
8161 pst = create_partial_symtab (per_cu, "");
8162 pst->anonymous = 1;
8163
8164 first_die = load_partial_dies (reader, info_ptr, 1);
8165
8166 lowpc = (CORE_ADDR) -1;
8167 highpc = (CORE_ADDR) 0;
8168 scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
8169
8763cede 8170 end_psymtab_common (objfile, pst);
f4dc4d17
DE
8171}
8172
73051182
DE
8173/* Struct used to sort TUs by their abbreviation table offset. */
8174
8175struct tu_abbrev_offset
8176{
8177 struct signatured_type *sig_type;
8178 sect_offset abbrev_offset;
8179};
8180
484cf504 8181/* Helper routine for build_type_psymtabs_1, passed to std::sort. */
73051182 8182
484cf504
TT
8183static bool
8184sort_tu_by_abbrev_offset (const struct tu_abbrev_offset &a,
8185 const struct tu_abbrev_offset &b)
73051182 8186{
484cf504 8187 return a.abbrev_offset < b.abbrev_offset;
73051182
DE
8188}
8189
8190/* Efficiently read all the type units.
8191 This does the bulk of the work for build_type_psymtabs.
8192
8193 The efficiency is because we sort TUs by the abbrev table they use and
8194 only read each abbrev table once. In one program there are 200K TUs
8195 sharing 8K abbrev tables.
8196
8197 The main purpose of this function is to support building the
8198 dwarf2_per_objfile->type_unit_groups table.
8199 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
8200 can collapse the search space by grouping them by stmt_list.
8201 The savings can be significant, in the same program from above the 200K TUs
8202 share 8K stmt_list tables.
8203
8204 FUNC is expected to call get_type_unit_group, which will create the
8205 struct type_unit_group if necessary and add it to
8206 dwarf2_per_objfile->type_unit_groups. */
8207
8208static void
ed2dc618 8209build_type_psymtabs_1 (struct dwarf2_per_objfile *dwarf2_per_objfile)
73051182 8210{
73051182 8211 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
685af9cd 8212 abbrev_table_up abbrev_table;
73051182 8213 sect_offset abbrev_offset;
73051182
DE
8214 int i;
8215
8216 /* It's up to the caller to not call us multiple times. */
8217 gdb_assert (dwarf2_per_objfile->type_unit_groups == NULL);
8218
8219 if (dwarf2_per_objfile->n_type_units == 0)
8220 return;
8221
8222 /* TUs typically share abbrev tables, and there can be way more TUs than
8223 abbrev tables. Sort by abbrev table to reduce the number of times we
8224 read each abbrev table in.
8225 Alternatives are to punt or to maintain a cache of abbrev tables.
8226 This is simpler and efficient enough for now.
8227
8228 Later we group TUs by their DW_AT_stmt_list value (as this defines the
8229 symtab to use). Typically TUs with the same abbrev offset have the same
8230 stmt_list value too so in practice this should work well.
8231
8232 The basic algorithm here is:
8233
8234 sort TUs by abbrev table
8235 for each TU with same abbrev table:
8236 read abbrev table if first user
8237 read TU top level DIE
8238 [IWBN if DWO skeletons had DW_AT_stmt_list]
8239 call FUNC */
8240
b4f54984 8241 if (dwarf_read_debug)
73051182
DE
8242 fprintf_unfiltered (gdb_stdlog, "Building type unit groups ...\n");
8243
8244 /* Sort in a separate table to maintain the order of all_type_units
8245 for .gdb_index: TU indices directly index all_type_units. */
484cf504
TT
8246 std::vector<struct tu_abbrev_offset> sorted_by_abbrev
8247 (dwarf2_per_objfile->n_type_units);
73051182
DE
8248 for (i = 0; i < dwarf2_per_objfile->n_type_units; ++i)
8249 {
8250 struct signatured_type *sig_type = dwarf2_per_objfile->all_type_units[i];
8251
8252 sorted_by_abbrev[i].sig_type = sig_type;
8253 sorted_by_abbrev[i].abbrev_offset =
ed2dc618
SM
8254 read_abbrev_offset (dwarf2_per_objfile,
8255 sig_type->per_cu.section,
9c541725 8256 sig_type->per_cu.sect_off);
73051182 8257 }
484cf504
TT
8258 std::sort (sorted_by_abbrev.begin (), sorted_by_abbrev.end (),
8259 sort_tu_by_abbrev_offset);
73051182 8260
9c541725 8261 abbrev_offset = (sect_offset) ~(unsigned) 0;
73051182
DE
8262
8263 for (i = 0; i < dwarf2_per_objfile->n_type_units; ++i)
8264 {
8265 const struct tu_abbrev_offset *tu = &sorted_by_abbrev[i];
8266
8267 /* Switch to the next abbrev table if necessary. */
8268 if (abbrev_table == NULL
9c541725 8269 || tu->abbrev_offset != abbrev_offset)
73051182 8270 {
73051182
DE
8271 abbrev_offset = tu->abbrev_offset;
8272 abbrev_table =
ed2dc618
SM
8273 abbrev_table_read_table (dwarf2_per_objfile,
8274 &dwarf2_per_objfile->abbrev,
73051182
DE
8275 abbrev_offset);
8276 ++tu_stats->nr_uniq_abbrev_tables;
8277 }
8278
685af9cd
TT
8279 init_cutu_and_read_dies (&tu->sig_type->per_cu, abbrev_table.get (),
8280 0, 0, build_type_psymtabs_reader, NULL);
73051182 8281 }
6aa5f3a6 8282}
73051182 8283
6aa5f3a6
DE
8284/* Print collected type unit statistics. */
8285
8286static void
ed2dc618 8287print_tu_stats (struct dwarf2_per_objfile *dwarf2_per_objfile)
6aa5f3a6
DE
8288{
8289 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
8290
8291 fprintf_unfiltered (gdb_stdlog, "Type unit statistics:\n");
8292 fprintf_unfiltered (gdb_stdlog, " %d TUs\n",
8293 dwarf2_per_objfile->n_type_units);
8294 fprintf_unfiltered (gdb_stdlog, " %d uniq abbrev tables\n",
8295 tu_stats->nr_uniq_abbrev_tables);
8296 fprintf_unfiltered (gdb_stdlog, " %d symtabs from stmt_list entries\n",
8297 tu_stats->nr_symtabs);
8298 fprintf_unfiltered (gdb_stdlog, " %d symtab sharers\n",
8299 tu_stats->nr_symtab_sharers);
8300 fprintf_unfiltered (gdb_stdlog, " %d type units without a stmt_list\n",
8301 tu_stats->nr_stmt_less_type_units);
8302 fprintf_unfiltered (gdb_stdlog, " %d all_type_units reallocs\n",
8303 tu_stats->nr_all_type_units_reallocs);
73051182
DE
8304}
8305
f4dc4d17
DE
8306/* Traversal function for build_type_psymtabs. */
8307
8308static int
8309build_type_psymtab_dependencies (void **slot, void *info)
8310{
ed2dc618
SM
8311 struct dwarf2_per_objfile *dwarf2_per_objfile
8312 = (struct dwarf2_per_objfile *) info;
f4dc4d17
DE
8313 struct objfile *objfile = dwarf2_per_objfile->objfile;
8314 struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
094b34ac 8315 struct dwarf2_per_cu_data *per_cu = &tu_group->per_cu;
f4dc4d17 8316 struct partial_symtab *pst = per_cu->v.psymtab;
0186c6a7
DE
8317 int len = VEC_length (sig_type_ptr, tu_group->tus);
8318 struct signatured_type *iter;
f4dc4d17
DE
8319 int i;
8320
8321 gdb_assert (len > 0);
0186c6a7 8322 gdb_assert (IS_TYPE_UNIT_GROUP (per_cu));
f4dc4d17
DE
8323
8324 pst->number_of_dependencies = len;
8d749320
SM
8325 pst->dependencies =
8326 XOBNEWVEC (&objfile->objfile_obstack, struct partial_symtab *, len);
f4dc4d17 8327 for (i = 0;
0186c6a7 8328 VEC_iterate (sig_type_ptr, tu_group->tus, i, iter);
f4dc4d17
DE
8329 ++i)
8330 {
0186c6a7
DE
8331 gdb_assert (iter->per_cu.is_debug_types);
8332 pst->dependencies[i] = iter->per_cu.v.psymtab;
796a7ff8 8333 iter->type_unit_group = tu_group;
f4dc4d17
DE
8334 }
8335
0186c6a7 8336 VEC_free (sig_type_ptr, tu_group->tus);
348e048f
DE
8337
8338 return 1;
8339}
8340
8341/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
8342 Build partial symbol tables for the .debug_types comp-units. */
8343
8344static void
ed2dc618 8345build_type_psymtabs (struct dwarf2_per_objfile *dwarf2_per_objfile)
348e048f 8346{
ed2dc618 8347 if (! create_all_type_units (dwarf2_per_objfile))
348e048f
DE
8348 return;
8349
ed2dc618 8350 build_type_psymtabs_1 (dwarf2_per_objfile);
6aa5f3a6 8351}
f4dc4d17 8352
6aa5f3a6
DE
8353/* Traversal function for process_skeletonless_type_unit.
8354 Read a TU in a DWO file and build partial symbols for it. */
8355
8356static int
8357process_skeletonless_type_unit (void **slot, void *info)
8358{
8359 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
ed2dc618
SM
8360 struct dwarf2_per_objfile *dwarf2_per_objfile
8361 = (struct dwarf2_per_objfile *) info;
6aa5f3a6
DE
8362 struct signatured_type find_entry, *entry;
8363
8364 /* If this TU doesn't exist in the global table, add it and read it in. */
8365
8366 if (dwarf2_per_objfile->signatured_types == NULL)
8367 {
8368 dwarf2_per_objfile->signatured_types
ed2dc618 8369 = allocate_signatured_type_table (dwarf2_per_objfile->objfile);
6aa5f3a6
DE
8370 }
8371
8372 find_entry.signature = dwo_unit->signature;
8373 slot = htab_find_slot (dwarf2_per_objfile->signatured_types, &find_entry,
8374 INSERT);
8375 /* If we've already seen this type there's nothing to do. What's happening
8376 is we're doing our own version of comdat-folding here. */
8377 if (*slot != NULL)
8378 return 1;
8379
8380 /* This does the job that create_all_type_units would have done for
8381 this TU. */
ed2dc618
SM
8382 entry = add_type_unit (dwarf2_per_objfile, dwo_unit->signature, slot);
8383 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, entry, dwo_unit);
6aa5f3a6
DE
8384 *slot = entry;
8385
8386 /* This does the job that build_type_psymtabs_1 would have done. */
8387 init_cutu_and_read_dies (&entry->per_cu, NULL, 0, 0,
8388 build_type_psymtabs_reader, NULL);
8389
8390 return 1;
8391}
8392
8393/* Traversal function for process_skeletonless_type_units. */
8394
8395static int
8396process_dwo_file_for_skeletonless_type_units (void **slot, void *info)
8397{
8398 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
8399
8400 if (dwo_file->tus != NULL)
8401 {
8402 htab_traverse_noresize (dwo_file->tus,
8403 process_skeletonless_type_unit, info);
8404 }
8405
8406 return 1;
8407}
8408
8409/* Scan all TUs of DWO files, verifying we've processed them.
8410 This is needed in case a TU was emitted without its skeleton.
8411 Note: This can't be done until we know what all the DWO files are. */
8412
8413static void
ed2dc618 8414process_skeletonless_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
6aa5f3a6
DE
8415{
8416 /* Skeletonless TUs in DWP files without .gdb_index is not supported yet. */
ed2dc618 8417 if (get_dwp_file (dwarf2_per_objfile) == NULL
6aa5f3a6
DE
8418 && dwarf2_per_objfile->dwo_files != NULL)
8419 {
8420 htab_traverse_noresize (dwarf2_per_objfile->dwo_files,
8421 process_dwo_file_for_skeletonless_type_units,
ed2dc618 8422 dwarf2_per_objfile);
6aa5f3a6 8423 }
348e048f
DE
8424}
8425
ed2dc618 8426/* Compute the 'user' field for each psymtab in DWARF2_PER_OBJFILE. */
95554aad
TT
8427
8428static void
ed2dc618 8429set_partial_user (struct dwarf2_per_objfile *dwarf2_per_objfile)
95554aad
TT
8430{
8431 int i;
8432
8433 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
8434 {
ed2dc618 8435 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (dwarf2_per_objfile, i);
95554aad
TT
8436 struct partial_symtab *pst = per_cu->v.psymtab;
8437 int j;
8438
36586728
TT
8439 if (pst == NULL)
8440 continue;
8441
95554aad
TT
8442 for (j = 0; j < pst->number_of_dependencies; ++j)
8443 {
8444 /* Set the 'user' field only if it is not already set. */
8445 if (pst->dependencies[j]->user == NULL)
8446 pst->dependencies[j]->user = pst;
8447 }
8448 }
8449}
8450
93311388
DE
8451/* Build the partial symbol table by doing a quick pass through the
8452 .debug_info and .debug_abbrev sections. */
72bf9492 8453
93311388 8454static void
ed2dc618 8455dwarf2_build_psymtabs_hard (struct dwarf2_per_objfile *dwarf2_per_objfile)
93311388 8456{
21b2bd31 8457 int i;
ed2dc618 8458 struct objfile *objfile = dwarf2_per_objfile->objfile;
93311388 8459
b4f54984 8460 if (dwarf_read_debug)
45cfd468
DE
8461 {
8462 fprintf_unfiltered (gdb_stdlog, "Building psymtabs of objfile %s ...\n",
4262abfb 8463 objfile_name (objfile));
45cfd468
DE
8464 }
8465
98bfdba5
PA
8466 dwarf2_per_objfile->reading_partial_symbols = 1;
8467
be391dca 8468 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
91c24f0a 8469
93311388
DE
8470 /* Any cached compilation units will be linked by the per-objfile
8471 read_in_chain. Make sure to free them when we're done. */
11ed8cad 8472 free_cached_comp_units freer (dwarf2_per_objfile);
72bf9492 8473
ed2dc618 8474 build_type_psymtabs (dwarf2_per_objfile);
348e048f 8475
ed2dc618 8476 create_all_comp_units (dwarf2_per_objfile);
c906108c 8477
60606b2c
TT
8478 /* Create a temporary address map on a temporary obstack. We later
8479 copy this to the final obstack. */
8268c778 8480 auto_obstack temp_obstack;
791afaa2
TT
8481
8482 scoped_restore save_psymtabs_addrmap
8483 = make_scoped_restore (&objfile->psymtabs_addrmap,
8484 addrmap_create_mutable (&temp_obstack));
72bf9492 8485
21b2bd31 8486 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
93311388 8487 {
ed2dc618 8488 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (dwarf2_per_objfile, i);
aaa75496 8489
b93601f3 8490 process_psymtab_comp_unit (per_cu, 0, language_minimal);
c906108c 8491 }
ff013f42 8492
6aa5f3a6 8493 /* This has to wait until we read the CUs, we need the list of DWOs. */
ed2dc618 8494 process_skeletonless_type_units (dwarf2_per_objfile);
6aa5f3a6
DE
8495
8496 /* Now that all TUs have been processed we can fill in the dependencies. */
8497 if (dwarf2_per_objfile->type_unit_groups != NULL)
8498 {
8499 htab_traverse_noresize (dwarf2_per_objfile->type_unit_groups,
ed2dc618 8500 build_type_psymtab_dependencies, dwarf2_per_objfile);
6aa5f3a6
DE
8501 }
8502
b4f54984 8503 if (dwarf_read_debug)
ed2dc618 8504 print_tu_stats (dwarf2_per_objfile);
6aa5f3a6 8505
ed2dc618 8506 set_partial_user (dwarf2_per_objfile);
95554aad 8507
ff013f42
JK
8508 objfile->psymtabs_addrmap = addrmap_create_fixed (objfile->psymtabs_addrmap,
8509 &objfile->objfile_obstack);
791afaa2
TT
8510 /* At this point we want to keep the address map. */
8511 save_psymtabs_addrmap.release ();
ff013f42 8512
b4f54984 8513 if (dwarf_read_debug)
45cfd468 8514 fprintf_unfiltered (gdb_stdlog, "Done building psymtabs of %s\n",
4262abfb 8515 objfile_name (objfile));
ae038cb0
DJ
8516}
8517
3019eac3 8518/* die_reader_func for load_partial_comp_unit. */
ae038cb0
DJ
8519
8520static void
dee91e82 8521load_partial_comp_unit_reader (const struct die_reader_specs *reader,
d521ce57 8522 const gdb_byte *info_ptr,
dee91e82
DE
8523 struct die_info *comp_unit_die,
8524 int has_children,
8525 void *data)
ae038cb0 8526{
dee91e82 8527 struct dwarf2_cu *cu = reader->cu;
ae038cb0 8528
95554aad 8529 prepare_one_comp_unit (cu, comp_unit_die, language_minimal);
ae038cb0 8530
ae038cb0
DJ
8531 /* Check if comp unit has_children.
8532 If so, read the rest of the partial symbols from this comp unit.
0963b4bd 8533 If not, there's no more debug_info for this comp unit. */
d85a05f0 8534 if (has_children)
dee91e82
DE
8535 load_partial_dies (reader, info_ptr, 0);
8536}
98bfdba5 8537
dee91e82
DE
8538/* Load the partial DIEs for a secondary CU into memory.
8539 This is also used when rereading a primary CU with load_all_dies. */
c5b7e1cb 8540
dee91e82
DE
8541static void
8542load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu)
8543{
f4dc4d17
DE
8544 init_cutu_and_read_dies (this_cu, NULL, 1, 1,
8545 load_partial_comp_unit_reader, NULL);
ae038cb0
DJ
8546}
8547
ae038cb0 8548static void
ed2dc618 8549read_comp_units_from_section (struct dwarf2_per_objfile *dwarf2_per_objfile,
36586728 8550 struct dwarf2_section_info *section,
f1902523 8551 struct dwarf2_section_info *abbrev_section,
36586728
TT
8552 unsigned int is_dwz,
8553 int *n_allocated,
8554 int *n_comp_units,
8555 struct dwarf2_per_cu_data ***all_comp_units)
ae038cb0 8556{
d521ce57 8557 const gdb_byte *info_ptr;
ed2dc618 8558 struct objfile *objfile = dwarf2_per_objfile->objfile;
be391dca 8559
b4f54984 8560 if (dwarf_read_debug)
bf6af496 8561 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s\n",
a32a8923
DE
8562 get_section_name (section),
8563 get_section_file_name (section));
bf6af496 8564
36586728 8565 dwarf2_read_section (objfile, section);
ae038cb0 8566
36586728 8567 info_ptr = section->buffer;
6e70227d 8568
36586728 8569 while (info_ptr < section->buffer + section->size)
ae038cb0 8570 {
ae038cb0 8571 struct dwarf2_per_cu_data *this_cu;
ae038cb0 8572
9c541725 8573 sect_offset sect_off = (sect_offset) (info_ptr - section->buffer);
ae038cb0 8574
f1902523 8575 comp_unit_head cu_header;
ed2dc618
SM
8576 read_and_check_comp_unit_head (dwarf2_per_objfile, &cu_header, section,
8577 abbrev_section, info_ptr,
8578 rcuh_kind::COMPILE);
ae038cb0
DJ
8579
8580 /* Save the compilation unit for later lookup. */
f1902523
JK
8581 if (cu_header.unit_type != DW_UT_type)
8582 {
8583 this_cu = XOBNEW (&objfile->objfile_obstack,
8584 struct dwarf2_per_cu_data);
8585 memset (this_cu, 0, sizeof (*this_cu));
8586 }
8587 else
8588 {
8589 auto sig_type = XOBNEW (&objfile->objfile_obstack,
8590 struct signatured_type);
8591 memset (sig_type, 0, sizeof (*sig_type));
8592 sig_type->signature = cu_header.signature;
8593 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
8594 this_cu = &sig_type->per_cu;
8595 }
8596 this_cu->is_debug_types = (cu_header.unit_type == DW_UT_type);
9c541725 8597 this_cu->sect_off = sect_off;
f1902523 8598 this_cu->length = cu_header.length + cu_header.initial_length_size;
36586728 8599 this_cu->is_dwz = is_dwz;
e3b94546 8600 this_cu->dwarf2_per_objfile = dwarf2_per_objfile;
8a0459fd 8601 this_cu->section = section;
ae038cb0 8602
36586728 8603 if (*n_comp_units == *n_allocated)
ae038cb0 8604 {
36586728 8605 *n_allocated *= 2;
224c3ddb
SM
8606 *all_comp_units = XRESIZEVEC (struct dwarf2_per_cu_data *,
8607 *all_comp_units, *n_allocated);
ae038cb0 8608 }
36586728
TT
8609 (*all_comp_units)[*n_comp_units] = this_cu;
8610 ++*n_comp_units;
ae038cb0
DJ
8611
8612 info_ptr = info_ptr + this_cu->length;
8613 }
36586728
TT
8614}
8615
8616/* Create a list of all compilation units in OBJFILE.
8617 This is only done for -readnow and building partial symtabs. */
8618
8619static void
ed2dc618 8620create_all_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
36586728
TT
8621{
8622 int n_allocated;
8623 int n_comp_units;
8624 struct dwarf2_per_cu_data **all_comp_units;
4db1a1dc 8625 struct dwz_file *dwz;
ed2dc618 8626 struct objfile *objfile = dwarf2_per_objfile->objfile;
36586728
TT
8627
8628 n_comp_units = 0;
8629 n_allocated = 10;
8d749320 8630 all_comp_units = XNEWVEC (struct dwarf2_per_cu_data *, n_allocated);
36586728 8631
ed2dc618 8632 read_comp_units_from_section (dwarf2_per_objfile, &dwarf2_per_objfile->info,
f1902523 8633 &dwarf2_per_objfile->abbrev, 0,
36586728
TT
8634 &n_allocated, &n_comp_units, &all_comp_units);
8635
ed2dc618 8636 dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
4db1a1dc 8637 if (dwz != NULL)
ed2dc618
SM
8638 read_comp_units_from_section (dwarf2_per_objfile, &dwz->info, &dwz->abbrev,
8639 1, &n_allocated, &n_comp_units,
4db1a1dc 8640 &all_comp_units);
ae038cb0 8641
8d749320
SM
8642 dwarf2_per_objfile->all_comp_units = XOBNEWVEC (&objfile->objfile_obstack,
8643 struct dwarf2_per_cu_data *,
8644 n_comp_units);
ae038cb0
DJ
8645 memcpy (dwarf2_per_objfile->all_comp_units, all_comp_units,
8646 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
8647 xfree (all_comp_units);
8648 dwarf2_per_objfile->n_comp_units = n_comp_units;
c906108c
SS
8649}
8650
5734ee8b 8651/* Process all loaded DIEs for compilation unit CU, starting at
cdc07690 8652 FIRST_DIE. The caller should pass SET_ADDRMAP == 1 if the compilation
5734ee8b 8653 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
cdc07690
YQ
8654 DW_AT_ranges). See the comments of add_partial_subprogram on how
8655 SET_ADDRMAP is used and how *LOWPC and *HIGHPC are updated. */
c906108c 8656
72bf9492
DJ
8657static void
8658scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
cdc07690
YQ
8659 CORE_ADDR *highpc, int set_addrmap,
8660 struct dwarf2_cu *cu)
c906108c 8661{
72bf9492 8662 struct partial_die_info *pdi;
c906108c 8663
91c24f0a
DC
8664 /* Now, march along the PDI's, descending into ones which have
8665 interesting children but skipping the children of the other ones,
8666 until we reach the end of the compilation unit. */
c906108c 8667
72bf9492 8668 pdi = first_die;
91c24f0a 8669
72bf9492
DJ
8670 while (pdi != NULL)
8671 {
52356b79 8672 pdi->fixup (cu);
c906108c 8673
f55ee35c 8674 /* Anonymous namespaces or modules have no name but have interesting
91c24f0a
DC
8675 children, so we need to look at them. Ditto for anonymous
8676 enums. */
933c6fe4 8677
72bf9492 8678 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
95554aad 8679 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
b1dc1806
XR
8680 || pdi->tag == DW_TAG_imported_unit
8681 || pdi->tag == DW_TAG_inlined_subroutine)
c906108c 8682 {
72bf9492 8683 switch (pdi->tag)
c906108c
SS
8684 {
8685 case DW_TAG_subprogram:
b1dc1806 8686 case DW_TAG_inlined_subroutine:
cdc07690 8687 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
c906108c 8688 break;
72929c62 8689 case DW_TAG_constant:
c906108c
SS
8690 case DW_TAG_variable:
8691 case DW_TAG_typedef:
91c24f0a 8692 case DW_TAG_union_type:
72bf9492 8693 if (!pdi->is_declaration)
63d06c5c 8694 {
72bf9492 8695 add_partial_symbol (pdi, cu);
63d06c5c
DC
8696 }
8697 break;
c906108c 8698 case DW_TAG_class_type:
680b30c7 8699 case DW_TAG_interface_type:
c906108c 8700 case DW_TAG_structure_type:
72bf9492 8701 if (!pdi->is_declaration)
c906108c 8702 {
72bf9492 8703 add_partial_symbol (pdi, cu);
c906108c 8704 }
b7fee5a3
KS
8705 if ((cu->language == language_rust
8706 || cu->language == language_cplus) && pdi->has_children)
e98c9e7c
TT
8707 scan_partial_symbols (pdi->die_child, lowpc, highpc,
8708 set_addrmap, cu);
c906108c 8709 break;
91c24f0a 8710 case DW_TAG_enumeration_type:
72bf9492
DJ
8711 if (!pdi->is_declaration)
8712 add_partial_enumeration (pdi, cu);
c906108c
SS
8713 break;
8714 case DW_TAG_base_type:
a02abb62 8715 case DW_TAG_subrange_type:
c906108c 8716 /* File scope base type definitions are added to the partial
c5aa993b 8717 symbol table. */
72bf9492 8718 add_partial_symbol (pdi, cu);
c906108c 8719 break;
d9fa45fe 8720 case DW_TAG_namespace:
cdc07690 8721 add_partial_namespace (pdi, lowpc, highpc, set_addrmap, cu);
91c24f0a 8722 break;
5d7cb8df 8723 case DW_TAG_module:
cdc07690 8724 add_partial_module (pdi, lowpc, highpc, set_addrmap, cu);
5d7cb8df 8725 break;
95554aad
TT
8726 case DW_TAG_imported_unit:
8727 {
8728 struct dwarf2_per_cu_data *per_cu;
8729
f4dc4d17
DE
8730 /* For now we don't handle imported units in type units. */
8731 if (cu->per_cu->is_debug_types)
8732 {
8733 error (_("Dwarf Error: DW_TAG_imported_unit is not"
8734 " supported in type units [in module %s]"),
518817b3 8735 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
f4dc4d17
DE
8736 }
8737
e3b94546
SM
8738 per_cu = dwarf2_find_containing_comp_unit
8739 (pdi->d.sect_off, pdi->is_dwz,
518817b3 8740 cu->per_cu->dwarf2_per_objfile);
95554aad
TT
8741
8742 /* Go read the partial unit, if needed. */
8743 if (per_cu->v.psymtab == NULL)
b93601f3 8744 process_psymtab_comp_unit (per_cu, 1, cu->language);
95554aad 8745
f4dc4d17 8746 VEC_safe_push (dwarf2_per_cu_ptr,
796a7ff8 8747 cu->per_cu->imported_symtabs, per_cu);
95554aad
TT
8748 }
8749 break;
74921315
KS
8750 case DW_TAG_imported_declaration:
8751 add_partial_symbol (pdi, cu);
8752 break;
c906108c
SS
8753 default:
8754 break;
8755 }
8756 }
8757
72bf9492
DJ
8758 /* If the die has a sibling, skip to the sibling. */
8759
8760 pdi = pdi->die_sibling;
8761 }
8762}
8763
8764/* Functions used to compute the fully scoped name of a partial DIE.
91c24f0a 8765
72bf9492 8766 Normally, this is simple. For C++, the parent DIE's fully scoped
9c37b5ae 8767 name is concatenated with "::" and the partial DIE's name.
72bf9492
DJ
8768 Enumerators are an exception; they use the scope of their parent
8769 enumeration type, i.e. the name of the enumeration type is not
8770 prepended to the enumerator.
91c24f0a 8771
72bf9492
DJ
8772 There are two complexities. One is DW_AT_specification; in this
8773 case "parent" means the parent of the target of the specification,
8774 instead of the direct parent of the DIE. The other is compilers
8775 which do not emit DW_TAG_namespace; in this case we try to guess
8776 the fully qualified name of structure types from their members'
8777 linkage names. This must be done using the DIE's children rather
8778 than the children of any DW_AT_specification target. We only need
8779 to do this for structures at the top level, i.e. if the target of
8780 any DW_AT_specification (if any; otherwise the DIE itself) does not
8781 have a parent. */
8782
8783/* Compute the scope prefix associated with PDI's parent, in
8784 compilation unit CU. The result will be allocated on CU's
8785 comp_unit_obstack, or a copy of the already allocated PDI->NAME
8786 field. NULL is returned if no prefix is necessary. */
15d034d0 8787static const char *
72bf9492
DJ
8788partial_die_parent_scope (struct partial_die_info *pdi,
8789 struct dwarf2_cu *cu)
8790{
15d034d0 8791 const char *grandparent_scope;
72bf9492 8792 struct partial_die_info *parent, *real_pdi;
91c24f0a 8793
72bf9492
DJ
8794 /* We need to look at our parent DIE; if we have a DW_AT_specification,
8795 then this means the parent of the specification DIE. */
8796
8797 real_pdi = pdi;
72bf9492 8798 while (real_pdi->has_specification)
36586728
TT
8799 real_pdi = find_partial_die (real_pdi->spec_offset,
8800 real_pdi->spec_is_dwz, cu);
72bf9492
DJ
8801
8802 parent = real_pdi->die_parent;
8803 if (parent == NULL)
8804 return NULL;
8805
8806 if (parent->scope_set)
8807 return parent->scope;
8808
52356b79 8809 parent->fixup (cu);
72bf9492 8810
10b3939b 8811 grandparent_scope = partial_die_parent_scope (parent, cu);
72bf9492 8812
acebe513
UW
8813 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
8814 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
8815 Work around this problem here. */
8816 if (cu->language == language_cplus
6e70227d 8817 && parent->tag == DW_TAG_namespace
acebe513
UW
8818 && strcmp (parent->name, "::") == 0
8819 && grandparent_scope == NULL)
8820 {
8821 parent->scope = NULL;
8822 parent->scope_set = 1;
8823 return NULL;
8824 }
8825
9c6c53f7
SA
8826 if (pdi->tag == DW_TAG_enumerator)
8827 /* Enumerators should not get the name of the enumeration as a prefix. */
8828 parent->scope = grandparent_scope;
8829 else if (parent->tag == DW_TAG_namespace
f55ee35c 8830 || parent->tag == DW_TAG_module
72bf9492
DJ
8831 || parent->tag == DW_TAG_structure_type
8832 || parent->tag == DW_TAG_class_type
680b30c7 8833 || parent->tag == DW_TAG_interface_type
ceeb3d5a
TT
8834 || parent->tag == DW_TAG_union_type
8835 || parent->tag == DW_TAG_enumeration_type)
72bf9492
DJ
8836 {
8837 if (grandparent_scope == NULL)
8838 parent->scope = parent->name;
8839 else
3e43a32a
MS
8840 parent->scope = typename_concat (&cu->comp_unit_obstack,
8841 grandparent_scope,
f55ee35c 8842 parent->name, 0, cu);
72bf9492 8843 }
72bf9492
DJ
8844 else
8845 {
8846 /* FIXME drow/2004-04-01: What should we be doing with
8847 function-local names? For partial symbols, we should probably be
8848 ignoring them. */
8849 complaint (&symfile_complaints,
9d8780f0
SM
8850 _("unhandled containing DIE tag %d for DIE at %s"),
8851 parent->tag, sect_offset_str (pdi->sect_off));
72bf9492 8852 parent->scope = grandparent_scope;
c906108c
SS
8853 }
8854
72bf9492
DJ
8855 parent->scope_set = 1;
8856 return parent->scope;
8857}
8858
8859/* Return the fully scoped name associated with PDI, from compilation unit
8860 CU. The result will be allocated with malloc. */
4568ecf9 8861
72bf9492
DJ
8862static char *
8863partial_die_full_name (struct partial_die_info *pdi,
8864 struct dwarf2_cu *cu)
8865{
15d034d0 8866 const char *parent_scope;
72bf9492 8867
98bfdba5
PA
8868 /* If this is a template instantiation, we can not work out the
8869 template arguments from partial DIEs. So, unfortunately, we have
8870 to go through the full DIEs. At least any work we do building
8871 types here will be reused if full symbols are loaded later. */
8872 if (pdi->has_template_arguments)
8873 {
52356b79 8874 pdi->fixup (cu);
98bfdba5
PA
8875
8876 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
8877 {
8878 struct die_info *die;
8879 struct attribute attr;
8880 struct dwarf2_cu *ref_cu = cu;
8881
b64f50a1 8882 /* DW_FORM_ref_addr is using section offset. */
b4069958 8883 attr.name = (enum dwarf_attribute) 0;
98bfdba5 8884 attr.form = DW_FORM_ref_addr;
9c541725 8885 attr.u.unsnd = to_underlying (pdi->sect_off);
98bfdba5
PA
8886 die = follow_die_ref (NULL, &attr, &ref_cu);
8887
8888 return xstrdup (dwarf2_full_name (NULL, die, ref_cu));
8889 }
8890 }
8891
72bf9492
DJ
8892 parent_scope = partial_die_parent_scope (pdi, cu);
8893 if (parent_scope == NULL)
8894 return NULL;
8895 else
f55ee35c 8896 return typename_concat (NULL, parent_scope, pdi->name, 0, cu);
c906108c
SS
8897}
8898
8899static void
72bf9492 8900add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
c906108c 8901{
518817b3
SM
8902 struct dwarf2_per_objfile *dwarf2_per_objfile
8903 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 8904 struct objfile *objfile = dwarf2_per_objfile->objfile;
3e29f34a 8905 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c 8906 CORE_ADDR addr = 0;
15d034d0 8907 const char *actual_name = NULL;
e142c38c 8908 CORE_ADDR baseaddr;
15d034d0 8909 char *built_actual_name;
e142c38c
DJ
8910
8911 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 8912
15d034d0
TT
8913 built_actual_name = partial_die_full_name (pdi, cu);
8914 if (built_actual_name != NULL)
8915 actual_name = built_actual_name;
63d06c5c 8916
72bf9492
DJ
8917 if (actual_name == NULL)
8918 actual_name = pdi->name;
8919
c906108c
SS
8920 switch (pdi->tag)
8921 {
b1dc1806 8922 case DW_TAG_inlined_subroutine:
c906108c 8923 case DW_TAG_subprogram:
3e29f34a 8924 addr = gdbarch_adjust_dwarf2_addr (gdbarch, pdi->lowpc + baseaddr);
2cfa0c8d 8925 if (pdi->is_external || cu->language == language_ada)
c906108c 8926 {
2cfa0c8d
JB
8927 /* brobecker/2007-12-26: Normally, only "external" DIEs are part
8928 of the global scope. But in Ada, we want to be able to access
8929 nested procedures globally. So all Ada subprograms are stored
8930 in the global scope. */
f47fb265 8931 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 8932 built_actual_name != NULL,
f47fb265
MS
8933 VAR_DOMAIN, LOC_BLOCK,
8934 &objfile->global_psymbols,
1762568f 8935 addr, cu->language, objfile);
c906108c
SS
8936 }
8937 else
8938 {
f47fb265 8939 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 8940 built_actual_name != NULL,
f47fb265
MS
8941 VAR_DOMAIN, LOC_BLOCK,
8942 &objfile->static_psymbols,
1762568f 8943 addr, cu->language, objfile);
c906108c 8944 }
0c1b455e
TT
8945
8946 if (pdi->main_subprogram && actual_name != NULL)
8947 set_objfile_main_name (objfile, actual_name, cu->language);
c906108c 8948 break;
72929c62
JB
8949 case DW_TAG_constant:
8950 {
af5bf4ad 8951 std::vector<partial_symbol *> *list;
72929c62
JB
8952
8953 if (pdi->is_external)
8954 list = &objfile->global_psymbols;
8955 else
8956 list = &objfile->static_psymbols;
f47fb265 8957 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 8958 built_actual_name != NULL, VAR_DOMAIN, LOC_STATIC,
1762568f 8959 list, 0, cu->language, objfile);
72929c62
JB
8960 }
8961 break;
c906108c 8962 case DW_TAG_variable:
95554aad
TT
8963 if (pdi->d.locdesc)
8964 addr = decode_locdesc (pdi->d.locdesc, cu);
caac4577 8965
95554aad 8966 if (pdi->d.locdesc
caac4577
JG
8967 && addr == 0
8968 && !dwarf2_per_objfile->has_section_at_zero)
8969 {
8970 /* A global or static variable may also have been stripped
8971 out by the linker if unused, in which case its address
8972 will be nullified; do not add such variables into partial
8973 symbol table then. */
8974 }
8975 else if (pdi->is_external)
c906108c
SS
8976 {
8977 /* Global Variable.
8978 Don't enter into the minimal symbol tables as there is
8979 a minimal symbol table entry from the ELF symbols already.
8980 Enter into partial symbol table if it has a location
8981 descriptor or a type.
8982 If the location descriptor is missing, new_symbol will create
8983 a LOC_UNRESOLVED symbol, the address of the variable will then
8984 be determined from the minimal symbol table whenever the variable
8985 is referenced.
8986 The address for the partial symbol table entry is not
8987 used by GDB, but it comes in handy for debugging partial symbol
8988 table building. */
8989
95554aad 8990 if (pdi->d.locdesc || pdi->has_type)
f47fb265 8991 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 8992 built_actual_name != NULL,
f47fb265
MS
8993 VAR_DOMAIN, LOC_STATIC,
8994 &objfile->global_psymbols,
1762568f 8995 addr + baseaddr,
f47fb265 8996 cu->language, objfile);
c906108c
SS
8997 }
8998 else
8999 {
ff908ebf
AW
9000 int has_loc = pdi->d.locdesc != NULL;
9001
9002 /* Static Variable. Skip symbols whose value we cannot know (those
9003 without location descriptors or constant values). */
9004 if (!has_loc && !pdi->has_const_value)
decbce07 9005 {
15d034d0 9006 xfree (built_actual_name);
decbce07
MS
9007 return;
9008 }
ff908ebf 9009
f47fb265 9010 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 9011 built_actual_name != NULL,
f47fb265
MS
9012 VAR_DOMAIN, LOC_STATIC,
9013 &objfile->static_psymbols,
ff908ebf 9014 has_loc ? addr + baseaddr : (CORE_ADDR) 0,
f47fb265 9015 cu->language, objfile);
c906108c
SS
9016 }
9017 break;
9018 case DW_TAG_typedef:
9019 case DW_TAG_base_type:
a02abb62 9020 case DW_TAG_subrange_type:
38d518c9 9021 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 9022 built_actual_name != NULL,
176620f1 9023 VAR_DOMAIN, LOC_TYPEDEF,
c906108c 9024 &objfile->static_psymbols,
1762568f 9025 0, cu->language, objfile);
c906108c 9026 break;
74921315 9027 case DW_TAG_imported_declaration:
72bf9492
DJ
9028 case DW_TAG_namespace:
9029 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 9030 built_actual_name != NULL,
72bf9492
DJ
9031 VAR_DOMAIN, LOC_TYPEDEF,
9032 &objfile->global_psymbols,
1762568f 9033 0, cu->language, objfile);
72bf9492 9034 break;
530e8392
KB
9035 case DW_TAG_module:
9036 add_psymbol_to_list (actual_name, strlen (actual_name),
9037 built_actual_name != NULL,
9038 MODULE_DOMAIN, LOC_TYPEDEF,
9039 &objfile->global_psymbols,
1762568f 9040 0, cu->language, objfile);
530e8392 9041 break;
c906108c 9042 case DW_TAG_class_type:
680b30c7 9043 case DW_TAG_interface_type:
c906108c
SS
9044 case DW_TAG_structure_type:
9045 case DW_TAG_union_type:
9046 case DW_TAG_enumeration_type:
fa4028e9
JB
9047 /* Skip external references. The DWARF standard says in the section
9048 about "Structure, Union, and Class Type Entries": "An incomplete
9049 structure, union or class type is represented by a structure,
9050 union or class entry that does not have a byte size attribute
9051 and that has a DW_AT_declaration attribute." */
9052 if (!pdi->has_byte_size && pdi->is_declaration)
decbce07 9053 {
15d034d0 9054 xfree (built_actual_name);
decbce07
MS
9055 return;
9056 }
fa4028e9 9057
63d06c5c
DC
9058 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
9059 static vs. global. */
38d518c9 9060 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 9061 built_actual_name != NULL,
176620f1 9062 STRUCT_DOMAIN, LOC_TYPEDEF,
9c37b5ae 9063 cu->language == language_cplus
63d06c5c
DC
9064 ? &objfile->global_psymbols
9065 : &objfile->static_psymbols,
1762568f 9066 0, cu->language, objfile);
c906108c 9067
c906108c
SS
9068 break;
9069 case DW_TAG_enumerator:
38d518c9 9070 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 9071 built_actual_name != NULL,
176620f1 9072 VAR_DOMAIN, LOC_CONST,
9c37b5ae 9073 cu->language == language_cplus
f6fe98ef
DJ
9074 ? &objfile->global_psymbols
9075 : &objfile->static_psymbols,
1762568f 9076 0, cu->language, objfile);
c906108c
SS
9077 break;
9078 default:
9079 break;
9080 }
5c4e30ca 9081
15d034d0 9082 xfree (built_actual_name);
c906108c
SS
9083}
9084
5c4e30ca
DC
9085/* Read a partial die corresponding to a namespace; also, add a symbol
9086 corresponding to that namespace to the symbol table. NAMESPACE is
9087 the name of the enclosing namespace. */
91c24f0a 9088
72bf9492
DJ
9089static void
9090add_partial_namespace (struct partial_die_info *pdi,
91c24f0a 9091 CORE_ADDR *lowpc, CORE_ADDR *highpc,
cdc07690 9092 int set_addrmap, struct dwarf2_cu *cu)
91c24f0a 9093{
72bf9492 9094 /* Add a symbol for the namespace. */
e7c27a73 9095
72bf9492 9096 add_partial_symbol (pdi, cu);
5c4e30ca
DC
9097
9098 /* Now scan partial symbols in that namespace. */
9099
91c24f0a 9100 if (pdi->has_children)
cdc07690 9101 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
91c24f0a
DC
9102}
9103
5d7cb8df
JK
9104/* Read a partial die corresponding to a Fortran module. */
9105
9106static void
9107add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
cdc07690 9108 CORE_ADDR *highpc, int set_addrmap, struct dwarf2_cu *cu)
5d7cb8df 9109{
530e8392
KB
9110 /* Add a symbol for the namespace. */
9111
9112 add_partial_symbol (pdi, cu);
9113
f55ee35c 9114 /* Now scan partial symbols in that module. */
5d7cb8df
JK
9115
9116 if (pdi->has_children)
cdc07690 9117 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
5d7cb8df
JK
9118}
9119
b1dc1806
XR
9120/* Read a partial die corresponding to a subprogram or an inlined
9121 subprogram and create a partial symbol for that subprogram.
9122 When the CU language allows it, this routine also defines a partial
9123 symbol for each nested subprogram that this subprogram contains.
9124 If SET_ADDRMAP is true, record the covered ranges in the addrmap.
9125 Set *LOWPC and *HIGHPC to the lowest and highest PC values found in PDI.
6e70227d 9126
cdc07690
YQ
9127 PDI may also be a lexical block, in which case we simply search
9128 recursively for subprograms defined inside that lexical block.
bc30ff58
JB
9129 Again, this is only performed when the CU language allows this
9130 type of definitions. */
9131
9132static void
9133add_partial_subprogram (struct partial_die_info *pdi,
9134 CORE_ADDR *lowpc, CORE_ADDR *highpc,
cdc07690 9135 int set_addrmap, struct dwarf2_cu *cu)
bc30ff58 9136{
b1dc1806 9137 if (pdi->tag == DW_TAG_subprogram || pdi->tag == DW_TAG_inlined_subroutine)
bc30ff58
JB
9138 {
9139 if (pdi->has_pc_info)
9140 {
9141 if (pdi->lowpc < *lowpc)
9142 *lowpc = pdi->lowpc;
9143 if (pdi->highpc > *highpc)
9144 *highpc = pdi->highpc;
cdc07690 9145 if (set_addrmap)
5734ee8b 9146 {
518817b3 9147 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
3e29f34a
MR
9148 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9149 CORE_ADDR baseaddr;
9150 CORE_ADDR highpc;
9151 CORE_ADDR lowpc;
5734ee8b
DJ
9152
9153 baseaddr = ANOFFSET (objfile->section_offsets,
9154 SECT_OFF_TEXT (objfile));
3e29f34a
MR
9155 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch,
9156 pdi->lowpc + baseaddr);
9157 highpc = gdbarch_adjust_dwarf2_addr (gdbarch,
9158 pdi->highpc + baseaddr);
9159 addrmap_set_empty (objfile->psymtabs_addrmap, lowpc, highpc - 1,
9291a0cd 9160 cu->per_cu->v.psymtab);
5734ee8b 9161 }
481860b3
GB
9162 }
9163
9164 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
9165 {
bc30ff58 9166 if (!pdi->is_declaration)
e8d05480
JB
9167 /* Ignore subprogram DIEs that do not have a name, they are
9168 illegal. Do not emit a complaint at this point, we will
9169 do so when we convert this psymtab into a symtab. */
9170 if (pdi->name)
9171 add_partial_symbol (pdi, cu);
bc30ff58
JB
9172 }
9173 }
6e70227d 9174
bc30ff58
JB
9175 if (! pdi->has_children)
9176 return;
9177
9178 if (cu->language == language_ada)
9179 {
9180 pdi = pdi->die_child;
9181 while (pdi != NULL)
9182 {
52356b79 9183 pdi->fixup (cu);
bc30ff58 9184 if (pdi->tag == DW_TAG_subprogram
b1dc1806 9185 || pdi->tag == DW_TAG_inlined_subroutine
bc30ff58 9186 || pdi->tag == DW_TAG_lexical_block)
cdc07690 9187 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
bc30ff58
JB
9188 pdi = pdi->die_sibling;
9189 }
9190 }
9191}
9192
91c24f0a
DC
9193/* Read a partial die corresponding to an enumeration type. */
9194
72bf9492
DJ
9195static void
9196add_partial_enumeration (struct partial_die_info *enum_pdi,
9197 struct dwarf2_cu *cu)
91c24f0a 9198{
72bf9492 9199 struct partial_die_info *pdi;
91c24f0a
DC
9200
9201 if (enum_pdi->name != NULL)
72bf9492
DJ
9202 add_partial_symbol (enum_pdi, cu);
9203
9204 pdi = enum_pdi->die_child;
9205 while (pdi)
91c24f0a 9206 {
72bf9492 9207 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
e2e0b3e5 9208 complaint (&symfile_complaints, _("malformed enumerator DIE ignored"));
91c24f0a 9209 else
72bf9492
DJ
9210 add_partial_symbol (pdi, cu);
9211 pdi = pdi->die_sibling;
91c24f0a 9212 }
91c24f0a
DC
9213}
9214
6caca83c
CC
9215/* Return the initial uleb128 in the die at INFO_PTR. */
9216
9217static unsigned int
d521ce57 9218peek_abbrev_code (bfd *abfd, const gdb_byte *info_ptr)
6caca83c
CC
9219{
9220 unsigned int bytes_read;
9221
9222 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9223}
9224
685af9cd
TT
9225/* Read the initial uleb128 in the die at INFO_PTR in compilation unit
9226 READER::CU. Use READER::ABBREV_TABLE to lookup any abbreviation.
9227
4bb7a0a7
DJ
9228 Return the corresponding abbrev, or NULL if the number is zero (indicating
9229 an empty DIE). In either case *BYTES_READ will be set to the length of
9230 the initial number. */
9231
9232static struct abbrev_info *
685af9cd
TT
9233peek_die_abbrev (const die_reader_specs &reader,
9234 const gdb_byte *info_ptr, unsigned int *bytes_read)
4bb7a0a7 9235{
685af9cd 9236 dwarf2_cu *cu = reader.cu;
518817b3 9237 bfd *abfd = cu->per_cu->dwarf2_per_objfile->objfile->obfd;
685af9cd
TT
9238 unsigned int abbrev_number
9239 = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
4bb7a0a7
DJ
9240
9241 if (abbrev_number == 0)
9242 return NULL;
9243
685af9cd 9244 abbrev_info *abbrev = reader.abbrev_table->lookup_abbrev (abbrev_number);
4bb7a0a7
DJ
9245 if (!abbrev)
9246 {
422b9917 9247 error (_("Dwarf Error: Could not find abbrev number %d in %s"
9d8780f0 9248 " at offset %s [in module %s]"),
422b9917 9249 abbrev_number, cu->per_cu->is_debug_types ? "TU" : "CU",
9d8780f0 9250 sect_offset_str (cu->header.sect_off), bfd_get_filename (abfd));
4bb7a0a7
DJ
9251 }
9252
9253 return abbrev;
9254}
9255
93311388
DE
9256/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
9257 Returns a pointer to the end of a series of DIEs, terminated by an empty
4bb7a0a7
DJ
9258 DIE. Any children of the skipped DIEs will also be skipped. */
9259
d521ce57
TT
9260static const gdb_byte *
9261skip_children (const struct die_reader_specs *reader, const gdb_byte *info_ptr)
4bb7a0a7 9262{
4bb7a0a7
DJ
9263 while (1)
9264 {
685af9cd
TT
9265 unsigned int bytes_read;
9266 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
9267
4bb7a0a7
DJ
9268 if (abbrev == NULL)
9269 return info_ptr + bytes_read;
9270 else
dee91e82 9271 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
4bb7a0a7
DJ
9272 }
9273}
9274
93311388
DE
9275/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
9276 INFO_PTR should point just after the initial uleb128 of a DIE, and the
4bb7a0a7
DJ
9277 abbrev corresponding to that skipped uleb128 should be passed in
9278 ABBREV. Returns a pointer to this DIE's sibling, skipping any
9279 children. */
9280
d521ce57
TT
9281static const gdb_byte *
9282skip_one_die (const struct die_reader_specs *reader, const gdb_byte *info_ptr,
dee91e82 9283 struct abbrev_info *abbrev)
4bb7a0a7
DJ
9284{
9285 unsigned int bytes_read;
9286 struct attribute attr;
dee91e82
DE
9287 bfd *abfd = reader->abfd;
9288 struct dwarf2_cu *cu = reader->cu;
d521ce57 9289 const gdb_byte *buffer = reader->buffer;
f664829e 9290 const gdb_byte *buffer_end = reader->buffer_end;
4bb7a0a7
DJ
9291 unsigned int form, i;
9292
9293 for (i = 0; i < abbrev->num_attrs; i++)
9294 {
9295 /* The only abbrev we care about is DW_AT_sibling. */
9296 if (abbrev->attrs[i].name == DW_AT_sibling)
9297 {
dee91e82 9298 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
4bb7a0a7 9299 if (attr.form == DW_FORM_ref_addr)
3e43a32a
MS
9300 complaint (&symfile_complaints,
9301 _("ignoring absolute DW_AT_sibling"));
4bb7a0a7 9302 else
b9502d3f 9303 {
9c541725
PA
9304 sect_offset off = dwarf2_get_ref_die_offset (&attr);
9305 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
b9502d3f
WN
9306
9307 if (sibling_ptr < info_ptr)
9308 complaint (&symfile_complaints,
9309 _("DW_AT_sibling points backwards"));
22869d73
KS
9310 else if (sibling_ptr > reader->buffer_end)
9311 dwarf2_section_buffer_overflow_complaint (reader->die_section);
b9502d3f
WN
9312 else
9313 return sibling_ptr;
9314 }
4bb7a0a7
DJ
9315 }
9316
9317 /* If it isn't DW_AT_sibling, skip this attribute. */
9318 form = abbrev->attrs[i].form;
9319 skip_attribute:
9320 switch (form)
9321 {
4bb7a0a7 9322 case DW_FORM_ref_addr:
ae411497
TT
9323 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
9324 and later it is offset sized. */
9325 if (cu->header.version == 2)
9326 info_ptr += cu->header.addr_size;
9327 else
9328 info_ptr += cu->header.offset_size;
9329 break;
36586728
TT
9330 case DW_FORM_GNU_ref_alt:
9331 info_ptr += cu->header.offset_size;
9332 break;
ae411497 9333 case DW_FORM_addr:
4bb7a0a7
DJ
9334 info_ptr += cu->header.addr_size;
9335 break;
9336 case DW_FORM_data1:
9337 case DW_FORM_ref1:
9338 case DW_FORM_flag:
9339 info_ptr += 1;
9340 break;
2dc7f7b3 9341 case DW_FORM_flag_present:
43988095 9342 case DW_FORM_implicit_const:
2dc7f7b3 9343 break;
4bb7a0a7
DJ
9344 case DW_FORM_data2:
9345 case DW_FORM_ref2:
9346 info_ptr += 2;
9347 break;
9348 case DW_FORM_data4:
9349 case DW_FORM_ref4:
9350 info_ptr += 4;
9351 break;
9352 case DW_FORM_data8:
9353 case DW_FORM_ref8:
55f1336d 9354 case DW_FORM_ref_sig8:
4bb7a0a7
DJ
9355 info_ptr += 8;
9356 break;
0224619f
JK
9357 case DW_FORM_data16:
9358 info_ptr += 16;
9359 break;
4bb7a0a7 9360 case DW_FORM_string:
9b1c24c8 9361 read_direct_string (abfd, info_ptr, &bytes_read);
4bb7a0a7
DJ
9362 info_ptr += bytes_read;
9363 break;
2dc7f7b3 9364 case DW_FORM_sec_offset:
4bb7a0a7 9365 case DW_FORM_strp:
36586728 9366 case DW_FORM_GNU_strp_alt:
4bb7a0a7
DJ
9367 info_ptr += cu->header.offset_size;
9368 break;
2dc7f7b3 9369 case DW_FORM_exprloc:
4bb7a0a7
DJ
9370 case DW_FORM_block:
9371 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9372 info_ptr += bytes_read;
9373 break;
9374 case DW_FORM_block1:
9375 info_ptr += 1 + read_1_byte (abfd, info_ptr);
9376 break;
9377 case DW_FORM_block2:
9378 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
9379 break;
9380 case DW_FORM_block4:
9381 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
9382 break;
9383 case DW_FORM_sdata:
9384 case DW_FORM_udata:
9385 case DW_FORM_ref_udata:
3019eac3
DE
9386 case DW_FORM_GNU_addr_index:
9387 case DW_FORM_GNU_str_index:
d521ce57 9388 info_ptr = safe_skip_leb128 (info_ptr, buffer_end);
4bb7a0a7
DJ
9389 break;
9390 case DW_FORM_indirect:
9391 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9392 info_ptr += bytes_read;
9393 /* We need to continue parsing from here, so just go back to
9394 the top. */
9395 goto skip_attribute;
9396
9397 default:
3e43a32a
MS
9398 error (_("Dwarf Error: Cannot handle %s "
9399 "in DWARF reader [in module %s]"),
4bb7a0a7
DJ
9400 dwarf_form_name (form),
9401 bfd_get_filename (abfd));
9402 }
9403 }
9404
9405 if (abbrev->has_children)
dee91e82 9406 return skip_children (reader, info_ptr);
4bb7a0a7
DJ
9407 else
9408 return info_ptr;
9409}
9410
93311388 9411/* Locate ORIG_PDI's sibling.
dee91e82 9412 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
91c24f0a 9413
d521ce57 9414static const gdb_byte *
dee91e82
DE
9415locate_pdi_sibling (const struct die_reader_specs *reader,
9416 struct partial_die_info *orig_pdi,
d521ce57 9417 const gdb_byte *info_ptr)
91c24f0a
DC
9418{
9419 /* Do we know the sibling already? */
72bf9492 9420
91c24f0a
DC
9421 if (orig_pdi->sibling)
9422 return orig_pdi->sibling;
9423
9424 /* Are there any children to deal with? */
9425
9426 if (!orig_pdi->has_children)
9427 return info_ptr;
9428
4bb7a0a7 9429 /* Skip the children the long way. */
91c24f0a 9430
dee91e82 9431 return skip_children (reader, info_ptr);
91c24f0a
DC
9432}
9433
257e7a09 9434/* Expand this partial symbol table into a full symbol table. SELF is
442e4d9c 9435 not NULL. */
c906108c
SS
9436
9437static void
257e7a09
YQ
9438dwarf2_read_symtab (struct partial_symtab *self,
9439 struct objfile *objfile)
c906108c 9440{
ed2dc618
SM
9441 struct dwarf2_per_objfile *dwarf2_per_objfile
9442 = get_dwarf2_per_objfile (objfile);
9443
257e7a09 9444 if (self->readin)
c906108c 9445 {
442e4d9c 9446 warning (_("bug: psymtab for %s is already read in."),
257e7a09 9447 self->filename);
442e4d9c
YQ
9448 }
9449 else
9450 {
9451 if (info_verbose)
c906108c 9452 {
442e4d9c 9453 printf_filtered (_("Reading in symbols for %s..."),
257e7a09 9454 self->filename);
442e4d9c 9455 gdb_flush (gdb_stdout);
c906108c 9456 }
c906108c 9457
442e4d9c
YQ
9458 /* If this psymtab is constructed from a debug-only objfile, the
9459 has_section_at_zero flag will not necessarily be correct. We
9460 can get the correct value for this flag by looking at the data
9461 associated with the (presumably stripped) associated objfile. */
9462 if (objfile->separate_debug_objfile_backlink)
9463 {
9464 struct dwarf2_per_objfile *dpo_backlink
ed2dc618 9465 = get_dwarf2_per_objfile (objfile->separate_debug_objfile_backlink);
9a619af0 9466
442e4d9c
YQ
9467 dwarf2_per_objfile->has_section_at_zero
9468 = dpo_backlink->has_section_at_zero;
9469 }
b2ab525c 9470
442e4d9c 9471 dwarf2_per_objfile->reading_partial_symbols = 0;
98bfdba5 9472
257e7a09 9473 psymtab_to_symtab_1 (self);
c906108c 9474
442e4d9c
YQ
9475 /* Finish up the debug error message. */
9476 if (info_verbose)
9477 printf_filtered (_("done.\n"));
c906108c 9478 }
95554aad 9479
ed2dc618 9480 process_cu_includes (dwarf2_per_objfile);
c906108c 9481}
9cdd5dbd
DE
9482\f
9483/* Reading in full CUs. */
c906108c 9484
10b3939b
DJ
9485/* Add PER_CU to the queue. */
9486
9487static void
95554aad
TT
9488queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
9489 enum language pretend_language)
10b3939b
DJ
9490{
9491 struct dwarf2_queue_item *item;
9492
9493 per_cu->queued = 1;
8d749320 9494 item = XNEW (struct dwarf2_queue_item);
10b3939b 9495 item->per_cu = per_cu;
95554aad 9496 item->pretend_language = pretend_language;
10b3939b
DJ
9497 item->next = NULL;
9498
9499 if (dwarf2_queue == NULL)
9500 dwarf2_queue = item;
9501 else
9502 dwarf2_queue_tail->next = item;
9503
9504 dwarf2_queue_tail = item;
9505}
9506
89e63ee4
DE
9507/* If PER_CU is not yet queued, add it to the queue.
9508 If DEPENDENT_CU is non-NULL, it has a reference to PER_CU so add a
9509 dependency.
0907af0c 9510 The result is non-zero if PER_CU was queued, otherwise the result is zero
69d751e3
DE
9511 meaning either PER_CU is already queued or it is already loaded.
9512
9513 N.B. There is an invariant here that if a CU is queued then it is loaded.
9514 The caller is required to load PER_CU if we return non-zero. */
0907af0c
DE
9515
9516static int
89e63ee4 9517maybe_queue_comp_unit (struct dwarf2_cu *dependent_cu,
0907af0c
DE
9518 struct dwarf2_per_cu_data *per_cu,
9519 enum language pretend_language)
9520{
9521 /* We may arrive here during partial symbol reading, if we need full
9522 DIEs to process an unusual case (e.g. template arguments). Do
9523 not queue PER_CU, just tell our caller to load its DIEs. */
ed2dc618 9524 if (per_cu->dwarf2_per_objfile->reading_partial_symbols)
0907af0c
DE
9525 {
9526 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
9527 return 1;
9528 return 0;
9529 }
9530
9531 /* Mark the dependence relation so that we don't flush PER_CU
9532 too early. */
89e63ee4
DE
9533 if (dependent_cu != NULL)
9534 dwarf2_add_dependence (dependent_cu, per_cu);
0907af0c
DE
9535
9536 /* If it's already on the queue, we have nothing to do. */
9537 if (per_cu->queued)
9538 return 0;
9539
9540 /* If the compilation unit is already loaded, just mark it as
9541 used. */
9542 if (per_cu->cu != NULL)
9543 {
9544 per_cu->cu->last_used = 0;
9545 return 0;
9546 }
9547
9548 /* Add it to the queue. */
9549 queue_comp_unit (per_cu, pretend_language);
9550
9551 return 1;
9552}
9553
10b3939b
DJ
9554/* Process the queue. */
9555
9556static void
ed2dc618 9557process_queue (struct dwarf2_per_objfile *dwarf2_per_objfile)
10b3939b
DJ
9558{
9559 struct dwarf2_queue_item *item, *next_item;
9560
b4f54984 9561 if (dwarf_read_debug)
45cfd468
DE
9562 {
9563 fprintf_unfiltered (gdb_stdlog,
9564 "Expanding one or more symtabs of objfile %s ...\n",
4262abfb 9565 objfile_name (dwarf2_per_objfile->objfile));
45cfd468
DE
9566 }
9567
03dd20cc
DJ
9568 /* The queue starts out with one item, but following a DIE reference
9569 may load a new CU, adding it to the end of the queue. */
10b3939b
DJ
9570 for (item = dwarf2_queue; item != NULL; dwarf2_queue = item = next_item)
9571 {
cc12ce38
DE
9572 if ((dwarf2_per_objfile->using_index
9573 ? !item->per_cu->v.quick->compunit_symtab
9574 : (item->per_cu->v.psymtab && !item->per_cu->v.psymtab->readin))
9575 /* Skip dummy CUs. */
9576 && item->per_cu->cu != NULL)
f4dc4d17
DE
9577 {
9578 struct dwarf2_per_cu_data *per_cu = item->per_cu;
73be47f5 9579 unsigned int debug_print_threshold;
247f5c4f 9580 char buf[100];
f4dc4d17 9581
247f5c4f 9582 if (per_cu->is_debug_types)
f4dc4d17 9583 {
247f5c4f
DE
9584 struct signatured_type *sig_type =
9585 (struct signatured_type *) per_cu;
9586
9d8780f0 9587 sprintf (buf, "TU %s at offset %s",
73be47f5 9588 hex_string (sig_type->signature),
9d8780f0 9589 sect_offset_str (per_cu->sect_off));
73be47f5
DE
9590 /* There can be 100s of TUs.
9591 Only print them in verbose mode. */
9592 debug_print_threshold = 2;
f4dc4d17 9593 }
247f5c4f 9594 else
73be47f5 9595 {
9d8780f0
SM
9596 sprintf (buf, "CU at offset %s",
9597 sect_offset_str (per_cu->sect_off));
73be47f5
DE
9598 debug_print_threshold = 1;
9599 }
247f5c4f 9600
b4f54984 9601 if (dwarf_read_debug >= debug_print_threshold)
247f5c4f 9602 fprintf_unfiltered (gdb_stdlog, "Expanding symtab of %s\n", buf);
f4dc4d17
DE
9603
9604 if (per_cu->is_debug_types)
9605 process_full_type_unit (per_cu, item->pretend_language);
9606 else
9607 process_full_comp_unit (per_cu, item->pretend_language);
9608
b4f54984 9609 if (dwarf_read_debug >= debug_print_threshold)
247f5c4f 9610 fprintf_unfiltered (gdb_stdlog, "Done expanding %s\n", buf);
f4dc4d17 9611 }
10b3939b
DJ
9612
9613 item->per_cu->queued = 0;
9614 next_item = item->next;
9615 xfree (item);
9616 }
9617
9618 dwarf2_queue_tail = NULL;
45cfd468 9619
b4f54984 9620 if (dwarf_read_debug)
45cfd468
DE
9621 {
9622 fprintf_unfiltered (gdb_stdlog, "Done expanding symtabs of %s.\n",
4262abfb 9623 objfile_name (dwarf2_per_objfile->objfile));
45cfd468 9624 }
10b3939b
DJ
9625}
9626
10b3939b
DJ
9627/* Read in full symbols for PST, and anything it depends on. */
9628
c906108c 9629static void
fba45db2 9630psymtab_to_symtab_1 (struct partial_symtab *pst)
c906108c 9631{
10b3939b 9632 struct dwarf2_per_cu_data *per_cu;
aaa75496
JB
9633 int i;
9634
95554aad
TT
9635 if (pst->readin)
9636 return;
9637
aaa75496 9638 for (i = 0; i < pst->number_of_dependencies; i++)
95554aad
TT
9639 if (!pst->dependencies[i]->readin
9640 && pst->dependencies[i]->user == NULL)
aaa75496
JB
9641 {
9642 /* Inform about additional files that need to be read in. */
9643 if (info_verbose)
9644 {
a3f17187 9645 /* FIXME: i18n: Need to make this a single string. */
aaa75496
JB
9646 fputs_filtered (" ", gdb_stdout);
9647 wrap_here ("");
9648 fputs_filtered ("and ", gdb_stdout);
9649 wrap_here ("");
9650 printf_filtered ("%s...", pst->dependencies[i]->filename);
0963b4bd 9651 wrap_here (""); /* Flush output. */
aaa75496
JB
9652 gdb_flush (gdb_stdout);
9653 }
9654 psymtab_to_symtab_1 (pst->dependencies[i]);
9655 }
9656
9a3c8263 9657 per_cu = (struct dwarf2_per_cu_data *) pst->read_symtab_private;
10b3939b
DJ
9658
9659 if (per_cu == NULL)
aaa75496
JB
9660 {
9661 /* It's an include file, no symbols to read for it.
9662 Everything is in the parent symtab. */
9663 pst->readin = 1;
9664 return;
9665 }
c906108c 9666
a0f42c21 9667 dw2_do_instantiate_symtab (per_cu);
10b3939b
DJ
9668}
9669
dee91e82
DE
9670/* Trivial hash function for die_info: the hash value of a DIE
9671 is its offset in .debug_info for this objfile. */
10b3939b 9672
dee91e82
DE
9673static hashval_t
9674die_hash (const void *item)
10b3939b 9675{
9a3c8263 9676 const struct die_info *die = (const struct die_info *) item;
6502dd73 9677
9c541725 9678 return to_underlying (die->sect_off);
dee91e82 9679}
63d06c5c 9680
dee91e82
DE
9681/* Trivial comparison function for die_info structures: two DIEs
9682 are equal if they have the same offset. */
98bfdba5 9683
dee91e82
DE
9684static int
9685die_eq (const void *item_lhs, const void *item_rhs)
9686{
9a3c8263
SM
9687 const struct die_info *die_lhs = (const struct die_info *) item_lhs;
9688 const struct die_info *die_rhs = (const struct die_info *) item_rhs;
c906108c 9689
9c541725 9690 return die_lhs->sect_off == die_rhs->sect_off;
dee91e82 9691}
c906108c 9692
dee91e82
DE
9693/* die_reader_func for load_full_comp_unit.
9694 This is identical to read_signatured_type_reader,
9695 but is kept separate for now. */
c906108c 9696
dee91e82
DE
9697static void
9698load_full_comp_unit_reader (const struct die_reader_specs *reader,
d521ce57 9699 const gdb_byte *info_ptr,
dee91e82
DE
9700 struct die_info *comp_unit_die,
9701 int has_children,
9702 void *data)
9703{
9704 struct dwarf2_cu *cu = reader->cu;
9a3c8263 9705 enum language *language_ptr = (enum language *) data;
6caca83c 9706
dee91e82
DE
9707 gdb_assert (cu->die_hash == NULL);
9708 cu->die_hash =
9709 htab_create_alloc_ex (cu->header.length / 12,
9710 die_hash,
9711 die_eq,
9712 NULL,
9713 &cu->comp_unit_obstack,
9714 hashtab_obstack_allocate,
9715 dummy_obstack_deallocate);
e142c38c 9716
dee91e82
DE
9717 if (has_children)
9718 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
9719 &info_ptr, comp_unit_die);
9720 cu->dies = comp_unit_die;
9721 /* comp_unit_die is not stored in die_hash, no need. */
10b3939b
DJ
9722
9723 /* We try not to read any attributes in this function, because not
9cdd5dbd 9724 all CUs needed for references have been loaded yet, and symbol
10b3939b 9725 table processing isn't initialized. But we have to set the CU language,
dee91e82
DE
9726 or we won't be able to build types correctly.
9727 Similarly, if we do not read the producer, we can not apply
9728 producer-specific interpretation. */
95554aad 9729 prepare_one_comp_unit (cu, cu->dies, *language_ptr);
dee91e82 9730}
10b3939b 9731
dee91e82 9732/* Load the DIEs associated with PER_CU into memory. */
a6c727b2 9733
dee91e82 9734static void
95554aad
TT
9735load_full_comp_unit (struct dwarf2_per_cu_data *this_cu,
9736 enum language pretend_language)
dee91e82 9737{
3019eac3 9738 gdb_assert (! this_cu->is_debug_types);
c5b7e1cb 9739
f4dc4d17
DE
9740 init_cutu_and_read_dies (this_cu, NULL, 1, 1,
9741 load_full_comp_unit_reader, &pretend_language);
10b3939b
DJ
9742}
9743
3da10d80
KS
9744/* Add a DIE to the delayed physname list. */
9745
9746static void
9747add_to_method_list (struct type *type, int fnfield_index, int index,
9748 const char *name, struct die_info *die,
9749 struct dwarf2_cu *cu)
9750{
9751 struct delayed_method_info mi;
9752 mi.type = type;
9753 mi.fnfield_index = fnfield_index;
9754 mi.index = index;
9755 mi.name = name;
9756 mi.die = die;
c89b44cd 9757 cu->method_list.push_back (mi);
3da10d80
KS
9758}
9759
3693fdb3
PA
9760/* Check whether [PHYSNAME, PHYSNAME+LEN) ends with a modifier like
9761 "const" / "volatile". If so, decrements LEN by the length of the
9762 modifier and return true. Otherwise return false. */
9763
9764template<size_t N>
9765static bool
9766check_modifier (const char *physname, size_t &len, const char (&mod)[N])
9767{
9768 size_t mod_len = sizeof (mod) - 1;
9769 if (len > mod_len && startswith (physname + (len - mod_len), mod))
9770 {
9771 len -= mod_len;
9772 return true;
9773 }
9774 return false;
9775}
9776
3da10d80
KS
9777/* Compute the physnames of any methods on the CU's method list.
9778
9779 The computation of method physnames is delayed in order to avoid the
9780 (bad) condition that one of the method's formal parameters is of an as yet
9781 incomplete type. */
9782
9783static void
9784compute_delayed_physnames (struct dwarf2_cu *cu)
9785{
3693fdb3 9786 /* Only C++ delays computing physnames. */
c89b44cd 9787 if (cu->method_list.empty ())
3693fdb3
PA
9788 return;
9789 gdb_assert (cu->language == language_cplus);
9790
c89b44cd 9791 for (struct delayed_method_info &mi : cu->method_list)
3da10d80 9792 {
1d06ead6 9793 const char *physname;
3da10d80 9794 struct fn_fieldlist *fn_flp
c89b44cd
TT
9795 = &TYPE_FN_FIELDLIST (mi.type, mi.fnfield_index);
9796 physname = dwarf2_physname (mi.name, mi.die, cu);
9797 TYPE_FN_FIELD_PHYSNAME (fn_flp->fn_fields, mi.index)
005e54bb 9798 = physname ? physname : "";
3693fdb3
PA
9799
9800 /* Since there's no tag to indicate whether a method is a
9801 const/volatile overload, extract that information out of the
9802 demangled name. */
9803 if (physname != NULL)
9804 {
9805 size_t len = strlen (physname);
9806
9807 while (1)
9808 {
9809 if (physname[len] == ')') /* shortcut */
9810 break;
9811 else if (check_modifier (physname, len, " const"))
c89b44cd 9812 TYPE_FN_FIELD_CONST (fn_flp->fn_fields, mi.index) = 1;
3693fdb3 9813 else if (check_modifier (physname, len, " volatile"))
c89b44cd 9814 TYPE_FN_FIELD_VOLATILE (fn_flp->fn_fields, mi.index) = 1;
3693fdb3
PA
9815 else
9816 break;
9817 }
9818 }
3da10d80 9819 }
c89b44cd
TT
9820
9821 /* The list is no longer needed. */
9822 cu->method_list.clear ();
3da10d80
KS
9823}
9824
a766d390
DE
9825/* Go objects should be embedded in a DW_TAG_module DIE,
9826 and it's not clear if/how imported objects will appear.
9827 To keep Go support simple until that's worked out,
9828 go back through what we've read and create something usable.
9829 We could do this while processing each DIE, and feels kinda cleaner,
9830 but that way is more invasive.
9831 This is to, for example, allow the user to type "p var" or "b main"
9832 without having to specify the package name, and allow lookups
9833 of module.object to work in contexts that use the expression
9834 parser. */
9835
9836static void
9837fixup_go_packaging (struct dwarf2_cu *cu)
9838{
9839 char *package_name = NULL;
9840 struct pending *list;
9841 int i;
9842
9843 for (list = global_symbols; list != NULL; list = list->next)
9844 {
9845 for (i = 0; i < list->nsyms; ++i)
9846 {
9847 struct symbol *sym = list->symbol[i];
9848
9849 if (SYMBOL_LANGUAGE (sym) == language_go
9850 && SYMBOL_CLASS (sym) == LOC_BLOCK)
9851 {
9852 char *this_package_name = go_symbol_package_name (sym);
9853
9854 if (this_package_name == NULL)
9855 continue;
9856 if (package_name == NULL)
9857 package_name = this_package_name;
9858 else
9859 {
518817b3
SM
9860 struct objfile *objfile
9861 = cu->per_cu->dwarf2_per_objfile->objfile;
a766d390
DE
9862 if (strcmp (package_name, this_package_name) != 0)
9863 complaint (&symfile_complaints,
9864 _("Symtab %s has objects from two different Go packages: %s and %s"),
08be3fe3
DE
9865 (symbol_symtab (sym) != NULL
9866 ? symtab_to_filename_for_display
9867 (symbol_symtab (sym))
e3b94546 9868 : objfile_name (objfile)),
a766d390
DE
9869 this_package_name, package_name);
9870 xfree (this_package_name);
9871 }
9872 }
9873 }
9874 }
9875
9876 if (package_name != NULL)
9877 {
518817b3 9878 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
34a68019 9879 const char *saved_package_name
224c3ddb
SM
9880 = (const char *) obstack_copy0 (&objfile->per_bfd->storage_obstack,
9881 package_name,
9882 strlen (package_name));
19f392bc
UW
9883 struct type *type = init_type (objfile, TYPE_CODE_MODULE, 0,
9884 saved_package_name);
a766d390
DE
9885 struct symbol *sym;
9886
9887 TYPE_TAG_NAME (type) = TYPE_NAME (type);
9888
e623cf5d 9889 sym = allocate_symbol (objfile);
f85f34ed 9890 SYMBOL_SET_LANGUAGE (sym, language_go, &objfile->objfile_obstack);
86f62fd7
TT
9891 SYMBOL_SET_NAMES (sym, saved_package_name,
9892 strlen (saved_package_name), 0, objfile);
a766d390
DE
9893 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
9894 e.g., "main" finds the "main" module and not C's main(). */
9895 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
f1e6e072 9896 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
a766d390
DE
9897 SYMBOL_TYPE (sym) = type;
9898
9899 add_symbol_to_list (sym, &global_symbols);
9900
9901 xfree (package_name);
9902 }
9903}
9904
c9317f21
TT
9905/* Allocate a fully-qualified name consisting of the two parts on the
9906 obstack. */
9907
9908static const char *
9909rust_fully_qualify (struct obstack *obstack, const char *p1, const char *p2)
9910{
9911 return obconcat (obstack, p1, "::", p2, (char *) NULL);
9912}
9913
9914/* A helper that allocates a struct discriminant_info to attach to a
9915 union type. */
9916
9917static struct discriminant_info *
9918alloc_discriminant_info (struct type *type, int discriminant_index,
9919 int default_index)
9920{
9921 gdb_assert (TYPE_CODE (type) == TYPE_CODE_UNION);
c7b15a66
TT
9922 gdb_assert (discriminant_index == -1
9923 || (discriminant_index >= 0
9924 && discriminant_index < TYPE_NFIELDS (type)));
c9317f21 9925 gdb_assert (default_index == -1
c7b15a66 9926 || (default_index >= 0 && default_index < TYPE_NFIELDS (type)));
c9317f21
TT
9927
9928 TYPE_FLAG_DISCRIMINATED_UNION (type) = 1;
9929
9930 struct discriminant_info *disc
9931 = ((struct discriminant_info *)
9932 TYPE_ZALLOC (type,
9933 offsetof (struct discriminant_info, discriminants)
9934 + TYPE_NFIELDS (type) * sizeof (disc->discriminants[0])));
9935 disc->default_index = default_index;
9936 disc->discriminant_index = discriminant_index;
9937
9938 struct dynamic_prop prop;
9939 prop.kind = PROP_UNDEFINED;
9940 prop.data.baton = disc;
9941
9942 add_dyn_prop (DYN_PROP_DISCRIMINATED, prop, type);
9943
9944 return disc;
9945}
9946
9947/* Some versions of rustc emitted enums in an unusual way.
9948
9949 Ordinary enums were emitted as unions. The first element of each
9950 structure in the union was named "RUST$ENUM$DISR". This element
9951 held the discriminant.
9952
9953 These versions of Rust also implemented the "non-zero"
9954 optimization. When the enum had two values, and one is empty and
9955 the other holds a pointer that cannot be zero, the pointer is used
9956 as the discriminant, with a zero value meaning the empty variant.
9957 Here, the union's first member is of the form
9958 RUST$ENCODED$ENUM$<fieldno>$<fieldno>$...$<variantname>
9959 where the fieldnos are the indices of the fields that should be
9960 traversed in order to find the field (which may be several fields deep)
9961 and the variantname is the name of the variant of the case when the
9962 field is zero.
9963
9964 This function recognizes whether TYPE is of one of these forms,
9965 and, if so, smashes it to be a variant type. */
9966
9967static void
9968quirk_rust_enum (struct type *type, struct objfile *objfile)
9969{
9970 gdb_assert (TYPE_CODE (type) == TYPE_CODE_UNION);
9971
9972 /* We don't need to deal with empty enums. */
9973 if (TYPE_NFIELDS (type) == 0)
9974 return;
9975
9976#define RUST_ENUM_PREFIX "RUST$ENCODED$ENUM$"
9977 if (TYPE_NFIELDS (type) == 1
9978 && startswith (TYPE_FIELD_NAME (type, 0), RUST_ENUM_PREFIX))
9979 {
9980 const char *name = TYPE_FIELD_NAME (type, 0) + strlen (RUST_ENUM_PREFIX);
9981
9982 /* Decode the field name to find the offset of the
9983 discriminant. */
9984 ULONGEST bit_offset = 0;
9985 struct type *field_type = TYPE_FIELD_TYPE (type, 0);
9986 while (name[0] >= '0' && name[0] <= '9')
9987 {
9988 char *tail;
9989 unsigned long index = strtoul (name, &tail, 10);
9990 name = tail;
9991 if (*name != '$'
9992 || index >= TYPE_NFIELDS (field_type)
9993 || (TYPE_FIELD_LOC_KIND (field_type, index)
9994 != FIELD_LOC_KIND_BITPOS))
9995 {
9996 complaint (&symfile_complaints,
9997 _("Could not parse Rust enum encoding string \"%s\""
9998 "[in module %s]"),
9999 TYPE_FIELD_NAME (type, 0),
10000 objfile_name (objfile));
10001 return;
10002 }
10003 ++name;
10004
10005 bit_offset += TYPE_FIELD_BITPOS (field_type, index);
10006 field_type = TYPE_FIELD_TYPE (field_type, index);
10007 }
10008
10009 /* Make a union to hold the variants. */
10010 struct type *union_type = alloc_type (objfile);
10011 TYPE_CODE (union_type) = TYPE_CODE_UNION;
10012 TYPE_NFIELDS (union_type) = 3;
10013 TYPE_FIELDS (union_type)
10014 = (struct field *) TYPE_ZALLOC (type, 3 * sizeof (struct field));
10015 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
10016
10017 /* Put the discriminant must at index 0. */
10018 TYPE_FIELD_TYPE (union_type, 0) = field_type;
10019 TYPE_FIELD_ARTIFICIAL (union_type, 0) = 1;
10020 TYPE_FIELD_NAME (union_type, 0) = "<<discriminant>>";
10021 SET_FIELD_BITPOS (TYPE_FIELD (union_type, 0), bit_offset);
10022
10023 /* The order of fields doesn't really matter, so put the real
10024 field at index 1 and the data-less field at index 2. */
10025 struct discriminant_info *disc
10026 = alloc_discriminant_info (union_type, 0, 1);
10027 TYPE_FIELD (union_type, 1) = TYPE_FIELD (type, 0);
10028 TYPE_FIELD_NAME (union_type, 1)
10029 = rust_last_path_segment (TYPE_NAME (TYPE_FIELD_TYPE (union_type, 1)));
10030 TYPE_NAME (TYPE_FIELD_TYPE (union_type, 1))
10031 = rust_fully_qualify (&objfile->objfile_obstack, TYPE_NAME (type),
10032 TYPE_FIELD_NAME (union_type, 1));
10033
10034 const char *dataless_name
10035 = rust_fully_qualify (&objfile->objfile_obstack, TYPE_NAME (type),
10036 name);
10037 struct type *dataless_type = init_type (objfile, TYPE_CODE_VOID, 0,
10038 dataless_name);
10039 TYPE_FIELD_TYPE (union_type, 2) = dataless_type;
10040 /* NAME points into the original discriminant name, which
10041 already has the correct lifetime. */
10042 TYPE_FIELD_NAME (union_type, 2) = name;
10043 SET_FIELD_BITPOS (TYPE_FIELD (union_type, 2), 0);
10044 disc->discriminants[2] = 0;
10045
10046 /* Smash this type to be a structure type. We have to do this
10047 because the type has already been recorded. */
10048 TYPE_CODE (type) = TYPE_CODE_STRUCT;
10049 TYPE_NFIELDS (type) = 1;
10050 TYPE_FIELDS (type)
10051 = (struct field *) TYPE_ZALLOC (type, sizeof (struct field));
10052
10053 /* Install the variant part. */
10054 TYPE_FIELD_TYPE (type, 0) = union_type;
10055 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
10056 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
10057 }
10058 else if (TYPE_NFIELDS (type) == 1)
10059 {
10060 /* We assume that a union with a single field is a univariant
10061 enum. */
10062 /* Smash this type to be a structure type. We have to do this
10063 because the type has already been recorded. */
10064 TYPE_CODE (type) = TYPE_CODE_STRUCT;
10065
10066 /* Make a union to hold the variants. */
10067 struct type *union_type = alloc_type (objfile);
10068 TYPE_CODE (union_type) = TYPE_CODE_UNION;
10069 TYPE_NFIELDS (union_type) = TYPE_NFIELDS (type);
10070 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
10071 TYPE_FIELDS (union_type) = TYPE_FIELDS (type);
10072
10073 struct type *field_type = TYPE_FIELD_TYPE (union_type, 0);
10074 const char *variant_name
10075 = rust_last_path_segment (TYPE_NAME (field_type));
10076 TYPE_FIELD_NAME (union_type, 0) = variant_name;
10077 TYPE_NAME (field_type)
10078 = rust_fully_qualify (&objfile->objfile_obstack,
c7b15a66 10079 TYPE_NAME (type), variant_name);
c9317f21
TT
10080
10081 /* Install the union in the outer struct type. */
10082 TYPE_NFIELDS (type) = 1;
10083 TYPE_FIELDS (type)
10084 = (struct field *) TYPE_ZALLOC (union_type, sizeof (struct field));
10085 TYPE_FIELD_TYPE (type, 0) = union_type;
10086 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
10087 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
10088
10089 alloc_discriminant_info (union_type, -1, 0);
10090 }
10091 else
10092 {
10093 struct type *disr_type = nullptr;
10094 for (int i = 0; i < TYPE_NFIELDS (type); ++i)
10095 {
10096 disr_type = TYPE_FIELD_TYPE (type, i);
10097
10098 if (TYPE_NFIELDS (disr_type) == 0)
10099 {
10100 /* Could be data-less variant, so keep going. */
10101 }
10102 else if (strcmp (TYPE_FIELD_NAME (disr_type, 0),
10103 "RUST$ENUM$DISR") != 0)
10104 {
10105 /* Not a Rust enum. */
10106 return;
10107 }
10108 else
10109 {
10110 /* Found one. */
10111 break;
10112 }
10113 }
10114
10115 /* If we got here without a discriminant, then it's probably
10116 just a union. */
10117 if (disr_type == nullptr)
10118 return;
10119
10120 /* Smash this type to be a structure type. We have to do this
10121 because the type has already been recorded. */
10122 TYPE_CODE (type) = TYPE_CODE_STRUCT;
10123
10124 /* Make a union to hold the variants. */
10125 struct field *disr_field = &TYPE_FIELD (disr_type, 0);
10126 struct type *union_type = alloc_type (objfile);
10127 TYPE_CODE (union_type) = TYPE_CODE_UNION;
10128 TYPE_NFIELDS (union_type) = 1 + TYPE_NFIELDS (type);
10129 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
10130 TYPE_FIELDS (union_type)
10131 = (struct field *) TYPE_ZALLOC (union_type,
10132 (TYPE_NFIELDS (union_type)
10133 * sizeof (struct field)));
10134
10135 memcpy (TYPE_FIELDS (union_type) + 1, TYPE_FIELDS (type),
10136 TYPE_NFIELDS (type) * sizeof (struct field));
10137
10138 /* Install the discriminant at index 0 in the union. */
10139 TYPE_FIELD (union_type, 0) = *disr_field;
10140 TYPE_FIELD_ARTIFICIAL (union_type, 0) = 1;
10141 TYPE_FIELD_NAME (union_type, 0) = "<<discriminant>>";
10142
10143 /* Install the union in the outer struct type. */
10144 TYPE_FIELD_TYPE (type, 0) = union_type;
10145 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
10146 TYPE_NFIELDS (type) = 1;
10147
10148 /* Set the size and offset of the union type. */
10149 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
10150
10151 /* We need a way to find the correct discriminant given a
10152 variant name. For convenience we build a map here. */
10153 struct type *enum_type = FIELD_TYPE (*disr_field);
10154 std::unordered_map<std::string, ULONGEST> discriminant_map;
10155 for (int i = 0; i < TYPE_NFIELDS (enum_type); ++i)
10156 {
10157 if (TYPE_FIELD_LOC_KIND (enum_type, i) == FIELD_LOC_KIND_ENUMVAL)
10158 {
10159 const char *name
10160 = rust_last_path_segment (TYPE_FIELD_NAME (enum_type, i));
10161 discriminant_map[name] = TYPE_FIELD_ENUMVAL (enum_type, i);
10162 }
10163 }
10164
10165 int n_fields = TYPE_NFIELDS (union_type);
10166 struct discriminant_info *disc
10167 = alloc_discriminant_info (union_type, 0, -1);
10168 /* Skip the discriminant here. */
10169 for (int i = 1; i < n_fields; ++i)
10170 {
10171 /* Find the final word in the name of this variant's type.
10172 That name can be used to look up the correct
10173 discriminant. */
10174 const char *variant_name
10175 = rust_last_path_segment (TYPE_NAME (TYPE_FIELD_TYPE (union_type,
10176 i)));
10177
10178 auto iter = discriminant_map.find (variant_name);
10179 if (iter != discriminant_map.end ())
10180 disc->discriminants[i] = iter->second;
10181
10182 /* Remove the discriminant field. */
10183 struct type *sub_type = TYPE_FIELD_TYPE (union_type, i);
10184 --TYPE_NFIELDS (sub_type);
10185 ++TYPE_FIELDS (sub_type);
10186 TYPE_FIELD_NAME (union_type, i) = variant_name;
10187 TYPE_NAME (sub_type)
10188 = rust_fully_qualify (&objfile->objfile_obstack,
10189 TYPE_NAME (type), variant_name);
10190 }
10191 }
10192}
10193
10194/* Rewrite some Rust unions to be structures with variants parts. */
10195
10196static void
10197rust_union_quirks (struct dwarf2_cu *cu)
10198{
10199 gdb_assert (cu->language == language_rust);
10200 for (struct type *type : cu->rust_unions)
10201 quirk_rust_enum (type, cu->per_cu->dwarf2_per_objfile->objfile);
10202}
10203
95554aad
TT
10204/* Return the symtab for PER_CU. This works properly regardless of
10205 whether we're using the index or psymtabs. */
10206
43f3e411
DE
10207static struct compunit_symtab *
10208get_compunit_symtab (struct dwarf2_per_cu_data *per_cu)
95554aad 10209{
ed2dc618 10210 return (per_cu->dwarf2_per_objfile->using_index
43f3e411
DE
10211 ? per_cu->v.quick->compunit_symtab
10212 : per_cu->v.psymtab->compunit_symtab);
95554aad
TT
10213}
10214
10215/* A helper function for computing the list of all symbol tables
10216 included by PER_CU. */
10217
10218static void
43f3e411 10219recursively_compute_inclusions (VEC (compunit_symtab_ptr) **result,
ec94af83 10220 htab_t all_children, htab_t all_type_symtabs,
f9125b6c 10221 struct dwarf2_per_cu_data *per_cu,
43f3e411 10222 struct compunit_symtab *immediate_parent)
95554aad
TT
10223{
10224 void **slot;
10225 int ix;
43f3e411 10226 struct compunit_symtab *cust;
95554aad
TT
10227 struct dwarf2_per_cu_data *iter;
10228
10229 slot = htab_find_slot (all_children, per_cu, INSERT);
10230 if (*slot != NULL)
10231 {
10232 /* This inclusion and its children have been processed. */
10233 return;
10234 }
10235
10236 *slot = per_cu;
10237 /* Only add a CU if it has a symbol table. */
43f3e411
DE
10238 cust = get_compunit_symtab (per_cu);
10239 if (cust != NULL)
ec94af83
DE
10240 {
10241 /* If this is a type unit only add its symbol table if we haven't
10242 seen it yet (type unit per_cu's can share symtabs). */
10243 if (per_cu->is_debug_types)
10244 {
43f3e411 10245 slot = htab_find_slot (all_type_symtabs, cust, INSERT);
ec94af83
DE
10246 if (*slot == NULL)
10247 {
43f3e411
DE
10248 *slot = cust;
10249 VEC_safe_push (compunit_symtab_ptr, *result, cust);
10250 if (cust->user == NULL)
10251 cust->user = immediate_parent;
ec94af83
DE
10252 }
10253 }
10254 else
f9125b6c 10255 {
43f3e411
DE
10256 VEC_safe_push (compunit_symtab_ptr, *result, cust);
10257 if (cust->user == NULL)
10258 cust->user = immediate_parent;
f9125b6c 10259 }
ec94af83 10260 }
95554aad
TT
10261
10262 for (ix = 0;
796a7ff8 10263 VEC_iterate (dwarf2_per_cu_ptr, per_cu->imported_symtabs, ix, iter);
95554aad 10264 ++ix)
ec94af83
DE
10265 {
10266 recursively_compute_inclusions (result, all_children,
43f3e411 10267 all_type_symtabs, iter, cust);
ec94af83 10268 }
95554aad
TT
10269}
10270
43f3e411 10271/* Compute the compunit_symtab 'includes' fields for the compunit_symtab of
95554aad
TT
10272 PER_CU. */
10273
10274static void
43f3e411 10275compute_compunit_symtab_includes (struct dwarf2_per_cu_data *per_cu)
95554aad 10276{
f4dc4d17
DE
10277 gdb_assert (! per_cu->is_debug_types);
10278
796a7ff8 10279 if (!VEC_empty (dwarf2_per_cu_ptr, per_cu->imported_symtabs))
95554aad
TT
10280 {
10281 int ix, len;
ec94af83 10282 struct dwarf2_per_cu_data *per_cu_iter;
43f3e411
DE
10283 struct compunit_symtab *compunit_symtab_iter;
10284 VEC (compunit_symtab_ptr) *result_symtabs = NULL;
ec94af83 10285 htab_t all_children, all_type_symtabs;
43f3e411 10286 struct compunit_symtab *cust = get_compunit_symtab (per_cu);
95554aad
TT
10287
10288 /* If we don't have a symtab, we can just skip this case. */
43f3e411 10289 if (cust == NULL)
95554aad
TT
10290 return;
10291
10292 all_children = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
10293 NULL, xcalloc, xfree);
ec94af83
DE
10294 all_type_symtabs = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
10295 NULL, xcalloc, xfree);
95554aad
TT
10296
10297 for (ix = 0;
796a7ff8 10298 VEC_iterate (dwarf2_per_cu_ptr, per_cu->imported_symtabs,
ec94af83 10299 ix, per_cu_iter);
95554aad 10300 ++ix)
ec94af83
DE
10301 {
10302 recursively_compute_inclusions (&result_symtabs, all_children,
f9125b6c 10303 all_type_symtabs, per_cu_iter,
43f3e411 10304 cust);
ec94af83 10305 }
95554aad 10306
ec94af83 10307 /* Now we have a transitive closure of all the included symtabs. */
43f3e411
DE
10308 len = VEC_length (compunit_symtab_ptr, result_symtabs);
10309 cust->includes
ed2dc618 10310 = XOBNEWVEC (&per_cu->dwarf2_per_objfile->objfile->objfile_obstack,
8d749320 10311 struct compunit_symtab *, len + 1);
95554aad 10312 for (ix = 0;
43f3e411
DE
10313 VEC_iterate (compunit_symtab_ptr, result_symtabs, ix,
10314 compunit_symtab_iter);
95554aad 10315 ++ix)
43f3e411
DE
10316 cust->includes[ix] = compunit_symtab_iter;
10317 cust->includes[len] = NULL;
95554aad 10318
43f3e411 10319 VEC_free (compunit_symtab_ptr, result_symtabs);
95554aad 10320 htab_delete (all_children);
ec94af83 10321 htab_delete (all_type_symtabs);
95554aad
TT
10322 }
10323}
10324
10325/* Compute the 'includes' field for the symtabs of all the CUs we just
10326 read. */
10327
10328static void
ed2dc618 10329process_cu_includes (struct dwarf2_per_objfile *dwarf2_per_objfile)
95554aad
TT
10330{
10331 int ix;
10332 struct dwarf2_per_cu_data *iter;
10333
10334 for (ix = 0;
10335 VEC_iterate (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus,
10336 ix, iter);
10337 ++ix)
f4dc4d17
DE
10338 {
10339 if (! iter->is_debug_types)
43f3e411 10340 compute_compunit_symtab_includes (iter);
f4dc4d17 10341 }
95554aad
TT
10342
10343 VEC_free (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus);
10344}
10345
9cdd5dbd 10346/* Generate full symbol information for PER_CU, whose DIEs have
10b3939b
DJ
10347 already been loaded into memory. */
10348
10349static void
95554aad
TT
10350process_full_comp_unit (struct dwarf2_per_cu_data *per_cu,
10351 enum language pretend_language)
10b3939b 10352{
10b3939b 10353 struct dwarf2_cu *cu = per_cu->cu;
ed2dc618
SM
10354 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
10355 struct objfile *objfile = dwarf2_per_objfile->objfile;
3e29f34a 10356 struct gdbarch *gdbarch = get_objfile_arch (objfile);
10b3939b 10357 CORE_ADDR lowpc, highpc;
43f3e411 10358 struct compunit_symtab *cust;
10b3939b 10359 CORE_ADDR baseaddr;
4359dff1 10360 struct block *static_block;
3e29f34a 10361 CORE_ADDR addr;
10b3939b
DJ
10362
10363 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
10364
10b3939b 10365 buildsym_init ();
33c7c59d 10366 scoped_free_pendings free_pending;
c89b44cd
TT
10367
10368 /* Clear the list here in case something was left over. */
10369 cu->method_list.clear ();
10b3939b
DJ
10370
10371 cu->list_in_scope = &file_symbols;
c906108c 10372
95554aad
TT
10373 cu->language = pretend_language;
10374 cu->language_defn = language_def (cu->language);
10375
c906108c 10376 /* Do line number decoding in read_file_scope () */
10b3939b 10377 process_die (cu->dies, cu);
c906108c 10378
a766d390
DE
10379 /* For now fudge the Go package. */
10380 if (cu->language == language_go)
10381 fixup_go_packaging (cu);
10382
3da10d80
KS
10383 /* Now that we have processed all the DIEs in the CU, all the types
10384 should be complete, and it should now be safe to compute all of the
10385 physnames. */
10386 compute_delayed_physnames (cu);
3da10d80 10387
c9317f21
TT
10388 if (cu->language == language_rust)
10389 rust_union_quirks (cu);
10390
fae299cd
DC
10391 /* Some compilers don't define a DW_AT_high_pc attribute for the
10392 compilation unit. If the DW_AT_high_pc is missing, synthesize
10393 it, by scanning the DIE's below the compilation unit. */
10b3939b 10394 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
c906108c 10395
3e29f34a
MR
10396 addr = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
10397 static_block = end_symtab_get_static_block (addr, 0, 1);
4359dff1
JK
10398
10399 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
10400 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
10401 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
10402 addrmap to help ensure it has an accurate map of pc values belonging to
10403 this comp unit. */
10404 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
10405
43f3e411
DE
10406 cust = end_symtab_from_static_block (static_block,
10407 SECT_OFF_TEXT (objfile), 0);
c906108c 10408
43f3e411 10409 if (cust != NULL)
c906108c 10410 {
df15bd07 10411 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
4632c0d0 10412
8be455d7
JK
10413 /* Set symtab language to language from DW_AT_language. If the
10414 compilation is from a C file generated by language preprocessors, do
10415 not set the language if it was already deduced by start_subfile. */
43f3e411 10416 if (!(cu->language == language_c
40e3ad0e 10417 && COMPUNIT_FILETABS (cust)->language != language_unknown))
43f3e411 10418 COMPUNIT_FILETABS (cust)->language = cu->language;
8be455d7
JK
10419
10420 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
10421 produce DW_AT_location with location lists but it can be possibly
ab260dad
JK
10422 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
10423 there were bugs in prologue debug info, fixed later in GCC-4.5
10424 by "unwind info for epilogues" patch (which is not directly related).
8be455d7
JK
10425
10426 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
10427 needed, it would be wrong due to missing DW_AT_producer there.
10428
10429 Still one can confuse GDB by using non-standard GCC compilation
10430 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
10431 */
ab260dad 10432 if (cu->has_loclist && gcc_4_minor >= 5)
43f3e411 10433 cust->locations_valid = 1;
e0d00bc7
JK
10434
10435 if (gcc_4_minor >= 5)
43f3e411 10436 cust->epilogue_unwind_valid = 1;
96408a79 10437
43f3e411 10438 cust->call_site_htab = cu->call_site_htab;
c906108c 10439 }
9291a0cd
TT
10440
10441 if (dwarf2_per_objfile->using_index)
43f3e411 10442 per_cu->v.quick->compunit_symtab = cust;
9291a0cd
TT
10443 else
10444 {
10445 struct partial_symtab *pst = per_cu->v.psymtab;
43f3e411 10446 pst->compunit_symtab = cust;
9291a0cd
TT
10447 pst->readin = 1;
10448 }
c906108c 10449
95554aad
TT
10450 /* Push it for inclusion processing later. */
10451 VEC_safe_push (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus, per_cu);
f4dc4d17 10452}
45cfd468 10453
f4dc4d17
DE
10454/* Generate full symbol information for type unit PER_CU, whose DIEs have
10455 already been loaded into memory. */
10456
10457static void
10458process_full_type_unit (struct dwarf2_per_cu_data *per_cu,
10459 enum language pretend_language)
10460{
10461 struct dwarf2_cu *cu = per_cu->cu;
ed2dc618
SM
10462 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
10463 struct objfile *objfile = dwarf2_per_objfile->objfile;
43f3e411 10464 struct compunit_symtab *cust;
0186c6a7
DE
10465 struct signatured_type *sig_type;
10466
10467 gdb_assert (per_cu->is_debug_types);
10468 sig_type = (struct signatured_type *) per_cu;
f4dc4d17
DE
10469
10470 buildsym_init ();
33c7c59d 10471 scoped_free_pendings free_pending;
c89b44cd
TT
10472
10473 /* Clear the list here in case something was left over. */
10474 cu->method_list.clear ();
f4dc4d17
DE
10475
10476 cu->list_in_scope = &file_symbols;
10477
10478 cu->language = pretend_language;
10479 cu->language_defn = language_def (cu->language);
10480
10481 /* The symbol tables are set up in read_type_unit_scope. */
10482 process_die (cu->dies, cu);
10483
10484 /* For now fudge the Go package. */
10485 if (cu->language == language_go)
10486 fixup_go_packaging (cu);
10487
10488 /* Now that we have processed all the DIEs in the CU, all the types
10489 should be complete, and it should now be safe to compute all of the
10490 physnames. */
10491 compute_delayed_physnames (cu);
f4dc4d17 10492
c9317f21
TT
10493 if (cu->language == language_rust)
10494 rust_union_quirks (cu);
10495
f4dc4d17
DE
10496 /* TUs share symbol tables.
10497 If this is the first TU to use this symtab, complete the construction
094b34ac
DE
10498 of it with end_expandable_symtab. Otherwise, complete the addition of
10499 this TU's symbols to the existing symtab. */
43f3e411 10500 if (sig_type->type_unit_group->compunit_symtab == NULL)
45cfd468 10501 {
43f3e411
DE
10502 cust = end_expandable_symtab (0, SECT_OFF_TEXT (objfile));
10503 sig_type->type_unit_group->compunit_symtab = cust;
f4dc4d17 10504
43f3e411 10505 if (cust != NULL)
f4dc4d17
DE
10506 {
10507 /* Set symtab language to language from DW_AT_language. If the
10508 compilation is from a C file generated by language preprocessors,
10509 do not set the language if it was already deduced by
10510 start_subfile. */
43f3e411
DE
10511 if (!(cu->language == language_c
10512 && COMPUNIT_FILETABS (cust)->language != language_c))
10513 COMPUNIT_FILETABS (cust)->language = cu->language;
f4dc4d17
DE
10514 }
10515 }
10516 else
10517 {
0ab9ce85 10518 augment_type_symtab ();
43f3e411 10519 cust = sig_type->type_unit_group->compunit_symtab;
f4dc4d17
DE
10520 }
10521
10522 if (dwarf2_per_objfile->using_index)
43f3e411 10523 per_cu->v.quick->compunit_symtab = cust;
f4dc4d17
DE
10524 else
10525 {
10526 struct partial_symtab *pst = per_cu->v.psymtab;
43f3e411 10527 pst->compunit_symtab = cust;
f4dc4d17 10528 pst->readin = 1;
45cfd468 10529 }
c906108c
SS
10530}
10531
95554aad
TT
10532/* Process an imported unit DIE. */
10533
10534static void
10535process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
10536{
10537 struct attribute *attr;
10538
f4dc4d17
DE
10539 /* For now we don't handle imported units in type units. */
10540 if (cu->per_cu->is_debug_types)
10541 {
10542 error (_("Dwarf Error: DW_TAG_imported_unit is not"
10543 " supported in type units [in module %s]"),
518817b3 10544 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
f4dc4d17
DE
10545 }
10546
95554aad
TT
10547 attr = dwarf2_attr (die, DW_AT_import, cu);
10548 if (attr != NULL)
10549 {
9c541725
PA
10550 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
10551 bool is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
10552 dwarf2_per_cu_data *per_cu
e3b94546 10553 = dwarf2_find_containing_comp_unit (sect_off, is_dwz,
518817b3 10554 cu->per_cu->dwarf2_per_objfile);
95554aad 10555
69d751e3 10556 /* If necessary, add it to the queue and load its DIEs. */
95554aad
TT
10557 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
10558 load_full_comp_unit (per_cu, cu->language);
10559
796a7ff8 10560 VEC_safe_push (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs,
95554aad
TT
10561 per_cu);
10562 }
10563}
10564
4c8aa72d
PA
10565/* RAII object that represents a process_die scope: i.e.,
10566 starts/finishes processing a DIE. */
10567class process_die_scope
adde2bff 10568{
4c8aa72d
PA
10569public:
10570 process_die_scope (die_info *die, dwarf2_cu *cu)
10571 : m_die (die), m_cu (cu)
10572 {
10573 /* We should only be processing DIEs not already in process. */
10574 gdb_assert (!m_die->in_process);
10575 m_die->in_process = true;
10576 }
8c3cb9fa 10577
4c8aa72d
PA
10578 ~process_die_scope ()
10579 {
10580 m_die->in_process = false;
10581
10582 /* If we're done processing the DIE for the CU that owns the line
10583 header, we don't need the line header anymore. */
10584 if (m_cu->line_header_die_owner == m_die)
10585 {
10586 delete m_cu->line_header;
10587 m_cu->line_header = NULL;
10588 m_cu->line_header_die_owner = NULL;
10589 }
10590 }
10591
10592private:
10593 die_info *m_die;
10594 dwarf2_cu *m_cu;
10595};
adde2bff 10596
c906108c
SS
10597/* Process a die and its children. */
10598
10599static void
e7c27a73 10600process_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 10601{
4c8aa72d 10602 process_die_scope scope (die, cu);
adde2bff 10603
c906108c
SS
10604 switch (die->tag)
10605 {
10606 case DW_TAG_padding:
10607 break;
10608 case DW_TAG_compile_unit:
95554aad 10609 case DW_TAG_partial_unit:
e7c27a73 10610 read_file_scope (die, cu);
c906108c 10611 break;
348e048f
DE
10612 case DW_TAG_type_unit:
10613 read_type_unit_scope (die, cu);
10614 break;
c906108c 10615 case DW_TAG_subprogram:
c906108c 10616 case DW_TAG_inlined_subroutine:
edb3359d 10617 read_func_scope (die, cu);
c906108c
SS
10618 break;
10619 case DW_TAG_lexical_block:
14898363
L
10620 case DW_TAG_try_block:
10621 case DW_TAG_catch_block:
e7c27a73 10622 read_lexical_block_scope (die, cu);
c906108c 10623 break;
216f72a1 10624 case DW_TAG_call_site:
96408a79
SA
10625 case DW_TAG_GNU_call_site:
10626 read_call_site_scope (die, cu);
10627 break;
c906108c 10628 case DW_TAG_class_type:
680b30c7 10629 case DW_TAG_interface_type:
c906108c
SS
10630 case DW_TAG_structure_type:
10631 case DW_TAG_union_type:
134d01f1 10632 process_structure_scope (die, cu);
c906108c
SS
10633 break;
10634 case DW_TAG_enumeration_type:
134d01f1 10635 process_enumeration_scope (die, cu);
c906108c 10636 break;
134d01f1 10637
f792889a
DJ
10638 /* These dies have a type, but processing them does not create
10639 a symbol or recurse to process the children. Therefore we can
10640 read them on-demand through read_type_die. */
c906108c 10641 case DW_TAG_subroutine_type:
72019c9c 10642 case DW_TAG_set_type:
c906108c 10643 case DW_TAG_array_type:
c906108c 10644 case DW_TAG_pointer_type:
c906108c 10645 case DW_TAG_ptr_to_member_type:
c906108c 10646 case DW_TAG_reference_type:
4297a3f0 10647 case DW_TAG_rvalue_reference_type:
c906108c 10648 case DW_TAG_string_type:
c906108c 10649 break;
134d01f1 10650
c906108c 10651 case DW_TAG_base_type:
a02abb62 10652 case DW_TAG_subrange_type:
cb249c71 10653 case DW_TAG_typedef:
134d01f1
DJ
10654 /* Add a typedef symbol for the type definition, if it has a
10655 DW_AT_name. */
f792889a 10656 new_symbol (die, read_type_die (die, cu), cu);
a02abb62 10657 break;
c906108c 10658 case DW_TAG_common_block:
e7c27a73 10659 read_common_block (die, cu);
c906108c
SS
10660 break;
10661 case DW_TAG_common_inclusion:
10662 break;
d9fa45fe 10663 case DW_TAG_namespace:
4d4ec4e5 10664 cu->processing_has_namespace_info = 1;
e7c27a73 10665 read_namespace (die, cu);
d9fa45fe 10666 break;
5d7cb8df 10667 case DW_TAG_module:
4d4ec4e5 10668 cu->processing_has_namespace_info = 1;
5d7cb8df
JK
10669 read_module (die, cu);
10670 break;
d9fa45fe 10671 case DW_TAG_imported_declaration:
74921315
KS
10672 cu->processing_has_namespace_info = 1;
10673 if (read_namespace_alias (die, cu))
10674 break;
10675 /* The declaration is not a global namespace alias: fall through. */
d9fa45fe 10676 case DW_TAG_imported_module:
4d4ec4e5 10677 cu->processing_has_namespace_info = 1;
27aa8d6a
SW
10678 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
10679 || cu->language != language_fortran))
10680 complaint (&symfile_complaints, _("Tag '%s' has unexpected children"),
10681 dwarf_tag_name (die->tag));
10682 read_import_statement (die, cu);
d9fa45fe 10683 break;
95554aad
TT
10684
10685 case DW_TAG_imported_unit:
10686 process_imported_unit_die (die, cu);
10687 break;
10688
71a3c369
TT
10689 case DW_TAG_variable:
10690 read_variable (die, cu);
10691 break;
10692
c906108c 10693 default:
e7c27a73 10694 new_symbol (die, NULL, cu);
c906108c
SS
10695 break;
10696 }
10697}
ca69b9e6
DE
10698\f
10699/* DWARF name computation. */
c906108c 10700
94af9270
KS
10701/* A helper function for dwarf2_compute_name which determines whether DIE
10702 needs to have the name of the scope prepended to the name listed in the
10703 die. */
10704
10705static int
10706die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
10707{
1c809c68
TT
10708 struct attribute *attr;
10709
94af9270
KS
10710 switch (die->tag)
10711 {
10712 case DW_TAG_namespace:
10713 case DW_TAG_typedef:
10714 case DW_TAG_class_type:
10715 case DW_TAG_interface_type:
10716 case DW_TAG_structure_type:
10717 case DW_TAG_union_type:
10718 case DW_TAG_enumeration_type:
10719 case DW_TAG_enumerator:
10720 case DW_TAG_subprogram:
08a76f8a 10721 case DW_TAG_inlined_subroutine:
94af9270 10722 case DW_TAG_member:
74921315 10723 case DW_TAG_imported_declaration:
94af9270
KS
10724 return 1;
10725
10726 case DW_TAG_variable:
c2b0a229 10727 case DW_TAG_constant:
94af9270
KS
10728 /* We only need to prefix "globally" visible variables. These include
10729 any variable marked with DW_AT_external or any variable that
10730 lives in a namespace. [Variables in anonymous namespaces
10731 require prefixing, but they are not DW_AT_external.] */
10732
10733 if (dwarf2_attr (die, DW_AT_specification, cu))
10734 {
10735 struct dwarf2_cu *spec_cu = cu;
9a619af0 10736
94af9270
KS
10737 return die_needs_namespace (die_specification (die, &spec_cu),
10738 spec_cu);
10739 }
10740
1c809c68 10741 attr = dwarf2_attr (die, DW_AT_external, cu);
f55ee35c
JK
10742 if (attr == NULL && die->parent->tag != DW_TAG_namespace
10743 && die->parent->tag != DW_TAG_module)
1c809c68
TT
10744 return 0;
10745 /* A variable in a lexical block of some kind does not need a
10746 namespace, even though in C++ such variables may be external
10747 and have a mangled name. */
10748 if (die->parent->tag == DW_TAG_lexical_block
10749 || die->parent->tag == DW_TAG_try_block
1054b214
TT
10750 || die->parent->tag == DW_TAG_catch_block
10751 || die->parent->tag == DW_TAG_subprogram)
1c809c68
TT
10752 return 0;
10753 return 1;
94af9270
KS
10754
10755 default:
10756 return 0;
10757 }
10758}
10759
73b9be8b
KS
10760/* Return the DIE's linkage name attribute, either DW_AT_linkage_name
10761 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10762 defined for the given DIE. */
10763
10764static struct attribute *
10765dw2_linkage_name_attr (struct die_info *die, struct dwarf2_cu *cu)
10766{
10767 struct attribute *attr;
10768
10769 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
10770 if (attr == NULL)
10771 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
10772
10773 return attr;
10774}
10775
10776/* Return the DIE's linkage name as a string, either DW_AT_linkage_name
10777 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10778 defined for the given DIE. */
10779
10780static const char *
10781dw2_linkage_name (struct die_info *die, struct dwarf2_cu *cu)
10782{
10783 const char *linkage_name;
10784
10785 linkage_name = dwarf2_string_attr (die, DW_AT_linkage_name, cu);
10786 if (linkage_name == NULL)
10787 linkage_name = dwarf2_string_attr (die, DW_AT_MIPS_linkage_name, cu);
10788
10789 return linkage_name;
10790}
10791
94af9270 10792/* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
a766d390 10793 compute the physname for the object, which include a method's:
9c37b5ae 10794 - formal parameters (C++),
a766d390 10795 - receiver type (Go),
a766d390
DE
10796
10797 The term "physname" is a bit confusing.
10798 For C++, for example, it is the demangled name.
10799 For Go, for example, it's the mangled name.
94af9270 10800
af6b7be1
JB
10801 For Ada, return the DIE's linkage name rather than the fully qualified
10802 name. PHYSNAME is ignored..
10803
94af9270
KS
10804 The result is allocated on the objfile_obstack and canonicalized. */
10805
10806static const char *
15d034d0
TT
10807dwarf2_compute_name (const char *name,
10808 struct die_info *die, struct dwarf2_cu *cu,
94af9270
KS
10809 int physname)
10810{
518817b3 10811 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
bb5ed363 10812
94af9270
KS
10813 if (name == NULL)
10814 name = dwarf2_name (die, cu);
10815
2ee7123e
DE
10816 /* For Fortran GDB prefers DW_AT_*linkage_name for the physname if present
10817 but otherwise compute it by typename_concat inside GDB.
10818 FIXME: Actually this is not really true, or at least not always true.
10819 It's all very confusing. SYMBOL_SET_NAMES doesn't try to demangle
5e2db402 10820 Fortran names because there is no mangling standard. So new_symbol
2ee7123e
DE
10821 will set the demangled name to the result of dwarf2_full_name, and it is
10822 the demangled name that GDB uses if it exists. */
f55ee35c
JK
10823 if (cu->language == language_ada
10824 || (cu->language == language_fortran && physname))
10825 {
10826 /* For Ada unit, we prefer the linkage name over the name, as
10827 the former contains the exported name, which the user expects
10828 to be able to reference. Ideally, we want the user to be able
10829 to reference this entity using either natural or linkage name,
10830 but we haven't started looking at this enhancement yet. */
73b9be8b 10831 const char *linkage_name = dw2_linkage_name (die, cu);
f55ee35c 10832
2ee7123e
DE
10833 if (linkage_name != NULL)
10834 return linkage_name;
f55ee35c
JK
10835 }
10836
94af9270
KS
10837 /* These are the only languages we know how to qualify names in. */
10838 if (name != NULL
9c37b5ae 10839 && (cu->language == language_cplus
c44af4eb
TT
10840 || cu->language == language_fortran || cu->language == language_d
10841 || cu->language == language_rust))
94af9270
KS
10842 {
10843 if (die_needs_namespace (die, cu))
10844 {
0d5cff50 10845 const char *prefix;
34a68019 10846 const char *canonical_name = NULL;
94af9270 10847
d7e74731
PA
10848 string_file buf;
10849
94af9270 10850 prefix = determine_prefix (die, cu);
94af9270
KS
10851 if (*prefix != '\0')
10852 {
f55ee35c
JK
10853 char *prefixed_name = typename_concat (NULL, prefix, name,
10854 physname, cu);
9a619af0 10855
d7e74731 10856 buf.puts (prefixed_name);
94af9270
KS
10857 xfree (prefixed_name);
10858 }
10859 else
d7e74731 10860 buf.puts (name);
94af9270 10861
98bfdba5
PA
10862 /* Template parameters may be specified in the DIE's DW_AT_name, or
10863 as children with DW_TAG_template_type_param or
10864 DW_TAG_value_type_param. If the latter, add them to the name
10865 here. If the name already has template parameters, then
10866 skip this step; some versions of GCC emit both, and
10867 it is more efficient to use the pre-computed name.
10868
10869 Something to keep in mind about this process: it is very
10870 unlikely, or in some cases downright impossible, to produce
10871 something that will match the mangled name of a function.
10872 If the definition of the function has the same debug info,
10873 we should be able to match up with it anyway. But fallbacks
10874 using the minimal symbol, for instance to find a method
10875 implemented in a stripped copy of libstdc++, will not work.
10876 If we do not have debug info for the definition, we will have to
10877 match them up some other way.
10878
10879 When we do name matching there is a related problem with function
10880 templates; two instantiated function templates are allowed to
10881 differ only by their return types, which we do not add here. */
10882
10883 if (cu->language == language_cplus && strchr (name, '<') == NULL)
10884 {
10885 struct attribute *attr;
10886 struct die_info *child;
10887 int first = 1;
10888
10889 die->building_fullname = 1;
10890
10891 for (child = die->child; child != NULL; child = child->sibling)
10892 {
10893 struct type *type;
12df843f 10894 LONGEST value;
d521ce57 10895 const gdb_byte *bytes;
98bfdba5
PA
10896 struct dwarf2_locexpr_baton *baton;
10897 struct value *v;
10898
10899 if (child->tag != DW_TAG_template_type_param
10900 && child->tag != DW_TAG_template_value_param)
10901 continue;
10902
10903 if (first)
10904 {
d7e74731 10905 buf.puts ("<");
98bfdba5
PA
10906 first = 0;
10907 }
10908 else
d7e74731 10909 buf.puts (", ");
98bfdba5
PA
10910
10911 attr = dwarf2_attr (child, DW_AT_type, cu);
10912 if (attr == NULL)
10913 {
10914 complaint (&symfile_complaints,
10915 _("template parameter missing DW_AT_type"));
d7e74731 10916 buf.puts ("UNKNOWN_TYPE");
98bfdba5
PA
10917 continue;
10918 }
10919 type = die_type (child, cu);
10920
10921 if (child->tag == DW_TAG_template_type_param)
10922 {
d7e74731 10923 c_print_type (type, "", &buf, -1, 0, &type_print_raw_options);
98bfdba5
PA
10924 continue;
10925 }
10926
10927 attr = dwarf2_attr (child, DW_AT_const_value, cu);
10928 if (attr == NULL)
10929 {
10930 complaint (&symfile_complaints,
3e43a32a
MS
10931 _("template parameter missing "
10932 "DW_AT_const_value"));
d7e74731 10933 buf.puts ("UNKNOWN_VALUE");
98bfdba5
PA
10934 continue;
10935 }
10936
10937 dwarf2_const_value_attr (attr, type, name,
10938 &cu->comp_unit_obstack, cu,
10939 &value, &bytes, &baton);
10940
10941 if (TYPE_NOSIGN (type))
10942 /* GDB prints characters as NUMBER 'CHAR'. If that's
10943 changed, this can use value_print instead. */
d7e74731 10944 c_printchar (value, type, &buf);
98bfdba5
PA
10945 else
10946 {
10947 struct value_print_options opts;
10948
10949 if (baton != NULL)
10950 v = dwarf2_evaluate_loc_desc (type, NULL,
10951 baton->data,
10952 baton->size,
10953 baton->per_cu);
10954 else if (bytes != NULL)
10955 {
10956 v = allocate_value (type);
10957 memcpy (value_contents_writeable (v), bytes,
10958 TYPE_LENGTH (type));
10959 }
10960 else
10961 v = value_from_longest (type, value);
10962
3e43a32a
MS
10963 /* Specify decimal so that we do not depend on
10964 the radix. */
98bfdba5
PA
10965 get_formatted_print_options (&opts, 'd');
10966 opts.raw = 1;
d7e74731 10967 value_print (v, &buf, &opts);
98bfdba5
PA
10968 release_value (v);
10969 value_free (v);
10970 }
10971 }
10972
10973 die->building_fullname = 0;
10974
10975 if (!first)
10976 {
10977 /* Close the argument list, with a space if necessary
10978 (nested templates). */
d7e74731
PA
10979 if (!buf.empty () && buf.string ().back () == '>')
10980 buf.puts (" >");
98bfdba5 10981 else
d7e74731 10982 buf.puts (">");
98bfdba5
PA
10983 }
10984 }
10985
9c37b5ae 10986 /* For C++ methods, append formal parameter type
94af9270 10987 information, if PHYSNAME. */
6e70227d 10988
94af9270 10989 if (physname && die->tag == DW_TAG_subprogram
9c37b5ae 10990 && cu->language == language_cplus)
94af9270
KS
10991 {
10992 struct type *type = read_type_die (die, cu);
10993
d7e74731 10994 c_type_print_args (type, &buf, 1, cu->language,
79d43c61 10995 &type_print_raw_options);
94af9270 10996
9c37b5ae 10997 if (cu->language == language_cplus)
94af9270 10998 {
60430eff
DJ
10999 /* Assume that an artificial first parameter is
11000 "this", but do not crash if it is not. RealView
11001 marks unnamed (and thus unused) parameters as
11002 artificial; there is no way to differentiate
11003 the two cases. */
94af9270
KS
11004 if (TYPE_NFIELDS (type) > 0
11005 && TYPE_FIELD_ARTIFICIAL (type, 0)
60430eff 11006 && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
3e43a32a
MS
11007 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
11008 0))))
d7e74731 11009 buf.puts (" const");
94af9270
KS
11010 }
11011 }
11012
d7e74731 11013 const std::string &intermediate_name = buf.string ();
94af9270
KS
11014
11015 if (cu->language == language_cplus)
34a68019 11016 canonical_name
322a8516 11017 = dwarf2_canonicalize_name (intermediate_name.c_str (), cu,
34a68019
TT
11018 &objfile->per_bfd->storage_obstack);
11019
11020 /* If we only computed INTERMEDIATE_NAME, or if
11021 INTERMEDIATE_NAME is already canonical, then we need to
11022 copy it to the appropriate obstack. */
322a8516 11023 if (canonical_name == NULL || canonical_name == intermediate_name.c_str ())
224c3ddb
SM
11024 name = ((const char *)
11025 obstack_copy0 (&objfile->per_bfd->storage_obstack,
322a8516
PA
11026 intermediate_name.c_str (),
11027 intermediate_name.length ()));
34a68019
TT
11028 else
11029 name = canonical_name;
94af9270
KS
11030 }
11031 }
11032
11033 return name;
11034}
11035
0114d602
DJ
11036/* Return the fully qualified name of DIE, based on its DW_AT_name.
11037 If scope qualifiers are appropriate they will be added. The result
34a68019 11038 will be allocated on the storage_obstack, or NULL if the DIE does
94af9270
KS
11039 not have a name. NAME may either be from a previous call to
11040 dwarf2_name or NULL.
11041
9c37b5ae 11042 The output string will be canonicalized (if C++). */
0114d602
DJ
11043
11044static const char *
15d034d0 11045dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
0114d602 11046{
94af9270
KS
11047 return dwarf2_compute_name (name, die, cu, 0);
11048}
0114d602 11049
94af9270
KS
11050/* Construct a physname for the given DIE in CU. NAME may either be
11051 from a previous call to dwarf2_name or NULL. The result will be
11052 allocated on the objfile_objstack or NULL if the DIE does not have a
11053 name.
0114d602 11054
9c37b5ae 11055 The output string will be canonicalized (if C++). */
0114d602 11056
94af9270 11057static const char *
15d034d0 11058dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
94af9270 11059{
518817b3 11060 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
900e11f9 11061 const char *retval, *mangled = NULL, *canon = NULL;
900e11f9
JK
11062 int need_copy = 1;
11063
11064 /* In this case dwarf2_compute_name is just a shortcut not building anything
11065 on its own. */
11066 if (!die_needs_namespace (die, cu))
11067 return dwarf2_compute_name (name, die, cu, 1);
11068
73b9be8b 11069 mangled = dw2_linkage_name (die, cu);
900e11f9 11070
e98c9e7c
TT
11071 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
11072 See https://github.com/rust-lang/rust/issues/32925. */
11073 if (cu->language == language_rust && mangled != NULL
11074 && strchr (mangled, '{') != NULL)
11075 mangled = NULL;
11076
900e11f9
JK
11077 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
11078 has computed. */
791afaa2 11079 gdb::unique_xmalloc_ptr<char> demangled;
7d45c7c3 11080 if (mangled != NULL)
900e11f9 11081 {
900e11f9 11082
59cc4834
JB
11083 if (language_def (cu->language)->la_store_sym_names_in_linkage_form_p)
11084 {
11085 /* Do nothing (do not demangle the symbol name). */
11086 }
11087 else if (cu->language == language_go)
a766d390 11088 {
5e2db402
TT
11089 /* This is a lie, but we already lie to the caller new_symbol.
11090 new_symbol assumes we return the mangled name.
a766d390 11091 This just undoes that lie until things are cleaned up. */
a766d390
DE
11092 }
11093 else
11094 {
0eb876f5
JB
11095 /* Use DMGL_RET_DROP for C++ template functions to suppress
11096 their return type. It is easier for GDB users to search
11097 for such functions as `name(params)' than `long name(params)'.
11098 In such case the minimal symbol names do not match the full
11099 symbol names but for template functions there is never a need
11100 to look up their definition from their declaration so
11101 the only disadvantage remains the minimal symbol variant
11102 `long name(params)' does not have the proper inferior type. */
791afaa2
TT
11103 demangled.reset (gdb_demangle (mangled,
11104 (DMGL_PARAMS | DMGL_ANSI
11105 | DMGL_RET_DROP)));
a766d390 11106 }
900e11f9 11107 if (demangled)
791afaa2 11108 canon = demangled.get ();
900e11f9
JK
11109 else
11110 {
11111 canon = mangled;
11112 need_copy = 0;
11113 }
11114 }
11115
11116 if (canon == NULL || check_physname)
11117 {
11118 const char *physname = dwarf2_compute_name (name, die, cu, 1);
11119
11120 if (canon != NULL && strcmp (physname, canon) != 0)
11121 {
11122 /* It may not mean a bug in GDB. The compiler could also
11123 compute DW_AT_linkage_name incorrectly. But in such case
11124 GDB would need to be bug-to-bug compatible. */
11125
11126 complaint (&symfile_complaints,
11127 _("Computed physname <%s> does not match demangled <%s> "
9d8780f0
SM
11128 "(from linkage <%s>) - DIE at %s [in module %s]"),
11129 physname, canon, mangled, sect_offset_str (die->sect_off),
4262abfb 11130 objfile_name (objfile));
900e11f9
JK
11131
11132 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
11133 is available here - over computed PHYSNAME. It is safer
11134 against both buggy GDB and buggy compilers. */
11135
11136 retval = canon;
11137 }
11138 else
11139 {
11140 retval = physname;
11141 need_copy = 0;
11142 }
11143 }
11144 else
11145 retval = canon;
11146
11147 if (need_copy)
224c3ddb
SM
11148 retval = ((const char *)
11149 obstack_copy0 (&objfile->per_bfd->storage_obstack,
11150 retval, strlen (retval)));
900e11f9 11151
900e11f9 11152 return retval;
0114d602
DJ
11153}
11154
74921315
KS
11155/* Inspect DIE in CU for a namespace alias. If one exists, record
11156 a new symbol for it.
11157
11158 Returns 1 if a namespace alias was recorded, 0 otherwise. */
11159
11160static int
11161read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu)
11162{
11163 struct attribute *attr;
11164
11165 /* If the die does not have a name, this is not a namespace
11166 alias. */
11167 attr = dwarf2_attr (die, DW_AT_name, cu);
11168 if (attr != NULL)
11169 {
11170 int num;
11171 struct die_info *d = die;
11172 struct dwarf2_cu *imported_cu = cu;
11173
11174 /* If the compiler has nested DW_AT_imported_declaration DIEs,
11175 keep inspecting DIEs until we hit the underlying import. */
11176#define MAX_NESTED_IMPORTED_DECLARATIONS 100
11177 for (num = 0; num < MAX_NESTED_IMPORTED_DECLARATIONS; ++num)
11178 {
11179 attr = dwarf2_attr (d, DW_AT_import, cu);
11180 if (attr == NULL)
11181 break;
11182
11183 d = follow_die_ref (d, attr, &imported_cu);
11184 if (d->tag != DW_TAG_imported_declaration)
11185 break;
11186 }
11187
11188 if (num == MAX_NESTED_IMPORTED_DECLARATIONS)
11189 {
11190 complaint (&symfile_complaints,
9d8780f0
SM
11191 _("DIE at %s has too many recursively imported "
11192 "declarations"), sect_offset_str (d->sect_off));
74921315
KS
11193 return 0;
11194 }
11195
11196 if (attr != NULL)
11197 {
11198 struct type *type;
9c541725 11199 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
74921315 11200
9c541725 11201 type = get_die_type_at_offset (sect_off, cu->per_cu);
74921315
KS
11202 if (type != NULL && TYPE_CODE (type) == TYPE_CODE_NAMESPACE)
11203 {
11204 /* This declaration is a global namespace alias. Add
11205 a symbol for it whose type is the aliased namespace. */
11206 new_symbol (die, type, cu);
11207 return 1;
11208 }
11209 }
11210 }
11211
11212 return 0;
11213}
11214
22cee43f
PMR
11215/* Return the using directives repository (global or local?) to use in the
11216 current context for LANGUAGE.
11217
11218 For Ada, imported declarations can materialize renamings, which *may* be
11219 global. However it is impossible (for now?) in DWARF to distinguish
11220 "external" imported declarations and "static" ones. As all imported
11221 declarations seem to be static in all other languages, make them all CU-wide
11222 global only in Ada. */
11223
11224static struct using_direct **
11225using_directives (enum language language)
11226{
11227 if (language == language_ada && context_stack_depth == 0)
11228 return &global_using_directives;
11229 else
11230 return &local_using_directives;
11231}
11232
27aa8d6a
SW
11233/* Read the import statement specified by the given die and record it. */
11234
11235static void
11236read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
11237{
518817b3 11238 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
27aa8d6a 11239 struct attribute *import_attr;
32019081 11240 struct die_info *imported_die, *child_die;
de4affc9 11241 struct dwarf2_cu *imported_cu;
27aa8d6a 11242 const char *imported_name;
794684b6 11243 const char *imported_name_prefix;
13387711
SW
11244 const char *canonical_name;
11245 const char *import_alias;
11246 const char *imported_declaration = NULL;
794684b6 11247 const char *import_prefix;
eb1e02fd 11248 std::vector<const char *> excludes;
13387711 11249
27aa8d6a
SW
11250 import_attr = dwarf2_attr (die, DW_AT_import, cu);
11251 if (import_attr == NULL)
11252 {
11253 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
11254 dwarf_tag_name (die->tag));
11255 return;
11256 }
11257
de4affc9
CC
11258 imported_cu = cu;
11259 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
11260 imported_name = dwarf2_name (imported_die, imported_cu);
27aa8d6a
SW
11261 if (imported_name == NULL)
11262 {
11263 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
11264
11265 The import in the following code:
11266 namespace A
11267 {
11268 typedef int B;
11269 }
11270
11271 int main ()
11272 {
11273 using A::B;
11274 B b;
11275 return b;
11276 }
11277
11278 ...
11279 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
11280 <52> DW_AT_decl_file : 1
11281 <53> DW_AT_decl_line : 6
11282 <54> DW_AT_import : <0x75>
11283 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
11284 <59> DW_AT_name : B
11285 <5b> DW_AT_decl_file : 1
11286 <5c> DW_AT_decl_line : 2
11287 <5d> DW_AT_type : <0x6e>
11288 ...
11289 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
11290 <76> DW_AT_byte_size : 4
11291 <77> DW_AT_encoding : 5 (signed)
11292
11293 imports the wrong die ( 0x75 instead of 0x58 ).
11294 This case will be ignored until the gcc bug is fixed. */
11295 return;
11296 }
11297
82856980
SW
11298 /* Figure out the local name after import. */
11299 import_alias = dwarf2_name (die, cu);
27aa8d6a 11300
794684b6
SW
11301 /* Figure out where the statement is being imported to. */
11302 import_prefix = determine_prefix (die, cu);
11303
11304 /* Figure out what the scope of the imported die is and prepend it
11305 to the name of the imported die. */
de4affc9 11306 imported_name_prefix = determine_prefix (imported_die, imported_cu);
794684b6 11307
f55ee35c
JK
11308 if (imported_die->tag != DW_TAG_namespace
11309 && imported_die->tag != DW_TAG_module)
794684b6 11310 {
13387711
SW
11311 imported_declaration = imported_name;
11312 canonical_name = imported_name_prefix;
794684b6 11313 }
13387711 11314 else if (strlen (imported_name_prefix) > 0)
12aaed36 11315 canonical_name = obconcat (&objfile->objfile_obstack,
45280282
IB
11316 imported_name_prefix,
11317 (cu->language == language_d ? "." : "::"),
11318 imported_name, (char *) NULL);
13387711
SW
11319 else
11320 canonical_name = imported_name;
794684b6 11321
32019081
JK
11322 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
11323 for (child_die = die->child; child_die && child_die->tag;
11324 child_die = sibling_die (child_die))
11325 {
11326 /* DWARF-4: A Fortran use statement with a “rename list” may be
11327 represented by an imported module entry with an import attribute
11328 referring to the module and owned entries corresponding to those
11329 entities that are renamed as part of being imported. */
11330
11331 if (child_die->tag != DW_TAG_imported_declaration)
11332 {
11333 complaint (&symfile_complaints,
11334 _("child DW_TAG_imported_declaration expected "
9d8780f0
SM
11335 "- DIE at %s [in module %s]"),
11336 sect_offset_str (child_die->sect_off),
11337 objfile_name (objfile));
32019081
JK
11338 continue;
11339 }
11340
11341 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
11342 if (import_attr == NULL)
11343 {
11344 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
11345 dwarf_tag_name (child_die->tag));
11346 continue;
11347 }
11348
11349 imported_cu = cu;
11350 imported_die = follow_die_ref_or_sig (child_die, import_attr,
11351 &imported_cu);
11352 imported_name = dwarf2_name (imported_die, imported_cu);
11353 if (imported_name == NULL)
11354 {
11355 complaint (&symfile_complaints,
11356 _("child DW_TAG_imported_declaration has unknown "
9d8780f0
SM
11357 "imported name - DIE at %s [in module %s]"),
11358 sect_offset_str (child_die->sect_off),
11359 objfile_name (objfile));
32019081
JK
11360 continue;
11361 }
11362
eb1e02fd 11363 excludes.push_back (imported_name);
32019081
JK
11364
11365 process_die (child_die, cu);
11366 }
11367
22cee43f
PMR
11368 add_using_directive (using_directives (cu->language),
11369 import_prefix,
11370 canonical_name,
11371 import_alias,
11372 imported_declaration,
11373 excludes,
11374 0,
11375 &objfile->objfile_obstack);
27aa8d6a
SW
11376}
11377
5230b05a
WT
11378/* ICC<14 does not output the required DW_AT_declaration on incomplete
11379 types, but gives them a size of zero. Starting with version 14,
11380 ICC is compatible with GCC. */
11381
11382static int
11383producer_is_icc_lt_14 (struct dwarf2_cu *cu)
11384{
11385 if (!cu->checked_producer)
11386 check_producer (cu);
11387
11388 return cu->producer_is_icc_lt_14;
11389}
11390
1b80a9fa
JK
11391/* Check for possibly missing DW_AT_comp_dir with relative .debug_line
11392 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
11393 this, it was first present in GCC release 4.3.0. */
11394
11395static int
11396producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
11397{
11398 if (!cu->checked_producer)
11399 check_producer (cu);
11400
11401 return cu->producer_is_gcc_lt_4_3;
11402}
11403
d721ba37
PA
11404static file_and_directory
11405find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu)
9291a0cd 11406{
d721ba37
PA
11407 file_and_directory res;
11408
9291a0cd
TT
11409 /* Find the filename. Do not use dwarf2_name here, since the filename
11410 is not a source language identifier. */
d721ba37
PA
11411 res.name = dwarf2_string_attr (die, DW_AT_name, cu);
11412 res.comp_dir = dwarf2_string_attr (die, DW_AT_comp_dir, cu);
9291a0cd 11413
d721ba37
PA
11414 if (res.comp_dir == NULL
11415 && producer_is_gcc_lt_4_3 (cu) && res.name != NULL
11416 && IS_ABSOLUTE_PATH (res.name))
9291a0cd 11417 {
d721ba37
PA
11418 res.comp_dir_storage = ldirname (res.name);
11419 if (!res.comp_dir_storage.empty ())
11420 res.comp_dir = res.comp_dir_storage.c_str ();
9291a0cd 11421 }
d721ba37 11422 if (res.comp_dir != NULL)
9291a0cd
TT
11423 {
11424 /* Irix 6.2 native cc prepends <machine>.: to the compilation
11425 directory, get rid of it. */
d721ba37 11426 const char *cp = strchr (res.comp_dir, ':');
9291a0cd 11427
d721ba37
PA
11428 if (cp && cp != res.comp_dir && cp[-1] == '.' && cp[1] == '/')
11429 res.comp_dir = cp + 1;
9291a0cd
TT
11430 }
11431
d721ba37
PA
11432 if (res.name == NULL)
11433 res.name = "<unknown>";
11434
11435 return res;
9291a0cd
TT
11436}
11437
f4dc4d17
DE
11438/* Handle DW_AT_stmt_list for a compilation unit.
11439 DIE is the DW_TAG_compile_unit die for CU.
c3b7b696
YQ
11440 COMP_DIR is the compilation directory. LOWPC is passed to
11441 dwarf_decode_lines. See dwarf_decode_lines comments about it. */
2ab95328
TT
11442
11443static void
11444handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
c3b7b696 11445 const char *comp_dir, CORE_ADDR lowpc) /* ARI: editCase function */
2ab95328 11446{
518817b3
SM
11447 struct dwarf2_per_objfile *dwarf2_per_objfile
11448 = cu->per_cu->dwarf2_per_objfile;
527f3840 11449 struct objfile *objfile = dwarf2_per_objfile->objfile;
2ab95328 11450 struct attribute *attr;
527f3840
JK
11451 struct line_header line_header_local;
11452 hashval_t line_header_local_hash;
527f3840
JK
11453 void **slot;
11454 int decode_mapping;
2ab95328 11455
f4dc4d17
DE
11456 gdb_assert (! cu->per_cu->is_debug_types);
11457
2ab95328 11458 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
527f3840
JK
11459 if (attr == NULL)
11460 return;
11461
9c541725 11462 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
527f3840
JK
11463
11464 /* The line header hash table is only created if needed (it exists to
11465 prevent redundant reading of the line table for partial_units).
11466 If we're given a partial_unit, we'll need it. If we're given a
11467 compile_unit, then use the line header hash table if it's already
11468 created, but don't create one just yet. */
11469
11470 if (dwarf2_per_objfile->line_header_hash == NULL
11471 && die->tag == DW_TAG_partial_unit)
2ab95328 11472 {
527f3840
JK
11473 dwarf2_per_objfile->line_header_hash
11474 = htab_create_alloc_ex (127, line_header_hash_voidp,
11475 line_header_eq_voidp,
11476 free_line_header_voidp,
11477 &objfile->objfile_obstack,
11478 hashtab_obstack_allocate,
11479 dummy_obstack_deallocate);
11480 }
2ab95328 11481
9c541725 11482 line_header_local.sect_off = line_offset;
527f3840
JK
11483 line_header_local.offset_in_dwz = cu->per_cu->is_dwz;
11484 line_header_local_hash = line_header_hash (&line_header_local);
11485 if (dwarf2_per_objfile->line_header_hash != NULL)
11486 {
11487 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash,
11488 &line_header_local,
11489 line_header_local_hash, NO_INSERT);
11490
11491 /* For DW_TAG_compile_unit we need info like symtab::linetable which
11492 is not present in *SLOT (since if there is something in *SLOT then
11493 it will be for a partial_unit). */
11494 if (die->tag == DW_TAG_partial_unit && slot != NULL)
dee91e82 11495 {
527f3840 11496 gdb_assert (*slot != NULL);
9a3c8263 11497 cu->line_header = (struct line_header *) *slot;
527f3840 11498 return;
dee91e82 11499 }
2ab95328 11500 }
527f3840
JK
11501
11502 /* dwarf_decode_line_header does not yet provide sufficient information.
11503 We always have to call also dwarf_decode_lines for it. */
fff8551c
PA
11504 line_header_up lh = dwarf_decode_line_header (line_offset, cu);
11505 if (lh == NULL)
527f3840 11506 return;
4c8aa72d
PA
11507
11508 cu->line_header = lh.release ();
11509 cu->line_header_die_owner = die;
527f3840
JK
11510
11511 if (dwarf2_per_objfile->line_header_hash == NULL)
11512 slot = NULL;
11513 else
11514 {
11515 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash,
11516 &line_header_local,
11517 line_header_local_hash, INSERT);
11518 gdb_assert (slot != NULL);
11519 }
11520 if (slot != NULL && *slot == NULL)
11521 {
11522 /* This newly decoded line number information unit will be owned
11523 by line_header_hash hash table. */
11524 *slot = cu->line_header;
4c8aa72d 11525 cu->line_header_die_owner = NULL;
527f3840
JK
11526 }
11527 else
11528 {
11529 /* We cannot free any current entry in (*slot) as that struct line_header
11530 may be already used by multiple CUs. Create only temporary decoded
11531 line_header for this CU - it may happen at most once for each line
11532 number information unit. And if we're not using line_header_hash
11533 then this is what we want as well. */
11534 gdb_assert (die->tag != DW_TAG_partial_unit);
527f3840
JK
11535 }
11536 decode_mapping = (die->tag != DW_TAG_partial_unit);
11537 dwarf_decode_lines (cu->line_header, comp_dir, cu, NULL, lowpc,
11538 decode_mapping);
fff8551c 11539
2ab95328
TT
11540}
11541
95554aad 11542/* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
ae2de4f8 11543
c906108c 11544static void
e7c27a73 11545read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 11546{
518817b3
SM
11547 struct dwarf2_per_objfile *dwarf2_per_objfile
11548 = cu->per_cu->dwarf2_per_objfile;
dee91e82 11549 struct objfile *objfile = dwarf2_per_objfile->objfile;
3e29f34a 11550 struct gdbarch *gdbarch = get_objfile_arch (objfile);
2acceee2 11551 CORE_ADDR lowpc = ((CORE_ADDR) -1);
c906108c
SS
11552 CORE_ADDR highpc = ((CORE_ADDR) 0);
11553 struct attribute *attr;
c906108c 11554 struct die_info *child_die;
e142c38c 11555 CORE_ADDR baseaddr;
6e70227d 11556
e142c38c 11557 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 11558
fae299cd 11559 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
c906108c
SS
11560
11561 /* If we didn't find a lowpc, set it to highpc to avoid complaints
11562 from finish_block. */
2acceee2 11563 if (lowpc == ((CORE_ADDR) -1))
c906108c 11564 lowpc = highpc;
3e29f34a 11565 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
c906108c 11566
d721ba37 11567 file_and_directory fnd = find_file_and_directory (die, cu);
e1024ff1 11568
95554aad 11569 prepare_one_comp_unit (cu, die, cu->language);
303b6f5d 11570
f4b8a18d
KW
11571 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
11572 standardised yet. As a workaround for the language detection we fall
11573 back to the DW_AT_producer string. */
11574 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
11575 cu->language = language_opencl;
11576
3019eac3
DE
11577 /* Similar hack for Go. */
11578 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
11579 set_cu_language (DW_LANG_Go, cu);
11580
d721ba37 11581 dwarf2_start_symtab (cu, fnd.name, fnd.comp_dir, lowpc);
3019eac3
DE
11582
11583 /* Decode line number information if present. We do this before
11584 processing child DIEs, so that the line header table is available
11585 for DW_AT_decl_file. */
d721ba37 11586 handle_DW_AT_stmt_list (die, cu, fnd.comp_dir, lowpc);
3019eac3
DE
11587
11588 /* Process all dies in compilation unit. */
11589 if (die->child != NULL)
11590 {
11591 child_die = die->child;
11592 while (child_die && child_die->tag)
11593 {
11594 process_die (child_die, cu);
11595 child_die = sibling_die (child_die);
11596 }
11597 }
11598
11599 /* Decode macro information, if present. Dwarf 2 macro information
11600 refers to information in the line number info statement program
11601 header, so we can only read it if we've read the header
11602 successfully. */
0af92d60
JK
11603 attr = dwarf2_attr (die, DW_AT_macros, cu);
11604 if (attr == NULL)
11605 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
3019eac3
DE
11606 if (attr && cu->line_header)
11607 {
11608 if (dwarf2_attr (die, DW_AT_macro_info, cu))
11609 complaint (&symfile_complaints,
0af92d60 11610 _("CU refers to both DW_AT_macros and DW_AT_macro_info"));
3019eac3 11611
43f3e411 11612 dwarf_decode_macros (cu, DW_UNSND (attr), 1);
3019eac3
DE
11613 }
11614 else
11615 {
11616 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
11617 if (attr && cu->line_header)
11618 {
11619 unsigned int macro_offset = DW_UNSND (attr);
11620
43f3e411 11621 dwarf_decode_macros (cu, macro_offset, 0);
3019eac3
DE
11622 }
11623 }
3019eac3
DE
11624}
11625
f4dc4d17
DE
11626/* TU version of handle_DW_AT_stmt_list for read_type_unit_scope.
11627 Create the set of symtabs used by this TU, or if this TU is sharing
11628 symtabs with another TU and the symtabs have already been created
11629 then restore those symtabs in the line header.
11630 We don't need the pc/line-number mapping for type units. */
3019eac3
DE
11631
11632static void
f4dc4d17 11633setup_type_unit_groups (struct die_info *die, struct dwarf2_cu *cu)
3019eac3 11634{
f4dc4d17
DE
11635 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
11636 struct type_unit_group *tu_group;
11637 int first_time;
3019eac3 11638 struct attribute *attr;
9c541725 11639 unsigned int i;
0186c6a7 11640 struct signatured_type *sig_type;
3019eac3 11641
f4dc4d17 11642 gdb_assert (per_cu->is_debug_types);
0186c6a7 11643 sig_type = (struct signatured_type *) per_cu;
3019eac3 11644
f4dc4d17 11645 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
3019eac3 11646
f4dc4d17 11647 /* If we're using .gdb_index (includes -readnow) then
74e04d1c 11648 per_cu->type_unit_group may not have been set up yet. */
0186c6a7
DE
11649 if (sig_type->type_unit_group == NULL)
11650 sig_type->type_unit_group = get_type_unit_group (cu, attr);
11651 tu_group = sig_type->type_unit_group;
f4dc4d17
DE
11652
11653 /* If we've already processed this stmt_list there's no real need to
11654 do it again, we could fake it and just recreate the part we need
11655 (file name,index -> symtab mapping). If data shows this optimization
11656 is useful we can do it then. */
43f3e411 11657 first_time = tu_group->compunit_symtab == NULL;
f4dc4d17
DE
11658
11659 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
11660 debug info. */
fff8551c 11661 line_header_up lh;
f4dc4d17 11662 if (attr != NULL)
3019eac3 11663 {
9c541725 11664 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
f4dc4d17
DE
11665 lh = dwarf_decode_line_header (line_offset, cu);
11666 }
11667 if (lh == NULL)
11668 {
11669 if (first_time)
11670 dwarf2_start_symtab (cu, "", NULL, 0);
11671 else
11672 {
11673 gdb_assert (tu_group->symtabs == NULL);
0ab9ce85 11674 restart_symtab (tu_group->compunit_symtab, "", 0);
f4dc4d17 11675 }
f4dc4d17 11676 return;
3019eac3
DE
11677 }
11678
4c8aa72d
PA
11679 cu->line_header = lh.release ();
11680 cu->line_header_die_owner = die;
3019eac3 11681
f4dc4d17
DE
11682 if (first_time)
11683 {
43f3e411 11684 struct compunit_symtab *cust = dwarf2_start_symtab (cu, "", NULL, 0);
3019eac3 11685
1fd60fc0
DE
11686 /* Note: We don't assign tu_group->compunit_symtab yet because we're
11687 still initializing it, and our caller (a few levels up)
11688 process_full_type_unit still needs to know if this is the first
11689 time. */
11690
4c8aa72d
PA
11691 tu_group->num_symtabs = cu->line_header->file_names.size ();
11692 tu_group->symtabs = XNEWVEC (struct symtab *,
11693 cu->line_header->file_names.size ());
3019eac3 11694
4c8aa72d 11695 for (i = 0; i < cu->line_header->file_names.size (); ++i)
f4dc4d17 11696 {
4c8aa72d 11697 file_entry &fe = cu->line_header->file_names[i];
3019eac3 11698
4c8aa72d 11699 dwarf2_start_subfile (fe.name, fe.include_dir (cu->line_header));
3019eac3 11700
f4dc4d17
DE
11701 if (current_subfile->symtab == NULL)
11702 {
4c8aa72d
PA
11703 /* NOTE: start_subfile will recognize when it's been
11704 passed a file it has already seen. So we can't
11705 assume there's a simple mapping from
11706 cu->line_header->file_names to subfiles, plus
11707 cu->line_header->file_names may contain dups. */
43f3e411
DE
11708 current_subfile->symtab
11709 = allocate_symtab (cust, current_subfile->name);
f4dc4d17
DE
11710 }
11711
8c43009f
PA
11712 fe.symtab = current_subfile->symtab;
11713 tu_group->symtabs[i] = fe.symtab;
f4dc4d17
DE
11714 }
11715 }
11716 else
3019eac3 11717 {
0ab9ce85 11718 restart_symtab (tu_group->compunit_symtab, "", 0);
f4dc4d17 11719
4c8aa72d 11720 for (i = 0; i < cu->line_header->file_names.size (); ++i)
f4dc4d17 11721 {
4c8aa72d 11722 file_entry &fe = cu->line_header->file_names[i];
f4dc4d17 11723
4c8aa72d 11724 fe.symtab = tu_group->symtabs[i];
f4dc4d17 11725 }
3019eac3
DE
11726 }
11727
f4dc4d17
DE
11728 /* The main symtab is allocated last. Type units don't have DW_AT_name
11729 so they don't have a "real" (so to speak) symtab anyway.
11730 There is later code that will assign the main symtab to all symbols
11731 that don't have one. We need to handle the case of a symbol with a
11732 missing symtab (DW_AT_decl_file) anyway. */
11733}
3019eac3 11734
f4dc4d17
DE
11735/* Process DW_TAG_type_unit.
11736 For TUs we want to skip the first top level sibling if it's not the
11737 actual type being defined by this TU. In this case the first top
11738 level sibling is there to provide context only. */
3019eac3 11739
f4dc4d17
DE
11740static void
11741read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
11742{
11743 struct die_info *child_die;
3019eac3 11744
f4dc4d17
DE
11745 prepare_one_comp_unit (cu, die, language_minimal);
11746
11747 /* Initialize (or reinitialize) the machinery for building symtabs.
11748 We do this before processing child DIEs, so that the line header table
11749 is available for DW_AT_decl_file. */
11750 setup_type_unit_groups (die, cu);
11751
11752 if (die->child != NULL)
11753 {
11754 child_die = die->child;
11755 while (child_die && child_die->tag)
11756 {
11757 process_die (child_die, cu);
11758 child_die = sibling_die (child_die);
11759 }
11760 }
3019eac3
DE
11761}
11762\f
80626a55
DE
11763/* DWO/DWP files.
11764
11765 http://gcc.gnu.org/wiki/DebugFission
11766 http://gcc.gnu.org/wiki/DebugFissionDWP
11767
11768 To simplify handling of both DWO files ("object" files with the DWARF info)
11769 and DWP files (a file with the DWOs packaged up into one file), we treat
11770 DWP files as having a collection of virtual DWO files. */
3019eac3
DE
11771
11772static hashval_t
11773hash_dwo_file (const void *item)
11774{
9a3c8263 11775 const struct dwo_file *dwo_file = (const struct dwo_file *) item;
a2ce51a0 11776 hashval_t hash;
3019eac3 11777
a2ce51a0
DE
11778 hash = htab_hash_string (dwo_file->dwo_name);
11779 if (dwo_file->comp_dir != NULL)
11780 hash += htab_hash_string (dwo_file->comp_dir);
11781 return hash;
3019eac3
DE
11782}
11783
11784static int
11785eq_dwo_file (const void *item_lhs, const void *item_rhs)
11786{
9a3c8263
SM
11787 const struct dwo_file *lhs = (const struct dwo_file *) item_lhs;
11788 const struct dwo_file *rhs = (const struct dwo_file *) item_rhs;
3019eac3 11789
a2ce51a0
DE
11790 if (strcmp (lhs->dwo_name, rhs->dwo_name) != 0)
11791 return 0;
11792 if (lhs->comp_dir == NULL || rhs->comp_dir == NULL)
11793 return lhs->comp_dir == rhs->comp_dir;
11794 return strcmp (lhs->comp_dir, rhs->comp_dir) == 0;
3019eac3
DE
11795}
11796
11797/* Allocate a hash table for DWO files. */
11798
11799static htab_t
ed2dc618 11800allocate_dwo_file_hash_table (struct objfile *objfile)
3019eac3 11801{
3019eac3
DE
11802 return htab_create_alloc_ex (41,
11803 hash_dwo_file,
11804 eq_dwo_file,
11805 NULL,
11806 &objfile->objfile_obstack,
11807 hashtab_obstack_allocate,
11808 dummy_obstack_deallocate);
11809}
11810
80626a55
DE
11811/* Lookup DWO file DWO_NAME. */
11812
11813static void **
ed2dc618
SM
11814lookup_dwo_file_slot (struct dwarf2_per_objfile *dwarf2_per_objfile,
11815 const char *dwo_name,
11816 const char *comp_dir)
80626a55
DE
11817{
11818 struct dwo_file find_entry;
11819 void **slot;
11820
11821 if (dwarf2_per_objfile->dwo_files == NULL)
ed2dc618
SM
11822 dwarf2_per_objfile->dwo_files
11823 = allocate_dwo_file_hash_table (dwarf2_per_objfile->objfile);
80626a55
DE
11824
11825 memset (&find_entry, 0, sizeof (find_entry));
0ac5b59e
DE
11826 find_entry.dwo_name = dwo_name;
11827 find_entry.comp_dir = comp_dir;
80626a55
DE
11828 slot = htab_find_slot (dwarf2_per_objfile->dwo_files, &find_entry, INSERT);
11829
11830 return slot;
11831}
11832
3019eac3
DE
11833static hashval_t
11834hash_dwo_unit (const void *item)
11835{
9a3c8263 11836 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
3019eac3
DE
11837
11838 /* This drops the top 32 bits of the id, but is ok for a hash. */
11839 return dwo_unit->signature;
11840}
11841
11842static int
11843eq_dwo_unit (const void *item_lhs, const void *item_rhs)
11844{
9a3c8263
SM
11845 const struct dwo_unit *lhs = (const struct dwo_unit *) item_lhs;
11846 const struct dwo_unit *rhs = (const struct dwo_unit *) item_rhs;
3019eac3
DE
11847
11848 /* The signature is assumed to be unique within the DWO file.
11849 So while object file CU dwo_id's always have the value zero,
11850 that's OK, assuming each object file DWO file has only one CU,
11851 and that's the rule for now. */
11852 return lhs->signature == rhs->signature;
11853}
11854
11855/* Allocate a hash table for DWO CUs,TUs.
11856 There is one of these tables for each of CUs,TUs for each DWO file. */
11857
11858static htab_t
11859allocate_dwo_unit_table (struct objfile *objfile)
11860{
11861 /* Start out with a pretty small number.
11862 Generally DWO files contain only one CU and maybe some TUs. */
11863 return htab_create_alloc_ex (3,
11864 hash_dwo_unit,
11865 eq_dwo_unit,
11866 NULL,
11867 &objfile->objfile_obstack,
11868 hashtab_obstack_allocate,
11869 dummy_obstack_deallocate);
11870}
11871
80626a55 11872/* Structure used to pass data to create_dwo_debug_info_hash_table_reader. */
3019eac3 11873
19c3d4c9 11874struct create_dwo_cu_data
3019eac3
DE
11875{
11876 struct dwo_file *dwo_file;
19c3d4c9 11877 struct dwo_unit dwo_unit;
3019eac3
DE
11878};
11879
19c3d4c9 11880/* die_reader_func for create_dwo_cu. */
3019eac3
DE
11881
11882static void
19c3d4c9
DE
11883create_dwo_cu_reader (const struct die_reader_specs *reader,
11884 const gdb_byte *info_ptr,
11885 struct die_info *comp_unit_die,
11886 int has_children,
11887 void *datap)
3019eac3
DE
11888{
11889 struct dwarf2_cu *cu = reader->cu;
9c541725 11890 sect_offset sect_off = cu->per_cu->sect_off;
8a0459fd 11891 struct dwarf2_section_info *section = cu->per_cu->section;
9a3c8263 11892 struct create_dwo_cu_data *data = (struct create_dwo_cu_data *) datap;
3019eac3 11893 struct dwo_file *dwo_file = data->dwo_file;
19c3d4c9 11894 struct dwo_unit *dwo_unit = &data->dwo_unit;
3019eac3 11895 struct attribute *attr;
3019eac3
DE
11896
11897 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
11898 if (attr == NULL)
11899 {
19c3d4c9 11900 complaint (&symfile_complaints,
9d8780f0 11901 _("Dwarf Error: debug entry at offset %s is missing"
19c3d4c9 11902 " its dwo_id [in module %s]"),
9d8780f0 11903 sect_offset_str (sect_off), dwo_file->dwo_name);
3019eac3
DE
11904 return;
11905 }
11906
3019eac3
DE
11907 dwo_unit->dwo_file = dwo_file;
11908 dwo_unit->signature = DW_UNSND (attr);
8a0459fd 11909 dwo_unit->section = section;
9c541725 11910 dwo_unit->sect_off = sect_off;
3019eac3
DE
11911 dwo_unit->length = cu->per_cu->length;
11912
b4f54984 11913 if (dwarf_read_debug)
9d8780f0
SM
11914 fprintf_unfiltered (gdb_stdlog, " offset %s, dwo_id %s\n",
11915 sect_offset_str (sect_off),
9c541725 11916 hex_string (dwo_unit->signature));
3019eac3
DE
11917}
11918
33c5cd75 11919/* Create the dwo_units for the CUs in a DWO_FILE.
19c3d4c9 11920 Note: This function processes DWO files only, not DWP files. */
3019eac3 11921
33c5cd75 11922static void
ed2dc618
SM
11923create_cus_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
11924 struct dwo_file &dwo_file, dwarf2_section_info &section,
33c5cd75 11925 htab_t &cus_htab)
3019eac3
DE
11926{
11927 struct objfile *objfile = dwarf2_per_objfile->objfile;
d521ce57 11928 const gdb_byte *info_ptr, *end_ptr;
3019eac3 11929
33c5cd75
DB
11930 dwarf2_read_section (objfile, &section);
11931 info_ptr = section.buffer;
3019eac3
DE
11932
11933 if (info_ptr == NULL)
33c5cd75 11934 return;
3019eac3 11935
b4f54984 11936 if (dwarf_read_debug)
19c3d4c9
DE
11937 {
11938 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
33c5cd75
DB
11939 get_section_name (&section),
11940 get_section_file_name (&section));
19c3d4c9 11941 }
3019eac3 11942
33c5cd75 11943 end_ptr = info_ptr + section.size;
3019eac3
DE
11944 while (info_ptr < end_ptr)
11945 {
11946 struct dwarf2_per_cu_data per_cu;
33c5cd75
DB
11947 struct create_dwo_cu_data create_dwo_cu_data;
11948 struct dwo_unit *dwo_unit;
11949 void **slot;
11950 sect_offset sect_off = (sect_offset) (info_ptr - section.buffer);
3019eac3 11951
19c3d4c9
DE
11952 memset (&create_dwo_cu_data.dwo_unit, 0,
11953 sizeof (create_dwo_cu_data.dwo_unit));
3019eac3 11954 memset (&per_cu, 0, sizeof (per_cu));
e3b94546 11955 per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
3019eac3 11956 per_cu.is_debug_types = 0;
33c5cd75
DB
11957 per_cu.sect_off = sect_offset (info_ptr - section.buffer);
11958 per_cu.section = &section;
c5ed0576 11959 create_dwo_cu_data.dwo_file = &dwo_file;
33c5cd75
DB
11960
11961 init_cutu_and_read_dies_no_follow (
11962 &per_cu, &dwo_file, create_dwo_cu_reader, &create_dwo_cu_data);
11963 info_ptr += per_cu.length;
11964
11965 // If the unit could not be parsed, skip it.
11966 if (create_dwo_cu_data.dwo_unit.dwo_file == NULL)
11967 continue;
3019eac3 11968
33c5cd75
DB
11969 if (cus_htab == NULL)
11970 cus_htab = allocate_dwo_unit_table (objfile);
19c3d4c9 11971
33c5cd75
DB
11972 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
11973 *dwo_unit = create_dwo_cu_data.dwo_unit;
11974 slot = htab_find_slot (cus_htab, dwo_unit, INSERT);
11975 gdb_assert (slot != NULL);
11976 if (*slot != NULL)
19c3d4c9 11977 {
33c5cd75
DB
11978 const struct dwo_unit *dup_cu = (const struct dwo_unit *)*slot;
11979 sect_offset dup_sect_off = dup_cu->sect_off;
19c3d4c9 11980
33c5cd75 11981 complaint (&symfile_complaints,
9d8780f0
SM
11982 _("debug cu entry at offset %s is duplicate to"
11983 " the entry at offset %s, signature %s"),
11984 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
33c5cd75 11985 hex_string (dwo_unit->signature));
19c3d4c9 11986 }
33c5cd75 11987 *slot = (void *)dwo_unit;
3019eac3 11988 }
3019eac3
DE
11989}
11990
80626a55
DE
11991/* DWP file .debug_{cu,tu}_index section format:
11992 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
11993
d2415c6c
DE
11994 DWP Version 1:
11995
80626a55
DE
11996 Both index sections have the same format, and serve to map a 64-bit
11997 signature to a set of section numbers. Each section begins with a header,
11998 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
11999 indexes, and a pool of 32-bit section numbers. The index sections will be
12000 aligned at 8-byte boundaries in the file.
12001
d2415c6c
DE
12002 The index section header consists of:
12003
12004 V, 32 bit version number
12005 -, 32 bits unused
12006 N, 32 bit number of compilation units or type units in the index
12007 M, 32 bit number of slots in the hash table
80626a55 12008
d2415c6c 12009 Numbers are recorded using the byte order of the application binary.
80626a55 12010
d2415c6c
DE
12011 The hash table begins at offset 16 in the section, and consists of an array
12012 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
12013 order of the application binary). Unused slots in the hash table are 0.
12014 (We rely on the extreme unlikeliness of a signature being exactly 0.)
80626a55 12015
d2415c6c
DE
12016 The parallel table begins immediately after the hash table
12017 (at offset 16 + 8 * M from the beginning of the section), and consists of an
12018 array of 32-bit indexes (using the byte order of the application binary),
12019 corresponding 1-1 with slots in the hash table. Each entry in the parallel
12020 table contains a 32-bit index into the pool of section numbers. For unused
12021 hash table slots, the corresponding entry in the parallel table will be 0.
80626a55 12022
73869dc2
DE
12023 The pool of section numbers begins immediately following the hash table
12024 (at offset 16 + 12 * M from the beginning of the section). The pool of
12025 section numbers consists of an array of 32-bit words (using the byte order
12026 of the application binary). Each item in the array is indexed starting
12027 from 0. The hash table entry provides the index of the first section
12028 number in the set. Additional section numbers in the set follow, and the
12029 set is terminated by a 0 entry (section number 0 is not used in ELF).
12030
12031 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
12032 section must be the first entry in the set, and the .debug_abbrev.dwo must
12033 be the second entry. Other members of the set may follow in any order.
12034
12035 ---
12036
12037 DWP Version 2:
12038
12039 DWP Version 2 combines all the .debug_info, etc. sections into one,
12040 and the entries in the index tables are now offsets into these sections.
12041 CU offsets begin at 0. TU offsets begin at the size of the .debug_info
12042 section.
12043
12044 Index Section Contents:
12045 Header
12046 Hash Table of Signatures dwp_hash_table.hash_table
12047 Parallel Table of Indices dwp_hash_table.unit_table
12048 Table of Section Offsets dwp_hash_table.v2.{section_ids,offsets}
12049 Table of Section Sizes dwp_hash_table.v2.sizes
12050
12051 The index section header consists of:
12052
12053 V, 32 bit version number
12054 L, 32 bit number of columns in the table of section offsets
12055 N, 32 bit number of compilation units or type units in the index
12056 M, 32 bit number of slots in the hash table
12057
12058 Numbers are recorded using the byte order of the application binary.
12059
12060 The hash table has the same format as version 1.
12061 The parallel table of indices has the same format as version 1,
12062 except that the entries are origin-1 indices into the table of sections
12063 offsets and the table of section sizes.
12064
12065 The table of offsets begins immediately following the parallel table
12066 (at offset 16 + 12 * M from the beginning of the section). The table is
12067 a two-dimensional array of 32-bit words (using the byte order of the
12068 application binary), with L columns and N+1 rows, in row-major order.
12069 Each row in the array is indexed starting from 0. The first row provides
12070 a key to the remaining rows: each column in this row provides an identifier
12071 for a debug section, and the offsets in the same column of subsequent rows
12072 refer to that section. The section identifiers are:
12073
12074 DW_SECT_INFO 1 .debug_info.dwo
12075 DW_SECT_TYPES 2 .debug_types.dwo
12076 DW_SECT_ABBREV 3 .debug_abbrev.dwo
12077 DW_SECT_LINE 4 .debug_line.dwo
12078 DW_SECT_LOC 5 .debug_loc.dwo
12079 DW_SECT_STR_OFFSETS 6 .debug_str_offsets.dwo
12080 DW_SECT_MACINFO 7 .debug_macinfo.dwo
12081 DW_SECT_MACRO 8 .debug_macro.dwo
12082
12083 The offsets provided by the CU and TU index sections are the base offsets
12084 for the contributions made by each CU or TU to the corresponding section
12085 in the package file. Each CU and TU header contains an abbrev_offset
12086 field, used to find the abbreviations table for that CU or TU within the
12087 contribution to the .debug_abbrev.dwo section for that CU or TU, and should
12088 be interpreted as relative to the base offset given in the index section.
12089 Likewise, offsets into .debug_line.dwo from DW_AT_stmt_list attributes
12090 should be interpreted as relative to the base offset for .debug_line.dwo,
12091 and offsets into other debug sections obtained from DWARF attributes should
12092 also be interpreted as relative to the corresponding base offset.
12093
12094 The table of sizes begins immediately following the table of offsets.
12095 Like the table of offsets, it is a two-dimensional array of 32-bit words,
12096 with L columns and N rows, in row-major order. Each row in the array is
12097 indexed starting from 1 (row 0 is shared by the two tables).
12098
12099 ---
12100
12101 Hash table lookup is handled the same in version 1 and 2:
12102
12103 We assume that N and M will not exceed 2^32 - 1.
12104 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
12105
d2415c6c
DE
12106 Given a 64-bit compilation unit signature or a type signature S, an entry
12107 in the hash table is located as follows:
80626a55 12108
d2415c6c
DE
12109 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
12110 the low-order k bits all set to 1.
80626a55 12111
d2415c6c 12112 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
80626a55 12113
d2415c6c
DE
12114 3) If the hash table entry at index H matches the signature, use that
12115 entry. If the hash table entry at index H is unused (all zeroes),
12116 terminate the search: the signature is not present in the table.
80626a55 12117
d2415c6c 12118 4) Let H = (H + H') modulo M. Repeat at Step 3.
80626a55 12119
d2415c6c 12120 Because M > N and H' and M are relatively prime, the search is guaranteed
73869dc2 12121 to stop at an unused slot or find the match. */
80626a55
DE
12122
12123/* Create a hash table to map DWO IDs to their CU/TU entry in
12124 .debug_{info,types}.dwo in DWP_FILE.
12125 Returns NULL if there isn't one.
12126 Note: This function processes DWP files only, not DWO files. */
12127
12128static struct dwp_hash_table *
ed2dc618
SM
12129create_dwp_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
12130 struct dwp_file *dwp_file, int is_debug_types)
80626a55
DE
12131{
12132 struct objfile *objfile = dwarf2_per_objfile->objfile;
12133 bfd *dbfd = dwp_file->dbfd;
948f8e3d 12134 const gdb_byte *index_ptr, *index_end;
80626a55 12135 struct dwarf2_section_info *index;
73869dc2 12136 uint32_t version, nr_columns, nr_units, nr_slots;
80626a55
DE
12137 struct dwp_hash_table *htab;
12138
12139 if (is_debug_types)
12140 index = &dwp_file->sections.tu_index;
12141 else
12142 index = &dwp_file->sections.cu_index;
12143
12144 if (dwarf2_section_empty_p (index))
12145 return NULL;
12146 dwarf2_read_section (objfile, index);
12147
12148 index_ptr = index->buffer;
12149 index_end = index_ptr + index->size;
12150
12151 version = read_4_bytes (dbfd, index_ptr);
73869dc2
DE
12152 index_ptr += 4;
12153 if (version == 2)
12154 nr_columns = read_4_bytes (dbfd, index_ptr);
12155 else
12156 nr_columns = 0;
12157 index_ptr += 4;
80626a55
DE
12158 nr_units = read_4_bytes (dbfd, index_ptr);
12159 index_ptr += 4;
12160 nr_slots = read_4_bytes (dbfd, index_ptr);
12161 index_ptr += 4;
12162
73869dc2 12163 if (version != 1 && version != 2)
80626a55 12164 {
21aa081e 12165 error (_("Dwarf Error: unsupported DWP file version (%s)"
80626a55 12166 " [in module %s]"),
21aa081e 12167 pulongest (version), dwp_file->name);
80626a55
DE
12168 }
12169 if (nr_slots != (nr_slots & -nr_slots))
12170 {
21aa081e 12171 error (_("Dwarf Error: number of slots in DWP hash table (%s)"
80626a55 12172 " is not power of 2 [in module %s]"),
21aa081e 12173 pulongest (nr_slots), dwp_file->name);
80626a55
DE
12174 }
12175
12176 htab = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_hash_table);
73869dc2
DE
12177 htab->version = version;
12178 htab->nr_columns = nr_columns;
80626a55
DE
12179 htab->nr_units = nr_units;
12180 htab->nr_slots = nr_slots;
12181 htab->hash_table = index_ptr;
12182 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
73869dc2
DE
12183
12184 /* Exit early if the table is empty. */
12185 if (nr_slots == 0 || nr_units == 0
12186 || (version == 2 && nr_columns == 0))
12187 {
12188 /* All must be zero. */
12189 if (nr_slots != 0 || nr_units != 0
12190 || (version == 2 && nr_columns != 0))
12191 {
12192 complaint (&symfile_complaints,
12193 _("Empty DWP but nr_slots,nr_units,nr_columns not"
12194 " all zero [in modules %s]"),
12195 dwp_file->name);
12196 }
12197 return htab;
12198 }
12199
12200 if (version == 1)
12201 {
12202 htab->section_pool.v1.indices =
12203 htab->unit_table + sizeof (uint32_t) * nr_slots;
12204 /* It's harder to decide whether the section is too small in v1.
12205 V1 is deprecated anyway so we punt. */
12206 }
12207 else
12208 {
12209 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
12210 int *ids = htab->section_pool.v2.section_ids;
12211 /* Reverse map for error checking. */
12212 int ids_seen[DW_SECT_MAX + 1];
12213 int i;
12214
12215 if (nr_columns < 2)
12216 {
12217 error (_("Dwarf Error: bad DWP hash table, too few columns"
12218 " in section table [in module %s]"),
12219 dwp_file->name);
12220 }
12221 if (nr_columns > MAX_NR_V2_DWO_SECTIONS)
12222 {
12223 error (_("Dwarf Error: bad DWP hash table, too many columns"
12224 " in section table [in module %s]"),
12225 dwp_file->name);
12226 }
12227 memset (ids, 255, (DW_SECT_MAX + 1) * sizeof (int32_t));
12228 memset (ids_seen, 255, (DW_SECT_MAX + 1) * sizeof (int32_t));
12229 for (i = 0; i < nr_columns; ++i)
12230 {
12231 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
12232
12233 if (id < DW_SECT_MIN || id > DW_SECT_MAX)
12234 {
12235 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
12236 " in section table [in module %s]"),
12237 id, dwp_file->name);
12238 }
12239 if (ids_seen[id] != -1)
12240 {
12241 error (_("Dwarf Error: bad DWP hash table, duplicate section"
12242 " id %d in section table [in module %s]"),
12243 id, dwp_file->name);
12244 }
12245 ids_seen[id] = i;
12246 ids[i] = id;
12247 }
12248 /* Must have exactly one info or types section. */
12249 if (((ids_seen[DW_SECT_INFO] != -1)
12250 + (ids_seen[DW_SECT_TYPES] != -1))
12251 != 1)
12252 {
12253 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
12254 " DWO info/types section [in module %s]"),
12255 dwp_file->name);
12256 }
12257 /* Must have an abbrev section. */
12258 if (ids_seen[DW_SECT_ABBREV] == -1)
12259 {
12260 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
12261 " section [in module %s]"),
12262 dwp_file->name);
12263 }
12264 htab->section_pool.v2.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
12265 htab->section_pool.v2.sizes =
12266 htab->section_pool.v2.offsets + (sizeof (uint32_t)
12267 * nr_units * nr_columns);
12268 if ((htab->section_pool.v2.sizes + (sizeof (uint32_t)
12269 * nr_units * nr_columns))
12270 > index_end)
12271 {
12272 error (_("Dwarf Error: DWP index section is corrupt (too small)"
12273 " [in module %s]"),
12274 dwp_file->name);
12275 }
12276 }
80626a55
DE
12277
12278 return htab;
12279}
12280
12281/* Update SECTIONS with the data from SECTP.
12282
12283 This function is like the other "locate" section routines that are
12284 passed to bfd_map_over_sections, but in this context the sections to
73869dc2 12285 read comes from the DWP V1 hash table, not the full ELF section table.
80626a55
DE
12286
12287 The result is non-zero for success, or zero if an error was found. */
12288
12289static int
73869dc2
DE
12290locate_v1_virtual_dwo_sections (asection *sectp,
12291 struct virtual_v1_dwo_sections *sections)
80626a55
DE
12292{
12293 const struct dwop_section_names *names = &dwop_section_names;
12294
12295 if (section_is_p (sectp->name, &names->abbrev_dwo))
12296 {
12297 /* There can be only one. */
049412e3 12298 if (sections->abbrev.s.section != NULL)
80626a55 12299 return 0;
049412e3 12300 sections->abbrev.s.section = sectp;
80626a55
DE
12301 sections->abbrev.size = bfd_get_section_size (sectp);
12302 }
12303 else if (section_is_p (sectp->name, &names->info_dwo)
12304 || section_is_p (sectp->name, &names->types_dwo))
12305 {
12306 /* There can be only one. */
049412e3 12307 if (sections->info_or_types.s.section != NULL)
80626a55 12308 return 0;
049412e3 12309 sections->info_or_types.s.section = sectp;
80626a55
DE
12310 sections->info_or_types.size = bfd_get_section_size (sectp);
12311 }
12312 else if (section_is_p (sectp->name, &names->line_dwo))
12313 {
12314 /* There can be only one. */
049412e3 12315 if (sections->line.s.section != NULL)
80626a55 12316 return 0;
049412e3 12317 sections->line.s.section = sectp;
80626a55
DE
12318 sections->line.size = bfd_get_section_size (sectp);
12319 }
12320 else if (section_is_p (sectp->name, &names->loc_dwo))
12321 {
12322 /* There can be only one. */
049412e3 12323 if (sections->loc.s.section != NULL)
80626a55 12324 return 0;
049412e3 12325 sections->loc.s.section = sectp;
80626a55
DE
12326 sections->loc.size = bfd_get_section_size (sectp);
12327 }
12328 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12329 {
12330 /* There can be only one. */
049412e3 12331 if (sections->macinfo.s.section != NULL)
80626a55 12332 return 0;
049412e3 12333 sections->macinfo.s.section = sectp;
80626a55
DE
12334 sections->macinfo.size = bfd_get_section_size (sectp);
12335 }
12336 else if (section_is_p (sectp->name, &names->macro_dwo))
12337 {
12338 /* There can be only one. */
049412e3 12339 if (sections->macro.s.section != NULL)
80626a55 12340 return 0;
049412e3 12341 sections->macro.s.section = sectp;
80626a55
DE
12342 sections->macro.size = bfd_get_section_size (sectp);
12343 }
12344 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12345 {
12346 /* There can be only one. */
049412e3 12347 if (sections->str_offsets.s.section != NULL)
80626a55 12348 return 0;
049412e3 12349 sections->str_offsets.s.section = sectp;
80626a55
DE
12350 sections->str_offsets.size = bfd_get_section_size (sectp);
12351 }
12352 else
12353 {
12354 /* No other kind of section is valid. */
12355 return 0;
12356 }
12357
12358 return 1;
12359}
12360
73869dc2
DE
12361/* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
12362 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
12363 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
12364 This is for DWP version 1 files. */
80626a55
DE
12365
12366static struct dwo_unit *
ed2dc618
SM
12367create_dwo_unit_in_dwp_v1 (struct dwarf2_per_objfile *dwarf2_per_objfile,
12368 struct dwp_file *dwp_file,
73869dc2
DE
12369 uint32_t unit_index,
12370 const char *comp_dir,
12371 ULONGEST signature, int is_debug_types)
80626a55
DE
12372{
12373 struct objfile *objfile = dwarf2_per_objfile->objfile;
73869dc2
DE
12374 const struct dwp_hash_table *dwp_htab =
12375 is_debug_types ? dwp_file->tus : dwp_file->cus;
80626a55
DE
12376 bfd *dbfd = dwp_file->dbfd;
12377 const char *kind = is_debug_types ? "TU" : "CU";
12378 struct dwo_file *dwo_file;
12379 struct dwo_unit *dwo_unit;
73869dc2 12380 struct virtual_v1_dwo_sections sections;
80626a55 12381 void **dwo_file_slot;
80626a55
DE
12382 int i;
12383
73869dc2
DE
12384 gdb_assert (dwp_file->version == 1);
12385
b4f54984 12386 if (dwarf_read_debug)
80626a55 12387 {
73869dc2 12388 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V1 file: %s\n",
80626a55 12389 kind,
73869dc2 12390 pulongest (unit_index), hex_string (signature),
80626a55
DE
12391 dwp_file->name);
12392 }
12393
19ac8c2e 12394 /* Fetch the sections of this DWO unit.
80626a55
DE
12395 Put a limit on the number of sections we look for so that bad data
12396 doesn't cause us to loop forever. */
12397
73869dc2 12398#define MAX_NR_V1_DWO_SECTIONS \
80626a55
DE
12399 (1 /* .debug_info or .debug_types */ \
12400 + 1 /* .debug_abbrev */ \
12401 + 1 /* .debug_line */ \
12402 + 1 /* .debug_loc */ \
12403 + 1 /* .debug_str_offsets */ \
19ac8c2e 12404 + 1 /* .debug_macro or .debug_macinfo */ \
80626a55
DE
12405 + 1 /* trailing zero */)
12406
12407 memset (&sections, 0, sizeof (sections));
80626a55 12408
73869dc2 12409 for (i = 0; i < MAX_NR_V1_DWO_SECTIONS; ++i)
80626a55
DE
12410 {
12411 asection *sectp;
12412 uint32_t section_nr =
12413 read_4_bytes (dbfd,
73869dc2
DE
12414 dwp_htab->section_pool.v1.indices
12415 + (unit_index + i) * sizeof (uint32_t));
80626a55
DE
12416
12417 if (section_nr == 0)
12418 break;
12419 if (section_nr >= dwp_file->num_sections)
12420 {
12421 error (_("Dwarf Error: bad DWP hash table, section number too large"
12422 " [in module %s]"),
12423 dwp_file->name);
12424 }
12425
12426 sectp = dwp_file->elf_sections[section_nr];
73869dc2 12427 if (! locate_v1_virtual_dwo_sections (sectp, &sections))
80626a55
DE
12428 {
12429 error (_("Dwarf Error: bad DWP hash table, invalid section found"
12430 " [in module %s]"),
12431 dwp_file->name);
12432 }
12433 }
12434
12435 if (i < 2
a32a8923
DE
12436 || dwarf2_section_empty_p (&sections.info_or_types)
12437 || dwarf2_section_empty_p (&sections.abbrev))
80626a55
DE
12438 {
12439 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
12440 " [in module %s]"),
12441 dwp_file->name);
12442 }
73869dc2 12443 if (i == MAX_NR_V1_DWO_SECTIONS)
80626a55
DE
12444 {
12445 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
12446 " [in module %s]"),
12447 dwp_file->name);
12448 }
12449
12450 /* It's easier for the rest of the code if we fake a struct dwo_file and
12451 have dwo_unit "live" in that. At least for now.
12452
12453 The DWP file can be made up of a random collection of CUs and TUs.
c766f7ec 12454 However, for each CU + set of TUs that came from the same original DWO
57d63ce2
DE
12455 file, we can combine them back into a virtual DWO file to save space
12456 (fewer struct dwo_file objects to allocate). Remember that for really
80626a55
DE
12457 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
12458
791afaa2
TT
12459 std::string virtual_dwo_name =
12460 string_printf ("virtual-dwo/%d-%d-%d-%d",
12461 get_section_id (&sections.abbrev),
12462 get_section_id (&sections.line),
12463 get_section_id (&sections.loc),
12464 get_section_id (&sections.str_offsets));
80626a55 12465 /* Can we use an existing virtual DWO file? */
ed2dc618
SM
12466 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
12467 virtual_dwo_name.c_str (),
12468 comp_dir);
80626a55
DE
12469 /* Create one if necessary. */
12470 if (*dwo_file_slot == NULL)
12471 {
b4f54984 12472 if (dwarf_read_debug)
80626a55
DE
12473 {
12474 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
791afaa2 12475 virtual_dwo_name.c_str ());
80626a55
DE
12476 }
12477 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
224c3ddb
SM
12478 dwo_file->dwo_name
12479 = (const char *) obstack_copy0 (&objfile->objfile_obstack,
791afaa2
TT
12480 virtual_dwo_name.c_str (),
12481 virtual_dwo_name.size ());
0ac5b59e 12482 dwo_file->comp_dir = comp_dir;
80626a55
DE
12483 dwo_file->sections.abbrev = sections.abbrev;
12484 dwo_file->sections.line = sections.line;
12485 dwo_file->sections.loc = sections.loc;
12486 dwo_file->sections.macinfo = sections.macinfo;
12487 dwo_file->sections.macro = sections.macro;
12488 dwo_file->sections.str_offsets = sections.str_offsets;
12489 /* The "str" section is global to the entire DWP file. */
12490 dwo_file->sections.str = dwp_file->sections.str;
57d63ce2 12491 /* The info or types section is assigned below to dwo_unit,
80626a55
DE
12492 there's no need to record it in dwo_file.
12493 Also, we can't simply record type sections in dwo_file because
12494 we record a pointer into the vector in dwo_unit. As we collect more
12495 types we'll grow the vector and eventually have to reallocate space
57d63ce2
DE
12496 for it, invalidating all copies of pointers into the previous
12497 contents. */
80626a55
DE
12498 *dwo_file_slot = dwo_file;
12499 }
12500 else
12501 {
b4f54984 12502 if (dwarf_read_debug)
80626a55
DE
12503 {
12504 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
791afaa2 12505 virtual_dwo_name.c_str ());
80626a55 12506 }
9a3c8263 12507 dwo_file = (struct dwo_file *) *dwo_file_slot;
80626a55 12508 }
80626a55
DE
12509
12510 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
12511 dwo_unit->dwo_file = dwo_file;
12512 dwo_unit->signature = signature;
8d749320
SM
12513 dwo_unit->section =
12514 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
8a0459fd 12515 *dwo_unit->section = sections.info_or_types;
57d63ce2 12516 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
80626a55
DE
12517
12518 return dwo_unit;
12519}
12520
73869dc2
DE
12521/* Subroutine of create_dwo_unit_in_dwp_v2 to simplify it.
12522 Given a pointer to the containing section SECTION, and OFFSET,SIZE of the
12523 piece within that section used by a TU/CU, return a virtual section
12524 of just that piece. */
12525
12526static struct dwarf2_section_info
ed2dc618
SM
12527create_dwp_v2_section (struct dwarf2_per_objfile *dwarf2_per_objfile,
12528 struct dwarf2_section_info *section,
73869dc2
DE
12529 bfd_size_type offset, bfd_size_type size)
12530{
12531 struct dwarf2_section_info result;
12532 asection *sectp;
12533
12534 gdb_assert (section != NULL);
12535 gdb_assert (!section->is_virtual);
12536
12537 memset (&result, 0, sizeof (result));
12538 result.s.containing_section = section;
12539 result.is_virtual = 1;
12540
12541 if (size == 0)
12542 return result;
12543
12544 sectp = get_section_bfd_section (section);
12545
12546 /* Flag an error if the piece denoted by OFFSET,SIZE is outside the
12547 bounds of the real section. This is a pretty-rare event, so just
12548 flag an error (easier) instead of a warning and trying to cope. */
12549 if (sectp == NULL
12550 || offset + size > bfd_get_section_size (sectp))
12551 {
73869dc2
DE
12552 error (_("Dwarf Error: Bad DWP V2 section info, doesn't fit"
12553 " in section %s [in module %s]"),
12554 sectp ? bfd_section_name (abfd, sectp) : "<unknown>",
12555 objfile_name (dwarf2_per_objfile->objfile));
12556 }
12557
12558 result.virtual_offset = offset;
12559 result.size = size;
12560 return result;
12561}
12562
12563/* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
12564 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
12565 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
12566 This is for DWP version 2 files. */
12567
12568static struct dwo_unit *
ed2dc618
SM
12569create_dwo_unit_in_dwp_v2 (struct dwarf2_per_objfile *dwarf2_per_objfile,
12570 struct dwp_file *dwp_file,
73869dc2
DE
12571 uint32_t unit_index,
12572 const char *comp_dir,
12573 ULONGEST signature, int is_debug_types)
12574{
12575 struct objfile *objfile = dwarf2_per_objfile->objfile;
12576 const struct dwp_hash_table *dwp_htab =
12577 is_debug_types ? dwp_file->tus : dwp_file->cus;
12578 bfd *dbfd = dwp_file->dbfd;
12579 const char *kind = is_debug_types ? "TU" : "CU";
12580 struct dwo_file *dwo_file;
12581 struct dwo_unit *dwo_unit;
12582 struct virtual_v2_dwo_sections sections;
12583 void **dwo_file_slot;
73869dc2
DE
12584 int i;
12585
12586 gdb_assert (dwp_file->version == 2);
12587
b4f54984 12588 if (dwarf_read_debug)
73869dc2
DE
12589 {
12590 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V2 file: %s\n",
12591 kind,
12592 pulongest (unit_index), hex_string (signature),
12593 dwp_file->name);
12594 }
12595
12596 /* Fetch the section offsets of this DWO unit. */
12597
12598 memset (&sections, 0, sizeof (sections));
73869dc2
DE
12599
12600 for (i = 0; i < dwp_htab->nr_columns; ++i)
12601 {
12602 uint32_t offset = read_4_bytes (dbfd,
12603 dwp_htab->section_pool.v2.offsets
12604 + (((unit_index - 1) * dwp_htab->nr_columns
12605 + i)
12606 * sizeof (uint32_t)));
12607 uint32_t size = read_4_bytes (dbfd,
12608 dwp_htab->section_pool.v2.sizes
12609 + (((unit_index - 1) * dwp_htab->nr_columns
12610 + i)
12611 * sizeof (uint32_t)));
12612
12613 switch (dwp_htab->section_pool.v2.section_ids[i])
12614 {
12615 case DW_SECT_INFO:
12616 case DW_SECT_TYPES:
12617 sections.info_or_types_offset = offset;
12618 sections.info_or_types_size = size;
12619 break;
12620 case DW_SECT_ABBREV:
12621 sections.abbrev_offset = offset;
12622 sections.abbrev_size = size;
12623 break;
12624 case DW_SECT_LINE:
12625 sections.line_offset = offset;
12626 sections.line_size = size;
12627 break;
12628 case DW_SECT_LOC:
12629 sections.loc_offset = offset;
12630 sections.loc_size = size;
12631 break;
12632 case DW_SECT_STR_OFFSETS:
12633 sections.str_offsets_offset = offset;
12634 sections.str_offsets_size = size;
12635 break;
12636 case DW_SECT_MACINFO:
12637 sections.macinfo_offset = offset;
12638 sections.macinfo_size = size;
12639 break;
12640 case DW_SECT_MACRO:
12641 sections.macro_offset = offset;
12642 sections.macro_size = size;
12643 break;
12644 }
12645 }
12646
12647 /* It's easier for the rest of the code if we fake a struct dwo_file and
12648 have dwo_unit "live" in that. At least for now.
12649
12650 The DWP file can be made up of a random collection of CUs and TUs.
12651 However, for each CU + set of TUs that came from the same original DWO
12652 file, we can combine them back into a virtual DWO file to save space
12653 (fewer struct dwo_file objects to allocate). Remember that for really
12654 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
12655
791afaa2
TT
12656 std::string virtual_dwo_name =
12657 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld",
12658 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
12659 (long) (sections.line_size ? sections.line_offset : 0),
12660 (long) (sections.loc_size ? sections.loc_offset : 0),
12661 (long) (sections.str_offsets_size
12662 ? sections.str_offsets_offset : 0));
73869dc2 12663 /* Can we use an existing virtual DWO file? */
ed2dc618
SM
12664 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
12665 virtual_dwo_name.c_str (),
12666 comp_dir);
73869dc2
DE
12667 /* Create one if necessary. */
12668 if (*dwo_file_slot == NULL)
12669 {
b4f54984 12670 if (dwarf_read_debug)
73869dc2
DE
12671 {
12672 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
791afaa2 12673 virtual_dwo_name.c_str ());
73869dc2
DE
12674 }
12675 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
224c3ddb
SM
12676 dwo_file->dwo_name
12677 = (const char *) obstack_copy0 (&objfile->objfile_obstack,
791afaa2
TT
12678 virtual_dwo_name.c_str (),
12679 virtual_dwo_name.size ());
73869dc2
DE
12680 dwo_file->comp_dir = comp_dir;
12681 dwo_file->sections.abbrev =
ed2dc618 12682 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.abbrev,
73869dc2
DE
12683 sections.abbrev_offset, sections.abbrev_size);
12684 dwo_file->sections.line =
ed2dc618 12685 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.line,
73869dc2
DE
12686 sections.line_offset, sections.line_size);
12687 dwo_file->sections.loc =
ed2dc618 12688 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.loc,
73869dc2
DE
12689 sections.loc_offset, sections.loc_size);
12690 dwo_file->sections.macinfo =
ed2dc618 12691 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.macinfo,
73869dc2
DE
12692 sections.macinfo_offset, sections.macinfo_size);
12693 dwo_file->sections.macro =
ed2dc618 12694 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.macro,
73869dc2
DE
12695 sections.macro_offset, sections.macro_size);
12696 dwo_file->sections.str_offsets =
ed2dc618
SM
12697 create_dwp_v2_section (dwarf2_per_objfile,
12698 &dwp_file->sections.str_offsets,
73869dc2
DE
12699 sections.str_offsets_offset,
12700 sections.str_offsets_size);
12701 /* The "str" section is global to the entire DWP file. */
12702 dwo_file->sections.str = dwp_file->sections.str;
12703 /* The info or types section is assigned below to dwo_unit,
12704 there's no need to record it in dwo_file.
12705 Also, we can't simply record type sections in dwo_file because
12706 we record a pointer into the vector in dwo_unit. As we collect more
12707 types we'll grow the vector and eventually have to reallocate space
12708 for it, invalidating all copies of pointers into the previous
12709 contents. */
12710 *dwo_file_slot = dwo_file;
12711 }
12712 else
12713 {
b4f54984 12714 if (dwarf_read_debug)
73869dc2
DE
12715 {
12716 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
791afaa2 12717 virtual_dwo_name.c_str ());
73869dc2 12718 }
9a3c8263 12719 dwo_file = (struct dwo_file *) *dwo_file_slot;
73869dc2 12720 }
73869dc2
DE
12721
12722 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
12723 dwo_unit->dwo_file = dwo_file;
12724 dwo_unit->signature = signature;
8d749320
SM
12725 dwo_unit->section =
12726 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
ed2dc618
SM
12727 *dwo_unit->section = create_dwp_v2_section (dwarf2_per_objfile,
12728 is_debug_types
73869dc2
DE
12729 ? &dwp_file->sections.types
12730 : &dwp_file->sections.info,
12731 sections.info_or_types_offset,
12732 sections.info_or_types_size);
12733 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
12734
12735 return dwo_unit;
12736}
12737
57d63ce2
DE
12738/* Lookup the DWO unit with SIGNATURE in DWP_FILE.
12739 Returns NULL if the signature isn't found. */
80626a55
DE
12740
12741static struct dwo_unit *
ed2dc618
SM
12742lookup_dwo_unit_in_dwp (struct dwarf2_per_objfile *dwarf2_per_objfile,
12743 struct dwp_file *dwp_file, const char *comp_dir,
57d63ce2 12744 ULONGEST signature, int is_debug_types)
80626a55 12745{
57d63ce2
DE
12746 const struct dwp_hash_table *dwp_htab =
12747 is_debug_types ? dwp_file->tus : dwp_file->cus;
80626a55 12748 bfd *dbfd = dwp_file->dbfd;
57d63ce2 12749 uint32_t mask = dwp_htab->nr_slots - 1;
80626a55
DE
12750 uint32_t hash = signature & mask;
12751 uint32_t hash2 = ((signature >> 32) & mask) | 1;
12752 unsigned int i;
12753 void **slot;
870f88f7 12754 struct dwo_unit find_dwo_cu;
80626a55
DE
12755
12756 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
12757 find_dwo_cu.signature = signature;
19ac8c2e
DE
12758 slot = htab_find_slot (is_debug_types
12759 ? dwp_file->loaded_tus
12760 : dwp_file->loaded_cus,
12761 &find_dwo_cu, INSERT);
80626a55
DE
12762
12763 if (*slot != NULL)
9a3c8263 12764 return (struct dwo_unit *) *slot;
80626a55
DE
12765
12766 /* Use a for loop so that we don't loop forever on bad debug info. */
57d63ce2 12767 for (i = 0; i < dwp_htab->nr_slots; ++i)
80626a55
DE
12768 {
12769 ULONGEST signature_in_table;
12770
12771 signature_in_table =
57d63ce2 12772 read_8_bytes (dbfd, dwp_htab->hash_table + hash * sizeof (uint64_t));
80626a55
DE
12773 if (signature_in_table == signature)
12774 {
57d63ce2
DE
12775 uint32_t unit_index =
12776 read_4_bytes (dbfd,
12777 dwp_htab->unit_table + hash * sizeof (uint32_t));
80626a55 12778
73869dc2
DE
12779 if (dwp_file->version == 1)
12780 {
ed2dc618
SM
12781 *slot = create_dwo_unit_in_dwp_v1 (dwarf2_per_objfile,
12782 dwp_file, unit_index,
73869dc2
DE
12783 comp_dir, signature,
12784 is_debug_types);
12785 }
12786 else
12787 {
ed2dc618
SM
12788 *slot = create_dwo_unit_in_dwp_v2 (dwarf2_per_objfile,
12789 dwp_file, unit_index,
73869dc2
DE
12790 comp_dir, signature,
12791 is_debug_types);
12792 }
9a3c8263 12793 return (struct dwo_unit *) *slot;
80626a55
DE
12794 }
12795 if (signature_in_table == 0)
12796 return NULL;
12797 hash = (hash + hash2) & mask;
12798 }
12799
12800 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
12801 " [in module %s]"),
12802 dwp_file->name);
12803}
12804
ab5088bf 12805/* Subroutine of open_dwo_file,open_dwp_file to simplify them.
3019eac3
DE
12806 Open the file specified by FILE_NAME and hand it off to BFD for
12807 preliminary analysis. Return a newly initialized bfd *, which
12808 includes a canonicalized copy of FILE_NAME.
80626a55 12809 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
6ac97d4c
DE
12810 SEARCH_CWD is true if the current directory is to be searched.
12811 It will be searched before debug-file-directory.
13aaf454
DE
12812 If successful, the file is added to the bfd include table of the
12813 objfile's bfd (see gdb_bfd_record_inclusion).
6ac97d4c 12814 If unable to find/open the file, return NULL.
3019eac3
DE
12815 NOTE: This function is derived from symfile_bfd_open. */
12816
192b62ce 12817static gdb_bfd_ref_ptr
ed2dc618
SM
12818try_open_dwop_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12819 const char *file_name, int is_dwp, int search_cwd)
3019eac3 12820{
24b9144d 12821 int desc;
9c02c129
DE
12822 /* Blech. OPF_TRY_CWD_FIRST also disables searching the path list if
12823 FILE_NAME contains a '/'. So we can't use it. Instead prepend "."
12824 to debug_file_directory. */
e0cc99a6 12825 const char *search_path;
9c02c129
DE
12826 static const char dirname_separator_string[] = { DIRNAME_SEPARATOR, '\0' };
12827
e0cc99a6 12828 gdb::unique_xmalloc_ptr<char> search_path_holder;
6ac97d4c
DE
12829 if (search_cwd)
12830 {
12831 if (*debug_file_directory != '\0')
e0cc99a6
TT
12832 {
12833 search_path_holder.reset (concat (".", dirname_separator_string,
12834 debug_file_directory,
12835 (char *) NULL));
12836 search_path = search_path_holder.get ();
12837 }
6ac97d4c 12838 else
e0cc99a6 12839 search_path = ".";
6ac97d4c 12840 }
9c02c129 12841 else
e0cc99a6 12842 search_path = debug_file_directory;
3019eac3 12843
24b9144d 12844 openp_flags flags = OPF_RETURN_REALPATH;
80626a55
DE
12845 if (is_dwp)
12846 flags |= OPF_SEARCH_IN_PATH;
e0cc99a6
TT
12847
12848 gdb::unique_xmalloc_ptr<char> absolute_name;
9c02c129 12849 desc = openp (search_path, flags, file_name,
3019eac3
DE
12850 O_RDONLY | O_BINARY, &absolute_name);
12851 if (desc < 0)
12852 return NULL;
12853
e0cc99a6
TT
12854 gdb_bfd_ref_ptr sym_bfd (gdb_bfd_open (absolute_name.get (),
12855 gnutarget, desc));
9c02c129
DE
12856 if (sym_bfd == NULL)
12857 return NULL;
192b62ce 12858 bfd_set_cacheable (sym_bfd.get (), 1);
3019eac3 12859
192b62ce
TT
12860 if (!bfd_check_format (sym_bfd.get (), bfd_object))
12861 return NULL;
3019eac3 12862
13aaf454
DE
12863 /* Success. Record the bfd as having been included by the objfile's bfd.
12864 This is important because things like demangled_names_hash lives in the
12865 objfile's per_bfd space and may have references to things like symbol
12866 names that live in the DWO/DWP file's per_bfd space. PR 16426. */
192b62ce 12867 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd, sym_bfd.get ());
13aaf454 12868
3019eac3
DE
12869 return sym_bfd;
12870}
12871
ab5088bf 12872/* Try to open DWO file FILE_NAME.
3019eac3
DE
12873 COMP_DIR is the DW_AT_comp_dir attribute.
12874 The result is the bfd handle of the file.
12875 If there is a problem finding or opening the file, return NULL.
12876 Upon success, the canonicalized path of the file is stored in the bfd,
12877 same as symfile_bfd_open. */
12878
192b62ce 12879static gdb_bfd_ref_ptr
ed2dc618
SM
12880open_dwo_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12881 const char *file_name, const char *comp_dir)
3019eac3 12882{
80626a55 12883 if (IS_ABSOLUTE_PATH (file_name))
ed2dc618
SM
12884 return try_open_dwop_file (dwarf2_per_objfile, file_name,
12885 0 /*is_dwp*/, 0 /*search_cwd*/);
3019eac3
DE
12886
12887 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
12888
12889 if (comp_dir != NULL)
12890 {
b36cec19
PA
12891 char *path_to_try = concat (comp_dir, SLASH_STRING,
12892 file_name, (char *) NULL);
3019eac3
DE
12893
12894 /* NOTE: If comp_dir is a relative path, this will also try the
12895 search path, which seems useful. */
ed2dc618
SM
12896 gdb_bfd_ref_ptr abfd (try_open_dwop_file (dwarf2_per_objfile,
12897 path_to_try,
12898 0 /*is_dwp*/,
192b62ce 12899 1 /*search_cwd*/));
3019eac3
DE
12900 xfree (path_to_try);
12901 if (abfd != NULL)
12902 return abfd;
12903 }
12904
12905 /* That didn't work, try debug-file-directory, which, despite its name,
12906 is a list of paths. */
12907
12908 if (*debug_file_directory == '\0')
12909 return NULL;
12910
ed2dc618
SM
12911 return try_open_dwop_file (dwarf2_per_objfile, file_name,
12912 0 /*is_dwp*/, 1 /*search_cwd*/);
3019eac3
DE
12913}
12914
80626a55
DE
12915/* This function is mapped across the sections and remembers the offset and
12916 size of each of the DWO debugging sections we are interested in. */
12917
12918static void
12919dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp, void *dwo_sections_ptr)
12920{
9a3c8263 12921 struct dwo_sections *dwo_sections = (struct dwo_sections *) dwo_sections_ptr;
80626a55
DE
12922 const struct dwop_section_names *names = &dwop_section_names;
12923
12924 if (section_is_p (sectp->name, &names->abbrev_dwo))
12925 {
049412e3 12926 dwo_sections->abbrev.s.section = sectp;
80626a55
DE
12927 dwo_sections->abbrev.size = bfd_get_section_size (sectp);
12928 }
12929 else if (section_is_p (sectp->name, &names->info_dwo))
12930 {
049412e3 12931 dwo_sections->info.s.section = sectp;
80626a55
DE
12932 dwo_sections->info.size = bfd_get_section_size (sectp);
12933 }
12934 else if (section_is_p (sectp->name, &names->line_dwo))
12935 {
049412e3 12936 dwo_sections->line.s.section = sectp;
80626a55
DE
12937 dwo_sections->line.size = bfd_get_section_size (sectp);
12938 }
12939 else if (section_is_p (sectp->name, &names->loc_dwo))
12940 {
049412e3 12941 dwo_sections->loc.s.section = sectp;
80626a55
DE
12942 dwo_sections->loc.size = bfd_get_section_size (sectp);
12943 }
12944 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12945 {
049412e3 12946 dwo_sections->macinfo.s.section = sectp;
80626a55
DE
12947 dwo_sections->macinfo.size = bfd_get_section_size (sectp);
12948 }
12949 else if (section_is_p (sectp->name, &names->macro_dwo))
12950 {
049412e3 12951 dwo_sections->macro.s.section = sectp;
80626a55
DE
12952 dwo_sections->macro.size = bfd_get_section_size (sectp);
12953 }
12954 else if (section_is_p (sectp->name, &names->str_dwo))
12955 {
049412e3 12956 dwo_sections->str.s.section = sectp;
80626a55
DE
12957 dwo_sections->str.size = bfd_get_section_size (sectp);
12958 }
12959 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12960 {
049412e3 12961 dwo_sections->str_offsets.s.section = sectp;
80626a55
DE
12962 dwo_sections->str_offsets.size = bfd_get_section_size (sectp);
12963 }
12964 else if (section_is_p (sectp->name, &names->types_dwo))
12965 {
12966 struct dwarf2_section_info type_section;
12967
12968 memset (&type_section, 0, sizeof (type_section));
049412e3 12969 type_section.s.section = sectp;
80626a55
DE
12970 type_section.size = bfd_get_section_size (sectp);
12971 VEC_safe_push (dwarf2_section_info_def, dwo_sections->types,
12972 &type_section);
12973 }
12974}
12975
ab5088bf 12976/* Initialize the use of the DWO file specified by DWO_NAME and referenced
19c3d4c9 12977 by PER_CU. This is for the non-DWP case.
80626a55 12978 The result is NULL if DWO_NAME can't be found. */
3019eac3
DE
12979
12980static struct dwo_file *
0ac5b59e
DE
12981open_and_init_dwo_file (struct dwarf2_per_cu_data *per_cu,
12982 const char *dwo_name, const char *comp_dir)
3019eac3 12983{
ed2dc618 12984 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
3019eac3 12985 struct objfile *objfile = dwarf2_per_objfile->objfile;
80626a55 12986 struct dwo_file *dwo_file;
3019eac3
DE
12987 struct cleanup *cleanups;
12988
ed2dc618 12989 gdb_bfd_ref_ptr dbfd (open_dwo_file (dwarf2_per_objfile, dwo_name, comp_dir));
80626a55
DE
12990 if (dbfd == NULL)
12991 {
b4f54984 12992 if (dwarf_read_debug)
80626a55
DE
12993 fprintf_unfiltered (gdb_stdlog, "DWO file not found: %s\n", dwo_name);
12994 return NULL;
12995 }
12996 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
0ac5b59e
DE
12997 dwo_file->dwo_name = dwo_name;
12998 dwo_file->comp_dir = comp_dir;
192b62ce 12999 dwo_file->dbfd = dbfd.release ();
3019eac3 13000
ed2dc618
SM
13001 free_dwo_file_cleanup_data *cleanup_data = XNEW (free_dwo_file_cleanup_data);
13002 cleanup_data->dwo_file = dwo_file;
13003 cleanup_data->dwarf2_per_objfile = dwarf2_per_objfile;
13004
13005 cleanups = make_cleanup (free_dwo_file_cleanup, cleanup_data);
3019eac3 13006
192b62ce
TT
13007 bfd_map_over_sections (dwo_file->dbfd, dwarf2_locate_dwo_sections,
13008 &dwo_file->sections);
3019eac3 13009
ed2dc618
SM
13010 create_cus_hash_table (dwarf2_per_objfile, *dwo_file, dwo_file->sections.info,
13011 dwo_file->cus);
3019eac3 13012
ed2dc618
SM
13013 create_debug_types_hash_table (dwarf2_per_objfile, dwo_file,
13014 dwo_file->sections.types, dwo_file->tus);
3019eac3
DE
13015
13016 discard_cleanups (cleanups);
13017
b4f54984 13018 if (dwarf_read_debug)
80626a55
DE
13019 fprintf_unfiltered (gdb_stdlog, "DWO file found: %s\n", dwo_name);
13020
3019eac3
DE
13021 return dwo_file;
13022}
13023
80626a55 13024/* This function is mapped across the sections and remembers the offset and
73869dc2
DE
13025 size of each of the DWP debugging sections common to version 1 and 2 that
13026 we are interested in. */
3019eac3 13027
80626a55 13028static void
73869dc2
DE
13029dwarf2_locate_common_dwp_sections (bfd *abfd, asection *sectp,
13030 void *dwp_file_ptr)
3019eac3 13031{
9a3c8263 13032 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
80626a55
DE
13033 const struct dwop_section_names *names = &dwop_section_names;
13034 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
3019eac3 13035
80626a55 13036 /* Record the ELF section number for later lookup: this is what the
73869dc2 13037 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
80626a55
DE
13038 gdb_assert (elf_section_nr < dwp_file->num_sections);
13039 dwp_file->elf_sections[elf_section_nr] = sectp;
3019eac3 13040
80626a55
DE
13041 /* Look for specific sections that we need. */
13042 if (section_is_p (sectp->name, &names->str_dwo))
13043 {
049412e3 13044 dwp_file->sections.str.s.section = sectp;
80626a55
DE
13045 dwp_file->sections.str.size = bfd_get_section_size (sectp);
13046 }
13047 else if (section_is_p (sectp->name, &names->cu_index))
13048 {
049412e3 13049 dwp_file->sections.cu_index.s.section = sectp;
80626a55
DE
13050 dwp_file->sections.cu_index.size = bfd_get_section_size (sectp);
13051 }
13052 else if (section_is_p (sectp->name, &names->tu_index))
13053 {
049412e3 13054 dwp_file->sections.tu_index.s.section = sectp;
80626a55
DE
13055 dwp_file->sections.tu_index.size = bfd_get_section_size (sectp);
13056 }
13057}
3019eac3 13058
73869dc2
DE
13059/* This function is mapped across the sections and remembers the offset and
13060 size of each of the DWP version 2 debugging sections that we are interested
13061 in. This is split into a separate function because we don't know if we
13062 have version 1 or 2 until we parse the cu_index/tu_index sections. */
13063
13064static void
13065dwarf2_locate_v2_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
13066{
9a3c8263 13067 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
73869dc2
DE
13068 const struct dwop_section_names *names = &dwop_section_names;
13069 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
13070
13071 /* Record the ELF section number for later lookup: this is what the
13072 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
13073 gdb_assert (elf_section_nr < dwp_file->num_sections);
13074 dwp_file->elf_sections[elf_section_nr] = sectp;
13075
13076 /* Look for specific sections that we need. */
13077 if (section_is_p (sectp->name, &names->abbrev_dwo))
13078 {
049412e3 13079 dwp_file->sections.abbrev.s.section = sectp;
73869dc2
DE
13080 dwp_file->sections.abbrev.size = bfd_get_section_size (sectp);
13081 }
13082 else if (section_is_p (sectp->name, &names->info_dwo))
13083 {
049412e3 13084 dwp_file->sections.info.s.section = sectp;
73869dc2
DE
13085 dwp_file->sections.info.size = bfd_get_section_size (sectp);
13086 }
13087 else if (section_is_p (sectp->name, &names->line_dwo))
13088 {
049412e3 13089 dwp_file->sections.line.s.section = sectp;
73869dc2
DE
13090 dwp_file->sections.line.size = bfd_get_section_size (sectp);
13091 }
13092 else if (section_is_p (sectp->name, &names->loc_dwo))
13093 {
049412e3 13094 dwp_file->sections.loc.s.section = sectp;
73869dc2
DE
13095 dwp_file->sections.loc.size = bfd_get_section_size (sectp);
13096 }
13097 else if (section_is_p (sectp->name, &names->macinfo_dwo))
13098 {
049412e3 13099 dwp_file->sections.macinfo.s.section = sectp;
73869dc2
DE
13100 dwp_file->sections.macinfo.size = bfd_get_section_size (sectp);
13101 }
13102 else if (section_is_p (sectp->name, &names->macro_dwo))
13103 {
049412e3 13104 dwp_file->sections.macro.s.section = sectp;
73869dc2
DE
13105 dwp_file->sections.macro.size = bfd_get_section_size (sectp);
13106 }
13107 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
13108 {
049412e3 13109 dwp_file->sections.str_offsets.s.section = sectp;
73869dc2
DE
13110 dwp_file->sections.str_offsets.size = bfd_get_section_size (sectp);
13111 }
13112 else if (section_is_p (sectp->name, &names->types_dwo))
13113 {
049412e3 13114 dwp_file->sections.types.s.section = sectp;
73869dc2
DE
13115 dwp_file->sections.types.size = bfd_get_section_size (sectp);
13116 }
13117}
13118
80626a55 13119/* Hash function for dwp_file loaded CUs/TUs. */
3019eac3 13120
80626a55
DE
13121static hashval_t
13122hash_dwp_loaded_cutus (const void *item)
13123{
9a3c8263 13124 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
3019eac3 13125
80626a55
DE
13126 /* This drops the top 32 bits of the signature, but is ok for a hash. */
13127 return dwo_unit->signature;
3019eac3
DE
13128}
13129
80626a55 13130/* Equality function for dwp_file loaded CUs/TUs. */
3019eac3 13131
80626a55
DE
13132static int
13133eq_dwp_loaded_cutus (const void *a, const void *b)
3019eac3 13134{
9a3c8263
SM
13135 const struct dwo_unit *dua = (const struct dwo_unit *) a;
13136 const struct dwo_unit *dub = (const struct dwo_unit *) b;
3019eac3 13137
80626a55
DE
13138 return dua->signature == dub->signature;
13139}
3019eac3 13140
80626a55 13141/* Allocate a hash table for dwp_file loaded CUs/TUs. */
3019eac3 13142
80626a55
DE
13143static htab_t
13144allocate_dwp_loaded_cutus_table (struct objfile *objfile)
13145{
13146 return htab_create_alloc_ex (3,
13147 hash_dwp_loaded_cutus,
13148 eq_dwp_loaded_cutus,
13149 NULL,
13150 &objfile->objfile_obstack,
13151 hashtab_obstack_allocate,
13152 dummy_obstack_deallocate);
13153}
3019eac3 13154
ab5088bf
DE
13155/* Try to open DWP file FILE_NAME.
13156 The result is the bfd handle of the file.
13157 If there is a problem finding or opening the file, return NULL.
13158 Upon success, the canonicalized path of the file is stored in the bfd,
13159 same as symfile_bfd_open. */
13160
192b62ce 13161static gdb_bfd_ref_ptr
ed2dc618
SM
13162open_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
13163 const char *file_name)
ab5088bf 13164{
ed2dc618
SM
13165 gdb_bfd_ref_ptr abfd (try_open_dwop_file (dwarf2_per_objfile, file_name,
13166 1 /*is_dwp*/,
192b62ce 13167 1 /*search_cwd*/));
6ac97d4c
DE
13168 if (abfd != NULL)
13169 return abfd;
13170
13171 /* Work around upstream bug 15652.
13172 http://sourceware.org/bugzilla/show_bug.cgi?id=15652
13173 [Whether that's a "bug" is debatable, but it is getting in our way.]
13174 We have no real idea where the dwp file is, because gdb's realpath-ing
13175 of the executable's path may have discarded the needed info.
13176 [IWBN if the dwp file name was recorded in the executable, akin to
13177 .gnu_debuglink, but that doesn't exist yet.]
13178 Strip the directory from FILE_NAME and search again. */
13179 if (*debug_file_directory != '\0')
13180 {
13181 /* Don't implicitly search the current directory here.
13182 If the user wants to search "." to handle this case,
13183 it must be added to debug-file-directory. */
ed2dc618
SM
13184 return try_open_dwop_file (dwarf2_per_objfile,
13185 lbasename (file_name), 1 /*is_dwp*/,
6ac97d4c
DE
13186 0 /*search_cwd*/);
13187 }
13188
13189 return NULL;
ab5088bf
DE
13190}
13191
80626a55
DE
13192/* Initialize the use of the DWP file for the current objfile.
13193 By convention the name of the DWP file is ${objfile}.dwp.
13194 The result is NULL if it can't be found. */
a766d390 13195
80626a55 13196static struct dwp_file *
ed2dc618 13197open_and_init_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
80626a55
DE
13198{
13199 struct objfile *objfile = dwarf2_per_objfile->objfile;
13200 struct dwp_file *dwp_file;
80626a55 13201
82bf32bc
JK
13202 /* Try to find first .dwp for the binary file before any symbolic links
13203 resolving. */
6c447423
DE
13204
13205 /* If the objfile is a debug file, find the name of the real binary
13206 file and get the name of dwp file from there. */
d721ba37 13207 std::string dwp_name;
6c447423
DE
13208 if (objfile->separate_debug_objfile_backlink != NULL)
13209 {
13210 struct objfile *backlink = objfile->separate_debug_objfile_backlink;
13211 const char *backlink_basename = lbasename (backlink->original_name);
6c447423 13212
d721ba37 13213 dwp_name = ldirname (objfile->original_name) + SLASH_STRING + backlink_basename;
6c447423
DE
13214 }
13215 else
d721ba37
PA
13216 dwp_name = objfile->original_name;
13217
13218 dwp_name += ".dwp";
80626a55 13219
ed2dc618 13220 gdb_bfd_ref_ptr dbfd (open_dwp_file (dwarf2_per_objfile, dwp_name.c_str ()));
82bf32bc
JK
13221 if (dbfd == NULL
13222 && strcmp (objfile->original_name, objfile_name (objfile)) != 0)
13223 {
13224 /* Try to find .dwp for the binary file after gdb_realpath resolving. */
d721ba37
PA
13225 dwp_name = objfile_name (objfile);
13226 dwp_name += ".dwp";
ed2dc618 13227 dbfd = open_dwp_file (dwarf2_per_objfile, dwp_name.c_str ());
82bf32bc
JK
13228 }
13229
80626a55
DE
13230 if (dbfd == NULL)
13231 {
b4f54984 13232 if (dwarf_read_debug)
d721ba37 13233 fprintf_unfiltered (gdb_stdlog, "DWP file not found: %s\n", dwp_name.c_str ());
80626a55 13234 return NULL;
3019eac3 13235 }
80626a55 13236 dwp_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_file);
192b62ce
TT
13237 dwp_file->name = bfd_get_filename (dbfd.get ());
13238 dwp_file->dbfd = dbfd.release ();
c906108c 13239
80626a55 13240 /* +1: section 0 is unused */
192b62ce 13241 dwp_file->num_sections = bfd_count_sections (dwp_file->dbfd) + 1;
80626a55
DE
13242 dwp_file->elf_sections =
13243 OBSTACK_CALLOC (&objfile->objfile_obstack,
13244 dwp_file->num_sections, asection *);
13245
192b62ce
TT
13246 bfd_map_over_sections (dwp_file->dbfd, dwarf2_locate_common_dwp_sections,
13247 dwp_file);
80626a55 13248
ed2dc618 13249 dwp_file->cus = create_dwp_hash_table (dwarf2_per_objfile, dwp_file, 0);
80626a55 13250
ed2dc618 13251 dwp_file->tus = create_dwp_hash_table (dwarf2_per_objfile, dwp_file, 1);
80626a55 13252
73869dc2 13253 /* The DWP file version is stored in the hash table. Oh well. */
08302ed2
DE
13254 if (dwp_file->cus && dwp_file->tus
13255 && dwp_file->cus->version != dwp_file->tus->version)
73869dc2
DE
13256 {
13257 /* Technically speaking, we should try to limp along, but this is
fbcbc3fd 13258 pretty bizarre. We use pulongest here because that's the established
4d65956b 13259 portability solution (e.g, we cannot use %u for uint32_t). */
fbcbc3fd
DE
13260 error (_("Dwarf Error: DWP file CU version %s doesn't match"
13261 " TU version %s [in DWP file %s]"),
13262 pulongest (dwp_file->cus->version),
d721ba37 13263 pulongest (dwp_file->tus->version), dwp_name.c_str ());
73869dc2 13264 }
08302ed2
DE
13265
13266 if (dwp_file->cus)
13267 dwp_file->version = dwp_file->cus->version;
13268 else if (dwp_file->tus)
13269 dwp_file->version = dwp_file->tus->version;
13270 else
13271 dwp_file->version = 2;
73869dc2
DE
13272
13273 if (dwp_file->version == 2)
192b62ce
TT
13274 bfd_map_over_sections (dwp_file->dbfd, dwarf2_locate_v2_dwp_sections,
13275 dwp_file);
73869dc2 13276
19ac8c2e
DE
13277 dwp_file->loaded_cus = allocate_dwp_loaded_cutus_table (objfile);
13278 dwp_file->loaded_tus = allocate_dwp_loaded_cutus_table (objfile);
80626a55 13279
b4f54984 13280 if (dwarf_read_debug)
80626a55
DE
13281 {
13282 fprintf_unfiltered (gdb_stdlog, "DWP file found: %s\n", dwp_file->name);
13283 fprintf_unfiltered (gdb_stdlog,
21aa081e
PA
13284 " %s CUs, %s TUs\n",
13285 pulongest (dwp_file->cus ? dwp_file->cus->nr_units : 0),
13286 pulongest (dwp_file->tus ? dwp_file->tus->nr_units : 0));
80626a55
DE
13287 }
13288
13289 return dwp_file;
3019eac3 13290}
c906108c 13291
ab5088bf
DE
13292/* Wrapper around open_and_init_dwp_file, only open it once. */
13293
13294static struct dwp_file *
ed2dc618 13295get_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
ab5088bf
DE
13296{
13297 if (! dwarf2_per_objfile->dwp_checked)
13298 {
ed2dc618
SM
13299 dwarf2_per_objfile->dwp_file
13300 = open_and_init_dwp_file (dwarf2_per_objfile);
ab5088bf
DE
13301 dwarf2_per_objfile->dwp_checked = 1;
13302 }
13303 return dwarf2_per_objfile->dwp_file;
13304}
13305
80626a55
DE
13306/* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
13307 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
13308 or in the DWP file for the objfile, referenced by THIS_UNIT.
3019eac3 13309 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
80626a55
DE
13310 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
13311
13312 This is called, for example, when wanting to read a variable with a
13313 complex location. Therefore we don't want to do file i/o for every call.
13314 Therefore we don't want to look for a DWO file on every call.
13315 Therefore we first see if we've already seen SIGNATURE in a DWP file,
13316 then we check if we've already seen DWO_NAME, and only THEN do we check
13317 for a DWO file.
13318
1c658ad5 13319 The result is a pointer to the dwo_unit object or NULL if we didn't find it
80626a55 13320 (dwo_id mismatch or couldn't find the DWO/DWP file). */
debd256d 13321
3019eac3 13322static struct dwo_unit *
80626a55
DE
13323lookup_dwo_cutu (struct dwarf2_per_cu_data *this_unit,
13324 const char *dwo_name, const char *comp_dir,
13325 ULONGEST signature, int is_debug_types)
3019eac3 13326{
ed2dc618 13327 struct dwarf2_per_objfile *dwarf2_per_objfile = this_unit->dwarf2_per_objfile;
3019eac3 13328 struct objfile *objfile = dwarf2_per_objfile->objfile;
80626a55
DE
13329 const char *kind = is_debug_types ? "TU" : "CU";
13330 void **dwo_file_slot;
3019eac3 13331 struct dwo_file *dwo_file;
80626a55 13332 struct dwp_file *dwp_file;
cb1df416 13333
6a506a2d
DE
13334 /* First see if there's a DWP file.
13335 If we have a DWP file but didn't find the DWO inside it, don't
13336 look for the original DWO file. It makes gdb behave differently
13337 depending on whether one is debugging in the build tree. */
cf2c3c16 13338
ed2dc618 13339 dwp_file = get_dwp_file (dwarf2_per_objfile);
80626a55 13340 if (dwp_file != NULL)
cf2c3c16 13341 {
80626a55
DE
13342 const struct dwp_hash_table *dwp_htab =
13343 is_debug_types ? dwp_file->tus : dwp_file->cus;
13344
13345 if (dwp_htab != NULL)
13346 {
13347 struct dwo_unit *dwo_cutu =
ed2dc618 13348 lookup_dwo_unit_in_dwp (dwarf2_per_objfile, dwp_file, comp_dir,
57d63ce2 13349 signature, is_debug_types);
80626a55
DE
13350
13351 if (dwo_cutu != NULL)
13352 {
b4f54984 13353 if (dwarf_read_debug)
80626a55
DE
13354 {
13355 fprintf_unfiltered (gdb_stdlog,
13356 "Virtual DWO %s %s found: @%s\n",
13357 kind, hex_string (signature),
13358 host_address_to_string (dwo_cutu));
13359 }
13360 return dwo_cutu;
13361 }
13362 }
13363 }
6a506a2d 13364 else
80626a55 13365 {
6a506a2d 13366 /* No DWP file, look for the DWO file. */
80626a55 13367
ed2dc618
SM
13368 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
13369 dwo_name, comp_dir);
6a506a2d 13370 if (*dwo_file_slot == NULL)
80626a55 13371 {
6a506a2d
DE
13372 /* Read in the file and build a table of the CUs/TUs it contains. */
13373 *dwo_file_slot = open_and_init_dwo_file (this_unit, dwo_name, comp_dir);
19c3d4c9 13374 }
6a506a2d 13375 /* NOTE: This will be NULL if unable to open the file. */
9a3c8263 13376 dwo_file = (struct dwo_file *) *dwo_file_slot;
3019eac3 13377
6a506a2d 13378 if (dwo_file != NULL)
19c3d4c9 13379 {
6a506a2d
DE
13380 struct dwo_unit *dwo_cutu = NULL;
13381
13382 if (is_debug_types && dwo_file->tus)
13383 {
13384 struct dwo_unit find_dwo_cutu;
13385
13386 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
13387 find_dwo_cutu.signature = signature;
9a3c8263
SM
13388 dwo_cutu
13389 = (struct dwo_unit *) htab_find (dwo_file->tus, &find_dwo_cutu);
6a506a2d 13390 }
33c5cd75 13391 else if (!is_debug_types && dwo_file->cus)
80626a55 13392 {
33c5cd75
DB
13393 struct dwo_unit find_dwo_cutu;
13394
13395 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
13396 find_dwo_cutu.signature = signature;
13397 dwo_cutu = (struct dwo_unit *)htab_find (dwo_file->cus,
13398 &find_dwo_cutu);
6a506a2d
DE
13399 }
13400
13401 if (dwo_cutu != NULL)
13402 {
b4f54984 13403 if (dwarf_read_debug)
6a506a2d
DE
13404 {
13405 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) found: @%s\n",
13406 kind, dwo_name, hex_string (signature),
13407 host_address_to_string (dwo_cutu));
13408 }
13409 return dwo_cutu;
80626a55
DE
13410 }
13411 }
2e276125 13412 }
9cdd5dbd 13413
80626a55
DE
13414 /* We didn't find it. This could mean a dwo_id mismatch, or
13415 someone deleted the DWO/DWP file, or the search path isn't set up
13416 correctly to find the file. */
13417
b4f54984 13418 if (dwarf_read_debug)
80626a55
DE
13419 {
13420 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) not found\n",
13421 kind, dwo_name, hex_string (signature));
13422 }
3019eac3 13423
6656a72d
DE
13424 /* This is a warning and not a complaint because it can be caused by
13425 pilot error (e.g., user accidentally deleting the DWO). */
43942612
DE
13426 {
13427 /* Print the name of the DWP file if we looked there, helps the user
13428 better diagnose the problem. */
791afaa2 13429 std::string dwp_text;
43942612
DE
13430
13431 if (dwp_file != NULL)
791afaa2
TT
13432 dwp_text = string_printf (" [in DWP file %s]",
13433 lbasename (dwp_file->name));
43942612 13434
9d8780f0 13435 warning (_("Could not find DWO %s %s(%s)%s referenced by %s at offset %s"
43942612
DE
13436 " [in module %s]"),
13437 kind, dwo_name, hex_string (signature),
791afaa2 13438 dwp_text.c_str (),
43942612 13439 this_unit->is_debug_types ? "TU" : "CU",
9d8780f0 13440 sect_offset_str (this_unit->sect_off), objfile_name (objfile));
43942612 13441 }
3019eac3 13442 return NULL;
5fb290d7
DJ
13443}
13444
80626a55
DE
13445/* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
13446 See lookup_dwo_cutu_unit for details. */
13447
13448static struct dwo_unit *
13449lookup_dwo_comp_unit (struct dwarf2_per_cu_data *this_cu,
13450 const char *dwo_name, const char *comp_dir,
13451 ULONGEST signature)
13452{
13453 return lookup_dwo_cutu (this_cu, dwo_name, comp_dir, signature, 0);
13454}
13455
13456/* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
13457 See lookup_dwo_cutu_unit for details. */
13458
13459static struct dwo_unit *
13460lookup_dwo_type_unit (struct signatured_type *this_tu,
13461 const char *dwo_name, const char *comp_dir)
13462{
13463 return lookup_dwo_cutu (&this_tu->per_cu, dwo_name, comp_dir, this_tu->signature, 1);
13464}
13465
89e63ee4
DE
13466/* Traversal function for queue_and_load_all_dwo_tus. */
13467
13468static int
13469queue_and_load_dwo_tu (void **slot, void *info)
13470{
13471 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
13472 struct dwarf2_per_cu_data *per_cu = (struct dwarf2_per_cu_data *) info;
13473 ULONGEST signature = dwo_unit->signature;
13474 struct signatured_type *sig_type =
13475 lookup_dwo_signatured_type (per_cu->cu, signature);
13476
13477 if (sig_type != NULL)
13478 {
13479 struct dwarf2_per_cu_data *sig_cu = &sig_type->per_cu;
13480
13481 /* We pass NULL for DEPENDENT_CU because we don't yet know if there's
13482 a real dependency of PER_CU on SIG_TYPE. That is detected later
13483 while processing PER_CU. */
13484 if (maybe_queue_comp_unit (NULL, sig_cu, per_cu->cu->language))
13485 load_full_type_unit (sig_cu);
13486 VEC_safe_push (dwarf2_per_cu_ptr, per_cu->imported_symtabs, sig_cu);
13487 }
13488
13489 return 1;
13490}
13491
13492/* Queue all TUs contained in the DWO of PER_CU to be read in.
13493 The DWO may have the only definition of the type, though it may not be
13494 referenced anywhere in PER_CU. Thus we have to load *all* its TUs.
13495 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
13496
13497static void
13498queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *per_cu)
13499{
13500 struct dwo_unit *dwo_unit;
13501 struct dwo_file *dwo_file;
13502
13503 gdb_assert (!per_cu->is_debug_types);
ed2dc618 13504 gdb_assert (get_dwp_file (per_cu->dwarf2_per_objfile) == NULL);
89e63ee4
DE
13505 gdb_assert (per_cu->cu != NULL);
13506
13507 dwo_unit = per_cu->cu->dwo_unit;
13508 gdb_assert (dwo_unit != NULL);
13509
13510 dwo_file = dwo_unit->dwo_file;
13511 if (dwo_file->tus != NULL)
13512 htab_traverse_noresize (dwo_file->tus, queue_and_load_dwo_tu, per_cu);
13513}
13514
3019eac3
DE
13515/* Free all resources associated with DWO_FILE.
13516 Close the DWO file and munmap the sections.
13517 All memory should be on the objfile obstack. */
348e048f
DE
13518
13519static void
3019eac3 13520free_dwo_file (struct dwo_file *dwo_file, struct objfile *objfile)
348e048f 13521{
348e048f 13522
5c6fa7ab 13523 /* Note: dbfd is NULL for virtual DWO files. */
80626a55 13524 gdb_bfd_unref (dwo_file->dbfd);
348e048f 13525
3019eac3
DE
13526 VEC_free (dwarf2_section_info_def, dwo_file->sections.types);
13527}
348e048f 13528
3019eac3 13529/* Wrapper for free_dwo_file for use in cleanups. */
348e048f 13530
3019eac3
DE
13531static void
13532free_dwo_file_cleanup (void *arg)
13533{
ed2dc618
SM
13534 struct free_dwo_file_cleanup_data *data
13535 = (struct free_dwo_file_cleanup_data *) arg;
13536 struct objfile *objfile = data->dwarf2_per_objfile->objfile;
348e048f 13537
ed2dc618
SM
13538 free_dwo_file (data->dwo_file, objfile);
13539
13540 xfree (data);
3019eac3 13541}
348e048f 13542
3019eac3 13543/* Traversal function for free_dwo_files. */
2ab95328 13544
3019eac3
DE
13545static int
13546free_dwo_file_from_slot (void **slot, void *info)
13547{
13548 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
13549 struct objfile *objfile = (struct objfile *) info;
348e048f 13550
3019eac3 13551 free_dwo_file (dwo_file, objfile);
348e048f 13552
3019eac3
DE
13553 return 1;
13554}
348e048f 13555
3019eac3 13556/* Free all resources associated with DWO_FILES. */
348e048f 13557
3019eac3
DE
13558static void
13559free_dwo_files (htab_t dwo_files, struct objfile *objfile)
13560{
13561 htab_traverse_noresize (dwo_files, free_dwo_file_from_slot, objfile);
348e048f 13562}
3019eac3
DE
13563\f
13564/* Read in various DIEs. */
348e048f 13565
d389af10 13566/* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
3e43a32a
MS
13567 Inherit only the children of the DW_AT_abstract_origin DIE not being
13568 already referenced by DW_AT_abstract_origin from the children of the
13569 current DIE. */
d389af10
JK
13570
13571static void
13572inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
13573{
13574 struct die_info *child_die;
791afaa2 13575 sect_offset *offsetp;
d389af10
JK
13576 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
13577 struct die_info *origin_die;
13578 /* Iterator of the ORIGIN_DIE children. */
13579 struct die_info *origin_child_die;
d389af10 13580 struct attribute *attr;
cd02d79d
PA
13581 struct dwarf2_cu *origin_cu;
13582 struct pending **origin_previous_list_in_scope;
d389af10
JK
13583
13584 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13585 if (!attr)
13586 return;
13587
cd02d79d
PA
13588 /* Note that following die references may follow to a die in a
13589 different cu. */
13590
13591 origin_cu = cu;
13592 origin_die = follow_die_ref (die, attr, &origin_cu);
13593
13594 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
13595 symbols in. */
13596 origin_previous_list_in_scope = origin_cu->list_in_scope;
13597 origin_cu->list_in_scope = cu->list_in_scope;
13598
edb3359d
DJ
13599 if (die->tag != origin_die->tag
13600 && !(die->tag == DW_TAG_inlined_subroutine
13601 && origin_die->tag == DW_TAG_subprogram))
d389af10 13602 complaint (&symfile_complaints,
9d8780f0
SM
13603 _("DIE %s and its abstract origin %s have different tags"),
13604 sect_offset_str (die->sect_off),
13605 sect_offset_str (origin_die->sect_off));
d389af10 13606
791afaa2 13607 std::vector<sect_offset> offsets;
d389af10 13608
3ea89b92
PMR
13609 for (child_die = die->child;
13610 child_die && child_die->tag;
13611 child_die = sibling_die (child_die))
13612 {
13613 struct die_info *child_origin_die;
13614 struct dwarf2_cu *child_origin_cu;
13615
13616 /* We are trying to process concrete instance entries:
216f72a1 13617 DW_TAG_call_site DIEs indeed have a DW_AT_abstract_origin tag, but
3ea89b92
PMR
13618 it's not relevant to our analysis here. i.e. detecting DIEs that are
13619 present in the abstract instance but not referenced in the concrete
13620 one. */
216f72a1
JK
13621 if (child_die->tag == DW_TAG_call_site
13622 || child_die->tag == DW_TAG_GNU_call_site)
3ea89b92
PMR
13623 continue;
13624
c38f313d
DJ
13625 /* For each CHILD_DIE, find the corresponding child of
13626 ORIGIN_DIE. If there is more than one layer of
13627 DW_AT_abstract_origin, follow them all; there shouldn't be,
13628 but GCC versions at least through 4.4 generate this (GCC PR
13629 40573). */
3ea89b92
PMR
13630 child_origin_die = child_die;
13631 child_origin_cu = cu;
c38f313d
DJ
13632 while (1)
13633 {
cd02d79d
PA
13634 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
13635 child_origin_cu);
c38f313d
DJ
13636 if (attr == NULL)
13637 break;
cd02d79d
PA
13638 child_origin_die = follow_die_ref (child_origin_die, attr,
13639 &child_origin_cu);
c38f313d
DJ
13640 }
13641
d389af10
JK
13642 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
13643 counterpart may exist. */
c38f313d 13644 if (child_origin_die != child_die)
d389af10 13645 {
edb3359d
DJ
13646 if (child_die->tag != child_origin_die->tag
13647 && !(child_die->tag == DW_TAG_inlined_subroutine
13648 && child_origin_die->tag == DW_TAG_subprogram))
d389af10 13649 complaint (&symfile_complaints,
9d8780f0 13650 _("Child DIE %s and its abstract origin %s have "
9c541725 13651 "different tags"),
9d8780f0
SM
13652 sect_offset_str (child_die->sect_off),
13653 sect_offset_str (child_origin_die->sect_off));
c38f313d
DJ
13654 if (child_origin_die->parent != origin_die)
13655 complaint (&symfile_complaints,
9d8780f0 13656 _("Child DIE %s and its abstract origin %s have "
9c541725 13657 "different parents"),
9d8780f0
SM
13658 sect_offset_str (child_die->sect_off),
13659 sect_offset_str (child_origin_die->sect_off));
c38f313d 13660 else
791afaa2 13661 offsets.push_back (child_origin_die->sect_off);
d389af10 13662 }
d389af10 13663 }
791afaa2
TT
13664 std::sort (offsets.begin (), offsets.end ());
13665 sect_offset *offsets_end = offsets.data () + offsets.size ();
13666 for (offsetp = offsets.data () + 1; offsetp < offsets_end; offsetp++)
9c541725 13667 if (offsetp[-1] == *offsetp)
3e43a32a 13668 complaint (&symfile_complaints,
9d8780f0
SM
13669 _("Multiple children of DIE %s refer "
13670 "to DIE %s as their abstract origin"),
13671 sect_offset_str (die->sect_off), sect_offset_str (*offsetp));
d389af10 13672
791afaa2 13673 offsetp = offsets.data ();
d389af10
JK
13674 origin_child_die = origin_die->child;
13675 while (origin_child_die && origin_child_die->tag)
13676 {
13677 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
b64f50a1 13678 while (offsetp < offsets_end
9c541725 13679 && *offsetp < origin_child_die->sect_off)
d389af10 13680 offsetp++;
b64f50a1 13681 if (offsetp >= offsets_end
9c541725 13682 || *offsetp > origin_child_die->sect_off)
d389af10 13683 {
adde2bff
DE
13684 /* Found that ORIGIN_CHILD_DIE is really not referenced.
13685 Check whether we're already processing ORIGIN_CHILD_DIE.
13686 This can happen with mutually referenced abstract_origins.
13687 PR 16581. */
13688 if (!origin_child_die->in_process)
13689 process_die (origin_child_die, origin_cu);
d389af10
JK
13690 }
13691 origin_child_die = sibling_die (origin_child_die);
13692 }
cd02d79d 13693 origin_cu->list_in_scope = origin_previous_list_in_scope;
d389af10
JK
13694}
13695
c906108c 13696static void
e7c27a73 13697read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 13698{
518817b3 13699 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
3e29f34a 13700 struct gdbarch *gdbarch = get_objfile_arch (objfile);
fe978cb0 13701 struct context_stack *newobj;
c906108c
SS
13702 CORE_ADDR lowpc;
13703 CORE_ADDR highpc;
13704 struct die_info *child_die;
edb3359d 13705 struct attribute *attr, *call_line, *call_file;
15d034d0 13706 const char *name;
e142c38c 13707 CORE_ADDR baseaddr;
801e3a5b 13708 struct block *block;
edb3359d 13709 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
2f4732b0 13710 std::vector<struct symbol *> template_args;
34eaf542 13711 struct template_symbol *templ_func = NULL;
edb3359d
DJ
13712
13713 if (inlined_func)
13714 {
13715 /* If we do not have call site information, we can't show the
13716 caller of this inlined function. That's too confusing, so
13717 only use the scope for local variables. */
13718 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
13719 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
13720 if (call_line == NULL || call_file == NULL)
13721 {
13722 read_lexical_block_scope (die, cu);
13723 return;
13724 }
13725 }
c906108c 13726
e142c38c
DJ
13727 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
13728
94af9270 13729 name = dwarf2_name (die, cu);
c906108c 13730
e8d05480
JB
13731 /* Ignore functions with missing or empty names. These are actually
13732 illegal according to the DWARF standard. */
13733 if (name == NULL)
13734 {
13735 complaint (&symfile_complaints,
9d8780f0
SM
13736 _("missing name for subprogram DIE at %s"),
13737 sect_offset_str (die->sect_off));
e8d05480
JB
13738 return;
13739 }
13740
13741 /* Ignore functions with missing or invalid low and high pc attributes. */
3a2b436a 13742 if (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL)
e385593e 13743 <= PC_BOUNDS_INVALID)
e8d05480 13744 {
ae4d0c03
PM
13745 attr = dwarf2_attr (die, DW_AT_external, cu);
13746 if (!attr || !DW_UNSND (attr))
13747 complaint (&symfile_complaints,
3e43a32a 13748 _("cannot get low and high bounds "
9d8780f0
SM
13749 "for subprogram DIE at %s"),
13750 sect_offset_str (die->sect_off));
e8d05480
JB
13751 return;
13752 }
c906108c 13753
3e29f34a
MR
13754 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13755 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
c906108c 13756
34eaf542
TT
13757 /* If we have any template arguments, then we must allocate a
13758 different sort of symbol. */
13759 for (child_die = die->child; child_die; child_die = sibling_die (child_die))
13760 {
13761 if (child_die->tag == DW_TAG_template_type_param
13762 || child_die->tag == DW_TAG_template_value_param)
13763 {
e623cf5d 13764 templ_func = allocate_template_symbol (objfile);
cf724bc9 13765 templ_func->subclass = SYMBOL_TEMPLATE;
34eaf542
TT
13766 break;
13767 }
13768 }
13769
fe978cb0 13770 newobj = push_context (0, lowpc);
5e2db402
TT
13771 newobj->name = new_symbol (die, read_type_die (die, cu), cu,
13772 (struct symbol *) templ_func);
4c2df51b 13773
4cecd739
DJ
13774 /* If there is a location expression for DW_AT_frame_base, record
13775 it. */
e142c38c 13776 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
4c2df51b 13777 if (attr)
fe978cb0 13778 dwarf2_symbol_mark_computed (attr, newobj->name, cu, 1);
4c2df51b 13779
63e43d3a
PMR
13780 /* If there is a location for the static link, record it. */
13781 newobj->static_link = NULL;
13782 attr = dwarf2_attr (die, DW_AT_static_link, cu);
13783 if (attr)
13784 {
224c3ddb
SM
13785 newobj->static_link
13786 = XOBNEW (&objfile->objfile_obstack, struct dynamic_prop);
63e43d3a
PMR
13787 attr_to_dynamic_prop (attr, die, cu, newobj->static_link);
13788 }
13789
e142c38c 13790 cu->list_in_scope = &local_symbols;
c906108c 13791
639d11d3 13792 if (die->child != NULL)
c906108c 13793 {
639d11d3 13794 child_die = die->child;
c906108c
SS
13795 while (child_die && child_die->tag)
13796 {
34eaf542
TT
13797 if (child_die->tag == DW_TAG_template_type_param
13798 || child_die->tag == DW_TAG_template_value_param)
13799 {
13800 struct symbol *arg = new_symbol (child_die, NULL, cu);
13801
f1078f66 13802 if (arg != NULL)
2f4732b0 13803 template_args.push_back (arg);
34eaf542
TT
13804 }
13805 else
13806 process_die (child_die, cu);
c906108c
SS
13807 child_die = sibling_die (child_die);
13808 }
13809 }
13810
d389af10
JK
13811 inherit_abstract_dies (die, cu);
13812
4a811a97
UW
13813 /* If we have a DW_AT_specification, we might need to import using
13814 directives from the context of the specification DIE. See the
13815 comment in determine_prefix. */
13816 if (cu->language == language_cplus
13817 && dwarf2_attr (die, DW_AT_specification, cu))
13818 {
13819 struct dwarf2_cu *spec_cu = cu;
13820 struct die_info *spec_die = die_specification (die, &spec_cu);
13821
13822 while (spec_die)
13823 {
13824 child_die = spec_die->child;
13825 while (child_die && child_die->tag)
13826 {
13827 if (child_die->tag == DW_TAG_imported_module)
13828 process_die (child_die, spec_cu);
13829 child_die = sibling_die (child_die);
13830 }
13831
13832 /* In some cases, GCC generates specification DIEs that
13833 themselves contain DW_AT_specification attributes. */
13834 spec_die = die_specification (spec_die, &spec_cu);
13835 }
13836 }
13837
fe978cb0 13838 newobj = pop_context ();
c906108c 13839 /* Make a block for the local symbols within. */
fe978cb0 13840 block = finish_block (newobj->name, &local_symbols, newobj->old_blocks,
63e43d3a 13841 newobj->static_link, lowpc, highpc);
801e3a5b 13842
df8a16a1 13843 /* For C++, set the block's scope. */
45280282
IB
13844 if ((cu->language == language_cplus
13845 || cu->language == language_fortran
c44af4eb
TT
13846 || cu->language == language_d
13847 || cu->language == language_rust)
4d4ec4e5 13848 && cu->processing_has_namespace_info)
195a3f6c
TT
13849 block_set_scope (block, determine_prefix (die, cu),
13850 &objfile->objfile_obstack);
df8a16a1 13851
801e3a5b
JB
13852 /* If we have address ranges, record them. */
13853 dwarf2_record_block_ranges (die, block, baseaddr, cu);
6e70227d 13854
fe978cb0 13855 gdbarch_make_symbol_special (gdbarch, newobj->name, objfile);
3e29f34a 13856
34eaf542 13857 /* Attach template arguments to function. */
2f4732b0 13858 if (!template_args.empty ())
34eaf542
TT
13859 {
13860 gdb_assert (templ_func != NULL);
13861
2f4732b0 13862 templ_func->n_template_arguments = template_args.size ();
34eaf542 13863 templ_func->template_arguments
8d749320
SM
13864 = XOBNEWVEC (&objfile->objfile_obstack, struct symbol *,
13865 templ_func->n_template_arguments);
34eaf542 13866 memcpy (templ_func->template_arguments,
2f4732b0 13867 template_args.data (),
34eaf542 13868 (templ_func->n_template_arguments * sizeof (struct symbol *)));
34eaf542
TT
13869 }
13870
208d8187
JB
13871 /* In C++, we can have functions nested inside functions (e.g., when
13872 a function declares a class that has methods). This means that
13873 when we finish processing a function scope, we may need to go
13874 back to building a containing block's symbol lists. */
fe978cb0 13875 local_symbols = newobj->locals;
22cee43f 13876 local_using_directives = newobj->local_using_directives;
208d8187 13877
921e78cf
JB
13878 /* If we've finished processing a top-level function, subsequent
13879 symbols go in the file symbol list. */
13880 if (outermost_context_p ())
e142c38c 13881 cu->list_in_scope = &file_symbols;
c906108c
SS
13882}
13883
13884/* Process all the DIES contained within a lexical block scope. Start
13885 a new scope, process the dies, and then close the scope. */
13886
13887static void
e7c27a73 13888read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 13889{
518817b3 13890 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
3e29f34a 13891 struct gdbarch *gdbarch = get_objfile_arch (objfile);
fe978cb0 13892 struct context_stack *newobj;
c906108c
SS
13893 CORE_ADDR lowpc, highpc;
13894 struct die_info *child_die;
e142c38c
DJ
13895 CORE_ADDR baseaddr;
13896
13897 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c
SS
13898
13899 /* Ignore blocks with missing or invalid low and high pc attributes. */
af34e669
DJ
13900 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
13901 as multiple lexical blocks? Handling children in a sane way would
6e70227d 13902 be nasty. Might be easier to properly extend generic blocks to
af34e669 13903 describe ranges. */
e385593e
JK
13904 switch (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
13905 {
13906 case PC_BOUNDS_NOT_PRESENT:
13907 /* DW_TAG_lexical_block has no attributes, process its children as if
13908 there was no wrapping by that DW_TAG_lexical_block.
13909 GCC does no longer produces such DWARF since GCC r224161. */
13910 for (child_die = die->child;
13911 child_die != NULL && child_die->tag;
13912 child_die = sibling_die (child_die))
13913 process_die (child_die, cu);
13914 return;
13915 case PC_BOUNDS_INVALID:
13916 return;
13917 }
3e29f34a
MR
13918 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13919 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
c906108c
SS
13920
13921 push_context (0, lowpc);
639d11d3 13922 if (die->child != NULL)
c906108c 13923 {
639d11d3 13924 child_die = die->child;
c906108c
SS
13925 while (child_die && child_die->tag)
13926 {
e7c27a73 13927 process_die (child_die, cu);
c906108c
SS
13928 child_die = sibling_die (child_die);
13929 }
13930 }
3ea89b92 13931 inherit_abstract_dies (die, cu);
fe978cb0 13932 newobj = pop_context ();
c906108c 13933
22cee43f 13934 if (local_symbols != NULL || local_using_directives != NULL)
c906108c 13935 {
801e3a5b 13936 struct block *block
63e43d3a 13937 = finish_block (0, &local_symbols, newobj->old_blocks, NULL,
fe978cb0 13938 newobj->start_addr, highpc);
801e3a5b
JB
13939
13940 /* Note that recording ranges after traversing children, as we
13941 do here, means that recording a parent's ranges entails
13942 walking across all its children's ranges as they appear in
13943 the address map, which is quadratic behavior.
13944
13945 It would be nicer to record the parent's ranges before
13946 traversing its children, simply overriding whatever you find
13947 there. But since we don't even decide whether to create a
13948 block until after we've traversed its children, that's hard
13949 to do. */
13950 dwarf2_record_block_ranges (die, block, baseaddr, cu);
c906108c 13951 }
fe978cb0 13952 local_symbols = newobj->locals;
22cee43f 13953 local_using_directives = newobj->local_using_directives;
c906108c
SS
13954}
13955
216f72a1 13956/* Read in DW_TAG_call_site and insert it to CU->call_site_htab. */
96408a79
SA
13957
13958static void
13959read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
13960{
518817b3 13961 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
96408a79
SA
13962 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13963 CORE_ADDR pc, baseaddr;
13964 struct attribute *attr;
13965 struct call_site *call_site, call_site_local;
13966 void **slot;
13967 int nparams;
13968 struct die_info *child_die;
13969
13970 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
13971
216f72a1
JK
13972 attr = dwarf2_attr (die, DW_AT_call_return_pc, cu);
13973 if (attr == NULL)
13974 {
13975 /* This was a pre-DWARF-5 GNU extension alias
13976 for DW_AT_call_return_pc. */
13977 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
13978 }
96408a79
SA
13979 if (!attr)
13980 {
13981 complaint (&symfile_complaints,
216f72a1 13982 _("missing DW_AT_call_return_pc for DW_TAG_call_site "
9d8780f0
SM
13983 "DIE %s [in module %s]"),
13984 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79
SA
13985 return;
13986 }
31aa7e4e 13987 pc = attr_value_as_address (attr) + baseaddr;
3e29f34a 13988 pc = gdbarch_adjust_dwarf2_addr (gdbarch, pc);
96408a79
SA
13989
13990 if (cu->call_site_htab == NULL)
13991 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
13992 NULL, &objfile->objfile_obstack,
13993 hashtab_obstack_allocate, NULL);
13994 call_site_local.pc = pc;
13995 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
13996 if (*slot != NULL)
13997 {
13998 complaint (&symfile_complaints,
216f72a1 13999 _("Duplicate PC %s for DW_TAG_call_site "
9d8780f0
SM
14000 "DIE %s [in module %s]"),
14001 paddress (gdbarch, pc), sect_offset_str (die->sect_off),
4262abfb 14002 objfile_name (objfile));
96408a79
SA
14003 return;
14004 }
14005
14006 /* Count parameters at the caller. */
14007
14008 nparams = 0;
14009 for (child_die = die->child; child_die && child_die->tag;
14010 child_die = sibling_die (child_die))
14011 {
216f72a1
JK
14012 if (child_die->tag != DW_TAG_call_site_parameter
14013 && child_die->tag != DW_TAG_GNU_call_site_parameter)
96408a79
SA
14014 {
14015 complaint (&symfile_complaints,
216f72a1 14016 _("Tag %d is not DW_TAG_call_site_parameter in "
9d8780f0
SM
14017 "DW_TAG_call_site child DIE %s [in module %s]"),
14018 child_die->tag, sect_offset_str (child_die->sect_off),
4262abfb 14019 objfile_name (objfile));
96408a79
SA
14020 continue;
14021 }
14022
14023 nparams++;
14024 }
14025
224c3ddb
SM
14026 call_site
14027 = ((struct call_site *)
14028 obstack_alloc (&objfile->objfile_obstack,
14029 sizeof (*call_site)
14030 + (sizeof (*call_site->parameter) * (nparams - 1))));
96408a79
SA
14031 *slot = call_site;
14032 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
14033 call_site->pc = pc;
14034
216f72a1
JK
14035 if (dwarf2_flag_true_p (die, DW_AT_call_tail_call, cu)
14036 || dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
96408a79
SA
14037 {
14038 struct die_info *func_die;
14039
14040 /* Skip also over DW_TAG_inlined_subroutine. */
14041 for (func_die = die->parent;
14042 func_die && func_die->tag != DW_TAG_subprogram
14043 && func_die->tag != DW_TAG_subroutine_type;
14044 func_die = func_die->parent);
14045
216f72a1
JK
14046 /* DW_AT_call_all_calls is a superset
14047 of DW_AT_call_all_tail_calls. */
96408a79 14048 if (func_die
216f72a1 14049 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_calls, cu)
96408a79 14050 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
216f72a1 14051 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_tail_calls, cu)
96408a79
SA
14052 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
14053 {
14054 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
14055 not complete. But keep CALL_SITE for look ups via call_site_htab,
14056 both the initial caller containing the real return address PC and
14057 the final callee containing the current PC of a chain of tail
14058 calls do not need to have the tail call list complete. But any
14059 function candidate for a virtual tail call frame searched via
14060 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
14061 determined unambiguously. */
14062 }
14063 else
14064 {
14065 struct type *func_type = NULL;
14066
14067 if (func_die)
14068 func_type = get_die_type (func_die, cu);
14069 if (func_type != NULL)
14070 {
14071 gdb_assert (TYPE_CODE (func_type) == TYPE_CODE_FUNC);
14072
14073 /* Enlist this call site to the function. */
14074 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
14075 TYPE_TAIL_CALL_LIST (func_type) = call_site;
14076 }
14077 else
14078 complaint (&symfile_complaints,
216f72a1 14079 _("Cannot find function owning DW_TAG_call_site "
9d8780f0
SM
14080 "DIE %s [in module %s]"),
14081 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79
SA
14082 }
14083 }
14084
216f72a1
JK
14085 attr = dwarf2_attr (die, DW_AT_call_target, cu);
14086 if (attr == NULL)
14087 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
14088 if (attr == NULL)
14089 attr = dwarf2_attr (die, DW_AT_call_origin, cu);
96408a79 14090 if (attr == NULL)
216f72a1
JK
14091 {
14092 /* This was a pre-DWARF-5 GNU extension alias for DW_AT_call_origin. */
14093 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
14094 }
96408a79
SA
14095 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
14096 if (!attr || (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0))
14097 /* Keep NULL DWARF_BLOCK. */;
14098 else if (attr_form_is_block (attr))
14099 {
14100 struct dwarf2_locexpr_baton *dlbaton;
14101
8d749320 14102 dlbaton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
96408a79
SA
14103 dlbaton->data = DW_BLOCK (attr)->data;
14104 dlbaton->size = DW_BLOCK (attr)->size;
14105 dlbaton->per_cu = cu->per_cu;
14106
14107 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
14108 }
7771576e 14109 else if (attr_form_is_ref (attr))
96408a79 14110 {
96408a79
SA
14111 struct dwarf2_cu *target_cu = cu;
14112 struct die_info *target_die;
14113
ac9ec31b 14114 target_die = follow_die_ref (die, attr, &target_cu);
518817b3 14115 gdb_assert (target_cu->per_cu->dwarf2_per_objfile->objfile == objfile);
96408a79
SA
14116 if (die_is_declaration (target_die, target_cu))
14117 {
7d45c7c3 14118 const char *target_physname;
9112db09
JK
14119
14120 /* Prefer the mangled name; otherwise compute the demangled one. */
73b9be8b 14121 target_physname = dw2_linkage_name (target_die, target_cu);
7d45c7c3 14122 if (target_physname == NULL)
9112db09 14123 target_physname = dwarf2_physname (NULL, target_die, target_cu);
96408a79
SA
14124 if (target_physname == NULL)
14125 complaint (&symfile_complaints,
216f72a1 14126 _("DW_AT_call_target target DIE has invalid "
9d8780f0
SM
14127 "physname, for referencing DIE %s [in module %s]"),
14128 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79 14129 else
7d455152 14130 SET_FIELD_PHYSNAME (call_site->target, target_physname);
96408a79
SA
14131 }
14132 else
14133 {
14134 CORE_ADDR lowpc;
14135
14136 /* DW_AT_entry_pc should be preferred. */
3a2b436a 14137 if (dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL)
e385593e 14138 <= PC_BOUNDS_INVALID)
96408a79 14139 complaint (&symfile_complaints,
216f72a1 14140 _("DW_AT_call_target target DIE has invalid "
9d8780f0
SM
14141 "low pc, for referencing DIE %s [in module %s]"),
14142 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79 14143 else
3e29f34a
MR
14144 {
14145 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
14146 SET_FIELD_PHYSADDR (call_site->target, lowpc);
14147 }
96408a79
SA
14148 }
14149 }
14150 else
14151 complaint (&symfile_complaints,
216f72a1 14152 _("DW_TAG_call_site DW_AT_call_target is neither "
9d8780f0
SM
14153 "block nor reference, for DIE %s [in module %s]"),
14154 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79
SA
14155
14156 call_site->per_cu = cu->per_cu;
14157
14158 for (child_die = die->child;
14159 child_die && child_die->tag;
14160 child_die = sibling_die (child_die))
14161 {
96408a79 14162 struct call_site_parameter *parameter;
1788b2d3 14163 struct attribute *loc, *origin;
96408a79 14164
216f72a1
JK
14165 if (child_die->tag != DW_TAG_call_site_parameter
14166 && child_die->tag != DW_TAG_GNU_call_site_parameter)
96408a79
SA
14167 {
14168 /* Already printed the complaint above. */
14169 continue;
14170 }
14171
14172 gdb_assert (call_site->parameter_count < nparams);
14173 parameter = &call_site->parameter[call_site->parameter_count];
14174
1788b2d3
JK
14175 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
14176 specifies DW_TAG_formal_parameter. Value of the data assumed for the
216f72a1 14177 register is contained in DW_AT_call_value. */
96408a79 14178
24c5c679 14179 loc = dwarf2_attr (child_die, DW_AT_location, cu);
216f72a1
JK
14180 origin = dwarf2_attr (child_die, DW_AT_call_parameter, cu);
14181 if (origin == NULL)
14182 {
14183 /* This was a pre-DWARF-5 GNU extension alias
14184 for DW_AT_call_parameter. */
14185 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
14186 }
7771576e 14187 if (loc == NULL && origin != NULL && attr_form_is_ref (origin))
1788b2d3 14188 {
1788b2d3 14189 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
9c541725
PA
14190
14191 sect_offset sect_off
14192 = (sect_offset) dwarf2_get_ref_die_offset (origin);
14193 if (!offset_in_cu_p (&cu->header, sect_off))
d76b7dbc
JK
14194 {
14195 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
14196 binding can be done only inside one CU. Such referenced DIE
14197 therefore cannot be even moved to DW_TAG_partial_unit. */
14198 complaint (&symfile_complaints,
216f72a1 14199 _("DW_AT_call_parameter offset is not in CU for "
9d8780f0
SM
14200 "DW_TAG_call_site child DIE %s [in module %s]"),
14201 sect_offset_str (child_die->sect_off),
9c541725 14202 objfile_name (objfile));
d76b7dbc
JK
14203 continue;
14204 }
9c541725
PA
14205 parameter->u.param_cu_off
14206 = (cu_offset) (sect_off - cu->header.sect_off);
1788b2d3
JK
14207 }
14208 else if (loc == NULL || origin != NULL || !attr_form_is_block (loc))
96408a79
SA
14209 {
14210 complaint (&symfile_complaints,
14211 _("No DW_FORM_block* DW_AT_location for "
9d8780f0
SM
14212 "DW_TAG_call_site child DIE %s [in module %s]"),
14213 sect_offset_str (child_die->sect_off), objfile_name (objfile));
96408a79
SA
14214 continue;
14215 }
24c5c679 14216 else
96408a79 14217 {
24c5c679
JK
14218 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
14219 (DW_BLOCK (loc)->data, &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size]);
14220 if (parameter->u.dwarf_reg != -1)
14221 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
14222 else if (dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (loc)->data,
14223 &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size],
14224 &parameter->u.fb_offset))
14225 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
14226 else
14227 {
14228 complaint (&symfile_complaints,
14229 _("Only single DW_OP_reg or DW_OP_fbreg is supported "
14230 "for DW_FORM_block* DW_AT_location is supported for "
9d8780f0 14231 "DW_TAG_call_site child DIE %s "
24c5c679 14232 "[in module %s]"),
9d8780f0 14233 sect_offset_str (child_die->sect_off),
9c541725 14234 objfile_name (objfile));
24c5c679
JK
14235 continue;
14236 }
96408a79
SA
14237 }
14238
216f72a1
JK
14239 attr = dwarf2_attr (child_die, DW_AT_call_value, cu);
14240 if (attr == NULL)
14241 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
96408a79
SA
14242 if (!attr_form_is_block (attr))
14243 {
14244 complaint (&symfile_complaints,
216f72a1 14245 _("No DW_FORM_block* DW_AT_call_value for "
9d8780f0
SM
14246 "DW_TAG_call_site child DIE %s [in module %s]"),
14247 sect_offset_str (child_die->sect_off),
9c541725 14248 objfile_name (objfile));
96408a79
SA
14249 continue;
14250 }
14251 parameter->value = DW_BLOCK (attr)->data;
14252 parameter->value_size = DW_BLOCK (attr)->size;
14253
14254 /* Parameters are not pre-cleared by memset above. */
14255 parameter->data_value = NULL;
14256 parameter->data_value_size = 0;
14257 call_site->parameter_count++;
14258
216f72a1
JK
14259 attr = dwarf2_attr (child_die, DW_AT_call_data_value, cu);
14260 if (attr == NULL)
14261 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
96408a79
SA
14262 if (attr)
14263 {
14264 if (!attr_form_is_block (attr))
14265 complaint (&symfile_complaints,
216f72a1 14266 _("No DW_FORM_block* DW_AT_call_data_value for "
9d8780f0
SM
14267 "DW_TAG_call_site child DIE %s [in module %s]"),
14268 sect_offset_str (child_die->sect_off),
9c541725 14269 objfile_name (objfile));
96408a79
SA
14270 else
14271 {
14272 parameter->data_value = DW_BLOCK (attr)->data;
14273 parameter->data_value_size = DW_BLOCK (attr)->size;
14274 }
14275 }
14276 }
14277}
14278
71a3c369
TT
14279/* Helper function for read_variable. If DIE represents a virtual
14280 table, then return the type of the concrete object that is
14281 associated with the virtual table. Otherwise, return NULL. */
14282
14283static struct type *
14284rust_containing_type (struct die_info *die, struct dwarf2_cu *cu)
14285{
14286 struct attribute *attr = dwarf2_attr (die, DW_AT_type, cu);
14287 if (attr == NULL)
14288 return NULL;
14289
14290 /* Find the type DIE. */
14291 struct die_info *type_die = NULL;
14292 struct dwarf2_cu *type_cu = cu;
14293
14294 if (attr_form_is_ref (attr))
14295 type_die = follow_die_ref (die, attr, &type_cu);
14296 if (type_die == NULL)
14297 return NULL;
14298
14299 if (dwarf2_attr (type_die, DW_AT_containing_type, type_cu) == NULL)
14300 return NULL;
14301 return die_containing_type (type_die, type_cu);
14302}
14303
14304/* Read a variable (DW_TAG_variable) DIE and create a new symbol. */
14305
14306static void
14307read_variable (struct die_info *die, struct dwarf2_cu *cu)
14308{
14309 struct rust_vtable_symbol *storage = NULL;
14310
14311 if (cu->language == language_rust)
14312 {
14313 struct type *containing_type = rust_containing_type (die, cu);
14314
14315 if (containing_type != NULL)
14316 {
518817b3 14317 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
71a3c369
TT
14318
14319 storage = OBSTACK_ZALLOC (&objfile->objfile_obstack,
14320 struct rust_vtable_symbol);
14321 initialize_objfile_symbol (storage);
14322 storage->concrete_type = containing_type;
cf724bc9 14323 storage->subclass = SYMBOL_RUST_VTABLE;
71a3c369
TT
14324 }
14325 }
14326
5e2db402 14327 new_symbol (die, NULL, cu, storage);
71a3c369
TT
14328}
14329
43988095
JK
14330/* Call CALLBACK from DW_AT_ranges attribute value OFFSET
14331 reading .debug_rnglists.
14332 Callback's type should be:
14333 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
14334 Return true if the attributes are present and valid, otherwise,
14335 return false. */
14336
14337template <typename Callback>
14338static bool
14339dwarf2_rnglists_process (unsigned offset, struct dwarf2_cu *cu,
14340 Callback &&callback)
14341{
ed2dc618 14342 struct dwarf2_per_objfile *dwarf2_per_objfile
518817b3 14343 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 14344 struct objfile *objfile = dwarf2_per_objfile->objfile;
43988095 14345 bfd *obfd = objfile->obfd;
43988095
JK
14346 /* Base address selection entry. */
14347 CORE_ADDR base;
14348 int found_base;
43988095 14349 const gdb_byte *buffer;
43988095
JK
14350 CORE_ADDR baseaddr;
14351 bool overflow = false;
14352
14353 found_base = cu->base_known;
14354 base = cu->base_address;
14355
14356 dwarf2_read_section (objfile, &dwarf2_per_objfile->rnglists);
14357 if (offset >= dwarf2_per_objfile->rnglists.size)
14358 {
14359 complaint (&symfile_complaints,
14360 _("Offset %d out of bounds for DW_AT_ranges attribute"),
14361 offset);
14362 return false;
14363 }
14364 buffer = dwarf2_per_objfile->rnglists.buffer + offset;
14365
14366 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
14367
14368 while (1)
14369 {
7814882a
JK
14370 /* Initialize it due to a false compiler warning. */
14371 CORE_ADDR range_beginning = 0, range_end = 0;
43988095
JK
14372 const gdb_byte *buf_end = (dwarf2_per_objfile->rnglists.buffer
14373 + dwarf2_per_objfile->rnglists.size);
14374 unsigned int bytes_read;
14375
14376 if (buffer == buf_end)
14377 {
14378 overflow = true;
14379 break;
14380 }
14381 const auto rlet = static_cast<enum dwarf_range_list_entry>(*buffer++);
14382 switch (rlet)
14383 {
14384 case DW_RLE_end_of_list:
14385 break;
14386 case DW_RLE_base_address:
14387 if (buffer + cu->header.addr_size > buf_end)
14388 {
14389 overflow = true;
14390 break;
14391 }
14392 base = read_address (obfd, buffer, cu, &bytes_read);
14393 found_base = 1;
14394 buffer += bytes_read;
14395 break;
14396 case DW_RLE_start_length:
14397 if (buffer + cu->header.addr_size > buf_end)
14398 {
14399 overflow = true;
14400 break;
14401 }
14402 range_beginning = read_address (obfd, buffer, cu, &bytes_read);
14403 buffer += bytes_read;
14404 range_end = (range_beginning
14405 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
14406 buffer += bytes_read;
14407 if (buffer > buf_end)
14408 {
14409 overflow = true;
14410 break;
14411 }
14412 break;
14413 case DW_RLE_offset_pair:
14414 range_beginning = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14415 buffer += bytes_read;
14416 if (buffer > buf_end)
14417 {
14418 overflow = true;
14419 break;
14420 }
14421 range_end = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14422 buffer += bytes_read;
14423 if (buffer > buf_end)
14424 {
14425 overflow = true;
14426 break;
14427 }
14428 break;
14429 case DW_RLE_start_end:
14430 if (buffer + 2 * cu->header.addr_size > buf_end)
14431 {
14432 overflow = true;
14433 break;
14434 }
14435 range_beginning = read_address (obfd, buffer, cu, &bytes_read);
14436 buffer += bytes_read;
14437 range_end = read_address (obfd, buffer, cu, &bytes_read);
14438 buffer += bytes_read;
14439 break;
14440 default:
14441 complaint (&symfile_complaints,
14442 _("Invalid .debug_rnglists data (no base address)"));
14443 return false;
14444 }
14445 if (rlet == DW_RLE_end_of_list || overflow)
14446 break;
14447 if (rlet == DW_RLE_base_address)
14448 continue;
14449
14450 if (!found_base)
14451 {
14452 /* We have no valid base address for the ranges
14453 data. */
14454 complaint (&symfile_complaints,
14455 _("Invalid .debug_rnglists data (no base address)"));
14456 return false;
14457 }
14458
14459 if (range_beginning > range_end)
14460 {
14461 /* Inverted range entries are invalid. */
14462 complaint (&symfile_complaints,
14463 _("Invalid .debug_rnglists data (inverted range)"));
14464 return false;
14465 }
14466
14467 /* Empty range entries have no effect. */
14468 if (range_beginning == range_end)
14469 continue;
14470
14471 range_beginning += base;
14472 range_end += base;
14473
14474 /* A not-uncommon case of bad debug info.
14475 Don't pollute the addrmap with bad data. */
14476 if (range_beginning + baseaddr == 0
14477 && !dwarf2_per_objfile->has_section_at_zero)
14478 {
14479 complaint (&symfile_complaints,
14480 _(".debug_rnglists entry has start address of zero"
14481 " [in module %s]"), objfile_name (objfile));
14482 continue;
14483 }
14484
14485 callback (range_beginning, range_end);
14486 }
14487
14488 if (overflow)
14489 {
14490 complaint (&symfile_complaints,
14491 _("Offset %d is not terminated "
14492 "for DW_AT_ranges attribute"),
14493 offset);
14494 return false;
14495 }
14496
14497 return true;
14498}
14499
14500/* Call CALLBACK from DW_AT_ranges attribute value OFFSET reading .debug_ranges.
14501 Callback's type should be:
14502 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
5f46c5a5 14503 Return 1 if the attributes are present and valid, otherwise, return 0. */
43039443 14504
43988095 14505template <typename Callback>
43039443 14506static int
5f46c5a5 14507dwarf2_ranges_process (unsigned offset, struct dwarf2_cu *cu,
43988095 14508 Callback &&callback)
43039443 14509{
ed2dc618 14510 struct dwarf2_per_objfile *dwarf2_per_objfile
518817b3 14511 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 14512 struct objfile *objfile = dwarf2_per_objfile->objfile;
43039443
JK
14513 struct comp_unit_head *cu_header = &cu->header;
14514 bfd *obfd = objfile->obfd;
14515 unsigned int addr_size = cu_header->addr_size;
14516 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
14517 /* Base address selection entry. */
14518 CORE_ADDR base;
14519 int found_base;
14520 unsigned int dummy;
d521ce57 14521 const gdb_byte *buffer;
ff013f42 14522 CORE_ADDR baseaddr;
43039443 14523
43988095
JK
14524 if (cu_header->version >= 5)
14525 return dwarf2_rnglists_process (offset, cu, callback);
14526
d00adf39
DE
14527 found_base = cu->base_known;
14528 base = cu->base_address;
43039443 14529
be391dca 14530 dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
dce234bc 14531 if (offset >= dwarf2_per_objfile->ranges.size)
43039443
JK
14532 {
14533 complaint (&symfile_complaints,
14534 _("Offset %d out of bounds for DW_AT_ranges attribute"),
14535 offset);
14536 return 0;
14537 }
dce234bc 14538 buffer = dwarf2_per_objfile->ranges.buffer + offset;
43039443 14539
e7030f15 14540 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
ff013f42 14541
43039443
JK
14542 while (1)
14543 {
14544 CORE_ADDR range_beginning, range_end;
14545
14546 range_beginning = read_address (obfd, buffer, cu, &dummy);
14547 buffer += addr_size;
14548 range_end = read_address (obfd, buffer, cu, &dummy);
14549 buffer += addr_size;
14550 offset += 2 * addr_size;
14551
14552 /* An end of list marker is a pair of zero addresses. */
14553 if (range_beginning == 0 && range_end == 0)
14554 /* Found the end of list entry. */
14555 break;
14556
14557 /* Each base address selection entry is a pair of 2 values.
14558 The first is the largest possible address, the second is
14559 the base address. Check for a base address here. */
14560 if ((range_beginning & mask) == mask)
14561 {
28d2bfb9
AB
14562 /* If we found the largest possible address, then we already
14563 have the base address in range_end. */
14564 base = range_end;
43039443
JK
14565 found_base = 1;
14566 continue;
14567 }
14568
14569 if (!found_base)
14570 {
14571 /* We have no valid base address for the ranges
14572 data. */
14573 complaint (&symfile_complaints,
14574 _("Invalid .debug_ranges data (no base address)"));
14575 return 0;
14576 }
14577
9277c30c
UW
14578 if (range_beginning > range_end)
14579 {
14580 /* Inverted range entries are invalid. */
14581 complaint (&symfile_complaints,
14582 _("Invalid .debug_ranges data (inverted range)"));
14583 return 0;
14584 }
14585
14586 /* Empty range entries have no effect. */
14587 if (range_beginning == range_end)
14588 continue;
14589
43039443
JK
14590 range_beginning += base;
14591 range_end += base;
14592
01093045
DE
14593 /* A not-uncommon case of bad debug info.
14594 Don't pollute the addrmap with bad data. */
14595 if (range_beginning + baseaddr == 0
14596 && !dwarf2_per_objfile->has_section_at_zero)
14597 {
14598 complaint (&symfile_complaints,
14599 _(".debug_ranges entry has start address of zero"
4262abfb 14600 " [in module %s]"), objfile_name (objfile));
01093045
DE
14601 continue;
14602 }
14603
5f46c5a5
JK
14604 callback (range_beginning, range_end);
14605 }
14606
14607 return 1;
14608}
14609
14610/* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
14611 Return 1 if the attributes are present and valid, otherwise, return 0.
14612 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
14613
14614static int
14615dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
14616 CORE_ADDR *high_return, struct dwarf2_cu *cu,
14617 struct partial_symtab *ranges_pst)
14618{
518817b3 14619 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
5f46c5a5
JK
14620 struct gdbarch *gdbarch = get_objfile_arch (objfile);
14621 const CORE_ADDR baseaddr = ANOFFSET (objfile->section_offsets,
14622 SECT_OFF_TEXT (objfile));
14623 int low_set = 0;
14624 CORE_ADDR low = 0;
14625 CORE_ADDR high = 0;
14626 int retval;
14627
14628 retval = dwarf2_ranges_process (offset, cu,
14629 [&] (CORE_ADDR range_beginning, CORE_ADDR range_end)
14630 {
9277c30c 14631 if (ranges_pst != NULL)
3e29f34a
MR
14632 {
14633 CORE_ADDR lowpc;
14634 CORE_ADDR highpc;
14635
14636 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch,
14637 range_beginning + baseaddr);
14638 highpc = gdbarch_adjust_dwarf2_addr (gdbarch,
14639 range_end + baseaddr);
14640 addrmap_set_empty (objfile->psymtabs_addrmap, lowpc, highpc - 1,
14641 ranges_pst);
14642 }
ff013f42 14643
43039443
JK
14644 /* FIXME: This is recording everything as a low-high
14645 segment of consecutive addresses. We should have a
14646 data structure for discontiguous block ranges
14647 instead. */
14648 if (! low_set)
14649 {
14650 low = range_beginning;
14651 high = range_end;
14652 low_set = 1;
14653 }
14654 else
14655 {
14656 if (range_beginning < low)
14657 low = range_beginning;
14658 if (range_end > high)
14659 high = range_end;
14660 }
5f46c5a5
JK
14661 });
14662 if (!retval)
14663 return 0;
43039443
JK
14664
14665 if (! low_set)
14666 /* If the first entry is an end-of-list marker, the range
14667 describes an empty scope, i.e. no instructions. */
14668 return 0;
14669
14670 if (low_return)
14671 *low_return = low;
14672 if (high_return)
14673 *high_return = high;
14674 return 1;
14675}
14676
3a2b436a
JK
14677/* Get low and high pc attributes from a die. See enum pc_bounds_kind
14678 definition for the return value. *LOWPC and *HIGHPC are set iff
e385593e 14679 neither PC_BOUNDS_NOT_PRESENT nor PC_BOUNDS_INVALID are returned. */
380bca97 14680
3a2b436a 14681static enum pc_bounds_kind
af34e669 14682dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
d85a05f0
DJ
14683 CORE_ADDR *highpc, struct dwarf2_cu *cu,
14684 struct partial_symtab *pst)
c906108c 14685{
518817b3
SM
14686 struct dwarf2_per_objfile *dwarf2_per_objfile
14687 = cu->per_cu->dwarf2_per_objfile;
c906108c 14688 struct attribute *attr;
91da1414 14689 struct attribute *attr_high;
af34e669
DJ
14690 CORE_ADDR low = 0;
14691 CORE_ADDR high = 0;
e385593e 14692 enum pc_bounds_kind ret;
c906108c 14693
91da1414
MW
14694 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14695 if (attr_high)
af34e669 14696 {
e142c38c 14697 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
af34e669 14698 if (attr)
91da1414 14699 {
31aa7e4e
JB
14700 low = attr_value_as_address (attr);
14701 high = attr_value_as_address (attr_high);
14702 if (cu->header.version >= 4 && attr_form_is_constant (attr_high))
14703 high += low;
91da1414 14704 }
af34e669
DJ
14705 else
14706 /* Found high w/o low attribute. */
e385593e 14707 return PC_BOUNDS_INVALID;
af34e669
DJ
14708
14709 /* Found consecutive range of addresses. */
3a2b436a 14710 ret = PC_BOUNDS_HIGH_LOW;
af34e669 14711 }
c906108c 14712 else
af34e669 14713 {
e142c38c 14714 attr = dwarf2_attr (die, DW_AT_ranges, cu);
af34e669
DJ
14715 if (attr != NULL)
14716 {
ab435259
DE
14717 /* DW_AT_ranges_base does not apply to DIEs from the DWO skeleton.
14718 We take advantage of the fact that DW_AT_ranges does not appear
14719 in DW_TAG_compile_unit of DWO files. */
14720 int need_ranges_base = die->tag != DW_TAG_compile_unit;
14721 unsigned int ranges_offset = (DW_UNSND (attr)
14722 + (need_ranges_base
14723 ? cu->ranges_base
14724 : 0));
2e3cf129 14725
af34e669 14726 /* Value of the DW_AT_ranges attribute is the offset in the
a604369a 14727 .debug_ranges section. */
2e3cf129 14728 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst))
e385593e 14729 return PC_BOUNDS_INVALID;
43039443 14730 /* Found discontinuous range of addresses. */
3a2b436a 14731 ret = PC_BOUNDS_RANGES;
af34e669 14732 }
e385593e
JK
14733 else
14734 return PC_BOUNDS_NOT_PRESENT;
af34e669 14735 }
c906108c 14736
48fbe735 14737 /* partial_die_info::read has also the strict LOW < HIGH requirement. */
9373cf26 14738 if (high <= low)
e385593e 14739 return PC_BOUNDS_INVALID;
c906108c
SS
14740
14741 /* When using the GNU linker, .gnu.linkonce. sections are used to
14742 eliminate duplicate copies of functions and vtables and such.
14743 The linker will arbitrarily choose one and discard the others.
14744 The AT_*_pc values for such functions refer to local labels in
14745 these sections. If the section from that file was discarded, the
14746 labels are not in the output, so the relocs get a value of 0.
14747 If this is a discarded function, mark the pc bounds as invalid,
14748 so that GDB will ignore it. */
72dca2f5 14749 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
e385593e 14750 return PC_BOUNDS_INVALID;
c906108c
SS
14751
14752 *lowpc = low;
96408a79
SA
14753 if (highpc)
14754 *highpc = high;
af34e669 14755 return ret;
c906108c
SS
14756}
14757
b084d499
JB
14758/* Assuming that DIE represents a subprogram DIE or a lexical block, get
14759 its low and high PC addresses. Do nothing if these addresses could not
14760 be determined. Otherwise, set LOWPC to the low address if it is smaller,
14761 and HIGHPC to the high address if greater than HIGHPC. */
14762
14763static void
14764dwarf2_get_subprogram_pc_bounds (struct die_info *die,
14765 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14766 struct dwarf2_cu *cu)
14767{
14768 CORE_ADDR low, high;
14769 struct die_info *child = die->child;
14770
e385593e 14771 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL) >= PC_BOUNDS_RANGES)
b084d499 14772 {
325fac50
PA
14773 *lowpc = std::min (*lowpc, low);
14774 *highpc = std::max (*highpc, high);
b084d499
JB
14775 }
14776
14777 /* If the language does not allow nested subprograms (either inside
14778 subprograms or lexical blocks), we're done. */
14779 if (cu->language != language_ada)
14780 return;
6e70227d 14781
b084d499
JB
14782 /* Check all the children of the given DIE. If it contains nested
14783 subprograms, then check their pc bounds. Likewise, we need to
14784 check lexical blocks as well, as they may also contain subprogram
14785 definitions. */
14786 while (child && child->tag)
14787 {
14788 if (child->tag == DW_TAG_subprogram
14789 || child->tag == DW_TAG_lexical_block)
14790 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
14791 child = sibling_die (child);
14792 }
14793}
14794
fae299cd
DC
14795/* Get the low and high pc's represented by the scope DIE, and store
14796 them in *LOWPC and *HIGHPC. If the correct values can't be
14797 determined, set *LOWPC to -1 and *HIGHPC to 0. */
14798
14799static void
14800get_scope_pc_bounds (struct die_info *die,
14801 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14802 struct dwarf2_cu *cu)
14803{
14804 CORE_ADDR best_low = (CORE_ADDR) -1;
14805 CORE_ADDR best_high = (CORE_ADDR) 0;
14806 CORE_ADDR current_low, current_high;
14807
3a2b436a 14808 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL)
e385593e 14809 >= PC_BOUNDS_RANGES)
fae299cd
DC
14810 {
14811 best_low = current_low;
14812 best_high = current_high;
14813 }
14814 else
14815 {
14816 struct die_info *child = die->child;
14817
14818 while (child && child->tag)
14819 {
14820 switch (child->tag) {
14821 case DW_TAG_subprogram:
b084d499 14822 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
fae299cd
DC
14823 break;
14824 case DW_TAG_namespace:
f55ee35c 14825 case DW_TAG_module:
fae299cd
DC
14826 /* FIXME: carlton/2004-01-16: Should we do this for
14827 DW_TAG_class_type/DW_TAG_structure_type, too? I think
14828 that current GCC's always emit the DIEs corresponding
14829 to definitions of methods of classes as children of a
14830 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
14831 the DIEs giving the declarations, which could be
14832 anywhere). But I don't see any reason why the
14833 standards says that they have to be there. */
14834 get_scope_pc_bounds (child, &current_low, &current_high, cu);
14835
14836 if (current_low != ((CORE_ADDR) -1))
14837 {
325fac50
PA
14838 best_low = std::min (best_low, current_low);
14839 best_high = std::max (best_high, current_high);
fae299cd
DC
14840 }
14841 break;
14842 default:
0963b4bd 14843 /* Ignore. */
fae299cd
DC
14844 break;
14845 }
14846
14847 child = sibling_die (child);
14848 }
14849 }
14850
14851 *lowpc = best_low;
14852 *highpc = best_high;
14853}
14854
801e3a5b
JB
14855/* Record the address ranges for BLOCK, offset by BASEADDR, as given
14856 in DIE. */
380bca97 14857
801e3a5b
JB
14858static void
14859dwarf2_record_block_ranges (struct die_info *die, struct block *block,
14860 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
14861{
518817b3 14862 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
3e29f34a 14863 struct gdbarch *gdbarch = get_objfile_arch (objfile);
801e3a5b 14864 struct attribute *attr;
91da1414 14865 struct attribute *attr_high;
801e3a5b 14866
91da1414
MW
14867 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14868 if (attr_high)
801e3a5b 14869 {
801e3a5b
JB
14870 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
14871 if (attr)
14872 {
31aa7e4e
JB
14873 CORE_ADDR low = attr_value_as_address (attr);
14874 CORE_ADDR high = attr_value_as_address (attr_high);
14875
14876 if (cu->header.version >= 4 && attr_form_is_constant (attr_high))
14877 high += low;
9a619af0 14878
3e29f34a
MR
14879 low = gdbarch_adjust_dwarf2_addr (gdbarch, low + baseaddr);
14880 high = gdbarch_adjust_dwarf2_addr (gdbarch, high + baseaddr);
14881 record_block_range (block, low, high - 1);
801e3a5b
JB
14882 }
14883 }
14884
14885 attr = dwarf2_attr (die, DW_AT_ranges, cu);
14886 if (attr)
14887 {
ab435259
DE
14888 /* DW_AT_ranges_base does not apply to DIEs from the DWO skeleton.
14889 We take advantage of the fact that DW_AT_ranges does not appear
14890 in DW_TAG_compile_unit of DWO files. */
14891 int need_ranges_base = die->tag != DW_TAG_compile_unit;
801e3a5b
JB
14892
14893 /* The value of the DW_AT_ranges attribute is the offset of the
14894 address range list in the .debug_ranges section. */
ab435259
DE
14895 unsigned long offset = (DW_UNSND (attr)
14896 + (need_ranges_base ? cu->ranges_base : 0));
d62bfeaf 14897 const gdb_byte *buffer;
801e3a5b
JB
14898
14899 /* For some target architectures, but not others, the
14900 read_address function sign-extends the addresses it returns.
14901 To recognize base address selection entries, we need a
14902 mask. */
14903 unsigned int addr_size = cu->header.addr_size;
14904 CORE_ADDR base_select_mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
14905
14906 /* The base address, to which the next pair is relative. Note
14907 that this 'base' is a DWARF concept: most entries in a range
14908 list are relative, to reduce the number of relocs against the
14909 debugging information. This is separate from this function's
14910 'baseaddr' argument, which GDB uses to relocate debugging
14911 information from a shared library based on the address at
14912 which the library was loaded. */
d00adf39
DE
14913 CORE_ADDR base = cu->base_address;
14914 int base_known = cu->base_known;
801e3a5b 14915
5f46c5a5
JK
14916 dwarf2_ranges_process (offset, cu,
14917 [&] (CORE_ADDR start, CORE_ADDR end)
14918 {
58fdfd2c
JK
14919 start += baseaddr;
14920 end += baseaddr;
5f46c5a5
JK
14921 start = gdbarch_adjust_dwarf2_addr (gdbarch, start);
14922 end = gdbarch_adjust_dwarf2_addr (gdbarch, end);
14923 record_block_range (block, start, end - 1);
14924 });
801e3a5b
JB
14925 }
14926}
14927
685b1105
JK
14928/* Check whether the producer field indicates either of GCC < 4.6, or the
14929 Intel C/C++ compiler, and cache the result in CU. */
60d5a603 14930
685b1105
JK
14931static void
14932check_producer (struct dwarf2_cu *cu)
60d5a603 14933{
38360086 14934 int major, minor;
60d5a603
JK
14935
14936 if (cu->producer == NULL)
14937 {
14938 /* For unknown compilers expect their behavior is DWARF version
14939 compliant.
14940
14941 GCC started to support .debug_types sections by -gdwarf-4 since
14942 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
14943 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
14944 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
14945 interpreted incorrectly by GDB now - GCC PR debug/48229. */
60d5a603 14946 }
b1ffba5a 14947 else if (producer_is_gcc (cu->producer, &major, &minor))
60d5a603 14948 {
38360086
MW
14949 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
14950 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
685b1105 14951 }
5230b05a
WT
14952 else if (producer_is_icc (cu->producer, &major, &minor))
14953 cu->producer_is_icc_lt_14 = major < 14;
685b1105
JK
14954 else
14955 {
14956 /* For other non-GCC compilers, expect their behavior is DWARF version
14957 compliant. */
60d5a603
JK
14958 }
14959
ba919b58 14960 cu->checked_producer = 1;
685b1105 14961}
ba919b58 14962
685b1105
JK
14963/* Check for GCC PR debug/45124 fix which is not present in any G++ version up
14964 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
14965 during 4.6.0 experimental. */
14966
14967static int
14968producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
14969{
14970 if (!cu->checked_producer)
14971 check_producer (cu);
14972
14973 return cu->producer_is_gxx_lt_4_6;
60d5a603
JK
14974}
14975
14976/* Return the default accessibility type if it is not overriden by
14977 DW_AT_accessibility. */
14978
14979static enum dwarf_access_attribute
14980dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
14981{
14982 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
14983 {
14984 /* The default DWARF 2 accessibility for members is public, the default
14985 accessibility for inheritance is private. */
14986
14987 if (die->tag != DW_TAG_inheritance)
14988 return DW_ACCESS_public;
14989 else
14990 return DW_ACCESS_private;
14991 }
14992 else
14993 {
14994 /* DWARF 3+ defines the default accessibility a different way. The same
14995 rules apply now for DW_TAG_inheritance as for the members and it only
14996 depends on the container kind. */
14997
14998 if (die->parent->tag == DW_TAG_class_type)
14999 return DW_ACCESS_private;
15000 else
15001 return DW_ACCESS_public;
15002 }
15003}
15004
74ac6d43
TT
15005/* Look for DW_AT_data_member_location. Set *OFFSET to the byte
15006 offset. If the attribute was not found return 0, otherwise return
15007 1. If it was found but could not properly be handled, set *OFFSET
15008 to 0. */
15009
15010static int
15011handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
15012 LONGEST *offset)
15013{
15014 struct attribute *attr;
15015
15016 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
15017 if (attr != NULL)
15018 {
15019 *offset = 0;
15020
15021 /* Note that we do not check for a section offset first here.
15022 This is because DW_AT_data_member_location is new in DWARF 4,
15023 so if we see it, we can assume that a constant form is really
15024 a constant and not a section offset. */
15025 if (attr_form_is_constant (attr))
15026 *offset = dwarf2_get_attr_constant_value (attr, 0);
15027 else if (attr_form_is_section_offset (attr))
15028 dwarf2_complex_location_expr_complaint ();
15029 else if (attr_form_is_block (attr))
15030 *offset = decode_locdesc (DW_BLOCK (attr), cu);
15031 else
15032 dwarf2_complex_location_expr_complaint ();
15033
15034 return 1;
15035 }
15036
15037 return 0;
15038}
15039
c906108c
SS
15040/* Add an aggregate field to the field list. */
15041
15042static void
107d2387 15043dwarf2_add_field (struct field_info *fip, struct die_info *die,
e7c27a73 15044 struct dwarf2_cu *cu)
6e70227d 15045{
518817b3 15046 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
5e2b427d 15047 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
15048 struct nextfield *new_field;
15049 struct attribute *attr;
15050 struct field *fp;
15d034d0 15051 const char *fieldname = "";
c906108c 15052
7d0ccb61
DJ
15053 if (die->tag == DW_TAG_inheritance)
15054 {
be2daae6
TT
15055 fip->baseclasses.emplace_back ();
15056 new_field = &fip->baseclasses.back ();
7d0ccb61
DJ
15057 }
15058 else
15059 {
be2daae6
TT
15060 fip->fields.emplace_back ();
15061 new_field = &fip->fields.back ();
7d0ccb61 15062 }
be2daae6 15063
c906108c
SS
15064 fip->nfields++;
15065
e142c38c 15066 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
c906108c
SS
15067 if (attr)
15068 new_field->accessibility = DW_UNSND (attr);
60d5a603
JK
15069 else
15070 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
c906108c
SS
15071 if (new_field->accessibility != DW_ACCESS_public)
15072 fip->non_public_fields = 1;
60d5a603 15073
e142c38c 15074 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
c906108c
SS
15075 if (attr)
15076 new_field->virtuality = DW_UNSND (attr);
60d5a603
JK
15077 else
15078 new_field->virtuality = DW_VIRTUALITY_none;
c906108c
SS
15079
15080 fp = &new_field->field;
a9a9bd0f 15081
e142c38c 15082 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
c906108c 15083 {
74ac6d43
TT
15084 LONGEST offset;
15085
a9a9bd0f 15086 /* Data member other than a C++ static data member. */
6e70227d 15087
c906108c 15088 /* Get type of field. */
e7c27a73 15089 fp->type = die_type (die, cu);
c906108c 15090
d6a843b5 15091 SET_FIELD_BITPOS (*fp, 0);
01ad7f36 15092
c906108c 15093 /* Get bit size of field (zero if none). */
e142c38c 15094 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
c906108c
SS
15095 if (attr)
15096 {
15097 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
15098 }
15099 else
15100 {
15101 FIELD_BITSIZE (*fp) = 0;
15102 }
15103
15104 /* Get bit offset of field. */
74ac6d43
TT
15105 if (handle_data_member_location (die, cu, &offset))
15106 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
e142c38c 15107 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
c906108c
SS
15108 if (attr)
15109 {
5e2b427d 15110 if (gdbarch_bits_big_endian (gdbarch))
c906108c
SS
15111 {
15112 /* For big endian bits, the DW_AT_bit_offset gives the
c5aa993b
JM
15113 additional bit offset from the MSB of the containing
15114 anonymous object to the MSB of the field. We don't
15115 have to do anything special since we don't need to
15116 know the size of the anonymous object. */
f41f5e61 15117 SET_FIELD_BITPOS (*fp, FIELD_BITPOS (*fp) + DW_UNSND (attr));
c906108c
SS
15118 }
15119 else
15120 {
15121 /* For little endian bits, compute the bit offset to the
c5aa993b
JM
15122 MSB of the anonymous object, subtract off the number of
15123 bits from the MSB of the field to the MSB of the
15124 object, and then subtract off the number of bits of
15125 the field itself. The result is the bit offset of
15126 the LSB of the field. */
c906108c
SS
15127 int anonymous_size;
15128 int bit_offset = DW_UNSND (attr);
15129
e142c38c 15130 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
15131 if (attr)
15132 {
15133 /* The size of the anonymous object containing
15134 the bit field is explicit, so use the
15135 indicated size (in bytes). */
15136 anonymous_size = DW_UNSND (attr);
15137 }
15138 else
15139 {
15140 /* The size of the anonymous object containing
15141 the bit field must be inferred from the type
15142 attribute of the data member containing the
15143 bit field. */
15144 anonymous_size = TYPE_LENGTH (fp->type);
15145 }
f41f5e61
PA
15146 SET_FIELD_BITPOS (*fp,
15147 (FIELD_BITPOS (*fp)
15148 + anonymous_size * bits_per_byte
15149 - bit_offset - FIELD_BITSIZE (*fp)));
c906108c
SS
15150 }
15151 }
da5b30da
AA
15152 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
15153 if (attr != NULL)
15154 SET_FIELD_BITPOS (*fp, (FIELD_BITPOS (*fp)
15155 + dwarf2_get_attr_constant_value (attr, 0)));
c906108c
SS
15156
15157 /* Get name of field. */
39cbfefa
DJ
15158 fieldname = dwarf2_name (die, cu);
15159 if (fieldname == NULL)
15160 fieldname = "";
d8151005
DJ
15161
15162 /* The name is already allocated along with this objfile, so we don't
15163 need to duplicate it for the type. */
15164 fp->name = fieldname;
c906108c
SS
15165
15166 /* Change accessibility for artificial fields (e.g. virtual table
c5aa993b 15167 pointer or virtual base class pointer) to private. */
e142c38c 15168 if (dwarf2_attr (die, DW_AT_artificial, cu))
c906108c 15169 {
d48cc9dd 15170 FIELD_ARTIFICIAL (*fp) = 1;
c906108c
SS
15171 new_field->accessibility = DW_ACCESS_private;
15172 fip->non_public_fields = 1;
15173 }
15174 }
a9a9bd0f 15175 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
c906108c 15176 {
a9a9bd0f
DC
15177 /* C++ static member. */
15178
15179 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
15180 is a declaration, but all versions of G++ as of this writing
15181 (so through at least 3.2.1) incorrectly generate
15182 DW_TAG_variable tags. */
6e70227d 15183
ff355380 15184 const char *physname;
c906108c 15185
a9a9bd0f 15186 /* Get name of field. */
39cbfefa
DJ
15187 fieldname = dwarf2_name (die, cu);
15188 if (fieldname == NULL)
c906108c
SS
15189 return;
15190
254e6b9e 15191 attr = dwarf2_attr (die, DW_AT_const_value, cu);
3863f96c
DE
15192 if (attr
15193 /* Only create a symbol if this is an external value.
15194 new_symbol checks this and puts the value in the global symbol
15195 table, which we want. If it is not external, new_symbol
15196 will try to put the value in cu->list_in_scope which is wrong. */
15197 && dwarf2_flag_true_p (die, DW_AT_external, cu))
254e6b9e
DE
15198 {
15199 /* A static const member, not much different than an enum as far as
15200 we're concerned, except that we can support more types. */
15201 new_symbol (die, NULL, cu);
15202 }
15203
2df3850c 15204 /* Get physical name. */
ff355380 15205 physname = dwarf2_physname (fieldname, die, cu);
c906108c 15206
d8151005
DJ
15207 /* The name is already allocated along with this objfile, so we don't
15208 need to duplicate it for the type. */
15209 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
e7c27a73 15210 FIELD_TYPE (*fp) = die_type (die, cu);
d8151005 15211 FIELD_NAME (*fp) = fieldname;
c906108c
SS
15212 }
15213 else if (die->tag == DW_TAG_inheritance)
15214 {
74ac6d43 15215 LONGEST offset;
d4b96c9a 15216
74ac6d43
TT
15217 /* C++ base class field. */
15218 if (handle_data_member_location (die, cu, &offset))
15219 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
c906108c 15220 FIELD_BITSIZE (*fp) = 0;
e7c27a73 15221 FIELD_TYPE (*fp) = die_type (die, cu);
c906108c 15222 FIELD_NAME (*fp) = type_name_no_tag (fp->type);
c906108c 15223 }
2ddeaf8a
TT
15224 else if (die->tag == DW_TAG_variant_part)
15225 {
15226 /* process_structure_scope will treat this DIE as a union. */
15227 process_structure_scope (die, cu);
15228
15229 /* The variant part is relative to the start of the enclosing
15230 structure. */
15231 SET_FIELD_BITPOS (*fp, 0);
15232 fp->type = get_die_type (die, cu);
15233 fp->artificial = 1;
15234 fp->name = "<<variant>>";
15235 }
15236 else
15237 gdb_assert_not_reached ("missing case in dwarf2_add_field");
c906108c
SS
15238}
15239
883fd55a
KS
15240/* Can the type given by DIE define another type? */
15241
15242static bool
15243type_can_define_types (const struct die_info *die)
15244{
15245 switch (die->tag)
15246 {
15247 case DW_TAG_typedef:
15248 case DW_TAG_class_type:
15249 case DW_TAG_structure_type:
15250 case DW_TAG_union_type:
15251 case DW_TAG_enumeration_type:
15252 return true;
15253
15254 default:
15255 return false;
15256 }
15257}
15258
15259/* Add a type definition defined in the scope of the FIP's class. */
98751a41
JK
15260
15261static void
883fd55a
KS
15262dwarf2_add_type_defn (struct field_info *fip, struct die_info *die,
15263 struct dwarf2_cu *cu)
6e70227d 15264{
be2daae6
TT
15265 struct decl_field fp;
15266 memset (&fp, 0, sizeof (fp));
98751a41 15267
883fd55a 15268 gdb_assert (type_can_define_types (die));
98751a41 15269
883fd55a 15270 /* Get name of field. NULL is okay here, meaning an anonymous type. */
be2daae6
TT
15271 fp.name = dwarf2_name (die, cu);
15272 fp.type = read_type_die (die, cu);
98751a41 15273
c191a687
KS
15274 /* Save accessibility. */
15275 enum dwarf_access_attribute accessibility;
15276 struct attribute *attr = dwarf2_attr (die, DW_AT_accessibility, cu);
15277 if (attr != NULL)
15278 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
15279 else
15280 accessibility = dwarf2_default_access_attribute (die, cu);
15281 switch (accessibility)
15282 {
15283 case DW_ACCESS_public:
15284 /* The assumed value if neither private nor protected. */
15285 break;
15286 case DW_ACCESS_private:
be2daae6 15287 fp.is_private = 1;
c191a687
KS
15288 break;
15289 case DW_ACCESS_protected:
be2daae6 15290 fp.is_protected = 1;
c191a687
KS
15291 break;
15292 default:
37534686
KS
15293 complaint (&symfile_complaints,
15294 _("Unhandled DW_AT_accessibility value (%x)"), accessibility);
c191a687
KS
15295 }
15296
883fd55a 15297 if (die->tag == DW_TAG_typedef)
be2daae6 15298 fip->typedef_field_list.push_back (fp);
883fd55a 15299 else
be2daae6 15300 fip->nested_types_list.push_back (fp);
98751a41
JK
15301}
15302
c906108c
SS
15303/* Create the vector of fields, and attach it to the type. */
15304
15305static void
fba45db2 15306dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 15307 struct dwarf2_cu *cu)
c906108c
SS
15308{
15309 int nfields = fip->nfields;
15310
15311 /* Record the field count, allocate space for the array of fields,
15312 and create blank accessibility bitfields if necessary. */
15313 TYPE_NFIELDS (type) = nfields;
15314 TYPE_FIELDS (type) = (struct field *)
be2daae6 15315 TYPE_ZALLOC (type, sizeof (struct field) * nfields);
c906108c 15316
b4ba55a1 15317 if (fip->non_public_fields && cu->language != language_ada)
c906108c
SS
15318 {
15319 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15320
15321 TYPE_FIELD_PRIVATE_BITS (type) =
15322 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15323 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
15324
15325 TYPE_FIELD_PROTECTED_BITS (type) =
15326 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15327 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
15328
774b6a14
TT
15329 TYPE_FIELD_IGNORE_BITS (type) =
15330 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15331 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
c906108c
SS
15332 }
15333
15334 /* If the type has baseclasses, allocate and clear a bit vector for
15335 TYPE_FIELD_VIRTUAL_BITS. */
be2daae6 15336 if (!fip->baseclasses.empty () && cu->language != language_ada)
c906108c 15337 {
be2daae6 15338 int num_bytes = B_BYTES (fip->baseclasses.size ());
fe1b8b76 15339 unsigned char *pointer;
c906108c
SS
15340
15341 ALLOCATE_CPLUS_STRUCT_TYPE (type);
224c3ddb 15342 pointer = (unsigned char *) TYPE_ALLOC (type, num_bytes);
fe1b8b76 15343 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
be2daae6
TT
15344 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->baseclasses.size ());
15345 TYPE_N_BASECLASSES (type) = fip->baseclasses.size ();
c906108c
SS
15346 }
15347
2ddeaf8a
TT
15348 if (TYPE_FLAG_DISCRIMINATED_UNION (type))
15349 {
15350 struct discriminant_info *di = alloc_discriminant_info (type, -1, -1);
15351
be2daae6 15352 for (int index = 0; index < nfields; ++index)
2ddeaf8a 15353 {
be2daae6
TT
15354 struct nextfield &field = fip->fields[index];
15355
15356 if (field.variant.is_discriminant)
2ddeaf8a 15357 di->discriminant_index = index;
be2daae6 15358 else if (field.variant.default_branch)
2ddeaf8a
TT
15359 di->default_index = index;
15360 else
be2daae6 15361 di->discriminants[index] = field.variant.discriminant_value;
2ddeaf8a
TT
15362 }
15363 }
15364
be2daae6
TT
15365 /* Copy the saved-up fields into the field vector. */
15366 for (int i = 0; i < nfields; ++i)
c906108c 15367 {
be2daae6
TT
15368 struct nextfield &field
15369 = ((i < fip->baseclasses.size ()) ? fip->baseclasses[i]
15370 : fip->fields[i - fip->baseclasses.size ()]);
7d0ccb61 15371
be2daae6
TT
15372 TYPE_FIELD (type, i) = field.field;
15373 switch (field.accessibility)
c906108c 15374 {
c5aa993b 15375 case DW_ACCESS_private:
b4ba55a1 15376 if (cu->language != language_ada)
be2daae6 15377 SET_TYPE_FIELD_PRIVATE (type, i);
c5aa993b 15378 break;
c906108c 15379
c5aa993b 15380 case DW_ACCESS_protected:
b4ba55a1 15381 if (cu->language != language_ada)
be2daae6 15382 SET_TYPE_FIELD_PROTECTED (type, i);
c5aa993b 15383 break;
c906108c 15384
c5aa993b
JM
15385 case DW_ACCESS_public:
15386 break;
c906108c 15387
c5aa993b
JM
15388 default:
15389 /* Unknown accessibility. Complain and treat it as public. */
15390 {
e2e0b3e5 15391 complaint (&symfile_complaints, _("unsupported accessibility %d"),
be2daae6 15392 field.accessibility);
c5aa993b
JM
15393 }
15394 break;
c906108c 15395 }
be2daae6 15396 if (i < fip->baseclasses.size ())
c906108c 15397 {
be2daae6 15398 switch (field.virtuality)
c906108c 15399 {
c5aa993b
JM
15400 case DW_VIRTUALITY_virtual:
15401 case DW_VIRTUALITY_pure_virtual:
b4ba55a1 15402 if (cu->language == language_ada)
a73c6dcd 15403 error (_("unexpected virtuality in component of Ada type"));
be2daae6 15404 SET_TYPE_FIELD_VIRTUAL (type, i);
c5aa993b 15405 break;
c906108c
SS
15406 }
15407 }
c906108c
SS
15408 }
15409}
15410
7d27a96d
TT
15411/* Return true if this member function is a constructor, false
15412 otherwise. */
15413
15414static int
15415dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
15416{
15417 const char *fieldname;
fe978cb0 15418 const char *type_name;
7d27a96d
TT
15419 int len;
15420
15421 if (die->parent == NULL)
15422 return 0;
15423
15424 if (die->parent->tag != DW_TAG_structure_type
15425 && die->parent->tag != DW_TAG_union_type
15426 && die->parent->tag != DW_TAG_class_type)
15427 return 0;
15428
15429 fieldname = dwarf2_name (die, cu);
fe978cb0
PA
15430 type_name = dwarf2_name (die->parent, cu);
15431 if (fieldname == NULL || type_name == NULL)
7d27a96d
TT
15432 return 0;
15433
15434 len = strlen (fieldname);
fe978cb0
PA
15435 return (strncmp (fieldname, type_name, len) == 0
15436 && (type_name[len] == '\0' || type_name[len] == '<'));
7d27a96d
TT
15437}
15438
c906108c
SS
15439/* Add a member function to the proper fieldlist. */
15440
15441static void
107d2387 15442dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
e7c27a73 15443 struct type *type, struct dwarf2_cu *cu)
c906108c 15444{
518817b3 15445 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c 15446 struct attribute *attr;
c906108c 15447 int i;
be2daae6 15448 struct fnfieldlist *flp = nullptr;
c906108c 15449 struct fn_field *fnp;
15d034d0 15450 const char *fieldname;
f792889a 15451 struct type *this_type;
60d5a603 15452 enum dwarf_access_attribute accessibility;
c906108c 15453
b4ba55a1 15454 if (cu->language == language_ada)
a73c6dcd 15455 error (_("unexpected member function in Ada type"));
b4ba55a1 15456
2df3850c 15457 /* Get name of member function. */
39cbfefa
DJ
15458 fieldname = dwarf2_name (die, cu);
15459 if (fieldname == NULL)
2df3850c 15460 return;
c906108c 15461
c906108c 15462 /* Look up member function name in fieldlist. */
be2daae6 15463 for (i = 0; i < fip->fnfieldlists.size (); i++)
c906108c 15464 {
27bfe10e 15465 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
be2daae6
TT
15466 {
15467 flp = &fip->fnfieldlists[i];
15468 break;
15469 }
c906108c
SS
15470 }
15471
be2daae6
TT
15472 /* Create a new fnfieldlist if necessary. */
15473 if (flp == nullptr)
c906108c 15474 {
be2daae6
TT
15475 fip->fnfieldlists.emplace_back ();
15476 flp = &fip->fnfieldlists.back ();
c906108c 15477 flp->name = fieldname;
be2daae6 15478 i = fip->fnfieldlists.size () - 1;
c906108c
SS
15479 }
15480
be2daae6
TT
15481 /* Create a new member function field and add it to the vector of
15482 fnfieldlists. */
15483 flp->fnfields.emplace_back ();
15484 fnp = &flp->fnfields.back ();
3da10d80
KS
15485
15486 /* Delay processing of the physname until later. */
9c37b5ae 15487 if (cu->language == language_cplus)
be2daae6
TT
15488 add_to_method_list (type, i, flp->fnfields.size () - 1, fieldname,
15489 die, cu);
3da10d80
KS
15490 else
15491 {
1d06ead6 15492 const char *physname = dwarf2_physname (fieldname, die, cu);
3da10d80
KS
15493 fnp->physname = physname ? physname : "";
15494 }
15495
c906108c 15496 fnp->type = alloc_type (objfile);
f792889a
DJ
15497 this_type = read_type_die (die, cu);
15498 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
c906108c 15499 {
f792889a 15500 int nparams = TYPE_NFIELDS (this_type);
c906108c 15501
f792889a 15502 /* TYPE is the domain of this method, and THIS_TYPE is the type
e26fb1d7
DC
15503 of the method itself (TYPE_CODE_METHOD). */
15504 smash_to_method_type (fnp->type, type,
f792889a
DJ
15505 TYPE_TARGET_TYPE (this_type),
15506 TYPE_FIELDS (this_type),
15507 TYPE_NFIELDS (this_type),
15508 TYPE_VARARGS (this_type));
c906108c
SS
15509
15510 /* Handle static member functions.
c5aa993b 15511 Dwarf2 has no clean way to discern C++ static and non-static
0963b4bd
MS
15512 member functions. G++ helps GDB by marking the first
15513 parameter for non-static member functions (which is the this
15514 pointer) as artificial. We obtain this information from
15515 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
f792889a 15516 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
c906108c
SS
15517 fnp->voffset = VOFFSET_STATIC;
15518 }
15519 else
e2e0b3e5 15520 complaint (&symfile_complaints, _("member function type missing for '%s'"),
3da10d80 15521 dwarf2_full_name (fieldname, die, cu));
c906108c
SS
15522
15523 /* Get fcontext from DW_AT_containing_type if present. */
e142c38c 15524 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
e7c27a73 15525 fnp->fcontext = die_containing_type (die, cu);
c906108c 15526
3e43a32a
MS
15527 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
15528 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
c906108c
SS
15529
15530 /* Get accessibility. */
e142c38c 15531 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
c906108c 15532 if (attr)
aead7601 15533 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
60d5a603
JK
15534 else
15535 accessibility = dwarf2_default_access_attribute (die, cu);
15536 switch (accessibility)
c906108c 15537 {
60d5a603
JK
15538 case DW_ACCESS_private:
15539 fnp->is_private = 1;
15540 break;
15541 case DW_ACCESS_protected:
15542 fnp->is_protected = 1;
15543 break;
c906108c
SS
15544 }
15545
b02dede2 15546 /* Check for artificial methods. */
e142c38c 15547 attr = dwarf2_attr (die, DW_AT_artificial, cu);
b02dede2
DJ
15548 if (attr && DW_UNSND (attr) != 0)
15549 fnp->is_artificial = 1;
15550
7d27a96d
TT
15551 fnp->is_constructor = dwarf2_is_constructor (die, cu);
15552
0d564a31 15553 /* Get index in virtual function table if it is a virtual member
aec5aa8b
TT
15554 function. For older versions of GCC, this is an offset in the
15555 appropriate virtual table, as specified by DW_AT_containing_type.
15556 For everyone else, it is an expression to be evaluated relative
0d564a31
DJ
15557 to the object address. */
15558
e142c38c 15559 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
aec5aa8b 15560 if (attr)
8e19ed76 15561 {
aec5aa8b 15562 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size > 0)
8e19ed76 15563 {
aec5aa8b
TT
15564 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
15565 {
15566 /* Old-style GCC. */
15567 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
15568 }
15569 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
15570 || (DW_BLOCK (attr)->size > 1
15571 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
15572 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
15573 {
aec5aa8b
TT
15574 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
15575 if ((fnp->voffset % cu->header.addr_size) != 0)
15576 dwarf2_complex_location_expr_complaint ();
15577 else
15578 fnp->voffset /= cu->header.addr_size;
15579 fnp->voffset += 2;
15580 }
15581 else
15582 dwarf2_complex_location_expr_complaint ();
15583
15584 if (!fnp->fcontext)
7e993ebf
KS
15585 {
15586 /* If there is no `this' field and no DW_AT_containing_type,
15587 we cannot actually find a base class context for the
15588 vtable! */
15589 if (TYPE_NFIELDS (this_type) == 0
15590 || !TYPE_FIELD_ARTIFICIAL (this_type, 0))
15591 {
15592 complaint (&symfile_complaints,
15593 _("cannot determine context for virtual member "
9d8780f0
SM
15594 "function \"%s\" (offset %s)"),
15595 fieldname, sect_offset_str (die->sect_off));
7e993ebf
KS
15596 }
15597 else
15598 {
15599 fnp->fcontext
15600 = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
15601 }
15602 }
aec5aa8b 15603 }
3690dd37 15604 else if (attr_form_is_section_offset (attr))
8e19ed76 15605 {
4d3c2250 15606 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
15607 }
15608 else
15609 {
4d3c2250
KB
15610 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
15611 fieldname);
8e19ed76 15612 }
0d564a31 15613 }
d48cc9dd
DJ
15614 else
15615 {
15616 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
15617 if (attr && DW_UNSND (attr))
15618 {
15619 /* GCC does this, as of 2008-08-25; PR debug/37237. */
15620 complaint (&symfile_complaints,
9d8780f0 15621 _("Member function \"%s\" (offset %s) is virtual "
3e43a32a 15622 "but the vtable offset is not specified"),
9d8780f0 15623 fieldname, sect_offset_str (die->sect_off));
9655fd1a 15624 ALLOCATE_CPLUS_STRUCT_TYPE (type);
d48cc9dd
DJ
15625 TYPE_CPLUS_DYNAMIC (type) = 1;
15626 }
15627 }
c906108c
SS
15628}
15629
15630/* Create the vector of member function fields, and attach it to the type. */
15631
15632static void
fba45db2 15633dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 15634 struct dwarf2_cu *cu)
c906108c 15635{
b4ba55a1 15636 if (cu->language == language_ada)
a73c6dcd 15637 error (_("unexpected member functions in Ada type"));
b4ba55a1 15638
c906108c
SS
15639 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15640 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
be2daae6
TT
15641 TYPE_ALLOC (type,
15642 sizeof (struct fn_fieldlist) * fip->fnfieldlists.size ());
c906108c 15643
be2daae6 15644 for (int i = 0; i < fip->fnfieldlists.size (); i++)
c906108c 15645 {
be2daae6 15646 struct fnfieldlist &nf = fip->fnfieldlists[i];
c906108c 15647 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
c906108c 15648
be2daae6
TT
15649 TYPE_FN_FIELDLIST_NAME (type, i) = nf.name;
15650 TYPE_FN_FIELDLIST_LENGTH (type, i) = nf.fnfields.size ();
c906108c 15651 fn_flp->fn_fields = (struct fn_field *)
be2daae6
TT
15652 TYPE_ALLOC (type, sizeof (struct fn_field) * nf.fnfields.size ());
15653
15654 for (int k = 0; k < nf.fnfields.size (); ++k)
15655 fn_flp->fn_fields[k] = nf.fnfields[k];
c906108c
SS
15656 }
15657
be2daae6 15658 TYPE_NFN_FIELDS (type) = fip->fnfieldlists.size ();
c906108c
SS
15659}
15660
1168df01
JB
15661/* Returns non-zero if NAME is the name of a vtable member in CU's
15662 language, zero otherwise. */
15663static int
15664is_vtable_name (const char *name, struct dwarf2_cu *cu)
15665{
15666 static const char vptr[] = "_vptr";
15667
9c37b5ae
TT
15668 /* Look for the C++ form of the vtable. */
15669 if (startswith (name, vptr) && is_cplus_marker (name[sizeof (vptr) - 1]))
1168df01
JB
15670 return 1;
15671
15672 return 0;
15673}
15674
c0dd20ea 15675/* GCC outputs unnamed structures that are really pointers to member
0b92b5bb
TT
15676 functions, with the ABI-specified layout. If TYPE describes
15677 such a structure, smash it into a member function type.
61049d3b
DJ
15678
15679 GCC shouldn't do this; it should just output pointer to member DIEs.
15680 This is GCC PR debug/28767. */
c0dd20ea 15681
0b92b5bb
TT
15682static void
15683quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
c0dd20ea 15684{
09e2d7c7 15685 struct type *pfn_type, *self_type, *new_type;
c0dd20ea
DJ
15686
15687 /* Check for a structure with no name and two children. */
0b92b5bb
TT
15688 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
15689 return;
c0dd20ea
DJ
15690
15691 /* Check for __pfn and __delta members. */
0b92b5bb
TT
15692 if (TYPE_FIELD_NAME (type, 0) == NULL
15693 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
15694 || TYPE_FIELD_NAME (type, 1) == NULL
15695 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
15696 return;
c0dd20ea
DJ
15697
15698 /* Find the type of the method. */
0b92b5bb 15699 pfn_type = TYPE_FIELD_TYPE (type, 0);
c0dd20ea
DJ
15700 if (pfn_type == NULL
15701 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
15702 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
0b92b5bb 15703 return;
c0dd20ea
DJ
15704
15705 /* Look for the "this" argument. */
15706 pfn_type = TYPE_TARGET_TYPE (pfn_type);
15707 if (TYPE_NFIELDS (pfn_type) == 0
0b92b5bb 15708 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
c0dd20ea 15709 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
0b92b5bb 15710 return;
c0dd20ea 15711
09e2d7c7 15712 self_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
0b92b5bb 15713 new_type = alloc_type (objfile);
09e2d7c7 15714 smash_to_method_type (new_type, self_type, TYPE_TARGET_TYPE (pfn_type),
c0dd20ea
DJ
15715 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
15716 TYPE_VARARGS (pfn_type));
0b92b5bb 15717 smash_to_methodptr_type (type, new_type);
c0dd20ea 15718}
1168df01 15719
685b1105 15720
c906108c 15721/* Called when we find the DIE that starts a structure or union scope
c767944b
DJ
15722 (definition) to create a type for the structure or union. Fill in
15723 the type's name and general properties; the members will not be
83655187
DE
15724 processed until process_structure_scope. A symbol table entry for
15725 the type will also not be done until process_structure_scope (assuming
15726 the type has a name).
c906108c 15727
c767944b
DJ
15728 NOTE: we need to call these functions regardless of whether or not the
15729 DIE has a DW_AT_name attribute, since it might be an anonymous
c906108c 15730 structure or union. This gets the type entered into our set of
83655187 15731 user defined types. */
c906108c 15732
f792889a 15733static struct type *
134d01f1 15734read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 15735{
518817b3 15736 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c
SS
15737 struct type *type;
15738 struct attribute *attr;
15d034d0 15739 const char *name;
c906108c 15740
348e048f
DE
15741 /* If the definition of this type lives in .debug_types, read that type.
15742 Don't follow DW_AT_specification though, that will take us back up
15743 the chain and we want to go down. */
45e58e77 15744 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
348e048f
DE
15745 if (attr)
15746 {
ac9ec31b 15747 type = get_DW_AT_signature_type (die, attr, cu);
9dc481d3 15748
ac9ec31b 15749 /* The type's CU may not be the same as CU.
02142a6c 15750 Ensure TYPE is recorded with CU in die_type_hash. */
348e048f
DE
15751 return set_die_type (die, type, cu);
15752 }
15753
c0dd20ea 15754 type = alloc_type (objfile);
c906108c 15755 INIT_CPLUS_SPECIFIC (type);
93311388 15756
39cbfefa
DJ
15757 name = dwarf2_name (die, cu);
15758 if (name != NULL)
c906108c 15759 {
987504bb 15760 if (cu->language == language_cplus
c44af4eb
TT
15761 || cu->language == language_d
15762 || cu->language == language_rust)
63d06c5c 15763 {
15d034d0 15764 const char *full_name = dwarf2_full_name (name, die, cu);
3da10d80
KS
15765
15766 /* dwarf2_full_name might have already finished building the DIE's
15767 type. If so, there is no need to continue. */
15768 if (get_die_type (die, cu) != NULL)
15769 return get_die_type (die, cu);
15770
15771 TYPE_TAG_NAME (type) = full_name;
94af9270
KS
15772 if (die->tag == DW_TAG_structure_type
15773 || die->tag == DW_TAG_class_type)
15774 TYPE_NAME (type) = TYPE_TAG_NAME (type);
63d06c5c
DC
15775 }
15776 else
15777 {
d8151005
DJ
15778 /* The name is already allocated along with this objfile, so
15779 we don't need to duplicate it for the type. */
7d455152 15780 TYPE_TAG_NAME (type) = name;
94af9270
KS
15781 if (die->tag == DW_TAG_class_type)
15782 TYPE_NAME (type) = TYPE_TAG_NAME (type);
63d06c5c 15783 }
c906108c
SS
15784 }
15785
15786 if (die->tag == DW_TAG_structure_type)
15787 {
15788 TYPE_CODE (type) = TYPE_CODE_STRUCT;
15789 }
15790 else if (die->tag == DW_TAG_union_type)
15791 {
15792 TYPE_CODE (type) = TYPE_CODE_UNION;
15793 }
2ddeaf8a
TT
15794 else if (die->tag == DW_TAG_variant_part)
15795 {
15796 TYPE_CODE (type) = TYPE_CODE_UNION;
15797 TYPE_FLAG_DISCRIMINATED_UNION (type) = 1;
15798 }
c906108c
SS
15799 else
15800 {
4753d33b 15801 TYPE_CODE (type) = TYPE_CODE_STRUCT;
c906108c
SS
15802 }
15803
0cc2414c
TT
15804 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
15805 TYPE_DECLARED_CLASS (type) = 1;
15806
e142c38c 15807 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
15808 if (attr)
15809 {
155bfbd3
JB
15810 if (attr_form_is_constant (attr))
15811 TYPE_LENGTH (type) = DW_UNSND (attr);
15812 else
15813 {
15814 /* For the moment, dynamic type sizes are not supported
15815 by GDB's struct type. The actual size is determined
15816 on-demand when resolving the type of a given object,
15817 so set the type's length to zero for now. Otherwise,
15818 we record an expression as the length, and that expression
15819 could lead to a very large value, which could eventually
15820 lead to us trying to allocate that much memory when creating
15821 a value of that type. */
15822 TYPE_LENGTH (type) = 0;
15823 }
c906108c
SS
15824 }
15825 else
15826 {
15827 TYPE_LENGTH (type) = 0;
15828 }
15829
5230b05a 15830 if (producer_is_icc_lt_14 (cu) && (TYPE_LENGTH (type) == 0))
685b1105 15831 {
5230b05a
WT
15832 /* ICC<14 does not output the required DW_AT_declaration on
15833 incomplete types, but gives them a size of zero. */
422b1cb0 15834 TYPE_STUB (type) = 1;
685b1105
JK
15835 }
15836 else
15837 TYPE_STUB_SUPPORTED (type) = 1;
15838
dc718098 15839 if (die_is_declaration (die, cu))
876cecd0 15840 TYPE_STUB (type) = 1;
a6c727b2
DJ
15841 else if (attr == NULL && die->child == NULL
15842 && producer_is_realview (cu->producer))
15843 /* RealView does not output the required DW_AT_declaration
15844 on incomplete types. */
15845 TYPE_STUB (type) = 1;
dc718098 15846
c906108c
SS
15847 /* We need to add the type field to the die immediately so we don't
15848 infinitely recurse when dealing with pointers to the structure
0963b4bd 15849 type within the structure itself. */
1c379e20 15850 set_die_type (die, type, cu);
c906108c 15851
7e314c57
JK
15852 /* set_die_type should be already done. */
15853 set_descriptive_type (type, die, cu);
15854
c767944b
DJ
15855 return type;
15856}
15857
2ddeaf8a
TT
15858/* A helper for process_structure_scope that handles a single member
15859 DIE. */
15860
15861static void
15862handle_struct_member_die (struct die_info *child_die, struct type *type,
15863 struct field_info *fi,
15864 std::vector<struct symbol *> *template_args,
15865 struct dwarf2_cu *cu)
15866{
15867 if (child_die->tag == DW_TAG_member
15868 || child_die->tag == DW_TAG_variable
15869 || child_die->tag == DW_TAG_variant_part)
15870 {
15871 /* NOTE: carlton/2002-11-05: A C++ static data member
15872 should be a DW_TAG_member that is a declaration, but
15873 all versions of G++ as of this writing (so through at
15874 least 3.2.1) incorrectly generate DW_TAG_variable
15875 tags for them instead. */
15876 dwarf2_add_field (fi, child_die, cu);
15877 }
15878 else if (child_die->tag == DW_TAG_subprogram)
15879 {
15880 /* Rust doesn't have member functions in the C++ sense.
15881 However, it does emit ordinary functions as children
15882 of a struct DIE. */
15883 if (cu->language == language_rust)
15884 read_func_scope (child_die, cu);
15885 else
15886 {
15887 /* C++ member function. */
15888 dwarf2_add_member_fn (fi, child_die, type, cu);
15889 }
15890 }
15891 else if (child_die->tag == DW_TAG_inheritance)
15892 {
15893 /* C++ base class field. */
15894 dwarf2_add_field (fi, child_die, cu);
15895 }
15896 else if (type_can_define_types (child_die))
15897 dwarf2_add_type_defn (fi, child_die, cu);
15898 else if (child_die->tag == DW_TAG_template_type_param
15899 || child_die->tag == DW_TAG_template_value_param)
15900 {
15901 struct symbol *arg = new_symbol (child_die, NULL, cu);
15902
15903 if (arg != NULL)
15904 template_args->push_back (arg);
15905 }
15906 else if (child_die->tag == DW_TAG_variant)
15907 {
15908 /* In a variant we want to get the discriminant and also add a
15909 field for our sole member child. */
15910 struct attribute *discr = dwarf2_attr (child_die, DW_AT_discr_value, cu);
15911
15912 for (struct die_info *variant_child = child_die->child;
15913 variant_child != NULL;
15914 variant_child = sibling_die (variant_child))
15915 {
15916 if (variant_child->tag == DW_TAG_member)
15917 {
15918 handle_struct_member_die (variant_child, type, fi,
15919 template_args, cu);
15920 /* Only handle the one. */
15921 break;
15922 }
15923 }
15924
15925 /* We don't handle this but we might as well report it if we see
15926 it. */
15927 if (dwarf2_attr (child_die, DW_AT_discr_list, cu) != nullptr)
15928 complaint (&symfile_complaints,
15929 _("DW_AT_discr_list is not supported yet"
15930 " - DIE at %s [in module %s]"),
15931 sect_offset_str (child_die->sect_off),
15932 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15933
15934 /* The first field was just added, so we can stash the
15935 discriminant there. */
be2daae6 15936 gdb_assert (!fi->fields.empty ());
2ddeaf8a 15937 if (discr == NULL)
be2daae6 15938 fi->fields.back ().variant.default_branch = true;
2ddeaf8a 15939 else
be2daae6 15940 fi->fields.back ().variant.discriminant_value = DW_UNSND (discr);
2ddeaf8a
TT
15941 }
15942}
15943
c767944b
DJ
15944/* Finish creating a structure or union type, including filling in
15945 its members and creating a symbol for it. */
15946
15947static void
15948process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
15949{
518817b3 15950 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
ca040673 15951 struct die_info *child_die;
c767944b
DJ
15952 struct type *type;
15953
15954 type = get_die_type (die, cu);
15955 if (type == NULL)
15956 type = read_structure_type (die, cu);
15957
2ddeaf8a
TT
15958 /* When reading a DW_TAG_variant_part, we need to notice when we
15959 read the discriminant member, so we can record it later in the
15960 discriminant_info. */
15961 bool is_variant_part = TYPE_FLAG_DISCRIMINATED_UNION (type);
15962 sect_offset discr_offset;
15963
15964 if (is_variant_part)
15965 {
15966 struct attribute *discr = dwarf2_attr (die, DW_AT_discr, cu);
15967 if (discr == NULL)
15968 {
15969 /* Maybe it's a univariant form, an extension we support.
15970 In this case arrange not to check the offset. */
15971 is_variant_part = false;
15972 }
15973 else if (attr_form_is_ref (discr))
15974 {
15975 struct dwarf2_cu *target_cu = cu;
15976 struct die_info *target_die = follow_die_ref (die, discr, &target_cu);
15977
15978 discr_offset = target_die->sect_off;
15979 }
15980 else
15981 {
15982 complaint (&symfile_complaints,
15983 _("DW_AT_discr does not have DIE reference form"
15984 " - DIE at %s [in module %s]"),
15985 sect_offset_str (die->sect_off),
15986 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15987 is_variant_part = false;
15988 }
15989 }
15990
e142c38c 15991 if (die->child != NULL && ! die_is_declaration (die, cu))
c906108c
SS
15992 {
15993 struct field_info fi;
2f4732b0 15994 std::vector<struct symbol *> template_args;
c906108c 15995
639d11d3 15996 child_die = die->child;
c906108c
SS
15997
15998 while (child_die && child_die->tag)
15999 {
2ddeaf8a 16000 handle_struct_member_die (child_die, type, &fi, &template_args, cu);
34eaf542 16001
2ddeaf8a 16002 if (is_variant_part && discr_offset == child_die->sect_off)
be2daae6 16003 fi.fields.back ().variant.is_discriminant = true;
34eaf542 16004
c906108c
SS
16005 child_die = sibling_die (child_die);
16006 }
16007
34eaf542 16008 /* Attach template arguments to type. */
2f4732b0 16009 if (!template_args.empty ())
34eaf542
TT
16010 {
16011 ALLOCATE_CPLUS_STRUCT_TYPE (type);
2f4732b0 16012 TYPE_N_TEMPLATE_ARGUMENTS (type) = template_args.size ();
34eaf542 16013 TYPE_TEMPLATE_ARGUMENTS (type)
8d749320
SM
16014 = XOBNEWVEC (&objfile->objfile_obstack,
16015 struct symbol *,
16016 TYPE_N_TEMPLATE_ARGUMENTS (type));
34eaf542 16017 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
2f4732b0 16018 template_args.data (),
34eaf542
TT
16019 (TYPE_N_TEMPLATE_ARGUMENTS (type)
16020 * sizeof (struct symbol *)));
34eaf542
TT
16021 }
16022
c906108c
SS
16023 /* Attach fields and member functions to the type. */
16024 if (fi.nfields)
e7c27a73 16025 dwarf2_attach_fields_to_type (&fi, type, cu);
be2daae6 16026 if (!fi.fnfieldlists.empty ())
c906108c 16027 {
e7c27a73 16028 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
c906108c 16029
c5aa993b 16030 /* Get the type which refers to the base class (possibly this
c906108c 16031 class itself) which contains the vtable pointer for the current
0d564a31
DJ
16032 class from the DW_AT_containing_type attribute. This use of
16033 DW_AT_containing_type is a GNU extension. */
c906108c 16034
e142c38c 16035 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
c906108c 16036 {
e7c27a73 16037 struct type *t = die_containing_type (die, cu);
c906108c 16038
ae6ae975 16039 set_type_vptr_basetype (type, t);
c906108c
SS
16040 if (type == t)
16041 {
c906108c
SS
16042 int i;
16043
16044 /* Our own class provides vtbl ptr. */
16045 for (i = TYPE_NFIELDS (t) - 1;
16046 i >= TYPE_N_BASECLASSES (t);
16047 --i)
16048 {
0d5cff50 16049 const char *fieldname = TYPE_FIELD_NAME (t, i);
c906108c 16050
1168df01 16051 if (is_vtable_name (fieldname, cu))
c906108c 16052 {
ae6ae975 16053 set_type_vptr_fieldno (type, i);
c906108c
SS
16054 break;
16055 }
16056 }
16057
16058 /* Complain if virtual function table field not found. */
16059 if (i < TYPE_N_BASECLASSES (t))
4d3c2250 16060 complaint (&symfile_complaints,
3e43a32a
MS
16061 _("virtual function table pointer "
16062 "not found when defining class '%s'"),
4d3c2250
KB
16063 TYPE_TAG_NAME (type) ? TYPE_TAG_NAME (type) :
16064 "");
c906108c
SS
16065 }
16066 else
16067 {
ae6ae975 16068 set_type_vptr_fieldno (type, TYPE_VPTR_FIELDNO (t));
c906108c
SS
16069 }
16070 }
f6235d4c 16071 else if (cu->producer
61012eef 16072 && startswith (cu->producer, "IBM(R) XL C/C++ Advanced Edition"))
f6235d4c
EZ
16073 {
16074 /* The IBM XLC compiler does not provide direct indication
16075 of the containing type, but the vtable pointer is
16076 always named __vfp. */
16077
16078 int i;
16079
16080 for (i = TYPE_NFIELDS (type) - 1;
16081 i >= TYPE_N_BASECLASSES (type);
16082 --i)
16083 {
16084 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
16085 {
ae6ae975
DE
16086 set_type_vptr_fieldno (type, i);
16087 set_type_vptr_basetype (type, type);
f6235d4c
EZ
16088 break;
16089 }
16090 }
16091 }
c906108c 16092 }
98751a41
JK
16093
16094 /* Copy fi.typedef_field_list linked list elements content into the
16095 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
be2daae6 16096 if (!fi.typedef_field_list.empty ())
98751a41 16097 {
be2daae6 16098 int count = fi.typedef_field_list.size ();
98751a41 16099
a0d7a4ff 16100 ALLOCATE_CPLUS_STRUCT_TYPE (type);
98751a41 16101 TYPE_TYPEDEF_FIELD_ARRAY (type)
883fd55a 16102 = ((struct decl_field *)
be2daae6
TT
16103 TYPE_ALLOC (type,
16104 sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * count));
16105 TYPE_TYPEDEF_FIELD_COUNT (type) = count;
6e70227d 16106
be2daae6
TT
16107 for (int i = 0; i < fi.typedef_field_list.size (); ++i)
16108 TYPE_TYPEDEF_FIELD (type, i) = fi.typedef_field_list[i];
98751a41 16109 }
c767944b 16110
883fd55a
KS
16111 /* Copy fi.nested_types_list linked list elements content into the
16112 allocated array TYPE_NESTED_TYPES_ARRAY (type). */
be2daae6 16113 if (!fi.nested_types_list.empty () && cu->language != language_ada)
883fd55a 16114 {
be2daae6 16115 int count = fi.nested_types_list.size ();
883fd55a
KS
16116
16117 ALLOCATE_CPLUS_STRUCT_TYPE (type);
16118 TYPE_NESTED_TYPES_ARRAY (type)
16119 = ((struct decl_field *)
be2daae6
TT
16120 TYPE_ALLOC (type, sizeof (struct decl_field) * count));
16121 TYPE_NESTED_TYPES_COUNT (type) = count;
883fd55a 16122
be2daae6
TT
16123 for (int i = 0; i < fi.nested_types_list.size (); ++i)
16124 TYPE_NESTED_TYPES_FIELD (type, i) = fi.nested_types_list[i];
883fd55a 16125 }
c906108c 16126 }
63d06c5c 16127
bb5ed363 16128 quirk_gcc_member_function_pointer (type, objfile);
c9317f21
TT
16129 if (cu->language == language_rust && die->tag == DW_TAG_union_type)
16130 cu->rust_unions.push_back (type);
0b92b5bb 16131
90aeadfc
DC
16132 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
16133 snapshots) has been known to create a die giving a declaration
16134 for a class that has, as a child, a die giving a definition for a
16135 nested class. So we have to process our children even if the
16136 current die is a declaration. Normally, of course, a declaration
16137 won't have any children at all. */
134d01f1 16138
ca040673
DE
16139 child_die = die->child;
16140
90aeadfc
DC
16141 while (child_die != NULL && child_die->tag)
16142 {
16143 if (child_die->tag == DW_TAG_member
16144 || child_die->tag == DW_TAG_variable
34eaf542
TT
16145 || child_die->tag == DW_TAG_inheritance
16146 || child_die->tag == DW_TAG_template_value_param
16147 || child_die->tag == DW_TAG_template_type_param)
134d01f1 16148 {
90aeadfc 16149 /* Do nothing. */
134d01f1 16150 }
90aeadfc
DC
16151 else
16152 process_die (child_die, cu);
134d01f1 16153
90aeadfc 16154 child_die = sibling_die (child_die);
134d01f1
DJ
16155 }
16156
fa4028e9
JB
16157 /* Do not consider external references. According to the DWARF standard,
16158 these DIEs are identified by the fact that they have no byte_size
16159 attribute, and a declaration attribute. */
16160 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
16161 || !die_is_declaration (die, cu))
c767944b 16162 new_symbol (die, type, cu);
134d01f1
DJ
16163}
16164
55426c9d
JB
16165/* Assuming DIE is an enumeration type, and TYPE is its associated type,
16166 update TYPE using some information only available in DIE's children. */
16167
16168static void
16169update_enumeration_type_from_children (struct die_info *die,
16170 struct type *type,
16171 struct dwarf2_cu *cu)
16172{
60f7655a 16173 struct die_info *child_die;
55426c9d
JB
16174 int unsigned_enum = 1;
16175 int flag_enum = 1;
16176 ULONGEST mask = 0;
55426c9d 16177
8268c778 16178 auto_obstack obstack;
55426c9d 16179
60f7655a
DE
16180 for (child_die = die->child;
16181 child_die != NULL && child_die->tag;
16182 child_die = sibling_die (child_die))
55426c9d
JB
16183 {
16184 struct attribute *attr;
16185 LONGEST value;
16186 const gdb_byte *bytes;
16187 struct dwarf2_locexpr_baton *baton;
16188 const char *name;
60f7655a 16189
55426c9d
JB
16190 if (child_die->tag != DW_TAG_enumerator)
16191 continue;
16192
16193 attr = dwarf2_attr (child_die, DW_AT_const_value, cu);
16194 if (attr == NULL)
16195 continue;
16196
16197 name = dwarf2_name (child_die, cu);
16198 if (name == NULL)
16199 name = "<anonymous enumerator>";
16200
16201 dwarf2_const_value_attr (attr, type, name, &obstack, cu,
16202 &value, &bytes, &baton);
16203 if (value < 0)
16204 {
16205 unsigned_enum = 0;
16206 flag_enum = 0;
16207 }
16208 else if ((mask & value) != 0)
16209 flag_enum = 0;
16210 else
16211 mask |= value;
16212
16213 /* If we already know that the enum type is neither unsigned, nor
16214 a flag type, no need to look at the rest of the enumerates. */
16215 if (!unsigned_enum && !flag_enum)
16216 break;
55426c9d
JB
16217 }
16218
16219 if (unsigned_enum)
16220 TYPE_UNSIGNED (type) = 1;
16221 if (flag_enum)
16222 TYPE_FLAG_ENUM (type) = 1;
55426c9d
JB
16223}
16224
134d01f1
DJ
16225/* Given a DW_AT_enumeration_type die, set its type. We do not
16226 complete the type's fields yet, or create any symbols. */
c906108c 16227
f792889a 16228static struct type *
134d01f1 16229read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16230{
518817b3 16231 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c 16232 struct type *type;
c906108c 16233 struct attribute *attr;
0114d602 16234 const char *name;
134d01f1 16235
348e048f
DE
16236 /* If the definition of this type lives in .debug_types, read that type.
16237 Don't follow DW_AT_specification though, that will take us back up
16238 the chain and we want to go down. */
45e58e77 16239 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
348e048f
DE
16240 if (attr)
16241 {
ac9ec31b 16242 type = get_DW_AT_signature_type (die, attr, cu);
9dc481d3 16243
ac9ec31b 16244 /* The type's CU may not be the same as CU.
02142a6c 16245 Ensure TYPE is recorded with CU in die_type_hash. */
348e048f
DE
16246 return set_die_type (die, type, cu);
16247 }
16248
c906108c
SS
16249 type = alloc_type (objfile);
16250
16251 TYPE_CODE (type) = TYPE_CODE_ENUM;
94af9270 16252 name = dwarf2_full_name (NULL, die, cu);
39cbfefa 16253 if (name != NULL)
7d455152 16254 TYPE_TAG_NAME (type) = name;
c906108c 16255
0626fc76
TT
16256 attr = dwarf2_attr (die, DW_AT_type, cu);
16257 if (attr != NULL)
16258 {
16259 struct type *underlying_type = die_type (die, cu);
16260
16261 TYPE_TARGET_TYPE (type) = underlying_type;
16262 }
16263
e142c38c 16264 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
16265 if (attr)
16266 {
16267 TYPE_LENGTH (type) = DW_UNSND (attr);
16268 }
16269 else
16270 {
16271 TYPE_LENGTH (type) = 0;
16272 }
16273
137033e9
JB
16274 /* The enumeration DIE can be incomplete. In Ada, any type can be
16275 declared as private in the package spec, and then defined only
16276 inside the package body. Such types are known as Taft Amendment
16277 Types. When another package uses such a type, an incomplete DIE
16278 may be generated by the compiler. */
02eb380e 16279 if (die_is_declaration (die, cu))
876cecd0 16280 TYPE_STUB (type) = 1;
02eb380e 16281
0626fc76
TT
16282 /* Finish the creation of this type by using the enum's children.
16283 We must call this even when the underlying type has been provided
16284 so that we can determine if we're looking at a "flag" enum. */
55426c9d
JB
16285 update_enumeration_type_from_children (die, type, cu);
16286
0626fc76
TT
16287 /* If this type has an underlying type that is not a stub, then we
16288 may use its attributes. We always use the "unsigned" attribute
16289 in this situation, because ordinarily we guess whether the type
16290 is unsigned -- but the guess can be wrong and the underlying type
16291 can tell us the reality. However, we defer to a local size
16292 attribute if one exists, because this lets the compiler override
16293 the underlying type if needed. */
16294 if (TYPE_TARGET_TYPE (type) != NULL && !TYPE_STUB (TYPE_TARGET_TYPE (type)))
16295 {
16296 TYPE_UNSIGNED (type) = TYPE_UNSIGNED (TYPE_TARGET_TYPE (type));
16297 if (TYPE_LENGTH (type) == 0)
16298 TYPE_LENGTH (type) = TYPE_LENGTH (TYPE_TARGET_TYPE (type));
16299 }
16300
3d567982
TT
16301 TYPE_DECLARED_CLASS (type) = dwarf2_flag_true_p (die, DW_AT_enum_class, cu);
16302
f792889a 16303 return set_die_type (die, type, cu);
134d01f1
DJ
16304}
16305
16306/* Given a pointer to a die which begins an enumeration, process all
16307 the dies that define the members of the enumeration, and create the
16308 symbol for the enumeration type.
16309
16310 NOTE: We reverse the order of the element list. */
16311
16312static void
16313process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
16314{
f792889a 16315 struct type *this_type;
134d01f1 16316
f792889a
DJ
16317 this_type = get_die_type (die, cu);
16318 if (this_type == NULL)
16319 this_type = read_enumeration_type (die, cu);
9dc481d3 16320
639d11d3 16321 if (die->child != NULL)
c906108c 16322 {
9dc481d3
DE
16323 struct die_info *child_die;
16324 struct symbol *sym;
16325 struct field *fields = NULL;
16326 int num_fields = 0;
15d034d0 16327 const char *name;
9dc481d3 16328
639d11d3 16329 child_die = die->child;
c906108c
SS
16330 while (child_die && child_die->tag)
16331 {
16332 if (child_die->tag != DW_TAG_enumerator)
16333 {
e7c27a73 16334 process_die (child_die, cu);
c906108c
SS
16335 }
16336 else
16337 {
39cbfefa
DJ
16338 name = dwarf2_name (child_die, cu);
16339 if (name)
c906108c 16340 {
f792889a 16341 sym = new_symbol (child_die, this_type, cu);
c906108c
SS
16342
16343 if ((num_fields % DW_FIELD_ALLOC_CHUNK) == 0)
16344 {
16345 fields = (struct field *)
16346 xrealloc (fields,
16347 (num_fields + DW_FIELD_ALLOC_CHUNK)
c5aa993b 16348 * sizeof (struct field));
c906108c
SS
16349 }
16350
3567439c 16351 FIELD_NAME (fields[num_fields]) = SYMBOL_LINKAGE_NAME (sym);
c906108c 16352 FIELD_TYPE (fields[num_fields]) = NULL;
14e75d8e 16353 SET_FIELD_ENUMVAL (fields[num_fields], SYMBOL_VALUE (sym));
c906108c
SS
16354 FIELD_BITSIZE (fields[num_fields]) = 0;
16355
16356 num_fields++;
16357 }
16358 }
16359
16360 child_die = sibling_die (child_die);
16361 }
16362
16363 if (num_fields)
16364 {
f792889a
DJ
16365 TYPE_NFIELDS (this_type) = num_fields;
16366 TYPE_FIELDS (this_type) = (struct field *)
16367 TYPE_ALLOC (this_type, sizeof (struct field) * num_fields);
16368 memcpy (TYPE_FIELDS (this_type), fields,
c906108c 16369 sizeof (struct field) * num_fields);
b8c9b27d 16370 xfree (fields);
c906108c 16371 }
c906108c 16372 }
134d01f1 16373
6c83ed52
TT
16374 /* If we are reading an enum from a .debug_types unit, and the enum
16375 is a declaration, and the enum is not the signatured type in the
16376 unit, then we do not want to add a symbol for it. Adding a
16377 symbol would in some cases obscure the true definition of the
16378 enum, giving users an incomplete type when the definition is
16379 actually available. Note that we do not want to do this for all
16380 enums which are just declarations, because C++0x allows forward
16381 enum declarations. */
3019eac3 16382 if (cu->per_cu->is_debug_types
6c83ed52
TT
16383 && die_is_declaration (die, cu))
16384 {
52dc124a 16385 struct signatured_type *sig_type;
6c83ed52 16386
c0f78cd4 16387 sig_type = (struct signatured_type *) cu->per_cu;
9c541725
PA
16388 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
16389 if (sig_type->type_offset_in_section != die->sect_off)
6c83ed52
TT
16390 return;
16391 }
16392
f792889a 16393 new_symbol (die, this_type, cu);
c906108c
SS
16394}
16395
16396/* Extract all information from a DW_TAG_array_type DIE and put it in
16397 the DIE's type field. For now, this only handles one dimensional
16398 arrays. */
16399
f792889a 16400static struct type *
e7c27a73 16401read_array_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16402{
518817b3 16403 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c 16404 struct die_info *child_die;
7e314c57 16405 struct type *type;
c906108c 16406 struct type *element_type, *range_type, *index_type;
c906108c 16407 struct attribute *attr;
15d034d0 16408 const char *name;
a405673c 16409 struct dynamic_prop *byte_stride_prop = NULL;
dc53a7ad 16410 unsigned int bit_stride = 0;
c906108c 16411
e7c27a73 16412 element_type = die_type (die, cu);
c906108c 16413
7e314c57
JK
16414 /* The die_type call above may have already set the type for this DIE. */
16415 type = get_die_type (die, cu);
16416 if (type)
16417 return type;
16418
dc53a7ad
JB
16419 attr = dwarf2_attr (die, DW_AT_byte_stride, cu);
16420 if (attr != NULL)
a405673c
JB
16421 {
16422 int stride_ok;
16423
16424 byte_stride_prop
16425 = (struct dynamic_prop *) alloca (sizeof (struct dynamic_prop));
16426 stride_ok = attr_to_dynamic_prop (attr, die, cu, byte_stride_prop);
16427 if (!stride_ok)
16428 {
16429 complaint (&symfile_complaints,
16430 _("unable to read array DW_AT_byte_stride "
9d8780f0
SM
16431 " - DIE at %s [in module %s]"),
16432 sect_offset_str (die->sect_off),
518817b3 16433 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
a405673c
JB
16434 /* Ignore this attribute. We will likely not be able to print
16435 arrays of this type correctly, but there is little we can do
16436 to help if we cannot read the attribute's value. */
16437 byte_stride_prop = NULL;
16438 }
16439 }
dc53a7ad
JB
16440
16441 attr = dwarf2_attr (die, DW_AT_bit_stride, cu);
16442 if (attr != NULL)
16443 bit_stride = DW_UNSND (attr);
16444
c906108c
SS
16445 /* Irix 6.2 native cc creates array types without children for
16446 arrays with unspecified length. */
639d11d3 16447 if (die->child == NULL)
c906108c 16448 {
46bf5051 16449 index_type = objfile_type (objfile)->builtin_int;
0c9c3474 16450 range_type = create_static_range_type (NULL, index_type, 0, -1);
dc53a7ad 16451 type = create_array_type_with_stride (NULL, element_type, range_type,
a405673c 16452 byte_stride_prop, bit_stride);
f792889a 16453 return set_die_type (die, type, cu);
c906108c
SS
16454 }
16455
791afaa2 16456 std::vector<struct type *> range_types;
639d11d3 16457 child_die = die->child;
c906108c
SS
16458 while (child_die && child_die->tag)
16459 {
16460 if (child_die->tag == DW_TAG_subrange_type)
16461 {
f792889a 16462 struct type *child_type = read_type_die (child_die, cu);
9a619af0 16463
f792889a 16464 if (child_type != NULL)
a02abb62 16465 {
0963b4bd
MS
16466 /* The range type was succesfully read. Save it for the
16467 array type creation. */
791afaa2 16468 range_types.push_back (child_type);
a02abb62 16469 }
c906108c
SS
16470 }
16471 child_die = sibling_die (child_die);
16472 }
16473
16474 /* Dwarf2 dimensions are output from left to right, create the
16475 necessary array types in backwards order. */
7ca2d3a3 16476
c906108c 16477 type = element_type;
7ca2d3a3
DL
16478
16479 if (read_array_order (die, cu) == DW_ORD_col_major)
16480 {
16481 int i = 0;
9a619af0 16482
791afaa2 16483 while (i < range_types.size ())
dc53a7ad 16484 type = create_array_type_with_stride (NULL, type, range_types[i++],
a405673c 16485 byte_stride_prop, bit_stride);
7ca2d3a3
DL
16486 }
16487 else
16488 {
791afaa2 16489 size_t ndim = range_types.size ();
7ca2d3a3 16490 while (ndim-- > 0)
dc53a7ad 16491 type = create_array_type_with_stride (NULL, type, range_types[ndim],
a405673c 16492 byte_stride_prop, bit_stride);
7ca2d3a3 16493 }
c906108c 16494
f5f8a009
EZ
16495 /* Understand Dwarf2 support for vector types (like they occur on
16496 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
16497 array type. This is not part of the Dwarf2/3 standard yet, but a
16498 custom vendor extension. The main difference between a regular
16499 array and the vector variant is that vectors are passed by value
16500 to functions. */
e142c38c 16501 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
f5f8a009 16502 if (attr)
ea37ba09 16503 make_vector_type (type);
f5f8a009 16504
dbc98a8b
KW
16505 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
16506 implementation may choose to implement triple vectors using this
16507 attribute. */
16508 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16509 if (attr)
16510 {
16511 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
16512 TYPE_LENGTH (type) = DW_UNSND (attr);
16513 else
3e43a32a
MS
16514 complaint (&symfile_complaints,
16515 _("DW_AT_byte_size for array type smaller "
16516 "than the total size of elements"));
dbc98a8b
KW
16517 }
16518
39cbfefa
DJ
16519 name = dwarf2_name (die, cu);
16520 if (name)
16521 TYPE_NAME (type) = name;
6e70227d 16522
0963b4bd 16523 /* Install the type in the die. */
7e314c57
JK
16524 set_die_type (die, type, cu);
16525
16526 /* set_die_type should be already done. */
b4ba55a1
JB
16527 set_descriptive_type (type, die, cu);
16528
7e314c57 16529 return type;
c906108c
SS
16530}
16531
7ca2d3a3 16532static enum dwarf_array_dim_ordering
6e70227d 16533read_array_order (struct die_info *die, struct dwarf2_cu *cu)
7ca2d3a3
DL
16534{
16535 struct attribute *attr;
16536
16537 attr = dwarf2_attr (die, DW_AT_ordering, cu);
16538
aead7601
SM
16539 if (attr)
16540 return (enum dwarf_array_dim_ordering) DW_SND (attr);
7ca2d3a3 16541
0963b4bd
MS
16542 /* GNU F77 is a special case, as at 08/2004 array type info is the
16543 opposite order to the dwarf2 specification, but data is still
16544 laid out as per normal fortran.
7ca2d3a3 16545
0963b4bd
MS
16546 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
16547 version checking. */
7ca2d3a3 16548
905e0470
PM
16549 if (cu->language == language_fortran
16550 && cu->producer && strstr (cu->producer, "GNU F77"))
7ca2d3a3
DL
16551 {
16552 return DW_ORD_row_major;
16553 }
16554
6e70227d 16555 switch (cu->language_defn->la_array_ordering)
7ca2d3a3
DL
16556 {
16557 case array_column_major:
16558 return DW_ORD_col_major;
16559 case array_row_major:
16560 default:
16561 return DW_ORD_row_major;
16562 };
16563}
16564
72019c9c 16565/* Extract all information from a DW_TAG_set_type DIE and put it in
0963b4bd 16566 the DIE's type field. */
72019c9c 16567
f792889a 16568static struct type *
72019c9c
GM
16569read_set_type (struct die_info *die, struct dwarf2_cu *cu)
16570{
7e314c57
JK
16571 struct type *domain_type, *set_type;
16572 struct attribute *attr;
f792889a 16573
7e314c57
JK
16574 domain_type = die_type (die, cu);
16575
16576 /* The die_type call above may have already set the type for this DIE. */
16577 set_type = get_die_type (die, cu);
16578 if (set_type)
16579 return set_type;
16580
16581 set_type = create_set_type (NULL, domain_type);
16582
16583 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
d09039dd
PM
16584 if (attr)
16585 TYPE_LENGTH (set_type) = DW_UNSND (attr);
7e314c57 16586
f792889a 16587 return set_die_type (die, set_type, cu);
72019c9c 16588}
7ca2d3a3 16589
0971de02
TT
16590/* A helper for read_common_block that creates a locexpr baton.
16591 SYM is the symbol which we are marking as computed.
16592 COMMON_DIE is the DIE for the common block.
16593 COMMON_LOC is the location expression attribute for the common
16594 block itself.
16595 MEMBER_LOC is the location expression attribute for the particular
16596 member of the common block that we are processing.
16597 CU is the CU from which the above come. */
16598
16599static void
16600mark_common_block_symbol_computed (struct symbol *sym,
16601 struct die_info *common_die,
16602 struct attribute *common_loc,
16603 struct attribute *member_loc,
16604 struct dwarf2_cu *cu)
16605{
518817b3
SM
16606 struct dwarf2_per_objfile *dwarf2_per_objfile
16607 = cu->per_cu->dwarf2_per_objfile;
0971de02
TT
16608 struct objfile *objfile = dwarf2_per_objfile->objfile;
16609 struct dwarf2_locexpr_baton *baton;
16610 gdb_byte *ptr;
16611 unsigned int cu_off;
16612 enum bfd_endian byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
16613 LONGEST offset = 0;
16614
16615 gdb_assert (common_loc && member_loc);
16616 gdb_assert (attr_form_is_block (common_loc));
16617 gdb_assert (attr_form_is_block (member_loc)
16618 || attr_form_is_constant (member_loc));
16619
8d749320 16620 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
0971de02
TT
16621 baton->per_cu = cu->per_cu;
16622 gdb_assert (baton->per_cu);
16623
16624 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
16625
16626 if (attr_form_is_constant (member_loc))
16627 {
16628 offset = dwarf2_get_attr_constant_value (member_loc, 0);
16629 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
16630 }
16631 else
16632 baton->size += DW_BLOCK (member_loc)->size;
16633
224c3ddb 16634 ptr = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, baton->size);
0971de02
TT
16635 baton->data = ptr;
16636
16637 *ptr++ = DW_OP_call4;
9c541725 16638 cu_off = common_die->sect_off - cu->per_cu->sect_off;
0971de02
TT
16639 store_unsigned_integer (ptr, 4, byte_order, cu_off);
16640 ptr += 4;
16641
16642 if (attr_form_is_constant (member_loc))
16643 {
16644 *ptr++ = DW_OP_addr;
16645 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
16646 ptr += cu->header.addr_size;
16647 }
16648 else
16649 {
16650 /* We have to copy the data here, because DW_OP_call4 will only
16651 use a DW_AT_location attribute. */
16652 memcpy (ptr, DW_BLOCK (member_loc)->data, DW_BLOCK (member_loc)->size);
16653 ptr += DW_BLOCK (member_loc)->size;
16654 }
16655
16656 *ptr++ = DW_OP_plus;
16657 gdb_assert (ptr - baton->data == baton->size);
16658
0971de02 16659 SYMBOL_LOCATION_BATON (sym) = baton;
f1e6e072 16660 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
0971de02
TT
16661}
16662
4357ac6c
TT
16663/* Create appropriate locally-scoped variables for all the
16664 DW_TAG_common_block entries. Also create a struct common_block
16665 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
16666 is used to sepate the common blocks name namespace from regular
16667 variable names. */
c906108c
SS
16668
16669static void
e7c27a73 16670read_common_block (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16671{
0971de02
TT
16672 struct attribute *attr;
16673
16674 attr = dwarf2_attr (die, DW_AT_location, cu);
16675 if (attr)
16676 {
16677 /* Support the .debug_loc offsets. */
16678 if (attr_form_is_block (attr))
16679 {
16680 /* Ok. */
16681 }
16682 else if (attr_form_is_section_offset (attr))
16683 {
16684 dwarf2_complex_location_expr_complaint ();
16685 attr = NULL;
16686 }
16687 else
16688 {
16689 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
16690 "common block member");
16691 attr = NULL;
16692 }
16693 }
16694
639d11d3 16695 if (die->child != NULL)
c906108c 16696 {
518817b3 16697 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
4357ac6c
TT
16698 struct die_info *child_die;
16699 size_t n_entries = 0, size;
16700 struct common_block *common_block;
16701 struct symbol *sym;
74ac6d43 16702
4357ac6c
TT
16703 for (child_die = die->child;
16704 child_die && child_die->tag;
16705 child_die = sibling_die (child_die))
16706 ++n_entries;
16707
16708 size = (sizeof (struct common_block)
16709 + (n_entries - 1) * sizeof (struct symbol *));
224c3ddb
SM
16710 common_block
16711 = (struct common_block *) obstack_alloc (&objfile->objfile_obstack,
16712 size);
4357ac6c
TT
16713 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
16714 common_block->n_entries = 0;
16715
16716 for (child_die = die->child;
16717 child_die && child_die->tag;
16718 child_die = sibling_die (child_die))
16719 {
16720 /* Create the symbol in the DW_TAG_common_block block in the current
16721 symbol scope. */
e7c27a73 16722 sym = new_symbol (child_die, NULL, cu);
0971de02
TT
16723 if (sym != NULL)
16724 {
16725 struct attribute *member_loc;
16726
16727 common_block->contents[common_block->n_entries++] = sym;
16728
16729 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
16730 cu);
16731 if (member_loc)
16732 {
16733 /* GDB has handled this for a long time, but it is
16734 not specified by DWARF. It seems to have been
16735 emitted by gfortran at least as recently as:
16736 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
16737 complaint (&symfile_complaints,
16738 _("Variable in common block has "
16739 "DW_AT_data_member_location "
9d8780f0
SM
16740 "- DIE at %s [in module %s]"),
16741 sect_offset_str (child_die->sect_off),
518817b3 16742 objfile_name (objfile));
0971de02
TT
16743
16744 if (attr_form_is_section_offset (member_loc))
16745 dwarf2_complex_location_expr_complaint ();
16746 else if (attr_form_is_constant (member_loc)
16747 || attr_form_is_block (member_loc))
16748 {
16749 if (attr)
16750 mark_common_block_symbol_computed (sym, die, attr,
16751 member_loc, cu);
16752 }
16753 else
16754 dwarf2_complex_location_expr_complaint ();
16755 }
16756 }
c906108c 16757 }
4357ac6c
TT
16758
16759 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
16760 SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
c906108c
SS
16761 }
16762}
16763
0114d602 16764/* Create a type for a C++ namespace. */
d9fa45fe 16765
0114d602
DJ
16766static struct type *
16767read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
d9fa45fe 16768{
518817b3 16769 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
0114d602 16770 const char *previous_prefix, *name;
9219021c 16771 int is_anonymous;
0114d602
DJ
16772 struct type *type;
16773
16774 /* For extensions, reuse the type of the original namespace. */
16775 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
16776 {
16777 struct die_info *ext_die;
16778 struct dwarf2_cu *ext_cu = cu;
9a619af0 16779
0114d602
DJ
16780 ext_die = dwarf2_extension (die, &ext_cu);
16781 type = read_type_die (ext_die, ext_cu);
9dc481d3
DE
16782
16783 /* EXT_CU may not be the same as CU.
02142a6c 16784 Ensure TYPE is recorded with CU in die_type_hash. */
0114d602
DJ
16785 return set_die_type (die, type, cu);
16786 }
9219021c 16787
e142c38c 16788 name = namespace_name (die, &is_anonymous, cu);
9219021c
DC
16789
16790 /* Now build the name of the current namespace. */
16791
0114d602
DJ
16792 previous_prefix = determine_prefix (die, cu);
16793 if (previous_prefix[0] != '\0')
16794 name = typename_concat (&objfile->objfile_obstack,
f55ee35c 16795 previous_prefix, name, 0, cu);
0114d602
DJ
16796
16797 /* Create the type. */
19f392bc 16798 type = init_type (objfile, TYPE_CODE_NAMESPACE, 0, name);
0114d602
DJ
16799 TYPE_TAG_NAME (type) = TYPE_NAME (type);
16800
60531b24 16801 return set_die_type (die, type, cu);
0114d602
DJ
16802}
16803
22cee43f 16804/* Read a namespace scope. */
0114d602
DJ
16805
16806static void
16807read_namespace (struct die_info *die, struct dwarf2_cu *cu)
16808{
518817b3 16809 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
0114d602 16810 int is_anonymous;
9219021c 16811
5c4e30ca
DC
16812 /* Add a symbol associated to this if we haven't seen the namespace
16813 before. Also, add a using directive if it's an anonymous
16814 namespace. */
9219021c 16815
f2f0e013 16816 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
5c4e30ca
DC
16817 {
16818 struct type *type;
16819
0114d602 16820 type = read_type_die (die, cu);
e7c27a73 16821 new_symbol (die, type, cu);
5c4e30ca 16822
e8e80198 16823 namespace_name (die, &is_anonymous, cu);
5c4e30ca 16824 if (is_anonymous)
0114d602
DJ
16825 {
16826 const char *previous_prefix = determine_prefix (die, cu);
9a619af0 16827
eb1e02fd 16828 std::vector<const char *> excludes;
22cee43f
PMR
16829 add_using_directive (using_directives (cu->language),
16830 previous_prefix, TYPE_NAME (type), NULL,
eb1e02fd 16831 NULL, excludes, 0, &objfile->objfile_obstack);
0114d602 16832 }
5c4e30ca 16833 }
9219021c 16834
639d11d3 16835 if (die->child != NULL)
d9fa45fe 16836 {
639d11d3 16837 struct die_info *child_die = die->child;
6e70227d 16838
d9fa45fe
DC
16839 while (child_die && child_die->tag)
16840 {
e7c27a73 16841 process_die (child_die, cu);
d9fa45fe
DC
16842 child_die = sibling_die (child_die);
16843 }
16844 }
38d518c9
EZ
16845}
16846
f55ee35c
JK
16847/* Read a Fortran module as type. This DIE can be only a declaration used for
16848 imported module. Still we need that type as local Fortran "use ... only"
16849 declaration imports depend on the created type in determine_prefix. */
16850
16851static struct type *
16852read_module_type (struct die_info *die, struct dwarf2_cu *cu)
16853{
518817b3 16854 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15d034d0 16855 const char *module_name;
f55ee35c
JK
16856 struct type *type;
16857
16858 module_name = dwarf2_name (die, cu);
16859 if (!module_name)
3e43a32a 16860 complaint (&symfile_complaints,
9d8780f0
SM
16861 _("DW_TAG_module has no name, offset %s"),
16862 sect_offset_str (die->sect_off));
19f392bc 16863 type = init_type (objfile, TYPE_CODE_MODULE, 0, module_name);
f55ee35c
JK
16864
16865 /* determine_prefix uses TYPE_TAG_NAME. */
16866 TYPE_TAG_NAME (type) = TYPE_NAME (type);
16867
16868 return set_die_type (die, type, cu);
16869}
16870
5d7cb8df
JK
16871/* Read a Fortran module. */
16872
16873static void
16874read_module (struct die_info *die, struct dwarf2_cu *cu)
16875{
16876 struct die_info *child_die = die->child;
530e8392
KB
16877 struct type *type;
16878
16879 type = read_type_die (die, cu);
16880 new_symbol (die, type, cu);
5d7cb8df 16881
5d7cb8df
JK
16882 while (child_die && child_die->tag)
16883 {
16884 process_die (child_die, cu);
16885 child_die = sibling_die (child_die);
16886 }
16887}
16888
38d518c9
EZ
16889/* Return the name of the namespace represented by DIE. Set
16890 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
16891 namespace. */
16892
16893static const char *
e142c38c 16894namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
38d518c9
EZ
16895{
16896 struct die_info *current_die;
16897 const char *name = NULL;
16898
16899 /* Loop through the extensions until we find a name. */
16900
16901 for (current_die = die;
16902 current_die != NULL;
f2f0e013 16903 current_die = dwarf2_extension (die, &cu))
38d518c9 16904 {
96553a0c
DE
16905 /* We don't use dwarf2_name here so that we can detect the absence
16906 of a name -> anonymous namespace. */
7d45c7c3 16907 name = dwarf2_string_attr (die, DW_AT_name, cu);
96553a0c 16908
38d518c9
EZ
16909 if (name != NULL)
16910 break;
16911 }
16912
16913 /* Is it an anonymous namespace? */
16914
16915 *is_anonymous = (name == NULL);
16916 if (*is_anonymous)
2b1dbab0 16917 name = CP_ANONYMOUS_NAMESPACE_STR;
38d518c9
EZ
16918
16919 return name;
d9fa45fe
DC
16920}
16921
c906108c
SS
16922/* Extract all information from a DW_TAG_pointer_type DIE and add to
16923 the user defined type vector. */
16924
f792889a 16925static struct type *
e7c27a73 16926read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16927{
518817b3
SM
16928 struct gdbarch *gdbarch
16929 = get_objfile_arch (cu->per_cu->dwarf2_per_objfile->objfile);
e7c27a73 16930 struct comp_unit_head *cu_header = &cu->header;
c906108c 16931 struct type *type;
8b2dbe47
KB
16932 struct attribute *attr_byte_size;
16933 struct attribute *attr_address_class;
16934 int byte_size, addr_class;
7e314c57
JK
16935 struct type *target_type;
16936
16937 target_type = die_type (die, cu);
c906108c 16938
7e314c57
JK
16939 /* The die_type call above may have already set the type for this DIE. */
16940 type = get_die_type (die, cu);
16941 if (type)
16942 return type;
16943
16944 type = lookup_pointer_type (target_type);
8b2dbe47 16945
e142c38c 16946 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
8b2dbe47
KB
16947 if (attr_byte_size)
16948 byte_size = DW_UNSND (attr_byte_size);
c906108c 16949 else
8b2dbe47
KB
16950 byte_size = cu_header->addr_size;
16951
e142c38c 16952 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
8b2dbe47
KB
16953 if (attr_address_class)
16954 addr_class = DW_UNSND (attr_address_class);
16955 else
16956 addr_class = DW_ADDR_none;
16957
16958 /* If the pointer size or address class is different than the
16959 default, create a type variant marked as such and set the
16960 length accordingly. */
16961 if (TYPE_LENGTH (type) != byte_size || addr_class != DW_ADDR_none)
c906108c 16962 {
5e2b427d 16963 if (gdbarch_address_class_type_flags_p (gdbarch))
8b2dbe47
KB
16964 {
16965 int type_flags;
16966
849957d9 16967 type_flags = gdbarch_address_class_type_flags
5e2b427d 16968 (gdbarch, byte_size, addr_class);
876cecd0
TT
16969 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
16970 == 0);
8b2dbe47
KB
16971 type = make_type_with_address_space (type, type_flags);
16972 }
16973 else if (TYPE_LENGTH (type) != byte_size)
16974 {
3e43a32a
MS
16975 complaint (&symfile_complaints,
16976 _("invalid pointer size %d"), byte_size);
8b2dbe47 16977 }
6e70227d 16978 else
9a619af0
MS
16979 {
16980 /* Should we also complain about unhandled address classes? */
16981 }
c906108c 16982 }
8b2dbe47
KB
16983
16984 TYPE_LENGTH (type) = byte_size;
f792889a 16985 return set_die_type (die, type, cu);
c906108c
SS
16986}
16987
16988/* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
16989 the user defined type vector. */
16990
f792889a 16991static struct type *
e7c27a73 16992read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
16993{
16994 struct type *type;
16995 struct type *to_type;
16996 struct type *domain;
16997
e7c27a73
DJ
16998 to_type = die_type (die, cu);
16999 domain = die_containing_type (die, cu);
0d5de010 17000
7e314c57
JK
17001 /* The calls above may have already set the type for this DIE. */
17002 type = get_die_type (die, cu);
17003 if (type)
17004 return type;
17005
0d5de010
DJ
17006 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
17007 type = lookup_methodptr_type (to_type);
7078baeb
TT
17008 else if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_FUNC)
17009 {
518817b3
SM
17010 struct type *new_type
17011 = alloc_type (cu->per_cu->dwarf2_per_objfile->objfile);
7078baeb
TT
17012
17013 smash_to_method_type (new_type, domain, TYPE_TARGET_TYPE (to_type),
17014 TYPE_FIELDS (to_type), TYPE_NFIELDS (to_type),
17015 TYPE_VARARGS (to_type));
17016 type = lookup_methodptr_type (new_type);
17017 }
0d5de010
DJ
17018 else
17019 type = lookup_memberptr_type (to_type, domain);
c906108c 17020
f792889a 17021 return set_die_type (die, type, cu);
c906108c
SS
17022}
17023
4297a3f0 17024/* Extract all information from a DW_TAG_{rvalue_,}reference_type DIE and add to
c906108c
SS
17025 the user defined type vector. */
17026
f792889a 17027static struct type *
4297a3f0
AV
17028read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu,
17029 enum type_code refcode)
c906108c 17030{
e7c27a73 17031 struct comp_unit_head *cu_header = &cu->header;
7e314c57 17032 struct type *type, *target_type;
c906108c
SS
17033 struct attribute *attr;
17034
4297a3f0
AV
17035 gdb_assert (refcode == TYPE_CODE_REF || refcode == TYPE_CODE_RVALUE_REF);
17036
7e314c57
JK
17037 target_type = die_type (die, cu);
17038
17039 /* The die_type call above may have already set the type for this DIE. */
17040 type = get_die_type (die, cu);
17041 if (type)
17042 return type;
17043
4297a3f0 17044 type = lookup_reference_type (target_type, refcode);
e142c38c 17045 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
17046 if (attr)
17047 {
17048 TYPE_LENGTH (type) = DW_UNSND (attr);
17049 }
17050 else
17051 {
107d2387 17052 TYPE_LENGTH (type) = cu_header->addr_size;
c906108c 17053 }
f792889a 17054 return set_die_type (die, type, cu);
c906108c
SS
17055}
17056
cf363f18
MW
17057/* Add the given cv-qualifiers to the element type of the array. GCC
17058 outputs DWARF type qualifiers that apply to an array, not the
17059 element type. But GDB relies on the array element type to carry
17060 the cv-qualifiers. This mimics section 6.7.3 of the C99
17061 specification. */
17062
17063static struct type *
17064add_array_cv_type (struct die_info *die, struct dwarf2_cu *cu,
17065 struct type *base_type, int cnst, int voltl)
17066{
17067 struct type *el_type, *inner_array;
17068
17069 base_type = copy_type (base_type);
17070 inner_array = base_type;
17071
17072 while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
17073 {
17074 TYPE_TARGET_TYPE (inner_array) =
17075 copy_type (TYPE_TARGET_TYPE (inner_array));
17076 inner_array = TYPE_TARGET_TYPE (inner_array);
17077 }
17078
17079 el_type = TYPE_TARGET_TYPE (inner_array);
17080 cnst |= TYPE_CONST (el_type);
17081 voltl |= TYPE_VOLATILE (el_type);
17082 TYPE_TARGET_TYPE (inner_array) = make_cv_type (cnst, voltl, el_type, NULL);
17083
17084 return set_die_type (die, base_type, cu);
17085}
17086
f792889a 17087static struct type *
e7c27a73 17088read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17089{
f792889a 17090 struct type *base_type, *cv_type;
c906108c 17091
e7c27a73 17092 base_type = die_type (die, cu);
7e314c57
JK
17093
17094 /* The die_type call above may have already set the type for this DIE. */
17095 cv_type = get_die_type (die, cu);
17096 if (cv_type)
17097 return cv_type;
17098
2f608a3a
KW
17099 /* In case the const qualifier is applied to an array type, the element type
17100 is so qualified, not the array type (section 6.7.3 of C99). */
17101 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
cf363f18 17102 return add_array_cv_type (die, cu, base_type, 1, 0);
2f608a3a 17103
f792889a
DJ
17104 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
17105 return set_die_type (die, cv_type, cu);
c906108c
SS
17106}
17107
f792889a 17108static struct type *
e7c27a73 17109read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17110{
f792889a 17111 struct type *base_type, *cv_type;
c906108c 17112
e7c27a73 17113 base_type = die_type (die, cu);
7e314c57
JK
17114
17115 /* The die_type call above may have already set the type for this DIE. */
17116 cv_type = get_die_type (die, cu);
17117 if (cv_type)
17118 return cv_type;
17119
cf363f18
MW
17120 /* In case the volatile qualifier is applied to an array type, the
17121 element type is so qualified, not the array type (section 6.7.3
17122 of C99). */
17123 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
17124 return add_array_cv_type (die, cu, base_type, 0, 1);
17125
f792889a
DJ
17126 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
17127 return set_die_type (die, cv_type, cu);
c906108c
SS
17128}
17129
06d66ee9
TT
17130/* Handle DW_TAG_restrict_type. */
17131
17132static struct type *
17133read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
17134{
17135 struct type *base_type, *cv_type;
17136
17137 base_type = die_type (die, cu);
17138
17139 /* The die_type call above may have already set the type for this DIE. */
17140 cv_type = get_die_type (die, cu);
17141 if (cv_type)
17142 return cv_type;
17143
17144 cv_type = make_restrict_type (base_type);
17145 return set_die_type (die, cv_type, cu);
17146}
17147
a2c2acaf
MW
17148/* Handle DW_TAG_atomic_type. */
17149
17150static struct type *
17151read_tag_atomic_type (struct die_info *die, struct dwarf2_cu *cu)
17152{
17153 struct type *base_type, *cv_type;
17154
17155 base_type = die_type (die, cu);
17156
17157 /* The die_type call above may have already set the type for this DIE. */
17158 cv_type = get_die_type (die, cu);
17159 if (cv_type)
17160 return cv_type;
17161
17162 cv_type = make_atomic_type (base_type);
17163 return set_die_type (die, cv_type, cu);
17164}
17165
c906108c
SS
17166/* Extract all information from a DW_TAG_string_type DIE and add to
17167 the user defined type vector. It isn't really a user defined type,
17168 but it behaves like one, with other DIE's using an AT_user_def_type
17169 attribute to reference it. */
17170
f792889a 17171static struct type *
e7c27a73 17172read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17173{
518817b3 17174 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
3b7538c0 17175 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
17176 struct type *type, *range_type, *index_type, *char_type;
17177 struct attribute *attr;
17178 unsigned int length;
17179
e142c38c 17180 attr = dwarf2_attr (die, DW_AT_string_length, cu);
c906108c
SS
17181 if (attr)
17182 {
17183 length = DW_UNSND (attr);
17184 }
17185 else
17186 {
0963b4bd 17187 /* Check for the DW_AT_byte_size attribute. */
e142c38c 17188 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
b21b22e0
PS
17189 if (attr)
17190 {
17191 length = DW_UNSND (attr);
17192 }
17193 else
17194 {
17195 length = 1;
17196 }
c906108c 17197 }
6ccb9162 17198
46bf5051 17199 index_type = objfile_type (objfile)->builtin_int;
0c9c3474 17200 range_type = create_static_range_type (NULL, index_type, 1, length);
3b7538c0
UW
17201 char_type = language_string_char_type (cu->language_defn, gdbarch);
17202 type = create_string_type (NULL, char_type, range_type);
6ccb9162 17203
f792889a 17204 return set_die_type (die, type, cu);
c906108c
SS
17205}
17206
4d804846
JB
17207/* Assuming that DIE corresponds to a function, returns nonzero
17208 if the function is prototyped. */
17209
17210static int
17211prototyped_function_p (struct die_info *die, struct dwarf2_cu *cu)
17212{
17213 struct attribute *attr;
17214
17215 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
17216 if (attr && (DW_UNSND (attr) != 0))
17217 return 1;
17218
17219 /* The DWARF standard implies that the DW_AT_prototyped attribute
17220 is only meaninful for C, but the concept also extends to other
17221 languages that allow unprototyped functions (Eg: Objective C).
17222 For all other languages, assume that functions are always
17223 prototyped. */
17224 if (cu->language != language_c
17225 && cu->language != language_objc
17226 && cu->language != language_opencl)
17227 return 1;
17228
17229 /* RealView does not emit DW_AT_prototyped. We can not distinguish
17230 prototyped and unprototyped functions; default to prototyped,
17231 since that is more common in modern code (and RealView warns
17232 about unprototyped functions). */
17233 if (producer_is_realview (cu->producer))
17234 return 1;
17235
17236 return 0;
17237}
17238
c906108c
SS
17239/* Handle DIES due to C code like:
17240
17241 struct foo
c5aa993b
JM
17242 {
17243 int (*funcp)(int a, long l);
17244 int b;
17245 };
c906108c 17246
0963b4bd 17247 ('funcp' generates a DW_TAG_subroutine_type DIE). */
c906108c 17248
f792889a 17249static struct type *
e7c27a73 17250read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17251{
518817b3 17252 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
0963b4bd
MS
17253 struct type *type; /* Type that this function returns. */
17254 struct type *ftype; /* Function that returns above type. */
c906108c
SS
17255 struct attribute *attr;
17256
e7c27a73 17257 type = die_type (die, cu);
7e314c57
JK
17258
17259 /* The die_type call above may have already set the type for this DIE. */
17260 ftype = get_die_type (die, cu);
17261 if (ftype)
17262 return ftype;
17263
0c8b41f1 17264 ftype = lookup_function_type (type);
c906108c 17265
4d804846 17266 if (prototyped_function_p (die, cu))
a6c727b2 17267 TYPE_PROTOTYPED (ftype) = 1;
c906108c 17268
c055b101
CV
17269 /* Store the calling convention in the type if it's available in
17270 the subroutine die. Otherwise set the calling convention to
17271 the default value DW_CC_normal. */
17272 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
54fcddd0
UW
17273 if (attr)
17274 TYPE_CALLING_CONVENTION (ftype) = DW_UNSND (attr);
17275 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
17276 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
17277 else
17278 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
76c10ea2 17279
743649fd
MW
17280 /* Record whether the function returns normally to its caller or not
17281 if the DWARF producer set that information. */
17282 attr = dwarf2_attr (die, DW_AT_noreturn, cu);
17283 if (attr && (DW_UNSND (attr) != 0))
17284 TYPE_NO_RETURN (ftype) = 1;
17285
76c10ea2
GM
17286 /* We need to add the subroutine type to the die immediately so
17287 we don't infinitely recurse when dealing with parameters
0963b4bd 17288 declared as the same subroutine type. */
76c10ea2 17289 set_die_type (die, ftype, cu);
6e70227d 17290
639d11d3 17291 if (die->child != NULL)
c906108c 17292 {
bb5ed363 17293 struct type *void_type = objfile_type (objfile)->builtin_void;
c906108c 17294 struct die_info *child_die;
8072405b 17295 int nparams, iparams;
c906108c
SS
17296
17297 /* Count the number of parameters.
17298 FIXME: GDB currently ignores vararg functions, but knows about
17299 vararg member functions. */
8072405b 17300 nparams = 0;
639d11d3 17301 child_die = die->child;
c906108c
SS
17302 while (child_die && child_die->tag)
17303 {
17304 if (child_die->tag == DW_TAG_formal_parameter)
17305 nparams++;
17306 else if (child_die->tag == DW_TAG_unspecified_parameters)
876cecd0 17307 TYPE_VARARGS (ftype) = 1;
c906108c
SS
17308 child_die = sibling_die (child_die);
17309 }
17310
17311 /* Allocate storage for parameters and fill them in. */
17312 TYPE_NFIELDS (ftype) = nparams;
17313 TYPE_FIELDS (ftype) = (struct field *)
ae5a43e0 17314 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
c906108c 17315
8072405b
JK
17316 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
17317 even if we error out during the parameters reading below. */
17318 for (iparams = 0; iparams < nparams; iparams++)
17319 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
17320
17321 iparams = 0;
639d11d3 17322 child_die = die->child;
c906108c
SS
17323 while (child_die && child_die->tag)
17324 {
17325 if (child_die->tag == DW_TAG_formal_parameter)
17326 {
3ce3b1ba
PA
17327 struct type *arg_type;
17328
17329 /* DWARF version 2 has no clean way to discern C++
17330 static and non-static member functions. G++ helps
17331 GDB by marking the first parameter for non-static
17332 member functions (which is the this pointer) as
17333 artificial. We pass this information to
17334 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
17335
17336 DWARF version 3 added DW_AT_object_pointer, which GCC
17337 4.5 does not yet generate. */
e142c38c 17338 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
c906108c
SS
17339 if (attr)
17340 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
17341 else
9c37b5ae 17342 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
3ce3b1ba
PA
17343 arg_type = die_type (child_die, cu);
17344
17345 /* RealView does not mark THIS as const, which the testsuite
17346 expects. GCC marks THIS as const in method definitions,
17347 but not in the class specifications (GCC PR 43053). */
17348 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
17349 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
17350 {
17351 int is_this = 0;
17352 struct dwarf2_cu *arg_cu = cu;
17353 const char *name = dwarf2_name (child_die, cu);
17354
17355 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
17356 if (attr)
17357 {
17358 /* If the compiler emits this, use it. */
17359 if (follow_die_ref (die, attr, &arg_cu) == child_die)
17360 is_this = 1;
17361 }
17362 else if (name && strcmp (name, "this") == 0)
17363 /* Function definitions will have the argument names. */
17364 is_this = 1;
17365 else if (name == NULL && iparams == 0)
17366 /* Declarations may not have the names, so like
17367 elsewhere in GDB, assume an artificial first
17368 argument is "this". */
17369 is_this = 1;
17370
17371 if (is_this)
17372 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
17373 arg_type, 0);
17374 }
17375
17376 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
c906108c
SS
17377 iparams++;
17378 }
17379 child_die = sibling_die (child_die);
17380 }
17381 }
17382
76c10ea2 17383 return ftype;
c906108c
SS
17384}
17385
f792889a 17386static struct type *
e7c27a73 17387read_typedef (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17388{
518817b3 17389 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
0114d602 17390 const char *name = NULL;
3c8e0968 17391 struct type *this_type, *target_type;
c906108c 17392
94af9270 17393 name = dwarf2_full_name (NULL, die, cu);
19f392bc
UW
17394 this_type = init_type (objfile, TYPE_CODE_TYPEDEF, 0, name);
17395 TYPE_TARGET_STUB (this_type) = 1;
f792889a 17396 set_die_type (die, this_type, cu);
3c8e0968
DE
17397 target_type = die_type (die, cu);
17398 if (target_type != this_type)
17399 TYPE_TARGET_TYPE (this_type) = target_type;
17400 else
17401 {
17402 /* Self-referential typedefs are, it seems, not allowed by the DWARF
17403 spec and cause infinite loops in GDB. */
17404 complaint (&symfile_complaints,
17405 _("Self-referential DW_TAG_typedef "
9d8780f0
SM
17406 "- DIE at %s [in module %s]"),
17407 sect_offset_str (die->sect_off), objfile_name (objfile));
3c8e0968
DE
17408 TYPE_TARGET_TYPE (this_type) = NULL;
17409 }
f792889a 17410 return this_type;
c906108c
SS
17411}
17412
9b790ce7
UW
17413/* Allocate a floating-point type of size BITS and name NAME. Pass NAME_HINT
17414 (which may be different from NAME) to the architecture back-end to allow
17415 it to guess the correct format if necessary. */
17416
17417static struct type *
17418dwarf2_init_float_type (struct objfile *objfile, int bits, const char *name,
17419 const char *name_hint)
17420{
17421 struct gdbarch *gdbarch = get_objfile_arch (objfile);
17422 const struct floatformat **format;
17423 struct type *type;
17424
17425 format = gdbarch_floatformat_for_type (gdbarch, name_hint, bits);
17426 if (format)
17427 type = init_float_type (objfile, bits, name, format);
17428 else
77b7c781 17429 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
9b790ce7
UW
17430
17431 return type;
17432}
17433
c906108c
SS
17434/* Find a representation of a given base type and install
17435 it in the TYPE field of the die. */
17436
f792889a 17437static struct type *
e7c27a73 17438read_base_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17439{
518817b3 17440 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c
SS
17441 struct type *type;
17442 struct attribute *attr;
19f392bc 17443 int encoding = 0, bits = 0;
15d034d0 17444 const char *name;
c906108c 17445
e142c38c 17446 attr = dwarf2_attr (die, DW_AT_encoding, cu);
c906108c
SS
17447 if (attr)
17448 {
17449 encoding = DW_UNSND (attr);
17450 }
e142c38c 17451 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
17452 if (attr)
17453 {
19f392bc 17454 bits = DW_UNSND (attr) * TARGET_CHAR_BIT;
c906108c 17455 }
39cbfefa 17456 name = dwarf2_name (die, cu);
6ccb9162 17457 if (!name)
c906108c 17458 {
6ccb9162
UW
17459 complaint (&symfile_complaints,
17460 _("DW_AT_name missing from DW_TAG_base_type"));
c906108c 17461 }
6ccb9162
UW
17462
17463 switch (encoding)
c906108c 17464 {
6ccb9162
UW
17465 case DW_ATE_address:
17466 /* Turn DW_ATE_address into a void * pointer. */
77b7c781 17467 type = init_type (objfile, TYPE_CODE_VOID, TARGET_CHAR_BIT, NULL);
19f392bc 17468 type = init_pointer_type (objfile, bits, name, type);
6ccb9162
UW
17469 break;
17470 case DW_ATE_boolean:
19f392bc 17471 type = init_boolean_type (objfile, bits, 1, name);
6ccb9162
UW
17472 break;
17473 case DW_ATE_complex_float:
9b790ce7 17474 type = dwarf2_init_float_type (objfile, bits / 2, NULL, name);
19f392bc 17475 type = init_complex_type (objfile, name, type);
6ccb9162
UW
17476 break;
17477 case DW_ATE_decimal_float:
19f392bc 17478 type = init_decfloat_type (objfile, bits, name);
6ccb9162
UW
17479 break;
17480 case DW_ATE_float:
9b790ce7 17481 type = dwarf2_init_float_type (objfile, bits, name, name);
6ccb9162
UW
17482 break;
17483 case DW_ATE_signed:
19f392bc 17484 type = init_integer_type (objfile, bits, 0, name);
6ccb9162
UW
17485 break;
17486 case DW_ATE_unsigned:
3b2b8fea
TT
17487 if (cu->language == language_fortran
17488 && name
61012eef 17489 && startswith (name, "character("))
19f392bc
UW
17490 type = init_character_type (objfile, bits, 1, name);
17491 else
17492 type = init_integer_type (objfile, bits, 1, name);
6ccb9162
UW
17493 break;
17494 case DW_ATE_signed_char:
6e70227d 17495 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea
TT
17496 || cu->language == language_pascal
17497 || cu->language == language_fortran)
19f392bc
UW
17498 type = init_character_type (objfile, bits, 0, name);
17499 else
17500 type = init_integer_type (objfile, bits, 0, name);
6ccb9162
UW
17501 break;
17502 case DW_ATE_unsigned_char:
868a0084 17503 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea 17504 || cu->language == language_pascal
c44af4eb
TT
17505 || cu->language == language_fortran
17506 || cu->language == language_rust)
19f392bc
UW
17507 type = init_character_type (objfile, bits, 1, name);
17508 else
17509 type = init_integer_type (objfile, bits, 1, name);
6ccb9162 17510 break;
75079b2b 17511 case DW_ATE_UTF:
53e710ac
PA
17512 {
17513 gdbarch *arch = get_objfile_arch (objfile);
17514
17515 if (bits == 16)
17516 type = builtin_type (arch)->builtin_char16;
17517 else if (bits == 32)
17518 type = builtin_type (arch)->builtin_char32;
17519 else
17520 {
17521 complaint (&symfile_complaints,
17522 _("unsupported DW_ATE_UTF bit size: '%d'"),
17523 bits);
17524 type = init_integer_type (objfile, bits, 1, name);
17525 }
17526 return set_die_type (die, type, cu);
17527 }
75079b2b
TT
17528 break;
17529
6ccb9162
UW
17530 default:
17531 complaint (&symfile_complaints, _("unsupported DW_AT_encoding: '%s'"),
17532 dwarf_type_encoding_name (encoding));
77b7c781 17533 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
6ccb9162 17534 break;
c906108c 17535 }
6ccb9162 17536
0114d602 17537 if (name && strcmp (name, "char") == 0)
876cecd0 17538 TYPE_NOSIGN (type) = 1;
0114d602 17539
f792889a 17540 return set_die_type (die, type, cu);
c906108c
SS
17541}
17542
80180f79
SA
17543/* Parse dwarf attribute if it's a block, reference or constant and put the
17544 resulting value of the attribute into struct bound_prop.
17545 Returns 1 if ATTR could be resolved into PROP, 0 otherwise. */
17546
17547static int
17548attr_to_dynamic_prop (const struct attribute *attr, struct die_info *die,
17549 struct dwarf2_cu *cu, struct dynamic_prop *prop)
17550{
17551 struct dwarf2_property_baton *baton;
518817b3
SM
17552 struct obstack *obstack
17553 = &cu->per_cu->dwarf2_per_objfile->objfile->objfile_obstack;
80180f79
SA
17554
17555 if (attr == NULL || prop == NULL)
17556 return 0;
17557
17558 if (attr_form_is_block (attr))
17559 {
8d749320 17560 baton = XOBNEW (obstack, struct dwarf2_property_baton);
80180f79
SA
17561 baton->referenced_type = NULL;
17562 baton->locexpr.per_cu = cu->per_cu;
17563 baton->locexpr.size = DW_BLOCK (attr)->size;
17564 baton->locexpr.data = DW_BLOCK (attr)->data;
17565 prop->data.baton = baton;
17566 prop->kind = PROP_LOCEXPR;
17567 gdb_assert (prop->data.baton != NULL);
17568 }
17569 else if (attr_form_is_ref (attr))
17570 {
17571 struct dwarf2_cu *target_cu = cu;
17572 struct die_info *target_die;
17573 struct attribute *target_attr;
17574
17575 target_die = follow_die_ref (die, attr, &target_cu);
17576 target_attr = dwarf2_attr (target_die, DW_AT_location, target_cu);
df25ebbd
JB
17577 if (target_attr == NULL)
17578 target_attr = dwarf2_attr (target_die, DW_AT_data_member_location,
17579 target_cu);
80180f79
SA
17580 if (target_attr == NULL)
17581 return 0;
17582
df25ebbd 17583 switch (target_attr->name)
80180f79 17584 {
df25ebbd
JB
17585 case DW_AT_location:
17586 if (attr_form_is_section_offset (target_attr))
17587 {
8d749320 17588 baton = XOBNEW (obstack, struct dwarf2_property_baton);
df25ebbd
JB
17589 baton->referenced_type = die_type (target_die, target_cu);
17590 fill_in_loclist_baton (cu, &baton->loclist, target_attr);
17591 prop->data.baton = baton;
17592 prop->kind = PROP_LOCLIST;
17593 gdb_assert (prop->data.baton != NULL);
17594 }
17595 else if (attr_form_is_block (target_attr))
17596 {
8d749320 17597 baton = XOBNEW (obstack, struct dwarf2_property_baton);
df25ebbd
JB
17598 baton->referenced_type = die_type (target_die, target_cu);
17599 baton->locexpr.per_cu = cu->per_cu;
17600 baton->locexpr.size = DW_BLOCK (target_attr)->size;
17601 baton->locexpr.data = DW_BLOCK (target_attr)->data;
17602 prop->data.baton = baton;
17603 prop->kind = PROP_LOCEXPR;
17604 gdb_assert (prop->data.baton != NULL);
17605 }
17606 else
17607 {
17608 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
17609 "dynamic property");
17610 return 0;
17611 }
17612 break;
17613 case DW_AT_data_member_location:
17614 {
17615 LONGEST offset;
17616
17617 if (!handle_data_member_location (target_die, target_cu,
17618 &offset))
17619 return 0;
17620
8d749320 17621 baton = XOBNEW (obstack, struct dwarf2_property_baton);
6ad395a7
JB
17622 baton->referenced_type = read_type_die (target_die->parent,
17623 target_cu);
df25ebbd
JB
17624 baton->offset_info.offset = offset;
17625 baton->offset_info.type = die_type (target_die, target_cu);
17626 prop->data.baton = baton;
17627 prop->kind = PROP_ADDR_OFFSET;
17628 break;
17629 }
80180f79
SA
17630 }
17631 }
17632 else if (attr_form_is_constant (attr))
17633 {
17634 prop->data.const_val = dwarf2_get_attr_constant_value (attr, 0);
17635 prop->kind = PROP_CONST;
17636 }
17637 else
17638 {
17639 dwarf2_invalid_attrib_class_complaint (dwarf_form_name (attr->form),
17640 dwarf2_name (die, cu));
17641 return 0;
17642 }
17643
17644 return 1;
17645}
17646
a02abb62
JB
17647/* Read the given DW_AT_subrange DIE. */
17648
f792889a 17649static struct type *
a02abb62
JB
17650read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
17651{
4c9ad8c2 17652 struct type *base_type, *orig_base_type;
a02abb62
JB
17653 struct type *range_type;
17654 struct attribute *attr;
729efb13 17655 struct dynamic_prop low, high;
4fae6e18 17656 int low_default_is_valid;
c451ebe5 17657 int high_bound_is_count = 0;
15d034d0 17658 const char *name;
43bbcdc2 17659 LONGEST negative_mask;
e77813c8 17660
4c9ad8c2
TT
17661 orig_base_type = die_type (die, cu);
17662 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
17663 whereas the real type might be. So, we use ORIG_BASE_TYPE when
17664 creating the range type, but we use the result of check_typedef
17665 when examining properties of the type. */
17666 base_type = check_typedef (orig_base_type);
a02abb62 17667
7e314c57
JK
17668 /* The die_type call above may have already set the type for this DIE. */
17669 range_type = get_die_type (die, cu);
17670 if (range_type)
17671 return range_type;
17672
729efb13
SA
17673 low.kind = PROP_CONST;
17674 high.kind = PROP_CONST;
17675 high.data.const_val = 0;
17676
4fae6e18
JK
17677 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
17678 omitting DW_AT_lower_bound. */
17679 switch (cu->language)
6e70227d 17680 {
4fae6e18
JK
17681 case language_c:
17682 case language_cplus:
729efb13 17683 low.data.const_val = 0;
4fae6e18
JK
17684 low_default_is_valid = 1;
17685 break;
17686 case language_fortran:
729efb13 17687 low.data.const_val = 1;
4fae6e18
JK
17688 low_default_is_valid = 1;
17689 break;
17690 case language_d:
4fae6e18 17691 case language_objc:
c44af4eb 17692 case language_rust:
729efb13 17693 low.data.const_val = 0;
4fae6e18
JK
17694 low_default_is_valid = (cu->header.version >= 4);
17695 break;
17696 case language_ada:
17697 case language_m2:
17698 case language_pascal:
729efb13 17699 low.data.const_val = 1;
4fae6e18
JK
17700 low_default_is_valid = (cu->header.version >= 4);
17701 break;
17702 default:
729efb13 17703 low.data.const_val = 0;
4fae6e18
JK
17704 low_default_is_valid = 0;
17705 break;
a02abb62
JB
17706 }
17707
e142c38c 17708 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
a02abb62 17709 if (attr)
11c1ba78 17710 attr_to_dynamic_prop (attr, die, cu, &low);
4fae6e18
JK
17711 else if (!low_default_is_valid)
17712 complaint (&symfile_complaints, _("Missing DW_AT_lower_bound "
9d8780f0
SM
17713 "- DIE at %s [in module %s]"),
17714 sect_offset_str (die->sect_off),
518817b3 17715 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
a02abb62 17716
e142c38c 17717 attr = dwarf2_attr (die, DW_AT_upper_bound, cu);
80180f79 17718 if (!attr_to_dynamic_prop (attr, die, cu, &high))
e77813c8
PM
17719 {
17720 attr = dwarf2_attr (die, DW_AT_count, cu);
c451ebe5 17721 if (attr_to_dynamic_prop (attr, die, cu, &high))
6b662e19 17722 {
c451ebe5
SA
17723 /* If bounds are constant do the final calculation here. */
17724 if (low.kind == PROP_CONST && high.kind == PROP_CONST)
17725 high.data.const_val = low.data.const_val + high.data.const_val - 1;
17726 else
17727 high_bound_is_count = 1;
c2ff108b 17728 }
e77813c8
PM
17729 }
17730
17731 /* Dwarf-2 specifications explicitly allows to create subrange types
17732 without specifying a base type.
17733 In that case, the base type must be set to the type of
17734 the lower bound, upper bound or count, in that order, if any of these
17735 three attributes references an object that has a type.
17736 If no base type is found, the Dwarf-2 specifications say that
17737 a signed integer type of size equal to the size of an address should
17738 be used.
17739 For the following C code: `extern char gdb_int [];'
17740 GCC produces an empty range DIE.
17741 FIXME: muller/2010-05-28: Possible references to object for low bound,
0963b4bd 17742 high bound or count are not yet handled by this code. */
e77813c8
PM
17743 if (TYPE_CODE (base_type) == TYPE_CODE_VOID)
17744 {
518817b3 17745 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
e77813c8
PM
17746 struct gdbarch *gdbarch = get_objfile_arch (objfile);
17747 int addr_size = gdbarch_addr_bit (gdbarch) /8;
17748 struct type *int_type = objfile_type (objfile)->builtin_int;
17749
17750 /* Test "int", "long int", and "long long int" objfile types,
17751 and select the first one having a size above or equal to the
17752 architecture address size. */
17753 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
17754 base_type = int_type;
17755 else
17756 {
17757 int_type = objfile_type (objfile)->builtin_long;
17758 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
17759 base_type = int_type;
17760 else
17761 {
17762 int_type = objfile_type (objfile)->builtin_long_long;
17763 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
17764 base_type = int_type;
17765 }
17766 }
17767 }
a02abb62 17768
dbb9c2b1
JB
17769 /* Normally, the DWARF producers are expected to use a signed
17770 constant form (Eg. DW_FORM_sdata) to express negative bounds.
17771 But this is unfortunately not always the case, as witnessed
17772 with GCC, for instance, where the ambiguous DW_FORM_dataN form
17773 is used instead. To work around that ambiguity, we treat
17774 the bounds as signed, and thus sign-extend their values, when
17775 the base type is signed. */
6e70227d 17776 negative_mask =
66c6502d 17777 -((LONGEST) 1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1));
729efb13
SA
17778 if (low.kind == PROP_CONST
17779 && !TYPE_UNSIGNED (base_type) && (low.data.const_val & negative_mask))
17780 low.data.const_val |= negative_mask;
17781 if (high.kind == PROP_CONST
17782 && !TYPE_UNSIGNED (base_type) && (high.data.const_val & negative_mask))
17783 high.data.const_val |= negative_mask;
43bbcdc2 17784
729efb13 17785 range_type = create_range_type (NULL, orig_base_type, &low, &high);
a02abb62 17786
c451ebe5
SA
17787 if (high_bound_is_count)
17788 TYPE_RANGE_DATA (range_type)->flag_upper_bound_is_count = 1;
17789
c2ff108b
JK
17790 /* Ada expects an empty array on no boundary attributes. */
17791 if (attr == NULL && cu->language != language_ada)
729efb13 17792 TYPE_HIGH_BOUND_KIND (range_type) = PROP_UNDEFINED;
c2ff108b 17793
39cbfefa
DJ
17794 name = dwarf2_name (die, cu);
17795 if (name)
17796 TYPE_NAME (range_type) = name;
6e70227d 17797
e142c38c 17798 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
a02abb62
JB
17799 if (attr)
17800 TYPE_LENGTH (range_type) = DW_UNSND (attr);
17801
7e314c57
JK
17802 set_die_type (die, range_type, cu);
17803
17804 /* set_die_type should be already done. */
b4ba55a1
JB
17805 set_descriptive_type (range_type, die, cu);
17806
7e314c57 17807 return range_type;
a02abb62 17808}
6e70227d 17809
f792889a 17810static struct type *
81a17f79
JB
17811read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
17812{
17813 struct type *type;
81a17f79 17814
518817b3
SM
17815 type = init_type (cu->per_cu->dwarf2_per_objfile->objfile, TYPE_CODE_VOID,0,
17816 NULL);
0114d602 17817 TYPE_NAME (type) = dwarf2_name (die, cu);
81a17f79 17818
74a2f8ff
JB
17819 /* In Ada, an unspecified type is typically used when the description
17820 of the type is defered to a different unit. When encountering
17821 such a type, we treat it as a stub, and try to resolve it later on,
17822 when needed. */
17823 if (cu->language == language_ada)
17824 TYPE_STUB (type) = 1;
17825
f792889a 17826 return set_die_type (die, type, cu);
81a17f79 17827}
a02abb62 17828
639d11d3
DC
17829/* Read a single die and all its descendents. Set the die's sibling
17830 field to NULL; set other fields in the die correctly, and set all
17831 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
17832 location of the info_ptr after reading all of those dies. PARENT
17833 is the parent of the die in question. */
17834
17835static struct die_info *
dee91e82 17836read_die_and_children (const struct die_reader_specs *reader,
d521ce57
TT
17837 const gdb_byte *info_ptr,
17838 const gdb_byte **new_info_ptr,
dee91e82 17839 struct die_info *parent)
639d11d3
DC
17840{
17841 struct die_info *die;
d521ce57 17842 const gdb_byte *cur_ptr;
639d11d3
DC
17843 int has_children;
17844
bf6af496 17845 cur_ptr = read_full_die_1 (reader, &die, info_ptr, &has_children, 0);
1d325ec1
DJ
17846 if (die == NULL)
17847 {
17848 *new_info_ptr = cur_ptr;
17849 return NULL;
17850 }
93311388 17851 store_in_ref_table (die, reader->cu);
639d11d3
DC
17852
17853 if (has_children)
bf6af496 17854 die->child = read_die_and_siblings_1 (reader, cur_ptr, new_info_ptr, die);
639d11d3
DC
17855 else
17856 {
17857 die->child = NULL;
17858 *new_info_ptr = cur_ptr;
17859 }
17860
17861 die->sibling = NULL;
17862 die->parent = parent;
17863 return die;
17864}
17865
17866/* Read a die, all of its descendents, and all of its siblings; set
17867 all of the fields of all of the dies correctly. Arguments are as
17868 in read_die_and_children. */
17869
17870static struct die_info *
bf6af496 17871read_die_and_siblings_1 (const struct die_reader_specs *reader,
d521ce57
TT
17872 const gdb_byte *info_ptr,
17873 const gdb_byte **new_info_ptr,
bf6af496 17874 struct die_info *parent)
639d11d3
DC
17875{
17876 struct die_info *first_die, *last_sibling;
d521ce57 17877 const gdb_byte *cur_ptr;
639d11d3 17878
c906108c 17879 cur_ptr = info_ptr;
639d11d3
DC
17880 first_die = last_sibling = NULL;
17881
17882 while (1)
c906108c 17883 {
639d11d3 17884 struct die_info *die
dee91e82 17885 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
639d11d3 17886
1d325ec1 17887 if (die == NULL)
c906108c 17888 {
639d11d3
DC
17889 *new_info_ptr = cur_ptr;
17890 return first_die;
c906108c 17891 }
1d325ec1
DJ
17892
17893 if (!first_die)
17894 first_die = die;
c906108c 17895 else
1d325ec1
DJ
17896 last_sibling->sibling = die;
17897
17898 last_sibling = die;
c906108c 17899 }
c906108c
SS
17900}
17901
bf6af496
DE
17902/* Read a die, all of its descendents, and all of its siblings; set
17903 all of the fields of all of the dies correctly. Arguments are as
17904 in read_die_and_children.
17905 This the main entry point for reading a DIE and all its children. */
17906
17907static struct die_info *
17908read_die_and_siblings (const struct die_reader_specs *reader,
d521ce57
TT
17909 const gdb_byte *info_ptr,
17910 const gdb_byte **new_info_ptr,
bf6af496
DE
17911 struct die_info *parent)
17912{
17913 struct die_info *die = read_die_and_siblings_1 (reader, info_ptr,
17914 new_info_ptr, parent);
17915
b4f54984 17916 if (dwarf_die_debug)
bf6af496
DE
17917 {
17918 fprintf_unfiltered (gdb_stdlog,
17919 "Read die from %s@0x%x of %s:\n",
a32a8923 17920 get_section_name (reader->die_section),
bf6af496
DE
17921 (unsigned) (info_ptr - reader->die_section->buffer),
17922 bfd_get_filename (reader->abfd));
b4f54984 17923 dump_die (die, dwarf_die_debug);
bf6af496
DE
17924 }
17925
17926 return die;
17927}
17928
3019eac3
DE
17929/* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
17930 attributes.
17931 The caller is responsible for filling in the extra attributes
17932 and updating (*DIEP)->num_attrs.
17933 Set DIEP to point to a newly allocated die with its information,
17934 except for its child, sibling, and parent fields.
17935 Set HAS_CHILDREN to tell whether the die has children or not. */
93311388 17936
d521ce57 17937static const gdb_byte *
3019eac3 17938read_full_die_1 (const struct die_reader_specs *reader,
d521ce57 17939 struct die_info **diep, const gdb_byte *info_ptr,
3019eac3 17940 int *has_children, int num_extra_attrs)
93311388 17941{
b64f50a1 17942 unsigned int abbrev_number, bytes_read, i;
93311388
DE
17943 struct abbrev_info *abbrev;
17944 struct die_info *die;
17945 struct dwarf2_cu *cu = reader->cu;
17946 bfd *abfd = reader->abfd;
17947
9c541725 17948 sect_offset sect_off = (sect_offset) (info_ptr - reader->buffer);
93311388
DE
17949 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
17950 info_ptr += bytes_read;
17951 if (!abbrev_number)
17952 {
17953 *diep = NULL;
17954 *has_children = 0;
17955 return info_ptr;
17956 }
17957
685af9cd 17958 abbrev = reader->abbrev_table->lookup_abbrev (abbrev_number);
93311388 17959 if (!abbrev)
348e048f
DE
17960 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
17961 abbrev_number,
17962 bfd_get_filename (abfd));
17963
3019eac3 17964 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
9c541725 17965 die->sect_off = sect_off;
93311388
DE
17966 die->tag = abbrev->tag;
17967 die->abbrev = abbrev_number;
17968
3019eac3
DE
17969 /* Make the result usable.
17970 The caller needs to update num_attrs after adding the extra
17971 attributes. */
93311388
DE
17972 die->num_attrs = abbrev->num_attrs;
17973
17974 for (i = 0; i < abbrev->num_attrs; ++i)
dee91e82
DE
17975 info_ptr = read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
17976 info_ptr);
93311388
DE
17977
17978 *diep = die;
17979 *has_children = abbrev->has_children;
17980 return info_ptr;
17981}
17982
3019eac3
DE
17983/* Read a die and all its attributes.
17984 Set DIEP to point to a newly allocated die with its information,
17985 except for its child, sibling, and parent fields.
17986 Set HAS_CHILDREN to tell whether the die has children or not. */
17987
d521ce57 17988static const gdb_byte *
3019eac3 17989read_full_die (const struct die_reader_specs *reader,
d521ce57 17990 struct die_info **diep, const gdb_byte *info_ptr,
3019eac3
DE
17991 int *has_children)
17992{
d521ce57 17993 const gdb_byte *result;
bf6af496
DE
17994
17995 result = read_full_die_1 (reader, diep, info_ptr, has_children, 0);
17996
b4f54984 17997 if (dwarf_die_debug)
bf6af496
DE
17998 {
17999 fprintf_unfiltered (gdb_stdlog,
18000 "Read die from %s@0x%x of %s:\n",
a32a8923 18001 get_section_name (reader->die_section),
bf6af496
DE
18002 (unsigned) (info_ptr - reader->die_section->buffer),
18003 bfd_get_filename (reader->abfd));
b4f54984 18004 dump_die (*diep, dwarf_die_debug);
bf6af496
DE
18005 }
18006
18007 return result;
3019eac3 18008}
433df2d4
DE
18009\f
18010/* Abbreviation tables.
3019eac3 18011
433df2d4 18012 In DWARF version 2, the description of the debugging information is
c906108c
SS
18013 stored in a separate .debug_abbrev section. Before we read any
18014 dies from a section we read in all abbreviations and install them
433df2d4
DE
18015 in a hash table. */
18016
18017/* Allocate space for a struct abbrev_info object in ABBREV_TABLE. */
18018
685af9cd
TT
18019struct abbrev_info *
18020abbrev_table::alloc_abbrev ()
433df2d4
DE
18021{
18022 struct abbrev_info *abbrev;
18023
685af9cd 18024 abbrev = XOBNEW (&abbrev_obstack, struct abbrev_info);
433df2d4 18025 memset (abbrev, 0, sizeof (struct abbrev_info));
8d749320 18026
433df2d4
DE
18027 return abbrev;
18028}
18029
18030/* Add an abbreviation to the table. */
c906108c 18031
685af9cd
TT
18032void
18033abbrev_table::add_abbrev (unsigned int abbrev_number,
18034 struct abbrev_info *abbrev)
433df2d4
DE
18035{
18036 unsigned int hash_number;
18037
18038 hash_number = abbrev_number % ABBREV_HASH_SIZE;
4a17f768
YQ
18039 abbrev->next = m_abbrevs[hash_number];
18040 m_abbrevs[hash_number] = abbrev;
433df2d4 18041}
dee91e82 18042
433df2d4
DE
18043/* Look up an abbrev in the table.
18044 Returns NULL if the abbrev is not found. */
18045
685af9cd
TT
18046struct abbrev_info *
18047abbrev_table::lookup_abbrev (unsigned int abbrev_number)
c906108c 18048{
433df2d4
DE
18049 unsigned int hash_number;
18050 struct abbrev_info *abbrev;
18051
18052 hash_number = abbrev_number % ABBREV_HASH_SIZE;
4a17f768 18053 abbrev = m_abbrevs[hash_number];
433df2d4
DE
18054
18055 while (abbrev)
18056 {
18057 if (abbrev->number == abbrev_number)
18058 return abbrev;
18059 abbrev = abbrev->next;
18060 }
18061 return NULL;
18062}
18063
18064/* Read in an abbrev table. */
18065
685af9cd 18066static abbrev_table_up
ed2dc618
SM
18067abbrev_table_read_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
18068 struct dwarf2_section_info *section,
9c541725 18069 sect_offset sect_off)
433df2d4
DE
18070{
18071 struct objfile *objfile = dwarf2_per_objfile->objfile;
a32a8923 18072 bfd *abfd = get_section_bfd_owner (section);
d521ce57 18073 const gdb_byte *abbrev_ptr;
c906108c
SS
18074 struct abbrev_info *cur_abbrev;
18075 unsigned int abbrev_number, bytes_read, abbrev_name;
433df2d4 18076 unsigned int abbrev_form;
f3dd6933
DJ
18077 struct attr_abbrev *cur_attrs;
18078 unsigned int allocated_attrs;
c906108c 18079
685af9cd 18080 abbrev_table_up abbrev_table (new struct abbrev_table (sect_off));
c906108c 18081
433df2d4 18082 dwarf2_read_section (objfile, section);
9c541725 18083 abbrev_ptr = section->buffer + to_underlying (sect_off);
c906108c
SS
18084 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18085 abbrev_ptr += bytes_read;
18086
f3dd6933 18087 allocated_attrs = ATTR_ALLOC_CHUNK;
8d749320 18088 cur_attrs = XNEWVEC (struct attr_abbrev, allocated_attrs);
6e70227d 18089
0963b4bd 18090 /* Loop until we reach an abbrev number of 0. */
c906108c
SS
18091 while (abbrev_number)
18092 {
685af9cd 18093 cur_abbrev = abbrev_table->alloc_abbrev ();
c906108c
SS
18094
18095 /* read in abbrev header */
18096 cur_abbrev->number = abbrev_number;
aead7601
SM
18097 cur_abbrev->tag
18098 = (enum dwarf_tag) read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
c906108c
SS
18099 abbrev_ptr += bytes_read;
18100 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
18101 abbrev_ptr += 1;
18102
18103 /* now read in declarations */
22d2f3ab 18104 for (;;)
c906108c 18105 {
43988095
JK
18106 LONGEST implicit_const;
18107
22d2f3ab
JK
18108 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18109 abbrev_ptr += bytes_read;
18110 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18111 abbrev_ptr += bytes_read;
43988095
JK
18112 if (abbrev_form == DW_FORM_implicit_const)
18113 {
18114 implicit_const = read_signed_leb128 (abfd, abbrev_ptr,
18115 &bytes_read);
18116 abbrev_ptr += bytes_read;
18117 }
18118 else
18119 {
18120 /* Initialize it due to a false compiler warning. */
18121 implicit_const = -1;
18122 }
22d2f3ab
JK
18123
18124 if (abbrev_name == 0)
18125 break;
18126
f3dd6933 18127 if (cur_abbrev->num_attrs == allocated_attrs)
c906108c 18128 {
f3dd6933
DJ
18129 allocated_attrs += ATTR_ALLOC_CHUNK;
18130 cur_attrs
224c3ddb 18131 = XRESIZEVEC (struct attr_abbrev, cur_attrs, allocated_attrs);
c906108c 18132 }
ae038cb0 18133
aead7601
SM
18134 cur_attrs[cur_abbrev->num_attrs].name
18135 = (enum dwarf_attribute) abbrev_name;
22d2f3ab 18136 cur_attrs[cur_abbrev->num_attrs].form
aead7601 18137 = (enum dwarf_form) abbrev_form;
43988095 18138 cur_attrs[cur_abbrev->num_attrs].implicit_const = implicit_const;
22d2f3ab 18139 ++cur_abbrev->num_attrs;
c906108c
SS
18140 }
18141
8d749320
SM
18142 cur_abbrev->attrs =
18143 XOBNEWVEC (&abbrev_table->abbrev_obstack, struct attr_abbrev,
18144 cur_abbrev->num_attrs);
f3dd6933
DJ
18145 memcpy (cur_abbrev->attrs, cur_attrs,
18146 cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
18147
685af9cd 18148 abbrev_table->add_abbrev (abbrev_number, cur_abbrev);
c906108c
SS
18149
18150 /* Get next abbreviation.
18151 Under Irix6 the abbreviations for a compilation unit are not
c5aa993b
JM
18152 always properly terminated with an abbrev number of 0.
18153 Exit loop if we encounter an abbreviation which we have
18154 already read (which means we are about to read the abbreviations
18155 for the next compile unit) or if the end of the abbreviation
18156 table is reached. */
433df2d4 18157 if ((unsigned int) (abbrev_ptr - section->buffer) >= section->size)
c906108c
SS
18158 break;
18159 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18160 abbrev_ptr += bytes_read;
685af9cd 18161 if (abbrev_table->lookup_abbrev (abbrev_number) != NULL)
c906108c
SS
18162 break;
18163 }
f3dd6933
DJ
18164
18165 xfree (cur_attrs);
433df2d4 18166 return abbrev_table;
c906108c
SS
18167}
18168
72bf9492
DJ
18169/* Returns nonzero if TAG represents a type that we might generate a partial
18170 symbol for. */
18171
18172static int
18173is_type_tag_for_partial (int tag)
18174{
18175 switch (tag)
18176 {
18177#if 0
18178 /* Some types that would be reasonable to generate partial symbols for,
18179 that we don't at present. */
18180 case DW_TAG_array_type:
18181 case DW_TAG_file_type:
18182 case DW_TAG_ptr_to_member_type:
18183 case DW_TAG_set_type:
18184 case DW_TAG_string_type:
18185 case DW_TAG_subroutine_type:
18186#endif
18187 case DW_TAG_base_type:
18188 case DW_TAG_class_type:
680b30c7 18189 case DW_TAG_interface_type:
72bf9492
DJ
18190 case DW_TAG_enumeration_type:
18191 case DW_TAG_structure_type:
18192 case DW_TAG_subrange_type:
18193 case DW_TAG_typedef:
18194 case DW_TAG_union_type:
18195 return 1;
18196 default:
18197 return 0;
18198 }
18199}
18200
18201/* Load all DIEs that are interesting for partial symbols into memory. */
18202
18203static struct partial_die_info *
dee91e82 18204load_partial_dies (const struct die_reader_specs *reader,
d521ce57 18205 const gdb_byte *info_ptr, int building_psymtab)
72bf9492 18206{
dee91e82 18207 struct dwarf2_cu *cu = reader->cu;
518817b3 18208 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
72bf9492 18209 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
72bf9492 18210 unsigned int bytes_read;
5afb4e99 18211 unsigned int load_all = 0;
72bf9492
DJ
18212 int nesting_level = 1;
18213
18214 parent_die = NULL;
18215 last_die = NULL;
18216
7adf1e79
DE
18217 gdb_assert (cu->per_cu != NULL);
18218 if (cu->per_cu->load_all_dies)
5afb4e99
DJ
18219 load_all = 1;
18220
72bf9492
DJ
18221 cu->partial_dies
18222 = htab_create_alloc_ex (cu->header.length / 12,
18223 partial_die_hash,
18224 partial_die_eq,
18225 NULL,
18226 &cu->comp_unit_obstack,
18227 hashtab_obstack_allocate,
18228 dummy_obstack_deallocate);
18229
72bf9492
DJ
18230 while (1)
18231 {
685af9cd 18232 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
72bf9492
DJ
18233
18234 /* A NULL abbrev means the end of a series of children. */
18235 if (abbrev == NULL)
18236 {
18237 if (--nesting_level == 0)
cd9983dd
YQ
18238 return first_die;
18239
72bf9492
DJ
18240 info_ptr += bytes_read;
18241 last_die = parent_die;
18242 parent_die = parent_die->die_parent;
18243 continue;
18244 }
18245
98bfdba5
PA
18246 /* Check for template arguments. We never save these; if
18247 they're seen, we just mark the parent, and go on our way. */
18248 if (parent_die != NULL
18249 && cu->language == language_cplus
18250 && (abbrev->tag == DW_TAG_template_type_param
18251 || abbrev->tag == DW_TAG_template_value_param))
18252 {
18253 parent_die->has_template_arguments = 1;
18254
18255 if (!load_all)
18256 {
18257 /* We don't need a partial DIE for the template argument. */
dee91e82 18258 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
98bfdba5
PA
18259 continue;
18260 }
18261 }
18262
0d99eb77 18263 /* We only recurse into c++ subprograms looking for template arguments.
98bfdba5
PA
18264 Skip their other children. */
18265 if (!load_all
18266 && cu->language == language_cplus
18267 && parent_die != NULL
18268 && parent_die->tag == DW_TAG_subprogram)
18269 {
dee91e82 18270 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
98bfdba5
PA
18271 continue;
18272 }
18273
5afb4e99
DJ
18274 /* Check whether this DIE is interesting enough to save. Normally
18275 we would not be interested in members here, but there may be
18276 later variables referencing them via DW_AT_specification (for
18277 static members). */
18278 if (!load_all
18279 && !is_type_tag_for_partial (abbrev->tag)
72929c62 18280 && abbrev->tag != DW_TAG_constant
72bf9492
DJ
18281 && abbrev->tag != DW_TAG_enumerator
18282 && abbrev->tag != DW_TAG_subprogram
b1dc1806 18283 && abbrev->tag != DW_TAG_inlined_subroutine
bc30ff58 18284 && abbrev->tag != DW_TAG_lexical_block
72bf9492 18285 && abbrev->tag != DW_TAG_variable
5afb4e99 18286 && abbrev->tag != DW_TAG_namespace
f55ee35c 18287 && abbrev->tag != DW_TAG_module
95554aad 18288 && abbrev->tag != DW_TAG_member
74921315
KS
18289 && abbrev->tag != DW_TAG_imported_unit
18290 && abbrev->tag != DW_TAG_imported_declaration)
72bf9492
DJ
18291 {
18292 /* Otherwise we skip to the next sibling, if any. */
dee91e82 18293 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
72bf9492
DJ
18294 continue;
18295 }
18296
6f06d47b
YQ
18297 struct partial_die_info pdi ((sect_offset) (info_ptr - reader->buffer),
18298 abbrev);
cd9983dd 18299
48fbe735 18300 info_ptr = pdi.read (reader, *abbrev, info_ptr + bytes_read);
72bf9492
DJ
18301
18302 /* This two-pass algorithm for processing partial symbols has a
18303 high cost in cache pressure. Thus, handle some simple cases
18304 here which cover the majority of C partial symbols. DIEs
18305 which neither have specification tags in them, nor could have
18306 specification tags elsewhere pointing at them, can simply be
18307 processed and discarded.
18308
18309 This segment is also optional; scan_partial_symbols and
18310 add_partial_symbol will handle these DIEs if we chain
18311 them in normally. When compilers which do not emit large
18312 quantities of duplicate debug information are more common,
18313 this code can probably be removed. */
18314
18315 /* Any complete simple types at the top level (pretty much all
18316 of them, for a language without namespaces), can be processed
18317 directly. */
18318 if (parent_die == NULL
cd9983dd
YQ
18319 && pdi.has_specification == 0
18320 && pdi.is_declaration == 0
18321 && ((pdi.tag == DW_TAG_typedef && !pdi.has_children)
18322 || pdi.tag == DW_TAG_base_type
18323 || pdi.tag == DW_TAG_subrange_type))
72bf9492 18324 {
cd9983dd
YQ
18325 if (building_psymtab && pdi.name != NULL)
18326 add_psymbol_to_list (pdi.name, strlen (pdi.name), 0,
72bf9492 18327 VAR_DOMAIN, LOC_TYPEDEF,
bb5ed363 18328 &objfile->static_psymbols,
1762568f 18329 0, cu->language, objfile);
cd9983dd 18330 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
72bf9492
DJ
18331 continue;
18332 }
18333
d8228535
JK
18334 /* The exception for DW_TAG_typedef with has_children above is
18335 a workaround of GCC PR debug/47510. In the case of this complaint
18336 type_name_no_tag_or_error will error on such types later.
18337
18338 GDB skipped children of DW_TAG_typedef by the shortcut above and then
18339 it could not find the child DIEs referenced later, this is checked
18340 above. In correct DWARF DW_TAG_typedef should have no children. */
18341
cd9983dd 18342 if (pdi.tag == DW_TAG_typedef && pdi.has_children)
d8228535
JK
18343 complaint (&symfile_complaints,
18344 _("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
9d8780f0 18345 "- DIE at %s [in module %s]"),
cd9983dd 18346 sect_offset_str (pdi.sect_off), objfile_name (objfile));
d8228535 18347
72bf9492
DJ
18348 /* If we're at the second level, and we're an enumerator, and
18349 our parent has no specification (meaning possibly lives in a
18350 namespace elsewhere), then we can add the partial symbol now
18351 instead of queueing it. */
cd9983dd 18352 if (pdi.tag == DW_TAG_enumerator
72bf9492
DJ
18353 && parent_die != NULL
18354 && parent_die->die_parent == NULL
18355 && parent_die->tag == DW_TAG_enumeration_type
18356 && parent_die->has_specification == 0)
18357 {
cd9983dd 18358 if (pdi.name == NULL)
3e43a32a
MS
18359 complaint (&symfile_complaints,
18360 _("malformed enumerator DIE ignored"));
72bf9492 18361 else if (building_psymtab)
cd9983dd 18362 add_psymbol_to_list (pdi.name, strlen (pdi.name), 0,
72bf9492 18363 VAR_DOMAIN, LOC_CONST,
9c37b5ae 18364 cu->language == language_cplus
bb5ed363
DE
18365 ? &objfile->global_psymbols
18366 : &objfile->static_psymbols,
1762568f 18367 0, cu->language, objfile);
72bf9492 18368
cd9983dd 18369 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
72bf9492
DJ
18370 continue;
18371 }
18372
cd9983dd 18373 struct partial_die_info *part_die
6f06d47b 18374 = new (&cu->comp_unit_obstack) partial_die_info (pdi);
cd9983dd 18375
72bf9492
DJ
18376 /* We'll save this DIE so link it in. */
18377 part_die->die_parent = parent_die;
18378 part_die->die_sibling = NULL;
18379 part_die->die_child = NULL;
18380
18381 if (last_die && last_die == parent_die)
18382 last_die->die_child = part_die;
18383 else if (last_die)
18384 last_die->die_sibling = part_die;
18385
18386 last_die = part_die;
18387
18388 if (first_die == NULL)
18389 first_die = part_die;
18390
18391 /* Maybe add the DIE to the hash table. Not all DIEs that we
18392 find interesting need to be in the hash table, because we
18393 also have the parent/sibling/child chains; only those that we
18394 might refer to by offset later during partial symbol reading.
18395
18396 For now this means things that might have be the target of a
18397 DW_AT_specification, DW_AT_abstract_origin, or
18398 DW_AT_extension. DW_AT_extension will refer only to
18399 namespaces; DW_AT_abstract_origin refers to functions (and
18400 many things under the function DIE, but we do not recurse
18401 into function DIEs during partial symbol reading) and
18402 possibly variables as well; DW_AT_specification refers to
18403 declarations. Declarations ought to have the DW_AT_declaration
18404 flag. It happens that GCC forgets to put it in sometimes, but
18405 only for functions, not for types.
18406
18407 Adding more things than necessary to the hash table is harmless
18408 except for the performance cost. Adding too few will result in
5afb4e99
DJ
18409 wasted time in find_partial_die, when we reread the compilation
18410 unit with load_all_dies set. */
72bf9492 18411
5afb4e99 18412 if (load_all
72929c62 18413 || abbrev->tag == DW_TAG_constant
5afb4e99 18414 || abbrev->tag == DW_TAG_subprogram
72bf9492
DJ
18415 || abbrev->tag == DW_TAG_variable
18416 || abbrev->tag == DW_TAG_namespace
18417 || part_die->is_declaration)
18418 {
18419 void **slot;
18420
18421 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
9c541725
PA
18422 to_underlying (part_die->sect_off),
18423 INSERT);
72bf9492
DJ
18424 *slot = part_die;
18425 }
18426
72bf9492 18427 /* For some DIEs we want to follow their children (if any). For C
bc30ff58 18428 we have no reason to follow the children of structures; for other
98bfdba5
PA
18429 languages we have to, so that we can get at method physnames
18430 to infer fully qualified class names, for DW_AT_specification,
18431 and for C++ template arguments. For C++, we also look one level
18432 inside functions to find template arguments (if the name of the
18433 function does not already contain the template arguments).
bc30ff58
JB
18434
18435 For Ada, we need to scan the children of subprograms and lexical
18436 blocks as well because Ada allows the definition of nested
18437 entities that could be interesting for the debugger, such as
18438 nested subprograms for instance. */
72bf9492 18439 if (last_die->has_children
5afb4e99
DJ
18440 && (load_all
18441 || last_die->tag == DW_TAG_namespace
f55ee35c 18442 || last_die->tag == DW_TAG_module
72bf9492 18443 || last_die->tag == DW_TAG_enumeration_type
98bfdba5
PA
18444 || (cu->language == language_cplus
18445 && last_die->tag == DW_TAG_subprogram
18446 && (last_die->name == NULL
18447 || strchr (last_die->name, '<') == NULL))
72bf9492
DJ
18448 || (cu->language != language_c
18449 && (last_die->tag == DW_TAG_class_type
680b30c7 18450 || last_die->tag == DW_TAG_interface_type
72bf9492 18451 || last_die->tag == DW_TAG_structure_type
bc30ff58
JB
18452 || last_die->tag == DW_TAG_union_type))
18453 || (cu->language == language_ada
18454 && (last_die->tag == DW_TAG_subprogram
18455 || last_die->tag == DW_TAG_lexical_block))))
72bf9492
DJ
18456 {
18457 nesting_level++;
18458 parent_die = last_die;
18459 continue;
18460 }
18461
18462 /* Otherwise we skip to the next sibling, if any. */
dee91e82 18463 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
72bf9492
DJ
18464
18465 /* Back to the top, do it again. */
18466 }
18467}
18468
6f06d47b
YQ
18469partial_die_info::partial_die_info (sect_offset sect_off_,
18470 struct abbrev_info *abbrev)
18471 : partial_die_info (sect_off_, abbrev->tag, abbrev->has_children)
18472{
18473}
18474
35cc7ed7
YQ
18475/* Read a minimal amount of information into the minimal die structure.
18476 INFO_PTR should point just after the initial uleb128 of a DIE. */
c906108c 18477
48fbe735
YQ
18478const gdb_byte *
18479partial_die_info::read (const struct die_reader_specs *reader,
18480 const struct abbrev_info &abbrev, const gdb_byte *info_ptr)
c906108c 18481{
dee91e82 18482 struct dwarf2_cu *cu = reader->cu;
518817b3
SM
18483 struct dwarf2_per_objfile *dwarf2_per_objfile
18484 = cu->per_cu->dwarf2_per_objfile;
fa238c03 18485 unsigned int i;
c5aa993b 18486 int has_low_pc_attr = 0;
c906108c 18487 int has_high_pc_attr = 0;
91da1414 18488 int high_pc_relative = 0;
c906108c 18489
fd0a254f 18490 for (i = 0; i < abbrev.num_attrs; ++i)
c906108c 18491 {
48fbe735
YQ
18492 struct attribute attr;
18493
fd0a254f 18494 info_ptr = read_attribute (reader, &attr, &abbrev.attrs[i], info_ptr);
c906108c
SS
18495
18496 /* Store the data if it is of an attribute we want to keep in a
c5aa993b 18497 partial symbol table. */
c906108c
SS
18498 switch (attr.name)
18499 {
18500 case DW_AT_name:
48fbe735 18501 switch (tag)
71c25dea
TT
18502 {
18503 case DW_TAG_compile_unit:
95554aad 18504 case DW_TAG_partial_unit:
348e048f 18505 case DW_TAG_type_unit:
71c25dea
TT
18506 /* Compilation units have a DW_AT_name that is a filename, not
18507 a source language identifier. */
18508 case DW_TAG_enumeration_type:
18509 case DW_TAG_enumerator:
18510 /* These tags always have simple identifiers already; no need
18511 to canonicalize them. */
48fbe735 18512 name = DW_STRING (&attr);
71c25dea
TT
18513 break;
18514 default:
48fbe735
YQ
18515 {
18516 struct objfile *objfile = dwarf2_per_objfile->objfile;
18517
18518 name
18519 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
18520 &objfile->per_bfd->storage_obstack);
18521 }
71c25dea
TT
18522 break;
18523 }
c906108c 18524 break;
31ef98ae 18525 case DW_AT_linkage_name:
c906108c 18526 case DW_AT_MIPS_linkage_name:
31ef98ae
TT
18527 /* Note that both forms of linkage name might appear. We
18528 assume they will be the same, and we only store the last
18529 one we see. */
94af9270 18530 if (cu->language == language_ada)
48fbe735
YQ
18531 name = DW_STRING (&attr);
18532 linkage_name = DW_STRING (&attr);
c906108c
SS
18533 break;
18534 case DW_AT_low_pc:
18535 has_low_pc_attr = 1;
48fbe735 18536 lowpc = attr_value_as_address (&attr);
c906108c
SS
18537 break;
18538 case DW_AT_high_pc:
18539 has_high_pc_attr = 1;
48fbe735 18540 highpc = attr_value_as_address (&attr);
31aa7e4e
JB
18541 if (cu->header.version >= 4 && attr_form_is_constant (&attr))
18542 high_pc_relative = 1;
c906108c
SS
18543 break;
18544 case DW_AT_location:
0963b4bd 18545 /* Support the .debug_loc offsets. */
8e19ed76
PS
18546 if (attr_form_is_block (&attr))
18547 {
48fbe735 18548 d.locdesc = DW_BLOCK (&attr);
8e19ed76 18549 }
3690dd37 18550 else if (attr_form_is_section_offset (&attr))
8e19ed76 18551 {
4d3c2250 18552 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
18553 }
18554 else
18555 {
4d3c2250
KB
18556 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
18557 "partial symbol information");
8e19ed76 18558 }
c906108c 18559 break;
c906108c 18560 case DW_AT_external:
48fbe735 18561 is_external = DW_UNSND (&attr);
c906108c
SS
18562 break;
18563 case DW_AT_declaration:
48fbe735 18564 is_declaration = DW_UNSND (&attr);
c906108c
SS
18565 break;
18566 case DW_AT_type:
48fbe735 18567 has_type = 1;
c906108c
SS
18568 break;
18569 case DW_AT_abstract_origin:
18570 case DW_AT_specification:
72bf9492 18571 case DW_AT_extension:
48fbe735
YQ
18572 has_specification = 1;
18573 spec_offset = dwarf2_get_ref_die_offset (&attr);
18574 spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
36586728 18575 || cu->per_cu->is_dwz);
c906108c
SS
18576 break;
18577 case DW_AT_sibling:
18578 /* Ignore absolute siblings, they might point outside of
18579 the current compile unit. */
18580 if (attr.form == DW_FORM_ref_addr)
3e43a32a
MS
18581 complaint (&symfile_complaints,
18582 _("ignoring absolute DW_AT_sibling"));
c906108c 18583 else
b9502d3f 18584 {
48fbe735 18585 const gdb_byte *buffer = reader->buffer;
9c541725
PA
18586 sect_offset off = dwarf2_get_ref_die_offset (&attr);
18587 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
b9502d3f
WN
18588
18589 if (sibling_ptr < info_ptr)
18590 complaint (&symfile_complaints,
18591 _("DW_AT_sibling points backwards"));
22869d73
KS
18592 else if (sibling_ptr > reader->buffer_end)
18593 dwarf2_section_buffer_overflow_complaint (reader->die_section);
b9502d3f 18594 else
48fbe735 18595 sibling = sibling_ptr;
b9502d3f 18596 }
c906108c 18597 break;
fa4028e9 18598 case DW_AT_byte_size:
48fbe735 18599 has_byte_size = 1;
fa4028e9 18600 break;
ff908ebf 18601 case DW_AT_const_value:
48fbe735 18602 has_const_value = 1;
ff908ebf 18603 break;
68511cec
CES
18604 case DW_AT_calling_convention:
18605 /* DWARF doesn't provide a way to identify a program's source-level
18606 entry point. DW_AT_calling_convention attributes are only meant
18607 to describe functions' calling conventions.
18608
18609 However, because it's a necessary piece of information in
0c1b455e
TT
18610 Fortran, and before DWARF 4 DW_CC_program was the only
18611 piece of debugging information whose definition refers to
18612 a 'main program' at all, several compilers marked Fortran
18613 main programs with DW_CC_program --- even when those
18614 functions use the standard calling conventions.
18615
18616 Although DWARF now specifies a way to provide this
18617 information, we support this practice for backward
18618 compatibility. */
68511cec 18619 if (DW_UNSND (&attr) == DW_CC_program
0c1b455e 18620 && cu->language == language_fortran)
48fbe735 18621 main_subprogram = 1;
68511cec 18622 break;
481860b3
GB
18623 case DW_AT_inline:
18624 if (DW_UNSND (&attr) == DW_INL_inlined
18625 || DW_UNSND (&attr) == DW_INL_declared_inlined)
48fbe735 18626 may_be_inlined = 1;
481860b3 18627 break;
95554aad
TT
18628
18629 case DW_AT_import:
48fbe735 18630 if (tag == DW_TAG_imported_unit)
36586728 18631 {
48fbe735
YQ
18632 d.sect_off = dwarf2_get_ref_die_offset (&attr);
18633 is_dwz = (attr.form == DW_FORM_GNU_ref_alt
36586728
TT
18634 || cu->per_cu->is_dwz);
18635 }
95554aad
TT
18636 break;
18637
0c1b455e 18638 case DW_AT_main_subprogram:
48fbe735 18639 main_subprogram = DW_UNSND (&attr);
0c1b455e
TT
18640 break;
18641
c906108c
SS
18642 default:
18643 break;
18644 }
18645 }
18646
91da1414 18647 if (high_pc_relative)
48fbe735 18648 highpc += lowpc;
91da1414 18649
9373cf26
JK
18650 if (has_low_pc_attr && has_high_pc_attr)
18651 {
18652 /* When using the GNU linker, .gnu.linkonce. sections are used to
18653 eliminate duplicate copies of functions and vtables and such.
18654 The linker will arbitrarily choose one and discard the others.
18655 The AT_*_pc values for such functions refer to local labels in
18656 these sections. If the section from that file was discarded, the
18657 labels are not in the output, so the relocs get a value of 0.
18658 If this is a discarded function, mark the pc bounds as invalid,
18659 so that GDB will ignore it. */
48fbe735 18660 if (lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
9373cf26 18661 {
48fbe735 18662 struct objfile *objfile = dwarf2_per_objfile->objfile;
bb5ed363 18663 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9373cf26
JK
18664
18665 complaint (&symfile_complaints,
18666 _("DW_AT_low_pc %s is zero "
9d8780f0 18667 "for DIE at %s [in module %s]"),
48fbe735
YQ
18668 paddress (gdbarch, lowpc),
18669 sect_offset_str (sect_off),
9d8780f0 18670 objfile_name (objfile));
9373cf26
JK
18671 }
18672 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
48fbe735 18673 else if (lowpc >= highpc)
9373cf26 18674 {
48fbe735 18675 struct objfile *objfile = dwarf2_per_objfile->objfile;
bb5ed363 18676 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9373cf26
JK
18677
18678 complaint (&symfile_complaints,
18679 _("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
9d8780f0 18680 "for DIE at %s [in module %s]"),
48fbe735
YQ
18681 paddress (gdbarch, lowpc),
18682 paddress (gdbarch, highpc),
18683 sect_offset_str (sect_off),
9c541725 18684 objfile_name (objfile));
9373cf26
JK
18685 }
18686 else
48fbe735 18687 has_pc_info = 1;
9373cf26 18688 }
85cbf3d3 18689
c906108c
SS
18690 return info_ptr;
18691}
18692
72bf9492
DJ
18693/* Find a cached partial DIE at OFFSET in CU. */
18694
d590ff25
YQ
18695struct partial_die_info *
18696dwarf2_cu::find_partial_die (sect_offset sect_off)
72bf9492
DJ
18697{
18698 struct partial_die_info *lookup_die = NULL;
6f06d47b 18699 struct partial_die_info part_die (sect_off);
72bf9492 18700
9a3c8263 18701 lookup_die = ((struct partial_die_info *)
d590ff25 18702 htab_find_with_hash (partial_dies, &part_die,
9c541725 18703 to_underlying (sect_off)));
72bf9492 18704
72bf9492
DJ
18705 return lookup_die;
18706}
18707
348e048f
DE
18708/* Find a partial DIE at OFFSET, which may or may not be in CU,
18709 except in the case of .debug_types DIEs which do not reference
18710 outside their CU (they do however referencing other types via
55f1336d 18711 DW_FORM_ref_sig8). */
72bf9492
DJ
18712
18713static struct partial_die_info *
9c541725 18714find_partial_die (sect_offset sect_off, int offset_in_dwz, struct dwarf2_cu *cu)
72bf9492 18715{
518817b3
SM
18716 struct dwarf2_per_objfile *dwarf2_per_objfile
18717 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 18718 struct objfile *objfile = dwarf2_per_objfile->objfile;
5afb4e99
DJ
18719 struct dwarf2_per_cu_data *per_cu = NULL;
18720 struct partial_die_info *pd = NULL;
72bf9492 18721
36586728 18722 if (offset_in_dwz == cu->per_cu->is_dwz
9c541725 18723 && offset_in_cu_p (&cu->header, sect_off))
5afb4e99 18724 {
d590ff25 18725 pd = cu->find_partial_die (sect_off);
5afb4e99
DJ
18726 if (pd != NULL)
18727 return pd;
0d99eb77
DE
18728 /* We missed recording what we needed.
18729 Load all dies and try again. */
18730 per_cu = cu->per_cu;
5afb4e99 18731 }
0d99eb77
DE
18732 else
18733 {
18734 /* TUs don't reference other CUs/TUs (except via type signatures). */
3019eac3 18735 if (cu->per_cu->is_debug_types)
0d99eb77 18736 {
9d8780f0
SM
18737 error (_("Dwarf Error: Type Unit at offset %s contains"
18738 " external reference to offset %s [in module %s].\n"),
18739 sect_offset_str (cu->header.sect_off), sect_offset_str (sect_off),
0d99eb77
DE
18740 bfd_get_filename (objfile->obfd));
18741 }
9c541725 18742 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
ed2dc618 18743 dwarf2_per_objfile);
72bf9492 18744
0d99eb77
DE
18745 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
18746 load_partial_comp_unit (per_cu);
ae038cb0 18747
0d99eb77 18748 per_cu->cu->last_used = 0;
d590ff25 18749 pd = per_cu->cu->find_partial_die (sect_off);
0d99eb77 18750 }
5afb4e99 18751
dee91e82
DE
18752 /* If we didn't find it, and not all dies have been loaded,
18753 load them all and try again. */
18754
5afb4e99
DJ
18755 if (pd == NULL && per_cu->load_all_dies == 0)
18756 {
5afb4e99 18757 per_cu->load_all_dies = 1;
fd820528
DE
18758
18759 /* This is nasty. When we reread the DIEs, somewhere up the call chain
18760 THIS_CU->cu may already be in use. So we can't just free it and
18761 replace its DIEs with the ones we read in. Instead, we leave those
18762 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
18763 and clobber THIS_CU->cu->partial_dies with the hash table for the new
18764 set. */
dee91e82 18765 load_partial_comp_unit (per_cu);
5afb4e99 18766
d590ff25 18767 pd = per_cu->cu->find_partial_die (sect_off);
5afb4e99
DJ
18768 }
18769
18770 if (pd == NULL)
18771 internal_error (__FILE__, __LINE__,
9d8780f0 18772 _("could not find partial DIE %s "
3e43a32a 18773 "in cache [from module %s]\n"),
9d8780f0 18774 sect_offset_str (sect_off), bfd_get_filename (objfile->obfd));
5afb4e99 18775 return pd;
72bf9492
DJ
18776}
18777
abc72ce4
DE
18778/* See if we can figure out if the class lives in a namespace. We do
18779 this by looking for a member function; its demangled name will
18780 contain namespace info, if there is any. */
18781
18782static void
18783guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
18784 struct dwarf2_cu *cu)
18785{
18786 /* NOTE: carlton/2003-10-07: Getting the info this way changes
18787 what template types look like, because the demangler
18788 frequently doesn't give the same name as the debug info. We
18789 could fix this by only using the demangled name to get the
18790 prefix (but see comment in read_structure_type). */
18791
18792 struct partial_die_info *real_pdi;
18793 struct partial_die_info *child_pdi;
18794
18795 /* If this DIE (this DIE's specification, if any) has a parent, then
18796 we should not do this. We'll prepend the parent's fully qualified
18797 name when we create the partial symbol. */
18798
18799 real_pdi = struct_pdi;
18800 while (real_pdi->has_specification)
36586728
TT
18801 real_pdi = find_partial_die (real_pdi->spec_offset,
18802 real_pdi->spec_is_dwz, cu);
abc72ce4
DE
18803
18804 if (real_pdi->die_parent != NULL)
18805 return;
18806
18807 for (child_pdi = struct_pdi->die_child;
18808 child_pdi != NULL;
18809 child_pdi = child_pdi->die_sibling)
18810 {
18811 if (child_pdi->tag == DW_TAG_subprogram
18812 && child_pdi->linkage_name != NULL)
18813 {
18814 char *actual_class_name
18815 = language_class_name_from_physname (cu->language_defn,
18816 child_pdi->linkage_name);
18817 if (actual_class_name != NULL)
18818 {
518817b3 18819 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
abc72ce4 18820 struct_pdi->name
224c3ddb 18821 = ((const char *)
e3b94546 18822 obstack_copy0 (&objfile->per_bfd->storage_obstack,
224c3ddb
SM
18823 actual_class_name,
18824 strlen (actual_class_name)));
abc72ce4
DE
18825 xfree (actual_class_name);
18826 }
18827 break;
18828 }
18829 }
18830}
18831
52356b79
YQ
18832void
18833partial_die_info::fixup (struct dwarf2_cu *cu)
72bf9492 18834{
abc72ce4
DE
18835 /* Once we've fixed up a die, there's no point in doing so again.
18836 This also avoids a memory leak if we were to call
18837 guess_partial_die_structure_name multiple times. */
52356b79 18838 if (fixup_called)
abc72ce4
DE
18839 return;
18840
72bf9492
DJ
18841 /* If we found a reference attribute and the DIE has no name, try
18842 to find a name in the referred to DIE. */
18843
52356b79 18844 if (name == NULL && has_specification)
72bf9492
DJ
18845 {
18846 struct partial_die_info *spec_die;
72bf9492 18847
52356b79 18848 spec_die = find_partial_die (spec_offset, spec_is_dwz, cu);
72bf9492 18849
52356b79 18850 spec_die->fixup (cu);
72bf9492
DJ
18851
18852 if (spec_die->name)
18853 {
52356b79 18854 name = spec_die->name;
72bf9492
DJ
18855
18856 /* Copy DW_AT_external attribute if it is set. */
18857 if (spec_die->is_external)
52356b79 18858 is_external = spec_die->is_external;
72bf9492
DJ
18859 }
18860 }
18861
18862 /* Set default names for some unnamed DIEs. */
72bf9492 18863
52356b79
YQ
18864 if (name == NULL && tag == DW_TAG_namespace)
18865 name = CP_ANONYMOUS_NAMESPACE_STR;
72bf9492 18866
abc72ce4
DE
18867 /* If there is no parent die to provide a namespace, and there are
18868 children, see if we can determine the namespace from their linkage
122d1940 18869 name. */
abc72ce4 18870 if (cu->language == language_cplus
518817b3
SM
18871 && !VEC_empty (dwarf2_section_info_def,
18872 cu->per_cu->dwarf2_per_objfile->types)
52356b79
YQ
18873 && die_parent == NULL
18874 && has_children
18875 && (tag == DW_TAG_class_type
18876 || tag == DW_TAG_structure_type
18877 || tag == DW_TAG_union_type))
18878 guess_partial_die_structure_name (this, cu);
abc72ce4 18879
53832f31
TT
18880 /* GCC might emit a nameless struct or union that has a linkage
18881 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
52356b79
YQ
18882 if (name == NULL
18883 && (tag == DW_TAG_class_type
18884 || tag == DW_TAG_interface_type
18885 || tag == DW_TAG_structure_type
18886 || tag == DW_TAG_union_type)
18887 && linkage_name != NULL)
53832f31
TT
18888 {
18889 char *demangled;
18890
52356b79 18891 demangled = gdb_demangle (linkage_name, DMGL_TYPES);
53832f31
TT
18892 if (demangled)
18893 {
96408a79
SA
18894 const char *base;
18895
18896 /* Strip any leading namespaces/classes, keep only the base name.
18897 DW_AT_name for named DIEs does not contain the prefixes. */
18898 base = strrchr (demangled, ':');
18899 if (base && base > demangled && base[-1] == ':')
18900 base++;
18901 else
18902 base = demangled;
18903
518817b3 18904 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
52356b79 18905 name
224c3ddb 18906 = ((const char *)
e3b94546 18907 obstack_copy0 (&objfile->per_bfd->storage_obstack,
224c3ddb 18908 base, strlen (base)));
53832f31
TT
18909 xfree (demangled);
18910 }
18911 }
18912
52356b79 18913 fixup_called = 1;
72bf9492
DJ
18914}
18915
a8329558 18916/* Read an attribute value described by an attribute form. */
c906108c 18917
d521ce57 18918static const gdb_byte *
dee91e82
DE
18919read_attribute_value (const struct die_reader_specs *reader,
18920 struct attribute *attr, unsigned form,
43988095 18921 LONGEST implicit_const, const gdb_byte *info_ptr)
c906108c 18922{
dee91e82 18923 struct dwarf2_cu *cu = reader->cu;
518817b3
SM
18924 struct dwarf2_per_objfile *dwarf2_per_objfile
18925 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 18926 struct objfile *objfile = dwarf2_per_objfile->objfile;
3e29f34a 18927 struct gdbarch *gdbarch = get_objfile_arch (objfile);
dee91e82 18928 bfd *abfd = reader->abfd;
e7c27a73 18929 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
18930 unsigned int bytes_read;
18931 struct dwarf_block *blk;
18932
aead7601 18933 attr->form = (enum dwarf_form) form;
a8329558 18934 switch (form)
c906108c 18935 {
c906108c 18936 case DW_FORM_ref_addr:
ae411497 18937 if (cu->header.version == 2)
4568ecf9 18938 DW_UNSND (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
ae411497 18939 else
4568ecf9
DE
18940 DW_UNSND (attr) = read_offset (abfd, info_ptr,
18941 &cu->header, &bytes_read);
ae411497
TT
18942 info_ptr += bytes_read;
18943 break;
36586728
TT
18944 case DW_FORM_GNU_ref_alt:
18945 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
18946 info_ptr += bytes_read;
18947 break;
ae411497 18948 case DW_FORM_addr:
e7c27a73 18949 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
3e29f34a 18950 DW_ADDR (attr) = gdbarch_adjust_dwarf2_addr (gdbarch, DW_ADDR (attr));
107d2387 18951 info_ptr += bytes_read;
c906108c
SS
18952 break;
18953 case DW_FORM_block2:
7b5a2f43 18954 blk = dwarf_alloc_block (cu);
c906108c
SS
18955 blk->size = read_2_bytes (abfd, info_ptr);
18956 info_ptr += 2;
18957 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
18958 info_ptr += blk->size;
18959 DW_BLOCK (attr) = blk;
18960 break;
18961 case DW_FORM_block4:
7b5a2f43 18962 blk = dwarf_alloc_block (cu);
c906108c
SS
18963 blk->size = read_4_bytes (abfd, info_ptr);
18964 info_ptr += 4;
18965 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
18966 info_ptr += blk->size;
18967 DW_BLOCK (attr) = blk;
18968 break;
18969 case DW_FORM_data2:
18970 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
18971 info_ptr += 2;
18972 break;
18973 case DW_FORM_data4:
18974 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
18975 info_ptr += 4;
18976 break;
18977 case DW_FORM_data8:
18978 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
18979 info_ptr += 8;
18980 break;
0224619f
JK
18981 case DW_FORM_data16:
18982 blk = dwarf_alloc_block (cu);
18983 blk->size = 16;
18984 blk->data = read_n_bytes (abfd, info_ptr, 16);
18985 info_ptr += 16;
18986 DW_BLOCK (attr) = blk;
18987 break;
2dc7f7b3
TT
18988 case DW_FORM_sec_offset:
18989 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
18990 info_ptr += bytes_read;
18991 break;
c906108c 18992 case DW_FORM_string:
9b1c24c8 18993 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
8285870a 18994 DW_STRING_IS_CANONICAL (attr) = 0;
c906108c
SS
18995 info_ptr += bytes_read;
18996 break;
4bdf3d34 18997 case DW_FORM_strp:
36586728
TT
18998 if (!cu->per_cu->is_dwz)
18999 {
ed2dc618
SM
19000 DW_STRING (attr) = read_indirect_string (dwarf2_per_objfile,
19001 abfd, info_ptr, cu_header,
36586728
TT
19002 &bytes_read);
19003 DW_STRING_IS_CANONICAL (attr) = 0;
19004 info_ptr += bytes_read;
19005 break;
19006 }
19007 /* FALLTHROUGH */
43988095
JK
19008 case DW_FORM_line_strp:
19009 if (!cu->per_cu->is_dwz)
19010 {
ed2dc618
SM
19011 DW_STRING (attr) = read_indirect_line_string (dwarf2_per_objfile,
19012 abfd, info_ptr,
43988095
JK
19013 cu_header, &bytes_read);
19014 DW_STRING_IS_CANONICAL (attr) = 0;
19015 info_ptr += bytes_read;
19016 break;
19017 }
19018 /* FALLTHROUGH */
36586728
TT
19019 case DW_FORM_GNU_strp_alt:
19020 {
ed2dc618 19021 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
36586728
TT
19022 LONGEST str_offset = read_offset (abfd, info_ptr, cu_header,
19023 &bytes_read);
19024
ed2dc618
SM
19025 DW_STRING (attr) = read_indirect_string_from_dwz (objfile,
19026 dwz, str_offset);
36586728
TT
19027 DW_STRING_IS_CANONICAL (attr) = 0;
19028 info_ptr += bytes_read;
19029 }
4bdf3d34 19030 break;
2dc7f7b3 19031 case DW_FORM_exprloc:
c906108c 19032 case DW_FORM_block:
7b5a2f43 19033 blk = dwarf_alloc_block (cu);
c906108c
SS
19034 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19035 info_ptr += bytes_read;
19036 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19037 info_ptr += blk->size;
19038 DW_BLOCK (attr) = blk;
19039 break;
19040 case DW_FORM_block1:
7b5a2f43 19041 blk = dwarf_alloc_block (cu);
c906108c
SS
19042 blk->size = read_1_byte (abfd, info_ptr);
19043 info_ptr += 1;
19044 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19045 info_ptr += blk->size;
19046 DW_BLOCK (attr) = blk;
19047 break;
19048 case DW_FORM_data1:
19049 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
19050 info_ptr += 1;
19051 break;
19052 case DW_FORM_flag:
19053 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
19054 info_ptr += 1;
19055 break;
2dc7f7b3
TT
19056 case DW_FORM_flag_present:
19057 DW_UNSND (attr) = 1;
19058 break;
c906108c
SS
19059 case DW_FORM_sdata:
19060 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
19061 info_ptr += bytes_read;
19062 break;
19063 case DW_FORM_udata:
19064 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19065 info_ptr += bytes_read;
19066 break;
19067 case DW_FORM_ref1:
9c541725 19068 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 19069 + read_1_byte (abfd, info_ptr));
c906108c
SS
19070 info_ptr += 1;
19071 break;
19072 case DW_FORM_ref2:
9c541725 19073 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 19074 + read_2_bytes (abfd, info_ptr));
c906108c
SS
19075 info_ptr += 2;
19076 break;
19077 case DW_FORM_ref4:
9c541725 19078 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 19079 + read_4_bytes (abfd, info_ptr));
c906108c
SS
19080 info_ptr += 4;
19081 break;
613e1657 19082 case DW_FORM_ref8:
9c541725 19083 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 19084 + read_8_bytes (abfd, info_ptr));
613e1657
KB
19085 info_ptr += 8;
19086 break;
55f1336d 19087 case DW_FORM_ref_sig8:
ac9ec31b 19088 DW_SIGNATURE (attr) = read_8_bytes (abfd, info_ptr);
348e048f
DE
19089 info_ptr += 8;
19090 break;
c906108c 19091 case DW_FORM_ref_udata:
9c541725 19092 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 19093 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
c906108c
SS
19094 info_ptr += bytes_read;
19095 break;
c906108c 19096 case DW_FORM_indirect:
a8329558
KW
19097 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19098 info_ptr += bytes_read;
43988095
JK
19099 if (form == DW_FORM_implicit_const)
19100 {
19101 implicit_const = read_signed_leb128 (abfd, info_ptr, &bytes_read);
19102 info_ptr += bytes_read;
19103 }
19104 info_ptr = read_attribute_value (reader, attr, form, implicit_const,
19105 info_ptr);
19106 break;
19107 case DW_FORM_implicit_const:
19108 DW_SND (attr) = implicit_const;
a8329558 19109 break;
3019eac3
DE
19110 case DW_FORM_GNU_addr_index:
19111 if (reader->dwo_file == NULL)
19112 {
19113 /* For now flag a hard error.
19114 Later we can turn this into a complaint. */
19115 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
19116 dwarf_form_name (form),
19117 bfd_get_filename (abfd));
19118 }
19119 DW_ADDR (attr) = read_addr_index_from_leb128 (cu, info_ptr, &bytes_read);
19120 info_ptr += bytes_read;
19121 break;
19122 case DW_FORM_GNU_str_index:
19123 if (reader->dwo_file == NULL)
19124 {
19125 /* For now flag a hard error.
19126 Later we can turn this into a complaint if warranted. */
19127 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
19128 dwarf_form_name (form),
19129 bfd_get_filename (abfd));
19130 }
19131 {
19132 ULONGEST str_index =
19133 read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19134
342587c4 19135 DW_STRING (attr) = read_str_index (reader, str_index);
3019eac3
DE
19136 DW_STRING_IS_CANONICAL (attr) = 0;
19137 info_ptr += bytes_read;
19138 }
19139 break;
c906108c 19140 default:
8a3fe4f8 19141 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
659b0389
ML
19142 dwarf_form_name (form),
19143 bfd_get_filename (abfd));
c906108c 19144 }
28e94949 19145
36586728 19146 /* Super hack. */
7771576e 19147 if (cu->per_cu->is_dwz && attr_form_is_ref (attr))
36586728
TT
19148 attr->form = DW_FORM_GNU_ref_alt;
19149
28e94949
JB
19150 /* We have seen instances where the compiler tried to emit a byte
19151 size attribute of -1 which ended up being encoded as an unsigned
19152 0xffffffff. Although 0xffffffff is technically a valid size value,
19153 an object of this size seems pretty unlikely so we can relatively
19154 safely treat these cases as if the size attribute was invalid and
19155 treat them as zero by default. */
19156 if (attr->name == DW_AT_byte_size
19157 && form == DW_FORM_data4
19158 && DW_UNSND (attr) >= 0xffffffff)
01c66ae6
JB
19159 {
19160 complaint
19161 (&symfile_complaints,
43bbcdc2
PH
19162 _("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
19163 hex_string (DW_UNSND (attr)));
01c66ae6
JB
19164 DW_UNSND (attr) = 0;
19165 }
28e94949 19166
c906108c
SS
19167 return info_ptr;
19168}
19169
a8329558
KW
19170/* Read an attribute described by an abbreviated attribute. */
19171
d521ce57 19172static const gdb_byte *
dee91e82
DE
19173read_attribute (const struct die_reader_specs *reader,
19174 struct attribute *attr, struct attr_abbrev *abbrev,
d521ce57 19175 const gdb_byte *info_ptr)
a8329558
KW
19176{
19177 attr->name = abbrev->name;
43988095
JK
19178 return read_attribute_value (reader, attr, abbrev->form,
19179 abbrev->implicit_const, info_ptr);
a8329558
KW
19180}
19181
0963b4bd 19182/* Read dwarf information from a buffer. */
c906108c
SS
19183
19184static unsigned int
a1855c1d 19185read_1_byte (bfd *abfd, const gdb_byte *buf)
c906108c 19186{
fe1b8b76 19187 return bfd_get_8 (abfd, buf);
c906108c
SS
19188}
19189
19190static int
a1855c1d 19191read_1_signed_byte (bfd *abfd, const gdb_byte *buf)
c906108c 19192{
fe1b8b76 19193 return bfd_get_signed_8 (abfd, buf);
c906108c
SS
19194}
19195
19196static unsigned int
a1855c1d 19197read_2_bytes (bfd *abfd, const gdb_byte *buf)
c906108c 19198{
fe1b8b76 19199 return bfd_get_16 (abfd, buf);
c906108c
SS
19200}
19201
21ae7a4d 19202static int
a1855c1d 19203read_2_signed_bytes (bfd *abfd, const gdb_byte *buf)
21ae7a4d
JK
19204{
19205 return bfd_get_signed_16 (abfd, buf);
19206}
19207
c906108c 19208static unsigned int
a1855c1d 19209read_4_bytes (bfd *abfd, const gdb_byte *buf)
c906108c 19210{
fe1b8b76 19211 return bfd_get_32 (abfd, buf);
c906108c
SS
19212}
19213
21ae7a4d 19214static int
a1855c1d 19215read_4_signed_bytes (bfd *abfd, const gdb_byte *buf)
21ae7a4d
JK
19216{
19217 return bfd_get_signed_32 (abfd, buf);
19218}
19219
93311388 19220static ULONGEST
a1855c1d 19221read_8_bytes (bfd *abfd, const gdb_byte *buf)
c906108c 19222{
fe1b8b76 19223 return bfd_get_64 (abfd, buf);
c906108c
SS
19224}
19225
19226static CORE_ADDR
d521ce57 19227read_address (bfd *abfd, const gdb_byte *buf, struct dwarf2_cu *cu,
891d2f0b 19228 unsigned int *bytes_read)
c906108c 19229{
e7c27a73 19230 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
19231 CORE_ADDR retval = 0;
19232
107d2387 19233 if (cu_header->signed_addr_p)
c906108c 19234 {
107d2387
AC
19235 switch (cu_header->addr_size)
19236 {
19237 case 2:
fe1b8b76 19238 retval = bfd_get_signed_16 (abfd, buf);
107d2387
AC
19239 break;
19240 case 4:
fe1b8b76 19241 retval = bfd_get_signed_32 (abfd, buf);
107d2387
AC
19242 break;
19243 case 8:
fe1b8b76 19244 retval = bfd_get_signed_64 (abfd, buf);
107d2387
AC
19245 break;
19246 default:
8e65ff28 19247 internal_error (__FILE__, __LINE__,
e2e0b3e5 19248 _("read_address: bad switch, signed [in module %s]"),
659b0389 19249 bfd_get_filename (abfd));
107d2387
AC
19250 }
19251 }
19252 else
19253 {
19254 switch (cu_header->addr_size)
19255 {
19256 case 2:
fe1b8b76 19257 retval = bfd_get_16 (abfd, buf);
107d2387
AC
19258 break;
19259 case 4:
fe1b8b76 19260 retval = bfd_get_32 (abfd, buf);
107d2387
AC
19261 break;
19262 case 8:
fe1b8b76 19263 retval = bfd_get_64 (abfd, buf);
107d2387
AC
19264 break;
19265 default:
8e65ff28 19266 internal_error (__FILE__, __LINE__,
a73c6dcd
MS
19267 _("read_address: bad switch, "
19268 "unsigned [in module %s]"),
659b0389 19269 bfd_get_filename (abfd));
107d2387 19270 }
c906108c 19271 }
64367e0a 19272
107d2387
AC
19273 *bytes_read = cu_header->addr_size;
19274 return retval;
c906108c
SS
19275}
19276
f7ef9339 19277/* Read the initial length from a section. The (draft) DWARF 3
613e1657
KB
19278 specification allows the initial length to take up either 4 bytes
19279 or 12 bytes. If the first 4 bytes are 0xffffffff, then the next 8
19280 bytes describe the length and all offsets will be 8 bytes in length
19281 instead of 4.
19282
f7ef9339
KB
19283 An older, non-standard 64-bit format is also handled by this
19284 function. The older format in question stores the initial length
19285 as an 8-byte quantity without an escape value. Lengths greater
19286 than 2^32 aren't very common which means that the initial 4 bytes
19287 is almost always zero. Since a length value of zero doesn't make
19288 sense for the 32-bit format, this initial zero can be considered to
19289 be an escape value which indicates the presence of the older 64-bit
19290 format. As written, the code can't detect (old format) lengths
917c78fc
MK
19291 greater than 4GB. If it becomes necessary to handle lengths
19292 somewhat larger than 4GB, we could allow other small values (such
19293 as the non-sensical values of 1, 2, and 3) to also be used as
19294 escape values indicating the presence of the old format.
f7ef9339 19295
917c78fc
MK
19296 The value returned via bytes_read should be used to increment the
19297 relevant pointer after calling read_initial_length().
c764a876 19298
613e1657
KB
19299 [ Note: read_initial_length() and read_offset() are based on the
19300 document entitled "DWARF Debugging Information Format", revision
f7ef9339 19301 3, draft 8, dated November 19, 2001. This document was obtained
613e1657
KB
19302 from:
19303
f7ef9339 19304 http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
6e70227d 19305
613e1657
KB
19306 This document is only a draft and is subject to change. (So beware.)
19307
f7ef9339 19308 Details regarding the older, non-standard 64-bit format were
917c78fc
MK
19309 determined empirically by examining 64-bit ELF files produced by
19310 the SGI toolchain on an IRIX 6.5 machine.
f7ef9339
KB
19311
19312 - Kevin, July 16, 2002
613e1657
KB
19313 ] */
19314
19315static LONGEST
d521ce57 19316read_initial_length (bfd *abfd, const gdb_byte *buf, unsigned int *bytes_read)
613e1657 19317{
fe1b8b76 19318 LONGEST length = bfd_get_32 (abfd, buf);
613e1657 19319
dd373385 19320 if (length == 0xffffffff)
613e1657 19321 {
fe1b8b76 19322 length = bfd_get_64 (abfd, buf + 4);
613e1657 19323 *bytes_read = 12;
613e1657 19324 }
dd373385 19325 else if (length == 0)
f7ef9339 19326 {
dd373385 19327 /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX. */
fe1b8b76 19328 length = bfd_get_64 (abfd, buf);
f7ef9339 19329 *bytes_read = 8;
f7ef9339 19330 }
613e1657
KB
19331 else
19332 {
19333 *bytes_read = 4;
613e1657
KB
19334 }
19335
c764a876
DE
19336 return length;
19337}
dd373385 19338
c764a876
DE
19339/* Cover function for read_initial_length.
19340 Returns the length of the object at BUF, and stores the size of the
19341 initial length in *BYTES_READ and stores the size that offsets will be in
19342 *OFFSET_SIZE.
19343 If the initial length size is not equivalent to that specified in
19344 CU_HEADER then issue a complaint.
19345 This is useful when reading non-comp-unit headers. */
dd373385 19346
c764a876 19347static LONGEST
d521ce57 19348read_checked_initial_length_and_offset (bfd *abfd, const gdb_byte *buf,
c764a876
DE
19349 const struct comp_unit_head *cu_header,
19350 unsigned int *bytes_read,
19351 unsigned int *offset_size)
19352{
19353 LONGEST length = read_initial_length (abfd, buf, bytes_read);
19354
19355 gdb_assert (cu_header->initial_length_size == 4
19356 || cu_header->initial_length_size == 8
19357 || cu_header->initial_length_size == 12);
19358
19359 if (cu_header->initial_length_size != *bytes_read)
19360 complaint (&symfile_complaints,
19361 _("intermixed 32-bit and 64-bit DWARF sections"));
dd373385 19362
c764a876 19363 *offset_size = (*bytes_read == 4) ? 4 : 8;
dd373385 19364 return length;
613e1657
KB
19365}
19366
19367/* Read an offset from the data stream. The size of the offset is
917c78fc 19368 given by cu_header->offset_size. */
613e1657
KB
19369
19370static LONGEST
d521ce57
TT
19371read_offset (bfd *abfd, const gdb_byte *buf,
19372 const struct comp_unit_head *cu_header,
891d2f0b 19373 unsigned int *bytes_read)
c764a876
DE
19374{
19375 LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
9a619af0 19376
c764a876
DE
19377 *bytes_read = cu_header->offset_size;
19378 return offset;
19379}
19380
19381/* Read an offset from the data stream. */
19382
19383static LONGEST
d521ce57 19384read_offset_1 (bfd *abfd, const gdb_byte *buf, unsigned int offset_size)
613e1657
KB
19385{
19386 LONGEST retval = 0;
19387
c764a876 19388 switch (offset_size)
613e1657
KB
19389 {
19390 case 4:
fe1b8b76 19391 retval = bfd_get_32 (abfd, buf);
613e1657
KB
19392 break;
19393 case 8:
fe1b8b76 19394 retval = bfd_get_64 (abfd, buf);
613e1657
KB
19395 break;
19396 default:
8e65ff28 19397 internal_error (__FILE__, __LINE__,
c764a876 19398 _("read_offset_1: bad switch [in module %s]"),
659b0389 19399 bfd_get_filename (abfd));
613e1657
KB
19400 }
19401
917c78fc 19402 return retval;
613e1657
KB
19403}
19404
d521ce57
TT
19405static const gdb_byte *
19406read_n_bytes (bfd *abfd, const gdb_byte *buf, unsigned int size)
c906108c
SS
19407{
19408 /* If the size of a host char is 8 bits, we can return a pointer
19409 to the buffer, otherwise we have to copy the data to a buffer
19410 allocated on the temporary obstack. */
4bdf3d34 19411 gdb_assert (HOST_CHAR_BIT == 8);
c906108c 19412 return buf;
c906108c
SS
19413}
19414
d521ce57
TT
19415static const char *
19416read_direct_string (bfd *abfd, const gdb_byte *buf,
19417 unsigned int *bytes_read_ptr)
c906108c
SS
19418{
19419 /* If the size of a host char is 8 bits, we can return a pointer
19420 to the string, otherwise we have to copy the string to a buffer
19421 allocated on the temporary obstack. */
4bdf3d34 19422 gdb_assert (HOST_CHAR_BIT == 8);
c906108c
SS
19423 if (*buf == '\0')
19424 {
19425 *bytes_read_ptr = 1;
19426 return NULL;
19427 }
d521ce57
TT
19428 *bytes_read_ptr = strlen ((const char *) buf) + 1;
19429 return (const char *) buf;
4bdf3d34
JJ
19430}
19431
43988095
JK
19432/* Return pointer to string at section SECT offset STR_OFFSET with error
19433 reporting strings FORM_NAME and SECT_NAME. */
19434
d521ce57 19435static const char *
ed2dc618
SM
19436read_indirect_string_at_offset_from (struct objfile *objfile,
19437 bfd *abfd, LONGEST str_offset,
43988095
JK
19438 struct dwarf2_section_info *sect,
19439 const char *form_name,
19440 const char *sect_name)
19441{
ed2dc618 19442 dwarf2_read_section (objfile, sect);
43988095
JK
19443 if (sect->buffer == NULL)
19444 error (_("%s used without %s section [in module %s]"),
19445 form_name, sect_name, bfd_get_filename (abfd));
19446 if (str_offset >= sect->size)
19447 error (_("%s pointing outside of %s section [in module %s]"),
19448 form_name, sect_name, bfd_get_filename (abfd));
4bdf3d34 19449 gdb_assert (HOST_CHAR_BIT == 8);
43988095 19450 if (sect->buffer[str_offset] == '\0')
4bdf3d34 19451 return NULL;
43988095
JK
19452 return (const char *) (sect->buffer + str_offset);
19453}
19454
19455/* Return pointer to string at .debug_str offset STR_OFFSET. */
19456
19457static const char *
ed2dc618
SM
19458read_indirect_string_at_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
19459 bfd *abfd, LONGEST str_offset)
43988095 19460{
ed2dc618
SM
19461 return read_indirect_string_at_offset_from (dwarf2_per_objfile->objfile,
19462 abfd, str_offset,
43988095
JK
19463 &dwarf2_per_objfile->str,
19464 "DW_FORM_strp", ".debug_str");
19465}
19466
19467/* Return pointer to string at .debug_line_str offset STR_OFFSET. */
19468
19469static const char *
ed2dc618
SM
19470read_indirect_line_string_at_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
19471 bfd *abfd, LONGEST str_offset)
43988095 19472{
ed2dc618
SM
19473 return read_indirect_string_at_offset_from (dwarf2_per_objfile->objfile,
19474 abfd, str_offset,
43988095
JK
19475 &dwarf2_per_objfile->line_str,
19476 "DW_FORM_line_strp",
19477 ".debug_line_str");
c906108c
SS
19478}
19479
36586728
TT
19480/* Read a string at offset STR_OFFSET in the .debug_str section from
19481 the .dwz file DWZ. Throw an error if the offset is too large. If
19482 the string consists of a single NUL byte, return NULL; otherwise
19483 return a pointer to the string. */
19484
d521ce57 19485static const char *
ed2dc618
SM
19486read_indirect_string_from_dwz (struct objfile *objfile, struct dwz_file *dwz,
19487 LONGEST str_offset)
36586728 19488{
ed2dc618 19489 dwarf2_read_section (objfile, &dwz->str);
36586728
TT
19490
19491 if (dwz->str.buffer == NULL)
19492 error (_("DW_FORM_GNU_strp_alt used without .debug_str "
19493 "section [in module %s]"),
19494 bfd_get_filename (dwz->dwz_bfd));
19495 if (str_offset >= dwz->str.size)
19496 error (_("DW_FORM_GNU_strp_alt pointing outside of "
19497 ".debug_str section [in module %s]"),
19498 bfd_get_filename (dwz->dwz_bfd));
19499 gdb_assert (HOST_CHAR_BIT == 8);
19500 if (dwz->str.buffer[str_offset] == '\0')
19501 return NULL;
d521ce57 19502 return (const char *) (dwz->str.buffer + str_offset);
36586728
TT
19503}
19504
43988095
JK
19505/* Return pointer to string at .debug_str offset as read from BUF.
19506 BUF is assumed to be in a compilation unit described by CU_HEADER.
19507 Return *BYTES_READ_PTR count of bytes read from BUF. */
19508
d521ce57 19509static const char *
ed2dc618
SM
19510read_indirect_string (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *abfd,
19511 const gdb_byte *buf,
cf2c3c16
TT
19512 const struct comp_unit_head *cu_header,
19513 unsigned int *bytes_read_ptr)
19514{
19515 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
19516
ed2dc618 19517 return read_indirect_string_at_offset (dwarf2_per_objfile, abfd, str_offset);
cf2c3c16
TT
19518}
19519
43988095
JK
19520/* Return pointer to string at .debug_line_str offset as read from BUF.
19521 BUF is assumed to be in a compilation unit described by CU_HEADER.
19522 Return *BYTES_READ_PTR count of bytes read from BUF. */
19523
19524static const char *
ed2dc618
SM
19525read_indirect_line_string (struct dwarf2_per_objfile *dwarf2_per_objfile,
19526 bfd *abfd, const gdb_byte *buf,
43988095
JK
19527 const struct comp_unit_head *cu_header,
19528 unsigned int *bytes_read_ptr)
19529{
19530 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
19531
ed2dc618
SM
19532 return read_indirect_line_string_at_offset (dwarf2_per_objfile, abfd,
19533 str_offset);
43988095
JK
19534}
19535
19536ULONGEST
d521ce57 19537read_unsigned_leb128 (bfd *abfd, const gdb_byte *buf,
43988095 19538 unsigned int *bytes_read_ptr)
c906108c 19539{
12df843f 19540 ULONGEST result;
ce5d95e1 19541 unsigned int num_read;
870f88f7 19542 int shift;
c906108c
SS
19543 unsigned char byte;
19544
19545 result = 0;
19546 shift = 0;
19547 num_read = 0;
c906108c
SS
19548 while (1)
19549 {
fe1b8b76 19550 byte = bfd_get_8 (abfd, buf);
c906108c
SS
19551 buf++;
19552 num_read++;
12df843f 19553 result |= ((ULONGEST) (byte & 127) << shift);
c906108c
SS
19554 if ((byte & 128) == 0)
19555 {
19556 break;
19557 }
19558 shift += 7;
19559 }
19560 *bytes_read_ptr = num_read;
19561 return result;
19562}
19563
12df843f 19564static LONGEST
d521ce57
TT
19565read_signed_leb128 (bfd *abfd, const gdb_byte *buf,
19566 unsigned int *bytes_read_ptr)
c906108c 19567{
12df843f 19568 LONGEST result;
870f88f7 19569 int shift, num_read;
c906108c
SS
19570 unsigned char byte;
19571
19572 result = 0;
19573 shift = 0;
c906108c 19574 num_read = 0;
c906108c
SS
19575 while (1)
19576 {
fe1b8b76 19577 byte = bfd_get_8 (abfd, buf);
c906108c
SS
19578 buf++;
19579 num_read++;
12df843f 19580 result |= ((LONGEST) (byte & 127) << shift);
c906108c
SS
19581 shift += 7;
19582 if ((byte & 128) == 0)
19583 {
19584 break;
19585 }
19586 }
77e0b926 19587 if ((shift < 8 * sizeof (result)) && (byte & 0x40))
12df843f 19588 result |= -(((LONGEST) 1) << shift);
c906108c
SS
19589 *bytes_read_ptr = num_read;
19590 return result;
19591}
19592
3019eac3
DE
19593/* Given index ADDR_INDEX in .debug_addr, fetch the value.
19594 ADDR_BASE is the DW_AT_GNU_addr_base attribute or zero.
19595 ADDR_SIZE is the size of addresses from the CU header. */
19596
19597static CORE_ADDR
ed2dc618
SM
19598read_addr_index_1 (struct dwarf2_per_objfile *dwarf2_per_objfile,
19599 unsigned int addr_index, ULONGEST addr_base, int addr_size)
3019eac3
DE
19600{
19601 struct objfile *objfile = dwarf2_per_objfile->objfile;
19602 bfd *abfd = objfile->obfd;
19603 const gdb_byte *info_ptr;
19604
19605 dwarf2_read_section (objfile, &dwarf2_per_objfile->addr);
19606 if (dwarf2_per_objfile->addr.buffer == NULL)
19607 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
4262abfb 19608 objfile_name (objfile));
3019eac3
DE
19609 if (addr_base + addr_index * addr_size >= dwarf2_per_objfile->addr.size)
19610 error (_("DW_FORM_addr_index pointing outside of "
19611 ".debug_addr section [in module %s]"),
4262abfb 19612 objfile_name (objfile));
3019eac3
DE
19613 info_ptr = (dwarf2_per_objfile->addr.buffer
19614 + addr_base + addr_index * addr_size);
19615 if (addr_size == 4)
19616 return bfd_get_32 (abfd, info_ptr);
19617 else
19618 return bfd_get_64 (abfd, info_ptr);
19619}
19620
19621/* Given index ADDR_INDEX in .debug_addr, fetch the value. */
19622
19623static CORE_ADDR
19624read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
19625{
518817b3
SM
19626 return read_addr_index_1 (cu->per_cu->dwarf2_per_objfile, addr_index,
19627 cu->addr_base, cu->header.addr_size);
3019eac3
DE
19628}
19629
19630/* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
19631
19632static CORE_ADDR
d521ce57 19633read_addr_index_from_leb128 (struct dwarf2_cu *cu, const gdb_byte *info_ptr,
3019eac3
DE
19634 unsigned int *bytes_read)
19635{
518817b3 19636 bfd *abfd = cu->per_cu->dwarf2_per_objfile->objfile->obfd;
3019eac3
DE
19637 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
19638
19639 return read_addr_index (cu, addr_index);
19640}
19641
19642/* Data structure to pass results from dwarf2_read_addr_index_reader
19643 back to dwarf2_read_addr_index. */
19644
19645struct dwarf2_read_addr_index_data
19646{
19647 ULONGEST addr_base;
19648 int addr_size;
19649};
19650
19651/* die_reader_func for dwarf2_read_addr_index. */
19652
19653static void
19654dwarf2_read_addr_index_reader (const struct die_reader_specs *reader,
d521ce57 19655 const gdb_byte *info_ptr,
3019eac3
DE
19656 struct die_info *comp_unit_die,
19657 int has_children,
19658 void *data)
19659{
19660 struct dwarf2_cu *cu = reader->cu;
19661 struct dwarf2_read_addr_index_data *aidata =
19662 (struct dwarf2_read_addr_index_data *) data;
19663
19664 aidata->addr_base = cu->addr_base;
19665 aidata->addr_size = cu->header.addr_size;
19666}
19667
19668/* Given an index in .debug_addr, fetch the value.
19669 NOTE: This can be called during dwarf expression evaluation,
19670 long after the debug information has been read, and thus per_cu->cu
19671 may no longer exist. */
19672
19673CORE_ADDR
19674dwarf2_read_addr_index (struct dwarf2_per_cu_data *per_cu,
19675 unsigned int addr_index)
19676{
ed2dc618
SM
19677 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
19678 struct objfile *objfile = dwarf2_per_objfile->objfile;
3019eac3
DE
19679 struct dwarf2_cu *cu = per_cu->cu;
19680 ULONGEST addr_base;
19681 int addr_size;
19682
3019eac3
DE
19683 /* We need addr_base and addr_size.
19684 If we don't have PER_CU->cu, we have to get it.
19685 Nasty, but the alternative is storing the needed info in PER_CU,
19686 which at this point doesn't seem justified: it's not clear how frequently
19687 it would get used and it would increase the size of every PER_CU.
19688 Entry points like dwarf2_per_cu_addr_size do a similar thing
19689 so we're not in uncharted territory here.
19690 Alas we need to be a bit more complicated as addr_base is contained
19691 in the DIE.
19692
19693 We don't need to read the entire CU(/TU).
19694 We just need the header and top level die.
a1b64ce1 19695
3019eac3 19696 IWBN to use the aging mechanism to let us lazily later discard the CU.
a1b64ce1 19697 For now we skip this optimization. */
3019eac3
DE
19698
19699 if (cu != NULL)
19700 {
19701 addr_base = cu->addr_base;
19702 addr_size = cu->header.addr_size;
19703 }
19704 else
19705 {
19706 struct dwarf2_read_addr_index_data aidata;
19707
a1b64ce1
DE
19708 /* Note: We can't use init_cutu_and_read_dies_simple here,
19709 we need addr_base. */
19710 init_cutu_and_read_dies (per_cu, NULL, 0, 0,
19711 dwarf2_read_addr_index_reader, &aidata);
3019eac3
DE
19712 addr_base = aidata.addr_base;
19713 addr_size = aidata.addr_size;
19714 }
19715
ed2dc618
SM
19716 return read_addr_index_1 (dwarf2_per_objfile, addr_index, addr_base,
19717 addr_size);
3019eac3
DE
19718}
19719
57d63ce2
DE
19720/* Given a DW_FORM_GNU_str_index, fetch the string.
19721 This is only used by the Fission support. */
3019eac3 19722
d521ce57 19723static const char *
342587c4 19724read_str_index (const struct die_reader_specs *reader, ULONGEST str_index)
3019eac3 19725{
ed2dc618 19726 struct dwarf2_cu *cu = reader->cu;
518817b3
SM
19727 struct dwarf2_per_objfile *dwarf2_per_objfile
19728 = cu->per_cu->dwarf2_per_objfile;
3019eac3 19729 struct objfile *objfile = dwarf2_per_objfile->objfile;
c5164cbc 19730 const char *objf_name = objfile_name (objfile);
3019eac3 19731 bfd *abfd = objfile->obfd;
73869dc2
DE
19732 struct dwarf2_section_info *str_section = &reader->dwo_file->sections.str;
19733 struct dwarf2_section_info *str_offsets_section =
19734 &reader->dwo_file->sections.str_offsets;
d521ce57 19735 const gdb_byte *info_ptr;
3019eac3 19736 ULONGEST str_offset;
57d63ce2 19737 static const char form_name[] = "DW_FORM_GNU_str_index";
3019eac3 19738
73869dc2
DE
19739 dwarf2_read_section (objfile, str_section);
19740 dwarf2_read_section (objfile, str_offsets_section);
19741 if (str_section->buffer == NULL)
57d63ce2 19742 error (_("%s used without .debug_str.dwo section"
9d8780f0
SM
19743 " in CU at offset %s [in module %s]"),
19744 form_name, sect_offset_str (cu->header.sect_off), objf_name);
73869dc2 19745 if (str_offsets_section->buffer == NULL)
57d63ce2 19746 error (_("%s used without .debug_str_offsets.dwo section"
9d8780f0
SM
19747 " in CU at offset %s [in module %s]"),
19748 form_name, sect_offset_str (cu->header.sect_off), objf_name);
73869dc2 19749 if (str_index * cu->header.offset_size >= str_offsets_section->size)
57d63ce2 19750 error (_("%s pointing outside of .debug_str_offsets.dwo"
9d8780f0
SM
19751 " section in CU at offset %s [in module %s]"),
19752 form_name, sect_offset_str (cu->header.sect_off), objf_name);
73869dc2 19753 info_ptr = (str_offsets_section->buffer
3019eac3
DE
19754 + str_index * cu->header.offset_size);
19755 if (cu->header.offset_size == 4)
19756 str_offset = bfd_get_32 (abfd, info_ptr);
19757 else
19758 str_offset = bfd_get_64 (abfd, info_ptr);
73869dc2 19759 if (str_offset >= str_section->size)
57d63ce2 19760 error (_("Offset from %s pointing outside of"
9d8780f0
SM
19761 " .debug_str.dwo section in CU at offset %s [in module %s]"),
19762 form_name, sect_offset_str (cu->header.sect_off), objf_name);
73869dc2 19763 return (const char *) (str_section->buffer + str_offset);
3019eac3
DE
19764}
19765
3019eac3
DE
19766/* Return the length of an LEB128 number in BUF. */
19767
19768static int
19769leb128_size (const gdb_byte *buf)
19770{
19771 const gdb_byte *begin = buf;
19772 gdb_byte byte;
19773
19774 while (1)
19775 {
19776 byte = *buf++;
19777 if ((byte & 128) == 0)
19778 return buf - begin;
19779 }
19780}
19781
c906108c 19782static void
e142c38c 19783set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
c906108c
SS
19784{
19785 switch (lang)
19786 {
19787 case DW_LANG_C89:
76bee0cc 19788 case DW_LANG_C99:
0cfd832f 19789 case DW_LANG_C11:
c906108c 19790 case DW_LANG_C:
d1be3247 19791 case DW_LANG_UPC:
e142c38c 19792 cu->language = language_c;
c906108c 19793 break;
9c37b5ae 19794 case DW_LANG_Java:
c906108c 19795 case DW_LANG_C_plus_plus:
0cfd832f
MW
19796 case DW_LANG_C_plus_plus_11:
19797 case DW_LANG_C_plus_plus_14:
e142c38c 19798 cu->language = language_cplus;
c906108c 19799 break;
6aecb9c2
JB
19800 case DW_LANG_D:
19801 cu->language = language_d;
19802 break;
c906108c
SS
19803 case DW_LANG_Fortran77:
19804 case DW_LANG_Fortran90:
b21b22e0 19805 case DW_LANG_Fortran95:
f7de9aab
MW
19806 case DW_LANG_Fortran03:
19807 case DW_LANG_Fortran08:
e142c38c 19808 cu->language = language_fortran;
c906108c 19809 break;
a766d390
DE
19810 case DW_LANG_Go:
19811 cu->language = language_go;
19812 break;
c906108c 19813 case DW_LANG_Mips_Assembler:
e142c38c 19814 cu->language = language_asm;
c906108c
SS
19815 break;
19816 case DW_LANG_Ada83:
8aaf0b47 19817 case DW_LANG_Ada95:
bc5f45f8
JB
19818 cu->language = language_ada;
19819 break;
72019c9c
GM
19820 case DW_LANG_Modula2:
19821 cu->language = language_m2;
19822 break;
fe8e67fd
PM
19823 case DW_LANG_Pascal83:
19824 cu->language = language_pascal;
19825 break;
22566fbd
DJ
19826 case DW_LANG_ObjC:
19827 cu->language = language_objc;
19828 break;
c44af4eb
TT
19829 case DW_LANG_Rust:
19830 case DW_LANG_Rust_old:
19831 cu->language = language_rust;
19832 break;
c906108c
SS
19833 case DW_LANG_Cobol74:
19834 case DW_LANG_Cobol85:
c906108c 19835 default:
e142c38c 19836 cu->language = language_minimal;
c906108c
SS
19837 break;
19838 }
e142c38c 19839 cu->language_defn = language_def (cu->language);
c906108c
SS
19840}
19841
19842/* Return the named attribute or NULL if not there. */
19843
19844static struct attribute *
e142c38c 19845dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
c906108c 19846{
a48e046c 19847 for (;;)
c906108c 19848 {
a48e046c
TT
19849 unsigned int i;
19850 struct attribute *spec = NULL;
19851
19852 for (i = 0; i < die->num_attrs; ++i)
19853 {
19854 if (die->attrs[i].name == name)
19855 return &die->attrs[i];
19856 if (die->attrs[i].name == DW_AT_specification
19857 || die->attrs[i].name == DW_AT_abstract_origin)
19858 spec = &die->attrs[i];
19859 }
19860
19861 if (!spec)
19862 break;
c906108c 19863
f2f0e013 19864 die = follow_die_ref (die, spec, &cu);
f2f0e013 19865 }
c5aa993b 19866
c906108c
SS
19867 return NULL;
19868}
19869
348e048f
DE
19870/* Return the named attribute or NULL if not there,
19871 but do not follow DW_AT_specification, etc.
19872 This is for use in contexts where we're reading .debug_types dies.
19873 Following DW_AT_specification, DW_AT_abstract_origin will take us
19874 back up the chain, and we want to go down. */
19875
19876static struct attribute *
45e58e77 19877dwarf2_attr_no_follow (struct die_info *die, unsigned int name)
348e048f
DE
19878{
19879 unsigned int i;
19880
19881 for (i = 0; i < die->num_attrs; ++i)
19882 if (die->attrs[i].name == name)
19883 return &die->attrs[i];
19884
19885 return NULL;
19886}
19887
7d45c7c3
KB
19888/* Return the string associated with a string-typed attribute, or NULL if it
19889 is either not found or is of an incorrect type. */
19890
19891static const char *
19892dwarf2_string_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
19893{
19894 struct attribute *attr;
19895 const char *str = NULL;
19896
19897 attr = dwarf2_attr (die, name, cu);
19898
19899 if (attr != NULL)
19900 {
43988095 19901 if (attr->form == DW_FORM_strp || attr->form == DW_FORM_line_strp
b3340438
L
19902 || attr->form == DW_FORM_string
19903 || attr->form == DW_FORM_GNU_str_index
16eb6b2d 19904 || attr->form == DW_FORM_GNU_strp_alt)
7d45c7c3
KB
19905 str = DW_STRING (attr);
19906 else
19907 complaint (&symfile_complaints,
19908 _("string type expected for attribute %s for "
9d8780f0
SM
19909 "DIE at %s in module %s"),
19910 dwarf_attr_name (name), sect_offset_str (die->sect_off),
518817b3 19911 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
7d45c7c3
KB
19912 }
19913
19914 return str;
19915}
19916
05cf31d1
JB
19917/* Return non-zero iff the attribute NAME is defined for the given DIE,
19918 and holds a non-zero value. This function should only be used for
2dc7f7b3 19919 DW_FORM_flag or DW_FORM_flag_present attributes. */
05cf31d1
JB
19920
19921static int
19922dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
19923{
19924 struct attribute *attr = dwarf2_attr (die, name, cu);
19925
19926 return (attr && DW_UNSND (attr));
19927}
19928
3ca72b44 19929static int
e142c38c 19930die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
3ca72b44 19931{
05cf31d1
JB
19932 /* A DIE is a declaration if it has a DW_AT_declaration attribute
19933 which value is non-zero. However, we have to be careful with
19934 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
19935 (via dwarf2_flag_true_p) follows this attribute. So we may
19936 end up accidently finding a declaration attribute that belongs
19937 to a different DIE referenced by the specification attribute,
19938 even though the given DIE does not have a declaration attribute. */
19939 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
19940 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
3ca72b44
AC
19941}
19942
63d06c5c 19943/* Return the die giving the specification for DIE, if there is
f2f0e013 19944 one. *SPEC_CU is the CU containing DIE on input, and the CU
edb3359d
DJ
19945 containing the return value on output. If there is no
19946 specification, but there is an abstract origin, that is
19947 returned. */
63d06c5c
DC
19948
19949static struct die_info *
f2f0e013 19950die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
63d06c5c 19951{
f2f0e013
DJ
19952 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
19953 *spec_cu);
63d06c5c 19954
edb3359d
DJ
19955 if (spec_attr == NULL)
19956 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
19957
63d06c5c
DC
19958 if (spec_attr == NULL)
19959 return NULL;
19960 else
f2f0e013 19961 return follow_die_ref (die, spec_attr, spec_cu);
63d06c5c 19962}
c906108c 19963
527f3840
JK
19964/* Stub for free_line_header to match void * callback types. */
19965
19966static void
19967free_line_header_voidp (void *arg)
19968{
9a3c8263 19969 struct line_header *lh = (struct line_header *) arg;
527f3840 19970
fff8551c 19971 delete lh;
527f3840
JK
19972}
19973
fff8551c
PA
19974void
19975line_header::add_include_dir (const char *include_dir)
c906108c 19976{
27e0867f 19977 if (dwarf_line_debug >= 2)
fff8551c
PA
19978 fprintf_unfiltered (gdb_stdlog, "Adding dir %zu: %s\n",
19979 include_dirs.size () + 1, include_dir);
27e0867f 19980
fff8551c 19981 include_dirs.push_back (include_dir);
debd256d 19982}
6e70227d 19983
fff8551c
PA
19984void
19985line_header::add_file_name (const char *name,
ecfb656c 19986 dir_index d_index,
fff8551c
PA
19987 unsigned int mod_time,
19988 unsigned int length)
debd256d 19989{
27e0867f
DE
19990 if (dwarf_line_debug >= 2)
19991 fprintf_unfiltered (gdb_stdlog, "Adding file %u: %s\n",
fff8551c 19992 (unsigned) file_names.size () + 1, name);
27e0867f 19993
ecfb656c 19994 file_names.emplace_back (name, d_index, mod_time, length);
debd256d 19995}
6e70227d 19996
83769d0b 19997/* A convenience function to find the proper .debug_line section for a CU. */
36586728
TT
19998
19999static struct dwarf2_section_info *
20000get_debug_line_section (struct dwarf2_cu *cu)
20001{
20002 struct dwarf2_section_info *section;
518817b3
SM
20003 struct dwarf2_per_objfile *dwarf2_per_objfile
20004 = cu->per_cu->dwarf2_per_objfile;
36586728
TT
20005
20006 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
20007 DWO file. */
20008 if (cu->dwo_unit && cu->per_cu->is_debug_types)
20009 section = &cu->dwo_unit->dwo_file->sections.line;
20010 else if (cu->per_cu->is_dwz)
20011 {
ed2dc618 20012 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
36586728
TT
20013
20014 section = &dwz->line;
20015 }
20016 else
20017 section = &dwarf2_per_objfile->line;
20018
20019 return section;
20020}
20021
43988095
JK
20022/* Read directory or file name entry format, starting with byte of
20023 format count entries, ULEB128 pairs of entry formats, ULEB128 of
20024 entries count and the entries themselves in the described entry
20025 format. */
20026
20027static void
ed2dc618
SM
20028read_formatted_entries (struct dwarf2_per_objfile *dwarf2_per_objfile,
20029 bfd *abfd, const gdb_byte **bufp,
43988095
JK
20030 struct line_header *lh,
20031 const struct comp_unit_head *cu_header,
20032 void (*callback) (struct line_header *lh,
20033 const char *name,
ecfb656c 20034 dir_index d_index,
43988095
JK
20035 unsigned int mod_time,
20036 unsigned int length))
20037{
20038 gdb_byte format_count, formati;
20039 ULONGEST data_count, datai;
20040 const gdb_byte *buf = *bufp;
20041 const gdb_byte *format_header_data;
43988095
JK
20042 unsigned int bytes_read;
20043
20044 format_count = read_1_byte (abfd, buf);
20045 buf += 1;
20046 format_header_data = buf;
20047 for (formati = 0; formati < format_count; formati++)
20048 {
20049 read_unsigned_leb128 (abfd, buf, &bytes_read);
20050 buf += bytes_read;
20051 read_unsigned_leb128 (abfd, buf, &bytes_read);
20052 buf += bytes_read;
20053 }
20054
20055 data_count = read_unsigned_leb128 (abfd, buf, &bytes_read);
20056 buf += bytes_read;
20057 for (datai = 0; datai < data_count; datai++)
20058 {
20059 const gdb_byte *format = format_header_data;
20060 struct file_entry fe;
20061
43988095
JK
20062 for (formati = 0; formati < format_count; formati++)
20063 {
ecfb656c 20064 ULONGEST content_type = read_unsigned_leb128 (abfd, format, &bytes_read);
43988095 20065 format += bytes_read;
43988095 20066
ecfb656c 20067 ULONGEST form = read_unsigned_leb128 (abfd, format, &bytes_read);
43988095 20068 format += bytes_read;
ecfb656c
PA
20069
20070 gdb::optional<const char *> string;
20071 gdb::optional<unsigned int> uint;
20072
43988095
JK
20073 switch (form)
20074 {
20075 case DW_FORM_string:
ecfb656c 20076 string.emplace (read_direct_string (abfd, buf, &bytes_read));
43988095
JK
20077 buf += bytes_read;
20078 break;
20079
20080 case DW_FORM_line_strp:
ed2dc618
SM
20081 string.emplace (read_indirect_line_string (dwarf2_per_objfile,
20082 abfd, buf,
ecfb656c
PA
20083 cu_header,
20084 &bytes_read));
43988095
JK
20085 buf += bytes_read;
20086 break;
20087
20088 case DW_FORM_data1:
ecfb656c 20089 uint.emplace (read_1_byte (abfd, buf));
43988095
JK
20090 buf += 1;
20091 break;
20092
20093 case DW_FORM_data2:
ecfb656c 20094 uint.emplace (read_2_bytes (abfd, buf));
43988095
JK
20095 buf += 2;
20096 break;
20097
20098 case DW_FORM_data4:
ecfb656c 20099 uint.emplace (read_4_bytes (abfd, buf));
43988095
JK
20100 buf += 4;
20101 break;
20102
20103 case DW_FORM_data8:
ecfb656c 20104 uint.emplace (read_8_bytes (abfd, buf));
43988095
JK
20105 buf += 8;
20106 break;
20107
20108 case DW_FORM_udata:
ecfb656c 20109 uint.emplace (read_unsigned_leb128 (abfd, buf, &bytes_read));
43988095
JK
20110 buf += bytes_read;
20111 break;
20112
20113 case DW_FORM_block:
20114 /* It is valid only for DW_LNCT_timestamp which is ignored by
20115 current GDB. */
20116 break;
20117 }
ecfb656c
PA
20118
20119 switch (content_type)
20120 {
20121 case DW_LNCT_path:
20122 if (string.has_value ())
20123 fe.name = *string;
20124 break;
20125 case DW_LNCT_directory_index:
20126 if (uint.has_value ())
20127 fe.d_index = (dir_index) *uint;
20128 break;
20129 case DW_LNCT_timestamp:
20130 if (uint.has_value ())
20131 fe.mod_time = *uint;
20132 break;
20133 case DW_LNCT_size:
20134 if (uint.has_value ())
20135 fe.length = *uint;
20136 break;
20137 case DW_LNCT_MD5:
20138 break;
20139 default:
20140 complaint (&symfile_complaints,
20141 _("Unknown format content type %s"),
20142 pulongest (content_type));
20143 }
43988095
JK
20144 }
20145
ecfb656c 20146 callback (lh, fe.name, fe.d_index, fe.mod_time, fe.length);
43988095
JK
20147 }
20148
20149 *bufp = buf;
20150}
20151
debd256d 20152/* Read the statement program header starting at OFFSET in
3019eac3 20153 .debug_line, or .debug_line.dwo. Return a pointer
6502dd73 20154 to a struct line_header, allocated using xmalloc.
cd366ee8
DE
20155 Returns NULL if there is a problem reading the header, e.g., if it
20156 has a version we don't understand.
debd256d
JB
20157
20158 NOTE: the strings in the include directory and file name tables of
3019eac3
DE
20159 the returned object point into the dwarf line section buffer,
20160 and must not be freed. */
ae2de4f8 20161
fff8551c 20162static line_header_up
9c541725 20163dwarf_decode_line_header (sect_offset sect_off, struct dwarf2_cu *cu)
debd256d 20164{
d521ce57 20165 const gdb_byte *line_ptr;
c764a876 20166 unsigned int bytes_read, offset_size;
debd256d 20167 int i;
d521ce57 20168 const char *cur_dir, *cur_file;
3019eac3
DE
20169 struct dwarf2_section_info *section;
20170 bfd *abfd;
518817b3
SM
20171 struct dwarf2_per_objfile *dwarf2_per_objfile
20172 = cu->per_cu->dwarf2_per_objfile;
3019eac3 20173
36586728 20174 section = get_debug_line_section (cu);
3019eac3
DE
20175 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
20176 if (section->buffer == NULL)
debd256d 20177 {
3019eac3
DE
20178 if (cu->dwo_unit && cu->per_cu->is_debug_types)
20179 complaint (&symfile_complaints, _("missing .debug_line.dwo section"));
20180 else
20181 complaint (&symfile_complaints, _("missing .debug_line section"));
debd256d
JB
20182 return 0;
20183 }
20184
fceca515
DE
20185 /* We can't do this until we know the section is non-empty.
20186 Only then do we know we have such a section. */
a32a8923 20187 abfd = get_section_bfd_owner (section);
fceca515 20188
a738430d
MK
20189 /* Make sure that at least there's room for the total_length field.
20190 That could be 12 bytes long, but we're just going to fudge that. */
9c541725 20191 if (to_underlying (sect_off) + 4 >= section->size)
debd256d 20192 {
4d3c2250 20193 dwarf2_statement_list_fits_in_line_number_section_complaint ();
debd256d
JB
20194 return 0;
20195 }
20196
fff8551c 20197 line_header_up lh (new line_header ());
debd256d 20198
9c541725 20199 lh->sect_off = sect_off;
527f3840
JK
20200 lh->offset_in_dwz = cu->per_cu->is_dwz;
20201
9c541725 20202 line_ptr = section->buffer + to_underlying (sect_off);
debd256d 20203
a738430d 20204 /* Read in the header. */
6e70227d 20205 lh->total_length =
c764a876
DE
20206 read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
20207 &bytes_read, &offset_size);
debd256d 20208 line_ptr += bytes_read;
3019eac3 20209 if (line_ptr + lh->total_length > (section->buffer + section->size))
debd256d 20210 {
4d3c2250 20211 dwarf2_statement_list_fits_in_line_number_section_complaint ();
debd256d
JB
20212 return 0;
20213 }
20214 lh->statement_program_end = line_ptr + lh->total_length;
20215 lh->version = read_2_bytes (abfd, line_ptr);
20216 line_ptr += 2;
43988095 20217 if (lh->version > 5)
cd366ee8
DE
20218 {
20219 /* This is a version we don't understand. The format could have
20220 changed in ways we don't handle properly so just punt. */
20221 complaint (&symfile_complaints,
20222 _("unsupported version in .debug_line section"));
20223 return NULL;
20224 }
43988095
JK
20225 if (lh->version >= 5)
20226 {
20227 gdb_byte segment_selector_size;
20228
20229 /* Skip address size. */
20230 read_1_byte (abfd, line_ptr);
20231 line_ptr += 1;
20232
20233 segment_selector_size = read_1_byte (abfd, line_ptr);
20234 line_ptr += 1;
20235 if (segment_selector_size != 0)
20236 {
20237 complaint (&symfile_complaints,
20238 _("unsupported segment selector size %u "
20239 "in .debug_line section"),
20240 segment_selector_size);
20241 return NULL;
20242 }
20243 }
c764a876
DE
20244 lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
20245 line_ptr += offset_size;
debd256d
JB
20246 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
20247 line_ptr += 1;
2dc7f7b3
TT
20248 if (lh->version >= 4)
20249 {
20250 lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
20251 line_ptr += 1;
20252 }
20253 else
20254 lh->maximum_ops_per_instruction = 1;
20255
20256 if (lh->maximum_ops_per_instruction == 0)
20257 {
20258 lh->maximum_ops_per_instruction = 1;
20259 complaint (&symfile_complaints,
3e43a32a
MS
20260 _("invalid maximum_ops_per_instruction "
20261 "in `.debug_line' section"));
2dc7f7b3
TT
20262 }
20263
debd256d
JB
20264 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
20265 line_ptr += 1;
20266 lh->line_base = read_1_signed_byte (abfd, line_ptr);
20267 line_ptr += 1;
20268 lh->line_range = read_1_byte (abfd, line_ptr);
20269 line_ptr += 1;
20270 lh->opcode_base = read_1_byte (abfd, line_ptr);
20271 line_ptr += 1;
fff8551c 20272 lh->standard_opcode_lengths.reset (new unsigned char[lh->opcode_base]);
debd256d
JB
20273
20274 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
20275 for (i = 1; i < lh->opcode_base; ++i)
20276 {
20277 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
20278 line_ptr += 1;
20279 }
20280
43988095 20281 if (lh->version >= 5)
debd256d 20282 {
43988095 20283 /* Read directory table. */
ed2dc618
SM
20284 read_formatted_entries (dwarf2_per_objfile, abfd, &line_ptr, lh.get (),
20285 &cu->header,
fff8551c 20286 [] (struct line_header *lh, const char *name,
ecfb656c 20287 dir_index d_index, unsigned int mod_time,
fff8551c
PA
20288 unsigned int length)
20289 {
20290 lh->add_include_dir (name);
20291 });
debd256d 20292
43988095 20293 /* Read file name table. */
ed2dc618
SM
20294 read_formatted_entries (dwarf2_per_objfile, abfd, &line_ptr, lh.get (),
20295 &cu->header,
fff8551c 20296 [] (struct line_header *lh, const char *name,
ecfb656c 20297 dir_index d_index, unsigned int mod_time,
fff8551c
PA
20298 unsigned int length)
20299 {
ecfb656c 20300 lh->add_file_name (name, d_index, mod_time, length);
fff8551c 20301 });
43988095
JK
20302 }
20303 else
debd256d 20304 {
43988095
JK
20305 /* Read directory table. */
20306 while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
20307 {
20308 line_ptr += bytes_read;
fff8551c 20309 lh->add_include_dir (cur_dir);
43988095 20310 }
debd256d
JB
20311 line_ptr += bytes_read;
20312
43988095
JK
20313 /* Read file name table. */
20314 while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
20315 {
ecfb656c
PA
20316 unsigned int mod_time, length;
20317 dir_index d_index;
43988095
JK
20318
20319 line_ptr += bytes_read;
ecfb656c 20320 d_index = (dir_index) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
43988095
JK
20321 line_ptr += bytes_read;
20322 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20323 line_ptr += bytes_read;
20324 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20325 line_ptr += bytes_read;
20326
ecfb656c 20327 lh->add_file_name (cur_file, d_index, mod_time, length);
43988095
JK
20328 }
20329 line_ptr += bytes_read;
debd256d 20330 }
6e70227d 20331 lh->statement_program_start = line_ptr;
debd256d 20332
3019eac3 20333 if (line_ptr > (section->buffer + section->size))
4d3c2250 20334 complaint (&symfile_complaints,
3e43a32a
MS
20335 _("line number info header doesn't "
20336 "fit in `.debug_line' section"));
debd256d 20337
debd256d
JB
20338 return lh;
20339}
c906108c 20340
c6da4cef
DE
20341/* Subroutine of dwarf_decode_lines to simplify it.
20342 Return the file name of the psymtab for included file FILE_INDEX
20343 in line header LH of PST.
20344 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
c89b44cd
TT
20345 If space for the result is malloc'd, *NAME_HOLDER will be set.
20346 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
c6da4cef 20347
d521ce57 20348static const char *
c6da4cef
DE
20349psymtab_include_file_name (const struct line_header *lh, int file_index,
20350 const struct partial_symtab *pst,
c89b44cd
TT
20351 const char *comp_dir,
20352 gdb::unique_xmalloc_ptr<char> *name_holder)
c6da4cef 20353{
8c43009f 20354 const file_entry &fe = lh->file_names[file_index];
d521ce57
TT
20355 const char *include_name = fe.name;
20356 const char *include_name_to_compare = include_name;
72b9f47f 20357 const char *pst_filename;
c6da4cef
DE
20358 int file_is_pst;
20359
8c43009f 20360 const char *dir_name = fe.include_dir (lh);
c6da4cef 20361
c89b44cd 20362 gdb::unique_xmalloc_ptr<char> hold_compare;
c6da4cef
DE
20363 if (!IS_ABSOLUTE_PATH (include_name)
20364 && (dir_name != NULL || comp_dir != NULL))
20365 {
20366 /* Avoid creating a duplicate psymtab for PST.
20367 We do this by comparing INCLUDE_NAME and PST_FILENAME.
20368 Before we do the comparison, however, we need to account
20369 for DIR_NAME and COMP_DIR.
20370 First prepend dir_name (if non-NULL). If we still don't
20371 have an absolute path prepend comp_dir (if non-NULL).
20372 However, the directory we record in the include-file's
20373 psymtab does not contain COMP_DIR (to match the
20374 corresponding symtab(s)).
20375
20376 Example:
20377
20378 bash$ cd /tmp
20379 bash$ gcc -g ./hello.c
20380 include_name = "hello.c"
20381 dir_name = "."
20382 DW_AT_comp_dir = comp_dir = "/tmp"
5f52445b
YQ
20383 DW_AT_name = "./hello.c"
20384
20385 */
c6da4cef
DE
20386
20387 if (dir_name != NULL)
20388 {
c89b44cd
TT
20389 name_holder->reset (concat (dir_name, SLASH_STRING,
20390 include_name, (char *) NULL));
20391 include_name = name_holder->get ();
c6da4cef 20392 include_name_to_compare = include_name;
c6da4cef
DE
20393 }
20394 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
20395 {
c89b44cd
TT
20396 hold_compare.reset (concat (comp_dir, SLASH_STRING,
20397 include_name, (char *) NULL));
20398 include_name_to_compare = hold_compare.get ();
c6da4cef
DE
20399 }
20400 }
20401
20402 pst_filename = pst->filename;
c89b44cd 20403 gdb::unique_xmalloc_ptr<char> copied_name;
c6da4cef
DE
20404 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
20405 {
c89b44cd
TT
20406 copied_name.reset (concat (pst->dirname, SLASH_STRING,
20407 pst_filename, (char *) NULL));
20408 pst_filename = copied_name.get ();
c6da4cef
DE
20409 }
20410
1e3fad37 20411 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
c6da4cef 20412
c6da4cef
DE
20413 if (file_is_pst)
20414 return NULL;
20415 return include_name;
20416}
20417
d9b3de22
DE
20418/* State machine to track the state of the line number program. */
20419
6f77053d 20420class lnp_state_machine
d9b3de22 20421{
6f77053d
PA
20422public:
20423 /* Initialize a machine state for the start of a line number
20424 program. */
20425 lnp_state_machine (gdbarch *arch, line_header *lh, bool record_lines_p);
20426
8c43009f
PA
20427 file_entry *current_file ()
20428 {
20429 /* lh->file_names is 0-based, but the file name numbers in the
20430 statement program are 1-based. */
6f77053d
PA
20431 return m_line_header->file_name_at (m_file);
20432 }
20433
20434 /* Record the line in the state machine. END_SEQUENCE is true if
20435 we're processing the end of a sequence. */
20436 void record_line (bool end_sequence);
20437
20438 /* Check address and if invalid nop-out the rest of the lines in this
20439 sequence. */
20440 void check_line_address (struct dwarf2_cu *cu,
20441 const gdb_byte *line_ptr,
20442 CORE_ADDR lowpc, CORE_ADDR address);
20443
20444 void handle_set_discriminator (unsigned int discriminator)
20445 {
20446 m_discriminator = discriminator;
20447 m_line_has_non_zero_discriminator |= discriminator != 0;
20448 }
20449
20450 /* Handle DW_LNE_set_address. */
20451 void handle_set_address (CORE_ADDR baseaddr, CORE_ADDR address)
20452 {
20453 m_op_index = 0;
20454 address += baseaddr;
20455 m_address = gdbarch_adjust_dwarf2_line (m_gdbarch, address, false);
20456 }
20457
20458 /* Handle DW_LNS_advance_pc. */
20459 void handle_advance_pc (CORE_ADDR adjust);
20460
20461 /* Handle a special opcode. */
20462 void handle_special_opcode (unsigned char op_code);
20463
20464 /* Handle DW_LNS_advance_line. */
20465 void handle_advance_line (int line_delta)
20466 {
20467 advance_line (line_delta);
20468 }
20469
20470 /* Handle DW_LNS_set_file. */
20471 void handle_set_file (file_name_index file);
20472
20473 /* Handle DW_LNS_negate_stmt. */
20474 void handle_negate_stmt ()
20475 {
20476 m_is_stmt = !m_is_stmt;
20477 }
20478
20479 /* Handle DW_LNS_const_add_pc. */
20480 void handle_const_add_pc ();
20481
20482 /* Handle DW_LNS_fixed_advance_pc. */
20483 void handle_fixed_advance_pc (CORE_ADDR addr_adj)
20484 {
20485 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20486 m_op_index = 0;
20487 }
20488
20489 /* Handle DW_LNS_copy. */
20490 void handle_copy ()
20491 {
20492 record_line (false);
20493 m_discriminator = 0;
20494 }
20495
20496 /* Handle DW_LNE_end_sequence. */
20497 void handle_end_sequence ()
20498 {
20499 m_record_line_callback = ::record_line;
20500 }
20501
20502private:
20503 /* Advance the line by LINE_DELTA. */
20504 void advance_line (int line_delta)
20505 {
20506 m_line += line_delta;
20507
20508 if (line_delta != 0)
20509 m_line_has_non_zero_discriminator = m_discriminator != 0;
8c43009f
PA
20510 }
20511
6f77053d
PA
20512 gdbarch *m_gdbarch;
20513
20514 /* True if we're recording lines.
20515 Otherwise we're building partial symtabs and are just interested in
20516 finding include files mentioned by the line number program. */
20517 bool m_record_lines_p;
20518
8c43009f 20519 /* The line number header. */
6f77053d 20520 line_header *m_line_header;
8c43009f 20521
6f77053d
PA
20522 /* These are part of the standard DWARF line number state machine,
20523 and initialized according to the DWARF spec. */
d9b3de22 20524
6f77053d 20525 unsigned char m_op_index = 0;
8c43009f 20526 /* The line table index (1-based) of the current file. */
6f77053d
PA
20527 file_name_index m_file = (file_name_index) 1;
20528 unsigned int m_line = 1;
20529
20530 /* These are initialized in the constructor. */
20531
20532 CORE_ADDR m_address;
20533 bool m_is_stmt;
20534 unsigned int m_discriminator;
d9b3de22
DE
20535
20536 /* Additional bits of state we need to track. */
20537
20538 /* The last file that we called dwarf2_start_subfile for.
20539 This is only used for TLLs. */
6f77053d 20540 unsigned int m_last_file = 0;
d9b3de22 20541 /* The last file a line number was recorded for. */
6f77053d 20542 struct subfile *m_last_subfile = NULL;
d9b3de22
DE
20543
20544 /* The function to call to record a line. */
6f77053d 20545 record_line_ftype *m_record_line_callback = NULL;
d9b3de22
DE
20546
20547 /* The last line number that was recorded, used to coalesce
20548 consecutive entries for the same line. This can happen, for
20549 example, when discriminators are present. PR 17276. */
6f77053d
PA
20550 unsigned int m_last_line = 0;
20551 bool m_line_has_non_zero_discriminator = false;
8c43009f 20552};
d9b3de22 20553
6f77053d
PA
20554void
20555lnp_state_machine::handle_advance_pc (CORE_ADDR adjust)
20556{
20557 CORE_ADDR addr_adj = (((m_op_index + adjust)
20558 / m_line_header->maximum_ops_per_instruction)
20559 * m_line_header->minimum_instruction_length);
20560 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20561 m_op_index = ((m_op_index + adjust)
20562 % m_line_header->maximum_ops_per_instruction);
20563}
d9b3de22 20564
6f77053d
PA
20565void
20566lnp_state_machine::handle_special_opcode (unsigned char op_code)
d9b3de22 20567{
6f77053d
PA
20568 unsigned char adj_opcode = op_code - m_line_header->opcode_base;
20569 CORE_ADDR addr_adj = (((m_op_index
20570 + (adj_opcode / m_line_header->line_range))
20571 / m_line_header->maximum_ops_per_instruction)
20572 * m_line_header->minimum_instruction_length);
20573 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20574 m_op_index = ((m_op_index + (adj_opcode / m_line_header->line_range))
20575 % m_line_header->maximum_ops_per_instruction);
d9b3de22 20576
6f77053d
PA
20577 int line_delta = (m_line_header->line_base
20578 + (adj_opcode % m_line_header->line_range));
20579 advance_line (line_delta);
20580 record_line (false);
20581 m_discriminator = 0;
20582}
d9b3de22 20583
6f77053d
PA
20584void
20585lnp_state_machine::handle_set_file (file_name_index file)
20586{
20587 m_file = file;
20588
20589 const file_entry *fe = current_file ();
20590 if (fe == NULL)
20591 dwarf2_debug_line_missing_file_complaint ();
20592 else if (m_record_lines_p)
20593 {
20594 const char *dir = fe->include_dir (m_line_header);
20595
20596 m_last_subfile = current_subfile;
20597 m_line_has_non_zero_discriminator = m_discriminator != 0;
20598 dwarf2_start_subfile (fe->name, dir);
20599 }
20600}
20601
20602void
20603lnp_state_machine::handle_const_add_pc ()
20604{
20605 CORE_ADDR adjust
20606 = (255 - m_line_header->opcode_base) / m_line_header->line_range;
20607
20608 CORE_ADDR addr_adj
20609 = (((m_op_index + adjust)
20610 / m_line_header->maximum_ops_per_instruction)
20611 * m_line_header->minimum_instruction_length);
20612
20613 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20614 m_op_index = ((m_op_index + adjust)
20615 % m_line_header->maximum_ops_per_instruction);
20616}
d9b3de22 20617
c91513d8
PP
20618/* Ignore this record_line request. */
20619
20620static void
20621noop_record_line (struct subfile *subfile, int line, CORE_ADDR pc)
20622{
20623 return;
20624}
20625
a05a36a5
DE
20626/* Return non-zero if we should add LINE to the line number table.
20627 LINE is the line to add, LAST_LINE is the last line that was added,
20628 LAST_SUBFILE is the subfile for LAST_LINE.
20629 LINE_HAS_NON_ZERO_DISCRIMINATOR is non-zero if LINE has ever
20630 had a non-zero discriminator.
20631
20632 We have to be careful in the presence of discriminators.
20633 E.g., for this line:
20634
20635 for (i = 0; i < 100000; i++);
20636
20637 clang can emit four line number entries for that one line,
20638 each with a different discriminator.
20639 See gdb.dwarf2/dw2-single-line-discriminators.exp for an example.
20640
20641 However, we want gdb to coalesce all four entries into one.
20642 Otherwise the user could stepi into the middle of the line and
20643 gdb would get confused about whether the pc really was in the
20644 middle of the line.
20645
20646 Things are further complicated by the fact that two consecutive
20647 line number entries for the same line is a heuristic used by gcc
20648 to denote the end of the prologue. So we can't just discard duplicate
20649 entries, we have to be selective about it. The heuristic we use is
20650 that we only collapse consecutive entries for the same line if at least
20651 one of those entries has a non-zero discriminator. PR 17276.
20652
20653 Note: Addresses in the line number state machine can never go backwards
20654 within one sequence, thus this coalescing is ok. */
20655
20656static int
20657dwarf_record_line_p (unsigned int line, unsigned int last_line,
20658 int line_has_non_zero_discriminator,
20659 struct subfile *last_subfile)
20660{
20661 if (current_subfile != last_subfile)
20662 return 1;
20663 if (line != last_line)
20664 return 1;
20665 /* Same line for the same file that we've seen already.
20666 As a last check, for pr 17276, only record the line if the line
20667 has never had a non-zero discriminator. */
20668 if (!line_has_non_zero_discriminator)
20669 return 1;
20670 return 0;
20671}
20672
252a6764
DE
20673/* Use P_RECORD_LINE to record line number LINE beginning at address ADDRESS
20674 in the line table of subfile SUBFILE. */
20675
20676static void
d9b3de22
DE
20677dwarf_record_line_1 (struct gdbarch *gdbarch, struct subfile *subfile,
20678 unsigned int line, CORE_ADDR address,
20679 record_line_ftype p_record_line)
252a6764
DE
20680{
20681 CORE_ADDR addr = gdbarch_addr_bits_remove (gdbarch, address);
20682
27e0867f
DE
20683 if (dwarf_line_debug)
20684 {
20685 fprintf_unfiltered (gdb_stdlog,
20686 "Recording line %u, file %s, address %s\n",
20687 line, lbasename (subfile->name),
20688 paddress (gdbarch, address));
20689 }
20690
d5962de5 20691 (*p_record_line) (subfile, line, addr);
252a6764
DE
20692}
20693
20694/* Subroutine of dwarf_decode_lines_1 to simplify it.
20695 Mark the end of a set of line number records.
d9b3de22 20696 The arguments are the same as for dwarf_record_line_1.
252a6764
DE
20697 If SUBFILE is NULL the request is ignored. */
20698
20699static void
20700dwarf_finish_line (struct gdbarch *gdbarch, struct subfile *subfile,
20701 CORE_ADDR address, record_line_ftype p_record_line)
20702{
27e0867f
DE
20703 if (subfile == NULL)
20704 return;
20705
20706 if (dwarf_line_debug)
20707 {
20708 fprintf_unfiltered (gdb_stdlog,
20709 "Finishing current line, file %s, address %s\n",
20710 lbasename (subfile->name),
20711 paddress (gdbarch, address));
20712 }
20713
d9b3de22
DE
20714 dwarf_record_line_1 (gdbarch, subfile, 0, address, p_record_line);
20715}
20716
6f77053d
PA
20717void
20718lnp_state_machine::record_line (bool end_sequence)
d9b3de22 20719{
d9b3de22
DE
20720 if (dwarf_line_debug)
20721 {
20722 fprintf_unfiltered (gdb_stdlog,
20723 "Processing actual line %u: file %u,"
20724 " address %s, is_stmt %u, discrim %u\n",
6f77053d
PA
20725 m_line, to_underlying (m_file),
20726 paddress (m_gdbarch, m_address),
20727 m_is_stmt, m_discriminator);
d9b3de22
DE
20728 }
20729
6f77053d 20730 file_entry *fe = current_file ();
8c43009f
PA
20731
20732 if (fe == NULL)
d9b3de22
DE
20733 dwarf2_debug_line_missing_file_complaint ();
20734 /* For now we ignore lines not starting on an instruction boundary.
20735 But not when processing end_sequence for compatibility with the
20736 previous version of the code. */
6f77053d 20737 else if (m_op_index == 0 || end_sequence)
d9b3de22 20738 {
8c43009f 20739 fe->included_p = 1;
6f77053d 20740 if (m_record_lines_p && m_is_stmt)
d9b3de22 20741 {
6f77053d 20742 if (m_last_subfile != current_subfile || end_sequence)
d9b3de22 20743 {
6f77053d
PA
20744 dwarf_finish_line (m_gdbarch, m_last_subfile,
20745 m_address, m_record_line_callback);
d9b3de22
DE
20746 }
20747
20748 if (!end_sequence)
20749 {
6f77053d
PA
20750 if (dwarf_record_line_p (m_line, m_last_line,
20751 m_line_has_non_zero_discriminator,
20752 m_last_subfile))
d9b3de22 20753 {
6f77053d
PA
20754 dwarf_record_line_1 (m_gdbarch, current_subfile,
20755 m_line, m_address,
20756 m_record_line_callback);
d9b3de22 20757 }
6f77053d
PA
20758 m_last_subfile = current_subfile;
20759 m_last_line = m_line;
d9b3de22
DE
20760 }
20761 }
20762 }
20763}
20764
6f77053d
PA
20765lnp_state_machine::lnp_state_machine (gdbarch *arch, line_header *lh,
20766 bool record_lines_p)
d9b3de22 20767{
6f77053d
PA
20768 m_gdbarch = arch;
20769 m_record_lines_p = record_lines_p;
20770 m_line_header = lh;
d9b3de22 20771
6f77053d 20772 m_record_line_callback = ::record_line;
d9b3de22 20773
d9b3de22
DE
20774 /* Call `gdbarch_adjust_dwarf2_line' on the initial 0 address as if there
20775 was a line entry for it so that the backend has a chance to adjust it
20776 and also record it in case it needs it. This is currently used by MIPS
20777 code, cf. `mips_adjust_dwarf2_line'. */
6f77053d
PA
20778 m_address = gdbarch_adjust_dwarf2_line (arch, 0, 0);
20779 m_is_stmt = lh->default_is_stmt;
20780 m_discriminator = 0;
252a6764
DE
20781}
20782
6f77053d
PA
20783void
20784lnp_state_machine::check_line_address (struct dwarf2_cu *cu,
20785 const gdb_byte *line_ptr,
20786 CORE_ADDR lowpc, CORE_ADDR address)
924c2928
DE
20787{
20788 /* If address < lowpc then it's not a usable value, it's outside the
20789 pc range of the CU. However, we restrict the test to only address
20790 values of zero to preserve GDB's previous behaviour which is to
20791 handle the specific case of a function being GC'd by the linker. */
20792
20793 if (address == 0 && address < lowpc)
20794 {
20795 /* This line table is for a function which has been
20796 GCd by the linker. Ignore it. PR gdb/12528 */
20797
518817b3 20798 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
924c2928
DE
20799 long line_offset = line_ptr - get_debug_line_section (cu)->buffer;
20800
20801 complaint (&symfile_complaints,
20802 _(".debug_line address at offset 0x%lx is 0 [in module %s]"),
20803 line_offset, objfile_name (objfile));
6f77053d
PA
20804 m_record_line_callback = noop_record_line;
20805 /* Note: record_line_callback is left as noop_record_line until
20806 we see DW_LNE_end_sequence. */
924c2928
DE
20807 }
20808}
20809
f3f5162e 20810/* Subroutine of dwarf_decode_lines to simplify it.
d9b3de22
DE
20811 Process the line number information in LH.
20812 If DECODE_FOR_PST_P is non-zero, all we do is process the line number
20813 program in order to set included_p for every referenced header. */
debd256d 20814
c906108c 20815static void
43f3e411
DE
20816dwarf_decode_lines_1 (struct line_header *lh, struct dwarf2_cu *cu,
20817 const int decode_for_pst_p, CORE_ADDR lowpc)
c906108c 20818{
d521ce57
TT
20819 const gdb_byte *line_ptr, *extended_end;
20820 const gdb_byte *line_end;
a8c50c1f 20821 unsigned int bytes_read, extended_len;
699ca60a 20822 unsigned char op_code, extended_op;
e142c38c 20823 CORE_ADDR baseaddr;
518817b3 20824 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
f3f5162e 20825 bfd *abfd = objfile->obfd;
fbf65064 20826 struct gdbarch *gdbarch = get_objfile_arch (objfile);
6f77053d
PA
20827 /* True if we're recording line info (as opposed to building partial
20828 symtabs and just interested in finding include files mentioned by
20829 the line number program). */
20830 bool record_lines_p = !decode_for_pst_p;
e142c38c
DJ
20831
20832 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 20833
debd256d
JB
20834 line_ptr = lh->statement_program_start;
20835 line_end = lh->statement_program_end;
c906108c
SS
20836
20837 /* Read the statement sequences until there's nothing left. */
20838 while (line_ptr < line_end)
20839 {
6f77053d
PA
20840 /* The DWARF line number program state machine. Reset the state
20841 machine at the start of each sequence. */
20842 lnp_state_machine state_machine (gdbarch, lh, record_lines_p);
20843 bool end_sequence = false;
d9b3de22 20844
8c43009f 20845 if (record_lines_p)
c906108c 20846 {
8c43009f
PA
20847 /* Start a subfile for the current file of the state
20848 machine. */
20849 const file_entry *fe = state_machine.current_file ();
20850
20851 if (fe != NULL)
20852 dwarf2_start_subfile (fe->name, fe->include_dir (lh));
c906108c
SS
20853 }
20854
a738430d 20855 /* Decode the table. */
d9b3de22 20856 while (line_ptr < line_end && !end_sequence)
c906108c
SS
20857 {
20858 op_code = read_1_byte (abfd, line_ptr);
20859 line_ptr += 1;
9aa1fe7e 20860
debd256d 20861 if (op_code >= lh->opcode_base)
6e70227d 20862 {
8e07a239 20863 /* Special opcode. */
6f77053d 20864 state_machine.handle_special_opcode (op_code);
9aa1fe7e
GK
20865 }
20866 else switch (op_code)
c906108c
SS
20867 {
20868 case DW_LNS_extended_op:
3e43a32a
MS
20869 extended_len = read_unsigned_leb128 (abfd, line_ptr,
20870 &bytes_read);
473b7be6 20871 line_ptr += bytes_read;
a8c50c1f 20872 extended_end = line_ptr + extended_len;
c906108c
SS
20873 extended_op = read_1_byte (abfd, line_ptr);
20874 line_ptr += 1;
20875 switch (extended_op)
20876 {
20877 case DW_LNE_end_sequence:
6f77053d
PA
20878 state_machine.handle_end_sequence ();
20879 end_sequence = true;
c906108c
SS
20880 break;
20881 case DW_LNE_set_address:
d9b3de22
DE
20882 {
20883 CORE_ADDR address
20884 = read_address (abfd, line_ptr, cu, &bytes_read);
d9b3de22 20885 line_ptr += bytes_read;
6f77053d
PA
20886
20887 state_machine.check_line_address (cu, line_ptr,
20888 lowpc, address);
20889 state_machine.handle_set_address (baseaddr, address);
d9b3de22 20890 }
c906108c
SS
20891 break;
20892 case DW_LNE_define_file:
debd256d 20893 {
d521ce57 20894 const char *cur_file;
ecfb656c
PA
20895 unsigned int mod_time, length;
20896 dir_index dindex;
6e70227d 20897
3e43a32a
MS
20898 cur_file = read_direct_string (abfd, line_ptr,
20899 &bytes_read);
debd256d 20900 line_ptr += bytes_read;
ecfb656c 20901 dindex = (dir_index)
debd256d
JB
20902 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20903 line_ptr += bytes_read;
20904 mod_time =
20905 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20906 line_ptr += bytes_read;
20907 length =
20908 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20909 line_ptr += bytes_read;
ecfb656c 20910 lh->add_file_name (cur_file, dindex, mod_time, length);
debd256d 20911 }
c906108c 20912 break;
d0c6ba3d 20913 case DW_LNE_set_discriminator:
6f77053d
PA
20914 {
20915 /* The discriminator is not interesting to the
20916 debugger; just ignore it. We still need to
20917 check its value though:
20918 if there are consecutive entries for the same
20919 (non-prologue) line we want to coalesce them.
20920 PR 17276. */
20921 unsigned int discr
20922 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20923 line_ptr += bytes_read;
20924
20925 state_machine.handle_set_discriminator (discr);
20926 }
d0c6ba3d 20927 break;
c906108c 20928 default:
4d3c2250 20929 complaint (&symfile_complaints,
e2e0b3e5 20930 _("mangled .debug_line section"));
debd256d 20931 return;
c906108c 20932 }
a8c50c1f
DJ
20933 /* Make sure that we parsed the extended op correctly. If e.g.
20934 we expected a different address size than the producer used,
20935 we may have read the wrong number of bytes. */
20936 if (line_ptr != extended_end)
20937 {
20938 complaint (&symfile_complaints,
20939 _("mangled .debug_line section"));
20940 return;
20941 }
c906108c
SS
20942 break;
20943 case DW_LNS_copy:
6f77053d 20944 state_machine.handle_copy ();
c906108c
SS
20945 break;
20946 case DW_LNS_advance_pc:
2dc7f7b3
TT
20947 {
20948 CORE_ADDR adjust
20949 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
2dc7f7b3 20950 line_ptr += bytes_read;
6f77053d
PA
20951
20952 state_machine.handle_advance_pc (adjust);
2dc7f7b3 20953 }
c906108c
SS
20954 break;
20955 case DW_LNS_advance_line:
a05a36a5
DE
20956 {
20957 int line_delta
20958 = read_signed_leb128 (abfd, line_ptr, &bytes_read);
a05a36a5 20959 line_ptr += bytes_read;
6f77053d
PA
20960
20961 state_machine.handle_advance_line (line_delta);
a05a36a5 20962 }
c906108c
SS
20963 break;
20964 case DW_LNS_set_file:
d9b3de22 20965 {
6f77053d 20966 file_name_index file
ecfb656c
PA
20967 = (file_name_index) read_unsigned_leb128 (abfd, line_ptr,
20968 &bytes_read);
d9b3de22 20969 line_ptr += bytes_read;
8c43009f 20970
6f77053d 20971 state_machine.handle_set_file (file);
d9b3de22 20972 }
c906108c
SS
20973 break;
20974 case DW_LNS_set_column:
0ad93d4f 20975 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
c906108c
SS
20976 line_ptr += bytes_read;
20977 break;
20978 case DW_LNS_negate_stmt:
6f77053d 20979 state_machine.handle_negate_stmt ();
c906108c
SS
20980 break;
20981 case DW_LNS_set_basic_block:
c906108c 20982 break;
c2c6d25f
JM
20983 /* Add to the address register of the state machine the
20984 address increment value corresponding to special opcode
a738430d
MK
20985 255. I.e., this value is scaled by the minimum
20986 instruction length since special opcode 255 would have
b021a221 20987 scaled the increment. */
c906108c 20988 case DW_LNS_const_add_pc:
6f77053d 20989 state_machine.handle_const_add_pc ();
c906108c
SS
20990 break;
20991 case DW_LNS_fixed_advance_pc:
3e29f34a 20992 {
6f77053d 20993 CORE_ADDR addr_adj = read_2_bytes (abfd, line_ptr);
3e29f34a 20994 line_ptr += 2;
6f77053d
PA
20995
20996 state_machine.handle_fixed_advance_pc (addr_adj);
3e29f34a 20997 }
c906108c 20998 break;
9aa1fe7e 20999 default:
a738430d
MK
21000 {
21001 /* Unknown standard opcode, ignore it. */
9aa1fe7e 21002 int i;
a738430d 21003
debd256d 21004 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
9aa1fe7e
GK
21005 {
21006 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21007 line_ptr += bytes_read;
21008 }
21009 }
c906108c
SS
21010 }
21011 }
d9b3de22
DE
21012
21013 if (!end_sequence)
21014 dwarf2_debug_line_missing_end_sequence_complaint ();
21015
21016 /* We got a DW_LNE_end_sequence (or we ran off the end of the buffer,
21017 in which case we still finish recording the last line). */
6f77053d 21018 state_machine.record_line (true);
c906108c 21019 }
f3f5162e
DE
21020}
21021
21022/* Decode the Line Number Program (LNP) for the given line_header
21023 structure and CU. The actual information extracted and the type
21024 of structures created from the LNP depends on the value of PST.
21025
21026 1. If PST is NULL, then this procedure uses the data from the program
21027 to create all necessary symbol tables, and their linetables.
21028
21029 2. If PST is not NULL, this procedure reads the program to determine
21030 the list of files included by the unit represented by PST, and
21031 builds all the associated partial symbol tables.
21032
21033 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
21034 It is used for relative paths in the line table.
21035 NOTE: When processing partial symtabs (pst != NULL),
21036 comp_dir == pst->dirname.
21037
21038 NOTE: It is important that psymtabs have the same file name (via strcmp)
21039 as the corresponding symtab. Since COMP_DIR is not used in the name of the
21040 symtab we don't use it in the name of the psymtabs we create.
21041 E.g. expand_line_sal requires this when finding psymtabs to expand.
c3b7b696
YQ
21042 A good testcase for this is mb-inline.exp.
21043
527f3840
JK
21044 LOWPC is the lowest address in CU (or 0 if not known).
21045
21046 Boolean DECODE_MAPPING specifies we need to fully decode .debug_line
21047 for its PC<->lines mapping information. Otherwise only the filename
21048 table is read in. */
f3f5162e
DE
21049
21050static void
21051dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
c3b7b696 21052 struct dwarf2_cu *cu, struct partial_symtab *pst,
527f3840 21053 CORE_ADDR lowpc, int decode_mapping)
f3f5162e 21054{
518817b3 21055 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
f3f5162e 21056 const int decode_for_pst_p = (pst != NULL);
f3f5162e 21057
527f3840
JK
21058 if (decode_mapping)
21059 dwarf_decode_lines_1 (lh, cu, decode_for_pst_p, lowpc);
aaa75496
JB
21060
21061 if (decode_for_pst_p)
21062 {
21063 int file_index;
21064
21065 /* Now that we're done scanning the Line Header Program, we can
21066 create the psymtab of each included file. */
fff8551c 21067 for (file_index = 0; file_index < lh->file_names.size (); file_index++)
aaa75496
JB
21068 if (lh->file_names[file_index].included_p == 1)
21069 {
c89b44cd 21070 gdb::unique_xmalloc_ptr<char> name_holder;
d521ce57 21071 const char *include_name =
c89b44cd
TT
21072 psymtab_include_file_name (lh, file_index, pst, comp_dir,
21073 &name_holder);
c6da4cef 21074 if (include_name != NULL)
aaa75496
JB
21075 dwarf2_create_include_psymtab (include_name, pst, objfile);
21076 }
21077 }
cb1df416
DJ
21078 else
21079 {
21080 /* Make sure a symtab is created for every file, even files
21081 which contain only variables (i.e. no code with associated
21082 line numbers). */
43f3e411 21083 struct compunit_symtab *cust = buildsym_compunit_symtab ();
cb1df416 21084 int i;
cb1df416 21085
fff8551c 21086 for (i = 0; i < lh->file_names.size (); i++)
cb1df416 21087 {
8c43009f 21088 file_entry &fe = lh->file_names[i];
9a619af0 21089
8c43009f 21090 dwarf2_start_subfile (fe.name, fe.include_dir (lh));
cb1df416 21091
cb1df416 21092 if (current_subfile->symtab == NULL)
43f3e411
DE
21093 {
21094 current_subfile->symtab
21095 = allocate_symtab (cust, current_subfile->name);
21096 }
8c43009f 21097 fe.symtab = current_subfile->symtab;
cb1df416
DJ
21098 }
21099 }
c906108c
SS
21100}
21101
21102/* Start a subfile for DWARF. FILENAME is the name of the file and
21103 DIRNAME the name of the source directory which contains FILENAME
4d663531 21104 or NULL if not known.
c906108c
SS
21105 This routine tries to keep line numbers from identical absolute and
21106 relative file names in a common subfile.
21107
21108 Using the `list' example from the GDB testsuite, which resides in
21109 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
21110 of /srcdir/list0.c yields the following debugging information for list0.c:
21111
c5aa993b 21112 DW_AT_name: /srcdir/list0.c
4d663531 21113 DW_AT_comp_dir: /compdir
357e46e7 21114 files.files[0].name: list0.h
c5aa993b 21115 files.files[0].dir: /srcdir
357e46e7 21116 files.files[1].name: list0.c
c5aa993b 21117 files.files[1].dir: /srcdir
c906108c
SS
21118
21119 The line number information for list0.c has to end up in a single
4f1520fb
FR
21120 subfile, so that `break /srcdir/list0.c:1' works as expected.
21121 start_subfile will ensure that this happens provided that we pass the
21122 concatenation of files.files[1].dir and files.files[1].name as the
21123 subfile's name. */
c906108c
SS
21124
21125static void
4d663531 21126dwarf2_start_subfile (const char *filename, const char *dirname)
c906108c 21127{
d521ce57 21128 char *copy = NULL;
4f1520fb 21129
4d663531 21130 /* In order not to lose the line information directory,
4f1520fb
FR
21131 we concatenate it to the filename when it makes sense.
21132 Note that the Dwarf3 standard says (speaking of filenames in line
21133 information): ``The directory index is ignored for file names
21134 that represent full path names''. Thus ignoring dirname in the
21135 `else' branch below isn't an issue. */
c906108c 21136
d5166ae1 21137 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
d521ce57
TT
21138 {
21139 copy = concat (dirname, SLASH_STRING, filename, (char *)NULL);
21140 filename = copy;
21141 }
c906108c 21142
4d663531 21143 start_subfile (filename);
4f1520fb 21144
d521ce57
TT
21145 if (copy != NULL)
21146 xfree (copy);
c906108c
SS
21147}
21148
f4dc4d17
DE
21149/* Start a symtab for DWARF.
21150 NAME, COMP_DIR, LOW_PC are passed to start_symtab. */
21151
43f3e411 21152static struct compunit_symtab *
f4dc4d17 21153dwarf2_start_symtab (struct dwarf2_cu *cu,
15d034d0 21154 const char *name, const char *comp_dir, CORE_ADDR low_pc)
f4dc4d17 21155{
43f3e411 21156 struct compunit_symtab *cust
518817b3
SM
21157 = start_symtab (cu->per_cu->dwarf2_per_objfile->objfile, name, comp_dir,
21158 low_pc, cu->language);
43f3e411 21159
f4dc4d17
DE
21160 record_debugformat ("DWARF 2");
21161 record_producer (cu->producer);
21162
21163 /* We assume that we're processing GCC output. */
21164 processing_gcc_compilation = 2;
21165
4d4ec4e5 21166 cu->processing_has_namespace_info = 0;
43f3e411
DE
21167
21168 return cust;
f4dc4d17
DE
21169}
21170
4c2df51b
DJ
21171static void
21172var_decode_location (struct attribute *attr, struct symbol *sym,
e7c27a73 21173 struct dwarf2_cu *cu)
4c2df51b 21174{
518817b3 21175 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
e7c27a73
DJ
21176 struct comp_unit_head *cu_header = &cu->header;
21177
4c2df51b
DJ
21178 /* NOTE drow/2003-01-30: There used to be a comment and some special
21179 code here to turn a symbol with DW_AT_external and a
21180 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
21181 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
21182 with some versions of binutils) where shared libraries could have
21183 relocations against symbols in their debug information - the
21184 minimal symbol would have the right address, but the debug info
21185 would not. It's no longer necessary, because we will explicitly
21186 apply relocations when we read in the debug information now. */
21187
21188 /* A DW_AT_location attribute with no contents indicates that a
21189 variable has been optimized away. */
21190 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
21191 {
f1e6e072 21192 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
4c2df51b
DJ
21193 return;
21194 }
21195
21196 /* Handle one degenerate form of location expression specially, to
21197 preserve GDB's previous behavior when section offsets are
3019eac3
DE
21198 specified. If this is just a DW_OP_addr or DW_OP_GNU_addr_index
21199 then mark this symbol as LOC_STATIC. */
4c2df51b
DJ
21200
21201 if (attr_form_is_block (attr)
3019eac3
DE
21202 && ((DW_BLOCK (attr)->data[0] == DW_OP_addr
21203 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size)
21204 || (DW_BLOCK (attr)->data[0] == DW_OP_GNU_addr_index
21205 && (DW_BLOCK (attr)->size
21206 == 1 + leb128_size (&DW_BLOCK (attr)->data[1])))))
4c2df51b 21207 {
891d2f0b 21208 unsigned int dummy;
4c2df51b 21209
3019eac3
DE
21210 if (DW_BLOCK (attr)->data[0] == DW_OP_addr)
21211 SYMBOL_VALUE_ADDRESS (sym) =
21212 read_address (objfile->obfd, DW_BLOCK (attr)->data + 1, cu, &dummy);
21213 else
21214 SYMBOL_VALUE_ADDRESS (sym) =
21215 read_addr_index_from_leb128 (cu, DW_BLOCK (attr)->data + 1, &dummy);
f1e6e072 21216 SYMBOL_ACLASS_INDEX (sym) = LOC_STATIC;
4c2df51b
DJ
21217 fixup_symbol_section (sym, objfile);
21218 SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (objfile->section_offsets,
21219 SYMBOL_SECTION (sym));
4c2df51b
DJ
21220 return;
21221 }
21222
21223 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
21224 expression evaluator, and use LOC_COMPUTED only when necessary
21225 (i.e. when the value of a register or memory location is
21226 referenced, or a thread-local block, etc.). Then again, it might
21227 not be worthwhile. I'm assuming that it isn't unless performance
21228 or memory numbers show me otherwise. */
21229
f1e6e072 21230 dwarf2_symbol_mark_computed (attr, sym, cu, 0);
8be455d7 21231
f1e6e072 21232 if (SYMBOL_COMPUTED_OPS (sym)->location_has_loclist)
8be455d7 21233 cu->has_loclist = 1;
4c2df51b
DJ
21234}
21235
c906108c
SS
21236/* Given a pointer to a DWARF information entry, figure out if we need
21237 to make a symbol table entry for it, and if so, create a new entry
21238 and return a pointer to it.
21239 If TYPE is NULL, determine symbol type from the die, otherwise
34eaf542
TT
21240 used the passed type.
21241 If SPACE is not NULL, use it to hold the new symbol. If it is
21242 NULL, allocate a new symbol on the objfile's obstack. */
c906108c
SS
21243
21244static struct symbol *
5e2db402
TT
21245new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
21246 struct symbol *space)
c906108c 21247{
518817b3
SM
21248 struct dwarf2_per_objfile *dwarf2_per_objfile
21249 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 21250 struct objfile *objfile = dwarf2_per_objfile->objfile;
3e29f34a 21251 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c 21252 struct symbol *sym = NULL;
15d034d0 21253 const char *name;
c906108c
SS
21254 struct attribute *attr = NULL;
21255 struct attribute *attr2 = NULL;
e142c38c 21256 CORE_ADDR baseaddr;
e37fd15a
SW
21257 struct pending **list_to_add = NULL;
21258
edb3359d 21259 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
e142c38c
DJ
21260
21261 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 21262
94af9270 21263 name = dwarf2_name (die, cu);
c906108c
SS
21264 if (name)
21265 {
94af9270 21266 const char *linkagename;
34eaf542 21267 int suppress_add = 0;
94af9270 21268
34eaf542
TT
21269 if (space)
21270 sym = space;
21271 else
e623cf5d 21272 sym = allocate_symbol (objfile);
c906108c 21273 OBJSTAT (objfile, n_syms++);
2de7ced7
DJ
21274
21275 /* Cache this symbol's name and the name's demangled form (if any). */
f85f34ed 21276 SYMBOL_SET_LANGUAGE (sym, cu->language, &objfile->objfile_obstack);
94af9270
KS
21277 linkagename = dwarf2_physname (name, die, cu);
21278 SYMBOL_SET_NAMES (sym, linkagename, strlen (linkagename), 0, objfile);
c906108c 21279
f55ee35c
JK
21280 /* Fortran does not have mangling standard and the mangling does differ
21281 between gfortran, iFort etc. */
21282 if (cu->language == language_fortran
b250c185 21283 && symbol_get_demangled_name (&(sym->ginfo)) == NULL)
29df156d 21284 symbol_set_demangled_name (&(sym->ginfo),
cfc594ee 21285 dwarf2_full_name (name, die, cu),
29df156d 21286 NULL);
f55ee35c 21287
c906108c 21288 /* Default assumptions.
c5aa993b 21289 Use the passed type or decode it from the die. */
176620f1 21290 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
f1e6e072 21291 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
c906108c
SS
21292 if (type != NULL)
21293 SYMBOL_TYPE (sym) = type;
21294 else
e7c27a73 21295 SYMBOL_TYPE (sym) = die_type (die, cu);
edb3359d
DJ
21296 attr = dwarf2_attr (die,
21297 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
21298 cu);
c906108c
SS
21299 if (attr)
21300 {
21301 SYMBOL_LINE (sym) = DW_UNSND (attr);
21302 }
cb1df416 21303
edb3359d
DJ
21304 attr = dwarf2_attr (die,
21305 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
21306 cu);
cb1df416
DJ
21307 if (attr)
21308 {
ecfb656c 21309 file_name_index file_index = (file_name_index) DW_UNSND (attr);
8c43009f 21310 struct file_entry *fe;
9a619af0 21311
ecfb656c
PA
21312 if (cu->line_header != NULL)
21313 fe = cu->line_header->file_name_at (file_index);
8c43009f
PA
21314 else
21315 fe = NULL;
21316
21317 if (fe == NULL)
cb1df416
DJ
21318 complaint (&symfile_complaints,
21319 _("file index out of range"));
8c43009f
PA
21320 else
21321 symbol_set_symtab (sym, fe->symtab);
cb1df416
DJ
21322 }
21323
c906108c
SS
21324 switch (die->tag)
21325 {
21326 case DW_TAG_label:
e142c38c 21327 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
c906108c 21328 if (attr)
3e29f34a
MR
21329 {
21330 CORE_ADDR addr;
21331
21332 addr = attr_value_as_address (attr);
21333 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr + baseaddr);
21334 SYMBOL_VALUE_ADDRESS (sym) = addr;
21335 }
0f5238ed
TT
21336 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
21337 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
f1e6e072 21338 SYMBOL_ACLASS_INDEX (sym) = LOC_LABEL;
0f5238ed 21339 add_symbol_to_list (sym, cu->list_in_scope);
c906108c
SS
21340 break;
21341 case DW_TAG_subprogram:
21342 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
21343 finish_block. */
f1e6e072 21344 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
e142c38c 21345 attr2 = dwarf2_attr (die, DW_AT_external, cu);
2cfa0c8d
JB
21346 if ((attr2 && (DW_UNSND (attr2) != 0))
21347 || cu->language == language_ada)
c906108c 21348 {
2cfa0c8d
JB
21349 /* Subprograms marked external are stored as a global symbol.
21350 Ada subprograms, whether marked external or not, are always
21351 stored as a global symbol, because we want to be able to
21352 access them globally. For instance, we want to be able
21353 to break on a nested subprogram without having to
21354 specify the context. */
e37fd15a 21355 list_to_add = &global_symbols;
c906108c
SS
21356 }
21357 else
21358 {
e37fd15a 21359 list_to_add = cu->list_in_scope;
c906108c
SS
21360 }
21361 break;
edb3359d
DJ
21362 case DW_TAG_inlined_subroutine:
21363 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
21364 finish_block. */
f1e6e072 21365 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
edb3359d 21366 SYMBOL_INLINED (sym) = 1;
481860b3 21367 list_to_add = cu->list_in_scope;
edb3359d 21368 break;
34eaf542
TT
21369 case DW_TAG_template_value_param:
21370 suppress_add = 1;
21371 /* Fall through. */
72929c62 21372 case DW_TAG_constant:
c906108c 21373 case DW_TAG_variable:
254e6b9e 21374 case DW_TAG_member:
0963b4bd
MS
21375 /* Compilation with minimal debug info may result in
21376 variables with missing type entries. Change the
21377 misleading `void' type to something sensible. */
c906108c 21378 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
46a4882b 21379 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_int;
64c50499 21380
e142c38c 21381 attr = dwarf2_attr (die, DW_AT_const_value, cu);
254e6b9e
DE
21382 /* In the case of DW_TAG_member, we should only be called for
21383 static const members. */
21384 if (die->tag == DW_TAG_member)
21385 {
3863f96c
DE
21386 /* dwarf2_add_field uses die_is_declaration,
21387 so we do the same. */
254e6b9e
DE
21388 gdb_assert (die_is_declaration (die, cu));
21389 gdb_assert (attr);
21390 }
c906108c
SS
21391 if (attr)
21392 {
e7c27a73 21393 dwarf2_const_value (attr, sym, cu);
e142c38c 21394 attr2 = dwarf2_attr (die, DW_AT_external, cu);
e37fd15a 21395 if (!suppress_add)
34eaf542
TT
21396 {
21397 if (attr2 && (DW_UNSND (attr2) != 0))
e37fd15a 21398 list_to_add = &global_symbols;
34eaf542 21399 else
e37fd15a 21400 list_to_add = cu->list_in_scope;
34eaf542 21401 }
c906108c
SS
21402 break;
21403 }
e142c38c 21404 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
21405 if (attr)
21406 {
e7c27a73 21407 var_decode_location (attr, sym, cu);
e142c38c 21408 attr2 = dwarf2_attr (die, DW_AT_external, cu);
4357ac6c
TT
21409
21410 /* Fortran explicitly imports any global symbols to the local
21411 scope by DW_TAG_common_block. */
21412 if (cu->language == language_fortran && die->parent
21413 && die->parent->tag == DW_TAG_common_block)
21414 attr2 = NULL;
21415
caac4577
JG
21416 if (SYMBOL_CLASS (sym) == LOC_STATIC
21417 && SYMBOL_VALUE_ADDRESS (sym) == 0
21418 && !dwarf2_per_objfile->has_section_at_zero)
21419 {
21420 /* When a static variable is eliminated by the linker,
21421 the corresponding debug information is not stripped
21422 out, but the variable address is set to null;
21423 do not add such variables into symbol table. */
21424 }
21425 else if (attr2 && (DW_UNSND (attr2) != 0))
1c809c68 21426 {
f55ee35c
JK
21427 /* Workaround gfortran PR debug/40040 - it uses
21428 DW_AT_location for variables in -fPIC libraries which may
21429 get overriden by other libraries/executable and get
21430 a different address. Resolve it by the minimal symbol
21431 which may come from inferior's executable using copy
21432 relocation. Make this workaround only for gfortran as for
21433 other compilers GDB cannot guess the minimal symbol
21434 Fortran mangling kind. */
21435 if (cu->language == language_fortran && die->parent
21436 && die->parent->tag == DW_TAG_module
21437 && cu->producer
28586665 21438 && startswith (cu->producer, "GNU Fortran"))
f1e6e072 21439 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
f55ee35c 21440
1c809c68
TT
21441 /* A variable with DW_AT_external is never static,
21442 but it may be block-scoped. */
21443 list_to_add = (cu->list_in_scope == &file_symbols
21444 ? &global_symbols : cu->list_in_scope);
1c809c68 21445 }
c906108c 21446 else
e37fd15a 21447 list_to_add = cu->list_in_scope;
c906108c
SS
21448 }
21449 else
21450 {
21451 /* We do not know the address of this symbol.
c5aa993b
JM
21452 If it is an external symbol and we have type information
21453 for it, enter the symbol as a LOC_UNRESOLVED symbol.
21454 The address of the variable will then be determined from
21455 the minimal symbol table whenever the variable is
21456 referenced. */
e142c38c 21457 attr2 = dwarf2_attr (die, DW_AT_external, cu);
0971de02
TT
21458
21459 /* Fortran explicitly imports any global symbols to the local
21460 scope by DW_TAG_common_block. */
21461 if (cu->language == language_fortran && die->parent
21462 && die->parent->tag == DW_TAG_common_block)
21463 {
21464 /* SYMBOL_CLASS doesn't matter here because
21465 read_common_block is going to reset it. */
21466 if (!suppress_add)
21467 list_to_add = cu->list_in_scope;
21468 }
21469 else if (attr2 && (DW_UNSND (attr2) != 0)
21470 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
c906108c 21471 {
0fe7935b
DJ
21472 /* A variable with DW_AT_external is never static, but it
21473 may be block-scoped. */
21474 list_to_add = (cu->list_in_scope == &file_symbols
21475 ? &global_symbols : cu->list_in_scope);
21476
f1e6e072 21477 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
c906108c 21478 }
442ddf59
JK
21479 else if (!die_is_declaration (die, cu))
21480 {
21481 /* Use the default LOC_OPTIMIZED_OUT class. */
21482 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
e37fd15a
SW
21483 if (!suppress_add)
21484 list_to_add = cu->list_in_scope;
442ddf59 21485 }
c906108c
SS
21486 }
21487 break;
21488 case DW_TAG_formal_parameter:
edb3359d
DJ
21489 /* If we are inside a function, mark this as an argument. If
21490 not, we might be looking at an argument to an inlined function
21491 when we do not have enough information to show inlined frames;
21492 pretend it's a local variable in that case so that the user can
21493 still see it. */
21494 if (context_stack_depth > 0
21495 && context_stack[context_stack_depth - 1].name != NULL)
21496 SYMBOL_IS_ARGUMENT (sym) = 1;
e142c38c 21497 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
21498 if (attr)
21499 {
e7c27a73 21500 var_decode_location (attr, sym, cu);
c906108c 21501 }
e142c38c 21502 attr = dwarf2_attr (die, DW_AT_const_value, cu);
c906108c
SS
21503 if (attr)
21504 {
e7c27a73 21505 dwarf2_const_value (attr, sym, cu);
c906108c 21506 }
f346a30d 21507
e37fd15a 21508 list_to_add = cu->list_in_scope;
c906108c
SS
21509 break;
21510 case DW_TAG_unspecified_parameters:
21511 /* From varargs functions; gdb doesn't seem to have any
21512 interest in this information, so just ignore it for now.
21513 (FIXME?) */
21514 break;
34eaf542
TT
21515 case DW_TAG_template_type_param:
21516 suppress_add = 1;
21517 /* Fall through. */
c906108c 21518 case DW_TAG_class_type:
680b30c7 21519 case DW_TAG_interface_type:
c906108c
SS
21520 case DW_TAG_structure_type:
21521 case DW_TAG_union_type:
72019c9c 21522 case DW_TAG_set_type:
c906108c 21523 case DW_TAG_enumeration_type:
f1e6e072 21524 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
176620f1 21525 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
c906108c 21526
63d06c5c 21527 {
9c37b5ae 21528 /* NOTE: carlton/2003-11-10: C++ class symbols shouldn't
63d06c5c
DC
21529 really ever be static objects: otherwise, if you try
21530 to, say, break of a class's method and you're in a file
21531 which doesn't mention that class, it won't work unless
21532 the check for all static symbols in lookup_symbol_aux
21533 saves you. See the OtherFileClass tests in
21534 gdb.c++/namespace.exp. */
21535
e37fd15a 21536 if (!suppress_add)
34eaf542 21537 {
34eaf542 21538 list_to_add = (cu->list_in_scope == &file_symbols
9c37b5ae 21539 && cu->language == language_cplus
34eaf542 21540 ? &global_symbols : cu->list_in_scope);
63d06c5c 21541
64382290 21542 /* The semantics of C++ state that "struct foo {
9c37b5ae 21543 ... }" also defines a typedef for "foo". */
64382290 21544 if (cu->language == language_cplus
45280282 21545 || cu->language == language_ada
c44af4eb
TT
21546 || cu->language == language_d
21547 || cu->language == language_rust)
64382290
TT
21548 {
21549 /* The symbol's name is already allocated along
21550 with this objfile, so we don't need to
21551 duplicate it for the type. */
21552 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
21553 TYPE_NAME (SYMBOL_TYPE (sym)) = SYMBOL_SEARCH_NAME (sym);
21554 }
63d06c5c
DC
21555 }
21556 }
c906108c
SS
21557 break;
21558 case DW_TAG_typedef:
f1e6e072 21559 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
63d06c5c 21560 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 21561 list_to_add = cu->list_in_scope;
63d06c5c 21562 break;
c906108c 21563 case DW_TAG_base_type:
a02abb62 21564 case DW_TAG_subrange_type:
f1e6e072 21565 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
176620f1 21566 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 21567 list_to_add = cu->list_in_scope;
c906108c
SS
21568 break;
21569 case DW_TAG_enumerator:
e142c38c 21570 attr = dwarf2_attr (die, DW_AT_const_value, cu);
c906108c
SS
21571 if (attr)
21572 {
e7c27a73 21573 dwarf2_const_value (attr, sym, cu);
c906108c 21574 }
63d06c5c
DC
21575 {
21576 /* NOTE: carlton/2003-11-10: See comment above in the
21577 DW_TAG_class_type, etc. block. */
21578
e142c38c 21579 list_to_add = (cu->list_in_scope == &file_symbols
9c37b5ae 21580 && cu->language == language_cplus
e142c38c 21581 ? &global_symbols : cu->list_in_scope);
63d06c5c 21582 }
c906108c 21583 break;
74921315 21584 case DW_TAG_imported_declaration:
5c4e30ca 21585 case DW_TAG_namespace:
f1e6e072 21586 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
e37fd15a 21587 list_to_add = &global_symbols;
5c4e30ca 21588 break;
530e8392
KB
21589 case DW_TAG_module:
21590 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21591 SYMBOL_DOMAIN (sym) = MODULE_DOMAIN;
21592 list_to_add = &global_symbols;
21593 break;
4357ac6c 21594 case DW_TAG_common_block:
f1e6e072 21595 SYMBOL_ACLASS_INDEX (sym) = LOC_COMMON_BLOCK;
4357ac6c
TT
21596 SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
21597 add_symbol_to_list (sym, cu->list_in_scope);
21598 break;
c906108c
SS
21599 default:
21600 /* Not a tag we recognize. Hopefully we aren't processing
21601 trash data, but since we must specifically ignore things
21602 we don't recognize, there is nothing else we should do at
0963b4bd 21603 this point. */
e2e0b3e5 21604 complaint (&symfile_complaints, _("unsupported tag: '%s'"),
4d3c2250 21605 dwarf_tag_name (die->tag));
c906108c
SS
21606 break;
21607 }
df8a16a1 21608
e37fd15a
SW
21609 if (suppress_add)
21610 {
21611 sym->hash_next = objfile->template_symbols;
21612 objfile->template_symbols = sym;
21613 list_to_add = NULL;
21614 }
21615
21616 if (list_to_add != NULL)
21617 add_symbol_to_list (sym, list_to_add);
21618
df8a16a1
DJ
21619 /* For the benefit of old versions of GCC, check for anonymous
21620 namespaces based on the demangled name. */
4d4ec4e5 21621 if (!cu->processing_has_namespace_info
94af9270 21622 && cu->language == language_cplus)
a10964d1 21623 cp_scan_for_anonymous_namespaces (sym, objfile);
c906108c
SS
21624 }
21625 return (sym);
21626}
21627
98bfdba5
PA
21628/* Given an attr with a DW_FORM_dataN value in host byte order,
21629 zero-extend it as appropriate for the symbol's type. The DWARF
21630 standard (v4) is not entirely clear about the meaning of using
21631 DW_FORM_dataN for a constant with a signed type, where the type is
21632 wider than the data. The conclusion of a discussion on the DWARF
21633 list was that this is unspecified. We choose to always zero-extend
21634 because that is the interpretation long in use by GCC. */
c906108c 21635
98bfdba5 21636static gdb_byte *
ff39bb5e 21637dwarf2_const_value_data (const struct attribute *attr, struct obstack *obstack,
12df843f 21638 struct dwarf2_cu *cu, LONGEST *value, int bits)
c906108c 21639{
518817b3 21640 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
e17a4113
UW
21641 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
21642 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
98bfdba5
PA
21643 LONGEST l = DW_UNSND (attr);
21644
21645 if (bits < sizeof (*value) * 8)
21646 {
21647 l &= ((LONGEST) 1 << bits) - 1;
21648 *value = l;
21649 }
21650 else if (bits == sizeof (*value) * 8)
21651 *value = l;
21652 else
21653 {
224c3ddb 21654 gdb_byte *bytes = (gdb_byte *) obstack_alloc (obstack, bits / 8);
98bfdba5
PA
21655 store_unsigned_integer (bytes, bits / 8, byte_order, l);
21656 return bytes;
21657 }
21658
21659 return NULL;
21660}
21661
21662/* Read a constant value from an attribute. Either set *VALUE, or if
21663 the value does not fit in *VALUE, set *BYTES - either already
21664 allocated on the objfile obstack, or newly allocated on OBSTACK,
21665 or, set *BATON, if we translated the constant to a location
21666 expression. */
21667
21668static void
ff39bb5e 21669dwarf2_const_value_attr (const struct attribute *attr, struct type *type,
98bfdba5
PA
21670 const char *name, struct obstack *obstack,
21671 struct dwarf2_cu *cu,
d521ce57 21672 LONGEST *value, const gdb_byte **bytes,
98bfdba5
PA
21673 struct dwarf2_locexpr_baton **baton)
21674{
518817b3 21675 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
98bfdba5 21676 struct comp_unit_head *cu_header = &cu->header;
c906108c 21677 struct dwarf_block *blk;
98bfdba5
PA
21678 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
21679 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
21680
21681 *value = 0;
21682 *bytes = NULL;
21683 *baton = NULL;
c906108c
SS
21684
21685 switch (attr->form)
21686 {
21687 case DW_FORM_addr:
3019eac3 21688 case DW_FORM_GNU_addr_index:
ac56253d 21689 {
ac56253d
TT
21690 gdb_byte *data;
21691
98bfdba5
PA
21692 if (TYPE_LENGTH (type) != cu_header->addr_size)
21693 dwarf2_const_value_length_mismatch_complaint (name,
ac56253d 21694 cu_header->addr_size,
98bfdba5 21695 TYPE_LENGTH (type));
ac56253d
TT
21696 /* Symbols of this form are reasonably rare, so we just
21697 piggyback on the existing location code rather than writing
21698 a new implementation of symbol_computed_ops. */
8d749320 21699 *baton = XOBNEW (obstack, struct dwarf2_locexpr_baton);
98bfdba5
PA
21700 (*baton)->per_cu = cu->per_cu;
21701 gdb_assert ((*baton)->per_cu);
ac56253d 21702
98bfdba5 21703 (*baton)->size = 2 + cu_header->addr_size;
224c3ddb 21704 data = (gdb_byte *) obstack_alloc (obstack, (*baton)->size);
98bfdba5 21705 (*baton)->data = data;
ac56253d
TT
21706
21707 data[0] = DW_OP_addr;
21708 store_unsigned_integer (&data[1], cu_header->addr_size,
21709 byte_order, DW_ADDR (attr));
21710 data[cu_header->addr_size + 1] = DW_OP_stack_value;
ac56253d 21711 }
c906108c 21712 break;
4ac36638 21713 case DW_FORM_string:
93b5768b 21714 case DW_FORM_strp:
3019eac3 21715 case DW_FORM_GNU_str_index:
36586728 21716 case DW_FORM_GNU_strp_alt:
98bfdba5
PA
21717 /* DW_STRING is already allocated on the objfile obstack, point
21718 directly to it. */
d521ce57 21719 *bytes = (const gdb_byte *) DW_STRING (attr);
93b5768b 21720 break;
c906108c
SS
21721 case DW_FORM_block1:
21722 case DW_FORM_block2:
21723 case DW_FORM_block4:
21724 case DW_FORM_block:
2dc7f7b3 21725 case DW_FORM_exprloc:
0224619f 21726 case DW_FORM_data16:
c906108c 21727 blk = DW_BLOCK (attr);
98bfdba5
PA
21728 if (TYPE_LENGTH (type) != blk->size)
21729 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
21730 TYPE_LENGTH (type));
21731 *bytes = blk->data;
c906108c 21732 break;
2df3850c
JM
21733
21734 /* The DW_AT_const_value attributes are supposed to carry the
21735 symbol's value "represented as it would be on the target
21736 architecture." By the time we get here, it's already been
21737 converted to host endianness, so we just need to sign- or
21738 zero-extend it as appropriate. */
21739 case DW_FORM_data1:
3aef2284 21740 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 8);
2df3850c 21741 break;
c906108c 21742 case DW_FORM_data2:
3aef2284 21743 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 16);
2df3850c 21744 break;
c906108c 21745 case DW_FORM_data4:
3aef2284 21746 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 32);
2df3850c 21747 break;
c906108c 21748 case DW_FORM_data8:
3aef2284 21749 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 64);
2df3850c
JM
21750 break;
21751
c906108c 21752 case DW_FORM_sdata:
663c44ac 21753 case DW_FORM_implicit_const:
98bfdba5 21754 *value = DW_SND (attr);
2df3850c
JM
21755 break;
21756
c906108c 21757 case DW_FORM_udata:
98bfdba5 21758 *value = DW_UNSND (attr);
c906108c 21759 break;
2df3850c 21760
c906108c 21761 default:
4d3c2250 21762 complaint (&symfile_complaints,
e2e0b3e5 21763 _("unsupported const value attribute form: '%s'"),
4d3c2250 21764 dwarf_form_name (attr->form));
98bfdba5 21765 *value = 0;
c906108c
SS
21766 break;
21767 }
21768}
21769
2df3850c 21770
98bfdba5
PA
21771/* Copy constant value from an attribute to a symbol. */
21772
2df3850c 21773static void
ff39bb5e 21774dwarf2_const_value (const struct attribute *attr, struct symbol *sym,
98bfdba5 21775 struct dwarf2_cu *cu)
2df3850c 21776{
518817b3 21777 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
12df843f 21778 LONGEST value;
d521ce57 21779 const gdb_byte *bytes;
98bfdba5 21780 struct dwarf2_locexpr_baton *baton;
2df3850c 21781
98bfdba5
PA
21782 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
21783 SYMBOL_PRINT_NAME (sym),
21784 &objfile->objfile_obstack, cu,
21785 &value, &bytes, &baton);
2df3850c 21786
98bfdba5
PA
21787 if (baton != NULL)
21788 {
98bfdba5 21789 SYMBOL_LOCATION_BATON (sym) = baton;
f1e6e072 21790 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
98bfdba5
PA
21791 }
21792 else if (bytes != NULL)
21793 {
21794 SYMBOL_VALUE_BYTES (sym) = bytes;
f1e6e072 21795 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST_BYTES;
98bfdba5
PA
21796 }
21797 else
21798 {
21799 SYMBOL_VALUE (sym) = value;
f1e6e072 21800 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST;
98bfdba5 21801 }
2df3850c
JM
21802}
21803
c906108c
SS
21804/* Return the type of the die in question using its DW_AT_type attribute. */
21805
21806static struct type *
e7c27a73 21807die_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 21808{
c906108c 21809 struct attribute *type_attr;
c906108c 21810
e142c38c 21811 type_attr = dwarf2_attr (die, DW_AT_type, cu);
c906108c
SS
21812 if (!type_attr)
21813 {
518817b3 21814 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c 21815 /* A missing DW_AT_type represents a void type. */
518817b3 21816 return objfile_type (objfile)->builtin_void;
c906108c 21817 }
348e048f 21818
673bfd45 21819 return lookup_die_type (die, type_attr, cu);
c906108c
SS
21820}
21821
b4ba55a1
JB
21822/* True iff CU's producer generates GNAT Ada auxiliary information
21823 that allows to find parallel types through that information instead
21824 of having to do expensive parallel lookups by type name. */
21825
21826static int
21827need_gnat_info (struct dwarf2_cu *cu)
21828{
de4cb04a
JB
21829 /* Assume that the Ada compiler was GNAT, which always produces
21830 the auxiliary information. */
21831 return (cu->language == language_ada);
b4ba55a1
JB
21832}
21833
b4ba55a1
JB
21834/* Return the auxiliary type of the die in question using its
21835 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
21836 attribute is not present. */
21837
21838static struct type *
21839die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
21840{
b4ba55a1 21841 struct attribute *type_attr;
b4ba55a1
JB
21842
21843 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
21844 if (!type_attr)
21845 return NULL;
21846
673bfd45 21847 return lookup_die_type (die, type_attr, cu);
b4ba55a1
JB
21848}
21849
21850/* If DIE has a descriptive_type attribute, then set the TYPE's
21851 descriptive type accordingly. */
21852
21853static void
21854set_descriptive_type (struct type *type, struct die_info *die,
21855 struct dwarf2_cu *cu)
21856{
21857 struct type *descriptive_type = die_descriptive_type (die, cu);
21858
21859 if (descriptive_type)
21860 {
21861 ALLOCATE_GNAT_AUX_TYPE (type);
21862 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
21863 }
21864}
21865
c906108c
SS
21866/* Return the containing type of the die in question using its
21867 DW_AT_containing_type attribute. */
21868
21869static struct type *
e7c27a73 21870die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 21871{
c906108c 21872 struct attribute *type_attr;
518817b3 21873 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c 21874
e142c38c 21875 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
33ac96f0
JK
21876 if (!type_attr)
21877 error (_("Dwarf Error: Problem turning containing type into gdb type "
518817b3 21878 "[in module %s]"), objfile_name (objfile));
33ac96f0 21879
673bfd45 21880 return lookup_die_type (die, type_attr, cu);
c906108c
SS
21881}
21882
ac9ec31b
DE
21883/* Return an error marker type to use for the ill formed type in DIE/CU. */
21884
21885static struct type *
21886build_error_marker_type (struct dwarf2_cu *cu, struct die_info *die)
21887{
518817b3
SM
21888 struct dwarf2_per_objfile *dwarf2_per_objfile
21889 = cu->per_cu->dwarf2_per_objfile;
ac9ec31b
DE
21890 struct objfile *objfile = dwarf2_per_objfile->objfile;
21891 char *message, *saved;
21892
9d8780f0 21893 message = xstrprintf (_("<unknown type in %s, CU %s, DIE %s>"),
4262abfb 21894 objfile_name (objfile),
9d8780f0
SM
21895 sect_offset_str (cu->header.sect_off),
21896 sect_offset_str (die->sect_off));
224c3ddb
SM
21897 saved = (char *) obstack_copy0 (&objfile->objfile_obstack,
21898 message, strlen (message));
ac9ec31b
DE
21899 xfree (message);
21900
19f392bc 21901 return init_type (objfile, TYPE_CODE_ERROR, 0, saved);
ac9ec31b
DE
21902}
21903
673bfd45 21904/* Look up the type of DIE in CU using its type attribute ATTR.
ac9ec31b
DE
21905 ATTR must be one of: DW_AT_type, DW_AT_GNAT_descriptive_type,
21906 DW_AT_containing_type.
673bfd45
DE
21907 If there is no type substitute an error marker. */
21908
c906108c 21909static struct type *
ff39bb5e 21910lookup_die_type (struct die_info *die, const struct attribute *attr,
673bfd45 21911 struct dwarf2_cu *cu)
c906108c 21912{
518817b3
SM
21913 struct dwarf2_per_objfile *dwarf2_per_objfile
21914 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 21915 struct objfile *objfile = dwarf2_per_objfile->objfile;
f792889a
DJ
21916 struct type *this_type;
21917
ac9ec31b
DE
21918 gdb_assert (attr->name == DW_AT_type
21919 || attr->name == DW_AT_GNAT_descriptive_type
21920 || attr->name == DW_AT_containing_type);
21921
673bfd45
DE
21922 /* First see if we have it cached. */
21923
36586728
TT
21924 if (attr->form == DW_FORM_GNU_ref_alt)
21925 {
21926 struct dwarf2_per_cu_data *per_cu;
9c541725 21927 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
36586728 21928
ed2dc618
SM
21929 per_cu = dwarf2_find_containing_comp_unit (sect_off, 1,
21930 dwarf2_per_objfile);
9c541725 21931 this_type = get_die_type_at_offset (sect_off, per_cu);
36586728 21932 }
7771576e 21933 else if (attr_form_is_ref (attr))
673bfd45 21934 {
9c541725 21935 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
673bfd45 21936
9c541725 21937 this_type = get_die_type_at_offset (sect_off, cu->per_cu);
673bfd45 21938 }
55f1336d 21939 else if (attr->form == DW_FORM_ref_sig8)
673bfd45 21940 {
ac9ec31b 21941 ULONGEST signature = DW_SIGNATURE (attr);
673bfd45 21942
ac9ec31b 21943 return get_signatured_type (die, signature, cu);
673bfd45
DE
21944 }
21945 else
21946 {
ac9ec31b
DE
21947 complaint (&symfile_complaints,
21948 _("Dwarf Error: Bad type attribute %s in DIE"
9d8780f0
SM
21949 " at %s [in module %s]"),
21950 dwarf_attr_name (attr->name), sect_offset_str (die->sect_off),
4262abfb 21951 objfile_name (objfile));
ac9ec31b 21952 return build_error_marker_type (cu, die);
673bfd45
DE
21953 }
21954
21955 /* If not cached we need to read it in. */
21956
21957 if (this_type == NULL)
21958 {
ac9ec31b 21959 struct die_info *type_die = NULL;
673bfd45
DE
21960 struct dwarf2_cu *type_cu = cu;
21961
7771576e 21962 if (attr_form_is_ref (attr))
ac9ec31b
DE
21963 type_die = follow_die_ref (die, attr, &type_cu);
21964 if (type_die == NULL)
21965 return build_error_marker_type (cu, die);
21966 /* If we find the type now, it's probably because the type came
3019eac3
DE
21967 from an inter-CU reference and the type's CU got expanded before
21968 ours. */
ac9ec31b 21969 this_type = read_type_die (type_die, type_cu);
673bfd45
DE
21970 }
21971
21972 /* If we still don't have a type use an error marker. */
21973
21974 if (this_type == NULL)
ac9ec31b 21975 return build_error_marker_type (cu, die);
673bfd45 21976
f792889a 21977 return this_type;
c906108c
SS
21978}
21979
673bfd45
DE
21980/* Return the type in DIE, CU.
21981 Returns NULL for invalid types.
21982
02142a6c 21983 This first does a lookup in die_type_hash,
673bfd45
DE
21984 and only reads the die in if necessary.
21985
21986 NOTE: This can be called when reading in partial or full symbols. */
21987
f792889a 21988static struct type *
e7c27a73 21989read_type_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 21990{
f792889a
DJ
21991 struct type *this_type;
21992
21993 this_type = get_die_type (die, cu);
21994 if (this_type)
21995 return this_type;
21996
673bfd45
DE
21997 return read_type_die_1 (die, cu);
21998}
21999
22000/* Read the type in DIE, CU.
22001 Returns NULL for invalid types. */
22002
22003static struct type *
22004read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
22005{
22006 struct type *this_type = NULL;
22007
c906108c
SS
22008 switch (die->tag)
22009 {
22010 case DW_TAG_class_type:
680b30c7 22011 case DW_TAG_interface_type:
c906108c
SS
22012 case DW_TAG_structure_type:
22013 case DW_TAG_union_type:
f792889a 22014 this_type = read_structure_type (die, cu);
c906108c
SS
22015 break;
22016 case DW_TAG_enumeration_type:
f792889a 22017 this_type = read_enumeration_type (die, cu);
c906108c
SS
22018 break;
22019 case DW_TAG_subprogram:
22020 case DW_TAG_subroutine_type:
edb3359d 22021 case DW_TAG_inlined_subroutine:
f792889a 22022 this_type = read_subroutine_type (die, cu);
c906108c
SS
22023 break;
22024 case DW_TAG_array_type:
f792889a 22025 this_type = read_array_type (die, cu);
c906108c 22026 break;
72019c9c 22027 case DW_TAG_set_type:
f792889a 22028 this_type = read_set_type (die, cu);
72019c9c 22029 break;
c906108c 22030 case DW_TAG_pointer_type:
f792889a 22031 this_type = read_tag_pointer_type (die, cu);
c906108c
SS
22032 break;
22033 case DW_TAG_ptr_to_member_type:
f792889a 22034 this_type = read_tag_ptr_to_member_type (die, cu);
c906108c
SS
22035 break;
22036 case DW_TAG_reference_type:
4297a3f0
AV
22037 this_type = read_tag_reference_type (die, cu, TYPE_CODE_REF);
22038 break;
22039 case DW_TAG_rvalue_reference_type:
22040 this_type = read_tag_reference_type (die, cu, TYPE_CODE_RVALUE_REF);
c906108c
SS
22041 break;
22042 case DW_TAG_const_type:
f792889a 22043 this_type = read_tag_const_type (die, cu);
c906108c
SS
22044 break;
22045 case DW_TAG_volatile_type:
f792889a 22046 this_type = read_tag_volatile_type (die, cu);
c906108c 22047 break;
06d66ee9
TT
22048 case DW_TAG_restrict_type:
22049 this_type = read_tag_restrict_type (die, cu);
22050 break;
c906108c 22051 case DW_TAG_string_type:
f792889a 22052 this_type = read_tag_string_type (die, cu);
c906108c
SS
22053 break;
22054 case DW_TAG_typedef:
f792889a 22055 this_type = read_typedef (die, cu);
c906108c 22056 break;
a02abb62 22057 case DW_TAG_subrange_type:
f792889a 22058 this_type = read_subrange_type (die, cu);
a02abb62 22059 break;
c906108c 22060 case DW_TAG_base_type:
f792889a 22061 this_type = read_base_type (die, cu);
c906108c 22062 break;
81a17f79 22063 case DW_TAG_unspecified_type:
f792889a 22064 this_type = read_unspecified_type (die, cu);
81a17f79 22065 break;
0114d602
DJ
22066 case DW_TAG_namespace:
22067 this_type = read_namespace_type (die, cu);
22068 break;
f55ee35c
JK
22069 case DW_TAG_module:
22070 this_type = read_module_type (die, cu);
22071 break;
a2c2acaf
MW
22072 case DW_TAG_atomic_type:
22073 this_type = read_tag_atomic_type (die, cu);
22074 break;
c906108c 22075 default:
3e43a32a
MS
22076 complaint (&symfile_complaints,
22077 _("unexpected tag in read_type_die: '%s'"),
4d3c2250 22078 dwarf_tag_name (die->tag));
c906108c
SS
22079 break;
22080 }
63d06c5c 22081
f792889a 22082 return this_type;
63d06c5c
DC
22083}
22084
abc72ce4
DE
22085/* See if we can figure out if the class lives in a namespace. We do
22086 this by looking for a member function; its demangled name will
22087 contain namespace info, if there is any.
22088 Return the computed name or NULL.
22089 Space for the result is allocated on the objfile's obstack.
22090 This is the full-die version of guess_partial_die_structure_name.
22091 In this case we know DIE has no useful parent. */
22092
22093static char *
22094guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
22095{
22096 struct die_info *spec_die;
22097 struct dwarf2_cu *spec_cu;
22098 struct die_info *child;
518817b3 22099 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
abc72ce4
DE
22100
22101 spec_cu = cu;
22102 spec_die = die_specification (die, &spec_cu);
22103 if (spec_die != NULL)
22104 {
22105 die = spec_die;
22106 cu = spec_cu;
22107 }
22108
22109 for (child = die->child;
22110 child != NULL;
22111 child = child->sibling)
22112 {
22113 if (child->tag == DW_TAG_subprogram)
22114 {
73b9be8b 22115 const char *linkage_name = dw2_linkage_name (child, cu);
abc72ce4 22116
7d45c7c3 22117 if (linkage_name != NULL)
abc72ce4
DE
22118 {
22119 char *actual_name
22120 = language_class_name_from_physname (cu->language_defn,
7d45c7c3 22121 linkage_name);
abc72ce4
DE
22122 char *name = NULL;
22123
22124 if (actual_name != NULL)
22125 {
15d034d0 22126 const char *die_name = dwarf2_name (die, cu);
abc72ce4
DE
22127
22128 if (die_name != NULL
22129 && strcmp (die_name, actual_name) != 0)
22130 {
22131 /* Strip off the class name from the full name.
22132 We want the prefix. */
22133 int die_name_len = strlen (die_name);
22134 int actual_name_len = strlen (actual_name);
22135
22136 /* Test for '::' as a sanity check. */
22137 if (actual_name_len > die_name_len + 2
3e43a32a
MS
22138 && actual_name[actual_name_len
22139 - die_name_len - 1] == ':')
224c3ddb 22140 name = (char *) obstack_copy0 (
e3b94546 22141 &objfile->per_bfd->storage_obstack,
224c3ddb 22142 actual_name, actual_name_len - die_name_len - 2);
abc72ce4
DE
22143 }
22144 }
22145 xfree (actual_name);
22146 return name;
22147 }
22148 }
22149 }
22150
22151 return NULL;
22152}
22153
96408a79
SA
22154/* GCC might emit a nameless typedef that has a linkage name. Determine the
22155 prefix part in such case. See
22156 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
22157
a121b7c1 22158static const char *
96408a79
SA
22159anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
22160{
22161 struct attribute *attr;
e6a959d6 22162 const char *base;
96408a79
SA
22163
22164 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
22165 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
22166 return NULL;
22167
7d45c7c3 22168 if (dwarf2_string_attr (die, DW_AT_name, cu) != NULL)
96408a79
SA
22169 return NULL;
22170
73b9be8b 22171 attr = dw2_linkage_name_attr (die, cu);
96408a79
SA
22172 if (attr == NULL || DW_STRING (attr) == NULL)
22173 return NULL;
22174
22175 /* dwarf2_name had to be already called. */
22176 gdb_assert (DW_STRING_IS_CANONICAL (attr));
22177
22178 /* Strip the base name, keep any leading namespaces/classes. */
22179 base = strrchr (DW_STRING (attr), ':');
22180 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
22181 return "";
22182
518817b3 22183 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
e3b94546 22184 return (char *) obstack_copy0 (&objfile->per_bfd->storage_obstack,
224c3ddb
SM
22185 DW_STRING (attr),
22186 &base[-1] - DW_STRING (attr));
96408a79
SA
22187}
22188
fdde2d81 22189/* Return the name of the namespace/class that DIE is defined within,
0114d602 22190 or "" if we can't tell. The caller should not xfree the result.
fdde2d81 22191
0114d602
DJ
22192 For example, if we're within the method foo() in the following
22193 code:
22194
22195 namespace N {
22196 class C {
22197 void foo () {
22198 }
22199 };
22200 }
22201
22202 then determine_prefix on foo's die will return "N::C". */
fdde2d81 22203
0d5cff50 22204static const char *
e142c38c 22205determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
63d06c5c 22206{
518817b3
SM
22207 struct dwarf2_per_objfile *dwarf2_per_objfile
22208 = cu->per_cu->dwarf2_per_objfile;
0114d602
DJ
22209 struct die_info *parent, *spec_die;
22210 struct dwarf2_cu *spec_cu;
22211 struct type *parent_type;
a121b7c1 22212 const char *retval;
63d06c5c 22213
9c37b5ae 22214 if (cu->language != language_cplus
c44af4eb
TT
22215 && cu->language != language_fortran && cu->language != language_d
22216 && cu->language != language_rust)
0114d602
DJ
22217 return "";
22218
96408a79
SA
22219 retval = anonymous_struct_prefix (die, cu);
22220 if (retval)
22221 return retval;
22222
0114d602
DJ
22223 /* We have to be careful in the presence of DW_AT_specification.
22224 For example, with GCC 3.4, given the code
22225
22226 namespace N {
22227 void foo() {
22228 // Definition of N::foo.
22229 }
22230 }
22231
22232 then we'll have a tree of DIEs like this:
22233
22234 1: DW_TAG_compile_unit
22235 2: DW_TAG_namespace // N
22236 3: DW_TAG_subprogram // declaration of N::foo
22237 4: DW_TAG_subprogram // definition of N::foo
22238 DW_AT_specification // refers to die #3
22239
22240 Thus, when processing die #4, we have to pretend that we're in
22241 the context of its DW_AT_specification, namely the contex of die
22242 #3. */
22243 spec_cu = cu;
22244 spec_die = die_specification (die, &spec_cu);
22245 if (spec_die == NULL)
22246 parent = die->parent;
22247 else
63d06c5c 22248 {
0114d602
DJ
22249 parent = spec_die->parent;
22250 cu = spec_cu;
63d06c5c 22251 }
0114d602
DJ
22252
22253 if (parent == NULL)
22254 return "";
98bfdba5
PA
22255 else if (parent->building_fullname)
22256 {
22257 const char *name;
22258 const char *parent_name;
22259
22260 /* It has been seen on RealView 2.2 built binaries,
22261 DW_TAG_template_type_param types actually _defined_ as
22262 children of the parent class:
22263
22264 enum E {};
22265 template class <class Enum> Class{};
22266 Class<enum E> class_e;
22267
22268 1: DW_TAG_class_type (Class)
22269 2: DW_TAG_enumeration_type (E)
22270 3: DW_TAG_enumerator (enum1:0)
22271 3: DW_TAG_enumerator (enum2:1)
22272 ...
22273 2: DW_TAG_template_type_param
22274 DW_AT_type DW_FORM_ref_udata (E)
22275
22276 Besides being broken debug info, it can put GDB into an
22277 infinite loop. Consider:
22278
22279 When we're building the full name for Class<E>, we'll start
22280 at Class, and go look over its template type parameters,
22281 finding E. We'll then try to build the full name of E, and
22282 reach here. We're now trying to build the full name of E,
22283 and look over the parent DIE for containing scope. In the
22284 broken case, if we followed the parent DIE of E, we'd again
22285 find Class, and once again go look at its template type
22286 arguments, etc., etc. Simply don't consider such parent die
22287 as source-level parent of this die (it can't be, the language
22288 doesn't allow it), and break the loop here. */
22289 name = dwarf2_name (die, cu);
22290 parent_name = dwarf2_name (parent, cu);
22291 complaint (&symfile_complaints,
22292 _("template param type '%s' defined within parent '%s'"),
22293 name ? name : "<unknown>",
22294 parent_name ? parent_name : "<unknown>");
22295 return "";
22296 }
63d06c5c 22297 else
0114d602
DJ
22298 switch (parent->tag)
22299 {
63d06c5c 22300 case DW_TAG_namespace:
0114d602 22301 parent_type = read_type_die (parent, cu);
acebe513
UW
22302 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
22303 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
22304 Work around this problem here. */
22305 if (cu->language == language_cplus
22306 && strcmp (TYPE_TAG_NAME (parent_type), "::") == 0)
22307 return "";
0114d602
DJ
22308 /* We give a name to even anonymous namespaces. */
22309 return TYPE_TAG_NAME (parent_type);
63d06c5c 22310 case DW_TAG_class_type:
680b30c7 22311 case DW_TAG_interface_type:
63d06c5c 22312 case DW_TAG_structure_type:
0114d602 22313 case DW_TAG_union_type:
f55ee35c 22314 case DW_TAG_module:
0114d602
DJ
22315 parent_type = read_type_die (parent, cu);
22316 if (TYPE_TAG_NAME (parent_type) != NULL)
22317 return TYPE_TAG_NAME (parent_type);
22318 else
22319 /* An anonymous structure is only allowed non-static data
22320 members; no typedefs, no member functions, et cetera.
22321 So it does not need a prefix. */
22322 return "";
abc72ce4 22323 case DW_TAG_compile_unit:
95554aad 22324 case DW_TAG_partial_unit:
abc72ce4
DE
22325 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
22326 if (cu->language == language_cplus
8b70b953 22327 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
abc72ce4
DE
22328 && die->child != NULL
22329 && (die->tag == DW_TAG_class_type
22330 || die->tag == DW_TAG_structure_type
22331 || die->tag == DW_TAG_union_type))
22332 {
22333 char *name = guess_full_die_structure_name (die, cu);
22334 if (name != NULL)
22335 return name;
22336 }
22337 return "";
3d567982
TT
22338 case DW_TAG_enumeration_type:
22339 parent_type = read_type_die (parent, cu);
22340 if (TYPE_DECLARED_CLASS (parent_type))
22341 {
22342 if (TYPE_TAG_NAME (parent_type) != NULL)
22343 return TYPE_TAG_NAME (parent_type);
22344 return "";
22345 }
22346 /* Fall through. */
63d06c5c 22347 default:
8176b9b8 22348 return determine_prefix (parent, cu);
63d06c5c 22349 }
63d06c5c
DC
22350}
22351
3e43a32a
MS
22352/* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
22353 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
22354 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
22355 an obconcat, otherwise allocate storage for the result. The CU argument is
22356 used to determine the language and hence, the appropriate separator. */
987504bb 22357
f55ee35c 22358#define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
63d06c5c
DC
22359
22360static char *
f55ee35c
JK
22361typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
22362 int physname, struct dwarf2_cu *cu)
63d06c5c 22363{
f55ee35c 22364 const char *lead = "";
5c315b68 22365 const char *sep;
63d06c5c 22366
3e43a32a
MS
22367 if (suffix == NULL || suffix[0] == '\0'
22368 || prefix == NULL || prefix[0] == '\0')
987504bb 22369 sep = "";
45280282
IB
22370 else if (cu->language == language_d)
22371 {
22372 /* For D, the 'main' function could be defined in any module, but it
22373 should never be prefixed. */
22374 if (strcmp (suffix, "D main") == 0)
22375 {
22376 prefix = "";
22377 sep = "";
22378 }
22379 else
22380 sep = ".";
22381 }
f55ee35c
JK
22382 else if (cu->language == language_fortran && physname)
22383 {
22384 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
22385 DW_AT_MIPS_linkage_name is preferred and used instead. */
22386
22387 lead = "__";
22388 sep = "_MOD_";
22389 }
987504bb
JJ
22390 else
22391 sep = "::";
63d06c5c 22392
6dd47d34
DE
22393 if (prefix == NULL)
22394 prefix = "";
22395 if (suffix == NULL)
22396 suffix = "";
22397
987504bb
JJ
22398 if (obs == NULL)
22399 {
3e43a32a 22400 char *retval
224c3ddb
SM
22401 = ((char *)
22402 xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1));
9a619af0 22403
f55ee35c
JK
22404 strcpy (retval, lead);
22405 strcat (retval, prefix);
6dd47d34
DE
22406 strcat (retval, sep);
22407 strcat (retval, suffix);
63d06c5c
DC
22408 return retval;
22409 }
987504bb
JJ
22410 else
22411 {
22412 /* We have an obstack. */
f55ee35c 22413 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
987504bb 22414 }
63d06c5c
DC
22415}
22416
c906108c
SS
22417/* Return sibling of die, NULL if no sibling. */
22418
f9aca02d 22419static struct die_info *
fba45db2 22420sibling_die (struct die_info *die)
c906108c 22421{
639d11d3 22422 return die->sibling;
c906108c
SS
22423}
22424
71c25dea
TT
22425/* Get name of a die, return NULL if not found. */
22426
15d034d0
TT
22427static const char *
22428dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
71c25dea
TT
22429 struct obstack *obstack)
22430{
22431 if (name && cu->language == language_cplus)
22432 {
2f408ecb 22433 std::string canon_name = cp_canonicalize_string (name);
71c25dea 22434
2f408ecb 22435 if (!canon_name.empty ())
71c25dea 22436 {
2f408ecb
PA
22437 if (canon_name != name)
22438 name = (const char *) obstack_copy0 (obstack,
22439 canon_name.c_str (),
22440 canon_name.length ());
71c25dea
TT
22441 }
22442 }
22443
22444 return name;
c906108c
SS
22445}
22446
96553a0c
DE
22447/* Get name of a die, return NULL if not found.
22448 Anonymous namespaces are converted to their magic string. */
9219021c 22449
15d034d0 22450static const char *
e142c38c 22451dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
9219021c
DC
22452{
22453 struct attribute *attr;
518817b3 22454 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
9219021c 22455
e142c38c 22456 attr = dwarf2_attr (die, DW_AT_name, cu);
53832f31 22457 if ((!attr || !DW_STRING (attr))
96553a0c 22458 && die->tag != DW_TAG_namespace
53832f31
TT
22459 && die->tag != DW_TAG_class_type
22460 && die->tag != DW_TAG_interface_type
22461 && die->tag != DW_TAG_structure_type
22462 && die->tag != DW_TAG_union_type)
71c25dea
TT
22463 return NULL;
22464
22465 switch (die->tag)
22466 {
22467 case DW_TAG_compile_unit:
95554aad 22468 case DW_TAG_partial_unit:
71c25dea
TT
22469 /* Compilation units have a DW_AT_name that is a filename, not
22470 a source language identifier. */
22471 case DW_TAG_enumeration_type:
22472 case DW_TAG_enumerator:
22473 /* These tags always have simple identifiers already; no need
22474 to canonicalize them. */
22475 return DW_STRING (attr);
907af001 22476
96553a0c
DE
22477 case DW_TAG_namespace:
22478 if (attr != NULL && DW_STRING (attr) != NULL)
22479 return DW_STRING (attr);
22480 return CP_ANONYMOUS_NAMESPACE_STR;
22481
907af001
UW
22482 case DW_TAG_class_type:
22483 case DW_TAG_interface_type:
22484 case DW_TAG_structure_type:
22485 case DW_TAG_union_type:
22486 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
22487 structures or unions. These were of the form "._%d" in GCC 4.1,
22488 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
22489 and GCC 4.4. We work around this problem by ignoring these. */
53832f31 22490 if (attr && DW_STRING (attr)
61012eef
GB
22491 && (startswith (DW_STRING (attr), "._")
22492 || startswith (DW_STRING (attr), "<anonymous")))
907af001 22493 return NULL;
53832f31
TT
22494
22495 /* GCC might emit a nameless typedef that has a linkage name. See
22496 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
22497 if (!attr || DW_STRING (attr) == NULL)
22498 {
df5c6c50 22499 char *demangled = NULL;
53832f31 22500
73b9be8b 22501 attr = dw2_linkage_name_attr (die, cu);
53832f31
TT
22502 if (attr == NULL || DW_STRING (attr) == NULL)
22503 return NULL;
22504
df5c6c50
JK
22505 /* Avoid demangling DW_STRING (attr) the second time on a second
22506 call for the same DIE. */
22507 if (!DW_STRING_IS_CANONICAL (attr))
8de20a37 22508 demangled = gdb_demangle (DW_STRING (attr), DMGL_TYPES);
53832f31
TT
22509
22510 if (demangled)
22511 {
e6a959d6 22512 const char *base;
96408a79 22513
53832f31 22514 /* FIXME: we already did this for the partial symbol... */
34a68019 22515 DW_STRING (attr)
224c3ddb 22516 = ((const char *)
e3b94546 22517 obstack_copy0 (&objfile->per_bfd->storage_obstack,
224c3ddb 22518 demangled, strlen (demangled)));
53832f31
TT
22519 DW_STRING_IS_CANONICAL (attr) = 1;
22520 xfree (demangled);
96408a79
SA
22521
22522 /* Strip any leading namespaces/classes, keep only the base name.
22523 DW_AT_name for named DIEs does not contain the prefixes. */
22524 base = strrchr (DW_STRING (attr), ':');
22525 if (base && base > DW_STRING (attr) && base[-1] == ':')
22526 return &base[1];
22527 else
22528 return DW_STRING (attr);
53832f31
TT
22529 }
22530 }
907af001
UW
22531 break;
22532
71c25dea 22533 default:
907af001
UW
22534 break;
22535 }
22536
22537 if (!DW_STRING_IS_CANONICAL (attr))
22538 {
22539 DW_STRING (attr)
22540 = dwarf2_canonicalize_name (DW_STRING (attr), cu,
e3b94546 22541 &objfile->per_bfd->storage_obstack);
907af001 22542 DW_STRING_IS_CANONICAL (attr) = 1;
71c25dea 22543 }
907af001 22544 return DW_STRING (attr);
9219021c
DC
22545}
22546
22547/* Return the die that this die in an extension of, or NULL if there
f2f0e013
DJ
22548 is none. *EXT_CU is the CU containing DIE on input, and the CU
22549 containing the return value on output. */
9219021c
DC
22550
22551static struct die_info *
f2f0e013 22552dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
9219021c
DC
22553{
22554 struct attribute *attr;
9219021c 22555
f2f0e013 22556 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
9219021c
DC
22557 if (attr == NULL)
22558 return NULL;
22559
f2f0e013 22560 return follow_die_ref (die, attr, ext_cu);
9219021c
DC
22561}
22562
c906108c
SS
22563/* Convert a DIE tag into its string name. */
22564
f39c6ffd 22565static const char *
aa1ee363 22566dwarf_tag_name (unsigned tag)
c906108c 22567{
f39c6ffd
TT
22568 const char *name = get_DW_TAG_name (tag);
22569
22570 if (name == NULL)
22571 return "DW_TAG_<unknown>";
22572
22573 return name;
c906108c
SS
22574}
22575
22576/* Convert a DWARF attribute code into its string name. */
22577
f39c6ffd 22578static const char *
aa1ee363 22579dwarf_attr_name (unsigned attr)
c906108c 22580{
f39c6ffd
TT
22581 const char *name;
22582
c764a876 22583#ifdef MIPS /* collides with DW_AT_HP_block_index */
f39c6ffd
TT
22584 if (attr == DW_AT_MIPS_fde)
22585 return "DW_AT_MIPS_fde";
22586#else
22587 if (attr == DW_AT_HP_block_index)
22588 return "DW_AT_HP_block_index";
c764a876 22589#endif
f39c6ffd
TT
22590
22591 name = get_DW_AT_name (attr);
22592
22593 if (name == NULL)
22594 return "DW_AT_<unknown>";
22595
22596 return name;
c906108c
SS
22597}
22598
22599/* Convert a DWARF value form code into its string name. */
22600
f39c6ffd 22601static const char *
aa1ee363 22602dwarf_form_name (unsigned form)
c906108c 22603{
f39c6ffd
TT
22604 const char *name = get_DW_FORM_name (form);
22605
22606 if (name == NULL)
22607 return "DW_FORM_<unknown>";
22608
22609 return name;
c906108c
SS
22610}
22611
a121b7c1 22612static const char *
fba45db2 22613dwarf_bool_name (unsigned mybool)
c906108c
SS
22614{
22615 if (mybool)
22616 return "TRUE";
22617 else
22618 return "FALSE";
22619}
22620
22621/* Convert a DWARF type code into its string name. */
22622
f39c6ffd 22623static const char *
aa1ee363 22624dwarf_type_encoding_name (unsigned enc)
c906108c 22625{
f39c6ffd 22626 const char *name = get_DW_ATE_name (enc);
c906108c 22627
f39c6ffd
TT
22628 if (name == NULL)
22629 return "DW_ATE_<unknown>";
c906108c 22630
f39c6ffd 22631 return name;
c906108c 22632}
c906108c 22633
f9aca02d 22634static void
d97bc12b 22635dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
c906108c
SS
22636{
22637 unsigned int i;
22638
d97bc12b 22639 print_spaces (indent, f);
9d8780f0 22640 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset %s)\n",
9c541725 22641 dwarf_tag_name (die->tag), die->abbrev,
9d8780f0 22642 sect_offset_str (die->sect_off));
d97bc12b
DE
22643
22644 if (die->parent != NULL)
22645 {
22646 print_spaces (indent, f);
9d8780f0
SM
22647 fprintf_unfiltered (f, " parent at offset: %s\n",
22648 sect_offset_str (die->parent->sect_off));
d97bc12b
DE
22649 }
22650
22651 print_spaces (indent, f);
22652 fprintf_unfiltered (f, " has children: %s\n",
639d11d3 22653 dwarf_bool_name (die->child != NULL));
c906108c 22654
d97bc12b
DE
22655 print_spaces (indent, f);
22656 fprintf_unfiltered (f, " attributes:\n");
22657
c906108c
SS
22658 for (i = 0; i < die->num_attrs; ++i)
22659 {
d97bc12b
DE
22660 print_spaces (indent, f);
22661 fprintf_unfiltered (f, " %s (%s) ",
c906108c
SS
22662 dwarf_attr_name (die->attrs[i].name),
22663 dwarf_form_name (die->attrs[i].form));
d97bc12b 22664
c906108c
SS
22665 switch (die->attrs[i].form)
22666 {
c906108c 22667 case DW_FORM_addr:
3019eac3 22668 case DW_FORM_GNU_addr_index:
d97bc12b 22669 fprintf_unfiltered (f, "address: ");
5af949e3 22670 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
c906108c
SS
22671 break;
22672 case DW_FORM_block2:
22673 case DW_FORM_block4:
22674 case DW_FORM_block:
22675 case DW_FORM_block1:
56eb65bd
SP
22676 fprintf_unfiltered (f, "block: size %s",
22677 pulongest (DW_BLOCK (&die->attrs[i])->size));
c906108c 22678 break;
2dc7f7b3 22679 case DW_FORM_exprloc:
56eb65bd
SP
22680 fprintf_unfiltered (f, "expression: size %s",
22681 pulongest (DW_BLOCK (&die->attrs[i])->size));
2dc7f7b3 22682 break;
0224619f
JK
22683 case DW_FORM_data16:
22684 fprintf_unfiltered (f, "constant of 16 bytes");
22685 break;
4568ecf9
DE
22686 case DW_FORM_ref_addr:
22687 fprintf_unfiltered (f, "ref address: ");
22688 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
22689 break;
36586728
TT
22690 case DW_FORM_GNU_ref_alt:
22691 fprintf_unfiltered (f, "alt ref address: ");
22692 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
22693 break;
10b3939b
DJ
22694 case DW_FORM_ref1:
22695 case DW_FORM_ref2:
22696 case DW_FORM_ref4:
4568ecf9
DE
22697 case DW_FORM_ref8:
22698 case DW_FORM_ref_udata:
d97bc12b 22699 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
4568ecf9 22700 (long) (DW_UNSND (&die->attrs[i])));
10b3939b 22701 break;
c906108c
SS
22702 case DW_FORM_data1:
22703 case DW_FORM_data2:
22704 case DW_FORM_data4:
ce5d95e1 22705 case DW_FORM_data8:
c906108c
SS
22706 case DW_FORM_udata:
22707 case DW_FORM_sdata:
43bbcdc2
PH
22708 fprintf_unfiltered (f, "constant: %s",
22709 pulongest (DW_UNSND (&die->attrs[i])));
c906108c 22710 break;
2dc7f7b3
TT
22711 case DW_FORM_sec_offset:
22712 fprintf_unfiltered (f, "section offset: %s",
22713 pulongest (DW_UNSND (&die->attrs[i])));
22714 break;
55f1336d 22715 case DW_FORM_ref_sig8:
ac9ec31b
DE
22716 fprintf_unfiltered (f, "signature: %s",
22717 hex_string (DW_SIGNATURE (&die->attrs[i])));
348e048f 22718 break;
c906108c 22719 case DW_FORM_string:
4bdf3d34 22720 case DW_FORM_strp:
43988095 22721 case DW_FORM_line_strp:
3019eac3 22722 case DW_FORM_GNU_str_index:
36586728 22723 case DW_FORM_GNU_strp_alt:
8285870a 22724 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
c906108c 22725 DW_STRING (&die->attrs[i])
8285870a
JK
22726 ? DW_STRING (&die->attrs[i]) : "",
22727 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
c906108c
SS
22728 break;
22729 case DW_FORM_flag:
22730 if (DW_UNSND (&die->attrs[i]))
d97bc12b 22731 fprintf_unfiltered (f, "flag: TRUE");
c906108c 22732 else
d97bc12b 22733 fprintf_unfiltered (f, "flag: FALSE");
c906108c 22734 break;
2dc7f7b3
TT
22735 case DW_FORM_flag_present:
22736 fprintf_unfiltered (f, "flag: TRUE");
22737 break;
a8329558 22738 case DW_FORM_indirect:
0963b4bd
MS
22739 /* The reader will have reduced the indirect form to
22740 the "base form" so this form should not occur. */
3e43a32a
MS
22741 fprintf_unfiltered (f,
22742 "unexpected attribute form: DW_FORM_indirect");
a8329558 22743 break;
663c44ac
JK
22744 case DW_FORM_implicit_const:
22745 fprintf_unfiltered (f, "constant: %s",
22746 plongest (DW_SND (&die->attrs[i])));
22747 break;
c906108c 22748 default:
d97bc12b 22749 fprintf_unfiltered (f, "unsupported attribute form: %d.",
c5aa993b 22750 die->attrs[i].form);
d97bc12b 22751 break;
c906108c 22752 }
d97bc12b 22753 fprintf_unfiltered (f, "\n");
c906108c
SS
22754 }
22755}
22756
f9aca02d 22757static void
d97bc12b 22758dump_die_for_error (struct die_info *die)
c906108c 22759{
d97bc12b
DE
22760 dump_die_shallow (gdb_stderr, 0, die);
22761}
22762
22763static void
22764dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
22765{
22766 int indent = level * 4;
22767
22768 gdb_assert (die != NULL);
22769
22770 if (level >= max_level)
22771 return;
22772
22773 dump_die_shallow (f, indent, die);
22774
22775 if (die->child != NULL)
c906108c 22776 {
d97bc12b
DE
22777 print_spaces (indent, f);
22778 fprintf_unfiltered (f, " Children:");
22779 if (level + 1 < max_level)
22780 {
22781 fprintf_unfiltered (f, "\n");
22782 dump_die_1 (f, level + 1, max_level, die->child);
22783 }
22784 else
22785 {
3e43a32a
MS
22786 fprintf_unfiltered (f,
22787 " [not printed, max nesting level reached]\n");
d97bc12b
DE
22788 }
22789 }
22790
22791 if (die->sibling != NULL && level > 0)
22792 {
22793 dump_die_1 (f, level, max_level, die->sibling);
c906108c
SS
22794 }
22795}
22796
d97bc12b
DE
22797/* This is called from the pdie macro in gdbinit.in.
22798 It's not static so gcc will keep a copy callable from gdb. */
22799
22800void
22801dump_die (struct die_info *die, int max_level)
22802{
22803 dump_die_1 (gdb_stdlog, 0, max_level, die);
22804}
22805
f9aca02d 22806static void
51545339 22807store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
c906108c 22808{
51545339 22809 void **slot;
c906108c 22810
9c541725
PA
22811 slot = htab_find_slot_with_hash (cu->die_hash, die,
22812 to_underlying (die->sect_off),
b64f50a1 22813 INSERT);
51545339
DJ
22814
22815 *slot = die;
c906108c
SS
22816}
22817
b64f50a1
JK
22818/* Return DIE offset of ATTR. Return 0 with complaint if ATTR is not of the
22819 required kind. */
22820
22821static sect_offset
ff39bb5e 22822dwarf2_get_ref_die_offset (const struct attribute *attr)
93311388 22823{
7771576e 22824 if (attr_form_is_ref (attr))
9c541725 22825 return (sect_offset) DW_UNSND (attr);
93311388
DE
22826
22827 complaint (&symfile_complaints,
22828 _("unsupported die ref attribute form: '%s'"),
22829 dwarf_form_name (attr->form));
9c541725 22830 return {};
c906108c
SS
22831}
22832
43bbcdc2
PH
22833/* Return the constant value held by ATTR. Return DEFAULT_VALUE if
22834 * the value held by the attribute is not constant. */
a02abb62 22835
43bbcdc2 22836static LONGEST
ff39bb5e 22837dwarf2_get_attr_constant_value (const struct attribute *attr, int default_value)
a02abb62 22838{
663c44ac 22839 if (attr->form == DW_FORM_sdata || attr->form == DW_FORM_implicit_const)
a02abb62
JB
22840 return DW_SND (attr);
22841 else if (attr->form == DW_FORM_udata
22842 || attr->form == DW_FORM_data1
22843 || attr->form == DW_FORM_data2
22844 || attr->form == DW_FORM_data4
22845 || attr->form == DW_FORM_data8)
22846 return DW_UNSND (attr);
22847 else
22848 {
0224619f 22849 /* For DW_FORM_data16 see attr_form_is_constant. */
3e43a32a
MS
22850 complaint (&symfile_complaints,
22851 _("Attribute value is not a constant (%s)"),
a02abb62
JB
22852 dwarf_form_name (attr->form));
22853 return default_value;
22854 }
22855}
22856
348e048f
DE
22857/* Follow reference or signature attribute ATTR of SRC_DIE.
22858 On entry *REF_CU is the CU of SRC_DIE.
22859 On exit *REF_CU is the CU of the result. */
22860
22861static struct die_info *
ff39bb5e 22862follow_die_ref_or_sig (struct die_info *src_die, const struct attribute *attr,
348e048f
DE
22863 struct dwarf2_cu **ref_cu)
22864{
22865 struct die_info *die;
22866
7771576e 22867 if (attr_form_is_ref (attr))
348e048f 22868 die = follow_die_ref (src_die, attr, ref_cu);
55f1336d 22869 else if (attr->form == DW_FORM_ref_sig8)
348e048f
DE
22870 die = follow_die_sig (src_die, attr, ref_cu);
22871 else
22872 {
22873 dump_die_for_error (src_die);
22874 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
518817b3 22875 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
348e048f
DE
22876 }
22877
22878 return die;
03dd20cc
DJ
22879}
22880
5c631832 22881/* Follow reference OFFSET.
673bfd45
DE
22882 On entry *REF_CU is the CU of the source die referencing OFFSET.
22883 On exit *REF_CU is the CU of the result.
22884 Returns NULL if OFFSET is invalid. */
f504f079 22885
f9aca02d 22886static struct die_info *
9c541725 22887follow_die_offset (sect_offset sect_off, int offset_in_dwz,
36586728 22888 struct dwarf2_cu **ref_cu)
c906108c 22889{
10b3939b 22890 struct die_info temp_die;
f2f0e013 22891 struct dwarf2_cu *target_cu, *cu = *ref_cu;
518817b3
SM
22892 struct dwarf2_per_objfile *dwarf2_per_objfile
22893 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 22894 struct objfile *objfile = dwarf2_per_objfile->objfile;
10b3939b 22895
348e048f
DE
22896 gdb_assert (cu->per_cu != NULL);
22897
98bfdba5
PA
22898 target_cu = cu;
22899
3019eac3 22900 if (cu->per_cu->is_debug_types)
348e048f
DE
22901 {
22902 /* .debug_types CUs cannot reference anything outside their CU.
22903 If they need to, they have to reference a signatured type via
55f1336d 22904 DW_FORM_ref_sig8. */
9c541725 22905 if (!offset_in_cu_p (&cu->header, sect_off))
5c631832 22906 return NULL;
348e048f 22907 }
36586728 22908 else if (offset_in_dwz != cu->per_cu->is_dwz
9c541725 22909 || !offset_in_cu_p (&cu->header, sect_off))
10b3939b
DJ
22910 {
22911 struct dwarf2_per_cu_data *per_cu;
9a619af0 22912
9c541725 22913 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
ed2dc618 22914 dwarf2_per_objfile);
03dd20cc
DJ
22915
22916 /* If necessary, add it to the queue and load its DIEs. */
95554aad
TT
22917 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
22918 load_full_comp_unit (per_cu, cu->language);
03dd20cc 22919
10b3939b
DJ
22920 target_cu = per_cu->cu;
22921 }
98bfdba5
PA
22922 else if (cu->dies == NULL)
22923 {
22924 /* We're loading full DIEs during partial symbol reading. */
22925 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
95554aad 22926 load_full_comp_unit (cu->per_cu, language_minimal);
98bfdba5 22927 }
c906108c 22928
f2f0e013 22929 *ref_cu = target_cu;
9c541725 22930 temp_die.sect_off = sect_off;
9a3c8263 22931 return (struct die_info *) htab_find_with_hash (target_cu->die_hash,
9c541725
PA
22932 &temp_die,
22933 to_underlying (sect_off));
5c631832 22934}
10b3939b 22935
5c631832
JK
22936/* Follow reference attribute ATTR of SRC_DIE.
22937 On entry *REF_CU is the CU of SRC_DIE.
22938 On exit *REF_CU is the CU of the result. */
22939
22940static struct die_info *
ff39bb5e 22941follow_die_ref (struct die_info *src_die, const struct attribute *attr,
5c631832
JK
22942 struct dwarf2_cu **ref_cu)
22943{
9c541725 22944 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
5c631832
JK
22945 struct dwarf2_cu *cu = *ref_cu;
22946 struct die_info *die;
22947
9c541725 22948 die = follow_die_offset (sect_off,
36586728
TT
22949 (attr->form == DW_FORM_GNU_ref_alt
22950 || cu->per_cu->is_dwz),
22951 ref_cu);
5c631832 22952 if (!die)
9d8780f0
SM
22953 error (_("Dwarf Error: Cannot find DIE at %s referenced from DIE "
22954 "at %s [in module %s]"),
22955 sect_offset_str (sect_off), sect_offset_str (src_die->sect_off),
518817b3 22956 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
348e048f 22957
5c631832
JK
22958 return die;
22959}
22960
9c541725 22961/* Return DWARF block referenced by DW_AT_location of DIE at SECT_OFF at PER_CU.
d83e736b 22962 Returned value is intended for DW_OP_call*. Returned
e3b94546
SM
22963 dwarf2_locexpr_baton->data has lifetime of
22964 PER_CU->DWARF2_PER_OBJFILE->OBJFILE. */
5c631832
JK
22965
22966struct dwarf2_locexpr_baton
9c541725 22967dwarf2_fetch_die_loc_sect_off (sect_offset sect_off,
8b9737bf
TT
22968 struct dwarf2_per_cu_data *per_cu,
22969 CORE_ADDR (*get_frame_pc) (void *baton),
22970 void *baton)
5c631832 22971{
918dd910 22972 struct dwarf2_cu *cu;
5c631832
JK
22973 struct die_info *die;
22974 struct attribute *attr;
22975 struct dwarf2_locexpr_baton retval;
e3b94546 22976 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
ed2dc618
SM
22977 struct dwarf2_per_objfile *dwarf2_per_objfile
22978 = get_dwarf2_per_objfile (objfile);
8cf6f0b1 22979
918dd910
JK
22980 if (per_cu->cu == NULL)
22981 load_cu (per_cu);
22982 cu = per_cu->cu;
cc12ce38
DE
22983 if (cu == NULL)
22984 {
22985 /* We shouldn't get here for a dummy CU, but don't crash on the user.
22986 Instead just throw an error, not much else we can do. */
9d8780f0
SM
22987 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
22988 sect_offset_str (sect_off), objfile_name (objfile));
cc12ce38 22989 }
918dd910 22990
9c541725 22991 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
5c631832 22992 if (!die)
9d8780f0
SM
22993 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
22994 sect_offset_str (sect_off), objfile_name (objfile));
5c631832
JK
22995
22996 attr = dwarf2_attr (die, DW_AT_location, cu);
22997 if (!attr)
22998 {
e103e986
JK
22999 /* DWARF: "If there is no such attribute, then there is no effect.".
23000 DATA is ignored if SIZE is 0. */
5c631832 23001
e103e986 23002 retval.data = NULL;
5c631832
JK
23003 retval.size = 0;
23004 }
8cf6f0b1
TT
23005 else if (attr_form_is_section_offset (attr))
23006 {
23007 struct dwarf2_loclist_baton loclist_baton;
23008 CORE_ADDR pc = (*get_frame_pc) (baton);
23009 size_t size;
23010
23011 fill_in_loclist_baton (cu, &loclist_baton, attr);
23012
23013 retval.data = dwarf2_find_location_expression (&loclist_baton,
23014 &size, pc);
23015 retval.size = size;
23016 }
5c631832
JK
23017 else
23018 {
23019 if (!attr_form_is_block (attr))
9d8780f0 23020 error (_("Dwarf Error: DIE at %s referenced in module %s "
5c631832 23021 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
9d8780f0 23022 sect_offset_str (sect_off), objfile_name (objfile));
5c631832
JK
23023
23024 retval.data = DW_BLOCK (attr)->data;
23025 retval.size = DW_BLOCK (attr)->size;
23026 }
23027 retval.per_cu = cu->per_cu;
918dd910 23028
ed2dc618 23029 age_cached_comp_units (dwarf2_per_objfile);
918dd910 23030
5c631832 23031 return retval;
348e048f
DE
23032}
23033
8b9737bf
TT
23034/* Like dwarf2_fetch_die_loc_sect_off, but take a CU
23035 offset. */
23036
23037struct dwarf2_locexpr_baton
23038dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
23039 struct dwarf2_per_cu_data *per_cu,
23040 CORE_ADDR (*get_frame_pc) (void *baton),
23041 void *baton)
23042{
9c541725 23043 sect_offset sect_off = per_cu->sect_off + to_underlying (offset_in_cu);
8b9737bf 23044
9c541725 23045 return dwarf2_fetch_die_loc_sect_off (sect_off, per_cu, get_frame_pc, baton);
8b9737bf
TT
23046}
23047
b6807d98
TT
23048/* Write a constant of a given type as target-ordered bytes into
23049 OBSTACK. */
23050
23051static const gdb_byte *
23052write_constant_as_bytes (struct obstack *obstack,
23053 enum bfd_endian byte_order,
23054 struct type *type,
23055 ULONGEST value,
23056 LONGEST *len)
23057{
23058 gdb_byte *result;
23059
23060 *len = TYPE_LENGTH (type);
224c3ddb 23061 result = (gdb_byte *) obstack_alloc (obstack, *len);
b6807d98
TT
23062 store_unsigned_integer (result, *len, byte_order, value);
23063
23064 return result;
23065}
23066
23067/* If the DIE at OFFSET in PER_CU has a DW_AT_const_value, return a
23068 pointer to the constant bytes and set LEN to the length of the
23069 data. If memory is needed, allocate it on OBSTACK. If the DIE
23070 does not have a DW_AT_const_value, return NULL. */
23071
23072const gdb_byte *
9c541725 23073dwarf2_fetch_constant_bytes (sect_offset sect_off,
b6807d98
TT
23074 struct dwarf2_per_cu_data *per_cu,
23075 struct obstack *obstack,
23076 LONGEST *len)
23077{
23078 struct dwarf2_cu *cu;
23079 struct die_info *die;
23080 struct attribute *attr;
23081 const gdb_byte *result = NULL;
23082 struct type *type;
23083 LONGEST value;
23084 enum bfd_endian byte_order;
e3b94546 23085 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
b6807d98 23086
b6807d98
TT
23087 if (per_cu->cu == NULL)
23088 load_cu (per_cu);
23089 cu = per_cu->cu;
cc12ce38
DE
23090 if (cu == NULL)
23091 {
23092 /* We shouldn't get here for a dummy CU, but don't crash on the user.
23093 Instead just throw an error, not much else we can do. */
9d8780f0
SM
23094 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
23095 sect_offset_str (sect_off), objfile_name (objfile));
cc12ce38 23096 }
b6807d98 23097
9c541725 23098 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
b6807d98 23099 if (!die)
9d8780f0
SM
23100 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
23101 sect_offset_str (sect_off), objfile_name (objfile));
b6807d98
TT
23102
23103 attr = dwarf2_attr (die, DW_AT_const_value, cu);
23104 if (attr == NULL)
23105 return NULL;
23106
e3b94546 23107 byte_order = (bfd_big_endian (objfile->obfd)
b6807d98
TT
23108 ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
23109
23110 switch (attr->form)
23111 {
23112 case DW_FORM_addr:
23113 case DW_FORM_GNU_addr_index:
23114 {
23115 gdb_byte *tem;
23116
23117 *len = cu->header.addr_size;
224c3ddb 23118 tem = (gdb_byte *) obstack_alloc (obstack, *len);
b6807d98
TT
23119 store_unsigned_integer (tem, *len, byte_order, DW_ADDR (attr));
23120 result = tem;
23121 }
23122 break;
23123 case DW_FORM_string:
23124 case DW_FORM_strp:
23125 case DW_FORM_GNU_str_index:
23126 case DW_FORM_GNU_strp_alt:
23127 /* DW_STRING is already allocated on the objfile obstack, point
23128 directly to it. */
23129 result = (const gdb_byte *) DW_STRING (attr);
23130 *len = strlen (DW_STRING (attr));
23131 break;
23132 case DW_FORM_block1:
23133 case DW_FORM_block2:
23134 case DW_FORM_block4:
23135 case DW_FORM_block:
23136 case DW_FORM_exprloc:
0224619f 23137 case DW_FORM_data16:
b6807d98
TT
23138 result = DW_BLOCK (attr)->data;
23139 *len = DW_BLOCK (attr)->size;
23140 break;
23141
23142 /* The DW_AT_const_value attributes are supposed to carry the
23143 symbol's value "represented as it would be on the target
23144 architecture." By the time we get here, it's already been
23145 converted to host endianness, so we just need to sign- or
23146 zero-extend it as appropriate. */
23147 case DW_FORM_data1:
23148 type = die_type (die, cu);
23149 result = dwarf2_const_value_data (attr, obstack, cu, &value, 8);
23150 if (result == NULL)
23151 result = write_constant_as_bytes (obstack, byte_order,
23152 type, value, len);
23153 break;
23154 case DW_FORM_data2:
23155 type = die_type (die, cu);
23156 result = dwarf2_const_value_data (attr, obstack, cu, &value, 16);
23157 if (result == NULL)
23158 result = write_constant_as_bytes (obstack, byte_order,
23159 type, value, len);
23160 break;
23161 case DW_FORM_data4:
23162 type = die_type (die, cu);
23163 result = dwarf2_const_value_data (attr, obstack, cu, &value, 32);
23164 if (result == NULL)
23165 result = write_constant_as_bytes (obstack, byte_order,
23166 type, value, len);
23167 break;
23168 case DW_FORM_data8:
23169 type = die_type (die, cu);
23170 result = dwarf2_const_value_data (attr, obstack, cu, &value, 64);
23171 if (result == NULL)
23172 result = write_constant_as_bytes (obstack, byte_order,
23173 type, value, len);
23174 break;
23175
23176 case DW_FORM_sdata:
663c44ac 23177 case DW_FORM_implicit_const:
b6807d98
TT
23178 type = die_type (die, cu);
23179 result = write_constant_as_bytes (obstack, byte_order,
23180 type, DW_SND (attr), len);
23181 break;
23182
23183 case DW_FORM_udata:
23184 type = die_type (die, cu);
23185 result = write_constant_as_bytes (obstack, byte_order,
23186 type, DW_UNSND (attr), len);
23187 break;
23188
23189 default:
23190 complaint (&symfile_complaints,
23191 _("unsupported const value attribute form: '%s'"),
23192 dwarf_form_name (attr->form));
23193 break;
23194 }
23195
23196 return result;
23197}
23198
7942e96e
AA
23199/* Return the type of the die at OFFSET in PER_CU. Return NULL if no
23200 valid type for this die is found. */
23201
23202struct type *
9c541725 23203dwarf2_fetch_die_type_sect_off (sect_offset sect_off,
7942e96e
AA
23204 struct dwarf2_per_cu_data *per_cu)
23205{
23206 struct dwarf2_cu *cu;
23207 struct die_info *die;
23208
7942e96e
AA
23209 if (per_cu->cu == NULL)
23210 load_cu (per_cu);
23211 cu = per_cu->cu;
23212 if (!cu)
23213 return NULL;
23214
9c541725 23215 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
7942e96e
AA
23216 if (!die)
23217 return NULL;
23218
23219 return die_type (die, cu);
23220}
23221
8a9b8146
TT
23222/* Return the type of the DIE at DIE_OFFSET in the CU named by
23223 PER_CU. */
23224
23225struct type *
b64f50a1 23226dwarf2_get_die_type (cu_offset die_offset,
8a9b8146
TT
23227 struct dwarf2_per_cu_data *per_cu)
23228{
9c541725 23229 sect_offset die_offset_sect = per_cu->sect_off + to_underlying (die_offset);
b64f50a1 23230 return get_die_type_at_offset (die_offset_sect, per_cu);
8a9b8146
TT
23231}
23232
ac9ec31b 23233/* Follow type unit SIG_TYPE referenced by SRC_DIE.
348e048f 23234 On entry *REF_CU is the CU of SRC_DIE.
ac9ec31b
DE
23235 On exit *REF_CU is the CU of the result.
23236 Returns NULL if the referenced DIE isn't found. */
348e048f
DE
23237
23238static struct die_info *
ac9ec31b
DE
23239follow_die_sig_1 (struct die_info *src_die, struct signatured_type *sig_type,
23240 struct dwarf2_cu **ref_cu)
348e048f 23241{
348e048f 23242 struct die_info temp_die;
348e048f
DE
23243 struct dwarf2_cu *sig_cu;
23244 struct die_info *die;
23245
ac9ec31b
DE
23246 /* While it might be nice to assert sig_type->type == NULL here,
23247 we can get here for DW_AT_imported_declaration where we need
23248 the DIE not the type. */
348e048f
DE
23249
23250 /* If necessary, add it to the queue and load its DIEs. */
23251
95554aad 23252 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, language_minimal))
a0f42c21 23253 read_signatured_type (sig_type);
348e048f 23254
348e048f 23255 sig_cu = sig_type->per_cu.cu;
69d751e3 23256 gdb_assert (sig_cu != NULL);
9c541725
PA
23257 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
23258 temp_die.sect_off = sig_type->type_offset_in_section;
9a3c8263 23259 die = (struct die_info *) htab_find_with_hash (sig_cu->die_hash, &temp_die,
9c541725 23260 to_underlying (temp_die.sect_off));
348e048f
DE
23261 if (die)
23262 {
ed2dc618 23263 struct dwarf2_per_objfile *dwarf2_per_objfile
518817b3 23264 = (*ref_cu)->per_cu->dwarf2_per_objfile;
ed2dc618 23265
796a7ff8
DE
23266 /* For .gdb_index version 7 keep track of included TUs.
23267 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
23268 if (dwarf2_per_objfile->index_table != NULL
23269 && dwarf2_per_objfile->index_table->version <= 7)
23270 {
23271 VEC_safe_push (dwarf2_per_cu_ptr,
23272 (*ref_cu)->per_cu->imported_symtabs,
23273 sig_cu->per_cu);
23274 }
23275
348e048f
DE
23276 *ref_cu = sig_cu;
23277 return die;
23278 }
23279
ac9ec31b
DE
23280 return NULL;
23281}
23282
23283/* Follow signatured type referenced by ATTR in SRC_DIE.
23284 On entry *REF_CU is the CU of SRC_DIE.
23285 On exit *REF_CU is the CU of the result.
23286 The result is the DIE of the type.
23287 If the referenced type cannot be found an error is thrown. */
23288
23289static struct die_info *
ff39bb5e 23290follow_die_sig (struct die_info *src_die, const struct attribute *attr,
ac9ec31b
DE
23291 struct dwarf2_cu **ref_cu)
23292{
23293 ULONGEST signature = DW_SIGNATURE (attr);
23294 struct signatured_type *sig_type;
23295 struct die_info *die;
23296
23297 gdb_assert (attr->form == DW_FORM_ref_sig8);
23298
a2ce51a0 23299 sig_type = lookup_signatured_type (*ref_cu, signature);
ac9ec31b
DE
23300 /* sig_type will be NULL if the signatured type is missing from
23301 the debug info. */
23302 if (sig_type == NULL)
23303 {
23304 error (_("Dwarf Error: Cannot find signatured DIE %s referenced"
9d8780f0
SM
23305 " from DIE at %s [in module %s]"),
23306 hex_string (signature), sect_offset_str (src_die->sect_off),
518817b3 23307 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
ac9ec31b
DE
23308 }
23309
23310 die = follow_die_sig_1 (src_die, sig_type, ref_cu);
23311 if (die == NULL)
23312 {
23313 dump_die_for_error (src_die);
23314 error (_("Dwarf Error: Problem reading signatured DIE %s referenced"
9d8780f0
SM
23315 " from DIE at %s [in module %s]"),
23316 hex_string (signature), sect_offset_str (src_die->sect_off),
518817b3 23317 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
ac9ec31b
DE
23318 }
23319
23320 return die;
23321}
23322
23323/* Get the type specified by SIGNATURE referenced in DIE/CU,
23324 reading in and processing the type unit if necessary. */
23325
23326static struct type *
23327get_signatured_type (struct die_info *die, ULONGEST signature,
23328 struct dwarf2_cu *cu)
23329{
518817b3
SM
23330 struct dwarf2_per_objfile *dwarf2_per_objfile
23331 = cu->per_cu->dwarf2_per_objfile;
ac9ec31b
DE
23332 struct signatured_type *sig_type;
23333 struct dwarf2_cu *type_cu;
23334 struct die_info *type_die;
23335 struct type *type;
23336
a2ce51a0 23337 sig_type = lookup_signatured_type (cu, signature);
ac9ec31b
DE
23338 /* sig_type will be NULL if the signatured type is missing from
23339 the debug info. */
23340 if (sig_type == NULL)
23341 {
23342 complaint (&symfile_complaints,
23343 _("Dwarf Error: Cannot find signatured DIE %s referenced"
9d8780f0
SM
23344 " from DIE at %s [in module %s]"),
23345 hex_string (signature), sect_offset_str (die->sect_off),
4262abfb 23346 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
23347 return build_error_marker_type (cu, die);
23348 }
23349
23350 /* If we already know the type we're done. */
23351 if (sig_type->type != NULL)
23352 return sig_type->type;
23353
23354 type_cu = cu;
23355 type_die = follow_die_sig_1 (die, sig_type, &type_cu);
23356 if (type_die != NULL)
23357 {
23358 /* N.B. We need to call get_die_type to ensure only one type for this DIE
23359 is created. This is important, for example, because for c++ classes
23360 we need TYPE_NAME set which is only done by new_symbol. Blech. */
23361 type = read_type_die (type_die, type_cu);
23362 if (type == NULL)
23363 {
23364 complaint (&symfile_complaints,
23365 _("Dwarf Error: Cannot build signatured type %s"
9d8780f0
SM
23366 " referenced from DIE at %s [in module %s]"),
23367 hex_string (signature), sect_offset_str (die->sect_off),
4262abfb 23368 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
23369 type = build_error_marker_type (cu, die);
23370 }
23371 }
23372 else
23373 {
23374 complaint (&symfile_complaints,
23375 _("Dwarf Error: Problem reading signatured DIE %s referenced"
9d8780f0
SM
23376 " from DIE at %s [in module %s]"),
23377 hex_string (signature), sect_offset_str (die->sect_off),
4262abfb 23378 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
23379 type = build_error_marker_type (cu, die);
23380 }
23381 sig_type->type = type;
23382
23383 return type;
23384}
23385
23386/* Get the type specified by the DW_AT_signature ATTR in DIE/CU,
23387 reading in and processing the type unit if necessary. */
23388
23389static struct type *
ff39bb5e 23390get_DW_AT_signature_type (struct die_info *die, const struct attribute *attr,
b385a60d 23391 struct dwarf2_cu *cu) /* ARI: editCase function */
ac9ec31b
DE
23392{
23393 /* Yes, DW_AT_signature can use a non-ref_sig8 reference. */
7771576e 23394 if (attr_form_is_ref (attr))
ac9ec31b
DE
23395 {
23396 struct dwarf2_cu *type_cu = cu;
23397 struct die_info *type_die = follow_die_ref (die, attr, &type_cu);
23398
23399 return read_type_die (type_die, type_cu);
23400 }
23401 else if (attr->form == DW_FORM_ref_sig8)
23402 {
23403 return get_signatured_type (die, DW_SIGNATURE (attr), cu);
23404 }
23405 else
23406 {
518817b3
SM
23407 struct dwarf2_per_objfile *dwarf2_per_objfile
23408 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 23409
ac9ec31b
DE
23410 complaint (&symfile_complaints,
23411 _("Dwarf Error: DW_AT_signature has bad form %s in DIE"
9d8780f0
SM
23412 " at %s [in module %s]"),
23413 dwarf_form_name (attr->form), sect_offset_str (die->sect_off),
4262abfb 23414 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
23415 return build_error_marker_type (cu, die);
23416 }
348e048f
DE
23417}
23418
e5fe5e75 23419/* Load the DIEs associated with type unit PER_CU into memory. */
348e048f
DE
23420
23421static void
e5fe5e75 23422load_full_type_unit (struct dwarf2_per_cu_data *per_cu)
348e048f 23423{
52dc124a 23424 struct signatured_type *sig_type;
348e048f 23425
f4dc4d17
DE
23426 /* Caller is responsible for ensuring type_unit_groups don't get here. */
23427 gdb_assert (! IS_TYPE_UNIT_GROUP (per_cu));
23428
6721b2ec
DE
23429 /* We have the per_cu, but we need the signatured_type.
23430 Fortunately this is an easy translation. */
23431 gdb_assert (per_cu->is_debug_types);
23432 sig_type = (struct signatured_type *) per_cu;
348e048f 23433
6721b2ec 23434 gdb_assert (per_cu->cu == NULL);
348e048f 23435
52dc124a 23436 read_signatured_type (sig_type);
348e048f 23437
6721b2ec 23438 gdb_assert (per_cu->cu != NULL);
348e048f
DE
23439}
23440
dee91e82
DE
23441/* die_reader_func for read_signatured_type.
23442 This is identical to load_full_comp_unit_reader,
23443 but is kept separate for now. */
348e048f
DE
23444
23445static void
dee91e82 23446read_signatured_type_reader (const struct die_reader_specs *reader,
d521ce57 23447 const gdb_byte *info_ptr,
dee91e82
DE
23448 struct die_info *comp_unit_die,
23449 int has_children,
23450 void *data)
348e048f 23451{
dee91e82 23452 struct dwarf2_cu *cu = reader->cu;
348e048f 23453
dee91e82
DE
23454 gdb_assert (cu->die_hash == NULL);
23455 cu->die_hash =
23456 htab_create_alloc_ex (cu->header.length / 12,
23457 die_hash,
23458 die_eq,
23459 NULL,
23460 &cu->comp_unit_obstack,
23461 hashtab_obstack_allocate,
23462 dummy_obstack_deallocate);
348e048f 23463
dee91e82
DE
23464 if (has_children)
23465 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
23466 &info_ptr, comp_unit_die);
23467 cu->dies = comp_unit_die;
23468 /* comp_unit_die is not stored in die_hash, no need. */
348e048f
DE
23469
23470 /* We try not to read any attributes in this function, because not
9cdd5dbd 23471 all CUs needed for references have been loaded yet, and symbol
348e048f 23472 table processing isn't initialized. But we have to set the CU language,
dee91e82
DE
23473 or we won't be able to build types correctly.
23474 Similarly, if we do not read the producer, we can not apply
23475 producer-specific interpretation. */
95554aad 23476 prepare_one_comp_unit (cu, cu->dies, language_minimal);
dee91e82 23477}
348e048f 23478
3019eac3
DE
23479/* Read in a signatured type and build its CU and DIEs.
23480 If the type is a stub for the real type in a DWO file,
23481 read in the real type from the DWO file as well. */
dee91e82
DE
23482
23483static void
23484read_signatured_type (struct signatured_type *sig_type)
23485{
23486 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
348e048f 23487
3019eac3 23488 gdb_assert (per_cu->is_debug_types);
dee91e82 23489 gdb_assert (per_cu->cu == NULL);
348e048f 23490
f4dc4d17
DE
23491 init_cutu_and_read_dies (per_cu, NULL, 0, 1,
23492 read_signatured_type_reader, NULL);
7ee85ab1 23493 sig_type->per_cu.tu_read = 1;
c906108c
SS
23494}
23495
c906108c
SS
23496/* Decode simple location descriptions.
23497 Given a pointer to a dwarf block that defines a location, compute
23498 the location and return the value.
23499
4cecd739
DJ
23500 NOTE drow/2003-11-18: This function is called in two situations
23501 now: for the address of static or global variables (partial symbols
23502 only) and for offsets into structures which are expected to be
23503 (more or less) constant. The partial symbol case should go away,
23504 and only the constant case should remain. That will let this
23505 function complain more accurately. A few special modes are allowed
23506 without complaint for global variables (for instance, global
23507 register values and thread-local values).
c906108c
SS
23508
23509 A location description containing no operations indicates that the
4cecd739 23510 object is optimized out. The return value is 0 for that case.
6b992462
DJ
23511 FIXME drow/2003-11-16: No callers check for this case any more; soon all
23512 callers will only want a very basic result and this can become a
21ae7a4d
JK
23513 complaint.
23514
23515 Note that stack[0] is unused except as a default error return. */
c906108c
SS
23516
23517static CORE_ADDR
e7c27a73 23518decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
c906108c 23519{
518817b3 23520 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
56eb65bd
SP
23521 size_t i;
23522 size_t size = blk->size;
d521ce57 23523 const gdb_byte *data = blk->data;
21ae7a4d
JK
23524 CORE_ADDR stack[64];
23525 int stacki;
23526 unsigned int bytes_read, unsnd;
23527 gdb_byte op;
c906108c 23528
21ae7a4d
JK
23529 i = 0;
23530 stacki = 0;
23531 stack[stacki] = 0;
23532 stack[++stacki] = 0;
23533
23534 while (i < size)
23535 {
23536 op = data[i++];
23537 switch (op)
23538 {
23539 case DW_OP_lit0:
23540 case DW_OP_lit1:
23541 case DW_OP_lit2:
23542 case DW_OP_lit3:
23543 case DW_OP_lit4:
23544 case DW_OP_lit5:
23545 case DW_OP_lit6:
23546 case DW_OP_lit7:
23547 case DW_OP_lit8:
23548 case DW_OP_lit9:
23549 case DW_OP_lit10:
23550 case DW_OP_lit11:
23551 case DW_OP_lit12:
23552 case DW_OP_lit13:
23553 case DW_OP_lit14:
23554 case DW_OP_lit15:
23555 case DW_OP_lit16:
23556 case DW_OP_lit17:
23557 case DW_OP_lit18:
23558 case DW_OP_lit19:
23559 case DW_OP_lit20:
23560 case DW_OP_lit21:
23561 case DW_OP_lit22:
23562 case DW_OP_lit23:
23563 case DW_OP_lit24:
23564 case DW_OP_lit25:
23565 case DW_OP_lit26:
23566 case DW_OP_lit27:
23567 case DW_OP_lit28:
23568 case DW_OP_lit29:
23569 case DW_OP_lit30:
23570 case DW_OP_lit31:
23571 stack[++stacki] = op - DW_OP_lit0;
23572 break;
f1bea926 23573
21ae7a4d
JK
23574 case DW_OP_reg0:
23575 case DW_OP_reg1:
23576 case DW_OP_reg2:
23577 case DW_OP_reg3:
23578 case DW_OP_reg4:
23579 case DW_OP_reg5:
23580 case DW_OP_reg6:
23581 case DW_OP_reg7:
23582 case DW_OP_reg8:
23583 case DW_OP_reg9:
23584 case DW_OP_reg10:
23585 case DW_OP_reg11:
23586 case DW_OP_reg12:
23587 case DW_OP_reg13:
23588 case DW_OP_reg14:
23589 case DW_OP_reg15:
23590 case DW_OP_reg16:
23591 case DW_OP_reg17:
23592 case DW_OP_reg18:
23593 case DW_OP_reg19:
23594 case DW_OP_reg20:
23595 case DW_OP_reg21:
23596 case DW_OP_reg22:
23597 case DW_OP_reg23:
23598 case DW_OP_reg24:
23599 case DW_OP_reg25:
23600 case DW_OP_reg26:
23601 case DW_OP_reg27:
23602 case DW_OP_reg28:
23603 case DW_OP_reg29:
23604 case DW_OP_reg30:
23605 case DW_OP_reg31:
23606 stack[++stacki] = op - DW_OP_reg0;
23607 if (i < size)
23608 dwarf2_complex_location_expr_complaint ();
23609 break;
c906108c 23610
21ae7a4d
JK
23611 case DW_OP_regx:
23612 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
23613 i += bytes_read;
23614 stack[++stacki] = unsnd;
23615 if (i < size)
23616 dwarf2_complex_location_expr_complaint ();
23617 break;
c906108c 23618
21ae7a4d
JK
23619 case DW_OP_addr:
23620 stack[++stacki] = read_address (objfile->obfd, &data[i],
23621 cu, &bytes_read);
23622 i += bytes_read;
23623 break;
d53d4ac5 23624
21ae7a4d
JK
23625 case DW_OP_const1u:
23626 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
23627 i += 1;
23628 break;
23629
23630 case DW_OP_const1s:
23631 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
23632 i += 1;
23633 break;
23634
23635 case DW_OP_const2u:
23636 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
23637 i += 2;
23638 break;
23639
23640 case DW_OP_const2s:
23641 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
23642 i += 2;
23643 break;
d53d4ac5 23644
21ae7a4d
JK
23645 case DW_OP_const4u:
23646 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
23647 i += 4;
23648 break;
23649
23650 case DW_OP_const4s:
23651 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
23652 i += 4;
23653 break;
23654
585861ea
JK
23655 case DW_OP_const8u:
23656 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
23657 i += 8;
23658 break;
23659
21ae7a4d
JK
23660 case DW_OP_constu:
23661 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
23662 &bytes_read);
23663 i += bytes_read;
23664 break;
23665
23666 case DW_OP_consts:
23667 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
23668 i += bytes_read;
23669 break;
23670
23671 case DW_OP_dup:
23672 stack[stacki + 1] = stack[stacki];
23673 stacki++;
23674 break;
23675
23676 case DW_OP_plus:
23677 stack[stacki - 1] += stack[stacki];
23678 stacki--;
23679 break;
23680
23681 case DW_OP_plus_uconst:
23682 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
23683 &bytes_read);
23684 i += bytes_read;
23685 break;
23686
23687 case DW_OP_minus:
23688 stack[stacki - 1] -= stack[stacki];
23689 stacki--;
23690 break;
23691
23692 case DW_OP_deref:
23693 /* If we're not the last op, then we definitely can't encode
23694 this using GDB's address_class enum. This is valid for partial
23695 global symbols, although the variable's address will be bogus
23696 in the psymtab. */
23697 if (i < size)
23698 dwarf2_complex_location_expr_complaint ();
23699 break;
23700
23701 case DW_OP_GNU_push_tls_address:
4aa4e28b 23702 case DW_OP_form_tls_address:
21ae7a4d
JK
23703 /* The top of the stack has the offset from the beginning
23704 of the thread control block at which the variable is located. */
23705 /* Nothing should follow this operator, so the top of stack would
23706 be returned. */
23707 /* This is valid for partial global symbols, but the variable's
585861ea
JK
23708 address will be bogus in the psymtab. Make it always at least
23709 non-zero to not look as a variable garbage collected by linker
23710 which have DW_OP_addr 0. */
21ae7a4d
JK
23711 if (i < size)
23712 dwarf2_complex_location_expr_complaint ();
585861ea 23713 stack[stacki]++;
21ae7a4d
JK
23714 break;
23715
23716 case DW_OP_GNU_uninit:
23717 break;
23718
3019eac3 23719 case DW_OP_GNU_addr_index:
49f6c839 23720 case DW_OP_GNU_const_index:
3019eac3
DE
23721 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
23722 &bytes_read);
23723 i += bytes_read;
23724 break;
23725
21ae7a4d
JK
23726 default:
23727 {
f39c6ffd 23728 const char *name = get_DW_OP_name (op);
21ae7a4d
JK
23729
23730 if (name)
23731 complaint (&symfile_complaints, _("unsupported stack op: '%s'"),
23732 name);
23733 else
23734 complaint (&symfile_complaints, _("unsupported stack op: '%02x'"),
23735 op);
23736 }
23737
23738 return (stack[stacki]);
d53d4ac5 23739 }
3c6e0cb3 23740
21ae7a4d
JK
23741 /* Enforce maximum stack depth of SIZE-1 to avoid writing
23742 outside of the allocated space. Also enforce minimum>0. */
23743 if (stacki >= ARRAY_SIZE (stack) - 1)
23744 {
23745 complaint (&symfile_complaints,
23746 _("location description stack overflow"));
23747 return 0;
23748 }
23749
23750 if (stacki <= 0)
23751 {
23752 complaint (&symfile_complaints,
23753 _("location description stack underflow"));
23754 return 0;
23755 }
23756 }
23757 return (stack[stacki]);
c906108c
SS
23758}
23759
23760/* memory allocation interface */
23761
c906108c 23762static struct dwarf_block *
7b5a2f43 23763dwarf_alloc_block (struct dwarf2_cu *cu)
c906108c 23764{
8d749320 23765 return XOBNEW (&cu->comp_unit_obstack, struct dwarf_block);
c906108c
SS
23766}
23767
c906108c 23768static struct die_info *
b60c80d6 23769dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
c906108c
SS
23770{
23771 struct die_info *die;
b60c80d6
DJ
23772 size_t size = sizeof (struct die_info);
23773
23774 if (num_attrs > 1)
23775 size += (num_attrs - 1) * sizeof (struct attribute);
c906108c 23776
b60c80d6 23777 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
c906108c
SS
23778 memset (die, 0, sizeof (struct die_info));
23779 return (die);
23780}
2e276125
JB
23781
23782\f
23783/* Macro support. */
23784
233d95b5
JK
23785/* Return file name relative to the compilation directory of file number I in
23786 *LH's file name table. The result is allocated using xmalloc; the caller is
2e276125 23787 responsible for freeing it. */
233d95b5 23788
2e276125 23789static char *
233d95b5 23790file_file_name (int file, struct line_header *lh)
2e276125 23791{
6a83a1e6
EZ
23792 /* Is the file number a valid index into the line header's file name
23793 table? Remember that file numbers start with one, not zero. */
fff8551c 23794 if (1 <= file && file <= lh->file_names.size ())
6a83a1e6 23795 {
8c43009f 23796 const file_entry &fe = lh->file_names[file - 1];
6e70227d 23797
8c43009f
PA
23798 if (!IS_ABSOLUTE_PATH (fe.name))
23799 {
23800 const char *dir = fe.include_dir (lh);
23801 if (dir != NULL)
23802 return concat (dir, SLASH_STRING, fe.name, (char *) NULL);
23803 }
23804 return xstrdup (fe.name);
6a83a1e6 23805 }
2e276125
JB
23806 else
23807 {
6a83a1e6
EZ
23808 /* The compiler produced a bogus file number. We can at least
23809 record the macro definitions made in the file, even if we
23810 won't be able to find the file by name. */
23811 char fake_name[80];
9a619af0 23812
8c042590
PM
23813 xsnprintf (fake_name, sizeof (fake_name),
23814 "<bad macro file number %d>", file);
2e276125 23815
6e70227d 23816 complaint (&symfile_complaints,
6a83a1e6
EZ
23817 _("bad file number in macro information (%d)"),
23818 file);
2e276125 23819
6a83a1e6 23820 return xstrdup (fake_name);
2e276125
JB
23821 }
23822}
23823
233d95b5
JK
23824/* Return the full name of file number I in *LH's file name table.
23825 Use COMP_DIR as the name of the current directory of the
23826 compilation. The result is allocated using xmalloc; the caller is
23827 responsible for freeing it. */
23828static char *
23829file_full_name (int file, struct line_header *lh, const char *comp_dir)
23830{
23831 /* Is the file number a valid index into the line header's file name
23832 table? Remember that file numbers start with one, not zero. */
fff8551c 23833 if (1 <= file && file <= lh->file_names.size ())
233d95b5
JK
23834 {
23835 char *relative = file_file_name (file, lh);
23836
23837 if (IS_ABSOLUTE_PATH (relative) || comp_dir == NULL)
23838 return relative;
b36cec19
PA
23839 return reconcat (relative, comp_dir, SLASH_STRING,
23840 relative, (char *) NULL);
233d95b5
JK
23841 }
23842 else
23843 return file_file_name (file, lh);
23844}
23845
2e276125
JB
23846
23847static struct macro_source_file *
23848macro_start_file (int file, int line,
23849 struct macro_source_file *current_file,
43f3e411 23850 struct line_header *lh)
2e276125 23851{
233d95b5
JK
23852 /* File name relative to the compilation directory of this source file. */
23853 char *file_name = file_file_name (file, lh);
2e276125 23854
2e276125 23855 if (! current_file)
abc9d0dc 23856 {
fc474241
DE
23857 /* Note: We don't create a macro table for this compilation unit
23858 at all until we actually get a filename. */
43f3e411 23859 struct macro_table *macro_table = get_macro_table ();
fc474241 23860
abc9d0dc
TT
23861 /* If we have no current file, then this must be the start_file
23862 directive for the compilation unit's main source file. */
fc474241
DE
23863 current_file = macro_set_main (macro_table, file_name);
23864 macro_define_special (macro_table);
abc9d0dc 23865 }
2e276125 23866 else
233d95b5 23867 current_file = macro_include (current_file, line, file_name);
2e276125 23868
233d95b5 23869 xfree (file_name);
6e70227d 23870
2e276125
JB
23871 return current_file;
23872}
23873
2e276125
JB
23874static const char *
23875consume_improper_spaces (const char *p, const char *body)
23876{
23877 if (*p == ' ')
23878 {
4d3c2250 23879 complaint (&symfile_complaints,
3e43a32a
MS
23880 _("macro definition contains spaces "
23881 "in formal argument list:\n`%s'"),
4d3c2250 23882 body);
2e276125
JB
23883
23884 while (*p == ' ')
23885 p++;
23886 }
23887
23888 return p;
23889}
23890
23891
23892static void
23893parse_macro_definition (struct macro_source_file *file, int line,
23894 const char *body)
23895{
23896 const char *p;
23897
23898 /* The body string takes one of two forms. For object-like macro
23899 definitions, it should be:
23900
23901 <macro name> " " <definition>
23902
23903 For function-like macro definitions, it should be:
23904
23905 <macro name> "() " <definition>
23906 or
23907 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
23908
23909 Spaces may appear only where explicitly indicated, and in the
23910 <definition>.
23911
23912 The Dwarf 2 spec says that an object-like macro's name is always
23913 followed by a space, but versions of GCC around March 2002 omit
6e70227d 23914 the space when the macro's definition is the empty string.
2e276125
JB
23915
23916 The Dwarf 2 spec says that there should be no spaces between the
23917 formal arguments in a function-like macro's formal argument list,
23918 but versions of GCC around March 2002 include spaces after the
23919 commas. */
23920
23921
23922 /* Find the extent of the macro name. The macro name is terminated
23923 by either a space or null character (for an object-like macro) or
23924 an opening paren (for a function-like macro). */
23925 for (p = body; *p; p++)
23926 if (*p == ' ' || *p == '(')
23927 break;
23928
23929 if (*p == ' ' || *p == '\0')
23930 {
23931 /* It's an object-like macro. */
23932 int name_len = p - body;
3f8a7804 23933 char *name = savestring (body, name_len);
2e276125
JB
23934 const char *replacement;
23935
23936 if (*p == ' ')
23937 replacement = body + name_len + 1;
23938 else
23939 {
4d3c2250 23940 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
23941 replacement = body + name_len;
23942 }
6e70227d 23943
2e276125
JB
23944 macro_define_object (file, line, name, replacement);
23945
23946 xfree (name);
23947 }
23948 else if (*p == '(')
23949 {
23950 /* It's a function-like macro. */
3f8a7804 23951 char *name = savestring (body, p - body);
2e276125
JB
23952 int argc = 0;
23953 int argv_size = 1;
8d749320 23954 char **argv = XNEWVEC (char *, argv_size);
2e276125
JB
23955
23956 p++;
23957
23958 p = consume_improper_spaces (p, body);
23959
23960 /* Parse the formal argument list. */
23961 while (*p && *p != ')')
23962 {
23963 /* Find the extent of the current argument name. */
23964 const char *arg_start = p;
23965
23966 while (*p && *p != ',' && *p != ')' && *p != ' ')
23967 p++;
23968
23969 if (! *p || p == arg_start)
4d3c2250 23970 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
23971 else
23972 {
23973 /* Make sure argv has room for the new argument. */
23974 if (argc >= argv_size)
23975 {
23976 argv_size *= 2;
224c3ddb 23977 argv = XRESIZEVEC (char *, argv, argv_size);
2e276125
JB
23978 }
23979
3f8a7804 23980 argv[argc++] = savestring (arg_start, p - arg_start);
2e276125
JB
23981 }
23982
23983 p = consume_improper_spaces (p, body);
23984
23985 /* Consume the comma, if present. */
23986 if (*p == ',')
23987 {
23988 p++;
23989
23990 p = consume_improper_spaces (p, body);
23991 }
23992 }
23993
23994 if (*p == ')')
23995 {
23996 p++;
23997
23998 if (*p == ' ')
23999 /* Perfectly formed definition, no complaints. */
24000 macro_define_function (file, line, name,
6e70227d 24001 argc, (const char **) argv,
2e276125
JB
24002 p + 1);
24003 else if (*p == '\0')
24004 {
24005 /* Complain, but do define it. */
4d3c2250 24006 dwarf2_macro_malformed_definition_complaint (body);
2e276125 24007 macro_define_function (file, line, name,
6e70227d 24008 argc, (const char **) argv,
2e276125
JB
24009 p);
24010 }
24011 else
24012 /* Just complain. */
4d3c2250 24013 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
24014 }
24015 else
24016 /* Just complain. */
4d3c2250 24017 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
24018
24019 xfree (name);
24020 {
24021 int i;
24022
24023 for (i = 0; i < argc; i++)
24024 xfree (argv[i]);
24025 }
24026 xfree (argv);
24027 }
24028 else
4d3c2250 24029 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
24030}
24031
cf2c3c16
TT
24032/* Skip some bytes from BYTES according to the form given in FORM.
24033 Returns the new pointer. */
2e276125 24034
d521ce57
TT
24035static const gdb_byte *
24036skip_form_bytes (bfd *abfd, const gdb_byte *bytes, const gdb_byte *buffer_end,
cf2c3c16
TT
24037 enum dwarf_form form,
24038 unsigned int offset_size,
24039 struct dwarf2_section_info *section)
2e276125 24040{
cf2c3c16 24041 unsigned int bytes_read;
2e276125 24042
cf2c3c16 24043 switch (form)
2e276125 24044 {
cf2c3c16
TT
24045 case DW_FORM_data1:
24046 case DW_FORM_flag:
24047 ++bytes;
24048 break;
24049
24050 case DW_FORM_data2:
24051 bytes += 2;
24052 break;
24053
24054 case DW_FORM_data4:
24055 bytes += 4;
24056 break;
24057
24058 case DW_FORM_data8:
24059 bytes += 8;
24060 break;
24061
0224619f
JK
24062 case DW_FORM_data16:
24063 bytes += 16;
24064 break;
24065
cf2c3c16
TT
24066 case DW_FORM_string:
24067 read_direct_string (abfd, bytes, &bytes_read);
24068 bytes += bytes_read;
24069 break;
24070
24071 case DW_FORM_sec_offset:
24072 case DW_FORM_strp:
36586728 24073 case DW_FORM_GNU_strp_alt:
cf2c3c16
TT
24074 bytes += offset_size;
24075 break;
24076
24077 case DW_FORM_block:
24078 bytes += read_unsigned_leb128 (abfd, bytes, &bytes_read);
24079 bytes += bytes_read;
24080 break;
24081
24082 case DW_FORM_block1:
24083 bytes += 1 + read_1_byte (abfd, bytes);
24084 break;
24085 case DW_FORM_block2:
24086 bytes += 2 + read_2_bytes (abfd, bytes);
24087 break;
24088 case DW_FORM_block4:
24089 bytes += 4 + read_4_bytes (abfd, bytes);
24090 break;
24091
24092 case DW_FORM_sdata:
24093 case DW_FORM_udata:
3019eac3
DE
24094 case DW_FORM_GNU_addr_index:
24095 case DW_FORM_GNU_str_index:
d521ce57 24096 bytes = gdb_skip_leb128 (bytes, buffer_end);
f664829e
DE
24097 if (bytes == NULL)
24098 {
24099 dwarf2_section_buffer_overflow_complaint (section);
24100 return NULL;
24101 }
cf2c3c16
TT
24102 break;
24103
663c44ac
JK
24104 case DW_FORM_implicit_const:
24105 break;
24106
cf2c3c16
TT
24107 default:
24108 {
cf2c3c16
TT
24109 complaint (&symfile_complaints,
24110 _("invalid form 0x%x in `%s'"),
a32a8923 24111 form, get_section_name (section));
cf2c3c16
TT
24112 return NULL;
24113 }
2e276125
JB
24114 }
24115
cf2c3c16
TT
24116 return bytes;
24117}
757a13d0 24118
cf2c3c16
TT
24119/* A helper for dwarf_decode_macros that handles skipping an unknown
24120 opcode. Returns an updated pointer to the macro data buffer; or,
24121 on error, issues a complaint and returns NULL. */
757a13d0 24122
d521ce57 24123static const gdb_byte *
cf2c3c16 24124skip_unknown_opcode (unsigned int opcode,
d521ce57
TT
24125 const gdb_byte **opcode_definitions,
24126 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
cf2c3c16
TT
24127 bfd *abfd,
24128 unsigned int offset_size,
24129 struct dwarf2_section_info *section)
24130{
24131 unsigned int bytes_read, i;
24132 unsigned long arg;
d521ce57 24133 const gdb_byte *defn;
2e276125 24134
cf2c3c16 24135 if (opcode_definitions[opcode] == NULL)
2e276125 24136 {
cf2c3c16
TT
24137 complaint (&symfile_complaints,
24138 _("unrecognized DW_MACFINO opcode 0x%x"),
24139 opcode);
24140 return NULL;
24141 }
2e276125 24142
cf2c3c16
TT
24143 defn = opcode_definitions[opcode];
24144 arg = read_unsigned_leb128 (abfd, defn, &bytes_read);
24145 defn += bytes_read;
2e276125 24146
cf2c3c16
TT
24147 for (i = 0; i < arg; ++i)
24148 {
aead7601
SM
24149 mac_ptr = skip_form_bytes (abfd, mac_ptr, mac_end,
24150 (enum dwarf_form) defn[i], offset_size,
f664829e 24151 section);
cf2c3c16
TT
24152 if (mac_ptr == NULL)
24153 {
24154 /* skip_form_bytes already issued the complaint. */
24155 return NULL;
24156 }
24157 }
757a13d0 24158
cf2c3c16
TT
24159 return mac_ptr;
24160}
757a13d0 24161
cf2c3c16
TT
24162/* A helper function which parses the header of a macro section.
24163 If the macro section is the extended (for now called "GNU") type,
24164 then this updates *OFFSET_SIZE. Returns a pointer to just after
24165 the header, or issues a complaint and returns NULL on error. */
757a13d0 24166
d521ce57
TT
24167static const gdb_byte *
24168dwarf_parse_macro_header (const gdb_byte **opcode_definitions,
cf2c3c16 24169 bfd *abfd,
d521ce57 24170 const gdb_byte *mac_ptr,
cf2c3c16
TT
24171 unsigned int *offset_size,
24172 int section_is_gnu)
24173{
24174 memset (opcode_definitions, 0, 256 * sizeof (gdb_byte *));
757a13d0 24175
cf2c3c16
TT
24176 if (section_is_gnu)
24177 {
24178 unsigned int version, flags;
757a13d0 24179
cf2c3c16 24180 version = read_2_bytes (abfd, mac_ptr);
0af92d60 24181 if (version != 4 && version != 5)
cf2c3c16
TT
24182 {
24183 complaint (&symfile_complaints,
24184 _("unrecognized version `%d' in .debug_macro section"),
24185 version);
24186 return NULL;
24187 }
24188 mac_ptr += 2;
757a13d0 24189
cf2c3c16
TT
24190 flags = read_1_byte (abfd, mac_ptr);
24191 ++mac_ptr;
24192 *offset_size = (flags & 1) ? 8 : 4;
757a13d0 24193
cf2c3c16
TT
24194 if ((flags & 2) != 0)
24195 /* We don't need the line table offset. */
24196 mac_ptr += *offset_size;
757a13d0 24197
cf2c3c16
TT
24198 /* Vendor opcode descriptions. */
24199 if ((flags & 4) != 0)
24200 {
24201 unsigned int i, count;
757a13d0 24202
cf2c3c16
TT
24203 count = read_1_byte (abfd, mac_ptr);
24204 ++mac_ptr;
24205 for (i = 0; i < count; ++i)
24206 {
24207 unsigned int opcode, bytes_read;
24208 unsigned long arg;
24209
24210 opcode = read_1_byte (abfd, mac_ptr);
24211 ++mac_ptr;
24212 opcode_definitions[opcode] = mac_ptr;
24213 arg = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24214 mac_ptr += bytes_read;
24215 mac_ptr += arg;
24216 }
757a13d0 24217 }
cf2c3c16 24218 }
757a13d0 24219
cf2c3c16
TT
24220 return mac_ptr;
24221}
757a13d0 24222
cf2c3c16 24223/* A helper for dwarf_decode_macros that handles the GNU extensions,
0af92d60 24224 including DW_MACRO_import. */
cf2c3c16
TT
24225
24226static void
ed2dc618
SM
24227dwarf_decode_macro_bytes (struct dwarf2_per_objfile *dwarf2_per_objfile,
24228 bfd *abfd,
d521ce57 24229 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
cf2c3c16 24230 struct macro_source_file *current_file,
43f3e411 24231 struct line_header *lh,
cf2c3c16 24232 struct dwarf2_section_info *section,
36586728 24233 int section_is_gnu, int section_is_dwz,
cf2c3c16 24234 unsigned int offset_size,
8fc3fc34 24235 htab_t include_hash)
cf2c3c16 24236{
4d663531 24237 struct objfile *objfile = dwarf2_per_objfile->objfile;
cf2c3c16
TT
24238 enum dwarf_macro_record_type macinfo_type;
24239 int at_commandline;
d521ce57 24240 const gdb_byte *opcode_definitions[256];
757a13d0 24241
cf2c3c16
TT
24242 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
24243 &offset_size, section_is_gnu);
24244 if (mac_ptr == NULL)
24245 {
24246 /* We already issued a complaint. */
24247 return;
24248 }
757a13d0
JK
24249
24250 /* Determines if GDB is still before first DW_MACINFO_start_file. If true
24251 GDB is still reading the definitions from command line. First
24252 DW_MACINFO_start_file will need to be ignored as it was already executed
24253 to create CURRENT_FILE for the main source holding also the command line
24254 definitions. On first met DW_MACINFO_start_file this flag is reset to
24255 normally execute all the remaining DW_MACINFO_start_file macinfos. */
24256
24257 at_commandline = 1;
24258
24259 do
24260 {
24261 /* Do we at least have room for a macinfo type byte? */
24262 if (mac_ptr >= mac_end)
24263 {
f664829e 24264 dwarf2_section_buffer_overflow_complaint (section);
757a13d0
JK
24265 break;
24266 }
24267
aead7601 24268 macinfo_type = (enum dwarf_macro_record_type) read_1_byte (abfd, mac_ptr);
757a13d0
JK
24269 mac_ptr++;
24270
cf2c3c16
TT
24271 /* Note that we rely on the fact that the corresponding GNU and
24272 DWARF constants are the same. */
132448f8
SM
24273 DIAGNOSTIC_PUSH
24274 DIAGNOSTIC_IGNORE_SWITCH_DIFFERENT_ENUM_TYPES
757a13d0
JK
24275 switch (macinfo_type)
24276 {
24277 /* A zero macinfo type indicates the end of the macro
24278 information. */
24279 case 0:
24280 break;
2e276125 24281
0af92d60
JK
24282 case DW_MACRO_define:
24283 case DW_MACRO_undef:
24284 case DW_MACRO_define_strp:
24285 case DW_MACRO_undef_strp:
24286 case DW_MACRO_define_sup:
24287 case DW_MACRO_undef_sup:
2e276125 24288 {
891d2f0b 24289 unsigned int bytes_read;
2e276125 24290 int line;
d521ce57 24291 const char *body;
cf2c3c16 24292 int is_define;
2e276125 24293
cf2c3c16
TT
24294 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24295 mac_ptr += bytes_read;
24296
0af92d60
JK
24297 if (macinfo_type == DW_MACRO_define
24298 || macinfo_type == DW_MACRO_undef)
cf2c3c16
TT
24299 {
24300 body = read_direct_string (abfd, mac_ptr, &bytes_read);
24301 mac_ptr += bytes_read;
24302 }
24303 else
24304 {
24305 LONGEST str_offset;
24306
24307 str_offset = read_offset_1 (abfd, mac_ptr, offset_size);
24308 mac_ptr += offset_size;
2e276125 24309
0af92d60
JK
24310 if (macinfo_type == DW_MACRO_define_sup
24311 || macinfo_type == DW_MACRO_undef_sup
f7a35f02 24312 || section_is_dwz)
36586728 24313 {
ed2dc618
SM
24314 struct dwz_file *dwz
24315 = dwarf2_get_dwz_file (dwarf2_per_objfile);
36586728 24316
ed2dc618
SM
24317 body = read_indirect_string_from_dwz (objfile,
24318 dwz, str_offset);
36586728
TT
24319 }
24320 else
ed2dc618
SM
24321 body = read_indirect_string_at_offset (dwarf2_per_objfile,
24322 abfd, str_offset);
cf2c3c16
TT
24323 }
24324
0af92d60
JK
24325 is_define = (macinfo_type == DW_MACRO_define
24326 || macinfo_type == DW_MACRO_define_strp
24327 || macinfo_type == DW_MACRO_define_sup);
2e276125 24328 if (! current_file)
757a13d0
JK
24329 {
24330 /* DWARF violation as no main source is present. */
24331 complaint (&symfile_complaints,
24332 _("debug info with no main source gives macro %s "
24333 "on line %d: %s"),
cf2c3c16
TT
24334 is_define ? _("definition") : _("undefinition"),
24335 line, body);
757a13d0
JK
24336 break;
24337 }
3e43a32a
MS
24338 if ((line == 0 && !at_commandline)
24339 || (line != 0 && at_commandline))
4d3c2250 24340 complaint (&symfile_complaints,
757a13d0
JK
24341 _("debug info gives %s macro %s with %s line %d: %s"),
24342 at_commandline ? _("command-line") : _("in-file"),
cf2c3c16 24343 is_define ? _("definition") : _("undefinition"),
757a13d0
JK
24344 line == 0 ? _("zero") : _("non-zero"), line, body);
24345
cf2c3c16 24346 if (is_define)
757a13d0 24347 parse_macro_definition (current_file, line, body);
cf2c3c16
TT
24348 else
24349 {
0af92d60
JK
24350 gdb_assert (macinfo_type == DW_MACRO_undef
24351 || macinfo_type == DW_MACRO_undef_strp
24352 || macinfo_type == DW_MACRO_undef_sup);
cf2c3c16
TT
24353 macro_undef (current_file, line, body);
24354 }
2e276125
JB
24355 }
24356 break;
24357
0af92d60 24358 case DW_MACRO_start_file:
2e276125 24359 {
891d2f0b 24360 unsigned int bytes_read;
2e276125
JB
24361 int line, file;
24362
24363 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24364 mac_ptr += bytes_read;
24365 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24366 mac_ptr += bytes_read;
24367
3e43a32a
MS
24368 if ((line == 0 && !at_commandline)
24369 || (line != 0 && at_commandline))
757a13d0
JK
24370 complaint (&symfile_complaints,
24371 _("debug info gives source %d included "
24372 "from %s at %s line %d"),
24373 file, at_commandline ? _("command-line") : _("file"),
24374 line == 0 ? _("zero") : _("non-zero"), line);
24375
24376 if (at_commandline)
24377 {
0af92d60 24378 /* This DW_MACRO_start_file was executed in the
cf2c3c16 24379 pass one. */
757a13d0
JK
24380 at_commandline = 0;
24381 }
24382 else
43f3e411 24383 current_file = macro_start_file (file, line, current_file, lh);
2e276125
JB
24384 }
24385 break;
24386
0af92d60 24387 case DW_MACRO_end_file:
2e276125 24388 if (! current_file)
4d3c2250 24389 complaint (&symfile_complaints,
3e43a32a
MS
24390 _("macro debug info has an unmatched "
24391 "`close_file' directive"));
2e276125
JB
24392 else
24393 {
24394 current_file = current_file->included_by;
24395 if (! current_file)
24396 {
cf2c3c16 24397 enum dwarf_macro_record_type next_type;
2e276125
JB
24398
24399 /* GCC circa March 2002 doesn't produce the zero
24400 type byte marking the end of the compilation
24401 unit. Complain if it's not there, but exit no
24402 matter what. */
24403
24404 /* Do we at least have room for a macinfo type byte? */
24405 if (mac_ptr >= mac_end)
24406 {
f664829e 24407 dwarf2_section_buffer_overflow_complaint (section);
2e276125
JB
24408 return;
24409 }
24410
24411 /* We don't increment mac_ptr here, so this is just
24412 a look-ahead. */
aead7601
SM
24413 next_type
24414 = (enum dwarf_macro_record_type) read_1_byte (abfd,
24415 mac_ptr);
2e276125 24416 if (next_type != 0)
4d3c2250 24417 complaint (&symfile_complaints,
3e43a32a
MS
24418 _("no terminating 0-type entry for "
24419 "macros in `.debug_macinfo' section"));
2e276125
JB
24420
24421 return;
24422 }
24423 }
24424 break;
24425
0af92d60
JK
24426 case DW_MACRO_import:
24427 case DW_MACRO_import_sup:
cf2c3c16
TT
24428 {
24429 LONGEST offset;
8fc3fc34 24430 void **slot;
a036ba48
TT
24431 bfd *include_bfd = abfd;
24432 struct dwarf2_section_info *include_section = section;
d521ce57 24433 const gdb_byte *include_mac_end = mac_end;
a036ba48 24434 int is_dwz = section_is_dwz;
d521ce57 24435 const gdb_byte *new_mac_ptr;
cf2c3c16
TT
24436
24437 offset = read_offset_1 (abfd, mac_ptr, offset_size);
24438 mac_ptr += offset_size;
24439
0af92d60 24440 if (macinfo_type == DW_MACRO_import_sup)
a036ba48 24441 {
ed2dc618 24442 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
a036ba48 24443
4d663531 24444 dwarf2_read_section (objfile, &dwz->macro);
a036ba48 24445
a036ba48 24446 include_section = &dwz->macro;
a32a8923 24447 include_bfd = get_section_bfd_owner (include_section);
a036ba48
TT
24448 include_mac_end = dwz->macro.buffer + dwz->macro.size;
24449 is_dwz = 1;
24450 }
24451
24452 new_mac_ptr = include_section->buffer + offset;
24453 slot = htab_find_slot (include_hash, new_mac_ptr, INSERT);
24454
8fc3fc34
TT
24455 if (*slot != NULL)
24456 {
24457 /* This has actually happened; see
24458 http://sourceware.org/bugzilla/show_bug.cgi?id=13568. */
24459 complaint (&symfile_complaints,
0af92d60 24460 _("recursive DW_MACRO_import in "
8fc3fc34
TT
24461 ".debug_macro section"));
24462 }
24463 else
24464 {
d521ce57 24465 *slot = (void *) new_mac_ptr;
36586728 24466
ed2dc618
SM
24467 dwarf_decode_macro_bytes (dwarf2_per_objfile,
24468 include_bfd, new_mac_ptr,
43f3e411 24469 include_mac_end, current_file, lh,
36586728 24470 section, section_is_gnu, is_dwz,
4d663531 24471 offset_size, include_hash);
8fc3fc34 24472
d521ce57 24473 htab_remove_elt (include_hash, (void *) new_mac_ptr);
8fc3fc34 24474 }
cf2c3c16
TT
24475 }
24476 break;
24477
2e276125 24478 case DW_MACINFO_vendor_ext:
cf2c3c16
TT
24479 if (!section_is_gnu)
24480 {
24481 unsigned int bytes_read;
2e276125 24482
ac298888
TT
24483 /* This reads the constant, but since we don't recognize
24484 any vendor extensions, we ignore it. */
24485 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
cf2c3c16
TT
24486 mac_ptr += bytes_read;
24487 read_direct_string (abfd, mac_ptr, &bytes_read);
24488 mac_ptr += bytes_read;
2e276125 24489
cf2c3c16
TT
24490 /* We don't recognize any vendor extensions. */
24491 break;
24492 }
24493 /* FALLTHROUGH */
24494
24495 default:
24496 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
f664829e 24497 mac_ptr, mac_end, abfd, offset_size,
cf2c3c16
TT
24498 section);
24499 if (mac_ptr == NULL)
24500 return;
24501 break;
2e276125 24502 }
132448f8 24503 DIAGNOSTIC_POP
757a13d0 24504 } while (macinfo_type != 0);
2e276125 24505}
8e19ed76 24506
cf2c3c16 24507static void
09262596 24508dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
43f3e411 24509 int section_is_gnu)
cf2c3c16 24510{
518817b3
SM
24511 struct dwarf2_per_objfile *dwarf2_per_objfile
24512 = cu->per_cu->dwarf2_per_objfile;
bb5ed363 24513 struct objfile *objfile = dwarf2_per_objfile->objfile;
09262596
DE
24514 struct line_header *lh = cu->line_header;
24515 bfd *abfd;
d521ce57 24516 const gdb_byte *mac_ptr, *mac_end;
cf2c3c16
TT
24517 struct macro_source_file *current_file = 0;
24518 enum dwarf_macro_record_type macinfo_type;
24519 unsigned int offset_size = cu->header.offset_size;
d521ce57 24520 const gdb_byte *opcode_definitions[256];
8fc3fc34 24521 void **slot;
09262596
DE
24522 struct dwarf2_section_info *section;
24523 const char *section_name;
24524
24525 if (cu->dwo_unit != NULL)
24526 {
24527 if (section_is_gnu)
24528 {
24529 section = &cu->dwo_unit->dwo_file->sections.macro;
24530 section_name = ".debug_macro.dwo";
24531 }
24532 else
24533 {
24534 section = &cu->dwo_unit->dwo_file->sections.macinfo;
24535 section_name = ".debug_macinfo.dwo";
24536 }
24537 }
24538 else
24539 {
24540 if (section_is_gnu)
24541 {
24542 section = &dwarf2_per_objfile->macro;
24543 section_name = ".debug_macro";
24544 }
24545 else
24546 {
24547 section = &dwarf2_per_objfile->macinfo;
24548 section_name = ".debug_macinfo";
24549 }
24550 }
cf2c3c16 24551
bb5ed363 24552 dwarf2_read_section (objfile, section);
cf2c3c16
TT
24553 if (section->buffer == NULL)
24554 {
fceca515 24555 complaint (&symfile_complaints, _("missing %s section"), section_name);
cf2c3c16
TT
24556 return;
24557 }
a32a8923 24558 abfd = get_section_bfd_owner (section);
cf2c3c16
TT
24559
24560 /* First pass: Find the name of the base filename.
24561 This filename is needed in order to process all macros whose definition
24562 (or undefinition) comes from the command line. These macros are defined
24563 before the first DW_MACINFO_start_file entry, and yet still need to be
24564 associated to the base file.
24565
24566 To determine the base file name, we scan the macro definitions until we
24567 reach the first DW_MACINFO_start_file entry. We then initialize
24568 CURRENT_FILE accordingly so that any macro definition found before the
24569 first DW_MACINFO_start_file can still be associated to the base file. */
24570
24571 mac_ptr = section->buffer + offset;
24572 mac_end = section->buffer + section->size;
24573
24574 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
24575 &offset_size, section_is_gnu);
24576 if (mac_ptr == NULL)
24577 {
24578 /* We already issued a complaint. */
24579 return;
24580 }
24581
24582 do
24583 {
24584 /* Do we at least have room for a macinfo type byte? */
24585 if (mac_ptr >= mac_end)
24586 {
24587 /* Complaint is printed during the second pass as GDB will probably
24588 stop the first pass earlier upon finding
24589 DW_MACINFO_start_file. */
24590 break;
24591 }
24592
aead7601 24593 macinfo_type = (enum dwarf_macro_record_type) read_1_byte (abfd, mac_ptr);
cf2c3c16
TT
24594 mac_ptr++;
24595
24596 /* Note that we rely on the fact that the corresponding GNU and
24597 DWARF constants are the same. */
132448f8
SM
24598 DIAGNOSTIC_PUSH
24599 DIAGNOSTIC_IGNORE_SWITCH_DIFFERENT_ENUM_TYPES
cf2c3c16
TT
24600 switch (macinfo_type)
24601 {
24602 /* A zero macinfo type indicates the end of the macro
24603 information. */
24604 case 0:
24605 break;
24606
0af92d60
JK
24607 case DW_MACRO_define:
24608 case DW_MACRO_undef:
cf2c3c16
TT
24609 /* Only skip the data by MAC_PTR. */
24610 {
24611 unsigned int bytes_read;
24612
24613 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24614 mac_ptr += bytes_read;
24615 read_direct_string (abfd, mac_ptr, &bytes_read);
24616 mac_ptr += bytes_read;
24617 }
24618 break;
24619
0af92d60 24620 case DW_MACRO_start_file:
cf2c3c16
TT
24621 {
24622 unsigned int bytes_read;
24623 int line, file;
24624
24625 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24626 mac_ptr += bytes_read;
24627 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24628 mac_ptr += bytes_read;
24629
43f3e411 24630 current_file = macro_start_file (file, line, current_file, lh);
cf2c3c16
TT
24631 }
24632 break;
24633
0af92d60 24634 case DW_MACRO_end_file:
cf2c3c16
TT
24635 /* No data to skip by MAC_PTR. */
24636 break;
24637
0af92d60
JK
24638 case DW_MACRO_define_strp:
24639 case DW_MACRO_undef_strp:
24640 case DW_MACRO_define_sup:
24641 case DW_MACRO_undef_sup:
cf2c3c16
TT
24642 {
24643 unsigned int bytes_read;
24644
24645 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24646 mac_ptr += bytes_read;
24647 mac_ptr += offset_size;
24648 }
24649 break;
24650
0af92d60
JK
24651 case DW_MACRO_import:
24652 case DW_MACRO_import_sup:
cf2c3c16 24653 /* Note that, according to the spec, a transparent include
0af92d60 24654 chain cannot call DW_MACRO_start_file. So, we can just
cf2c3c16
TT
24655 skip this opcode. */
24656 mac_ptr += offset_size;
24657 break;
24658
24659 case DW_MACINFO_vendor_ext:
24660 /* Only skip the data by MAC_PTR. */
24661 if (!section_is_gnu)
24662 {
24663 unsigned int bytes_read;
24664
24665 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24666 mac_ptr += bytes_read;
24667 read_direct_string (abfd, mac_ptr, &bytes_read);
24668 mac_ptr += bytes_read;
24669 }
24670 /* FALLTHROUGH */
24671
24672 default:
24673 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
f664829e 24674 mac_ptr, mac_end, abfd, offset_size,
cf2c3c16
TT
24675 section);
24676 if (mac_ptr == NULL)
24677 return;
24678 break;
24679 }
132448f8 24680 DIAGNOSTIC_POP
cf2c3c16
TT
24681 } while (macinfo_type != 0 && current_file == NULL);
24682
24683 /* Second pass: Process all entries.
24684
24685 Use the AT_COMMAND_LINE flag to determine whether we are still processing
24686 command-line macro definitions/undefinitions. This flag is unset when we
24687 reach the first DW_MACINFO_start_file entry. */
24688
fc4007c9
TT
24689 htab_up include_hash (htab_create_alloc (1, htab_hash_pointer,
24690 htab_eq_pointer,
24691 NULL, xcalloc, xfree));
8fc3fc34 24692 mac_ptr = section->buffer + offset;
fc4007c9 24693 slot = htab_find_slot (include_hash.get (), mac_ptr, INSERT);
d521ce57 24694 *slot = (void *) mac_ptr;
ed2dc618
SM
24695 dwarf_decode_macro_bytes (dwarf2_per_objfile,
24696 abfd, mac_ptr, mac_end,
43f3e411 24697 current_file, lh, section,
fc4007c9
TT
24698 section_is_gnu, 0, offset_size,
24699 include_hash.get ());
cf2c3c16
TT
24700}
24701
8e19ed76 24702/* Check if the attribute's form is a DW_FORM_block*
0963b4bd 24703 if so return true else false. */
380bca97 24704
8e19ed76 24705static int
6e5a29e1 24706attr_form_is_block (const struct attribute *attr)
8e19ed76
PS
24707{
24708 return (attr == NULL ? 0 :
24709 attr->form == DW_FORM_block1
24710 || attr->form == DW_FORM_block2
24711 || attr->form == DW_FORM_block4
2dc7f7b3
TT
24712 || attr->form == DW_FORM_block
24713 || attr->form == DW_FORM_exprloc);
8e19ed76 24714}
4c2df51b 24715
c6a0999f
JB
24716/* Return non-zero if ATTR's value is a section offset --- classes
24717 lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
24718 You may use DW_UNSND (attr) to retrieve such offsets.
24719
24720 Section 7.5.4, "Attribute Encodings", explains that no attribute
24721 may have a value that belongs to more than one of these classes; it
24722 would be ambiguous if we did, because we use the same forms for all
24723 of them. */
380bca97 24724
3690dd37 24725static int
6e5a29e1 24726attr_form_is_section_offset (const struct attribute *attr)
3690dd37
JB
24727{
24728 return (attr->form == DW_FORM_data4
2dc7f7b3
TT
24729 || attr->form == DW_FORM_data8
24730 || attr->form == DW_FORM_sec_offset);
3690dd37
JB
24731}
24732
3690dd37
JB
24733/* Return non-zero if ATTR's value falls in the 'constant' class, or
24734 zero otherwise. When this function returns true, you can apply
24735 dwarf2_get_attr_constant_value to it.
24736
24737 However, note that for some attributes you must check
24738 attr_form_is_section_offset before using this test. DW_FORM_data4
24739 and DW_FORM_data8 are members of both the constant class, and of
24740 the classes that contain offsets into other debug sections
24741 (lineptr, loclistptr, macptr or rangelistptr). The DWARF spec says
24742 that, if an attribute's can be either a constant or one of the
24743 section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
0224619f
JK
24744 taken as section offsets, not constants.
24745
24746 DW_FORM_data16 is not considered as dwarf2_get_attr_constant_value
24747 cannot handle that. */
380bca97 24748
3690dd37 24749static int
6e5a29e1 24750attr_form_is_constant (const struct attribute *attr)
3690dd37
JB
24751{
24752 switch (attr->form)
24753 {
24754 case DW_FORM_sdata:
24755 case DW_FORM_udata:
24756 case DW_FORM_data1:
24757 case DW_FORM_data2:
24758 case DW_FORM_data4:
24759 case DW_FORM_data8:
663c44ac 24760 case DW_FORM_implicit_const:
3690dd37
JB
24761 return 1;
24762 default:
24763 return 0;
24764 }
24765}
24766
7771576e
SA
24767
24768/* DW_ADDR is always stored already as sect_offset; despite for the forms
24769 besides DW_FORM_ref_addr it is stored as cu_offset in the DWARF file. */
24770
24771static int
6e5a29e1 24772attr_form_is_ref (const struct attribute *attr)
7771576e
SA
24773{
24774 switch (attr->form)
24775 {
24776 case DW_FORM_ref_addr:
24777 case DW_FORM_ref1:
24778 case DW_FORM_ref2:
24779 case DW_FORM_ref4:
24780 case DW_FORM_ref8:
24781 case DW_FORM_ref_udata:
24782 case DW_FORM_GNU_ref_alt:
24783 return 1;
24784 default:
24785 return 0;
24786 }
24787}
24788
3019eac3
DE
24789/* Return the .debug_loc section to use for CU.
24790 For DWO files use .debug_loc.dwo. */
24791
24792static struct dwarf2_section_info *
24793cu_debug_loc_section (struct dwarf2_cu *cu)
24794{
518817b3
SM
24795 struct dwarf2_per_objfile *dwarf2_per_objfile
24796 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 24797
3019eac3 24798 if (cu->dwo_unit)
43988095
JK
24799 {
24800 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
24801
24802 return cu->header.version >= 5 ? &sections->loclists : &sections->loc;
24803 }
24804 return (cu->header.version >= 5 ? &dwarf2_per_objfile->loclists
24805 : &dwarf2_per_objfile->loc);
3019eac3
DE
24806}
24807
8cf6f0b1
TT
24808/* A helper function that fills in a dwarf2_loclist_baton. */
24809
24810static void
24811fill_in_loclist_baton (struct dwarf2_cu *cu,
24812 struct dwarf2_loclist_baton *baton,
ff39bb5e 24813 const struct attribute *attr)
8cf6f0b1 24814{
518817b3
SM
24815 struct dwarf2_per_objfile *dwarf2_per_objfile
24816 = cu->per_cu->dwarf2_per_objfile;
3019eac3
DE
24817 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
24818
24819 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
8cf6f0b1
TT
24820
24821 baton->per_cu = cu->per_cu;
24822 gdb_assert (baton->per_cu);
24823 /* We don't know how long the location list is, but make sure we
24824 don't run off the edge of the section. */
3019eac3
DE
24825 baton->size = section->size - DW_UNSND (attr);
24826 baton->data = section->buffer + DW_UNSND (attr);
8cf6f0b1 24827 baton->base_address = cu->base_address;
f664829e 24828 baton->from_dwo = cu->dwo_unit != NULL;
8cf6f0b1
TT
24829}
24830
4c2df51b 24831static void
ff39bb5e 24832dwarf2_symbol_mark_computed (const struct attribute *attr, struct symbol *sym,
f1e6e072 24833 struct dwarf2_cu *cu, int is_block)
4c2df51b 24834{
518817b3
SM
24835 struct dwarf2_per_objfile *dwarf2_per_objfile
24836 = cu->per_cu->dwarf2_per_objfile;
bb5ed363 24837 struct objfile *objfile = dwarf2_per_objfile->objfile;
3019eac3 24838 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
bb5ed363 24839
3690dd37 24840 if (attr_form_is_section_offset (attr)
3019eac3 24841 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
99bcc461
DJ
24842 the section. If so, fall through to the complaint in the
24843 other branch. */
3019eac3 24844 && DW_UNSND (attr) < dwarf2_section_size (objfile, section))
4c2df51b 24845 {
0d53c4c4 24846 struct dwarf2_loclist_baton *baton;
4c2df51b 24847
8d749320 24848 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_loclist_baton);
4c2df51b 24849
8cf6f0b1 24850 fill_in_loclist_baton (cu, baton, attr);
be391dca 24851
d00adf39 24852 if (cu->base_known == 0)
0d53c4c4 24853 complaint (&symfile_complaints,
3e43a32a
MS
24854 _("Location list used without "
24855 "specifying the CU base address."));
4c2df51b 24856
f1e6e072
TT
24857 SYMBOL_ACLASS_INDEX (sym) = (is_block
24858 ? dwarf2_loclist_block_index
24859 : dwarf2_loclist_index);
0d53c4c4
DJ
24860 SYMBOL_LOCATION_BATON (sym) = baton;
24861 }
24862 else
24863 {
24864 struct dwarf2_locexpr_baton *baton;
24865
8d749320 24866 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
ae0d2f24
UW
24867 baton->per_cu = cu->per_cu;
24868 gdb_assert (baton->per_cu);
0d53c4c4
DJ
24869
24870 if (attr_form_is_block (attr))
24871 {
24872 /* Note that we're just copying the block's data pointer
24873 here, not the actual data. We're still pointing into the
6502dd73
DJ
24874 info_buffer for SYM's objfile; right now we never release
24875 that buffer, but when we do clean up properly this may
24876 need to change. */
0d53c4c4
DJ
24877 baton->size = DW_BLOCK (attr)->size;
24878 baton->data = DW_BLOCK (attr)->data;
24879 }
24880 else
24881 {
24882 dwarf2_invalid_attrib_class_complaint ("location description",
24883 SYMBOL_NATURAL_NAME (sym));
24884 baton->size = 0;
0d53c4c4 24885 }
6e70227d 24886
f1e6e072
TT
24887 SYMBOL_ACLASS_INDEX (sym) = (is_block
24888 ? dwarf2_locexpr_block_index
24889 : dwarf2_locexpr_index);
0d53c4c4
DJ
24890 SYMBOL_LOCATION_BATON (sym) = baton;
24891 }
4c2df51b 24892}
6502dd73 24893
9aa1f1e3
TT
24894/* Return the OBJFILE associated with the compilation unit CU. If CU
24895 came from a separate debuginfo file, then the master objfile is
24896 returned. */
ae0d2f24
UW
24897
24898struct objfile *
24899dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
24900{
e3b94546 24901 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
ae0d2f24
UW
24902
24903 /* Return the master objfile, so that we can report and look up the
24904 correct file containing this variable. */
24905 if (objfile->separate_debug_objfile_backlink)
24906 objfile = objfile->separate_debug_objfile_backlink;
24907
24908 return objfile;
24909}
24910
96408a79
SA
24911/* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
24912 (CU_HEADERP is unused in such case) or prepare a temporary copy at
24913 CU_HEADERP first. */
24914
24915static const struct comp_unit_head *
24916per_cu_header_read_in (struct comp_unit_head *cu_headerp,
24917 struct dwarf2_per_cu_data *per_cu)
24918{
d521ce57 24919 const gdb_byte *info_ptr;
96408a79
SA
24920
24921 if (per_cu->cu)
24922 return &per_cu->cu->header;
24923
9c541725 24924 info_ptr = per_cu->section->buffer + to_underlying (per_cu->sect_off);
96408a79
SA
24925
24926 memset (cu_headerp, 0, sizeof (*cu_headerp));
43988095
JK
24927 read_comp_unit_head (cu_headerp, info_ptr, per_cu->section,
24928 rcuh_kind::COMPILE);
96408a79
SA
24929
24930 return cu_headerp;
24931}
24932
ae0d2f24
UW
24933/* Return the address size given in the compilation unit header for CU. */
24934
98714339 24935int
ae0d2f24
UW
24936dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
24937{
96408a79
SA
24938 struct comp_unit_head cu_header_local;
24939 const struct comp_unit_head *cu_headerp;
c471e790 24940
96408a79
SA
24941 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
24942
24943 return cu_headerp->addr_size;
ae0d2f24
UW
24944}
24945
9eae7c52
TT
24946/* Return the offset size given in the compilation unit header for CU. */
24947
24948int
24949dwarf2_per_cu_offset_size (struct dwarf2_per_cu_data *per_cu)
24950{
96408a79
SA
24951 struct comp_unit_head cu_header_local;
24952 const struct comp_unit_head *cu_headerp;
9c6c53f7 24953
96408a79
SA
24954 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
24955
24956 return cu_headerp->offset_size;
24957}
24958
24959/* See its dwarf2loc.h declaration. */
24960
24961int
24962dwarf2_per_cu_ref_addr_size (struct dwarf2_per_cu_data *per_cu)
24963{
24964 struct comp_unit_head cu_header_local;
24965 const struct comp_unit_head *cu_headerp;
24966
24967 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
24968
24969 if (cu_headerp->version == 2)
24970 return cu_headerp->addr_size;
24971 else
24972 return cu_headerp->offset_size;
181cebd4
JK
24973}
24974
9aa1f1e3
TT
24975/* Return the text offset of the CU. The returned offset comes from
24976 this CU's objfile. If this objfile came from a separate debuginfo
24977 file, then the offset may be different from the corresponding
24978 offset in the parent objfile. */
24979
24980CORE_ADDR
24981dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
24982{
e3b94546 24983 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
9aa1f1e3
TT
24984
24985 return ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
24986}
24987
43988095
JK
24988/* Return DWARF version number of PER_CU. */
24989
24990short
24991dwarf2_version (struct dwarf2_per_cu_data *per_cu)
24992{
24993 return per_cu->dwarf_version;
24994}
24995
348e048f
DE
24996/* Locate the .debug_info compilation unit from CU's objfile which contains
24997 the DIE at OFFSET. Raises an error on failure. */
ae038cb0
DJ
24998
24999static struct dwarf2_per_cu_data *
9c541725 25000dwarf2_find_containing_comp_unit (sect_offset sect_off,
36586728 25001 unsigned int offset_in_dwz,
ed2dc618 25002 struct dwarf2_per_objfile *dwarf2_per_objfile)
ae038cb0
DJ
25003{
25004 struct dwarf2_per_cu_data *this_cu;
25005 int low, high;
36586728 25006 const sect_offset *cu_off;
ae038cb0 25007
ae038cb0
DJ
25008 low = 0;
25009 high = dwarf2_per_objfile->n_comp_units - 1;
25010 while (high > low)
25011 {
36586728 25012 struct dwarf2_per_cu_data *mid_cu;
ae038cb0 25013 int mid = low + (high - low) / 2;
9a619af0 25014
36586728 25015 mid_cu = dwarf2_per_objfile->all_comp_units[mid];
9c541725 25016 cu_off = &mid_cu->sect_off;
36586728 25017 if (mid_cu->is_dwz > offset_in_dwz
9c541725 25018 || (mid_cu->is_dwz == offset_in_dwz && *cu_off >= sect_off))
ae038cb0
DJ
25019 high = mid;
25020 else
25021 low = mid + 1;
25022 }
25023 gdb_assert (low == high);
36586728 25024 this_cu = dwarf2_per_objfile->all_comp_units[low];
9c541725
PA
25025 cu_off = &this_cu->sect_off;
25026 if (this_cu->is_dwz != offset_in_dwz || *cu_off > sect_off)
ae038cb0 25027 {
36586728 25028 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
8a3fe4f8 25029 error (_("Dwarf Error: could not find partial DIE containing "
9d8780f0
SM
25030 "offset %s [in module %s]"),
25031 sect_offset_str (sect_off),
ed2dc618 25032 bfd_get_filename (dwarf2_per_objfile->objfile->obfd));
10b3939b 25033
9c541725
PA
25034 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->sect_off
25035 <= sect_off);
ae038cb0
DJ
25036 return dwarf2_per_objfile->all_comp_units[low-1];
25037 }
25038 else
25039 {
25040 this_cu = dwarf2_per_objfile->all_comp_units[low];
25041 if (low == dwarf2_per_objfile->n_comp_units - 1
9c541725 25042 && sect_off >= this_cu->sect_off + this_cu->length)
9d8780f0 25043 error (_("invalid dwarf2 offset %s"), sect_offset_str (sect_off));
9c541725 25044 gdb_assert (sect_off < this_cu->sect_off + this_cu->length);
ae038cb0
DJ
25045 return this_cu;
25046 }
25047}
25048
23745b47 25049/* Initialize dwarf2_cu CU, owned by PER_CU. */
93311388 25050
fcd3b13d
SM
25051dwarf2_cu::dwarf2_cu (struct dwarf2_per_cu_data *per_cu_)
25052 : per_cu (per_cu_),
25053 mark (0),
25054 has_loclist (0),
25055 checked_producer (0),
25056 producer_is_gxx_lt_4_6 (0),
25057 producer_is_gcc_lt_4_3 (0),
25058 producer_is_icc_lt_14 (0),
25059 processing_has_namespace_info (0)
93311388 25060{
fcd3b13d
SM
25061 per_cu->cu = this;
25062}
25063
25064/* Destroy a dwarf2_cu. */
25065
25066dwarf2_cu::~dwarf2_cu ()
25067{
25068 per_cu->cu = NULL;
9816fde3
JK
25069}
25070
25071/* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
25072
25073static void
95554aad
TT
25074prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
25075 enum language pretend_language)
9816fde3
JK
25076{
25077 struct attribute *attr;
25078
25079 /* Set the language we're debugging. */
25080 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
25081 if (attr)
25082 set_cu_language (DW_UNSND (attr), cu);
25083 else
9cded63f 25084 {
95554aad 25085 cu->language = pretend_language;
9cded63f
TT
25086 cu->language_defn = language_def (cu->language);
25087 }
dee91e82 25088
7d45c7c3 25089 cu->producer = dwarf2_string_attr (comp_unit_die, DW_AT_producer, cu);
93311388
DE
25090}
25091
ae038cb0
DJ
25092/* Increase the age counter on each cached compilation unit, and free
25093 any that are too old. */
25094
25095static void
ed2dc618 25096age_cached_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
ae038cb0
DJ
25097{
25098 struct dwarf2_per_cu_data *per_cu, **last_chain;
25099
25100 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
25101 per_cu = dwarf2_per_objfile->read_in_chain;
25102 while (per_cu != NULL)
25103 {
25104 per_cu->cu->last_used ++;
b4f54984 25105 if (per_cu->cu->last_used <= dwarf_max_cache_age)
ae038cb0
DJ
25106 dwarf2_mark (per_cu->cu);
25107 per_cu = per_cu->cu->read_in_chain;
25108 }
25109
25110 per_cu = dwarf2_per_objfile->read_in_chain;
25111 last_chain = &dwarf2_per_objfile->read_in_chain;
25112 while (per_cu != NULL)
25113 {
25114 struct dwarf2_per_cu_data *next_cu;
25115
25116 next_cu = per_cu->cu->read_in_chain;
25117
25118 if (!per_cu->cu->mark)
25119 {
fcd3b13d 25120 delete per_cu->cu;
ae038cb0
DJ
25121 *last_chain = next_cu;
25122 }
25123 else
25124 last_chain = &per_cu->cu->read_in_chain;
25125
25126 per_cu = next_cu;
25127 }
25128}
25129
25130/* Remove a single compilation unit from the cache. */
25131
25132static void
dee91e82 25133free_one_cached_comp_unit (struct dwarf2_per_cu_data *target_per_cu)
ae038cb0
DJ
25134{
25135 struct dwarf2_per_cu_data *per_cu, **last_chain;
ed2dc618
SM
25136 struct dwarf2_per_objfile *dwarf2_per_objfile
25137 = target_per_cu->dwarf2_per_objfile;
ae038cb0
DJ
25138
25139 per_cu = dwarf2_per_objfile->read_in_chain;
25140 last_chain = &dwarf2_per_objfile->read_in_chain;
25141 while (per_cu != NULL)
25142 {
25143 struct dwarf2_per_cu_data *next_cu;
25144
25145 next_cu = per_cu->cu->read_in_chain;
25146
dee91e82 25147 if (per_cu == target_per_cu)
ae038cb0 25148 {
fcd3b13d 25149 delete per_cu->cu;
dee91e82 25150 per_cu->cu = NULL;
ae038cb0
DJ
25151 *last_chain = next_cu;
25152 break;
25153 }
25154 else
25155 last_chain = &per_cu->cu->read_in_chain;
25156
25157 per_cu = next_cu;
25158 }
25159}
25160
fe3e1990
DJ
25161/* Release all extra memory associated with OBJFILE. */
25162
25163void
25164dwarf2_free_objfile (struct objfile *objfile)
25165{
ed2dc618
SM
25166 struct dwarf2_per_objfile *dwarf2_per_objfile
25167 = get_dwarf2_per_objfile (objfile);
fe3e1990 25168
fd90ace4 25169 delete dwarf2_per_objfile;
fe3e1990
DJ
25170}
25171
dee91e82
DE
25172/* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
25173 We store these in a hash table separate from the DIEs, and preserve them
25174 when the DIEs are flushed out of cache.
25175
25176 The CU "per_cu" pointer is needed because offset alone is not enough to
3019eac3 25177 uniquely identify the type. A file may have multiple .debug_types sections,
c88ee1f0
DE
25178 or the type may come from a DWO file. Furthermore, while it's more logical
25179 to use per_cu->section+offset, with Fission the section with the data is in
25180 the DWO file but we don't know that section at the point we need it.
25181 We have to use something in dwarf2_per_cu_data (or the pointer to it)
25182 because we can enter the lookup routine, get_die_type_at_offset, from
25183 outside this file, and thus won't necessarily have PER_CU->cu.
25184 Fortunately, PER_CU is stable for the life of the objfile. */
1c379e20 25185
dee91e82 25186struct dwarf2_per_cu_offset_and_type
1c379e20 25187{
dee91e82 25188 const struct dwarf2_per_cu_data *per_cu;
9c541725 25189 sect_offset sect_off;
1c379e20
DJ
25190 struct type *type;
25191};
25192
dee91e82 25193/* Hash function for a dwarf2_per_cu_offset_and_type. */
1c379e20
DJ
25194
25195static hashval_t
dee91e82 25196per_cu_offset_and_type_hash (const void *item)
1c379e20 25197{
9a3c8263
SM
25198 const struct dwarf2_per_cu_offset_and_type *ofs
25199 = (const struct dwarf2_per_cu_offset_and_type *) item;
9a619af0 25200
9c541725 25201 return (uintptr_t) ofs->per_cu + to_underlying (ofs->sect_off);
1c379e20
DJ
25202}
25203
dee91e82 25204/* Equality function for a dwarf2_per_cu_offset_and_type. */
1c379e20
DJ
25205
25206static int
dee91e82 25207per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
1c379e20 25208{
9a3c8263
SM
25209 const struct dwarf2_per_cu_offset_and_type *ofs_lhs
25210 = (const struct dwarf2_per_cu_offset_and_type *) item_lhs;
25211 const struct dwarf2_per_cu_offset_and_type *ofs_rhs
25212 = (const struct dwarf2_per_cu_offset_and_type *) item_rhs;
9a619af0 25213
dee91e82 25214 return (ofs_lhs->per_cu == ofs_rhs->per_cu
9c541725 25215 && ofs_lhs->sect_off == ofs_rhs->sect_off);
1c379e20
DJ
25216}
25217
25218/* Set the type associated with DIE to TYPE. Save it in CU's hash
7e314c57
JK
25219 table if necessary. For convenience, return TYPE.
25220
25221 The DIEs reading must have careful ordering to:
25222 * Not cause infite loops trying to read in DIEs as a prerequisite for
25223 reading current DIE.
25224 * Not trying to dereference contents of still incompletely read in types
25225 while reading in other DIEs.
25226 * Enable referencing still incompletely read in types just by a pointer to
25227 the type without accessing its fields.
25228
25229 Therefore caller should follow these rules:
25230 * Try to fetch any prerequisite types we may need to build this DIE type
25231 before building the type and calling set_die_type.
e71ec853 25232 * After building type call set_die_type for current DIE as soon as
7e314c57
JK
25233 possible before fetching more types to complete the current type.
25234 * Make the type as complete as possible before fetching more types. */
1c379e20 25235
f792889a 25236static struct type *
1c379e20
DJ
25237set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
25238{
518817b3
SM
25239 struct dwarf2_per_objfile *dwarf2_per_objfile
25240 = cu->per_cu->dwarf2_per_objfile;
dee91e82 25241 struct dwarf2_per_cu_offset_and_type **slot, ofs;
ed2dc618 25242 struct objfile *objfile = dwarf2_per_objfile->objfile;
3cdcd0ce
JB
25243 struct attribute *attr;
25244 struct dynamic_prop prop;
1c379e20 25245
b4ba55a1
JB
25246 /* For Ada types, make sure that the gnat-specific data is always
25247 initialized (if not already set). There are a few types where
25248 we should not be doing so, because the type-specific area is
25249 already used to hold some other piece of info (eg: TYPE_CODE_FLT
25250 where the type-specific area is used to store the floatformat).
25251 But this is not a problem, because the gnat-specific information
25252 is actually not needed for these types. */
25253 if (need_gnat_info (cu)
25254 && TYPE_CODE (type) != TYPE_CODE_FUNC
25255 && TYPE_CODE (type) != TYPE_CODE_FLT
09e2d7c7
DE
25256 && TYPE_CODE (type) != TYPE_CODE_METHODPTR
25257 && TYPE_CODE (type) != TYPE_CODE_MEMBERPTR
25258 && TYPE_CODE (type) != TYPE_CODE_METHOD
b4ba55a1
JB
25259 && !HAVE_GNAT_AUX_INFO (type))
25260 INIT_GNAT_SPECIFIC (type);
25261
3f2f83dd
KB
25262 /* Read DW_AT_allocated and set in type. */
25263 attr = dwarf2_attr (die, DW_AT_allocated, cu);
25264 if (attr_form_is_block (attr))
25265 {
25266 if (attr_to_dynamic_prop (attr, die, cu, &prop))
50a82047 25267 add_dyn_prop (DYN_PROP_ALLOCATED, prop, type);
3f2f83dd
KB
25268 }
25269 else if (attr != NULL)
25270 {
25271 complaint (&symfile_complaints,
9d8780f0 25272 _("DW_AT_allocated has the wrong form (%s) at DIE %s"),
9c541725 25273 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
9d8780f0 25274 sect_offset_str (die->sect_off));
3f2f83dd
KB
25275 }
25276
25277 /* Read DW_AT_associated and set in type. */
25278 attr = dwarf2_attr (die, DW_AT_associated, cu);
25279 if (attr_form_is_block (attr))
25280 {
25281 if (attr_to_dynamic_prop (attr, die, cu, &prop))
50a82047 25282 add_dyn_prop (DYN_PROP_ASSOCIATED, prop, type);
3f2f83dd
KB
25283 }
25284 else if (attr != NULL)
25285 {
25286 complaint (&symfile_complaints,
9d8780f0 25287 _("DW_AT_associated has the wrong form (%s) at DIE %s"),
9c541725 25288 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
9d8780f0 25289 sect_offset_str (die->sect_off));
3f2f83dd
KB
25290 }
25291
3cdcd0ce
JB
25292 /* Read DW_AT_data_location and set in type. */
25293 attr = dwarf2_attr (die, DW_AT_data_location, cu);
25294 if (attr_to_dynamic_prop (attr, die, cu, &prop))
50a82047 25295 add_dyn_prop (DYN_PROP_DATA_LOCATION, prop, type);
3cdcd0ce 25296
dee91e82 25297 if (dwarf2_per_objfile->die_type_hash == NULL)
f792889a 25298 {
dee91e82
DE
25299 dwarf2_per_objfile->die_type_hash =
25300 htab_create_alloc_ex (127,
25301 per_cu_offset_and_type_hash,
25302 per_cu_offset_and_type_eq,
25303 NULL,
25304 &objfile->objfile_obstack,
25305 hashtab_obstack_allocate,
25306 dummy_obstack_deallocate);
f792889a 25307 }
1c379e20 25308
dee91e82 25309 ofs.per_cu = cu->per_cu;
9c541725 25310 ofs.sect_off = die->sect_off;
1c379e20 25311 ofs.type = type;
dee91e82
DE
25312 slot = (struct dwarf2_per_cu_offset_and_type **)
25313 htab_find_slot (dwarf2_per_objfile->die_type_hash, &ofs, INSERT);
7e314c57
JK
25314 if (*slot)
25315 complaint (&symfile_complaints,
9d8780f0
SM
25316 _("A problem internal to GDB: DIE %s has type already set"),
25317 sect_offset_str (die->sect_off));
8d749320
SM
25318 *slot = XOBNEW (&objfile->objfile_obstack,
25319 struct dwarf2_per_cu_offset_and_type);
1c379e20 25320 **slot = ofs;
f792889a 25321 return type;
1c379e20
DJ
25322}
25323
9c541725 25324/* Look up the type for the die at SECT_OFF in PER_CU in die_type_hash,
02142a6c 25325 or return NULL if the die does not have a saved type. */
1c379e20
DJ
25326
25327static struct type *
9c541725 25328get_die_type_at_offset (sect_offset sect_off,
673bfd45 25329 struct dwarf2_per_cu_data *per_cu)
1c379e20 25330{
dee91e82 25331 struct dwarf2_per_cu_offset_and_type *slot, ofs;
ed2dc618 25332 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
f792889a 25333
dee91e82 25334 if (dwarf2_per_objfile->die_type_hash == NULL)
f792889a 25335 return NULL;
1c379e20 25336
dee91e82 25337 ofs.per_cu = per_cu;
9c541725 25338 ofs.sect_off = sect_off;
9a3c8263
SM
25339 slot = ((struct dwarf2_per_cu_offset_and_type *)
25340 htab_find (dwarf2_per_objfile->die_type_hash, &ofs));
1c379e20
DJ
25341 if (slot)
25342 return slot->type;
25343 else
25344 return NULL;
25345}
25346
02142a6c 25347/* Look up the type for DIE in CU in die_type_hash,
673bfd45
DE
25348 or return NULL if DIE does not have a saved type. */
25349
25350static struct type *
25351get_die_type (struct die_info *die, struct dwarf2_cu *cu)
25352{
9c541725 25353 return get_die_type_at_offset (die->sect_off, cu->per_cu);
673bfd45
DE
25354}
25355
10b3939b
DJ
25356/* Add a dependence relationship from CU to REF_PER_CU. */
25357
25358static void
25359dwarf2_add_dependence (struct dwarf2_cu *cu,
25360 struct dwarf2_per_cu_data *ref_per_cu)
25361{
25362 void **slot;
25363
25364 if (cu->dependencies == NULL)
25365 cu->dependencies
25366 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
25367 NULL, &cu->comp_unit_obstack,
25368 hashtab_obstack_allocate,
25369 dummy_obstack_deallocate);
25370
25371 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
25372 if (*slot == NULL)
25373 *slot = ref_per_cu;
25374}
1c379e20 25375
f504f079
DE
25376/* Subroutine of dwarf2_mark to pass to htab_traverse.
25377 Set the mark field in every compilation unit in the
ae038cb0
DJ
25378 cache that we must keep because we are keeping CU. */
25379
10b3939b
DJ
25380static int
25381dwarf2_mark_helper (void **slot, void *data)
25382{
25383 struct dwarf2_per_cu_data *per_cu;
25384
25385 per_cu = (struct dwarf2_per_cu_data *) *slot;
d07ed419
JK
25386
25387 /* cu->dependencies references may not yet have been ever read if QUIT aborts
25388 reading of the chain. As such dependencies remain valid it is not much
25389 useful to track and undo them during QUIT cleanups. */
25390 if (per_cu->cu == NULL)
25391 return 1;
25392
10b3939b
DJ
25393 if (per_cu->cu->mark)
25394 return 1;
25395 per_cu->cu->mark = 1;
25396
25397 if (per_cu->cu->dependencies != NULL)
25398 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
25399
25400 return 1;
25401}
25402
f504f079
DE
25403/* Set the mark field in CU and in every other compilation unit in the
25404 cache that we must keep because we are keeping CU. */
25405
ae038cb0
DJ
25406static void
25407dwarf2_mark (struct dwarf2_cu *cu)
25408{
25409 if (cu->mark)
25410 return;
25411 cu->mark = 1;
10b3939b
DJ
25412 if (cu->dependencies != NULL)
25413 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
ae038cb0
DJ
25414}
25415
25416static void
25417dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
25418{
25419 while (per_cu)
25420 {
25421 per_cu->cu->mark = 0;
25422 per_cu = per_cu->cu->read_in_chain;
25423 }
72bf9492
DJ
25424}
25425
72bf9492
DJ
25426/* Trivial hash function for partial_die_info: the hash value of a DIE
25427 is its offset in .debug_info for this objfile. */
25428
25429static hashval_t
25430partial_die_hash (const void *item)
25431{
9a3c8263
SM
25432 const struct partial_die_info *part_die
25433 = (const struct partial_die_info *) item;
9a619af0 25434
9c541725 25435 return to_underlying (part_die->sect_off);
72bf9492
DJ
25436}
25437
25438/* Trivial comparison function for partial_die_info structures: two DIEs
25439 are equal if they have the same offset. */
25440
25441static int
25442partial_die_eq (const void *item_lhs, const void *item_rhs)
25443{
9a3c8263
SM
25444 const struct partial_die_info *part_die_lhs
25445 = (const struct partial_die_info *) item_lhs;
25446 const struct partial_die_info *part_die_rhs
25447 = (const struct partial_die_info *) item_rhs;
9a619af0 25448
9c541725 25449 return part_die_lhs->sect_off == part_die_rhs->sect_off;
72bf9492
DJ
25450}
25451
b4f54984
DE
25452static struct cmd_list_element *set_dwarf_cmdlist;
25453static struct cmd_list_element *show_dwarf_cmdlist;
ae038cb0
DJ
25454
25455static void
981a3fb3 25456set_dwarf_cmd (const char *args, int from_tty)
ae038cb0 25457{
b4f54984 25458 help_list (set_dwarf_cmdlist, "maintenance set dwarf ", all_commands,
635c7e8a 25459 gdb_stdout);
ae038cb0
DJ
25460}
25461
25462static void
981a3fb3 25463show_dwarf_cmd (const char *args, int from_tty)
6e70227d 25464{
b4f54984 25465 cmd_show_list (show_dwarf_cmdlist, from_tty, "");
ae038cb0
DJ
25466}
25467
cd4fb1b2 25468int dwarf_always_disassemble;
437afbb8 25469
437afbb8 25470static void
cd4fb1b2
SM
25471show_dwarf_always_disassemble (struct ui_file *file, int from_tty,
25472 struct cmd_list_element *c, const char *value)
9291a0cd 25473{
cd4fb1b2
SM
25474 fprintf_filtered (file,
25475 _("Whether to always disassemble "
25476 "DWARF expressions is %s.\n"),
25477 value);
9291a0cd
TT
25478}
25479
9291a0cd 25480static void
cd4fb1b2
SM
25481show_check_physname (struct ui_file *file, int from_tty,
25482 struct cmd_list_element *c, const char *value)
9291a0cd 25483{
cd4fb1b2
SM
25484 fprintf_filtered (file,
25485 _("Whether to check \"physname\" is %s.\n"),
25486 value);
9291a0cd
TT
25487}
25488
cd4fb1b2
SM
25489void
25490_initialize_dwarf2_read (void)
9291a0cd 25491{
9291a0cd 25492
cd4fb1b2 25493 dwarf2_objfile_data_key = register_objfile_data ();
156942c7 25494
cd4fb1b2
SM
25495 add_prefix_cmd ("dwarf", class_maintenance, set_dwarf_cmd, _("\
25496Set DWARF specific variables.\n\
25497Configure DWARF variables such as the cache size"),
25498 &set_dwarf_cmdlist, "maintenance set dwarf ",
25499 0/*allow-unknown*/, &maintenance_set_cmdlist);
156942c7 25500
cd4fb1b2
SM
25501 add_prefix_cmd ("dwarf", class_maintenance, show_dwarf_cmd, _("\
25502Show DWARF specific variables\n\
25503Show DWARF variables such as the cache size"),
25504 &show_dwarf_cmdlist, "maintenance show dwarf ",
25505 0/*allow-unknown*/, &maintenance_show_cmdlist);
156942c7 25506
cd4fb1b2
SM
25507 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
25508 &dwarf_max_cache_age, _("\
25509Set the upper bound on the age of cached DWARF compilation units."), _("\
25510Show the upper bound on the age of cached DWARF compilation units."), _("\
25511A higher limit means that cached compilation units will be stored\n\
25512in memory longer, and more total memory will be used. Zero disables\n\
25513caching, which can slow down startup."),
25514 NULL,
25515 show_dwarf_max_cache_age,
25516 &set_dwarf_cmdlist,
25517 &show_dwarf_cmdlist);
156942c7 25518
cd4fb1b2
SM
25519 add_setshow_boolean_cmd ("always-disassemble", class_obscure,
25520 &dwarf_always_disassemble, _("\
25521Set whether `info address' always disassembles DWARF expressions."), _("\
25522Show whether `info address' always disassembles DWARF expressions."), _("\
25523When enabled, DWARF expressions are always printed in an assembly-like\n\
25524syntax. When disabled, expressions will be printed in a more\n\
25525conversational style, when possible."),
25526 NULL,
25527 show_dwarf_always_disassemble,
25528 &set_dwarf_cmdlist,
25529 &show_dwarf_cmdlist);
9291a0cd 25530
cd4fb1b2
SM
25531 add_setshow_zuinteger_cmd ("dwarf-read", no_class, &dwarf_read_debug, _("\
25532Set debugging of the DWARF reader."), _("\
25533Show debugging of the DWARF reader."), _("\
25534When enabled (non-zero), debugging messages are printed during DWARF\n\
25535reading and symtab expansion. A value of 1 (one) provides basic\n\
25536information. A value greater than 1 provides more verbose information."),
25537 NULL,
25538 NULL,
25539 &setdebuglist, &showdebuglist);
9291a0cd 25540
cd4fb1b2
SM
25541 add_setshow_zuinteger_cmd ("dwarf-die", no_class, &dwarf_die_debug, _("\
25542Set debugging of the DWARF DIE reader."), _("\
25543Show debugging of the DWARF DIE reader."), _("\
25544When enabled (non-zero), DIEs are dumped after they are read in.\n\
25545The value is the maximum depth to print."),
25546 NULL,
25547 NULL,
25548 &setdebuglist, &showdebuglist);
9291a0cd 25549
cd4fb1b2
SM
25550 add_setshow_zuinteger_cmd ("dwarf-line", no_class, &dwarf_line_debug, _("\
25551Set debugging of the dwarf line reader."), _("\
25552Show debugging of the dwarf line reader."), _("\
25553When enabled (non-zero), line number entries are dumped as they are read in.\n\
25554A value of 1 (one) provides basic information.\n\
25555A value greater than 1 provides more verbose information."),
25556 NULL,
25557 NULL,
25558 &setdebuglist, &showdebuglist);
437afbb8 25559
cd4fb1b2
SM
25560 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
25561Set cross-checking of \"physname\" code against demangler."), _("\
25562Show cross-checking of \"physname\" code against demangler."), _("\
25563When enabled, GDB's internal \"physname\" code is checked against\n\
25564the demangler."),
25565 NULL, show_check_physname,
25566 &setdebuglist, &showdebuglist);
900e11f9 25567
e615022a
DE
25568 add_setshow_boolean_cmd ("use-deprecated-index-sections",
25569 no_class, &use_deprecated_index_sections, _("\
25570Set whether to use deprecated gdb_index sections."), _("\
25571Show whether to use deprecated gdb_index sections."), _("\
25572When enabled, deprecated .gdb_index sections are used anyway.\n\
25573Normally they are ignored either because of a missing feature or\n\
25574performance issue.\n\
25575Warning: This option must be enabled before gdb reads the file."),
25576 NULL,
25577 NULL,
25578 &setlist, &showlist);
25579
f1e6e072
TT
25580 dwarf2_locexpr_index = register_symbol_computed_impl (LOC_COMPUTED,
25581 &dwarf2_locexpr_funcs);
25582 dwarf2_loclist_index = register_symbol_computed_impl (LOC_COMPUTED,
25583 &dwarf2_loclist_funcs);
25584
25585 dwarf2_locexpr_block_index = register_symbol_block_impl (LOC_BLOCK,
25586 &dwarf2_block_frame_base_locexpr_funcs);
25587 dwarf2_loclist_block_index = register_symbol_block_impl (LOC_BLOCK,
25588 &dwarf2_block_frame_base_loclist_funcs);
c62446b1
PA
25589
25590#if GDB_SELF_TEST
25591 selftests::register_test ("dw2_expand_symtabs_matching",
25592 selftests::dw2_expand_symtabs_matching::run_test);
25593#endif
6502dd73 25594}
This page took 4.32129 seconds and 4 git commands to generate.