x86: Move x86-specific linker options to elf_linker_x86_params
[deliverable/binutils-gdb.git] / gdb / dwarf2read.c
CommitLineData
c906108c 1/* DWARF 2 debugging format support for GDB.
917c78fc 2
42a4f53d 3 Copyright (C) 1994-2019 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"
d55e5aa6
TT
32
33/* Standard C includes. */
34#include <ctype.h>
35#include <fcntl.h>
36#include <sys/stat.h>
37#include <sys/types.h>
38
39/* Standard C++ includes. */
40#include <algorithm>
41#include <cmath>
42#include <forward_list>
43#include <set>
44#include <unordered_map>
45#include <unordered_set>
46
47/* Local non-gdb includes. */
48#include "addrmap.h"
c906108c 49#include "bfd.h"
edb3359d 50#include "block.h"
d55e5aa6
TT
51#include "build-id.h"
52#include "buildsym.h"
98bfdba5 53#include "c-lang.h"
d55e5aa6
TT
54#include "command.h"
55#include "common/byte-vector.h"
0747795c 56#include "common/filestuff.h"
14bc53a8 57#include "common/function-view.h"
ecfb656c 58#include "common/gdb_optional.h"
d55e5aa6 59#include "common/gdb_unlinker.h"
927aa2e7 60#include "common/hash_enum.h"
d55e5aa6
TT
61#include "common/pathstuff.h"
62#include "common/selftest.h"
63#include "common/underlying.h"
64#include "common/vec.h"
65#include "complaints.h"
66#include "completer.h"
67#include "cp-support.h"
68#include "demangle.h"
69#include "dwarf-index-cache.h"
70#include "dwarf-index-common.h"
71#include "dwarf2.h"
72#include "dwarf2expr.h"
73#include "dwarf2loc.h"
74#include "dwarf2read.h"
75#include "elf-bfd.h"
76#include "expression.h"
77#include "f-lang.h"
bbf2f4df 78#include "filename-seen-cache.h"
d55e5aa6
TT
79#include "filenames.h"
80#include "gdb-demangle.h"
81#include "gdb/gdb-index.h"
82#include "gdb_bfd.h"
83#include "gdbcmd.h"
84#include "gdbcore.h"
85#include "gdbtypes.h"
86#include "go-lang.h"
87#include "hashtab.h"
88#include "language.h"
89#include "macrotab.h"
90#include "namespace.h"
91#include "objfiles.h"
b32b108a 92#include "producer.h"
d55e5aa6 93#include "psympriv.h"
c9317f21 94#include "rust-lang.h"
d55e5aa6
TT
95#include "source.h"
96#include "symtab.h"
97#include "typeprint.h"
98#include "valprint.h"
437afbb8 99
73be47f5
DE
100/* When == 1, print basic high level tracing messages.
101 When > 1, be more verbose.
b4f54984
DE
102 This is in contrast to the low level DIE reading of dwarf_die_debug. */
103static unsigned int dwarf_read_debug = 0;
45cfd468 104
d97bc12b 105/* When non-zero, dump DIEs after they are read in. */
b4f54984 106static unsigned int dwarf_die_debug = 0;
d97bc12b 107
27e0867f
DE
108/* When non-zero, dump line number entries as they are read in. */
109static unsigned int dwarf_line_debug = 0;
110
900e11f9
JK
111/* When non-zero, cross-check physname against demangler. */
112static int check_physname = 0;
113
481860b3 114/* When non-zero, do not reject deprecated .gdb_index sections. */
e615022a 115static int use_deprecated_index_sections = 0;
481860b3 116
6502dd73
DJ
117static const struct objfile_data *dwarf2_objfile_data_key;
118
f1e6e072
TT
119/* The "aclass" indices for various kinds of computed DWARF symbols. */
120
121static int dwarf2_locexpr_index;
122static int dwarf2_loclist_index;
123static int dwarf2_locexpr_block_index;
124static int dwarf2_loclist_block_index;
125
3f563c84
PA
126/* An index into a (C++) symbol name component in a symbol name as
127 recorded in the mapped_index's symbol table. For each C++ symbol
128 in the symbol table, we record one entry for the start of each
129 component in the symbol in a table of name components, and then
130 sort the table, in order to be able to binary search symbol names,
131 ignoring leading namespaces, both completion and regular look up.
132 For example, for symbol "A::B::C", we'll have an entry that points
133 to "A::B::C", another that points to "B::C", and another for "C".
134 Note that function symbols in GDB index have no parameter
135 information, just the function/method names. You can convert a
136 name_component to a "const char *" using the
137 'mapped_index::symbol_name_at(offset_type)' method. */
138
139struct name_component
140{
141 /* Offset in the symbol name where the component starts. Stored as
142 a (32-bit) offset instead of a pointer to save memory and improve
143 locality on 64-bit architectures. */
144 offset_type name_offset;
145
146 /* The symbol's index in the symbol and constant pool tables of a
147 mapped_index. */
148 offset_type idx;
149};
150
44ed8f3e
PA
151/* Base class containing bits shared by both .gdb_index and
152 .debug_name indexes. */
153
154struct mapped_index_base
155{
22ca247e
TT
156 mapped_index_base () = default;
157 DISABLE_COPY_AND_ASSIGN (mapped_index_base);
158
44ed8f3e
PA
159 /* The name_component table (a sorted vector). See name_component's
160 description above. */
161 std::vector<name_component> name_components;
162
163 /* How NAME_COMPONENTS is sorted. */
164 enum case_sensitivity name_components_casing;
165
166 /* Return the number of names in the symbol table. */
167 virtual size_t symbol_name_count () const = 0;
168
169 /* Get the name of the symbol at IDX in the symbol table. */
170 virtual const char *symbol_name_at (offset_type idx) const = 0;
171
172 /* Return whether the name at IDX in the symbol table should be
173 ignored. */
174 virtual bool symbol_name_slot_invalid (offset_type idx) const
175 {
176 return false;
177 }
178
179 /* Build the symbol name component sorted vector, if we haven't
180 yet. */
181 void build_name_components ();
182
183 /* Returns the lower (inclusive) and upper (exclusive) bounds of the
184 possible matches for LN_NO_PARAMS in the name component
185 vector. */
186 std::pair<std::vector<name_component>::const_iterator,
187 std::vector<name_component>::const_iterator>
188 find_name_components_bounds (const lookup_name_info &ln_no_params) const;
189
190 /* Prevent deleting/destroying via a base class pointer. */
191protected:
192 ~mapped_index_base() = default;
193};
194
9291a0cd
TT
195/* A description of the mapped index. The file format is described in
196 a comment by the code that writes the index. */
fc898b42 197struct mapped_index final : public mapped_index_base
9291a0cd 198{
f00a2de2
PA
199 /* A slot/bucket in the symbol table hash. */
200 struct symbol_table_slot
201 {
202 const offset_type name;
203 const offset_type vec;
204 };
205
559a7a62 206 /* Index data format version. */
3063847f 207 int version = 0;
559a7a62 208
f00a2de2
PA
209 /* The address table data. */
210 gdb::array_view<const gdb_byte> address_table;
b11b1f88 211
3876f04e 212 /* The symbol table, implemented as a hash table. */
f00a2de2 213 gdb::array_view<symbol_table_slot> symbol_table;
b11b1f88 214
9291a0cd 215 /* A pointer to the constant pool. */
3063847f 216 const char *constant_pool = nullptr;
3f563c84 217
44ed8f3e
PA
218 bool symbol_name_slot_invalid (offset_type idx) const override
219 {
220 const auto &bucket = this->symbol_table[idx];
221 return bucket.name == 0 && bucket.vec;
222 }
5c58de74 223
3f563c84
PA
224 /* Convenience method to get at the name of the symbol at IDX in the
225 symbol table. */
44ed8f3e 226 const char *symbol_name_at (offset_type idx) const override
f00a2de2 227 { return this->constant_pool + MAYBE_SWAP (this->symbol_table[idx].name); }
5c58de74 228
44ed8f3e
PA
229 size_t symbol_name_count () const override
230 { return this->symbol_table.size (); }
9291a0cd
TT
231};
232
927aa2e7
JK
233/* A description of the mapped .debug_names.
234 Uninitialized map has CU_COUNT 0. */
fc898b42 235struct mapped_debug_names final : public mapped_index_base
927aa2e7 236{
ed2dc618
SM
237 mapped_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile_)
238 : dwarf2_per_objfile (dwarf2_per_objfile_)
239 {}
240
241 struct dwarf2_per_objfile *dwarf2_per_objfile;
927aa2e7
JK
242 bfd_endian dwarf5_byte_order;
243 bool dwarf5_is_dwarf64;
244 bool augmentation_is_gdb;
245 uint8_t offset_size;
246 uint32_t cu_count = 0;
247 uint32_t tu_count, bucket_count, name_count;
248 const gdb_byte *cu_table_reordered, *tu_table_reordered;
249 const uint32_t *bucket_table_reordered, *hash_table_reordered;
250 const gdb_byte *name_table_string_offs_reordered;
251 const gdb_byte *name_table_entry_offs_reordered;
252 const gdb_byte *entry_pool;
253
254 struct index_val
255 {
256 ULONGEST dwarf_tag;
257 struct attr
258 {
259 /* Attribute name DW_IDX_*. */
260 ULONGEST dw_idx;
261
262 /* Attribute form DW_FORM_*. */
263 ULONGEST form;
264
265 /* Value if FORM is DW_FORM_implicit_const. */
266 LONGEST implicit_const;
267 };
268 std::vector<attr> attr_vec;
269 };
270
271 std::unordered_map<ULONGEST, index_val> abbrev_map;
272
273 const char *namei_to_name (uint32_t namei) const;
44ed8f3e
PA
274
275 /* Implementation of the mapped_index_base virtual interface, for
276 the name_components cache. */
277
278 const char *symbol_name_at (offset_type idx) const override
279 { return namei_to_name (idx); }
280
281 size_t symbol_name_count () const override
282 { return this->name_count; }
927aa2e7
JK
283};
284
cd4fb1b2 285/* See dwarf2read.h. */
ed2dc618 286
cd4fb1b2 287dwarf2_per_objfile *
ed2dc618
SM
288get_dwarf2_per_objfile (struct objfile *objfile)
289{
290 return ((struct dwarf2_per_objfile *)
291 objfile_data (objfile, dwarf2_objfile_data_key));
292}
293
294/* Set the dwarf2_per_objfile associated to OBJFILE. */
295
296void
297set_dwarf2_per_objfile (struct objfile *objfile,
298 struct dwarf2_per_objfile *dwarf2_per_objfile)
299{
300 gdb_assert (get_dwarf2_per_objfile (objfile) == NULL);
301 set_objfile_data (objfile, dwarf2_objfile_data_key, dwarf2_per_objfile);
302}
c906108c 303
251d32d9 304/* Default names of the debugging sections. */
c906108c 305
233a11ab
CS
306/* Note that if the debugging section has been compressed, it might
307 have a name like .zdebug_info. */
308
9cdd5dbd
DE
309static const struct dwarf2_debug_sections dwarf2_elf_names =
310{
251d32d9
TG
311 { ".debug_info", ".zdebug_info" },
312 { ".debug_abbrev", ".zdebug_abbrev" },
313 { ".debug_line", ".zdebug_line" },
314 { ".debug_loc", ".zdebug_loc" },
43988095 315 { ".debug_loclists", ".zdebug_loclists" },
251d32d9 316 { ".debug_macinfo", ".zdebug_macinfo" },
cf2c3c16 317 { ".debug_macro", ".zdebug_macro" },
251d32d9 318 { ".debug_str", ".zdebug_str" },
43988095 319 { ".debug_line_str", ".zdebug_line_str" },
251d32d9 320 { ".debug_ranges", ".zdebug_ranges" },
43988095 321 { ".debug_rnglists", ".zdebug_rnglists" },
251d32d9 322 { ".debug_types", ".zdebug_types" },
3019eac3 323 { ".debug_addr", ".zdebug_addr" },
251d32d9
TG
324 { ".debug_frame", ".zdebug_frame" },
325 { ".eh_frame", NULL },
24d3216f 326 { ".gdb_index", ".zgdb_index" },
927aa2e7
JK
327 { ".debug_names", ".zdebug_names" },
328 { ".debug_aranges", ".zdebug_aranges" },
24d3216f 329 23
251d32d9 330};
c906108c 331
80626a55 332/* List of DWO/DWP sections. */
3019eac3 333
80626a55 334static const struct dwop_section_names
3019eac3
DE
335{
336 struct dwarf2_section_names abbrev_dwo;
337 struct dwarf2_section_names info_dwo;
338 struct dwarf2_section_names line_dwo;
339 struct dwarf2_section_names loc_dwo;
43988095 340 struct dwarf2_section_names loclists_dwo;
09262596
DE
341 struct dwarf2_section_names macinfo_dwo;
342 struct dwarf2_section_names macro_dwo;
3019eac3
DE
343 struct dwarf2_section_names str_dwo;
344 struct dwarf2_section_names str_offsets_dwo;
345 struct dwarf2_section_names types_dwo;
80626a55
DE
346 struct dwarf2_section_names cu_index;
347 struct dwarf2_section_names tu_index;
3019eac3 348}
80626a55 349dwop_section_names =
3019eac3
DE
350{
351 { ".debug_abbrev.dwo", ".zdebug_abbrev.dwo" },
352 { ".debug_info.dwo", ".zdebug_info.dwo" },
353 { ".debug_line.dwo", ".zdebug_line.dwo" },
354 { ".debug_loc.dwo", ".zdebug_loc.dwo" },
43988095 355 { ".debug_loclists.dwo", ".zdebug_loclists.dwo" },
09262596
DE
356 { ".debug_macinfo.dwo", ".zdebug_macinfo.dwo" },
357 { ".debug_macro.dwo", ".zdebug_macro.dwo" },
3019eac3
DE
358 { ".debug_str.dwo", ".zdebug_str.dwo" },
359 { ".debug_str_offsets.dwo", ".zdebug_str_offsets.dwo" },
360 { ".debug_types.dwo", ".zdebug_types.dwo" },
80626a55
DE
361 { ".debug_cu_index", ".zdebug_cu_index" },
362 { ".debug_tu_index", ".zdebug_tu_index" },
3019eac3
DE
363};
364
c906108c
SS
365/* local data types */
366
107d2387
AC
367/* The data in a compilation unit header, after target2host
368 translation, looks like this. */
c906108c 369struct comp_unit_head
a738430d 370{
c764a876 371 unsigned int length;
a738430d 372 short version;
a738430d
MK
373 unsigned char addr_size;
374 unsigned char signed_addr_p;
9c541725 375 sect_offset abbrev_sect_off;
57349743 376
a738430d
MK
377 /* Size of file offsets; either 4 or 8. */
378 unsigned int offset_size;
57349743 379
a738430d
MK
380 /* Size of the length field; either 4 or 12. */
381 unsigned int initial_length_size;
57349743 382
43988095
JK
383 enum dwarf_unit_type unit_type;
384
a738430d
MK
385 /* Offset to the first byte of this compilation unit header in the
386 .debug_info section, for resolving relative reference dies. */
9c541725 387 sect_offset sect_off;
57349743 388
d00adf39
DE
389 /* Offset to first die in this cu from the start of the cu.
390 This will be the first byte following the compilation unit header. */
9c541725 391 cu_offset first_die_cu_offset;
43988095
JK
392
393 /* 64-bit signature of this type unit - it is valid only for
394 UNIT_TYPE DW_UT_type. */
395 ULONGEST signature;
396
397 /* For types, offset in the type's DIE of the type defined by this TU. */
9c541725 398 cu_offset type_cu_offset_in_tu;
a738430d 399};
c906108c 400
3da10d80
KS
401/* Type used for delaying computation of method physnames.
402 See comments for compute_delayed_physnames. */
403struct delayed_method_info
404{
405 /* The type to which the method is attached, i.e., its parent class. */
406 struct type *type;
407
408 /* The index of the method in the type's function fieldlists. */
409 int fnfield_index;
410
411 /* The index of the method in the fieldlist. */
412 int index;
413
414 /* The name of the DIE. */
415 const char *name;
416
417 /* The DIE associated with this method. */
418 struct die_info *die;
419};
420
e7c27a73
DJ
421/* Internal state when decoding a particular compilation unit. */
422struct dwarf2_cu
423{
fcd3b13d
SM
424 explicit dwarf2_cu (struct dwarf2_per_cu_data *per_cu);
425 ~dwarf2_cu ();
426
427 DISABLE_COPY_AND_ASSIGN (dwarf2_cu);
428
c24bdb02
KS
429 /* TU version of handle_DW_AT_stmt_list for read_type_unit_scope.
430 Create the set of symtabs used by this TU, or if this TU is sharing
431 symtabs with another TU and the symtabs have already been created
432 then restore those symtabs in the line header.
433 We don't need the pc/line-number mapping for type units. */
434 void setup_type_unit_groups (struct die_info *die);
435
436 /* Start a symtab for DWARF. NAME, COMP_DIR, LOW_PC are passed to the
437 buildsym_compunit constructor. */
438 struct compunit_symtab *start_symtab (const char *name,
439 const char *comp_dir,
440 CORE_ADDR low_pc);
441
442 /* Reset the builder. */
443 void reset_builder () { m_builder.reset (); }
444
d00adf39 445 /* The header of the compilation unit. */
fcd3b13d 446 struct comp_unit_head header {};
e142c38c 447
d00adf39 448 /* Base address of this compilation unit. */
fcd3b13d 449 CORE_ADDR base_address = 0;
d00adf39
DE
450
451 /* Non-zero if base_address has been set. */
fcd3b13d 452 int base_known = 0;
d00adf39 453
e142c38c 454 /* The language we are debugging. */
fcd3b13d
SM
455 enum language language = language_unknown;
456 const struct language_defn *language_defn = nullptr;
e142c38c 457
fcd3b13d 458 const char *producer = nullptr;
b0f35d58 459
c24bdb02 460private:
804d2729
TT
461 /* The symtab builder for this CU. This is only non-NULL when full
462 symbols are being read. */
c24bdb02 463 std::unique_ptr<buildsym_compunit> m_builder;
804d2729 464
c24bdb02 465public:
e142c38c
DJ
466 /* The generic symbol table building routines have separate lists for
467 file scope symbols and all all other scopes (local scopes). So
468 we need to select the right one to pass to add_symbol_to_list().
469 We do it by keeping a pointer to the correct list in list_in_scope.
470
471 FIXME: The original dwarf code just treated the file scope as the
472 first local scope, and all other local scopes as nested local
473 scopes, and worked fine. Check to see if we really need to
474 distinguish these in buildsym.c. */
fcd3b13d 475 struct pending **list_in_scope = nullptr;
e142c38c 476
b64f50a1
JK
477 /* Hash table holding all the loaded partial DIEs
478 with partial_die->offset.SECT_OFF as hash. */
fcd3b13d 479 htab_t partial_dies = nullptr;
72bf9492
DJ
480
481 /* Storage for things with the same lifetime as this read-in compilation
482 unit, including partial DIEs. */
fcd3b13d 483 auto_obstack comp_unit_obstack;
72bf9492 484
ae038cb0
DJ
485 /* When multiple dwarf2_cu structures are living in memory, this field
486 chains them all together, so that they can be released efficiently.
487 We will probably also want a generation counter so that most-recently-used
488 compilation units are cached... */
fcd3b13d 489 struct dwarf2_per_cu_data *read_in_chain = nullptr;
ae038cb0 490
69d751e3 491 /* Backlink to our per_cu entry. */
ae038cb0
DJ
492 struct dwarf2_per_cu_data *per_cu;
493
494 /* How many compilation units ago was this CU last referenced? */
fcd3b13d 495 int last_used = 0;
ae038cb0 496
b64f50a1
JK
497 /* A hash table of DIE cu_offset for following references with
498 die_info->offset.sect_off as hash. */
fcd3b13d 499 htab_t die_hash = nullptr;
10b3939b
DJ
500
501 /* Full DIEs if read in. */
fcd3b13d 502 struct die_info *dies = nullptr;
10b3939b
DJ
503
504 /* A set of pointers to dwarf2_per_cu_data objects for compilation
505 units referenced by this one. Only set during full symbol processing;
506 partial symbol tables do not have dependencies. */
fcd3b13d 507 htab_t dependencies = nullptr;
10b3939b 508
cb1df416 509 /* Header data from the line table, during full symbol processing. */
fcd3b13d 510 struct line_header *line_header = nullptr;
4c8aa72d
PA
511 /* Non-NULL if LINE_HEADER is owned by this DWARF_CU. Otherwise,
512 it's owned by dwarf2_per_objfile::line_header_hash. If non-NULL,
513 this is the DW_TAG_compile_unit die for this CU. We'll hold on
514 to the line header as long as this DIE is being processed. See
515 process_die_scope. */
fcd3b13d 516 die_info *line_header_die_owner = nullptr;
cb1df416 517
3da10d80
KS
518 /* A list of methods which need to have physnames computed
519 after all type information has been read. */
c89b44cd 520 std::vector<delayed_method_info> method_list;
3da10d80 521
96408a79 522 /* To be copied to symtab->call_site_htab. */
fcd3b13d 523 htab_t call_site_htab = nullptr;
96408a79 524
034e5797
DE
525 /* Non-NULL if this CU came from a DWO file.
526 There is an invariant here that is important to remember:
527 Except for attributes copied from the top level DIE in the "main"
528 (or "stub") file in preparation for reading the DWO file
529 (e.g., DW_AT_GNU_addr_base), we KISS: there is only *one* CU.
530 Either there isn't a DWO file (in which case this is NULL and the point
531 is moot), or there is and either we're not going to read it (in which
532 case this is NULL) or there is and we are reading it (in which case this
533 is non-NULL). */
fcd3b13d 534 struct dwo_unit *dwo_unit = nullptr;
3019eac3
DE
535
536 /* The DW_AT_addr_base attribute if present, zero otherwise
537 (zero is a valid value though).
1dbab08b 538 Note this value comes from the Fission stub CU/TU's DIE. */
fcd3b13d 539 ULONGEST addr_base = 0;
3019eac3 540
2e3cf129
DE
541 /* The DW_AT_ranges_base attribute if present, zero otherwise
542 (zero is a valid value though).
1dbab08b 543 Note this value comes from the Fission stub CU/TU's DIE.
2e3cf129 544 Also note that the value is zero in the non-DWO case so this value can
ab435259
DE
545 be used without needing to know whether DWO files are in use or not.
546 N.B. This does not apply to DW_AT_ranges appearing in
547 DW_TAG_compile_unit dies. This is a bit of a wart, consider if ever
548 DW_AT_ranges appeared in the DW_TAG_compile_unit of DWO DIEs: then
549 DW_AT_ranges_base *would* have to be applied, and we'd have to care
550 whether the DW_AT_ranges attribute came from the skeleton or DWO. */
fcd3b13d 551 ULONGEST ranges_base = 0;
2e3cf129 552
c9317f21
TT
553 /* When reading debug info generated by older versions of rustc, we
554 have to rewrite some union types to be struct types with a
555 variant part. This rewriting must be done after the CU is fully
556 read in, because otherwise at the point of rewriting some struct
557 type might not have been fully processed. So, we keep a list of
558 all such types here and process them after expansion. */
559 std::vector<struct type *> rust_unions;
560
ae038cb0 561 /* Mark used when releasing cached dies. */
9068261f 562 bool mark : 1;
ae038cb0 563
8be455d7
JK
564 /* This CU references .debug_loc. See the symtab->locations_valid field.
565 This test is imperfect as there may exist optimized debug code not using
566 any location list and still facing inlining issues if handled as
567 unoptimized code. For a future better test see GCC PR other/32998. */
9068261f 568 bool has_loclist : 1;
ba919b58 569
9068261f 570 /* These cache the results for producer_is_* fields. CHECKED_PRODUCER is true
1b80a9fa
JK
571 if all the producer_is_* fields are valid. This information is cached
572 because profiling CU expansion showed excessive time spent in
573 producer_is_gxx_lt_4_6. */
9068261f
AB
574 bool checked_producer : 1;
575 bool producer_is_gxx_lt_4_6 : 1;
576 bool producer_is_gcc_lt_4_3 : 1;
eb77c9df 577 bool producer_is_icc : 1;
9068261f 578 bool producer_is_icc_lt_14 : 1;
c258c396 579 bool producer_is_codewarrior : 1;
4d4ec4e5 580
9068261f 581 /* When true, the file that we're processing is known to have
4d4ec4e5
TT
582 debugging info for C++ namespaces. GCC 3.3.x did not produce
583 this information, but later versions do. */
584
9068261f 585 bool processing_has_namespace_info : 1;
d590ff25
YQ
586
587 struct partial_die_info *find_partial_die (sect_offset sect_off);
c24bdb02
KS
588
589 /* If this CU was inherited by another CU (via specification,
590 abstract_origin, etc), this is the ancestor CU. */
591 dwarf2_cu *ancestor;
592
593 /* Get the buildsym_compunit for this CU. */
594 buildsym_compunit *get_builder ()
595 {
596 /* If this CU has a builder associated with it, use that. */
597 if (m_builder != nullptr)
598 return m_builder.get ();
599
600 /* Otherwise, search ancestors for a valid builder. */
601 if (ancestor != nullptr)
602 return ancestor->get_builder ();
603
604 return nullptr;
605 }
e7c27a73
DJ
606};
607
094b34ac
DE
608/* A struct that can be used as a hash key for tables based on DW_AT_stmt_list.
609 This includes type_unit_group and quick_file_names. */
610
611struct stmt_list_hash
612{
613 /* The DWO unit this table is from or NULL if there is none. */
614 struct dwo_unit *dwo_unit;
615
616 /* Offset in .debug_line or .debug_line.dwo. */
9c541725 617 sect_offset line_sect_off;
094b34ac
DE
618};
619
f4dc4d17
DE
620/* Each element of dwarf2_per_objfile->type_unit_groups is a pointer to
621 an object of this type. */
622
623struct type_unit_group
624{
0186c6a7 625 /* dwarf2read.c's main "handle" on a TU symtab.
f4dc4d17
DE
626 To simplify things we create an artificial CU that "includes" all the
627 type units using this stmt_list so that the rest of the code still has
628 a "per_cu" handle on the symtab.
629 This PER_CU is recognized by having no section. */
8a0459fd 630#define IS_TYPE_UNIT_GROUP(per_cu) ((per_cu)->section == NULL)
094b34ac
DE
631 struct dwarf2_per_cu_data per_cu;
632
0186c6a7
DE
633 /* The TUs that share this DW_AT_stmt_list entry.
634 This is added to while parsing type units to build partial symtabs,
635 and is deleted afterwards and not used again. */
636 VEC (sig_type_ptr) *tus;
f4dc4d17 637
43f3e411 638 /* The compunit symtab.
094b34ac 639 Type units in a group needn't all be defined in the same source file,
43f3e411
DE
640 so we create an essentially anonymous symtab as the compunit symtab. */
641 struct compunit_symtab *compunit_symtab;
f4dc4d17 642
094b34ac
DE
643 /* The data used to construct the hash key. */
644 struct stmt_list_hash hash;
f4dc4d17
DE
645
646 /* The number of symtabs from the line header.
647 The value here must match line_header.num_file_names. */
648 unsigned int num_symtabs;
649
650 /* The symbol tables for this TU (obtained from the files listed in
651 DW_AT_stmt_list).
652 WARNING: The order of entries here must match the order of entries
653 in the line header. After the first TU using this type_unit_group, the
654 line header for the subsequent TUs is recreated from this. This is done
655 because we need to use the same symtabs for each TU using the same
656 DW_AT_stmt_list value. Also note that symtabs may be repeated here,
657 there's no guarantee the line header doesn't have duplicate entries. */
658 struct symtab **symtabs;
659};
660
73869dc2 661/* These sections are what may appear in a (real or virtual) DWO file. */
3019eac3
DE
662
663struct dwo_sections
664{
665 struct dwarf2_section_info abbrev;
3019eac3
DE
666 struct dwarf2_section_info line;
667 struct dwarf2_section_info loc;
43988095 668 struct dwarf2_section_info loclists;
09262596
DE
669 struct dwarf2_section_info macinfo;
670 struct dwarf2_section_info macro;
3019eac3
DE
671 struct dwarf2_section_info str;
672 struct dwarf2_section_info str_offsets;
80626a55
DE
673 /* In the case of a virtual DWO file, these two are unused. */
674 struct dwarf2_section_info info;
3019eac3
DE
675 VEC (dwarf2_section_info_def) *types;
676};
677
c88ee1f0 678/* CUs/TUs in DWP/DWO files. */
3019eac3
DE
679
680struct dwo_unit
681{
682 /* Backlink to the containing struct dwo_file. */
683 struct dwo_file *dwo_file;
684
685 /* The "id" that distinguishes this CU/TU.
686 .debug_info calls this "dwo_id", .debug_types calls this "signature".
687 Since signatures came first, we stick with it for consistency. */
688 ULONGEST signature;
689
690 /* The section this CU/TU lives in, in the DWO file. */
8a0459fd 691 struct dwarf2_section_info *section;
3019eac3 692
9c541725
PA
693 /* Same as dwarf2_per_cu_data:{sect_off,length} but in the DWO section. */
694 sect_offset sect_off;
3019eac3
DE
695 unsigned int length;
696
697 /* For types, offset in the type's DIE of the type defined by this TU. */
698 cu_offset type_offset_in_tu;
699};
700
73869dc2
DE
701/* include/dwarf2.h defines the DWP section codes.
702 It defines a max value but it doesn't define a min value, which we
703 use for error checking, so provide one. */
704
705enum dwp_v2_section_ids
706{
707 DW_SECT_MIN = 1
708};
709
80626a55 710/* Data for one DWO file.
57d63ce2
DE
711
712 This includes virtual DWO files (a virtual DWO file is a DWO file as it
713 appears in a DWP file). DWP files don't really have DWO files per se -
714 comdat folding of types "loses" the DWO file they came from, and from
715 a high level view DWP files appear to contain a mass of random types.
716 However, to maintain consistency with the non-DWP case we pretend DWP
717 files contain virtual DWO files, and we assign each TU with one virtual
718 DWO file (generally based on the line and abbrev section offsets -
719 a heuristic that seems to work in practice). */
3019eac3
DE
720
721struct dwo_file
722{
0ac5b59e 723 /* The DW_AT_GNU_dwo_name attribute.
80626a55
DE
724 For virtual DWO files the name is constructed from the section offsets
725 of abbrev,line,loc,str_offsets so that we combine virtual DWO files
726 from related CU+TUs. */
0ac5b59e
DE
727 const char *dwo_name;
728
729 /* The DW_AT_comp_dir attribute. */
730 const char *comp_dir;
3019eac3 731
80626a55
DE
732 /* The bfd, when the file is open. Otherwise this is NULL.
733 This is unused(NULL) for virtual DWO files where we use dwp_file.dbfd. */
734 bfd *dbfd;
3019eac3 735
73869dc2
DE
736 /* The sections that make up this DWO file.
737 Remember that for virtual DWO files in DWP V2, these are virtual
738 sections (for lack of a better name). */
3019eac3
DE
739 struct dwo_sections sections;
740
33c5cd75
DB
741 /* The CUs in the file.
742 Each element is a struct dwo_unit. Multiple CUs per DWO are supported as
743 an extension to handle LLVM's Link Time Optimization output (where
744 multiple source files may be compiled into a single object/dwo pair). */
745 htab_t cus;
3019eac3
DE
746
747 /* Table of TUs in the file.
748 Each element is a struct dwo_unit. */
749 htab_t tus;
750};
751
80626a55
DE
752/* These sections are what may appear in a DWP file. */
753
754struct dwp_sections
755{
73869dc2 756 /* These are used by both DWP version 1 and 2. */
80626a55
DE
757 struct dwarf2_section_info str;
758 struct dwarf2_section_info cu_index;
759 struct dwarf2_section_info tu_index;
73869dc2
DE
760
761 /* These are only used by DWP version 2 files.
762 In DWP version 1 the .debug_info.dwo, .debug_types.dwo, and other
763 sections are referenced by section number, and are not recorded here.
764 In DWP version 2 there is at most one copy of all these sections, each
765 section being (effectively) comprised of the concatenation of all of the
766 individual sections that exist in the version 1 format.
767 To keep the code simple we treat each of these concatenated pieces as a
768 section itself (a virtual section?). */
769 struct dwarf2_section_info abbrev;
770 struct dwarf2_section_info info;
771 struct dwarf2_section_info line;
772 struct dwarf2_section_info loc;
773 struct dwarf2_section_info macinfo;
774 struct dwarf2_section_info macro;
775 struct dwarf2_section_info str_offsets;
776 struct dwarf2_section_info types;
80626a55
DE
777};
778
73869dc2
DE
779/* These sections are what may appear in a virtual DWO file in DWP version 1.
780 A virtual DWO file is a DWO file as it appears in a DWP file. */
80626a55 781
73869dc2 782struct virtual_v1_dwo_sections
80626a55
DE
783{
784 struct dwarf2_section_info abbrev;
785 struct dwarf2_section_info line;
786 struct dwarf2_section_info loc;
787 struct dwarf2_section_info macinfo;
788 struct dwarf2_section_info macro;
789 struct dwarf2_section_info str_offsets;
790 /* Each DWP hash table entry records one CU or one TU.
8a0459fd 791 That is recorded here, and copied to dwo_unit.section. */
80626a55
DE
792 struct dwarf2_section_info info_or_types;
793};
794
73869dc2
DE
795/* Similar to virtual_v1_dwo_sections, but for DWP version 2.
796 In version 2, the sections of the DWO files are concatenated together
797 and stored in one section of that name. Thus each ELF section contains
798 several "virtual" sections. */
799
800struct virtual_v2_dwo_sections
801{
802 bfd_size_type abbrev_offset;
803 bfd_size_type abbrev_size;
804
805 bfd_size_type line_offset;
806 bfd_size_type line_size;
807
808 bfd_size_type loc_offset;
809 bfd_size_type loc_size;
810
811 bfd_size_type macinfo_offset;
812 bfd_size_type macinfo_size;
813
814 bfd_size_type macro_offset;
815 bfd_size_type macro_size;
816
817 bfd_size_type str_offsets_offset;
818 bfd_size_type str_offsets_size;
819
820 /* Each DWP hash table entry records one CU or one TU.
821 That is recorded here, and copied to dwo_unit.section. */
822 bfd_size_type info_or_types_offset;
823 bfd_size_type info_or_types_size;
824};
825
80626a55
DE
826/* Contents of DWP hash tables. */
827
828struct dwp_hash_table
829{
73869dc2 830 uint32_t version, nr_columns;
80626a55 831 uint32_t nr_units, nr_slots;
73869dc2
DE
832 const gdb_byte *hash_table, *unit_table;
833 union
834 {
835 struct
836 {
837 const gdb_byte *indices;
838 } v1;
839 struct
840 {
841 /* This is indexed by column number and gives the id of the section
842 in that column. */
843#define MAX_NR_V2_DWO_SECTIONS \
844 (1 /* .debug_info or .debug_types */ \
845 + 1 /* .debug_abbrev */ \
846 + 1 /* .debug_line */ \
847 + 1 /* .debug_loc */ \
848 + 1 /* .debug_str_offsets */ \
849 + 1 /* .debug_macro or .debug_macinfo */)
850 int section_ids[MAX_NR_V2_DWO_SECTIONS];
851 const gdb_byte *offsets;
852 const gdb_byte *sizes;
853 } v2;
854 } section_pool;
80626a55
DE
855};
856
857/* Data for one DWP file. */
858
859struct dwp_file
860{
400174b1
TT
861 dwp_file (const char *name_, gdb_bfd_ref_ptr &&abfd)
862 : name (name_),
863 dbfd (std::move (abfd))
864 {
865 }
866
80626a55
DE
867 /* Name of the file. */
868 const char *name;
869
73869dc2 870 /* File format version. */
400174b1 871 int version = 0;
73869dc2 872
93417882 873 /* The bfd. */
400174b1 874 gdb_bfd_ref_ptr dbfd;
80626a55
DE
875
876 /* Section info for this file. */
400174b1 877 struct dwp_sections sections {};
80626a55 878
57d63ce2 879 /* Table of CUs in the file. */
400174b1 880 const struct dwp_hash_table *cus = nullptr;
80626a55
DE
881
882 /* Table of TUs in the file. */
400174b1 883 const struct dwp_hash_table *tus = nullptr;
80626a55 884
19ac8c2e 885 /* Tables of loaded CUs/TUs. Each entry is a struct dwo_unit *. */
400174b1
TT
886 htab_t loaded_cus {};
887 htab_t loaded_tus {};
80626a55 888
73869dc2
DE
889 /* Table to map ELF section numbers to their sections.
890 This is only needed for the DWP V1 file format. */
400174b1
TT
891 unsigned int num_sections = 0;
892 asection **elf_sections = nullptr;
80626a55
DE
893};
894
36586728
TT
895/* This represents a '.dwz' file. */
896
897struct dwz_file
898{
7ff8cb8c
TT
899 dwz_file (gdb_bfd_ref_ptr &&bfd)
900 : dwz_bfd (std::move (bfd))
901 {
902 }
903
36586728 904 /* A dwz file can only contain a few sections. */
7ff8cb8c
TT
905 struct dwarf2_section_info abbrev {};
906 struct dwarf2_section_info info {};
907 struct dwarf2_section_info str {};
908 struct dwarf2_section_info line {};
909 struct dwarf2_section_info macro {};
910 struct dwarf2_section_info gdb_index {};
911 struct dwarf2_section_info debug_names {};
36586728
TT
912
913 /* The dwz's BFD. */
7ff8cb8c 914 gdb_bfd_ref_ptr dwz_bfd;
87d6a7aa
SM
915
916 /* If we loaded the index from an external file, this contains the
917 resources associated to the open file, memory mapping, etc. */
918 std::unique_ptr<index_cache_resource> index_cache_res;
36586728
TT
919};
920
0963b4bd
MS
921/* Struct used to pass misc. parameters to read_die_and_children, et
922 al. which are used for both .debug_info and .debug_types dies.
923 All parameters here are unchanging for the life of the call. This
dee91e82 924 struct exists to abstract away the constant parameters of die reading. */
93311388
DE
925
926struct die_reader_specs
927{
a32a8923 928 /* The bfd of die_section. */
93311388
DE
929 bfd* abfd;
930
931 /* The CU of the DIE we are parsing. */
932 struct dwarf2_cu *cu;
933
80626a55 934 /* Non-NULL if reading a DWO file (including one packaged into a DWP). */
3019eac3
DE
935 struct dwo_file *dwo_file;
936
dee91e82 937 /* The section the die comes from.
3019eac3 938 This is either .debug_info or .debug_types, or the .dwo variants. */
dee91e82
DE
939 struct dwarf2_section_info *die_section;
940
941 /* die_section->buffer. */
d521ce57 942 const gdb_byte *buffer;
f664829e
DE
943
944 /* The end of the buffer. */
945 const gdb_byte *buffer_end;
a2ce51a0
DE
946
947 /* The value of the DW_AT_comp_dir attribute. */
948 const char *comp_dir;
685af9cd
TT
949
950 /* The abbreviation table to use when reading the DIEs. */
951 struct abbrev_table *abbrev_table;
93311388
DE
952};
953
fd820528 954/* Type of function passed to init_cutu_and_read_dies, et.al. */
dee91e82 955typedef void (die_reader_func_ftype) (const struct die_reader_specs *reader,
d521ce57 956 const gdb_byte *info_ptr,
dee91e82
DE
957 struct die_info *comp_unit_die,
958 int has_children,
959 void *data);
960
ecfb656c
PA
961/* A 1-based directory index. This is a strong typedef to prevent
962 accidentally using a directory index as a 0-based index into an
963 array/vector. */
964enum class dir_index : unsigned int {};
965
966/* Likewise, a 1-based file name index. */
967enum class file_name_index : unsigned int {};
968
52059ffd
TT
969struct file_entry
970{
fff8551c
PA
971 file_entry () = default;
972
ecfb656c 973 file_entry (const char *name_, dir_index d_index_,
fff8551c
PA
974 unsigned int mod_time_, unsigned int length_)
975 : name (name_),
ecfb656c 976 d_index (d_index_),
fff8551c
PA
977 mod_time (mod_time_),
978 length (length_)
979 {}
980
ecfb656c
PA
981 /* Return the include directory at D_INDEX stored in LH. Returns
982 NULL if D_INDEX is out of bounds. */
8c43009f
PA
983 const char *include_dir (const line_header *lh) const;
984
fff8551c
PA
985 /* The file name. Note this is an observing pointer. The memory is
986 owned by debug_line_buffer. */
987 const char *name {};
988
8c43009f 989 /* The directory index (1-based). */
ecfb656c 990 dir_index d_index {};
fff8551c
PA
991
992 unsigned int mod_time {};
993
994 unsigned int length {};
995
996 /* True if referenced by the Line Number Program. */
997 bool included_p {};
998
83769d0b 999 /* The associated symbol table, if any. */
fff8551c 1000 struct symtab *symtab {};
52059ffd
TT
1001};
1002
debd256d
JB
1003/* The line number information for a compilation unit (found in the
1004 .debug_line section) begins with a "statement program header",
1005 which contains the following information. */
1006struct line_header
1007{
fff8551c
PA
1008 line_header ()
1009 : offset_in_dwz {}
1010 {}
1011
1012 /* Add an entry to the include directory table. */
1013 void add_include_dir (const char *include_dir);
1014
1015 /* Add an entry to the file name table. */
ecfb656c 1016 void add_file_name (const char *name, dir_index d_index,
fff8551c
PA
1017 unsigned int mod_time, unsigned int length);
1018
ecfb656c 1019 /* Return the include dir at INDEX (1-based). Returns NULL if INDEX
8c43009f 1020 is out of bounds. */
ecfb656c 1021 const char *include_dir_at (dir_index index) const
8c43009f 1022 {
ecfb656c
PA
1023 /* Convert directory index number (1-based) to vector index
1024 (0-based). */
1025 size_t vec_index = to_underlying (index) - 1;
1026
1027 if (vec_index >= include_dirs.size ())
8c43009f 1028 return NULL;
ecfb656c 1029 return include_dirs[vec_index];
8c43009f
PA
1030 }
1031
ecfb656c 1032 /* Return the file name at INDEX (1-based). Returns NULL if INDEX
8c43009f 1033 is out of bounds. */
ecfb656c 1034 file_entry *file_name_at (file_name_index index)
8c43009f 1035 {
ecfb656c
PA
1036 /* Convert file name index number (1-based) to vector index
1037 (0-based). */
1038 size_t vec_index = to_underlying (index) - 1;
1039
1040 if (vec_index >= file_names.size ())
fff8551c 1041 return NULL;
ecfb656c 1042 return &file_names[vec_index];
fff8551c
PA
1043 }
1044
1045 /* Const version of the above. */
1046 const file_entry *file_name_at (unsigned int index) const
1047 {
1048 if (index >= file_names.size ())
8c43009f
PA
1049 return NULL;
1050 return &file_names[index];
1051 }
1052
527f3840 1053 /* Offset of line number information in .debug_line section. */
9c541725 1054 sect_offset sect_off {};
527f3840
JK
1055
1056 /* OFFSET is for struct dwz_file associated with dwarf2_per_objfile. */
fff8551c
PA
1057 unsigned offset_in_dwz : 1; /* Can't initialize bitfields in-class. */
1058
1059 unsigned int total_length {};
1060 unsigned short version {};
1061 unsigned int header_length {};
1062 unsigned char minimum_instruction_length {};
1063 unsigned char maximum_ops_per_instruction {};
1064 unsigned char default_is_stmt {};
1065 int line_base {};
1066 unsigned char line_range {};
1067 unsigned char opcode_base {};
debd256d
JB
1068
1069 /* standard_opcode_lengths[i] is the number of operands for the
1070 standard opcode whose value is i. This means that
1071 standard_opcode_lengths[0] is unused, and the last meaningful
1072 element is standard_opcode_lengths[opcode_base - 1]. */
fff8551c 1073 std::unique_ptr<unsigned char[]> standard_opcode_lengths;
debd256d 1074
fff8551c
PA
1075 /* The include_directories table. Note these are observing
1076 pointers. The memory is owned by debug_line_buffer. */
1077 std::vector<const char *> include_dirs;
debd256d 1078
fff8551c
PA
1079 /* The file_names table. */
1080 std::vector<file_entry> file_names;
debd256d
JB
1081
1082 /* The start and end of the statement program following this
6502dd73 1083 header. These point into dwarf2_per_objfile->line_buffer. */
fff8551c 1084 const gdb_byte *statement_program_start {}, *statement_program_end {};
debd256d 1085};
c906108c 1086
fff8551c
PA
1087typedef std::unique_ptr<line_header> line_header_up;
1088
8c43009f
PA
1089const char *
1090file_entry::include_dir (const line_header *lh) const
1091{
ecfb656c 1092 return lh->include_dir_at (d_index);
8c43009f
PA
1093}
1094
c906108c 1095/* When we construct a partial symbol table entry we only
0963b4bd 1096 need this much information. */
6f06d47b 1097struct partial_die_info : public allocate_on_obstack
c906108c 1098 {
6f06d47b
YQ
1099 partial_die_info (sect_offset sect_off, struct abbrev_info *abbrev);
1100
1101 /* Disable assign but still keep copy ctor, which is needed
1102 load_partial_dies. */
1103 partial_die_info& operator=(const partial_die_info& rhs) = delete;
1104
52356b79
YQ
1105 /* Adjust the partial die before generating a symbol for it. This
1106 function may set the is_external flag or change the DIE's
1107 name. */
1108 void fixup (struct dwarf2_cu *cu);
1109
48fbe735
YQ
1110 /* Read a minimal amount of information into the minimal die
1111 structure. */
1112 const gdb_byte *read (const struct die_reader_specs *reader,
1113 const struct abbrev_info &abbrev,
1114 const gdb_byte *info_ptr);
1115
72bf9492 1116 /* Offset of this DIE. */
6f06d47b 1117 const sect_offset sect_off;
72bf9492
DJ
1118
1119 /* DWARF-2 tag for this DIE. */
6f06d47b 1120 const ENUM_BITFIELD(dwarf_tag) tag : 16;
72bf9492 1121
72bf9492 1122 /* Assorted flags describing the data found in this DIE. */
6f06d47b
YQ
1123 const unsigned int has_children : 1;
1124
72bf9492
DJ
1125 unsigned int is_external : 1;
1126 unsigned int is_declaration : 1;
1127 unsigned int has_type : 1;
1128 unsigned int has_specification : 1;
1129 unsigned int has_pc_info : 1;
481860b3 1130 unsigned int may_be_inlined : 1;
72bf9492 1131
0c1b455e
TT
1132 /* This DIE has been marked DW_AT_main_subprogram. */
1133 unsigned int main_subprogram : 1;
1134
72bf9492
DJ
1135 /* Flag set if the SCOPE field of this structure has been
1136 computed. */
1137 unsigned int scope_set : 1;
1138
fa4028e9
JB
1139 /* Flag set if the DIE has a byte_size attribute. */
1140 unsigned int has_byte_size : 1;
1141
ff908ebf
AW
1142 /* Flag set if the DIE has a DW_AT_const_value attribute. */
1143 unsigned int has_const_value : 1;
1144
98bfdba5
PA
1145 /* Flag set if any of the DIE's children are template arguments. */
1146 unsigned int has_template_arguments : 1;
1147
52356b79 1148 /* Flag set if fixup has been called on this die. */
abc72ce4
DE
1149 unsigned int fixup_called : 1;
1150
36586728
TT
1151 /* Flag set if DW_TAG_imported_unit uses DW_FORM_GNU_ref_alt. */
1152 unsigned int is_dwz : 1;
1153
1154 /* Flag set if spec_offset uses DW_FORM_GNU_ref_alt. */
1155 unsigned int spec_is_dwz : 1;
1156
72bf9492 1157 /* The name of this DIE. Normally the value of DW_AT_name, but
94af9270 1158 sometimes a default name for unnamed DIEs. */
6f06d47b 1159 const char *name = nullptr;
72bf9492 1160
abc72ce4 1161 /* The linkage name, if present. */
6f06d47b 1162 const char *linkage_name = nullptr;
abc72ce4 1163
72bf9492
DJ
1164 /* The scope to prepend to our children. This is generally
1165 allocated on the comp_unit_obstack, so will disappear
1166 when this compilation unit leaves the cache. */
6f06d47b 1167 const char *scope = nullptr;
72bf9492 1168
95554aad
TT
1169 /* Some data associated with the partial DIE. The tag determines
1170 which field is live. */
1171 union
1172 {
1173 /* The location description associated with this DIE, if any. */
1174 struct dwarf_block *locdesc;
1175 /* The offset of an import, for DW_TAG_imported_unit. */
9c541725 1176 sect_offset sect_off;
6f06d47b 1177 } d {};
72bf9492
DJ
1178
1179 /* If HAS_PC_INFO, the PC range associated with this DIE. */
6f06d47b
YQ
1180 CORE_ADDR lowpc = 0;
1181 CORE_ADDR highpc = 0;
72bf9492 1182
93311388 1183 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
72bf9492 1184 DW_AT_sibling, if any. */
48fbe735
YQ
1185 /* NOTE: This member isn't strictly necessary, partial_die_info::read
1186 could return DW_AT_sibling values to its caller load_partial_dies. */
6f06d47b 1187 const gdb_byte *sibling = nullptr;
72bf9492
DJ
1188
1189 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
1190 DW_AT_specification (or DW_AT_abstract_origin or
1191 DW_AT_extension). */
6f06d47b 1192 sect_offset spec_offset {};
72bf9492
DJ
1193
1194 /* Pointers to this DIE's parent, first child, and next sibling,
1195 if any. */
6f06d47b
YQ
1196 struct partial_die_info *die_parent = nullptr;
1197 struct partial_die_info *die_child = nullptr;
1198 struct partial_die_info *die_sibling = nullptr;
1199
1200 friend struct partial_die_info *
1201 dwarf2_cu::find_partial_die (sect_offset sect_off);
1202
1203 private:
1204 /* Only need to do look up in dwarf2_cu::find_partial_die. */
1205 partial_die_info (sect_offset sect_off)
1206 : partial_die_info (sect_off, DW_TAG_padding, 0)
1207 {
1208 }
1209
1210 partial_die_info (sect_offset sect_off_, enum dwarf_tag tag_,
1211 int has_children_)
1212 : sect_off (sect_off_), tag (tag_), has_children (has_children_)
1213 {
1214 is_external = 0;
1215 is_declaration = 0;
1216 has_type = 0;
1217 has_specification = 0;
1218 has_pc_info = 0;
1219 may_be_inlined = 0;
1220 main_subprogram = 0;
1221 scope_set = 0;
1222 has_byte_size = 0;
1223 has_const_value = 0;
1224 has_template_arguments = 0;
1225 fixup_called = 0;
1226 is_dwz = 0;
1227 spec_is_dwz = 0;
1228 }
c906108c
SS
1229 };
1230
0963b4bd 1231/* This data structure holds the information of an abbrev. */
c906108c
SS
1232struct abbrev_info
1233 {
1234 unsigned int number; /* number identifying abbrev */
1235 enum dwarf_tag tag; /* dwarf tag */
f3dd6933
DJ
1236 unsigned short has_children; /* boolean */
1237 unsigned short num_attrs; /* number of attributes */
c906108c
SS
1238 struct attr_abbrev *attrs; /* an array of attribute descriptions */
1239 struct abbrev_info *next; /* next in chain */
1240 };
1241
1242struct attr_abbrev
1243 {
9d25dd43
DE
1244 ENUM_BITFIELD(dwarf_attribute) name : 16;
1245 ENUM_BITFIELD(dwarf_form) form : 16;
43988095
JK
1246
1247 /* It is valid only if FORM is DW_FORM_implicit_const. */
1248 LONGEST implicit_const;
c906108c
SS
1249 };
1250
433df2d4
DE
1251/* Size of abbrev_table.abbrev_hash_table. */
1252#define ABBREV_HASH_SIZE 121
1253
1254/* Top level data structure to contain an abbreviation table. */
1255
1256struct abbrev_table
1257{
685af9cd
TT
1258 explicit abbrev_table (sect_offset off)
1259 : sect_off (off)
1260 {
4a17f768 1261 m_abbrevs =
685af9cd 1262 XOBNEWVEC (&abbrev_obstack, struct abbrev_info *, ABBREV_HASH_SIZE);
4a17f768 1263 memset (m_abbrevs, 0, ABBREV_HASH_SIZE * sizeof (struct abbrev_info *));
685af9cd
TT
1264 }
1265
1266 DISABLE_COPY_AND_ASSIGN (abbrev_table);
1267
1268 /* Allocate space for a struct abbrev_info object in
1269 ABBREV_TABLE. */
1270 struct abbrev_info *alloc_abbrev ();
1271
1272 /* Add an abbreviation to the table. */
1273 void add_abbrev (unsigned int abbrev_number, struct abbrev_info *abbrev);
1274
1275 /* Look up an abbrev in the table.
1276 Returns NULL if the abbrev is not found. */
1277
1278 struct abbrev_info *lookup_abbrev (unsigned int abbrev_number);
1279
1280
f4dc4d17
DE
1281 /* Where the abbrev table came from.
1282 This is used as a sanity check when the table is used. */
685af9cd 1283 const sect_offset sect_off;
433df2d4
DE
1284
1285 /* Storage for the abbrev table. */
685af9cd 1286 auto_obstack abbrev_obstack;
433df2d4 1287
4a17f768
YQ
1288private:
1289
433df2d4
DE
1290 /* Hash table of abbrevs.
1291 This is an array of size ABBREV_HASH_SIZE allocated in abbrev_obstack.
1292 It could be statically allocated, but the previous code didn't so we
1293 don't either. */
4a17f768 1294 struct abbrev_info **m_abbrevs;
433df2d4
DE
1295};
1296
685af9cd
TT
1297typedef std::unique_ptr<struct abbrev_table> abbrev_table_up;
1298
0963b4bd 1299/* Attributes have a name and a value. */
b60c80d6
DJ
1300struct attribute
1301 {
9d25dd43 1302 ENUM_BITFIELD(dwarf_attribute) name : 16;
8285870a
JK
1303 ENUM_BITFIELD(dwarf_form) form : 15;
1304
1305 /* Has DW_STRING already been updated by dwarf2_canonicalize_name? This
1306 field should be in u.str (existing only for DW_STRING) but it is kept
1307 here for better struct attribute alignment. */
1308 unsigned int string_is_canonical : 1;
1309
b60c80d6
DJ
1310 union
1311 {
15d034d0 1312 const char *str;
b60c80d6 1313 struct dwarf_block *blk;
43bbcdc2
PH
1314 ULONGEST unsnd;
1315 LONGEST snd;
b60c80d6 1316 CORE_ADDR addr;
ac9ec31b 1317 ULONGEST signature;
b60c80d6
DJ
1318 }
1319 u;
1320 };
1321
0963b4bd 1322/* This data structure holds a complete die structure. */
c906108c
SS
1323struct die_info
1324 {
76815b17
DE
1325 /* DWARF-2 tag for this DIE. */
1326 ENUM_BITFIELD(dwarf_tag) tag : 16;
1327
1328 /* Number of attributes */
98bfdba5
PA
1329 unsigned char num_attrs;
1330
1331 /* True if we're presently building the full type name for the
1332 type derived from this DIE. */
1333 unsigned char building_fullname : 1;
76815b17 1334
adde2bff
DE
1335 /* True if this die is in process. PR 16581. */
1336 unsigned char in_process : 1;
1337
76815b17
DE
1338 /* Abbrev number */
1339 unsigned int abbrev;
1340
93311388 1341 /* Offset in .debug_info or .debug_types section. */
9c541725 1342 sect_offset sect_off;
78ba4af6
JB
1343
1344 /* The dies in a compilation unit form an n-ary tree. PARENT
1345 points to this die's parent; CHILD points to the first child of
1346 this node; and all the children of a given node are chained
4950bc1c 1347 together via their SIBLING fields. */
639d11d3
DC
1348 struct die_info *child; /* Its first child, if any. */
1349 struct die_info *sibling; /* Its next sibling, if any. */
1350 struct die_info *parent; /* Its parent, if any. */
c906108c 1351
b60c80d6
DJ
1352 /* An array of attributes, with NUM_ATTRS elements. There may be
1353 zero, but it's not common and zero-sized arrays are not
1354 sufficiently portable C. */
1355 struct attribute attrs[1];
c906108c
SS
1356 };
1357
0963b4bd 1358/* Get at parts of an attribute structure. */
c906108c
SS
1359
1360#define DW_STRING(attr) ((attr)->u.str)
8285870a 1361#define DW_STRING_IS_CANONICAL(attr) ((attr)->string_is_canonical)
c906108c
SS
1362#define DW_UNSND(attr) ((attr)->u.unsnd)
1363#define DW_BLOCK(attr) ((attr)->u.blk)
1364#define DW_SND(attr) ((attr)->u.snd)
1365#define DW_ADDR(attr) ((attr)->u.addr)
ac9ec31b 1366#define DW_SIGNATURE(attr) ((attr)->u.signature)
c906108c 1367
0963b4bd 1368/* Blocks are a bunch of untyped bytes. */
c906108c
SS
1369struct dwarf_block
1370 {
56eb65bd 1371 size_t size;
1d6edc3c
JK
1372
1373 /* Valid only if SIZE is not zero. */
d521ce57 1374 const gdb_byte *data;
c906108c
SS
1375 };
1376
c906108c
SS
1377#ifndef ATTR_ALLOC_CHUNK
1378#define ATTR_ALLOC_CHUNK 4
1379#endif
1380
c906108c
SS
1381/* Allocate fields for structs, unions and enums in this size. */
1382#ifndef DW_FIELD_ALLOC_CHUNK
1383#define DW_FIELD_ALLOC_CHUNK 4
1384#endif
1385
c906108c
SS
1386/* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
1387 but this would require a corresponding change in unpack_field_as_long
1388 and friends. */
1389static int bits_per_byte = 8;
1390
2ddeaf8a
TT
1391/* When reading a variant or variant part, we track a bit more
1392 information about the field, and store it in an object of this
1393 type. */
1394
1395struct variant_field
1396{
1397 /* If we see a DW_TAG_variant, then this will be the discriminant
1398 value. */
1399 ULONGEST discriminant_value;
1400 /* If we see a DW_TAG_variant, then this will be set if this is the
1401 default branch. */
1402 bool default_branch;
1403 /* While reading a DW_TAG_variant_part, this will be set if this
1404 field is the discriminant. */
1405 bool is_discriminant;
1406};
1407
52059ffd
TT
1408struct nextfield
1409{
be2daae6
TT
1410 int accessibility = 0;
1411 int virtuality = 0;
2ddeaf8a 1412 /* Extra information to describe a variant or variant part. */
be2daae6
TT
1413 struct variant_field variant {};
1414 struct field field {};
52059ffd
TT
1415};
1416
1417struct fnfieldlist
1418{
be2daae6
TT
1419 const char *name = nullptr;
1420 std::vector<struct fn_field> fnfields;
52059ffd
TT
1421};
1422
c906108c
SS
1423/* The routines that read and process dies for a C struct or C++ class
1424 pass lists of data member fields and lists of member function fields
1425 in an instance of a field_info structure, as defined below. */
1426struct field_info
c5aa993b 1427 {
0963b4bd 1428 /* List of data member and baseclasses fields. */
be2daae6
TT
1429 std::vector<struct nextfield> fields;
1430 std::vector<struct nextfield> baseclasses;
c906108c 1431
7d0ccb61 1432 /* Number of fields (including baseclasses). */
be2daae6 1433 int nfields = 0;
c906108c 1434
c5aa993b 1435 /* Set if the accesibility of one of the fields is not public. */
be2daae6 1436 int non_public_fields = 0;
c906108c 1437
c5aa993b
JM
1438 /* Member function fieldlist array, contains name of possibly overloaded
1439 member function, number of overloaded member functions and a pointer
1440 to the head of the member function field chain. */
be2daae6 1441 std::vector<struct fnfieldlist> fnfieldlists;
98751a41
JK
1442
1443 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
1444 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
be2daae6 1445 std::vector<struct decl_field> typedef_field_list;
883fd55a
KS
1446
1447 /* Nested types defined by this class and the number of elements in this
1448 list. */
be2daae6 1449 std::vector<struct decl_field> nested_types_list;
c5aa993b 1450 };
c906108c 1451
10b3939b
DJ
1452/* One item on the queue of compilation units to read in full symbols
1453 for. */
1454struct dwarf2_queue_item
1455{
1456 struct dwarf2_per_cu_data *per_cu;
95554aad 1457 enum language pretend_language;
10b3939b
DJ
1458 struct dwarf2_queue_item *next;
1459};
1460
1461/* The current queue. */
1462static struct dwarf2_queue_item *dwarf2_queue, *dwarf2_queue_tail;
1463
ae038cb0
DJ
1464/* Loaded secondary compilation units are kept in memory until they
1465 have not been referenced for the processing of this many
1466 compilation units. Set this to zero to disable caching. Cache
1467 sizes of up to at least twenty will improve startup time for
1468 typical inter-CU-reference binaries, at an obvious memory cost. */
b4f54984 1469static int dwarf_max_cache_age = 5;
920d2a44 1470static void
b4f54984
DE
1471show_dwarf_max_cache_age (struct ui_file *file, int from_tty,
1472 struct cmd_list_element *c, const char *value)
920d2a44 1473{
3e43a32a 1474 fprintf_filtered (file, _("The upper bound on the age of cached "
b4f54984 1475 "DWARF compilation units is %s.\n"),
920d2a44
AC
1476 value);
1477}
4390d890 1478\f
c906108c
SS
1479/* local function prototypes */
1480
a32a8923
DE
1481static const char *get_section_name (const struct dwarf2_section_info *);
1482
1483static const char *get_section_file_name (const struct dwarf2_section_info *);
1484
918dd910
JK
1485static void dwarf2_find_base_address (struct die_info *die,
1486 struct dwarf2_cu *cu);
1487
0018ea6f
DE
1488static struct partial_symtab *create_partial_symtab
1489 (struct dwarf2_per_cu_data *per_cu, const char *name);
1490
f1902523
JK
1491static void build_type_psymtabs_reader (const struct die_reader_specs *reader,
1492 const gdb_byte *info_ptr,
1493 struct die_info *type_unit_die,
1494 int has_children, void *data);
1495
ed2dc618
SM
1496static void dwarf2_build_psymtabs_hard
1497 (struct dwarf2_per_objfile *dwarf2_per_objfile);
c906108c 1498
72bf9492
DJ
1499static void scan_partial_symbols (struct partial_die_info *,
1500 CORE_ADDR *, CORE_ADDR *,
5734ee8b 1501 int, struct dwarf2_cu *);
c906108c 1502
72bf9492
DJ
1503static void add_partial_symbol (struct partial_die_info *,
1504 struct dwarf2_cu *);
63d06c5c 1505
72bf9492
DJ
1506static void add_partial_namespace (struct partial_die_info *pdi,
1507 CORE_ADDR *lowpc, CORE_ADDR *highpc,
cdc07690 1508 int set_addrmap, struct dwarf2_cu *cu);
63d06c5c 1509
5d7cb8df 1510static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
cdc07690 1511 CORE_ADDR *highpc, int set_addrmap,
5d7cb8df
JK
1512 struct dwarf2_cu *cu);
1513
72bf9492
DJ
1514static void add_partial_enumeration (struct partial_die_info *enum_pdi,
1515 struct dwarf2_cu *cu);
91c24f0a 1516
bc30ff58
JB
1517static void add_partial_subprogram (struct partial_die_info *pdi,
1518 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 1519 int need_pc, struct dwarf2_cu *cu);
bc30ff58 1520
257e7a09
YQ
1521static void dwarf2_read_symtab (struct partial_symtab *,
1522 struct objfile *);
c906108c 1523
a14ed312 1524static void psymtab_to_symtab_1 (struct partial_symtab *);
c906108c 1525
685af9cd 1526static abbrev_table_up abbrev_table_read_table
ed2dc618
SM
1527 (struct dwarf2_per_objfile *dwarf2_per_objfile, struct dwarf2_section_info *,
1528 sect_offset);
433df2d4 1529
d521ce57 1530static unsigned int peek_abbrev_code (bfd *, const gdb_byte *);
6caca83c 1531
dee91e82 1532static struct partial_die_info *load_partial_dies
d521ce57 1533 (const struct die_reader_specs *, const gdb_byte *, int);
72bf9492 1534
36586728 1535static struct partial_die_info *find_partial_die (sect_offset, int,
10b3939b 1536 struct dwarf2_cu *);
72bf9492 1537
d521ce57
TT
1538static const gdb_byte *read_attribute (const struct die_reader_specs *,
1539 struct attribute *, struct attr_abbrev *,
1540 const gdb_byte *);
a8329558 1541
a1855c1d 1542static unsigned int read_1_byte (bfd *, const gdb_byte *);
c906108c 1543
a1855c1d 1544static int read_1_signed_byte (bfd *, const gdb_byte *);
c906108c 1545
a1855c1d 1546static unsigned int read_2_bytes (bfd *, const gdb_byte *);
c906108c 1547
a1855c1d 1548static unsigned int read_4_bytes (bfd *, const gdb_byte *);
c906108c 1549
a1855c1d 1550static ULONGEST read_8_bytes (bfd *, const gdb_byte *);
c906108c 1551
d521ce57 1552static CORE_ADDR read_address (bfd *, const gdb_byte *ptr, struct dwarf2_cu *,
891d2f0b 1553 unsigned int *);
c906108c 1554
d521ce57 1555static LONGEST read_initial_length (bfd *, const gdb_byte *, unsigned int *);
c764a876
DE
1556
1557static LONGEST read_checked_initial_length_and_offset
d521ce57 1558 (bfd *, const gdb_byte *, const struct comp_unit_head *,
c764a876 1559 unsigned int *, unsigned int *);
613e1657 1560
d521ce57
TT
1561static LONGEST read_offset (bfd *, const gdb_byte *,
1562 const struct comp_unit_head *,
c764a876
DE
1563 unsigned int *);
1564
d521ce57 1565static LONGEST read_offset_1 (bfd *, const gdb_byte *, unsigned int);
613e1657 1566
ed2dc618
SM
1567static sect_offset read_abbrev_offset
1568 (struct dwarf2_per_objfile *dwarf2_per_objfile,
1569 struct dwarf2_section_info *, sect_offset);
f4dc4d17 1570
d521ce57 1571static const gdb_byte *read_n_bytes (bfd *, const gdb_byte *, unsigned int);
c906108c 1572
d521ce57 1573static const char *read_direct_string (bfd *, const gdb_byte *, unsigned int *);
c906108c 1574
ed2dc618
SM
1575static const char *read_indirect_string
1576 (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *, const gdb_byte *,
1577 const struct comp_unit_head *, unsigned int *);
4bdf3d34 1578
ed2dc618
SM
1579static const char *read_indirect_line_string
1580 (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *, const gdb_byte *,
1581 const struct comp_unit_head *, unsigned int *);
36586728 1582
ed2dc618
SM
1583static const char *read_indirect_string_at_offset
1584 (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *abfd,
1585 LONGEST str_offset);
927aa2e7 1586
ed2dc618
SM
1587static const char *read_indirect_string_from_dwz
1588 (struct objfile *objfile, struct dwz_file *, LONGEST);
c906108c 1589
d521ce57 1590static LONGEST read_signed_leb128 (bfd *, const gdb_byte *, unsigned int *);
c906108c 1591
d521ce57
TT
1592static CORE_ADDR read_addr_index_from_leb128 (struct dwarf2_cu *,
1593 const gdb_byte *,
3019eac3
DE
1594 unsigned int *);
1595
d521ce57 1596static const char *read_str_index (const struct die_reader_specs *reader,
342587c4 1597 ULONGEST str_index);
3019eac3 1598
e142c38c 1599static void set_cu_language (unsigned int, struct dwarf2_cu *);
c906108c 1600
e142c38c
DJ
1601static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
1602 struct dwarf2_cu *);
c906108c 1603
348e048f 1604static struct attribute *dwarf2_attr_no_follow (struct die_info *,
45e58e77 1605 unsigned int);
348e048f 1606
7d45c7c3
KB
1607static const char *dwarf2_string_attr (struct die_info *die, unsigned int name,
1608 struct dwarf2_cu *cu);
1609
05cf31d1
JB
1610static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
1611 struct dwarf2_cu *cu);
1612
e142c38c 1613static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
3ca72b44 1614
e142c38c 1615static struct die_info *die_specification (struct die_info *die,
f2f0e013 1616 struct dwarf2_cu **);
63d06c5c 1617
9c541725 1618static line_header_up dwarf_decode_line_header (sect_offset sect_off,
fff8551c 1619 struct dwarf2_cu *cu);
debd256d 1620
f3f5162e 1621static void dwarf_decode_lines (struct line_header *, const char *,
c3b7b696 1622 struct dwarf2_cu *, struct partial_symtab *,
527f3840 1623 CORE_ADDR, int decode_mapping);
c906108c 1624
804d2729
TT
1625static void dwarf2_start_subfile (struct dwarf2_cu *, const char *,
1626 const char *);
c906108c 1627
a14ed312 1628static struct symbol *new_symbol (struct die_info *, struct type *,
5e2db402 1629 struct dwarf2_cu *, struct symbol * = NULL);
34eaf542 1630
ff39bb5e 1631static void dwarf2_const_value (const struct attribute *, struct symbol *,
e7c27a73 1632 struct dwarf2_cu *);
c906108c 1633
ff39bb5e 1634static void dwarf2_const_value_attr (const struct attribute *attr,
98bfdba5
PA
1635 struct type *type,
1636 const char *name,
1637 struct obstack *obstack,
12df843f 1638 struct dwarf2_cu *cu, LONGEST *value,
d521ce57 1639 const gdb_byte **bytes,
98bfdba5 1640 struct dwarf2_locexpr_baton **baton);
2df3850c 1641
e7c27a73 1642static struct type *die_type (struct die_info *, struct dwarf2_cu *);
c906108c 1643
b4ba55a1
JB
1644static int need_gnat_info (struct dwarf2_cu *);
1645
3e43a32a
MS
1646static struct type *die_descriptive_type (struct die_info *,
1647 struct dwarf2_cu *);
b4ba55a1
JB
1648
1649static void set_descriptive_type (struct type *, struct die_info *,
1650 struct dwarf2_cu *);
1651
e7c27a73
DJ
1652static struct type *die_containing_type (struct die_info *,
1653 struct dwarf2_cu *);
c906108c 1654
ff39bb5e 1655static struct type *lookup_die_type (struct die_info *, const struct attribute *,
673bfd45 1656 struct dwarf2_cu *);
c906108c 1657
f792889a 1658static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
c906108c 1659
673bfd45
DE
1660static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1661
0d5cff50 1662static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
63d06c5c 1663
6e70227d 1664static char *typename_concat (struct obstack *obs, const char *prefix,
f55ee35c
JK
1665 const char *suffix, int physname,
1666 struct dwarf2_cu *cu);
63d06c5c 1667
e7c27a73 1668static void read_file_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1669
348e048f
DE
1670static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1671
e7c27a73 1672static void read_func_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1673
e7c27a73 1674static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1675
96408a79
SA
1676static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1677
71a3c369
TT
1678static void read_variable (struct die_info *die, struct dwarf2_cu *cu);
1679
ff013f42
JK
1680static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
1681 struct dwarf2_cu *, struct partial_symtab *);
1682
3a2b436a 1683/* How dwarf2_get_pc_bounds constructed its *LOWPC and *HIGHPC return
e385593e 1684 values. Keep the items ordered with increasing constraints compliance. */
3a2b436a
JK
1685enum pc_bounds_kind
1686{
e385593e 1687 /* No attribute DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges was found. */
3a2b436a
JK
1688 PC_BOUNDS_NOT_PRESENT,
1689
e385593e
JK
1690 /* Some of the attributes DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges
1691 were present but they do not form a valid range of PC addresses. */
1692 PC_BOUNDS_INVALID,
1693
3a2b436a
JK
1694 /* Discontiguous range was found - that is DW_AT_ranges was found. */
1695 PC_BOUNDS_RANGES,
1696
1697 /* Contiguous range was found - DW_AT_low_pc and DW_AT_high_pc were found. */
1698 PC_BOUNDS_HIGH_LOW,
1699};
1700
1701static enum pc_bounds_kind dwarf2_get_pc_bounds (struct die_info *,
1702 CORE_ADDR *, CORE_ADDR *,
1703 struct dwarf2_cu *,
1704 struct partial_symtab *);
c906108c 1705
fae299cd
DC
1706static void get_scope_pc_bounds (struct die_info *,
1707 CORE_ADDR *, CORE_ADDR *,
1708 struct dwarf2_cu *);
1709
801e3a5b
JB
1710static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1711 CORE_ADDR, struct dwarf2_cu *);
1712
a14ed312 1713static void dwarf2_add_field (struct field_info *, struct die_info *,
e7c27a73 1714 struct dwarf2_cu *);
c906108c 1715
a14ed312 1716static void dwarf2_attach_fields_to_type (struct field_info *,
e7c27a73 1717 struct type *, struct dwarf2_cu *);
c906108c 1718
a14ed312 1719static void dwarf2_add_member_fn (struct field_info *,
e26fb1d7 1720 struct die_info *, struct type *,
e7c27a73 1721 struct dwarf2_cu *);
c906108c 1722
a14ed312 1723static void dwarf2_attach_fn_fields_to_type (struct field_info *,
3e43a32a
MS
1724 struct type *,
1725 struct dwarf2_cu *);
c906108c 1726
134d01f1 1727static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1728
e7c27a73 1729static void read_common_block (struct die_info *, struct dwarf2_cu *);
c906108c 1730
e7c27a73 1731static void read_namespace (struct die_info *die, struct dwarf2_cu *);
d9fa45fe 1732
5d7cb8df
JK
1733static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1734
804d2729 1735static struct using_direct **using_directives (struct dwarf2_cu *cu);
22cee43f 1736
27aa8d6a
SW
1737static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1738
74921315
KS
1739static int read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu);
1740
f55ee35c
JK
1741static struct type *read_module_type (struct die_info *die,
1742 struct dwarf2_cu *cu);
1743
38d518c9 1744static const char *namespace_name (struct die_info *die,
e142c38c 1745 int *is_anonymous, struct dwarf2_cu *);
38d518c9 1746
134d01f1 1747static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1748
e7c27a73 1749static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *);
c906108c 1750
6e70227d 1751static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
7ca2d3a3
DL
1752 struct dwarf2_cu *);
1753
bf6af496 1754static struct die_info *read_die_and_siblings_1
d521ce57 1755 (const struct die_reader_specs *, const gdb_byte *, const gdb_byte **,
bf6af496 1756 struct die_info *);
639d11d3 1757
dee91e82 1758static struct die_info *read_die_and_siblings (const struct die_reader_specs *,
d521ce57
TT
1759 const gdb_byte *info_ptr,
1760 const gdb_byte **new_info_ptr,
639d11d3
DC
1761 struct die_info *parent);
1762
d521ce57
TT
1763static const gdb_byte *read_full_die_1 (const struct die_reader_specs *,
1764 struct die_info **, const gdb_byte *,
1765 int *, int);
3019eac3 1766
d521ce57
TT
1767static const gdb_byte *read_full_die (const struct die_reader_specs *,
1768 struct die_info **, const gdb_byte *,
1769 int *);
93311388 1770
e7c27a73 1771static void process_die (struct die_info *, struct dwarf2_cu *);
c906108c 1772
15d034d0
TT
1773static const char *dwarf2_canonicalize_name (const char *, struct dwarf2_cu *,
1774 struct obstack *);
71c25dea 1775
15d034d0 1776static const char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
9219021c 1777
15d034d0 1778static const char *dwarf2_full_name (const char *name,
98bfdba5
PA
1779 struct die_info *die,
1780 struct dwarf2_cu *cu);
1781
ca69b9e6
DE
1782static const char *dwarf2_physname (const char *name, struct die_info *die,
1783 struct dwarf2_cu *cu);
1784
e142c38c 1785static struct die_info *dwarf2_extension (struct die_info *die,
f2f0e013 1786 struct dwarf2_cu **);
9219021c 1787
f39c6ffd 1788static const char *dwarf_tag_name (unsigned int);
c906108c 1789
f39c6ffd 1790static const char *dwarf_attr_name (unsigned int);
c906108c 1791
f39c6ffd 1792static const char *dwarf_form_name (unsigned int);
c906108c 1793
a121b7c1 1794static const char *dwarf_bool_name (unsigned int);
c906108c 1795
f39c6ffd 1796static const char *dwarf_type_encoding_name (unsigned int);
c906108c 1797
f9aca02d 1798static struct die_info *sibling_die (struct die_info *);
c906108c 1799
d97bc12b
DE
1800static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1801
1802static void dump_die_for_error (struct die_info *);
1803
1804static void dump_die_1 (struct ui_file *, int level, int max_level,
1805 struct die_info *);
c906108c 1806
d97bc12b 1807/*static*/ void dump_die (struct die_info *, int max_level);
c906108c 1808
51545339 1809static void store_in_ref_table (struct die_info *,
10b3939b 1810 struct dwarf2_cu *);
c906108c 1811
ff39bb5e 1812static sect_offset dwarf2_get_ref_die_offset (const struct attribute *);
c906108c 1813
ff39bb5e 1814static LONGEST dwarf2_get_attr_constant_value (const struct attribute *, int);
a02abb62 1815
348e048f 1816static struct die_info *follow_die_ref_or_sig (struct die_info *,
ff39bb5e 1817 const struct attribute *,
348e048f
DE
1818 struct dwarf2_cu **);
1819
10b3939b 1820static struct die_info *follow_die_ref (struct die_info *,
ff39bb5e 1821 const struct attribute *,
f2f0e013 1822 struct dwarf2_cu **);
c906108c 1823
348e048f 1824static struct die_info *follow_die_sig (struct die_info *,
ff39bb5e 1825 const struct attribute *,
348e048f
DE
1826 struct dwarf2_cu **);
1827
ac9ec31b
DE
1828static struct type *get_signatured_type (struct die_info *, ULONGEST,
1829 struct dwarf2_cu *);
1830
1831static struct type *get_DW_AT_signature_type (struct die_info *,
ff39bb5e 1832 const struct attribute *,
ac9ec31b
DE
1833 struct dwarf2_cu *);
1834
e5fe5e75 1835static void load_full_type_unit (struct dwarf2_per_cu_data *per_cu);
348e048f 1836
52dc124a 1837static void read_signatured_type (struct signatured_type *);
348e048f 1838
63e43d3a
PMR
1839static int attr_to_dynamic_prop (const struct attribute *attr,
1840 struct die_info *die, struct dwarf2_cu *cu,
1841 struct dynamic_prop *prop);
1842
c906108c
SS
1843/* memory allocation interface */
1844
7b5a2f43 1845static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
c906108c 1846
b60c80d6 1847static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
c906108c 1848
43f3e411 1849static void dwarf_decode_macros (struct dwarf2_cu *, unsigned int, int);
2e276125 1850
6e5a29e1 1851static int attr_form_is_block (const struct attribute *);
8e19ed76 1852
6e5a29e1 1853static int attr_form_is_section_offset (const struct attribute *);
3690dd37 1854
6e5a29e1 1855static int attr_form_is_constant (const struct attribute *);
3690dd37 1856
6e5a29e1 1857static int attr_form_is_ref (const struct attribute *);
7771576e 1858
8cf6f0b1
TT
1859static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1860 struct dwarf2_loclist_baton *baton,
ff39bb5e 1861 const struct attribute *attr);
8cf6f0b1 1862
ff39bb5e 1863static void dwarf2_symbol_mark_computed (const struct attribute *attr,
93e7bd98 1864 struct symbol *sym,
f1e6e072
TT
1865 struct dwarf2_cu *cu,
1866 int is_block);
4c2df51b 1867
d521ce57
TT
1868static const gdb_byte *skip_one_die (const struct die_reader_specs *reader,
1869 const gdb_byte *info_ptr,
1870 struct abbrev_info *abbrev);
4bb7a0a7 1871
72bf9492
DJ
1872static hashval_t partial_die_hash (const void *item);
1873
1874static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1875
ae038cb0 1876static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
ed2dc618
SM
1877 (sect_offset sect_off, unsigned int offset_in_dwz,
1878 struct dwarf2_per_objfile *dwarf2_per_objfile);
ae038cb0 1879
9816fde3 1880static void prepare_one_comp_unit (struct dwarf2_cu *cu,
95554aad
TT
1881 struct die_info *comp_unit_die,
1882 enum language pretend_language);
93311388 1883
ed2dc618 1884static void age_cached_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
ae038cb0 1885
dee91e82 1886static void free_one_cached_comp_unit (struct dwarf2_per_cu_data *);
ae038cb0 1887
f792889a
DJ
1888static struct type *set_die_type (struct die_info *, struct type *,
1889 struct dwarf2_cu *);
1c379e20 1890
ed2dc618 1891static void create_all_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
ae038cb0 1892
ed2dc618 1893static int create_all_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
1fd400ff 1894
58f0c718 1895static void load_full_comp_unit (struct dwarf2_per_cu_data *, bool,
95554aad 1896 enum language);
10b3939b 1897
95554aad
TT
1898static void process_full_comp_unit (struct dwarf2_per_cu_data *,
1899 enum language);
10b3939b 1900
f4dc4d17
DE
1901static void process_full_type_unit (struct dwarf2_per_cu_data *,
1902 enum language);
1903
10b3939b
DJ
1904static void dwarf2_add_dependence (struct dwarf2_cu *,
1905 struct dwarf2_per_cu_data *);
1906
ae038cb0
DJ
1907static void dwarf2_mark (struct dwarf2_cu *);
1908
1909static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
1910
b64f50a1 1911static struct type *get_die_type_at_offset (sect_offset,
ac9ec31b 1912 struct dwarf2_per_cu_data *);
673bfd45 1913
f792889a 1914static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
72019c9c 1915
95554aad
TT
1916static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
1917 enum language pretend_language);
1918
ed2dc618 1919static void process_queue (struct dwarf2_per_objfile *dwarf2_per_objfile);
9291a0cd 1920
b303c6f6
AB
1921/* Class, the destructor of which frees all allocated queue entries. This
1922 will only have work to do if an error was thrown while processing the
1923 dwarf. If no error was thrown then the queue entries should have all
1924 been processed, and freed, as we went along. */
1925
1926class dwarf2_queue_guard
1927{
1928public:
1929 dwarf2_queue_guard () = default;
1930
1931 /* Free any entries remaining on the queue. There should only be
1932 entries left if we hit an error while processing the dwarf. */
1933 ~dwarf2_queue_guard ()
1934 {
1935 struct dwarf2_queue_item *item, *last;
1936
1937 item = dwarf2_queue;
1938 while (item)
1939 {
1940 /* Anything still marked queued is likely to be in an
1941 inconsistent state, so discard it. */
1942 if (item->per_cu->queued)
1943 {
1944 if (item->per_cu->cu != NULL)
1945 free_one_cached_comp_unit (item->per_cu);
1946 item->per_cu->queued = 0;
1947 }
1948
1949 last = item;
1950 item = item->next;
1951 xfree (last);
1952 }
1953
1954 dwarf2_queue = dwarf2_queue_tail = NULL;
1955 }
1956};
1957
d721ba37
PA
1958/* The return type of find_file_and_directory. Note, the enclosed
1959 string pointers are only valid while this object is valid. */
1960
1961struct file_and_directory
1962{
1963 /* The filename. This is never NULL. */
1964 const char *name;
1965
1966 /* The compilation directory. NULL if not known. If we needed to
1967 compute a new string, this points to COMP_DIR_STORAGE, otherwise,
1968 points directly to the DW_AT_comp_dir string attribute owned by
1969 the obstack that owns the DIE. */
1970 const char *comp_dir;
1971
1972 /* If we needed to build a new string for comp_dir, this is what
1973 owns the storage. */
1974 std::string comp_dir_storage;
1975};
1976
1977static file_and_directory find_file_and_directory (struct die_info *die,
1978 struct dwarf2_cu *cu);
9291a0cd
TT
1979
1980static char *file_full_name (int file, struct line_header *lh,
1981 const char *comp_dir);
1982
43988095
JK
1983/* Expected enum dwarf_unit_type for read_comp_unit_head. */
1984enum class rcuh_kind { COMPILE, TYPE };
1985
d521ce57 1986static const gdb_byte *read_and_check_comp_unit_head
ed2dc618
SM
1987 (struct dwarf2_per_objfile* dwarf2_per_objfile,
1988 struct comp_unit_head *header,
36586728 1989 struct dwarf2_section_info *section,
d521ce57 1990 struct dwarf2_section_info *abbrev_section, const gdb_byte *info_ptr,
43988095 1991 rcuh_kind section_kind);
36586728 1992
fd820528 1993static void init_cutu_and_read_dies
f4dc4d17 1994 (struct dwarf2_per_cu_data *this_cu, struct abbrev_table *abbrev_table,
58f0c718 1995 int use_existing_cu, int keep, bool skip_partial,
3019eac3
DE
1996 die_reader_func_ftype *die_reader_func, void *data);
1997
dee91e82
DE
1998static void init_cutu_and_read_dies_simple
1999 (struct dwarf2_per_cu_data *this_cu,
2000 die_reader_func_ftype *die_reader_func, void *data);
9291a0cd 2001
673bfd45 2002static htab_t allocate_signatured_type_table (struct objfile *objfile);
1fd400ff 2003
3019eac3
DE
2004static htab_t allocate_dwo_unit_table (struct objfile *objfile);
2005
57d63ce2 2006static struct dwo_unit *lookup_dwo_unit_in_dwp
ed2dc618
SM
2007 (struct dwarf2_per_objfile *dwarf2_per_objfile,
2008 struct dwp_file *dwp_file, const char *comp_dir,
57d63ce2 2009 ULONGEST signature, int is_debug_types);
a2ce51a0 2010
ed2dc618
SM
2011static struct dwp_file *get_dwp_file
2012 (struct dwarf2_per_objfile *dwarf2_per_objfile);
a2ce51a0 2013
3019eac3 2014static struct dwo_unit *lookup_dwo_comp_unit
a1855c1d 2015 (struct dwarf2_per_cu_data *, const char *, const char *, ULONGEST);
3019eac3
DE
2016
2017static struct dwo_unit *lookup_dwo_type_unit
a1855c1d 2018 (struct signatured_type *, const char *, const char *);
3019eac3 2019
89e63ee4
DE
2020static void queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *);
2021
263db9a1 2022static void free_dwo_file (struct dwo_file *);
3019eac3 2023
263db9a1
TT
2024/* A unique_ptr helper to free a dwo_file. */
2025
2026struct dwo_file_deleter
ed2dc618 2027{
263db9a1
TT
2028 void operator() (struct dwo_file *df) const
2029 {
2030 free_dwo_file (df);
2031 }
ed2dc618
SM
2032};
2033
263db9a1
TT
2034/* A unique pointer to a dwo_file. */
2035
2036typedef std::unique_ptr<struct dwo_file, dwo_file_deleter> dwo_file_up;
2037
ed2dc618 2038static void process_cu_includes (struct dwarf2_per_objfile *dwarf2_per_objfile);
95554aad 2039
1b80a9fa 2040static void check_producer (struct dwarf2_cu *cu);
527f3840
JK
2041
2042static void free_line_header_voidp (void *arg);
4390d890
DE
2043\f
2044/* Various complaints about symbol reading that don't abort the process. */
2045
2046static void
2047dwarf2_statement_list_fits_in_line_number_section_complaint (void)
2048{
b98664d3 2049 complaint (_("statement list doesn't fit in .debug_line section"));
4390d890
DE
2050}
2051
2052static void
2053dwarf2_debug_line_missing_file_complaint (void)
2054{
b98664d3 2055 complaint (_(".debug_line section has line data without a file"));
4390d890
DE
2056}
2057
2058static void
2059dwarf2_debug_line_missing_end_sequence_complaint (void)
2060{
b98664d3 2061 complaint (_(".debug_line section has line "
4390d890
DE
2062 "program sequence without an end"));
2063}
2064
2065static void
2066dwarf2_complex_location_expr_complaint (void)
2067{
b98664d3 2068 complaint (_("location expression too complex"));
4390d890
DE
2069}
2070
2071static void
2072dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
2073 int arg3)
2074{
b98664d3 2075 complaint (_("const value length mismatch for '%s', got %d, expected %d"),
4390d890
DE
2076 arg1, arg2, arg3);
2077}
2078
2079static void
2080dwarf2_section_buffer_overflow_complaint (struct dwarf2_section_info *section)
2081{
b98664d3 2082 complaint (_("debug info runs off end of %s section"
4390d890 2083 " [in module %s]"),
a32a8923
DE
2084 get_section_name (section),
2085 get_section_file_name (section));
4390d890 2086}
1b80a9fa 2087
4390d890
DE
2088static void
2089dwarf2_macro_malformed_definition_complaint (const char *arg1)
2090{
b98664d3 2091 complaint (_("macro debug info contains a "
4390d890
DE
2092 "malformed macro definition:\n`%s'"),
2093 arg1);
2094}
2095
2096static void
2097dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
2098{
b98664d3 2099 complaint (_("invalid attribute class or form for '%s' in '%s'"),
4390d890
DE
2100 arg1, arg2);
2101}
527f3840
JK
2102
2103/* Hash function for line_header_hash. */
2104
2105static hashval_t
2106line_header_hash (const struct line_header *ofs)
2107{
9c541725 2108 return to_underlying (ofs->sect_off) ^ ofs->offset_in_dwz;
527f3840
JK
2109}
2110
2111/* Hash function for htab_create_alloc_ex for line_header_hash. */
2112
2113static hashval_t
2114line_header_hash_voidp (const void *item)
2115{
9a3c8263 2116 const struct line_header *ofs = (const struct line_header *) item;
527f3840
JK
2117
2118 return line_header_hash (ofs);
2119}
2120
2121/* Equality function for line_header_hash. */
2122
2123static int
2124line_header_eq_voidp (const void *item_lhs, const void *item_rhs)
2125{
9a3c8263
SM
2126 const struct line_header *ofs_lhs = (const struct line_header *) item_lhs;
2127 const struct line_header *ofs_rhs = (const struct line_header *) item_rhs;
527f3840 2128
9c541725 2129 return (ofs_lhs->sect_off == ofs_rhs->sect_off
527f3840
JK
2130 && ofs_lhs->offset_in_dwz == ofs_rhs->offset_in_dwz);
2131}
2132
4390d890 2133\f
9291a0cd 2134
31aa7e4e
JB
2135/* Read the given attribute value as an address, taking the attribute's
2136 form into account. */
2137
2138static CORE_ADDR
2139attr_value_as_address (struct attribute *attr)
2140{
2141 CORE_ADDR addr;
2142
2143 if (attr->form != DW_FORM_addr && attr->form != DW_FORM_GNU_addr_index)
2144 {
2145 /* Aside from a few clearly defined exceptions, attributes that
2146 contain an address must always be in DW_FORM_addr form.
2147 Unfortunately, some compilers happen to be violating this
2148 requirement by encoding addresses using other forms, such
2149 as DW_FORM_data4 for example. For those broken compilers,
2150 we try to do our best, without any guarantee of success,
2151 to interpret the address correctly. It would also be nice
2152 to generate a complaint, but that would require us to maintain
2153 a list of legitimate cases where a non-address form is allowed,
2154 as well as update callers to pass in at least the CU's DWARF
2155 version. This is more overhead than what we're willing to
2156 expand for a pretty rare case. */
2157 addr = DW_UNSND (attr);
2158 }
2159 else
2160 addr = DW_ADDR (attr);
2161
2162 return addr;
2163}
2164
330cdd98
PA
2165/* See declaration. */
2166
2167dwarf2_per_objfile::dwarf2_per_objfile (struct objfile *objfile_,
2168 const dwarf2_debug_sections *names)
2169 : objfile (objfile_)
2170{
2171 if (names == NULL)
2172 names = &dwarf2_elf_names;
2173
2174 bfd *obfd = objfile->obfd;
2175
2176 for (asection *sec = obfd->sections; sec != NULL; sec = sec->next)
2177 locate_sections (obfd, sec, *names);
2178}
2179
fc8e7e75
SM
2180static void free_dwo_files (htab_t dwo_files, struct objfile *objfile);
2181
330cdd98
PA
2182dwarf2_per_objfile::~dwarf2_per_objfile ()
2183{
2184 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
2185 free_cached_comp_units ();
2186
2187 if (quick_file_names_table)
2188 htab_delete (quick_file_names_table);
2189
2190 if (line_header_hash)
2191 htab_delete (line_header_hash);
2192
b76e467d
SM
2193 for (dwarf2_per_cu_data *per_cu : all_comp_units)
2194 VEC_free (dwarf2_per_cu_ptr, per_cu->imported_symtabs);
fc8e7e75 2195
b2bdb8cf
SM
2196 for (signatured_type *sig_type : all_type_units)
2197 VEC_free (dwarf2_per_cu_ptr, sig_type->per_cu.imported_symtabs);
fc8e7e75
SM
2198
2199 VEC_free (dwarf2_section_info_def, types);
2200
2201 if (dwo_files != NULL)
2202 free_dwo_files (dwo_files, objfile);
fc8e7e75 2203
330cdd98
PA
2204 /* Everything else should be on the objfile obstack. */
2205}
2206
2207/* See declaration. */
2208
2209void
2210dwarf2_per_objfile::free_cached_comp_units ()
2211{
2212 dwarf2_per_cu_data *per_cu = read_in_chain;
2213 dwarf2_per_cu_data **last_chain = &read_in_chain;
2214 while (per_cu != NULL)
2215 {
2216 dwarf2_per_cu_data *next_cu = per_cu->cu->read_in_chain;
2217
fcd3b13d 2218 delete per_cu->cu;
330cdd98
PA
2219 *last_chain = next_cu;
2220 per_cu = next_cu;
2221 }
2222}
2223
11ed8cad
TT
2224/* A helper class that calls free_cached_comp_units on
2225 destruction. */
2226
2227class free_cached_comp_units
2228{
2229public:
2230
2231 explicit free_cached_comp_units (dwarf2_per_objfile *per_objfile)
2232 : m_per_objfile (per_objfile)
2233 {
2234 }
2235
2236 ~free_cached_comp_units ()
2237 {
2238 m_per_objfile->free_cached_comp_units ();
2239 }
2240
2241 DISABLE_COPY_AND_ASSIGN (free_cached_comp_units);
2242
2243private:
2244
2245 dwarf2_per_objfile *m_per_objfile;
2246};
2247
c906108c 2248/* Try to locate the sections we need for DWARF 2 debugging
251d32d9
TG
2249 information and return true if we have enough to do something.
2250 NAMES points to the dwarf2 section names, or is NULL if the standard
2251 ELF names are used. */
c906108c
SS
2252
2253int
251d32d9
TG
2254dwarf2_has_info (struct objfile *objfile,
2255 const struct dwarf2_debug_sections *names)
c906108c 2256{
97cbe998
SDJ
2257 if (objfile->flags & OBJF_READNEVER)
2258 return 0;
2259
ed2dc618
SM
2260 struct dwarf2_per_objfile *dwarf2_per_objfile
2261 = get_dwarf2_per_objfile (objfile);
2262
2263 if (dwarf2_per_objfile == NULL)
be391dca
TT
2264 {
2265 /* Initialize per-objfile state. */
fd90ace4
YQ
2266 dwarf2_per_objfile
2267 = new (&objfile->objfile_obstack) struct dwarf2_per_objfile (objfile,
2268 names);
ed2dc618 2269 set_dwarf2_per_objfile (objfile, dwarf2_per_objfile);
be391dca 2270 }
73869dc2 2271 return (!dwarf2_per_objfile->info.is_virtual
049412e3 2272 && dwarf2_per_objfile->info.s.section != NULL
73869dc2 2273 && !dwarf2_per_objfile->abbrev.is_virtual
049412e3 2274 && dwarf2_per_objfile->abbrev.s.section != NULL);
73869dc2
DE
2275}
2276
2277/* Return the containing section of virtual section SECTION. */
2278
2279static struct dwarf2_section_info *
2280get_containing_section (const struct dwarf2_section_info *section)
2281{
2282 gdb_assert (section->is_virtual);
2283 return section->s.containing_section;
c906108c
SS
2284}
2285
a32a8923
DE
2286/* Return the bfd owner of SECTION. */
2287
2288static struct bfd *
2289get_section_bfd_owner (const struct dwarf2_section_info *section)
2290{
73869dc2
DE
2291 if (section->is_virtual)
2292 {
2293 section = get_containing_section (section);
2294 gdb_assert (!section->is_virtual);
2295 }
049412e3 2296 return section->s.section->owner;
a32a8923
DE
2297}
2298
2299/* Return the bfd section of SECTION.
2300 Returns NULL if the section is not present. */
2301
2302static asection *
2303get_section_bfd_section (const struct dwarf2_section_info *section)
2304{
73869dc2
DE
2305 if (section->is_virtual)
2306 {
2307 section = get_containing_section (section);
2308 gdb_assert (!section->is_virtual);
2309 }
049412e3 2310 return section->s.section;
a32a8923
DE
2311}
2312
2313/* Return the name of SECTION. */
2314
2315static const char *
2316get_section_name (const struct dwarf2_section_info *section)
2317{
2318 asection *sectp = get_section_bfd_section (section);
2319
2320 gdb_assert (sectp != NULL);
2321 return bfd_section_name (get_section_bfd_owner (section), sectp);
2322}
2323
2324/* Return the name of the file SECTION is in. */
2325
2326static const char *
2327get_section_file_name (const struct dwarf2_section_info *section)
2328{
2329 bfd *abfd = get_section_bfd_owner (section);
2330
2331 return bfd_get_filename (abfd);
2332}
2333
2334/* Return the id of SECTION.
2335 Returns 0 if SECTION doesn't exist. */
2336
2337static int
2338get_section_id (const struct dwarf2_section_info *section)
2339{
2340 asection *sectp = get_section_bfd_section (section);
2341
2342 if (sectp == NULL)
2343 return 0;
2344 return sectp->id;
2345}
2346
2347/* Return the flags of SECTION.
73869dc2 2348 SECTION (or containing section if this is a virtual section) must exist. */
a32a8923
DE
2349
2350static int
2351get_section_flags (const struct dwarf2_section_info *section)
2352{
2353 asection *sectp = get_section_bfd_section (section);
2354
2355 gdb_assert (sectp != NULL);
2356 return bfd_get_section_flags (sectp->owner, sectp);
2357}
2358
251d32d9
TG
2359/* When loading sections, we look either for uncompressed section or for
2360 compressed section names. */
233a11ab
CS
2361
2362static int
251d32d9
TG
2363section_is_p (const char *section_name,
2364 const struct dwarf2_section_names *names)
233a11ab 2365{
251d32d9
TG
2366 if (names->normal != NULL
2367 && strcmp (section_name, names->normal) == 0)
2368 return 1;
2369 if (names->compressed != NULL
2370 && strcmp (section_name, names->compressed) == 0)
2371 return 1;
2372 return 0;
233a11ab
CS
2373}
2374
330cdd98 2375/* See declaration. */
c906108c 2376
330cdd98
PA
2377void
2378dwarf2_per_objfile::locate_sections (bfd *abfd, asection *sectp,
2379 const dwarf2_debug_sections &names)
c906108c 2380{
dc7650b8 2381 flagword aflag = bfd_get_section_flags (abfd, sectp);
251d32d9 2382
dc7650b8
JK
2383 if ((aflag & SEC_HAS_CONTENTS) == 0)
2384 {
2385 }
330cdd98 2386 else if (section_is_p (sectp->name, &names.info))
c906108c 2387 {
330cdd98
PA
2388 this->info.s.section = sectp;
2389 this->info.size = bfd_get_section_size (sectp);
c906108c 2390 }
330cdd98 2391 else if (section_is_p (sectp->name, &names.abbrev))
c906108c 2392 {
330cdd98
PA
2393 this->abbrev.s.section = sectp;
2394 this->abbrev.size = bfd_get_section_size (sectp);
c906108c 2395 }
330cdd98 2396 else if (section_is_p (sectp->name, &names.line))
c906108c 2397 {
330cdd98
PA
2398 this->line.s.section = sectp;
2399 this->line.size = bfd_get_section_size (sectp);
c906108c 2400 }
330cdd98 2401 else if (section_is_p (sectp->name, &names.loc))
c906108c 2402 {
330cdd98
PA
2403 this->loc.s.section = sectp;
2404 this->loc.size = bfd_get_section_size (sectp);
c906108c 2405 }
330cdd98 2406 else if (section_is_p (sectp->name, &names.loclists))
43988095 2407 {
330cdd98
PA
2408 this->loclists.s.section = sectp;
2409 this->loclists.size = bfd_get_section_size (sectp);
43988095 2410 }
330cdd98 2411 else if (section_is_p (sectp->name, &names.macinfo))
c906108c 2412 {
330cdd98
PA
2413 this->macinfo.s.section = sectp;
2414 this->macinfo.size = bfd_get_section_size (sectp);
c906108c 2415 }
330cdd98 2416 else if (section_is_p (sectp->name, &names.macro))
cf2c3c16 2417 {
330cdd98
PA
2418 this->macro.s.section = sectp;
2419 this->macro.size = bfd_get_section_size (sectp);
cf2c3c16 2420 }
330cdd98 2421 else if (section_is_p (sectp->name, &names.str))
c906108c 2422 {
330cdd98
PA
2423 this->str.s.section = sectp;
2424 this->str.size = bfd_get_section_size (sectp);
c906108c 2425 }
330cdd98 2426 else if (section_is_p (sectp->name, &names.line_str))
43988095 2427 {
330cdd98
PA
2428 this->line_str.s.section = sectp;
2429 this->line_str.size = bfd_get_section_size (sectp);
43988095 2430 }
330cdd98 2431 else if (section_is_p (sectp->name, &names.addr))
3019eac3 2432 {
330cdd98
PA
2433 this->addr.s.section = sectp;
2434 this->addr.size = bfd_get_section_size (sectp);
3019eac3 2435 }
330cdd98 2436 else if (section_is_p (sectp->name, &names.frame))
b6af0555 2437 {
330cdd98
PA
2438 this->frame.s.section = sectp;
2439 this->frame.size = bfd_get_section_size (sectp);
b6af0555 2440 }
330cdd98 2441 else if (section_is_p (sectp->name, &names.eh_frame))
b6af0555 2442 {
330cdd98
PA
2443 this->eh_frame.s.section = sectp;
2444 this->eh_frame.size = bfd_get_section_size (sectp);
b6af0555 2445 }
330cdd98 2446 else if (section_is_p (sectp->name, &names.ranges))
af34e669 2447 {
330cdd98
PA
2448 this->ranges.s.section = sectp;
2449 this->ranges.size = bfd_get_section_size (sectp);
af34e669 2450 }
330cdd98 2451 else if (section_is_p (sectp->name, &names.rnglists))
43988095 2452 {
330cdd98
PA
2453 this->rnglists.s.section = sectp;
2454 this->rnglists.size = bfd_get_section_size (sectp);
43988095 2455 }
330cdd98 2456 else if (section_is_p (sectp->name, &names.types))
348e048f 2457 {
8b70b953
TT
2458 struct dwarf2_section_info type_section;
2459
2460 memset (&type_section, 0, sizeof (type_section));
049412e3 2461 type_section.s.section = sectp;
8b70b953
TT
2462 type_section.size = bfd_get_section_size (sectp);
2463
330cdd98 2464 VEC_safe_push (dwarf2_section_info_def, this->types,
8b70b953 2465 &type_section);
348e048f 2466 }
330cdd98 2467 else if (section_is_p (sectp->name, &names.gdb_index))
9291a0cd 2468 {
330cdd98
PA
2469 this->gdb_index.s.section = sectp;
2470 this->gdb_index.size = bfd_get_section_size (sectp);
9291a0cd 2471 }
927aa2e7
JK
2472 else if (section_is_p (sectp->name, &names.debug_names))
2473 {
2474 this->debug_names.s.section = sectp;
2475 this->debug_names.size = bfd_get_section_size (sectp);
2476 }
2477 else if (section_is_p (sectp->name, &names.debug_aranges))
2478 {
2479 this->debug_aranges.s.section = sectp;
2480 this->debug_aranges.size = bfd_get_section_size (sectp);
2481 }
dce234bc 2482
b4e1fd61 2483 if ((bfd_get_section_flags (abfd, sectp) & (SEC_LOAD | SEC_ALLOC))
72dca2f5 2484 && bfd_section_vma (abfd, sectp) == 0)
330cdd98 2485 this->has_section_at_zero = true;
c906108c
SS
2486}
2487
fceca515
DE
2488/* A helper function that decides whether a section is empty,
2489 or not present. */
9e0ac564
TT
2490
2491static int
19ac8c2e 2492dwarf2_section_empty_p (const struct dwarf2_section_info *section)
9e0ac564 2493{
73869dc2
DE
2494 if (section->is_virtual)
2495 return section->size == 0;
049412e3 2496 return section->s.section == NULL || section->size == 0;
9e0ac564
TT
2497}
2498
cd4fb1b2 2499/* See dwarf2read.h. */
c906108c 2500
cd4fb1b2
SM
2501void
2502dwarf2_read_section (struct objfile *objfile, dwarf2_section_info *info)
c906108c 2503{
a32a8923 2504 asection *sectp;
3019eac3 2505 bfd *abfd;
dce234bc 2506 gdb_byte *buf, *retbuf;
c906108c 2507
be391dca
TT
2508 if (info->readin)
2509 return;
dce234bc 2510 info->buffer = NULL;
be391dca 2511 info->readin = 1;
188dd5d6 2512
9e0ac564 2513 if (dwarf2_section_empty_p (info))
dce234bc 2514 return;
c906108c 2515
a32a8923 2516 sectp = get_section_bfd_section (info);
3019eac3 2517
73869dc2
DE
2518 /* If this is a virtual section we need to read in the real one first. */
2519 if (info->is_virtual)
2520 {
2521 struct dwarf2_section_info *containing_section =
2522 get_containing_section (info);
2523
2524 gdb_assert (sectp != NULL);
2525 if ((sectp->flags & SEC_RELOC) != 0)
2526 {
2527 error (_("Dwarf Error: DWP format V2 with relocations is not"
2528 " supported in section %s [in module %s]"),
2529 get_section_name (info), get_section_file_name (info));
2530 }
2531 dwarf2_read_section (objfile, containing_section);
2532 /* Other code should have already caught virtual sections that don't
2533 fit. */
2534 gdb_assert (info->virtual_offset + info->size
2535 <= containing_section->size);
2536 /* If the real section is empty or there was a problem reading the
2537 section we shouldn't get here. */
2538 gdb_assert (containing_section->buffer != NULL);
2539 info->buffer = containing_section->buffer + info->virtual_offset;
2540 return;
2541 }
2542
4bf44c1c
TT
2543 /* If the section has relocations, we must read it ourselves.
2544 Otherwise we attach it to the BFD. */
2545 if ((sectp->flags & SEC_RELOC) == 0)
dce234bc 2546 {
d521ce57 2547 info->buffer = gdb_bfd_map_section (sectp, &info->size);
4bf44c1c 2548 return;
dce234bc 2549 }
dce234bc 2550
224c3ddb 2551 buf = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, info->size);
4bf44c1c 2552 info->buffer = buf;
dce234bc
PP
2553
2554 /* When debugging .o files, we may need to apply relocations; see
2555 http://sourceware.org/ml/gdb-patches/2002-04/msg00136.html .
2556 We never compress sections in .o files, so we only need to
2557 try this when the section is not compressed. */
ac8035ab 2558 retbuf = symfile_relocate_debug_section (objfile, sectp, buf);
dce234bc
PP
2559 if (retbuf != NULL)
2560 {
2561 info->buffer = retbuf;
2562 return;
2563 }
2564
a32a8923
DE
2565 abfd = get_section_bfd_owner (info);
2566 gdb_assert (abfd != NULL);
2567
dce234bc
PP
2568 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
2569 || bfd_bread (buf, info->size, abfd) != info->size)
19ac8c2e
DE
2570 {
2571 error (_("Dwarf Error: Can't read DWARF data"
2572 " in section %s [in module %s]"),
2573 bfd_section_name (abfd, sectp), bfd_get_filename (abfd));
2574 }
dce234bc
PP
2575}
2576
9e0ac564
TT
2577/* A helper function that returns the size of a section in a safe way.
2578 If you are positive that the section has been read before using the
2579 size, then it is safe to refer to the dwarf2_section_info object's
2580 "size" field directly. In other cases, you must call this
2581 function, because for compressed sections the size field is not set
2582 correctly until the section has been read. */
2583
2584static bfd_size_type
2585dwarf2_section_size (struct objfile *objfile,
2586 struct dwarf2_section_info *info)
2587{
2588 if (!info->readin)
2589 dwarf2_read_section (objfile, info);
2590 return info->size;
2591}
2592
dce234bc 2593/* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
0963b4bd 2594 SECTION_NAME. */
af34e669 2595
dce234bc 2596void
3017a003
TG
2597dwarf2_get_section_info (struct objfile *objfile,
2598 enum dwarf2_section_enum sect,
d521ce57 2599 asection **sectp, const gdb_byte **bufp,
dce234bc
PP
2600 bfd_size_type *sizep)
2601{
2602 struct dwarf2_per_objfile *data
9a3c8263
SM
2603 = (struct dwarf2_per_objfile *) objfile_data (objfile,
2604 dwarf2_objfile_data_key);
dce234bc 2605 struct dwarf2_section_info *info;
a3b2a86b
TT
2606
2607 /* We may see an objfile without any DWARF, in which case we just
2608 return nothing. */
2609 if (data == NULL)
2610 {
2611 *sectp = NULL;
2612 *bufp = NULL;
2613 *sizep = 0;
2614 return;
2615 }
3017a003
TG
2616 switch (sect)
2617 {
2618 case DWARF2_DEBUG_FRAME:
2619 info = &data->frame;
2620 break;
2621 case DWARF2_EH_FRAME:
2622 info = &data->eh_frame;
2623 break;
2624 default:
2625 gdb_assert_not_reached ("unexpected section");
2626 }
dce234bc 2627
9e0ac564 2628 dwarf2_read_section (objfile, info);
dce234bc 2629
a32a8923 2630 *sectp = get_section_bfd_section (info);
dce234bc
PP
2631 *bufp = info->buffer;
2632 *sizep = info->size;
2633}
2634
36586728
TT
2635/* A helper function to find the sections for a .dwz file. */
2636
2637static void
2638locate_dwz_sections (bfd *abfd, asection *sectp, void *arg)
2639{
9a3c8263 2640 struct dwz_file *dwz_file = (struct dwz_file *) arg;
36586728
TT
2641
2642 /* Note that we only support the standard ELF names, because .dwz
2643 is ELF-only (at the time of writing). */
2644 if (section_is_p (sectp->name, &dwarf2_elf_names.abbrev))
2645 {
049412e3 2646 dwz_file->abbrev.s.section = sectp;
36586728
TT
2647 dwz_file->abbrev.size = bfd_get_section_size (sectp);
2648 }
2649 else if (section_is_p (sectp->name, &dwarf2_elf_names.info))
2650 {
049412e3 2651 dwz_file->info.s.section = sectp;
36586728
TT
2652 dwz_file->info.size = bfd_get_section_size (sectp);
2653 }
2654 else if (section_is_p (sectp->name, &dwarf2_elf_names.str))
2655 {
049412e3 2656 dwz_file->str.s.section = sectp;
36586728
TT
2657 dwz_file->str.size = bfd_get_section_size (sectp);
2658 }
2659 else if (section_is_p (sectp->name, &dwarf2_elf_names.line))
2660 {
049412e3 2661 dwz_file->line.s.section = sectp;
36586728
TT
2662 dwz_file->line.size = bfd_get_section_size (sectp);
2663 }
2664 else if (section_is_p (sectp->name, &dwarf2_elf_names.macro))
2665 {
049412e3 2666 dwz_file->macro.s.section = sectp;
36586728
TT
2667 dwz_file->macro.size = bfd_get_section_size (sectp);
2668 }
2ec9a5e0
TT
2669 else if (section_is_p (sectp->name, &dwarf2_elf_names.gdb_index))
2670 {
049412e3 2671 dwz_file->gdb_index.s.section = sectp;
2ec9a5e0
TT
2672 dwz_file->gdb_index.size = bfd_get_section_size (sectp);
2673 }
927aa2e7
JK
2674 else if (section_is_p (sectp->name, &dwarf2_elf_names.debug_names))
2675 {
2676 dwz_file->debug_names.s.section = sectp;
2677 dwz_file->debug_names.size = bfd_get_section_size (sectp);
2678 }
36586728
TT
2679}
2680
4db1a1dc
TT
2681/* Open the separate '.dwz' debug file, if needed. Return NULL if
2682 there is no .gnu_debugaltlink section in the file. Error if there
2683 is such a section but the file cannot be found. */
36586728
TT
2684
2685static struct dwz_file *
ed2dc618 2686dwarf2_get_dwz_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
36586728 2687{
36586728 2688 const char *filename;
acd13123 2689 bfd_size_type buildid_len_arg;
dc294be5
TT
2690 size_t buildid_len;
2691 bfd_byte *buildid;
36586728
TT
2692
2693 if (dwarf2_per_objfile->dwz_file != NULL)
7ff8cb8c 2694 return dwarf2_per_objfile->dwz_file.get ();
36586728 2695
4db1a1dc 2696 bfd_set_error (bfd_error_no_error);
791afaa2
TT
2697 gdb::unique_xmalloc_ptr<char> data
2698 (bfd_get_alt_debug_link_info (dwarf2_per_objfile->objfile->obfd,
2699 &buildid_len_arg, &buildid));
4db1a1dc
TT
2700 if (data == NULL)
2701 {
2702 if (bfd_get_error () == bfd_error_no_error)
2703 return NULL;
2704 error (_("could not read '.gnu_debugaltlink' section: %s"),
2705 bfd_errmsg (bfd_get_error ()));
2706 }
791afaa2
TT
2707
2708 gdb::unique_xmalloc_ptr<bfd_byte> buildid_holder (buildid);
36586728 2709
acd13123
TT
2710 buildid_len = (size_t) buildid_len_arg;
2711
791afaa2 2712 filename = data.get ();
d721ba37
PA
2713
2714 std::string abs_storage;
36586728
TT
2715 if (!IS_ABSOLUTE_PATH (filename))
2716 {
14278e1f
TT
2717 gdb::unique_xmalloc_ptr<char> abs
2718 = gdb_realpath (objfile_name (dwarf2_per_objfile->objfile));
36586728 2719
14278e1f 2720 abs_storage = ldirname (abs.get ()) + SLASH_STRING + filename;
d721ba37 2721 filename = abs_storage.c_str ();
36586728
TT
2722 }
2723
dc294be5
TT
2724 /* First try the file name given in the section. If that doesn't
2725 work, try to use the build-id instead. */
192b62ce 2726 gdb_bfd_ref_ptr dwz_bfd (gdb_bfd_open (filename, gnutarget, -1));
dc294be5 2727 if (dwz_bfd != NULL)
36586728 2728 {
192b62ce 2729 if (!build_id_verify (dwz_bfd.get (), buildid_len, buildid))
0f58c9e8 2730 dwz_bfd.reset (nullptr);
36586728
TT
2731 }
2732
dc294be5
TT
2733 if (dwz_bfd == NULL)
2734 dwz_bfd = build_id_to_debug_bfd (buildid_len, buildid);
2735
2736 if (dwz_bfd == NULL)
2737 error (_("could not find '.gnu_debugaltlink' file for %s"),
2738 objfile_name (dwarf2_per_objfile->objfile));
2739
7ff8cb8c
TT
2740 std::unique_ptr<struct dwz_file> result
2741 (new struct dwz_file (std::move (dwz_bfd)));
36586728 2742
7ff8cb8c
TT
2743 bfd_map_over_sections (result->dwz_bfd.get (), locate_dwz_sections,
2744 result.get ());
36586728 2745
7ff8cb8c
TT
2746 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd,
2747 result->dwz_bfd.get ());
2748 dwarf2_per_objfile->dwz_file = std::move (result);
2749 return dwarf2_per_objfile->dwz_file.get ();
36586728 2750}
9291a0cd 2751\f
7b9f3c50
DE
2752/* DWARF quick_symbols_functions support. */
2753
2754/* TUs can share .debug_line entries, and there can be a lot more TUs than
2755 unique line tables, so we maintain a separate table of all .debug_line
2756 derived entries to support the sharing.
2757 All the quick functions need is the list of file names. We discard the
2758 line_header when we're done and don't need to record it here. */
2759struct quick_file_names
2760{
094b34ac
DE
2761 /* The data used to construct the hash key. */
2762 struct stmt_list_hash hash;
7b9f3c50
DE
2763
2764 /* The number of entries in file_names, real_names. */
2765 unsigned int num_file_names;
2766
2767 /* The file names from the line table, after being run through
2768 file_full_name. */
2769 const char **file_names;
2770
2771 /* The file names from the line table after being run through
2772 gdb_realpath. These are computed lazily. */
2773 const char **real_names;
2774};
2775
2776/* When using the index (and thus not using psymtabs), each CU has an
2777 object of this type. This is used to hold information needed by
2778 the various "quick" methods. */
2779struct dwarf2_per_cu_quick_data
2780{
2781 /* The file table. This can be NULL if there was no file table
2782 or it's currently not read in.
2783 NOTE: This points into dwarf2_per_objfile->quick_file_names_table. */
2784 struct quick_file_names *file_names;
2785
2786 /* The corresponding symbol table. This is NULL if symbols for this
2787 CU have not yet been read. */
43f3e411 2788 struct compunit_symtab *compunit_symtab;
7b9f3c50
DE
2789
2790 /* A temporary mark bit used when iterating over all CUs in
2791 expand_symtabs_matching. */
2792 unsigned int mark : 1;
2793
2794 /* True if we've tried to read the file table and found there isn't one.
2795 There will be no point in trying to read it again next time. */
2796 unsigned int no_file_data : 1;
2797};
2798
094b34ac
DE
2799/* Utility hash function for a stmt_list_hash. */
2800
2801static hashval_t
2802hash_stmt_list_entry (const struct stmt_list_hash *stmt_list_hash)
2803{
2804 hashval_t v = 0;
2805
2806 if (stmt_list_hash->dwo_unit != NULL)
2807 v += (uintptr_t) stmt_list_hash->dwo_unit->dwo_file;
9c541725 2808 v += to_underlying (stmt_list_hash->line_sect_off);
094b34ac
DE
2809 return v;
2810}
2811
2812/* Utility equality function for a stmt_list_hash. */
2813
2814static int
2815eq_stmt_list_entry (const struct stmt_list_hash *lhs,
2816 const struct stmt_list_hash *rhs)
2817{
2818 if ((lhs->dwo_unit != NULL) != (rhs->dwo_unit != NULL))
2819 return 0;
2820 if (lhs->dwo_unit != NULL
2821 && lhs->dwo_unit->dwo_file != rhs->dwo_unit->dwo_file)
2822 return 0;
2823
9c541725 2824 return lhs->line_sect_off == rhs->line_sect_off;
094b34ac
DE
2825}
2826
7b9f3c50
DE
2827/* Hash function for a quick_file_names. */
2828
2829static hashval_t
2830hash_file_name_entry (const void *e)
2831{
9a3c8263
SM
2832 const struct quick_file_names *file_data
2833 = (const struct quick_file_names *) e;
7b9f3c50 2834
094b34ac 2835 return hash_stmt_list_entry (&file_data->hash);
7b9f3c50
DE
2836}
2837
2838/* Equality function for a quick_file_names. */
2839
2840static int
2841eq_file_name_entry (const void *a, const void *b)
2842{
9a3c8263
SM
2843 const struct quick_file_names *ea = (const struct quick_file_names *) a;
2844 const struct quick_file_names *eb = (const struct quick_file_names *) b;
7b9f3c50 2845
094b34ac 2846 return eq_stmt_list_entry (&ea->hash, &eb->hash);
7b9f3c50
DE
2847}
2848
2849/* Delete function for a quick_file_names. */
2850
2851static void
2852delete_file_name_entry (void *e)
2853{
9a3c8263 2854 struct quick_file_names *file_data = (struct quick_file_names *) e;
7b9f3c50
DE
2855 int i;
2856
2857 for (i = 0; i < file_data->num_file_names; ++i)
2858 {
2859 xfree ((void*) file_data->file_names[i]);
2860 if (file_data->real_names)
2861 xfree ((void*) file_data->real_names[i]);
2862 }
2863
2864 /* The space for the struct itself lives on objfile_obstack,
2865 so we don't free it here. */
2866}
2867
2868/* Create a quick_file_names hash table. */
2869
2870static htab_t
2871create_quick_file_names_table (unsigned int nr_initial_entries)
2872{
2873 return htab_create_alloc (nr_initial_entries,
2874 hash_file_name_entry, eq_file_name_entry,
2875 delete_file_name_entry, xcalloc, xfree);
2876}
9291a0cd 2877
918dd910
JK
2878/* Read in PER_CU->CU. This function is unrelated to symtabs, symtab would
2879 have to be created afterwards. You should call age_cached_comp_units after
2880 processing PER_CU->CU. dw2_setup must have been already called. */
2881
2882static void
58f0c718 2883load_cu (struct dwarf2_per_cu_data *per_cu, bool skip_partial)
918dd910 2884{
3019eac3 2885 if (per_cu->is_debug_types)
e5fe5e75 2886 load_full_type_unit (per_cu);
918dd910 2887 else
58f0c718 2888 load_full_comp_unit (per_cu, skip_partial, language_minimal);
918dd910 2889
cc12ce38
DE
2890 if (per_cu->cu == NULL)
2891 return; /* Dummy CU. */
2dc860c0
DE
2892
2893 dwarf2_find_base_address (per_cu->cu->dies, per_cu->cu);
918dd910
JK
2894}
2895
a0f42c21 2896/* Read in the symbols for PER_CU. */
2fdf6df6 2897
9291a0cd 2898static void
58f0c718 2899dw2_do_instantiate_symtab (struct dwarf2_per_cu_data *per_cu, bool skip_partial)
9291a0cd 2900{
ed2dc618 2901 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
9291a0cd 2902
f4dc4d17
DE
2903 /* Skip type_unit_groups, reading the type units they contain
2904 is handled elsewhere. */
2905 if (IS_TYPE_UNIT_GROUP (per_cu))
2906 return;
2907
b303c6f6
AB
2908 /* The destructor of dwarf2_queue_guard frees any entries left on
2909 the queue. After this point we're guaranteed to leave this function
2910 with the dwarf queue empty. */
2911 dwarf2_queue_guard q_guard;
9291a0cd 2912
95554aad 2913 if (dwarf2_per_objfile->using_index
43f3e411 2914 ? per_cu->v.quick->compunit_symtab == NULL
95554aad
TT
2915 : (per_cu->v.psymtab == NULL || !per_cu->v.psymtab->readin))
2916 {
2917 queue_comp_unit (per_cu, language_minimal);
58f0c718 2918 load_cu (per_cu, skip_partial);
89e63ee4
DE
2919
2920 /* If we just loaded a CU from a DWO, and we're working with an index
2921 that may badly handle TUs, load all the TUs in that DWO as well.
2922 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
2923 if (!per_cu->is_debug_types
cc12ce38 2924 && per_cu->cu != NULL
89e63ee4
DE
2925 && per_cu->cu->dwo_unit != NULL
2926 && dwarf2_per_objfile->index_table != NULL
2927 && dwarf2_per_objfile->index_table->version <= 7
2928 /* DWP files aren't supported yet. */
ed2dc618 2929 && get_dwp_file (dwarf2_per_objfile) == NULL)
89e63ee4 2930 queue_and_load_all_dwo_tus (per_cu);
95554aad 2931 }
9291a0cd 2932
ed2dc618 2933 process_queue (dwarf2_per_objfile);
9291a0cd
TT
2934
2935 /* Age the cache, releasing compilation units that have not
2936 been used recently. */
ed2dc618 2937 age_cached_comp_units (dwarf2_per_objfile);
9291a0cd
TT
2938}
2939
2940/* Ensure that the symbols for PER_CU have been read in. OBJFILE is
2941 the objfile from which this CU came. Returns the resulting symbol
2942 table. */
2fdf6df6 2943
43f3e411 2944static struct compunit_symtab *
58f0c718 2945dw2_instantiate_symtab (struct dwarf2_per_cu_data *per_cu, bool skip_partial)
9291a0cd 2946{
ed2dc618
SM
2947 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
2948
95554aad 2949 gdb_assert (dwarf2_per_objfile->using_index);
43f3e411 2950 if (!per_cu->v.quick->compunit_symtab)
9291a0cd 2951 {
11ed8cad 2952 free_cached_comp_units freer (dwarf2_per_objfile);
c83dd867 2953 scoped_restore decrementer = increment_reading_symtab ();
58f0c718 2954 dw2_do_instantiate_symtab (per_cu, skip_partial);
ed2dc618 2955 process_cu_includes (dwarf2_per_objfile);
9291a0cd 2956 }
f194fefb 2957
43f3e411 2958 return per_cu->v.quick->compunit_symtab;
9291a0cd
TT
2959}
2960
ff4c9fec 2961/* See declaration. */
f4dc4d17 2962
ff4c9fec
SM
2963dwarf2_per_cu_data *
2964dwarf2_per_objfile::get_cutu (int index)
2965{
b76e467d 2966 if (index >= this->all_comp_units.size ())
ff4c9fec 2967 {
b76e467d 2968 index -= this->all_comp_units.size ();
b2bdb8cf 2969 gdb_assert (index < this->all_type_units.size ());
ff4c9fec
SM
2970 return &this->all_type_units[index]->per_cu;
2971 }
f4dc4d17 2972
ff4c9fec
SM
2973 return this->all_comp_units[index];
2974}
f4dc4d17 2975
ff4c9fec 2976/* See declaration. */
2fdf6df6 2977
ff4c9fec
SM
2978dwarf2_per_cu_data *
2979dwarf2_per_objfile::get_cu (int index)
1fd400ff 2980{
b76e467d 2981 gdb_assert (index >= 0 && index < this->all_comp_units.size ());
f4dc4d17 2982
ff4c9fec 2983 return this->all_comp_units[index];
f4dc4d17
DE
2984}
2985
ff4c9fec 2986/* See declaration. */
f4dc4d17 2987
ff4c9fec
SM
2988signatured_type *
2989dwarf2_per_objfile::get_tu (int index)
f4dc4d17 2990{
b2bdb8cf 2991 gdb_assert (index >= 0 && index < this->all_type_units.size ());
f4dc4d17 2992
ff4c9fec 2993 return this->all_type_units[index];
1fd400ff
TT
2994}
2995
4b514bc8
JK
2996/* Return a new dwarf2_per_cu_data allocated on OBJFILE's
2997 objfile_obstack, and constructed with the specified field
2998 values. */
2999
3000static dwarf2_per_cu_data *
ed2dc618 3001create_cu_from_index_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
4b514bc8
JK
3002 struct dwarf2_section_info *section,
3003 int is_dwz,
3004 sect_offset sect_off, ULONGEST length)
3005{
ed2dc618 3006 struct objfile *objfile = dwarf2_per_objfile->objfile;
4b514bc8
JK
3007 dwarf2_per_cu_data *the_cu
3008 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3009 struct dwarf2_per_cu_data);
3010 the_cu->sect_off = sect_off;
3011 the_cu->length = length;
e3b94546 3012 the_cu->dwarf2_per_objfile = dwarf2_per_objfile;
4b514bc8
JK
3013 the_cu->section = section;
3014 the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3015 struct dwarf2_per_cu_quick_data);
3016 the_cu->is_dwz = is_dwz;
3017 return the_cu;
3018}
3019
2ec9a5e0
TT
3020/* A helper for create_cus_from_index that handles a given list of
3021 CUs. */
2fdf6df6 3022
74a0d9f6 3023static void
12359b5e 3024create_cus_from_index_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
2ec9a5e0
TT
3025 const gdb_byte *cu_list, offset_type n_elements,
3026 struct dwarf2_section_info *section,
b76e467d 3027 int is_dwz)
9291a0cd 3028{
12359b5e 3029 for (offset_type i = 0; i < n_elements; i += 2)
9291a0cd 3030 {
74a0d9f6 3031 gdb_static_assert (sizeof (ULONGEST) >= 8);
9c541725
PA
3032
3033 sect_offset sect_off
3034 = (sect_offset) extract_unsigned_integer (cu_list, 8, BFD_ENDIAN_LITTLE);
3035 ULONGEST length = extract_unsigned_integer (cu_list + 8, 8, BFD_ENDIAN_LITTLE);
9291a0cd
TT
3036 cu_list += 2 * 8;
3037
b76e467d 3038 dwarf2_per_cu_data *per_cu
ed2dc618
SM
3039 = create_cu_from_index_list (dwarf2_per_objfile, section, is_dwz,
3040 sect_off, length);
b76e467d 3041 dwarf2_per_objfile->all_comp_units.push_back (per_cu);
9291a0cd 3042 }
9291a0cd
TT
3043}
3044
2ec9a5e0 3045/* Read the CU list from the mapped index, and use it to create all
74a0d9f6 3046 the CU objects for this objfile. */
2ec9a5e0 3047
74a0d9f6 3048static void
12359b5e 3049create_cus_from_index (struct dwarf2_per_objfile *dwarf2_per_objfile,
2ec9a5e0
TT
3050 const gdb_byte *cu_list, offset_type cu_list_elements,
3051 const gdb_byte *dwz_list, offset_type dwz_elements)
3052{
b76e467d
SM
3053 gdb_assert (dwarf2_per_objfile->all_comp_units.empty ());
3054 dwarf2_per_objfile->all_comp_units.reserve
3055 ((cu_list_elements + dwz_elements) / 2);
2ec9a5e0 3056
12359b5e 3057 create_cus_from_index_list (dwarf2_per_objfile, cu_list, cu_list_elements,
b76e467d 3058 &dwarf2_per_objfile->info, 0);
2ec9a5e0
TT
3059
3060 if (dwz_elements == 0)
74a0d9f6 3061 return;
2ec9a5e0 3062
12359b5e
SM
3063 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
3064 create_cus_from_index_list (dwarf2_per_objfile, dwz_list, dwz_elements,
b76e467d 3065 &dwz->info, 1);
2ec9a5e0
TT
3066}
3067
1fd400ff 3068/* Create the signatured type hash table from the index. */
673bfd45 3069
74a0d9f6 3070static void
12359b5e
SM
3071create_signatured_type_table_from_index
3072 (struct dwarf2_per_objfile *dwarf2_per_objfile,
3073 struct dwarf2_section_info *section,
3074 const gdb_byte *bytes,
3075 offset_type elements)
1fd400ff 3076{
12359b5e 3077 struct objfile *objfile = dwarf2_per_objfile->objfile;
1fd400ff 3078
b2bdb8cf
SM
3079 gdb_assert (dwarf2_per_objfile->all_type_units.empty ());
3080 dwarf2_per_objfile->all_type_units.reserve (elements / 3);
1fd400ff 3081
12359b5e 3082 htab_t sig_types_hash = allocate_signatured_type_table (objfile);
1fd400ff 3083
12359b5e 3084 for (offset_type i = 0; i < elements; i += 3)
1fd400ff 3085 {
52dc124a 3086 struct signatured_type *sig_type;
9c541725 3087 ULONGEST signature;
1fd400ff 3088 void **slot;
9c541725 3089 cu_offset type_offset_in_tu;
1fd400ff 3090
74a0d9f6 3091 gdb_static_assert (sizeof (ULONGEST) >= 8);
9c541725
PA
3092 sect_offset sect_off
3093 = (sect_offset) extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
3094 type_offset_in_tu
3095 = (cu_offset) extract_unsigned_integer (bytes + 8, 8,
3096 BFD_ENDIAN_LITTLE);
1fd400ff
TT
3097 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
3098 bytes += 3 * 8;
3099
52dc124a 3100 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1fd400ff 3101 struct signatured_type);
52dc124a 3102 sig_type->signature = signature;
9c541725 3103 sig_type->type_offset_in_tu = type_offset_in_tu;
3019eac3 3104 sig_type->per_cu.is_debug_types = 1;
8a0459fd 3105 sig_type->per_cu.section = section;
9c541725 3106 sig_type->per_cu.sect_off = sect_off;
e3b94546 3107 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
52dc124a 3108 sig_type->per_cu.v.quick
1fd400ff
TT
3109 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3110 struct dwarf2_per_cu_quick_data);
3111
52dc124a
DE
3112 slot = htab_find_slot (sig_types_hash, sig_type, INSERT);
3113 *slot = sig_type;
1fd400ff 3114
b2bdb8cf 3115 dwarf2_per_objfile->all_type_units.push_back (sig_type);
1fd400ff
TT
3116 }
3117
673bfd45 3118 dwarf2_per_objfile->signatured_types = sig_types_hash;
1fd400ff
TT
3119}
3120
927aa2e7
JK
3121/* Create the signatured type hash table from .debug_names. */
3122
3123static void
3124create_signatured_type_table_from_debug_names
ed2dc618 3125 (struct dwarf2_per_objfile *dwarf2_per_objfile,
927aa2e7
JK
3126 const mapped_debug_names &map,
3127 struct dwarf2_section_info *section,
3128 struct dwarf2_section_info *abbrev_section)
3129{
ed2dc618
SM
3130 struct objfile *objfile = dwarf2_per_objfile->objfile;
3131
927aa2e7
JK
3132 dwarf2_read_section (objfile, section);
3133 dwarf2_read_section (objfile, abbrev_section);
3134
b2bdb8cf
SM
3135 gdb_assert (dwarf2_per_objfile->all_type_units.empty ());
3136 dwarf2_per_objfile->all_type_units.reserve (map.tu_count);
927aa2e7
JK
3137
3138 htab_t sig_types_hash = allocate_signatured_type_table (objfile);
3139
3140 for (uint32_t i = 0; i < map.tu_count; ++i)
3141 {
3142 struct signatured_type *sig_type;
927aa2e7 3143 void **slot;
927aa2e7
JK
3144
3145 sect_offset sect_off
3146 = (sect_offset) (extract_unsigned_integer
3147 (map.tu_table_reordered + i * map.offset_size,
3148 map.offset_size,
3149 map.dwarf5_byte_order));
3150
3151 comp_unit_head cu_header;
ed2dc618
SM
3152 read_and_check_comp_unit_head (dwarf2_per_objfile, &cu_header, section,
3153 abbrev_section,
927aa2e7
JK
3154 section->buffer + to_underlying (sect_off),
3155 rcuh_kind::TYPE);
3156
3157 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3158 struct signatured_type);
3159 sig_type->signature = cu_header.signature;
3160 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
3161 sig_type->per_cu.is_debug_types = 1;
3162 sig_type->per_cu.section = section;
3163 sig_type->per_cu.sect_off = sect_off;
e3b94546 3164 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
927aa2e7
JK
3165 sig_type->per_cu.v.quick
3166 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3167 struct dwarf2_per_cu_quick_data);
3168
3169 slot = htab_find_slot (sig_types_hash, sig_type, INSERT);
3170 *slot = sig_type;
3171
b2bdb8cf 3172 dwarf2_per_objfile->all_type_units.push_back (sig_type);
927aa2e7
JK
3173 }
3174
3175 dwarf2_per_objfile->signatured_types = sig_types_hash;
3176}
3177
9291a0cd
TT
3178/* Read the address map data from the mapped index, and use it to
3179 populate the objfile's psymtabs_addrmap. */
2fdf6df6 3180
9291a0cd 3181static void
ed2dc618
SM
3182create_addrmap_from_index (struct dwarf2_per_objfile *dwarf2_per_objfile,
3183 struct mapped_index *index)
9291a0cd 3184{
ed2dc618 3185 struct objfile *objfile = dwarf2_per_objfile->objfile;
3e29f34a 3186 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9291a0cd 3187 const gdb_byte *iter, *end;
9291a0cd 3188 struct addrmap *mutable_map;
9291a0cd
TT
3189 CORE_ADDR baseaddr;
3190
8268c778
PA
3191 auto_obstack temp_obstack;
3192
9291a0cd
TT
3193 mutable_map = addrmap_create_mutable (&temp_obstack);
3194
f00a2de2
PA
3195 iter = index->address_table.data ();
3196 end = iter + index->address_table.size ();
9291a0cd
TT
3197
3198 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
3199
3200 while (iter < end)
3201 {
3202 ULONGEST hi, lo, cu_index;
3203 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
3204 iter += 8;
3205 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
3206 iter += 8;
3207 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
3208 iter += 4;
f652bce2 3209
24a55014 3210 if (lo > hi)
f652bce2 3211 {
b98664d3 3212 complaint (_(".gdb_index address table has invalid range (%s - %s)"),
c0cd8254 3213 hex_string (lo), hex_string (hi));
24a55014 3214 continue;
f652bce2 3215 }
24a55014 3216
b76e467d 3217 if (cu_index >= dwarf2_per_objfile->all_comp_units.size ())
f652bce2 3218 {
b98664d3 3219 complaint (_(".gdb_index address table has invalid CU number %u"),
f652bce2 3220 (unsigned) cu_index);
24a55014 3221 continue;
f652bce2 3222 }
24a55014 3223
79748972
TT
3224 lo = gdbarch_adjust_dwarf2_addr (gdbarch, lo + baseaddr) - baseaddr;
3225 hi = gdbarch_adjust_dwarf2_addr (gdbarch, hi + baseaddr) - baseaddr;
ed2dc618 3226 addrmap_set_empty (mutable_map, lo, hi - 1,
ff4c9fec 3227 dwarf2_per_objfile->get_cu (cu_index));
9291a0cd
TT
3228 }
3229
d320c2b5 3230 objfile->partial_symtabs->psymtabs_addrmap
5923a04c 3231 = addrmap_create_fixed (mutable_map, objfile->partial_symtabs->obstack ());
9291a0cd
TT
3232}
3233
927aa2e7
JK
3234/* Read the address map data from DWARF-5 .debug_aranges, and use it to
3235 populate the objfile's psymtabs_addrmap. */
3236
3237static void
ed2dc618 3238create_addrmap_from_aranges (struct dwarf2_per_objfile *dwarf2_per_objfile,
927aa2e7
JK
3239 struct dwarf2_section_info *section)
3240{
ed2dc618 3241 struct objfile *objfile = dwarf2_per_objfile->objfile;
927aa2e7
JK
3242 bfd *abfd = objfile->obfd;
3243 struct gdbarch *gdbarch = get_objfile_arch (objfile);
3244 const CORE_ADDR baseaddr = ANOFFSET (objfile->section_offsets,
3245 SECT_OFF_TEXT (objfile));
3246
3247 auto_obstack temp_obstack;
3248 addrmap *mutable_map = addrmap_create_mutable (&temp_obstack);
3249
3250 std::unordered_map<sect_offset,
3251 dwarf2_per_cu_data *,
3252 gdb::hash_enum<sect_offset>>
3253 debug_info_offset_to_per_cu;
b76e467d 3254 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
927aa2e7 3255 {
927aa2e7
JK
3256 const auto insertpair
3257 = debug_info_offset_to_per_cu.emplace (per_cu->sect_off, per_cu);
3258 if (!insertpair.second)
3259 {
3260 warning (_("Section .debug_aranges in %s has duplicate "
9d8780f0
SM
3261 "debug_info_offset %s, ignoring .debug_aranges."),
3262 objfile_name (objfile), sect_offset_str (per_cu->sect_off));
927aa2e7
JK
3263 return;
3264 }
3265 }
3266
3267 dwarf2_read_section (objfile, section);
3268
3269 const bfd_endian dwarf5_byte_order = gdbarch_byte_order (gdbarch);
3270
3271 const gdb_byte *addr = section->buffer;
3272
3273 while (addr < section->buffer + section->size)
3274 {
3275 const gdb_byte *const entry_addr = addr;
3276 unsigned int bytes_read;
3277
3278 const LONGEST entry_length = read_initial_length (abfd, addr,
3279 &bytes_read);
3280 addr += bytes_read;
3281
3282 const gdb_byte *const entry_end = addr + entry_length;
3283 const bool dwarf5_is_dwarf64 = bytes_read != 4;
3284 const uint8_t offset_size = dwarf5_is_dwarf64 ? 8 : 4;
3285 if (addr + entry_length > section->buffer + section->size)
3286 {
3287 warning (_("Section .debug_aranges in %s entry at offset %zu "
3288 "length %s exceeds section length %s, "
3289 "ignoring .debug_aranges."),
3290 objfile_name (objfile), entry_addr - section->buffer,
3291 plongest (bytes_read + entry_length),
3292 pulongest (section->size));
3293 return;
3294 }
3295
3296 /* The version number. */
3297 const uint16_t version = read_2_bytes (abfd, addr);
3298 addr += 2;
3299 if (version != 2)
3300 {
3301 warning (_("Section .debug_aranges in %s entry at offset %zu "
3302 "has unsupported version %d, ignoring .debug_aranges."),
3303 objfile_name (objfile), entry_addr - section->buffer,
3304 version);
3305 return;
3306 }
3307
3308 const uint64_t debug_info_offset
3309 = extract_unsigned_integer (addr, offset_size, dwarf5_byte_order);
3310 addr += offset_size;
3311 const auto per_cu_it
3312 = debug_info_offset_to_per_cu.find (sect_offset (debug_info_offset));
3313 if (per_cu_it == debug_info_offset_to_per_cu.cend ())
3314 {
3315 warning (_("Section .debug_aranges in %s entry at offset %zu "
3316 "debug_info_offset %s does not exists, "
3317 "ignoring .debug_aranges."),
3318 objfile_name (objfile), entry_addr - section->buffer,
3319 pulongest (debug_info_offset));
3320 return;
3321 }
3322 dwarf2_per_cu_data *const per_cu = per_cu_it->second;
3323
3324 const uint8_t address_size = *addr++;
3325 if (address_size < 1 || address_size > 8)
3326 {
3327 warning (_("Section .debug_aranges in %s entry at offset %zu "
3328 "address_size %u is invalid, ignoring .debug_aranges."),
3329 objfile_name (objfile), entry_addr - section->buffer,
3330 address_size);
3331 return;
3332 }
3333
3334 const uint8_t segment_selector_size = *addr++;
3335 if (segment_selector_size != 0)
3336 {
3337 warning (_("Section .debug_aranges in %s entry at offset %zu "
3338 "segment_selector_size %u is not supported, "
3339 "ignoring .debug_aranges."),
3340 objfile_name (objfile), entry_addr - section->buffer,
3341 segment_selector_size);
3342 return;
3343 }
3344
3345 /* Must pad to an alignment boundary that is twice the address
3346 size. It is undocumented by the DWARF standard but GCC does
3347 use it. */
3348 for (size_t padding = ((-(addr - section->buffer))
3349 & (2 * address_size - 1));
3350 padding > 0; padding--)
3351 if (*addr++ != 0)
3352 {
3353 warning (_("Section .debug_aranges in %s entry at offset %zu "
3354 "padding is not zero, ignoring .debug_aranges."),
3355 objfile_name (objfile), entry_addr - section->buffer);
3356 return;
3357 }
3358
3359 for (;;)
3360 {
3361 if (addr + 2 * address_size > entry_end)
3362 {
3363 warning (_("Section .debug_aranges in %s entry at offset %zu "
3364 "address list is not properly terminated, "
3365 "ignoring .debug_aranges."),
3366 objfile_name (objfile), entry_addr - section->buffer);
3367 return;
3368 }
3369 ULONGEST start = extract_unsigned_integer (addr, address_size,
3370 dwarf5_byte_order);
3371 addr += address_size;
3372 ULONGEST length = extract_unsigned_integer (addr, address_size,
3373 dwarf5_byte_order);
3374 addr += address_size;
3375 if (start == 0 && length == 0)
3376 break;
3377 if (start == 0 && !dwarf2_per_objfile->has_section_at_zero)
3378 {
3379 /* Symbol was eliminated due to a COMDAT group. */
3380 continue;
3381 }
3382 ULONGEST end = start + length;
79748972
TT
3383 start = (gdbarch_adjust_dwarf2_addr (gdbarch, start + baseaddr)
3384 - baseaddr);
3385 end = (gdbarch_adjust_dwarf2_addr (gdbarch, end + baseaddr)
3386 - baseaddr);
927aa2e7
JK
3387 addrmap_set_empty (mutable_map, start, end - 1, per_cu);
3388 }
3389 }
3390
d320c2b5 3391 objfile->partial_symtabs->psymtabs_addrmap
5923a04c 3392 = addrmap_create_fixed (mutable_map, objfile->partial_symtabs->obstack ());
927aa2e7
JK
3393}
3394
9291a0cd
TT
3395/* Find a slot in the mapped index INDEX for the object named NAME.
3396 If NAME is found, set *VEC_OUT to point to the CU vector in the
109483d9
PA
3397 constant pool and return true. If NAME cannot be found, return
3398 false. */
2fdf6df6 3399
109483d9 3400static bool
9291a0cd
TT
3401find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
3402 offset_type **vec_out)
3403{
0cf03b49 3404 offset_type hash;
9291a0cd 3405 offset_type slot, step;
559a7a62 3406 int (*cmp) (const char *, const char *);
9291a0cd 3407
791afaa2 3408 gdb::unique_xmalloc_ptr<char> without_params;
0cf03b49 3409 if (current_language->la_language == language_cplus
45280282
IB
3410 || current_language->la_language == language_fortran
3411 || current_language->la_language == language_d)
0cf03b49
JK
3412 {
3413 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
3414 not contain any. */
a8719064 3415
72998fb3 3416 if (strchr (name, '(') != NULL)
0cf03b49 3417 {
109483d9 3418 without_params = cp_remove_params (name);
0cf03b49 3419
72998fb3 3420 if (without_params != NULL)
791afaa2 3421 name = without_params.get ();
0cf03b49
JK
3422 }
3423 }
3424
559a7a62 3425 /* Index version 4 did not support case insensitive searches. But the
feea76c2 3426 indices for case insensitive languages are built in lowercase, therefore
559a7a62
JK
3427 simulate our NAME being searched is also lowercased. */
3428 hash = mapped_index_string_hash ((index->version == 4
3429 && case_sensitivity == case_sensitive_off
3430 ? 5 : index->version),
3431 name);
3432
f00a2de2
PA
3433 slot = hash & (index->symbol_table.size () - 1);
3434 step = ((hash * 17) & (index->symbol_table.size () - 1)) | 1;
559a7a62 3435 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
9291a0cd
TT
3436
3437 for (;;)
3438 {
9291a0cd 3439 const char *str;
f00a2de2
PA
3440
3441 const auto &bucket = index->symbol_table[slot];
3442 if (bucket.name == 0 && bucket.vec == 0)
109483d9 3443 return false;
9291a0cd 3444
f00a2de2 3445 str = index->constant_pool + MAYBE_SWAP (bucket.name);
559a7a62 3446 if (!cmp (name, str))
9291a0cd
TT
3447 {
3448 *vec_out = (offset_type *) (index->constant_pool
f00a2de2 3449 + MAYBE_SWAP (bucket.vec));
109483d9 3450 return true;
9291a0cd
TT
3451 }
3452
f00a2de2 3453 slot = (slot + step) & (index->symbol_table.size () - 1);
9291a0cd
TT
3454 }
3455}
3456
4485a1c1
SM
3457/* A helper function that reads the .gdb_index from BUFFER and fills
3458 in MAP. FILENAME is the name of the file containing the data;
d33bc52e 3459 it is used for error reporting. DEPRECATED_OK is true if it is
2ec9a5e0
TT
3460 ok to use deprecated sections.
3461
3462 CU_LIST, CU_LIST_ELEMENTS, TYPES_LIST, and TYPES_LIST_ELEMENTS are
3463 out parameters that are filled in with information about the CU and
3464 TU lists in the section.
3465
4485a1c1 3466 Returns true if all went well, false otherwise. */
2fdf6df6 3467
d33bc52e 3468static bool
4485a1c1
SM
3469read_gdb_index_from_buffer (struct objfile *objfile,
3470 const char *filename,
3471 bool deprecated_ok,
3472 gdb::array_view<const gdb_byte> buffer,
3473 struct mapped_index *map,
3474 const gdb_byte **cu_list,
3475 offset_type *cu_list_elements,
3476 const gdb_byte **types_list,
3477 offset_type *types_list_elements)
3478{
3479 const gdb_byte *addr = &buffer[0];
82430852 3480
9291a0cd 3481 /* Version check. */
4485a1c1 3482 offset_type version = MAYBE_SWAP (*(offset_type *) addr);
987d643c 3483 /* Versions earlier than 3 emitted every copy of a psymbol. This
a6e293d1 3484 causes the index to behave very poorly for certain requests. Version 3
831adc1f 3485 contained incomplete addrmap. So, it seems better to just ignore such
481860b3 3486 indices. */
831adc1f 3487 if (version < 4)
481860b3
GB
3488 {
3489 static int warning_printed = 0;
3490 if (!warning_printed)
3491 {
3492 warning (_("Skipping obsolete .gdb_index section in %s."),
2ec9a5e0 3493 filename);
481860b3
GB
3494 warning_printed = 1;
3495 }
3496 return 0;
3497 }
3498 /* Index version 4 uses a different hash function than index version
3499 5 and later.
3500
3501 Versions earlier than 6 did not emit psymbols for inlined
3502 functions. Using these files will cause GDB not to be able to
3503 set breakpoints on inlined functions by name, so we ignore these
e615022a
DE
3504 indices unless the user has done
3505 "set use-deprecated-index-sections on". */
2ec9a5e0 3506 if (version < 6 && !deprecated_ok)
481860b3
GB
3507 {
3508 static int warning_printed = 0;
3509 if (!warning_printed)
3510 {
e615022a
DE
3511 warning (_("\
3512Skipping deprecated .gdb_index section in %s.\n\
3513Do \"set use-deprecated-index-sections on\" before the file is read\n\
3514to use the section anyway."),
2ec9a5e0 3515 filename);
481860b3
GB
3516 warning_printed = 1;
3517 }
3518 return 0;
3519 }
796a7ff8 3520 /* Version 7 indices generated by gold refer to the CU for a symbol instead
8943b874
DE
3521 of the TU (for symbols coming from TUs),
3522 http://sourceware.org/bugzilla/show_bug.cgi?id=15021.
3523 Plus gold-generated indices can have duplicate entries for global symbols,
3524 http://sourceware.org/bugzilla/show_bug.cgi?id=15646.
3525 These are just performance bugs, and we can't distinguish gdb-generated
3526 indices from gold-generated ones, so issue no warning here. */
796a7ff8 3527
481860b3 3528 /* Indexes with higher version than the one supported by GDB may be no
594e8718 3529 longer backward compatible. */
796a7ff8 3530 if (version > 8)
594e8718 3531 return 0;
9291a0cd 3532
559a7a62 3533 map->version = version;
9291a0cd 3534
4485a1c1 3535 offset_type *metadata = (offset_type *) (addr + sizeof (offset_type));
1fd400ff 3536
4485a1c1 3537 int i = 0;
2ec9a5e0
TT
3538 *cu_list = addr + MAYBE_SWAP (metadata[i]);
3539 *cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
3540 / 8);
1fd400ff
TT
3541 ++i;
3542
2ec9a5e0
TT
3543 *types_list = addr + MAYBE_SWAP (metadata[i]);
3544 *types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
3545 - MAYBE_SWAP (metadata[i]))
3546 / 8);
987d643c 3547 ++i;
1fd400ff 3548
f00a2de2
PA
3549 const gdb_byte *address_table = addr + MAYBE_SWAP (metadata[i]);
3550 const gdb_byte *address_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
3551 map->address_table
3552 = gdb::array_view<const gdb_byte> (address_table, address_table_end);
1fd400ff
TT
3553 ++i;
3554
f00a2de2
PA
3555 const gdb_byte *symbol_table = addr + MAYBE_SWAP (metadata[i]);
3556 const gdb_byte *symbol_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
3557 map->symbol_table
3558 = gdb::array_view<mapped_index::symbol_table_slot>
3559 ((mapped_index::symbol_table_slot *) symbol_table,
3560 (mapped_index::symbol_table_slot *) symbol_table_end);
9291a0cd 3561
f00a2de2 3562 ++i;
f9d83a0b 3563 map->constant_pool = (char *) (addr + MAYBE_SWAP (metadata[i]));
1fd400ff 3564
2ec9a5e0
TT
3565 return 1;
3566}
3567
4485a1c1
SM
3568/* Callback types for dwarf2_read_gdb_index. */
3569
3570typedef gdb::function_view
3571 <gdb::array_view<const gdb_byte>(objfile *, dwarf2_per_objfile *)>
3572 get_gdb_index_contents_ftype;
3573typedef gdb::function_view
3574 <gdb::array_view<const gdb_byte>(objfile *, dwz_file *)>
3575 get_gdb_index_contents_dwz_ftype;
3576
927aa2e7 3577/* Read .gdb_index. If everything went ok, initialize the "quick"
2ec9a5e0
TT
3578 elements of all the CUs and return 1. Otherwise, return 0. */
3579
3580static int
4485a1c1
SM
3581dwarf2_read_gdb_index
3582 (struct dwarf2_per_objfile *dwarf2_per_objfile,
3583 get_gdb_index_contents_ftype get_gdb_index_contents,
3584 get_gdb_index_contents_dwz_ftype get_gdb_index_contents_dwz)
2ec9a5e0 3585{
2ec9a5e0
TT
3586 const gdb_byte *cu_list, *types_list, *dwz_list = NULL;
3587 offset_type cu_list_elements, types_list_elements, dwz_list_elements = 0;
4db1a1dc 3588 struct dwz_file *dwz;
12359b5e 3589 struct objfile *objfile = dwarf2_per_objfile->objfile;
2ec9a5e0 3590
4485a1c1
SM
3591 gdb::array_view<const gdb_byte> main_index_contents
3592 = get_gdb_index_contents (objfile, dwarf2_per_objfile);
3593
3594 if (main_index_contents.empty ())
3595 return 0;
3596
3063847f 3597 std::unique_ptr<struct mapped_index> map (new struct mapped_index);
4485a1c1
SM
3598 if (!read_gdb_index_from_buffer (objfile, objfile_name (objfile),
3599 use_deprecated_index_sections,
3600 main_index_contents, map.get (), &cu_list,
3601 &cu_list_elements, &types_list,
3602 &types_list_elements))
2ec9a5e0
TT
3603 return 0;
3604
0fefef59 3605 /* Don't use the index if it's empty. */
3063847f 3606 if (map->symbol_table.empty ())
0fefef59
DE
3607 return 0;
3608
2ec9a5e0
TT
3609 /* If there is a .dwz file, read it so we can get its CU list as
3610 well. */
ed2dc618 3611 dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
4db1a1dc 3612 if (dwz != NULL)
2ec9a5e0 3613 {
2ec9a5e0
TT
3614 struct mapped_index dwz_map;
3615 const gdb_byte *dwz_types_ignore;
3616 offset_type dwz_types_elements_ignore;
3617
4485a1c1
SM
3618 gdb::array_view<const gdb_byte> dwz_index_content
3619 = get_gdb_index_contents_dwz (objfile, dwz);
3620
3621 if (dwz_index_content.empty ())
3622 return 0;
3623
3624 if (!read_gdb_index_from_buffer (objfile,
3625 bfd_get_filename (dwz->dwz_bfd), 1,
3626 dwz_index_content, &dwz_map,
3627 &dwz_list, &dwz_list_elements,
3628 &dwz_types_ignore,
3629 &dwz_types_elements_ignore))
2ec9a5e0
TT
3630 {
3631 warning (_("could not read '.gdb_index' section from %s; skipping"),
3632 bfd_get_filename (dwz->dwz_bfd));
3633 return 0;
3634 }
3635 }
3636
12359b5e
SM
3637 create_cus_from_index (dwarf2_per_objfile, cu_list, cu_list_elements,
3638 dwz_list, dwz_list_elements);
1fd400ff 3639
8b70b953
TT
3640 if (types_list_elements)
3641 {
3642 struct dwarf2_section_info *section;
3643
3644 /* We can only handle a single .debug_types when we have an
3645 index. */
3646 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) != 1)
3647 return 0;
3648
3649 section = VEC_index (dwarf2_section_info_def,
3650 dwarf2_per_objfile->types, 0);
3651
12359b5e
SM
3652 create_signatured_type_table_from_index (dwarf2_per_objfile, section,
3653 types_list, types_list_elements);
8b70b953 3654 }
9291a0cd 3655
3063847f 3656 create_addrmap_from_index (dwarf2_per_objfile, map.get ());
9291a0cd 3657
3063847f 3658 dwarf2_per_objfile->index_table = std::move (map);
9291a0cd 3659 dwarf2_per_objfile->using_index = 1;
7b9f3c50 3660 dwarf2_per_objfile->quick_file_names_table =
b76e467d 3661 create_quick_file_names_table (dwarf2_per_objfile->all_comp_units.size ());
9291a0cd
TT
3662
3663 return 1;
3664}
3665
dee91e82 3666/* die_reader_func for dw2_get_file_names. */
2fdf6df6 3667
dee91e82
DE
3668static void
3669dw2_get_file_names_reader (const struct die_reader_specs *reader,
d521ce57 3670 const gdb_byte *info_ptr,
dee91e82
DE
3671 struct die_info *comp_unit_die,
3672 int has_children,
3673 void *data)
9291a0cd 3674{
dee91e82 3675 struct dwarf2_cu *cu = reader->cu;
ed2dc618 3676 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
518817b3
SM
3677 struct dwarf2_per_objfile *dwarf2_per_objfile
3678 = cu->per_cu->dwarf2_per_objfile;
dee91e82 3679 struct objfile *objfile = dwarf2_per_objfile->objfile;
094b34ac 3680 struct dwarf2_per_cu_data *lh_cu;
9291a0cd 3681 struct attribute *attr;
dee91e82 3682 int i;
7b9f3c50
DE
3683 void **slot;
3684 struct quick_file_names *qfn;
9291a0cd 3685
0186c6a7
DE
3686 gdb_assert (! this_cu->is_debug_types);
3687
07261596
TT
3688 /* Our callers never want to match partial units -- instead they
3689 will match the enclosing full CU. */
3690 if (comp_unit_die->tag == DW_TAG_partial_unit)
3691 {
3692 this_cu->v.quick->no_file_data = 1;
3693 return;
3694 }
3695
0186c6a7 3696 lh_cu = this_cu;
7b9f3c50 3697 slot = NULL;
dee91e82 3698
fff8551c 3699 line_header_up lh;
9c541725 3700 sect_offset line_offset {};
fff8551c 3701
dee91e82 3702 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
9291a0cd
TT
3703 if (attr)
3704 {
7b9f3c50
DE
3705 struct quick_file_names find_entry;
3706
9c541725 3707 line_offset = (sect_offset) DW_UNSND (attr);
7b9f3c50
DE
3708
3709 /* We may have already read in this line header (TU line header sharing).
3710 If we have we're done. */
094b34ac 3711 find_entry.hash.dwo_unit = cu->dwo_unit;
9c541725 3712 find_entry.hash.line_sect_off = line_offset;
7b9f3c50
DE
3713 slot = htab_find_slot (dwarf2_per_objfile->quick_file_names_table,
3714 &find_entry, INSERT);
3715 if (*slot != NULL)
3716 {
9a3c8263 3717 lh_cu->v.quick->file_names = (struct quick_file_names *) *slot;
dee91e82 3718 return;
7b9f3c50
DE
3719 }
3720
3019eac3 3721 lh = dwarf_decode_line_header (line_offset, cu);
9291a0cd
TT
3722 }
3723 if (lh == NULL)
3724 {
094b34ac 3725 lh_cu->v.quick->no_file_data = 1;
dee91e82 3726 return;
9291a0cd
TT
3727 }
3728
8d749320 3729 qfn = XOBNEW (&objfile->objfile_obstack, struct quick_file_names);
094b34ac 3730 qfn->hash.dwo_unit = cu->dwo_unit;
9c541725 3731 qfn->hash.line_sect_off = line_offset;
7b9f3c50
DE
3732 gdb_assert (slot != NULL);
3733 *slot = qfn;
9291a0cd 3734
d721ba37 3735 file_and_directory fnd = find_file_and_directory (comp_unit_die, cu);
9291a0cd 3736
fff8551c 3737 qfn->num_file_names = lh->file_names.size ();
8d749320 3738 qfn->file_names =
fff8551c
PA
3739 XOBNEWVEC (&objfile->objfile_obstack, const char *, lh->file_names.size ());
3740 for (i = 0; i < lh->file_names.size (); ++i)
3741 qfn->file_names[i] = file_full_name (i + 1, lh.get (), fnd.comp_dir);
7b9f3c50 3742 qfn->real_names = NULL;
9291a0cd 3743
094b34ac 3744 lh_cu->v.quick->file_names = qfn;
dee91e82
DE
3745}
3746
3747/* A helper for the "quick" functions which attempts to read the line
3748 table for THIS_CU. */
3749
3750static struct quick_file_names *
e4a48d9d 3751dw2_get_file_names (struct dwarf2_per_cu_data *this_cu)
dee91e82 3752{
0186c6a7
DE
3753 /* This should never be called for TUs. */
3754 gdb_assert (! this_cu->is_debug_types);
3755 /* Nor type unit groups. */
3756 gdb_assert (! IS_TYPE_UNIT_GROUP (this_cu));
f4dc4d17 3757
dee91e82
DE
3758 if (this_cu->v.quick->file_names != NULL)
3759 return this_cu->v.quick->file_names;
3760 /* If we know there is no line data, no point in looking again. */
3761 if (this_cu->v.quick->no_file_data)
3762 return NULL;
3763
0186c6a7 3764 init_cutu_and_read_dies_simple (this_cu, dw2_get_file_names_reader, NULL);
dee91e82
DE
3765
3766 if (this_cu->v.quick->no_file_data)
3767 return NULL;
3768 return this_cu->v.quick->file_names;
9291a0cd
TT
3769}
3770
3771/* A helper for the "quick" functions which computes and caches the
7b9f3c50 3772 real path for a given file name from the line table. */
2fdf6df6 3773
9291a0cd 3774static const char *
7b9f3c50
DE
3775dw2_get_real_path (struct objfile *objfile,
3776 struct quick_file_names *qfn, int index)
9291a0cd 3777{
7b9f3c50
DE
3778 if (qfn->real_names == NULL)
3779 qfn->real_names = OBSTACK_CALLOC (&objfile->objfile_obstack,
26f2dc30 3780 qfn->num_file_names, const char *);
9291a0cd 3781
7b9f3c50 3782 if (qfn->real_names[index] == NULL)
14278e1f 3783 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]).release ();
9291a0cd 3784
7b9f3c50 3785 return qfn->real_names[index];
9291a0cd
TT
3786}
3787
3788static struct symtab *
3789dw2_find_last_source_symtab (struct objfile *objfile)
3790{
ed2dc618
SM
3791 struct dwarf2_per_objfile *dwarf2_per_objfile
3792 = get_dwarf2_per_objfile (objfile);
b76e467d 3793 dwarf2_per_cu_data *dwarf_cu = dwarf2_per_objfile->all_comp_units.back ();
58f0c718 3794 compunit_symtab *cust = dw2_instantiate_symtab (dwarf_cu, false);
ae2de4f8 3795
43f3e411
DE
3796 if (cust == NULL)
3797 return NULL;
ed2dc618 3798
43f3e411 3799 return compunit_primary_filetab (cust);
9291a0cd
TT
3800}
3801
7b9f3c50
DE
3802/* Traversal function for dw2_forget_cached_source_info. */
3803
3804static int
3805dw2_free_cached_file_names (void **slot, void *info)
9291a0cd 3806{
7b9f3c50 3807 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
9291a0cd 3808
7b9f3c50 3809 if (file_data->real_names)
9291a0cd 3810 {
7b9f3c50 3811 int i;
9291a0cd 3812
7b9f3c50 3813 for (i = 0; i < file_data->num_file_names; ++i)
9291a0cd 3814 {
7b9f3c50
DE
3815 xfree ((void*) file_data->real_names[i]);
3816 file_data->real_names[i] = NULL;
9291a0cd
TT
3817 }
3818 }
7b9f3c50
DE
3819
3820 return 1;
3821}
3822
3823static void
3824dw2_forget_cached_source_info (struct objfile *objfile)
3825{
ed2dc618
SM
3826 struct dwarf2_per_objfile *dwarf2_per_objfile
3827 = get_dwarf2_per_objfile (objfile);
7b9f3c50
DE
3828
3829 htab_traverse_noresize (dwarf2_per_objfile->quick_file_names_table,
3830 dw2_free_cached_file_names, NULL);
9291a0cd
TT
3831}
3832
f8eba3c6
TT
3833/* Helper function for dw2_map_symtabs_matching_filename that expands
3834 the symtabs and calls the iterator. */
3835
3836static int
3837dw2_map_expand_apply (struct objfile *objfile,
3838 struct dwarf2_per_cu_data *per_cu,
f5b95b50 3839 const char *name, const char *real_path,
14bc53a8 3840 gdb::function_view<bool (symtab *)> callback)
f8eba3c6 3841{
43f3e411 3842 struct compunit_symtab *last_made = objfile->compunit_symtabs;
f8eba3c6
TT
3843
3844 /* Don't visit already-expanded CUs. */
43f3e411 3845 if (per_cu->v.quick->compunit_symtab)
f8eba3c6
TT
3846 return 0;
3847
3848 /* This may expand more than one symtab, and we want to iterate over
3849 all of them. */
58f0c718 3850 dw2_instantiate_symtab (per_cu, false);
f8eba3c6 3851
14bc53a8
PA
3852 return iterate_over_some_symtabs (name, real_path, objfile->compunit_symtabs,
3853 last_made, callback);
f8eba3c6
TT
3854}
3855
3856/* Implementation of the map_symtabs_matching_filename method. */
3857
14bc53a8
PA
3858static bool
3859dw2_map_symtabs_matching_filename
3860 (struct objfile *objfile, const char *name, const char *real_path,
3861 gdb::function_view<bool (symtab *)> callback)
9291a0cd 3862{
c011a4f4 3863 const char *name_basename = lbasename (name);
ed2dc618
SM
3864 struct dwarf2_per_objfile *dwarf2_per_objfile
3865 = get_dwarf2_per_objfile (objfile);
ae2de4f8 3866
848e3e78
DE
3867 /* The rule is CUs specify all the files, including those used by
3868 any TU, so there's no need to scan TUs here. */
f4dc4d17 3869
b76e467d 3870 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
9291a0cd 3871 {
3d7bb9d9 3872 /* We only need to look at symtabs not already expanded. */
43f3e411 3873 if (per_cu->v.quick->compunit_symtab)
9291a0cd
TT
3874 continue;
3875
b76e467d 3876 quick_file_names *file_data = dw2_get_file_names (per_cu);
7b9f3c50 3877 if (file_data == NULL)
9291a0cd
TT
3878 continue;
3879
b76e467d 3880 for (int j = 0; j < file_data->num_file_names; ++j)
9291a0cd 3881 {
7b9f3c50 3882 const char *this_name = file_data->file_names[j];
da235a7c 3883 const char *this_real_name;
9291a0cd 3884
af529f8f 3885 if (compare_filenames_for_search (this_name, name))
9291a0cd 3886 {
f5b95b50 3887 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
14bc53a8
PA
3888 callback))
3889 return true;
288e77a7 3890 continue;
4aac40c8 3891 }
9291a0cd 3892
c011a4f4
DE
3893 /* Before we invoke realpath, which can get expensive when many
3894 files are involved, do a quick comparison of the basenames. */
3895 if (! basenames_may_differ
3896 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
3897 continue;
3898
da235a7c
JK
3899 this_real_name = dw2_get_real_path (objfile, file_data, j);
3900 if (compare_filenames_for_search (this_real_name, name))
9291a0cd 3901 {
da235a7c 3902 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
14bc53a8
PA
3903 callback))
3904 return true;
288e77a7 3905 continue;
da235a7c 3906 }
9291a0cd 3907
da235a7c
JK
3908 if (real_path != NULL)
3909 {
af529f8f
JK
3910 gdb_assert (IS_ABSOLUTE_PATH (real_path));
3911 gdb_assert (IS_ABSOLUTE_PATH (name));
7b9f3c50 3912 if (this_real_name != NULL
af529f8f 3913 && FILENAME_CMP (real_path, this_real_name) == 0)
9291a0cd 3914 {
f5b95b50 3915 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
14bc53a8
PA
3916 callback))
3917 return true;
288e77a7 3918 continue;
9291a0cd
TT
3919 }
3920 }
3921 }
3922 }
3923
14bc53a8 3924 return false;
9291a0cd
TT
3925}
3926
da51c347
DE
3927/* Struct used to manage iterating over all CUs looking for a symbol. */
3928
3929struct dw2_symtab_iterator
9291a0cd 3930{
ed2dc618
SM
3931 /* The dwarf2_per_objfile owning the CUs we are iterating on. */
3932 struct dwarf2_per_objfile *dwarf2_per_objfile;
da51c347
DE
3933 /* If non-zero, only look for symbols that match BLOCK_INDEX. */
3934 int want_specific_block;
3935 /* One of GLOBAL_BLOCK or STATIC_BLOCK.
3936 Unused if !WANT_SPECIFIC_BLOCK. */
3937 int block_index;
3938 /* The kind of symbol we're looking for. */
3939 domain_enum domain;
3940 /* The list of CUs from the index entry of the symbol,
3941 or NULL if not found. */
3942 offset_type *vec;
3943 /* The next element in VEC to look at. */
3944 int next;
3945 /* The number of elements in VEC, or zero if there is no match. */
3946 int length;
8943b874
DE
3947 /* Have we seen a global version of the symbol?
3948 If so we can ignore all further global instances.
3949 This is to work around gold/15646, inefficient gold-generated
3950 indices. */
3951 int global_seen;
da51c347 3952};
9291a0cd 3953
da51c347
DE
3954/* Initialize the index symtab iterator ITER.
3955 If WANT_SPECIFIC_BLOCK is non-zero, only look for symbols
3956 in block BLOCK_INDEX. Otherwise BLOCK_INDEX is ignored. */
2fdf6df6 3957
9291a0cd 3958static void
da51c347 3959dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
ed2dc618 3960 struct dwarf2_per_objfile *dwarf2_per_objfile,
da51c347
DE
3961 int want_specific_block,
3962 int block_index,
3963 domain_enum domain,
3964 const char *name)
3965{
ed2dc618 3966 iter->dwarf2_per_objfile = dwarf2_per_objfile;
da51c347
DE
3967 iter->want_specific_block = want_specific_block;
3968 iter->block_index = block_index;
3969 iter->domain = domain;
3970 iter->next = 0;
8943b874 3971 iter->global_seen = 0;
da51c347 3972
3063847f 3973 mapped_index *index = dwarf2_per_objfile->index_table.get ();
ed2dc618
SM
3974
3975 /* index is NULL if OBJF_READNOW. */
3976 if (index != NULL && find_slot_in_mapped_hash (index, name, &iter->vec))
da51c347
DE
3977 iter->length = MAYBE_SWAP (*iter->vec);
3978 else
3979 {
3980 iter->vec = NULL;
3981 iter->length = 0;
3982 }
3983}
3984
3985/* Return the next matching CU or NULL if there are no more. */
3986
3987static struct dwarf2_per_cu_data *
3988dw2_symtab_iter_next (struct dw2_symtab_iterator *iter)
3989{
ed2dc618
SM
3990 struct dwarf2_per_objfile *dwarf2_per_objfile = iter->dwarf2_per_objfile;
3991
da51c347
DE
3992 for ( ; iter->next < iter->length; ++iter->next)
3993 {
3994 offset_type cu_index_and_attrs =
3995 MAYBE_SWAP (iter->vec[iter->next + 1]);
3996 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
da51c347
DE
3997 int want_static = iter->block_index != GLOBAL_BLOCK;
3998 /* This value is only valid for index versions >= 7. */
3999 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
4000 gdb_index_symbol_kind symbol_kind =
4001 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
4002 /* Only check the symbol attributes if they're present.
4003 Indices prior to version 7 don't record them,
4004 and indices >= 7 may elide them for certain symbols
4005 (gold does this). */
4006 int attrs_valid =
ed2dc618 4007 (dwarf2_per_objfile->index_table->version >= 7
da51c347
DE
4008 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
4009
3190f0c6 4010 /* Don't crash on bad data. */
b76e467d 4011 if (cu_index >= (dwarf2_per_objfile->all_comp_units.size ()
b2bdb8cf 4012 + dwarf2_per_objfile->all_type_units.size ()))
3190f0c6 4013 {
b98664d3 4014 complaint (_(".gdb_index entry has bad CU index"
4262abfb
JK
4015 " [in module %s]"),
4016 objfile_name (dwarf2_per_objfile->objfile));
3190f0c6
DE
4017 continue;
4018 }
4019
ff4c9fec 4020 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (cu_index);
3190f0c6 4021
da51c347 4022 /* Skip if already read in. */
43f3e411 4023 if (per_cu->v.quick->compunit_symtab)
da51c347
DE
4024 continue;
4025
8943b874
DE
4026 /* Check static vs global. */
4027 if (attrs_valid)
4028 {
4029 if (iter->want_specific_block
4030 && want_static != is_static)
4031 continue;
4032 /* Work around gold/15646. */
4033 if (!is_static && iter->global_seen)
4034 continue;
4035 if (!is_static)
4036 iter->global_seen = 1;
4037 }
da51c347
DE
4038
4039 /* Only check the symbol's kind if it has one. */
4040 if (attrs_valid)
4041 {
4042 switch (iter->domain)
4043 {
4044 case VAR_DOMAIN:
4045 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
4046 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
4047 /* Some types are also in VAR_DOMAIN. */
4048 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
4049 continue;
4050 break;
4051 case STRUCT_DOMAIN:
4052 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
4053 continue;
4054 break;
4055 case LABEL_DOMAIN:
4056 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
4057 continue;
4058 break;
4059 default:
4060 break;
4061 }
4062 }
4063
4064 ++iter->next;
4065 return per_cu;
4066 }
4067
4068 return NULL;
4069}
4070
43f3e411 4071static struct compunit_symtab *
da51c347
DE
4072dw2_lookup_symbol (struct objfile *objfile, int block_index,
4073 const char *name, domain_enum domain)
9291a0cd 4074{
43f3e411 4075 struct compunit_symtab *stab_best = NULL;
ed2dc618
SM
4076 struct dwarf2_per_objfile *dwarf2_per_objfile
4077 = get_dwarf2_per_objfile (objfile);
9291a0cd 4078
b5ec771e
PA
4079 lookup_name_info lookup_name (name, symbol_name_match_type::FULL);
4080
ed2dc618
SM
4081 struct dw2_symtab_iterator iter;
4082 struct dwarf2_per_cu_data *per_cu;
da51c347 4083
ed2dc618 4084 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, 1, block_index, domain, name);
9291a0cd 4085
ed2dc618
SM
4086 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
4087 {
4088 struct symbol *sym, *with_opaque = NULL;
58f0c718 4089 struct compunit_symtab *stab = dw2_instantiate_symtab (per_cu, false);
ed2dc618 4090 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
582942f4 4091 const struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
da51c347 4092
ed2dc618
SM
4093 sym = block_find_symbol (block, name, domain,
4094 block_find_non_opaque_type_preferred,
4095 &with_opaque);
b2e2f908 4096
ed2dc618
SM
4097 /* Some caution must be observed with overloaded functions
4098 and methods, since the index will not contain any overload
4099 information (but NAME might contain it). */
da51c347 4100
ed2dc618
SM
4101 if (sym != NULL
4102 && SYMBOL_MATCHES_SEARCH_NAME (sym, lookup_name))
4103 return stab;
4104 if (with_opaque != NULL
4105 && SYMBOL_MATCHES_SEARCH_NAME (with_opaque, lookup_name))
4106 stab_best = stab;
da51c347 4107
ed2dc618 4108 /* Keep looking through other CUs. */
9291a0cd 4109 }
9291a0cd 4110
da51c347 4111 return stab_best;
9291a0cd
TT
4112}
4113
4114static void
4115dw2_print_stats (struct objfile *objfile)
4116{
ed2dc618
SM
4117 struct dwarf2_per_objfile *dwarf2_per_objfile
4118 = get_dwarf2_per_objfile (objfile);
b76e467d 4119 int total = (dwarf2_per_objfile->all_comp_units.size ()
b2bdb8cf 4120 + dwarf2_per_objfile->all_type_units.size ());
ed2dc618 4121 int count = 0;
9291a0cd 4122
ed2dc618 4123 for (int i = 0; i < total; ++i)
9291a0cd 4124 {
ff4c9fec 4125 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (i);
9291a0cd 4126
43f3e411 4127 if (!per_cu->v.quick->compunit_symtab)
9291a0cd
TT
4128 ++count;
4129 }
e4a48d9d 4130 printf_filtered (_(" Number of read CUs: %d\n"), total - count);
9291a0cd
TT
4131 printf_filtered (_(" Number of unread CUs: %d\n"), count);
4132}
4133
779bd270
DE
4134/* This dumps minimal information about the index.
4135 It is called via "mt print objfiles".
4136 One use is to verify .gdb_index has been loaded by the
4137 gdb.dwarf2/gdb-index.exp testcase. */
4138
9291a0cd
TT
4139static void
4140dw2_dump (struct objfile *objfile)
4141{
ed2dc618
SM
4142 struct dwarf2_per_objfile *dwarf2_per_objfile
4143 = get_dwarf2_per_objfile (objfile);
4144
779bd270
DE
4145 gdb_assert (dwarf2_per_objfile->using_index);
4146 printf_filtered (".gdb_index:");
4147 if (dwarf2_per_objfile->index_table != NULL)
4148 {
4149 printf_filtered (" version %d\n",
4150 dwarf2_per_objfile->index_table->version);
4151 }
4152 else
4153 printf_filtered (" faked for \"readnow\"\n");
4154 printf_filtered ("\n");
9291a0cd
TT
4155}
4156
9291a0cd
TT
4157static void
4158dw2_expand_symtabs_for_function (struct objfile *objfile,
4159 const char *func_name)
4160{
ed2dc618
SM
4161 struct dwarf2_per_objfile *dwarf2_per_objfile
4162 = get_dwarf2_per_objfile (objfile);
da51c347 4163
ed2dc618
SM
4164 struct dw2_symtab_iterator iter;
4165 struct dwarf2_per_cu_data *per_cu;
da51c347 4166
ed2dc618
SM
4167 /* Note: It doesn't matter what we pass for block_index here. */
4168 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, 0, GLOBAL_BLOCK, VAR_DOMAIN,
4169 func_name);
da51c347 4170
ed2dc618 4171 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
58f0c718 4172 dw2_instantiate_symtab (per_cu, false);
da51c347 4173
9291a0cd
TT
4174}
4175
4176static void
4177dw2_expand_all_symtabs (struct objfile *objfile)
4178{
ed2dc618
SM
4179 struct dwarf2_per_objfile *dwarf2_per_objfile
4180 = get_dwarf2_per_objfile (objfile);
b76e467d 4181 int total_units = (dwarf2_per_objfile->all_comp_units.size ()
b2bdb8cf 4182 + dwarf2_per_objfile->all_type_units.size ());
9291a0cd 4183
ed2dc618 4184 for (int i = 0; i < total_units; ++i)
9291a0cd 4185 {
ff4c9fec 4186 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (i);
9291a0cd 4187
58f0c718
TT
4188 /* We don't want to directly expand a partial CU, because if we
4189 read it with the wrong language, then assertion failures can
4190 be triggered later on. See PR symtab/23010. So, tell
4191 dw2_instantiate_symtab to skip partial CUs -- any important
4192 partial CU will be read via DW_TAG_imported_unit anyway. */
4193 dw2_instantiate_symtab (per_cu, true);
9291a0cd
TT
4194 }
4195}
4196
4197static void
652a8996
JK
4198dw2_expand_symtabs_with_fullname (struct objfile *objfile,
4199 const char *fullname)
9291a0cd 4200{
ed2dc618
SM
4201 struct dwarf2_per_objfile *dwarf2_per_objfile
4202 = get_dwarf2_per_objfile (objfile);
d4637a04
DE
4203
4204 /* We don't need to consider type units here.
4205 This is only called for examining code, e.g. expand_line_sal.
4206 There can be an order of magnitude (or more) more type units
4207 than comp units, and we avoid them if we can. */
4208
b76e467d 4209 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
9291a0cd 4210 {
3d7bb9d9 4211 /* We only need to look at symtabs not already expanded. */
43f3e411 4212 if (per_cu->v.quick->compunit_symtab)
9291a0cd
TT
4213 continue;
4214
b76e467d 4215 quick_file_names *file_data = dw2_get_file_names (per_cu);
7b9f3c50 4216 if (file_data == NULL)
9291a0cd
TT
4217 continue;
4218
b76e467d 4219 for (int j = 0; j < file_data->num_file_names; ++j)
9291a0cd 4220 {
652a8996
JK
4221 const char *this_fullname = file_data->file_names[j];
4222
4223 if (filename_cmp (this_fullname, fullname) == 0)
9291a0cd 4224 {
58f0c718 4225 dw2_instantiate_symtab (per_cu, false);
9291a0cd
TT
4226 break;
4227 }
4228 }
4229 }
4230}
4231
9291a0cd 4232static void
ade7ed9e 4233dw2_map_matching_symbols (struct objfile *objfile,
fe978cb0 4234 const char * name, domain_enum domain,
ade7ed9e 4235 int global,
582942f4 4236 int (*callback) (const struct block *,
40658b94 4237 struct symbol *, void *),
b5ec771e 4238 void *data, symbol_name_match_type match,
2edb89d3 4239 symbol_compare_ftype *ordered_compare)
9291a0cd 4240{
40658b94 4241 /* Currently unimplemented; used for Ada. The function can be called if the
a9e6a4bb
JK
4242 current language is Ada for a non-Ada objfile using GNU index. As Ada
4243 does not look for non-Ada symbols this function should just return. */
9291a0cd
TT
4244}
4245
b5ec771e
PA
4246/* Symbol name matcher for .gdb_index names.
4247
4248 Symbol names in .gdb_index have a few particularities:
4249
4250 - There's no indication of which is the language of each symbol.
4251
4252 Since each language has its own symbol name matching algorithm,
4253 and we don't know which language is the right one, we must match
3f563c84
PA
4254 each symbol against all languages. This would be a potential
4255 performance problem if it were not mitigated by the
4256 mapped_index::name_components lookup table, which significantly
4257 reduces the number of times we need to call into this matcher,
4258 making it a non-issue.
b5ec771e
PA
4259
4260 - Symbol names in the index have no overload (parameter)
4261 information. I.e., in C++, "foo(int)" and "foo(long)" both
4262 appear as "foo" in the index, for example.
4263
4264 This means that the lookup names passed to the symbol name
4265 matcher functions must have no parameter information either
4266 because (e.g.) symbol search name "foo" does not match
4267 lookup-name "foo(int)" [while swapping search name for lookup
4268 name would match].
4269*/
4270class gdb_index_symbol_name_matcher
4271{
4272public:
4273 /* Prepares the vector of comparison functions for LOOKUP_NAME. */
4274 gdb_index_symbol_name_matcher (const lookup_name_info &lookup_name);
4275
4276 /* Walk all the matcher routines and match SYMBOL_NAME against them.
4277 Returns true if any matcher matches. */
4278 bool matches (const char *symbol_name);
4279
4280private:
4281 /* A reference to the lookup name we're matching against. */
4282 const lookup_name_info &m_lookup_name;
4283
4284 /* A vector holding all the different symbol name matchers, for all
4285 languages. */
4286 std::vector<symbol_name_matcher_ftype *> m_symbol_name_matcher_funcs;
4287};
4288
4289gdb_index_symbol_name_matcher::gdb_index_symbol_name_matcher
4290 (const lookup_name_info &lookup_name)
4291 : m_lookup_name (lookup_name)
4292{
4293 /* Prepare the vector of comparison functions upfront, to avoid
4294 doing the same work for each symbol. Care is taken to avoid
4295 matching with the same matcher more than once if/when multiple
4296 languages use the same matcher function. */
4297 auto &matchers = m_symbol_name_matcher_funcs;
4298 matchers.reserve (nr_languages);
4299
4300 matchers.push_back (default_symbol_name_matcher);
4301
4302 for (int i = 0; i < nr_languages; i++)
4303 {
4304 const language_defn *lang = language_def ((enum language) i);
c63d3e8d 4305 symbol_name_matcher_ftype *name_matcher
618daa93 4306 = get_symbol_name_matcher (lang, m_lookup_name);
c63d3e8d
PA
4307
4308 /* Don't insert the same comparison routine more than once.
4309 Note that we do this linear walk instead of a seemingly
4310 cheaper sorted insert, or use a std::set or something like
4311 that, because relative order of function addresses is not
4312 stable. This is not a problem in practice because the number
4313 of supported languages is low, and the cost here is tiny
4314 compared to the number of searches we'll do afterwards using
4315 this object. */
4316 if (name_matcher != default_symbol_name_matcher
4317 && (std::find (matchers.begin (), matchers.end (), name_matcher)
4318 == matchers.end ()))
4319 matchers.push_back (name_matcher);
b5ec771e
PA
4320 }
4321}
4322
4323bool
4324gdb_index_symbol_name_matcher::matches (const char *symbol_name)
4325{
4326 for (auto matches_name : m_symbol_name_matcher_funcs)
4327 if (matches_name (symbol_name, m_lookup_name, NULL))
4328 return true;
4329
4330 return false;
4331}
4332
e1ef7d7a
PA
4333/* Starting from a search name, return the string that finds the upper
4334 bound of all strings that start with SEARCH_NAME in a sorted name
4335 list. Returns the empty string to indicate that the upper bound is
4336 the end of the list. */
4337
4338static std::string
4339make_sort_after_prefix_name (const char *search_name)
4340{
4341 /* When looking to complete "func", we find the upper bound of all
4342 symbols that start with "func" by looking for where we'd insert
4343 the closest string that would follow "func" in lexicographical
4344 order. Usually, that's "func"-with-last-character-incremented,
4345 i.e. "fund". Mind non-ASCII characters, though. Usually those
4346 will be UTF-8 multi-byte sequences, but we can't be certain.
4347 Especially mind the 0xff character, which is a valid character in
4348 non-UTF-8 source character sets (e.g. Latin1 'ÿ'), and we can't
4349 rule out compilers allowing it in identifiers. Note that
4350 conveniently, strcmp/strcasecmp are specified to compare
4351 characters interpreted as unsigned char. So what we do is treat
4352 the whole string as a base 256 number composed of a sequence of
4353 base 256 "digits" and add 1 to it. I.e., adding 1 to 0xff wraps
4354 to 0, and carries 1 to the following more-significant position.
4355 If the very first character in SEARCH_NAME ends up incremented
4356 and carries/overflows, then the upper bound is the end of the
4357 list. The string after the empty string is also the empty
4358 string.
4359
4360 Some examples of this operation:
4361
4362 SEARCH_NAME => "+1" RESULT
4363
4364 "abc" => "abd"
4365 "ab\xff" => "ac"
4366 "\xff" "a" "\xff" => "\xff" "b"
4367 "\xff" => ""
4368 "\xff\xff" => ""
4369 "" => ""
4370
4371 Then, with these symbols for example:
4372
4373 func
4374 func1
4375 fund
4376
4377 completing "func" looks for symbols between "func" and
4378 "func"-with-last-character-incremented, i.e. "fund" (exclusive),
4379 which finds "func" and "func1", but not "fund".
4380
4381 And with:
4382
4383 funcÿ (Latin1 'ÿ' [0xff])
4384 funcÿ1
4385 fund
4386
4387 completing "funcÿ" looks for symbols between "funcÿ" and "fund"
4388 (exclusive), which finds "funcÿ" and "funcÿ1", but not "fund".
4389
4390 And with:
4391
4392 ÿÿ (Latin1 'ÿ' [0xff])
4393 ÿÿ1
4394
4395 completing "ÿ" or "ÿÿ" looks for symbols between between "ÿÿ" and
4396 the end of the list.
4397 */
4398 std::string after = search_name;
4399 while (!after.empty () && (unsigned char) after.back () == 0xff)
4400 after.pop_back ();
4401 if (!after.empty ())
4402 after.back () = (unsigned char) after.back () + 1;
4403 return after;
4404}
4405
5c58de74 4406/* See declaration. */
61d96d7e 4407
5c58de74
PA
4408std::pair<std::vector<name_component>::const_iterator,
4409 std::vector<name_component>::const_iterator>
44ed8f3e 4410mapped_index_base::find_name_components_bounds
5c58de74 4411 (const lookup_name_info &lookup_name_without_params) const
3f563c84 4412{
5c58de74
PA
4413 auto *name_cmp
4414 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
3f563c84
PA
4415
4416 const char *cplus
c62446b1 4417 = lookup_name_without_params.cplus ().lookup_name ().c_str ();
9291a0cd 4418
3f563c84
PA
4419 /* Comparison function object for lower_bound that matches against a
4420 given symbol name. */
4421 auto lookup_compare_lower = [&] (const name_component &elem,
4422 const char *name)
4423 {
5c58de74 4424 const char *elem_qualified = this->symbol_name_at (elem.idx);
3f563c84
PA
4425 const char *elem_name = elem_qualified + elem.name_offset;
4426 return name_cmp (elem_name, name) < 0;
4427 };
4428
4429 /* Comparison function object for upper_bound that matches against a
4430 given symbol name. */
4431 auto lookup_compare_upper = [&] (const char *name,
4432 const name_component &elem)
4433 {
5c58de74 4434 const char *elem_qualified = this->symbol_name_at (elem.idx);
3f563c84
PA
4435 const char *elem_name = elem_qualified + elem.name_offset;
4436 return name_cmp (name, elem_name) < 0;
4437 };
4438
5c58de74
PA
4439 auto begin = this->name_components.begin ();
4440 auto end = this->name_components.end ();
3f563c84
PA
4441
4442 /* Find the lower bound. */
4443 auto lower = [&] ()
4444 {
5c58de74 4445 if (lookup_name_without_params.completion_mode () && cplus[0] == '\0')
3f563c84
PA
4446 return begin;
4447 else
4448 return std::lower_bound (begin, end, cplus, lookup_compare_lower);
4449 } ();
4450
4451 /* Find the upper bound. */
4452 auto upper = [&] ()
4453 {
5c58de74 4454 if (lookup_name_without_params.completion_mode ())
3f563c84 4455 {
e1ef7d7a
PA
4456 /* In completion mode, we want UPPER to point past all
4457 symbols names that have the same prefix. I.e., with
4458 these symbols, and completing "func":
4459
4460 function << lower bound
4461 function1
4462 other_function << upper bound
4463
4464 We find the upper bound by looking for the insertion
4465 point of "func"-with-last-character-incremented,
4466 i.e. "fund". */
4467 std::string after = make_sort_after_prefix_name (cplus);
4468 if (after.empty ())
3f563c84 4469 return end;
e6b2f5ef
PA
4470 return std::lower_bound (lower, end, after.c_str (),
4471 lookup_compare_lower);
3f563c84
PA
4472 }
4473 else
4474 return std::upper_bound (lower, end, cplus, lookup_compare_upper);
4475 } ();
4476
5c58de74
PA
4477 return {lower, upper};
4478}
4479
4480/* See declaration. */
4481
4482void
44ed8f3e 4483mapped_index_base::build_name_components ()
5c58de74
PA
4484{
4485 if (!this->name_components.empty ())
4486 return;
4487
4488 this->name_components_casing = case_sensitivity;
4489 auto *name_cmp
4490 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
4491
4492 /* The code below only knows how to break apart components of C++
4493 symbol names (and other languages that use '::' as
4494 namespace/module separator). If we add support for wild matching
4495 to some language that uses some other operator (E.g., Ada, Go and
4496 D use '.'), then we'll need to try splitting the symbol name
4497 according to that language too. Note that Ada does support wild
4498 matching, but doesn't currently support .gdb_index. */
44ed8f3e
PA
4499 auto count = this->symbol_name_count ();
4500 for (offset_type idx = 0; idx < count; idx++)
5c58de74 4501 {
44ed8f3e 4502 if (this->symbol_name_slot_invalid (idx))
5c58de74
PA
4503 continue;
4504
4505 const char *name = this->symbol_name_at (idx);
4506
4507 /* Add each name component to the name component table. */
4508 unsigned int previous_len = 0;
4509 for (unsigned int current_len = cp_find_first_component (name);
4510 name[current_len] != '\0';
4511 current_len += cp_find_first_component (name + current_len))
4512 {
4513 gdb_assert (name[current_len] == ':');
4514 this->name_components.push_back ({previous_len, idx});
4515 /* Skip the '::'. */
4516 current_len += 2;
4517 previous_len = current_len;
4518 }
4519 this->name_components.push_back ({previous_len, idx});
4520 }
4521
4522 /* Sort name_components elements by name. */
4523 auto name_comp_compare = [&] (const name_component &left,
4524 const name_component &right)
4525 {
4526 const char *left_qualified = this->symbol_name_at (left.idx);
4527 const char *right_qualified = this->symbol_name_at (right.idx);
4528
4529 const char *left_name = left_qualified + left.name_offset;
4530 const char *right_name = right_qualified + right.name_offset;
4531
4532 return name_cmp (left_name, right_name) < 0;
4533 };
4534
4535 std::sort (this->name_components.begin (),
4536 this->name_components.end (),
4537 name_comp_compare);
4538}
4539
4540/* Helper for dw2_expand_symtabs_matching that works with a
44ed8f3e
PA
4541 mapped_index_base instead of the containing objfile. This is split
4542 to a separate function in order to be able to unit test the
4543 name_components matching using a mock mapped_index_base. For each
5c58de74 4544 symbol name that matches, calls MATCH_CALLBACK, passing it the
44ed8f3e 4545 symbol's index in the mapped_index_base symbol table. */
5c58de74
PA
4546
4547static void
4548dw2_expand_symtabs_matching_symbol
44ed8f3e 4549 (mapped_index_base &index,
5c58de74
PA
4550 const lookup_name_info &lookup_name_in,
4551 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4552 enum search_domain kind,
4553 gdb::function_view<void (offset_type)> match_callback)
4554{
4555 lookup_name_info lookup_name_without_params
4556 = lookup_name_in.make_ignore_params ();
4557 gdb_index_symbol_name_matcher lookup_name_matcher
4558 (lookup_name_without_params);
4559
4560 /* Build the symbol name component sorted vector, if we haven't
4561 yet. */
4562 index.build_name_components ();
4563
4564 auto bounds = index.find_name_components_bounds (lookup_name_without_params);
4565
3f563c84
PA
4566 /* Now for each symbol name in range, check to see if we have a name
4567 match, and if so, call the MATCH_CALLBACK callback. */
4568
4569 /* The same symbol may appear more than once in the range though.
4570 E.g., if we're looking for symbols that complete "w", and we have
4571 a symbol named "w1::w2", we'll find the two name components for
4572 that same symbol in the range. To be sure we only call the
4573 callback once per symbol, we first collect the symbol name
4574 indexes that matched in a temporary vector and ignore
4575 duplicates. */
4576 std::vector<offset_type> matches;
5c58de74 4577 matches.reserve (std::distance (bounds.first, bounds.second));
3f563c84 4578
5c58de74 4579 for (; bounds.first != bounds.second; ++bounds.first)
3f563c84 4580 {
5c58de74 4581 const char *qualified = index.symbol_name_at (bounds.first->idx);
3f563c84
PA
4582
4583 if (!lookup_name_matcher.matches (qualified)
4584 || (symbol_matcher != NULL && !symbol_matcher (qualified)))
9291a0cd
TT
4585 continue;
4586
5c58de74 4587 matches.push_back (bounds.first->idx);
3f563c84
PA
4588 }
4589
4590 std::sort (matches.begin (), matches.end ());
4591
4592 /* Finally call the callback, once per match. */
4593 ULONGEST prev = -1;
4594 for (offset_type idx : matches)
4595 {
4596 if (prev != idx)
4597 {
4598 match_callback (idx);
4599 prev = idx;
4600 }
4601 }
4602
4603 /* Above we use a type wider than idx's for 'prev', since 0 and
4604 (offset_type)-1 are both possible values. */
4605 static_assert (sizeof (prev) > sizeof (offset_type), "");
4606}
4607
c62446b1
PA
4608#if GDB_SELF_TEST
4609
4610namespace selftests { namespace dw2_expand_symtabs_matching {
4611
a3c5fafd
PA
4612/* A mock .gdb_index/.debug_names-like name index table, enough to
4613 exercise dw2_expand_symtabs_matching_symbol, which works with the
4614 mapped_index_base interface. Builds an index from the symbol list
4615 passed as parameter to the constructor. */
4616class mock_mapped_index : public mapped_index_base
c62446b1
PA
4617{
4618public:
a3c5fafd
PA
4619 mock_mapped_index (gdb::array_view<const char *> symbols)
4620 : m_symbol_table (symbols)
c62446b1
PA
4621 {}
4622
a3c5fafd 4623 DISABLE_COPY_AND_ASSIGN (mock_mapped_index);
c62446b1 4624
a3c5fafd 4625 /* Return the number of names in the symbol table. */
632e107b 4626 size_t symbol_name_count () const override
c62446b1 4627 {
a3c5fafd 4628 return m_symbol_table.size ();
c62446b1
PA
4629 }
4630
a3c5fafd 4631 /* Get the name of the symbol at IDX in the symbol table. */
632e107b 4632 const char *symbol_name_at (offset_type idx) const override
a3c5fafd
PA
4633 {
4634 return m_symbol_table[idx];
4635 }
c62446b1 4636
a3c5fafd
PA
4637private:
4638 gdb::array_view<const char *> m_symbol_table;
c62446b1
PA
4639};
4640
4641/* Convenience function that converts a NULL pointer to a "<null>"
4642 string, to pass to print routines. */
4643
4644static const char *
4645string_or_null (const char *str)
4646{
4647 return str != NULL ? str : "<null>";
4648}
4649
4650/* Check if a lookup_name_info built from
4651 NAME/MATCH_TYPE/COMPLETION_MODE matches the symbols in the mock
4652 index. EXPECTED_LIST is the list of expected matches, in expected
4653 matching order. If no match expected, then an empty list is
4654 specified. Returns true on success. On failure prints a warning
4655 indicating the file:line that failed, and returns false. */
4656
4657static bool
4658check_match (const char *file, int line,
4659 mock_mapped_index &mock_index,
4660 const char *name, symbol_name_match_type match_type,
4661 bool completion_mode,
4662 std::initializer_list<const char *> expected_list)
4663{
4664 lookup_name_info lookup_name (name, match_type, completion_mode);
4665
4666 bool matched = true;
4667
4668 auto mismatch = [&] (const char *expected_str,
4669 const char *got)
4670 {
4671 warning (_("%s:%d: match_type=%s, looking-for=\"%s\", "
4672 "expected=\"%s\", got=\"%s\"\n"),
4673 file, line,
4674 (match_type == symbol_name_match_type::FULL
4675 ? "FULL" : "WILD"),
4676 name, string_or_null (expected_str), string_or_null (got));
4677 matched = false;
4678 };
4679
4680 auto expected_it = expected_list.begin ();
4681 auto expected_end = expected_list.end ();
4682
a3c5fafd 4683 dw2_expand_symtabs_matching_symbol (mock_index, lookup_name,
c62446b1
PA
4684 NULL, ALL_DOMAIN,
4685 [&] (offset_type idx)
4686 {
a3c5fafd 4687 const char *matched_name = mock_index.symbol_name_at (idx);
c62446b1
PA
4688 const char *expected_str
4689 = expected_it == expected_end ? NULL : *expected_it++;
4690
4691 if (expected_str == NULL || strcmp (expected_str, matched_name) != 0)
4692 mismatch (expected_str, matched_name);
4693 });
4694
4695 const char *expected_str
4696 = expected_it == expected_end ? NULL : *expected_it++;
4697 if (expected_str != NULL)
4698 mismatch (expected_str, NULL);
4699
4700 return matched;
4701}
4702
4703/* The symbols added to the mock mapped_index for testing (in
4704 canonical form). */
4705static const char *test_symbols[] = {
4706 "function",
4707 "std::bar",
4708 "std::zfunction",
4709 "std::zfunction2",
4710 "w1::w2",
4711 "ns::foo<char*>",
4712 "ns::foo<int>",
4713 "ns::foo<long>",
a20714ff
PA
4714 "ns2::tmpl<int>::foo2",
4715 "(anonymous namespace)::A::B::C",
c62446b1 4716
e1ef7d7a
PA
4717 /* These are used to check that the increment-last-char in the
4718 matching algorithm for completion doesn't match "t1_fund" when
4719 completing "t1_func". */
4720 "t1_func",
4721 "t1_func1",
4722 "t1_fund",
4723 "t1_fund1",
4724
4725 /* A UTF-8 name with multi-byte sequences to make sure that
4726 cp-name-parser understands this as a single identifier ("função"
4727 is "function" in PT). */
4728 u8"u8função",
4729
4730 /* \377 (0xff) is Latin1 'ÿ'. */
4731 "yfunc\377",
4732
4733 /* \377 (0xff) is Latin1 'ÿ'. */
4734 "\377",
4735 "\377\377123",
4736
c62446b1
PA
4737 /* A name with all sorts of complications. Starts with "z" to make
4738 it easier for the completion tests below. */
4739#define Z_SYM_NAME \
4740 "z::std::tuple<(anonymous namespace)::ui*, std::bar<(anonymous namespace)::ui> >" \
4741 "::tuple<(anonymous namespace)::ui*, " \
4742 "std::default_delete<(anonymous namespace)::ui>, void>"
4743
4744 Z_SYM_NAME
4745};
4746
a3c5fafd
PA
4747/* Returns true if the mapped_index_base::find_name_component_bounds
4748 method finds EXPECTED_SYMS in INDEX when looking for SEARCH_NAME,
4749 in completion mode. */
5c58de74
PA
4750
4751static bool
a3c5fafd 4752check_find_bounds_finds (mapped_index_base &index,
5c58de74
PA
4753 const char *search_name,
4754 gdb::array_view<const char *> expected_syms)
4755{
4756 lookup_name_info lookup_name (search_name,
4757 symbol_name_match_type::FULL, true);
4758
4759 auto bounds = index.find_name_components_bounds (lookup_name);
4760
4761 size_t distance = std::distance (bounds.first, bounds.second);
4762 if (distance != expected_syms.size ())
4763 return false;
4764
4765 for (size_t exp_elem = 0; exp_elem < distance; exp_elem++)
4766 {
4767 auto nc_elem = bounds.first + exp_elem;
4768 const char *qualified = index.symbol_name_at (nc_elem->idx);
4769 if (strcmp (qualified, expected_syms[exp_elem]) != 0)
4770 return false;
4771 }
4772
4773 return true;
4774}
4775
4776/* Test the lower-level mapped_index::find_name_component_bounds
4777 method. */
4778
c62446b1 4779static void
5c58de74
PA
4780test_mapped_index_find_name_component_bounds ()
4781{
4782 mock_mapped_index mock_index (test_symbols);
4783
a3c5fafd 4784 mock_index.build_name_components ();
5c58de74
PA
4785
4786 /* Test the lower-level mapped_index::find_name_component_bounds
4787 method in completion mode. */
4788 {
4789 static const char *expected_syms[] = {
4790 "t1_func",
4791 "t1_func1",
5c58de74
PA
4792 };
4793
a3c5fafd 4794 SELF_CHECK (check_find_bounds_finds (mock_index,
5c58de74
PA
4795 "t1_func", expected_syms));
4796 }
4797
4798 /* Check that the increment-last-char in the name matching algorithm
4799 for completion doesn't get confused with Ansi1 'ÿ' / 0xff. */
4800 {
4801 static const char *expected_syms1[] = {
4802 "\377",
4803 "\377\377123",
4804 };
a3c5fafd 4805 SELF_CHECK (check_find_bounds_finds (mock_index,
5c58de74
PA
4806 "\377", expected_syms1));
4807
4808 static const char *expected_syms2[] = {
4809 "\377\377123",
4810 };
a3c5fafd 4811 SELF_CHECK (check_find_bounds_finds (mock_index,
5c58de74
PA
4812 "\377\377", expected_syms2));
4813 }
4814}
4815
4816/* Test dw2_expand_symtabs_matching_symbol. */
4817
4818static void
4819test_dw2_expand_symtabs_matching_symbol ()
c62446b1
PA
4820{
4821 mock_mapped_index mock_index (test_symbols);
4822
4823 /* We let all tests run until the end even if some fails, for debug
4824 convenience. */
4825 bool any_mismatch = false;
4826
4827 /* Create the expected symbols list (an initializer_list). Needed
4828 because lists have commas, and we need to pass them to CHECK,
4829 which is a macro. */
4830#define EXPECT(...) { __VA_ARGS__ }
4831
4832 /* Wrapper for check_match that passes down the current
4833 __FILE__/__LINE__. */
4834#define CHECK_MATCH(NAME, MATCH_TYPE, COMPLETION_MODE, EXPECTED_LIST) \
4835 any_mismatch |= !check_match (__FILE__, __LINE__, \
4836 mock_index, \
4837 NAME, MATCH_TYPE, COMPLETION_MODE, \
4838 EXPECTED_LIST)
4839
4840 /* Identity checks. */
4841 for (const char *sym : test_symbols)
4842 {
4843 /* Should be able to match all existing symbols. */
4844 CHECK_MATCH (sym, symbol_name_match_type::FULL, false,
4845 EXPECT (sym));
4846
4847 /* Should be able to match all existing symbols with
4848 parameters. */
4849 std::string with_params = std::string (sym) + "(int)";
4850 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4851 EXPECT (sym));
4852
4853 /* Should be able to match all existing symbols with
4854 parameters and qualifiers. */
4855 with_params = std::string (sym) + " ( int ) const";
4856 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4857 EXPECT (sym));
4858
4859 /* This should really find sym, but cp-name-parser.y doesn't
4860 know about lvalue/rvalue qualifiers yet. */
4861 with_params = std::string (sym) + " ( int ) &&";
4862 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4863 {});
4864 }
4865
e1ef7d7a
PA
4866 /* Check that the name matching algorithm for completion doesn't get
4867 confused with Latin1 'ÿ' / 0xff. */
4868 {
4869 static const char str[] = "\377";
4870 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4871 EXPECT ("\377", "\377\377123"));
4872 }
4873
4874 /* Check that the increment-last-char in the matching algorithm for
4875 completion doesn't match "t1_fund" when completing "t1_func". */
4876 {
4877 static const char str[] = "t1_func";
4878 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4879 EXPECT ("t1_func", "t1_func1"));
4880 }
4881
c62446b1
PA
4882 /* Check that completion mode works at each prefix of the expected
4883 symbol name. */
4884 {
4885 static const char str[] = "function(int)";
4886 size_t len = strlen (str);
4887 std::string lookup;
4888
4889 for (size_t i = 1; i < len; i++)
4890 {
4891 lookup.assign (str, i);
4892 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4893 EXPECT ("function"));
4894 }
4895 }
4896
4897 /* While "w" is a prefix of both components, the match function
4898 should still only be called once. */
4899 {
4900 CHECK_MATCH ("w", symbol_name_match_type::FULL, true,
4901 EXPECT ("w1::w2"));
a20714ff
PA
4902 CHECK_MATCH ("w", symbol_name_match_type::WILD, true,
4903 EXPECT ("w1::w2"));
c62446b1
PA
4904 }
4905
4906 /* Same, with a "complicated" symbol. */
4907 {
4908 static const char str[] = Z_SYM_NAME;
4909 size_t len = strlen (str);
4910 std::string lookup;
4911
4912 for (size_t i = 1; i < len; i++)
4913 {
4914 lookup.assign (str, i);
4915 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4916 EXPECT (Z_SYM_NAME));
4917 }
4918 }
4919
4920 /* In FULL mode, an incomplete symbol doesn't match. */
4921 {
4922 CHECK_MATCH ("std::zfunction(int", symbol_name_match_type::FULL, false,
4923 {});
4924 }
4925
4926 /* A complete symbol with parameters matches any overload, since the
4927 index has no overload info. */
4928 {
4929 CHECK_MATCH ("std::zfunction(int)", symbol_name_match_type::FULL, true,
4930 EXPECT ("std::zfunction", "std::zfunction2"));
a20714ff
PA
4931 CHECK_MATCH ("zfunction(int)", symbol_name_match_type::WILD, true,
4932 EXPECT ("std::zfunction", "std::zfunction2"));
4933 CHECK_MATCH ("zfunc", symbol_name_match_type::WILD, true,
4934 EXPECT ("std::zfunction", "std::zfunction2"));
c62446b1
PA
4935 }
4936
4937 /* Check that whitespace is ignored appropriately. A symbol with a
4938 template argument list. */
4939 {
4940 static const char expected[] = "ns::foo<int>";
4941 CHECK_MATCH ("ns :: foo < int > ", symbol_name_match_type::FULL, false,
4942 EXPECT (expected));
a20714ff
PA
4943 CHECK_MATCH ("foo < int > ", symbol_name_match_type::WILD, false,
4944 EXPECT (expected));
c62446b1
PA
4945 }
4946
4947 /* Check that whitespace is ignored appropriately. A symbol with a
4948 template argument list that includes a pointer. */
4949 {
4950 static const char expected[] = "ns::foo<char*>";
4951 /* Try both completion and non-completion modes. */
4952 static const bool completion_mode[2] = {false, true};
4953 for (size_t i = 0; i < 2; i++)
4954 {
4955 CHECK_MATCH ("ns :: foo < char * >", symbol_name_match_type::FULL,
4956 completion_mode[i], EXPECT (expected));
a20714ff
PA
4957 CHECK_MATCH ("foo < char * >", symbol_name_match_type::WILD,
4958 completion_mode[i], EXPECT (expected));
c62446b1
PA
4959
4960 CHECK_MATCH ("ns :: foo < char * > (int)", symbol_name_match_type::FULL,
4961 completion_mode[i], EXPECT (expected));
a20714ff
PA
4962 CHECK_MATCH ("foo < char * > (int)", symbol_name_match_type::WILD,
4963 completion_mode[i], EXPECT (expected));
c62446b1
PA
4964 }
4965 }
4966
4967 {
4968 /* Check method qualifiers are ignored. */
4969 static const char expected[] = "ns::foo<char*>";
4970 CHECK_MATCH ("ns :: foo < char * > ( int ) const",
4971 symbol_name_match_type::FULL, true, EXPECT (expected));
4972 CHECK_MATCH ("ns :: foo < char * > ( int ) &&",
4973 symbol_name_match_type::FULL, true, EXPECT (expected));
a20714ff
PA
4974 CHECK_MATCH ("foo < char * > ( int ) const",
4975 symbol_name_match_type::WILD, true, EXPECT (expected));
4976 CHECK_MATCH ("foo < char * > ( int ) &&",
4977 symbol_name_match_type::WILD, true, EXPECT (expected));
c62446b1
PA
4978 }
4979
4980 /* Test lookup names that don't match anything. */
4981 {
a20714ff
PA
4982 CHECK_MATCH ("bar2", symbol_name_match_type::WILD, false,
4983 {});
4984
c62446b1
PA
4985 CHECK_MATCH ("doesntexist", symbol_name_match_type::FULL, false,
4986 {});
4987 }
4988
a20714ff
PA
4989 /* Some wild matching tests, exercising "(anonymous namespace)",
4990 which should not be confused with a parameter list. */
4991 {
4992 static const char *syms[] = {
4993 "A::B::C",
4994 "B::C",
4995 "C",
4996 "A :: B :: C ( int )",
4997 "B :: C ( int )",
4998 "C ( int )",
4999 };
5000
5001 for (const char *s : syms)
5002 {
5003 CHECK_MATCH (s, symbol_name_match_type::WILD, false,
5004 EXPECT ("(anonymous namespace)::A::B::C"));
5005 }
5006 }
5007
5008 {
5009 static const char expected[] = "ns2::tmpl<int>::foo2";
5010 CHECK_MATCH ("tmp", symbol_name_match_type::WILD, true,
5011 EXPECT (expected));
5012 CHECK_MATCH ("tmpl<", symbol_name_match_type::WILD, true,
5013 EXPECT (expected));
5014 }
5015
c62446b1
PA
5016 SELF_CHECK (!any_mismatch);
5017
5018#undef EXPECT
5019#undef CHECK_MATCH
5020}
5021
5c58de74
PA
5022static void
5023run_test ()
5024{
5025 test_mapped_index_find_name_component_bounds ();
5026 test_dw2_expand_symtabs_matching_symbol ();
5027}
5028
c62446b1
PA
5029}} // namespace selftests::dw2_expand_symtabs_matching
5030
5031#endif /* GDB_SELF_TEST */
5032
4b514bc8
JK
5033/* If FILE_MATCHER is NULL or if PER_CU has
5034 dwarf2_per_cu_quick_data::MARK set (see
5035 dw_expand_symtabs_matching_file_matcher), expand the CU and call
5036 EXPANSION_NOTIFY on it. */
5037
5038static void
5039dw2_expand_symtabs_matching_one
5040 (struct dwarf2_per_cu_data *per_cu,
5041 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
5042 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify)
5043{
5044 if (file_matcher == NULL || per_cu->v.quick->mark)
5045 {
5046 bool symtab_was_null
5047 = (per_cu->v.quick->compunit_symtab == NULL);
5048
58f0c718 5049 dw2_instantiate_symtab (per_cu, false);
4b514bc8
JK
5050
5051 if (expansion_notify != NULL
5052 && symtab_was_null
5053 && per_cu->v.quick->compunit_symtab != NULL)
5054 expansion_notify (per_cu->v.quick->compunit_symtab);
5055 }
5056}
5057
3f563c84
PA
5058/* Helper for dw2_expand_matching symtabs. Called on each symbol
5059 matched, to expand corresponding CUs that were marked. IDX is the
5060 index of the symbol name that matched. */
5061
5062static void
5063dw2_expand_marked_cus
ed2dc618 5064 (struct dwarf2_per_objfile *dwarf2_per_objfile, offset_type idx,
3f563c84
PA
5065 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
5066 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
5067 search_domain kind)
5068{
3f563c84
PA
5069 offset_type *vec, vec_len, vec_idx;
5070 bool global_seen = false;
ed2dc618 5071 mapped_index &index = *dwarf2_per_objfile->index_table;
3f563c84 5072
61920122 5073 vec = (offset_type *) (index.constant_pool
f00a2de2 5074 + MAYBE_SWAP (index.symbol_table[idx].vec));
61920122
PA
5075 vec_len = MAYBE_SWAP (vec[0]);
5076 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
5077 {
61920122
PA
5078 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[vec_idx + 1]);
5079 /* This value is only valid for index versions >= 7. */
5080 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
5081 gdb_index_symbol_kind symbol_kind =
5082 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
5083 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
5084 /* Only check the symbol attributes if they're present.
5085 Indices prior to version 7 don't record them,
5086 and indices >= 7 may elide them for certain symbols
5087 (gold does this). */
5088 int attrs_valid =
5089 (index.version >= 7
5090 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
5091
5092 /* Work around gold/15646. */
5093 if (attrs_valid)
9291a0cd 5094 {
61920122
PA
5095 if (!is_static && global_seen)
5096 continue;
5097 if (!is_static)
5098 global_seen = true;
5099 }
3190f0c6 5100
61920122
PA
5101 /* Only check the symbol's kind if it has one. */
5102 if (attrs_valid)
5103 {
5104 switch (kind)
8943b874 5105 {
61920122
PA
5106 case VARIABLES_DOMAIN:
5107 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
5108 continue;
5109 break;
5110 case FUNCTIONS_DOMAIN:
5111 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
8943b874 5112 continue;
61920122
PA
5113 break;
5114 case TYPES_DOMAIN:
5115 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
5116 continue;
5117 break;
5118 default:
5119 break;
8943b874 5120 }
61920122 5121 }
8943b874 5122
61920122 5123 /* Don't crash on bad data. */
b76e467d 5124 if (cu_index >= (dwarf2_per_objfile->all_comp_units.size ()
b2bdb8cf 5125 + dwarf2_per_objfile->all_type_units.size ()))
61920122 5126 {
b98664d3 5127 complaint (_(".gdb_index entry has bad CU index"
ed2dc618
SM
5128 " [in module %s]"),
5129 objfile_name (dwarf2_per_objfile->objfile));
61920122
PA
5130 continue;
5131 }
5132
ff4c9fec 5133 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (cu_index);
4b514bc8
JK
5134 dw2_expand_symtabs_matching_one (per_cu, file_matcher,
5135 expansion_notify);
61920122
PA
5136 }
5137}
5138
4b514bc8
JK
5139/* If FILE_MATCHER is non-NULL, set all the
5140 dwarf2_per_cu_quick_data::MARK of the current DWARF2_PER_OBJFILE
5141 that match FILE_MATCHER. */
5142
61920122 5143static void
4b514bc8 5144dw_expand_symtabs_matching_file_matcher
ed2dc618
SM
5145 (struct dwarf2_per_objfile *dwarf2_per_objfile,
5146 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher)
61920122 5147{
4b514bc8 5148 if (file_matcher == NULL)
61920122
PA
5149 return;
5150
4b514bc8
JK
5151 objfile *const objfile = dwarf2_per_objfile->objfile;
5152
5153 htab_up visited_found (htab_create_alloc (10, htab_hash_pointer,
5154 htab_eq_pointer,
5155 NULL, xcalloc, xfree));
5156 htab_up visited_not_found (htab_create_alloc (10, htab_hash_pointer,
61920122
PA
5157 htab_eq_pointer,
5158 NULL, xcalloc, xfree));
61920122 5159
4b514bc8
JK
5160 /* The rule is CUs specify all the files, including those used by
5161 any TU, so there's no need to scan TUs here. */
61920122 5162
b76e467d 5163 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
927aa2e7 5164 {
927aa2e7
JK
5165 QUIT;
5166
5167 per_cu->v.quick->mark = 0;
5168
5169 /* We only need to look at symtabs not already expanded. */
5170 if (per_cu->v.quick->compunit_symtab)
5171 continue;
5172
b76e467d 5173 quick_file_names *file_data = dw2_get_file_names (per_cu);
927aa2e7
JK
5174 if (file_data == NULL)
5175 continue;
5176
5177 if (htab_find (visited_not_found.get (), file_data) != NULL)
5178 continue;
5179 else if (htab_find (visited_found.get (), file_data) != NULL)
5180 {
5181 per_cu->v.quick->mark = 1;
5182 continue;
5183 }
5184
b76e467d 5185 for (int j = 0; j < file_data->num_file_names; ++j)
927aa2e7
JK
5186 {
5187 const char *this_real_name;
5188
5189 if (file_matcher (file_data->file_names[j], false))
5190 {
5191 per_cu->v.quick->mark = 1;
5192 break;
5193 }
5194
5195 /* Before we invoke realpath, which can get expensive when many
5196 files are involved, do a quick comparison of the basenames. */
5197 if (!basenames_may_differ
5198 && !file_matcher (lbasename (file_data->file_names[j]),
5199 true))
5200 continue;
5201
5202 this_real_name = dw2_get_real_path (objfile, file_data, j);
5203 if (file_matcher (this_real_name, false))
5204 {
5205 per_cu->v.quick->mark = 1;
5206 break;
5207 }
5208 }
5209
b76e467d
SM
5210 void **slot = htab_find_slot (per_cu->v.quick->mark
5211 ? visited_found.get ()
5212 : visited_not_found.get (),
5213 file_data, INSERT);
927aa2e7
JK
5214 *slot = file_data;
5215 }
5216}
5217
5218static void
5219dw2_expand_symtabs_matching
5220 (struct objfile *objfile,
5221 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
5222 const lookup_name_info &lookup_name,
5223 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
5224 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
5225 enum search_domain kind)
5226{
ed2dc618
SM
5227 struct dwarf2_per_objfile *dwarf2_per_objfile
5228 = get_dwarf2_per_objfile (objfile);
927aa2e7
JK
5229
5230 /* index_table is NULL if OBJF_READNOW. */
5231 if (!dwarf2_per_objfile->index_table)
5232 return;
5233
ed2dc618 5234 dw_expand_symtabs_matching_file_matcher (dwarf2_per_objfile, file_matcher);
927aa2e7
JK
5235
5236 mapped_index &index = *dwarf2_per_objfile->index_table;
5237
5238 dw2_expand_symtabs_matching_symbol (index, lookup_name,
5239 symbol_matcher,
5240 kind, [&] (offset_type idx)
5241 {
ed2dc618 5242 dw2_expand_marked_cus (dwarf2_per_objfile, idx, file_matcher,
927aa2e7
JK
5243 expansion_notify, kind);
5244 });
5245}
5246
5247/* A helper for dw2_find_pc_sect_compunit_symtab which finds the most specific
5248 symtab. */
5249
5250static struct compunit_symtab *
5251recursively_find_pc_sect_compunit_symtab (struct compunit_symtab *cust,
5252 CORE_ADDR pc)
5253{
5254 int i;
5255
5256 if (COMPUNIT_BLOCKVECTOR (cust) != NULL
5257 && blockvector_contains_pc (COMPUNIT_BLOCKVECTOR (cust), pc))
5258 return cust;
5259
5260 if (cust->includes == NULL)
5261 return NULL;
5262
5263 for (i = 0; cust->includes[i]; ++i)
5264 {
5265 struct compunit_symtab *s = cust->includes[i];
5266
5267 s = recursively_find_pc_sect_compunit_symtab (s, pc);
5268 if (s != NULL)
5269 return s;
5270 }
5271
5272 return NULL;
5273}
5274
5275static struct compunit_symtab *
5276dw2_find_pc_sect_compunit_symtab (struct objfile *objfile,
5277 struct bound_minimal_symbol msymbol,
5278 CORE_ADDR pc,
5279 struct obj_section *section,
5280 int warn_if_readin)
5281{
5282 struct dwarf2_per_cu_data *data;
5283 struct compunit_symtab *result;
5284
d320c2b5 5285 if (!objfile->partial_symtabs->psymtabs_addrmap)
927aa2e7
JK
5286 return NULL;
5287
79748972
TT
5288 CORE_ADDR baseaddr = ANOFFSET (objfile->section_offsets,
5289 SECT_OFF_TEXT (objfile));
d320c2b5
TT
5290 data = (struct dwarf2_per_cu_data *) addrmap_find
5291 (objfile->partial_symtabs->psymtabs_addrmap, pc - baseaddr);
927aa2e7
JK
5292 if (!data)
5293 return NULL;
5294
5295 if (warn_if_readin && data->v.quick->compunit_symtab)
5296 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
5297 paddress (get_objfile_arch (objfile), pc));
5298
5299 result
58f0c718
TT
5300 = recursively_find_pc_sect_compunit_symtab (dw2_instantiate_symtab (data,
5301 false),
927aa2e7
JK
5302 pc);
5303 gdb_assert (result != NULL);
5304 return result;
5305}
5306
5307static void
5308dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
5309 void *data, int need_fullname)
5310{
ed2dc618
SM
5311 struct dwarf2_per_objfile *dwarf2_per_objfile
5312 = get_dwarf2_per_objfile (objfile);
927aa2e7
JK
5313
5314 if (!dwarf2_per_objfile->filenames_cache)
5315 {
5316 dwarf2_per_objfile->filenames_cache.emplace ();
5317
5318 htab_up visited (htab_create_alloc (10,
5319 htab_hash_pointer, htab_eq_pointer,
5320 NULL, xcalloc, xfree));
5321
5322 /* The rule is CUs specify all the files, including those used
5323 by any TU, so there's no need to scan TUs here. We can
5324 ignore file names coming from already-expanded CUs. */
5325
b76e467d 5326 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
927aa2e7 5327 {
927aa2e7
JK
5328 if (per_cu->v.quick->compunit_symtab)
5329 {
5330 void **slot = htab_find_slot (visited.get (),
5331 per_cu->v.quick->file_names,
5332 INSERT);
5333
5334 *slot = per_cu->v.quick->file_names;
5335 }
5336 }
5337
b76e467d 5338 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
927aa2e7 5339 {
927aa2e7
JK
5340 /* We only need to look at symtabs not already expanded. */
5341 if (per_cu->v.quick->compunit_symtab)
5342 continue;
5343
b76e467d 5344 quick_file_names *file_data = dw2_get_file_names (per_cu);
927aa2e7
JK
5345 if (file_data == NULL)
5346 continue;
5347
b76e467d 5348 void **slot = htab_find_slot (visited.get (), file_data, INSERT);
927aa2e7
JK
5349 if (*slot)
5350 {
5351 /* Already visited. */
5352 continue;
5353 }
5354 *slot = file_data;
5355
5356 for (int j = 0; j < file_data->num_file_names; ++j)
5357 {
5358 const char *filename = file_data->file_names[j];
5359 dwarf2_per_objfile->filenames_cache->seen (filename);
5360 }
5361 }
5362 }
5363
5364 dwarf2_per_objfile->filenames_cache->traverse ([&] (const char *filename)
5365 {
5366 gdb::unique_xmalloc_ptr<char> this_real_name;
5367
5368 if (need_fullname)
5369 this_real_name = gdb_realpath (filename);
5370 (*fun) (filename, this_real_name.get (), data);
5371 });
5372}
5373
5374static int
5375dw2_has_symbols (struct objfile *objfile)
5376{
5377 return 1;
5378}
5379
5380const struct quick_symbol_functions dwarf2_gdb_index_functions =
5381{
5382 dw2_has_symbols,
5383 dw2_find_last_source_symtab,
5384 dw2_forget_cached_source_info,
5385 dw2_map_symtabs_matching_filename,
5386 dw2_lookup_symbol,
5387 dw2_print_stats,
5388 dw2_dump,
927aa2e7
JK
5389 dw2_expand_symtabs_for_function,
5390 dw2_expand_all_symtabs,
5391 dw2_expand_symtabs_with_fullname,
5392 dw2_map_matching_symbols,
5393 dw2_expand_symtabs_matching,
5394 dw2_find_pc_sect_compunit_symtab,
5395 NULL,
5396 dw2_map_symbol_filenames
5397};
5398
5399/* DWARF-5 debug_names reader. */
5400
5401/* DWARF-5 augmentation string for GDB's DW_IDX_GNU_* extension. */
5402static const gdb_byte dwarf5_augmentation[] = { 'G', 'D', 'B', 0 };
5403
5404/* A helper function that reads the .debug_names section in SECTION
5405 and fills in MAP. FILENAME is the name of the file containing the
5406 section; it is used for error reporting.
5407
5408 Returns true if all went well, false otherwise. */
5409
5410static bool
5411read_debug_names_from_section (struct objfile *objfile,
5412 const char *filename,
5413 struct dwarf2_section_info *section,
5414 mapped_debug_names &map)
5415{
5416 if (dwarf2_section_empty_p (section))
5417 return false;
5418
5419 /* Older elfutils strip versions could keep the section in the main
5420 executable while splitting it for the separate debug info file. */
5421 if ((get_section_flags (section) & SEC_HAS_CONTENTS) == 0)
5422 return false;
5423
5424 dwarf2_read_section (objfile, section);
5425
5426 map.dwarf5_byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
5427
5428 const gdb_byte *addr = section->buffer;
5429
5430 bfd *const abfd = get_section_bfd_owner (section);
5431
5432 unsigned int bytes_read;
5433 LONGEST length = read_initial_length (abfd, addr, &bytes_read);
5434 addr += bytes_read;
5435
5436 map.dwarf5_is_dwarf64 = bytes_read != 4;
5437 map.offset_size = map.dwarf5_is_dwarf64 ? 8 : 4;
5438 if (bytes_read + length != section->size)
5439 {
5440 /* There may be multiple per-CU indices. */
5441 warning (_("Section .debug_names in %s length %s does not match "
5442 "section length %s, ignoring .debug_names."),
5443 filename, plongest (bytes_read + length),
5444 pulongest (section->size));
5445 return false;
5446 }
5447
5448 /* The version number. */
5449 uint16_t version = read_2_bytes (abfd, addr);
5450 addr += 2;
5451 if (version != 5)
5452 {
5453 warning (_("Section .debug_names in %s has unsupported version %d, "
5454 "ignoring .debug_names."),
5455 filename, version);
5456 return false;
5457 }
5458
5459 /* Padding. */
5460 uint16_t padding = read_2_bytes (abfd, addr);
5461 addr += 2;
5462 if (padding != 0)
5463 {
5464 warning (_("Section .debug_names in %s has unsupported padding %d, "
5465 "ignoring .debug_names."),
5466 filename, padding);
5467 return false;
5468 }
5469
5470 /* comp_unit_count - The number of CUs in the CU list. */
5471 map.cu_count = read_4_bytes (abfd, addr);
5472 addr += 4;
5473
5474 /* local_type_unit_count - The number of TUs in the local TU
5475 list. */
5476 map.tu_count = read_4_bytes (abfd, addr);
5477 addr += 4;
5478
5479 /* foreign_type_unit_count - The number of TUs in the foreign TU
5480 list. */
5481 uint32_t foreign_tu_count = read_4_bytes (abfd, addr);
5482 addr += 4;
5483 if (foreign_tu_count != 0)
5484 {
5485 warning (_("Section .debug_names in %s has unsupported %lu foreign TUs, "
5486 "ignoring .debug_names."),
5487 filename, static_cast<unsigned long> (foreign_tu_count));
5488 return false;
5489 }
5490
5491 /* bucket_count - The number of hash buckets in the hash lookup
5492 table. */
5493 map.bucket_count = read_4_bytes (abfd, addr);
5494 addr += 4;
5495
5496 /* name_count - The number of unique names in the index. */
5497 map.name_count = read_4_bytes (abfd, addr);
5498 addr += 4;
5499
5500 /* abbrev_table_size - The size in bytes of the abbreviations
5501 table. */
5502 uint32_t abbrev_table_size = read_4_bytes (abfd, addr);
5503 addr += 4;
5504
5505 /* augmentation_string_size - The size in bytes of the augmentation
5506 string. This value is rounded up to a multiple of 4. */
5507 uint32_t augmentation_string_size = read_4_bytes (abfd, addr);
5508 addr += 4;
5509 map.augmentation_is_gdb = ((augmentation_string_size
5510 == sizeof (dwarf5_augmentation))
5511 && memcmp (addr, dwarf5_augmentation,
5512 sizeof (dwarf5_augmentation)) == 0);
5513 augmentation_string_size += (-augmentation_string_size) & 3;
5514 addr += augmentation_string_size;
5515
5516 /* List of CUs */
5517 map.cu_table_reordered = addr;
5518 addr += map.cu_count * map.offset_size;
5519
5520 /* List of Local TUs */
5521 map.tu_table_reordered = addr;
5522 addr += map.tu_count * map.offset_size;
5523
5524 /* Hash Lookup Table */
5525 map.bucket_table_reordered = reinterpret_cast<const uint32_t *> (addr);
5526 addr += map.bucket_count * 4;
5527 map.hash_table_reordered = reinterpret_cast<const uint32_t *> (addr);
5528 addr += map.name_count * 4;
5529
5530 /* Name Table */
5531 map.name_table_string_offs_reordered = addr;
5532 addr += map.name_count * map.offset_size;
5533 map.name_table_entry_offs_reordered = addr;
5534 addr += map.name_count * map.offset_size;
5535
5536 const gdb_byte *abbrev_table_start = addr;
5537 for (;;)
5538 {
927aa2e7
JK
5539 const ULONGEST index_num = read_unsigned_leb128 (abfd, addr, &bytes_read);
5540 addr += bytes_read;
5541 if (index_num == 0)
5542 break;
5543
5544 const auto insertpair
5545 = map.abbrev_map.emplace (index_num, mapped_debug_names::index_val ());
5546 if (!insertpair.second)
5547 {
5548 warning (_("Section .debug_names in %s has duplicate index %s, "
5549 "ignoring .debug_names."),
5550 filename, pulongest (index_num));
5551 return false;
5552 }
5553 mapped_debug_names::index_val &indexval = insertpair.first->second;
5554 indexval.dwarf_tag = read_unsigned_leb128 (abfd, addr, &bytes_read);
5555 addr += bytes_read;
5556
5557 for (;;)
5558 {
5559 mapped_debug_names::index_val::attr attr;
5560 attr.dw_idx = read_unsigned_leb128 (abfd, addr, &bytes_read);
5561 addr += bytes_read;
5562 attr.form = read_unsigned_leb128 (abfd, addr, &bytes_read);
5563 addr += bytes_read;
5564 if (attr.form == DW_FORM_implicit_const)
5565 {
5566 attr.implicit_const = read_signed_leb128 (abfd, addr,
5567 &bytes_read);
5568 addr += bytes_read;
5569 }
5570 if (attr.dw_idx == 0 && attr.form == 0)
5571 break;
5572 indexval.attr_vec.push_back (std::move (attr));
5573 }
5574 }
5575 if (addr != abbrev_table_start + abbrev_table_size)
5576 {
5577 warning (_("Section .debug_names in %s has abbreviation_table "
5578 "of size %zu vs. written as %u, ignoring .debug_names."),
5579 filename, addr - abbrev_table_start, abbrev_table_size);
5580 return false;
5581 }
5582 map.entry_pool = addr;
5583
5584 return true;
5585}
5586
5587/* A helper for create_cus_from_debug_names that handles the MAP's CU
5588 list. */
5589
5590static void
ed2dc618 5591create_cus_from_debug_names_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
927aa2e7
JK
5592 const mapped_debug_names &map,
5593 dwarf2_section_info &section,
b76e467d 5594 bool is_dwz)
927aa2e7
JK
5595{
5596 sect_offset sect_off_prev;
5597 for (uint32_t i = 0; i <= map.cu_count; ++i)
5598 {
5599 sect_offset sect_off_next;
5600 if (i < map.cu_count)
5601 {
5602 sect_off_next
5603 = (sect_offset) (extract_unsigned_integer
5604 (map.cu_table_reordered + i * map.offset_size,
5605 map.offset_size,
5606 map.dwarf5_byte_order));
5607 }
5608 else
5609 sect_off_next = (sect_offset) section.size;
5610 if (i >= 1)
5611 {
5612 const ULONGEST length = sect_off_next - sect_off_prev;
b76e467d 5613 dwarf2_per_cu_data *per_cu
ed2dc618 5614 = create_cu_from_index_list (dwarf2_per_objfile, &section, is_dwz,
927aa2e7 5615 sect_off_prev, length);
b76e467d 5616 dwarf2_per_objfile->all_comp_units.push_back (per_cu);
927aa2e7
JK
5617 }
5618 sect_off_prev = sect_off_next;
5619 }
5620}
5621
5622/* Read the CU list from the mapped index, and use it to create all
ed2dc618 5623 the CU objects for this dwarf2_per_objfile. */
927aa2e7
JK
5624
5625static void
ed2dc618 5626create_cus_from_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile,
927aa2e7
JK
5627 const mapped_debug_names &map,
5628 const mapped_debug_names &dwz_map)
5629{
b76e467d
SM
5630 gdb_assert (dwarf2_per_objfile->all_comp_units.empty ());
5631 dwarf2_per_objfile->all_comp_units.reserve (map.cu_count + dwz_map.cu_count);
927aa2e7 5632
ed2dc618
SM
5633 create_cus_from_debug_names_list (dwarf2_per_objfile, map,
5634 dwarf2_per_objfile->info,
b76e467d 5635 false /* is_dwz */);
927aa2e7
JK
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,
b76e467d 5642 true /* is_dwz */);
927aa2e7
JK
5643}
5644
5645/* Read .debug_names. If everything went ok, initialize the "quick"
5646 elements of all the CUs and return true. Otherwise, return false. */
5647
5648static bool
ed2dc618 5649dwarf2_read_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile)
927aa2e7 5650{
22ca247e
TT
5651 std::unique_ptr<mapped_debug_names> map
5652 (new mapped_debug_names (dwarf2_per_objfile));
ed2dc618
SM
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,
22ca247e 5658 *map))
927aa2e7
JK
5659 return false;
5660
5661 /* Don't use the index if it's empty. */
22ca247e 5662 if (map->name_count == 0)
927aa2e7
JK
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
22ca247e 5680 create_cus_from_debug_names (dwarf2_per_objfile, *map, dwz_map);
927aa2e7 5681
22ca247e 5682 if (map->tu_count != 0)
927aa2e7
JK
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
22ca247e 5693 (dwarf2_per_objfile, *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
22ca247e 5699 dwarf2_per_objfile->debug_names_table = std::move (map);
927aa2e7
JK
5700 dwarf2_per_objfile->using_index = 1;
5701 dwarf2_per_objfile->quick_file_names_table =
b76e467d 5702 create_quick_file_names_table (dwarf2_per_objfile->all_comp_units.size ());
927aa2e7
JK
5703
5704 return true;
5705}
5706
927aa2e7
JK
5707/* Type used to manage iterating over all CUs looking for a symbol for
5708 .debug_names. */
5709
5710class dw2_debug_names_iterator
5711{
5712public:
5713 /* If WANT_SPECIFIC_BLOCK is true, only look for symbols in block
5714 BLOCK_INDEX. Otherwise BLOCK_INDEX is ignored. */
5715 dw2_debug_names_iterator (const mapped_debug_names &map,
5716 bool want_specific_block,
5717 block_enum block_index, domain_enum domain,
5718 const char *name)
5719 : m_map (map), m_want_specific_block (want_specific_block),
5720 m_block_index (block_index), m_domain (domain),
5721 m_addr (find_vec_in_debug_names (map, name))
5722 {}
5723
5724 dw2_debug_names_iterator (const mapped_debug_names &map,
5725 search_domain search, uint32_t namei)
5726 : m_map (map),
5727 m_search (search),
5728 m_addr (find_vec_in_debug_names (map, namei))
5729 {}
5730
5731 /* Return the next matching CU or NULL if there are no more. */
5732 dwarf2_per_cu_data *next ();
5733
5734private:
5735 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5736 const char *name);
5737 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5738 uint32_t namei);
5739
5740 /* The internalized form of .debug_names. */
5741 const mapped_debug_names &m_map;
5742
5743 /* If true, only look for symbols that match BLOCK_INDEX. */
5744 const bool m_want_specific_block = false;
5745
5746 /* One of GLOBAL_BLOCK or STATIC_BLOCK.
5747 Unused if !WANT_SPECIFIC_BLOCK - FIRST_LOCAL_BLOCK is an invalid
5748 value. */
5749 const block_enum m_block_index = FIRST_LOCAL_BLOCK;
5750
5751 /* The kind of symbol we're looking for. */
5752 const domain_enum m_domain = UNDEF_DOMAIN;
5753 const search_domain m_search = ALL_DOMAIN;
5754
5755 /* The list of CUs from the index entry of the symbol, or NULL if
5756 not found. */
5757 const gdb_byte *m_addr;
5758};
5759
5760const char *
5761mapped_debug_names::namei_to_name (uint32_t namei) const
5762{
5763 const ULONGEST namei_string_offs
5764 = extract_unsigned_integer ((name_table_string_offs_reordered
5765 + namei * offset_size),
5766 offset_size,
5767 dwarf5_byte_order);
5768 return read_indirect_string_at_offset
ed2dc618 5769 (dwarf2_per_objfile, dwarf2_per_objfile->objfile->obfd, namei_string_offs);
927aa2e7
JK
5770}
5771
5772/* Find a slot in .debug_names for the object named NAME. If NAME is
5773 found, return pointer to its pool data. If NAME cannot be found,
5774 return NULL. */
5775
5776const gdb_byte *
5777dw2_debug_names_iterator::find_vec_in_debug_names
5778 (const mapped_debug_names &map, const char *name)
5779{
5780 int (*cmp) (const char *, const char *);
5781
5782 if (current_language->la_language == language_cplus
5783 || current_language->la_language == language_fortran
5784 || current_language->la_language == language_d)
5785 {
5786 /* NAME is already canonical. Drop any qualifiers as
5787 .debug_names does not contain any. */
5788
5789 if (strchr (name, '(') != NULL)
5790 {
5791 gdb::unique_xmalloc_ptr<char> without_params
5792 = cp_remove_params (name);
5793
5794 if (without_params != NULL)
5795 {
5796 name = without_params.get();
5797 }
5798 }
5799 }
5800
5801 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
5802
5803 const uint32_t full_hash = dwarf5_djb_hash (name);
5804 uint32_t namei
5805 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5806 (map.bucket_table_reordered
5807 + (full_hash % map.bucket_count)), 4,
5808 map.dwarf5_byte_order);
5809 if (namei == 0)
5810 return NULL;
5811 --namei;
5812 if (namei >= map.name_count)
5813 {
b98664d3 5814 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
927aa2e7
JK
5815 "[in module %s]"),
5816 namei, map.name_count,
ed2dc618 5817 objfile_name (map.dwarf2_per_objfile->objfile));
927aa2e7
JK
5818 return NULL;
5819 }
5820
5821 for (;;)
5822 {
5823 const uint32_t namei_full_hash
5824 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5825 (map.hash_table_reordered + namei), 4,
5826 map.dwarf5_byte_order);
5827 if (full_hash % map.bucket_count != namei_full_hash % map.bucket_count)
5828 return NULL;
5829
5830 if (full_hash == namei_full_hash)
5831 {
5832 const char *const namei_string = map.namei_to_name (namei);
5833
5834#if 0 /* An expensive sanity check. */
5835 if (namei_full_hash != dwarf5_djb_hash (namei_string))
5836 {
b98664d3 5837 complaint (_("Wrong .debug_names hash for string at index %u "
927aa2e7
JK
5838 "[in module %s]"),
5839 namei, objfile_name (dwarf2_per_objfile->objfile));
5840 return NULL;
5841 }
5842#endif
5843
5844 if (cmp (namei_string, name) == 0)
5845 {
5846 const ULONGEST namei_entry_offs
5847 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5848 + namei * map.offset_size),
5849 map.offset_size, map.dwarf5_byte_order);
5850 return map.entry_pool + namei_entry_offs;
5851 }
5852 }
5853
5854 ++namei;
5855 if (namei >= map.name_count)
5856 return NULL;
5857 }
5858}
5859
5860const gdb_byte *
5861dw2_debug_names_iterator::find_vec_in_debug_names
5862 (const mapped_debug_names &map, uint32_t namei)
5863{
5864 if (namei >= map.name_count)
5865 {
b98664d3 5866 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
927aa2e7
JK
5867 "[in module %s]"),
5868 namei, map.name_count,
ed2dc618 5869 objfile_name (map.dwarf2_per_objfile->objfile));
927aa2e7
JK
5870 return NULL;
5871 }
5872
5873 const ULONGEST namei_entry_offs
5874 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5875 + namei * map.offset_size),
5876 map.offset_size, map.dwarf5_byte_order);
5877 return map.entry_pool + namei_entry_offs;
5878}
5879
5880/* See dw2_debug_names_iterator. */
5881
5882dwarf2_per_cu_data *
5883dw2_debug_names_iterator::next ()
5884{
5885 if (m_addr == NULL)
5886 return NULL;
5887
ed2dc618
SM
5888 struct dwarf2_per_objfile *dwarf2_per_objfile = m_map.dwarf2_per_objfile;
5889 struct objfile *objfile = dwarf2_per_objfile->objfile;
5890 bfd *const abfd = objfile->obfd;
927aa2e7
JK
5891
5892 again:
5893
5894 unsigned int bytes_read;
5895 const ULONGEST abbrev = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5896 m_addr += bytes_read;
5897 if (abbrev == 0)
5898 return NULL;
5899
5900 const auto indexval_it = m_map.abbrev_map.find (abbrev);
5901 if (indexval_it == m_map.abbrev_map.cend ())
5902 {
b98664d3 5903 complaint (_("Wrong .debug_names undefined abbrev code %s "
927aa2e7 5904 "[in module %s]"),
ed2dc618 5905 pulongest (abbrev), objfile_name (objfile));
927aa2e7
JK
5906 return NULL;
5907 }
5908 const mapped_debug_names::index_val &indexval = indexval_it->second;
5909 bool have_is_static = false;
5910 bool is_static;
5911 dwarf2_per_cu_data *per_cu = NULL;
5912 for (const mapped_debug_names::index_val::attr &attr : indexval.attr_vec)
5913 {
5914 ULONGEST ull;
5915 switch (attr.form)
5916 {
5917 case DW_FORM_implicit_const:
5918 ull = attr.implicit_const;
5919 break;
5920 case DW_FORM_flag_present:
5921 ull = 1;
5922 break;
5923 case DW_FORM_udata:
5924 ull = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5925 m_addr += bytes_read;
5926 break;
5927 default:
b98664d3 5928 complaint (_("Unsupported .debug_names form %s [in module %s]"),
927aa2e7 5929 dwarf_form_name (attr.form),
ed2dc618 5930 objfile_name (objfile));
927aa2e7
JK
5931 return NULL;
5932 }
5933 switch (attr.dw_idx)
5934 {
5935 case DW_IDX_compile_unit:
5936 /* Don't crash on bad data. */
b76e467d 5937 if (ull >= dwarf2_per_objfile->all_comp_units.size ())
927aa2e7 5938 {
b98664d3 5939 complaint (_(".debug_names entry has bad CU index %s"
927aa2e7
JK
5940 " [in module %s]"),
5941 pulongest (ull),
5942 objfile_name (dwarf2_per_objfile->objfile));
5943 continue;
5944 }
ff4c9fec 5945 per_cu = dwarf2_per_objfile->get_cutu (ull);
927aa2e7 5946 break;
8af5c486
JK
5947 case DW_IDX_type_unit:
5948 /* Don't crash on bad data. */
b2bdb8cf 5949 if (ull >= dwarf2_per_objfile->all_type_units.size ())
8af5c486 5950 {
b98664d3 5951 complaint (_(".debug_names entry has bad TU index %s"
8af5c486
JK
5952 " [in module %s]"),
5953 pulongest (ull),
5954 objfile_name (dwarf2_per_objfile->objfile));
5955 continue;
5956 }
ff4c9fec 5957 per_cu = &dwarf2_per_objfile->get_tu (ull)->per_cu;
8af5c486 5958 break;
927aa2e7
JK
5959 case DW_IDX_GNU_internal:
5960 if (!m_map.augmentation_is_gdb)
5961 break;
5962 have_is_static = true;
5963 is_static = true;
5964 break;
5965 case DW_IDX_GNU_external:
5966 if (!m_map.augmentation_is_gdb)
5967 break;
5968 have_is_static = true;
5969 is_static = false;
5970 break;
5971 }
5972 }
5973
5974 /* Skip if already read in. */
5975 if (per_cu->v.quick->compunit_symtab)
5976 goto again;
5977
5978 /* Check static vs global. */
5979 if (have_is_static)
5980 {
5981 const bool want_static = m_block_index != GLOBAL_BLOCK;
5982 if (m_want_specific_block && want_static != is_static)
5983 goto again;
5984 }
5985
5986 /* Match dw2_symtab_iter_next, symbol_kind
5987 and debug_names::psymbol_tag. */
5988 switch (m_domain)
5989 {
5990 case VAR_DOMAIN:
5991 switch (indexval.dwarf_tag)
5992 {
5993 case DW_TAG_variable:
5994 case DW_TAG_subprogram:
5995 /* Some types are also in VAR_DOMAIN. */
5996 case DW_TAG_typedef:
5997 case DW_TAG_structure_type:
5998 break;
5999 default:
6000 goto again;
6001 }
6002 break;
6003 case STRUCT_DOMAIN:
6004 switch (indexval.dwarf_tag)
6005 {
6006 case DW_TAG_typedef:
6007 case DW_TAG_structure_type:
6008 break;
6009 default:
6010 goto again;
6011 }
6012 break;
6013 case LABEL_DOMAIN:
6014 switch (indexval.dwarf_tag)
6015 {
6016 case 0:
6017 case DW_TAG_variable:
6018 break;
6019 default:
6020 goto again;
6021 }
6022 break;
6023 default:
6024 break;
6025 }
6026
6027 /* Match dw2_expand_symtabs_matching, symbol_kind and
6028 debug_names::psymbol_tag. */
6029 switch (m_search)
4b514bc8 6030 {
927aa2e7
JK
6031 case VARIABLES_DOMAIN:
6032 switch (indexval.dwarf_tag)
4b514bc8 6033 {
927aa2e7
JK
6034 case DW_TAG_variable:
6035 break;
6036 default:
6037 goto again;
4b514bc8 6038 }
927aa2e7
JK
6039 break;
6040 case FUNCTIONS_DOMAIN:
6041 switch (indexval.dwarf_tag)
4b514bc8 6042 {
927aa2e7
JK
6043 case DW_TAG_subprogram:
6044 break;
6045 default:
6046 goto again;
4b514bc8 6047 }
927aa2e7
JK
6048 break;
6049 case TYPES_DOMAIN:
6050 switch (indexval.dwarf_tag)
6051 {
6052 case DW_TAG_typedef:
6053 case DW_TAG_structure_type:
6054 break;
6055 default:
6056 goto again;
6057 }
6058 break;
6059 default:
6060 break;
4b514bc8 6061 }
927aa2e7
JK
6062
6063 return per_cu;
4b514bc8 6064}
61920122 6065
927aa2e7
JK
6066static struct compunit_symtab *
6067dw2_debug_names_lookup_symbol (struct objfile *objfile, int block_index_int,
6068 const char *name, domain_enum domain)
4b514bc8 6069{
927aa2e7 6070 const block_enum block_index = static_cast<block_enum> (block_index_int);
ed2dc618
SM
6071 struct dwarf2_per_objfile *dwarf2_per_objfile
6072 = get_dwarf2_per_objfile (objfile);
61920122 6073
927aa2e7
JK
6074 const auto &mapp = dwarf2_per_objfile->debug_names_table;
6075 if (!mapp)
61920122 6076 {
927aa2e7
JK
6077 /* index is NULL if OBJF_READNOW. */
6078 return NULL;
6079 }
6080 const auto &map = *mapp;
9291a0cd 6081
927aa2e7
JK
6082 dw2_debug_names_iterator iter (map, true /* want_specific_block */,
6083 block_index, domain, name);
9703b513 6084
927aa2e7
JK
6085 struct compunit_symtab *stab_best = NULL;
6086 struct dwarf2_per_cu_data *per_cu;
6087 while ((per_cu = iter.next ()) != NULL)
6088 {
6089 struct symbol *sym, *with_opaque = NULL;
58f0c718 6090 struct compunit_symtab *stab = dw2_instantiate_symtab (per_cu, false);
927aa2e7 6091 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
582942f4 6092 const struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
9703b513 6093
927aa2e7
JK
6094 sym = block_find_symbol (block, name, domain,
6095 block_find_non_opaque_type_preferred,
6096 &with_opaque);
9703b513 6097
927aa2e7
JK
6098 /* Some caution must be observed with overloaded functions and
6099 methods, since the index will not contain any overload
6100 information (but NAME might contain it). */
a3ec0bb1 6101
927aa2e7
JK
6102 if (sym != NULL
6103 && strcmp_iw (SYMBOL_SEARCH_NAME (sym), name) == 0)
6104 return stab;
6105 if (with_opaque != NULL
6106 && strcmp_iw (SYMBOL_SEARCH_NAME (with_opaque), name) == 0)
6107 stab_best = stab;
9703b513 6108
927aa2e7 6109 /* Keep looking through other CUs. */
9703b513
TT
6110 }
6111
927aa2e7 6112 return stab_best;
9703b513
TT
6113}
6114
927aa2e7
JK
6115/* This dumps minimal information about .debug_names. It is called
6116 via "mt print objfiles". The gdb.dwarf2/gdb-index.exp testcase
6117 uses this to verify that .debug_names has been loaded. */
9291a0cd 6118
927aa2e7
JK
6119static void
6120dw2_debug_names_dump (struct objfile *objfile)
6121{
ed2dc618
SM
6122 struct dwarf2_per_objfile *dwarf2_per_objfile
6123 = get_dwarf2_per_objfile (objfile);
6124
927aa2e7
JK
6125 gdb_assert (dwarf2_per_objfile->using_index);
6126 printf_filtered (".debug_names:");
6127 if (dwarf2_per_objfile->debug_names_table)
6128 printf_filtered (" exists\n");
6129 else
6130 printf_filtered (" faked for \"readnow\"\n");
6131 printf_filtered ("\n");
9291a0cd
TT
6132}
6133
9291a0cd 6134static void
927aa2e7
JK
6135dw2_debug_names_expand_symtabs_for_function (struct objfile *objfile,
6136 const char *func_name)
9291a0cd 6137{
ed2dc618
SM
6138 struct dwarf2_per_objfile *dwarf2_per_objfile
6139 = get_dwarf2_per_objfile (objfile);
ae2de4f8 6140
927aa2e7
JK
6141 /* dwarf2_per_objfile->debug_names_table is NULL if OBJF_READNOW. */
6142 if (dwarf2_per_objfile->debug_names_table)
24c79950 6143 {
927aa2e7 6144 const mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
24c79950 6145
927aa2e7
JK
6146 /* Note: It doesn't matter what we pass for block_index here. */
6147 dw2_debug_names_iterator iter (map, false /* want_specific_block */,
6148 GLOBAL_BLOCK, VAR_DOMAIN, func_name);
24c79950 6149
927aa2e7
JK
6150 struct dwarf2_per_cu_data *per_cu;
6151 while ((per_cu = iter.next ()) != NULL)
58f0c718 6152 dw2_instantiate_symtab (per_cu, false);
927aa2e7
JK
6153 }
6154}
24c79950 6155
927aa2e7
JK
6156static void
6157dw2_debug_names_expand_symtabs_matching
6158 (struct objfile *objfile,
6159 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
6160 const lookup_name_info &lookup_name,
6161 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
6162 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
6163 enum search_domain kind)
6164{
ed2dc618
SM
6165 struct dwarf2_per_objfile *dwarf2_per_objfile
6166 = get_dwarf2_per_objfile (objfile);
9291a0cd 6167
927aa2e7
JK
6168 /* debug_names_table is NULL if OBJF_READNOW. */
6169 if (!dwarf2_per_objfile->debug_names_table)
6170 return;
9291a0cd 6171
ed2dc618 6172 dw_expand_symtabs_matching_file_matcher (dwarf2_per_objfile, file_matcher);
24c79950 6173
44ed8f3e 6174 mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
bbf2f4df 6175
44ed8f3e
PA
6176 dw2_expand_symtabs_matching_symbol (map, lookup_name,
6177 symbol_matcher,
6178 kind, [&] (offset_type namei)
927aa2e7 6179 {
927aa2e7
JK
6180 /* The name was matched, now expand corresponding CUs that were
6181 marked. */
6182 dw2_debug_names_iterator iter (map, kind, namei);
bbf2f4df 6183
927aa2e7
JK
6184 struct dwarf2_per_cu_data *per_cu;
6185 while ((per_cu = iter.next ()) != NULL)
6186 dw2_expand_symtabs_matching_one (per_cu, file_matcher,
6187 expansion_notify);
44ed8f3e 6188 });
9291a0cd
TT
6189}
6190
927aa2e7 6191const struct quick_symbol_functions dwarf2_debug_names_functions =
9291a0cd
TT
6192{
6193 dw2_has_symbols,
6194 dw2_find_last_source_symtab,
6195 dw2_forget_cached_source_info,
f8eba3c6 6196 dw2_map_symtabs_matching_filename,
927aa2e7 6197 dw2_debug_names_lookup_symbol,
9291a0cd 6198 dw2_print_stats,
927aa2e7 6199 dw2_debug_names_dump,
927aa2e7 6200 dw2_debug_names_expand_symtabs_for_function,
9291a0cd 6201 dw2_expand_all_symtabs,
652a8996 6202 dw2_expand_symtabs_with_fullname,
40658b94 6203 dw2_map_matching_symbols,
927aa2e7 6204 dw2_debug_names_expand_symtabs_matching,
43f3e411 6205 dw2_find_pc_sect_compunit_symtab,
71a3c369 6206 NULL,
9291a0cd
TT
6207 dw2_map_symbol_filenames
6208};
6209
4485a1c1
SM
6210/* Get the content of the .gdb_index section of OBJ. SECTION_OWNER should point
6211 to either a dwarf2_per_objfile or dwz_file object. */
6212
6213template <typename T>
6214static gdb::array_view<const gdb_byte>
6215get_gdb_index_contents_from_section (objfile *obj, T *section_owner)
6216{
6217 dwarf2_section_info *section = &section_owner->gdb_index;
6218
6219 if (dwarf2_section_empty_p (section))
6220 return {};
6221
6222 /* Older elfutils strip versions could keep the section in the main
6223 executable while splitting it for the separate debug info file. */
6224 if ((get_section_flags (section) & SEC_HAS_CONTENTS) == 0)
6225 return {};
6226
6227 dwarf2_read_section (obj, section);
6228
8bebfcda
PA
6229 /* dwarf2_section_info::size is a bfd_size_type, while
6230 gdb::array_view works with size_t. On 32-bit hosts, with
6231 --enable-64-bit-bfd, bfd_size_type is a 64-bit type, while size_t
6232 is 32-bit. So we need an explicit narrowing conversion here.
6233 This is fine, because it's impossible to allocate or mmap an
6234 array/buffer larger than what size_t can represent. */
6235 return gdb::make_array_view (section->buffer, section->size);
4485a1c1
SM
6236}
6237
87d6a7aa
SM
6238/* Lookup the index cache for the contents of the index associated to
6239 DWARF2_OBJ. */
6240
6241static gdb::array_view<const gdb_byte>
6242get_gdb_index_contents_from_cache (objfile *obj, dwarf2_per_objfile *dwarf2_obj)
6243{
6244 const bfd_build_id *build_id = build_id_bfd_get (obj->obfd);
6245 if (build_id == nullptr)
6246 return {};
6247
6248 return global_index_cache.lookup_gdb_index (build_id,
6249 &dwarf2_obj->index_cache_res);
6250}
6251
6252/* Same as the above, but for DWZ. */
6253
6254static gdb::array_view<const gdb_byte>
6255get_gdb_index_contents_from_cache_dwz (objfile *obj, dwz_file *dwz)
6256{
6257 const bfd_build_id *build_id = build_id_bfd_get (dwz->dwz_bfd.get ());
6258 if (build_id == nullptr)
6259 return {};
6260
6261 return global_index_cache.lookup_gdb_index (build_id, &dwz->index_cache_res);
6262}
6263
3c0aa29a 6264/* See symfile.h. */
9291a0cd 6265
3c0aa29a
PA
6266bool
6267dwarf2_initialize_objfile (struct objfile *objfile, dw_index_kind *index_kind)
9291a0cd 6268{
ed2dc618
SM
6269 struct dwarf2_per_objfile *dwarf2_per_objfile
6270 = get_dwarf2_per_objfile (objfile);
6271
9291a0cd
TT
6272 /* If we're about to read full symbols, don't bother with the
6273 indices. In this case we also don't care if some other debug
6274 format is making psymtabs, because they are all about to be
6275 expanded anyway. */
6276 if ((objfile->flags & OBJF_READNOW))
6277 {
9291a0cd 6278 dwarf2_per_objfile->using_index = 1;
ed2dc618
SM
6279 create_all_comp_units (dwarf2_per_objfile);
6280 create_all_type_units (dwarf2_per_objfile);
b76e467d
SM
6281 dwarf2_per_objfile->quick_file_names_table
6282 = create_quick_file_names_table
6283 (dwarf2_per_objfile->all_comp_units.size ());
9291a0cd 6284
b76e467d 6285 for (int i = 0; i < (dwarf2_per_objfile->all_comp_units.size ()
b2bdb8cf 6286 + dwarf2_per_objfile->all_type_units.size ()); ++i)
9291a0cd 6287 {
ff4c9fec 6288 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (i);
9291a0cd 6289
e254ef6a
DE
6290 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6291 struct dwarf2_per_cu_quick_data);
9291a0cd
TT
6292 }
6293
6294 /* Return 1 so that gdb sees the "quick" functions. However,
6295 these functions will be no-ops because we will have expanded
6296 all symtabs. */
3c0aa29a
PA
6297 *index_kind = dw_index_kind::GDB_INDEX;
6298 return true;
9291a0cd
TT
6299 }
6300
ed2dc618 6301 if (dwarf2_read_debug_names (dwarf2_per_objfile))
3c0aa29a
PA
6302 {
6303 *index_kind = dw_index_kind::DEBUG_NAMES;
6304 return true;
6305 }
927aa2e7 6306
4485a1c1
SM
6307 if (dwarf2_read_gdb_index (dwarf2_per_objfile,
6308 get_gdb_index_contents_from_section<struct dwarf2_per_objfile>,
6309 get_gdb_index_contents_from_section<dwz_file>))
3c0aa29a
PA
6310 {
6311 *index_kind = dw_index_kind::GDB_INDEX;
6312 return true;
6313 }
9291a0cd 6314
87d6a7aa
SM
6315 /* ... otherwise, try to find the index in the index cache. */
6316 if (dwarf2_read_gdb_index (dwarf2_per_objfile,
6317 get_gdb_index_contents_from_cache,
6318 get_gdb_index_contents_from_cache_dwz))
6319 {
6320 global_index_cache.hit ();
6321 *index_kind = dw_index_kind::GDB_INDEX;
6322 return true;
6323 }
6324
6325 global_index_cache.miss ();
3c0aa29a 6326 return false;
9291a0cd
TT
6327}
6328
6329\f
6330
dce234bc
PP
6331/* Build a partial symbol table. */
6332
6333void
f29dff0a 6334dwarf2_build_psymtabs (struct objfile *objfile)
dce234bc 6335{
ed2dc618
SM
6336 struct dwarf2_per_objfile *dwarf2_per_objfile
6337 = get_dwarf2_per_objfile (objfile);
c9bf0622 6338
6eee24ce 6339 init_psymbol_list (objfile, 1024);
c906108c 6340
492d29ea 6341 TRY
c9bf0622
TT
6342 {
6343 /* This isn't really ideal: all the data we allocate on the
6344 objfile's obstack is still uselessly kept around. However,
6345 freeing it seems unsafe. */
906768f9 6346 psymtab_discarder psymtabs (objfile);
ed2dc618 6347 dwarf2_build_psymtabs_hard (dwarf2_per_objfile);
906768f9 6348 psymtabs.keep ();
87d6a7aa
SM
6349
6350 /* (maybe) store an index in the cache. */
6351 global_index_cache.store (dwarf2_per_objfile);
c9bf0622 6352 }
492d29ea
PA
6353 CATCH (except, RETURN_MASK_ERROR)
6354 {
6355 exception_print (gdb_stderr, except);
6356 }
6357 END_CATCH
c906108c 6358}
c906108c 6359
1ce1cefd
DE
6360/* Return the total length of the CU described by HEADER. */
6361
6362static unsigned int
6363get_cu_length (const struct comp_unit_head *header)
6364{
6365 return header->initial_length_size + header->length;
6366}
6367
9c541725 6368/* Return TRUE if SECT_OFF is within CU_HEADER. */
45452591 6369
9c541725
PA
6370static inline bool
6371offset_in_cu_p (const comp_unit_head *cu_header, sect_offset sect_off)
45452591 6372{
9c541725
PA
6373 sect_offset bottom = cu_header->sect_off;
6374 sect_offset top = cu_header->sect_off + get_cu_length (cu_header);
9a619af0 6375
9c541725 6376 return sect_off >= bottom && sect_off < top;
45452591
DE
6377}
6378
3b80fe9b
DE
6379/* Find the base address of the compilation unit for range lists and
6380 location lists. It will normally be specified by DW_AT_low_pc.
6381 In DWARF-3 draft 4, the base address could be overridden by
6382 DW_AT_entry_pc. It's been removed, but GCC still uses this for
6383 compilation units with discontinuous ranges. */
6384
6385static void
6386dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
6387{
6388 struct attribute *attr;
6389
6390 cu->base_known = 0;
6391 cu->base_address = 0;
6392
6393 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
6394 if (attr)
6395 {
31aa7e4e 6396 cu->base_address = attr_value_as_address (attr);
3b80fe9b
DE
6397 cu->base_known = 1;
6398 }
6399 else
6400 {
6401 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
6402 if (attr)
6403 {
31aa7e4e 6404 cu->base_address = attr_value_as_address (attr);
3b80fe9b
DE
6405 cu->base_known = 1;
6406 }
6407 }
6408}
6409
93311388 6410/* Read in the comp unit header information from the debug_info at info_ptr.
43988095 6411 Use rcuh_kind::COMPILE as the default type if not known by the caller.
93311388
DE
6412 NOTE: This leaves members offset, first_die_offset to be filled in
6413 by the caller. */
107d2387 6414
d521ce57 6415static const gdb_byte *
107d2387 6416read_comp_unit_head (struct comp_unit_head *cu_header,
43988095
JK
6417 const gdb_byte *info_ptr,
6418 struct dwarf2_section_info *section,
6419 rcuh_kind section_kind)
107d2387
AC
6420{
6421 int signed_addr;
891d2f0b 6422 unsigned int bytes_read;
43988095
JK
6423 const char *filename = get_section_file_name (section);
6424 bfd *abfd = get_section_bfd_owner (section);
c764a876
DE
6425
6426 cu_header->length = read_initial_length (abfd, info_ptr, &bytes_read);
6427 cu_header->initial_length_size = bytes_read;
6428 cu_header->offset_size = (bytes_read == 4) ? 4 : 8;
613e1657 6429 info_ptr += bytes_read;
107d2387 6430 cu_header->version = read_2_bytes (abfd, info_ptr);
1ea5da02
TV
6431 if (cu_header->version < 2 || cu_header->version > 5)
6432 error (_("Dwarf Error: wrong version in compilation unit header "
6433 "(is %d, should be 2, 3, 4 or 5) [in module %s]"),
6434 cu_header->version, filename);
107d2387 6435 info_ptr += 2;
43988095
JK
6436 if (cu_header->version < 5)
6437 switch (section_kind)
6438 {
6439 case rcuh_kind::COMPILE:
6440 cu_header->unit_type = DW_UT_compile;
6441 break;
6442 case rcuh_kind::TYPE:
6443 cu_header->unit_type = DW_UT_type;
6444 break;
6445 default:
6446 internal_error (__FILE__, __LINE__,
6447 _("read_comp_unit_head: invalid section_kind"));
6448 }
6449 else
6450 {
6451 cu_header->unit_type = static_cast<enum dwarf_unit_type>
6452 (read_1_byte (abfd, info_ptr));
6453 info_ptr += 1;
6454 switch (cu_header->unit_type)
6455 {
6456 case DW_UT_compile:
6457 if (section_kind != rcuh_kind::COMPILE)
6458 error (_("Dwarf Error: wrong unit_type in compilation unit header "
6459 "(is DW_UT_compile, should be DW_UT_type) [in module %s]"),
6460 filename);
6461 break;
6462 case DW_UT_type:
6463 section_kind = rcuh_kind::TYPE;
6464 break;
6465 default:
6466 error (_("Dwarf Error: wrong unit_type in compilation unit header "
6467 "(is %d, should be %d or %d) [in module %s]"),
6468 cu_header->unit_type, DW_UT_compile, DW_UT_type, filename);
6469 }
6470
6471 cu_header->addr_size = read_1_byte (abfd, info_ptr);
6472 info_ptr += 1;
6473 }
9c541725
PA
6474 cu_header->abbrev_sect_off = (sect_offset) read_offset (abfd, info_ptr,
6475 cu_header,
6476 &bytes_read);
613e1657 6477 info_ptr += bytes_read;
43988095
JK
6478 if (cu_header->version < 5)
6479 {
6480 cu_header->addr_size = read_1_byte (abfd, info_ptr);
6481 info_ptr += 1;
6482 }
107d2387
AC
6483 signed_addr = bfd_get_sign_extend_vma (abfd);
6484 if (signed_addr < 0)
8e65ff28 6485 internal_error (__FILE__, __LINE__,
e2e0b3e5 6486 _("read_comp_unit_head: dwarf from non elf file"));
107d2387 6487 cu_header->signed_addr_p = signed_addr;
c764a876 6488
43988095
JK
6489 if (section_kind == rcuh_kind::TYPE)
6490 {
6491 LONGEST type_offset;
6492
6493 cu_header->signature = read_8_bytes (abfd, info_ptr);
6494 info_ptr += 8;
6495
6496 type_offset = read_offset (abfd, info_ptr, cu_header, &bytes_read);
6497 info_ptr += bytes_read;
9c541725
PA
6498 cu_header->type_cu_offset_in_tu = (cu_offset) type_offset;
6499 if (to_underlying (cu_header->type_cu_offset_in_tu) != type_offset)
43988095
JK
6500 error (_("Dwarf Error: Too big type_offset in compilation unit "
6501 "header (is %s) [in module %s]"), plongest (type_offset),
6502 filename);
6503 }
6504
107d2387
AC
6505 return info_ptr;
6506}
6507
36586728
TT
6508/* Helper function that returns the proper abbrev section for
6509 THIS_CU. */
6510
6511static struct dwarf2_section_info *
6512get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
6513{
6514 struct dwarf2_section_info *abbrev;
ed2dc618 6515 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
36586728
TT
6516
6517 if (this_cu->is_dwz)
ed2dc618 6518 abbrev = &dwarf2_get_dwz_file (dwarf2_per_objfile)->abbrev;
36586728
TT
6519 else
6520 abbrev = &dwarf2_per_objfile->abbrev;
6521
6522 return abbrev;
6523}
6524
9ff913ba
DE
6525/* Subroutine of read_and_check_comp_unit_head and
6526 read_and_check_type_unit_head to simplify them.
6527 Perform various error checking on the header. */
6528
6529static void
ed2dc618
SM
6530error_check_comp_unit_head (struct dwarf2_per_objfile *dwarf2_per_objfile,
6531 struct comp_unit_head *header,
4bdcc0c1
DE
6532 struct dwarf2_section_info *section,
6533 struct dwarf2_section_info *abbrev_section)
9ff913ba 6534{
a32a8923 6535 const char *filename = get_section_file_name (section);
9ff913ba 6536
9c541725 6537 if (to_underlying (header->abbrev_sect_off)
36586728 6538 >= dwarf2_section_size (dwarf2_per_objfile->objfile, abbrev_section))
9d8780f0
SM
6539 error (_("Dwarf Error: bad offset (%s) in compilation unit header "
6540 "(offset %s + 6) [in module %s]"),
6541 sect_offset_str (header->abbrev_sect_off),
6542 sect_offset_str (header->sect_off),
9ff913ba
DE
6543 filename);
6544
9c541725 6545 /* Cast to ULONGEST to use 64-bit arithmetic when possible to
9ff913ba 6546 avoid potential 32-bit overflow. */
9c541725 6547 if (((ULONGEST) header->sect_off + get_cu_length (header))
9ff913ba 6548 > section->size)
9c541725 6549 error (_("Dwarf Error: bad length (0x%x) in compilation unit header "
9d8780f0
SM
6550 "(offset %s + 0) [in module %s]"),
6551 header->length, sect_offset_str (header->sect_off),
9ff913ba
DE
6552 filename);
6553}
6554
6555/* Read in a CU/TU header and perform some basic error checking.
6556 The contents of the header are stored in HEADER.
6557 The result is a pointer to the start of the first DIE. */
adabb602 6558
d521ce57 6559static const gdb_byte *
ed2dc618
SM
6560read_and_check_comp_unit_head (struct dwarf2_per_objfile *dwarf2_per_objfile,
6561 struct comp_unit_head *header,
9ff913ba 6562 struct dwarf2_section_info *section,
4bdcc0c1 6563 struct dwarf2_section_info *abbrev_section,
d521ce57 6564 const gdb_byte *info_ptr,
43988095 6565 rcuh_kind section_kind)
72bf9492 6566{
d521ce57 6567 const gdb_byte *beg_of_comp_unit = info_ptr;
72bf9492 6568
9c541725 6569 header->sect_off = (sect_offset) (beg_of_comp_unit - section->buffer);
adabb602 6570
43988095 6571 info_ptr = read_comp_unit_head (header, info_ptr, section, section_kind);
9ff913ba 6572
9c541725 6573 header->first_die_cu_offset = (cu_offset) (info_ptr - beg_of_comp_unit);
348e048f 6574
ed2dc618
SM
6575 error_check_comp_unit_head (dwarf2_per_objfile, header, section,
6576 abbrev_section);
9ff913ba
DE
6577
6578 return info_ptr;
348e048f
DE
6579}
6580
f4dc4d17
DE
6581/* Fetch the abbreviation table offset from a comp or type unit header. */
6582
6583static sect_offset
ed2dc618
SM
6584read_abbrev_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
6585 struct dwarf2_section_info *section,
9c541725 6586 sect_offset sect_off)
f4dc4d17 6587{
a32a8923 6588 bfd *abfd = get_section_bfd_owner (section);
d521ce57 6589 const gdb_byte *info_ptr;
ac298888 6590 unsigned int initial_length_size, offset_size;
43988095 6591 uint16_t version;
f4dc4d17
DE
6592
6593 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
9c541725 6594 info_ptr = section->buffer + to_underlying (sect_off);
ac298888 6595 read_initial_length (abfd, info_ptr, &initial_length_size);
f4dc4d17 6596 offset_size = initial_length_size == 4 ? 4 : 8;
43988095
JK
6597 info_ptr += initial_length_size;
6598
6599 version = read_2_bytes (abfd, info_ptr);
6600 info_ptr += 2;
6601 if (version >= 5)
6602 {
6603 /* Skip unit type and address size. */
6604 info_ptr += 2;
6605 }
6606
9c541725 6607 return (sect_offset) read_offset_1 (abfd, info_ptr, offset_size);
f4dc4d17
DE
6608}
6609
aaa75496
JB
6610/* Allocate a new partial symtab for file named NAME and mark this new
6611 partial symtab as being an include of PST. */
6612
6613static void
d521ce57 6614dwarf2_create_include_psymtab (const char *name, struct partial_symtab *pst,
aaa75496
JB
6615 struct objfile *objfile)
6616{
6617 struct partial_symtab *subpst = allocate_psymtab (name, objfile);
6618
fbd9ab74
JK
6619 if (!IS_ABSOLUTE_PATH (subpst->filename))
6620 {
6621 /* It shares objfile->objfile_obstack. */
6622 subpst->dirname = pst->dirname;
6623 }
6624
a9342b62 6625 subpst->dependencies = objfile->partial_symtabs->allocate_dependencies (1);
aaa75496
JB
6626 subpst->dependencies[0] = pst;
6627 subpst->number_of_dependencies = 1;
6628
aaa75496 6629 subpst->read_symtab = pst->read_symtab;
aaa75496
JB
6630
6631 /* No private part is necessary for include psymtabs. This property
6632 can be used to differentiate between such include psymtabs and
10b3939b 6633 the regular ones. */
58a9656e 6634 subpst->read_symtab_private = NULL;
aaa75496
JB
6635}
6636
6637/* Read the Line Number Program data and extract the list of files
6638 included by the source file represented by PST. Build an include
d85a05f0 6639 partial symtab for each of these included files. */
aaa75496
JB
6640
6641static void
6642dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
dee91e82
DE
6643 struct die_info *die,
6644 struct partial_symtab *pst)
aaa75496 6645{
fff8551c 6646 line_header_up lh;
d85a05f0 6647 struct attribute *attr;
aaa75496 6648
d85a05f0
DJ
6649 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
6650 if (attr)
9c541725 6651 lh = dwarf_decode_line_header ((sect_offset) DW_UNSND (attr), cu);
aaa75496
JB
6652 if (lh == NULL)
6653 return; /* No linetable, so no includes. */
6654
79748972
TT
6655 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). Also note
6656 that we pass in the raw text_low here; that is ok because we're
6657 only decoding the line table to make include partial symtabs, and
6658 so the addresses aren't really used. */
4ae976d1 6659 dwarf_decode_lines (lh.get (), pst->dirname, cu, pst,
79748972 6660 pst->raw_text_low (), 1);
aaa75496
JB
6661}
6662
348e048f 6663static hashval_t
52dc124a 6664hash_signatured_type (const void *item)
348e048f 6665{
9a3c8263
SM
6666 const struct signatured_type *sig_type
6667 = (const struct signatured_type *) item;
9a619af0 6668
348e048f 6669 /* This drops the top 32 bits of the signature, but is ok for a hash. */
52dc124a 6670 return sig_type->signature;
348e048f
DE
6671}
6672
6673static int
52dc124a 6674eq_signatured_type (const void *item_lhs, const void *item_rhs)
348e048f 6675{
9a3c8263
SM
6676 const struct signatured_type *lhs = (const struct signatured_type *) item_lhs;
6677 const struct signatured_type *rhs = (const struct signatured_type *) item_rhs;
9a619af0 6678
348e048f
DE
6679 return lhs->signature == rhs->signature;
6680}
6681
1fd400ff
TT
6682/* Allocate a hash table for signatured types. */
6683
6684static htab_t
673bfd45 6685allocate_signatured_type_table (struct objfile *objfile)
1fd400ff
TT
6686{
6687 return htab_create_alloc_ex (41,
52dc124a
DE
6688 hash_signatured_type,
6689 eq_signatured_type,
1fd400ff
TT
6690 NULL,
6691 &objfile->objfile_obstack,
6692 hashtab_obstack_allocate,
6693 dummy_obstack_deallocate);
6694}
6695
d467dd73 6696/* A helper function to add a signatured type CU to a table. */
1fd400ff
TT
6697
6698static int
d467dd73 6699add_signatured_type_cu_to_table (void **slot, void *datum)
1fd400ff 6700{
9a3c8263 6701 struct signatured_type *sigt = (struct signatured_type *) *slot;
b2bdb8cf
SM
6702 std::vector<signatured_type *> *all_type_units
6703 = (std::vector<signatured_type *> *) datum;
1fd400ff 6704
b2bdb8cf 6705 all_type_units->push_back (sigt);
1fd400ff
TT
6706
6707 return 1;
6708}
6709
78d4d2c5 6710/* A helper for create_debug_types_hash_table. Read types from SECTION
43988095
JK
6711 and fill them into TYPES_HTAB. It will process only type units,
6712 therefore DW_UT_type. */
c88ee1f0 6713
78d4d2c5 6714static void
ed2dc618
SM
6715create_debug_type_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
6716 struct dwo_file *dwo_file,
43988095
JK
6717 dwarf2_section_info *section, htab_t &types_htab,
6718 rcuh_kind section_kind)
348e048f 6719{
3019eac3 6720 struct objfile *objfile = dwarf2_per_objfile->objfile;
4bdcc0c1 6721 struct dwarf2_section_info *abbrev_section;
78d4d2c5
JK
6722 bfd *abfd;
6723 const gdb_byte *info_ptr, *end_ptr;
348e048f 6724
4bdcc0c1
DE
6725 abbrev_section = (dwo_file != NULL
6726 ? &dwo_file->sections.abbrev
6727 : &dwarf2_per_objfile->abbrev);
6728
b4f54984 6729 if (dwarf_read_debug)
43988095
JK
6730 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
6731 get_section_name (section),
a32a8923 6732 get_section_file_name (abbrev_section));
09406207 6733
78d4d2c5
JK
6734 dwarf2_read_section (objfile, section);
6735 info_ptr = section->buffer;
348e048f 6736
78d4d2c5
JK
6737 if (info_ptr == NULL)
6738 return;
348e048f 6739
78d4d2c5
JK
6740 /* We can't set abfd until now because the section may be empty or
6741 not present, in which case the bfd is unknown. */
6742 abfd = get_section_bfd_owner (section);
348e048f 6743
78d4d2c5
JK
6744 /* We don't use init_cutu_and_read_dies_simple, or some such, here
6745 because we don't need to read any dies: the signature is in the
6746 header. */
3019eac3 6747
78d4d2c5
JK
6748 end_ptr = info_ptr + section->size;
6749 while (info_ptr < end_ptr)
6750 {
78d4d2c5
JK
6751 struct signatured_type *sig_type;
6752 struct dwo_unit *dwo_tu;
6753 void **slot;
6754 const gdb_byte *ptr = info_ptr;
6755 struct comp_unit_head header;
6756 unsigned int length;
8b70b953 6757
9c541725 6758 sect_offset sect_off = (sect_offset) (ptr - section->buffer);
348e048f 6759
a49dd8dd
JK
6760 /* Initialize it due to a false compiler warning. */
6761 header.signature = -1;
9c541725 6762 header.type_cu_offset_in_tu = (cu_offset) -1;
a49dd8dd 6763
78d4d2c5
JK
6764 /* We need to read the type's signature in order to build the hash
6765 table, but we don't need anything else just yet. */
348e048f 6766
ed2dc618 6767 ptr = read_and_check_comp_unit_head (dwarf2_per_objfile, &header, section,
43988095 6768 abbrev_section, ptr, section_kind);
348e048f 6769
78d4d2c5 6770 length = get_cu_length (&header);
6caca83c 6771
78d4d2c5
JK
6772 /* Skip dummy type units. */
6773 if (ptr >= info_ptr + length
43988095
JK
6774 || peek_abbrev_code (abfd, ptr) == 0
6775 || header.unit_type != DW_UT_type)
78d4d2c5
JK
6776 {
6777 info_ptr += length;
6778 continue;
6779 }
dee91e82 6780
78d4d2c5
JK
6781 if (types_htab == NULL)
6782 {
6783 if (dwo_file)
6784 types_htab = allocate_dwo_unit_table (objfile);
6785 else
6786 types_htab = allocate_signatured_type_table (objfile);
6787 }
8b70b953 6788
78d4d2c5
JK
6789 if (dwo_file)
6790 {
6791 sig_type = NULL;
6792 dwo_tu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6793 struct dwo_unit);
6794 dwo_tu->dwo_file = dwo_file;
43988095 6795 dwo_tu->signature = header.signature;
9c541725 6796 dwo_tu->type_offset_in_tu = header.type_cu_offset_in_tu;
78d4d2c5 6797 dwo_tu->section = section;
9c541725 6798 dwo_tu->sect_off = sect_off;
78d4d2c5
JK
6799 dwo_tu->length = length;
6800 }
6801 else
6802 {
6803 /* N.B.: type_offset is not usable if this type uses a DWO file.
6804 The real type_offset is in the DWO file. */
6805 dwo_tu = NULL;
6806 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6807 struct signatured_type);
43988095 6808 sig_type->signature = header.signature;
9c541725 6809 sig_type->type_offset_in_tu = header.type_cu_offset_in_tu;
e3b94546 6810 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
78d4d2c5
JK
6811 sig_type->per_cu.is_debug_types = 1;
6812 sig_type->per_cu.section = section;
9c541725 6813 sig_type->per_cu.sect_off = sect_off;
78d4d2c5
JK
6814 sig_type->per_cu.length = length;
6815 }
6816
6817 slot = htab_find_slot (types_htab,
6818 dwo_file ? (void*) dwo_tu : (void *) sig_type,
6819 INSERT);
6820 gdb_assert (slot != NULL);
6821 if (*slot != NULL)
6822 {
9c541725 6823 sect_offset dup_sect_off;
0349ea22 6824
3019eac3
DE
6825 if (dwo_file)
6826 {
78d4d2c5
JK
6827 const struct dwo_unit *dup_tu
6828 = (const struct dwo_unit *) *slot;
6829
9c541725 6830 dup_sect_off = dup_tu->sect_off;
3019eac3
DE
6831 }
6832 else
6833 {
78d4d2c5
JK
6834 const struct signatured_type *dup_tu
6835 = (const struct signatured_type *) *slot;
6836
9c541725 6837 dup_sect_off = dup_tu->per_cu.sect_off;
3019eac3 6838 }
8b70b953 6839
b98664d3 6840 complaint (_("debug type entry at offset %s is duplicate to"
9d8780f0
SM
6841 " the entry at offset %s, signature %s"),
6842 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
43988095 6843 hex_string (header.signature));
78d4d2c5
JK
6844 }
6845 *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
3019eac3 6846
78d4d2c5 6847 if (dwarf_read_debug > 1)
9d8780f0
SM
6848 fprintf_unfiltered (gdb_stdlog, " offset %s, signature %s\n",
6849 sect_offset_str (sect_off),
43988095 6850 hex_string (header.signature));
3019eac3 6851
78d4d2c5
JK
6852 info_ptr += length;
6853 }
6854}
3019eac3 6855
78d4d2c5
JK
6856/* Create the hash table of all entries in the .debug_types
6857 (or .debug_types.dwo) section(s).
6858 If reading a DWO file, then DWO_FILE is a pointer to the DWO file object,
6859 otherwise it is NULL.
b3c8eb43 6860
78d4d2c5 6861 The result is a pointer to the hash table or NULL if there are no types.
348e048f 6862
78d4d2c5 6863 Note: This function processes DWO files only, not DWP files. */
348e048f 6864
78d4d2c5 6865static void
ed2dc618
SM
6866create_debug_types_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
6867 struct dwo_file *dwo_file,
78d4d2c5
JK
6868 VEC (dwarf2_section_info_def) *types,
6869 htab_t &types_htab)
6870{
6871 int ix;
6872 struct dwarf2_section_info *section;
6873
6874 if (VEC_empty (dwarf2_section_info_def, types))
6875 return;
348e048f 6876
78d4d2c5
JK
6877 for (ix = 0;
6878 VEC_iterate (dwarf2_section_info_def, types, ix, section);
6879 ++ix)
ed2dc618
SM
6880 create_debug_type_hash_table (dwarf2_per_objfile, dwo_file, section,
6881 types_htab, rcuh_kind::TYPE);
3019eac3
DE
6882}
6883
6884/* Create the hash table of all entries in the .debug_types section,
6885 and initialize all_type_units.
6886 The result is zero if there is an error (e.g. missing .debug_types section),
6887 otherwise non-zero. */
6888
6889static int
ed2dc618 6890create_all_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
3019eac3 6891{
78d4d2c5 6892 htab_t types_htab = NULL;
3019eac3 6893
ed2dc618
SM
6894 create_debug_type_hash_table (dwarf2_per_objfile, NULL,
6895 &dwarf2_per_objfile->info, types_htab,
43988095 6896 rcuh_kind::COMPILE);
ed2dc618
SM
6897 create_debug_types_hash_table (dwarf2_per_objfile, NULL,
6898 dwarf2_per_objfile->types, types_htab);
3019eac3
DE
6899 if (types_htab == NULL)
6900 {
6901 dwarf2_per_objfile->signatured_types = NULL;
6902 return 0;
6903 }
6904
348e048f
DE
6905 dwarf2_per_objfile->signatured_types = types_htab;
6906
b2bdb8cf
SM
6907 gdb_assert (dwarf2_per_objfile->all_type_units.empty ());
6908 dwarf2_per_objfile->all_type_units.reserve (htab_elements (types_htab));
6909
6910 htab_traverse_noresize (types_htab, add_signatured_type_cu_to_table,
6911 &dwarf2_per_objfile->all_type_units);
1fd400ff 6912
348e048f
DE
6913 return 1;
6914}
6915
6aa5f3a6
DE
6916/* Add an entry for signature SIG to dwarf2_per_objfile->signatured_types.
6917 If SLOT is non-NULL, it is the entry to use in the hash table.
6918 Otherwise we find one. */
6919
6920static struct signatured_type *
ed2dc618
SM
6921add_type_unit (struct dwarf2_per_objfile *dwarf2_per_objfile, ULONGEST sig,
6922 void **slot)
6aa5f3a6
DE
6923{
6924 struct objfile *objfile = dwarf2_per_objfile->objfile;
6aa5f3a6 6925
b2bdb8cf
SM
6926 if (dwarf2_per_objfile->all_type_units.size ()
6927 == dwarf2_per_objfile->all_type_units.capacity ())
6928 ++dwarf2_per_objfile->tu_stats.nr_all_type_units_reallocs;
6aa5f3a6 6929
b2bdb8cf
SM
6930 signatured_type *sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6931 struct signatured_type);
6932
6933 dwarf2_per_objfile->all_type_units.push_back (sig_type);
6aa5f3a6
DE
6934 sig_type->signature = sig;
6935 sig_type->per_cu.is_debug_types = 1;
6936 if (dwarf2_per_objfile->using_index)
6937 {
6938 sig_type->per_cu.v.quick =
6939 OBSTACK_ZALLOC (&objfile->objfile_obstack,
6940 struct dwarf2_per_cu_quick_data);
6941 }
6942
6943 if (slot == NULL)
6944 {
6945 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
6946 sig_type, INSERT);
6947 }
6948 gdb_assert (*slot == NULL);
6949 *slot = sig_type;
6950 /* The rest of sig_type must be filled in by the caller. */
6951 return sig_type;
6952}
6953
a2ce51a0
DE
6954/* Subroutine of lookup_dwo_signatured_type and lookup_dwp_signatured_type.
6955 Fill in SIG_ENTRY with DWO_ENTRY. */
6956
6957static void
ed2dc618 6958fill_in_sig_entry_from_dwo_entry (struct dwarf2_per_objfile *dwarf2_per_objfile,
a2ce51a0
DE
6959 struct signatured_type *sig_entry,
6960 struct dwo_unit *dwo_entry)
6961{
7ee85ab1 6962 /* Make sure we're not clobbering something we don't expect to. */
a2ce51a0
DE
6963 gdb_assert (! sig_entry->per_cu.queued);
6964 gdb_assert (sig_entry->per_cu.cu == NULL);
6aa5f3a6
DE
6965 if (dwarf2_per_objfile->using_index)
6966 {
6967 gdb_assert (sig_entry->per_cu.v.quick != NULL);
43f3e411 6968 gdb_assert (sig_entry->per_cu.v.quick->compunit_symtab == NULL);
6aa5f3a6
DE
6969 }
6970 else
6971 gdb_assert (sig_entry->per_cu.v.psymtab == NULL);
a2ce51a0 6972 gdb_assert (sig_entry->signature == dwo_entry->signature);
9c541725 6973 gdb_assert (to_underlying (sig_entry->type_offset_in_section) == 0);
a2ce51a0 6974 gdb_assert (sig_entry->type_unit_group == NULL);
7ee85ab1
DE
6975 gdb_assert (sig_entry->dwo_unit == NULL);
6976
6977 sig_entry->per_cu.section = dwo_entry->section;
9c541725 6978 sig_entry->per_cu.sect_off = dwo_entry->sect_off;
7ee85ab1
DE
6979 sig_entry->per_cu.length = dwo_entry->length;
6980 sig_entry->per_cu.reading_dwo_directly = 1;
e3b94546 6981 sig_entry->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
a2ce51a0
DE
6982 sig_entry->type_offset_in_tu = dwo_entry->type_offset_in_tu;
6983 sig_entry->dwo_unit = dwo_entry;
6984}
6985
6986/* Subroutine of lookup_signatured_type.
7ee85ab1
DE
6987 If we haven't read the TU yet, create the signatured_type data structure
6988 for a TU to be read in directly from a DWO file, bypassing the stub.
6989 This is the "Stay in DWO Optimization": When there is no DWP file and we're
6990 using .gdb_index, then when reading a CU we want to stay in the DWO file
6991 containing that CU. Otherwise we could end up reading several other DWO
6992 files (due to comdat folding) to process the transitive closure of all the
6993 mentioned TUs, and that can be slow. The current DWO file will have every
6994 type signature that it needs.
a2ce51a0
DE
6995 We only do this for .gdb_index because in the psymtab case we already have
6996 to read all the DWOs to build the type unit groups. */
6997
6998static struct signatured_type *
6999lookup_dwo_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
7000{
518817b3
SM
7001 struct dwarf2_per_objfile *dwarf2_per_objfile
7002 = cu->per_cu->dwarf2_per_objfile;
a2ce51a0
DE
7003 struct objfile *objfile = dwarf2_per_objfile->objfile;
7004 struct dwo_file *dwo_file;
7005 struct dwo_unit find_dwo_entry, *dwo_entry;
7006 struct signatured_type find_sig_entry, *sig_entry;
6aa5f3a6 7007 void **slot;
a2ce51a0
DE
7008
7009 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
7010
6aa5f3a6
DE
7011 /* If TU skeletons have been removed then we may not have read in any
7012 TUs yet. */
7013 if (dwarf2_per_objfile->signatured_types == NULL)
7014 {
7015 dwarf2_per_objfile->signatured_types
7016 = allocate_signatured_type_table (objfile);
7017 }
a2ce51a0
DE
7018
7019 /* We only ever need to read in one copy of a signatured type.
6aa5f3a6
DE
7020 Use the global signatured_types array to do our own comdat-folding
7021 of types. If this is the first time we're reading this TU, and
7022 the TU has an entry in .gdb_index, replace the recorded data from
7023 .gdb_index with this TU. */
a2ce51a0 7024
a2ce51a0 7025 find_sig_entry.signature = sig;
6aa5f3a6
DE
7026 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
7027 &find_sig_entry, INSERT);
9a3c8263 7028 sig_entry = (struct signatured_type *) *slot;
7ee85ab1
DE
7029
7030 /* We can get here with the TU already read, *or* in the process of being
6aa5f3a6
DE
7031 read. Don't reassign the global entry to point to this DWO if that's
7032 the case. Also note that if the TU is already being read, it may not
7033 have come from a DWO, the program may be a mix of Fission-compiled
7034 code and non-Fission-compiled code. */
7035
7036 /* Have we already tried to read this TU?
7037 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
7038 needn't exist in the global table yet). */
7039 if (sig_entry != NULL && sig_entry->per_cu.tu_read)
a2ce51a0
DE
7040 return sig_entry;
7041
6aa5f3a6
DE
7042 /* Note: cu->dwo_unit is the dwo_unit that references this TU, not the
7043 dwo_unit of the TU itself. */
7044 dwo_file = cu->dwo_unit->dwo_file;
7045
a2ce51a0
DE
7046 /* Ok, this is the first time we're reading this TU. */
7047 if (dwo_file->tus == NULL)
7048 return NULL;
7049 find_dwo_entry.signature = sig;
9a3c8263 7050 dwo_entry = (struct dwo_unit *) htab_find (dwo_file->tus, &find_dwo_entry);
a2ce51a0
DE
7051 if (dwo_entry == NULL)
7052 return NULL;
7053
6aa5f3a6
DE
7054 /* If the global table doesn't have an entry for this TU, add one. */
7055 if (sig_entry == NULL)
ed2dc618 7056 sig_entry = add_type_unit (dwarf2_per_objfile, sig, slot);
6aa5f3a6 7057
ed2dc618 7058 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, sig_entry, dwo_entry);
89e63ee4 7059 sig_entry->per_cu.tu_read = 1;
a2ce51a0
DE
7060 return sig_entry;
7061}
7062
a2ce51a0
DE
7063/* Subroutine of lookup_signatured_type.
7064 Look up the type for signature SIG, and if we can't find SIG in .gdb_index
6aa5f3a6
DE
7065 then try the DWP file. If the TU stub (skeleton) has been removed then
7066 it won't be in .gdb_index. */
a2ce51a0
DE
7067
7068static struct signatured_type *
7069lookup_dwp_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
7070{
518817b3
SM
7071 struct dwarf2_per_objfile *dwarf2_per_objfile
7072 = cu->per_cu->dwarf2_per_objfile;
a2ce51a0 7073 struct objfile *objfile = dwarf2_per_objfile->objfile;
ed2dc618 7074 struct dwp_file *dwp_file = get_dwp_file (dwarf2_per_objfile);
a2ce51a0
DE
7075 struct dwo_unit *dwo_entry;
7076 struct signatured_type find_sig_entry, *sig_entry;
6aa5f3a6 7077 void **slot;
a2ce51a0
DE
7078
7079 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
7080 gdb_assert (dwp_file != NULL);
7081
6aa5f3a6
DE
7082 /* If TU skeletons have been removed then we may not have read in any
7083 TUs yet. */
7084 if (dwarf2_per_objfile->signatured_types == NULL)
a2ce51a0 7085 {
6aa5f3a6
DE
7086 dwarf2_per_objfile->signatured_types
7087 = allocate_signatured_type_table (objfile);
a2ce51a0
DE
7088 }
7089
6aa5f3a6
DE
7090 find_sig_entry.signature = sig;
7091 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
7092 &find_sig_entry, INSERT);
9a3c8263 7093 sig_entry = (struct signatured_type *) *slot;
6aa5f3a6
DE
7094
7095 /* Have we already tried to read this TU?
7096 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
7097 needn't exist in the global table yet). */
7098 if (sig_entry != NULL)
7099 return sig_entry;
7100
a2ce51a0
DE
7101 if (dwp_file->tus == NULL)
7102 return NULL;
ed2dc618 7103 dwo_entry = lookup_dwo_unit_in_dwp (dwarf2_per_objfile, dwp_file, NULL,
57d63ce2 7104 sig, 1 /* is_debug_types */);
a2ce51a0
DE
7105 if (dwo_entry == NULL)
7106 return NULL;
7107
ed2dc618
SM
7108 sig_entry = add_type_unit (dwarf2_per_objfile, sig, slot);
7109 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, sig_entry, dwo_entry);
a2ce51a0 7110
a2ce51a0
DE
7111 return sig_entry;
7112}
7113
380bca97 7114/* Lookup a signature based type for DW_FORM_ref_sig8.
5a8b3f62
DE
7115 Returns NULL if signature SIG is not present in the table.
7116 It is up to the caller to complain about this. */
348e048f
DE
7117
7118static struct signatured_type *
a2ce51a0 7119lookup_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
348e048f 7120{
518817b3
SM
7121 struct dwarf2_per_objfile *dwarf2_per_objfile
7122 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 7123
a2ce51a0
DE
7124 if (cu->dwo_unit
7125 && dwarf2_per_objfile->using_index)
7126 {
7127 /* We're in a DWO/DWP file, and we're using .gdb_index.
7128 These cases require special processing. */
ed2dc618 7129 if (get_dwp_file (dwarf2_per_objfile) == NULL)
a2ce51a0
DE
7130 return lookup_dwo_signatured_type (cu, sig);
7131 else
7132 return lookup_dwp_signatured_type (cu, sig);
7133 }
7134 else
7135 {
7136 struct signatured_type find_entry, *entry;
348e048f 7137
a2ce51a0
DE
7138 if (dwarf2_per_objfile->signatured_types == NULL)
7139 return NULL;
7140 find_entry.signature = sig;
9a3c8263
SM
7141 entry = ((struct signatured_type *)
7142 htab_find (dwarf2_per_objfile->signatured_types, &find_entry));
a2ce51a0
DE
7143 return entry;
7144 }
348e048f 7145}
42e7ad6c
DE
7146\f
7147/* Low level DIE reading support. */
348e048f 7148
d85a05f0
DJ
7149/* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
7150
7151static void
7152init_cu_die_reader (struct die_reader_specs *reader,
dee91e82 7153 struct dwarf2_cu *cu,
3019eac3 7154 struct dwarf2_section_info *section,
685af9cd
TT
7155 struct dwo_file *dwo_file,
7156 struct abbrev_table *abbrev_table)
d85a05f0 7157{
fceca515 7158 gdb_assert (section->readin && section->buffer != NULL);
a32a8923 7159 reader->abfd = get_section_bfd_owner (section);
d85a05f0 7160 reader->cu = cu;
3019eac3 7161 reader->dwo_file = dwo_file;
dee91e82
DE
7162 reader->die_section = section;
7163 reader->buffer = section->buffer;
f664829e 7164 reader->buffer_end = section->buffer + section->size;
a2ce51a0 7165 reader->comp_dir = NULL;
685af9cd 7166 reader->abbrev_table = abbrev_table;
d85a05f0
DJ
7167}
7168
b0c7bfa9
DE
7169/* Subroutine of init_cutu_and_read_dies to simplify it.
7170 Read in the rest of a CU/TU top level DIE from DWO_UNIT.
7171 There's just a lot of work to do, and init_cutu_and_read_dies is big enough
7172 already.
7173
7174 STUB_COMP_UNIT_DIE is for the stub DIE, we copy over certain attributes
7175 from it to the DIE in the DWO. If NULL we are skipping the stub.
a2ce51a0
DE
7176 STUB_COMP_DIR is similar to STUB_COMP_UNIT_DIE: When reading a TU directly
7177 from the DWO file, bypassing the stub, it contains the DW_AT_comp_dir
c54a1dd8
DE
7178 attribute of the referencing CU. At most one of STUB_COMP_UNIT_DIE and
7179 STUB_COMP_DIR may be non-NULL.
b0c7bfa9
DE
7180 *RESULT_READER,*RESULT_INFO_PTR,*RESULT_COMP_UNIT_DIE,*RESULT_HAS_CHILDREN
7181 are filled in with the info of the DIE from the DWO file.
685af9cd
TT
7182 *RESULT_DWO_ABBREV_TABLE will be filled in with the abbrev table allocated
7183 from the dwo. Since *RESULT_READER references this abbrev table, it must be
7184 kept around for at least as long as *RESULT_READER.
7185
b0c7bfa9
DE
7186 The result is non-zero if a valid (non-dummy) DIE was found. */
7187
7188static int
7189read_cutu_die_from_dwo (struct dwarf2_per_cu_data *this_cu,
7190 struct dwo_unit *dwo_unit,
b0c7bfa9 7191 struct die_info *stub_comp_unit_die,
a2ce51a0 7192 const char *stub_comp_dir,
b0c7bfa9 7193 struct die_reader_specs *result_reader,
d521ce57 7194 const gdb_byte **result_info_ptr,
b0c7bfa9 7195 struct die_info **result_comp_unit_die,
685af9cd
TT
7196 int *result_has_children,
7197 abbrev_table_up *result_dwo_abbrev_table)
b0c7bfa9 7198{
ed2dc618 7199 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
b0c7bfa9
DE
7200 struct objfile *objfile = dwarf2_per_objfile->objfile;
7201 struct dwarf2_cu *cu = this_cu->cu;
b0c7bfa9 7202 bfd *abfd;
d521ce57 7203 const gdb_byte *begin_info_ptr, *info_ptr;
b0c7bfa9
DE
7204 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
7205 int i,num_extra_attrs;
7206 struct dwarf2_section_info *dwo_abbrev_section;
7207 struct attribute *attr;
7208 struct die_info *comp_unit_die;
7209
b0aeadb3
DE
7210 /* At most one of these may be provided. */
7211 gdb_assert ((stub_comp_unit_die != NULL) + (stub_comp_dir != NULL) <= 1);
a2ce51a0 7212
b0c7bfa9
DE
7213 /* These attributes aren't processed until later:
7214 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
0d60c288
DE
7215 DW_AT_comp_dir is used now, to find the DWO file, but it is also
7216 referenced later. However, these attributes are found in the stub
7217 which we won't have later. In order to not impose this complication
7218 on the rest of the code, we read them here and copy them to the
7219 DWO CU/TU die. */
b0c7bfa9
DE
7220
7221 stmt_list = NULL;
7222 low_pc = NULL;
7223 high_pc = NULL;
7224 ranges = NULL;
7225 comp_dir = NULL;
7226
7227 if (stub_comp_unit_die != NULL)
7228 {
7229 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
7230 DWO file. */
7231 if (! this_cu->is_debug_types)
7232 stmt_list = dwarf2_attr (stub_comp_unit_die, DW_AT_stmt_list, cu);
7233 low_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_low_pc, cu);
7234 high_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_high_pc, cu);
7235 ranges = dwarf2_attr (stub_comp_unit_die, DW_AT_ranges, cu);
7236 comp_dir = dwarf2_attr (stub_comp_unit_die, DW_AT_comp_dir, cu);
7237
7238 /* There should be a DW_AT_addr_base attribute here (if needed).
7239 We need the value before we can process DW_FORM_GNU_addr_index. */
7240 cu->addr_base = 0;
7241 attr = dwarf2_attr (stub_comp_unit_die, DW_AT_GNU_addr_base, cu);
7242 if (attr)
7243 cu->addr_base = DW_UNSND (attr);
7244
7245 /* There should be a DW_AT_ranges_base attribute here (if needed).
7246 We need the value before we can process DW_AT_ranges. */
7247 cu->ranges_base = 0;
7248 attr = dwarf2_attr (stub_comp_unit_die, DW_AT_GNU_ranges_base, cu);
7249 if (attr)
7250 cu->ranges_base = DW_UNSND (attr);
7251 }
a2ce51a0
DE
7252 else if (stub_comp_dir != NULL)
7253 {
7254 /* Reconstruct the comp_dir attribute to simplify the code below. */
8d749320 7255 comp_dir = XOBNEW (&cu->comp_unit_obstack, struct attribute);
a2ce51a0
DE
7256 comp_dir->name = DW_AT_comp_dir;
7257 comp_dir->form = DW_FORM_string;
7258 DW_STRING_IS_CANONICAL (comp_dir) = 0;
7259 DW_STRING (comp_dir) = stub_comp_dir;
7260 }
b0c7bfa9
DE
7261
7262 /* Set up for reading the DWO CU/TU. */
7263 cu->dwo_unit = dwo_unit;
685af9cd 7264 dwarf2_section_info *section = dwo_unit->section;
b0c7bfa9 7265 dwarf2_read_section (objfile, section);
a32a8923 7266 abfd = get_section_bfd_owner (section);
9c541725
PA
7267 begin_info_ptr = info_ptr = (section->buffer
7268 + to_underlying (dwo_unit->sect_off));
b0c7bfa9 7269 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
b0c7bfa9
DE
7270
7271 if (this_cu->is_debug_types)
7272 {
b0c7bfa9
DE
7273 struct signatured_type *sig_type = (struct signatured_type *) this_cu;
7274
ed2dc618
SM
7275 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7276 &cu->header, section,
b0c7bfa9 7277 dwo_abbrev_section,
43988095 7278 info_ptr, rcuh_kind::TYPE);
a2ce51a0 7279 /* This is not an assert because it can be caused by bad debug info. */
43988095 7280 if (sig_type->signature != cu->header.signature)
a2ce51a0
DE
7281 {
7282 error (_("Dwarf Error: signature mismatch %s vs %s while reading"
9d8780f0 7283 " TU at offset %s [in module %s]"),
a2ce51a0 7284 hex_string (sig_type->signature),
43988095 7285 hex_string (cu->header.signature),
9d8780f0 7286 sect_offset_str (dwo_unit->sect_off),
a2ce51a0
DE
7287 bfd_get_filename (abfd));
7288 }
9c541725 7289 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
b0c7bfa9
DE
7290 /* For DWOs coming from DWP files, we don't know the CU length
7291 nor the type's offset in the TU until now. */
7292 dwo_unit->length = get_cu_length (&cu->header);
9c541725 7293 dwo_unit->type_offset_in_tu = cu->header.type_cu_offset_in_tu;
b0c7bfa9
DE
7294
7295 /* Establish the type offset that can be used to lookup the type.
7296 For DWO files, we don't know it until now. */
9c541725
PA
7297 sig_type->type_offset_in_section
7298 = dwo_unit->sect_off + to_underlying (dwo_unit->type_offset_in_tu);
b0c7bfa9
DE
7299 }
7300 else
7301 {
ed2dc618
SM
7302 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7303 &cu->header, section,
b0c7bfa9 7304 dwo_abbrev_section,
43988095 7305 info_ptr, rcuh_kind::COMPILE);
9c541725 7306 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
b0c7bfa9
DE
7307 /* For DWOs coming from DWP files, we don't know the CU length
7308 until now. */
7309 dwo_unit->length = get_cu_length (&cu->header);
7310 }
7311
685af9cd
TT
7312 *result_dwo_abbrev_table
7313 = abbrev_table_read_table (dwarf2_per_objfile, dwo_abbrev_section,
7314 cu->header.abbrev_sect_off);
7315 init_cu_die_reader (result_reader, cu, section, dwo_unit->dwo_file,
7316 result_dwo_abbrev_table->get ());
b0c7bfa9
DE
7317
7318 /* Read in the die, but leave space to copy over the attributes
7319 from the stub. This has the benefit of simplifying the rest of
7320 the code - all the work to maintain the illusion of a single
7321 DW_TAG_{compile,type}_unit DIE is done here. */
7322 num_extra_attrs = ((stmt_list != NULL)
7323 + (low_pc != NULL)
7324 + (high_pc != NULL)
7325 + (ranges != NULL)
7326 + (comp_dir != NULL));
7327 info_ptr = read_full_die_1 (result_reader, result_comp_unit_die, info_ptr,
7328 result_has_children, num_extra_attrs);
7329
7330 /* Copy over the attributes from the stub to the DIE we just read in. */
7331 comp_unit_die = *result_comp_unit_die;
7332 i = comp_unit_die->num_attrs;
7333 if (stmt_list != NULL)
7334 comp_unit_die->attrs[i++] = *stmt_list;
7335 if (low_pc != NULL)
7336 comp_unit_die->attrs[i++] = *low_pc;
7337 if (high_pc != NULL)
7338 comp_unit_die->attrs[i++] = *high_pc;
7339 if (ranges != NULL)
7340 comp_unit_die->attrs[i++] = *ranges;
7341 if (comp_dir != NULL)
7342 comp_unit_die->attrs[i++] = *comp_dir;
7343 comp_unit_die->num_attrs += num_extra_attrs;
7344
b4f54984 7345 if (dwarf_die_debug)
bf6af496
DE
7346 {
7347 fprintf_unfiltered (gdb_stdlog,
7348 "Read die from %s@0x%x of %s:\n",
a32a8923 7349 get_section_name (section),
bf6af496
DE
7350 (unsigned) (begin_info_ptr - section->buffer),
7351 bfd_get_filename (abfd));
b4f54984 7352 dump_die (comp_unit_die, dwarf_die_debug);
bf6af496
DE
7353 }
7354
a2ce51a0
DE
7355 /* Save the comp_dir attribute. If there is no DWP file then we'll read
7356 TUs by skipping the stub and going directly to the entry in the DWO file.
7357 However, skipping the stub means we won't get DW_AT_comp_dir, so we have
7358 to get it via circuitous means. Blech. */
7359 if (comp_dir != NULL)
7360 result_reader->comp_dir = DW_STRING (comp_dir);
7361
b0c7bfa9
DE
7362 /* Skip dummy compilation units. */
7363 if (info_ptr >= begin_info_ptr + dwo_unit->length
7364 || peek_abbrev_code (abfd, info_ptr) == 0)
7365 return 0;
7366
7367 *result_info_ptr = info_ptr;
7368 return 1;
7369}
7370
7371/* Subroutine of init_cutu_and_read_dies to simplify it.
7372 Look up the DWO unit specified by COMP_UNIT_DIE of THIS_CU.
6a506a2d 7373 Returns NULL if the specified DWO unit cannot be found. */
b0c7bfa9
DE
7374
7375static struct dwo_unit *
7376lookup_dwo_unit (struct dwarf2_per_cu_data *this_cu,
7377 struct die_info *comp_unit_die)
7378{
7379 struct dwarf2_cu *cu = this_cu->cu;
b0c7bfa9
DE
7380 ULONGEST signature;
7381 struct dwo_unit *dwo_unit;
7382 const char *comp_dir, *dwo_name;
7383
a2ce51a0
DE
7384 gdb_assert (cu != NULL);
7385
b0c7bfa9 7386 /* Yeah, we look dwo_name up again, but it simplifies the code. */
7d45c7c3
KB
7387 dwo_name = dwarf2_string_attr (comp_unit_die, DW_AT_GNU_dwo_name, cu);
7388 comp_dir = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
b0c7bfa9
DE
7389
7390 if (this_cu->is_debug_types)
7391 {
7392 struct signatured_type *sig_type;
7393
7394 /* Since this_cu is the first member of struct signatured_type,
7395 we can go from a pointer to one to a pointer to the other. */
7396 sig_type = (struct signatured_type *) this_cu;
7397 signature = sig_type->signature;
7398 dwo_unit = lookup_dwo_type_unit (sig_type, dwo_name, comp_dir);
7399 }
7400 else
7401 {
7402 struct attribute *attr;
7403
7404 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
7405 if (! attr)
7406 error (_("Dwarf Error: missing dwo_id for dwo_name %s"
7407 " [in module %s]"),
e3b94546 7408 dwo_name, objfile_name (this_cu->dwarf2_per_objfile->objfile));
b0c7bfa9
DE
7409 signature = DW_UNSND (attr);
7410 dwo_unit = lookup_dwo_comp_unit (this_cu, dwo_name, comp_dir,
7411 signature);
7412 }
7413
b0c7bfa9
DE
7414 return dwo_unit;
7415}
7416
a2ce51a0 7417/* Subroutine of init_cutu_and_read_dies to simplify it.
6aa5f3a6 7418 See it for a description of the parameters.
fcd3b13d 7419 Read a TU directly from a DWO file, bypassing the stub. */
a2ce51a0
DE
7420
7421static void
6aa5f3a6
DE
7422init_tu_and_read_dwo_dies (struct dwarf2_per_cu_data *this_cu,
7423 int use_existing_cu, int keep,
a2ce51a0
DE
7424 die_reader_func_ftype *die_reader_func,
7425 void *data)
7426{
fcd3b13d 7427 std::unique_ptr<dwarf2_cu> new_cu;
a2ce51a0 7428 struct signatured_type *sig_type;
a2ce51a0
DE
7429 struct die_reader_specs reader;
7430 const gdb_byte *info_ptr;
7431 struct die_info *comp_unit_die;
7432 int has_children;
ed2dc618 7433 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
a2ce51a0
DE
7434
7435 /* Verify we can do the following downcast, and that we have the
7436 data we need. */
7437 gdb_assert (this_cu->is_debug_types && this_cu->reading_dwo_directly);
7438 sig_type = (struct signatured_type *) this_cu;
7439 gdb_assert (sig_type->dwo_unit != NULL);
7440
6aa5f3a6
DE
7441 if (use_existing_cu && this_cu->cu != NULL)
7442 {
7443 gdb_assert (this_cu->cu->dwo_unit == sig_type->dwo_unit);
6aa5f3a6
DE
7444 /* There's no need to do the rereading_dwo_cu handling that
7445 init_cutu_and_read_dies does since we don't read the stub. */
7446 }
7447 else
7448 {
7449 /* If !use_existing_cu, this_cu->cu must be NULL. */
7450 gdb_assert (this_cu->cu == NULL);
fcd3b13d 7451 new_cu.reset (new dwarf2_cu (this_cu));
6aa5f3a6
DE
7452 }
7453
7454 /* A future optimization, if needed, would be to use an existing
7455 abbrev table. When reading DWOs with skeletonless TUs, all the TUs
7456 could share abbrev tables. */
a2ce51a0 7457
685af9cd
TT
7458 /* The abbreviation table used by READER, this must live at least as long as
7459 READER. */
7460 abbrev_table_up dwo_abbrev_table;
7461
a2ce51a0 7462 if (read_cutu_die_from_dwo (this_cu, sig_type->dwo_unit,
a2ce51a0
DE
7463 NULL /* stub_comp_unit_die */,
7464 sig_type->dwo_unit->dwo_file->comp_dir,
7465 &reader, &info_ptr,
685af9cd
TT
7466 &comp_unit_die, &has_children,
7467 &dwo_abbrev_table) == 0)
a2ce51a0
DE
7468 {
7469 /* Dummy die. */
a2ce51a0
DE
7470 return;
7471 }
7472
7473 /* All the "real" work is done here. */
7474 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
7475
6aa5f3a6 7476 /* This duplicates the code in init_cutu_and_read_dies,
a2ce51a0
DE
7477 but the alternative is making the latter more complex.
7478 This function is only for the special case of using DWO files directly:
7479 no point in overly complicating the general case just to handle this. */
fcd3b13d 7480 if (new_cu != NULL && keep)
a2ce51a0 7481 {
fcd3b13d
SM
7482 /* Link this CU into read_in_chain. */
7483 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
7484 dwarf2_per_objfile->read_in_chain = this_cu;
7485 /* The chain owns it now. */
7486 new_cu.release ();
a2ce51a0 7487 }
a2ce51a0
DE
7488}
7489
fd820528 7490/* Initialize a CU (or TU) and read its DIEs.
3019eac3 7491 If the CU defers to a DWO file, read the DWO file as well.
dee91e82 7492
f4dc4d17
DE
7493 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
7494 Otherwise the table specified in the comp unit header is read in and used.
7495 This is an optimization for when we already have the abbrev table.
7496
dee91e82
DE
7497 If USE_EXISTING_CU is non-zero, and THIS_CU->cu is non-NULL, then use it.
7498 Otherwise, a new CU is allocated with xmalloc.
7499
7500 If KEEP is non-zero, then if we allocated a dwarf2_cu we add it to
7501 read_in_chain. Otherwise the dwarf2_cu data is freed at the end.
7502
7503 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
fd820528 7504 linker) then DIE_READER_FUNC will not get called. */
aaa75496 7505
70221824 7506static void
fd820528 7507init_cutu_and_read_dies (struct dwarf2_per_cu_data *this_cu,
f4dc4d17 7508 struct abbrev_table *abbrev_table,
fd820528 7509 int use_existing_cu, int keep,
58f0c718 7510 bool skip_partial,
fd820528
DE
7511 die_reader_func_ftype *die_reader_func,
7512 void *data)
c906108c 7513{
ed2dc618 7514 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
dee91e82 7515 struct objfile *objfile = dwarf2_per_objfile->objfile;
8a0459fd 7516 struct dwarf2_section_info *section = this_cu->section;
a32a8923 7517 bfd *abfd = get_section_bfd_owner (section);
dee91e82 7518 struct dwarf2_cu *cu;
d521ce57 7519 const gdb_byte *begin_info_ptr, *info_ptr;
dee91e82 7520 struct die_reader_specs reader;
d85a05f0 7521 struct die_info *comp_unit_die;
dee91e82 7522 int has_children;
d85a05f0 7523 struct attribute *attr;
dee91e82 7524 struct signatured_type *sig_type = NULL;
4bdcc0c1 7525 struct dwarf2_section_info *abbrev_section;
42e7ad6c
DE
7526 /* Non-zero if CU currently points to a DWO file and we need to
7527 reread it. When this happens we need to reread the skeleton die
a2ce51a0 7528 before we can reread the DWO file (this only applies to CUs, not TUs). */
42e7ad6c 7529 int rereading_dwo_cu = 0;
c906108c 7530
b4f54984 7531 if (dwarf_die_debug)
9d8780f0 7532 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
09406207 7533 this_cu->is_debug_types ? "type" : "comp",
9d8780f0 7534 sect_offset_str (this_cu->sect_off));
09406207 7535
dee91e82
DE
7536 if (use_existing_cu)
7537 gdb_assert (keep);
23745b47 7538
a2ce51a0
DE
7539 /* If we're reading a TU directly from a DWO file, including a virtual DWO
7540 file (instead of going through the stub), short-circuit all of this. */
7541 if (this_cu->reading_dwo_directly)
7542 {
7543 /* Narrow down the scope of possibilities to have to understand. */
7544 gdb_assert (this_cu->is_debug_types);
7545 gdb_assert (abbrev_table == NULL);
6aa5f3a6
DE
7546 init_tu_and_read_dwo_dies (this_cu, use_existing_cu, keep,
7547 die_reader_func, data);
a2ce51a0
DE
7548 return;
7549 }
7550
dee91e82
DE
7551 /* This is cheap if the section is already read in. */
7552 dwarf2_read_section (objfile, section);
7553
9c541725 7554 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
36586728
TT
7555
7556 abbrev_section = get_abbrev_section_for_cu (this_cu);
dee91e82 7557
fcd3b13d 7558 std::unique_ptr<dwarf2_cu> new_cu;
dee91e82
DE
7559 if (use_existing_cu && this_cu->cu != NULL)
7560 {
7561 cu = this_cu->cu;
42e7ad6c
DE
7562 /* If this CU is from a DWO file we need to start over, we need to
7563 refetch the attributes from the skeleton CU.
7564 This could be optimized by retrieving those attributes from when we
7565 were here the first time: the previous comp_unit_die was stored in
7566 comp_unit_obstack. But there's no data yet that we need this
7567 optimization. */
7568 if (cu->dwo_unit != NULL)
7569 rereading_dwo_cu = 1;
dee91e82
DE
7570 }
7571 else
7572 {
7573 /* If !use_existing_cu, this_cu->cu must be NULL. */
7574 gdb_assert (this_cu->cu == NULL);
fcd3b13d
SM
7575 new_cu.reset (new dwarf2_cu (this_cu));
7576 cu = new_cu.get ();
42e7ad6c 7577 }
dee91e82 7578
b0c7bfa9 7579 /* Get the header. */
9c541725 7580 if (to_underlying (cu->header.first_die_cu_offset) != 0 && !rereading_dwo_cu)
42e7ad6c
DE
7581 {
7582 /* We already have the header, there's no need to read it in again. */
9c541725 7583 info_ptr += to_underlying (cu->header.first_die_cu_offset);
42e7ad6c
DE
7584 }
7585 else
7586 {
3019eac3 7587 if (this_cu->is_debug_types)
dee91e82 7588 {
ed2dc618
SM
7589 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7590 &cu->header, section,
4bdcc0c1 7591 abbrev_section, info_ptr,
43988095 7592 rcuh_kind::TYPE);
dee91e82 7593
42e7ad6c
DE
7594 /* Since per_cu is the first member of struct signatured_type,
7595 we can go from a pointer to one to a pointer to the other. */
7596 sig_type = (struct signatured_type *) this_cu;
43988095 7597 gdb_assert (sig_type->signature == cu->header.signature);
9c541725
PA
7598 gdb_assert (sig_type->type_offset_in_tu
7599 == cu->header.type_cu_offset_in_tu);
7600 gdb_assert (this_cu->sect_off == cu->header.sect_off);
dee91e82 7601
42e7ad6c
DE
7602 /* LENGTH has not been set yet for type units if we're
7603 using .gdb_index. */
1ce1cefd 7604 this_cu->length = get_cu_length (&cu->header);
3019eac3
DE
7605
7606 /* Establish the type offset that can be used to lookup the type. */
9c541725
PA
7607 sig_type->type_offset_in_section =
7608 this_cu->sect_off + to_underlying (sig_type->type_offset_in_tu);
43988095
JK
7609
7610 this_cu->dwarf_version = cu->header.version;
dee91e82
DE
7611 }
7612 else
7613 {
ed2dc618
SM
7614 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7615 &cu->header, section,
4bdcc0c1 7616 abbrev_section,
43988095
JK
7617 info_ptr,
7618 rcuh_kind::COMPILE);
dee91e82 7619
9c541725 7620 gdb_assert (this_cu->sect_off == cu->header.sect_off);
1ce1cefd 7621 gdb_assert (this_cu->length == get_cu_length (&cu->header));
43988095 7622 this_cu->dwarf_version = cu->header.version;
dee91e82
DE
7623 }
7624 }
10b3939b 7625
6caca83c 7626 /* Skip dummy compilation units. */
dee91e82 7627 if (info_ptr >= begin_info_ptr + this_cu->length
6caca83c 7628 || peek_abbrev_code (abfd, info_ptr) == 0)
fcd3b13d 7629 return;
6caca83c 7630
433df2d4
DE
7631 /* If we don't have them yet, read the abbrevs for this compilation unit.
7632 And if we need to read them now, make sure they're freed when we're
685af9cd
TT
7633 done (own the table through ABBREV_TABLE_HOLDER). */
7634 abbrev_table_up abbrev_table_holder;
f4dc4d17 7635 if (abbrev_table != NULL)
685af9cd
TT
7636 gdb_assert (cu->header.abbrev_sect_off == abbrev_table->sect_off);
7637 else
f4dc4d17 7638 {
685af9cd
TT
7639 abbrev_table_holder
7640 = abbrev_table_read_table (dwarf2_per_objfile, abbrev_section,
7641 cu->header.abbrev_sect_off);
7642 abbrev_table = abbrev_table_holder.get ();
42e7ad6c 7643 }
af703f96 7644
dee91e82 7645 /* Read the top level CU/TU die. */
685af9cd 7646 init_cu_die_reader (&reader, cu, section, NULL, abbrev_table);
dee91e82 7647 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
93311388 7648
58f0c718
TT
7649 if (skip_partial && comp_unit_die->tag == DW_TAG_partial_unit)
7650 return;
7651
b0c7bfa9 7652 /* If we are in a DWO stub, process it and then read in the "real" CU/TU
685af9cd
TT
7653 from the DWO file. read_cutu_die_from_dwo will allocate the abbreviation
7654 table from the DWO file and pass the ownership over to us. It will be
7655 referenced from READER, so we must make sure to free it after we're done
7656 with READER.
7657
b0c7bfa9
DE
7658 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains a
7659 DWO CU, that this test will fail (the attribute will not be present). */
3019eac3 7660 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_name, cu);
685af9cd 7661 abbrev_table_up dwo_abbrev_table;
3019eac3
DE
7662 if (attr)
7663 {
3019eac3 7664 struct dwo_unit *dwo_unit;
b0c7bfa9 7665 struct die_info *dwo_comp_unit_die;
3019eac3
DE
7666
7667 if (has_children)
6a506a2d 7668 {
b98664d3 7669 complaint (_("compilation unit with DW_AT_GNU_dwo_name"
9d8780f0
SM
7670 " has children (offset %s) [in module %s]"),
7671 sect_offset_str (this_cu->sect_off),
7672 bfd_get_filename (abfd));
6a506a2d 7673 }
b0c7bfa9 7674 dwo_unit = lookup_dwo_unit (this_cu, comp_unit_die);
6a506a2d 7675 if (dwo_unit != NULL)
3019eac3 7676 {
6a506a2d 7677 if (read_cutu_die_from_dwo (this_cu, dwo_unit,
a2ce51a0 7678 comp_unit_die, NULL,
6a506a2d 7679 &reader, &info_ptr,
685af9cd
TT
7680 &dwo_comp_unit_die, &has_children,
7681 &dwo_abbrev_table) == 0)
6a506a2d
DE
7682 {
7683 /* Dummy die. */
6a506a2d
DE
7684 return;
7685 }
7686 comp_unit_die = dwo_comp_unit_die;
7687 }
7688 else
7689 {
7690 /* Yikes, we couldn't find the rest of the DIE, we only have
7691 the stub. A complaint has already been logged. There's
7692 not much more we can do except pass on the stub DIE to
7693 die_reader_func. We don't want to throw an error on bad
7694 debug info. */
3019eac3
DE
7695 }
7696 }
7697
b0c7bfa9 7698 /* All of the above is setup for this call. Yikes. */
dee91e82
DE
7699 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
7700
b0c7bfa9 7701 /* Done, clean up. */
fcd3b13d 7702 if (new_cu != NULL && keep)
348e048f 7703 {
fcd3b13d
SM
7704 /* Link this CU into read_in_chain. */
7705 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
7706 dwarf2_per_objfile->read_in_chain = this_cu;
7707 /* The chain owns it now. */
7708 new_cu.release ();
348e048f 7709 }
dee91e82
DE
7710}
7711
33e80786
DE
7712/* Read CU/TU THIS_CU but do not follow DW_AT_GNU_dwo_name if present.
7713 DWO_FILE, if non-NULL, is the DWO file to read (the caller is assumed
7714 to have already done the lookup to find the DWO file).
dee91e82
DE
7715
7716 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
3019eac3 7717 THIS_CU->is_debug_types, but nothing else.
dee91e82
DE
7718
7719 We fill in THIS_CU->length.
7720
7721 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
7722 linker) then DIE_READER_FUNC will not get called.
7723
7724 THIS_CU->cu is always freed when done.
3019eac3
DE
7725 This is done in order to not leave THIS_CU->cu in a state where we have
7726 to care whether it refers to the "main" CU or the DWO CU. */
dee91e82
DE
7727
7728static void
7729init_cutu_and_read_dies_no_follow (struct dwarf2_per_cu_data *this_cu,
3019eac3 7730 struct dwo_file *dwo_file,
dee91e82
DE
7731 die_reader_func_ftype *die_reader_func,
7732 void *data)
7733{
ed2dc618 7734 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
dee91e82 7735 struct objfile *objfile = dwarf2_per_objfile->objfile;
8a0459fd 7736 struct dwarf2_section_info *section = this_cu->section;
a32a8923 7737 bfd *abfd = get_section_bfd_owner (section);
33e80786 7738 struct dwarf2_section_info *abbrev_section;
d521ce57 7739 const gdb_byte *begin_info_ptr, *info_ptr;
dee91e82 7740 struct die_reader_specs reader;
dee91e82
DE
7741 struct die_info *comp_unit_die;
7742 int has_children;
7743
b4f54984 7744 if (dwarf_die_debug)
9d8780f0 7745 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
09406207 7746 this_cu->is_debug_types ? "type" : "comp",
9d8780f0 7747 sect_offset_str (this_cu->sect_off));
09406207 7748
dee91e82
DE
7749 gdb_assert (this_cu->cu == NULL);
7750
33e80786
DE
7751 abbrev_section = (dwo_file != NULL
7752 ? &dwo_file->sections.abbrev
7753 : get_abbrev_section_for_cu (this_cu));
7754
dee91e82
DE
7755 /* This is cheap if the section is already read in. */
7756 dwarf2_read_section (objfile, section);
7757
fcd3b13d 7758 struct dwarf2_cu cu (this_cu);
dee91e82 7759
9c541725 7760 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
ed2dc618
SM
7761 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7762 &cu.header, section,
4bdcc0c1 7763 abbrev_section, info_ptr,
43988095
JK
7764 (this_cu->is_debug_types
7765 ? rcuh_kind::TYPE
7766 : rcuh_kind::COMPILE));
dee91e82 7767
1ce1cefd 7768 this_cu->length = get_cu_length (&cu.header);
dee91e82
DE
7769
7770 /* Skip dummy compilation units. */
7771 if (info_ptr >= begin_info_ptr + this_cu->length
7772 || peek_abbrev_code (abfd, info_ptr) == 0)
fcd3b13d 7773 return;
72bf9492 7774
685af9cd
TT
7775 abbrev_table_up abbrev_table
7776 = abbrev_table_read_table (dwarf2_per_objfile, abbrev_section,
7777 cu.header.abbrev_sect_off);
dee91e82 7778
685af9cd 7779 init_cu_die_reader (&reader, &cu, section, dwo_file, abbrev_table.get ());
dee91e82
DE
7780 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
7781
7782 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
dee91e82
DE
7783}
7784
3019eac3
DE
7785/* Read a CU/TU, except that this does not look for DW_AT_GNU_dwo_name and
7786 does not lookup the specified DWO file.
7787 This cannot be used to read DWO files.
dee91e82
DE
7788
7789 THIS_CU->cu is always freed when done.
3019eac3
DE
7790 This is done in order to not leave THIS_CU->cu in a state where we have
7791 to care whether it refers to the "main" CU or the DWO CU.
7792 We can revisit this if the data shows there's a performance issue. */
dee91e82
DE
7793
7794static void
7795init_cutu_and_read_dies_simple (struct dwarf2_per_cu_data *this_cu,
7796 die_reader_func_ftype *die_reader_func,
7797 void *data)
7798{
33e80786 7799 init_cutu_and_read_dies_no_follow (this_cu, NULL, die_reader_func, data);
dee91e82 7800}
0018ea6f
DE
7801\f
7802/* Type Unit Groups.
dee91e82 7803
0018ea6f
DE
7804 Type Unit Groups are a way to collapse the set of all TUs (type units) into
7805 a more manageable set. The grouping is done by DW_AT_stmt_list entry
7806 so that all types coming from the same compilation (.o file) are grouped
7807 together. A future step could be to put the types in the same symtab as
7808 the CU the types ultimately came from. */
ff013f42 7809
f4dc4d17
DE
7810static hashval_t
7811hash_type_unit_group (const void *item)
7812{
9a3c8263
SM
7813 const struct type_unit_group *tu_group
7814 = (const struct type_unit_group *) item;
f4dc4d17 7815
094b34ac 7816 return hash_stmt_list_entry (&tu_group->hash);
f4dc4d17 7817}
348e048f
DE
7818
7819static int
f4dc4d17 7820eq_type_unit_group (const void *item_lhs, const void *item_rhs)
348e048f 7821{
9a3c8263
SM
7822 const struct type_unit_group *lhs = (const struct type_unit_group *) item_lhs;
7823 const struct type_unit_group *rhs = (const struct type_unit_group *) item_rhs;
348e048f 7824
094b34ac 7825 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
f4dc4d17 7826}
348e048f 7827
f4dc4d17
DE
7828/* Allocate a hash table for type unit groups. */
7829
7830static htab_t
ed2dc618 7831allocate_type_unit_groups_table (struct objfile *objfile)
f4dc4d17
DE
7832{
7833 return htab_create_alloc_ex (3,
7834 hash_type_unit_group,
7835 eq_type_unit_group,
7836 NULL,
ed2dc618 7837 &objfile->objfile_obstack,
f4dc4d17
DE
7838 hashtab_obstack_allocate,
7839 dummy_obstack_deallocate);
7840}
dee91e82 7841
f4dc4d17
DE
7842/* Type units that don't have DW_AT_stmt_list are grouped into their own
7843 partial symtabs. We combine several TUs per psymtab to not let the size
7844 of any one psymtab grow too big. */
7845#define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
7846#define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
dee91e82 7847
094b34ac 7848/* Helper routine for get_type_unit_group.
f4dc4d17
DE
7849 Create the type_unit_group object used to hold one or more TUs. */
7850
7851static struct type_unit_group *
094b34ac 7852create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
f4dc4d17 7853{
518817b3
SM
7854 struct dwarf2_per_objfile *dwarf2_per_objfile
7855 = cu->per_cu->dwarf2_per_objfile;
f4dc4d17 7856 struct objfile *objfile = dwarf2_per_objfile->objfile;
094b34ac 7857 struct dwarf2_per_cu_data *per_cu;
f4dc4d17 7858 struct type_unit_group *tu_group;
f4dc4d17
DE
7859
7860 tu_group = OBSTACK_ZALLOC (&objfile->objfile_obstack,
7861 struct type_unit_group);
094b34ac 7862 per_cu = &tu_group->per_cu;
518817b3 7863 per_cu->dwarf2_per_objfile = dwarf2_per_objfile;
f4dc4d17 7864
094b34ac
DE
7865 if (dwarf2_per_objfile->using_index)
7866 {
7867 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
7868 struct dwarf2_per_cu_quick_data);
094b34ac
DE
7869 }
7870 else
7871 {
9c541725 7872 unsigned int line_offset = to_underlying (line_offset_struct);
094b34ac 7873 struct partial_symtab *pst;
528e1572 7874 std::string name;
094b34ac
DE
7875
7876 /* Give the symtab a useful name for debug purposes. */
7877 if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
528e1572
SM
7878 name = string_printf ("<type_units_%d>",
7879 (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
094b34ac 7880 else
528e1572 7881 name = string_printf ("<type_units_at_0x%x>", line_offset);
094b34ac 7882
528e1572 7883 pst = create_partial_symtab (per_cu, name.c_str ());
094b34ac 7884 pst->anonymous = 1;
094b34ac 7885 }
f4dc4d17 7886
094b34ac 7887 tu_group->hash.dwo_unit = cu->dwo_unit;
9c541725 7888 tu_group->hash.line_sect_off = line_offset_struct;
f4dc4d17
DE
7889
7890 return tu_group;
7891}
7892
094b34ac
DE
7893/* Look up the type_unit_group for type unit CU, and create it if necessary.
7894 STMT_LIST is a DW_AT_stmt_list attribute. */
f4dc4d17
DE
7895
7896static struct type_unit_group *
ff39bb5e 7897get_type_unit_group (struct dwarf2_cu *cu, const struct attribute *stmt_list)
f4dc4d17 7898{
518817b3
SM
7899 struct dwarf2_per_objfile *dwarf2_per_objfile
7900 = cu->per_cu->dwarf2_per_objfile;
f4dc4d17
DE
7901 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
7902 struct type_unit_group *tu_group;
7903 void **slot;
7904 unsigned int line_offset;
7905 struct type_unit_group type_unit_group_for_lookup;
7906
7907 if (dwarf2_per_objfile->type_unit_groups == NULL)
7908 {
7909 dwarf2_per_objfile->type_unit_groups =
ed2dc618 7910 allocate_type_unit_groups_table (dwarf2_per_objfile->objfile);
f4dc4d17
DE
7911 }
7912
7913 /* Do we need to create a new group, or can we use an existing one? */
7914
7915 if (stmt_list)
7916 {
7917 line_offset = DW_UNSND (stmt_list);
7918 ++tu_stats->nr_symtab_sharers;
7919 }
7920 else
7921 {
7922 /* Ugh, no stmt_list. Rare, but we have to handle it.
7923 We can do various things here like create one group per TU or
7924 spread them over multiple groups to split up the expansion work.
7925 To avoid worst case scenarios (too many groups or too large groups)
7926 we, umm, group them in bunches. */
7927 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
7928 | (tu_stats->nr_stmt_less_type_units
7929 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
7930 ++tu_stats->nr_stmt_less_type_units;
7931 }
7932
094b34ac 7933 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
9c541725 7934 type_unit_group_for_lookup.hash.line_sect_off = (sect_offset) line_offset;
f4dc4d17
DE
7935 slot = htab_find_slot (dwarf2_per_objfile->type_unit_groups,
7936 &type_unit_group_for_lookup, INSERT);
7937 if (*slot != NULL)
7938 {
9a3c8263 7939 tu_group = (struct type_unit_group *) *slot;
f4dc4d17
DE
7940 gdb_assert (tu_group != NULL);
7941 }
7942 else
7943 {
9c541725 7944 sect_offset line_offset_struct = (sect_offset) line_offset;
094b34ac 7945 tu_group = create_type_unit_group (cu, line_offset_struct);
f4dc4d17
DE
7946 *slot = tu_group;
7947 ++tu_stats->nr_symtabs;
7948 }
7949
7950 return tu_group;
7951}
0018ea6f
DE
7952\f
7953/* Partial symbol tables. */
7954
7955/* Create a psymtab named NAME and assign it to PER_CU.
7956
7957 The caller must fill in the following details:
7958 dirname, textlow, texthigh. */
7959
7960static struct partial_symtab *
7961create_partial_symtab (struct dwarf2_per_cu_data *per_cu, const char *name)
7962{
e3b94546 7963 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
0018ea6f
DE
7964 struct partial_symtab *pst;
7965
939652a5 7966 pst = start_psymtab_common (objfile, name, 0);
0018ea6f
DE
7967
7968 pst->psymtabs_addrmap_supported = 1;
7969
7970 /* This is the glue that links PST into GDB's symbol API. */
7971 pst->read_symtab_private = per_cu;
7972 pst->read_symtab = dwarf2_read_symtab;
7973 per_cu->v.psymtab = pst;
7974
7975 return pst;
7976}
7977
b93601f3
TT
7978/* The DATA object passed to process_psymtab_comp_unit_reader has this
7979 type. */
7980
7981struct process_psymtab_comp_unit_data
7982{
7983 /* True if we are reading a DW_TAG_partial_unit. */
7984
7985 int want_partial_unit;
7986
7987 /* The "pretend" language that is used if the CU doesn't declare a
7988 language. */
7989
7990 enum language pretend_language;
7991};
7992
0018ea6f
DE
7993/* die_reader_func for process_psymtab_comp_unit. */
7994
7995static void
7996process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
d521ce57 7997 const gdb_byte *info_ptr,
0018ea6f
DE
7998 struct die_info *comp_unit_die,
7999 int has_children,
8000 void *data)
8001{
8002 struct dwarf2_cu *cu = reader->cu;
518817b3 8003 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
3e29f34a 8004 struct gdbarch *gdbarch = get_objfile_arch (objfile);
0018ea6f 8005 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
0018ea6f
DE
8006 CORE_ADDR baseaddr;
8007 CORE_ADDR best_lowpc = 0, best_highpc = 0;
8008 struct partial_symtab *pst;
3a2b436a 8009 enum pc_bounds_kind cu_bounds_kind;
0018ea6f 8010 const char *filename;
9a3c8263
SM
8011 struct process_psymtab_comp_unit_data *info
8012 = (struct process_psymtab_comp_unit_data *) data;
0018ea6f 8013
b93601f3 8014 if (comp_unit_die->tag == DW_TAG_partial_unit && !info->want_partial_unit)
0018ea6f
DE
8015 return;
8016
8017 gdb_assert (! per_cu->is_debug_types);
8018
b93601f3 8019 prepare_one_comp_unit (cu, comp_unit_die, info->pretend_language);
0018ea6f 8020
0018ea6f 8021 /* Allocate a new partial symbol table structure. */
7d45c7c3
KB
8022 filename = dwarf2_string_attr (comp_unit_die, DW_AT_name, cu);
8023 if (filename == NULL)
0018ea6f 8024 filename = "";
0018ea6f
DE
8025
8026 pst = create_partial_symtab (per_cu, filename);
8027
8028 /* This must be done before calling dwarf2_build_include_psymtabs. */
7d45c7c3 8029 pst->dirname = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
0018ea6f
DE
8030
8031 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
8032
8033 dwarf2_find_base_address (comp_unit_die, cu);
8034
8035 /* Possibly set the default values of LOWPC and HIGHPC from
8036 `DW_AT_ranges'. */
3a2b436a
JK
8037 cu_bounds_kind = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
8038 &best_highpc, cu, pst);
8039 if (cu_bounds_kind == PC_BOUNDS_HIGH_LOW && best_lowpc < best_highpc)
79748972
TT
8040 {
8041 CORE_ADDR low
8042 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_lowpc + baseaddr)
8043 - baseaddr);
8044 CORE_ADDR high
8045 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_highpc + baseaddr)
8046 - baseaddr - 1);
8047 /* Store the contiguous range if it is not empty; it can be
8048 empty for CUs with no code. */
d320c2b5
TT
8049 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
8050 low, high, pst);
79748972 8051 }
0018ea6f
DE
8052
8053 /* Check if comp unit has_children.
8054 If so, read the rest of the partial symbols from this comp unit.
8055 If not, there's no more debug_info for this comp unit. */
8056 if (has_children)
8057 {
8058 struct partial_die_info *first_die;
8059 CORE_ADDR lowpc, highpc;
8060
8061 lowpc = ((CORE_ADDR) -1);
8062 highpc = ((CORE_ADDR) 0);
8063
8064 first_die = load_partial_dies (reader, info_ptr, 1);
8065
8066 scan_partial_symbols (first_die, &lowpc, &highpc,
e385593e 8067 cu_bounds_kind <= PC_BOUNDS_INVALID, cu);
0018ea6f
DE
8068
8069 /* If we didn't find a lowpc, set it to highpc to avoid
8070 complaints from `maint check'. */
8071 if (lowpc == ((CORE_ADDR) -1))
8072 lowpc = highpc;
8073
8074 /* If the compilation unit didn't have an explicit address range,
8075 then use the information extracted from its child dies. */
e385593e 8076 if (cu_bounds_kind <= PC_BOUNDS_INVALID)
0018ea6f
DE
8077 {
8078 best_lowpc = lowpc;
8079 best_highpc = highpc;
8080 }
8081 }
4ae976d1 8082 pst->set_text_low (gdbarch_adjust_dwarf2_addr (gdbarch,
79748972
TT
8083 best_lowpc + baseaddr)
8084 - baseaddr);
4ae976d1 8085 pst->set_text_high (gdbarch_adjust_dwarf2_addr (gdbarch,
79748972
TT
8086 best_highpc + baseaddr)
8087 - baseaddr);
0018ea6f 8088
8763cede 8089 end_psymtab_common (objfile, pst);
0018ea6f
DE
8090
8091 if (!VEC_empty (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs))
8092 {
8093 int i;
8094 int len = VEC_length (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs);
8095 struct dwarf2_per_cu_data *iter;
8096
8097 /* Fill in 'dependencies' here; we fill in 'users' in a
8098 post-pass. */
8099 pst->number_of_dependencies = len;
a9342b62
TT
8100 pst->dependencies
8101 = objfile->partial_symtabs->allocate_dependencies (len);
0018ea6f
DE
8102 for (i = 0;
8103 VEC_iterate (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs,
8104 i, iter);
8105 ++i)
8106 pst->dependencies[i] = iter->v.psymtab;
8107
8108 VEC_free (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs);
8109 }
8110
8111 /* Get the list of files included in the current compilation unit,
8112 and build a psymtab for each of them. */
8113 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
8114
b4f54984 8115 if (dwarf_read_debug)
b926417a
TT
8116 fprintf_unfiltered (gdb_stdlog,
8117 "Psymtab for %s unit @%s: %s - %s"
8118 ", %d global, %d static syms\n",
8119 per_cu->is_debug_types ? "type" : "comp",
8120 sect_offset_str (per_cu->sect_off),
8121 paddress (gdbarch, pst->text_low (objfile)),
8122 paddress (gdbarch, pst->text_high (objfile)),
8123 pst->n_global_syms, pst->n_static_syms);
0018ea6f
DE
8124}
8125
8126/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
8127 Process compilation unit THIS_CU for a psymtab. */
8128
8129static void
8130process_psymtab_comp_unit (struct dwarf2_per_cu_data *this_cu,
b93601f3
TT
8131 int want_partial_unit,
8132 enum language pretend_language)
0018ea6f
DE
8133{
8134 /* If this compilation unit was already read in, free the
8135 cached copy in order to read it in again. This is
8136 necessary because we skipped some symbols when we first
8137 read in the compilation unit (see load_partial_dies).
8138 This problem could be avoided, but the benefit is unclear. */
8139 if (this_cu->cu != NULL)
8140 free_one_cached_comp_unit (this_cu);
8141
f1902523 8142 if (this_cu->is_debug_types)
58f0c718
TT
8143 init_cutu_and_read_dies (this_cu, NULL, 0, 0, false,
8144 build_type_psymtabs_reader, NULL);
f1902523
JK
8145 else
8146 {
8147 process_psymtab_comp_unit_data info;
8148 info.want_partial_unit = want_partial_unit;
8149 info.pretend_language = pretend_language;
58f0c718 8150 init_cutu_and_read_dies (this_cu, NULL, 0, 0, false,
f1902523
JK
8151 process_psymtab_comp_unit_reader, &info);
8152 }
0018ea6f
DE
8153
8154 /* Age out any secondary CUs. */
ed2dc618 8155 age_cached_comp_units (this_cu->dwarf2_per_objfile);
0018ea6f 8156}
f4dc4d17
DE
8157
8158/* Reader function for build_type_psymtabs. */
8159
8160static void
8161build_type_psymtabs_reader (const struct die_reader_specs *reader,
d521ce57 8162 const gdb_byte *info_ptr,
f4dc4d17
DE
8163 struct die_info *type_unit_die,
8164 int has_children,
8165 void *data)
8166{
ed2dc618 8167 struct dwarf2_per_objfile *dwarf2_per_objfile
518817b3 8168 = reader->cu->per_cu->dwarf2_per_objfile;
f4dc4d17
DE
8169 struct objfile *objfile = dwarf2_per_objfile->objfile;
8170 struct dwarf2_cu *cu = reader->cu;
8171 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
0186c6a7 8172 struct signatured_type *sig_type;
f4dc4d17
DE
8173 struct type_unit_group *tu_group;
8174 struct attribute *attr;
8175 struct partial_die_info *first_die;
8176 CORE_ADDR lowpc, highpc;
8177 struct partial_symtab *pst;
8178
8179 gdb_assert (data == NULL);
0186c6a7
DE
8180 gdb_assert (per_cu->is_debug_types);
8181 sig_type = (struct signatured_type *) per_cu;
f4dc4d17
DE
8182
8183 if (! has_children)
8184 return;
8185
8186 attr = dwarf2_attr_no_follow (type_unit_die, DW_AT_stmt_list);
094b34ac 8187 tu_group = get_type_unit_group (cu, attr);
f4dc4d17 8188
0186c6a7 8189 VEC_safe_push (sig_type_ptr, tu_group->tus, sig_type);
f4dc4d17
DE
8190
8191 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
f4dc4d17
DE
8192 pst = create_partial_symtab (per_cu, "");
8193 pst->anonymous = 1;
8194
8195 first_die = load_partial_dies (reader, info_ptr, 1);
8196
8197 lowpc = (CORE_ADDR) -1;
8198 highpc = (CORE_ADDR) 0;
8199 scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
8200
8763cede 8201 end_psymtab_common (objfile, pst);
f4dc4d17
DE
8202}
8203
73051182
DE
8204/* Struct used to sort TUs by their abbreviation table offset. */
8205
8206struct tu_abbrev_offset
8207{
b2bdb8cf
SM
8208 tu_abbrev_offset (signatured_type *sig_type_, sect_offset abbrev_offset_)
8209 : sig_type (sig_type_), abbrev_offset (abbrev_offset_)
8210 {}
8211
8212 signatured_type *sig_type;
73051182
DE
8213 sect_offset abbrev_offset;
8214};
8215
484cf504 8216/* Helper routine for build_type_psymtabs_1, passed to std::sort. */
73051182 8217
484cf504
TT
8218static bool
8219sort_tu_by_abbrev_offset (const struct tu_abbrev_offset &a,
8220 const struct tu_abbrev_offset &b)
73051182 8221{
484cf504 8222 return a.abbrev_offset < b.abbrev_offset;
73051182
DE
8223}
8224
8225/* Efficiently read all the type units.
8226 This does the bulk of the work for build_type_psymtabs.
8227
8228 The efficiency is because we sort TUs by the abbrev table they use and
8229 only read each abbrev table once. In one program there are 200K TUs
8230 sharing 8K abbrev tables.
8231
8232 The main purpose of this function is to support building the
8233 dwarf2_per_objfile->type_unit_groups table.
8234 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
8235 can collapse the search space by grouping them by stmt_list.
8236 The savings can be significant, in the same program from above the 200K TUs
8237 share 8K stmt_list tables.
8238
8239 FUNC is expected to call get_type_unit_group, which will create the
8240 struct type_unit_group if necessary and add it to
8241 dwarf2_per_objfile->type_unit_groups. */
8242
8243static void
ed2dc618 8244build_type_psymtabs_1 (struct dwarf2_per_objfile *dwarf2_per_objfile)
73051182 8245{
73051182 8246 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
685af9cd 8247 abbrev_table_up abbrev_table;
73051182 8248 sect_offset abbrev_offset;
73051182
DE
8249
8250 /* It's up to the caller to not call us multiple times. */
8251 gdb_assert (dwarf2_per_objfile->type_unit_groups == NULL);
8252
b2bdb8cf 8253 if (dwarf2_per_objfile->all_type_units.empty ())
73051182
DE
8254 return;
8255
8256 /* TUs typically share abbrev tables, and there can be way more TUs than
8257 abbrev tables. Sort by abbrev table to reduce the number of times we
8258 read each abbrev table in.
8259 Alternatives are to punt or to maintain a cache of abbrev tables.
8260 This is simpler and efficient enough for now.
8261
8262 Later we group TUs by their DW_AT_stmt_list value (as this defines the
8263 symtab to use). Typically TUs with the same abbrev offset have the same
8264 stmt_list value too so in practice this should work well.
8265
8266 The basic algorithm here is:
8267
8268 sort TUs by abbrev table
8269 for each TU with same abbrev table:
8270 read abbrev table if first user
8271 read TU top level DIE
8272 [IWBN if DWO skeletons had DW_AT_stmt_list]
8273 call FUNC */
8274
b4f54984 8275 if (dwarf_read_debug)
73051182
DE
8276 fprintf_unfiltered (gdb_stdlog, "Building type unit groups ...\n");
8277
8278 /* Sort in a separate table to maintain the order of all_type_units
8279 for .gdb_index: TU indices directly index all_type_units. */
b2bdb8cf
SM
8280 std::vector<tu_abbrev_offset> sorted_by_abbrev;
8281 sorted_by_abbrev.reserve (dwarf2_per_objfile->all_type_units.size ());
8282
8283 for (signatured_type *sig_type : dwarf2_per_objfile->all_type_units)
8284 sorted_by_abbrev.emplace_back
8285 (sig_type, read_abbrev_offset (dwarf2_per_objfile,
8286 sig_type->per_cu.section,
8287 sig_type->per_cu.sect_off));
73051182 8288
484cf504
TT
8289 std::sort (sorted_by_abbrev.begin (), sorted_by_abbrev.end (),
8290 sort_tu_by_abbrev_offset);
73051182 8291
9c541725 8292 abbrev_offset = (sect_offset) ~(unsigned) 0;
73051182 8293
b2bdb8cf 8294 for (const tu_abbrev_offset &tu : sorted_by_abbrev)
73051182 8295 {
73051182
DE
8296 /* Switch to the next abbrev table if necessary. */
8297 if (abbrev_table == NULL
b2bdb8cf 8298 || tu.abbrev_offset != abbrev_offset)
73051182 8299 {
b2bdb8cf 8300 abbrev_offset = tu.abbrev_offset;
73051182 8301 abbrev_table =
ed2dc618
SM
8302 abbrev_table_read_table (dwarf2_per_objfile,
8303 &dwarf2_per_objfile->abbrev,
73051182
DE
8304 abbrev_offset);
8305 ++tu_stats->nr_uniq_abbrev_tables;
8306 }
8307
b2bdb8cf 8308 init_cutu_and_read_dies (&tu.sig_type->per_cu, abbrev_table.get (),
58f0c718 8309 0, 0, false, build_type_psymtabs_reader, NULL);
73051182 8310 }
6aa5f3a6 8311}
73051182 8312
6aa5f3a6
DE
8313/* Print collected type unit statistics. */
8314
8315static void
ed2dc618 8316print_tu_stats (struct dwarf2_per_objfile *dwarf2_per_objfile)
6aa5f3a6
DE
8317{
8318 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
8319
8320 fprintf_unfiltered (gdb_stdlog, "Type unit statistics:\n");
b2bdb8cf
SM
8321 fprintf_unfiltered (gdb_stdlog, " %zu TUs\n",
8322 dwarf2_per_objfile->all_type_units.size ());
6aa5f3a6
DE
8323 fprintf_unfiltered (gdb_stdlog, " %d uniq abbrev tables\n",
8324 tu_stats->nr_uniq_abbrev_tables);
8325 fprintf_unfiltered (gdb_stdlog, " %d symtabs from stmt_list entries\n",
8326 tu_stats->nr_symtabs);
8327 fprintf_unfiltered (gdb_stdlog, " %d symtab sharers\n",
8328 tu_stats->nr_symtab_sharers);
8329 fprintf_unfiltered (gdb_stdlog, " %d type units without a stmt_list\n",
8330 tu_stats->nr_stmt_less_type_units);
8331 fprintf_unfiltered (gdb_stdlog, " %d all_type_units reallocs\n",
8332 tu_stats->nr_all_type_units_reallocs);
73051182
DE
8333}
8334
f4dc4d17
DE
8335/* Traversal function for build_type_psymtabs. */
8336
8337static int
8338build_type_psymtab_dependencies (void **slot, void *info)
8339{
ed2dc618
SM
8340 struct dwarf2_per_objfile *dwarf2_per_objfile
8341 = (struct dwarf2_per_objfile *) info;
f4dc4d17
DE
8342 struct objfile *objfile = dwarf2_per_objfile->objfile;
8343 struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
094b34ac 8344 struct dwarf2_per_cu_data *per_cu = &tu_group->per_cu;
f4dc4d17 8345 struct partial_symtab *pst = per_cu->v.psymtab;
0186c6a7
DE
8346 int len = VEC_length (sig_type_ptr, tu_group->tus);
8347 struct signatured_type *iter;
f4dc4d17
DE
8348 int i;
8349
8350 gdb_assert (len > 0);
0186c6a7 8351 gdb_assert (IS_TYPE_UNIT_GROUP (per_cu));
f4dc4d17
DE
8352
8353 pst->number_of_dependencies = len;
a9342b62 8354 pst->dependencies = objfile->partial_symtabs->allocate_dependencies (len);
f4dc4d17 8355 for (i = 0;
0186c6a7 8356 VEC_iterate (sig_type_ptr, tu_group->tus, i, iter);
f4dc4d17
DE
8357 ++i)
8358 {
0186c6a7
DE
8359 gdb_assert (iter->per_cu.is_debug_types);
8360 pst->dependencies[i] = iter->per_cu.v.psymtab;
796a7ff8 8361 iter->type_unit_group = tu_group;
f4dc4d17
DE
8362 }
8363
0186c6a7 8364 VEC_free (sig_type_ptr, tu_group->tus);
348e048f
DE
8365
8366 return 1;
8367}
8368
8369/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
8370 Build partial symbol tables for the .debug_types comp-units. */
8371
8372static void
ed2dc618 8373build_type_psymtabs (struct dwarf2_per_objfile *dwarf2_per_objfile)
348e048f 8374{
ed2dc618 8375 if (! create_all_type_units (dwarf2_per_objfile))
348e048f
DE
8376 return;
8377
ed2dc618 8378 build_type_psymtabs_1 (dwarf2_per_objfile);
6aa5f3a6 8379}
f4dc4d17 8380
6aa5f3a6
DE
8381/* Traversal function for process_skeletonless_type_unit.
8382 Read a TU in a DWO file and build partial symbols for it. */
8383
8384static int
8385process_skeletonless_type_unit (void **slot, void *info)
8386{
8387 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
ed2dc618
SM
8388 struct dwarf2_per_objfile *dwarf2_per_objfile
8389 = (struct dwarf2_per_objfile *) info;
6aa5f3a6
DE
8390 struct signatured_type find_entry, *entry;
8391
8392 /* If this TU doesn't exist in the global table, add it and read it in. */
8393
8394 if (dwarf2_per_objfile->signatured_types == NULL)
8395 {
8396 dwarf2_per_objfile->signatured_types
ed2dc618 8397 = allocate_signatured_type_table (dwarf2_per_objfile->objfile);
6aa5f3a6
DE
8398 }
8399
8400 find_entry.signature = dwo_unit->signature;
8401 slot = htab_find_slot (dwarf2_per_objfile->signatured_types, &find_entry,
8402 INSERT);
8403 /* If we've already seen this type there's nothing to do. What's happening
8404 is we're doing our own version of comdat-folding here. */
8405 if (*slot != NULL)
8406 return 1;
8407
8408 /* This does the job that create_all_type_units would have done for
8409 this TU. */
ed2dc618
SM
8410 entry = add_type_unit (dwarf2_per_objfile, dwo_unit->signature, slot);
8411 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, entry, dwo_unit);
6aa5f3a6
DE
8412 *slot = entry;
8413
8414 /* This does the job that build_type_psymtabs_1 would have done. */
58f0c718 8415 init_cutu_and_read_dies (&entry->per_cu, NULL, 0, 0, false,
6aa5f3a6
DE
8416 build_type_psymtabs_reader, NULL);
8417
8418 return 1;
8419}
8420
8421/* Traversal function for process_skeletonless_type_units. */
8422
8423static int
8424process_dwo_file_for_skeletonless_type_units (void **slot, void *info)
8425{
8426 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
8427
8428 if (dwo_file->tus != NULL)
8429 {
8430 htab_traverse_noresize (dwo_file->tus,
8431 process_skeletonless_type_unit, info);
8432 }
8433
8434 return 1;
8435}
8436
8437/* Scan all TUs of DWO files, verifying we've processed them.
8438 This is needed in case a TU was emitted without its skeleton.
8439 Note: This can't be done until we know what all the DWO files are. */
8440
8441static void
ed2dc618 8442process_skeletonless_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
6aa5f3a6
DE
8443{
8444 /* Skeletonless TUs in DWP files without .gdb_index is not supported yet. */
ed2dc618 8445 if (get_dwp_file (dwarf2_per_objfile) == NULL
6aa5f3a6
DE
8446 && dwarf2_per_objfile->dwo_files != NULL)
8447 {
8448 htab_traverse_noresize (dwarf2_per_objfile->dwo_files,
8449 process_dwo_file_for_skeletonless_type_units,
ed2dc618 8450 dwarf2_per_objfile);
6aa5f3a6 8451 }
348e048f
DE
8452}
8453
ed2dc618 8454/* Compute the 'user' field for each psymtab in DWARF2_PER_OBJFILE. */
95554aad
TT
8455
8456static void
ed2dc618 8457set_partial_user (struct dwarf2_per_objfile *dwarf2_per_objfile)
95554aad 8458{
b76e467d 8459 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
95554aad 8460 {
95554aad 8461 struct partial_symtab *pst = per_cu->v.psymtab;
95554aad 8462
36586728
TT
8463 if (pst == NULL)
8464 continue;
8465
b76e467d 8466 for (int j = 0; j < pst->number_of_dependencies; ++j)
95554aad
TT
8467 {
8468 /* Set the 'user' field only if it is not already set. */
8469 if (pst->dependencies[j]->user == NULL)
8470 pst->dependencies[j]->user = pst;
8471 }
8472 }
8473}
8474
93311388
DE
8475/* Build the partial symbol table by doing a quick pass through the
8476 .debug_info and .debug_abbrev sections. */
72bf9492 8477
93311388 8478static void
ed2dc618 8479dwarf2_build_psymtabs_hard (struct dwarf2_per_objfile *dwarf2_per_objfile)
93311388 8480{
ed2dc618 8481 struct objfile *objfile = dwarf2_per_objfile->objfile;
93311388 8482
b4f54984 8483 if (dwarf_read_debug)
45cfd468
DE
8484 {
8485 fprintf_unfiltered (gdb_stdlog, "Building psymtabs of objfile %s ...\n",
4262abfb 8486 objfile_name (objfile));
45cfd468
DE
8487 }
8488
98bfdba5
PA
8489 dwarf2_per_objfile->reading_partial_symbols = 1;
8490
be391dca 8491 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
91c24f0a 8492
93311388
DE
8493 /* Any cached compilation units will be linked by the per-objfile
8494 read_in_chain. Make sure to free them when we're done. */
11ed8cad 8495 free_cached_comp_units freer (dwarf2_per_objfile);
72bf9492 8496
ed2dc618 8497 build_type_psymtabs (dwarf2_per_objfile);
348e048f 8498
ed2dc618 8499 create_all_comp_units (dwarf2_per_objfile);
c906108c 8500
60606b2c
TT
8501 /* Create a temporary address map on a temporary obstack. We later
8502 copy this to the final obstack. */
8268c778 8503 auto_obstack temp_obstack;
791afaa2
TT
8504
8505 scoped_restore save_psymtabs_addrmap
d320c2b5 8506 = make_scoped_restore (&objfile->partial_symtabs->psymtabs_addrmap,
791afaa2 8507 addrmap_create_mutable (&temp_obstack));
72bf9492 8508
b76e467d
SM
8509 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
8510 process_psymtab_comp_unit (per_cu, 0, language_minimal);
ff013f42 8511
6aa5f3a6 8512 /* This has to wait until we read the CUs, we need the list of DWOs. */
ed2dc618 8513 process_skeletonless_type_units (dwarf2_per_objfile);
6aa5f3a6
DE
8514
8515 /* Now that all TUs have been processed we can fill in the dependencies. */
8516 if (dwarf2_per_objfile->type_unit_groups != NULL)
8517 {
8518 htab_traverse_noresize (dwarf2_per_objfile->type_unit_groups,
ed2dc618 8519 build_type_psymtab_dependencies, dwarf2_per_objfile);
6aa5f3a6
DE
8520 }
8521
b4f54984 8522 if (dwarf_read_debug)
ed2dc618 8523 print_tu_stats (dwarf2_per_objfile);
6aa5f3a6 8524
ed2dc618 8525 set_partial_user (dwarf2_per_objfile);
95554aad 8526
d320c2b5
TT
8527 objfile->partial_symtabs->psymtabs_addrmap
8528 = addrmap_create_fixed (objfile->partial_symtabs->psymtabs_addrmap,
5923a04c 8529 objfile->partial_symtabs->obstack ());
791afaa2
TT
8530 /* At this point we want to keep the address map. */
8531 save_psymtabs_addrmap.release ();
ff013f42 8532
b4f54984 8533 if (dwarf_read_debug)
45cfd468 8534 fprintf_unfiltered (gdb_stdlog, "Done building psymtabs of %s\n",
4262abfb 8535 objfile_name (objfile));
ae038cb0
DJ
8536}
8537
3019eac3 8538/* die_reader_func for load_partial_comp_unit. */
ae038cb0
DJ
8539
8540static void
dee91e82 8541load_partial_comp_unit_reader (const struct die_reader_specs *reader,
d521ce57 8542 const gdb_byte *info_ptr,
dee91e82
DE
8543 struct die_info *comp_unit_die,
8544 int has_children,
8545 void *data)
ae038cb0 8546{
dee91e82 8547 struct dwarf2_cu *cu = reader->cu;
ae038cb0 8548
95554aad 8549 prepare_one_comp_unit (cu, comp_unit_die, language_minimal);
ae038cb0 8550
ae038cb0
DJ
8551 /* Check if comp unit has_children.
8552 If so, read the rest of the partial symbols from this comp unit.
0963b4bd 8553 If not, there's no more debug_info for this comp unit. */
d85a05f0 8554 if (has_children)
dee91e82
DE
8555 load_partial_dies (reader, info_ptr, 0);
8556}
98bfdba5 8557
dee91e82
DE
8558/* Load the partial DIEs for a secondary CU into memory.
8559 This is also used when rereading a primary CU with load_all_dies. */
c5b7e1cb 8560
dee91e82
DE
8561static void
8562load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu)
8563{
58f0c718 8564 init_cutu_and_read_dies (this_cu, NULL, 1, 1, false,
f4dc4d17 8565 load_partial_comp_unit_reader, NULL);
ae038cb0
DJ
8566}
8567
ae038cb0 8568static void
ed2dc618 8569read_comp_units_from_section (struct dwarf2_per_objfile *dwarf2_per_objfile,
36586728 8570 struct dwarf2_section_info *section,
f1902523 8571 struct dwarf2_section_info *abbrev_section,
b76e467d 8572 unsigned int is_dwz)
ae038cb0 8573{
d521ce57 8574 const gdb_byte *info_ptr;
ed2dc618 8575 struct objfile *objfile = dwarf2_per_objfile->objfile;
be391dca 8576
b4f54984 8577 if (dwarf_read_debug)
bf6af496 8578 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s\n",
a32a8923
DE
8579 get_section_name (section),
8580 get_section_file_name (section));
bf6af496 8581
36586728 8582 dwarf2_read_section (objfile, section);
ae038cb0 8583
36586728 8584 info_ptr = section->buffer;
6e70227d 8585
36586728 8586 while (info_ptr < section->buffer + section->size)
ae038cb0 8587 {
ae038cb0 8588 struct dwarf2_per_cu_data *this_cu;
ae038cb0 8589
9c541725 8590 sect_offset sect_off = (sect_offset) (info_ptr - section->buffer);
ae038cb0 8591
f1902523 8592 comp_unit_head cu_header;
ed2dc618
SM
8593 read_and_check_comp_unit_head (dwarf2_per_objfile, &cu_header, section,
8594 abbrev_section, info_ptr,
8595 rcuh_kind::COMPILE);
ae038cb0
DJ
8596
8597 /* Save the compilation unit for later lookup. */
f1902523
JK
8598 if (cu_header.unit_type != DW_UT_type)
8599 {
8600 this_cu = XOBNEW (&objfile->objfile_obstack,
8601 struct dwarf2_per_cu_data);
8602 memset (this_cu, 0, sizeof (*this_cu));
8603 }
8604 else
8605 {
8606 auto sig_type = XOBNEW (&objfile->objfile_obstack,
8607 struct signatured_type);
8608 memset (sig_type, 0, sizeof (*sig_type));
8609 sig_type->signature = cu_header.signature;
8610 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
8611 this_cu = &sig_type->per_cu;
8612 }
8613 this_cu->is_debug_types = (cu_header.unit_type == DW_UT_type);
9c541725 8614 this_cu->sect_off = sect_off;
f1902523 8615 this_cu->length = cu_header.length + cu_header.initial_length_size;
36586728 8616 this_cu->is_dwz = is_dwz;
e3b94546 8617 this_cu->dwarf2_per_objfile = dwarf2_per_objfile;
8a0459fd 8618 this_cu->section = section;
ae038cb0 8619
b76e467d 8620 dwarf2_per_objfile->all_comp_units.push_back (this_cu);
ae038cb0
DJ
8621
8622 info_ptr = info_ptr + this_cu->length;
8623 }
36586728
TT
8624}
8625
8626/* Create a list of all compilation units in OBJFILE.
8627 This is only done for -readnow and building partial symtabs. */
8628
8629static void
ed2dc618 8630create_all_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
36586728 8631{
b76e467d 8632 gdb_assert (dwarf2_per_objfile->all_comp_units.empty ());
ed2dc618 8633 read_comp_units_from_section (dwarf2_per_objfile, &dwarf2_per_objfile->info,
b76e467d 8634 &dwarf2_per_objfile->abbrev, 0);
36586728 8635
b76e467d 8636 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
4db1a1dc 8637 if (dwz != NULL)
ed2dc618 8638 read_comp_units_from_section (dwarf2_per_objfile, &dwz->info, &dwz->abbrev,
b76e467d 8639 1);
c906108c
SS
8640}
8641
5734ee8b 8642/* Process all loaded DIEs for compilation unit CU, starting at
cdc07690 8643 FIRST_DIE. The caller should pass SET_ADDRMAP == 1 if the compilation
5734ee8b 8644 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
cdc07690
YQ
8645 DW_AT_ranges). See the comments of add_partial_subprogram on how
8646 SET_ADDRMAP is used and how *LOWPC and *HIGHPC are updated. */
c906108c 8647
72bf9492
DJ
8648static void
8649scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
cdc07690
YQ
8650 CORE_ADDR *highpc, int set_addrmap,
8651 struct dwarf2_cu *cu)
c906108c 8652{
72bf9492 8653 struct partial_die_info *pdi;
c906108c 8654
91c24f0a
DC
8655 /* Now, march along the PDI's, descending into ones which have
8656 interesting children but skipping the children of the other ones,
8657 until we reach the end of the compilation unit. */
c906108c 8658
72bf9492 8659 pdi = first_die;
91c24f0a 8660
72bf9492
DJ
8661 while (pdi != NULL)
8662 {
52356b79 8663 pdi->fixup (cu);
c906108c 8664
f55ee35c 8665 /* Anonymous namespaces or modules have no name but have interesting
91c24f0a
DC
8666 children, so we need to look at them. Ditto for anonymous
8667 enums. */
933c6fe4 8668
72bf9492 8669 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
95554aad 8670 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
b1dc1806
XR
8671 || pdi->tag == DW_TAG_imported_unit
8672 || pdi->tag == DW_TAG_inlined_subroutine)
c906108c 8673 {
72bf9492 8674 switch (pdi->tag)
c906108c
SS
8675 {
8676 case DW_TAG_subprogram:
b1dc1806 8677 case DW_TAG_inlined_subroutine:
cdc07690 8678 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
c906108c 8679 break;
72929c62 8680 case DW_TAG_constant:
c906108c
SS
8681 case DW_TAG_variable:
8682 case DW_TAG_typedef:
91c24f0a 8683 case DW_TAG_union_type:
72bf9492 8684 if (!pdi->is_declaration)
63d06c5c 8685 {
72bf9492 8686 add_partial_symbol (pdi, cu);
63d06c5c
DC
8687 }
8688 break;
c906108c 8689 case DW_TAG_class_type:
680b30c7 8690 case DW_TAG_interface_type:
c906108c 8691 case DW_TAG_structure_type:
72bf9492 8692 if (!pdi->is_declaration)
c906108c 8693 {
72bf9492 8694 add_partial_symbol (pdi, cu);
c906108c 8695 }
b7fee5a3
KS
8696 if ((cu->language == language_rust
8697 || cu->language == language_cplus) && pdi->has_children)
e98c9e7c
TT
8698 scan_partial_symbols (pdi->die_child, lowpc, highpc,
8699 set_addrmap, cu);
c906108c 8700 break;
91c24f0a 8701 case DW_TAG_enumeration_type:
72bf9492
DJ
8702 if (!pdi->is_declaration)
8703 add_partial_enumeration (pdi, cu);
c906108c
SS
8704 break;
8705 case DW_TAG_base_type:
a02abb62 8706 case DW_TAG_subrange_type:
c906108c 8707 /* File scope base type definitions are added to the partial
c5aa993b 8708 symbol table. */
72bf9492 8709 add_partial_symbol (pdi, cu);
c906108c 8710 break;
d9fa45fe 8711 case DW_TAG_namespace:
cdc07690 8712 add_partial_namespace (pdi, lowpc, highpc, set_addrmap, cu);
91c24f0a 8713 break;
5d7cb8df 8714 case DW_TAG_module:
cdc07690 8715 add_partial_module (pdi, lowpc, highpc, set_addrmap, cu);
5d7cb8df 8716 break;
95554aad
TT
8717 case DW_TAG_imported_unit:
8718 {
8719 struct dwarf2_per_cu_data *per_cu;
8720
f4dc4d17
DE
8721 /* For now we don't handle imported units in type units. */
8722 if (cu->per_cu->is_debug_types)
8723 {
8724 error (_("Dwarf Error: DW_TAG_imported_unit is not"
8725 " supported in type units [in module %s]"),
518817b3 8726 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
f4dc4d17
DE
8727 }
8728
e3b94546
SM
8729 per_cu = dwarf2_find_containing_comp_unit
8730 (pdi->d.sect_off, pdi->is_dwz,
518817b3 8731 cu->per_cu->dwarf2_per_objfile);
95554aad
TT
8732
8733 /* Go read the partial unit, if needed. */
8734 if (per_cu->v.psymtab == NULL)
b93601f3 8735 process_psymtab_comp_unit (per_cu, 1, cu->language);
95554aad 8736
f4dc4d17 8737 VEC_safe_push (dwarf2_per_cu_ptr,
796a7ff8 8738 cu->per_cu->imported_symtabs, per_cu);
95554aad
TT
8739 }
8740 break;
74921315
KS
8741 case DW_TAG_imported_declaration:
8742 add_partial_symbol (pdi, cu);
8743 break;
c906108c
SS
8744 default:
8745 break;
8746 }
8747 }
8748
72bf9492
DJ
8749 /* If the die has a sibling, skip to the sibling. */
8750
8751 pdi = pdi->die_sibling;
8752 }
8753}
8754
8755/* Functions used to compute the fully scoped name of a partial DIE.
91c24f0a 8756
72bf9492 8757 Normally, this is simple. For C++, the parent DIE's fully scoped
9c37b5ae 8758 name is concatenated with "::" and the partial DIE's name.
72bf9492
DJ
8759 Enumerators are an exception; they use the scope of their parent
8760 enumeration type, i.e. the name of the enumeration type is not
8761 prepended to the enumerator.
91c24f0a 8762
72bf9492
DJ
8763 There are two complexities. One is DW_AT_specification; in this
8764 case "parent" means the parent of the target of the specification,
8765 instead of the direct parent of the DIE. The other is compilers
8766 which do not emit DW_TAG_namespace; in this case we try to guess
8767 the fully qualified name of structure types from their members'
8768 linkage names. This must be done using the DIE's children rather
8769 than the children of any DW_AT_specification target. We only need
8770 to do this for structures at the top level, i.e. if the target of
8771 any DW_AT_specification (if any; otherwise the DIE itself) does not
8772 have a parent. */
8773
8774/* Compute the scope prefix associated with PDI's parent, in
8775 compilation unit CU. The result will be allocated on CU's
8776 comp_unit_obstack, or a copy of the already allocated PDI->NAME
8777 field. NULL is returned if no prefix is necessary. */
15d034d0 8778static const char *
72bf9492
DJ
8779partial_die_parent_scope (struct partial_die_info *pdi,
8780 struct dwarf2_cu *cu)
8781{
15d034d0 8782 const char *grandparent_scope;
72bf9492 8783 struct partial_die_info *parent, *real_pdi;
91c24f0a 8784
72bf9492
DJ
8785 /* We need to look at our parent DIE; if we have a DW_AT_specification,
8786 then this means the parent of the specification DIE. */
8787
8788 real_pdi = pdi;
72bf9492 8789 while (real_pdi->has_specification)
36586728
TT
8790 real_pdi = find_partial_die (real_pdi->spec_offset,
8791 real_pdi->spec_is_dwz, cu);
72bf9492
DJ
8792
8793 parent = real_pdi->die_parent;
8794 if (parent == NULL)
8795 return NULL;
8796
8797 if (parent->scope_set)
8798 return parent->scope;
8799
52356b79 8800 parent->fixup (cu);
72bf9492 8801
10b3939b 8802 grandparent_scope = partial_die_parent_scope (parent, cu);
72bf9492 8803
acebe513
UW
8804 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
8805 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
8806 Work around this problem here. */
8807 if (cu->language == language_cplus
6e70227d 8808 && parent->tag == DW_TAG_namespace
acebe513
UW
8809 && strcmp (parent->name, "::") == 0
8810 && grandparent_scope == NULL)
8811 {
8812 parent->scope = NULL;
8813 parent->scope_set = 1;
8814 return NULL;
8815 }
8816
9c6c53f7
SA
8817 if (pdi->tag == DW_TAG_enumerator)
8818 /* Enumerators should not get the name of the enumeration as a prefix. */
8819 parent->scope = grandparent_scope;
8820 else if (parent->tag == DW_TAG_namespace
f55ee35c 8821 || parent->tag == DW_TAG_module
72bf9492
DJ
8822 || parent->tag == DW_TAG_structure_type
8823 || parent->tag == DW_TAG_class_type
680b30c7 8824 || parent->tag == DW_TAG_interface_type
ceeb3d5a
TT
8825 || parent->tag == DW_TAG_union_type
8826 || parent->tag == DW_TAG_enumeration_type)
72bf9492
DJ
8827 {
8828 if (grandparent_scope == NULL)
8829 parent->scope = parent->name;
8830 else
3e43a32a
MS
8831 parent->scope = typename_concat (&cu->comp_unit_obstack,
8832 grandparent_scope,
f55ee35c 8833 parent->name, 0, cu);
72bf9492 8834 }
72bf9492
DJ
8835 else
8836 {
8837 /* FIXME drow/2004-04-01: What should we be doing with
8838 function-local names? For partial symbols, we should probably be
8839 ignoring them. */
b98664d3 8840 complaint (_("unhandled containing DIE tag %d for DIE at %s"),
9d8780f0 8841 parent->tag, sect_offset_str (pdi->sect_off));
72bf9492 8842 parent->scope = grandparent_scope;
c906108c
SS
8843 }
8844
72bf9492
DJ
8845 parent->scope_set = 1;
8846 return parent->scope;
8847}
8848
8849/* Return the fully scoped name associated with PDI, from compilation unit
8850 CU. The result will be allocated with malloc. */
4568ecf9 8851
72bf9492
DJ
8852static char *
8853partial_die_full_name (struct partial_die_info *pdi,
8854 struct dwarf2_cu *cu)
8855{
15d034d0 8856 const char *parent_scope;
72bf9492 8857
98bfdba5
PA
8858 /* If this is a template instantiation, we can not work out the
8859 template arguments from partial DIEs. So, unfortunately, we have
8860 to go through the full DIEs. At least any work we do building
8861 types here will be reused if full symbols are loaded later. */
8862 if (pdi->has_template_arguments)
8863 {
52356b79 8864 pdi->fixup (cu);
98bfdba5
PA
8865
8866 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
8867 {
8868 struct die_info *die;
8869 struct attribute attr;
8870 struct dwarf2_cu *ref_cu = cu;
8871
b64f50a1 8872 /* DW_FORM_ref_addr is using section offset. */
b4069958 8873 attr.name = (enum dwarf_attribute) 0;
98bfdba5 8874 attr.form = DW_FORM_ref_addr;
9c541725 8875 attr.u.unsnd = to_underlying (pdi->sect_off);
98bfdba5
PA
8876 die = follow_die_ref (NULL, &attr, &ref_cu);
8877
8878 return xstrdup (dwarf2_full_name (NULL, die, ref_cu));
8879 }
8880 }
8881
72bf9492
DJ
8882 parent_scope = partial_die_parent_scope (pdi, cu);
8883 if (parent_scope == NULL)
8884 return NULL;
8885 else
f55ee35c 8886 return typename_concat (NULL, parent_scope, pdi->name, 0, cu);
c906108c
SS
8887}
8888
8889static void
72bf9492 8890add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
c906108c 8891{
518817b3
SM
8892 struct dwarf2_per_objfile *dwarf2_per_objfile
8893 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 8894 struct objfile *objfile = dwarf2_per_objfile->objfile;
3e29f34a 8895 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c 8896 CORE_ADDR addr = 0;
15d034d0 8897 const char *actual_name = NULL;
e142c38c 8898 CORE_ADDR baseaddr;
15d034d0 8899 char *built_actual_name;
e142c38c
DJ
8900
8901 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 8902
15d034d0
TT
8903 built_actual_name = partial_die_full_name (pdi, cu);
8904 if (built_actual_name != NULL)
8905 actual_name = built_actual_name;
63d06c5c 8906
72bf9492
DJ
8907 if (actual_name == NULL)
8908 actual_name = pdi->name;
8909
c906108c
SS
8910 switch (pdi->tag)
8911 {
b1dc1806 8912 case DW_TAG_inlined_subroutine:
c906108c 8913 case DW_TAG_subprogram:
79748972
TT
8914 addr = (gdbarch_adjust_dwarf2_addr (gdbarch, pdi->lowpc + baseaddr)
8915 - baseaddr);
2cfa0c8d 8916 if (pdi->is_external || cu->language == language_ada)
c906108c 8917 {
2cfa0c8d
JB
8918 /* brobecker/2007-12-26: Normally, only "external" DIEs are part
8919 of the global scope. But in Ada, we want to be able to access
8920 nested procedures globally. So all Ada subprograms are stored
8921 in the global scope. */
f47fb265 8922 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 8923 built_actual_name != NULL,
f47fb265 8924 VAR_DOMAIN, LOC_BLOCK,
79748972 8925 SECT_OFF_TEXT (objfile),
75aedd27 8926 psymbol_placement::GLOBAL,
79748972
TT
8927 addr,
8928 cu->language, objfile);
c906108c
SS
8929 }
8930 else
8931 {
f47fb265 8932 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 8933 built_actual_name != NULL,
f47fb265 8934 VAR_DOMAIN, LOC_BLOCK,
79748972 8935 SECT_OFF_TEXT (objfile),
75aedd27 8936 psymbol_placement::STATIC,
1762568f 8937 addr, cu->language, objfile);
c906108c 8938 }
0c1b455e
TT
8939
8940 if (pdi->main_subprogram && actual_name != NULL)
8941 set_objfile_main_name (objfile, actual_name, cu->language);
c906108c 8942 break;
72929c62 8943 case DW_TAG_constant:
75aedd27
TT
8944 add_psymbol_to_list (actual_name, strlen (actual_name),
8945 built_actual_name != NULL, VAR_DOMAIN, LOC_STATIC,
8946 -1, (pdi->is_external
8947 ? psymbol_placement::GLOBAL
8948 : psymbol_placement::STATIC),
8949 0, cu->language, objfile);
72929c62 8950 break;
c906108c 8951 case DW_TAG_variable:
95554aad
TT
8952 if (pdi->d.locdesc)
8953 addr = decode_locdesc (pdi->d.locdesc, cu);
caac4577 8954
95554aad 8955 if (pdi->d.locdesc
caac4577
JG
8956 && addr == 0
8957 && !dwarf2_per_objfile->has_section_at_zero)
8958 {
8959 /* A global or static variable may also have been stripped
8960 out by the linker if unused, in which case its address
8961 will be nullified; do not add such variables into partial
8962 symbol table then. */
8963 }
8964 else if (pdi->is_external)
c906108c
SS
8965 {
8966 /* Global Variable.
8967 Don't enter into the minimal symbol tables as there is
8968 a minimal symbol table entry from the ELF symbols already.
8969 Enter into partial symbol table if it has a location
8970 descriptor or a type.
8971 If the location descriptor is missing, new_symbol will create
8972 a LOC_UNRESOLVED symbol, the address of the variable will then
8973 be determined from the minimal symbol table whenever the variable
8974 is referenced.
8975 The address for the partial symbol table entry is not
8976 used by GDB, but it comes in handy for debugging partial symbol
8977 table building. */
8978
95554aad 8979 if (pdi->d.locdesc || pdi->has_type)
f47fb265 8980 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 8981 built_actual_name != NULL,
f47fb265 8982 VAR_DOMAIN, LOC_STATIC,
79748972 8983 SECT_OFF_TEXT (objfile),
75aedd27 8984 psymbol_placement::GLOBAL,
79748972 8985 addr, cu->language, objfile);
c906108c
SS
8986 }
8987 else
8988 {
ff908ebf
AW
8989 int has_loc = pdi->d.locdesc != NULL;
8990
8991 /* Static Variable. Skip symbols whose value we cannot know (those
8992 without location descriptors or constant values). */
8993 if (!has_loc && !pdi->has_const_value)
decbce07 8994 {
15d034d0 8995 xfree (built_actual_name);
decbce07
MS
8996 return;
8997 }
ff908ebf 8998
f47fb265 8999 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 9000 built_actual_name != NULL,
f47fb265 9001 VAR_DOMAIN, LOC_STATIC,
79748972 9002 SECT_OFF_TEXT (objfile),
75aedd27 9003 psymbol_placement::STATIC,
79748972 9004 has_loc ? addr : 0,
f47fb265 9005 cu->language, objfile);
c906108c
SS
9006 }
9007 break;
9008 case DW_TAG_typedef:
9009 case DW_TAG_base_type:
a02abb62 9010 case DW_TAG_subrange_type:
38d518c9 9011 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 9012 built_actual_name != NULL,
79748972 9013 VAR_DOMAIN, LOC_TYPEDEF, -1,
75aedd27 9014 psymbol_placement::STATIC,
1762568f 9015 0, cu->language, objfile);
c906108c 9016 break;
74921315 9017 case DW_TAG_imported_declaration:
72bf9492
DJ
9018 case DW_TAG_namespace:
9019 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 9020 built_actual_name != NULL,
79748972 9021 VAR_DOMAIN, LOC_TYPEDEF, -1,
75aedd27 9022 psymbol_placement::GLOBAL,
1762568f 9023 0, cu->language, objfile);
72bf9492 9024 break;
530e8392
KB
9025 case DW_TAG_module:
9026 add_psymbol_to_list (actual_name, strlen (actual_name),
9027 built_actual_name != NULL,
79748972 9028 MODULE_DOMAIN, LOC_TYPEDEF, -1,
75aedd27 9029 psymbol_placement::GLOBAL,
1762568f 9030 0, cu->language, objfile);
530e8392 9031 break;
c906108c 9032 case DW_TAG_class_type:
680b30c7 9033 case DW_TAG_interface_type:
c906108c
SS
9034 case DW_TAG_structure_type:
9035 case DW_TAG_union_type:
9036 case DW_TAG_enumeration_type:
fa4028e9
JB
9037 /* Skip external references. The DWARF standard says in the section
9038 about "Structure, Union, and Class Type Entries": "An incomplete
9039 structure, union or class type is represented by a structure,
9040 union or class entry that does not have a byte size attribute
9041 and that has a DW_AT_declaration attribute." */
9042 if (!pdi->has_byte_size && pdi->is_declaration)
decbce07 9043 {
15d034d0 9044 xfree (built_actual_name);
decbce07
MS
9045 return;
9046 }
fa4028e9 9047
63d06c5c
DC
9048 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
9049 static vs. global. */
38d518c9 9050 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 9051 built_actual_name != NULL,
79748972 9052 STRUCT_DOMAIN, LOC_TYPEDEF, -1,
9c37b5ae 9053 cu->language == language_cplus
75aedd27
TT
9054 ? psymbol_placement::GLOBAL
9055 : psymbol_placement::STATIC,
1762568f 9056 0, cu->language, objfile);
c906108c 9057
c906108c
SS
9058 break;
9059 case DW_TAG_enumerator:
38d518c9 9060 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 9061 built_actual_name != NULL,
79748972 9062 VAR_DOMAIN, LOC_CONST, -1,
9c37b5ae 9063 cu->language == language_cplus
75aedd27
TT
9064 ? psymbol_placement::GLOBAL
9065 : psymbol_placement::STATIC,
1762568f 9066 0, cu->language, objfile);
c906108c
SS
9067 break;
9068 default:
9069 break;
9070 }
5c4e30ca 9071
15d034d0 9072 xfree (built_actual_name);
c906108c
SS
9073}
9074
5c4e30ca
DC
9075/* Read a partial die corresponding to a namespace; also, add a symbol
9076 corresponding to that namespace to the symbol table. NAMESPACE is
9077 the name of the enclosing namespace. */
91c24f0a 9078
72bf9492
DJ
9079static void
9080add_partial_namespace (struct partial_die_info *pdi,
91c24f0a 9081 CORE_ADDR *lowpc, CORE_ADDR *highpc,
cdc07690 9082 int set_addrmap, struct dwarf2_cu *cu)
91c24f0a 9083{
72bf9492 9084 /* Add a symbol for the namespace. */
e7c27a73 9085
72bf9492 9086 add_partial_symbol (pdi, cu);
5c4e30ca
DC
9087
9088 /* Now scan partial symbols in that namespace. */
9089
91c24f0a 9090 if (pdi->has_children)
cdc07690 9091 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
91c24f0a
DC
9092}
9093
5d7cb8df
JK
9094/* Read a partial die corresponding to a Fortran module. */
9095
9096static void
9097add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
cdc07690 9098 CORE_ADDR *highpc, int set_addrmap, struct dwarf2_cu *cu)
5d7cb8df 9099{
530e8392
KB
9100 /* Add a symbol for the namespace. */
9101
9102 add_partial_symbol (pdi, cu);
9103
f55ee35c 9104 /* Now scan partial symbols in that module. */
5d7cb8df
JK
9105
9106 if (pdi->has_children)
cdc07690 9107 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
5d7cb8df
JK
9108}
9109
b1dc1806
XR
9110/* Read a partial die corresponding to a subprogram or an inlined
9111 subprogram and create a partial symbol for that subprogram.
9112 When the CU language allows it, this routine also defines a partial
9113 symbol for each nested subprogram that this subprogram contains.
9114 If SET_ADDRMAP is true, record the covered ranges in the addrmap.
9115 Set *LOWPC and *HIGHPC to the lowest and highest PC values found in PDI.
6e70227d 9116
cdc07690
YQ
9117 PDI may also be a lexical block, in which case we simply search
9118 recursively for subprograms defined inside that lexical block.
bc30ff58
JB
9119 Again, this is only performed when the CU language allows this
9120 type of definitions. */
9121
9122static void
9123add_partial_subprogram (struct partial_die_info *pdi,
9124 CORE_ADDR *lowpc, CORE_ADDR *highpc,
cdc07690 9125 int set_addrmap, struct dwarf2_cu *cu)
bc30ff58 9126{
b1dc1806 9127 if (pdi->tag == DW_TAG_subprogram || pdi->tag == DW_TAG_inlined_subroutine)
bc30ff58
JB
9128 {
9129 if (pdi->has_pc_info)
9130 {
9131 if (pdi->lowpc < *lowpc)
9132 *lowpc = pdi->lowpc;
9133 if (pdi->highpc > *highpc)
9134 *highpc = pdi->highpc;
cdc07690 9135 if (set_addrmap)
5734ee8b 9136 {
518817b3 9137 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
3e29f34a
MR
9138 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9139 CORE_ADDR baseaddr;
b926417a
TT
9140 CORE_ADDR this_highpc;
9141 CORE_ADDR this_lowpc;
5734ee8b
DJ
9142
9143 baseaddr = ANOFFSET (objfile->section_offsets,
9144 SECT_OFF_TEXT (objfile));
b926417a
TT
9145 this_lowpc
9146 = (gdbarch_adjust_dwarf2_addr (gdbarch,
9147 pdi->lowpc + baseaddr)
9148 - baseaddr);
9149 this_highpc
9150 = (gdbarch_adjust_dwarf2_addr (gdbarch,
9151 pdi->highpc + baseaddr)
9152 - baseaddr);
d320c2b5 9153 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
b926417a 9154 this_lowpc, this_highpc - 1,
9291a0cd 9155 cu->per_cu->v.psymtab);
5734ee8b 9156 }
481860b3
GB
9157 }
9158
9159 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
9160 {
bc30ff58 9161 if (!pdi->is_declaration)
e8d05480
JB
9162 /* Ignore subprogram DIEs that do not have a name, they are
9163 illegal. Do not emit a complaint at this point, we will
9164 do so when we convert this psymtab into a symtab. */
9165 if (pdi->name)
9166 add_partial_symbol (pdi, cu);
bc30ff58
JB
9167 }
9168 }
6e70227d 9169
bc30ff58
JB
9170 if (! pdi->has_children)
9171 return;
9172
9173 if (cu->language == language_ada)
9174 {
9175 pdi = pdi->die_child;
9176 while (pdi != NULL)
9177 {
52356b79 9178 pdi->fixup (cu);
bc30ff58 9179 if (pdi->tag == DW_TAG_subprogram
b1dc1806 9180 || pdi->tag == DW_TAG_inlined_subroutine
bc30ff58 9181 || pdi->tag == DW_TAG_lexical_block)
cdc07690 9182 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
bc30ff58
JB
9183 pdi = pdi->die_sibling;
9184 }
9185 }
9186}
9187
91c24f0a
DC
9188/* Read a partial die corresponding to an enumeration type. */
9189
72bf9492
DJ
9190static void
9191add_partial_enumeration (struct partial_die_info *enum_pdi,
9192 struct dwarf2_cu *cu)
91c24f0a 9193{
72bf9492 9194 struct partial_die_info *pdi;
91c24f0a
DC
9195
9196 if (enum_pdi->name != NULL)
72bf9492
DJ
9197 add_partial_symbol (enum_pdi, cu);
9198
9199 pdi = enum_pdi->die_child;
9200 while (pdi)
91c24f0a 9201 {
72bf9492 9202 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
b98664d3 9203 complaint (_("malformed enumerator DIE ignored"));
91c24f0a 9204 else
72bf9492
DJ
9205 add_partial_symbol (pdi, cu);
9206 pdi = pdi->die_sibling;
91c24f0a 9207 }
91c24f0a
DC
9208}
9209
6caca83c
CC
9210/* Return the initial uleb128 in the die at INFO_PTR. */
9211
9212static unsigned int
d521ce57 9213peek_abbrev_code (bfd *abfd, const gdb_byte *info_ptr)
6caca83c
CC
9214{
9215 unsigned int bytes_read;
9216
9217 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9218}
9219
685af9cd
TT
9220/* Read the initial uleb128 in the die at INFO_PTR in compilation unit
9221 READER::CU. Use READER::ABBREV_TABLE to lookup any abbreviation.
9222
4bb7a0a7
DJ
9223 Return the corresponding abbrev, or NULL if the number is zero (indicating
9224 an empty DIE). In either case *BYTES_READ will be set to the length of
9225 the initial number. */
9226
9227static struct abbrev_info *
685af9cd
TT
9228peek_die_abbrev (const die_reader_specs &reader,
9229 const gdb_byte *info_ptr, unsigned int *bytes_read)
4bb7a0a7 9230{
685af9cd 9231 dwarf2_cu *cu = reader.cu;
518817b3 9232 bfd *abfd = cu->per_cu->dwarf2_per_objfile->objfile->obfd;
685af9cd
TT
9233 unsigned int abbrev_number
9234 = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
4bb7a0a7
DJ
9235
9236 if (abbrev_number == 0)
9237 return NULL;
9238
685af9cd 9239 abbrev_info *abbrev = reader.abbrev_table->lookup_abbrev (abbrev_number);
4bb7a0a7
DJ
9240 if (!abbrev)
9241 {
422b9917 9242 error (_("Dwarf Error: Could not find abbrev number %d in %s"
9d8780f0 9243 " at offset %s [in module %s]"),
422b9917 9244 abbrev_number, cu->per_cu->is_debug_types ? "TU" : "CU",
9d8780f0 9245 sect_offset_str (cu->header.sect_off), bfd_get_filename (abfd));
4bb7a0a7
DJ
9246 }
9247
9248 return abbrev;
9249}
9250
93311388
DE
9251/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
9252 Returns a pointer to the end of a series of DIEs, terminated by an empty
4bb7a0a7
DJ
9253 DIE. Any children of the skipped DIEs will also be skipped. */
9254
d521ce57
TT
9255static const gdb_byte *
9256skip_children (const struct die_reader_specs *reader, const gdb_byte *info_ptr)
4bb7a0a7 9257{
4bb7a0a7
DJ
9258 while (1)
9259 {
685af9cd
TT
9260 unsigned int bytes_read;
9261 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
9262
4bb7a0a7
DJ
9263 if (abbrev == NULL)
9264 return info_ptr + bytes_read;
9265 else
dee91e82 9266 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
4bb7a0a7
DJ
9267 }
9268}
9269
93311388
DE
9270/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
9271 INFO_PTR should point just after the initial uleb128 of a DIE, and the
4bb7a0a7
DJ
9272 abbrev corresponding to that skipped uleb128 should be passed in
9273 ABBREV. Returns a pointer to this DIE's sibling, skipping any
9274 children. */
9275
d521ce57
TT
9276static const gdb_byte *
9277skip_one_die (const struct die_reader_specs *reader, const gdb_byte *info_ptr,
dee91e82 9278 struct abbrev_info *abbrev)
4bb7a0a7
DJ
9279{
9280 unsigned int bytes_read;
9281 struct attribute attr;
dee91e82
DE
9282 bfd *abfd = reader->abfd;
9283 struct dwarf2_cu *cu = reader->cu;
d521ce57 9284 const gdb_byte *buffer = reader->buffer;
f664829e 9285 const gdb_byte *buffer_end = reader->buffer_end;
4bb7a0a7
DJ
9286 unsigned int form, i;
9287
9288 for (i = 0; i < abbrev->num_attrs; i++)
9289 {
9290 /* The only abbrev we care about is DW_AT_sibling. */
9291 if (abbrev->attrs[i].name == DW_AT_sibling)
9292 {
dee91e82 9293 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
4bb7a0a7 9294 if (attr.form == DW_FORM_ref_addr)
b98664d3 9295 complaint (_("ignoring absolute DW_AT_sibling"));
4bb7a0a7 9296 else
b9502d3f 9297 {
9c541725
PA
9298 sect_offset off = dwarf2_get_ref_die_offset (&attr);
9299 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
b9502d3f
WN
9300
9301 if (sibling_ptr < info_ptr)
b98664d3 9302 complaint (_("DW_AT_sibling points backwards"));
22869d73
KS
9303 else if (sibling_ptr > reader->buffer_end)
9304 dwarf2_section_buffer_overflow_complaint (reader->die_section);
b9502d3f
WN
9305 else
9306 return sibling_ptr;
9307 }
4bb7a0a7
DJ
9308 }
9309
9310 /* If it isn't DW_AT_sibling, skip this attribute. */
9311 form = abbrev->attrs[i].form;
9312 skip_attribute:
9313 switch (form)
9314 {
4bb7a0a7 9315 case DW_FORM_ref_addr:
ae411497
TT
9316 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
9317 and later it is offset sized. */
9318 if (cu->header.version == 2)
9319 info_ptr += cu->header.addr_size;
9320 else
9321 info_ptr += cu->header.offset_size;
9322 break;
36586728
TT
9323 case DW_FORM_GNU_ref_alt:
9324 info_ptr += cu->header.offset_size;
9325 break;
ae411497 9326 case DW_FORM_addr:
4bb7a0a7
DJ
9327 info_ptr += cu->header.addr_size;
9328 break;
9329 case DW_FORM_data1:
9330 case DW_FORM_ref1:
9331 case DW_FORM_flag:
9332 info_ptr += 1;
9333 break;
2dc7f7b3 9334 case DW_FORM_flag_present:
43988095 9335 case DW_FORM_implicit_const:
2dc7f7b3 9336 break;
4bb7a0a7
DJ
9337 case DW_FORM_data2:
9338 case DW_FORM_ref2:
9339 info_ptr += 2;
9340 break;
9341 case DW_FORM_data4:
9342 case DW_FORM_ref4:
9343 info_ptr += 4;
9344 break;
9345 case DW_FORM_data8:
9346 case DW_FORM_ref8:
55f1336d 9347 case DW_FORM_ref_sig8:
4bb7a0a7
DJ
9348 info_ptr += 8;
9349 break;
0224619f
JK
9350 case DW_FORM_data16:
9351 info_ptr += 16;
9352 break;
4bb7a0a7 9353 case DW_FORM_string:
9b1c24c8 9354 read_direct_string (abfd, info_ptr, &bytes_read);
4bb7a0a7
DJ
9355 info_ptr += bytes_read;
9356 break;
2dc7f7b3 9357 case DW_FORM_sec_offset:
4bb7a0a7 9358 case DW_FORM_strp:
36586728 9359 case DW_FORM_GNU_strp_alt:
4bb7a0a7
DJ
9360 info_ptr += cu->header.offset_size;
9361 break;
2dc7f7b3 9362 case DW_FORM_exprloc:
4bb7a0a7
DJ
9363 case DW_FORM_block:
9364 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9365 info_ptr += bytes_read;
9366 break;
9367 case DW_FORM_block1:
9368 info_ptr += 1 + read_1_byte (abfd, info_ptr);
9369 break;
9370 case DW_FORM_block2:
9371 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
9372 break;
9373 case DW_FORM_block4:
9374 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
9375 break;
9376 case DW_FORM_sdata:
9377 case DW_FORM_udata:
9378 case DW_FORM_ref_udata:
3019eac3
DE
9379 case DW_FORM_GNU_addr_index:
9380 case DW_FORM_GNU_str_index:
d521ce57 9381 info_ptr = safe_skip_leb128 (info_ptr, buffer_end);
4bb7a0a7
DJ
9382 break;
9383 case DW_FORM_indirect:
9384 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9385 info_ptr += bytes_read;
9386 /* We need to continue parsing from here, so just go back to
9387 the top. */
9388 goto skip_attribute;
9389
9390 default:
3e43a32a
MS
9391 error (_("Dwarf Error: Cannot handle %s "
9392 "in DWARF reader [in module %s]"),
4bb7a0a7
DJ
9393 dwarf_form_name (form),
9394 bfd_get_filename (abfd));
9395 }
9396 }
9397
9398 if (abbrev->has_children)
dee91e82 9399 return skip_children (reader, info_ptr);
4bb7a0a7
DJ
9400 else
9401 return info_ptr;
9402}
9403
93311388 9404/* Locate ORIG_PDI's sibling.
dee91e82 9405 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
91c24f0a 9406
d521ce57 9407static const gdb_byte *
dee91e82
DE
9408locate_pdi_sibling (const struct die_reader_specs *reader,
9409 struct partial_die_info *orig_pdi,
d521ce57 9410 const gdb_byte *info_ptr)
91c24f0a
DC
9411{
9412 /* Do we know the sibling already? */
72bf9492 9413
91c24f0a
DC
9414 if (orig_pdi->sibling)
9415 return orig_pdi->sibling;
9416
9417 /* Are there any children to deal with? */
9418
9419 if (!orig_pdi->has_children)
9420 return info_ptr;
9421
4bb7a0a7 9422 /* Skip the children the long way. */
91c24f0a 9423
dee91e82 9424 return skip_children (reader, info_ptr);
91c24f0a
DC
9425}
9426
257e7a09 9427/* Expand this partial symbol table into a full symbol table. SELF is
442e4d9c 9428 not NULL. */
c906108c
SS
9429
9430static void
257e7a09
YQ
9431dwarf2_read_symtab (struct partial_symtab *self,
9432 struct objfile *objfile)
c906108c 9433{
ed2dc618
SM
9434 struct dwarf2_per_objfile *dwarf2_per_objfile
9435 = get_dwarf2_per_objfile (objfile);
9436
257e7a09 9437 if (self->readin)
c906108c 9438 {
442e4d9c 9439 warning (_("bug: psymtab for %s is already read in."),
257e7a09 9440 self->filename);
442e4d9c
YQ
9441 }
9442 else
9443 {
9444 if (info_verbose)
c906108c 9445 {
442e4d9c 9446 printf_filtered (_("Reading in symbols for %s..."),
257e7a09 9447 self->filename);
442e4d9c 9448 gdb_flush (gdb_stdout);
c906108c 9449 }
c906108c 9450
442e4d9c
YQ
9451 /* If this psymtab is constructed from a debug-only objfile, the
9452 has_section_at_zero flag will not necessarily be correct. We
9453 can get the correct value for this flag by looking at the data
9454 associated with the (presumably stripped) associated objfile. */
9455 if (objfile->separate_debug_objfile_backlink)
9456 {
9457 struct dwarf2_per_objfile *dpo_backlink
ed2dc618 9458 = get_dwarf2_per_objfile (objfile->separate_debug_objfile_backlink);
9a619af0 9459
442e4d9c
YQ
9460 dwarf2_per_objfile->has_section_at_zero
9461 = dpo_backlink->has_section_at_zero;
9462 }
b2ab525c 9463
442e4d9c 9464 dwarf2_per_objfile->reading_partial_symbols = 0;
98bfdba5 9465
257e7a09 9466 psymtab_to_symtab_1 (self);
c906108c 9467
442e4d9c
YQ
9468 /* Finish up the debug error message. */
9469 if (info_verbose)
9470 printf_filtered (_("done.\n"));
c906108c 9471 }
95554aad 9472
ed2dc618 9473 process_cu_includes (dwarf2_per_objfile);
c906108c 9474}
9cdd5dbd
DE
9475\f
9476/* Reading in full CUs. */
c906108c 9477
10b3939b
DJ
9478/* Add PER_CU to the queue. */
9479
9480static void
95554aad
TT
9481queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
9482 enum language pretend_language)
10b3939b
DJ
9483{
9484 struct dwarf2_queue_item *item;
9485
9486 per_cu->queued = 1;
8d749320 9487 item = XNEW (struct dwarf2_queue_item);
10b3939b 9488 item->per_cu = per_cu;
95554aad 9489 item->pretend_language = pretend_language;
10b3939b
DJ
9490 item->next = NULL;
9491
9492 if (dwarf2_queue == NULL)
9493 dwarf2_queue = item;
9494 else
9495 dwarf2_queue_tail->next = item;
9496
9497 dwarf2_queue_tail = item;
9498}
9499
89e63ee4
DE
9500/* If PER_CU is not yet queued, add it to the queue.
9501 If DEPENDENT_CU is non-NULL, it has a reference to PER_CU so add a
9502 dependency.
0907af0c 9503 The result is non-zero if PER_CU was queued, otherwise the result is zero
69d751e3
DE
9504 meaning either PER_CU is already queued or it is already loaded.
9505
9506 N.B. There is an invariant here that if a CU is queued then it is loaded.
9507 The caller is required to load PER_CU if we return non-zero. */
0907af0c
DE
9508
9509static int
89e63ee4 9510maybe_queue_comp_unit (struct dwarf2_cu *dependent_cu,
0907af0c
DE
9511 struct dwarf2_per_cu_data *per_cu,
9512 enum language pretend_language)
9513{
9514 /* We may arrive here during partial symbol reading, if we need full
9515 DIEs to process an unusual case (e.g. template arguments). Do
9516 not queue PER_CU, just tell our caller to load its DIEs. */
ed2dc618 9517 if (per_cu->dwarf2_per_objfile->reading_partial_symbols)
0907af0c
DE
9518 {
9519 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
9520 return 1;
9521 return 0;
9522 }
9523
9524 /* Mark the dependence relation so that we don't flush PER_CU
9525 too early. */
89e63ee4
DE
9526 if (dependent_cu != NULL)
9527 dwarf2_add_dependence (dependent_cu, per_cu);
0907af0c
DE
9528
9529 /* If it's already on the queue, we have nothing to do. */
9530 if (per_cu->queued)
9531 return 0;
9532
9533 /* If the compilation unit is already loaded, just mark it as
9534 used. */
9535 if (per_cu->cu != NULL)
9536 {
9537 per_cu->cu->last_used = 0;
9538 return 0;
9539 }
9540
9541 /* Add it to the queue. */
9542 queue_comp_unit (per_cu, pretend_language);
9543
9544 return 1;
9545}
9546
10b3939b
DJ
9547/* Process the queue. */
9548
9549static void
ed2dc618 9550process_queue (struct dwarf2_per_objfile *dwarf2_per_objfile)
10b3939b
DJ
9551{
9552 struct dwarf2_queue_item *item, *next_item;
9553
b4f54984 9554 if (dwarf_read_debug)
45cfd468
DE
9555 {
9556 fprintf_unfiltered (gdb_stdlog,
9557 "Expanding one or more symtabs of objfile %s ...\n",
4262abfb 9558 objfile_name (dwarf2_per_objfile->objfile));
45cfd468
DE
9559 }
9560
03dd20cc
DJ
9561 /* The queue starts out with one item, but following a DIE reference
9562 may load a new CU, adding it to the end of the queue. */
10b3939b
DJ
9563 for (item = dwarf2_queue; item != NULL; dwarf2_queue = item = next_item)
9564 {
cc12ce38
DE
9565 if ((dwarf2_per_objfile->using_index
9566 ? !item->per_cu->v.quick->compunit_symtab
9567 : (item->per_cu->v.psymtab && !item->per_cu->v.psymtab->readin))
9568 /* Skip dummy CUs. */
9569 && item->per_cu->cu != NULL)
f4dc4d17
DE
9570 {
9571 struct dwarf2_per_cu_data *per_cu = item->per_cu;
73be47f5 9572 unsigned int debug_print_threshold;
247f5c4f 9573 char buf[100];
f4dc4d17 9574
247f5c4f 9575 if (per_cu->is_debug_types)
f4dc4d17 9576 {
247f5c4f
DE
9577 struct signatured_type *sig_type =
9578 (struct signatured_type *) per_cu;
9579
9d8780f0 9580 sprintf (buf, "TU %s at offset %s",
73be47f5 9581 hex_string (sig_type->signature),
9d8780f0 9582 sect_offset_str (per_cu->sect_off));
73be47f5
DE
9583 /* There can be 100s of TUs.
9584 Only print them in verbose mode. */
9585 debug_print_threshold = 2;
f4dc4d17 9586 }
247f5c4f 9587 else
73be47f5 9588 {
9d8780f0
SM
9589 sprintf (buf, "CU at offset %s",
9590 sect_offset_str (per_cu->sect_off));
73be47f5
DE
9591 debug_print_threshold = 1;
9592 }
247f5c4f 9593
b4f54984 9594 if (dwarf_read_debug >= debug_print_threshold)
247f5c4f 9595 fprintf_unfiltered (gdb_stdlog, "Expanding symtab of %s\n", buf);
f4dc4d17
DE
9596
9597 if (per_cu->is_debug_types)
9598 process_full_type_unit (per_cu, item->pretend_language);
9599 else
9600 process_full_comp_unit (per_cu, item->pretend_language);
9601
b4f54984 9602 if (dwarf_read_debug >= debug_print_threshold)
247f5c4f 9603 fprintf_unfiltered (gdb_stdlog, "Done expanding %s\n", buf);
f4dc4d17 9604 }
10b3939b
DJ
9605
9606 item->per_cu->queued = 0;
9607 next_item = item->next;
9608 xfree (item);
9609 }
9610
9611 dwarf2_queue_tail = NULL;
45cfd468 9612
b4f54984 9613 if (dwarf_read_debug)
45cfd468
DE
9614 {
9615 fprintf_unfiltered (gdb_stdlog, "Done expanding symtabs of %s.\n",
4262abfb 9616 objfile_name (dwarf2_per_objfile->objfile));
45cfd468 9617 }
10b3939b
DJ
9618}
9619
10b3939b
DJ
9620/* Read in full symbols for PST, and anything it depends on. */
9621
c906108c 9622static void
fba45db2 9623psymtab_to_symtab_1 (struct partial_symtab *pst)
c906108c 9624{
10b3939b 9625 struct dwarf2_per_cu_data *per_cu;
aaa75496
JB
9626 int i;
9627
95554aad
TT
9628 if (pst->readin)
9629 return;
9630
aaa75496 9631 for (i = 0; i < pst->number_of_dependencies; i++)
95554aad
TT
9632 if (!pst->dependencies[i]->readin
9633 && pst->dependencies[i]->user == NULL)
aaa75496
JB
9634 {
9635 /* Inform about additional files that need to be read in. */
9636 if (info_verbose)
9637 {
a3f17187 9638 /* FIXME: i18n: Need to make this a single string. */
aaa75496
JB
9639 fputs_filtered (" ", gdb_stdout);
9640 wrap_here ("");
9641 fputs_filtered ("and ", gdb_stdout);
9642 wrap_here ("");
9643 printf_filtered ("%s...", pst->dependencies[i]->filename);
0963b4bd 9644 wrap_here (""); /* Flush output. */
aaa75496
JB
9645 gdb_flush (gdb_stdout);
9646 }
9647 psymtab_to_symtab_1 (pst->dependencies[i]);
9648 }
9649
9a3c8263 9650 per_cu = (struct dwarf2_per_cu_data *) pst->read_symtab_private;
10b3939b
DJ
9651
9652 if (per_cu == NULL)
aaa75496
JB
9653 {
9654 /* It's an include file, no symbols to read for it.
9655 Everything is in the parent symtab. */
9656 pst->readin = 1;
9657 return;
9658 }
c906108c 9659
58f0c718 9660 dw2_do_instantiate_symtab (per_cu, false);
10b3939b
DJ
9661}
9662
dee91e82
DE
9663/* Trivial hash function for die_info: the hash value of a DIE
9664 is its offset in .debug_info for this objfile. */
10b3939b 9665
dee91e82
DE
9666static hashval_t
9667die_hash (const void *item)
10b3939b 9668{
9a3c8263 9669 const struct die_info *die = (const struct die_info *) item;
6502dd73 9670
9c541725 9671 return to_underlying (die->sect_off);
dee91e82 9672}
63d06c5c 9673
dee91e82
DE
9674/* Trivial comparison function for die_info structures: two DIEs
9675 are equal if they have the same offset. */
98bfdba5 9676
dee91e82
DE
9677static int
9678die_eq (const void *item_lhs, const void *item_rhs)
9679{
9a3c8263
SM
9680 const struct die_info *die_lhs = (const struct die_info *) item_lhs;
9681 const struct die_info *die_rhs = (const struct die_info *) item_rhs;
c906108c 9682
9c541725 9683 return die_lhs->sect_off == die_rhs->sect_off;
dee91e82 9684}
c906108c 9685
dee91e82
DE
9686/* die_reader_func for load_full_comp_unit.
9687 This is identical to read_signatured_type_reader,
9688 but is kept separate for now. */
c906108c 9689
dee91e82
DE
9690static void
9691load_full_comp_unit_reader (const struct die_reader_specs *reader,
d521ce57 9692 const gdb_byte *info_ptr,
dee91e82
DE
9693 struct die_info *comp_unit_die,
9694 int has_children,
9695 void *data)
9696{
9697 struct dwarf2_cu *cu = reader->cu;
9a3c8263 9698 enum language *language_ptr = (enum language *) data;
6caca83c 9699
dee91e82
DE
9700 gdb_assert (cu->die_hash == NULL);
9701 cu->die_hash =
9702 htab_create_alloc_ex (cu->header.length / 12,
9703 die_hash,
9704 die_eq,
9705 NULL,
9706 &cu->comp_unit_obstack,
9707 hashtab_obstack_allocate,
9708 dummy_obstack_deallocate);
e142c38c 9709
dee91e82
DE
9710 if (has_children)
9711 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
9712 &info_ptr, comp_unit_die);
9713 cu->dies = comp_unit_die;
9714 /* comp_unit_die is not stored in die_hash, no need. */
10b3939b
DJ
9715
9716 /* We try not to read any attributes in this function, because not
9cdd5dbd 9717 all CUs needed for references have been loaded yet, and symbol
10b3939b 9718 table processing isn't initialized. But we have to set the CU language,
dee91e82
DE
9719 or we won't be able to build types correctly.
9720 Similarly, if we do not read the producer, we can not apply
9721 producer-specific interpretation. */
95554aad 9722 prepare_one_comp_unit (cu, cu->dies, *language_ptr);
dee91e82 9723}
10b3939b 9724
dee91e82 9725/* Load the DIEs associated with PER_CU into memory. */
a6c727b2 9726
dee91e82 9727static void
95554aad 9728load_full_comp_unit (struct dwarf2_per_cu_data *this_cu,
58f0c718 9729 bool skip_partial,
95554aad 9730 enum language pretend_language)
dee91e82 9731{
3019eac3 9732 gdb_assert (! this_cu->is_debug_types);
c5b7e1cb 9733
58f0c718 9734 init_cutu_and_read_dies (this_cu, NULL, 1, 1, skip_partial,
f4dc4d17 9735 load_full_comp_unit_reader, &pretend_language);
10b3939b
DJ
9736}
9737
3da10d80
KS
9738/* Add a DIE to the delayed physname list. */
9739
9740static void
9741add_to_method_list (struct type *type, int fnfield_index, int index,
9742 const char *name, struct die_info *die,
9743 struct dwarf2_cu *cu)
9744{
9745 struct delayed_method_info mi;
9746 mi.type = type;
9747 mi.fnfield_index = fnfield_index;
9748 mi.index = index;
9749 mi.name = name;
9750 mi.die = die;
c89b44cd 9751 cu->method_list.push_back (mi);
3da10d80
KS
9752}
9753
3693fdb3
PA
9754/* Check whether [PHYSNAME, PHYSNAME+LEN) ends with a modifier like
9755 "const" / "volatile". If so, decrements LEN by the length of the
9756 modifier and return true. Otherwise return false. */
9757
9758template<size_t N>
9759static bool
9760check_modifier (const char *physname, size_t &len, const char (&mod)[N])
9761{
9762 size_t mod_len = sizeof (mod) - 1;
9763 if (len > mod_len && startswith (physname + (len - mod_len), mod))
9764 {
9765 len -= mod_len;
9766 return true;
9767 }
9768 return false;
9769}
9770
3da10d80
KS
9771/* Compute the physnames of any methods on the CU's method list.
9772
9773 The computation of method physnames is delayed in order to avoid the
9774 (bad) condition that one of the method's formal parameters is of an as yet
9775 incomplete type. */
9776
9777static void
9778compute_delayed_physnames (struct dwarf2_cu *cu)
9779{
3693fdb3 9780 /* Only C++ delays computing physnames. */
c89b44cd 9781 if (cu->method_list.empty ())
3693fdb3
PA
9782 return;
9783 gdb_assert (cu->language == language_cplus);
9784
52941706 9785 for (const delayed_method_info &mi : cu->method_list)
3da10d80 9786 {
1d06ead6 9787 const char *physname;
3da10d80 9788 struct fn_fieldlist *fn_flp
c89b44cd
TT
9789 = &TYPE_FN_FIELDLIST (mi.type, mi.fnfield_index);
9790 physname = dwarf2_physname (mi.name, mi.die, cu);
9791 TYPE_FN_FIELD_PHYSNAME (fn_flp->fn_fields, mi.index)
005e54bb 9792 = physname ? physname : "";
3693fdb3
PA
9793
9794 /* Since there's no tag to indicate whether a method is a
9795 const/volatile overload, extract that information out of the
9796 demangled name. */
9797 if (physname != NULL)
9798 {
9799 size_t len = strlen (physname);
9800
9801 while (1)
9802 {
9803 if (physname[len] == ')') /* shortcut */
9804 break;
9805 else if (check_modifier (physname, len, " const"))
c89b44cd 9806 TYPE_FN_FIELD_CONST (fn_flp->fn_fields, mi.index) = 1;
3693fdb3 9807 else if (check_modifier (physname, len, " volatile"))
c89b44cd 9808 TYPE_FN_FIELD_VOLATILE (fn_flp->fn_fields, mi.index) = 1;
3693fdb3
PA
9809 else
9810 break;
9811 }
9812 }
3da10d80 9813 }
c89b44cd
TT
9814
9815 /* The list is no longer needed. */
9816 cu->method_list.clear ();
3da10d80
KS
9817}
9818
a766d390
DE
9819/* Go objects should be embedded in a DW_TAG_module DIE,
9820 and it's not clear if/how imported objects will appear.
9821 To keep Go support simple until that's worked out,
9822 go back through what we've read and create something usable.
9823 We could do this while processing each DIE, and feels kinda cleaner,
9824 but that way is more invasive.
9825 This is to, for example, allow the user to type "p var" or "b main"
9826 without having to specify the package name, and allow lookups
9827 of module.object to work in contexts that use the expression
9828 parser. */
9829
9830static void
9831fixup_go_packaging (struct dwarf2_cu *cu)
9832{
9833 char *package_name = NULL;
9834 struct pending *list;
9835 int i;
9836
c24bdb02 9837 for (list = *cu->get_builder ()->get_global_symbols ();
804d2729
TT
9838 list != NULL;
9839 list = list->next)
a766d390
DE
9840 {
9841 for (i = 0; i < list->nsyms; ++i)
9842 {
9843 struct symbol *sym = list->symbol[i];
9844
9845 if (SYMBOL_LANGUAGE (sym) == language_go
9846 && SYMBOL_CLASS (sym) == LOC_BLOCK)
9847 {
9848 char *this_package_name = go_symbol_package_name (sym);
9849
9850 if (this_package_name == NULL)
9851 continue;
9852 if (package_name == NULL)
9853 package_name = this_package_name;
9854 else
9855 {
518817b3
SM
9856 struct objfile *objfile
9857 = cu->per_cu->dwarf2_per_objfile->objfile;
a766d390 9858 if (strcmp (package_name, this_package_name) != 0)
b98664d3 9859 complaint (_("Symtab %s has objects from two different Go packages: %s and %s"),
08be3fe3
DE
9860 (symbol_symtab (sym) != NULL
9861 ? symtab_to_filename_for_display
9862 (symbol_symtab (sym))
e3b94546 9863 : objfile_name (objfile)),
a766d390
DE
9864 this_package_name, package_name);
9865 xfree (this_package_name);
9866 }
9867 }
9868 }
9869 }
9870
9871 if (package_name != NULL)
9872 {
518817b3 9873 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
34a68019 9874 const char *saved_package_name
224c3ddb
SM
9875 = (const char *) obstack_copy0 (&objfile->per_bfd->storage_obstack,
9876 package_name,
9877 strlen (package_name));
19f392bc
UW
9878 struct type *type = init_type (objfile, TYPE_CODE_MODULE, 0,
9879 saved_package_name);
a766d390
DE
9880 struct symbol *sym;
9881
e623cf5d 9882 sym = allocate_symbol (objfile);
f85f34ed 9883 SYMBOL_SET_LANGUAGE (sym, language_go, &objfile->objfile_obstack);
86f62fd7
TT
9884 SYMBOL_SET_NAMES (sym, saved_package_name,
9885 strlen (saved_package_name), 0, objfile);
a766d390
DE
9886 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
9887 e.g., "main" finds the "main" module and not C's main(). */
9888 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
f1e6e072 9889 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
a766d390
DE
9890 SYMBOL_TYPE (sym) = type;
9891
c24bdb02 9892 add_symbol_to_list (sym, cu->get_builder ()->get_global_symbols ());
a766d390
DE
9893
9894 xfree (package_name);
9895 }
9896}
9897
c9317f21
TT
9898/* Allocate a fully-qualified name consisting of the two parts on the
9899 obstack. */
9900
9901static const char *
9902rust_fully_qualify (struct obstack *obstack, const char *p1, const char *p2)
9903{
9904 return obconcat (obstack, p1, "::", p2, (char *) NULL);
9905}
9906
9907/* A helper that allocates a struct discriminant_info to attach to a
9908 union type. */
9909
9910static struct discriminant_info *
9911alloc_discriminant_info (struct type *type, int discriminant_index,
9912 int default_index)
9913{
9914 gdb_assert (TYPE_CODE (type) == TYPE_CODE_UNION);
c7b15a66
TT
9915 gdb_assert (discriminant_index == -1
9916 || (discriminant_index >= 0
9917 && discriminant_index < TYPE_NFIELDS (type)));
c9317f21 9918 gdb_assert (default_index == -1
c7b15a66 9919 || (default_index >= 0 && default_index < TYPE_NFIELDS (type)));
c9317f21
TT
9920
9921 TYPE_FLAG_DISCRIMINATED_UNION (type) = 1;
9922
9923 struct discriminant_info *disc
9924 = ((struct discriminant_info *)
9925 TYPE_ZALLOC (type,
9926 offsetof (struct discriminant_info, discriminants)
9927 + TYPE_NFIELDS (type) * sizeof (disc->discriminants[0])));
9928 disc->default_index = default_index;
9929 disc->discriminant_index = discriminant_index;
9930
9931 struct dynamic_prop prop;
9932 prop.kind = PROP_UNDEFINED;
9933 prop.data.baton = disc;
9934
9935 add_dyn_prop (DYN_PROP_DISCRIMINATED, prop, type);
9936
9937 return disc;
9938}
9939
9940/* Some versions of rustc emitted enums in an unusual way.
9941
9942 Ordinary enums were emitted as unions. The first element of each
9943 structure in the union was named "RUST$ENUM$DISR". This element
9944 held the discriminant.
9945
9946 These versions of Rust also implemented the "non-zero"
9947 optimization. When the enum had two values, and one is empty and
9948 the other holds a pointer that cannot be zero, the pointer is used
9949 as the discriminant, with a zero value meaning the empty variant.
9950 Here, the union's first member is of the form
9951 RUST$ENCODED$ENUM$<fieldno>$<fieldno>$...$<variantname>
9952 where the fieldnos are the indices of the fields that should be
9953 traversed in order to find the field (which may be several fields deep)
9954 and the variantname is the name of the variant of the case when the
9955 field is zero.
9956
9957 This function recognizes whether TYPE is of one of these forms,
9958 and, if so, smashes it to be a variant type. */
9959
9960static void
9961quirk_rust_enum (struct type *type, struct objfile *objfile)
9962{
9963 gdb_assert (TYPE_CODE (type) == TYPE_CODE_UNION);
9964
9965 /* We don't need to deal with empty enums. */
9966 if (TYPE_NFIELDS (type) == 0)
9967 return;
9968
9969#define RUST_ENUM_PREFIX "RUST$ENCODED$ENUM$"
9970 if (TYPE_NFIELDS (type) == 1
9971 && startswith (TYPE_FIELD_NAME (type, 0), RUST_ENUM_PREFIX))
9972 {
9973 const char *name = TYPE_FIELD_NAME (type, 0) + strlen (RUST_ENUM_PREFIX);
9974
9975 /* Decode the field name to find the offset of the
9976 discriminant. */
9977 ULONGEST bit_offset = 0;
9978 struct type *field_type = TYPE_FIELD_TYPE (type, 0);
9979 while (name[0] >= '0' && name[0] <= '9')
9980 {
9981 char *tail;
9982 unsigned long index = strtoul (name, &tail, 10);
9983 name = tail;
9984 if (*name != '$'
9985 || index >= TYPE_NFIELDS (field_type)
9986 || (TYPE_FIELD_LOC_KIND (field_type, index)
9987 != FIELD_LOC_KIND_BITPOS))
9988 {
b98664d3 9989 complaint (_("Could not parse Rust enum encoding string \"%s\""
c9317f21
TT
9990 "[in module %s]"),
9991 TYPE_FIELD_NAME (type, 0),
9992 objfile_name (objfile));
9993 return;
9994 }
9995 ++name;
9996
9997 bit_offset += TYPE_FIELD_BITPOS (field_type, index);
9998 field_type = TYPE_FIELD_TYPE (field_type, index);
9999 }
10000
10001 /* Make a union to hold the variants. */
10002 struct type *union_type = alloc_type (objfile);
10003 TYPE_CODE (union_type) = TYPE_CODE_UNION;
10004 TYPE_NFIELDS (union_type) = 3;
10005 TYPE_FIELDS (union_type)
10006 = (struct field *) TYPE_ZALLOC (type, 3 * sizeof (struct field));
10007 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
2b4424c3 10008 set_type_align (union_type, TYPE_RAW_ALIGN (type));
c9317f21
TT
10009
10010 /* Put the discriminant must at index 0. */
10011 TYPE_FIELD_TYPE (union_type, 0) = field_type;
10012 TYPE_FIELD_ARTIFICIAL (union_type, 0) = 1;
10013 TYPE_FIELD_NAME (union_type, 0) = "<<discriminant>>";
10014 SET_FIELD_BITPOS (TYPE_FIELD (union_type, 0), bit_offset);
10015
10016 /* The order of fields doesn't really matter, so put the real
10017 field at index 1 and the data-less field at index 2. */
10018 struct discriminant_info *disc
10019 = alloc_discriminant_info (union_type, 0, 1);
10020 TYPE_FIELD (union_type, 1) = TYPE_FIELD (type, 0);
10021 TYPE_FIELD_NAME (union_type, 1)
10022 = rust_last_path_segment (TYPE_NAME (TYPE_FIELD_TYPE (union_type, 1)));
10023 TYPE_NAME (TYPE_FIELD_TYPE (union_type, 1))
10024 = rust_fully_qualify (&objfile->objfile_obstack, TYPE_NAME (type),
10025 TYPE_FIELD_NAME (union_type, 1));
10026
10027 const char *dataless_name
10028 = rust_fully_qualify (&objfile->objfile_obstack, TYPE_NAME (type),
10029 name);
10030 struct type *dataless_type = init_type (objfile, TYPE_CODE_VOID, 0,
10031 dataless_name);
10032 TYPE_FIELD_TYPE (union_type, 2) = dataless_type;
10033 /* NAME points into the original discriminant name, which
10034 already has the correct lifetime. */
10035 TYPE_FIELD_NAME (union_type, 2) = name;
10036 SET_FIELD_BITPOS (TYPE_FIELD (union_type, 2), 0);
10037 disc->discriminants[2] = 0;
10038
10039 /* Smash this type to be a structure type. We have to do this
10040 because the type has already been recorded. */
10041 TYPE_CODE (type) = TYPE_CODE_STRUCT;
10042 TYPE_NFIELDS (type) = 1;
10043 TYPE_FIELDS (type)
10044 = (struct field *) TYPE_ZALLOC (type, sizeof (struct field));
10045
10046 /* Install the variant part. */
10047 TYPE_FIELD_TYPE (type, 0) = union_type;
10048 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
10049 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
10050 }
10051 else if (TYPE_NFIELDS (type) == 1)
10052 {
10053 /* We assume that a union with a single field is a univariant
10054 enum. */
10055 /* Smash this type to be a structure type. We have to do this
10056 because the type has already been recorded. */
10057 TYPE_CODE (type) = TYPE_CODE_STRUCT;
10058
10059 /* Make a union to hold the variants. */
10060 struct type *union_type = alloc_type (objfile);
10061 TYPE_CODE (union_type) = TYPE_CODE_UNION;
10062 TYPE_NFIELDS (union_type) = TYPE_NFIELDS (type);
10063 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
2b4424c3 10064 set_type_align (union_type, TYPE_RAW_ALIGN (type));
c9317f21
TT
10065 TYPE_FIELDS (union_type) = TYPE_FIELDS (type);
10066
10067 struct type *field_type = TYPE_FIELD_TYPE (union_type, 0);
10068 const char *variant_name
10069 = rust_last_path_segment (TYPE_NAME (field_type));
10070 TYPE_FIELD_NAME (union_type, 0) = variant_name;
10071 TYPE_NAME (field_type)
10072 = rust_fully_qualify (&objfile->objfile_obstack,
c7b15a66 10073 TYPE_NAME (type), variant_name);
c9317f21
TT
10074
10075 /* Install the union in the outer struct type. */
10076 TYPE_NFIELDS (type) = 1;
10077 TYPE_FIELDS (type)
10078 = (struct field *) TYPE_ZALLOC (union_type, sizeof (struct field));
10079 TYPE_FIELD_TYPE (type, 0) = union_type;
10080 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
10081 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
10082
10083 alloc_discriminant_info (union_type, -1, 0);
10084 }
10085 else
10086 {
10087 struct type *disr_type = nullptr;
10088 for (int i = 0; i < TYPE_NFIELDS (type); ++i)
10089 {
10090 disr_type = TYPE_FIELD_TYPE (type, i);
10091
a037790e
TT
10092 if (TYPE_CODE (disr_type) != TYPE_CODE_STRUCT)
10093 {
10094 /* All fields of a true enum will be structs. */
10095 return;
10096 }
10097 else if (TYPE_NFIELDS (disr_type) == 0)
c9317f21
TT
10098 {
10099 /* Could be data-less variant, so keep going. */
a037790e 10100 disr_type = nullptr;
c9317f21
TT
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);
2b4424c3 10130 set_type_align (union_type, TYPE_RAW_ALIGN (type));
c9317f21
TT
10131 TYPE_FIELDS (union_type)
10132 = (struct field *) TYPE_ZALLOC (union_type,
10133 (TYPE_NFIELDS (union_type)
10134 * sizeof (struct field)));
10135
10136 memcpy (TYPE_FIELDS (union_type) + 1, TYPE_FIELDS (type),
10137 TYPE_NFIELDS (type) * sizeof (struct field));
10138
10139 /* Install the discriminant at index 0 in the union. */
10140 TYPE_FIELD (union_type, 0) = *disr_field;
10141 TYPE_FIELD_ARTIFICIAL (union_type, 0) = 1;
10142 TYPE_FIELD_NAME (union_type, 0) = "<<discriminant>>";
10143
10144 /* Install the union in the outer struct type. */
10145 TYPE_FIELD_TYPE (type, 0) = union_type;
10146 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
10147 TYPE_NFIELDS (type) = 1;
10148
10149 /* Set the size and offset of the union type. */
10150 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
10151
10152 /* We need a way to find the correct discriminant given a
10153 variant name. For convenience we build a map here. */
10154 struct type *enum_type = FIELD_TYPE (*disr_field);
10155 std::unordered_map<std::string, ULONGEST> discriminant_map;
10156 for (int i = 0; i < TYPE_NFIELDS (enum_type); ++i)
10157 {
10158 if (TYPE_FIELD_LOC_KIND (enum_type, i) == FIELD_LOC_KIND_ENUMVAL)
10159 {
10160 const char *name
10161 = rust_last_path_segment (TYPE_FIELD_NAME (enum_type, i));
10162 discriminant_map[name] = TYPE_FIELD_ENUMVAL (enum_type, i);
10163 }
10164 }
10165
10166 int n_fields = TYPE_NFIELDS (union_type);
10167 struct discriminant_info *disc
10168 = alloc_discriminant_info (union_type, 0, -1);
10169 /* Skip the discriminant here. */
10170 for (int i = 1; i < n_fields; ++i)
10171 {
10172 /* Find the final word in the name of this variant's type.
10173 That name can be used to look up the correct
10174 discriminant. */
10175 const char *variant_name
10176 = rust_last_path_segment (TYPE_NAME (TYPE_FIELD_TYPE (union_type,
10177 i)));
10178
10179 auto iter = discriminant_map.find (variant_name);
10180 if (iter != discriminant_map.end ())
10181 disc->discriminants[i] = iter->second;
10182
bedda9ac 10183 /* Remove the discriminant field, if it exists. */
c9317f21 10184 struct type *sub_type = TYPE_FIELD_TYPE (union_type, i);
bedda9ac
TT
10185 if (TYPE_NFIELDS (sub_type) > 0)
10186 {
10187 --TYPE_NFIELDS (sub_type);
10188 ++TYPE_FIELDS (sub_type);
10189 }
c9317f21
TT
10190 TYPE_FIELD_NAME (union_type, i) = variant_name;
10191 TYPE_NAME (sub_type)
10192 = rust_fully_qualify (&objfile->objfile_obstack,
10193 TYPE_NAME (type), variant_name);
10194 }
10195 }
10196}
10197
10198/* Rewrite some Rust unions to be structures with variants parts. */
10199
10200static void
10201rust_union_quirks (struct dwarf2_cu *cu)
10202{
10203 gdb_assert (cu->language == language_rust);
52941706
SM
10204 for (type *type_ : cu->rust_unions)
10205 quirk_rust_enum (type_, cu->per_cu->dwarf2_per_objfile->objfile);
2d79090e
TT
10206 /* We don't need this any more. */
10207 cu->rust_unions.clear ();
c9317f21
TT
10208}
10209
95554aad
TT
10210/* Return the symtab for PER_CU. This works properly regardless of
10211 whether we're using the index or psymtabs. */
10212
43f3e411
DE
10213static struct compunit_symtab *
10214get_compunit_symtab (struct dwarf2_per_cu_data *per_cu)
95554aad 10215{
ed2dc618 10216 return (per_cu->dwarf2_per_objfile->using_index
43f3e411
DE
10217 ? per_cu->v.quick->compunit_symtab
10218 : per_cu->v.psymtab->compunit_symtab);
95554aad
TT
10219}
10220
10221/* A helper function for computing the list of all symbol tables
10222 included by PER_CU. */
10223
10224static void
4c39bc03 10225recursively_compute_inclusions (std::vector<compunit_symtab *> *result,
ec94af83 10226 htab_t all_children, htab_t all_type_symtabs,
f9125b6c 10227 struct dwarf2_per_cu_data *per_cu,
43f3e411 10228 struct compunit_symtab *immediate_parent)
95554aad
TT
10229{
10230 void **slot;
10231 int ix;
43f3e411 10232 struct compunit_symtab *cust;
95554aad
TT
10233 struct dwarf2_per_cu_data *iter;
10234
10235 slot = htab_find_slot (all_children, per_cu, INSERT);
10236 if (*slot != NULL)
10237 {
10238 /* This inclusion and its children have been processed. */
10239 return;
10240 }
10241
10242 *slot = per_cu;
10243 /* Only add a CU if it has a symbol table. */
43f3e411
DE
10244 cust = get_compunit_symtab (per_cu);
10245 if (cust != NULL)
ec94af83
DE
10246 {
10247 /* If this is a type unit only add its symbol table if we haven't
10248 seen it yet (type unit per_cu's can share symtabs). */
10249 if (per_cu->is_debug_types)
10250 {
43f3e411 10251 slot = htab_find_slot (all_type_symtabs, cust, INSERT);
ec94af83
DE
10252 if (*slot == NULL)
10253 {
43f3e411 10254 *slot = cust;
4c39bc03 10255 result->push_back (cust);
43f3e411
DE
10256 if (cust->user == NULL)
10257 cust->user = immediate_parent;
ec94af83
DE
10258 }
10259 }
10260 else
f9125b6c 10261 {
4c39bc03 10262 result->push_back (cust);
43f3e411
DE
10263 if (cust->user == NULL)
10264 cust->user = immediate_parent;
f9125b6c 10265 }
ec94af83 10266 }
95554aad
TT
10267
10268 for (ix = 0;
796a7ff8 10269 VEC_iterate (dwarf2_per_cu_ptr, per_cu->imported_symtabs, ix, iter);
95554aad 10270 ++ix)
ec94af83
DE
10271 {
10272 recursively_compute_inclusions (result, all_children,
43f3e411 10273 all_type_symtabs, iter, cust);
ec94af83 10274 }
95554aad
TT
10275}
10276
43f3e411 10277/* Compute the compunit_symtab 'includes' fields for the compunit_symtab of
95554aad
TT
10278 PER_CU. */
10279
10280static void
43f3e411 10281compute_compunit_symtab_includes (struct dwarf2_per_cu_data *per_cu)
95554aad 10282{
f4dc4d17
DE
10283 gdb_assert (! per_cu->is_debug_types);
10284
796a7ff8 10285 if (!VEC_empty (dwarf2_per_cu_ptr, per_cu->imported_symtabs))
95554aad
TT
10286 {
10287 int ix, len;
ec94af83 10288 struct dwarf2_per_cu_data *per_cu_iter;
4c39bc03 10289 std::vector<compunit_symtab *> result_symtabs;
ec94af83 10290 htab_t all_children, all_type_symtabs;
43f3e411 10291 struct compunit_symtab *cust = get_compunit_symtab (per_cu);
95554aad
TT
10292
10293 /* If we don't have a symtab, we can just skip this case. */
43f3e411 10294 if (cust == NULL)
95554aad
TT
10295 return;
10296
10297 all_children = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
10298 NULL, xcalloc, xfree);
ec94af83
DE
10299 all_type_symtabs = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
10300 NULL, xcalloc, xfree);
95554aad
TT
10301
10302 for (ix = 0;
796a7ff8 10303 VEC_iterate (dwarf2_per_cu_ptr, per_cu->imported_symtabs,
ec94af83 10304 ix, per_cu_iter);
95554aad 10305 ++ix)
ec94af83
DE
10306 {
10307 recursively_compute_inclusions (&result_symtabs, all_children,
f9125b6c 10308 all_type_symtabs, per_cu_iter,
43f3e411 10309 cust);
ec94af83 10310 }
95554aad 10311
ec94af83 10312 /* Now we have a transitive closure of all the included symtabs. */
4c39bc03 10313 len = result_symtabs.size ();
43f3e411 10314 cust->includes
ed2dc618 10315 = XOBNEWVEC (&per_cu->dwarf2_per_objfile->objfile->objfile_obstack,
8d749320 10316 struct compunit_symtab *, len + 1);
4c39bc03
TT
10317 memcpy (cust->includes, result_symtabs.data (),
10318 len * sizeof (compunit_symtab *));
43f3e411 10319 cust->includes[len] = NULL;
95554aad 10320
95554aad 10321 htab_delete (all_children);
ec94af83 10322 htab_delete (all_type_symtabs);
95554aad
TT
10323 }
10324}
10325
10326/* Compute the 'includes' field for the symtabs of all the CUs we just
10327 read. */
10328
10329static void
ed2dc618 10330process_cu_includes (struct dwarf2_per_objfile *dwarf2_per_objfile)
95554aad 10331{
71b73764 10332 for (dwarf2_per_cu_data *iter : dwarf2_per_objfile->just_read_cus)
f4dc4d17
DE
10333 {
10334 if (! iter->is_debug_types)
43f3e411 10335 compute_compunit_symtab_includes (iter);
f4dc4d17 10336 }
95554aad 10337
c5d0225d 10338 dwarf2_per_objfile->just_read_cus.clear ();
95554aad
TT
10339}
10340
9cdd5dbd 10341/* Generate full symbol information for PER_CU, whose DIEs have
10b3939b
DJ
10342 already been loaded into memory. */
10343
10344static void
95554aad
TT
10345process_full_comp_unit (struct dwarf2_per_cu_data *per_cu,
10346 enum language pretend_language)
10b3939b 10347{
10b3939b 10348 struct dwarf2_cu *cu = per_cu->cu;
ed2dc618
SM
10349 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
10350 struct objfile *objfile = dwarf2_per_objfile->objfile;
3e29f34a 10351 struct gdbarch *gdbarch = get_objfile_arch (objfile);
10b3939b 10352 CORE_ADDR lowpc, highpc;
43f3e411 10353 struct compunit_symtab *cust;
10b3939b 10354 CORE_ADDR baseaddr;
4359dff1 10355 struct block *static_block;
3e29f34a 10356 CORE_ADDR addr;
10b3939b
DJ
10357
10358 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
10359
c89b44cd
TT
10360 /* Clear the list here in case something was left over. */
10361 cu->method_list.clear ();
10b3939b 10362
95554aad
TT
10363 cu->language = pretend_language;
10364 cu->language_defn = language_def (cu->language);
10365
c906108c 10366 /* Do line number decoding in read_file_scope () */
10b3939b 10367 process_die (cu->dies, cu);
c906108c 10368
a766d390
DE
10369 /* For now fudge the Go package. */
10370 if (cu->language == language_go)
10371 fixup_go_packaging (cu);
10372
3da10d80
KS
10373 /* Now that we have processed all the DIEs in the CU, all the types
10374 should be complete, and it should now be safe to compute all of the
10375 physnames. */
10376 compute_delayed_physnames (cu);
3da10d80 10377
c9317f21
TT
10378 if (cu->language == language_rust)
10379 rust_union_quirks (cu);
10380
fae299cd
DC
10381 /* Some compilers don't define a DW_AT_high_pc attribute for the
10382 compilation unit. If the DW_AT_high_pc is missing, synthesize
10383 it, by scanning the DIE's below the compilation unit. */
10b3939b 10384 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
c906108c 10385
3e29f34a 10386 addr = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
c24bdb02 10387 static_block = cu->get_builder ()->end_symtab_get_static_block (addr, 0, 1);
4359dff1
JK
10388
10389 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
10390 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
10391 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
10392 addrmap to help ensure it has an accurate map of pc values belonging to
10393 this comp unit. */
10394 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
10395
c24bdb02 10396 cust = cu->get_builder ()->end_symtab_from_static_block (static_block,
804d2729
TT
10397 SECT_OFF_TEXT (objfile),
10398 0);
c906108c 10399
43f3e411 10400 if (cust != NULL)
c906108c 10401 {
df15bd07 10402 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
4632c0d0 10403
8be455d7
JK
10404 /* Set symtab language to language from DW_AT_language. If the
10405 compilation is from a C file generated by language preprocessors, do
10406 not set the language if it was already deduced by start_subfile. */
43f3e411 10407 if (!(cu->language == language_c
40e3ad0e 10408 && COMPUNIT_FILETABS (cust)->language != language_unknown))
43f3e411 10409 COMPUNIT_FILETABS (cust)->language = cu->language;
8be455d7
JK
10410
10411 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
10412 produce DW_AT_location with location lists but it can be possibly
ab260dad
JK
10413 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
10414 there were bugs in prologue debug info, fixed later in GCC-4.5
10415 by "unwind info for epilogues" patch (which is not directly related).
8be455d7
JK
10416
10417 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
10418 needed, it would be wrong due to missing DW_AT_producer there.
10419
10420 Still one can confuse GDB by using non-standard GCC compilation
10421 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
10422 */
ab260dad 10423 if (cu->has_loclist && gcc_4_minor >= 5)
43f3e411 10424 cust->locations_valid = 1;
e0d00bc7
JK
10425
10426 if (gcc_4_minor >= 5)
43f3e411 10427 cust->epilogue_unwind_valid = 1;
96408a79 10428
43f3e411 10429 cust->call_site_htab = cu->call_site_htab;
c906108c 10430 }
9291a0cd
TT
10431
10432 if (dwarf2_per_objfile->using_index)
43f3e411 10433 per_cu->v.quick->compunit_symtab = cust;
9291a0cd
TT
10434 else
10435 {
10436 struct partial_symtab *pst = per_cu->v.psymtab;
43f3e411 10437 pst->compunit_symtab = cust;
9291a0cd
TT
10438 pst->readin = 1;
10439 }
c906108c 10440
95554aad 10441 /* Push it for inclusion processing later. */
c5d0225d 10442 dwarf2_per_objfile->just_read_cus.push_back (per_cu);
804d2729
TT
10443
10444 /* Not needed any more. */
c24bdb02 10445 cu->reset_builder ();
f4dc4d17 10446}
45cfd468 10447
f4dc4d17
DE
10448/* Generate full symbol information for type unit PER_CU, whose DIEs have
10449 already been loaded into memory. */
10450
10451static void
10452process_full_type_unit (struct dwarf2_per_cu_data *per_cu,
10453 enum language pretend_language)
10454{
10455 struct dwarf2_cu *cu = per_cu->cu;
ed2dc618
SM
10456 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
10457 struct objfile *objfile = dwarf2_per_objfile->objfile;
43f3e411 10458 struct compunit_symtab *cust;
0186c6a7
DE
10459 struct signatured_type *sig_type;
10460
10461 gdb_assert (per_cu->is_debug_types);
10462 sig_type = (struct signatured_type *) per_cu;
f4dc4d17 10463
c89b44cd
TT
10464 /* Clear the list here in case something was left over. */
10465 cu->method_list.clear ();
f4dc4d17 10466
f4dc4d17
DE
10467 cu->language = pretend_language;
10468 cu->language_defn = language_def (cu->language);
10469
10470 /* The symbol tables are set up in read_type_unit_scope. */
10471 process_die (cu->dies, cu);
10472
10473 /* For now fudge the Go package. */
10474 if (cu->language == language_go)
10475 fixup_go_packaging (cu);
10476
10477 /* Now that we have processed all the DIEs in the CU, all the types
10478 should be complete, and it should now be safe to compute all of the
10479 physnames. */
10480 compute_delayed_physnames (cu);
f4dc4d17 10481
c9317f21
TT
10482 if (cu->language == language_rust)
10483 rust_union_quirks (cu);
10484
f4dc4d17
DE
10485 /* TUs share symbol tables.
10486 If this is the first TU to use this symtab, complete the construction
094b34ac
DE
10487 of it with end_expandable_symtab. Otherwise, complete the addition of
10488 this TU's symbols to the existing symtab. */
43f3e411 10489 if (sig_type->type_unit_group->compunit_symtab == NULL)
45cfd468 10490 {
c24bdb02
KS
10491 buildsym_compunit *builder = cu->get_builder ();
10492 cust = builder->end_expandable_symtab (0, SECT_OFF_TEXT (objfile));
43f3e411 10493 sig_type->type_unit_group->compunit_symtab = cust;
f4dc4d17 10494
43f3e411 10495 if (cust != NULL)
f4dc4d17
DE
10496 {
10497 /* Set symtab language to language from DW_AT_language. If the
10498 compilation is from a C file generated by language preprocessors,
10499 do not set the language if it was already deduced by
10500 start_subfile. */
43f3e411
DE
10501 if (!(cu->language == language_c
10502 && COMPUNIT_FILETABS (cust)->language != language_c))
10503 COMPUNIT_FILETABS (cust)->language = cu->language;
f4dc4d17
DE
10504 }
10505 }
10506 else
10507 {
c24bdb02 10508 cu->get_builder ()->augment_type_symtab ();
43f3e411 10509 cust = sig_type->type_unit_group->compunit_symtab;
f4dc4d17
DE
10510 }
10511
10512 if (dwarf2_per_objfile->using_index)
43f3e411 10513 per_cu->v.quick->compunit_symtab = cust;
f4dc4d17
DE
10514 else
10515 {
10516 struct partial_symtab *pst = per_cu->v.psymtab;
43f3e411 10517 pst->compunit_symtab = cust;
f4dc4d17 10518 pst->readin = 1;
45cfd468 10519 }
804d2729
TT
10520
10521 /* Not needed any more. */
c24bdb02 10522 cu->reset_builder ();
c906108c
SS
10523}
10524
95554aad
TT
10525/* Process an imported unit DIE. */
10526
10527static void
10528process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
10529{
10530 struct attribute *attr;
10531
f4dc4d17
DE
10532 /* For now we don't handle imported units in type units. */
10533 if (cu->per_cu->is_debug_types)
10534 {
10535 error (_("Dwarf Error: DW_TAG_imported_unit is not"
10536 " supported in type units [in module %s]"),
518817b3 10537 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
f4dc4d17
DE
10538 }
10539
95554aad
TT
10540 attr = dwarf2_attr (die, DW_AT_import, cu);
10541 if (attr != NULL)
10542 {
9c541725
PA
10543 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
10544 bool is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
10545 dwarf2_per_cu_data *per_cu
e3b94546 10546 = dwarf2_find_containing_comp_unit (sect_off, is_dwz,
518817b3 10547 cu->per_cu->dwarf2_per_objfile);
95554aad 10548
69d751e3 10549 /* If necessary, add it to the queue and load its DIEs. */
95554aad 10550 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
58f0c718 10551 load_full_comp_unit (per_cu, false, cu->language);
95554aad 10552
796a7ff8 10553 VEC_safe_push (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs,
95554aad
TT
10554 per_cu);
10555 }
10556}
10557
4c8aa72d
PA
10558/* RAII object that represents a process_die scope: i.e.,
10559 starts/finishes processing a DIE. */
10560class process_die_scope
adde2bff 10561{
4c8aa72d
PA
10562public:
10563 process_die_scope (die_info *die, dwarf2_cu *cu)
10564 : m_die (die), m_cu (cu)
10565 {
10566 /* We should only be processing DIEs not already in process. */
10567 gdb_assert (!m_die->in_process);
10568 m_die->in_process = true;
10569 }
8c3cb9fa 10570
4c8aa72d
PA
10571 ~process_die_scope ()
10572 {
10573 m_die->in_process = false;
10574
10575 /* If we're done processing the DIE for the CU that owns the line
10576 header, we don't need the line header anymore. */
10577 if (m_cu->line_header_die_owner == m_die)
10578 {
10579 delete m_cu->line_header;
10580 m_cu->line_header = NULL;
10581 m_cu->line_header_die_owner = NULL;
10582 }
10583 }
10584
10585private:
10586 die_info *m_die;
10587 dwarf2_cu *m_cu;
10588};
adde2bff 10589
c906108c
SS
10590/* Process a die and its children. */
10591
10592static void
e7c27a73 10593process_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 10594{
4c8aa72d 10595 process_die_scope scope (die, cu);
adde2bff 10596
c906108c
SS
10597 switch (die->tag)
10598 {
10599 case DW_TAG_padding:
10600 break;
10601 case DW_TAG_compile_unit:
95554aad 10602 case DW_TAG_partial_unit:
e7c27a73 10603 read_file_scope (die, cu);
c906108c 10604 break;
348e048f
DE
10605 case DW_TAG_type_unit:
10606 read_type_unit_scope (die, cu);
10607 break;
c906108c 10608 case DW_TAG_subprogram:
c906108c 10609 case DW_TAG_inlined_subroutine:
edb3359d 10610 read_func_scope (die, cu);
c906108c
SS
10611 break;
10612 case DW_TAG_lexical_block:
14898363
L
10613 case DW_TAG_try_block:
10614 case DW_TAG_catch_block:
e7c27a73 10615 read_lexical_block_scope (die, cu);
c906108c 10616 break;
216f72a1 10617 case DW_TAG_call_site:
96408a79
SA
10618 case DW_TAG_GNU_call_site:
10619 read_call_site_scope (die, cu);
10620 break;
c906108c 10621 case DW_TAG_class_type:
680b30c7 10622 case DW_TAG_interface_type:
c906108c
SS
10623 case DW_TAG_structure_type:
10624 case DW_TAG_union_type:
134d01f1 10625 process_structure_scope (die, cu);
c906108c
SS
10626 break;
10627 case DW_TAG_enumeration_type:
134d01f1 10628 process_enumeration_scope (die, cu);
c906108c 10629 break;
134d01f1 10630
f792889a
DJ
10631 /* These dies have a type, but processing them does not create
10632 a symbol or recurse to process the children. Therefore we can
10633 read them on-demand through read_type_die. */
c906108c 10634 case DW_TAG_subroutine_type:
72019c9c 10635 case DW_TAG_set_type:
c906108c 10636 case DW_TAG_array_type:
c906108c 10637 case DW_TAG_pointer_type:
c906108c 10638 case DW_TAG_ptr_to_member_type:
c906108c 10639 case DW_TAG_reference_type:
4297a3f0 10640 case DW_TAG_rvalue_reference_type:
c906108c 10641 case DW_TAG_string_type:
c906108c 10642 break;
134d01f1 10643
c906108c 10644 case DW_TAG_base_type:
a02abb62 10645 case DW_TAG_subrange_type:
cb249c71 10646 case DW_TAG_typedef:
134d01f1
DJ
10647 /* Add a typedef symbol for the type definition, if it has a
10648 DW_AT_name. */
f792889a 10649 new_symbol (die, read_type_die (die, cu), cu);
a02abb62 10650 break;
c906108c 10651 case DW_TAG_common_block:
e7c27a73 10652 read_common_block (die, cu);
c906108c
SS
10653 break;
10654 case DW_TAG_common_inclusion:
10655 break;
d9fa45fe 10656 case DW_TAG_namespace:
9068261f 10657 cu->processing_has_namespace_info = true;
e7c27a73 10658 read_namespace (die, cu);
d9fa45fe 10659 break;
5d7cb8df 10660 case DW_TAG_module:
9068261f 10661 cu->processing_has_namespace_info = true;
5d7cb8df
JK
10662 read_module (die, cu);
10663 break;
d9fa45fe 10664 case DW_TAG_imported_declaration:
9068261f 10665 cu->processing_has_namespace_info = true;
74921315
KS
10666 if (read_namespace_alias (die, cu))
10667 break;
86a73007
TT
10668 /* The declaration is not a global namespace alias. */
10669 /* Fall through. */
d9fa45fe 10670 case DW_TAG_imported_module:
9068261f 10671 cu->processing_has_namespace_info = true;
27aa8d6a
SW
10672 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
10673 || cu->language != language_fortran))
b98664d3 10674 complaint (_("Tag '%s' has unexpected children"),
27aa8d6a
SW
10675 dwarf_tag_name (die->tag));
10676 read_import_statement (die, cu);
d9fa45fe 10677 break;
95554aad
TT
10678
10679 case DW_TAG_imported_unit:
10680 process_imported_unit_die (die, cu);
10681 break;
10682
71a3c369
TT
10683 case DW_TAG_variable:
10684 read_variable (die, cu);
10685 break;
10686
c906108c 10687 default:
e7c27a73 10688 new_symbol (die, NULL, cu);
c906108c
SS
10689 break;
10690 }
10691}
ca69b9e6
DE
10692\f
10693/* DWARF name computation. */
c906108c 10694
94af9270
KS
10695/* A helper function for dwarf2_compute_name which determines whether DIE
10696 needs to have the name of the scope prepended to the name listed in the
10697 die. */
10698
10699static int
10700die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
10701{
1c809c68
TT
10702 struct attribute *attr;
10703
94af9270
KS
10704 switch (die->tag)
10705 {
10706 case DW_TAG_namespace:
10707 case DW_TAG_typedef:
10708 case DW_TAG_class_type:
10709 case DW_TAG_interface_type:
10710 case DW_TAG_structure_type:
10711 case DW_TAG_union_type:
10712 case DW_TAG_enumeration_type:
10713 case DW_TAG_enumerator:
10714 case DW_TAG_subprogram:
08a76f8a 10715 case DW_TAG_inlined_subroutine:
94af9270 10716 case DW_TAG_member:
74921315 10717 case DW_TAG_imported_declaration:
94af9270
KS
10718 return 1;
10719
10720 case DW_TAG_variable:
c2b0a229 10721 case DW_TAG_constant:
94af9270
KS
10722 /* We only need to prefix "globally" visible variables. These include
10723 any variable marked with DW_AT_external or any variable that
10724 lives in a namespace. [Variables in anonymous namespaces
10725 require prefixing, but they are not DW_AT_external.] */
10726
10727 if (dwarf2_attr (die, DW_AT_specification, cu))
10728 {
10729 struct dwarf2_cu *spec_cu = cu;
9a619af0 10730
94af9270
KS
10731 return die_needs_namespace (die_specification (die, &spec_cu),
10732 spec_cu);
10733 }
10734
1c809c68 10735 attr = dwarf2_attr (die, DW_AT_external, cu);
f55ee35c
JK
10736 if (attr == NULL && die->parent->tag != DW_TAG_namespace
10737 && die->parent->tag != DW_TAG_module)
1c809c68
TT
10738 return 0;
10739 /* A variable in a lexical block of some kind does not need a
10740 namespace, even though in C++ such variables may be external
10741 and have a mangled name. */
10742 if (die->parent->tag == DW_TAG_lexical_block
10743 || die->parent->tag == DW_TAG_try_block
1054b214
TT
10744 || die->parent->tag == DW_TAG_catch_block
10745 || die->parent->tag == DW_TAG_subprogram)
1c809c68
TT
10746 return 0;
10747 return 1;
94af9270
KS
10748
10749 default:
10750 return 0;
10751 }
10752}
10753
73b9be8b
KS
10754/* Return the DIE's linkage name attribute, either DW_AT_linkage_name
10755 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10756 defined for the given DIE. */
10757
10758static struct attribute *
10759dw2_linkage_name_attr (struct die_info *die, struct dwarf2_cu *cu)
10760{
10761 struct attribute *attr;
10762
10763 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
10764 if (attr == NULL)
10765 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
10766
10767 return attr;
10768}
10769
10770/* Return the DIE's linkage name as a string, either DW_AT_linkage_name
10771 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10772 defined for the given DIE. */
10773
10774static const char *
10775dw2_linkage_name (struct die_info *die, struct dwarf2_cu *cu)
10776{
10777 const char *linkage_name;
10778
10779 linkage_name = dwarf2_string_attr (die, DW_AT_linkage_name, cu);
10780 if (linkage_name == NULL)
10781 linkage_name = dwarf2_string_attr (die, DW_AT_MIPS_linkage_name, cu);
10782
10783 return linkage_name;
10784}
10785
94af9270 10786/* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
a766d390 10787 compute the physname for the object, which include a method's:
9c37b5ae 10788 - formal parameters (C++),
a766d390 10789 - receiver type (Go),
a766d390
DE
10790
10791 The term "physname" is a bit confusing.
10792 For C++, for example, it is the demangled name.
10793 For Go, for example, it's the mangled name.
94af9270 10794
af6b7be1
JB
10795 For Ada, return the DIE's linkage name rather than the fully qualified
10796 name. PHYSNAME is ignored..
10797
94af9270
KS
10798 The result is allocated on the objfile_obstack and canonicalized. */
10799
10800static const char *
15d034d0
TT
10801dwarf2_compute_name (const char *name,
10802 struct die_info *die, struct dwarf2_cu *cu,
94af9270
KS
10803 int physname)
10804{
518817b3 10805 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
bb5ed363 10806
94af9270
KS
10807 if (name == NULL)
10808 name = dwarf2_name (die, cu);
10809
2ee7123e
DE
10810 /* For Fortran GDB prefers DW_AT_*linkage_name for the physname if present
10811 but otherwise compute it by typename_concat inside GDB.
10812 FIXME: Actually this is not really true, or at least not always true.
10813 It's all very confusing. SYMBOL_SET_NAMES doesn't try to demangle
5e2db402 10814 Fortran names because there is no mangling standard. So new_symbol
2ee7123e
DE
10815 will set the demangled name to the result of dwarf2_full_name, and it is
10816 the demangled name that GDB uses if it exists. */
f55ee35c
JK
10817 if (cu->language == language_ada
10818 || (cu->language == language_fortran && physname))
10819 {
10820 /* For Ada unit, we prefer the linkage name over the name, as
10821 the former contains the exported name, which the user expects
10822 to be able to reference. Ideally, we want the user to be able
10823 to reference this entity using either natural or linkage name,
10824 but we haven't started looking at this enhancement yet. */
73b9be8b 10825 const char *linkage_name = dw2_linkage_name (die, cu);
f55ee35c 10826
2ee7123e
DE
10827 if (linkage_name != NULL)
10828 return linkage_name;
f55ee35c
JK
10829 }
10830
94af9270
KS
10831 /* These are the only languages we know how to qualify names in. */
10832 if (name != NULL
9c37b5ae 10833 && (cu->language == language_cplus
c44af4eb
TT
10834 || cu->language == language_fortran || cu->language == language_d
10835 || cu->language == language_rust))
94af9270
KS
10836 {
10837 if (die_needs_namespace (die, cu))
10838 {
0d5cff50 10839 const char *prefix;
34a68019 10840 const char *canonical_name = NULL;
94af9270 10841
d7e74731
PA
10842 string_file buf;
10843
94af9270 10844 prefix = determine_prefix (die, cu);
94af9270
KS
10845 if (*prefix != '\0')
10846 {
f55ee35c
JK
10847 char *prefixed_name = typename_concat (NULL, prefix, name,
10848 physname, cu);
9a619af0 10849
d7e74731 10850 buf.puts (prefixed_name);
94af9270
KS
10851 xfree (prefixed_name);
10852 }
10853 else
d7e74731 10854 buf.puts (name);
94af9270 10855
98bfdba5
PA
10856 /* Template parameters may be specified in the DIE's DW_AT_name, or
10857 as children with DW_TAG_template_type_param or
10858 DW_TAG_value_type_param. If the latter, add them to the name
10859 here. If the name already has template parameters, then
10860 skip this step; some versions of GCC emit both, and
10861 it is more efficient to use the pre-computed name.
10862
10863 Something to keep in mind about this process: it is very
10864 unlikely, or in some cases downright impossible, to produce
10865 something that will match the mangled name of a function.
10866 If the definition of the function has the same debug info,
10867 we should be able to match up with it anyway. But fallbacks
10868 using the minimal symbol, for instance to find a method
10869 implemented in a stripped copy of libstdc++, will not work.
10870 If we do not have debug info for the definition, we will have to
10871 match them up some other way.
10872
10873 When we do name matching there is a related problem with function
10874 templates; two instantiated function templates are allowed to
10875 differ only by their return types, which we do not add here. */
10876
10877 if (cu->language == language_cplus && strchr (name, '<') == NULL)
10878 {
10879 struct attribute *attr;
10880 struct die_info *child;
10881 int first = 1;
10882
10883 die->building_fullname = 1;
10884
10885 for (child = die->child; child != NULL; child = child->sibling)
10886 {
10887 struct type *type;
12df843f 10888 LONGEST value;
d521ce57 10889 const gdb_byte *bytes;
98bfdba5
PA
10890 struct dwarf2_locexpr_baton *baton;
10891 struct value *v;
10892
10893 if (child->tag != DW_TAG_template_type_param
10894 && child->tag != DW_TAG_template_value_param)
10895 continue;
10896
10897 if (first)
10898 {
d7e74731 10899 buf.puts ("<");
98bfdba5
PA
10900 first = 0;
10901 }
10902 else
d7e74731 10903 buf.puts (", ");
98bfdba5
PA
10904
10905 attr = dwarf2_attr (child, DW_AT_type, cu);
10906 if (attr == NULL)
10907 {
b98664d3 10908 complaint (_("template parameter missing DW_AT_type"));
d7e74731 10909 buf.puts ("UNKNOWN_TYPE");
98bfdba5
PA
10910 continue;
10911 }
10912 type = die_type (child, cu);
10913
10914 if (child->tag == DW_TAG_template_type_param)
10915 {
c1ec8cea
TT
10916 c_print_type (type, "", &buf, -1, 0, cu->language,
10917 &type_print_raw_options);
98bfdba5
PA
10918 continue;
10919 }
10920
10921 attr = dwarf2_attr (child, DW_AT_const_value, cu);
10922 if (attr == NULL)
10923 {
b98664d3 10924 complaint (_("template parameter missing "
3e43a32a 10925 "DW_AT_const_value"));
d7e74731 10926 buf.puts ("UNKNOWN_VALUE");
98bfdba5
PA
10927 continue;
10928 }
10929
10930 dwarf2_const_value_attr (attr, type, name,
10931 &cu->comp_unit_obstack, cu,
10932 &value, &bytes, &baton);
10933
10934 if (TYPE_NOSIGN (type))
10935 /* GDB prints characters as NUMBER 'CHAR'. If that's
10936 changed, this can use value_print instead. */
d7e74731 10937 c_printchar (value, type, &buf);
98bfdba5
PA
10938 else
10939 {
10940 struct value_print_options opts;
10941
10942 if (baton != NULL)
10943 v = dwarf2_evaluate_loc_desc (type, NULL,
10944 baton->data,
10945 baton->size,
10946 baton->per_cu);
10947 else if (bytes != NULL)
10948 {
10949 v = allocate_value (type);
10950 memcpy (value_contents_writeable (v), bytes,
10951 TYPE_LENGTH (type));
10952 }
10953 else
10954 v = value_from_longest (type, value);
10955
3e43a32a
MS
10956 /* Specify decimal so that we do not depend on
10957 the radix. */
98bfdba5
PA
10958 get_formatted_print_options (&opts, 'd');
10959 opts.raw = 1;
d7e74731 10960 value_print (v, &buf, &opts);
98bfdba5 10961 release_value (v);
98bfdba5
PA
10962 }
10963 }
10964
10965 die->building_fullname = 0;
10966
10967 if (!first)
10968 {
10969 /* Close the argument list, with a space if necessary
10970 (nested templates). */
d7e74731
PA
10971 if (!buf.empty () && buf.string ().back () == '>')
10972 buf.puts (" >");
98bfdba5 10973 else
d7e74731 10974 buf.puts (">");
98bfdba5
PA
10975 }
10976 }
10977
9c37b5ae 10978 /* For C++ methods, append formal parameter type
94af9270 10979 information, if PHYSNAME. */
6e70227d 10980
94af9270 10981 if (physname && die->tag == DW_TAG_subprogram
9c37b5ae 10982 && cu->language == language_cplus)
94af9270
KS
10983 {
10984 struct type *type = read_type_die (die, cu);
10985
d7e74731 10986 c_type_print_args (type, &buf, 1, cu->language,
79d43c61 10987 &type_print_raw_options);
94af9270 10988
9c37b5ae 10989 if (cu->language == language_cplus)
94af9270 10990 {
60430eff
DJ
10991 /* Assume that an artificial first parameter is
10992 "this", but do not crash if it is not. RealView
10993 marks unnamed (and thus unused) parameters as
10994 artificial; there is no way to differentiate
10995 the two cases. */
94af9270
KS
10996 if (TYPE_NFIELDS (type) > 0
10997 && TYPE_FIELD_ARTIFICIAL (type, 0)
60430eff 10998 && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
3e43a32a
MS
10999 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
11000 0))))
d7e74731 11001 buf.puts (" const");
94af9270
KS
11002 }
11003 }
11004
d7e74731 11005 const std::string &intermediate_name = buf.string ();
94af9270
KS
11006
11007 if (cu->language == language_cplus)
34a68019 11008 canonical_name
322a8516 11009 = dwarf2_canonicalize_name (intermediate_name.c_str (), cu,
34a68019
TT
11010 &objfile->per_bfd->storage_obstack);
11011
11012 /* If we only computed INTERMEDIATE_NAME, or if
11013 INTERMEDIATE_NAME is already canonical, then we need to
11014 copy it to the appropriate obstack. */
322a8516 11015 if (canonical_name == NULL || canonical_name == intermediate_name.c_str ())
224c3ddb
SM
11016 name = ((const char *)
11017 obstack_copy0 (&objfile->per_bfd->storage_obstack,
322a8516
PA
11018 intermediate_name.c_str (),
11019 intermediate_name.length ()));
34a68019
TT
11020 else
11021 name = canonical_name;
94af9270
KS
11022 }
11023 }
11024
11025 return name;
11026}
11027
0114d602
DJ
11028/* Return the fully qualified name of DIE, based on its DW_AT_name.
11029 If scope qualifiers are appropriate they will be added. The result
34a68019 11030 will be allocated on the storage_obstack, or NULL if the DIE does
94af9270
KS
11031 not have a name. NAME may either be from a previous call to
11032 dwarf2_name or NULL.
11033
9c37b5ae 11034 The output string will be canonicalized (if C++). */
0114d602
DJ
11035
11036static const char *
15d034d0 11037dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
0114d602 11038{
94af9270
KS
11039 return dwarf2_compute_name (name, die, cu, 0);
11040}
0114d602 11041
94af9270
KS
11042/* Construct a physname for the given DIE in CU. NAME may either be
11043 from a previous call to dwarf2_name or NULL. The result will be
11044 allocated on the objfile_objstack or NULL if the DIE does not have a
11045 name.
0114d602 11046
9c37b5ae 11047 The output string will be canonicalized (if C++). */
0114d602 11048
94af9270 11049static const char *
15d034d0 11050dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
94af9270 11051{
518817b3 11052 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
900e11f9 11053 const char *retval, *mangled = NULL, *canon = NULL;
900e11f9
JK
11054 int need_copy = 1;
11055
11056 /* In this case dwarf2_compute_name is just a shortcut not building anything
11057 on its own. */
11058 if (!die_needs_namespace (die, cu))
11059 return dwarf2_compute_name (name, die, cu, 1);
11060
73b9be8b 11061 mangled = dw2_linkage_name (die, cu);
900e11f9 11062
e98c9e7c
TT
11063 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
11064 See https://github.com/rust-lang/rust/issues/32925. */
11065 if (cu->language == language_rust && mangled != NULL
11066 && strchr (mangled, '{') != NULL)
11067 mangled = NULL;
11068
900e11f9
JK
11069 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
11070 has computed. */
791afaa2 11071 gdb::unique_xmalloc_ptr<char> demangled;
7d45c7c3 11072 if (mangled != NULL)
900e11f9 11073 {
900e11f9 11074
59cc4834
JB
11075 if (language_def (cu->language)->la_store_sym_names_in_linkage_form_p)
11076 {
11077 /* Do nothing (do not demangle the symbol name). */
11078 }
11079 else if (cu->language == language_go)
a766d390 11080 {
5e2db402
TT
11081 /* This is a lie, but we already lie to the caller new_symbol.
11082 new_symbol assumes we return the mangled name.
a766d390 11083 This just undoes that lie until things are cleaned up. */
a766d390
DE
11084 }
11085 else
11086 {
0eb876f5
JB
11087 /* Use DMGL_RET_DROP for C++ template functions to suppress
11088 their return type. It is easier for GDB users to search
11089 for such functions as `name(params)' than `long name(params)'.
11090 In such case the minimal symbol names do not match the full
11091 symbol names but for template functions there is never a need
11092 to look up their definition from their declaration so
11093 the only disadvantage remains the minimal symbol variant
11094 `long name(params)' does not have the proper inferior type. */
791afaa2
TT
11095 demangled.reset (gdb_demangle (mangled,
11096 (DMGL_PARAMS | DMGL_ANSI
11097 | DMGL_RET_DROP)));
a766d390 11098 }
900e11f9 11099 if (demangled)
791afaa2 11100 canon = demangled.get ();
900e11f9
JK
11101 else
11102 {
11103 canon = mangled;
11104 need_copy = 0;
11105 }
11106 }
11107
11108 if (canon == NULL || check_physname)
11109 {
11110 const char *physname = dwarf2_compute_name (name, die, cu, 1);
11111
11112 if (canon != NULL && strcmp (physname, canon) != 0)
11113 {
11114 /* It may not mean a bug in GDB. The compiler could also
11115 compute DW_AT_linkage_name incorrectly. But in such case
11116 GDB would need to be bug-to-bug compatible. */
11117
b98664d3 11118 complaint (_("Computed physname <%s> does not match demangled <%s> "
9d8780f0
SM
11119 "(from linkage <%s>) - DIE at %s [in module %s]"),
11120 physname, canon, mangled, sect_offset_str (die->sect_off),
4262abfb 11121 objfile_name (objfile));
900e11f9
JK
11122
11123 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
11124 is available here - over computed PHYSNAME. It is safer
11125 against both buggy GDB and buggy compilers. */
11126
11127 retval = canon;
11128 }
11129 else
11130 {
11131 retval = physname;
11132 need_copy = 0;
11133 }
11134 }
11135 else
11136 retval = canon;
11137
11138 if (need_copy)
224c3ddb
SM
11139 retval = ((const char *)
11140 obstack_copy0 (&objfile->per_bfd->storage_obstack,
11141 retval, strlen (retval)));
900e11f9 11142
900e11f9 11143 return retval;
0114d602
DJ
11144}
11145
74921315
KS
11146/* Inspect DIE in CU for a namespace alias. If one exists, record
11147 a new symbol for it.
11148
11149 Returns 1 if a namespace alias was recorded, 0 otherwise. */
11150
11151static int
11152read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu)
11153{
11154 struct attribute *attr;
11155
11156 /* If the die does not have a name, this is not a namespace
11157 alias. */
11158 attr = dwarf2_attr (die, DW_AT_name, cu);
11159 if (attr != NULL)
11160 {
11161 int num;
11162 struct die_info *d = die;
11163 struct dwarf2_cu *imported_cu = cu;
11164
11165 /* If the compiler has nested DW_AT_imported_declaration DIEs,
11166 keep inspecting DIEs until we hit the underlying import. */
11167#define MAX_NESTED_IMPORTED_DECLARATIONS 100
11168 for (num = 0; num < MAX_NESTED_IMPORTED_DECLARATIONS; ++num)
11169 {
11170 attr = dwarf2_attr (d, DW_AT_import, cu);
11171 if (attr == NULL)
11172 break;
11173
11174 d = follow_die_ref (d, attr, &imported_cu);
11175 if (d->tag != DW_TAG_imported_declaration)
11176 break;
11177 }
11178
11179 if (num == MAX_NESTED_IMPORTED_DECLARATIONS)
11180 {
b98664d3 11181 complaint (_("DIE at %s has too many recursively imported "
9d8780f0 11182 "declarations"), sect_offset_str (d->sect_off));
74921315
KS
11183 return 0;
11184 }
11185
11186 if (attr != NULL)
11187 {
11188 struct type *type;
9c541725 11189 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
74921315 11190
9c541725 11191 type = get_die_type_at_offset (sect_off, cu->per_cu);
74921315
KS
11192 if (type != NULL && TYPE_CODE (type) == TYPE_CODE_NAMESPACE)
11193 {
11194 /* This declaration is a global namespace alias. Add
11195 a symbol for it whose type is the aliased namespace. */
11196 new_symbol (die, type, cu);
11197 return 1;
11198 }
11199 }
11200 }
11201
11202 return 0;
11203}
11204
22cee43f 11205/* Return the using directives repository (global or local?) to use in the
804d2729 11206 current context for CU.
22cee43f
PMR
11207
11208 For Ada, imported declarations can materialize renamings, which *may* be
11209 global. However it is impossible (for now?) in DWARF to distinguish
11210 "external" imported declarations and "static" ones. As all imported
11211 declarations seem to be static in all other languages, make them all CU-wide
11212 global only in Ada. */
11213
11214static struct using_direct **
804d2729 11215using_directives (struct dwarf2_cu *cu)
22cee43f 11216{
c24bdb02
KS
11217 if (cu->language == language_ada
11218 && cu->get_builder ()->outermost_context_p ())
11219 return cu->get_builder ()->get_global_using_directives ();
22cee43f 11220 else
c24bdb02 11221 return cu->get_builder ()->get_local_using_directives ();
22cee43f
PMR
11222}
11223
27aa8d6a
SW
11224/* Read the import statement specified by the given die and record it. */
11225
11226static void
11227read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
11228{
518817b3 11229 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
27aa8d6a 11230 struct attribute *import_attr;
32019081 11231 struct die_info *imported_die, *child_die;
de4affc9 11232 struct dwarf2_cu *imported_cu;
27aa8d6a 11233 const char *imported_name;
794684b6 11234 const char *imported_name_prefix;
13387711
SW
11235 const char *canonical_name;
11236 const char *import_alias;
11237 const char *imported_declaration = NULL;
794684b6 11238 const char *import_prefix;
eb1e02fd 11239 std::vector<const char *> excludes;
13387711 11240
27aa8d6a
SW
11241 import_attr = dwarf2_attr (die, DW_AT_import, cu);
11242 if (import_attr == NULL)
11243 {
b98664d3 11244 complaint (_("Tag '%s' has no DW_AT_import"),
27aa8d6a
SW
11245 dwarf_tag_name (die->tag));
11246 return;
11247 }
11248
de4affc9
CC
11249 imported_cu = cu;
11250 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
11251 imported_name = dwarf2_name (imported_die, imported_cu);
27aa8d6a
SW
11252 if (imported_name == NULL)
11253 {
11254 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
11255
11256 The import in the following code:
11257 namespace A
11258 {
11259 typedef int B;
11260 }
11261
11262 int main ()
11263 {
11264 using A::B;
11265 B b;
11266 return b;
11267 }
11268
11269 ...
11270 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
11271 <52> DW_AT_decl_file : 1
11272 <53> DW_AT_decl_line : 6
11273 <54> DW_AT_import : <0x75>
11274 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
11275 <59> DW_AT_name : B
11276 <5b> DW_AT_decl_file : 1
11277 <5c> DW_AT_decl_line : 2
11278 <5d> DW_AT_type : <0x6e>
11279 ...
11280 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
11281 <76> DW_AT_byte_size : 4
11282 <77> DW_AT_encoding : 5 (signed)
11283
11284 imports the wrong die ( 0x75 instead of 0x58 ).
11285 This case will be ignored until the gcc bug is fixed. */
11286 return;
11287 }
11288
82856980
SW
11289 /* Figure out the local name after import. */
11290 import_alias = dwarf2_name (die, cu);
27aa8d6a 11291
794684b6
SW
11292 /* Figure out where the statement is being imported to. */
11293 import_prefix = determine_prefix (die, cu);
11294
11295 /* Figure out what the scope of the imported die is and prepend it
11296 to the name of the imported die. */
de4affc9 11297 imported_name_prefix = determine_prefix (imported_die, imported_cu);
794684b6 11298
f55ee35c
JK
11299 if (imported_die->tag != DW_TAG_namespace
11300 && imported_die->tag != DW_TAG_module)
794684b6 11301 {
13387711
SW
11302 imported_declaration = imported_name;
11303 canonical_name = imported_name_prefix;
794684b6 11304 }
13387711 11305 else if (strlen (imported_name_prefix) > 0)
12aaed36 11306 canonical_name = obconcat (&objfile->objfile_obstack,
45280282
IB
11307 imported_name_prefix,
11308 (cu->language == language_d ? "." : "::"),
11309 imported_name, (char *) NULL);
13387711
SW
11310 else
11311 canonical_name = imported_name;
794684b6 11312
32019081
JK
11313 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
11314 for (child_die = die->child; child_die && child_die->tag;
11315 child_die = sibling_die (child_die))
11316 {
11317 /* DWARF-4: A Fortran use statement with a “rename list” may be
11318 represented by an imported module entry with an import attribute
11319 referring to the module and owned entries corresponding to those
11320 entities that are renamed as part of being imported. */
11321
11322 if (child_die->tag != DW_TAG_imported_declaration)
11323 {
b98664d3 11324 complaint (_("child DW_TAG_imported_declaration expected "
9d8780f0
SM
11325 "- DIE at %s [in module %s]"),
11326 sect_offset_str (child_die->sect_off),
11327 objfile_name (objfile));
32019081
JK
11328 continue;
11329 }
11330
11331 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
11332 if (import_attr == NULL)
11333 {
b98664d3 11334 complaint (_("Tag '%s' has no DW_AT_import"),
32019081
JK
11335 dwarf_tag_name (child_die->tag));
11336 continue;
11337 }
11338
11339 imported_cu = cu;
11340 imported_die = follow_die_ref_or_sig (child_die, import_attr,
11341 &imported_cu);
11342 imported_name = dwarf2_name (imported_die, imported_cu);
11343 if (imported_name == NULL)
11344 {
b98664d3 11345 complaint (_("child DW_TAG_imported_declaration has unknown "
9d8780f0
SM
11346 "imported name - DIE at %s [in module %s]"),
11347 sect_offset_str (child_die->sect_off),
11348 objfile_name (objfile));
32019081
JK
11349 continue;
11350 }
11351
eb1e02fd 11352 excludes.push_back (imported_name);
32019081
JK
11353
11354 process_die (child_die, cu);
11355 }
11356
804d2729 11357 add_using_directive (using_directives (cu),
22cee43f
PMR
11358 import_prefix,
11359 canonical_name,
11360 import_alias,
11361 imported_declaration,
11362 excludes,
11363 0,
11364 &objfile->objfile_obstack);
27aa8d6a
SW
11365}
11366
5230b05a
WT
11367/* ICC<14 does not output the required DW_AT_declaration on incomplete
11368 types, but gives them a size of zero. Starting with version 14,
11369 ICC is compatible with GCC. */
11370
9068261f 11371static bool
5230b05a
WT
11372producer_is_icc_lt_14 (struct dwarf2_cu *cu)
11373{
11374 if (!cu->checked_producer)
11375 check_producer (cu);
11376
11377 return cu->producer_is_icc_lt_14;
11378}
11379
eb77c9df
AB
11380/* ICC generates a DW_AT_type for C void functions. This was observed on
11381 ICC 14.0.5.212, and appears to be against the DWARF spec (V5 3.3.2)
11382 which says that void functions should not have a DW_AT_type. */
11383
11384static bool
11385producer_is_icc (struct dwarf2_cu *cu)
11386{
11387 if (!cu->checked_producer)
11388 check_producer (cu);
11389
11390 return cu->producer_is_icc;
11391}
11392
1b80a9fa
JK
11393/* Check for possibly missing DW_AT_comp_dir with relative .debug_line
11394 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
11395 this, it was first present in GCC release 4.3.0. */
11396
9068261f 11397static bool
1b80a9fa
JK
11398producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
11399{
11400 if (!cu->checked_producer)
11401 check_producer (cu);
11402
11403 return cu->producer_is_gcc_lt_4_3;
11404}
11405
d721ba37
PA
11406static file_and_directory
11407find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu)
9291a0cd 11408{
d721ba37
PA
11409 file_and_directory res;
11410
9291a0cd
TT
11411 /* Find the filename. Do not use dwarf2_name here, since the filename
11412 is not a source language identifier. */
d721ba37
PA
11413 res.name = dwarf2_string_attr (die, DW_AT_name, cu);
11414 res.comp_dir = dwarf2_string_attr (die, DW_AT_comp_dir, cu);
9291a0cd 11415
d721ba37
PA
11416 if (res.comp_dir == NULL
11417 && producer_is_gcc_lt_4_3 (cu) && res.name != NULL
11418 && IS_ABSOLUTE_PATH (res.name))
9291a0cd 11419 {
d721ba37
PA
11420 res.comp_dir_storage = ldirname (res.name);
11421 if (!res.comp_dir_storage.empty ())
11422 res.comp_dir = res.comp_dir_storage.c_str ();
9291a0cd 11423 }
d721ba37 11424 if (res.comp_dir != NULL)
9291a0cd
TT
11425 {
11426 /* Irix 6.2 native cc prepends <machine>.: to the compilation
11427 directory, get rid of it. */
d721ba37 11428 const char *cp = strchr (res.comp_dir, ':');
9291a0cd 11429
d721ba37
PA
11430 if (cp && cp != res.comp_dir && cp[-1] == '.' && cp[1] == '/')
11431 res.comp_dir = cp + 1;
9291a0cd
TT
11432 }
11433
d721ba37
PA
11434 if (res.name == NULL)
11435 res.name = "<unknown>";
11436
11437 return res;
9291a0cd
TT
11438}
11439
f4dc4d17
DE
11440/* Handle DW_AT_stmt_list for a compilation unit.
11441 DIE is the DW_TAG_compile_unit die for CU.
c3b7b696
YQ
11442 COMP_DIR is the compilation directory. LOWPC is passed to
11443 dwarf_decode_lines. See dwarf_decode_lines comments about it. */
2ab95328
TT
11444
11445static void
11446handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
c3b7b696 11447 const char *comp_dir, CORE_ADDR lowpc) /* ARI: editCase function */
2ab95328 11448{
518817b3
SM
11449 struct dwarf2_per_objfile *dwarf2_per_objfile
11450 = cu->per_cu->dwarf2_per_objfile;
527f3840 11451 struct objfile *objfile = dwarf2_per_objfile->objfile;
2ab95328 11452 struct attribute *attr;
527f3840
JK
11453 struct line_header line_header_local;
11454 hashval_t line_header_local_hash;
527f3840
JK
11455 void **slot;
11456 int decode_mapping;
2ab95328 11457
f4dc4d17
DE
11458 gdb_assert (! cu->per_cu->is_debug_types);
11459
2ab95328 11460 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
527f3840
JK
11461 if (attr == NULL)
11462 return;
11463
9c541725 11464 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
527f3840
JK
11465
11466 /* The line header hash table is only created if needed (it exists to
11467 prevent redundant reading of the line table for partial_units).
11468 If we're given a partial_unit, we'll need it. If we're given a
11469 compile_unit, then use the line header hash table if it's already
11470 created, but don't create one just yet. */
11471
11472 if (dwarf2_per_objfile->line_header_hash == NULL
11473 && die->tag == DW_TAG_partial_unit)
2ab95328 11474 {
527f3840
JK
11475 dwarf2_per_objfile->line_header_hash
11476 = htab_create_alloc_ex (127, line_header_hash_voidp,
11477 line_header_eq_voidp,
11478 free_line_header_voidp,
11479 &objfile->objfile_obstack,
11480 hashtab_obstack_allocate,
11481 dummy_obstack_deallocate);
11482 }
2ab95328 11483
9c541725 11484 line_header_local.sect_off = line_offset;
527f3840
JK
11485 line_header_local.offset_in_dwz = cu->per_cu->is_dwz;
11486 line_header_local_hash = line_header_hash (&line_header_local);
11487 if (dwarf2_per_objfile->line_header_hash != NULL)
11488 {
11489 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash,
11490 &line_header_local,
11491 line_header_local_hash, NO_INSERT);
11492
11493 /* For DW_TAG_compile_unit we need info like symtab::linetable which
11494 is not present in *SLOT (since if there is something in *SLOT then
11495 it will be for a partial_unit). */
11496 if (die->tag == DW_TAG_partial_unit && slot != NULL)
dee91e82 11497 {
527f3840 11498 gdb_assert (*slot != NULL);
9a3c8263 11499 cu->line_header = (struct line_header *) *slot;
527f3840 11500 return;
dee91e82 11501 }
2ab95328 11502 }
527f3840
JK
11503
11504 /* dwarf_decode_line_header does not yet provide sufficient information.
11505 We always have to call also dwarf_decode_lines for it. */
fff8551c
PA
11506 line_header_up lh = dwarf_decode_line_header (line_offset, cu);
11507 if (lh == NULL)
527f3840 11508 return;
4c8aa72d
PA
11509
11510 cu->line_header = lh.release ();
11511 cu->line_header_die_owner = die;
527f3840
JK
11512
11513 if (dwarf2_per_objfile->line_header_hash == NULL)
11514 slot = NULL;
11515 else
11516 {
11517 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash,
11518 &line_header_local,
11519 line_header_local_hash, INSERT);
11520 gdb_assert (slot != NULL);
11521 }
11522 if (slot != NULL && *slot == NULL)
11523 {
11524 /* This newly decoded line number information unit will be owned
11525 by line_header_hash hash table. */
11526 *slot = cu->line_header;
4c8aa72d 11527 cu->line_header_die_owner = NULL;
527f3840
JK
11528 }
11529 else
11530 {
11531 /* We cannot free any current entry in (*slot) as that struct line_header
11532 may be already used by multiple CUs. Create only temporary decoded
11533 line_header for this CU - it may happen at most once for each line
11534 number information unit. And if we're not using line_header_hash
11535 then this is what we want as well. */
11536 gdb_assert (die->tag != DW_TAG_partial_unit);
527f3840
JK
11537 }
11538 decode_mapping = (die->tag != DW_TAG_partial_unit);
11539 dwarf_decode_lines (cu->line_header, comp_dir, cu, NULL, lowpc,
11540 decode_mapping);
fff8551c 11541
2ab95328
TT
11542}
11543
95554aad 11544/* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
ae2de4f8 11545
c906108c 11546static void
e7c27a73 11547read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 11548{
518817b3
SM
11549 struct dwarf2_per_objfile *dwarf2_per_objfile
11550 = cu->per_cu->dwarf2_per_objfile;
dee91e82 11551 struct objfile *objfile = dwarf2_per_objfile->objfile;
3e29f34a 11552 struct gdbarch *gdbarch = get_objfile_arch (objfile);
2acceee2 11553 CORE_ADDR lowpc = ((CORE_ADDR) -1);
c906108c
SS
11554 CORE_ADDR highpc = ((CORE_ADDR) 0);
11555 struct attribute *attr;
c906108c 11556 struct die_info *child_die;
e142c38c 11557 CORE_ADDR baseaddr;
6e70227d 11558
380618d6 11559 prepare_one_comp_unit (cu, die, cu->language);
e142c38c 11560 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 11561
fae299cd 11562 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
c906108c
SS
11563
11564 /* If we didn't find a lowpc, set it to highpc to avoid complaints
11565 from finish_block. */
2acceee2 11566 if (lowpc == ((CORE_ADDR) -1))
c906108c 11567 lowpc = highpc;
3e29f34a 11568 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
c906108c 11569
d721ba37 11570 file_and_directory fnd = find_file_and_directory (die, cu);
e1024ff1 11571
f4b8a18d
KW
11572 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
11573 standardised yet. As a workaround for the language detection we fall
11574 back to the DW_AT_producer string. */
11575 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
11576 cu->language = language_opencl;
11577
3019eac3
DE
11578 /* Similar hack for Go. */
11579 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
11580 set_cu_language (DW_LANG_Go, cu);
11581
c24bdb02 11582 cu->start_symtab (fnd.name, fnd.comp_dir, lowpc);
3019eac3
DE
11583
11584 /* Decode line number information if present. We do this before
11585 processing child DIEs, so that the line header table is available
11586 for DW_AT_decl_file. */
d721ba37 11587 handle_DW_AT_stmt_list (die, cu, fnd.comp_dir, lowpc);
3019eac3
DE
11588
11589 /* Process all dies in compilation unit. */
11590 if (die->child != NULL)
11591 {
11592 child_die = die->child;
11593 while (child_die && child_die->tag)
11594 {
11595 process_die (child_die, cu);
11596 child_die = sibling_die (child_die);
11597 }
11598 }
11599
11600 /* Decode macro information, if present. Dwarf 2 macro information
11601 refers to information in the line number info statement program
11602 header, so we can only read it if we've read the header
11603 successfully. */
0af92d60
JK
11604 attr = dwarf2_attr (die, DW_AT_macros, cu);
11605 if (attr == NULL)
11606 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
3019eac3
DE
11607 if (attr && cu->line_header)
11608 {
11609 if (dwarf2_attr (die, DW_AT_macro_info, cu))
b98664d3 11610 complaint (_("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
c24bdb02
KS
11626void
11627dwarf2_cu::setup_type_unit_groups (struct die_info *die)
3019eac3 11628{
f4dc4d17
DE
11629 struct type_unit_group *tu_group;
11630 int first_time;
3019eac3 11631 struct attribute *attr;
9c541725 11632 unsigned int i;
0186c6a7 11633 struct signatured_type *sig_type;
3019eac3 11634
f4dc4d17 11635 gdb_assert (per_cu->is_debug_types);
0186c6a7 11636 sig_type = (struct signatured_type *) per_cu;
3019eac3 11637
c24bdb02 11638 attr = dwarf2_attr (die, DW_AT_stmt_list, this);
3019eac3 11639
f4dc4d17 11640 /* If we're using .gdb_index (includes -readnow) then
74e04d1c 11641 per_cu->type_unit_group may not have been set up yet. */
0186c6a7 11642 if (sig_type->type_unit_group == NULL)
c24bdb02 11643 sig_type->type_unit_group = get_type_unit_group (this, attr);
0186c6a7 11644 tu_group = sig_type->type_unit_group;
f4dc4d17
DE
11645
11646 /* If we've already processed this stmt_list there's no real need to
11647 do it again, we could fake it and just recreate the part we need
11648 (file name,index -> symtab mapping). If data shows this optimization
11649 is useful we can do it then. */
43f3e411 11650 first_time = tu_group->compunit_symtab == NULL;
f4dc4d17
DE
11651
11652 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
11653 debug info. */
fff8551c 11654 line_header_up lh;
f4dc4d17 11655 if (attr != NULL)
3019eac3 11656 {
9c541725 11657 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
c24bdb02 11658 lh = dwarf_decode_line_header (line_offset, this);
f4dc4d17
DE
11659 }
11660 if (lh == NULL)
11661 {
11662 if (first_time)
c24bdb02 11663 start_symtab ("", NULL, 0);
f4dc4d17
DE
11664 else
11665 {
11666 gdb_assert (tu_group->symtabs == NULL);
c24bdb02 11667 gdb_assert (m_builder == nullptr);
804d2729 11668 struct compunit_symtab *cust = tu_group->compunit_symtab;
c24bdb02
KS
11669 m_builder.reset (new struct buildsym_compunit
11670 (COMPUNIT_OBJFILE (cust), "",
11671 COMPUNIT_DIRNAME (cust),
11672 compunit_language (cust),
11673 0, cust));
f4dc4d17 11674 }
f4dc4d17 11675 return;
3019eac3
DE
11676 }
11677
c24bdb02
KS
11678 line_header = lh.release ();
11679 line_header_die_owner = die;
3019eac3 11680
f4dc4d17
DE
11681 if (first_time)
11682 {
c24bdb02 11683 struct compunit_symtab *cust = start_symtab ("", NULL, 0);
3019eac3 11684
1fd60fc0
DE
11685 /* Note: We don't assign tu_group->compunit_symtab yet because we're
11686 still initializing it, and our caller (a few levels up)
11687 process_full_type_unit still needs to know if this is the first
11688 time. */
11689
c24bdb02 11690 tu_group->num_symtabs = line_header->file_names.size ();
4c8aa72d 11691 tu_group->symtabs = XNEWVEC (struct symtab *,
c24bdb02 11692 line_header->file_names.size ());
3019eac3 11693
c24bdb02 11694 for (i = 0; i < line_header->file_names.size (); ++i)
f4dc4d17 11695 {
c24bdb02 11696 file_entry &fe = line_header->file_names[i];
3019eac3 11697
c24bdb02
KS
11698 dwarf2_start_subfile (this, fe.name,
11699 fe.include_dir (line_header));
11700 buildsym_compunit *b = get_builder ();
11701 if (b->get_current_subfile ()->symtab == NULL)
f4dc4d17 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. */
c24bdb02
KS
11708 b->get_current_subfile ()->symtab
11709 = allocate_symtab (cust, b->get_current_subfile ()->name);
f4dc4d17
DE
11710 }
11711
c24bdb02 11712 fe.symtab = b->get_current_subfile ()->symtab;
8c43009f 11713 tu_group->symtabs[i] = fe.symtab;
f4dc4d17
DE
11714 }
11715 }
11716 else
3019eac3 11717 {
c24bdb02 11718 gdb_assert (m_builder == nullptr);
804d2729 11719 struct compunit_symtab *cust = tu_group->compunit_symtab;
c24bdb02
KS
11720 m_builder.reset (new struct buildsym_compunit
11721 (COMPUNIT_OBJFILE (cust), "",
11722 COMPUNIT_DIRNAME (cust),
11723 compunit_language (cust),
11724 0, cust));
f4dc4d17 11725
c24bdb02 11726 for (i = 0; i < line_header->file_names.size (); ++i)
f4dc4d17 11727 {
c24bdb02 11728 file_entry &fe = line_header->file_names[i];
f4dc4d17 11729
4c8aa72d 11730 fe.symtab = tu_group->symtabs[i];
f4dc4d17 11731 }
3019eac3
DE
11732 }
11733
f4dc4d17
DE
11734 /* The main symtab is allocated last. Type units don't have DW_AT_name
11735 so they don't have a "real" (so to speak) symtab anyway.
11736 There is later code that will assign the main symtab to all symbols
11737 that don't have one. We need to handle the case of a symbol with a
11738 missing symtab (DW_AT_decl_file) anyway. */
11739}
3019eac3 11740
f4dc4d17
DE
11741/* Process DW_TAG_type_unit.
11742 For TUs we want to skip the first top level sibling if it's not the
11743 actual type being defined by this TU. In this case the first top
11744 level sibling is there to provide context only. */
3019eac3 11745
f4dc4d17
DE
11746static void
11747read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
11748{
11749 struct die_info *child_die;
3019eac3 11750
f4dc4d17
DE
11751 prepare_one_comp_unit (cu, die, language_minimal);
11752
11753 /* Initialize (or reinitialize) the machinery for building symtabs.
11754 We do this before processing child DIEs, so that the line header table
11755 is available for DW_AT_decl_file. */
c24bdb02 11756 cu->setup_type_unit_groups (die);
f4dc4d17
DE
11757
11758 if (die->child != NULL)
11759 {
11760 child_die = die->child;
11761 while (child_die && child_die->tag)
11762 {
11763 process_die (child_die, cu);
11764 child_die = sibling_die (child_die);
11765 }
11766 }
3019eac3
DE
11767}
11768\f
80626a55
DE
11769/* DWO/DWP files.
11770
11771 http://gcc.gnu.org/wiki/DebugFission
11772 http://gcc.gnu.org/wiki/DebugFissionDWP
11773
11774 To simplify handling of both DWO files ("object" files with the DWARF info)
11775 and DWP files (a file with the DWOs packaged up into one file), we treat
11776 DWP files as having a collection of virtual DWO files. */
3019eac3
DE
11777
11778static hashval_t
11779hash_dwo_file (const void *item)
11780{
9a3c8263 11781 const struct dwo_file *dwo_file = (const struct dwo_file *) item;
a2ce51a0 11782 hashval_t hash;
3019eac3 11783
a2ce51a0
DE
11784 hash = htab_hash_string (dwo_file->dwo_name);
11785 if (dwo_file->comp_dir != NULL)
11786 hash += htab_hash_string (dwo_file->comp_dir);
11787 return hash;
3019eac3
DE
11788}
11789
11790static int
11791eq_dwo_file (const void *item_lhs, const void *item_rhs)
11792{
9a3c8263
SM
11793 const struct dwo_file *lhs = (const struct dwo_file *) item_lhs;
11794 const struct dwo_file *rhs = (const struct dwo_file *) item_rhs;
3019eac3 11795
a2ce51a0
DE
11796 if (strcmp (lhs->dwo_name, rhs->dwo_name) != 0)
11797 return 0;
11798 if (lhs->comp_dir == NULL || rhs->comp_dir == NULL)
11799 return lhs->comp_dir == rhs->comp_dir;
11800 return strcmp (lhs->comp_dir, rhs->comp_dir) == 0;
3019eac3
DE
11801}
11802
11803/* Allocate a hash table for DWO files. */
11804
11805static htab_t
ed2dc618 11806allocate_dwo_file_hash_table (struct objfile *objfile)
3019eac3 11807{
3019eac3
DE
11808 return htab_create_alloc_ex (41,
11809 hash_dwo_file,
11810 eq_dwo_file,
11811 NULL,
11812 &objfile->objfile_obstack,
11813 hashtab_obstack_allocate,
11814 dummy_obstack_deallocate);
11815}
11816
80626a55
DE
11817/* Lookup DWO file DWO_NAME. */
11818
11819static void **
ed2dc618
SM
11820lookup_dwo_file_slot (struct dwarf2_per_objfile *dwarf2_per_objfile,
11821 const char *dwo_name,
11822 const char *comp_dir)
80626a55
DE
11823{
11824 struct dwo_file find_entry;
11825 void **slot;
11826
11827 if (dwarf2_per_objfile->dwo_files == NULL)
ed2dc618
SM
11828 dwarf2_per_objfile->dwo_files
11829 = allocate_dwo_file_hash_table (dwarf2_per_objfile->objfile);
80626a55
DE
11830
11831 memset (&find_entry, 0, sizeof (find_entry));
0ac5b59e
DE
11832 find_entry.dwo_name = dwo_name;
11833 find_entry.comp_dir = comp_dir;
80626a55
DE
11834 slot = htab_find_slot (dwarf2_per_objfile->dwo_files, &find_entry, INSERT);
11835
11836 return slot;
11837}
11838
3019eac3
DE
11839static hashval_t
11840hash_dwo_unit (const void *item)
11841{
9a3c8263 11842 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
3019eac3
DE
11843
11844 /* This drops the top 32 bits of the id, but is ok for a hash. */
11845 return dwo_unit->signature;
11846}
11847
11848static int
11849eq_dwo_unit (const void *item_lhs, const void *item_rhs)
11850{
9a3c8263
SM
11851 const struct dwo_unit *lhs = (const struct dwo_unit *) item_lhs;
11852 const struct dwo_unit *rhs = (const struct dwo_unit *) item_rhs;
3019eac3
DE
11853
11854 /* The signature is assumed to be unique within the DWO file.
11855 So while object file CU dwo_id's always have the value zero,
11856 that's OK, assuming each object file DWO file has only one CU,
11857 and that's the rule for now. */
11858 return lhs->signature == rhs->signature;
11859}
11860
11861/* Allocate a hash table for DWO CUs,TUs.
11862 There is one of these tables for each of CUs,TUs for each DWO file. */
11863
11864static htab_t
11865allocate_dwo_unit_table (struct objfile *objfile)
11866{
11867 /* Start out with a pretty small number.
11868 Generally DWO files contain only one CU and maybe some TUs. */
11869 return htab_create_alloc_ex (3,
11870 hash_dwo_unit,
11871 eq_dwo_unit,
11872 NULL,
11873 &objfile->objfile_obstack,
11874 hashtab_obstack_allocate,
11875 dummy_obstack_deallocate);
11876}
11877
80626a55 11878/* Structure used to pass data to create_dwo_debug_info_hash_table_reader. */
3019eac3 11879
19c3d4c9 11880struct create_dwo_cu_data
3019eac3
DE
11881{
11882 struct dwo_file *dwo_file;
19c3d4c9 11883 struct dwo_unit dwo_unit;
3019eac3
DE
11884};
11885
19c3d4c9 11886/* die_reader_func for create_dwo_cu. */
3019eac3
DE
11887
11888static void
19c3d4c9
DE
11889create_dwo_cu_reader (const struct die_reader_specs *reader,
11890 const gdb_byte *info_ptr,
11891 struct die_info *comp_unit_die,
11892 int has_children,
11893 void *datap)
3019eac3
DE
11894{
11895 struct dwarf2_cu *cu = reader->cu;
9c541725 11896 sect_offset sect_off = cu->per_cu->sect_off;
8a0459fd 11897 struct dwarf2_section_info *section = cu->per_cu->section;
9a3c8263 11898 struct create_dwo_cu_data *data = (struct create_dwo_cu_data *) datap;
3019eac3 11899 struct dwo_file *dwo_file = data->dwo_file;
19c3d4c9 11900 struct dwo_unit *dwo_unit = &data->dwo_unit;
3019eac3 11901 struct attribute *attr;
3019eac3
DE
11902
11903 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
11904 if (attr == NULL)
11905 {
b98664d3 11906 complaint (_("Dwarf Error: debug entry at offset %s is missing"
19c3d4c9 11907 " its dwo_id [in module %s]"),
9d8780f0 11908 sect_offset_str (sect_off), dwo_file->dwo_name);
3019eac3
DE
11909 return;
11910 }
11911
3019eac3
DE
11912 dwo_unit->dwo_file = dwo_file;
11913 dwo_unit->signature = DW_UNSND (attr);
8a0459fd 11914 dwo_unit->section = section;
9c541725 11915 dwo_unit->sect_off = sect_off;
3019eac3
DE
11916 dwo_unit->length = cu->per_cu->length;
11917
b4f54984 11918 if (dwarf_read_debug)
9d8780f0
SM
11919 fprintf_unfiltered (gdb_stdlog, " offset %s, dwo_id %s\n",
11920 sect_offset_str (sect_off),
9c541725 11921 hex_string (dwo_unit->signature));
3019eac3
DE
11922}
11923
33c5cd75 11924/* Create the dwo_units for the CUs in a DWO_FILE.
19c3d4c9 11925 Note: This function processes DWO files only, not DWP files. */
3019eac3 11926
33c5cd75 11927static void
ed2dc618
SM
11928create_cus_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
11929 struct dwo_file &dwo_file, dwarf2_section_info &section,
33c5cd75 11930 htab_t &cus_htab)
3019eac3
DE
11931{
11932 struct objfile *objfile = dwarf2_per_objfile->objfile;
d521ce57 11933 const gdb_byte *info_ptr, *end_ptr;
3019eac3 11934
33c5cd75
DB
11935 dwarf2_read_section (objfile, &section);
11936 info_ptr = section.buffer;
3019eac3
DE
11937
11938 if (info_ptr == NULL)
33c5cd75 11939 return;
3019eac3 11940
b4f54984 11941 if (dwarf_read_debug)
19c3d4c9
DE
11942 {
11943 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
33c5cd75
DB
11944 get_section_name (&section),
11945 get_section_file_name (&section));
19c3d4c9 11946 }
3019eac3 11947
33c5cd75 11948 end_ptr = info_ptr + section.size;
3019eac3
DE
11949 while (info_ptr < end_ptr)
11950 {
11951 struct dwarf2_per_cu_data per_cu;
33c5cd75
DB
11952 struct create_dwo_cu_data create_dwo_cu_data;
11953 struct dwo_unit *dwo_unit;
11954 void **slot;
11955 sect_offset sect_off = (sect_offset) (info_ptr - section.buffer);
3019eac3 11956
19c3d4c9
DE
11957 memset (&create_dwo_cu_data.dwo_unit, 0,
11958 sizeof (create_dwo_cu_data.dwo_unit));
3019eac3 11959 memset (&per_cu, 0, sizeof (per_cu));
e3b94546 11960 per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
3019eac3 11961 per_cu.is_debug_types = 0;
33c5cd75
DB
11962 per_cu.sect_off = sect_offset (info_ptr - section.buffer);
11963 per_cu.section = &section;
c5ed0576 11964 create_dwo_cu_data.dwo_file = &dwo_file;
33c5cd75
DB
11965
11966 init_cutu_and_read_dies_no_follow (
11967 &per_cu, &dwo_file, create_dwo_cu_reader, &create_dwo_cu_data);
11968 info_ptr += per_cu.length;
11969
11970 // If the unit could not be parsed, skip it.
11971 if (create_dwo_cu_data.dwo_unit.dwo_file == NULL)
11972 continue;
3019eac3 11973
33c5cd75
DB
11974 if (cus_htab == NULL)
11975 cus_htab = allocate_dwo_unit_table (objfile);
19c3d4c9 11976
33c5cd75
DB
11977 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
11978 *dwo_unit = create_dwo_cu_data.dwo_unit;
11979 slot = htab_find_slot (cus_htab, dwo_unit, INSERT);
11980 gdb_assert (slot != NULL);
11981 if (*slot != NULL)
19c3d4c9 11982 {
33c5cd75
DB
11983 const struct dwo_unit *dup_cu = (const struct dwo_unit *)*slot;
11984 sect_offset dup_sect_off = dup_cu->sect_off;
19c3d4c9 11985
b98664d3 11986 complaint (_("debug cu entry at offset %s is duplicate to"
9d8780f0
SM
11987 " the entry at offset %s, signature %s"),
11988 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
33c5cd75 11989 hex_string (dwo_unit->signature));
19c3d4c9 11990 }
33c5cd75 11991 *slot = (void *)dwo_unit;
3019eac3 11992 }
3019eac3
DE
11993}
11994
80626a55
DE
11995/* DWP file .debug_{cu,tu}_index section format:
11996 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
11997
d2415c6c
DE
11998 DWP Version 1:
11999
80626a55
DE
12000 Both index sections have the same format, and serve to map a 64-bit
12001 signature to a set of section numbers. Each section begins with a header,
12002 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
12003 indexes, and a pool of 32-bit section numbers. The index sections will be
12004 aligned at 8-byte boundaries in the file.
12005
d2415c6c
DE
12006 The index section header consists of:
12007
12008 V, 32 bit version number
12009 -, 32 bits unused
12010 N, 32 bit number of compilation units or type units in the index
12011 M, 32 bit number of slots in the hash table
80626a55 12012
d2415c6c 12013 Numbers are recorded using the byte order of the application binary.
80626a55 12014
d2415c6c
DE
12015 The hash table begins at offset 16 in the section, and consists of an array
12016 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
12017 order of the application binary). Unused slots in the hash table are 0.
12018 (We rely on the extreme unlikeliness of a signature being exactly 0.)
80626a55 12019
d2415c6c
DE
12020 The parallel table begins immediately after the hash table
12021 (at offset 16 + 8 * M from the beginning of the section), and consists of an
12022 array of 32-bit indexes (using the byte order of the application binary),
12023 corresponding 1-1 with slots in the hash table. Each entry in the parallel
12024 table contains a 32-bit index into the pool of section numbers. For unused
12025 hash table slots, the corresponding entry in the parallel table will be 0.
80626a55 12026
73869dc2
DE
12027 The pool of section numbers begins immediately following the hash table
12028 (at offset 16 + 12 * M from the beginning of the section). The pool of
12029 section numbers consists of an array of 32-bit words (using the byte order
12030 of the application binary). Each item in the array is indexed starting
12031 from 0. The hash table entry provides the index of the first section
12032 number in the set. Additional section numbers in the set follow, and the
12033 set is terminated by a 0 entry (section number 0 is not used in ELF).
12034
12035 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
12036 section must be the first entry in the set, and the .debug_abbrev.dwo must
12037 be the second entry. Other members of the set may follow in any order.
12038
12039 ---
12040
12041 DWP Version 2:
12042
12043 DWP Version 2 combines all the .debug_info, etc. sections into one,
12044 and the entries in the index tables are now offsets into these sections.
12045 CU offsets begin at 0. TU offsets begin at the size of the .debug_info
12046 section.
12047
12048 Index Section Contents:
12049 Header
12050 Hash Table of Signatures dwp_hash_table.hash_table
12051 Parallel Table of Indices dwp_hash_table.unit_table
12052 Table of Section Offsets dwp_hash_table.v2.{section_ids,offsets}
12053 Table of Section Sizes dwp_hash_table.v2.sizes
12054
12055 The index section header consists of:
12056
12057 V, 32 bit version number
12058 L, 32 bit number of columns in the table of section offsets
12059 N, 32 bit number of compilation units or type units in the index
12060 M, 32 bit number of slots in the hash table
12061
12062 Numbers are recorded using the byte order of the application binary.
12063
12064 The hash table has the same format as version 1.
12065 The parallel table of indices has the same format as version 1,
12066 except that the entries are origin-1 indices into the table of sections
12067 offsets and the table of section sizes.
12068
12069 The table of offsets begins immediately following the parallel table
12070 (at offset 16 + 12 * M from the beginning of the section). The table is
12071 a two-dimensional array of 32-bit words (using the byte order of the
12072 application binary), with L columns and N+1 rows, in row-major order.
12073 Each row in the array is indexed starting from 0. The first row provides
12074 a key to the remaining rows: each column in this row provides an identifier
12075 for a debug section, and the offsets in the same column of subsequent rows
12076 refer to that section. The section identifiers are:
12077
12078 DW_SECT_INFO 1 .debug_info.dwo
12079 DW_SECT_TYPES 2 .debug_types.dwo
12080 DW_SECT_ABBREV 3 .debug_abbrev.dwo
12081 DW_SECT_LINE 4 .debug_line.dwo
12082 DW_SECT_LOC 5 .debug_loc.dwo
12083 DW_SECT_STR_OFFSETS 6 .debug_str_offsets.dwo
12084 DW_SECT_MACINFO 7 .debug_macinfo.dwo
12085 DW_SECT_MACRO 8 .debug_macro.dwo
12086
12087 The offsets provided by the CU and TU index sections are the base offsets
12088 for the contributions made by each CU or TU to the corresponding section
12089 in the package file. Each CU and TU header contains an abbrev_offset
12090 field, used to find the abbreviations table for that CU or TU within the
12091 contribution to the .debug_abbrev.dwo section for that CU or TU, and should
12092 be interpreted as relative to the base offset given in the index section.
12093 Likewise, offsets into .debug_line.dwo from DW_AT_stmt_list attributes
12094 should be interpreted as relative to the base offset for .debug_line.dwo,
12095 and offsets into other debug sections obtained from DWARF attributes should
12096 also be interpreted as relative to the corresponding base offset.
12097
12098 The table of sizes begins immediately following the table of offsets.
12099 Like the table of offsets, it is a two-dimensional array of 32-bit words,
12100 with L columns and N rows, in row-major order. Each row in the array is
12101 indexed starting from 1 (row 0 is shared by the two tables).
12102
12103 ---
12104
12105 Hash table lookup is handled the same in version 1 and 2:
12106
12107 We assume that N and M will not exceed 2^32 - 1.
12108 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
12109
d2415c6c
DE
12110 Given a 64-bit compilation unit signature or a type signature S, an entry
12111 in the hash table is located as follows:
80626a55 12112
d2415c6c
DE
12113 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
12114 the low-order k bits all set to 1.
80626a55 12115
d2415c6c 12116 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
80626a55 12117
d2415c6c
DE
12118 3) If the hash table entry at index H matches the signature, use that
12119 entry. If the hash table entry at index H is unused (all zeroes),
12120 terminate the search: the signature is not present in the table.
80626a55 12121
d2415c6c 12122 4) Let H = (H + H') modulo M. Repeat at Step 3.
80626a55 12123
d2415c6c 12124 Because M > N and H' and M are relatively prime, the search is guaranteed
73869dc2 12125 to stop at an unused slot or find the match. */
80626a55
DE
12126
12127/* Create a hash table to map DWO IDs to their CU/TU entry in
12128 .debug_{info,types}.dwo in DWP_FILE.
12129 Returns NULL if there isn't one.
12130 Note: This function processes DWP files only, not DWO files. */
12131
12132static struct dwp_hash_table *
ed2dc618
SM
12133create_dwp_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
12134 struct dwp_file *dwp_file, int is_debug_types)
80626a55
DE
12135{
12136 struct objfile *objfile = dwarf2_per_objfile->objfile;
400174b1 12137 bfd *dbfd = dwp_file->dbfd.get ();
948f8e3d 12138 const gdb_byte *index_ptr, *index_end;
80626a55 12139 struct dwarf2_section_info *index;
73869dc2 12140 uint32_t version, nr_columns, nr_units, nr_slots;
80626a55
DE
12141 struct dwp_hash_table *htab;
12142
12143 if (is_debug_types)
12144 index = &dwp_file->sections.tu_index;
12145 else
12146 index = &dwp_file->sections.cu_index;
12147
12148 if (dwarf2_section_empty_p (index))
12149 return NULL;
12150 dwarf2_read_section (objfile, index);
12151
12152 index_ptr = index->buffer;
12153 index_end = index_ptr + index->size;
12154
12155 version = read_4_bytes (dbfd, index_ptr);
73869dc2
DE
12156 index_ptr += 4;
12157 if (version == 2)
12158 nr_columns = read_4_bytes (dbfd, index_ptr);
12159 else
12160 nr_columns = 0;
12161 index_ptr += 4;
80626a55
DE
12162 nr_units = read_4_bytes (dbfd, index_ptr);
12163 index_ptr += 4;
12164 nr_slots = read_4_bytes (dbfd, index_ptr);
12165 index_ptr += 4;
12166
73869dc2 12167 if (version != 1 && version != 2)
80626a55 12168 {
21aa081e 12169 error (_("Dwarf Error: unsupported DWP file version (%s)"
80626a55 12170 " [in module %s]"),
21aa081e 12171 pulongest (version), dwp_file->name);
80626a55
DE
12172 }
12173 if (nr_slots != (nr_slots & -nr_slots))
12174 {
21aa081e 12175 error (_("Dwarf Error: number of slots in DWP hash table (%s)"
80626a55 12176 " is not power of 2 [in module %s]"),
21aa081e 12177 pulongest (nr_slots), dwp_file->name);
80626a55
DE
12178 }
12179
12180 htab = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_hash_table);
73869dc2
DE
12181 htab->version = version;
12182 htab->nr_columns = nr_columns;
80626a55
DE
12183 htab->nr_units = nr_units;
12184 htab->nr_slots = nr_slots;
12185 htab->hash_table = index_ptr;
12186 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
73869dc2
DE
12187
12188 /* Exit early if the table is empty. */
12189 if (nr_slots == 0 || nr_units == 0
12190 || (version == 2 && nr_columns == 0))
12191 {
12192 /* All must be zero. */
12193 if (nr_slots != 0 || nr_units != 0
12194 || (version == 2 && nr_columns != 0))
12195 {
b98664d3 12196 complaint (_("Empty DWP but nr_slots,nr_units,nr_columns not"
73869dc2
DE
12197 " all zero [in modules %s]"),
12198 dwp_file->name);
12199 }
12200 return htab;
12201 }
12202
12203 if (version == 1)
12204 {
12205 htab->section_pool.v1.indices =
12206 htab->unit_table + sizeof (uint32_t) * nr_slots;
12207 /* It's harder to decide whether the section is too small in v1.
12208 V1 is deprecated anyway so we punt. */
12209 }
12210 else
12211 {
12212 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
12213 int *ids = htab->section_pool.v2.section_ids;
04fd5eed 12214 size_t sizeof_ids = sizeof (htab->section_pool.v2.section_ids);
73869dc2
DE
12215 /* Reverse map for error checking. */
12216 int ids_seen[DW_SECT_MAX + 1];
12217 int i;
12218
12219 if (nr_columns < 2)
12220 {
12221 error (_("Dwarf Error: bad DWP hash table, too few columns"
12222 " in section table [in module %s]"),
12223 dwp_file->name);
12224 }
12225 if (nr_columns > MAX_NR_V2_DWO_SECTIONS)
12226 {
12227 error (_("Dwarf Error: bad DWP hash table, too many columns"
12228 " in section table [in module %s]"),
12229 dwp_file->name);
12230 }
04fd5eed
GB
12231 memset (ids, 255, sizeof_ids);
12232 memset (ids_seen, 255, sizeof (ids_seen));
73869dc2
DE
12233 for (i = 0; i < nr_columns; ++i)
12234 {
12235 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
12236
12237 if (id < DW_SECT_MIN || id > DW_SECT_MAX)
12238 {
12239 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
12240 " in section table [in module %s]"),
12241 id, dwp_file->name);
12242 }
12243 if (ids_seen[id] != -1)
12244 {
12245 error (_("Dwarf Error: bad DWP hash table, duplicate section"
12246 " id %d in section table [in module %s]"),
12247 id, dwp_file->name);
12248 }
12249 ids_seen[id] = i;
12250 ids[i] = id;
12251 }
12252 /* Must have exactly one info or types section. */
12253 if (((ids_seen[DW_SECT_INFO] != -1)
12254 + (ids_seen[DW_SECT_TYPES] != -1))
12255 != 1)
12256 {
12257 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
12258 " DWO info/types section [in module %s]"),
12259 dwp_file->name);
12260 }
12261 /* Must have an abbrev section. */
12262 if (ids_seen[DW_SECT_ABBREV] == -1)
12263 {
12264 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
12265 " section [in module %s]"),
12266 dwp_file->name);
12267 }
12268 htab->section_pool.v2.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
12269 htab->section_pool.v2.sizes =
12270 htab->section_pool.v2.offsets + (sizeof (uint32_t)
12271 * nr_units * nr_columns);
12272 if ((htab->section_pool.v2.sizes + (sizeof (uint32_t)
12273 * nr_units * nr_columns))
12274 > index_end)
12275 {
12276 error (_("Dwarf Error: DWP index section is corrupt (too small)"
12277 " [in module %s]"),
12278 dwp_file->name);
12279 }
12280 }
80626a55
DE
12281
12282 return htab;
12283}
12284
12285/* Update SECTIONS with the data from SECTP.
12286
12287 This function is like the other "locate" section routines that are
12288 passed to bfd_map_over_sections, but in this context the sections to
73869dc2 12289 read comes from the DWP V1 hash table, not the full ELF section table.
80626a55
DE
12290
12291 The result is non-zero for success, or zero if an error was found. */
12292
12293static int
73869dc2
DE
12294locate_v1_virtual_dwo_sections (asection *sectp,
12295 struct virtual_v1_dwo_sections *sections)
80626a55
DE
12296{
12297 const struct dwop_section_names *names = &dwop_section_names;
12298
12299 if (section_is_p (sectp->name, &names->abbrev_dwo))
12300 {
12301 /* There can be only one. */
049412e3 12302 if (sections->abbrev.s.section != NULL)
80626a55 12303 return 0;
049412e3 12304 sections->abbrev.s.section = sectp;
80626a55
DE
12305 sections->abbrev.size = bfd_get_section_size (sectp);
12306 }
12307 else if (section_is_p (sectp->name, &names->info_dwo)
12308 || section_is_p (sectp->name, &names->types_dwo))
12309 {
12310 /* There can be only one. */
049412e3 12311 if (sections->info_or_types.s.section != NULL)
80626a55 12312 return 0;
049412e3 12313 sections->info_or_types.s.section = sectp;
80626a55
DE
12314 sections->info_or_types.size = bfd_get_section_size (sectp);
12315 }
12316 else if (section_is_p (sectp->name, &names->line_dwo))
12317 {
12318 /* There can be only one. */
049412e3 12319 if (sections->line.s.section != NULL)
80626a55 12320 return 0;
049412e3 12321 sections->line.s.section = sectp;
80626a55
DE
12322 sections->line.size = bfd_get_section_size (sectp);
12323 }
12324 else if (section_is_p (sectp->name, &names->loc_dwo))
12325 {
12326 /* There can be only one. */
049412e3 12327 if (sections->loc.s.section != NULL)
80626a55 12328 return 0;
049412e3 12329 sections->loc.s.section = sectp;
80626a55
DE
12330 sections->loc.size = bfd_get_section_size (sectp);
12331 }
12332 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12333 {
12334 /* There can be only one. */
049412e3 12335 if (sections->macinfo.s.section != NULL)
80626a55 12336 return 0;
049412e3 12337 sections->macinfo.s.section = sectp;
80626a55
DE
12338 sections->macinfo.size = bfd_get_section_size (sectp);
12339 }
12340 else if (section_is_p (sectp->name, &names->macro_dwo))
12341 {
12342 /* There can be only one. */
049412e3 12343 if (sections->macro.s.section != NULL)
80626a55 12344 return 0;
049412e3 12345 sections->macro.s.section = sectp;
80626a55
DE
12346 sections->macro.size = bfd_get_section_size (sectp);
12347 }
12348 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12349 {
12350 /* There can be only one. */
049412e3 12351 if (sections->str_offsets.s.section != NULL)
80626a55 12352 return 0;
049412e3 12353 sections->str_offsets.s.section = sectp;
80626a55
DE
12354 sections->str_offsets.size = bfd_get_section_size (sectp);
12355 }
12356 else
12357 {
12358 /* No other kind of section is valid. */
12359 return 0;
12360 }
12361
12362 return 1;
12363}
12364
73869dc2
DE
12365/* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
12366 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
12367 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
12368 This is for DWP version 1 files. */
80626a55
DE
12369
12370static struct dwo_unit *
ed2dc618
SM
12371create_dwo_unit_in_dwp_v1 (struct dwarf2_per_objfile *dwarf2_per_objfile,
12372 struct dwp_file *dwp_file,
73869dc2
DE
12373 uint32_t unit_index,
12374 const char *comp_dir,
12375 ULONGEST signature, int is_debug_types)
80626a55
DE
12376{
12377 struct objfile *objfile = dwarf2_per_objfile->objfile;
73869dc2
DE
12378 const struct dwp_hash_table *dwp_htab =
12379 is_debug_types ? dwp_file->tus : dwp_file->cus;
400174b1 12380 bfd *dbfd = dwp_file->dbfd.get ();
80626a55
DE
12381 const char *kind = is_debug_types ? "TU" : "CU";
12382 struct dwo_file *dwo_file;
12383 struct dwo_unit *dwo_unit;
73869dc2 12384 struct virtual_v1_dwo_sections sections;
80626a55 12385 void **dwo_file_slot;
80626a55
DE
12386 int i;
12387
73869dc2
DE
12388 gdb_assert (dwp_file->version == 1);
12389
b4f54984 12390 if (dwarf_read_debug)
80626a55 12391 {
73869dc2 12392 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V1 file: %s\n",
80626a55 12393 kind,
73869dc2 12394 pulongest (unit_index), hex_string (signature),
80626a55
DE
12395 dwp_file->name);
12396 }
12397
19ac8c2e 12398 /* Fetch the sections of this DWO unit.
80626a55
DE
12399 Put a limit on the number of sections we look for so that bad data
12400 doesn't cause us to loop forever. */
12401
73869dc2 12402#define MAX_NR_V1_DWO_SECTIONS \
80626a55
DE
12403 (1 /* .debug_info or .debug_types */ \
12404 + 1 /* .debug_abbrev */ \
12405 + 1 /* .debug_line */ \
12406 + 1 /* .debug_loc */ \
12407 + 1 /* .debug_str_offsets */ \
19ac8c2e 12408 + 1 /* .debug_macro or .debug_macinfo */ \
80626a55
DE
12409 + 1 /* trailing zero */)
12410
12411 memset (&sections, 0, sizeof (sections));
80626a55 12412
73869dc2 12413 for (i = 0; i < MAX_NR_V1_DWO_SECTIONS; ++i)
80626a55
DE
12414 {
12415 asection *sectp;
12416 uint32_t section_nr =
12417 read_4_bytes (dbfd,
73869dc2
DE
12418 dwp_htab->section_pool.v1.indices
12419 + (unit_index + i) * sizeof (uint32_t));
80626a55
DE
12420
12421 if (section_nr == 0)
12422 break;
12423 if (section_nr >= dwp_file->num_sections)
12424 {
12425 error (_("Dwarf Error: bad DWP hash table, section number too large"
12426 " [in module %s]"),
12427 dwp_file->name);
12428 }
12429
12430 sectp = dwp_file->elf_sections[section_nr];
73869dc2 12431 if (! locate_v1_virtual_dwo_sections (sectp, &sections))
80626a55
DE
12432 {
12433 error (_("Dwarf Error: bad DWP hash table, invalid section found"
12434 " [in module %s]"),
12435 dwp_file->name);
12436 }
12437 }
12438
12439 if (i < 2
a32a8923
DE
12440 || dwarf2_section_empty_p (&sections.info_or_types)
12441 || dwarf2_section_empty_p (&sections.abbrev))
80626a55
DE
12442 {
12443 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
12444 " [in module %s]"),
12445 dwp_file->name);
12446 }
73869dc2 12447 if (i == MAX_NR_V1_DWO_SECTIONS)
80626a55
DE
12448 {
12449 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
12450 " [in module %s]"),
12451 dwp_file->name);
12452 }
12453
12454 /* It's easier for the rest of the code if we fake a struct dwo_file and
12455 have dwo_unit "live" in that. At least for now.
12456
12457 The DWP file can be made up of a random collection of CUs and TUs.
c766f7ec 12458 However, for each CU + set of TUs that came from the same original DWO
57d63ce2
DE
12459 file, we can combine them back into a virtual DWO file to save space
12460 (fewer struct dwo_file objects to allocate). Remember that for really
80626a55
DE
12461 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
12462
791afaa2
TT
12463 std::string virtual_dwo_name =
12464 string_printf ("virtual-dwo/%d-%d-%d-%d",
12465 get_section_id (&sections.abbrev),
12466 get_section_id (&sections.line),
12467 get_section_id (&sections.loc),
12468 get_section_id (&sections.str_offsets));
80626a55 12469 /* Can we use an existing virtual DWO file? */
ed2dc618
SM
12470 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
12471 virtual_dwo_name.c_str (),
12472 comp_dir);
80626a55
DE
12473 /* Create one if necessary. */
12474 if (*dwo_file_slot == NULL)
12475 {
b4f54984 12476 if (dwarf_read_debug)
80626a55
DE
12477 {
12478 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
791afaa2 12479 virtual_dwo_name.c_str ());
80626a55
DE
12480 }
12481 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
224c3ddb
SM
12482 dwo_file->dwo_name
12483 = (const char *) obstack_copy0 (&objfile->objfile_obstack,
791afaa2
TT
12484 virtual_dwo_name.c_str (),
12485 virtual_dwo_name.size ());
0ac5b59e 12486 dwo_file->comp_dir = comp_dir;
80626a55
DE
12487 dwo_file->sections.abbrev = sections.abbrev;
12488 dwo_file->sections.line = sections.line;
12489 dwo_file->sections.loc = sections.loc;
12490 dwo_file->sections.macinfo = sections.macinfo;
12491 dwo_file->sections.macro = sections.macro;
12492 dwo_file->sections.str_offsets = sections.str_offsets;
12493 /* The "str" section is global to the entire DWP file. */
12494 dwo_file->sections.str = dwp_file->sections.str;
57d63ce2 12495 /* The info or types section is assigned below to dwo_unit,
80626a55
DE
12496 there's no need to record it in dwo_file.
12497 Also, we can't simply record type sections in dwo_file because
12498 we record a pointer into the vector in dwo_unit. As we collect more
12499 types we'll grow the vector and eventually have to reallocate space
57d63ce2
DE
12500 for it, invalidating all copies of pointers into the previous
12501 contents. */
80626a55
DE
12502 *dwo_file_slot = dwo_file;
12503 }
12504 else
12505 {
b4f54984 12506 if (dwarf_read_debug)
80626a55
DE
12507 {
12508 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
791afaa2 12509 virtual_dwo_name.c_str ());
80626a55 12510 }
9a3c8263 12511 dwo_file = (struct dwo_file *) *dwo_file_slot;
80626a55 12512 }
80626a55
DE
12513
12514 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
12515 dwo_unit->dwo_file = dwo_file;
12516 dwo_unit->signature = signature;
8d749320
SM
12517 dwo_unit->section =
12518 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
8a0459fd 12519 *dwo_unit->section = sections.info_or_types;
57d63ce2 12520 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
80626a55
DE
12521
12522 return dwo_unit;
12523}
12524
73869dc2
DE
12525/* Subroutine of create_dwo_unit_in_dwp_v2 to simplify it.
12526 Given a pointer to the containing section SECTION, and OFFSET,SIZE of the
12527 piece within that section used by a TU/CU, return a virtual section
12528 of just that piece. */
12529
12530static struct dwarf2_section_info
ed2dc618
SM
12531create_dwp_v2_section (struct dwarf2_per_objfile *dwarf2_per_objfile,
12532 struct dwarf2_section_info *section,
73869dc2
DE
12533 bfd_size_type offset, bfd_size_type size)
12534{
12535 struct dwarf2_section_info result;
12536 asection *sectp;
12537
12538 gdb_assert (section != NULL);
12539 gdb_assert (!section->is_virtual);
12540
12541 memset (&result, 0, sizeof (result));
12542 result.s.containing_section = section;
12543 result.is_virtual = 1;
12544
12545 if (size == 0)
12546 return result;
12547
12548 sectp = get_section_bfd_section (section);
12549
12550 /* Flag an error if the piece denoted by OFFSET,SIZE is outside the
12551 bounds of the real section. This is a pretty-rare event, so just
12552 flag an error (easier) instead of a warning and trying to cope. */
12553 if (sectp == NULL
12554 || offset + size > bfd_get_section_size (sectp))
12555 {
73869dc2
DE
12556 error (_("Dwarf Error: Bad DWP V2 section info, doesn't fit"
12557 " in section %s [in module %s]"),
12558 sectp ? bfd_section_name (abfd, sectp) : "<unknown>",
12559 objfile_name (dwarf2_per_objfile->objfile));
12560 }
12561
12562 result.virtual_offset = offset;
12563 result.size = size;
12564 return result;
12565}
12566
12567/* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
12568 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
12569 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
12570 This is for DWP version 2 files. */
12571
12572static struct dwo_unit *
ed2dc618
SM
12573create_dwo_unit_in_dwp_v2 (struct dwarf2_per_objfile *dwarf2_per_objfile,
12574 struct dwp_file *dwp_file,
73869dc2
DE
12575 uint32_t unit_index,
12576 const char *comp_dir,
12577 ULONGEST signature, int is_debug_types)
12578{
12579 struct objfile *objfile = dwarf2_per_objfile->objfile;
12580 const struct dwp_hash_table *dwp_htab =
12581 is_debug_types ? dwp_file->tus : dwp_file->cus;
400174b1 12582 bfd *dbfd = dwp_file->dbfd.get ();
73869dc2
DE
12583 const char *kind = is_debug_types ? "TU" : "CU";
12584 struct dwo_file *dwo_file;
12585 struct dwo_unit *dwo_unit;
12586 struct virtual_v2_dwo_sections sections;
12587 void **dwo_file_slot;
73869dc2
DE
12588 int i;
12589
12590 gdb_assert (dwp_file->version == 2);
12591
b4f54984 12592 if (dwarf_read_debug)
73869dc2
DE
12593 {
12594 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V2 file: %s\n",
12595 kind,
12596 pulongest (unit_index), hex_string (signature),
12597 dwp_file->name);
12598 }
12599
12600 /* Fetch the section offsets of this DWO unit. */
12601
12602 memset (&sections, 0, sizeof (sections));
73869dc2
DE
12603
12604 for (i = 0; i < dwp_htab->nr_columns; ++i)
12605 {
12606 uint32_t offset = read_4_bytes (dbfd,
12607 dwp_htab->section_pool.v2.offsets
12608 + (((unit_index - 1) * dwp_htab->nr_columns
12609 + i)
12610 * sizeof (uint32_t)));
12611 uint32_t size = read_4_bytes (dbfd,
12612 dwp_htab->section_pool.v2.sizes
12613 + (((unit_index - 1) * dwp_htab->nr_columns
12614 + i)
12615 * sizeof (uint32_t)));
12616
12617 switch (dwp_htab->section_pool.v2.section_ids[i])
12618 {
12619 case DW_SECT_INFO:
12620 case DW_SECT_TYPES:
12621 sections.info_or_types_offset = offset;
12622 sections.info_or_types_size = size;
12623 break;
12624 case DW_SECT_ABBREV:
12625 sections.abbrev_offset = offset;
12626 sections.abbrev_size = size;
12627 break;
12628 case DW_SECT_LINE:
12629 sections.line_offset = offset;
12630 sections.line_size = size;
12631 break;
12632 case DW_SECT_LOC:
12633 sections.loc_offset = offset;
12634 sections.loc_size = size;
12635 break;
12636 case DW_SECT_STR_OFFSETS:
12637 sections.str_offsets_offset = offset;
12638 sections.str_offsets_size = size;
12639 break;
12640 case DW_SECT_MACINFO:
12641 sections.macinfo_offset = offset;
12642 sections.macinfo_size = size;
12643 break;
12644 case DW_SECT_MACRO:
12645 sections.macro_offset = offset;
12646 sections.macro_size = size;
12647 break;
12648 }
12649 }
12650
12651 /* It's easier for the rest of the code if we fake a struct dwo_file and
12652 have dwo_unit "live" in that. At least for now.
12653
12654 The DWP file can be made up of a random collection of CUs and TUs.
12655 However, for each CU + set of TUs that came from the same original DWO
12656 file, we can combine them back into a virtual DWO file to save space
12657 (fewer struct dwo_file objects to allocate). Remember that for really
12658 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
12659
791afaa2
TT
12660 std::string virtual_dwo_name =
12661 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld",
12662 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
12663 (long) (sections.line_size ? sections.line_offset : 0),
12664 (long) (sections.loc_size ? sections.loc_offset : 0),
12665 (long) (sections.str_offsets_size
12666 ? sections.str_offsets_offset : 0));
73869dc2 12667 /* Can we use an existing virtual DWO file? */
ed2dc618
SM
12668 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
12669 virtual_dwo_name.c_str (),
12670 comp_dir);
73869dc2
DE
12671 /* Create one if necessary. */
12672 if (*dwo_file_slot == NULL)
12673 {
b4f54984 12674 if (dwarf_read_debug)
73869dc2
DE
12675 {
12676 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
791afaa2 12677 virtual_dwo_name.c_str ());
73869dc2
DE
12678 }
12679 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
224c3ddb
SM
12680 dwo_file->dwo_name
12681 = (const char *) obstack_copy0 (&objfile->objfile_obstack,
791afaa2
TT
12682 virtual_dwo_name.c_str (),
12683 virtual_dwo_name.size ());
73869dc2
DE
12684 dwo_file->comp_dir = comp_dir;
12685 dwo_file->sections.abbrev =
ed2dc618 12686 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.abbrev,
73869dc2
DE
12687 sections.abbrev_offset, sections.abbrev_size);
12688 dwo_file->sections.line =
ed2dc618 12689 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.line,
73869dc2
DE
12690 sections.line_offset, sections.line_size);
12691 dwo_file->sections.loc =
ed2dc618 12692 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.loc,
73869dc2
DE
12693 sections.loc_offset, sections.loc_size);
12694 dwo_file->sections.macinfo =
ed2dc618 12695 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.macinfo,
73869dc2
DE
12696 sections.macinfo_offset, sections.macinfo_size);
12697 dwo_file->sections.macro =
ed2dc618 12698 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.macro,
73869dc2
DE
12699 sections.macro_offset, sections.macro_size);
12700 dwo_file->sections.str_offsets =
ed2dc618
SM
12701 create_dwp_v2_section (dwarf2_per_objfile,
12702 &dwp_file->sections.str_offsets,
73869dc2
DE
12703 sections.str_offsets_offset,
12704 sections.str_offsets_size);
12705 /* The "str" section is global to the entire DWP file. */
12706 dwo_file->sections.str = dwp_file->sections.str;
12707 /* The info or types section is assigned below to dwo_unit,
12708 there's no need to record it in dwo_file.
12709 Also, we can't simply record type sections in dwo_file because
12710 we record a pointer into the vector in dwo_unit. As we collect more
12711 types we'll grow the vector and eventually have to reallocate space
12712 for it, invalidating all copies of pointers into the previous
12713 contents. */
12714 *dwo_file_slot = dwo_file;
12715 }
12716 else
12717 {
b4f54984 12718 if (dwarf_read_debug)
73869dc2
DE
12719 {
12720 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
791afaa2 12721 virtual_dwo_name.c_str ());
73869dc2 12722 }
9a3c8263 12723 dwo_file = (struct dwo_file *) *dwo_file_slot;
73869dc2 12724 }
73869dc2
DE
12725
12726 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
12727 dwo_unit->dwo_file = dwo_file;
12728 dwo_unit->signature = signature;
8d749320
SM
12729 dwo_unit->section =
12730 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
ed2dc618
SM
12731 *dwo_unit->section = create_dwp_v2_section (dwarf2_per_objfile,
12732 is_debug_types
73869dc2
DE
12733 ? &dwp_file->sections.types
12734 : &dwp_file->sections.info,
12735 sections.info_or_types_offset,
12736 sections.info_or_types_size);
12737 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
12738
12739 return dwo_unit;
12740}
12741
57d63ce2
DE
12742/* Lookup the DWO unit with SIGNATURE in DWP_FILE.
12743 Returns NULL if the signature isn't found. */
80626a55
DE
12744
12745static struct dwo_unit *
ed2dc618
SM
12746lookup_dwo_unit_in_dwp (struct dwarf2_per_objfile *dwarf2_per_objfile,
12747 struct dwp_file *dwp_file, const char *comp_dir,
57d63ce2 12748 ULONGEST signature, int is_debug_types)
80626a55 12749{
57d63ce2
DE
12750 const struct dwp_hash_table *dwp_htab =
12751 is_debug_types ? dwp_file->tus : dwp_file->cus;
400174b1 12752 bfd *dbfd = dwp_file->dbfd.get ();
57d63ce2 12753 uint32_t mask = dwp_htab->nr_slots - 1;
80626a55
DE
12754 uint32_t hash = signature & mask;
12755 uint32_t hash2 = ((signature >> 32) & mask) | 1;
12756 unsigned int i;
12757 void **slot;
870f88f7 12758 struct dwo_unit find_dwo_cu;
80626a55
DE
12759
12760 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
12761 find_dwo_cu.signature = signature;
19ac8c2e
DE
12762 slot = htab_find_slot (is_debug_types
12763 ? dwp_file->loaded_tus
12764 : dwp_file->loaded_cus,
12765 &find_dwo_cu, INSERT);
80626a55
DE
12766
12767 if (*slot != NULL)
9a3c8263 12768 return (struct dwo_unit *) *slot;
80626a55
DE
12769
12770 /* Use a for loop so that we don't loop forever on bad debug info. */
57d63ce2 12771 for (i = 0; i < dwp_htab->nr_slots; ++i)
80626a55
DE
12772 {
12773 ULONGEST signature_in_table;
12774
12775 signature_in_table =
57d63ce2 12776 read_8_bytes (dbfd, dwp_htab->hash_table + hash * sizeof (uint64_t));
80626a55
DE
12777 if (signature_in_table == signature)
12778 {
57d63ce2
DE
12779 uint32_t unit_index =
12780 read_4_bytes (dbfd,
12781 dwp_htab->unit_table + hash * sizeof (uint32_t));
80626a55 12782
73869dc2
DE
12783 if (dwp_file->version == 1)
12784 {
ed2dc618
SM
12785 *slot = create_dwo_unit_in_dwp_v1 (dwarf2_per_objfile,
12786 dwp_file, unit_index,
73869dc2
DE
12787 comp_dir, signature,
12788 is_debug_types);
12789 }
12790 else
12791 {
ed2dc618
SM
12792 *slot = create_dwo_unit_in_dwp_v2 (dwarf2_per_objfile,
12793 dwp_file, unit_index,
73869dc2
DE
12794 comp_dir, signature,
12795 is_debug_types);
12796 }
9a3c8263 12797 return (struct dwo_unit *) *slot;
80626a55
DE
12798 }
12799 if (signature_in_table == 0)
12800 return NULL;
12801 hash = (hash + hash2) & mask;
12802 }
12803
12804 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
12805 " [in module %s]"),
12806 dwp_file->name);
12807}
12808
ab5088bf 12809/* Subroutine of open_dwo_file,open_dwp_file to simplify them.
3019eac3
DE
12810 Open the file specified by FILE_NAME and hand it off to BFD for
12811 preliminary analysis. Return a newly initialized bfd *, which
12812 includes a canonicalized copy of FILE_NAME.
80626a55 12813 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
6ac97d4c
DE
12814 SEARCH_CWD is true if the current directory is to be searched.
12815 It will be searched before debug-file-directory.
13aaf454
DE
12816 If successful, the file is added to the bfd include table of the
12817 objfile's bfd (see gdb_bfd_record_inclusion).
6ac97d4c 12818 If unable to find/open the file, return NULL.
3019eac3
DE
12819 NOTE: This function is derived from symfile_bfd_open. */
12820
192b62ce 12821static gdb_bfd_ref_ptr
ed2dc618
SM
12822try_open_dwop_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12823 const char *file_name, int is_dwp, int search_cwd)
3019eac3 12824{
24b9144d 12825 int desc;
9c02c129
DE
12826 /* Blech. OPF_TRY_CWD_FIRST also disables searching the path list if
12827 FILE_NAME contains a '/'. So we can't use it. Instead prepend "."
12828 to debug_file_directory. */
e0cc99a6 12829 const char *search_path;
9c02c129
DE
12830 static const char dirname_separator_string[] = { DIRNAME_SEPARATOR, '\0' };
12831
e0cc99a6 12832 gdb::unique_xmalloc_ptr<char> search_path_holder;
6ac97d4c
DE
12833 if (search_cwd)
12834 {
12835 if (*debug_file_directory != '\0')
e0cc99a6
TT
12836 {
12837 search_path_holder.reset (concat (".", dirname_separator_string,
12838 debug_file_directory,
12839 (char *) NULL));
12840 search_path = search_path_holder.get ();
12841 }
6ac97d4c 12842 else
e0cc99a6 12843 search_path = ".";
6ac97d4c 12844 }
9c02c129 12845 else
e0cc99a6 12846 search_path = debug_file_directory;
3019eac3 12847
24b9144d 12848 openp_flags flags = OPF_RETURN_REALPATH;
80626a55
DE
12849 if (is_dwp)
12850 flags |= OPF_SEARCH_IN_PATH;
e0cc99a6
TT
12851
12852 gdb::unique_xmalloc_ptr<char> absolute_name;
9c02c129 12853 desc = openp (search_path, flags, file_name,
3019eac3
DE
12854 O_RDONLY | O_BINARY, &absolute_name);
12855 if (desc < 0)
12856 return NULL;
12857
e0cc99a6
TT
12858 gdb_bfd_ref_ptr sym_bfd (gdb_bfd_open (absolute_name.get (),
12859 gnutarget, desc));
9c02c129
DE
12860 if (sym_bfd == NULL)
12861 return NULL;
192b62ce 12862 bfd_set_cacheable (sym_bfd.get (), 1);
3019eac3 12863
192b62ce
TT
12864 if (!bfd_check_format (sym_bfd.get (), bfd_object))
12865 return NULL;
3019eac3 12866
13aaf454
DE
12867 /* Success. Record the bfd as having been included by the objfile's bfd.
12868 This is important because things like demangled_names_hash lives in the
12869 objfile's per_bfd space and may have references to things like symbol
12870 names that live in the DWO/DWP file's per_bfd space. PR 16426. */
192b62ce 12871 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd, sym_bfd.get ());
13aaf454 12872
3019eac3
DE
12873 return sym_bfd;
12874}
12875
ab5088bf 12876/* Try to open DWO file FILE_NAME.
3019eac3
DE
12877 COMP_DIR is the DW_AT_comp_dir attribute.
12878 The result is the bfd handle of the file.
12879 If there is a problem finding or opening the file, return NULL.
12880 Upon success, the canonicalized path of the file is stored in the bfd,
12881 same as symfile_bfd_open. */
12882
192b62ce 12883static gdb_bfd_ref_ptr
ed2dc618
SM
12884open_dwo_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12885 const char *file_name, const char *comp_dir)
3019eac3 12886{
80626a55 12887 if (IS_ABSOLUTE_PATH (file_name))
ed2dc618
SM
12888 return try_open_dwop_file (dwarf2_per_objfile, file_name,
12889 0 /*is_dwp*/, 0 /*search_cwd*/);
3019eac3
DE
12890
12891 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
12892
12893 if (comp_dir != NULL)
12894 {
b36cec19
PA
12895 char *path_to_try = concat (comp_dir, SLASH_STRING,
12896 file_name, (char *) NULL);
3019eac3
DE
12897
12898 /* NOTE: If comp_dir is a relative path, this will also try the
12899 search path, which seems useful. */
ed2dc618
SM
12900 gdb_bfd_ref_ptr abfd (try_open_dwop_file (dwarf2_per_objfile,
12901 path_to_try,
12902 0 /*is_dwp*/,
192b62ce 12903 1 /*search_cwd*/));
3019eac3
DE
12904 xfree (path_to_try);
12905 if (abfd != NULL)
12906 return abfd;
12907 }
12908
12909 /* That didn't work, try debug-file-directory, which, despite its name,
12910 is a list of paths. */
12911
12912 if (*debug_file_directory == '\0')
12913 return NULL;
12914
ed2dc618
SM
12915 return try_open_dwop_file (dwarf2_per_objfile, file_name,
12916 0 /*is_dwp*/, 1 /*search_cwd*/);
3019eac3
DE
12917}
12918
80626a55
DE
12919/* This function is mapped across the sections and remembers the offset and
12920 size of each of the DWO debugging sections we are interested in. */
12921
12922static void
12923dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp, void *dwo_sections_ptr)
12924{
9a3c8263 12925 struct dwo_sections *dwo_sections = (struct dwo_sections *) dwo_sections_ptr;
80626a55
DE
12926 const struct dwop_section_names *names = &dwop_section_names;
12927
12928 if (section_is_p (sectp->name, &names->abbrev_dwo))
12929 {
049412e3 12930 dwo_sections->abbrev.s.section = sectp;
80626a55
DE
12931 dwo_sections->abbrev.size = bfd_get_section_size (sectp);
12932 }
12933 else if (section_is_p (sectp->name, &names->info_dwo))
12934 {
049412e3 12935 dwo_sections->info.s.section = sectp;
80626a55
DE
12936 dwo_sections->info.size = bfd_get_section_size (sectp);
12937 }
12938 else if (section_is_p (sectp->name, &names->line_dwo))
12939 {
049412e3 12940 dwo_sections->line.s.section = sectp;
80626a55
DE
12941 dwo_sections->line.size = bfd_get_section_size (sectp);
12942 }
12943 else if (section_is_p (sectp->name, &names->loc_dwo))
12944 {
049412e3 12945 dwo_sections->loc.s.section = sectp;
80626a55
DE
12946 dwo_sections->loc.size = bfd_get_section_size (sectp);
12947 }
12948 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12949 {
049412e3 12950 dwo_sections->macinfo.s.section = sectp;
80626a55
DE
12951 dwo_sections->macinfo.size = bfd_get_section_size (sectp);
12952 }
12953 else if (section_is_p (sectp->name, &names->macro_dwo))
12954 {
049412e3 12955 dwo_sections->macro.s.section = sectp;
80626a55
DE
12956 dwo_sections->macro.size = bfd_get_section_size (sectp);
12957 }
12958 else if (section_is_p (sectp->name, &names->str_dwo))
12959 {
049412e3 12960 dwo_sections->str.s.section = sectp;
80626a55
DE
12961 dwo_sections->str.size = bfd_get_section_size (sectp);
12962 }
12963 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12964 {
049412e3 12965 dwo_sections->str_offsets.s.section = sectp;
80626a55
DE
12966 dwo_sections->str_offsets.size = bfd_get_section_size (sectp);
12967 }
12968 else if (section_is_p (sectp->name, &names->types_dwo))
12969 {
12970 struct dwarf2_section_info type_section;
12971
12972 memset (&type_section, 0, sizeof (type_section));
049412e3 12973 type_section.s.section = sectp;
80626a55
DE
12974 type_section.size = bfd_get_section_size (sectp);
12975 VEC_safe_push (dwarf2_section_info_def, dwo_sections->types,
12976 &type_section);
12977 }
12978}
12979
ab5088bf 12980/* Initialize the use of the DWO file specified by DWO_NAME and referenced
19c3d4c9 12981 by PER_CU. This is for the non-DWP case.
80626a55 12982 The result is NULL if DWO_NAME can't be found. */
3019eac3
DE
12983
12984static struct dwo_file *
0ac5b59e
DE
12985open_and_init_dwo_file (struct dwarf2_per_cu_data *per_cu,
12986 const char *dwo_name, const char *comp_dir)
3019eac3 12987{
ed2dc618 12988 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
3019eac3 12989 struct objfile *objfile = dwarf2_per_objfile->objfile;
3019eac3 12990
ed2dc618 12991 gdb_bfd_ref_ptr dbfd (open_dwo_file (dwarf2_per_objfile, dwo_name, comp_dir));
80626a55
DE
12992 if (dbfd == NULL)
12993 {
b4f54984 12994 if (dwarf_read_debug)
80626a55
DE
12995 fprintf_unfiltered (gdb_stdlog, "DWO file not found: %s\n", dwo_name);
12996 return NULL;
12997 }
263db9a1
TT
12998
12999 /* We use a unique pointer here, despite the obstack allocation,
13000 because a dwo_file needs some cleanup if it is abandoned. */
13001 dwo_file_up dwo_file (OBSTACK_ZALLOC (&objfile->objfile_obstack,
13002 struct dwo_file));
0ac5b59e
DE
13003 dwo_file->dwo_name = dwo_name;
13004 dwo_file->comp_dir = comp_dir;
192b62ce 13005 dwo_file->dbfd = dbfd.release ();
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
263db9a1 13013 create_debug_types_hash_table (dwarf2_per_objfile, dwo_file.get (),
ed2dc618 13014 dwo_file->sections.types, dwo_file->tus);
3019eac3 13015
b4f54984 13016 if (dwarf_read_debug)
80626a55
DE
13017 fprintf_unfiltered (gdb_stdlog, "DWO file found: %s\n", dwo_name);
13018
263db9a1 13019 return dwo_file.release ();
3019eac3
DE
13020}
13021
80626a55 13022/* This function is mapped across the sections and remembers the offset and
73869dc2
DE
13023 size of each of the DWP debugging sections common to version 1 and 2 that
13024 we are interested in. */
3019eac3 13025
80626a55 13026static void
73869dc2
DE
13027dwarf2_locate_common_dwp_sections (bfd *abfd, asection *sectp,
13028 void *dwp_file_ptr)
3019eac3 13029{
9a3c8263 13030 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
80626a55
DE
13031 const struct dwop_section_names *names = &dwop_section_names;
13032 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
3019eac3 13033
80626a55 13034 /* Record the ELF section number for later lookup: this is what the
73869dc2 13035 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
80626a55
DE
13036 gdb_assert (elf_section_nr < dwp_file->num_sections);
13037 dwp_file->elf_sections[elf_section_nr] = sectp;
3019eac3 13038
80626a55
DE
13039 /* Look for specific sections that we need. */
13040 if (section_is_p (sectp->name, &names->str_dwo))
13041 {
049412e3 13042 dwp_file->sections.str.s.section = sectp;
80626a55
DE
13043 dwp_file->sections.str.size = bfd_get_section_size (sectp);
13044 }
13045 else if (section_is_p (sectp->name, &names->cu_index))
13046 {
049412e3 13047 dwp_file->sections.cu_index.s.section = sectp;
80626a55
DE
13048 dwp_file->sections.cu_index.size = bfd_get_section_size (sectp);
13049 }
13050 else if (section_is_p (sectp->name, &names->tu_index))
13051 {
049412e3 13052 dwp_file->sections.tu_index.s.section = sectp;
80626a55
DE
13053 dwp_file->sections.tu_index.size = bfd_get_section_size (sectp);
13054 }
13055}
3019eac3 13056
73869dc2
DE
13057/* This function is mapped across the sections and remembers the offset and
13058 size of each of the DWP version 2 debugging sections that we are interested
13059 in. This is split into a separate function because we don't know if we
13060 have version 1 or 2 until we parse the cu_index/tu_index sections. */
13061
13062static void
13063dwarf2_locate_v2_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
13064{
9a3c8263 13065 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
73869dc2
DE
13066 const struct dwop_section_names *names = &dwop_section_names;
13067 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
13068
13069 /* Record the ELF section number for later lookup: this is what the
13070 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
13071 gdb_assert (elf_section_nr < dwp_file->num_sections);
13072 dwp_file->elf_sections[elf_section_nr] = sectp;
13073
13074 /* Look for specific sections that we need. */
13075 if (section_is_p (sectp->name, &names->abbrev_dwo))
13076 {
049412e3 13077 dwp_file->sections.abbrev.s.section = sectp;
73869dc2
DE
13078 dwp_file->sections.abbrev.size = bfd_get_section_size (sectp);
13079 }
13080 else if (section_is_p (sectp->name, &names->info_dwo))
13081 {
049412e3 13082 dwp_file->sections.info.s.section = sectp;
73869dc2
DE
13083 dwp_file->sections.info.size = bfd_get_section_size (sectp);
13084 }
13085 else if (section_is_p (sectp->name, &names->line_dwo))
13086 {
049412e3 13087 dwp_file->sections.line.s.section = sectp;
73869dc2
DE
13088 dwp_file->sections.line.size = bfd_get_section_size (sectp);
13089 }
13090 else if (section_is_p (sectp->name, &names->loc_dwo))
13091 {
049412e3 13092 dwp_file->sections.loc.s.section = sectp;
73869dc2
DE
13093 dwp_file->sections.loc.size = bfd_get_section_size (sectp);
13094 }
13095 else if (section_is_p (sectp->name, &names->macinfo_dwo))
13096 {
049412e3 13097 dwp_file->sections.macinfo.s.section = sectp;
73869dc2
DE
13098 dwp_file->sections.macinfo.size = bfd_get_section_size (sectp);
13099 }
13100 else if (section_is_p (sectp->name, &names->macro_dwo))
13101 {
049412e3 13102 dwp_file->sections.macro.s.section = sectp;
73869dc2
DE
13103 dwp_file->sections.macro.size = bfd_get_section_size (sectp);
13104 }
13105 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
13106 {
049412e3 13107 dwp_file->sections.str_offsets.s.section = sectp;
73869dc2
DE
13108 dwp_file->sections.str_offsets.size = bfd_get_section_size (sectp);
13109 }
13110 else if (section_is_p (sectp->name, &names->types_dwo))
13111 {
049412e3 13112 dwp_file->sections.types.s.section = sectp;
73869dc2
DE
13113 dwp_file->sections.types.size = bfd_get_section_size (sectp);
13114 }
13115}
13116
80626a55 13117/* Hash function for dwp_file loaded CUs/TUs. */
3019eac3 13118
80626a55
DE
13119static hashval_t
13120hash_dwp_loaded_cutus (const void *item)
13121{
9a3c8263 13122 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
3019eac3 13123
80626a55
DE
13124 /* This drops the top 32 bits of the signature, but is ok for a hash. */
13125 return dwo_unit->signature;
3019eac3
DE
13126}
13127
80626a55 13128/* Equality function for dwp_file loaded CUs/TUs. */
3019eac3 13129
80626a55
DE
13130static int
13131eq_dwp_loaded_cutus (const void *a, const void *b)
3019eac3 13132{
9a3c8263
SM
13133 const struct dwo_unit *dua = (const struct dwo_unit *) a;
13134 const struct dwo_unit *dub = (const struct dwo_unit *) b;
3019eac3 13135
80626a55
DE
13136 return dua->signature == dub->signature;
13137}
3019eac3 13138
80626a55 13139/* Allocate a hash table for dwp_file loaded CUs/TUs. */
3019eac3 13140
80626a55
DE
13141static htab_t
13142allocate_dwp_loaded_cutus_table (struct objfile *objfile)
13143{
13144 return htab_create_alloc_ex (3,
13145 hash_dwp_loaded_cutus,
13146 eq_dwp_loaded_cutus,
13147 NULL,
13148 &objfile->objfile_obstack,
13149 hashtab_obstack_allocate,
13150 dummy_obstack_deallocate);
13151}
3019eac3 13152
ab5088bf
DE
13153/* Try to open DWP file FILE_NAME.
13154 The result is the bfd handle of the file.
13155 If there is a problem finding or opening the file, return NULL.
13156 Upon success, the canonicalized path of the file is stored in the bfd,
13157 same as symfile_bfd_open. */
13158
192b62ce 13159static gdb_bfd_ref_ptr
ed2dc618
SM
13160open_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
13161 const char *file_name)
ab5088bf 13162{
ed2dc618
SM
13163 gdb_bfd_ref_ptr abfd (try_open_dwop_file (dwarf2_per_objfile, file_name,
13164 1 /*is_dwp*/,
192b62ce 13165 1 /*search_cwd*/));
6ac97d4c
DE
13166 if (abfd != NULL)
13167 return abfd;
13168
13169 /* Work around upstream bug 15652.
13170 http://sourceware.org/bugzilla/show_bug.cgi?id=15652
13171 [Whether that's a "bug" is debatable, but it is getting in our way.]
13172 We have no real idea where the dwp file is, because gdb's realpath-ing
13173 of the executable's path may have discarded the needed info.
13174 [IWBN if the dwp file name was recorded in the executable, akin to
13175 .gnu_debuglink, but that doesn't exist yet.]
13176 Strip the directory from FILE_NAME and search again. */
13177 if (*debug_file_directory != '\0')
13178 {
13179 /* Don't implicitly search the current directory here.
13180 If the user wants to search "." to handle this case,
13181 it must be added to debug-file-directory. */
ed2dc618
SM
13182 return try_open_dwop_file (dwarf2_per_objfile,
13183 lbasename (file_name), 1 /*is_dwp*/,
6ac97d4c
DE
13184 0 /*search_cwd*/);
13185 }
13186
13187 return NULL;
ab5088bf
DE
13188}
13189
80626a55
DE
13190/* Initialize the use of the DWP file for the current objfile.
13191 By convention the name of the DWP file is ${objfile}.dwp.
13192 The result is NULL if it can't be found. */
a766d390 13193
400174b1 13194static std::unique_ptr<struct dwp_file>
ed2dc618 13195open_and_init_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
80626a55
DE
13196{
13197 struct objfile *objfile = dwarf2_per_objfile->objfile;
80626a55 13198
82bf32bc
JK
13199 /* Try to find first .dwp for the binary file before any symbolic links
13200 resolving. */
6c447423
DE
13201
13202 /* If the objfile is a debug file, find the name of the real binary
13203 file and get the name of dwp file from there. */
d721ba37 13204 std::string dwp_name;
6c447423
DE
13205 if (objfile->separate_debug_objfile_backlink != NULL)
13206 {
13207 struct objfile *backlink = objfile->separate_debug_objfile_backlink;
13208 const char *backlink_basename = lbasename (backlink->original_name);
6c447423 13209
d721ba37 13210 dwp_name = ldirname (objfile->original_name) + SLASH_STRING + backlink_basename;
6c447423
DE
13211 }
13212 else
d721ba37
PA
13213 dwp_name = objfile->original_name;
13214
13215 dwp_name += ".dwp";
80626a55 13216
ed2dc618 13217 gdb_bfd_ref_ptr dbfd (open_dwp_file (dwarf2_per_objfile, dwp_name.c_str ()));
82bf32bc
JK
13218 if (dbfd == NULL
13219 && strcmp (objfile->original_name, objfile_name (objfile)) != 0)
13220 {
13221 /* Try to find .dwp for the binary file after gdb_realpath resolving. */
d721ba37
PA
13222 dwp_name = objfile_name (objfile);
13223 dwp_name += ".dwp";
ed2dc618 13224 dbfd = open_dwp_file (dwarf2_per_objfile, dwp_name.c_str ());
82bf32bc
JK
13225 }
13226
80626a55
DE
13227 if (dbfd == NULL)
13228 {
b4f54984 13229 if (dwarf_read_debug)
d721ba37 13230 fprintf_unfiltered (gdb_stdlog, "DWP file not found: %s\n", dwp_name.c_str ());
400174b1 13231 return std::unique_ptr<dwp_file> ();
3019eac3 13232 }
400174b1
TT
13233
13234 const char *name = bfd_get_filename (dbfd.get ());
13235 std::unique_ptr<struct dwp_file> dwp_file
13236 (new struct dwp_file (name, std::move (dbfd)));
c906108c 13237
0a0f4c01 13238 dwp_file->num_sections = elf_numsections (dwp_file->dbfd);
80626a55
DE
13239 dwp_file->elf_sections =
13240 OBSTACK_CALLOC (&objfile->objfile_obstack,
13241 dwp_file->num_sections, asection *);
13242
400174b1
TT
13243 bfd_map_over_sections (dwp_file->dbfd.get (),
13244 dwarf2_locate_common_dwp_sections,
13245 dwp_file.get ());
80626a55 13246
400174b1
TT
13247 dwp_file->cus = create_dwp_hash_table (dwarf2_per_objfile, dwp_file.get (),
13248 0);
80626a55 13249
400174b1
TT
13250 dwp_file->tus = create_dwp_hash_table (dwarf2_per_objfile, dwp_file.get (),
13251 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)
400174b1
TT
13274 bfd_map_over_sections (dwp_file->dbfd.get (),
13275 dwarf2_locate_v2_dwp_sections,
13276 dwp_file.get ());
73869dc2 13277
19ac8c2e
DE
13278 dwp_file->loaded_cus = allocate_dwp_loaded_cutus_table (objfile);
13279 dwp_file->loaded_tus = allocate_dwp_loaded_cutus_table (objfile);
80626a55 13280
b4f54984 13281 if (dwarf_read_debug)
80626a55
DE
13282 {
13283 fprintf_unfiltered (gdb_stdlog, "DWP file found: %s\n", dwp_file->name);
13284 fprintf_unfiltered (gdb_stdlog,
21aa081e
PA
13285 " %s CUs, %s TUs\n",
13286 pulongest (dwp_file->cus ? dwp_file->cus->nr_units : 0),
13287 pulongest (dwp_file->tus ? dwp_file->tus->nr_units : 0));
80626a55
DE
13288 }
13289
13290 return dwp_file;
3019eac3 13291}
c906108c 13292
ab5088bf
DE
13293/* Wrapper around open_and_init_dwp_file, only open it once. */
13294
13295static struct dwp_file *
ed2dc618 13296get_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
ab5088bf
DE
13297{
13298 if (! dwarf2_per_objfile->dwp_checked)
13299 {
ed2dc618
SM
13300 dwarf2_per_objfile->dwp_file
13301 = open_and_init_dwp_file (dwarf2_per_objfile);
ab5088bf
DE
13302 dwarf2_per_objfile->dwp_checked = 1;
13303 }
400174b1 13304 return dwarf2_per_objfile->dwp_file.get ();
ab5088bf
DE
13305}
13306
80626a55
DE
13307/* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
13308 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
13309 or in the DWP file for the objfile, referenced by THIS_UNIT.
3019eac3 13310 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
80626a55
DE
13311 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
13312
13313 This is called, for example, when wanting to read a variable with a
13314 complex location. Therefore we don't want to do file i/o for every call.
13315 Therefore we don't want to look for a DWO file on every call.
13316 Therefore we first see if we've already seen SIGNATURE in a DWP file,
13317 then we check if we've already seen DWO_NAME, and only THEN do we check
13318 for a DWO file.
13319
1c658ad5 13320 The result is a pointer to the dwo_unit object or NULL if we didn't find it
80626a55 13321 (dwo_id mismatch or couldn't find the DWO/DWP file). */
debd256d 13322
3019eac3 13323static struct dwo_unit *
80626a55
DE
13324lookup_dwo_cutu (struct dwarf2_per_cu_data *this_unit,
13325 const char *dwo_name, const char *comp_dir,
13326 ULONGEST signature, int is_debug_types)
3019eac3 13327{
ed2dc618 13328 struct dwarf2_per_objfile *dwarf2_per_objfile = this_unit->dwarf2_per_objfile;
3019eac3 13329 struct objfile *objfile = dwarf2_per_objfile->objfile;
80626a55
DE
13330 const char *kind = is_debug_types ? "TU" : "CU";
13331 void **dwo_file_slot;
3019eac3 13332 struct dwo_file *dwo_file;
80626a55 13333 struct dwp_file *dwp_file;
cb1df416 13334
6a506a2d
DE
13335 /* First see if there's a DWP file.
13336 If we have a DWP file but didn't find the DWO inside it, don't
13337 look for the original DWO file. It makes gdb behave differently
13338 depending on whether one is debugging in the build tree. */
cf2c3c16 13339
ed2dc618 13340 dwp_file = get_dwp_file (dwarf2_per_objfile);
80626a55 13341 if (dwp_file != NULL)
cf2c3c16 13342 {
80626a55
DE
13343 const struct dwp_hash_table *dwp_htab =
13344 is_debug_types ? dwp_file->tus : dwp_file->cus;
13345
13346 if (dwp_htab != NULL)
13347 {
13348 struct dwo_unit *dwo_cutu =
ed2dc618 13349 lookup_dwo_unit_in_dwp (dwarf2_per_objfile, dwp_file, comp_dir,
57d63ce2 13350 signature, is_debug_types);
80626a55
DE
13351
13352 if (dwo_cutu != NULL)
13353 {
b4f54984 13354 if (dwarf_read_debug)
80626a55
DE
13355 {
13356 fprintf_unfiltered (gdb_stdlog,
13357 "Virtual DWO %s %s found: @%s\n",
13358 kind, hex_string (signature),
13359 host_address_to_string (dwo_cutu));
13360 }
13361 return dwo_cutu;
13362 }
13363 }
13364 }
6a506a2d 13365 else
80626a55 13366 {
6a506a2d 13367 /* No DWP file, look for the DWO file. */
80626a55 13368
ed2dc618
SM
13369 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
13370 dwo_name, comp_dir);
6a506a2d 13371 if (*dwo_file_slot == NULL)
80626a55 13372 {
6a506a2d
DE
13373 /* Read in the file and build a table of the CUs/TUs it contains. */
13374 *dwo_file_slot = open_and_init_dwo_file (this_unit, dwo_name, comp_dir);
19c3d4c9 13375 }
6a506a2d 13376 /* NOTE: This will be NULL if unable to open the file. */
9a3c8263 13377 dwo_file = (struct dwo_file *) *dwo_file_slot;
3019eac3 13378
6a506a2d 13379 if (dwo_file != NULL)
19c3d4c9 13380 {
6a506a2d
DE
13381 struct dwo_unit *dwo_cutu = NULL;
13382
13383 if (is_debug_types && dwo_file->tus)
13384 {
13385 struct dwo_unit find_dwo_cutu;
13386
13387 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
13388 find_dwo_cutu.signature = signature;
9a3c8263
SM
13389 dwo_cutu
13390 = (struct dwo_unit *) htab_find (dwo_file->tus, &find_dwo_cutu);
6a506a2d 13391 }
33c5cd75 13392 else if (!is_debug_types && dwo_file->cus)
80626a55 13393 {
33c5cd75
DB
13394 struct dwo_unit find_dwo_cutu;
13395
13396 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
13397 find_dwo_cutu.signature = signature;
13398 dwo_cutu = (struct dwo_unit *)htab_find (dwo_file->cus,
13399 &find_dwo_cutu);
6a506a2d
DE
13400 }
13401
13402 if (dwo_cutu != NULL)
13403 {
b4f54984 13404 if (dwarf_read_debug)
6a506a2d
DE
13405 {
13406 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) found: @%s\n",
13407 kind, dwo_name, hex_string (signature),
13408 host_address_to_string (dwo_cutu));
13409 }
13410 return dwo_cutu;
80626a55
DE
13411 }
13412 }
2e276125 13413 }
9cdd5dbd 13414
80626a55
DE
13415 /* We didn't find it. This could mean a dwo_id mismatch, or
13416 someone deleted the DWO/DWP file, or the search path isn't set up
13417 correctly to find the file. */
13418
b4f54984 13419 if (dwarf_read_debug)
80626a55
DE
13420 {
13421 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) not found\n",
13422 kind, dwo_name, hex_string (signature));
13423 }
3019eac3 13424
6656a72d
DE
13425 /* This is a warning and not a complaint because it can be caused by
13426 pilot error (e.g., user accidentally deleting the DWO). */
43942612
DE
13427 {
13428 /* Print the name of the DWP file if we looked there, helps the user
13429 better diagnose the problem. */
791afaa2 13430 std::string dwp_text;
43942612
DE
13431
13432 if (dwp_file != NULL)
791afaa2
TT
13433 dwp_text = string_printf (" [in DWP file %s]",
13434 lbasename (dwp_file->name));
43942612 13435
9d8780f0 13436 warning (_("Could not find DWO %s %s(%s)%s referenced by %s at offset %s"
43942612
DE
13437 " [in module %s]"),
13438 kind, dwo_name, hex_string (signature),
791afaa2 13439 dwp_text.c_str (),
43942612 13440 this_unit->is_debug_types ? "TU" : "CU",
9d8780f0 13441 sect_offset_str (this_unit->sect_off), objfile_name (objfile));
43942612 13442 }
3019eac3 13443 return NULL;
5fb290d7
DJ
13444}
13445
80626a55
DE
13446/* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
13447 See lookup_dwo_cutu_unit for details. */
13448
13449static struct dwo_unit *
13450lookup_dwo_comp_unit (struct dwarf2_per_cu_data *this_cu,
13451 const char *dwo_name, const char *comp_dir,
13452 ULONGEST signature)
13453{
13454 return lookup_dwo_cutu (this_cu, dwo_name, comp_dir, signature, 0);
13455}
13456
13457/* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
13458 See lookup_dwo_cutu_unit for details. */
13459
13460static struct dwo_unit *
13461lookup_dwo_type_unit (struct signatured_type *this_tu,
13462 const char *dwo_name, const char *comp_dir)
13463{
13464 return lookup_dwo_cutu (&this_tu->per_cu, dwo_name, comp_dir, this_tu->signature, 1);
13465}
13466
89e63ee4
DE
13467/* Traversal function for queue_and_load_all_dwo_tus. */
13468
13469static int
13470queue_and_load_dwo_tu (void **slot, void *info)
13471{
13472 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
13473 struct dwarf2_per_cu_data *per_cu = (struct dwarf2_per_cu_data *) info;
13474 ULONGEST signature = dwo_unit->signature;
13475 struct signatured_type *sig_type =
13476 lookup_dwo_signatured_type (per_cu->cu, signature);
13477
13478 if (sig_type != NULL)
13479 {
13480 struct dwarf2_per_cu_data *sig_cu = &sig_type->per_cu;
13481
13482 /* We pass NULL for DEPENDENT_CU because we don't yet know if there's
13483 a real dependency of PER_CU on SIG_TYPE. That is detected later
13484 while processing PER_CU. */
13485 if (maybe_queue_comp_unit (NULL, sig_cu, per_cu->cu->language))
13486 load_full_type_unit (sig_cu);
13487 VEC_safe_push (dwarf2_per_cu_ptr, per_cu->imported_symtabs, sig_cu);
13488 }
13489
13490 return 1;
13491}
13492
13493/* Queue all TUs contained in the DWO of PER_CU to be read in.
13494 The DWO may have the only definition of the type, though it may not be
13495 referenced anywhere in PER_CU. Thus we have to load *all* its TUs.
13496 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
13497
13498static void
13499queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *per_cu)
13500{
13501 struct dwo_unit *dwo_unit;
13502 struct dwo_file *dwo_file;
13503
13504 gdb_assert (!per_cu->is_debug_types);
ed2dc618 13505 gdb_assert (get_dwp_file (per_cu->dwarf2_per_objfile) == NULL);
89e63ee4
DE
13506 gdb_assert (per_cu->cu != NULL);
13507
13508 dwo_unit = per_cu->cu->dwo_unit;
13509 gdb_assert (dwo_unit != NULL);
13510
13511 dwo_file = dwo_unit->dwo_file;
13512 if (dwo_file->tus != NULL)
13513 htab_traverse_noresize (dwo_file->tus, queue_and_load_dwo_tu, per_cu);
13514}
13515
3019eac3 13516/* Free all resources associated with DWO_FILE.
5dafb3d1 13517 Close the DWO file and munmap the sections. */
348e048f
DE
13518
13519static void
5dafb3d1 13520free_dwo_file (struct dwo_file *dwo_file)
348e048f 13521{
5c6fa7ab 13522 /* Note: dbfd is NULL for virtual DWO files. */
80626a55 13523 gdb_bfd_unref (dwo_file->dbfd);
348e048f 13524
3019eac3
DE
13525 VEC_free (dwarf2_section_info_def, dwo_file->sections.types);
13526}
348e048f 13527
3019eac3 13528/* Traversal function for free_dwo_files. */
2ab95328 13529
3019eac3
DE
13530static int
13531free_dwo_file_from_slot (void **slot, void *info)
13532{
13533 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
348e048f 13534
5dafb3d1 13535 free_dwo_file (dwo_file);
348e048f 13536
3019eac3
DE
13537 return 1;
13538}
348e048f 13539
3019eac3 13540/* Free all resources associated with DWO_FILES. */
348e048f 13541
3019eac3
DE
13542static void
13543free_dwo_files (htab_t dwo_files, struct objfile *objfile)
13544{
13545 htab_traverse_noresize (dwo_files, free_dwo_file_from_slot, objfile);
348e048f 13546}
3019eac3
DE
13547\f
13548/* Read in various DIEs. */
348e048f 13549
d389af10 13550/* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
3e43a32a
MS
13551 Inherit only the children of the DW_AT_abstract_origin DIE not being
13552 already referenced by DW_AT_abstract_origin from the children of the
13553 current DIE. */
d389af10
JK
13554
13555static void
13556inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
13557{
13558 struct die_info *child_die;
791afaa2 13559 sect_offset *offsetp;
d389af10
JK
13560 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
13561 struct die_info *origin_die;
13562 /* Iterator of the ORIGIN_DIE children. */
13563 struct die_info *origin_child_die;
d389af10 13564 struct attribute *attr;
cd02d79d
PA
13565 struct dwarf2_cu *origin_cu;
13566 struct pending **origin_previous_list_in_scope;
d389af10
JK
13567
13568 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13569 if (!attr)
13570 return;
13571
cd02d79d
PA
13572 /* Note that following die references may follow to a die in a
13573 different cu. */
13574
13575 origin_cu = cu;
13576 origin_die = follow_die_ref (die, attr, &origin_cu);
13577
13578 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
13579 symbols in. */
13580 origin_previous_list_in_scope = origin_cu->list_in_scope;
13581 origin_cu->list_in_scope = cu->list_in_scope;
13582
edb3359d
DJ
13583 if (die->tag != origin_die->tag
13584 && !(die->tag == DW_TAG_inlined_subroutine
13585 && origin_die->tag == DW_TAG_subprogram))
b98664d3 13586 complaint (_("DIE %s and its abstract origin %s have different tags"),
9d8780f0
SM
13587 sect_offset_str (die->sect_off),
13588 sect_offset_str (origin_die->sect_off));
d389af10 13589
791afaa2 13590 std::vector<sect_offset> offsets;
d389af10 13591
3ea89b92
PMR
13592 for (child_die = die->child;
13593 child_die && child_die->tag;
13594 child_die = sibling_die (child_die))
13595 {
13596 struct die_info *child_origin_die;
13597 struct dwarf2_cu *child_origin_cu;
13598
13599 /* We are trying to process concrete instance entries:
216f72a1 13600 DW_TAG_call_site DIEs indeed have a DW_AT_abstract_origin tag, but
3ea89b92
PMR
13601 it's not relevant to our analysis here. i.e. detecting DIEs that are
13602 present in the abstract instance but not referenced in the concrete
13603 one. */
216f72a1
JK
13604 if (child_die->tag == DW_TAG_call_site
13605 || child_die->tag == DW_TAG_GNU_call_site)
3ea89b92
PMR
13606 continue;
13607
c38f313d
DJ
13608 /* For each CHILD_DIE, find the corresponding child of
13609 ORIGIN_DIE. If there is more than one layer of
13610 DW_AT_abstract_origin, follow them all; there shouldn't be,
13611 but GCC versions at least through 4.4 generate this (GCC PR
13612 40573). */
3ea89b92
PMR
13613 child_origin_die = child_die;
13614 child_origin_cu = cu;
c38f313d
DJ
13615 while (1)
13616 {
cd02d79d
PA
13617 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
13618 child_origin_cu);
c38f313d
DJ
13619 if (attr == NULL)
13620 break;
cd02d79d
PA
13621 child_origin_die = follow_die_ref (child_origin_die, attr,
13622 &child_origin_cu);
c38f313d
DJ
13623 }
13624
d389af10
JK
13625 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
13626 counterpart may exist. */
c38f313d 13627 if (child_origin_die != child_die)
d389af10 13628 {
edb3359d
DJ
13629 if (child_die->tag != child_origin_die->tag
13630 && !(child_die->tag == DW_TAG_inlined_subroutine
13631 && child_origin_die->tag == DW_TAG_subprogram))
b98664d3 13632 complaint (_("Child DIE %s and its abstract origin %s have "
9c541725 13633 "different tags"),
9d8780f0
SM
13634 sect_offset_str (child_die->sect_off),
13635 sect_offset_str (child_origin_die->sect_off));
c38f313d 13636 if (child_origin_die->parent != origin_die)
b98664d3 13637 complaint (_("Child DIE %s and its abstract origin %s have "
9c541725 13638 "different parents"),
9d8780f0
SM
13639 sect_offset_str (child_die->sect_off),
13640 sect_offset_str (child_origin_die->sect_off));
c38f313d 13641 else
791afaa2 13642 offsets.push_back (child_origin_die->sect_off);
d389af10 13643 }
d389af10 13644 }
791afaa2
TT
13645 std::sort (offsets.begin (), offsets.end ());
13646 sect_offset *offsets_end = offsets.data () + offsets.size ();
13647 for (offsetp = offsets.data () + 1; offsetp < offsets_end; offsetp++)
9c541725 13648 if (offsetp[-1] == *offsetp)
b98664d3 13649 complaint (_("Multiple children of DIE %s refer "
9d8780f0
SM
13650 "to DIE %s as their abstract origin"),
13651 sect_offset_str (die->sect_off), sect_offset_str (*offsetp));
d389af10 13652
791afaa2 13653 offsetp = offsets.data ();
d389af10
JK
13654 origin_child_die = origin_die->child;
13655 while (origin_child_die && origin_child_die->tag)
13656 {
13657 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
b64f50a1 13658 while (offsetp < offsets_end
9c541725 13659 && *offsetp < origin_child_die->sect_off)
d389af10 13660 offsetp++;
b64f50a1 13661 if (offsetp >= offsets_end
9c541725 13662 || *offsetp > origin_child_die->sect_off)
d389af10 13663 {
adde2bff
DE
13664 /* Found that ORIGIN_CHILD_DIE is really not referenced.
13665 Check whether we're already processing ORIGIN_CHILD_DIE.
13666 This can happen with mutually referenced abstract_origins.
13667 PR 16581. */
13668 if (!origin_child_die->in_process)
13669 process_die (origin_child_die, origin_cu);
d389af10
JK
13670 }
13671 origin_child_die = sibling_die (origin_child_die);
13672 }
cd02d79d 13673 origin_cu->list_in_scope = origin_previous_list_in_scope;
d389af10
JK
13674}
13675
c906108c 13676static void
e7c27a73 13677read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 13678{
518817b3 13679 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
3e29f34a 13680 struct gdbarch *gdbarch = get_objfile_arch (objfile);
fe978cb0 13681 struct context_stack *newobj;
c906108c
SS
13682 CORE_ADDR lowpc;
13683 CORE_ADDR highpc;
13684 struct die_info *child_die;
edb3359d 13685 struct attribute *attr, *call_line, *call_file;
15d034d0 13686 const char *name;
e142c38c 13687 CORE_ADDR baseaddr;
801e3a5b 13688 struct block *block;
edb3359d 13689 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
2f4732b0 13690 std::vector<struct symbol *> template_args;
34eaf542 13691 struct template_symbol *templ_func = NULL;
edb3359d
DJ
13692
13693 if (inlined_func)
13694 {
13695 /* If we do not have call site information, we can't show the
13696 caller of this inlined function. That's too confusing, so
13697 only use the scope for local variables. */
13698 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
13699 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
13700 if (call_line == NULL || call_file == NULL)
13701 {
13702 read_lexical_block_scope (die, cu);
13703 return;
13704 }
13705 }
c906108c 13706
e142c38c
DJ
13707 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
13708
94af9270 13709 name = dwarf2_name (die, cu);
c906108c 13710
e8d05480
JB
13711 /* Ignore functions with missing or empty names. These are actually
13712 illegal according to the DWARF standard. */
13713 if (name == NULL)
13714 {
b98664d3 13715 complaint (_("missing name for subprogram DIE at %s"),
9d8780f0 13716 sect_offset_str (die->sect_off));
e8d05480
JB
13717 return;
13718 }
13719
13720 /* Ignore functions with missing or invalid low and high pc attributes. */
3a2b436a 13721 if (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL)
e385593e 13722 <= PC_BOUNDS_INVALID)
e8d05480 13723 {
ae4d0c03
PM
13724 attr = dwarf2_attr (die, DW_AT_external, cu);
13725 if (!attr || !DW_UNSND (attr))
b98664d3 13726 complaint (_("cannot get low and high bounds "
9d8780f0
SM
13727 "for subprogram DIE at %s"),
13728 sect_offset_str (die->sect_off));
e8d05480
JB
13729 return;
13730 }
c906108c 13731
3e29f34a
MR
13732 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13733 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
c906108c 13734
34eaf542
TT
13735 /* If we have any template arguments, then we must allocate a
13736 different sort of symbol. */
13737 for (child_die = die->child; child_die; child_die = sibling_die (child_die))
13738 {
13739 if (child_die->tag == DW_TAG_template_type_param
13740 || child_die->tag == DW_TAG_template_value_param)
13741 {
e623cf5d 13742 templ_func = allocate_template_symbol (objfile);
cf724bc9 13743 templ_func->subclass = SYMBOL_TEMPLATE;
34eaf542
TT
13744 break;
13745 }
13746 }
13747
c24bdb02 13748 newobj = cu->get_builder ()->push_context (0, lowpc);
5e2db402
TT
13749 newobj->name = new_symbol (die, read_type_die (die, cu), cu,
13750 (struct symbol *) templ_func);
4c2df51b 13751
4cecd739
DJ
13752 /* If there is a location expression for DW_AT_frame_base, record
13753 it. */
e142c38c 13754 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
4c2df51b 13755 if (attr)
fe978cb0 13756 dwarf2_symbol_mark_computed (attr, newobj->name, cu, 1);
4c2df51b 13757
63e43d3a
PMR
13758 /* If there is a location for the static link, record it. */
13759 newobj->static_link = NULL;
13760 attr = dwarf2_attr (die, DW_AT_static_link, cu);
13761 if (attr)
13762 {
224c3ddb
SM
13763 newobj->static_link
13764 = XOBNEW (&objfile->objfile_obstack, struct dynamic_prop);
63e43d3a
PMR
13765 attr_to_dynamic_prop (attr, die, cu, newobj->static_link);
13766 }
13767
c24bdb02 13768 cu->list_in_scope = cu->get_builder ()->get_local_symbols ();
c906108c 13769
639d11d3 13770 if (die->child != NULL)
c906108c 13771 {
639d11d3 13772 child_die = die->child;
c906108c
SS
13773 while (child_die && child_die->tag)
13774 {
34eaf542
TT
13775 if (child_die->tag == DW_TAG_template_type_param
13776 || child_die->tag == DW_TAG_template_value_param)
13777 {
13778 struct symbol *arg = new_symbol (child_die, NULL, cu);
13779
f1078f66 13780 if (arg != NULL)
2f4732b0 13781 template_args.push_back (arg);
34eaf542
TT
13782 }
13783 else
13784 process_die (child_die, cu);
c906108c
SS
13785 child_die = sibling_die (child_die);
13786 }
13787 }
13788
d389af10
JK
13789 inherit_abstract_dies (die, cu);
13790
4a811a97
UW
13791 /* If we have a DW_AT_specification, we might need to import using
13792 directives from the context of the specification DIE. See the
13793 comment in determine_prefix. */
13794 if (cu->language == language_cplus
13795 && dwarf2_attr (die, DW_AT_specification, cu))
13796 {
13797 struct dwarf2_cu *spec_cu = cu;
13798 struct die_info *spec_die = die_specification (die, &spec_cu);
13799
13800 while (spec_die)
13801 {
13802 child_die = spec_die->child;
13803 while (child_die && child_die->tag)
13804 {
13805 if (child_die->tag == DW_TAG_imported_module)
13806 process_die (child_die, spec_cu);
13807 child_die = sibling_die (child_die);
13808 }
13809
13810 /* In some cases, GCC generates specification DIEs that
13811 themselves contain DW_AT_specification attributes. */
13812 spec_die = die_specification (spec_die, &spec_cu);
13813 }
13814 }
13815
c24bdb02 13816 struct context_stack cstk = cu->get_builder ()->pop_context ();
c906108c 13817 /* Make a block for the local symbols within. */
c24bdb02 13818 block = cu->get_builder ()->finish_block (cstk.name, cstk.old_blocks,
804d2729 13819 cstk.static_link, lowpc, highpc);
801e3a5b 13820
df8a16a1 13821 /* For C++, set the block's scope. */
45280282
IB
13822 if ((cu->language == language_cplus
13823 || cu->language == language_fortran
c44af4eb
TT
13824 || cu->language == language_d
13825 || cu->language == language_rust)
4d4ec4e5 13826 && cu->processing_has_namespace_info)
195a3f6c
TT
13827 block_set_scope (block, determine_prefix (die, cu),
13828 &objfile->objfile_obstack);
df8a16a1 13829
801e3a5b
JB
13830 /* If we have address ranges, record them. */
13831 dwarf2_record_block_ranges (die, block, baseaddr, cu);
6e70227d 13832
a60f3166 13833 gdbarch_make_symbol_special (gdbarch, cstk.name, objfile);
3e29f34a 13834
34eaf542 13835 /* Attach template arguments to function. */
2f4732b0 13836 if (!template_args.empty ())
34eaf542
TT
13837 {
13838 gdb_assert (templ_func != NULL);
13839
2f4732b0 13840 templ_func->n_template_arguments = template_args.size ();
34eaf542 13841 templ_func->template_arguments
8d749320
SM
13842 = XOBNEWVEC (&objfile->objfile_obstack, struct symbol *,
13843 templ_func->n_template_arguments);
34eaf542 13844 memcpy (templ_func->template_arguments,
2f4732b0 13845 template_args.data (),
34eaf542 13846 (templ_func->n_template_arguments * sizeof (struct symbol *)));
3e1d3d8c
TT
13847
13848 /* Make sure that the symtab is set on the new symbols. Even
13849 though they don't appear in this symtab directly, other parts
13850 of gdb assume that symbols do, and this is reasonably
13851 true. */
8634679f 13852 for (symbol *sym : template_args)
3e1d3d8c 13853 symbol_set_symtab (sym, symbol_symtab (templ_func));
34eaf542
TT
13854 }
13855
208d8187
JB
13856 /* In C++, we can have functions nested inside functions (e.g., when
13857 a function declares a class that has methods). This means that
13858 when we finish processing a function scope, we may need to go
13859 back to building a containing block's symbol lists. */
c24bdb02
KS
13860 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13861 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
208d8187 13862
921e78cf
JB
13863 /* If we've finished processing a top-level function, subsequent
13864 symbols go in the file symbol list. */
c24bdb02
KS
13865 if (cu->get_builder ()->outermost_context_p ())
13866 cu->list_in_scope = cu->get_builder ()->get_file_symbols ();
c906108c
SS
13867}
13868
13869/* Process all the DIES contained within a lexical block scope. Start
13870 a new scope, process the dies, and then close the scope. */
13871
13872static void
e7c27a73 13873read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 13874{
518817b3 13875 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
3e29f34a 13876 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
13877 CORE_ADDR lowpc, highpc;
13878 struct die_info *child_die;
e142c38c
DJ
13879 CORE_ADDR baseaddr;
13880
13881 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c
SS
13882
13883 /* Ignore blocks with missing or invalid low and high pc attributes. */
af34e669
DJ
13884 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
13885 as multiple lexical blocks? Handling children in a sane way would
6e70227d 13886 be nasty. Might be easier to properly extend generic blocks to
af34e669 13887 describe ranges. */
e385593e
JK
13888 switch (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
13889 {
13890 case PC_BOUNDS_NOT_PRESENT:
13891 /* DW_TAG_lexical_block has no attributes, process its children as if
13892 there was no wrapping by that DW_TAG_lexical_block.
13893 GCC does no longer produces such DWARF since GCC r224161. */
13894 for (child_die = die->child;
13895 child_die != NULL && child_die->tag;
13896 child_die = sibling_die (child_die))
13897 process_die (child_die, cu);
13898 return;
13899 case PC_BOUNDS_INVALID:
13900 return;
13901 }
3e29f34a
MR
13902 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13903 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
c906108c 13904
c24bdb02 13905 cu->get_builder ()->push_context (0, lowpc);
639d11d3 13906 if (die->child != NULL)
c906108c 13907 {
639d11d3 13908 child_die = die->child;
c906108c
SS
13909 while (child_die && child_die->tag)
13910 {
e7c27a73 13911 process_die (child_die, cu);
c906108c
SS
13912 child_die = sibling_die (child_die);
13913 }
13914 }
3ea89b92 13915 inherit_abstract_dies (die, cu);
c24bdb02 13916 struct context_stack cstk = cu->get_builder ()->pop_context ();
c906108c 13917
c24bdb02
KS
13918 if (*cu->get_builder ()->get_local_symbols () != NULL
13919 || (*cu->get_builder ()->get_local_using_directives ()) != NULL)
c906108c 13920 {
801e3a5b 13921 struct block *block
c24bdb02 13922 = cu->get_builder ()->finish_block (0, cstk.old_blocks, NULL,
804d2729 13923 cstk.start_addr, highpc);
801e3a5b
JB
13924
13925 /* Note that recording ranges after traversing children, as we
13926 do here, means that recording a parent's ranges entails
13927 walking across all its children's ranges as they appear in
13928 the address map, which is quadratic behavior.
13929
13930 It would be nicer to record the parent's ranges before
13931 traversing its children, simply overriding whatever you find
13932 there. But since we don't even decide whether to create a
13933 block until after we've traversed its children, that's hard
13934 to do. */
13935 dwarf2_record_block_ranges (die, block, baseaddr, cu);
c906108c 13936 }
c24bdb02
KS
13937 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13938 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
c906108c
SS
13939}
13940
216f72a1 13941/* Read in DW_TAG_call_site and insert it to CU->call_site_htab. */
96408a79
SA
13942
13943static void
13944read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
13945{
518817b3 13946 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
96408a79
SA
13947 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13948 CORE_ADDR pc, baseaddr;
13949 struct attribute *attr;
13950 struct call_site *call_site, call_site_local;
13951 void **slot;
13952 int nparams;
13953 struct die_info *child_die;
13954
13955 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
13956
216f72a1
JK
13957 attr = dwarf2_attr (die, DW_AT_call_return_pc, cu);
13958 if (attr == NULL)
13959 {
13960 /* This was a pre-DWARF-5 GNU extension alias
13961 for DW_AT_call_return_pc. */
13962 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
13963 }
96408a79
SA
13964 if (!attr)
13965 {
b98664d3 13966 complaint (_("missing DW_AT_call_return_pc for DW_TAG_call_site "
9d8780f0
SM
13967 "DIE %s [in module %s]"),
13968 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79
SA
13969 return;
13970 }
31aa7e4e 13971 pc = attr_value_as_address (attr) + baseaddr;
3e29f34a 13972 pc = gdbarch_adjust_dwarf2_addr (gdbarch, pc);
96408a79
SA
13973
13974 if (cu->call_site_htab == NULL)
13975 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
13976 NULL, &objfile->objfile_obstack,
13977 hashtab_obstack_allocate, NULL);
13978 call_site_local.pc = pc;
13979 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
13980 if (*slot != NULL)
13981 {
b98664d3 13982 complaint (_("Duplicate PC %s for DW_TAG_call_site "
9d8780f0
SM
13983 "DIE %s [in module %s]"),
13984 paddress (gdbarch, pc), sect_offset_str (die->sect_off),
4262abfb 13985 objfile_name (objfile));
96408a79
SA
13986 return;
13987 }
13988
13989 /* Count parameters at the caller. */
13990
13991 nparams = 0;
13992 for (child_die = die->child; child_die && child_die->tag;
13993 child_die = sibling_die (child_die))
13994 {
216f72a1
JK
13995 if (child_die->tag != DW_TAG_call_site_parameter
13996 && child_die->tag != DW_TAG_GNU_call_site_parameter)
96408a79 13997 {
b98664d3 13998 complaint (_("Tag %d is not DW_TAG_call_site_parameter in "
9d8780f0
SM
13999 "DW_TAG_call_site child DIE %s [in module %s]"),
14000 child_die->tag, sect_offset_str (child_die->sect_off),
4262abfb 14001 objfile_name (objfile));
96408a79
SA
14002 continue;
14003 }
14004
14005 nparams++;
14006 }
14007
224c3ddb
SM
14008 call_site
14009 = ((struct call_site *)
14010 obstack_alloc (&objfile->objfile_obstack,
14011 sizeof (*call_site)
14012 + (sizeof (*call_site->parameter) * (nparams - 1))));
96408a79
SA
14013 *slot = call_site;
14014 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
14015 call_site->pc = pc;
14016
216f72a1
JK
14017 if (dwarf2_flag_true_p (die, DW_AT_call_tail_call, cu)
14018 || dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
96408a79
SA
14019 {
14020 struct die_info *func_die;
14021
14022 /* Skip also over DW_TAG_inlined_subroutine. */
14023 for (func_die = die->parent;
14024 func_die && func_die->tag != DW_TAG_subprogram
14025 && func_die->tag != DW_TAG_subroutine_type;
14026 func_die = func_die->parent);
14027
216f72a1
JK
14028 /* DW_AT_call_all_calls is a superset
14029 of DW_AT_call_all_tail_calls. */
96408a79 14030 if (func_die
216f72a1 14031 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_calls, cu)
96408a79 14032 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
216f72a1 14033 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_tail_calls, cu)
96408a79
SA
14034 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
14035 {
14036 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
14037 not complete. But keep CALL_SITE for look ups via call_site_htab,
14038 both the initial caller containing the real return address PC and
14039 the final callee containing the current PC of a chain of tail
14040 calls do not need to have the tail call list complete. But any
14041 function candidate for a virtual tail call frame searched via
14042 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
14043 determined unambiguously. */
14044 }
14045 else
14046 {
14047 struct type *func_type = NULL;
14048
14049 if (func_die)
14050 func_type = get_die_type (func_die, cu);
14051 if (func_type != NULL)
14052 {
14053 gdb_assert (TYPE_CODE (func_type) == TYPE_CODE_FUNC);
14054
14055 /* Enlist this call site to the function. */
14056 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
14057 TYPE_TAIL_CALL_LIST (func_type) = call_site;
14058 }
14059 else
b98664d3 14060 complaint (_("Cannot find function owning DW_TAG_call_site "
9d8780f0
SM
14061 "DIE %s [in module %s]"),
14062 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79
SA
14063 }
14064 }
14065
216f72a1
JK
14066 attr = dwarf2_attr (die, DW_AT_call_target, cu);
14067 if (attr == NULL)
14068 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
14069 if (attr == NULL)
14070 attr = dwarf2_attr (die, DW_AT_call_origin, cu);
96408a79 14071 if (attr == NULL)
216f72a1
JK
14072 {
14073 /* This was a pre-DWARF-5 GNU extension alias for DW_AT_call_origin. */
14074 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
14075 }
96408a79
SA
14076 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
14077 if (!attr || (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0))
14078 /* Keep NULL DWARF_BLOCK. */;
14079 else if (attr_form_is_block (attr))
14080 {
14081 struct dwarf2_locexpr_baton *dlbaton;
14082
8d749320 14083 dlbaton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
96408a79
SA
14084 dlbaton->data = DW_BLOCK (attr)->data;
14085 dlbaton->size = DW_BLOCK (attr)->size;
14086 dlbaton->per_cu = cu->per_cu;
14087
14088 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
14089 }
7771576e 14090 else if (attr_form_is_ref (attr))
96408a79 14091 {
96408a79
SA
14092 struct dwarf2_cu *target_cu = cu;
14093 struct die_info *target_die;
14094
ac9ec31b 14095 target_die = follow_die_ref (die, attr, &target_cu);
518817b3 14096 gdb_assert (target_cu->per_cu->dwarf2_per_objfile->objfile == objfile);
96408a79
SA
14097 if (die_is_declaration (target_die, target_cu))
14098 {
7d45c7c3 14099 const char *target_physname;
9112db09
JK
14100
14101 /* Prefer the mangled name; otherwise compute the demangled one. */
73b9be8b 14102 target_physname = dw2_linkage_name (target_die, target_cu);
7d45c7c3 14103 if (target_physname == NULL)
9112db09 14104 target_physname = dwarf2_physname (NULL, target_die, target_cu);
96408a79 14105 if (target_physname == NULL)
b98664d3 14106 complaint (_("DW_AT_call_target target DIE has invalid "
9d8780f0
SM
14107 "physname, for referencing DIE %s [in module %s]"),
14108 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79 14109 else
7d455152 14110 SET_FIELD_PHYSNAME (call_site->target, target_physname);
96408a79
SA
14111 }
14112 else
14113 {
14114 CORE_ADDR lowpc;
14115
14116 /* DW_AT_entry_pc should be preferred. */
3a2b436a 14117 if (dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL)
e385593e 14118 <= PC_BOUNDS_INVALID)
b98664d3 14119 complaint (_("DW_AT_call_target target DIE has invalid "
9d8780f0
SM
14120 "low pc, for referencing DIE %s [in module %s]"),
14121 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79 14122 else
3e29f34a
MR
14123 {
14124 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
14125 SET_FIELD_PHYSADDR (call_site->target, lowpc);
14126 }
96408a79
SA
14127 }
14128 }
14129 else
b98664d3 14130 complaint (_("DW_TAG_call_site DW_AT_call_target is neither "
9d8780f0
SM
14131 "block nor reference, for DIE %s [in module %s]"),
14132 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79
SA
14133
14134 call_site->per_cu = cu->per_cu;
14135
14136 for (child_die = die->child;
14137 child_die && child_die->tag;
14138 child_die = sibling_die (child_die))
14139 {
96408a79 14140 struct call_site_parameter *parameter;
1788b2d3 14141 struct attribute *loc, *origin;
96408a79 14142
216f72a1
JK
14143 if (child_die->tag != DW_TAG_call_site_parameter
14144 && child_die->tag != DW_TAG_GNU_call_site_parameter)
96408a79
SA
14145 {
14146 /* Already printed the complaint above. */
14147 continue;
14148 }
14149
14150 gdb_assert (call_site->parameter_count < nparams);
14151 parameter = &call_site->parameter[call_site->parameter_count];
14152
1788b2d3
JK
14153 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
14154 specifies DW_TAG_formal_parameter. Value of the data assumed for the
216f72a1 14155 register is contained in DW_AT_call_value. */
96408a79 14156
24c5c679 14157 loc = dwarf2_attr (child_die, DW_AT_location, cu);
216f72a1
JK
14158 origin = dwarf2_attr (child_die, DW_AT_call_parameter, cu);
14159 if (origin == NULL)
14160 {
14161 /* This was a pre-DWARF-5 GNU extension alias
14162 for DW_AT_call_parameter. */
14163 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
14164 }
7771576e 14165 if (loc == NULL && origin != NULL && attr_form_is_ref (origin))
1788b2d3 14166 {
1788b2d3 14167 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
9c541725
PA
14168
14169 sect_offset sect_off
14170 = (sect_offset) dwarf2_get_ref_die_offset (origin);
14171 if (!offset_in_cu_p (&cu->header, sect_off))
d76b7dbc
JK
14172 {
14173 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
14174 binding can be done only inside one CU. Such referenced DIE
14175 therefore cannot be even moved to DW_TAG_partial_unit. */
b98664d3 14176 complaint (_("DW_AT_call_parameter offset is not in CU for "
9d8780f0
SM
14177 "DW_TAG_call_site child DIE %s [in module %s]"),
14178 sect_offset_str (child_die->sect_off),
9c541725 14179 objfile_name (objfile));
d76b7dbc
JK
14180 continue;
14181 }
9c541725
PA
14182 parameter->u.param_cu_off
14183 = (cu_offset) (sect_off - cu->header.sect_off);
1788b2d3
JK
14184 }
14185 else if (loc == NULL || origin != NULL || !attr_form_is_block (loc))
96408a79 14186 {
b98664d3 14187 complaint (_("No DW_FORM_block* DW_AT_location for "
9d8780f0
SM
14188 "DW_TAG_call_site child DIE %s [in module %s]"),
14189 sect_offset_str (child_die->sect_off), objfile_name (objfile));
96408a79
SA
14190 continue;
14191 }
24c5c679 14192 else
96408a79 14193 {
24c5c679
JK
14194 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
14195 (DW_BLOCK (loc)->data, &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size]);
14196 if (parameter->u.dwarf_reg != -1)
14197 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
14198 else if (dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (loc)->data,
14199 &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size],
14200 &parameter->u.fb_offset))
14201 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
14202 else
14203 {
b98664d3 14204 complaint (_("Only single DW_OP_reg or DW_OP_fbreg is supported "
24c5c679 14205 "for DW_FORM_block* DW_AT_location is supported for "
9d8780f0 14206 "DW_TAG_call_site child DIE %s "
24c5c679 14207 "[in module %s]"),
9d8780f0 14208 sect_offset_str (child_die->sect_off),
9c541725 14209 objfile_name (objfile));
24c5c679
JK
14210 continue;
14211 }
96408a79
SA
14212 }
14213
216f72a1
JK
14214 attr = dwarf2_attr (child_die, DW_AT_call_value, cu);
14215 if (attr == NULL)
14216 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
96408a79
SA
14217 if (!attr_form_is_block (attr))
14218 {
b98664d3 14219 complaint (_("No DW_FORM_block* DW_AT_call_value for "
9d8780f0
SM
14220 "DW_TAG_call_site child DIE %s [in module %s]"),
14221 sect_offset_str (child_die->sect_off),
9c541725 14222 objfile_name (objfile));
96408a79
SA
14223 continue;
14224 }
14225 parameter->value = DW_BLOCK (attr)->data;
14226 parameter->value_size = DW_BLOCK (attr)->size;
14227
14228 /* Parameters are not pre-cleared by memset above. */
14229 parameter->data_value = NULL;
14230 parameter->data_value_size = 0;
14231 call_site->parameter_count++;
14232
216f72a1
JK
14233 attr = dwarf2_attr (child_die, DW_AT_call_data_value, cu);
14234 if (attr == NULL)
14235 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
96408a79
SA
14236 if (attr)
14237 {
14238 if (!attr_form_is_block (attr))
b98664d3 14239 complaint (_("No DW_FORM_block* DW_AT_call_data_value for "
9d8780f0
SM
14240 "DW_TAG_call_site child DIE %s [in module %s]"),
14241 sect_offset_str (child_die->sect_off),
9c541725 14242 objfile_name (objfile));
96408a79
SA
14243 else
14244 {
14245 parameter->data_value = DW_BLOCK (attr)->data;
14246 parameter->data_value_size = DW_BLOCK (attr)->size;
14247 }
14248 }
14249 }
14250}
14251
71a3c369
TT
14252/* Helper function for read_variable. If DIE represents a virtual
14253 table, then return the type of the concrete object that is
14254 associated with the virtual table. Otherwise, return NULL. */
14255
14256static struct type *
14257rust_containing_type (struct die_info *die, struct dwarf2_cu *cu)
14258{
14259 struct attribute *attr = dwarf2_attr (die, DW_AT_type, cu);
14260 if (attr == NULL)
14261 return NULL;
14262
14263 /* Find the type DIE. */
14264 struct die_info *type_die = NULL;
14265 struct dwarf2_cu *type_cu = cu;
14266
14267 if (attr_form_is_ref (attr))
14268 type_die = follow_die_ref (die, attr, &type_cu);
14269 if (type_die == NULL)
14270 return NULL;
14271
14272 if (dwarf2_attr (type_die, DW_AT_containing_type, type_cu) == NULL)
14273 return NULL;
14274 return die_containing_type (type_die, type_cu);
14275}
14276
14277/* Read a variable (DW_TAG_variable) DIE and create a new symbol. */
14278
14279static void
14280read_variable (struct die_info *die, struct dwarf2_cu *cu)
14281{
14282 struct rust_vtable_symbol *storage = NULL;
14283
14284 if (cu->language == language_rust)
14285 {
14286 struct type *containing_type = rust_containing_type (die, cu);
14287
14288 if (containing_type != NULL)
14289 {
518817b3 14290 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
71a3c369
TT
14291
14292 storage = OBSTACK_ZALLOC (&objfile->objfile_obstack,
14293 struct rust_vtable_symbol);
14294 initialize_objfile_symbol (storage);
14295 storage->concrete_type = containing_type;
cf724bc9 14296 storage->subclass = SYMBOL_RUST_VTABLE;
71a3c369
TT
14297 }
14298 }
14299
e4a62c65
TV
14300 struct symbol *res = new_symbol (die, NULL, cu, storage);
14301 struct attribute *abstract_origin
14302 = dwarf2_attr (die, DW_AT_abstract_origin, cu);
14303 struct attribute *loc = dwarf2_attr (die, DW_AT_location, cu);
14304 if (res == NULL && loc && abstract_origin)
14305 {
14306 /* We have a variable without a name, but with a location and an abstract
14307 origin. This may be a concrete instance of an abstract variable
14308 referenced from an DW_OP_GNU_variable_value, so save it to find it back
14309 later. */
14310 struct dwarf2_cu *origin_cu = cu;
14311 struct die_info *origin_die
14312 = follow_die_ref (die, abstract_origin, &origin_cu);
14313 dwarf2_per_objfile *dpo = cu->per_cu->dwarf2_per_objfile;
14314 dpo->abstract_to_concrete[origin_die].push_back (die);
14315 }
71a3c369
TT
14316}
14317
43988095
JK
14318/* Call CALLBACK from DW_AT_ranges attribute value OFFSET
14319 reading .debug_rnglists.
14320 Callback's type should be:
14321 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
14322 Return true if the attributes are present and valid, otherwise,
14323 return false. */
14324
14325template <typename Callback>
14326static bool
14327dwarf2_rnglists_process (unsigned offset, struct dwarf2_cu *cu,
14328 Callback &&callback)
14329{
ed2dc618 14330 struct dwarf2_per_objfile *dwarf2_per_objfile
518817b3 14331 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 14332 struct objfile *objfile = dwarf2_per_objfile->objfile;
43988095 14333 bfd *obfd = objfile->obfd;
43988095
JK
14334 /* Base address selection entry. */
14335 CORE_ADDR base;
14336 int found_base;
43988095 14337 const gdb_byte *buffer;
43988095
JK
14338 CORE_ADDR baseaddr;
14339 bool overflow = false;
14340
14341 found_base = cu->base_known;
14342 base = cu->base_address;
14343
14344 dwarf2_read_section (objfile, &dwarf2_per_objfile->rnglists);
14345 if (offset >= dwarf2_per_objfile->rnglists.size)
14346 {
b98664d3 14347 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
43988095
JK
14348 offset);
14349 return false;
14350 }
14351 buffer = dwarf2_per_objfile->rnglists.buffer + offset;
14352
14353 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
14354
14355 while (1)
14356 {
7814882a
JK
14357 /* Initialize it due to a false compiler warning. */
14358 CORE_ADDR range_beginning = 0, range_end = 0;
43988095
JK
14359 const gdb_byte *buf_end = (dwarf2_per_objfile->rnglists.buffer
14360 + dwarf2_per_objfile->rnglists.size);
14361 unsigned int bytes_read;
14362
14363 if (buffer == buf_end)
14364 {
14365 overflow = true;
14366 break;
14367 }
14368 const auto rlet = static_cast<enum dwarf_range_list_entry>(*buffer++);
14369 switch (rlet)
14370 {
14371 case DW_RLE_end_of_list:
14372 break;
14373 case DW_RLE_base_address:
14374 if (buffer + cu->header.addr_size > buf_end)
14375 {
14376 overflow = true;
14377 break;
14378 }
14379 base = read_address (obfd, buffer, cu, &bytes_read);
14380 found_base = 1;
14381 buffer += bytes_read;
14382 break;
14383 case DW_RLE_start_length:
14384 if (buffer + cu->header.addr_size > buf_end)
14385 {
14386 overflow = true;
14387 break;
14388 }
14389 range_beginning = read_address (obfd, buffer, cu, &bytes_read);
14390 buffer += bytes_read;
14391 range_end = (range_beginning
14392 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
14393 buffer += bytes_read;
14394 if (buffer > buf_end)
14395 {
14396 overflow = true;
14397 break;
14398 }
14399 break;
14400 case DW_RLE_offset_pair:
14401 range_beginning = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14402 buffer += bytes_read;
14403 if (buffer > buf_end)
14404 {
14405 overflow = true;
14406 break;
14407 }
14408 range_end = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14409 buffer += bytes_read;
14410 if (buffer > buf_end)
14411 {
14412 overflow = true;
14413 break;
14414 }
14415 break;
14416 case DW_RLE_start_end:
14417 if (buffer + 2 * cu->header.addr_size > buf_end)
14418 {
14419 overflow = true;
14420 break;
14421 }
14422 range_beginning = read_address (obfd, buffer, cu, &bytes_read);
14423 buffer += bytes_read;
14424 range_end = read_address (obfd, buffer, cu, &bytes_read);
14425 buffer += bytes_read;
14426 break;
14427 default:
b98664d3 14428 complaint (_("Invalid .debug_rnglists data (no base address)"));
43988095
JK
14429 return false;
14430 }
14431 if (rlet == DW_RLE_end_of_list || overflow)
14432 break;
14433 if (rlet == DW_RLE_base_address)
14434 continue;
14435
14436 if (!found_base)
14437 {
14438 /* We have no valid base address for the ranges
14439 data. */
b98664d3 14440 complaint (_("Invalid .debug_rnglists data (no base address)"));
43988095
JK
14441 return false;
14442 }
14443
14444 if (range_beginning > range_end)
14445 {
14446 /* Inverted range entries are invalid. */
b98664d3 14447 complaint (_("Invalid .debug_rnglists data (inverted range)"));
43988095
JK
14448 return false;
14449 }
14450
14451 /* Empty range entries have no effect. */
14452 if (range_beginning == range_end)
14453 continue;
14454
14455 range_beginning += base;
14456 range_end += base;
14457
14458 /* A not-uncommon case of bad debug info.
14459 Don't pollute the addrmap with bad data. */
14460 if (range_beginning + baseaddr == 0
14461 && !dwarf2_per_objfile->has_section_at_zero)
14462 {
b98664d3 14463 complaint (_(".debug_rnglists entry has start address of zero"
43988095
JK
14464 " [in module %s]"), objfile_name (objfile));
14465 continue;
14466 }
14467
14468 callback (range_beginning, range_end);
14469 }
14470
14471 if (overflow)
14472 {
b98664d3 14473 complaint (_("Offset %d is not terminated "
43988095
JK
14474 "for DW_AT_ranges attribute"),
14475 offset);
14476 return false;
14477 }
14478
14479 return true;
14480}
14481
14482/* Call CALLBACK from DW_AT_ranges attribute value OFFSET reading .debug_ranges.
14483 Callback's type should be:
14484 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
5f46c5a5 14485 Return 1 if the attributes are present and valid, otherwise, return 0. */
43039443 14486
43988095 14487template <typename Callback>
43039443 14488static int
5f46c5a5 14489dwarf2_ranges_process (unsigned offset, struct dwarf2_cu *cu,
43988095 14490 Callback &&callback)
43039443 14491{
ed2dc618 14492 struct dwarf2_per_objfile *dwarf2_per_objfile
518817b3 14493 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 14494 struct objfile *objfile = dwarf2_per_objfile->objfile;
43039443
JK
14495 struct comp_unit_head *cu_header = &cu->header;
14496 bfd *obfd = objfile->obfd;
14497 unsigned int addr_size = cu_header->addr_size;
14498 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
14499 /* Base address selection entry. */
14500 CORE_ADDR base;
14501 int found_base;
14502 unsigned int dummy;
d521ce57 14503 const gdb_byte *buffer;
ff013f42 14504 CORE_ADDR baseaddr;
43039443 14505
43988095
JK
14506 if (cu_header->version >= 5)
14507 return dwarf2_rnglists_process (offset, cu, callback);
14508
d00adf39
DE
14509 found_base = cu->base_known;
14510 base = cu->base_address;
43039443 14511
be391dca 14512 dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
dce234bc 14513 if (offset >= dwarf2_per_objfile->ranges.size)
43039443 14514 {
b98664d3 14515 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
43039443
JK
14516 offset);
14517 return 0;
14518 }
dce234bc 14519 buffer = dwarf2_per_objfile->ranges.buffer + offset;
43039443 14520
e7030f15 14521 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
ff013f42 14522
43039443
JK
14523 while (1)
14524 {
14525 CORE_ADDR range_beginning, range_end;
14526
14527 range_beginning = read_address (obfd, buffer, cu, &dummy);
14528 buffer += addr_size;
14529 range_end = read_address (obfd, buffer, cu, &dummy);
14530 buffer += addr_size;
14531 offset += 2 * addr_size;
14532
14533 /* An end of list marker is a pair of zero addresses. */
14534 if (range_beginning == 0 && range_end == 0)
14535 /* Found the end of list entry. */
14536 break;
14537
14538 /* Each base address selection entry is a pair of 2 values.
14539 The first is the largest possible address, the second is
14540 the base address. Check for a base address here. */
14541 if ((range_beginning & mask) == mask)
14542 {
28d2bfb9
AB
14543 /* If we found the largest possible address, then we already
14544 have the base address in range_end. */
14545 base = range_end;
43039443
JK
14546 found_base = 1;
14547 continue;
14548 }
14549
14550 if (!found_base)
14551 {
14552 /* We have no valid base address for the ranges
14553 data. */
b98664d3 14554 complaint (_("Invalid .debug_ranges data (no base address)"));
43039443
JK
14555 return 0;
14556 }
14557
9277c30c
UW
14558 if (range_beginning > range_end)
14559 {
14560 /* Inverted range entries are invalid. */
b98664d3 14561 complaint (_("Invalid .debug_ranges data (inverted range)"));
9277c30c
UW
14562 return 0;
14563 }
14564
14565 /* Empty range entries have no effect. */
14566 if (range_beginning == range_end)
14567 continue;
14568
43039443
JK
14569 range_beginning += base;
14570 range_end += base;
14571
01093045
DE
14572 /* A not-uncommon case of bad debug info.
14573 Don't pollute the addrmap with bad data. */
14574 if (range_beginning + baseaddr == 0
14575 && !dwarf2_per_objfile->has_section_at_zero)
14576 {
b98664d3 14577 complaint (_(".debug_ranges entry has start address of zero"
4262abfb 14578 " [in module %s]"), objfile_name (objfile));
01093045
DE
14579 continue;
14580 }
14581
5f46c5a5
JK
14582 callback (range_beginning, range_end);
14583 }
14584
14585 return 1;
14586}
14587
14588/* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
14589 Return 1 if the attributes are present and valid, otherwise, return 0.
14590 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
14591
14592static int
14593dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
14594 CORE_ADDR *high_return, struct dwarf2_cu *cu,
14595 struct partial_symtab *ranges_pst)
14596{
518817b3 14597 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
5f46c5a5
JK
14598 struct gdbarch *gdbarch = get_objfile_arch (objfile);
14599 const CORE_ADDR baseaddr = ANOFFSET (objfile->section_offsets,
14600 SECT_OFF_TEXT (objfile));
14601 int low_set = 0;
14602 CORE_ADDR low = 0;
14603 CORE_ADDR high = 0;
14604 int retval;
14605
14606 retval = dwarf2_ranges_process (offset, cu,
14607 [&] (CORE_ADDR range_beginning, CORE_ADDR range_end)
14608 {
9277c30c 14609 if (ranges_pst != NULL)
3e29f34a
MR
14610 {
14611 CORE_ADDR lowpc;
14612 CORE_ADDR highpc;
14613
79748972
TT
14614 lowpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
14615 range_beginning + baseaddr)
14616 - baseaddr);
14617 highpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
14618 range_end + baseaddr)
14619 - baseaddr);
d320c2b5
TT
14620 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
14621 lowpc, highpc - 1, ranges_pst);
3e29f34a 14622 }
ff013f42 14623
43039443
JK
14624 /* FIXME: This is recording everything as a low-high
14625 segment of consecutive addresses. We should have a
14626 data structure for discontiguous block ranges
14627 instead. */
14628 if (! low_set)
14629 {
14630 low = range_beginning;
14631 high = range_end;
14632 low_set = 1;
14633 }
14634 else
14635 {
14636 if (range_beginning < low)
14637 low = range_beginning;
14638 if (range_end > high)
14639 high = range_end;
14640 }
5f46c5a5
JK
14641 });
14642 if (!retval)
14643 return 0;
43039443
JK
14644
14645 if (! low_set)
14646 /* If the first entry is an end-of-list marker, the range
14647 describes an empty scope, i.e. no instructions. */
14648 return 0;
14649
14650 if (low_return)
14651 *low_return = low;
14652 if (high_return)
14653 *high_return = high;
14654 return 1;
14655}
14656
3a2b436a
JK
14657/* Get low and high pc attributes from a die. See enum pc_bounds_kind
14658 definition for the return value. *LOWPC and *HIGHPC are set iff
e385593e 14659 neither PC_BOUNDS_NOT_PRESENT nor PC_BOUNDS_INVALID are returned. */
380bca97 14660
3a2b436a 14661static enum pc_bounds_kind
af34e669 14662dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
d85a05f0
DJ
14663 CORE_ADDR *highpc, struct dwarf2_cu *cu,
14664 struct partial_symtab *pst)
c906108c 14665{
518817b3
SM
14666 struct dwarf2_per_objfile *dwarf2_per_objfile
14667 = cu->per_cu->dwarf2_per_objfile;
c906108c 14668 struct attribute *attr;
91da1414 14669 struct attribute *attr_high;
af34e669
DJ
14670 CORE_ADDR low = 0;
14671 CORE_ADDR high = 0;
e385593e 14672 enum pc_bounds_kind ret;
c906108c 14673
91da1414
MW
14674 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14675 if (attr_high)
af34e669 14676 {
e142c38c 14677 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
af34e669 14678 if (attr)
91da1414 14679 {
31aa7e4e
JB
14680 low = attr_value_as_address (attr);
14681 high = attr_value_as_address (attr_high);
14682 if (cu->header.version >= 4 && attr_form_is_constant (attr_high))
14683 high += low;
91da1414 14684 }
af34e669
DJ
14685 else
14686 /* Found high w/o low attribute. */
e385593e 14687 return PC_BOUNDS_INVALID;
af34e669
DJ
14688
14689 /* Found consecutive range of addresses. */
3a2b436a 14690 ret = PC_BOUNDS_HIGH_LOW;
af34e669 14691 }
c906108c 14692 else
af34e669 14693 {
e142c38c 14694 attr = dwarf2_attr (die, DW_AT_ranges, cu);
af34e669
DJ
14695 if (attr != NULL)
14696 {
ab435259
DE
14697 /* DW_AT_ranges_base does not apply to DIEs from the DWO skeleton.
14698 We take advantage of the fact that DW_AT_ranges does not appear
14699 in DW_TAG_compile_unit of DWO files. */
14700 int need_ranges_base = die->tag != DW_TAG_compile_unit;
14701 unsigned int ranges_offset = (DW_UNSND (attr)
14702 + (need_ranges_base
14703 ? cu->ranges_base
14704 : 0));
2e3cf129 14705
af34e669 14706 /* Value of the DW_AT_ranges attribute is the offset in the
a604369a 14707 .debug_ranges section. */
2e3cf129 14708 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst))
e385593e 14709 return PC_BOUNDS_INVALID;
43039443 14710 /* Found discontinuous range of addresses. */
3a2b436a 14711 ret = PC_BOUNDS_RANGES;
af34e669 14712 }
e385593e
JK
14713 else
14714 return PC_BOUNDS_NOT_PRESENT;
af34e669 14715 }
c906108c 14716
48fbe735 14717 /* partial_die_info::read has also the strict LOW < HIGH requirement. */
9373cf26 14718 if (high <= low)
e385593e 14719 return PC_BOUNDS_INVALID;
c906108c
SS
14720
14721 /* When using the GNU linker, .gnu.linkonce. sections are used to
14722 eliminate duplicate copies of functions and vtables and such.
14723 The linker will arbitrarily choose one and discard the others.
14724 The AT_*_pc values for such functions refer to local labels in
14725 these sections. If the section from that file was discarded, the
14726 labels are not in the output, so the relocs get a value of 0.
14727 If this is a discarded function, mark the pc bounds as invalid,
14728 so that GDB will ignore it. */
72dca2f5 14729 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
e385593e 14730 return PC_BOUNDS_INVALID;
c906108c
SS
14731
14732 *lowpc = low;
96408a79
SA
14733 if (highpc)
14734 *highpc = high;
af34e669 14735 return ret;
c906108c
SS
14736}
14737
b084d499
JB
14738/* Assuming that DIE represents a subprogram DIE or a lexical block, get
14739 its low and high PC addresses. Do nothing if these addresses could not
14740 be determined. Otherwise, set LOWPC to the low address if it is smaller,
14741 and HIGHPC to the high address if greater than HIGHPC. */
14742
14743static void
14744dwarf2_get_subprogram_pc_bounds (struct die_info *die,
14745 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14746 struct dwarf2_cu *cu)
14747{
14748 CORE_ADDR low, high;
14749 struct die_info *child = die->child;
14750
e385593e 14751 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL) >= PC_BOUNDS_RANGES)
b084d499 14752 {
325fac50
PA
14753 *lowpc = std::min (*lowpc, low);
14754 *highpc = std::max (*highpc, high);
b084d499
JB
14755 }
14756
14757 /* If the language does not allow nested subprograms (either inside
14758 subprograms or lexical blocks), we're done. */
14759 if (cu->language != language_ada)
14760 return;
6e70227d 14761
b084d499
JB
14762 /* Check all the children of the given DIE. If it contains nested
14763 subprograms, then check their pc bounds. Likewise, we need to
14764 check lexical blocks as well, as they may also contain subprogram
14765 definitions. */
14766 while (child && child->tag)
14767 {
14768 if (child->tag == DW_TAG_subprogram
14769 || child->tag == DW_TAG_lexical_block)
14770 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
14771 child = sibling_die (child);
14772 }
14773}
14774
fae299cd
DC
14775/* Get the low and high pc's represented by the scope DIE, and store
14776 them in *LOWPC and *HIGHPC. If the correct values can't be
14777 determined, set *LOWPC to -1 and *HIGHPC to 0. */
14778
14779static void
14780get_scope_pc_bounds (struct die_info *die,
14781 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14782 struct dwarf2_cu *cu)
14783{
14784 CORE_ADDR best_low = (CORE_ADDR) -1;
14785 CORE_ADDR best_high = (CORE_ADDR) 0;
14786 CORE_ADDR current_low, current_high;
14787
3a2b436a 14788 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL)
e385593e 14789 >= PC_BOUNDS_RANGES)
fae299cd
DC
14790 {
14791 best_low = current_low;
14792 best_high = current_high;
14793 }
14794 else
14795 {
14796 struct die_info *child = die->child;
14797
14798 while (child && child->tag)
14799 {
14800 switch (child->tag) {
14801 case DW_TAG_subprogram:
b084d499 14802 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
fae299cd
DC
14803 break;
14804 case DW_TAG_namespace:
f55ee35c 14805 case DW_TAG_module:
fae299cd
DC
14806 /* FIXME: carlton/2004-01-16: Should we do this for
14807 DW_TAG_class_type/DW_TAG_structure_type, too? I think
14808 that current GCC's always emit the DIEs corresponding
14809 to definitions of methods of classes as children of a
14810 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
14811 the DIEs giving the declarations, which could be
14812 anywhere). But I don't see any reason why the
14813 standards says that they have to be there. */
14814 get_scope_pc_bounds (child, &current_low, &current_high, cu);
14815
14816 if (current_low != ((CORE_ADDR) -1))
14817 {
325fac50
PA
14818 best_low = std::min (best_low, current_low);
14819 best_high = std::max (best_high, current_high);
fae299cd
DC
14820 }
14821 break;
14822 default:
0963b4bd 14823 /* Ignore. */
fae299cd
DC
14824 break;
14825 }
14826
14827 child = sibling_die (child);
14828 }
14829 }
14830
14831 *lowpc = best_low;
14832 *highpc = best_high;
14833}
14834
801e3a5b
JB
14835/* Record the address ranges for BLOCK, offset by BASEADDR, as given
14836 in DIE. */
380bca97 14837
801e3a5b
JB
14838static void
14839dwarf2_record_block_ranges (struct die_info *die, struct block *block,
14840 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
14841{
518817b3 14842 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
3e29f34a 14843 struct gdbarch *gdbarch = get_objfile_arch (objfile);
801e3a5b 14844 struct attribute *attr;
91da1414 14845 struct attribute *attr_high;
801e3a5b 14846
91da1414
MW
14847 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14848 if (attr_high)
801e3a5b 14849 {
801e3a5b
JB
14850 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
14851 if (attr)
14852 {
31aa7e4e
JB
14853 CORE_ADDR low = attr_value_as_address (attr);
14854 CORE_ADDR high = attr_value_as_address (attr_high);
14855
14856 if (cu->header.version >= 4 && attr_form_is_constant (attr_high))
14857 high += low;
9a619af0 14858
3e29f34a
MR
14859 low = gdbarch_adjust_dwarf2_addr (gdbarch, low + baseaddr);
14860 high = gdbarch_adjust_dwarf2_addr (gdbarch, high + baseaddr);
c24bdb02 14861 cu->get_builder ()->record_block_range (block, low, high - 1);
801e3a5b
JB
14862 }
14863 }
14864
14865 attr = dwarf2_attr (die, DW_AT_ranges, cu);
14866 if (attr)
14867 {
ab435259
DE
14868 /* DW_AT_ranges_base does not apply to DIEs from the DWO skeleton.
14869 We take advantage of the fact that DW_AT_ranges does not appear
14870 in DW_TAG_compile_unit of DWO files. */
14871 int need_ranges_base = die->tag != DW_TAG_compile_unit;
801e3a5b
JB
14872
14873 /* The value of the DW_AT_ranges attribute is the offset of the
14874 address range list in the .debug_ranges section. */
ab435259
DE
14875 unsigned long offset = (DW_UNSND (attr)
14876 + (need_ranges_base ? cu->ranges_base : 0));
801e3a5b 14877
2d5f09ec 14878 std::vector<blockrange> blockvec;
5f46c5a5
JK
14879 dwarf2_ranges_process (offset, cu,
14880 [&] (CORE_ADDR start, CORE_ADDR end)
14881 {
58fdfd2c
JK
14882 start += baseaddr;
14883 end += baseaddr;
5f46c5a5
JK
14884 start = gdbarch_adjust_dwarf2_addr (gdbarch, start);
14885 end = gdbarch_adjust_dwarf2_addr (gdbarch, end);
c24bdb02 14886 cu->get_builder ()->record_block_range (block, start, end - 1);
2d5f09ec 14887 blockvec.emplace_back (start, end);
5f46c5a5 14888 });
2d5f09ec
KB
14889
14890 BLOCK_RANGES(block) = make_blockranges (objfile, blockvec);
801e3a5b
JB
14891 }
14892}
14893
685b1105
JK
14894/* Check whether the producer field indicates either of GCC < 4.6, or the
14895 Intel C/C++ compiler, and cache the result in CU. */
60d5a603 14896
685b1105
JK
14897static void
14898check_producer (struct dwarf2_cu *cu)
60d5a603 14899{
38360086 14900 int major, minor;
60d5a603
JK
14901
14902 if (cu->producer == NULL)
14903 {
14904 /* For unknown compilers expect their behavior is DWARF version
14905 compliant.
14906
14907 GCC started to support .debug_types sections by -gdwarf-4 since
14908 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
14909 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
14910 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
14911 interpreted incorrectly by GDB now - GCC PR debug/48229. */
60d5a603 14912 }
b1ffba5a 14913 else if (producer_is_gcc (cu->producer, &major, &minor))
60d5a603 14914 {
38360086
MW
14915 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
14916 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
685b1105 14917 }
5230b05a 14918 else if (producer_is_icc (cu->producer, &major, &minor))
eb77c9df
AB
14919 {
14920 cu->producer_is_icc = true;
14921 cu->producer_is_icc_lt_14 = major < 14;
14922 }
c258c396
JD
14923 else if (startswith (cu->producer, "CodeWarrior S12/L-ISA"))
14924 cu->producer_is_codewarrior = true;
685b1105
JK
14925 else
14926 {
14927 /* For other non-GCC compilers, expect their behavior is DWARF version
14928 compliant. */
60d5a603
JK
14929 }
14930
9068261f 14931 cu->checked_producer = true;
685b1105 14932}
ba919b58 14933
685b1105
JK
14934/* Check for GCC PR debug/45124 fix which is not present in any G++ version up
14935 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
14936 during 4.6.0 experimental. */
14937
9068261f 14938static bool
685b1105
JK
14939producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
14940{
14941 if (!cu->checked_producer)
14942 check_producer (cu);
14943
14944 return cu->producer_is_gxx_lt_4_6;
60d5a603
JK
14945}
14946
c258c396
JD
14947
14948/* Codewarrior (at least as of version 5.0.40) generates dwarf line information
14949 with incorrect is_stmt attributes. */
14950
14951static bool
14952producer_is_codewarrior (struct dwarf2_cu *cu)
14953{
14954 if (!cu->checked_producer)
14955 check_producer (cu);
14956
14957 return cu->producer_is_codewarrior;
14958}
14959
60d5a603
JK
14960/* Return the default accessibility type if it is not overriden by
14961 DW_AT_accessibility. */
14962
14963static enum dwarf_access_attribute
14964dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
14965{
14966 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
14967 {
14968 /* The default DWARF 2 accessibility for members is public, the default
14969 accessibility for inheritance is private. */
14970
14971 if (die->tag != DW_TAG_inheritance)
14972 return DW_ACCESS_public;
14973 else
14974 return DW_ACCESS_private;
14975 }
14976 else
14977 {
14978 /* DWARF 3+ defines the default accessibility a different way. The same
14979 rules apply now for DW_TAG_inheritance as for the members and it only
14980 depends on the container kind. */
14981
14982 if (die->parent->tag == DW_TAG_class_type)
14983 return DW_ACCESS_private;
14984 else
14985 return DW_ACCESS_public;
14986 }
14987}
14988
74ac6d43
TT
14989/* Look for DW_AT_data_member_location. Set *OFFSET to the byte
14990 offset. If the attribute was not found return 0, otherwise return
14991 1. If it was found but could not properly be handled, set *OFFSET
14992 to 0. */
14993
14994static int
14995handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
14996 LONGEST *offset)
14997{
14998 struct attribute *attr;
14999
15000 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
15001 if (attr != NULL)
15002 {
15003 *offset = 0;
15004
15005 /* Note that we do not check for a section offset first here.
15006 This is because DW_AT_data_member_location is new in DWARF 4,
15007 so if we see it, we can assume that a constant form is really
15008 a constant and not a section offset. */
15009 if (attr_form_is_constant (attr))
15010 *offset = dwarf2_get_attr_constant_value (attr, 0);
15011 else if (attr_form_is_section_offset (attr))
15012 dwarf2_complex_location_expr_complaint ();
15013 else if (attr_form_is_block (attr))
15014 *offset = decode_locdesc (DW_BLOCK (attr), cu);
15015 else
15016 dwarf2_complex_location_expr_complaint ();
15017
15018 return 1;
15019 }
15020
15021 return 0;
15022}
15023
c906108c
SS
15024/* Add an aggregate field to the field list. */
15025
15026static void
107d2387 15027dwarf2_add_field (struct field_info *fip, struct die_info *die,
e7c27a73 15028 struct dwarf2_cu *cu)
6e70227d 15029{
518817b3 15030 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
5e2b427d 15031 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
15032 struct nextfield *new_field;
15033 struct attribute *attr;
15034 struct field *fp;
15d034d0 15035 const char *fieldname = "";
c906108c 15036
7d0ccb61
DJ
15037 if (die->tag == DW_TAG_inheritance)
15038 {
be2daae6
TT
15039 fip->baseclasses.emplace_back ();
15040 new_field = &fip->baseclasses.back ();
7d0ccb61
DJ
15041 }
15042 else
15043 {
be2daae6
TT
15044 fip->fields.emplace_back ();
15045 new_field = &fip->fields.back ();
7d0ccb61 15046 }
be2daae6 15047
c906108c
SS
15048 fip->nfields++;
15049
e142c38c 15050 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
c906108c
SS
15051 if (attr)
15052 new_field->accessibility = DW_UNSND (attr);
60d5a603
JK
15053 else
15054 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
c906108c
SS
15055 if (new_field->accessibility != DW_ACCESS_public)
15056 fip->non_public_fields = 1;
60d5a603 15057
e142c38c 15058 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
c906108c
SS
15059 if (attr)
15060 new_field->virtuality = DW_UNSND (attr);
60d5a603
JK
15061 else
15062 new_field->virtuality = DW_VIRTUALITY_none;
c906108c
SS
15063
15064 fp = &new_field->field;
a9a9bd0f 15065
e142c38c 15066 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
c906108c 15067 {
74ac6d43
TT
15068 LONGEST offset;
15069
a9a9bd0f 15070 /* Data member other than a C++ static data member. */
6e70227d 15071
c906108c 15072 /* Get type of field. */
e7c27a73 15073 fp->type = die_type (die, cu);
c906108c 15074
d6a843b5 15075 SET_FIELD_BITPOS (*fp, 0);
01ad7f36 15076
c906108c 15077 /* Get bit size of field (zero if none). */
e142c38c 15078 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
c906108c
SS
15079 if (attr)
15080 {
15081 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
15082 }
15083 else
15084 {
15085 FIELD_BITSIZE (*fp) = 0;
15086 }
15087
15088 /* Get bit offset of field. */
74ac6d43
TT
15089 if (handle_data_member_location (die, cu, &offset))
15090 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
e142c38c 15091 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
c906108c
SS
15092 if (attr)
15093 {
5e2b427d 15094 if (gdbarch_bits_big_endian (gdbarch))
c906108c
SS
15095 {
15096 /* For big endian bits, the DW_AT_bit_offset gives the
c5aa993b
JM
15097 additional bit offset from the MSB of the containing
15098 anonymous object to the MSB of the field. We don't
15099 have to do anything special since we don't need to
15100 know the size of the anonymous object. */
f41f5e61 15101 SET_FIELD_BITPOS (*fp, FIELD_BITPOS (*fp) + DW_UNSND (attr));
c906108c
SS
15102 }
15103 else
15104 {
15105 /* For little endian bits, compute the bit offset to the
c5aa993b
JM
15106 MSB of the anonymous object, subtract off the number of
15107 bits from the MSB of the field to the MSB of the
15108 object, and then subtract off the number of bits of
15109 the field itself. The result is the bit offset of
15110 the LSB of the field. */
c906108c
SS
15111 int anonymous_size;
15112 int bit_offset = DW_UNSND (attr);
15113
e142c38c 15114 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
15115 if (attr)
15116 {
15117 /* The size of the anonymous object containing
15118 the bit field is explicit, so use the
15119 indicated size (in bytes). */
15120 anonymous_size = DW_UNSND (attr);
15121 }
15122 else
15123 {
15124 /* The size of the anonymous object containing
15125 the bit field must be inferred from the type
15126 attribute of the data member containing the
15127 bit field. */
15128 anonymous_size = TYPE_LENGTH (fp->type);
15129 }
f41f5e61
PA
15130 SET_FIELD_BITPOS (*fp,
15131 (FIELD_BITPOS (*fp)
15132 + anonymous_size * bits_per_byte
15133 - bit_offset - FIELD_BITSIZE (*fp)));
c906108c
SS
15134 }
15135 }
da5b30da
AA
15136 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
15137 if (attr != NULL)
15138 SET_FIELD_BITPOS (*fp, (FIELD_BITPOS (*fp)
15139 + dwarf2_get_attr_constant_value (attr, 0)));
c906108c
SS
15140
15141 /* Get name of field. */
39cbfefa
DJ
15142 fieldname = dwarf2_name (die, cu);
15143 if (fieldname == NULL)
15144 fieldname = "";
d8151005
DJ
15145
15146 /* The name is already allocated along with this objfile, so we don't
15147 need to duplicate it for the type. */
15148 fp->name = fieldname;
c906108c
SS
15149
15150 /* Change accessibility for artificial fields (e.g. virtual table
c5aa993b 15151 pointer or virtual base class pointer) to private. */
e142c38c 15152 if (dwarf2_attr (die, DW_AT_artificial, cu))
c906108c 15153 {
d48cc9dd 15154 FIELD_ARTIFICIAL (*fp) = 1;
c906108c
SS
15155 new_field->accessibility = DW_ACCESS_private;
15156 fip->non_public_fields = 1;
15157 }
15158 }
a9a9bd0f 15159 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
c906108c 15160 {
a9a9bd0f
DC
15161 /* C++ static member. */
15162
15163 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
15164 is a declaration, but all versions of G++ as of this writing
15165 (so through at least 3.2.1) incorrectly generate
15166 DW_TAG_variable tags. */
6e70227d 15167
ff355380 15168 const char *physname;
c906108c 15169
a9a9bd0f 15170 /* Get name of field. */
39cbfefa
DJ
15171 fieldname = dwarf2_name (die, cu);
15172 if (fieldname == NULL)
c906108c
SS
15173 return;
15174
254e6b9e 15175 attr = dwarf2_attr (die, DW_AT_const_value, cu);
3863f96c
DE
15176 if (attr
15177 /* Only create a symbol if this is an external value.
15178 new_symbol checks this and puts the value in the global symbol
15179 table, which we want. If it is not external, new_symbol
15180 will try to put the value in cu->list_in_scope which is wrong. */
15181 && dwarf2_flag_true_p (die, DW_AT_external, cu))
254e6b9e
DE
15182 {
15183 /* A static const member, not much different than an enum as far as
15184 we're concerned, except that we can support more types. */
15185 new_symbol (die, NULL, cu);
15186 }
15187
2df3850c 15188 /* Get physical name. */
ff355380 15189 physname = dwarf2_physname (fieldname, die, cu);
c906108c 15190
d8151005
DJ
15191 /* The name is already allocated along with this objfile, so we don't
15192 need to duplicate it for the type. */
15193 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
e7c27a73 15194 FIELD_TYPE (*fp) = die_type (die, cu);
d8151005 15195 FIELD_NAME (*fp) = fieldname;
c906108c
SS
15196 }
15197 else if (die->tag == DW_TAG_inheritance)
15198 {
74ac6d43 15199 LONGEST offset;
d4b96c9a 15200
74ac6d43
TT
15201 /* C++ base class field. */
15202 if (handle_data_member_location (die, cu, &offset))
15203 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
c906108c 15204 FIELD_BITSIZE (*fp) = 0;
e7c27a73 15205 FIELD_TYPE (*fp) = die_type (die, cu);
a737d952 15206 FIELD_NAME (*fp) = TYPE_NAME (fp->type);
c906108c 15207 }
2ddeaf8a
TT
15208 else if (die->tag == DW_TAG_variant_part)
15209 {
15210 /* process_structure_scope will treat this DIE as a union. */
15211 process_structure_scope (die, cu);
15212
15213 /* The variant part is relative to the start of the enclosing
15214 structure. */
15215 SET_FIELD_BITPOS (*fp, 0);
15216 fp->type = get_die_type (die, cu);
15217 fp->artificial = 1;
15218 fp->name = "<<variant>>";
c8c81635
TT
15219
15220 /* Normally a DW_TAG_variant_part won't have a size, but our
15221 representation requires one, so set it to the maximum of the
15222 child sizes. */
15223 if (TYPE_LENGTH (fp->type) == 0)
15224 {
15225 unsigned max = 0;
15226 for (int i = 0; i < TYPE_NFIELDS (fp->type); ++i)
15227 if (TYPE_LENGTH (TYPE_FIELD_TYPE (fp->type, i)) > max)
15228 max = TYPE_LENGTH (TYPE_FIELD_TYPE (fp->type, i));
15229 TYPE_LENGTH (fp->type) = max;
15230 }
2ddeaf8a
TT
15231 }
15232 else
15233 gdb_assert_not_reached ("missing case in dwarf2_add_field");
c906108c
SS
15234}
15235
883fd55a
KS
15236/* Can the type given by DIE define another type? */
15237
15238static bool
15239type_can_define_types (const struct die_info *die)
15240{
15241 switch (die->tag)
15242 {
15243 case DW_TAG_typedef:
15244 case DW_TAG_class_type:
15245 case DW_TAG_structure_type:
15246 case DW_TAG_union_type:
15247 case DW_TAG_enumeration_type:
15248 return true;
15249
15250 default:
15251 return false;
15252 }
15253}
15254
15255/* Add a type definition defined in the scope of the FIP's class. */
98751a41
JK
15256
15257static void
883fd55a
KS
15258dwarf2_add_type_defn (struct field_info *fip, struct die_info *die,
15259 struct dwarf2_cu *cu)
6e70227d 15260{
be2daae6
TT
15261 struct decl_field fp;
15262 memset (&fp, 0, sizeof (fp));
98751a41 15263
883fd55a 15264 gdb_assert (type_can_define_types (die));
98751a41 15265
883fd55a 15266 /* Get name of field. NULL is okay here, meaning an anonymous type. */
be2daae6
TT
15267 fp.name = dwarf2_name (die, cu);
15268 fp.type = read_type_die (die, cu);
98751a41 15269
c191a687
KS
15270 /* Save accessibility. */
15271 enum dwarf_access_attribute accessibility;
15272 struct attribute *attr = dwarf2_attr (die, DW_AT_accessibility, cu);
15273 if (attr != NULL)
15274 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
15275 else
15276 accessibility = dwarf2_default_access_attribute (die, cu);
15277 switch (accessibility)
15278 {
15279 case DW_ACCESS_public:
15280 /* The assumed value if neither private nor protected. */
15281 break;
15282 case DW_ACCESS_private:
be2daae6 15283 fp.is_private = 1;
c191a687
KS
15284 break;
15285 case DW_ACCESS_protected:
be2daae6 15286 fp.is_protected = 1;
c191a687
KS
15287 break;
15288 default:
b98664d3 15289 complaint (_("Unhandled DW_AT_accessibility value (%x)"), accessibility);
c191a687
KS
15290 }
15291
883fd55a 15292 if (die->tag == DW_TAG_typedef)
be2daae6 15293 fip->typedef_field_list.push_back (fp);
883fd55a 15294 else
be2daae6 15295 fip->nested_types_list.push_back (fp);
98751a41
JK
15296}
15297
c906108c
SS
15298/* Create the vector of fields, and attach it to the type. */
15299
15300static void
fba45db2 15301dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 15302 struct dwarf2_cu *cu)
c906108c
SS
15303{
15304 int nfields = fip->nfields;
15305
15306 /* Record the field count, allocate space for the array of fields,
15307 and create blank accessibility bitfields if necessary. */
15308 TYPE_NFIELDS (type) = nfields;
15309 TYPE_FIELDS (type) = (struct field *)
be2daae6 15310 TYPE_ZALLOC (type, sizeof (struct field) * nfields);
c906108c 15311
b4ba55a1 15312 if (fip->non_public_fields && cu->language != language_ada)
c906108c
SS
15313 {
15314 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15315
15316 TYPE_FIELD_PRIVATE_BITS (type) =
15317 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15318 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
15319
15320 TYPE_FIELD_PROTECTED_BITS (type) =
15321 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15322 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
15323
774b6a14
TT
15324 TYPE_FIELD_IGNORE_BITS (type) =
15325 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15326 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
c906108c
SS
15327 }
15328
15329 /* If the type has baseclasses, allocate and clear a bit vector for
15330 TYPE_FIELD_VIRTUAL_BITS. */
be2daae6 15331 if (!fip->baseclasses.empty () && cu->language != language_ada)
c906108c 15332 {
be2daae6 15333 int num_bytes = B_BYTES (fip->baseclasses.size ());
fe1b8b76 15334 unsigned char *pointer;
c906108c
SS
15335
15336 ALLOCATE_CPLUS_STRUCT_TYPE (type);
224c3ddb 15337 pointer = (unsigned char *) TYPE_ALLOC (type, num_bytes);
fe1b8b76 15338 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
be2daae6
TT
15339 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->baseclasses.size ());
15340 TYPE_N_BASECLASSES (type) = fip->baseclasses.size ();
c906108c
SS
15341 }
15342
2ddeaf8a
TT
15343 if (TYPE_FLAG_DISCRIMINATED_UNION (type))
15344 {
15345 struct discriminant_info *di = alloc_discriminant_info (type, -1, -1);
15346
be2daae6 15347 for (int index = 0; index < nfields; ++index)
2ddeaf8a 15348 {
be2daae6
TT
15349 struct nextfield &field = fip->fields[index];
15350
15351 if (field.variant.is_discriminant)
2ddeaf8a 15352 di->discriminant_index = index;
be2daae6 15353 else if (field.variant.default_branch)
2ddeaf8a
TT
15354 di->default_index = index;
15355 else
be2daae6 15356 di->discriminants[index] = field.variant.discriminant_value;
2ddeaf8a
TT
15357 }
15358 }
15359
be2daae6
TT
15360 /* Copy the saved-up fields into the field vector. */
15361 for (int i = 0; i < nfields; ++i)
c906108c 15362 {
be2daae6
TT
15363 struct nextfield &field
15364 = ((i < fip->baseclasses.size ()) ? fip->baseclasses[i]
15365 : fip->fields[i - fip->baseclasses.size ()]);
7d0ccb61 15366
be2daae6
TT
15367 TYPE_FIELD (type, i) = field.field;
15368 switch (field.accessibility)
c906108c 15369 {
c5aa993b 15370 case DW_ACCESS_private:
b4ba55a1 15371 if (cu->language != language_ada)
be2daae6 15372 SET_TYPE_FIELD_PRIVATE (type, i);
c5aa993b 15373 break;
c906108c 15374
c5aa993b 15375 case DW_ACCESS_protected:
b4ba55a1 15376 if (cu->language != language_ada)
be2daae6 15377 SET_TYPE_FIELD_PROTECTED (type, i);
c5aa993b 15378 break;
c906108c 15379
c5aa993b
JM
15380 case DW_ACCESS_public:
15381 break;
c906108c 15382
c5aa993b
JM
15383 default:
15384 /* Unknown accessibility. Complain and treat it as public. */
15385 {
b98664d3 15386 complaint (_("unsupported accessibility %d"),
be2daae6 15387 field.accessibility);
c5aa993b
JM
15388 }
15389 break;
c906108c 15390 }
be2daae6 15391 if (i < fip->baseclasses.size ())
c906108c 15392 {
be2daae6 15393 switch (field.virtuality)
c906108c 15394 {
c5aa993b
JM
15395 case DW_VIRTUALITY_virtual:
15396 case DW_VIRTUALITY_pure_virtual:
b4ba55a1 15397 if (cu->language == language_ada)
a73c6dcd 15398 error (_("unexpected virtuality in component of Ada type"));
be2daae6 15399 SET_TYPE_FIELD_VIRTUAL (type, i);
c5aa993b 15400 break;
c906108c
SS
15401 }
15402 }
c906108c
SS
15403 }
15404}
15405
7d27a96d
TT
15406/* Return true if this member function is a constructor, false
15407 otherwise. */
15408
15409static int
15410dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
15411{
15412 const char *fieldname;
fe978cb0 15413 const char *type_name;
7d27a96d
TT
15414 int len;
15415
15416 if (die->parent == NULL)
15417 return 0;
15418
15419 if (die->parent->tag != DW_TAG_structure_type
15420 && die->parent->tag != DW_TAG_union_type
15421 && die->parent->tag != DW_TAG_class_type)
15422 return 0;
15423
15424 fieldname = dwarf2_name (die, cu);
fe978cb0
PA
15425 type_name = dwarf2_name (die->parent, cu);
15426 if (fieldname == NULL || type_name == NULL)
7d27a96d
TT
15427 return 0;
15428
15429 len = strlen (fieldname);
fe978cb0
PA
15430 return (strncmp (fieldname, type_name, len) == 0
15431 && (type_name[len] == '\0' || type_name[len] == '<'));
7d27a96d
TT
15432}
15433
c906108c
SS
15434/* Add a member function to the proper fieldlist. */
15435
15436static void
107d2387 15437dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
e7c27a73 15438 struct type *type, struct dwarf2_cu *cu)
c906108c 15439{
518817b3 15440 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c 15441 struct attribute *attr;
c906108c 15442 int i;
be2daae6 15443 struct fnfieldlist *flp = nullptr;
c906108c 15444 struct fn_field *fnp;
15d034d0 15445 const char *fieldname;
f792889a 15446 struct type *this_type;
60d5a603 15447 enum dwarf_access_attribute accessibility;
c906108c 15448
b4ba55a1 15449 if (cu->language == language_ada)
a73c6dcd 15450 error (_("unexpected member function in Ada type"));
b4ba55a1 15451
2df3850c 15452 /* Get name of member function. */
39cbfefa
DJ
15453 fieldname = dwarf2_name (die, cu);
15454 if (fieldname == NULL)
2df3850c 15455 return;
c906108c 15456
c906108c 15457 /* Look up member function name in fieldlist. */
be2daae6 15458 for (i = 0; i < fip->fnfieldlists.size (); i++)
c906108c 15459 {
27bfe10e 15460 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
be2daae6
TT
15461 {
15462 flp = &fip->fnfieldlists[i];
15463 break;
15464 }
c906108c
SS
15465 }
15466
be2daae6
TT
15467 /* Create a new fnfieldlist if necessary. */
15468 if (flp == nullptr)
c906108c 15469 {
be2daae6
TT
15470 fip->fnfieldlists.emplace_back ();
15471 flp = &fip->fnfieldlists.back ();
c906108c 15472 flp->name = fieldname;
be2daae6 15473 i = fip->fnfieldlists.size () - 1;
c906108c
SS
15474 }
15475
be2daae6
TT
15476 /* Create a new member function field and add it to the vector of
15477 fnfieldlists. */
15478 flp->fnfields.emplace_back ();
15479 fnp = &flp->fnfields.back ();
3da10d80
KS
15480
15481 /* Delay processing of the physname until later. */
9c37b5ae 15482 if (cu->language == language_cplus)
be2daae6
TT
15483 add_to_method_list (type, i, flp->fnfields.size () - 1, fieldname,
15484 die, cu);
3da10d80
KS
15485 else
15486 {
1d06ead6 15487 const char *physname = dwarf2_physname (fieldname, die, cu);
3da10d80
KS
15488 fnp->physname = physname ? physname : "";
15489 }
15490
c906108c 15491 fnp->type = alloc_type (objfile);
f792889a
DJ
15492 this_type = read_type_die (die, cu);
15493 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
c906108c 15494 {
f792889a 15495 int nparams = TYPE_NFIELDS (this_type);
c906108c 15496
f792889a 15497 /* TYPE is the domain of this method, and THIS_TYPE is the type
e26fb1d7
DC
15498 of the method itself (TYPE_CODE_METHOD). */
15499 smash_to_method_type (fnp->type, type,
f792889a
DJ
15500 TYPE_TARGET_TYPE (this_type),
15501 TYPE_FIELDS (this_type),
15502 TYPE_NFIELDS (this_type),
15503 TYPE_VARARGS (this_type));
c906108c
SS
15504
15505 /* Handle static member functions.
c5aa993b 15506 Dwarf2 has no clean way to discern C++ static and non-static
0963b4bd
MS
15507 member functions. G++ helps GDB by marking the first
15508 parameter for non-static member functions (which is the this
15509 pointer) as artificial. We obtain this information from
15510 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
f792889a 15511 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
c906108c
SS
15512 fnp->voffset = VOFFSET_STATIC;
15513 }
15514 else
b98664d3 15515 complaint (_("member function type missing for '%s'"),
3da10d80 15516 dwarf2_full_name (fieldname, die, cu));
c906108c
SS
15517
15518 /* Get fcontext from DW_AT_containing_type if present. */
e142c38c 15519 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
e7c27a73 15520 fnp->fcontext = die_containing_type (die, cu);
c906108c 15521
3e43a32a
MS
15522 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
15523 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
c906108c
SS
15524
15525 /* Get accessibility. */
e142c38c 15526 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
c906108c 15527 if (attr)
aead7601 15528 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
60d5a603
JK
15529 else
15530 accessibility = dwarf2_default_access_attribute (die, cu);
15531 switch (accessibility)
c906108c 15532 {
60d5a603
JK
15533 case DW_ACCESS_private:
15534 fnp->is_private = 1;
15535 break;
15536 case DW_ACCESS_protected:
15537 fnp->is_protected = 1;
15538 break;
c906108c
SS
15539 }
15540
b02dede2 15541 /* Check for artificial methods. */
e142c38c 15542 attr = dwarf2_attr (die, DW_AT_artificial, cu);
b02dede2
DJ
15543 if (attr && DW_UNSND (attr) != 0)
15544 fnp->is_artificial = 1;
15545
7d27a96d
TT
15546 fnp->is_constructor = dwarf2_is_constructor (die, cu);
15547
0d564a31 15548 /* Get index in virtual function table if it is a virtual member
aec5aa8b
TT
15549 function. For older versions of GCC, this is an offset in the
15550 appropriate virtual table, as specified by DW_AT_containing_type.
15551 For everyone else, it is an expression to be evaluated relative
0d564a31
DJ
15552 to the object address. */
15553
e142c38c 15554 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
aec5aa8b 15555 if (attr)
8e19ed76 15556 {
aec5aa8b 15557 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size > 0)
8e19ed76 15558 {
aec5aa8b
TT
15559 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
15560 {
15561 /* Old-style GCC. */
15562 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
15563 }
15564 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
15565 || (DW_BLOCK (attr)->size > 1
15566 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
15567 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
15568 {
aec5aa8b
TT
15569 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
15570 if ((fnp->voffset % cu->header.addr_size) != 0)
15571 dwarf2_complex_location_expr_complaint ();
15572 else
15573 fnp->voffset /= cu->header.addr_size;
15574 fnp->voffset += 2;
15575 }
15576 else
15577 dwarf2_complex_location_expr_complaint ();
15578
15579 if (!fnp->fcontext)
7e993ebf
KS
15580 {
15581 /* If there is no `this' field and no DW_AT_containing_type,
15582 we cannot actually find a base class context for the
15583 vtable! */
15584 if (TYPE_NFIELDS (this_type) == 0
15585 || !TYPE_FIELD_ARTIFICIAL (this_type, 0))
15586 {
b98664d3 15587 complaint (_("cannot determine context for virtual member "
9d8780f0
SM
15588 "function \"%s\" (offset %s)"),
15589 fieldname, sect_offset_str (die->sect_off));
7e993ebf
KS
15590 }
15591 else
15592 {
15593 fnp->fcontext
15594 = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
15595 }
15596 }
aec5aa8b 15597 }
3690dd37 15598 else if (attr_form_is_section_offset (attr))
8e19ed76 15599 {
4d3c2250 15600 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
15601 }
15602 else
15603 {
4d3c2250
KB
15604 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
15605 fieldname);
8e19ed76 15606 }
0d564a31 15607 }
d48cc9dd
DJ
15608 else
15609 {
15610 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
15611 if (attr && DW_UNSND (attr))
15612 {
15613 /* GCC does this, as of 2008-08-25; PR debug/37237. */
b98664d3 15614 complaint (_("Member function \"%s\" (offset %s) is virtual "
3e43a32a 15615 "but the vtable offset is not specified"),
9d8780f0 15616 fieldname, sect_offset_str (die->sect_off));
9655fd1a 15617 ALLOCATE_CPLUS_STRUCT_TYPE (type);
d48cc9dd
DJ
15618 TYPE_CPLUS_DYNAMIC (type) = 1;
15619 }
15620 }
c906108c
SS
15621}
15622
15623/* Create the vector of member function fields, and attach it to the type. */
15624
15625static void
fba45db2 15626dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 15627 struct dwarf2_cu *cu)
c906108c 15628{
b4ba55a1 15629 if (cu->language == language_ada)
a73c6dcd 15630 error (_("unexpected member functions in Ada type"));
b4ba55a1 15631
c906108c
SS
15632 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15633 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
be2daae6
TT
15634 TYPE_ALLOC (type,
15635 sizeof (struct fn_fieldlist) * fip->fnfieldlists.size ());
c906108c 15636
be2daae6 15637 for (int i = 0; i < fip->fnfieldlists.size (); i++)
c906108c 15638 {
be2daae6 15639 struct fnfieldlist &nf = fip->fnfieldlists[i];
c906108c 15640 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
c906108c 15641
be2daae6
TT
15642 TYPE_FN_FIELDLIST_NAME (type, i) = nf.name;
15643 TYPE_FN_FIELDLIST_LENGTH (type, i) = nf.fnfields.size ();
c906108c 15644 fn_flp->fn_fields = (struct fn_field *)
be2daae6
TT
15645 TYPE_ALLOC (type, sizeof (struct fn_field) * nf.fnfields.size ());
15646
15647 for (int k = 0; k < nf.fnfields.size (); ++k)
15648 fn_flp->fn_fields[k] = nf.fnfields[k];
c906108c
SS
15649 }
15650
be2daae6 15651 TYPE_NFN_FIELDS (type) = fip->fnfieldlists.size ();
c906108c
SS
15652}
15653
1168df01
JB
15654/* Returns non-zero if NAME is the name of a vtable member in CU's
15655 language, zero otherwise. */
15656static int
15657is_vtable_name (const char *name, struct dwarf2_cu *cu)
15658{
15659 static const char vptr[] = "_vptr";
15660
9c37b5ae
TT
15661 /* Look for the C++ form of the vtable. */
15662 if (startswith (name, vptr) && is_cplus_marker (name[sizeof (vptr) - 1]))
1168df01
JB
15663 return 1;
15664
15665 return 0;
15666}
15667
c0dd20ea 15668/* GCC outputs unnamed structures that are really pointers to member
0b92b5bb
TT
15669 functions, with the ABI-specified layout. If TYPE describes
15670 such a structure, smash it into a member function type.
61049d3b
DJ
15671
15672 GCC shouldn't do this; it should just output pointer to member DIEs.
15673 This is GCC PR debug/28767. */
c0dd20ea 15674
0b92b5bb
TT
15675static void
15676quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
c0dd20ea 15677{
09e2d7c7 15678 struct type *pfn_type, *self_type, *new_type;
c0dd20ea
DJ
15679
15680 /* Check for a structure with no name and two children. */
0b92b5bb
TT
15681 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
15682 return;
c0dd20ea
DJ
15683
15684 /* Check for __pfn and __delta members. */
0b92b5bb
TT
15685 if (TYPE_FIELD_NAME (type, 0) == NULL
15686 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
15687 || TYPE_FIELD_NAME (type, 1) == NULL
15688 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
15689 return;
c0dd20ea
DJ
15690
15691 /* Find the type of the method. */
0b92b5bb 15692 pfn_type = TYPE_FIELD_TYPE (type, 0);
c0dd20ea
DJ
15693 if (pfn_type == NULL
15694 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
15695 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
0b92b5bb 15696 return;
c0dd20ea
DJ
15697
15698 /* Look for the "this" argument. */
15699 pfn_type = TYPE_TARGET_TYPE (pfn_type);
15700 if (TYPE_NFIELDS (pfn_type) == 0
0b92b5bb 15701 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
c0dd20ea 15702 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
0b92b5bb 15703 return;
c0dd20ea 15704
09e2d7c7 15705 self_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
0b92b5bb 15706 new_type = alloc_type (objfile);
09e2d7c7 15707 smash_to_method_type (new_type, self_type, TYPE_TARGET_TYPE (pfn_type),
c0dd20ea
DJ
15708 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
15709 TYPE_VARARGS (pfn_type));
0b92b5bb 15710 smash_to_methodptr_type (type, new_type);
c0dd20ea 15711}
1168df01 15712
2b4424c3
TT
15713/* If the DIE has a DW_AT_alignment attribute, return its value, doing
15714 appropriate error checking and issuing complaints if there is a
15715 problem. */
15716
15717static ULONGEST
15718get_alignment (struct dwarf2_cu *cu, struct die_info *die)
15719{
15720 struct attribute *attr = dwarf2_attr (die, DW_AT_alignment, cu);
15721
15722 if (attr == nullptr)
15723 return 0;
15724
15725 if (!attr_form_is_constant (attr))
15726 {
b98664d3 15727 complaint (_("DW_AT_alignment must have constant form"
2b4424c3
TT
15728 " - DIE at %s [in module %s]"),
15729 sect_offset_str (die->sect_off),
15730 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15731 return 0;
15732 }
15733
15734 ULONGEST align;
15735 if (attr->form == DW_FORM_sdata)
15736 {
15737 LONGEST val = DW_SND (attr);
15738 if (val < 0)
15739 {
b98664d3 15740 complaint (_("DW_AT_alignment value must not be negative"
2b4424c3
TT
15741 " - DIE at %s [in module %s]"),
15742 sect_offset_str (die->sect_off),
15743 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15744 return 0;
15745 }
15746 align = val;
15747 }
15748 else
15749 align = DW_UNSND (attr);
15750
15751 if (align == 0)
15752 {
b98664d3 15753 complaint (_("DW_AT_alignment value must not be zero"
2b4424c3
TT
15754 " - DIE at %s [in module %s]"),
15755 sect_offset_str (die->sect_off),
15756 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15757 return 0;
15758 }
15759 if ((align & (align - 1)) != 0)
15760 {
b98664d3 15761 complaint (_("DW_AT_alignment value must be a power of 2"
2b4424c3
TT
15762 " - DIE at %s [in module %s]"),
15763 sect_offset_str (die->sect_off),
15764 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15765 return 0;
15766 }
15767
15768 return align;
15769}
15770
15771/* If the DIE has a DW_AT_alignment attribute, use its value to set
15772 the alignment for TYPE. */
15773
15774static void
15775maybe_set_alignment (struct dwarf2_cu *cu, struct die_info *die,
15776 struct type *type)
15777{
15778 if (!set_type_align (type, get_alignment (cu, die)))
b98664d3 15779 complaint (_("DW_AT_alignment value too large"
2b4424c3
TT
15780 " - DIE at %s [in module %s]"),
15781 sect_offset_str (die->sect_off),
15782 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15783}
685b1105 15784
c906108c 15785/* Called when we find the DIE that starts a structure or union scope
c767944b
DJ
15786 (definition) to create a type for the structure or union. Fill in
15787 the type's name and general properties; the members will not be
83655187
DE
15788 processed until process_structure_scope. A symbol table entry for
15789 the type will also not be done until process_structure_scope (assuming
15790 the type has a name).
c906108c 15791
c767944b
DJ
15792 NOTE: we need to call these functions regardless of whether or not the
15793 DIE has a DW_AT_name attribute, since it might be an anonymous
c906108c 15794 structure or union. This gets the type entered into our set of
83655187 15795 user defined types. */
c906108c 15796
f792889a 15797static struct type *
134d01f1 15798read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 15799{
518817b3 15800 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c
SS
15801 struct type *type;
15802 struct attribute *attr;
15d034d0 15803 const char *name;
c906108c 15804
348e048f
DE
15805 /* If the definition of this type lives in .debug_types, read that type.
15806 Don't follow DW_AT_specification though, that will take us back up
15807 the chain and we want to go down. */
45e58e77 15808 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
348e048f
DE
15809 if (attr)
15810 {
ac9ec31b 15811 type = get_DW_AT_signature_type (die, attr, cu);
9dc481d3 15812
ac9ec31b 15813 /* The type's CU may not be the same as CU.
02142a6c 15814 Ensure TYPE is recorded with CU in die_type_hash. */
348e048f
DE
15815 return set_die_type (die, type, cu);
15816 }
15817
c0dd20ea 15818 type = alloc_type (objfile);
c906108c 15819 INIT_CPLUS_SPECIFIC (type);
93311388 15820
39cbfefa
DJ
15821 name = dwarf2_name (die, cu);
15822 if (name != NULL)
c906108c 15823 {
987504bb 15824 if (cu->language == language_cplus
c44af4eb
TT
15825 || cu->language == language_d
15826 || cu->language == language_rust)
63d06c5c 15827 {
15d034d0 15828 const char *full_name = dwarf2_full_name (name, die, cu);
3da10d80
KS
15829
15830 /* dwarf2_full_name might have already finished building the DIE's
15831 type. If so, there is no need to continue. */
15832 if (get_die_type (die, cu) != NULL)
15833 return get_die_type (die, cu);
15834
e86ca25f 15835 TYPE_NAME (type) = full_name;
63d06c5c
DC
15836 }
15837 else
15838 {
d8151005
DJ
15839 /* The name is already allocated along with this objfile, so
15840 we don't need to duplicate it for the type. */
e86ca25f 15841 TYPE_NAME (type) = name;
63d06c5c 15842 }
c906108c
SS
15843 }
15844
15845 if (die->tag == DW_TAG_structure_type)
15846 {
15847 TYPE_CODE (type) = TYPE_CODE_STRUCT;
15848 }
15849 else if (die->tag == DW_TAG_union_type)
15850 {
15851 TYPE_CODE (type) = TYPE_CODE_UNION;
15852 }
2ddeaf8a
TT
15853 else if (die->tag == DW_TAG_variant_part)
15854 {
15855 TYPE_CODE (type) = TYPE_CODE_UNION;
15856 TYPE_FLAG_DISCRIMINATED_UNION (type) = 1;
15857 }
c906108c
SS
15858 else
15859 {
4753d33b 15860 TYPE_CODE (type) = TYPE_CODE_STRUCT;
c906108c
SS
15861 }
15862
0cc2414c
TT
15863 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
15864 TYPE_DECLARED_CLASS (type) = 1;
15865
e142c38c 15866 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
15867 if (attr)
15868 {
155bfbd3
JB
15869 if (attr_form_is_constant (attr))
15870 TYPE_LENGTH (type) = DW_UNSND (attr);
15871 else
15872 {
15873 /* For the moment, dynamic type sizes are not supported
15874 by GDB's struct type. The actual size is determined
15875 on-demand when resolving the type of a given object,
15876 so set the type's length to zero for now. Otherwise,
15877 we record an expression as the length, and that expression
15878 could lead to a very large value, which could eventually
15879 lead to us trying to allocate that much memory when creating
15880 a value of that type. */
15881 TYPE_LENGTH (type) = 0;
15882 }
c906108c
SS
15883 }
15884 else
15885 {
15886 TYPE_LENGTH (type) = 0;
15887 }
15888
2b4424c3
TT
15889 maybe_set_alignment (cu, die, type);
15890
5230b05a 15891 if (producer_is_icc_lt_14 (cu) && (TYPE_LENGTH (type) == 0))
685b1105 15892 {
5230b05a
WT
15893 /* ICC<14 does not output the required DW_AT_declaration on
15894 incomplete types, but gives them a size of zero. */
422b1cb0 15895 TYPE_STUB (type) = 1;
685b1105
JK
15896 }
15897 else
15898 TYPE_STUB_SUPPORTED (type) = 1;
15899
dc718098 15900 if (die_is_declaration (die, cu))
876cecd0 15901 TYPE_STUB (type) = 1;
a6c727b2
DJ
15902 else if (attr == NULL && die->child == NULL
15903 && producer_is_realview (cu->producer))
15904 /* RealView does not output the required DW_AT_declaration
15905 on incomplete types. */
15906 TYPE_STUB (type) = 1;
dc718098 15907
c906108c
SS
15908 /* We need to add the type field to the die immediately so we don't
15909 infinitely recurse when dealing with pointers to the structure
0963b4bd 15910 type within the structure itself. */
1c379e20 15911 set_die_type (die, type, cu);
c906108c 15912
7e314c57
JK
15913 /* set_die_type should be already done. */
15914 set_descriptive_type (type, die, cu);
15915
c767944b
DJ
15916 return type;
15917}
15918
2ddeaf8a
TT
15919/* A helper for process_structure_scope that handles a single member
15920 DIE. */
15921
15922static void
15923handle_struct_member_die (struct die_info *child_die, struct type *type,
15924 struct field_info *fi,
15925 std::vector<struct symbol *> *template_args,
15926 struct dwarf2_cu *cu)
15927{
15928 if (child_die->tag == DW_TAG_member
15929 || child_die->tag == DW_TAG_variable
15930 || child_die->tag == DW_TAG_variant_part)
15931 {
15932 /* NOTE: carlton/2002-11-05: A C++ static data member
15933 should be a DW_TAG_member that is a declaration, but
15934 all versions of G++ as of this writing (so through at
15935 least 3.2.1) incorrectly generate DW_TAG_variable
15936 tags for them instead. */
15937 dwarf2_add_field (fi, child_die, cu);
15938 }
15939 else if (child_die->tag == DW_TAG_subprogram)
15940 {
15941 /* Rust doesn't have member functions in the C++ sense.
15942 However, it does emit ordinary functions as children
15943 of a struct DIE. */
15944 if (cu->language == language_rust)
15945 read_func_scope (child_die, cu);
15946 else
15947 {
15948 /* C++ member function. */
15949 dwarf2_add_member_fn (fi, child_die, type, cu);
15950 }
15951 }
15952 else if (child_die->tag == DW_TAG_inheritance)
15953 {
15954 /* C++ base class field. */
15955 dwarf2_add_field (fi, child_die, cu);
15956 }
15957 else if (type_can_define_types (child_die))
15958 dwarf2_add_type_defn (fi, child_die, cu);
15959 else if (child_die->tag == DW_TAG_template_type_param
15960 || child_die->tag == DW_TAG_template_value_param)
15961 {
15962 struct symbol *arg = new_symbol (child_die, NULL, cu);
15963
15964 if (arg != NULL)
15965 template_args->push_back (arg);
15966 }
15967 else if (child_die->tag == DW_TAG_variant)
15968 {
15969 /* In a variant we want to get the discriminant and also add a
15970 field for our sole member child. */
15971 struct attribute *discr = dwarf2_attr (child_die, DW_AT_discr_value, cu);
15972
15973 for (struct die_info *variant_child = child_die->child;
15974 variant_child != NULL;
15975 variant_child = sibling_die (variant_child))
15976 {
15977 if (variant_child->tag == DW_TAG_member)
15978 {
15979 handle_struct_member_die (variant_child, type, fi,
15980 template_args, cu);
15981 /* Only handle the one. */
15982 break;
15983 }
15984 }
15985
15986 /* We don't handle this but we might as well report it if we see
15987 it. */
15988 if (dwarf2_attr (child_die, DW_AT_discr_list, cu) != nullptr)
b98664d3 15989 complaint (_("DW_AT_discr_list is not supported yet"
2ddeaf8a
TT
15990 " - DIE at %s [in module %s]"),
15991 sect_offset_str (child_die->sect_off),
15992 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15993
15994 /* The first field was just added, so we can stash the
15995 discriminant there. */
be2daae6 15996 gdb_assert (!fi->fields.empty ());
2ddeaf8a 15997 if (discr == NULL)
be2daae6 15998 fi->fields.back ().variant.default_branch = true;
2ddeaf8a 15999 else
be2daae6 16000 fi->fields.back ().variant.discriminant_value = DW_UNSND (discr);
2ddeaf8a
TT
16001 }
16002}
16003
c767944b
DJ
16004/* Finish creating a structure or union type, including filling in
16005 its members and creating a symbol for it. */
16006
16007static void
16008process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
16009{
518817b3 16010 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
ca040673 16011 struct die_info *child_die;
c767944b
DJ
16012 struct type *type;
16013
16014 type = get_die_type (die, cu);
16015 if (type == NULL)
16016 type = read_structure_type (die, cu);
16017
2ddeaf8a
TT
16018 /* When reading a DW_TAG_variant_part, we need to notice when we
16019 read the discriminant member, so we can record it later in the
16020 discriminant_info. */
16021 bool is_variant_part = TYPE_FLAG_DISCRIMINATED_UNION (type);
16022 sect_offset discr_offset;
3e1d3d8c 16023 bool has_template_parameters = false;
2ddeaf8a
TT
16024
16025 if (is_variant_part)
16026 {
16027 struct attribute *discr = dwarf2_attr (die, DW_AT_discr, cu);
16028 if (discr == NULL)
16029 {
16030 /* Maybe it's a univariant form, an extension we support.
16031 In this case arrange not to check the offset. */
16032 is_variant_part = false;
16033 }
16034 else if (attr_form_is_ref (discr))
16035 {
16036 struct dwarf2_cu *target_cu = cu;
16037 struct die_info *target_die = follow_die_ref (die, discr, &target_cu);
16038
16039 discr_offset = target_die->sect_off;
16040 }
16041 else
16042 {
b98664d3 16043 complaint (_("DW_AT_discr does not have DIE reference form"
2ddeaf8a
TT
16044 " - DIE at %s [in module %s]"),
16045 sect_offset_str (die->sect_off),
16046 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
16047 is_variant_part = false;
16048 }
16049 }
16050
e142c38c 16051 if (die->child != NULL && ! die_is_declaration (die, cu))
c906108c
SS
16052 {
16053 struct field_info fi;
2f4732b0 16054 std::vector<struct symbol *> template_args;
c906108c 16055
639d11d3 16056 child_die = die->child;
c906108c
SS
16057
16058 while (child_die && child_die->tag)
16059 {
2ddeaf8a 16060 handle_struct_member_die (child_die, type, &fi, &template_args, cu);
34eaf542 16061
2ddeaf8a 16062 if (is_variant_part && discr_offset == child_die->sect_off)
be2daae6 16063 fi.fields.back ().variant.is_discriminant = true;
34eaf542 16064
c906108c
SS
16065 child_die = sibling_die (child_die);
16066 }
16067
34eaf542 16068 /* Attach template arguments to type. */
2f4732b0 16069 if (!template_args.empty ())
34eaf542 16070 {
3e1d3d8c 16071 has_template_parameters = true;
34eaf542 16072 ALLOCATE_CPLUS_STRUCT_TYPE (type);
2f4732b0 16073 TYPE_N_TEMPLATE_ARGUMENTS (type) = template_args.size ();
34eaf542 16074 TYPE_TEMPLATE_ARGUMENTS (type)
8d749320
SM
16075 = XOBNEWVEC (&objfile->objfile_obstack,
16076 struct symbol *,
16077 TYPE_N_TEMPLATE_ARGUMENTS (type));
34eaf542 16078 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
2f4732b0 16079 template_args.data (),
34eaf542
TT
16080 (TYPE_N_TEMPLATE_ARGUMENTS (type)
16081 * sizeof (struct symbol *)));
34eaf542
TT
16082 }
16083
c906108c
SS
16084 /* Attach fields and member functions to the type. */
16085 if (fi.nfields)
e7c27a73 16086 dwarf2_attach_fields_to_type (&fi, type, cu);
be2daae6 16087 if (!fi.fnfieldlists.empty ())
c906108c 16088 {
e7c27a73 16089 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
c906108c 16090
c5aa993b 16091 /* Get the type which refers to the base class (possibly this
c906108c 16092 class itself) which contains the vtable pointer for the current
0d564a31
DJ
16093 class from the DW_AT_containing_type attribute. This use of
16094 DW_AT_containing_type is a GNU extension. */
c906108c 16095
e142c38c 16096 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
c906108c 16097 {
e7c27a73 16098 struct type *t = die_containing_type (die, cu);
c906108c 16099
ae6ae975 16100 set_type_vptr_basetype (type, t);
c906108c
SS
16101 if (type == t)
16102 {
c906108c
SS
16103 int i;
16104
16105 /* Our own class provides vtbl ptr. */
16106 for (i = TYPE_NFIELDS (t) - 1;
16107 i >= TYPE_N_BASECLASSES (t);
16108 --i)
16109 {
0d5cff50 16110 const char *fieldname = TYPE_FIELD_NAME (t, i);
c906108c 16111
1168df01 16112 if (is_vtable_name (fieldname, cu))
c906108c 16113 {
ae6ae975 16114 set_type_vptr_fieldno (type, i);
c906108c
SS
16115 break;
16116 }
16117 }
16118
16119 /* Complain if virtual function table field not found. */
16120 if (i < TYPE_N_BASECLASSES (t))
b98664d3 16121 complaint (_("virtual function table pointer "
3e43a32a 16122 "not found when defining class '%s'"),
e86ca25f 16123 TYPE_NAME (type) ? TYPE_NAME (type) : "");
c906108c
SS
16124 }
16125 else
16126 {
ae6ae975 16127 set_type_vptr_fieldno (type, TYPE_VPTR_FIELDNO (t));
c906108c
SS
16128 }
16129 }
f6235d4c 16130 else if (cu->producer
61012eef 16131 && startswith (cu->producer, "IBM(R) XL C/C++ Advanced Edition"))
f6235d4c
EZ
16132 {
16133 /* The IBM XLC compiler does not provide direct indication
16134 of the containing type, but the vtable pointer is
16135 always named __vfp. */
16136
16137 int i;
16138
16139 for (i = TYPE_NFIELDS (type) - 1;
16140 i >= TYPE_N_BASECLASSES (type);
16141 --i)
16142 {
16143 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
16144 {
ae6ae975
DE
16145 set_type_vptr_fieldno (type, i);
16146 set_type_vptr_basetype (type, type);
f6235d4c
EZ
16147 break;
16148 }
16149 }
16150 }
c906108c 16151 }
98751a41
JK
16152
16153 /* Copy fi.typedef_field_list linked list elements content into the
16154 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
be2daae6 16155 if (!fi.typedef_field_list.empty ())
98751a41 16156 {
be2daae6 16157 int count = fi.typedef_field_list.size ();
98751a41 16158
a0d7a4ff 16159 ALLOCATE_CPLUS_STRUCT_TYPE (type);
98751a41 16160 TYPE_TYPEDEF_FIELD_ARRAY (type)
883fd55a 16161 = ((struct decl_field *)
be2daae6
TT
16162 TYPE_ALLOC (type,
16163 sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * count));
16164 TYPE_TYPEDEF_FIELD_COUNT (type) = count;
6e70227d 16165
be2daae6
TT
16166 for (int i = 0; i < fi.typedef_field_list.size (); ++i)
16167 TYPE_TYPEDEF_FIELD (type, i) = fi.typedef_field_list[i];
98751a41 16168 }
c767944b 16169
883fd55a
KS
16170 /* Copy fi.nested_types_list linked list elements content into the
16171 allocated array TYPE_NESTED_TYPES_ARRAY (type). */
be2daae6 16172 if (!fi.nested_types_list.empty () && cu->language != language_ada)
883fd55a 16173 {
be2daae6 16174 int count = fi.nested_types_list.size ();
883fd55a
KS
16175
16176 ALLOCATE_CPLUS_STRUCT_TYPE (type);
16177 TYPE_NESTED_TYPES_ARRAY (type)
16178 = ((struct decl_field *)
be2daae6
TT
16179 TYPE_ALLOC (type, sizeof (struct decl_field) * count));
16180 TYPE_NESTED_TYPES_COUNT (type) = count;
883fd55a 16181
be2daae6
TT
16182 for (int i = 0; i < fi.nested_types_list.size (); ++i)
16183 TYPE_NESTED_TYPES_FIELD (type, i) = fi.nested_types_list[i];
883fd55a 16184 }
c906108c 16185 }
63d06c5c 16186
bb5ed363 16187 quirk_gcc_member_function_pointer (type, objfile);
c9317f21
TT
16188 if (cu->language == language_rust && die->tag == DW_TAG_union_type)
16189 cu->rust_unions.push_back (type);
0b92b5bb 16190
90aeadfc
DC
16191 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
16192 snapshots) has been known to create a die giving a declaration
16193 for a class that has, as a child, a die giving a definition for a
16194 nested class. So we have to process our children even if the
16195 current die is a declaration. Normally, of course, a declaration
16196 won't have any children at all. */
134d01f1 16197
ca040673
DE
16198 child_die = die->child;
16199
90aeadfc
DC
16200 while (child_die != NULL && child_die->tag)
16201 {
16202 if (child_die->tag == DW_TAG_member
16203 || child_die->tag == DW_TAG_variable
34eaf542
TT
16204 || child_die->tag == DW_TAG_inheritance
16205 || child_die->tag == DW_TAG_template_value_param
16206 || child_die->tag == DW_TAG_template_type_param)
134d01f1 16207 {
90aeadfc 16208 /* Do nothing. */
134d01f1 16209 }
90aeadfc
DC
16210 else
16211 process_die (child_die, cu);
134d01f1 16212
90aeadfc 16213 child_die = sibling_die (child_die);
134d01f1
DJ
16214 }
16215
fa4028e9
JB
16216 /* Do not consider external references. According to the DWARF standard,
16217 these DIEs are identified by the fact that they have no byte_size
16218 attribute, and a declaration attribute. */
16219 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
16220 || !die_is_declaration (die, cu))
3e1d3d8c
TT
16221 {
16222 struct symbol *sym = new_symbol (die, type, cu);
16223
16224 if (has_template_parameters)
16225 {
16226 /* Make sure that the symtab is set on the new symbols.
16227 Even though they don't appear in this symtab directly,
16228 other parts of gdb assume that symbols do, and this is
16229 reasonably true. */
16230 for (int i = 0; i < TYPE_N_TEMPLATE_ARGUMENTS (type); ++i)
16231 symbol_set_symtab (TYPE_TEMPLATE_ARGUMENT (type, i),
16232 symbol_symtab (sym));
16233 }
16234 }
134d01f1
DJ
16235}
16236
55426c9d
JB
16237/* Assuming DIE is an enumeration type, and TYPE is its associated type,
16238 update TYPE using some information only available in DIE's children. */
16239
16240static void
16241update_enumeration_type_from_children (struct die_info *die,
16242 struct type *type,
16243 struct dwarf2_cu *cu)
16244{
60f7655a 16245 struct die_info *child_die;
55426c9d
JB
16246 int unsigned_enum = 1;
16247 int flag_enum = 1;
16248 ULONGEST mask = 0;
55426c9d 16249
8268c778 16250 auto_obstack obstack;
55426c9d 16251
60f7655a
DE
16252 for (child_die = die->child;
16253 child_die != NULL && child_die->tag;
16254 child_die = sibling_die (child_die))
55426c9d
JB
16255 {
16256 struct attribute *attr;
16257 LONGEST value;
16258 const gdb_byte *bytes;
16259 struct dwarf2_locexpr_baton *baton;
16260 const char *name;
60f7655a 16261
55426c9d
JB
16262 if (child_die->tag != DW_TAG_enumerator)
16263 continue;
16264
16265 attr = dwarf2_attr (child_die, DW_AT_const_value, cu);
16266 if (attr == NULL)
16267 continue;
16268
16269 name = dwarf2_name (child_die, cu);
16270 if (name == NULL)
16271 name = "<anonymous enumerator>";
16272
16273 dwarf2_const_value_attr (attr, type, name, &obstack, cu,
16274 &value, &bytes, &baton);
16275 if (value < 0)
16276 {
16277 unsigned_enum = 0;
16278 flag_enum = 0;
16279 }
16280 else if ((mask & value) != 0)
16281 flag_enum = 0;
16282 else
16283 mask |= value;
16284
16285 /* If we already know that the enum type is neither unsigned, nor
16286 a flag type, no need to look at the rest of the enumerates. */
16287 if (!unsigned_enum && !flag_enum)
16288 break;
55426c9d
JB
16289 }
16290
16291 if (unsigned_enum)
16292 TYPE_UNSIGNED (type) = 1;
16293 if (flag_enum)
16294 TYPE_FLAG_ENUM (type) = 1;
55426c9d
JB
16295}
16296
134d01f1
DJ
16297/* Given a DW_AT_enumeration_type die, set its type. We do not
16298 complete the type's fields yet, or create any symbols. */
c906108c 16299
f792889a 16300static struct type *
134d01f1 16301read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16302{
518817b3 16303 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c 16304 struct type *type;
c906108c 16305 struct attribute *attr;
0114d602 16306 const char *name;
134d01f1 16307
348e048f
DE
16308 /* If the definition of this type lives in .debug_types, read that type.
16309 Don't follow DW_AT_specification though, that will take us back up
16310 the chain and we want to go down. */
45e58e77 16311 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
348e048f
DE
16312 if (attr)
16313 {
ac9ec31b 16314 type = get_DW_AT_signature_type (die, attr, cu);
9dc481d3 16315
ac9ec31b 16316 /* The type's CU may not be the same as CU.
02142a6c 16317 Ensure TYPE is recorded with CU in die_type_hash. */
348e048f
DE
16318 return set_die_type (die, type, cu);
16319 }
16320
c906108c
SS
16321 type = alloc_type (objfile);
16322
16323 TYPE_CODE (type) = TYPE_CODE_ENUM;
94af9270 16324 name = dwarf2_full_name (NULL, die, cu);
39cbfefa 16325 if (name != NULL)
e86ca25f 16326 TYPE_NAME (type) = name;
c906108c 16327
0626fc76
TT
16328 attr = dwarf2_attr (die, DW_AT_type, cu);
16329 if (attr != NULL)
16330 {
16331 struct type *underlying_type = die_type (die, cu);
16332
16333 TYPE_TARGET_TYPE (type) = underlying_type;
16334 }
16335
e142c38c 16336 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
16337 if (attr)
16338 {
16339 TYPE_LENGTH (type) = DW_UNSND (attr);
16340 }
16341 else
16342 {
16343 TYPE_LENGTH (type) = 0;
16344 }
16345
2b4424c3
TT
16346 maybe_set_alignment (cu, die, type);
16347
137033e9
JB
16348 /* The enumeration DIE can be incomplete. In Ada, any type can be
16349 declared as private in the package spec, and then defined only
16350 inside the package body. Such types are known as Taft Amendment
16351 Types. When another package uses such a type, an incomplete DIE
16352 may be generated by the compiler. */
02eb380e 16353 if (die_is_declaration (die, cu))
876cecd0 16354 TYPE_STUB (type) = 1;
02eb380e 16355
0626fc76
TT
16356 /* Finish the creation of this type by using the enum's children.
16357 We must call this even when the underlying type has been provided
16358 so that we can determine if we're looking at a "flag" enum. */
55426c9d
JB
16359 update_enumeration_type_from_children (die, type, cu);
16360
0626fc76
TT
16361 /* If this type has an underlying type that is not a stub, then we
16362 may use its attributes. We always use the "unsigned" attribute
16363 in this situation, because ordinarily we guess whether the type
16364 is unsigned -- but the guess can be wrong and the underlying type
16365 can tell us the reality. However, we defer to a local size
16366 attribute if one exists, because this lets the compiler override
16367 the underlying type if needed. */
16368 if (TYPE_TARGET_TYPE (type) != NULL && !TYPE_STUB (TYPE_TARGET_TYPE (type)))
16369 {
16370 TYPE_UNSIGNED (type) = TYPE_UNSIGNED (TYPE_TARGET_TYPE (type));
16371 if (TYPE_LENGTH (type) == 0)
16372 TYPE_LENGTH (type) = TYPE_LENGTH (TYPE_TARGET_TYPE (type));
2b4424c3
TT
16373 if (TYPE_RAW_ALIGN (type) == 0
16374 && TYPE_RAW_ALIGN (TYPE_TARGET_TYPE (type)) != 0)
16375 set_type_align (type, TYPE_RAW_ALIGN (TYPE_TARGET_TYPE (type)));
0626fc76
TT
16376 }
16377
3d567982
TT
16378 TYPE_DECLARED_CLASS (type) = dwarf2_flag_true_p (die, DW_AT_enum_class, cu);
16379
f792889a 16380 return set_die_type (die, type, cu);
134d01f1
DJ
16381}
16382
16383/* Given a pointer to a die which begins an enumeration, process all
16384 the dies that define the members of the enumeration, and create the
16385 symbol for the enumeration type.
16386
16387 NOTE: We reverse the order of the element list. */
16388
16389static void
16390process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
16391{
f792889a 16392 struct type *this_type;
134d01f1 16393
f792889a
DJ
16394 this_type = get_die_type (die, cu);
16395 if (this_type == NULL)
16396 this_type = read_enumeration_type (die, cu);
9dc481d3 16397
639d11d3 16398 if (die->child != NULL)
c906108c 16399 {
9dc481d3
DE
16400 struct die_info *child_die;
16401 struct symbol *sym;
16402 struct field *fields = NULL;
16403 int num_fields = 0;
15d034d0 16404 const char *name;
9dc481d3 16405
639d11d3 16406 child_die = die->child;
c906108c
SS
16407 while (child_die && child_die->tag)
16408 {
16409 if (child_die->tag != DW_TAG_enumerator)
16410 {
e7c27a73 16411 process_die (child_die, cu);
c906108c
SS
16412 }
16413 else
16414 {
39cbfefa
DJ
16415 name = dwarf2_name (child_die, cu);
16416 if (name)
c906108c 16417 {
f792889a 16418 sym = new_symbol (child_die, this_type, cu);
c906108c
SS
16419
16420 if ((num_fields % DW_FIELD_ALLOC_CHUNK) == 0)
16421 {
16422 fields = (struct field *)
16423 xrealloc (fields,
16424 (num_fields + DW_FIELD_ALLOC_CHUNK)
c5aa993b 16425 * sizeof (struct field));
c906108c
SS
16426 }
16427
3567439c 16428 FIELD_NAME (fields[num_fields]) = SYMBOL_LINKAGE_NAME (sym);
c906108c 16429 FIELD_TYPE (fields[num_fields]) = NULL;
14e75d8e 16430 SET_FIELD_ENUMVAL (fields[num_fields], SYMBOL_VALUE (sym));
c906108c
SS
16431 FIELD_BITSIZE (fields[num_fields]) = 0;
16432
16433 num_fields++;
16434 }
16435 }
16436
16437 child_die = sibling_die (child_die);
16438 }
16439
16440 if (num_fields)
16441 {
f792889a
DJ
16442 TYPE_NFIELDS (this_type) = num_fields;
16443 TYPE_FIELDS (this_type) = (struct field *)
16444 TYPE_ALLOC (this_type, sizeof (struct field) * num_fields);
16445 memcpy (TYPE_FIELDS (this_type), fields,
c906108c 16446 sizeof (struct field) * num_fields);
b8c9b27d 16447 xfree (fields);
c906108c 16448 }
c906108c 16449 }
134d01f1 16450
6c83ed52
TT
16451 /* If we are reading an enum from a .debug_types unit, and the enum
16452 is a declaration, and the enum is not the signatured type in the
16453 unit, then we do not want to add a symbol for it. Adding a
16454 symbol would in some cases obscure the true definition of the
16455 enum, giving users an incomplete type when the definition is
16456 actually available. Note that we do not want to do this for all
16457 enums which are just declarations, because C++0x allows forward
16458 enum declarations. */
3019eac3 16459 if (cu->per_cu->is_debug_types
6c83ed52
TT
16460 && die_is_declaration (die, cu))
16461 {
52dc124a 16462 struct signatured_type *sig_type;
6c83ed52 16463
c0f78cd4 16464 sig_type = (struct signatured_type *) cu->per_cu;
9c541725
PA
16465 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
16466 if (sig_type->type_offset_in_section != die->sect_off)
6c83ed52
TT
16467 return;
16468 }
16469
f792889a 16470 new_symbol (die, this_type, cu);
c906108c
SS
16471}
16472
16473/* Extract all information from a DW_TAG_array_type DIE and put it in
16474 the DIE's type field. For now, this only handles one dimensional
16475 arrays. */
16476
f792889a 16477static struct type *
e7c27a73 16478read_array_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16479{
518817b3 16480 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c 16481 struct die_info *child_die;
7e314c57 16482 struct type *type;
c906108c 16483 struct type *element_type, *range_type, *index_type;
c906108c 16484 struct attribute *attr;
15d034d0 16485 const char *name;
a405673c 16486 struct dynamic_prop *byte_stride_prop = NULL;
dc53a7ad 16487 unsigned int bit_stride = 0;
c906108c 16488
e7c27a73 16489 element_type = die_type (die, cu);
c906108c 16490
7e314c57
JK
16491 /* The die_type call above may have already set the type for this DIE. */
16492 type = get_die_type (die, cu);
16493 if (type)
16494 return type;
16495
dc53a7ad
JB
16496 attr = dwarf2_attr (die, DW_AT_byte_stride, cu);
16497 if (attr != NULL)
a405673c
JB
16498 {
16499 int stride_ok;
16500
16501 byte_stride_prop
16502 = (struct dynamic_prop *) alloca (sizeof (struct dynamic_prop));
16503 stride_ok = attr_to_dynamic_prop (attr, die, cu, byte_stride_prop);
16504 if (!stride_ok)
16505 {
b98664d3 16506 complaint (_("unable to read array DW_AT_byte_stride "
9d8780f0
SM
16507 " - DIE at %s [in module %s]"),
16508 sect_offset_str (die->sect_off),
518817b3 16509 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
a405673c
JB
16510 /* Ignore this attribute. We will likely not be able to print
16511 arrays of this type correctly, but there is little we can do
16512 to help if we cannot read the attribute's value. */
16513 byte_stride_prop = NULL;
16514 }
16515 }
dc53a7ad
JB
16516
16517 attr = dwarf2_attr (die, DW_AT_bit_stride, cu);
16518 if (attr != NULL)
16519 bit_stride = DW_UNSND (attr);
16520
c906108c
SS
16521 /* Irix 6.2 native cc creates array types without children for
16522 arrays with unspecified length. */
639d11d3 16523 if (die->child == NULL)
c906108c 16524 {
46bf5051 16525 index_type = objfile_type (objfile)->builtin_int;
0c9c3474 16526 range_type = create_static_range_type (NULL, index_type, 0, -1);
dc53a7ad 16527 type = create_array_type_with_stride (NULL, element_type, range_type,
a405673c 16528 byte_stride_prop, bit_stride);
f792889a 16529 return set_die_type (die, type, cu);
c906108c
SS
16530 }
16531
791afaa2 16532 std::vector<struct type *> range_types;
639d11d3 16533 child_die = die->child;
c906108c
SS
16534 while (child_die && child_die->tag)
16535 {
16536 if (child_die->tag == DW_TAG_subrange_type)
16537 {
f792889a 16538 struct type *child_type = read_type_die (child_die, cu);
9a619af0 16539
f792889a 16540 if (child_type != NULL)
a02abb62 16541 {
0963b4bd
MS
16542 /* The range type was succesfully read. Save it for the
16543 array type creation. */
791afaa2 16544 range_types.push_back (child_type);
a02abb62 16545 }
c906108c
SS
16546 }
16547 child_die = sibling_die (child_die);
16548 }
16549
16550 /* Dwarf2 dimensions are output from left to right, create the
16551 necessary array types in backwards order. */
7ca2d3a3 16552
c906108c 16553 type = element_type;
7ca2d3a3
DL
16554
16555 if (read_array_order (die, cu) == DW_ORD_col_major)
16556 {
16557 int i = 0;
9a619af0 16558
791afaa2 16559 while (i < range_types.size ())
dc53a7ad 16560 type = create_array_type_with_stride (NULL, type, range_types[i++],
a405673c 16561 byte_stride_prop, bit_stride);
7ca2d3a3
DL
16562 }
16563 else
16564 {
791afaa2 16565 size_t ndim = range_types.size ();
7ca2d3a3 16566 while (ndim-- > 0)
dc53a7ad 16567 type = create_array_type_with_stride (NULL, type, range_types[ndim],
a405673c 16568 byte_stride_prop, bit_stride);
7ca2d3a3 16569 }
c906108c 16570
f5f8a009
EZ
16571 /* Understand Dwarf2 support for vector types (like they occur on
16572 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
16573 array type. This is not part of the Dwarf2/3 standard yet, but a
16574 custom vendor extension. The main difference between a regular
16575 array and the vector variant is that vectors are passed by value
16576 to functions. */
e142c38c 16577 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
f5f8a009 16578 if (attr)
ea37ba09 16579 make_vector_type (type);
f5f8a009 16580
dbc98a8b
KW
16581 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
16582 implementation may choose to implement triple vectors using this
16583 attribute. */
16584 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16585 if (attr)
16586 {
16587 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
16588 TYPE_LENGTH (type) = DW_UNSND (attr);
16589 else
b98664d3 16590 complaint (_("DW_AT_byte_size for array type smaller "
3e43a32a 16591 "than the total size of elements"));
dbc98a8b
KW
16592 }
16593
39cbfefa
DJ
16594 name = dwarf2_name (die, cu);
16595 if (name)
16596 TYPE_NAME (type) = name;
6e70227d 16597
2b4424c3
TT
16598 maybe_set_alignment (cu, die, type);
16599
0963b4bd 16600 /* Install the type in the die. */
7e314c57
JK
16601 set_die_type (die, type, cu);
16602
16603 /* set_die_type should be already done. */
b4ba55a1
JB
16604 set_descriptive_type (type, die, cu);
16605
7e314c57 16606 return type;
c906108c
SS
16607}
16608
7ca2d3a3 16609static enum dwarf_array_dim_ordering
6e70227d 16610read_array_order (struct die_info *die, struct dwarf2_cu *cu)
7ca2d3a3
DL
16611{
16612 struct attribute *attr;
16613
16614 attr = dwarf2_attr (die, DW_AT_ordering, cu);
16615
aead7601
SM
16616 if (attr)
16617 return (enum dwarf_array_dim_ordering) DW_SND (attr);
7ca2d3a3 16618
0963b4bd
MS
16619 /* GNU F77 is a special case, as at 08/2004 array type info is the
16620 opposite order to the dwarf2 specification, but data is still
16621 laid out as per normal fortran.
7ca2d3a3 16622
0963b4bd
MS
16623 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
16624 version checking. */
7ca2d3a3 16625
905e0470
PM
16626 if (cu->language == language_fortran
16627 && cu->producer && strstr (cu->producer, "GNU F77"))
7ca2d3a3
DL
16628 {
16629 return DW_ORD_row_major;
16630 }
16631
6e70227d 16632 switch (cu->language_defn->la_array_ordering)
7ca2d3a3
DL
16633 {
16634 case array_column_major:
16635 return DW_ORD_col_major;
16636 case array_row_major:
16637 default:
16638 return DW_ORD_row_major;
16639 };
16640}
16641
72019c9c 16642/* Extract all information from a DW_TAG_set_type DIE and put it in
0963b4bd 16643 the DIE's type field. */
72019c9c 16644
f792889a 16645static struct type *
72019c9c
GM
16646read_set_type (struct die_info *die, struct dwarf2_cu *cu)
16647{
7e314c57
JK
16648 struct type *domain_type, *set_type;
16649 struct attribute *attr;
f792889a 16650
7e314c57
JK
16651 domain_type = die_type (die, cu);
16652
16653 /* The die_type call above may have already set the type for this DIE. */
16654 set_type = get_die_type (die, cu);
16655 if (set_type)
16656 return set_type;
16657
16658 set_type = create_set_type (NULL, domain_type);
16659
16660 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
d09039dd
PM
16661 if (attr)
16662 TYPE_LENGTH (set_type) = DW_UNSND (attr);
7e314c57 16663
2b4424c3
TT
16664 maybe_set_alignment (cu, die, set_type);
16665
f792889a 16666 return set_die_type (die, set_type, cu);
72019c9c 16667}
7ca2d3a3 16668
0971de02
TT
16669/* A helper for read_common_block that creates a locexpr baton.
16670 SYM is the symbol which we are marking as computed.
16671 COMMON_DIE is the DIE for the common block.
16672 COMMON_LOC is the location expression attribute for the common
16673 block itself.
16674 MEMBER_LOC is the location expression attribute for the particular
16675 member of the common block that we are processing.
16676 CU is the CU from which the above come. */
16677
16678static void
16679mark_common_block_symbol_computed (struct symbol *sym,
16680 struct die_info *common_die,
16681 struct attribute *common_loc,
16682 struct attribute *member_loc,
16683 struct dwarf2_cu *cu)
16684{
518817b3
SM
16685 struct dwarf2_per_objfile *dwarf2_per_objfile
16686 = cu->per_cu->dwarf2_per_objfile;
0971de02
TT
16687 struct objfile *objfile = dwarf2_per_objfile->objfile;
16688 struct dwarf2_locexpr_baton *baton;
16689 gdb_byte *ptr;
16690 unsigned int cu_off;
16691 enum bfd_endian byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
16692 LONGEST offset = 0;
16693
16694 gdb_assert (common_loc && member_loc);
16695 gdb_assert (attr_form_is_block (common_loc));
16696 gdb_assert (attr_form_is_block (member_loc)
16697 || attr_form_is_constant (member_loc));
16698
8d749320 16699 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
0971de02
TT
16700 baton->per_cu = cu->per_cu;
16701 gdb_assert (baton->per_cu);
16702
16703 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
16704
16705 if (attr_form_is_constant (member_loc))
16706 {
16707 offset = dwarf2_get_attr_constant_value (member_loc, 0);
16708 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
16709 }
16710 else
16711 baton->size += DW_BLOCK (member_loc)->size;
16712
224c3ddb 16713 ptr = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, baton->size);
0971de02
TT
16714 baton->data = ptr;
16715
16716 *ptr++ = DW_OP_call4;
9c541725 16717 cu_off = common_die->sect_off - cu->per_cu->sect_off;
0971de02
TT
16718 store_unsigned_integer (ptr, 4, byte_order, cu_off);
16719 ptr += 4;
16720
16721 if (attr_form_is_constant (member_loc))
16722 {
16723 *ptr++ = DW_OP_addr;
16724 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
16725 ptr += cu->header.addr_size;
16726 }
16727 else
16728 {
16729 /* We have to copy the data here, because DW_OP_call4 will only
16730 use a DW_AT_location attribute. */
16731 memcpy (ptr, DW_BLOCK (member_loc)->data, DW_BLOCK (member_loc)->size);
16732 ptr += DW_BLOCK (member_loc)->size;
16733 }
16734
16735 *ptr++ = DW_OP_plus;
16736 gdb_assert (ptr - baton->data == baton->size);
16737
0971de02 16738 SYMBOL_LOCATION_BATON (sym) = baton;
f1e6e072 16739 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
0971de02
TT
16740}
16741
4357ac6c
TT
16742/* Create appropriate locally-scoped variables for all the
16743 DW_TAG_common_block entries. Also create a struct common_block
16744 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
16745 is used to sepate the common blocks name namespace from regular
16746 variable names. */
c906108c
SS
16747
16748static void
e7c27a73 16749read_common_block (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16750{
0971de02
TT
16751 struct attribute *attr;
16752
16753 attr = dwarf2_attr (die, DW_AT_location, cu);
16754 if (attr)
16755 {
16756 /* Support the .debug_loc offsets. */
16757 if (attr_form_is_block (attr))
16758 {
16759 /* Ok. */
16760 }
16761 else if (attr_form_is_section_offset (attr))
16762 {
16763 dwarf2_complex_location_expr_complaint ();
16764 attr = NULL;
16765 }
16766 else
16767 {
16768 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
16769 "common block member");
16770 attr = NULL;
16771 }
16772 }
16773
639d11d3 16774 if (die->child != NULL)
c906108c 16775 {
518817b3 16776 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
4357ac6c
TT
16777 struct die_info *child_die;
16778 size_t n_entries = 0, size;
16779 struct common_block *common_block;
16780 struct symbol *sym;
74ac6d43 16781
4357ac6c
TT
16782 for (child_die = die->child;
16783 child_die && child_die->tag;
16784 child_die = sibling_die (child_die))
16785 ++n_entries;
16786
16787 size = (sizeof (struct common_block)
16788 + (n_entries - 1) * sizeof (struct symbol *));
224c3ddb
SM
16789 common_block
16790 = (struct common_block *) obstack_alloc (&objfile->objfile_obstack,
16791 size);
4357ac6c
TT
16792 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
16793 common_block->n_entries = 0;
16794
16795 for (child_die = die->child;
16796 child_die && child_die->tag;
16797 child_die = sibling_die (child_die))
16798 {
16799 /* Create the symbol in the DW_TAG_common_block block in the current
16800 symbol scope. */
e7c27a73 16801 sym = new_symbol (child_die, NULL, cu);
0971de02
TT
16802 if (sym != NULL)
16803 {
16804 struct attribute *member_loc;
16805
16806 common_block->contents[common_block->n_entries++] = sym;
16807
16808 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
16809 cu);
16810 if (member_loc)
16811 {
16812 /* GDB has handled this for a long time, but it is
16813 not specified by DWARF. It seems to have been
16814 emitted by gfortran at least as recently as:
16815 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
b98664d3 16816 complaint (_("Variable in common block has "
0971de02 16817 "DW_AT_data_member_location "
9d8780f0
SM
16818 "- DIE at %s [in module %s]"),
16819 sect_offset_str (child_die->sect_off),
518817b3 16820 objfile_name (objfile));
0971de02
TT
16821
16822 if (attr_form_is_section_offset (member_loc))
16823 dwarf2_complex_location_expr_complaint ();
16824 else if (attr_form_is_constant (member_loc)
16825 || attr_form_is_block (member_loc))
16826 {
16827 if (attr)
16828 mark_common_block_symbol_computed (sym, die, attr,
16829 member_loc, cu);
16830 }
16831 else
16832 dwarf2_complex_location_expr_complaint ();
16833 }
16834 }
c906108c 16835 }
4357ac6c
TT
16836
16837 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
16838 SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
c906108c
SS
16839 }
16840}
16841
0114d602 16842/* Create a type for a C++ namespace. */
d9fa45fe 16843
0114d602
DJ
16844static struct type *
16845read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
d9fa45fe 16846{
518817b3 16847 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
0114d602 16848 const char *previous_prefix, *name;
9219021c 16849 int is_anonymous;
0114d602
DJ
16850 struct type *type;
16851
16852 /* For extensions, reuse the type of the original namespace. */
16853 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
16854 {
16855 struct die_info *ext_die;
16856 struct dwarf2_cu *ext_cu = cu;
9a619af0 16857
0114d602
DJ
16858 ext_die = dwarf2_extension (die, &ext_cu);
16859 type = read_type_die (ext_die, ext_cu);
9dc481d3
DE
16860
16861 /* EXT_CU may not be the same as CU.
02142a6c 16862 Ensure TYPE is recorded with CU in die_type_hash. */
0114d602
DJ
16863 return set_die_type (die, type, cu);
16864 }
9219021c 16865
e142c38c 16866 name = namespace_name (die, &is_anonymous, cu);
9219021c
DC
16867
16868 /* Now build the name of the current namespace. */
16869
0114d602
DJ
16870 previous_prefix = determine_prefix (die, cu);
16871 if (previous_prefix[0] != '\0')
16872 name = typename_concat (&objfile->objfile_obstack,
f55ee35c 16873 previous_prefix, name, 0, cu);
0114d602
DJ
16874
16875 /* Create the type. */
19f392bc 16876 type = init_type (objfile, TYPE_CODE_NAMESPACE, 0, name);
0114d602 16877
60531b24 16878 return set_die_type (die, type, cu);
0114d602
DJ
16879}
16880
22cee43f 16881/* Read a namespace scope. */
0114d602
DJ
16882
16883static void
16884read_namespace (struct die_info *die, struct dwarf2_cu *cu)
16885{
518817b3 16886 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
0114d602 16887 int is_anonymous;
9219021c 16888
5c4e30ca
DC
16889 /* Add a symbol associated to this if we haven't seen the namespace
16890 before. Also, add a using directive if it's an anonymous
16891 namespace. */
9219021c 16892
f2f0e013 16893 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
5c4e30ca
DC
16894 {
16895 struct type *type;
16896
0114d602 16897 type = read_type_die (die, cu);
e7c27a73 16898 new_symbol (die, type, cu);
5c4e30ca 16899
e8e80198 16900 namespace_name (die, &is_anonymous, cu);
5c4e30ca 16901 if (is_anonymous)
0114d602
DJ
16902 {
16903 const char *previous_prefix = determine_prefix (die, cu);
9a619af0 16904
eb1e02fd 16905 std::vector<const char *> excludes;
804d2729 16906 add_using_directive (using_directives (cu),
22cee43f 16907 previous_prefix, TYPE_NAME (type), NULL,
eb1e02fd 16908 NULL, excludes, 0, &objfile->objfile_obstack);
0114d602 16909 }
5c4e30ca 16910 }
9219021c 16911
639d11d3 16912 if (die->child != NULL)
d9fa45fe 16913 {
639d11d3 16914 struct die_info *child_die = die->child;
6e70227d 16915
d9fa45fe
DC
16916 while (child_die && child_die->tag)
16917 {
e7c27a73 16918 process_die (child_die, cu);
d9fa45fe
DC
16919 child_die = sibling_die (child_die);
16920 }
16921 }
38d518c9
EZ
16922}
16923
f55ee35c
JK
16924/* Read a Fortran module as type. This DIE can be only a declaration used for
16925 imported module. Still we need that type as local Fortran "use ... only"
16926 declaration imports depend on the created type in determine_prefix. */
16927
16928static struct type *
16929read_module_type (struct die_info *die, struct dwarf2_cu *cu)
16930{
518817b3 16931 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15d034d0 16932 const char *module_name;
f55ee35c
JK
16933 struct type *type;
16934
16935 module_name = dwarf2_name (die, cu);
16936 if (!module_name)
b98664d3 16937 complaint (_("DW_TAG_module has no name, offset %s"),
9d8780f0 16938 sect_offset_str (die->sect_off));
19f392bc 16939 type = init_type (objfile, TYPE_CODE_MODULE, 0, module_name);
f55ee35c 16940
f55ee35c
JK
16941 return set_die_type (die, type, cu);
16942}
16943
5d7cb8df
JK
16944/* Read a Fortran module. */
16945
16946static void
16947read_module (struct die_info *die, struct dwarf2_cu *cu)
16948{
16949 struct die_info *child_die = die->child;
530e8392
KB
16950 struct type *type;
16951
16952 type = read_type_die (die, cu);
16953 new_symbol (die, type, cu);
5d7cb8df 16954
5d7cb8df
JK
16955 while (child_die && child_die->tag)
16956 {
16957 process_die (child_die, cu);
16958 child_die = sibling_die (child_die);
16959 }
16960}
16961
38d518c9
EZ
16962/* Return the name of the namespace represented by DIE. Set
16963 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
16964 namespace. */
16965
16966static const char *
e142c38c 16967namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
38d518c9
EZ
16968{
16969 struct die_info *current_die;
16970 const char *name = NULL;
16971
16972 /* Loop through the extensions until we find a name. */
16973
16974 for (current_die = die;
16975 current_die != NULL;
f2f0e013 16976 current_die = dwarf2_extension (die, &cu))
38d518c9 16977 {
96553a0c
DE
16978 /* We don't use dwarf2_name here so that we can detect the absence
16979 of a name -> anonymous namespace. */
7d45c7c3 16980 name = dwarf2_string_attr (die, DW_AT_name, cu);
96553a0c 16981
38d518c9
EZ
16982 if (name != NULL)
16983 break;
16984 }
16985
16986 /* Is it an anonymous namespace? */
16987
16988 *is_anonymous = (name == NULL);
16989 if (*is_anonymous)
2b1dbab0 16990 name = CP_ANONYMOUS_NAMESPACE_STR;
38d518c9
EZ
16991
16992 return name;
d9fa45fe
DC
16993}
16994
c906108c
SS
16995/* Extract all information from a DW_TAG_pointer_type DIE and add to
16996 the user defined type vector. */
16997
f792889a 16998static struct type *
e7c27a73 16999read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17000{
518817b3
SM
17001 struct gdbarch *gdbarch
17002 = get_objfile_arch (cu->per_cu->dwarf2_per_objfile->objfile);
e7c27a73 17003 struct comp_unit_head *cu_header = &cu->header;
c906108c 17004 struct type *type;
8b2dbe47
KB
17005 struct attribute *attr_byte_size;
17006 struct attribute *attr_address_class;
17007 int byte_size, addr_class;
7e314c57
JK
17008 struct type *target_type;
17009
17010 target_type = die_type (die, cu);
c906108c 17011
7e314c57
JK
17012 /* The die_type call above may have already set the type for this DIE. */
17013 type = get_die_type (die, cu);
17014 if (type)
17015 return type;
17016
17017 type = lookup_pointer_type (target_type);
8b2dbe47 17018
e142c38c 17019 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
8b2dbe47
KB
17020 if (attr_byte_size)
17021 byte_size = DW_UNSND (attr_byte_size);
c906108c 17022 else
8b2dbe47
KB
17023 byte_size = cu_header->addr_size;
17024
e142c38c 17025 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
8b2dbe47
KB
17026 if (attr_address_class)
17027 addr_class = DW_UNSND (attr_address_class);
17028 else
17029 addr_class = DW_ADDR_none;
17030
2b4424c3
TT
17031 ULONGEST alignment = get_alignment (cu, die);
17032
17033 /* If the pointer size, alignment, or address class is different
17034 than the default, create a type variant marked as such and set
17035 the length accordingly. */
17036 if (TYPE_LENGTH (type) != byte_size
17037 || (alignment != 0 && TYPE_RAW_ALIGN (type) != 0
17038 && alignment != TYPE_RAW_ALIGN (type))
17039 || addr_class != DW_ADDR_none)
c906108c 17040 {
5e2b427d 17041 if (gdbarch_address_class_type_flags_p (gdbarch))
8b2dbe47
KB
17042 {
17043 int type_flags;
17044
849957d9 17045 type_flags = gdbarch_address_class_type_flags
5e2b427d 17046 (gdbarch, byte_size, addr_class);
876cecd0
TT
17047 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
17048 == 0);
8b2dbe47
KB
17049 type = make_type_with_address_space (type, type_flags);
17050 }
17051 else if (TYPE_LENGTH (type) != byte_size)
17052 {
b98664d3 17053 complaint (_("invalid pointer size %d"), byte_size);
8b2dbe47 17054 }
2b4424c3
TT
17055 else if (TYPE_RAW_ALIGN (type) != alignment)
17056 {
b98664d3 17057 complaint (_("Invalid DW_AT_alignment"
2b4424c3
TT
17058 " - DIE at %s [in module %s]"),
17059 sect_offset_str (die->sect_off),
17060 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
17061 }
6e70227d 17062 else
9a619af0
MS
17063 {
17064 /* Should we also complain about unhandled address classes? */
17065 }
c906108c 17066 }
8b2dbe47
KB
17067
17068 TYPE_LENGTH (type) = byte_size;
2b4424c3 17069 set_type_align (type, alignment);
f792889a 17070 return set_die_type (die, type, cu);
c906108c
SS
17071}
17072
17073/* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
17074 the user defined type vector. */
17075
f792889a 17076static struct type *
e7c27a73 17077read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
17078{
17079 struct type *type;
17080 struct type *to_type;
17081 struct type *domain;
17082
e7c27a73
DJ
17083 to_type = die_type (die, cu);
17084 domain = die_containing_type (die, cu);
0d5de010 17085
7e314c57
JK
17086 /* The calls above may have already set the type for this DIE. */
17087 type = get_die_type (die, cu);
17088 if (type)
17089 return type;
17090
0d5de010
DJ
17091 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
17092 type = lookup_methodptr_type (to_type);
7078baeb
TT
17093 else if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_FUNC)
17094 {
518817b3
SM
17095 struct type *new_type
17096 = alloc_type (cu->per_cu->dwarf2_per_objfile->objfile);
7078baeb
TT
17097
17098 smash_to_method_type (new_type, domain, TYPE_TARGET_TYPE (to_type),
17099 TYPE_FIELDS (to_type), TYPE_NFIELDS (to_type),
17100 TYPE_VARARGS (to_type));
17101 type = lookup_methodptr_type (new_type);
17102 }
0d5de010
DJ
17103 else
17104 type = lookup_memberptr_type (to_type, domain);
c906108c 17105
f792889a 17106 return set_die_type (die, type, cu);
c906108c
SS
17107}
17108
4297a3f0 17109/* Extract all information from a DW_TAG_{rvalue_,}reference_type DIE and add to
c906108c
SS
17110 the user defined type vector. */
17111
f792889a 17112static struct type *
4297a3f0
AV
17113read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu,
17114 enum type_code refcode)
c906108c 17115{
e7c27a73 17116 struct comp_unit_head *cu_header = &cu->header;
7e314c57 17117 struct type *type, *target_type;
c906108c
SS
17118 struct attribute *attr;
17119
4297a3f0
AV
17120 gdb_assert (refcode == TYPE_CODE_REF || refcode == TYPE_CODE_RVALUE_REF);
17121
7e314c57
JK
17122 target_type = die_type (die, cu);
17123
17124 /* The die_type call above may have already set the type for this DIE. */
17125 type = get_die_type (die, cu);
17126 if (type)
17127 return type;
17128
4297a3f0 17129 type = lookup_reference_type (target_type, refcode);
e142c38c 17130 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
17131 if (attr)
17132 {
17133 TYPE_LENGTH (type) = DW_UNSND (attr);
17134 }
17135 else
17136 {
107d2387 17137 TYPE_LENGTH (type) = cu_header->addr_size;
c906108c 17138 }
2b4424c3 17139 maybe_set_alignment (cu, die, type);
f792889a 17140 return set_die_type (die, type, cu);
c906108c
SS
17141}
17142
cf363f18
MW
17143/* Add the given cv-qualifiers to the element type of the array. GCC
17144 outputs DWARF type qualifiers that apply to an array, not the
17145 element type. But GDB relies on the array element type to carry
17146 the cv-qualifiers. This mimics section 6.7.3 of the C99
17147 specification. */
17148
17149static struct type *
17150add_array_cv_type (struct die_info *die, struct dwarf2_cu *cu,
17151 struct type *base_type, int cnst, int voltl)
17152{
17153 struct type *el_type, *inner_array;
17154
17155 base_type = copy_type (base_type);
17156 inner_array = base_type;
17157
17158 while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
17159 {
17160 TYPE_TARGET_TYPE (inner_array) =
17161 copy_type (TYPE_TARGET_TYPE (inner_array));
17162 inner_array = TYPE_TARGET_TYPE (inner_array);
17163 }
17164
17165 el_type = TYPE_TARGET_TYPE (inner_array);
17166 cnst |= TYPE_CONST (el_type);
17167 voltl |= TYPE_VOLATILE (el_type);
17168 TYPE_TARGET_TYPE (inner_array) = make_cv_type (cnst, voltl, el_type, NULL);
17169
17170 return set_die_type (die, base_type, cu);
17171}
17172
f792889a 17173static struct type *
e7c27a73 17174read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17175{
f792889a 17176 struct type *base_type, *cv_type;
c906108c 17177
e7c27a73 17178 base_type = die_type (die, cu);
7e314c57
JK
17179
17180 /* The die_type call above may have already set the type for this DIE. */
17181 cv_type = get_die_type (die, cu);
17182 if (cv_type)
17183 return cv_type;
17184
2f608a3a
KW
17185 /* In case the const qualifier is applied to an array type, the element type
17186 is so qualified, not the array type (section 6.7.3 of C99). */
17187 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
cf363f18 17188 return add_array_cv_type (die, cu, base_type, 1, 0);
2f608a3a 17189
f792889a
DJ
17190 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
17191 return set_die_type (die, cv_type, cu);
c906108c
SS
17192}
17193
f792889a 17194static struct type *
e7c27a73 17195read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17196{
f792889a 17197 struct type *base_type, *cv_type;
c906108c 17198
e7c27a73 17199 base_type = die_type (die, cu);
7e314c57
JK
17200
17201 /* The die_type call above may have already set the type for this DIE. */
17202 cv_type = get_die_type (die, cu);
17203 if (cv_type)
17204 return cv_type;
17205
cf363f18
MW
17206 /* In case the volatile qualifier is applied to an array type, the
17207 element type is so qualified, not the array type (section 6.7.3
17208 of C99). */
17209 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
17210 return add_array_cv_type (die, cu, base_type, 0, 1);
17211
f792889a
DJ
17212 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
17213 return set_die_type (die, cv_type, cu);
c906108c
SS
17214}
17215
06d66ee9
TT
17216/* Handle DW_TAG_restrict_type. */
17217
17218static struct type *
17219read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
17220{
17221 struct type *base_type, *cv_type;
17222
17223 base_type = die_type (die, cu);
17224
17225 /* The die_type call above may have already set the type for this DIE. */
17226 cv_type = get_die_type (die, cu);
17227 if (cv_type)
17228 return cv_type;
17229
17230 cv_type = make_restrict_type (base_type);
17231 return set_die_type (die, cv_type, cu);
17232}
17233
a2c2acaf
MW
17234/* Handle DW_TAG_atomic_type. */
17235
17236static struct type *
17237read_tag_atomic_type (struct die_info *die, struct dwarf2_cu *cu)
17238{
17239 struct type *base_type, *cv_type;
17240
17241 base_type = die_type (die, cu);
17242
17243 /* The die_type call above may have already set the type for this DIE. */
17244 cv_type = get_die_type (die, cu);
17245 if (cv_type)
17246 return cv_type;
17247
17248 cv_type = make_atomic_type (base_type);
17249 return set_die_type (die, cv_type, cu);
17250}
17251
c906108c
SS
17252/* Extract all information from a DW_TAG_string_type DIE and add to
17253 the user defined type vector. It isn't really a user defined type,
17254 but it behaves like one, with other DIE's using an AT_user_def_type
17255 attribute to reference it. */
17256
f792889a 17257static struct type *
e7c27a73 17258read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17259{
518817b3 17260 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
3b7538c0 17261 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
17262 struct type *type, *range_type, *index_type, *char_type;
17263 struct attribute *attr;
17264 unsigned int length;
17265
e142c38c 17266 attr = dwarf2_attr (die, DW_AT_string_length, cu);
c906108c
SS
17267 if (attr)
17268 {
17269 length = DW_UNSND (attr);
17270 }
17271 else
17272 {
0963b4bd 17273 /* Check for the DW_AT_byte_size attribute. */
e142c38c 17274 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
b21b22e0
PS
17275 if (attr)
17276 {
17277 length = DW_UNSND (attr);
17278 }
17279 else
17280 {
17281 length = 1;
17282 }
c906108c 17283 }
6ccb9162 17284
46bf5051 17285 index_type = objfile_type (objfile)->builtin_int;
0c9c3474 17286 range_type = create_static_range_type (NULL, index_type, 1, length);
3b7538c0
UW
17287 char_type = language_string_char_type (cu->language_defn, gdbarch);
17288 type = create_string_type (NULL, char_type, range_type);
6ccb9162 17289
f792889a 17290 return set_die_type (die, type, cu);
c906108c
SS
17291}
17292
4d804846
JB
17293/* Assuming that DIE corresponds to a function, returns nonzero
17294 if the function is prototyped. */
17295
17296static int
17297prototyped_function_p (struct die_info *die, struct dwarf2_cu *cu)
17298{
17299 struct attribute *attr;
17300
17301 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
17302 if (attr && (DW_UNSND (attr) != 0))
17303 return 1;
17304
17305 /* The DWARF standard implies that the DW_AT_prototyped attribute
17306 is only meaninful for C, but the concept also extends to other
17307 languages that allow unprototyped functions (Eg: Objective C).
17308 For all other languages, assume that functions are always
17309 prototyped. */
17310 if (cu->language != language_c
17311 && cu->language != language_objc
17312 && cu->language != language_opencl)
17313 return 1;
17314
17315 /* RealView does not emit DW_AT_prototyped. We can not distinguish
17316 prototyped and unprototyped functions; default to prototyped,
17317 since that is more common in modern code (and RealView warns
17318 about unprototyped functions). */
17319 if (producer_is_realview (cu->producer))
17320 return 1;
17321
17322 return 0;
17323}
17324
c906108c
SS
17325/* Handle DIES due to C code like:
17326
17327 struct foo
c5aa993b
JM
17328 {
17329 int (*funcp)(int a, long l);
17330 int b;
17331 };
c906108c 17332
0963b4bd 17333 ('funcp' generates a DW_TAG_subroutine_type DIE). */
c906108c 17334
f792889a 17335static struct type *
e7c27a73 17336read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17337{
518817b3 17338 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
0963b4bd
MS
17339 struct type *type; /* Type that this function returns. */
17340 struct type *ftype; /* Function that returns above type. */
c906108c
SS
17341 struct attribute *attr;
17342
e7c27a73 17343 type = die_type (die, cu);
7e314c57
JK
17344
17345 /* The die_type call above may have already set the type for this DIE. */
17346 ftype = get_die_type (die, cu);
17347 if (ftype)
17348 return ftype;
17349
0c8b41f1 17350 ftype = lookup_function_type (type);
c906108c 17351
4d804846 17352 if (prototyped_function_p (die, cu))
a6c727b2 17353 TYPE_PROTOTYPED (ftype) = 1;
c906108c 17354
c055b101
CV
17355 /* Store the calling convention in the type if it's available in
17356 the subroutine die. Otherwise set the calling convention to
17357 the default value DW_CC_normal. */
17358 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
54fcddd0
UW
17359 if (attr)
17360 TYPE_CALLING_CONVENTION (ftype) = DW_UNSND (attr);
17361 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
17362 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
17363 else
17364 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
76c10ea2 17365
743649fd
MW
17366 /* Record whether the function returns normally to its caller or not
17367 if the DWARF producer set that information. */
17368 attr = dwarf2_attr (die, DW_AT_noreturn, cu);
17369 if (attr && (DW_UNSND (attr) != 0))
17370 TYPE_NO_RETURN (ftype) = 1;
17371
76c10ea2
GM
17372 /* We need to add the subroutine type to the die immediately so
17373 we don't infinitely recurse when dealing with parameters
0963b4bd 17374 declared as the same subroutine type. */
76c10ea2 17375 set_die_type (die, ftype, cu);
6e70227d 17376
639d11d3 17377 if (die->child != NULL)
c906108c 17378 {
bb5ed363 17379 struct type *void_type = objfile_type (objfile)->builtin_void;
c906108c 17380 struct die_info *child_die;
8072405b 17381 int nparams, iparams;
c906108c
SS
17382
17383 /* Count the number of parameters.
17384 FIXME: GDB currently ignores vararg functions, but knows about
17385 vararg member functions. */
8072405b 17386 nparams = 0;
639d11d3 17387 child_die = die->child;
c906108c
SS
17388 while (child_die && child_die->tag)
17389 {
17390 if (child_die->tag == DW_TAG_formal_parameter)
17391 nparams++;
17392 else if (child_die->tag == DW_TAG_unspecified_parameters)
876cecd0 17393 TYPE_VARARGS (ftype) = 1;
c906108c
SS
17394 child_die = sibling_die (child_die);
17395 }
17396
17397 /* Allocate storage for parameters and fill them in. */
17398 TYPE_NFIELDS (ftype) = nparams;
17399 TYPE_FIELDS (ftype) = (struct field *)
ae5a43e0 17400 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
c906108c 17401
8072405b
JK
17402 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
17403 even if we error out during the parameters reading below. */
17404 for (iparams = 0; iparams < nparams; iparams++)
17405 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
17406
17407 iparams = 0;
639d11d3 17408 child_die = die->child;
c906108c
SS
17409 while (child_die && child_die->tag)
17410 {
17411 if (child_die->tag == DW_TAG_formal_parameter)
17412 {
3ce3b1ba
PA
17413 struct type *arg_type;
17414
17415 /* DWARF version 2 has no clean way to discern C++
17416 static and non-static member functions. G++ helps
17417 GDB by marking the first parameter for non-static
17418 member functions (which is the this pointer) as
17419 artificial. We pass this information to
17420 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
17421
17422 DWARF version 3 added DW_AT_object_pointer, which GCC
17423 4.5 does not yet generate. */
e142c38c 17424 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
c906108c
SS
17425 if (attr)
17426 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
17427 else
9c37b5ae 17428 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
3ce3b1ba
PA
17429 arg_type = die_type (child_die, cu);
17430
17431 /* RealView does not mark THIS as const, which the testsuite
17432 expects. GCC marks THIS as const in method definitions,
17433 but not in the class specifications (GCC PR 43053). */
17434 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
17435 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
17436 {
17437 int is_this = 0;
17438 struct dwarf2_cu *arg_cu = cu;
17439 const char *name = dwarf2_name (child_die, cu);
17440
17441 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
17442 if (attr)
17443 {
17444 /* If the compiler emits this, use it. */
17445 if (follow_die_ref (die, attr, &arg_cu) == child_die)
17446 is_this = 1;
17447 }
17448 else if (name && strcmp (name, "this") == 0)
17449 /* Function definitions will have the argument names. */
17450 is_this = 1;
17451 else if (name == NULL && iparams == 0)
17452 /* Declarations may not have the names, so like
17453 elsewhere in GDB, assume an artificial first
17454 argument is "this". */
17455 is_this = 1;
17456
17457 if (is_this)
17458 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
17459 arg_type, 0);
17460 }
17461
17462 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
c906108c
SS
17463 iparams++;
17464 }
17465 child_die = sibling_die (child_die);
17466 }
17467 }
17468
76c10ea2 17469 return ftype;
c906108c
SS
17470}
17471
f792889a 17472static struct type *
e7c27a73 17473read_typedef (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17474{
518817b3 17475 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
0114d602 17476 const char *name = NULL;
3c8e0968 17477 struct type *this_type, *target_type;
c906108c 17478
94af9270 17479 name = dwarf2_full_name (NULL, die, cu);
19f392bc
UW
17480 this_type = init_type (objfile, TYPE_CODE_TYPEDEF, 0, name);
17481 TYPE_TARGET_STUB (this_type) = 1;
f792889a 17482 set_die_type (die, this_type, cu);
3c8e0968
DE
17483 target_type = die_type (die, cu);
17484 if (target_type != this_type)
17485 TYPE_TARGET_TYPE (this_type) = target_type;
17486 else
17487 {
17488 /* Self-referential typedefs are, it seems, not allowed by the DWARF
17489 spec and cause infinite loops in GDB. */
b98664d3 17490 complaint (_("Self-referential DW_TAG_typedef "
9d8780f0
SM
17491 "- DIE at %s [in module %s]"),
17492 sect_offset_str (die->sect_off), objfile_name (objfile));
3c8e0968
DE
17493 TYPE_TARGET_TYPE (this_type) = NULL;
17494 }
f792889a 17495 return this_type;
c906108c
SS
17496}
17497
9b790ce7
UW
17498/* Allocate a floating-point type of size BITS and name NAME. Pass NAME_HINT
17499 (which may be different from NAME) to the architecture back-end to allow
17500 it to guess the correct format if necessary. */
17501
17502static struct type *
17503dwarf2_init_float_type (struct objfile *objfile, int bits, const char *name,
17504 const char *name_hint)
17505{
17506 struct gdbarch *gdbarch = get_objfile_arch (objfile);
17507 const struct floatformat **format;
17508 struct type *type;
17509
17510 format = gdbarch_floatformat_for_type (gdbarch, name_hint, bits);
17511 if (format)
17512 type = init_float_type (objfile, bits, name, format);
17513 else
77b7c781 17514 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
9b790ce7
UW
17515
17516 return type;
17517}
17518
eb77c9df
AB
17519/* Allocate an integer type of size BITS and name NAME. */
17520
17521static struct type *
17522dwarf2_init_integer_type (struct dwarf2_cu *cu, struct objfile *objfile,
17523 int bits, int unsigned_p, const char *name)
17524{
17525 struct type *type;
17526
17527 /* Versions of Intel's C Compiler generate an integer type called "void"
17528 instead of using DW_TAG_unspecified_type. This has been seen on
17529 at least versions 14, 17, and 18. */
35ee2dc2
AB
17530 if (bits == 0 && producer_is_icc (cu) && name != nullptr
17531 && strcmp (name, "void") == 0)
eb77c9df
AB
17532 type = objfile_type (objfile)->builtin_void;
17533 else
17534 type = init_integer_type (objfile, bits, unsigned_p, name);
17535
17536 return type;
17537}
17538
8bdc1658
AB
17539/* Initialise and return a floating point type of size BITS suitable for
17540 use as a component of a complex number. The NAME_HINT is passed through
17541 when initialising the floating point type and is the name of the complex
17542 type.
17543
17544 As DWARF doesn't currently provide an explicit name for the components
17545 of a complex number, but it can be helpful to have these components
17546 named, we try to select a suitable name based on the size of the
17547 component. */
17548static struct type *
17549dwarf2_init_complex_target_type (struct dwarf2_cu *cu,
17550 struct objfile *objfile,
17551 int bits, const char *name_hint)
17552{
17553 gdbarch *gdbarch = get_objfile_arch (objfile);
17554 struct type *tt = nullptr;
17555
17556 switch (bits)
17557 {
17558 case 32:
17559 tt = builtin_type (gdbarch)->builtin_float;
17560 break;
17561 case 64:
17562 tt = builtin_type (gdbarch)->builtin_double;
17563 break;
17564 case 128:
17565 tt = builtin_type (gdbarch)->builtin_long_double;
17566 break;
17567 }
17568
17569 const char *name = (tt == nullptr) ? nullptr : TYPE_NAME (tt);
17570 return dwarf2_init_float_type (objfile, bits, name, name_hint);
17571}
17572
c906108c
SS
17573/* Find a representation of a given base type and install
17574 it in the TYPE field of the die. */
17575
f792889a 17576static struct type *
e7c27a73 17577read_base_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17578{
518817b3 17579 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c
SS
17580 struct type *type;
17581 struct attribute *attr;
19f392bc 17582 int encoding = 0, bits = 0;
15d034d0 17583 const char *name;
c906108c 17584
e142c38c 17585 attr = dwarf2_attr (die, DW_AT_encoding, cu);
c906108c
SS
17586 if (attr)
17587 {
17588 encoding = DW_UNSND (attr);
17589 }
e142c38c 17590 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
17591 if (attr)
17592 {
19f392bc 17593 bits = DW_UNSND (attr) * TARGET_CHAR_BIT;
c906108c 17594 }
39cbfefa 17595 name = dwarf2_name (die, cu);
6ccb9162 17596 if (!name)
c906108c 17597 {
b98664d3 17598 complaint (_("DW_AT_name missing from DW_TAG_base_type"));
c906108c 17599 }
6ccb9162
UW
17600
17601 switch (encoding)
c906108c 17602 {
6ccb9162
UW
17603 case DW_ATE_address:
17604 /* Turn DW_ATE_address into a void * pointer. */
77b7c781 17605 type = init_type (objfile, TYPE_CODE_VOID, TARGET_CHAR_BIT, NULL);
19f392bc 17606 type = init_pointer_type (objfile, bits, name, type);
6ccb9162
UW
17607 break;
17608 case DW_ATE_boolean:
19f392bc 17609 type = init_boolean_type (objfile, bits, 1, name);
6ccb9162
UW
17610 break;
17611 case DW_ATE_complex_float:
8bdc1658 17612 type = dwarf2_init_complex_target_type (cu, objfile, bits / 2, name);
19f392bc 17613 type = init_complex_type (objfile, name, type);
6ccb9162
UW
17614 break;
17615 case DW_ATE_decimal_float:
19f392bc 17616 type = init_decfloat_type (objfile, bits, name);
6ccb9162
UW
17617 break;
17618 case DW_ATE_float:
9b790ce7 17619 type = dwarf2_init_float_type (objfile, bits, name, name);
6ccb9162
UW
17620 break;
17621 case DW_ATE_signed:
eb77c9df 17622 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
6ccb9162
UW
17623 break;
17624 case DW_ATE_unsigned:
3b2b8fea
TT
17625 if (cu->language == language_fortran
17626 && name
61012eef 17627 && startswith (name, "character("))
19f392bc
UW
17628 type = init_character_type (objfile, bits, 1, name);
17629 else
eb77c9df 17630 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
6ccb9162
UW
17631 break;
17632 case DW_ATE_signed_char:
6e70227d 17633 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea
TT
17634 || cu->language == language_pascal
17635 || cu->language == language_fortran)
19f392bc
UW
17636 type = init_character_type (objfile, bits, 0, name);
17637 else
eb77c9df 17638 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
6ccb9162
UW
17639 break;
17640 case DW_ATE_unsigned_char:
868a0084 17641 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea 17642 || cu->language == language_pascal
c44af4eb
TT
17643 || cu->language == language_fortran
17644 || cu->language == language_rust)
19f392bc
UW
17645 type = init_character_type (objfile, bits, 1, name);
17646 else
eb77c9df 17647 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
6ccb9162 17648 break;
75079b2b 17649 case DW_ATE_UTF:
53e710ac
PA
17650 {
17651 gdbarch *arch = get_objfile_arch (objfile);
17652
17653 if (bits == 16)
17654 type = builtin_type (arch)->builtin_char16;
17655 else if (bits == 32)
17656 type = builtin_type (arch)->builtin_char32;
17657 else
17658 {
b98664d3 17659 complaint (_("unsupported DW_ATE_UTF bit size: '%d'"),
53e710ac 17660 bits);
eb77c9df 17661 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
53e710ac
PA
17662 }
17663 return set_die_type (die, type, cu);
17664 }
75079b2b
TT
17665 break;
17666
6ccb9162 17667 default:
b98664d3 17668 complaint (_("unsupported DW_AT_encoding: '%s'"),
6ccb9162 17669 dwarf_type_encoding_name (encoding));
77b7c781 17670 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
6ccb9162 17671 break;
c906108c 17672 }
6ccb9162 17673
0114d602 17674 if (name && strcmp (name, "char") == 0)
876cecd0 17675 TYPE_NOSIGN (type) = 1;
0114d602 17676
2b4424c3
TT
17677 maybe_set_alignment (cu, die, type);
17678
f792889a 17679 return set_die_type (die, type, cu);
c906108c
SS
17680}
17681
80180f79
SA
17682/* Parse dwarf attribute if it's a block, reference or constant and put the
17683 resulting value of the attribute into struct bound_prop.
17684 Returns 1 if ATTR could be resolved into PROP, 0 otherwise. */
17685
17686static int
17687attr_to_dynamic_prop (const struct attribute *attr, struct die_info *die,
17688 struct dwarf2_cu *cu, struct dynamic_prop *prop)
17689{
17690 struct dwarf2_property_baton *baton;
518817b3
SM
17691 struct obstack *obstack
17692 = &cu->per_cu->dwarf2_per_objfile->objfile->objfile_obstack;
80180f79
SA
17693
17694 if (attr == NULL || prop == NULL)
17695 return 0;
17696
17697 if (attr_form_is_block (attr))
17698 {
8d749320 17699 baton = XOBNEW (obstack, struct dwarf2_property_baton);
80180f79
SA
17700 baton->referenced_type = NULL;
17701 baton->locexpr.per_cu = cu->per_cu;
17702 baton->locexpr.size = DW_BLOCK (attr)->size;
17703 baton->locexpr.data = DW_BLOCK (attr)->data;
17704 prop->data.baton = baton;
17705 prop->kind = PROP_LOCEXPR;
17706 gdb_assert (prop->data.baton != NULL);
17707 }
17708 else if (attr_form_is_ref (attr))
17709 {
17710 struct dwarf2_cu *target_cu = cu;
17711 struct die_info *target_die;
17712 struct attribute *target_attr;
17713
17714 target_die = follow_die_ref (die, attr, &target_cu);
17715 target_attr = dwarf2_attr (target_die, DW_AT_location, target_cu);
df25ebbd
JB
17716 if (target_attr == NULL)
17717 target_attr = dwarf2_attr (target_die, DW_AT_data_member_location,
17718 target_cu);
80180f79
SA
17719 if (target_attr == NULL)
17720 return 0;
17721
df25ebbd 17722 switch (target_attr->name)
80180f79 17723 {
df25ebbd
JB
17724 case DW_AT_location:
17725 if (attr_form_is_section_offset (target_attr))
17726 {
8d749320 17727 baton = XOBNEW (obstack, struct dwarf2_property_baton);
df25ebbd
JB
17728 baton->referenced_type = die_type (target_die, target_cu);
17729 fill_in_loclist_baton (cu, &baton->loclist, target_attr);
17730 prop->data.baton = baton;
17731 prop->kind = PROP_LOCLIST;
17732 gdb_assert (prop->data.baton != NULL);
17733 }
17734 else if (attr_form_is_block (target_attr))
17735 {
8d749320 17736 baton = XOBNEW (obstack, struct dwarf2_property_baton);
df25ebbd
JB
17737 baton->referenced_type = die_type (target_die, target_cu);
17738 baton->locexpr.per_cu = cu->per_cu;
17739 baton->locexpr.size = DW_BLOCK (target_attr)->size;
17740 baton->locexpr.data = DW_BLOCK (target_attr)->data;
17741 prop->data.baton = baton;
17742 prop->kind = PROP_LOCEXPR;
17743 gdb_assert (prop->data.baton != NULL);
17744 }
17745 else
17746 {
17747 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
17748 "dynamic property");
17749 return 0;
17750 }
17751 break;
17752 case DW_AT_data_member_location:
17753 {
17754 LONGEST offset;
17755
17756 if (!handle_data_member_location (target_die, target_cu,
17757 &offset))
17758 return 0;
17759
8d749320 17760 baton = XOBNEW (obstack, struct dwarf2_property_baton);
6ad395a7
JB
17761 baton->referenced_type = read_type_die (target_die->parent,
17762 target_cu);
df25ebbd
JB
17763 baton->offset_info.offset = offset;
17764 baton->offset_info.type = die_type (target_die, target_cu);
17765 prop->data.baton = baton;
17766 prop->kind = PROP_ADDR_OFFSET;
17767 break;
17768 }
80180f79
SA
17769 }
17770 }
17771 else if (attr_form_is_constant (attr))
17772 {
17773 prop->data.const_val = dwarf2_get_attr_constant_value (attr, 0);
17774 prop->kind = PROP_CONST;
17775 }
17776 else
17777 {
17778 dwarf2_invalid_attrib_class_complaint (dwarf_form_name (attr->form),
17779 dwarf2_name (die, cu));
17780 return 0;
17781 }
17782
17783 return 1;
17784}
17785
a02abb62
JB
17786/* Read the given DW_AT_subrange DIE. */
17787
f792889a 17788static struct type *
a02abb62
JB
17789read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
17790{
4c9ad8c2 17791 struct type *base_type, *orig_base_type;
a02abb62
JB
17792 struct type *range_type;
17793 struct attribute *attr;
729efb13 17794 struct dynamic_prop low, high;
4fae6e18 17795 int low_default_is_valid;
c451ebe5 17796 int high_bound_is_count = 0;
15d034d0 17797 const char *name;
d359392f 17798 ULONGEST negative_mask;
e77813c8 17799
4c9ad8c2
TT
17800 orig_base_type = die_type (die, cu);
17801 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
17802 whereas the real type might be. So, we use ORIG_BASE_TYPE when
17803 creating the range type, but we use the result of check_typedef
17804 when examining properties of the type. */
17805 base_type = check_typedef (orig_base_type);
a02abb62 17806
7e314c57
JK
17807 /* The die_type call above may have already set the type for this DIE. */
17808 range_type = get_die_type (die, cu);
17809 if (range_type)
17810 return range_type;
17811
729efb13
SA
17812 low.kind = PROP_CONST;
17813 high.kind = PROP_CONST;
17814 high.data.const_val = 0;
17815
4fae6e18
JK
17816 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
17817 omitting DW_AT_lower_bound. */
17818 switch (cu->language)
6e70227d 17819 {
4fae6e18
JK
17820 case language_c:
17821 case language_cplus:
729efb13 17822 low.data.const_val = 0;
4fae6e18
JK
17823 low_default_is_valid = 1;
17824 break;
17825 case language_fortran:
729efb13 17826 low.data.const_val = 1;
4fae6e18
JK
17827 low_default_is_valid = 1;
17828 break;
17829 case language_d:
4fae6e18 17830 case language_objc:
c44af4eb 17831 case language_rust:
729efb13 17832 low.data.const_val = 0;
4fae6e18
JK
17833 low_default_is_valid = (cu->header.version >= 4);
17834 break;
17835 case language_ada:
17836 case language_m2:
17837 case language_pascal:
729efb13 17838 low.data.const_val = 1;
4fae6e18
JK
17839 low_default_is_valid = (cu->header.version >= 4);
17840 break;
17841 default:
729efb13 17842 low.data.const_val = 0;
4fae6e18
JK
17843 low_default_is_valid = 0;
17844 break;
a02abb62
JB
17845 }
17846
e142c38c 17847 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
a02abb62 17848 if (attr)
11c1ba78 17849 attr_to_dynamic_prop (attr, die, cu, &low);
4fae6e18 17850 else if (!low_default_is_valid)
b98664d3 17851 complaint (_("Missing DW_AT_lower_bound "
9d8780f0
SM
17852 "- DIE at %s [in module %s]"),
17853 sect_offset_str (die->sect_off),
518817b3 17854 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
a02abb62 17855
506f5c41
TV
17856 struct attribute *attr_ub, *attr_count;
17857 attr = attr_ub = dwarf2_attr (die, DW_AT_upper_bound, cu);
80180f79 17858 if (!attr_to_dynamic_prop (attr, die, cu, &high))
e77813c8 17859 {
506f5c41 17860 attr = attr_count = dwarf2_attr (die, DW_AT_count, cu);
c451ebe5 17861 if (attr_to_dynamic_prop (attr, die, cu, &high))
6b662e19 17862 {
c451ebe5
SA
17863 /* If bounds are constant do the final calculation here. */
17864 if (low.kind == PROP_CONST && high.kind == PROP_CONST)
17865 high.data.const_val = low.data.const_val + high.data.const_val - 1;
17866 else
17867 high_bound_is_count = 1;
c2ff108b 17868 }
506f5c41
TV
17869 else
17870 {
17871 if (attr_ub != NULL)
17872 complaint (_("Unresolved DW_AT_upper_bound "
17873 "- DIE at %s [in module %s]"),
17874 sect_offset_str (die->sect_off),
17875 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
17876 if (attr_count != NULL)
17877 complaint (_("Unresolved DW_AT_count "
17878 "- DIE at %s [in module %s]"),
17879 sect_offset_str (die->sect_off),
17880 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
17881 }
17882
e77813c8
PM
17883 }
17884
17885 /* Dwarf-2 specifications explicitly allows to create subrange types
17886 without specifying a base type.
17887 In that case, the base type must be set to the type of
17888 the lower bound, upper bound or count, in that order, if any of these
17889 three attributes references an object that has a type.
17890 If no base type is found, the Dwarf-2 specifications say that
17891 a signed integer type of size equal to the size of an address should
17892 be used.
17893 For the following C code: `extern char gdb_int [];'
17894 GCC produces an empty range DIE.
17895 FIXME: muller/2010-05-28: Possible references to object for low bound,
0963b4bd 17896 high bound or count are not yet handled by this code. */
e77813c8
PM
17897 if (TYPE_CODE (base_type) == TYPE_CODE_VOID)
17898 {
518817b3 17899 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
e77813c8
PM
17900 struct gdbarch *gdbarch = get_objfile_arch (objfile);
17901 int addr_size = gdbarch_addr_bit (gdbarch) /8;
17902 struct type *int_type = objfile_type (objfile)->builtin_int;
17903
17904 /* Test "int", "long int", and "long long int" objfile types,
17905 and select the first one having a size above or equal to the
17906 architecture address size. */
17907 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
17908 base_type = int_type;
17909 else
17910 {
17911 int_type = objfile_type (objfile)->builtin_long;
17912 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
17913 base_type = int_type;
17914 else
17915 {
17916 int_type = objfile_type (objfile)->builtin_long_long;
17917 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
17918 base_type = int_type;
17919 }
17920 }
17921 }
a02abb62 17922
dbb9c2b1
JB
17923 /* Normally, the DWARF producers are expected to use a signed
17924 constant form (Eg. DW_FORM_sdata) to express negative bounds.
17925 But this is unfortunately not always the case, as witnessed
17926 with GCC, for instance, where the ambiguous DW_FORM_dataN form
17927 is used instead. To work around that ambiguity, we treat
17928 the bounds as signed, and thus sign-extend their values, when
17929 the base type is signed. */
6e70227d 17930 negative_mask =
d359392f 17931 -((ULONGEST) 1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1));
729efb13
SA
17932 if (low.kind == PROP_CONST
17933 && !TYPE_UNSIGNED (base_type) && (low.data.const_val & negative_mask))
17934 low.data.const_val |= negative_mask;
17935 if (high.kind == PROP_CONST
17936 && !TYPE_UNSIGNED (base_type) && (high.data.const_val & negative_mask))
17937 high.data.const_val |= negative_mask;
43bbcdc2 17938
729efb13 17939 range_type = create_range_type (NULL, orig_base_type, &low, &high);
a02abb62 17940
c451ebe5
SA
17941 if (high_bound_is_count)
17942 TYPE_RANGE_DATA (range_type)->flag_upper_bound_is_count = 1;
17943
c2ff108b
JK
17944 /* Ada expects an empty array on no boundary attributes. */
17945 if (attr == NULL && cu->language != language_ada)
729efb13 17946 TYPE_HIGH_BOUND_KIND (range_type) = PROP_UNDEFINED;
c2ff108b 17947
39cbfefa
DJ
17948 name = dwarf2_name (die, cu);
17949 if (name)
17950 TYPE_NAME (range_type) = name;
6e70227d 17951
e142c38c 17952 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
a02abb62
JB
17953 if (attr)
17954 TYPE_LENGTH (range_type) = DW_UNSND (attr);
17955
2b4424c3
TT
17956 maybe_set_alignment (cu, die, range_type);
17957
7e314c57
JK
17958 set_die_type (die, range_type, cu);
17959
17960 /* set_die_type should be already done. */
b4ba55a1
JB
17961 set_descriptive_type (range_type, die, cu);
17962
7e314c57 17963 return range_type;
a02abb62 17964}
6e70227d 17965
f792889a 17966static struct type *
81a17f79
JB
17967read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
17968{
17969 struct type *type;
81a17f79 17970
518817b3
SM
17971 type = init_type (cu->per_cu->dwarf2_per_objfile->objfile, TYPE_CODE_VOID,0,
17972 NULL);
0114d602 17973 TYPE_NAME (type) = dwarf2_name (die, cu);
81a17f79 17974
74a2f8ff
JB
17975 /* In Ada, an unspecified type is typically used when the description
17976 of the type is defered to a different unit. When encountering
17977 such a type, we treat it as a stub, and try to resolve it later on,
17978 when needed. */
17979 if (cu->language == language_ada)
17980 TYPE_STUB (type) = 1;
17981
f792889a 17982 return set_die_type (die, type, cu);
81a17f79 17983}
a02abb62 17984
639d11d3
DC
17985/* Read a single die and all its descendents. Set the die's sibling
17986 field to NULL; set other fields in the die correctly, and set all
17987 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
17988 location of the info_ptr after reading all of those dies. PARENT
17989 is the parent of the die in question. */
17990
17991static struct die_info *
dee91e82 17992read_die_and_children (const struct die_reader_specs *reader,
d521ce57
TT
17993 const gdb_byte *info_ptr,
17994 const gdb_byte **new_info_ptr,
dee91e82 17995 struct die_info *parent)
639d11d3
DC
17996{
17997 struct die_info *die;
d521ce57 17998 const gdb_byte *cur_ptr;
639d11d3
DC
17999 int has_children;
18000
bf6af496 18001 cur_ptr = read_full_die_1 (reader, &die, info_ptr, &has_children, 0);
1d325ec1
DJ
18002 if (die == NULL)
18003 {
18004 *new_info_ptr = cur_ptr;
18005 return NULL;
18006 }
93311388 18007 store_in_ref_table (die, reader->cu);
639d11d3
DC
18008
18009 if (has_children)
bf6af496 18010 die->child = read_die_and_siblings_1 (reader, cur_ptr, new_info_ptr, die);
639d11d3
DC
18011 else
18012 {
18013 die->child = NULL;
18014 *new_info_ptr = cur_ptr;
18015 }
18016
18017 die->sibling = NULL;
18018 die->parent = parent;
18019 return die;
18020}
18021
18022/* Read a die, all of its descendents, and all of its siblings; set
18023 all of the fields of all of the dies correctly. Arguments are as
18024 in read_die_and_children. */
18025
18026static struct die_info *
bf6af496 18027read_die_and_siblings_1 (const struct die_reader_specs *reader,
d521ce57
TT
18028 const gdb_byte *info_ptr,
18029 const gdb_byte **new_info_ptr,
bf6af496 18030 struct die_info *parent)
639d11d3
DC
18031{
18032 struct die_info *first_die, *last_sibling;
d521ce57 18033 const gdb_byte *cur_ptr;
639d11d3 18034
c906108c 18035 cur_ptr = info_ptr;
639d11d3
DC
18036 first_die = last_sibling = NULL;
18037
18038 while (1)
c906108c 18039 {
639d11d3 18040 struct die_info *die
dee91e82 18041 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
639d11d3 18042
1d325ec1 18043 if (die == NULL)
c906108c 18044 {
639d11d3
DC
18045 *new_info_ptr = cur_ptr;
18046 return first_die;
c906108c 18047 }
1d325ec1
DJ
18048
18049 if (!first_die)
18050 first_die = die;
c906108c 18051 else
1d325ec1
DJ
18052 last_sibling->sibling = die;
18053
18054 last_sibling = die;
c906108c 18055 }
c906108c
SS
18056}
18057
bf6af496
DE
18058/* Read a die, all of its descendents, and all of its siblings; set
18059 all of the fields of all of the dies correctly. Arguments are as
18060 in read_die_and_children.
18061 This the main entry point for reading a DIE and all its children. */
18062
18063static struct die_info *
18064read_die_and_siblings (const struct die_reader_specs *reader,
d521ce57
TT
18065 const gdb_byte *info_ptr,
18066 const gdb_byte **new_info_ptr,
bf6af496
DE
18067 struct die_info *parent)
18068{
18069 struct die_info *die = read_die_and_siblings_1 (reader, info_ptr,
18070 new_info_ptr, parent);
18071
b4f54984 18072 if (dwarf_die_debug)
bf6af496
DE
18073 {
18074 fprintf_unfiltered (gdb_stdlog,
18075 "Read die from %s@0x%x of %s:\n",
a32a8923 18076 get_section_name (reader->die_section),
bf6af496
DE
18077 (unsigned) (info_ptr - reader->die_section->buffer),
18078 bfd_get_filename (reader->abfd));
b4f54984 18079 dump_die (die, dwarf_die_debug);
bf6af496
DE
18080 }
18081
18082 return die;
18083}
18084
3019eac3
DE
18085/* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
18086 attributes.
18087 The caller is responsible for filling in the extra attributes
18088 and updating (*DIEP)->num_attrs.
18089 Set DIEP to point to a newly allocated die with its information,
18090 except for its child, sibling, and parent fields.
18091 Set HAS_CHILDREN to tell whether the die has children or not. */
93311388 18092
d521ce57 18093static const gdb_byte *
3019eac3 18094read_full_die_1 (const struct die_reader_specs *reader,
d521ce57 18095 struct die_info **diep, const gdb_byte *info_ptr,
3019eac3 18096 int *has_children, int num_extra_attrs)
93311388 18097{
b64f50a1 18098 unsigned int abbrev_number, bytes_read, i;
93311388
DE
18099 struct abbrev_info *abbrev;
18100 struct die_info *die;
18101 struct dwarf2_cu *cu = reader->cu;
18102 bfd *abfd = reader->abfd;
18103
9c541725 18104 sect_offset sect_off = (sect_offset) (info_ptr - reader->buffer);
93311388
DE
18105 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18106 info_ptr += bytes_read;
18107 if (!abbrev_number)
18108 {
18109 *diep = NULL;
18110 *has_children = 0;
18111 return info_ptr;
18112 }
18113
685af9cd 18114 abbrev = reader->abbrev_table->lookup_abbrev (abbrev_number);
93311388 18115 if (!abbrev)
348e048f
DE
18116 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
18117 abbrev_number,
18118 bfd_get_filename (abfd));
18119
3019eac3 18120 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
9c541725 18121 die->sect_off = sect_off;
93311388
DE
18122 die->tag = abbrev->tag;
18123 die->abbrev = abbrev_number;
18124
3019eac3
DE
18125 /* Make the result usable.
18126 The caller needs to update num_attrs after adding the extra
18127 attributes. */
93311388
DE
18128 die->num_attrs = abbrev->num_attrs;
18129
18130 for (i = 0; i < abbrev->num_attrs; ++i)
dee91e82
DE
18131 info_ptr = read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
18132 info_ptr);
93311388
DE
18133
18134 *diep = die;
18135 *has_children = abbrev->has_children;
18136 return info_ptr;
18137}
18138
3019eac3
DE
18139/* Read a die and all its attributes.
18140 Set DIEP to point to a newly allocated die with its information,
18141 except for its child, sibling, and parent fields.
18142 Set HAS_CHILDREN to tell whether the die has children or not. */
18143
d521ce57 18144static const gdb_byte *
3019eac3 18145read_full_die (const struct die_reader_specs *reader,
d521ce57 18146 struct die_info **diep, const gdb_byte *info_ptr,
3019eac3
DE
18147 int *has_children)
18148{
d521ce57 18149 const gdb_byte *result;
bf6af496
DE
18150
18151 result = read_full_die_1 (reader, diep, info_ptr, has_children, 0);
18152
b4f54984 18153 if (dwarf_die_debug)
bf6af496
DE
18154 {
18155 fprintf_unfiltered (gdb_stdlog,
18156 "Read die from %s@0x%x of %s:\n",
a32a8923 18157 get_section_name (reader->die_section),
bf6af496
DE
18158 (unsigned) (info_ptr - reader->die_section->buffer),
18159 bfd_get_filename (reader->abfd));
b4f54984 18160 dump_die (*diep, dwarf_die_debug);
bf6af496
DE
18161 }
18162
18163 return result;
3019eac3 18164}
433df2d4
DE
18165\f
18166/* Abbreviation tables.
3019eac3 18167
433df2d4 18168 In DWARF version 2, the description of the debugging information is
c906108c
SS
18169 stored in a separate .debug_abbrev section. Before we read any
18170 dies from a section we read in all abbreviations and install them
433df2d4
DE
18171 in a hash table. */
18172
18173/* Allocate space for a struct abbrev_info object in ABBREV_TABLE. */
18174
685af9cd
TT
18175struct abbrev_info *
18176abbrev_table::alloc_abbrev ()
433df2d4
DE
18177{
18178 struct abbrev_info *abbrev;
18179
685af9cd 18180 abbrev = XOBNEW (&abbrev_obstack, struct abbrev_info);
433df2d4 18181 memset (abbrev, 0, sizeof (struct abbrev_info));
8d749320 18182
433df2d4
DE
18183 return abbrev;
18184}
18185
18186/* Add an abbreviation to the table. */
c906108c 18187
685af9cd
TT
18188void
18189abbrev_table::add_abbrev (unsigned int abbrev_number,
18190 struct abbrev_info *abbrev)
433df2d4
DE
18191{
18192 unsigned int hash_number;
18193
18194 hash_number = abbrev_number % ABBREV_HASH_SIZE;
4a17f768
YQ
18195 abbrev->next = m_abbrevs[hash_number];
18196 m_abbrevs[hash_number] = abbrev;
433df2d4 18197}
dee91e82 18198
433df2d4
DE
18199/* Look up an abbrev in the table.
18200 Returns NULL if the abbrev is not found. */
18201
685af9cd
TT
18202struct abbrev_info *
18203abbrev_table::lookup_abbrev (unsigned int abbrev_number)
c906108c 18204{
433df2d4
DE
18205 unsigned int hash_number;
18206 struct abbrev_info *abbrev;
18207
18208 hash_number = abbrev_number % ABBREV_HASH_SIZE;
4a17f768 18209 abbrev = m_abbrevs[hash_number];
433df2d4
DE
18210
18211 while (abbrev)
18212 {
18213 if (abbrev->number == abbrev_number)
18214 return abbrev;
18215 abbrev = abbrev->next;
18216 }
18217 return NULL;
18218}
18219
18220/* Read in an abbrev table. */
18221
685af9cd 18222static abbrev_table_up
ed2dc618
SM
18223abbrev_table_read_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
18224 struct dwarf2_section_info *section,
9c541725 18225 sect_offset sect_off)
433df2d4
DE
18226{
18227 struct objfile *objfile = dwarf2_per_objfile->objfile;
a32a8923 18228 bfd *abfd = get_section_bfd_owner (section);
d521ce57 18229 const gdb_byte *abbrev_ptr;
c906108c
SS
18230 struct abbrev_info *cur_abbrev;
18231 unsigned int abbrev_number, bytes_read, abbrev_name;
433df2d4 18232 unsigned int abbrev_form;
f3dd6933
DJ
18233 struct attr_abbrev *cur_attrs;
18234 unsigned int allocated_attrs;
c906108c 18235
685af9cd 18236 abbrev_table_up abbrev_table (new struct abbrev_table (sect_off));
c906108c 18237
433df2d4 18238 dwarf2_read_section (objfile, section);
9c541725 18239 abbrev_ptr = section->buffer + to_underlying (sect_off);
c906108c
SS
18240 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18241 abbrev_ptr += bytes_read;
18242
f3dd6933 18243 allocated_attrs = ATTR_ALLOC_CHUNK;
8d749320 18244 cur_attrs = XNEWVEC (struct attr_abbrev, allocated_attrs);
6e70227d 18245
0963b4bd 18246 /* Loop until we reach an abbrev number of 0. */
c906108c
SS
18247 while (abbrev_number)
18248 {
685af9cd 18249 cur_abbrev = abbrev_table->alloc_abbrev ();
c906108c
SS
18250
18251 /* read in abbrev header */
18252 cur_abbrev->number = abbrev_number;
aead7601
SM
18253 cur_abbrev->tag
18254 = (enum dwarf_tag) read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
c906108c
SS
18255 abbrev_ptr += bytes_read;
18256 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
18257 abbrev_ptr += 1;
18258
18259 /* now read in declarations */
22d2f3ab 18260 for (;;)
c906108c 18261 {
43988095
JK
18262 LONGEST implicit_const;
18263
22d2f3ab
JK
18264 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18265 abbrev_ptr += bytes_read;
18266 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18267 abbrev_ptr += bytes_read;
43988095
JK
18268 if (abbrev_form == DW_FORM_implicit_const)
18269 {
18270 implicit_const = read_signed_leb128 (abfd, abbrev_ptr,
18271 &bytes_read);
18272 abbrev_ptr += bytes_read;
18273 }
18274 else
18275 {
18276 /* Initialize it due to a false compiler warning. */
18277 implicit_const = -1;
18278 }
22d2f3ab
JK
18279
18280 if (abbrev_name == 0)
18281 break;
18282
f3dd6933 18283 if (cur_abbrev->num_attrs == allocated_attrs)
c906108c 18284 {
f3dd6933
DJ
18285 allocated_attrs += ATTR_ALLOC_CHUNK;
18286 cur_attrs
224c3ddb 18287 = XRESIZEVEC (struct attr_abbrev, cur_attrs, allocated_attrs);
c906108c 18288 }
ae038cb0 18289
aead7601
SM
18290 cur_attrs[cur_abbrev->num_attrs].name
18291 = (enum dwarf_attribute) abbrev_name;
22d2f3ab 18292 cur_attrs[cur_abbrev->num_attrs].form
aead7601 18293 = (enum dwarf_form) abbrev_form;
43988095 18294 cur_attrs[cur_abbrev->num_attrs].implicit_const = implicit_const;
22d2f3ab 18295 ++cur_abbrev->num_attrs;
c906108c
SS
18296 }
18297
8d749320
SM
18298 cur_abbrev->attrs =
18299 XOBNEWVEC (&abbrev_table->abbrev_obstack, struct attr_abbrev,
18300 cur_abbrev->num_attrs);
f3dd6933
DJ
18301 memcpy (cur_abbrev->attrs, cur_attrs,
18302 cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
18303
685af9cd 18304 abbrev_table->add_abbrev (abbrev_number, cur_abbrev);
c906108c
SS
18305
18306 /* Get next abbreviation.
18307 Under Irix6 the abbreviations for a compilation unit are not
c5aa993b
JM
18308 always properly terminated with an abbrev number of 0.
18309 Exit loop if we encounter an abbreviation which we have
18310 already read (which means we are about to read the abbreviations
18311 for the next compile unit) or if the end of the abbreviation
18312 table is reached. */
433df2d4 18313 if ((unsigned int) (abbrev_ptr - section->buffer) >= section->size)
c906108c
SS
18314 break;
18315 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18316 abbrev_ptr += bytes_read;
685af9cd 18317 if (abbrev_table->lookup_abbrev (abbrev_number) != NULL)
c906108c
SS
18318 break;
18319 }
f3dd6933
DJ
18320
18321 xfree (cur_attrs);
433df2d4 18322 return abbrev_table;
c906108c
SS
18323}
18324
72bf9492
DJ
18325/* Returns nonzero if TAG represents a type that we might generate a partial
18326 symbol for. */
18327
18328static int
18329is_type_tag_for_partial (int tag)
18330{
18331 switch (tag)
18332 {
18333#if 0
18334 /* Some types that would be reasonable to generate partial symbols for,
18335 that we don't at present. */
18336 case DW_TAG_array_type:
18337 case DW_TAG_file_type:
18338 case DW_TAG_ptr_to_member_type:
18339 case DW_TAG_set_type:
18340 case DW_TAG_string_type:
18341 case DW_TAG_subroutine_type:
18342#endif
18343 case DW_TAG_base_type:
18344 case DW_TAG_class_type:
680b30c7 18345 case DW_TAG_interface_type:
72bf9492
DJ
18346 case DW_TAG_enumeration_type:
18347 case DW_TAG_structure_type:
18348 case DW_TAG_subrange_type:
18349 case DW_TAG_typedef:
18350 case DW_TAG_union_type:
18351 return 1;
18352 default:
18353 return 0;
18354 }
18355}
18356
18357/* Load all DIEs that are interesting for partial symbols into memory. */
18358
18359static struct partial_die_info *
dee91e82 18360load_partial_dies (const struct die_reader_specs *reader,
d521ce57 18361 const gdb_byte *info_ptr, int building_psymtab)
72bf9492 18362{
dee91e82 18363 struct dwarf2_cu *cu = reader->cu;
518817b3 18364 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
72bf9492 18365 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
72bf9492 18366 unsigned int bytes_read;
5afb4e99 18367 unsigned int load_all = 0;
72bf9492
DJ
18368 int nesting_level = 1;
18369
18370 parent_die = NULL;
18371 last_die = NULL;
18372
7adf1e79
DE
18373 gdb_assert (cu->per_cu != NULL);
18374 if (cu->per_cu->load_all_dies)
5afb4e99
DJ
18375 load_all = 1;
18376
72bf9492
DJ
18377 cu->partial_dies
18378 = htab_create_alloc_ex (cu->header.length / 12,
18379 partial_die_hash,
18380 partial_die_eq,
18381 NULL,
18382 &cu->comp_unit_obstack,
18383 hashtab_obstack_allocate,
18384 dummy_obstack_deallocate);
18385
72bf9492
DJ
18386 while (1)
18387 {
685af9cd 18388 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
72bf9492
DJ
18389
18390 /* A NULL abbrev means the end of a series of children. */
18391 if (abbrev == NULL)
18392 {
18393 if (--nesting_level == 0)
cd9983dd
YQ
18394 return first_die;
18395
72bf9492
DJ
18396 info_ptr += bytes_read;
18397 last_die = parent_die;
18398 parent_die = parent_die->die_parent;
18399 continue;
18400 }
18401
98bfdba5
PA
18402 /* Check for template arguments. We never save these; if
18403 they're seen, we just mark the parent, and go on our way. */
18404 if (parent_die != NULL
18405 && cu->language == language_cplus
18406 && (abbrev->tag == DW_TAG_template_type_param
18407 || abbrev->tag == DW_TAG_template_value_param))
18408 {
18409 parent_die->has_template_arguments = 1;
18410
18411 if (!load_all)
18412 {
18413 /* We don't need a partial DIE for the template argument. */
dee91e82 18414 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
98bfdba5
PA
18415 continue;
18416 }
18417 }
18418
0d99eb77 18419 /* We only recurse into c++ subprograms looking for template arguments.
98bfdba5
PA
18420 Skip their other children. */
18421 if (!load_all
18422 && cu->language == language_cplus
18423 && parent_die != NULL
18424 && parent_die->tag == DW_TAG_subprogram)
18425 {
dee91e82 18426 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
98bfdba5
PA
18427 continue;
18428 }
18429
5afb4e99
DJ
18430 /* Check whether this DIE is interesting enough to save. Normally
18431 we would not be interested in members here, but there may be
18432 later variables referencing them via DW_AT_specification (for
18433 static members). */
18434 if (!load_all
18435 && !is_type_tag_for_partial (abbrev->tag)
72929c62 18436 && abbrev->tag != DW_TAG_constant
72bf9492
DJ
18437 && abbrev->tag != DW_TAG_enumerator
18438 && abbrev->tag != DW_TAG_subprogram
b1dc1806 18439 && abbrev->tag != DW_TAG_inlined_subroutine
bc30ff58 18440 && abbrev->tag != DW_TAG_lexical_block
72bf9492 18441 && abbrev->tag != DW_TAG_variable
5afb4e99 18442 && abbrev->tag != DW_TAG_namespace
f55ee35c 18443 && abbrev->tag != DW_TAG_module
95554aad 18444 && abbrev->tag != DW_TAG_member
74921315
KS
18445 && abbrev->tag != DW_TAG_imported_unit
18446 && abbrev->tag != DW_TAG_imported_declaration)
72bf9492
DJ
18447 {
18448 /* Otherwise we skip to the next sibling, if any. */
dee91e82 18449 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
72bf9492
DJ
18450 continue;
18451 }
18452
6f06d47b
YQ
18453 struct partial_die_info pdi ((sect_offset) (info_ptr - reader->buffer),
18454 abbrev);
cd9983dd 18455
48fbe735 18456 info_ptr = pdi.read (reader, *abbrev, info_ptr + bytes_read);
72bf9492
DJ
18457
18458 /* This two-pass algorithm for processing partial symbols has a
18459 high cost in cache pressure. Thus, handle some simple cases
18460 here which cover the majority of C partial symbols. DIEs
18461 which neither have specification tags in them, nor could have
18462 specification tags elsewhere pointing at them, can simply be
18463 processed and discarded.
18464
18465 This segment is also optional; scan_partial_symbols and
18466 add_partial_symbol will handle these DIEs if we chain
18467 them in normally. When compilers which do not emit large
18468 quantities of duplicate debug information are more common,
18469 this code can probably be removed. */
18470
18471 /* Any complete simple types at the top level (pretty much all
18472 of them, for a language without namespaces), can be processed
18473 directly. */
18474 if (parent_die == NULL
cd9983dd
YQ
18475 && pdi.has_specification == 0
18476 && pdi.is_declaration == 0
18477 && ((pdi.tag == DW_TAG_typedef && !pdi.has_children)
18478 || pdi.tag == DW_TAG_base_type
18479 || pdi.tag == DW_TAG_subrange_type))
72bf9492 18480 {
cd9983dd
YQ
18481 if (building_psymtab && pdi.name != NULL)
18482 add_psymbol_to_list (pdi.name, strlen (pdi.name), 0,
79748972 18483 VAR_DOMAIN, LOC_TYPEDEF, -1,
75aedd27 18484 psymbol_placement::STATIC,
1762568f 18485 0, cu->language, objfile);
cd9983dd 18486 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
72bf9492
DJ
18487 continue;
18488 }
18489
d8228535
JK
18490 /* The exception for DW_TAG_typedef with has_children above is
18491 a workaround of GCC PR debug/47510. In the case of this complaint
a737d952 18492 type_name_or_error will error on such types later.
d8228535
JK
18493
18494 GDB skipped children of DW_TAG_typedef by the shortcut above and then
18495 it could not find the child DIEs referenced later, this is checked
18496 above. In correct DWARF DW_TAG_typedef should have no children. */
18497
cd9983dd 18498 if (pdi.tag == DW_TAG_typedef && pdi.has_children)
b98664d3 18499 complaint (_("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
9d8780f0 18500 "- DIE at %s [in module %s]"),
cd9983dd 18501 sect_offset_str (pdi.sect_off), objfile_name (objfile));
d8228535 18502
72bf9492
DJ
18503 /* If we're at the second level, and we're an enumerator, and
18504 our parent has no specification (meaning possibly lives in a
18505 namespace elsewhere), then we can add the partial symbol now
18506 instead of queueing it. */
cd9983dd 18507 if (pdi.tag == DW_TAG_enumerator
72bf9492
DJ
18508 && parent_die != NULL
18509 && parent_die->die_parent == NULL
18510 && parent_die->tag == DW_TAG_enumeration_type
18511 && parent_die->has_specification == 0)
18512 {
cd9983dd 18513 if (pdi.name == NULL)
b98664d3 18514 complaint (_("malformed enumerator DIE ignored"));
72bf9492 18515 else if (building_psymtab)
cd9983dd 18516 add_psymbol_to_list (pdi.name, strlen (pdi.name), 0,
79748972 18517 VAR_DOMAIN, LOC_CONST, -1,
9c37b5ae 18518 cu->language == language_cplus
75aedd27
TT
18519 ? psymbol_placement::GLOBAL
18520 : psymbol_placement::STATIC,
1762568f 18521 0, cu->language, objfile);
72bf9492 18522
cd9983dd 18523 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
72bf9492
DJ
18524 continue;
18525 }
18526
cd9983dd 18527 struct partial_die_info *part_die
6f06d47b 18528 = new (&cu->comp_unit_obstack) partial_die_info (pdi);
cd9983dd 18529
72bf9492
DJ
18530 /* We'll save this DIE so link it in. */
18531 part_die->die_parent = parent_die;
18532 part_die->die_sibling = NULL;
18533 part_die->die_child = NULL;
18534
18535 if (last_die && last_die == parent_die)
18536 last_die->die_child = part_die;
18537 else if (last_die)
18538 last_die->die_sibling = part_die;
18539
18540 last_die = part_die;
18541
18542 if (first_die == NULL)
18543 first_die = part_die;
18544
18545 /* Maybe add the DIE to the hash table. Not all DIEs that we
18546 find interesting need to be in the hash table, because we
18547 also have the parent/sibling/child chains; only those that we
18548 might refer to by offset later during partial symbol reading.
18549
18550 For now this means things that might have be the target of a
18551 DW_AT_specification, DW_AT_abstract_origin, or
18552 DW_AT_extension. DW_AT_extension will refer only to
18553 namespaces; DW_AT_abstract_origin refers to functions (and
18554 many things under the function DIE, but we do not recurse
18555 into function DIEs during partial symbol reading) and
18556 possibly variables as well; DW_AT_specification refers to
18557 declarations. Declarations ought to have the DW_AT_declaration
18558 flag. It happens that GCC forgets to put it in sometimes, but
18559 only for functions, not for types.
18560
18561 Adding more things than necessary to the hash table is harmless
18562 except for the performance cost. Adding too few will result in
5afb4e99
DJ
18563 wasted time in find_partial_die, when we reread the compilation
18564 unit with load_all_dies set. */
72bf9492 18565
5afb4e99 18566 if (load_all
72929c62 18567 || abbrev->tag == DW_TAG_constant
5afb4e99 18568 || abbrev->tag == DW_TAG_subprogram
72bf9492
DJ
18569 || abbrev->tag == DW_TAG_variable
18570 || abbrev->tag == DW_TAG_namespace
18571 || part_die->is_declaration)
18572 {
18573 void **slot;
18574
18575 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
9c541725
PA
18576 to_underlying (part_die->sect_off),
18577 INSERT);
72bf9492
DJ
18578 *slot = part_die;
18579 }
18580
72bf9492 18581 /* For some DIEs we want to follow their children (if any). For C
bc30ff58 18582 we have no reason to follow the children of structures; for other
98bfdba5
PA
18583 languages we have to, so that we can get at method physnames
18584 to infer fully qualified class names, for DW_AT_specification,
18585 and for C++ template arguments. For C++, we also look one level
18586 inside functions to find template arguments (if the name of the
18587 function does not already contain the template arguments).
bc30ff58
JB
18588
18589 For Ada, we need to scan the children of subprograms and lexical
18590 blocks as well because Ada allows the definition of nested
18591 entities that could be interesting for the debugger, such as
18592 nested subprograms for instance. */
72bf9492 18593 if (last_die->has_children
5afb4e99
DJ
18594 && (load_all
18595 || last_die->tag == DW_TAG_namespace
f55ee35c 18596 || last_die->tag == DW_TAG_module
72bf9492 18597 || last_die->tag == DW_TAG_enumeration_type
98bfdba5
PA
18598 || (cu->language == language_cplus
18599 && last_die->tag == DW_TAG_subprogram
18600 && (last_die->name == NULL
18601 || strchr (last_die->name, '<') == NULL))
72bf9492
DJ
18602 || (cu->language != language_c
18603 && (last_die->tag == DW_TAG_class_type
680b30c7 18604 || last_die->tag == DW_TAG_interface_type
72bf9492 18605 || last_die->tag == DW_TAG_structure_type
bc30ff58
JB
18606 || last_die->tag == DW_TAG_union_type))
18607 || (cu->language == language_ada
18608 && (last_die->tag == DW_TAG_subprogram
18609 || last_die->tag == DW_TAG_lexical_block))))
72bf9492
DJ
18610 {
18611 nesting_level++;
18612 parent_die = last_die;
18613 continue;
18614 }
18615
18616 /* Otherwise we skip to the next sibling, if any. */
dee91e82 18617 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
72bf9492
DJ
18618
18619 /* Back to the top, do it again. */
18620 }
18621}
18622
6f06d47b
YQ
18623partial_die_info::partial_die_info (sect_offset sect_off_,
18624 struct abbrev_info *abbrev)
18625 : partial_die_info (sect_off_, abbrev->tag, abbrev->has_children)
18626{
18627}
18628
35cc7ed7
YQ
18629/* Read a minimal amount of information into the minimal die structure.
18630 INFO_PTR should point just after the initial uleb128 of a DIE. */
c906108c 18631
48fbe735
YQ
18632const gdb_byte *
18633partial_die_info::read (const struct die_reader_specs *reader,
18634 const struct abbrev_info &abbrev, const gdb_byte *info_ptr)
c906108c 18635{
dee91e82 18636 struct dwarf2_cu *cu = reader->cu;
518817b3
SM
18637 struct dwarf2_per_objfile *dwarf2_per_objfile
18638 = cu->per_cu->dwarf2_per_objfile;
fa238c03 18639 unsigned int i;
c5aa993b 18640 int has_low_pc_attr = 0;
c906108c 18641 int has_high_pc_attr = 0;
91da1414 18642 int high_pc_relative = 0;
c906108c 18643
fd0a254f 18644 for (i = 0; i < abbrev.num_attrs; ++i)
c906108c 18645 {
48fbe735
YQ
18646 struct attribute attr;
18647
fd0a254f 18648 info_ptr = read_attribute (reader, &attr, &abbrev.attrs[i], info_ptr);
c906108c
SS
18649
18650 /* Store the data if it is of an attribute we want to keep in a
c5aa993b 18651 partial symbol table. */
c906108c
SS
18652 switch (attr.name)
18653 {
18654 case DW_AT_name:
48fbe735 18655 switch (tag)
71c25dea
TT
18656 {
18657 case DW_TAG_compile_unit:
95554aad 18658 case DW_TAG_partial_unit:
348e048f 18659 case DW_TAG_type_unit:
71c25dea
TT
18660 /* Compilation units have a DW_AT_name that is a filename, not
18661 a source language identifier. */
18662 case DW_TAG_enumeration_type:
18663 case DW_TAG_enumerator:
18664 /* These tags always have simple identifiers already; no need
18665 to canonicalize them. */
48fbe735 18666 name = DW_STRING (&attr);
71c25dea
TT
18667 break;
18668 default:
48fbe735
YQ
18669 {
18670 struct objfile *objfile = dwarf2_per_objfile->objfile;
18671
18672 name
18673 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
18674 &objfile->per_bfd->storage_obstack);
18675 }
71c25dea
TT
18676 break;
18677 }
c906108c 18678 break;
31ef98ae 18679 case DW_AT_linkage_name:
c906108c 18680 case DW_AT_MIPS_linkage_name:
31ef98ae
TT
18681 /* Note that both forms of linkage name might appear. We
18682 assume they will be the same, and we only store the last
18683 one we see. */
94af9270 18684 if (cu->language == language_ada)
48fbe735
YQ
18685 name = DW_STRING (&attr);
18686 linkage_name = DW_STRING (&attr);
c906108c
SS
18687 break;
18688 case DW_AT_low_pc:
18689 has_low_pc_attr = 1;
48fbe735 18690 lowpc = attr_value_as_address (&attr);
c906108c
SS
18691 break;
18692 case DW_AT_high_pc:
18693 has_high_pc_attr = 1;
48fbe735 18694 highpc = attr_value_as_address (&attr);
31aa7e4e
JB
18695 if (cu->header.version >= 4 && attr_form_is_constant (&attr))
18696 high_pc_relative = 1;
c906108c
SS
18697 break;
18698 case DW_AT_location:
0963b4bd 18699 /* Support the .debug_loc offsets. */
8e19ed76
PS
18700 if (attr_form_is_block (&attr))
18701 {
48fbe735 18702 d.locdesc = DW_BLOCK (&attr);
8e19ed76 18703 }
3690dd37 18704 else if (attr_form_is_section_offset (&attr))
8e19ed76 18705 {
4d3c2250 18706 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
18707 }
18708 else
18709 {
4d3c2250
KB
18710 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
18711 "partial symbol information");
8e19ed76 18712 }
c906108c 18713 break;
c906108c 18714 case DW_AT_external:
48fbe735 18715 is_external = DW_UNSND (&attr);
c906108c
SS
18716 break;
18717 case DW_AT_declaration:
48fbe735 18718 is_declaration = DW_UNSND (&attr);
c906108c
SS
18719 break;
18720 case DW_AT_type:
48fbe735 18721 has_type = 1;
c906108c
SS
18722 break;
18723 case DW_AT_abstract_origin:
18724 case DW_AT_specification:
72bf9492 18725 case DW_AT_extension:
48fbe735
YQ
18726 has_specification = 1;
18727 spec_offset = dwarf2_get_ref_die_offset (&attr);
18728 spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
36586728 18729 || cu->per_cu->is_dwz);
c906108c
SS
18730 break;
18731 case DW_AT_sibling:
18732 /* Ignore absolute siblings, they might point outside of
18733 the current compile unit. */
18734 if (attr.form == DW_FORM_ref_addr)
b98664d3 18735 complaint (_("ignoring absolute DW_AT_sibling"));
c906108c 18736 else
b9502d3f 18737 {
48fbe735 18738 const gdb_byte *buffer = reader->buffer;
9c541725
PA
18739 sect_offset off = dwarf2_get_ref_die_offset (&attr);
18740 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
b9502d3f
WN
18741
18742 if (sibling_ptr < info_ptr)
b98664d3 18743 complaint (_("DW_AT_sibling points backwards"));
22869d73
KS
18744 else if (sibling_ptr > reader->buffer_end)
18745 dwarf2_section_buffer_overflow_complaint (reader->die_section);
b9502d3f 18746 else
48fbe735 18747 sibling = sibling_ptr;
b9502d3f 18748 }
c906108c 18749 break;
fa4028e9 18750 case DW_AT_byte_size:
48fbe735 18751 has_byte_size = 1;
fa4028e9 18752 break;
ff908ebf 18753 case DW_AT_const_value:
48fbe735 18754 has_const_value = 1;
ff908ebf 18755 break;
68511cec
CES
18756 case DW_AT_calling_convention:
18757 /* DWARF doesn't provide a way to identify a program's source-level
18758 entry point. DW_AT_calling_convention attributes are only meant
18759 to describe functions' calling conventions.
18760
18761 However, because it's a necessary piece of information in
0c1b455e
TT
18762 Fortran, and before DWARF 4 DW_CC_program was the only
18763 piece of debugging information whose definition refers to
18764 a 'main program' at all, several compilers marked Fortran
18765 main programs with DW_CC_program --- even when those
18766 functions use the standard calling conventions.
18767
18768 Although DWARF now specifies a way to provide this
18769 information, we support this practice for backward
18770 compatibility. */
68511cec 18771 if (DW_UNSND (&attr) == DW_CC_program
0c1b455e 18772 && cu->language == language_fortran)
48fbe735 18773 main_subprogram = 1;
68511cec 18774 break;
481860b3
GB
18775 case DW_AT_inline:
18776 if (DW_UNSND (&attr) == DW_INL_inlined
18777 || DW_UNSND (&attr) == DW_INL_declared_inlined)
48fbe735 18778 may_be_inlined = 1;
481860b3 18779 break;
95554aad
TT
18780
18781 case DW_AT_import:
48fbe735 18782 if (tag == DW_TAG_imported_unit)
36586728 18783 {
48fbe735
YQ
18784 d.sect_off = dwarf2_get_ref_die_offset (&attr);
18785 is_dwz = (attr.form == DW_FORM_GNU_ref_alt
36586728
TT
18786 || cu->per_cu->is_dwz);
18787 }
95554aad
TT
18788 break;
18789
0c1b455e 18790 case DW_AT_main_subprogram:
48fbe735 18791 main_subprogram = DW_UNSND (&attr);
0c1b455e
TT
18792 break;
18793
05caa1d2
TT
18794 case DW_AT_ranges:
18795 {
18796 /* It would be nice to reuse dwarf2_get_pc_bounds here,
18797 but that requires a full DIE, so instead we just
18798 reimplement it. */
18799 int need_ranges_base = tag != DW_TAG_compile_unit;
18800 unsigned int ranges_offset = (DW_UNSND (&attr)
18801 + (need_ranges_base
18802 ? cu->ranges_base
18803 : 0));
18804
18805 /* Value of the DW_AT_ranges attribute is the offset in the
18806 .debug_ranges section. */
18807 if (dwarf2_ranges_read (ranges_offset, &lowpc, &highpc, cu,
18808 nullptr))
18809 has_pc_info = 1;
18810 }
18811 break;
18812
c906108c
SS
18813 default:
18814 break;
18815 }
18816 }
18817
91da1414 18818 if (high_pc_relative)
48fbe735 18819 highpc += lowpc;
91da1414 18820
9373cf26
JK
18821 if (has_low_pc_attr && has_high_pc_attr)
18822 {
18823 /* When using the GNU linker, .gnu.linkonce. sections are used to
18824 eliminate duplicate copies of functions and vtables and such.
18825 The linker will arbitrarily choose one and discard the others.
18826 The AT_*_pc values for such functions refer to local labels in
18827 these sections. If the section from that file was discarded, the
18828 labels are not in the output, so the relocs get a value of 0.
18829 If this is a discarded function, mark the pc bounds as invalid,
18830 so that GDB will ignore it. */
48fbe735 18831 if (lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
9373cf26 18832 {
48fbe735 18833 struct objfile *objfile = dwarf2_per_objfile->objfile;
bb5ed363 18834 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9373cf26 18835
b98664d3 18836 complaint (_("DW_AT_low_pc %s is zero "
9d8780f0 18837 "for DIE at %s [in module %s]"),
48fbe735
YQ
18838 paddress (gdbarch, lowpc),
18839 sect_offset_str (sect_off),
9d8780f0 18840 objfile_name (objfile));
9373cf26
JK
18841 }
18842 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
48fbe735 18843 else if (lowpc >= highpc)
9373cf26 18844 {
48fbe735 18845 struct objfile *objfile = dwarf2_per_objfile->objfile;
bb5ed363 18846 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9373cf26 18847
b98664d3 18848 complaint (_("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
9d8780f0 18849 "for DIE at %s [in module %s]"),
48fbe735
YQ
18850 paddress (gdbarch, lowpc),
18851 paddress (gdbarch, highpc),
18852 sect_offset_str (sect_off),
9c541725 18853 objfile_name (objfile));
9373cf26
JK
18854 }
18855 else
48fbe735 18856 has_pc_info = 1;
9373cf26 18857 }
85cbf3d3 18858
c906108c
SS
18859 return info_ptr;
18860}
18861
72bf9492
DJ
18862/* Find a cached partial DIE at OFFSET in CU. */
18863
d590ff25
YQ
18864struct partial_die_info *
18865dwarf2_cu::find_partial_die (sect_offset sect_off)
72bf9492
DJ
18866{
18867 struct partial_die_info *lookup_die = NULL;
6f06d47b 18868 struct partial_die_info part_die (sect_off);
72bf9492 18869
9a3c8263 18870 lookup_die = ((struct partial_die_info *)
d590ff25 18871 htab_find_with_hash (partial_dies, &part_die,
9c541725 18872 to_underlying (sect_off)));
72bf9492 18873
72bf9492
DJ
18874 return lookup_die;
18875}
18876
348e048f
DE
18877/* Find a partial DIE at OFFSET, which may or may not be in CU,
18878 except in the case of .debug_types DIEs which do not reference
18879 outside their CU (they do however referencing other types via
55f1336d 18880 DW_FORM_ref_sig8). */
72bf9492
DJ
18881
18882static struct partial_die_info *
9c541725 18883find_partial_die (sect_offset sect_off, int offset_in_dwz, struct dwarf2_cu *cu)
72bf9492 18884{
518817b3
SM
18885 struct dwarf2_per_objfile *dwarf2_per_objfile
18886 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 18887 struct objfile *objfile = dwarf2_per_objfile->objfile;
5afb4e99
DJ
18888 struct dwarf2_per_cu_data *per_cu = NULL;
18889 struct partial_die_info *pd = NULL;
72bf9492 18890
36586728 18891 if (offset_in_dwz == cu->per_cu->is_dwz
9c541725 18892 && offset_in_cu_p (&cu->header, sect_off))
5afb4e99 18893 {
d590ff25 18894 pd = cu->find_partial_die (sect_off);
5afb4e99
DJ
18895 if (pd != NULL)
18896 return pd;
0d99eb77
DE
18897 /* We missed recording what we needed.
18898 Load all dies and try again. */
18899 per_cu = cu->per_cu;
5afb4e99 18900 }
0d99eb77
DE
18901 else
18902 {
18903 /* TUs don't reference other CUs/TUs (except via type signatures). */
3019eac3 18904 if (cu->per_cu->is_debug_types)
0d99eb77 18905 {
9d8780f0
SM
18906 error (_("Dwarf Error: Type Unit at offset %s contains"
18907 " external reference to offset %s [in module %s].\n"),
18908 sect_offset_str (cu->header.sect_off), sect_offset_str (sect_off),
0d99eb77
DE
18909 bfd_get_filename (objfile->obfd));
18910 }
9c541725 18911 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
ed2dc618 18912 dwarf2_per_objfile);
72bf9492 18913
0d99eb77
DE
18914 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
18915 load_partial_comp_unit (per_cu);
ae038cb0 18916
0d99eb77 18917 per_cu->cu->last_used = 0;
d590ff25 18918 pd = per_cu->cu->find_partial_die (sect_off);
0d99eb77 18919 }
5afb4e99 18920
dee91e82
DE
18921 /* If we didn't find it, and not all dies have been loaded,
18922 load them all and try again. */
18923
5afb4e99
DJ
18924 if (pd == NULL && per_cu->load_all_dies == 0)
18925 {
5afb4e99 18926 per_cu->load_all_dies = 1;
fd820528
DE
18927
18928 /* This is nasty. When we reread the DIEs, somewhere up the call chain
18929 THIS_CU->cu may already be in use. So we can't just free it and
18930 replace its DIEs with the ones we read in. Instead, we leave those
18931 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
18932 and clobber THIS_CU->cu->partial_dies with the hash table for the new
18933 set. */
dee91e82 18934 load_partial_comp_unit (per_cu);
5afb4e99 18935
d590ff25 18936 pd = per_cu->cu->find_partial_die (sect_off);
5afb4e99
DJ
18937 }
18938
18939 if (pd == NULL)
18940 internal_error (__FILE__, __LINE__,
9d8780f0 18941 _("could not find partial DIE %s "
3e43a32a 18942 "in cache [from module %s]\n"),
9d8780f0 18943 sect_offset_str (sect_off), bfd_get_filename (objfile->obfd));
5afb4e99 18944 return pd;
72bf9492
DJ
18945}
18946
abc72ce4
DE
18947/* See if we can figure out if the class lives in a namespace. We do
18948 this by looking for a member function; its demangled name will
18949 contain namespace info, if there is any. */
18950
18951static void
18952guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
18953 struct dwarf2_cu *cu)
18954{
18955 /* NOTE: carlton/2003-10-07: Getting the info this way changes
18956 what template types look like, because the demangler
18957 frequently doesn't give the same name as the debug info. We
18958 could fix this by only using the demangled name to get the
18959 prefix (but see comment in read_structure_type). */
18960
18961 struct partial_die_info *real_pdi;
18962 struct partial_die_info *child_pdi;
18963
18964 /* If this DIE (this DIE's specification, if any) has a parent, then
18965 we should not do this. We'll prepend the parent's fully qualified
18966 name when we create the partial symbol. */
18967
18968 real_pdi = struct_pdi;
18969 while (real_pdi->has_specification)
36586728
TT
18970 real_pdi = find_partial_die (real_pdi->spec_offset,
18971 real_pdi->spec_is_dwz, cu);
abc72ce4
DE
18972
18973 if (real_pdi->die_parent != NULL)
18974 return;
18975
18976 for (child_pdi = struct_pdi->die_child;
18977 child_pdi != NULL;
18978 child_pdi = child_pdi->die_sibling)
18979 {
18980 if (child_pdi->tag == DW_TAG_subprogram
18981 && child_pdi->linkage_name != NULL)
18982 {
18983 char *actual_class_name
18984 = language_class_name_from_physname (cu->language_defn,
18985 child_pdi->linkage_name);
18986 if (actual_class_name != NULL)
18987 {
518817b3 18988 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
abc72ce4 18989 struct_pdi->name
224c3ddb 18990 = ((const char *)
e3b94546 18991 obstack_copy0 (&objfile->per_bfd->storage_obstack,
224c3ddb
SM
18992 actual_class_name,
18993 strlen (actual_class_name)));
abc72ce4
DE
18994 xfree (actual_class_name);
18995 }
18996 break;
18997 }
18998 }
18999}
19000
52356b79
YQ
19001void
19002partial_die_info::fixup (struct dwarf2_cu *cu)
72bf9492 19003{
abc72ce4
DE
19004 /* Once we've fixed up a die, there's no point in doing so again.
19005 This also avoids a memory leak if we were to call
19006 guess_partial_die_structure_name multiple times. */
52356b79 19007 if (fixup_called)
abc72ce4
DE
19008 return;
19009
72bf9492
DJ
19010 /* If we found a reference attribute and the DIE has no name, try
19011 to find a name in the referred to DIE. */
19012
52356b79 19013 if (name == NULL && has_specification)
72bf9492
DJ
19014 {
19015 struct partial_die_info *spec_die;
72bf9492 19016
52356b79 19017 spec_die = find_partial_die (spec_offset, spec_is_dwz, cu);
72bf9492 19018
52356b79 19019 spec_die->fixup (cu);
72bf9492
DJ
19020
19021 if (spec_die->name)
19022 {
52356b79 19023 name = spec_die->name;
72bf9492
DJ
19024
19025 /* Copy DW_AT_external attribute if it is set. */
19026 if (spec_die->is_external)
52356b79 19027 is_external = spec_die->is_external;
72bf9492
DJ
19028 }
19029 }
19030
19031 /* Set default names for some unnamed DIEs. */
72bf9492 19032
52356b79
YQ
19033 if (name == NULL && tag == DW_TAG_namespace)
19034 name = CP_ANONYMOUS_NAMESPACE_STR;
72bf9492 19035
abc72ce4
DE
19036 /* If there is no parent die to provide a namespace, and there are
19037 children, see if we can determine the namespace from their linkage
122d1940 19038 name. */
abc72ce4 19039 if (cu->language == language_cplus
518817b3
SM
19040 && !VEC_empty (dwarf2_section_info_def,
19041 cu->per_cu->dwarf2_per_objfile->types)
52356b79
YQ
19042 && die_parent == NULL
19043 && has_children
19044 && (tag == DW_TAG_class_type
19045 || tag == DW_TAG_structure_type
19046 || tag == DW_TAG_union_type))
19047 guess_partial_die_structure_name (this, cu);
abc72ce4 19048
53832f31
TT
19049 /* GCC might emit a nameless struct or union that has a linkage
19050 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
52356b79
YQ
19051 if (name == NULL
19052 && (tag == DW_TAG_class_type
19053 || tag == DW_TAG_interface_type
19054 || tag == DW_TAG_structure_type
19055 || tag == DW_TAG_union_type)
19056 && linkage_name != NULL)
53832f31
TT
19057 {
19058 char *demangled;
19059
52356b79 19060 demangled = gdb_demangle (linkage_name, DMGL_TYPES);
53832f31
TT
19061 if (demangled)
19062 {
96408a79
SA
19063 const char *base;
19064
19065 /* Strip any leading namespaces/classes, keep only the base name.
19066 DW_AT_name for named DIEs does not contain the prefixes. */
19067 base = strrchr (demangled, ':');
19068 if (base && base > demangled && base[-1] == ':')
19069 base++;
19070 else
19071 base = demangled;
19072
518817b3 19073 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
52356b79 19074 name
224c3ddb 19075 = ((const char *)
e3b94546 19076 obstack_copy0 (&objfile->per_bfd->storage_obstack,
224c3ddb 19077 base, strlen (base)));
53832f31
TT
19078 xfree (demangled);
19079 }
19080 }
19081
52356b79 19082 fixup_called = 1;
72bf9492
DJ
19083}
19084
a8329558 19085/* Read an attribute value described by an attribute form. */
c906108c 19086
d521ce57 19087static const gdb_byte *
dee91e82
DE
19088read_attribute_value (const struct die_reader_specs *reader,
19089 struct attribute *attr, unsigned form,
43988095 19090 LONGEST implicit_const, const gdb_byte *info_ptr)
c906108c 19091{
dee91e82 19092 struct dwarf2_cu *cu = reader->cu;
518817b3
SM
19093 struct dwarf2_per_objfile *dwarf2_per_objfile
19094 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 19095 struct objfile *objfile = dwarf2_per_objfile->objfile;
3e29f34a 19096 struct gdbarch *gdbarch = get_objfile_arch (objfile);
dee91e82 19097 bfd *abfd = reader->abfd;
e7c27a73 19098 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
19099 unsigned int bytes_read;
19100 struct dwarf_block *blk;
19101
aead7601 19102 attr->form = (enum dwarf_form) form;
a8329558 19103 switch (form)
c906108c 19104 {
c906108c 19105 case DW_FORM_ref_addr:
ae411497 19106 if (cu->header.version == 2)
4568ecf9 19107 DW_UNSND (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
ae411497 19108 else
4568ecf9
DE
19109 DW_UNSND (attr) = read_offset (abfd, info_ptr,
19110 &cu->header, &bytes_read);
ae411497
TT
19111 info_ptr += bytes_read;
19112 break;
36586728
TT
19113 case DW_FORM_GNU_ref_alt:
19114 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
19115 info_ptr += bytes_read;
19116 break;
ae411497 19117 case DW_FORM_addr:
e7c27a73 19118 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
3e29f34a 19119 DW_ADDR (attr) = gdbarch_adjust_dwarf2_addr (gdbarch, DW_ADDR (attr));
107d2387 19120 info_ptr += bytes_read;
c906108c
SS
19121 break;
19122 case DW_FORM_block2:
7b5a2f43 19123 blk = dwarf_alloc_block (cu);
c906108c
SS
19124 blk->size = read_2_bytes (abfd, info_ptr);
19125 info_ptr += 2;
19126 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19127 info_ptr += blk->size;
19128 DW_BLOCK (attr) = blk;
19129 break;
19130 case DW_FORM_block4:
7b5a2f43 19131 blk = dwarf_alloc_block (cu);
c906108c
SS
19132 blk->size = read_4_bytes (abfd, info_ptr);
19133 info_ptr += 4;
19134 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19135 info_ptr += blk->size;
19136 DW_BLOCK (attr) = blk;
19137 break;
19138 case DW_FORM_data2:
19139 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
19140 info_ptr += 2;
19141 break;
19142 case DW_FORM_data4:
19143 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
19144 info_ptr += 4;
19145 break;
19146 case DW_FORM_data8:
19147 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
19148 info_ptr += 8;
19149 break;
0224619f
JK
19150 case DW_FORM_data16:
19151 blk = dwarf_alloc_block (cu);
19152 blk->size = 16;
19153 blk->data = read_n_bytes (abfd, info_ptr, 16);
19154 info_ptr += 16;
19155 DW_BLOCK (attr) = blk;
19156 break;
2dc7f7b3
TT
19157 case DW_FORM_sec_offset:
19158 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
19159 info_ptr += bytes_read;
19160 break;
c906108c 19161 case DW_FORM_string:
9b1c24c8 19162 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
8285870a 19163 DW_STRING_IS_CANONICAL (attr) = 0;
c906108c
SS
19164 info_ptr += bytes_read;
19165 break;
4bdf3d34 19166 case DW_FORM_strp:
36586728
TT
19167 if (!cu->per_cu->is_dwz)
19168 {
ed2dc618
SM
19169 DW_STRING (attr) = read_indirect_string (dwarf2_per_objfile,
19170 abfd, info_ptr, cu_header,
36586728
TT
19171 &bytes_read);
19172 DW_STRING_IS_CANONICAL (attr) = 0;
19173 info_ptr += bytes_read;
19174 break;
19175 }
19176 /* FALLTHROUGH */
43988095
JK
19177 case DW_FORM_line_strp:
19178 if (!cu->per_cu->is_dwz)
19179 {
ed2dc618
SM
19180 DW_STRING (attr) = read_indirect_line_string (dwarf2_per_objfile,
19181 abfd, info_ptr,
43988095
JK
19182 cu_header, &bytes_read);
19183 DW_STRING_IS_CANONICAL (attr) = 0;
19184 info_ptr += bytes_read;
19185 break;
19186 }
19187 /* FALLTHROUGH */
36586728
TT
19188 case DW_FORM_GNU_strp_alt:
19189 {
ed2dc618 19190 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
36586728
TT
19191 LONGEST str_offset = read_offset (abfd, info_ptr, cu_header,
19192 &bytes_read);
19193
ed2dc618
SM
19194 DW_STRING (attr) = read_indirect_string_from_dwz (objfile,
19195 dwz, str_offset);
36586728
TT
19196 DW_STRING_IS_CANONICAL (attr) = 0;
19197 info_ptr += bytes_read;
19198 }
4bdf3d34 19199 break;
2dc7f7b3 19200 case DW_FORM_exprloc:
c906108c 19201 case DW_FORM_block:
7b5a2f43 19202 blk = dwarf_alloc_block (cu);
c906108c
SS
19203 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19204 info_ptr += bytes_read;
19205 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19206 info_ptr += blk->size;
19207 DW_BLOCK (attr) = blk;
19208 break;
19209 case DW_FORM_block1:
7b5a2f43 19210 blk = dwarf_alloc_block (cu);
c906108c
SS
19211 blk->size = read_1_byte (abfd, info_ptr);
19212 info_ptr += 1;
19213 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19214 info_ptr += blk->size;
19215 DW_BLOCK (attr) = blk;
19216 break;
19217 case DW_FORM_data1:
19218 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
19219 info_ptr += 1;
19220 break;
19221 case DW_FORM_flag:
19222 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
19223 info_ptr += 1;
19224 break;
2dc7f7b3
TT
19225 case DW_FORM_flag_present:
19226 DW_UNSND (attr) = 1;
19227 break;
c906108c
SS
19228 case DW_FORM_sdata:
19229 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
19230 info_ptr += bytes_read;
19231 break;
19232 case DW_FORM_udata:
19233 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19234 info_ptr += bytes_read;
19235 break;
19236 case DW_FORM_ref1:
9c541725 19237 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 19238 + read_1_byte (abfd, info_ptr));
c906108c
SS
19239 info_ptr += 1;
19240 break;
19241 case DW_FORM_ref2:
9c541725 19242 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 19243 + read_2_bytes (abfd, info_ptr));
c906108c
SS
19244 info_ptr += 2;
19245 break;
19246 case DW_FORM_ref4:
9c541725 19247 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 19248 + read_4_bytes (abfd, info_ptr));
c906108c
SS
19249 info_ptr += 4;
19250 break;
613e1657 19251 case DW_FORM_ref8:
9c541725 19252 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 19253 + read_8_bytes (abfd, info_ptr));
613e1657
KB
19254 info_ptr += 8;
19255 break;
55f1336d 19256 case DW_FORM_ref_sig8:
ac9ec31b 19257 DW_SIGNATURE (attr) = read_8_bytes (abfd, info_ptr);
348e048f
DE
19258 info_ptr += 8;
19259 break;
c906108c 19260 case DW_FORM_ref_udata:
9c541725 19261 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 19262 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
c906108c
SS
19263 info_ptr += bytes_read;
19264 break;
c906108c 19265 case DW_FORM_indirect:
a8329558
KW
19266 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19267 info_ptr += bytes_read;
43988095
JK
19268 if (form == DW_FORM_implicit_const)
19269 {
19270 implicit_const = read_signed_leb128 (abfd, info_ptr, &bytes_read);
19271 info_ptr += bytes_read;
19272 }
19273 info_ptr = read_attribute_value (reader, attr, form, implicit_const,
19274 info_ptr);
19275 break;
19276 case DW_FORM_implicit_const:
19277 DW_SND (attr) = implicit_const;
a8329558 19278 break;
3019eac3
DE
19279 case DW_FORM_GNU_addr_index:
19280 if (reader->dwo_file == NULL)
19281 {
19282 /* For now flag a hard error.
19283 Later we can turn this into a complaint. */
19284 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
19285 dwarf_form_name (form),
19286 bfd_get_filename (abfd));
19287 }
19288 DW_ADDR (attr) = read_addr_index_from_leb128 (cu, info_ptr, &bytes_read);
19289 info_ptr += bytes_read;
19290 break;
19291 case DW_FORM_GNU_str_index:
19292 if (reader->dwo_file == NULL)
19293 {
19294 /* For now flag a hard error.
19295 Later we can turn this into a complaint if warranted. */
19296 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
19297 dwarf_form_name (form),
19298 bfd_get_filename (abfd));
19299 }
19300 {
19301 ULONGEST str_index =
19302 read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19303
342587c4 19304 DW_STRING (attr) = read_str_index (reader, str_index);
3019eac3
DE
19305 DW_STRING_IS_CANONICAL (attr) = 0;
19306 info_ptr += bytes_read;
19307 }
19308 break;
c906108c 19309 default:
8a3fe4f8 19310 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
659b0389
ML
19311 dwarf_form_name (form),
19312 bfd_get_filename (abfd));
c906108c 19313 }
28e94949 19314
36586728 19315 /* Super hack. */
7771576e 19316 if (cu->per_cu->is_dwz && attr_form_is_ref (attr))
36586728
TT
19317 attr->form = DW_FORM_GNU_ref_alt;
19318
28e94949
JB
19319 /* We have seen instances where the compiler tried to emit a byte
19320 size attribute of -1 which ended up being encoded as an unsigned
19321 0xffffffff. Although 0xffffffff is technically a valid size value,
19322 an object of this size seems pretty unlikely so we can relatively
19323 safely treat these cases as if the size attribute was invalid and
19324 treat them as zero by default. */
19325 if (attr->name == DW_AT_byte_size
19326 && form == DW_FORM_data4
19327 && DW_UNSND (attr) >= 0xffffffff)
01c66ae6
JB
19328 {
19329 complaint
b98664d3 19330 (_("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
43bbcdc2 19331 hex_string (DW_UNSND (attr)));
01c66ae6
JB
19332 DW_UNSND (attr) = 0;
19333 }
28e94949 19334
c906108c
SS
19335 return info_ptr;
19336}
19337
a8329558
KW
19338/* Read an attribute described by an abbreviated attribute. */
19339
d521ce57 19340static const gdb_byte *
dee91e82
DE
19341read_attribute (const struct die_reader_specs *reader,
19342 struct attribute *attr, struct attr_abbrev *abbrev,
d521ce57 19343 const gdb_byte *info_ptr)
a8329558
KW
19344{
19345 attr->name = abbrev->name;
43988095
JK
19346 return read_attribute_value (reader, attr, abbrev->form,
19347 abbrev->implicit_const, info_ptr);
a8329558
KW
19348}
19349
0963b4bd 19350/* Read dwarf information from a buffer. */
c906108c
SS
19351
19352static unsigned int
a1855c1d 19353read_1_byte (bfd *abfd, const gdb_byte *buf)
c906108c 19354{
fe1b8b76 19355 return bfd_get_8 (abfd, buf);
c906108c
SS
19356}
19357
19358static int
a1855c1d 19359read_1_signed_byte (bfd *abfd, const gdb_byte *buf)
c906108c 19360{
fe1b8b76 19361 return bfd_get_signed_8 (abfd, buf);
c906108c
SS
19362}
19363
19364static unsigned int
a1855c1d 19365read_2_bytes (bfd *abfd, const gdb_byte *buf)
c906108c 19366{
fe1b8b76 19367 return bfd_get_16 (abfd, buf);
c906108c
SS
19368}
19369
21ae7a4d 19370static int
a1855c1d 19371read_2_signed_bytes (bfd *abfd, const gdb_byte *buf)
21ae7a4d
JK
19372{
19373 return bfd_get_signed_16 (abfd, buf);
19374}
19375
c906108c 19376static unsigned int
a1855c1d 19377read_4_bytes (bfd *abfd, const gdb_byte *buf)
c906108c 19378{
fe1b8b76 19379 return bfd_get_32 (abfd, buf);
c906108c
SS
19380}
19381
21ae7a4d 19382static int
a1855c1d 19383read_4_signed_bytes (bfd *abfd, const gdb_byte *buf)
21ae7a4d
JK
19384{
19385 return bfd_get_signed_32 (abfd, buf);
19386}
19387
93311388 19388static ULONGEST
a1855c1d 19389read_8_bytes (bfd *abfd, const gdb_byte *buf)
c906108c 19390{
fe1b8b76 19391 return bfd_get_64 (abfd, buf);
c906108c
SS
19392}
19393
19394static CORE_ADDR
d521ce57 19395read_address (bfd *abfd, const gdb_byte *buf, struct dwarf2_cu *cu,
891d2f0b 19396 unsigned int *bytes_read)
c906108c 19397{
e7c27a73 19398 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
19399 CORE_ADDR retval = 0;
19400
107d2387 19401 if (cu_header->signed_addr_p)
c906108c 19402 {
107d2387
AC
19403 switch (cu_header->addr_size)
19404 {
19405 case 2:
fe1b8b76 19406 retval = bfd_get_signed_16 (abfd, buf);
107d2387
AC
19407 break;
19408 case 4:
fe1b8b76 19409 retval = bfd_get_signed_32 (abfd, buf);
107d2387
AC
19410 break;
19411 case 8:
fe1b8b76 19412 retval = bfd_get_signed_64 (abfd, buf);
107d2387
AC
19413 break;
19414 default:
8e65ff28 19415 internal_error (__FILE__, __LINE__,
e2e0b3e5 19416 _("read_address: bad switch, signed [in module %s]"),
659b0389 19417 bfd_get_filename (abfd));
107d2387
AC
19418 }
19419 }
19420 else
19421 {
19422 switch (cu_header->addr_size)
19423 {
19424 case 2:
fe1b8b76 19425 retval = bfd_get_16 (abfd, buf);
107d2387
AC
19426 break;
19427 case 4:
fe1b8b76 19428 retval = bfd_get_32 (abfd, buf);
107d2387
AC
19429 break;
19430 case 8:
fe1b8b76 19431 retval = bfd_get_64 (abfd, buf);
107d2387
AC
19432 break;
19433 default:
8e65ff28 19434 internal_error (__FILE__, __LINE__,
a73c6dcd
MS
19435 _("read_address: bad switch, "
19436 "unsigned [in module %s]"),
659b0389 19437 bfd_get_filename (abfd));
107d2387 19438 }
c906108c 19439 }
64367e0a 19440
107d2387
AC
19441 *bytes_read = cu_header->addr_size;
19442 return retval;
c906108c
SS
19443}
19444
f7ef9339 19445/* Read the initial length from a section. The (draft) DWARF 3
613e1657
KB
19446 specification allows the initial length to take up either 4 bytes
19447 or 12 bytes. If the first 4 bytes are 0xffffffff, then the next 8
19448 bytes describe the length and all offsets will be 8 bytes in length
19449 instead of 4.
19450
f7ef9339
KB
19451 An older, non-standard 64-bit format is also handled by this
19452 function. The older format in question stores the initial length
19453 as an 8-byte quantity without an escape value. Lengths greater
19454 than 2^32 aren't very common which means that the initial 4 bytes
19455 is almost always zero. Since a length value of zero doesn't make
19456 sense for the 32-bit format, this initial zero can be considered to
19457 be an escape value which indicates the presence of the older 64-bit
19458 format. As written, the code can't detect (old format) lengths
917c78fc
MK
19459 greater than 4GB. If it becomes necessary to handle lengths
19460 somewhat larger than 4GB, we could allow other small values (such
19461 as the non-sensical values of 1, 2, and 3) to also be used as
19462 escape values indicating the presence of the old format.
f7ef9339 19463
917c78fc
MK
19464 The value returned via bytes_read should be used to increment the
19465 relevant pointer after calling read_initial_length().
c764a876 19466
613e1657
KB
19467 [ Note: read_initial_length() and read_offset() are based on the
19468 document entitled "DWARF Debugging Information Format", revision
f7ef9339 19469 3, draft 8, dated November 19, 2001. This document was obtained
613e1657
KB
19470 from:
19471
f7ef9339 19472 http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
6e70227d 19473
613e1657
KB
19474 This document is only a draft and is subject to change. (So beware.)
19475
f7ef9339 19476 Details regarding the older, non-standard 64-bit format were
917c78fc
MK
19477 determined empirically by examining 64-bit ELF files produced by
19478 the SGI toolchain on an IRIX 6.5 machine.
f7ef9339
KB
19479
19480 - Kevin, July 16, 2002
613e1657
KB
19481 ] */
19482
19483static LONGEST
d521ce57 19484read_initial_length (bfd *abfd, const gdb_byte *buf, unsigned int *bytes_read)
613e1657 19485{
fe1b8b76 19486 LONGEST length = bfd_get_32 (abfd, buf);
613e1657 19487
dd373385 19488 if (length == 0xffffffff)
613e1657 19489 {
fe1b8b76 19490 length = bfd_get_64 (abfd, buf + 4);
613e1657 19491 *bytes_read = 12;
613e1657 19492 }
dd373385 19493 else if (length == 0)
f7ef9339 19494 {
dd373385 19495 /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX. */
fe1b8b76 19496 length = bfd_get_64 (abfd, buf);
f7ef9339 19497 *bytes_read = 8;
f7ef9339 19498 }
613e1657
KB
19499 else
19500 {
19501 *bytes_read = 4;
613e1657
KB
19502 }
19503
c764a876
DE
19504 return length;
19505}
dd373385 19506
c764a876
DE
19507/* Cover function for read_initial_length.
19508 Returns the length of the object at BUF, and stores the size of the
19509 initial length in *BYTES_READ and stores the size that offsets will be in
19510 *OFFSET_SIZE.
19511 If the initial length size is not equivalent to that specified in
19512 CU_HEADER then issue a complaint.
19513 This is useful when reading non-comp-unit headers. */
dd373385 19514
c764a876 19515static LONGEST
d521ce57 19516read_checked_initial_length_and_offset (bfd *abfd, const gdb_byte *buf,
c764a876
DE
19517 const struct comp_unit_head *cu_header,
19518 unsigned int *bytes_read,
19519 unsigned int *offset_size)
19520{
19521 LONGEST length = read_initial_length (abfd, buf, bytes_read);
19522
19523 gdb_assert (cu_header->initial_length_size == 4
19524 || cu_header->initial_length_size == 8
19525 || cu_header->initial_length_size == 12);
19526
19527 if (cu_header->initial_length_size != *bytes_read)
b98664d3 19528 complaint (_("intermixed 32-bit and 64-bit DWARF sections"));
dd373385 19529
c764a876 19530 *offset_size = (*bytes_read == 4) ? 4 : 8;
dd373385 19531 return length;
613e1657
KB
19532}
19533
19534/* Read an offset from the data stream. The size of the offset is
917c78fc 19535 given by cu_header->offset_size. */
613e1657
KB
19536
19537static LONGEST
d521ce57
TT
19538read_offset (bfd *abfd, const gdb_byte *buf,
19539 const struct comp_unit_head *cu_header,
891d2f0b 19540 unsigned int *bytes_read)
c764a876
DE
19541{
19542 LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
9a619af0 19543
c764a876
DE
19544 *bytes_read = cu_header->offset_size;
19545 return offset;
19546}
19547
19548/* Read an offset from the data stream. */
19549
19550static LONGEST
d521ce57 19551read_offset_1 (bfd *abfd, const gdb_byte *buf, unsigned int offset_size)
613e1657
KB
19552{
19553 LONGEST retval = 0;
19554
c764a876 19555 switch (offset_size)
613e1657
KB
19556 {
19557 case 4:
fe1b8b76 19558 retval = bfd_get_32 (abfd, buf);
613e1657
KB
19559 break;
19560 case 8:
fe1b8b76 19561 retval = bfd_get_64 (abfd, buf);
613e1657
KB
19562 break;
19563 default:
8e65ff28 19564 internal_error (__FILE__, __LINE__,
c764a876 19565 _("read_offset_1: bad switch [in module %s]"),
659b0389 19566 bfd_get_filename (abfd));
613e1657
KB
19567 }
19568
917c78fc 19569 return retval;
613e1657
KB
19570}
19571
d521ce57
TT
19572static const gdb_byte *
19573read_n_bytes (bfd *abfd, const gdb_byte *buf, unsigned int size)
c906108c
SS
19574{
19575 /* If the size of a host char is 8 bits, we can return a pointer
19576 to the buffer, otherwise we have to copy the data to a buffer
19577 allocated on the temporary obstack. */
4bdf3d34 19578 gdb_assert (HOST_CHAR_BIT == 8);
c906108c 19579 return buf;
c906108c
SS
19580}
19581
d521ce57
TT
19582static const char *
19583read_direct_string (bfd *abfd, const gdb_byte *buf,
19584 unsigned int *bytes_read_ptr)
c906108c
SS
19585{
19586 /* If the size of a host char is 8 bits, we can return a pointer
19587 to the string, otherwise we have to copy the string to a buffer
19588 allocated on the temporary obstack. */
4bdf3d34 19589 gdb_assert (HOST_CHAR_BIT == 8);
c906108c
SS
19590 if (*buf == '\0')
19591 {
19592 *bytes_read_ptr = 1;
19593 return NULL;
19594 }
d521ce57
TT
19595 *bytes_read_ptr = strlen ((const char *) buf) + 1;
19596 return (const char *) buf;
4bdf3d34
JJ
19597}
19598
43988095
JK
19599/* Return pointer to string at section SECT offset STR_OFFSET with error
19600 reporting strings FORM_NAME and SECT_NAME. */
19601
d521ce57 19602static const char *
ed2dc618
SM
19603read_indirect_string_at_offset_from (struct objfile *objfile,
19604 bfd *abfd, LONGEST str_offset,
43988095
JK
19605 struct dwarf2_section_info *sect,
19606 const char *form_name,
19607 const char *sect_name)
19608{
ed2dc618 19609 dwarf2_read_section (objfile, sect);
43988095
JK
19610 if (sect->buffer == NULL)
19611 error (_("%s used without %s section [in module %s]"),
19612 form_name, sect_name, bfd_get_filename (abfd));
19613 if (str_offset >= sect->size)
19614 error (_("%s pointing outside of %s section [in module %s]"),
19615 form_name, sect_name, bfd_get_filename (abfd));
4bdf3d34 19616 gdb_assert (HOST_CHAR_BIT == 8);
43988095 19617 if (sect->buffer[str_offset] == '\0')
4bdf3d34 19618 return NULL;
43988095
JK
19619 return (const char *) (sect->buffer + str_offset);
19620}
19621
19622/* Return pointer to string at .debug_str offset STR_OFFSET. */
19623
19624static const char *
ed2dc618
SM
19625read_indirect_string_at_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
19626 bfd *abfd, LONGEST str_offset)
43988095 19627{
ed2dc618
SM
19628 return read_indirect_string_at_offset_from (dwarf2_per_objfile->objfile,
19629 abfd, str_offset,
43988095
JK
19630 &dwarf2_per_objfile->str,
19631 "DW_FORM_strp", ".debug_str");
19632}
19633
19634/* Return pointer to string at .debug_line_str offset STR_OFFSET. */
19635
19636static const char *
ed2dc618
SM
19637read_indirect_line_string_at_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
19638 bfd *abfd, LONGEST str_offset)
43988095 19639{
ed2dc618
SM
19640 return read_indirect_string_at_offset_from (dwarf2_per_objfile->objfile,
19641 abfd, str_offset,
43988095
JK
19642 &dwarf2_per_objfile->line_str,
19643 "DW_FORM_line_strp",
19644 ".debug_line_str");
c906108c
SS
19645}
19646
36586728
TT
19647/* Read a string at offset STR_OFFSET in the .debug_str section from
19648 the .dwz file DWZ. Throw an error if the offset is too large. If
19649 the string consists of a single NUL byte, return NULL; otherwise
19650 return a pointer to the string. */
19651
d521ce57 19652static const char *
ed2dc618
SM
19653read_indirect_string_from_dwz (struct objfile *objfile, struct dwz_file *dwz,
19654 LONGEST str_offset)
36586728 19655{
ed2dc618 19656 dwarf2_read_section (objfile, &dwz->str);
36586728
TT
19657
19658 if (dwz->str.buffer == NULL)
19659 error (_("DW_FORM_GNU_strp_alt used without .debug_str "
19660 "section [in module %s]"),
19661 bfd_get_filename (dwz->dwz_bfd));
19662 if (str_offset >= dwz->str.size)
19663 error (_("DW_FORM_GNU_strp_alt pointing outside of "
19664 ".debug_str section [in module %s]"),
19665 bfd_get_filename (dwz->dwz_bfd));
19666 gdb_assert (HOST_CHAR_BIT == 8);
19667 if (dwz->str.buffer[str_offset] == '\0')
19668 return NULL;
d521ce57 19669 return (const char *) (dwz->str.buffer + str_offset);
36586728
TT
19670}
19671
43988095
JK
19672/* Return pointer to string at .debug_str offset as read from BUF.
19673 BUF is assumed to be in a compilation unit described by CU_HEADER.
19674 Return *BYTES_READ_PTR count of bytes read from BUF. */
19675
d521ce57 19676static const char *
ed2dc618
SM
19677read_indirect_string (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *abfd,
19678 const gdb_byte *buf,
cf2c3c16
TT
19679 const struct comp_unit_head *cu_header,
19680 unsigned int *bytes_read_ptr)
19681{
19682 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
19683
ed2dc618 19684 return read_indirect_string_at_offset (dwarf2_per_objfile, abfd, str_offset);
cf2c3c16
TT
19685}
19686
43988095
JK
19687/* Return pointer to string at .debug_line_str offset as read from BUF.
19688 BUF is assumed to be in a compilation unit described by CU_HEADER.
19689 Return *BYTES_READ_PTR count of bytes read from BUF. */
19690
19691static const char *
ed2dc618
SM
19692read_indirect_line_string (struct dwarf2_per_objfile *dwarf2_per_objfile,
19693 bfd *abfd, const gdb_byte *buf,
43988095
JK
19694 const struct comp_unit_head *cu_header,
19695 unsigned int *bytes_read_ptr)
19696{
19697 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
19698
ed2dc618
SM
19699 return read_indirect_line_string_at_offset (dwarf2_per_objfile, abfd,
19700 str_offset);
43988095
JK
19701}
19702
19703ULONGEST
d521ce57 19704read_unsigned_leb128 (bfd *abfd, const gdb_byte *buf,
43988095 19705 unsigned int *bytes_read_ptr)
c906108c 19706{
12df843f 19707 ULONGEST result;
ce5d95e1 19708 unsigned int num_read;
870f88f7 19709 int shift;
c906108c
SS
19710 unsigned char byte;
19711
19712 result = 0;
19713 shift = 0;
19714 num_read = 0;
c906108c
SS
19715 while (1)
19716 {
fe1b8b76 19717 byte = bfd_get_8 (abfd, buf);
c906108c
SS
19718 buf++;
19719 num_read++;
12df843f 19720 result |= ((ULONGEST) (byte & 127) << shift);
c906108c
SS
19721 if ((byte & 128) == 0)
19722 {
19723 break;
19724 }
19725 shift += 7;
19726 }
19727 *bytes_read_ptr = num_read;
19728 return result;
19729}
19730
12df843f 19731static LONGEST
d521ce57
TT
19732read_signed_leb128 (bfd *abfd, const gdb_byte *buf,
19733 unsigned int *bytes_read_ptr)
c906108c 19734{
4dd1b460 19735 ULONGEST result;
870f88f7 19736 int shift, num_read;
c906108c
SS
19737 unsigned char byte;
19738
19739 result = 0;
19740 shift = 0;
c906108c 19741 num_read = 0;
c906108c
SS
19742 while (1)
19743 {
fe1b8b76 19744 byte = bfd_get_8 (abfd, buf);
c906108c
SS
19745 buf++;
19746 num_read++;
4dd1b460 19747 result |= ((ULONGEST) (byte & 127) << shift);
c906108c
SS
19748 shift += 7;
19749 if ((byte & 128) == 0)
19750 {
19751 break;
19752 }
19753 }
77e0b926 19754 if ((shift < 8 * sizeof (result)) && (byte & 0x40))
4dd1b460 19755 result |= -(((ULONGEST) 1) << shift);
c906108c
SS
19756 *bytes_read_ptr = num_read;
19757 return result;
19758}
19759
3019eac3
DE
19760/* Given index ADDR_INDEX in .debug_addr, fetch the value.
19761 ADDR_BASE is the DW_AT_GNU_addr_base attribute or zero.
19762 ADDR_SIZE is the size of addresses from the CU header. */
19763
19764static CORE_ADDR
ed2dc618
SM
19765read_addr_index_1 (struct dwarf2_per_objfile *dwarf2_per_objfile,
19766 unsigned int addr_index, ULONGEST addr_base, int addr_size)
3019eac3
DE
19767{
19768 struct objfile *objfile = dwarf2_per_objfile->objfile;
19769 bfd *abfd = objfile->obfd;
19770 const gdb_byte *info_ptr;
19771
19772 dwarf2_read_section (objfile, &dwarf2_per_objfile->addr);
19773 if (dwarf2_per_objfile->addr.buffer == NULL)
19774 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
4262abfb 19775 objfile_name (objfile));
3019eac3
DE
19776 if (addr_base + addr_index * addr_size >= dwarf2_per_objfile->addr.size)
19777 error (_("DW_FORM_addr_index pointing outside of "
19778 ".debug_addr section [in module %s]"),
4262abfb 19779 objfile_name (objfile));
3019eac3
DE
19780 info_ptr = (dwarf2_per_objfile->addr.buffer
19781 + addr_base + addr_index * addr_size);
19782 if (addr_size == 4)
19783 return bfd_get_32 (abfd, info_ptr);
19784 else
19785 return bfd_get_64 (abfd, info_ptr);
19786}
19787
19788/* Given index ADDR_INDEX in .debug_addr, fetch the value. */
19789
19790static CORE_ADDR
19791read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
19792{
518817b3
SM
19793 return read_addr_index_1 (cu->per_cu->dwarf2_per_objfile, addr_index,
19794 cu->addr_base, cu->header.addr_size);
3019eac3
DE
19795}
19796
19797/* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
19798
19799static CORE_ADDR
d521ce57 19800read_addr_index_from_leb128 (struct dwarf2_cu *cu, const gdb_byte *info_ptr,
3019eac3
DE
19801 unsigned int *bytes_read)
19802{
518817b3 19803 bfd *abfd = cu->per_cu->dwarf2_per_objfile->objfile->obfd;
3019eac3
DE
19804 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
19805
19806 return read_addr_index (cu, addr_index);
19807}
19808
19809/* Data structure to pass results from dwarf2_read_addr_index_reader
19810 back to dwarf2_read_addr_index. */
19811
19812struct dwarf2_read_addr_index_data
19813{
19814 ULONGEST addr_base;
19815 int addr_size;
19816};
19817
19818/* die_reader_func for dwarf2_read_addr_index. */
19819
19820static void
19821dwarf2_read_addr_index_reader (const struct die_reader_specs *reader,
d521ce57 19822 const gdb_byte *info_ptr,
3019eac3
DE
19823 struct die_info *comp_unit_die,
19824 int has_children,
19825 void *data)
19826{
19827 struct dwarf2_cu *cu = reader->cu;
19828 struct dwarf2_read_addr_index_data *aidata =
19829 (struct dwarf2_read_addr_index_data *) data;
19830
19831 aidata->addr_base = cu->addr_base;
19832 aidata->addr_size = cu->header.addr_size;
19833}
19834
19835/* Given an index in .debug_addr, fetch the value.
19836 NOTE: This can be called during dwarf expression evaluation,
19837 long after the debug information has been read, and thus per_cu->cu
19838 may no longer exist. */
19839
19840CORE_ADDR
19841dwarf2_read_addr_index (struct dwarf2_per_cu_data *per_cu,
19842 unsigned int addr_index)
19843{
ed2dc618 19844 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
3019eac3
DE
19845 struct dwarf2_cu *cu = per_cu->cu;
19846 ULONGEST addr_base;
19847 int addr_size;
19848
3019eac3
DE
19849 /* We need addr_base and addr_size.
19850 If we don't have PER_CU->cu, we have to get it.
19851 Nasty, but the alternative is storing the needed info in PER_CU,
19852 which at this point doesn't seem justified: it's not clear how frequently
19853 it would get used and it would increase the size of every PER_CU.
19854 Entry points like dwarf2_per_cu_addr_size do a similar thing
19855 so we're not in uncharted territory here.
19856 Alas we need to be a bit more complicated as addr_base is contained
19857 in the DIE.
19858
19859 We don't need to read the entire CU(/TU).
19860 We just need the header and top level die.
a1b64ce1 19861
3019eac3 19862 IWBN to use the aging mechanism to let us lazily later discard the CU.
a1b64ce1 19863 For now we skip this optimization. */
3019eac3
DE
19864
19865 if (cu != NULL)
19866 {
19867 addr_base = cu->addr_base;
19868 addr_size = cu->header.addr_size;
19869 }
19870 else
19871 {
19872 struct dwarf2_read_addr_index_data aidata;
19873
a1b64ce1
DE
19874 /* Note: We can't use init_cutu_and_read_dies_simple here,
19875 we need addr_base. */
58f0c718 19876 init_cutu_and_read_dies (per_cu, NULL, 0, 0, false,
a1b64ce1 19877 dwarf2_read_addr_index_reader, &aidata);
3019eac3
DE
19878 addr_base = aidata.addr_base;
19879 addr_size = aidata.addr_size;
19880 }
19881
ed2dc618
SM
19882 return read_addr_index_1 (dwarf2_per_objfile, addr_index, addr_base,
19883 addr_size);
3019eac3
DE
19884}
19885
57d63ce2
DE
19886/* Given a DW_FORM_GNU_str_index, fetch the string.
19887 This is only used by the Fission support. */
3019eac3 19888
d521ce57 19889static const char *
342587c4 19890read_str_index (const struct die_reader_specs *reader, ULONGEST str_index)
3019eac3 19891{
ed2dc618 19892 struct dwarf2_cu *cu = reader->cu;
518817b3
SM
19893 struct dwarf2_per_objfile *dwarf2_per_objfile
19894 = cu->per_cu->dwarf2_per_objfile;
3019eac3 19895 struct objfile *objfile = dwarf2_per_objfile->objfile;
c5164cbc 19896 const char *objf_name = objfile_name (objfile);
3019eac3 19897 bfd *abfd = objfile->obfd;
73869dc2
DE
19898 struct dwarf2_section_info *str_section = &reader->dwo_file->sections.str;
19899 struct dwarf2_section_info *str_offsets_section =
19900 &reader->dwo_file->sections.str_offsets;
d521ce57 19901 const gdb_byte *info_ptr;
3019eac3 19902 ULONGEST str_offset;
57d63ce2 19903 static const char form_name[] = "DW_FORM_GNU_str_index";
3019eac3 19904
73869dc2
DE
19905 dwarf2_read_section (objfile, str_section);
19906 dwarf2_read_section (objfile, str_offsets_section);
19907 if (str_section->buffer == NULL)
57d63ce2 19908 error (_("%s used without .debug_str.dwo section"
9d8780f0
SM
19909 " in CU at offset %s [in module %s]"),
19910 form_name, sect_offset_str (cu->header.sect_off), objf_name);
73869dc2 19911 if (str_offsets_section->buffer == NULL)
57d63ce2 19912 error (_("%s used without .debug_str_offsets.dwo section"
9d8780f0
SM
19913 " in CU at offset %s [in module %s]"),
19914 form_name, sect_offset_str (cu->header.sect_off), objf_name);
73869dc2 19915 if (str_index * cu->header.offset_size >= str_offsets_section->size)
57d63ce2 19916 error (_("%s pointing outside of .debug_str_offsets.dwo"
9d8780f0
SM
19917 " section in CU at offset %s [in module %s]"),
19918 form_name, sect_offset_str (cu->header.sect_off), objf_name);
73869dc2 19919 info_ptr = (str_offsets_section->buffer
3019eac3
DE
19920 + str_index * cu->header.offset_size);
19921 if (cu->header.offset_size == 4)
19922 str_offset = bfd_get_32 (abfd, info_ptr);
19923 else
19924 str_offset = bfd_get_64 (abfd, info_ptr);
73869dc2 19925 if (str_offset >= str_section->size)
57d63ce2 19926 error (_("Offset from %s pointing outside of"
9d8780f0
SM
19927 " .debug_str.dwo section in CU at offset %s [in module %s]"),
19928 form_name, sect_offset_str (cu->header.sect_off), objf_name);
73869dc2 19929 return (const char *) (str_section->buffer + str_offset);
3019eac3
DE
19930}
19931
3019eac3
DE
19932/* Return the length of an LEB128 number in BUF. */
19933
19934static int
19935leb128_size (const gdb_byte *buf)
19936{
19937 const gdb_byte *begin = buf;
19938 gdb_byte byte;
19939
19940 while (1)
19941 {
19942 byte = *buf++;
19943 if ((byte & 128) == 0)
19944 return buf - begin;
19945 }
19946}
19947
c906108c 19948static void
e142c38c 19949set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
c906108c
SS
19950{
19951 switch (lang)
19952 {
19953 case DW_LANG_C89:
76bee0cc 19954 case DW_LANG_C99:
0cfd832f 19955 case DW_LANG_C11:
c906108c 19956 case DW_LANG_C:
d1be3247 19957 case DW_LANG_UPC:
e142c38c 19958 cu->language = language_c;
c906108c 19959 break;
9c37b5ae 19960 case DW_LANG_Java:
c906108c 19961 case DW_LANG_C_plus_plus:
0cfd832f
MW
19962 case DW_LANG_C_plus_plus_11:
19963 case DW_LANG_C_plus_plus_14:
e142c38c 19964 cu->language = language_cplus;
c906108c 19965 break;
6aecb9c2
JB
19966 case DW_LANG_D:
19967 cu->language = language_d;
19968 break;
c906108c
SS
19969 case DW_LANG_Fortran77:
19970 case DW_LANG_Fortran90:
b21b22e0 19971 case DW_LANG_Fortran95:
f7de9aab
MW
19972 case DW_LANG_Fortran03:
19973 case DW_LANG_Fortran08:
e142c38c 19974 cu->language = language_fortran;
c906108c 19975 break;
a766d390
DE
19976 case DW_LANG_Go:
19977 cu->language = language_go;
19978 break;
c906108c 19979 case DW_LANG_Mips_Assembler:
e142c38c 19980 cu->language = language_asm;
c906108c
SS
19981 break;
19982 case DW_LANG_Ada83:
8aaf0b47 19983 case DW_LANG_Ada95:
bc5f45f8
JB
19984 cu->language = language_ada;
19985 break;
72019c9c
GM
19986 case DW_LANG_Modula2:
19987 cu->language = language_m2;
19988 break;
fe8e67fd
PM
19989 case DW_LANG_Pascal83:
19990 cu->language = language_pascal;
19991 break;
22566fbd
DJ
19992 case DW_LANG_ObjC:
19993 cu->language = language_objc;
19994 break;
c44af4eb
TT
19995 case DW_LANG_Rust:
19996 case DW_LANG_Rust_old:
19997 cu->language = language_rust;
19998 break;
c906108c
SS
19999 case DW_LANG_Cobol74:
20000 case DW_LANG_Cobol85:
c906108c 20001 default:
e142c38c 20002 cu->language = language_minimal;
c906108c
SS
20003 break;
20004 }
e142c38c 20005 cu->language_defn = language_def (cu->language);
c906108c
SS
20006}
20007
20008/* Return the named attribute or NULL if not there. */
20009
20010static struct attribute *
e142c38c 20011dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
c906108c 20012{
a48e046c 20013 for (;;)
c906108c 20014 {
a48e046c
TT
20015 unsigned int i;
20016 struct attribute *spec = NULL;
20017
20018 for (i = 0; i < die->num_attrs; ++i)
20019 {
20020 if (die->attrs[i].name == name)
20021 return &die->attrs[i];
20022 if (die->attrs[i].name == DW_AT_specification
20023 || die->attrs[i].name == DW_AT_abstract_origin)
20024 spec = &die->attrs[i];
20025 }
20026
20027 if (!spec)
20028 break;
c906108c 20029
f2f0e013 20030 die = follow_die_ref (die, spec, &cu);
f2f0e013 20031 }
c5aa993b 20032
c906108c
SS
20033 return NULL;
20034}
20035
348e048f
DE
20036/* Return the named attribute or NULL if not there,
20037 but do not follow DW_AT_specification, etc.
20038 This is for use in contexts where we're reading .debug_types dies.
20039 Following DW_AT_specification, DW_AT_abstract_origin will take us
20040 back up the chain, and we want to go down. */
20041
20042static struct attribute *
45e58e77 20043dwarf2_attr_no_follow (struct die_info *die, unsigned int name)
348e048f
DE
20044{
20045 unsigned int i;
20046
20047 for (i = 0; i < die->num_attrs; ++i)
20048 if (die->attrs[i].name == name)
20049 return &die->attrs[i];
20050
20051 return NULL;
20052}
20053
7d45c7c3
KB
20054/* Return the string associated with a string-typed attribute, or NULL if it
20055 is either not found or is of an incorrect type. */
20056
20057static const char *
20058dwarf2_string_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
20059{
20060 struct attribute *attr;
20061 const char *str = NULL;
20062
20063 attr = dwarf2_attr (die, name, cu);
20064
20065 if (attr != NULL)
20066 {
43988095 20067 if (attr->form == DW_FORM_strp || attr->form == DW_FORM_line_strp
b3340438
L
20068 || attr->form == DW_FORM_string
20069 || attr->form == DW_FORM_GNU_str_index
16eb6b2d 20070 || attr->form == DW_FORM_GNU_strp_alt)
7d45c7c3
KB
20071 str = DW_STRING (attr);
20072 else
b98664d3 20073 complaint (_("string type expected for attribute %s for "
9d8780f0
SM
20074 "DIE at %s in module %s"),
20075 dwarf_attr_name (name), sect_offset_str (die->sect_off),
518817b3 20076 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
7d45c7c3
KB
20077 }
20078
20079 return str;
20080}
20081
05cf31d1
JB
20082/* Return non-zero iff the attribute NAME is defined for the given DIE,
20083 and holds a non-zero value. This function should only be used for
2dc7f7b3 20084 DW_FORM_flag or DW_FORM_flag_present attributes. */
05cf31d1
JB
20085
20086static int
20087dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
20088{
20089 struct attribute *attr = dwarf2_attr (die, name, cu);
20090
20091 return (attr && DW_UNSND (attr));
20092}
20093
3ca72b44 20094static int
e142c38c 20095die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
3ca72b44 20096{
05cf31d1
JB
20097 /* A DIE is a declaration if it has a DW_AT_declaration attribute
20098 which value is non-zero. However, we have to be careful with
20099 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
20100 (via dwarf2_flag_true_p) follows this attribute. So we may
20101 end up accidently finding a declaration attribute that belongs
20102 to a different DIE referenced by the specification attribute,
20103 even though the given DIE does not have a declaration attribute. */
20104 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
20105 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
3ca72b44
AC
20106}
20107
63d06c5c 20108/* Return the die giving the specification for DIE, if there is
f2f0e013 20109 one. *SPEC_CU is the CU containing DIE on input, and the CU
edb3359d
DJ
20110 containing the return value on output. If there is no
20111 specification, but there is an abstract origin, that is
20112 returned. */
63d06c5c
DC
20113
20114static struct die_info *
f2f0e013 20115die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
63d06c5c 20116{
f2f0e013
DJ
20117 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
20118 *spec_cu);
63d06c5c 20119
edb3359d
DJ
20120 if (spec_attr == NULL)
20121 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
20122
63d06c5c
DC
20123 if (spec_attr == NULL)
20124 return NULL;
20125 else
f2f0e013 20126 return follow_die_ref (die, spec_attr, spec_cu);
63d06c5c 20127}
c906108c 20128
527f3840
JK
20129/* Stub for free_line_header to match void * callback types. */
20130
20131static void
20132free_line_header_voidp (void *arg)
20133{
9a3c8263 20134 struct line_header *lh = (struct line_header *) arg;
527f3840 20135
fff8551c 20136 delete lh;
527f3840
JK
20137}
20138
fff8551c
PA
20139void
20140line_header::add_include_dir (const char *include_dir)
c906108c 20141{
27e0867f 20142 if (dwarf_line_debug >= 2)
fff8551c
PA
20143 fprintf_unfiltered (gdb_stdlog, "Adding dir %zu: %s\n",
20144 include_dirs.size () + 1, include_dir);
27e0867f 20145
fff8551c 20146 include_dirs.push_back (include_dir);
debd256d 20147}
6e70227d 20148
fff8551c
PA
20149void
20150line_header::add_file_name (const char *name,
ecfb656c 20151 dir_index d_index,
fff8551c
PA
20152 unsigned int mod_time,
20153 unsigned int length)
debd256d 20154{
27e0867f
DE
20155 if (dwarf_line_debug >= 2)
20156 fprintf_unfiltered (gdb_stdlog, "Adding file %u: %s\n",
fff8551c 20157 (unsigned) file_names.size () + 1, name);
27e0867f 20158
ecfb656c 20159 file_names.emplace_back (name, d_index, mod_time, length);
debd256d 20160}
6e70227d 20161
83769d0b 20162/* A convenience function to find the proper .debug_line section for a CU. */
36586728
TT
20163
20164static struct dwarf2_section_info *
20165get_debug_line_section (struct dwarf2_cu *cu)
20166{
20167 struct dwarf2_section_info *section;
518817b3
SM
20168 struct dwarf2_per_objfile *dwarf2_per_objfile
20169 = cu->per_cu->dwarf2_per_objfile;
36586728
TT
20170
20171 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
20172 DWO file. */
20173 if (cu->dwo_unit && cu->per_cu->is_debug_types)
20174 section = &cu->dwo_unit->dwo_file->sections.line;
20175 else if (cu->per_cu->is_dwz)
20176 {
ed2dc618 20177 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
36586728
TT
20178
20179 section = &dwz->line;
20180 }
20181 else
20182 section = &dwarf2_per_objfile->line;
20183
20184 return section;
20185}
20186
43988095
JK
20187/* Read directory or file name entry format, starting with byte of
20188 format count entries, ULEB128 pairs of entry formats, ULEB128 of
20189 entries count and the entries themselves in the described entry
20190 format. */
20191
20192static void
ed2dc618
SM
20193read_formatted_entries (struct dwarf2_per_objfile *dwarf2_per_objfile,
20194 bfd *abfd, const gdb_byte **bufp,
43988095
JK
20195 struct line_header *lh,
20196 const struct comp_unit_head *cu_header,
20197 void (*callback) (struct line_header *lh,
20198 const char *name,
ecfb656c 20199 dir_index d_index,
43988095
JK
20200 unsigned int mod_time,
20201 unsigned int length))
20202{
20203 gdb_byte format_count, formati;
20204 ULONGEST data_count, datai;
20205 const gdb_byte *buf = *bufp;
20206 const gdb_byte *format_header_data;
43988095
JK
20207 unsigned int bytes_read;
20208
20209 format_count = read_1_byte (abfd, buf);
20210 buf += 1;
20211 format_header_data = buf;
20212 for (formati = 0; formati < format_count; formati++)
20213 {
20214 read_unsigned_leb128 (abfd, buf, &bytes_read);
20215 buf += bytes_read;
20216 read_unsigned_leb128 (abfd, buf, &bytes_read);
20217 buf += bytes_read;
20218 }
20219
20220 data_count = read_unsigned_leb128 (abfd, buf, &bytes_read);
20221 buf += bytes_read;
20222 for (datai = 0; datai < data_count; datai++)
20223 {
20224 const gdb_byte *format = format_header_data;
20225 struct file_entry fe;
20226
43988095
JK
20227 for (formati = 0; formati < format_count; formati++)
20228 {
ecfb656c 20229 ULONGEST content_type = read_unsigned_leb128 (abfd, format, &bytes_read);
43988095 20230 format += bytes_read;
43988095 20231
ecfb656c 20232 ULONGEST form = read_unsigned_leb128 (abfd, format, &bytes_read);
43988095 20233 format += bytes_read;
ecfb656c
PA
20234
20235 gdb::optional<const char *> string;
20236 gdb::optional<unsigned int> uint;
20237
43988095
JK
20238 switch (form)
20239 {
20240 case DW_FORM_string:
ecfb656c 20241 string.emplace (read_direct_string (abfd, buf, &bytes_read));
43988095
JK
20242 buf += bytes_read;
20243 break;
20244
20245 case DW_FORM_line_strp:
ed2dc618
SM
20246 string.emplace (read_indirect_line_string (dwarf2_per_objfile,
20247 abfd, buf,
ecfb656c
PA
20248 cu_header,
20249 &bytes_read));
43988095
JK
20250 buf += bytes_read;
20251 break;
20252
20253 case DW_FORM_data1:
ecfb656c 20254 uint.emplace (read_1_byte (abfd, buf));
43988095
JK
20255 buf += 1;
20256 break;
20257
20258 case DW_FORM_data2:
ecfb656c 20259 uint.emplace (read_2_bytes (abfd, buf));
43988095
JK
20260 buf += 2;
20261 break;
20262
20263 case DW_FORM_data4:
ecfb656c 20264 uint.emplace (read_4_bytes (abfd, buf));
43988095
JK
20265 buf += 4;
20266 break;
20267
20268 case DW_FORM_data8:
ecfb656c 20269 uint.emplace (read_8_bytes (abfd, buf));
43988095
JK
20270 buf += 8;
20271 break;
20272
20273 case DW_FORM_udata:
ecfb656c 20274 uint.emplace (read_unsigned_leb128 (abfd, buf, &bytes_read));
43988095
JK
20275 buf += bytes_read;
20276 break;
20277
20278 case DW_FORM_block:
20279 /* It is valid only for DW_LNCT_timestamp which is ignored by
20280 current GDB. */
20281 break;
20282 }
ecfb656c
PA
20283
20284 switch (content_type)
20285 {
20286 case DW_LNCT_path:
20287 if (string.has_value ())
20288 fe.name = *string;
20289 break;
20290 case DW_LNCT_directory_index:
20291 if (uint.has_value ())
20292 fe.d_index = (dir_index) *uint;
20293 break;
20294 case DW_LNCT_timestamp:
20295 if (uint.has_value ())
20296 fe.mod_time = *uint;
20297 break;
20298 case DW_LNCT_size:
20299 if (uint.has_value ())
20300 fe.length = *uint;
20301 break;
20302 case DW_LNCT_MD5:
20303 break;
20304 default:
b98664d3 20305 complaint (_("Unknown format content type %s"),
ecfb656c
PA
20306 pulongest (content_type));
20307 }
43988095
JK
20308 }
20309
ecfb656c 20310 callback (lh, fe.name, fe.d_index, fe.mod_time, fe.length);
43988095
JK
20311 }
20312
20313 *bufp = buf;
20314}
20315
debd256d 20316/* Read the statement program header starting at OFFSET in
3019eac3 20317 .debug_line, or .debug_line.dwo. Return a pointer
6502dd73 20318 to a struct line_header, allocated using xmalloc.
cd366ee8
DE
20319 Returns NULL if there is a problem reading the header, e.g., if it
20320 has a version we don't understand.
debd256d
JB
20321
20322 NOTE: the strings in the include directory and file name tables of
3019eac3
DE
20323 the returned object point into the dwarf line section buffer,
20324 and must not be freed. */
ae2de4f8 20325
fff8551c 20326static line_header_up
9c541725 20327dwarf_decode_line_header (sect_offset sect_off, struct dwarf2_cu *cu)
debd256d 20328{
d521ce57 20329 const gdb_byte *line_ptr;
c764a876 20330 unsigned int bytes_read, offset_size;
debd256d 20331 int i;
d521ce57 20332 const char *cur_dir, *cur_file;
3019eac3
DE
20333 struct dwarf2_section_info *section;
20334 bfd *abfd;
518817b3
SM
20335 struct dwarf2_per_objfile *dwarf2_per_objfile
20336 = cu->per_cu->dwarf2_per_objfile;
3019eac3 20337
36586728 20338 section = get_debug_line_section (cu);
3019eac3
DE
20339 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
20340 if (section->buffer == NULL)
debd256d 20341 {
3019eac3 20342 if (cu->dwo_unit && cu->per_cu->is_debug_types)
b98664d3 20343 complaint (_("missing .debug_line.dwo section"));
3019eac3 20344 else
b98664d3 20345 complaint (_("missing .debug_line section"));
debd256d
JB
20346 return 0;
20347 }
20348
fceca515
DE
20349 /* We can't do this until we know the section is non-empty.
20350 Only then do we know we have such a section. */
a32a8923 20351 abfd = get_section_bfd_owner (section);
fceca515 20352
a738430d
MK
20353 /* Make sure that at least there's room for the total_length field.
20354 That could be 12 bytes long, but we're just going to fudge that. */
9c541725 20355 if (to_underlying (sect_off) + 4 >= section->size)
debd256d 20356 {
4d3c2250 20357 dwarf2_statement_list_fits_in_line_number_section_complaint ();
debd256d
JB
20358 return 0;
20359 }
20360
fff8551c 20361 line_header_up lh (new line_header ());
debd256d 20362
9c541725 20363 lh->sect_off = sect_off;
527f3840
JK
20364 lh->offset_in_dwz = cu->per_cu->is_dwz;
20365
9c541725 20366 line_ptr = section->buffer + to_underlying (sect_off);
debd256d 20367
a738430d 20368 /* Read in the header. */
6e70227d 20369 lh->total_length =
c764a876
DE
20370 read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
20371 &bytes_read, &offset_size);
debd256d 20372 line_ptr += bytes_read;
3019eac3 20373 if (line_ptr + lh->total_length > (section->buffer + section->size))
debd256d 20374 {
4d3c2250 20375 dwarf2_statement_list_fits_in_line_number_section_complaint ();
debd256d
JB
20376 return 0;
20377 }
20378 lh->statement_program_end = line_ptr + lh->total_length;
20379 lh->version = read_2_bytes (abfd, line_ptr);
20380 line_ptr += 2;
43988095 20381 if (lh->version > 5)
cd366ee8
DE
20382 {
20383 /* This is a version we don't understand. The format could have
20384 changed in ways we don't handle properly so just punt. */
b98664d3 20385 complaint (_("unsupported version in .debug_line section"));
cd366ee8
DE
20386 return NULL;
20387 }
43988095
JK
20388 if (lh->version >= 5)
20389 {
20390 gdb_byte segment_selector_size;
20391
20392 /* Skip address size. */
20393 read_1_byte (abfd, line_ptr);
20394 line_ptr += 1;
20395
20396 segment_selector_size = read_1_byte (abfd, line_ptr);
20397 line_ptr += 1;
20398 if (segment_selector_size != 0)
20399 {
b98664d3 20400 complaint (_("unsupported segment selector size %u "
43988095
JK
20401 "in .debug_line section"),
20402 segment_selector_size);
20403 return NULL;
20404 }
20405 }
c764a876
DE
20406 lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
20407 line_ptr += offset_size;
debd256d
JB
20408 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
20409 line_ptr += 1;
2dc7f7b3
TT
20410 if (lh->version >= 4)
20411 {
20412 lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
20413 line_ptr += 1;
20414 }
20415 else
20416 lh->maximum_ops_per_instruction = 1;
20417
20418 if (lh->maximum_ops_per_instruction == 0)
20419 {
20420 lh->maximum_ops_per_instruction = 1;
b98664d3 20421 complaint (_("invalid maximum_ops_per_instruction "
3e43a32a 20422 "in `.debug_line' section"));
2dc7f7b3
TT
20423 }
20424
debd256d
JB
20425 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
20426 line_ptr += 1;
20427 lh->line_base = read_1_signed_byte (abfd, line_ptr);
20428 line_ptr += 1;
20429 lh->line_range = read_1_byte (abfd, line_ptr);
20430 line_ptr += 1;
20431 lh->opcode_base = read_1_byte (abfd, line_ptr);
20432 line_ptr += 1;
fff8551c 20433 lh->standard_opcode_lengths.reset (new unsigned char[lh->opcode_base]);
debd256d
JB
20434
20435 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
20436 for (i = 1; i < lh->opcode_base; ++i)
20437 {
20438 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
20439 line_ptr += 1;
20440 }
20441
43988095 20442 if (lh->version >= 5)
debd256d 20443 {
43988095 20444 /* Read directory table. */
ed2dc618
SM
20445 read_formatted_entries (dwarf2_per_objfile, abfd, &line_ptr, lh.get (),
20446 &cu->header,
b926417a 20447 [] (struct line_header *header, const char *name,
ecfb656c 20448 dir_index d_index, unsigned int mod_time,
fff8551c
PA
20449 unsigned int length)
20450 {
b926417a 20451 header->add_include_dir (name);
fff8551c 20452 });
debd256d 20453
43988095 20454 /* Read file name table. */
ed2dc618
SM
20455 read_formatted_entries (dwarf2_per_objfile, abfd, &line_ptr, lh.get (),
20456 &cu->header,
b926417a 20457 [] (struct line_header *header, const char *name,
ecfb656c 20458 dir_index d_index, unsigned int mod_time,
fff8551c
PA
20459 unsigned int length)
20460 {
b926417a 20461 header->add_file_name (name, d_index, mod_time, length);
fff8551c 20462 });
43988095
JK
20463 }
20464 else
debd256d 20465 {
43988095
JK
20466 /* Read directory table. */
20467 while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
20468 {
20469 line_ptr += bytes_read;
fff8551c 20470 lh->add_include_dir (cur_dir);
43988095 20471 }
debd256d
JB
20472 line_ptr += bytes_read;
20473
43988095
JK
20474 /* Read file name table. */
20475 while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
20476 {
ecfb656c
PA
20477 unsigned int mod_time, length;
20478 dir_index d_index;
43988095
JK
20479
20480 line_ptr += bytes_read;
ecfb656c 20481 d_index = (dir_index) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
43988095
JK
20482 line_ptr += bytes_read;
20483 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20484 line_ptr += bytes_read;
20485 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20486 line_ptr += bytes_read;
20487
ecfb656c 20488 lh->add_file_name (cur_file, d_index, mod_time, length);
43988095
JK
20489 }
20490 line_ptr += bytes_read;
debd256d 20491 }
6e70227d 20492 lh->statement_program_start = line_ptr;
debd256d 20493
3019eac3 20494 if (line_ptr > (section->buffer + section->size))
b98664d3 20495 complaint (_("line number info header doesn't "
3e43a32a 20496 "fit in `.debug_line' section"));
debd256d 20497
debd256d
JB
20498 return lh;
20499}
c906108c 20500
c6da4cef
DE
20501/* Subroutine of dwarf_decode_lines to simplify it.
20502 Return the file name of the psymtab for included file FILE_INDEX
20503 in line header LH of PST.
20504 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
c89b44cd
TT
20505 If space for the result is malloc'd, *NAME_HOLDER will be set.
20506 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
c6da4cef 20507
d521ce57 20508static const char *
c6da4cef
DE
20509psymtab_include_file_name (const struct line_header *lh, int file_index,
20510 const struct partial_symtab *pst,
c89b44cd
TT
20511 const char *comp_dir,
20512 gdb::unique_xmalloc_ptr<char> *name_holder)
c6da4cef 20513{
8c43009f 20514 const file_entry &fe = lh->file_names[file_index];
d521ce57
TT
20515 const char *include_name = fe.name;
20516 const char *include_name_to_compare = include_name;
72b9f47f 20517 const char *pst_filename;
c6da4cef
DE
20518 int file_is_pst;
20519
8c43009f 20520 const char *dir_name = fe.include_dir (lh);
c6da4cef 20521
c89b44cd 20522 gdb::unique_xmalloc_ptr<char> hold_compare;
c6da4cef
DE
20523 if (!IS_ABSOLUTE_PATH (include_name)
20524 && (dir_name != NULL || comp_dir != NULL))
20525 {
20526 /* Avoid creating a duplicate psymtab for PST.
20527 We do this by comparing INCLUDE_NAME and PST_FILENAME.
20528 Before we do the comparison, however, we need to account
20529 for DIR_NAME and COMP_DIR.
20530 First prepend dir_name (if non-NULL). If we still don't
20531 have an absolute path prepend comp_dir (if non-NULL).
20532 However, the directory we record in the include-file's
20533 psymtab does not contain COMP_DIR (to match the
20534 corresponding symtab(s)).
20535
20536 Example:
20537
20538 bash$ cd /tmp
20539 bash$ gcc -g ./hello.c
20540 include_name = "hello.c"
20541 dir_name = "."
20542 DW_AT_comp_dir = comp_dir = "/tmp"
5f52445b
YQ
20543 DW_AT_name = "./hello.c"
20544
20545 */
c6da4cef
DE
20546
20547 if (dir_name != NULL)
20548 {
c89b44cd
TT
20549 name_holder->reset (concat (dir_name, SLASH_STRING,
20550 include_name, (char *) NULL));
20551 include_name = name_holder->get ();
c6da4cef 20552 include_name_to_compare = include_name;
c6da4cef
DE
20553 }
20554 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
20555 {
c89b44cd
TT
20556 hold_compare.reset (concat (comp_dir, SLASH_STRING,
20557 include_name, (char *) NULL));
20558 include_name_to_compare = hold_compare.get ();
c6da4cef
DE
20559 }
20560 }
20561
20562 pst_filename = pst->filename;
c89b44cd 20563 gdb::unique_xmalloc_ptr<char> copied_name;
c6da4cef
DE
20564 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
20565 {
c89b44cd
TT
20566 copied_name.reset (concat (pst->dirname, SLASH_STRING,
20567 pst_filename, (char *) NULL));
20568 pst_filename = copied_name.get ();
c6da4cef
DE
20569 }
20570
1e3fad37 20571 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
c6da4cef 20572
c6da4cef
DE
20573 if (file_is_pst)
20574 return NULL;
20575 return include_name;
20576}
20577
d9b3de22
DE
20578/* State machine to track the state of the line number program. */
20579
6f77053d 20580class lnp_state_machine
d9b3de22 20581{
6f77053d
PA
20582public:
20583 /* Initialize a machine state for the start of a line number
20584 program. */
804d2729
TT
20585 lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch, line_header *lh,
20586 bool record_lines_p);
6f77053d 20587
8c43009f
PA
20588 file_entry *current_file ()
20589 {
20590 /* lh->file_names is 0-based, but the file name numbers in the
20591 statement program are 1-based. */
6f77053d
PA
20592 return m_line_header->file_name_at (m_file);
20593 }
20594
20595 /* Record the line in the state machine. END_SEQUENCE is true if
20596 we're processing the end of a sequence. */
20597 void record_line (bool end_sequence);
20598
7ab6656f
OJ
20599 /* Check ADDRESS is zero and less than UNRELOCATED_LOWPC and if true
20600 nop-out rest of the lines in this sequence. */
6f77053d
PA
20601 void check_line_address (struct dwarf2_cu *cu,
20602 const gdb_byte *line_ptr,
7ab6656f 20603 CORE_ADDR unrelocated_lowpc, CORE_ADDR address);
6f77053d
PA
20604
20605 void handle_set_discriminator (unsigned int discriminator)
20606 {
20607 m_discriminator = discriminator;
20608 m_line_has_non_zero_discriminator |= discriminator != 0;
20609 }
20610
20611 /* Handle DW_LNE_set_address. */
20612 void handle_set_address (CORE_ADDR baseaddr, CORE_ADDR address)
20613 {
20614 m_op_index = 0;
20615 address += baseaddr;
20616 m_address = gdbarch_adjust_dwarf2_line (m_gdbarch, address, false);
20617 }
20618
20619 /* Handle DW_LNS_advance_pc. */
20620 void handle_advance_pc (CORE_ADDR adjust);
20621
20622 /* Handle a special opcode. */
20623 void handle_special_opcode (unsigned char op_code);
20624
20625 /* Handle DW_LNS_advance_line. */
20626 void handle_advance_line (int line_delta)
20627 {
20628 advance_line (line_delta);
20629 }
20630
20631 /* Handle DW_LNS_set_file. */
20632 void handle_set_file (file_name_index file);
20633
20634 /* Handle DW_LNS_negate_stmt. */
20635 void handle_negate_stmt ()
20636 {
20637 m_is_stmt = !m_is_stmt;
20638 }
20639
20640 /* Handle DW_LNS_const_add_pc. */
20641 void handle_const_add_pc ();
20642
20643 /* Handle DW_LNS_fixed_advance_pc. */
20644 void handle_fixed_advance_pc (CORE_ADDR addr_adj)
20645 {
20646 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20647 m_op_index = 0;
20648 }
20649
20650 /* Handle DW_LNS_copy. */
20651 void handle_copy ()
20652 {
20653 record_line (false);
20654 m_discriminator = 0;
20655 }
20656
20657 /* Handle DW_LNE_end_sequence. */
20658 void handle_end_sequence ()
20659 {
804d2729 20660 m_currently_recording_lines = true;
6f77053d
PA
20661 }
20662
20663private:
20664 /* Advance the line by LINE_DELTA. */
20665 void advance_line (int line_delta)
20666 {
20667 m_line += line_delta;
20668
20669 if (line_delta != 0)
20670 m_line_has_non_zero_discriminator = m_discriminator != 0;
8c43009f
PA
20671 }
20672
804d2729
TT
20673 struct dwarf2_cu *m_cu;
20674
6f77053d
PA
20675 gdbarch *m_gdbarch;
20676
20677 /* True if we're recording lines.
20678 Otherwise we're building partial symtabs and are just interested in
20679 finding include files mentioned by the line number program. */
20680 bool m_record_lines_p;
20681
8c43009f 20682 /* The line number header. */
6f77053d 20683 line_header *m_line_header;
8c43009f 20684
6f77053d
PA
20685 /* These are part of the standard DWARF line number state machine,
20686 and initialized according to the DWARF spec. */
d9b3de22 20687
6f77053d 20688 unsigned char m_op_index = 0;
8c43009f 20689 /* The line table index (1-based) of the current file. */
6f77053d
PA
20690 file_name_index m_file = (file_name_index) 1;
20691 unsigned int m_line = 1;
20692
20693 /* These are initialized in the constructor. */
20694
20695 CORE_ADDR m_address;
20696 bool m_is_stmt;
20697 unsigned int m_discriminator;
d9b3de22
DE
20698
20699 /* Additional bits of state we need to track. */
20700
20701 /* The last file that we called dwarf2_start_subfile for.
20702 This is only used for TLLs. */
6f77053d 20703 unsigned int m_last_file = 0;
d9b3de22 20704 /* The last file a line number was recorded for. */
6f77053d 20705 struct subfile *m_last_subfile = NULL;
d9b3de22 20706
804d2729
TT
20707 /* When true, record the lines we decode. */
20708 bool m_currently_recording_lines = false;
d9b3de22
DE
20709
20710 /* The last line number that was recorded, used to coalesce
20711 consecutive entries for the same line. This can happen, for
20712 example, when discriminators are present. PR 17276. */
6f77053d
PA
20713 unsigned int m_last_line = 0;
20714 bool m_line_has_non_zero_discriminator = false;
8c43009f 20715};
d9b3de22 20716
6f77053d
PA
20717void
20718lnp_state_machine::handle_advance_pc (CORE_ADDR adjust)
20719{
20720 CORE_ADDR addr_adj = (((m_op_index + adjust)
20721 / m_line_header->maximum_ops_per_instruction)
20722 * m_line_header->minimum_instruction_length);
20723 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20724 m_op_index = ((m_op_index + adjust)
20725 % m_line_header->maximum_ops_per_instruction);
20726}
d9b3de22 20727
6f77053d
PA
20728void
20729lnp_state_machine::handle_special_opcode (unsigned char op_code)
d9b3de22 20730{
6f77053d
PA
20731 unsigned char adj_opcode = op_code - m_line_header->opcode_base;
20732 CORE_ADDR addr_adj = (((m_op_index
20733 + (adj_opcode / m_line_header->line_range))
20734 / m_line_header->maximum_ops_per_instruction)
20735 * m_line_header->minimum_instruction_length);
20736 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20737 m_op_index = ((m_op_index + (adj_opcode / m_line_header->line_range))
20738 % m_line_header->maximum_ops_per_instruction);
d9b3de22 20739
6f77053d
PA
20740 int line_delta = (m_line_header->line_base
20741 + (adj_opcode % m_line_header->line_range));
20742 advance_line (line_delta);
20743 record_line (false);
20744 m_discriminator = 0;
20745}
d9b3de22 20746
6f77053d
PA
20747void
20748lnp_state_machine::handle_set_file (file_name_index file)
20749{
20750 m_file = file;
20751
20752 const file_entry *fe = current_file ();
20753 if (fe == NULL)
20754 dwarf2_debug_line_missing_file_complaint ();
20755 else if (m_record_lines_p)
20756 {
20757 const char *dir = fe->include_dir (m_line_header);
20758
c24bdb02 20759 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
6f77053d 20760 m_line_has_non_zero_discriminator = m_discriminator != 0;
804d2729 20761 dwarf2_start_subfile (m_cu, fe->name, dir);
6f77053d
PA
20762 }
20763}
20764
20765void
20766lnp_state_machine::handle_const_add_pc ()
20767{
20768 CORE_ADDR adjust
20769 = (255 - m_line_header->opcode_base) / m_line_header->line_range;
20770
20771 CORE_ADDR addr_adj
20772 = (((m_op_index + adjust)
20773 / m_line_header->maximum_ops_per_instruction)
20774 * m_line_header->minimum_instruction_length);
20775
20776 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20777 m_op_index = ((m_op_index + adjust)
20778 % m_line_header->maximum_ops_per_instruction);
20779}
d9b3de22 20780
a05a36a5
DE
20781/* Return non-zero if we should add LINE to the line number table.
20782 LINE is the line to add, LAST_LINE is the last line that was added,
20783 LAST_SUBFILE is the subfile for LAST_LINE.
20784 LINE_HAS_NON_ZERO_DISCRIMINATOR is non-zero if LINE has ever
20785 had a non-zero discriminator.
20786
20787 We have to be careful in the presence of discriminators.
20788 E.g., for this line:
20789
20790 for (i = 0; i < 100000; i++);
20791
20792 clang can emit four line number entries for that one line,
20793 each with a different discriminator.
20794 See gdb.dwarf2/dw2-single-line-discriminators.exp for an example.
20795
20796 However, we want gdb to coalesce all four entries into one.
20797 Otherwise the user could stepi into the middle of the line and
20798 gdb would get confused about whether the pc really was in the
20799 middle of the line.
20800
20801 Things are further complicated by the fact that two consecutive
20802 line number entries for the same line is a heuristic used by gcc
20803 to denote the end of the prologue. So we can't just discard duplicate
20804 entries, we have to be selective about it. The heuristic we use is
20805 that we only collapse consecutive entries for the same line if at least
20806 one of those entries has a non-zero discriminator. PR 17276.
20807
20808 Note: Addresses in the line number state machine can never go backwards
20809 within one sequence, thus this coalescing is ok. */
20810
20811static int
804d2729
TT
20812dwarf_record_line_p (struct dwarf2_cu *cu,
20813 unsigned int line, unsigned int last_line,
a05a36a5
DE
20814 int line_has_non_zero_discriminator,
20815 struct subfile *last_subfile)
20816{
c24bdb02 20817 if (cu->get_builder ()->get_current_subfile () != last_subfile)
a05a36a5
DE
20818 return 1;
20819 if (line != last_line)
20820 return 1;
20821 /* Same line for the same file that we've seen already.
20822 As a last check, for pr 17276, only record the line if the line
20823 has never had a non-zero discriminator. */
20824 if (!line_has_non_zero_discriminator)
20825 return 1;
20826 return 0;
20827}
20828
804d2729
TT
20829/* Use the CU's builder to record line number LINE beginning at
20830 address ADDRESS in the line table of subfile SUBFILE. */
252a6764
DE
20831
20832static void
d9b3de22
DE
20833dwarf_record_line_1 (struct gdbarch *gdbarch, struct subfile *subfile,
20834 unsigned int line, CORE_ADDR address,
804d2729 20835 struct dwarf2_cu *cu)
252a6764
DE
20836{
20837 CORE_ADDR addr = gdbarch_addr_bits_remove (gdbarch, address);
20838
27e0867f
DE
20839 if (dwarf_line_debug)
20840 {
20841 fprintf_unfiltered (gdb_stdlog,
20842 "Recording line %u, file %s, address %s\n",
20843 line, lbasename (subfile->name),
20844 paddress (gdbarch, address));
20845 }
20846
804d2729 20847 if (cu != nullptr)
c24bdb02 20848 cu->get_builder ()->record_line (subfile, line, addr);
252a6764
DE
20849}
20850
20851/* Subroutine of dwarf_decode_lines_1 to simplify it.
20852 Mark the end of a set of line number records.
d9b3de22 20853 The arguments are the same as for dwarf_record_line_1.
252a6764
DE
20854 If SUBFILE is NULL the request is ignored. */
20855
20856static void
20857dwarf_finish_line (struct gdbarch *gdbarch, struct subfile *subfile,
804d2729 20858 CORE_ADDR address, struct dwarf2_cu *cu)
252a6764 20859{
27e0867f
DE
20860 if (subfile == NULL)
20861 return;
20862
20863 if (dwarf_line_debug)
20864 {
20865 fprintf_unfiltered (gdb_stdlog,
20866 "Finishing current line, file %s, address %s\n",
20867 lbasename (subfile->name),
20868 paddress (gdbarch, address));
20869 }
20870
804d2729 20871 dwarf_record_line_1 (gdbarch, subfile, 0, address, cu);
d9b3de22
DE
20872}
20873
6f77053d
PA
20874void
20875lnp_state_machine::record_line (bool end_sequence)
d9b3de22 20876{
d9b3de22
DE
20877 if (dwarf_line_debug)
20878 {
20879 fprintf_unfiltered (gdb_stdlog,
20880 "Processing actual line %u: file %u,"
20881 " address %s, is_stmt %u, discrim %u\n",
6f77053d
PA
20882 m_line, to_underlying (m_file),
20883 paddress (m_gdbarch, m_address),
20884 m_is_stmt, m_discriminator);
d9b3de22
DE
20885 }
20886
6f77053d 20887 file_entry *fe = current_file ();
8c43009f
PA
20888
20889 if (fe == NULL)
d9b3de22
DE
20890 dwarf2_debug_line_missing_file_complaint ();
20891 /* For now we ignore lines not starting on an instruction boundary.
20892 But not when processing end_sequence for compatibility with the
20893 previous version of the code. */
6f77053d 20894 else if (m_op_index == 0 || end_sequence)
d9b3de22 20895 {
8c43009f 20896 fe->included_p = 1;
c258c396 20897 if (m_record_lines_p && (producer_is_codewarrior (m_cu) || m_is_stmt))
d9b3de22 20898 {
c24bdb02 20899 if (m_last_subfile != m_cu->get_builder ()->get_current_subfile ()
804d2729 20900 || end_sequence)
d9b3de22 20901 {
804d2729
TT
20902 dwarf_finish_line (m_gdbarch, m_last_subfile, m_address,
20903 m_currently_recording_lines ? m_cu : nullptr);
d9b3de22
DE
20904 }
20905
20906 if (!end_sequence)
20907 {
804d2729 20908 if (dwarf_record_line_p (m_cu, m_line, m_last_line,
6f77053d
PA
20909 m_line_has_non_zero_discriminator,
20910 m_last_subfile))
d9b3de22 20911 {
c24bdb02 20912 buildsym_compunit *builder = m_cu->get_builder ();
804d2729 20913 dwarf_record_line_1 (m_gdbarch,
c24bdb02 20914 builder->get_current_subfile (),
6f77053d 20915 m_line, m_address,
804d2729 20916 m_currently_recording_lines ? m_cu : nullptr);
d9b3de22 20917 }
c24bdb02 20918 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
6f77053d 20919 m_last_line = m_line;
d9b3de22
DE
20920 }
20921 }
20922 }
20923}
20924
804d2729
TT
20925lnp_state_machine::lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch,
20926 line_header *lh, bool record_lines_p)
d9b3de22 20927{
804d2729 20928 m_cu = cu;
6f77053d
PA
20929 m_gdbarch = arch;
20930 m_record_lines_p = record_lines_p;
20931 m_line_header = lh;
d9b3de22 20932
804d2729 20933 m_currently_recording_lines = true;
d9b3de22 20934
d9b3de22
DE
20935 /* Call `gdbarch_adjust_dwarf2_line' on the initial 0 address as if there
20936 was a line entry for it so that the backend has a chance to adjust it
20937 and also record it in case it needs it. This is currently used by MIPS
20938 code, cf. `mips_adjust_dwarf2_line'. */
6f77053d
PA
20939 m_address = gdbarch_adjust_dwarf2_line (arch, 0, 0);
20940 m_is_stmt = lh->default_is_stmt;
20941 m_discriminator = 0;
252a6764
DE
20942}
20943
6f77053d
PA
20944void
20945lnp_state_machine::check_line_address (struct dwarf2_cu *cu,
20946 const gdb_byte *line_ptr,
7ab6656f 20947 CORE_ADDR unrelocated_lowpc, CORE_ADDR address)
924c2928 20948{
7ab6656f
OJ
20949 /* If ADDRESS < UNRELOCATED_LOWPC then it's not a usable value, it's outside
20950 the pc range of the CU. However, we restrict the test to only ADDRESS
20951 values of zero to preserve GDB's previous behaviour which is to handle
20952 the specific case of a function being GC'd by the linker. */
924c2928 20953
7ab6656f 20954 if (address == 0 && address < unrelocated_lowpc)
924c2928
DE
20955 {
20956 /* This line table is for a function which has been
20957 GCd by the linker. Ignore it. PR gdb/12528 */
20958
518817b3 20959 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
924c2928
DE
20960 long line_offset = line_ptr - get_debug_line_section (cu)->buffer;
20961
b98664d3 20962 complaint (_(".debug_line address at offset 0x%lx is 0 [in module %s]"),
924c2928 20963 line_offset, objfile_name (objfile));
804d2729
TT
20964 m_currently_recording_lines = false;
20965 /* Note: m_currently_recording_lines is left as false until we see
20966 DW_LNE_end_sequence. */
924c2928
DE
20967 }
20968}
20969
f3f5162e 20970/* Subroutine of dwarf_decode_lines to simplify it.
d9b3de22
DE
20971 Process the line number information in LH.
20972 If DECODE_FOR_PST_P is non-zero, all we do is process the line number
20973 program in order to set included_p for every referenced header. */
debd256d 20974
c906108c 20975static void
43f3e411
DE
20976dwarf_decode_lines_1 (struct line_header *lh, struct dwarf2_cu *cu,
20977 const int decode_for_pst_p, CORE_ADDR lowpc)
c906108c 20978{
d521ce57
TT
20979 const gdb_byte *line_ptr, *extended_end;
20980 const gdb_byte *line_end;
a8c50c1f 20981 unsigned int bytes_read, extended_len;
699ca60a 20982 unsigned char op_code, extended_op;
e142c38c 20983 CORE_ADDR baseaddr;
518817b3 20984 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
f3f5162e 20985 bfd *abfd = objfile->obfd;
fbf65064 20986 struct gdbarch *gdbarch = get_objfile_arch (objfile);
6f77053d
PA
20987 /* True if we're recording line info (as opposed to building partial
20988 symtabs and just interested in finding include files mentioned by
20989 the line number program). */
20990 bool record_lines_p = !decode_for_pst_p;
e142c38c
DJ
20991
20992 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 20993
debd256d
JB
20994 line_ptr = lh->statement_program_start;
20995 line_end = lh->statement_program_end;
c906108c
SS
20996
20997 /* Read the statement sequences until there's nothing left. */
20998 while (line_ptr < line_end)
20999 {
6f77053d
PA
21000 /* The DWARF line number program state machine. Reset the state
21001 machine at the start of each sequence. */
804d2729 21002 lnp_state_machine state_machine (cu, gdbarch, lh, record_lines_p);
6f77053d 21003 bool end_sequence = false;
d9b3de22 21004
8c43009f 21005 if (record_lines_p)
c906108c 21006 {
8c43009f
PA
21007 /* Start a subfile for the current file of the state
21008 machine. */
21009 const file_entry *fe = state_machine.current_file ();
21010
21011 if (fe != NULL)
804d2729 21012 dwarf2_start_subfile (cu, fe->name, fe->include_dir (lh));
c906108c
SS
21013 }
21014
a738430d 21015 /* Decode the table. */
d9b3de22 21016 while (line_ptr < line_end && !end_sequence)
c906108c
SS
21017 {
21018 op_code = read_1_byte (abfd, line_ptr);
21019 line_ptr += 1;
9aa1fe7e 21020
debd256d 21021 if (op_code >= lh->opcode_base)
6e70227d 21022 {
8e07a239 21023 /* Special opcode. */
6f77053d 21024 state_machine.handle_special_opcode (op_code);
9aa1fe7e
GK
21025 }
21026 else switch (op_code)
c906108c
SS
21027 {
21028 case DW_LNS_extended_op:
3e43a32a
MS
21029 extended_len = read_unsigned_leb128 (abfd, line_ptr,
21030 &bytes_read);
473b7be6 21031 line_ptr += bytes_read;
a8c50c1f 21032 extended_end = line_ptr + extended_len;
c906108c
SS
21033 extended_op = read_1_byte (abfd, line_ptr);
21034 line_ptr += 1;
21035 switch (extended_op)
21036 {
21037 case DW_LNE_end_sequence:
6f77053d
PA
21038 state_machine.handle_end_sequence ();
21039 end_sequence = true;
c906108c
SS
21040 break;
21041 case DW_LNE_set_address:
d9b3de22
DE
21042 {
21043 CORE_ADDR address
21044 = read_address (abfd, line_ptr, cu, &bytes_read);
d9b3de22 21045 line_ptr += bytes_read;
6f77053d
PA
21046
21047 state_machine.check_line_address (cu, line_ptr,
7ab6656f 21048 lowpc - baseaddr, address);
6f77053d 21049 state_machine.handle_set_address (baseaddr, address);
d9b3de22 21050 }
c906108c
SS
21051 break;
21052 case DW_LNE_define_file:
debd256d 21053 {
d521ce57 21054 const char *cur_file;
ecfb656c
PA
21055 unsigned int mod_time, length;
21056 dir_index dindex;
6e70227d 21057
3e43a32a
MS
21058 cur_file = read_direct_string (abfd, line_ptr,
21059 &bytes_read);
debd256d 21060 line_ptr += bytes_read;
ecfb656c 21061 dindex = (dir_index)
debd256d
JB
21062 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21063 line_ptr += bytes_read;
21064 mod_time =
21065 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21066 line_ptr += bytes_read;
21067 length =
21068 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21069 line_ptr += bytes_read;
ecfb656c 21070 lh->add_file_name (cur_file, dindex, mod_time, length);
debd256d 21071 }
c906108c 21072 break;
d0c6ba3d 21073 case DW_LNE_set_discriminator:
6f77053d
PA
21074 {
21075 /* The discriminator is not interesting to the
21076 debugger; just ignore it. We still need to
21077 check its value though:
21078 if there are consecutive entries for the same
21079 (non-prologue) line we want to coalesce them.
21080 PR 17276. */
21081 unsigned int discr
21082 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21083 line_ptr += bytes_read;
21084
21085 state_machine.handle_set_discriminator (discr);
21086 }
d0c6ba3d 21087 break;
c906108c 21088 default:
b98664d3 21089 complaint (_("mangled .debug_line section"));
debd256d 21090 return;
c906108c 21091 }
a8c50c1f
DJ
21092 /* Make sure that we parsed the extended op correctly. If e.g.
21093 we expected a different address size than the producer used,
21094 we may have read the wrong number of bytes. */
21095 if (line_ptr != extended_end)
21096 {
b98664d3 21097 complaint (_("mangled .debug_line section"));
a8c50c1f
DJ
21098 return;
21099 }
c906108c
SS
21100 break;
21101 case DW_LNS_copy:
6f77053d 21102 state_machine.handle_copy ();
c906108c
SS
21103 break;
21104 case DW_LNS_advance_pc:
2dc7f7b3
TT
21105 {
21106 CORE_ADDR adjust
21107 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
2dc7f7b3 21108 line_ptr += bytes_read;
6f77053d
PA
21109
21110 state_machine.handle_advance_pc (adjust);
2dc7f7b3 21111 }
c906108c
SS
21112 break;
21113 case DW_LNS_advance_line:
a05a36a5
DE
21114 {
21115 int line_delta
21116 = read_signed_leb128 (abfd, line_ptr, &bytes_read);
a05a36a5 21117 line_ptr += bytes_read;
6f77053d
PA
21118
21119 state_machine.handle_advance_line (line_delta);
a05a36a5 21120 }
c906108c
SS
21121 break;
21122 case DW_LNS_set_file:
d9b3de22 21123 {
6f77053d 21124 file_name_index file
ecfb656c
PA
21125 = (file_name_index) read_unsigned_leb128 (abfd, line_ptr,
21126 &bytes_read);
d9b3de22 21127 line_ptr += bytes_read;
8c43009f 21128
6f77053d 21129 state_machine.handle_set_file (file);
d9b3de22 21130 }
c906108c
SS
21131 break;
21132 case DW_LNS_set_column:
0ad93d4f 21133 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
c906108c
SS
21134 line_ptr += bytes_read;
21135 break;
21136 case DW_LNS_negate_stmt:
6f77053d 21137 state_machine.handle_negate_stmt ();
c906108c
SS
21138 break;
21139 case DW_LNS_set_basic_block:
c906108c 21140 break;
c2c6d25f
JM
21141 /* Add to the address register of the state machine the
21142 address increment value corresponding to special opcode
a738430d
MK
21143 255. I.e., this value is scaled by the minimum
21144 instruction length since special opcode 255 would have
b021a221 21145 scaled the increment. */
c906108c 21146 case DW_LNS_const_add_pc:
6f77053d 21147 state_machine.handle_const_add_pc ();
c906108c
SS
21148 break;
21149 case DW_LNS_fixed_advance_pc:
3e29f34a 21150 {
6f77053d 21151 CORE_ADDR addr_adj = read_2_bytes (abfd, line_ptr);
3e29f34a 21152 line_ptr += 2;
6f77053d
PA
21153
21154 state_machine.handle_fixed_advance_pc (addr_adj);
3e29f34a 21155 }
c906108c 21156 break;
9aa1fe7e 21157 default:
a738430d
MK
21158 {
21159 /* Unknown standard opcode, ignore it. */
9aa1fe7e 21160 int i;
a738430d 21161
debd256d 21162 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
9aa1fe7e
GK
21163 {
21164 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21165 line_ptr += bytes_read;
21166 }
21167 }
c906108c
SS
21168 }
21169 }
d9b3de22
DE
21170
21171 if (!end_sequence)
21172 dwarf2_debug_line_missing_end_sequence_complaint ();
21173
21174 /* We got a DW_LNE_end_sequence (or we ran off the end of the buffer,
21175 in which case we still finish recording the last line). */
6f77053d 21176 state_machine.record_line (true);
c906108c 21177 }
f3f5162e
DE
21178}
21179
21180/* Decode the Line Number Program (LNP) for the given line_header
21181 structure and CU. The actual information extracted and the type
21182 of structures created from the LNP depends on the value of PST.
21183
21184 1. If PST is NULL, then this procedure uses the data from the program
21185 to create all necessary symbol tables, and their linetables.
21186
21187 2. If PST is not NULL, this procedure reads the program to determine
21188 the list of files included by the unit represented by PST, and
21189 builds all the associated partial symbol tables.
21190
21191 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
21192 It is used for relative paths in the line table.
21193 NOTE: When processing partial symtabs (pst != NULL),
21194 comp_dir == pst->dirname.
21195
21196 NOTE: It is important that psymtabs have the same file name (via strcmp)
21197 as the corresponding symtab. Since COMP_DIR is not used in the name of the
21198 symtab we don't use it in the name of the psymtabs we create.
21199 E.g. expand_line_sal requires this when finding psymtabs to expand.
c3b7b696
YQ
21200 A good testcase for this is mb-inline.exp.
21201
527f3840
JK
21202 LOWPC is the lowest address in CU (or 0 if not known).
21203
21204 Boolean DECODE_MAPPING specifies we need to fully decode .debug_line
21205 for its PC<->lines mapping information. Otherwise only the filename
21206 table is read in. */
f3f5162e
DE
21207
21208static void
21209dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
c3b7b696 21210 struct dwarf2_cu *cu, struct partial_symtab *pst,
527f3840 21211 CORE_ADDR lowpc, int decode_mapping)
f3f5162e 21212{
518817b3 21213 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
f3f5162e 21214 const int decode_for_pst_p = (pst != NULL);
f3f5162e 21215
527f3840
JK
21216 if (decode_mapping)
21217 dwarf_decode_lines_1 (lh, cu, decode_for_pst_p, lowpc);
aaa75496
JB
21218
21219 if (decode_for_pst_p)
21220 {
21221 int file_index;
21222
21223 /* Now that we're done scanning the Line Header Program, we can
21224 create the psymtab of each included file. */
fff8551c 21225 for (file_index = 0; file_index < lh->file_names.size (); file_index++)
aaa75496
JB
21226 if (lh->file_names[file_index].included_p == 1)
21227 {
c89b44cd 21228 gdb::unique_xmalloc_ptr<char> name_holder;
d521ce57 21229 const char *include_name =
c89b44cd
TT
21230 psymtab_include_file_name (lh, file_index, pst, comp_dir,
21231 &name_holder);
c6da4cef 21232 if (include_name != NULL)
aaa75496
JB
21233 dwarf2_create_include_psymtab (include_name, pst, objfile);
21234 }
21235 }
cb1df416
DJ
21236 else
21237 {
21238 /* Make sure a symtab is created for every file, even files
21239 which contain only variables (i.e. no code with associated
21240 line numbers). */
c24bdb02
KS
21241 buildsym_compunit *builder = cu->get_builder ();
21242 struct compunit_symtab *cust = builder->get_compunit_symtab ();
cb1df416 21243 int i;
cb1df416 21244
fff8551c 21245 for (i = 0; i < lh->file_names.size (); i++)
cb1df416 21246 {
8c43009f 21247 file_entry &fe = lh->file_names[i];
9a619af0 21248
804d2729 21249 dwarf2_start_subfile (cu, fe.name, fe.include_dir (lh));
cb1df416 21250
c24bdb02 21251 if (builder->get_current_subfile ()->symtab == NULL)
43f3e411 21252 {
c24bdb02 21253 builder->get_current_subfile ()->symtab
804d2729 21254 = allocate_symtab (cust,
c24bdb02 21255 builder->get_current_subfile ()->name);
43f3e411 21256 }
c24bdb02 21257 fe.symtab = builder->get_current_subfile ()->symtab;
cb1df416
DJ
21258 }
21259 }
c906108c
SS
21260}
21261
21262/* Start a subfile for DWARF. FILENAME is the name of the file and
21263 DIRNAME the name of the source directory which contains FILENAME
4d663531 21264 or NULL if not known.
c906108c
SS
21265 This routine tries to keep line numbers from identical absolute and
21266 relative file names in a common subfile.
21267
21268 Using the `list' example from the GDB testsuite, which resides in
21269 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
21270 of /srcdir/list0.c yields the following debugging information for list0.c:
21271
c5aa993b 21272 DW_AT_name: /srcdir/list0.c
4d663531 21273 DW_AT_comp_dir: /compdir
357e46e7 21274 files.files[0].name: list0.h
c5aa993b 21275 files.files[0].dir: /srcdir
357e46e7 21276 files.files[1].name: list0.c
c5aa993b 21277 files.files[1].dir: /srcdir
c906108c
SS
21278
21279 The line number information for list0.c has to end up in a single
4f1520fb
FR
21280 subfile, so that `break /srcdir/list0.c:1' works as expected.
21281 start_subfile will ensure that this happens provided that we pass the
21282 concatenation of files.files[1].dir and files.files[1].name as the
21283 subfile's name. */
c906108c
SS
21284
21285static void
804d2729
TT
21286dwarf2_start_subfile (struct dwarf2_cu *cu, const char *filename,
21287 const char *dirname)
c906108c 21288{
d521ce57 21289 char *copy = NULL;
4f1520fb 21290
4d663531 21291 /* In order not to lose the line information directory,
4f1520fb
FR
21292 we concatenate it to the filename when it makes sense.
21293 Note that the Dwarf3 standard says (speaking of filenames in line
21294 information): ``The directory index is ignored for file names
21295 that represent full path names''. Thus ignoring dirname in the
21296 `else' branch below isn't an issue. */
c906108c 21297
d5166ae1 21298 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
d521ce57
TT
21299 {
21300 copy = concat (dirname, SLASH_STRING, filename, (char *)NULL);
21301 filename = copy;
21302 }
c906108c 21303
c24bdb02 21304 cu->get_builder ()->start_subfile (filename);
4f1520fb 21305
d521ce57
TT
21306 if (copy != NULL)
21307 xfree (copy);
c906108c
SS
21308}
21309
804d2729
TT
21310/* Start a symtab for DWARF. NAME, COMP_DIR, LOW_PC are passed to the
21311 buildsym_compunit constructor. */
f4dc4d17 21312
c24bdb02
KS
21313struct compunit_symtab *
21314dwarf2_cu::start_symtab (const char *name, const char *comp_dir,
21315 CORE_ADDR low_pc)
f4dc4d17 21316{
c24bdb02 21317 gdb_assert (m_builder == nullptr);
43f3e411 21318
c24bdb02
KS
21319 m_builder.reset (new struct buildsym_compunit
21320 (per_cu->dwarf2_per_objfile->objfile,
21321 name, comp_dir, language, low_pc));
93b8bea4 21322
c24bdb02 21323 list_in_scope = get_builder ()->get_file_symbols ();
804d2729 21324
c24bdb02
KS
21325 get_builder ()->record_debugformat ("DWARF 2");
21326 get_builder ()->record_producer (producer);
f4dc4d17 21327
c24bdb02 21328 processing_has_namespace_info = false;
43f3e411 21329
c24bdb02 21330 return get_builder ()->get_compunit_symtab ();
f4dc4d17
DE
21331}
21332
4c2df51b
DJ
21333static void
21334var_decode_location (struct attribute *attr, struct symbol *sym,
e7c27a73 21335 struct dwarf2_cu *cu)
4c2df51b 21336{
518817b3 21337 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
e7c27a73
DJ
21338 struct comp_unit_head *cu_header = &cu->header;
21339
4c2df51b
DJ
21340 /* NOTE drow/2003-01-30: There used to be a comment and some special
21341 code here to turn a symbol with DW_AT_external and a
21342 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
21343 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
21344 with some versions of binutils) where shared libraries could have
21345 relocations against symbols in their debug information - the
21346 minimal symbol would have the right address, but the debug info
21347 would not. It's no longer necessary, because we will explicitly
21348 apply relocations when we read in the debug information now. */
21349
21350 /* A DW_AT_location attribute with no contents indicates that a
21351 variable has been optimized away. */
21352 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
21353 {
f1e6e072 21354 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
4c2df51b
DJ
21355 return;
21356 }
21357
21358 /* Handle one degenerate form of location expression specially, to
21359 preserve GDB's previous behavior when section offsets are
3019eac3
DE
21360 specified. If this is just a DW_OP_addr or DW_OP_GNU_addr_index
21361 then mark this symbol as LOC_STATIC. */
4c2df51b
DJ
21362
21363 if (attr_form_is_block (attr)
3019eac3
DE
21364 && ((DW_BLOCK (attr)->data[0] == DW_OP_addr
21365 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size)
21366 || (DW_BLOCK (attr)->data[0] == DW_OP_GNU_addr_index
21367 && (DW_BLOCK (attr)->size
21368 == 1 + leb128_size (&DW_BLOCK (attr)->data[1])))))
4c2df51b 21369 {
891d2f0b 21370 unsigned int dummy;
4c2df51b 21371
3019eac3
DE
21372 if (DW_BLOCK (attr)->data[0] == DW_OP_addr)
21373 SYMBOL_VALUE_ADDRESS (sym) =
21374 read_address (objfile->obfd, DW_BLOCK (attr)->data + 1, cu, &dummy);
21375 else
21376 SYMBOL_VALUE_ADDRESS (sym) =
21377 read_addr_index_from_leb128 (cu, DW_BLOCK (attr)->data + 1, &dummy);
f1e6e072 21378 SYMBOL_ACLASS_INDEX (sym) = LOC_STATIC;
4c2df51b
DJ
21379 fixup_symbol_section (sym, objfile);
21380 SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (objfile->section_offsets,
21381 SYMBOL_SECTION (sym));
4c2df51b
DJ
21382 return;
21383 }
21384
21385 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
21386 expression evaluator, and use LOC_COMPUTED only when necessary
21387 (i.e. when the value of a register or memory location is
21388 referenced, or a thread-local block, etc.). Then again, it might
21389 not be worthwhile. I'm assuming that it isn't unless performance
21390 or memory numbers show me otherwise. */
21391
f1e6e072 21392 dwarf2_symbol_mark_computed (attr, sym, cu, 0);
8be455d7 21393
f1e6e072 21394 if (SYMBOL_COMPUTED_OPS (sym)->location_has_loclist)
9068261f 21395 cu->has_loclist = true;
4c2df51b
DJ
21396}
21397
c906108c
SS
21398/* Given a pointer to a DWARF information entry, figure out if we need
21399 to make a symbol table entry for it, and if so, create a new entry
21400 and return a pointer to it.
21401 If TYPE is NULL, determine symbol type from the die, otherwise
34eaf542
TT
21402 used the passed type.
21403 If SPACE is not NULL, use it to hold the new symbol. If it is
21404 NULL, allocate a new symbol on the objfile's obstack. */
c906108c
SS
21405
21406static struct symbol *
5e2db402
TT
21407new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
21408 struct symbol *space)
c906108c 21409{
518817b3
SM
21410 struct dwarf2_per_objfile *dwarf2_per_objfile
21411 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 21412 struct objfile *objfile = dwarf2_per_objfile->objfile;
3e29f34a 21413 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c 21414 struct symbol *sym = NULL;
15d034d0 21415 const char *name;
c906108c
SS
21416 struct attribute *attr = NULL;
21417 struct attribute *attr2 = NULL;
e142c38c 21418 CORE_ADDR baseaddr;
e37fd15a
SW
21419 struct pending **list_to_add = NULL;
21420
edb3359d 21421 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
e142c38c
DJ
21422
21423 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 21424
94af9270 21425 name = dwarf2_name (die, cu);
c906108c
SS
21426 if (name)
21427 {
94af9270 21428 const char *linkagename;
34eaf542 21429 int suppress_add = 0;
94af9270 21430
34eaf542
TT
21431 if (space)
21432 sym = space;
21433 else
e623cf5d 21434 sym = allocate_symbol (objfile);
c906108c 21435 OBJSTAT (objfile, n_syms++);
2de7ced7
DJ
21436
21437 /* Cache this symbol's name and the name's demangled form (if any). */
f85f34ed 21438 SYMBOL_SET_LANGUAGE (sym, cu->language, &objfile->objfile_obstack);
94af9270
KS
21439 linkagename = dwarf2_physname (name, die, cu);
21440 SYMBOL_SET_NAMES (sym, linkagename, strlen (linkagename), 0, objfile);
c906108c 21441
f55ee35c
JK
21442 /* Fortran does not have mangling standard and the mangling does differ
21443 between gfortran, iFort etc. */
21444 if (cu->language == language_fortran
b250c185 21445 && symbol_get_demangled_name (&(sym->ginfo)) == NULL)
29df156d 21446 symbol_set_demangled_name (&(sym->ginfo),
cfc594ee 21447 dwarf2_full_name (name, die, cu),
29df156d 21448 NULL);
f55ee35c 21449
c906108c 21450 /* Default assumptions.
c5aa993b 21451 Use the passed type or decode it from the die. */
176620f1 21452 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
f1e6e072 21453 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
c906108c
SS
21454 if (type != NULL)
21455 SYMBOL_TYPE (sym) = type;
21456 else
e7c27a73 21457 SYMBOL_TYPE (sym) = die_type (die, cu);
edb3359d
DJ
21458 attr = dwarf2_attr (die,
21459 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
21460 cu);
c906108c
SS
21461 if (attr)
21462 {
21463 SYMBOL_LINE (sym) = DW_UNSND (attr);
21464 }
cb1df416 21465
edb3359d
DJ
21466 attr = dwarf2_attr (die,
21467 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
21468 cu);
cb1df416
DJ
21469 if (attr)
21470 {
ecfb656c 21471 file_name_index file_index = (file_name_index) DW_UNSND (attr);
8c43009f 21472 struct file_entry *fe;
9a619af0 21473
ecfb656c
PA
21474 if (cu->line_header != NULL)
21475 fe = cu->line_header->file_name_at (file_index);
8c43009f
PA
21476 else
21477 fe = NULL;
21478
21479 if (fe == NULL)
b98664d3 21480 complaint (_("file index out of range"));
8c43009f
PA
21481 else
21482 symbol_set_symtab (sym, fe->symtab);
cb1df416
DJ
21483 }
21484
c906108c
SS
21485 switch (die->tag)
21486 {
21487 case DW_TAG_label:
e142c38c 21488 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
c906108c 21489 if (attr)
3e29f34a
MR
21490 {
21491 CORE_ADDR addr;
21492
21493 addr = attr_value_as_address (attr);
21494 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr + baseaddr);
21495 SYMBOL_VALUE_ADDRESS (sym) = addr;
21496 }
0f5238ed
TT
21497 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
21498 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
f1e6e072 21499 SYMBOL_ACLASS_INDEX (sym) = LOC_LABEL;
d3cb6808 21500 add_symbol_to_list (sym, cu->list_in_scope);
c906108c
SS
21501 break;
21502 case DW_TAG_subprogram:
21503 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
21504 finish_block. */
f1e6e072 21505 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
e142c38c 21506 attr2 = dwarf2_attr (die, DW_AT_external, cu);
2cfa0c8d
JB
21507 if ((attr2 && (DW_UNSND (attr2) != 0))
21508 || cu->language == language_ada)
c906108c 21509 {
2cfa0c8d
JB
21510 /* Subprograms marked external are stored as a global symbol.
21511 Ada subprograms, whether marked external or not, are always
21512 stored as a global symbol, because we want to be able to
21513 access them globally. For instance, we want to be able
21514 to break on a nested subprogram without having to
21515 specify the context. */
c24bdb02 21516 list_to_add = cu->get_builder ()->get_global_symbols ();
c906108c
SS
21517 }
21518 else
21519 {
e37fd15a 21520 list_to_add = cu->list_in_scope;
c906108c
SS
21521 }
21522 break;
edb3359d
DJ
21523 case DW_TAG_inlined_subroutine:
21524 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
21525 finish_block. */
f1e6e072 21526 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
edb3359d 21527 SYMBOL_INLINED (sym) = 1;
481860b3 21528 list_to_add = cu->list_in_scope;
edb3359d 21529 break;
34eaf542
TT
21530 case DW_TAG_template_value_param:
21531 suppress_add = 1;
21532 /* Fall through. */
72929c62 21533 case DW_TAG_constant:
c906108c 21534 case DW_TAG_variable:
254e6b9e 21535 case DW_TAG_member:
0963b4bd
MS
21536 /* Compilation with minimal debug info may result in
21537 variables with missing type entries. Change the
21538 misleading `void' type to something sensible. */
c906108c 21539 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
46a4882b 21540 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_int;
64c50499 21541
e142c38c 21542 attr = dwarf2_attr (die, DW_AT_const_value, cu);
254e6b9e
DE
21543 /* In the case of DW_TAG_member, we should only be called for
21544 static const members. */
21545 if (die->tag == DW_TAG_member)
21546 {
3863f96c
DE
21547 /* dwarf2_add_field uses die_is_declaration,
21548 so we do the same. */
254e6b9e
DE
21549 gdb_assert (die_is_declaration (die, cu));
21550 gdb_assert (attr);
21551 }
c906108c
SS
21552 if (attr)
21553 {
e7c27a73 21554 dwarf2_const_value (attr, sym, cu);
e142c38c 21555 attr2 = dwarf2_attr (die, DW_AT_external, cu);
e37fd15a 21556 if (!suppress_add)
34eaf542
TT
21557 {
21558 if (attr2 && (DW_UNSND (attr2) != 0))
c24bdb02 21559 list_to_add = cu->get_builder ()->get_global_symbols ();
34eaf542 21560 else
e37fd15a 21561 list_to_add = cu->list_in_scope;
34eaf542 21562 }
c906108c
SS
21563 break;
21564 }
e142c38c 21565 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
21566 if (attr)
21567 {
e7c27a73 21568 var_decode_location (attr, sym, cu);
e142c38c 21569 attr2 = dwarf2_attr (die, DW_AT_external, cu);
4357ac6c
TT
21570
21571 /* Fortran explicitly imports any global symbols to the local
21572 scope by DW_TAG_common_block. */
21573 if (cu->language == language_fortran && die->parent
21574 && die->parent->tag == DW_TAG_common_block)
21575 attr2 = NULL;
21576
caac4577
JG
21577 if (SYMBOL_CLASS (sym) == LOC_STATIC
21578 && SYMBOL_VALUE_ADDRESS (sym) == 0
21579 && !dwarf2_per_objfile->has_section_at_zero)
21580 {
21581 /* When a static variable is eliminated by the linker,
21582 the corresponding debug information is not stripped
21583 out, but the variable address is set to null;
21584 do not add such variables into symbol table. */
21585 }
21586 else if (attr2 && (DW_UNSND (attr2) != 0))
1c809c68 21587 {
f55ee35c
JK
21588 /* Workaround gfortran PR debug/40040 - it uses
21589 DW_AT_location for variables in -fPIC libraries which may
21590 get overriden by other libraries/executable and get
21591 a different address. Resolve it by the minimal symbol
21592 which may come from inferior's executable using copy
21593 relocation. Make this workaround only for gfortran as for
21594 other compilers GDB cannot guess the minimal symbol
21595 Fortran mangling kind. */
21596 if (cu->language == language_fortran && die->parent
21597 && die->parent->tag == DW_TAG_module
21598 && cu->producer
28586665 21599 && startswith (cu->producer, "GNU Fortran"))
f1e6e072 21600 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
f55ee35c 21601
1c809c68
TT
21602 /* A variable with DW_AT_external is never static,
21603 but it may be block-scoped. */
804d2729 21604 list_to_add
c24bdb02
KS
21605 = ((cu->list_in_scope
21606 == cu->get_builder ()->get_file_symbols ())
21607 ? cu->get_builder ()->get_global_symbols ()
804d2729 21608 : cu->list_in_scope);
1c809c68 21609 }
c906108c 21610 else
e37fd15a 21611 list_to_add = cu->list_in_scope;
c906108c
SS
21612 }
21613 else
21614 {
21615 /* We do not know the address of this symbol.
c5aa993b
JM
21616 If it is an external symbol and we have type information
21617 for it, enter the symbol as a LOC_UNRESOLVED symbol.
21618 The address of the variable will then be determined from
21619 the minimal symbol table whenever the variable is
21620 referenced. */
e142c38c 21621 attr2 = dwarf2_attr (die, DW_AT_external, cu);
0971de02
TT
21622
21623 /* Fortran explicitly imports any global symbols to the local
21624 scope by DW_TAG_common_block. */
21625 if (cu->language == language_fortran && die->parent
21626 && die->parent->tag == DW_TAG_common_block)
21627 {
21628 /* SYMBOL_CLASS doesn't matter here because
21629 read_common_block is going to reset it. */
21630 if (!suppress_add)
21631 list_to_add = cu->list_in_scope;
21632 }
21633 else if (attr2 && (DW_UNSND (attr2) != 0)
21634 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
c906108c 21635 {
0fe7935b
DJ
21636 /* A variable with DW_AT_external is never static, but it
21637 may be block-scoped. */
804d2729 21638 list_to_add
c24bdb02
KS
21639 = ((cu->list_in_scope
21640 == cu->get_builder ()->get_file_symbols ())
21641 ? cu->get_builder ()->get_global_symbols ()
804d2729 21642 : cu->list_in_scope);
0fe7935b 21643
f1e6e072 21644 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
c906108c 21645 }
442ddf59
JK
21646 else if (!die_is_declaration (die, cu))
21647 {
21648 /* Use the default LOC_OPTIMIZED_OUT class. */
21649 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
e37fd15a
SW
21650 if (!suppress_add)
21651 list_to_add = cu->list_in_scope;
442ddf59 21652 }
c906108c
SS
21653 }
21654 break;
21655 case DW_TAG_formal_parameter:
a60f3166
TT
21656 {
21657 /* If we are inside a function, mark this as an argument. If
21658 not, we might be looking at an argument to an inlined function
21659 when we do not have enough information to show inlined frames;
21660 pretend it's a local variable in that case so that the user can
21661 still see it. */
804d2729 21662 struct context_stack *curr
c24bdb02 21663 = cu->get_builder ()->get_current_context_stack ();
a60f3166
TT
21664 if (curr != nullptr && curr->name != nullptr)
21665 SYMBOL_IS_ARGUMENT (sym) = 1;
21666 attr = dwarf2_attr (die, DW_AT_location, cu);
21667 if (attr)
21668 {
21669 var_decode_location (attr, sym, cu);
21670 }
21671 attr = dwarf2_attr (die, DW_AT_const_value, cu);
21672 if (attr)
21673 {
21674 dwarf2_const_value (attr, sym, cu);
21675 }
f346a30d 21676
a60f3166
TT
21677 list_to_add = cu->list_in_scope;
21678 }
c906108c
SS
21679 break;
21680 case DW_TAG_unspecified_parameters:
21681 /* From varargs functions; gdb doesn't seem to have any
21682 interest in this information, so just ignore it for now.
21683 (FIXME?) */
21684 break;
34eaf542
TT
21685 case DW_TAG_template_type_param:
21686 suppress_add = 1;
21687 /* Fall through. */
c906108c 21688 case DW_TAG_class_type:
680b30c7 21689 case DW_TAG_interface_type:
c906108c
SS
21690 case DW_TAG_structure_type:
21691 case DW_TAG_union_type:
72019c9c 21692 case DW_TAG_set_type:
c906108c 21693 case DW_TAG_enumeration_type:
f1e6e072 21694 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
176620f1 21695 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
c906108c 21696
63d06c5c 21697 {
9c37b5ae 21698 /* NOTE: carlton/2003-11-10: C++ class symbols shouldn't
63d06c5c
DC
21699 really ever be static objects: otherwise, if you try
21700 to, say, break of a class's method and you're in a file
21701 which doesn't mention that class, it won't work unless
21702 the check for all static symbols in lookup_symbol_aux
21703 saves you. See the OtherFileClass tests in
21704 gdb.c++/namespace.exp. */
21705
e37fd15a 21706 if (!suppress_add)
34eaf542 21707 {
c24bdb02 21708 buildsym_compunit *builder = cu->get_builder ();
804d2729 21709 list_to_add
c24bdb02 21710 = (cu->list_in_scope == builder->get_file_symbols ()
804d2729 21711 && cu->language == language_cplus
c24bdb02 21712 ? builder->get_global_symbols ()
804d2729 21713 : cu->list_in_scope);
63d06c5c 21714
64382290 21715 /* The semantics of C++ state that "struct foo {
9c37b5ae 21716 ... }" also defines a typedef for "foo". */
64382290 21717 if (cu->language == language_cplus
45280282 21718 || cu->language == language_ada
c44af4eb
TT
21719 || cu->language == language_d
21720 || cu->language == language_rust)
64382290
TT
21721 {
21722 /* The symbol's name is already allocated along
21723 with this objfile, so we don't need to
21724 duplicate it for the type. */
21725 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
21726 TYPE_NAME (SYMBOL_TYPE (sym)) = SYMBOL_SEARCH_NAME (sym);
21727 }
63d06c5c
DC
21728 }
21729 }
c906108c
SS
21730 break;
21731 case DW_TAG_typedef:
f1e6e072 21732 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
63d06c5c 21733 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 21734 list_to_add = cu->list_in_scope;
63d06c5c 21735 break;
c906108c 21736 case DW_TAG_base_type:
a02abb62 21737 case DW_TAG_subrange_type:
f1e6e072 21738 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
176620f1 21739 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 21740 list_to_add = cu->list_in_scope;
c906108c
SS
21741 break;
21742 case DW_TAG_enumerator:
e142c38c 21743 attr = dwarf2_attr (die, DW_AT_const_value, cu);
c906108c
SS
21744 if (attr)
21745 {
e7c27a73 21746 dwarf2_const_value (attr, sym, cu);
c906108c 21747 }
63d06c5c
DC
21748 {
21749 /* NOTE: carlton/2003-11-10: See comment above in the
21750 DW_TAG_class_type, etc. block. */
21751
804d2729 21752 list_to_add
c24bdb02 21753 = (cu->list_in_scope == cu->get_builder ()->get_file_symbols ()
804d2729 21754 && cu->language == language_cplus
c24bdb02 21755 ? cu->get_builder ()->get_global_symbols ()
804d2729 21756 : cu->list_in_scope);
63d06c5c 21757 }
c906108c 21758 break;
74921315 21759 case DW_TAG_imported_declaration:
5c4e30ca 21760 case DW_TAG_namespace:
f1e6e072 21761 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
c24bdb02 21762 list_to_add = cu->get_builder ()->get_global_symbols ();
5c4e30ca 21763 break;
530e8392
KB
21764 case DW_TAG_module:
21765 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21766 SYMBOL_DOMAIN (sym) = MODULE_DOMAIN;
c24bdb02 21767 list_to_add = cu->get_builder ()->get_global_symbols ();
530e8392 21768 break;
4357ac6c 21769 case DW_TAG_common_block:
f1e6e072 21770 SYMBOL_ACLASS_INDEX (sym) = LOC_COMMON_BLOCK;
4357ac6c 21771 SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
d3cb6808 21772 add_symbol_to_list (sym, cu->list_in_scope);
4357ac6c 21773 break;
c906108c
SS
21774 default:
21775 /* Not a tag we recognize. Hopefully we aren't processing
21776 trash data, but since we must specifically ignore things
21777 we don't recognize, there is nothing else we should do at
0963b4bd 21778 this point. */
b98664d3 21779 complaint (_("unsupported tag: '%s'"),
4d3c2250 21780 dwarf_tag_name (die->tag));
c906108c
SS
21781 break;
21782 }
df8a16a1 21783
e37fd15a
SW
21784 if (suppress_add)
21785 {
21786 sym->hash_next = objfile->template_symbols;
21787 objfile->template_symbols = sym;
21788 list_to_add = NULL;
21789 }
21790
21791 if (list_to_add != NULL)
d3cb6808 21792 add_symbol_to_list (sym, list_to_add);
e37fd15a 21793
df8a16a1
DJ
21794 /* For the benefit of old versions of GCC, check for anonymous
21795 namespaces based on the demangled name. */
4d4ec4e5 21796 if (!cu->processing_has_namespace_info
94af9270 21797 && cu->language == language_cplus)
c24bdb02 21798 cp_scan_for_anonymous_namespaces (cu->get_builder (), sym, objfile);
c906108c
SS
21799 }
21800 return (sym);
21801}
21802
98bfdba5
PA
21803/* Given an attr with a DW_FORM_dataN value in host byte order,
21804 zero-extend it as appropriate for the symbol's type. The DWARF
21805 standard (v4) is not entirely clear about the meaning of using
21806 DW_FORM_dataN for a constant with a signed type, where the type is
21807 wider than the data. The conclusion of a discussion on the DWARF
21808 list was that this is unspecified. We choose to always zero-extend
21809 because that is the interpretation long in use by GCC. */
c906108c 21810
98bfdba5 21811static gdb_byte *
ff39bb5e 21812dwarf2_const_value_data (const struct attribute *attr, struct obstack *obstack,
12df843f 21813 struct dwarf2_cu *cu, LONGEST *value, int bits)
c906108c 21814{
518817b3 21815 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
e17a4113
UW
21816 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
21817 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
98bfdba5
PA
21818 LONGEST l = DW_UNSND (attr);
21819
21820 if (bits < sizeof (*value) * 8)
21821 {
21822 l &= ((LONGEST) 1 << bits) - 1;
21823 *value = l;
21824 }
21825 else if (bits == sizeof (*value) * 8)
21826 *value = l;
21827 else
21828 {
224c3ddb 21829 gdb_byte *bytes = (gdb_byte *) obstack_alloc (obstack, bits / 8);
98bfdba5
PA
21830 store_unsigned_integer (bytes, bits / 8, byte_order, l);
21831 return bytes;
21832 }
21833
21834 return NULL;
21835}
21836
21837/* Read a constant value from an attribute. Either set *VALUE, or if
21838 the value does not fit in *VALUE, set *BYTES - either already
21839 allocated on the objfile obstack, or newly allocated on OBSTACK,
21840 or, set *BATON, if we translated the constant to a location
21841 expression. */
21842
21843static void
ff39bb5e 21844dwarf2_const_value_attr (const struct attribute *attr, struct type *type,
98bfdba5
PA
21845 const char *name, struct obstack *obstack,
21846 struct dwarf2_cu *cu,
d521ce57 21847 LONGEST *value, const gdb_byte **bytes,
98bfdba5
PA
21848 struct dwarf2_locexpr_baton **baton)
21849{
518817b3 21850 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
98bfdba5 21851 struct comp_unit_head *cu_header = &cu->header;
c906108c 21852 struct dwarf_block *blk;
98bfdba5
PA
21853 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
21854 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
21855
21856 *value = 0;
21857 *bytes = NULL;
21858 *baton = NULL;
c906108c
SS
21859
21860 switch (attr->form)
21861 {
21862 case DW_FORM_addr:
3019eac3 21863 case DW_FORM_GNU_addr_index:
ac56253d 21864 {
ac56253d
TT
21865 gdb_byte *data;
21866
98bfdba5
PA
21867 if (TYPE_LENGTH (type) != cu_header->addr_size)
21868 dwarf2_const_value_length_mismatch_complaint (name,
ac56253d 21869 cu_header->addr_size,
98bfdba5 21870 TYPE_LENGTH (type));
ac56253d
TT
21871 /* Symbols of this form are reasonably rare, so we just
21872 piggyback on the existing location code rather than writing
21873 a new implementation of symbol_computed_ops. */
8d749320 21874 *baton = XOBNEW (obstack, struct dwarf2_locexpr_baton);
98bfdba5
PA
21875 (*baton)->per_cu = cu->per_cu;
21876 gdb_assert ((*baton)->per_cu);
ac56253d 21877
98bfdba5 21878 (*baton)->size = 2 + cu_header->addr_size;
224c3ddb 21879 data = (gdb_byte *) obstack_alloc (obstack, (*baton)->size);
98bfdba5 21880 (*baton)->data = data;
ac56253d
TT
21881
21882 data[0] = DW_OP_addr;
21883 store_unsigned_integer (&data[1], cu_header->addr_size,
21884 byte_order, DW_ADDR (attr));
21885 data[cu_header->addr_size + 1] = DW_OP_stack_value;
ac56253d 21886 }
c906108c 21887 break;
4ac36638 21888 case DW_FORM_string:
93b5768b 21889 case DW_FORM_strp:
3019eac3 21890 case DW_FORM_GNU_str_index:
36586728 21891 case DW_FORM_GNU_strp_alt:
98bfdba5
PA
21892 /* DW_STRING is already allocated on the objfile obstack, point
21893 directly to it. */
d521ce57 21894 *bytes = (const gdb_byte *) DW_STRING (attr);
93b5768b 21895 break;
c906108c
SS
21896 case DW_FORM_block1:
21897 case DW_FORM_block2:
21898 case DW_FORM_block4:
21899 case DW_FORM_block:
2dc7f7b3 21900 case DW_FORM_exprloc:
0224619f 21901 case DW_FORM_data16:
c906108c 21902 blk = DW_BLOCK (attr);
98bfdba5
PA
21903 if (TYPE_LENGTH (type) != blk->size)
21904 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
21905 TYPE_LENGTH (type));
21906 *bytes = blk->data;
c906108c 21907 break;
2df3850c
JM
21908
21909 /* The DW_AT_const_value attributes are supposed to carry the
21910 symbol's value "represented as it would be on the target
21911 architecture." By the time we get here, it's already been
21912 converted to host endianness, so we just need to sign- or
21913 zero-extend it as appropriate. */
21914 case DW_FORM_data1:
3aef2284 21915 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 8);
2df3850c 21916 break;
c906108c 21917 case DW_FORM_data2:
3aef2284 21918 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 16);
2df3850c 21919 break;
c906108c 21920 case DW_FORM_data4:
3aef2284 21921 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 32);
2df3850c 21922 break;
c906108c 21923 case DW_FORM_data8:
3aef2284 21924 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 64);
2df3850c
JM
21925 break;
21926
c906108c 21927 case DW_FORM_sdata:
663c44ac 21928 case DW_FORM_implicit_const:
98bfdba5 21929 *value = DW_SND (attr);
2df3850c
JM
21930 break;
21931
c906108c 21932 case DW_FORM_udata:
98bfdba5 21933 *value = DW_UNSND (attr);
c906108c 21934 break;
2df3850c 21935
c906108c 21936 default:
b98664d3 21937 complaint (_("unsupported const value attribute form: '%s'"),
4d3c2250 21938 dwarf_form_name (attr->form));
98bfdba5 21939 *value = 0;
c906108c
SS
21940 break;
21941 }
21942}
21943
2df3850c 21944
98bfdba5
PA
21945/* Copy constant value from an attribute to a symbol. */
21946
2df3850c 21947static void
ff39bb5e 21948dwarf2_const_value (const struct attribute *attr, struct symbol *sym,
98bfdba5 21949 struct dwarf2_cu *cu)
2df3850c 21950{
518817b3 21951 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
12df843f 21952 LONGEST value;
d521ce57 21953 const gdb_byte *bytes;
98bfdba5 21954 struct dwarf2_locexpr_baton *baton;
2df3850c 21955
98bfdba5
PA
21956 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
21957 SYMBOL_PRINT_NAME (sym),
21958 &objfile->objfile_obstack, cu,
21959 &value, &bytes, &baton);
2df3850c 21960
98bfdba5
PA
21961 if (baton != NULL)
21962 {
98bfdba5 21963 SYMBOL_LOCATION_BATON (sym) = baton;
f1e6e072 21964 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
98bfdba5
PA
21965 }
21966 else if (bytes != NULL)
21967 {
21968 SYMBOL_VALUE_BYTES (sym) = bytes;
f1e6e072 21969 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST_BYTES;
98bfdba5
PA
21970 }
21971 else
21972 {
21973 SYMBOL_VALUE (sym) = value;
f1e6e072 21974 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST;
98bfdba5 21975 }
2df3850c
JM
21976}
21977
c906108c
SS
21978/* Return the type of the die in question using its DW_AT_type attribute. */
21979
21980static struct type *
e7c27a73 21981die_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 21982{
c906108c 21983 struct attribute *type_attr;
c906108c 21984
e142c38c 21985 type_attr = dwarf2_attr (die, DW_AT_type, cu);
c906108c
SS
21986 if (!type_attr)
21987 {
518817b3 21988 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c 21989 /* A missing DW_AT_type represents a void type. */
518817b3 21990 return objfile_type (objfile)->builtin_void;
c906108c 21991 }
348e048f 21992
673bfd45 21993 return lookup_die_type (die, type_attr, cu);
c906108c
SS
21994}
21995
b4ba55a1
JB
21996/* True iff CU's producer generates GNAT Ada auxiliary information
21997 that allows to find parallel types through that information instead
21998 of having to do expensive parallel lookups by type name. */
21999
22000static int
22001need_gnat_info (struct dwarf2_cu *cu)
22002{
de4cb04a
JB
22003 /* Assume that the Ada compiler was GNAT, which always produces
22004 the auxiliary information. */
22005 return (cu->language == language_ada);
b4ba55a1
JB
22006}
22007
b4ba55a1
JB
22008/* Return the auxiliary type of the die in question using its
22009 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
22010 attribute is not present. */
22011
22012static struct type *
22013die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
22014{
b4ba55a1 22015 struct attribute *type_attr;
b4ba55a1
JB
22016
22017 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
22018 if (!type_attr)
22019 return NULL;
22020
673bfd45 22021 return lookup_die_type (die, type_attr, cu);
b4ba55a1
JB
22022}
22023
22024/* If DIE has a descriptive_type attribute, then set the TYPE's
22025 descriptive type accordingly. */
22026
22027static void
22028set_descriptive_type (struct type *type, struct die_info *die,
22029 struct dwarf2_cu *cu)
22030{
22031 struct type *descriptive_type = die_descriptive_type (die, cu);
22032
22033 if (descriptive_type)
22034 {
22035 ALLOCATE_GNAT_AUX_TYPE (type);
22036 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
22037 }
22038}
22039
c906108c
SS
22040/* Return the containing type of the die in question using its
22041 DW_AT_containing_type attribute. */
22042
22043static struct type *
e7c27a73 22044die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 22045{
c906108c 22046 struct attribute *type_attr;
518817b3 22047 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c 22048
e142c38c 22049 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
33ac96f0
JK
22050 if (!type_attr)
22051 error (_("Dwarf Error: Problem turning containing type into gdb type "
518817b3 22052 "[in module %s]"), objfile_name (objfile));
33ac96f0 22053
673bfd45 22054 return lookup_die_type (die, type_attr, cu);
c906108c
SS
22055}
22056
ac9ec31b
DE
22057/* Return an error marker type to use for the ill formed type in DIE/CU. */
22058
22059static struct type *
22060build_error_marker_type (struct dwarf2_cu *cu, struct die_info *die)
22061{
518817b3
SM
22062 struct dwarf2_per_objfile *dwarf2_per_objfile
22063 = cu->per_cu->dwarf2_per_objfile;
ac9ec31b 22064 struct objfile *objfile = dwarf2_per_objfile->objfile;
528e1572 22065 char *saved;
ac9ec31b 22066
528e1572
SM
22067 std::string message
22068 = string_printf (_("<unknown type in %s, CU %s, DIE %s>"),
22069 objfile_name (objfile),
22070 sect_offset_str (cu->header.sect_off),
22071 sect_offset_str (die->sect_off));
224c3ddb 22072 saved = (char *) obstack_copy0 (&objfile->objfile_obstack,
528e1572 22073 message.c_str (), message.length ());
ac9ec31b 22074
19f392bc 22075 return init_type (objfile, TYPE_CODE_ERROR, 0, saved);
ac9ec31b
DE
22076}
22077
673bfd45 22078/* Look up the type of DIE in CU using its type attribute ATTR.
ac9ec31b
DE
22079 ATTR must be one of: DW_AT_type, DW_AT_GNAT_descriptive_type,
22080 DW_AT_containing_type.
673bfd45
DE
22081 If there is no type substitute an error marker. */
22082
c906108c 22083static struct type *
ff39bb5e 22084lookup_die_type (struct die_info *die, const struct attribute *attr,
673bfd45 22085 struct dwarf2_cu *cu)
c906108c 22086{
518817b3
SM
22087 struct dwarf2_per_objfile *dwarf2_per_objfile
22088 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 22089 struct objfile *objfile = dwarf2_per_objfile->objfile;
f792889a
DJ
22090 struct type *this_type;
22091
ac9ec31b
DE
22092 gdb_assert (attr->name == DW_AT_type
22093 || attr->name == DW_AT_GNAT_descriptive_type
22094 || attr->name == DW_AT_containing_type);
22095
673bfd45
DE
22096 /* First see if we have it cached. */
22097
36586728
TT
22098 if (attr->form == DW_FORM_GNU_ref_alt)
22099 {
22100 struct dwarf2_per_cu_data *per_cu;
9c541725 22101 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
36586728 22102
ed2dc618
SM
22103 per_cu = dwarf2_find_containing_comp_unit (sect_off, 1,
22104 dwarf2_per_objfile);
9c541725 22105 this_type = get_die_type_at_offset (sect_off, per_cu);
36586728 22106 }
7771576e 22107 else if (attr_form_is_ref (attr))
673bfd45 22108 {
9c541725 22109 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
673bfd45 22110
9c541725 22111 this_type = get_die_type_at_offset (sect_off, cu->per_cu);
673bfd45 22112 }
55f1336d 22113 else if (attr->form == DW_FORM_ref_sig8)
673bfd45 22114 {
ac9ec31b 22115 ULONGEST signature = DW_SIGNATURE (attr);
673bfd45 22116
ac9ec31b 22117 return get_signatured_type (die, signature, cu);
673bfd45
DE
22118 }
22119 else
22120 {
b98664d3 22121 complaint (_("Dwarf Error: Bad type attribute %s in DIE"
9d8780f0
SM
22122 " at %s [in module %s]"),
22123 dwarf_attr_name (attr->name), sect_offset_str (die->sect_off),
4262abfb 22124 objfile_name (objfile));
ac9ec31b 22125 return build_error_marker_type (cu, die);
673bfd45
DE
22126 }
22127
22128 /* If not cached we need to read it in. */
22129
22130 if (this_type == NULL)
22131 {
ac9ec31b 22132 struct die_info *type_die = NULL;
673bfd45
DE
22133 struct dwarf2_cu *type_cu = cu;
22134
7771576e 22135 if (attr_form_is_ref (attr))
ac9ec31b
DE
22136 type_die = follow_die_ref (die, attr, &type_cu);
22137 if (type_die == NULL)
22138 return build_error_marker_type (cu, die);
22139 /* If we find the type now, it's probably because the type came
3019eac3
DE
22140 from an inter-CU reference and the type's CU got expanded before
22141 ours. */
ac9ec31b 22142 this_type = read_type_die (type_die, type_cu);
673bfd45
DE
22143 }
22144
22145 /* If we still don't have a type use an error marker. */
22146
22147 if (this_type == NULL)
ac9ec31b 22148 return build_error_marker_type (cu, die);
673bfd45 22149
f792889a 22150 return this_type;
c906108c
SS
22151}
22152
673bfd45
DE
22153/* Return the type in DIE, CU.
22154 Returns NULL for invalid types.
22155
02142a6c 22156 This first does a lookup in die_type_hash,
673bfd45
DE
22157 and only reads the die in if necessary.
22158
22159 NOTE: This can be called when reading in partial or full symbols. */
22160
f792889a 22161static struct type *
e7c27a73 22162read_type_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 22163{
f792889a
DJ
22164 struct type *this_type;
22165
22166 this_type = get_die_type (die, cu);
22167 if (this_type)
22168 return this_type;
22169
673bfd45
DE
22170 return read_type_die_1 (die, cu);
22171}
22172
22173/* Read the type in DIE, CU.
22174 Returns NULL for invalid types. */
22175
22176static struct type *
22177read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
22178{
22179 struct type *this_type = NULL;
22180
c906108c
SS
22181 switch (die->tag)
22182 {
22183 case DW_TAG_class_type:
680b30c7 22184 case DW_TAG_interface_type:
c906108c
SS
22185 case DW_TAG_structure_type:
22186 case DW_TAG_union_type:
f792889a 22187 this_type = read_structure_type (die, cu);
c906108c
SS
22188 break;
22189 case DW_TAG_enumeration_type:
f792889a 22190 this_type = read_enumeration_type (die, cu);
c906108c
SS
22191 break;
22192 case DW_TAG_subprogram:
22193 case DW_TAG_subroutine_type:
edb3359d 22194 case DW_TAG_inlined_subroutine:
f792889a 22195 this_type = read_subroutine_type (die, cu);
c906108c
SS
22196 break;
22197 case DW_TAG_array_type:
f792889a 22198 this_type = read_array_type (die, cu);
c906108c 22199 break;
72019c9c 22200 case DW_TAG_set_type:
f792889a 22201 this_type = read_set_type (die, cu);
72019c9c 22202 break;
c906108c 22203 case DW_TAG_pointer_type:
f792889a 22204 this_type = read_tag_pointer_type (die, cu);
c906108c
SS
22205 break;
22206 case DW_TAG_ptr_to_member_type:
f792889a 22207 this_type = read_tag_ptr_to_member_type (die, cu);
c906108c
SS
22208 break;
22209 case DW_TAG_reference_type:
4297a3f0
AV
22210 this_type = read_tag_reference_type (die, cu, TYPE_CODE_REF);
22211 break;
22212 case DW_TAG_rvalue_reference_type:
22213 this_type = read_tag_reference_type (die, cu, TYPE_CODE_RVALUE_REF);
c906108c
SS
22214 break;
22215 case DW_TAG_const_type:
f792889a 22216 this_type = read_tag_const_type (die, cu);
c906108c
SS
22217 break;
22218 case DW_TAG_volatile_type:
f792889a 22219 this_type = read_tag_volatile_type (die, cu);
c906108c 22220 break;
06d66ee9
TT
22221 case DW_TAG_restrict_type:
22222 this_type = read_tag_restrict_type (die, cu);
22223 break;
c906108c 22224 case DW_TAG_string_type:
f792889a 22225 this_type = read_tag_string_type (die, cu);
c906108c
SS
22226 break;
22227 case DW_TAG_typedef:
f792889a 22228 this_type = read_typedef (die, cu);
c906108c 22229 break;
a02abb62 22230 case DW_TAG_subrange_type:
f792889a 22231 this_type = read_subrange_type (die, cu);
a02abb62 22232 break;
c906108c 22233 case DW_TAG_base_type:
f792889a 22234 this_type = read_base_type (die, cu);
c906108c 22235 break;
81a17f79 22236 case DW_TAG_unspecified_type:
f792889a 22237 this_type = read_unspecified_type (die, cu);
81a17f79 22238 break;
0114d602
DJ
22239 case DW_TAG_namespace:
22240 this_type = read_namespace_type (die, cu);
22241 break;
f55ee35c
JK
22242 case DW_TAG_module:
22243 this_type = read_module_type (die, cu);
22244 break;
a2c2acaf
MW
22245 case DW_TAG_atomic_type:
22246 this_type = read_tag_atomic_type (die, cu);
22247 break;
c906108c 22248 default:
b98664d3 22249 complaint (_("unexpected tag in read_type_die: '%s'"),
4d3c2250 22250 dwarf_tag_name (die->tag));
c906108c
SS
22251 break;
22252 }
63d06c5c 22253
f792889a 22254 return this_type;
63d06c5c
DC
22255}
22256
abc72ce4
DE
22257/* See if we can figure out if the class lives in a namespace. We do
22258 this by looking for a member function; its demangled name will
22259 contain namespace info, if there is any.
22260 Return the computed name or NULL.
22261 Space for the result is allocated on the objfile's obstack.
22262 This is the full-die version of guess_partial_die_structure_name.
22263 In this case we know DIE has no useful parent. */
22264
22265static char *
22266guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
22267{
22268 struct die_info *spec_die;
22269 struct dwarf2_cu *spec_cu;
22270 struct die_info *child;
518817b3 22271 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
abc72ce4
DE
22272
22273 spec_cu = cu;
22274 spec_die = die_specification (die, &spec_cu);
22275 if (spec_die != NULL)
22276 {
22277 die = spec_die;
22278 cu = spec_cu;
22279 }
22280
22281 for (child = die->child;
22282 child != NULL;
22283 child = child->sibling)
22284 {
22285 if (child->tag == DW_TAG_subprogram)
22286 {
73b9be8b 22287 const char *linkage_name = dw2_linkage_name (child, cu);
abc72ce4 22288
7d45c7c3 22289 if (linkage_name != NULL)
abc72ce4
DE
22290 {
22291 char *actual_name
22292 = language_class_name_from_physname (cu->language_defn,
7d45c7c3 22293 linkage_name);
abc72ce4
DE
22294 char *name = NULL;
22295
22296 if (actual_name != NULL)
22297 {
15d034d0 22298 const char *die_name = dwarf2_name (die, cu);
abc72ce4
DE
22299
22300 if (die_name != NULL
22301 && strcmp (die_name, actual_name) != 0)
22302 {
22303 /* Strip off the class name from the full name.
22304 We want the prefix. */
22305 int die_name_len = strlen (die_name);
22306 int actual_name_len = strlen (actual_name);
22307
22308 /* Test for '::' as a sanity check. */
22309 if (actual_name_len > die_name_len + 2
3e43a32a
MS
22310 && actual_name[actual_name_len
22311 - die_name_len - 1] == ':')
224c3ddb 22312 name = (char *) obstack_copy0 (
e3b94546 22313 &objfile->per_bfd->storage_obstack,
224c3ddb 22314 actual_name, actual_name_len - die_name_len - 2);
abc72ce4
DE
22315 }
22316 }
22317 xfree (actual_name);
22318 return name;
22319 }
22320 }
22321 }
22322
22323 return NULL;
22324}
22325
96408a79
SA
22326/* GCC might emit a nameless typedef that has a linkage name. Determine the
22327 prefix part in such case. See
22328 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
22329
a121b7c1 22330static const char *
96408a79
SA
22331anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
22332{
22333 struct attribute *attr;
e6a959d6 22334 const char *base;
96408a79
SA
22335
22336 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
22337 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
22338 return NULL;
22339
7d45c7c3 22340 if (dwarf2_string_attr (die, DW_AT_name, cu) != NULL)
96408a79
SA
22341 return NULL;
22342
73b9be8b 22343 attr = dw2_linkage_name_attr (die, cu);
96408a79
SA
22344 if (attr == NULL || DW_STRING (attr) == NULL)
22345 return NULL;
22346
22347 /* dwarf2_name had to be already called. */
22348 gdb_assert (DW_STRING_IS_CANONICAL (attr));
22349
22350 /* Strip the base name, keep any leading namespaces/classes. */
22351 base = strrchr (DW_STRING (attr), ':');
22352 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
22353 return "";
22354
518817b3 22355 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
e3b94546 22356 return (char *) obstack_copy0 (&objfile->per_bfd->storage_obstack,
224c3ddb
SM
22357 DW_STRING (attr),
22358 &base[-1] - DW_STRING (attr));
96408a79
SA
22359}
22360
fdde2d81 22361/* Return the name of the namespace/class that DIE is defined within,
0114d602 22362 or "" if we can't tell. The caller should not xfree the result.
fdde2d81 22363
0114d602
DJ
22364 For example, if we're within the method foo() in the following
22365 code:
22366
22367 namespace N {
22368 class C {
22369 void foo () {
22370 }
22371 };
22372 }
22373
22374 then determine_prefix on foo's die will return "N::C". */
fdde2d81 22375
0d5cff50 22376static const char *
e142c38c 22377determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
63d06c5c 22378{
518817b3
SM
22379 struct dwarf2_per_objfile *dwarf2_per_objfile
22380 = cu->per_cu->dwarf2_per_objfile;
0114d602
DJ
22381 struct die_info *parent, *spec_die;
22382 struct dwarf2_cu *spec_cu;
22383 struct type *parent_type;
a121b7c1 22384 const char *retval;
63d06c5c 22385
9c37b5ae 22386 if (cu->language != language_cplus
c44af4eb
TT
22387 && cu->language != language_fortran && cu->language != language_d
22388 && cu->language != language_rust)
0114d602
DJ
22389 return "";
22390
96408a79
SA
22391 retval = anonymous_struct_prefix (die, cu);
22392 if (retval)
22393 return retval;
22394
0114d602
DJ
22395 /* We have to be careful in the presence of DW_AT_specification.
22396 For example, with GCC 3.4, given the code
22397
22398 namespace N {
22399 void foo() {
22400 // Definition of N::foo.
22401 }
22402 }
22403
22404 then we'll have a tree of DIEs like this:
22405
22406 1: DW_TAG_compile_unit
22407 2: DW_TAG_namespace // N
22408 3: DW_TAG_subprogram // declaration of N::foo
22409 4: DW_TAG_subprogram // definition of N::foo
22410 DW_AT_specification // refers to die #3
22411
22412 Thus, when processing die #4, we have to pretend that we're in
22413 the context of its DW_AT_specification, namely the contex of die
22414 #3. */
22415 spec_cu = cu;
22416 spec_die = die_specification (die, &spec_cu);
22417 if (spec_die == NULL)
22418 parent = die->parent;
22419 else
63d06c5c 22420 {
0114d602
DJ
22421 parent = spec_die->parent;
22422 cu = spec_cu;
63d06c5c 22423 }
0114d602
DJ
22424
22425 if (parent == NULL)
22426 return "";
98bfdba5
PA
22427 else if (parent->building_fullname)
22428 {
22429 const char *name;
22430 const char *parent_name;
22431
22432 /* It has been seen on RealView 2.2 built binaries,
22433 DW_TAG_template_type_param types actually _defined_ as
22434 children of the parent class:
22435
22436 enum E {};
22437 template class <class Enum> Class{};
22438 Class<enum E> class_e;
22439
22440 1: DW_TAG_class_type (Class)
22441 2: DW_TAG_enumeration_type (E)
22442 3: DW_TAG_enumerator (enum1:0)
22443 3: DW_TAG_enumerator (enum2:1)
22444 ...
22445 2: DW_TAG_template_type_param
22446 DW_AT_type DW_FORM_ref_udata (E)
22447
22448 Besides being broken debug info, it can put GDB into an
22449 infinite loop. Consider:
22450
22451 When we're building the full name for Class<E>, we'll start
22452 at Class, and go look over its template type parameters,
22453 finding E. We'll then try to build the full name of E, and
22454 reach here. We're now trying to build the full name of E,
22455 and look over the parent DIE for containing scope. In the
22456 broken case, if we followed the parent DIE of E, we'd again
22457 find Class, and once again go look at its template type
22458 arguments, etc., etc. Simply don't consider such parent die
22459 as source-level parent of this die (it can't be, the language
22460 doesn't allow it), and break the loop here. */
22461 name = dwarf2_name (die, cu);
22462 parent_name = dwarf2_name (parent, cu);
b98664d3 22463 complaint (_("template param type '%s' defined within parent '%s'"),
98bfdba5
PA
22464 name ? name : "<unknown>",
22465 parent_name ? parent_name : "<unknown>");
22466 return "";
22467 }
63d06c5c 22468 else
0114d602
DJ
22469 switch (parent->tag)
22470 {
63d06c5c 22471 case DW_TAG_namespace:
0114d602 22472 parent_type = read_type_die (parent, cu);
acebe513
UW
22473 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
22474 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
22475 Work around this problem here. */
22476 if (cu->language == language_cplus
e86ca25f 22477 && strcmp (TYPE_NAME (parent_type), "::") == 0)
acebe513 22478 return "";
0114d602 22479 /* We give a name to even anonymous namespaces. */
e86ca25f 22480 return TYPE_NAME (parent_type);
63d06c5c 22481 case DW_TAG_class_type:
680b30c7 22482 case DW_TAG_interface_type:
63d06c5c 22483 case DW_TAG_structure_type:
0114d602 22484 case DW_TAG_union_type:
f55ee35c 22485 case DW_TAG_module:
0114d602 22486 parent_type = read_type_die (parent, cu);
e86ca25f
TT
22487 if (TYPE_NAME (parent_type) != NULL)
22488 return TYPE_NAME (parent_type);
0114d602
DJ
22489 else
22490 /* An anonymous structure is only allowed non-static data
22491 members; no typedefs, no member functions, et cetera.
22492 So it does not need a prefix. */
22493 return "";
abc72ce4 22494 case DW_TAG_compile_unit:
95554aad 22495 case DW_TAG_partial_unit:
abc72ce4
DE
22496 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
22497 if (cu->language == language_cplus
8b70b953 22498 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
abc72ce4
DE
22499 && die->child != NULL
22500 && (die->tag == DW_TAG_class_type
22501 || die->tag == DW_TAG_structure_type
22502 || die->tag == DW_TAG_union_type))
22503 {
22504 char *name = guess_full_die_structure_name (die, cu);
22505 if (name != NULL)
22506 return name;
22507 }
22508 return "";
3d567982
TT
22509 case DW_TAG_enumeration_type:
22510 parent_type = read_type_die (parent, cu);
22511 if (TYPE_DECLARED_CLASS (parent_type))
22512 {
e86ca25f
TT
22513 if (TYPE_NAME (parent_type) != NULL)
22514 return TYPE_NAME (parent_type);
3d567982
TT
22515 return "";
22516 }
22517 /* Fall through. */
63d06c5c 22518 default:
8176b9b8 22519 return determine_prefix (parent, cu);
63d06c5c 22520 }
63d06c5c
DC
22521}
22522
3e43a32a
MS
22523/* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
22524 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
22525 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
22526 an obconcat, otherwise allocate storage for the result. The CU argument is
22527 used to determine the language and hence, the appropriate separator. */
987504bb 22528
f55ee35c 22529#define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
63d06c5c
DC
22530
22531static char *
f55ee35c
JK
22532typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
22533 int physname, struct dwarf2_cu *cu)
63d06c5c 22534{
f55ee35c 22535 const char *lead = "";
5c315b68 22536 const char *sep;
63d06c5c 22537
3e43a32a
MS
22538 if (suffix == NULL || suffix[0] == '\0'
22539 || prefix == NULL || prefix[0] == '\0')
987504bb 22540 sep = "";
45280282
IB
22541 else if (cu->language == language_d)
22542 {
22543 /* For D, the 'main' function could be defined in any module, but it
22544 should never be prefixed. */
22545 if (strcmp (suffix, "D main") == 0)
22546 {
22547 prefix = "";
22548 sep = "";
22549 }
22550 else
22551 sep = ".";
22552 }
f55ee35c
JK
22553 else if (cu->language == language_fortran && physname)
22554 {
22555 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
22556 DW_AT_MIPS_linkage_name is preferred and used instead. */
22557
22558 lead = "__";
22559 sep = "_MOD_";
22560 }
987504bb
JJ
22561 else
22562 sep = "::";
63d06c5c 22563
6dd47d34
DE
22564 if (prefix == NULL)
22565 prefix = "";
22566 if (suffix == NULL)
22567 suffix = "";
22568
987504bb
JJ
22569 if (obs == NULL)
22570 {
3e43a32a 22571 char *retval
224c3ddb
SM
22572 = ((char *)
22573 xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1));
9a619af0 22574
f55ee35c
JK
22575 strcpy (retval, lead);
22576 strcat (retval, prefix);
6dd47d34
DE
22577 strcat (retval, sep);
22578 strcat (retval, suffix);
63d06c5c
DC
22579 return retval;
22580 }
987504bb
JJ
22581 else
22582 {
22583 /* We have an obstack. */
f55ee35c 22584 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
987504bb 22585 }
63d06c5c
DC
22586}
22587
c906108c
SS
22588/* Return sibling of die, NULL if no sibling. */
22589
f9aca02d 22590static struct die_info *
fba45db2 22591sibling_die (struct die_info *die)
c906108c 22592{
639d11d3 22593 return die->sibling;
c906108c
SS
22594}
22595
71c25dea
TT
22596/* Get name of a die, return NULL if not found. */
22597
15d034d0
TT
22598static const char *
22599dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
71c25dea
TT
22600 struct obstack *obstack)
22601{
22602 if (name && cu->language == language_cplus)
22603 {
2f408ecb 22604 std::string canon_name = cp_canonicalize_string (name);
71c25dea 22605
2f408ecb 22606 if (!canon_name.empty ())
71c25dea 22607 {
2f408ecb
PA
22608 if (canon_name != name)
22609 name = (const char *) obstack_copy0 (obstack,
22610 canon_name.c_str (),
22611 canon_name.length ());
71c25dea
TT
22612 }
22613 }
22614
22615 return name;
c906108c
SS
22616}
22617
96553a0c
DE
22618/* Get name of a die, return NULL if not found.
22619 Anonymous namespaces are converted to their magic string. */
9219021c 22620
15d034d0 22621static const char *
e142c38c 22622dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
9219021c
DC
22623{
22624 struct attribute *attr;
518817b3 22625 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
9219021c 22626
e142c38c 22627 attr = dwarf2_attr (die, DW_AT_name, cu);
53832f31 22628 if ((!attr || !DW_STRING (attr))
96553a0c 22629 && die->tag != DW_TAG_namespace
53832f31
TT
22630 && die->tag != DW_TAG_class_type
22631 && die->tag != DW_TAG_interface_type
22632 && die->tag != DW_TAG_structure_type
22633 && die->tag != DW_TAG_union_type)
71c25dea
TT
22634 return NULL;
22635
22636 switch (die->tag)
22637 {
22638 case DW_TAG_compile_unit:
95554aad 22639 case DW_TAG_partial_unit:
71c25dea
TT
22640 /* Compilation units have a DW_AT_name that is a filename, not
22641 a source language identifier. */
22642 case DW_TAG_enumeration_type:
22643 case DW_TAG_enumerator:
22644 /* These tags always have simple identifiers already; no need
22645 to canonicalize them. */
22646 return DW_STRING (attr);
907af001 22647
96553a0c
DE
22648 case DW_TAG_namespace:
22649 if (attr != NULL && DW_STRING (attr) != NULL)
22650 return DW_STRING (attr);
22651 return CP_ANONYMOUS_NAMESPACE_STR;
22652
907af001
UW
22653 case DW_TAG_class_type:
22654 case DW_TAG_interface_type:
22655 case DW_TAG_structure_type:
22656 case DW_TAG_union_type:
22657 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
22658 structures or unions. These were of the form "._%d" in GCC 4.1,
22659 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
22660 and GCC 4.4. We work around this problem by ignoring these. */
53832f31 22661 if (attr && DW_STRING (attr)
61012eef
GB
22662 && (startswith (DW_STRING (attr), "._")
22663 || startswith (DW_STRING (attr), "<anonymous")))
907af001 22664 return NULL;
53832f31
TT
22665
22666 /* GCC might emit a nameless typedef that has a linkage name. See
22667 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
22668 if (!attr || DW_STRING (attr) == NULL)
22669 {
df5c6c50 22670 char *demangled = NULL;
53832f31 22671
73b9be8b 22672 attr = dw2_linkage_name_attr (die, cu);
53832f31
TT
22673 if (attr == NULL || DW_STRING (attr) == NULL)
22674 return NULL;
22675
df5c6c50
JK
22676 /* Avoid demangling DW_STRING (attr) the second time on a second
22677 call for the same DIE. */
22678 if (!DW_STRING_IS_CANONICAL (attr))
8de20a37 22679 demangled = gdb_demangle (DW_STRING (attr), DMGL_TYPES);
53832f31
TT
22680
22681 if (demangled)
22682 {
e6a959d6 22683 const char *base;
96408a79 22684
53832f31 22685 /* FIXME: we already did this for the partial symbol... */
34a68019 22686 DW_STRING (attr)
224c3ddb 22687 = ((const char *)
e3b94546 22688 obstack_copy0 (&objfile->per_bfd->storage_obstack,
224c3ddb 22689 demangled, strlen (demangled)));
53832f31
TT
22690 DW_STRING_IS_CANONICAL (attr) = 1;
22691 xfree (demangled);
96408a79
SA
22692
22693 /* Strip any leading namespaces/classes, keep only the base name.
22694 DW_AT_name for named DIEs does not contain the prefixes. */
22695 base = strrchr (DW_STRING (attr), ':');
22696 if (base && base > DW_STRING (attr) && base[-1] == ':')
22697 return &base[1];
22698 else
22699 return DW_STRING (attr);
53832f31
TT
22700 }
22701 }
907af001
UW
22702 break;
22703
71c25dea 22704 default:
907af001
UW
22705 break;
22706 }
22707
22708 if (!DW_STRING_IS_CANONICAL (attr))
22709 {
22710 DW_STRING (attr)
22711 = dwarf2_canonicalize_name (DW_STRING (attr), cu,
e3b94546 22712 &objfile->per_bfd->storage_obstack);
907af001 22713 DW_STRING_IS_CANONICAL (attr) = 1;
71c25dea 22714 }
907af001 22715 return DW_STRING (attr);
9219021c
DC
22716}
22717
22718/* Return the die that this die in an extension of, or NULL if there
f2f0e013
DJ
22719 is none. *EXT_CU is the CU containing DIE on input, and the CU
22720 containing the return value on output. */
9219021c
DC
22721
22722static struct die_info *
f2f0e013 22723dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
9219021c
DC
22724{
22725 struct attribute *attr;
9219021c 22726
f2f0e013 22727 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
9219021c
DC
22728 if (attr == NULL)
22729 return NULL;
22730
f2f0e013 22731 return follow_die_ref (die, attr, ext_cu);
9219021c
DC
22732}
22733
c906108c
SS
22734/* Convert a DIE tag into its string name. */
22735
f39c6ffd 22736static const char *
aa1ee363 22737dwarf_tag_name (unsigned tag)
c906108c 22738{
f39c6ffd
TT
22739 const char *name = get_DW_TAG_name (tag);
22740
22741 if (name == NULL)
22742 return "DW_TAG_<unknown>";
22743
22744 return name;
c906108c
SS
22745}
22746
22747/* Convert a DWARF attribute code into its string name. */
22748
f39c6ffd 22749static const char *
aa1ee363 22750dwarf_attr_name (unsigned attr)
c906108c 22751{
f39c6ffd
TT
22752 const char *name;
22753
c764a876 22754#ifdef MIPS /* collides with DW_AT_HP_block_index */
f39c6ffd
TT
22755 if (attr == DW_AT_MIPS_fde)
22756 return "DW_AT_MIPS_fde";
22757#else
22758 if (attr == DW_AT_HP_block_index)
22759 return "DW_AT_HP_block_index";
c764a876 22760#endif
f39c6ffd
TT
22761
22762 name = get_DW_AT_name (attr);
22763
22764 if (name == NULL)
22765 return "DW_AT_<unknown>";
22766
22767 return name;
c906108c
SS
22768}
22769
22770/* Convert a DWARF value form code into its string name. */
22771
f39c6ffd 22772static const char *
aa1ee363 22773dwarf_form_name (unsigned form)
c906108c 22774{
f39c6ffd
TT
22775 const char *name = get_DW_FORM_name (form);
22776
22777 if (name == NULL)
22778 return "DW_FORM_<unknown>";
22779
22780 return name;
c906108c
SS
22781}
22782
a121b7c1 22783static const char *
fba45db2 22784dwarf_bool_name (unsigned mybool)
c906108c
SS
22785{
22786 if (mybool)
22787 return "TRUE";
22788 else
22789 return "FALSE";
22790}
22791
22792/* Convert a DWARF type code into its string name. */
22793
f39c6ffd 22794static const char *
aa1ee363 22795dwarf_type_encoding_name (unsigned enc)
c906108c 22796{
f39c6ffd 22797 const char *name = get_DW_ATE_name (enc);
c906108c 22798
f39c6ffd
TT
22799 if (name == NULL)
22800 return "DW_ATE_<unknown>";
c906108c 22801
f39c6ffd 22802 return name;
c906108c 22803}
c906108c 22804
f9aca02d 22805static void
d97bc12b 22806dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
c906108c
SS
22807{
22808 unsigned int i;
22809
d97bc12b 22810 print_spaces (indent, f);
9d8780f0 22811 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset %s)\n",
9c541725 22812 dwarf_tag_name (die->tag), die->abbrev,
9d8780f0 22813 sect_offset_str (die->sect_off));
d97bc12b
DE
22814
22815 if (die->parent != NULL)
22816 {
22817 print_spaces (indent, f);
9d8780f0
SM
22818 fprintf_unfiltered (f, " parent at offset: %s\n",
22819 sect_offset_str (die->parent->sect_off));
d97bc12b
DE
22820 }
22821
22822 print_spaces (indent, f);
22823 fprintf_unfiltered (f, " has children: %s\n",
639d11d3 22824 dwarf_bool_name (die->child != NULL));
c906108c 22825
d97bc12b
DE
22826 print_spaces (indent, f);
22827 fprintf_unfiltered (f, " attributes:\n");
22828
c906108c
SS
22829 for (i = 0; i < die->num_attrs; ++i)
22830 {
d97bc12b
DE
22831 print_spaces (indent, f);
22832 fprintf_unfiltered (f, " %s (%s) ",
c906108c
SS
22833 dwarf_attr_name (die->attrs[i].name),
22834 dwarf_form_name (die->attrs[i].form));
d97bc12b 22835
c906108c
SS
22836 switch (die->attrs[i].form)
22837 {
c906108c 22838 case DW_FORM_addr:
3019eac3 22839 case DW_FORM_GNU_addr_index:
d97bc12b 22840 fprintf_unfiltered (f, "address: ");
5af949e3 22841 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
c906108c
SS
22842 break;
22843 case DW_FORM_block2:
22844 case DW_FORM_block4:
22845 case DW_FORM_block:
22846 case DW_FORM_block1:
56eb65bd
SP
22847 fprintf_unfiltered (f, "block: size %s",
22848 pulongest (DW_BLOCK (&die->attrs[i])->size));
c906108c 22849 break;
2dc7f7b3 22850 case DW_FORM_exprloc:
56eb65bd
SP
22851 fprintf_unfiltered (f, "expression: size %s",
22852 pulongest (DW_BLOCK (&die->attrs[i])->size));
2dc7f7b3 22853 break;
0224619f
JK
22854 case DW_FORM_data16:
22855 fprintf_unfiltered (f, "constant of 16 bytes");
22856 break;
4568ecf9
DE
22857 case DW_FORM_ref_addr:
22858 fprintf_unfiltered (f, "ref address: ");
22859 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
22860 break;
36586728
TT
22861 case DW_FORM_GNU_ref_alt:
22862 fprintf_unfiltered (f, "alt ref address: ");
22863 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
22864 break;
10b3939b
DJ
22865 case DW_FORM_ref1:
22866 case DW_FORM_ref2:
22867 case DW_FORM_ref4:
4568ecf9
DE
22868 case DW_FORM_ref8:
22869 case DW_FORM_ref_udata:
d97bc12b 22870 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
4568ecf9 22871 (long) (DW_UNSND (&die->attrs[i])));
10b3939b 22872 break;
c906108c
SS
22873 case DW_FORM_data1:
22874 case DW_FORM_data2:
22875 case DW_FORM_data4:
ce5d95e1 22876 case DW_FORM_data8:
c906108c
SS
22877 case DW_FORM_udata:
22878 case DW_FORM_sdata:
43bbcdc2
PH
22879 fprintf_unfiltered (f, "constant: %s",
22880 pulongest (DW_UNSND (&die->attrs[i])));
c906108c 22881 break;
2dc7f7b3
TT
22882 case DW_FORM_sec_offset:
22883 fprintf_unfiltered (f, "section offset: %s",
22884 pulongest (DW_UNSND (&die->attrs[i])));
22885 break;
55f1336d 22886 case DW_FORM_ref_sig8:
ac9ec31b
DE
22887 fprintf_unfiltered (f, "signature: %s",
22888 hex_string (DW_SIGNATURE (&die->attrs[i])));
348e048f 22889 break;
c906108c 22890 case DW_FORM_string:
4bdf3d34 22891 case DW_FORM_strp:
43988095 22892 case DW_FORM_line_strp:
3019eac3 22893 case DW_FORM_GNU_str_index:
36586728 22894 case DW_FORM_GNU_strp_alt:
8285870a 22895 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
c906108c 22896 DW_STRING (&die->attrs[i])
8285870a
JK
22897 ? DW_STRING (&die->attrs[i]) : "",
22898 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
c906108c
SS
22899 break;
22900 case DW_FORM_flag:
22901 if (DW_UNSND (&die->attrs[i]))
d97bc12b 22902 fprintf_unfiltered (f, "flag: TRUE");
c906108c 22903 else
d97bc12b 22904 fprintf_unfiltered (f, "flag: FALSE");
c906108c 22905 break;
2dc7f7b3
TT
22906 case DW_FORM_flag_present:
22907 fprintf_unfiltered (f, "flag: TRUE");
22908 break;
a8329558 22909 case DW_FORM_indirect:
0963b4bd
MS
22910 /* The reader will have reduced the indirect form to
22911 the "base form" so this form should not occur. */
3e43a32a
MS
22912 fprintf_unfiltered (f,
22913 "unexpected attribute form: DW_FORM_indirect");
a8329558 22914 break;
663c44ac
JK
22915 case DW_FORM_implicit_const:
22916 fprintf_unfiltered (f, "constant: %s",
22917 plongest (DW_SND (&die->attrs[i])));
22918 break;
c906108c 22919 default:
d97bc12b 22920 fprintf_unfiltered (f, "unsupported attribute form: %d.",
c5aa993b 22921 die->attrs[i].form);
d97bc12b 22922 break;
c906108c 22923 }
d97bc12b 22924 fprintf_unfiltered (f, "\n");
c906108c
SS
22925 }
22926}
22927
f9aca02d 22928static void
d97bc12b 22929dump_die_for_error (struct die_info *die)
c906108c 22930{
d97bc12b
DE
22931 dump_die_shallow (gdb_stderr, 0, die);
22932}
22933
22934static void
22935dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
22936{
22937 int indent = level * 4;
22938
22939 gdb_assert (die != NULL);
22940
22941 if (level >= max_level)
22942 return;
22943
22944 dump_die_shallow (f, indent, die);
22945
22946 if (die->child != NULL)
c906108c 22947 {
d97bc12b
DE
22948 print_spaces (indent, f);
22949 fprintf_unfiltered (f, " Children:");
22950 if (level + 1 < max_level)
22951 {
22952 fprintf_unfiltered (f, "\n");
22953 dump_die_1 (f, level + 1, max_level, die->child);
22954 }
22955 else
22956 {
3e43a32a
MS
22957 fprintf_unfiltered (f,
22958 " [not printed, max nesting level reached]\n");
d97bc12b
DE
22959 }
22960 }
22961
22962 if (die->sibling != NULL && level > 0)
22963 {
22964 dump_die_1 (f, level, max_level, die->sibling);
c906108c
SS
22965 }
22966}
22967
d97bc12b
DE
22968/* This is called from the pdie macro in gdbinit.in.
22969 It's not static so gcc will keep a copy callable from gdb. */
22970
22971void
22972dump_die (struct die_info *die, int max_level)
22973{
22974 dump_die_1 (gdb_stdlog, 0, max_level, die);
22975}
22976
f9aca02d 22977static void
51545339 22978store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
c906108c 22979{
51545339 22980 void **slot;
c906108c 22981
9c541725
PA
22982 slot = htab_find_slot_with_hash (cu->die_hash, die,
22983 to_underlying (die->sect_off),
b64f50a1 22984 INSERT);
51545339
DJ
22985
22986 *slot = die;
c906108c
SS
22987}
22988
b64f50a1
JK
22989/* Return DIE offset of ATTR. Return 0 with complaint if ATTR is not of the
22990 required kind. */
22991
22992static sect_offset
ff39bb5e 22993dwarf2_get_ref_die_offset (const struct attribute *attr)
93311388 22994{
7771576e 22995 if (attr_form_is_ref (attr))
9c541725 22996 return (sect_offset) DW_UNSND (attr);
93311388 22997
b98664d3 22998 complaint (_("unsupported die ref attribute form: '%s'"),
93311388 22999 dwarf_form_name (attr->form));
9c541725 23000 return {};
c906108c
SS
23001}
23002
43bbcdc2
PH
23003/* Return the constant value held by ATTR. Return DEFAULT_VALUE if
23004 * the value held by the attribute is not constant. */
a02abb62 23005
43bbcdc2 23006static LONGEST
ff39bb5e 23007dwarf2_get_attr_constant_value (const struct attribute *attr, int default_value)
a02abb62 23008{
663c44ac 23009 if (attr->form == DW_FORM_sdata || attr->form == DW_FORM_implicit_const)
a02abb62
JB
23010 return DW_SND (attr);
23011 else if (attr->form == DW_FORM_udata
23012 || attr->form == DW_FORM_data1
23013 || attr->form == DW_FORM_data2
23014 || attr->form == DW_FORM_data4
23015 || attr->form == DW_FORM_data8)
23016 return DW_UNSND (attr);
23017 else
23018 {
0224619f 23019 /* For DW_FORM_data16 see attr_form_is_constant. */
b98664d3 23020 complaint (_("Attribute value is not a constant (%s)"),
a02abb62
JB
23021 dwarf_form_name (attr->form));
23022 return default_value;
23023 }
23024}
23025
348e048f
DE
23026/* Follow reference or signature attribute ATTR of SRC_DIE.
23027 On entry *REF_CU is the CU of SRC_DIE.
23028 On exit *REF_CU is the CU of the result. */
23029
23030static struct die_info *
ff39bb5e 23031follow_die_ref_or_sig (struct die_info *src_die, const struct attribute *attr,
348e048f
DE
23032 struct dwarf2_cu **ref_cu)
23033{
23034 struct die_info *die;
23035
7771576e 23036 if (attr_form_is_ref (attr))
348e048f 23037 die = follow_die_ref (src_die, attr, ref_cu);
55f1336d 23038 else if (attr->form == DW_FORM_ref_sig8)
348e048f
DE
23039 die = follow_die_sig (src_die, attr, ref_cu);
23040 else
23041 {
23042 dump_die_for_error (src_die);
23043 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
518817b3 23044 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
348e048f
DE
23045 }
23046
23047 return die;
03dd20cc
DJ
23048}
23049
5c631832 23050/* Follow reference OFFSET.
673bfd45
DE
23051 On entry *REF_CU is the CU of the source die referencing OFFSET.
23052 On exit *REF_CU is the CU of the result.
23053 Returns NULL if OFFSET is invalid. */
f504f079 23054
f9aca02d 23055static struct die_info *
9c541725 23056follow_die_offset (sect_offset sect_off, int offset_in_dwz,
36586728 23057 struct dwarf2_cu **ref_cu)
c906108c 23058{
10b3939b 23059 struct die_info temp_die;
f2f0e013 23060 struct dwarf2_cu *target_cu, *cu = *ref_cu;
518817b3
SM
23061 struct dwarf2_per_objfile *dwarf2_per_objfile
23062 = cu->per_cu->dwarf2_per_objfile;
10b3939b 23063
348e048f
DE
23064 gdb_assert (cu->per_cu != NULL);
23065
98bfdba5
PA
23066 target_cu = cu;
23067
3019eac3 23068 if (cu->per_cu->is_debug_types)
348e048f
DE
23069 {
23070 /* .debug_types CUs cannot reference anything outside their CU.
23071 If they need to, they have to reference a signatured type via
55f1336d 23072 DW_FORM_ref_sig8. */
9c541725 23073 if (!offset_in_cu_p (&cu->header, sect_off))
5c631832 23074 return NULL;
348e048f 23075 }
36586728 23076 else if (offset_in_dwz != cu->per_cu->is_dwz
9c541725 23077 || !offset_in_cu_p (&cu->header, sect_off))
10b3939b
DJ
23078 {
23079 struct dwarf2_per_cu_data *per_cu;
9a619af0 23080
9c541725 23081 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
ed2dc618 23082 dwarf2_per_objfile);
03dd20cc
DJ
23083
23084 /* If necessary, add it to the queue and load its DIEs. */
95554aad 23085 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
58f0c718 23086 load_full_comp_unit (per_cu, false, cu->language);
03dd20cc 23087
10b3939b
DJ
23088 target_cu = per_cu->cu;
23089 }
98bfdba5
PA
23090 else if (cu->dies == NULL)
23091 {
23092 /* We're loading full DIEs during partial symbol reading. */
23093 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
58f0c718 23094 load_full_comp_unit (cu->per_cu, false, language_minimal);
98bfdba5 23095 }
c906108c 23096
f2f0e013 23097 *ref_cu = target_cu;
9c541725 23098 temp_die.sect_off = sect_off;
c24bdb02
KS
23099
23100 if (target_cu != cu)
23101 target_cu->ancestor = cu;
23102
9a3c8263 23103 return (struct die_info *) htab_find_with_hash (target_cu->die_hash,
9c541725
PA
23104 &temp_die,
23105 to_underlying (sect_off));
5c631832 23106}
10b3939b 23107
5c631832
JK
23108/* Follow reference attribute ATTR of SRC_DIE.
23109 On entry *REF_CU is the CU of SRC_DIE.
23110 On exit *REF_CU is the CU of the result. */
23111
23112static struct die_info *
ff39bb5e 23113follow_die_ref (struct die_info *src_die, const struct attribute *attr,
5c631832
JK
23114 struct dwarf2_cu **ref_cu)
23115{
9c541725 23116 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
5c631832
JK
23117 struct dwarf2_cu *cu = *ref_cu;
23118 struct die_info *die;
23119
9c541725 23120 die = follow_die_offset (sect_off,
36586728
TT
23121 (attr->form == DW_FORM_GNU_ref_alt
23122 || cu->per_cu->is_dwz),
23123 ref_cu);
5c631832 23124 if (!die)
9d8780f0
SM
23125 error (_("Dwarf Error: Cannot find DIE at %s referenced from DIE "
23126 "at %s [in module %s]"),
23127 sect_offset_str (sect_off), sect_offset_str (src_die->sect_off),
518817b3 23128 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
348e048f 23129
5c631832
JK
23130 return die;
23131}
23132
9c541725 23133/* Return DWARF block referenced by DW_AT_location of DIE at SECT_OFF at PER_CU.
d83e736b 23134 Returned value is intended for DW_OP_call*. Returned
e3b94546
SM
23135 dwarf2_locexpr_baton->data has lifetime of
23136 PER_CU->DWARF2_PER_OBJFILE->OBJFILE. */
5c631832
JK
23137
23138struct dwarf2_locexpr_baton
9c541725 23139dwarf2_fetch_die_loc_sect_off (sect_offset sect_off,
8b9737bf
TT
23140 struct dwarf2_per_cu_data *per_cu,
23141 CORE_ADDR (*get_frame_pc) (void *baton),
e4a62c65 23142 void *baton, bool resolve_abstract_p)
5c631832 23143{
918dd910 23144 struct dwarf2_cu *cu;
5c631832
JK
23145 struct die_info *die;
23146 struct attribute *attr;
23147 struct dwarf2_locexpr_baton retval;
12359b5e
SM
23148 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
23149 struct objfile *objfile = dwarf2_per_objfile->objfile;
8cf6f0b1 23150
918dd910 23151 if (per_cu->cu == NULL)
58f0c718 23152 load_cu (per_cu, false);
918dd910 23153 cu = per_cu->cu;
cc12ce38
DE
23154 if (cu == NULL)
23155 {
23156 /* We shouldn't get here for a dummy CU, but don't crash on the user.
23157 Instead just throw an error, not much else we can do. */
9d8780f0
SM
23158 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
23159 sect_offset_str (sect_off), objfile_name (objfile));
cc12ce38 23160 }
918dd910 23161
9c541725 23162 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
5c631832 23163 if (!die)
9d8780f0
SM
23164 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
23165 sect_offset_str (sect_off), objfile_name (objfile));
5c631832
JK
23166
23167 attr = dwarf2_attr (die, DW_AT_location, cu);
e4a62c65
TV
23168 if (!attr && resolve_abstract_p
23169 && (dwarf2_per_objfile->abstract_to_concrete.find (die)
23170 != dwarf2_per_objfile->abstract_to_concrete.end ()))
23171 {
23172 CORE_ADDR pc = (*get_frame_pc) (baton);
23173
23174 for (const auto &cand : dwarf2_per_objfile->abstract_to_concrete[die])
23175 {
23176 if (!cand->parent
23177 || cand->parent->tag != DW_TAG_subprogram)
23178 continue;
23179
23180 CORE_ADDR pc_low, pc_high;
23181 get_scope_pc_bounds (cand->parent, &pc_low, &pc_high, cu);
23182 if (pc_low == ((CORE_ADDR) -1)
23183 || !(pc_low <= pc && pc < pc_high))
23184 continue;
23185
23186 die = cand;
23187 attr = dwarf2_attr (die, DW_AT_location, cu);
23188 break;
23189 }
23190 }
23191
5c631832
JK
23192 if (!attr)
23193 {
e103e986
JK
23194 /* DWARF: "If there is no such attribute, then there is no effect.".
23195 DATA is ignored if SIZE is 0. */
5c631832 23196
e103e986 23197 retval.data = NULL;
5c631832
JK
23198 retval.size = 0;
23199 }
8cf6f0b1
TT
23200 else if (attr_form_is_section_offset (attr))
23201 {
23202 struct dwarf2_loclist_baton loclist_baton;
23203 CORE_ADDR pc = (*get_frame_pc) (baton);
23204 size_t size;
23205
23206 fill_in_loclist_baton (cu, &loclist_baton, attr);
23207
23208 retval.data = dwarf2_find_location_expression (&loclist_baton,
23209 &size, pc);
23210 retval.size = size;
23211 }
5c631832
JK
23212 else
23213 {
23214 if (!attr_form_is_block (attr))
9d8780f0 23215 error (_("Dwarf Error: DIE at %s referenced in module %s "
5c631832 23216 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
9d8780f0 23217 sect_offset_str (sect_off), objfile_name (objfile));
5c631832
JK
23218
23219 retval.data = DW_BLOCK (attr)->data;
23220 retval.size = DW_BLOCK (attr)->size;
23221 }
23222 retval.per_cu = cu->per_cu;
918dd910 23223
ed2dc618 23224 age_cached_comp_units (dwarf2_per_objfile);
918dd910 23225
5c631832 23226 return retval;
348e048f
DE
23227}
23228
8b9737bf
TT
23229/* Like dwarf2_fetch_die_loc_sect_off, but take a CU
23230 offset. */
23231
23232struct dwarf2_locexpr_baton
23233dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
23234 struct dwarf2_per_cu_data *per_cu,
23235 CORE_ADDR (*get_frame_pc) (void *baton),
23236 void *baton)
23237{
9c541725 23238 sect_offset sect_off = per_cu->sect_off + to_underlying (offset_in_cu);
8b9737bf 23239
9c541725 23240 return dwarf2_fetch_die_loc_sect_off (sect_off, per_cu, get_frame_pc, baton);
8b9737bf
TT
23241}
23242
b6807d98
TT
23243/* Write a constant of a given type as target-ordered bytes into
23244 OBSTACK. */
23245
23246static const gdb_byte *
23247write_constant_as_bytes (struct obstack *obstack,
23248 enum bfd_endian byte_order,
23249 struct type *type,
23250 ULONGEST value,
23251 LONGEST *len)
23252{
23253 gdb_byte *result;
23254
23255 *len = TYPE_LENGTH (type);
224c3ddb 23256 result = (gdb_byte *) obstack_alloc (obstack, *len);
b6807d98
TT
23257 store_unsigned_integer (result, *len, byte_order, value);
23258
23259 return result;
23260}
23261
23262/* If the DIE at OFFSET in PER_CU has a DW_AT_const_value, return a
23263 pointer to the constant bytes and set LEN to the length of the
23264 data. If memory is needed, allocate it on OBSTACK. If the DIE
23265 does not have a DW_AT_const_value, return NULL. */
23266
23267const gdb_byte *
9c541725 23268dwarf2_fetch_constant_bytes (sect_offset sect_off,
b6807d98
TT
23269 struct dwarf2_per_cu_data *per_cu,
23270 struct obstack *obstack,
23271 LONGEST *len)
23272{
23273 struct dwarf2_cu *cu;
23274 struct die_info *die;
23275 struct attribute *attr;
23276 const gdb_byte *result = NULL;
23277 struct type *type;
23278 LONGEST value;
23279 enum bfd_endian byte_order;
e3b94546 23280 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
b6807d98 23281
b6807d98 23282 if (per_cu->cu == NULL)
58f0c718 23283 load_cu (per_cu, false);
b6807d98 23284 cu = per_cu->cu;
cc12ce38
DE
23285 if (cu == NULL)
23286 {
23287 /* We shouldn't get here for a dummy CU, but don't crash on the user.
23288 Instead just throw an error, not much else we can do. */
9d8780f0
SM
23289 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
23290 sect_offset_str (sect_off), objfile_name (objfile));
cc12ce38 23291 }
b6807d98 23292
9c541725 23293 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
b6807d98 23294 if (!die)
9d8780f0
SM
23295 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
23296 sect_offset_str (sect_off), objfile_name (objfile));
b6807d98
TT
23297
23298 attr = dwarf2_attr (die, DW_AT_const_value, cu);
23299 if (attr == NULL)
23300 return NULL;
23301
e3b94546 23302 byte_order = (bfd_big_endian (objfile->obfd)
b6807d98
TT
23303 ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
23304
23305 switch (attr->form)
23306 {
23307 case DW_FORM_addr:
23308 case DW_FORM_GNU_addr_index:
23309 {
23310 gdb_byte *tem;
23311
23312 *len = cu->header.addr_size;
224c3ddb 23313 tem = (gdb_byte *) obstack_alloc (obstack, *len);
b6807d98
TT
23314 store_unsigned_integer (tem, *len, byte_order, DW_ADDR (attr));
23315 result = tem;
23316 }
23317 break;
23318 case DW_FORM_string:
23319 case DW_FORM_strp:
23320 case DW_FORM_GNU_str_index:
23321 case DW_FORM_GNU_strp_alt:
23322 /* DW_STRING is already allocated on the objfile obstack, point
23323 directly to it. */
23324 result = (const gdb_byte *) DW_STRING (attr);
23325 *len = strlen (DW_STRING (attr));
23326 break;
23327 case DW_FORM_block1:
23328 case DW_FORM_block2:
23329 case DW_FORM_block4:
23330 case DW_FORM_block:
23331 case DW_FORM_exprloc:
0224619f 23332 case DW_FORM_data16:
b6807d98
TT
23333 result = DW_BLOCK (attr)->data;
23334 *len = DW_BLOCK (attr)->size;
23335 break;
23336
23337 /* The DW_AT_const_value attributes are supposed to carry the
23338 symbol's value "represented as it would be on the target
23339 architecture." By the time we get here, it's already been
23340 converted to host endianness, so we just need to sign- or
23341 zero-extend it as appropriate. */
23342 case DW_FORM_data1:
23343 type = die_type (die, cu);
23344 result = dwarf2_const_value_data (attr, obstack, cu, &value, 8);
23345 if (result == NULL)
23346 result = write_constant_as_bytes (obstack, byte_order,
23347 type, value, len);
23348 break;
23349 case DW_FORM_data2:
23350 type = die_type (die, cu);
23351 result = dwarf2_const_value_data (attr, obstack, cu, &value, 16);
23352 if (result == NULL)
23353 result = write_constant_as_bytes (obstack, byte_order,
23354 type, value, len);
23355 break;
23356 case DW_FORM_data4:
23357 type = die_type (die, cu);
23358 result = dwarf2_const_value_data (attr, obstack, cu, &value, 32);
23359 if (result == NULL)
23360 result = write_constant_as_bytes (obstack, byte_order,
23361 type, value, len);
23362 break;
23363 case DW_FORM_data8:
23364 type = die_type (die, cu);
23365 result = dwarf2_const_value_data (attr, obstack, cu, &value, 64);
23366 if (result == NULL)
23367 result = write_constant_as_bytes (obstack, byte_order,
23368 type, value, len);
23369 break;
23370
23371 case DW_FORM_sdata:
663c44ac 23372 case DW_FORM_implicit_const:
b6807d98
TT
23373 type = die_type (die, cu);
23374 result = write_constant_as_bytes (obstack, byte_order,
23375 type, DW_SND (attr), len);
23376 break;
23377
23378 case DW_FORM_udata:
23379 type = die_type (die, cu);
23380 result = write_constant_as_bytes (obstack, byte_order,
23381 type, DW_UNSND (attr), len);
23382 break;
23383
23384 default:
b98664d3 23385 complaint (_("unsupported const value attribute form: '%s'"),
b6807d98
TT
23386 dwarf_form_name (attr->form));
23387 break;
23388 }
23389
23390 return result;
23391}
23392
7942e96e
AA
23393/* Return the type of the die at OFFSET in PER_CU. Return NULL if no
23394 valid type for this die is found. */
23395
23396struct type *
9c541725 23397dwarf2_fetch_die_type_sect_off (sect_offset sect_off,
7942e96e
AA
23398 struct dwarf2_per_cu_data *per_cu)
23399{
23400 struct dwarf2_cu *cu;
23401 struct die_info *die;
23402
7942e96e 23403 if (per_cu->cu == NULL)
58f0c718 23404 load_cu (per_cu, false);
7942e96e
AA
23405 cu = per_cu->cu;
23406 if (!cu)
23407 return NULL;
23408
9c541725 23409 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
7942e96e
AA
23410 if (!die)
23411 return NULL;
23412
23413 return die_type (die, cu);
23414}
23415
8a9b8146
TT
23416/* Return the type of the DIE at DIE_OFFSET in the CU named by
23417 PER_CU. */
23418
23419struct type *
b64f50a1 23420dwarf2_get_die_type (cu_offset die_offset,
8a9b8146
TT
23421 struct dwarf2_per_cu_data *per_cu)
23422{
9c541725 23423 sect_offset die_offset_sect = per_cu->sect_off + to_underlying (die_offset);
b64f50a1 23424 return get_die_type_at_offset (die_offset_sect, per_cu);
8a9b8146
TT
23425}
23426
ac9ec31b 23427/* Follow type unit SIG_TYPE referenced by SRC_DIE.
348e048f 23428 On entry *REF_CU is the CU of SRC_DIE.
ac9ec31b
DE
23429 On exit *REF_CU is the CU of the result.
23430 Returns NULL if the referenced DIE isn't found. */
348e048f
DE
23431
23432static struct die_info *
ac9ec31b
DE
23433follow_die_sig_1 (struct die_info *src_die, struct signatured_type *sig_type,
23434 struct dwarf2_cu **ref_cu)
348e048f 23435{
348e048f 23436 struct die_info temp_die;
c24bdb02 23437 struct dwarf2_cu *sig_cu, *cu = *ref_cu;
348e048f
DE
23438 struct die_info *die;
23439
ac9ec31b
DE
23440 /* While it might be nice to assert sig_type->type == NULL here,
23441 we can get here for DW_AT_imported_declaration where we need
23442 the DIE not the type. */
348e048f
DE
23443
23444 /* If necessary, add it to the queue and load its DIEs. */
23445
95554aad 23446 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, language_minimal))
a0f42c21 23447 read_signatured_type (sig_type);
348e048f 23448
348e048f 23449 sig_cu = sig_type->per_cu.cu;
69d751e3 23450 gdb_assert (sig_cu != NULL);
9c541725
PA
23451 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
23452 temp_die.sect_off = sig_type->type_offset_in_section;
9a3c8263 23453 die = (struct die_info *) htab_find_with_hash (sig_cu->die_hash, &temp_die,
9c541725 23454 to_underlying (temp_die.sect_off));
348e048f
DE
23455 if (die)
23456 {
ed2dc618 23457 struct dwarf2_per_objfile *dwarf2_per_objfile
518817b3 23458 = (*ref_cu)->per_cu->dwarf2_per_objfile;
ed2dc618 23459
796a7ff8
DE
23460 /* For .gdb_index version 7 keep track of included TUs.
23461 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
23462 if (dwarf2_per_objfile->index_table != NULL
23463 && dwarf2_per_objfile->index_table->version <= 7)
23464 {
23465 VEC_safe_push (dwarf2_per_cu_ptr,
23466 (*ref_cu)->per_cu->imported_symtabs,
23467 sig_cu->per_cu);
23468 }
23469
348e048f 23470 *ref_cu = sig_cu;
c24bdb02
KS
23471 if (sig_cu != cu)
23472 sig_cu->ancestor = cu;
23473
348e048f
DE
23474 return die;
23475 }
23476
ac9ec31b
DE
23477 return NULL;
23478}
23479
23480/* Follow signatured type referenced by ATTR in SRC_DIE.
23481 On entry *REF_CU is the CU of SRC_DIE.
23482 On exit *REF_CU is the CU of the result.
23483 The result is the DIE of the type.
23484 If the referenced type cannot be found an error is thrown. */
23485
23486static struct die_info *
ff39bb5e 23487follow_die_sig (struct die_info *src_die, const struct attribute *attr,
ac9ec31b
DE
23488 struct dwarf2_cu **ref_cu)
23489{
23490 ULONGEST signature = DW_SIGNATURE (attr);
23491 struct signatured_type *sig_type;
23492 struct die_info *die;
23493
23494 gdb_assert (attr->form == DW_FORM_ref_sig8);
23495
a2ce51a0 23496 sig_type = lookup_signatured_type (*ref_cu, signature);
ac9ec31b
DE
23497 /* sig_type will be NULL if the signatured type is missing from
23498 the debug info. */
23499 if (sig_type == NULL)
23500 {
23501 error (_("Dwarf Error: Cannot find signatured DIE %s referenced"
9d8780f0
SM
23502 " from DIE at %s [in module %s]"),
23503 hex_string (signature), sect_offset_str (src_die->sect_off),
518817b3 23504 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
ac9ec31b
DE
23505 }
23506
23507 die = follow_die_sig_1 (src_die, sig_type, ref_cu);
23508 if (die == NULL)
23509 {
23510 dump_die_for_error (src_die);
23511 error (_("Dwarf Error: Problem reading signatured DIE %s referenced"
9d8780f0
SM
23512 " from DIE at %s [in module %s]"),
23513 hex_string (signature), sect_offset_str (src_die->sect_off),
518817b3 23514 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
ac9ec31b
DE
23515 }
23516
23517 return die;
23518}
23519
23520/* Get the type specified by SIGNATURE referenced in DIE/CU,
23521 reading in and processing the type unit if necessary. */
23522
23523static struct type *
23524get_signatured_type (struct die_info *die, ULONGEST signature,
23525 struct dwarf2_cu *cu)
23526{
518817b3
SM
23527 struct dwarf2_per_objfile *dwarf2_per_objfile
23528 = cu->per_cu->dwarf2_per_objfile;
ac9ec31b
DE
23529 struct signatured_type *sig_type;
23530 struct dwarf2_cu *type_cu;
23531 struct die_info *type_die;
23532 struct type *type;
23533
a2ce51a0 23534 sig_type = lookup_signatured_type (cu, signature);
ac9ec31b
DE
23535 /* sig_type will be NULL if the signatured type is missing from
23536 the debug info. */
23537 if (sig_type == NULL)
23538 {
b98664d3 23539 complaint (_("Dwarf Error: Cannot find signatured DIE %s referenced"
9d8780f0
SM
23540 " from DIE at %s [in module %s]"),
23541 hex_string (signature), sect_offset_str (die->sect_off),
4262abfb 23542 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
23543 return build_error_marker_type (cu, die);
23544 }
23545
23546 /* If we already know the type we're done. */
23547 if (sig_type->type != NULL)
23548 return sig_type->type;
23549
23550 type_cu = cu;
23551 type_die = follow_die_sig_1 (die, sig_type, &type_cu);
23552 if (type_die != NULL)
23553 {
23554 /* N.B. We need to call get_die_type to ensure only one type for this DIE
23555 is created. This is important, for example, because for c++ classes
23556 we need TYPE_NAME set which is only done by new_symbol. Blech. */
23557 type = read_type_die (type_die, type_cu);
23558 if (type == NULL)
23559 {
b98664d3 23560 complaint (_("Dwarf Error: Cannot build signatured type %s"
9d8780f0
SM
23561 " referenced from DIE at %s [in module %s]"),
23562 hex_string (signature), sect_offset_str (die->sect_off),
4262abfb 23563 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
23564 type = build_error_marker_type (cu, die);
23565 }
23566 }
23567 else
23568 {
b98664d3 23569 complaint (_("Dwarf Error: Problem reading signatured DIE %s referenced"
9d8780f0
SM
23570 " from DIE at %s [in module %s]"),
23571 hex_string (signature), sect_offset_str (die->sect_off),
4262abfb 23572 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
23573 type = build_error_marker_type (cu, die);
23574 }
23575 sig_type->type = type;
23576
23577 return type;
23578}
23579
23580/* Get the type specified by the DW_AT_signature ATTR in DIE/CU,
23581 reading in and processing the type unit if necessary. */
23582
23583static struct type *
ff39bb5e 23584get_DW_AT_signature_type (struct die_info *die, const struct attribute *attr,
b385a60d 23585 struct dwarf2_cu *cu) /* ARI: editCase function */
ac9ec31b
DE
23586{
23587 /* Yes, DW_AT_signature can use a non-ref_sig8 reference. */
7771576e 23588 if (attr_form_is_ref (attr))
ac9ec31b
DE
23589 {
23590 struct dwarf2_cu *type_cu = cu;
23591 struct die_info *type_die = follow_die_ref (die, attr, &type_cu);
23592
23593 return read_type_die (type_die, type_cu);
23594 }
23595 else if (attr->form == DW_FORM_ref_sig8)
23596 {
23597 return get_signatured_type (die, DW_SIGNATURE (attr), cu);
23598 }
23599 else
23600 {
518817b3
SM
23601 struct dwarf2_per_objfile *dwarf2_per_objfile
23602 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 23603
b98664d3 23604 complaint (_("Dwarf Error: DW_AT_signature has bad form %s in DIE"
9d8780f0
SM
23605 " at %s [in module %s]"),
23606 dwarf_form_name (attr->form), sect_offset_str (die->sect_off),
4262abfb 23607 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
23608 return build_error_marker_type (cu, die);
23609 }
348e048f
DE
23610}
23611
e5fe5e75 23612/* Load the DIEs associated with type unit PER_CU into memory. */
348e048f
DE
23613
23614static void
e5fe5e75 23615load_full_type_unit (struct dwarf2_per_cu_data *per_cu)
348e048f 23616{
52dc124a 23617 struct signatured_type *sig_type;
348e048f 23618
f4dc4d17
DE
23619 /* Caller is responsible for ensuring type_unit_groups don't get here. */
23620 gdb_assert (! IS_TYPE_UNIT_GROUP (per_cu));
23621
6721b2ec
DE
23622 /* We have the per_cu, but we need the signatured_type.
23623 Fortunately this is an easy translation. */
23624 gdb_assert (per_cu->is_debug_types);
23625 sig_type = (struct signatured_type *) per_cu;
348e048f 23626
6721b2ec 23627 gdb_assert (per_cu->cu == NULL);
348e048f 23628
52dc124a 23629 read_signatured_type (sig_type);
348e048f 23630
6721b2ec 23631 gdb_assert (per_cu->cu != NULL);
348e048f
DE
23632}
23633
dee91e82
DE
23634/* die_reader_func for read_signatured_type.
23635 This is identical to load_full_comp_unit_reader,
23636 but is kept separate for now. */
348e048f
DE
23637
23638static void
dee91e82 23639read_signatured_type_reader (const struct die_reader_specs *reader,
d521ce57 23640 const gdb_byte *info_ptr,
dee91e82
DE
23641 struct die_info *comp_unit_die,
23642 int has_children,
23643 void *data)
348e048f 23644{
dee91e82 23645 struct dwarf2_cu *cu = reader->cu;
348e048f 23646
dee91e82
DE
23647 gdb_assert (cu->die_hash == NULL);
23648 cu->die_hash =
23649 htab_create_alloc_ex (cu->header.length / 12,
23650 die_hash,
23651 die_eq,
23652 NULL,
23653 &cu->comp_unit_obstack,
23654 hashtab_obstack_allocate,
23655 dummy_obstack_deallocate);
348e048f 23656
dee91e82
DE
23657 if (has_children)
23658 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
23659 &info_ptr, comp_unit_die);
23660 cu->dies = comp_unit_die;
23661 /* comp_unit_die is not stored in die_hash, no need. */
348e048f
DE
23662
23663 /* We try not to read any attributes in this function, because not
9cdd5dbd 23664 all CUs needed for references have been loaded yet, and symbol
348e048f 23665 table processing isn't initialized. But we have to set the CU language,
dee91e82
DE
23666 or we won't be able to build types correctly.
23667 Similarly, if we do not read the producer, we can not apply
23668 producer-specific interpretation. */
95554aad 23669 prepare_one_comp_unit (cu, cu->dies, language_minimal);
dee91e82 23670}
348e048f 23671
3019eac3
DE
23672/* Read in a signatured type and build its CU and DIEs.
23673 If the type is a stub for the real type in a DWO file,
23674 read in the real type from the DWO file as well. */
dee91e82
DE
23675
23676static void
23677read_signatured_type (struct signatured_type *sig_type)
23678{
23679 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
348e048f 23680
3019eac3 23681 gdb_assert (per_cu->is_debug_types);
dee91e82 23682 gdb_assert (per_cu->cu == NULL);
348e048f 23683
58f0c718 23684 init_cutu_and_read_dies (per_cu, NULL, 0, 1, false,
f4dc4d17 23685 read_signatured_type_reader, NULL);
7ee85ab1 23686 sig_type->per_cu.tu_read = 1;
c906108c
SS
23687}
23688
c906108c
SS
23689/* Decode simple location descriptions.
23690 Given a pointer to a dwarf block that defines a location, compute
23691 the location and return the value.
23692
4cecd739
DJ
23693 NOTE drow/2003-11-18: This function is called in two situations
23694 now: for the address of static or global variables (partial symbols
23695 only) and for offsets into structures which are expected to be
23696 (more or less) constant. The partial symbol case should go away,
23697 and only the constant case should remain. That will let this
23698 function complain more accurately. A few special modes are allowed
23699 without complaint for global variables (for instance, global
23700 register values and thread-local values).
c906108c
SS
23701
23702 A location description containing no operations indicates that the
4cecd739 23703 object is optimized out. The return value is 0 for that case.
6b992462
DJ
23704 FIXME drow/2003-11-16: No callers check for this case any more; soon all
23705 callers will only want a very basic result and this can become a
21ae7a4d
JK
23706 complaint.
23707
23708 Note that stack[0] is unused except as a default error return. */
c906108c
SS
23709
23710static CORE_ADDR
e7c27a73 23711decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
c906108c 23712{
518817b3 23713 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
56eb65bd
SP
23714 size_t i;
23715 size_t size = blk->size;
d521ce57 23716 const gdb_byte *data = blk->data;
21ae7a4d
JK
23717 CORE_ADDR stack[64];
23718 int stacki;
23719 unsigned int bytes_read, unsnd;
23720 gdb_byte op;
c906108c 23721
21ae7a4d
JK
23722 i = 0;
23723 stacki = 0;
23724 stack[stacki] = 0;
23725 stack[++stacki] = 0;
23726
23727 while (i < size)
23728 {
23729 op = data[i++];
23730 switch (op)
23731 {
23732 case DW_OP_lit0:
23733 case DW_OP_lit1:
23734 case DW_OP_lit2:
23735 case DW_OP_lit3:
23736 case DW_OP_lit4:
23737 case DW_OP_lit5:
23738 case DW_OP_lit6:
23739 case DW_OP_lit7:
23740 case DW_OP_lit8:
23741 case DW_OP_lit9:
23742 case DW_OP_lit10:
23743 case DW_OP_lit11:
23744 case DW_OP_lit12:
23745 case DW_OP_lit13:
23746 case DW_OP_lit14:
23747 case DW_OP_lit15:
23748 case DW_OP_lit16:
23749 case DW_OP_lit17:
23750 case DW_OP_lit18:
23751 case DW_OP_lit19:
23752 case DW_OP_lit20:
23753 case DW_OP_lit21:
23754 case DW_OP_lit22:
23755 case DW_OP_lit23:
23756 case DW_OP_lit24:
23757 case DW_OP_lit25:
23758 case DW_OP_lit26:
23759 case DW_OP_lit27:
23760 case DW_OP_lit28:
23761 case DW_OP_lit29:
23762 case DW_OP_lit30:
23763 case DW_OP_lit31:
23764 stack[++stacki] = op - DW_OP_lit0;
23765 break;
f1bea926 23766
21ae7a4d
JK
23767 case DW_OP_reg0:
23768 case DW_OP_reg1:
23769 case DW_OP_reg2:
23770 case DW_OP_reg3:
23771 case DW_OP_reg4:
23772 case DW_OP_reg5:
23773 case DW_OP_reg6:
23774 case DW_OP_reg7:
23775 case DW_OP_reg8:
23776 case DW_OP_reg9:
23777 case DW_OP_reg10:
23778 case DW_OP_reg11:
23779 case DW_OP_reg12:
23780 case DW_OP_reg13:
23781 case DW_OP_reg14:
23782 case DW_OP_reg15:
23783 case DW_OP_reg16:
23784 case DW_OP_reg17:
23785 case DW_OP_reg18:
23786 case DW_OP_reg19:
23787 case DW_OP_reg20:
23788 case DW_OP_reg21:
23789 case DW_OP_reg22:
23790 case DW_OP_reg23:
23791 case DW_OP_reg24:
23792 case DW_OP_reg25:
23793 case DW_OP_reg26:
23794 case DW_OP_reg27:
23795 case DW_OP_reg28:
23796 case DW_OP_reg29:
23797 case DW_OP_reg30:
23798 case DW_OP_reg31:
23799 stack[++stacki] = op - DW_OP_reg0;
23800 if (i < size)
23801 dwarf2_complex_location_expr_complaint ();
23802 break;
c906108c 23803
21ae7a4d
JK
23804 case DW_OP_regx:
23805 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
23806 i += bytes_read;
23807 stack[++stacki] = unsnd;
23808 if (i < size)
23809 dwarf2_complex_location_expr_complaint ();
23810 break;
c906108c 23811
21ae7a4d
JK
23812 case DW_OP_addr:
23813 stack[++stacki] = read_address (objfile->obfd, &data[i],
23814 cu, &bytes_read);
23815 i += bytes_read;
23816 break;
d53d4ac5 23817
21ae7a4d
JK
23818 case DW_OP_const1u:
23819 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
23820 i += 1;
23821 break;
23822
23823 case DW_OP_const1s:
23824 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
23825 i += 1;
23826 break;
23827
23828 case DW_OP_const2u:
23829 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
23830 i += 2;
23831 break;
23832
23833 case DW_OP_const2s:
23834 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
23835 i += 2;
23836 break;
d53d4ac5 23837
21ae7a4d
JK
23838 case DW_OP_const4u:
23839 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
23840 i += 4;
23841 break;
23842
23843 case DW_OP_const4s:
23844 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
23845 i += 4;
23846 break;
23847
585861ea
JK
23848 case DW_OP_const8u:
23849 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
23850 i += 8;
23851 break;
23852
21ae7a4d
JK
23853 case DW_OP_constu:
23854 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
23855 &bytes_read);
23856 i += bytes_read;
23857 break;
23858
23859 case DW_OP_consts:
23860 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
23861 i += bytes_read;
23862 break;
23863
23864 case DW_OP_dup:
23865 stack[stacki + 1] = stack[stacki];
23866 stacki++;
23867 break;
23868
23869 case DW_OP_plus:
23870 stack[stacki - 1] += stack[stacki];
23871 stacki--;
23872 break;
23873
23874 case DW_OP_plus_uconst:
23875 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
23876 &bytes_read);
23877 i += bytes_read;
23878 break;
23879
23880 case DW_OP_minus:
23881 stack[stacki - 1] -= stack[stacki];
23882 stacki--;
23883 break;
23884
23885 case DW_OP_deref:
23886 /* If we're not the last op, then we definitely can't encode
23887 this using GDB's address_class enum. This is valid for partial
23888 global symbols, although the variable's address will be bogus
23889 in the psymtab. */
23890 if (i < size)
23891 dwarf2_complex_location_expr_complaint ();
23892 break;
23893
23894 case DW_OP_GNU_push_tls_address:
4aa4e28b 23895 case DW_OP_form_tls_address:
21ae7a4d
JK
23896 /* The top of the stack has the offset from the beginning
23897 of the thread control block at which the variable is located. */
23898 /* Nothing should follow this operator, so the top of stack would
23899 be returned. */
23900 /* This is valid for partial global symbols, but the variable's
585861ea
JK
23901 address will be bogus in the psymtab. Make it always at least
23902 non-zero to not look as a variable garbage collected by linker
23903 which have DW_OP_addr 0. */
21ae7a4d
JK
23904 if (i < size)
23905 dwarf2_complex_location_expr_complaint ();
585861ea 23906 stack[stacki]++;
21ae7a4d
JK
23907 break;
23908
23909 case DW_OP_GNU_uninit:
23910 break;
23911
3019eac3 23912 case DW_OP_GNU_addr_index:
49f6c839 23913 case DW_OP_GNU_const_index:
3019eac3
DE
23914 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
23915 &bytes_read);
23916 i += bytes_read;
23917 break;
23918
21ae7a4d
JK
23919 default:
23920 {
f39c6ffd 23921 const char *name = get_DW_OP_name (op);
21ae7a4d
JK
23922
23923 if (name)
b98664d3 23924 complaint (_("unsupported stack op: '%s'"),
21ae7a4d
JK
23925 name);
23926 else
b98664d3 23927 complaint (_("unsupported stack op: '%02x'"),
21ae7a4d
JK
23928 op);
23929 }
23930
23931 return (stack[stacki]);
d53d4ac5 23932 }
3c6e0cb3 23933
21ae7a4d
JK
23934 /* Enforce maximum stack depth of SIZE-1 to avoid writing
23935 outside of the allocated space. Also enforce minimum>0. */
23936 if (stacki >= ARRAY_SIZE (stack) - 1)
23937 {
b98664d3 23938 complaint (_("location description stack overflow"));
21ae7a4d
JK
23939 return 0;
23940 }
23941
23942 if (stacki <= 0)
23943 {
b98664d3 23944 complaint (_("location description stack underflow"));
21ae7a4d
JK
23945 return 0;
23946 }
23947 }
23948 return (stack[stacki]);
c906108c
SS
23949}
23950
23951/* memory allocation interface */
23952
c906108c 23953static struct dwarf_block *
7b5a2f43 23954dwarf_alloc_block (struct dwarf2_cu *cu)
c906108c 23955{
8d749320 23956 return XOBNEW (&cu->comp_unit_obstack, struct dwarf_block);
c906108c
SS
23957}
23958
c906108c 23959static struct die_info *
b60c80d6 23960dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
c906108c
SS
23961{
23962 struct die_info *die;
b60c80d6
DJ
23963 size_t size = sizeof (struct die_info);
23964
23965 if (num_attrs > 1)
23966 size += (num_attrs - 1) * sizeof (struct attribute);
c906108c 23967
b60c80d6 23968 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
c906108c
SS
23969 memset (die, 0, sizeof (struct die_info));
23970 return (die);
23971}
2e276125
JB
23972
23973\f
23974/* Macro support. */
23975
233d95b5
JK
23976/* Return file name relative to the compilation directory of file number I in
23977 *LH's file name table. The result is allocated using xmalloc; the caller is
2e276125 23978 responsible for freeing it. */
233d95b5 23979
2e276125 23980static char *
233d95b5 23981file_file_name (int file, struct line_header *lh)
2e276125 23982{
6a83a1e6
EZ
23983 /* Is the file number a valid index into the line header's file name
23984 table? Remember that file numbers start with one, not zero. */
fff8551c 23985 if (1 <= file && file <= lh->file_names.size ())
6a83a1e6 23986 {
8c43009f 23987 const file_entry &fe = lh->file_names[file - 1];
6e70227d 23988
8c43009f
PA
23989 if (!IS_ABSOLUTE_PATH (fe.name))
23990 {
23991 const char *dir = fe.include_dir (lh);
23992 if (dir != NULL)
23993 return concat (dir, SLASH_STRING, fe.name, (char *) NULL);
23994 }
23995 return xstrdup (fe.name);
6a83a1e6 23996 }
2e276125
JB
23997 else
23998 {
6a83a1e6
EZ
23999 /* The compiler produced a bogus file number. We can at least
24000 record the macro definitions made in the file, even if we
24001 won't be able to find the file by name. */
24002 char fake_name[80];
9a619af0 24003
8c042590
PM
24004 xsnprintf (fake_name, sizeof (fake_name),
24005 "<bad macro file number %d>", file);
2e276125 24006
b98664d3 24007 complaint (_("bad file number in macro information (%d)"),
6a83a1e6 24008 file);
2e276125 24009
6a83a1e6 24010 return xstrdup (fake_name);
2e276125
JB
24011 }
24012}
24013
233d95b5
JK
24014/* Return the full name of file number I in *LH's file name table.
24015 Use COMP_DIR as the name of the current directory of the
24016 compilation. The result is allocated using xmalloc; the caller is
24017 responsible for freeing it. */
24018static char *
24019file_full_name (int file, struct line_header *lh, const char *comp_dir)
24020{
24021 /* Is the file number a valid index into the line header's file name
24022 table? Remember that file numbers start with one, not zero. */
fff8551c 24023 if (1 <= file && file <= lh->file_names.size ())
233d95b5
JK
24024 {
24025 char *relative = file_file_name (file, lh);
24026
24027 if (IS_ABSOLUTE_PATH (relative) || comp_dir == NULL)
24028 return relative;
b36cec19
PA
24029 return reconcat (relative, comp_dir, SLASH_STRING,
24030 relative, (char *) NULL);
233d95b5
JK
24031 }
24032 else
24033 return file_file_name (file, lh);
24034}
24035
2e276125
JB
24036
24037static struct macro_source_file *
804d2729
TT
24038macro_start_file (struct dwarf2_cu *cu,
24039 int file, int line,
2e276125 24040 struct macro_source_file *current_file,
43f3e411 24041 struct line_header *lh)
2e276125 24042{
233d95b5
JK
24043 /* File name relative to the compilation directory of this source file. */
24044 char *file_name = file_file_name (file, lh);
2e276125 24045
2e276125 24046 if (! current_file)
abc9d0dc 24047 {
fc474241
DE
24048 /* Note: We don't create a macro table for this compilation unit
24049 at all until we actually get a filename. */
c24bdb02 24050 struct macro_table *macro_table = cu->get_builder ()->get_macro_table ();
fc474241 24051
abc9d0dc
TT
24052 /* If we have no current file, then this must be the start_file
24053 directive for the compilation unit's main source file. */
fc474241
DE
24054 current_file = macro_set_main (macro_table, file_name);
24055 macro_define_special (macro_table);
abc9d0dc 24056 }
2e276125 24057 else
233d95b5 24058 current_file = macro_include (current_file, line, file_name);
2e276125 24059
233d95b5 24060 xfree (file_name);
6e70227d 24061
2e276125
JB
24062 return current_file;
24063}
24064
2e276125
JB
24065static const char *
24066consume_improper_spaces (const char *p, const char *body)
24067{
24068 if (*p == ' ')
24069 {
b98664d3 24070 complaint (_("macro definition contains spaces "
3e43a32a 24071 "in formal argument list:\n`%s'"),
4d3c2250 24072 body);
2e276125
JB
24073
24074 while (*p == ' ')
24075 p++;
24076 }
24077
24078 return p;
24079}
24080
24081
24082static void
24083parse_macro_definition (struct macro_source_file *file, int line,
24084 const char *body)
24085{
24086 const char *p;
24087
24088 /* The body string takes one of two forms. For object-like macro
24089 definitions, it should be:
24090
24091 <macro name> " " <definition>
24092
24093 For function-like macro definitions, it should be:
24094
24095 <macro name> "() " <definition>
24096 or
24097 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
24098
24099 Spaces may appear only where explicitly indicated, and in the
24100 <definition>.
24101
24102 The Dwarf 2 spec says that an object-like macro's name is always
24103 followed by a space, but versions of GCC around March 2002 omit
6e70227d 24104 the space when the macro's definition is the empty string.
2e276125
JB
24105
24106 The Dwarf 2 spec says that there should be no spaces between the
24107 formal arguments in a function-like macro's formal argument list,
24108 but versions of GCC around March 2002 include spaces after the
24109 commas. */
24110
24111
24112 /* Find the extent of the macro name. The macro name is terminated
24113 by either a space or null character (for an object-like macro) or
24114 an opening paren (for a function-like macro). */
24115 for (p = body; *p; p++)
24116 if (*p == ' ' || *p == '(')
24117 break;
24118
24119 if (*p == ' ' || *p == '\0')
24120 {
24121 /* It's an object-like macro. */
24122 int name_len = p - body;
3f8a7804 24123 char *name = savestring (body, name_len);
2e276125
JB
24124 const char *replacement;
24125
24126 if (*p == ' ')
24127 replacement = body + name_len + 1;
24128 else
24129 {
4d3c2250 24130 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
24131 replacement = body + name_len;
24132 }
6e70227d 24133
2e276125
JB
24134 macro_define_object (file, line, name, replacement);
24135
24136 xfree (name);
24137 }
24138 else if (*p == '(')
24139 {
24140 /* It's a function-like macro. */
3f8a7804 24141 char *name = savestring (body, p - body);
2e276125
JB
24142 int argc = 0;
24143 int argv_size = 1;
8d749320 24144 char **argv = XNEWVEC (char *, argv_size);
2e276125
JB
24145
24146 p++;
24147
24148 p = consume_improper_spaces (p, body);
24149
24150 /* Parse the formal argument list. */
24151 while (*p && *p != ')')
24152 {
24153 /* Find the extent of the current argument name. */
24154 const char *arg_start = p;
24155
24156 while (*p && *p != ',' && *p != ')' && *p != ' ')
24157 p++;
24158
24159 if (! *p || p == arg_start)
4d3c2250 24160 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
24161 else
24162 {
24163 /* Make sure argv has room for the new argument. */
24164 if (argc >= argv_size)
24165 {
24166 argv_size *= 2;
224c3ddb 24167 argv = XRESIZEVEC (char *, argv, argv_size);
2e276125
JB
24168 }
24169
3f8a7804 24170 argv[argc++] = savestring (arg_start, p - arg_start);
2e276125
JB
24171 }
24172
24173 p = consume_improper_spaces (p, body);
24174
24175 /* Consume the comma, if present. */
24176 if (*p == ',')
24177 {
24178 p++;
24179
24180 p = consume_improper_spaces (p, body);
24181 }
24182 }
24183
24184 if (*p == ')')
24185 {
24186 p++;
24187
24188 if (*p == ' ')
24189 /* Perfectly formed definition, no complaints. */
24190 macro_define_function (file, line, name,
6e70227d 24191 argc, (const char **) argv,
2e276125
JB
24192 p + 1);
24193 else if (*p == '\0')
24194 {
24195 /* Complain, but do define it. */
4d3c2250 24196 dwarf2_macro_malformed_definition_complaint (body);
2e276125 24197 macro_define_function (file, line, name,
6e70227d 24198 argc, (const char **) argv,
2e276125
JB
24199 p);
24200 }
24201 else
24202 /* Just complain. */
4d3c2250 24203 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
24204 }
24205 else
24206 /* Just complain. */
4d3c2250 24207 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
24208
24209 xfree (name);
24210 {
24211 int i;
24212
24213 for (i = 0; i < argc; i++)
24214 xfree (argv[i]);
24215 }
24216 xfree (argv);
24217 }
24218 else
4d3c2250 24219 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
24220}
24221
cf2c3c16
TT
24222/* Skip some bytes from BYTES according to the form given in FORM.
24223 Returns the new pointer. */
2e276125 24224
d521ce57
TT
24225static const gdb_byte *
24226skip_form_bytes (bfd *abfd, const gdb_byte *bytes, const gdb_byte *buffer_end,
cf2c3c16
TT
24227 enum dwarf_form form,
24228 unsigned int offset_size,
24229 struct dwarf2_section_info *section)
2e276125 24230{
cf2c3c16 24231 unsigned int bytes_read;
2e276125 24232
cf2c3c16 24233 switch (form)
2e276125 24234 {
cf2c3c16
TT
24235 case DW_FORM_data1:
24236 case DW_FORM_flag:
24237 ++bytes;
24238 break;
24239
24240 case DW_FORM_data2:
24241 bytes += 2;
24242 break;
24243
24244 case DW_FORM_data4:
24245 bytes += 4;
24246 break;
24247
24248 case DW_FORM_data8:
24249 bytes += 8;
24250 break;
24251
0224619f
JK
24252 case DW_FORM_data16:
24253 bytes += 16;
24254 break;
24255
cf2c3c16
TT
24256 case DW_FORM_string:
24257 read_direct_string (abfd, bytes, &bytes_read);
24258 bytes += bytes_read;
24259 break;
24260
24261 case DW_FORM_sec_offset:
24262 case DW_FORM_strp:
36586728 24263 case DW_FORM_GNU_strp_alt:
cf2c3c16
TT
24264 bytes += offset_size;
24265 break;
24266
24267 case DW_FORM_block:
24268 bytes += read_unsigned_leb128 (abfd, bytes, &bytes_read);
24269 bytes += bytes_read;
24270 break;
24271
24272 case DW_FORM_block1:
24273 bytes += 1 + read_1_byte (abfd, bytes);
24274 break;
24275 case DW_FORM_block2:
24276 bytes += 2 + read_2_bytes (abfd, bytes);
24277 break;
24278 case DW_FORM_block4:
24279 bytes += 4 + read_4_bytes (abfd, bytes);
24280 break;
24281
24282 case DW_FORM_sdata:
24283 case DW_FORM_udata:
3019eac3
DE
24284 case DW_FORM_GNU_addr_index:
24285 case DW_FORM_GNU_str_index:
d521ce57 24286 bytes = gdb_skip_leb128 (bytes, buffer_end);
f664829e
DE
24287 if (bytes == NULL)
24288 {
24289 dwarf2_section_buffer_overflow_complaint (section);
24290 return NULL;
24291 }
cf2c3c16
TT
24292 break;
24293
663c44ac
JK
24294 case DW_FORM_implicit_const:
24295 break;
24296
cf2c3c16
TT
24297 default:
24298 {
b98664d3 24299 complaint (_("invalid form 0x%x in `%s'"),
a32a8923 24300 form, get_section_name (section));
cf2c3c16
TT
24301 return NULL;
24302 }
2e276125
JB
24303 }
24304
cf2c3c16
TT
24305 return bytes;
24306}
757a13d0 24307
cf2c3c16
TT
24308/* A helper for dwarf_decode_macros that handles skipping an unknown
24309 opcode. Returns an updated pointer to the macro data buffer; or,
24310 on error, issues a complaint and returns NULL. */
757a13d0 24311
d521ce57 24312static const gdb_byte *
cf2c3c16 24313skip_unknown_opcode (unsigned int opcode,
d521ce57
TT
24314 const gdb_byte **opcode_definitions,
24315 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
cf2c3c16
TT
24316 bfd *abfd,
24317 unsigned int offset_size,
24318 struct dwarf2_section_info *section)
24319{
24320 unsigned int bytes_read, i;
24321 unsigned long arg;
d521ce57 24322 const gdb_byte *defn;
2e276125 24323
cf2c3c16 24324 if (opcode_definitions[opcode] == NULL)
2e276125 24325 {
b98664d3 24326 complaint (_("unrecognized DW_MACFINO opcode 0x%x"),
cf2c3c16
TT
24327 opcode);
24328 return NULL;
24329 }
2e276125 24330
cf2c3c16
TT
24331 defn = opcode_definitions[opcode];
24332 arg = read_unsigned_leb128 (abfd, defn, &bytes_read);
24333 defn += bytes_read;
2e276125 24334
cf2c3c16
TT
24335 for (i = 0; i < arg; ++i)
24336 {
aead7601
SM
24337 mac_ptr = skip_form_bytes (abfd, mac_ptr, mac_end,
24338 (enum dwarf_form) defn[i], offset_size,
f664829e 24339 section);
cf2c3c16
TT
24340 if (mac_ptr == NULL)
24341 {
24342 /* skip_form_bytes already issued the complaint. */
24343 return NULL;
24344 }
24345 }
757a13d0 24346
cf2c3c16
TT
24347 return mac_ptr;
24348}
757a13d0 24349
cf2c3c16
TT
24350/* A helper function which parses the header of a macro section.
24351 If the macro section is the extended (for now called "GNU") type,
24352 then this updates *OFFSET_SIZE. Returns a pointer to just after
24353 the header, or issues a complaint and returns NULL on error. */
757a13d0 24354
d521ce57
TT
24355static const gdb_byte *
24356dwarf_parse_macro_header (const gdb_byte **opcode_definitions,
cf2c3c16 24357 bfd *abfd,
d521ce57 24358 const gdb_byte *mac_ptr,
cf2c3c16
TT
24359 unsigned int *offset_size,
24360 int section_is_gnu)
24361{
24362 memset (opcode_definitions, 0, 256 * sizeof (gdb_byte *));
757a13d0 24363
cf2c3c16
TT
24364 if (section_is_gnu)
24365 {
24366 unsigned int version, flags;
757a13d0 24367
cf2c3c16 24368 version = read_2_bytes (abfd, mac_ptr);
0af92d60 24369 if (version != 4 && version != 5)
cf2c3c16 24370 {
b98664d3 24371 complaint (_("unrecognized version `%d' in .debug_macro section"),
cf2c3c16
TT
24372 version);
24373 return NULL;
24374 }
24375 mac_ptr += 2;
757a13d0 24376
cf2c3c16
TT
24377 flags = read_1_byte (abfd, mac_ptr);
24378 ++mac_ptr;
24379 *offset_size = (flags & 1) ? 8 : 4;
757a13d0 24380
cf2c3c16
TT
24381 if ((flags & 2) != 0)
24382 /* We don't need the line table offset. */
24383 mac_ptr += *offset_size;
757a13d0 24384
cf2c3c16
TT
24385 /* Vendor opcode descriptions. */
24386 if ((flags & 4) != 0)
24387 {
24388 unsigned int i, count;
757a13d0 24389
cf2c3c16
TT
24390 count = read_1_byte (abfd, mac_ptr);
24391 ++mac_ptr;
24392 for (i = 0; i < count; ++i)
24393 {
24394 unsigned int opcode, bytes_read;
24395 unsigned long arg;
24396
24397 opcode = read_1_byte (abfd, mac_ptr);
24398 ++mac_ptr;
24399 opcode_definitions[opcode] = mac_ptr;
24400 arg = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24401 mac_ptr += bytes_read;
24402 mac_ptr += arg;
24403 }
757a13d0 24404 }
cf2c3c16 24405 }
757a13d0 24406
cf2c3c16
TT
24407 return mac_ptr;
24408}
757a13d0 24409
cf2c3c16 24410/* A helper for dwarf_decode_macros that handles the GNU extensions,
0af92d60 24411 including DW_MACRO_import. */
cf2c3c16
TT
24412
24413static void
804d2729 24414dwarf_decode_macro_bytes (struct dwarf2_cu *cu,
ed2dc618 24415 bfd *abfd,
d521ce57 24416 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
cf2c3c16 24417 struct macro_source_file *current_file,
43f3e411 24418 struct line_header *lh,
cf2c3c16 24419 struct dwarf2_section_info *section,
36586728 24420 int section_is_gnu, int section_is_dwz,
cf2c3c16 24421 unsigned int offset_size,
8fc3fc34 24422 htab_t include_hash)
cf2c3c16 24423{
804d2729
TT
24424 struct dwarf2_per_objfile *dwarf2_per_objfile
24425 = cu->per_cu->dwarf2_per_objfile;
4d663531 24426 struct objfile *objfile = dwarf2_per_objfile->objfile;
cf2c3c16
TT
24427 enum dwarf_macro_record_type macinfo_type;
24428 int at_commandline;
d521ce57 24429 const gdb_byte *opcode_definitions[256];
757a13d0 24430
cf2c3c16
TT
24431 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
24432 &offset_size, section_is_gnu);
24433 if (mac_ptr == NULL)
24434 {
24435 /* We already issued a complaint. */
24436 return;
24437 }
757a13d0
JK
24438
24439 /* Determines if GDB is still before first DW_MACINFO_start_file. If true
24440 GDB is still reading the definitions from command line. First
24441 DW_MACINFO_start_file will need to be ignored as it was already executed
24442 to create CURRENT_FILE for the main source holding also the command line
24443 definitions. On first met DW_MACINFO_start_file this flag is reset to
24444 normally execute all the remaining DW_MACINFO_start_file macinfos. */
24445
24446 at_commandline = 1;
24447
24448 do
24449 {
24450 /* Do we at least have room for a macinfo type byte? */
24451 if (mac_ptr >= mac_end)
24452 {
f664829e 24453 dwarf2_section_buffer_overflow_complaint (section);
757a13d0
JK
24454 break;
24455 }
24456
aead7601 24457 macinfo_type = (enum dwarf_macro_record_type) read_1_byte (abfd, mac_ptr);
757a13d0
JK
24458 mac_ptr++;
24459
cf2c3c16
TT
24460 /* Note that we rely on the fact that the corresponding GNU and
24461 DWARF constants are the same. */
132448f8
SM
24462 DIAGNOSTIC_PUSH
24463 DIAGNOSTIC_IGNORE_SWITCH_DIFFERENT_ENUM_TYPES
757a13d0
JK
24464 switch (macinfo_type)
24465 {
24466 /* A zero macinfo type indicates the end of the macro
24467 information. */
24468 case 0:
24469 break;
2e276125 24470
0af92d60
JK
24471 case DW_MACRO_define:
24472 case DW_MACRO_undef:
24473 case DW_MACRO_define_strp:
24474 case DW_MACRO_undef_strp:
24475 case DW_MACRO_define_sup:
24476 case DW_MACRO_undef_sup:
2e276125 24477 {
891d2f0b 24478 unsigned int bytes_read;
2e276125 24479 int line;
d521ce57 24480 const char *body;
cf2c3c16 24481 int is_define;
2e276125 24482
cf2c3c16
TT
24483 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24484 mac_ptr += bytes_read;
24485
0af92d60
JK
24486 if (macinfo_type == DW_MACRO_define
24487 || macinfo_type == DW_MACRO_undef)
cf2c3c16
TT
24488 {
24489 body = read_direct_string (abfd, mac_ptr, &bytes_read);
24490 mac_ptr += bytes_read;
24491 }
24492 else
24493 {
24494 LONGEST str_offset;
24495
24496 str_offset = read_offset_1 (abfd, mac_ptr, offset_size);
24497 mac_ptr += offset_size;
2e276125 24498
0af92d60
JK
24499 if (macinfo_type == DW_MACRO_define_sup
24500 || macinfo_type == DW_MACRO_undef_sup
f7a35f02 24501 || section_is_dwz)
36586728 24502 {
ed2dc618
SM
24503 struct dwz_file *dwz
24504 = dwarf2_get_dwz_file (dwarf2_per_objfile);
36586728 24505
ed2dc618
SM
24506 body = read_indirect_string_from_dwz (objfile,
24507 dwz, str_offset);
36586728
TT
24508 }
24509 else
ed2dc618
SM
24510 body = read_indirect_string_at_offset (dwarf2_per_objfile,
24511 abfd, str_offset);
cf2c3c16
TT
24512 }
24513
0af92d60
JK
24514 is_define = (macinfo_type == DW_MACRO_define
24515 || macinfo_type == DW_MACRO_define_strp
24516 || macinfo_type == DW_MACRO_define_sup);
2e276125 24517 if (! current_file)
757a13d0
JK
24518 {
24519 /* DWARF violation as no main source is present. */
b98664d3 24520 complaint (_("debug info with no main source gives macro %s "
757a13d0 24521 "on line %d: %s"),
cf2c3c16
TT
24522 is_define ? _("definition") : _("undefinition"),
24523 line, body);
757a13d0
JK
24524 break;
24525 }
3e43a32a
MS
24526 if ((line == 0 && !at_commandline)
24527 || (line != 0 && at_commandline))
b98664d3 24528 complaint (_("debug info gives %s macro %s with %s line %d: %s"),
757a13d0 24529 at_commandline ? _("command-line") : _("in-file"),
cf2c3c16 24530 is_define ? _("definition") : _("undefinition"),
757a13d0
JK
24531 line == 0 ? _("zero") : _("non-zero"), line, body);
24532
cf2c3c16 24533 if (is_define)
757a13d0 24534 parse_macro_definition (current_file, line, body);
cf2c3c16
TT
24535 else
24536 {
0af92d60
JK
24537 gdb_assert (macinfo_type == DW_MACRO_undef
24538 || macinfo_type == DW_MACRO_undef_strp
24539 || macinfo_type == DW_MACRO_undef_sup);
cf2c3c16
TT
24540 macro_undef (current_file, line, body);
24541 }
2e276125
JB
24542 }
24543 break;
24544
0af92d60 24545 case DW_MACRO_start_file:
2e276125 24546 {
891d2f0b 24547 unsigned int bytes_read;
2e276125
JB
24548 int line, file;
24549
24550 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24551 mac_ptr += bytes_read;
24552 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24553 mac_ptr += bytes_read;
24554
3e43a32a
MS
24555 if ((line == 0 && !at_commandline)
24556 || (line != 0 && at_commandline))
b98664d3 24557 complaint (_("debug info gives source %d included "
757a13d0
JK
24558 "from %s at %s line %d"),
24559 file, at_commandline ? _("command-line") : _("file"),
24560 line == 0 ? _("zero") : _("non-zero"), line);
24561
24562 if (at_commandline)
24563 {
0af92d60 24564 /* This DW_MACRO_start_file was executed in the
cf2c3c16 24565 pass one. */
757a13d0
JK
24566 at_commandline = 0;
24567 }
24568 else
804d2729
TT
24569 current_file = macro_start_file (cu, file, line, current_file,
24570 lh);
2e276125
JB
24571 }
24572 break;
24573
0af92d60 24574 case DW_MACRO_end_file:
2e276125 24575 if (! current_file)
b98664d3 24576 complaint (_("macro debug info has an unmatched "
3e43a32a 24577 "`close_file' directive"));
2e276125
JB
24578 else
24579 {
24580 current_file = current_file->included_by;
24581 if (! current_file)
24582 {
cf2c3c16 24583 enum dwarf_macro_record_type next_type;
2e276125
JB
24584
24585 /* GCC circa March 2002 doesn't produce the zero
24586 type byte marking the end of the compilation
24587 unit. Complain if it's not there, but exit no
24588 matter what. */
24589
24590 /* Do we at least have room for a macinfo type byte? */
24591 if (mac_ptr >= mac_end)
24592 {
f664829e 24593 dwarf2_section_buffer_overflow_complaint (section);
2e276125
JB
24594 return;
24595 }
24596
24597 /* We don't increment mac_ptr here, so this is just
24598 a look-ahead. */
aead7601
SM
24599 next_type
24600 = (enum dwarf_macro_record_type) read_1_byte (abfd,
24601 mac_ptr);
2e276125 24602 if (next_type != 0)
b98664d3 24603 complaint (_("no terminating 0-type entry for "
3e43a32a 24604 "macros in `.debug_macinfo' section"));
2e276125
JB
24605
24606 return;
24607 }
24608 }
24609 break;
24610
0af92d60
JK
24611 case DW_MACRO_import:
24612 case DW_MACRO_import_sup:
cf2c3c16
TT
24613 {
24614 LONGEST offset;
8fc3fc34 24615 void **slot;
a036ba48
TT
24616 bfd *include_bfd = abfd;
24617 struct dwarf2_section_info *include_section = section;
d521ce57 24618 const gdb_byte *include_mac_end = mac_end;
a036ba48 24619 int is_dwz = section_is_dwz;
d521ce57 24620 const gdb_byte *new_mac_ptr;
cf2c3c16
TT
24621
24622 offset = read_offset_1 (abfd, mac_ptr, offset_size);
24623 mac_ptr += offset_size;
24624
0af92d60 24625 if (macinfo_type == DW_MACRO_import_sup)
a036ba48 24626 {
ed2dc618 24627 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
a036ba48 24628
4d663531 24629 dwarf2_read_section (objfile, &dwz->macro);
a036ba48 24630
a036ba48 24631 include_section = &dwz->macro;
a32a8923 24632 include_bfd = get_section_bfd_owner (include_section);
a036ba48
TT
24633 include_mac_end = dwz->macro.buffer + dwz->macro.size;
24634 is_dwz = 1;
24635 }
24636
24637 new_mac_ptr = include_section->buffer + offset;
24638 slot = htab_find_slot (include_hash, new_mac_ptr, INSERT);
24639
8fc3fc34
TT
24640 if (*slot != NULL)
24641 {
24642 /* This has actually happened; see
24643 http://sourceware.org/bugzilla/show_bug.cgi?id=13568. */
b98664d3 24644 complaint (_("recursive DW_MACRO_import in "
8fc3fc34
TT
24645 ".debug_macro section"));
24646 }
24647 else
24648 {
d521ce57 24649 *slot = (void *) new_mac_ptr;
36586728 24650
804d2729 24651 dwarf_decode_macro_bytes (cu, include_bfd, new_mac_ptr,
43f3e411 24652 include_mac_end, current_file, lh,
36586728 24653 section, section_is_gnu, is_dwz,
4d663531 24654 offset_size, include_hash);
8fc3fc34 24655
d521ce57 24656 htab_remove_elt (include_hash, (void *) new_mac_ptr);
8fc3fc34 24657 }
cf2c3c16
TT
24658 }
24659 break;
24660
2e276125 24661 case DW_MACINFO_vendor_ext:
cf2c3c16
TT
24662 if (!section_is_gnu)
24663 {
24664 unsigned int bytes_read;
2e276125 24665
ac298888
TT
24666 /* This reads the constant, but since we don't recognize
24667 any vendor extensions, we ignore it. */
24668 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
cf2c3c16
TT
24669 mac_ptr += bytes_read;
24670 read_direct_string (abfd, mac_ptr, &bytes_read);
24671 mac_ptr += bytes_read;
2e276125 24672
cf2c3c16
TT
24673 /* We don't recognize any vendor extensions. */
24674 break;
24675 }
24676 /* FALLTHROUGH */
24677
24678 default:
24679 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
f664829e 24680 mac_ptr, mac_end, abfd, offset_size,
cf2c3c16
TT
24681 section);
24682 if (mac_ptr == NULL)
24683 return;
24684 break;
2e276125 24685 }
132448f8 24686 DIAGNOSTIC_POP
757a13d0 24687 } while (macinfo_type != 0);
2e276125 24688}
8e19ed76 24689
cf2c3c16 24690static void
09262596 24691dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
43f3e411 24692 int section_is_gnu)
cf2c3c16 24693{
518817b3
SM
24694 struct dwarf2_per_objfile *dwarf2_per_objfile
24695 = cu->per_cu->dwarf2_per_objfile;
bb5ed363 24696 struct objfile *objfile = dwarf2_per_objfile->objfile;
09262596
DE
24697 struct line_header *lh = cu->line_header;
24698 bfd *abfd;
d521ce57 24699 const gdb_byte *mac_ptr, *mac_end;
cf2c3c16
TT
24700 struct macro_source_file *current_file = 0;
24701 enum dwarf_macro_record_type macinfo_type;
24702 unsigned int offset_size = cu->header.offset_size;
d521ce57 24703 const gdb_byte *opcode_definitions[256];
8fc3fc34 24704 void **slot;
09262596
DE
24705 struct dwarf2_section_info *section;
24706 const char *section_name;
24707
24708 if (cu->dwo_unit != NULL)
24709 {
24710 if (section_is_gnu)
24711 {
24712 section = &cu->dwo_unit->dwo_file->sections.macro;
24713 section_name = ".debug_macro.dwo";
24714 }
24715 else
24716 {
24717 section = &cu->dwo_unit->dwo_file->sections.macinfo;
24718 section_name = ".debug_macinfo.dwo";
24719 }
24720 }
24721 else
24722 {
24723 if (section_is_gnu)
24724 {
24725 section = &dwarf2_per_objfile->macro;
24726 section_name = ".debug_macro";
24727 }
24728 else
24729 {
24730 section = &dwarf2_per_objfile->macinfo;
24731 section_name = ".debug_macinfo";
24732 }
24733 }
cf2c3c16 24734
bb5ed363 24735 dwarf2_read_section (objfile, section);
cf2c3c16
TT
24736 if (section->buffer == NULL)
24737 {
b98664d3 24738 complaint (_("missing %s section"), section_name);
cf2c3c16
TT
24739 return;
24740 }
a32a8923 24741 abfd = get_section_bfd_owner (section);
cf2c3c16
TT
24742
24743 /* First pass: Find the name of the base filename.
24744 This filename is needed in order to process all macros whose definition
24745 (or undefinition) comes from the command line. These macros are defined
24746 before the first DW_MACINFO_start_file entry, and yet still need to be
24747 associated to the base file.
24748
24749 To determine the base file name, we scan the macro definitions until we
24750 reach the first DW_MACINFO_start_file entry. We then initialize
24751 CURRENT_FILE accordingly so that any macro definition found before the
24752 first DW_MACINFO_start_file can still be associated to the base file. */
24753
24754 mac_ptr = section->buffer + offset;
24755 mac_end = section->buffer + section->size;
24756
24757 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
24758 &offset_size, section_is_gnu);
24759 if (mac_ptr == NULL)
24760 {
24761 /* We already issued a complaint. */
24762 return;
24763 }
24764
24765 do
24766 {
24767 /* Do we at least have room for a macinfo type byte? */
24768 if (mac_ptr >= mac_end)
24769 {
24770 /* Complaint is printed during the second pass as GDB will probably
24771 stop the first pass earlier upon finding
24772 DW_MACINFO_start_file. */
24773 break;
24774 }
24775
aead7601 24776 macinfo_type = (enum dwarf_macro_record_type) read_1_byte (abfd, mac_ptr);
cf2c3c16
TT
24777 mac_ptr++;
24778
24779 /* Note that we rely on the fact that the corresponding GNU and
24780 DWARF constants are the same. */
132448f8
SM
24781 DIAGNOSTIC_PUSH
24782 DIAGNOSTIC_IGNORE_SWITCH_DIFFERENT_ENUM_TYPES
cf2c3c16
TT
24783 switch (macinfo_type)
24784 {
24785 /* A zero macinfo type indicates the end of the macro
24786 information. */
24787 case 0:
24788 break;
24789
0af92d60
JK
24790 case DW_MACRO_define:
24791 case DW_MACRO_undef:
cf2c3c16
TT
24792 /* Only skip the data by MAC_PTR. */
24793 {
24794 unsigned int bytes_read;
24795
24796 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24797 mac_ptr += bytes_read;
24798 read_direct_string (abfd, mac_ptr, &bytes_read);
24799 mac_ptr += bytes_read;
24800 }
24801 break;
24802
0af92d60 24803 case DW_MACRO_start_file:
cf2c3c16
TT
24804 {
24805 unsigned int bytes_read;
24806 int line, file;
24807
24808 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24809 mac_ptr += bytes_read;
24810 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24811 mac_ptr += bytes_read;
24812
804d2729 24813 current_file = macro_start_file (cu, file, line, current_file, lh);
cf2c3c16
TT
24814 }
24815 break;
24816
0af92d60 24817 case DW_MACRO_end_file:
cf2c3c16
TT
24818 /* No data to skip by MAC_PTR. */
24819 break;
24820
0af92d60
JK
24821 case DW_MACRO_define_strp:
24822 case DW_MACRO_undef_strp:
24823 case DW_MACRO_define_sup:
24824 case DW_MACRO_undef_sup:
cf2c3c16
TT
24825 {
24826 unsigned int bytes_read;
24827
24828 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24829 mac_ptr += bytes_read;
24830 mac_ptr += offset_size;
24831 }
24832 break;
24833
0af92d60
JK
24834 case DW_MACRO_import:
24835 case DW_MACRO_import_sup:
cf2c3c16 24836 /* Note that, according to the spec, a transparent include
0af92d60 24837 chain cannot call DW_MACRO_start_file. So, we can just
cf2c3c16
TT
24838 skip this opcode. */
24839 mac_ptr += offset_size;
24840 break;
24841
24842 case DW_MACINFO_vendor_ext:
24843 /* Only skip the data by MAC_PTR. */
24844 if (!section_is_gnu)
24845 {
24846 unsigned int bytes_read;
24847
24848 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24849 mac_ptr += bytes_read;
24850 read_direct_string (abfd, mac_ptr, &bytes_read);
24851 mac_ptr += bytes_read;
24852 }
24853 /* FALLTHROUGH */
24854
24855 default:
24856 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
f664829e 24857 mac_ptr, mac_end, abfd, offset_size,
cf2c3c16
TT
24858 section);
24859 if (mac_ptr == NULL)
24860 return;
24861 break;
24862 }
132448f8 24863 DIAGNOSTIC_POP
cf2c3c16
TT
24864 } while (macinfo_type != 0 && current_file == NULL);
24865
24866 /* Second pass: Process all entries.
24867
24868 Use the AT_COMMAND_LINE flag to determine whether we are still processing
24869 command-line macro definitions/undefinitions. This flag is unset when we
24870 reach the first DW_MACINFO_start_file entry. */
24871
fc4007c9
TT
24872 htab_up include_hash (htab_create_alloc (1, htab_hash_pointer,
24873 htab_eq_pointer,
24874 NULL, xcalloc, xfree));
8fc3fc34 24875 mac_ptr = section->buffer + offset;
fc4007c9 24876 slot = htab_find_slot (include_hash.get (), mac_ptr, INSERT);
d521ce57 24877 *slot = (void *) mac_ptr;
804d2729 24878 dwarf_decode_macro_bytes (cu, abfd, mac_ptr, mac_end,
43f3e411 24879 current_file, lh, section,
fc4007c9
TT
24880 section_is_gnu, 0, offset_size,
24881 include_hash.get ());
cf2c3c16
TT
24882}
24883
8e19ed76 24884/* Check if the attribute's form is a DW_FORM_block*
0963b4bd 24885 if so return true else false. */
380bca97 24886
8e19ed76 24887static int
6e5a29e1 24888attr_form_is_block (const struct attribute *attr)
8e19ed76
PS
24889{
24890 return (attr == NULL ? 0 :
24891 attr->form == DW_FORM_block1
24892 || attr->form == DW_FORM_block2
24893 || attr->form == DW_FORM_block4
2dc7f7b3
TT
24894 || attr->form == DW_FORM_block
24895 || attr->form == DW_FORM_exprloc);
8e19ed76 24896}
4c2df51b 24897
c6a0999f
JB
24898/* Return non-zero if ATTR's value is a section offset --- classes
24899 lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
24900 You may use DW_UNSND (attr) to retrieve such offsets.
24901
24902 Section 7.5.4, "Attribute Encodings", explains that no attribute
24903 may have a value that belongs to more than one of these classes; it
24904 would be ambiguous if we did, because we use the same forms for all
24905 of them. */
380bca97 24906
3690dd37 24907static int
6e5a29e1 24908attr_form_is_section_offset (const struct attribute *attr)
3690dd37
JB
24909{
24910 return (attr->form == DW_FORM_data4
2dc7f7b3
TT
24911 || attr->form == DW_FORM_data8
24912 || attr->form == DW_FORM_sec_offset);
3690dd37
JB
24913}
24914
3690dd37
JB
24915/* Return non-zero if ATTR's value falls in the 'constant' class, or
24916 zero otherwise. When this function returns true, you can apply
24917 dwarf2_get_attr_constant_value to it.
24918
24919 However, note that for some attributes you must check
24920 attr_form_is_section_offset before using this test. DW_FORM_data4
24921 and DW_FORM_data8 are members of both the constant class, and of
24922 the classes that contain offsets into other debug sections
24923 (lineptr, loclistptr, macptr or rangelistptr). The DWARF spec says
24924 that, if an attribute's can be either a constant or one of the
24925 section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
0224619f
JK
24926 taken as section offsets, not constants.
24927
24928 DW_FORM_data16 is not considered as dwarf2_get_attr_constant_value
24929 cannot handle that. */
380bca97 24930
3690dd37 24931static int
6e5a29e1 24932attr_form_is_constant (const struct attribute *attr)
3690dd37
JB
24933{
24934 switch (attr->form)
24935 {
24936 case DW_FORM_sdata:
24937 case DW_FORM_udata:
24938 case DW_FORM_data1:
24939 case DW_FORM_data2:
24940 case DW_FORM_data4:
24941 case DW_FORM_data8:
663c44ac 24942 case DW_FORM_implicit_const:
3690dd37
JB
24943 return 1;
24944 default:
24945 return 0;
24946 }
24947}
24948
7771576e
SA
24949
24950/* DW_ADDR is always stored already as sect_offset; despite for the forms
24951 besides DW_FORM_ref_addr it is stored as cu_offset in the DWARF file. */
24952
24953static int
6e5a29e1 24954attr_form_is_ref (const struct attribute *attr)
7771576e
SA
24955{
24956 switch (attr->form)
24957 {
24958 case DW_FORM_ref_addr:
24959 case DW_FORM_ref1:
24960 case DW_FORM_ref2:
24961 case DW_FORM_ref4:
24962 case DW_FORM_ref8:
24963 case DW_FORM_ref_udata:
24964 case DW_FORM_GNU_ref_alt:
24965 return 1;
24966 default:
24967 return 0;
24968 }
24969}
24970
3019eac3
DE
24971/* Return the .debug_loc section to use for CU.
24972 For DWO files use .debug_loc.dwo. */
24973
24974static struct dwarf2_section_info *
24975cu_debug_loc_section (struct dwarf2_cu *cu)
24976{
518817b3
SM
24977 struct dwarf2_per_objfile *dwarf2_per_objfile
24978 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 24979
3019eac3 24980 if (cu->dwo_unit)
43988095
JK
24981 {
24982 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
24983
24984 return cu->header.version >= 5 ? &sections->loclists : &sections->loc;
24985 }
24986 return (cu->header.version >= 5 ? &dwarf2_per_objfile->loclists
24987 : &dwarf2_per_objfile->loc);
3019eac3
DE
24988}
24989
8cf6f0b1
TT
24990/* A helper function that fills in a dwarf2_loclist_baton. */
24991
24992static void
24993fill_in_loclist_baton (struct dwarf2_cu *cu,
24994 struct dwarf2_loclist_baton *baton,
ff39bb5e 24995 const struct attribute *attr)
8cf6f0b1 24996{
518817b3
SM
24997 struct dwarf2_per_objfile *dwarf2_per_objfile
24998 = cu->per_cu->dwarf2_per_objfile;
3019eac3
DE
24999 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
25000
25001 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
8cf6f0b1
TT
25002
25003 baton->per_cu = cu->per_cu;
25004 gdb_assert (baton->per_cu);
25005 /* We don't know how long the location list is, but make sure we
25006 don't run off the edge of the section. */
3019eac3
DE
25007 baton->size = section->size - DW_UNSND (attr);
25008 baton->data = section->buffer + DW_UNSND (attr);
8cf6f0b1 25009 baton->base_address = cu->base_address;
f664829e 25010 baton->from_dwo = cu->dwo_unit != NULL;
8cf6f0b1
TT
25011}
25012
4c2df51b 25013static void
ff39bb5e 25014dwarf2_symbol_mark_computed (const struct attribute *attr, struct symbol *sym,
f1e6e072 25015 struct dwarf2_cu *cu, int is_block)
4c2df51b 25016{
518817b3
SM
25017 struct dwarf2_per_objfile *dwarf2_per_objfile
25018 = cu->per_cu->dwarf2_per_objfile;
bb5ed363 25019 struct objfile *objfile = dwarf2_per_objfile->objfile;
3019eac3 25020 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
bb5ed363 25021
3690dd37 25022 if (attr_form_is_section_offset (attr)
3019eac3 25023 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
99bcc461
DJ
25024 the section. If so, fall through to the complaint in the
25025 other branch. */
3019eac3 25026 && DW_UNSND (attr) < dwarf2_section_size (objfile, section))
4c2df51b 25027 {
0d53c4c4 25028 struct dwarf2_loclist_baton *baton;
4c2df51b 25029
8d749320 25030 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_loclist_baton);
4c2df51b 25031
8cf6f0b1 25032 fill_in_loclist_baton (cu, baton, attr);
be391dca 25033
d00adf39 25034 if (cu->base_known == 0)
b98664d3 25035 complaint (_("Location list used without "
3e43a32a 25036 "specifying the CU base address."));
4c2df51b 25037
f1e6e072
TT
25038 SYMBOL_ACLASS_INDEX (sym) = (is_block
25039 ? dwarf2_loclist_block_index
25040 : dwarf2_loclist_index);
0d53c4c4
DJ
25041 SYMBOL_LOCATION_BATON (sym) = baton;
25042 }
25043 else
25044 {
25045 struct dwarf2_locexpr_baton *baton;
25046
8d749320 25047 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
ae0d2f24
UW
25048 baton->per_cu = cu->per_cu;
25049 gdb_assert (baton->per_cu);
0d53c4c4
DJ
25050
25051 if (attr_form_is_block (attr))
25052 {
25053 /* Note that we're just copying the block's data pointer
25054 here, not the actual data. We're still pointing into the
6502dd73
DJ
25055 info_buffer for SYM's objfile; right now we never release
25056 that buffer, but when we do clean up properly this may
25057 need to change. */
0d53c4c4
DJ
25058 baton->size = DW_BLOCK (attr)->size;
25059 baton->data = DW_BLOCK (attr)->data;
25060 }
25061 else
25062 {
25063 dwarf2_invalid_attrib_class_complaint ("location description",
25064 SYMBOL_NATURAL_NAME (sym));
25065 baton->size = 0;
0d53c4c4 25066 }
6e70227d 25067
f1e6e072
TT
25068 SYMBOL_ACLASS_INDEX (sym) = (is_block
25069 ? dwarf2_locexpr_block_index
25070 : dwarf2_locexpr_index);
0d53c4c4
DJ
25071 SYMBOL_LOCATION_BATON (sym) = baton;
25072 }
4c2df51b 25073}
6502dd73 25074
9aa1f1e3
TT
25075/* Return the OBJFILE associated with the compilation unit CU. If CU
25076 came from a separate debuginfo file, then the master objfile is
25077 returned. */
ae0d2f24
UW
25078
25079struct objfile *
25080dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
25081{
e3b94546 25082 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
ae0d2f24
UW
25083
25084 /* Return the master objfile, so that we can report and look up the
25085 correct file containing this variable. */
25086 if (objfile->separate_debug_objfile_backlink)
25087 objfile = objfile->separate_debug_objfile_backlink;
25088
25089 return objfile;
25090}
25091
96408a79
SA
25092/* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
25093 (CU_HEADERP is unused in such case) or prepare a temporary copy at
25094 CU_HEADERP first. */
25095
25096static const struct comp_unit_head *
25097per_cu_header_read_in (struct comp_unit_head *cu_headerp,
25098 struct dwarf2_per_cu_data *per_cu)
25099{
d521ce57 25100 const gdb_byte *info_ptr;
96408a79
SA
25101
25102 if (per_cu->cu)
25103 return &per_cu->cu->header;
25104
9c541725 25105 info_ptr = per_cu->section->buffer + to_underlying (per_cu->sect_off);
96408a79
SA
25106
25107 memset (cu_headerp, 0, sizeof (*cu_headerp));
43988095
JK
25108 read_comp_unit_head (cu_headerp, info_ptr, per_cu->section,
25109 rcuh_kind::COMPILE);
96408a79
SA
25110
25111 return cu_headerp;
25112}
25113
ae0d2f24
UW
25114/* Return the address size given in the compilation unit header for CU. */
25115
98714339 25116int
ae0d2f24
UW
25117dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
25118{
96408a79
SA
25119 struct comp_unit_head cu_header_local;
25120 const struct comp_unit_head *cu_headerp;
c471e790 25121
96408a79
SA
25122 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
25123
25124 return cu_headerp->addr_size;
ae0d2f24
UW
25125}
25126
9eae7c52
TT
25127/* Return the offset size given in the compilation unit header for CU. */
25128
25129int
25130dwarf2_per_cu_offset_size (struct dwarf2_per_cu_data *per_cu)
25131{
96408a79
SA
25132 struct comp_unit_head cu_header_local;
25133 const struct comp_unit_head *cu_headerp;
9c6c53f7 25134
96408a79
SA
25135 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
25136
25137 return cu_headerp->offset_size;
25138}
25139
25140/* See its dwarf2loc.h declaration. */
25141
25142int
25143dwarf2_per_cu_ref_addr_size (struct dwarf2_per_cu_data *per_cu)
25144{
25145 struct comp_unit_head cu_header_local;
25146 const struct comp_unit_head *cu_headerp;
25147
25148 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
25149
25150 if (cu_headerp->version == 2)
25151 return cu_headerp->addr_size;
25152 else
25153 return cu_headerp->offset_size;
181cebd4
JK
25154}
25155
9aa1f1e3
TT
25156/* Return the text offset of the CU. The returned offset comes from
25157 this CU's objfile. If this objfile came from a separate debuginfo
25158 file, then the offset may be different from the corresponding
25159 offset in the parent objfile. */
25160
25161CORE_ADDR
25162dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
25163{
e3b94546 25164 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
9aa1f1e3
TT
25165
25166 return ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
25167}
25168
43988095
JK
25169/* Return DWARF version number of PER_CU. */
25170
25171short
25172dwarf2_version (struct dwarf2_per_cu_data *per_cu)
25173{
25174 return per_cu->dwarf_version;
25175}
25176
348e048f
DE
25177/* Locate the .debug_info compilation unit from CU's objfile which contains
25178 the DIE at OFFSET. Raises an error on failure. */
ae038cb0
DJ
25179
25180static struct dwarf2_per_cu_data *
9c541725 25181dwarf2_find_containing_comp_unit (sect_offset sect_off,
36586728 25182 unsigned int offset_in_dwz,
ed2dc618 25183 struct dwarf2_per_objfile *dwarf2_per_objfile)
ae038cb0
DJ
25184{
25185 struct dwarf2_per_cu_data *this_cu;
25186 int low, high;
25187
ae038cb0 25188 low = 0;
b76e467d 25189 high = dwarf2_per_objfile->all_comp_units.size () - 1;
ae038cb0
DJ
25190 while (high > low)
25191 {
36586728 25192 struct dwarf2_per_cu_data *mid_cu;
ae038cb0 25193 int mid = low + (high - low) / 2;
9a619af0 25194
36586728 25195 mid_cu = dwarf2_per_objfile->all_comp_units[mid];
36586728 25196 if (mid_cu->is_dwz > offset_in_dwz
81fbbaf9 25197 || (mid_cu->is_dwz == offset_in_dwz
45b8ae0c 25198 && mid_cu->sect_off + mid_cu->length >= sect_off))
ae038cb0
DJ
25199 high = mid;
25200 else
25201 low = mid + 1;
25202 }
25203 gdb_assert (low == high);
36586728 25204 this_cu = dwarf2_per_objfile->all_comp_units[low];
45b8ae0c 25205 if (this_cu->is_dwz != offset_in_dwz || this_cu->sect_off > sect_off)
ae038cb0 25206 {
36586728 25207 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
8a3fe4f8 25208 error (_("Dwarf Error: could not find partial DIE containing "
9d8780f0
SM
25209 "offset %s [in module %s]"),
25210 sect_offset_str (sect_off),
ed2dc618 25211 bfd_get_filename (dwarf2_per_objfile->objfile->obfd));
10b3939b 25212
9c541725
PA
25213 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->sect_off
25214 <= sect_off);
ae038cb0
DJ
25215 return dwarf2_per_objfile->all_comp_units[low-1];
25216 }
25217 else
25218 {
b76e467d 25219 if (low == dwarf2_per_objfile->all_comp_units.size () - 1
9c541725 25220 && sect_off >= this_cu->sect_off + this_cu->length)
9d8780f0 25221 error (_("invalid dwarf2 offset %s"), sect_offset_str (sect_off));
9c541725 25222 gdb_assert (sect_off < this_cu->sect_off + this_cu->length);
ae038cb0
DJ
25223 return this_cu;
25224 }
25225}
25226
23745b47 25227/* Initialize dwarf2_cu CU, owned by PER_CU. */
93311388 25228
fcd3b13d
SM
25229dwarf2_cu::dwarf2_cu (struct dwarf2_per_cu_data *per_cu_)
25230 : per_cu (per_cu_),
9068261f
AB
25231 mark (false),
25232 has_loclist (false),
25233 checked_producer (false),
25234 producer_is_gxx_lt_4_6 (false),
25235 producer_is_gcc_lt_4_3 (false),
eb77c9df 25236 producer_is_icc (false),
9068261f 25237 producer_is_icc_lt_14 (false),
c258c396 25238 producer_is_codewarrior (false),
9068261f 25239 processing_has_namespace_info (false)
93311388 25240{
fcd3b13d
SM
25241 per_cu->cu = this;
25242}
25243
25244/* Destroy a dwarf2_cu. */
25245
25246dwarf2_cu::~dwarf2_cu ()
25247{
25248 per_cu->cu = NULL;
9816fde3
JK
25249}
25250
25251/* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
25252
25253static void
95554aad
TT
25254prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
25255 enum language pretend_language)
9816fde3
JK
25256{
25257 struct attribute *attr;
25258
25259 /* Set the language we're debugging. */
25260 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
25261 if (attr)
25262 set_cu_language (DW_UNSND (attr), cu);
25263 else
9cded63f 25264 {
95554aad 25265 cu->language = pretend_language;
9cded63f
TT
25266 cu->language_defn = language_def (cu->language);
25267 }
dee91e82 25268
7d45c7c3 25269 cu->producer = dwarf2_string_attr (comp_unit_die, DW_AT_producer, cu);
93311388
DE
25270}
25271
ae038cb0
DJ
25272/* Increase the age counter on each cached compilation unit, and free
25273 any that are too old. */
25274
25275static void
ed2dc618 25276age_cached_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
ae038cb0
DJ
25277{
25278 struct dwarf2_per_cu_data *per_cu, **last_chain;
25279
25280 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
25281 per_cu = dwarf2_per_objfile->read_in_chain;
25282 while (per_cu != NULL)
25283 {
25284 per_cu->cu->last_used ++;
b4f54984 25285 if (per_cu->cu->last_used <= dwarf_max_cache_age)
ae038cb0
DJ
25286 dwarf2_mark (per_cu->cu);
25287 per_cu = per_cu->cu->read_in_chain;
25288 }
25289
25290 per_cu = dwarf2_per_objfile->read_in_chain;
25291 last_chain = &dwarf2_per_objfile->read_in_chain;
25292 while (per_cu != NULL)
25293 {
25294 struct dwarf2_per_cu_data *next_cu;
25295
25296 next_cu = per_cu->cu->read_in_chain;
25297
25298 if (!per_cu->cu->mark)
25299 {
fcd3b13d 25300 delete per_cu->cu;
ae038cb0
DJ
25301 *last_chain = next_cu;
25302 }
25303 else
25304 last_chain = &per_cu->cu->read_in_chain;
25305
25306 per_cu = next_cu;
25307 }
25308}
25309
25310/* Remove a single compilation unit from the cache. */
25311
25312static void
dee91e82 25313free_one_cached_comp_unit (struct dwarf2_per_cu_data *target_per_cu)
ae038cb0
DJ
25314{
25315 struct dwarf2_per_cu_data *per_cu, **last_chain;
ed2dc618
SM
25316 struct dwarf2_per_objfile *dwarf2_per_objfile
25317 = target_per_cu->dwarf2_per_objfile;
ae038cb0
DJ
25318
25319 per_cu = dwarf2_per_objfile->read_in_chain;
25320 last_chain = &dwarf2_per_objfile->read_in_chain;
25321 while (per_cu != NULL)
25322 {
25323 struct dwarf2_per_cu_data *next_cu;
25324
25325 next_cu = per_cu->cu->read_in_chain;
25326
dee91e82 25327 if (per_cu == target_per_cu)
ae038cb0 25328 {
fcd3b13d 25329 delete per_cu->cu;
dee91e82 25330 per_cu->cu = NULL;
ae038cb0
DJ
25331 *last_chain = next_cu;
25332 break;
25333 }
25334 else
25335 last_chain = &per_cu->cu->read_in_chain;
25336
25337 per_cu = next_cu;
25338 }
25339}
25340
d95d3aef 25341/* Cleanup function for the dwarf2_per_objfile data. */
fe3e1990 25342
d95d3aef
TT
25343static void
25344dwarf2_free_objfile (struct objfile *objfile, void *datum)
fe3e1990 25345{
ed2dc618 25346 struct dwarf2_per_objfile *dwarf2_per_objfile
d95d3aef 25347 = static_cast<struct dwarf2_per_objfile *> (datum);
fe3e1990 25348
fd90ace4 25349 delete dwarf2_per_objfile;
fe3e1990
DJ
25350}
25351
dee91e82
DE
25352/* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
25353 We store these in a hash table separate from the DIEs, and preserve them
25354 when the DIEs are flushed out of cache.
25355
25356 The CU "per_cu" pointer is needed because offset alone is not enough to
3019eac3 25357 uniquely identify the type. A file may have multiple .debug_types sections,
c88ee1f0
DE
25358 or the type may come from a DWO file. Furthermore, while it's more logical
25359 to use per_cu->section+offset, with Fission the section with the data is in
25360 the DWO file but we don't know that section at the point we need it.
25361 We have to use something in dwarf2_per_cu_data (or the pointer to it)
25362 because we can enter the lookup routine, get_die_type_at_offset, from
25363 outside this file, and thus won't necessarily have PER_CU->cu.
25364 Fortunately, PER_CU is stable for the life of the objfile. */
1c379e20 25365
dee91e82 25366struct dwarf2_per_cu_offset_and_type
1c379e20 25367{
dee91e82 25368 const struct dwarf2_per_cu_data *per_cu;
9c541725 25369 sect_offset sect_off;
1c379e20
DJ
25370 struct type *type;
25371};
25372
dee91e82 25373/* Hash function for a dwarf2_per_cu_offset_and_type. */
1c379e20
DJ
25374
25375static hashval_t
dee91e82 25376per_cu_offset_and_type_hash (const void *item)
1c379e20 25377{
9a3c8263
SM
25378 const struct dwarf2_per_cu_offset_and_type *ofs
25379 = (const struct dwarf2_per_cu_offset_and_type *) item;
9a619af0 25380
9c541725 25381 return (uintptr_t) ofs->per_cu + to_underlying (ofs->sect_off);
1c379e20
DJ
25382}
25383
dee91e82 25384/* Equality function for a dwarf2_per_cu_offset_and_type. */
1c379e20
DJ
25385
25386static int
dee91e82 25387per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
1c379e20 25388{
9a3c8263
SM
25389 const struct dwarf2_per_cu_offset_and_type *ofs_lhs
25390 = (const struct dwarf2_per_cu_offset_and_type *) item_lhs;
25391 const struct dwarf2_per_cu_offset_and_type *ofs_rhs
25392 = (const struct dwarf2_per_cu_offset_and_type *) item_rhs;
9a619af0 25393
dee91e82 25394 return (ofs_lhs->per_cu == ofs_rhs->per_cu
9c541725 25395 && ofs_lhs->sect_off == ofs_rhs->sect_off);
1c379e20
DJ
25396}
25397
25398/* Set the type associated with DIE to TYPE. Save it in CU's hash
7e314c57
JK
25399 table if necessary. For convenience, return TYPE.
25400
25401 The DIEs reading must have careful ordering to:
25402 * Not cause infite loops trying to read in DIEs as a prerequisite for
25403 reading current DIE.
25404 * Not trying to dereference contents of still incompletely read in types
25405 while reading in other DIEs.
25406 * Enable referencing still incompletely read in types just by a pointer to
25407 the type without accessing its fields.
25408
25409 Therefore caller should follow these rules:
25410 * Try to fetch any prerequisite types we may need to build this DIE type
25411 before building the type and calling set_die_type.
e71ec853 25412 * After building type call set_die_type for current DIE as soon as
7e314c57
JK
25413 possible before fetching more types to complete the current type.
25414 * Make the type as complete as possible before fetching more types. */
1c379e20 25415
f792889a 25416static struct type *
1c379e20
DJ
25417set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
25418{
518817b3
SM
25419 struct dwarf2_per_objfile *dwarf2_per_objfile
25420 = cu->per_cu->dwarf2_per_objfile;
dee91e82 25421 struct dwarf2_per_cu_offset_and_type **slot, ofs;
ed2dc618 25422 struct objfile *objfile = dwarf2_per_objfile->objfile;
3cdcd0ce
JB
25423 struct attribute *attr;
25424 struct dynamic_prop prop;
1c379e20 25425
b4ba55a1
JB
25426 /* For Ada types, make sure that the gnat-specific data is always
25427 initialized (if not already set). There are a few types where
25428 we should not be doing so, because the type-specific area is
25429 already used to hold some other piece of info (eg: TYPE_CODE_FLT
25430 where the type-specific area is used to store the floatformat).
25431 But this is not a problem, because the gnat-specific information
25432 is actually not needed for these types. */
25433 if (need_gnat_info (cu)
25434 && TYPE_CODE (type) != TYPE_CODE_FUNC
25435 && TYPE_CODE (type) != TYPE_CODE_FLT
09e2d7c7
DE
25436 && TYPE_CODE (type) != TYPE_CODE_METHODPTR
25437 && TYPE_CODE (type) != TYPE_CODE_MEMBERPTR
25438 && TYPE_CODE (type) != TYPE_CODE_METHOD
b4ba55a1
JB
25439 && !HAVE_GNAT_AUX_INFO (type))
25440 INIT_GNAT_SPECIFIC (type);
25441
3f2f83dd
KB
25442 /* Read DW_AT_allocated and set in type. */
25443 attr = dwarf2_attr (die, DW_AT_allocated, cu);
25444 if (attr_form_is_block (attr))
25445 {
25446 if (attr_to_dynamic_prop (attr, die, cu, &prop))
50a82047 25447 add_dyn_prop (DYN_PROP_ALLOCATED, prop, type);
3f2f83dd
KB
25448 }
25449 else if (attr != NULL)
25450 {
b98664d3 25451 complaint (_("DW_AT_allocated has the wrong form (%s) at DIE %s"),
9c541725 25452 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
9d8780f0 25453 sect_offset_str (die->sect_off));
3f2f83dd
KB
25454 }
25455
25456 /* Read DW_AT_associated and set in type. */
25457 attr = dwarf2_attr (die, DW_AT_associated, cu);
25458 if (attr_form_is_block (attr))
25459 {
25460 if (attr_to_dynamic_prop (attr, die, cu, &prop))
50a82047 25461 add_dyn_prop (DYN_PROP_ASSOCIATED, prop, type);
3f2f83dd
KB
25462 }
25463 else if (attr != NULL)
25464 {
b98664d3 25465 complaint (_("DW_AT_associated has the wrong form (%s) at DIE %s"),
9c541725 25466 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
9d8780f0 25467 sect_offset_str (die->sect_off));
3f2f83dd
KB
25468 }
25469
3cdcd0ce
JB
25470 /* Read DW_AT_data_location and set in type. */
25471 attr = dwarf2_attr (die, DW_AT_data_location, cu);
25472 if (attr_to_dynamic_prop (attr, die, cu, &prop))
50a82047 25473 add_dyn_prop (DYN_PROP_DATA_LOCATION, prop, type);
3cdcd0ce 25474
dee91e82 25475 if (dwarf2_per_objfile->die_type_hash == NULL)
f792889a 25476 {
dee91e82
DE
25477 dwarf2_per_objfile->die_type_hash =
25478 htab_create_alloc_ex (127,
25479 per_cu_offset_and_type_hash,
25480 per_cu_offset_and_type_eq,
25481 NULL,
25482 &objfile->objfile_obstack,
25483 hashtab_obstack_allocate,
25484 dummy_obstack_deallocate);
f792889a 25485 }
1c379e20 25486
dee91e82 25487 ofs.per_cu = cu->per_cu;
9c541725 25488 ofs.sect_off = die->sect_off;
1c379e20 25489 ofs.type = type;
dee91e82
DE
25490 slot = (struct dwarf2_per_cu_offset_and_type **)
25491 htab_find_slot (dwarf2_per_objfile->die_type_hash, &ofs, INSERT);
7e314c57 25492 if (*slot)
b98664d3 25493 complaint (_("A problem internal to GDB: DIE %s has type already set"),
9d8780f0 25494 sect_offset_str (die->sect_off));
8d749320
SM
25495 *slot = XOBNEW (&objfile->objfile_obstack,
25496 struct dwarf2_per_cu_offset_and_type);
1c379e20 25497 **slot = ofs;
f792889a 25498 return type;
1c379e20
DJ
25499}
25500
9c541725 25501/* Look up the type for the die at SECT_OFF in PER_CU in die_type_hash,
02142a6c 25502 or return NULL if the die does not have a saved type. */
1c379e20
DJ
25503
25504static struct type *
9c541725 25505get_die_type_at_offset (sect_offset sect_off,
673bfd45 25506 struct dwarf2_per_cu_data *per_cu)
1c379e20 25507{
dee91e82 25508 struct dwarf2_per_cu_offset_and_type *slot, ofs;
ed2dc618 25509 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
f792889a 25510
dee91e82 25511 if (dwarf2_per_objfile->die_type_hash == NULL)
f792889a 25512 return NULL;
1c379e20 25513
dee91e82 25514 ofs.per_cu = per_cu;
9c541725 25515 ofs.sect_off = sect_off;
9a3c8263
SM
25516 slot = ((struct dwarf2_per_cu_offset_and_type *)
25517 htab_find (dwarf2_per_objfile->die_type_hash, &ofs));
1c379e20
DJ
25518 if (slot)
25519 return slot->type;
25520 else
25521 return NULL;
25522}
25523
02142a6c 25524/* Look up the type for DIE in CU in die_type_hash,
673bfd45
DE
25525 or return NULL if DIE does not have a saved type. */
25526
25527static struct type *
25528get_die_type (struct die_info *die, struct dwarf2_cu *cu)
25529{
9c541725 25530 return get_die_type_at_offset (die->sect_off, cu->per_cu);
673bfd45
DE
25531}
25532
10b3939b
DJ
25533/* Add a dependence relationship from CU to REF_PER_CU. */
25534
25535static void
25536dwarf2_add_dependence (struct dwarf2_cu *cu,
25537 struct dwarf2_per_cu_data *ref_per_cu)
25538{
25539 void **slot;
25540
25541 if (cu->dependencies == NULL)
25542 cu->dependencies
25543 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
25544 NULL, &cu->comp_unit_obstack,
25545 hashtab_obstack_allocate,
25546 dummy_obstack_deallocate);
25547
25548 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
25549 if (*slot == NULL)
25550 *slot = ref_per_cu;
25551}
1c379e20 25552
f504f079
DE
25553/* Subroutine of dwarf2_mark to pass to htab_traverse.
25554 Set the mark field in every compilation unit in the
ae038cb0
DJ
25555 cache that we must keep because we are keeping CU. */
25556
10b3939b
DJ
25557static int
25558dwarf2_mark_helper (void **slot, void *data)
25559{
25560 struct dwarf2_per_cu_data *per_cu;
25561
25562 per_cu = (struct dwarf2_per_cu_data *) *slot;
d07ed419
JK
25563
25564 /* cu->dependencies references may not yet have been ever read if QUIT aborts
25565 reading of the chain. As such dependencies remain valid it is not much
25566 useful to track and undo them during QUIT cleanups. */
25567 if (per_cu->cu == NULL)
25568 return 1;
25569
10b3939b
DJ
25570 if (per_cu->cu->mark)
25571 return 1;
9068261f 25572 per_cu->cu->mark = true;
10b3939b
DJ
25573
25574 if (per_cu->cu->dependencies != NULL)
25575 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
25576
25577 return 1;
25578}
25579
f504f079
DE
25580/* Set the mark field in CU and in every other compilation unit in the
25581 cache that we must keep because we are keeping CU. */
25582
ae038cb0
DJ
25583static void
25584dwarf2_mark (struct dwarf2_cu *cu)
25585{
25586 if (cu->mark)
25587 return;
9068261f 25588 cu->mark = true;
10b3939b
DJ
25589 if (cu->dependencies != NULL)
25590 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
ae038cb0
DJ
25591}
25592
25593static void
25594dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
25595{
25596 while (per_cu)
25597 {
9068261f 25598 per_cu->cu->mark = false;
ae038cb0
DJ
25599 per_cu = per_cu->cu->read_in_chain;
25600 }
72bf9492
DJ
25601}
25602
72bf9492
DJ
25603/* Trivial hash function for partial_die_info: the hash value of a DIE
25604 is its offset in .debug_info for this objfile. */
25605
25606static hashval_t
25607partial_die_hash (const void *item)
25608{
9a3c8263
SM
25609 const struct partial_die_info *part_die
25610 = (const struct partial_die_info *) item;
9a619af0 25611
9c541725 25612 return to_underlying (part_die->sect_off);
72bf9492
DJ
25613}
25614
25615/* Trivial comparison function for partial_die_info structures: two DIEs
25616 are equal if they have the same offset. */
25617
25618static int
25619partial_die_eq (const void *item_lhs, const void *item_rhs)
25620{
9a3c8263
SM
25621 const struct partial_die_info *part_die_lhs
25622 = (const struct partial_die_info *) item_lhs;
25623 const struct partial_die_info *part_die_rhs
25624 = (const struct partial_die_info *) item_rhs;
9a619af0 25625
9c541725 25626 return part_die_lhs->sect_off == part_die_rhs->sect_off;
72bf9492
DJ
25627}
25628
3c3bb058
AB
25629struct cmd_list_element *set_dwarf_cmdlist;
25630struct cmd_list_element *show_dwarf_cmdlist;
ae038cb0
DJ
25631
25632static void
981a3fb3 25633set_dwarf_cmd (const char *args, int from_tty)
ae038cb0 25634{
b4f54984 25635 help_list (set_dwarf_cmdlist, "maintenance set dwarf ", all_commands,
635c7e8a 25636 gdb_stdout);
ae038cb0
DJ
25637}
25638
25639static void
981a3fb3 25640show_dwarf_cmd (const char *args, int from_tty)
6e70227d 25641{
b4f54984 25642 cmd_show_list (show_dwarf_cmdlist, from_tty, "");
ae038cb0
DJ
25643}
25644
cd4fb1b2 25645int dwarf_always_disassemble;
437afbb8 25646
437afbb8 25647static void
cd4fb1b2
SM
25648show_dwarf_always_disassemble (struct ui_file *file, int from_tty,
25649 struct cmd_list_element *c, const char *value)
9291a0cd 25650{
cd4fb1b2
SM
25651 fprintf_filtered (file,
25652 _("Whether to always disassemble "
25653 "DWARF expressions is %s.\n"),
25654 value);
9291a0cd
TT
25655}
25656
9291a0cd 25657static void
cd4fb1b2
SM
25658show_check_physname (struct ui_file *file, int from_tty,
25659 struct cmd_list_element *c, const char *value)
9291a0cd 25660{
cd4fb1b2
SM
25661 fprintf_filtered (file,
25662 _("Whether to check \"physname\" is %s.\n"),
25663 value);
9291a0cd
TT
25664}
25665
cd4fb1b2
SM
25666void
25667_initialize_dwarf2_read (void)
9291a0cd 25668{
d95d3aef
TT
25669 dwarf2_objfile_data_key
25670 = register_objfile_data_with_cleanup (nullptr, dwarf2_free_objfile);
156942c7 25671
cd4fb1b2
SM
25672 add_prefix_cmd ("dwarf", class_maintenance, set_dwarf_cmd, _("\
25673Set DWARF specific variables.\n\
25674Configure DWARF variables such as the cache size"),
25675 &set_dwarf_cmdlist, "maintenance set dwarf ",
25676 0/*allow-unknown*/, &maintenance_set_cmdlist);
156942c7 25677
cd4fb1b2
SM
25678 add_prefix_cmd ("dwarf", class_maintenance, show_dwarf_cmd, _("\
25679Show DWARF specific variables\n\
25680Show DWARF variables such as the cache size"),
25681 &show_dwarf_cmdlist, "maintenance show dwarf ",
25682 0/*allow-unknown*/, &maintenance_show_cmdlist);
156942c7 25683
cd4fb1b2
SM
25684 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
25685 &dwarf_max_cache_age, _("\
25686Set the upper bound on the age of cached DWARF compilation units."), _("\
25687Show the upper bound on the age of cached DWARF compilation units."), _("\
25688A higher limit means that cached compilation units will be stored\n\
25689in memory longer, and more total memory will be used. Zero disables\n\
25690caching, which can slow down startup."),
25691 NULL,
25692 show_dwarf_max_cache_age,
25693 &set_dwarf_cmdlist,
25694 &show_dwarf_cmdlist);
156942c7 25695
cd4fb1b2
SM
25696 add_setshow_boolean_cmd ("always-disassemble", class_obscure,
25697 &dwarf_always_disassemble, _("\
25698Set whether `info address' always disassembles DWARF expressions."), _("\
25699Show whether `info address' always disassembles DWARF expressions."), _("\
25700When enabled, DWARF expressions are always printed in an assembly-like\n\
25701syntax. When disabled, expressions will be printed in a more\n\
25702conversational style, when possible."),
25703 NULL,
25704 show_dwarf_always_disassemble,
25705 &set_dwarf_cmdlist,
25706 &show_dwarf_cmdlist);
9291a0cd 25707
cd4fb1b2
SM
25708 add_setshow_zuinteger_cmd ("dwarf-read", no_class, &dwarf_read_debug, _("\
25709Set debugging of the DWARF reader."), _("\
25710Show debugging of the DWARF reader."), _("\
25711When enabled (non-zero), debugging messages are printed during DWARF\n\
25712reading and symtab expansion. A value of 1 (one) provides basic\n\
25713information. A value greater than 1 provides more verbose information."),
25714 NULL,
25715 NULL,
25716 &setdebuglist, &showdebuglist);
9291a0cd 25717
cd4fb1b2
SM
25718 add_setshow_zuinteger_cmd ("dwarf-die", no_class, &dwarf_die_debug, _("\
25719Set debugging of the DWARF DIE reader."), _("\
25720Show debugging of the DWARF DIE reader."), _("\
25721When enabled (non-zero), DIEs are dumped after they are read in.\n\
25722The value is the maximum depth to print."),
25723 NULL,
25724 NULL,
25725 &setdebuglist, &showdebuglist);
9291a0cd 25726
cd4fb1b2
SM
25727 add_setshow_zuinteger_cmd ("dwarf-line", no_class, &dwarf_line_debug, _("\
25728Set debugging of the dwarf line reader."), _("\
25729Show debugging of the dwarf line reader."), _("\
25730When enabled (non-zero), line number entries are dumped as they are read in.\n\
25731A value of 1 (one) provides basic information.\n\
25732A value greater than 1 provides more verbose information."),
25733 NULL,
25734 NULL,
25735 &setdebuglist, &showdebuglist);
437afbb8 25736
cd4fb1b2
SM
25737 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
25738Set cross-checking of \"physname\" code against demangler."), _("\
25739Show cross-checking of \"physname\" code against demangler."), _("\
25740When enabled, GDB's internal \"physname\" code is checked against\n\
25741the demangler."),
25742 NULL, show_check_physname,
25743 &setdebuglist, &showdebuglist);
900e11f9 25744
e615022a
DE
25745 add_setshow_boolean_cmd ("use-deprecated-index-sections",
25746 no_class, &use_deprecated_index_sections, _("\
25747Set whether to use deprecated gdb_index sections."), _("\
25748Show whether to use deprecated gdb_index sections."), _("\
25749When enabled, deprecated .gdb_index sections are used anyway.\n\
25750Normally they are ignored either because of a missing feature or\n\
25751performance issue.\n\
25752Warning: This option must be enabled before gdb reads the file."),
25753 NULL,
25754 NULL,
25755 &setlist, &showlist);
25756
f1e6e072
TT
25757 dwarf2_locexpr_index = register_symbol_computed_impl (LOC_COMPUTED,
25758 &dwarf2_locexpr_funcs);
25759 dwarf2_loclist_index = register_symbol_computed_impl (LOC_COMPUTED,
25760 &dwarf2_loclist_funcs);
25761
25762 dwarf2_locexpr_block_index = register_symbol_block_impl (LOC_BLOCK,
25763 &dwarf2_block_frame_base_locexpr_funcs);
25764 dwarf2_loclist_block_index = register_symbol_block_impl (LOC_BLOCK,
25765 &dwarf2_block_frame_base_loclist_funcs);
c62446b1
PA
25766
25767#if GDB_SELF_TEST
25768 selftests::register_test ("dw2_expand_symtabs_matching",
25769 selftests::dw2_expand_symtabs_matching::run_test);
25770#endif
6502dd73 25771}
This page took 4.787141 seconds and 4 git commands to generate.