Implement write_async_safe for mi_console_file (PR 22299)
[deliverable/binutils-gdb.git] / gdb / dwarf2read.c
CommitLineData
c906108c 1/* DWARF 2 debugging format support for GDB.
917c78fc 2
e2882c85 3 Copyright (C) 1994-2018 Free Software Foundation, Inc.
c906108c
SS
4
5 Adapted by Gary Funck (gary@intrepid.com), Intrepid Technology,
6 Inc. with support from Florida State University (under contract
7 with the Ada Joint Program Office), and Silicon Graphics, Inc.
8 Initial contribution by Brent Benson, Harris Computer Systems, Inc.,
9 based on Fred Fish's (Cygnus Support) implementation of DWARF 1
7ce59000 10 support.
c906108c 11
c5aa993b 12 This file is part of GDB.
c906108c 13
c5aa993b
JM
14 This program is free software; you can redistribute it and/or modify
15 it under the terms of the GNU General Public License as published by
a9762ec7
JB
16 the Free Software Foundation; either version 3 of the License, or
17 (at your option) any later version.
c906108c 18
a9762ec7
JB
19 This program is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 GNU General Public License for more details.
c906108c 23
c5aa993b 24 You should have received a copy of the GNU General Public License
a9762ec7 25 along with this program. If not, see <http://www.gnu.org/licenses/>. */
c906108c 26
21b2bd31
DE
27/* FIXME: Various die-reading functions need to be more careful with
28 reading off the end of the section.
29 E.g., load_partial_dies, read_partial_die. */
30
c906108c 31#include "defs.h"
cd4fb1b2
SM
32#include "dwarf2read.h"
33#include "dwarf-index-common.h"
c906108c 34#include "bfd.h"
80626a55 35#include "elf-bfd.h"
c906108c
SS
36#include "symtab.h"
37#include "gdbtypes.h"
c906108c 38#include "objfiles.h"
fa8f86ff 39#include "dwarf2.h"
c906108c
SS
40#include "buildsym.h"
41#include "demangle.h"
50f182aa 42#include "gdb-demangle.h"
c906108c 43#include "expression.h"
d5166ae1 44#include "filenames.h" /* for DOSish file names */
2e276125 45#include "macrotab.h"
c906108c
SS
46#include "language.h"
47#include "complaints.h"
357e46e7 48#include "bcache.h"
4c2df51b
DJ
49#include "dwarf2expr.h"
50#include "dwarf2loc.h"
9219021c 51#include "cp-support.h"
72bf9492 52#include "hashtab.h"
ae038cb0
DJ
53#include "command.h"
54#include "gdbcmd.h"
edb3359d 55#include "block.h"
ff013f42 56#include "addrmap.h"
94af9270 57#include "typeprint.h"
ccefe4c4 58#include "psympriv.h"
53ce3c39 59#include <sys/stat.h>
96d19272 60#include "completer.h"
34eaf542 61#include "vec.h"
98bfdba5 62#include "c-lang.h"
a766d390 63#include "go-lang.h"
98bfdba5 64#include "valprint.h"
3019eac3 65#include "gdbcore.h" /* for gnutarget */
156942c7 66#include "gdb/gdb-index.h"
60d5a603 67#include <ctype.h>
cbb099e8 68#include "gdb_bfd.h"
4357ac6c 69#include "f-lang.h"
05cba821 70#include "source.h"
614c279d 71#include "filestuff.h"
dc294be5 72#include "build-id.h"
22cee43f 73#include "namespace.h"
bef155c3 74#include "common/gdb_unlinker.h"
14bc53a8 75#include "common/function-view.h"
ecfb656c
PA
76#include "common/gdb_optional.h"
77#include "common/underlying.h"
d5722aa2 78#include "common/byte-vector.h"
927aa2e7 79#include "common/hash_enum.h"
bbf2f4df 80#include "filename-seen-cache.h"
b32b108a 81#include "producer.h"
c906108c 82#include <fcntl.h>
c906108c 83#include <sys/types.h>
325fac50 84#include <algorithm>
bc8f2430
JK
85#include <unordered_set>
86#include <unordered_map>
c62446b1 87#include "selftest.h"
437afbb8
JK
88#include <cmath>
89#include <set>
90#include <forward_list>
c9317f21 91#include "rust-lang.h"
b4987c95 92#include "common/pathstuff.h"
437afbb8 93
73be47f5
DE
94/* When == 1, print basic high level tracing messages.
95 When > 1, be more verbose.
b4f54984
DE
96 This is in contrast to the low level DIE reading of dwarf_die_debug. */
97static unsigned int dwarf_read_debug = 0;
45cfd468 98
d97bc12b 99/* When non-zero, dump DIEs after they are read in. */
b4f54984 100static unsigned int dwarf_die_debug = 0;
d97bc12b 101
27e0867f
DE
102/* When non-zero, dump line number entries as they are read in. */
103static unsigned int dwarf_line_debug = 0;
104
900e11f9
JK
105/* When non-zero, cross-check physname against demangler. */
106static int check_physname = 0;
107
481860b3 108/* When non-zero, do not reject deprecated .gdb_index sections. */
e615022a 109static int use_deprecated_index_sections = 0;
481860b3 110
6502dd73
DJ
111static const struct objfile_data *dwarf2_objfile_data_key;
112
f1e6e072
TT
113/* The "aclass" indices for various kinds of computed DWARF symbols. */
114
115static int dwarf2_locexpr_index;
116static int dwarf2_loclist_index;
117static int dwarf2_locexpr_block_index;
118static int dwarf2_loclist_block_index;
119
3f563c84
PA
120/* An index into a (C++) symbol name component in a symbol name as
121 recorded in the mapped_index's symbol table. For each C++ symbol
122 in the symbol table, we record one entry for the start of each
123 component in the symbol in a table of name components, and then
124 sort the table, in order to be able to binary search symbol names,
125 ignoring leading namespaces, both completion and regular look up.
126 For example, for symbol "A::B::C", we'll have an entry that points
127 to "A::B::C", another that points to "B::C", and another for "C".
128 Note that function symbols in GDB index have no parameter
129 information, just the function/method names. You can convert a
130 name_component to a "const char *" using the
131 'mapped_index::symbol_name_at(offset_type)' method. */
132
133struct name_component
134{
135 /* Offset in the symbol name where the component starts. Stored as
136 a (32-bit) offset instead of a pointer to save memory and improve
137 locality on 64-bit architectures. */
138 offset_type name_offset;
139
140 /* The symbol's index in the symbol and constant pool tables of a
141 mapped_index. */
142 offset_type idx;
143};
144
44ed8f3e
PA
145/* Base class containing bits shared by both .gdb_index and
146 .debug_name indexes. */
147
148struct mapped_index_base
149{
150 /* The name_component table (a sorted vector). See name_component's
151 description above. */
152 std::vector<name_component> name_components;
153
154 /* How NAME_COMPONENTS is sorted. */
155 enum case_sensitivity name_components_casing;
156
157 /* Return the number of names in the symbol table. */
158 virtual size_t symbol_name_count () const = 0;
159
160 /* Get the name of the symbol at IDX in the symbol table. */
161 virtual const char *symbol_name_at (offset_type idx) const = 0;
162
163 /* Return whether the name at IDX in the symbol table should be
164 ignored. */
165 virtual bool symbol_name_slot_invalid (offset_type idx) const
166 {
167 return false;
168 }
169
170 /* Build the symbol name component sorted vector, if we haven't
171 yet. */
172 void build_name_components ();
173
174 /* Returns the lower (inclusive) and upper (exclusive) bounds of the
175 possible matches for LN_NO_PARAMS in the name component
176 vector. */
177 std::pair<std::vector<name_component>::const_iterator,
178 std::vector<name_component>::const_iterator>
179 find_name_components_bounds (const lookup_name_info &ln_no_params) const;
180
181 /* Prevent deleting/destroying via a base class pointer. */
182protected:
183 ~mapped_index_base() = default;
184};
185
9291a0cd
TT
186/* A description of the mapped index. The file format is described in
187 a comment by the code that writes the index. */
fc898b42 188struct mapped_index final : public mapped_index_base
9291a0cd 189{
f00a2de2
PA
190 /* A slot/bucket in the symbol table hash. */
191 struct symbol_table_slot
192 {
193 const offset_type name;
194 const offset_type vec;
195 };
196
559a7a62
JK
197 /* Index data format version. */
198 int version;
199
9291a0cd
TT
200 /* The total length of the buffer. */
201 off_t total_size;
b11b1f88 202
f00a2de2
PA
203 /* The address table data. */
204 gdb::array_view<const gdb_byte> address_table;
b11b1f88 205
3876f04e 206 /* The symbol table, implemented as a hash table. */
f00a2de2 207 gdb::array_view<symbol_table_slot> symbol_table;
b11b1f88 208
9291a0cd
TT
209 /* A pointer to the constant pool. */
210 const char *constant_pool;
3f563c84 211
44ed8f3e
PA
212 bool symbol_name_slot_invalid (offset_type idx) const override
213 {
214 const auto &bucket = this->symbol_table[idx];
215 return bucket.name == 0 && bucket.vec;
216 }
5c58de74 217
3f563c84
PA
218 /* Convenience method to get at the name of the symbol at IDX in the
219 symbol table. */
44ed8f3e 220 const char *symbol_name_at (offset_type idx) const override
f00a2de2 221 { return this->constant_pool + MAYBE_SWAP (this->symbol_table[idx].name); }
5c58de74 222
44ed8f3e
PA
223 size_t symbol_name_count () const override
224 { return this->symbol_table.size (); }
9291a0cd
TT
225};
226
927aa2e7
JK
227/* A description of the mapped .debug_names.
228 Uninitialized map has CU_COUNT 0. */
fc898b42 229struct mapped_debug_names final : public mapped_index_base
927aa2e7 230{
ed2dc618
SM
231 mapped_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile_)
232 : dwarf2_per_objfile (dwarf2_per_objfile_)
233 {}
234
235 struct dwarf2_per_objfile *dwarf2_per_objfile;
927aa2e7
JK
236 bfd_endian dwarf5_byte_order;
237 bool dwarf5_is_dwarf64;
238 bool augmentation_is_gdb;
239 uint8_t offset_size;
240 uint32_t cu_count = 0;
241 uint32_t tu_count, bucket_count, name_count;
242 const gdb_byte *cu_table_reordered, *tu_table_reordered;
243 const uint32_t *bucket_table_reordered, *hash_table_reordered;
244 const gdb_byte *name_table_string_offs_reordered;
245 const gdb_byte *name_table_entry_offs_reordered;
246 const gdb_byte *entry_pool;
247
248 struct index_val
249 {
250 ULONGEST dwarf_tag;
251 struct attr
252 {
253 /* Attribute name DW_IDX_*. */
254 ULONGEST dw_idx;
255
256 /* Attribute form DW_FORM_*. */
257 ULONGEST form;
258
259 /* Value if FORM is DW_FORM_implicit_const. */
260 LONGEST implicit_const;
261 };
262 std::vector<attr> attr_vec;
263 };
264
265 std::unordered_map<ULONGEST, index_val> abbrev_map;
266
267 const char *namei_to_name (uint32_t namei) const;
44ed8f3e
PA
268
269 /* Implementation of the mapped_index_base virtual interface, for
270 the name_components cache. */
271
272 const char *symbol_name_at (offset_type idx) const override
273 { return namei_to_name (idx); }
274
275 size_t symbol_name_count () const override
276 { return this->name_count; }
927aa2e7
JK
277};
278
cd4fb1b2 279/* See dwarf2read.h. */
ed2dc618 280
cd4fb1b2 281dwarf2_per_objfile *
ed2dc618
SM
282get_dwarf2_per_objfile (struct objfile *objfile)
283{
284 return ((struct dwarf2_per_objfile *)
285 objfile_data (objfile, dwarf2_objfile_data_key));
286}
287
288/* Set the dwarf2_per_objfile associated to OBJFILE. */
289
290void
291set_dwarf2_per_objfile (struct objfile *objfile,
292 struct dwarf2_per_objfile *dwarf2_per_objfile)
293{
294 gdb_assert (get_dwarf2_per_objfile (objfile) == NULL);
295 set_objfile_data (objfile, dwarf2_objfile_data_key, dwarf2_per_objfile);
296}
c906108c 297
251d32d9 298/* Default names of the debugging sections. */
c906108c 299
233a11ab
CS
300/* Note that if the debugging section has been compressed, it might
301 have a name like .zdebug_info. */
302
9cdd5dbd
DE
303static const struct dwarf2_debug_sections dwarf2_elf_names =
304{
251d32d9
TG
305 { ".debug_info", ".zdebug_info" },
306 { ".debug_abbrev", ".zdebug_abbrev" },
307 { ".debug_line", ".zdebug_line" },
308 { ".debug_loc", ".zdebug_loc" },
43988095 309 { ".debug_loclists", ".zdebug_loclists" },
251d32d9 310 { ".debug_macinfo", ".zdebug_macinfo" },
cf2c3c16 311 { ".debug_macro", ".zdebug_macro" },
251d32d9 312 { ".debug_str", ".zdebug_str" },
43988095 313 { ".debug_line_str", ".zdebug_line_str" },
251d32d9 314 { ".debug_ranges", ".zdebug_ranges" },
43988095 315 { ".debug_rnglists", ".zdebug_rnglists" },
251d32d9 316 { ".debug_types", ".zdebug_types" },
3019eac3 317 { ".debug_addr", ".zdebug_addr" },
251d32d9
TG
318 { ".debug_frame", ".zdebug_frame" },
319 { ".eh_frame", NULL },
24d3216f 320 { ".gdb_index", ".zgdb_index" },
927aa2e7
JK
321 { ".debug_names", ".zdebug_names" },
322 { ".debug_aranges", ".zdebug_aranges" },
24d3216f 323 23
251d32d9 324};
c906108c 325
80626a55 326/* List of DWO/DWP sections. */
3019eac3 327
80626a55 328static const struct dwop_section_names
3019eac3
DE
329{
330 struct dwarf2_section_names abbrev_dwo;
331 struct dwarf2_section_names info_dwo;
332 struct dwarf2_section_names line_dwo;
333 struct dwarf2_section_names loc_dwo;
43988095 334 struct dwarf2_section_names loclists_dwo;
09262596
DE
335 struct dwarf2_section_names macinfo_dwo;
336 struct dwarf2_section_names macro_dwo;
3019eac3
DE
337 struct dwarf2_section_names str_dwo;
338 struct dwarf2_section_names str_offsets_dwo;
339 struct dwarf2_section_names types_dwo;
80626a55
DE
340 struct dwarf2_section_names cu_index;
341 struct dwarf2_section_names tu_index;
3019eac3 342}
80626a55 343dwop_section_names =
3019eac3
DE
344{
345 { ".debug_abbrev.dwo", ".zdebug_abbrev.dwo" },
346 { ".debug_info.dwo", ".zdebug_info.dwo" },
347 { ".debug_line.dwo", ".zdebug_line.dwo" },
348 { ".debug_loc.dwo", ".zdebug_loc.dwo" },
43988095 349 { ".debug_loclists.dwo", ".zdebug_loclists.dwo" },
09262596
DE
350 { ".debug_macinfo.dwo", ".zdebug_macinfo.dwo" },
351 { ".debug_macro.dwo", ".zdebug_macro.dwo" },
3019eac3
DE
352 { ".debug_str.dwo", ".zdebug_str.dwo" },
353 { ".debug_str_offsets.dwo", ".zdebug_str_offsets.dwo" },
354 { ".debug_types.dwo", ".zdebug_types.dwo" },
80626a55
DE
355 { ".debug_cu_index", ".zdebug_cu_index" },
356 { ".debug_tu_index", ".zdebug_tu_index" },
3019eac3
DE
357};
358
c906108c
SS
359/* local data types */
360
107d2387
AC
361/* The data in a compilation unit header, after target2host
362 translation, looks like this. */
c906108c 363struct comp_unit_head
a738430d 364{
c764a876 365 unsigned int length;
a738430d 366 short version;
a738430d
MK
367 unsigned char addr_size;
368 unsigned char signed_addr_p;
9c541725 369 sect_offset abbrev_sect_off;
57349743 370
a738430d
MK
371 /* Size of file offsets; either 4 or 8. */
372 unsigned int offset_size;
57349743 373
a738430d
MK
374 /* Size of the length field; either 4 or 12. */
375 unsigned int initial_length_size;
57349743 376
43988095
JK
377 enum dwarf_unit_type unit_type;
378
a738430d
MK
379 /* Offset to the first byte of this compilation unit header in the
380 .debug_info section, for resolving relative reference dies. */
9c541725 381 sect_offset sect_off;
57349743 382
d00adf39
DE
383 /* Offset to first die in this cu from the start of the cu.
384 This will be the first byte following the compilation unit header. */
9c541725 385 cu_offset first_die_cu_offset;
43988095
JK
386
387 /* 64-bit signature of this type unit - it is valid only for
388 UNIT_TYPE DW_UT_type. */
389 ULONGEST signature;
390
391 /* For types, offset in the type's DIE of the type defined by this TU. */
9c541725 392 cu_offset type_cu_offset_in_tu;
a738430d 393};
c906108c 394
3da10d80
KS
395/* Type used for delaying computation of method physnames.
396 See comments for compute_delayed_physnames. */
397struct delayed_method_info
398{
399 /* The type to which the method is attached, i.e., its parent class. */
400 struct type *type;
401
402 /* The index of the method in the type's function fieldlists. */
403 int fnfield_index;
404
405 /* The index of the method in the fieldlist. */
406 int index;
407
408 /* The name of the DIE. */
409 const char *name;
410
411 /* The DIE associated with this method. */
412 struct die_info *die;
413};
414
e7c27a73
DJ
415/* Internal state when decoding a particular compilation unit. */
416struct dwarf2_cu
417{
fcd3b13d
SM
418 explicit dwarf2_cu (struct dwarf2_per_cu_data *per_cu);
419 ~dwarf2_cu ();
420
421 DISABLE_COPY_AND_ASSIGN (dwarf2_cu);
422
d00adf39 423 /* The header of the compilation unit. */
fcd3b13d 424 struct comp_unit_head header {};
e142c38c 425
d00adf39 426 /* Base address of this compilation unit. */
fcd3b13d 427 CORE_ADDR base_address = 0;
d00adf39
DE
428
429 /* Non-zero if base_address has been set. */
fcd3b13d 430 int base_known = 0;
d00adf39 431
e142c38c 432 /* The language we are debugging. */
fcd3b13d
SM
433 enum language language = language_unknown;
434 const struct language_defn *language_defn = nullptr;
e142c38c 435
fcd3b13d 436 const char *producer = nullptr;
b0f35d58 437
e142c38c
DJ
438 /* The generic symbol table building routines have separate lists for
439 file scope symbols and all all other scopes (local scopes). So
440 we need to select the right one to pass to add_symbol_to_list().
441 We do it by keeping a pointer to the correct list in list_in_scope.
442
443 FIXME: The original dwarf code just treated the file scope as the
444 first local scope, and all other local scopes as nested local
445 scopes, and worked fine. Check to see if we really need to
446 distinguish these in buildsym.c. */
fcd3b13d 447 struct pending **list_in_scope = nullptr;
e142c38c 448
b64f50a1
JK
449 /* Hash table holding all the loaded partial DIEs
450 with partial_die->offset.SECT_OFF as hash. */
fcd3b13d 451 htab_t partial_dies = nullptr;
72bf9492
DJ
452
453 /* Storage for things with the same lifetime as this read-in compilation
454 unit, including partial DIEs. */
fcd3b13d 455 auto_obstack comp_unit_obstack;
72bf9492 456
ae038cb0
DJ
457 /* When multiple dwarf2_cu structures are living in memory, this field
458 chains them all together, so that they can be released efficiently.
459 We will probably also want a generation counter so that most-recently-used
460 compilation units are cached... */
fcd3b13d 461 struct dwarf2_per_cu_data *read_in_chain = nullptr;
ae038cb0 462
69d751e3 463 /* Backlink to our per_cu entry. */
ae038cb0
DJ
464 struct dwarf2_per_cu_data *per_cu;
465
466 /* How many compilation units ago was this CU last referenced? */
fcd3b13d 467 int last_used = 0;
ae038cb0 468
b64f50a1
JK
469 /* A hash table of DIE cu_offset for following references with
470 die_info->offset.sect_off as hash. */
fcd3b13d 471 htab_t die_hash = nullptr;
10b3939b
DJ
472
473 /* Full DIEs if read in. */
fcd3b13d 474 struct die_info *dies = nullptr;
10b3939b
DJ
475
476 /* A set of pointers to dwarf2_per_cu_data objects for compilation
477 units referenced by this one. Only set during full symbol processing;
478 partial symbol tables do not have dependencies. */
fcd3b13d 479 htab_t dependencies = nullptr;
10b3939b 480
cb1df416 481 /* Header data from the line table, during full symbol processing. */
fcd3b13d 482 struct line_header *line_header = nullptr;
4c8aa72d
PA
483 /* Non-NULL if LINE_HEADER is owned by this DWARF_CU. Otherwise,
484 it's owned by dwarf2_per_objfile::line_header_hash. If non-NULL,
485 this is the DW_TAG_compile_unit die for this CU. We'll hold on
486 to the line header as long as this DIE is being processed. See
487 process_die_scope. */
fcd3b13d 488 die_info *line_header_die_owner = nullptr;
cb1df416 489
3da10d80
KS
490 /* A list of methods which need to have physnames computed
491 after all type information has been read. */
c89b44cd 492 std::vector<delayed_method_info> method_list;
3da10d80 493
96408a79 494 /* To be copied to symtab->call_site_htab. */
fcd3b13d 495 htab_t call_site_htab = nullptr;
96408a79 496
034e5797
DE
497 /* Non-NULL if this CU came from a DWO file.
498 There is an invariant here that is important to remember:
499 Except for attributes copied from the top level DIE in the "main"
500 (or "stub") file in preparation for reading the DWO file
501 (e.g., DW_AT_GNU_addr_base), we KISS: there is only *one* CU.
502 Either there isn't a DWO file (in which case this is NULL and the point
503 is moot), or there is and either we're not going to read it (in which
504 case this is NULL) or there is and we are reading it (in which case this
505 is non-NULL). */
fcd3b13d 506 struct dwo_unit *dwo_unit = nullptr;
3019eac3
DE
507
508 /* The DW_AT_addr_base attribute if present, zero otherwise
509 (zero is a valid value though).
1dbab08b 510 Note this value comes from the Fission stub CU/TU's DIE. */
fcd3b13d 511 ULONGEST addr_base = 0;
3019eac3 512
2e3cf129
DE
513 /* The DW_AT_ranges_base attribute if present, zero otherwise
514 (zero is a valid value though).
1dbab08b 515 Note this value comes from the Fission stub CU/TU's DIE.
2e3cf129 516 Also note that the value is zero in the non-DWO case so this value can
ab435259
DE
517 be used without needing to know whether DWO files are in use or not.
518 N.B. This does not apply to DW_AT_ranges appearing in
519 DW_TAG_compile_unit dies. This is a bit of a wart, consider if ever
520 DW_AT_ranges appeared in the DW_TAG_compile_unit of DWO DIEs: then
521 DW_AT_ranges_base *would* have to be applied, and we'd have to care
522 whether the DW_AT_ranges attribute came from the skeleton or DWO. */
fcd3b13d 523 ULONGEST ranges_base = 0;
2e3cf129 524
c9317f21
TT
525 /* When reading debug info generated by older versions of rustc, we
526 have to rewrite some union types to be struct types with a
527 variant part. This rewriting must be done after the CU is fully
528 read in, because otherwise at the point of rewriting some struct
529 type might not have been fully processed. So, we keep a list of
530 all such types here and process them after expansion. */
531 std::vector<struct type *> rust_unions;
532
ae038cb0
DJ
533 /* Mark used when releasing cached dies. */
534 unsigned int mark : 1;
535
8be455d7
JK
536 /* This CU references .debug_loc. See the symtab->locations_valid field.
537 This test is imperfect as there may exist optimized debug code not using
538 any location list and still facing inlining issues if handled as
539 unoptimized code. For a future better test see GCC PR other/32998. */
8be455d7 540 unsigned int has_loclist : 1;
ba919b58 541
1b80a9fa
JK
542 /* These cache the results for producer_is_* fields. CHECKED_PRODUCER is set
543 if all the producer_is_* fields are valid. This information is cached
544 because profiling CU expansion showed excessive time spent in
545 producer_is_gxx_lt_4_6. */
ba919b58
TT
546 unsigned int checked_producer : 1;
547 unsigned int producer_is_gxx_lt_4_6 : 1;
1b80a9fa 548 unsigned int producer_is_gcc_lt_4_3 : 1;
5230b05a 549 unsigned int producer_is_icc_lt_14 : 1;
4d4ec4e5
TT
550
551 /* When set, the file that we're processing is known to have
552 debugging info for C++ namespaces. GCC 3.3.x did not produce
553 this information, but later versions do. */
554
555 unsigned int processing_has_namespace_info : 1;
d590ff25
YQ
556
557 struct partial_die_info *find_partial_die (sect_offset sect_off);
e7c27a73
DJ
558};
559
094b34ac
DE
560/* A struct that can be used as a hash key for tables based on DW_AT_stmt_list.
561 This includes type_unit_group and quick_file_names. */
562
563struct stmt_list_hash
564{
565 /* The DWO unit this table is from or NULL if there is none. */
566 struct dwo_unit *dwo_unit;
567
568 /* Offset in .debug_line or .debug_line.dwo. */
9c541725 569 sect_offset line_sect_off;
094b34ac
DE
570};
571
f4dc4d17
DE
572/* Each element of dwarf2_per_objfile->type_unit_groups is a pointer to
573 an object of this type. */
574
575struct type_unit_group
576{
0186c6a7 577 /* dwarf2read.c's main "handle" on a TU symtab.
f4dc4d17
DE
578 To simplify things we create an artificial CU that "includes" all the
579 type units using this stmt_list so that the rest of the code still has
580 a "per_cu" handle on the symtab.
581 This PER_CU is recognized by having no section. */
8a0459fd 582#define IS_TYPE_UNIT_GROUP(per_cu) ((per_cu)->section == NULL)
094b34ac
DE
583 struct dwarf2_per_cu_data per_cu;
584
0186c6a7
DE
585 /* The TUs that share this DW_AT_stmt_list entry.
586 This is added to while parsing type units to build partial symtabs,
587 and is deleted afterwards and not used again. */
588 VEC (sig_type_ptr) *tus;
f4dc4d17 589
43f3e411 590 /* The compunit symtab.
094b34ac 591 Type units in a group needn't all be defined in the same source file,
43f3e411
DE
592 so we create an essentially anonymous symtab as the compunit symtab. */
593 struct compunit_symtab *compunit_symtab;
f4dc4d17 594
094b34ac
DE
595 /* The data used to construct the hash key. */
596 struct stmt_list_hash hash;
f4dc4d17
DE
597
598 /* The number of symtabs from the line header.
599 The value here must match line_header.num_file_names. */
600 unsigned int num_symtabs;
601
602 /* The symbol tables for this TU (obtained from the files listed in
603 DW_AT_stmt_list).
604 WARNING: The order of entries here must match the order of entries
605 in the line header. After the first TU using this type_unit_group, the
606 line header for the subsequent TUs is recreated from this. This is done
607 because we need to use the same symtabs for each TU using the same
608 DW_AT_stmt_list value. Also note that symtabs may be repeated here,
609 there's no guarantee the line header doesn't have duplicate entries. */
610 struct symtab **symtabs;
611};
612
73869dc2 613/* These sections are what may appear in a (real or virtual) DWO file. */
3019eac3
DE
614
615struct dwo_sections
616{
617 struct dwarf2_section_info abbrev;
3019eac3
DE
618 struct dwarf2_section_info line;
619 struct dwarf2_section_info loc;
43988095 620 struct dwarf2_section_info loclists;
09262596
DE
621 struct dwarf2_section_info macinfo;
622 struct dwarf2_section_info macro;
3019eac3
DE
623 struct dwarf2_section_info str;
624 struct dwarf2_section_info str_offsets;
80626a55
DE
625 /* In the case of a virtual DWO file, these two are unused. */
626 struct dwarf2_section_info info;
3019eac3
DE
627 VEC (dwarf2_section_info_def) *types;
628};
629
c88ee1f0 630/* CUs/TUs in DWP/DWO files. */
3019eac3
DE
631
632struct dwo_unit
633{
634 /* Backlink to the containing struct dwo_file. */
635 struct dwo_file *dwo_file;
636
637 /* The "id" that distinguishes this CU/TU.
638 .debug_info calls this "dwo_id", .debug_types calls this "signature".
639 Since signatures came first, we stick with it for consistency. */
640 ULONGEST signature;
641
642 /* The section this CU/TU lives in, in the DWO file. */
8a0459fd 643 struct dwarf2_section_info *section;
3019eac3 644
9c541725
PA
645 /* Same as dwarf2_per_cu_data:{sect_off,length} but in the DWO section. */
646 sect_offset sect_off;
3019eac3
DE
647 unsigned int length;
648
649 /* For types, offset in the type's DIE of the type defined by this TU. */
650 cu_offset type_offset_in_tu;
651};
652
73869dc2
DE
653/* include/dwarf2.h defines the DWP section codes.
654 It defines a max value but it doesn't define a min value, which we
655 use for error checking, so provide one. */
656
657enum dwp_v2_section_ids
658{
659 DW_SECT_MIN = 1
660};
661
80626a55 662/* Data for one DWO file.
57d63ce2
DE
663
664 This includes virtual DWO files (a virtual DWO file is a DWO file as it
665 appears in a DWP file). DWP files don't really have DWO files per se -
666 comdat folding of types "loses" the DWO file they came from, and from
667 a high level view DWP files appear to contain a mass of random types.
668 However, to maintain consistency with the non-DWP case we pretend DWP
669 files contain virtual DWO files, and we assign each TU with one virtual
670 DWO file (generally based on the line and abbrev section offsets -
671 a heuristic that seems to work in practice). */
3019eac3
DE
672
673struct dwo_file
674{
0ac5b59e 675 /* The DW_AT_GNU_dwo_name attribute.
80626a55
DE
676 For virtual DWO files the name is constructed from the section offsets
677 of abbrev,line,loc,str_offsets so that we combine virtual DWO files
678 from related CU+TUs. */
0ac5b59e
DE
679 const char *dwo_name;
680
681 /* The DW_AT_comp_dir attribute. */
682 const char *comp_dir;
3019eac3 683
80626a55
DE
684 /* The bfd, when the file is open. Otherwise this is NULL.
685 This is unused(NULL) for virtual DWO files where we use dwp_file.dbfd. */
686 bfd *dbfd;
3019eac3 687
73869dc2
DE
688 /* The sections that make up this DWO file.
689 Remember that for virtual DWO files in DWP V2, these are virtual
690 sections (for lack of a better name). */
3019eac3
DE
691 struct dwo_sections sections;
692
33c5cd75
DB
693 /* The CUs in the file.
694 Each element is a struct dwo_unit. Multiple CUs per DWO are supported as
695 an extension to handle LLVM's Link Time Optimization output (where
696 multiple source files may be compiled into a single object/dwo pair). */
697 htab_t cus;
3019eac3
DE
698
699 /* Table of TUs in the file.
700 Each element is a struct dwo_unit. */
701 htab_t tus;
702};
703
80626a55
DE
704/* These sections are what may appear in a DWP file. */
705
706struct dwp_sections
707{
73869dc2 708 /* These are used by both DWP version 1 and 2. */
80626a55
DE
709 struct dwarf2_section_info str;
710 struct dwarf2_section_info cu_index;
711 struct dwarf2_section_info tu_index;
73869dc2
DE
712
713 /* These are only used by DWP version 2 files.
714 In DWP version 1 the .debug_info.dwo, .debug_types.dwo, and other
715 sections are referenced by section number, and are not recorded here.
716 In DWP version 2 there is at most one copy of all these sections, each
717 section being (effectively) comprised of the concatenation of all of the
718 individual sections that exist in the version 1 format.
719 To keep the code simple we treat each of these concatenated pieces as a
720 section itself (a virtual section?). */
721 struct dwarf2_section_info abbrev;
722 struct dwarf2_section_info info;
723 struct dwarf2_section_info line;
724 struct dwarf2_section_info loc;
725 struct dwarf2_section_info macinfo;
726 struct dwarf2_section_info macro;
727 struct dwarf2_section_info str_offsets;
728 struct dwarf2_section_info types;
80626a55
DE
729};
730
73869dc2
DE
731/* These sections are what may appear in a virtual DWO file in DWP version 1.
732 A virtual DWO file is a DWO file as it appears in a DWP file. */
80626a55 733
73869dc2 734struct virtual_v1_dwo_sections
80626a55
DE
735{
736 struct dwarf2_section_info abbrev;
737 struct dwarf2_section_info line;
738 struct dwarf2_section_info loc;
739 struct dwarf2_section_info macinfo;
740 struct dwarf2_section_info macro;
741 struct dwarf2_section_info str_offsets;
742 /* Each DWP hash table entry records one CU or one TU.
8a0459fd 743 That is recorded here, and copied to dwo_unit.section. */
80626a55
DE
744 struct dwarf2_section_info info_or_types;
745};
746
73869dc2
DE
747/* Similar to virtual_v1_dwo_sections, but for DWP version 2.
748 In version 2, the sections of the DWO files are concatenated together
749 and stored in one section of that name. Thus each ELF section contains
750 several "virtual" sections. */
751
752struct virtual_v2_dwo_sections
753{
754 bfd_size_type abbrev_offset;
755 bfd_size_type abbrev_size;
756
757 bfd_size_type line_offset;
758 bfd_size_type line_size;
759
760 bfd_size_type loc_offset;
761 bfd_size_type loc_size;
762
763 bfd_size_type macinfo_offset;
764 bfd_size_type macinfo_size;
765
766 bfd_size_type macro_offset;
767 bfd_size_type macro_size;
768
769 bfd_size_type str_offsets_offset;
770 bfd_size_type str_offsets_size;
771
772 /* Each DWP hash table entry records one CU or one TU.
773 That is recorded here, and copied to dwo_unit.section. */
774 bfd_size_type info_or_types_offset;
775 bfd_size_type info_or_types_size;
776};
777
80626a55
DE
778/* Contents of DWP hash tables. */
779
780struct dwp_hash_table
781{
73869dc2 782 uint32_t version, nr_columns;
80626a55 783 uint32_t nr_units, nr_slots;
73869dc2
DE
784 const gdb_byte *hash_table, *unit_table;
785 union
786 {
787 struct
788 {
789 const gdb_byte *indices;
790 } v1;
791 struct
792 {
793 /* This is indexed by column number and gives the id of the section
794 in that column. */
795#define MAX_NR_V2_DWO_SECTIONS \
796 (1 /* .debug_info or .debug_types */ \
797 + 1 /* .debug_abbrev */ \
798 + 1 /* .debug_line */ \
799 + 1 /* .debug_loc */ \
800 + 1 /* .debug_str_offsets */ \
801 + 1 /* .debug_macro or .debug_macinfo */)
802 int section_ids[MAX_NR_V2_DWO_SECTIONS];
803 const gdb_byte *offsets;
804 const gdb_byte *sizes;
805 } v2;
806 } section_pool;
80626a55
DE
807};
808
809/* Data for one DWP file. */
810
811struct dwp_file
812{
813 /* Name of the file. */
814 const char *name;
815
73869dc2
DE
816 /* File format version. */
817 int version;
818
93417882 819 /* The bfd. */
80626a55
DE
820 bfd *dbfd;
821
822 /* Section info for this file. */
823 struct dwp_sections sections;
824
57d63ce2 825 /* Table of CUs in the file. */
80626a55
DE
826 const struct dwp_hash_table *cus;
827
828 /* Table of TUs in the file. */
829 const struct dwp_hash_table *tus;
830
19ac8c2e
DE
831 /* Tables of loaded CUs/TUs. Each entry is a struct dwo_unit *. */
832 htab_t loaded_cus;
833 htab_t loaded_tus;
80626a55 834
73869dc2
DE
835 /* Table to map ELF section numbers to their sections.
836 This is only needed for the DWP V1 file format. */
80626a55
DE
837 unsigned int num_sections;
838 asection **elf_sections;
839};
840
36586728
TT
841/* This represents a '.dwz' file. */
842
843struct dwz_file
844{
845 /* A dwz file can only contain a few sections. */
846 struct dwarf2_section_info abbrev;
847 struct dwarf2_section_info info;
848 struct dwarf2_section_info str;
849 struct dwarf2_section_info line;
850 struct dwarf2_section_info macro;
2ec9a5e0 851 struct dwarf2_section_info gdb_index;
927aa2e7 852 struct dwarf2_section_info debug_names;
36586728
TT
853
854 /* The dwz's BFD. */
855 bfd *dwz_bfd;
856};
857
0963b4bd
MS
858/* Struct used to pass misc. parameters to read_die_and_children, et
859 al. which are used for both .debug_info and .debug_types dies.
860 All parameters here are unchanging for the life of the call. This
dee91e82 861 struct exists to abstract away the constant parameters of die reading. */
93311388
DE
862
863struct die_reader_specs
864{
a32a8923 865 /* The bfd of die_section. */
93311388
DE
866 bfd* abfd;
867
868 /* The CU of the DIE we are parsing. */
869 struct dwarf2_cu *cu;
870
80626a55 871 /* Non-NULL if reading a DWO file (including one packaged into a DWP). */
3019eac3
DE
872 struct dwo_file *dwo_file;
873
dee91e82 874 /* The section the die comes from.
3019eac3 875 This is either .debug_info or .debug_types, or the .dwo variants. */
dee91e82
DE
876 struct dwarf2_section_info *die_section;
877
878 /* die_section->buffer. */
d521ce57 879 const gdb_byte *buffer;
f664829e
DE
880
881 /* The end of the buffer. */
882 const gdb_byte *buffer_end;
a2ce51a0
DE
883
884 /* The value of the DW_AT_comp_dir attribute. */
885 const char *comp_dir;
685af9cd
TT
886
887 /* The abbreviation table to use when reading the DIEs. */
888 struct abbrev_table *abbrev_table;
93311388
DE
889};
890
fd820528 891/* Type of function passed to init_cutu_and_read_dies, et.al. */
dee91e82 892typedef void (die_reader_func_ftype) (const struct die_reader_specs *reader,
d521ce57 893 const gdb_byte *info_ptr,
dee91e82
DE
894 struct die_info *comp_unit_die,
895 int has_children,
896 void *data);
897
ecfb656c
PA
898/* A 1-based directory index. This is a strong typedef to prevent
899 accidentally using a directory index as a 0-based index into an
900 array/vector. */
901enum class dir_index : unsigned int {};
902
903/* Likewise, a 1-based file name index. */
904enum class file_name_index : unsigned int {};
905
52059ffd
TT
906struct file_entry
907{
fff8551c
PA
908 file_entry () = default;
909
ecfb656c 910 file_entry (const char *name_, dir_index d_index_,
fff8551c
PA
911 unsigned int mod_time_, unsigned int length_)
912 : name (name_),
ecfb656c 913 d_index (d_index_),
fff8551c
PA
914 mod_time (mod_time_),
915 length (length_)
916 {}
917
ecfb656c
PA
918 /* Return the include directory at D_INDEX stored in LH. Returns
919 NULL if D_INDEX is out of bounds. */
8c43009f
PA
920 const char *include_dir (const line_header *lh) const;
921
fff8551c
PA
922 /* The file name. Note this is an observing pointer. The memory is
923 owned by debug_line_buffer. */
924 const char *name {};
925
8c43009f 926 /* The directory index (1-based). */
ecfb656c 927 dir_index d_index {};
fff8551c
PA
928
929 unsigned int mod_time {};
930
931 unsigned int length {};
932
933 /* True if referenced by the Line Number Program. */
934 bool included_p {};
935
83769d0b 936 /* The associated symbol table, if any. */
fff8551c 937 struct symtab *symtab {};
52059ffd
TT
938};
939
debd256d
JB
940/* The line number information for a compilation unit (found in the
941 .debug_line section) begins with a "statement program header",
942 which contains the following information. */
943struct line_header
944{
fff8551c
PA
945 line_header ()
946 : offset_in_dwz {}
947 {}
948
949 /* Add an entry to the include directory table. */
950 void add_include_dir (const char *include_dir);
951
952 /* Add an entry to the file name table. */
ecfb656c 953 void add_file_name (const char *name, dir_index d_index,
fff8551c
PA
954 unsigned int mod_time, unsigned int length);
955
ecfb656c 956 /* Return the include dir at INDEX (1-based). Returns NULL if INDEX
8c43009f 957 is out of bounds. */
ecfb656c 958 const char *include_dir_at (dir_index index) const
8c43009f 959 {
ecfb656c
PA
960 /* Convert directory index number (1-based) to vector index
961 (0-based). */
962 size_t vec_index = to_underlying (index) - 1;
963
964 if (vec_index >= include_dirs.size ())
8c43009f 965 return NULL;
ecfb656c 966 return include_dirs[vec_index];
8c43009f
PA
967 }
968
ecfb656c 969 /* Return the file name at INDEX (1-based). Returns NULL if INDEX
8c43009f 970 is out of bounds. */
ecfb656c 971 file_entry *file_name_at (file_name_index index)
8c43009f 972 {
ecfb656c
PA
973 /* Convert file name index number (1-based) to vector index
974 (0-based). */
975 size_t vec_index = to_underlying (index) - 1;
976
977 if (vec_index >= file_names.size ())
fff8551c 978 return NULL;
ecfb656c 979 return &file_names[vec_index];
fff8551c
PA
980 }
981
982 /* Const version of the above. */
983 const file_entry *file_name_at (unsigned int index) const
984 {
985 if (index >= file_names.size ())
8c43009f
PA
986 return NULL;
987 return &file_names[index];
988 }
989
527f3840 990 /* Offset of line number information in .debug_line section. */
9c541725 991 sect_offset sect_off {};
527f3840
JK
992
993 /* OFFSET is for struct dwz_file associated with dwarf2_per_objfile. */
fff8551c
PA
994 unsigned offset_in_dwz : 1; /* Can't initialize bitfields in-class. */
995
996 unsigned int total_length {};
997 unsigned short version {};
998 unsigned int header_length {};
999 unsigned char minimum_instruction_length {};
1000 unsigned char maximum_ops_per_instruction {};
1001 unsigned char default_is_stmt {};
1002 int line_base {};
1003 unsigned char line_range {};
1004 unsigned char opcode_base {};
debd256d
JB
1005
1006 /* standard_opcode_lengths[i] is the number of operands for the
1007 standard opcode whose value is i. This means that
1008 standard_opcode_lengths[0] is unused, and the last meaningful
1009 element is standard_opcode_lengths[opcode_base - 1]. */
fff8551c 1010 std::unique_ptr<unsigned char[]> standard_opcode_lengths;
debd256d 1011
fff8551c
PA
1012 /* The include_directories table. Note these are observing
1013 pointers. The memory is owned by debug_line_buffer. */
1014 std::vector<const char *> include_dirs;
debd256d 1015
fff8551c
PA
1016 /* The file_names table. */
1017 std::vector<file_entry> file_names;
debd256d
JB
1018
1019 /* The start and end of the statement program following this
6502dd73 1020 header. These point into dwarf2_per_objfile->line_buffer. */
fff8551c 1021 const gdb_byte *statement_program_start {}, *statement_program_end {};
debd256d 1022};
c906108c 1023
fff8551c
PA
1024typedef std::unique_ptr<line_header> line_header_up;
1025
8c43009f
PA
1026const char *
1027file_entry::include_dir (const line_header *lh) const
1028{
ecfb656c 1029 return lh->include_dir_at (d_index);
8c43009f
PA
1030}
1031
c906108c 1032/* When we construct a partial symbol table entry we only
0963b4bd 1033 need this much information. */
6f06d47b 1034struct partial_die_info : public allocate_on_obstack
c906108c 1035 {
6f06d47b
YQ
1036 partial_die_info (sect_offset sect_off, struct abbrev_info *abbrev);
1037
1038 /* Disable assign but still keep copy ctor, which is needed
1039 load_partial_dies. */
1040 partial_die_info& operator=(const partial_die_info& rhs) = delete;
1041
52356b79
YQ
1042 /* Adjust the partial die before generating a symbol for it. This
1043 function may set the is_external flag or change the DIE's
1044 name. */
1045 void fixup (struct dwarf2_cu *cu);
1046
48fbe735
YQ
1047 /* Read a minimal amount of information into the minimal die
1048 structure. */
1049 const gdb_byte *read (const struct die_reader_specs *reader,
1050 const struct abbrev_info &abbrev,
1051 const gdb_byte *info_ptr);
1052
72bf9492 1053 /* Offset of this DIE. */
6f06d47b 1054 const sect_offset sect_off;
72bf9492
DJ
1055
1056 /* DWARF-2 tag for this DIE. */
6f06d47b 1057 const ENUM_BITFIELD(dwarf_tag) tag : 16;
72bf9492 1058
72bf9492 1059 /* Assorted flags describing the data found in this DIE. */
6f06d47b
YQ
1060 const unsigned int has_children : 1;
1061
72bf9492
DJ
1062 unsigned int is_external : 1;
1063 unsigned int is_declaration : 1;
1064 unsigned int has_type : 1;
1065 unsigned int has_specification : 1;
1066 unsigned int has_pc_info : 1;
481860b3 1067 unsigned int may_be_inlined : 1;
72bf9492 1068
0c1b455e
TT
1069 /* This DIE has been marked DW_AT_main_subprogram. */
1070 unsigned int main_subprogram : 1;
1071
72bf9492
DJ
1072 /* Flag set if the SCOPE field of this structure has been
1073 computed. */
1074 unsigned int scope_set : 1;
1075
fa4028e9
JB
1076 /* Flag set if the DIE has a byte_size attribute. */
1077 unsigned int has_byte_size : 1;
1078
ff908ebf
AW
1079 /* Flag set if the DIE has a DW_AT_const_value attribute. */
1080 unsigned int has_const_value : 1;
1081
98bfdba5
PA
1082 /* Flag set if any of the DIE's children are template arguments. */
1083 unsigned int has_template_arguments : 1;
1084
52356b79 1085 /* Flag set if fixup has been called on this die. */
abc72ce4
DE
1086 unsigned int fixup_called : 1;
1087
36586728
TT
1088 /* Flag set if DW_TAG_imported_unit uses DW_FORM_GNU_ref_alt. */
1089 unsigned int is_dwz : 1;
1090
1091 /* Flag set if spec_offset uses DW_FORM_GNU_ref_alt. */
1092 unsigned int spec_is_dwz : 1;
1093
72bf9492 1094 /* The name of this DIE. Normally the value of DW_AT_name, but
94af9270 1095 sometimes a default name for unnamed DIEs. */
6f06d47b 1096 const char *name = nullptr;
72bf9492 1097
abc72ce4 1098 /* The linkage name, if present. */
6f06d47b 1099 const char *linkage_name = nullptr;
abc72ce4 1100
72bf9492
DJ
1101 /* The scope to prepend to our children. This is generally
1102 allocated on the comp_unit_obstack, so will disappear
1103 when this compilation unit leaves the cache. */
6f06d47b 1104 const char *scope = nullptr;
72bf9492 1105
95554aad
TT
1106 /* Some data associated with the partial DIE. The tag determines
1107 which field is live. */
1108 union
1109 {
1110 /* The location description associated with this DIE, if any. */
1111 struct dwarf_block *locdesc;
1112 /* The offset of an import, for DW_TAG_imported_unit. */
9c541725 1113 sect_offset sect_off;
6f06d47b 1114 } d {};
72bf9492
DJ
1115
1116 /* If HAS_PC_INFO, the PC range associated with this DIE. */
6f06d47b
YQ
1117 CORE_ADDR lowpc = 0;
1118 CORE_ADDR highpc = 0;
72bf9492 1119
93311388 1120 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
72bf9492 1121 DW_AT_sibling, if any. */
48fbe735
YQ
1122 /* NOTE: This member isn't strictly necessary, partial_die_info::read
1123 could return DW_AT_sibling values to its caller load_partial_dies. */
6f06d47b 1124 const gdb_byte *sibling = nullptr;
72bf9492
DJ
1125
1126 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
1127 DW_AT_specification (or DW_AT_abstract_origin or
1128 DW_AT_extension). */
6f06d47b 1129 sect_offset spec_offset {};
72bf9492
DJ
1130
1131 /* Pointers to this DIE's parent, first child, and next sibling,
1132 if any. */
6f06d47b
YQ
1133 struct partial_die_info *die_parent = nullptr;
1134 struct partial_die_info *die_child = nullptr;
1135 struct partial_die_info *die_sibling = nullptr;
1136
1137 friend struct partial_die_info *
1138 dwarf2_cu::find_partial_die (sect_offset sect_off);
1139
1140 private:
1141 /* Only need to do look up in dwarf2_cu::find_partial_die. */
1142 partial_die_info (sect_offset sect_off)
1143 : partial_die_info (sect_off, DW_TAG_padding, 0)
1144 {
1145 }
1146
1147 partial_die_info (sect_offset sect_off_, enum dwarf_tag tag_,
1148 int has_children_)
1149 : sect_off (sect_off_), tag (tag_), has_children (has_children_)
1150 {
1151 is_external = 0;
1152 is_declaration = 0;
1153 has_type = 0;
1154 has_specification = 0;
1155 has_pc_info = 0;
1156 may_be_inlined = 0;
1157 main_subprogram = 0;
1158 scope_set = 0;
1159 has_byte_size = 0;
1160 has_const_value = 0;
1161 has_template_arguments = 0;
1162 fixup_called = 0;
1163 is_dwz = 0;
1164 spec_is_dwz = 0;
1165 }
c906108c
SS
1166 };
1167
0963b4bd 1168/* This data structure holds the information of an abbrev. */
c906108c
SS
1169struct abbrev_info
1170 {
1171 unsigned int number; /* number identifying abbrev */
1172 enum dwarf_tag tag; /* dwarf tag */
f3dd6933
DJ
1173 unsigned short has_children; /* boolean */
1174 unsigned short num_attrs; /* number of attributes */
c906108c
SS
1175 struct attr_abbrev *attrs; /* an array of attribute descriptions */
1176 struct abbrev_info *next; /* next in chain */
1177 };
1178
1179struct attr_abbrev
1180 {
9d25dd43
DE
1181 ENUM_BITFIELD(dwarf_attribute) name : 16;
1182 ENUM_BITFIELD(dwarf_form) form : 16;
43988095
JK
1183
1184 /* It is valid only if FORM is DW_FORM_implicit_const. */
1185 LONGEST implicit_const;
c906108c
SS
1186 };
1187
433df2d4
DE
1188/* Size of abbrev_table.abbrev_hash_table. */
1189#define ABBREV_HASH_SIZE 121
1190
1191/* Top level data structure to contain an abbreviation table. */
1192
1193struct abbrev_table
1194{
685af9cd
TT
1195 explicit abbrev_table (sect_offset off)
1196 : sect_off (off)
1197 {
4a17f768 1198 m_abbrevs =
685af9cd 1199 XOBNEWVEC (&abbrev_obstack, struct abbrev_info *, ABBREV_HASH_SIZE);
4a17f768 1200 memset (m_abbrevs, 0, ABBREV_HASH_SIZE * sizeof (struct abbrev_info *));
685af9cd
TT
1201 }
1202
1203 DISABLE_COPY_AND_ASSIGN (abbrev_table);
1204
1205 /* Allocate space for a struct abbrev_info object in
1206 ABBREV_TABLE. */
1207 struct abbrev_info *alloc_abbrev ();
1208
1209 /* Add an abbreviation to the table. */
1210 void add_abbrev (unsigned int abbrev_number, struct abbrev_info *abbrev);
1211
1212 /* Look up an abbrev in the table.
1213 Returns NULL if the abbrev is not found. */
1214
1215 struct abbrev_info *lookup_abbrev (unsigned int abbrev_number);
1216
1217
f4dc4d17
DE
1218 /* Where the abbrev table came from.
1219 This is used as a sanity check when the table is used. */
685af9cd 1220 const sect_offset sect_off;
433df2d4
DE
1221
1222 /* Storage for the abbrev table. */
685af9cd 1223 auto_obstack abbrev_obstack;
433df2d4 1224
4a17f768
YQ
1225private:
1226
433df2d4
DE
1227 /* Hash table of abbrevs.
1228 This is an array of size ABBREV_HASH_SIZE allocated in abbrev_obstack.
1229 It could be statically allocated, but the previous code didn't so we
1230 don't either. */
4a17f768 1231 struct abbrev_info **m_abbrevs;
433df2d4
DE
1232};
1233
685af9cd
TT
1234typedef std::unique_ptr<struct abbrev_table> abbrev_table_up;
1235
0963b4bd 1236/* Attributes have a name and a value. */
b60c80d6
DJ
1237struct attribute
1238 {
9d25dd43 1239 ENUM_BITFIELD(dwarf_attribute) name : 16;
8285870a
JK
1240 ENUM_BITFIELD(dwarf_form) form : 15;
1241
1242 /* Has DW_STRING already been updated by dwarf2_canonicalize_name? This
1243 field should be in u.str (existing only for DW_STRING) but it is kept
1244 here for better struct attribute alignment. */
1245 unsigned int string_is_canonical : 1;
1246
b60c80d6
DJ
1247 union
1248 {
15d034d0 1249 const char *str;
b60c80d6 1250 struct dwarf_block *blk;
43bbcdc2
PH
1251 ULONGEST unsnd;
1252 LONGEST snd;
b60c80d6 1253 CORE_ADDR addr;
ac9ec31b 1254 ULONGEST signature;
b60c80d6
DJ
1255 }
1256 u;
1257 };
1258
0963b4bd 1259/* This data structure holds a complete die structure. */
c906108c
SS
1260struct die_info
1261 {
76815b17
DE
1262 /* DWARF-2 tag for this DIE. */
1263 ENUM_BITFIELD(dwarf_tag) tag : 16;
1264
1265 /* Number of attributes */
98bfdba5
PA
1266 unsigned char num_attrs;
1267
1268 /* True if we're presently building the full type name for the
1269 type derived from this DIE. */
1270 unsigned char building_fullname : 1;
76815b17 1271
adde2bff
DE
1272 /* True if this die is in process. PR 16581. */
1273 unsigned char in_process : 1;
1274
76815b17
DE
1275 /* Abbrev number */
1276 unsigned int abbrev;
1277
93311388 1278 /* Offset in .debug_info or .debug_types section. */
9c541725 1279 sect_offset sect_off;
78ba4af6
JB
1280
1281 /* The dies in a compilation unit form an n-ary tree. PARENT
1282 points to this die's parent; CHILD points to the first child of
1283 this node; and all the children of a given node are chained
4950bc1c 1284 together via their SIBLING fields. */
639d11d3
DC
1285 struct die_info *child; /* Its first child, if any. */
1286 struct die_info *sibling; /* Its next sibling, if any. */
1287 struct die_info *parent; /* Its parent, if any. */
c906108c 1288
b60c80d6
DJ
1289 /* An array of attributes, with NUM_ATTRS elements. There may be
1290 zero, but it's not common and zero-sized arrays are not
1291 sufficiently portable C. */
1292 struct attribute attrs[1];
c906108c
SS
1293 };
1294
0963b4bd 1295/* Get at parts of an attribute structure. */
c906108c
SS
1296
1297#define DW_STRING(attr) ((attr)->u.str)
8285870a 1298#define DW_STRING_IS_CANONICAL(attr) ((attr)->string_is_canonical)
c906108c
SS
1299#define DW_UNSND(attr) ((attr)->u.unsnd)
1300#define DW_BLOCK(attr) ((attr)->u.blk)
1301#define DW_SND(attr) ((attr)->u.snd)
1302#define DW_ADDR(attr) ((attr)->u.addr)
ac9ec31b 1303#define DW_SIGNATURE(attr) ((attr)->u.signature)
c906108c 1304
0963b4bd 1305/* Blocks are a bunch of untyped bytes. */
c906108c
SS
1306struct dwarf_block
1307 {
56eb65bd 1308 size_t size;
1d6edc3c
JK
1309
1310 /* Valid only if SIZE is not zero. */
d521ce57 1311 const gdb_byte *data;
c906108c
SS
1312 };
1313
c906108c
SS
1314#ifndef ATTR_ALLOC_CHUNK
1315#define ATTR_ALLOC_CHUNK 4
1316#endif
1317
c906108c
SS
1318/* Allocate fields for structs, unions and enums in this size. */
1319#ifndef DW_FIELD_ALLOC_CHUNK
1320#define DW_FIELD_ALLOC_CHUNK 4
1321#endif
1322
c906108c
SS
1323/* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
1324 but this would require a corresponding change in unpack_field_as_long
1325 and friends. */
1326static int bits_per_byte = 8;
1327
2ddeaf8a
TT
1328/* When reading a variant or variant part, we track a bit more
1329 information about the field, and store it in an object of this
1330 type. */
1331
1332struct variant_field
1333{
1334 /* If we see a DW_TAG_variant, then this will be the discriminant
1335 value. */
1336 ULONGEST discriminant_value;
1337 /* If we see a DW_TAG_variant, then this will be set if this is the
1338 default branch. */
1339 bool default_branch;
1340 /* While reading a DW_TAG_variant_part, this will be set if this
1341 field is the discriminant. */
1342 bool is_discriminant;
1343};
1344
52059ffd
TT
1345struct nextfield
1346{
be2daae6
TT
1347 int accessibility = 0;
1348 int virtuality = 0;
2ddeaf8a 1349 /* Extra information to describe a variant or variant part. */
be2daae6
TT
1350 struct variant_field variant {};
1351 struct field field {};
52059ffd
TT
1352};
1353
1354struct fnfieldlist
1355{
be2daae6
TT
1356 const char *name = nullptr;
1357 std::vector<struct fn_field> fnfields;
52059ffd
TT
1358};
1359
c906108c
SS
1360/* The routines that read and process dies for a C struct or C++ class
1361 pass lists of data member fields and lists of member function fields
1362 in an instance of a field_info structure, as defined below. */
1363struct field_info
c5aa993b 1364 {
0963b4bd 1365 /* List of data member and baseclasses fields. */
be2daae6
TT
1366 std::vector<struct nextfield> fields;
1367 std::vector<struct nextfield> baseclasses;
c906108c 1368
7d0ccb61 1369 /* Number of fields (including baseclasses). */
be2daae6 1370 int nfields = 0;
c906108c 1371
c5aa993b 1372 /* Set if the accesibility of one of the fields is not public. */
be2daae6 1373 int non_public_fields = 0;
c906108c 1374
c5aa993b
JM
1375 /* Member function fieldlist array, contains name of possibly overloaded
1376 member function, number of overloaded member functions and a pointer
1377 to the head of the member function field chain. */
be2daae6 1378 std::vector<struct fnfieldlist> fnfieldlists;
98751a41
JK
1379
1380 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
1381 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
be2daae6 1382 std::vector<struct decl_field> typedef_field_list;
883fd55a
KS
1383
1384 /* Nested types defined by this class and the number of elements in this
1385 list. */
be2daae6 1386 std::vector<struct decl_field> nested_types_list;
c5aa993b 1387 };
c906108c 1388
10b3939b
DJ
1389/* One item on the queue of compilation units to read in full symbols
1390 for. */
1391struct dwarf2_queue_item
1392{
1393 struct dwarf2_per_cu_data *per_cu;
95554aad 1394 enum language pretend_language;
10b3939b
DJ
1395 struct dwarf2_queue_item *next;
1396};
1397
1398/* The current queue. */
1399static struct dwarf2_queue_item *dwarf2_queue, *dwarf2_queue_tail;
1400
ae038cb0
DJ
1401/* Loaded secondary compilation units are kept in memory until they
1402 have not been referenced for the processing of this many
1403 compilation units. Set this to zero to disable caching. Cache
1404 sizes of up to at least twenty will improve startup time for
1405 typical inter-CU-reference binaries, at an obvious memory cost. */
b4f54984 1406static int dwarf_max_cache_age = 5;
920d2a44 1407static void
b4f54984
DE
1408show_dwarf_max_cache_age (struct ui_file *file, int from_tty,
1409 struct cmd_list_element *c, const char *value)
920d2a44 1410{
3e43a32a 1411 fprintf_filtered (file, _("The upper bound on the age of cached "
b4f54984 1412 "DWARF compilation units is %s.\n"),
920d2a44
AC
1413 value);
1414}
4390d890 1415\f
c906108c
SS
1416/* local function prototypes */
1417
a32a8923
DE
1418static const char *get_section_name (const struct dwarf2_section_info *);
1419
1420static const char *get_section_file_name (const struct dwarf2_section_info *);
1421
918dd910
JK
1422static void dwarf2_find_base_address (struct die_info *die,
1423 struct dwarf2_cu *cu);
1424
0018ea6f
DE
1425static struct partial_symtab *create_partial_symtab
1426 (struct dwarf2_per_cu_data *per_cu, const char *name);
1427
f1902523
JK
1428static void build_type_psymtabs_reader (const struct die_reader_specs *reader,
1429 const gdb_byte *info_ptr,
1430 struct die_info *type_unit_die,
1431 int has_children, void *data);
1432
ed2dc618
SM
1433static void dwarf2_build_psymtabs_hard
1434 (struct dwarf2_per_objfile *dwarf2_per_objfile);
c906108c 1435
72bf9492
DJ
1436static void scan_partial_symbols (struct partial_die_info *,
1437 CORE_ADDR *, CORE_ADDR *,
5734ee8b 1438 int, struct dwarf2_cu *);
c906108c 1439
72bf9492
DJ
1440static void add_partial_symbol (struct partial_die_info *,
1441 struct dwarf2_cu *);
63d06c5c 1442
72bf9492
DJ
1443static void add_partial_namespace (struct partial_die_info *pdi,
1444 CORE_ADDR *lowpc, CORE_ADDR *highpc,
cdc07690 1445 int set_addrmap, struct dwarf2_cu *cu);
63d06c5c 1446
5d7cb8df 1447static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
cdc07690 1448 CORE_ADDR *highpc, int set_addrmap,
5d7cb8df
JK
1449 struct dwarf2_cu *cu);
1450
72bf9492
DJ
1451static void add_partial_enumeration (struct partial_die_info *enum_pdi,
1452 struct dwarf2_cu *cu);
91c24f0a 1453
bc30ff58
JB
1454static void add_partial_subprogram (struct partial_die_info *pdi,
1455 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 1456 int need_pc, struct dwarf2_cu *cu);
bc30ff58 1457
257e7a09
YQ
1458static void dwarf2_read_symtab (struct partial_symtab *,
1459 struct objfile *);
c906108c 1460
a14ed312 1461static void psymtab_to_symtab_1 (struct partial_symtab *);
c906108c 1462
685af9cd 1463static abbrev_table_up abbrev_table_read_table
ed2dc618
SM
1464 (struct dwarf2_per_objfile *dwarf2_per_objfile, struct dwarf2_section_info *,
1465 sect_offset);
433df2d4 1466
d521ce57 1467static unsigned int peek_abbrev_code (bfd *, const gdb_byte *);
6caca83c 1468
dee91e82 1469static struct partial_die_info *load_partial_dies
d521ce57 1470 (const struct die_reader_specs *, const gdb_byte *, int);
72bf9492 1471
36586728 1472static struct partial_die_info *find_partial_die (sect_offset, int,
10b3939b 1473 struct dwarf2_cu *);
72bf9492 1474
d521ce57
TT
1475static const gdb_byte *read_attribute (const struct die_reader_specs *,
1476 struct attribute *, struct attr_abbrev *,
1477 const gdb_byte *);
a8329558 1478
a1855c1d 1479static unsigned int read_1_byte (bfd *, const gdb_byte *);
c906108c 1480
a1855c1d 1481static int read_1_signed_byte (bfd *, const gdb_byte *);
c906108c 1482
a1855c1d 1483static unsigned int read_2_bytes (bfd *, const gdb_byte *);
c906108c 1484
a1855c1d 1485static unsigned int read_4_bytes (bfd *, const gdb_byte *);
c906108c 1486
a1855c1d 1487static ULONGEST read_8_bytes (bfd *, const gdb_byte *);
c906108c 1488
d521ce57 1489static CORE_ADDR read_address (bfd *, const gdb_byte *ptr, struct dwarf2_cu *,
891d2f0b 1490 unsigned int *);
c906108c 1491
d521ce57 1492static LONGEST read_initial_length (bfd *, const gdb_byte *, unsigned int *);
c764a876
DE
1493
1494static LONGEST read_checked_initial_length_and_offset
d521ce57 1495 (bfd *, const gdb_byte *, const struct comp_unit_head *,
c764a876 1496 unsigned int *, unsigned int *);
613e1657 1497
d521ce57
TT
1498static LONGEST read_offset (bfd *, const gdb_byte *,
1499 const struct comp_unit_head *,
c764a876
DE
1500 unsigned int *);
1501
d521ce57 1502static LONGEST read_offset_1 (bfd *, const gdb_byte *, unsigned int);
613e1657 1503
ed2dc618
SM
1504static sect_offset read_abbrev_offset
1505 (struct dwarf2_per_objfile *dwarf2_per_objfile,
1506 struct dwarf2_section_info *, sect_offset);
f4dc4d17 1507
d521ce57 1508static const gdb_byte *read_n_bytes (bfd *, const gdb_byte *, unsigned int);
c906108c 1509
d521ce57 1510static const char *read_direct_string (bfd *, const gdb_byte *, unsigned int *);
c906108c 1511
ed2dc618
SM
1512static const char *read_indirect_string
1513 (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *, const gdb_byte *,
1514 const struct comp_unit_head *, unsigned int *);
4bdf3d34 1515
ed2dc618
SM
1516static const char *read_indirect_line_string
1517 (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *, const gdb_byte *,
1518 const struct comp_unit_head *, unsigned int *);
36586728 1519
ed2dc618
SM
1520static const char *read_indirect_string_at_offset
1521 (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *abfd,
1522 LONGEST str_offset);
927aa2e7 1523
ed2dc618
SM
1524static const char *read_indirect_string_from_dwz
1525 (struct objfile *objfile, struct dwz_file *, LONGEST);
c906108c 1526
d521ce57 1527static LONGEST read_signed_leb128 (bfd *, const gdb_byte *, unsigned int *);
c906108c 1528
d521ce57
TT
1529static CORE_ADDR read_addr_index_from_leb128 (struct dwarf2_cu *,
1530 const gdb_byte *,
3019eac3
DE
1531 unsigned int *);
1532
d521ce57 1533static const char *read_str_index (const struct die_reader_specs *reader,
342587c4 1534 ULONGEST str_index);
3019eac3 1535
e142c38c 1536static void set_cu_language (unsigned int, struct dwarf2_cu *);
c906108c 1537
e142c38c
DJ
1538static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
1539 struct dwarf2_cu *);
c906108c 1540
348e048f 1541static struct attribute *dwarf2_attr_no_follow (struct die_info *,
45e58e77 1542 unsigned int);
348e048f 1543
7d45c7c3
KB
1544static const char *dwarf2_string_attr (struct die_info *die, unsigned int name,
1545 struct dwarf2_cu *cu);
1546
05cf31d1
JB
1547static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
1548 struct dwarf2_cu *cu);
1549
e142c38c 1550static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
3ca72b44 1551
e142c38c 1552static struct die_info *die_specification (struct die_info *die,
f2f0e013 1553 struct dwarf2_cu **);
63d06c5c 1554
9c541725 1555static line_header_up dwarf_decode_line_header (sect_offset sect_off,
fff8551c 1556 struct dwarf2_cu *cu);
debd256d 1557
f3f5162e 1558static void dwarf_decode_lines (struct line_header *, const char *,
c3b7b696 1559 struct dwarf2_cu *, struct partial_symtab *,
527f3840 1560 CORE_ADDR, int decode_mapping);
c906108c 1561
4d663531 1562static void dwarf2_start_subfile (const char *, const char *);
c906108c 1563
43f3e411
DE
1564static struct compunit_symtab *dwarf2_start_symtab (struct dwarf2_cu *,
1565 const char *, const char *,
1566 CORE_ADDR);
f4dc4d17 1567
a14ed312 1568static struct symbol *new_symbol (struct die_info *, struct type *,
5e2db402 1569 struct dwarf2_cu *, struct symbol * = NULL);
34eaf542 1570
ff39bb5e 1571static void dwarf2_const_value (const struct attribute *, struct symbol *,
e7c27a73 1572 struct dwarf2_cu *);
c906108c 1573
ff39bb5e 1574static void dwarf2_const_value_attr (const struct attribute *attr,
98bfdba5
PA
1575 struct type *type,
1576 const char *name,
1577 struct obstack *obstack,
12df843f 1578 struct dwarf2_cu *cu, LONGEST *value,
d521ce57 1579 const gdb_byte **bytes,
98bfdba5 1580 struct dwarf2_locexpr_baton **baton);
2df3850c 1581
e7c27a73 1582static struct type *die_type (struct die_info *, struct dwarf2_cu *);
c906108c 1583
b4ba55a1
JB
1584static int need_gnat_info (struct dwarf2_cu *);
1585
3e43a32a
MS
1586static struct type *die_descriptive_type (struct die_info *,
1587 struct dwarf2_cu *);
b4ba55a1
JB
1588
1589static void set_descriptive_type (struct type *, struct die_info *,
1590 struct dwarf2_cu *);
1591
e7c27a73
DJ
1592static struct type *die_containing_type (struct die_info *,
1593 struct dwarf2_cu *);
c906108c 1594
ff39bb5e 1595static struct type *lookup_die_type (struct die_info *, const struct attribute *,
673bfd45 1596 struct dwarf2_cu *);
c906108c 1597
f792889a 1598static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
c906108c 1599
673bfd45
DE
1600static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1601
0d5cff50 1602static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
63d06c5c 1603
6e70227d 1604static char *typename_concat (struct obstack *obs, const char *prefix,
f55ee35c
JK
1605 const char *suffix, int physname,
1606 struct dwarf2_cu *cu);
63d06c5c 1607
e7c27a73 1608static void read_file_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1609
348e048f
DE
1610static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1611
e7c27a73 1612static void read_func_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1613
e7c27a73 1614static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1615
96408a79
SA
1616static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1617
71a3c369
TT
1618static void read_variable (struct die_info *die, struct dwarf2_cu *cu);
1619
ff013f42
JK
1620static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
1621 struct dwarf2_cu *, struct partial_symtab *);
1622
3a2b436a 1623/* How dwarf2_get_pc_bounds constructed its *LOWPC and *HIGHPC return
e385593e 1624 values. Keep the items ordered with increasing constraints compliance. */
3a2b436a
JK
1625enum pc_bounds_kind
1626{
e385593e 1627 /* No attribute DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges was found. */
3a2b436a
JK
1628 PC_BOUNDS_NOT_PRESENT,
1629
e385593e
JK
1630 /* Some of the attributes DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges
1631 were present but they do not form a valid range of PC addresses. */
1632 PC_BOUNDS_INVALID,
1633
3a2b436a
JK
1634 /* Discontiguous range was found - that is DW_AT_ranges was found. */
1635 PC_BOUNDS_RANGES,
1636
1637 /* Contiguous range was found - DW_AT_low_pc and DW_AT_high_pc were found. */
1638 PC_BOUNDS_HIGH_LOW,
1639};
1640
1641static enum pc_bounds_kind dwarf2_get_pc_bounds (struct die_info *,
1642 CORE_ADDR *, CORE_ADDR *,
1643 struct dwarf2_cu *,
1644 struct partial_symtab *);
c906108c 1645
fae299cd
DC
1646static void get_scope_pc_bounds (struct die_info *,
1647 CORE_ADDR *, CORE_ADDR *,
1648 struct dwarf2_cu *);
1649
801e3a5b
JB
1650static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1651 CORE_ADDR, struct dwarf2_cu *);
1652
a14ed312 1653static void dwarf2_add_field (struct field_info *, struct die_info *,
e7c27a73 1654 struct dwarf2_cu *);
c906108c 1655
a14ed312 1656static void dwarf2_attach_fields_to_type (struct field_info *,
e7c27a73 1657 struct type *, struct dwarf2_cu *);
c906108c 1658
a14ed312 1659static void dwarf2_add_member_fn (struct field_info *,
e26fb1d7 1660 struct die_info *, struct type *,
e7c27a73 1661 struct dwarf2_cu *);
c906108c 1662
a14ed312 1663static void dwarf2_attach_fn_fields_to_type (struct field_info *,
3e43a32a
MS
1664 struct type *,
1665 struct dwarf2_cu *);
c906108c 1666
134d01f1 1667static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1668
e7c27a73 1669static void read_common_block (struct die_info *, struct dwarf2_cu *);
c906108c 1670
e7c27a73 1671static void read_namespace (struct die_info *die, struct dwarf2_cu *);
d9fa45fe 1672
5d7cb8df
JK
1673static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1674
22cee43f
PMR
1675static struct using_direct **using_directives (enum language);
1676
27aa8d6a
SW
1677static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1678
74921315
KS
1679static int read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu);
1680
f55ee35c
JK
1681static struct type *read_module_type (struct die_info *die,
1682 struct dwarf2_cu *cu);
1683
38d518c9 1684static const char *namespace_name (struct die_info *die,
e142c38c 1685 int *is_anonymous, struct dwarf2_cu *);
38d518c9 1686
134d01f1 1687static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1688
e7c27a73 1689static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *);
c906108c 1690
6e70227d 1691static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
7ca2d3a3
DL
1692 struct dwarf2_cu *);
1693
bf6af496 1694static struct die_info *read_die_and_siblings_1
d521ce57 1695 (const struct die_reader_specs *, const gdb_byte *, const gdb_byte **,
bf6af496 1696 struct die_info *);
639d11d3 1697
dee91e82 1698static struct die_info *read_die_and_siblings (const struct die_reader_specs *,
d521ce57
TT
1699 const gdb_byte *info_ptr,
1700 const gdb_byte **new_info_ptr,
639d11d3
DC
1701 struct die_info *parent);
1702
d521ce57
TT
1703static const gdb_byte *read_full_die_1 (const struct die_reader_specs *,
1704 struct die_info **, const gdb_byte *,
1705 int *, int);
3019eac3 1706
d521ce57
TT
1707static const gdb_byte *read_full_die (const struct die_reader_specs *,
1708 struct die_info **, const gdb_byte *,
1709 int *);
93311388 1710
e7c27a73 1711static void process_die (struct die_info *, struct dwarf2_cu *);
c906108c 1712
15d034d0
TT
1713static const char *dwarf2_canonicalize_name (const char *, struct dwarf2_cu *,
1714 struct obstack *);
71c25dea 1715
15d034d0 1716static const char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
9219021c 1717
15d034d0 1718static const char *dwarf2_full_name (const char *name,
98bfdba5
PA
1719 struct die_info *die,
1720 struct dwarf2_cu *cu);
1721
ca69b9e6
DE
1722static const char *dwarf2_physname (const char *name, struct die_info *die,
1723 struct dwarf2_cu *cu);
1724
e142c38c 1725static struct die_info *dwarf2_extension (struct die_info *die,
f2f0e013 1726 struct dwarf2_cu **);
9219021c 1727
f39c6ffd 1728static const char *dwarf_tag_name (unsigned int);
c906108c 1729
f39c6ffd 1730static const char *dwarf_attr_name (unsigned int);
c906108c 1731
f39c6ffd 1732static const char *dwarf_form_name (unsigned int);
c906108c 1733
a121b7c1 1734static const char *dwarf_bool_name (unsigned int);
c906108c 1735
f39c6ffd 1736static const char *dwarf_type_encoding_name (unsigned int);
c906108c 1737
f9aca02d 1738static struct die_info *sibling_die (struct die_info *);
c906108c 1739
d97bc12b
DE
1740static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1741
1742static void dump_die_for_error (struct die_info *);
1743
1744static void dump_die_1 (struct ui_file *, int level, int max_level,
1745 struct die_info *);
c906108c 1746
d97bc12b 1747/*static*/ void dump_die (struct die_info *, int max_level);
c906108c 1748
51545339 1749static void store_in_ref_table (struct die_info *,
10b3939b 1750 struct dwarf2_cu *);
c906108c 1751
ff39bb5e 1752static sect_offset dwarf2_get_ref_die_offset (const struct attribute *);
c906108c 1753
ff39bb5e 1754static LONGEST dwarf2_get_attr_constant_value (const struct attribute *, int);
a02abb62 1755
348e048f 1756static struct die_info *follow_die_ref_or_sig (struct die_info *,
ff39bb5e 1757 const struct attribute *,
348e048f
DE
1758 struct dwarf2_cu **);
1759
10b3939b 1760static struct die_info *follow_die_ref (struct die_info *,
ff39bb5e 1761 const struct attribute *,
f2f0e013 1762 struct dwarf2_cu **);
c906108c 1763
348e048f 1764static struct die_info *follow_die_sig (struct die_info *,
ff39bb5e 1765 const struct attribute *,
348e048f
DE
1766 struct dwarf2_cu **);
1767
ac9ec31b
DE
1768static struct type *get_signatured_type (struct die_info *, ULONGEST,
1769 struct dwarf2_cu *);
1770
1771static struct type *get_DW_AT_signature_type (struct die_info *,
ff39bb5e 1772 const struct attribute *,
ac9ec31b
DE
1773 struct dwarf2_cu *);
1774
e5fe5e75 1775static void load_full_type_unit (struct dwarf2_per_cu_data *per_cu);
348e048f 1776
52dc124a 1777static void read_signatured_type (struct signatured_type *);
348e048f 1778
63e43d3a
PMR
1779static int attr_to_dynamic_prop (const struct attribute *attr,
1780 struct die_info *die, struct dwarf2_cu *cu,
1781 struct dynamic_prop *prop);
1782
c906108c
SS
1783/* memory allocation interface */
1784
7b5a2f43 1785static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
c906108c 1786
b60c80d6 1787static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
c906108c 1788
43f3e411 1789static void dwarf_decode_macros (struct dwarf2_cu *, unsigned int, int);
2e276125 1790
6e5a29e1 1791static int attr_form_is_block (const struct attribute *);
8e19ed76 1792
6e5a29e1 1793static int attr_form_is_section_offset (const struct attribute *);
3690dd37 1794
6e5a29e1 1795static int attr_form_is_constant (const struct attribute *);
3690dd37 1796
6e5a29e1 1797static int attr_form_is_ref (const struct attribute *);
7771576e 1798
8cf6f0b1
TT
1799static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1800 struct dwarf2_loclist_baton *baton,
ff39bb5e 1801 const struct attribute *attr);
8cf6f0b1 1802
ff39bb5e 1803static void dwarf2_symbol_mark_computed (const struct attribute *attr,
93e7bd98 1804 struct symbol *sym,
f1e6e072
TT
1805 struct dwarf2_cu *cu,
1806 int is_block);
4c2df51b 1807
d521ce57
TT
1808static const gdb_byte *skip_one_die (const struct die_reader_specs *reader,
1809 const gdb_byte *info_ptr,
1810 struct abbrev_info *abbrev);
4bb7a0a7 1811
72bf9492
DJ
1812static hashval_t partial_die_hash (const void *item);
1813
1814static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1815
ae038cb0 1816static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
ed2dc618
SM
1817 (sect_offset sect_off, unsigned int offset_in_dwz,
1818 struct dwarf2_per_objfile *dwarf2_per_objfile);
ae038cb0 1819
9816fde3 1820static void prepare_one_comp_unit (struct dwarf2_cu *cu,
95554aad
TT
1821 struct die_info *comp_unit_die,
1822 enum language pretend_language);
93311388 1823
ed2dc618 1824static void age_cached_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
ae038cb0 1825
dee91e82 1826static void free_one_cached_comp_unit (struct dwarf2_per_cu_data *);
ae038cb0 1827
f792889a
DJ
1828static struct type *set_die_type (struct die_info *, struct type *,
1829 struct dwarf2_cu *);
1c379e20 1830
ed2dc618 1831static void create_all_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
ae038cb0 1832
ed2dc618 1833static int create_all_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
1fd400ff 1834
95554aad
TT
1835static void load_full_comp_unit (struct dwarf2_per_cu_data *,
1836 enum language);
10b3939b 1837
95554aad
TT
1838static void process_full_comp_unit (struct dwarf2_per_cu_data *,
1839 enum language);
10b3939b 1840
f4dc4d17
DE
1841static void process_full_type_unit (struct dwarf2_per_cu_data *,
1842 enum language);
1843
10b3939b
DJ
1844static void dwarf2_add_dependence (struct dwarf2_cu *,
1845 struct dwarf2_per_cu_data *);
1846
ae038cb0
DJ
1847static void dwarf2_mark (struct dwarf2_cu *);
1848
1849static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
1850
b64f50a1 1851static struct type *get_die_type_at_offset (sect_offset,
ac9ec31b 1852 struct dwarf2_per_cu_data *);
673bfd45 1853
f792889a 1854static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
72019c9c 1855
95554aad
TT
1856static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
1857 enum language pretend_language);
1858
ed2dc618 1859static void process_queue (struct dwarf2_per_objfile *dwarf2_per_objfile);
9291a0cd 1860
b303c6f6
AB
1861/* Class, the destructor of which frees all allocated queue entries. This
1862 will only have work to do if an error was thrown while processing the
1863 dwarf. If no error was thrown then the queue entries should have all
1864 been processed, and freed, as we went along. */
1865
1866class dwarf2_queue_guard
1867{
1868public:
1869 dwarf2_queue_guard () = default;
1870
1871 /* Free any entries remaining on the queue. There should only be
1872 entries left if we hit an error while processing the dwarf. */
1873 ~dwarf2_queue_guard ()
1874 {
1875 struct dwarf2_queue_item *item, *last;
1876
1877 item = dwarf2_queue;
1878 while (item)
1879 {
1880 /* Anything still marked queued is likely to be in an
1881 inconsistent state, so discard it. */
1882 if (item->per_cu->queued)
1883 {
1884 if (item->per_cu->cu != NULL)
1885 free_one_cached_comp_unit (item->per_cu);
1886 item->per_cu->queued = 0;
1887 }
1888
1889 last = item;
1890 item = item->next;
1891 xfree (last);
1892 }
1893
1894 dwarf2_queue = dwarf2_queue_tail = NULL;
1895 }
1896};
1897
d721ba37
PA
1898/* The return type of find_file_and_directory. Note, the enclosed
1899 string pointers are only valid while this object is valid. */
1900
1901struct file_and_directory
1902{
1903 /* The filename. This is never NULL. */
1904 const char *name;
1905
1906 /* The compilation directory. NULL if not known. If we needed to
1907 compute a new string, this points to COMP_DIR_STORAGE, otherwise,
1908 points directly to the DW_AT_comp_dir string attribute owned by
1909 the obstack that owns the DIE. */
1910 const char *comp_dir;
1911
1912 /* If we needed to build a new string for comp_dir, this is what
1913 owns the storage. */
1914 std::string comp_dir_storage;
1915};
1916
1917static file_and_directory find_file_and_directory (struct die_info *die,
1918 struct dwarf2_cu *cu);
9291a0cd
TT
1919
1920static char *file_full_name (int file, struct line_header *lh,
1921 const char *comp_dir);
1922
43988095
JK
1923/* Expected enum dwarf_unit_type for read_comp_unit_head. */
1924enum class rcuh_kind { COMPILE, TYPE };
1925
d521ce57 1926static const gdb_byte *read_and_check_comp_unit_head
ed2dc618
SM
1927 (struct dwarf2_per_objfile* dwarf2_per_objfile,
1928 struct comp_unit_head *header,
36586728 1929 struct dwarf2_section_info *section,
d521ce57 1930 struct dwarf2_section_info *abbrev_section, const gdb_byte *info_ptr,
43988095 1931 rcuh_kind section_kind);
36586728 1932
fd820528 1933static void init_cutu_and_read_dies
f4dc4d17
DE
1934 (struct dwarf2_per_cu_data *this_cu, struct abbrev_table *abbrev_table,
1935 int use_existing_cu, int keep,
3019eac3
DE
1936 die_reader_func_ftype *die_reader_func, void *data);
1937
dee91e82
DE
1938static void init_cutu_and_read_dies_simple
1939 (struct dwarf2_per_cu_data *this_cu,
1940 die_reader_func_ftype *die_reader_func, void *data);
9291a0cd 1941
673bfd45 1942static htab_t allocate_signatured_type_table (struct objfile *objfile);
1fd400ff 1943
3019eac3
DE
1944static htab_t allocate_dwo_unit_table (struct objfile *objfile);
1945
57d63ce2 1946static struct dwo_unit *lookup_dwo_unit_in_dwp
ed2dc618
SM
1947 (struct dwarf2_per_objfile *dwarf2_per_objfile,
1948 struct dwp_file *dwp_file, const char *comp_dir,
57d63ce2 1949 ULONGEST signature, int is_debug_types);
a2ce51a0 1950
ed2dc618
SM
1951static struct dwp_file *get_dwp_file
1952 (struct dwarf2_per_objfile *dwarf2_per_objfile);
a2ce51a0 1953
3019eac3 1954static struct dwo_unit *lookup_dwo_comp_unit
a1855c1d 1955 (struct dwarf2_per_cu_data *, const char *, const char *, ULONGEST);
3019eac3
DE
1956
1957static struct dwo_unit *lookup_dwo_type_unit
a1855c1d 1958 (struct signatured_type *, const char *, const char *);
3019eac3 1959
89e63ee4
DE
1960static void queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *);
1961
263db9a1 1962static void free_dwo_file (struct dwo_file *);
3019eac3 1963
263db9a1
TT
1964/* A unique_ptr helper to free a dwo_file. */
1965
1966struct dwo_file_deleter
ed2dc618 1967{
263db9a1
TT
1968 void operator() (struct dwo_file *df) const
1969 {
1970 free_dwo_file (df);
1971 }
ed2dc618
SM
1972};
1973
263db9a1
TT
1974/* A unique pointer to a dwo_file. */
1975
1976typedef std::unique_ptr<struct dwo_file, dwo_file_deleter> dwo_file_up;
1977
ed2dc618 1978static void process_cu_includes (struct dwarf2_per_objfile *dwarf2_per_objfile);
95554aad 1979
1b80a9fa 1980static void check_producer (struct dwarf2_cu *cu);
527f3840
JK
1981
1982static void free_line_header_voidp (void *arg);
4390d890
DE
1983\f
1984/* Various complaints about symbol reading that don't abort the process. */
1985
1986static void
1987dwarf2_statement_list_fits_in_line_number_section_complaint (void)
1988{
1989 complaint (&symfile_complaints,
1990 _("statement list doesn't fit in .debug_line section"));
1991}
1992
1993static void
1994dwarf2_debug_line_missing_file_complaint (void)
1995{
1996 complaint (&symfile_complaints,
1997 _(".debug_line section has line data without a file"));
1998}
1999
2000static void
2001dwarf2_debug_line_missing_end_sequence_complaint (void)
2002{
2003 complaint (&symfile_complaints,
2004 _(".debug_line section has line "
2005 "program sequence without an end"));
2006}
2007
2008static void
2009dwarf2_complex_location_expr_complaint (void)
2010{
2011 complaint (&symfile_complaints, _("location expression too complex"));
2012}
2013
2014static void
2015dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
2016 int arg3)
2017{
2018 complaint (&symfile_complaints,
2019 _("const value length mismatch for '%s', got %d, expected %d"),
2020 arg1, arg2, arg3);
2021}
2022
2023static void
2024dwarf2_section_buffer_overflow_complaint (struct dwarf2_section_info *section)
2025{
2026 complaint (&symfile_complaints,
2027 _("debug info runs off end of %s section"
2028 " [in module %s]"),
a32a8923
DE
2029 get_section_name (section),
2030 get_section_file_name (section));
4390d890 2031}
1b80a9fa 2032
4390d890
DE
2033static void
2034dwarf2_macro_malformed_definition_complaint (const char *arg1)
2035{
2036 complaint (&symfile_complaints,
2037 _("macro debug info contains a "
2038 "malformed macro definition:\n`%s'"),
2039 arg1);
2040}
2041
2042static void
2043dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
2044{
2045 complaint (&symfile_complaints,
2046 _("invalid attribute class or form for '%s' in '%s'"),
2047 arg1, arg2);
2048}
527f3840
JK
2049
2050/* Hash function for line_header_hash. */
2051
2052static hashval_t
2053line_header_hash (const struct line_header *ofs)
2054{
9c541725 2055 return to_underlying (ofs->sect_off) ^ ofs->offset_in_dwz;
527f3840
JK
2056}
2057
2058/* Hash function for htab_create_alloc_ex for line_header_hash. */
2059
2060static hashval_t
2061line_header_hash_voidp (const void *item)
2062{
9a3c8263 2063 const struct line_header *ofs = (const struct line_header *) item;
527f3840
JK
2064
2065 return line_header_hash (ofs);
2066}
2067
2068/* Equality function for line_header_hash. */
2069
2070static int
2071line_header_eq_voidp (const void *item_lhs, const void *item_rhs)
2072{
9a3c8263
SM
2073 const struct line_header *ofs_lhs = (const struct line_header *) item_lhs;
2074 const struct line_header *ofs_rhs = (const struct line_header *) item_rhs;
527f3840 2075
9c541725 2076 return (ofs_lhs->sect_off == ofs_rhs->sect_off
527f3840
JK
2077 && ofs_lhs->offset_in_dwz == ofs_rhs->offset_in_dwz);
2078}
2079
4390d890 2080\f
9291a0cd 2081
31aa7e4e
JB
2082/* Read the given attribute value as an address, taking the attribute's
2083 form into account. */
2084
2085static CORE_ADDR
2086attr_value_as_address (struct attribute *attr)
2087{
2088 CORE_ADDR addr;
2089
2090 if (attr->form != DW_FORM_addr && attr->form != DW_FORM_GNU_addr_index)
2091 {
2092 /* Aside from a few clearly defined exceptions, attributes that
2093 contain an address must always be in DW_FORM_addr form.
2094 Unfortunately, some compilers happen to be violating this
2095 requirement by encoding addresses using other forms, such
2096 as DW_FORM_data4 for example. For those broken compilers,
2097 we try to do our best, without any guarantee of success,
2098 to interpret the address correctly. It would also be nice
2099 to generate a complaint, but that would require us to maintain
2100 a list of legitimate cases where a non-address form is allowed,
2101 as well as update callers to pass in at least the CU's DWARF
2102 version. This is more overhead than what we're willing to
2103 expand for a pretty rare case. */
2104 addr = DW_UNSND (attr);
2105 }
2106 else
2107 addr = DW_ADDR (attr);
2108
2109 return addr;
2110}
2111
330cdd98
PA
2112/* See declaration. */
2113
2114dwarf2_per_objfile::dwarf2_per_objfile (struct objfile *objfile_,
2115 const dwarf2_debug_sections *names)
2116 : objfile (objfile_)
2117{
2118 if (names == NULL)
2119 names = &dwarf2_elf_names;
2120
2121 bfd *obfd = objfile->obfd;
2122
2123 for (asection *sec = obfd->sections; sec != NULL; sec = sec->next)
2124 locate_sections (obfd, sec, *names);
2125}
2126
fc8e7e75
SM
2127static void free_dwo_files (htab_t dwo_files, struct objfile *objfile);
2128
330cdd98
PA
2129dwarf2_per_objfile::~dwarf2_per_objfile ()
2130{
2131 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
2132 free_cached_comp_units ();
2133
2134 if (quick_file_names_table)
2135 htab_delete (quick_file_names_table);
2136
2137 if (line_header_hash)
2138 htab_delete (line_header_hash);
2139
fc8e7e75
SM
2140 for (int ix = 0; ix < n_comp_units; ++ix)
2141 VEC_free (dwarf2_per_cu_ptr, all_comp_units[ix]->imported_symtabs);
2142
2143 for (int ix = 0; ix < n_type_units; ++ix)
2144 VEC_free (dwarf2_per_cu_ptr,
2145 all_type_units[ix]->per_cu.imported_symtabs);
2146 xfree (all_type_units);
2147
2148 VEC_free (dwarf2_section_info_def, types);
2149
2150 if (dwo_files != NULL)
2151 free_dwo_files (dwo_files, objfile);
2152 if (dwp_file != NULL)
2153 gdb_bfd_unref (dwp_file->dbfd);
2154
2155 if (dwz_file != NULL && dwz_file->dwz_bfd)
2156 gdb_bfd_unref (dwz_file->dwz_bfd);
2157
2158 if (index_table != NULL)
2159 index_table->~mapped_index ();
2160
330cdd98
PA
2161 /* Everything else should be on the objfile obstack. */
2162}
2163
2164/* See declaration. */
2165
2166void
2167dwarf2_per_objfile::free_cached_comp_units ()
2168{
2169 dwarf2_per_cu_data *per_cu = read_in_chain;
2170 dwarf2_per_cu_data **last_chain = &read_in_chain;
2171 while (per_cu != NULL)
2172 {
2173 dwarf2_per_cu_data *next_cu = per_cu->cu->read_in_chain;
2174
fcd3b13d 2175 delete per_cu->cu;
330cdd98
PA
2176 *last_chain = next_cu;
2177 per_cu = next_cu;
2178 }
2179}
2180
11ed8cad
TT
2181/* A helper class that calls free_cached_comp_units on
2182 destruction. */
2183
2184class free_cached_comp_units
2185{
2186public:
2187
2188 explicit free_cached_comp_units (dwarf2_per_objfile *per_objfile)
2189 : m_per_objfile (per_objfile)
2190 {
2191 }
2192
2193 ~free_cached_comp_units ()
2194 {
2195 m_per_objfile->free_cached_comp_units ();
2196 }
2197
2198 DISABLE_COPY_AND_ASSIGN (free_cached_comp_units);
2199
2200private:
2201
2202 dwarf2_per_objfile *m_per_objfile;
2203};
2204
c906108c 2205/* Try to locate the sections we need for DWARF 2 debugging
251d32d9
TG
2206 information and return true if we have enough to do something.
2207 NAMES points to the dwarf2 section names, or is NULL if the standard
2208 ELF names are used. */
c906108c
SS
2209
2210int
251d32d9
TG
2211dwarf2_has_info (struct objfile *objfile,
2212 const struct dwarf2_debug_sections *names)
c906108c 2213{
97cbe998
SDJ
2214 if (objfile->flags & OBJF_READNEVER)
2215 return 0;
2216
ed2dc618
SM
2217 struct dwarf2_per_objfile *dwarf2_per_objfile
2218 = get_dwarf2_per_objfile (objfile);
2219
2220 if (dwarf2_per_objfile == NULL)
be391dca
TT
2221 {
2222 /* Initialize per-objfile state. */
fd90ace4
YQ
2223 dwarf2_per_objfile
2224 = new (&objfile->objfile_obstack) struct dwarf2_per_objfile (objfile,
2225 names);
ed2dc618 2226 set_dwarf2_per_objfile (objfile, dwarf2_per_objfile);
be391dca 2227 }
73869dc2 2228 return (!dwarf2_per_objfile->info.is_virtual
049412e3 2229 && dwarf2_per_objfile->info.s.section != NULL
73869dc2 2230 && !dwarf2_per_objfile->abbrev.is_virtual
049412e3 2231 && dwarf2_per_objfile->abbrev.s.section != NULL);
73869dc2
DE
2232}
2233
2234/* Return the containing section of virtual section SECTION. */
2235
2236static struct dwarf2_section_info *
2237get_containing_section (const struct dwarf2_section_info *section)
2238{
2239 gdb_assert (section->is_virtual);
2240 return section->s.containing_section;
c906108c
SS
2241}
2242
a32a8923
DE
2243/* Return the bfd owner of SECTION. */
2244
2245static struct bfd *
2246get_section_bfd_owner (const struct dwarf2_section_info *section)
2247{
73869dc2
DE
2248 if (section->is_virtual)
2249 {
2250 section = get_containing_section (section);
2251 gdb_assert (!section->is_virtual);
2252 }
049412e3 2253 return section->s.section->owner;
a32a8923
DE
2254}
2255
2256/* Return the bfd section of SECTION.
2257 Returns NULL if the section is not present. */
2258
2259static asection *
2260get_section_bfd_section (const struct dwarf2_section_info *section)
2261{
73869dc2
DE
2262 if (section->is_virtual)
2263 {
2264 section = get_containing_section (section);
2265 gdb_assert (!section->is_virtual);
2266 }
049412e3 2267 return section->s.section;
a32a8923
DE
2268}
2269
2270/* Return the name of SECTION. */
2271
2272static const char *
2273get_section_name (const struct dwarf2_section_info *section)
2274{
2275 asection *sectp = get_section_bfd_section (section);
2276
2277 gdb_assert (sectp != NULL);
2278 return bfd_section_name (get_section_bfd_owner (section), sectp);
2279}
2280
2281/* Return the name of the file SECTION is in. */
2282
2283static const char *
2284get_section_file_name (const struct dwarf2_section_info *section)
2285{
2286 bfd *abfd = get_section_bfd_owner (section);
2287
2288 return bfd_get_filename (abfd);
2289}
2290
2291/* Return the id of SECTION.
2292 Returns 0 if SECTION doesn't exist. */
2293
2294static int
2295get_section_id (const struct dwarf2_section_info *section)
2296{
2297 asection *sectp = get_section_bfd_section (section);
2298
2299 if (sectp == NULL)
2300 return 0;
2301 return sectp->id;
2302}
2303
2304/* Return the flags of SECTION.
73869dc2 2305 SECTION (or containing section if this is a virtual section) must exist. */
a32a8923
DE
2306
2307static int
2308get_section_flags (const struct dwarf2_section_info *section)
2309{
2310 asection *sectp = get_section_bfd_section (section);
2311
2312 gdb_assert (sectp != NULL);
2313 return bfd_get_section_flags (sectp->owner, sectp);
2314}
2315
251d32d9
TG
2316/* When loading sections, we look either for uncompressed section or for
2317 compressed section names. */
233a11ab
CS
2318
2319static int
251d32d9
TG
2320section_is_p (const char *section_name,
2321 const struct dwarf2_section_names *names)
233a11ab 2322{
251d32d9
TG
2323 if (names->normal != NULL
2324 && strcmp (section_name, names->normal) == 0)
2325 return 1;
2326 if (names->compressed != NULL
2327 && strcmp (section_name, names->compressed) == 0)
2328 return 1;
2329 return 0;
233a11ab
CS
2330}
2331
330cdd98 2332/* See declaration. */
c906108c 2333
330cdd98
PA
2334void
2335dwarf2_per_objfile::locate_sections (bfd *abfd, asection *sectp,
2336 const dwarf2_debug_sections &names)
c906108c 2337{
dc7650b8 2338 flagword aflag = bfd_get_section_flags (abfd, sectp);
251d32d9 2339
dc7650b8
JK
2340 if ((aflag & SEC_HAS_CONTENTS) == 0)
2341 {
2342 }
330cdd98 2343 else if (section_is_p (sectp->name, &names.info))
c906108c 2344 {
330cdd98
PA
2345 this->info.s.section = sectp;
2346 this->info.size = bfd_get_section_size (sectp);
c906108c 2347 }
330cdd98 2348 else if (section_is_p (sectp->name, &names.abbrev))
c906108c 2349 {
330cdd98
PA
2350 this->abbrev.s.section = sectp;
2351 this->abbrev.size = bfd_get_section_size (sectp);
c906108c 2352 }
330cdd98 2353 else if (section_is_p (sectp->name, &names.line))
c906108c 2354 {
330cdd98
PA
2355 this->line.s.section = sectp;
2356 this->line.size = bfd_get_section_size (sectp);
c906108c 2357 }
330cdd98 2358 else if (section_is_p (sectp->name, &names.loc))
c906108c 2359 {
330cdd98
PA
2360 this->loc.s.section = sectp;
2361 this->loc.size = bfd_get_section_size (sectp);
c906108c 2362 }
330cdd98 2363 else if (section_is_p (sectp->name, &names.loclists))
43988095 2364 {
330cdd98
PA
2365 this->loclists.s.section = sectp;
2366 this->loclists.size = bfd_get_section_size (sectp);
43988095 2367 }
330cdd98 2368 else if (section_is_p (sectp->name, &names.macinfo))
c906108c 2369 {
330cdd98
PA
2370 this->macinfo.s.section = sectp;
2371 this->macinfo.size = bfd_get_section_size (sectp);
c906108c 2372 }
330cdd98 2373 else if (section_is_p (sectp->name, &names.macro))
cf2c3c16 2374 {
330cdd98
PA
2375 this->macro.s.section = sectp;
2376 this->macro.size = bfd_get_section_size (sectp);
cf2c3c16 2377 }
330cdd98 2378 else if (section_is_p (sectp->name, &names.str))
c906108c 2379 {
330cdd98
PA
2380 this->str.s.section = sectp;
2381 this->str.size = bfd_get_section_size (sectp);
c906108c 2382 }
330cdd98 2383 else if (section_is_p (sectp->name, &names.line_str))
43988095 2384 {
330cdd98
PA
2385 this->line_str.s.section = sectp;
2386 this->line_str.size = bfd_get_section_size (sectp);
43988095 2387 }
330cdd98 2388 else if (section_is_p (sectp->name, &names.addr))
3019eac3 2389 {
330cdd98
PA
2390 this->addr.s.section = sectp;
2391 this->addr.size = bfd_get_section_size (sectp);
3019eac3 2392 }
330cdd98 2393 else if (section_is_p (sectp->name, &names.frame))
b6af0555 2394 {
330cdd98
PA
2395 this->frame.s.section = sectp;
2396 this->frame.size = bfd_get_section_size (sectp);
b6af0555 2397 }
330cdd98 2398 else if (section_is_p (sectp->name, &names.eh_frame))
b6af0555 2399 {
330cdd98
PA
2400 this->eh_frame.s.section = sectp;
2401 this->eh_frame.size = bfd_get_section_size (sectp);
b6af0555 2402 }
330cdd98 2403 else if (section_is_p (sectp->name, &names.ranges))
af34e669 2404 {
330cdd98
PA
2405 this->ranges.s.section = sectp;
2406 this->ranges.size = bfd_get_section_size (sectp);
af34e669 2407 }
330cdd98 2408 else if (section_is_p (sectp->name, &names.rnglists))
43988095 2409 {
330cdd98
PA
2410 this->rnglists.s.section = sectp;
2411 this->rnglists.size = bfd_get_section_size (sectp);
43988095 2412 }
330cdd98 2413 else if (section_is_p (sectp->name, &names.types))
348e048f 2414 {
8b70b953
TT
2415 struct dwarf2_section_info type_section;
2416
2417 memset (&type_section, 0, sizeof (type_section));
049412e3 2418 type_section.s.section = sectp;
8b70b953
TT
2419 type_section.size = bfd_get_section_size (sectp);
2420
330cdd98 2421 VEC_safe_push (dwarf2_section_info_def, this->types,
8b70b953 2422 &type_section);
348e048f 2423 }
330cdd98 2424 else if (section_is_p (sectp->name, &names.gdb_index))
9291a0cd 2425 {
330cdd98
PA
2426 this->gdb_index.s.section = sectp;
2427 this->gdb_index.size = bfd_get_section_size (sectp);
9291a0cd 2428 }
927aa2e7
JK
2429 else if (section_is_p (sectp->name, &names.debug_names))
2430 {
2431 this->debug_names.s.section = sectp;
2432 this->debug_names.size = bfd_get_section_size (sectp);
2433 }
2434 else if (section_is_p (sectp->name, &names.debug_aranges))
2435 {
2436 this->debug_aranges.s.section = sectp;
2437 this->debug_aranges.size = bfd_get_section_size (sectp);
2438 }
dce234bc 2439
b4e1fd61 2440 if ((bfd_get_section_flags (abfd, sectp) & (SEC_LOAD | SEC_ALLOC))
72dca2f5 2441 && bfd_section_vma (abfd, sectp) == 0)
330cdd98 2442 this->has_section_at_zero = true;
c906108c
SS
2443}
2444
fceca515
DE
2445/* A helper function that decides whether a section is empty,
2446 or not present. */
9e0ac564
TT
2447
2448static int
19ac8c2e 2449dwarf2_section_empty_p (const struct dwarf2_section_info *section)
9e0ac564 2450{
73869dc2
DE
2451 if (section->is_virtual)
2452 return section->size == 0;
049412e3 2453 return section->s.section == NULL || section->size == 0;
9e0ac564
TT
2454}
2455
cd4fb1b2 2456/* See dwarf2read.h. */
c906108c 2457
cd4fb1b2
SM
2458void
2459dwarf2_read_section (struct objfile *objfile, dwarf2_section_info *info)
c906108c 2460{
a32a8923 2461 asection *sectp;
3019eac3 2462 bfd *abfd;
dce234bc 2463 gdb_byte *buf, *retbuf;
c906108c 2464
be391dca
TT
2465 if (info->readin)
2466 return;
dce234bc 2467 info->buffer = NULL;
be391dca 2468 info->readin = 1;
188dd5d6 2469
9e0ac564 2470 if (dwarf2_section_empty_p (info))
dce234bc 2471 return;
c906108c 2472
a32a8923 2473 sectp = get_section_bfd_section (info);
3019eac3 2474
73869dc2
DE
2475 /* If this is a virtual section we need to read in the real one first. */
2476 if (info->is_virtual)
2477 {
2478 struct dwarf2_section_info *containing_section =
2479 get_containing_section (info);
2480
2481 gdb_assert (sectp != NULL);
2482 if ((sectp->flags & SEC_RELOC) != 0)
2483 {
2484 error (_("Dwarf Error: DWP format V2 with relocations is not"
2485 " supported in section %s [in module %s]"),
2486 get_section_name (info), get_section_file_name (info));
2487 }
2488 dwarf2_read_section (objfile, containing_section);
2489 /* Other code should have already caught virtual sections that don't
2490 fit. */
2491 gdb_assert (info->virtual_offset + info->size
2492 <= containing_section->size);
2493 /* If the real section is empty or there was a problem reading the
2494 section we shouldn't get here. */
2495 gdb_assert (containing_section->buffer != NULL);
2496 info->buffer = containing_section->buffer + info->virtual_offset;
2497 return;
2498 }
2499
4bf44c1c
TT
2500 /* If the section has relocations, we must read it ourselves.
2501 Otherwise we attach it to the BFD. */
2502 if ((sectp->flags & SEC_RELOC) == 0)
dce234bc 2503 {
d521ce57 2504 info->buffer = gdb_bfd_map_section (sectp, &info->size);
4bf44c1c 2505 return;
dce234bc 2506 }
dce234bc 2507
224c3ddb 2508 buf = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, info->size);
4bf44c1c 2509 info->buffer = buf;
dce234bc
PP
2510
2511 /* When debugging .o files, we may need to apply relocations; see
2512 http://sourceware.org/ml/gdb-patches/2002-04/msg00136.html .
2513 We never compress sections in .o files, so we only need to
2514 try this when the section is not compressed. */
ac8035ab 2515 retbuf = symfile_relocate_debug_section (objfile, sectp, buf);
dce234bc
PP
2516 if (retbuf != NULL)
2517 {
2518 info->buffer = retbuf;
2519 return;
2520 }
2521
a32a8923
DE
2522 abfd = get_section_bfd_owner (info);
2523 gdb_assert (abfd != NULL);
2524
dce234bc
PP
2525 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
2526 || bfd_bread (buf, info->size, abfd) != info->size)
19ac8c2e
DE
2527 {
2528 error (_("Dwarf Error: Can't read DWARF data"
2529 " in section %s [in module %s]"),
2530 bfd_section_name (abfd, sectp), bfd_get_filename (abfd));
2531 }
dce234bc
PP
2532}
2533
9e0ac564
TT
2534/* A helper function that returns the size of a section in a safe way.
2535 If you are positive that the section has been read before using the
2536 size, then it is safe to refer to the dwarf2_section_info object's
2537 "size" field directly. In other cases, you must call this
2538 function, because for compressed sections the size field is not set
2539 correctly until the section has been read. */
2540
2541static bfd_size_type
2542dwarf2_section_size (struct objfile *objfile,
2543 struct dwarf2_section_info *info)
2544{
2545 if (!info->readin)
2546 dwarf2_read_section (objfile, info);
2547 return info->size;
2548}
2549
dce234bc 2550/* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
0963b4bd 2551 SECTION_NAME. */
af34e669 2552
dce234bc 2553void
3017a003
TG
2554dwarf2_get_section_info (struct objfile *objfile,
2555 enum dwarf2_section_enum sect,
d521ce57 2556 asection **sectp, const gdb_byte **bufp,
dce234bc
PP
2557 bfd_size_type *sizep)
2558{
2559 struct dwarf2_per_objfile *data
9a3c8263
SM
2560 = (struct dwarf2_per_objfile *) objfile_data (objfile,
2561 dwarf2_objfile_data_key);
dce234bc 2562 struct dwarf2_section_info *info;
a3b2a86b
TT
2563
2564 /* We may see an objfile without any DWARF, in which case we just
2565 return nothing. */
2566 if (data == NULL)
2567 {
2568 *sectp = NULL;
2569 *bufp = NULL;
2570 *sizep = 0;
2571 return;
2572 }
3017a003
TG
2573 switch (sect)
2574 {
2575 case DWARF2_DEBUG_FRAME:
2576 info = &data->frame;
2577 break;
2578 case DWARF2_EH_FRAME:
2579 info = &data->eh_frame;
2580 break;
2581 default:
2582 gdb_assert_not_reached ("unexpected section");
2583 }
dce234bc 2584
9e0ac564 2585 dwarf2_read_section (objfile, info);
dce234bc 2586
a32a8923 2587 *sectp = get_section_bfd_section (info);
dce234bc
PP
2588 *bufp = info->buffer;
2589 *sizep = info->size;
2590}
2591
36586728
TT
2592/* A helper function to find the sections for a .dwz file. */
2593
2594static void
2595locate_dwz_sections (bfd *abfd, asection *sectp, void *arg)
2596{
9a3c8263 2597 struct dwz_file *dwz_file = (struct dwz_file *) arg;
36586728
TT
2598
2599 /* Note that we only support the standard ELF names, because .dwz
2600 is ELF-only (at the time of writing). */
2601 if (section_is_p (sectp->name, &dwarf2_elf_names.abbrev))
2602 {
049412e3 2603 dwz_file->abbrev.s.section = sectp;
36586728
TT
2604 dwz_file->abbrev.size = bfd_get_section_size (sectp);
2605 }
2606 else if (section_is_p (sectp->name, &dwarf2_elf_names.info))
2607 {
049412e3 2608 dwz_file->info.s.section = sectp;
36586728
TT
2609 dwz_file->info.size = bfd_get_section_size (sectp);
2610 }
2611 else if (section_is_p (sectp->name, &dwarf2_elf_names.str))
2612 {
049412e3 2613 dwz_file->str.s.section = sectp;
36586728
TT
2614 dwz_file->str.size = bfd_get_section_size (sectp);
2615 }
2616 else if (section_is_p (sectp->name, &dwarf2_elf_names.line))
2617 {
049412e3 2618 dwz_file->line.s.section = sectp;
36586728
TT
2619 dwz_file->line.size = bfd_get_section_size (sectp);
2620 }
2621 else if (section_is_p (sectp->name, &dwarf2_elf_names.macro))
2622 {
049412e3 2623 dwz_file->macro.s.section = sectp;
36586728
TT
2624 dwz_file->macro.size = bfd_get_section_size (sectp);
2625 }
2ec9a5e0
TT
2626 else if (section_is_p (sectp->name, &dwarf2_elf_names.gdb_index))
2627 {
049412e3 2628 dwz_file->gdb_index.s.section = sectp;
2ec9a5e0
TT
2629 dwz_file->gdb_index.size = bfd_get_section_size (sectp);
2630 }
927aa2e7
JK
2631 else if (section_is_p (sectp->name, &dwarf2_elf_names.debug_names))
2632 {
2633 dwz_file->debug_names.s.section = sectp;
2634 dwz_file->debug_names.size = bfd_get_section_size (sectp);
2635 }
36586728
TT
2636}
2637
4db1a1dc
TT
2638/* Open the separate '.dwz' debug file, if needed. Return NULL if
2639 there is no .gnu_debugaltlink section in the file. Error if there
2640 is such a section but the file cannot be found. */
36586728
TT
2641
2642static struct dwz_file *
ed2dc618 2643dwarf2_get_dwz_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
36586728 2644{
36586728
TT
2645 const char *filename;
2646 struct dwz_file *result;
acd13123 2647 bfd_size_type buildid_len_arg;
dc294be5
TT
2648 size_t buildid_len;
2649 bfd_byte *buildid;
36586728
TT
2650
2651 if (dwarf2_per_objfile->dwz_file != NULL)
2652 return dwarf2_per_objfile->dwz_file;
2653
4db1a1dc 2654 bfd_set_error (bfd_error_no_error);
791afaa2
TT
2655 gdb::unique_xmalloc_ptr<char> data
2656 (bfd_get_alt_debug_link_info (dwarf2_per_objfile->objfile->obfd,
2657 &buildid_len_arg, &buildid));
4db1a1dc
TT
2658 if (data == NULL)
2659 {
2660 if (bfd_get_error () == bfd_error_no_error)
2661 return NULL;
2662 error (_("could not read '.gnu_debugaltlink' section: %s"),
2663 bfd_errmsg (bfd_get_error ()));
2664 }
791afaa2
TT
2665
2666 gdb::unique_xmalloc_ptr<bfd_byte> buildid_holder (buildid);
36586728 2667
acd13123
TT
2668 buildid_len = (size_t) buildid_len_arg;
2669
791afaa2 2670 filename = data.get ();
d721ba37
PA
2671
2672 std::string abs_storage;
36586728
TT
2673 if (!IS_ABSOLUTE_PATH (filename))
2674 {
14278e1f
TT
2675 gdb::unique_xmalloc_ptr<char> abs
2676 = gdb_realpath (objfile_name (dwarf2_per_objfile->objfile));
36586728 2677
14278e1f 2678 abs_storage = ldirname (abs.get ()) + SLASH_STRING + filename;
d721ba37 2679 filename = abs_storage.c_str ();
36586728
TT
2680 }
2681
dc294be5
TT
2682 /* First try the file name given in the section. If that doesn't
2683 work, try to use the build-id instead. */
192b62ce 2684 gdb_bfd_ref_ptr dwz_bfd (gdb_bfd_open (filename, gnutarget, -1));
dc294be5 2685 if (dwz_bfd != NULL)
36586728 2686 {
192b62ce
TT
2687 if (!build_id_verify (dwz_bfd.get (), buildid_len, buildid))
2688 dwz_bfd.release ();
36586728
TT
2689 }
2690
dc294be5
TT
2691 if (dwz_bfd == NULL)
2692 dwz_bfd = build_id_to_debug_bfd (buildid_len, buildid);
2693
2694 if (dwz_bfd == NULL)
2695 error (_("could not find '.gnu_debugaltlink' file for %s"),
2696 objfile_name (dwarf2_per_objfile->objfile));
2697
36586728
TT
2698 result = OBSTACK_ZALLOC (&dwarf2_per_objfile->objfile->objfile_obstack,
2699 struct dwz_file);
192b62ce 2700 result->dwz_bfd = dwz_bfd.release ();
36586728 2701
192b62ce 2702 bfd_map_over_sections (result->dwz_bfd, locate_dwz_sections, result);
36586728 2703
192b62ce 2704 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd, result->dwz_bfd);
8d2cc612 2705 dwarf2_per_objfile->dwz_file = result;
36586728
TT
2706 return result;
2707}
9291a0cd 2708\f
7b9f3c50
DE
2709/* DWARF quick_symbols_functions support. */
2710
2711/* TUs can share .debug_line entries, and there can be a lot more TUs than
2712 unique line tables, so we maintain a separate table of all .debug_line
2713 derived entries to support the sharing.
2714 All the quick functions need is the list of file names. We discard the
2715 line_header when we're done and don't need to record it here. */
2716struct quick_file_names
2717{
094b34ac
DE
2718 /* The data used to construct the hash key. */
2719 struct stmt_list_hash hash;
7b9f3c50
DE
2720
2721 /* The number of entries in file_names, real_names. */
2722 unsigned int num_file_names;
2723
2724 /* The file names from the line table, after being run through
2725 file_full_name. */
2726 const char **file_names;
2727
2728 /* The file names from the line table after being run through
2729 gdb_realpath. These are computed lazily. */
2730 const char **real_names;
2731};
2732
2733/* When using the index (and thus not using psymtabs), each CU has an
2734 object of this type. This is used to hold information needed by
2735 the various "quick" methods. */
2736struct dwarf2_per_cu_quick_data
2737{
2738 /* The file table. This can be NULL if there was no file table
2739 or it's currently not read in.
2740 NOTE: This points into dwarf2_per_objfile->quick_file_names_table. */
2741 struct quick_file_names *file_names;
2742
2743 /* The corresponding symbol table. This is NULL if symbols for this
2744 CU have not yet been read. */
43f3e411 2745 struct compunit_symtab *compunit_symtab;
7b9f3c50
DE
2746
2747 /* A temporary mark bit used when iterating over all CUs in
2748 expand_symtabs_matching. */
2749 unsigned int mark : 1;
2750
2751 /* True if we've tried to read the file table and found there isn't one.
2752 There will be no point in trying to read it again next time. */
2753 unsigned int no_file_data : 1;
2754};
2755
094b34ac
DE
2756/* Utility hash function for a stmt_list_hash. */
2757
2758static hashval_t
2759hash_stmt_list_entry (const struct stmt_list_hash *stmt_list_hash)
2760{
2761 hashval_t v = 0;
2762
2763 if (stmt_list_hash->dwo_unit != NULL)
2764 v += (uintptr_t) stmt_list_hash->dwo_unit->dwo_file;
9c541725 2765 v += to_underlying (stmt_list_hash->line_sect_off);
094b34ac
DE
2766 return v;
2767}
2768
2769/* Utility equality function for a stmt_list_hash. */
2770
2771static int
2772eq_stmt_list_entry (const struct stmt_list_hash *lhs,
2773 const struct stmt_list_hash *rhs)
2774{
2775 if ((lhs->dwo_unit != NULL) != (rhs->dwo_unit != NULL))
2776 return 0;
2777 if (lhs->dwo_unit != NULL
2778 && lhs->dwo_unit->dwo_file != rhs->dwo_unit->dwo_file)
2779 return 0;
2780
9c541725 2781 return lhs->line_sect_off == rhs->line_sect_off;
094b34ac
DE
2782}
2783
7b9f3c50
DE
2784/* Hash function for a quick_file_names. */
2785
2786static hashval_t
2787hash_file_name_entry (const void *e)
2788{
9a3c8263
SM
2789 const struct quick_file_names *file_data
2790 = (const struct quick_file_names *) e;
7b9f3c50 2791
094b34ac 2792 return hash_stmt_list_entry (&file_data->hash);
7b9f3c50
DE
2793}
2794
2795/* Equality function for a quick_file_names. */
2796
2797static int
2798eq_file_name_entry (const void *a, const void *b)
2799{
9a3c8263
SM
2800 const struct quick_file_names *ea = (const struct quick_file_names *) a;
2801 const struct quick_file_names *eb = (const struct quick_file_names *) b;
7b9f3c50 2802
094b34ac 2803 return eq_stmt_list_entry (&ea->hash, &eb->hash);
7b9f3c50
DE
2804}
2805
2806/* Delete function for a quick_file_names. */
2807
2808static void
2809delete_file_name_entry (void *e)
2810{
9a3c8263 2811 struct quick_file_names *file_data = (struct quick_file_names *) e;
7b9f3c50
DE
2812 int i;
2813
2814 for (i = 0; i < file_data->num_file_names; ++i)
2815 {
2816 xfree ((void*) file_data->file_names[i]);
2817 if (file_data->real_names)
2818 xfree ((void*) file_data->real_names[i]);
2819 }
2820
2821 /* The space for the struct itself lives on objfile_obstack,
2822 so we don't free it here. */
2823}
2824
2825/* Create a quick_file_names hash table. */
2826
2827static htab_t
2828create_quick_file_names_table (unsigned int nr_initial_entries)
2829{
2830 return htab_create_alloc (nr_initial_entries,
2831 hash_file_name_entry, eq_file_name_entry,
2832 delete_file_name_entry, xcalloc, xfree);
2833}
9291a0cd 2834
918dd910
JK
2835/* Read in PER_CU->CU. This function is unrelated to symtabs, symtab would
2836 have to be created afterwards. You should call age_cached_comp_units after
2837 processing PER_CU->CU. dw2_setup must have been already called. */
2838
2839static void
2840load_cu (struct dwarf2_per_cu_data *per_cu)
2841{
3019eac3 2842 if (per_cu->is_debug_types)
e5fe5e75 2843 load_full_type_unit (per_cu);
918dd910 2844 else
95554aad 2845 load_full_comp_unit (per_cu, language_minimal);
918dd910 2846
cc12ce38
DE
2847 if (per_cu->cu == NULL)
2848 return; /* Dummy CU. */
2dc860c0
DE
2849
2850 dwarf2_find_base_address (per_cu->cu->dies, per_cu->cu);
918dd910
JK
2851}
2852
a0f42c21 2853/* Read in the symbols for PER_CU. */
2fdf6df6 2854
9291a0cd 2855static void
a0f42c21 2856dw2_do_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
9291a0cd 2857{
ed2dc618 2858 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
9291a0cd 2859
f4dc4d17
DE
2860 /* Skip type_unit_groups, reading the type units they contain
2861 is handled elsewhere. */
2862 if (IS_TYPE_UNIT_GROUP (per_cu))
2863 return;
2864
b303c6f6
AB
2865 /* The destructor of dwarf2_queue_guard frees any entries left on
2866 the queue. After this point we're guaranteed to leave this function
2867 with the dwarf queue empty. */
2868 dwarf2_queue_guard q_guard;
9291a0cd 2869
95554aad 2870 if (dwarf2_per_objfile->using_index
43f3e411 2871 ? per_cu->v.quick->compunit_symtab == NULL
95554aad
TT
2872 : (per_cu->v.psymtab == NULL || !per_cu->v.psymtab->readin))
2873 {
2874 queue_comp_unit (per_cu, language_minimal);
2875 load_cu (per_cu);
89e63ee4
DE
2876
2877 /* If we just loaded a CU from a DWO, and we're working with an index
2878 that may badly handle TUs, load all the TUs in that DWO as well.
2879 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
2880 if (!per_cu->is_debug_types
cc12ce38 2881 && per_cu->cu != NULL
89e63ee4
DE
2882 && per_cu->cu->dwo_unit != NULL
2883 && dwarf2_per_objfile->index_table != NULL
2884 && dwarf2_per_objfile->index_table->version <= 7
2885 /* DWP files aren't supported yet. */
ed2dc618 2886 && get_dwp_file (dwarf2_per_objfile) == NULL)
89e63ee4 2887 queue_and_load_all_dwo_tus (per_cu);
95554aad 2888 }
9291a0cd 2889
ed2dc618 2890 process_queue (dwarf2_per_objfile);
9291a0cd
TT
2891
2892 /* Age the cache, releasing compilation units that have not
2893 been used recently. */
ed2dc618 2894 age_cached_comp_units (dwarf2_per_objfile);
9291a0cd
TT
2895}
2896
2897/* Ensure that the symbols for PER_CU have been read in. OBJFILE is
2898 the objfile from which this CU came. Returns the resulting symbol
2899 table. */
2fdf6df6 2900
43f3e411 2901static struct compunit_symtab *
a0f42c21 2902dw2_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
9291a0cd 2903{
ed2dc618
SM
2904 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
2905
95554aad 2906 gdb_assert (dwarf2_per_objfile->using_index);
43f3e411 2907 if (!per_cu->v.quick->compunit_symtab)
9291a0cd 2908 {
11ed8cad 2909 free_cached_comp_units freer (dwarf2_per_objfile);
c83dd867 2910 scoped_restore decrementer = increment_reading_symtab ();
a0f42c21 2911 dw2_do_instantiate_symtab (per_cu);
ed2dc618 2912 process_cu_includes (dwarf2_per_objfile);
9291a0cd 2913 }
f194fefb 2914
43f3e411 2915 return per_cu->v.quick->compunit_symtab;
9291a0cd
TT
2916}
2917
8832e7e3 2918/* Return the CU/TU given its index.
f4dc4d17
DE
2919
2920 This is intended for loops like:
2921
2922 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2923 + dwarf2_per_objfile->n_type_units); ++i)
2924 {
8832e7e3 2925 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
f4dc4d17
DE
2926
2927 ...;
2928 }
2929*/
2fdf6df6 2930
1fd400ff 2931static struct dwarf2_per_cu_data *
ed2dc618
SM
2932dw2_get_cutu (struct dwarf2_per_objfile *dwarf2_per_objfile,
2933 int index)
1fd400ff
TT
2934{
2935 if (index >= dwarf2_per_objfile->n_comp_units)
2936 {
f4dc4d17 2937 index -= dwarf2_per_objfile->n_comp_units;
094b34ac
DE
2938 gdb_assert (index < dwarf2_per_objfile->n_type_units);
2939 return &dwarf2_per_objfile->all_type_units[index]->per_cu;
f4dc4d17
DE
2940 }
2941
2942 return dwarf2_per_objfile->all_comp_units[index];
2943}
2944
8832e7e3
DE
2945/* Return the CU given its index.
2946 This differs from dw2_get_cutu in that it's for when you know INDEX
2947 refers to a CU. */
f4dc4d17
DE
2948
2949static struct dwarf2_per_cu_data *
ed2dc618 2950dw2_get_cu (struct dwarf2_per_objfile *dwarf2_per_objfile, int index)
f4dc4d17 2951{
8832e7e3 2952 gdb_assert (index >= 0 && index < dwarf2_per_objfile->n_comp_units);
f4dc4d17 2953
1fd400ff
TT
2954 return dwarf2_per_objfile->all_comp_units[index];
2955}
2956
4b514bc8
JK
2957/* Return a new dwarf2_per_cu_data allocated on OBJFILE's
2958 objfile_obstack, and constructed with the specified field
2959 values. */
2960
2961static dwarf2_per_cu_data *
ed2dc618 2962create_cu_from_index_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
4b514bc8
JK
2963 struct dwarf2_section_info *section,
2964 int is_dwz,
2965 sect_offset sect_off, ULONGEST length)
2966{
ed2dc618 2967 struct objfile *objfile = dwarf2_per_objfile->objfile;
4b514bc8
JK
2968 dwarf2_per_cu_data *the_cu
2969 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2970 struct dwarf2_per_cu_data);
2971 the_cu->sect_off = sect_off;
2972 the_cu->length = length;
e3b94546 2973 the_cu->dwarf2_per_objfile = dwarf2_per_objfile;
4b514bc8
JK
2974 the_cu->section = section;
2975 the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2976 struct dwarf2_per_cu_quick_data);
2977 the_cu->is_dwz = is_dwz;
2978 return the_cu;
2979}
2980
2ec9a5e0
TT
2981/* A helper for create_cus_from_index that handles a given list of
2982 CUs. */
2fdf6df6 2983
74a0d9f6 2984static void
2ec9a5e0
TT
2985create_cus_from_index_list (struct objfile *objfile,
2986 const gdb_byte *cu_list, offset_type n_elements,
2987 struct dwarf2_section_info *section,
2988 int is_dwz,
2989 int base_offset)
9291a0cd
TT
2990{
2991 offset_type i;
ed2dc618
SM
2992 struct dwarf2_per_objfile *dwarf2_per_objfile
2993 = get_dwarf2_per_objfile (objfile);
9291a0cd 2994
2ec9a5e0 2995 for (i = 0; i < n_elements; i += 2)
9291a0cd 2996 {
74a0d9f6 2997 gdb_static_assert (sizeof (ULONGEST) >= 8);
9c541725
PA
2998
2999 sect_offset sect_off
3000 = (sect_offset) extract_unsigned_integer (cu_list, 8, BFD_ENDIAN_LITTLE);
3001 ULONGEST length = extract_unsigned_integer (cu_list + 8, 8, BFD_ENDIAN_LITTLE);
9291a0cd
TT
3002 cu_list += 2 * 8;
3003
4b514bc8 3004 dwarf2_per_objfile->all_comp_units[base_offset + i / 2]
ed2dc618
SM
3005 = create_cu_from_index_list (dwarf2_per_objfile, section, is_dwz,
3006 sect_off, length);
9291a0cd 3007 }
9291a0cd
TT
3008}
3009
2ec9a5e0 3010/* Read the CU list from the mapped index, and use it to create all
74a0d9f6 3011 the CU objects for this objfile. */
2ec9a5e0 3012
74a0d9f6 3013static void
2ec9a5e0
TT
3014create_cus_from_index (struct objfile *objfile,
3015 const gdb_byte *cu_list, offset_type cu_list_elements,
3016 const gdb_byte *dwz_list, offset_type dwz_elements)
3017{
3018 struct dwz_file *dwz;
ed2dc618
SM
3019 struct dwarf2_per_objfile *dwarf2_per_objfile
3020 = get_dwarf2_per_objfile (objfile);
2ec9a5e0
TT
3021
3022 dwarf2_per_objfile->n_comp_units = (cu_list_elements + dwz_elements) / 2;
8d749320
SM
3023 dwarf2_per_objfile->all_comp_units =
3024 XOBNEWVEC (&objfile->objfile_obstack, struct dwarf2_per_cu_data *,
3025 dwarf2_per_objfile->n_comp_units);
2ec9a5e0 3026
74a0d9f6
JK
3027 create_cus_from_index_list (objfile, cu_list, cu_list_elements,
3028 &dwarf2_per_objfile->info, 0, 0);
2ec9a5e0
TT
3029
3030 if (dwz_elements == 0)
74a0d9f6 3031 return;
2ec9a5e0 3032
ed2dc618 3033 dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
74a0d9f6
JK
3034 create_cus_from_index_list (objfile, dwz_list, dwz_elements, &dwz->info, 1,
3035 cu_list_elements / 2);
2ec9a5e0
TT
3036}
3037
1fd400ff 3038/* Create the signatured type hash table from the index. */
673bfd45 3039
74a0d9f6 3040static void
673bfd45 3041create_signatured_type_table_from_index (struct objfile *objfile,
8b70b953 3042 struct dwarf2_section_info *section,
673bfd45
DE
3043 const gdb_byte *bytes,
3044 offset_type elements)
1fd400ff
TT
3045{
3046 offset_type i;
673bfd45 3047 htab_t sig_types_hash;
ed2dc618
SM
3048 struct dwarf2_per_objfile *dwarf2_per_objfile
3049 = get_dwarf2_per_objfile (objfile);
1fd400ff 3050
6aa5f3a6
DE
3051 dwarf2_per_objfile->n_type_units
3052 = dwarf2_per_objfile->n_allocated_type_units
3053 = elements / 3;
8d749320
SM
3054 dwarf2_per_objfile->all_type_units =
3055 XNEWVEC (struct signatured_type *, dwarf2_per_objfile->n_type_units);
1fd400ff 3056
673bfd45 3057 sig_types_hash = allocate_signatured_type_table (objfile);
1fd400ff
TT
3058
3059 for (i = 0; i < elements; i += 3)
3060 {
52dc124a 3061 struct signatured_type *sig_type;
9c541725 3062 ULONGEST signature;
1fd400ff 3063 void **slot;
9c541725 3064 cu_offset type_offset_in_tu;
1fd400ff 3065
74a0d9f6 3066 gdb_static_assert (sizeof (ULONGEST) >= 8);
9c541725
PA
3067 sect_offset sect_off
3068 = (sect_offset) extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
3069 type_offset_in_tu
3070 = (cu_offset) extract_unsigned_integer (bytes + 8, 8,
3071 BFD_ENDIAN_LITTLE);
1fd400ff
TT
3072 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
3073 bytes += 3 * 8;
3074
52dc124a 3075 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1fd400ff 3076 struct signatured_type);
52dc124a 3077 sig_type->signature = signature;
9c541725 3078 sig_type->type_offset_in_tu = type_offset_in_tu;
3019eac3 3079 sig_type->per_cu.is_debug_types = 1;
8a0459fd 3080 sig_type->per_cu.section = section;
9c541725 3081 sig_type->per_cu.sect_off = sect_off;
e3b94546 3082 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
52dc124a 3083 sig_type->per_cu.v.quick
1fd400ff
TT
3084 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3085 struct dwarf2_per_cu_quick_data);
3086
52dc124a
DE
3087 slot = htab_find_slot (sig_types_hash, sig_type, INSERT);
3088 *slot = sig_type;
1fd400ff 3089
b4dd5633 3090 dwarf2_per_objfile->all_type_units[i / 3] = sig_type;
1fd400ff
TT
3091 }
3092
673bfd45 3093 dwarf2_per_objfile->signatured_types = sig_types_hash;
1fd400ff
TT
3094}
3095
927aa2e7
JK
3096/* Create the signatured type hash table from .debug_names. */
3097
3098static void
3099create_signatured_type_table_from_debug_names
ed2dc618 3100 (struct dwarf2_per_objfile *dwarf2_per_objfile,
927aa2e7
JK
3101 const mapped_debug_names &map,
3102 struct dwarf2_section_info *section,
3103 struct dwarf2_section_info *abbrev_section)
3104{
ed2dc618
SM
3105 struct objfile *objfile = dwarf2_per_objfile->objfile;
3106
927aa2e7
JK
3107 dwarf2_read_section (objfile, section);
3108 dwarf2_read_section (objfile, abbrev_section);
3109
3110 dwarf2_per_objfile->n_type_units
3111 = dwarf2_per_objfile->n_allocated_type_units
3112 = map.tu_count;
3113 dwarf2_per_objfile->all_type_units
3114 = XNEWVEC (struct signatured_type *, dwarf2_per_objfile->n_type_units);
3115
3116 htab_t sig_types_hash = allocate_signatured_type_table (objfile);
3117
3118 for (uint32_t i = 0; i < map.tu_count; ++i)
3119 {
3120 struct signatured_type *sig_type;
3121 ULONGEST signature;
3122 void **slot;
3123 cu_offset type_offset_in_tu;
3124
3125 sect_offset sect_off
3126 = (sect_offset) (extract_unsigned_integer
3127 (map.tu_table_reordered + i * map.offset_size,
3128 map.offset_size,
3129 map.dwarf5_byte_order));
3130
3131 comp_unit_head cu_header;
ed2dc618
SM
3132 read_and_check_comp_unit_head (dwarf2_per_objfile, &cu_header, section,
3133 abbrev_section,
927aa2e7
JK
3134 section->buffer + to_underlying (sect_off),
3135 rcuh_kind::TYPE);
3136
3137 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3138 struct signatured_type);
3139 sig_type->signature = cu_header.signature;
3140 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
3141 sig_type->per_cu.is_debug_types = 1;
3142 sig_type->per_cu.section = section;
3143 sig_type->per_cu.sect_off = sect_off;
e3b94546 3144 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
927aa2e7
JK
3145 sig_type->per_cu.v.quick
3146 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3147 struct dwarf2_per_cu_quick_data);
3148
3149 slot = htab_find_slot (sig_types_hash, sig_type, INSERT);
3150 *slot = sig_type;
3151
3152 dwarf2_per_objfile->all_type_units[i] = sig_type;
3153 }
3154
3155 dwarf2_per_objfile->signatured_types = sig_types_hash;
3156}
3157
9291a0cd
TT
3158/* Read the address map data from the mapped index, and use it to
3159 populate the objfile's psymtabs_addrmap. */
2fdf6df6 3160
9291a0cd 3161static void
ed2dc618
SM
3162create_addrmap_from_index (struct dwarf2_per_objfile *dwarf2_per_objfile,
3163 struct mapped_index *index)
9291a0cd 3164{
ed2dc618 3165 struct objfile *objfile = dwarf2_per_objfile->objfile;
3e29f34a 3166 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9291a0cd 3167 const gdb_byte *iter, *end;
9291a0cd 3168 struct addrmap *mutable_map;
9291a0cd
TT
3169 CORE_ADDR baseaddr;
3170
8268c778
PA
3171 auto_obstack temp_obstack;
3172
9291a0cd
TT
3173 mutable_map = addrmap_create_mutable (&temp_obstack);
3174
f00a2de2
PA
3175 iter = index->address_table.data ();
3176 end = iter + index->address_table.size ();
9291a0cd
TT
3177
3178 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
3179
3180 while (iter < end)
3181 {
3182 ULONGEST hi, lo, cu_index;
3183 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
3184 iter += 8;
3185 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
3186 iter += 8;
3187 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
3188 iter += 4;
f652bce2 3189
24a55014 3190 if (lo > hi)
f652bce2 3191 {
24a55014
DE
3192 complaint (&symfile_complaints,
3193 _(".gdb_index address table has invalid range (%s - %s)"),
c0cd8254 3194 hex_string (lo), hex_string (hi));
24a55014 3195 continue;
f652bce2 3196 }
24a55014
DE
3197
3198 if (cu_index >= dwarf2_per_objfile->n_comp_units)
f652bce2
DE
3199 {
3200 complaint (&symfile_complaints,
3201 _(".gdb_index address table has invalid CU number %u"),
3202 (unsigned) cu_index);
24a55014 3203 continue;
f652bce2 3204 }
24a55014 3205
3e29f34a
MR
3206 lo = gdbarch_adjust_dwarf2_addr (gdbarch, lo + baseaddr);
3207 hi = gdbarch_adjust_dwarf2_addr (gdbarch, hi + baseaddr);
ed2dc618
SM
3208 addrmap_set_empty (mutable_map, lo, hi - 1,
3209 dw2_get_cutu (dwarf2_per_objfile, cu_index));
9291a0cd
TT
3210 }
3211
3212 objfile->psymtabs_addrmap = addrmap_create_fixed (mutable_map,
3213 &objfile->objfile_obstack);
9291a0cd
TT
3214}
3215
927aa2e7
JK
3216/* Read the address map data from DWARF-5 .debug_aranges, and use it to
3217 populate the objfile's psymtabs_addrmap. */
3218
3219static void
ed2dc618 3220create_addrmap_from_aranges (struct dwarf2_per_objfile *dwarf2_per_objfile,
927aa2e7
JK
3221 struct dwarf2_section_info *section)
3222{
ed2dc618 3223 struct objfile *objfile = dwarf2_per_objfile->objfile;
927aa2e7
JK
3224 bfd *abfd = objfile->obfd;
3225 struct gdbarch *gdbarch = get_objfile_arch (objfile);
3226 const CORE_ADDR baseaddr = ANOFFSET (objfile->section_offsets,
3227 SECT_OFF_TEXT (objfile));
3228
3229 auto_obstack temp_obstack;
3230 addrmap *mutable_map = addrmap_create_mutable (&temp_obstack);
3231
3232 std::unordered_map<sect_offset,
3233 dwarf2_per_cu_data *,
3234 gdb::hash_enum<sect_offset>>
3235 debug_info_offset_to_per_cu;
3236 for (int cui = 0; cui < dwarf2_per_objfile->n_comp_units; ++cui)
3237 {
ed2dc618 3238 dwarf2_per_cu_data *per_cu = dw2_get_cutu (dwarf2_per_objfile, cui);
927aa2e7
JK
3239 const auto insertpair
3240 = debug_info_offset_to_per_cu.emplace (per_cu->sect_off, per_cu);
3241 if (!insertpair.second)
3242 {
3243 warning (_("Section .debug_aranges in %s has duplicate "
9d8780f0
SM
3244 "debug_info_offset %s, ignoring .debug_aranges."),
3245 objfile_name (objfile), sect_offset_str (per_cu->sect_off));
927aa2e7
JK
3246 return;
3247 }
3248 }
3249
3250 dwarf2_read_section (objfile, section);
3251
3252 const bfd_endian dwarf5_byte_order = gdbarch_byte_order (gdbarch);
3253
3254 const gdb_byte *addr = section->buffer;
3255
3256 while (addr < section->buffer + section->size)
3257 {
3258 const gdb_byte *const entry_addr = addr;
3259 unsigned int bytes_read;
3260
3261 const LONGEST entry_length = read_initial_length (abfd, addr,
3262 &bytes_read);
3263 addr += bytes_read;
3264
3265 const gdb_byte *const entry_end = addr + entry_length;
3266 const bool dwarf5_is_dwarf64 = bytes_read != 4;
3267 const uint8_t offset_size = dwarf5_is_dwarf64 ? 8 : 4;
3268 if (addr + entry_length > section->buffer + section->size)
3269 {
3270 warning (_("Section .debug_aranges in %s entry at offset %zu "
3271 "length %s exceeds section length %s, "
3272 "ignoring .debug_aranges."),
3273 objfile_name (objfile), entry_addr - section->buffer,
3274 plongest (bytes_read + entry_length),
3275 pulongest (section->size));
3276 return;
3277 }
3278
3279 /* The version number. */
3280 const uint16_t version = read_2_bytes (abfd, addr);
3281 addr += 2;
3282 if (version != 2)
3283 {
3284 warning (_("Section .debug_aranges in %s entry at offset %zu "
3285 "has unsupported version %d, ignoring .debug_aranges."),
3286 objfile_name (objfile), entry_addr - section->buffer,
3287 version);
3288 return;
3289 }
3290
3291 const uint64_t debug_info_offset
3292 = extract_unsigned_integer (addr, offset_size, dwarf5_byte_order);
3293 addr += offset_size;
3294 const auto per_cu_it
3295 = debug_info_offset_to_per_cu.find (sect_offset (debug_info_offset));
3296 if (per_cu_it == debug_info_offset_to_per_cu.cend ())
3297 {
3298 warning (_("Section .debug_aranges in %s entry at offset %zu "
3299 "debug_info_offset %s does not exists, "
3300 "ignoring .debug_aranges."),
3301 objfile_name (objfile), entry_addr - section->buffer,
3302 pulongest (debug_info_offset));
3303 return;
3304 }
3305 dwarf2_per_cu_data *const per_cu = per_cu_it->second;
3306
3307 const uint8_t address_size = *addr++;
3308 if (address_size < 1 || address_size > 8)
3309 {
3310 warning (_("Section .debug_aranges in %s entry at offset %zu "
3311 "address_size %u is invalid, ignoring .debug_aranges."),
3312 objfile_name (objfile), entry_addr - section->buffer,
3313 address_size);
3314 return;
3315 }
3316
3317 const uint8_t segment_selector_size = *addr++;
3318 if (segment_selector_size != 0)
3319 {
3320 warning (_("Section .debug_aranges in %s entry at offset %zu "
3321 "segment_selector_size %u is not supported, "
3322 "ignoring .debug_aranges."),
3323 objfile_name (objfile), entry_addr - section->buffer,
3324 segment_selector_size);
3325 return;
3326 }
3327
3328 /* Must pad to an alignment boundary that is twice the address
3329 size. It is undocumented by the DWARF standard but GCC does
3330 use it. */
3331 for (size_t padding = ((-(addr - section->buffer))
3332 & (2 * address_size - 1));
3333 padding > 0; padding--)
3334 if (*addr++ != 0)
3335 {
3336 warning (_("Section .debug_aranges in %s entry at offset %zu "
3337 "padding is not zero, ignoring .debug_aranges."),
3338 objfile_name (objfile), entry_addr - section->buffer);
3339 return;
3340 }
3341
3342 for (;;)
3343 {
3344 if (addr + 2 * address_size > entry_end)
3345 {
3346 warning (_("Section .debug_aranges in %s entry at offset %zu "
3347 "address list is not properly terminated, "
3348 "ignoring .debug_aranges."),
3349 objfile_name (objfile), entry_addr - section->buffer);
3350 return;
3351 }
3352 ULONGEST start = extract_unsigned_integer (addr, address_size,
3353 dwarf5_byte_order);
3354 addr += address_size;
3355 ULONGEST length = extract_unsigned_integer (addr, address_size,
3356 dwarf5_byte_order);
3357 addr += address_size;
3358 if (start == 0 && length == 0)
3359 break;
3360 if (start == 0 && !dwarf2_per_objfile->has_section_at_zero)
3361 {
3362 /* Symbol was eliminated due to a COMDAT group. */
3363 continue;
3364 }
3365 ULONGEST end = start + length;
3366 start = gdbarch_adjust_dwarf2_addr (gdbarch, start + baseaddr);
3367 end = gdbarch_adjust_dwarf2_addr (gdbarch, end + baseaddr);
3368 addrmap_set_empty (mutable_map, start, end - 1, per_cu);
3369 }
3370 }
3371
3372 objfile->psymtabs_addrmap = addrmap_create_fixed (mutable_map,
3373 &objfile->objfile_obstack);
3374}
3375
9291a0cd
TT
3376/* Find a slot in the mapped index INDEX for the object named NAME.
3377 If NAME is found, set *VEC_OUT to point to the CU vector in the
109483d9
PA
3378 constant pool and return true. If NAME cannot be found, return
3379 false. */
2fdf6df6 3380
109483d9 3381static bool
9291a0cd
TT
3382find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
3383 offset_type **vec_out)
3384{
0cf03b49 3385 offset_type hash;
9291a0cd 3386 offset_type slot, step;
559a7a62 3387 int (*cmp) (const char *, const char *);
9291a0cd 3388
791afaa2 3389 gdb::unique_xmalloc_ptr<char> without_params;
0cf03b49 3390 if (current_language->la_language == language_cplus
45280282
IB
3391 || current_language->la_language == language_fortran
3392 || current_language->la_language == language_d)
0cf03b49
JK
3393 {
3394 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
3395 not contain any. */
a8719064 3396
72998fb3 3397 if (strchr (name, '(') != NULL)
0cf03b49 3398 {
109483d9 3399 without_params = cp_remove_params (name);
0cf03b49 3400
72998fb3 3401 if (without_params != NULL)
791afaa2 3402 name = without_params.get ();
0cf03b49
JK
3403 }
3404 }
3405
559a7a62 3406 /* Index version 4 did not support case insensitive searches. But the
feea76c2 3407 indices for case insensitive languages are built in lowercase, therefore
559a7a62
JK
3408 simulate our NAME being searched is also lowercased. */
3409 hash = mapped_index_string_hash ((index->version == 4
3410 && case_sensitivity == case_sensitive_off
3411 ? 5 : index->version),
3412 name);
3413
f00a2de2
PA
3414 slot = hash & (index->symbol_table.size () - 1);
3415 step = ((hash * 17) & (index->symbol_table.size () - 1)) | 1;
559a7a62 3416 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
9291a0cd
TT
3417
3418 for (;;)
3419 {
9291a0cd 3420 const char *str;
f00a2de2
PA
3421
3422 const auto &bucket = index->symbol_table[slot];
3423 if (bucket.name == 0 && bucket.vec == 0)
109483d9 3424 return false;
9291a0cd 3425
f00a2de2 3426 str = index->constant_pool + MAYBE_SWAP (bucket.name);
559a7a62 3427 if (!cmp (name, str))
9291a0cd
TT
3428 {
3429 *vec_out = (offset_type *) (index->constant_pool
f00a2de2 3430 + MAYBE_SWAP (bucket.vec));
109483d9 3431 return true;
9291a0cd
TT
3432 }
3433
f00a2de2 3434 slot = (slot + step) & (index->symbol_table.size () - 1);
9291a0cd
TT
3435 }
3436}
3437
2ec9a5e0
TT
3438/* A helper function that reads the .gdb_index from SECTION and fills
3439 in MAP. FILENAME is the name of the file containing the section;
3440 it is used for error reporting. DEPRECATED_OK is nonzero if it is
3441 ok to use deprecated sections.
3442
3443 CU_LIST, CU_LIST_ELEMENTS, TYPES_LIST, and TYPES_LIST_ELEMENTS are
3444 out parameters that are filled in with information about the CU and
3445 TU lists in the section.
3446
3447 Returns 1 if all went well, 0 otherwise. */
2fdf6df6 3448
9291a0cd 3449static int
2ec9a5e0
TT
3450read_index_from_section (struct objfile *objfile,
3451 const char *filename,
3452 int deprecated_ok,
3453 struct dwarf2_section_info *section,
3454 struct mapped_index *map,
3455 const gdb_byte **cu_list,
3456 offset_type *cu_list_elements,
3457 const gdb_byte **types_list,
3458 offset_type *types_list_elements)
9291a0cd 3459{
948f8e3d 3460 const gdb_byte *addr;
2ec9a5e0 3461 offset_type version;
b3b272e1 3462 offset_type *metadata;
1fd400ff 3463 int i;
9291a0cd 3464
2ec9a5e0 3465 if (dwarf2_section_empty_p (section))
9291a0cd 3466 return 0;
82430852
JK
3467
3468 /* Older elfutils strip versions could keep the section in the main
3469 executable while splitting it for the separate debug info file. */
a32a8923 3470 if ((get_section_flags (section) & SEC_HAS_CONTENTS) == 0)
82430852
JK
3471 return 0;
3472
2ec9a5e0 3473 dwarf2_read_section (objfile, section);
9291a0cd 3474
2ec9a5e0 3475 addr = section->buffer;
9291a0cd 3476 /* Version check. */
1fd400ff 3477 version = MAYBE_SWAP (*(offset_type *) addr);
987d643c 3478 /* Versions earlier than 3 emitted every copy of a psymbol. This
a6e293d1 3479 causes the index to behave very poorly for certain requests. Version 3
831adc1f 3480 contained incomplete addrmap. So, it seems better to just ignore such
481860b3 3481 indices. */
831adc1f 3482 if (version < 4)
481860b3
GB
3483 {
3484 static int warning_printed = 0;
3485 if (!warning_printed)
3486 {
3487 warning (_("Skipping obsolete .gdb_index section in %s."),
2ec9a5e0 3488 filename);
481860b3
GB
3489 warning_printed = 1;
3490 }
3491 return 0;
3492 }
3493 /* Index version 4 uses a different hash function than index version
3494 5 and later.
3495
3496 Versions earlier than 6 did not emit psymbols for inlined
3497 functions. Using these files will cause GDB not to be able to
3498 set breakpoints on inlined functions by name, so we ignore these
e615022a
DE
3499 indices unless the user has done
3500 "set use-deprecated-index-sections on". */
2ec9a5e0 3501 if (version < 6 && !deprecated_ok)
481860b3
GB
3502 {
3503 static int warning_printed = 0;
3504 if (!warning_printed)
3505 {
e615022a
DE
3506 warning (_("\
3507Skipping deprecated .gdb_index section in %s.\n\
3508Do \"set use-deprecated-index-sections on\" before the file is read\n\
3509to use the section anyway."),
2ec9a5e0 3510 filename);
481860b3
GB
3511 warning_printed = 1;
3512 }
3513 return 0;
3514 }
796a7ff8 3515 /* Version 7 indices generated by gold refer to the CU for a symbol instead
8943b874
DE
3516 of the TU (for symbols coming from TUs),
3517 http://sourceware.org/bugzilla/show_bug.cgi?id=15021.
3518 Plus gold-generated indices can have duplicate entries for global symbols,
3519 http://sourceware.org/bugzilla/show_bug.cgi?id=15646.
3520 These are just performance bugs, and we can't distinguish gdb-generated
3521 indices from gold-generated ones, so issue no warning here. */
796a7ff8 3522
481860b3 3523 /* Indexes with higher version than the one supported by GDB may be no
594e8718 3524 longer backward compatible. */
796a7ff8 3525 if (version > 8)
594e8718 3526 return 0;
9291a0cd 3527
559a7a62 3528 map->version = version;
2ec9a5e0 3529 map->total_size = section->size;
9291a0cd
TT
3530
3531 metadata = (offset_type *) (addr + sizeof (offset_type));
1fd400ff
TT
3532
3533 i = 0;
2ec9a5e0
TT
3534 *cu_list = addr + MAYBE_SWAP (metadata[i]);
3535 *cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
3536 / 8);
1fd400ff
TT
3537 ++i;
3538
2ec9a5e0
TT
3539 *types_list = addr + MAYBE_SWAP (metadata[i]);
3540 *types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
3541 - MAYBE_SWAP (metadata[i]))
3542 / 8);
987d643c 3543 ++i;
1fd400ff 3544
f00a2de2
PA
3545 const gdb_byte *address_table = addr + MAYBE_SWAP (metadata[i]);
3546 const gdb_byte *address_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
3547 map->address_table
3548 = gdb::array_view<const gdb_byte> (address_table, address_table_end);
1fd400ff
TT
3549 ++i;
3550
f00a2de2
PA
3551 const gdb_byte *symbol_table = addr + MAYBE_SWAP (metadata[i]);
3552 const gdb_byte *symbol_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
3553 map->symbol_table
3554 = gdb::array_view<mapped_index::symbol_table_slot>
3555 ((mapped_index::symbol_table_slot *) symbol_table,
3556 (mapped_index::symbol_table_slot *) symbol_table_end);
9291a0cd 3557
f00a2de2 3558 ++i;
f9d83a0b 3559 map->constant_pool = (char *) (addr + MAYBE_SWAP (metadata[i]));
1fd400ff 3560
2ec9a5e0
TT
3561 return 1;
3562}
3563
927aa2e7 3564/* Read .gdb_index. If everything went ok, initialize the "quick"
2ec9a5e0
TT
3565 elements of all the CUs and return 1. Otherwise, return 0. */
3566
3567static int
3568dwarf2_read_index (struct objfile *objfile)
3569{
3570 struct mapped_index local_map, *map;
3571 const gdb_byte *cu_list, *types_list, *dwz_list = NULL;
3572 offset_type cu_list_elements, types_list_elements, dwz_list_elements = 0;
4db1a1dc 3573 struct dwz_file *dwz;
ed2dc618
SM
3574 struct dwarf2_per_objfile *dwarf2_per_objfile
3575 = get_dwarf2_per_objfile (objfile);
2ec9a5e0 3576
4262abfb 3577 if (!read_index_from_section (objfile, objfile_name (objfile),
2ec9a5e0
TT
3578 use_deprecated_index_sections,
3579 &dwarf2_per_objfile->gdb_index, &local_map,
3580 &cu_list, &cu_list_elements,
3581 &types_list, &types_list_elements))
3582 return 0;
3583
0fefef59 3584 /* Don't use the index if it's empty. */
f00a2de2 3585 if (local_map.symbol_table.empty ())
0fefef59
DE
3586 return 0;
3587
2ec9a5e0
TT
3588 /* If there is a .dwz file, read it so we can get its CU list as
3589 well. */
ed2dc618 3590 dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
4db1a1dc 3591 if (dwz != NULL)
2ec9a5e0 3592 {
2ec9a5e0
TT
3593 struct mapped_index dwz_map;
3594 const gdb_byte *dwz_types_ignore;
3595 offset_type dwz_types_elements_ignore;
3596
3597 if (!read_index_from_section (objfile, bfd_get_filename (dwz->dwz_bfd),
3598 1,
3599 &dwz->gdb_index, &dwz_map,
3600 &dwz_list, &dwz_list_elements,
3601 &dwz_types_ignore,
3602 &dwz_types_elements_ignore))
3603 {
3604 warning (_("could not read '.gdb_index' section from %s; skipping"),
3605 bfd_get_filename (dwz->dwz_bfd));
3606 return 0;
3607 }
3608 }
3609
74a0d9f6
JK
3610 create_cus_from_index (objfile, cu_list, cu_list_elements, dwz_list,
3611 dwz_list_elements);
1fd400ff 3612
8b70b953
TT
3613 if (types_list_elements)
3614 {
3615 struct dwarf2_section_info *section;
3616
3617 /* We can only handle a single .debug_types when we have an
3618 index. */
3619 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) != 1)
3620 return 0;
3621
3622 section = VEC_index (dwarf2_section_info_def,
3623 dwarf2_per_objfile->types, 0);
3624
74a0d9f6
JK
3625 create_signatured_type_table_from_index (objfile, section, types_list,
3626 types_list_elements);
8b70b953 3627 }
9291a0cd 3628
ed2dc618 3629 create_addrmap_from_index (dwarf2_per_objfile, &local_map);
2ec9a5e0 3630
8d749320 3631 map = XOBNEW (&objfile->objfile_obstack, struct mapped_index);
3f563c84 3632 map = new (map) mapped_index ();
2ec9a5e0 3633 *map = local_map;
9291a0cd
TT
3634
3635 dwarf2_per_objfile->index_table = map;
3636 dwarf2_per_objfile->using_index = 1;
7b9f3c50
DE
3637 dwarf2_per_objfile->quick_file_names_table =
3638 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
9291a0cd
TT
3639
3640 return 1;
3641}
3642
dee91e82 3643/* die_reader_func for dw2_get_file_names. */
2fdf6df6 3644
dee91e82
DE
3645static void
3646dw2_get_file_names_reader (const struct die_reader_specs *reader,
d521ce57 3647 const gdb_byte *info_ptr,
dee91e82
DE
3648 struct die_info *comp_unit_die,
3649 int has_children,
3650 void *data)
9291a0cd 3651{
dee91e82 3652 struct dwarf2_cu *cu = reader->cu;
ed2dc618 3653 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
518817b3
SM
3654 struct dwarf2_per_objfile *dwarf2_per_objfile
3655 = cu->per_cu->dwarf2_per_objfile;
dee91e82 3656 struct objfile *objfile = dwarf2_per_objfile->objfile;
094b34ac 3657 struct dwarf2_per_cu_data *lh_cu;
9291a0cd 3658 struct attribute *attr;
dee91e82 3659 int i;
7b9f3c50
DE
3660 void **slot;
3661 struct quick_file_names *qfn;
9291a0cd 3662
0186c6a7
DE
3663 gdb_assert (! this_cu->is_debug_types);
3664
07261596
TT
3665 /* Our callers never want to match partial units -- instead they
3666 will match the enclosing full CU. */
3667 if (comp_unit_die->tag == DW_TAG_partial_unit)
3668 {
3669 this_cu->v.quick->no_file_data = 1;
3670 return;
3671 }
3672
0186c6a7 3673 lh_cu = this_cu;
7b9f3c50 3674 slot = NULL;
dee91e82 3675
fff8551c 3676 line_header_up lh;
9c541725 3677 sect_offset line_offset {};
fff8551c 3678
dee91e82 3679 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
9291a0cd
TT
3680 if (attr)
3681 {
7b9f3c50
DE
3682 struct quick_file_names find_entry;
3683
9c541725 3684 line_offset = (sect_offset) DW_UNSND (attr);
7b9f3c50
DE
3685
3686 /* We may have already read in this line header (TU line header sharing).
3687 If we have we're done. */
094b34ac 3688 find_entry.hash.dwo_unit = cu->dwo_unit;
9c541725 3689 find_entry.hash.line_sect_off = line_offset;
7b9f3c50
DE
3690 slot = htab_find_slot (dwarf2_per_objfile->quick_file_names_table,
3691 &find_entry, INSERT);
3692 if (*slot != NULL)
3693 {
9a3c8263 3694 lh_cu->v.quick->file_names = (struct quick_file_names *) *slot;
dee91e82 3695 return;
7b9f3c50
DE
3696 }
3697
3019eac3 3698 lh = dwarf_decode_line_header (line_offset, cu);
9291a0cd
TT
3699 }
3700 if (lh == NULL)
3701 {
094b34ac 3702 lh_cu->v.quick->no_file_data = 1;
dee91e82 3703 return;
9291a0cd
TT
3704 }
3705
8d749320 3706 qfn = XOBNEW (&objfile->objfile_obstack, struct quick_file_names);
094b34ac 3707 qfn->hash.dwo_unit = cu->dwo_unit;
9c541725 3708 qfn->hash.line_sect_off = line_offset;
7b9f3c50
DE
3709 gdb_assert (slot != NULL);
3710 *slot = qfn;
9291a0cd 3711
d721ba37 3712 file_and_directory fnd = find_file_and_directory (comp_unit_die, cu);
9291a0cd 3713
fff8551c 3714 qfn->num_file_names = lh->file_names.size ();
8d749320 3715 qfn->file_names =
fff8551c
PA
3716 XOBNEWVEC (&objfile->objfile_obstack, const char *, lh->file_names.size ());
3717 for (i = 0; i < lh->file_names.size (); ++i)
3718 qfn->file_names[i] = file_full_name (i + 1, lh.get (), fnd.comp_dir);
7b9f3c50 3719 qfn->real_names = NULL;
9291a0cd 3720
094b34ac 3721 lh_cu->v.quick->file_names = qfn;
dee91e82
DE
3722}
3723
3724/* A helper for the "quick" functions which attempts to read the line
3725 table for THIS_CU. */
3726
3727static struct quick_file_names *
e4a48d9d 3728dw2_get_file_names (struct dwarf2_per_cu_data *this_cu)
dee91e82 3729{
0186c6a7
DE
3730 /* This should never be called for TUs. */
3731 gdb_assert (! this_cu->is_debug_types);
3732 /* Nor type unit groups. */
3733 gdb_assert (! IS_TYPE_UNIT_GROUP (this_cu));
f4dc4d17 3734
dee91e82
DE
3735 if (this_cu->v.quick->file_names != NULL)
3736 return this_cu->v.quick->file_names;
3737 /* If we know there is no line data, no point in looking again. */
3738 if (this_cu->v.quick->no_file_data)
3739 return NULL;
3740
0186c6a7 3741 init_cutu_and_read_dies_simple (this_cu, dw2_get_file_names_reader, NULL);
dee91e82
DE
3742
3743 if (this_cu->v.quick->no_file_data)
3744 return NULL;
3745 return this_cu->v.quick->file_names;
9291a0cd
TT
3746}
3747
3748/* A helper for the "quick" functions which computes and caches the
7b9f3c50 3749 real path for a given file name from the line table. */
2fdf6df6 3750
9291a0cd 3751static const char *
7b9f3c50
DE
3752dw2_get_real_path (struct objfile *objfile,
3753 struct quick_file_names *qfn, int index)
9291a0cd 3754{
7b9f3c50
DE
3755 if (qfn->real_names == NULL)
3756 qfn->real_names = OBSTACK_CALLOC (&objfile->objfile_obstack,
26f2dc30 3757 qfn->num_file_names, const char *);
9291a0cd 3758
7b9f3c50 3759 if (qfn->real_names[index] == NULL)
14278e1f 3760 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]).release ();
9291a0cd 3761
7b9f3c50 3762 return qfn->real_names[index];
9291a0cd
TT
3763}
3764
3765static struct symtab *
3766dw2_find_last_source_symtab (struct objfile *objfile)
3767{
ed2dc618
SM
3768 struct dwarf2_per_objfile *dwarf2_per_objfile
3769 = get_dwarf2_per_objfile (objfile);
3770 int index = dwarf2_per_objfile->n_comp_units - 1;
3771 dwarf2_per_cu_data *dwarf_cu = dw2_get_cutu (dwarf2_per_objfile, index);
3772 compunit_symtab *cust = dw2_instantiate_symtab (dwarf_cu);
ae2de4f8 3773
43f3e411
DE
3774 if (cust == NULL)
3775 return NULL;
ed2dc618 3776
43f3e411 3777 return compunit_primary_filetab (cust);
9291a0cd
TT
3778}
3779
7b9f3c50
DE
3780/* Traversal function for dw2_forget_cached_source_info. */
3781
3782static int
3783dw2_free_cached_file_names (void **slot, void *info)
9291a0cd 3784{
7b9f3c50 3785 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
9291a0cd 3786
7b9f3c50 3787 if (file_data->real_names)
9291a0cd 3788 {
7b9f3c50 3789 int i;
9291a0cd 3790
7b9f3c50 3791 for (i = 0; i < file_data->num_file_names; ++i)
9291a0cd 3792 {
7b9f3c50
DE
3793 xfree ((void*) file_data->real_names[i]);
3794 file_data->real_names[i] = NULL;
9291a0cd
TT
3795 }
3796 }
7b9f3c50
DE
3797
3798 return 1;
3799}
3800
3801static void
3802dw2_forget_cached_source_info (struct objfile *objfile)
3803{
ed2dc618
SM
3804 struct dwarf2_per_objfile *dwarf2_per_objfile
3805 = get_dwarf2_per_objfile (objfile);
7b9f3c50
DE
3806
3807 htab_traverse_noresize (dwarf2_per_objfile->quick_file_names_table,
3808 dw2_free_cached_file_names, NULL);
9291a0cd
TT
3809}
3810
f8eba3c6
TT
3811/* Helper function for dw2_map_symtabs_matching_filename that expands
3812 the symtabs and calls the iterator. */
3813
3814static int
3815dw2_map_expand_apply (struct objfile *objfile,
3816 struct dwarf2_per_cu_data *per_cu,
f5b95b50 3817 const char *name, const char *real_path,
14bc53a8 3818 gdb::function_view<bool (symtab *)> callback)
f8eba3c6 3819{
43f3e411 3820 struct compunit_symtab *last_made = objfile->compunit_symtabs;
f8eba3c6
TT
3821
3822 /* Don't visit already-expanded CUs. */
43f3e411 3823 if (per_cu->v.quick->compunit_symtab)
f8eba3c6
TT
3824 return 0;
3825
3826 /* This may expand more than one symtab, and we want to iterate over
3827 all of them. */
a0f42c21 3828 dw2_instantiate_symtab (per_cu);
f8eba3c6 3829
14bc53a8
PA
3830 return iterate_over_some_symtabs (name, real_path, objfile->compunit_symtabs,
3831 last_made, callback);
f8eba3c6
TT
3832}
3833
3834/* Implementation of the map_symtabs_matching_filename method. */
3835
14bc53a8
PA
3836static bool
3837dw2_map_symtabs_matching_filename
3838 (struct objfile *objfile, const char *name, const char *real_path,
3839 gdb::function_view<bool (symtab *)> callback)
9291a0cd
TT
3840{
3841 int i;
c011a4f4 3842 const char *name_basename = lbasename (name);
ed2dc618
SM
3843 struct dwarf2_per_objfile *dwarf2_per_objfile
3844 = get_dwarf2_per_objfile (objfile);
ae2de4f8 3845
848e3e78
DE
3846 /* The rule is CUs specify all the files, including those used by
3847 any TU, so there's no need to scan TUs here. */
f4dc4d17 3848
ed2dc618 3849 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
9291a0cd
TT
3850 {
3851 int j;
ed2dc618 3852 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (dwarf2_per_objfile, i);
7b9f3c50 3853 struct quick_file_names *file_data;
9291a0cd 3854
3d7bb9d9 3855 /* We only need to look at symtabs not already expanded. */
43f3e411 3856 if (per_cu->v.quick->compunit_symtab)
9291a0cd
TT
3857 continue;
3858
e4a48d9d 3859 file_data = dw2_get_file_names (per_cu);
7b9f3c50 3860 if (file_data == NULL)
9291a0cd
TT
3861 continue;
3862
7b9f3c50 3863 for (j = 0; j < file_data->num_file_names; ++j)
9291a0cd 3864 {
7b9f3c50 3865 const char *this_name = file_data->file_names[j];
da235a7c 3866 const char *this_real_name;
9291a0cd 3867
af529f8f 3868 if (compare_filenames_for_search (this_name, name))
9291a0cd 3869 {
f5b95b50 3870 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
14bc53a8
PA
3871 callback))
3872 return true;
288e77a7 3873 continue;
4aac40c8 3874 }
9291a0cd 3875
c011a4f4
DE
3876 /* Before we invoke realpath, which can get expensive when many
3877 files are involved, do a quick comparison of the basenames. */
3878 if (! basenames_may_differ
3879 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
3880 continue;
3881
da235a7c
JK
3882 this_real_name = dw2_get_real_path (objfile, file_data, j);
3883 if (compare_filenames_for_search (this_real_name, name))
9291a0cd 3884 {
da235a7c 3885 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
14bc53a8
PA
3886 callback))
3887 return true;
288e77a7 3888 continue;
da235a7c 3889 }
9291a0cd 3890
da235a7c
JK
3891 if (real_path != NULL)
3892 {
af529f8f
JK
3893 gdb_assert (IS_ABSOLUTE_PATH (real_path));
3894 gdb_assert (IS_ABSOLUTE_PATH (name));
7b9f3c50 3895 if (this_real_name != NULL
af529f8f 3896 && FILENAME_CMP (real_path, this_real_name) == 0)
9291a0cd 3897 {
f5b95b50 3898 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
14bc53a8
PA
3899 callback))
3900 return true;
288e77a7 3901 continue;
9291a0cd
TT
3902 }
3903 }
3904 }
3905 }
3906
14bc53a8 3907 return false;
9291a0cd
TT
3908}
3909
da51c347
DE
3910/* Struct used to manage iterating over all CUs looking for a symbol. */
3911
3912struct dw2_symtab_iterator
9291a0cd 3913{
ed2dc618
SM
3914 /* The dwarf2_per_objfile owning the CUs we are iterating on. */
3915 struct dwarf2_per_objfile *dwarf2_per_objfile;
da51c347
DE
3916 /* If non-zero, only look for symbols that match BLOCK_INDEX. */
3917 int want_specific_block;
3918 /* One of GLOBAL_BLOCK or STATIC_BLOCK.
3919 Unused if !WANT_SPECIFIC_BLOCK. */
3920 int block_index;
3921 /* The kind of symbol we're looking for. */
3922 domain_enum domain;
3923 /* The list of CUs from the index entry of the symbol,
3924 or NULL if not found. */
3925 offset_type *vec;
3926 /* The next element in VEC to look at. */
3927 int next;
3928 /* The number of elements in VEC, or zero if there is no match. */
3929 int length;
8943b874
DE
3930 /* Have we seen a global version of the symbol?
3931 If so we can ignore all further global instances.
3932 This is to work around gold/15646, inefficient gold-generated
3933 indices. */
3934 int global_seen;
da51c347 3935};
9291a0cd 3936
da51c347
DE
3937/* Initialize the index symtab iterator ITER.
3938 If WANT_SPECIFIC_BLOCK is non-zero, only look for symbols
3939 in block BLOCK_INDEX. Otherwise BLOCK_INDEX is ignored. */
2fdf6df6 3940
9291a0cd 3941static void
da51c347 3942dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
ed2dc618 3943 struct dwarf2_per_objfile *dwarf2_per_objfile,
da51c347
DE
3944 int want_specific_block,
3945 int block_index,
3946 domain_enum domain,
3947 const char *name)
3948{
ed2dc618 3949 iter->dwarf2_per_objfile = dwarf2_per_objfile;
da51c347
DE
3950 iter->want_specific_block = want_specific_block;
3951 iter->block_index = block_index;
3952 iter->domain = domain;
3953 iter->next = 0;
8943b874 3954 iter->global_seen = 0;
da51c347 3955
ed2dc618
SM
3956 mapped_index *index = dwarf2_per_objfile->index_table;
3957
3958 /* index is NULL if OBJF_READNOW. */
3959 if (index != NULL && find_slot_in_mapped_hash (index, name, &iter->vec))
da51c347
DE
3960 iter->length = MAYBE_SWAP (*iter->vec);
3961 else
3962 {
3963 iter->vec = NULL;
3964 iter->length = 0;
3965 }
3966}
3967
3968/* Return the next matching CU or NULL if there are no more. */
3969
3970static struct dwarf2_per_cu_data *
3971dw2_symtab_iter_next (struct dw2_symtab_iterator *iter)
3972{
ed2dc618
SM
3973 struct dwarf2_per_objfile *dwarf2_per_objfile = iter->dwarf2_per_objfile;
3974
da51c347
DE
3975 for ( ; iter->next < iter->length; ++iter->next)
3976 {
3977 offset_type cu_index_and_attrs =
3978 MAYBE_SWAP (iter->vec[iter->next + 1]);
3979 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
3190f0c6 3980 struct dwarf2_per_cu_data *per_cu;
da51c347
DE
3981 int want_static = iter->block_index != GLOBAL_BLOCK;
3982 /* This value is only valid for index versions >= 7. */
3983 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
3984 gdb_index_symbol_kind symbol_kind =
3985 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3986 /* Only check the symbol attributes if they're present.
3987 Indices prior to version 7 don't record them,
3988 and indices >= 7 may elide them for certain symbols
3989 (gold does this). */
3990 int attrs_valid =
ed2dc618 3991 (dwarf2_per_objfile->index_table->version >= 7
da51c347
DE
3992 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
3993
3190f0c6
DE
3994 /* Don't crash on bad data. */
3995 if (cu_index >= (dwarf2_per_objfile->n_comp_units
3996 + dwarf2_per_objfile->n_type_units))
3997 {
3998 complaint (&symfile_complaints,
3999 _(".gdb_index entry has bad CU index"
4262abfb
JK
4000 " [in module %s]"),
4001 objfile_name (dwarf2_per_objfile->objfile));
3190f0c6
DE
4002 continue;
4003 }
4004
ed2dc618 4005 per_cu = dw2_get_cutu (dwarf2_per_objfile, cu_index);
3190f0c6 4006
da51c347 4007 /* Skip if already read in. */
43f3e411 4008 if (per_cu->v.quick->compunit_symtab)
da51c347
DE
4009 continue;
4010
8943b874
DE
4011 /* Check static vs global. */
4012 if (attrs_valid)
4013 {
4014 if (iter->want_specific_block
4015 && want_static != is_static)
4016 continue;
4017 /* Work around gold/15646. */
4018 if (!is_static && iter->global_seen)
4019 continue;
4020 if (!is_static)
4021 iter->global_seen = 1;
4022 }
da51c347
DE
4023
4024 /* Only check the symbol's kind if it has one. */
4025 if (attrs_valid)
4026 {
4027 switch (iter->domain)
4028 {
4029 case VAR_DOMAIN:
4030 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
4031 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
4032 /* Some types are also in VAR_DOMAIN. */
4033 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
4034 continue;
4035 break;
4036 case STRUCT_DOMAIN:
4037 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
4038 continue;
4039 break;
4040 case LABEL_DOMAIN:
4041 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
4042 continue;
4043 break;
4044 default:
4045 break;
4046 }
4047 }
4048
4049 ++iter->next;
4050 return per_cu;
4051 }
4052
4053 return NULL;
4054}
4055
43f3e411 4056static struct compunit_symtab *
da51c347
DE
4057dw2_lookup_symbol (struct objfile *objfile, int block_index,
4058 const char *name, domain_enum domain)
9291a0cd 4059{
43f3e411 4060 struct compunit_symtab *stab_best = NULL;
ed2dc618
SM
4061 struct dwarf2_per_objfile *dwarf2_per_objfile
4062 = get_dwarf2_per_objfile (objfile);
9291a0cd 4063
b5ec771e
PA
4064 lookup_name_info lookup_name (name, symbol_name_match_type::FULL);
4065
ed2dc618
SM
4066 struct dw2_symtab_iterator iter;
4067 struct dwarf2_per_cu_data *per_cu;
da51c347 4068
ed2dc618 4069 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, 1, block_index, domain, name);
9291a0cd 4070
ed2dc618
SM
4071 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
4072 {
4073 struct symbol *sym, *with_opaque = NULL;
4074 struct compunit_symtab *stab = dw2_instantiate_symtab (per_cu);
4075 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
4076 struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
da51c347 4077
ed2dc618
SM
4078 sym = block_find_symbol (block, name, domain,
4079 block_find_non_opaque_type_preferred,
4080 &with_opaque);
b2e2f908 4081
ed2dc618
SM
4082 /* Some caution must be observed with overloaded functions
4083 and methods, since the index will not contain any overload
4084 information (but NAME might contain it). */
da51c347 4085
ed2dc618
SM
4086 if (sym != NULL
4087 && SYMBOL_MATCHES_SEARCH_NAME (sym, lookup_name))
4088 return stab;
4089 if (with_opaque != NULL
4090 && SYMBOL_MATCHES_SEARCH_NAME (with_opaque, lookup_name))
4091 stab_best = stab;
da51c347 4092
ed2dc618 4093 /* Keep looking through other CUs. */
9291a0cd 4094 }
9291a0cd 4095
da51c347 4096 return stab_best;
9291a0cd
TT
4097}
4098
4099static void
4100dw2_print_stats (struct objfile *objfile)
4101{
ed2dc618
SM
4102 struct dwarf2_per_objfile *dwarf2_per_objfile
4103 = get_dwarf2_per_objfile (objfile);
4104 int total = dwarf2_per_objfile->n_comp_units + dwarf2_per_objfile->n_type_units;
4105 int count = 0;
9291a0cd 4106
ed2dc618 4107 for (int i = 0; i < total; ++i)
9291a0cd 4108 {
ed2dc618 4109 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (dwarf2_per_objfile, i);
9291a0cd 4110
43f3e411 4111 if (!per_cu->v.quick->compunit_symtab)
9291a0cd
TT
4112 ++count;
4113 }
e4a48d9d 4114 printf_filtered (_(" Number of read CUs: %d\n"), total - count);
9291a0cd
TT
4115 printf_filtered (_(" Number of unread CUs: %d\n"), count);
4116}
4117
779bd270
DE
4118/* This dumps minimal information about the index.
4119 It is called via "mt print objfiles".
4120 One use is to verify .gdb_index has been loaded by the
4121 gdb.dwarf2/gdb-index.exp testcase. */
4122
9291a0cd
TT
4123static void
4124dw2_dump (struct objfile *objfile)
4125{
ed2dc618
SM
4126 struct dwarf2_per_objfile *dwarf2_per_objfile
4127 = get_dwarf2_per_objfile (objfile);
4128
779bd270
DE
4129 gdb_assert (dwarf2_per_objfile->using_index);
4130 printf_filtered (".gdb_index:");
4131 if (dwarf2_per_objfile->index_table != NULL)
4132 {
4133 printf_filtered (" version %d\n",
4134 dwarf2_per_objfile->index_table->version);
4135 }
4136 else
4137 printf_filtered (" faked for \"readnow\"\n");
4138 printf_filtered ("\n");
9291a0cd
TT
4139}
4140
4141static void
3189cb12
DE
4142dw2_relocate (struct objfile *objfile,
4143 const struct section_offsets *new_offsets,
4144 const struct section_offsets *delta)
9291a0cd
TT
4145{
4146 /* There's nothing to relocate here. */
4147}
4148
4149static void
4150dw2_expand_symtabs_for_function (struct objfile *objfile,
4151 const char *func_name)
4152{
ed2dc618
SM
4153 struct dwarf2_per_objfile *dwarf2_per_objfile
4154 = get_dwarf2_per_objfile (objfile);
da51c347 4155
ed2dc618
SM
4156 struct dw2_symtab_iterator iter;
4157 struct dwarf2_per_cu_data *per_cu;
da51c347 4158
ed2dc618
SM
4159 /* Note: It doesn't matter what we pass for block_index here. */
4160 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, 0, GLOBAL_BLOCK, VAR_DOMAIN,
4161 func_name);
da51c347 4162
ed2dc618
SM
4163 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
4164 dw2_instantiate_symtab (per_cu);
da51c347 4165
9291a0cd
TT
4166}
4167
4168static void
4169dw2_expand_all_symtabs (struct objfile *objfile)
4170{
ed2dc618
SM
4171 struct dwarf2_per_objfile *dwarf2_per_objfile
4172 = get_dwarf2_per_objfile (objfile);
4173 int total_units = (dwarf2_per_objfile->n_comp_units
4174 + dwarf2_per_objfile->n_type_units);
9291a0cd 4175
ed2dc618 4176 for (int i = 0; i < total_units; ++i)
9291a0cd 4177 {
ed2dc618
SM
4178 struct dwarf2_per_cu_data *per_cu
4179 = dw2_get_cutu (dwarf2_per_objfile, i);
9291a0cd 4180
a0f42c21 4181 dw2_instantiate_symtab (per_cu);
9291a0cd
TT
4182 }
4183}
4184
4185static void
652a8996
JK
4186dw2_expand_symtabs_with_fullname (struct objfile *objfile,
4187 const char *fullname)
9291a0cd 4188{
ed2dc618
SM
4189 struct dwarf2_per_objfile *dwarf2_per_objfile
4190 = get_dwarf2_per_objfile (objfile);
d4637a04
DE
4191
4192 /* We don't need to consider type units here.
4193 This is only called for examining code, e.g. expand_line_sal.
4194 There can be an order of magnitude (or more) more type units
4195 than comp units, and we avoid them if we can. */
4196
ed2dc618 4197 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
9291a0cd
TT
4198 {
4199 int j;
ed2dc618 4200 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (dwarf2_per_objfile, i);
7b9f3c50 4201 struct quick_file_names *file_data;
9291a0cd 4202
3d7bb9d9 4203 /* We only need to look at symtabs not already expanded. */
43f3e411 4204 if (per_cu->v.quick->compunit_symtab)
9291a0cd
TT
4205 continue;
4206
e4a48d9d 4207 file_data = dw2_get_file_names (per_cu);
7b9f3c50 4208 if (file_data == NULL)
9291a0cd
TT
4209 continue;
4210
7b9f3c50 4211 for (j = 0; j < file_data->num_file_names; ++j)
9291a0cd 4212 {
652a8996
JK
4213 const char *this_fullname = file_data->file_names[j];
4214
4215 if (filename_cmp (this_fullname, fullname) == 0)
9291a0cd 4216 {
a0f42c21 4217 dw2_instantiate_symtab (per_cu);
9291a0cd
TT
4218 break;
4219 }
4220 }
4221 }
4222}
4223
9291a0cd 4224static void
ade7ed9e 4225dw2_map_matching_symbols (struct objfile *objfile,
fe978cb0 4226 const char * name, domain_enum domain,
ade7ed9e 4227 int global,
40658b94
PH
4228 int (*callback) (struct block *,
4229 struct symbol *, void *),
b5ec771e 4230 void *data, symbol_name_match_type match,
2edb89d3 4231 symbol_compare_ftype *ordered_compare)
9291a0cd 4232{
40658b94 4233 /* Currently unimplemented; used for Ada. The function can be called if the
a9e6a4bb
JK
4234 current language is Ada for a non-Ada objfile using GNU index. As Ada
4235 does not look for non-Ada symbols this function should just return. */
9291a0cd
TT
4236}
4237
b5ec771e
PA
4238/* Symbol name matcher for .gdb_index names.
4239
4240 Symbol names in .gdb_index have a few particularities:
4241
4242 - There's no indication of which is the language of each symbol.
4243
4244 Since each language has its own symbol name matching algorithm,
4245 and we don't know which language is the right one, we must match
3f563c84
PA
4246 each symbol against all languages. This would be a potential
4247 performance problem if it were not mitigated by the
4248 mapped_index::name_components lookup table, which significantly
4249 reduces the number of times we need to call into this matcher,
4250 making it a non-issue.
b5ec771e
PA
4251
4252 - Symbol names in the index have no overload (parameter)
4253 information. I.e., in C++, "foo(int)" and "foo(long)" both
4254 appear as "foo" in the index, for example.
4255
4256 This means that the lookup names passed to the symbol name
4257 matcher functions must have no parameter information either
4258 because (e.g.) symbol search name "foo" does not match
4259 lookup-name "foo(int)" [while swapping search name for lookup
4260 name would match].
4261*/
4262class gdb_index_symbol_name_matcher
4263{
4264public:
4265 /* Prepares the vector of comparison functions for LOOKUP_NAME. */
4266 gdb_index_symbol_name_matcher (const lookup_name_info &lookup_name);
4267
4268 /* Walk all the matcher routines and match SYMBOL_NAME against them.
4269 Returns true if any matcher matches. */
4270 bool matches (const char *symbol_name);
4271
4272private:
4273 /* A reference to the lookup name we're matching against. */
4274 const lookup_name_info &m_lookup_name;
4275
4276 /* A vector holding all the different symbol name matchers, for all
4277 languages. */
4278 std::vector<symbol_name_matcher_ftype *> m_symbol_name_matcher_funcs;
4279};
4280
4281gdb_index_symbol_name_matcher::gdb_index_symbol_name_matcher
4282 (const lookup_name_info &lookup_name)
4283 : m_lookup_name (lookup_name)
4284{
4285 /* Prepare the vector of comparison functions upfront, to avoid
4286 doing the same work for each symbol. Care is taken to avoid
4287 matching with the same matcher more than once if/when multiple
4288 languages use the same matcher function. */
4289 auto &matchers = m_symbol_name_matcher_funcs;
4290 matchers.reserve (nr_languages);
4291
4292 matchers.push_back (default_symbol_name_matcher);
4293
4294 for (int i = 0; i < nr_languages; i++)
4295 {
4296 const language_defn *lang = language_def ((enum language) i);
c63d3e8d 4297 symbol_name_matcher_ftype *name_matcher
618daa93 4298 = get_symbol_name_matcher (lang, m_lookup_name);
c63d3e8d
PA
4299
4300 /* Don't insert the same comparison routine more than once.
4301 Note that we do this linear walk instead of a seemingly
4302 cheaper sorted insert, or use a std::set or something like
4303 that, because relative order of function addresses is not
4304 stable. This is not a problem in practice because the number
4305 of supported languages is low, and the cost here is tiny
4306 compared to the number of searches we'll do afterwards using
4307 this object. */
4308 if (name_matcher != default_symbol_name_matcher
4309 && (std::find (matchers.begin (), matchers.end (), name_matcher)
4310 == matchers.end ()))
4311 matchers.push_back (name_matcher);
b5ec771e
PA
4312 }
4313}
4314
4315bool
4316gdb_index_symbol_name_matcher::matches (const char *symbol_name)
4317{
4318 for (auto matches_name : m_symbol_name_matcher_funcs)
4319 if (matches_name (symbol_name, m_lookup_name, NULL))
4320 return true;
4321
4322 return false;
4323}
4324
e1ef7d7a
PA
4325/* Starting from a search name, return the string that finds the upper
4326 bound of all strings that start with SEARCH_NAME in a sorted name
4327 list. Returns the empty string to indicate that the upper bound is
4328 the end of the list. */
4329
4330static std::string
4331make_sort_after_prefix_name (const char *search_name)
4332{
4333 /* When looking to complete "func", we find the upper bound of all
4334 symbols that start with "func" by looking for where we'd insert
4335 the closest string that would follow "func" in lexicographical
4336 order. Usually, that's "func"-with-last-character-incremented,
4337 i.e. "fund". Mind non-ASCII characters, though. Usually those
4338 will be UTF-8 multi-byte sequences, but we can't be certain.
4339 Especially mind the 0xff character, which is a valid character in
4340 non-UTF-8 source character sets (e.g. Latin1 'ÿ'), and we can't
4341 rule out compilers allowing it in identifiers. Note that
4342 conveniently, strcmp/strcasecmp are specified to compare
4343 characters interpreted as unsigned char. So what we do is treat
4344 the whole string as a base 256 number composed of a sequence of
4345 base 256 "digits" and add 1 to it. I.e., adding 1 to 0xff wraps
4346 to 0, and carries 1 to the following more-significant position.
4347 If the very first character in SEARCH_NAME ends up incremented
4348 and carries/overflows, then the upper bound is the end of the
4349 list. The string after the empty string is also the empty
4350 string.
4351
4352 Some examples of this operation:
4353
4354 SEARCH_NAME => "+1" RESULT
4355
4356 "abc" => "abd"
4357 "ab\xff" => "ac"
4358 "\xff" "a" "\xff" => "\xff" "b"
4359 "\xff" => ""
4360 "\xff\xff" => ""
4361 "" => ""
4362
4363 Then, with these symbols for example:
4364
4365 func
4366 func1
4367 fund
4368
4369 completing "func" looks for symbols between "func" and
4370 "func"-with-last-character-incremented, i.e. "fund" (exclusive),
4371 which finds "func" and "func1", but not "fund".
4372
4373 And with:
4374
4375 funcÿ (Latin1 'ÿ' [0xff])
4376 funcÿ1
4377 fund
4378
4379 completing "funcÿ" looks for symbols between "funcÿ" and "fund"
4380 (exclusive), which finds "funcÿ" and "funcÿ1", but not "fund".
4381
4382 And with:
4383
4384 ÿÿ (Latin1 'ÿ' [0xff])
4385 ÿÿ1
4386
4387 completing "ÿ" or "ÿÿ" looks for symbols between between "ÿÿ" and
4388 the end of the list.
4389 */
4390 std::string after = search_name;
4391 while (!after.empty () && (unsigned char) after.back () == 0xff)
4392 after.pop_back ();
4393 if (!after.empty ())
4394 after.back () = (unsigned char) after.back () + 1;
4395 return after;
4396}
4397
5c58de74 4398/* See declaration. */
61d96d7e 4399
5c58de74
PA
4400std::pair<std::vector<name_component>::const_iterator,
4401 std::vector<name_component>::const_iterator>
44ed8f3e 4402mapped_index_base::find_name_components_bounds
5c58de74 4403 (const lookup_name_info &lookup_name_without_params) const
3f563c84 4404{
5c58de74
PA
4405 auto *name_cmp
4406 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
3f563c84
PA
4407
4408 const char *cplus
c62446b1 4409 = lookup_name_without_params.cplus ().lookup_name ().c_str ();
9291a0cd 4410
3f563c84
PA
4411 /* Comparison function object for lower_bound that matches against a
4412 given symbol name. */
4413 auto lookup_compare_lower = [&] (const name_component &elem,
4414 const char *name)
4415 {
5c58de74 4416 const char *elem_qualified = this->symbol_name_at (elem.idx);
3f563c84
PA
4417 const char *elem_name = elem_qualified + elem.name_offset;
4418 return name_cmp (elem_name, name) < 0;
4419 };
4420
4421 /* Comparison function object for upper_bound that matches against a
4422 given symbol name. */
4423 auto lookup_compare_upper = [&] (const char *name,
4424 const name_component &elem)
4425 {
5c58de74 4426 const char *elem_qualified = this->symbol_name_at (elem.idx);
3f563c84
PA
4427 const char *elem_name = elem_qualified + elem.name_offset;
4428 return name_cmp (name, elem_name) < 0;
4429 };
4430
5c58de74
PA
4431 auto begin = this->name_components.begin ();
4432 auto end = this->name_components.end ();
3f563c84
PA
4433
4434 /* Find the lower bound. */
4435 auto lower = [&] ()
4436 {
5c58de74 4437 if (lookup_name_without_params.completion_mode () && cplus[0] == '\0')
3f563c84
PA
4438 return begin;
4439 else
4440 return std::lower_bound (begin, end, cplus, lookup_compare_lower);
4441 } ();
4442
4443 /* Find the upper bound. */
4444 auto upper = [&] ()
4445 {
5c58de74 4446 if (lookup_name_without_params.completion_mode ())
3f563c84 4447 {
e1ef7d7a
PA
4448 /* In completion mode, we want UPPER to point past all
4449 symbols names that have the same prefix. I.e., with
4450 these symbols, and completing "func":
4451
4452 function << lower bound
4453 function1
4454 other_function << upper bound
4455
4456 We find the upper bound by looking for the insertion
4457 point of "func"-with-last-character-incremented,
4458 i.e. "fund". */
4459 std::string after = make_sort_after_prefix_name (cplus);
4460 if (after.empty ())
3f563c84 4461 return end;
e6b2f5ef
PA
4462 return std::lower_bound (lower, end, after.c_str (),
4463 lookup_compare_lower);
3f563c84
PA
4464 }
4465 else
4466 return std::upper_bound (lower, end, cplus, lookup_compare_upper);
4467 } ();
4468
5c58de74
PA
4469 return {lower, upper};
4470}
4471
4472/* See declaration. */
4473
4474void
44ed8f3e 4475mapped_index_base::build_name_components ()
5c58de74
PA
4476{
4477 if (!this->name_components.empty ())
4478 return;
4479
4480 this->name_components_casing = case_sensitivity;
4481 auto *name_cmp
4482 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
4483
4484 /* The code below only knows how to break apart components of C++
4485 symbol names (and other languages that use '::' as
4486 namespace/module separator). If we add support for wild matching
4487 to some language that uses some other operator (E.g., Ada, Go and
4488 D use '.'), then we'll need to try splitting the symbol name
4489 according to that language too. Note that Ada does support wild
4490 matching, but doesn't currently support .gdb_index. */
44ed8f3e
PA
4491 auto count = this->symbol_name_count ();
4492 for (offset_type idx = 0; idx < count; idx++)
5c58de74 4493 {
44ed8f3e 4494 if (this->symbol_name_slot_invalid (idx))
5c58de74
PA
4495 continue;
4496
4497 const char *name = this->symbol_name_at (idx);
4498
4499 /* Add each name component to the name component table. */
4500 unsigned int previous_len = 0;
4501 for (unsigned int current_len = cp_find_first_component (name);
4502 name[current_len] != '\0';
4503 current_len += cp_find_first_component (name + current_len))
4504 {
4505 gdb_assert (name[current_len] == ':');
4506 this->name_components.push_back ({previous_len, idx});
4507 /* Skip the '::'. */
4508 current_len += 2;
4509 previous_len = current_len;
4510 }
4511 this->name_components.push_back ({previous_len, idx});
4512 }
4513
4514 /* Sort name_components elements by name. */
4515 auto name_comp_compare = [&] (const name_component &left,
4516 const name_component &right)
4517 {
4518 const char *left_qualified = this->symbol_name_at (left.idx);
4519 const char *right_qualified = this->symbol_name_at (right.idx);
4520
4521 const char *left_name = left_qualified + left.name_offset;
4522 const char *right_name = right_qualified + right.name_offset;
4523
4524 return name_cmp (left_name, right_name) < 0;
4525 };
4526
4527 std::sort (this->name_components.begin (),
4528 this->name_components.end (),
4529 name_comp_compare);
4530}
4531
4532/* Helper for dw2_expand_symtabs_matching that works with a
44ed8f3e
PA
4533 mapped_index_base instead of the containing objfile. This is split
4534 to a separate function in order to be able to unit test the
4535 name_components matching using a mock mapped_index_base. For each
5c58de74 4536 symbol name that matches, calls MATCH_CALLBACK, passing it the
44ed8f3e 4537 symbol's index in the mapped_index_base symbol table. */
5c58de74
PA
4538
4539static void
4540dw2_expand_symtabs_matching_symbol
44ed8f3e 4541 (mapped_index_base &index,
5c58de74
PA
4542 const lookup_name_info &lookup_name_in,
4543 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4544 enum search_domain kind,
4545 gdb::function_view<void (offset_type)> match_callback)
4546{
4547 lookup_name_info lookup_name_without_params
4548 = lookup_name_in.make_ignore_params ();
4549 gdb_index_symbol_name_matcher lookup_name_matcher
4550 (lookup_name_without_params);
4551
4552 /* Build the symbol name component sorted vector, if we haven't
4553 yet. */
4554 index.build_name_components ();
4555
4556 auto bounds = index.find_name_components_bounds (lookup_name_without_params);
4557
3f563c84
PA
4558 /* Now for each symbol name in range, check to see if we have a name
4559 match, and if so, call the MATCH_CALLBACK callback. */
4560
4561 /* The same symbol may appear more than once in the range though.
4562 E.g., if we're looking for symbols that complete "w", and we have
4563 a symbol named "w1::w2", we'll find the two name components for
4564 that same symbol in the range. To be sure we only call the
4565 callback once per symbol, we first collect the symbol name
4566 indexes that matched in a temporary vector and ignore
4567 duplicates. */
4568 std::vector<offset_type> matches;
5c58de74 4569 matches.reserve (std::distance (bounds.first, bounds.second));
3f563c84 4570
5c58de74 4571 for (; bounds.first != bounds.second; ++bounds.first)
3f563c84 4572 {
5c58de74 4573 const char *qualified = index.symbol_name_at (bounds.first->idx);
3f563c84
PA
4574
4575 if (!lookup_name_matcher.matches (qualified)
4576 || (symbol_matcher != NULL && !symbol_matcher (qualified)))
9291a0cd
TT
4577 continue;
4578
5c58de74 4579 matches.push_back (bounds.first->idx);
3f563c84
PA
4580 }
4581
4582 std::sort (matches.begin (), matches.end ());
4583
4584 /* Finally call the callback, once per match. */
4585 ULONGEST prev = -1;
4586 for (offset_type idx : matches)
4587 {
4588 if (prev != idx)
4589 {
4590 match_callback (idx);
4591 prev = idx;
4592 }
4593 }
4594
4595 /* Above we use a type wider than idx's for 'prev', since 0 and
4596 (offset_type)-1 are both possible values. */
4597 static_assert (sizeof (prev) > sizeof (offset_type), "");
4598}
4599
c62446b1
PA
4600#if GDB_SELF_TEST
4601
4602namespace selftests { namespace dw2_expand_symtabs_matching {
4603
a3c5fafd
PA
4604/* A mock .gdb_index/.debug_names-like name index table, enough to
4605 exercise dw2_expand_symtabs_matching_symbol, which works with the
4606 mapped_index_base interface. Builds an index from the symbol list
4607 passed as parameter to the constructor. */
4608class mock_mapped_index : public mapped_index_base
c62446b1
PA
4609{
4610public:
a3c5fafd
PA
4611 mock_mapped_index (gdb::array_view<const char *> symbols)
4612 : m_symbol_table (symbols)
c62446b1
PA
4613 {}
4614
a3c5fafd 4615 DISABLE_COPY_AND_ASSIGN (mock_mapped_index);
c62446b1 4616
a3c5fafd
PA
4617 /* Return the number of names in the symbol table. */
4618 virtual size_t symbol_name_count () const
c62446b1 4619 {
a3c5fafd 4620 return m_symbol_table.size ();
c62446b1
PA
4621 }
4622
a3c5fafd
PA
4623 /* Get the name of the symbol at IDX in the symbol table. */
4624 virtual const char *symbol_name_at (offset_type idx) const
4625 {
4626 return m_symbol_table[idx];
4627 }
c62446b1 4628
a3c5fafd
PA
4629private:
4630 gdb::array_view<const char *> m_symbol_table;
c62446b1
PA
4631};
4632
4633/* Convenience function that converts a NULL pointer to a "<null>"
4634 string, to pass to print routines. */
4635
4636static const char *
4637string_or_null (const char *str)
4638{
4639 return str != NULL ? str : "<null>";
4640}
4641
4642/* Check if a lookup_name_info built from
4643 NAME/MATCH_TYPE/COMPLETION_MODE matches the symbols in the mock
4644 index. EXPECTED_LIST is the list of expected matches, in expected
4645 matching order. If no match expected, then an empty list is
4646 specified. Returns true on success. On failure prints a warning
4647 indicating the file:line that failed, and returns false. */
4648
4649static bool
4650check_match (const char *file, int line,
4651 mock_mapped_index &mock_index,
4652 const char *name, symbol_name_match_type match_type,
4653 bool completion_mode,
4654 std::initializer_list<const char *> expected_list)
4655{
4656 lookup_name_info lookup_name (name, match_type, completion_mode);
4657
4658 bool matched = true;
4659
4660 auto mismatch = [&] (const char *expected_str,
4661 const char *got)
4662 {
4663 warning (_("%s:%d: match_type=%s, looking-for=\"%s\", "
4664 "expected=\"%s\", got=\"%s\"\n"),
4665 file, line,
4666 (match_type == symbol_name_match_type::FULL
4667 ? "FULL" : "WILD"),
4668 name, string_or_null (expected_str), string_or_null (got));
4669 matched = false;
4670 };
4671
4672 auto expected_it = expected_list.begin ();
4673 auto expected_end = expected_list.end ();
4674
a3c5fafd 4675 dw2_expand_symtabs_matching_symbol (mock_index, lookup_name,
c62446b1
PA
4676 NULL, ALL_DOMAIN,
4677 [&] (offset_type idx)
4678 {
a3c5fafd 4679 const char *matched_name = mock_index.symbol_name_at (idx);
c62446b1
PA
4680 const char *expected_str
4681 = expected_it == expected_end ? NULL : *expected_it++;
4682
4683 if (expected_str == NULL || strcmp (expected_str, matched_name) != 0)
4684 mismatch (expected_str, matched_name);
4685 });
4686
4687 const char *expected_str
4688 = expected_it == expected_end ? NULL : *expected_it++;
4689 if (expected_str != NULL)
4690 mismatch (expected_str, NULL);
4691
4692 return matched;
4693}
4694
4695/* The symbols added to the mock mapped_index for testing (in
4696 canonical form). */
4697static const char *test_symbols[] = {
4698 "function",
4699 "std::bar",
4700 "std::zfunction",
4701 "std::zfunction2",
4702 "w1::w2",
4703 "ns::foo<char*>",
4704 "ns::foo<int>",
4705 "ns::foo<long>",
a20714ff
PA
4706 "ns2::tmpl<int>::foo2",
4707 "(anonymous namespace)::A::B::C",
c62446b1 4708
e1ef7d7a
PA
4709 /* These are used to check that the increment-last-char in the
4710 matching algorithm for completion doesn't match "t1_fund" when
4711 completing "t1_func". */
4712 "t1_func",
4713 "t1_func1",
4714 "t1_fund",
4715 "t1_fund1",
4716
4717 /* A UTF-8 name with multi-byte sequences to make sure that
4718 cp-name-parser understands this as a single identifier ("função"
4719 is "function" in PT). */
4720 u8"u8função",
4721
4722 /* \377 (0xff) is Latin1 'ÿ'. */
4723 "yfunc\377",
4724
4725 /* \377 (0xff) is Latin1 'ÿ'. */
4726 "\377",
4727 "\377\377123",
4728
c62446b1
PA
4729 /* A name with all sorts of complications. Starts with "z" to make
4730 it easier for the completion tests below. */
4731#define Z_SYM_NAME \
4732 "z::std::tuple<(anonymous namespace)::ui*, std::bar<(anonymous namespace)::ui> >" \
4733 "::tuple<(anonymous namespace)::ui*, " \
4734 "std::default_delete<(anonymous namespace)::ui>, void>"
4735
4736 Z_SYM_NAME
4737};
4738
a3c5fafd
PA
4739/* Returns true if the mapped_index_base::find_name_component_bounds
4740 method finds EXPECTED_SYMS in INDEX when looking for SEARCH_NAME,
4741 in completion mode. */
5c58de74
PA
4742
4743static bool
a3c5fafd 4744check_find_bounds_finds (mapped_index_base &index,
5c58de74
PA
4745 const char *search_name,
4746 gdb::array_view<const char *> expected_syms)
4747{
4748 lookup_name_info lookup_name (search_name,
4749 symbol_name_match_type::FULL, true);
4750
4751 auto bounds = index.find_name_components_bounds (lookup_name);
4752
4753 size_t distance = std::distance (bounds.first, bounds.second);
4754 if (distance != expected_syms.size ())
4755 return false;
4756
4757 for (size_t exp_elem = 0; exp_elem < distance; exp_elem++)
4758 {
4759 auto nc_elem = bounds.first + exp_elem;
4760 const char *qualified = index.symbol_name_at (nc_elem->idx);
4761 if (strcmp (qualified, expected_syms[exp_elem]) != 0)
4762 return false;
4763 }
4764
4765 return true;
4766}
4767
4768/* Test the lower-level mapped_index::find_name_component_bounds
4769 method. */
4770
c62446b1 4771static void
5c58de74
PA
4772test_mapped_index_find_name_component_bounds ()
4773{
4774 mock_mapped_index mock_index (test_symbols);
4775
a3c5fafd 4776 mock_index.build_name_components ();
5c58de74
PA
4777
4778 /* Test the lower-level mapped_index::find_name_component_bounds
4779 method in completion mode. */
4780 {
4781 static const char *expected_syms[] = {
4782 "t1_func",
4783 "t1_func1",
5c58de74
PA
4784 };
4785
a3c5fafd 4786 SELF_CHECK (check_find_bounds_finds (mock_index,
5c58de74
PA
4787 "t1_func", expected_syms));
4788 }
4789
4790 /* Check that the increment-last-char in the name matching algorithm
4791 for completion doesn't get confused with Ansi1 'ÿ' / 0xff. */
4792 {
4793 static const char *expected_syms1[] = {
4794 "\377",
4795 "\377\377123",
4796 };
a3c5fafd 4797 SELF_CHECK (check_find_bounds_finds (mock_index,
5c58de74
PA
4798 "\377", expected_syms1));
4799
4800 static const char *expected_syms2[] = {
4801 "\377\377123",
4802 };
a3c5fafd 4803 SELF_CHECK (check_find_bounds_finds (mock_index,
5c58de74
PA
4804 "\377\377", expected_syms2));
4805 }
4806}
4807
4808/* Test dw2_expand_symtabs_matching_symbol. */
4809
4810static void
4811test_dw2_expand_symtabs_matching_symbol ()
c62446b1
PA
4812{
4813 mock_mapped_index mock_index (test_symbols);
4814
4815 /* We let all tests run until the end even if some fails, for debug
4816 convenience. */
4817 bool any_mismatch = false;
4818
4819 /* Create the expected symbols list (an initializer_list). Needed
4820 because lists have commas, and we need to pass them to CHECK,
4821 which is a macro. */
4822#define EXPECT(...) { __VA_ARGS__ }
4823
4824 /* Wrapper for check_match that passes down the current
4825 __FILE__/__LINE__. */
4826#define CHECK_MATCH(NAME, MATCH_TYPE, COMPLETION_MODE, EXPECTED_LIST) \
4827 any_mismatch |= !check_match (__FILE__, __LINE__, \
4828 mock_index, \
4829 NAME, MATCH_TYPE, COMPLETION_MODE, \
4830 EXPECTED_LIST)
4831
4832 /* Identity checks. */
4833 for (const char *sym : test_symbols)
4834 {
4835 /* Should be able to match all existing symbols. */
4836 CHECK_MATCH (sym, symbol_name_match_type::FULL, false,
4837 EXPECT (sym));
4838
4839 /* Should be able to match all existing symbols with
4840 parameters. */
4841 std::string with_params = std::string (sym) + "(int)";
4842 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4843 EXPECT (sym));
4844
4845 /* Should be able to match all existing symbols with
4846 parameters and qualifiers. */
4847 with_params = std::string (sym) + " ( int ) const";
4848 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4849 EXPECT (sym));
4850
4851 /* This should really find sym, but cp-name-parser.y doesn't
4852 know about lvalue/rvalue qualifiers yet. */
4853 with_params = std::string (sym) + " ( int ) &&";
4854 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4855 {});
4856 }
4857
e1ef7d7a
PA
4858 /* Check that the name matching algorithm for completion doesn't get
4859 confused with Latin1 'ÿ' / 0xff. */
4860 {
4861 static const char str[] = "\377";
4862 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4863 EXPECT ("\377", "\377\377123"));
4864 }
4865
4866 /* Check that the increment-last-char in the matching algorithm for
4867 completion doesn't match "t1_fund" when completing "t1_func". */
4868 {
4869 static const char str[] = "t1_func";
4870 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4871 EXPECT ("t1_func", "t1_func1"));
4872 }
4873
c62446b1
PA
4874 /* Check that completion mode works at each prefix of the expected
4875 symbol name. */
4876 {
4877 static const char str[] = "function(int)";
4878 size_t len = strlen (str);
4879 std::string lookup;
4880
4881 for (size_t i = 1; i < len; i++)
4882 {
4883 lookup.assign (str, i);
4884 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4885 EXPECT ("function"));
4886 }
4887 }
4888
4889 /* While "w" is a prefix of both components, the match function
4890 should still only be called once. */
4891 {
4892 CHECK_MATCH ("w", symbol_name_match_type::FULL, true,
4893 EXPECT ("w1::w2"));
a20714ff
PA
4894 CHECK_MATCH ("w", symbol_name_match_type::WILD, true,
4895 EXPECT ("w1::w2"));
c62446b1
PA
4896 }
4897
4898 /* Same, with a "complicated" symbol. */
4899 {
4900 static const char str[] = Z_SYM_NAME;
4901 size_t len = strlen (str);
4902 std::string lookup;
4903
4904 for (size_t i = 1; i < len; i++)
4905 {
4906 lookup.assign (str, i);
4907 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4908 EXPECT (Z_SYM_NAME));
4909 }
4910 }
4911
4912 /* In FULL mode, an incomplete symbol doesn't match. */
4913 {
4914 CHECK_MATCH ("std::zfunction(int", symbol_name_match_type::FULL, false,
4915 {});
4916 }
4917
4918 /* A complete symbol with parameters matches any overload, since the
4919 index has no overload info. */
4920 {
4921 CHECK_MATCH ("std::zfunction(int)", symbol_name_match_type::FULL, true,
4922 EXPECT ("std::zfunction", "std::zfunction2"));
a20714ff
PA
4923 CHECK_MATCH ("zfunction(int)", symbol_name_match_type::WILD, true,
4924 EXPECT ("std::zfunction", "std::zfunction2"));
4925 CHECK_MATCH ("zfunc", symbol_name_match_type::WILD, true,
4926 EXPECT ("std::zfunction", "std::zfunction2"));
c62446b1
PA
4927 }
4928
4929 /* Check that whitespace is ignored appropriately. A symbol with a
4930 template argument list. */
4931 {
4932 static const char expected[] = "ns::foo<int>";
4933 CHECK_MATCH ("ns :: foo < int > ", symbol_name_match_type::FULL, false,
4934 EXPECT (expected));
a20714ff
PA
4935 CHECK_MATCH ("foo < int > ", symbol_name_match_type::WILD, false,
4936 EXPECT (expected));
c62446b1
PA
4937 }
4938
4939 /* Check that whitespace is ignored appropriately. A symbol with a
4940 template argument list that includes a pointer. */
4941 {
4942 static const char expected[] = "ns::foo<char*>";
4943 /* Try both completion and non-completion modes. */
4944 static const bool completion_mode[2] = {false, true};
4945 for (size_t i = 0; i < 2; i++)
4946 {
4947 CHECK_MATCH ("ns :: foo < char * >", symbol_name_match_type::FULL,
4948 completion_mode[i], EXPECT (expected));
a20714ff
PA
4949 CHECK_MATCH ("foo < char * >", symbol_name_match_type::WILD,
4950 completion_mode[i], EXPECT (expected));
c62446b1
PA
4951
4952 CHECK_MATCH ("ns :: foo < char * > (int)", symbol_name_match_type::FULL,
4953 completion_mode[i], EXPECT (expected));
a20714ff
PA
4954 CHECK_MATCH ("foo < char * > (int)", symbol_name_match_type::WILD,
4955 completion_mode[i], EXPECT (expected));
c62446b1
PA
4956 }
4957 }
4958
4959 {
4960 /* Check method qualifiers are ignored. */
4961 static const char expected[] = "ns::foo<char*>";
4962 CHECK_MATCH ("ns :: foo < char * > ( int ) const",
4963 symbol_name_match_type::FULL, true, EXPECT (expected));
4964 CHECK_MATCH ("ns :: foo < char * > ( int ) &&",
4965 symbol_name_match_type::FULL, true, EXPECT (expected));
a20714ff
PA
4966 CHECK_MATCH ("foo < char * > ( int ) const",
4967 symbol_name_match_type::WILD, true, EXPECT (expected));
4968 CHECK_MATCH ("foo < char * > ( int ) &&",
4969 symbol_name_match_type::WILD, true, EXPECT (expected));
c62446b1
PA
4970 }
4971
4972 /* Test lookup names that don't match anything. */
4973 {
a20714ff
PA
4974 CHECK_MATCH ("bar2", symbol_name_match_type::WILD, false,
4975 {});
4976
c62446b1
PA
4977 CHECK_MATCH ("doesntexist", symbol_name_match_type::FULL, false,
4978 {});
4979 }
4980
a20714ff
PA
4981 /* Some wild matching tests, exercising "(anonymous namespace)",
4982 which should not be confused with a parameter list. */
4983 {
4984 static const char *syms[] = {
4985 "A::B::C",
4986 "B::C",
4987 "C",
4988 "A :: B :: C ( int )",
4989 "B :: C ( int )",
4990 "C ( int )",
4991 };
4992
4993 for (const char *s : syms)
4994 {
4995 CHECK_MATCH (s, symbol_name_match_type::WILD, false,
4996 EXPECT ("(anonymous namespace)::A::B::C"));
4997 }
4998 }
4999
5000 {
5001 static const char expected[] = "ns2::tmpl<int>::foo2";
5002 CHECK_MATCH ("tmp", symbol_name_match_type::WILD, true,
5003 EXPECT (expected));
5004 CHECK_MATCH ("tmpl<", symbol_name_match_type::WILD, true,
5005 EXPECT (expected));
5006 }
5007
c62446b1
PA
5008 SELF_CHECK (!any_mismatch);
5009
5010#undef EXPECT
5011#undef CHECK_MATCH
5012}
5013
5c58de74
PA
5014static void
5015run_test ()
5016{
5017 test_mapped_index_find_name_component_bounds ();
5018 test_dw2_expand_symtabs_matching_symbol ();
5019}
5020
c62446b1
PA
5021}} // namespace selftests::dw2_expand_symtabs_matching
5022
5023#endif /* GDB_SELF_TEST */
5024
4b514bc8
JK
5025/* If FILE_MATCHER is NULL or if PER_CU has
5026 dwarf2_per_cu_quick_data::MARK set (see
5027 dw_expand_symtabs_matching_file_matcher), expand the CU and call
5028 EXPANSION_NOTIFY on it. */
5029
5030static void
5031dw2_expand_symtabs_matching_one
5032 (struct dwarf2_per_cu_data *per_cu,
5033 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
5034 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify)
5035{
5036 if (file_matcher == NULL || per_cu->v.quick->mark)
5037 {
5038 bool symtab_was_null
5039 = (per_cu->v.quick->compunit_symtab == NULL);
5040
5041 dw2_instantiate_symtab (per_cu);
5042
5043 if (expansion_notify != NULL
5044 && symtab_was_null
5045 && per_cu->v.quick->compunit_symtab != NULL)
5046 expansion_notify (per_cu->v.quick->compunit_symtab);
5047 }
5048}
5049
3f563c84
PA
5050/* Helper for dw2_expand_matching symtabs. Called on each symbol
5051 matched, to expand corresponding CUs that were marked. IDX is the
5052 index of the symbol name that matched. */
5053
5054static void
5055dw2_expand_marked_cus
ed2dc618 5056 (struct dwarf2_per_objfile *dwarf2_per_objfile, offset_type idx,
3f563c84
PA
5057 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
5058 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
5059 search_domain kind)
5060{
3f563c84
PA
5061 offset_type *vec, vec_len, vec_idx;
5062 bool global_seen = false;
ed2dc618 5063 mapped_index &index = *dwarf2_per_objfile->index_table;
3f563c84 5064
61920122 5065 vec = (offset_type *) (index.constant_pool
f00a2de2 5066 + MAYBE_SWAP (index.symbol_table[idx].vec));
61920122
PA
5067 vec_len = MAYBE_SWAP (vec[0]);
5068 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
5069 {
5070 struct dwarf2_per_cu_data *per_cu;
5071 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[vec_idx + 1]);
5072 /* This value is only valid for index versions >= 7. */
5073 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
5074 gdb_index_symbol_kind symbol_kind =
5075 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
5076 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
5077 /* Only check the symbol attributes if they're present.
5078 Indices prior to version 7 don't record them,
5079 and indices >= 7 may elide them for certain symbols
5080 (gold does this). */
5081 int attrs_valid =
5082 (index.version >= 7
5083 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
5084
5085 /* Work around gold/15646. */
5086 if (attrs_valid)
9291a0cd 5087 {
61920122
PA
5088 if (!is_static && global_seen)
5089 continue;
5090 if (!is_static)
5091 global_seen = true;
5092 }
3190f0c6 5093
61920122
PA
5094 /* Only check the symbol's kind if it has one. */
5095 if (attrs_valid)
5096 {
5097 switch (kind)
8943b874 5098 {
61920122
PA
5099 case VARIABLES_DOMAIN:
5100 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
5101 continue;
5102 break;
5103 case FUNCTIONS_DOMAIN:
5104 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
8943b874 5105 continue;
61920122
PA
5106 break;
5107 case TYPES_DOMAIN:
5108 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
5109 continue;
5110 break;
5111 default:
5112 break;
8943b874 5113 }
61920122 5114 }
8943b874 5115
61920122
PA
5116 /* Don't crash on bad data. */
5117 if (cu_index >= (dwarf2_per_objfile->n_comp_units
5118 + dwarf2_per_objfile->n_type_units))
5119 {
5120 complaint (&symfile_complaints,
5121 _(".gdb_index entry has bad CU index"
ed2dc618
SM
5122 " [in module %s]"),
5123 objfile_name (dwarf2_per_objfile->objfile));
61920122
PA
5124 continue;
5125 }
5126
ed2dc618 5127 per_cu = dw2_get_cutu (dwarf2_per_objfile, cu_index);
4b514bc8
JK
5128 dw2_expand_symtabs_matching_one (per_cu, file_matcher,
5129 expansion_notify);
61920122
PA
5130 }
5131}
5132
4b514bc8
JK
5133/* If FILE_MATCHER is non-NULL, set all the
5134 dwarf2_per_cu_quick_data::MARK of the current DWARF2_PER_OBJFILE
5135 that match FILE_MATCHER. */
5136
61920122 5137static void
4b514bc8 5138dw_expand_symtabs_matching_file_matcher
ed2dc618
SM
5139 (struct dwarf2_per_objfile *dwarf2_per_objfile,
5140 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher)
61920122 5141{
4b514bc8 5142 if (file_matcher == NULL)
61920122
PA
5143 return;
5144
4b514bc8
JK
5145 objfile *const objfile = dwarf2_per_objfile->objfile;
5146
5147 htab_up visited_found (htab_create_alloc (10, htab_hash_pointer,
5148 htab_eq_pointer,
5149 NULL, xcalloc, xfree));
5150 htab_up visited_not_found (htab_create_alloc (10, htab_hash_pointer,
61920122
PA
5151 htab_eq_pointer,
5152 NULL, xcalloc, xfree));
61920122 5153
4b514bc8
JK
5154 /* The rule is CUs specify all the files, including those used by
5155 any TU, so there's no need to scan TUs here. */
61920122 5156
927aa2e7
JK
5157 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
5158 {
5159 int j;
ed2dc618 5160 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (dwarf2_per_objfile, i);
927aa2e7
JK
5161 struct quick_file_names *file_data;
5162 void **slot;
5163
5164 QUIT;
5165
5166 per_cu->v.quick->mark = 0;
5167
5168 /* We only need to look at symtabs not already expanded. */
5169 if (per_cu->v.quick->compunit_symtab)
5170 continue;
5171
5172 file_data = dw2_get_file_names (per_cu);
5173 if (file_data == NULL)
5174 continue;
5175
5176 if (htab_find (visited_not_found.get (), file_data) != NULL)
5177 continue;
5178 else if (htab_find (visited_found.get (), file_data) != NULL)
5179 {
5180 per_cu->v.quick->mark = 1;
5181 continue;
5182 }
5183
5184 for (j = 0; j < file_data->num_file_names; ++j)
5185 {
5186 const char *this_real_name;
5187
5188 if (file_matcher (file_data->file_names[j], false))
5189 {
5190 per_cu->v.quick->mark = 1;
5191 break;
5192 }
5193
5194 /* Before we invoke realpath, which can get expensive when many
5195 files are involved, do a quick comparison of the basenames. */
5196 if (!basenames_may_differ
5197 && !file_matcher (lbasename (file_data->file_names[j]),
5198 true))
5199 continue;
5200
5201 this_real_name = dw2_get_real_path (objfile, file_data, j);
5202 if (file_matcher (this_real_name, false))
5203 {
5204 per_cu->v.quick->mark = 1;
5205 break;
5206 }
5207 }
5208
5209 slot = htab_find_slot (per_cu->v.quick->mark
5210 ? visited_found.get ()
5211 : visited_not_found.get (),
5212 file_data, INSERT);
5213 *slot = file_data;
5214 }
5215}
5216
5217static void
5218dw2_expand_symtabs_matching
5219 (struct objfile *objfile,
5220 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
5221 const lookup_name_info &lookup_name,
5222 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
5223 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
5224 enum search_domain kind)
5225{
ed2dc618
SM
5226 struct dwarf2_per_objfile *dwarf2_per_objfile
5227 = get_dwarf2_per_objfile (objfile);
927aa2e7
JK
5228
5229 /* index_table is NULL if OBJF_READNOW. */
5230 if (!dwarf2_per_objfile->index_table)
5231 return;
5232
ed2dc618 5233 dw_expand_symtabs_matching_file_matcher (dwarf2_per_objfile, file_matcher);
927aa2e7
JK
5234
5235 mapped_index &index = *dwarf2_per_objfile->index_table;
5236
5237 dw2_expand_symtabs_matching_symbol (index, lookup_name,
5238 symbol_matcher,
5239 kind, [&] (offset_type idx)
5240 {
ed2dc618 5241 dw2_expand_marked_cus (dwarf2_per_objfile, idx, file_matcher,
927aa2e7
JK
5242 expansion_notify, kind);
5243 });
5244}
5245
5246/* A helper for dw2_find_pc_sect_compunit_symtab which finds the most specific
5247 symtab. */
5248
5249static struct compunit_symtab *
5250recursively_find_pc_sect_compunit_symtab (struct compunit_symtab *cust,
5251 CORE_ADDR pc)
5252{
5253 int i;
5254
5255 if (COMPUNIT_BLOCKVECTOR (cust) != NULL
5256 && blockvector_contains_pc (COMPUNIT_BLOCKVECTOR (cust), pc))
5257 return cust;
5258
5259 if (cust->includes == NULL)
5260 return NULL;
5261
5262 for (i = 0; cust->includes[i]; ++i)
5263 {
5264 struct compunit_symtab *s = cust->includes[i];
5265
5266 s = recursively_find_pc_sect_compunit_symtab (s, pc);
5267 if (s != NULL)
5268 return s;
5269 }
5270
5271 return NULL;
5272}
5273
5274static struct compunit_symtab *
5275dw2_find_pc_sect_compunit_symtab (struct objfile *objfile,
5276 struct bound_minimal_symbol msymbol,
5277 CORE_ADDR pc,
5278 struct obj_section *section,
5279 int warn_if_readin)
5280{
5281 struct dwarf2_per_cu_data *data;
5282 struct compunit_symtab *result;
5283
927aa2e7
JK
5284 if (!objfile->psymtabs_addrmap)
5285 return NULL;
5286
5287 data = (struct dwarf2_per_cu_data *) addrmap_find (objfile->psymtabs_addrmap,
5288 pc);
5289 if (!data)
5290 return NULL;
5291
5292 if (warn_if_readin && data->v.quick->compunit_symtab)
5293 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
5294 paddress (get_objfile_arch (objfile), pc));
5295
5296 result
5297 = recursively_find_pc_sect_compunit_symtab (dw2_instantiate_symtab (data),
5298 pc);
5299 gdb_assert (result != NULL);
5300 return result;
5301}
5302
5303static void
5304dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
5305 void *data, int need_fullname)
5306{
ed2dc618
SM
5307 struct dwarf2_per_objfile *dwarf2_per_objfile
5308 = get_dwarf2_per_objfile (objfile);
927aa2e7
JK
5309
5310 if (!dwarf2_per_objfile->filenames_cache)
5311 {
5312 dwarf2_per_objfile->filenames_cache.emplace ();
5313
5314 htab_up visited (htab_create_alloc (10,
5315 htab_hash_pointer, htab_eq_pointer,
5316 NULL, xcalloc, xfree));
5317
5318 /* The rule is CUs specify all the files, including those used
5319 by any TU, so there's no need to scan TUs here. We can
5320 ignore file names coming from already-expanded CUs. */
5321
5322 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
5323 {
ed2dc618 5324 dwarf2_per_cu_data *per_cu = dw2_get_cutu (dwarf2_per_objfile, i);
927aa2e7
JK
5325
5326 if (per_cu->v.quick->compunit_symtab)
5327 {
5328 void **slot = htab_find_slot (visited.get (),
5329 per_cu->v.quick->file_names,
5330 INSERT);
5331
5332 *slot = per_cu->v.quick->file_names;
5333 }
5334 }
5335
5336 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
5337 {
ed2dc618 5338 dwarf2_per_cu_data *per_cu = dw2_get_cu (dwarf2_per_objfile, i);
927aa2e7
JK
5339 struct quick_file_names *file_data;
5340 void **slot;
5341
5342 /* We only need to look at symtabs not already expanded. */
5343 if (per_cu->v.quick->compunit_symtab)
5344 continue;
5345
5346 file_data = dw2_get_file_names (per_cu);
5347 if (file_data == NULL)
5348 continue;
5349
5350 slot = htab_find_slot (visited.get (), file_data, INSERT);
5351 if (*slot)
5352 {
5353 /* Already visited. */
5354 continue;
5355 }
5356 *slot = file_data;
5357
5358 for (int j = 0; j < file_data->num_file_names; ++j)
5359 {
5360 const char *filename = file_data->file_names[j];
5361 dwarf2_per_objfile->filenames_cache->seen (filename);
5362 }
5363 }
5364 }
5365
5366 dwarf2_per_objfile->filenames_cache->traverse ([&] (const char *filename)
5367 {
5368 gdb::unique_xmalloc_ptr<char> this_real_name;
5369
5370 if (need_fullname)
5371 this_real_name = gdb_realpath (filename);
5372 (*fun) (filename, this_real_name.get (), data);
5373 });
5374}
5375
5376static int
5377dw2_has_symbols (struct objfile *objfile)
5378{
5379 return 1;
5380}
5381
5382const struct quick_symbol_functions dwarf2_gdb_index_functions =
5383{
5384 dw2_has_symbols,
5385 dw2_find_last_source_symtab,
5386 dw2_forget_cached_source_info,
5387 dw2_map_symtabs_matching_filename,
5388 dw2_lookup_symbol,
5389 dw2_print_stats,
5390 dw2_dump,
5391 dw2_relocate,
5392 dw2_expand_symtabs_for_function,
5393 dw2_expand_all_symtabs,
5394 dw2_expand_symtabs_with_fullname,
5395 dw2_map_matching_symbols,
5396 dw2_expand_symtabs_matching,
5397 dw2_find_pc_sect_compunit_symtab,
5398 NULL,
5399 dw2_map_symbol_filenames
5400};
5401
5402/* DWARF-5 debug_names reader. */
5403
5404/* DWARF-5 augmentation string for GDB's DW_IDX_GNU_* extension. */
5405static const gdb_byte dwarf5_augmentation[] = { 'G', 'D', 'B', 0 };
5406
5407/* A helper function that reads the .debug_names section in SECTION
5408 and fills in MAP. FILENAME is the name of the file containing the
5409 section; it is used for error reporting.
5410
5411 Returns true if all went well, false otherwise. */
5412
5413static bool
5414read_debug_names_from_section (struct objfile *objfile,
5415 const char *filename,
5416 struct dwarf2_section_info *section,
5417 mapped_debug_names &map)
5418{
5419 if (dwarf2_section_empty_p (section))
5420 return false;
5421
5422 /* Older elfutils strip versions could keep the section in the main
5423 executable while splitting it for the separate debug info file. */
5424 if ((get_section_flags (section) & SEC_HAS_CONTENTS) == 0)
5425 return false;
5426
5427 dwarf2_read_section (objfile, section);
5428
5429 map.dwarf5_byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
5430
5431 const gdb_byte *addr = section->buffer;
5432
5433 bfd *const abfd = get_section_bfd_owner (section);
5434
5435 unsigned int bytes_read;
5436 LONGEST length = read_initial_length (abfd, addr, &bytes_read);
5437 addr += bytes_read;
5438
5439 map.dwarf5_is_dwarf64 = bytes_read != 4;
5440 map.offset_size = map.dwarf5_is_dwarf64 ? 8 : 4;
5441 if (bytes_read + length != section->size)
5442 {
5443 /* There may be multiple per-CU indices. */
5444 warning (_("Section .debug_names in %s length %s does not match "
5445 "section length %s, ignoring .debug_names."),
5446 filename, plongest (bytes_read + length),
5447 pulongest (section->size));
5448 return false;
5449 }
5450
5451 /* The version number. */
5452 uint16_t version = read_2_bytes (abfd, addr);
5453 addr += 2;
5454 if (version != 5)
5455 {
5456 warning (_("Section .debug_names in %s has unsupported version %d, "
5457 "ignoring .debug_names."),
5458 filename, version);
5459 return false;
5460 }
5461
5462 /* Padding. */
5463 uint16_t padding = read_2_bytes (abfd, addr);
5464 addr += 2;
5465 if (padding != 0)
5466 {
5467 warning (_("Section .debug_names in %s has unsupported padding %d, "
5468 "ignoring .debug_names."),
5469 filename, padding);
5470 return false;
5471 }
5472
5473 /* comp_unit_count - The number of CUs in the CU list. */
5474 map.cu_count = read_4_bytes (abfd, addr);
5475 addr += 4;
5476
5477 /* local_type_unit_count - The number of TUs in the local TU
5478 list. */
5479 map.tu_count = read_4_bytes (abfd, addr);
5480 addr += 4;
5481
5482 /* foreign_type_unit_count - The number of TUs in the foreign TU
5483 list. */
5484 uint32_t foreign_tu_count = read_4_bytes (abfd, addr);
5485 addr += 4;
5486 if (foreign_tu_count != 0)
5487 {
5488 warning (_("Section .debug_names in %s has unsupported %lu foreign TUs, "
5489 "ignoring .debug_names."),
5490 filename, static_cast<unsigned long> (foreign_tu_count));
5491 return false;
5492 }
5493
5494 /* bucket_count - The number of hash buckets in the hash lookup
5495 table. */
5496 map.bucket_count = read_4_bytes (abfd, addr);
5497 addr += 4;
5498
5499 /* name_count - The number of unique names in the index. */
5500 map.name_count = read_4_bytes (abfd, addr);
5501 addr += 4;
5502
5503 /* abbrev_table_size - The size in bytes of the abbreviations
5504 table. */
5505 uint32_t abbrev_table_size = read_4_bytes (abfd, addr);
5506 addr += 4;
5507
5508 /* augmentation_string_size - The size in bytes of the augmentation
5509 string. This value is rounded up to a multiple of 4. */
5510 uint32_t augmentation_string_size = read_4_bytes (abfd, addr);
5511 addr += 4;
5512 map.augmentation_is_gdb = ((augmentation_string_size
5513 == sizeof (dwarf5_augmentation))
5514 && memcmp (addr, dwarf5_augmentation,
5515 sizeof (dwarf5_augmentation)) == 0);
5516 augmentation_string_size += (-augmentation_string_size) & 3;
5517 addr += augmentation_string_size;
5518
5519 /* List of CUs */
5520 map.cu_table_reordered = addr;
5521 addr += map.cu_count * map.offset_size;
5522
5523 /* List of Local TUs */
5524 map.tu_table_reordered = addr;
5525 addr += map.tu_count * map.offset_size;
5526
5527 /* Hash Lookup Table */
5528 map.bucket_table_reordered = reinterpret_cast<const uint32_t *> (addr);
5529 addr += map.bucket_count * 4;
5530 map.hash_table_reordered = reinterpret_cast<const uint32_t *> (addr);
5531 addr += map.name_count * 4;
5532
5533 /* Name Table */
5534 map.name_table_string_offs_reordered = addr;
5535 addr += map.name_count * map.offset_size;
5536 map.name_table_entry_offs_reordered = addr;
5537 addr += map.name_count * map.offset_size;
5538
5539 const gdb_byte *abbrev_table_start = addr;
5540 for (;;)
5541 {
5542 unsigned int bytes_read;
5543 const ULONGEST index_num = read_unsigned_leb128 (abfd, addr, &bytes_read);
5544 addr += bytes_read;
5545 if (index_num == 0)
5546 break;
5547
5548 const auto insertpair
5549 = map.abbrev_map.emplace (index_num, mapped_debug_names::index_val ());
5550 if (!insertpair.second)
5551 {
5552 warning (_("Section .debug_names in %s has duplicate index %s, "
5553 "ignoring .debug_names."),
5554 filename, pulongest (index_num));
5555 return false;
5556 }
5557 mapped_debug_names::index_val &indexval = insertpair.first->second;
5558 indexval.dwarf_tag = read_unsigned_leb128 (abfd, addr, &bytes_read);
5559 addr += bytes_read;
5560
5561 for (;;)
5562 {
5563 mapped_debug_names::index_val::attr attr;
5564 attr.dw_idx = read_unsigned_leb128 (abfd, addr, &bytes_read);
5565 addr += bytes_read;
5566 attr.form = read_unsigned_leb128 (abfd, addr, &bytes_read);
5567 addr += bytes_read;
5568 if (attr.form == DW_FORM_implicit_const)
5569 {
5570 attr.implicit_const = read_signed_leb128 (abfd, addr,
5571 &bytes_read);
5572 addr += bytes_read;
5573 }
5574 if (attr.dw_idx == 0 && attr.form == 0)
5575 break;
5576 indexval.attr_vec.push_back (std::move (attr));
5577 }
5578 }
5579 if (addr != abbrev_table_start + abbrev_table_size)
5580 {
5581 warning (_("Section .debug_names in %s has abbreviation_table "
5582 "of size %zu vs. written as %u, ignoring .debug_names."),
5583 filename, addr - abbrev_table_start, abbrev_table_size);
5584 return false;
5585 }
5586 map.entry_pool = addr;
5587
5588 return true;
5589}
5590
5591/* A helper for create_cus_from_debug_names that handles the MAP's CU
5592 list. */
5593
5594static void
ed2dc618 5595create_cus_from_debug_names_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
927aa2e7
JK
5596 const mapped_debug_names &map,
5597 dwarf2_section_info &section,
5598 bool is_dwz, int base_offset)
5599{
5600 sect_offset sect_off_prev;
5601 for (uint32_t i = 0; i <= map.cu_count; ++i)
5602 {
5603 sect_offset sect_off_next;
5604 if (i < map.cu_count)
5605 {
5606 sect_off_next
5607 = (sect_offset) (extract_unsigned_integer
5608 (map.cu_table_reordered + i * map.offset_size,
5609 map.offset_size,
5610 map.dwarf5_byte_order));
5611 }
5612 else
5613 sect_off_next = (sect_offset) section.size;
5614 if (i >= 1)
5615 {
5616 const ULONGEST length = sect_off_next - sect_off_prev;
5617 dwarf2_per_objfile->all_comp_units[base_offset + (i - 1)]
ed2dc618 5618 = create_cu_from_index_list (dwarf2_per_objfile, &section, is_dwz,
927aa2e7
JK
5619 sect_off_prev, length);
5620 }
5621 sect_off_prev = sect_off_next;
5622 }
5623}
5624
5625/* Read the CU list from the mapped index, and use it to create all
ed2dc618 5626 the CU objects for this dwarf2_per_objfile. */
927aa2e7
JK
5627
5628static void
ed2dc618 5629create_cus_from_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile,
927aa2e7
JK
5630 const mapped_debug_names &map,
5631 const mapped_debug_names &dwz_map)
5632{
ed2dc618 5633 struct objfile *objfile = dwarf2_per_objfile->objfile;
927aa2e7
JK
5634
5635 dwarf2_per_objfile->n_comp_units = map.cu_count + dwz_map.cu_count;
5636 dwarf2_per_objfile->all_comp_units
5637 = XOBNEWVEC (&objfile->objfile_obstack, struct dwarf2_per_cu_data *,
5638 dwarf2_per_objfile->n_comp_units);
5639
ed2dc618
SM
5640 create_cus_from_debug_names_list (dwarf2_per_objfile, map,
5641 dwarf2_per_objfile->info,
927aa2e7
JK
5642 false /* is_dwz */,
5643 0 /* base_offset */);
5644
5645 if (dwz_map.cu_count == 0)
5646 return;
5647
ed2dc618
SM
5648 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
5649 create_cus_from_debug_names_list (dwarf2_per_objfile, dwz_map, dwz->info,
927aa2e7
JK
5650 true /* is_dwz */,
5651 map.cu_count /* base_offset */);
5652}
5653
5654/* Read .debug_names. If everything went ok, initialize the "quick"
5655 elements of all the CUs and return true. Otherwise, return false. */
5656
5657static bool
ed2dc618 5658dwarf2_read_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile)
927aa2e7 5659{
ed2dc618
SM
5660 mapped_debug_names local_map (dwarf2_per_objfile);
5661 mapped_debug_names dwz_map (dwarf2_per_objfile);
5662 struct objfile *objfile = dwarf2_per_objfile->objfile;
927aa2e7
JK
5663
5664 if (!read_debug_names_from_section (objfile, objfile_name (objfile),
5665 &dwarf2_per_objfile->debug_names,
5666 local_map))
5667 return false;
5668
5669 /* Don't use the index if it's empty. */
5670 if (local_map.name_count == 0)
5671 return false;
5672
5673 /* If there is a .dwz file, read it so we can get its CU list as
5674 well. */
ed2dc618 5675 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
927aa2e7
JK
5676 if (dwz != NULL)
5677 {
5678 if (!read_debug_names_from_section (objfile,
5679 bfd_get_filename (dwz->dwz_bfd),
5680 &dwz->debug_names, dwz_map))
5681 {
5682 warning (_("could not read '.debug_names' section from %s; skipping"),
5683 bfd_get_filename (dwz->dwz_bfd));
5684 return false;
5685 }
5686 }
5687
ed2dc618 5688 create_cus_from_debug_names (dwarf2_per_objfile, local_map, dwz_map);
927aa2e7
JK
5689
5690 if (local_map.tu_count != 0)
5691 {
5692 /* We can only handle a single .debug_types when we have an
5693 index. */
5694 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) != 1)
5695 return false;
5696
5697 dwarf2_section_info *section = VEC_index (dwarf2_section_info_def,
5698 dwarf2_per_objfile->types, 0);
5699
5700 create_signatured_type_table_from_debug_names
ed2dc618 5701 (dwarf2_per_objfile, local_map, section, &dwarf2_per_objfile->abbrev);
927aa2e7
JK
5702 }
5703
ed2dc618
SM
5704 create_addrmap_from_aranges (dwarf2_per_objfile,
5705 &dwarf2_per_objfile->debug_aranges);
927aa2e7 5706
ed2dc618
SM
5707 dwarf2_per_objfile->debug_names_table.reset
5708 (new mapped_debug_names (dwarf2_per_objfile));
927aa2e7
JK
5709 *dwarf2_per_objfile->debug_names_table = std::move (local_map);
5710 dwarf2_per_objfile->using_index = 1;
5711 dwarf2_per_objfile->quick_file_names_table =
5712 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
5713
5714 return true;
5715}
5716
927aa2e7
JK
5717/* Type used to manage iterating over all CUs looking for a symbol for
5718 .debug_names. */
5719
5720class dw2_debug_names_iterator
5721{
5722public:
5723 /* If WANT_SPECIFIC_BLOCK is true, only look for symbols in block
5724 BLOCK_INDEX. Otherwise BLOCK_INDEX is ignored. */
5725 dw2_debug_names_iterator (const mapped_debug_names &map,
5726 bool want_specific_block,
5727 block_enum block_index, domain_enum domain,
5728 const char *name)
5729 : m_map (map), m_want_specific_block (want_specific_block),
5730 m_block_index (block_index), m_domain (domain),
5731 m_addr (find_vec_in_debug_names (map, name))
5732 {}
5733
5734 dw2_debug_names_iterator (const mapped_debug_names &map,
5735 search_domain search, uint32_t namei)
5736 : m_map (map),
5737 m_search (search),
5738 m_addr (find_vec_in_debug_names (map, namei))
5739 {}
5740
5741 /* Return the next matching CU or NULL if there are no more. */
5742 dwarf2_per_cu_data *next ();
5743
5744private:
5745 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5746 const char *name);
5747 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5748 uint32_t namei);
5749
5750 /* The internalized form of .debug_names. */
5751 const mapped_debug_names &m_map;
5752
5753 /* If true, only look for symbols that match BLOCK_INDEX. */
5754 const bool m_want_specific_block = false;
5755
5756 /* One of GLOBAL_BLOCK or STATIC_BLOCK.
5757 Unused if !WANT_SPECIFIC_BLOCK - FIRST_LOCAL_BLOCK is an invalid
5758 value. */
5759 const block_enum m_block_index = FIRST_LOCAL_BLOCK;
5760
5761 /* The kind of symbol we're looking for. */
5762 const domain_enum m_domain = UNDEF_DOMAIN;
5763 const search_domain m_search = ALL_DOMAIN;
5764
5765 /* The list of CUs from the index entry of the symbol, or NULL if
5766 not found. */
5767 const gdb_byte *m_addr;
5768};
5769
5770const char *
5771mapped_debug_names::namei_to_name (uint32_t namei) const
5772{
5773 const ULONGEST namei_string_offs
5774 = extract_unsigned_integer ((name_table_string_offs_reordered
5775 + namei * offset_size),
5776 offset_size,
5777 dwarf5_byte_order);
5778 return read_indirect_string_at_offset
ed2dc618 5779 (dwarf2_per_objfile, dwarf2_per_objfile->objfile->obfd, namei_string_offs);
927aa2e7
JK
5780}
5781
5782/* Find a slot in .debug_names for the object named NAME. If NAME is
5783 found, return pointer to its pool data. If NAME cannot be found,
5784 return NULL. */
5785
5786const gdb_byte *
5787dw2_debug_names_iterator::find_vec_in_debug_names
5788 (const mapped_debug_names &map, const char *name)
5789{
5790 int (*cmp) (const char *, const char *);
5791
5792 if (current_language->la_language == language_cplus
5793 || current_language->la_language == language_fortran
5794 || current_language->la_language == language_d)
5795 {
5796 /* NAME is already canonical. Drop any qualifiers as
5797 .debug_names does not contain any. */
5798
5799 if (strchr (name, '(') != NULL)
5800 {
5801 gdb::unique_xmalloc_ptr<char> without_params
5802 = cp_remove_params (name);
5803
5804 if (without_params != NULL)
5805 {
5806 name = without_params.get();
5807 }
5808 }
5809 }
5810
5811 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
5812
5813 const uint32_t full_hash = dwarf5_djb_hash (name);
5814 uint32_t namei
5815 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5816 (map.bucket_table_reordered
5817 + (full_hash % map.bucket_count)), 4,
5818 map.dwarf5_byte_order);
5819 if (namei == 0)
5820 return NULL;
5821 --namei;
5822 if (namei >= map.name_count)
5823 {
5824 complaint (&symfile_complaints,
5825 _("Wrong .debug_names with name index %u but name_count=%u "
5826 "[in module %s]"),
5827 namei, map.name_count,
ed2dc618 5828 objfile_name (map.dwarf2_per_objfile->objfile));
927aa2e7
JK
5829 return NULL;
5830 }
5831
5832 for (;;)
5833 {
5834 const uint32_t namei_full_hash
5835 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5836 (map.hash_table_reordered + namei), 4,
5837 map.dwarf5_byte_order);
5838 if (full_hash % map.bucket_count != namei_full_hash % map.bucket_count)
5839 return NULL;
5840
5841 if (full_hash == namei_full_hash)
5842 {
5843 const char *const namei_string = map.namei_to_name (namei);
5844
5845#if 0 /* An expensive sanity check. */
5846 if (namei_full_hash != dwarf5_djb_hash (namei_string))
5847 {
5848 complaint (&symfile_complaints,
5849 _("Wrong .debug_names hash for string at index %u "
5850 "[in module %s]"),
5851 namei, objfile_name (dwarf2_per_objfile->objfile));
5852 return NULL;
5853 }
5854#endif
5855
5856 if (cmp (namei_string, name) == 0)
5857 {
5858 const ULONGEST namei_entry_offs
5859 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5860 + namei * map.offset_size),
5861 map.offset_size, map.dwarf5_byte_order);
5862 return map.entry_pool + namei_entry_offs;
5863 }
5864 }
5865
5866 ++namei;
5867 if (namei >= map.name_count)
5868 return NULL;
5869 }
5870}
5871
5872const gdb_byte *
5873dw2_debug_names_iterator::find_vec_in_debug_names
5874 (const mapped_debug_names &map, uint32_t namei)
5875{
5876 if (namei >= map.name_count)
5877 {
5878 complaint (&symfile_complaints,
5879 _("Wrong .debug_names with name index %u but name_count=%u "
5880 "[in module %s]"),
5881 namei, map.name_count,
ed2dc618 5882 objfile_name (map.dwarf2_per_objfile->objfile));
927aa2e7
JK
5883 return NULL;
5884 }
5885
5886 const ULONGEST namei_entry_offs
5887 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5888 + namei * map.offset_size),
5889 map.offset_size, map.dwarf5_byte_order);
5890 return map.entry_pool + namei_entry_offs;
5891}
5892
5893/* See dw2_debug_names_iterator. */
5894
5895dwarf2_per_cu_data *
5896dw2_debug_names_iterator::next ()
5897{
5898 if (m_addr == NULL)
5899 return NULL;
5900
ed2dc618
SM
5901 struct dwarf2_per_objfile *dwarf2_per_objfile = m_map.dwarf2_per_objfile;
5902 struct objfile *objfile = dwarf2_per_objfile->objfile;
5903 bfd *const abfd = objfile->obfd;
927aa2e7
JK
5904
5905 again:
5906
5907 unsigned int bytes_read;
5908 const ULONGEST abbrev = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5909 m_addr += bytes_read;
5910 if (abbrev == 0)
5911 return NULL;
5912
5913 const auto indexval_it = m_map.abbrev_map.find (abbrev);
5914 if (indexval_it == m_map.abbrev_map.cend ())
5915 {
5916 complaint (&symfile_complaints,
5917 _("Wrong .debug_names undefined abbrev code %s "
5918 "[in module %s]"),
ed2dc618 5919 pulongest (abbrev), objfile_name (objfile));
927aa2e7
JK
5920 return NULL;
5921 }
5922 const mapped_debug_names::index_val &indexval = indexval_it->second;
5923 bool have_is_static = false;
5924 bool is_static;
5925 dwarf2_per_cu_data *per_cu = NULL;
5926 for (const mapped_debug_names::index_val::attr &attr : indexval.attr_vec)
5927 {
5928 ULONGEST ull;
5929 switch (attr.form)
5930 {
5931 case DW_FORM_implicit_const:
5932 ull = attr.implicit_const;
5933 break;
5934 case DW_FORM_flag_present:
5935 ull = 1;
5936 break;
5937 case DW_FORM_udata:
5938 ull = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5939 m_addr += bytes_read;
5940 break;
5941 default:
5942 complaint (&symfile_complaints,
5943 _("Unsupported .debug_names form %s [in module %s]"),
5944 dwarf_form_name (attr.form),
ed2dc618 5945 objfile_name (objfile));
927aa2e7
JK
5946 return NULL;
5947 }
5948 switch (attr.dw_idx)
5949 {
5950 case DW_IDX_compile_unit:
5951 /* Don't crash on bad data. */
8af5c486 5952 if (ull >= dwarf2_per_objfile->n_comp_units)
927aa2e7
JK
5953 {
5954 complaint (&symfile_complaints,
5955 _(".debug_names entry has bad CU index %s"
5956 " [in module %s]"),
5957 pulongest (ull),
5958 objfile_name (dwarf2_per_objfile->objfile));
5959 continue;
5960 }
ed2dc618 5961 per_cu = dw2_get_cutu (dwarf2_per_objfile, ull);
927aa2e7 5962 break;
8af5c486
JK
5963 case DW_IDX_type_unit:
5964 /* Don't crash on bad data. */
5965 if (ull >= dwarf2_per_objfile->n_type_units)
5966 {
5967 complaint (&symfile_complaints,
5968 _(".debug_names entry has bad TU index %s"
5969 " [in module %s]"),
5970 pulongest (ull),
5971 objfile_name (dwarf2_per_objfile->objfile));
5972 continue;
5973 }
ed2dc618
SM
5974 per_cu = dw2_get_cutu (dwarf2_per_objfile,
5975 dwarf2_per_objfile->n_comp_units + ull);
8af5c486 5976 break;
927aa2e7
JK
5977 case DW_IDX_GNU_internal:
5978 if (!m_map.augmentation_is_gdb)
5979 break;
5980 have_is_static = true;
5981 is_static = true;
5982 break;
5983 case DW_IDX_GNU_external:
5984 if (!m_map.augmentation_is_gdb)
5985 break;
5986 have_is_static = true;
5987 is_static = false;
5988 break;
5989 }
5990 }
5991
5992 /* Skip if already read in. */
5993 if (per_cu->v.quick->compunit_symtab)
5994 goto again;
5995
5996 /* Check static vs global. */
5997 if (have_is_static)
5998 {
5999 const bool want_static = m_block_index != GLOBAL_BLOCK;
6000 if (m_want_specific_block && want_static != is_static)
6001 goto again;
6002 }
6003
6004 /* Match dw2_symtab_iter_next, symbol_kind
6005 and debug_names::psymbol_tag. */
6006 switch (m_domain)
6007 {
6008 case VAR_DOMAIN:
6009 switch (indexval.dwarf_tag)
6010 {
6011 case DW_TAG_variable:
6012 case DW_TAG_subprogram:
6013 /* Some types are also in VAR_DOMAIN. */
6014 case DW_TAG_typedef:
6015 case DW_TAG_structure_type:
6016 break;
6017 default:
6018 goto again;
6019 }
6020 break;
6021 case STRUCT_DOMAIN:
6022 switch (indexval.dwarf_tag)
6023 {
6024 case DW_TAG_typedef:
6025 case DW_TAG_structure_type:
6026 break;
6027 default:
6028 goto again;
6029 }
6030 break;
6031 case LABEL_DOMAIN:
6032 switch (indexval.dwarf_tag)
6033 {
6034 case 0:
6035 case DW_TAG_variable:
6036 break;
6037 default:
6038 goto again;
6039 }
6040 break;
6041 default:
6042 break;
6043 }
6044
6045 /* Match dw2_expand_symtabs_matching, symbol_kind and
6046 debug_names::psymbol_tag. */
6047 switch (m_search)
4b514bc8 6048 {
927aa2e7
JK
6049 case VARIABLES_DOMAIN:
6050 switch (indexval.dwarf_tag)
4b514bc8 6051 {
927aa2e7
JK
6052 case DW_TAG_variable:
6053 break;
6054 default:
6055 goto again;
4b514bc8 6056 }
927aa2e7
JK
6057 break;
6058 case FUNCTIONS_DOMAIN:
6059 switch (indexval.dwarf_tag)
4b514bc8 6060 {
927aa2e7
JK
6061 case DW_TAG_subprogram:
6062 break;
6063 default:
6064 goto again;
4b514bc8 6065 }
927aa2e7
JK
6066 break;
6067 case TYPES_DOMAIN:
6068 switch (indexval.dwarf_tag)
6069 {
6070 case DW_TAG_typedef:
6071 case DW_TAG_structure_type:
6072 break;
6073 default:
6074 goto again;
6075 }
6076 break;
6077 default:
6078 break;
4b514bc8 6079 }
927aa2e7
JK
6080
6081 return per_cu;
4b514bc8 6082}
61920122 6083
927aa2e7
JK
6084static struct compunit_symtab *
6085dw2_debug_names_lookup_symbol (struct objfile *objfile, int block_index_int,
6086 const char *name, domain_enum domain)
4b514bc8 6087{
927aa2e7 6088 const block_enum block_index = static_cast<block_enum> (block_index_int);
ed2dc618
SM
6089 struct dwarf2_per_objfile *dwarf2_per_objfile
6090 = get_dwarf2_per_objfile (objfile);
61920122 6091
927aa2e7
JK
6092 const auto &mapp = dwarf2_per_objfile->debug_names_table;
6093 if (!mapp)
61920122 6094 {
927aa2e7
JK
6095 /* index is NULL if OBJF_READNOW. */
6096 return NULL;
6097 }
6098 const auto &map = *mapp;
9291a0cd 6099
927aa2e7
JK
6100 dw2_debug_names_iterator iter (map, true /* want_specific_block */,
6101 block_index, domain, name);
9703b513 6102
927aa2e7
JK
6103 struct compunit_symtab *stab_best = NULL;
6104 struct dwarf2_per_cu_data *per_cu;
6105 while ((per_cu = iter.next ()) != NULL)
6106 {
6107 struct symbol *sym, *with_opaque = NULL;
6108 struct compunit_symtab *stab = dw2_instantiate_symtab (per_cu);
6109 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
6110 struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
9703b513 6111
927aa2e7
JK
6112 sym = block_find_symbol (block, name, domain,
6113 block_find_non_opaque_type_preferred,
6114 &with_opaque);
9703b513 6115
927aa2e7
JK
6116 /* Some caution must be observed with overloaded functions and
6117 methods, since the index will not contain any overload
6118 information (but NAME might contain it). */
a3ec0bb1 6119
927aa2e7
JK
6120 if (sym != NULL
6121 && strcmp_iw (SYMBOL_SEARCH_NAME (sym), name) == 0)
6122 return stab;
6123 if (with_opaque != NULL
6124 && strcmp_iw (SYMBOL_SEARCH_NAME (with_opaque), name) == 0)
6125 stab_best = stab;
9703b513 6126
927aa2e7 6127 /* Keep looking through other CUs. */
9703b513
TT
6128 }
6129
927aa2e7 6130 return stab_best;
9703b513
TT
6131}
6132
927aa2e7
JK
6133/* This dumps minimal information about .debug_names. It is called
6134 via "mt print objfiles". The gdb.dwarf2/gdb-index.exp testcase
6135 uses this to verify that .debug_names has been loaded. */
9291a0cd 6136
927aa2e7
JK
6137static void
6138dw2_debug_names_dump (struct objfile *objfile)
6139{
ed2dc618
SM
6140 struct dwarf2_per_objfile *dwarf2_per_objfile
6141 = get_dwarf2_per_objfile (objfile);
6142
927aa2e7
JK
6143 gdb_assert (dwarf2_per_objfile->using_index);
6144 printf_filtered (".debug_names:");
6145 if (dwarf2_per_objfile->debug_names_table)
6146 printf_filtered (" exists\n");
6147 else
6148 printf_filtered (" faked for \"readnow\"\n");
6149 printf_filtered ("\n");
9291a0cd
TT
6150}
6151
9291a0cd 6152static void
927aa2e7
JK
6153dw2_debug_names_expand_symtabs_for_function (struct objfile *objfile,
6154 const char *func_name)
9291a0cd 6155{
ed2dc618
SM
6156 struct dwarf2_per_objfile *dwarf2_per_objfile
6157 = get_dwarf2_per_objfile (objfile);
ae2de4f8 6158
927aa2e7
JK
6159 /* dwarf2_per_objfile->debug_names_table is NULL if OBJF_READNOW. */
6160 if (dwarf2_per_objfile->debug_names_table)
24c79950 6161 {
927aa2e7 6162 const mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
24c79950 6163
927aa2e7
JK
6164 /* Note: It doesn't matter what we pass for block_index here. */
6165 dw2_debug_names_iterator iter (map, false /* want_specific_block */,
6166 GLOBAL_BLOCK, VAR_DOMAIN, func_name);
24c79950 6167
927aa2e7
JK
6168 struct dwarf2_per_cu_data *per_cu;
6169 while ((per_cu = iter.next ()) != NULL)
6170 dw2_instantiate_symtab (per_cu);
6171 }
6172}
24c79950 6173
927aa2e7
JK
6174static void
6175dw2_debug_names_expand_symtabs_matching
6176 (struct objfile *objfile,
6177 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
6178 const lookup_name_info &lookup_name,
6179 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
6180 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
6181 enum search_domain kind)
6182{
ed2dc618
SM
6183 struct dwarf2_per_objfile *dwarf2_per_objfile
6184 = get_dwarf2_per_objfile (objfile);
9291a0cd 6185
927aa2e7
JK
6186 /* debug_names_table is NULL if OBJF_READNOW. */
6187 if (!dwarf2_per_objfile->debug_names_table)
6188 return;
9291a0cd 6189
ed2dc618 6190 dw_expand_symtabs_matching_file_matcher (dwarf2_per_objfile, file_matcher);
24c79950 6191
44ed8f3e 6192 mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
bbf2f4df 6193
44ed8f3e
PA
6194 dw2_expand_symtabs_matching_symbol (map, lookup_name,
6195 symbol_matcher,
6196 kind, [&] (offset_type namei)
927aa2e7 6197 {
927aa2e7
JK
6198 /* The name was matched, now expand corresponding CUs that were
6199 marked. */
6200 dw2_debug_names_iterator iter (map, kind, namei);
bbf2f4df 6201
927aa2e7
JK
6202 struct dwarf2_per_cu_data *per_cu;
6203 while ((per_cu = iter.next ()) != NULL)
6204 dw2_expand_symtabs_matching_one (per_cu, file_matcher,
6205 expansion_notify);
44ed8f3e 6206 });
9291a0cd
TT
6207}
6208
927aa2e7 6209const struct quick_symbol_functions dwarf2_debug_names_functions =
9291a0cd
TT
6210{
6211 dw2_has_symbols,
6212 dw2_find_last_source_symtab,
6213 dw2_forget_cached_source_info,
f8eba3c6 6214 dw2_map_symtabs_matching_filename,
927aa2e7 6215 dw2_debug_names_lookup_symbol,
9291a0cd 6216 dw2_print_stats,
927aa2e7 6217 dw2_debug_names_dump,
9291a0cd 6218 dw2_relocate,
927aa2e7 6219 dw2_debug_names_expand_symtabs_for_function,
9291a0cd 6220 dw2_expand_all_symtabs,
652a8996 6221 dw2_expand_symtabs_with_fullname,
40658b94 6222 dw2_map_matching_symbols,
927aa2e7 6223 dw2_debug_names_expand_symtabs_matching,
43f3e411 6224 dw2_find_pc_sect_compunit_symtab,
71a3c369 6225 NULL,
9291a0cd
TT
6226 dw2_map_symbol_filenames
6227};
6228
3c0aa29a 6229/* See symfile.h. */
9291a0cd 6230
3c0aa29a
PA
6231bool
6232dwarf2_initialize_objfile (struct objfile *objfile, dw_index_kind *index_kind)
9291a0cd 6233{
ed2dc618
SM
6234 struct dwarf2_per_objfile *dwarf2_per_objfile
6235 = get_dwarf2_per_objfile (objfile);
6236
9291a0cd
TT
6237 /* If we're about to read full symbols, don't bother with the
6238 indices. In this case we also don't care if some other debug
6239 format is making psymtabs, because they are all about to be
6240 expanded anyway. */
6241 if ((objfile->flags & OBJF_READNOW))
6242 {
6243 int i;
6244
6245 dwarf2_per_objfile->using_index = 1;
ed2dc618
SM
6246 create_all_comp_units (dwarf2_per_objfile);
6247 create_all_type_units (dwarf2_per_objfile);
7b9f3c50
DE
6248 dwarf2_per_objfile->quick_file_names_table =
6249 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
9291a0cd 6250
1fd400ff 6251 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
d467dd73 6252 + dwarf2_per_objfile->n_type_units); ++i)
9291a0cd 6253 {
ed2dc618 6254 dwarf2_per_cu_data *per_cu = dw2_get_cutu (dwarf2_per_objfile, i);
9291a0cd 6255
e254ef6a
DE
6256 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6257 struct dwarf2_per_cu_quick_data);
9291a0cd
TT
6258 }
6259
6260 /* Return 1 so that gdb sees the "quick" functions. However,
6261 these functions will be no-ops because we will have expanded
6262 all symtabs. */
3c0aa29a
PA
6263 *index_kind = dw_index_kind::GDB_INDEX;
6264 return true;
9291a0cd
TT
6265 }
6266
ed2dc618 6267 if (dwarf2_read_debug_names (dwarf2_per_objfile))
3c0aa29a
PA
6268 {
6269 *index_kind = dw_index_kind::DEBUG_NAMES;
6270 return true;
6271 }
927aa2e7 6272
9291a0cd 6273 if (dwarf2_read_index (objfile))
3c0aa29a
PA
6274 {
6275 *index_kind = dw_index_kind::GDB_INDEX;
6276 return true;
6277 }
9291a0cd 6278
3c0aa29a 6279 return false;
9291a0cd
TT
6280}
6281
6282\f
6283
dce234bc
PP
6284/* Build a partial symbol table. */
6285
6286void
f29dff0a 6287dwarf2_build_psymtabs (struct objfile *objfile)
dce234bc 6288{
ed2dc618
SM
6289 struct dwarf2_per_objfile *dwarf2_per_objfile
6290 = get_dwarf2_per_objfile (objfile);
c9bf0622 6291
af5bf4ad
SM
6292 if (objfile->global_psymbols.capacity () == 0
6293 && objfile->static_psymbols.capacity () == 0)
6294 init_psymbol_list (objfile, 1024);
c906108c 6295
492d29ea 6296 TRY
c9bf0622
TT
6297 {
6298 /* This isn't really ideal: all the data we allocate on the
6299 objfile's obstack is still uselessly kept around. However,
6300 freeing it seems unsafe. */
906768f9 6301 psymtab_discarder psymtabs (objfile);
ed2dc618 6302 dwarf2_build_psymtabs_hard (dwarf2_per_objfile);
906768f9 6303 psymtabs.keep ();
c9bf0622 6304 }
492d29ea
PA
6305 CATCH (except, RETURN_MASK_ERROR)
6306 {
6307 exception_print (gdb_stderr, except);
6308 }
6309 END_CATCH
c906108c 6310}
c906108c 6311
1ce1cefd
DE
6312/* Return the total length of the CU described by HEADER. */
6313
6314static unsigned int
6315get_cu_length (const struct comp_unit_head *header)
6316{
6317 return header->initial_length_size + header->length;
6318}
6319
9c541725 6320/* Return TRUE if SECT_OFF is within CU_HEADER. */
45452591 6321
9c541725
PA
6322static inline bool
6323offset_in_cu_p (const comp_unit_head *cu_header, sect_offset sect_off)
45452591 6324{
9c541725
PA
6325 sect_offset bottom = cu_header->sect_off;
6326 sect_offset top = cu_header->sect_off + get_cu_length (cu_header);
9a619af0 6327
9c541725 6328 return sect_off >= bottom && sect_off < top;
45452591
DE
6329}
6330
3b80fe9b
DE
6331/* Find the base address of the compilation unit for range lists and
6332 location lists. It will normally be specified by DW_AT_low_pc.
6333 In DWARF-3 draft 4, the base address could be overridden by
6334 DW_AT_entry_pc. It's been removed, but GCC still uses this for
6335 compilation units with discontinuous ranges. */
6336
6337static void
6338dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
6339{
6340 struct attribute *attr;
6341
6342 cu->base_known = 0;
6343 cu->base_address = 0;
6344
6345 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
6346 if (attr)
6347 {
31aa7e4e 6348 cu->base_address = attr_value_as_address (attr);
3b80fe9b
DE
6349 cu->base_known = 1;
6350 }
6351 else
6352 {
6353 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
6354 if (attr)
6355 {
31aa7e4e 6356 cu->base_address = attr_value_as_address (attr);
3b80fe9b
DE
6357 cu->base_known = 1;
6358 }
6359 }
6360}
6361
93311388 6362/* Read in the comp unit header information from the debug_info at info_ptr.
43988095 6363 Use rcuh_kind::COMPILE as the default type if not known by the caller.
93311388
DE
6364 NOTE: This leaves members offset, first_die_offset to be filled in
6365 by the caller. */
107d2387 6366
d521ce57 6367static const gdb_byte *
107d2387 6368read_comp_unit_head (struct comp_unit_head *cu_header,
43988095
JK
6369 const gdb_byte *info_ptr,
6370 struct dwarf2_section_info *section,
6371 rcuh_kind section_kind)
107d2387
AC
6372{
6373 int signed_addr;
891d2f0b 6374 unsigned int bytes_read;
43988095
JK
6375 const char *filename = get_section_file_name (section);
6376 bfd *abfd = get_section_bfd_owner (section);
c764a876
DE
6377
6378 cu_header->length = read_initial_length (abfd, info_ptr, &bytes_read);
6379 cu_header->initial_length_size = bytes_read;
6380 cu_header->offset_size = (bytes_read == 4) ? 4 : 8;
613e1657 6381 info_ptr += bytes_read;
107d2387
AC
6382 cu_header->version = read_2_bytes (abfd, info_ptr);
6383 info_ptr += 2;
43988095
JK
6384 if (cu_header->version < 5)
6385 switch (section_kind)
6386 {
6387 case rcuh_kind::COMPILE:
6388 cu_header->unit_type = DW_UT_compile;
6389 break;
6390 case rcuh_kind::TYPE:
6391 cu_header->unit_type = DW_UT_type;
6392 break;
6393 default:
6394 internal_error (__FILE__, __LINE__,
6395 _("read_comp_unit_head: invalid section_kind"));
6396 }
6397 else
6398 {
6399 cu_header->unit_type = static_cast<enum dwarf_unit_type>
6400 (read_1_byte (abfd, info_ptr));
6401 info_ptr += 1;
6402 switch (cu_header->unit_type)
6403 {
6404 case DW_UT_compile:
6405 if (section_kind != rcuh_kind::COMPILE)
6406 error (_("Dwarf Error: wrong unit_type in compilation unit header "
6407 "(is DW_UT_compile, should be DW_UT_type) [in module %s]"),
6408 filename);
6409 break;
6410 case DW_UT_type:
6411 section_kind = rcuh_kind::TYPE;
6412 break;
6413 default:
6414 error (_("Dwarf Error: wrong unit_type in compilation unit header "
6415 "(is %d, should be %d or %d) [in module %s]"),
6416 cu_header->unit_type, DW_UT_compile, DW_UT_type, filename);
6417 }
6418
6419 cu_header->addr_size = read_1_byte (abfd, info_ptr);
6420 info_ptr += 1;
6421 }
9c541725
PA
6422 cu_header->abbrev_sect_off = (sect_offset) read_offset (abfd, info_ptr,
6423 cu_header,
6424 &bytes_read);
613e1657 6425 info_ptr += bytes_read;
43988095
JK
6426 if (cu_header->version < 5)
6427 {
6428 cu_header->addr_size = read_1_byte (abfd, info_ptr);
6429 info_ptr += 1;
6430 }
107d2387
AC
6431 signed_addr = bfd_get_sign_extend_vma (abfd);
6432 if (signed_addr < 0)
8e65ff28 6433 internal_error (__FILE__, __LINE__,
e2e0b3e5 6434 _("read_comp_unit_head: dwarf from non elf file"));
107d2387 6435 cu_header->signed_addr_p = signed_addr;
c764a876 6436
43988095
JK
6437 if (section_kind == rcuh_kind::TYPE)
6438 {
6439 LONGEST type_offset;
6440
6441 cu_header->signature = read_8_bytes (abfd, info_ptr);
6442 info_ptr += 8;
6443
6444 type_offset = read_offset (abfd, info_ptr, cu_header, &bytes_read);
6445 info_ptr += bytes_read;
9c541725
PA
6446 cu_header->type_cu_offset_in_tu = (cu_offset) type_offset;
6447 if (to_underlying (cu_header->type_cu_offset_in_tu) != type_offset)
43988095
JK
6448 error (_("Dwarf Error: Too big type_offset in compilation unit "
6449 "header (is %s) [in module %s]"), plongest (type_offset),
6450 filename);
6451 }
6452
107d2387
AC
6453 return info_ptr;
6454}
6455
36586728
TT
6456/* Helper function that returns the proper abbrev section for
6457 THIS_CU. */
6458
6459static struct dwarf2_section_info *
6460get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
6461{
6462 struct dwarf2_section_info *abbrev;
ed2dc618 6463 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
36586728
TT
6464
6465 if (this_cu->is_dwz)
ed2dc618 6466 abbrev = &dwarf2_get_dwz_file (dwarf2_per_objfile)->abbrev;
36586728
TT
6467 else
6468 abbrev = &dwarf2_per_objfile->abbrev;
6469
6470 return abbrev;
6471}
6472
9ff913ba
DE
6473/* Subroutine of read_and_check_comp_unit_head and
6474 read_and_check_type_unit_head to simplify them.
6475 Perform various error checking on the header. */
6476
6477static void
ed2dc618
SM
6478error_check_comp_unit_head (struct dwarf2_per_objfile *dwarf2_per_objfile,
6479 struct comp_unit_head *header,
4bdcc0c1
DE
6480 struct dwarf2_section_info *section,
6481 struct dwarf2_section_info *abbrev_section)
9ff913ba 6482{
a32a8923 6483 const char *filename = get_section_file_name (section);
9ff913ba 6484
43988095 6485 if (header->version < 2 || header->version > 5)
9ff913ba 6486 error (_("Dwarf Error: wrong version in compilation unit header "
43988095 6487 "(is %d, should be 2, 3, 4 or 5) [in module %s]"), header->version,
9ff913ba
DE
6488 filename);
6489
9c541725 6490 if (to_underlying (header->abbrev_sect_off)
36586728 6491 >= dwarf2_section_size (dwarf2_per_objfile->objfile, abbrev_section))
9d8780f0
SM
6492 error (_("Dwarf Error: bad offset (%s) in compilation unit header "
6493 "(offset %s + 6) [in module %s]"),
6494 sect_offset_str (header->abbrev_sect_off),
6495 sect_offset_str (header->sect_off),
9ff913ba
DE
6496 filename);
6497
9c541725 6498 /* Cast to ULONGEST to use 64-bit arithmetic when possible to
9ff913ba 6499 avoid potential 32-bit overflow. */
9c541725 6500 if (((ULONGEST) header->sect_off + get_cu_length (header))
9ff913ba 6501 > section->size)
9c541725 6502 error (_("Dwarf Error: bad length (0x%x) in compilation unit header "
9d8780f0
SM
6503 "(offset %s + 0) [in module %s]"),
6504 header->length, sect_offset_str (header->sect_off),
9ff913ba
DE
6505 filename);
6506}
6507
6508/* Read in a CU/TU header and perform some basic error checking.
6509 The contents of the header are stored in HEADER.
6510 The result is a pointer to the start of the first DIE. */
adabb602 6511
d521ce57 6512static const gdb_byte *
ed2dc618
SM
6513read_and_check_comp_unit_head (struct dwarf2_per_objfile *dwarf2_per_objfile,
6514 struct comp_unit_head *header,
9ff913ba 6515 struct dwarf2_section_info *section,
4bdcc0c1 6516 struct dwarf2_section_info *abbrev_section,
d521ce57 6517 const gdb_byte *info_ptr,
43988095 6518 rcuh_kind section_kind)
72bf9492 6519{
d521ce57 6520 const gdb_byte *beg_of_comp_unit = info_ptr;
72bf9492 6521
9c541725 6522 header->sect_off = (sect_offset) (beg_of_comp_unit - section->buffer);
adabb602 6523
43988095 6524 info_ptr = read_comp_unit_head (header, info_ptr, section, section_kind);
9ff913ba 6525
9c541725 6526 header->first_die_cu_offset = (cu_offset) (info_ptr - beg_of_comp_unit);
348e048f 6527
ed2dc618
SM
6528 error_check_comp_unit_head (dwarf2_per_objfile, header, section,
6529 abbrev_section);
9ff913ba
DE
6530
6531 return info_ptr;
348e048f
DE
6532}
6533
f4dc4d17
DE
6534/* Fetch the abbreviation table offset from a comp or type unit header. */
6535
6536static sect_offset
ed2dc618
SM
6537read_abbrev_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
6538 struct dwarf2_section_info *section,
9c541725 6539 sect_offset sect_off)
f4dc4d17 6540{
a32a8923 6541 bfd *abfd = get_section_bfd_owner (section);
d521ce57 6542 const gdb_byte *info_ptr;
ac298888 6543 unsigned int initial_length_size, offset_size;
43988095 6544 uint16_t version;
f4dc4d17
DE
6545
6546 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
9c541725 6547 info_ptr = section->buffer + to_underlying (sect_off);
ac298888 6548 read_initial_length (abfd, info_ptr, &initial_length_size);
f4dc4d17 6549 offset_size = initial_length_size == 4 ? 4 : 8;
43988095
JK
6550 info_ptr += initial_length_size;
6551
6552 version = read_2_bytes (abfd, info_ptr);
6553 info_ptr += 2;
6554 if (version >= 5)
6555 {
6556 /* Skip unit type and address size. */
6557 info_ptr += 2;
6558 }
6559
9c541725 6560 return (sect_offset) read_offset_1 (abfd, info_ptr, offset_size);
f4dc4d17
DE
6561}
6562
aaa75496
JB
6563/* Allocate a new partial symtab for file named NAME and mark this new
6564 partial symtab as being an include of PST. */
6565
6566static void
d521ce57 6567dwarf2_create_include_psymtab (const char *name, struct partial_symtab *pst,
aaa75496
JB
6568 struct objfile *objfile)
6569{
6570 struct partial_symtab *subpst = allocate_psymtab (name, objfile);
6571
fbd9ab74
JK
6572 if (!IS_ABSOLUTE_PATH (subpst->filename))
6573 {
6574 /* It shares objfile->objfile_obstack. */
6575 subpst->dirname = pst->dirname;
6576 }
6577
aaa75496
JB
6578 subpst->textlow = 0;
6579 subpst->texthigh = 0;
6580
8d749320
SM
6581 subpst->dependencies
6582 = XOBNEW (&objfile->objfile_obstack, struct partial_symtab *);
aaa75496
JB
6583 subpst->dependencies[0] = pst;
6584 subpst->number_of_dependencies = 1;
6585
6586 subpst->globals_offset = 0;
6587 subpst->n_global_syms = 0;
6588 subpst->statics_offset = 0;
6589 subpst->n_static_syms = 0;
43f3e411 6590 subpst->compunit_symtab = NULL;
aaa75496
JB
6591 subpst->read_symtab = pst->read_symtab;
6592 subpst->readin = 0;
6593
6594 /* No private part is necessary for include psymtabs. This property
6595 can be used to differentiate between such include psymtabs and
10b3939b 6596 the regular ones. */
58a9656e 6597 subpst->read_symtab_private = NULL;
aaa75496
JB
6598}
6599
6600/* Read the Line Number Program data and extract the list of files
6601 included by the source file represented by PST. Build an include
d85a05f0 6602 partial symtab for each of these included files. */
aaa75496
JB
6603
6604static void
6605dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
dee91e82
DE
6606 struct die_info *die,
6607 struct partial_symtab *pst)
aaa75496 6608{
fff8551c 6609 line_header_up lh;
d85a05f0 6610 struct attribute *attr;
aaa75496 6611
d85a05f0
DJ
6612 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
6613 if (attr)
9c541725 6614 lh = dwarf_decode_line_header ((sect_offset) DW_UNSND (attr), cu);
aaa75496
JB
6615 if (lh == NULL)
6616 return; /* No linetable, so no includes. */
6617
c6da4cef 6618 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). */
fff8551c 6619 dwarf_decode_lines (lh.get (), pst->dirname, cu, pst, pst->textlow, 1);
aaa75496
JB
6620}
6621
348e048f 6622static hashval_t
52dc124a 6623hash_signatured_type (const void *item)
348e048f 6624{
9a3c8263
SM
6625 const struct signatured_type *sig_type
6626 = (const struct signatured_type *) item;
9a619af0 6627
348e048f 6628 /* This drops the top 32 bits of the signature, but is ok for a hash. */
52dc124a 6629 return sig_type->signature;
348e048f
DE
6630}
6631
6632static int
52dc124a 6633eq_signatured_type (const void *item_lhs, const void *item_rhs)
348e048f 6634{
9a3c8263
SM
6635 const struct signatured_type *lhs = (const struct signatured_type *) item_lhs;
6636 const struct signatured_type *rhs = (const struct signatured_type *) item_rhs;
9a619af0 6637
348e048f
DE
6638 return lhs->signature == rhs->signature;
6639}
6640
1fd400ff
TT
6641/* Allocate a hash table for signatured types. */
6642
6643static htab_t
673bfd45 6644allocate_signatured_type_table (struct objfile *objfile)
1fd400ff
TT
6645{
6646 return htab_create_alloc_ex (41,
52dc124a
DE
6647 hash_signatured_type,
6648 eq_signatured_type,
1fd400ff
TT
6649 NULL,
6650 &objfile->objfile_obstack,
6651 hashtab_obstack_allocate,
6652 dummy_obstack_deallocate);
6653}
6654
d467dd73 6655/* A helper function to add a signatured type CU to a table. */
1fd400ff
TT
6656
6657static int
d467dd73 6658add_signatured_type_cu_to_table (void **slot, void *datum)
1fd400ff 6659{
9a3c8263
SM
6660 struct signatured_type *sigt = (struct signatured_type *) *slot;
6661 struct signatured_type ***datap = (struct signatured_type ***) datum;
1fd400ff 6662
b4dd5633 6663 **datap = sigt;
1fd400ff
TT
6664 ++*datap;
6665
6666 return 1;
6667}
6668
78d4d2c5 6669/* A helper for create_debug_types_hash_table. Read types from SECTION
43988095
JK
6670 and fill them into TYPES_HTAB. It will process only type units,
6671 therefore DW_UT_type. */
c88ee1f0 6672
78d4d2c5 6673static void
ed2dc618
SM
6674create_debug_type_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
6675 struct dwo_file *dwo_file,
43988095
JK
6676 dwarf2_section_info *section, htab_t &types_htab,
6677 rcuh_kind section_kind)
348e048f 6678{
3019eac3 6679 struct objfile *objfile = dwarf2_per_objfile->objfile;
4bdcc0c1 6680 struct dwarf2_section_info *abbrev_section;
78d4d2c5
JK
6681 bfd *abfd;
6682 const gdb_byte *info_ptr, *end_ptr;
348e048f 6683
4bdcc0c1
DE
6684 abbrev_section = (dwo_file != NULL
6685 ? &dwo_file->sections.abbrev
6686 : &dwarf2_per_objfile->abbrev);
6687
b4f54984 6688 if (dwarf_read_debug)
43988095
JK
6689 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
6690 get_section_name (section),
a32a8923 6691 get_section_file_name (abbrev_section));
09406207 6692
78d4d2c5
JK
6693 dwarf2_read_section (objfile, section);
6694 info_ptr = section->buffer;
348e048f 6695
78d4d2c5
JK
6696 if (info_ptr == NULL)
6697 return;
348e048f 6698
78d4d2c5
JK
6699 /* We can't set abfd until now because the section may be empty or
6700 not present, in which case the bfd is unknown. */
6701 abfd = get_section_bfd_owner (section);
348e048f 6702
78d4d2c5
JK
6703 /* We don't use init_cutu_and_read_dies_simple, or some such, here
6704 because we don't need to read any dies: the signature is in the
6705 header. */
3019eac3 6706
78d4d2c5
JK
6707 end_ptr = info_ptr + section->size;
6708 while (info_ptr < end_ptr)
6709 {
78d4d2c5
JK
6710 struct signatured_type *sig_type;
6711 struct dwo_unit *dwo_tu;
6712 void **slot;
6713 const gdb_byte *ptr = info_ptr;
6714 struct comp_unit_head header;
6715 unsigned int length;
8b70b953 6716
9c541725 6717 sect_offset sect_off = (sect_offset) (ptr - section->buffer);
348e048f 6718
a49dd8dd
JK
6719 /* Initialize it due to a false compiler warning. */
6720 header.signature = -1;
9c541725 6721 header.type_cu_offset_in_tu = (cu_offset) -1;
a49dd8dd 6722
78d4d2c5
JK
6723 /* We need to read the type's signature in order to build the hash
6724 table, but we don't need anything else just yet. */
348e048f 6725
ed2dc618 6726 ptr = read_and_check_comp_unit_head (dwarf2_per_objfile, &header, section,
43988095 6727 abbrev_section, ptr, section_kind);
348e048f 6728
78d4d2c5 6729 length = get_cu_length (&header);
6caca83c 6730
78d4d2c5
JK
6731 /* Skip dummy type units. */
6732 if (ptr >= info_ptr + length
43988095
JK
6733 || peek_abbrev_code (abfd, ptr) == 0
6734 || header.unit_type != DW_UT_type)
78d4d2c5
JK
6735 {
6736 info_ptr += length;
6737 continue;
6738 }
dee91e82 6739
78d4d2c5
JK
6740 if (types_htab == NULL)
6741 {
6742 if (dwo_file)
6743 types_htab = allocate_dwo_unit_table (objfile);
6744 else
6745 types_htab = allocate_signatured_type_table (objfile);
6746 }
8b70b953 6747
78d4d2c5
JK
6748 if (dwo_file)
6749 {
6750 sig_type = NULL;
6751 dwo_tu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6752 struct dwo_unit);
6753 dwo_tu->dwo_file = dwo_file;
43988095 6754 dwo_tu->signature = header.signature;
9c541725 6755 dwo_tu->type_offset_in_tu = header.type_cu_offset_in_tu;
78d4d2c5 6756 dwo_tu->section = section;
9c541725 6757 dwo_tu->sect_off = sect_off;
78d4d2c5
JK
6758 dwo_tu->length = length;
6759 }
6760 else
6761 {
6762 /* N.B.: type_offset is not usable if this type uses a DWO file.
6763 The real type_offset is in the DWO file. */
6764 dwo_tu = NULL;
6765 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6766 struct signatured_type);
43988095 6767 sig_type->signature = header.signature;
9c541725 6768 sig_type->type_offset_in_tu = header.type_cu_offset_in_tu;
e3b94546 6769 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
78d4d2c5
JK
6770 sig_type->per_cu.is_debug_types = 1;
6771 sig_type->per_cu.section = section;
9c541725 6772 sig_type->per_cu.sect_off = sect_off;
78d4d2c5
JK
6773 sig_type->per_cu.length = length;
6774 }
6775
6776 slot = htab_find_slot (types_htab,
6777 dwo_file ? (void*) dwo_tu : (void *) sig_type,
6778 INSERT);
6779 gdb_assert (slot != NULL);
6780 if (*slot != NULL)
6781 {
9c541725 6782 sect_offset dup_sect_off;
0349ea22 6783
3019eac3
DE
6784 if (dwo_file)
6785 {
78d4d2c5
JK
6786 const struct dwo_unit *dup_tu
6787 = (const struct dwo_unit *) *slot;
6788
9c541725 6789 dup_sect_off = dup_tu->sect_off;
3019eac3
DE
6790 }
6791 else
6792 {
78d4d2c5
JK
6793 const struct signatured_type *dup_tu
6794 = (const struct signatured_type *) *slot;
6795
9c541725 6796 dup_sect_off = dup_tu->per_cu.sect_off;
3019eac3 6797 }
8b70b953 6798
78d4d2c5 6799 complaint (&symfile_complaints,
9d8780f0
SM
6800 _("debug type entry at offset %s is duplicate to"
6801 " the entry at offset %s, signature %s"),
6802 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
43988095 6803 hex_string (header.signature));
78d4d2c5
JK
6804 }
6805 *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
3019eac3 6806
78d4d2c5 6807 if (dwarf_read_debug > 1)
9d8780f0
SM
6808 fprintf_unfiltered (gdb_stdlog, " offset %s, signature %s\n",
6809 sect_offset_str (sect_off),
43988095 6810 hex_string (header.signature));
3019eac3 6811
78d4d2c5
JK
6812 info_ptr += length;
6813 }
6814}
3019eac3 6815
78d4d2c5
JK
6816/* Create the hash table of all entries in the .debug_types
6817 (or .debug_types.dwo) section(s).
6818 If reading a DWO file, then DWO_FILE is a pointer to the DWO file object,
6819 otherwise it is NULL.
b3c8eb43 6820
78d4d2c5 6821 The result is a pointer to the hash table or NULL if there are no types.
348e048f 6822
78d4d2c5 6823 Note: This function processes DWO files only, not DWP files. */
348e048f 6824
78d4d2c5 6825static void
ed2dc618
SM
6826create_debug_types_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
6827 struct dwo_file *dwo_file,
78d4d2c5
JK
6828 VEC (dwarf2_section_info_def) *types,
6829 htab_t &types_htab)
6830{
6831 int ix;
6832 struct dwarf2_section_info *section;
6833
6834 if (VEC_empty (dwarf2_section_info_def, types))
6835 return;
348e048f 6836
78d4d2c5
JK
6837 for (ix = 0;
6838 VEC_iterate (dwarf2_section_info_def, types, ix, section);
6839 ++ix)
ed2dc618
SM
6840 create_debug_type_hash_table (dwarf2_per_objfile, dwo_file, section,
6841 types_htab, rcuh_kind::TYPE);
3019eac3
DE
6842}
6843
6844/* Create the hash table of all entries in the .debug_types section,
6845 and initialize all_type_units.
6846 The result is zero if there is an error (e.g. missing .debug_types section),
6847 otherwise non-zero. */
6848
6849static int
ed2dc618 6850create_all_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
3019eac3 6851{
78d4d2c5 6852 htab_t types_htab = NULL;
b4dd5633 6853 struct signatured_type **iter;
3019eac3 6854
ed2dc618
SM
6855 create_debug_type_hash_table (dwarf2_per_objfile, NULL,
6856 &dwarf2_per_objfile->info, types_htab,
43988095 6857 rcuh_kind::COMPILE);
ed2dc618
SM
6858 create_debug_types_hash_table (dwarf2_per_objfile, NULL,
6859 dwarf2_per_objfile->types, types_htab);
3019eac3
DE
6860 if (types_htab == NULL)
6861 {
6862 dwarf2_per_objfile->signatured_types = NULL;
6863 return 0;
6864 }
6865
348e048f
DE
6866 dwarf2_per_objfile->signatured_types = types_htab;
6867
6aa5f3a6
DE
6868 dwarf2_per_objfile->n_type_units
6869 = dwarf2_per_objfile->n_allocated_type_units
6870 = htab_elements (types_htab);
8d749320
SM
6871 dwarf2_per_objfile->all_type_units =
6872 XNEWVEC (struct signatured_type *, dwarf2_per_objfile->n_type_units);
d467dd73
DE
6873 iter = &dwarf2_per_objfile->all_type_units[0];
6874 htab_traverse_noresize (types_htab, add_signatured_type_cu_to_table, &iter);
6875 gdb_assert (iter - &dwarf2_per_objfile->all_type_units[0]
6876 == dwarf2_per_objfile->n_type_units);
1fd400ff 6877
348e048f
DE
6878 return 1;
6879}
6880
6aa5f3a6
DE
6881/* Add an entry for signature SIG to dwarf2_per_objfile->signatured_types.
6882 If SLOT is non-NULL, it is the entry to use in the hash table.
6883 Otherwise we find one. */
6884
6885static struct signatured_type *
ed2dc618
SM
6886add_type_unit (struct dwarf2_per_objfile *dwarf2_per_objfile, ULONGEST sig,
6887 void **slot)
6aa5f3a6
DE
6888{
6889 struct objfile *objfile = dwarf2_per_objfile->objfile;
6890 int n_type_units = dwarf2_per_objfile->n_type_units;
6891 struct signatured_type *sig_type;
6892
6893 gdb_assert (n_type_units <= dwarf2_per_objfile->n_allocated_type_units);
6894 ++n_type_units;
6895 if (n_type_units > dwarf2_per_objfile->n_allocated_type_units)
6896 {
6897 if (dwarf2_per_objfile->n_allocated_type_units == 0)
6898 dwarf2_per_objfile->n_allocated_type_units = 1;
6899 dwarf2_per_objfile->n_allocated_type_units *= 2;
6900 dwarf2_per_objfile->all_type_units
224c3ddb
SM
6901 = XRESIZEVEC (struct signatured_type *,
6902 dwarf2_per_objfile->all_type_units,
6903 dwarf2_per_objfile->n_allocated_type_units);
6aa5f3a6
DE
6904 ++dwarf2_per_objfile->tu_stats.nr_all_type_units_reallocs;
6905 }
6906 dwarf2_per_objfile->n_type_units = n_type_units;
6907
6908 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6909 struct signatured_type);
6910 dwarf2_per_objfile->all_type_units[n_type_units - 1] = sig_type;
6911 sig_type->signature = sig;
6912 sig_type->per_cu.is_debug_types = 1;
6913 if (dwarf2_per_objfile->using_index)
6914 {
6915 sig_type->per_cu.v.quick =
6916 OBSTACK_ZALLOC (&objfile->objfile_obstack,
6917 struct dwarf2_per_cu_quick_data);
6918 }
6919
6920 if (slot == NULL)
6921 {
6922 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
6923 sig_type, INSERT);
6924 }
6925 gdb_assert (*slot == NULL);
6926 *slot = sig_type;
6927 /* The rest of sig_type must be filled in by the caller. */
6928 return sig_type;
6929}
6930
a2ce51a0
DE
6931/* Subroutine of lookup_dwo_signatured_type and lookup_dwp_signatured_type.
6932 Fill in SIG_ENTRY with DWO_ENTRY. */
6933
6934static void
ed2dc618 6935fill_in_sig_entry_from_dwo_entry (struct dwarf2_per_objfile *dwarf2_per_objfile,
a2ce51a0
DE
6936 struct signatured_type *sig_entry,
6937 struct dwo_unit *dwo_entry)
6938{
7ee85ab1 6939 /* Make sure we're not clobbering something we don't expect to. */
a2ce51a0
DE
6940 gdb_assert (! sig_entry->per_cu.queued);
6941 gdb_assert (sig_entry->per_cu.cu == NULL);
6aa5f3a6
DE
6942 if (dwarf2_per_objfile->using_index)
6943 {
6944 gdb_assert (sig_entry->per_cu.v.quick != NULL);
43f3e411 6945 gdb_assert (sig_entry->per_cu.v.quick->compunit_symtab == NULL);
6aa5f3a6
DE
6946 }
6947 else
6948 gdb_assert (sig_entry->per_cu.v.psymtab == NULL);
a2ce51a0 6949 gdb_assert (sig_entry->signature == dwo_entry->signature);
9c541725 6950 gdb_assert (to_underlying (sig_entry->type_offset_in_section) == 0);
a2ce51a0 6951 gdb_assert (sig_entry->type_unit_group == NULL);
7ee85ab1
DE
6952 gdb_assert (sig_entry->dwo_unit == NULL);
6953
6954 sig_entry->per_cu.section = dwo_entry->section;
9c541725 6955 sig_entry->per_cu.sect_off = dwo_entry->sect_off;
7ee85ab1
DE
6956 sig_entry->per_cu.length = dwo_entry->length;
6957 sig_entry->per_cu.reading_dwo_directly = 1;
e3b94546 6958 sig_entry->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
a2ce51a0
DE
6959 sig_entry->type_offset_in_tu = dwo_entry->type_offset_in_tu;
6960 sig_entry->dwo_unit = dwo_entry;
6961}
6962
6963/* Subroutine of lookup_signatured_type.
7ee85ab1
DE
6964 If we haven't read the TU yet, create the signatured_type data structure
6965 for a TU to be read in directly from a DWO file, bypassing the stub.
6966 This is the "Stay in DWO Optimization": When there is no DWP file and we're
6967 using .gdb_index, then when reading a CU we want to stay in the DWO file
6968 containing that CU. Otherwise we could end up reading several other DWO
6969 files (due to comdat folding) to process the transitive closure of all the
6970 mentioned TUs, and that can be slow. The current DWO file will have every
6971 type signature that it needs.
a2ce51a0
DE
6972 We only do this for .gdb_index because in the psymtab case we already have
6973 to read all the DWOs to build the type unit groups. */
6974
6975static struct signatured_type *
6976lookup_dwo_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6977{
518817b3
SM
6978 struct dwarf2_per_objfile *dwarf2_per_objfile
6979 = cu->per_cu->dwarf2_per_objfile;
a2ce51a0
DE
6980 struct objfile *objfile = dwarf2_per_objfile->objfile;
6981 struct dwo_file *dwo_file;
6982 struct dwo_unit find_dwo_entry, *dwo_entry;
6983 struct signatured_type find_sig_entry, *sig_entry;
6aa5f3a6 6984 void **slot;
a2ce51a0
DE
6985
6986 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
6987
6aa5f3a6
DE
6988 /* If TU skeletons have been removed then we may not have read in any
6989 TUs yet. */
6990 if (dwarf2_per_objfile->signatured_types == NULL)
6991 {
6992 dwarf2_per_objfile->signatured_types
6993 = allocate_signatured_type_table (objfile);
6994 }
a2ce51a0
DE
6995
6996 /* We only ever need to read in one copy of a signatured type.
6aa5f3a6
DE
6997 Use the global signatured_types array to do our own comdat-folding
6998 of types. If this is the first time we're reading this TU, and
6999 the TU has an entry in .gdb_index, replace the recorded data from
7000 .gdb_index with this TU. */
a2ce51a0 7001
a2ce51a0 7002 find_sig_entry.signature = sig;
6aa5f3a6
DE
7003 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
7004 &find_sig_entry, INSERT);
9a3c8263 7005 sig_entry = (struct signatured_type *) *slot;
7ee85ab1
DE
7006
7007 /* We can get here with the TU already read, *or* in the process of being
6aa5f3a6
DE
7008 read. Don't reassign the global entry to point to this DWO if that's
7009 the case. Also note that if the TU is already being read, it may not
7010 have come from a DWO, the program may be a mix of Fission-compiled
7011 code and non-Fission-compiled code. */
7012
7013 /* Have we already tried to read this TU?
7014 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
7015 needn't exist in the global table yet). */
7016 if (sig_entry != NULL && sig_entry->per_cu.tu_read)
a2ce51a0
DE
7017 return sig_entry;
7018
6aa5f3a6
DE
7019 /* Note: cu->dwo_unit is the dwo_unit that references this TU, not the
7020 dwo_unit of the TU itself. */
7021 dwo_file = cu->dwo_unit->dwo_file;
7022
a2ce51a0
DE
7023 /* Ok, this is the first time we're reading this TU. */
7024 if (dwo_file->tus == NULL)
7025 return NULL;
7026 find_dwo_entry.signature = sig;
9a3c8263 7027 dwo_entry = (struct dwo_unit *) htab_find (dwo_file->tus, &find_dwo_entry);
a2ce51a0
DE
7028 if (dwo_entry == NULL)
7029 return NULL;
7030
6aa5f3a6
DE
7031 /* If the global table doesn't have an entry for this TU, add one. */
7032 if (sig_entry == NULL)
ed2dc618 7033 sig_entry = add_type_unit (dwarf2_per_objfile, sig, slot);
6aa5f3a6 7034
ed2dc618 7035 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, sig_entry, dwo_entry);
89e63ee4 7036 sig_entry->per_cu.tu_read = 1;
a2ce51a0
DE
7037 return sig_entry;
7038}
7039
a2ce51a0
DE
7040/* Subroutine of lookup_signatured_type.
7041 Look up the type for signature SIG, and if we can't find SIG in .gdb_index
6aa5f3a6
DE
7042 then try the DWP file. If the TU stub (skeleton) has been removed then
7043 it won't be in .gdb_index. */
a2ce51a0
DE
7044
7045static struct signatured_type *
7046lookup_dwp_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
7047{
518817b3
SM
7048 struct dwarf2_per_objfile *dwarf2_per_objfile
7049 = cu->per_cu->dwarf2_per_objfile;
a2ce51a0 7050 struct objfile *objfile = dwarf2_per_objfile->objfile;
ed2dc618 7051 struct dwp_file *dwp_file = get_dwp_file (dwarf2_per_objfile);
a2ce51a0
DE
7052 struct dwo_unit *dwo_entry;
7053 struct signatured_type find_sig_entry, *sig_entry;
6aa5f3a6 7054 void **slot;
a2ce51a0
DE
7055
7056 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
7057 gdb_assert (dwp_file != NULL);
7058
6aa5f3a6
DE
7059 /* If TU skeletons have been removed then we may not have read in any
7060 TUs yet. */
7061 if (dwarf2_per_objfile->signatured_types == NULL)
a2ce51a0 7062 {
6aa5f3a6
DE
7063 dwarf2_per_objfile->signatured_types
7064 = allocate_signatured_type_table (objfile);
a2ce51a0
DE
7065 }
7066
6aa5f3a6
DE
7067 find_sig_entry.signature = sig;
7068 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
7069 &find_sig_entry, INSERT);
9a3c8263 7070 sig_entry = (struct signatured_type *) *slot;
6aa5f3a6
DE
7071
7072 /* Have we already tried to read this TU?
7073 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
7074 needn't exist in the global table yet). */
7075 if (sig_entry != NULL)
7076 return sig_entry;
7077
a2ce51a0
DE
7078 if (dwp_file->tus == NULL)
7079 return NULL;
ed2dc618 7080 dwo_entry = lookup_dwo_unit_in_dwp (dwarf2_per_objfile, dwp_file, NULL,
57d63ce2 7081 sig, 1 /* is_debug_types */);
a2ce51a0
DE
7082 if (dwo_entry == NULL)
7083 return NULL;
7084
ed2dc618
SM
7085 sig_entry = add_type_unit (dwarf2_per_objfile, sig, slot);
7086 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, sig_entry, dwo_entry);
a2ce51a0 7087
a2ce51a0
DE
7088 return sig_entry;
7089}
7090
380bca97 7091/* Lookup a signature based type for DW_FORM_ref_sig8.
5a8b3f62
DE
7092 Returns NULL if signature SIG is not present in the table.
7093 It is up to the caller to complain about this. */
348e048f
DE
7094
7095static struct signatured_type *
a2ce51a0 7096lookup_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
348e048f 7097{
518817b3
SM
7098 struct dwarf2_per_objfile *dwarf2_per_objfile
7099 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 7100
a2ce51a0
DE
7101 if (cu->dwo_unit
7102 && dwarf2_per_objfile->using_index)
7103 {
7104 /* We're in a DWO/DWP file, and we're using .gdb_index.
7105 These cases require special processing. */
ed2dc618 7106 if (get_dwp_file (dwarf2_per_objfile) == NULL)
a2ce51a0
DE
7107 return lookup_dwo_signatured_type (cu, sig);
7108 else
7109 return lookup_dwp_signatured_type (cu, sig);
7110 }
7111 else
7112 {
7113 struct signatured_type find_entry, *entry;
348e048f 7114
a2ce51a0
DE
7115 if (dwarf2_per_objfile->signatured_types == NULL)
7116 return NULL;
7117 find_entry.signature = sig;
9a3c8263
SM
7118 entry = ((struct signatured_type *)
7119 htab_find (dwarf2_per_objfile->signatured_types, &find_entry));
a2ce51a0
DE
7120 return entry;
7121 }
348e048f 7122}
42e7ad6c
DE
7123\f
7124/* Low level DIE reading support. */
348e048f 7125
d85a05f0
DJ
7126/* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
7127
7128static void
7129init_cu_die_reader (struct die_reader_specs *reader,
dee91e82 7130 struct dwarf2_cu *cu,
3019eac3 7131 struct dwarf2_section_info *section,
685af9cd
TT
7132 struct dwo_file *dwo_file,
7133 struct abbrev_table *abbrev_table)
d85a05f0 7134{
fceca515 7135 gdb_assert (section->readin && section->buffer != NULL);
a32a8923 7136 reader->abfd = get_section_bfd_owner (section);
d85a05f0 7137 reader->cu = cu;
3019eac3 7138 reader->dwo_file = dwo_file;
dee91e82
DE
7139 reader->die_section = section;
7140 reader->buffer = section->buffer;
f664829e 7141 reader->buffer_end = section->buffer + section->size;
a2ce51a0 7142 reader->comp_dir = NULL;
685af9cd 7143 reader->abbrev_table = abbrev_table;
d85a05f0
DJ
7144}
7145
b0c7bfa9
DE
7146/* Subroutine of init_cutu_and_read_dies to simplify it.
7147 Read in the rest of a CU/TU top level DIE from DWO_UNIT.
7148 There's just a lot of work to do, and init_cutu_and_read_dies is big enough
7149 already.
7150
7151 STUB_COMP_UNIT_DIE is for the stub DIE, we copy over certain attributes
7152 from it to the DIE in the DWO. If NULL we are skipping the stub.
a2ce51a0
DE
7153 STUB_COMP_DIR is similar to STUB_COMP_UNIT_DIE: When reading a TU directly
7154 from the DWO file, bypassing the stub, it contains the DW_AT_comp_dir
c54a1dd8
DE
7155 attribute of the referencing CU. At most one of STUB_COMP_UNIT_DIE and
7156 STUB_COMP_DIR may be non-NULL.
b0c7bfa9
DE
7157 *RESULT_READER,*RESULT_INFO_PTR,*RESULT_COMP_UNIT_DIE,*RESULT_HAS_CHILDREN
7158 are filled in with the info of the DIE from the DWO file.
685af9cd
TT
7159 *RESULT_DWO_ABBREV_TABLE will be filled in with the abbrev table allocated
7160 from the dwo. Since *RESULT_READER references this abbrev table, it must be
7161 kept around for at least as long as *RESULT_READER.
7162
b0c7bfa9
DE
7163 The result is non-zero if a valid (non-dummy) DIE was found. */
7164
7165static int
7166read_cutu_die_from_dwo (struct dwarf2_per_cu_data *this_cu,
7167 struct dwo_unit *dwo_unit,
b0c7bfa9 7168 struct die_info *stub_comp_unit_die,
a2ce51a0 7169 const char *stub_comp_dir,
b0c7bfa9 7170 struct die_reader_specs *result_reader,
d521ce57 7171 const gdb_byte **result_info_ptr,
b0c7bfa9 7172 struct die_info **result_comp_unit_die,
685af9cd
TT
7173 int *result_has_children,
7174 abbrev_table_up *result_dwo_abbrev_table)
b0c7bfa9 7175{
ed2dc618 7176 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
b0c7bfa9
DE
7177 struct objfile *objfile = dwarf2_per_objfile->objfile;
7178 struct dwarf2_cu *cu = this_cu->cu;
b0c7bfa9 7179 bfd *abfd;
d521ce57 7180 const gdb_byte *begin_info_ptr, *info_ptr;
b0c7bfa9
DE
7181 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
7182 int i,num_extra_attrs;
7183 struct dwarf2_section_info *dwo_abbrev_section;
7184 struct attribute *attr;
7185 struct die_info *comp_unit_die;
7186
b0aeadb3
DE
7187 /* At most one of these may be provided. */
7188 gdb_assert ((stub_comp_unit_die != NULL) + (stub_comp_dir != NULL) <= 1);
a2ce51a0 7189
b0c7bfa9
DE
7190 /* These attributes aren't processed until later:
7191 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
0d60c288
DE
7192 DW_AT_comp_dir is used now, to find the DWO file, but it is also
7193 referenced later. However, these attributes are found in the stub
7194 which we won't have later. In order to not impose this complication
7195 on the rest of the code, we read them here and copy them to the
7196 DWO CU/TU die. */
b0c7bfa9
DE
7197
7198 stmt_list = NULL;
7199 low_pc = NULL;
7200 high_pc = NULL;
7201 ranges = NULL;
7202 comp_dir = NULL;
7203
7204 if (stub_comp_unit_die != NULL)
7205 {
7206 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
7207 DWO file. */
7208 if (! this_cu->is_debug_types)
7209 stmt_list = dwarf2_attr (stub_comp_unit_die, DW_AT_stmt_list, cu);
7210 low_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_low_pc, cu);
7211 high_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_high_pc, cu);
7212 ranges = dwarf2_attr (stub_comp_unit_die, DW_AT_ranges, cu);
7213 comp_dir = dwarf2_attr (stub_comp_unit_die, DW_AT_comp_dir, cu);
7214
7215 /* There should be a DW_AT_addr_base attribute here (if needed).
7216 We need the value before we can process DW_FORM_GNU_addr_index. */
7217 cu->addr_base = 0;
7218 attr = dwarf2_attr (stub_comp_unit_die, DW_AT_GNU_addr_base, cu);
7219 if (attr)
7220 cu->addr_base = DW_UNSND (attr);
7221
7222 /* There should be a DW_AT_ranges_base attribute here (if needed).
7223 We need the value before we can process DW_AT_ranges. */
7224 cu->ranges_base = 0;
7225 attr = dwarf2_attr (stub_comp_unit_die, DW_AT_GNU_ranges_base, cu);
7226 if (attr)
7227 cu->ranges_base = DW_UNSND (attr);
7228 }
a2ce51a0
DE
7229 else if (stub_comp_dir != NULL)
7230 {
7231 /* Reconstruct the comp_dir attribute to simplify the code below. */
8d749320 7232 comp_dir = XOBNEW (&cu->comp_unit_obstack, struct attribute);
a2ce51a0
DE
7233 comp_dir->name = DW_AT_comp_dir;
7234 comp_dir->form = DW_FORM_string;
7235 DW_STRING_IS_CANONICAL (comp_dir) = 0;
7236 DW_STRING (comp_dir) = stub_comp_dir;
7237 }
b0c7bfa9
DE
7238
7239 /* Set up for reading the DWO CU/TU. */
7240 cu->dwo_unit = dwo_unit;
685af9cd 7241 dwarf2_section_info *section = dwo_unit->section;
b0c7bfa9 7242 dwarf2_read_section (objfile, section);
a32a8923 7243 abfd = get_section_bfd_owner (section);
9c541725
PA
7244 begin_info_ptr = info_ptr = (section->buffer
7245 + to_underlying (dwo_unit->sect_off));
b0c7bfa9 7246 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
b0c7bfa9
DE
7247
7248 if (this_cu->is_debug_types)
7249 {
b0c7bfa9
DE
7250 struct signatured_type *sig_type = (struct signatured_type *) this_cu;
7251
ed2dc618
SM
7252 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7253 &cu->header, section,
b0c7bfa9 7254 dwo_abbrev_section,
43988095 7255 info_ptr, rcuh_kind::TYPE);
a2ce51a0 7256 /* This is not an assert because it can be caused by bad debug info. */
43988095 7257 if (sig_type->signature != cu->header.signature)
a2ce51a0
DE
7258 {
7259 error (_("Dwarf Error: signature mismatch %s vs %s while reading"
9d8780f0 7260 " TU at offset %s [in module %s]"),
a2ce51a0 7261 hex_string (sig_type->signature),
43988095 7262 hex_string (cu->header.signature),
9d8780f0 7263 sect_offset_str (dwo_unit->sect_off),
a2ce51a0
DE
7264 bfd_get_filename (abfd));
7265 }
9c541725 7266 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
b0c7bfa9
DE
7267 /* For DWOs coming from DWP files, we don't know the CU length
7268 nor the type's offset in the TU until now. */
7269 dwo_unit->length = get_cu_length (&cu->header);
9c541725 7270 dwo_unit->type_offset_in_tu = cu->header.type_cu_offset_in_tu;
b0c7bfa9
DE
7271
7272 /* Establish the type offset that can be used to lookup the type.
7273 For DWO files, we don't know it until now. */
9c541725
PA
7274 sig_type->type_offset_in_section
7275 = dwo_unit->sect_off + to_underlying (dwo_unit->type_offset_in_tu);
b0c7bfa9
DE
7276 }
7277 else
7278 {
ed2dc618
SM
7279 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7280 &cu->header, section,
b0c7bfa9 7281 dwo_abbrev_section,
43988095 7282 info_ptr, rcuh_kind::COMPILE);
9c541725 7283 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
b0c7bfa9
DE
7284 /* For DWOs coming from DWP files, we don't know the CU length
7285 until now. */
7286 dwo_unit->length = get_cu_length (&cu->header);
7287 }
7288
685af9cd
TT
7289 *result_dwo_abbrev_table
7290 = abbrev_table_read_table (dwarf2_per_objfile, dwo_abbrev_section,
7291 cu->header.abbrev_sect_off);
7292 init_cu_die_reader (result_reader, cu, section, dwo_unit->dwo_file,
7293 result_dwo_abbrev_table->get ());
b0c7bfa9
DE
7294
7295 /* Read in the die, but leave space to copy over the attributes
7296 from the stub. This has the benefit of simplifying the rest of
7297 the code - all the work to maintain the illusion of a single
7298 DW_TAG_{compile,type}_unit DIE is done here. */
7299 num_extra_attrs = ((stmt_list != NULL)
7300 + (low_pc != NULL)
7301 + (high_pc != NULL)
7302 + (ranges != NULL)
7303 + (comp_dir != NULL));
7304 info_ptr = read_full_die_1 (result_reader, result_comp_unit_die, info_ptr,
7305 result_has_children, num_extra_attrs);
7306
7307 /* Copy over the attributes from the stub to the DIE we just read in. */
7308 comp_unit_die = *result_comp_unit_die;
7309 i = comp_unit_die->num_attrs;
7310 if (stmt_list != NULL)
7311 comp_unit_die->attrs[i++] = *stmt_list;
7312 if (low_pc != NULL)
7313 comp_unit_die->attrs[i++] = *low_pc;
7314 if (high_pc != NULL)
7315 comp_unit_die->attrs[i++] = *high_pc;
7316 if (ranges != NULL)
7317 comp_unit_die->attrs[i++] = *ranges;
7318 if (comp_dir != NULL)
7319 comp_unit_die->attrs[i++] = *comp_dir;
7320 comp_unit_die->num_attrs += num_extra_attrs;
7321
b4f54984 7322 if (dwarf_die_debug)
bf6af496
DE
7323 {
7324 fprintf_unfiltered (gdb_stdlog,
7325 "Read die from %s@0x%x of %s:\n",
a32a8923 7326 get_section_name (section),
bf6af496
DE
7327 (unsigned) (begin_info_ptr - section->buffer),
7328 bfd_get_filename (abfd));
b4f54984 7329 dump_die (comp_unit_die, dwarf_die_debug);
bf6af496
DE
7330 }
7331
a2ce51a0
DE
7332 /* Save the comp_dir attribute. If there is no DWP file then we'll read
7333 TUs by skipping the stub and going directly to the entry in the DWO file.
7334 However, skipping the stub means we won't get DW_AT_comp_dir, so we have
7335 to get it via circuitous means. Blech. */
7336 if (comp_dir != NULL)
7337 result_reader->comp_dir = DW_STRING (comp_dir);
7338
b0c7bfa9
DE
7339 /* Skip dummy compilation units. */
7340 if (info_ptr >= begin_info_ptr + dwo_unit->length
7341 || peek_abbrev_code (abfd, info_ptr) == 0)
7342 return 0;
7343
7344 *result_info_ptr = info_ptr;
7345 return 1;
7346}
7347
7348/* Subroutine of init_cutu_and_read_dies to simplify it.
7349 Look up the DWO unit specified by COMP_UNIT_DIE of THIS_CU.
6a506a2d 7350 Returns NULL if the specified DWO unit cannot be found. */
b0c7bfa9
DE
7351
7352static struct dwo_unit *
7353lookup_dwo_unit (struct dwarf2_per_cu_data *this_cu,
7354 struct die_info *comp_unit_die)
7355{
7356 struct dwarf2_cu *cu = this_cu->cu;
b0c7bfa9
DE
7357 ULONGEST signature;
7358 struct dwo_unit *dwo_unit;
7359 const char *comp_dir, *dwo_name;
7360
a2ce51a0
DE
7361 gdb_assert (cu != NULL);
7362
b0c7bfa9 7363 /* Yeah, we look dwo_name up again, but it simplifies the code. */
7d45c7c3
KB
7364 dwo_name = dwarf2_string_attr (comp_unit_die, DW_AT_GNU_dwo_name, cu);
7365 comp_dir = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
b0c7bfa9
DE
7366
7367 if (this_cu->is_debug_types)
7368 {
7369 struct signatured_type *sig_type;
7370
7371 /* Since this_cu is the first member of struct signatured_type,
7372 we can go from a pointer to one to a pointer to the other. */
7373 sig_type = (struct signatured_type *) this_cu;
7374 signature = sig_type->signature;
7375 dwo_unit = lookup_dwo_type_unit (sig_type, dwo_name, comp_dir);
7376 }
7377 else
7378 {
7379 struct attribute *attr;
7380
7381 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
7382 if (! attr)
7383 error (_("Dwarf Error: missing dwo_id for dwo_name %s"
7384 " [in module %s]"),
e3b94546 7385 dwo_name, objfile_name (this_cu->dwarf2_per_objfile->objfile));
b0c7bfa9
DE
7386 signature = DW_UNSND (attr);
7387 dwo_unit = lookup_dwo_comp_unit (this_cu, dwo_name, comp_dir,
7388 signature);
7389 }
7390
b0c7bfa9
DE
7391 return dwo_unit;
7392}
7393
a2ce51a0 7394/* Subroutine of init_cutu_and_read_dies to simplify it.
6aa5f3a6 7395 See it for a description of the parameters.
fcd3b13d 7396 Read a TU directly from a DWO file, bypassing the stub. */
a2ce51a0
DE
7397
7398static void
6aa5f3a6
DE
7399init_tu_and_read_dwo_dies (struct dwarf2_per_cu_data *this_cu,
7400 int use_existing_cu, int keep,
a2ce51a0
DE
7401 die_reader_func_ftype *die_reader_func,
7402 void *data)
7403{
fcd3b13d 7404 std::unique_ptr<dwarf2_cu> new_cu;
a2ce51a0 7405 struct signatured_type *sig_type;
a2ce51a0
DE
7406 struct die_reader_specs reader;
7407 const gdb_byte *info_ptr;
7408 struct die_info *comp_unit_die;
7409 int has_children;
ed2dc618 7410 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
a2ce51a0
DE
7411
7412 /* Verify we can do the following downcast, and that we have the
7413 data we need. */
7414 gdb_assert (this_cu->is_debug_types && this_cu->reading_dwo_directly);
7415 sig_type = (struct signatured_type *) this_cu;
7416 gdb_assert (sig_type->dwo_unit != NULL);
7417
6aa5f3a6
DE
7418 if (use_existing_cu && this_cu->cu != NULL)
7419 {
7420 gdb_assert (this_cu->cu->dwo_unit == sig_type->dwo_unit);
6aa5f3a6
DE
7421 /* There's no need to do the rereading_dwo_cu handling that
7422 init_cutu_and_read_dies does since we don't read the stub. */
7423 }
7424 else
7425 {
7426 /* If !use_existing_cu, this_cu->cu must be NULL. */
7427 gdb_assert (this_cu->cu == NULL);
fcd3b13d 7428 new_cu.reset (new dwarf2_cu (this_cu));
6aa5f3a6
DE
7429 }
7430
7431 /* A future optimization, if needed, would be to use an existing
7432 abbrev table. When reading DWOs with skeletonless TUs, all the TUs
7433 could share abbrev tables. */
a2ce51a0 7434
685af9cd
TT
7435 /* The abbreviation table used by READER, this must live at least as long as
7436 READER. */
7437 abbrev_table_up dwo_abbrev_table;
7438
a2ce51a0 7439 if (read_cutu_die_from_dwo (this_cu, sig_type->dwo_unit,
a2ce51a0
DE
7440 NULL /* stub_comp_unit_die */,
7441 sig_type->dwo_unit->dwo_file->comp_dir,
7442 &reader, &info_ptr,
685af9cd
TT
7443 &comp_unit_die, &has_children,
7444 &dwo_abbrev_table) == 0)
a2ce51a0
DE
7445 {
7446 /* Dummy die. */
a2ce51a0
DE
7447 return;
7448 }
7449
7450 /* All the "real" work is done here. */
7451 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
7452
6aa5f3a6 7453 /* This duplicates the code in init_cutu_and_read_dies,
a2ce51a0
DE
7454 but the alternative is making the latter more complex.
7455 This function is only for the special case of using DWO files directly:
7456 no point in overly complicating the general case just to handle this. */
fcd3b13d 7457 if (new_cu != NULL && keep)
a2ce51a0 7458 {
fcd3b13d
SM
7459 /* Link this CU into read_in_chain. */
7460 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
7461 dwarf2_per_objfile->read_in_chain = this_cu;
7462 /* The chain owns it now. */
7463 new_cu.release ();
a2ce51a0 7464 }
a2ce51a0
DE
7465}
7466
fd820528 7467/* Initialize a CU (or TU) and read its DIEs.
3019eac3 7468 If the CU defers to a DWO file, read the DWO file as well.
dee91e82 7469
f4dc4d17
DE
7470 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
7471 Otherwise the table specified in the comp unit header is read in and used.
7472 This is an optimization for when we already have the abbrev table.
7473
dee91e82
DE
7474 If USE_EXISTING_CU is non-zero, and THIS_CU->cu is non-NULL, then use it.
7475 Otherwise, a new CU is allocated with xmalloc.
7476
7477 If KEEP is non-zero, then if we allocated a dwarf2_cu we add it to
7478 read_in_chain. Otherwise the dwarf2_cu data is freed at the end.
7479
7480 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
fd820528 7481 linker) then DIE_READER_FUNC will not get called. */
aaa75496 7482
70221824 7483static void
fd820528 7484init_cutu_and_read_dies (struct dwarf2_per_cu_data *this_cu,
f4dc4d17 7485 struct abbrev_table *abbrev_table,
fd820528
DE
7486 int use_existing_cu, int keep,
7487 die_reader_func_ftype *die_reader_func,
7488 void *data)
c906108c 7489{
ed2dc618 7490 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
dee91e82 7491 struct objfile *objfile = dwarf2_per_objfile->objfile;
8a0459fd 7492 struct dwarf2_section_info *section = this_cu->section;
a32a8923 7493 bfd *abfd = get_section_bfd_owner (section);
dee91e82 7494 struct dwarf2_cu *cu;
d521ce57 7495 const gdb_byte *begin_info_ptr, *info_ptr;
dee91e82 7496 struct die_reader_specs reader;
d85a05f0 7497 struct die_info *comp_unit_die;
dee91e82 7498 int has_children;
d85a05f0 7499 struct attribute *attr;
dee91e82 7500 struct signatured_type *sig_type = NULL;
4bdcc0c1 7501 struct dwarf2_section_info *abbrev_section;
42e7ad6c
DE
7502 /* Non-zero if CU currently points to a DWO file and we need to
7503 reread it. When this happens we need to reread the skeleton die
a2ce51a0 7504 before we can reread the DWO file (this only applies to CUs, not TUs). */
42e7ad6c 7505 int rereading_dwo_cu = 0;
c906108c 7506
b4f54984 7507 if (dwarf_die_debug)
9d8780f0 7508 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
09406207 7509 this_cu->is_debug_types ? "type" : "comp",
9d8780f0 7510 sect_offset_str (this_cu->sect_off));
09406207 7511
dee91e82
DE
7512 if (use_existing_cu)
7513 gdb_assert (keep);
23745b47 7514
a2ce51a0
DE
7515 /* If we're reading a TU directly from a DWO file, including a virtual DWO
7516 file (instead of going through the stub), short-circuit all of this. */
7517 if (this_cu->reading_dwo_directly)
7518 {
7519 /* Narrow down the scope of possibilities to have to understand. */
7520 gdb_assert (this_cu->is_debug_types);
7521 gdb_assert (abbrev_table == NULL);
6aa5f3a6
DE
7522 init_tu_and_read_dwo_dies (this_cu, use_existing_cu, keep,
7523 die_reader_func, data);
a2ce51a0
DE
7524 return;
7525 }
7526
dee91e82
DE
7527 /* This is cheap if the section is already read in. */
7528 dwarf2_read_section (objfile, section);
7529
9c541725 7530 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
36586728
TT
7531
7532 abbrev_section = get_abbrev_section_for_cu (this_cu);
dee91e82 7533
fcd3b13d 7534 std::unique_ptr<dwarf2_cu> new_cu;
dee91e82
DE
7535 if (use_existing_cu && this_cu->cu != NULL)
7536 {
7537 cu = this_cu->cu;
42e7ad6c
DE
7538 /* If this CU is from a DWO file we need to start over, we need to
7539 refetch the attributes from the skeleton CU.
7540 This could be optimized by retrieving those attributes from when we
7541 were here the first time: the previous comp_unit_die was stored in
7542 comp_unit_obstack. But there's no data yet that we need this
7543 optimization. */
7544 if (cu->dwo_unit != NULL)
7545 rereading_dwo_cu = 1;
dee91e82
DE
7546 }
7547 else
7548 {
7549 /* If !use_existing_cu, this_cu->cu must be NULL. */
7550 gdb_assert (this_cu->cu == NULL);
fcd3b13d
SM
7551 new_cu.reset (new dwarf2_cu (this_cu));
7552 cu = new_cu.get ();
42e7ad6c 7553 }
dee91e82 7554
b0c7bfa9 7555 /* Get the header. */
9c541725 7556 if (to_underlying (cu->header.first_die_cu_offset) != 0 && !rereading_dwo_cu)
42e7ad6c
DE
7557 {
7558 /* We already have the header, there's no need to read it in again. */
9c541725 7559 info_ptr += to_underlying (cu->header.first_die_cu_offset);
42e7ad6c
DE
7560 }
7561 else
7562 {
3019eac3 7563 if (this_cu->is_debug_types)
dee91e82 7564 {
ed2dc618
SM
7565 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7566 &cu->header, section,
4bdcc0c1 7567 abbrev_section, info_ptr,
43988095 7568 rcuh_kind::TYPE);
dee91e82 7569
42e7ad6c
DE
7570 /* Since per_cu is the first member of struct signatured_type,
7571 we can go from a pointer to one to a pointer to the other. */
7572 sig_type = (struct signatured_type *) this_cu;
43988095 7573 gdb_assert (sig_type->signature == cu->header.signature);
9c541725
PA
7574 gdb_assert (sig_type->type_offset_in_tu
7575 == cu->header.type_cu_offset_in_tu);
7576 gdb_assert (this_cu->sect_off == cu->header.sect_off);
dee91e82 7577
42e7ad6c
DE
7578 /* LENGTH has not been set yet for type units if we're
7579 using .gdb_index. */
1ce1cefd 7580 this_cu->length = get_cu_length (&cu->header);
3019eac3
DE
7581
7582 /* Establish the type offset that can be used to lookup the type. */
9c541725
PA
7583 sig_type->type_offset_in_section =
7584 this_cu->sect_off + to_underlying (sig_type->type_offset_in_tu);
43988095
JK
7585
7586 this_cu->dwarf_version = cu->header.version;
dee91e82
DE
7587 }
7588 else
7589 {
ed2dc618
SM
7590 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7591 &cu->header, section,
4bdcc0c1 7592 abbrev_section,
43988095
JK
7593 info_ptr,
7594 rcuh_kind::COMPILE);
dee91e82 7595
9c541725 7596 gdb_assert (this_cu->sect_off == cu->header.sect_off);
1ce1cefd 7597 gdb_assert (this_cu->length == get_cu_length (&cu->header));
43988095 7598 this_cu->dwarf_version = cu->header.version;
dee91e82
DE
7599 }
7600 }
10b3939b 7601
6caca83c 7602 /* Skip dummy compilation units. */
dee91e82 7603 if (info_ptr >= begin_info_ptr + this_cu->length
6caca83c 7604 || peek_abbrev_code (abfd, info_ptr) == 0)
fcd3b13d 7605 return;
6caca83c 7606
433df2d4
DE
7607 /* If we don't have them yet, read the abbrevs for this compilation unit.
7608 And if we need to read them now, make sure they're freed when we're
685af9cd
TT
7609 done (own the table through ABBREV_TABLE_HOLDER). */
7610 abbrev_table_up abbrev_table_holder;
f4dc4d17 7611 if (abbrev_table != NULL)
685af9cd
TT
7612 gdb_assert (cu->header.abbrev_sect_off == abbrev_table->sect_off);
7613 else
f4dc4d17 7614 {
685af9cd
TT
7615 abbrev_table_holder
7616 = abbrev_table_read_table (dwarf2_per_objfile, abbrev_section,
7617 cu->header.abbrev_sect_off);
7618 abbrev_table = abbrev_table_holder.get ();
42e7ad6c 7619 }
af703f96 7620
dee91e82 7621 /* Read the top level CU/TU die. */
685af9cd 7622 init_cu_die_reader (&reader, cu, section, NULL, abbrev_table);
dee91e82 7623 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
93311388 7624
b0c7bfa9 7625 /* If we are in a DWO stub, process it and then read in the "real" CU/TU
685af9cd
TT
7626 from the DWO file. read_cutu_die_from_dwo will allocate the abbreviation
7627 table from the DWO file and pass the ownership over to us. It will be
7628 referenced from READER, so we must make sure to free it after we're done
7629 with READER.
7630
b0c7bfa9
DE
7631 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains a
7632 DWO CU, that this test will fail (the attribute will not be present). */
3019eac3 7633 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_name, cu);
685af9cd 7634 abbrev_table_up dwo_abbrev_table;
3019eac3
DE
7635 if (attr)
7636 {
3019eac3 7637 struct dwo_unit *dwo_unit;
b0c7bfa9 7638 struct die_info *dwo_comp_unit_die;
3019eac3
DE
7639
7640 if (has_children)
6a506a2d
DE
7641 {
7642 complaint (&symfile_complaints,
7643 _("compilation unit with DW_AT_GNU_dwo_name"
9d8780f0
SM
7644 " has children (offset %s) [in module %s]"),
7645 sect_offset_str (this_cu->sect_off),
7646 bfd_get_filename (abfd));
6a506a2d 7647 }
b0c7bfa9 7648 dwo_unit = lookup_dwo_unit (this_cu, comp_unit_die);
6a506a2d 7649 if (dwo_unit != NULL)
3019eac3 7650 {
6a506a2d 7651 if (read_cutu_die_from_dwo (this_cu, dwo_unit,
a2ce51a0 7652 comp_unit_die, NULL,
6a506a2d 7653 &reader, &info_ptr,
685af9cd
TT
7654 &dwo_comp_unit_die, &has_children,
7655 &dwo_abbrev_table) == 0)
6a506a2d
DE
7656 {
7657 /* Dummy die. */
6a506a2d
DE
7658 return;
7659 }
7660 comp_unit_die = dwo_comp_unit_die;
7661 }
7662 else
7663 {
7664 /* Yikes, we couldn't find the rest of the DIE, we only have
7665 the stub. A complaint has already been logged. There's
7666 not much more we can do except pass on the stub DIE to
7667 die_reader_func. We don't want to throw an error on bad
7668 debug info. */
3019eac3
DE
7669 }
7670 }
7671
b0c7bfa9 7672 /* All of the above is setup for this call. Yikes. */
dee91e82
DE
7673 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
7674
b0c7bfa9 7675 /* Done, clean up. */
fcd3b13d 7676 if (new_cu != NULL && keep)
348e048f 7677 {
fcd3b13d
SM
7678 /* Link this CU into read_in_chain. */
7679 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
7680 dwarf2_per_objfile->read_in_chain = this_cu;
7681 /* The chain owns it now. */
7682 new_cu.release ();
348e048f 7683 }
dee91e82
DE
7684}
7685
33e80786
DE
7686/* Read CU/TU THIS_CU but do not follow DW_AT_GNU_dwo_name if present.
7687 DWO_FILE, if non-NULL, is the DWO file to read (the caller is assumed
7688 to have already done the lookup to find the DWO file).
dee91e82
DE
7689
7690 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
3019eac3 7691 THIS_CU->is_debug_types, but nothing else.
dee91e82
DE
7692
7693 We fill in THIS_CU->length.
7694
7695 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
7696 linker) then DIE_READER_FUNC will not get called.
7697
7698 THIS_CU->cu is always freed when done.
3019eac3
DE
7699 This is done in order to not leave THIS_CU->cu in a state where we have
7700 to care whether it refers to the "main" CU or the DWO CU. */
dee91e82
DE
7701
7702static void
7703init_cutu_and_read_dies_no_follow (struct dwarf2_per_cu_data *this_cu,
3019eac3 7704 struct dwo_file *dwo_file,
dee91e82
DE
7705 die_reader_func_ftype *die_reader_func,
7706 void *data)
7707{
ed2dc618 7708 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
dee91e82 7709 struct objfile *objfile = dwarf2_per_objfile->objfile;
8a0459fd 7710 struct dwarf2_section_info *section = this_cu->section;
a32a8923 7711 bfd *abfd = get_section_bfd_owner (section);
33e80786 7712 struct dwarf2_section_info *abbrev_section;
d521ce57 7713 const gdb_byte *begin_info_ptr, *info_ptr;
dee91e82 7714 struct die_reader_specs reader;
dee91e82
DE
7715 struct die_info *comp_unit_die;
7716 int has_children;
7717
b4f54984 7718 if (dwarf_die_debug)
9d8780f0 7719 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
09406207 7720 this_cu->is_debug_types ? "type" : "comp",
9d8780f0 7721 sect_offset_str (this_cu->sect_off));
09406207 7722
dee91e82
DE
7723 gdb_assert (this_cu->cu == NULL);
7724
33e80786
DE
7725 abbrev_section = (dwo_file != NULL
7726 ? &dwo_file->sections.abbrev
7727 : get_abbrev_section_for_cu (this_cu));
7728
dee91e82
DE
7729 /* This is cheap if the section is already read in. */
7730 dwarf2_read_section (objfile, section);
7731
fcd3b13d 7732 struct dwarf2_cu cu (this_cu);
dee91e82 7733
9c541725 7734 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
ed2dc618
SM
7735 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7736 &cu.header, section,
4bdcc0c1 7737 abbrev_section, info_ptr,
43988095
JK
7738 (this_cu->is_debug_types
7739 ? rcuh_kind::TYPE
7740 : rcuh_kind::COMPILE));
dee91e82 7741
1ce1cefd 7742 this_cu->length = get_cu_length (&cu.header);
dee91e82
DE
7743
7744 /* Skip dummy compilation units. */
7745 if (info_ptr >= begin_info_ptr + this_cu->length
7746 || peek_abbrev_code (abfd, info_ptr) == 0)
fcd3b13d 7747 return;
72bf9492 7748
685af9cd
TT
7749 abbrev_table_up abbrev_table
7750 = abbrev_table_read_table (dwarf2_per_objfile, abbrev_section,
7751 cu.header.abbrev_sect_off);
dee91e82 7752
685af9cd 7753 init_cu_die_reader (&reader, &cu, section, dwo_file, abbrev_table.get ());
dee91e82
DE
7754 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
7755
7756 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
dee91e82
DE
7757}
7758
3019eac3
DE
7759/* Read a CU/TU, except that this does not look for DW_AT_GNU_dwo_name and
7760 does not lookup the specified DWO file.
7761 This cannot be used to read DWO files.
dee91e82
DE
7762
7763 THIS_CU->cu is always freed when done.
3019eac3
DE
7764 This is done in order to not leave THIS_CU->cu in a state where we have
7765 to care whether it refers to the "main" CU or the DWO CU.
7766 We can revisit this if the data shows there's a performance issue. */
dee91e82
DE
7767
7768static void
7769init_cutu_and_read_dies_simple (struct dwarf2_per_cu_data *this_cu,
7770 die_reader_func_ftype *die_reader_func,
7771 void *data)
7772{
33e80786 7773 init_cutu_and_read_dies_no_follow (this_cu, NULL, die_reader_func, data);
dee91e82 7774}
0018ea6f
DE
7775\f
7776/* Type Unit Groups.
dee91e82 7777
0018ea6f
DE
7778 Type Unit Groups are a way to collapse the set of all TUs (type units) into
7779 a more manageable set. The grouping is done by DW_AT_stmt_list entry
7780 so that all types coming from the same compilation (.o file) are grouped
7781 together. A future step could be to put the types in the same symtab as
7782 the CU the types ultimately came from. */
ff013f42 7783
f4dc4d17
DE
7784static hashval_t
7785hash_type_unit_group (const void *item)
7786{
9a3c8263
SM
7787 const struct type_unit_group *tu_group
7788 = (const struct type_unit_group *) item;
f4dc4d17 7789
094b34ac 7790 return hash_stmt_list_entry (&tu_group->hash);
f4dc4d17 7791}
348e048f
DE
7792
7793static int
f4dc4d17 7794eq_type_unit_group (const void *item_lhs, const void *item_rhs)
348e048f 7795{
9a3c8263
SM
7796 const struct type_unit_group *lhs = (const struct type_unit_group *) item_lhs;
7797 const struct type_unit_group *rhs = (const struct type_unit_group *) item_rhs;
348e048f 7798
094b34ac 7799 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
f4dc4d17 7800}
348e048f 7801
f4dc4d17
DE
7802/* Allocate a hash table for type unit groups. */
7803
7804static htab_t
ed2dc618 7805allocate_type_unit_groups_table (struct objfile *objfile)
f4dc4d17
DE
7806{
7807 return htab_create_alloc_ex (3,
7808 hash_type_unit_group,
7809 eq_type_unit_group,
7810 NULL,
ed2dc618 7811 &objfile->objfile_obstack,
f4dc4d17
DE
7812 hashtab_obstack_allocate,
7813 dummy_obstack_deallocate);
7814}
dee91e82 7815
f4dc4d17
DE
7816/* Type units that don't have DW_AT_stmt_list are grouped into their own
7817 partial symtabs. We combine several TUs per psymtab to not let the size
7818 of any one psymtab grow too big. */
7819#define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
7820#define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
dee91e82 7821
094b34ac 7822/* Helper routine for get_type_unit_group.
f4dc4d17
DE
7823 Create the type_unit_group object used to hold one or more TUs. */
7824
7825static struct type_unit_group *
094b34ac 7826create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
f4dc4d17 7827{
518817b3
SM
7828 struct dwarf2_per_objfile *dwarf2_per_objfile
7829 = cu->per_cu->dwarf2_per_objfile;
f4dc4d17 7830 struct objfile *objfile = dwarf2_per_objfile->objfile;
094b34ac 7831 struct dwarf2_per_cu_data *per_cu;
f4dc4d17 7832 struct type_unit_group *tu_group;
f4dc4d17
DE
7833
7834 tu_group = OBSTACK_ZALLOC (&objfile->objfile_obstack,
7835 struct type_unit_group);
094b34ac 7836 per_cu = &tu_group->per_cu;
518817b3 7837 per_cu->dwarf2_per_objfile = dwarf2_per_objfile;
f4dc4d17 7838
094b34ac
DE
7839 if (dwarf2_per_objfile->using_index)
7840 {
7841 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
7842 struct dwarf2_per_cu_quick_data);
094b34ac
DE
7843 }
7844 else
7845 {
9c541725 7846 unsigned int line_offset = to_underlying (line_offset_struct);
094b34ac
DE
7847 struct partial_symtab *pst;
7848 char *name;
7849
7850 /* Give the symtab a useful name for debug purposes. */
7851 if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
7852 name = xstrprintf ("<type_units_%d>",
7853 (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
7854 else
7855 name = xstrprintf ("<type_units_at_0x%x>", line_offset);
7856
7857 pst = create_partial_symtab (per_cu, name);
7858 pst->anonymous = 1;
f4dc4d17 7859
094b34ac
DE
7860 xfree (name);
7861 }
f4dc4d17 7862
094b34ac 7863 tu_group->hash.dwo_unit = cu->dwo_unit;
9c541725 7864 tu_group->hash.line_sect_off = line_offset_struct;
f4dc4d17
DE
7865
7866 return tu_group;
7867}
7868
094b34ac
DE
7869/* Look up the type_unit_group for type unit CU, and create it if necessary.
7870 STMT_LIST is a DW_AT_stmt_list attribute. */
f4dc4d17
DE
7871
7872static struct type_unit_group *
ff39bb5e 7873get_type_unit_group (struct dwarf2_cu *cu, const struct attribute *stmt_list)
f4dc4d17 7874{
518817b3
SM
7875 struct dwarf2_per_objfile *dwarf2_per_objfile
7876 = cu->per_cu->dwarf2_per_objfile;
f4dc4d17
DE
7877 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
7878 struct type_unit_group *tu_group;
7879 void **slot;
7880 unsigned int line_offset;
7881 struct type_unit_group type_unit_group_for_lookup;
7882
7883 if (dwarf2_per_objfile->type_unit_groups == NULL)
7884 {
7885 dwarf2_per_objfile->type_unit_groups =
ed2dc618 7886 allocate_type_unit_groups_table (dwarf2_per_objfile->objfile);
f4dc4d17
DE
7887 }
7888
7889 /* Do we need to create a new group, or can we use an existing one? */
7890
7891 if (stmt_list)
7892 {
7893 line_offset = DW_UNSND (stmt_list);
7894 ++tu_stats->nr_symtab_sharers;
7895 }
7896 else
7897 {
7898 /* Ugh, no stmt_list. Rare, but we have to handle it.
7899 We can do various things here like create one group per TU or
7900 spread them over multiple groups to split up the expansion work.
7901 To avoid worst case scenarios (too many groups or too large groups)
7902 we, umm, group them in bunches. */
7903 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
7904 | (tu_stats->nr_stmt_less_type_units
7905 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
7906 ++tu_stats->nr_stmt_less_type_units;
7907 }
7908
094b34ac 7909 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
9c541725 7910 type_unit_group_for_lookup.hash.line_sect_off = (sect_offset) line_offset;
f4dc4d17
DE
7911 slot = htab_find_slot (dwarf2_per_objfile->type_unit_groups,
7912 &type_unit_group_for_lookup, INSERT);
7913 if (*slot != NULL)
7914 {
9a3c8263 7915 tu_group = (struct type_unit_group *) *slot;
f4dc4d17
DE
7916 gdb_assert (tu_group != NULL);
7917 }
7918 else
7919 {
9c541725 7920 sect_offset line_offset_struct = (sect_offset) line_offset;
094b34ac 7921 tu_group = create_type_unit_group (cu, line_offset_struct);
f4dc4d17
DE
7922 *slot = tu_group;
7923 ++tu_stats->nr_symtabs;
7924 }
7925
7926 return tu_group;
7927}
0018ea6f
DE
7928\f
7929/* Partial symbol tables. */
7930
7931/* Create a psymtab named NAME and assign it to PER_CU.
7932
7933 The caller must fill in the following details:
7934 dirname, textlow, texthigh. */
7935
7936static struct partial_symtab *
7937create_partial_symtab (struct dwarf2_per_cu_data *per_cu, const char *name)
7938{
e3b94546 7939 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
0018ea6f
DE
7940 struct partial_symtab *pst;
7941
18a94d75 7942 pst = start_psymtab_common (objfile, name, 0,
af5bf4ad
SM
7943 objfile->global_psymbols,
7944 objfile->static_psymbols);
0018ea6f
DE
7945
7946 pst->psymtabs_addrmap_supported = 1;
7947
7948 /* This is the glue that links PST into GDB's symbol API. */
7949 pst->read_symtab_private = per_cu;
7950 pst->read_symtab = dwarf2_read_symtab;
7951 per_cu->v.psymtab = pst;
7952
7953 return pst;
7954}
7955
b93601f3
TT
7956/* The DATA object passed to process_psymtab_comp_unit_reader has this
7957 type. */
7958
7959struct process_psymtab_comp_unit_data
7960{
7961 /* True if we are reading a DW_TAG_partial_unit. */
7962
7963 int want_partial_unit;
7964
7965 /* The "pretend" language that is used if the CU doesn't declare a
7966 language. */
7967
7968 enum language pretend_language;
7969};
7970
0018ea6f
DE
7971/* die_reader_func for process_psymtab_comp_unit. */
7972
7973static void
7974process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
d521ce57 7975 const gdb_byte *info_ptr,
0018ea6f
DE
7976 struct die_info *comp_unit_die,
7977 int has_children,
7978 void *data)
7979{
7980 struct dwarf2_cu *cu = reader->cu;
518817b3 7981 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
3e29f34a 7982 struct gdbarch *gdbarch = get_objfile_arch (objfile);
0018ea6f 7983 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
0018ea6f
DE
7984 CORE_ADDR baseaddr;
7985 CORE_ADDR best_lowpc = 0, best_highpc = 0;
7986 struct partial_symtab *pst;
3a2b436a 7987 enum pc_bounds_kind cu_bounds_kind;
0018ea6f 7988 const char *filename;
9a3c8263
SM
7989 struct process_psymtab_comp_unit_data *info
7990 = (struct process_psymtab_comp_unit_data *) data;
0018ea6f 7991
b93601f3 7992 if (comp_unit_die->tag == DW_TAG_partial_unit && !info->want_partial_unit)
0018ea6f
DE
7993 return;
7994
7995 gdb_assert (! per_cu->is_debug_types);
7996
b93601f3 7997 prepare_one_comp_unit (cu, comp_unit_die, info->pretend_language);
0018ea6f
DE
7998
7999 cu->list_in_scope = &file_symbols;
8000
8001 /* Allocate a new partial symbol table structure. */
7d45c7c3
KB
8002 filename = dwarf2_string_attr (comp_unit_die, DW_AT_name, cu);
8003 if (filename == NULL)
0018ea6f 8004 filename = "";
0018ea6f
DE
8005
8006 pst = create_partial_symtab (per_cu, filename);
8007
8008 /* This must be done before calling dwarf2_build_include_psymtabs. */
7d45c7c3 8009 pst->dirname = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
0018ea6f
DE
8010
8011 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
8012
8013 dwarf2_find_base_address (comp_unit_die, cu);
8014
8015 /* Possibly set the default values of LOWPC and HIGHPC from
8016 `DW_AT_ranges'. */
3a2b436a
JK
8017 cu_bounds_kind = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
8018 &best_highpc, cu, pst);
8019 if (cu_bounds_kind == PC_BOUNDS_HIGH_LOW && best_lowpc < best_highpc)
0018ea6f
DE
8020 /* Store the contiguous range if it is not empty; it can be empty for
8021 CUs with no code. */
8022 addrmap_set_empty (objfile->psymtabs_addrmap,
3e29f34a
MR
8023 gdbarch_adjust_dwarf2_addr (gdbarch,
8024 best_lowpc + baseaddr),
8025 gdbarch_adjust_dwarf2_addr (gdbarch,
8026 best_highpc + baseaddr) - 1,
8027 pst);
0018ea6f
DE
8028
8029 /* Check if comp unit has_children.
8030 If so, read the rest of the partial symbols from this comp unit.
8031 If not, there's no more debug_info for this comp unit. */
8032 if (has_children)
8033 {
8034 struct partial_die_info *first_die;
8035 CORE_ADDR lowpc, highpc;
8036
8037 lowpc = ((CORE_ADDR) -1);
8038 highpc = ((CORE_ADDR) 0);
8039
8040 first_die = load_partial_dies (reader, info_ptr, 1);
8041
8042 scan_partial_symbols (first_die, &lowpc, &highpc,
e385593e 8043 cu_bounds_kind <= PC_BOUNDS_INVALID, cu);
0018ea6f
DE
8044
8045 /* If we didn't find a lowpc, set it to highpc to avoid
8046 complaints from `maint check'. */
8047 if (lowpc == ((CORE_ADDR) -1))
8048 lowpc = highpc;
8049
8050 /* If the compilation unit didn't have an explicit address range,
8051 then use the information extracted from its child dies. */
e385593e 8052 if (cu_bounds_kind <= PC_BOUNDS_INVALID)
0018ea6f
DE
8053 {
8054 best_lowpc = lowpc;
8055 best_highpc = highpc;
8056 }
8057 }
3e29f34a
MR
8058 pst->textlow = gdbarch_adjust_dwarf2_addr (gdbarch, best_lowpc + baseaddr);
8059 pst->texthigh = gdbarch_adjust_dwarf2_addr (gdbarch, best_highpc + baseaddr);
0018ea6f 8060
8763cede 8061 end_psymtab_common (objfile, pst);
0018ea6f
DE
8062
8063 if (!VEC_empty (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs))
8064 {
8065 int i;
8066 int len = VEC_length (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs);
8067 struct dwarf2_per_cu_data *iter;
8068
8069 /* Fill in 'dependencies' here; we fill in 'users' in a
8070 post-pass. */
8071 pst->number_of_dependencies = len;
8d749320
SM
8072 pst->dependencies =
8073 XOBNEWVEC (&objfile->objfile_obstack, struct partial_symtab *, len);
0018ea6f
DE
8074 for (i = 0;
8075 VEC_iterate (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs,
8076 i, iter);
8077 ++i)
8078 pst->dependencies[i] = iter->v.psymtab;
8079
8080 VEC_free (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs);
8081 }
8082
8083 /* Get the list of files included in the current compilation unit,
8084 and build a psymtab for each of them. */
8085 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
8086
b4f54984 8087 if (dwarf_read_debug)
0018ea6f
DE
8088 {
8089 struct gdbarch *gdbarch = get_objfile_arch (objfile);
8090
8091 fprintf_unfiltered (gdb_stdlog,
9d8780f0 8092 "Psymtab for %s unit @%s: %s - %s"
0018ea6f
DE
8093 ", %d global, %d static syms\n",
8094 per_cu->is_debug_types ? "type" : "comp",
9d8780f0 8095 sect_offset_str (per_cu->sect_off),
0018ea6f
DE
8096 paddress (gdbarch, pst->textlow),
8097 paddress (gdbarch, pst->texthigh),
8098 pst->n_global_syms, pst->n_static_syms);
8099 }
8100}
8101
8102/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
8103 Process compilation unit THIS_CU for a psymtab. */
8104
8105static void
8106process_psymtab_comp_unit (struct dwarf2_per_cu_data *this_cu,
b93601f3
TT
8107 int want_partial_unit,
8108 enum language pretend_language)
0018ea6f
DE
8109{
8110 /* If this compilation unit was already read in, free the
8111 cached copy in order to read it in again. This is
8112 necessary because we skipped some symbols when we first
8113 read in the compilation unit (see load_partial_dies).
8114 This problem could be avoided, but the benefit is unclear. */
8115 if (this_cu->cu != NULL)
8116 free_one_cached_comp_unit (this_cu);
8117
f1902523
JK
8118 if (this_cu->is_debug_types)
8119 init_cutu_and_read_dies (this_cu, NULL, 0, 0, build_type_psymtabs_reader,
8120 NULL);
8121 else
8122 {
8123 process_psymtab_comp_unit_data info;
8124 info.want_partial_unit = want_partial_unit;
8125 info.pretend_language = pretend_language;
8126 init_cutu_and_read_dies (this_cu, NULL, 0, 0,
8127 process_psymtab_comp_unit_reader, &info);
8128 }
0018ea6f
DE
8129
8130 /* Age out any secondary CUs. */
ed2dc618 8131 age_cached_comp_units (this_cu->dwarf2_per_objfile);
0018ea6f 8132}
f4dc4d17
DE
8133
8134/* Reader function for build_type_psymtabs. */
8135
8136static void
8137build_type_psymtabs_reader (const struct die_reader_specs *reader,
d521ce57 8138 const gdb_byte *info_ptr,
f4dc4d17
DE
8139 struct die_info *type_unit_die,
8140 int has_children,
8141 void *data)
8142{
ed2dc618 8143 struct dwarf2_per_objfile *dwarf2_per_objfile
518817b3 8144 = reader->cu->per_cu->dwarf2_per_objfile;
f4dc4d17
DE
8145 struct objfile *objfile = dwarf2_per_objfile->objfile;
8146 struct dwarf2_cu *cu = reader->cu;
8147 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
0186c6a7 8148 struct signatured_type *sig_type;
f4dc4d17
DE
8149 struct type_unit_group *tu_group;
8150 struct attribute *attr;
8151 struct partial_die_info *first_die;
8152 CORE_ADDR lowpc, highpc;
8153 struct partial_symtab *pst;
8154
8155 gdb_assert (data == NULL);
0186c6a7
DE
8156 gdb_assert (per_cu->is_debug_types);
8157 sig_type = (struct signatured_type *) per_cu;
f4dc4d17
DE
8158
8159 if (! has_children)
8160 return;
8161
8162 attr = dwarf2_attr_no_follow (type_unit_die, DW_AT_stmt_list);
094b34ac 8163 tu_group = get_type_unit_group (cu, attr);
f4dc4d17 8164
0186c6a7 8165 VEC_safe_push (sig_type_ptr, tu_group->tus, sig_type);
f4dc4d17
DE
8166
8167 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
8168 cu->list_in_scope = &file_symbols;
8169 pst = create_partial_symtab (per_cu, "");
8170 pst->anonymous = 1;
8171
8172 first_die = load_partial_dies (reader, info_ptr, 1);
8173
8174 lowpc = (CORE_ADDR) -1;
8175 highpc = (CORE_ADDR) 0;
8176 scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
8177
8763cede 8178 end_psymtab_common (objfile, pst);
f4dc4d17
DE
8179}
8180
73051182
DE
8181/* Struct used to sort TUs by their abbreviation table offset. */
8182
8183struct tu_abbrev_offset
8184{
8185 struct signatured_type *sig_type;
8186 sect_offset abbrev_offset;
8187};
8188
484cf504 8189/* Helper routine for build_type_psymtabs_1, passed to std::sort. */
73051182 8190
484cf504
TT
8191static bool
8192sort_tu_by_abbrev_offset (const struct tu_abbrev_offset &a,
8193 const struct tu_abbrev_offset &b)
73051182 8194{
484cf504 8195 return a.abbrev_offset < b.abbrev_offset;
73051182
DE
8196}
8197
8198/* Efficiently read all the type units.
8199 This does the bulk of the work for build_type_psymtabs.
8200
8201 The efficiency is because we sort TUs by the abbrev table they use and
8202 only read each abbrev table once. In one program there are 200K TUs
8203 sharing 8K abbrev tables.
8204
8205 The main purpose of this function is to support building the
8206 dwarf2_per_objfile->type_unit_groups table.
8207 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
8208 can collapse the search space by grouping them by stmt_list.
8209 The savings can be significant, in the same program from above the 200K TUs
8210 share 8K stmt_list tables.
8211
8212 FUNC is expected to call get_type_unit_group, which will create the
8213 struct type_unit_group if necessary and add it to
8214 dwarf2_per_objfile->type_unit_groups. */
8215
8216static void
ed2dc618 8217build_type_psymtabs_1 (struct dwarf2_per_objfile *dwarf2_per_objfile)
73051182 8218{
73051182 8219 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
685af9cd 8220 abbrev_table_up abbrev_table;
73051182 8221 sect_offset abbrev_offset;
73051182
DE
8222 int i;
8223
8224 /* It's up to the caller to not call us multiple times. */
8225 gdb_assert (dwarf2_per_objfile->type_unit_groups == NULL);
8226
8227 if (dwarf2_per_objfile->n_type_units == 0)
8228 return;
8229
8230 /* TUs typically share abbrev tables, and there can be way more TUs than
8231 abbrev tables. Sort by abbrev table to reduce the number of times we
8232 read each abbrev table in.
8233 Alternatives are to punt or to maintain a cache of abbrev tables.
8234 This is simpler and efficient enough for now.
8235
8236 Later we group TUs by their DW_AT_stmt_list value (as this defines the
8237 symtab to use). Typically TUs with the same abbrev offset have the same
8238 stmt_list value too so in practice this should work well.
8239
8240 The basic algorithm here is:
8241
8242 sort TUs by abbrev table
8243 for each TU with same abbrev table:
8244 read abbrev table if first user
8245 read TU top level DIE
8246 [IWBN if DWO skeletons had DW_AT_stmt_list]
8247 call FUNC */
8248
b4f54984 8249 if (dwarf_read_debug)
73051182
DE
8250 fprintf_unfiltered (gdb_stdlog, "Building type unit groups ...\n");
8251
8252 /* Sort in a separate table to maintain the order of all_type_units
8253 for .gdb_index: TU indices directly index all_type_units. */
484cf504
TT
8254 std::vector<struct tu_abbrev_offset> sorted_by_abbrev
8255 (dwarf2_per_objfile->n_type_units);
73051182
DE
8256 for (i = 0; i < dwarf2_per_objfile->n_type_units; ++i)
8257 {
8258 struct signatured_type *sig_type = dwarf2_per_objfile->all_type_units[i];
8259
8260 sorted_by_abbrev[i].sig_type = sig_type;
8261 sorted_by_abbrev[i].abbrev_offset =
ed2dc618
SM
8262 read_abbrev_offset (dwarf2_per_objfile,
8263 sig_type->per_cu.section,
9c541725 8264 sig_type->per_cu.sect_off);
73051182 8265 }
484cf504
TT
8266 std::sort (sorted_by_abbrev.begin (), sorted_by_abbrev.end (),
8267 sort_tu_by_abbrev_offset);
73051182 8268
9c541725 8269 abbrev_offset = (sect_offset) ~(unsigned) 0;
73051182
DE
8270
8271 for (i = 0; i < dwarf2_per_objfile->n_type_units; ++i)
8272 {
8273 const struct tu_abbrev_offset *tu = &sorted_by_abbrev[i];
8274
8275 /* Switch to the next abbrev table if necessary. */
8276 if (abbrev_table == NULL
9c541725 8277 || tu->abbrev_offset != abbrev_offset)
73051182 8278 {
73051182
DE
8279 abbrev_offset = tu->abbrev_offset;
8280 abbrev_table =
ed2dc618
SM
8281 abbrev_table_read_table (dwarf2_per_objfile,
8282 &dwarf2_per_objfile->abbrev,
73051182
DE
8283 abbrev_offset);
8284 ++tu_stats->nr_uniq_abbrev_tables;
8285 }
8286
685af9cd
TT
8287 init_cutu_and_read_dies (&tu->sig_type->per_cu, abbrev_table.get (),
8288 0, 0, build_type_psymtabs_reader, NULL);
73051182 8289 }
6aa5f3a6 8290}
73051182 8291
6aa5f3a6
DE
8292/* Print collected type unit statistics. */
8293
8294static void
ed2dc618 8295print_tu_stats (struct dwarf2_per_objfile *dwarf2_per_objfile)
6aa5f3a6
DE
8296{
8297 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
8298
8299 fprintf_unfiltered (gdb_stdlog, "Type unit statistics:\n");
8300 fprintf_unfiltered (gdb_stdlog, " %d TUs\n",
8301 dwarf2_per_objfile->n_type_units);
8302 fprintf_unfiltered (gdb_stdlog, " %d uniq abbrev tables\n",
8303 tu_stats->nr_uniq_abbrev_tables);
8304 fprintf_unfiltered (gdb_stdlog, " %d symtabs from stmt_list entries\n",
8305 tu_stats->nr_symtabs);
8306 fprintf_unfiltered (gdb_stdlog, " %d symtab sharers\n",
8307 tu_stats->nr_symtab_sharers);
8308 fprintf_unfiltered (gdb_stdlog, " %d type units without a stmt_list\n",
8309 tu_stats->nr_stmt_less_type_units);
8310 fprintf_unfiltered (gdb_stdlog, " %d all_type_units reallocs\n",
8311 tu_stats->nr_all_type_units_reallocs);
73051182
DE
8312}
8313
f4dc4d17
DE
8314/* Traversal function for build_type_psymtabs. */
8315
8316static int
8317build_type_psymtab_dependencies (void **slot, void *info)
8318{
ed2dc618
SM
8319 struct dwarf2_per_objfile *dwarf2_per_objfile
8320 = (struct dwarf2_per_objfile *) info;
f4dc4d17
DE
8321 struct objfile *objfile = dwarf2_per_objfile->objfile;
8322 struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
094b34ac 8323 struct dwarf2_per_cu_data *per_cu = &tu_group->per_cu;
f4dc4d17 8324 struct partial_symtab *pst = per_cu->v.psymtab;
0186c6a7
DE
8325 int len = VEC_length (sig_type_ptr, tu_group->tus);
8326 struct signatured_type *iter;
f4dc4d17
DE
8327 int i;
8328
8329 gdb_assert (len > 0);
0186c6a7 8330 gdb_assert (IS_TYPE_UNIT_GROUP (per_cu));
f4dc4d17
DE
8331
8332 pst->number_of_dependencies = len;
8d749320
SM
8333 pst->dependencies =
8334 XOBNEWVEC (&objfile->objfile_obstack, struct partial_symtab *, len);
f4dc4d17 8335 for (i = 0;
0186c6a7 8336 VEC_iterate (sig_type_ptr, tu_group->tus, i, iter);
f4dc4d17
DE
8337 ++i)
8338 {
0186c6a7
DE
8339 gdb_assert (iter->per_cu.is_debug_types);
8340 pst->dependencies[i] = iter->per_cu.v.psymtab;
796a7ff8 8341 iter->type_unit_group = tu_group;
f4dc4d17
DE
8342 }
8343
0186c6a7 8344 VEC_free (sig_type_ptr, tu_group->tus);
348e048f
DE
8345
8346 return 1;
8347}
8348
8349/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
8350 Build partial symbol tables for the .debug_types comp-units. */
8351
8352static void
ed2dc618 8353build_type_psymtabs (struct dwarf2_per_objfile *dwarf2_per_objfile)
348e048f 8354{
ed2dc618 8355 if (! create_all_type_units (dwarf2_per_objfile))
348e048f
DE
8356 return;
8357
ed2dc618 8358 build_type_psymtabs_1 (dwarf2_per_objfile);
6aa5f3a6 8359}
f4dc4d17 8360
6aa5f3a6
DE
8361/* Traversal function for process_skeletonless_type_unit.
8362 Read a TU in a DWO file and build partial symbols for it. */
8363
8364static int
8365process_skeletonless_type_unit (void **slot, void *info)
8366{
8367 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
ed2dc618
SM
8368 struct dwarf2_per_objfile *dwarf2_per_objfile
8369 = (struct dwarf2_per_objfile *) info;
6aa5f3a6
DE
8370 struct signatured_type find_entry, *entry;
8371
8372 /* If this TU doesn't exist in the global table, add it and read it in. */
8373
8374 if (dwarf2_per_objfile->signatured_types == NULL)
8375 {
8376 dwarf2_per_objfile->signatured_types
ed2dc618 8377 = allocate_signatured_type_table (dwarf2_per_objfile->objfile);
6aa5f3a6
DE
8378 }
8379
8380 find_entry.signature = dwo_unit->signature;
8381 slot = htab_find_slot (dwarf2_per_objfile->signatured_types, &find_entry,
8382 INSERT);
8383 /* If we've already seen this type there's nothing to do. What's happening
8384 is we're doing our own version of comdat-folding here. */
8385 if (*slot != NULL)
8386 return 1;
8387
8388 /* This does the job that create_all_type_units would have done for
8389 this TU. */
ed2dc618
SM
8390 entry = add_type_unit (dwarf2_per_objfile, dwo_unit->signature, slot);
8391 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, entry, dwo_unit);
6aa5f3a6
DE
8392 *slot = entry;
8393
8394 /* This does the job that build_type_psymtabs_1 would have done. */
8395 init_cutu_and_read_dies (&entry->per_cu, NULL, 0, 0,
8396 build_type_psymtabs_reader, NULL);
8397
8398 return 1;
8399}
8400
8401/* Traversal function for process_skeletonless_type_units. */
8402
8403static int
8404process_dwo_file_for_skeletonless_type_units (void **slot, void *info)
8405{
8406 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
8407
8408 if (dwo_file->tus != NULL)
8409 {
8410 htab_traverse_noresize (dwo_file->tus,
8411 process_skeletonless_type_unit, info);
8412 }
8413
8414 return 1;
8415}
8416
8417/* Scan all TUs of DWO files, verifying we've processed them.
8418 This is needed in case a TU was emitted without its skeleton.
8419 Note: This can't be done until we know what all the DWO files are. */
8420
8421static void
ed2dc618 8422process_skeletonless_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
6aa5f3a6
DE
8423{
8424 /* Skeletonless TUs in DWP files without .gdb_index is not supported yet. */
ed2dc618 8425 if (get_dwp_file (dwarf2_per_objfile) == NULL
6aa5f3a6
DE
8426 && dwarf2_per_objfile->dwo_files != NULL)
8427 {
8428 htab_traverse_noresize (dwarf2_per_objfile->dwo_files,
8429 process_dwo_file_for_skeletonless_type_units,
ed2dc618 8430 dwarf2_per_objfile);
6aa5f3a6 8431 }
348e048f
DE
8432}
8433
ed2dc618 8434/* Compute the 'user' field for each psymtab in DWARF2_PER_OBJFILE. */
95554aad
TT
8435
8436static void
ed2dc618 8437set_partial_user (struct dwarf2_per_objfile *dwarf2_per_objfile)
95554aad
TT
8438{
8439 int i;
8440
8441 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
8442 {
ed2dc618 8443 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (dwarf2_per_objfile, i);
95554aad
TT
8444 struct partial_symtab *pst = per_cu->v.psymtab;
8445 int j;
8446
36586728
TT
8447 if (pst == NULL)
8448 continue;
8449
95554aad
TT
8450 for (j = 0; j < pst->number_of_dependencies; ++j)
8451 {
8452 /* Set the 'user' field only if it is not already set. */
8453 if (pst->dependencies[j]->user == NULL)
8454 pst->dependencies[j]->user = pst;
8455 }
8456 }
8457}
8458
93311388
DE
8459/* Build the partial symbol table by doing a quick pass through the
8460 .debug_info and .debug_abbrev sections. */
72bf9492 8461
93311388 8462static void
ed2dc618 8463dwarf2_build_psymtabs_hard (struct dwarf2_per_objfile *dwarf2_per_objfile)
93311388 8464{
21b2bd31 8465 int i;
ed2dc618 8466 struct objfile *objfile = dwarf2_per_objfile->objfile;
93311388 8467
b4f54984 8468 if (dwarf_read_debug)
45cfd468
DE
8469 {
8470 fprintf_unfiltered (gdb_stdlog, "Building psymtabs of objfile %s ...\n",
4262abfb 8471 objfile_name (objfile));
45cfd468
DE
8472 }
8473
98bfdba5
PA
8474 dwarf2_per_objfile->reading_partial_symbols = 1;
8475
be391dca 8476 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
91c24f0a 8477
93311388
DE
8478 /* Any cached compilation units will be linked by the per-objfile
8479 read_in_chain. Make sure to free them when we're done. */
11ed8cad 8480 free_cached_comp_units freer (dwarf2_per_objfile);
72bf9492 8481
ed2dc618 8482 build_type_psymtabs (dwarf2_per_objfile);
348e048f 8483
ed2dc618 8484 create_all_comp_units (dwarf2_per_objfile);
c906108c 8485
60606b2c
TT
8486 /* Create a temporary address map on a temporary obstack. We later
8487 copy this to the final obstack. */
8268c778 8488 auto_obstack temp_obstack;
791afaa2
TT
8489
8490 scoped_restore save_psymtabs_addrmap
8491 = make_scoped_restore (&objfile->psymtabs_addrmap,
8492 addrmap_create_mutable (&temp_obstack));
72bf9492 8493
21b2bd31 8494 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
93311388 8495 {
ed2dc618 8496 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (dwarf2_per_objfile, i);
aaa75496 8497
b93601f3 8498 process_psymtab_comp_unit (per_cu, 0, language_minimal);
c906108c 8499 }
ff013f42 8500
6aa5f3a6 8501 /* This has to wait until we read the CUs, we need the list of DWOs. */
ed2dc618 8502 process_skeletonless_type_units (dwarf2_per_objfile);
6aa5f3a6
DE
8503
8504 /* Now that all TUs have been processed we can fill in the dependencies. */
8505 if (dwarf2_per_objfile->type_unit_groups != NULL)
8506 {
8507 htab_traverse_noresize (dwarf2_per_objfile->type_unit_groups,
ed2dc618 8508 build_type_psymtab_dependencies, dwarf2_per_objfile);
6aa5f3a6
DE
8509 }
8510
b4f54984 8511 if (dwarf_read_debug)
ed2dc618 8512 print_tu_stats (dwarf2_per_objfile);
6aa5f3a6 8513
ed2dc618 8514 set_partial_user (dwarf2_per_objfile);
95554aad 8515
ff013f42
JK
8516 objfile->psymtabs_addrmap = addrmap_create_fixed (objfile->psymtabs_addrmap,
8517 &objfile->objfile_obstack);
791afaa2
TT
8518 /* At this point we want to keep the address map. */
8519 save_psymtabs_addrmap.release ();
ff013f42 8520
b4f54984 8521 if (dwarf_read_debug)
45cfd468 8522 fprintf_unfiltered (gdb_stdlog, "Done building psymtabs of %s\n",
4262abfb 8523 objfile_name (objfile));
ae038cb0
DJ
8524}
8525
3019eac3 8526/* die_reader_func for load_partial_comp_unit. */
ae038cb0
DJ
8527
8528static void
dee91e82 8529load_partial_comp_unit_reader (const struct die_reader_specs *reader,
d521ce57 8530 const gdb_byte *info_ptr,
dee91e82
DE
8531 struct die_info *comp_unit_die,
8532 int has_children,
8533 void *data)
ae038cb0 8534{
dee91e82 8535 struct dwarf2_cu *cu = reader->cu;
ae038cb0 8536
95554aad 8537 prepare_one_comp_unit (cu, comp_unit_die, language_minimal);
ae038cb0 8538
ae038cb0
DJ
8539 /* Check if comp unit has_children.
8540 If so, read the rest of the partial symbols from this comp unit.
0963b4bd 8541 If not, there's no more debug_info for this comp unit. */
d85a05f0 8542 if (has_children)
dee91e82
DE
8543 load_partial_dies (reader, info_ptr, 0);
8544}
98bfdba5 8545
dee91e82
DE
8546/* Load the partial DIEs for a secondary CU into memory.
8547 This is also used when rereading a primary CU with load_all_dies. */
c5b7e1cb 8548
dee91e82
DE
8549static void
8550load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu)
8551{
f4dc4d17
DE
8552 init_cutu_and_read_dies (this_cu, NULL, 1, 1,
8553 load_partial_comp_unit_reader, NULL);
ae038cb0
DJ
8554}
8555
ae038cb0 8556static void
ed2dc618 8557read_comp_units_from_section (struct dwarf2_per_objfile *dwarf2_per_objfile,
36586728 8558 struct dwarf2_section_info *section,
f1902523 8559 struct dwarf2_section_info *abbrev_section,
36586728
TT
8560 unsigned int is_dwz,
8561 int *n_allocated,
8562 int *n_comp_units,
8563 struct dwarf2_per_cu_data ***all_comp_units)
ae038cb0 8564{
d521ce57 8565 const gdb_byte *info_ptr;
ed2dc618 8566 struct objfile *objfile = dwarf2_per_objfile->objfile;
be391dca 8567
b4f54984 8568 if (dwarf_read_debug)
bf6af496 8569 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s\n",
a32a8923
DE
8570 get_section_name (section),
8571 get_section_file_name (section));
bf6af496 8572
36586728 8573 dwarf2_read_section (objfile, section);
ae038cb0 8574
36586728 8575 info_ptr = section->buffer;
6e70227d 8576
36586728 8577 while (info_ptr < section->buffer + section->size)
ae038cb0 8578 {
ae038cb0 8579 struct dwarf2_per_cu_data *this_cu;
ae038cb0 8580
9c541725 8581 sect_offset sect_off = (sect_offset) (info_ptr - section->buffer);
ae038cb0 8582
f1902523 8583 comp_unit_head cu_header;
ed2dc618
SM
8584 read_and_check_comp_unit_head (dwarf2_per_objfile, &cu_header, section,
8585 abbrev_section, info_ptr,
8586 rcuh_kind::COMPILE);
ae038cb0
DJ
8587
8588 /* Save the compilation unit for later lookup. */
f1902523
JK
8589 if (cu_header.unit_type != DW_UT_type)
8590 {
8591 this_cu = XOBNEW (&objfile->objfile_obstack,
8592 struct dwarf2_per_cu_data);
8593 memset (this_cu, 0, sizeof (*this_cu));
8594 }
8595 else
8596 {
8597 auto sig_type = XOBNEW (&objfile->objfile_obstack,
8598 struct signatured_type);
8599 memset (sig_type, 0, sizeof (*sig_type));
8600 sig_type->signature = cu_header.signature;
8601 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
8602 this_cu = &sig_type->per_cu;
8603 }
8604 this_cu->is_debug_types = (cu_header.unit_type == DW_UT_type);
9c541725 8605 this_cu->sect_off = sect_off;
f1902523 8606 this_cu->length = cu_header.length + cu_header.initial_length_size;
36586728 8607 this_cu->is_dwz = is_dwz;
e3b94546 8608 this_cu->dwarf2_per_objfile = dwarf2_per_objfile;
8a0459fd 8609 this_cu->section = section;
ae038cb0 8610
36586728 8611 if (*n_comp_units == *n_allocated)
ae038cb0 8612 {
36586728 8613 *n_allocated *= 2;
224c3ddb
SM
8614 *all_comp_units = XRESIZEVEC (struct dwarf2_per_cu_data *,
8615 *all_comp_units, *n_allocated);
ae038cb0 8616 }
36586728
TT
8617 (*all_comp_units)[*n_comp_units] = this_cu;
8618 ++*n_comp_units;
ae038cb0
DJ
8619
8620 info_ptr = info_ptr + this_cu->length;
8621 }
36586728
TT
8622}
8623
8624/* Create a list of all compilation units in OBJFILE.
8625 This is only done for -readnow and building partial symtabs. */
8626
8627static void
ed2dc618 8628create_all_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
36586728
TT
8629{
8630 int n_allocated;
8631 int n_comp_units;
8632 struct dwarf2_per_cu_data **all_comp_units;
4db1a1dc 8633 struct dwz_file *dwz;
ed2dc618 8634 struct objfile *objfile = dwarf2_per_objfile->objfile;
36586728
TT
8635
8636 n_comp_units = 0;
8637 n_allocated = 10;
8d749320 8638 all_comp_units = XNEWVEC (struct dwarf2_per_cu_data *, n_allocated);
36586728 8639
ed2dc618 8640 read_comp_units_from_section (dwarf2_per_objfile, &dwarf2_per_objfile->info,
f1902523 8641 &dwarf2_per_objfile->abbrev, 0,
36586728
TT
8642 &n_allocated, &n_comp_units, &all_comp_units);
8643
ed2dc618 8644 dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
4db1a1dc 8645 if (dwz != NULL)
ed2dc618
SM
8646 read_comp_units_from_section (dwarf2_per_objfile, &dwz->info, &dwz->abbrev,
8647 1, &n_allocated, &n_comp_units,
4db1a1dc 8648 &all_comp_units);
ae038cb0 8649
8d749320
SM
8650 dwarf2_per_objfile->all_comp_units = XOBNEWVEC (&objfile->objfile_obstack,
8651 struct dwarf2_per_cu_data *,
8652 n_comp_units);
ae038cb0
DJ
8653 memcpy (dwarf2_per_objfile->all_comp_units, all_comp_units,
8654 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
8655 xfree (all_comp_units);
8656 dwarf2_per_objfile->n_comp_units = n_comp_units;
c906108c
SS
8657}
8658
5734ee8b 8659/* Process all loaded DIEs for compilation unit CU, starting at
cdc07690 8660 FIRST_DIE. The caller should pass SET_ADDRMAP == 1 if the compilation
5734ee8b 8661 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
cdc07690
YQ
8662 DW_AT_ranges). See the comments of add_partial_subprogram on how
8663 SET_ADDRMAP is used and how *LOWPC and *HIGHPC are updated. */
c906108c 8664
72bf9492
DJ
8665static void
8666scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
cdc07690
YQ
8667 CORE_ADDR *highpc, int set_addrmap,
8668 struct dwarf2_cu *cu)
c906108c 8669{
72bf9492 8670 struct partial_die_info *pdi;
c906108c 8671
91c24f0a
DC
8672 /* Now, march along the PDI's, descending into ones which have
8673 interesting children but skipping the children of the other ones,
8674 until we reach the end of the compilation unit. */
c906108c 8675
72bf9492 8676 pdi = first_die;
91c24f0a 8677
72bf9492
DJ
8678 while (pdi != NULL)
8679 {
52356b79 8680 pdi->fixup (cu);
c906108c 8681
f55ee35c 8682 /* Anonymous namespaces or modules have no name but have interesting
91c24f0a
DC
8683 children, so we need to look at them. Ditto for anonymous
8684 enums. */
933c6fe4 8685
72bf9492 8686 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
95554aad 8687 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
b1dc1806
XR
8688 || pdi->tag == DW_TAG_imported_unit
8689 || pdi->tag == DW_TAG_inlined_subroutine)
c906108c 8690 {
72bf9492 8691 switch (pdi->tag)
c906108c
SS
8692 {
8693 case DW_TAG_subprogram:
b1dc1806 8694 case DW_TAG_inlined_subroutine:
cdc07690 8695 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
c906108c 8696 break;
72929c62 8697 case DW_TAG_constant:
c906108c
SS
8698 case DW_TAG_variable:
8699 case DW_TAG_typedef:
91c24f0a 8700 case DW_TAG_union_type:
72bf9492 8701 if (!pdi->is_declaration)
63d06c5c 8702 {
72bf9492 8703 add_partial_symbol (pdi, cu);
63d06c5c
DC
8704 }
8705 break;
c906108c 8706 case DW_TAG_class_type:
680b30c7 8707 case DW_TAG_interface_type:
c906108c 8708 case DW_TAG_structure_type:
72bf9492 8709 if (!pdi->is_declaration)
c906108c 8710 {
72bf9492 8711 add_partial_symbol (pdi, cu);
c906108c 8712 }
b7fee5a3
KS
8713 if ((cu->language == language_rust
8714 || cu->language == language_cplus) && pdi->has_children)
e98c9e7c
TT
8715 scan_partial_symbols (pdi->die_child, lowpc, highpc,
8716 set_addrmap, cu);
c906108c 8717 break;
91c24f0a 8718 case DW_TAG_enumeration_type:
72bf9492
DJ
8719 if (!pdi->is_declaration)
8720 add_partial_enumeration (pdi, cu);
c906108c
SS
8721 break;
8722 case DW_TAG_base_type:
a02abb62 8723 case DW_TAG_subrange_type:
c906108c 8724 /* File scope base type definitions are added to the partial
c5aa993b 8725 symbol table. */
72bf9492 8726 add_partial_symbol (pdi, cu);
c906108c 8727 break;
d9fa45fe 8728 case DW_TAG_namespace:
cdc07690 8729 add_partial_namespace (pdi, lowpc, highpc, set_addrmap, cu);
91c24f0a 8730 break;
5d7cb8df 8731 case DW_TAG_module:
cdc07690 8732 add_partial_module (pdi, lowpc, highpc, set_addrmap, cu);
5d7cb8df 8733 break;
95554aad
TT
8734 case DW_TAG_imported_unit:
8735 {
8736 struct dwarf2_per_cu_data *per_cu;
8737
f4dc4d17
DE
8738 /* For now we don't handle imported units in type units. */
8739 if (cu->per_cu->is_debug_types)
8740 {
8741 error (_("Dwarf Error: DW_TAG_imported_unit is not"
8742 " supported in type units [in module %s]"),
518817b3 8743 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
f4dc4d17
DE
8744 }
8745
e3b94546
SM
8746 per_cu = dwarf2_find_containing_comp_unit
8747 (pdi->d.sect_off, pdi->is_dwz,
518817b3 8748 cu->per_cu->dwarf2_per_objfile);
95554aad
TT
8749
8750 /* Go read the partial unit, if needed. */
8751 if (per_cu->v.psymtab == NULL)
b93601f3 8752 process_psymtab_comp_unit (per_cu, 1, cu->language);
95554aad 8753
f4dc4d17 8754 VEC_safe_push (dwarf2_per_cu_ptr,
796a7ff8 8755 cu->per_cu->imported_symtabs, per_cu);
95554aad
TT
8756 }
8757 break;
74921315
KS
8758 case DW_TAG_imported_declaration:
8759 add_partial_symbol (pdi, cu);
8760 break;
c906108c
SS
8761 default:
8762 break;
8763 }
8764 }
8765
72bf9492
DJ
8766 /* If the die has a sibling, skip to the sibling. */
8767
8768 pdi = pdi->die_sibling;
8769 }
8770}
8771
8772/* Functions used to compute the fully scoped name of a partial DIE.
91c24f0a 8773
72bf9492 8774 Normally, this is simple. For C++, the parent DIE's fully scoped
9c37b5ae 8775 name is concatenated with "::" and the partial DIE's name.
72bf9492
DJ
8776 Enumerators are an exception; they use the scope of their parent
8777 enumeration type, i.e. the name of the enumeration type is not
8778 prepended to the enumerator.
91c24f0a 8779
72bf9492
DJ
8780 There are two complexities. One is DW_AT_specification; in this
8781 case "parent" means the parent of the target of the specification,
8782 instead of the direct parent of the DIE. The other is compilers
8783 which do not emit DW_TAG_namespace; in this case we try to guess
8784 the fully qualified name of structure types from their members'
8785 linkage names. This must be done using the DIE's children rather
8786 than the children of any DW_AT_specification target. We only need
8787 to do this for structures at the top level, i.e. if the target of
8788 any DW_AT_specification (if any; otherwise the DIE itself) does not
8789 have a parent. */
8790
8791/* Compute the scope prefix associated with PDI's parent, in
8792 compilation unit CU. The result will be allocated on CU's
8793 comp_unit_obstack, or a copy of the already allocated PDI->NAME
8794 field. NULL is returned if no prefix is necessary. */
15d034d0 8795static const char *
72bf9492
DJ
8796partial_die_parent_scope (struct partial_die_info *pdi,
8797 struct dwarf2_cu *cu)
8798{
15d034d0 8799 const char *grandparent_scope;
72bf9492 8800 struct partial_die_info *parent, *real_pdi;
91c24f0a 8801
72bf9492
DJ
8802 /* We need to look at our parent DIE; if we have a DW_AT_specification,
8803 then this means the parent of the specification DIE. */
8804
8805 real_pdi = pdi;
72bf9492 8806 while (real_pdi->has_specification)
36586728
TT
8807 real_pdi = find_partial_die (real_pdi->spec_offset,
8808 real_pdi->spec_is_dwz, cu);
72bf9492
DJ
8809
8810 parent = real_pdi->die_parent;
8811 if (parent == NULL)
8812 return NULL;
8813
8814 if (parent->scope_set)
8815 return parent->scope;
8816
52356b79 8817 parent->fixup (cu);
72bf9492 8818
10b3939b 8819 grandparent_scope = partial_die_parent_scope (parent, cu);
72bf9492 8820
acebe513
UW
8821 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
8822 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
8823 Work around this problem here. */
8824 if (cu->language == language_cplus
6e70227d 8825 && parent->tag == DW_TAG_namespace
acebe513
UW
8826 && strcmp (parent->name, "::") == 0
8827 && grandparent_scope == NULL)
8828 {
8829 parent->scope = NULL;
8830 parent->scope_set = 1;
8831 return NULL;
8832 }
8833
9c6c53f7
SA
8834 if (pdi->tag == DW_TAG_enumerator)
8835 /* Enumerators should not get the name of the enumeration as a prefix. */
8836 parent->scope = grandparent_scope;
8837 else if (parent->tag == DW_TAG_namespace
f55ee35c 8838 || parent->tag == DW_TAG_module
72bf9492
DJ
8839 || parent->tag == DW_TAG_structure_type
8840 || parent->tag == DW_TAG_class_type
680b30c7 8841 || parent->tag == DW_TAG_interface_type
ceeb3d5a
TT
8842 || parent->tag == DW_TAG_union_type
8843 || parent->tag == DW_TAG_enumeration_type)
72bf9492
DJ
8844 {
8845 if (grandparent_scope == NULL)
8846 parent->scope = parent->name;
8847 else
3e43a32a
MS
8848 parent->scope = typename_concat (&cu->comp_unit_obstack,
8849 grandparent_scope,
f55ee35c 8850 parent->name, 0, cu);
72bf9492 8851 }
72bf9492
DJ
8852 else
8853 {
8854 /* FIXME drow/2004-04-01: What should we be doing with
8855 function-local names? For partial symbols, we should probably be
8856 ignoring them. */
8857 complaint (&symfile_complaints,
9d8780f0
SM
8858 _("unhandled containing DIE tag %d for DIE at %s"),
8859 parent->tag, sect_offset_str (pdi->sect_off));
72bf9492 8860 parent->scope = grandparent_scope;
c906108c
SS
8861 }
8862
72bf9492
DJ
8863 parent->scope_set = 1;
8864 return parent->scope;
8865}
8866
8867/* Return the fully scoped name associated with PDI, from compilation unit
8868 CU. The result will be allocated with malloc. */
4568ecf9 8869
72bf9492
DJ
8870static char *
8871partial_die_full_name (struct partial_die_info *pdi,
8872 struct dwarf2_cu *cu)
8873{
15d034d0 8874 const char *parent_scope;
72bf9492 8875
98bfdba5
PA
8876 /* If this is a template instantiation, we can not work out the
8877 template arguments from partial DIEs. So, unfortunately, we have
8878 to go through the full DIEs. At least any work we do building
8879 types here will be reused if full symbols are loaded later. */
8880 if (pdi->has_template_arguments)
8881 {
52356b79 8882 pdi->fixup (cu);
98bfdba5
PA
8883
8884 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
8885 {
8886 struct die_info *die;
8887 struct attribute attr;
8888 struct dwarf2_cu *ref_cu = cu;
8889
b64f50a1 8890 /* DW_FORM_ref_addr is using section offset. */
b4069958 8891 attr.name = (enum dwarf_attribute) 0;
98bfdba5 8892 attr.form = DW_FORM_ref_addr;
9c541725 8893 attr.u.unsnd = to_underlying (pdi->sect_off);
98bfdba5
PA
8894 die = follow_die_ref (NULL, &attr, &ref_cu);
8895
8896 return xstrdup (dwarf2_full_name (NULL, die, ref_cu));
8897 }
8898 }
8899
72bf9492
DJ
8900 parent_scope = partial_die_parent_scope (pdi, cu);
8901 if (parent_scope == NULL)
8902 return NULL;
8903 else
f55ee35c 8904 return typename_concat (NULL, parent_scope, pdi->name, 0, cu);
c906108c
SS
8905}
8906
8907static void
72bf9492 8908add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
c906108c 8909{
518817b3
SM
8910 struct dwarf2_per_objfile *dwarf2_per_objfile
8911 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 8912 struct objfile *objfile = dwarf2_per_objfile->objfile;
3e29f34a 8913 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c 8914 CORE_ADDR addr = 0;
15d034d0 8915 const char *actual_name = NULL;
e142c38c 8916 CORE_ADDR baseaddr;
15d034d0 8917 char *built_actual_name;
e142c38c
DJ
8918
8919 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 8920
15d034d0
TT
8921 built_actual_name = partial_die_full_name (pdi, cu);
8922 if (built_actual_name != NULL)
8923 actual_name = built_actual_name;
63d06c5c 8924
72bf9492
DJ
8925 if (actual_name == NULL)
8926 actual_name = pdi->name;
8927
c906108c
SS
8928 switch (pdi->tag)
8929 {
b1dc1806 8930 case DW_TAG_inlined_subroutine:
c906108c 8931 case DW_TAG_subprogram:
3e29f34a 8932 addr = gdbarch_adjust_dwarf2_addr (gdbarch, pdi->lowpc + baseaddr);
2cfa0c8d 8933 if (pdi->is_external || cu->language == language_ada)
c906108c 8934 {
2cfa0c8d
JB
8935 /* brobecker/2007-12-26: Normally, only "external" DIEs are part
8936 of the global scope. But in Ada, we want to be able to access
8937 nested procedures globally. So all Ada subprograms are stored
8938 in the global scope. */
f47fb265 8939 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 8940 built_actual_name != NULL,
f47fb265
MS
8941 VAR_DOMAIN, LOC_BLOCK,
8942 &objfile->global_psymbols,
1762568f 8943 addr, cu->language, objfile);
c906108c
SS
8944 }
8945 else
8946 {
f47fb265 8947 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 8948 built_actual_name != NULL,
f47fb265
MS
8949 VAR_DOMAIN, LOC_BLOCK,
8950 &objfile->static_psymbols,
1762568f 8951 addr, cu->language, objfile);
c906108c 8952 }
0c1b455e
TT
8953
8954 if (pdi->main_subprogram && actual_name != NULL)
8955 set_objfile_main_name (objfile, actual_name, cu->language);
c906108c 8956 break;
72929c62
JB
8957 case DW_TAG_constant:
8958 {
af5bf4ad 8959 std::vector<partial_symbol *> *list;
72929c62
JB
8960
8961 if (pdi->is_external)
8962 list = &objfile->global_psymbols;
8963 else
8964 list = &objfile->static_psymbols;
f47fb265 8965 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 8966 built_actual_name != NULL, VAR_DOMAIN, LOC_STATIC,
1762568f 8967 list, 0, cu->language, objfile);
72929c62
JB
8968 }
8969 break;
c906108c 8970 case DW_TAG_variable:
95554aad
TT
8971 if (pdi->d.locdesc)
8972 addr = decode_locdesc (pdi->d.locdesc, cu);
caac4577 8973
95554aad 8974 if (pdi->d.locdesc
caac4577
JG
8975 && addr == 0
8976 && !dwarf2_per_objfile->has_section_at_zero)
8977 {
8978 /* A global or static variable may also have been stripped
8979 out by the linker if unused, in which case its address
8980 will be nullified; do not add such variables into partial
8981 symbol table then. */
8982 }
8983 else if (pdi->is_external)
c906108c
SS
8984 {
8985 /* Global Variable.
8986 Don't enter into the minimal symbol tables as there is
8987 a minimal symbol table entry from the ELF symbols already.
8988 Enter into partial symbol table if it has a location
8989 descriptor or a type.
8990 If the location descriptor is missing, new_symbol will create
8991 a LOC_UNRESOLVED symbol, the address of the variable will then
8992 be determined from the minimal symbol table whenever the variable
8993 is referenced.
8994 The address for the partial symbol table entry is not
8995 used by GDB, but it comes in handy for debugging partial symbol
8996 table building. */
8997
95554aad 8998 if (pdi->d.locdesc || pdi->has_type)
f47fb265 8999 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 9000 built_actual_name != NULL,
f47fb265
MS
9001 VAR_DOMAIN, LOC_STATIC,
9002 &objfile->global_psymbols,
1762568f 9003 addr + baseaddr,
f47fb265 9004 cu->language, objfile);
c906108c
SS
9005 }
9006 else
9007 {
ff908ebf
AW
9008 int has_loc = pdi->d.locdesc != NULL;
9009
9010 /* Static Variable. Skip symbols whose value we cannot know (those
9011 without location descriptors or constant values). */
9012 if (!has_loc && !pdi->has_const_value)
decbce07 9013 {
15d034d0 9014 xfree (built_actual_name);
decbce07
MS
9015 return;
9016 }
ff908ebf 9017
f47fb265 9018 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 9019 built_actual_name != NULL,
f47fb265
MS
9020 VAR_DOMAIN, LOC_STATIC,
9021 &objfile->static_psymbols,
ff908ebf 9022 has_loc ? addr + baseaddr : (CORE_ADDR) 0,
f47fb265 9023 cu->language, objfile);
c906108c
SS
9024 }
9025 break;
9026 case DW_TAG_typedef:
9027 case DW_TAG_base_type:
a02abb62 9028 case DW_TAG_subrange_type:
38d518c9 9029 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 9030 built_actual_name != NULL,
176620f1 9031 VAR_DOMAIN, LOC_TYPEDEF,
c906108c 9032 &objfile->static_psymbols,
1762568f 9033 0, cu->language, objfile);
c906108c 9034 break;
74921315 9035 case DW_TAG_imported_declaration:
72bf9492
DJ
9036 case DW_TAG_namespace:
9037 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 9038 built_actual_name != NULL,
72bf9492
DJ
9039 VAR_DOMAIN, LOC_TYPEDEF,
9040 &objfile->global_psymbols,
1762568f 9041 0, cu->language, objfile);
72bf9492 9042 break;
530e8392
KB
9043 case DW_TAG_module:
9044 add_psymbol_to_list (actual_name, strlen (actual_name),
9045 built_actual_name != NULL,
9046 MODULE_DOMAIN, LOC_TYPEDEF,
9047 &objfile->global_psymbols,
1762568f 9048 0, cu->language, objfile);
530e8392 9049 break;
c906108c 9050 case DW_TAG_class_type:
680b30c7 9051 case DW_TAG_interface_type:
c906108c
SS
9052 case DW_TAG_structure_type:
9053 case DW_TAG_union_type:
9054 case DW_TAG_enumeration_type:
fa4028e9
JB
9055 /* Skip external references. The DWARF standard says in the section
9056 about "Structure, Union, and Class Type Entries": "An incomplete
9057 structure, union or class type is represented by a structure,
9058 union or class entry that does not have a byte size attribute
9059 and that has a DW_AT_declaration attribute." */
9060 if (!pdi->has_byte_size && pdi->is_declaration)
decbce07 9061 {
15d034d0 9062 xfree (built_actual_name);
decbce07
MS
9063 return;
9064 }
fa4028e9 9065
63d06c5c
DC
9066 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
9067 static vs. global. */
38d518c9 9068 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 9069 built_actual_name != NULL,
176620f1 9070 STRUCT_DOMAIN, LOC_TYPEDEF,
9c37b5ae 9071 cu->language == language_cplus
63d06c5c
DC
9072 ? &objfile->global_psymbols
9073 : &objfile->static_psymbols,
1762568f 9074 0, cu->language, objfile);
c906108c 9075
c906108c
SS
9076 break;
9077 case DW_TAG_enumerator:
38d518c9 9078 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 9079 built_actual_name != NULL,
176620f1 9080 VAR_DOMAIN, LOC_CONST,
9c37b5ae 9081 cu->language == language_cplus
f6fe98ef
DJ
9082 ? &objfile->global_psymbols
9083 : &objfile->static_psymbols,
1762568f 9084 0, cu->language, objfile);
c906108c
SS
9085 break;
9086 default:
9087 break;
9088 }
5c4e30ca 9089
15d034d0 9090 xfree (built_actual_name);
c906108c
SS
9091}
9092
5c4e30ca
DC
9093/* Read a partial die corresponding to a namespace; also, add a symbol
9094 corresponding to that namespace to the symbol table. NAMESPACE is
9095 the name of the enclosing namespace. */
91c24f0a 9096
72bf9492
DJ
9097static void
9098add_partial_namespace (struct partial_die_info *pdi,
91c24f0a 9099 CORE_ADDR *lowpc, CORE_ADDR *highpc,
cdc07690 9100 int set_addrmap, struct dwarf2_cu *cu)
91c24f0a 9101{
72bf9492 9102 /* Add a symbol for the namespace. */
e7c27a73 9103
72bf9492 9104 add_partial_symbol (pdi, cu);
5c4e30ca
DC
9105
9106 /* Now scan partial symbols in that namespace. */
9107
91c24f0a 9108 if (pdi->has_children)
cdc07690 9109 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
91c24f0a
DC
9110}
9111
5d7cb8df
JK
9112/* Read a partial die corresponding to a Fortran module. */
9113
9114static void
9115add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
cdc07690 9116 CORE_ADDR *highpc, int set_addrmap, struct dwarf2_cu *cu)
5d7cb8df 9117{
530e8392
KB
9118 /* Add a symbol for the namespace. */
9119
9120 add_partial_symbol (pdi, cu);
9121
f55ee35c 9122 /* Now scan partial symbols in that module. */
5d7cb8df
JK
9123
9124 if (pdi->has_children)
cdc07690 9125 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
5d7cb8df
JK
9126}
9127
b1dc1806
XR
9128/* Read a partial die corresponding to a subprogram or an inlined
9129 subprogram and create a partial symbol for that subprogram.
9130 When the CU language allows it, this routine also defines a partial
9131 symbol for each nested subprogram that this subprogram contains.
9132 If SET_ADDRMAP is true, record the covered ranges in the addrmap.
9133 Set *LOWPC and *HIGHPC to the lowest and highest PC values found in PDI.
6e70227d 9134
cdc07690
YQ
9135 PDI may also be a lexical block, in which case we simply search
9136 recursively for subprograms defined inside that lexical block.
bc30ff58
JB
9137 Again, this is only performed when the CU language allows this
9138 type of definitions. */
9139
9140static void
9141add_partial_subprogram (struct partial_die_info *pdi,
9142 CORE_ADDR *lowpc, CORE_ADDR *highpc,
cdc07690 9143 int set_addrmap, struct dwarf2_cu *cu)
bc30ff58 9144{
b1dc1806 9145 if (pdi->tag == DW_TAG_subprogram || pdi->tag == DW_TAG_inlined_subroutine)
bc30ff58
JB
9146 {
9147 if (pdi->has_pc_info)
9148 {
9149 if (pdi->lowpc < *lowpc)
9150 *lowpc = pdi->lowpc;
9151 if (pdi->highpc > *highpc)
9152 *highpc = pdi->highpc;
cdc07690 9153 if (set_addrmap)
5734ee8b 9154 {
518817b3 9155 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
3e29f34a
MR
9156 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9157 CORE_ADDR baseaddr;
9158 CORE_ADDR highpc;
9159 CORE_ADDR lowpc;
5734ee8b
DJ
9160
9161 baseaddr = ANOFFSET (objfile->section_offsets,
9162 SECT_OFF_TEXT (objfile));
3e29f34a
MR
9163 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch,
9164 pdi->lowpc + baseaddr);
9165 highpc = gdbarch_adjust_dwarf2_addr (gdbarch,
9166 pdi->highpc + baseaddr);
9167 addrmap_set_empty (objfile->psymtabs_addrmap, lowpc, highpc - 1,
9291a0cd 9168 cu->per_cu->v.psymtab);
5734ee8b 9169 }
481860b3
GB
9170 }
9171
9172 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
9173 {
bc30ff58 9174 if (!pdi->is_declaration)
e8d05480
JB
9175 /* Ignore subprogram DIEs that do not have a name, they are
9176 illegal. Do not emit a complaint at this point, we will
9177 do so when we convert this psymtab into a symtab. */
9178 if (pdi->name)
9179 add_partial_symbol (pdi, cu);
bc30ff58
JB
9180 }
9181 }
6e70227d 9182
bc30ff58
JB
9183 if (! pdi->has_children)
9184 return;
9185
9186 if (cu->language == language_ada)
9187 {
9188 pdi = pdi->die_child;
9189 while (pdi != NULL)
9190 {
52356b79 9191 pdi->fixup (cu);
bc30ff58 9192 if (pdi->tag == DW_TAG_subprogram
b1dc1806 9193 || pdi->tag == DW_TAG_inlined_subroutine
bc30ff58 9194 || pdi->tag == DW_TAG_lexical_block)
cdc07690 9195 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
bc30ff58
JB
9196 pdi = pdi->die_sibling;
9197 }
9198 }
9199}
9200
91c24f0a
DC
9201/* Read a partial die corresponding to an enumeration type. */
9202
72bf9492
DJ
9203static void
9204add_partial_enumeration (struct partial_die_info *enum_pdi,
9205 struct dwarf2_cu *cu)
91c24f0a 9206{
72bf9492 9207 struct partial_die_info *pdi;
91c24f0a
DC
9208
9209 if (enum_pdi->name != NULL)
72bf9492
DJ
9210 add_partial_symbol (enum_pdi, cu);
9211
9212 pdi = enum_pdi->die_child;
9213 while (pdi)
91c24f0a 9214 {
72bf9492 9215 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
e2e0b3e5 9216 complaint (&symfile_complaints, _("malformed enumerator DIE ignored"));
91c24f0a 9217 else
72bf9492
DJ
9218 add_partial_symbol (pdi, cu);
9219 pdi = pdi->die_sibling;
91c24f0a 9220 }
91c24f0a
DC
9221}
9222
6caca83c
CC
9223/* Return the initial uleb128 in the die at INFO_PTR. */
9224
9225static unsigned int
d521ce57 9226peek_abbrev_code (bfd *abfd, const gdb_byte *info_ptr)
6caca83c
CC
9227{
9228 unsigned int bytes_read;
9229
9230 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9231}
9232
685af9cd
TT
9233/* Read the initial uleb128 in the die at INFO_PTR in compilation unit
9234 READER::CU. Use READER::ABBREV_TABLE to lookup any abbreviation.
9235
4bb7a0a7
DJ
9236 Return the corresponding abbrev, or NULL if the number is zero (indicating
9237 an empty DIE). In either case *BYTES_READ will be set to the length of
9238 the initial number. */
9239
9240static struct abbrev_info *
685af9cd
TT
9241peek_die_abbrev (const die_reader_specs &reader,
9242 const gdb_byte *info_ptr, unsigned int *bytes_read)
4bb7a0a7 9243{
685af9cd 9244 dwarf2_cu *cu = reader.cu;
518817b3 9245 bfd *abfd = cu->per_cu->dwarf2_per_objfile->objfile->obfd;
685af9cd
TT
9246 unsigned int abbrev_number
9247 = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
4bb7a0a7
DJ
9248
9249 if (abbrev_number == 0)
9250 return NULL;
9251
685af9cd 9252 abbrev_info *abbrev = reader.abbrev_table->lookup_abbrev (abbrev_number);
4bb7a0a7
DJ
9253 if (!abbrev)
9254 {
422b9917 9255 error (_("Dwarf Error: Could not find abbrev number %d in %s"
9d8780f0 9256 " at offset %s [in module %s]"),
422b9917 9257 abbrev_number, cu->per_cu->is_debug_types ? "TU" : "CU",
9d8780f0 9258 sect_offset_str (cu->header.sect_off), bfd_get_filename (abfd));
4bb7a0a7
DJ
9259 }
9260
9261 return abbrev;
9262}
9263
93311388
DE
9264/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
9265 Returns a pointer to the end of a series of DIEs, terminated by an empty
4bb7a0a7
DJ
9266 DIE. Any children of the skipped DIEs will also be skipped. */
9267
d521ce57
TT
9268static const gdb_byte *
9269skip_children (const struct die_reader_specs *reader, const gdb_byte *info_ptr)
4bb7a0a7 9270{
4bb7a0a7
DJ
9271 while (1)
9272 {
685af9cd
TT
9273 unsigned int bytes_read;
9274 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
9275
4bb7a0a7
DJ
9276 if (abbrev == NULL)
9277 return info_ptr + bytes_read;
9278 else
dee91e82 9279 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
4bb7a0a7
DJ
9280 }
9281}
9282
93311388
DE
9283/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
9284 INFO_PTR should point just after the initial uleb128 of a DIE, and the
4bb7a0a7
DJ
9285 abbrev corresponding to that skipped uleb128 should be passed in
9286 ABBREV. Returns a pointer to this DIE's sibling, skipping any
9287 children. */
9288
d521ce57
TT
9289static const gdb_byte *
9290skip_one_die (const struct die_reader_specs *reader, const gdb_byte *info_ptr,
dee91e82 9291 struct abbrev_info *abbrev)
4bb7a0a7
DJ
9292{
9293 unsigned int bytes_read;
9294 struct attribute attr;
dee91e82
DE
9295 bfd *abfd = reader->abfd;
9296 struct dwarf2_cu *cu = reader->cu;
d521ce57 9297 const gdb_byte *buffer = reader->buffer;
f664829e 9298 const gdb_byte *buffer_end = reader->buffer_end;
4bb7a0a7
DJ
9299 unsigned int form, i;
9300
9301 for (i = 0; i < abbrev->num_attrs; i++)
9302 {
9303 /* The only abbrev we care about is DW_AT_sibling. */
9304 if (abbrev->attrs[i].name == DW_AT_sibling)
9305 {
dee91e82 9306 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
4bb7a0a7 9307 if (attr.form == DW_FORM_ref_addr)
3e43a32a
MS
9308 complaint (&symfile_complaints,
9309 _("ignoring absolute DW_AT_sibling"));
4bb7a0a7 9310 else
b9502d3f 9311 {
9c541725
PA
9312 sect_offset off = dwarf2_get_ref_die_offset (&attr);
9313 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
b9502d3f
WN
9314
9315 if (sibling_ptr < info_ptr)
9316 complaint (&symfile_complaints,
9317 _("DW_AT_sibling points backwards"));
22869d73
KS
9318 else if (sibling_ptr > reader->buffer_end)
9319 dwarf2_section_buffer_overflow_complaint (reader->die_section);
b9502d3f
WN
9320 else
9321 return sibling_ptr;
9322 }
4bb7a0a7
DJ
9323 }
9324
9325 /* If it isn't DW_AT_sibling, skip this attribute. */
9326 form = abbrev->attrs[i].form;
9327 skip_attribute:
9328 switch (form)
9329 {
4bb7a0a7 9330 case DW_FORM_ref_addr:
ae411497
TT
9331 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
9332 and later it is offset sized. */
9333 if (cu->header.version == 2)
9334 info_ptr += cu->header.addr_size;
9335 else
9336 info_ptr += cu->header.offset_size;
9337 break;
36586728
TT
9338 case DW_FORM_GNU_ref_alt:
9339 info_ptr += cu->header.offset_size;
9340 break;
ae411497 9341 case DW_FORM_addr:
4bb7a0a7
DJ
9342 info_ptr += cu->header.addr_size;
9343 break;
9344 case DW_FORM_data1:
9345 case DW_FORM_ref1:
9346 case DW_FORM_flag:
9347 info_ptr += 1;
9348 break;
2dc7f7b3 9349 case DW_FORM_flag_present:
43988095 9350 case DW_FORM_implicit_const:
2dc7f7b3 9351 break;
4bb7a0a7
DJ
9352 case DW_FORM_data2:
9353 case DW_FORM_ref2:
9354 info_ptr += 2;
9355 break;
9356 case DW_FORM_data4:
9357 case DW_FORM_ref4:
9358 info_ptr += 4;
9359 break;
9360 case DW_FORM_data8:
9361 case DW_FORM_ref8:
55f1336d 9362 case DW_FORM_ref_sig8:
4bb7a0a7
DJ
9363 info_ptr += 8;
9364 break;
0224619f
JK
9365 case DW_FORM_data16:
9366 info_ptr += 16;
9367 break;
4bb7a0a7 9368 case DW_FORM_string:
9b1c24c8 9369 read_direct_string (abfd, info_ptr, &bytes_read);
4bb7a0a7
DJ
9370 info_ptr += bytes_read;
9371 break;
2dc7f7b3 9372 case DW_FORM_sec_offset:
4bb7a0a7 9373 case DW_FORM_strp:
36586728 9374 case DW_FORM_GNU_strp_alt:
4bb7a0a7
DJ
9375 info_ptr += cu->header.offset_size;
9376 break;
2dc7f7b3 9377 case DW_FORM_exprloc:
4bb7a0a7
DJ
9378 case DW_FORM_block:
9379 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9380 info_ptr += bytes_read;
9381 break;
9382 case DW_FORM_block1:
9383 info_ptr += 1 + read_1_byte (abfd, info_ptr);
9384 break;
9385 case DW_FORM_block2:
9386 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
9387 break;
9388 case DW_FORM_block4:
9389 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
9390 break;
9391 case DW_FORM_sdata:
9392 case DW_FORM_udata:
9393 case DW_FORM_ref_udata:
3019eac3
DE
9394 case DW_FORM_GNU_addr_index:
9395 case DW_FORM_GNU_str_index:
d521ce57 9396 info_ptr = safe_skip_leb128 (info_ptr, buffer_end);
4bb7a0a7
DJ
9397 break;
9398 case DW_FORM_indirect:
9399 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9400 info_ptr += bytes_read;
9401 /* We need to continue parsing from here, so just go back to
9402 the top. */
9403 goto skip_attribute;
9404
9405 default:
3e43a32a
MS
9406 error (_("Dwarf Error: Cannot handle %s "
9407 "in DWARF reader [in module %s]"),
4bb7a0a7
DJ
9408 dwarf_form_name (form),
9409 bfd_get_filename (abfd));
9410 }
9411 }
9412
9413 if (abbrev->has_children)
dee91e82 9414 return skip_children (reader, info_ptr);
4bb7a0a7
DJ
9415 else
9416 return info_ptr;
9417}
9418
93311388 9419/* Locate ORIG_PDI's sibling.
dee91e82 9420 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
91c24f0a 9421
d521ce57 9422static const gdb_byte *
dee91e82
DE
9423locate_pdi_sibling (const struct die_reader_specs *reader,
9424 struct partial_die_info *orig_pdi,
d521ce57 9425 const gdb_byte *info_ptr)
91c24f0a
DC
9426{
9427 /* Do we know the sibling already? */
72bf9492 9428
91c24f0a
DC
9429 if (orig_pdi->sibling)
9430 return orig_pdi->sibling;
9431
9432 /* Are there any children to deal with? */
9433
9434 if (!orig_pdi->has_children)
9435 return info_ptr;
9436
4bb7a0a7 9437 /* Skip the children the long way. */
91c24f0a 9438
dee91e82 9439 return skip_children (reader, info_ptr);
91c24f0a
DC
9440}
9441
257e7a09 9442/* Expand this partial symbol table into a full symbol table. SELF is
442e4d9c 9443 not NULL. */
c906108c
SS
9444
9445static void
257e7a09
YQ
9446dwarf2_read_symtab (struct partial_symtab *self,
9447 struct objfile *objfile)
c906108c 9448{
ed2dc618
SM
9449 struct dwarf2_per_objfile *dwarf2_per_objfile
9450 = get_dwarf2_per_objfile (objfile);
9451
257e7a09 9452 if (self->readin)
c906108c 9453 {
442e4d9c 9454 warning (_("bug: psymtab for %s is already read in."),
257e7a09 9455 self->filename);
442e4d9c
YQ
9456 }
9457 else
9458 {
9459 if (info_verbose)
c906108c 9460 {
442e4d9c 9461 printf_filtered (_("Reading in symbols for %s..."),
257e7a09 9462 self->filename);
442e4d9c 9463 gdb_flush (gdb_stdout);
c906108c 9464 }
c906108c 9465
442e4d9c
YQ
9466 /* If this psymtab is constructed from a debug-only objfile, the
9467 has_section_at_zero flag will not necessarily be correct. We
9468 can get the correct value for this flag by looking at the data
9469 associated with the (presumably stripped) associated objfile. */
9470 if (objfile->separate_debug_objfile_backlink)
9471 {
9472 struct dwarf2_per_objfile *dpo_backlink
ed2dc618 9473 = get_dwarf2_per_objfile (objfile->separate_debug_objfile_backlink);
9a619af0 9474
442e4d9c
YQ
9475 dwarf2_per_objfile->has_section_at_zero
9476 = dpo_backlink->has_section_at_zero;
9477 }
b2ab525c 9478
442e4d9c 9479 dwarf2_per_objfile->reading_partial_symbols = 0;
98bfdba5 9480
257e7a09 9481 psymtab_to_symtab_1 (self);
c906108c 9482
442e4d9c
YQ
9483 /* Finish up the debug error message. */
9484 if (info_verbose)
9485 printf_filtered (_("done.\n"));
c906108c 9486 }
95554aad 9487
ed2dc618 9488 process_cu_includes (dwarf2_per_objfile);
c906108c 9489}
9cdd5dbd
DE
9490\f
9491/* Reading in full CUs. */
c906108c 9492
10b3939b
DJ
9493/* Add PER_CU to the queue. */
9494
9495static void
95554aad
TT
9496queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
9497 enum language pretend_language)
10b3939b
DJ
9498{
9499 struct dwarf2_queue_item *item;
9500
9501 per_cu->queued = 1;
8d749320 9502 item = XNEW (struct dwarf2_queue_item);
10b3939b 9503 item->per_cu = per_cu;
95554aad 9504 item->pretend_language = pretend_language;
10b3939b
DJ
9505 item->next = NULL;
9506
9507 if (dwarf2_queue == NULL)
9508 dwarf2_queue = item;
9509 else
9510 dwarf2_queue_tail->next = item;
9511
9512 dwarf2_queue_tail = item;
9513}
9514
89e63ee4
DE
9515/* If PER_CU is not yet queued, add it to the queue.
9516 If DEPENDENT_CU is non-NULL, it has a reference to PER_CU so add a
9517 dependency.
0907af0c 9518 The result is non-zero if PER_CU was queued, otherwise the result is zero
69d751e3
DE
9519 meaning either PER_CU is already queued or it is already loaded.
9520
9521 N.B. There is an invariant here that if a CU is queued then it is loaded.
9522 The caller is required to load PER_CU if we return non-zero. */
0907af0c
DE
9523
9524static int
89e63ee4 9525maybe_queue_comp_unit (struct dwarf2_cu *dependent_cu,
0907af0c
DE
9526 struct dwarf2_per_cu_data *per_cu,
9527 enum language pretend_language)
9528{
9529 /* We may arrive here during partial symbol reading, if we need full
9530 DIEs to process an unusual case (e.g. template arguments). Do
9531 not queue PER_CU, just tell our caller to load its DIEs. */
ed2dc618 9532 if (per_cu->dwarf2_per_objfile->reading_partial_symbols)
0907af0c
DE
9533 {
9534 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
9535 return 1;
9536 return 0;
9537 }
9538
9539 /* Mark the dependence relation so that we don't flush PER_CU
9540 too early. */
89e63ee4
DE
9541 if (dependent_cu != NULL)
9542 dwarf2_add_dependence (dependent_cu, per_cu);
0907af0c
DE
9543
9544 /* If it's already on the queue, we have nothing to do. */
9545 if (per_cu->queued)
9546 return 0;
9547
9548 /* If the compilation unit is already loaded, just mark it as
9549 used. */
9550 if (per_cu->cu != NULL)
9551 {
9552 per_cu->cu->last_used = 0;
9553 return 0;
9554 }
9555
9556 /* Add it to the queue. */
9557 queue_comp_unit (per_cu, pretend_language);
9558
9559 return 1;
9560}
9561
10b3939b
DJ
9562/* Process the queue. */
9563
9564static void
ed2dc618 9565process_queue (struct dwarf2_per_objfile *dwarf2_per_objfile)
10b3939b
DJ
9566{
9567 struct dwarf2_queue_item *item, *next_item;
9568
b4f54984 9569 if (dwarf_read_debug)
45cfd468
DE
9570 {
9571 fprintf_unfiltered (gdb_stdlog,
9572 "Expanding one or more symtabs of objfile %s ...\n",
4262abfb 9573 objfile_name (dwarf2_per_objfile->objfile));
45cfd468
DE
9574 }
9575
03dd20cc
DJ
9576 /* The queue starts out with one item, but following a DIE reference
9577 may load a new CU, adding it to the end of the queue. */
10b3939b
DJ
9578 for (item = dwarf2_queue; item != NULL; dwarf2_queue = item = next_item)
9579 {
cc12ce38
DE
9580 if ((dwarf2_per_objfile->using_index
9581 ? !item->per_cu->v.quick->compunit_symtab
9582 : (item->per_cu->v.psymtab && !item->per_cu->v.psymtab->readin))
9583 /* Skip dummy CUs. */
9584 && item->per_cu->cu != NULL)
f4dc4d17
DE
9585 {
9586 struct dwarf2_per_cu_data *per_cu = item->per_cu;
73be47f5 9587 unsigned int debug_print_threshold;
247f5c4f 9588 char buf[100];
f4dc4d17 9589
247f5c4f 9590 if (per_cu->is_debug_types)
f4dc4d17 9591 {
247f5c4f
DE
9592 struct signatured_type *sig_type =
9593 (struct signatured_type *) per_cu;
9594
9d8780f0 9595 sprintf (buf, "TU %s at offset %s",
73be47f5 9596 hex_string (sig_type->signature),
9d8780f0 9597 sect_offset_str (per_cu->sect_off));
73be47f5
DE
9598 /* There can be 100s of TUs.
9599 Only print them in verbose mode. */
9600 debug_print_threshold = 2;
f4dc4d17 9601 }
247f5c4f 9602 else
73be47f5 9603 {
9d8780f0
SM
9604 sprintf (buf, "CU at offset %s",
9605 sect_offset_str (per_cu->sect_off));
73be47f5
DE
9606 debug_print_threshold = 1;
9607 }
247f5c4f 9608
b4f54984 9609 if (dwarf_read_debug >= debug_print_threshold)
247f5c4f 9610 fprintf_unfiltered (gdb_stdlog, "Expanding symtab of %s\n", buf);
f4dc4d17
DE
9611
9612 if (per_cu->is_debug_types)
9613 process_full_type_unit (per_cu, item->pretend_language);
9614 else
9615 process_full_comp_unit (per_cu, item->pretend_language);
9616
b4f54984 9617 if (dwarf_read_debug >= debug_print_threshold)
247f5c4f 9618 fprintf_unfiltered (gdb_stdlog, "Done expanding %s\n", buf);
f4dc4d17 9619 }
10b3939b
DJ
9620
9621 item->per_cu->queued = 0;
9622 next_item = item->next;
9623 xfree (item);
9624 }
9625
9626 dwarf2_queue_tail = NULL;
45cfd468 9627
b4f54984 9628 if (dwarf_read_debug)
45cfd468
DE
9629 {
9630 fprintf_unfiltered (gdb_stdlog, "Done expanding symtabs of %s.\n",
4262abfb 9631 objfile_name (dwarf2_per_objfile->objfile));
45cfd468 9632 }
10b3939b
DJ
9633}
9634
10b3939b
DJ
9635/* Read in full symbols for PST, and anything it depends on. */
9636
c906108c 9637static void
fba45db2 9638psymtab_to_symtab_1 (struct partial_symtab *pst)
c906108c 9639{
10b3939b 9640 struct dwarf2_per_cu_data *per_cu;
aaa75496
JB
9641 int i;
9642
95554aad
TT
9643 if (pst->readin)
9644 return;
9645
aaa75496 9646 for (i = 0; i < pst->number_of_dependencies; i++)
95554aad
TT
9647 if (!pst->dependencies[i]->readin
9648 && pst->dependencies[i]->user == NULL)
aaa75496
JB
9649 {
9650 /* Inform about additional files that need to be read in. */
9651 if (info_verbose)
9652 {
a3f17187 9653 /* FIXME: i18n: Need to make this a single string. */
aaa75496
JB
9654 fputs_filtered (" ", gdb_stdout);
9655 wrap_here ("");
9656 fputs_filtered ("and ", gdb_stdout);
9657 wrap_here ("");
9658 printf_filtered ("%s...", pst->dependencies[i]->filename);
0963b4bd 9659 wrap_here (""); /* Flush output. */
aaa75496
JB
9660 gdb_flush (gdb_stdout);
9661 }
9662 psymtab_to_symtab_1 (pst->dependencies[i]);
9663 }
9664
9a3c8263 9665 per_cu = (struct dwarf2_per_cu_data *) pst->read_symtab_private;
10b3939b
DJ
9666
9667 if (per_cu == NULL)
aaa75496
JB
9668 {
9669 /* It's an include file, no symbols to read for it.
9670 Everything is in the parent symtab. */
9671 pst->readin = 1;
9672 return;
9673 }
c906108c 9674
a0f42c21 9675 dw2_do_instantiate_symtab (per_cu);
10b3939b
DJ
9676}
9677
dee91e82
DE
9678/* Trivial hash function for die_info: the hash value of a DIE
9679 is its offset in .debug_info for this objfile. */
10b3939b 9680
dee91e82
DE
9681static hashval_t
9682die_hash (const void *item)
10b3939b 9683{
9a3c8263 9684 const struct die_info *die = (const struct die_info *) item;
6502dd73 9685
9c541725 9686 return to_underlying (die->sect_off);
dee91e82 9687}
63d06c5c 9688
dee91e82
DE
9689/* Trivial comparison function for die_info structures: two DIEs
9690 are equal if they have the same offset. */
98bfdba5 9691
dee91e82
DE
9692static int
9693die_eq (const void *item_lhs, const void *item_rhs)
9694{
9a3c8263
SM
9695 const struct die_info *die_lhs = (const struct die_info *) item_lhs;
9696 const struct die_info *die_rhs = (const struct die_info *) item_rhs;
c906108c 9697
9c541725 9698 return die_lhs->sect_off == die_rhs->sect_off;
dee91e82 9699}
c906108c 9700
dee91e82
DE
9701/* die_reader_func for load_full_comp_unit.
9702 This is identical to read_signatured_type_reader,
9703 but is kept separate for now. */
c906108c 9704
dee91e82
DE
9705static void
9706load_full_comp_unit_reader (const struct die_reader_specs *reader,
d521ce57 9707 const gdb_byte *info_ptr,
dee91e82
DE
9708 struct die_info *comp_unit_die,
9709 int has_children,
9710 void *data)
9711{
9712 struct dwarf2_cu *cu = reader->cu;
9a3c8263 9713 enum language *language_ptr = (enum language *) data;
6caca83c 9714
dee91e82
DE
9715 gdb_assert (cu->die_hash == NULL);
9716 cu->die_hash =
9717 htab_create_alloc_ex (cu->header.length / 12,
9718 die_hash,
9719 die_eq,
9720 NULL,
9721 &cu->comp_unit_obstack,
9722 hashtab_obstack_allocate,
9723 dummy_obstack_deallocate);
e142c38c 9724
dee91e82
DE
9725 if (has_children)
9726 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
9727 &info_ptr, comp_unit_die);
9728 cu->dies = comp_unit_die;
9729 /* comp_unit_die is not stored in die_hash, no need. */
10b3939b
DJ
9730
9731 /* We try not to read any attributes in this function, because not
9cdd5dbd 9732 all CUs needed for references have been loaded yet, and symbol
10b3939b 9733 table processing isn't initialized. But we have to set the CU language,
dee91e82
DE
9734 or we won't be able to build types correctly.
9735 Similarly, if we do not read the producer, we can not apply
9736 producer-specific interpretation. */
95554aad 9737 prepare_one_comp_unit (cu, cu->dies, *language_ptr);
dee91e82 9738}
10b3939b 9739
dee91e82 9740/* Load the DIEs associated with PER_CU into memory. */
a6c727b2 9741
dee91e82 9742static void
95554aad
TT
9743load_full_comp_unit (struct dwarf2_per_cu_data *this_cu,
9744 enum language pretend_language)
dee91e82 9745{
3019eac3 9746 gdb_assert (! this_cu->is_debug_types);
c5b7e1cb 9747
f4dc4d17
DE
9748 init_cutu_and_read_dies (this_cu, NULL, 1, 1,
9749 load_full_comp_unit_reader, &pretend_language);
10b3939b
DJ
9750}
9751
3da10d80
KS
9752/* Add a DIE to the delayed physname list. */
9753
9754static void
9755add_to_method_list (struct type *type, int fnfield_index, int index,
9756 const char *name, struct die_info *die,
9757 struct dwarf2_cu *cu)
9758{
9759 struct delayed_method_info mi;
9760 mi.type = type;
9761 mi.fnfield_index = fnfield_index;
9762 mi.index = index;
9763 mi.name = name;
9764 mi.die = die;
c89b44cd 9765 cu->method_list.push_back (mi);
3da10d80
KS
9766}
9767
3693fdb3
PA
9768/* Check whether [PHYSNAME, PHYSNAME+LEN) ends with a modifier like
9769 "const" / "volatile". If so, decrements LEN by the length of the
9770 modifier and return true. Otherwise return false. */
9771
9772template<size_t N>
9773static bool
9774check_modifier (const char *physname, size_t &len, const char (&mod)[N])
9775{
9776 size_t mod_len = sizeof (mod) - 1;
9777 if (len > mod_len && startswith (physname + (len - mod_len), mod))
9778 {
9779 len -= mod_len;
9780 return true;
9781 }
9782 return false;
9783}
9784
3da10d80
KS
9785/* Compute the physnames of any methods on the CU's method list.
9786
9787 The computation of method physnames is delayed in order to avoid the
9788 (bad) condition that one of the method's formal parameters is of an as yet
9789 incomplete type. */
9790
9791static void
9792compute_delayed_physnames (struct dwarf2_cu *cu)
9793{
3693fdb3 9794 /* Only C++ delays computing physnames. */
c89b44cd 9795 if (cu->method_list.empty ())
3693fdb3
PA
9796 return;
9797 gdb_assert (cu->language == language_cplus);
9798
c89b44cd 9799 for (struct delayed_method_info &mi : cu->method_list)
3da10d80 9800 {
1d06ead6 9801 const char *physname;
3da10d80 9802 struct fn_fieldlist *fn_flp
c89b44cd
TT
9803 = &TYPE_FN_FIELDLIST (mi.type, mi.fnfield_index);
9804 physname = dwarf2_physname (mi.name, mi.die, cu);
9805 TYPE_FN_FIELD_PHYSNAME (fn_flp->fn_fields, mi.index)
005e54bb 9806 = physname ? physname : "";
3693fdb3
PA
9807
9808 /* Since there's no tag to indicate whether a method is a
9809 const/volatile overload, extract that information out of the
9810 demangled name. */
9811 if (physname != NULL)
9812 {
9813 size_t len = strlen (physname);
9814
9815 while (1)
9816 {
9817 if (physname[len] == ')') /* shortcut */
9818 break;
9819 else if (check_modifier (physname, len, " const"))
c89b44cd 9820 TYPE_FN_FIELD_CONST (fn_flp->fn_fields, mi.index) = 1;
3693fdb3 9821 else if (check_modifier (physname, len, " volatile"))
c89b44cd 9822 TYPE_FN_FIELD_VOLATILE (fn_flp->fn_fields, mi.index) = 1;
3693fdb3
PA
9823 else
9824 break;
9825 }
9826 }
3da10d80 9827 }
c89b44cd
TT
9828
9829 /* The list is no longer needed. */
9830 cu->method_list.clear ();
3da10d80
KS
9831}
9832
a766d390
DE
9833/* Go objects should be embedded in a DW_TAG_module DIE,
9834 and it's not clear if/how imported objects will appear.
9835 To keep Go support simple until that's worked out,
9836 go back through what we've read and create something usable.
9837 We could do this while processing each DIE, and feels kinda cleaner,
9838 but that way is more invasive.
9839 This is to, for example, allow the user to type "p var" or "b main"
9840 without having to specify the package name, and allow lookups
9841 of module.object to work in contexts that use the expression
9842 parser. */
9843
9844static void
9845fixup_go_packaging (struct dwarf2_cu *cu)
9846{
9847 char *package_name = NULL;
9848 struct pending *list;
9849 int i;
9850
9851 for (list = global_symbols; list != NULL; list = list->next)
9852 {
9853 for (i = 0; i < list->nsyms; ++i)
9854 {
9855 struct symbol *sym = list->symbol[i];
9856
9857 if (SYMBOL_LANGUAGE (sym) == language_go
9858 && SYMBOL_CLASS (sym) == LOC_BLOCK)
9859 {
9860 char *this_package_name = go_symbol_package_name (sym);
9861
9862 if (this_package_name == NULL)
9863 continue;
9864 if (package_name == NULL)
9865 package_name = this_package_name;
9866 else
9867 {
518817b3
SM
9868 struct objfile *objfile
9869 = cu->per_cu->dwarf2_per_objfile->objfile;
a766d390
DE
9870 if (strcmp (package_name, this_package_name) != 0)
9871 complaint (&symfile_complaints,
9872 _("Symtab %s has objects from two different Go packages: %s and %s"),
08be3fe3
DE
9873 (symbol_symtab (sym) != NULL
9874 ? symtab_to_filename_for_display
9875 (symbol_symtab (sym))
e3b94546 9876 : objfile_name (objfile)),
a766d390
DE
9877 this_package_name, package_name);
9878 xfree (this_package_name);
9879 }
9880 }
9881 }
9882 }
9883
9884 if (package_name != NULL)
9885 {
518817b3 9886 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
34a68019 9887 const char *saved_package_name
224c3ddb
SM
9888 = (const char *) obstack_copy0 (&objfile->per_bfd->storage_obstack,
9889 package_name,
9890 strlen (package_name));
19f392bc
UW
9891 struct type *type = init_type (objfile, TYPE_CODE_MODULE, 0,
9892 saved_package_name);
a766d390
DE
9893 struct symbol *sym;
9894
9895 TYPE_TAG_NAME (type) = TYPE_NAME (type);
9896
e623cf5d 9897 sym = allocate_symbol (objfile);
f85f34ed 9898 SYMBOL_SET_LANGUAGE (sym, language_go, &objfile->objfile_obstack);
86f62fd7
TT
9899 SYMBOL_SET_NAMES (sym, saved_package_name,
9900 strlen (saved_package_name), 0, objfile);
a766d390
DE
9901 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
9902 e.g., "main" finds the "main" module and not C's main(). */
9903 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
f1e6e072 9904 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
a766d390
DE
9905 SYMBOL_TYPE (sym) = type;
9906
9907 add_symbol_to_list (sym, &global_symbols);
9908
9909 xfree (package_name);
9910 }
9911}
9912
c9317f21
TT
9913/* Allocate a fully-qualified name consisting of the two parts on the
9914 obstack. */
9915
9916static const char *
9917rust_fully_qualify (struct obstack *obstack, const char *p1, const char *p2)
9918{
9919 return obconcat (obstack, p1, "::", p2, (char *) NULL);
9920}
9921
9922/* A helper that allocates a struct discriminant_info to attach to a
9923 union type. */
9924
9925static struct discriminant_info *
9926alloc_discriminant_info (struct type *type, int discriminant_index,
9927 int default_index)
9928{
9929 gdb_assert (TYPE_CODE (type) == TYPE_CODE_UNION);
c7b15a66
TT
9930 gdb_assert (discriminant_index == -1
9931 || (discriminant_index >= 0
9932 && discriminant_index < TYPE_NFIELDS (type)));
c9317f21 9933 gdb_assert (default_index == -1
c7b15a66 9934 || (default_index >= 0 && default_index < TYPE_NFIELDS (type)));
c9317f21
TT
9935
9936 TYPE_FLAG_DISCRIMINATED_UNION (type) = 1;
9937
9938 struct discriminant_info *disc
9939 = ((struct discriminant_info *)
9940 TYPE_ZALLOC (type,
9941 offsetof (struct discriminant_info, discriminants)
9942 + TYPE_NFIELDS (type) * sizeof (disc->discriminants[0])));
9943 disc->default_index = default_index;
9944 disc->discriminant_index = discriminant_index;
9945
9946 struct dynamic_prop prop;
9947 prop.kind = PROP_UNDEFINED;
9948 prop.data.baton = disc;
9949
9950 add_dyn_prop (DYN_PROP_DISCRIMINATED, prop, type);
9951
9952 return disc;
9953}
9954
9955/* Some versions of rustc emitted enums in an unusual way.
9956
9957 Ordinary enums were emitted as unions. The first element of each
9958 structure in the union was named "RUST$ENUM$DISR". This element
9959 held the discriminant.
9960
9961 These versions of Rust also implemented the "non-zero"
9962 optimization. When the enum had two values, and one is empty and
9963 the other holds a pointer that cannot be zero, the pointer is used
9964 as the discriminant, with a zero value meaning the empty variant.
9965 Here, the union's first member is of the form
9966 RUST$ENCODED$ENUM$<fieldno>$<fieldno>$...$<variantname>
9967 where the fieldnos are the indices of the fields that should be
9968 traversed in order to find the field (which may be several fields deep)
9969 and the variantname is the name of the variant of the case when the
9970 field is zero.
9971
9972 This function recognizes whether TYPE is of one of these forms,
9973 and, if so, smashes it to be a variant type. */
9974
9975static void
9976quirk_rust_enum (struct type *type, struct objfile *objfile)
9977{
9978 gdb_assert (TYPE_CODE (type) == TYPE_CODE_UNION);
9979
9980 /* We don't need to deal with empty enums. */
9981 if (TYPE_NFIELDS (type) == 0)
9982 return;
9983
9984#define RUST_ENUM_PREFIX "RUST$ENCODED$ENUM$"
9985 if (TYPE_NFIELDS (type) == 1
9986 && startswith (TYPE_FIELD_NAME (type, 0), RUST_ENUM_PREFIX))
9987 {
9988 const char *name = TYPE_FIELD_NAME (type, 0) + strlen (RUST_ENUM_PREFIX);
9989
9990 /* Decode the field name to find the offset of the
9991 discriminant. */
9992 ULONGEST bit_offset = 0;
9993 struct type *field_type = TYPE_FIELD_TYPE (type, 0);
9994 while (name[0] >= '0' && name[0] <= '9')
9995 {
9996 char *tail;
9997 unsigned long index = strtoul (name, &tail, 10);
9998 name = tail;
9999 if (*name != '$'
10000 || index >= TYPE_NFIELDS (field_type)
10001 || (TYPE_FIELD_LOC_KIND (field_type, index)
10002 != FIELD_LOC_KIND_BITPOS))
10003 {
10004 complaint (&symfile_complaints,
10005 _("Could not parse Rust enum encoding string \"%s\""
10006 "[in module %s]"),
10007 TYPE_FIELD_NAME (type, 0),
10008 objfile_name (objfile));
10009 return;
10010 }
10011 ++name;
10012
10013 bit_offset += TYPE_FIELD_BITPOS (field_type, index);
10014 field_type = TYPE_FIELD_TYPE (field_type, index);
10015 }
10016
10017 /* Make a union to hold the variants. */
10018 struct type *union_type = alloc_type (objfile);
10019 TYPE_CODE (union_type) = TYPE_CODE_UNION;
10020 TYPE_NFIELDS (union_type) = 3;
10021 TYPE_FIELDS (union_type)
10022 = (struct field *) TYPE_ZALLOC (type, 3 * sizeof (struct field));
10023 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
10024
10025 /* Put the discriminant must at index 0. */
10026 TYPE_FIELD_TYPE (union_type, 0) = field_type;
10027 TYPE_FIELD_ARTIFICIAL (union_type, 0) = 1;
10028 TYPE_FIELD_NAME (union_type, 0) = "<<discriminant>>";
10029 SET_FIELD_BITPOS (TYPE_FIELD (union_type, 0), bit_offset);
10030
10031 /* The order of fields doesn't really matter, so put the real
10032 field at index 1 and the data-less field at index 2. */
10033 struct discriminant_info *disc
10034 = alloc_discriminant_info (union_type, 0, 1);
10035 TYPE_FIELD (union_type, 1) = TYPE_FIELD (type, 0);
10036 TYPE_FIELD_NAME (union_type, 1)
10037 = rust_last_path_segment (TYPE_NAME (TYPE_FIELD_TYPE (union_type, 1)));
10038 TYPE_NAME (TYPE_FIELD_TYPE (union_type, 1))
10039 = rust_fully_qualify (&objfile->objfile_obstack, TYPE_NAME (type),
10040 TYPE_FIELD_NAME (union_type, 1));
10041
10042 const char *dataless_name
10043 = rust_fully_qualify (&objfile->objfile_obstack, TYPE_NAME (type),
10044 name);
10045 struct type *dataless_type = init_type (objfile, TYPE_CODE_VOID, 0,
10046 dataless_name);
10047 TYPE_FIELD_TYPE (union_type, 2) = dataless_type;
10048 /* NAME points into the original discriminant name, which
10049 already has the correct lifetime. */
10050 TYPE_FIELD_NAME (union_type, 2) = name;
10051 SET_FIELD_BITPOS (TYPE_FIELD (union_type, 2), 0);
10052 disc->discriminants[2] = 0;
10053
10054 /* Smash this type to be a structure type. We have to do this
10055 because the type has already been recorded. */
10056 TYPE_CODE (type) = TYPE_CODE_STRUCT;
10057 TYPE_NFIELDS (type) = 1;
10058 TYPE_FIELDS (type)
10059 = (struct field *) TYPE_ZALLOC (type, sizeof (struct field));
10060
10061 /* Install the variant part. */
10062 TYPE_FIELD_TYPE (type, 0) = union_type;
10063 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
10064 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
10065 }
10066 else if (TYPE_NFIELDS (type) == 1)
10067 {
10068 /* We assume that a union with a single field is a univariant
10069 enum. */
10070 /* Smash this type to be a structure type. We have to do this
10071 because the type has already been recorded. */
10072 TYPE_CODE (type) = TYPE_CODE_STRUCT;
10073
10074 /* Make a union to hold the variants. */
10075 struct type *union_type = alloc_type (objfile);
10076 TYPE_CODE (union_type) = TYPE_CODE_UNION;
10077 TYPE_NFIELDS (union_type) = TYPE_NFIELDS (type);
10078 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
10079 TYPE_FIELDS (union_type) = TYPE_FIELDS (type);
10080
10081 struct type *field_type = TYPE_FIELD_TYPE (union_type, 0);
10082 const char *variant_name
10083 = rust_last_path_segment (TYPE_NAME (field_type));
10084 TYPE_FIELD_NAME (union_type, 0) = variant_name;
10085 TYPE_NAME (field_type)
10086 = rust_fully_qualify (&objfile->objfile_obstack,
c7b15a66 10087 TYPE_NAME (type), variant_name);
c9317f21
TT
10088
10089 /* Install the union in the outer struct type. */
10090 TYPE_NFIELDS (type) = 1;
10091 TYPE_FIELDS (type)
10092 = (struct field *) TYPE_ZALLOC (union_type, sizeof (struct field));
10093 TYPE_FIELD_TYPE (type, 0) = union_type;
10094 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
10095 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
10096
10097 alloc_discriminant_info (union_type, -1, 0);
10098 }
10099 else
10100 {
10101 struct type *disr_type = nullptr;
10102 for (int i = 0; i < TYPE_NFIELDS (type); ++i)
10103 {
10104 disr_type = TYPE_FIELD_TYPE (type, i);
10105
10106 if (TYPE_NFIELDS (disr_type) == 0)
10107 {
10108 /* Could be data-less variant, so keep going. */
10109 }
10110 else if (strcmp (TYPE_FIELD_NAME (disr_type, 0),
10111 "RUST$ENUM$DISR") != 0)
10112 {
10113 /* Not a Rust enum. */
10114 return;
10115 }
10116 else
10117 {
10118 /* Found one. */
10119 break;
10120 }
10121 }
10122
10123 /* If we got here without a discriminant, then it's probably
10124 just a union. */
10125 if (disr_type == nullptr)
10126 return;
10127
10128 /* Smash this type to be a structure type. We have to do this
10129 because the type has already been recorded. */
10130 TYPE_CODE (type) = TYPE_CODE_STRUCT;
10131
10132 /* Make a union to hold the variants. */
10133 struct field *disr_field = &TYPE_FIELD (disr_type, 0);
10134 struct type *union_type = alloc_type (objfile);
10135 TYPE_CODE (union_type) = TYPE_CODE_UNION;
10136 TYPE_NFIELDS (union_type) = 1 + TYPE_NFIELDS (type);
10137 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
10138 TYPE_FIELDS (union_type)
10139 = (struct field *) TYPE_ZALLOC (union_type,
10140 (TYPE_NFIELDS (union_type)
10141 * sizeof (struct field)));
10142
10143 memcpy (TYPE_FIELDS (union_type) + 1, TYPE_FIELDS (type),
10144 TYPE_NFIELDS (type) * sizeof (struct field));
10145
10146 /* Install the discriminant at index 0 in the union. */
10147 TYPE_FIELD (union_type, 0) = *disr_field;
10148 TYPE_FIELD_ARTIFICIAL (union_type, 0) = 1;
10149 TYPE_FIELD_NAME (union_type, 0) = "<<discriminant>>";
10150
10151 /* Install the union in the outer struct type. */
10152 TYPE_FIELD_TYPE (type, 0) = union_type;
10153 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
10154 TYPE_NFIELDS (type) = 1;
10155
10156 /* Set the size and offset of the union type. */
10157 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
10158
10159 /* We need a way to find the correct discriminant given a
10160 variant name. For convenience we build a map here. */
10161 struct type *enum_type = FIELD_TYPE (*disr_field);
10162 std::unordered_map<std::string, ULONGEST> discriminant_map;
10163 for (int i = 0; i < TYPE_NFIELDS (enum_type); ++i)
10164 {
10165 if (TYPE_FIELD_LOC_KIND (enum_type, i) == FIELD_LOC_KIND_ENUMVAL)
10166 {
10167 const char *name
10168 = rust_last_path_segment (TYPE_FIELD_NAME (enum_type, i));
10169 discriminant_map[name] = TYPE_FIELD_ENUMVAL (enum_type, i);
10170 }
10171 }
10172
10173 int n_fields = TYPE_NFIELDS (union_type);
10174 struct discriminant_info *disc
10175 = alloc_discriminant_info (union_type, 0, -1);
10176 /* Skip the discriminant here. */
10177 for (int i = 1; i < n_fields; ++i)
10178 {
10179 /* Find the final word in the name of this variant's type.
10180 That name can be used to look up the correct
10181 discriminant. */
10182 const char *variant_name
10183 = rust_last_path_segment (TYPE_NAME (TYPE_FIELD_TYPE (union_type,
10184 i)));
10185
10186 auto iter = discriminant_map.find (variant_name);
10187 if (iter != discriminant_map.end ())
10188 disc->discriminants[i] = iter->second;
10189
10190 /* Remove the discriminant field. */
10191 struct type *sub_type = TYPE_FIELD_TYPE (union_type, i);
10192 --TYPE_NFIELDS (sub_type);
10193 ++TYPE_FIELDS (sub_type);
10194 TYPE_FIELD_NAME (union_type, i) = variant_name;
10195 TYPE_NAME (sub_type)
10196 = rust_fully_qualify (&objfile->objfile_obstack,
10197 TYPE_NAME (type), variant_name);
10198 }
10199 }
10200}
10201
10202/* Rewrite some Rust unions to be structures with variants parts. */
10203
10204static void
10205rust_union_quirks (struct dwarf2_cu *cu)
10206{
10207 gdb_assert (cu->language == language_rust);
10208 for (struct type *type : cu->rust_unions)
10209 quirk_rust_enum (type, cu->per_cu->dwarf2_per_objfile->objfile);
10210}
10211
95554aad
TT
10212/* Return the symtab for PER_CU. This works properly regardless of
10213 whether we're using the index or psymtabs. */
10214
43f3e411
DE
10215static struct compunit_symtab *
10216get_compunit_symtab (struct dwarf2_per_cu_data *per_cu)
95554aad 10217{
ed2dc618 10218 return (per_cu->dwarf2_per_objfile->using_index
43f3e411
DE
10219 ? per_cu->v.quick->compunit_symtab
10220 : per_cu->v.psymtab->compunit_symtab);
95554aad
TT
10221}
10222
10223/* A helper function for computing the list of all symbol tables
10224 included by PER_CU. */
10225
10226static void
43f3e411 10227recursively_compute_inclusions (VEC (compunit_symtab_ptr) **result,
ec94af83 10228 htab_t all_children, htab_t all_type_symtabs,
f9125b6c 10229 struct dwarf2_per_cu_data *per_cu,
43f3e411 10230 struct compunit_symtab *immediate_parent)
95554aad
TT
10231{
10232 void **slot;
10233 int ix;
43f3e411 10234 struct compunit_symtab *cust;
95554aad
TT
10235 struct dwarf2_per_cu_data *iter;
10236
10237 slot = htab_find_slot (all_children, per_cu, INSERT);
10238 if (*slot != NULL)
10239 {
10240 /* This inclusion and its children have been processed. */
10241 return;
10242 }
10243
10244 *slot = per_cu;
10245 /* Only add a CU if it has a symbol table. */
43f3e411
DE
10246 cust = get_compunit_symtab (per_cu);
10247 if (cust != NULL)
ec94af83
DE
10248 {
10249 /* If this is a type unit only add its symbol table if we haven't
10250 seen it yet (type unit per_cu's can share symtabs). */
10251 if (per_cu->is_debug_types)
10252 {
43f3e411 10253 slot = htab_find_slot (all_type_symtabs, cust, INSERT);
ec94af83
DE
10254 if (*slot == NULL)
10255 {
43f3e411
DE
10256 *slot = cust;
10257 VEC_safe_push (compunit_symtab_ptr, *result, cust);
10258 if (cust->user == NULL)
10259 cust->user = immediate_parent;
ec94af83
DE
10260 }
10261 }
10262 else
f9125b6c 10263 {
43f3e411
DE
10264 VEC_safe_push (compunit_symtab_ptr, *result, cust);
10265 if (cust->user == NULL)
10266 cust->user = immediate_parent;
f9125b6c 10267 }
ec94af83 10268 }
95554aad
TT
10269
10270 for (ix = 0;
796a7ff8 10271 VEC_iterate (dwarf2_per_cu_ptr, per_cu->imported_symtabs, ix, iter);
95554aad 10272 ++ix)
ec94af83
DE
10273 {
10274 recursively_compute_inclusions (result, all_children,
43f3e411 10275 all_type_symtabs, iter, cust);
ec94af83 10276 }
95554aad
TT
10277}
10278
43f3e411 10279/* Compute the compunit_symtab 'includes' fields for the compunit_symtab of
95554aad
TT
10280 PER_CU. */
10281
10282static void
43f3e411 10283compute_compunit_symtab_includes (struct dwarf2_per_cu_data *per_cu)
95554aad 10284{
f4dc4d17
DE
10285 gdb_assert (! per_cu->is_debug_types);
10286
796a7ff8 10287 if (!VEC_empty (dwarf2_per_cu_ptr, per_cu->imported_symtabs))
95554aad
TT
10288 {
10289 int ix, len;
ec94af83 10290 struct dwarf2_per_cu_data *per_cu_iter;
43f3e411
DE
10291 struct compunit_symtab *compunit_symtab_iter;
10292 VEC (compunit_symtab_ptr) *result_symtabs = NULL;
ec94af83 10293 htab_t all_children, all_type_symtabs;
43f3e411 10294 struct compunit_symtab *cust = get_compunit_symtab (per_cu);
95554aad
TT
10295
10296 /* If we don't have a symtab, we can just skip this case. */
43f3e411 10297 if (cust == NULL)
95554aad
TT
10298 return;
10299
10300 all_children = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
10301 NULL, xcalloc, xfree);
ec94af83
DE
10302 all_type_symtabs = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
10303 NULL, xcalloc, xfree);
95554aad
TT
10304
10305 for (ix = 0;
796a7ff8 10306 VEC_iterate (dwarf2_per_cu_ptr, per_cu->imported_symtabs,
ec94af83 10307 ix, per_cu_iter);
95554aad 10308 ++ix)
ec94af83
DE
10309 {
10310 recursively_compute_inclusions (&result_symtabs, all_children,
f9125b6c 10311 all_type_symtabs, per_cu_iter,
43f3e411 10312 cust);
ec94af83 10313 }
95554aad 10314
ec94af83 10315 /* Now we have a transitive closure of all the included symtabs. */
43f3e411
DE
10316 len = VEC_length (compunit_symtab_ptr, result_symtabs);
10317 cust->includes
ed2dc618 10318 = XOBNEWVEC (&per_cu->dwarf2_per_objfile->objfile->objfile_obstack,
8d749320 10319 struct compunit_symtab *, len + 1);
95554aad 10320 for (ix = 0;
43f3e411
DE
10321 VEC_iterate (compunit_symtab_ptr, result_symtabs, ix,
10322 compunit_symtab_iter);
95554aad 10323 ++ix)
43f3e411
DE
10324 cust->includes[ix] = compunit_symtab_iter;
10325 cust->includes[len] = NULL;
95554aad 10326
43f3e411 10327 VEC_free (compunit_symtab_ptr, result_symtabs);
95554aad 10328 htab_delete (all_children);
ec94af83 10329 htab_delete (all_type_symtabs);
95554aad
TT
10330 }
10331}
10332
10333/* Compute the 'includes' field for the symtabs of all the CUs we just
10334 read. */
10335
10336static void
ed2dc618 10337process_cu_includes (struct dwarf2_per_objfile *dwarf2_per_objfile)
95554aad
TT
10338{
10339 int ix;
10340 struct dwarf2_per_cu_data *iter;
10341
10342 for (ix = 0;
10343 VEC_iterate (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus,
10344 ix, iter);
10345 ++ix)
f4dc4d17
DE
10346 {
10347 if (! iter->is_debug_types)
43f3e411 10348 compute_compunit_symtab_includes (iter);
f4dc4d17 10349 }
95554aad
TT
10350
10351 VEC_free (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus);
10352}
10353
9cdd5dbd 10354/* Generate full symbol information for PER_CU, whose DIEs have
10b3939b
DJ
10355 already been loaded into memory. */
10356
10357static void
95554aad
TT
10358process_full_comp_unit (struct dwarf2_per_cu_data *per_cu,
10359 enum language pretend_language)
10b3939b 10360{
10b3939b 10361 struct dwarf2_cu *cu = per_cu->cu;
ed2dc618
SM
10362 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
10363 struct objfile *objfile = dwarf2_per_objfile->objfile;
3e29f34a 10364 struct gdbarch *gdbarch = get_objfile_arch (objfile);
10b3939b 10365 CORE_ADDR lowpc, highpc;
43f3e411 10366 struct compunit_symtab *cust;
10b3939b 10367 CORE_ADDR baseaddr;
4359dff1 10368 struct block *static_block;
3e29f34a 10369 CORE_ADDR addr;
10b3939b
DJ
10370
10371 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
10372
10b3939b 10373 buildsym_init ();
33c7c59d 10374 scoped_free_pendings free_pending;
c89b44cd
TT
10375
10376 /* Clear the list here in case something was left over. */
10377 cu->method_list.clear ();
10b3939b
DJ
10378
10379 cu->list_in_scope = &file_symbols;
c906108c 10380
95554aad
TT
10381 cu->language = pretend_language;
10382 cu->language_defn = language_def (cu->language);
10383
c906108c 10384 /* Do line number decoding in read_file_scope () */
10b3939b 10385 process_die (cu->dies, cu);
c906108c 10386
a766d390
DE
10387 /* For now fudge the Go package. */
10388 if (cu->language == language_go)
10389 fixup_go_packaging (cu);
10390
3da10d80
KS
10391 /* Now that we have processed all the DIEs in the CU, all the types
10392 should be complete, and it should now be safe to compute all of the
10393 physnames. */
10394 compute_delayed_physnames (cu);
3da10d80 10395
c9317f21
TT
10396 if (cu->language == language_rust)
10397 rust_union_quirks (cu);
10398
fae299cd
DC
10399 /* Some compilers don't define a DW_AT_high_pc attribute for the
10400 compilation unit. If the DW_AT_high_pc is missing, synthesize
10401 it, by scanning the DIE's below the compilation unit. */
10b3939b 10402 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
c906108c 10403
3e29f34a
MR
10404 addr = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
10405 static_block = end_symtab_get_static_block (addr, 0, 1);
4359dff1
JK
10406
10407 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
10408 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
10409 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
10410 addrmap to help ensure it has an accurate map of pc values belonging to
10411 this comp unit. */
10412 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
10413
43f3e411
DE
10414 cust = end_symtab_from_static_block (static_block,
10415 SECT_OFF_TEXT (objfile), 0);
c906108c 10416
43f3e411 10417 if (cust != NULL)
c906108c 10418 {
df15bd07 10419 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
4632c0d0 10420
8be455d7
JK
10421 /* Set symtab language to language from DW_AT_language. If the
10422 compilation is from a C file generated by language preprocessors, do
10423 not set the language if it was already deduced by start_subfile. */
43f3e411 10424 if (!(cu->language == language_c
40e3ad0e 10425 && COMPUNIT_FILETABS (cust)->language != language_unknown))
43f3e411 10426 COMPUNIT_FILETABS (cust)->language = cu->language;
8be455d7
JK
10427
10428 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
10429 produce DW_AT_location with location lists but it can be possibly
ab260dad
JK
10430 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
10431 there were bugs in prologue debug info, fixed later in GCC-4.5
10432 by "unwind info for epilogues" patch (which is not directly related).
8be455d7
JK
10433
10434 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
10435 needed, it would be wrong due to missing DW_AT_producer there.
10436
10437 Still one can confuse GDB by using non-standard GCC compilation
10438 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
10439 */
ab260dad 10440 if (cu->has_loclist && gcc_4_minor >= 5)
43f3e411 10441 cust->locations_valid = 1;
e0d00bc7
JK
10442
10443 if (gcc_4_minor >= 5)
43f3e411 10444 cust->epilogue_unwind_valid = 1;
96408a79 10445
43f3e411 10446 cust->call_site_htab = cu->call_site_htab;
c906108c 10447 }
9291a0cd
TT
10448
10449 if (dwarf2_per_objfile->using_index)
43f3e411 10450 per_cu->v.quick->compunit_symtab = cust;
9291a0cd
TT
10451 else
10452 {
10453 struct partial_symtab *pst = per_cu->v.psymtab;
43f3e411 10454 pst->compunit_symtab = cust;
9291a0cd
TT
10455 pst->readin = 1;
10456 }
c906108c 10457
95554aad
TT
10458 /* Push it for inclusion processing later. */
10459 VEC_safe_push (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus, per_cu);
f4dc4d17 10460}
45cfd468 10461
f4dc4d17
DE
10462/* Generate full symbol information for type unit PER_CU, whose DIEs have
10463 already been loaded into memory. */
10464
10465static void
10466process_full_type_unit (struct dwarf2_per_cu_data *per_cu,
10467 enum language pretend_language)
10468{
10469 struct dwarf2_cu *cu = per_cu->cu;
ed2dc618
SM
10470 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
10471 struct objfile *objfile = dwarf2_per_objfile->objfile;
43f3e411 10472 struct compunit_symtab *cust;
0186c6a7
DE
10473 struct signatured_type *sig_type;
10474
10475 gdb_assert (per_cu->is_debug_types);
10476 sig_type = (struct signatured_type *) per_cu;
f4dc4d17
DE
10477
10478 buildsym_init ();
33c7c59d 10479 scoped_free_pendings free_pending;
c89b44cd
TT
10480
10481 /* Clear the list here in case something was left over. */
10482 cu->method_list.clear ();
f4dc4d17
DE
10483
10484 cu->list_in_scope = &file_symbols;
10485
10486 cu->language = pretend_language;
10487 cu->language_defn = language_def (cu->language);
10488
10489 /* The symbol tables are set up in read_type_unit_scope. */
10490 process_die (cu->dies, cu);
10491
10492 /* For now fudge the Go package. */
10493 if (cu->language == language_go)
10494 fixup_go_packaging (cu);
10495
10496 /* Now that we have processed all the DIEs in the CU, all the types
10497 should be complete, and it should now be safe to compute all of the
10498 physnames. */
10499 compute_delayed_physnames (cu);
f4dc4d17 10500
c9317f21
TT
10501 if (cu->language == language_rust)
10502 rust_union_quirks (cu);
10503
f4dc4d17
DE
10504 /* TUs share symbol tables.
10505 If this is the first TU to use this symtab, complete the construction
094b34ac
DE
10506 of it with end_expandable_symtab. Otherwise, complete the addition of
10507 this TU's symbols to the existing symtab. */
43f3e411 10508 if (sig_type->type_unit_group->compunit_symtab == NULL)
45cfd468 10509 {
43f3e411
DE
10510 cust = end_expandable_symtab (0, SECT_OFF_TEXT (objfile));
10511 sig_type->type_unit_group->compunit_symtab = cust;
f4dc4d17 10512
43f3e411 10513 if (cust != NULL)
f4dc4d17
DE
10514 {
10515 /* Set symtab language to language from DW_AT_language. If the
10516 compilation is from a C file generated by language preprocessors,
10517 do not set the language if it was already deduced by
10518 start_subfile. */
43f3e411
DE
10519 if (!(cu->language == language_c
10520 && COMPUNIT_FILETABS (cust)->language != language_c))
10521 COMPUNIT_FILETABS (cust)->language = cu->language;
f4dc4d17
DE
10522 }
10523 }
10524 else
10525 {
0ab9ce85 10526 augment_type_symtab ();
43f3e411 10527 cust = sig_type->type_unit_group->compunit_symtab;
f4dc4d17
DE
10528 }
10529
10530 if (dwarf2_per_objfile->using_index)
43f3e411 10531 per_cu->v.quick->compunit_symtab = cust;
f4dc4d17
DE
10532 else
10533 {
10534 struct partial_symtab *pst = per_cu->v.psymtab;
43f3e411 10535 pst->compunit_symtab = cust;
f4dc4d17 10536 pst->readin = 1;
45cfd468 10537 }
c906108c
SS
10538}
10539
95554aad
TT
10540/* Process an imported unit DIE. */
10541
10542static void
10543process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
10544{
10545 struct attribute *attr;
10546
f4dc4d17
DE
10547 /* For now we don't handle imported units in type units. */
10548 if (cu->per_cu->is_debug_types)
10549 {
10550 error (_("Dwarf Error: DW_TAG_imported_unit is not"
10551 " supported in type units [in module %s]"),
518817b3 10552 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
f4dc4d17
DE
10553 }
10554
95554aad
TT
10555 attr = dwarf2_attr (die, DW_AT_import, cu);
10556 if (attr != NULL)
10557 {
9c541725
PA
10558 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
10559 bool is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
10560 dwarf2_per_cu_data *per_cu
e3b94546 10561 = dwarf2_find_containing_comp_unit (sect_off, is_dwz,
518817b3 10562 cu->per_cu->dwarf2_per_objfile);
95554aad 10563
69d751e3 10564 /* If necessary, add it to the queue and load its DIEs. */
95554aad
TT
10565 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
10566 load_full_comp_unit (per_cu, cu->language);
10567
796a7ff8 10568 VEC_safe_push (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs,
95554aad
TT
10569 per_cu);
10570 }
10571}
10572
4c8aa72d
PA
10573/* RAII object that represents a process_die scope: i.e.,
10574 starts/finishes processing a DIE. */
10575class process_die_scope
adde2bff 10576{
4c8aa72d
PA
10577public:
10578 process_die_scope (die_info *die, dwarf2_cu *cu)
10579 : m_die (die), m_cu (cu)
10580 {
10581 /* We should only be processing DIEs not already in process. */
10582 gdb_assert (!m_die->in_process);
10583 m_die->in_process = true;
10584 }
8c3cb9fa 10585
4c8aa72d
PA
10586 ~process_die_scope ()
10587 {
10588 m_die->in_process = false;
10589
10590 /* If we're done processing the DIE for the CU that owns the line
10591 header, we don't need the line header anymore. */
10592 if (m_cu->line_header_die_owner == m_die)
10593 {
10594 delete m_cu->line_header;
10595 m_cu->line_header = NULL;
10596 m_cu->line_header_die_owner = NULL;
10597 }
10598 }
10599
10600private:
10601 die_info *m_die;
10602 dwarf2_cu *m_cu;
10603};
adde2bff 10604
c906108c
SS
10605/* Process a die and its children. */
10606
10607static void
e7c27a73 10608process_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 10609{
4c8aa72d 10610 process_die_scope scope (die, cu);
adde2bff 10611
c906108c
SS
10612 switch (die->tag)
10613 {
10614 case DW_TAG_padding:
10615 break;
10616 case DW_TAG_compile_unit:
95554aad 10617 case DW_TAG_partial_unit:
e7c27a73 10618 read_file_scope (die, cu);
c906108c 10619 break;
348e048f
DE
10620 case DW_TAG_type_unit:
10621 read_type_unit_scope (die, cu);
10622 break;
c906108c 10623 case DW_TAG_subprogram:
c906108c 10624 case DW_TAG_inlined_subroutine:
edb3359d 10625 read_func_scope (die, cu);
c906108c
SS
10626 break;
10627 case DW_TAG_lexical_block:
14898363
L
10628 case DW_TAG_try_block:
10629 case DW_TAG_catch_block:
e7c27a73 10630 read_lexical_block_scope (die, cu);
c906108c 10631 break;
216f72a1 10632 case DW_TAG_call_site:
96408a79
SA
10633 case DW_TAG_GNU_call_site:
10634 read_call_site_scope (die, cu);
10635 break;
c906108c 10636 case DW_TAG_class_type:
680b30c7 10637 case DW_TAG_interface_type:
c906108c
SS
10638 case DW_TAG_structure_type:
10639 case DW_TAG_union_type:
134d01f1 10640 process_structure_scope (die, cu);
c906108c
SS
10641 break;
10642 case DW_TAG_enumeration_type:
134d01f1 10643 process_enumeration_scope (die, cu);
c906108c 10644 break;
134d01f1 10645
f792889a
DJ
10646 /* These dies have a type, but processing them does not create
10647 a symbol or recurse to process the children. Therefore we can
10648 read them on-demand through read_type_die. */
c906108c 10649 case DW_TAG_subroutine_type:
72019c9c 10650 case DW_TAG_set_type:
c906108c 10651 case DW_TAG_array_type:
c906108c 10652 case DW_TAG_pointer_type:
c906108c 10653 case DW_TAG_ptr_to_member_type:
c906108c 10654 case DW_TAG_reference_type:
4297a3f0 10655 case DW_TAG_rvalue_reference_type:
c906108c 10656 case DW_TAG_string_type:
c906108c 10657 break;
134d01f1 10658
c906108c 10659 case DW_TAG_base_type:
a02abb62 10660 case DW_TAG_subrange_type:
cb249c71 10661 case DW_TAG_typedef:
134d01f1
DJ
10662 /* Add a typedef symbol for the type definition, if it has a
10663 DW_AT_name. */
f792889a 10664 new_symbol (die, read_type_die (die, cu), cu);
a02abb62 10665 break;
c906108c 10666 case DW_TAG_common_block:
e7c27a73 10667 read_common_block (die, cu);
c906108c
SS
10668 break;
10669 case DW_TAG_common_inclusion:
10670 break;
d9fa45fe 10671 case DW_TAG_namespace:
4d4ec4e5 10672 cu->processing_has_namespace_info = 1;
e7c27a73 10673 read_namespace (die, cu);
d9fa45fe 10674 break;
5d7cb8df 10675 case DW_TAG_module:
4d4ec4e5 10676 cu->processing_has_namespace_info = 1;
5d7cb8df
JK
10677 read_module (die, cu);
10678 break;
d9fa45fe 10679 case DW_TAG_imported_declaration:
74921315
KS
10680 cu->processing_has_namespace_info = 1;
10681 if (read_namespace_alias (die, cu))
10682 break;
10683 /* The declaration is not a global namespace alias: fall through. */
d9fa45fe 10684 case DW_TAG_imported_module:
4d4ec4e5 10685 cu->processing_has_namespace_info = 1;
27aa8d6a
SW
10686 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
10687 || cu->language != language_fortran))
10688 complaint (&symfile_complaints, _("Tag '%s' has unexpected children"),
10689 dwarf_tag_name (die->tag));
10690 read_import_statement (die, cu);
d9fa45fe 10691 break;
95554aad
TT
10692
10693 case DW_TAG_imported_unit:
10694 process_imported_unit_die (die, cu);
10695 break;
10696
71a3c369
TT
10697 case DW_TAG_variable:
10698 read_variable (die, cu);
10699 break;
10700
c906108c 10701 default:
e7c27a73 10702 new_symbol (die, NULL, cu);
c906108c
SS
10703 break;
10704 }
10705}
ca69b9e6
DE
10706\f
10707/* DWARF name computation. */
c906108c 10708
94af9270
KS
10709/* A helper function for dwarf2_compute_name which determines whether DIE
10710 needs to have the name of the scope prepended to the name listed in the
10711 die. */
10712
10713static int
10714die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
10715{
1c809c68
TT
10716 struct attribute *attr;
10717
94af9270
KS
10718 switch (die->tag)
10719 {
10720 case DW_TAG_namespace:
10721 case DW_TAG_typedef:
10722 case DW_TAG_class_type:
10723 case DW_TAG_interface_type:
10724 case DW_TAG_structure_type:
10725 case DW_TAG_union_type:
10726 case DW_TAG_enumeration_type:
10727 case DW_TAG_enumerator:
10728 case DW_TAG_subprogram:
08a76f8a 10729 case DW_TAG_inlined_subroutine:
94af9270 10730 case DW_TAG_member:
74921315 10731 case DW_TAG_imported_declaration:
94af9270
KS
10732 return 1;
10733
10734 case DW_TAG_variable:
c2b0a229 10735 case DW_TAG_constant:
94af9270
KS
10736 /* We only need to prefix "globally" visible variables. These include
10737 any variable marked with DW_AT_external or any variable that
10738 lives in a namespace. [Variables in anonymous namespaces
10739 require prefixing, but they are not DW_AT_external.] */
10740
10741 if (dwarf2_attr (die, DW_AT_specification, cu))
10742 {
10743 struct dwarf2_cu *spec_cu = cu;
9a619af0 10744
94af9270
KS
10745 return die_needs_namespace (die_specification (die, &spec_cu),
10746 spec_cu);
10747 }
10748
1c809c68 10749 attr = dwarf2_attr (die, DW_AT_external, cu);
f55ee35c
JK
10750 if (attr == NULL && die->parent->tag != DW_TAG_namespace
10751 && die->parent->tag != DW_TAG_module)
1c809c68
TT
10752 return 0;
10753 /* A variable in a lexical block of some kind does not need a
10754 namespace, even though in C++ such variables may be external
10755 and have a mangled name. */
10756 if (die->parent->tag == DW_TAG_lexical_block
10757 || die->parent->tag == DW_TAG_try_block
1054b214
TT
10758 || die->parent->tag == DW_TAG_catch_block
10759 || die->parent->tag == DW_TAG_subprogram)
1c809c68
TT
10760 return 0;
10761 return 1;
94af9270
KS
10762
10763 default:
10764 return 0;
10765 }
10766}
10767
73b9be8b
KS
10768/* Return the DIE's linkage name attribute, either DW_AT_linkage_name
10769 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10770 defined for the given DIE. */
10771
10772static struct attribute *
10773dw2_linkage_name_attr (struct die_info *die, struct dwarf2_cu *cu)
10774{
10775 struct attribute *attr;
10776
10777 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
10778 if (attr == NULL)
10779 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
10780
10781 return attr;
10782}
10783
10784/* Return the DIE's linkage name as a string, either DW_AT_linkage_name
10785 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10786 defined for the given DIE. */
10787
10788static const char *
10789dw2_linkage_name (struct die_info *die, struct dwarf2_cu *cu)
10790{
10791 const char *linkage_name;
10792
10793 linkage_name = dwarf2_string_attr (die, DW_AT_linkage_name, cu);
10794 if (linkage_name == NULL)
10795 linkage_name = dwarf2_string_attr (die, DW_AT_MIPS_linkage_name, cu);
10796
10797 return linkage_name;
10798}
10799
94af9270 10800/* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
a766d390 10801 compute the physname for the object, which include a method's:
9c37b5ae 10802 - formal parameters (C++),
a766d390 10803 - receiver type (Go),
a766d390
DE
10804
10805 The term "physname" is a bit confusing.
10806 For C++, for example, it is the demangled name.
10807 For Go, for example, it's the mangled name.
94af9270 10808
af6b7be1
JB
10809 For Ada, return the DIE's linkage name rather than the fully qualified
10810 name. PHYSNAME is ignored..
10811
94af9270
KS
10812 The result is allocated on the objfile_obstack and canonicalized. */
10813
10814static const char *
15d034d0
TT
10815dwarf2_compute_name (const char *name,
10816 struct die_info *die, struct dwarf2_cu *cu,
94af9270
KS
10817 int physname)
10818{
518817b3 10819 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
bb5ed363 10820
94af9270
KS
10821 if (name == NULL)
10822 name = dwarf2_name (die, cu);
10823
2ee7123e
DE
10824 /* For Fortran GDB prefers DW_AT_*linkage_name for the physname if present
10825 but otherwise compute it by typename_concat inside GDB.
10826 FIXME: Actually this is not really true, or at least not always true.
10827 It's all very confusing. SYMBOL_SET_NAMES doesn't try to demangle
5e2db402 10828 Fortran names because there is no mangling standard. So new_symbol
2ee7123e
DE
10829 will set the demangled name to the result of dwarf2_full_name, and it is
10830 the demangled name that GDB uses if it exists. */
f55ee35c
JK
10831 if (cu->language == language_ada
10832 || (cu->language == language_fortran && physname))
10833 {
10834 /* For Ada unit, we prefer the linkage name over the name, as
10835 the former contains the exported name, which the user expects
10836 to be able to reference. Ideally, we want the user to be able
10837 to reference this entity using either natural or linkage name,
10838 but we haven't started looking at this enhancement yet. */
73b9be8b 10839 const char *linkage_name = dw2_linkage_name (die, cu);
f55ee35c 10840
2ee7123e
DE
10841 if (linkage_name != NULL)
10842 return linkage_name;
f55ee35c
JK
10843 }
10844
94af9270
KS
10845 /* These are the only languages we know how to qualify names in. */
10846 if (name != NULL
9c37b5ae 10847 && (cu->language == language_cplus
c44af4eb
TT
10848 || cu->language == language_fortran || cu->language == language_d
10849 || cu->language == language_rust))
94af9270
KS
10850 {
10851 if (die_needs_namespace (die, cu))
10852 {
0d5cff50 10853 const char *prefix;
34a68019 10854 const char *canonical_name = NULL;
94af9270 10855
d7e74731
PA
10856 string_file buf;
10857
94af9270 10858 prefix = determine_prefix (die, cu);
94af9270
KS
10859 if (*prefix != '\0')
10860 {
f55ee35c
JK
10861 char *prefixed_name = typename_concat (NULL, prefix, name,
10862 physname, cu);
9a619af0 10863
d7e74731 10864 buf.puts (prefixed_name);
94af9270
KS
10865 xfree (prefixed_name);
10866 }
10867 else
d7e74731 10868 buf.puts (name);
94af9270 10869
98bfdba5
PA
10870 /* Template parameters may be specified in the DIE's DW_AT_name, or
10871 as children with DW_TAG_template_type_param or
10872 DW_TAG_value_type_param. If the latter, add them to the name
10873 here. If the name already has template parameters, then
10874 skip this step; some versions of GCC emit both, and
10875 it is more efficient to use the pre-computed name.
10876
10877 Something to keep in mind about this process: it is very
10878 unlikely, or in some cases downright impossible, to produce
10879 something that will match the mangled name of a function.
10880 If the definition of the function has the same debug info,
10881 we should be able to match up with it anyway. But fallbacks
10882 using the minimal symbol, for instance to find a method
10883 implemented in a stripped copy of libstdc++, will not work.
10884 If we do not have debug info for the definition, we will have to
10885 match them up some other way.
10886
10887 When we do name matching there is a related problem with function
10888 templates; two instantiated function templates are allowed to
10889 differ only by their return types, which we do not add here. */
10890
10891 if (cu->language == language_cplus && strchr (name, '<') == NULL)
10892 {
10893 struct attribute *attr;
10894 struct die_info *child;
10895 int first = 1;
10896
10897 die->building_fullname = 1;
10898
10899 for (child = die->child; child != NULL; child = child->sibling)
10900 {
10901 struct type *type;
12df843f 10902 LONGEST value;
d521ce57 10903 const gdb_byte *bytes;
98bfdba5
PA
10904 struct dwarf2_locexpr_baton *baton;
10905 struct value *v;
10906
10907 if (child->tag != DW_TAG_template_type_param
10908 && child->tag != DW_TAG_template_value_param)
10909 continue;
10910
10911 if (first)
10912 {
d7e74731 10913 buf.puts ("<");
98bfdba5
PA
10914 first = 0;
10915 }
10916 else
d7e74731 10917 buf.puts (", ");
98bfdba5
PA
10918
10919 attr = dwarf2_attr (child, DW_AT_type, cu);
10920 if (attr == NULL)
10921 {
10922 complaint (&symfile_complaints,
10923 _("template parameter missing DW_AT_type"));
d7e74731 10924 buf.puts ("UNKNOWN_TYPE");
98bfdba5
PA
10925 continue;
10926 }
10927 type = die_type (child, cu);
10928
10929 if (child->tag == DW_TAG_template_type_param)
10930 {
d7e74731 10931 c_print_type (type, "", &buf, -1, 0, &type_print_raw_options);
98bfdba5
PA
10932 continue;
10933 }
10934
10935 attr = dwarf2_attr (child, DW_AT_const_value, cu);
10936 if (attr == NULL)
10937 {
10938 complaint (&symfile_complaints,
3e43a32a
MS
10939 _("template parameter missing "
10940 "DW_AT_const_value"));
d7e74731 10941 buf.puts ("UNKNOWN_VALUE");
98bfdba5
PA
10942 continue;
10943 }
10944
10945 dwarf2_const_value_attr (attr, type, name,
10946 &cu->comp_unit_obstack, cu,
10947 &value, &bytes, &baton);
10948
10949 if (TYPE_NOSIGN (type))
10950 /* GDB prints characters as NUMBER 'CHAR'. If that's
10951 changed, this can use value_print instead. */
d7e74731 10952 c_printchar (value, type, &buf);
98bfdba5
PA
10953 else
10954 {
10955 struct value_print_options opts;
10956
10957 if (baton != NULL)
10958 v = dwarf2_evaluate_loc_desc (type, NULL,
10959 baton->data,
10960 baton->size,
10961 baton->per_cu);
10962 else if (bytes != NULL)
10963 {
10964 v = allocate_value (type);
10965 memcpy (value_contents_writeable (v), bytes,
10966 TYPE_LENGTH (type));
10967 }
10968 else
10969 v = value_from_longest (type, value);
10970
3e43a32a
MS
10971 /* Specify decimal so that we do not depend on
10972 the radix. */
98bfdba5
PA
10973 get_formatted_print_options (&opts, 'd');
10974 opts.raw = 1;
d7e74731 10975 value_print (v, &buf, &opts);
98bfdba5 10976 release_value (v);
98bfdba5
PA
10977 }
10978 }
10979
10980 die->building_fullname = 0;
10981
10982 if (!first)
10983 {
10984 /* Close the argument list, with a space if necessary
10985 (nested templates). */
d7e74731
PA
10986 if (!buf.empty () && buf.string ().back () == '>')
10987 buf.puts (" >");
98bfdba5 10988 else
d7e74731 10989 buf.puts (">");
98bfdba5
PA
10990 }
10991 }
10992
9c37b5ae 10993 /* For C++ methods, append formal parameter type
94af9270 10994 information, if PHYSNAME. */
6e70227d 10995
94af9270 10996 if (physname && die->tag == DW_TAG_subprogram
9c37b5ae 10997 && cu->language == language_cplus)
94af9270
KS
10998 {
10999 struct type *type = read_type_die (die, cu);
11000
d7e74731 11001 c_type_print_args (type, &buf, 1, cu->language,
79d43c61 11002 &type_print_raw_options);
94af9270 11003
9c37b5ae 11004 if (cu->language == language_cplus)
94af9270 11005 {
60430eff
DJ
11006 /* Assume that an artificial first parameter is
11007 "this", but do not crash if it is not. RealView
11008 marks unnamed (and thus unused) parameters as
11009 artificial; there is no way to differentiate
11010 the two cases. */
94af9270
KS
11011 if (TYPE_NFIELDS (type) > 0
11012 && TYPE_FIELD_ARTIFICIAL (type, 0)
60430eff 11013 && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
3e43a32a
MS
11014 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
11015 0))))
d7e74731 11016 buf.puts (" const");
94af9270
KS
11017 }
11018 }
11019
d7e74731 11020 const std::string &intermediate_name = buf.string ();
94af9270
KS
11021
11022 if (cu->language == language_cplus)
34a68019 11023 canonical_name
322a8516 11024 = dwarf2_canonicalize_name (intermediate_name.c_str (), cu,
34a68019
TT
11025 &objfile->per_bfd->storage_obstack);
11026
11027 /* If we only computed INTERMEDIATE_NAME, or if
11028 INTERMEDIATE_NAME is already canonical, then we need to
11029 copy it to the appropriate obstack. */
322a8516 11030 if (canonical_name == NULL || canonical_name == intermediate_name.c_str ())
224c3ddb
SM
11031 name = ((const char *)
11032 obstack_copy0 (&objfile->per_bfd->storage_obstack,
322a8516
PA
11033 intermediate_name.c_str (),
11034 intermediate_name.length ()));
34a68019
TT
11035 else
11036 name = canonical_name;
94af9270
KS
11037 }
11038 }
11039
11040 return name;
11041}
11042
0114d602
DJ
11043/* Return the fully qualified name of DIE, based on its DW_AT_name.
11044 If scope qualifiers are appropriate they will be added. The result
34a68019 11045 will be allocated on the storage_obstack, or NULL if the DIE does
94af9270
KS
11046 not have a name. NAME may either be from a previous call to
11047 dwarf2_name or NULL.
11048
9c37b5ae 11049 The output string will be canonicalized (if C++). */
0114d602
DJ
11050
11051static const char *
15d034d0 11052dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
0114d602 11053{
94af9270
KS
11054 return dwarf2_compute_name (name, die, cu, 0);
11055}
0114d602 11056
94af9270
KS
11057/* Construct a physname for the given DIE in CU. NAME may either be
11058 from a previous call to dwarf2_name or NULL. The result will be
11059 allocated on the objfile_objstack or NULL if the DIE does not have a
11060 name.
0114d602 11061
9c37b5ae 11062 The output string will be canonicalized (if C++). */
0114d602 11063
94af9270 11064static const char *
15d034d0 11065dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
94af9270 11066{
518817b3 11067 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
900e11f9 11068 const char *retval, *mangled = NULL, *canon = NULL;
900e11f9
JK
11069 int need_copy = 1;
11070
11071 /* In this case dwarf2_compute_name is just a shortcut not building anything
11072 on its own. */
11073 if (!die_needs_namespace (die, cu))
11074 return dwarf2_compute_name (name, die, cu, 1);
11075
73b9be8b 11076 mangled = dw2_linkage_name (die, cu);
900e11f9 11077
e98c9e7c
TT
11078 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
11079 See https://github.com/rust-lang/rust/issues/32925. */
11080 if (cu->language == language_rust && mangled != NULL
11081 && strchr (mangled, '{') != NULL)
11082 mangled = NULL;
11083
900e11f9
JK
11084 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
11085 has computed. */
791afaa2 11086 gdb::unique_xmalloc_ptr<char> demangled;
7d45c7c3 11087 if (mangled != NULL)
900e11f9 11088 {
900e11f9 11089
59cc4834
JB
11090 if (language_def (cu->language)->la_store_sym_names_in_linkage_form_p)
11091 {
11092 /* Do nothing (do not demangle the symbol name). */
11093 }
11094 else if (cu->language == language_go)
a766d390 11095 {
5e2db402
TT
11096 /* This is a lie, but we already lie to the caller new_symbol.
11097 new_symbol assumes we return the mangled name.
a766d390 11098 This just undoes that lie until things are cleaned up. */
a766d390
DE
11099 }
11100 else
11101 {
0eb876f5
JB
11102 /* Use DMGL_RET_DROP for C++ template functions to suppress
11103 their return type. It is easier for GDB users to search
11104 for such functions as `name(params)' than `long name(params)'.
11105 In such case the minimal symbol names do not match the full
11106 symbol names but for template functions there is never a need
11107 to look up their definition from their declaration so
11108 the only disadvantage remains the minimal symbol variant
11109 `long name(params)' does not have the proper inferior type. */
791afaa2
TT
11110 demangled.reset (gdb_demangle (mangled,
11111 (DMGL_PARAMS | DMGL_ANSI
11112 | DMGL_RET_DROP)));
a766d390 11113 }
900e11f9 11114 if (demangled)
791afaa2 11115 canon = demangled.get ();
900e11f9
JK
11116 else
11117 {
11118 canon = mangled;
11119 need_copy = 0;
11120 }
11121 }
11122
11123 if (canon == NULL || check_physname)
11124 {
11125 const char *physname = dwarf2_compute_name (name, die, cu, 1);
11126
11127 if (canon != NULL && strcmp (physname, canon) != 0)
11128 {
11129 /* It may not mean a bug in GDB. The compiler could also
11130 compute DW_AT_linkage_name incorrectly. But in such case
11131 GDB would need to be bug-to-bug compatible. */
11132
11133 complaint (&symfile_complaints,
11134 _("Computed physname <%s> does not match demangled <%s> "
9d8780f0
SM
11135 "(from linkage <%s>) - DIE at %s [in module %s]"),
11136 physname, canon, mangled, sect_offset_str (die->sect_off),
4262abfb 11137 objfile_name (objfile));
900e11f9
JK
11138
11139 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
11140 is available here - over computed PHYSNAME. It is safer
11141 against both buggy GDB and buggy compilers. */
11142
11143 retval = canon;
11144 }
11145 else
11146 {
11147 retval = physname;
11148 need_copy = 0;
11149 }
11150 }
11151 else
11152 retval = canon;
11153
11154 if (need_copy)
224c3ddb
SM
11155 retval = ((const char *)
11156 obstack_copy0 (&objfile->per_bfd->storage_obstack,
11157 retval, strlen (retval)));
900e11f9 11158
900e11f9 11159 return retval;
0114d602
DJ
11160}
11161
74921315
KS
11162/* Inspect DIE in CU for a namespace alias. If one exists, record
11163 a new symbol for it.
11164
11165 Returns 1 if a namespace alias was recorded, 0 otherwise. */
11166
11167static int
11168read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu)
11169{
11170 struct attribute *attr;
11171
11172 /* If the die does not have a name, this is not a namespace
11173 alias. */
11174 attr = dwarf2_attr (die, DW_AT_name, cu);
11175 if (attr != NULL)
11176 {
11177 int num;
11178 struct die_info *d = die;
11179 struct dwarf2_cu *imported_cu = cu;
11180
11181 /* If the compiler has nested DW_AT_imported_declaration DIEs,
11182 keep inspecting DIEs until we hit the underlying import. */
11183#define MAX_NESTED_IMPORTED_DECLARATIONS 100
11184 for (num = 0; num < MAX_NESTED_IMPORTED_DECLARATIONS; ++num)
11185 {
11186 attr = dwarf2_attr (d, DW_AT_import, cu);
11187 if (attr == NULL)
11188 break;
11189
11190 d = follow_die_ref (d, attr, &imported_cu);
11191 if (d->tag != DW_TAG_imported_declaration)
11192 break;
11193 }
11194
11195 if (num == MAX_NESTED_IMPORTED_DECLARATIONS)
11196 {
11197 complaint (&symfile_complaints,
9d8780f0
SM
11198 _("DIE at %s has too many recursively imported "
11199 "declarations"), sect_offset_str (d->sect_off));
74921315
KS
11200 return 0;
11201 }
11202
11203 if (attr != NULL)
11204 {
11205 struct type *type;
9c541725 11206 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
74921315 11207
9c541725 11208 type = get_die_type_at_offset (sect_off, cu->per_cu);
74921315
KS
11209 if (type != NULL && TYPE_CODE (type) == TYPE_CODE_NAMESPACE)
11210 {
11211 /* This declaration is a global namespace alias. Add
11212 a symbol for it whose type is the aliased namespace. */
11213 new_symbol (die, type, cu);
11214 return 1;
11215 }
11216 }
11217 }
11218
11219 return 0;
11220}
11221
22cee43f
PMR
11222/* Return the using directives repository (global or local?) to use in the
11223 current context for LANGUAGE.
11224
11225 For Ada, imported declarations can materialize renamings, which *may* be
11226 global. However it is impossible (for now?) in DWARF to distinguish
11227 "external" imported declarations and "static" ones. As all imported
11228 declarations seem to be static in all other languages, make them all CU-wide
11229 global only in Ada. */
11230
11231static struct using_direct **
11232using_directives (enum language language)
11233{
11234 if (language == language_ada && context_stack_depth == 0)
11235 return &global_using_directives;
11236 else
11237 return &local_using_directives;
11238}
11239
27aa8d6a
SW
11240/* Read the import statement specified by the given die and record it. */
11241
11242static void
11243read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
11244{
518817b3 11245 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
27aa8d6a 11246 struct attribute *import_attr;
32019081 11247 struct die_info *imported_die, *child_die;
de4affc9 11248 struct dwarf2_cu *imported_cu;
27aa8d6a 11249 const char *imported_name;
794684b6 11250 const char *imported_name_prefix;
13387711
SW
11251 const char *canonical_name;
11252 const char *import_alias;
11253 const char *imported_declaration = NULL;
794684b6 11254 const char *import_prefix;
eb1e02fd 11255 std::vector<const char *> excludes;
13387711 11256
27aa8d6a
SW
11257 import_attr = dwarf2_attr (die, DW_AT_import, cu);
11258 if (import_attr == NULL)
11259 {
11260 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
11261 dwarf_tag_name (die->tag));
11262 return;
11263 }
11264
de4affc9
CC
11265 imported_cu = cu;
11266 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
11267 imported_name = dwarf2_name (imported_die, imported_cu);
27aa8d6a
SW
11268 if (imported_name == NULL)
11269 {
11270 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
11271
11272 The import in the following code:
11273 namespace A
11274 {
11275 typedef int B;
11276 }
11277
11278 int main ()
11279 {
11280 using A::B;
11281 B b;
11282 return b;
11283 }
11284
11285 ...
11286 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
11287 <52> DW_AT_decl_file : 1
11288 <53> DW_AT_decl_line : 6
11289 <54> DW_AT_import : <0x75>
11290 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
11291 <59> DW_AT_name : B
11292 <5b> DW_AT_decl_file : 1
11293 <5c> DW_AT_decl_line : 2
11294 <5d> DW_AT_type : <0x6e>
11295 ...
11296 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
11297 <76> DW_AT_byte_size : 4
11298 <77> DW_AT_encoding : 5 (signed)
11299
11300 imports the wrong die ( 0x75 instead of 0x58 ).
11301 This case will be ignored until the gcc bug is fixed. */
11302 return;
11303 }
11304
82856980
SW
11305 /* Figure out the local name after import. */
11306 import_alias = dwarf2_name (die, cu);
27aa8d6a 11307
794684b6
SW
11308 /* Figure out where the statement is being imported to. */
11309 import_prefix = determine_prefix (die, cu);
11310
11311 /* Figure out what the scope of the imported die is and prepend it
11312 to the name of the imported die. */
de4affc9 11313 imported_name_prefix = determine_prefix (imported_die, imported_cu);
794684b6 11314
f55ee35c
JK
11315 if (imported_die->tag != DW_TAG_namespace
11316 && imported_die->tag != DW_TAG_module)
794684b6 11317 {
13387711
SW
11318 imported_declaration = imported_name;
11319 canonical_name = imported_name_prefix;
794684b6 11320 }
13387711 11321 else if (strlen (imported_name_prefix) > 0)
12aaed36 11322 canonical_name = obconcat (&objfile->objfile_obstack,
45280282
IB
11323 imported_name_prefix,
11324 (cu->language == language_d ? "." : "::"),
11325 imported_name, (char *) NULL);
13387711
SW
11326 else
11327 canonical_name = imported_name;
794684b6 11328
32019081
JK
11329 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
11330 for (child_die = die->child; child_die && child_die->tag;
11331 child_die = sibling_die (child_die))
11332 {
11333 /* DWARF-4: A Fortran use statement with a “rename list” may be
11334 represented by an imported module entry with an import attribute
11335 referring to the module and owned entries corresponding to those
11336 entities that are renamed as part of being imported. */
11337
11338 if (child_die->tag != DW_TAG_imported_declaration)
11339 {
11340 complaint (&symfile_complaints,
11341 _("child DW_TAG_imported_declaration expected "
9d8780f0
SM
11342 "- DIE at %s [in module %s]"),
11343 sect_offset_str (child_die->sect_off),
11344 objfile_name (objfile));
32019081
JK
11345 continue;
11346 }
11347
11348 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
11349 if (import_attr == NULL)
11350 {
11351 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
11352 dwarf_tag_name (child_die->tag));
11353 continue;
11354 }
11355
11356 imported_cu = cu;
11357 imported_die = follow_die_ref_or_sig (child_die, import_attr,
11358 &imported_cu);
11359 imported_name = dwarf2_name (imported_die, imported_cu);
11360 if (imported_name == NULL)
11361 {
11362 complaint (&symfile_complaints,
11363 _("child DW_TAG_imported_declaration has unknown "
9d8780f0
SM
11364 "imported name - DIE at %s [in module %s]"),
11365 sect_offset_str (child_die->sect_off),
11366 objfile_name (objfile));
32019081
JK
11367 continue;
11368 }
11369
eb1e02fd 11370 excludes.push_back (imported_name);
32019081
JK
11371
11372 process_die (child_die, cu);
11373 }
11374
22cee43f
PMR
11375 add_using_directive (using_directives (cu->language),
11376 import_prefix,
11377 canonical_name,
11378 import_alias,
11379 imported_declaration,
11380 excludes,
11381 0,
11382 &objfile->objfile_obstack);
27aa8d6a
SW
11383}
11384
5230b05a
WT
11385/* ICC<14 does not output the required DW_AT_declaration on incomplete
11386 types, but gives them a size of zero. Starting with version 14,
11387 ICC is compatible with GCC. */
11388
11389static int
11390producer_is_icc_lt_14 (struct dwarf2_cu *cu)
11391{
11392 if (!cu->checked_producer)
11393 check_producer (cu);
11394
11395 return cu->producer_is_icc_lt_14;
11396}
11397
1b80a9fa
JK
11398/* Check for possibly missing DW_AT_comp_dir with relative .debug_line
11399 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
11400 this, it was first present in GCC release 4.3.0. */
11401
11402static int
11403producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
11404{
11405 if (!cu->checked_producer)
11406 check_producer (cu);
11407
11408 return cu->producer_is_gcc_lt_4_3;
11409}
11410
d721ba37
PA
11411static file_and_directory
11412find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu)
9291a0cd 11413{
d721ba37
PA
11414 file_and_directory res;
11415
9291a0cd
TT
11416 /* Find the filename. Do not use dwarf2_name here, since the filename
11417 is not a source language identifier. */
d721ba37
PA
11418 res.name = dwarf2_string_attr (die, DW_AT_name, cu);
11419 res.comp_dir = dwarf2_string_attr (die, DW_AT_comp_dir, cu);
9291a0cd 11420
d721ba37
PA
11421 if (res.comp_dir == NULL
11422 && producer_is_gcc_lt_4_3 (cu) && res.name != NULL
11423 && IS_ABSOLUTE_PATH (res.name))
9291a0cd 11424 {
d721ba37
PA
11425 res.comp_dir_storage = ldirname (res.name);
11426 if (!res.comp_dir_storage.empty ())
11427 res.comp_dir = res.comp_dir_storage.c_str ();
9291a0cd 11428 }
d721ba37 11429 if (res.comp_dir != NULL)
9291a0cd
TT
11430 {
11431 /* Irix 6.2 native cc prepends <machine>.: to the compilation
11432 directory, get rid of it. */
d721ba37 11433 const char *cp = strchr (res.comp_dir, ':');
9291a0cd 11434
d721ba37
PA
11435 if (cp && cp != res.comp_dir && cp[-1] == '.' && cp[1] == '/')
11436 res.comp_dir = cp + 1;
9291a0cd
TT
11437 }
11438
d721ba37
PA
11439 if (res.name == NULL)
11440 res.name = "<unknown>";
11441
11442 return res;
9291a0cd
TT
11443}
11444
f4dc4d17
DE
11445/* Handle DW_AT_stmt_list for a compilation unit.
11446 DIE is the DW_TAG_compile_unit die for CU.
c3b7b696
YQ
11447 COMP_DIR is the compilation directory. LOWPC is passed to
11448 dwarf_decode_lines. See dwarf_decode_lines comments about it. */
2ab95328
TT
11449
11450static void
11451handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
c3b7b696 11452 const char *comp_dir, CORE_ADDR lowpc) /* ARI: editCase function */
2ab95328 11453{
518817b3
SM
11454 struct dwarf2_per_objfile *dwarf2_per_objfile
11455 = cu->per_cu->dwarf2_per_objfile;
527f3840 11456 struct objfile *objfile = dwarf2_per_objfile->objfile;
2ab95328 11457 struct attribute *attr;
527f3840
JK
11458 struct line_header line_header_local;
11459 hashval_t line_header_local_hash;
527f3840
JK
11460 void **slot;
11461 int decode_mapping;
2ab95328 11462
f4dc4d17
DE
11463 gdb_assert (! cu->per_cu->is_debug_types);
11464
2ab95328 11465 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
527f3840
JK
11466 if (attr == NULL)
11467 return;
11468
9c541725 11469 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
527f3840
JK
11470
11471 /* The line header hash table is only created if needed (it exists to
11472 prevent redundant reading of the line table for partial_units).
11473 If we're given a partial_unit, we'll need it. If we're given a
11474 compile_unit, then use the line header hash table if it's already
11475 created, but don't create one just yet. */
11476
11477 if (dwarf2_per_objfile->line_header_hash == NULL
11478 && die->tag == DW_TAG_partial_unit)
2ab95328 11479 {
527f3840
JK
11480 dwarf2_per_objfile->line_header_hash
11481 = htab_create_alloc_ex (127, line_header_hash_voidp,
11482 line_header_eq_voidp,
11483 free_line_header_voidp,
11484 &objfile->objfile_obstack,
11485 hashtab_obstack_allocate,
11486 dummy_obstack_deallocate);
11487 }
2ab95328 11488
9c541725 11489 line_header_local.sect_off = line_offset;
527f3840
JK
11490 line_header_local.offset_in_dwz = cu->per_cu->is_dwz;
11491 line_header_local_hash = line_header_hash (&line_header_local);
11492 if (dwarf2_per_objfile->line_header_hash != NULL)
11493 {
11494 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash,
11495 &line_header_local,
11496 line_header_local_hash, NO_INSERT);
11497
11498 /* For DW_TAG_compile_unit we need info like symtab::linetable which
11499 is not present in *SLOT (since if there is something in *SLOT then
11500 it will be for a partial_unit). */
11501 if (die->tag == DW_TAG_partial_unit && slot != NULL)
dee91e82 11502 {
527f3840 11503 gdb_assert (*slot != NULL);
9a3c8263 11504 cu->line_header = (struct line_header *) *slot;
527f3840 11505 return;
dee91e82 11506 }
2ab95328 11507 }
527f3840
JK
11508
11509 /* dwarf_decode_line_header does not yet provide sufficient information.
11510 We always have to call also dwarf_decode_lines for it. */
fff8551c
PA
11511 line_header_up lh = dwarf_decode_line_header (line_offset, cu);
11512 if (lh == NULL)
527f3840 11513 return;
4c8aa72d
PA
11514
11515 cu->line_header = lh.release ();
11516 cu->line_header_die_owner = die;
527f3840
JK
11517
11518 if (dwarf2_per_objfile->line_header_hash == NULL)
11519 slot = NULL;
11520 else
11521 {
11522 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash,
11523 &line_header_local,
11524 line_header_local_hash, INSERT);
11525 gdb_assert (slot != NULL);
11526 }
11527 if (slot != NULL && *slot == NULL)
11528 {
11529 /* This newly decoded line number information unit will be owned
11530 by line_header_hash hash table. */
11531 *slot = cu->line_header;
4c8aa72d 11532 cu->line_header_die_owner = NULL;
527f3840
JK
11533 }
11534 else
11535 {
11536 /* We cannot free any current entry in (*slot) as that struct line_header
11537 may be already used by multiple CUs. Create only temporary decoded
11538 line_header for this CU - it may happen at most once for each line
11539 number information unit. And if we're not using line_header_hash
11540 then this is what we want as well. */
11541 gdb_assert (die->tag != DW_TAG_partial_unit);
527f3840
JK
11542 }
11543 decode_mapping = (die->tag != DW_TAG_partial_unit);
11544 dwarf_decode_lines (cu->line_header, comp_dir, cu, NULL, lowpc,
11545 decode_mapping);
fff8551c 11546
2ab95328
TT
11547}
11548
95554aad 11549/* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
ae2de4f8 11550
c906108c 11551static void
e7c27a73 11552read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 11553{
518817b3
SM
11554 struct dwarf2_per_objfile *dwarf2_per_objfile
11555 = cu->per_cu->dwarf2_per_objfile;
dee91e82 11556 struct objfile *objfile = dwarf2_per_objfile->objfile;
3e29f34a 11557 struct gdbarch *gdbarch = get_objfile_arch (objfile);
2acceee2 11558 CORE_ADDR lowpc = ((CORE_ADDR) -1);
c906108c
SS
11559 CORE_ADDR highpc = ((CORE_ADDR) 0);
11560 struct attribute *attr;
c906108c 11561 struct die_info *child_die;
e142c38c 11562 CORE_ADDR baseaddr;
6e70227d 11563
e142c38c 11564 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 11565
fae299cd 11566 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
c906108c
SS
11567
11568 /* If we didn't find a lowpc, set it to highpc to avoid complaints
11569 from finish_block. */
2acceee2 11570 if (lowpc == ((CORE_ADDR) -1))
c906108c 11571 lowpc = highpc;
3e29f34a 11572 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
c906108c 11573
d721ba37 11574 file_and_directory fnd = find_file_and_directory (die, cu);
e1024ff1 11575
95554aad 11576 prepare_one_comp_unit (cu, die, cu->language);
303b6f5d 11577
f4b8a18d
KW
11578 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
11579 standardised yet. As a workaround for the language detection we fall
11580 back to the DW_AT_producer string. */
11581 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
11582 cu->language = language_opencl;
11583
3019eac3
DE
11584 /* Similar hack for Go. */
11585 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
11586 set_cu_language (DW_LANG_Go, cu);
11587
d721ba37 11588 dwarf2_start_symtab (cu, fnd.name, fnd.comp_dir, lowpc);
3019eac3
DE
11589
11590 /* Decode line number information if present. We do this before
11591 processing child DIEs, so that the line header table is available
11592 for DW_AT_decl_file. */
d721ba37 11593 handle_DW_AT_stmt_list (die, cu, fnd.comp_dir, lowpc);
3019eac3
DE
11594
11595 /* Process all dies in compilation unit. */
11596 if (die->child != NULL)
11597 {
11598 child_die = die->child;
11599 while (child_die && child_die->tag)
11600 {
11601 process_die (child_die, cu);
11602 child_die = sibling_die (child_die);
11603 }
11604 }
11605
11606 /* Decode macro information, if present. Dwarf 2 macro information
11607 refers to information in the line number info statement program
11608 header, so we can only read it if we've read the header
11609 successfully. */
0af92d60
JK
11610 attr = dwarf2_attr (die, DW_AT_macros, cu);
11611 if (attr == NULL)
11612 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
3019eac3
DE
11613 if (attr && cu->line_header)
11614 {
11615 if (dwarf2_attr (die, DW_AT_macro_info, cu))
11616 complaint (&symfile_complaints,
0af92d60 11617 _("CU refers to both DW_AT_macros and DW_AT_macro_info"));
3019eac3 11618
43f3e411 11619 dwarf_decode_macros (cu, DW_UNSND (attr), 1);
3019eac3
DE
11620 }
11621 else
11622 {
11623 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
11624 if (attr && cu->line_header)
11625 {
11626 unsigned int macro_offset = DW_UNSND (attr);
11627
43f3e411 11628 dwarf_decode_macros (cu, macro_offset, 0);
3019eac3
DE
11629 }
11630 }
3019eac3
DE
11631}
11632
f4dc4d17
DE
11633/* TU version of handle_DW_AT_stmt_list for read_type_unit_scope.
11634 Create the set of symtabs used by this TU, or if this TU is sharing
11635 symtabs with another TU and the symtabs have already been created
11636 then restore those symtabs in the line header.
11637 We don't need the pc/line-number mapping for type units. */
3019eac3
DE
11638
11639static void
f4dc4d17 11640setup_type_unit_groups (struct die_info *die, struct dwarf2_cu *cu)
3019eac3 11641{
f4dc4d17
DE
11642 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
11643 struct type_unit_group *tu_group;
11644 int first_time;
3019eac3 11645 struct attribute *attr;
9c541725 11646 unsigned int i;
0186c6a7 11647 struct signatured_type *sig_type;
3019eac3 11648
f4dc4d17 11649 gdb_assert (per_cu->is_debug_types);
0186c6a7 11650 sig_type = (struct signatured_type *) per_cu;
3019eac3 11651
f4dc4d17 11652 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
3019eac3 11653
f4dc4d17 11654 /* If we're using .gdb_index (includes -readnow) then
74e04d1c 11655 per_cu->type_unit_group may not have been set up yet. */
0186c6a7
DE
11656 if (sig_type->type_unit_group == NULL)
11657 sig_type->type_unit_group = get_type_unit_group (cu, attr);
11658 tu_group = sig_type->type_unit_group;
f4dc4d17
DE
11659
11660 /* If we've already processed this stmt_list there's no real need to
11661 do it again, we could fake it and just recreate the part we need
11662 (file name,index -> symtab mapping). If data shows this optimization
11663 is useful we can do it then. */
43f3e411 11664 first_time = tu_group->compunit_symtab == NULL;
f4dc4d17
DE
11665
11666 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
11667 debug info. */
fff8551c 11668 line_header_up lh;
f4dc4d17 11669 if (attr != NULL)
3019eac3 11670 {
9c541725 11671 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
f4dc4d17
DE
11672 lh = dwarf_decode_line_header (line_offset, cu);
11673 }
11674 if (lh == NULL)
11675 {
11676 if (first_time)
11677 dwarf2_start_symtab (cu, "", NULL, 0);
11678 else
11679 {
11680 gdb_assert (tu_group->symtabs == NULL);
0ab9ce85 11681 restart_symtab (tu_group->compunit_symtab, "", 0);
f4dc4d17 11682 }
f4dc4d17 11683 return;
3019eac3
DE
11684 }
11685
4c8aa72d
PA
11686 cu->line_header = lh.release ();
11687 cu->line_header_die_owner = die;
3019eac3 11688
f4dc4d17
DE
11689 if (first_time)
11690 {
43f3e411 11691 struct compunit_symtab *cust = dwarf2_start_symtab (cu, "", NULL, 0);
3019eac3 11692
1fd60fc0
DE
11693 /* Note: We don't assign tu_group->compunit_symtab yet because we're
11694 still initializing it, and our caller (a few levels up)
11695 process_full_type_unit still needs to know if this is the first
11696 time. */
11697
4c8aa72d
PA
11698 tu_group->num_symtabs = cu->line_header->file_names.size ();
11699 tu_group->symtabs = XNEWVEC (struct symtab *,
11700 cu->line_header->file_names.size ());
3019eac3 11701
4c8aa72d 11702 for (i = 0; i < cu->line_header->file_names.size (); ++i)
f4dc4d17 11703 {
4c8aa72d 11704 file_entry &fe = cu->line_header->file_names[i];
3019eac3 11705
4c8aa72d 11706 dwarf2_start_subfile (fe.name, fe.include_dir (cu->line_header));
3019eac3 11707
f4dc4d17
DE
11708 if (current_subfile->symtab == NULL)
11709 {
4c8aa72d
PA
11710 /* NOTE: start_subfile will recognize when it's been
11711 passed a file it has already seen. So we can't
11712 assume there's a simple mapping from
11713 cu->line_header->file_names to subfiles, plus
11714 cu->line_header->file_names may contain dups. */
43f3e411
DE
11715 current_subfile->symtab
11716 = allocate_symtab (cust, current_subfile->name);
f4dc4d17
DE
11717 }
11718
8c43009f
PA
11719 fe.symtab = current_subfile->symtab;
11720 tu_group->symtabs[i] = fe.symtab;
f4dc4d17
DE
11721 }
11722 }
11723 else
3019eac3 11724 {
0ab9ce85 11725 restart_symtab (tu_group->compunit_symtab, "", 0);
f4dc4d17 11726
4c8aa72d 11727 for (i = 0; i < cu->line_header->file_names.size (); ++i)
f4dc4d17 11728 {
4c8aa72d 11729 file_entry &fe = cu->line_header->file_names[i];
f4dc4d17 11730
4c8aa72d 11731 fe.symtab = tu_group->symtabs[i];
f4dc4d17 11732 }
3019eac3
DE
11733 }
11734
f4dc4d17
DE
11735 /* The main symtab is allocated last. Type units don't have DW_AT_name
11736 so they don't have a "real" (so to speak) symtab anyway.
11737 There is later code that will assign the main symtab to all symbols
11738 that don't have one. We need to handle the case of a symbol with a
11739 missing symtab (DW_AT_decl_file) anyway. */
11740}
3019eac3 11741
f4dc4d17
DE
11742/* Process DW_TAG_type_unit.
11743 For TUs we want to skip the first top level sibling if it's not the
11744 actual type being defined by this TU. In this case the first top
11745 level sibling is there to provide context only. */
3019eac3 11746
f4dc4d17
DE
11747static void
11748read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
11749{
11750 struct die_info *child_die;
3019eac3 11751
f4dc4d17
DE
11752 prepare_one_comp_unit (cu, die, language_minimal);
11753
11754 /* Initialize (or reinitialize) the machinery for building symtabs.
11755 We do this before processing child DIEs, so that the line header table
11756 is available for DW_AT_decl_file. */
11757 setup_type_unit_groups (die, cu);
11758
11759 if (die->child != NULL)
11760 {
11761 child_die = die->child;
11762 while (child_die && child_die->tag)
11763 {
11764 process_die (child_die, cu);
11765 child_die = sibling_die (child_die);
11766 }
11767 }
3019eac3
DE
11768}
11769\f
80626a55
DE
11770/* DWO/DWP files.
11771
11772 http://gcc.gnu.org/wiki/DebugFission
11773 http://gcc.gnu.org/wiki/DebugFissionDWP
11774
11775 To simplify handling of both DWO files ("object" files with the DWARF info)
11776 and DWP files (a file with the DWOs packaged up into one file), we treat
11777 DWP files as having a collection of virtual DWO files. */
3019eac3
DE
11778
11779static hashval_t
11780hash_dwo_file (const void *item)
11781{
9a3c8263 11782 const struct dwo_file *dwo_file = (const struct dwo_file *) item;
a2ce51a0 11783 hashval_t hash;
3019eac3 11784
a2ce51a0
DE
11785 hash = htab_hash_string (dwo_file->dwo_name);
11786 if (dwo_file->comp_dir != NULL)
11787 hash += htab_hash_string (dwo_file->comp_dir);
11788 return hash;
3019eac3
DE
11789}
11790
11791static int
11792eq_dwo_file (const void *item_lhs, const void *item_rhs)
11793{
9a3c8263
SM
11794 const struct dwo_file *lhs = (const struct dwo_file *) item_lhs;
11795 const struct dwo_file *rhs = (const struct dwo_file *) item_rhs;
3019eac3 11796
a2ce51a0
DE
11797 if (strcmp (lhs->dwo_name, rhs->dwo_name) != 0)
11798 return 0;
11799 if (lhs->comp_dir == NULL || rhs->comp_dir == NULL)
11800 return lhs->comp_dir == rhs->comp_dir;
11801 return strcmp (lhs->comp_dir, rhs->comp_dir) == 0;
3019eac3
DE
11802}
11803
11804/* Allocate a hash table for DWO files. */
11805
11806static htab_t
ed2dc618 11807allocate_dwo_file_hash_table (struct objfile *objfile)
3019eac3 11808{
3019eac3
DE
11809 return htab_create_alloc_ex (41,
11810 hash_dwo_file,
11811 eq_dwo_file,
11812 NULL,
11813 &objfile->objfile_obstack,
11814 hashtab_obstack_allocate,
11815 dummy_obstack_deallocate);
11816}
11817
80626a55
DE
11818/* Lookup DWO file DWO_NAME. */
11819
11820static void **
ed2dc618
SM
11821lookup_dwo_file_slot (struct dwarf2_per_objfile *dwarf2_per_objfile,
11822 const char *dwo_name,
11823 const char *comp_dir)
80626a55
DE
11824{
11825 struct dwo_file find_entry;
11826 void **slot;
11827
11828 if (dwarf2_per_objfile->dwo_files == NULL)
ed2dc618
SM
11829 dwarf2_per_objfile->dwo_files
11830 = allocate_dwo_file_hash_table (dwarf2_per_objfile->objfile);
80626a55
DE
11831
11832 memset (&find_entry, 0, sizeof (find_entry));
0ac5b59e
DE
11833 find_entry.dwo_name = dwo_name;
11834 find_entry.comp_dir = comp_dir;
80626a55
DE
11835 slot = htab_find_slot (dwarf2_per_objfile->dwo_files, &find_entry, INSERT);
11836
11837 return slot;
11838}
11839
3019eac3
DE
11840static hashval_t
11841hash_dwo_unit (const void *item)
11842{
9a3c8263 11843 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
3019eac3
DE
11844
11845 /* This drops the top 32 bits of the id, but is ok for a hash. */
11846 return dwo_unit->signature;
11847}
11848
11849static int
11850eq_dwo_unit (const void *item_lhs, const void *item_rhs)
11851{
9a3c8263
SM
11852 const struct dwo_unit *lhs = (const struct dwo_unit *) item_lhs;
11853 const struct dwo_unit *rhs = (const struct dwo_unit *) item_rhs;
3019eac3
DE
11854
11855 /* The signature is assumed to be unique within the DWO file.
11856 So while object file CU dwo_id's always have the value zero,
11857 that's OK, assuming each object file DWO file has only one CU,
11858 and that's the rule for now. */
11859 return lhs->signature == rhs->signature;
11860}
11861
11862/* Allocate a hash table for DWO CUs,TUs.
11863 There is one of these tables for each of CUs,TUs for each DWO file. */
11864
11865static htab_t
11866allocate_dwo_unit_table (struct objfile *objfile)
11867{
11868 /* Start out with a pretty small number.
11869 Generally DWO files contain only one CU and maybe some TUs. */
11870 return htab_create_alloc_ex (3,
11871 hash_dwo_unit,
11872 eq_dwo_unit,
11873 NULL,
11874 &objfile->objfile_obstack,
11875 hashtab_obstack_allocate,
11876 dummy_obstack_deallocate);
11877}
11878
80626a55 11879/* Structure used to pass data to create_dwo_debug_info_hash_table_reader. */
3019eac3 11880
19c3d4c9 11881struct create_dwo_cu_data
3019eac3
DE
11882{
11883 struct dwo_file *dwo_file;
19c3d4c9 11884 struct dwo_unit dwo_unit;
3019eac3
DE
11885};
11886
19c3d4c9 11887/* die_reader_func for create_dwo_cu. */
3019eac3
DE
11888
11889static void
19c3d4c9
DE
11890create_dwo_cu_reader (const struct die_reader_specs *reader,
11891 const gdb_byte *info_ptr,
11892 struct die_info *comp_unit_die,
11893 int has_children,
11894 void *datap)
3019eac3
DE
11895{
11896 struct dwarf2_cu *cu = reader->cu;
9c541725 11897 sect_offset sect_off = cu->per_cu->sect_off;
8a0459fd 11898 struct dwarf2_section_info *section = cu->per_cu->section;
9a3c8263 11899 struct create_dwo_cu_data *data = (struct create_dwo_cu_data *) datap;
3019eac3 11900 struct dwo_file *dwo_file = data->dwo_file;
19c3d4c9 11901 struct dwo_unit *dwo_unit = &data->dwo_unit;
3019eac3 11902 struct attribute *attr;
3019eac3
DE
11903
11904 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
11905 if (attr == NULL)
11906 {
19c3d4c9 11907 complaint (&symfile_complaints,
9d8780f0 11908 _("Dwarf Error: debug entry at offset %s is missing"
19c3d4c9 11909 " its dwo_id [in module %s]"),
9d8780f0 11910 sect_offset_str (sect_off), dwo_file->dwo_name);
3019eac3
DE
11911 return;
11912 }
11913
3019eac3
DE
11914 dwo_unit->dwo_file = dwo_file;
11915 dwo_unit->signature = DW_UNSND (attr);
8a0459fd 11916 dwo_unit->section = section;
9c541725 11917 dwo_unit->sect_off = sect_off;
3019eac3
DE
11918 dwo_unit->length = cu->per_cu->length;
11919
b4f54984 11920 if (dwarf_read_debug)
9d8780f0
SM
11921 fprintf_unfiltered (gdb_stdlog, " offset %s, dwo_id %s\n",
11922 sect_offset_str (sect_off),
9c541725 11923 hex_string (dwo_unit->signature));
3019eac3
DE
11924}
11925
33c5cd75 11926/* Create the dwo_units for the CUs in a DWO_FILE.
19c3d4c9 11927 Note: This function processes DWO files only, not DWP files. */
3019eac3 11928
33c5cd75 11929static void
ed2dc618
SM
11930create_cus_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
11931 struct dwo_file &dwo_file, dwarf2_section_info &section,
33c5cd75 11932 htab_t &cus_htab)
3019eac3
DE
11933{
11934 struct objfile *objfile = dwarf2_per_objfile->objfile;
d521ce57 11935 const gdb_byte *info_ptr, *end_ptr;
3019eac3 11936
33c5cd75
DB
11937 dwarf2_read_section (objfile, &section);
11938 info_ptr = section.buffer;
3019eac3
DE
11939
11940 if (info_ptr == NULL)
33c5cd75 11941 return;
3019eac3 11942
b4f54984 11943 if (dwarf_read_debug)
19c3d4c9
DE
11944 {
11945 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
33c5cd75
DB
11946 get_section_name (&section),
11947 get_section_file_name (&section));
19c3d4c9 11948 }
3019eac3 11949
33c5cd75 11950 end_ptr = info_ptr + section.size;
3019eac3
DE
11951 while (info_ptr < end_ptr)
11952 {
11953 struct dwarf2_per_cu_data per_cu;
33c5cd75
DB
11954 struct create_dwo_cu_data create_dwo_cu_data;
11955 struct dwo_unit *dwo_unit;
11956 void **slot;
11957 sect_offset sect_off = (sect_offset) (info_ptr - section.buffer);
3019eac3 11958
19c3d4c9
DE
11959 memset (&create_dwo_cu_data.dwo_unit, 0,
11960 sizeof (create_dwo_cu_data.dwo_unit));
3019eac3 11961 memset (&per_cu, 0, sizeof (per_cu));
e3b94546 11962 per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
3019eac3 11963 per_cu.is_debug_types = 0;
33c5cd75
DB
11964 per_cu.sect_off = sect_offset (info_ptr - section.buffer);
11965 per_cu.section = &section;
c5ed0576 11966 create_dwo_cu_data.dwo_file = &dwo_file;
33c5cd75
DB
11967
11968 init_cutu_and_read_dies_no_follow (
11969 &per_cu, &dwo_file, create_dwo_cu_reader, &create_dwo_cu_data);
11970 info_ptr += per_cu.length;
11971
11972 // If the unit could not be parsed, skip it.
11973 if (create_dwo_cu_data.dwo_unit.dwo_file == NULL)
11974 continue;
3019eac3 11975
33c5cd75
DB
11976 if (cus_htab == NULL)
11977 cus_htab = allocate_dwo_unit_table (objfile);
19c3d4c9 11978
33c5cd75
DB
11979 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
11980 *dwo_unit = create_dwo_cu_data.dwo_unit;
11981 slot = htab_find_slot (cus_htab, dwo_unit, INSERT);
11982 gdb_assert (slot != NULL);
11983 if (*slot != NULL)
19c3d4c9 11984 {
33c5cd75
DB
11985 const struct dwo_unit *dup_cu = (const struct dwo_unit *)*slot;
11986 sect_offset dup_sect_off = dup_cu->sect_off;
19c3d4c9 11987
33c5cd75 11988 complaint (&symfile_complaints,
9d8780f0
SM
11989 _("debug cu entry at offset %s is duplicate to"
11990 " the entry at offset %s, signature %s"),
11991 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
33c5cd75 11992 hex_string (dwo_unit->signature));
19c3d4c9 11993 }
33c5cd75 11994 *slot = (void *)dwo_unit;
3019eac3 11995 }
3019eac3
DE
11996}
11997
80626a55
DE
11998/* DWP file .debug_{cu,tu}_index section format:
11999 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
12000
d2415c6c
DE
12001 DWP Version 1:
12002
80626a55
DE
12003 Both index sections have the same format, and serve to map a 64-bit
12004 signature to a set of section numbers. Each section begins with a header,
12005 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
12006 indexes, and a pool of 32-bit section numbers. The index sections will be
12007 aligned at 8-byte boundaries in the file.
12008
d2415c6c
DE
12009 The index section header consists of:
12010
12011 V, 32 bit version number
12012 -, 32 bits unused
12013 N, 32 bit number of compilation units or type units in the index
12014 M, 32 bit number of slots in the hash table
80626a55 12015
d2415c6c 12016 Numbers are recorded using the byte order of the application binary.
80626a55 12017
d2415c6c
DE
12018 The hash table begins at offset 16 in the section, and consists of an array
12019 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
12020 order of the application binary). Unused slots in the hash table are 0.
12021 (We rely on the extreme unlikeliness of a signature being exactly 0.)
80626a55 12022
d2415c6c
DE
12023 The parallel table begins immediately after the hash table
12024 (at offset 16 + 8 * M from the beginning of the section), and consists of an
12025 array of 32-bit indexes (using the byte order of the application binary),
12026 corresponding 1-1 with slots in the hash table. Each entry in the parallel
12027 table contains a 32-bit index into the pool of section numbers. For unused
12028 hash table slots, the corresponding entry in the parallel table will be 0.
80626a55 12029
73869dc2
DE
12030 The pool of section numbers begins immediately following the hash table
12031 (at offset 16 + 12 * M from the beginning of the section). The pool of
12032 section numbers consists of an array of 32-bit words (using the byte order
12033 of the application binary). Each item in the array is indexed starting
12034 from 0. The hash table entry provides the index of the first section
12035 number in the set. Additional section numbers in the set follow, and the
12036 set is terminated by a 0 entry (section number 0 is not used in ELF).
12037
12038 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
12039 section must be the first entry in the set, and the .debug_abbrev.dwo must
12040 be the second entry. Other members of the set may follow in any order.
12041
12042 ---
12043
12044 DWP Version 2:
12045
12046 DWP Version 2 combines all the .debug_info, etc. sections into one,
12047 and the entries in the index tables are now offsets into these sections.
12048 CU offsets begin at 0. TU offsets begin at the size of the .debug_info
12049 section.
12050
12051 Index Section Contents:
12052 Header
12053 Hash Table of Signatures dwp_hash_table.hash_table
12054 Parallel Table of Indices dwp_hash_table.unit_table
12055 Table of Section Offsets dwp_hash_table.v2.{section_ids,offsets}
12056 Table of Section Sizes dwp_hash_table.v2.sizes
12057
12058 The index section header consists of:
12059
12060 V, 32 bit version number
12061 L, 32 bit number of columns in the table of section offsets
12062 N, 32 bit number of compilation units or type units in the index
12063 M, 32 bit number of slots in the hash table
12064
12065 Numbers are recorded using the byte order of the application binary.
12066
12067 The hash table has the same format as version 1.
12068 The parallel table of indices has the same format as version 1,
12069 except that the entries are origin-1 indices into the table of sections
12070 offsets and the table of section sizes.
12071
12072 The table of offsets begins immediately following the parallel table
12073 (at offset 16 + 12 * M from the beginning of the section). The table is
12074 a two-dimensional array of 32-bit words (using the byte order of the
12075 application binary), with L columns and N+1 rows, in row-major order.
12076 Each row in the array is indexed starting from 0. The first row provides
12077 a key to the remaining rows: each column in this row provides an identifier
12078 for a debug section, and the offsets in the same column of subsequent rows
12079 refer to that section. The section identifiers are:
12080
12081 DW_SECT_INFO 1 .debug_info.dwo
12082 DW_SECT_TYPES 2 .debug_types.dwo
12083 DW_SECT_ABBREV 3 .debug_abbrev.dwo
12084 DW_SECT_LINE 4 .debug_line.dwo
12085 DW_SECT_LOC 5 .debug_loc.dwo
12086 DW_SECT_STR_OFFSETS 6 .debug_str_offsets.dwo
12087 DW_SECT_MACINFO 7 .debug_macinfo.dwo
12088 DW_SECT_MACRO 8 .debug_macro.dwo
12089
12090 The offsets provided by the CU and TU index sections are the base offsets
12091 for the contributions made by each CU or TU to the corresponding section
12092 in the package file. Each CU and TU header contains an abbrev_offset
12093 field, used to find the abbreviations table for that CU or TU within the
12094 contribution to the .debug_abbrev.dwo section for that CU or TU, and should
12095 be interpreted as relative to the base offset given in the index section.
12096 Likewise, offsets into .debug_line.dwo from DW_AT_stmt_list attributes
12097 should be interpreted as relative to the base offset for .debug_line.dwo,
12098 and offsets into other debug sections obtained from DWARF attributes should
12099 also be interpreted as relative to the corresponding base offset.
12100
12101 The table of sizes begins immediately following the table of offsets.
12102 Like the table of offsets, it is a two-dimensional array of 32-bit words,
12103 with L columns and N rows, in row-major order. Each row in the array is
12104 indexed starting from 1 (row 0 is shared by the two tables).
12105
12106 ---
12107
12108 Hash table lookup is handled the same in version 1 and 2:
12109
12110 We assume that N and M will not exceed 2^32 - 1.
12111 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
12112
d2415c6c
DE
12113 Given a 64-bit compilation unit signature or a type signature S, an entry
12114 in the hash table is located as follows:
80626a55 12115
d2415c6c
DE
12116 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
12117 the low-order k bits all set to 1.
80626a55 12118
d2415c6c 12119 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
80626a55 12120
d2415c6c
DE
12121 3) If the hash table entry at index H matches the signature, use that
12122 entry. If the hash table entry at index H is unused (all zeroes),
12123 terminate the search: the signature is not present in the table.
80626a55 12124
d2415c6c 12125 4) Let H = (H + H') modulo M. Repeat at Step 3.
80626a55 12126
d2415c6c 12127 Because M > N and H' and M are relatively prime, the search is guaranteed
73869dc2 12128 to stop at an unused slot or find the match. */
80626a55
DE
12129
12130/* Create a hash table to map DWO IDs to their CU/TU entry in
12131 .debug_{info,types}.dwo in DWP_FILE.
12132 Returns NULL if there isn't one.
12133 Note: This function processes DWP files only, not DWO files. */
12134
12135static struct dwp_hash_table *
ed2dc618
SM
12136create_dwp_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
12137 struct dwp_file *dwp_file, int is_debug_types)
80626a55
DE
12138{
12139 struct objfile *objfile = dwarf2_per_objfile->objfile;
12140 bfd *dbfd = dwp_file->dbfd;
948f8e3d 12141 const gdb_byte *index_ptr, *index_end;
80626a55 12142 struct dwarf2_section_info *index;
73869dc2 12143 uint32_t version, nr_columns, nr_units, nr_slots;
80626a55
DE
12144 struct dwp_hash_table *htab;
12145
12146 if (is_debug_types)
12147 index = &dwp_file->sections.tu_index;
12148 else
12149 index = &dwp_file->sections.cu_index;
12150
12151 if (dwarf2_section_empty_p (index))
12152 return NULL;
12153 dwarf2_read_section (objfile, index);
12154
12155 index_ptr = index->buffer;
12156 index_end = index_ptr + index->size;
12157
12158 version = read_4_bytes (dbfd, index_ptr);
73869dc2
DE
12159 index_ptr += 4;
12160 if (version == 2)
12161 nr_columns = read_4_bytes (dbfd, index_ptr);
12162 else
12163 nr_columns = 0;
12164 index_ptr += 4;
80626a55
DE
12165 nr_units = read_4_bytes (dbfd, index_ptr);
12166 index_ptr += 4;
12167 nr_slots = read_4_bytes (dbfd, index_ptr);
12168 index_ptr += 4;
12169
73869dc2 12170 if (version != 1 && version != 2)
80626a55 12171 {
21aa081e 12172 error (_("Dwarf Error: unsupported DWP file version (%s)"
80626a55 12173 " [in module %s]"),
21aa081e 12174 pulongest (version), dwp_file->name);
80626a55
DE
12175 }
12176 if (nr_slots != (nr_slots & -nr_slots))
12177 {
21aa081e 12178 error (_("Dwarf Error: number of slots in DWP hash table (%s)"
80626a55 12179 " is not power of 2 [in module %s]"),
21aa081e 12180 pulongest (nr_slots), dwp_file->name);
80626a55
DE
12181 }
12182
12183 htab = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_hash_table);
73869dc2
DE
12184 htab->version = version;
12185 htab->nr_columns = nr_columns;
80626a55
DE
12186 htab->nr_units = nr_units;
12187 htab->nr_slots = nr_slots;
12188 htab->hash_table = index_ptr;
12189 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
73869dc2
DE
12190
12191 /* Exit early if the table is empty. */
12192 if (nr_slots == 0 || nr_units == 0
12193 || (version == 2 && nr_columns == 0))
12194 {
12195 /* All must be zero. */
12196 if (nr_slots != 0 || nr_units != 0
12197 || (version == 2 && nr_columns != 0))
12198 {
12199 complaint (&symfile_complaints,
12200 _("Empty DWP but nr_slots,nr_units,nr_columns not"
12201 " all zero [in modules %s]"),
12202 dwp_file->name);
12203 }
12204 return htab;
12205 }
12206
12207 if (version == 1)
12208 {
12209 htab->section_pool.v1.indices =
12210 htab->unit_table + sizeof (uint32_t) * nr_slots;
12211 /* It's harder to decide whether the section is too small in v1.
12212 V1 is deprecated anyway so we punt. */
12213 }
12214 else
12215 {
12216 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
12217 int *ids = htab->section_pool.v2.section_ids;
12218 /* Reverse map for error checking. */
12219 int ids_seen[DW_SECT_MAX + 1];
12220 int i;
12221
12222 if (nr_columns < 2)
12223 {
12224 error (_("Dwarf Error: bad DWP hash table, too few columns"
12225 " in section table [in module %s]"),
12226 dwp_file->name);
12227 }
12228 if (nr_columns > MAX_NR_V2_DWO_SECTIONS)
12229 {
12230 error (_("Dwarf Error: bad DWP hash table, too many columns"
12231 " in section table [in module %s]"),
12232 dwp_file->name);
12233 }
12234 memset (ids, 255, (DW_SECT_MAX + 1) * sizeof (int32_t));
12235 memset (ids_seen, 255, (DW_SECT_MAX + 1) * sizeof (int32_t));
12236 for (i = 0; i < nr_columns; ++i)
12237 {
12238 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
12239
12240 if (id < DW_SECT_MIN || id > DW_SECT_MAX)
12241 {
12242 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
12243 " in section table [in module %s]"),
12244 id, dwp_file->name);
12245 }
12246 if (ids_seen[id] != -1)
12247 {
12248 error (_("Dwarf Error: bad DWP hash table, duplicate section"
12249 " id %d in section table [in module %s]"),
12250 id, dwp_file->name);
12251 }
12252 ids_seen[id] = i;
12253 ids[i] = id;
12254 }
12255 /* Must have exactly one info or types section. */
12256 if (((ids_seen[DW_SECT_INFO] != -1)
12257 + (ids_seen[DW_SECT_TYPES] != -1))
12258 != 1)
12259 {
12260 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
12261 " DWO info/types section [in module %s]"),
12262 dwp_file->name);
12263 }
12264 /* Must have an abbrev section. */
12265 if (ids_seen[DW_SECT_ABBREV] == -1)
12266 {
12267 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
12268 " section [in module %s]"),
12269 dwp_file->name);
12270 }
12271 htab->section_pool.v2.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
12272 htab->section_pool.v2.sizes =
12273 htab->section_pool.v2.offsets + (sizeof (uint32_t)
12274 * nr_units * nr_columns);
12275 if ((htab->section_pool.v2.sizes + (sizeof (uint32_t)
12276 * nr_units * nr_columns))
12277 > index_end)
12278 {
12279 error (_("Dwarf Error: DWP index section is corrupt (too small)"
12280 " [in module %s]"),
12281 dwp_file->name);
12282 }
12283 }
80626a55
DE
12284
12285 return htab;
12286}
12287
12288/* Update SECTIONS with the data from SECTP.
12289
12290 This function is like the other "locate" section routines that are
12291 passed to bfd_map_over_sections, but in this context the sections to
73869dc2 12292 read comes from the DWP V1 hash table, not the full ELF section table.
80626a55
DE
12293
12294 The result is non-zero for success, or zero if an error was found. */
12295
12296static int
73869dc2
DE
12297locate_v1_virtual_dwo_sections (asection *sectp,
12298 struct virtual_v1_dwo_sections *sections)
80626a55
DE
12299{
12300 const struct dwop_section_names *names = &dwop_section_names;
12301
12302 if (section_is_p (sectp->name, &names->abbrev_dwo))
12303 {
12304 /* There can be only one. */
049412e3 12305 if (sections->abbrev.s.section != NULL)
80626a55 12306 return 0;
049412e3 12307 sections->abbrev.s.section = sectp;
80626a55
DE
12308 sections->abbrev.size = bfd_get_section_size (sectp);
12309 }
12310 else if (section_is_p (sectp->name, &names->info_dwo)
12311 || section_is_p (sectp->name, &names->types_dwo))
12312 {
12313 /* There can be only one. */
049412e3 12314 if (sections->info_or_types.s.section != NULL)
80626a55 12315 return 0;
049412e3 12316 sections->info_or_types.s.section = sectp;
80626a55
DE
12317 sections->info_or_types.size = bfd_get_section_size (sectp);
12318 }
12319 else if (section_is_p (sectp->name, &names->line_dwo))
12320 {
12321 /* There can be only one. */
049412e3 12322 if (sections->line.s.section != NULL)
80626a55 12323 return 0;
049412e3 12324 sections->line.s.section = sectp;
80626a55
DE
12325 sections->line.size = bfd_get_section_size (sectp);
12326 }
12327 else if (section_is_p (sectp->name, &names->loc_dwo))
12328 {
12329 /* There can be only one. */
049412e3 12330 if (sections->loc.s.section != NULL)
80626a55 12331 return 0;
049412e3 12332 sections->loc.s.section = sectp;
80626a55
DE
12333 sections->loc.size = bfd_get_section_size (sectp);
12334 }
12335 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12336 {
12337 /* There can be only one. */
049412e3 12338 if (sections->macinfo.s.section != NULL)
80626a55 12339 return 0;
049412e3 12340 sections->macinfo.s.section = sectp;
80626a55
DE
12341 sections->macinfo.size = bfd_get_section_size (sectp);
12342 }
12343 else if (section_is_p (sectp->name, &names->macro_dwo))
12344 {
12345 /* There can be only one. */
049412e3 12346 if (sections->macro.s.section != NULL)
80626a55 12347 return 0;
049412e3 12348 sections->macro.s.section = sectp;
80626a55
DE
12349 sections->macro.size = bfd_get_section_size (sectp);
12350 }
12351 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12352 {
12353 /* There can be only one. */
049412e3 12354 if (sections->str_offsets.s.section != NULL)
80626a55 12355 return 0;
049412e3 12356 sections->str_offsets.s.section = sectp;
80626a55
DE
12357 sections->str_offsets.size = bfd_get_section_size (sectp);
12358 }
12359 else
12360 {
12361 /* No other kind of section is valid. */
12362 return 0;
12363 }
12364
12365 return 1;
12366}
12367
73869dc2
DE
12368/* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
12369 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
12370 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
12371 This is for DWP version 1 files. */
80626a55
DE
12372
12373static struct dwo_unit *
ed2dc618
SM
12374create_dwo_unit_in_dwp_v1 (struct dwarf2_per_objfile *dwarf2_per_objfile,
12375 struct dwp_file *dwp_file,
73869dc2
DE
12376 uint32_t unit_index,
12377 const char *comp_dir,
12378 ULONGEST signature, int is_debug_types)
80626a55
DE
12379{
12380 struct objfile *objfile = dwarf2_per_objfile->objfile;
73869dc2
DE
12381 const struct dwp_hash_table *dwp_htab =
12382 is_debug_types ? dwp_file->tus : dwp_file->cus;
80626a55
DE
12383 bfd *dbfd = dwp_file->dbfd;
12384 const char *kind = is_debug_types ? "TU" : "CU";
12385 struct dwo_file *dwo_file;
12386 struct dwo_unit *dwo_unit;
73869dc2 12387 struct virtual_v1_dwo_sections sections;
80626a55 12388 void **dwo_file_slot;
80626a55
DE
12389 int i;
12390
73869dc2
DE
12391 gdb_assert (dwp_file->version == 1);
12392
b4f54984 12393 if (dwarf_read_debug)
80626a55 12394 {
73869dc2 12395 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V1 file: %s\n",
80626a55 12396 kind,
73869dc2 12397 pulongest (unit_index), hex_string (signature),
80626a55
DE
12398 dwp_file->name);
12399 }
12400
19ac8c2e 12401 /* Fetch the sections of this DWO unit.
80626a55
DE
12402 Put a limit on the number of sections we look for so that bad data
12403 doesn't cause us to loop forever. */
12404
73869dc2 12405#define MAX_NR_V1_DWO_SECTIONS \
80626a55
DE
12406 (1 /* .debug_info or .debug_types */ \
12407 + 1 /* .debug_abbrev */ \
12408 + 1 /* .debug_line */ \
12409 + 1 /* .debug_loc */ \
12410 + 1 /* .debug_str_offsets */ \
19ac8c2e 12411 + 1 /* .debug_macro or .debug_macinfo */ \
80626a55
DE
12412 + 1 /* trailing zero */)
12413
12414 memset (&sections, 0, sizeof (sections));
80626a55 12415
73869dc2 12416 for (i = 0; i < MAX_NR_V1_DWO_SECTIONS; ++i)
80626a55
DE
12417 {
12418 asection *sectp;
12419 uint32_t section_nr =
12420 read_4_bytes (dbfd,
73869dc2
DE
12421 dwp_htab->section_pool.v1.indices
12422 + (unit_index + i) * sizeof (uint32_t));
80626a55
DE
12423
12424 if (section_nr == 0)
12425 break;
12426 if (section_nr >= dwp_file->num_sections)
12427 {
12428 error (_("Dwarf Error: bad DWP hash table, section number too large"
12429 " [in module %s]"),
12430 dwp_file->name);
12431 }
12432
12433 sectp = dwp_file->elf_sections[section_nr];
73869dc2 12434 if (! locate_v1_virtual_dwo_sections (sectp, &sections))
80626a55
DE
12435 {
12436 error (_("Dwarf Error: bad DWP hash table, invalid section found"
12437 " [in module %s]"),
12438 dwp_file->name);
12439 }
12440 }
12441
12442 if (i < 2
a32a8923
DE
12443 || dwarf2_section_empty_p (&sections.info_or_types)
12444 || dwarf2_section_empty_p (&sections.abbrev))
80626a55
DE
12445 {
12446 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
12447 " [in module %s]"),
12448 dwp_file->name);
12449 }
73869dc2 12450 if (i == MAX_NR_V1_DWO_SECTIONS)
80626a55
DE
12451 {
12452 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
12453 " [in module %s]"),
12454 dwp_file->name);
12455 }
12456
12457 /* It's easier for the rest of the code if we fake a struct dwo_file and
12458 have dwo_unit "live" in that. At least for now.
12459
12460 The DWP file can be made up of a random collection of CUs and TUs.
c766f7ec 12461 However, for each CU + set of TUs that came from the same original DWO
57d63ce2
DE
12462 file, we can combine them back into a virtual DWO file to save space
12463 (fewer struct dwo_file objects to allocate). Remember that for really
80626a55
DE
12464 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
12465
791afaa2
TT
12466 std::string virtual_dwo_name =
12467 string_printf ("virtual-dwo/%d-%d-%d-%d",
12468 get_section_id (&sections.abbrev),
12469 get_section_id (&sections.line),
12470 get_section_id (&sections.loc),
12471 get_section_id (&sections.str_offsets));
80626a55 12472 /* Can we use an existing virtual DWO file? */
ed2dc618
SM
12473 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
12474 virtual_dwo_name.c_str (),
12475 comp_dir);
80626a55
DE
12476 /* Create one if necessary. */
12477 if (*dwo_file_slot == NULL)
12478 {
b4f54984 12479 if (dwarf_read_debug)
80626a55
DE
12480 {
12481 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
791afaa2 12482 virtual_dwo_name.c_str ());
80626a55
DE
12483 }
12484 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
224c3ddb
SM
12485 dwo_file->dwo_name
12486 = (const char *) obstack_copy0 (&objfile->objfile_obstack,
791afaa2
TT
12487 virtual_dwo_name.c_str (),
12488 virtual_dwo_name.size ());
0ac5b59e 12489 dwo_file->comp_dir = comp_dir;
80626a55
DE
12490 dwo_file->sections.abbrev = sections.abbrev;
12491 dwo_file->sections.line = sections.line;
12492 dwo_file->sections.loc = sections.loc;
12493 dwo_file->sections.macinfo = sections.macinfo;
12494 dwo_file->sections.macro = sections.macro;
12495 dwo_file->sections.str_offsets = sections.str_offsets;
12496 /* The "str" section is global to the entire DWP file. */
12497 dwo_file->sections.str = dwp_file->sections.str;
57d63ce2 12498 /* The info or types section is assigned below to dwo_unit,
80626a55
DE
12499 there's no need to record it in dwo_file.
12500 Also, we can't simply record type sections in dwo_file because
12501 we record a pointer into the vector in dwo_unit. As we collect more
12502 types we'll grow the vector and eventually have to reallocate space
57d63ce2
DE
12503 for it, invalidating all copies of pointers into the previous
12504 contents. */
80626a55
DE
12505 *dwo_file_slot = dwo_file;
12506 }
12507 else
12508 {
b4f54984 12509 if (dwarf_read_debug)
80626a55
DE
12510 {
12511 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
791afaa2 12512 virtual_dwo_name.c_str ());
80626a55 12513 }
9a3c8263 12514 dwo_file = (struct dwo_file *) *dwo_file_slot;
80626a55 12515 }
80626a55
DE
12516
12517 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
12518 dwo_unit->dwo_file = dwo_file;
12519 dwo_unit->signature = signature;
8d749320
SM
12520 dwo_unit->section =
12521 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
8a0459fd 12522 *dwo_unit->section = sections.info_or_types;
57d63ce2 12523 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
80626a55
DE
12524
12525 return dwo_unit;
12526}
12527
73869dc2
DE
12528/* Subroutine of create_dwo_unit_in_dwp_v2 to simplify it.
12529 Given a pointer to the containing section SECTION, and OFFSET,SIZE of the
12530 piece within that section used by a TU/CU, return a virtual section
12531 of just that piece. */
12532
12533static struct dwarf2_section_info
ed2dc618
SM
12534create_dwp_v2_section (struct dwarf2_per_objfile *dwarf2_per_objfile,
12535 struct dwarf2_section_info *section,
73869dc2
DE
12536 bfd_size_type offset, bfd_size_type size)
12537{
12538 struct dwarf2_section_info result;
12539 asection *sectp;
12540
12541 gdb_assert (section != NULL);
12542 gdb_assert (!section->is_virtual);
12543
12544 memset (&result, 0, sizeof (result));
12545 result.s.containing_section = section;
12546 result.is_virtual = 1;
12547
12548 if (size == 0)
12549 return result;
12550
12551 sectp = get_section_bfd_section (section);
12552
12553 /* Flag an error if the piece denoted by OFFSET,SIZE is outside the
12554 bounds of the real section. This is a pretty-rare event, so just
12555 flag an error (easier) instead of a warning and trying to cope. */
12556 if (sectp == NULL
12557 || offset + size > bfd_get_section_size (sectp))
12558 {
73869dc2
DE
12559 error (_("Dwarf Error: Bad DWP V2 section info, doesn't fit"
12560 " in section %s [in module %s]"),
12561 sectp ? bfd_section_name (abfd, sectp) : "<unknown>",
12562 objfile_name (dwarf2_per_objfile->objfile));
12563 }
12564
12565 result.virtual_offset = offset;
12566 result.size = size;
12567 return result;
12568}
12569
12570/* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
12571 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
12572 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
12573 This is for DWP version 2 files. */
12574
12575static struct dwo_unit *
ed2dc618
SM
12576create_dwo_unit_in_dwp_v2 (struct dwarf2_per_objfile *dwarf2_per_objfile,
12577 struct dwp_file *dwp_file,
73869dc2
DE
12578 uint32_t unit_index,
12579 const char *comp_dir,
12580 ULONGEST signature, int is_debug_types)
12581{
12582 struct objfile *objfile = dwarf2_per_objfile->objfile;
12583 const struct dwp_hash_table *dwp_htab =
12584 is_debug_types ? dwp_file->tus : dwp_file->cus;
12585 bfd *dbfd = dwp_file->dbfd;
12586 const char *kind = is_debug_types ? "TU" : "CU";
12587 struct dwo_file *dwo_file;
12588 struct dwo_unit *dwo_unit;
12589 struct virtual_v2_dwo_sections sections;
12590 void **dwo_file_slot;
73869dc2
DE
12591 int i;
12592
12593 gdb_assert (dwp_file->version == 2);
12594
b4f54984 12595 if (dwarf_read_debug)
73869dc2
DE
12596 {
12597 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V2 file: %s\n",
12598 kind,
12599 pulongest (unit_index), hex_string (signature),
12600 dwp_file->name);
12601 }
12602
12603 /* Fetch the section offsets of this DWO unit. */
12604
12605 memset (&sections, 0, sizeof (sections));
73869dc2
DE
12606
12607 for (i = 0; i < dwp_htab->nr_columns; ++i)
12608 {
12609 uint32_t offset = read_4_bytes (dbfd,
12610 dwp_htab->section_pool.v2.offsets
12611 + (((unit_index - 1) * dwp_htab->nr_columns
12612 + i)
12613 * sizeof (uint32_t)));
12614 uint32_t size = read_4_bytes (dbfd,
12615 dwp_htab->section_pool.v2.sizes
12616 + (((unit_index - 1) * dwp_htab->nr_columns
12617 + i)
12618 * sizeof (uint32_t)));
12619
12620 switch (dwp_htab->section_pool.v2.section_ids[i])
12621 {
12622 case DW_SECT_INFO:
12623 case DW_SECT_TYPES:
12624 sections.info_or_types_offset = offset;
12625 sections.info_or_types_size = size;
12626 break;
12627 case DW_SECT_ABBREV:
12628 sections.abbrev_offset = offset;
12629 sections.abbrev_size = size;
12630 break;
12631 case DW_SECT_LINE:
12632 sections.line_offset = offset;
12633 sections.line_size = size;
12634 break;
12635 case DW_SECT_LOC:
12636 sections.loc_offset = offset;
12637 sections.loc_size = size;
12638 break;
12639 case DW_SECT_STR_OFFSETS:
12640 sections.str_offsets_offset = offset;
12641 sections.str_offsets_size = size;
12642 break;
12643 case DW_SECT_MACINFO:
12644 sections.macinfo_offset = offset;
12645 sections.macinfo_size = size;
12646 break;
12647 case DW_SECT_MACRO:
12648 sections.macro_offset = offset;
12649 sections.macro_size = size;
12650 break;
12651 }
12652 }
12653
12654 /* It's easier for the rest of the code if we fake a struct dwo_file and
12655 have dwo_unit "live" in that. At least for now.
12656
12657 The DWP file can be made up of a random collection of CUs and TUs.
12658 However, for each CU + set of TUs that came from the same original DWO
12659 file, we can combine them back into a virtual DWO file to save space
12660 (fewer struct dwo_file objects to allocate). Remember that for really
12661 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
12662
791afaa2
TT
12663 std::string virtual_dwo_name =
12664 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld",
12665 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
12666 (long) (sections.line_size ? sections.line_offset : 0),
12667 (long) (sections.loc_size ? sections.loc_offset : 0),
12668 (long) (sections.str_offsets_size
12669 ? sections.str_offsets_offset : 0));
73869dc2 12670 /* Can we use an existing virtual DWO file? */
ed2dc618
SM
12671 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
12672 virtual_dwo_name.c_str (),
12673 comp_dir);
73869dc2
DE
12674 /* Create one if necessary. */
12675 if (*dwo_file_slot == NULL)
12676 {
b4f54984 12677 if (dwarf_read_debug)
73869dc2
DE
12678 {
12679 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
791afaa2 12680 virtual_dwo_name.c_str ());
73869dc2
DE
12681 }
12682 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
224c3ddb
SM
12683 dwo_file->dwo_name
12684 = (const char *) obstack_copy0 (&objfile->objfile_obstack,
791afaa2
TT
12685 virtual_dwo_name.c_str (),
12686 virtual_dwo_name.size ());
73869dc2
DE
12687 dwo_file->comp_dir = comp_dir;
12688 dwo_file->sections.abbrev =
ed2dc618 12689 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.abbrev,
73869dc2
DE
12690 sections.abbrev_offset, sections.abbrev_size);
12691 dwo_file->sections.line =
ed2dc618 12692 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.line,
73869dc2
DE
12693 sections.line_offset, sections.line_size);
12694 dwo_file->sections.loc =
ed2dc618 12695 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.loc,
73869dc2
DE
12696 sections.loc_offset, sections.loc_size);
12697 dwo_file->sections.macinfo =
ed2dc618 12698 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.macinfo,
73869dc2
DE
12699 sections.macinfo_offset, sections.macinfo_size);
12700 dwo_file->sections.macro =
ed2dc618 12701 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.macro,
73869dc2
DE
12702 sections.macro_offset, sections.macro_size);
12703 dwo_file->sections.str_offsets =
ed2dc618
SM
12704 create_dwp_v2_section (dwarf2_per_objfile,
12705 &dwp_file->sections.str_offsets,
73869dc2
DE
12706 sections.str_offsets_offset,
12707 sections.str_offsets_size);
12708 /* The "str" section is global to the entire DWP file. */
12709 dwo_file->sections.str = dwp_file->sections.str;
12710 /* The info or types section is assigned below to dwo_unit,
12711 there's no need to record it in dwo_file.
12712 Also, we can't simply record type sections in dwo_file because
12713 we record a pointer into the vector in dwo_unit. As we collect more
12714 types we'll grow the vector and eventually have to reallocate space
12715 for it, invalidating all copies of pointers into the previous
12716 contents. */
12717 *dwo_file_slot = dwo_file;
12718 }
12719 else
12720 {
b4f54984 12721 if (dwarf_read_debug)
73869dc2
DE
12722 {
12723 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
791afaa2 12724 virtual_dwo_name.c_str ());
73869dc2 12725 }
9a3c8263 12726 dwo_file = (struct dwo_file *) *dwo_file_slot;
73869dc2 12727 }
73869dc2
DE
12728
12729 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
12730 dwo_unit->dwo_file = dwo_file;
12731 dwo_unit->signature = signature;
8d749320
SM
12732 dwo_unit->section =
12733 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
ed2dc618
SM
12734 *dwo_unit->section = create_dwp_v2_section (dwarf2_per_objfile,
12735 is_debug_types
73869dc2
DE
12736 ? &dwp_file->sections.types
12737 : &dwp_file->sections.info,
12738 sections.info_or_types_offset,
12739 sections.info_or_types_size);
12740 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
12741
12742 return dwo_unit;
12743}
12744
57d63ce2
DE
12745/* Lookup the DWO unit with SIGNATURE in DWP_FILE.
12746 Returns NULL if the signature isn't found. */
80626a55
DE
12747
12748static struct dwo_unit *
ed2dc618
SM
12749lookup_dwo_unit_in_dwp (struct dwarf2_per_objfile *dwarf2_per_objfile,
12750 struct dwp_file *dwp_file, const char *comp_dir,
57d63ce2 12751 ULONGEST signature, int is_debug_types)
80626a55 12752{
57d63ce2
DE
12753 const struct dwp_hash_table *dwp_htab =
12754 is_debug_types ? dwp_file->tus : dwp_file->cus;
80626a55 12755 bfd *dbfd = dwp_file->dbfd;
57d63ce2 12756 uint32_t mask = dwp_htab->nr_slots - 1;
80626a55
DE
12757 uint32_t hash = signature & mask;
12758 uint32_t hash2 = ((signature >> 32) & mask) | 1;
12759 unsigned int i;
12760 void **slot;
870f88f7 12761 struct dwo_unit find_dwo_cu;
80626a55
DE
12762
12763 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
12764 find_dwo_cu.signature = signature;
19ac8c2e
DE
12765 slot = htab_find_slot (is_debug_types
12766 ? dwp_file->loaded_tus
12767 : dwp_file->loaded_cus,
12768 &find_dwo_cu, INSERT);
80626a55
DE
12769
12770 if (*slot != NULL)
9a3c8263 12771 return (struct dwo_unit *) *slot;
80626a55
DE
12772
12773 /* Use a for loop so that we don't loop forever on bad debug info. */
57d63ce2 12774 for (i = 0; i < dwp_htab->nr_slots; ++i)
80626a55
DE
12775 {
12776 ULONGEST signature_in_table;
12777
12778 signature_in_table =
57d63ce2 12779 read_8_bytes (dbfd, dwp_htab->hash_table + hash * sizeof (uint64_t));
80626a55
DE
12780 if (signature_in_table == signature)
12781 {
57d63ce2
DE
12782 uint32_t unit_index =
12783 read_4_bytes (dbfd,
12784 dwp_htab->unit_table + hash * sizeof (uint32_t));
80626a55 12785
73869dc2
DE
12786 if (dwp_file->version == 1)
12787 {
ed2dc618
SM
12788 *slot = create_dwo_unit_in_dwp_v1 (dwarf2_per_objfile,
12789 dwp_file, unit_index,
73869dc2
DE
12790 comp_dir, signature,
12791 is_debug_types);
12792 }
12793 else
12794 {
ed2dc618
SM
12795 *slot = create_dwo_unit_in_dwp_v2 (dwarf2_per_objfile,
12796 dwp_file, unit_index,
73869dc2
DE
12797 comp_dir, signature,
12798 is_debug_types);
12799 }
9a3c8263 12800 return (struct dwo_unit *) *slot;
80626a55
DE
12801 }
12802 if (signature_in_table == 0)
12803 return NULL;
12804 hash = (hash + hash2) & mask;
12805 }
12806
12807 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
12808 " [in module %s]"),
12809 dwp_file->name);
12810}
12811
ab5088bf 12812/* Subroutine of open_dwo_file,open_dwp_file to simplify them.
3019eac3
DE
12813 Open the file specified by FILE_NAME and hand it off to BFD for
12814 preliminary analysis. Return a newly initialized bfd *, which
12815 includes a canonicalized copy of FILE_NAME.
80626a55 12816 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
6ac97d4c
DE
12817 SEARCH_CWD is true if the current directory is to be searched.
12818 It will be searched before debug-file-directory.
13aaf454
DE
12819 If successful, the file is added to the bfd include table of the
12820 objfile's bfd (see gdb_bfd_record_inclusion).
6ac97d4c 12821 If unable to find/open the file, return NULL.
3019eac3
DE
12822 NOTE: This function is derived from symfile_bfd_open. */
12823
192b62ce 12824static gdb_bfd_ref_ptr
ed2dc618
SM
12825try_open_dwop_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12826 const char *file_name, int is_dwp, int search_cwd)
3019eac3 12827{
24b9144d 12828 int desc;
9c02c129
DE
12829 /* Blech. OPF_TRY_CWD_FIRST also disables searching the path list if
12830 FILE_NAME contains a '/'. So we can't use it. Instead prepend "."
12831 to debug_file_directory. */
e0cc99a6 12832 const char *search_path;
9c02c129
DE
12833 static const char dirname_separator_string[] = { DIRNAME_SEPARATOR, '\0' };
12834
e0cc99a6 12835 gdb::unique_xmalloc_ptr<char> search_path_holder;
6ac97d4c
DE
12836 if (search_cwd)
12837 {
12838 if (*debug_file_directory != '\0')
e0cc99a6
TT
12839 {
12840 search_path_holder.reset (concat (".", dirname_separator_string,
12841 debug_file_directory,
12842 (char *) NULL));
12843 search_path = search_path_holder.get ();
12844 }
6ac97d4c 12845 else
e0cc99a6 12846 search_path = ".";
6ac97d4c 12847 }
9c02c129 12848 else
e0cc99a6 12849 search_path = debug_file_directory;
3019eac3 12850
24b9144d 12851 openp_flags flags = OPF_RETURN_REALPATH;
80626a55
DE
12852 if (is_dwp)
12853 flags |= OPF_SEARCH_IN_PATH;
e0cc99a6
TT
12854
12855 gdb::unique_xmalloc_ptr<char> absolute_name;
9c02c129 12856 desc = openp (search_path, flags, file_name,
3019eac3
DE
12857 O_RDONLY | O_BINARY, &absolute_name);
12858 if (desc < 0)
12859 return NULL;
12860
e0cc99a6
TT
12861 gdb_bfd_ref_ptr sym_bfd (gdb_bfd_open (absolute_name.get (),
12862 gnutarget, desc));
9c02c129
DE
12863 if (sym_bfd == NULL)
12864 return NULL;
192b62ce 12865 bfd_set_cacheable (sym_bfd.get (), 1);
3019eac3 12866
192b62ce
TT
12867 if (!bfd_check_format (sym_bfd.get (), bfd_object))
12868 return NULL;
3019eac3 12869
13aaf454
DE
12870 /* Success. Record the bfd as having been included by the objfile's bfd.
12871 This is important because things like demangled_names_hash lives in the
12872 objfile's per_bfd space and may have references to things like symbol
12873 names that live in the DWO/DWP file's per_bfd space. PR 16426. */
192b62ce 12874 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd, sym_bfd.get ());
13aaf454 12875
3019eac3
DE
12876 return sym_bfd;
12877}
12878
ab5088bf 12879/* Try to open DWO file FILE_NAME.
3019eac3
DE
12880 COMP_DIR is the DW_AT_comp_dir attribute.
12881 The result is the bfd handle of the file.
12882 If there is a problem finding or opening the file, return NULL.
12883 Upon success, the canonicalized path of the file is stored in the bfd,
12884 same as symfile_bfd_open. */
12885
192b62ce 12886static gdb_bfd_ref_ptr
ed2dc618
SM
12887open_dwo_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12888 const char *file_name, const char *comp_dir)
3019eac3 12889{
80626a55 12890 if (IS_ABSOLUTE_PATH (file_name))
ed2dc618
SM
12891 return try_open_dwop_file (dwarf2_per_objfile, file_name,
12892 0 /*is_dwp*/, 0 /*search_cwd*/);
3019eac3
DE
12893
12894 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
12895
12896 if (comp_dir != NULL)
12897 {
b36cec19
PA
12898 char *path_to_try = concat (comp_dir, SLASH_STRING,
12899 file_name, (char *) NULL);
3019eac3
DE
12900
12901 /* NOTE: If comp_dir is a relative path, this will also try the
12902 search path, which seems useful. */
ed2dc618
SM
12903 gdb_bfd_ref_ptr abfd (try_open_dwop_file (dwarf2_per_objfile,
12904 path_to_try,
12905 0 /*is_dwp*/,
192b62ce 12906 1 /*search_cwd*/));
3019eac3
DE
12907 xfree (path_to_try);
12908 if (abfd != NULL)
12909 return abfd;
12910 }
12911
12912 /* That didn't work, try debug-file-directory, which, despite its name,
12913 is a list of paths. */
12914
12915 if (*debug_file_directory == '\0')
12916 return NULL;
12917
ed2dc618
SM
12918 return try_open_dwop_file (dwarf2_per_objfile, file_name,
12919 0 /*is_dwp*/, 1 /*search_cwd*/);
3019eac3
DE
12920}
12921
80626a55
DE
12922/* This function is mapped across the sections and remembers the offset and
12923 size of each of the DWO debugging sections we are interested in. */
12924
12925static void
12926dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp, void *dwo_sections_ptr)
12927{
9a3c8263 12928 struct dwo_sections *dwo_sections = (struct dwo_sections *) dwo_sections_ptr;
80626a55
DE
12929 const struct dwop_section_names *names = &dwop_section_names;
12930
12931 if (section_is_p (sectp->name, &names->abbrev_dwo))
12932 {
049412e3 12933 dwo_sections->abbrev.s.section = sectp;
80626a55
DE
12934 dwo_sections->abbrev.size = bfd_get_section_size (sectp);
12935 }
12936 else if (section_is_p (sectp->name, &names->info_dwo))
12937 {
049412e3 12938 dwo_sections->info.s.section = sectp;
80626a55
DE
12939 dwo_sections->info.size = bfd_get_section_size (sectp);
12940 }
12941 else if (section_is_p (sectp->name, &names->line_dwo))
12942 {
049412e3 12943 dwo_sections->line.s.section = sectp;
80626a55
DE
12944 dwo_sections->line.size = bfd_get_section_size (sectp);
12945 }
12946 else if (section_is_p (sectp->name, &names->loc_dwo))
12947 {
049412e3 12948 dwo_sections->loc.s.section = sectp;
80626a55
DE
12949 dwo_sections->loc.size = bfd_get_section_size (sectp);
12950 }
12951 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12952 {
049412e3 12953 dwo_sections->macinfo.s.section = sectp;
80626a55
DE
12954 dwo_sections->macinfo.size = bfd_get_section_size (sectp);
12955 }
12956 else if (section_is_p (sectp->name, &names->macro_dwo))
12957 {
049412e3 12958 dwo_sections->macro.s.section = sectp;
80626a55
DE
12959 dwo_sections->macro.size = bfd_get_section_size (sectp);
12960 }
12961 else if (section_is_p (sectp->name, &names->str_dwo))
12962 {
049412e3 12963 dwo_sections->str.s.section = sectp;
80626a55
DE
12964 dwo_sections->str.size = bfd_get_section_size (sectp);
12965 }
12966 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12967 {
049412e3 12968 dwo_sections->str_offsets.s.section = sectp;
80626a55
DE
12969 dwo_sections->str_offsets.size = bfd_get_section_size (sectp);
12970 }
12971 else if (section_is_p (sectp->name, &names->types_dwo))
12972 {
12973 struct dwarf2_section_info type_section;
12974
12975 memset (&type_section, 0, sizeof (type_section));
049412e3 12976 type_section.s.section = sectp;
80626a55
DE
12977 type_section.size = bfd_get_section_size (sectp);
12978 VEC_safe_push (dwarf2_section_info_def, dwo_sections->types,
12979 &type_section);
12980 }
12981}
12982
ab5088bf 12983/* Initialize the use of the DWO file specified by DWO_NAME and referenced
19c3d4c9 12984 by PER_CU. This is for the non-DWP case.
80626a55 12985 The result is NULL if DWO_NAME can't be found. */
3019eac3
DE
12986
12987static struct dwo_file *
0ac5b59e
DE
12988open_and_init_dwo_file (struct dwarf2_per_cu_data *per_cu,
12989 const char *dwo_name, const char *comp_dir)
3019eac3 12990{
ed2dc618 12991 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
3019eac3 12992 struct objfile *objfile = dwarf2_per_objfile->objfile;
3019eac3 12993
ed2dc618 12994 gdb_bfd_ref_ptr dbfd (open_dwo_file (dwarf2_per_objfile, dwo_name, comp_dir));
80626a55
DE
12995 if (dbfd == NULL)
12996 {
b4f54984 12997 if (dwarf_read_debug)
80626a55
DE
12998 fprintf_unfiltered (gdb_stdlog, "DWO file not found: %s\n", dwo_name);
12999 return NULL;
13000 }
263db9a1
TT
13001
13002 /* We use a unique pointer here, despite the obstack allocation,
13003 because a dwo_file needs some cleanup if it is abandoned. */
13004 dwo_file_up dwo_file (OBSTACK_ZALLOC (&objfile->objfile_obstack,
13005 struct dwo_file));
0ac5b59e
DE
13006 dwo_file->dwo_name = dwo_name;
13007 dwo_file->comp_dir = comp_dir;
192b62ce 13008 dwo_file->dbfd = dbfd.release ();
3019eac3 13009
192b62ce
TT
13010 bfd_map_over_sections (dwo_file->dbfd, dwarf2_locate_dwo_sections,
13011 &dwo_file->sections);
3019eac3 13012
ed2dc618
SM
13013 create_cus_hash_table (dwarf2_per_objfile, *dwo_file, dwo_file->sections.info,
13014 dwo_file->cus);
3019eac3 13015
263db9a1 13016 create_debug_types_hash_table (dwarf2_per_objfile, dwo_file.get (),
ed2dc618 13017 dwo_file->sections.types, dwo_file->tus);
3019eac3 13018
b4f54984 13019 if (dwarf_read_debug)
80626a55
DE
13020 fprintf_unfiltered (gdb_stdlog, "DWO file found: %s\n", dwo_name);
13021
263db9a1 13022 return dwo_file.release ();
3019eac3
DE
13023}
13024
80626a55 13025/* This function is mapped across the sections and remembers the offset and
73869dc2
DE
13026 size of each of the DWP debugging sections common to version 1 and 2 that
13027 we are interested in. */
3019eac3 13028
80626a55 13029static void
73869dc2
DE
13030dwarf2_locate_common_dwp_sections (bfd *abfd, asection *sectp,
13031 void *dwp_file_ptr)
3019eac3 13032{
9a3c8263 13033 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
80626a55
DE
13034 const struct dwop_section_names *names = &dwop_section_names;
13035 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
3019eac3 13036
80626a55 13037 /* Record the ELF section number for later lookup: this is what the
73869dc2 13038 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
80626a55
DE
13039 gdb_assert (elf_section_nr < dwp_file->num_sections);
13040 dwp_file->elf_sections[elf_section_nr] = sectp;
3019eac3 13041
80626a55
DE
13042 /* Look for specific sections that we need. */
13043 if (section_is_p (sectp->name, &names->str_dwo))
13044 {
049412e3 13045 dwp_file->sections.str.s.section = sectp;
80626a55
DE
13046 dwp_file->sections.str.size = bfd_get_section_size (sectp);
13047 }
13048 else if (section_is_p (sectp->name, &names->cu_index))
13049 {
049412e3 13050 dwp_file->sections.cu_index.s.section = sectp;
80626a55
DE
13051 dwp_file->sections.cu_index.size = bfd_get_section_size (sectp);
13052 }
13053 else if (section_is_p (sectp->name, &names->tu_index))
13054 {
049412e3 13055 dwp_file->sections.tu_index.s.section = sectp;
80626a55
DE
13056 dwp_file->sections.tu_index.size = bfd_get_section_size (sectp);
13057 }
13058}
3019eac3 13059
73869dc2
DE
13060/* This function is mapped across the sections and remembers the offset and
13061 size of each of the DWP version 2 debugging sections that we are interested
13062 in. This is split into a separate function because we don't know if we
13063 have version 1 or 2 until we parse the cu_index/tu_index sections. */
13064
13065static void
13066dwarf2_locate_v2_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
13067{
9a3c8263 13068 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
73869dc2
DE
13069 const struct dwop_section_names *names = &dwop_section_names;
13070 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
13071
13072 /* Record the ELF section number for later lookup: this is what the
13073 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
13074 gdb_assert (elf_section_nr < dwp_file->num_sections);
13075 dwp_file->elf_sections[elf_section_nr] = sectp;
13076
13077 /* Look for specific sections that we need. */
13078 if (section_is_p (sectp->name, &names->abbrev_dwo))
13079 {
049412e3 13080 dwp_file->sections.abbrev.s.section = sectp;
73869dc2
DE
13081 dwp_file->sections.abbrev.size = bfd_get_section_size (sectp);
13082 }
13083 else if (section_is_p (sectp->name, &names->info_dwo))
13084 {
049412e3 13085 dwp_file->sections.info.s.section = sectp;
73869dc2
DE
13086 dwp_file->sections.info.size = bfd_get_section_size (sectp);
13087 }
13088 else if (section_is_p (sectp->name, &names->line_dwo))
13089 {
049412e3 13090 dwp_file->sections.line.s.section = sectp;
73869dc2
DE
13091 dwp_file->sections.line.size = bfd_get_section_size (sectp);
13092 }
13093 else if (section_is_p (sectp->name, &names->loc_dwo))
13094 {
049412e3 13095 dwp_file->sections.loc.s.section = sectp;
73869dc2
DE
13096 dwp_file->sections.loc.size = bfd_get_section_size (sectp);
13097 }
13098 else if (section_is_p (sectp->name, &names->macinfo_dwo))
13099 {
049412e3 13100 dwp_file->sections.macinfo.s.section = sectp;
73869dc2
DE
13101 dwp_file->sections.macinfo.size = bfd_get_section_size (sectp);
13102 }
13103 else if (section_is_p (sectp->name, &names->macro_dwo))
13104 {
049412e3 13105 dwp_file->sections.macro.s.section = sectp;
73869dc2
DE
13106 dwp_file->sections.macro.size = bfd_get_section_size (sectp);
13107 }
13108 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
13109 {
049412e3 13110 dwp_file->sections.str_offsets.s.section = sectp;
73869dc2
DE
13111 dwp_file->sections.str_offsets.size = bfd_get_section_size (sectp);
13112 }
13113 else if (section_is_p (sectp->name, &names->types_dwo))
13114 {
049412e3 13115 dwp_file->sections.types.s.section = sectp;
73869dc2
DE
13116 dwp_file->sections.types.size = bfd_get_section_size (sectp);
13117 }
13118}
13119
80626a55 13120/* Hash function for dwp_file loaded CUs/TUs. */
3019eac3 13121
80626a55
DE
13122static hashval_t
13123hash_dwp_loaded_cutus (const void *item)
13124{
9a3c8263 13125 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
3019eac3 13126
80626a55
DE
13127 /* This drops the top 32 bits of the signature, but is ok for a hash. */
13128 return dwo_unit->signature;
3019eac3
DE
13129}
13130
80626a55 13131/* Equality function for dwp_file loaded CUs/TUs. */
3019eac3 13132
80626a55
DE
13133static int
13134eq_dwp_loaded_cutus (const void *a, const void *b)
3019eac3 13135{
9a3c8263
SM
13136 const struct dwo_unit *dua = (const struct dwo_unit *) a;
13137 const struct dwo_unit *dub = (const struct dwo_unit *) b;
3019eac3 13138
80626a55
DE
13139 return dua->signature == dub->signature;
13140}
3019eac3 13141
80626a55 13142/* Allocate a hash table for dwp_file loaded CUs/TUs. */
3019eac3 13143
80626a55
DE
13144static htab_t
13145allocate_dwp_loaded_cutus_table (struct objfile *objfile)
13146{
13147 return htab_create_alloc_ex (3,
13148 hash_dwp_loaded_cutus,
13149 eq_dwp_loaded_cutus,
13150 NULL,
13151 &objfile->objfile_obstack,
13152 hashtab_obstack_allocate,
13153 dummy_obstack_deallocate);
13154}
3019eac3 13155
ab5088bf
DE
13156/* Try to open DWP file FILE_NAME.
13157 The result is the bfd handle of the file.
13158 If there is a problem finding or opening the file, return NULL.
13159 Upon success, the canonicalized path of the file is stored in the bfd,
13160 same as symfile_bfd_open. */
13161
192b62ce 13162static gdb_bfd_ref_ptr
ed2dc618
SM
13163open_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
13164 const char *file_name)
ab5088bf 13165{
ed2dc618
SM
13166 gdb_bfd_ref_ptr abfd (try_open_dwop_file (dwarf2_per_objfile, file_name,
13167 1 /*is_dwp*/,
192b62ce 13168 1 /*search_cwd*/));
6ac97d4c
DE
13169 if (abfd != NULL)
13170 return abfd;
13171
13172 /* Work around upstream bug 15652.
13173 http://sourceware.org/bugzilla/show_bug.cgi?id=15652
13174 [Whether that's a "bug" is debatable, but it is getting in our way.]
13175 We have no real idea where the dwp file is, because gdb's realpath-ing
13176 of the executable's path may have discarded the needed info.
13177 [IWBN if the dwp file name was recorded in the executable, akin to
13178 .gnu_debuglink, but that doesn't exist yet.]
13179 Strip the directory from FILE_NAME and search again. */
13180 if (*debug_file_directory != '\0')
13181 {
13182 /* Don't implicitly search the current directory here.
13183 If the user wants to search "." to handle this case,
13184 it must be added to debug-file-directory. */
ed2dc618
SM
13185 return try_open_dwop_file (dwarf2_per_objfile,
13186 lbasename (file_name), 1 /*is_dwp*/,
6ac97d4c
DE
13187 0 /*search_cwd*/);
13188 }
13189
13190 return NULL;
ab5088bf
DE
13191}
13192
80626a55
DE
13193/* Initialize the use of the DWP file for the current objfile.
13194 By convention the name of the DWP file is ${objfile}.dwp.
13195 The result is NULL if it can't be found. */
a766d390 13196
80626a55 13197static struct dwp_file *
ed2dc618 13198open_and_init_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
80626a55
DE
13199{
13200 struct objfile *objfile = dwarf2_per_objfile->objfile;
13201 struct dwp_file *dwp_file;
80626a55 13202
82bf32bc
JK
13203 /* Try to find first .dwp for the binary file before any symbolic links
13204 resolving. */
6c447423
DE
13205
13206 /* If the objfile is a debug file, find the name of the real binary
13207 file and get the name of dwp file from there. */
d721ba37 13208 std::string dwp_name;
6c447423
DE
13209 if (objfile->separate_debug_objfile_backlink != NULL)
13210 {
13211 struct objfile *backlink = objfile->separate_debug_objfile_backlink;
13212 const char *backlink_basename = lbasename (backlink->original_name);
6c447423 13213
d721ba37 13214 dwp_name = ldirname (objfile->original_name) + SLASH_STRING + backlink_basename;
6c447423
DE
13215 }
13216 else
d721ba37
PA
13217 dwp_name = objfile->original_name;
13218
13219 dwp_name += ".dwp";
80626a55 13220
ed2dc618 13221 gdb_bfd_ref_ptr dbfd (open_dwp_file (dwarf2_per_objfile, dwp_name.c_str ()));
82bf32bc
JK
13222 if (dbfd == NULL
13223 && strcmp (objfile->original_name, objfile_name (objfile)) != 0)
13224 {
13225 /* Try to find .dwp for the binary file after gdb_realpath resolving. */
d721ba37
PA
13226 dwp_name = objfile_name (objfile);
13227 dwp_name += ".dwp";
ed2dc618 13228 dbfd = open_dwp_file (dwarf2_per_objfile, dwp_name.c_str ());
82bf32bc
JK
13229 }
13230
80626a55
DE
13231 if (dbfd == NULL)
13232 {
b4f54984 13233 if (dwarf_read_debug)
d721ba37 13234 fprintf_unfiltered (gdb_stdlog, "DWP file not found: %s\n", dwp_name.c_str ());
80626a55 13235 return NULL;
3019eac3 13236 }
80626a55 13237 dwp_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_file);
192b62ce
TT
13238 dwp_file->name = bfd_get_filename (dbfd.get ());
13239 dwp_file->dbfd = dbfd.release ();
c906108c 13240
80626a55 13241 /* +1: section 0 is unused */
192b62ce 13242 dwp_file->num_sections = bfd_count_sections (dwp_file->dbfd) + 1;
80626a55
DE
13243 dwp_file->elf_sections =
13244 OBSTACK_CALLOC (&objfile->objfile_obstack,
13245 dwp_file->num_sections, asection *);
13246
192b62ce
TT
13247 bfd_map_over_sections (dwp_file->dbfd, dwarf2_locate_common_dwp_sections,
13248 dwp_file);
80626a55 13249
ed2dc618 13250 dwp_file->cus = create_dwp_hash_table (dwarf2_per_objfile, dwp_file, 0);
80626a55 13251
ed2dc618 13252 dwp_file->tus = create_dwp_hash_table (dwarf2_per_objfile, dwp_file, 1);
80626a55 13253
73869dc2 13254 /* The DWP file version is stored in the hash table. Oh well. */
08302ed2
DE
13255 if (dwp_file->cus && dwp_file->tus
13256 && dwp_file->cus->version != dwp_file->tus->version)
73869dc2
DE
13257 {
13258 /* Technically speaking, we should try to limp along, but this is
fbcbc3fd 13259 pretty bizarre. We use pulongest here because that's the established
4d65956b 13260 portability solution (e.g, we cannot use %u for uint32_t). */
fbcbc3fd
DE
13261 error (_("Dwarf Error: DWP file CU version %s doesn't match"
13262 " TU version %s [in DWP file %s]"),
13263 pulongest (dwp_file->cus->version),
d721ba37 13264 pulongest (dwp_file->tus->version), dwp_name.c_str ());
73869dc2 13265 }
08302ed2
DE
13266
13267 if (dwp_file->cus)
13268 dwp_file->version = dwp_file->cus->version;
13269 else if (dwp_file->tus)
13270 dwp_file->version = dwp_file->tus->version;
13271 else
13272 dwp_file->version = 2;
73869dc2
DE
13273
13274 if (dwp_file->version == 2)
192b62ce
TT
13275 bfd_map_over_sections (dwp_file->dbfd, dwarf2_locate_v2_dwp_sections,
13276 dwp_file);
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 }
13304 return dwarf2_per_objfile->dwp_file;
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))
d389af10 13586 complaint (&symfile_complaints,
9d8780f0
SM
13587 _("DIE %s and its abstract origin %s have different tags"),
13588 sect_offset_str (die->sect_off),
13589 sect_offset_str (origin_die->sect_off));
d389af10 13590
791afaa2 13591 std::vector<sect_offset> offsets;
d389af10 13592
3ea89b92
PMR
13593 for (child_die = die->child;
13594 child_die && child_die->tag;
13595 child_die = sibling_die (child_die))
13596 {
13597 struct die_info *child_origin_die;
13598 struct dwarf2_cu *child_origin_cu;
13599
13600 /* We are trying to process concrete instance entries:
216f72a1 13601 DW_TAG_call_site DIEs indeed have a DW_AT_abstract_origin tag, but
3ea89b92
PMR
13602 it's not relevant to our analysis here. i.e. detecting DIEs that are
13603 present in the abstract instance but not referenced in the concrete
13604 one. */
216f72a1
JK
13605 if (child_die->tag == DW_TAG_call_site
13606 || child_die->tag == DW_TAG_GNU_call_site)
3ea89b92
PMR
13607 continue;
13608
c38f313d
DJ
13609 /* For each CHILD_DIE, find the corresponding child of
13610 ORIGIN_DIE. If there is more than one layer of
13611 DW_AT_abstract_origin, follow them all; there shouldn't be,
13612 but GCC versions at least through 4.4 generate this (GCC PR
13613 40573). */
3ea89b92
PMR
13614 child_origin_die = child_die;
13615 child_origin_cu = cu;
c38f313d
DJ
13616 while (1)
13617 {
cd02d79d
PA
13618 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
13619 child_origin_cu);
c38f313d
DJ
13620 if (attr == NULL)
13621 break;
cd02d79d
PA
13622 child_origin_die = follow_die_ref (child_origin_die, attr,
13623 &child_origin_cu);
c38f313d
DJ
13624 }
13625
d389af10
JK
13626 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
13627 counterpart may exist. */
c38f313d 13628 if (child_origin_die != child_die)
d389af10 13629 {
edb3359d
DJ
13630 if (child_die->tag != child_origin_die->tag
13631 && !(child_die->tag == DW_TAG_inlined_subroutine
13632 && child_origin_die->tag == DW_TAG_subprogram))
d389af10 13633 complaint (&symfile_complaints,
9d8780f0 13634 _("Child DIE %s and its abstract origin %s have "
9c541725 13635 "different tags"),
9d8780f0
SM
13636 sect_offset_str (child_die->sect_off),
13637 sect_offset_str (child_origin_die->sect_off));
c38f313d
DJ
13638 if (child_origin_die->parent != origin_die)
13639 complaint (&symfile_complaints,
9d8780f0 13640 _("Child DIE %s and its abstract origin %s have "
9c541725 13641 "different parents"),
9d8780f0
SM
13642 sect_offset_str (child_die->sect_off),
13643 sect_offset_str (child_origin_die->sect_off));
c38f313d 13644 else
791afaa2 13645 offsets.push_back (child_origin_die->sect_off);
d389af10 13646 }
d389af10 13647 }
791afaa2
TT
13648 std::sort (offsets.begin (), offsets.end ());
13649 sect_offset *offsets_end = offsets.data () + offsets.size ();
13650 for (offsetp = offsets.data () + 1; offsetp < offsets_end; offsetp++)
9c541725 13651 if (offsetp[-1] == *offsetp)
3e43a32a 13652 complaint (&symfile_complaints,
9d8780f0
SM
13653 _("Multiple children of DIE %s refer "
13654 "to DIE %s as their abstract origin"),
13655 sect_offset_str (die->sect_off), sect_offset_str (*offsetp));
d389af10 13656
791afaa2 13657 offsetp = offsets.data ();
d389af10
JK
13658 origin_child_die = origin_die->child;
13659 while (origin_child_die && origin_child_die->tag)
13660 {
13661 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
b64f50a1 13662 while (offsetp < offsets_end
9c541725 13663 && *offsetp < origin_child_die->sect_off)
d389af10 13664 offsetp++;
b64f50a1 13665 if (offsetp >= offsets_end
9c541725 13666 || *offsetp > origin_child_die->sect_off)
d389af10 13667 {
adde2bff
DE
13668 /* Found that ORIGIN_CHILD_DIE is really not referenced.
13669 Check whether we're already processing ORIGIN_CHILD_DIE.
13670 This can happen with mutually referenced abstract_origins.
13671 PR 16581. */
13672 if (!origin_child_die->in_process)
13673 process_die (origin_child_die, origin_cu);
d389af10
JK
13674 }
13675 origin_child_die = sibling_die (origin_child_die);
13676 }
cd02d79d 13677 origin_cu->list_in_scope = origin_previous_list_in_scope;
d389af10
JK
13678}
13679
c906108c 13680static void
e7c27a73 13681read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 13682{
518817b3 13683 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
3e29f34a 13684 struct gdbarch *gdbarch = get_objfile_arch (objfile);
fe978cb0 13685 struct context_stack *newobj;
c906108c
SS
13686 CORE_ADDR lowpc;
13687 CORE_ADDR highpc;
13688 struct die_info *child_die;
edb3359d 13689 struct attribute *attr, *call_line, *call_file;
15d034d0 13690 const char *name;
e142c38c 13691 CORE_ADDR baseaddr;
801e3a5b 13692 struct block *block;
edb3359d 13693 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
2f4732b0 13694 std::vector<struct symbol *> template_args;
34eaf542 13695 struct template_symbol *templ_func = NULL;
edb3359d
DJ
13696
13697 if (inlined_func)
13698 {
13699 /* If we do not have call site information, we can't show the
13700 caller of this inlined function. That's too confusing, so
13701 only use the scope for local variables. */
13702 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
13703 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
13704 if (call_line == NULL || call_file == NULL)
13705 {
13706 read_lexical_block_scope (die, cu);
13707 return;
13708 }
13709 }
c906108c 13710
e142c38c
DJ
13711 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
13712
94af9270 13713 name = dwarf2_name (die, cu);
c906108c 13714
e8d05480
JB
13715 /* Ignore functions with missing or empty names. These are actually
13716 illegal according to the DWARF standard. */
13717 if (name == NULL)
13718 {
13719 complaint (&symfile_complaints,
9d8780f0
SM
13720 _("missing name for subprogram DIE at %s"),
13721 sect_offset_str (die->sect_off));
e8d05480
JB
13722 return;
13723 }
13724
13725 /* Ignore functions with missing or invalid low and high pc attributes. */
3a2b436a 13726 if (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL)
e385593e 13727 <= PC_BOUNDS_INVALID)
e8d05480 13728 {
ae4d0c03
PM
13729 attr = dwarf2_attr (die, DW_AT_external, cu);
13730 if (!attr || !DW_UNSND (attr))
13731 complaint (&symfile_complaints,
3e43a32a 13732 _("cannot get low and high bounds "
9d8780f0
SM
13733 "for subprogram DIE at %s"),
13734 sect_offset_str (die->sect_off));
e8d05480
JB
13735 return;
13736 }
c906108c 13737
3e29f34a
MR
13738 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13739 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
c906108c 13740
34eaf542
TT
13741 /* If we have any template arguments, then we must allocate a
13742 different sort of symbol. */
13743 for (child_die = die->child; child_die; child_die = sibling_die (child_die))
13744 {
13745 if (child_die->tag == DW_TAG_template_type_param
13746 || child_die->tag == DW_TAG_template_value_param)
13747 {
e623cf5d 13748 templ_func = allocate_template_symbol (objfile);
cf724bc9 13749 templ_func->subclass = SYMBOL_TEMPLATE;
34eaf542
TT
13750 break;
13751 }
13752 }
13753
fe978cb0 13754 newobj = push_context (0, lowpc);
5e2db402
TT
13755 newobj->name = new_symbol (die, read_type_die (die, cu), cu,
13756 (struct symbol *) templ_func);
4c2df51b 13757
4cecd739
DJ
13758 /* If there is a location expression for DW_AT_frame_base, record
13759 it. */
e142c38c 13760 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
4c2df51b 13761 if (attr)
fe978cb0 13762 dwarf2_symbol_mark_computed (attr, newobj->name, cu, 1);
4c2df51b 13763
63e43d3a
PMR
13764 /* If there is a location for the static link, record it. */
13765 newobj->static_link = NULL;
13766 attr = dwarf2_attr (die, DW_AT_static_link, cu);
13767 if (attr)
13768 {
224c3ddb
SM
13769 newobj->static_link
13770 = XOBNEW (&objfile->objfile_obstack, struct dynamic_prop);
63e43d3a
PMR
13771 attr_to_dynamic_prop (attr, die, cu, newobj->static_link);
13772 }
13773
e142c38c 13774 cu->list_in_scope = &local_symbols;
c906108c 13775
639d11d3 13776 if (die->child != NULL)
c906108c 13777 {
639d11d3 13778 child_die = die->child;
c906108c
SS
13779 while (child_die && child_die->tag)
13780 {
34eaf542
TT
13781 if (child_die->tag == DW_TAG_template_type_param
13782 || child_die->tag == DW_TAG_template_value_param)
13783 {
13784 struct symbol *arg = new_symbol (child_die, NULL, cu);
13785
f1078f66 13786 if (arg != NULL)
2f4732b0 13787 template_args.push_back (arg);
34eaf542
TT
13788 }
13789 else
13790 process_die (child_die, cu);
c906108c
SS
13791 child_die = sibling_die (child_die);
13792 }
13793 }
13794
d389af10
JK
13795 inherit_abstract_dies (die, cu);
13796
4a811a97
UW
13797 /* If we have a DW_AT_specification, we might need to import using
13798 directives from the context of the specification DIE. See the
13799 comment in determine_prefix. */
13800 if (cu->language == language_cplus
13801 && dwarf2_attr (die, DW_AT_specification, cu))
13802 {
13803 struct dwarf2_cu *spec_cu = cu;
13804 struct die_info *spec_die = die_specification (die, &spec_cu);
13805
13806 while (spec_die)
13807 {
13808 child_die = spec_die->child;
13809 while (child_die && child_die->tag)
13810 {
13811 if (child_die->tag == DW_TAG_imported_module)
13812 process_die (child_die, spec_cu);
13813 child_die = sibling_die (child_die);
13814 }
13815
13816 /* In some cases, GCC generates specification DIEs that
13817 themselves contain DW_AT_specification attributes. */
13818 spec_die = die_specification (spec_die, &spec_cu);
13819 }
13820 }
13821
fe978cb0 13822 newobj = pop_context ();
c906108c 13823 /* Make a block for the local symbols within. */
fe978cb0 13824 block = finish_block (newobj->name, &local_symbols, newobj->old_blocks,
63e43d3a 13825 newobj->static_link, lowpc, highpc);
801e3a5b 13826
df8a16a1 13827 /* For C++, set the block's scope. */
45280282
IB
13828 if ((cu->language == language_cplus
13829 || cu->language == language_fortran
c44af4eb
TT
13830 || cu->language == language_d
13831 || cu->language == language_rust)
4d4ec4e5 13832 && cu->processing_has_namespace_info)
195a3f6c
TT
13833 block_set_scope (block, determine_prefix (die, cu),
13834 &objfile->objfile_obstack);
df8a16a1 13835
801e3a5b
JB
13836 /* If we have address ranges, record them. */
13837 dwarf2_record_block_ranges (die, block, baseaddr, cu);
6e70227d 13838
fe978cb0 13839 gdbarch_make_symbol_special (gdbarch, newobj->name, objfile);
3e29f34a 13840
34eaf542 13841 /* Attach template arguments to function. */
2f4732b0 13842 if (!template_args.empty ())
34eaf542
TT
13843 {
13844 gdb_assert (templ_func != NULL);
13845
2f4732b0 13846 templ_func->n_template_arguments = template_args.size ();
34eaf542 13847 templ_func->template_arguments
8d749320
SM
13848 = XOBNEWVEC (&objfile->objfile_obstack, struct symbol *,
13849 templ_func->n_template_arguments);
34eaf542 13850 memcpy (templ_func->template_arguments,
2f4732b0 13851 template_args.data (),
34eaf542 13852 (templ_func->n_template_arguments * sizeof (struct symbol *)));
34eaf542
TT
13853 }
13854
208d8187
JB
13855 /* In C++, we can have functions nested inside functions (e.g., when
13856 a function declares a class that has methods). This means that
13857 when we finish processing a function scope, we may need to go
13858 back to building a containing block's symbol lists. */
fe978cb0 13859 local_symbols = newobj->locals;
22cee43f 13860 local_using_directives = newobj->local_using_directives;
208d8187 13861
921e78cf
JB
13862 /* If we've finished processing a top-level function, subsequent
13863 symbols go in the file symbol list. */
13864 if (outermost_context_p ())
e142c38c 13865 cu->list_in_scope = &file_symbols;
c906108c
SS
13866}
13867
13868/* Process all the DIES contained within a lexical block scope. Start
13869 a new scope, process the dies, and then close the scope. */
13870
13871static void
e7c27a73 13872read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 13873{
518817b3 13874 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
3e29f34a 13875 struct gdbarch *gdbarch = get_objfile_arch (objfile);
fe978cb0 13876 struct context_stack *newobj;
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
SS
13904
13905 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);
fe978cb0 13916 newobj = pop_context ();
c906108c 13917
22cee43f 13918 if (local_symbols != NULL || local_using_directives != NULL)
c906108c 13919 {
801e3a5b 13920 struct block *block
63e43d3a 13921 = finish_block (0, &local_symbols, newobj->old_blocks, NULL,
fe978cb0 13922 newobj->start_addr, highpc);
801e3a5b
JB
13923
13924 /* Note that recording ranges after traversing children, as we
13925 do here, means that recording a parent's ranges entails
13926 walking across all its children's ranges as they appear in
13927 the address map, which is quadratic behavior.
13928
13929 It would be nicer to record the parent's ranges before
13930 traversing its children, simply overriding whatever you find
13931 there. But since we don't even decide whether to create a
13932 block until after we've traversed its children, that's hard
13933 to do. */
13934 dwarf2_record_block_ranges (die, block, baseaddr, cu);
c906108c 13935 }
fe978cb0 13936 local_symbols = newobj->locals;
22cee43f 13937 local_using_directives = newobj->local_using_directives;
c906108c
SS
13938}
13939
216f72a1 13940/* Read in DW_TAG_call_site and insert it to CU->call_site_htab. */
96408a79
SA
13941
13942static void
13943read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
13944{
518817b3 13945 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
96408a79
SA
13946 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13947 CORE_ADDR pc, baseaddr;
13948 struct attribute *attr;
13949 struct call_site *call_site, call_site_local;
13950 void **slot;
13951 int nparams;
13952 struct die_info *child_die;
13953
13954 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
13955
216f72a1
JK
13956 attr = dwarf2_attr (die, DW_AT_call_return_pc, cu);
13957 if (attr == NULL)
13958 {
13959 /* This was a pre-DWARF-5 GNU extension alias
13960 for DW_AT_call_return_pc. */
13961 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
13962 }
96408a79
SA
13963 if (!attr)
13964 {
13965 complaint (&symfile_complaints,
216f72a1 13966 _("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 {
13982 complaint (&symfile_complaints,
216f72a1 13983 _("Duplicate PC %s for DW_TAG_call_site "
9d8780f0
SM
13984 "DIE %s [in module %s]"),
13985 paddress (gdbarch, pc), sect_offset_str (die->sect_off),
4262abfb 13986 objfile_name (objfile));
96408a79
SA
13987 return;
13988 }
13989
13990 /* Count parameters at the caller. */
13991
13992 nparams = 0;
13993 for (child_die = die->child; child_die && child_die->tag;
13994 child_die = sibling_die (child_die))
13995 {
216f72a1
JK
13996 if (child_die->tag != DW_TAG_call_site_parameter
13997 && child_die->tag != DW_TAG_GNU_call_site_parameter)
96408a79
SA
13998 {
13999 complaint (&symfile_complaints,
216f72a1 14000 _("Tag %d is not DW_TAG_call_site_parameter in "
9d8780f0
SM
14001 "DW_TAG_call_site child DIE %s [in module %s]"),
14002 child_die->tag, sect_offset_str (child_die->sect_off),
4262abfb 14003 objfile_name (objfile));
96408a79
SA
14004 continue;
14005 }
14006
14007 nparams++;
14008 }
14009
224c3ddb
SM
14010 call_site
14011 = ((struct call_site *)
14012 obstack_alloc (&objfile->objfile_obstack,
14013 sizeof (*call_site)
14014 + (sizeof (*call_site->parameter) * (nparams - 1))));
96408a79
SA
14015 *slot = call_site;
14016 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
14017 call_site->pc = pc;
14018
216f72a1
JK
14019 if (dwarf2_flag_true_p (die, DW_AT_call_tail_call, cu)
14020 || dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
96408a79
SA
14021 {
14022 struct die_info *func_die;
14023
14024 /* Skip also over DW_TAG_inlined_subroutine. */
14025 for (func_die = die->parent;
14026 func_die && func_die->tag != DW_TAG_subprogram
14027 && func_die->tag != DW_TAG_subroutine_type;
14028 func_die = func_die->parent);
14029
216f72a1
JK
14030 /* DW_AT_call_all_calls is a superset
14031 of DW_AT_call_all_tail_calls. */
96408a79 14032 if (func_die
216f72a1 14033 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_calls, cu)
96408a79 14034 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
216f72a1 14035 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_tail_calls, cu)
96408a79
SA
14036 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
14037 {
14038 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
14039 not complete. But keep CALL_SITE for look ups via call_site_htab,
14040 both the initial caller containing the real return address PC and
14041 the final callee containing the current PC of a chain of tail
14042 calls do not need to have the tail call list complete. But any
14043 function candidate for a virtual tail call frame searched via
14044 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
14045 determined unambiguously. */
14046 }
14047 else
14048 {
14049 struct type *func_type = NULL;
14050
14051 if (func_die)
14052 func_type = get_die_type (func_die, cu);
14053 if (func_type != NULL)
14054 {
14055 gdb_assert (TYPE_CODE (func_type) == TYPE_CODE_FUNC);
14056
14057 /* Enlist this call site to the function. */
14058 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
14059 TYPE_TAIL_CALL_LIST (func_type) = call_site;
14060 }
14061 else
14062 complaint (&symfile_complaints,
216f72a1 14063 _("Cannot find function owning DW_TAG_call_site "
9d8780f0
SM
14064 "DIE %s [in module %s]"),
14065 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79
SA
14066 }
14067 }
14068
216f72a1
JK
14069 attr = dwarf2_attr (die, DW_AT_call_target, cu);
14070 if (attr == NULL)
14071 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
14072 if (attr == NULL)
14073 attr = dwarf2_attr (die, DW_AT_call_origin, cu);
96408a79 14074 if (attr == NULL)
216f72a1
JK
14075 {
14076 /* This was a pre-DWARF-5 GNU extension alias for DW_AT_call_origin. */
14077 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
14078 }
96408a79
SA
14079 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
14080 if (!attr || (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0))
14081 /* Keep NULL DWARF_BLOCK. */;
14082 else if (attr_form_is_block (attr))
14083 {
14084 struct dwarf2_locexpr_baton *dlbaton;
14085
8d749320 14086 dlbaton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
96408a79
SA
14087 dlbaton->data = DW_BLOCK (attr)->data;
14088 dlbaton->size = DW_BLOCK (attr)->size;
14089 dlbaton->per_cu = cu->per_cu;
14090
14091 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
14092 }
7771576e 14093 else if (attr_form_is_ref (attr))
96408a79 14094 {
96408a79
SA
14095 struct dwarf2_cu *target_cu = cu;
14096 struct die_info *target_die;
14097
ac9ec31b 14098 target_die = follow_die_ref (die, attr, &target_cu);
518817b3 14099 gdb_assert (target_cu->per_cu->dwarf2_per_objfile->objfile == objfile);
96408a79
SA
14100 if (die_is_declaration (target_die, target_cu))
14101 {
7d45c7c3 14102 const char *target_physname;
9112db09
JK
14103
14104 /* Prefer the mangled name; otherwise compute the demangled one. */
73b9be8b 14105 target_physname = dw2_linkage_name (target_die, target_cu);
7d45c7c3 14106 if (target_physname == NULL)
9112db09 14107 target_physname = dwarf2_physname (NULL, target_die, target_cu);
96408a79
SA
14108 if (target_physname == NULL)
14109 complaint (&symfile_complaints,
216f72a1 14110 _("DW_AT_call_target target DIE has invalid "
9d8780f0
SM
14111 "physname, for referencing DIE %s [in module %s]"),
14112 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79 14113 else
7d455152 14114 SET_FIELD_PHYSNAME (call_site->target, target_physname);
96408a79
SA
14115 }
14116 else
14117 {
14118 CORE_ADDR lowpc;
14119
14120 /* DW_AT_entry_pc should be preferred. */
3a2b436a 14121 if (dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL)
e385593e 14122 <= PC_BOUNDS_INVALID)
96408a79 14123 complaint (&symfile_complaints,
216f72a1 14124 _("DW_AT_call_target target DIE has invalid "
9d8780f0
SM
14125 "low pc, for referencing DIE %s [in module %s]"),
14126 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79 14127 else
3e29f34a
MR
14128 {
14129 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
14130 SET_FIELD_PHYSADDR (call_site->target, lowpc);
14131 }
96408a79
SA
14132 }
14133 }
14134 else
14135 complaint (&symfile_complaints,
216f72a1 14136 _("DW_TAG_call_site DW_AT_call_target is neither "
9d8780f0
SM
14137 "block nor reference, for DIE %s [in module %s]"),
14138 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79
SA
14139
14140 call_site->per_cu = cu->per_cu;
14141
14142 for (child_die = die->child;
14143 child_die && child_die->tag;
14144 child_die = sibling_die (child_die))
14145 {
96408a79 14146 struct call_site_parameter *parameter;
1788b2d3 14147 struct attribute *loc, *origin;
96408a79 14148
216f72a1
JK
14149 if (child_die->tag != DW_TAG_call_site_parameter
14150 && child_die->tag != DW_TAG_GNU_call_site_parameter)
96408a79
SA
14151 {
14152 /* Already printed the complaint above. */
14153 continue;
14154 }
14155
14156 gdb_assert (call_site->parameter_count < nparams);
14157 parameter = &call_site->parameter[call_site->parameter_count];
14158
1788b2d3
JK
14159 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
14160 specifies DW_TAG_formal_parameter. Value of the data assumed for the
216f72a1 14161 register is contained in DW_AT_call_value. */
96408a79 14162
24c5c679 14163 loc = dwarf2_attr (child_die, DW_AT_location, cu);
216f72a1
JK
14164 origin = dwarf2_attr (child_die, DW_AT_call_parameter, cu);
14165 if (origin == NULL)
14166 {
14167 /* This was a pre-DWARF-5 GNU extension alias
14168 for DW_AT_call_parameter. */
14169 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
14170 }
7771576e 14171 if (loc == NULL && origin != NULL && attr_form_is_ref (origin))
1788b2d3 14172 {
1788b2d3 14173 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
9c541725
PA
14174
14175 sect_offset sect_off
14176 = (sect_offset) dwarf2_get_ref_die_offset (origin);
14177 if (!offset_in_cu_p (&cu->header, sect_off))
d76b7dbc
JK
14178 {
14179 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
14180 binding can be done only inside one CU. Such referenced DIE
14181 therefore cannot be even moved to DW_TAG_partial_unit. */
14182 complaint (&symfile_complaints,
216f72a1 14183 _("DW_AT_call_parameter offset is not in CU for "
9d8780f0
SM
14184 "DW_TAG_call_site child DIE %s [in module %s]"),
14185 sect_offset_str (child_die->sect_off),
9c541725 14186 objfile_name (objfile));
d76b7dbc
JK
14187 continue;
14188 }
9c541725
PA
14189 parameter->u.param_cu_off
14190 = (cu_offset) (sect_off - cu->header.sect_off);
1788b2d3
JK
14191 }
14192 else if (loc == NULL || origin != NULL || !attr_form_is_block (loc))
96408a79
SA
14193 {
14194 complaint (&symfile_complaints,
14195 _("No DW_FORM_block* DW_AT_location for "
9d8780f0
SM
14196 "DW_TAG_call_site child DIE %s [in module %s]"),
14197 sect_offset_str (child_die->sect_off), objfile_name (objfile));
96408a79
SA
14198 continue;
14199 }
24c5c679 14200 else
96408a79 14201 {
24c5c679
JK
14202 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
14203 (DW_BLOCK (loc)->data, &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size]);
14204 if (parameter->u.dwarf_reg != -1)
14205 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
14206 else if (dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (loc)->data,
14207 &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size],
14208 &parameter->u.fb_offset))
14209 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
14210 else
14211 {
14212 complaint (&symfile_complaints,
14213 _("Only single DW_OP_reg or DW_OP_fbreg is supported "
14214 "for DW_FORM_block* DW_AT_location is supported for "
9d8780f0 14215 "DW_TAG_call_site child DIE %s "
24c5c679 14216 "[in module %s]"),
9d8780f0 14217 sect_offset_str (child_die->sect_off),
9c541725 14218 objfile_name (objfile));
24c5c679
JK
14219 continue;
14220 }
96408a79
SA
14221 }
14222
216f72a1
JK
14223 attr = dwarf2_attr (child_die, DW_AT_call_value, cu);
14224 if (attr == NULL)
14225 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
96408a79
SA
14226 if (!attr_form_is_block (attr))
14227 {
14228 complaint (&symfile_complaints,
216f72a1 14229 _("No DW_FORM_block* DW_AT_call_value for "
9d8780f0
SM
14230 "DW_TAG_call_site child DIE %s [in module %s]"),
14231 sect_offset_str (child_die->sect_off),
9c541725 14232 objfile_name (objfile));
96408a79
SA
14233 continue;
14234 }
14235 parameter->value = DW_BLOCK (attr)->data;
14236 parameter->value_size = DW_BLOCK (attr)->size;
14237
14238 /* Parameters are not pre-cleared by memset above. */
14239 parameter->data_value = NULL;
14240 parameter->data_value_size = 0;
14241 call_site->parameter_count++;
14242
216f72a1
JK
14243 attr = dwarf2_attr (child_die, DW_AT_call_data_value, cu);
14244 if (attr == NULL)
14245 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
96408a79
SA
14246 if (attr)
14247 {
14248 if (!attr_form_is_block (attr))
14249 complaint (&symfile_complaints,
216f72a1 14250 _("No DW_FORM_block* DW_AT_call_data_value for "
9d8780f0
SM
14251 "DW_TAG_call_site child DIE %s [in module %s]"),
14252 sect_offset_str (child_die->sect_off),
9c541725 14253 objfile_name (objfile));
96408a79
SA
14254 else
14255 {
14256 parameter->data_value = DW_BLOCK (attr)->data;
14257 parameter->data_value_size = DW_BLOCK (attr)->size;
14258 }
14259 }
14260 }
14261}
14262
71a3c369
TT
14263/* Helper function for read_variable. If DIE represents a virtual
14264 table, then return the type of the concrete object that is
14265 associated with the virtual table. Otherwise, return NULL. */
14266
14267static struct type *
14268rust_containing_type (struct die_info *die, struct dwarf2_cu *cu)
14269{
14270 struct attribute *attr = dwarf2_attr (die, DW_AT_type, cu);
14271 if (attr == NULL)
14272 return NULL;
14273
14274 /* Find the type DIE. */
14275 struct die_info *type_die = NULL;
14276 struct dwarf2_cu *type_cu = cu;
14277
14278 if (attr_form_is_ref (attr))
14279 type_die = follow_die_ref (die, attr, &type_cu);
14280 if (type_die == NULL)
14281 return NULL;
14282
14283 if (dwarf2_attr (type_die, DW_AT_containing_type, type_cu) == NULL)
14284 return NULL;
14285 return die_containing_type (type_die, type_cu);
14286}
14287
14288/* Read a variable (DW_TAG_variable) DIE and create a new symbol. */
14289
14290static void
14291read_variable (struct die_info *die, struct dwarf2_cu *cu)
14292{
14293 struct rust_vtable_symbol *storage = NULL;
14294
14295 if (cu->language == language_rust)
14296 {
14297 struct type *containing_type = rust_containing_type (die, cu);
14298
14299 if (containing_type != NULL)
14300 {
518817b3 14301 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
71a3c369
TT
14302
14303 storage = OBSTACK_ZALLOC (&objfile->objfile_obstack,
14304 struct rust_vtable_symbol);
14305 initialize_objfile_symbol (storage);
14306 storage->concrete_type = containing_type;
cf724bc9 14307 storage->subclass = SYMBOL_RUST_VTABLE;
71a3c369
TT
14308 }
14309 }
14310
5e2db402 14311 new_symbol (die, NULL, cu, storage);
71a3c369
TT
14312}
14313
43988095
JK
14314/* Call CALLBACK from DW_AT_ranges attribute value OFFSET
14315 reading .debug_rnglists.
14316 Callback's type should be:
14317 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
14318 Return true if the attributes are present and valid, otherwise,
14319 return false. */
14320
14321template <typename Callback>
14322static bool
14323dwarf2_rnglists_process (unsigned offset, struct dwarf2_cu *cu,
14324 Callback &&callback)
14325{
ed2dc618 14326 struct dwarf2_per_objfile *dwarf2_per_objfile
518817b3 14327 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 14328 struct objfile *objfile = dwarf2_per_objfile->objfile;
43988095 14329 bfd *obfd = objfile->obfd;
43988095
JK
14330 /* Base address selection entry. */
14331 CORE_ADDR base;
14332 int found_base;
43988095 14333 const gdb_byte *buffer;
43988095
JK
14334 CORE_ADDR baseaddr;
14335 bool overflow = false;
14336
14337 found_base = cu->base_known;
14338 base = cu->base_address;
14339
14340 dwarf2_read_section (objfile, &dwarf2_per_objfile->rnglists);
14341 if (offset >= dwarf2_per_objfile->rnglists.size)
14342 {
14343 complaint (&symfile_complaints,
14344 _("Offset %d out of bounds for DW_AT_ranges attribute"),
14345 offset);
14346 return false;
14347 }
14348 buffer = dwarf2_per_objfile->rnglists.buffer + offset;
14349
14350 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
14351
14352 while (1)
14353 {
7814882a
JK
14354 /* Initialize it due to a false compiler warning. */
14355 CORE_ADDR range_beginning = 0, range_end = 0;
43988095
JK
14356 const gdb_byte *buf_end = (dwarf2_per_objfile->rnglists.buffer
14357 + dwarf2_per_objfile->rnglists.size);
14358 unsigned int bytes_read;
14359
14360 if (buffer == buf_end)
14361 {
14362 overflow = true;
14363 break;
14364 }
14365 const auto rlet = static_cast<enum dwarf_range_list_entry>(*buffer++);
14366 switch (rlet)
14367 {
14368 case DW_RLE_end_of_list:
14369 break;
14370 case DW_RLE_base_address:
14371 if (buffer + cu->header.addr_size > buf_end)
14372 {
14373 overflow = true;
14374 break;
14375 }
14376 base = read_address (obfd, buffer, cu, &bytes_read);
14377 found_base = 1;
14378 buffer += bytes_read;
14379 break;
14380 case DW_RLE_start_length:
14381 if (buffer + cu->header.addr_size > buf_end)
14382 {
14383 overflow = true;
14384 break;
14385 }
14386 range_beginning = read_address (obfd, buffer, cu, &bytes_read);
14387 buffer += bytes_read;
14388 range_end = (range_beginning
14389 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
14390 buffer += bytes_read;
14391 if (buffer > buf_end)
14392 {
14393 overflow = true;
14394 break;
14395 }
14396 break;
14397 case DW_RLE_offset_pair:
14398 range_beginning = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14399 buffer += bytes_read;
14400 if (buffer > buf_end)
14401 {
14402 overflow = true;
14403 break;
14404 }
14405 range_end = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14406 buffer += bytes_read;
14407 if (buffer > buf_end)
14408 {
14409 overflow = true;
14410 break;
14411 }
14412 break;
14413 case DW_RLE_start_end:
14414 if (buffer + 2 * cu->header.addr_size > buf_end)
14415 {
14416 overflow = true;
14417 break;
14418 }
14419 range_beginning = read_address (obfd, buffer, cu, &bytes_read);
14420 buffer += bytes_read;
14421 range_end = read_address (obfd, buffer, cu, &bytes_read);
14422 buffer += bytes_read;
14423 break;
14424 default:
14425 complaint (&symfile_complaints,
14426 _("Invalid .debug_rnglists data (no base address)"));
14427 return false;
14428 }
14429 if (rlet == DW_RLE_end_of_list || overflow)
14430 break;
14431 if (rlet == DW_RLE_base_address)
14432 continue;
14433
14434 if (!found_base)
14435 {
14436 /* We have no valid base address for the ranges
14437 data. */
14438 complaint (&symfile_complaints,
14439 _("Invalid .debug_rnglists data (no base address)"));
14440 return false;
14441 }
14442
14443 if (range_beginning > range_end)
14444 {
14445 /* Inverted range entries are invalid. */
14446 complaint (&symfile_complaints,
14447 _("Invalid .debug_rnglists data (inverted range)"));
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 {
14463 complaint (&symfile_complaints,
14464 _(".debug_rnglists entry has start address of zero"
14465 " [in module %s]"), objfile_name (objfile));
14466 continue;
14467 }
14468
14469 callback (range_beginning, range_end);
14470 }
14471
14472 if (overflow)
14473 {
14474 complaint (&symfile_complaints,
14475 _("Offset %d is not terminated "
14476 "for DW_AT_ranges attribute"),
14477 offset);
14478 return false;
14479 }
14480
14481 return true;
14482}
14483
14484/* Call CALLBACK from DW_AT_ranges attribute value OFFSET reading .debug_ranges.
14485 Callback's type should be:
14486 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
5f46c5a5 14487 Return 1 if the attributes are present and valid, otherwise, return 0. */
43039443 14488
43988095 14489template <typename Callback>
43039443 14490static int
5f46c5a5 14491dwarf2_ranges_process (unsigned offset, struct dwarf2_cu *cu,
43988095 14492 Callback &&callback)
43039443 14493{
ed2dc618 14494 struct dwarf2_per_objfile *dwarf2_per_objfile
518817b3 14495 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 14496 struct objfile *objfile = dwarf2_per_objfile->objfile;
43039443
JK
14497 struct comp_unit_head *cu_header = &cu->header;
14498 bfd *obfd = objfile->obfd;
14499 unsigned int addr_size = cu_header->addr_size;
14500 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
14501 /* Base address selection entry. */
14502 CORE_ADDR base;
14503 int found_base;
14504 unsigned int dummy;
d521ce57 14505 const gdb_byte *buffer;
ff013f42 14506 CORE_ADDR baseaddr;
43039443 14507
43988095
JK
14508 if (cu_header->version >= 5)
14509 return dwarf2_rnglists_process (offset, cu, callback);
14510
d00adf39
DE
14511 found_base = cu->base_known;
14512 base = cu->base_address;
43039443 14513
be391dca 14514 dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
dce234bc 14515 if (offset >= dwarf2_per_objfile->ranges.size)
43039443
JK
14516 {
14517 complaint (&symfile_complaints,
14518 _("Offset %d out of bounds for DW_AT_ranges attribute"),
14519 offset);
14520 return 0;
14521 }
dce234bc 14522 buffer = dwarf2_per_objfile->ranges.buffer + offset;
43039443 14523
e7030f15 14524 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
ff013f42 14525
43039443
JK
14526 while (1)
14527 {
14528 CORE_ADDR range_beginning, range_end;
14529
14530 range_beginning = read_address (obfd, buffer, cu, &dummy);
14531 buffer += addr_size;
14532 range_end = read_address (obfd, buffer, cu, &dummy);
14533 buffer += addr_size;
14534 offset += 2 * addr_size;
14535
14536 /* An end of list marker is a pair of zero addresses. */
14537 if (range_beginning == 0 && range_end == 0)
14538 /* Found the end of list entry. */
14539 break;
14540
14541 /* Each base address selection entry is a pair of 2 values.
14542 The first is the largest possible address, the second is
14543 the base address. Check for a base address here. */
14544 if ((range_beginning & mask) == mask)
14545 {
28d2bfb9
AB
14546 /* If we found the largest possible address, then we already
14547 have the base address in range_end. */
14548 base = range_end;
43039443
JK
14549 found_base = 1;
14550 continue;
14551 }
14552
14553 if (!found_base)
14554 {
14555 /* We have no valid base address for the ranges
14556 data. */
14557 complaint (&symfile_complaints,
14558 _("Invalid .debug_ranges data (no base address)"));
14559 return 0;
14560 }
14561
9277c30c
UW
14562 if (range_beginning > range_end)
14563 {
14564 /* Inverted range entries are invalid. */
14565 complaint (&symfile_complaints,
14566 _("Invalid .debug_ranges data (inverted range)"));
14567 return 0;
14568 }
14569
14570 /* Empty range entries have no effect. */
14571 if (range_beginning == range_end)
14572 continue;
14573
43039443
JK
14574 range_beginning += base;
14575 range_end += base;
14576
01093045
DE
14577 /* A not-uncommon case of bad debug info.
14578 Don't pollute the addrmap with bad data. */
14579 if (range_beginning + baseaddr == 0
14580 && !dwarf2_per_objfile->has_section_at_zero)
14581 {
14582 complaint (&symfile_complaints,
14583 _(".debug_ranges entry has start address of zero"
4262abfb 14584 " [in module %s]"), objfile_name (objfile));
01093045
DE
14585 continue;
14586 }
14587
5f46c5a5
JK
14588 callback (range_beginning, range_end);
14589 }
14590
14591 return 1;
14592}
14593
14594/* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
14595 Return 1 if the attributes are present and valid, otherwise, return 0.
14596 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
14597
14598static int
14599dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
14600 CORE_ADDR *high_return, struct dwarf2_cu *cu,
14601 struct partial_symtab *ranges_pst)
14602{
518817b3 14603 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
5f46c5a5
JK
14604 struct gdbarch *gdbarch = get_objfile_arch (objfile);
14605 const CORE_ADDR baseaddr = ANOFFSET (objfile->section_offsets,
14606 SECT_OFF_TEXT (objfile));
14607 int low_set = 0;
14608 CORE_ADDR low = 0;
14609 CORE_ADDR high = 0;
14610 int retval;
14611
14612 retval = dwarf2_ranges_process (offset, cu,
14613 [&] (CORE_ADDR range_beginning, CORE_ADDR range_end)
14614 {
9277c30c 14615 if (ranges_pst != NULL)
3e29f34a
MR
14616 {
14617 CORE_ADDR lowpc;
14618 CORE_ADDR highpc;
14619
14620 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch,
14621 range_beginning + baseaddr);
14622 highpc = gdbarch_adjust_dwarf2_addr (gdbarch,
14623 range_end + baseaddr);
14624 addrmap_set_empty (objfile->psymtabs_addrmap, lowpc, highpc - 1,
14625 ranges_pst);
14626 }
ff013f42 14627
43039443
JK
14628 /* FIXME: This is recording everything as a low-high
14629 segment of consecutive addresses. We should have a
14630 data structure for discontiguous block ranges
14631 instead. */
14632 if (! low_set)
14633 {
14634 low = range_beginning;
14635 high = range_end;
14636 low_set = 1;
14637 }
14638 else
14639 {
14640 if (range_beginning < low)
14641 low = range_beginning;
14642 if (range_end > high)
14643 high = range_end;
14644 }
5f46c5a5
JK
14645 });
14646 if (!retval)
14647 return 0;
43039443
JK
14648
14649 if (! low_set)
14650 /* If the first entry is an end-of-list marker, the range
14651 describes an empty scope, i.e. no instructions. */
14652 return 0;
14653
14654 if (low_return)
14655 *low_return = low;
14656 if (high_return)
14657 *high_return = high;
14658 return 1;
14659}
14660
3a2b436a
JK
14661/* Get low and high pc attributes from a die. See enum pc_bounds_kind
14662 definition for the return value. *LOWPC and *HIGHPC are set iff
e385593e 14663 neither PC_BOUNDS_NOT_PRESENT nor PC_BOUNDS_INVALID are returned. */
380bca97 14664
3a2b436a 14665static enum pc_bounds_kind
af34e669 14666dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
d85a05f0
DJ
14667 CORE_ADDR *highpc, struct dwarf2_cu *cu,
14668 struct partial_symtab *pst)
c906108c 14669{
518817b3
SM
14670 struct dwarf2_per_objfile *dwarf2_per_objfile
14671 = cu->per_cu->dwarf2_per_objfile;
c906108c 14672 struct attribute *attr;
91da1414 14673 struct attribute *attr_high;
af34e669
DJ
14674 CORE_ADDR low = 0;
14675 CORE_ADDR high = 0;
e385593e 14676 enum pc_bounds_kind ret;
c906108c 14677
91da1414
MW
14678 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14679 if (attr_high)
af34e669 14680 {
e142c38c 14681 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
af34e669 14682 if (attr)
91da1414 14683 {
31aa7e4e
JB
14684 low = attr_value_as_address (attr);
14685 high = attr_value_as_address (attr_high);
14686 if (cu->header.version >= 4 && attr_form_is_constant (attr_high))
14687 high += low;
91da1414 14688 }
af34e669
DJ
14689 else
14690 /* Found high w/o low attribute. */
e385593e 14691 return PC_BOUNDS_INVALID;
af34e669
DJ
14692
14693 /* Found consecutive range of addresses. */
3a2b436a 14694 ret = PC_BOUNDS_HIGH_LOW;
af34e669 14695 }
c906108c 14696 else
af34e669 14697 {
e142c38c 14698 attr = dwarf2_attr (die, DW_AT_ranges, cu);
af34e669
DJ
14699 if (attr != NULL)
14700 {
ab435259
DE
14701 /* DW_AT_ranges_base does not apply to DIEs from the DWO skeleton.
14702 We take advantage of the fact that DW_AT_ranges does not appear
14703 in DW_TAG_compile_unit of DWO files. */
14704 int need_ranges_base = die->tag != DW_TAG_compile_unit;
14705 unsigned int ranges_offset = (DW_UNSND (attr)
14706 + (need_ranges_base
14707 ? cu->ranges_base
14708 : 0));
2e3cf129 14709
af34e669 14710 /* Value of the DW_AT_ranges attribute is the offset in the
a604369a 14711 .debug_ranges section. */
2e3cf129 14712 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst))
e385593e 14713 return PC_BOUNDS_INVALID;
43039443 14714 /* Found discontinuous range of addresses. */
3a2b436a 14715 ret = PC_BOUNDS_RANGES;
af34e669 14716 }
e385593e
JK
14717 else
14718 return PC_BOUNDS_NOT_PRESENT;
af34e669 14719 }
c906108c 14720
48fbe735 14721 /* partial_die_info::read has also the strict LOW < HIGH requirement. */
9373cf26 14722 if (high <= low)
e385593e 14723 return PC_BOUNDS_INVALID;
c906108c
SS
14724
14725 /* When using the GNU linker, .gnu.linkonce. sections are used to
14726 eliminate duplicate copies of functions and vtables and such.
14727 The linker will arbitrarily choose one and discard the others.
14728 The AT_*_pc values for such functions refer to local labels in
14729 these sections. If the section from that file was discarded, the
14730 labels are not in the output, so the relocs get a value of 0.
14731 If this is a discarded function, mark the pc bounds as invalid,
14732 so that GDB will ignore it. */
72dca2f5 14733 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
e385593e 14734 return PC_BOUNDS_INVALID;
c906108c
SS
14735
14736 *lowpc = low;
96408a79
SA
14737 if (highpc)
14738 *highpc = high;
af34e669 14739 return ret;
c906108c
SS
14740}
14741
b084d499
JB
14742/* Assuming that DIE represents a subprogram DIE or a lexical block, get
14743 its low and high PC addresses. Do nothing if these addresses could not
14744 be determined. Otherwise, set LOWPC to the low address if it is smaller,
14745 and HIGHPC to the high address if greater than HIGHPC. */
14746
14747static void
14748dwarf2_get_subprogram_pc_bounds (struct die_info *die,
14749 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14750 struct dwarf2_cu *cu)
14751{
14752 CORE_ADDR low, high;
14753 struct die_info *child = die->child;
14754
e385593e 14755 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL) >= PC_BOUNDS_RANGES)
b084d499 14756 {
325fac50
PA
14757 *lowpc = std::min (*lowpc, low);
14758 *highpc = std::max (*highpc, high);
b084d499
JB
14759 }
14760
14761 /* If the language does not allow nested subprograms (either inside
14762 subprograms or lexical blocks), we're done. */
14763 if (cu->language != language_ada)
14764 return;
6e70227d 14765
b084d499
JB
14766 /* Check all the children of the given DIE. If it contains nested
14767 subprograms, then check their pc bounds. Likewise, we need to
14768 check lexical blocks as well, as they may also contain subprogram
14769 definitions. */
14770 while (child && child->tag)
14771 {
14772 if (child->tag == DW_TAG_subprogram
14773 || child->tag == DW_TAG_lexical_block)
14774 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
14775 child = sibling_die (child);
14776 }
14777}
14778
fae299cd
DC
14779/* Get the low and high pc's represented by the scope DIE, and store
14780 them in *LOWPC and *HIGHPC. If the correct values can't be
14781 determined, set *LOWPC to -1 and *HIGHPC to 0. */
14782
14783static void
14784get_scope_pc_bounds (struct die_info *die,
14785 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14786 struct dwarf2_cu *cu)
14787{
14788 CORE_ADDR best_low = (CORE_ADDR) -1;
14789 CORE_ADDR best_high = (CORE_ADDR) 0;
14790 CORE_ADDR current_low, current_high;
14791
3a2b436a 14792 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL)
e385593e 14793 >= PC_BOUNDS_RANGES)
fae299cd
DC
14794 {
14795 best_low = current_low;
14796 best_high = current_high;
14797 }
14798 else
14799 {
14800 struct die_info *child = die->child;
14801
14802 while (child && child->tag)
14803 {
14804 switch (child->tag) {
14805 case DW_TAG_subprogram:
b084d499 14806 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
fae299cd
DC
14807 break;
14808 case DW_TAG_namespace:
f55ee35c 14809 case DW_TAG_module:
fae299cd
DC
14810 /* FIXME: carlton/2004-01-16: Should we do this for
14811 DW_TAG_class_type/DW_TAG_structure_type, too? I think
14812 that current GCC's always emit the DIEs corresponding
14813 to definitions of methods of classes as children of a
14814 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
14815 the DIEs giving the declarations, which could be
14816 anywhere). But I don't see any reason why the
14817 standards says that they have to be there. */
14818 get_scope_pc_bounds (child, &current_low, &current_high, cu);
14819
14820 if (current_low != ((CORE_ADDR) -1))
14821 {
325fac50
PA
14822 best_low = std::min (best_low, current_low);
14823 best_high = std::max (best_high, current_high);
fae299cd
DC
14824 }
14825 break;
14826 default:
0963b4bd 14827 /* Ignore. */
fae299cd
DC
14828 break;
14829 }
14830
14831 child = sibling_die (child);
14832 }
14833 }
14834
14835 *lowpc = best_low;
14836 *highpc = best_high;
14837}
14838
801e3a5b
JB
14839/* Record the address ranges for BLOCK, offset by BASEADDR, as given
14840 in DIE. */
380bca97 14841
801e3a5b
JB
14842static void
14843dwarf2_record_block_ranges (struct die_info *die, struct block *block,
14844 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
14845{
518817b3 14846 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
3e29f34a 14847 struct gdbarch *gdbarch = get_objfile_arch (objfile);
801e3a5b 14848 struct attribute *attr;
91da1414 14849 struct attribute *attr_high;
801e3a5b 14850
91da1414
MW
14851 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14852 if (attr_high)
801e3a5b 14853 {
801e3a5b
JB
14854 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
14855 if (attr)
14856 {
31aa7e4e
JB
14857 CORE_ADDR low = attr_value_as_address (attr);
14858 CORE_ADDR high = attr_value_as_address (attr_high);
14859
14860 if (cu->header.version >= 4 && attr_form_is_constant (attr_high))
14861 high += low;
9a619af0 14862
3e29f34a
MR
14863 low = gdbarch_adjust_dwarf2_addr (gdbarch, low + baseaddr);
14864 high = gdbarch_adjust_dwarf2_addr (gdbarch, high + baseaddr);
14865 record_block_range (block, low, high - 1);
801e3a5b
JB
14866 }
14867 }
14868
14869 attr = dwarf2_attr (die, DW_AT_ranges, cu);
14870 if (attr)
14871 {
ab435259
DE
14872 /* DW_AT_ranges_base does not apply to DIEs from the DWO skeleton.
14873 We take advantage of the fact that DW_AT_ranges does not appear
14874 in DW_TAG_compile_unit of DWO files. */
14875 int need_ranges_base = die->tag != DW_TAG_compile_unit;
801e3a5b
JB
14876
14877 /* The value of the DW_AT_ranges attribute is the offset of the
14878 address range list in the .debug_ranges section. */
ab435259
DE
14879 unsigned long offset = (DW_UNSND (attr)
14880 + (need_ranges_base ? cu->ranges_base : 0));
d62bfeaf 14881 const gdb_byte *buffer;
801e3a5b
JB
14882
14883 /* For some target architectures, but not others, the
14884 read_address function sign-extends the addresses it returns.
14885 To recognize base address selection entries, we need a
14886 mask. */
14887 unsigned int addr_size = cu->header.addr_size;
14888 CORE_ADDR base_select_mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
14889
14890 /* The base address, to which the next pair is relative. Note
14891 that this 'base' is a DWARF concept: most entries in a range
14892 list are relative, to reduce the number of relocs against the
14893 debugging information. This is separate from this function's
14894 'baseaddr' argument, which GDB uses to relocate debugging
14895 information from a shared library based on the address at
14896 which the library was loaded. */
d00adf39
DE
14897 CORE_ADDR base = cu->base_address;
14898 int base_known = cu->base_known;
801e3a5b 14899
5f46c5a5
JK
14900 dwarf2_ranges_process (offset, cu,
14901 [&] (CORE_ADDR start, CORE_ADDR end)
14902 {
58fdfd2c
JK
14903 start += baseaddr;
14904 end += baseaddr;
5f46c5a5
JK
14905 start = gdbarch_adjust_dwarf2_addr (gdbarch, start);
14906 end = gdbarch_adjust_dwarf2_addr (gdbarch, end);
14907 record_block_range (block, start, end - 1);
14908 });
801e3a5b
JB
14909 }
14910}
14911
685b1105
JK
14912/* Check whether the producer field indicates either of GCC < 4.6, or the
14913 Intel C/C++ compiler, and cache the result in CU. */
60d5a603 14914
685b1105
JK
14915static void
14916check_producer (struct dwarf2_cu *cu)
60d5a603 14917{
38360086 14918 int major, minor;
60d5a603
JK
14919
14920 if (cu->producer == NULL)
14921 {
14922 /* For unknown compilers expect their behavior is DWARF version
14923 compliant.
14924
14925 GCC started to support .debug_types sections by -gdwarf-4 since
14926 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
14927 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
14928 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
14929 interpreted incorrectly by GDB now - GCC PR debug/48229. */
60d5a603 14930 }
b1ffba5a 14931 else if (producer_is_gcc (cu->producer, &major, &minor))
60d5a603 14932 {
38360086
MW
14933 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
14934 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
685b1105 14935 }
5230b05a
WT
14936 else if (producer_is_icc (cu->producer, &major, &minor))
14937 cu->producer_is_icc_lt_14 = major < 14;
685b1105
JK
14938 else
14939 {
14940 /* For other non-GCC compilers, expect their behavior is DWARF version
14941 compliant. */
60d5a603
JK
14942 }
14943
ba919b58 14944 cu->checked_producer = 1;
685b1105 14945}
ba919b58 14946
685b1105
JK
14947/* Check for GCC PR debug/45124 fix which is not present in any G++ version up
14948 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
14949 during 4.6.0 experimental. */
14950
14951static int
14952producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
14953{
14954 if (!cu->checked_producer)
14955 check_producer (cu);
14956
14957 return cu->producer_is_gxx_lt_4_6;
60d5a603
JK
14958}
14959
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);
c906108c 15206 FIELD_NAME (*fp) = type_name_no_tag (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>>";
15219 }
15220 else
15221 gdb_assert_not_reached ("missing case in dwarf2_add_field");
c906108c
SS
15222}
15223
883fd55a
KS
15224/* Can the type given by DIE define another type? */
15225
15226static bool
15227type_can_define_types (const struct die_info *die)
15228{
15229 switch (die->tag)
15230 {
15231 case DW_TAG_typedef:
15232 case DW_TAG_class_type:
15233 case DW_TAG_structure_type:
15234 case DW_TAG_union_type:
15235 case DW_TAG_enumeration_type:
15236 return true;
15237
15238 default:
15239 return false;
15240 }
15241}
15242
15243/* Add a type definition defined in the scope of the FIP's class. */
98751a41
JK
15244
15245static void
883fd55a
KS
15246dwarf2_add_type_defn (struct field_info *fip, struct die_info *die,
15247 struct dwarf2_cu *cu)
6e70227d 15248{
be2daae6
TT
15249 struct decl_field fp;
15250 memset (&fp, 0, sizeof (fp));
98751a41 15251
883fd55a 15252 gdb_assert (type_can_define_types (die));
98751a41 15253
883fd55a 15254 /* Get name of field. NULL is okay here, meaning an anonymous type. */
be2daae6
TT
15255 fp.name = dwarf2_name (die, cu);
15256 fp.type = read_type_die (die, cu);
98751a41 15257
c191a687
KS
15258 /* Save accessibility. */
15259 enum dwarf_access_attribute accessibility;
15260 struct attribute *attr = dwarf2_attr (die, DW_AT_accessibility, cu);
15261 if (attr != NULL)
15262 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
15263 else
15264 accessibility = dwarf2_default_access_attribute (die, cu);
15265 switch (accessibility)
15266 {
15267 case DW_ACCESS_public:
15268 /* The assumed value if neither private nor protected. */
15269 break;
15270 case DW_ACCESS_private:
be2daae6 15271 fp.is_private = 1;
c191a687
KS
15272 break;
15273 case DW_ACCESS_protected:
be2daae6 15274 fp.is_protected = 1;
c191a687
KS
15275 break;
15276 default:
37534686
KS
15277 complaint (&symfile_complaints,
15278 _("Unhandled DW_AT_accessibility value (%x)"), accessibility);
c191a687
KS
15279 }
15280
883fd55a 15281 if (die->tag == DW_TAG_typedef)
be2daae6 15282 fip->typedef_field_list.push_back (fp);
883fd55a 15283 else
be2daae6 15284 fip->nested_types_list.push_back (fp);
98751a41
JK
15285}
15286
c906108c
SS
15287/* Create the vector of fields, and attach it to the type. */
15288
15289static void
fba45db2 15290dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 15291 struct dwarf2_cu *cu)
c906108c
SS
15292{
15293 int nfields = fip->nfields;
15294
15295 /* Record the field count, allocate space for the array of fields,
15296 and create blank accessibility bitfields if necessary. */
15297 TYPE_NFIELDS (type) = nfields;
15298 TYPE_FIELDS (type) = (struct field *)
be2daae6 15299 TYPE_ZALLOC (type, sizeof (struct field) * nfields);
c906108c 15300
b4ba55a1 15301 if (fip->non_public_fields && cu->language != language_ada)
c906108c
SS
15302 {
15303 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15304
15305 TYPE_FIELD_PRIVATE_BITS (type) =
15306 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15307 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
15308
15309 TYPE_FIELD_PROTECTED_BITS (type) =
15310 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15311 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
15312
774b6a14
TT
15313 TYPE_FIELD_IGNORE_BITS (type) =
15314 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15315 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
c906108c
SS
15316 }
15317
15318 /* If the type has baseclasses, allocate and clear a bit vector for
15319 TYPE_FIELD_VIRTUAL_BITS. */
be2daae6 15320 if (!fip->baseclasses.empty () && cu->language != language_ada)
c906108c 15321 {
be2daae6 15322 int num_bytes = B_BYTES (fip->baseclasses.size ());
fe1b8b76 15323 unsigned char *pointer;
c906108c
SS
15324
15325 ALLOCATE_CPLUS_STRUCT_TYPE (type);
224c3ddb 15326 pointer = (unsigned char *) TYPE_ALLOC (type, num_bytes);
fe1b8b76 15327 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
be2daae6
TT
15328 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->baseclasses.size ());
15329 TYPE_N_BASECLASSES (type) = fip->baseclasses.size ();
c906108c
SS
15330 }
15331
2ddeaf8a
TT
15332 if (TYPE_FLAG_DISCRIMINATED_UNION (type))
15333 {
15334 struct discriminant_info *di = alloc_discriminant_info (type, -1, -1);
15335
be2daae6 15336 for (int index = 0; index < nfields; ++index)
2ddeaf8a 15337 {
be2daae6
TT
15338 struct nextfield &field = fip->fields[index];
15339
15340 if (field.variant.is_discriminant)
2ddeaf8a 15341 di->discriminant_index = index;
be2daae6 15342 else if (field.variant.default_branch)
2ddeaf8a
TT
15343 di->default_index = index;
15344 else
be2daae6 15345 di->discriminants[index] = field.variant.discriminant_value;
2ddeaf8a
TT
15346 }
15347 }
15348
be2daae6
TT
15349 /* Copy the saved-up fields into the field vector. */
15350 for (int i = 0; i < nfields; ++i)
c906108c 15351 {
be2daae6
TT
15352 struct nextfield &field
15353 = ((i < fip->baseclasses.size ()) ? fip->baseclasses[i]
15354 : fip->fields[i - fip->baseclasses.size ()]);
7d0ccb61 15355
be2daae6
TT
15356 TYPE_FIELD (type, i) = field.field;
15357 switch (field.accessibility)
c906108c 15358 {
c5aa993b 15359 case DW_ACCESS_private:
b4ba55a1 15360 if (cu->language != language_ada)
be2daae6 15361 SET_TYPE_FIELD_PRIVATE (type, i);
c5aa993b 15362 break;
c906108c 15363
c5aa993b 15364 case DW_ACCESS_protected:
b4ba55a1 15365 if (cu->language != language_ada)
be2daae6 15366 SET_TYPE_FIELD_PROTECTED (type, i);
c5aa993b 15367 break;
c906108c 15368
c5aa993b
JM
15369 case DW_ACCESS_public:
15370 break;
c906108c 15371
c5aa993b
JM
15372 default:
15373 /* Unknown accessibility. Complain and treat it as public. */
15374 {
e2e0b3e5 15375 complaint (&symfile_complaints, _("unsupported accessibility %d"),
be2daae6 15376 field.accessibility);
c5aa993b
JM
15377 }
15378 break;
c906108c 15379 }
be2daae6 15380 if (i < fip->baseclasses.size ())
c906108c 15381 {
be2daae6 15382 switch (field.virtuality)
c906108c 15383 {
c5aa993b
JM
15384 case DW_VIRTUALITY_virtual:
15385 case DW_VIRTUALITY_pure_virtual:
b4ba55a1 15386 if (cu->language == language_ada)
a73c6dcd 15387 error (_("unexpected virtuality in component of Ada type"));
be2daae6 15388 SET_TYPE_FIELD_VIRTUAL (type, i);
c5aa993b 15389 break;
c906108c
SS
15390 }
15391 }
c906108c
SS
15392 }
15393}
15394
7d27a96d
TT
15395/* Return true if this member function is a constructor, false
15396 otherwise. */
15397
15398static int
15399dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
15400{
15401 const char *fieldname;
fe978cb0 15402 const char *type_name;
7d27a96d
TT
15403 int len;
15404
15405 if (die->parent == NULL)
15406 return 0;
15407
15408 if (die->parent->tag != DW_TAG_structure_type
15409 && die->parent->tag != DW_TAG_union_type
15410 && die->parent->tag != DW_TAG_class_type)
15411 return 0;
15412
15413 fieldname = dwarf2_name (die, cu);
fe978cb0
PA
15414 type_name = dwarf2_name (die->parent, cu);
15415 if (fieldname == NULL || type_name == NULL)
7d27a96d
TT
15416 return 0;
15417
15418 len = strlen (fieldname);
fe978cb0
PA
15419 return (strncmp (fieldname, type_name, len) == 0
15420 && (type_name[len] == '\0' || type_name[len] == '<'));
7d27a96d
TT
15421}
15422
c906108c
SS
15423/* Add a member function to the proper fieldlist. */
15424
15425static void
107d2387 15426dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
e7c27a73 15427 struct type *type, struct dwarf2_cu *cu)
c906108c 15428{
518817b3 15429 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c 15430 struct attribute *attr;
c906108c 15431 int i;
be2daae6 15432 struct fnfieldlist *flp = nullptr;
c906108c 15433 struct fn_field *fnp;
15d034d0 15434 const char *fieldname;
f792889a 15435 struct type *this_type;
60d5a603 15436 enum dwarf_access_attribute accessibility;
c906108c 15437
b4ba55a1 15438 if (cu->language == language_ada)
a73c6dcd 15439 error (_("unexpected member function in Ada type"));
b4ba55a1 15440
2df3850c 15441 /* Get name of member function. */
39cbfefa
DJ
15442 fieldname = dwarf2_name (die, cu);
15443 if (fieldname == NULL)
2df3850c 15444 return;
c906108c 15445
c906108c 15446 /* Look up member function name in fieldlist. */
be2daae6 15447 for (i = 0; i < fip->fnfieldlists.size (); i++)
c906108c 15448 {
27bfe10e 15449 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
be2daae6
TT
15450 {
15451 flp = &fip->fnfieldlists[i];
15452 break;
15453 }
c906108c
SS
15454 }
15455
be2daae6
TT
15456 /* Create a new fnfieldlist if necessary. */
15457 if (flp == nullptr)
c906108c 15458 {
be2daae6
TT
15459 fip->fnfieldlists.emplace_back ();
15460 flp = &fip->fnfieldlists.back ();
c906108c 15461 flp->name = fieldname;
be2daae6 15462 i = fip->fnfieldlists.size () - 1;
c906108c
SS
15463 }
15464
be2daae6
TT
15465 /* Create a new member function field and add it to the vector of
15466 fnfieldlists. */
15467 flp->fnfields.emplace_back ();
15468 fnp = &flp->fnfields.back ();
3da10d80
KS
15469
15470 /* Delay processing of the physname until later. */
9c37b5ae 15471 if (cu->language == language_cplus)
be2daae6
TT
15472 add_to_method_list (type, i, flp->fnfields.size () - 1, fieldname,
15473 die, cu);
3da10d80
KS
15474 else
15475 {
1d06ead6 15476 const char *physname = dwarf2_physname (fieldname, die, cu);
3da10d80
KS
15477 fnp->physname = physname ? physname : "";
15478 }
15479
c906108c 15480 fnp->type = alloc_type (objfile);
f792889a
DJ
15481 this_type = read_type_die (die, cu);
15482 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
c906108c 15483 {
f792889a 15484 int nparams = TYPE_NFIELDS (this_type);
c906108c 15485
f792889a 15486 /* TYPE is the domain of this method, and THIS_TYPE is the type
e26fb1d7
DC
15487 of the method itself (TYPE_CODE_METHOD). */
15488 smash_to_method_type (fnp->type, type,
f792889a
DJ
15489 TYPE_TARGET_TYPE (this_type),
15490 TYPE_FIELDS (this_type),
15491 TYPE_NFIELDS (this_type),
15492 TYPE_VARARGS (this_type));
c906108c
SS
15493
15494 /* Handle static member functions.
c5aa993b 15495 Dwarf2 has no clean way to discern C++ static and non-static
0963b4bd
MS
15496 member functions. G++ helps GDB by marking the first
15497 parameter for non-static member functions (which is the this
15498 pointer) as artificial. We obtain this information from
15499 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
f792889a 15500 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
c906108c
SS
15501 fnp->voffset = VOFFSET_STATIC;
15502 }
15503 else
e2e0b3e5 15504 complaint (&symfile_complaints, _("member function type missing for '%s'"),
3da10d80 15505 dwarf2_full_name (fieldname, die, cu));
c906108c
SS
15506
15507 /* Get fcontext from DW_AT_containing_type if present. */
e142c38c 15508 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
e7c27a73 15509 fnp->fcontext = die_containing_type (die, cu);
c906108c 15510
3e43a32a
MS
15511 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
15512 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
c906108c
SS
15513
15514 /* Get accessibility. */
e142c38c 15515 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
c906108c 15516 if (attr)
aead7601 15517 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
60d5a603
JK
15518 else
15519 accessibility = dwarf2_default_access_attribute (die, cu);
15520 switch (accessibility)
c906108c 15521 {
60d5a603
JK
15522 case DW_ACCESS_private:
15523 fnp->is_private = 1;
15524 break;
15525 case DW_ACCESS_protected:
15526 fnp->is_protected = 1;
15527 break;
c906108c
SS
15528 }
15529
b02dede2 15530 /* Check for artificial methods. */
e142c38c 15531 attr = dwarf2_attr (die, DW_AT_artificial, cu);
b02dede2
DJ
15532 if (attr && DW_UNSND (attr) != 0)
15533 fnp->is_artificial = 1;
15534
7d27a96d
TT
15535 fnp->is_constructor = dwarf2_is_constructor (die, cu);
15536
0d564a31 15537 /* Get index in virtual function table if it is a virtual member
aec5aa8b
TT
15538 function. For older versions of GCC, this is an offset in the
15539 appropriate virtual table, as specified by DW_AT_containing_type.
15540 For everyone else, it is an expression to be evaluated relative
0d564a31
DJ
15541 to the object address. */
15542
e142c38c 15543 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
aec5aa8b 15544 if (attr)
8e19ed76 15545 {
aec5aa8b 15546 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size > 0)
8e19ed76 15547 {
aec5aa8b
TT
15548 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
15549 {
15550 /* Old-style GCC. */
15551 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
15552 }
15553 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
15554 || (DW_BLOCK (attr)->size > 1
15555 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
15556 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
15557 {
aec5aa8b
TT
15558 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
15559 if ((fnp->voffset % cu->header.addr_size) != 0)
15560 dwarf2_complex_location_expr_complaint ();
15561 else
15562 fnp->voffset /= cu->header.addr_size;
15563 fnp->voffset += 2;
15564 }
15565 else
15566 dwarf2_complex_location_expr_complaint ();
15567
15568 if (!fnp->fcontext)
7e993ebf
KS
15569 {
15570 /* If there is no `this' field and no DW_AT_containing_type,
15571 we cannot actually find a base class context for the
15572 vtable! */
15573 if (TYPE_NFIELDS (this_type) == 0
15574 || !TYPE_FIELD_ARTIFICIAL (this_type, 0))
15575 {
15576 complaint (&symfile_complaints,
15577 _("cannot determine context for virtual member "
9d8780f0
SM
15578 "function \"%s\" (offset %s)"),
15579 fieldname, sect_offset_str (die->sect_off));
7e993ebf
KS
15580 }
15581 else
15582 {
15583 fnp->fcontext
15584 = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
15585 }
15586 }
aec5aa8b 15587 }
3690dd37 15588 else if (attr_form_is_section_offset (attr))
8e19ed76 15589 {
4d3c2250 15590 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
15591 }
15592 else
15593 {
4d3c2250
KB
15594 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
15595 fieldname);
8e19ed76 15596 }
0d564a31 15597 }
d48cc9dd
DJ
15598 else
15599 {
15600 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
15601 if (attr && DW_UNSND (attr))
15602 {
15603 /* GCC does this, as of 2008-08-25; PR debug/37237. */
15604 complaint (&symfile_complaints,
9d8780f0 15605 _("Member function \"%s\" (offset %s) is virtual "
3e43a32a 15606 "but the vtable offset is not specified"),
9d8780f0 15607 fieldname, sect_offset_str (die->sect_off));
9655fd1a 15608 ALLOCATE_CPLUS_STRUCT_TYPE (type);
d48cc9dd
DJ
15609 TYPE_CPLUS_DYNAMIC (type) = 1;
15610 }
15611 }
c906108c
SS
15612}
15613
15614/* Create the vector of member function fields, and attach it to the type. */
15615
15616static void
fba45db2 15617dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 15618 struct dwarf2_cu *cu)
c906108c 15619{
b4ba55a1 15620 if (cu->language == language_ada)
a73c6dcd 15621 error (_("unexpected member functions in Ada type"));
b4ba55a1 15622
c906108c
SS
15623 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15624 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
be2daae6
TT
15625 TYPE_ALLOC (type,
15626 sizeof (struct fn_fieldlist) * fip->fnfieldlists.size ());
c906108c 15627
be2daae6 15628 for (int i = 0; i < fip->fnfieldlists.size (); i++)
c906108c 15629 {
be2daae6 15630 struct fnfieldlist &nf = fip->fnfieldlists[i];
c906108c 15631 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
c906108c 15632
be2daae6
TT
15633 TYPE_FN_FIELDLIST_NAME (type, i) = nf.name;
15634 TYPE_FN_FIELDLIST_LENGTH (type, i) = nf.fnfields.size ();
c906108c 15635 fn_flp->fn_fields = (struct fn_field *)
be2daae6
TT
15636 TYPE_ALLOC (type, sizeof (struct fn_field) * nf.fnfields.size ());
15637
15638 for (int k = 0; k < nf.fnfields.size (); ++k)
15639 fn_flp->fn_fields[k] = nf.fnfields[k];
c906108c
SS
15640 }
15641
be2daae6 15642 TYPE_NFN_FIELDS (type) = fip->fnfieldlists.size ();
c906108c
SS
15643}
15644
1168df01
JB
15645/* Returns non-zero if NAME is the name of a vtable member in CU's
15646 language, zero otherwise. */
15647static int
15648is_vtable_name (const char *name, struct dwarf2_cu *cu)
15649{
15650 static const char vptr[] = "_vptr";
15651
9c37b5ae
TT
15652 /* Look for the C++ form of the vtable. */
15653 if (startswith (name, vptr) && is_cplus_marker (name[sizeof (vptr) - 1]))
1168df01
JB
15654 return 1;
15655
15656 return 0;
15657}
15658
c0dd20ea 15659/* GCC outputs unnamed structures that are really pointers to member
0b92b5bb
TT
15660 functions, with the ABI-specified layout. If TYPE describes
15661 such a structure, smash it into a member function type.
61049d3b
DJ
15662
15663 GCC shouldn't do this; it should just output pointer to member DIEs.
15664 This is GCC PR debug/28767. */
c0dd20ea 15665
0b92b5bb
TT
15666static void
15667quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
c0dd20ea 15668{
09e2d7c7 15669 struct type *pfn_type, *self_type, *new_type;
c0dd20ea
DJ
15670
15671 /* Check for a structure with no name and two children. */
0b92b5bb
TT
15672 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
15673 return;
c0dd20ea
DJ
15674
15675 /* Check for __pfn and __delta members. */
0b92b5bb
TT
15676 if (TYPE_FIELD_NAME (type, 0) == NULL
15677 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
15678 || TYPE_FIELD_NAME (type, 1) == NULL
15679 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
15680 return;
c0dd20ea
DJ
15681
15682 /* Find the type of the method. */
0b92b5bb 15683 pfn_type = TYPE_FIELD_TYPE (type, 0);
c0dd20ea
DJ
15684 if (pfn_type == NULL
15685 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
15686 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
0b92b5bb 15687 return;
c0dd20ea
DJ
15688
15689 /* Look for the "this" argument. */
15690 pfn_type = TYPE_TARGET_TYPE (pfn_type);
15691 if (TYPE_NFIELDS (pfn_type) == 0
0b92b5bb 15692 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
c0dd20ea 15693 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
0b92b5bb 15694 return;
c0dd20ea 15695
09e2d7c7 15696 self_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
0b92b5bb 15697 new_type = alloc_type (objfile);
09e2d7c7 15698 smash_to_method_type (new_type, self_type, TYPE_TARGET_TYPE (pfn_type),
c0dd20ea
DJ
15699 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
15700 TYPE_VARARGS (pfn_type));
0b92b5bb 15701 smash_to_methodptr_type (type, new_type);
c0dd20ea 15702}
1168df01 15703
685b1105 15704
c906108c 15705/* Called when we find the DIE that starts a structure or union scope
c767944b
DJ
15706 (definition) to create a type for the structure or union. Fill in
15707 the type's name and general properties; the members will not be
83655187
DE
15708 processed until process_structure_scope. A symbol table entry for
15709 the type will also not be done until process_structure_scope (assuming
15710 the type has a name).
c906108c 15711
c767944b
DJ
15712 NOTE: we need to call these functions regardless of whether or not the
15713 DIE has a DW_AT_name attribute, since it might be an anonymous
c906108c 15714 structure or union. This gets the type entered into our set of
83655187 15715 user defined types. */
c906108c 15716
f792889a 15717static struct type *
134d01f1 15718read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 15719{
518817b3 15720 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c
SS
15721 struct type *type;
15722 struct attribute *attr;
15d034d0 15723 const char *name;
c906108c 15724
348e048f
DE
15725 /* If the definition of this type lives in .debug_types, read that type.
15726 Don't follow DW_AT_specification though, that will take us back up
15727 the chain and we want to go down. */
45e58e77 15728 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
348e048f
DE
15729 if (attr)
15730 {
ac9ec31b 15731 type = get_DW_AT_signature_type (die, attr, cu);
9dc481d3 15732
ac9ec31b 15733 /* The type's CU may not be the same as CU.
02142a6c 15734 Ensure TYPE is recorded with CU in die_type_hash. */
348e048f
DE
15735 return set_die_type (die, type, cu);
15736 }
15737
c0dd20ea 15738 type = alloc_type (objfile);
c906108c 15739 INIT_CPLUS_SPECIFIC (type);
93311388 15740
39cbfefa
DJ
15741 name = dwarf2_name (die, cu);
15742 if (name != NULL)
c906108c 15743 {
987504bb 15744 if (cu->language == language_cplus
c44af4eb
TT
15745 || cu->language == language_d
15746 || cu->language == language_rust)
63d06c5c 15747 {
15d034d0 15748 const char *full_name = dwarf2_full_name (name, die, cu);
3da10d80
KS
15749
15750 /* dwarf2_full_name might have already finished building the DIE's
15751 type. If so, there is no need to continue. */
15752 if (get_die_type (die, cu) != NULL)
15753 return get_die_type (die, cu);
15754
15755 TYPE_TAG_NAME (type) = full_name;
94af9270
KS
15756 if (die->tag == DW_TAG_structure_type
15757 || die->tag == DW_TAG_class_type)
15758 TYPE_NAME (type) = TYPE_TAG_NAME (type);
63d06c5c
DC
15759 }
15760 else
15761 {
d8151005
DJ
15762 /* The name is already allocated along with this objfile, so
15763 we don't need to duplicate it for the type. */
7d455152 15764 TYPE_TAG_NAME (type) = name;
94af9270
KS
15765 if (die->tag == DW_TAG_class_type)
15766 TYPE_NAME (type) = TYPE_TAG_NAME (type);
63d06c5c 15767 }
c906108c
SS
15768 }
15769
15770 if (die->tag == DW_TAG_structure_type)
15771 {
15772 TYPE_CODE (type) = TYPE_CODE_STRUCT;
15773 }
15774 else if (die->tag == DW_TAG_union_type)
15775 {
15776 TYPE_CODE (type) = TYPE_CODE_UNION;
15777 }
2ddeaf8a
TT
15778 else if (die->tag == DW_TAG_variant_part)
15779 {
15780 TYPE_CODE (type) = TYPE_CODE_UNION;
15781 TYPE_FLAG_DISCRIMINATED_UNION (type) = 1;
15782 }
c906108c
SS
15783 else
15784 {
4753d33b 15785 TYPE_CODE (type) = TYPE_CODE_STRUCT;
c906108c
SS
15786 }
15787
0cc2414c
TT
15788 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
15789 TYPE_DECLARED_CLASS (type) = 1;
15790
e142c38c 15791 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
15792 if (attr)
15793 {
155bfbd3
JB
15794 if (attr_form_is_constant (attr))
15795 TYPE_LENGTH (type) = DW_UNSND (attr);
15796 else
15797 {
15798 /* For the moment, dynamic type sizes are not supported
15799 by GDB's struct type. The actual size is determined
15800 on-demand when resolving the type of a given object,
15801 so set the type's length to zero for now. Otherwise,
15802 we record an expression as the length, and that expression
15803 could lead to a very large value, which could eventually
15804 lead to us trying to allocate that much memory when creating
15805 a value of that type. */
15806 TYPE_LENGTH (type) = 0;
15807 }
c906108c
SS
15808 }
15809 else
15810 {
15811 TYPE_LENGTH (type) = 0;
15812 }
15813
5230b05a 15814 if (producer_is_icc_lt_14 (cu) && (TYPE_LENGTH (type) == 0))
685b1105 15815 {
5230b05a
WT
15816 /* ICC<14 does not output the required DW_AT_declaration on
15817 incomplete types, but gives them a size of zero. */
422b1cb0 15818 TYPE_STUB (type) = 1;
685b1105
JK
15819 }
15820 else
15821 TYPE_STUB_SUPPORTED (type) = 1;
15822
dc718098 15823 if (die_is_declaration (die, cu))
876cecd0 15824 TYPE_STUB (type) = 1;
a6c727b2
DJ
15825 else if (attr == NULL && die->child == NULL
15826 && producer_is_realview (cu->producer))
15827 /* RealView does not output the required DW_AT_declaration
15828 on incomplete types. */
15829 TYPE_STUB (type) = 1;
dc718098 15830
c906108c
SS
15831 /* We need to add the type field to the die immediately so we don't
15832 infinitely recurse when dealing with pointers to the structure
0963b4bd 15833 type within the structure itself. */
1c379e20 15834 set_die_type (die, type, cu);
c906108c 15835
7e314c57
JK
15836 /* set_die_type should be already done. */
15837 set_descriptive_type (type, die, cu);
15838
c767944b
DJ
15839 return type;
15840}
15841
2ddeaf8a
TT
15842/* A helper for process_structure_scope that handles a single member
15843 DIE. */
15844
15845static void
15846handle_struct_member_die (struct die_info *child_die, struct type *type,
15847 struct field_info *fi,
15848 std::vector<struct symbol *> *template_args,
15849 struct dwarf2_cu *cu)
15850{
15851 if (child_die->tag == DW_TAG_member
15852 || child_die->tag == DW_TAG_variable
15853 || child_die->tag == DW_TAG_variant_part)
15854 {
15855 /* NOTE: carlton/2002-11-05: A C++ static data member
15856 should be a DW_TAG_member that is a declaration, but
15857 all versions of G++ as of this writing (so through at
15858 least 3.2.1) incorrectly generate DW_TAG_variable
15859 tags for them instead. */
15860 dwarf2_add_field (fi, child_die, cu);
15861 }
15862 else if (child_die->tag == DW_TAG_subprogram)
15863 {
15864 /* Rust doesn't have member functions in the C++ sense.
15865 However, it does emit ordinary functions as children
15866 of a struct DIE. */
15867 if (cu->language == language_rust)
15868 read_func_scope (child_die, cu);
15869 else
15870 {
15871 /* C++ member function. */
15872 dwarf2_add_member_fn (fi, child_die, type, cu);
15873 }
15874 }
15875 else if (child_die->tag == DW_TAG_inheritance)
15876 {
15877 /* C++ base class field. */
15878 dwarf2_add_field (fi, child_die, cu);
15879 }
15880 else if (type_can_define_types (child_die))
15881 dwarf2_add_type_defn (fi, child_die, cu);
15882 else if (child_die->tag == DW_TAG_template_type_param
15883 || child_die->tag == DW_TAG_template_value_param)
15884 {
15885 struct symbol *arg = new_symbol (child_die, NULL, cu);
15886
15887 if (arg != NULL)
15888 template_args->push_back (arg);
15889 }
15890 else if (child_die->tag == DW_TAG_variant)
15891 {
15892 /* In a variant we want to get the discriminant and also add a
15893 field for our sole member child. */
15894 struct attribute *discr = dwarf2_attr (child_die, DW_AT_discr_value, cu);
15895
15896 for (struct die_info *variant_child = child_die->child;
15897 variant_child != NULL;
15898 variant_child = sibling_die (variant_child))
15899 {
15900 if (variant_child->tag == DW_TAG_member)
15901 {
15902 handle_struct_member_die (variant_child, type, fi,
15903 template_args, cu);
15904 /* Only handle the one. */
15905 break;
15906 }
15907 }
15908
15909 /* We don't handle this but we might as well report it if we see
15910 it. */
15911 if (dwarf2_attr (child_die, DW_AT_discr_list, cu) != nullptr)
15912 complaint (&symfile_complaints,
15913 _("DW_AT_discr_list is not supported yet"
15914 " - DIE at %s [in module %s]"),
15915 sect_offset_str (child_die->sect_off),
15916 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15917
15918 /* The first field was just added, so we can stash the
15919 discriminant there. */
be2daae6 15920 gdb_assert (!fi->fields.empty ());
2ddeaf8a 15921 if (discr == NULL)
be2daae6 15922 fi->fields.back ().variant.default_branch = true;
2ddeaf8a 15923 else
be2daae6 15924 fi->fields.back ().variant.discriminant_value = DW_UNSND (discr);
2ddeaf8a
TT
15925 }
15926}
15927
c767944b
DJ
15928/* Finish creating a structure or union type, including filling in
15929 its members and creating a symbol for it. */
15930
15931static void
15932process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
15933{
518817b3 15934 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
ca040673 15935 struct die_info *child_die;
c767944b
DJ
15936 struct type *type;
15937
15938 type = get_die_type (die, cu);
15939 if (type == NULL)
15940 type = read_structure_type (die, cu);
15941
2ddeaf8a
TT
15942 /* When reading a DW_TAG_variant_part, we need to notice when we
15943 read the discriminant member, so we can record it later in the
15944 discriminant_info. */
15945 bool is_variant_part = TYPE_FLAG_DISCRIMINATED_UNION (type);
15946 sect_offset discr_offset;
15947
15948 if (is_variant_part)
15949 {
15950 struct attribute *discr = dwarf2_attr (die, DW_AT_discr, cu);
15951 if (discr == NULL)
15952 {
15953 /* Maybe it's a univariant form, an extension we support.
15954 In this case arrange not to check the offset. */
15955 is_variant_part = false;
15956 }
15957 else if (attr_form_is_ref (discr))
15958 {
15959 struct dwarf2_cu *target_cu = cu;
15960 struct die_info *target_die = follow_die_ref (die, discr, &target_cu);
15961
15962 discr_offset = target_die->sect_off;
15963 }
15964 else
15965 {
15966 complaint (&symfile_complaints,
15967 _("DW_AT_discr does not have DIE reference form"
15968 " - DIE at %s [in module %s]"),
15969 sect_offset_str (die->sect_off),
15970 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15971 is_variant_part = false;
15972 }
15973 }
15974
e142c38c 15975 if (die->child != NULL && ! die_is_declaration (die, cu))
c906108c
SS
15976 {
15977 struct field_info fi;
2f4732b0 15978 std::vector<struct symbol *> template_args;
c906108c 15979
639d11d3 15980 child_die = die->child;
c906108c
SS
15981
15982 while (child_die && child_die->tag)
15983 {
2ddeaf8a 15984 handle_struct_member_die (child_die, type, &fi, &template_args, cu);
34eaf542 15985
2ddeaf8a 15986 if (is_variant_part && discr_offset == child_die->sect_off)
be2daae6 15987 fi.fields.back ().variant.is_discriminant = true;
34eaf542 15988
c906108c
SS
15989 child_die = sibling_die (child_die);
15990 }
15991
34eaf542 15992 /* Attach template arguments to type. */
2f4732b0 15993 if (!template_args.empty ())
34eaf542
TT
15994 {
15995 ALLOCATE_CPLUS_STRUCT_TYPE (type);
2f4732b0 15996 TYPE_N_TEMPLATE_ARGUMENTS (type) = template_args.size ();
34eaf542 15997 TYPE_TEMPLATE_ARGUMENTS (type)
8d749320
SM
15998 = XOBNEWVEC (&objfile->objfile_obstack,
15999 struct symbol *,
16000 TYPE_N_TEMPLATE_ARGUMENTS (type));
34eaf542 16001 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
2f4732b0 16002 template_args.data (),
34eaf542
TT
16003 (TYPE_N_TEMPLATE_ARGUMENTS (type)
16004 * sizeof (struct symbol *)));
34eaf542
TT
16005 }
16006
c906108c
SS
16007 /* Attach fields and member functions to the type. */
16008 if (fi.nfields)
e7c27a73 16009 dwarf2_attach_fields_to_type (&fi, type, cu);
be2daae6 16010 if (!fi.fnfieldlists.empty ())
c906108c 16011 {
e7c27a73 16012 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
c906108c 16013
c5aa993b 16014 /* Get the type which refers to the base class (possibly this
c906108c 16015 class itself) which contains the vtable pointer for the current
0d564a31
DJ
16016 class from the DW_AT_containing_type attribute. This use of
16017 DW_AT_containing_type is a GNU extension. */
c906108c 16018
e142c38c 16019 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
c906108c 16020 {
e7c27a73 16021 struct type *t = die_containing_type (die, cu);
c906108c 16022
ae6ae975 16023 set_type_vptr_basetype (type, t);
c906108c
SS
16024 if (type == t)
16025 {
c906108c
SS
16026 int i;
16027
16028 /* Our own class provides vtbl ptr. */
16029 for (i = TYPE_NFIELDS (t) - 1;
16030 i >= TYPE_N_BASECLASSES (t);
16031 --i)
16032 {
0d5cff50 16033 const char *fieldname = TYPE_FIELD_NAME (t, i);
c906108c 16034
1168df01 16035 if (is_vtable_name (fieldname, cu))
c906108c 16036 {
ae6ae975 16037 set_type_vptr_fieldno (type, i);
c906108c
SS
16038 break;
16039 }
16040 }
16041
16042 /* Complain if virtual function table field not found. */
16043 if (i < TYPE_N_BASECLASSES (t))
4d3c2250 16044 complaint (&symfile_complaints,
3e43a32a
MS
16045 _("virtual function table pointer "
16046 "not found when defining class '%s'"),
4d3c2250
KB
16047 TYPE_TAG_NAME (type) ? TYPE_TAG_NAME (type) :
16048 "");
c906108c
SS
16049 }
16050 else
16051 {
ae6ae975 16052 set_type_vptr_fieldno (type, TYPE_VPTR_FIELDNO (t));
c906108c
SS
16053 }
16054 }
f6235d4c 16055 else if (cu->producer
61012eef 16056 && startswith (cu->producer, "IBM(R) XL C/C++ Advanced Edition"))
f6235d4c
EZ
16057 {
16058 /* The IBM XLC compiler does not provide direct indication
16059 of the containing type, but the vtable pointer is
16060 always named __vfp. */
16061
16062 int i;
16063
16064 for (i = TYPE_NFIELDS (type) - 1;
16065 i >= TYPE_N_BASECLASSES (type);
16066 --i)
16067 {
16068 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
16069 {
ae6ae975
DE
16070 set_type_vptr_fieldno (type, i);
16071 set_type_vptr_basetype (type, type);
f6235d4c
EZ
16072 break;
16073 }
16074 }
16075 }
c906108c 16076 }
98751a41
JK
16077
16078 /* Copy fi.typedef_field_list linked list elements content into the
16079 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
be2daae6 16080 if (!fi.typedef_field_list.empty ())
98751a41 16081 {
be2daae6 16082 int count = fi.typedef_field_list.size ();
98751a41 16083
a0d7a4ff 16084 ALLOCATE_CPLUS_STRUCT_TYPE (type);
98751a41 16085 TYPE_TYPEDEF_FIELD_ARRAY (type)
883fd55a 16086 = ((struct decl_field *)
be2daae6
TT
16087 TYPE_ALLOC (type,
16088 sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * count));
16089 TYPE_TYPEDEF_FIELD_COUNT (type) = count;
6e70227d 16090
be2daae6
TT
16091 for (int i = 0; i < fi.typedef_field_list.size (); ++i)
16092 TYPE_TYPEDEF_FIELD (type, i) = fi.typedef_field_list[i];
98751a41 16093 }
c767944b 16094
883fd55a
KS
16095 /* Copy fi.nested_types_list linked list elements content into the
16096 allocated array TYPE_NESTED_TYPES_ARRAY (type). */
be2daae6 16097 if (!fi.nested_types_list.empty () && cu->language != language_ada)
883fd55a 16098 {
be2daae6 16099 int count = fi.nested_types_list.size ();
883fd55a
KS
16100
16101 ALLOCATE_CPLUS_STRUCT_TYPE (type);
16102 TYPE_NESTED_TYPES_ARRAY (type)
16103 = ((struct decl_field *)
be2daae6
TT
16104 TYPE_ALLOC (type, sizeof (struct decl_field) * count));
16105 TYPE_NESTED_TYPES_COUNT (type) = count;
883fd55a 16106
be2daae6
TT
16107 for (int i = 0; i < fi.nested_types_list.size (); ++i)
16108 TYPE_NESTED_TYPES_FIELD (type, i) = fi.nested_types_list[i];
883fd55a 16109 }
c906108c 16110 }
63d06c5c 16111
bb5ed363 16112 quirk_gcc_member_function_pointer (type, objfile);
c9317f21
TT
16113 if (cu->language == language_rust && die->tag == DW_TAG_union_type)
16114 cu->rust_unions.push_back (type);
0b92b5bb 16115
90aeadfc
DC
16116 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
16117 snapshots) has been known to create a die giving a declaration
16118 for a class that has, as a child, a die giving a definition for a
16119 nested class. So we have to process our children even if the
16120 current die is a declaration. Normally, of course, a declaration
16121 won't have any children at all. */
134d01f1 16122
ca040673
DE
16123 child_die = die->child;
16124
90aeadfc
DC
16125 while (child_die != NULL && child_die->tag)
16126 {
16127 if (child_die->tag == DW_TAG_member
16128 || child_die->tag == DW_TAG_variable
34eaf542
TT
16129 || child_die->tag == DW_TAG_inheritance
16130 || child_die->tag == DW_TAG_template_value_param
16131 || child_die->tag == DW_TAG_template_type_param)
134d01f1 16132 {
90aeadfc 16133 /* Do nothing. */
134d01f1 16134 }
90aeadfc
DC
16135 else
16136 process_die (child_die, cu);
134d01f1 16137
90aeadfc 16138 child_die = sibling_die (child_die);
134d01f1
DJ
16139 }
16140
fa4028e9
JB
16141 /* Do not consider external references. According to the DWARF standard,
16142 these DIEs are identified by the fact that they have no byte_size
16143 attribute, and a declaration attribute. */
16144 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
16145 || !die_is_declaration (die, cu))
c767944b 16146 new_symbol (die, type, cu);
134d01f1
DJ
16147}
16148
55426c9d
JB
16149/* Assuming DIE is an enumeration type, and TYPE is its associated type,
16150 update TYPE using some information only available in DIE's children. */
16151
16152static void
16153update_enumeration_type_from_children (struct die_info *die,
16154 struct type *type,
16155 struct dwarf2_cu *cu)
16156{
60f7655a 16157 struct die_info *child_die;
55426c9d
JB
16158 int unsigned_enum = 1;
16159 int flag_enum = 1;
16160 ULONGEST mask = 0;
55426c9d 16161
8268c778 16162 auto_obstack obstack;
55426c9d 16163
60f7655a
DE
16164 for (child_die = die->child;
16165 child_die != NULL && child_die->tag;
16166 child_die = sibling_die (child_die))
55426c9d
JB
16167 {
16168 struct attribute *attr;
16169 LONGEST value;
16170 const gdb_byte *bytes;
16171 struct dwarf2_locexpr_baton *baton;
16172 const char *name;
60f7655a 16173
55426c9d
JB
16174 if (child_die->tag != DW_TAG_enumerator)
16175 continue;
16176
16177 attr = dwarf2_attr (child_die, DW_AT_const_value, cu);
16178 if (attr == NULL)
16179 continue;
16180
16181 name = dwarf2_name (child_die, cu);
16182 if (name == NULL)
16183 name = "<anonymous enumerator>";
16184
16185 dwarf2_const_value_attr (attr, type, name, &obstack, cu,
16186 &value, &bytes, &baton);
16187 if (value < 0)
16188 {
16189 unsigned_enum = 0;
16190 flag_enum = 0;
16191 }
16192 else if ((mask & value) != 0)
16193 flag_enum = 0;
16194 else
16195 mask |= value;
16196
16197 /* If we already know that the enum type is neither unsigned, nor
16198 a flag type, no need to look at the rest of the enumerates. */
16199 if (!unsigned_enum && !flag_enum)
16200 break;
55426c9d
JB
16201 }
16202
16203 if (unsigned_enum)
16204 TYPE_UNSIGNED (type) = 1;
16205 if (flag_enum)
16206 TYPE_FLAG_ENUM (type) = 1;
55426c9d
JB
16207}
16208
134d01f1
DJ
16209/* Given a DW_AT_enumeration_type die, set its type. We do not
16210 complete the type's fields yet, or create any symbols. */
c906108c 16211
f792889a 16212static struct type *
134d01f1 16213read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16214{
518817b3 16215 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c 16216 struct type *type;
c906108c 16217 struct attribute *attr;
0114d602 16218 const char *name;
134d01f1 16219
348e048f
DE
16220 /* If the definition of this type lives in .debug_types, read that type.
16221 Don't follow DW_AT_specification though, that will take us back up
16222 the chain and we want to go down. */
45e58e77 16223 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
348e048f
DE
16224 if (attr)
16225 {
ac9ec31b 16226 type = get_DW_AT_signature_type (die, attr, cu);
9dc481d3 16227
ac9ec31b 16228 /* The type's CU may not be the same as CU.
02142a6c 16229 Ensure TYPE is recorded with CU in die_type_hash. */
348e048f
DE
16230 return set_die_type (die, type, cu);
16231 }
16232
c906108c
SS
16233 type = alloc_type (objfile);
16234
16235 TYPE_CODE (type) = TYPE_CODE_ENUM;
94af9270 16236 name = dwarf2_full_name (NULL, die, cu);
39cbfefa 16237 if (name != NULL)
7d455152 16238 TYPE_TAG_NAME (type) = name;
c906108c 16239
0626fc76
TT
16240 attr = dwarf2_attr (die, DW_AT_type, cu);
16241 if (attr != NULL)
16242 {
16243 struct type *underlying_type = die_type (die, cu);
16244
16245 TYPE_TARGET_TYPE (type) = underlying_type;
16246 }
16247
e142c38c 16248 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
16249 if (attr)
16250 {
16251 TYPE_LENGTH (type) = DW_UNSND (attr);
16252 }
16253 else
16254 {
16255 TYPE_LENGTH (type) = 0;
16256 }
16257
137033e9
JB
16258 /* The enumeration DIE can be incomplete. In Ada, any type can be
16259 declared as private in the package spec, and then defined only
16260 inside the package body. Such types are known as Taft Amendment
16261 Types. When another package uses such a type, an incomplete DIE
16262 may be generated by the compiler. */
02eb380e 16263 if (die_is_declaration (die, cu))
876cecd0 16264 TYPE_STUB (type) = 1;
02eb380e 16265
0626fc76
TT
16266 /* Finish the creation of this type by using the enum's children.
16267 We must call this even when the underlying type has been provided
16268 so that we can determine if we're looking at a "flag" enum. */
55426c9d
JB
16269 update_enumeration_type_from_children (die, type, cu);
16270
0626fc76
TT
16271 /* If this type has an underlying type that is not a stub, then we
16272 may use its attributes. We always use the "unsigned" attribute
16273 in this situation, because ordinarily we guess whether the type
16274 is unsigned -- but the guess can be wrong and the underlying type
16275 can tell us the reality. However, we defer to a local size
16276 attribute if one exists, because this lets the compiler override
16277 the underlying type if needed. */
16278 if (TYPE_TARGET_TYPE (type) != NULL && !TYPE_STUB (TYPE_TARGET_TYPE (type)))
16279 {
16280 TYPE_UNSIGNED (type) = TYPE_UNSIGNED (TYPE_TARGET_TYPE (type));
16281 if (TYPE_LENGTH (type) == 0)
16282 TYPE_LENGTH (type) = TYPE_LENGTH (TYPE_TARGET_TYPE (type));
16283 }
16284
3d567982
TT
16285 TYPE_DECLARED_CLASS (type) = dwarf2_flag_true_p (die, DW_AT_enum_class, cu);
16286
f792889a 16287 return set_die_type (die, type, cu);
134d01f1
DJ
16288}
16289
16290/* Given a pointer to a die which begins an enumeration, process all
16291 the dies that define the members of the enumeration, and create the
16292 symbol for the enumeration type.
16293
16294 NOTE: We reverse the order of the element list. */
16295
16296static void
16297process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
16298{
f792889a 16299 struct type *this_type;
134d01f1 16300
f792889a
DJ
16301 this_type = get_die_type (die, cu);
16302 if (this_type == NULL)
16303 this_type = read_enumeration_type (die, cu);
9dc481d3 16304
639d11d3 16305 if (die->child != NULL)
c906108c 16306 {
9dc481d3
DE
16307 struct die_info *child_die;
16308 struct symbol *sym;
16309 struct field *fields = NULL;
16310 int num_fields = 0;
15d034d0 16311 const char *name;
9dc481d3 16312
639d11d3 16313 child_die = die->child;
c906108c
SS
16314 while (child_die && child_die->tag)
16315 {
16316 if (child_die->tag != DW_TAG_enumerator)
16317 {
e7c27a73 16318 process_die (child_die, cu);
c906108c
SS
16319 }
16320 else
16321 {
39cbfefa
DJ
16322 name = dwarf2_name (child_die, cu);
16323 if (name)
c906108c 16324 {
f792889a 16325 sym = new_symbol (child_die, this_type, cu);
c906108c
SS
16326
16327 if ((num_fields % DW_FIELD_ALLOC_CHUNK) == 0)
16328 {
16329 fields = (struct field *)
16330 xrealloc (fields,
16331 (num_fields + DW_FIELD_ALLOC_CHUNK)
c5aa993b 16332 * sizeof (struct field));
c906108c
SS
16333 }
16334
3567439c 16335 FIELD_NAME (fields[num_fields]) = SYMBOL_LINKAGE_NAME (sym);
c906108c 16336 FIELD_TYPE (fields[num_fields]) = NULL;
14e75d8e 16337 SET_FIELD_ENUMVAL (fields[num_fields], SYMBOL_VALUE (sym));
c906108c
SS
16338 FIELD_BITSIZE (fields[num_fields]) = 0;
16339
16340 num_fields++;
16341 }
16342 }
16343
16344 child_die = sibling_die (child_die);
16345 }
16346
16347 if (num_fields)
16348 {
f792889a
DJ
16349 TYPE_NFIELDS (this_type) = num_fields;
16350 TYPE_FIELDS (this_type) = (struct field *)
16351 TYPE_ALLOC (this_type, sizeof (struct field) * num_fields);
16352 memcpy (TYPE_FIELDS (this_type), fields,
c906108c 16353 sizeof (struct field) * num_fields);
b8c9b27d 16354 xfree (fields);
c906108c 16355 }
c906108c 16356 }
134d01f1 16357
6c83ed52
TT
16358 /* If we are reading an enum from a .debug_types unit, and the enum
16359 is a declaration, and the enum is not the signatured type in the
16360 unit, then we do not want to add a symbol for it. Adding a
16361 symbol would in some cases obscure the true definition of the
16362 enum, giving users an incomplete type when the definition is
16363 actually available. Note that we do not want to do this for all
16364 enums which are just declarations, because C++0x allows forward
16365 enum declarations. */
3019eac3 16366 if (cu->per_cu->is_debug_types
6c83ed52
TT
16367 && die_is_declaration (die, cu))
16368 {
52dc124a 16369 struct signatured_type *sig_type;
6c83ed52 16370
c0f78cd4 16371 sig_type = (struct signatured_type *) cu->per_cu;
9c541725
PA
16372 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
16373 if (sig_type->type_offset_in_section != die->sect_off)
6c83ed52
TT
16374 return;
16375 }
16376
f792889a 16377 new_symbol (die, this_type, cu);
c906108c
SS
16378}
16379
16380/* Extract all information from a DW_TAG_array_type DIE and put it in
16381 the DIE's type field. For now, this only handles one dimensional
16382 arrays. */
16383
f792889a 16384static struct type *
e7c27a73 16385read_array_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16386{
518817b3 16387 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c 16388 struct die_info *child_die;
7e314c57 16389 struct type *type;
c906108c 16390 struct type *element_type, *range_type, *index_type;
c906108c 16391 struct attribute *attr;
15d034d0 16392 const char *name;
a405673c 16393 struct dynamic_prop *byte_stride_prop = NULL;
dc53a7ad 16394 unsigned int bit_stride = 0;
c906108c 16395
e7c27a73 16396 element_type = die_type (die, cu);
c906108c 16397
7e314c57
JK
16398 /* The die_type call above may have already set the type for this DIE. */
16399 type = get_die_type (die, cu);
16400 if (type)
16401 return type;
16402
dc53a7ad
JB
16403 attr = dwarf2_attr (die, DW_AT_byte_stride, cu);
16404 if (attr != NULL)
a405673c
JB
16405 {
16406 int stride_ok;
16407
16408 byte_stride_prop
16409 = (struct dynamic_prop *) alloca (sizeof (struct dynamic_prop));
16410 stride_ok = attr_to_dynamic_prop (attr, die, cu, byte_stride_prop);
16411 if (!stride_ok)
16412 {
16413 complaint (&symfile_complaints,
16414 _("unable to read array DW_AT_byte_stride "
9d8780f0
SM
16415 " - DIE at %s [in module %s]"),
16416 sect_offset_str (die->sect_off),
518817b3 16417 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
a405673c
JB
16418 /* Ignore this attribute. We will likely not be able to print
16419 arrays of this type correctly, but there is little we can do
16420 to help if we cannot read the attribute's value. */
16421 byte_stride_prop = NULL;
16422 }
16423 }
dc53a7ad
JB
16424
16425 attr = dwarf2_attr (die, DW_AT_bit_stride, cu);
16426 if (attr != NULL)
16427 bit_stride = DW_UNSND (attr);
16428
c906108c
SS
16429 /* Irix 6.2 native cc creates array types without children for
16430 arrays with unspecified length. */
639d11d3 16431 if (die->child == NULL)
c906108c 16432 {
46bf5051 16433 index_type = objfile_type (objfile)->builtin_int;
0c9c3474 16434 range_type = create_static_range_type (NULL, index_type, 0, -1);
dc53a7ad 16435 type = create_array_type_with_stride (NULL, element_type, range_type,
a405673c 16436 byte_stride_prop, bit_stride);
f792889a 16437 return set_die_type (die, type, cu);
c906108c
SS
16438 }
16439
791afaa2 16440 std::vector<struct type *> range_types;
639d11d3 16441 child_die = die->child;
c906108c
SS
16442 while (child_die && child_die->tag)
16443 {
16444 if (child_die->tag == DW_TAG_subrange_type)
16445 {
f792889a 16446 struct type *child_type = read_type_die (child_die, cu);
9a619af0 16447
f792889a 16448 if (child_type != NULL)
a02abb62 16449 {
0963b4bd
MS
16450 /* The range type was succesfully read. Save it for the
16451 array type creation. */
791afaa2 16452 range_types.push_back (child_type);
a02abb62 16453 }
c906108c
SS
16454 }
16455 child_die = sibling_die (child_die);
16456 }
16457
16458 /* Dwarf2 dimensions are output from left to right, create the
16459 necessary array types in backwards order. */
7ca2d3a3 16460
c906108c 16461 type = element_type;
7ca2d3a3
DL
16462
16463 if (read_array_order (die, cu) == DW_ORD_col_major)
16464 {
16465 int i = 0;
9a619af0 16466
791afaa2 16467 while (i < range_types.size ())
dc53a7ad 16468 type = create_array_type_with_stride (NULL, type, range_types[i++],
a405673c 16469 byte_stride_prop, bit_stride);
7ca2d3a3
DL
16470 }
16471 else
16472 {
791afaa2 16473 size_t ndim = range_types.size ();
7ca2d3a3 16474 while (ndim-- > 0)
dc53a7ad 16475 type = create_array_type_with_stride (NULL, type, range_types[ndim],
a405673c 16476 byte_stride_prop, bit_stride);
7ca2d3a3 16477 }
c906108c 16478
f5f8a009
EZ
16479 /* Understand Dwarf2 support for vector types (like they occur on
16480 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
16481 array type. This is not part of the Dwarf2/3 standard yet, but a
16482 custom vendor extension. The main difference between a regular
16483 array and the vector variant is that vectors are passed by value
16484 to functions. */
e142c38c 16485 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
f5f8a009 16486 if (attr)
ea37ba09 16487 make_vector_type (type);
f5f8a009 16488
dbc98a8b
KW
16489 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
16490 implementation may choose to implement triple vectors using this
16491 attribute. */
16492 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16493 if (attr)
16494 {
16495 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
16496 TYPE_LENGTH (type) = DW_UNSND (attr);
16497 else
3e43a32a
MS
16498 complaint (&symfile_complaints,
16499 _("DW_AT_byte_size for array type smaller "
16500 "than the total size of elements"));
dbc98a8b
KW
16501 }
16502
39cbfefa
DJ
16503 name = dwarf2_name (die, cu);
16504 if (name)
16505 TYPE_NAME (type) = name;
6e70227d 16506
0963b4bd 16507 /* Install the type in the die. */
7e314c57
JK
16508 set_die_type (die, type, cu);
16509
16510 /* set_die_type should be already done. */
b4ba55a1
JB
16511 set_descriptive_type (type, die, cu);
16512
7e314c57 16513 return type;
c906108c
SS
16514}
16515
7ca2d3a3 16516static enum dwarf_array_dim_ordering
6e70227d 16517read_array_order (struct die_info *die, struct dwarf2_cu *cu)
7ca2d3a3
DL
16518{
16519 struct attribute *attr;
16520
16521 attr = dwarf2_attr (die, DW_AT_ordering, cu);
16522
aead7601
SM
16523 if (attr)
16524 return (enum dwarf_array_dim_ordering) DW_SND (attr);
7ca2d3a3 16525
0963b4bd
MS
16526 /* GNU F77 is a special case, as at 08/2004 array type info is the
16527 opposite order to the dwarf2 specification, but data is still
16528 laid out as per normal fortran.
7ca2d3a3 16529
0963b4bd
MS
16530 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
16531 version checking. */
7ca2d3a3 16532
905e0470
PM
16533 if (cu->language == language_fortran
16534 && cu->producer && strstr (cu->producer, "GNU F77"))
7ca2d3a3
DL
16535 {
16536 return DW_ORD_row_major;
16537 }
16538
6e70227d 16539 switch (cu->language_defn->la_array_ordering)
7ca2d3a3
DL
16540 {
16541 case array_column_major:
16542 return DW_ORD_col_major;
16543 case array_row_major:
16544 default:
16545 return DW_ORD_row_major;
16546 };
16547}
16548
72019c9c 16549/* Extract all information from a DW_TAG_set_type DIE and put it in
0963b4bd 16550 the DIE's type field. */
72019c9c 16551
f792889a 16552static struct type *
72019c9c
GM
16553read_set_type (struct die_info *die, struct dwarf2_cu *cu)
16554{
7e314c57
JK
16555 struct type *domain_type, *set_type;
16556 struct attribute *attr;
f792889a 16557
7e314c57
JK
16558 domain_type = die_type (die, cu);
16559
16560 /* The die_type call above may have already set the type for this DIE. */
16561 set_type = get_die_type (die, cu);
16562 if (set_type)
16563 return set_type;
16564
16565 set_type = create_set_type (NULL, domain_type);
16566
16567 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
d09039dd
PM
16568 if (attr)
16569 TYPE_LENGTH (set_type) = DW_UNSND (attr);
7e314c57 16570
f792889a 16571 return set_die_type (die, set_type, cu);
72019c9c 16572}
7ca2d3a3 16573
0971de02
TT
16574/* A helper for read_common_block that creates a locexpr baton.
16575 SYM is the symbol which we are marking as computed.
16576 COMMON_DIE is the DIE for the common block.
16577 COMMON_LOC is the location expression attribute for the common
16578 block itself.
16579 MEMBER_LOC is the location expression attribute for the particular
16580 member of the common block that we are processing.
16581 CU is the CU from which the above come. */
16582
16583static void
16584mark_common_block_symbol_computed (struct symbol *sym,
16585 struct die_info *common_die,
16586 struct attribute *common_loc,
16587 struct attribute *member_loc,
16588 struct dwarf2_cu *cu)
16589{
518817b3
SM
16590 struct dwarf2_per_objfile *dwarf2_per_objfile
16591 = cu->per_cu->dwarf2_per_objfile;
0971de02
TT
16592 struct objfile *objfile = dwarf2_per_objfile->objfile;
16593 struct dwarf2_locexpr_baton *baton;
16594 gdb_byte *ptr;
16595 unsigned int cu_off;
16596 enum bfd_endian byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
16597 LONGEST offset = 0;
16598
16599 gdb_assert (common_loc && member_loc);
16600 gdb_assert (attr_form_is_block (common_loc));
16601 gdb_assert (attr_form_is_block (member_loc)
16602 || attr_form_is_constant (member_loc));
16603
8d749320 16604 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
0971de02
TT
16605 baton->per_cu = cu->per_cu;
16606 gdb_assert (baton->per_cu);
16607
16608 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
16609
16610 if (attr_form_is_constant (member_loc))
16611 {
16612 offset = dwarf2_get_attr_constant_value (member_loc, 0);
16613 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
16614 }
16615 else
16616 baton->size += DW_BLOCK (member_loc)->size;
16617
224c3ddb 16618 ptr = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, baton->size);
0971de02
TT
16619 baton->data = ptr;
16620
16621 *ptr++ = DW_OP_call4;
9c541725 16622 cu_off = common_die->sect_off - cu->per_cu->sect_off;
0971de02
TT
16623 store_unsigned_integer (ptr, 4, byte_order, cu_off);
16624 ptr += 4;
16625
16626 if (attr_form_is_constant (member_loc))
16627 {
16628 *ptr++ = DW_OP_addr;
16629 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
16630 ptr += cu->header.addr_size;
16631 }
16632 else
16633 {
16634 /* We have to copy the data here, because DW_OP_call4 will only
16635 use a DW_AT_location attribute. */
16636 memcpy (ptr, DW_BLOCK (member_loc)->data, DW_BLOCK (member_loc)->size);
16637 ptr += DW_BLOCK (member_loc)->size;
16638 }
16639
16640 *ptr++ = DW_OP_plus;
16641 gdb_assert (ptr - baton->data == baton->size);
16642
0971de02 16643 SYMBOL_LOCATION_BATON (sym) = baton;
f1e6e072 16644 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
0971de02
TT
16645}
16646
4357ac6c
TT
16647/* Create appropriate locally-scoped variables for all the
16648 DW_TAG_common_block entries. Also create a struct common_block
16649 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
16650 is used to sepate the common blocks name namespace from regular
16651 variable names. */
c906108c
SS
16652
16653static void
e7c27a73 16654read_common_block (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16655{
0971de02
TT
16656 struct attribute *attr;
16657
16658 attr = dwarf2_attr (die, DW_AT_location, cu);
16659 if (attr)
16660 {
16661 /* Support the .debug_loc offsets. */
16662 if (attr_form_is_block (attr))
16663 {
16664 /* Ok. */
16665 }
16666 else if (attr_form_is_section_offset (attr))
16667 {
16668 dwarf2_complex_location_expr_complaint ();
16669 attr = NULL;
16670 }
16671 else
16672 {
16673 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
16674 "common block member");
16675 attr = NULL;
16676 }
16677 }
16678
639d11d3 16679 if (die->child != NULL)
c906108c 16680 {
518817b3 16681 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
4357ac6c
TT
16682 struct die_info *child_die;
16683 size_t n_entries = 0, size;
16684 struct common_block *common_block;
16685 struct symbol *sym;
74ac6d43 16686
4357ac6c
TT
16687 for (child_die = die->child;
16688 child_die && child_die->tag;
16689 child_die = sibling_die (child_die))
16690 ++n_entries;
16691
16692 size = (sizeof (struct common_block)
16693 + (n_entries - 1) * sizeof (struct symbol *));
224c3ddb
SM
16694 common_block
16695 = (struct common_block *) obstack_alloc (&objfile->objfile_obstack,
16696 size);
4357ac6c
TT
16697 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
16698 common_block->n_entries = 0;
16699
16700 for (child_die = die->child;
16701 child_die && child_die->tag;
16702 child_die = sibling_die (child_die))
16703 {
16704 /* Create the symbol in the DW_TAG_common_block block in the current
16705 symbol scope. */
e7c27a73 16706 sym = new_symbol (child_die, NULL, cu);
0971de02
TT
16707 if (sym != NULL)
16708 {
16709 struct attribute *member_loc;
16710
16711 common_block->contents[common_block->n_entries++] = sym;
16712
16713 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
16714 cu);
16715 if (member_loc)
16716 {
16717 /* GDB has handled this for a long time, but it is
16718 not specified by DWARF. It seems to have been
16719 emitted by gfortran at least as recently as:
16720 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
16721 complaint (&symfile_complaints,
16722 _("Variable in common block has "
16723 "DW_AT_data_member_location "
9d8780f0
SM
16724 "- DIE at %s [in module %s]"),
16725 sect_offset_str (child_die->sect_off),
518817b3 16726 objfile_name (objfile));
0971de02
TT
16727
16728 if (attr_form_is_section_offset (member_loc))
16729 dwarf2_complex_location_expr_complaint ();
16730 else if (attr_form_is_constant (member_loc)
16731 || attr_form_is_block (member_loc))
16732 {
16733 if (attr)
16734 mark_common_block_symbol_computed (sym, die, attr,
16735 member_loc, cu);
16736 }
16737 else
16738 dwarf2_complex_location_expr_complaint ();
16739 }
16740 }
c906108c 16741 }
4357ac6c
TT
16742
16743 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
16744 SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
c906108c
SS
16745 }
16746}
16747
0114d602 16748/* Create a type for a C++ namespace. */
d9fa45fe 16749
0114d602
DJ
16750static struct type *
16751read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
d9fa45fe 16752{
518817b3 16753 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
0114d602 16754 const char *previous_prefix, *name;
9219021c 16755 int is_anonymous;
0114d602
DJ
16756 struct type *type;
16757
16758 /* For extensions, reuse the type of the original namespace. */
16759 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
16760 {
16761 struct die_info *ext_die;
16762 struct dwarf2_cu *ext_cu = cu;
9a619af0 16763
0114d602
DJ
16764 ext_die = dwarf2_extension (die, &ext_cu);
16765 type = read_type_die (ext_die, ext_cu);
9dc481d3
DE
16766
16767 /* EXT_CU may not be the same as CU.
02142a6c 16768 Ensure TYPE is recorded with CU in die_type_hash. */
0114d602
DJ
16769 return set_die_type (die, type, cu);
16770 }
9219021c 16771
e142c38c 16772 name = namespace_name (die, &is_anonymous, cu);
9219021c
DC
16773
16774 /* Now build the name of the current namespace. */
16775
0114d602
DJ
16776 previous_prefix = determine_prefix (die, cu);
16777 if (previous_prefix[0] != '\0')
16778 name = typename_concat (&objfile->objfile_obstack,
f55ee35c 16779 previous_prefix, name, 0, cu);
0114d602
DJ
16780
16781 /* Create the type. */
19f392bc 16782 type = init_type (objfile, TYPE_CODE_NAMESPACE, 0, name);
0114d602
DJ
16783 TYPE_TAG_NAME (type) = TYPE_NAME (type);
16784
60531b24 16785 return set_die_type (die, type, cu);
0114d602
DJ
16786}
16787
22cee43f 16788/* Read a namespace scope. */
0114d602
DJ
16789
16790static void
16791read_namespace (struct die_info *die, struct dwarf2_cu *cu)
16792{
518817b3 16793 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
0114d602 16794 int is_anonymous;
9219021c 16795
5c4e30ca
DC
16796 /* Add a symbol associated to this if we haven't seen the namespace
16797 before. Also, add a using directive if it's an anonymous
16798 namespace. */
9219021c 16799
f2f0e013 16800 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
5c4e30ca
DC
16801 {
16802 struct type *type;
16803
0114d602 16804 type = read_type_die (die, cu);
e7c27a73 16805 new_symbol (die, type, cu);
5c4e30ca 16806
e8e80198 16807 namespace_name (die, &is_anonymous, cu);
5c4e30ca 16808 if (is_anonymous)
0114d602
DJ
16809 {
16810 const char *previous_prefix = determine_prefix (die, cu);
9a619af0 16811
eb1e02fd 16812 std::vector<const char *> excludes;
22cee43f
PMR
16813 add_using_directive (using_directives (cu->language),
16814 previous_prefix, TYPE_NAME (type), NULL,
eb1e02fd 16815 NULL, excludes, 0, &objfile->objfile_obstack);
0114d602 16816 }
5c4e30ca 16817 }
9219021c 16818
639d11d3 16819 if (die->child != NULL)
d9fa45fe 16820 {
639d11d3 16821 struct die_info *child_die = die->child;
6e70227d 16822
d9fa45fe
DC
16823 while (child_die && child_die->tag)
16824 {
e7c27a73 16825 process_die (child_die, cu);
d9fa45fe
DC
16826 child_die = sibling_die (child_die);
16827 }
16828 }
38d518c9
EZ
16829}
16830
f55ee35c
JK
16831/* Read a Fortran module as type. This DIE can be only a declaration used for
16832 imported module. Still we need that type as local Fortran "use ... only"
16833 declaration imports depend on the created type in determine_prefix. */
16834
16835static struct type *
16836read_module_type (struct die_info *die, struct dwarf2_cu *cu)
16837{
518817b3 16838 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15d034d0 16839 const char *module_name;
f55ee35c
JK
16840 struct type *type;
16841
16842 module_name = dwarf2_name (die, cu);
16843 if (!module_name)
3e43a32a 16844 complaint (&symfile_complaints,
9d8780f0
SM
16845 _("DW_TAG_module has no name, offset %s"),
16846 sect_offset_str (die->sect_off));
19f392bc 16847 type = init_type (objfile, TYPE_CODE_MODULE, 0, module_name);
f55ee35c
JK
16848
16849 /* determine_prefix uses TYPE_TAG_NAME. */
16850 TYPE_TAG_NAME (type) = TYPE_NAME (type);
16851
16852 return set_die_type (die, type, cu);
16853}
16854
5d7cb8df
JK
16855/* Read a Fortran module. */
16856
16857static void
16858read_module (struct die_info *die, struct dwarf2_cu *cu)
16859{
16860 struct die_info *child_die = die->child;
530e8392
KB
16861 struct type *type;
16862
16863 type = read_type_die (die, cu);
16864 new_symbol (die, type, cu);
5d7cb8df 16865
5d7cb8df
JK
16866 while (child_die && child_die->tag)
16867 {
16868 process_die (child_die, cu);
16869 child_die = sibling_die (child_die);
16870 }
16871}
16872
38d518c9
EZ
16873/* Return the name of the namespace represented by DIE. Set
16874 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
16875 namespace. */
16876
16877static const char *
e142c38c 16878namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
38d518c9
EZ
16879{
16880 struct die_info *current_die;
16881 const char *name = NULL;
16882
16883 /* Loop through the extensions until we find a name. */
16884
16885 for (current_die = die;
16886 current_die != NULL;
f2f0e013 16887 current_die = dwarf2_extension (die, &cu))
38d518c9 16888 {
96553a0c
DE
16889 /* We don't use dwarf2_name here so that we can detect the absence
16890 of a name -> anonymous namespace. */
7d45c7c3 16891 name = dwarf2_string_attr (die, DW_AT_name, cu);
96553a0c 16892
38d518c9
EZ
16893 if (name != NULL)
16894 break;
16895 }
16896
16897 /* Is it an anonymous namespace? */
16898
16899 *is_anonymous = (name == NULL);
16900 if (*is_anonymous)
2b1dbab0 16901 name = CP_ANONYMOUS_NAMESPACE_STR;
38d518c9
EZ
16902
16903 return name;
d9fa45fe
DC
16904}
16905
c906108c
SS
16906/* Extract all information from a DW_TAG_pointer_type DIE and add to
16907 the user defined type vector. */
16908
f792889a 16909static struct type *
e7c27a73 16910read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16911{
518817b3
SM
16912 struct gdbarch *gdbarch
16913 = get_objfile_arch (cu->per_cu->dwarf2_per_objfile->objfile);
e7c27a73 16914 struct comp_unit_head *cu_header = &cu->header;
c906108c 16915 struct type *type;
8b2dbe47
KB
16916 struct attribute *attr_byte_size;
16917 struct attribute *attr_address_class;
16918 int byte_size, addr_class;
7e314c57
JK
16919 struct type *target_type;
16920
16921 target_type = die_type (die, cu);
c906108c 16922
7e314c57
JK
16923 /* The die_type call above may have already set the type for this DIE. */
16924 type = get_die_type (die, cu);
16925 if (type)
16926 return type;
16927
16928 type = lookup_pointer_type (target_type);
8b2dbe47 16929
e142c38c 16930 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
8b2dbe47
KB
16931 if (attr_byte_size)
16932 byte_size = DW_UNSND (attr_byte_size);
c906108c 16933 else
8b2dbe47
KB
16934 byte_size = cu_header->addr_size;
16935
e142c38c 16936 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
8b2dbe47
KB
16937 if (attr_address_class)
16938 addr_class = DW_UNSND (attr_address_class);
16939 else
16940 addr_class = DW_ADDR_none;
16941
16942 /* If the pointer size or address class is different than the
16943 default, create a type variant marked as such and set the
16944 length accordingly. */
16945 if (TYPE_LENGTH (type) != byte_size || addr_class != DW_ADDR_none)
c906108c 16946 {
5e2b427d 16947 if (gdbarch_address_class_type_flags_p (gdbarch))
8b2dbe47
KB
16948 {
16949 int type_flags;
16950
849957d9 16951 type_flags = gdbarch_address_class_type_flags
5e2b427d 16952 (gdbarch, byte_size, addr_class);
876cecd0
TT
16953 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
16954 == 0);
8b2dbe47
KB
16955 type = make_type_with_address_space (type, type_flags);
16956 }
16957 else if (TYPE_LENGTH (type) != byte_size)
16958 {
3e43a32a
MS
16959 complaint (&symfile_complaints,
16960 _("invalid pointer size %d"), byte_size);
8b2dbe47 16961 }
6e70227d 16962 else
9a619af0
MS
16963 {
16964 /* Should we also complain about unhandled address classes? */
16965 }
c906108c 16966 }
8b2dbe47
KB
16967
16968 TYPE_LENGTH (type) = byte_size;
f792889a 16969 return set_die_type (die, type, cu);
c906108c
SS
16970}
16971
16972/* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
16973 the user defined type vector. */
16974
f792889a 16975static struct type *
e7c27a73 16976read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
16977{
16978 struct type *type;
16979 struct type *to_type;
16980 struct type *domain;
16981
e7c27a73
DJ
16982 to_type = die_type (die, cu);
16983 domain = die_containing_type (die, cu);
0d5de010 16984
7e314c57
JK
16985 /* The calls above may have already set the type for this DIE. */
16986 type = get_die_type (die, cu);
16987 if (type)
16988 return type;
16989
0d5de010
DJ
16990 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
16991 type = lookup_methodptr_type (to_type);
7078baeb
TT
16992 else if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_FUNC)
16993 {
518817b3
SM
16994 struct type *new_type
16995 = alloc_type (cu->per_cu->dwarf2_per_objfile->objfile);
7078baeb
TT
16996
16997 smash_to_method_type (new_type, domain, TYPE_TARGET_TYPE (to_type),
16998 TYPE_FIELDS (to_type), TYPE_NFIELDS (to_type),
16999 TYPE_VARARGS (to_type));
17000 type = lookup_methodptr_type (new_type);
17001 }
0d5de010
DJ
17002 else
17003 type = lookup_memberptr_type (to_type, domain);
c906108c 17004
f792889a 17005 return set_die_type (die, type, cu);
c906108c
SS
17006}
17007
4297a3f0 17008/* Extract all information from a DW_TAG_{rvalue_,}reference_type DIE and add to
c906108c
SS
17009 the user defined type vector. */
17010
f792889a 17011static struct type *
4297a3f0
AV
17012read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu,
17013 enum type_code refcode)
c906108c 17014{
e7c27a73 17015 struct comp_unit_head *cu_header = &cu->header;
7e314c57 17016 struct type *type, *target_type;
c906108c
SS
17017 struct attribute *attr;
17018
4297a3f0
AV
17019 gdb_assert (refcode == TYPE_CODE_REF || refcode == TYPE_CODE_RVALUE_REF);
17020
7e314c57
JK
17021 target_type = die_type (die, cu);
17022
17023 /* The die_type call above may have already set the type for this DIE. */
17024 type = get_die_type (die, cu);
17025 if (type)
17026 return type;
17027
4297a3f0 17028 type = lookup_reference_type (target_type, refcode);
e142c38c 17029 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
17030 if (attr)
17031 {
17032 TYPE_LENGTH (type) = DW_UNSND (attr);
17033 }
17034 else
17035 {
107d2387 17036 TYPE_LENGTH (type) = cu_header->addr_size;
c906108c 17037 }
f792889a 17038 return set_die_type (die, type, cu);
c906108c
SS
17039}
17040
cf363f18
MW
17041/* Add the given cv-qualifiers to the element type of the array. GCC
17042 outputs DWARF type qualifiers that apply to an array, not the
17043 element type. But GDB relies on the array element type to carry
17044 the cv-qualifiers. This mimics section 6.7.3 of the C99
17045 specification. */
17046
17047static struct type *
17048add_array_cv_type (struct die_info *die, struct dwarf2_cu *cu,
17049 struct type *base_type, int cnst, int voltl)
17050{
17051 struct type *el_type, *inner_array;
17052
17053 base_type = copy_type (base_type);
17054 inner_array = base_type;
17055
17056 while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
17057 {
17058 TYPE_TARGET_TYPE (inner_array) =
17059 copy_type (TYPE_TARGET_TYPE (inner_array));
17060 inner_array = TYPE_TARGET_TYPE (inner_array);
17061 }
17062
17063 el_type = TYPE_TARGET_TYPE (inner_array);
17064 cnst |= TYPE_CONST (el_type);
17065 voltl |= TYPE_VOLATILE (el_type);
17066 TYPE_TARGET_TYPE (inner_array) = make_cv_type (cnst, voltl, el_type, NULL);
17067
17068 return set_die_type (die, base_type, cu);
17069}
17070
f792889a 17071static struct type *
e7c27a73 17072read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17073{
f792889a 17074 struct type *base_type, *cv_type;
c906108c 17075
e7c27a73 17076 base_type = die_type (die, cu);
7e314c57
JK
17077
17078 /* The die_type call above may have already set the type for this DIE. */
17079 cv_type = get_die_type (die, cu);
17080 if (cv_type)
17081 return cv_type;
17082
2f608a3a
KW
17083 /* In case the const qualifier is applied to an array type, the element type
17084 is so qualified, not the array type (section 6.7.3 of C99). */
17085 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
cf363f18 17086 return add_array_cv_type (die, cu, base_type, 1, 0);
2f608a3a 17087
f792889a
DJ
17088 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
17089 return set_die_type (die, cv_type, cu);
c906108c
SS
17090}
17091
f792889a 17092static struct type *
e7c27a73 17093read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17094{
f792889a 17095 struct type *base_type, *cv_type;
c906108c 17096
e7c27a73 17097 base_type = die_type (die, cu);
7e314c57
JK
17098
17099 /* The die_type call above may have already set the type for this DIE. */
17100 cv_type = get_die_type (die, cu);
17101 if (cv_type)
17102 return cv_type;
17103
cf363f18
MW
17104 /* In case the volatile qualifier is applied to an array type, the
17105 element type is so qualified, not the array type (section 6.7.3
17106 of C99). */
17107 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
17108 return add_array_cv_type (die, cu, base_type, 0, 1);
17109
f792889a
DJ
17110 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
17111 return set_die_type (die, cv_type, cu);
c906108c
SS
17112}
17113
06d66ee9
TT
17114/* Handle DW_TAG_restrict_type. */
17115
17116static struct type *
17117read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
17118{
17119 struct type *base_type, *cv_type;
17120
17121 base_type = die_type (die, cu);
17122
17123 /* The die_type call above may have already set the type for this DIE. */
17124 cv_type = get_die_type (die, cu);
17125 if (cv_type)
17126 return cv_type;
17127
17128 cv_type = make_restrict_type (base_type);
17129 return set_die_type (die, cv_type, cu);
17130}
17131
a2c2acaf
MW
17132/* Handle DW_TAG_atomic_type. */
17133
17134static struct type *
17135read_tag_atomic_type (struct die_info *die, struct dwarf2_cu *cu)
17136{
17137 struct type *base_type, *cv_type;
17138
17139 base_type = die_type (die, cu);
17140
17141 /* The die_type call above may have already set the type for this DIE. */
17142 cv_type = get_die_type (die, cu);
17143 if (cv_type)
17144 return cv_type;
17145
17146 cv_type = make_atomic_type (base_type);
17147 return set_die_type (die, cv_type, cu);
17148}
17149
c906108c
SS
17150/* Extract all information from a DW_TAG_string_type DIE and add to
17151 the user defined type vector. It isn't really a user defined type,
17152 but it behaves like one, with other DIE's using an AT_user_def_type
17153 attribute to reference it. */
17154
f792889a 17155static struct type *
e7c27a73 17156read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17157{
518817b3 17158 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
3b7538c0 17159 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
17160 struct type *type, *range_type, *index_type, *char_type;
17161 struct attribute *attr;
17162 unsigned int length;
17163
e142c38c 17164 attr = dwarf2_attr (die, DW_AT_string_length, cu);
c906108c
SS
17165 if (attr)
17166 {
17167 length = DW_UNSND (attr);
17168 }
17169 else
17170 {
0963b4bd 17171 /* Check for the DW_AT_byte_size attribute. */
e142c38c 17172 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
b21b22e0
PS
17173 if (attr)
17174 {
17175 length = DW_UNSND (attr);
17176 }
17177 else
17178 {
17179 length = 1;
17180 }
c906108c 17181 }
6ccb9162 17182
46bf5051 17183 index_type = objfile_type (objfile)->builtin_int;
0c9c3474 17184 range_type = create_static_range_type (NULL, index_type, 1, length);
3b7538c0
UW
17185 char_type = language_string_char_type (cu->language_defn, gdbarch);
17186 type = create_string_type (NULL, char_type, range_type);
6ccb9162 17187
f792889a 17188 return set_die_type (die, type, cu);
c906108c
SS
17189}
17190
4d804846
JB
17191/* Assuming that DIE corresponds to a function, returns nonzero
17192 if the function is prototyped. */
17193
17194static int
17195prototyped_function_p (struct die_info *die, struct dwarf2_cu *cu)
17196{
17197 struct attribute *attr;
17198
17199 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
17200 if (attr && (DW_UNSND (attr) != 0))
17201 return 1;
17202
17203 /* The DWARF standard implies that the DW_AT_prototyped attribute
17204 is only meaninful for C, but the concept also extends to other
17205 languages that allow unprototyped functions (Eg: Objective C).
17206 For all other languages, assume that functions are always
17207 prototyped. */
17208 if (cu->language != language_c
17209 && cu->language != language_objc
17210 && cu->language != language_opencl)
17211 return 1;
17212
17213 /* RealView does not emit DW_AT_prototyped. We can not distinguish
17214 prototyped and unprototyped functions; default to prototyped,
17215 since that is more common in modern code (and RealView warns
17216 about unprototyped functions). */
17217 if (producer_is_realview (cu->producer))
17218 return 1;
17219
17220 return 0;
17221}
17222
c906108c
SS
17223/* Handle DIES due to C code like:
17224
17225 struct foo
c5aa993b
JM
17226 {
17227 int (*funcp)(int a, long l);
17228 int b;
17229 };
c906108c 17230
0963b4bd 17231 ('funcp' generates a DW_TAG_subroutine_type DIE). */
c906108c 17232
f792889a 17233static struct type *
e7c27a73 17234read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17235{
518817b3 17236 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
0963b4bd
MS
17237 struct type *type; /* Type that this function returns. */
17238 struct type *ftype; /* Function that returns above type. */
c906108c
SS
17239 struct attribute *attr;
17240
e7c27a73 17241 type = die_type (die, cu);
7e314c57
JK
17242
17243 /* The die_type call above may have already set the type for this DIE. */
17244 ftype = get_die_type (die, cu);
17245 if (ftype)
17246 return ftype;
17247
0c8b41f1 17248 ftype = lookup_function_type (type);
c906108c 17249
4d804846 17250 if (prototyped_function_p (die, cu))
a6c727b2 17251 TYPE_PROTOTYPED (ftype) = 1;
c906108c 17252
c055b101
CV
17253 /* Store the calling convention in the type if it's available in
17254 the subroutine die. Otherwise set the calling convention to
17255 the default value DW_CC_normal. */
17256 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
54fcddd0
UW
17257 if (attr)
17258 TYPE_CALLING_CONVENTION (ftype) = DW_UNSND (attr);
17259 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
17260 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
17261 else
17262 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
76c10ea2 17263
743649fd
MW
17264 /* Record whether the function returns normally to its caller or not
17265 if the DWARF producer set that information. */
17266 attr = dwarf2_attr (die, DW_AT_noreturn, cu);
17267 if (attr && (DW_UNSND (attr) != 0))
17268 TYPE_NO_RETURN (ftype) = 1;
17269
76c10ea2
GM
17270 /* We need to add the subroutine type to the die immediately so
17271 we don't infinitely recurse when dealing with parameters
0963b4bd 17272 declared as the same subroutine type. */
76c10ea2 17273 set_die_type (die, ftype, cu);
6e70227d 17274
639d11d3 17275 if (die->child != NULL)
c906108c 17276 {
bb5ed363 17277 struct type *void_type = objfile_type (objfile)->builtin_void;
c906108c 17278 struct die_info *child_die;
8072405b 17279 int nparams, iparams;
c906108c
SS
17280
17281 /* Count the number of parameters.
17282 FIXME: GDB currently ignores vararg functions, but knows about
17283 vararg member functions. */
8072405b 17284 nparams = 0;
639d11d3 17285 child_die = die->child;
c906108c
SS
17286 while (child_die && child_die->tag)
17287 {
17288 if (child_die->tag == DW_TAG_formal_parameter)
17289 nparams++;
17290 else if (child_die->tag == DW_TAG_unspecified_parameters)
876cecd0 17291 TYPE_VARARGS (ftype) = 1;
c906108c
SS
17292 child_die = sibling_die (child_die);
17293 }
17294
17295 /* Allocate storage for parameters and fill them in. */
17296 TYPE_NFIELDS (ftype) = nparams;
17297 TYPE_FIELDS (ftype) = (struct field *)
ae5a43e0 17298 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
c906108c 17299
8072405b
JK
17300 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
17301 even if we error out during the parameters reading below. */
17302 for (iparams = 0; iparams < nparams; iparams++)
17303 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
17304
17305 iparams = 0;
639d11d3 17306 child_die = die->child;
c906108c
SS
17307 while (child_die && child_die->tag)
17308 {
17309 if (child_die->tag == DW_TAG_formal_parameter)
17310 {
3ce3b1ba
PA
17311 struct type *arg_type;
17312
17313 /* DWARF version 2 has no clean way to discern C++
17314 static and non-static member functions. G++ helps
17315 GDB by marking the first parameter for non-static
17316 member functions (which is the this pointer) as
17317 artificial. We pass this information to
17318 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
17319
17320 DWARF version 3 added DW_AT_object_pointer, which GCC
17321 4.5 does not yet generate. */
e142c38c 17322 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
c906108c
SS
17323 if (attr)
17324 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
17325 else
9c37b5ae 17326 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
3ce3b1ba
PA
17327 arg_type = die_type (child_die, cu);
17328
17329 /* RealView does not mark THIS as const, which the testsuite
17330 expects. GCC marks THIS as const in method definitions,
17331 but not in the class specifications (GCC PR 43053). */
17332 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
17333 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
17334 {
17335 int is_this = 0;
17336 struct dwarf2_cu *arg_cu = cu;
17337 const char *name = dwarf2_name (child_die, cu);
17338
17339 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
17340 if (attr)
17341 {
17342 /* If the compiler emits this, use it. */
17343 if (follow_die_ref (die, attr, &arg_cu) == child_die)
17344 is_this = 1;
17345 }
17346 else if (name && strcmp (name, "this") == 0)
17347 /* Function definitions will have the argument names. */
17348 is_this = 1;
17349 else if (name == NULL && iparams == 0)
17350 /* Declarations may not have the names, so like
17351 elsewhere in GDB, assume an artificial first
17352 argument is "this". */
17353 is_this = 1;
17354
17355 if (is_this)
17356 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
17357 arg_type, 0);
17358 }
17359
17360 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
c906108c
SS
17361 iparams++;
17362 }
17363 child_die = sibling_die (child_die);
17364 }
17365 }
17366
76c10ea2 17367 return ftype;
c906108c
SS
17368}
17369
f792889a 17370static struct type *
e7c27a73 17371read_typedef (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17372{
518817b3 17373 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
0114d602 17374 const char *name = NULL;
3c8e0968 17375 struct type *this_type, *target_type;
c906108c 17376
94af9270 17377 name = dwarf2_full_name (NULL, die, cu);
19f392bc
UW
17378 this_type = init_type (objfile, TYPE_CODE_TYPEDEF, 0, name);
17379 TYPE_TARGET_STUB (this_type) = 1;
f792889a 17380 set_die_type (die, this_type, cu);
3c8e0968
DE
17381 target_type = die_type (die, cu);
17382 if (target_type != this_type)
17383 TYPE_TARGET_TYPE (this_type) = target_type;
17384 else
17385 {
17386 /* Self-referential typedefs are, it seems, not allowed by the DWARF
17387 spec and cause infinite loops in GDB. */
17388 complaint (&symfile_complaints,
17389 _("Self-referential DW_TAG_typedef "
9d8780f0
SM
17390 "- DIE at %s [in module %s]"),
17391 sect_offset_str (die->sect_off), objfile_name (objfile));
3c8e0968
DE
17392 TYPE_TARGET_TYPE (this_type) = NULL;
17393 }
f792889a 17394 return this_type;
c906108c
SS
17395}
17396
9b790ce7
UW
17397/* Allocate a floating-point type of size BITS and name NAME. Pass NAME_HINT
17398 (which may be different from NAME) to the architecture back-end to allow
17399 it to guess the correct format if necessary. */
17400
17401static struct type *
17402dwarf2_init_float_type (struct objfile *objfile, int bits, const char *name,
17403 const char *name_hint)
17404{
17405 struct gdbarch *gdbarch = get_objfile_arch (objfile);
17406 const struct floatformat **format;
17407 struct type *type;
17408
17409 format = gdbarch_floatformat_for_type (gdbarch, name_hint, bits);
17410 if (format)
17411 type = init_float_type (objfile, bits, name, format);
17412 else
77b7c781 17413 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
9b790ce7
UW
17414
17415 return type;
17416}
17417
c906108c
SS
17418/* Find a representation of a given base type and install
17419 it in the TYPE field of the die. */
17420
f792889a 17421static struct type *
e7c27a73 17422read_base_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17423{
518817b3 17424 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c
SS
17425 struct type *type;
17426 struct attribute *attr;
19f392bc 17427 int encoding = 0, bits = 0;
15d034d0 17428 const char *name;
c906108c 17429
e142c38c 17430 attr = dwarf2_attr (die, DW_AT_encoding, cu);
c906108c
SS
17431 if (attr)
17432 {
17433 encoding = DW_UNSND (attr);
17434 }
e142c38c 17435 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
17436 if (attr)
17437 {
19f392bc 17438 bits = DW_UNSND (attr) * TARGET_CHAR_BIT;
c906108c 17439 }
39cbfefa 17440 name = dwarf2_name (die, cu);
6ccb9162 17441 if (!name)
c906108c 17442 {
6ccb9162
UW
17443 complaint (&symfile_complaints,
17444 _("DW_AT_name missing from DW_TAG_base_type"));
c906108c 17445 }
6ccb9162
UW
17446
17447 switch (encoding)
c906108c 17448 {
6ccb9162
UW
17449 case DW_ATE_address:
17450 /* Turn DW_ATE_address into a void * pointer. */
77b7c781 17451 type = init_type (objfile, TYPE_CODE_VOID, TARGET_CHAR_BIT, NULL);
19f392bc 17452 type = init_pointer_type (objfile, bits, name, type);
6ccb9162
UW
17453 break;
17454 case DW_ATE_boolean:
19f392bc 17455 type = init_boolean_type (objfile, bits, 1, name);
6ccb9162
UW
17456 break;
17457 case DW_ATE_complex_float:
9b790ce7 17458 type = dwarf2_init_float_type (objfile, bits / 2, NULL, name);
19f392bc 17459 type = init_complex_type (objfile, name, type);
6ccb9162
UW
17460 break;
17461 case DW_ATE_decimal_float:
19f392bc 17462 type = init_decfloat_type (objfile, bits, name);
6ccb9162
UW
17463 break;
17464 case DW_ATE_float:
9b790ce7 17465 type = dwarf2_init_float_type (objfile, bits, name, name);
6ccb9162
UW
17466 break;
17467 case DW_ATE_signed:
19f392bc 17468 type = init_integer_type (objfile, bits, 0, name);
6ccb9162
UW
17469 break;
17470 case DW_ATE_unsigned:
3b2b8fea
TT
17471 if (cu->language == language_fortran
17472 && name
61012eef 17473 && startswith (name, "character("))
19f392bc
UW
17474 type = init_character_type (objfile, bits, 1, name);
17475 else
17476 type = init_integer_type (objfile, bits, 1, name);
6ccb9162
UW
17477 break;
17478 case DW_ATE_signed_char:
6e70227d 17479 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea
TT
17480 || cu->language == language_pascal
17481 || cu->language == language_fortran)
19f392bc
UW
17482 type = init_character_type (objfile, bits, 0, name);
17483 else
17484 type = init_integer_type (objfile, bits, 0, name);
6ccb9162
UW
17485 break;
17486 case DW_ATE_unsigned_char:
868a0084 17487 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea 17488 || cu->language == language_pascal
c44af4eb
TT
17489 || cu->language == language_fortran
17490 || cu->language == language_rust)
19f392bc
UW
17491 type = init_character_type (objfile, bits, 1, name);
17492 else
17493 type = init_integer_type (objfile, bits, 1, name);
6ccb9162 17494 break;
75079b2b 17495 case DW_ATE_UTF:
53e710ac
PA
17496 {
17497 gdbarch *arch = get_objfile_arch (objfile);
17498
17499 if (bits == 16)
17500 type = builtin_type (arch)->builtin_char16;
17501 else if (bits == 32)
17502 type = builtin_type (arch)->builtin_char32;
17503 else
17504 {
17505 complaint (&symfile_complaints,
17506 _("unsupported DW_ATE_UTF bit size: '%d'"),
17507 bits);
17508 type = init_integer_type (objfile, bits, 1, name);
17509 }
17510 return set_die_type (die, type, cu);
17511 }
75079b2b
TT
17512 break;
17513
6ccb9162
UW
17514 default:
17515 complaint (&symfile_complaints, _("unsupported DW_AT_encoding: '%s'"),
17516 dwarf_type_encoding_name (encoding));
77b7c781 17517 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
6ccb9162 17518 break;
c906108c 17519 }
6ccb9162 17520
0114d602 17521 if (name && strcmp (name, "char") == 0)
876cecd0 17522 TYPE_NOSIGN (type) = 1;
0114d602 17523
f792889a 17524 return set_die_type (die, type, cu);
c906108c
SS
17525}
17526
80180f79
SA
17527/* Parse dwarf attribute if it's a block, reference or constant and put the
17528 resulting value of the attribute into struct bound_prop.
17529 Returns 1 if ATTR could be resolved into PROP, 0 otherwise. */
17530
17531static int
17532attr_to_dynamic_prop (const struct attribute *attr, struct die_info *die,
17533 struct dwarf2_cu *cu, struct dynamic_prop *prop)
17534{
17535 struct dwarf2_property_baton *baton;
518817b3
SM
17536 struct obstack *obstack
17537 = &cu->per_cu->dwarf2_per_objfile->objfile->objfile_obstack;
80180f79
SA
17538
17539 if (attr == NULL || prop == NULL)
17540 return 0;
17541
17542 if (attr_form_is_block (attr))
17543 {
8d749320 17544 baton = XOBNEW (obstack, struct dwarf2_property_baton);
80180f79
SA
17545 baton->referenced_type = NULL;
17546 baton->locexpr.per_cu = cu->per_cu;
17547 baton->locexpr.size = DW_BLOCK (attr)->size;
17548 baton->locexpr.data = DW_BLOCK (attr)->data;
17549 prop->data.baton = baton;
17550 prop->kind = PROP_LOCEXPR;
17551 gdb_assert (prop->data.baton != NULL);
17552 }
17553 else if (attr_form_is_ref (attr))
17554 {
17555 struct dwarf2_cu *target_cu = cu;
17556 struct die_info *target_die;
17557 struct attribute *target_attr;
17558
17559 target_die = follow_die_ref (die, attr, &target_cu);
17560 target_attr = dwarf2_attr (target_die, DW_AT_location, target_cu);
df25ebbd
JB
17561 if (target_attr == NULL)
17562 target_attr = dwarf2_attr (target_die, DW_AT_data_member_location,
17563 target_cu);
80180f79
SA
17564 if (target_attr == NULL)
17565 return 0;
17566
df25ebbd 17567 switch (target_attr->name)
80180f79 17568 {
df25ebbd
JB
17569 case DW_AT_location:
17570 if (attr_form_is_section_offset (target_attr))
17571 {
8d749320 17572 baton = XOBNEW (obstack, struct dwarf2_property_baton);
df25ebbd
JB
17573 baton->referenced_type = die_type (target_die, target_cu);
17574 fill_in_loclist_baton (cu, &baton->loclist, target_attr);
17575 prop->data.baton = baton;
17576 prop->kind = PROP_LOCLIST;
17577 gdb_assert (prop->data.baton != NULL);
17578 }
17579 else if (attr_form_is_block (target_attr))
17580 {
8d749320 17581 baton = XOBNEW (obstack, struct dwarf2_property_baton);
df25ebbd
JB
17582 baton->referenced_type = die_type (target_die, target_cu);
17583 baton->locexpr.per_cu = cu->per_cu;
17584 baton->locexpr.size = DW_BLOCK (target_attr)->size;
17585 baton->locexpr.data = DW_BLOCK (target_attr)->data;
17586 prop->data.baton = baton;
17587 prop->kind = PROP_LOCEXPR;
17588 gdb_assert (prop->data.baton != NULL);
17589 }
17590 else
17591 {
17592 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
17593 "dynamic property");
17594 return 0;
17595 }
17596 break;
17597 case DW_AT_data_member_location:
17598 {
17599 LONGEST offset;
17600
17601 if (!handle_data_member_location (target_die, target_cu,
17602 &offset))
17603 return 0;
17604
8d749320 17605 baton = XOBNEW (obstack, struct dwarf2_property_baton);
6ad395a7
JB
17606 baton->referenced_type = read_type_die (target_die->parent,
17607 target_cu);
df25ebbd
JB
17608 baton->offset_info.offset = offset;
17609 baton->offset_info.type = die_type (target_die, target_cu);
17610 prop->data.baton = baton;
17611 prop->kind = PROP_ADDR_OFFSET;
17612 break;
17613 }
80180f79
SA
17614 }
17615 }
17616 else if (attr_form_is_constant (attr))
17617 {
17618 prop->data.const_val = dwarf2_get_attr_constant_value (attr, 0);
17619 prop->kind = PROP_CONST;
17620 }
17621 else
17622 {
17623 dwarf2_invalid_attrib_class_complaint (dwarf_form_name (attr->form),
17624 dwarf2_name (die, cu));
17625 return 0;
17626 }
17627
17628 return 1;
17629}
17630
a02abb62
JB
17631/* Read the given DW_AT_subrange DIE. */
17632
f792889a 17633static struct type *
a02abb62
JB
17634read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
17635{
4c9ad8c2 17636 struct type *base_type, *orig_base_type;
a02abb62
JB
17637 struct type *range_type;
17638 struct attribute *attr;
729efb13 17639 struct dynamic_prop low, high;
4fae6e18 17640 int low_default_is_valid;
c451ebe5 17641 int high_bound_is_count = 0;
15d034d0 17642 const char *name;
43bbcdc2 17643 LONGEST negative_mask;
e77813c8 17644
4c9ad8c2
TT
17645 orig_base_type = die_type (die, cu);
17646 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
17647 whereas the real type might be. So, we use ORIG_BASE_TYPE when
17648 creating the range type, but we use the result of check_typedef
17649 when examining properties of the type. */
17650 base_type = check_typedef (orig_base_type);
a02abb62 17651
7e314c57
JK
17652 /* The die_type call above may have already set the type for this DIE. */
17653 range_type = get_die_type (die, cu);
17654 if (range_type)
17655 return range_type;
17656
729efb13
SA
17657 low.kind = PROP_CONST;
17658 high.kind = PROP_CONST;
17659 high.data.const_val = 0;
17660
4fae6e18
JK
17661 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
17662 omitting DW_AT_lower_bound. */
17663 switch (cu->language)
6e70227d 17664 {
4fae6e18
JK
17665 case language_c:
17666 case language_cplus:
729efb13 17667 low.data.const_val = 0;
4fae6e18
JK
17668 low_default_is_valid = 1;
17669 break;
17670 case language_fortran:
729efb13 17671 low.data.const_val = 1;
4fae6e18
JK
17672 low_default_is_valid = 1;
17673 break;
17674 case language_d:
4fae6e18 17675 case language_objc:
c44af4eb 17676 case language_rust:
729efb13 17677 low.data.const_val = 0;
4fae6e18
JK
17678 low_default_is_valid = (cu->header.version >= 4);
17679 break;
17680 case language_ada:
17681 case language_m2:
17682 case language_pascal:
729efb13 17683 low.data.const_val = 1;
4fae6e18
JK
17684 low_default_is_valid = (cu->header.version >= 4);
17685 break;
17686 default:
729efb13 17687 low.data.const_val = 0;
4fae6e18
JK
17688 low_default_is_valid = 0;
17689 break;
a02abb62
JB
17690 }
17691
e142c38c 17692 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
a02abb62 17693 if (attr)
11c1ba78 17694 attr_to_dynamic_prop (attr, die, cu, &low);
4fae6e18
JK
17695 else if (!low_default_is_valid)
17696 complaint (&symfile_complaints, _("Missing DW_AT_lower_bound "
9d8780f0
SM
17697 "- DIE at %s [in module %s]"),
17698 sect_offset_str (die->sect_off),
518817b3 17699 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
a02abb62 17700
e142c38c 17701 attr = dwarf2_attr (die, DW_AT_upper_bound, cu);
80180f79 17702 if (!attr_to_dynamic_prop (attr, die, cu, &high))
e77813c8
PM
17703 {
17704 attr = dwarf2_attr (die, DW_AT_count, cu);
c451ebe5 17705 if (attr_to_dynamic_prop (attr, die, cu, &high))
6b662e19 17706 {
c451ebe5
SA
17707 /* If bounds are constant do the final calculation here. */
17708 if (low.kind == PROP_CONST && high.kind == PROP_CONST)
17709 high.data.const_val = low.data.const_val + high.data.const_val - 1;
17710 else
17711 high_bound_is_count = 1;
c2ff108b 17712 }
e77813c8
PM
17713 }
17714
17715 /* Dwarf-2 specifications explicitly allows to create subrange types
17716 without specifying a base type.
17717 In that case, the base type must be set to the type of
17718 the lower bound, upper bound or count, in that order, if any of these
17719 three attributes references an object that has a type.
17720 If no base type is found, the Dwarf-2 specifications say that
17721 a signed integer type of size equal to the size of an address should
17722 be used.
17723 For the following C code: `extern char gdb_int [];'
17724 GCC produces an empty range DIE.
17725 FIXME: muller/2010-05-28: Possible references to object for low bound,
0963b4bd 17726 high bound or count are not yet handled by this code. */
e77813c8
PM
17727 if (TYPE_CODE (base_type) == TYPE_CODE_VOID)
17728 {
518817b3 17729 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
e77813c8
PM
17730 struct gdbarch *gdbarch = get_objfile_arch (objfile);
17731 int addr_size = gdbarch_addr_bit (gdbarch) /8;
17732 struct type *int_type = objfile_type (objfile)->builtin_int;
17733
17734 /* Test "int", "long int", and "long long int" objfile types,
17735 and select the first one having a size above or equal to the
17736 architecture address size. */
17737 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
17738 base_type = int_type;
17739 else
17740 {
17741 int_type = objfile_type (objfile)->builtin_long;
17742 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
17743 base_type = int_type;
17744 else
17745 {
17746 int_type = objfile_type (objfile)->builtin_long_long;
17747 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
17748 base_type = int_type;
17749 }
17750 }
17751 }
a02abb62 17752
dbb9c2b1
JB
17753 /* Normally, the DWARF producers are expected to use a signed
17754 constant form (Eg. DW_FORM_sdata) to express negative bounds.
17755 But this is unfortunately not always the case, as witnessed
17756 with GCC, for instance, where the ambiguous DW_FORM_dataN form
17757 is used instead. To work around that ambiguity, we treat
17758 the bounds as signed, and thus sign-extend their values, when
17759 the base type is signed. */
6e70227d 17760 negative_mask =
66c6502d 17761 -((LONGEST) 1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1));
729efb13
SA
17762 if (low.kind == PROP_CONST
17763 && !TYPE_UNSIGNED (base_type) && (low.data.const_val & negative_mask))
17764 low.data.const_val |= negative_mask;
17765 if (high.kind == PROP_CONST
17766 && !TYPE_UNSIGNED (base_type) && (high.data.const_val & negative_mask))
17767 high.data.const_val |= negative_mask;
43bbcdc2 17768
729efb13 17769 range_type = create_range_type (NULL, orig_base_type, &low, &high);
a02abb62 17770
c451ebe5
SA
17771 if (high_bound_is_count)
17772 TYPE_RANGE_DATA (range_type)->flag_upper_bound_is_count = 1;
17773
c2ff108b
JK
17774 /* Ada expects an empty array on no boundary attributes. */
17775 if (attr == NULL && cu->language != language_ada)
729efb13 17776 TYPE_HIGH_BOUND_KIND (range_type) = PROP_UNDEFINED;
c2ff108b 17777
39cbfefa
DJ
17778 name = dwarf2_name (die, cu);
17779 if (name)
17780 TYPE_NAME (range_type) = name;
6e70227d 17781
e142c38c 17782 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
a02abb62
JB
17783 if (attr)
17784 TYPE_LENGTH (range_type) = DW_UNSND (attr);
17785
7e314c57
JK
17786 set_die_type (die, range_type, cu);
17787
17788 /* set_die_type should be already done. */
b4ba55a1
JB
17789 set_descriptive_type (range_type, die, cu);
17790
7e314c57 17791 return range_type;
a02abb62 17792}
6e70227d 17793
f792889a 17794static struct type *
81a17f79
JB
17795read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
17796{
17797 struct type *type;
81a17f79 17798
518817b3
SM
17799 type = init_type (cu->per_cu->dwarf2_per_objfile->objfile, TYPE_CODE_VOID,0,
17800 NULL);
0114d602 17801 TYPE_NAME (type) = dwarf2_name (die, cu);
81a17f79 17802
74a2f8ff
JB
17803 /* In Ada, an unspecified type is typically used when the description
17804 of the type is defered to a different unit. When encountering
17805 such a type, we treat it as a stub, and try to resolve it later on,
17806 when needed. */
17807 if (cu->language == language_ada)
17808 TYPE_STUB (type) = 1;
17809
f792889a 17810 return set_die_type (die, type, cu);
81a17f79 17811}
a02abb62 17812
639d11d3
DC
17813/* Read a single die and all its descendents. Set the die's sibling
17814 field to NULL; set other fields in the die correctly, and set all
17815 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
17816 location of the info_ptr after reading all of those dies. PARENT
17817 is the parent of the die in question. */
17818
17819static struct die_info *
dee91e82 17820read_die_and_children (const struct die_reader_specs *reader,
d521ce57
TT
17821 const gdb_byte *info_ptr,
17822 const gdb_byte **new_info_ptr,
dee91e82 17823 struct die_info *parent)
639d11d3
DC
17824{
17825 struct die_info *die;
d521ce57 17826 const gdb_byte *cur_ptr;
639d11d3
DC
17827 int has_children;
17828
bf6af496 17829 cur_ptr = read_full_die_1 (reader, &die, info_ptr, &has_children, 0);
1d325ec1
DJ
17830 if (die == NULL)
17831 {
17832 *new_info_ptr = cur_ptr;
17833 return NULL;
17834 }
93311388 17835 store_in_ref_table (die, reader->cu);
639d11d3
DC
17836
17837 if (has_children)
bf6af496 17838 die->child = read_die_and_siblings_1 (reader, cur_ptr, new_info_ptr, die);
639d11d3
DC
17839 else
17840 {
17841 die->child = NULL;
17842 *new_info_ptr = cur_ptr;
17843 }
17844
17845 die->sibling = NULL;
17846 die->parent = parent;
17847 return die;
17848}
17849
17850/* Read a die, all of its descendents, and all of its siblings; set
17851 all of the fields of all of the dies correctly. Arguments are as
17852 in read_die_and_children. */
17853
17854static struct die_info *
bf6af496 17855read_die_and_siblings_1 (const struct die_reader_specs *reader,
d521ce57
TT
17856 const gdb_byte *info_ptr,
17857 const gdb_byte **new_info_ptr,
bf6af496 17858 struct die_info *parent)
639d11d3
DC
17859{
17860 struct die_info *first_die, *last_sibling;
d521ce57 17861 const gdb_byte *cur_ptr;
639d11d3 17862
c906108c 17863 cur_ptr = info_ptr;
639d11d3
DC
17864 first_die = last_sibling = NULL;
17865
17866 while (1)
c906108c 17867 {
639d11d3 17868 struct die_info *die
dee91e82 17869 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
639d11d3 17870
1d325ec1 17871 if (die == NULL)
c906108c 17872 {
639d11d3
DC
17873 *new_info_ptr = cur_ptr;
17874 return first_die;
c906108c 17875 }
1d325ec1
DJ
17876
17877 if (!first_die)
17878 first_die = die;
c906108c 17879 else
1d325ec1
DJ
17880 last_sibling->sibling = die;
17881
17882 last_sibling = die;
c906108c 17883 }
c906108c
SS
17884}
17885
bf6af496
DE
17886/* Read a die, all of its descendents, and all of its siblings; set
17887 all of the fields of all of the dies correctly. Arguments are as
17888 in read_die_and_children.
17889 This the main entry point for reading a DIE and all its children. */
17890
17891static struct die_info *
17892read_die_and_siblings (const struct die_reader_specs *reader,
d521ce57
TT
17893 const gdb_byte *info_ptr,
17894 const gdb_byte **new_info_ptr,
bf6af496
DE
17895 struct die_info *parent)
17896{
17897 struct die_info *die = read_die_and_siblings_1 (reader, info_ptr,
17898 new_info_ptr, parent);
17899
b4f54984 17900 if (dwarf_die_debug)
bf6af496
DE
17901 {
17902 fprintf_unfiltered (gdb_stdlog,
17903 "Read die from %s@0x%x of %s:\n",
a32a8923 17904 get_section_name (reader->die_section),
bf6af496
DE
17905 (unsigned) (info_ptr - reader->die_section->buffer),
17906 bfd_get_filename (reader->abfd));
b4f54984 17907 dump_die (die, dwarf_die_debug);
bf6af496
DE
17908 }
17909
17910 return die;
17911}
17912
3019eac3
DE
17913/* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
17914 attributes.
17915 The caller is responsible for filling in the extra attributes
17916 and updating (*DIEP)->num_attrs.
17917 Set DIEP to point to a newly allocated die with its information,
17918 except for its child, sibling, and parent fields.
17919 Set HAS_CHILDREN to tell whether the die has children or not. */
93311388 17920
d521ce57 17921static const gdb_byte *
3019eac3 17922read_full_die_1 (const struct die_reader_specs *reader,
d521ce57 17923 struct die_info **diep, const gdb_byte *info_ptr,
3019eac3 17924 int *has_children, int num_extra_attrs)
93311388 17925{
b64f50a1 17926 unsigned int abbrev_number, bytes_read, i;
93311388
DE
17927 struct abbrev_info *abbrev;
17928 struct die_info *die;
17929 struct dwarf2_cu *cu = reader->cu;
17930 bfd *abfd = reader->abfd;
17931
9c541725 17932 sect_offset sect_off = (sect_offset) (info_ptr - reader->buffer);
93311388
DE
17933 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
17934 info_ptr += bytes_read;
17935 if (!abbrev_number)
17936 {
17937 *diep = NULL;
17938 *has_children = 0;
17939 return info_ptr;
17940 }
17941
685af9cd 17942 abbrev = reader->abbrev_table->lookup_abbrev (abbrev_number);
93311388 17943 if (!abbrev)
348e048f
DE
17944 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
17945 abbrev_number,
17946 bfd_get_filename (abfd));
17947
3019eac3 17948 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
9c541725 17949 die->sect_off = sect_off;
93311388
DE
17950 die->tag = abbrev->tag;
17951 die->abbrev = abbrev_number;
17952
3019eac3
DE
17953 /* Make the result usable.
17954 The caller needs to update num_attrs after adding the extra
17955 attributes. */
93311388
DE
17956 die->num_attrs = abbrev->num_attrs;
17957
17958 for (i = 0; i < abbrev->num_attrs; ++i)
dee91e82
DE
17959 info_ptr = read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
17960 info_ptr);
93311388
DE
17961
17962 *diep = die;
17963 *has_children = abbrev->has_children;
17964 return info_ptr;
17965}
17966
3019eac3
DE
17967/* Read a die and all its attributes.
17968 Set DIEP to point to a newly allocated die with its information,
17969 except for its child, sibling, and parent fields.
17970 Set HAS_CHILDREN to tell whether the die has children or not. */
17971
d521ce57 17972static const gdb_byte *
3019eac3 17973read_full_die (const struct die_reader_specs *reader,
d521ce57 17974 struct die_info **diep, const gdb_byte *info_ptr,
3019eac3
DE
17975 int *has_children)
17976{
d521ce57 17977 const gdb_byte *result;
bf6af496
DE
17978
17979 result = read_full_die_1 (reader, diep, info_ptr, has_children, 0);
17980
b4f54984 17981 if (dwarf_die_debug)
bf6af496
DE
17982 {
17983 fprintf_unfiltered (gdb_stdlog,
17984 "Read die from %s@0x%x of %s:\n",
a32a8923 17985 get_section_name (reader->die_section),
bf6af496
DE
17986 (unsigned) (info_ptr - reader->die_section->buffer),
17987 bfd_get_filename (reader->abfd));
b4f54984 17988 dump_die (*diep, dwarf_die_debug);
bf6af496
DE
17989 }
17990
17991 return result;
3019eac3 17992}
433df2d4
DE
17993\f
17994/* Abbreviation tables.
3019eac3 17995
433df2d4 17996 In DWARF version 2, the description of the debugging information is
c906108c
SS
17997 stored in a separate .debug_abbrev section. Before we read any
17998 dies from a section we read in all abbreviations and install them
433df2d4
DE
17999 in a hash table. */
18000
18001/* Allocate space for a struct abbrev_info object in ABBREV_TABLE. */
18002
685af9cd
TT
18003struct abbrev_info *
18004abbrev_table::alloc_abbrev ()
433df2d4
DE
18005{
18006 struct abbrev_info *abbrev;
18007
685af9cd 18008 abbrev = XOBNEW (&abbrev_obstack, struct abbrev_info);
433df2d4 18009 memset (abbrev, 0, sizeof (struct abbrev_info));
8d749320 18010
433df2d4
DE
18011 return abbrev;
18012}
18013
18014/* Add an abbreviation to the table. */
c906108c 18015
685af9cd
TT
18016void
18017abbrev_table::add_abbrev (unsigned int abbrev_number,
18018 struct abbrev_info *abbrev)
433df2d4
DE
18019{
18020 unsigned int hash_number;
18021
18022 hash_number = abbrev_number % ABBREV_HASH_SIZE;
4a17f768
YQ
18023 abbrev->next = m_abbrevs[hash_number];
18024 m_abbrevs[hash_number] = abbrev;
433df2d4 18025}
dee91e82 18026
433df2d4
DE
18027/* Look up an abbrev in the table.
18028 Returns NULL if the abbrev is not found. */
18029
685af9cd
TT
18030struct abbrev_info *
18031abbrev_table::lookup_abbrev (unsigned int abbrev_number)
c906108c 18032{
433df2d4
DE
18033 unsigned int hash_number;
18034 struct abbrev_info *abbrev;
18035
18036 hash_number = abbrev_number % ABBREV_HASH_SIZE;
4a17f768 18037 abbrev = m_abbrevs[hash_number];
433df2d4
DE
18038
18039 while (abbrev)
18040 {
18041 if (abbrev->number == abbrev_number)
18042 return abbrev;
18043 abbrev = abbrev->next;
18044 }
18045 return NULL;
18046}
18047
18048/* Read in an abbrev table. */
18049
685af9cd 18050static abbrev_table_up
ed2dc618
SM
18051abbrev_table_read_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
18052 struct dwarf2_section_info *section,
9c541725 18053 sect_offset sect_off)
433df2d4
DE
18054{
18055 struct objfile *objfile = dwarf2_per_objfile->objfile;
a32a8923 18056 bfd *abfd = get_section_bfd_owner (section);
d521ce57 18057 const gdb_byte *abbrev_ptr;
c906108c
SS
18058 struct abbrev_info *cur_abbrev;
18059 unsigned int abbrev_number, bytes_read, abbrev_name;
433df2d4 18060 unsigned int abbrev_form;
f3dd6933
DJ
18061 struct attr_abbrev *cur_attrs;
18062 unsigned int allocated_attrs;
c906108c 18063
685af9cd 18064 abbrev_table_up abbrev_table (new struct abbrev_table (sect_off));
c906108c 18065
433df2d4 18066 dwarf2_read_section (objfile, section);
9c541725 18067 abbrev_ptr = section->buffer + to_underlying (sect_off);
c906108c
SS
18068 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18069 abbrev_ptr += bytes_read;
18070
f3dd6933 18071 allocated_attrs = ATTR_ALLOC_CHUNK;
8d749320 18072 cur_attrs = XNEWVEC (struct attr_abbrev, allocated_attrs);
6e70227d 18073
0963b4bd 18074 /* Loop until we reach an abbrev number of 0. */
c906108c
SS
18075 while (abbrev_number)
18076 {
685af9cd 18077 cur_abbrev = abbrev_table->alloc_abbrev ();
c906108c
SS
18078
18079 /* read in abbrev header */
18080 cur_abbrev->number = abbrev_number;
aead7601
SM
18081 cur_abbrev->tag
18082 = (enum dwarf_tag) read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
c906108c
SS
18083 abbrev_ptr += bytes_read;
18084 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
18085 abbrev_ptr += 1;
18086
18087 /* now read in declarations */
22d2f3ab 18088 for (;;)
c906108c 18089 {
43988095
JK
18090 LONGEST implicit_const;
18091
22d2f3ab
JK
18092 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18093 abbrev_ptr += bytes_read;
18094 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18095 abbrev_ptr += bytes_read;
43988095
JK
18096 if (abbrev_form == DW_FORM_implicit_const)
18097 {
18098 implicit_const = read_signed_leb128 (abfd, abbrev_ptr,
18099 &bytes_read);
18100 abbrev_ptr += bytes_read;
18101 }
18102 else
18103 {
18104 /* Initialize it due to a false compiler warning. */
18105 implicit_const = -1;
18106 }
22d2f3ab
JK
18107
18108 if (abbrev_name == 0)
18109 break;
18110
f3dd6933 18111 if (cur_abbrev->num_attrs == allocated_attrs)
c906108c 18112 {
f3dd6933
DJ
18113 allocated_attrs += ATTR_ALLOC_CHUNK;
18114 cur_attrs
224c3ddb 18115 = XRESIZEVEC (struct attr_abbrev, cur_attrs, allocated_attrs);
c906108c 18116 }
ae038cb0 18117
aead7601
SM
18118 cur_attrs[cur_abbrev->num_attrs].name
18119 = (enum dwarf_attribute) abbrev_name;
22d2f3ab 18120 cur_attrs[cur_abbrev->num_attrs].form
aead7601 18121 = (enum dwarf_form) abbrev_form;
43988095 18122 cur_attrs[cur_abbrev->num_attrs].implicit_const = implicit_const;
22d2f3ab 18123 ++cur_abbrev->num_attrs;
c906108c
SS
18124 }
18125
8d749320
SM
18126 cur_abbrev->attrs =
18127 XOBNEWVEC (&abbrev_table->abbrev_obstack, struct attr_abbrev,
18128 cur_abbrev->num_attrs);
f3dd6933
DJ
18129 memcpy (cur_abbrev->attrs, cur_attrs,
18130 cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
18131
685af9cd 18132 abbrev_table->add_abbrev (abbrev_number, cur_abbrev);
c906108c
SS
18133
18134 /* Get next abbreviation.
18135 Under Irix6 the abbreviations for a compilation unit are not
c5aa993b
JM
18136 always properly terminated with an abbrev number of 0.
18137 Exit loop if we encounter an abbreviation which we have
18138 already read (which means we are about to read the abbreviations
18139 for the next compile unit) or if the end of the abbreviation
18140 table is reached. */
433df2d4 18141 if ((unsigned int) (abbrev_ptr - section->buffer) >= section->size)
c906108c
SS
18142 break;
18143 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18144 abbrev_ptr += bytes_read;
685af9cd 18145 if (abbrev_table->lookup_abbrev (abbrev_number) != NULL)
c906108c
SS
18146 break;
18147 }
f3dd6933
DJ
18148
18149 xfree (cur_attrs);
433df2d4 18150 return abbrev_table;
c906108c
SS
18151}
18152
72bf9492
DJ
18153/* Returns nonzero if TAG represents a type that we might generate a partial
18154 symbol for. */
18155
18156static int
18157is_type_tag_for_partial (int tag)
18158{
18159 switch (tag)
18160 {
18161#if 0
18162 /* Some types that would be reasonable to generate partial symbols for,
18163 that we don't at present. */
18164 case DW_TAG_array_type:
18165 case DW_TAG_file_type:
18166 case DW_TAG_ptr_to_member_type:
18167 case DW_TAG_set_type:
18168 case DW_TAG_string_type:
18169 case DW_TAG_subroutine_type:
18170#endif
18171 case DW_TAG_base_type:
18172 case DW_TAG_class_type:
680b30c7 18173 case DW_TAG_interface_type:
72bf9492
DJ
18174 case DW_TAG_enumeration_type:
18175 case DW_TAG_structure_type:
18176 case DW_TAG_subrange_type:
18177 case DW_TAG_typedef:
18178 case DW_TAG_union_type:
18179 return 1;
18180 default:
18181 return 0;
18182 }
18183}
18184
18185/* Load all DIEs that are interesting for partial symbols into memory. */
18186
18187static struct partial_die_info *
dee91e82 18188load_partial_dies (const struct die_reader_specs *reader,
d521ce57 18189 const gdb_byte *info_ptr, int building_psymtab)
72bf9492 18190{
dee91e82 18191 struct dwarf2_cu *cu = reader->cu;
518817b3 18192 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
72bf9492 18193 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
72bf9492 18194 unsigned int bytes_read;
5afb4e99 18195 unsigned int load_all = 0;
72bf9492
DJ
18196 int nesting_level = 1;
18197
18198 parent_die = NULL;
18199 last_die = NULL;
18200
7adf1e79
DE
18201 gdb_assert (cu->per_cu != NULL);
18202 if (cu->per_cu->load_all_dies)
5afb4e99
DJ
18203 load_all = 1;
18204
72bf9492
DJ
18205 cu->partial_dies
18206 = htab_create_alloc_ex (cu->header.length / 12,
18207 partial_die_hash,
18208 partial_die_eq,
18209 NULL,
18210 &cu->comp_unit_obstack,
18211 hashtab_obstack_allocate,
18212 dummy_obstack_deallocate);
18213
72bf9492
DJ
18214 while (1)
18215 {
685af9cd 18216 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
72bf9492
DJ
18217
18218 /* A NULL abbrev means the end of a series of children. */
18219 if (abbrev == NULL)
18220 {
18221 if (--nesting_level == 0)
cd9983dd
YQ
18222 return first_die;
18223
72bf9492
DJ
18224 info_ptr += bytes_read;
18225 last_die = parent_die;
18226 parent_die = parent_die->die_parent;
18227 continue;
18228 }
18229
98bfdba5
PA
18230 /* Check for template arguments. We never save these; if
18231 they're seen, we just mark the parent, and go on our way. */
18232 if (parent_die != NULL
18233 && cu->language == language_cplus
18234 && (abbrev->tag == DW_TAG_template_type_param
18235 || abbrev->tag == DW_TAG_template_value_param))
18236 {
18237 parent_die->has_template_arguments = 1;
18238
18239 if (!load_all)
18240 {
18241 /* We don't need a partial DIE for the template argument. */
dee91e82 18242 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
98bfdba5
PA
18243 continue;
18244 }
18245 }
18246
0d99eb77 18247 /* We only recurse into c++ subprograms looking for template arguments.
98bfdba5
PA
18248 Skip their other children. */
18249 if (!load_all
18250 && cu->language == language_cplus
18251 && parent_die != NULL
18252 && parent_die->tag == DW_TAG_subprogram)
18253 {
dee91e82 18254 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
98bfdba5
PA
18255 continue;
18256 }
18257
5afb4e99
DJ
18258 /* Check whether this DIE is interesting enough to save. Normally
18259 we would not be interested in members here, but there may be
18260 later variables referencing them via DW_AT_specification (for
18261 static members). */
18262 if (!load_all
18263 && !is_type_tag_for_partial (abbrev->tag)
72929c62 18264 && abbrev->tag != DW_TAG_constant
72bf9492
DJ
18265 && abbrev->tag != DW_TAG_enumerator
18266 && abbrev->tag != DW_TAG_subprogram
b1dc1806 18267 && abbrev->tag != DW_TAG_inlined_subroutine
bc30ff58 18268 && abbrev->tag != DW_TAG_lexical_block
72bf9492 18269 && abbrev->tag != DW_TAG_variable
5afb4e99 18270 && abbrev->tag != DW_TAG_namespace
f55ee35c 18271 && abbrev->tag != DW_TAG_module
95554aad 18272 && abbrev->tag != DW_TAG_member
74921315
KS
18273 && abbrev->tag != DW_TAG_imported_unit
18274 && abbrev->tag != DW_TAG_imported_declaration)
72bf9492
DJ
18275 {
18276 /* Otherwise we skip to the next sibling, if any. */
dee91e82 18277 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
72bf9492
DJ
18278 continue;
18279 }
18280
6f06d47b
YQ
18281 struct partial_die_info pdi ((sect_offset) (info_ptr - reader->buffer),
18282 abbrev);
cd9983dd 18283
48fbe735 18284 info_ptr = pdi.read (reader, *abbrev, info_ptr + bytes_read);
72bf9492
DJ
18285
18286 /* This two-pass algorithm for processing partial symbols has a
18287 high cost in cache pressure. Thus, handle some simple cases
18288 here which cover the majority of C partial symbols. DIEs
18289 which neither have specification tags in them, nor could have
18290 specification tags elsewhere pointing at them, can simply be
18291 processed and discarded.
18292
18293 This segment is also optional; scan_partial_symbols and
18294 add_partial_symbol will handle these DIEs if we chain
18295 them in normally. When compilers which do not emit large
18296 quantities of duplicate debug information are more common,
18297 this code can probably be removed. */
18298
18299 /* Any complete simple types at the top level (pretty much all
18300 of them, for a language without namespaces), can be processed
18301 directly. */
18302 if (parent_die == NULL
cd9983dd
YQ
18303 && pdi.has_specification == 0
18304 && pdi.is_declaration == 0
18305 && ((pdi.tag == DW_TAG_typedef && !pdi.has_children)
18306 || pdi.tag == DW_TAG_base_type
18307 || pdi.tag == DW_TAG_subrange_type))
72bf9492 18308 {
cd9983dd
YQ
18309 if (building_psymtab && pdi.name != NULL)
18310 add_psymbol_to_list (pdi.name, strlen (pdi.name), 0,
72bf9492 18311 VAR_DOMAIN, LOC_TYPEDEF,
bb5ed363 18312 &objfile->static_psymbols,
1762568f 18313 0, cu->language, objfile);
cd9983dd 18314 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
72bf9492
DJ
18315 continue;
18316 }
18317
d8228535
JK
18318 /* The exception for DW_TAG_typedef with has_children above is
18319 a workaround of GCC PR debug/47510. In the case of this complaint
18320 type_name_no_tag_or_error will error on such types later.
18321
18322 GDB skipped children of DW_TAG_typedef by the shortcut above and then
18323 it could not find the child DIEs referenced later, this is checked
18324 above. In correct DWARF DW_TAG_typedef should have no children. */
18325
cd9983dd 18326 if (pdi.tag == DW_TAG_typedef && pdi.has_children)
d8228535
JK
18327 complaint (&symfile_complaints,
18328 _("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
9d8780f0 18329 "- DIE at %s [in module %s]"),
cd9983dd 18330 sect_offset_str (pdi.sect_off), objfile_name (objfile));
d8228535 18331
72bf9492
DJ
18332 /* If we're at the second level, and we're an enumerator, and
18333 our parent has no specification (meaning possibly lives in a
18334 namespace elsewhere), then we can add the partial symbol now
18335 instead of queueing it. */
cd9983dd 18336 if (pdi.tag == DW_TAG_enumerator
72bf9492
DJ
18337 && parent_die != NULL
18338 && parent_die->die_parent == NULL
18339 && parent_die->tag == DW_TAG_enumeration_type
18340 && parent_die->has_specification == 0)
18341 {
cd9983dd 18342 if (pdi.name == NULL)
3e43a32a
MS
18343 complaint (&symfile_complaints,
18344 _("malformed enumerator DIE ignored"));
72bf9492 18345 else if (building_psymtab)
cd9983dd 18346 add_psymbol_to_list (pdi.name, strlen (pdi.name), 0,
72bf9492 18347 VAR_DOMAIN, LOC_CONST,
9c37b5ae 18348 cu->language == language_cplus
bb5ed363
DE
18349 ? &objfile->global_psymbols
18350 : &objfile->static_psymbols,
1762568f 18351 0, cu->language, objfile);
72bf9492 18352
cd9983dd 18353 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
72bf9492
DJ
18354 continue;
18355 }
18356
cd9983dd 18357 struct partial_die_info *part_die
6f06d47b 18358 = new (&cu->comp_unit_obstack) partial_die_info (pdi);
cd9983dd 18359
72bf9492
DJ
18360 /* We'll save this DIE so link it in. */
18361 part_die->die_parent = parent_die;
18362 part_die->die_sibling = NULL;
18363 part_die->die_child = NULL;
18364
18365 if (last_die && last_die == parent_die)
18366 last_die->die_child = part_die;
18367 else if (last_die)
18368 last_die->die_sibling = part_die;
18369
18370 last_die = part_die;
18371
18372 if (first_die == NULL)
18373 first_die = part_die;
18374
18375 /* Maybe add the DIE to the hash table. Not all DIEs that we
18376 find interesting need to be in the hash table, because we
18377 also have the parent/sibling/child chains; only those that we
18378 might refer to by offset later during partial symbol reading.
18379
18380 For now this means things that might have be the target of a
18381 DW_AT_specification, DW_AT_abstract_origin, or
18382 DW_AT_extension. DW_AT_extension will refer only to
18383 namespaces; DW_AT_abstract_origin refers to functions (and
18384 many things under the function DIE, but we do not recurse
18385 into function DIEs during partial symbol reading) and
18386 possibly variables as well; DW_AT_specification refers to
18387 declarations. Declarations ought to have the DW_AT_declaration
18388 flag. It happens that GCC forgets to put it in sometimes, but
18389 only for functions, not for types.
18390
18391 Adding more things than necessary to the hash table is harmless
18392 except for the performance cost. Adding too few will result in
5afb4e99
DJ
18393 wasted time in find_partial_die, when we reread the compilation
18394 unit with load_all_dies set. */
72bf9492 18395
5afb4e99 18396 if (load_all
72929c62 18397 || abbrev->tag == DW_TAG_constant
5afb4e99 18398 || abbrev->tag == DW_TAG_subprogram
72bf9492
DJ
18399 || abbrev->tag == DW_TAG_variable
18400 || abbrev->tag == DW_TAG_namespace
18401 || part_die->is_declaration)
18402 {
18403 void **slot;
18404
18405 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
9c541725
PA
18406 to_underlying (part_die->sect_off),
18407 INSERT);
72bf9492
DJ
18408 *slot = part_die;
18409 }
18410
72bf9492 18411 /* For some DIEs we want to follow their children (if any). For C
bc30ff58 18412 we have no reason to follow the children of structures; for other
98bfdba5
PA
18413 languages we have to, so that we can get at method physnames
18414 to infer fully qualified class names, for DW_AT_specification,
18415 and for C++ template arguments. For C++, we also look one level
18416 inside functions to find template arguments (if the name of the
18417 function does not already contain the template arguments).
bc30ff58
JB
18418
18419 For Ada, we need to scan the children of subprograms and lexical
18420 blocks as well because Ada allows the definition of nested
18421 entities that could be interesting for the debugger, such as
18422 nested subprograms for instance. */
72bf9492 18423 if (last_die->has_children
5afb4e99
DJ
18424 && (load_all
18425 || last_die->tag == DW_TAG_namespace
f55ee35c 18426 || last_die->tag == DW_TAG_module
72bf9492 18427 || last_die->tag == DW_TAG_enumeration_type
98bfdba5
PA
18428 || (cu->language == language_cplus
18429 && last_die->tag == DW_TAG_subprogram
18430 && (last_die->name == NULL
18431 || strchr (last_die->name, '<') == NULL))
72bf9492
DJ
18432 || (cu->language != language_c
18433 && (last_die->tag == DW_TAG_class_type
680b30c7 18434 || last_die->tag == DW_TAG_interface_type
72bf9492 18435 || last_die->tag == DW_TAG_structure_type
bc30ff58
JB
18436 || last_die->tag == DW_TAG_union_type))
18437 || (cu->language == language_ada
18438 && (last_die->tag == DW_TAG_subprogram
18439 || last_die->tag == DW_TAG_lexical_block))))
72bf9492
DJ
18440 {
18441 nesting_level++;
18442 parent_die = last_die;
18443 continue;
18444 }
18445
18446 /* Otherwise we skip to the next sibling, if any. */
dee91e82 18447 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
72bf9492
DJ
18448
18449 /* Back to the top, do it again. */
18450 }
18451}
18452
6f06d47b
YQ
18453partial_die_info::partial_die_info (sect_offset sect_off_,
18454 struct abbrev_info *abbrev)
18455 : partial_die_info (sect_off_, abbrev->tag, abbrev->has_children)
18456{
18457}
18458
35cc7ed7
YQ
18459/* Read a minimal amount of information into the minimal die structure.
18460 INFO_PTR should point just after the initial uleb128 of a DIE. */
c906108c 18461
48fbe735
YQ
18462const gdb_byte *
18463partial_die_info::read (const struct die_reader_specs *reader,
18464 const struct abbrev_info &abbrev, const gdb_byte *info_ptr)
c906108c 18465{
dee91e82 18466 struct dwarf2_cu *cu = reader->cu;
518817b3
SM
18467 struct dwarf2_per_objfile *dwarf2_per_objfile
18468 = cu->per_cu->dwarf2_per_objfile;
fa238c03 18469 unsigned int i;
c5aa993b 18470 int has_low_pc_attr = 0;
c906108c 18471 int has_high_pc_attr = 0;
91da1414 18472 int high_pc_relative = 0;
c906108c 18473
fd0a254f 18474 for (i = 0; i < abbrev.num_attrs; ++i)
c906108c 18475 {
48fbe735
YQ
18476 struct attribute attr;
18477
fd0a254f 18478 info_ptr = read_attribute (reader, &attr, &abbrev.attrs[i], info_ptr);
c906108c
SS
18479
18480 /* Store the data if it is of an attribute we want to keep in a
c5aa993b 18481 partial symbol table. */
c906108c
SS
18482 switch (attr.name)
18483 {
18484 case DW_AT_name:
48fbe735 18485 switch (tag)
71c25dea
TT
18486 {
18487 case DW_TAG_compile_unit:
95554aad 18488 case DW_TAG_partial_unit:
348e048f 18489 case DW_TAG_type_unit:
71c25dea
TT
18490 /* Compilation units have a DW_AT_name that is a filename, not
18491 a source language identifier. */
18492 case DW_TAG_enumeration_type:
18493 case DW_TAG_enumerator:
18494 /* These tags always have simple identifiers already; no need
18495 to canonicalize them. */
48fbe735 18496 name = DW_STRING (&attr);
71c25dea
TT
18497 break;
18498 default:
48fbe735
YQ
18499 {
18500 struct objfile *objfile = dwarf2_per_objfile->objfile;
18501
18502 name
18503 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
18504 &objfile->per_bfd->storage_obstack);
18505 }
71c25dea
TT
18506 break;
18507 }
c906108c 18508 break;
31ef98ae 18509 case DW_AT_linkage_name:
c906108c 18510 case DW_AT_MIPS_linkage_name:
31ef98ae
TT
18511 /* Note that both forms of linkage name might appear. We
18512 assume they will be the same, and we only store the last
18513 one we see. */
94af9270 18514 if (cu->language == language_ada)
48fbe735
YQ
18515 name = DW_STRING (&attr);
18516 linkage_name = DW_STRING (&attr);
c906108c
SS
18517 break;
18518 case DW_AT_low_pc:
18519 has_low_pc_attr = 1;
48fbe735 18520 lowpc = attr_value_as_address (&attr);
c906108c
SS
18521 break;
18522 case DW_AT_high_pc:
18523 has_high_pc_attr = 1;
48fbe735 18524 highpc = attr_value_as_address (&attr);
31aa7e4e
JB
18525 if (cu->header.version >= 4 && attr_form_is_constant (&attr))
18526 high_pc_relative = 1;
c906108c
SS
18527 break;
18528 case DW_AT_location:
0963b4bd 18529 /* Support the .debug_loc offsets. */
8e19ed76
PS
18530 if (attr_form_is_block (&attr))
18531 {
48fbe735 18532 d.locdesc = DW_BLOCK (&attr);
8e19ed76 18533 }
3690dd37 18534 else if (attr_form_is_section_offset (&attr))
8e19ed76 18535 {
4d3c2250 18536 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
18537 }
18538 else
18539 {
4d3c2250
KB
18540 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
18541 "partial symbol information");
8e19ed76 18542 }
c906108c 18543 break;
c906108c 18544 case DW_AT_external:
48fbe735 18545 is_external = DW_UNSND (&attr);
c906108c
SS
18546 break;
18547 case DW_AT_declaration:
48fbe735 18548 is_declaration = DW_UNSND (&attr);
c906108c
SS
18549 break;
18550 case DW_AT_type:
48fbe735 18551 has_type = 1;
c906108c
SS
18552 break;
18553 case DW_AT_abstract_origin:
18554 case DW_AT_specification:
72bf9492 18555 case DW_AT_extension:
48fbe735
YQ
18556 has_specification = 1;
18557 spec_offset = dwarf2_get_ref_die_offset (&attr);
18558 spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
36586728 18559 || cu->per_cu->is_dwz);
c906108c
SS
18560 break;
18561 case DW_AT_sibling:
18562 /* Ignore absolute siblings, they might point outside of
18563 the current compile unit. */
18564 if (attr.form == DW_FORM_ref_addr)
3e43a32a
MS
18565 complaint (&symfile_complaints,
18566 _("ignoring absolute DW_AT_sibling"));
c906108c 18567 else
b9502d3f 18568 {
48fbe735 18569 const gdb_byte *buffer = reader->buffer;
9c541725
PA
18570 sect_offset off = dwarf2_get_ref_die_offset (&attr);
18571 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
b9502d3f
WN
18572
18573 if (sibling_ptr < info_ptr)
18574 complaint (&symfile_complaints,
18575 _("DW_AT_sibling points backwards"));
22869d73
KS
18576 else if (sibling_ptr > reader->buffer_end)
18577 dwarf2_section_buffer_overflow_complaint (reader->die_section);
b9502d3f 18578 else
48fbe735 18579 sibling = sibling_ptr;
b9502d3f 18580 }
c906108c 18581 break;
fa4028e9 18582 case DW_AT_byte_size:
48fbe735 18583 has_byte_size = 1;
fa4028e9 18584 break;
ff908ebf 18585 case DW_AT_const_value:
48fbe735 18586 has_const_value = 1;
ff908ebf 18587 break;
68511cec
CES
18588 case DW_AT_calling_convention:
18589 /* DWARF doesn't provide a way to identify a program's source-level
18590 entry point. DW_AT_calling_convention attributes are only meant
18591 to describe functions' calling conventions.
18592
18593 However, because it's a necessary piece of information in
0c1b455e
TT
18594 Fortran, and before DWARF 4 DW_CC_program was the only
18595 piece of debugging information whose definition refers to
18596 a 'main program' at all, several compilers marked Fortran
18597 main programs with DW_CC_program --- even when those
18598 functions use the standard calling conventions.
18599
18600 Although DWARF now specifies a way to provide this
18601 information, we support this practice for backward
18602 compatibility. */
68511cec 18603 if (DW_UNSND (&attr) == DW_CC_program
0c1b455e 18604 && cu->language == language_fortran)
48fbe735 18605 main_subprogram = 1;
68511cec 18606 break;
481860b3
GB
18607 case DW_AT_inline:
18608 if (DW_UNSND (&attr) == DW_INL_inlined
18609 || DW_UNSND (&attr) == DW_INL_declared_inlined)
48fbe735 18610 may_be_inlined = 1;
481860b3 18611 break;
95554aad
TT
18612
18613 case DW_AT_import:
48fbe735 18614 if (tag == DW_TAG_imported_unit)
36586728 18615 {
48fbe735
YQ
18616 d.sect_off = dwarf2_get_ref_die_offset (&attr);
18617 is_dwz = (attr.form == DW_FORM_GNU_ref_alt
36586728
TT
18618 || cu->per_cu->is_dwz);
18619 }
95554aad
TT
18620 break;
18621
0c1b455e 18622 case DW_AT_main_subprogram:
48fbe735 18623 main_subprogram = DW_UNSND (&attr);
0c1b455e
TT
18624 break;
18625
c906108c
SS
18626 default:
18627 break;
18628 }
18629 }
18630
91da1414 18631 if (high_pc_relative)
48fbe735 18632 highpc += lowpc;
91da1414 18633
9373cf26
JK
18634 if (has_low_pc_attr && has_high_pc_attr)
18635 {
18636 /* When using the GNU linker, .gnu.linkonce. sections are used to
18637 eliminate duplicate copies of functions and vtables and such.
18638 The linker will arbitrarily choose one and discard the others.
18639 The AT_*_pc values for such functions refer to local labels in
18640 these sections. If the section from that file was discarded, the
18641 labels are not in the output, so the relocs get a value of 0.
18642 If this is a discarded function, mark the pc bounds as invalid,
18643 so that GDB will ignore it. */
48fbe735 18644 if (lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
9373cf26 18645 {
48fbe735 18646 struct objfile *objfile = dwarf2_per_objfile->objfile;
bb5ed363 18647 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9373cf26
JK
18648
18649 complaint (&symfile_complaints,
18650 _("DW_AT_low_pc %s is zero "
9d8780f0 18651 "for DIE at %s [in module %s]"),
48fbe735
YQ
18652 paddress (gdbarch, lowpc),
18653 sect_offset_str (sect_off),
9d8780f0 18654 objfile_name (objfile));
9373cf26
JK
18655 }
18656 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
48fbe735 18657 else if (lowpc >= highpc)
9373cf26 18658 {
48fbe735 18659 struct objfile *objfile = dwarf2_per_objfile->objfile;
bb5ed363 18660 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9373cf26
JK
18661
18662 complaint (&symfile_complaints,
18663 _("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
9d8780f0 18664 "for DIE at %s [in module %s]"),
48fbe735
YQ
18665 paddress (gdbarch, lowpc),
18666 paddress (gdbarch, highpc),
18667 sect_offset_str (sect_off),
9c541725 18668 objfile_name (objfile));
9373cf26
JK
18669 }
18670 else
48fbe735 18671 has_pc_info = 1;
9373cf26 18672 }
85cbf3d3 18673
c906108c
SS
18674 return info_ptr;
18675}
18676
72bf9492
DJ
18677/* Find a cached partial DIE at OFFSET in CU. */
18678
d590ff25
YQ
18679struct partial_die_info *
18680dwarf2_cu::find_partial_die (sect_offset sect_off)
72bf9492
DJ
18681{
18682 struct partial_die_info *lookup_die = NULL;
6f06d47b 18683 struct partial_die_info part_die (sect_off);
72bf9492 18684
9a3c8263 18685 lookup_die = ((struct partial_die_info *)
d590ff25 18686 htab_find_with_hash (partial_dies, &part_die,
9c541725 18687 to_underlying (sect_off)));
72bf9492 18688
72bf9492
DJ
18689 return lookup_die;
18690}
18691
348e048f
DE
18692/* Find a partial DIE at OFFSET, which may or may not be in CU,
18693 except in the case of .debug_types DIEs which do not reference
18694 outside their CU (they do however referencing other types via
55f1336d 18695 DW_FORM_ref_sig8). */
72bf9492
DJ
18696
18697static struct partial_die_info *
9c541725 18698find_partial_die (sect_offset sect_off, int offset_in_dwz, struct dwarf2_cu *cu)
72bf9492 18699{
518817b3
SM
18700 struct dwarf2_per_objfile *dwarf2_per_objfile
18701 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 18702 struct objfile *objfile = dwarf2_per_objfile->objfile;
5afb4e99
DJ
18703 struct dwarf2_per_cu_data *per_cu = NULL;
18704 struct partial_die_info *pd = NULL;
72bf9492 18705
36586728 18706 if (offset_in_dwz == cu->per_cu->is_dwz
9c541725 18707 && offset_in_cu_p (&cu->header, sect_off))
5afb4e99 18708 {
d590ff25 18709 pd = cu->find_partial_die (sect_off);
5afb4e99
DJ
18710 if (pd != NULL)
18711 return pd;
0d99eb77
DE
18712 /* We missed recording what we needed.
18713 Load all dies and try again. */
18714 per_cu = cu->per_cu;
5afb4e99 18715 }
0d99eb77
DE
18716 else
18717 {
18718 /* TUs don't reference other CUs/TUs (except via type signatures). */
3019eac3 18719 if (cu->per_cu->is_debug_types)
0d99eb77 18720 {
9d8780f0
SM
18721 error (_("Dwarf Error: Type Unit at offset %s contains"
18722 " external reference to offset %s [in module %s].\n"),
18723 sect_offset_str (cu->header.sect_off), sect_offset_str (sect_off),
0d99eb77
DE
18724 bfd_get_filename (objfile->obfd));
18725 }
9c541725 18726 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
ed2dc618 18727 dwarf2_per_objfile);
72bf9492 18728
0d99eb77
DE
18729 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
18730 load_partial_comp_unit (per_cu);
ae038cb0 18731
0d99eb77 18732 per_cu->cu->last_used = 0;
d590ff25 18733 pd = per_cu->cu->find_partial_die (sect_off);
0d99eb77 18734 }
5afb4e99 18735
dee91e82
DE
18736 /* If we didn't find it, and not all dies have been loaded,
18737 load them all and try again. */
18738
5afb4e99
DJ
18739 if (pd == NULL && per_cu->load_all_dies == 0)
18740 {
5afb4e99 18741 per_cu->load_all_dies = 1;
fd820528
DE
18742
18743 /* This is nasty. When we reread the DIEs, somewhere up the call chain
18744 THIS_CU->cu may already be in use. So we can't just free it and
18745 replace its DIEs with the ones we read in. Instead, we leave those
18746 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
18747 and clobber THIS_CU->cu->partial_dies with the hash table for the new
18748 set. */
dee91e82 18749 load_partial_comp_unit (per_cu);
5afb4e99 18750
d590ff25 18751 pd = per_cu->cu->find_partial_die (sect_off);
5afb4e99
DJ
18752 }
18753
18754 if (pd == NULL)
18755 internal_error (__FILE__, __LINE__,
9d8780f0 18756 _("could not find partial DIE %s "
3e43a32a 18757 "in cache [from module %s]\n"),
9d8780f0 18758 sect_offset_str (sect_off), bfd_get_filename (objfile->obfd));
5afb4e99 18759 return pd;
72bf9492
DJ
18760}
18761
abc72ce4
DE
18762/* See if we can figure out if the class lives in a namespace. We do
18763 this by looking for a member function; its demangled name will
18764 contain namespace info, if there is any. */
18765
18766static void
18767guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
18768 struct dwarf2_cu *cu)
18769{
18770 /* NOTE: carlton/2003-10-07: Getting the info this way changes
18771 what template types look like, because the demangler
18772 frequently doesn't give the same name as the debug info. We
18773 could fix this by only using the demangled name to get the
18774 prefix (but see comment in read_structure_type). */
18775
18776 struct partial_die_info *real_pdi;
18777 struct partial_die_info *child_pdi;
18778
18779 /* If this DIE (this DIE's specification, if any) has a parent, then
18780 we should not do this. We'll prepend the parent's fully qualified
18781 name when we create the partial symbol. */
18782
18783 real_pdi = struct_pdi;
18784 while (real_pdi->has_specification)
36586728
TT
18785 real_pdi = find_partial_die (real_pdi->spec_offset,
18786 real_pdi->spec_is_dwz, cu);
abc72ce4
DE
18787
18788 if (real_pdi->die_parent != NULL)
18789 return;
18790
18791 for (child_pdi = struct_pdi->die_child;
18792 child_pdi != NULL;
18793 child_pdi = child_pdi->die_sibling)
18794 {
18795 if (child_pdi->tag == DW_TAG_subprogram
18796 && child_pdi->linkage_name != NULL)
18797 {
18798 char *actual_class_name
18799 = language_class_name_from_physname (cu->language_defn,
18800 child_pdi->linkage_name);
18801 if (actual_class_name != NULL)
18802 {
518817b3 18803 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
abc72ce4 18804 struct_pdi->name
224c3ddb 18805 = ((const char *)
e3b94546 18806 obstack_copy0 (&objfile->per_bfd->storage_obstack,
224c3ddb
SM
18807 actual_class_name,
18808 strlen (actual_class_name)));
abc72ce4
DE
18809 xfree (actual_class_name);
18810 }
18811 break;
18812 }
18813 }
18814}
18815
52356b79
YQ
18816void
18817partial_die_info::fixup (struct dwarf2_cu *cu)
72bf9492 18818{
abc72ce4
DE
18819 /* Once we've fixed up a die, there's no point in doing so again.
18820 This also avoids a memory leak if we were to call
18821 guess_partial_die_structure_name multiple times. */
52356b79 18822 if (fixup_called)
abc72ce4
DE
18823 return;
18824
72bf9492
DJ
18825 /* If we found a reference attribute and the DIE has no name, try
18826 to find a name in the referred to DIE. */
18827
52356b79 18828 if (name == NULL && has_specification)
72bf9492
DJ
18829 {
18830 struct partial_die_info *spec_die;
72bf9492 18831
52356b79 18832 spec_die = find_partial_die (spec_offset, spec_is_dwz, cu);
72bf9492 18833
52356b79 18834 spec_die->fixup (cu);
72bf9492
DJ
18835
18836 if (spec_die->name)
18837 {
52356b79 18838 name = spec_die->name;
72bf9492
DJ
18839
18840 /* Copy DW_AT_external attribute if it is set. */
18841 if (spec_die->is_external)
52356b79 18842 is_external = spec_die->is_external;
72bf9492
DJ
18843 }
18844 }
18845
18846 /* Set default names for some unnamed DIEs. */
72bf9492 18847
52356b79
YQ
18848 if (name == NULL && tag == DW_TAG_namespace)
18849 name = CP_ANONYMOUS_NAMESPACE_STR;
72bf9492 18850
abc72ce4
DE
18851 /* If there is no parent die to provide a namespace, and there are
18852 children, see if we can determine the namespace from their linkage
122d1940 18853 name. */
abc72ce4 18854 if (cu->language == language_cplus
518817b3
SM
18855 && !VEC_empty (dwarf2_section_info_def,
18856 cu->per_cu->dwarf2_per_objfile->types)
52356b79
YQ
18857 && die_parent == NULL
18858 && has_children
18859 && (tag == DW_TAG_class_type
18860 || tag == DW_TAG_structure_type
18861 || tag == DW_TAG_union_type))
18862 guess_partial_die_structure_name (this, cu);
abc72ce4 18863
53832f31
TT
18864 /* GCC might emit a nameless struct or union that has a linkage
18865 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
52356b79
YQ
18866 if (name == NULL
18867 && (tag == DW_TAG_class_type
18868 || tag == DW_TAG_interface_type
18869 || tag == DW_TAG_structure_type
18870 || tag == DW_TAG_union_type)
18871 && linkage_name != NULL)
53832f31
TT
18872 {
18873 char *demangled;
18874
52356b79 18875 demangled = gdb_demangle (linkage_name, DMGL_TYPES);
53832f31
TT
18876 if (demangled)
18877 {
96408a79
SA
18878 const char *base;
18879
18880 /* Strip any leading namespaces/classes, keep only the base name.
18881 DW_AT_name for named DIEs does not contain the prefixes. */
18882 base = strrchr (demangled, ':');
18883 if (base && base > demangled && base[-1] == ':')
18884 base++;
18885 else
18886 base = demangled;
18887
518817b3 18888 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
52356b79 18889 name
224c3ddb 18890 = ((const char *)
e3b94546 18891 obstack_copy0 (&objfile->per_bfd->storage_obstack,
224c3ddb 18892 base, strlen (base)));
53832f31
TT
18893 xfree (demangled);
18894 }
18895 }
18896
52356b79 18897 fixup_called = 1;
72bf9492
DJ
18898}
18899
a8329558 18900/* Read an attribute value described by an attribute form. */
c906108c 18901
d521ce57 18902static const gdb_byte *
dee91e82
DE
18903read_attribute_value (const struct die_reader_specs *reader,
18904 struct attribute *attr, unsigned form,
43988095 18905 LONGEST implicit_const, const gdb_byte *info_ptr)
c906108c 18906{
dee91e82 18907 struct dwarf2_cu *cu = reader->cu;
518817b3
SM
18908 struct dwarf2_per_objfile *dwarf2_per_objfile
18909 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 18910 struct objfile *objfile = dwarf2_per_objfile->objfile;
3e29f34a 18911 struct gdbarch *gdbarch = get_objfile_arch (objfile);
dee91e82 18912 bfd *abfd = reader->abfd;
e7c27a73 18913 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
18914 unsigned int bytes_read;
18915 struct dwarf_block *blk;
18916
aead7601 18917 attr->form = (enum dwarf_form) form;
a8329558 18918 switch (form)
c906108c 18919 {
c906108c 18920 case DW_FORM_ref_addr:
ae411497 18921 if (cu->header.version == 2)
4568ecf9 18922 DW_UNSND (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
ae411497 18923 else
4568ecf9
DE
18924 DW_UNSND (attr) = read_offset (abfd, info_ptr,
18925 &cu->header, &bytes_read);
ae411497
TT
18926 info_ptr += bytes_read;
18927 break;
36586728
TT
18928 case DW_FORM_GNU_ref_alt:
18929 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
18930 info_ptr += bytes_read;
18931 break;
ae411497 18932 case DW_FORM_addr:
e7c27a73 18933 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
3e29f34a 18934 DW_ADDR (attr) = gdbarch_adjust_dwarf2_addr (gdbarch, DW_ADDR (attr));
107d2387 18935 info_ptr += bytes_read;
c906108c
SS
18936 break;
18937 case DW_FORM_block2:
7b5a2f43 18938 blk = dwarf_alloc_block (cu);
c906108c
SS
18939 blk->size = read_2_bytes (abfd, info_ptr);
18940 info_ptr += 2;
18941 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
18942 info_ptr += blk->size;
18943 DW_BLOCK (attr) = blk;
18944 break;
18945 case DW_FORM_block4:
7b5a2f43 18946 blk = dwarf_alloc_block (cu);
c906108c
SS
18947 blk->size = read_4_bytes (abfd, info_ptr);
18948 info_ptr += 4;
18949 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
18950 info_ptr += blk->size;
18951 DW_BLOCK (attr) = blk;
18952 break;
18953 case DW_FORM_data2:
18954 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
18955 info_ptr += 2;
18956 break;
18957 case DW_FORM_data4:
18958 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
18959 info_ptr += 4;
18960 break;
18961 case DW_FORM_data8:
18962 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
18963 info_ptr += 8;
18964 break;
0224619f
JK
18965 case DW_FORM_data16:
18966 blk = dwarf_alloc_block (cu);
18967 blk->size = 16;
18968 blk->data = read_n_bytes (abfd, info_ptr, 16);
18969 info_ptr += 16;
18970 DW_BLOCK (attr) = blk;
18971 break;
2dc7f7b3
TT
18972 case DW_FORM_sec_offset:
18973 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
18974 info_ptr += bytes_read;
18975 break;
c906108c 18976 case DW_FORM_string:
9b1c24c8 18977 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
8285870a 18978 DW_STRING_IS_CANONICAL (attr) = 0;
c906108c
SS
18979 info_ptr += bytes_read;
18980 break;
4bdf3d34 18981 case DW_FORM_strp:
36586728
TT
18982 if (!cu->per_cu->is_dwz)
18983 {
ed2dc618
SM
18984 DW_STRING (attr) = read_indirect_string (dwarf2_per_objfile,
18985 abfd, info_ptr, cu_header,
36586728
TT
18986 &bytes_read);
18987 DW_STRING_IS_CANONICAL (attr) = 0;
18988 info_ptr += bytes_read;
18989 break;
18990 }
18991 /* FALLTHROUGH */
43988095
JK
18992 case DW_FORM_line_strp:
18993 if (!cu->per_cu->is_dwz)
18994 {
ed2dc618
SM
18995 DW_STRING (attr) = read_indirect_line_string (dwarf2_per_objfile,
18996 abfd, info_ptr,
43988095
JK
18997 cu_header, &bytes_read);
18998 DW_STRING_IS_CANONICAL (attr) = 0;
18999 info_ptr += bytes_read;
19000 break;
19001 }
19002 /* FALLTHROUGH */
36586728
TT
19003 case DW_FORM_GNU_strp_alt:
19004 {
ed2dc618 19005 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
36586728
TT
19006 LONGEST str_offset = read_offset (abfd, info_ptr, cu_header,
19007 &bytes_read);
19008
ed2dc618
SM
19009 DW_STRING (attr) = read_indirect_string_from_dwz (objfile,
19010 dwz, str_offset);
36586728
TT
19011 DW_STRING_IS_CANONICAL (attr) = 0;
19012 info_ptr += bytes_read;
19013 }
4bdf3d34 19014 break;
2dc7f7b3 19015 case DW_FORM_exprloc:
c906108c 19016 case DW_FORM_block:
7b5a2f43 19017 blk = dwarf_alloc_block (cu);
c906108c
SS
19018 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19019 info_ptr += bytes_read;
19020 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19021 info_ptr += blk->size;
19022 DW_BLOCK (attr) = blk;
19023 break;
19024 case DW_FORM_block1:
7b5a2f43 19025 blk = dwarf_alloc_block (cu);
c906108c
SS
19026 blk->size = read_1_byte (abfd, info_ptr);
19027 info_ptr += 1;
19028 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19029 info_ptr += blk->size;
19030 DW_BLOCK (attr) = blk;
19031 break;
19032 case DW_FORM_data1:
19033 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
19034 info_ptr += 1;
19035 break;
19036 case DW_FORM_flag:
19037 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
19038 info_ptr += 1;
19039 break;
2dc7f7b3
TT
19040 case DW_FORM_flag_present:
19041 DW_UNSND (attr) = 1;
19042 break;
c906108c
SS
19043 case DW_FORM_sdata:
19044 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
19045 info_ptr += bytes_read;
19046 break;
19047 case DW_FORM_udata:
19048 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19049 info_ptr += bytes_read;
19050 break;
19051 case DW_FORM_ref1:
9c541725 19052 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 19053 + read_1_byte (abfd, info_ptr));
c906108c
SS
19054 info_ptr += 1;
19055 break;
19056 case DW_FORM_ref2:
9c541725 19057 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 19058 + read_2_bytes (abfd, info_ptr));
c906108c
SS
19059 info_ptr += 2;
19060 break;
19061 case DW_FORM_ref4:
9c541725 19062 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 19063 + read_4_bytes (abfd, info_ptr));
c906108c
SS
19064 info_ptr += 4;
19065 break;
613e1657 19066 case DW_FORM_ref8:
9c541725 19067 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 19068 + read_8_bytes (abfd, info_ptr));
613e1657
KB
19069 info_ptr += 8;
19070 break;
55f1336d 19071 case DW_FORM_ref_sig8:
ac9ec31b 19072 DW_SIGNATURE (attr) = read_8_bytes (abfd, info_ptr);
348e048f
DE
19073 info_ptr += 8;
19074 break;
c906108c 19075 case DW_FORM_ref_udata:
9c541725 19076 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 19077 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
c906108c
SS
19078 info_ptr += bytes_read;
19079 break;
c906108c 19080 case DW_FORM_indirect:
a8329558
KW
19081 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19082 info_ptr += bytes_read;
43988095
JK
19083 if (form == DW_FORM_implicit_const)
19084 {
19085 implicit_const = read_signed_leb128 (abfd, info_ptr, &bytes_read);
19086 info_ptr += bytes_read;
19087 }
19088 info_ptr = read_attribute_value (reader, attr, form, implicit_const,
19089 info_ptr);
19090 break;
19091 case DW_FORM_implicit_const:
19092 DW_SND (attr) = implicit_const;
a8329558 19093 break;
3019eac3
DE
19094 case DW_FORM_GNU_addr_index:
19095 if (reader->dwo_file == NULL)
19096 {
19097 /* For now flag a hard error.
19098 Later we can turn this into a complaint. */
19099 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
19100 dwarf_form_name (form),
19101 bfd_get_filename (abfd));
19102 }
19103 DW_ADDR (attr) = read_addr_index_from_leb128 (cu, info_ptr, &bytes_read);
19104 info_ptr += bytes_read;
19105 break;
19106 case DW_FORM_GNU_str_index:
19107 if (reader->dwo_file == NULL)
19108 {
19109 /* For now flag a hard error.
19110 Later we can turn this into a complaint if warranted. */
19111 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
19112 dwarf_form_name (form),
19113 bfd_get_filename (abfd));
19114 }
19115 {
19116 ULONGEST str_index =
19117 read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19118
342587c4 19119 DW_STRING (attr) = read_str_index (reader, str_index);
3019eac3
DE
19120 DW_STRING_IS_CANONICAL (attr) = 0;
19121 info_ptr += bytes_read;
19122 }
19123 break;
c906108c 19124 default:
8a3fe4f8 19125 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
659b0389
ML
19126 dwarf_form_name (form),
19127 bfd_get_filename (abfd));
c906108c 19128 }
28e94949 19129
36586728 19130 /* Super hack. */
7771576e 19131 if (cu->per_cu->is_dwz && attr_form_is_ref (attr))
36586728
TT
19132 attr->form = DW_FORM_GNU_ref_alt;
19133
28e94949
JB
19134 /* We have seen instances where the compiler tried to emit a byte
19135 size attribute of -1 which ended up being encoded as an unsigned
19136 0xffffffff. Although 0xffffffff is technically a valid size value,
19137 an object of this size seems pretty unlikely so we can relatively
19138 safely treat these cases as if the size attribute was invalid and
19139 treat them as zero by default. */
19140 if (attr->name == DW_AT_byte_size
19141 && form == DW_FORM_data4
19142 && DW_UNSND (attr) >= 0xffffffff)
01c66ae6
JB
19143 {
19144 complaint
19145 (&symfile_complaints,
43bbcdc2
PH
19146 _("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
19147 hex_string (DW_UNSND (attr)));
01c66ae6
JB
19148 DW_UNSND (attr) = 0;
19149 }
28e94949 19150
c906108c
SS
19151 return info_ptr;
19152}
19153
a8329558
KW
19154/* Read an attribute described by an abbreviated attribute. */
19155
d521ce57 19156static const gdb_byte *
dee91e82
DE
19157read_attribute (const struct die_reader_specs *reader,
19158 struct attribute *attr, struct attr_abbrev *abbrev,
d521ce57 19159 const gdb_byte *info_ptr)
a8329558
KW
19160{
19161 attr->name = abbrev->name;
43988095
JK
19162 return read_attribute_value (reader, attr, abbrev->form,
19163 abbrev->implicit_const, info_ptr);
a8329558
KW
19164}
19165
0963b4bd 19166/* Read dwarf information from a buffer. */
c906108c
SS
19167
19168static unsigned int
a1855c1d 19169read_1_byte (bfd *abfd, const gdb_byte *buf)
c906108c 19170{
fe1b8b76 19171 return bfd_get_8 (abfd, buf);
c906108c
SS
19172}
19173
19174static int
a1855c1d 19175read_1_signed_byte (bfd *abfd, const gdb_byte *buf)
c906108c 19176{
fe1b8b76 19177 return bfd_get_signed_8 (abfd, buf);
c906108c
SS
19178}
19179
19180static unsigned int
a1855c1d 19181read_2_bytes (bfd *abfd, const gdb_byte *buf)
c906108c 19182{
fe1b8b76 19183 return bfd_get_16 (abfd, buf);
c906108c
SS
19184}
19185
21ae7a4d 19186static int
a1855c1d 19187read_2_signed_bytes (bfd *abfd, const gdb_byte *buf)
21ae7a4d
JK
19188{
19189 return bfd_get_signed_16 (abfd, buf);
19190}
19191
c906108c 19192static unsigned int
a1855c1d 19193read_4_bytes (bfd *abfd, const gdb_byte *buf)
c906108c 19194{
fe1b8b76 19195 return bfd_get_32 (abfd, buf);
c906108c
SS
19196}
19197
21ae7a4d 19198static int
a1855c1d 19199read_4_signed_bytes (bfd *abfd, const gdb_byte *buf)
21ae7a4d
JK
19200{
19201 return bfd_get_signed_32 (abfd, buf);
19202}
19203
93311388 19204static ULONGEST
a1855c1d 19205read_8_bytes (bfd *abfd, const gdb_byte *buf)
c906108c 19206{
fe1b8b76 19207 return bfd_get_64 (abfd, buf);
c906108c
SS
19208}
19209
19210static CORE_ADDR
d521ce57 19211read_address (bfd *abfd, const gdb_byte *buf, struct dwarf2_cu *cu,
891d2f0b 19212 unsigned int *bytes_read)
c906108c 19213{
e7c27a73 19214 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
19215 CORE_ADDR retval = 0;
19216
107d2387 19217 if (cu_header->signed_addr_p)
c906108c 19218 {
107d2387
AC
19219 switch (cu_header->addr_size)
19220 {
19221 case 2:
fe1b8b76 19222 retval = bfd_get_signed_16 (abfd, buf);
107d2387
AC
19223 break;
19224 case 4:
fe1b8b76 19225 retval = bfd_get_signed_32 (abfd, buf);
107d2387
AC
19226 break;
19227 case 8:
fe1b8b76 19228 retval = bfd_get_signed_64 (abfd, buf);
107d2387
AC
19229 break;
19230 default:
8e65ff28 19231 internal_error (__FILE__, __LINE__,
e2e0b3e5 19232 _("read_address: bad switch, signed [in module %s]"),
659b0389 19233 bfd_get_filename (abfd));
107d2387
AC
19234 }
19235 }
19236 else
19237 {
19238 switch (cu_header->addr_size)
19239 {
19240 case 2:
fe1b8b76 19241 retval = bfd_get_16 (abfd, buf);
107d2387
AC
19242 break;
19243 case 4:
fe1b8b76 19244 retval = bfd_get_32 (abfd, buf);
107d2387
AC
19245 break;
19246 case 8:
fe1b8b76 19247 retval = bfd_get_64 (abfd, buf);
107d2387
AC
19248 break;
19249 default:
8e65ff28 19250 internal_error (__FILE__, __LINE__,
a73c6dcd
MS
19251 _("read_address: bad switch, "
19252 "unsigned [in module %s]"),
659b0389 19253 bfd_get_filename (abfd));
107d2387 19254 }
c906108c 19255 }
64367e0a 19256
107d2387
AC
19257 *bytes_read = cu_header->addr_size;
19258 return retval;
c906108c
SS
19259}
19260
f7ef9339 19261/* Read the initial length from a section. The (draft) DWARF 3
613e1657
KB
19262 specification allows the initial length to take up either 4 bytes
19263 or 12 bytes. If the first 4 bytes are 0xffffffff, then the next 8
19264 bytes describe the length and all offsets will be 8 bytes in length
19265 instead of 4.
19266
f7ef9339
KB
19267 An older, non-standard 64-bit format is also handled by this
19268 function. The older format in question stores the initial length
19269 as an 8-byte quantity without an escape value. Lengths greater
19270 than 2^32 aren't very common which means that the initial 4 bytes
19271 is almost always zero. Since a length value of zero doesn't make
19272 sense for the 32-bit format, this initial zero can be considered to
19273 be an escape value which indicates the presence of the older 64-bit
19274 format. As written, the code can't detect (old format) lengths
917c78fc
MK
19275 greater than 4GB. If it becomes necessary to handle lengths
19276 somewhat larger than 4GB, we could allow other small values (such
19277 as the non-sensical values of 1, 2, and 3) to also be used as
19278 escape values indicating the presence of the old format.
f7ef9339 19279
917c78fc
MK
19280 The value returned via bytes_read should be used to increment the
19281 relevant pointer after calling read_initial_length().
c764a876 19282
613e1657
KB
19283 [ Note: read_initial_length() and read_offset() are based on the
19284 document entitled "DWARF Debugging Information Format", revision
f7ef9339 19285 3, draft 8, dated November 19, 2001. This document was obtained
613e1657
KB
19286 from:
19287
f7ef9339 19288 http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
6e70227d 19289
613e1657
KB
19290 This document is only a draft and is subject to change. (So beware.)
19291
f7ef9339 19292 Details regarding the older, non-standard 64-bit format were
917c78fc
MK
19293 determined empirically by examining 64-bit ELF files produced by
19294 the SGI toolchain on an IRIX 6.5 machine.
f7ef9339
KB
19295
19296 - Kevin, July 16, 2002
613e1657
KB
19297 ] */
19298
19299static LONGEST
d521ce57 19300read_initial_length (bfd *abfd, const gdb_byte *buf, unsigned int *bytes_read)
613e1657 19301{
fe1b8b76 19302 LONGEST length = bfd_get_32 (abfd, buf);
613e1657 19303
dd373385 19304 if (length == 0xffffffff)
613e1657 19305 {
fe1b8b76 19306 length = bfd_get_64 (abfd, buf + 4);
613e1657 19307 *bytes_read = 12;
613e1657 19308 }
dd373385 19309 else if (length == 0)
f7ef9339 19310 {
dd373385 19311 /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX. */
fe1b8b76 19312 length = bfd_get_64 (abfd, buf);
f7ef9339 19313 *bytes_read = 8;
f7ef9339 19314 }
613e1657
KB
19315 else
19316 {
19317 *bytes_read = 4;
613e1657
KB
19318 }
19319
c764a876
DE
19320 return length;
19321}
dd373385 19322
c764a876
DE
19323/* Cover function for read_initial_length.
19324 Returns the length of the object at BUF, and stores the size of the
19325 initial length in *BYTES_READ and stores the size that offsets will be in
19326 *OFFSET_SIZE.
19327 If the initial length size is not equivalent to that specified in
19328 CU_HEADER then issue a complaint.
19329 This is useful when reading non-comp-unit headers. */
dd373385 19330
c764a876 19331static LONGEST
d521ce57 19332read_checked_initial_length_and_offset (bfd *abfd, const gdb_byte *buf,
c764a876
DE
19333 const struct comp_unit_head *cu_header,
19334 unsigned int *bytes_read,
19335 unsigned int *offset_size)
19336{
19337 LONGEST length = read_initial_length (abfd, buf, bytes_read);
19338
19339 gdb_assert (cu_header->initial_length_size == 4
19340 || cu_header->initial_length_size == 8
19341 || cu_header->initial_length_size == 12);
19342
19343 if (cu_header->initial_length_size != *bytes_read)
19344 complaint (&symfile_complaints,
19345 _("intermixed 32-bit and 64-bit DWARF sections"));
dd373385 19346
c764a876 19347 *offset_size = (*bytes_read == 4) ? 4 : 8;
dd373385 19348 return length;
613e1657
KB
19349}
19350
19351/* Read an offset from the data stream. The size of the offset is
917c78fc 19352 given by cu_header->offset_size. */
613e1657
KB
19353
19354static LONGEST
d521ce57
TT
19355read_offset (bfd *abfd, const gdb_byte *buf,
19356 const struct comp_unit_head *cu_header,
891d2f0b 19357 unsigned int *bytes_read)
c764a876
DE
19358{
19359 LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
9a619af0 19360
c764a876
DE
19361 *bytes_read = cu_header->offset_size;
19362 return offset;
19363}
19364
19365/* Read an offset from the data stream. */
19366
19367static LONGEST
d521ce57 19368read_offset_1 (bfd *abfd, const gdb_byte *buf, unsigned int offset_size)
613e1657
KB
19369{
19370 LONGEST retval = 0;
19371
c764a876 19372 switch (offset_size)
613e1657
KB
19373 {
19374 case 4:
fe1b8b76 19375 retval = bfd_get_32 (abfd, buf);
613e1657
KB
19376 break;
19377 case 8:
fe1b8b76 19378 retval = bfd_get_64 (abfd, buf);
613e1657
KB
19379 break;
19380 default:
8e65ff28 19381 internal_error (__FILE__, __LINE__,
c764a876 19382 _("read_offset_1: bad switch [in module %s]"),
659b0389 19383 bfd_get_filename (abfd));
613e1657
KB
19384 }
19385
917c78fc 19386 return retval;
613e1657
KB
19387}
19388
d521ce57
TT
19389static const gdb_byte *
19390read_n_bytes (bfd *abfd, const gdb_byte *buf, unsigned int size)
c906108c
SS
19391{
19392 /* If the size of a host char is 8 bits, we can return a pointer
19393 to the buffer, otherwise we have to copy the data to a buffer
19394 allocated on the temporary obstack. */
4bdf3d34 19395 gdb_assert (HOST_CHAR_BIT == 8);
c906108c 19396 return buf;
c906108c
SS
19397}
19398
d521ce57
TT
19399static const char *
19400read_direct_string (bfd *abfd, const gdb_byte *buf,
19401 unsigned int *bytes_read_ptr)
c906108c
SS
19402{
19403 /* If the size of a host char is 8 bits, we can return a pointer
19404 to the string, otherwise we have to copy the string to a buffer
19405 allocated on the temporary obstack. */
4bdf3d34 19406 gdb_assert (HOST_CHAR_BIT == 8);
c906108c
SS
19407 if (*buf == '\0')
19408 {
19409 *bytes_read_ptr = 1;
19410 return NULL;
19411 }
d521ce57
TT
19412 *bytes_read_ptr = strlen ((const char *) buf) + 1;
19413 return (const char *) buf;
4bdf3d34
JJ
19414}
19415
43988095
JK
19416/* Return pointer to string at section SECT offset STR_OFFSET with error
19417 reporting strings FORM_NAME and SECT_NAME. */
19418
d521ce57 19419static const char *
ed2dc618
SM
19420read_indirect_string_at_offset_from (struct objfile *objfile,
19421 bfd *abfd, LONGEST str_offset,
43988095
JK
19422 struct dwarf2_section_info *sect,
19423 const char *form_name,
19424 const char *sect_name)
19425{
ed2dc618 19426 dwarf2_read_section (objfile, sect);
43988095
JK
19427 if (sect->buffer == NULL)
19428 error (_("%s used without %s section [in module %s]"),
19429 form_name, sect_name, bfd_get_filename (abfd));
19430 if (str_offset >= sect->size)
19431 error (_("%s pointing outside of %s section [in module %s]"),
19432 form_name, sect_name, bfd_get_filename (abfd));
4bdf3d34 19433 gdb_assert (HOST_CHAR_BIT == 8);
43988095 19434 if (sect->buffer[str_offset] == '\0')
4bdf3d34 19435 return NULL;
43988095
JK
19436 return (const char *) (sect->buffer + str_offset);
19437}
19438
19439/* Return pointer to string at .debug_str offset STR_OFFSET. */
19440
19441static const char *
ed2dc618
SM
19442read_indirect_string_at_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
19443 bfd *abfd, LONGEST str_offset)
43988095 19444{
ed2dc618
SM
19445 return read_indirect_string_at_offset_from (dwarf2_per_objfile->objfile,
19446 abfd, str_offset,
43988095
JK
19447 &dwarf2_per_objfile->str,
19448 "DW_FORM_strp", ".debug_str");
19449}
19450
19451/* Return pointer to string at .debug_line_str offset STR_OFFSET. */
19452
19453static const char *
ed2dc618
SM
19454read_indirect_line_string_at_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
19455 bfd *abfd, LONGEST str_offset)
43988095 19456{
ed2dc618
SM
19457 return read_indirect_string_at_offset_from (dwarf2_per_objfile->objfile,
19458 abfd, str_offset,
43988095
JK
19459 &dwarf2_per_objfile->line_str,
19460 "DW_FORM_line_strp",
19461 ".debug_line_str");
c906108c
SS
19462}
19463
36586728
TT
19464/* Read a string at offset STR_OFFSET in the .debug_str section from
19465 the .dwz file DWZ. Throw an error if the offset is too large. If
19466 the string consists of a single NUL byte, return NULL; otherwise
19467 return a pointer to the string. */
19468
d521ce57 19469static const char *
ed2dc618
SM
19470read_indirect_string_from_dwz (struct objfile *objfile, struct dwz_file *dwz,
19471 LONGEST str_offset)
36586728 19472{
ed2dc618 19473 dwarf2_read_section (objfile, &dwz->str);
36586728
TT
19474
19475 if (dwz->str.buffer == NULL)
19476 error (_("DW_FORM_GNU_strp_alt used without .debug_str "
19477 "section [in module %s]"),
19478 bfd_get_filename (dwz->dwz_bfd));
19479 if (str_offset >= dwz->str.size)
19480 error (_("DW_FORM_GNU_strp_alt pointing outside of "
19481 ".debug_str section [in module %s]"),
19482 bfd_get_filename (dwz->dwz_bfd));
19483 gdb_assert (HOST_CHAR_BIT == 8);
19484 if (dwz->str.buffer[str_offset] == '\0')
19485 return NULL;
d521ce57 19486 return (const char *) (dwz->str.buffer + str_offset);
36586728
TT
19487}
19488
43988095
JK
19489/* Return pointer to string at .debug_str offset as read from BUF.
19490 BUF is assumed to be in a compilation unit described by CU_HEADER.
19491 Return *BYTES_READ_PTR count of bytes read from BUF. */
19492
d521ce57 19493static const char *
ed2dc618
SM
19494read_indirect_string (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *abfd,
19495 const gdb_byte *buf,
cf2c3c16
TT
19496 const struct comp_unit_head *cu_header,
19497 unsigned int *bytes_read_ptr)
19498{
19499 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
19500
ed2dc618 19501 return read_indirect_string_at_offset (dwarf2_per_objfile, abfd, str_offset);
cf2c3c16
TT
19502}
19503
43988095
JK
19504/* Return pointer to string at .debug_line_str offset as read from BUF.
19505 BUF is assumed to be in a compilation unit described by CU_HEADER.
19506 Return *BYTES_READ_PTR count of bytes read from BUF. */
19507
19508static const char *
ed2dc618
SM
19509read_indirect_line_string (struct dwarf2_per_objfile *dwarf2_per_objfile,
19510 bfd *abfd, const gdb_byte *buf,
43988095
JK
19511 const struct comp_unit_head *cu_header,
19512 unsigned int *bytes_read_ptr)
19513{
19514 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
19515
ed2dc618
SM
19516 return read_indirect_line_string_at_offset (dwarf2_per_objfile, abfd,
19517 str_offset);
43988095
JK
19518}
19519
19520ULONGEST
d521ce57 19521read_unsigned_leb128 (bfd *abfd, const gdb_byte *buf,
43988095 19522 unsigned int *bytes_read_ptr)
c906108c 19523{
12df843f 19524 ULONGEST result;
ce5d95e1 19525 unsigned int num_read;
870f88f7 19526 int shift;
c906108c
SS
19527 unsigned char byte;
19528
19529 result = 0;
19530 shift = 0;
19531 num_read = 0;
c906108c
SS
19532 while (1)
19533 {
fe1b8b76 19534 byte = bfd_get_8 (abfd, buf);
c906108c
SS
19535 buf++;
19536 num_read++;
12df843f 19537 result |= ((ULONGEST) (byte & 127) << shift);
c906108c
SS
19538 if ((byte & 128) == 0)
19539 {
19540 break;
19541 }
19542 shift += 7;
19543 }
19544 *bytes_read_ptr = num_read;
19545 return result;
19546}
19547
12df843f 19548static LONGEST
d521ce57
TT
19549read_signed_leb128 (bfd *abfd, const gdb_byte *buf,
19550 unsigned int *bytes_read_ptr)
c906108c 19551{
12df843f 19552 LONGEST result;
870f88f7 19553 int shift, num_read;
c906108c
SS
19554 unsigned char byte;
19555
19556 result = 0;
19557 shift = 0;
c906108c 19558 num_read = 0;
c906108c
SS
19559 while (1)
19560 {
fe1b8b76 19561 byte = bfd_get_8 (abfd, buf);
c906108c
SS
19562 buf++;
19563 num_read++;
12df843f 19564 result |= ((LONGEST) (byte & 127) << shift);
c906108c
SS
19565 shift += 7;
19566 if ((byte & 128) == 0)
19567 {
19568 break;
19569 }
19570 }
77e0b926 19571 if ((shift < 8 * sizeof (result)) && (byte & 0x40))
12df843f 19572 result |= -(((LONGEST) 1) << shift);
c906108c
SS
19573 *bytes_read_ptr = num_read;
19574 return result;
19575}
19576
3019eac3
DE
19577/* Given index ADDR_INDEX in .debug_addr, fetch the value.
19578 ADDR_BASE is the DW_AT_GNU_addr_base attribute or zero.
19579 ADDR_SIZE is the size of addresses from the CU header. */
19580
19581static CORE_ADDR
ed2dc618
SM
19582read_addr_index_1 (struct dwarf2_per_objfile *dwarf2_per_objfile,
19583 unsigned int addr_index, ULONGEST addr_base, int addr_size)
3019eac3
DE
19584{
19585 struct objfile *objfile = dwarf2_per_objfile->objfile;
19586 bfd *abfd = objfile->obfd;
19587 const gdb_byte *info_ptr;
19588
19589 dwarf2_read_section (objfile, &dwarf2_per_objfile->addr);
19590 if (dwarf2_per_objfile->addr.buffer == NULL)
19591 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
4262abfb 19592 objfile_name (objfile));
3019eac3
DE
19593 if (addr_base + addr_index * addr_size >= dwarf2_per_objfile->addr.size)
19594 error (_("DW_FORM_addr_index pointing outside of "
19595 ".debug_addr section [in module %s]"),
4262abfb 19596 objfile_name (objfile));
3019eac3
DE
19597 info_ptr = (dwarf2_per_objfile->addr.buffer
19598 + addr_base + addr_index * addr_size);
19599 if (addr_size == 4)
19600 return bfd_get_32 (abfd, info_ptr);
19601 else
19602 return bfd_get_64 (abfd, info_ptr);
19603}
19604
19605/* Given index ADDR_INDEX in .debug_addr, fetch the value. */
19606
19607static CORE_ADDR
19608read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
19609{
518817b3
SM
19610 return read_addr_index_1 (cu->per_cu->dwarf2_per_objfile, addr_index,
19611 cu->addr_base, cu->header.addr_size);
3019eac3
DE
19612}
19613
19614/* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
19615
19616static CORE_ADDR
d521ce57 19617read_addr_index_from_leb128 (struct dwarf2_cu *cu, const gdb_byte *info_ptr,
3019eac3
DE
19618 unsigned int *bytes_read)
19619{
518817b3 19620 bfd *abfd = cu->per_cu->dwarf2_per_objfile->objfile->obfd;
3019eac3
DE
19621 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
19622
19623 return read_addr_index (cu, addr_index);
19624}
19625
19626/* Data structure to pass results from dwarf2_read_addr_index_reader
19627 back to dwarf2_read_addr_index. */
19628
19629struct dwarf2_read_addr_index_data
19630{
19631 ULONGEST addr_base;
19632 int addr_size;
19633};
19634
19635/* die_reader_func for dwarf2_read_addr_index. */
19636
19637static void
19638dwarf2_read_addr_index_reader (const struct die_reader_specs *reader,
d521ce57 19639 const gdb_byte *info_ptr,
3019eac3
DE
19640 struct die_info *comp_unit_die,
19641 int has_children,
19642 void *data)
19643{
19644 struct dwarf2_cu *cu = reader->cu;
19645 struct dwarf2_read_addr_index_data *aidata =
19646 (struct dwarf2_read_addr_index_data *) data;
19647
19648 aidata->addr_base = cu->addr_base;
19649 aidata->addr_size = cu->header.addr_size;
19650}
19651
19652/* Given an index in .debug_addr, fetch the value.
19653 NOTE: This can be called during dwarf expression evaluation,
19654 long after the debug information has been read, and thus per_cu->cu
19655 may no longer exist. */
19656
19657CORE_ADDR
19658dwarf2_read_addr_index (struct dwarf2_per_cu_data *per_cu,
19659 unsigned int addr_index)
19660{
ed2dc618
SM
19661 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
19662 struct objfile *objfile = dwarf2_per_objfile->objfile;
3019eac3
DE
19663 struct dwarf2_cu *cu = per_cu->cu;
19664 ULONGEST addr_base;
19665 int addr_size;
19666
3019eac3
DE
19667 /* We need addr_base and addr_size.
19668 If we don't have PER_CU->cu, we have to get it.
19669 Nasty, but the alternative is storing the needed info in PER_CU,
19670 which at this point doesn't seem justified: it's not clear how frequently
19671 it would get used and it would increase the size of every PER_CU.
19672 Entry points like dwarf2_per_cu_addr_size do a similar thing
19673 so we're not in uncharted territory here.
19674 Alas we need to be a bit more complicated as addr_base is contained
19675 in the DIE.
19676
19677 We don't need to read the entire CU(/TU).
19678 We just need the header and top level die.
a1b64ce1 19679
3019eac3 19680 IWBN to use the aging mechanism to let us lazily later discard the CU.
a1b64ce1 19681 For now we skip this optimization. */
3019eac3
DE
19682
19683 if (cu != NULL)
19684 {
19685 addr_base = cu->addr_base;
19686 addr_size = cu->header.addr_size;
19687 }
19688 else
19689 {
19690 struct dwarf2_read_addr_index_data aidata;
19691
a1b64ce1
DE
19692 /* Note: We can't use init_cutu_and_read_dies_simple here,
19693 we need addr_base. */
19694 init_cutu_and_read_dies (per_cu, NULL, 0, 0,
19695 dwarf2_read_addr_index_reader, &aidata);
3019eac3
DE
19696 addr_base = aidata.addr_base;
19697 addr_size = aidata.addr_size;
19698 }
19699
ed2dc618
SM
19700 return read_addr_index_1 (dwarf2_per_objfile, addr_index, addr_base,
19701 addr_size);
3019eac3
DE
19702}
19703
57d63ce2
DE
19704/* Given a DW_FORM_GNU_str_index, fetch the string.
19705 This is only used by the Fission support. */
3019eac3 19706
d521ce57 19707static const char *
342587c4 19708read_str_index (const struct die_reader_specs *reader, ULONGEST str_index)
3019eac3 19709{
ed2dc618 19710 struct dwarf2_cu *cu = reader->cu;
518817b3
SM
19711 struct dwarf2_per_objfile *dwarf2_per_objfile
19712 = cu->per_cu->dwarf2_per_objfile;
3019eac3 19713 struct objfile *objfile = dwarf2_per_objfile->objfile;
c5164cbc 19714 const char *objf_name = objfile_name (objfile);
3019eac3 19715 bfd *abfd = objfile->obfd;
73869dc2
DE
19716 struct dwarf2_section_info *str_section = &reader->dwo_file->sections.str;
19717 struct dwarf2_section_info *str_offsets_section =
19718 &reader->dwo_file->sections.str_offsets;
d521ce57 19719 const gdb_byte *info_ptr;
3019eac3 19720 ULONGEST str_offset;
57d63ce2 19721 static const char form_name[] = "DW_FORM_GNU_str_index";
3019eac3 19722
73869dc2
DE
19723 dwarf2_read_section (objfile, str_section);
19724 dwarf2_read_section (objfile, str_offsets_section);
19725 if (str_section->buffer == NULL)
57d63ce2 19726 error (_("%s used without .debug_str.dwo section"
9d8780f0
SM
19727 " in CU at offset %s [in module %s]"),
19728 form_name, sect_offset_str (cu->header.sect_off), objf_name);
73869dc2 19729 if (str_offsets_section->buffer == NULL)
57d63ce2 19730 error (_("%s used without .debug_str_offsets.dwo section"
9d8780f0
SM
19731 " in CU at offset %s [in module %s]"),
19732 form_name, sect_offset_str (cu->header.sect_off), objf_name);
73869dc2 19733 if (str_index * cu->header.offset_size >= str_offsets_section->size)
57d63ce2 19734 error (_("%s pointing outside of .debug_str_offsets.dwo"
9d8780f0
SM
19735 " section in CU at offset %s [in module %s]"),
19736 form_name, sect_offset_str (cu->header.sect_off), objf_name);
73869dc2 19737 info_ptr = (str_offsets_section->buffer
3019eac3
DE
19738 + str_index * cu->header.offset_size);
19739 if (cu->header.offset_size == 4)
19740 str_offset = bfd_get_32 (abfd, info_ptr);
19741 else
19742 str_offset = bfd_get_64 (abfd, info_ptr);
73869dc2 19743 if (str_offset >= str_section->size)
57d63ce2 19744 error (_("Offset from %s pointing outside of"
9d8780f0
SM
19745 " .debug_str.dwo section in CU at offset %s [in module %s]"),
19746 form_name, sect_offset_str (cu->header.sect_off), objf_name);
73869dc2 19747 return (const char *) (str_section->buffer + str_offset);
3019eac3
DE
19748}
19749
3019eac3
DE
19750/* Return the length of an LEB128 number in BUF. */
19751
19752static int
19753leb128_size (const gdb_byte *buf)
19754{
19755 const gdb_byte *begin = buf;
19756 gdb_byte byte;
19757
19758 while (1)
19759 {
19760 byte = *buf++;
19761 if ((byte & 128) == 0)
19762 return buf - begin;
19763 }
19764}
19765
c906108c 19766static void
e142c38c 19767set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
c906108c
SS
19768{
19769 switch (lang)
19770 {
19771 case DW_LANG_C89:
76bee0cc 19772 case DW_LANG_C99:
0cfd832f 19773 case DW_LANG_C11:
c906108c 19774 case DW_LANG_C:
d1be3247 19775 case DW_LANG_UPC:
e142c38c 19776 cu->language = language_c;
c906108c 19777 break;
9c37b5ae 19778 case DW_LANG_Java:
c906108c 19779 case DW_LANG_C_plus_plus:
0cfd832f
MW
19780 case DW_LANG_C_plus_plus_11:
19781 case DW_LANG_C_plus_plus_14:
e142c38c 19782 cu->language = language_cplus;
c906108c 19783 break;
6aecb9c2
JB
19784 case DW_LANG_D:
19785 cu->language = language_d;
19786 break;
c906108c
SS
19787 case DW_LANG_Fortran77:
19788 case DW_LANG_Fortran90:
b21b22e0 19789 case DW_LANG_Fortran95:
f7de9aab
MW
19790 case DW_LANG_Fortran03:
19791 case DW_LANG_Fortran08:
e142c38c 19792 cu->language = language_fortran;
c906108c 19793 break;
a766d390
DE
19794 case DW_LANG_Go:
19795 cu->language = language_go;
19796 break;
c906108c 19797 case DW_LANG_Mips_Assembler:
e142c38c 19798 cu->language = language_asm;
c906108c
SS
19799 break;
19800 case DW_LANG_Ada83:
8aaf0b47 19801 case DW_LANG_Ada95:
bc5f45f8
JB
19802 cu->language = language_ada;
19803 break;
72019c9c
GM
19804 case DW_LANG_Modula2:
19805 cu->language = language_m2;
19806 break;
fe8e67fd
PM
19807 case DW_LANG_Pascal83:
19808 cu->language = language_pascal;
19809 break;
22566fbd
DJ
19810 case DW_LANG_ObjC:
19811 cu->language = language_objc;
19812 break;
c44af4eb
TT
19813 case DW_LANG_Rust:
19814 case DW_LANG_Rust_old:
19815 cu->language = language_rust;
19816 break;
c906108c
SS
19817 case DW_LANG_Cobol74:
19818 case DW_LANG_Cobol85:
c906108c 19819 default:
e142c38c 19820 cu->language = language_minimal;
c906108c
SS
19821 break;
19822 }
e142c38c 19823 cu->language_defn = language_def (cu->language);
c906108c
SS
19824}
19825
19826/* Return the named attribute or NULL if not there. */
19827
19828static struct attribute *
e142c38c 19829dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
c906108c 19830{
a48e046c 19831 for (;;)
c906108c 19832 {
a48e046c
TT
19833 unsigned int i;
19834 struct attribute *spec = NULL;
19835
19836 for (i = 0; i < die->num_attrs; ++i)
19837 {
19838 if (die->attrs[i].name == name)
19839 return &die->attrs[i];
19840 if (die->attrs[i].name == DW_AT_specification
19841 || die->attrs[i].name == DW_AT_abstract_origin)
19842 spec = &die->attrs[i];
19843 }
19844
19845 if (!spec)
19846 break;
c906108c 19847
f2f0e013 19848 die = follow_die_ref (die, spec, &cu);
f2f0e013 19849 }
c5aa993b 19850
c906108c
SS
19851 return NULL;
19852}
19853
348e048f
DE
19854/* Return the named attribute or NULL if not there,
19855 but do not follow DW_AT_specification, etc.
19856 This is for use in contexts where we're reading .debug_types dies.
19857 Following DW_AT_specification, DW_AT_abstract_origin will take us
19858 back up the chain, and we want to go down. */
19859
19860static struct attribute *
45e58e77 19861dwarf2_attr_no_follow (struct die_info *die, unsigned int name)
348e048f
DE
19862{
19863 unsigned int i;
19864
19865 for (i = 0; i < die->num_attrs; ++i)
19866 if (die->attrs[i].name == name)
19867 return &die->attrs[i];
19868
19869 return NULL;
19870}
19871
7d45c7c3
KB
19872/* Return the string associated with a string-typed attribute, or NULL if it
19873 is either not found or is of an incorrect type. */
19874
19875static const char *
19876dwarf2_string_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
19877{
19878 struct attribute *attr;
19879 const char *str = NULL;
19880
19881 attr = dwarf2_attr (die, name, cu);
19882
19883 if (attr != NULL)
19884 {
43988095 19885 if (attr->form == DW_FORM_strp || attr->form == DW_FORM_line_strp
b3340438
L
19886 || attr->form == DW_FORM_string
19887 || attr->form == DW_FORM_GNU_str_index
16eb6b2d 19888 || attr->form == DW_FORM_GNU_strp_alt)
7d45c7c3
KB
19889 str = DW_STRING (attr);
19890 else
19891 complaint (&symfile_complaints,
19892 _("string type expected for attribute %s for "
9d8780f0
SM
19893 "DIE at %s in module %s"),
19894 dwarf_attr_name (name), sect_offset_str (die->sect_off),
518817b3 19895 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
7d45c7c3
KB
19896 }
19897
19898 return str;
19899}
19900
05cf31d1
JB
19901/* Return non-zero iff the attribute NAME is defined for the given DIE,
19902 and holds a non-zero value. This function should only be used for
2dc7f7b3 19903 DW_FORM_flag or DW_FORM_flag_present attributes. */
05cf31d1
JB
19904
19905static int
19906dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
19907{
19908 struct attribute *attr = dwarf2_attr (die, name, cu);
19909
19910 return (attr && DW_UNSND (attr));
19911}
19912
3ca72b44 19913static int
e142c38c 19914die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
3ca72b44 19915{
05cf31d1
JB
19916 /* A DIE is a declaration if it has a DW_AT_declaration attribute
19917 which value is non-zero. However, we have to be careful with
19918 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
19919 (via dwarf2_flag_true_p) follows this attribute. So we may
19920 end up accidently finding a declaration attribute that belongs
19921 to a different DIE referenced by the specification attribute,
19922 even though the given DIE does not have a declaration attribute. */
19923 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
19924 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
3ca72b44
AC
19925}
19926
63d06c5c 19927/* Return the die giving the specification for DIE, if there is
f2f0e013 19928 one. *SPEC_CU is the CU containing DIE on input, and the CU
edb3359d
DJ
19929 containing the return value on output. If there is no
19930 specification, but there is an abstract origin, that is
19931 returned. */
63d06c5c
DC
19932
19933static struct die_info *
f2f0e013 19934die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
63d06c5c 19935{
f2f0e013
DJ
19936 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
19937 *spec_cu);
63d06c5c 19938
edb3359d
DJ
19939 if (spec_attr == NULL)
19940 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
19941
63d06c5c
DC
19942 if (spec_attr == NULL)
19943 return NULL;
19944 else
f2f0e013 19945 return follow_die_ref (die, spec_attr, spec_cu);
63d06c5c 19946}
c906108c 19947
527f3840
JK
19948/* Stub for free_line_header to match void * callback types. */
19949
19950static void
19951free_line_header_voidp (void *arg)
19952{
9a3c8263 19953 struct line_header *lh = (struct line_header *) arg;
527f3840 19954
fff8551c 19955 delete lh;
527f3840
JK
19956}
19957
fff8551c
PA
19958void
19959line_header::add_include_dir (const char *include_dir)
c906108c 19960{
27e0867f 19961 if (dwarf_line_debug >= 2)
fff8551c
PA
19962 fprintf_unfiltered (gdb_stdlog, "Adding dir %zu: %s\n",
19963 include_dirs.size () + 1, include_dir);
27e0867f 19964
fff8551c 19965 include_dirs.push_back (include_dir);
debd256d 19966}
6e70227d 19967
fff8551c
PA
19968void
19969line_header::add_file_name (const char *name,
ecfb656c 19970 dir_index d_index,
fff8551c
PA
19971 unsigned int mod_time,
19972 unsigned int length)
debd256d 19973{
27e0867f
DE
19974 if (dwarf_line_debug >= 2)
19975 fprintf_unfiltered (gdb_stdlog, "Adding file %u: %s\n",
fff8551c 19976 (unsigned) file_names.size () + 1, name);
27e0867f 19977
ecfb656c 19978 file_names.emplace_back (name, d_index, mod_time, length);
debd256d 19979}
6e70227d 19980
83769d0b 19981/* A convenience function to find the proper .debug_line section for a CU. */
36586728
TT
19982
19983static struct dwarf2_section_info *
19984get_debug_line_section (struct dwarf2_cu *cu)
19985{
19986 struct dwarf2_section_info *section;
518817b3
SM
19987 struct dwarf2_per_objfile *dwarf2_per_objfile
19988 = cu->per_cu->dwarf2_per_objfile;
36586728
TT
19989
19990 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
19991 DWO file. */
19992 if (cu->dwo_unit && cu->per_cu->is_debug_types)
19993 section = &cu->dwo_unit->dwo_file->sections.line;
19994 else if (cu->per_cu->is_dwz)
19995 {
ed2dc618 19996 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
36586728
TT
19997
19998 section = &dwz->line;
19999 }
20000 else
20001 section = &dwarf2_per_objfile->line;
20002
20003 return section;
20004}
20005
43988095
JK
20006/* Read directory or file name entry format, starting with byte of
20007 format count entries, ULEB128 pairs of entry formats, ULEB128 of
20008 entries count and the entries themselves in the described entry
20009 format. */
20010
20011static void
ed2dc618
SM
20012read_formatted_entries (struct dwarf2_per_objfile *dwarf2_per_objfile,
20013 bfd *abfd, const gdb_byte **bufp,
43988095
JK
20014 struct line_header *lh,
20015 const struct comp_unit_head *cu_header,
20016 void (*callback) (struct line_header *lh,
20017 const char *name,
ecfb656c 20018 dir_index d_index,
43988095
JK
20019 unsigned int mod_time,
20020 unsigned int length))
20021{
20022 gdb_byte format_count, formati;
20023 ULONGEST data_count, datai;
20024 const gdb_byte *buf = *bufp;
20025 const gdb_byte *format_header_data;
43988095
JK
20026 unsigned int bytes_read;
20027
20028 format_count = read_1_byte (abfd, buf);
20029 buf += 1;
20030 format_header_data = buf;
20031 for (formati = 0; formati < format_count; formati++)
20032 {
20033 read_unsigned_leb128 (abfd, buf, &bytes_read);
20034 buf += bytes_read;
20035 read_unsigned_leb128 (abfd, buf, &bytes_read);
20036 buf += bytes_read;
20037 }
20038
20039 data_count = read_unsigned_leb128 (abfd, buf, &bytes_read);
20040 buf += bytes_read;
20041 for (datai = 0; datai < data_count; datai++)
20042 {
20043 const gdb_byte *format = format_header_data;
20044 struct file_entry fe;
20045
43988095
JK
20046 for (formati = 0; formati < format_count; formati++)
20047 {
ecfb656c 20048 ULONGEST content_type = read_unsigned_leb128 (abfd, format, &bytes_read);
43988095 20049 format += bytes_read;
43988095 20050
ecfb656c 20051 ULONGEST form = read_unsigned_leb128 (abfd, format, &bytes_read);
43988095 20052 format += bytes_read;
ecfb656c
PA
20053
20054 gdb::optional<const char *> string;
20055 gdb::optional<unsigned int> uint;
20056
43988095
JK
20057 switch (form)
20058 {
20059 case DW_FORM_string:
ecfb656c 20060 string.emplace (read_direct_string (abfd, buf, &bytes_read));
43988095
JK
20061 buf += bytes_read;
20062 break;
20063
20064 case DW_FORM_line_strp:
ed2dc618
SM
20065 string.emplace (read_indirect_line_string (dwarf2_per_objfile,
20066 abfd, buf,
ecfb656c
PA
20067 cu_header,
20068 &bytes_read));
43988095
JK
20069 buf += bytes_read;
20070 break;
20071
20072 case DW_FORM_data1:
ecfb656c 20073 uint.emplace (read_1_byte (abfd, buf));
43988095
JK
20074 buf += 1;
20075 break;
20076
20077 case DW_FORM_data2:
ecfb656c 20078 uint.emplace (read_2_bytes (abfd, buf));
43988095
JK
20079 buf += 2;
20080 break;
20081
20082 case DW_FORM_data4:
ecfb656c 20083 uint.emplace (read_4_bytes (abfd, buf));
43988095
JK
20084 buf += 4;
20085 break;
20086
20087 case DW_FORM_data8:
ecfb656c 20088 uint.emplace (read_8_bytes (abfd, buf));
43988095
JK
20089 buf += 8;
20090 break;
20091
20092 case DW_FORM_udata:
ecfb656c 20093 uint.emplace (read_unsigned_leb128 (abfd, buf, &bytes_read));
43988095
JK
20094 buf += bytes_read;
20095 break;
20096
20097 case DW_FORM_block:
20098 /* It is valid only for DW_LNCT_timestamp which is ignored by
20099 current GDB. */
20100 break;
20101 }
ecfb656c
PA
20102
20103 switch (content_type)
20104 {
20105 case DW_LNCT_path:
20106 if (string.has_value ())
20107 fe.name = *string;
20108 break;
20109 case DW_LNCT_directory_index:
20110 if (uint.has_value ())
20111 fe.d_index = (dir_index) *uint;
20112 break;
20113 case DW_LNCT_timestamp:
20114 if (uint.has_value ())
20115 fe.mod_time = *uint;
20116 break;
20117 case DW_LNCT_size:
20118 if (uint.has_value ())
20119 fe.length = *uint;
20120 break;
20121 case DW_LNCT_MD5:
20122 break;
20123 default:
20124 complaint (&symfile_complaints,
20125 _("Unknown format content type %s"),
20126 pulongest (content_type));
20127 }
43988095
JK
20128 }
20129
ecfb656c 20130 callback (lh, fe.name, fe.d_index, fe.mod_time, fe.length);
43988095
JK
20131 }
20132
20133 *bufp = buf;
20134}
20135
debd256d 20136/* Read the statement program header starting at OFFSET in
3019eac3 20137 .debug_line, or .debug_line.dwo. Return a pointer
6502dd73 20138 to a struct line_header, allocated using xmalloc.
cd366ee8
DE
20139 Returns NULL if there is a problem reading the header, e.g., if it
20140 has a version we don't understand.
debd256d
JB
20141
20142 NOTE: the strings in the include directory and file name tables of
3019eac3
DE
20143 the returned object point into the dwarf line section buffer,
20144 and must not be freed. */
ae2de4f8 20145
fff8551c 20146static line_header_up
9c541725 20147dwarf_decode_line_header (sect_offset sect_off, struct dwarf2_cu *cu)
debd256d 20148{
d521ce57 20149 const gdb_byte *line_ptr;
c764a876 20150 unsigned int bytes_read, offset_size;
debd256d 20151 int i;
d521ce57 20152 const char *cur_dir, *cur_file;
3019eac3
DE
20153 struct dwarf2_section_info *section;
20154 bfd *abfd;
518817b3
SM
20155 struct dwarf2_per_objfile *dwarf2_per_objfile
20156 = cu->per_cu->dwarf2_per_objfile;
3019eac3 20157
36586728 20158 section = get_debug_line_section (cu);
3019eac3
DE
20159 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
20160 if (section->buffer == NULL)
debd256d 20161 {
3019eac3
DE
20162 if (cu->dwo_unit && cu->per_cu->is_debug_types)
20163 complaint (&symfile_complaints, _("missing .debug_line.dwo section"));
20164 else
20165 complaint (&symfile_complaints, _("missing .debug_line section"));
debd256d
JB
20166 return 0;
20167 }
20168
fceca515
DE
20169 /* We can't do this until we know the section is non-empty.
20170 Only then do we know we have such a section. */
a32a8923 20171 abfd = get_section_bfd_owner (section);
fceca515 20172
a738430d
MK
20173 /* Make sure that at least there's room for the total_length field.
20174 That could be 12 bytes long, but we're just going to fudge that. */
9c541725 20175 if (to_underlying (sect_off) + 4 >= section->size)
debd256d 20176 {
4d3c2250 20177 dwarf2_statement_list_fits_in_line_number_section_complaint ();
debd256d
JB
20178 return 0;
20179 }
20180
fff8551c 20181 line_header_up lh (new line_header ());
debd256d 20182
9c541725 20183 lh->sect_off = sect_off;
527f3840
JK
20184 lh->offset_in_dwz = cu->per_cu->is_dwz;
20185
9c541725 20186 line_ptr = section->buffer + to_underlying (sect_off);
debd256d 20187
a738430d 20188 /* Read in the header. */
6e70227d 20189 lh->total_length =
c764a876
DE
20190 read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
20191 &bytes_read, &offset_size);
debd256d 20192 line_ptr += bytes_read;
3019eac3 20193 if (line_ptr + lh->total_length > (section->buffer + section->size))
debd256d 20194 {
4d3c2250 20195 dwarf2_statement_list_fits_in_line_number_section_complaint ();
debd256d
JB
20196 return 0;
20197 }
20198 lh->statement_program_end = line_ptr + lh->total_length;
20199 lh->version = read_2_bytes (abfd, line_ptr);
20200 line_ptr += 2;
43988095 20201 if (lh->version > 5)
cd366ee8
DE
20202 {
20203 /* This is a version we don't understand. The format could have
20204 changed in ways we don't handle properly so just punt. */
20205 complaint (&symfile_complaints,
20206 _("unsupported version in .debug_line section"));
20207 return NULL;
20208 }
43988095
JK
20209 if (lh->version >= 5)
20210 {
20211 gdb_byte segment_selector_size;
20212
20213 /* Skip address size. */
20214 read_1_byte (abfd, line_ptr);
20215 line_ptr += 1;
20216
20217 segment_selector_size = read_1_byte (abfd, line_ptr);
20218 line_ptr += 1;
20219 if (segment_selector_size != 0)
20220 {
20221 complaint (&symfile_complaints,
20222 _("unsupported segment selector size %u "
20223 "in .debug_line section"),
20224 segment_selector_size);
20225 return NULL;
20226 }
20227 }
c764a876
DE
20228 lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
20229 line_ptr += offset_size;
debd256d
JB
20230 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
20231 line_ptr += 1;
2dc7f7b3
TT
20232 if (lh->version >= 4)
20233 {
20234 lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
20235 line_ptr += 1;
20236 }
20237 else
20238 lh->maximum_ops_per_instruction = 1;
20239
20240 if (lh->maximum_ops_per_instruction == 0)
20241 {
20242 lh->maximum_ops_per_instruction = 1;
20243 complaint (&symfile_complaints,
3e43a32a
MS
20244 _("invalid maximum_ops_per_instruction "
20245 "in `.debug_line' section"));
2dc7f7b3
TT
20246 }
20247
debd256d
JB
20248 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
20249 line_ptr += 1;
20250 lh->line_base = read_1_signed_byte (abfd, line_ptr);
20251 line_ptr += 1;
20252 lh->line_range = read_1_byte (abfd, line_ptr);
20253 line_ptr += 1;
20254 lh->opcode_base = read_1_byte (abfd, line_ptr);
20255 line_ptr += 1;
fff8551c 20256 lh->standard_opcode_lengths.reset (new unsigned char[lh->opcode_base]);
debd256d
JB
20257
20258 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
20259 for (i = 1; i < lh->opcode_base; ++i)
20260 {
20261 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
20262 line_ptr += 1;
20263 }
20264
43988095 20265 if (lh->version >= 5)
debd256d 20266 {
43988095 20267 /* Read directory table. */
ed2dc618
SM
20268 read_formatted_entries (dwarf2_per_objfile, abfd, &line_ptr, lh.get (),
20269 &cu->header,
fff8551c 20270 [] (struct line_header *lh, const char *name,
ecfb656c 20271 dir_index d_index, unsigned int mod_time,
fff8551c
PA
20272 unsigned int length)
20273 {
20274 lh->add_include_dir (name);
20275 });
debd256d 20276
43988095 20277 /* Read file name table. */
ed2dc618
SM
20278 read_formatted_entries (dwarf2_per_objfile, abfd, &line_ptr, lh.get (),
20279 &cu->header,
fff8551c 20280 [] (struct line_header *lh, const char *name,
ecfb656c 20281 dir_index d_index, unsigned int mod_time,
fff8551c
PA
20282 unsigned int length)
20283 {
ecfb656c 20284 lh->add_file_name (name, d_index, mod_time, length);
fff8551c 20285 });
43988095
JK
20286 }
20287 else
debd256d 20288 {
43988095
JK
20289 /* Read directory table. */
20290 while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
20291 {
20292 line_ptr += bytes_read;
fff8551c 20293 lh->add_include_dir (cur_dir);
43988095 20294 }
debd256d
JB
20295 line_ptr += bytes_read;
20296
43988095
JK
20297 /* Read file name table. */
20298 while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
20299 {
ecfb656c
PA
20300 unsigned int mod_time, length;
20301 dir_index d_index;
43988095
JK
20302
20303 line_ptr += bytes_read;
ecfb656c 20304 d_index = (dir_index) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
43988095
JK
20305 line_ptr += bytes_read;
20306 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20307 line_ptr += bytes_read;
20308 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20309 line_ptr += bytes_read;
20310
ecfb656c 20311 lh->add_file_name (cur_file, d_index, mod_time, length);
43988095
JK
20312 }
20313 line_ptr += bytes_read;
debd256d 20314 }
6e70227d 20315 lh->statement_program_start = line_ptr;
debd256d 20316
3019eac3 20317 if (line_ptr > (section->buffer + section->size))
4d3c2250 20318 complaint (&symfile_complaints,
3e43a32a
MS
20319 _("line number info header doesn't "
20320 "fit in `.debug_line' section"));
debd256d 20321
debd256d
JB
20322 return lh;
20323}
c906108c 20324
c6da4cef
DE
20325/* Subroutine of dwarf_decode_lines to simplify it.
20326 Return the file name of the psymtab for included file FILE_INDEX
20327 in line header LH of PST.
20328 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
c89b44cd
TT
20329 If space for the result is malloc'd, *NAME_HOLDER will be set.
20330 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
c6da4cef 20331
d521ce57 20332static const char *
c6da4cef
DE
20333psymtab_include_file_name (const struct line_header *lh, int file_index,
20334 const struct partial_symtab *pst,
c89b44cd
TT
20335 const char *comp_dir,
20336 gdb::unique_xmalloc_ptr<char> *name_holder)
c6da4cef 20337{
8c43009f 20338 const file_entry &fe = lh->file_names[file_index];
d521ce57
TT
20339 const char *include_name = fe.name;
20340 const char *include_name_to_compare = include_name;
72b9f47f 20341 const char *pst_filename;
c6da4cef
DE
20342 int file_is_pst;
20343
8c43009f 20344 const char *dir_name = fe.include_dir (lh);
c6da4cef 20345
c89b44cd 20346 gdb::unique_xmalloc_ptr<char> hold_compare;
c6da4cef
DE
20347 if (!IS_ABSOLUTE_PATH (include_name)
20348 && (dir_name != NULL || comp_dir != NULL))
20349 {
20350 /* Avoid creating a duplicate psymtab for PST.
20351 We do this by comparing INCLUDE_NAME and PST_FILENAME.
20352 Before we do the comparison, however, we need to account
20353 for DIR_NAME and COMP_DIR.
20354 First prepend dir_name (if non-NULL). If we still don't
20355 have an absolute path prepend comp_dir (if non-NULL).
20356 However, the directory we record in the include-file's
20357 psymtab does not contain COMP_DIR (to match the
20358 corresponding symtab(s)).
20359
20360 Example:
20361
20362 bash$ cd /tmp
20363 bash$ gcc -g ./hello.c
20364 include_name = "hello.c"
20365 dir_name = "."
20366 DW_AT_comp_dir = comp_dir = "/tmp"
5f52445b
YQ
20367 DW_AT_name = "./hello.c"
20368
20369 */
c6da4cef
DE
20370
20371 if (dir_name != NULL)
20372 {
c89b44cd
TT
20373 name_holder->reset (concat (dir_name, SLASH_STRING,
20374 include_name, (char *) NULL));
20375 include_name = name_holder->get ();
c6da4cef 20376 include_name_to_compare = include_name;
c6da4cef
DE
20377 }
20378 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
20379 {
c89b44cd
TT
20380 hold_compare.reset (concat (comp_dir, SLASH_STRING,
20381 include_name, (char *) NULL));
20382 include_name_to_compare = hold_compare.get ();
c6da4cef
DE
20383 }
20384 }
20385
20386 pst_filename = pst->filename;
c89b44cd 20387 gdb::unique_xmalloc_ptr<char> copied_name;
c6da4cef
DE
20388 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
20389 {
c89b44cd
TT
20390 copied_name.reset (concat (pst->dirname, SLASH_STRING,
20391 pst_filename, (char *) NULL));
20392 pst_filename = copied_name.get ();
c6da4cef
DE
20393 }
20394
1e3fad37 20395 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
c6da4cef 20396
c6da4cef
DE
20397 if (file_is_pst)
20398 return NULL;
20399 return include_name;
20400}
20401
d9b3de22
DE
20402/* State machine to track the state of the line number program. */
20403
6f77053d 20404class lnp_state_machine
d9b3de22 20405{
6f77053d
PA
20406public:
20407 /* Initialize a machine state for the start of a line number
20408 program. */
20409 lnp_state_machine (gdbarch *arch, line_header *lh, bool record_lines_p);
20410
8c43009f
PA
20411 file_entry *current_file ()
20412 {
20413 /* lh->file_names is 0-based, but the file name numbers in the
20414 statement program are 1-based. */
6f77053d
PA
20415 return m_line_header->file_name_at (m_file);
20416 }
20417
20418 /* Record the line in the state machine. END_SEQUENCE is true if
20419 we're processing the end of a sequence. */
20420 void record_line (bool end_sequence);
20421
20422 /* Check address and if invalid nop-out the rest of the lines in this
20423 sequence. */
20424 void check_line_address (struct dwarf2_cu *cu,
20425 const gdb_byte *line_ptr,
20426 CORE_ADDR lowpc, CORE_ADDR address);
20427
20428 void handle_set_discriminator (unsigned int discriminator)
20429 {
20430 m_discriminator = discriminator;
20431 m_line_has_non_zero_discriminator |= discriminator != 0;
20432 }
20433
20434 /* Handle DW_LNE_set_address. */
20435 void handle_set_address (CORE_ADDR baseaddr, CORE_ADDR address)
20436 {
20437 m_op_index = 0;
20438 address += baseaddr;
20439 m_address = gdbarch_adjust_dwarf2_line (m_gdbarch, address, false);
20440 }
20441
20442 /* Handle DW_LNS_advance_pc. */
20443 void handle_advance_pc (CORE_ADDR adjust);
20444
20445 /* Handle a special opcode. */
20446 void handle_special_opcode (unsigned char op_code);
20447
20448 /* Handle DW_LNS_advance_line. */
20449 void handle_advance_line (int line_delta)
20450 {
20451 advance_line (line_delta);
20452 }
20453
20454 /* Handle DW_LNS_set_file. */
20455 void handle_set_file (file_name_index file);
20456
20457 /* Handle DW_LNS_negate_stmt. */
20458 void handle_negate_stmt ()
20459 {
20460 m_is_stmt = !m_is_stmt;
20461 }
20462
20463 /* Handle DW_LNS_const_add_pc. */
20464 void handle_const_add_pc ();
20465
20466 /* Handle DW_LNS_fixed_advance_pc. */
20467 void handle_fixed_advance_pc (CORE_ADDR addr_adj)
20468 {
20469 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20470 m_op_index = 0;
20471 }
20472
20473 /* Handle DW_LNS_copy. */
20474 void handle_copy ()
20475 {
20476 record_line (false);
20477 m_discriminator = 0;
20478 }
20479
20480 /* Handle DW_LNE_end_sequence. */
20481 void handle_end_sequence ()
20482 {
20483 m_record_line_callback = ::record_line;
20484 }
20485
20486private:
20487 /* Advance the line by LINE_DELTA. */
20488 void advance_line (int line_delta)
20489 {
20490 m_line += line_delta;
20491
20492 if (line_delta != 0)
20493 m_line_has_non_zero_discriminator = m_discriminator != 0;
8c43009f
PA
20494 }
20495
6f77053d
PA
20496 gdbarch *m_gdbarch;
20497
20498 /* True if we're recording lines.
20499 Otherwise we're building partial symtabs and are just interested in
20500 finding include files mentioned by the line number program. */
20501 bool m_record_lines_p;
20502
8c43009f 20503 /* The line number header. */
6f77053d 20504 line_header *m_line_header;
8c43009f 20505
6f77053d
PA
20506 /* These are part of the standard DWARF line number state machine,
20507 and initialized according to the DWARF spec. */
d9b3de22 20508
6f77053d 20509 unsigned char m_op_index = 0;
8c43009f 20510 /* The line table index (1-based) of the current file. */
6f77053d
PA
20511 file_name_index m_file = (file_name_index) 1;
20512 unsigned int m_line = 1;
20513
20514 /* These are initialized in the constructor. */
20515
20516 CORE_ADDR m_address;
20517 bool m_is_stmt;
20518 unsigned int m_discriminator;
d9b3de22
DE
20519
20520 /* Additional bits of state we need to track. */
20521
20522 /* The last file that we called dwarf2_start_subfile for.
20523 This is only used for TLLs. */
6f77053d 20524 unsigned int m_last_file = 0;
d9b3de22 20525 /* The last file a line number was recorded for. */
6f77053d 20526 struct subfile *m_last_subfile = NULL;
d9b3de22
DE
20527
20528 /* The function to call to record a line. */
6f77053d 20529 record_line_ftype *m_record_line_callback = NULL;
d9b3de22
DE
20530
20531 /* The last line number that was recorded, used to coalesce
20532 consecutive entries for the same line. This can happen, for
20533 example, when discriminators are present. PR 17276. */
6f77053d
PA
20534 unsigned int m_last_line = 0;
20535 bool m_line_has_non_zero_discriminator = false;
8c43009f 20536};
d9b3de22 20537
6f77053d
PA
20538void
20539lnp_state_machine::handle_advance_pc (CORE_ADDR adjust)
20540{
20541 CORE_ADDR addr_adj = (((m_op_index + adjust)
20542 / m_line_header->maximum_ops_per_instruction)
20543 * m_line_header->minimum_instruction_length);
20544 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20545 m_op_index = ((m_op_index + adjust)
20546 % m_line_header->maximum_ops_per_instruction);
20547}
d9b3de22 20548
6f77053d
PA
20549void
20550lnp_state_machine::handle_special_opcode (unsigned char op_code)
d9b3de22 20551{
6f77053d
PA
20552 unsigned char adj_opcode = op_code - m_line_header->opcode_base;
20553 CORE_ADDR addr_adj = (((m_op_index
20554 + (adj_opcode / m_line_header->line_range))
20555 / m_line_header->maximum_ops_per_instruction)
20556 * m_line_header->minimum_instruction_length);
20557 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20558 m_op_index = ((m_op_index + (adj_opcode / m_line_header->line_range))
20559 % m_line_header->maximum_ops_per_instruction);
d9b3de22 20560
6f77053d
PA
20561 int line_delta = (m_line_header->line_base
20562 + (adj_opcode % m_line_header->line_range));
20563 advance_line (line_delta);
20564 record_line (false);
20565 m_discriminator = 0;
20566}
d9b3de22 20567
6f77053d
PA
20568void
20569lnp_state_machine::handle_set_file (file_name_index file)
20570{
20571 m_file = file;
20572
20573 const file_entry *fe = current_file ();
20574 if (fe == NULL)
20575 dwarf2_debug_line_missing_file_complaint ();
20576 else if (m_record_lines_p)
20577 {
20578 const char *dir = fe->include_dir (m_line_header);
20579
20580 m_last_subfile = current_subfile;
20581 m_line_has_non_zero_discriminator = m_discriminator != 0;
20582 dwarf2_start_subfile (fe->name, dir);
20583 }
20584}
20585
20586void
20587lnp_state_machine::handle_const_add_pc ()
20588{
20589 CORE_ADDR adjust
20590 = (255 - m_line_header->opcode_base) / m_line_header->line_range;
20591
20592 CORE_ADDR addr_adj
20593 = (((m_op_index + adjust)
20594 / m_line_header->maximum_ops_per_instruction)
20595 * m_line_header->minimum_instruction_length);
20596
20597 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20598 m_op_index = ((m_op_index + adjust)
20599 % m_line_header->maximum_ops_per_instruction);
20600}
d9b3de22 20601
c91513d8
PP
20602/* Ignore this record_line request. */
20603
20604static void
20605noop_record_line (struct subfile *subfile, int line, CORE_ADDR pc)
20606{
20607 return;
20608}
20609
a05a36a5
DE
20610/* Return non-zero if we should add LINE to the line number table.
20611 LINE is the line to add, LAST_LINE is the last line that was added,
20612 LAST_SUBFILE is the subfile for LAST_LINE.
20613 LINE_HAS_NON_ZERO_DISCRIMINATOR is non-zero if LINE has ever
20614 had a non-zero discriminator.
20615
20616 We have to be careful in the presence of discriminators.
20617 E.g., for this line:
20618
20619 for (i = 0; i < 100000; i++);
20620
20621 clang can emit four line number entries for that one line,
20622 each with a different discriminator.
20623 See gdb.dwarf2/dw2-single-line-discriminators.exp for an example.
20624
20625 However, we want gdb to coalesce all four entries into one.
20626 Otherwise the user could stepi into the middle of the line and
20627 gdb would get confused about whether the pc really was in the
20628 middle of the line.
20629
20630 Things are further complicated by the fact that two consecutive
20631 line number entries for the same line is a heuristic used by gcc
20632 to denote the end of the prologue. So we can't just discard duplicate
20633 entries, we have to be selective about it. The heuristic we use is
20634 that we only collapse consecutive entries for the same line if at least
20635 one of those entries has a non-zero discriminator. PR 17276.
20636
20637 Note: Addresses in the line number state machine can never go backwards
20638 within one sequence, thus this coalescing is ok. */
20639
20640static int
20641dwarf_record_line_p (unsigned int line, unsigned int last_line,
20642 int line_has_non_zero_discriminator,
20643 struct subfile *last_subfile)
20644{
20645 if (current_subfile != last_subfile)
20646 return 1;
20647 if (line != last_line)
20648 return 1;
20649 /* Same line for the same file that we've seen already.
20650 As a last check, for pr 17276, only record the line if the line
20651 has never had a non-zero discriminator. */
20652 if (!line_has_non_zero_discriminator)
20653 return 1;
20654 return 0;
20655}
20656
252a6764
DE
20657/* Use P_RECORD_LINE to record line number LINE beginning at address ADDRESS
20658 in the line table of subfile SUBFILE. */
20659
20660static void
d9b3de22
DE
20661dwarf_record_line_1 (struct gdbarch *gdbarch, struct subfile *subfile,
20662 unsigned int line, CORE_ADDR address,
20663 record_line_ftype p_record_line)
252a6764
DE
20664{
20665 CORE_ADDR addr = gdbarch_addr_bits_remove (gdbarch, address);
20666
27e0867f
DE
20667 if (dwarf_line_debug)
20668 {
20669 fprintf_unfiltered (gdb_stdlog,
20670 "Recording line %u, file %s, address %s\n",
20671 line, lbasename (subfile->name),
20672 paddress (gdbarch, address));
20673 }
20674
d5962de5 20675 (*p_record_line) (subfile, line, addr);
252a6764
DE
20676}
20677
20678/* Subroutine of dwarf_decode_lines_1 to simplify it.
20679 Mark the end of a set of line number records.
d9b3de22 20680 The arguments are the same as for dwarf_record_line_1.
252a6764
DE
20681 If SUBFILE is NULL the request is ignored. */
20682
20683static void
20684dwarf_finish_line (struct gdbarch *gdbarch, struct subfile *subfile,
20685 CORE_ADDR address, record_line_ftype p_record_line)
20686{
27e0867f
DE
20687 if (subfile == NULL)
20688 return;
20689
20690 if (dwarf_line_debug)
20691 {
20692 fprintf_unfiltered (gdb_stdlog,
20693 "Finishing current line, file %s, address %s\n",
20694 lbasename (subfile->name),
20695 paddress (gdbarch, address));
20696 }
20697
d9b3de22
DE
20698 dwarf_record_line_1 (gdbarch, subfile, 0, address, p_record_line);
20699}
20700
6f77053d
PA
20701void
20702lnp_state_machine::record_line (bool end_sequence)
d9b3de22 20703{
d9b3de22
DE
20704 if (dwarf_line_debug)
20705 {
20706 fprintf_unfiltered (gdb_stdlog,
20707 "Processing actual line %u: file %u,"
20708 " address %s, is_stmt %u, discrim %u\n",
6f77053d
PA
20709 m_line, to_underlying (m_file),
20710 paddress (m_gdbarch, m_address),
20711 m_is_stmt, m_discriminator);
d9b3de22
DE
20712 }
20713
6f77053d 20714 file_entry *fe = current_file ();
8c43009f
PA
20715
20716 if (fe == NULL)
d9b3de22
DE
20717 dwarf2_debug_line_missing_file_complaint ();
20718 /* For now we ignore lines not starting on an instruction boundary.
20719 But not when processing end_sequence for compatibility with the
20720 previous version of the code. */
6f77053d 20721 else if (m_op_index == 0 || end_sequence)
d9b3de22 20722 {
8c43009f 20723 fe->included_p = 1;
6f77053d 20724 if (m_record_lines_p && m_is_stmt)
d9b3de22 20725 {
6f77053d 20726 if (m_last_subfile != current_subfile || end_sequence)
d9b3de22 20727 {
6f77053d
PA
20728 dwarf_finish_line (m_gdbarch, m_last_subfile,
20729 m_address, m_record_line_callback);
d9b3de22
DE
20730 }
20731
20732 if (!end_sequence)
20733 {
6f77053d
PA
20734 if (dwarf_record_line_p (m_line, m_last_line,
20735 m_line_has_non_zero_discriminator,
20736 m_last_subfile))
d9b3de22 20737 {
6f77053d
PA
20738 dwarf_record_line_1 (m_gdbarch, current_subfile,
20739 m_line, m_address,
20740 m_record_line_callback);
d9b3de22 20741 }
6f77053d
PA
20742 m_last_subfile = current_subfile;
20743 m_last_line = m_line;
d9b3de22
DE
20744 }
20745 }
20746 }
20747}
20748
6f77053d
PA
20749lnp_state_machine::lnp_state_machine (gdbarch *arch, line_header *lh,
20750 bool record_lines_p)
d9b3de22 20751{
6f77053d
PA
20752 m_gdbarch = arch;
20753 m_record_lines_p = record_lines_p;
20754 m_line_header = lh;
d9b3de22 20755
6f77053d 20756 m_record_line_callback = ::record_line;
d9b3de22 20757
d9b3de22
DE
20758 /* Call `gdbarch_adjust_dwarf2_line' on the initial 0 address as if there
20759 was a line entry for it so that the backend has a chance to adjust it
20760 and also record it in case it needs it. This is currently used by MIPS
20761 code, cf. `mips_adjust_dwarf2_line'. */
6f77053d
PA
20762 m_address = gdbarch_adjust_dwarf2_line (arch, 0, 0);
20763 m_is_stmt = lh->default_is_stmt;
20764 m_discriminator = 0;
252a6764
DE
20765}
20766
6f77053d
PA
20767void
20768lnp_state_machine::check_line_address (struct dwarf2_cu *cu,
20769 const gdb_byte *line_ptr,
20770 CORE_ADDR lowpc, CORE_ADDR address)
924c2928
DE
20771{
20772 /* If address < lowpc then it's not a usable value, it's outside the
20773 pc range of the CU. However, we restrict the test to only address
20774 values of zero to preserve GDB's previous behaviour which is to
20775 handle the specific case of a function being GC'd by the linker. */
20776
20777 if (address == 0 && address < lowpc)
20778 {
20779 /* This line table is for a function which has been
20780 GCd by the linker. Ignore it. PR gdb/12528 */
20781
518817b3 20782 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
924c2928
DE
20783 long line_offset = line_ptr - get_debug_line_section (cu)->buffer;
20784
20785 complaint (&symfile_complaints,
20786 _(".debug_line address at offset 0x%lx is 0 [in module %s]"),
20787 line_offset, objfile_name (objfile));
6f77053d
PA
20788 m_record_line_callback = noop_record_line;
20789 /* Note: record_line_callback is left as noop_record_line until
20790 we see DW_LNE_end_sequence. */
924c2928
DE
20791 }
20792}
20793
f3f5162e 20794/* Subroutine of dwarf_decode_lines to simplify it.
d9b3de22
DE
20795 Process the line number information in LH.
20796 If DECODE_FOR_PST_P is non-zero, all we do is process the line number
20797 program in order to set included_p for every referenced header. */
debd256d 20798
c906108c 20799static void
43f3e411
DE
20800dwarf_decode_lines_1 (struct line_header *lh, struct dwarf2_cu *cu,
20801 const int decode_for_pst_p, CORE_ADDR lowpc)
c906108c 20802{
d521ce57
TT
20803 const gdb_byte *line_ptr, *extended_end;
20804 const gdb_byte *line_end;
a8c50c1f 20805 unsigned int bytes_read, extended_len;
699ca60a 20806 unsigned char op_code, extended_op;
e142c38c 20807 CORE_ADDR baseaddr;
518817b3 20808 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
f3f5162e 20809 bfd *abfd = objfile->obfd;
fbf65064 20810 struct gdbarch *gdbarch = get_objfile_arch (objfile);
6f77053d
PA
20811 /* True if we're recording line info (as opposed to building partial
20812 symtabs and just interested in finding include files mentioned by
20813 the line number program). */
20814 bool record_lines_p = !decode_for_pst_p;
e142c38c
DJ
20815
20816 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 20817
debd256d
JB
20818 line_ptr = lh->statement_program_start;
20819 line_end = lh->statement_program_end;
c906108c
SS
20820
20821 /* Read the statement sequences until there's nothing left. */
20822 while (line_ptr < line_end)
20823 {
6f77053d
PA
20824 /* The DWARF line number program state machine. Reset the state
20825 machine at the start of each sequence. */
20826 lnp_state_machine state_machine (gdbarch, lh, record_lines_p);
20827 bool end_sequence = false;
d9b3de22 20828
8c43009f 20829 if (record_lines_p)
c906108c 20830 {
8c43009f
PA
20831 /* Start a subfile for the current file of the state
20832 machine. */
20833 const file_entry *fe = state_machine.current_file ();
20834
20835 if (fe != NULL)
20836 dwarf2_start_subfile (fe->name, fe->include_dir (lh));
c906108c
SS
20837 }
20838
a738430d 20839 /* Decode the table. */
d9b3de22 20840 while (line_ptr < line_end && !end_sequence)
c906108c
SS
20841 {
20842 op_code = read_1_byte (abfd, line_ptr);
20843 line_ptr += 1;
9aa1fe7e 20844
debd256d 20845 if (op_code >= lh->opcode_base)
6e70227d 20846 {
8e07a239 20847 /* Special opcode. */
6f77053d 20848 state_machine.handle_special_opcode (op_code);
9aa1fe7e
GK
20849 }
20850 else switch (op_code)
c906108c
SS
20851 {
20852 case DW_LNS_extended_op:
3e43a32a
MS
20853 extended_len = read_unsigned_leb128 (abfd, line_ptr,
20854 &bytes_read);
473b7be6 20855 line_ptr += bytes_read;
a8c50c1f 20856 extended_end = line_ptr + extended_len;
c906108c
SS
20857 extended_op = read_1_byte (abfd, line_ptr);
20858 line_ptr += 1;
20859 switch (extended_op)
20860 {
20861 case DW_LNE_end_sequence:
6f77053d
PA
20862 state_machine.handle_end_sequence ();
20863 end_sequence = true;
c906108c
SS
20864 break;
20865 case DW_LNE_set_address:
d9b3de22
DE
20866 {
20867 CORE_ADDR address
20868 = read_address (abfd, line_ptr, cu, &bytes_read);
d9b3de22 20869 line_ptr += bytes_read;
6f77053d
PA
20870
20871 state_machine.check_line_address (cu, line_ptr,
20872 lowpc, address);
20873 state_machine.handle_set_address (baseaddr, address);
d9b3de22 20874 }
c906108c
SS
20875 break;
20876 case DW_LNE_define_file:
debd256d 20877 {
d521ce57 20878 const char *cur_file;
ecfb656c
PA
20879 unsigned int mod_time, length;
20880 dir_index dindex;
6e70227d 20881
3e43a32a
MS
20882 cur_file = read_direct_string (abfd, line_ptr,
20883 &bytes_read);
debd256d 20884 line_ptr += bytes_read;
ecfb656c 20885 dindex = (dir_index)
debd256d
JB
20886 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20887 line_ptr += bytes_read;
20888 mod_time =
20889 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20890 line_ptr += bytes_read;
20891 length =
20892 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20893 line_ptr += bytes_read;
ecfb656c 20894 lh->add_file_name (cur_file, dindex, mod_time, length);
debd256d 20895 }
c906108c 20896 break;
d0c6ba3d 20897 case DW_LNE_set_discriminator:
6f77053d
PA
20898 {
20899 /* The discriminator is not interesting to the
20900 debugger; just ignore it. We still need to
20901 check its value though:
20902 if there are consecutive entries for the same
20903 (non-prologue) line we want to coalesce them.
20904 PR 17276. */
20905 unsigned int discr
20906 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20907 line_ptr += bytes_read;
20908
20909 state_machine.handle_set_discriminator (discr);
20910 }
d0c6ba3d 20911 break;
c906108c 20912 default:
4d3c2250 20913 complaint (&symfile_complaints,
e2e0b3e5 20914 _("mangled .debug_line section"));
debd256d 20915 return;
c906108c 20916 }
a8c50c1f
DJ
20917 /* Make sure that we parsed the extended op correctly. If e.g.
20918 we expected a different address size than the producer used,
20919 we may have read the wrong number of bytes. */
20920 if (line_ptr != extended_end)
20921 {
20922 complaint (&symfile_complaints,
20923 _("mangled .debug_line section"));
20924 return;
20925 }
c906108c
SS
20926 break;
20927 case DW_LNS_copy:
6f77053d 20928 state_machine.handle_copy ();
c906108c
SS
20929 break;
20930 case DW_LNS_advance_pc:
2dc7f7b3
TT
20931 {
20932 CORE_ADDR adjust
20933 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
2dc7f7b3 20934 line_ptr += bytes_read;
6f77053d
PA
20935
20936 state_machine.handle_advance_pc (adjust);
2dc7f7b3 20937 }
c906108c
SS
20938 break;
20939 case DW_LNS_advance_line:
a05a36a5
DE
20940 {
20941 int line_delta
20942 = read_signed_leb128 (abfd, line_ptr, &bytes_read);
a05a36a5 20943 line_ptr += bytes_read;
6f77053d
PA
20944
20945 state_machine.handle_advance_line (line_delta);
a05a36a5 20946 }
c906108c
SS
20947 break;
20948 case DW_LNS_set_file:
d9b3de22 20949 {
6f77053d 20950 file_name_index file
ecfb656c
PA
20951 = (file_name_index) read_unsigned_leb128 (abfd, line_ptr,
20952 &bytes_read);
d9b3de22 20953 line_ptr += bytes_read;
8c43009f 20954
6f77053d 20955 state_machine.handle_set_file (file);
d9b3de22 20956 }
c906108c
SS
20957 break;
20958 case DW_LNS_set_column:
0ad93d4f 20959 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
c906108c
SS
20960 line_ptr += bytes_read;
20961 break;
20962 case DW_LNS_negate_stmt:
6f77053d 20963 state_machine.handle_negate_stmt ();
c906108c
SS
20964 break;
20965 case DW_LNS_set_basic_block:
c906108c 20966 break;
c2c6d25f
JM
20967 /* Add to the address register of the state machine the
20968 address increment value corresponding to special opcode
a738430d
MK
20969 255. I.e., this value is scaled by the minimum
20970 instruction length since special opcode 255 would have
b021a221 20971 scaled the increment. */
c906108c 20972 case DW_LNS_const_add_pc:
6f77053d 20973 state_machine.handle_const_add_pc ();
c906108c
SS
20974 break;
20975 case DW_LNS_fixed_advance_pc:
3e29f34a 20976 {
6f77053d 20977 CORE_ADDR addr_adj = read_2_bytes (abfd, line_ptr);
3e29f34a 20978 line_ptr += 2;
6f77053d
PA
20979
20980 state_machine.handle_fixed_advance_pc (addr_adj);
3e29f34a 20981 }
c906108c 20982 break;
9aa1fe7e 20983 default:
a738430d
MK
20984 {
20985 /* Unknown standard opcode, ignore it. */
9aa1fe7e 20986 int i;
a738430d 20987
debd256d 20988 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
9aa1fe7e
GK
20989 {
20990 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20991 line_ptr += bytes_read;
20992 }
20993 }
c906108c
SS
20994 }
20995 }
d9b3de22
DE
20996
20997 if (!end_sequence)
20998 dwarf2_debug_line_missing_end_sequence_complaint ();
20999
21000 /* We got a DW_LNE_end_sequence (or we ran off the end of the buffer,
21001 in which case we still finish recording the last line). */
6f77053d 21002 state_machine.record_line (true);
c906108c 21003 }
f3f5162e
DE
21004}
21005
21006/* Decode the Line Number Program (LNP) for the given line_header
21007 structure and CU. The actual information extracted and the type
21008 of structures created from the LNP depends on the value of PST.
21009
21010 1. If PST is NULL, then this procedure uses the data from the program
21011 to create all necessary symbol tables, and their linetables.
21012
21013 2. If PST is not NULL, this procedure reads the program to determine
21014 the list of files included by the unit represented by PST, and
21015 builds all the associated partial symbol tables.
21016
21017 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
21018 It is used for relative paths in the line table.
21019 NOTE: When processing partial symtabs (pst != NULL),
21020 comp_dir == pst->dirname.
21021
21022 NOTE: It is important that psymtabs have the same file name (via strcmp)
21023 as the corresponding symtab. Since COMP_DIR is not used in the name of the
21024 symtab we don't use it in the name of the psymtabs we create.
21025 E.g. expand_line_sal requires this when finding psymtabs to expand.
c3b7b696
YQ
21026 A good testcase for this is mb-inline.exp.
21027
527f3840
JK
21028 LOWPC is the lowest address in CU (or 0 if not known).
21029
21030 Boolean DECODE_MAPPING specifies we need to fully decode .debug_line
21031 for its PC<->lines mapping information. Otherwise only the filename
21032 table is read in. */
f3f5162e
DE
21033
21034static void
21035dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
c3b7b696 21036 struct dwarf2_cu *cu, struct partial_symtab *pst,
527f3840 21037 CORE_ADDR lowpc, int decode_mapping)
f3f5162e 21038{
518817b3 21039 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
f3f5162e 21040 const int decode_for_pst_p = (pst != NULL);
f3f5162e 21041
527f3840
JK
21042 if (decode_mapping)
21043 dwarf_decode_lines_1 (lh, cu, decode_for_pst_p, lowpc);
aaa75496
JB
21044
21045 if (decode_for_pst_p)
21046 {
21047 int file_index;
21048
21049 /* Now that we're done scanning the Line Header Program, we can
21050 create the psymtab of each included file. */
fff8551c 21051 for (file_index = 0; file_index < lh->file_names.size (); file_index++)
aaa75496
JB
21052 if (lh->file_names[file_index].included_p == 1)
21053 {
c89b44cd 21054 gdb::unique_xmalloc_ptr<char> name_holder;
d521ce57 21055 const char *include_name =
c89b44cd
TT
21056 psymtab_include_file_name (lh, file_index, pst, comp_dir,
21057 &name_holder);
c6da4cef 21058 if (include_name != NULL)
aaa75496
JB
21059 dwarf2_create_include_psymtab (include_name, pst, objfile);
21060 }
21061 }
cb1df416
DJ
21062 else
21063 {
21064 /* Make sure a symtab is created for every file, even files
21065 which contain only variables (i.e. no code with associated
21066 line numbers). */
43f3e411 21067 struct compunit_symtab *cust = buildsym_compunit_symtab ();
cb1df416 21068 int i;
cb1df416 21069
fff8551c 21070 for (i = 0; i < lh->file_names.size (); i++)
cb1df416 21071 {
8c43009f 21072 file_entry &fe = lh->file_names[i];
9a619af0 21073
8c43009f 21074 dwarf2_start_subfile (fe.name, fe.include_dir (lh));
cb1df416 21075
cb1df416 21076 if (current_subfile->symtab == NULL)
43f3e411
DE
21077 {
21078 current_subfile->symtab
21079 = allocate_symtab (cust, current_subfile->name);
21080 }
8c43009f 21081 fe.symtab = current_subfile->symtab;
cb1df416
DJ
21082 }
21083 }
c906108c
SS
21084}
21085
21086/* Start a subfile for DWARF. FILENAME is the name of the file and
21087 DIRNAME the name of the source directory which contains FILENAME
4d663531 21088 or NULL if not known.
c906108c
SS
21089 This routine tries to keep line numbers from identical absolute and
21090 relative file names in a common subfile.
21091
21092 Using the `list' example from the GDB testsuite, which resides in
21093 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
21094 of /srcdir/list0.c yields the following debugging information for list0.c:
21095
c5aa993b 21096 DW_AT_name: /srcdir/list0.c
4d663531 21097 DW_AT_comp_dir: /compdir
357e46e7 21098 files.files[0].name: list0.h
c5aa993b 21099 files.files[0].dir: /srcdir
357e46e7 21100 files.files[1].name: list0.c
c5aa993b 21101 files.files[1].dir: /srcdir
c906108c
SS
21102
21103 The line number information for list0.c has to end up in a single
4f1520fb
FR
21104 subfile, so that `break /srcdir/list0.c:1' works as expected.
21105 start_subfile will ensure that this happens provided that we pass the
21106 concatenation of files.files[1].dir and files.files[1].name as the
21107 subfile's name. */
c906108c
SS
21108
21109static void
4d663531 21110dwarf2_start_subfile (const char *filename, const char *dirname)
c906108c 21111{
d521ce57 21112 char *copy = NULL;
4f1520fb 21113
4d663531 21114 /* In order not to lose the line information directory,
4f1520fb
FR
21115 we concatenate it to the filename when it makes sense.
21116 Note that the Dwarf3 standard says (speaking of filenames in line
21117 information): ``The directory index is ignored for file names
21118 that represent full path names''. Thus ignoring dirname in the
21119 `else' branch below isn't an issue. */
c906108c 21120
d5166ae1 21121 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
d521ce57
TT
21122 {
21123 copy = concat (dirname, SLASH_STRING, filename, (char *)NULL);
21124 filename = copy;
21125 }
c906108c 21126
4d663531 21127 start_subfile (filename);
4f1520fb 21128
d521ce57
TT
21129 if (copy != NULL)
21130 xfree (copy);
c906108c
SS
21131}
21132
f4dc4d17
DE
21133/* Start a symtab for DWARF.
21134 NAME, COMP_DIR, LOW_PC are passed to start_symtab. */
21135
43f3e411 21136static struct compunit_symtab *
f4dc4d17 21137dwarf2_start_symtab (struct dwarf2_cu *cu,
15d034d0 21138 const char *name, const char *comp_dir, CORE_ADDR low_pc)
f4dc4d17 21139{
43f3e411 21140 struct compunit_symtab *cust
518817b3
SM
21141 = start_symtab (cu->per_cu->dwarf2_per_objfile->objfile, name, comp_dir,
21142 low_pc, cu->language);
43f3e411 21143
f4dc4d17
DE
21144 record_debugformat ("DWARF 2");
21145 record_producer (cu->producer);
21146
21147 /* We assume that we're processing GCC output. */
21148 processing_gcc_compilation = 2;
21149
4d4ec4e5 21150 cu->processing_has_namespace_info = 0;
43f3e411
DE
21151
21152 return cust;
f4dc4d17
DE
21153}
21154
4c2df51b
DJ
21155static void
21156var_decode_location (struct attribute *attr, struct symbol *sym,
e7c27a73 21157 struct dwarf2_cu *cu)
4c2df51b 21158{
518817b3 21159 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
e7c27a73
DJ
21160 struct comp_unit_head *cu_header = &cu->header;
21161
4c2df51b
DJ
21162 /* NOTE drow/2003-01-30: There used to be a comment and some special
21163 code here to turn a symbol with DW_AT_external and a
21164 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
21165 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
21166 with some versions of binutils) where shared libraries could have
21167 relocations against symbols in their debug information - the
21168 minimal symbol would have the right address, but the debug info
21169 would not. It's no longer necessary, because we will explicitly
21170 apply relocations when we read in the debug information now. */
21171
21172 /* A DW_AT_location attribute with no contents indicates that a
21173 variable has been optimized away. */
21174 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
21175 {
f1e6e072 21176 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
4c2df51b
DJ
21177 return;
21178 }
21179
21180 /* Handle one degenerate form of location expression specially, to
21181 preserve GDB's previous behavior when section offsets are
3019eac3
DE
21182 specified. If this is just a DW_OP_addr or DW_OP_GNU_addr_index
21183 then mark this symbol as LOC_STATIC. */
4c2df51b
DJ
21184
21185 if (attr_form_is_block (attr)
3019eac3
DE
21186 && ((DW_BLOCK (attr)->data[0] == DW_OP_addr
21187 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size)
21188 || (DW_BLOCK (attr)->data[0] == DW_OP_GNU_addr_index
21189 && (DW_BLOCK (attr)->size
21190 == 1 + leb128_size (&DW_BLOCK (attr)->data[1])))))
4c2df51b 21191 {
891d2f0b 21192 unsigned int dummy;
4c2df51b 21193
3019eac3
DE
21194 if (DW_BLOCK (attr)->data[0] == DW_OP_addr)
21195 SYMBOL_VALUE_ADDRESS (sym) =
21196 read_address (objfile->obfd, DW_BLOCK (attr)->data + 1, cu, &dummy);
21197 else
21198 SYMBOL_VALUE_ADDRESS (sym) =
21199 read_addr_index_from_leb128 (cu, DW_BLOCK (attr)->data + 1, &dummy);
f1e6e072 21200 SYMBOL_ACLASS_INDEX (sym) = LOC_STATIC;
4c2df51b
DJ
21201 fixup_symbol_section (sym, objfile);
21202 SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (objfile->section_offsets,
21203 SYMBOL_SECTION (sym));
4c2df51b
DJ
21204 return;
21205 }
21206
21207 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
21208 expression evaluator, and use LOC_COMPUTED only when necessary
21209 (i.e. when the value of a register or memory location is
21210 referenced, or a thread-local block, etc.). Then again, it might
21211 not be worthwhile. I'm assuming that it isn't unless performance
21212 or memory numbers show me otherwise. */
21213
f1e6e072 21214 dwarf2_symbol_mark_computed (attr, sym, cu, 0);
8be455d7 21215
f1e6e072 21216 if (SYMBOL_COMPUTED_OPS (sym)->location_has_loclist)
8be455d7 21217 cu->has_loclist = 1;
4c2df51b
DJ
21218}
21219
c906108c
SS
21220/* Given a pointer to a DWARF information entry, figure out if we need
21221 to make a symbol table entry for it, and if so, create a new entry
21222 and return a pointer to it.
21223 If TYPE is NULL, determine symbol type from the die, otherwise
34eaf542
TT
21224 used the passed type.
21225 If SPACE is not NULL, use it to hold the new symbol. If it is
21226 NULL, allocate a new symbol on the objfile's obstack. */
c906108c
SS
21227
21228static struct symbol *
5e2db402
TT
21229new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
21230 struct symbol *space)
c906108c 21231{
518817b3
SM
21232 struct dwarf2_per_objfile *dwarf2_per_objfile
21233 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 21234 struct objfile *objfile = dwarf2_per_objfile->objfile;
3e29f34a 21235 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c 21236 struct symbol *sym = NULL;
15d034d0 21237 const char *name;
c906108c
SS
21238 struct attribute *attr = NULL;
21239 struct attribute *attr2 = NULL;
e142c38c 21240 CORE_ADDR baseaddr;
e37fd15a
SW
21241 struct pending **list_to_add = NULL;
21242
edb3359d 21243 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
e142c38c
DJ
21244
21245 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 21246
94af9270 21247 name = dwarf2_name (die, cu);
c906108c
SS
21248 if (name)
21249 {
94af9270 21250 const char *linkagename;
34eaf542 21251 int suppress_add = 0;
94af9270 21252
34eaf542
TT
21253 if (space)
21254 sym = space;
21255 else
e623cf5d 21256 sym = allocate_symbol (objfile);
c906108c 21257 OBJSTAT (objfile, n_syms++);
2de7ced7
DJ
21258
21259 /* Cache this symbol's name and the name's demangled form (if any). */
f85f34ed 21260 SYMBOL_SET_LANGUAGE (sym, cu->language, &objfile->objfile_obstack);
94af9270
KS
21261 linkagename = dwarf2_physname (name, die, cu);
21262 SYMBOL_SET_NAMES (sym, linkagename, strlen (linkagename), 0, objfile);
c906108c 21263
f55ee35c
JK
21264 /* Fortran does not have mangling standard and the mangling does differ
21265 between gfortran, iFort etc. */
21266 if (cu->language == language_fortran
b250c185 21267 && symbol_get_demangled_name (&(sym->ginfo)) == NULL)
29df156d 21268 symbol_set_demangled_name (&(sym->ginfo),
cfc594ee 21269 dwarf2_full_name (name, die, cu),
29df156d 21270 NULL);
f55ee35c 21271
c906108c 21272 /* Default assumptions.
c5aa993b 21273 Use the passed type or decode it from the die. */
176620f1 21274 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
f1e6e072 21275 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
c906108c
SS
21276 if (type != NULL)
21277 SYMBOL_TYPE (sym) = type;
21278 else
e7c27a73 21279 SYMBOL_TYPE (sym) = die_type (die, cu);
edb3359d
DJ
21280 attr = dwarf2_attr (die,
21281 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
21282 cu);
c906108c
SS
21283 if (attr)
21284 {
21285 SYMBOL_LINE (sym) = DW_UNSND (attr);
21286 }
cb1df416 21287
edb3359d
DJ
21288 attr = dwarf2_attr (die,
21289 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
21290 cu);
cb1df416
DJ
21291 if (attr)
21292 {
ecfb656c 21293 file_name_index file_index = (file_name_index) DW_UNSND (attr);
8c43009f 21294 struct file_entry *fe;
9a619af0 21295
ecfb656c
PA
21296 if (cu->line_header != NULL)
21297 fe = cu->line_header->file_name_at (file_index);
8c43009f
PA
21298 else
21299 fe = NULL;
21300
21301 if (fe == NULL)
cb1df416
DJ
21302 complaint (&symfile_complaints,
21303 _("file index out of range"));
8c43009f
PA
21304 else
21305 symbol_set_symtab (sym, fe->symtab);
cb1df416
DJ
21306 }
21307
c906108c
SS
21308 switch (die->tag)
21309 {
21310 case DW_TAG_label:
e142c38c 21311 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
c906108c 21312 if (attr)
3e29f34a
MR
21313 {
21314 CORE_ADDR addr;
21315
21316 addr = attr_value_as_address (attr);
21317 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr + baseaddr);
21318 SYMBOL_VALUE_ADDRESS (sym) = addr;
21319 }
0f5238ed
TT
21320 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
21321 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
f1e6e072 21322 SYMBOL_ACLASS_INDEX (sym) = LOC_LABEL;
0f5238ed 21323 add_symbol_to_list (sym, cu->list_in_scope);
c906108c
SS
21324 break;
21325 case DW_TAG_subprogram:
21326 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
21327 finish_block. */
f1e6e072 21328 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
e142c38c 21329 attr2 = dwarf2_attr (die, DW_AT_external, cu);
2cfa0c8d
JB
21330 if ((attr2 && (DW_UNSND (attr2) != 0))
21331 || cu->language == language_ada)
c906108c 21332 {
2cfa0c8d
JB
21333 /* Subprograms marked external are stored as a global symbol.
21334 Ada subprograms, whether marked external or not, are always
21335 stored as a global symbol, because we want to be able to
21336 access them globally. For instance, we want to be able
21337 to break on a nested subprogram without having to
21338 specify the context. */
e37fd15a 21339 list_to_add = &global_symbols;
c906108c
SS
21340 }
21341 else
21342 {
e37fd15a 21343 list_to_add = cu->list_in_scope;
c906108c
SS
21344 }
21345 break;
edb3359d
DJ
21346 case DW_TAG_inlined_subroutine:
21347 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
21348 finish_block. */
f1e6e072 21349 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
edb3359d 21350 SYMBOL_INLINED (sym) = 1;
481860b3 21351 list_to_add = cu->list_in_scope;
edb3359d 21352 break;
34eaf542
TT
21353 case DW_TAG_template_value_param:
21354 suppress_add = 1;
21355 /* Fall through. */
72929c62 21356 case DW_TAG_constant:
c906108c 21357 case DW_TAG_variable:
254e6b9e 21358 case DW_TAG_member:
0963b4bd
MS
21359 /* Compilation with minimal debug info may result in
21360 variables with missing type entries. Change the
21361 misleading `void' type to something sensible. */
c906108c 21362 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
46a4882b 21363 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_int;
64c50499 21364
e142c38c 21365 attr = dwarf2_attr (die, DW_AT_const_value, cu);
254e6b9e
DE
21366 /* In the case of DW_TAG_member, we should only be called for
21367 static const members. */
21368 if (die->tag == DW_TAG_member)
21369 {
3863f96c
DE
21370 /* dwarf2_add_field uses die_is_declaration,
21371 so we do the same. */
254e6b9e
DE
21372 gdb_assert (die_is_declaration (die, cu));
21373 gdb_assert (attr);
21374 }
c906108c
SS
21375 if (attr)
21376 {
e7c27a73 21377 dwarf2_const_value (attr, sym, cu);
e142c38c 21378 attr2 = dwarf2_attr (die, DW_AT_external, cu);
e37fd15a 21379 if (!suppress_add)
34eaf542
TT
21380 {
21381 if (attr2 && (DW_UNSND (attr2) != 0))
e37fd15a 21382 list_to_add = &global_symbols;
34eaf542 21383 else
e37fd15a 21384 list_to_add = cu->list_in_scope;
34eaf542 21385 }
c906108c
SS
21386 break;
21387 }
e142c38c 21388 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
21389 if (attr)
21390 {
e7c27a73 21391 var_decode_location (attr, sym, cu);
e142c38c 21392 attr2 = dwarf2_attr (die, DW_AT_external, cu);
4357ac6c
TT
21393
21394 /* Fortran explicitly imports any global symbols to the local
21395 scope by DW_TAG_common_block. */
21396 if (cu->language == language_fortran && die->parent
21397 && die->parent->tag == DW_TAG_common_block)
21398 attr2 = NULL;
21399
caac4577
JG
21400 if (SYMBOL_CLASS (sym) == LOC_STATIC
21401 && SYMBOL_VALUE_ADDRESS (sym) == 0
21402 && !dwarf2_per_objfile->has_section_at_zero)
21403 {
21404 /* When a static variable is eliminated by the linker,
21405 the corresponding debug information is not stripped
21406 out, but the variable address is set to null;
21407 do not add such variables into symbol table. */
21408 }
21409 else if (attr2 && (DW_UNSND (attr2) != 0))
1c809c68 21410 {
f55ee35c
JK
21411 /* Workaround gfortran PR debug/40040 - it uses
21412 DW_AT_location for variables in -fPIC libraries which may
21413 get overriden by other libraries/executable and get
21414 a different address. Resolve it by the minimal symbol
21415 which may come from inferior's executable using copy
21416 relocation. Make this workaround only for gfortran as for
21417 other compilers GDB cannot guess the minimal symbol
21418 Fortran mangling kind. */
21419 if (cu->language == language_fortran && die->parent
21420 && die->parent->tag == DW_TAG_module
21421 && cu->producer
28586665 21422 && startswith (cu->producer, "GNU Fortran"))
f1e6e072 21423 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
f55ee35c 21424
1c809c68
TT
21425 /* A variable with DW_AT_external is never static,
21426 but it may be block-scoped. */
21427 list_to_add = (cu->list_in_scope == &file_symbols
21428 ? &global_symbols : cu->list_in_scope);
1c809c68 21429 }
c906108c 21430 else
e37fd15a 21431 list_to_add = cu->list_in_scope;
c906108c
SS
21432 }
21433 else
21434 {
21435 /* We do not know the address of this symbol.
c5aa993b
JM
21436 If it is an external symbol and we have type information
21437 for it, enter the symbol as a LOC_UNRESOLVED symbol.
21438 The address of the variable will then be determined from
21439 the minimal symbol table whenever the variable is
21440 referenced. */
e142c38c 21441 attr2 = dwarf2_attr (die, DW_AT_external, cu);
0971de02
TT
21442
21443 /* Fortran explicitly imports any global symbols to the local
21444 scope by DW_TAG_common_block. */
21445 if (cu->language == language_fortran && die->parent
21446 && die->parent->tag == DW_TAG_common_block)
21447 {
21448 /* SYMBOL_CLASS doesn't matter here because
21449 read_common_block is going to reset it. */
21450 if (!suppress_add)
21451 list_to_add = cu->list_in_scope;
21452 }
21453 else if (attr2 && (DW_UNSND (attr2) != 0)
21454 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
c906108c 21455 {
0fe7935b
DJ
21456 /* A variable with DW_AT_external is never static, but it
21457 may be block-scoped. */
21458 list_to_add = (cu->list_in_scope == &file_symbols
21459 ? &global_symbols : cu->list_in_scope);
21460
f1e6e072 21461 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
c906108c 21462 }
442ddf59
JK
21463 else if (!die_is_declaration (die, cu))
21464 {
21465 /* Use the default LOC_OPTIMIZED_OUT class. */
21466 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
e37fd15a
SW
21467 if (!suppress_add)
21468 list_to_add = cu->list_in_scope;
442ddf59 21469 }
c906108c
SS
21470 }
21471 break;
21472 case DW_TAG_formal_parameter:
edb3359d
DJ
21473 /* If we are inside a function, mark this as an argument. If
21474 not, we might be looking at an argument to an inlined function
21475 when we do not have enough information to show inlined frames;
21476 pretend it's a local variable in that case so that the user can
21477 still see it. */
21478 if (context_stack_depth > 0
21479 && context_stack[context_stack_depth - 1].name != NULL)
21480 SYMBOL_IS_ARGUMENT (sym) = 1;
e142c38c 21481 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
21482 if (attr)
21483 {
e7c27a73 21484 var_decode_location (attr, sym, cu);
c906108c 21485 }
e142c38c 21486 attr = dwarf2_attr (die, DW_AT_const_value, cu);
c906108c
SS
21487 if (attr)
21488 {
e7c27a73 21489 dwarf2_const_value (attr, sym, cu);
c906108c 21490 }
f346a30d 21491
e37fd15a 21492 list_to_add = cu->list_in_scope;
c906108c
SS
21493 break;
21494 case DW_TAG_unspecified_parameters:
21495 /* From varargs functions; gdb doesn't seem to have any
21496 interest in this information, so just ignore it for now.
21497 (FIXME?) */
21498 break;
34eaf542
TT
21499 case DW_TAG_template_type_param:
21500 suppress_add = 1;
21501 /* Fall through. */
c906108c 21502 case DW_TAG_class_type:
680b30c7 21503 case DW_TAG_interface_type:
c906108c
SS
21504 case DW_TAG_structure_type:
21505 case DW_TAG_union_type:
72019c9c 21506 case DW_TAG_set_type:
c906108c 21507 case DW_TAG_enumeration_type:
f1e6e072 21508 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
176620f1 21509 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
c906108c 21510
63d06c5c 21511 {
9c37b5ae 21512 /* NOTE: carlton/2003-11-10: C++ class symbols shouldn't
63d06c5c
DC
21513 really ever be static objects: otherwise, if you try
21514 to, say, break of a class's method and you're in a file
21515 which doesn't mention that class, it won't work unless
21516 the check for all static symbols in lookup_symbol_aux
21517 saves you. See the OtherFileClass tests in
21518 gdb.c++/namespace.exp. */
21519
e37fd15a 21520 if (!suppress_add)
34eaf542 21521 {
34eaf542 21522 list_to_add = (cu->list_in_scope == &file_symbols
9c37b5ae 21523 && cu->language == language_cplus
34eaf542 21524 ? &global_symbols : cu->list_in_scope);
63d06c5c 21525
64382290 21526 /* The semantics of C++ state that "struct foo {
9c37b5ae 21527 ... }" also defines a typedef for "foo". */
64382290 21528 if (cu->language == language_cplus
45280282 21529 || cu->language == language_ada
c44af4eb
TT
21530 || cu->language == language_d
21531 || cu->language == language_rust)
64382290
TT
21532 {
21533 /* The symbol's name is already allocated along
21534 with this objfile, so we don't need to
21535 duplicate it for the type. */
21536 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
21537 TYPE_NAME (SYMBOL_TYPE (sym)) = SYMBOL_SEARCH_NAME (sym);
21538 }
63d06c5c
DC
21539 }
21540 }
c906108c
SS
21541 break;
21542 case DW_TAG_typedef:
f1e6e072 21543 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
63d06c5c 21544 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 21545 list_to_add = cu->list_in_scope;
63d06c5c 21546 break;
c906108c 21547 case DW_TAG_base_type:
a02abb62 21548 case DW_TAG_subrange_type:
f1e6e072 21549 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
176620f1 21550 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 21551 list_to_add = cu->list_in_scope;
c906108c
SS
21552 break;
21553 case DW_TAG_enumerator:
e142c38c 21554 attr = dwarf2_attr (die, DW_AT_const_value, cu);
c906108c
SS
21555 if (attr)
21556 {
e7c27a73 21557 dwarf2_const_value (attr, sym, cu);
c906108c 21558 }
63d06c5c
DC
21559 {
21560 /* NOTE: carlton/2003-11-10: See comment above in the
21561 DW_TAG_class_type, etc. block. */
21562
e142c38c 21563 list_to_add = (cu->list_in_scope == &file_symbols
9c37b5ae 21564 && cu->language == language_cplus
e142c38c 21565 ? &global_symbols : cu->list_in_scope);
63d06c5c 21566 }
c906108c 21567 break;
74921315 21568 case DW_TAG_imported_declaration:
5c4e30ca 21569 case DW_TAG_namespace:
f1e6e072 21570 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
e37fd15a 21571 list_to_add = &global_symbols;
5c4e30ca 21572 break;
530e8392
KB
21573 case DW_TAG_module:
21574 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21575 SYMBOL_DOMAIN (sym) = MODULE_DOMAIN;
21576 list_to_add = &global_symbols;
21577 break;
4357ac6c 21578 case DW_TAG_common_block:
f1e6e072 21579 SYMBOL_ACLASS_INDEX (sym) = LOC_COMMON_BLOCK;
4357ac6c
TT
21580 SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
21581 add_symbol_to_list (sym, cu->list_in_scope);
21582 break;
c906108c
SS
21583 default:
21584 /* Not a tag we recognize. Hopefully we aren't processing
21585 trash data, but since we must specifically ignore things
21586 we don't recognize, there is nothing else we should do at
0963b4bd 21587 this point. */
e2e0b3e5 21588 complaint (&symfile_complaints, _("unsupported tag: '%s'"),
4d3c2250 21589 dwarf_tag_name (die->tag));
c906108c
SS
21590 break;
21591 }
df8a16a1 21592
e37fd15a
SW
21593 if (suppress_add)
21594 {
21595 sym->hash_next = objfile->template_symbols;
21596 objfile->template_symbols = sym;
21597 list_to_add = NULL;
21598 }
21599
21600 if (list_to_add != NULL)
21601 add_symbol_to_list (sym, list_to_add);
21602
df8a16a1
DJ
21603 /* For the benefit of old versions of GCC, check for anonymous
21604 namespaces based on the demangled name. */
4d4ec4e5 21605 if (!cu->processing_has_namespace_info
94af9270 21606 && cu->language == language_cplus)
a10964d1 21607 cp_scan_for_anonymous_namespaces (sym, objfile);
c906108c
SS
21608 }
21609 return (sym);
21610}
21611
98bfdba5
PA
21612/* Given an attr with a DW_FORM_dataN value in host byte order,
21613 zero-extend it as appropriate for the symbol's type. The DWARF
21614 standard (v4) is not entirely clear about the meaning of using
21615 DW_FORM_dataN for a constant with a signed type, where the type is
21616 wider than the data. The conclusion of a discussion on the DWARF
21617 list was that this is unspecified. We choose to always zero-extend
21618 because that is the interpretation long in use by GCC. */
c906108c 21619
98bfdba5 21620static gdb_byte *
ff39bb5e 21621dwarf2_const_value_data (const struct attribute *attr, struct obstack *obstack,
12df843f 21622 struct dwarf2_cu *cu, LONGEST *value, int bits)
c906108c 21623{
518817b3 21624 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
e17a4113
UW
21625 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
21626 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
98bfdba5
PA
21627 LONGEST l = DW_UNSND (attr);
21628
21629 if (bits < sizeof (*value) * 8)
21630 {
21631 l &= ((LONGEST) 1 << bits) - 1;
21632 *value = l;
21633 }
21634 else if (bits == sizeof (*value) * 8)
21635 *value = l;
21636 else
21637 {
224c3ddb 21638 gdb_byte *bytes = (gdb_byte *) obstack_alloc (obstack, bits / 8);
98bfdba5
PA
21639 store_unsigned_integer (bytes, bits / 8, byte_order, l);
21640 return bytes;
21641 }
21642
21643 return NULL;
21644}
21645
21646/* Read a constant value from an attribute. Either set *VALUE, or if
21647 the value does not fit in *VALUE, set *BYTES - either already
21648 allocated on the objfile obstack, or newly allocated on OBSTACK,
21649 or, set *BATON, if we translated the constant to a location
21650 expression. */
21651
21652static void
ff39bb5e 21653dwarf2_const_value_attr (const struct attribute *attr, struct type *type,
98bfdba5
PA
21654 const char *name, struct obstack *obstack,
21655 struct dwarf2_cu *cu,
d521ce57 21656 LONGEST *value, const gdb_byte **bytes,
98bfdba5
PA
21657 struct dwarf2_locexpr_baton **baton)
21658{
518817b3 21659 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
98bfdba5 21660 struct comp_unit_head *cu_header = &cu->header;
c906108c 21661 struct dwarf_block *blk;
98bfdba5
PA
21662 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
21663 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
21664
21665 *value = 0;
21666 *bytes = NULL;
21667 *baton = NULL;
c906108c
SS
21668
21669 switch (attr->form)
21670 {
21671 case DW_FORM_addr:
3019eac3 21672 case DW_FORM_GNU_addr_index:
ac56253d 21673 {
ac56253d
TT
21674 gdb_byte *data;
21675
98bfdba5
PA
21676 if (TYPE_LENGTH (type) != cu_header->addr_size)
21677 dwarf2_const_value_length_mismatch_complaint (name,
ac56253d 21678 cu_header->addr_size,
98bfdba5 21679 TYPE_LENGTH (type));
ac56253d
TT
21680 /* Symbols of this form are reasonably rare, so we just
21681 piggyback on the existing location code rather than writing
21682 a new implementation of symbol_computed_ops. */
8d749320 21683 *baton = XOBNEW (obstack, struct dwarf2_locexpr_baton);
98bfdba5
PA
21684 (*baton)->per_cu = cu->per_cu;
21685 gdb_assert ((*baton)->per_cu);
ac56253d 21686
98bfdba5 21687 (*baton)->size = 2 + cu_header->addr_size;
224c3ddb 21688 data = (gdb_byte *) obstack_alloc (obstack, (*baton)->size);
98bfdba5 21689 (*baton)->data = data;
ac56253d
TT
21690
21691 data[0] = DW_OP_addr;
21692 store_unsigned_integer (&data[1], cu_header->addr_size,
21693 byte_order, DW_ADDR (attr));
21694 data[cu_header->addr_size + 1] = DW_OP_stack_value;
ac56253d 21695 }
c906108c 21696 break;
4ac36638 21697 case DW_FORM_string:
93b5768b 21698 case DW_FORM_strp:
3019eac3 21699 case DW_FORM_GNU_str_index:
36586728 21700 case DW_FORM_GNU_strp_alt:
98bfdba5
PA
21701 /* DW_STRING is already allocated on the objfile obstack, point
21702 directly to it. */
d521ce57 21703 *bytes = (const gdb_byte *) DW_STRING (attr);
93b5768b 21704 break;
c906108c
SS
21705 case DW_FORM_block1:
21706 case DW_FORM_block2:
21707 case DW_FORM_block4:
21708 case DW_FORM_block:
2dc7f7b3 21709 case DW_FORM_exprloc:
0224619f 21710 case DW_FORM_data16:
c906108c 21711 blk = DW_BLOCK (attr);
98bfdba5
PA
21712 if (TYPE_LENGTH (type) != blk->size)
21713 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
21714 TYPE_LENGTH (type));
21715 *bytes = blk->data;
c906108c 21716 break;
2df3850c
JM
21717
21718 /* The DW_AT_const_value attributes are supposed to carry the
21719 symbol's value "represented as it would be on the target
21720 architecture." By the time we get here, it's already been
21721 converted to host endianness, so we just need to sign- or
21722 zero-extend it as appropriate. */
21723 case DW_FORM_data1:
3aef2284 21724 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 8);
2df3850c 21725 break;
c906108c 21726 case DW_FORM_data2:
3aef2284 21727 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 16);
2df3850c 21728 break;
c906108c 21729 case DW_FORM_data4:
3aef2284 21730 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 32);
2df3850c 21731 break;
c906108c 21732 case DW_FORM_data8:
3aef2284 21733 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 64);
2df3850c
JM
21734 break;
21735
c906108c 21736 case DW_FORM_sdata:
663c44ac 21737 case DW_FORM_implicit_const:
98bfdba5 21738 *value = DW_SND (attr);
2df3850c
JM
21739 break;
21740
c906108c 21741 case DW_FORM_udata:
98bfdba5 21742 *value = DW_UNSND (attr);
c906108c 21743 break;
2df3850c 21744
c906108c 21745 default:
4d3c2250 21746 complaint (&symfile_complaints,
e2e0b3e5 21747 _("unsupported const value attribute form: '%s'"),
4d3c2250 21748 dwarf_form_name (attr->form));
98bfdba5 21749 *value = 0;
c906108c
SS
21750 break;
21751 }
21752}
21753
2df3850c 21754
98bfdba5
PA
21755/* Copy constant value from an attribute to a symbol. */
21756
2df3850c 21757static void
ff39bb5e 21758dwarf2_const_value (const struct attribute *attr, struct symbol *sym,
98bfdba5 21759 struct dwarf2_cu *cu)
2df3850c 21760{
518817b3 21761 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
12df843f 21762 LONGEST value;
d521ce57 21763 const gdb_byte *bytes;
98bfdba5 21764 struct dwarf2_locexpr_baton *baton;
2df3850c 21765
98bfdba5
PA
21766 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
21767 SYMBOL_PRINT_NAME (sym),
21768 &objfile->objfile_obstack, cu,
21769 &value, &bytes, &baton);
2df3850c 21770
98bfdba5
PA
21771 if (baton != NULL)
21772 {
98bfdba5 21773 SYMBOL_LOCATION_BATON (sym) = baton;
f1e6e072 21774 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
98bfdba5
PA
21775 }
21776 else if (bytes != NULL)
21777 {
21778 SYMBOL_VALUE_BYTES (sym) = bytes;
f1e6e072 21779 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST_BYTES;
98bfdba5
PA
21780 }
21781 else
21782 {
21783 SYMBOL_VALUE (sym) = value;
f1e6e072 21784 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST;
98bfdba5 21785 }
2df3850c
JM
21786}
21787
c906108c
SS
21788/* Return the type of the die in question using its DW_AT_type attribute. */
21789
21790static struct type *
e7c27a73 21791die_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 21792{
c906108c 21793 struct attribute *type_attr;
c906108c 21794
e142c38c 21795 type_attr = dwarf2_attr (die, DW_AT_type, cu);
c906108c
SS
21796 if (!type_attr)
21797 {
518817b3 21798 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c 21799 /* A missing DW_AT_type represents a void type. */
518817b3 21800 return objfile_type (objfile)->builtin_void;
c906108c 21801 }
348e048f 21802
673bfd45 21803 return lookup_die_type (die, type_attr, cu);
c906108c
SS
21804}
21805
b4ba55a1
JB
21806/* True iff CU's producer generates GNAT Ada auxiliary information
21807 that allows to find parallel types through that information instead
21808 of having to do expensive parallel lookups by type name. */
21809
21810static int
21811need_gnat_info (struct dwarf2_cu *cu)
21812{
de4cb04a
JB
21813 /* Assume that the Ada compiler was GNAT, which always produces
21814 the auxiliary information. */
21815 return (cu->language == language_ada);
b4ba55a1
JB
21816}
21817
b4ba55a1
JB
21818/* Return the auxiliary type of the die in question using its
21819 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
21820 attribute is not present. */
21821
21822static struct type *
21823die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
21824{
b4ba55a1 21825 struct attribute *type_attr;
b4ba55a1
JB
21826
21827 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
21828 if (!type_attr)
21829 return NULL;
21830
673bfd45 21831 return lookup_die_type (die, type_attr, cu);
b4ba55a1
JB
21832}
21833
21834/* If DIE has a descriptive_type attribute, then set the TYPE's
21835 descriptive type accordingly. */
21836
21837static void
21838set_descriptive_type (struct type *type, struct die_info *die,
21839 struct dwarf2_cu *cu)
21840{
21841 struct type *descriptive_type = die_descriptive_type (die, cu);
21842
21843 if (descriptive_type)
21844 {
21845 ALLOCATE_GNAT_AUX_TYPE (type);
21846 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
21847 }
21848}
21849
c906108c
SS
21850/* Return the containing type of the die in question using its
21851 DW_AT_containing_type attribute. */
21852
21853static struct type *
e7c27a73 21854die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 21855{
c906108c 21856 struct attribute *type_attr;
518817b3 21857 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c 21858
e142c38c 21859 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
33ac96f0
JK
21860 if (!type_attr)
21861 error (_("Dwarf Error: Problem turning containing type into gdb type "
518817b3 21862 "[in module %s]"), objfile_name (objfile));
33ac96f0 21863
673bfd45 21864 return lookup_die_type (die, type_attr, cu);
c906108c
SS
21865}
21866
ac9ec31b
DE
21867/* Return an error marker type to use for the ill formed type in DIE/CU. */
21868
21869static struct type *
21870build_error_marker_type (struct dwarf2_cu *cu, struct die_info *die)
21871{
518817b3
SM
21872 struct dwarf2_per_objfile *dwarf2_per_objfile
21873 = cu->per_cu->dwarf2_per_objfile;
ac9ec31b
DE
21874 struct objfile *objfile = dwarf2_per_objfile->objfile;
21875 char *message, *saved;
21876
9d8780f0 21877 message = xstrprintf (_("<unknown type in %s, CU %s, DIE %s>"),
4262abfb 21878 objfile_name (objfile),
9d8780f0
SM
21879 sect_offset_str (cu->header.sect_off),
21880 sect_offset_str (die->sect_off));
224c3ddb
SM
21881 saved = (char *) obstack_copy0 (&objfile->objfile_obstack,
21882 message, strlen (message));
ac9ec31b
DE
21883 xfree (message);
21884
19f392bc 21885 return init_type (objfile, TYPE_CODE_ERROR, 0, saved);
ac9ec31b
DE
21886}
21887
673bfd45 21888/* Look up the type of DIE in CU using its type attribute ATTR.
ac9ec31b
DE
21889 ATTR must be one of: DW_AT_type, DW_AT_GNAT_descriptive_type,
21890 DW_AT_containing_type.
673bfd45
DE
21891 If there is no type substitute an error marker. */
21892
c906108c 21893static struct type *
ff39bb5e 21894lookup_die_type (struct die_info *die, const struct attribute *attr,
673bfd45 21895 struct dwarf2_cu *cu)
c906108c 21896{
518817b3
SM
21897 struct dwarf2_per_objfile *dwarf2_per_objfile
21898 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 21899 struct objfile *objfile = dwarf2_per_objfile->objfile;
f792889a
DJ
21900 struct type *this_type;
21901
ac9ec31b
DE
21902 gdb_assert (attr->name == DW_AT_type
21903 || attr->name == DW_AT_GNAT_descriptive_type
21904 || attr->name == DW_AT_containing_type);
21905
673bfd45
DE
21906 /* First see if we have it cached. */
21907
36586728
TT
21908 if (attr->form == DW_FORM_GNU_ref_alt)
21909 {
21910 struct dwarf2_per_cu_data *per_cu;
9c541725 21911 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
36586728 21912
ed2dc618
SM
21913 per_cu = dwarf2_find_containing_comp_unit (sect_off, 1,
21914 dwarf2_per_objfile);
9c541725 21915 this_type = get_die_type_at_offset (sect_off, per_cu);
36586728 21916 }
7771576e 21917 else if (attr_form_is_ref (attr))
673bfd45 21918 {
9c541725 21919 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
673bfd45 21920
9c541725 21921 this_type = get_die_type_at_offset (sect_off, cu->per_cu);
673bfd45 21922 }
55f1336d 21923 else if (attr->form == DW_FORM_ref_sig8)
673bfd45 21924 {
ac9ec31b 21925 ULONGEST signature = DW_SIGNATURE (attr);
673bfd45 21926
ac9ec31b 21927 return get_signatured_type (die, signature, cu);
673bfd45
DE
21928 }
21929 else
21930 {
ac9ec31b
DE
21931 complaint (&symfile_complaints,
21932 _("Dwarf Error: Bad type attribute %s in DIE"
9d8780f0
SM
21933 " at %s [in module %s]"),
21934 dwarf_attr_name (attr->name), sect_offset_str (die->sect_off),
4262abfb 21935 objfile_name (objfile));
ac9ec31b 21936 return build_error_marker_type (cu, die);
673bfd45
DE
21937 }
21938
21939 /* If not cached we need to read it in. */
21940
21941 if (this_type == NULL)
21942 {
ac9ec31b 21943 struct die_info *type_die = NULL;
673bfd45
DE
21944 struct dwarf2_cu *type_cu = cu;
21945
7771576e 21946 if (attr_form_is_ref (attr))
ac9ec31b
DE
21947 type_die = follow_die_ref (die, attr, &type_cu);
21948 if (type_die == NULL)
21949 return build_error_marker_type (cu, die);
21950 /* If we find the type now, it's probably because the type came
3019eac3
DE
21951 from an inter-CU reference and the type's CU got expanded before
21952 ours. */
ac9ec31b 21953 this_type = read_type_die (type_die, type_cu);
673bfd45
DE
21954 }
21955
21956 /* If we still don't have a type use an error marker. */
21957
21958 if (this_type == NULL)
ac9ec31b 21959 return build_error_marker_type (cu, die);
673bfd45 21960
f792889a 21961 return this_type;
c906108c
SS
21962}
21963
673bfd45
DE
21964/* Return the type in DIE, CU.
21965 Returns NULL for invalid types.
21966
02142a6c 21967 This first does a lookup in die_type_hash,
673bfd45
DE
21968 and only reads the die in if necessary.
21969
21970 NOTE: This can be called when reading in partial or full symbols. */
21971
f792889a 21972static struct type *
e7c27a73 21973read_type_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 21974{
f792889a
DJ
21975 struct type *this_type;
21976
21977 this_type = get_die_type (die, cu);
21978 if (this_type)
21979 return this_type;
21980
673bfd45
DE
21981 return read_type_die_1 (die, cu);
21982}
21983
21984/* Read the type in DIE, CU.
21985 Returns NULL for invalid types. */
21986
21987static struct type *
21988read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
21989{
21990 struct type *this_type = NULL;
21991
c906108c
SS
21992 switch (die->tag)
21993 {
21994 case DW_TAG_class_type:
680b30c7 21995 case DW_TAG_interface_type:
c906108c
SS
21996 case DW_TAG_structure_type:
21997 case DW_TAG_union_type:
f792889a 21998 this_type = read_structure_type (die, cu);
c906108c
SS
21999 break;
22000 case DW_TAG_enumeration_type:
f792889a 22001 this_type = read_enumeration_type (die, cu);
c906108c
SS
22002 break;
22003 case DW_TAG_subprogram:
22004 case DW_TAG_subroutine_type:
edb3359d 22005 case DW_TAG_inlined_subroutine:
f792889a 22006 this_type = read_subroutine_type (die, cu);
c906108c
SS
22007 break;
22008 case DW_TAG_array_type:
f792889a 22009 this_type = read_array_type (die, cu);
c906108c 22010 break;
72019c9c 22011 case DW_TAG_set_type:
f792889a 22012 this_type = read_set_type (die, cu);
72019c9c 22013 break;
c906108c 22014 case DW_TAG_pointer_type:
f792889a 22015 this_type = read_tag_pointer_type (die, cu);
c906108c
SS
22016 break;
22017 case DW_TAG_ptr_to_member_type:
f792889a 22018 this_type = read_tag_ptr_to_member_type (die, cu);
c906108c
SS
22019 break;
22020 case DW_TAG_reference_type:
4297a3f0
AV
22021 this_type = read_tag_reference_type (die, cu, TYPE_CODE_REF);
22022 break;
22023 case DW_TAG_rvalue_reference_type:
22024 this_type = read_tag_reference_type (die, cu, TYPE_CODE_RVALUE_REF);
c906108c
SS
22025 break;
22026 case DW_TAG_const_type:
f792889a 22027 this_type = read_tag_const_type (die, cu);
c906108c
SS
22028 break;
22029 case DW_TAG_volatile_type:
f792889a 22030 this_type = read_tag_volatile_type (die, cu);
c906108c 22031 break;
06d66ee9
TT
22032 case DW_TAG_restrict_type:
22033 this_type = read_tag_restrict_type (die, cu);
22034 break;
c906108c 22035 case DW_TAG_string_type:
f792889a 22036 this_type = read_tag_string_type (die, cu);
c906108c
SS
22037 break;
22038 case DW_TAG_typedef:
f792889a 22039 this_type = read_typedef (die, cu);
c906108c 22040 break;
a02abb62 22041 case DW_TAG_subrange_type:
f792889a 22042 this_type = read_subrange_type (die, cu);
a02abb62 22043 break;
c906108c 22044 case DW_TAG_base_type:
f792889a 22045 this_type = read_base_type (die, cu);
c906108c 22046 break;
81a17f79 22047 case DW_TAG_unspecified_type:
f792889a 22048 this_type = read_unspecified_type (die, cu);
81a17f79 22049 break;
0114d602
DJ
22050 case DW_TAG_namespace:
22051 this_type = read_namespace_type (die, cu);
22052 break;
f55ee35c
JK
22053 case DW_TAG_module:
22054 this_type = read_module_type (die, cu);
22055 break;
a2c2acaf
MW
22056 case DW_TAG_atomic_type:
22057 this_type = read_tag_atomic_type (die, cu);
22058 break;
c906108c 22059 default:
3e43a32a
MS
22060 complaint (&symfile_complaints,
22061 _("unexpected tag in read_type_die: '%s'"),
4d3c2250 22062 dwarf_tag_name (die->tag));
c906108c
SS
22063 break;
22064 }
63d06c5c 22065
f792889a 22066 return this_type;
63d06c5c
DC
22067}
22068
abc72ce4
DE
22069/* See if we can figure out if the class lives in a namespace. We do
22070 this by looking for a member function; its demangled name will
22071 contain namespace info, if there is any.
22072 Return the computed name or NULL.
22073 Space for the result is allocated on the objfile's obstack.
22074 This is the full-die version of guess_partial_die_structure_name.
22075 In this case we know DIE has no useful parent. */
22076
22077static char *
22078guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
22079{
22080 struct die_info *spec_die;
22081 struct dwarf2_cu *spec_cu;
22082 struct die_info *child;
518817b3 22083 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
abc72ce4
DE
22084
22085 spec_cu = cu;
22086 spec_die = die_specification (die, &spec_cu);
22087 if (spec_die != NULL)
22088 {
22089 die = spec_die;
22090 cu = spec_cu;
22091 }
22092
22093 for (child = die->child;
22094 child != NULL;
22095 child = child->sibling)
22096 {
22097 if (child->tag == DW_TAG_subprogram)
22098 {
73b9be8b 22099 const char *linkage_name = dw2_linkage_name (child, cu);
abc72ce4 22100
7d45c7c3 22101 if (linkage_name != NULL)
abc72ce4
DE
22102 {
22103 char *actual_name
22104 = language_class_name_from_physname (cu->language_defn,
7d45c7c3 22105 linkage_name);
abc72ce4
DE
22106 char *name = NULL;
22107
22108 if (actual_name != NULL)
22109 {
15d034d0 22110 const char *die_name = dwarf2_name (die, cu);
abc72ce4
DE
22111
22112 if (die_name != NULL
22113 && strcmp (die_name, actual_name) != 0)
22114 {
22115 /* Strip off the class name from the full name.
22116 We want the prefix. */
22117 int die_name_len = strlen (die_name);
22118 int actual_name_len = strlen (actual_name);
22119
22120 /* Test for '::' as a sanity check. */
22121 if (actual_name_len > die_name_len + 2
3e43a32a
MS
22122 && actual_name[actual_name_len
22123 - die_name_len - 1] == ':')
224c3ddb 22124 name = (char *) obstack_copy0 (
e3b94546 22125 &objfile->per_bfd->storage_obstack,
224c3ddb 22126 actual_name, actual_name_len - die_name_len - 2);
abc72ce4
DE
22127 }
22128 }
22129 xfree (actual_name);
22130 return name;
22131 }
22132 }
22133 }
22134
22135 return NULL;
22136}
22137
96408a79
SA
22138/* GCC might emit a nameless typedef that has a linkage name. Determine the
22139 prefix part in such case. See
22140 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
22141
a121b7c1 22142static const char *
96408a79
SA
22143anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
22144{
22145 struct attribute *attr;
e6a959d6 22146 const char *base;
96408a79
SA
22147
22148 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
22149 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
22150 return NULL;
22151
7d45c7c3 22152 if (dwarf2_string_attr (die, DW_AT_name, cu) != NULL)
96408a79
SA
22153 return NULL;
22154
73b9be8b 22155 attr = dw2_linkage_name_attr (die, cu);
96408a79
SA
22156 if (attr == NULL || DW_STRING (attr) == NULL)
22157 return NULL;
22158
22159 /* dwarf2_name had to be already called. */
22160 gdb_assert (DW_STRING_IS_CANONICAL (attr));
22161
22162 /* Strip the base name, keep any leading namespaces/classes. */
22163 base = strrchr (DW_STRING (attr), ':');
22164 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
22165 return "";
22166
518817b3 22167 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
e3b94546 22168 return (char *) obstack_copy0 (&objfile->per_bfd->storage_obstack,
224c3ddb
SM
22169 DW_STRING (attr),
22170 &base[-1] - DW_STRING (attr));
96408a79
SA
22171}
22172
fdde2d81 22173/* Return the name of the namespace/class that DIE is defined within,
0114d602 22174 or "" if we can't tell. The caller should not xfree the result.
fdde2d81 22175
0114d602
DJ
22176 For example, if we're within the method foo() in the following
22177 code:
22178
22179 namespace N {
22180 class C {
22181 void foo () {
22182 }
22183 };
22184 }
22185
22186 then determine_prefix on foo's die will return "N::C". */
fdde2d81 22187
0d5cff50 22188static const char *
e142c38c 22189determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
63d06c5c 22190{
518817b3
SM
22191 struct dwarf2_per_objfile *dwarf2_per_objfile
22192 = cu->per_cu->dwarf2_per_objfile;
0114d602
DJ
22193 struct die_info *parent, *spec_die;
22194 struct dwarf2_cu *spec_cu;
22195 struct type *parent_type;
a121b7c1 22196 const char *retval;
63d06c5c 22197
9c37b5ae 22198 if (cu->language != language_cplus
c44af4eb
TT
22199 && cu->language != language_fortran && cu->language != language_d
22200 && cu->language != language_rust)
0114d602
DJ
22201 return "";
22202
96408a79
SA
22203 retval = anonymous_struct_prefix (die, cu);
22204 if (retval)
22205 return retval;
22206
0114d602
DJ
22207 /* We have to be careful in the presence of DW_AT_specification.
22208 For example, with GCC 3.4, given the code
22209
22210 namespace N {
22211 void foo() {
22212 // Definition of N::foo.
22213 }
22214 }
22215
22216 then we'll have a tree of DIEs like this:
22217
22218 1: DW_TAG_compile_unit
22219 2: DW_TAG_namespace // N
22220 3: DW_TAG_subprogram // declaration of N::foo
22221 4: DW_TAG_subprogram // definition of N::foo
22222 DW_AT_specification // refers to die #3
22223
22224 Thus, when processing die #4, we have to pretend that we're in
22225 the context of its DW_AT_specification, namely the contex of die
22226 #3. */
22227 spec_cu = cu;
22228 spec_die = die_specification (die, &spec_cu);
22229 if (spec_die == NULL)
22230 parent = die->parent;
22231 else
63d06c5c 22232 {
0114d602
DJ
22233 parent = spec_die->parent;
22234 cu = spec_cu;
63d06c5c 22235 }
0114d602
DJ
22236
22237 if (parent == NULL)
22238 return "";
98bfdba5
PA
22239 else if (parent->building_fullname)
22240 {
22241 const char *name;
22242 const char *parent_name;
22243
22244 /* It has been seen on RealView 2.2 built binaries,
22245 DW_TAG_template_type_param types actually _defined_ as
22246 children of the parent class:
22247
22248 enum E {};
22249 template class <class Enum> Class{};
22250 Class<enum E> class_e;
22251
22252 1: DW_TAG_class_type (Class)
22253 2: DW_TAG_enumeration_type (E)
22254 3: DW_TAG_enumerator (enum1:0)
22255 3: DW_TAG_enumerator (enum2:1)
22256 ...
22257 2: DW_TAG_template_type_param
22258 DW_AT_type DW_FORM_ref_udata (E)
22259
22260 Besides being broken debug info, it can put GDB into an
22261 infinite loop. Consider:
22262
22263 When we're building the full name for Class<E>, we'll start
22264 at Class, and go look over its template type parameters,
22265 finding E. We'll then try to build the full name of E, and
22266 reach here. We're now trying to build the full name of E,
22267 and look over the parent DIE for containing scope. In the
22268 broken case, if we followed the parent DIE of E, we'd again
22269 find Class, and once again go look at its template type
22270 arguments, etc., etc. Simply don't consider such parent die
22271 as source-level parent of this die (it can't be, the language
22272 doesn't allow it), and break the loop here. */
22273 name = dwarf2_name (die, cu);
22274 parent_name = dwarf2_name (parent, cu);
22275 complaint (&symfile_complaints,
22276 _("template param type '%s' defined within parent '%s'"),
22277 name ? name : "<unknown>",
22278 parent_name ? parent_name : "<unknown>");
22279 return "";
22280 }
63d06c5c 22281 else
0114d602
DJ
22282 switch (parent->tag)
22283 {
63d06c5c 22284 case DW_TAG_namespace:
0114d602 22285 parent_type = read_type_die (parent, cu);
acebe513
UW
22286 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
22287 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
22288 Work around this problem here. */
22289 if (cu->language == language_cplus
22290 && strcmp (TYPE_TAG_NAME (parent_type), "::") == 0)
22291 return "";
0114d602
DJ
22292 /* We give a name to even anonymous namespaces. */
22293 return TYPE_TAG_NAME (parent_type);
63d06c5c 22294 case DW_TAG_class_type:
680b30c7 22295 case DW_TAG_interface_type:
63d06c5c 22296 case DW_TAG_structure_type:
0114d602 22297 case DW_TAG_union_type:
f55ee35c 22298 case DW_TAG_module:
0114d602
DJ
22299 parent_type = read_type_die (parent, cu);
22300 if (TYPE_TAG_NAME (parent_type) != NULL)
22301 return TYPE_TAG_NAME (parent_type);
22302 else
22303 /* An anonymous structure is only allowed non-static data
22304 members; no typedefs, no member functions, et cetera.
22305 So it does not need a prefix. */
22306 return "";
abc72ce4 22307 case DW_TAG_compile_unit:
95554aad 22308 case DW_TAG_partial_unit:
abc72ce4
DE
22309 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
22310 if (cu->language == language_cplus
8b70b953 22311 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
abc72ce4
DE
22312 && die->child != NULL
22313 && (die->tag == DW_TAG_class_type
22314 || die->tag == DW_TAG_structure_type
22315 || die->tag == DW_TAG_union_type))
22316 {
22317 char *name = guess_full_die_structure_name (die, cu);
22318 if (name != NULL)
22319 return name;
22320 }
22321 return "";
3d567982
TT
22322 case DW_TAG_enumeration_type:
22323 parent_type = read_type_die (parent, cu);
22324 if (TYPE_DECLARED_CLASS (parent_type))
22325 {
22326 if (TYPE_TAG_NAME (parent_type) != NULL)
22327 return TYPE_TAG_NAME (parent_type);
22328 return "";
22329 }
22330 /* Fall through. */
63d06c5c 22331 default:
8176b9b8 22332 return determine_prefix (parent, cu);
63d06c5c 22333 }
63d06c5c
DC
22334}
22335
3e43a32a
MS
22336/* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
22337 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
22338 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
22339 an obconcat, otherwise allocate storage for the result. The CU argument is
22340 used to determine the language and hence, the appropriate separator. */
987504bb 22341
f55ee35c 22342#define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
63d06c5c
DC
22343
22344static char *
f55ee35c
JK
22345typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
22346 int physname, struct dwarf2_cu *cu)
63d06c5c 22347{
f55ee35c 22348 const char *lead = "";
5c315b68 22349 const char *sep;
63d06c5c 22350
3e43a32a
MS
22351 if (suffix == NULL || suffix[0] == '\0'
22352 || prefix == NULL || prefix[0] == '\0')
987504bb 22353 sep = "";
45280282
IB
22354 else if (cu->language == language_d)
22355 {
22356 /* For D, the 'main' function could be defined in any module, but it
22357 should never be prefixed. */
22358 if (strcmp (suffix, "D main") == 0)
22359 {
22360 prefix = "";
22361 sep = "";
22362 }
22363 else
22364 sep = ".";
22365 }
f55ee35c
JK
22366 else if (cu->language == language_fortran && physname)
22367 {
22368 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
22369 DW_AT_MIPS_linkage_name is preferred and used instead. */
22370
22371 lead = "__";
22372 sep = "_MOD_";
22373 }
987504bb
JJ
22374 else
22375 sep = "::";
63d06c5c 22376
6dd47d34
DE
22377 if (prefix == NULL)
22378 prefix = "";
22379 if (suffix == NULL)
22380 suffix = "";
22381
987504bb
JJ
22382 if (obs == NULL)
22383 {
3e43a32a 22384 char *retval
224c3ddb
SM
22385 = ((char *)
22386 xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1));
9a619af0 22387
f55ee35c
JK
22388 strcpy (retval, lead);
22389 strcat (retval, prefix);
6dd47d34
DE
22390 strcat (retval, sep);
22391 strcat (retval, suffix);
63d06c5c
DC
22392 return retval;
22393 }
987504bb
JJ
22394 else
22395 {
22396 /* We have an obstack. */
f55ee35c 22397 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
987504bb 22398 }
63d06c5c
DC
22399}
22400
c906108c
SS
22401/* Return sibling of die, NULL if no sibling. */
22402
f9aca02d 22403static struct die_info *
fba45db2 22404sibling_die (struct die_info *die)
c906108c 22405{
639d11d3 22406 return die->sibling;
c906108c
SS
22407}
22408
71c25dea
TT
22409/* Get name of a die, return NULL if not found. */
22410
15d034d0
TT
22411static const char *
22412dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
71c25dea
TT
22413 struct obstack *obstack)
22414{
22415 if (name && cu->language == language_cplus)
22416 {
2f408ecb 22417 std::string canon_name = cp_canonicalize_string (name);
71c25dea 22418
2f408ecb 22419 if (!canon_name.empty ())
71c25dea 22420 {
2f408ecb
PA
22421 if (canon_name != name)
22422 name = (const char *) obstack_copy0 (obstack,
22423 canon_name.c_str (),
22424 canon_name.length ());
71c25dea
TT
22425 }
22426 }
22427
22428 return name;
c906108c
SS
22429}
22430
96553a0c
DE
22431/* Get name of a die, return NULL if not found.
22432 Anonymous namespaces are converted to their magic string. */
9219021c 22433
15d034d0 22434static const char *
e142c38c 22435dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
9219021c
DC
22436{
22437 struct attribute *attr;
518817b3 22438 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
9219021c 22439
e142c38c 22440 attr = dwarf2_attr (die, DW_AT_name, cu);
53832f31 22441 if ((!attr || !DW_STRING (attr))
96553a0c 22442 && die->tag != DW_TAG_namespace
53832f31
TT
22443 && die->tag != DW_TAG_class_type
22444 && die->tag != DW_TAG_interface_type
22445 && die->tag != DW_TAG_structure_type
22446 && die->tag != DW_TAG_union_type)
71c25dea
TT
22447 return NULL;
22448
22449 switch (die->tag)
22450 {
22451 case DW_TAG_compile_unit:
95554aad 22452 case DW_TAG_partial_unit:
71c25dea
TT
22453 /* Compilation units have a DW_AT_name that is a filename, not
22454 a source language identifier. */
22455 case DW_TAG_enumeration_type:
22456 case DW_TAG_enumerator:
22457 /* These tags always have simple identifiers already; no need
22458 to canonicalize them. */
22459 return DW_STRING (attr);
907af001 22460
96553a0c
DE
22461 case DW_TAG_namespace:
22462 if (attr != NULL && DW_STRING (attr) != NULL)
22463 return DW_STRING (attr);
22464 return CP_ANONYMOUS_NAMESPACE_STR;
22465
907af001
UW
22466 case DW_TAG_class_type:
22467 case DW_TAG_interface_type:
22468 case DW_TAG_structure_type:
22469 case DW_TAG_union_type:
22470 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
22471 structures or unions. These were of the form "._%d" in GCC 4.1,
22472 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
22473 and GCC 4.4. We work around this problem by ignoring these. */
53832f31 22474 if (attr && DW_STRING (attr)
61012eef
GB
22475 && (startswith (DW_STRING (attr), "._")
22476 || startswith (DW_STRING (attr), "<anonymous")))
907af001 22477 return NULL;
53832f31
TT
22478
22479 /* GCC might emit a nameless typedef that has a linkage name. See
22480 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
22481 if (!attr || DW_STRING (attr) == NULL)
22482 {
df5c6c50 22483 char *demangled = NULL;
53832f31 22484
73b9be8b 22485 attr = dw2_linkage_name_attr (die, cu);
53832f31
TT
22486 if (attr == NULL || DW_STRING (attr) == NULL)
22487 return NULL;
22488
df5c6c50
JK
22489 /* Avoid demangling DW_STRING (attr) the second time on a second
22490 call for the same DIE. */
22491 if (!DW_STRING_IS_CANONICAL (attr))
8de20a37 22492 demangled = gdb_demangle (DW_STRING (attr), DMGL_TYPES);
53832f31
TT
22493
22494 if (demangled)
22495 {
e6a959d6 22496 const char *base;
96408a79 22497
53832f31 22498 /* FIXME: we already did this for the partial symbol... */
34a68019 22499 DW_STRING (attr)
224c3ddb 22500 = ((const char *)
e3b94546 22501 obstack_copy0 (&objfile->per_bfd->storage_obstack,
224c3ddb 22502 demangled, strlen (demangled)));
53832f31
TT
22503 DW_STRING_IS_CANONICAL (attr) = 1;
22504 xfree (demangled);
96408a79
SA
22505
22506 /* Strip any leading namespaces/classes, keep only the base name.
22507 DW_AT_name for named DIEs does not contain the prefixes. */
22508 base = strrchr (DW_STRING (attr), ':');
22509 if (base && base > DW_STRING (attr) && base[-1] == ':')
22510 return &base[1];
22511 else
22512 return DW_STRING (attr);
53832f31
TT
22513 }
22514 }
907af001
UW
22515 break;
22516
71c25dea 22517 default:
907af001
UW
22518 break;
22519 }
22520
22521 if (!DW_STRING_IS_CANONICAL (attr))
22522 {
22523 DW_STRING (attr)
22524 = dwarf2_canonicalize_name (DW_STRING (attr), cu,
e3b94546 22525 &objfile->per_bfd->storage_obstack);
907af001 22526 DW_STRING_IS_CANONICAL (attr) = 1;
71c25dea 22527 }
907af001 22528 return DW_STRING (attr);
9219021c
DC
22529}
22530
22531/* Return the die that this die in an extension of, or NULL if there
f2f0e013
DJ
22532 is none. *EXT_CU is the CU containing DIE on input, and the CU
22533 containing the return value on output. */
9219021c
DC
22534
22535static struct die_info *
f2f0e013 22536dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
9219021c
DC
22537{
22538 struct attribute *attr;
9219021c 22539
f2f0e013 22540 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
9219021c
DC
22541 if (attr == NULL)
22542 return NULL;
22543
f2f0e013 22544 return follow_die_ref (die, attr, ext_cu);
9219021c
DC
22545}
22546
c906108c
SS
22547/* Convert a DIE tag into its string name. */
22548
f39c6ffd 22549static const char *
aa1ee363 22550dwarf_tag_name (unsigned tag)
c906108c 22551{
f39c6ffd
TT
22552 const char *name = get_DW_TAG_name (tag);
22553
22554 if (name == NULL)
22555 return "DW_TAG_<unknown>";
22556
22557 return name;
c906108c
SS
22558}
22559
22560/* Convert a DWARF attribute code into its string name. */
22561
f39c6ffd 22562static const char *
aa1ee363 22563dwarf_attr_name (unsigned attr)
c906108c 22564{
f39c6ffd
TT
22565 const char *name;
22566
c764a876 22567#ifdef MIPS /* collides with DW_AT_HP_block_index */
f39c6ffd
TT
22568 if (attr == DW_AT_MIPS_fde)
22569 return "DW_AT_MIPS_fde";
22570#else
22571 if (attr == DW_AT_HP_block_index)
22572 return "DW_AT_HP_block_index";
c764a876 22573#endif
f39c6ffd
TT
22574
22575 name = get_DW_AT_name (attr);
22576
22577 if (name == NULL)
22578 return "DW_AT_<unknown>";
22579
22580 return name;
c906108c
SS
22581}
22582
22583/* Convert a DWARF value form code into its string name. */
22584
f39c6ffd 22585static const char *
aa1ee363 22586dwarf_form_name (unsigned form)
c906108c 22587{
f39c6ffd
TT
22588 const char *name = get_DW_FORM_name (form);
22589
22590 if (name == NULL)
22591 return "DW_FORM_<unknown>";
22592
22593 return name;
c906108c
SS
22594}
22595
a121b7c1 22596static const char *
fba45db2 22597dwarf_bool_name (unsigned mybool)
c906108c
SS
22598{
22599 if (mybool)
22600 return "TRUE";
22601 else
22602 return "FALSE";
22603}
22604
22605/* Convert a DWARF type code into its string name. */
22606
f39c6ffd 22607static const char *
aa1ee363 22608dwarf_type_encoding_name (unsigned enc)
c906108c 22609{
f39c6ffd 22610 const char *name = get_DW_ATE_name (enc);
c906108c 22611
f39c6ffd
TT
22612 if (name == NULL)
22613 return "DW_ATE_<unknown>";
c906108c 22614
f39c6ffd 22615 return name;
c906108c 22616}
c906108c 22617
f9aca02d 22618static void
d97bc12b 22619dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
c906108c
SS
22620{
22621 unsigned int i;
22622
d97bc12b 22623 print_spaces (indent, f);
9d8780f0 22624 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset %s)\n",
9c541725 22625 dwarf_tag_name (die->tag), die->abbrev,
9d8780f0 22626 sect_offset_str (die->sect_off));
d97bc12b
DE
22627
22628 if (die->parent != NULL)
22629 {
22630 print_spaces (indent, f);
9d8780f0
SM
22631 fprintf_unfiltered (f, " parent at offset: %s\n",
22632 sect_offset_str (die->parent->sect_off));
d97bc12b
DE
22633 }
22634
22635 print_spaces (indent, f);
22636 fprintf_unfiltered (f, " has children: %s\n",
639d11d3 22637 dwarf_bool_name (die->child != NULL));
c906108c 22638
d97bc12b
DE
22639 print_spaces (indent, f);
22640 fprintf_unfiltered (f, " attributes:\n");
22641
c906108c
SS
22642 for (i = 0; i < die->num_attrs; ++i)
22643 {
d97bc12b
DE
22644 print_spaces (indent, f);
22645 fprintf_unfiltered (f, " %s (%s) ",
c906108c
SS
22646 dwarf_attr_name (die->attrs[i].name),
22647 dwarf_form_name (die->attrs[i].form));
d97bc12b 22648
c906108c
SS
22649 switch (die->attrs[i].form)
22650 {
c906108c 22651 case DW_FORM_addr:
3019eac3 22652 case DW_FORM_GNU_addr_index:
d97bc12b 22653 fprintf_unfiltered (f, "address: ");
5af949e3 22654 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
c906108c
SS
22655 break;
22656 case DW_FORM_block2:
22657 case DW_FORM_block4:
22658 case DW_FORM_block:
22659 case DW_FORM_block1:
56eb65bd
SP
22660 fprintf_unfiltered (f, "block: size %s",
22661 pulongest (DW_BLOCK (&die->attrs[i])->size));
c906108c 22662 break;
2dc7f7b3 22663 case DW_FORM_exprloc:
56eb65bd
SP
22664 fprintf_unfiltered (f, "expression: size %s",
22665 pulongest (DW_BLOCK (&die->attrs[i])->size));
2dc7f7b3 22666 break;
0224619f
JK
22667 case DW_FORM_data16:
22668 fprintf_unfiltered (f, "constant of 16 bytes");
22669 break;
4568ecf9
DE
22670 case DW_FORM_ref_addr:
22671 fprintf_unfiltered (f, "ref address: ");
22672 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
22673 break;
36586728
TT
22674 case DW_FORM_GNU_ref_alt:
22675 fprintf_unfiltered (f, "alt ref address: ");
22676 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
22677 break;
10b3939b
DJ
22678 case DW_FORM_ref1:
22679 case DW_FORM_ref2:
22680 case DW_FORM_ref4:
4568ecf9
DE
22681 case DW_FORM_ref8:
22682 case DW_FORM_ref_udata:
d97bc12b 22683 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
4568ecf9 22684 (long) (DW_UNSND (&die->attrs[i])));
10b3939b 22685 break;
c906108c
SS
22686 case DW_FORM_data1:
22687 case DW_FORM_data2:
22688 case DW_FORM_data4:
ce5d95e1 22689 case DW_FORM_data8:
c906108c
SS
22690 case DW_FORM_udata:
22691 case DW_FORM_sdata:
43bbcdc2
PH
22692 fprintf_unfiltered (f, "constant: %s",
22693 pulongest (DW_UNSND (&die->attrs[i])));
c906108c 22694 break;
2dc7f7b3
TT
22695 case DW_FORM_sec_offset:
22696 fprintf_unfiltered (f, "section offset: %s",
22697 pulongest (DW_UNSND (&die->attrs[i])));
22698 break;
55f1336d 22699 case DW_FORM_ref_sig8:
ac9ec31b
DE
22700 fprintf_unfiltered (f, "signature: %s",
22701 hex_string (DW_SIGNATURE (&die->attrs[i])));
348e048f 22702 break;
c906108c 22703 case DW_FORM_string:
4bdf3d34 22704 case DW_FORM_strp:
43988095 22705 case DW_FORM_line_strp:
3019eac3 22706 case DW_FORM_GNU_str_index:
36586728 22707 case DW_FORM_GNU_strp_alt:
8285870a 22708 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
c906108c 22709 DW_STRING (&die->attrs[i])
8285870a
JK
22710 ? DW_STRING (&die->attrs[i]) : "",
22711 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
c906108c
SS
22712 break;
22713 case DW_FORM_flag:
22714 if (DW_UNSND (&die->attrs[i]))
d97bc12b 22715 fprintf_unfiltered (f, "flag: TRUE");
c906108c 22716 else
d97bc12b 22717 fprintf_unfiltered (f, "flag: FALSE");
c906108c 22718 break;
2dc7f7b3
TT
22719 case DW_FORM_flag_present:
22720 fprintf_unfiltered (f, "flag: TRUE");
22721 break;
a8329558 22722 case DW_FORM_indirect:
0963b4bd
MS
22723 /* The reader will have reduced the indirect form to
22724 the "base form" so this form should not occur. */
3e43a32a
MS
22725 fprintf_unfiltered (f,
22726 "unexpected attribute form: DW_FORM_indirect");
a8329558 22727 break;
663c44ac
JK
22728 case DW_FORM_implicit_const:
22729 fprintf_unfiltered (f, "constant: %s",
22730 plongest (DW_SND (&die->attrs[i])));
22731 break;
c906108c 22732 default:
d97bc12b 22733 fprintf_unfiltered (f, "unsupported attribute form: %d.",
c5aa993b 22734 die->attrs[i].form);
d97bc12b 22735 break;
c906108c 22736 }
d97bc12b 22737 fprintf_unfiltered (f, "\n");
c906108c
SS
22738 }
22739}
22740
f9aca02d 22741static void
d97bc12b 22742dump_die_for_error (struct die_info *die)
c906108c 22743{
d97bc12b
DE
22744 dump_die_shallow (gdb_stderr, 0, die);
22745}
22746
22747static void
22748dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
22749{
22750 int indent = level * 4;
22751
22752 gdb_assert (die != NULL);
22753
22754 if (level >= max_level)
22755 return;
22756
22757 dump_die_shallow (f, indent, die);
22758
22759 if (die->child != NULL)
c906108c 22760 {
d97bc12b
DE
22761 print_spaces (indent, f);
22762 fprintf_unfiltered (f, " Children:");
22763 if (level + 1 < max_level)
22764 {
22765 fprintf_unfiltered (f, "\n");
22766 dump_die_1 (f, level + 1, max_level, die->child);
22767 }
22768 else
22769 {
3e43a32a
MS
22770 fprintf_unfiltered (f,
22771 " [not printed, max nesting level reached]\n");
d97bc12b
DE
22772 }
22773 }
22774
22775 if (die->sibling != NULL && level > 0)
22776 {
22777 dump_die_1 (f, level, max_level, die->sibling);
c906108c
SS
22778 }
22779}
22780
d97bc12b
DE
22781/* This is called from the pdie macro in gdbinit.in.
22782 It's not static so gcc will keep a copy callable from gdb. */
22783
22784void
22785dump_die (struct die_info *die, int max_level)
22786{
22787 dump_die_1 (gdb_stdlog, 0, max_level, die);
22788}
22789
f9aca02d 22790static void
51545339 22791store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
c906108c 22792{
51545339 22793 void **slot;
c906108c 22794
9c541725
PA
22795 slot = htab_find_slot_with_hash (cu->die_hash, die,
22796 to_underlying (die->sect_off),
b64f50a1 22797 INSERT);
51545339
DJ
22798
22799 *slot = die;
c906108c
SS
22800}
22801
b64f50a1
JK
22802/* Return DIE offset of ATTR. Return 0 with complaint if ATTR is not of the
22803 required kind. */
22804
22805static sect_offset
ff39bb5e 22806dwarf2_get_ref_die_offset (const struct attribute *attr)
93311388 22807{
7771576e 22808 if (attr_form_is_ref (attr))
9c541725 22809 return (sect_offset) DW_UNSND (attr);
93311388
DE
22810
22811 complaint (&symfile_complaints,
22812 _("unsupported die ref attribute form: '%s'"),
22813 dwarf_form_name (attr->form));
9c541725 22814 return {};
c906108c
SS
22815}
22816
43bbcdc2
PH
22817/* Return the constant value held by ATTR. Return DEFAULT_VALUE if
22818 * the value held by the attribute is not constant. */
a02abb62 22819
43bbcdc2 22820static LONGEST
ff39bb5e 22821dwarf2_get_attr_constant_value (const struct attribute *attr, int default_value)
a02abb62 22822{
663c44ac 22823 if (attr->form == DW_FORM_sdata || attr->form == DW_FORM_implicit_const)
a02abb62
JB
22824 return DW_SND (attr);
22825 else if (attr->form == DW_FORM_udata
22826 || attr->form == DW_FORM_data1
22827 || attr->form == DW_FORM_data2
22828 || attr->form == DW_FORM_data4
22829 || attr->form == DW_FORM_data8)
22830 return DW_UNSND (attr);
22831 else
22832 {
0224619f 22833 /* For DW_FORM_data16 see attr_form_is_constant. */
3e43a32a
MS
22834 complaint (&symfile_complaints,
22835 _("Attribute value is not a constant (%s)"),
a02abb62
JB
22836 dwarf_form_name (attr->form));
22837 return default_value;
22838 }
22839}
22840
348e048f
DE
22841/* Follow reference or signature attribute ATTR of SRC_DIE.
22842 On entry *REF_CU is the CU of SRC_DIE.
22843 On exit *REF_CU is the CU of the result. */
22844
22845static struct die_info *
ff39bb5e 22846follow_die_ref_or_sig (struct die_info *src_die, const struct attribute *attr,
348e048f
DE
22847 struct dwarf2_cu **ref_cu)
22848{
22849 struct die_info *die;
22850
7771576e 22851 if (attr_form_is_ref (attr))
348e048f 22852 die = follow_die_ref (src_die, attr, ref_cu);
55f1336d 22853 else if (attr->form == DW_FORM_ref_sig8)
348e048f
DE
22854 die = follow_die_sig (src_die, attr, ref_cu);
22855 else
22856 {
22857 dump_die_for_error (src_die);
22858 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
518817b3 22859 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
348e048f
DE
22860 }
22861
22862 return die;
03dd20cc
DJ
22863}
22864
5c631832 22865/* Follow reference OFFSET.
673bfd45
DE
22866 On entry *REF_CU is the CU of the source die referencing OFFSET.
22867 On exit *REF_CU is the CU of the result.
22868 Returns NULL if OFFSET is invalid. */
f504f079 22869
f9aca02d 22870static struct die_info *
9c541725 22871follow_die_offset (sect_offset sect_off, int offset_in_dwz,
36586728 22872 struct dwarf2_cu **ref_cu)
c906108c 22873{
10b3939b 22874 struct die_info temp_die;
f2f0e013 22875 struct dwarf2_cu *target_cu, *cu = *ref_cu;
518817b3
SM
22876 struct dwarf2_per_objfile *dwarf2_per_objfile
22877 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 22878 struct objfile *objfile = dwarf2_per_objfile->objfile;
10b3939b 22879
348e048f
DE
22880 gdb_assert (cu->per_cu != NULL);
22881
98bfdba5
PA
22882 target_cu = cu;
22883
3019eac3 22884 if (cu->per_cu->is_debug_types)
348e048f
DE
22885 {
22886 /* .debug_types CUs cannot reference anything outside their CU.
22887 If they need to, they have to reference a signatured type via
55f1336d 22888 DW_FORM_ref_sig8. */
9c541725 22889 if (!offset_in_cu_p (&cu->header, sect_off))
5c631832 22890 return NULL;
348e048f 22891 }
36586728 22892 else if (offset_in_dwz != cu->per_cu->is_dwz
9c541725 22893 || !offset_in_cu_p (&cu->header, sect_off))
10b3939b
DJ
22894 {
22895 struct dwarf2_per_cu_data *per_cu;
9a619af0 22896
9c541725 22897 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
ed2dc618 22898 dwarf2_per_objfile);
03dd20cc
DJ
22899
22900 /* If necessary, add it to the queue and load its DIEs. */
95554aad
TT
22901 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
22902 load_full_comp_unit (per_cu, cu->language);
03dd20cc 22903
10b3939b
DJ
22904 target_cu = per_cu->cu;
22905 }
98bfdba5
PA
22906 else if (cu->dies == NULL)
22907 {
22908 /* We're loading full DIEs during partial symbol reading. */
22909 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
95554aad 22910 load_full_comp_unit (cu->per_cu, language_minimal);
98bfdba5 22911 }
c906108c 22912
f2f0e013 22913 *ref_cu = target_cu;
9c541725 22914 temp_die.sect_off = sect_off;
9a3c8263 22915 return (struct die_info *) htab_find_with_hash (target_cu->die_hash,
9c541725
PA
22916 &temp_die,
22917 to_underlying (sect_off));
5c631832 22918}
10b3939b 22919
5c631832
JK
22920/* Follow reference attribute ATTR of SRC_DIE.
22921 On entry *REF_CU is the CU of SRC_DIE.
22922 On exit *REF_CU is the CU of the result. */
22923
22924static struct die_info *
ff39bb5e 22925follow_die_ref (struct die_info *src_die, const struct attribute *attr,
5c631832
JK
22926 struct dwarf2_cu **ref_cu)
22927{
9c541725 22928 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
5c631832
JK
22929 struct dwarf2_cu *cu = *ref_cu;
22930 struct die_info *die;
22931
9c541725 22932 die = follow_die_offset (sect_off,
36586728
TT
22933 (attr->form == DW_FORM_GNU_ref_alt
22934 || cu->per_cu->is_dwz),
22935 ref_cu);
5c631832 22936 if (!die)
9d8780f0
SM
22937 error (_("Dwarf Error: Cannot find DIE at %s referenced from DIE "
22938 "at %s [in module %s]"),
22939 sect_offset_str (sect_off), sect_offset_str (src_die->sect_off),
518817b3 22940 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
348e048f 22941
5c631832
JK
22942 return die;
22943}
22944
9c541725 22945/* Return DWARF block referenced by DW_AT_location of DIE at SECT_OFF at PER_CU.
d83e736b 22946 Returned value is intended for DW_OP_call*. Returned
e3b94546
SM
22947 dwarf2_locexpr_baton->data has lifetime of
22948 PER_CU->DWARF2_PER_OBJFILE->OBJFILE. */
5c631832
JK
22949
22950struct dwarf2_locexpr_baton
9c541725 22951dwarf2_fetch_die_loc_sect_off (sect_offset sect_off,
8b9737bf
TT
22952 struct dwarf2_per_cu_data *per_cu,
22953 CORE_ADDR (*get_frame_pc) (void *baton),
22954 void *baton)
5c631832 22955{
918dd910 22956 struct dwarf2_cu *cu;
5c631832
JK
22957 struct die_info *die;
22958 struct attribute *attr;
22959 struct dwarf2_locexpr_baton retval;
e3b94546 22960 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
ed2dc618
SM
22961 struct dwarf2_per_objfile *dwarf2_per_objfile
22962 = get_dwarf2_per_objfile (objfile);
8cf6f0b1 22963
918dd910
JK
22964 if (per_cu->cu == NULL)
22965 load_cu (per_cu);
22966 cu = per_cu->cu;
cc12ce38
DE
22967 if (cu == NULL)
22968 {
22969 /* We shouldn't get here for a dummy CU, but don't crash on the user.
22970 Instead just throw an error, not much else we can do. */
9d8780f0
SM
22971 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
22972 sect_offset_str (sect_off), objfile_name (objfile));
cc12ce38 22973 }
918dd910 22974
9c541725 22975 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
5c631832 22976 if (!die)
9d8780f0
SM
22977 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
22978 sect_offset_str (sect_off), objfile_name (objfile));
5c631832
JK
22979
22980 attr = dwarf2_attr (die, DW_AT_location, cu);
22981 if (!attr)
22982 {
e103e986
JK
22983 /* DWARF: "If there is no such attribute, then there is no effect.".
22984 DATA is ignored if SIZE is 0. */
5c631832 22985
e103e986 22986 retval.data = NULL;
5c631832
JK
22987 retval.size = 0;
22988 }
8cf6f0b1
TT
22989 else if (attr_form_is_section_offset (attr))
22990 {
22991 struct dwarf2_loclist_baton loclist_baton;
22992 CORE_ADDR pc = (*get_frame_pc) (baton);
22993 size_t size;
22994
22995 fill_in_loclist_baton (cu, &loclist_baton, attr);
22996
22997 retval.data = dwarf2_find_location_expression (&loclist_baton,
22998 &size, pc);
22999 retval.size = size;
23000 }
5c631832
JK
23001 else
23002 {
23003 if (!attr_form_is_block (attr))
9d8780f0 23004 error (_("Dwarf Error: DIE at %s referenced in module %s "
5c631832 23005 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
9d8780f0 23006 sect_offset_str (sect_off), objfile_name (objfile));
5c631832
JK
23007
23008 retval.data = DW_BLOCK (attr)->data;
23009 retval.size = DW_BLOCK (attr)->size;
23010 }
23011 retval.per_cu = cu->per_cu;
918dd910 23012
ed2dc618 23013 age_cached_comp_units (dwarf2_per_objfile);
918dd910 23014
5c631832 23015 return retval;
348e048f
DE
23016}
23017
8b9737bf
TT
23018/* Like dwarf2_fetch_die_loc_sect_off, but take a CU
23019 offset. */
23020
23021struct dwarf2_locexpr_baton
23022dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
23023 struct dwarf2_per_cu_data *per_cu,
23024 CORE_ADDR (*get_frame_pc) (void *baton),
23025 void *baton)
23026{
9c541725 23027 sect_offset sect_off = per_cu->sect_off + to_underlying (offset_in_cu);
8b9737bf 23028
9c541725 23029 return dwarf2_fetch_die_loc_sect_off (sect_off, per_cu, get_frame_pc, baton);
8b9737bf
TT
23030}
23031
b6807d98
TT
23032/* Write a constant of a given type as target-ordered bytes into
23033 OBSTACK. */
23034
23035static const gdb_byte *
23036write_constant_as_bytes (struct obstack *obstack,
23037 enum bfd_endian byte_order,
23038 struct type *type,
23039 ULONGEST value,
23040 LONGEST *len)
23041{
23042 gdb_byte *result;
23043
23044 *len = TYPE_LENGTH (type);
224c3ddb 23045 result = (gdb_byte *) obstack_alloc (obstack, *len);
b6807d98
TT
23046 store_unsigned_integer (result, *len, byte_order, value);
23047
23048 return result;
23049}
23050
23051/* If the DIE at OFFSET in PER_CU has a DW_AT_const_value, return a
23052 pointer to the constant bytes and set LEN to the length of the
23053 data. If memory is needed, allocate it on OBSTACK. If the DIE
23054 does not have a DW_AT_const_value, return NULL. */
23055
23056const gdb_byte *
9c541725 23057dwarf2_fetch_constant_bytes (sect_offset sect_off,
b6807d98
TT
23058 struct dwarf2_per_cu_data *per_cu,
23059 struct obstack *obstack,
23060 LONGEST *len)
23061{
23062 struct dwarf2_cu *cu;
23063 struct die_info *die;
23064 struct attribute *attr;
23065 const gdb_byte *result = NULL;
23066 struct type *type;
23067 LONGEST value;
23068 enum bfd_endian byte_order;
e3b94546 23069 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
b6807d98 23070
b6807d98
TT
23071 if (per_cu->cu == NULL)
23072 load_cu (per_cu);
23073 cu = per_cu->cu;
cc12ce38
DE
23074 if (cu == NULL)
23075 {
23076 /* We shouldn't get here for a dummy CU, but don't crash on the user.
23077 Instead just throw an error, not much else we can do. */
9d8780f0
SM
23078 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
23079 sect_offset_str (sect_off), objfile_name (objfile));
cc12ce38 23080 }
b6807d98 23081
9c541725 23082 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
b6807d98 23083 if (!die)
9d8780f0
SM
23084 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
23085 sect_offset_str (sect_off), objfile_name (objfile));
b6807d98
TT
23086
23087 attr = dwarf2_attr (die, DW_AT_const_value, cu);
23088 if (attr == NULL)
23089 return NULL;
23090
e3b94546 23091 byte_order = (bfd_big_endian (objfile->obfd)
b6807d98
TT
23092 ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
23093
23094 switch (attr->form)
23095 {
23096 case DW_FORM_addr:
23097 case DW_FORM_GNU_addr_index:
23098 {
23099 gdb_byte *tem;
23100
23101 *len = cu->header.addr_size;
224c3ddb 23102 tem = (gdb_byte *) obstack_alloc (obstack, *len);
b6807d98
TT
23103 store_unsigned_integer (tem, *len, byte_order, DW_ADDR (attr));
23104 result = tem;
23105 }
23106 break;
23107 case DW_FORM_string:
23108 case DW_FORM_strp:
23109 case DW_FORM_GNU_str_index:
23110 case DW_FORM_GNU_strp_alt:
23111 /* DW_STRING is already allocated on the objfile obstack, point
23112 directly to it. */
23113 result = (const gdb_byte *) DW_STRING (attr);
23114 *len = strlen (DW_STRING (attr));
23115 break;
23116 case DW_FORM_block1:
23117 case DW_FORM_block2:
23118 case DW_FORM_block4:
23119 case DW_FORM_block:
23120 case DW_FORM_exprloc:
0224619f 23121 case DW_FORM_data16:
b6807d98
TT
23122 result = DW_BLOCK (attr)->data;
23123 *len = DW_BLOCK (attr)->size;
23124 break;
23125
23126 /* The DW_AT_const_value attributes are supposed to carry the
23127 symbol's value "represented as it would be on the target
23128 architecture." By the time we get here, it's already been
23129 converted to host endianness, so we just need to sign- or
23130 zero-extend it as appropriate. */
23131 case DW_FORM_data1:
23132 type = die_type (die, cu);
23133 result = dwarf2_const_value_data (attr, obstack, cu, &value, 8);
23134 if (result == NULL)
23135 result = write_constant_as_bytes (obstack, byte_order,
23136 type, value, len);
23137 break;
23138 case DW_FORM_data2:
23139 type = die_type (die, cu);
23140 result = dwarf2_const_value_data (attr, obstack, cu, &value, 16);
23141 if (result == NULL)
23142 result = write_constant_as_bytes (obstack, byte_order,
23143 type, value, len);
23144 break;
23145 case DW_FORM_data4:
23146 type = die_type (die, cu);
23147 result = dwarf2_const_value_data (attr, obstack, cu, &value, 32);
23148 if (result == NULL)
23149 result = write_constant_as_bytes (obstack, byte_order,
23150 type, value, len);
23151 break;
23152 case DW_FORM_data8:
23153 type = die_type (die, cu);
23154 result = dwarf2_const_value_data (attr, obstack, cu, &value, 64);
23155 if (result == NULL)
23156 result = write_constant_as_bytes (obstack, byte_order,
23157 type, value, len);
23158 break;
23159
23160 case DW_FORM_sdata:
663c44ac 23161 case DW_FORM_implicit_const:
b6807d98
TT
23162 type = die_type (die, cu);
23163 result = write_constant_as_bytes (obstack, byte_order,
23164 type, DW_SND (attr), len);
23165 break;
23166
23167 case DW_FORM_udata:
23168 type = die_type (die, cu);
23169 result = write_constant_as_bytes (obstack, byte_order,
23170 type, DW_UNSND (attr), len);
23171 break;
23172
23173 default:
23174 complaint (&symfile_complaints,
23175 _("unsupported const value attribute form: '%s'"),
23176 dwarf_form_name (attr->form));
23177 break;
23178 }
23179
23180 return result;
23181}
23182
7942e96e
AA
23183/* Return the type of the die at OFFSET in PER_CU. Return NULL if no
23184 valid type for this die is found. */
23185
23186struct type *
9c541725 23187dwarf2_fetch_die_type_sect_off (sect_offset sect_off,
7942e96e
AA
23188 struct dwarf2_per_cu_data *per_cu)
23189{
23190 struct dwarf2_cu *cu;
23191 struct die_info *die;
23192
7942e96e
AA
23193 if (per_cu->cu == NULL)
23194 load_cu (per_cu);
23195 cu = per_cu->cu;
23196 if (!cu)
23197 return NULL;
23198
9c541725 23199 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
7942e96e
AA
23200 if (!die)
23201 return NULL;
23202
23203 return die_type (die, cu);
23204}
23205
8a9b8146
TT
23206/* Return the type of the DIE at DIE_OFFSET in the CU named by
23207 PER_CU. */
23208
23209struct type *
b64f50a1 23210dwarf2_get_die_type (cu_offset die_offset,
8a9b8146
TT
23211 struct dwarf2_per_cu_data *per_cu)
23212{
9c541725 23213 sect_offset die_offset_sect = per_cu->sect_off + to_underlying (die_offset);
b64f50a1 23214 return get_die_type_at_offset (die_offset_sect, per_cu);
8a9b8146
TT
23215}
23216
ac9ec31b 23217/* Follow type unit SIG_TYPE referenced by SRC_DIE.
348e048f 23218 On entry *REF_CU is the CU of SRC_DIE.
ac9ec31b
DE
23219 On exit *REF_CU is the CU of the result.
23220 Returns NULL if the referenced DIE isn't found. */
348e048f
DE
23221
23222static struct die_info *
ac9ec31b
DE
23223follow_die_sig_1 (struct die_info *src_die, struct signatured_type *sig_type,
23224 struct dwarf2_cu **ref_cu)
348e048f 23225{
348e048f 23226 struct die_info temp_die;
348e048f
DE
23227 struct dwarf2_cu *sig_cu;
23228 struct die_info *die;
23229
ac9ec31b
DE
23230 /* While it might be nice to assert sig_type->type == NULL here,
23231 we can get here for DW_AT_imported_declaration where we need
23232 the DIE not the type. */
348e048f
DE
23233
23234 /* If necessary, add it to the queue and load its DIEs. */
23235
95554aad 23236 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, language_minimal))
a0f42c21 23237 read_signatured_type (sig_type);
348e048f 23238
348e048f 23239 sig_cu = sig_type->per_cu.cu;
69d751e3 23240 gdb_assert (sig_cu != NULL);
9c541725
PA
23241 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
23242 temp_die.sect_off = sig_type->type_offset_in_section;
9a3c8263 23243 die = (struct die_info *) htab_find_with_hash (sig_cu->die_hash, &temp_die,
9c541725 23244 to_underlying (temp_die.sect_off));
348e048f
DE
23245 if (die)
23246 {
ed2dc618 23247 struct dwarf2_per_objfile *dwarf2_per_objfile
518817b3 23248 = (*ref_cu)->per_cu->dwarf2_per_objfile;
ed2dc618 23249
796a7ff8
DE
23250 /* For .gdb_index version 7 keep track of included TUs.
23251 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
23252 if (dwarf2_per_objfile->index_table != NULL
23253 && dwarf2_per_objfile->index_table->version <= 7)
23254 {
23255 VEC_safe_push (dwarf2_per_cu_ptr,
23256 (*ref_cu)->per_cu->imported_symtabs,
23257 sig_cu->per_cu);
23258 }
23259
348e048f
DE
23260 *ref_cu = sig_cu;
23261 return die;
23262 }
23263
ac9ec31b
DE
23264 return NULL;
23265}
23266
23267/* Follow signatured type referenced by ATTR in SRC_DIE.
23268 On entry *REF_CU is the CU of SRC_DIE.
23269 On exit *REF_CU is the CU of the result.
23270 The result is the DIE of the type.
23271 If the referenced type cannot be found an error is thrown. */
23272
23273static struct die_info *
ff39bb5e 23274follow_die_sig (struct die_info *src_die, const struct attribute *attr,
ac9ec31b
DE
23275 struct dwarf2_cu **ref_cu)
23276{
23277 ULONGEST signature = DW_SIGNATURE (attr);
23278 struct signatured_type *sig_type;
23279 struct die_info *die;
23280
23281 gdb_assert (attr->form == DW_FORM_ref_sig8);
23282
a2ce51a0 23283 sig_type = lookup_signatured_type (*ref_cu, signature);
ac9ec31b
DE
23284 /* sig_type will be NULL if the signatured type is missing from
23285 the debug info. */
23286 if (sig_type == NULL)
23287 {
23288 error (_("Dwarf Error: Cannot find signatured DIE %s referenced"
9d8780f0
SM
23289 " from DIE at %s [in module %s]"),
23290 hex_string (signature), sect_offset_str (src_die->sect_off),
518817b3 23291 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
ac9ec31b
DE
23292 }
23293
23294 die = follow_die_sig_1 (src_die, sig_type, ref_cu);
23295 if (die == NULL)
23296 {
23297 dump_die_for_error (src_die);
23298 error (_("Dwarf Error: Problem reading signatured DIE %s referenced"
9d8780f0
SM
23299 " from DIE at %s [in module %s]"),
23300 hex_string (signature), sect_offset_str (src_die->sect_off),
518817b3 23301 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
ac9ec31b
DE
23302 }
23303
23304 return die;
23305}
23306
23307/* Get the type specified by SIGNATURE referenced in DIE/CU,
23308 reading in and processing the type unit if necessary. */
23309
23310static struct type *
23311get_signatured_type (struct die_info *die, ULONGEST signature,
23312 struct dwarf2_cu *cu)
23313{
518817b3
SM
23314 struct dwarf2_per_objfile *dwarf2_per_objfile
23315 = cu->per_cu->dwarf2_per_objfile;
ac9ec31b
DE
23316 struct signatured_type *sig_type;
23317 struct dwarf2_cu *type_cu;
23318 struct die_info *type_die;
23319 struct type *type;
23320
a2ce51a0 23321 sig_type = lookup_signatured_type (cu, signature);
ac9ec31b
DE
23322 /* sig_type will be NULL if the signatured type is missing from
23323 the debug info. */
23324 if (sig_type == NULL)
23325 {
23326 complaint (&symfile_complaints,
23327 _("Dwarf Error: Cannot find signatured DIE %s referenced"
9d8780f0
SM
23328 " from DIE at %s [in module %s]"),
23329 hex_string (signature), sect_offset_str (die->sect_off),
4262abfb 23330 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
23331 return build_error_marker_type (cu, die);
23332 }
23333
23334 /* If we already know the type we're done. */
23335 if (sig_type->type != NULL)
23336 return sig_type->type;
23337
23338 type_cu = cu;
23339 type_die = follow_die_sig_1 (die, sig_type, &type_cu);
23340 if (type_die != NULL)
23341 {
23342 /* N.B. We need to call get_die_type to ensure only one type for this DIE
23343 is created. This is important, for example, because for c++ classes
23344 we need TYPE_NAME set which is only done by new_symbol. Blech. */
23345 type = read_type_die (type_die, type_cu);
23346 if (type == NULL)
23347 {
23348 complaint (&symfile_complaints,
23349 _("Dwarf Error: Cannot build signatured type %s"
9d8780f0
SM
23350 " referenced from DIE at %s [in module %s]"),
23351 hex_string (signature), sect_offset_str (die->sect_off),
4262abfb 23352 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
23353 type = build_error_marker_type (cu, die);
23354 }
23355 }
23356 else
23357 {
23358 complaint (&symfile_complaints,
23359 _("Dwarf Error: Problem reading signatured DIE %s referenced"
9d8780f0
SM
23360 " from DIE at %s [in module %s]"),
23361 hex_string (signature), sect_offset_str (die->sect_off),
4262abfb 23362 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
23363 type = build_error_marker_type (cu, die);
23364 }
23365 sig_type->type = type;
23366
23367 return type;
23368}
23369
23370/* Get the type specified by the DW_AT_signature ATTR in DIE/CU,
23371 reading in and processing the type unit if necessary. */
23372
23373static struct type *
ff39bb5e 23374get_DW_AT_signature_type (struct die_info *die, const struct attribute *attr,
b385a60d 23375 struct dwarf2_cu *cu) /* ARI: editCase function */
ac9ec31b
DE
23376{
23377 /* Yes, DW_AT_signature can use a non-ref_sig8 reference. */
7771576e 23378 if (attr_form_is_ref (attr))
ac9ec31b
DE
23379 {
23380 struct dwarf2_cu *type_cu = cu;
23381 struct die_info *type_die = follow_die_ref (die, attr, &type_cu);
23382
23383 return read_type_die (type_die, type_cu);
23384 }
23385 else if (attr->form == DW_FORM_ref_sig8)
23386 {
23387 return get_signatured_type (die, DW_SIGNATURE (attr), cu);
23388 }
23389 else
23390 {
518817b3
SM
23391 struct dwarf2_per_objfile *dwarf2_per_objfile
23392 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 23393
ac9ec31b
DE
23394 complaint (&symfile_complaints,
23395 _("Dwarf Error: DW_AT_signature has bad form %s in DIE"
9d8780f0
SM
23396 " at %s [in module %s]"),
23397 dwarf_form_name (attr->form), sect_offset_str (die->sect_off),
4262abfb 23398 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
23399 return build_error_marker_type (cu, die);
23400 }
348e048f
DE
23401}
23402
e5fe5e75 23403/* Load the DIEs associated with type unit PER_CU into memory. */
348e048f
DE
23404
23405static void
e5fe5e75 23406load_full_type_unit (struct dwarf2_per_cu_data *per_cu)
348e048f 23407{
52dc124a 23408 struct signatured_type *sig_type;
348e048f 23409
f4dc4d17
DE
23410 /* Caller is responsible for ensuring type_unit_groups don't get here. */
23411 gdb_assert (! IS_TYPE_UNIT_GROUP (per_cu));
23412
6721b2ec
DE
23413 /* We have the per_cu, but we need the signatured_type.
23414 Fortunately this is an easy translation. */
23415 gdb_assert (per_cu->is_debug_types);
23416 sig_type = (struct signatured_type *) per_cu;
348e048f 23417
6721b2ec 23418 gdb_assert (per_cu->cu == NULL);
348e048f 23419
52dc124a 23420 read_signatured_type (sig_type);
348e048f 23421
6721b2ec 23422 gdb_assert (per_cu->cu != NULL);
348e048f
DE
23423}
23424
dee91e82
DE
23425/* die_reader_func for read_signatured_type.
23426 This is identical to load_full_comp_unit_reader,
23427 but is kept separate for now. */
348e048f
DE
23428
23429static void
dee91e82 23430read_signatured_type_reader (const struct die_reader_specs *reader,
d521ce57 23431 const gdb_byte *info_ptr,
dee91e82
DE
23432 struct die_info *comp_unit_die,
23433 int has_children,
23434 void *data)
348e048f 23435{
dee91e82 23436 struct dwarf2_cu *cu = reader->cu;
348e048f 23437
dee91e82
DE
23438 gdb_assert (cu->die_hash == NULL);
23439 cu->die_hash =
23440 htab_create_alloc_ex (cu->header.length / 12,
23441 die_hash,
23442 die_eq,
23443 NULL,
23444 &cu->comp_unit_obstack,
23445 hashtab_obstack_allocate,
23446 dummy_obstack_deallocate);
348e048f 23447
dee91e82
DE
23448 if (has_children)
23449 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
23450 &info_ptr, comp_unit_die);
23451 cu->dies = comp_unit_die;
23452 /* comp_unit_die is not stored in die_hash, no need. */
348e048f
DE
23453
23454 /* We try not to read any attributes in this function, because not
9cdd5dbd 23455 all CUs needed for references have been loaded yet, and symbol
348e048f 23456 table processing isn't initialized. But we have to set the CU language,
dee91e82
DE
23457 or we won't be able to build types correctly.
23458 Similarly, if we do not read the producer, we can not apply
23459 producer-specific interpretation. */
95554aad 23460 prepare_one_comp_unit (cu, cu->dies, language_minimal);
dee91e82 23461}
348e048f 23462
3019eac3
DE
23463/* Read in a signatured type and build its CU and DIEs.
23464 If the type is a stub for the real type in a DWO file,
23465 read in the real type from the DWO file as well. */
dee91e82
DE
23466
23467static void
23468read_signatured_type (struct signatured_type *sig_type)
23469{
23470 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
348e048f 23471
3019eac3 23472 gdb_assert (per_cu->is_debug_types);
dee91e82 23473 gdb_assert (per_cu->cu == NULL);
348e048f 23474
f4dc4d17
DE
23475 init_cutu_and_read_dies (per_cu, NULL, 0, 1,
23476 read_signatured_type_reader, NULL);
7ee85ab1 23477 sig_type->per_cu.tu_read = 1;
c906108c
SS
23478}
23479
c906108c
SS
23480/* Decode simple location descriptions.
23481 Given a pointer to a dwarf block that defines a location, compute
23482 the location and return the value.
23483
4cecd739
DJ
23484 NOTE drow/2003-11-18: This function is called in two situations
23485 now: for the address of static or global variables (partial symbols
23486 only) and for offsets into structures which are expected to be
23487 (more or less) constant. The partial symbol case should go away,
23488 and only the constant case should remain. That will let this
23489 function complain more accurately. A few special modes are allowed
23490 without complaint for global variables (for instance, global
23491 register values and thread-local values).
c906108c
SS
23492
23493 A location description containing no operations indicates that the
4cecd739 23494 object is optimized out. The return value is 0 for that case.
6b992462
DJ
23495 FIXME drow/2003-11-16: No callers check for this case any more; soon all
23496 callers will only want a very basic result and this can become a
21ae7a4d
JK
23497 complaint.
23498
23499 Note that stack[0] is unused except as a default error return. */
c906108c
SS
23500
23501static CORE_ADDR
e7c27a73 23502decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
c906108c 23503{
518817b3 23504 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
56eb65bd
SP
23505 size_t i;
23506 size_t size = blk->size;
d521ce57 23507 const gdb_byte *data = blk->data;
21ae7a4d
JK
23508 CORE_ADDR stack[64];
23509 int stacki;
23510 unsigned int bytes_read, unsnd;
23511 gdb_byte op;
c906108c 23512
21ae7a4d
JK
23513 i = 0;
23514 stacki = 0;
23515 stack[stacki] = 0;
23516 stack[++stacki] = 0;
23517
23518 while (i < size)
23519 {
23520 op = data[i++];
23521 switch (op)
23522 {
23523 case DW_OP_lit0:
23524 case DW_OP_lit1:
23525 case DW_OP_lit2:
23526 case DW_OP_lit3:
23527 case DW_OP_lit4:
23528 case DW_OP_lit5:
23529 case DW_OP_lit6:
23530 case DW_OP_lit7:
23531 case DW_OP_lit8:
23532 case DW_OP_lit9:
23533 case DW_OP_lit10:
23534 case DW_OP_lit11:
23535 case DW_OP_lit12:
23536 case DW_OP_lit13:
23537 case DW_OP_lit14:
23538 case DW_OP_lit15:
23539 case DW_OP_lit16:
23540 case DW_OP_lit17:
23541 case DW_OP_lit18:
23542 case DW_OP_lit19:
23543 case DW_OP_lit20:
23544 case DW_OP_lit21:
23545 case DW_OP_lit22:
23546 case DW_OP_lit23:
23547 case DW_OP_lit24:
23548 case DW_OP_lit25:
23549 case DW_OP_lit26:
23550 case DW_OP_lit27:
23551 case DW_OP_lit28:
23552 case DW_OP_lit29:
23553 case DW_OP_lit30:
23554 case DW_OP_lit31:
23555 stack[++stacki] = op - DW_OP_lit0;
23556 break;
f1bea926 23557
21ae7a4d
JK
23558 case DW_OP_reg0:
23559 case DW_OP_reg1:
23560 case DW_OP_reg2:
23561 case DW_OP_reg3:
23562 case DW_OP_reg4:
23563 case DW_OP_reg5:
23564 case DW_OP_reg6:
23565 case DW_OP_reg7:
23566 case DW_OP_reg8:
23567 case DW_OP_reg9:
23568 case DW_OP_reg10:
23569 case DW_OP_reg11:
23570 case DW_OP_reg12:
23571 case DW_OP_reg13:
23572 case DW_OP_reg14:
23573 case DW_OP_reg15:
23574 case DW_OP_reg16:
23575 case DW_OP_reg17:
23576 case DW_OP_reg18:
23577 case DW_OP_reg19:
23578 case DW_OP_reg20:
23579 case DW_OP_reg21:
23580 case DW_OP_reg22:
23581 case DW_OP_reg23:
23582 case DW_OP_reg24:
23583 case DW_OP_reg25:
23584 case DW_OP_reg26:
23585 case DW_OP_reg27:
23586 case DW_OP_reg28:
23587 case DW_OP_reg29:
23588 case DW_OP_reg30:
23589 case DW_OP_reg31:
23590 stack[++stacki] = op - DW_OP_reg0;
23591 if (i < size)
23592 dwarf2_complex_location_expr_complaint ();
23593 break;
c906108c 23594
21ae7a4d
JK
23595 case DW_OP_regx:
23596 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
23597 i += bytes_read;
23598 stack[++stacki] = unsnd;
23599 if (i < size)
23600 dwarf2_complex_location_expr_complaint ();
23601 break;
c906108c 23602
21ae7a4d
JK
23603 case DW_OP_addr:
23604 stack[++stacki] = read_address (objfile->obfd, &data[i],
23605 cu, &bytes_read);
23606 i += bytes_read;
23607 break;
d53d4ac5 23608
21ae7a4d
JK
23609 case DW_OP_const1u:
23610 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
23611 i += 1;
23612 break;
23613
23614 case DW_OP_const1s:
23615 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
23616 i += 1;
23617 break;
23618
23619 case DW_OP_const2u:
23620 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
23621 i += 2;
23622 break;
23623
23624 case DW_OP_const2s:
23625 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
23626 i += 2;
23627 break;
d53d4ac5 23628
21ae7a4d
JK
23629 case DW_OP_const4u:
23630 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
23631 i += 4;
23632 break;
23633
23634 case DW_OP_const4s:
23635 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
23636 i += 4;
23637 break;
23638
585861ea
JK
23639 case DW_OP_const8u:
23640 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
23641 i += 8;
23642 break;
23643
21ae7a4d
JK
23644 case DW_OP_constu:
23645 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
23646 &bytes_read);
23647 i += bytes_read;
23648 break;
23649
23650 case DW_OP_consts:
23651 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
23652 i += bytes_read;
23653 break;
23654
23655 case DW_OP_dup:
23656 stack[stacki + 1] = stack[stacki];
23657 stacki++;
23658 break;
23659
23660 case DW_OP_plus:
23661 stack[stacki - 1] += stack[stacki];
23662 stacki--;
23663 break;
23664
23665 case DW_OP_plus_uconst:
23666 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
23667 &bytes_read);
23668 i += bytes_read;
23669 break;
23670
23671 case DW_OP_minus:
23672 stack[stacki - 1] -= stack[stacki];
23673 stacki--;
23674 break;
23675
23676 case DW_OP_deref:
23677 /* If we're not the last op, then we definitely can't encode
23678 this using GDB's address_class enum. This is valid for partial
23679 global symbols, although the variable's address will be bogus
23680 in the psymtab. */
23681 if (i < size)
23682 dwarf2_complex_location_expr_complaint ();
23683 break;
23684
23685 case DW_OP_GNU_push_tls_address:
4aa4e28b 23686 case DW_OP_form_tls_address:
21ae7a4d
JK
23687 /* The top of the stack has the offset from the beginning
23688 of the thread control block at which the variable is located. */
23689 /* Nothing should follow this operator, so the top of stack would
23690 be returned. */
23691 /* This is valid for partial global symbols, but the variable's
585861ea
JK
23692 address will be bogus in the psymtab. Make it always at least
23693 non-zero to not look as a variable garbage collected by linker
23694 which have DW_OP_addr 0. */
21ae7a4d
JK
23695 if (i < size)
23696 dwarf2_complex_location_expr_complaint ();
585861ea 23697 stack[stacki]++;
21ae7a4d
JK
23698 break;
23699
23700 case DW_OP_GNU_uninit:
23701 break;
23702
3019eac3 23703 case DW_OP_GNU_addr_index:
49f6c839 23704 case DW_OP_GNU_const_index:
3019eac3
DE
23705 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
23706 &bytes_read);
23707 i += bytes_read;
23708 break;
23709
21ae7a4d
JK
23710 default:
23711 {
f39c6ffd 23712 const char *name = get_DW_OP_name (op);
21ae7a4d
JK
23713
23714 if (name)
23715 complaint (&symfile_complaints, _("unsupported stack op: '%s'"),
23716 name);
23717 else
23718 complaint (&symfile_complaints, _("unsupported stack op: '%02x'"),
23719 op);
23720 }
23721
23722 return (stack[stacki]);
d53d4ac5 23723 }
3c6e0cb3 23724
21ae7a4d
JK
23725 /* Enforce maximum stack depth of SIZE-1 to avoid writing
23726 outside of the allocated space. Also enforce minimum>0. */
23727 if (stacki >= ARRAY_SIZE (stack) - 1)
23728 {
23729 complaint (&symfile_complaints,
23730 _("location description stack overflow"));
23731 return 0;
23732 }
23733
23734 if (stacki <= 0)
23735 {
23736 complaint (&symfile_complaints,
23737 _("location description stack underflow"));
23738 return 0;
23739 }
23740 }
23741 return (stack[stacki]);
c906108c
SS
23742}
23743
23744/* memory allocation interface */
23745
c906108c 23746static struct dwarf_block *
7b5a2f43 23747dwarf_alloc_block (struct dwarf2_cu *cu)
c906108c 23748{
8d749320 23749 return XOBNEW (&cu->comp_unit_obstack, struct dwarf_block);
c906108c
SS
23750}
23751
c906108c 23752static struct die_info *
b60c80d6 23753dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
c906108c
SS
23754{
23755 struct die_info *die;
b60c80d6
DJ
23756 size_t size = sizeof (struct die_info);
23757
23758 if (num_attrs > 1)
23759 size += (num_attrs - 1) * sizeof (struct attribute);
c906108c 23760
b60c80d6 23761 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
c906108c
SS
23762 memset (die, 0, sizeof (struct die_info));
23763 return (die);
23764}
2e276125
JB
23765
23766\f
23767/* Macro support. */
23768
233d95b5
JK
23769/* Return file name relative to the compilation directory of file number I in
23770 *LH's file name table. The result is allocated using xmalloc; the caller is
2e276125 23771 responsible for freeing it. */
233d95b5 23772
2e276125 23773static char *
233d95b5 23774file_file_name (int file, struct line_header *lh)
2e276125 23775{
6a83a1e6
EZ
23776 /* Is the file number a valid index into the line header's file name
23777 table? Remember that file numbers start with one, not zero. */
fff8551c 23778 if (1 <= file && file <= lh->file_names.size ())
6a83a1e6 23779 {
8c43009f 23780 const file_entry &fe = lh->file_names[file - 1];
6e70227d 23781
8c43009f
PA
23782 if (!IS_ABSOLUTE_PATH (fe.name))
23783 {
23784 const char *dir = fe.include_dir (lh);
23785 if (dir != NULL)
23786 return concat (dir, SLASH_STRING, fe.name, (char *) NULL);
23787 }
23788 return xstrdup (fe.name);
6a83a1e6 23789 }
2e276125
JB
23790 else
23791 {
6a83a1e6
EZ
23792 /* The compiler produced a bogus file number. We can at least
23793 record the macro definitions made in the file, even if we
23794 won't be able to find the file by name. */
23795 char fake_name[80];
9a619af0 23796
8c042590
PM
23797 xsnprintf (fake_name, sizeof (fake_name),
23798 "<bad macro file number %d>", file);
2e276125 23799
6e70227d 23800 complaint (&symfile_complaints,
6a83a1e6
EZ
23801 _("bad file number in macro information (%d)"),
23802 file);
2e276125 23803
6a83a1e6 23804 return xstrdup (fake_name);
2e276125
JB
23805 }
23806}
23807
233d95b5
JK
23808/* Return the full name of file number I in *LH's file name table.
23809 Use COMP_DIR as the name of the current directory of the
23810 compilation. The result is allocated using xmalloc; the caller is
23811 responsible for freeing it. */
23812static char *
23813file_full_name (int file, struct line_header *lh, const char *comp_dir)
23814{
23815 /* Is the file number a valid index into the line header's file name
23816 table? Remember that file numbers start with one, not zero. */
fff8551c 23817 if (1 <= file && file <= lh->file_names.size ())
233d95b5
JK
23818 {
23819 char *relative = file_file_name (file, lh);
23820
23821 if (IS_ABSOLUTE_PATH (relative) || comp_dir == NULL)
23822 return relative;
b36cec19
PA
23823 return reconcat (relative, comp_dir, SLASH_STRING,
23824 relative, (char *) NULL);
233d95b5
JK
23825 }
23826 else
23827 return file_file_name (file, lh);
23828}
23829
2e276125
JB
23830
23831static struct macro_source_file *
23832macro_start_file (int file, int line,
23833 struct macro_source_file *current_file,
43f3e411 23834 struct line_header *lh)
2e276125 23835{
233d95b5
JK
23836 /* File name relative to the compilation directory of this source file. */
23837 char *file_name = file_file_name (file, lh);
2e276125 23838
2e276125 23839 if (! current_file)
abc9d0dc 23840 {
fc474241
DE
23841 /* Note: We don't create a macro table for this compilation unit
23842 at all until we actually get a filename. */
43f3e411 23843 struct macro_table *macro_table = get_macro_table ();
fc474241 23844
abc9d0dc
TT
23845 /* If we have no current file, then this must be the start_file
23846 directive for the compilation unit's main source file. */
fc474241
DE
23847 current_file = macro_set_main (macro_table, file_name);
23848 macro_define_special (macro_table);
abc9d0dc 23849 }
2e276125 23850 else
233d95b5 23851 current_file = macro_include (current_file, line, file_name);
2e276125 23852
233d95b5 23853 xfree (file_name);
6e70227d 23854
2e276125
JB
23855 return current_file;
23856}
23857
2e276125
JB
23858static const char *
23859consume_improper_spaces (const char *p, const char *body)
23860{
23861 if (*p == ' ')
23862 {
4d3c2250 23863 complaint (&symfile_complaints,
3e43a32a
MS
23864 _("macro definition contains spaces "
23865 "in formal argument list:\n`%s'"),
4d3c2250 23866 body);
2e276125
JB
23867
23868 while (*p == ' ')
23869 p++;
23870 }
23871
23872 return p;
23873}
23874
23875
23876static void
23877parse_macro_definition (struct macro_source_file *file, int line,
23878 const char *body)
23879{
23880 const char *p;
23881
23882 /* The body string takes one of two forms. For object-like macro
23883 definitions, it should be:
23884
23885 <macro name> " " <definition>
23886
23887 For function-like macro definitions, it should be:
23888
23889 <macro name> "() " <definition>
23890 or
23891 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
23892
23893 Spaces may appear only where explicitly indicated, and in the
23894 <definition>.
23895
23896 The Dwarf 2 spec says that an object-like macro's name is always
23897 followed by a space, but versions of GCC around March 2002 omit
6e70227d 23898 the space when the macro's definition is the empty string.
2e276125
JB
23899
23900 The Dwarf 2 spec says that there should be no spaces between the
23901 formal arguments in a function-like macro's formal argument list,
23902 but versions of GCC around March 2002 include spaces after the
23903 commas. */
23904
23905
23906 /* Find the extent of the macro name. The macro name is terminated
23907 by either a space or null character (for an object-like macro) or
23908 an opening paren (for a function-like macro). */
23909 for (p = body; *p; p++)
23910 if (*p == ' ' || *p == '(')
23911 break;
23912
23913 if (*p == ' ' || *p == '\0')
23914 {
23915 /* It's an object-like macro. */
23916 int name_len = p - body;
3f8a7804 23917 char *name = savestring (body, name_len);
2e276125
JB
23918 const char *replacement;
23919
23920 if (*p == ' ')
23921 replacement = body + name_len + 1;
23922 else
23923 {
4d3c2250 23924 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
23925 replacement = body + name_len;
23926 }
6e70227d 23927
2e276125
JB
23928 macro_define_object (file, line, name, replacement);
23929
23930 xfree (name);
23931 }
23932 else if (*p == '(')
23933 {
23934 /* It's a function-like macro. */
3f8a7804 23935 char *name = savestring (body, p - body);
2e276125
JB
23936 int argc = 0;
23937 int argv_size = 1;
8d749320 23938 char **argv = XNEWVEC (char *, argv_size);
2e276125
JB
23939
23940 p++;
23941
23942 p = consume_improper_spaces (p, body);
23943
23944 /* Parse the formal argument list. */
23945 while (*p && *p != ')')
23946 {
23947 /* Find the extent of the current argument name. */
23948 const char *arg_start = p;
23949
23950 while (*p && *p != ',' && *p != ')' && *p != ' ')
23951 p++;
23952
23953 if (! *p || p == arg_start)
4d3c2250 23954 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
23955 else
23956 {
23957 /* Make sure argv has room for the new argument. */
23958 if (argc >= argv_size)
23959 {
23960 argv_size *= 2;
224c3ddb 23961 argv = XRESIZEVEC (char *, argv, argv_size);
2e276125
JB
23962 }
23963
3f8a7804 23964 argv[argc++] = savestring (arg_start, p - arg_start);
2e276125
JB
23965 }
23966
23967 p = consume_improper_spaces (p, body);
23968
23969 /* Consume the comma, if present. */
23970 if (*p == ',')
23971 {
23972 p++;
23973
23974 p = consume_improper_spaces (p, body);
23975 }
23976 }
23977
23978 if (*p == ')')
23979 {
23980 p++;
23981
23982 if (*p == ' ')
23983 /* Perfectly formed definition, no complaints. */
23984 macro_define_function (file, line, name,
6e70227d 23985 argc, (const char **) argv,
2e276125
JB
23986 p + 1);
23987 else if (*p == '\0')
23988 {
23989 /* Complain, but do define it. */
4d3c2250 23990 dwarf2_macro_malformed_definition_complaint (body);
2e276125 23991 macro_define_function (file, line, name,
6e70227d 23992 argc, (const char **) argv,
2e276125
JB
23993 p);
23994 }
23995 else
23996 /* Just complain. */
4d3c2250 23997 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
23998 }
23999 else
24000 /* Just complain. */
4d3c2250 24001 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
24002
24003 xfree (name);
24004 {
24005 int i;
24006
24007 for (i = 0; i < argc; i++)
24008 xfree (argv[i]);
24009 }
24010 xfree (argv);
24011 }
24012 else
4d3c2250 24013 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
24014}
24015
cf2c3c16
TT
24016/* Skip some bytes from BYTES according to the form given in FORM.
24017 Returns the new pointer. */
2e276125 24018
d521ce57
TT
24019static const gdb_byte *
24020skip_form_bytes (bfd *abfd, const gdb_byte *bytes, const gdb_byte *buffer_end,
cf2c3c16
TT
24021 enum dwarf_form form,
24022 unsigned int offset_size,
24023 struct dwarf2_section_info *section)
2e276125 24024{
cf2c3c16 24025 unsigned int bytes_read;
2e276125 24026
cf2c3c16 24027 switch (form)
2e276125 24028 {
cf2c3c16
TT
24029 case DW_FORM_data1:
24030 case DW_FORM_flag:
24031 ++bytes;
24032 break;
24033
24034 case DW_FORM_data2:
24035 bytes += 2;
24036 break;
24037
24038 case DW_FORM_data4:
24039 bytes += 4;
24040 break;
24041
24042 case DW_FORM_data8:
24043 bytes += 8;
24044 break;
24045
0224619f
JK
24046 case DW_FORM_data16:
24047 bytes += 16;
24048 break;
24049
cf2c3c16
TT
24050 case DW_FORM_string:
24051 read_direct_string (abfd, bytes, &bytes_read);
24052 bytes += bytes_read;
24053 break;
24054
24055 case DW_FORM_sec_offset:
24056 case DW_FORM_strp:
36586728 24057 case DW_FORM_GNU_strp_alt:
cf2c3c16
TT
24058 bytes += offset_size;
24059 break;
24060
24061 case DW_FORM_block:
24062 bytes += read_unsigned_leb128 (abfd, bytes, &bytes_read);
24063 bytes += bytes_read;
24064 break;
24065
24066 case DW_FORM_block1:
24067 bytes += 1 + read_1_byte (abfd, bytes);
24068 break;
24069 case DW_FORM_block2:
24070 bytes += 2 + read_2_bytes (abfd, bytes);
24071 break;
24072 case DW_FORM_block4:
24073 bytes += 4 + read_4_bytes (abfd, bytes);
24074 break;
24075
24076 case DW_FORM_sdata:
24077 case DW_FORM_udata:
3019eac3
DE
24078 case DW_FORM_GNU_addr_index:
24079 case DW_FORM_GNU_str_index:
d521ce57 24080 bytes = gdb_skip_leb128 (bytes, buffer_end);
f664829e
DE
24081 if (bytes == NULL)
24082 {
24083 dwarf2_section_buffer_overflow_complaint (section);
24084 return NULL;
24085 }
cf2c3c16
TT
24086 break;
24087
663c44ac
JK
24088 case DW_FORM_implicit_const:
24089 break;
24090
cf2c3c16
TT
24091 default:
24092 {
cf2c3c16
TT
24093 complaint (&symfile_complaints,
24094 _("invalid form 0x%x in `%s'"),
a32a8923 24095 form, get_section_name (section));
cf2c3c16
TT
24096 return NULL;
24097 }
2e276125
JB
24098 }
24099
cf2c3c16
TT
24100 return bytes;
24101}
757a13d0 24102
cf2c3c16
TT
24103/* A helper for dwarf_decode_macros that handles skipping an unknown
24104 opcode. Returns an updated pointer to the macro data buffer; or,
24105 on error, issues a complaint and returns NULL. */
757a13d0 24106
d521ce57 24107static const gdb_byte *
cf2c3c16 24108skip_unknown_opcode (unsigned int opcode,
d521ce57
TT
24109 const gdb_byte **opcode_definitions,
24110 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
cf2c3c16
TT
24111 bfd *abfd,
24112 unsigned int offset_size,
24113 struct dwarf2_section_info *section)
24114{
24115 unsigned int bytes_read, i;
24116 unsigned long arg;
d521ce57 24117 const gdb_byte *defn;
2e276125 24118
cf2c3c16 24119 if (opcode_definitions[opcode] == NULL)
2e276125 24120 {
cf2c3c16
TT
24121 complaint (&symfile_complaints,
24122 _("unrecognized DW_MACFINO opcode 0x%x"),
24123 opcode);
24124 return NULL;
24125 }
2e276125 24126
cf2c3c16
TT
24127 defn = opcode_definitions[opcode];
24128 arg = read_unsigned_leb128 (abfd, defn, &bytes_read);
24129 defn += bytes_read;
2e276125 24130
cf2c3c16
TT
24131 for (i = 0; i < arg; ++i)
24132 {
aead7601
SM
24133 mac_ptr = skip_form_bytes (abfd, mac_ptr, mac_end,
24134 (enum dwarf_form) defn[i], offset_size,
f664829e 24135 section);
cf2c3c16
TT
24136 if (mac_ptr == NULL)
24137 {
24138 /* skip_form_bytes already issued the complaint. */
24139 return NULL;
24140 }
24141 }
757a13d0 24142
cf2c3c16
TT
24143 return mac_ptr;
24144}
757a13d0 24145
cf2c3c16
TT
24146/* A helper function which parses the header of a macro section.
24147 If the macro section is the extended (for now called "GNU") type,
24148 then this updates *OFFSET_SIZE. Returns a pointer to just after
24149 the header, or issues a complaint and returns NULL on error. */
757a13d0 24150
d521ce57
TT
24151static const gdb_byte *
24152dwarf_parse_macro_header (const gdb_byte **opcode_definitions,
cf2c3c16 24153 bfd *abfd,
d521ce57 24154 const gdb_byte *mac_ptr,
cf2c3c16
TT
24155 unsigned int *offset_size,
24156 int section_is_gnu)
24157{
24158 memset (opcode_definitions, 0, 256 * sizeof (gdb_byte *));
757a13d0 24159
cf2c3c16
TT
24160 if (section_is_gnu)
24161 {
24162 unsigned int version, flags;
757a13d0 24163
cf2c3c16 24164 version = read_2_bytes (abfd, mac_ptr);
0af92d60 24165 if (version != 4 && version != 5)
cf2c3c16
TT
24166 {
24167 complaint (&symfile_complaints,
24168 _("unrecognized version `%d' in .debug_macro section"),
24169 version);
24170 return NULL;
24171 }
24172 mac_ptr += 2;
757a13d0 24173
cf2c3c16
TT
24174 flags = read_1_byte (abfd, mac_ptr);
24175 ++mac_ptr;
24176 *offset_size = (flags & 1) ? 8 : 4;
757a13d0 24177
cf2c3c16
TT
24178 if ((flags & 2) != 0)
24179 /* We don't need the line table offset. */
24180 mac_ptr += *offset_size;
757a13d0 24181
cf2c3c16
TT
24182 /* Vendor opcode descriptions. */
24183 if ((flags & 4) != 0)
24184 {
24185 unsigned int i, count;
757a13d0 24186
cf2c3c16
TT
24187 count = read_1_byte (abfd, mac_ptr);
24188 ++mac_ptr;
24189 for (i = 0; i < count; ++i)
24190 {
24191 unsigned int opcode, bytes_read;
24192 unsigned long arg;
24193
24194 opcode = read_1_byte (abfd, mac_ptr);
24195 ++mac_ptr;
24196 opcode_definitions[opcode] = mac_ptr;
24197 arg = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24198 mac_ptr += bytes_read;
24199 mac_ptr += arg;
24200 }
757a13d0 24201 }
cf2c3c16 24202 }
757a13d0 24203
cf2c3c16
TT
24204 return mac_ptr;
24205}
757a13d0 24206
cf2c3c16 24207/* A helper for dwarf_decode_macros that handles the GNU extensions,
0af92d60 24208 including DW_MACRO_import. */
cf2c3c16
TT
24209
24210static void
ed2dc618
SM
24211dwarf_decode_macro_bytes (struct dwarf2_per_objfile *dwarf2_per_objfile,
24212 bfd *abfd,
d521ce57 24213 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
cf2c3c16 24214 struct macro_source_file *current_file,
43f3e411 24215 struct line_header *lh,
cf2c3c16 24216 struct dwarf2_section_info *section,
36586728 24217 int section_is_gnu, int section_is_dwz,
cf2c3c16 24218 unsigned int offset_size,
8fc3fc34 24219 htab_t include_hash)
cf2c3c16 24220{
4d663531 24221 struct objfile *objfile = dwarf2_per_objfile->objfile;
cf2c3c16
TT
24222 enum dwarf_macro_record_type macinfo_type;
24223 int at_commandline;
d521ce57 24224 const gdb_byte *opcode_definitions[256];
757a13d0 24225
cf2c3c16
TT
24226 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
24227 &offset_size, section_is_gnu);
24228 if (mac_ptr == NULL)
24229 {
24230 /* We already issued a complaint. */
24231 return;
24232 }
757a13d0
JK
24233
24234 /* Determines if GDB is still before first DW_MACINFO_start_file. If true
24235 GDB is still reading the definitions from command line. First
24236 DW_MACINFO_start_file will need to be ignored as it was already executed
24237 to create CURRENT_FILE for the main source holding also the command line
24238 definitions. On first met DW_MACINFO_start_file this flag is reset to
24239 normally execute all the remaining DW_MACINFO_start_file macinfos. */
24240
24241 at_commandline = 1;
24242
24243 do
24244 {
24245 /* Do we at least have room for a macinfo type byte? */
24246 if (mac_ptr >= mac_end)
24247 {
f664829e 24248 dwarf2_section_buffer_overflow_complaint (section);
757a13d0
JK
24249 break;
24250 }
24251
aead7601 24252 macinfo_type = (enum dwarf_macro_record_type) read_1_byte (abfd, mac_ptr);
757a13d0
JK
24253 mac_ptr++;
24254
cf2c3c16
TT
24255 /* Note that we rely on the fact that the corresponding GNU and
24256 DWARF constants are the same. */
132448f8
SM
24257 DIAGNOSTIC_PUSH
24258 DIAGNOSTIC_IGNORE_SWITCH_DIFFERENT_ENUM_TYPES
757a13d0
JK
24259 switch (macinfo_type)
24260 {
24261 /* A zero macinfo type indicates the end of the macro
24262 information. */
24263 case 0:
24264 break;
2e276125 24265
0af92d60
JK
24266 case DW_MACRO_define:
24267 case DW_MACRO_undef:
24268 case DW_MACRO_define_strp:
24269 case DW_MACRO_undef_strp:
24270 case DW_MACRO_define_sup:
24271 case DW_MACRO_undef_sup:
2e276125 24272 {
891d2f0b 24273 unsigned int bytes_read;
2e276125 24274 int line;
d521ce57 24275 const char *body;
cf2c3c16 24276 int is_define;
2e276125 24277
cf2c3c16
TT
24278 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24279 mac_ptr += bytes_read;
24280
0af92d60
JK
24281 if (macinfo_type == DW_MACRO_define
24282 || macinfo_type == DW_MACRO_undef)
cf2c3c16
TT
24283 {
24284 body = read_direct_string (abfd, mac_ptr, &bytes_read);
24285 mac_ptr += bytes_read;
24286 }
24287 else
24288 {
24289 LONGEST str_offset;
24290
24291 str_offset = read_offset_1 (abfd, mac_ptr, offset_size);
24292 mac_ptr += offset_size;
2e276125 24293
0af92d60
JK
24294 if (macinfo_type == DW_MACRO_define_sup
24295 || macinfo_type == DW_MACRO_undef_sup
f7a35f02 24296 || section_is_dwz)
36586728 24297 {
ed2dc618
SM
24298 struct dwz_file *dwz
24299 = dwarf2_get_dwz_file (dwarf2_per_objfile);
36586728 24300
ed2dc618
SM
24301 body = read_indirect_string_from_dwz (objfile,
24302 dwz, str_offset);
36586728
TT
24303 }
24304 else
ed2dc618
SM
24305 body = read_indirect_string_at_offset (dwarf2_per_objfile,
24306 abfd, str_offset);
cf2c3c16
TT
24307 }
24308
0af92d60
JK
24309 is_define = (macinfo_type == DW_MACRO_define
24310 || macinfo_type == DW_MACRO_define_strp
24311 || macinfo_type == DW_MACRO_define_sup);
2e276125 24312 if (! current_file)
757a13d0
JK
24313 {
24314 /* DWARF violation as no main source is present. */
24315 complaint (&symfile_complaints,
24316 _("debug info with no main source gives macro %s "
24317 "on line %d: %s"),
cf2c3c16
TT
24318 is_define ? _("definition") : _("undefinition"),
24319 line, body);
757a13d0
JK
24320 break;
24321 }
3e43a32a
MS
24322 if ((line == 0 && !at_commandline)
24323 || (line != 0 && at_commandline))
4d3c2250 24324 complaint (&symfile_complaints,
757a13d0
JK
24325 _("debug info gives %s macro %s with %s line %d: %s"),
24326 at_commandline ? _("command-line") : _("in-file"),
cf2c3c16 24327 is_define ? _("definition") : _("undefinition"),
757a13d0
JK
24328 line == 0 ? _("zero") : _("non-zero"), line, body);
24329
cf2c3c16 24330 if (is_define)
757a13d0 24331 parse_macro_definition (current_file, line, body);
cf2c3c16
TT
24332 else
24333 {
0af92d60
JK
24334 gdb_assert (macinfo_type == DW_MACRO_undef
24335 || macinfo_type == DW_MACRO_undef_strp
24336 || macinfo_type == DW_MACRO_undef_sup);
cf2c3c16
TT
24337 macro_undef (current_file, line, body);
24338 }
2e276125
JB
24339 }
24340 break;
24341
0af92d60 24342 case DW_MACRO_start_file:
2e276125 24343 {
891d2f0b 24344 unsigned int bytes_read;
2e276125
JB
24345 int line, file;
24346
24347 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24348 mac_ptr += bytes_read;
24349 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24350 mac_ptr += bytes_read;
24351
3e43a32a
MS
24352 if ((line == 0 && !at_commandline)
24353 || (line != 0 && at_commandline))
757a13d0
JK
24354 complaint (&symfile_complaints,
24355 _("debug info gives source %d included "
24356 "from %s at %s line %d"),
24357 file, at_commandline ? _("command-line") : _("file"),
24358 line == 0 ? _("zero") : _("non-zero"), line);
24359
24360 if (at_commandline)
24361 {
0af92d60 24362 /* This DW_MACRO_start_file was executed in the
cf2c3c16 24363 pass one. */
757a13d0
JK
24364 at_commandline = 0;
24365 }
24366 else
43f3e411 24367 current_file = macro_start_file (file, line, current_file, lh);
2e276125
JB
24368 }
24369 break;
24370
0af92d60 24371 case DW_MACRO_end_file:
2e276125 24372 if (! current_file)
4d3c2250 24373 complaint (&symfile_complaints,
3e43a32a
MS
24374 _("macro debug info has an unmatched "
24375 "`close_file' directive"));
2e276125
JB
24376 else
24377 {
24378 current_file = current_file->included_by;
24379 if (! current_file)
24380 {
cf2c3c16 24381 enum dwarf_macro_record_type next_type;
2e276125
JB
24382
24383 /* GCC circa March 2002 doesn't produce the zero
24384 type byte marking the end of the compilation
24385 unit. Complain if it's not there, but exit no
24386 matter what. */
24387
24388 /* Do we at least have room for a macinfo type byte? */
24389 if (mac_ptr >= mac_end)
24390 {
f664829e 24391 dwarf2_section_buffer_overflow_complaint (section);
2e276125
JB
24392 return;
24393 }
24394
24395 /* We don't increment mac_ptr here, so this is just
24396 a look-ahead. */
aead7601
SM
24397 next_type
24398 = (enum dwarf_macro_record_type) read_1_byte (abfd,
24399 mac_ptr);
2e276125 24400 if (next_type != 0)
4d3c2250 24401 complaint (&symfile_complaints,
3e43a32a
MS
24402 _("no terminating 0-type entry for "
24403 "macros in `.debug_macinfo' section"));
2e276125
JB
24404
24405 return;
24406 }
24407 }
24408 break;
24409
0af92d60
JK
24410 case DW_MACRO_import:
24411 case DW_MACRO_import_sup:
cf2c3c16
TT
24412 {
24413 LONGEST offset;
8fc3fc34 24414 void **slot;
a036ba48
TT
24415 bfd *include_bfd = abfd;
24416 struct dwarf2_section_info *include_section = section;
d521ce57 24417 const gdb_byte *include_mac_end = mac_end;
a036ba48 24418 int is_dwz = section_is_dwz;
d521ce57 24419 const gdb_byte *new_mac_ptr;
cf2c3c16
TT
24420
24421 offset = read_offset_1 (abfd, mac_ptr, offset_size);
24422 mac_ptr += offset_size;
24423
0af92d60 24424 if (macinfo_type == DW_MACRO_import_sup)
a036ba48 24425 {
ed2dc618 24426 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
a036ba48 24427
4d663531 24428 dwarf2_read_section (objfile, &dwz->macro);
a036ba48 24429
a036ba48 24430 include_section = &dwz->macro;
a32a8923 24431 include_bfd = get_section_bfd_owner (include_section);
a036ba48
TT
24432 include_mac_end = dwz->macro.buffer + dwz->macro.size;
24433 is_dwz = 1;
24434 }
24435
24436 new_mac_ptr = include_section->buffer + offset;
24437 slot = htab_find_slot (include_hash, new_mac_ptr, INSERT);
24438
8fc3fc34
TT
24439 if (*slot != NULL)
24440 {
24441 /* This has actually happened; see
24442 http://sourceware.org/bugzilla/show_bug.cgi?id=13568. */
24443 complaint (&symfile_complaints,
0af92d60 24444 _("recursive DW_MACRO_import in "
8fc3fc34
TT
24445 ".debug_macro section"));
24446 }
24447 else
24448 {
d521ce57 24449 *slot = (void *) new_mac_ptr;
36586728 24450
ed2dc618
SM
24451 dwarf_decode_macro_bytes (dwarf2_per_objfile,
24452 include_bfd, new_mac_ptr,
43f3e411 24453 include_mac_end, current_file, lh,
36586728 24454 section, section_is_gnu, is_dwz,
4d663531 24455 offset_size, include_hash);
8fc3fc34 24456
d521ce57 24457 htab_remove_elt (include_hash, (void *) new_mac_ptr);
8fc3fc34 24458 }
cf2c3c16
TT
24459 }
24460 break;
24461
2e276125 24462 case DW_MACINFO_vendor_ext:
cf2c3c16
TT
24463 if (!section_is_gnu)
24464 {
24465 unsigned int bytes_read;
2e276125 24466
ac298888
TT
24467 /* This reads the constant, but since we don't recognize
24468 any vendor extensions, we ignore it. */
24469 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
cf2c3c16
TT
24470 mac_ptr += bytes_read;
24471 read_direct_string (abfd, mac_ptr, &bytes_read);
24472 mac_ptr += bytes_read;
2e276125 24473
cf2c3c16
TT
24474 /* We don't recognize any vendor extensions. */
24475 break;
24476 }
24477 /* FALLTHROUGH */
24478
24479 default:
24480 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
f664829e 24481 mac_ptr, mac_end, abfd, offset_size,
cf2c3c16
TT
24482 section);
24483 if (mac_ptr == NULL)
24484 return;
24485 break;
2e276125 24486 }
132448f8 24487 DIAGNOSTIC_POP
757a13d0 24488 } while (macinfo_type != 0);
2e276125 24489}
8e19ed76 24490
cf2c3c16 24491static void
09262596 24492dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
43f3e411 24493 int section_is_gnu)
cf2c3c16 24494{
518817b3
SM
24495 struct dwarf2_per_objfile *dwarf2_per_objfile
24496 = cu->per_cu->dwarf2_per_objfile;
bb5ed363 24497 struct objfile *objfile = dwarf2_per_objfile->objfile;
09262596
DE
24498 struct line_header *lh = cu->line_header;
24499 bfd *abfd;
d521ce57 24500 const gdb_byte *mac_ptr, *mac_end;
cf2c3c16
TT
24501 struct macro_source_file *current_file = 0;
24502 enum dwarf_macro_record_type macinfo_type;
24503 unsigned int offset_size = cu->header.offset_size;
d521ce57 24504 const gdb_byte *opcode_definitions[256];
8fc3fc34 24505 void **slot;
09262596
DE
24506 struct dwarf2_section_info *section;
24507 const char *section_name;
24508
24509 if (cu->dwo_unit != NULL)
24510 {
24511 if (section_is_gnu)
24512 {
24513 section = &cu->dwo_unit->dwo_file->sections.macro;
24514 section_name = ".debug_macro.dwo";
24515 }
24516 else
24517 {
24518 section = &cu->dwo_unit->dwo_file->sections.macinfo;
24519 section_name = ".debug_macinfo.dwo";
24520 }
24521 }
24522 else
24523 {
24524 if (section_is_gnu)
24525 {
24526 section = &dwarf2_per_objfile->macro;
24527 section_name = ".debug_macro";
24528 }
24529 else
24530 {
24531 section = &dwarf2_per_objfile->macinfo;
24532 section_name = ".debug_macinfo";
24533 }
24534 }
cf2c3c16 24535
bb5ed363 24536 dwarf2_read_section (objfile, section);
cf2c3c16
TT
24537 if (section->buffer == NULL)
24538 {
fceca515 24539 complaint (&symfile_complaints, _("missing %s section"), section_name);
cf2c3c16
TT
24540 return;
24541 }
a32a8923 24542 abfd = get_section_bfd_owner (section);
cf2c3c16
TT
24543
24544 /* First pass: Find the name of the base filename.
24545 This filename is needed in order to process all macros whose definition
24546 (or undefinition) comes from the command line. These macros are defined
24547 before the first DW_MACINFO_start_file entry, and yet still need to be
24548 associated to the base file.
24549
24550 To determine the base file name, we scan the macro definitions until we
24551 reach the first DW_MACINFO_start_file entry. We then initialize
24552 CURRENT_FILE accordingly so that any macro definition found before the
24553 first DW_MACINFO_start_file can still be associated to the base file. */
24554
24555 mac_ptr = section->buffer + offset;
24556 mac_end = section->buffer + section->size;
24557
24558 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
24559 &offset_size, section_is_gnu);
24560 if (mac_ptr == NULL)
24561 {
24562 /* We already issued a complaint. */
24563 return;
24564 }
24565
24566 do
24567 {
24568 /* Do we at least have room for a macinfo type byte? */
24569 if (mac_ptr >= mac_end)
24570 {
24571 /* Complaint is printed during the second pass as GDB will probably
24572 stop the first pass earlier upon finding
24573 DW_MACINFO_start_file. */
24574 break;
24575 }
24576
aead7601 24577 macinfo_type = (enum dwarf_macro_record_type) read_1_byte (abfd, mac_ptr);
cf2c3c16
TT
24578 mac_ptr++;
24579
24580 /* Note that we rely on the fact that the corresponding GNU and
24581 DWARF constants are the same. */
132448f8
SM
24582 DIAGNOSTIC_PUSH
24583 DIAGNOSTIC_IGNORE_SWITCH_DIFFERENT_ENUM_TYPES
cf2c3c16
TT
24584 switch (macinfo_type)
24585 {
24586 /* A zero macinfo type indicates the end of the macro
24587 information. */
24588 case 0:
24589 break;
24590
0af92d60
JK
24591 case DW_MACRO_define:
24592 case DW_MACRO_undef:
cf2c3c16
TT
24593 /* Only skip the data by MAC_PTR. */
24594 {
24595 unsigned int bytes_read;
24596
24597 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24598 mac_ptr += bytes_read;
24599 read_direct_string (abfd, mac_ptr, &bytes_read);
24600 mac_ptr += bytes_read;
24601 }
24602 break;
24603
0af92d60 24604 case DW_MACRO_start_file:
cf2c3c16
TT
24605 {
24606 unsigned int bytes_read;
24607 int line, file;
24608
24609 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24610 mac_ptr += bytes_read;
24611 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24612 mac_ptr += bytes_read;
24613
43f3e411 24614 current_file = macro_start_file (file, line, current_file, lh);
cf2c3c16
TT
24615 }
24616 break;
24617
0af92d60 24618 case DW_MACRO_end_file:
cf2c3c16
TT
24619 /* No data to skip by MAC_PTR. */
24620 break;
24621
0af92d60
JK
24622 case DW_MACRO_define_strp:
24623 case DW_MACRO_undef_strp:
24624 case DW_MACRO_define_sup:
24625 case DW_MACRO_undef_sup:
cf2c3c16
TT
24626 {
24627 unsigned int bytes_read;
24628
24629 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24630 mac_ptr += bytes_read;
24631 mac_ptr += offset_size;
24632 }
24633 break;
24634
0af92d60
JK
24635 case DW_MACRO_import:
24636 case DW_MACRO_import_sup:
cf2c3c16 24637 /* Note that, according to the spec, a transparent include
0af92d60 24638 chain cannot call DW_MACRO_start_file. So, we can just
cf2c3c16
TT
24639 skip this opcode. */
24640 mac_ptr += offset_size;
24641 break;
24642
24643 case DW_MACINFO_vendor_ext:
24644 /* Only skip the data by MAC_PTR. */
24645 if (!section_is_gnu)
24646 {
24647 unsigned int bytes_read;
24648
24649 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24650 mac_ptr += bytes_read;
24651 read_direct_string (abfd, mac_ptr, &bytes_read);
24652 mac_ptr += bytes_read;
24653 }
24654 /* FALLTHROUGH */
24655
24656 default:
24657 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
f664829e 24658 mac_ptr, mac_end, abfd, offset_size,
cf2c3c16
TT
24659 section);
24660 if (mac_ptr == NULL)
24661 return;
24662 break;
24663 }
132448f8 24664 DIAGNOSTIC_POP
cf2c3c16
TT
24665 } while (macinfo_type != 0 && current_file == NULL);
24666
24667 /* Second pass: Process all entries.
24668
24669 Use the AT_COMMAND_LINE flag to determine whether we are still processing
24670 command-line macro definitions/undefinitions. This flag is unset when we
24671 reach the first DW_MACINFO_start_file entry. */
24672
fc4007c9
TT
24673 htab_up include_hash (htab_create_alloc (1, htab_hash_pointer,
24674 htab_eq_pointer,
24675 NULL, xcalloc, xfree));
8fc3fc34 24676 mac_ptr = section->buffer + offset;
fc4007c9 24677 slot = htab_find_slot (include_hash.get (), mac_ptr, INSERT);
d521ce57 24678 *slot = (void *) mac_ptr;
ed2dc618
SM
24679 dwarf_decode_macro_bytes (dwarf2_per_objfile,
24680 abfd, mac_ptr, mac_end,
43f3e411 24681 current_file, lh, section,
fc4007c9
TT
24682 section_is_gnu, 0, offset_size,
24683 include_hash.get ());
cf2c3c16
TT
24684}
24685
8e19ed76 24686/* Check if the attribute's form is a DW_FORM_block*
0963b4bd 24687 if so return true else false. */
380bca97 24688
8e19ed76 24689static int
6e5a29e1 24690attr_form_is_block (const struct attribute *attr)
8e19ed76
PS
24691{
24692 return (attr == NULL ? 0 :
24693 attr->form == DW_FORM_block1
24694 || attr->form == DW_FORM_block2
24695 || attr->form == DW_FORM_block4
2dc7f7b3
TT
24696 || attr->form == DW_FORM_block
24697 || attr->form == DW_FORM_exprloc);
8e19ed76 24698}
4c2df51b 24699
c6a0999f
JB
24700/* Return non-zero if ATTR's value is a section offset --- classes
24701 lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
24702 You may use DW_UNSND (attr) to retrieve such offsets.
24703
24704 Section 7.5.4, "Attribute Encodings", explains that no attribute
24705 may have a value that belongs to more than one of these classes; it
24706 would be ambiguous if we did, because we use the same forms for all
24707 of them. */
380bca97 24708
3690dd37 24709static int
6e5a29e1 24710attr_form_is_section_offset (const struct attribute *attr)
3690dd37
JB
24711{
24712 return (attr->form == DW_FORM_data4
2dc7f7b3
TT
24713 || attr->form == DW_FORM_data8
24714 || attr->form == DW_FORM_sec_offset);
3690dd37
JB
24715}
24716
3690dd37
JB
24717/* Return non-zero if ATTR's value falls in the 'constant' class, or
24718 zero otherwise. When this function returns true, you can apply
24719 dwarf2_get_attr_constant_value to it.
24720
24721 However, note that for some attributes you must check
24722 attr_form_is_section_offset before using this test. DW_FORM_data4
24723 and DW_FORM_data8 are members of both the constant class, and of
24724 the classes that contain offsets into other debug sections
24725 (lineptr, loclistptr, macptr or rangelistptr). The DWARF spec says
24726 that, if an attribute's can be either a constant or one of the
24727 section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
0224619f
JK
24728 taken as section offsets, not constants.
24729
24730 DW_FORM_data16 is not considered as dwarf2_get_attr_constant_value
24731 cannot handle that. */
380bca97 24732
3690dd37 24733static int
6e5a29e1 24734attr_form_is_constant (const struct attribute *attr)
3690dd37
JB
24735{
24736 switch (attr->form)
24737 {
24738 case DW_FORM_sdata:
24739 case DW_FORM_udata:
24740 case DW_FORM_data1:
24741 case DW_FORM_data2:
24742 case DW_FORM_data4:
24743 case DW_FORM_data8:
663c44ac 24744 case DW_FORM_implicit_const:
3690dd37
JB
24745 return 1;
24746 default:
24747 return 0;
24748 }
24749}
24750
7771576e
SA
24751
24752/* DW_ADDR is always stored already as sect_offset; despite for the forms
24753 besides DW_FORM_ref_addr it is stored as cu_offset in the DWARF file. */
24754
24755static int
6e5a29e1 24756attr_form_is_ref (const struct attribute *attr)
7771576e
SA
24757{
24758 switch (attr->form)
24759 {
24760 case DW_FORM_ref_addr:
24761 case DW_FORM_ref1:
24762 case DW_FORM_ref2:
24763 case DW_FORM_ref4:
24764 case DW_FORM_ref8:
24765 case DW_FORM_ref_udata:
24766 case DW_FORM_GNU_ref_alt:
24767 return 1;
24768 default:
24769 return 0;
24770 }
24771}
24772
3019eac3
DE
24773/* Return the .debug_loc section to use for CU.
24774 For DWO files use .debug_loc.dwo. */
24775
24776static struct dwarf2_section_info *
24777cu_debug_loc_section (struct dwarf2_cu *cu)
24778{
518817b3
SM
24779 struct dwarf2_per_objfile *dwarf2_per_objfile
24780 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 24781
3019eac3 24782 if (cu->dwo_unit)
43988095
JK
24783 {
24784 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
24785
24786 return cu->header.version >= 5 ? &sections->loclists : &sections->loc;
24787 }
24788 return (cu->header.version >= 5 ? &dwarf2_per_objfile->loclists
24789 : &dwarf2_per_objfile->loc);
3019eac3
DE
24790}
24791
8cf6f0b1
TT
24792/* A helper function that fills in a dwarf2_loclist_baton. */
24793
24794static void
24795fill_in_loclist_baton (struct dwarf2_cu *cu,
24796 struct dwarf2_loclist_baton *baton,
ff39bb5e 24797 const struct attribute *attr)
8cf6f0b1 24798{
518817b3
SM
24799 struct dwarf2_per_objfile *dwarf2_per_objfile
24800 = cu->per_cu->dwarf2_per_objfile;
3019eac3
DE
24801 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
24802
24803 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
8cf6f0b1
TT
24804
24805 baton->per_cu = cu->per_cu;
24806 gdb_assert (baton->per_cu);
24807 /* We don't know how long the location list is, but make sure we
24808 don't run off the edge of the section. */
3019eac3
DE
24809 baton->size = section->size - DW_UNSND (attr);
24810 baton->data = section->buffer + DW_UNSND (attr);
8cf6f0b1 24811 baton->base_address = cu->base_address;
f664829e 24812 baton->from_dwo = cu->dwo_unit != NULL;
8cf6f0b1
TT
24813}
24814
4c2df51b 24815static void
ff39bb5e 24816dwarf2_symbol_mark_computed (const struct attribute *attr, struct symbol *sym,
f1e6e072 24817 struct dwarf2_cu *cu, int is_block)
4c2df51b 24818{
518817b3
SM
24819 struct dwarf2_per_objfile *dwarf2_per_objfile
24820 = cu->per_cu->dwarf2_per_objfile;
bb5ed363 24821 struct objfile *objfile = dwarf2_per_objfile->objfile;
3019eac3 24822 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
bb5ed363 24823
3690dd37 24824 if (attr_form_is_section_offset (attr)
3019eac3 24825 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
99bcc461
DJ
24826 the section. If so, fall through to the complaint in the
24827 other branch. */
3019eac3 24828 && DW_UNSND (attr) < dwarf2_section_size (objfile, section))
4c2df51b 24829 {
0d53c4c4 24830 struct dwarf2_loclist_baton *baton;
4c2df51b 24831
8d749320 24832 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_loclist_baton);
4c2df51b 24833
8cf6f0b1 24834 fill_in_loclist_baton (cu, baton, attr);
be391dca 24835
d00adf39 24836 if (cu->base_known == 0)
0d53c4c4 24837 complaint (&symfile_complaints,
3e43a32a
MS
24838 _("Location list used without "
24839 "specifying the CU base address."));
4c2df51b 24840
f1e6e072
TT
24841 SYMBOL_ACLASS_INDEX (sym) = (is_block
24842 ? dwarf2_loclist_block_index
24843 : dwarf2_loclist_index);
0d53c4c4
DJ
24844 SYMBOL_LOCATION_BATON (sym) = baton;
24845 }
24846 else
24847 {
24848 struct dwarf2_locexpr_baton *baton;
24849
8d749320 24850 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
ae0d2f24
UW
24851 baton->per_cu = cu->per_cu;
24852 gdb_assert (baton->per_cu);
0d53c4c4
DJ
24853
24854 if (attr_form_is_block (attr))
24855 {
24856 /* Note that we're just copying the block's data pointer
24857 here, not the actual data. We're still pointing into the
6502dd73
DJ
24858 info_buffer for SYM's objfile; right now we never release
24859 that buffer, but when we do clean up properly this may
24860 need to change. */
0d53c4c4
DJ
24861 baton->size = DW_BLOCK (attr)->size;
24862 baton->data = DW_BLOCK (attr)->data;
24863 }
24864 else
24865 {
24866 dwarf2_invalid_attrib_class_complaint ("location description",
24867 SYMBOL_NATURAL_NAME (sym));
24868 baton->size = 0;
0d53c4c4 24869 }
6e70227d 24870
f1e6e072
TT
24871 SYMBOL_ACLASS_INDEX (sym) = (is_block
24872 ? dwarf2_locexpr_block_index
24873 : dwarf2_locexpr_index);
0d53c4c4
DJ
24874 SYMBOL_LOCATION_BATON (sym) = baton;
24875 }
4c2df51b 24876}
6502dd73 24877
9aa1f1e3
TT
24878/* Return the OBJFILE associated with the compilation unit CU. If CU
24879 came from a separate debuginfo file, then the master objfile is
24880 returned. */
ae0d2f24
UW
24881
24882struct objfile *
24883dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
24884{
e3b94546 24885 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
ae0d2f24
UW
24886
24887 /* Return the master objfile, so that we can report and look up the
24888 correct file containing this variable. */
24889 if (objfile->separate_debug_objfile_backlink)
24890 objfile = objfile->separate_debug_objfile_backlink;
24891
24892 return objfile;
24893}
24894
96408a79
SA
24895/* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
24896 (CU_HEADERP is unused in such case) or prepare a temporary copy at
24897 CU_HEADERP first. */
24898
24899static const struct comp_unit_head *
24900per_cu_header_read_in (struct comp_unit_head *cu_headerp,
24901 struct dwarf2_per_cu_data *per_cu)
24902{
d521ce57 24903 const gdb_byte *info_ptr;
96408a79
SA
24904
24905 if (per_cu->cu)
24906 return &per_cu->cu->header;
24907
9c541725 24908 info_ptr = per_cu->section->buffer + to_underlying (per_cu->sect_off);
96408a79
SA
24909
24910 memset (cu_headerp, 0, sizeof (*cu_headerp));
43988095
JK
24911 read_comp_unit_head (cu_headerp, info_ptr, per_cu->section,
24912 rcuh_kind::COMPILE);
96408a79
SA
24913
24914 return cu_headerp;
24915}
24916
ae0d2f24
UW
24917/* Return the address size given in the compilation unit header for CU. */
24918
98714339 24919int
ae0d2f24
UW
24920dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
24921{
96408a79
SA
24922 struct comp_unit_head cu_header_local;
24923 const struct comp_unit_head *cu_headerp;
c471e790 24924
96408a79
SA
24925 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
24926
24927 return cu_headerp->addr_size;
ae0d2f24
UW
24928}
24929
9eae7c52
TT
24930/* Return the offset size given in the compilation unit header for CU. */
24931
24932int
24933dwarf2_per_cu_offset_size (struct dwarf2_per_cu_data *per_cu)
24934{
96408a79
SA
24935 struct comp_unit_head cu_header_local;
24936 const struct comp_unit_head *cu_headerp;
9c6c53f7 24937
96408a79
SA
24938 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
24939
24940 return cu_headerp->offset_size;
24941}
24942
24943/* See its dwarf2loc.h declaration. */
24944
24945int
24946dwarf2_per_cu_ref_addr_size (struct dwarf2_per_cu_data *per_cu)
24947{
24948 struct comp_unit_head cu_header_local;
24949 const struct comp_unit_head *cu_headerp;
24950
24951 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
24952
24953 if (cu_headerp->version == 2)
24954 return cu_headerp->addr_size;
24955 else
24956 return cu_headerp->offset_size;
181cebd4
JK
24957}
24958
9aa1f1e3
TT
24959/* Return the text offset of the CU. The returned offset comes from
24960 this CU's objfile. If this objfile came from a separate debuginfo
24961 file, then the offset may be different from the corresponding
24962 offset in the parent objfile. */
24963
24964CORE_ADDR
24965dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
24966{
e3b94546 24967 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
9aa1f1e3
TT
24968
24969 return ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
24970}
24971
43988095
JK
24972/* Return DWARF version number of PER_CU. */
24973
24974short
24975dwarf2_version (struct dwarf2_per_cu_data *per_cu)
24976{
24977 return per_cu->dwarf_version;
24978}
24979
348e048f
DE
24980/* Locate the .debug_info compilation unit from CU's objfile which contains
24981 the DIE at OFFSET. Raises an error on failure. */
ae038cb0
DJ
24982
24983static struct dwarf2_per_cu_data *
9c541725 24984dwarf2_find_containing_comp_unit (sect_offset sect_off,
36586728 24985 unsigned int offset_in_dwz,
ed2dc618 24986 struct dwarf2_per_objfile *dwarf2_per_objfile)
ae038cb0
DJ
24987{
24988 struct dwarf2_per_cu_data *this_cu;
24989 int low, high;
36586728 24990 const sect_offset *cu_off;
ae038cb0 24991
ae038cb0
DJ
24992 low = 0;
24993 high = dwarf2_per_objfile->n_comp_units - 1;
24994 while (high > low)
24995 {
36586728 24996 struct dwarf2_per_cu_data *mid_cu;
ae038cb0 24997 int mid = low + (high - low) / 2;
9a619af0 24998
36586728 24999 mid_cu = dwarf2_per_objfile->all_comp_units[mid];
9c541725 25000 cu_off = &mid_cu->sect_off;
36586728 25001 if (mid_cu->is_dwz > offset_in_dwz
9c541725 25002 || (mid_cu->is_dwz == offset_in_dwz && *cu_off >= sect_off))
ae038cb0
DJ
25003 high = mid;
25004 else
25005 low = mid + 1;
25006 }
25007 gdb_assert (low == high);
36586728 25008 this_cu = dwarf2_per_objfile->all_comp_units[low];
9c541725
PA
25009 cu_off = &this_cu->sect_off;
25010 if (this_cu->is_dwz != offset_in_dwz || *cu_off > sect_off)
ae038cb0 25011 {
36586728 25012 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
8a3fe4f8 25013 error (_("Dwarf Error: could not find partial DIE containing "
9d8780f0
SM
25014 "offset %s [in module %s]"),
25015 sect_offset_str (sect_off),
ed2dc618 25016 bfd_get_filename (dwarf2_per_objfile->objfile->obfd));
10b3939b 25017
9c541725
PA
25018 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->sect_off
25019 <= sect_off);
ae038cb0
DJ
25020 return dwarf2_per_objfile->all_comp_units[low-1];
25021 }
25022 else
25023 {
25024 this_cu = dwarf2_per_objfile->all_comp_units[low];
25025 if (low == dwarf2_per_objfile->n_comp_units - 1
9c541725 25026 && sect_off >= this_cu->sect_off + this_cu->length)
9d8780f0 25027 error (_("invalid dwarf2 offset %s"), sect_offset_str (sect_off));
9c541725 25028 gdb_assert (sect_off < this_cu->sect_off + this_cu->length);
ae038cb0
DJ
25029 return this_cu;
25030 }
25031}
25032
23745b47 25033/* Initialize dwarf2_cu CU, owned by PER_CU. */
93311388 25034
fcd3b13d
SM
25035dwarf2_cu::dwarf2_cu (struct dwarf2_per_cu_data *per_cu_)
25036 : per_cu (per_cu_),
25037 mark (0),
25038 has_loclist (0),
25039 checked_producer (0),
25040 producer_is_gxx_lt_4_6 (0),
25041 producer_is_gcc_lt_4_3 (0),
25042 producer_is_icc_lt_14 (0),
25043 processing_has_namespace_info (0)
93311388 25044{
fcd3b13d
SM
25045 per_cu->cu = this;
25046}
25047
25048/* Destroy a dwarf2_cu. */
25049
25050dwarf2_cu::~dwarf2_cu ()
25051{
25052 per_cu->cu = NULL;
9816fde3
JK
25053}
25054
25055/* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
25056
25057static void
95554aad
TT
25058prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
25059 enum language pretend_language)
9816fde3
JK
25060{
25061 struct attribute *attr;
25062
25063 /* Set the language we're debugging. */
25064 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
25065 if (attr)
25066 set_cu_language (DW_UNSND (attr), cu);
25067 else
9cded63f 25068 {
95554aad 25069 cu->language = pretend_language;
9cded63f
TT
25070 cu->language_defn = language_def (cu->language);
25071 }
dee91e82 25072
7d45c7c3 25073 cu->producer = dwarf2_string_attr (comp_unit_die, DW_AT_producer, cu);
93311388
DE
25074}
25075
ae038cb0
DJ
25076/* Increase the age counter on each cached compilation unit, and free
25077 any that are too old. */
25078
25079static void
ed2dc618 25080age_cached_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
ae038cb0
DJ
25081{
25082 struct dwarf2_per_cu_data *per_cu, **last_chain;
25083
25084 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
25085 per_cu = dwarf2_per_objfile->read_in_chain;
25086 while (per_cu != NULL)
25087 {
25088 per_cu->cu->last_used ++;
b4f54984 25089 if (per_cu->cu->last_used <= dwarf_max_cache_age)
ae038cb0
DJ
25090 dwarf2_mark (per_cu->cu);
25091 per_cu = per_cu->cu->read_in_chain;
25092 }
25093
25094 per_cu = dwarf2_per_objfile->read_in_chain;
25095 last_chain = &dwarf2_per_objfile->read_in_chain;
25096 while (per_cu != NULL)
25097 {
25098 struct dwarf2_per_cu_data *next_cu;
25099
25100 next_cu = per_cu->cu->read_in_chain;
25101
25102 if (!per_cu->cu->mark)
25103 {
fcd3b13d 25104 delete per_cu->cu;
ae038cb0
DJ
25105 *last_chain = next_cu;
25106 }
25107 else
25108 last_chain = &per_cu->cu->read_in_chain;
25109
25110 per_cu = next_cu;
25111 }
25112}
25113
25114/* Remove a single compilation unit from the cache. */
25115
25116static void
dee91e82 25117free_one_cached_comp_unit (struct dwarf2_per_cu_data *target_per_cu)
ae038cb0
DJ
25118{
25119 struct dwarf2_per_cu_data *per_cu, **last_chain;
ed2dc618
SM
25120 struct dwarf2_per_objfile *dwarf2_per_objfile
25121 = target_per_cu->dwarf2_per_objfile;
ae038cb0
DJ
25122
25123 per_cu = dwarf2_per_objfile->read_in_chain;
25124 last_chain = &dwarf2_per_objfile->read_in_chain;
25125 while (per_cu != NULL)
25126 {
25127 struct dwarf2_per_cu_data *next_cu;
25128
25129 next_cu = per_cu->cu->read_in_chain;
25130
dee91e82 25131 if (per_cu == target_per_cu)
ae038cb0 25132 {
fcd3b13d 25133 delete per_cu->cu;
dee91e82 25134 per_cu->cu = NULL;
ae038cb0
DJ
25135 *last_chain = next_cu;
25136 break;
25137 }
25138 else
25139 last_chain = &per_cu->cu->read_in_chain;
25140
25141 per_cu = next_cu;
25142 }
25143}
25144
fe3e1990
DJ
25145/* Release all extra memory associated with OBJFILE. */
25146
25147void
25148dwarf2_free_objfile (struct objfile *objfile)
25149{
ed2dc618
SM
25150 struct dwarf2_per_objfile *dwarf2_per_objfile
25151 = get_dwarf2_per_objfile (objfile);
fe3e1990 25152
fd90ace4 25153 delete dwarf2_per_objfile;
fe3e1990
DJ
25154}
25155
dee91e82
DE
25156/* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
25157 We store these in a hash table separate from the DIEs, and preserve them
25158 when the DIEs are flushed out of cache.
25159
25160 The CU "per_cu" pointer is needed because offset alone is not enough to
3019eac3 25161 uniquely identify the type. A file may have multiple .debug_types sections,
c88ee1f0
DE
25162 or the type may come from a DWO file. Furthermore, while it's more logical
25163 to use per_cu->section+offset, with Fission the section with the data is in
25164 the DWO file but we don't know that section at the point we need it.
25165 We have to use something in dwarf2_per_cu_data (or the pointer to it)
25166 because we can enter the lookup routine, get_die_type_at_offset, from
25167 outside this file, and thus won't necessarily have PER_CU->cu.
25168 Fortunately, PER_CU is stable for the life of the objfile. */
1c379e20 25169
dee91e82 25170struct dwarf2_per_cu_offset_and_type
1c379e20 25171{
dee91e82 25172 const struct dwarf2_per_cu_data *per_cu;
9c541725 25173 sect_offset sect_off;
1c379e20
DJ
25174 struct type *type;
25175};
25176
dee91e82 25177/* Hash function for a dwarf2_per_cu_offset_and_type. */
1c379e20
DJ
25178
25179static hashval_t
dee91e82 25180per_cu_offset_and_type_hash (const void *item)
1c379e20 25181{
9a3c8263
SM
25182 const struct dwarf2_per_cu_offset_and_type *ofs
25183 = (const struct dwarf2_per_cu_offset_and_type *) item;
9a619af0 25184
9c541725 25185 return (uintptr_t) ofs->per_cu + to_underlying (ofs->sect_off);
1c379e20
DJ
25186}
25187
dee91e82 25188/* Equality function for a dwarf2_per_cu_offset_and_type. */
1c379e20
DJ
25189
25190static int
dee91e82 25191per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
1c379e20 25192{
9a3c8263
SM
25193 const struct dwarf2_per_cu_offset_and_type *ofs_lhs
25194 = (const struct dwarf2_per_cu_offset_and_type *) item_lhs;
25195 const struct dwarf2_per_cu_offset_and_type *ofs_rhs
25196 = (const struct dwarf2_per_cu_offset_and_type *) item_rhs;
9a619af0 25197
dee91e82 25198 return (ofs_lhs->per_cu == ofs_rhs->per_cu
9c541725 25199 && ofs_lhs->sect_off == ofs_rhs->sect_off);
1c379e20
DJ
25200}
25201
25202/* Set the type associated with DIE to TYPE. Save it in CU's hash
7e314c57
JK
25203 table if necessary. For convenience, return TYPE.
25204
25205 The DIEs reading must have careful ordering to:
25206 * Not cause infite loops trying to read in DIEs as a prerequisite for
25207 reading current DIE.
25208 * Not trying to dereference contents of still incompletely read in types
25209 while reading in other DIEs.
25210 * Enable referencing still incompletely read in types just by a pointer to
25211 the type without accessing its fields.
25212
25213 Therefore caller should follow these rules:
25214 * Try to fetch any prerequisite types we may need to build this DIE type
25215 before building the type and calling set_die_type.
e71ec853 25216 * After building type call set_die_type for current DIE as soon as
7e314c57
JK
25217 possible before fetching more types to complete the current type.
25218 * Make the type as complete as possible before fetching more types. */
1c379e20 25219
f792889a 25220static struct type *
1c379e20
DJ
25221set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
25222{
518817b3
SM
25223 struct dwarf2_per_objfile *dwarf2_per_objfile
25224 = cu->per_cu->dwarf2_per_objfile;
dee91e82 25225 struct dwarf2_per_cu_offset_and_type **slot, ofs;
ed2dc618 25226 struct objfile *objfile = dwarf2_per_objfile->objfile;
3cdcd0ce
JB
25227 struct attribute *attr;
25228 struct dynamic_prop prop;
1c379e20 25229
b4ba55a1
JB
25230 /* For Ada types, make sure that the gnat-specific data is always
25231 initialized (if not already set). There are a few types where
25232 we should not be doing so, because the type-specific area is
25233 already used to hold some other piece of info (eg: TYPE_CODE_FLT
25234 where the type-specific area is used to store the floatformat).
25235 But this is not a problem, because the gnat-specific information
25236 is actually not needed for these types. */
25237 if (need_gnat_info (cu)
25238 && TYPE_CODE (type) != TYPE_CODE_FUNC
25239 && TYPE_CODE (type) != TYPE_CODE_FLT
09e2d7c7
DE
25240 && TYPE_CODE (type) != TYPE_CODE_METHODPTR
25241 && TYPE_CODE (type) != TYPE_CODE_MEMBERPTR
25242 && TYPE_CODE (type) != TYPE_CODE_METHOD
b4ba55a1
JB
25243 && !HAVE_GNAT_AUX_INFO (type))
25244 INIT_GNAT_SPECIFIC (type);
25245
3f2f83dd
KB
25246 /* Read DW_AT_allocated and set in type. */
25247 attr = dwarf2_attr (die, DW_AT_allocated, cu);
25248 if (attr_form_is_block (attr))
25249 {
25250 if (attr_to_dynamic_prop (attr, die, cu, &prop))
50a82047 25251 add_dyn_prop (DYN_PROP_ALLOCATED, prop, type);
3f2f83dd
KB
25252 }
25253 else if (attr != NULL)
25254 {
25255 complaint (&symfile_complaints,
9d8780f0 25256 _("DW_AT_allocated has the wrong form (%s) at DIE %s"),
9c541725 25257 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
9d8780f0 25258 sect_offset_str (die->sect_off));
3f2f83dd
KB
25259 }
25260
25261 /* Read DW_AT_associated and set in type. */
25262 attr = dwarf2_attr (die, DW_AT_associated, cu);
25263 if (attr_form_is_block (attr))
25264 {
25265 if (attr_to_dynamic_prop (attr, die, cu, &prop))
50a82047 25266 add_dyn_prop (DYN_PROP_ASSOCIATED, prop, type);
3f2f83dd
KB
25267 }
25268 else if (attr != NULL)
25269 {
25270 complaint (&symfile_complaints,
9d8780f0 25271 _("DW_AT_associated has the wrong form (%s) at DIE %s"),
9c541725 25272 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
9d8780f0 25273 sect_offset_str (die->sect_off));
3f2f83dd
KB
25274 }
25275
3cdcd0ce
JB
25276 /* Read DW_AT_data_location and set in type. */
25277 attr = dwarf2_attr (die, DW_AT_data_location, cu);
25278 if (attr_to_dynamic_prop (attr, die, cu, &prop))
50a82047 25279 add_dyn_prop (DYN_PROP_DATA_LOCATION, prop, type);
3cdcd0ce 25280
dee91e82 25281 if (dwarf2_per_objfile->die_type_hash == NULL)
f792889a 25282 {
dee91e82
DE
25283 dwarf2_per_objfile->die_type_hash =
25284 htab_create_alloc_ex (127,
25285 per_cu_offset_and_type_hash,
25286 per_cu_offset_and_type_eq,
25287 NULL,
25288 &objfile->objfile_obstack,
25289 hashtab_obstack_allocate,
25290 dummy_obstack_deallocate);
f792889a 25291 }
1c379e20 25292
dee91e82 25293 ofs.per_cu = cu->per_cu;
9c541725 25294 ofs.sect_off = die->sect_off;
1c379e20 25295 ofs.type = type;
dee91e82
DE
25296 slot = (struct dwarf2_per_cu_offset_and_type **)
25297 htab_find_slot (dwarf2_per_objfile->die_type_hash, &ofs, INSERT);
7e314c57
JK
25298 if (*slot)
25299 complaint (&symfile_complaints,
9d8780f0
SM
25300 _("A problem internal to GDB: DIE %s has type already set"),
25301 sect_offset_str (die->sect_off));
8d749320
SM
25302 *slot = XOBNEW (&objfile->objfile_obstack,
25303 struct dwarf2_per_cu_offset_and_type);
1c379e20 25304 **slot = ofs;
f792889a 25305 return type;
1c379e20
DJ
25306}
25307
9c541725 25308/* Look up the type for the die at SECT_OFF in PER_CU in die_type_hash,
02142a6c 25309 or return NULL if the die does not have a saved type. */
1c379e20
DJ
25310
25311static struct type *
9c541725 25312get_die_type_at_offset (sect_offset sect_off,
673bfd45 25313 struct dwarf2_per_cu_data *per_cu)
1c379e20 25314{
dee91e82 25315 struct dwarf2_per_cu_offset_and_type *slot, ofs;
ed2dc618 25316 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
f792889a 25317
dee91e82 25318 if (dwarf2_per_objfile->die_type_hash == NULL)
f792889a 25319 return NULL;
1c379e20 25320
dee91e82 25321 ofs.per_cu = per_cu;
9c541725 25322 ofs.sect_off = sect_off;
9a3c8263
SM
25323 slot = ((struct dwarf2_per_cu_offset_and_type *)
25324 htab_find (dwarf2_per_objfile->die_type_hash, &ofs));
1c379e20
DJ
25325 if (slot)
25326 return slot->type;
25327 else
25328 return NULL;
25329}
25330
02142a6c 25331/* Look up the type for DIE in CU in die_type_hash,
673bfd45
DE
25332 or return NULL if DIE does not have a saved type. */
25333
25334static struct type *
25335get_die_type (struct die_info *die, struct dwarf2_cu *cu)
25336{
9c541725 25337 return get_die_type_at_offset (die->sect_off, cu->per_cu);
673bfd45
DE
25338}
25339
10b3939b
DJ
25340/* Add a dependence relationship from CU to REF_PER_CU. */
25341
25342static void
25343dwarf2_add_dependence (struct dwarf2_cu *cu,
25344 struct dwarf2_per_cu_data *ref_per_cu)
25345{
25346 void **slot;
25347
25348 if (cu->dependencies == NULL)
25349 cu->dependencies
25350 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
25351 NULL, &cu->comp_unit_obstack,
25352 hashtab_obstack_allocate,
25353 dummy_obstack_deallocate);
25354
25355 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
25356 if (*slot == NULL)
25357 *slot = ref_per_cu;
25358}
1c379e20 25359
f504f079
DE
25360/* Subroutine of dwarf2_mark to pass to htab_traverse.
25361 Set the mark field in every compilation unit in the
ae038cb0
DJ
25362 cache that we must keep because we are keeping CU. */
25363
10b3939b
DJ
25364static int
25365dwarf2_mark_helper (void **slot, void *data)
25366{
25367 struct dwarf2_per_cu_data *per_cu;
25368
25369 per_cu = (struct dwarf2_per_cu_data *) *slot;
d07ed419
JK
25370
25371 /* cu->dependencies references may not yet have been ever read if QUIT aborts
25372 reading of the chain. As such dependencies remain valid it is not much
25373 useful to track and undo them during QUIT cleanups. */
25374 if (per_cu->cu == NULL)
25375 return 1;
25376
10b3939b
DJ
25377 if (per_cu->cu->mark)
25378 return 1;
25379 per_cu->cu->mark = 1;
25380
25381 if (per_cu->cu->dependencies != NULL)
25382 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
25383
25384 return 1;
25385}
25386
f504f079
DE
25387/* Set the mark field in CU and in every other compilation unit in the
25388 cache that we must keep because we are keeping CU. */
25389
ae038cb0
DJ
25390static void
25391dwarf2_mark (struct dwarf2_cu *cu)
25392{
25393 if (cu->mark)
25394 return;
25395 cu->mark = 1;
10b3939b
DJ
25396 if (cu->dependencies != NULL)
25397 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
ae038cb0
DJ
25398}
25399
25400static void
25401dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
25402{
25403 while (per_cu)
25404 {
25405 per_cu->cu->mark = 0;
25406 per_cu = per_cu->cu->read_in_chain;
25407 }
72bf9492
DJ
25408}
25409
72bf9492
DJ
25410/* Trivial hash function for partial_die_info: the hash value of a DIE
25411 is its offset in .debug_info for this objfile. */
25412
25413static hashval_t
25414partial_die_hash (const void *item)
25415{
9a3c8263
SM
25416 const struct partial_die_info *part_die
25417 = (const struct partial_die_info *) item;
9a619af0 25418
9c541725 25419 return to_underlying (part_die->sect_off);
72bf9492
DJ
25420}
25421
25422/* Trivial comparison function for partial_die_info structures: two DIEs
25423 are equal if they have the same offset. */
25424
25425static int
25426partial_die_eq (const void *item_lhs, const void *item_rhs)
25427{
9a3c8263
SM
25428 const struct partial_die_info *part_die_lhs
25429 = (const struct partial_die_info *) item_lhs;
25430 const struct partial_die_info *part_die_rhs
25431 = (const struct partial_die_info *) item_rhs;
9a619af0 25432
9c541725 25433 return part_die_lhs->sect_off == part_die_rhs->sect_off;
72bf9492
DJ
25434}
25435
b4f54984
DE
25436static struct cmd_list_element *set_dwarf_cmdlist;
25437static struct cmd_list_element *show_dwarf_cmdlist;
ae038cb0
DJ
25438
25439static void
981a3fb3 25440set_dwarf_cmd (const char *args, int from_tty)
ae038cb0 25441{
b4f54984 25442 help_list (set_dwarf_cmdlist, "maintenance set dwarf ", all_commands,
635c7e8a 25443 gdb_stdout);
ae038cb0
DJ
25444}
25445
25446static void
981a3fb3 25447show_dwarf_cmd (const char *args, int from_tty)
6e70227d 25448{
b4f54984 25449 cmd_show_list (show_dwarf_cmdlist, from_tty, "");
ae038cb0
DJ
25450}
25451
cd4fb1b2 25452int dwarf_always_disassemble;
437afbb8 25453
437afbb8 25454static void
cd4fb1b2
SM
25455show_dwarf_always_disassemble (struct ui_file *file, int from_tty,
25456 struct cmd_list_element *c, const char *value)
9291a0cd 25457{
cd4fb1b2
SM
25458 fprintf_filtered (file,
25459 _("Whether to always disassemble "
25460 "DWARF expressions is %s.\n"),
25461 value);
9291a0cd
TT
25462}
25463
9291a0cd 25464static void
cd4fb1b2
SM
25465show_check_physname (struct ui_file *file, int from_tty,
25466 struct cmd_list_element *c, const char *value)
9291a0cd 25467{
cd4fb1b2
SM
25468 fprintf_filtered (file,
25469 _("Whether to check \"physname\" is %s.\n"),
25470 value);
9291a0cd
TT
25471}
25472
cd4fb1b2
SM
25473void
25474_initialize_dwarf2_read (void)
9291a0cd 25475{
9291a0cd 25476
cd4fb1b2 25477 dwarf2_objfile_data_key = register_objfile_data ();
156942c7 25478
cd4fb1b2
SM
25479 add_prefix_cmd ("dwarf", class_maintenance, set_dwarf_cmd, _("\
25480Set DWARF specific variables.\n\
25481Configure DWARF variables such as the cache size"),
25482 &set_dwarf_cmdlist, "maintenance set dwarf ",
25483 0/*allow-unknown*/, &maintenance_set_cmdlist);
156942c7 25484
cd4fb1b2
SM
25485 add_prefix_cmd ("dwarf", class_maintenance, show_dwarf_cmd, _("\
25486Show DWARF specific variables\n\
25487Show DWARF variables such as the cache size"),
25488 &show_dwarf_cmdlist, "maintenance show dwarf ",
25489 0/*allow-unknown*/, &maintenance_show_cmdlist);
156942c7 25490
cd4fb1b2
SM
25491 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
25492 &dwarf_max_cache_age, _("\
25493Set the upper bound on the age of cached DWARF compilation units."), _("\
25494Show the upper bound on the age of cached DWARF compilation units."), _("\
25495A higher limit means that cached compilation units will be stored\n\
25496in memory longer, and more total memory will be used. Zero disables\n\
25497caching, which can slow down startup."),
25498 NULL,
25499 show_dwarf_max_cache_age,
25500 &set_dwarf_cmdlist,
25501 &show_dwarf_cmdlist);
156942c7 25502
cd4fb1b2
SM
25503 add_setshow_boolean_cmd ("always-disassemble", class_obscure,
25504 &dwarf_always_disassemble, _("\
25505Set whether `info address' always disassembles DWARF expressions."), _("\
25506Show whether `info address' always disassembles DWARF expressions."), _("\
25507When enabled, DWARF expressions are always printed in an assembly-like\n\
25508syntax. When disabled, expressions will be printed in a more\n\
25509conversational style, when possible."),
25510 NULL,
25511 show_dwarf_always_disassemble,
25512 &set_dwarf_cmdlist,
25513 &show_dwarf_cmdlist);
9291a0cd 25514
cd4fb1b2
SM
25515 add_setshow_zuinteger_cmd ("dwarf-read", no_class, &dwarf_read_debug, _("\
25516Set debugging of the DWARF reader."), _("\
25517Show debugging of the DWARF reader."), _("\
25518When enabled (non-zero), debugging messages are printed during DWARF\n\
25519reading and symtab expansion. A value of 1 (one) provides basic\n\
25520information. A value greater than 1 provides more verbose information."),
25521 NULL,
25522 NULL,
25523 &setdebuglist, &showdebuglist);
9291a0cd 25524
cd4fb1b2
SM
25525 add_setshow_zuinteger_cmd ("dwarf-die", no_class, &dwarf_die_debug, _("\
25526Set debugging of the DWARF DIE reader."), _("\
25527Show debugging of the DWARF DIE reader."), _("\
25528When enabled (non-zero), DIEs are dumped after they are read in.\n\
25529The value is the maximum depth to print."),
25530 NULL,
25531 NULL,
25532 &setdebuglist, &showdebuglist);
9291a0cd 25533
cd4fb1b2
SM
25534 add_setshow_zuinteger_cmd ("dwarf-line", no_class, &dwarf_line_debug, _("\
25535Set debugging of the dwarf line reader."), _("\
25536Show debugging of the dwarf line reader."), _("\
25537When enabled (non-zero), line number entries are dumped as they are read in.\n\
25538A value of 1 (one) provides basic information.\n\
25539A value greater than 1 provides more verbose information."),
25540 NULL,
25541 NULL,
25542 &setdebuglist, &showdebuglist);
437afbb8 25543
cd4fb1b2
SM
25544 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
25545Set cross-checking of \"physname\" code against demangler."), _("\
25546Show cross-checking of \"physname\" code against demangler."), _("\
25547When enabled, GDB's internal \"physname\" code is checked against\n\
25548the demangler."),
25549 NULL, show_check_physname,
25550 &setdebuglist, &showdebuglist);
900e11f9 25551
e615022a
DE
25552 add_setshow_boolean_cmd ("use-deprecated-index-sections",
25553 no_class, &use_deprecated_index_sections, _("\
25554Set whether to use deprecated gdb_index sections."), _("\
25555Show whether to use deprecated gdb_index sections."), _("\
25556When enabled, deprecated .gdb_index sections are used anyway.\n\
25557Normally they are ignored either because of a missing feature or\n\
25558performance issue.\n\
25559Warning: This option must be enabled before gdb reads the file."),
25560 NULL,
25561 NULL,
25562 &setlist, &showlist);
25563
f1e6e072
TT
25564 dwarf2_locexpr_index = register_symbol_computed_impl (LOC_COMPUTED,
25565 &dwarf2_locexpr_funcs);
25566 dwarf2_loclist_index = register_symbol_computed_impl (LOC_COMPUTED,
25567 &dwarf2_loclist_funcs);
25568
25569 dwarf2_locexpr_block_index = register_symbol_block_impl (LOC_BLOCK,
25570 &dwarf2_block_frame_base_locexpr_funcs);
25571 dwarf2_loclist_block_index = register_symbol_block_impl (LOC_BLOCK,
25572 &dwarf2_block_frame_base_loclist_funcs);
c62446b1
PA
25573
25574#if GDB_SELF_TEST
25575 selftests::register_test ("dw2_expand_symtabs_matching",
25576 selftests::dw2_expand_symtabs_matching::run_test);
25577#endif
6502dd73 25578}
This page took 4.560591 seconds and 4 git commands to generate.